net/enic: support flow counter action
[dpdk.git] / drivers / net / enic / base / vnic_dev.c
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.
4  */
5
6 #include <rte_memzone.h>
7 #include <rte_memcpy.h>
8 #include <rte_string_fns.h>
9
10 #include "vnic_dev.h"
11 #include "vnic_resource.h"
12 #include "vnic_devcmd.h"
13 #include "vnic_nic.h"
14 #include "vnic_stats.h"
15
16
17 enum vnic_proxy_type {
18         PROXY_NONE,
19         PROXY_BY_BDF,
20         PROXY_BY_INDEX,
21 };
22
23 struct vnic_res {
24         void __iomem *vaddr;
25         dma_addr_t bus_addr;
26         unsigned int count;
27 };
28
29 struct vnic_intr_coal_timer_info {
30         u32 mul;
31         u32 div;
32         u32 max_usec;
33 };
34
35 struct vnic_dev {
36         void *priv;
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;
43         dma_addr_t notify_pa;
44         u32 notify_sz;
45         dma_addr_t linkstatus_pa;
46         struct vnic_stats *stats;
47         dma_addr_t stats_pa;
48         struct vnic_devcmd_fw_info *fw_info;
49         dma_addr_t fw_info_pa;
50         enum vnic_proxy_type proxy;
51         u32 proxy_index;
52         u64 args[VNIC_DEVCMD_NARGS];
53         int in_reset;
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;
62 };
63
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
68
69 #define VNIC_MAX_FLOW_COUNTERS 2048
70
71 void *vnic_dev_priv(struct vnic_dev *vdev)
72 {
73         return vdev->priv;
74 }
75
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))
82 {
83         vdev->alloc_consistent = alloc_consistent;
84         vdev->free_consistent = free_consistent;
85 }
86
87 static int vnic_dev_discover_res(struct vnic_dev *vdev,
88         struct vnic_dev_bar *bar, unsigned int num_bars)
89 {
90         struct vnic_resource_header __iomem *rh;
91         struct mgmt_barmap_hdr __iomem *mrh;
92         struct vnic_resource __iomem *r;
93         u8 type;
94
95         if (num_bars == 0)
96                 return -EINVAL;
97
98         if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
99                 pr_err("vNIC BAR0 res hdr length error\n");
100                 return -EINVAL;
101         }
102
103         rh  = bar->vaddr;
104         mrh = bar->vaddr;
105         if (!rh) {
106                 pr_err("vNIC BAR0 res hdr not mem-mapped\n");
107                 return -EINVAL;
108         }
109
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));
120                         return -EINVAL;
121                 }
122         }
123
124         if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
125                 r = (struct vnic_resource __iomem *)(mrh + 1);
126         else
127                 r = (struct vnic_resource __iomem *)(rh + 1);
128
129
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);
134                 u32 len;
135
136                 r++;
137
138                 if (bar_num >= num_bars)
139                         continue;
140
141                 if (!bar[bar_num].len || !bar[bar_num].vaddr)
142                         continue;
143
144                 switch (type) {
145                 case RES_TYPE_WQ:
146                 case RES_TYPE_RQ:
147                 case RES_TYPE_CQ:
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",
155                                         type, bar_offset,
156                                         len,
157                                         bar[bar_num].len);
158                                 return -EINVAL;
159                         }
160                         break;
161                 case RES_TYPE_INTR_PBA_LEGACY:
162                 case RES_TYPE_DEVCMD:
163                         len = count;
164                         break;
165                 default:
166                         continue;
167                 }
168
169                 vdev->res[type].count = count;
170                 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
171                     bar_offset;
172                 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
173         }
174
175         return 0;
176 }
177
178 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
179         enum vnic_res_type type)
180 {
181         return vdev->res[type].count;
182 }
183
184 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
185         unsigned int index)
186 {
187         if (!vdev->res[type].vaddr)
188                 return NULL;
189
190         switch (type) {
191         case RES_TYPE_WQ:
192         case RES_TYPE_RQ:
193         case RES_TYPE_CQ:
194         case RES_TYPE_INTR_CTRL:
195                 return (char __iomem *)vdev->res[type].vaddr +
196                         index * VNIC_RES_STRIDE;
197         default:
198                 return (char __iomem *)vdev->res[type].vaddr;
199         }
200 }
201
202 unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
203         unsigned int desc_count, unsigned int desc_size)
204 {
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.
209          */
210
211         unsigned int count_align = 32;
212         unsigned int desc_align = 16;
213
214         ring->base_align = 512;
215
216         if (desc_count == 0)
217                 desc_count = 4096;
218
219         ring->desc_count = VNIC_ALIGN(desc_count, count_align);
220
221         ring->desc_size = VNIC_ALIGN(desc_size, desc_align);
222
223         ring->size = ring->desc_count * ring->desc_size;
224         ring->size_unaligned = ring->size + ring->base_align;
225
226         return ring->size_unaligned;
227 }
228
229 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
230 {
231         memset(ring->descs, 0, ring->size);
232 }
233
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,
238         char *z_name)
239 {
240         void *alloc_addr;
241         dma_addr_t alloc_pa = 0;
242
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);
247         if (!alloc_addr) {
248                 pr_err("Failed to allocate ring (size=%d), aborting\n",
249                         (int)ring->size);
250                 return -ENOMEM;
251         }
252         ring->descs_unaligned = alloc_addr;
253         if (!alloc_pa) {
254                 pr_err("Failed to map allocated ring (size=%d), aborting\n",
255                         (int)ring->size);
256                 vdev->free_consistent(vdev->priv,
257                                       ring->size_unaligned,
258                                       alloc_addr,
259                                       alloc_pa);
260                 return -ENOMEM;
261         }
262         ring->base_addr_unaligned = alloc_pa;
263
264         ring->base_addr = VNIC_ALIGN(ring->base_addr_unaligned,
265                 ring->base_align);
266         ring->descs = (u8 *)ring->descs_unaligned +
267             (ring->base_addr - ring->base_addr_unaligned);
268
269         vnic_dev_clear_desc_ring(ring);
270
271         ring->desc_avail = ring->desc_count - 1;
272
273         return 0;
274 }
275
276 void vnic_dev_free_desc_ring(__attribute__((unused))  struct vnic_dev *vdev,
277         struct vnic_dev_ring *ring)
278 {
279         if (ring->descs) {
280                 vdev->free_consistent(vdev->priv,
281                                       ring->size_unaligned,
282                                       ring->descs_unaligned,
283                                       ring->base_addr_unaligned);
284                 ring->descs = NULL;
285         }
286 }
287
288 static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
289         int wait)
290 {
291         struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
292         unsigned int i;
293         int delay;
294         u32 status;
295         int err;
296
297         status = ioread32(&devcmd->status);
298         if (status == 0xFFFFFFFF) {
299                 /* PCI-e target device is gone */
300                 return -ENODEV;
301         }
302         if (status & STAT_BUSY) {
303
304                 pr_err("Busy devcmd %d\n",  _CMD_N(cmd));
305                 return -EBUSY;
306         }
307
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 */
312         }
313
314         iowrite32(cmd, &devcmd->cmd);
315
316         if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
317                 return 0;
318
319         for (delay = 0; delay < wait; delay++) {
320
321                 udelay(100);
322
323                 status = ioread32(&devcmd->status);
324                 if (status == 0xFFFFFFFF) {
325                         /* PCI-e target device is gone */
326                         return -ENODEV;
327                 }
328
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",
335                                                 _CMD_N(cmd), err);
336                                 return err;
337                         }
338
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]);
343                         }
344
345                         return 0;
346                 }
347         }
348
349         pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
350         return -ETIMEDOUT;
351 }
352
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)
356 {
357         u32 status;
358         int err;
359
360         /*
361          * Proxy command consumes 2 arguments. One for proxy index,
362          * the other is for command to be proxied
363          */
364         if (nargs > VNIC_DEVCMD_NARGS - 2) {
365                 pr_err("number of args %d exceeds the maximum\n", nargs);
366                 return -EINVAL;
367         }
368         memset(vdev->args, 0, sizeof(vdev->args));
369
370         vdev->args[0] = vdev->proxy_index;
371         vdev->args[1] = cmd;
372         memcpy(&vdev->args[2], args, nargs * sizeof(args[0]));
373
374         err = _vnic_dev_cmd(vdev, proxy_cmd, wait);
375         if (err)
376                 return err;
377
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));
384                 return err;
385         }
386
387         memcpy(args, &vdev->args[1], nargs * sizeof(args[0]));
388
389         return 0;
390 }
391
392 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
393         enum vnic_devcmd_cmd cmd, u64 *args, int nargs, int wait)
394 {
395         int err;
396
397         if (nargs > VNIC_DEVCMD_NARGS) {
398                 pr_err("number of args %d exceeds the maximum\n", nargs);
399                 return -EINVAL;
400         }
401         memset(vdev->args, 0, sizeof(vdev->args));
402         memcpy(vdev->args, args, nargs * sizeof(args[0]));
403
404         err = _vnic_dev_cmd(vdev, cmd, wait);
405
406         memcpy(args, vdev->args, nargs * sizeof(args[0]));
407
408         return err;
409 }
410
411 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
412         u64 *a0, u64 *a1, int wait)
413 {
414         u64 args[2];
415         int err;
416
417         args[0] = *a0;
418         args[1] = *a1;
419         memset(vdev->args, 0, sizeof(vdev->args));
420
421         switch (vdev->proxy) {
422         case PROXY_BY_INDEX:
423                 err =  vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
424                                 args, ARRAY_SIZE(args), wait);
425                 break;
426         case PROXY_BY_BDF:
427                 err =  vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
428                                 args, ARRAY_SIZE(args), wait);
429                 break;
430         case PROXY_NONE:
431         default:
432                 err = vnic_dev_cmd_no_proxy(vdev, cmd, args, 2, wait);
433                 break;
434         }
435
436         if (err == 0) {
437                 *a0 = args[0];
438                 *a1 = args[1];
439         }
440
441         return err;
442 }
443
444 int vnic_dev_cmd_args(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
445                       u64 *args, int nargs, int wait)
446 {
447         switch (vdev->proxy) {
448         case PROXY_BY_INDEX:
449                 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
450                                 args, nargs, wait);
451         case PROXY_BY_BDF:
452                 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
453                                 args, nargs, wait);
454         case PROXY_NONE:
455         default:
456                 return vnic_dev_cmd_no_proxy(vdev, cmd, args, nargs, wait);
457         }
458 }
459
460 static int vnic_dev_advanced_filters_cap(struct vnic_dev *vdev, u64 *args,
461                 int nargs)
462 {
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);
467 }
468
469 int vnic_dev_capable_adv_filters(struct vnic_dev *vdev)
470 {
471         u64 a0 = CMD_ADD_ADV_FILTER, a1 = 0;
472         int wait = 1000;
473         int err;
474
475         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
476         if (err)
477                 return 0;
478         return (a1 >= (u32)FILTER_DPDK_1);
479 }
480
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
490  */
491 int vnic_dev_capable_filter_mode(struct vnic_dev *vdev, u32 *mode,
492                                  u8 *filter_actions)
493 {
494         u64 args[4];
495         int err;
496         u32 max_level = 0;
497
498         err = vnic_dev_advanced_filters_cap(vdev, args, 4);
499
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];
504
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.
508                  */
509                 args[0] = CMD_ADD_FILTER;
510                 args[1] = 0;
511                 err = vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, 2, 1000);
512                 if (err)
513                         return err;
514                 max_level = args[1];
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;
524                 return 0;
525         }
526         max_level = args[1];
527 parse_max_level:
528         if (max_level >= (u32)FILTER_USNIC_IP)
529                 *mode = FILTER_USNIC_IP;
530         else
531                 *mode = FILTER_IPV4_5TUPLE;
532         return 0;
533 }
534
535 void vnic_dev_capable_udp_rss_weak(struct vnic_dev *vdev, bool *cfg_chk,
536                                    bool *weak)
537 {
538         u64 a0 = CMD_NIC_CFG, a1 = 0;
539         int wait = 1000;
540         int err;
541
542         *cfg_chk = false;
543         *weak = false;
544         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
545         if (err == 0 && a0 != 0 && a1 != 0) {
546                 *cfg_chk = true;
547                 *weak = !!((a1 >> 32) & CMD_NIC_CFG_CAPF_UDP_WEAK);
548         }
549 }
550
551 int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
552 {
553         u64 a0 = (u32)cmd, a1 = 0;
554         int wait = 1000;
555         int err;
556
557         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
558
559         return !(err || a0);
560 }
561
562 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, size_t size,
563         void *value)
564 {
565         u64 a0, a1;
566         int wait = 1000;
567         int err;
568
569         a0 = offset;
570         a1 = size;
571
572         err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
573
574         switch (size) {
575         case 1:
576                 *(u8 *)value = (u8)a0;
577                 break;
578         case 2:
579                 *(u16 *)value = (u16)a0;
580                 break;
581         case 4:
582                 *(u32 *)value = (u32)a0;
583                 break;
584         case 8:
585                 *(u64 *)value = a0;
586                 break;
587         default:
588                 BUG();
589                 break;
590         }
591
592         return err;
593 }
594
595 int vnic_dev_stats_clear(struct vnic_dev *vdev)
596 {
597         u64 a0 = 0, a1 = 0;
598         int wait = 1000;
599
600         return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
601 }
602
603 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
604 {
605         u64 a0, a1;
606         int wait = 1000;
607
608         if (!vdev->stats)
609                 return -ENOMEM;
610
611         *stats = vdev->stats;
612         a0 = vdev->stats_pa;
613         a1 = sizeof(struct vnic_stats);
614
615         return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
616 }
617
618 /*
619  * Configure counter DMA
620  */
621 int vnic_dev_counter_dma_cfg(struct vnic_dev *vdev, u32 period, u32 counter_idx)
622 {
623         u64 args[3];
624         int wait = 1000;
625
626         if (!vdev->flow_counters || counter_idx >= VNIC_MAX_FLOW_COUNTERS)
627                 return -ENOMEM;
628
629         args[0] = counter_idx + 1;
630         args[1] = vdev->flow_counters_pa;
631         args[2] = period;
632         return vnic_dev_cmd_args(vdev, CMD_COUNTER_DMA_CONFIG, args, 3, wait);
633 }
634
635 int vnic_dev_close(struct vnic_dev *vdev)
636 {
637         u64 a0 = 0, a1 = 0;
638         int wait = 1000;
639
640         return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
641 }
642
643 int vnic_dev_enable_wait(struct vnic_dev *vdev)
644 {
645         u64 a0 = 0, a1 = 0;
646         int wait = 1000;
647
648         if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
649                 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
650         else
651                 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
652 }
653
654 int vnic_dev_disable(struct vnic_dev *vdev)
655 {
656         u64 a0 = 0, a1 = 0;
657         int wait = 1000;
658
659         return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
660 }
661
662 int vnic_dev_open(struct vnic_dev *vdev, int arg)
663 {
664         u64 a0 = (u32)arg, a1 = 0;
665         int wait = 1000;
666
667         return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
668 }
669
670 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
671 {
672         u64 a0 = 0, a1 = 0;
673         int wait = 1000;
674         int err;
675
676         *done = 0;
677
678         err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
679         if (err)
680                 return err;
681
682         *done = (a0 == 0);
683
684         return 0;
685 }
686
687 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
688 {
689         u64 a0 = 0, a1 = 0;
690         int wait = 1000;
691         int err, i;
692
693         for (i = 0; i < ETH_ALEN; i++)
694                 mac_addr[i] = 0;
695
696         err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
697         if (err)
698                 return err;
699
700         for (i = 0; i < ETH_ALEN; i++)
701                 mac_addr[i] = ((u8 *)&a0)[i];
702
703         return 0;
704 }
705
706 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
707         int broadcast, int promisc, int allmulti)
708 {
709         u64 a0, a1 = 0;
710         int wait = 1000;
711         int err;
712
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);
718
719         err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
720         if (err)
721                 pr_err("Can't set packet filter\n");
722
723         return err;
724 }
725
726 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
727 {
728         u64 a0 = 0, a1 = 0;
729         int wait = 1000;
730         int err;
731         int i;
732
733         for (i = 0; i < ETH_ALEN; i++)
734                 ((u8 *)&a0)[i] = addr[i];
735
736         err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
737         if (err)
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],
740                         err);
741
742         return err;
743 }
744
745 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
746 {
747         u64 a0 = 0, a1 = 0;
748         int wait = 1000;
749         int err;
750         int i;
751
752         for (i = 0; i < ETH_ALEN; i++)
753                 ((u8 *)&a0)[i] = addr[i];
754
755         err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
756         if (err)
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],
759                         err);
760
761         return err;
762 }
763
764 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
765         u8 ig_vlan_rewrite_mode)
766 {
767         u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
768         int wait = 1000;
769
770         if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
771                 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
772                                 &a0, &a1, wait);
773         else
774                 return 0;
775 }
776
777 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state)
778 {
779         vdev->in_reset = state;
780 }
781
782 static inline int vnic_dev_in_reset(struct vnic_dev *vdev)
783 {
784         return vdev->in_reset;
785 }
786
787 int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
788         void *notify_addr, dma_addr_t notify_pa, u16 intr)
789 {
790         u64 a0, a1;
791         int wait = 1000;
792         int r;
793
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;
798         }
799
800         a0 = (u64)notify_pa;
801         a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
802         a1 += sizeof(struct vnic_devcmd_notify);
803
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;
807
808         return r;
809 }
810
811 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
812 {
813         void *notify_addr = NULL;
814         dma_addr_t notify_pa = 0;
815         char name[NAME_MAX];
816         static u32 instance;
817
818         if (vdev->notify || vdev->notify_pa) {
819                 return vnic_dev_notify_setcmd(vdev, vdev->notify,
820                                               vdev->notify_pa, intr);
821         }
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                         &notify_pa, (u8 *)name);
828                 if (!notify_addr)
829                         return -ENOMEM;
830         }
831
832         return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
833 }
834
835 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
836 {
837         u64 a0, a1;
838         int wait = 1000;
839         int err;
840
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);
844
845         err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
846         if (!vnic_dev_in_reset(vdev)) {
847                 vdev->notify = NULL;
848                 vdev->notify_pa = 0;
849                 vdev->notify_sz = 0;
850         }
851
852         return err;
853 }
854
855 int vnic_dev_notify_unset(struct vnic_dev *vdev)
856 {
857         if (vdev->notify && !vnic_dev_in_reset(vdev)) {
858                 vdev->free_consistent(vdev->priv,
859                         sizeof(struct vnic_devcmd_notify),
860                         vdev->notify,
861                         vdev->notify_pa);
862         }
863
864         return vnic_dev_notify_unsetcmd(vdev);
865 }
866
867 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
868 {
869         u32 *words;
870         unsigned int nwords = vdev->notify_sz / 4;
871         unsigned int i;
872         u32 csum;
873
874         if (!vdev->notify || !vdev->notify_sz)
875                 return 0;
876
877         do {
878                 csum = 0;
879                 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
880                 words = (u32 *)&vdev->notify_copy;
881                 for (i = 1; i < nwords; i++)
882                         csum += words[i];
883         } while (csum != words[0]);
884
885         return 1;
886 }
887
888 int vnic_dev_init(struct vnic_dev *vdev, int arg)
889 {
890         u64 a0 = (u32)arg, a1 = 0;
891         int wait = 1000;
892         int r = 0;
893
894         if (vnic_dev_capable(vdev, CMD_INIT))
895                 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
896         else {
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_*.
901                          */
902                         vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
903                         vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
904                 }
905         }
906         return r;
907 }
908
909 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
910 {
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);
916 }
917
918 int vnic_dev_link_status(struct vnic_dev *vdev)
919 {
920         if (!vnic_dev_notify_ready(vdev))
921                 return 0;
922
923         return vdev->notify_copy.link_state;
924 }
925
926 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
927 {
928         if (!vnic_dev_notify_ready(vdev))
929                 return 0;
930
931         return vdev->notify_copy.port_speed;
932 }
933
934 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
935 {
936         return (usec * vdev->intr_coal_timer_info.mul) /
937                 vdev->intr_coal_timer_info.div;
938 }
939
940 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
941 {
942         return (hw_cycles * vdev->intr_coal_timer_info.div) /
943                 vdev->intr_coal_timer_info.mul;
944 }
945
946 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
947 {
948         return vdev->intr_coal_timer_info.max_usec;
949 }
950
951 int vnic_dev_alloc_stats_mem(struct vnic_dev *vdev)
952 {
953         char name[NAME_MAX];
954         static u32 instance;
955
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;
961 }
962
963 /*
964  * Initialize for up to VNIC_MAX_FLOW_COUNTERS
965  */
966 int vnic_dev_alloc_counter_mem(struct vnic_dev *vdev)
967 {
968         char name[NAME_MAX];
969         static u32 instance;
970
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,
976                                              (u8 *)name);
977         return vdev->flow_counters == NULL ? -ENOMEM : 0;
978 }
979
980 void vnic_dev_unregister(struct vnic_dev *vdev)
981 {
982         if (vdev) {
983                 if (vdev->notify)
984                         vdev->free_consistent(vdev->priv,
985                                 sizeof(struct vnic_devcmd_notify),
986                                 vdev->notify,
987                                 vdev->notify_pa);
988                 if (vdev->stats)
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);
995
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);
1000                 }
1001                 if (vdev->fw_info)
1002                         vdev->free_consistent(vdev->priv,
1003                                 sizeof(struct vnic_devcmd_fw_info),
1004                                 vdev->fw_info, vdev->fw_info_pa);
1005                 rte_free(vdev);
1006         }
1007 }
1008
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)
1012 {
1013         if (!vdev) {
1014                 char name[NAME_MAX];
1015                 snprintf((char *)name, sizeof(name), "%s-vnic",
1016                           pdev->device.name);
1017                 vdev = (struct vnic_dev *)rte_zmalloc_socket(name,
1018                                         sizeof(struct vnic_dev),
1019                                         RTE_CACHE_LINE_SIZE,
1020                                         pdev->device.numa_node);
1021                 if (!vdev)
1022                         return NULL;
1023         }
1024
1025         vdev->priv = priv;
1026         vdev->pdev = pdev;
1027
1028         if (vnic_dev_discover_res(vdev, bar, num_bars))
1029                 goto err_out;
1030
1031         vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
1032         if (!vdev->devcmd)
1033                 goto err_out;
1034
1035         return vdev;
1036
1037 err_out:
1038         vnic_dev_unregister(vdev);
1039         return NULL;
1040 }
1041
1042 /*
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
1048  *          variable.
1049  *          This function stores the filter_id returned by the
1050  *          firmware in the same variable before return;
1051  *
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
1056  */
1057 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
1058         struct filter_v2 *data, struct filter_action_v2 *action_v2)
1059 {
1060         u64 a0 = 0, a1 = 0;
1061         int wait = 1000;
1062         dma_addr_t tlv_pa;
1063         int ret = -EINVAL;
1064         struct filter_tlv *tlv, *tlv_va;
1065         u64 tlv_size;
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;
1070
1071         if (cmd == CLSF_ADD) {
1072                 dev_cmd = (data->type >= FILTER_DPDK_1) ?
1073                           CMD_ADD_ADV_FILTER : CMD_ADD_FILTER;
1074
1075                 filter_size = vnic_filter_size(data);
1076                 action_size = vnic_action_size(action_v2);
1077
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);
1084                 if (!tlv_va)
1085                         return -ENOMEM;
1086                 tlv = tlv_va;
1087                 a0 = tlv_pa;
1088                 a1 = tlv_size;
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);
1093
1094                 tlv = (struct filter_tlv *)((char *)tlv +
1095                                          sizeof(struct filter_tlv) +
1096                                          filter_size);
1097
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);
1102                 *entry = (u16)a0;
1103                 vdev->free_consistent(vdev->priv, tlv_size, tlv_va, tlv_pa);
1104         } else if (cmd == CLSF_DEL) {
1105                 a0 = *entry;
1106                 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);
1107         }
1108
1109         return ret;
1110 }
1111
1112 int vnic_dev_overlay_offload_ctrl(struct vnic_dev *vdev, u8 overlay, u8 config)
1113 {
1114         u64 a0 = overlay;
1115         u64 a1 = config;
1116         int wait = 1000;
1117
1118         return vnic_dev_cmd(vdev, CMD_OVERLAY_OFFLOAD_CTRL, &a0, &a1, wait);
1119 }
1120
1121 int vnic_dev_overlay_offload_cfg(struct vnic_dev *vdev, u8 overlay,
1122                                  u16 vxlan_udp_port_number)
1123 {
1124         u64 a1 = vxlan_udp_port_number;
1125         u64 a0 = overlay;
1126         int wait = 1000;
1127
1128         return vnic_dev_cmd(vdev, CMD_OVERLAY_OFFLOAD_CFG, &a0, &a1, wait);
1129 }
1130
1131 int vnic_dev_capable_vxlan(struct vnic_dev *vdev)
1132 {
1133         u64 a0 = VIC_FEATURE_VXLAN;
1134         u64 a1 = 0;
1135         int wait = 1000;
1136         int ret;
1137
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 */
1140         return ret == 0 &&
1141                 (a1 & (FEATURE_VXLAN_IPV6 | FEATURE_VXLAN_MULTI_WQ)) ==
1142                 (FEATURE_VXLAN_IPV6 | FEATURE_VXLAN_MULTI_WQ);
1143 }
1144
1145 bool vnic_dev_counter_alloc(struct vnic_dev *vdev, uint32_t *idx)
1146 {
1147         u64 a0 = 0;
1148         u64 a1 = 0;
1149         int wait = 1000;
1150
1151         if (vnic_dev_cmd(vdev, CMD_COUNTER_ALLOC, &a0, &a1, wait))
1152                 return false;
1153         *idx = (uint32_t)a0;
1154         return true;
1155 }
1156
1157 bool vnic_dev_counter_free(struct vnic_dev *vdev, uint32_t idx)
1158 {
1159         u64 a0 = idx;
1160         u64 a1 = 0;
1161         int wait = 1000;
1162
1163         return vnic_dev_cmd(vdev, CMD_COUNTER_FREE, &a0, &a1,
1164                             wait) == 0;
1165 }
1166
1167 bool vnic_dev_counter_query(struct vnic_dev *vdev, uint32_t idx,
1168                             bool reset, uint64_t *packets, uint64_t *bytes)
1169 {
1170         u64 a0 = idx;
1171         u64 a1 = reset ? 1 : 0;
1172         int wait = 1000;
1173
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;
1178                 if (reset)
1179                         if (vnic_dev_cmd(vdev, CMD_COUNTER_QUERY, &a0, &a1,
1180                             wait))
1181                                 return false;
1182         } else {
1183                 if (vnic_dev_cmd(vdev, CMD_COUNTER_QUERY, &a0, &a1, wait))
1184                         return false;
1185                 *packets = a0;
1186                 *bytes = a1;
1187         }
1188         return true;
1189 }