New upstream version 18.11-rc1
[deb_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         u8 flow_counters_dma_active;
63 };
64
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
69
70 #define VNIC_MAX_FLOW_COUNTERS 2048
71
72 void *vnic_dev_priv(struct vnic_dev *vdev)
73 {
74         return vdev->priv;
75 }
76
77 void vnic_register_cbacks(struct vnic_dev *vdev,
78         void *(*alloc_consistent)(void *priv, size_t size,
79             dma_addr_t *dma_handle, u8 *name),
80         void (*free_consistent)(void *priv,
81             size_t size, void *vaddr,
82             dma_addr_t dma_handle))
83 {
84         vdev->alloc_consistent = alloc_consistent;
85         vdev->free_consistent = free_consistent;
86 }
87
88 static int vnic_dev_discover_res(struct vnic_dev *vdev,
89         struct vnic_dev_bar *bar, unsigned int num_bars)
90 {
91         struct vnic_resource_header __iomem *rh;
92         struct mgmt_barmap_hdr __iomem *mrh;
93         struct vnic_resource __iomem *r;
94         u8 type;
95
96         if (num_bars == 0)
97                 return -EINVAL;
98
99         if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
100                 pr_err("vNIC BAR0 res hdr length error\n");
101                 return -EINVAL;
102         }
103
104         rh  = bar->vaddr;
105         mrh = bar->vaddr;
106         if (!rh) {
107                 pr_err("vNIC BAR0 res hdr not mem-mapped\n");
108                 return -EINVAL;
109         }
110
111         /* Check for mgmt vnic in addition to normal vnic */
112         if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) ||
113                 (ioread32(&rh->version) != VNIC_RES_VERSION)) {
114                 if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) ||
115                         (ioread32(&mrh->version) != MGMTVNIC_VERSION)) {
116                         pr_err("vNIC BAR0 res magic/version error " \
117                                 "exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n",
118                                 VNIC_RES_MAGIC, VNIC_RES_VERSION,
119                                 MGMTVNIC_MAGIC, MGMTVNIC_VERSION,
120                                 ioread32(&rh->magic), ioread32(&rh->version));
121                         return -EINVAL;
122                 }
123         }
124
125         if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
126                 r = (struct vnic_resource __iomem *)(mrh + 1);
127         else
128                 r = (struct vnic_resource __iomem *)(rh + 1);
129
130
131         while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
132                 u8 bar_num = ioread8(&r->bar);
133                 u32 bar_offset = ioread32(&r->bar_offset);
134                 u32 count = ioread32(&r->count);
135                 u32 len;
136
137                 r++;
138
139                 if (bar_num >= num_bars)
140                         continue;
141
142                 if (!bar[bar_num].len || !bar[bar_num].vaddr)
143                         continue;
144
145                 switch (type) {
146                 case RES_TYPE_WQ:
147                 case RES_TYPE_RQ:
148                 case RES_TYPE_CQ:
149                 case RES_TYPE_INTR_CTRL:
150                         /* each count is stride bytes long */
151                         len = count * VNIC_RES_STRIDE;
152                         if (len + bar_offset > bar[bar_num].len) {
153                                 pr_err("vNIC BAR0 resource %d " \
154                                         "out-of-bounds, offset 0x%x + " \
155                                         "size 0x%x > bar len 0x%lx\n",
156                                         type, bar_offset,
157                                         len,
158                                         bar[bar_num].len);
159                                 return -EINVAL;
160                         }
161                         break;
162                 case RES_TYPE_INTR_PBA_LEGACY:
163                 case RES_TYPE_DEVCMD:
164                         len = count;
165                         break;
166                 default:
167                         continue;
168                 }
169
170                 vdev->res[type].count = count;
171                 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
172                     bar_offset;
173                 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
174         }
175
176         return 0;
177 }
178
179 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
180         enum vnic_res_type type)
181 {
182         return vdev->res[type].count;
183 }
184
185 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
186         unsigned int index)
187 {
188         if (!vdev->res[type].vaddr)
189                 return NULL;
190
191         switch (type) {
192         case RES_TYPE_WQ:
193         case RES_TYPE_RQ:
194         case RES_TYPE_CQ:
195         case RES_TYPE_INTR_CTRL:
196                 return (char __iomem *)vdev->res[type].vaddr +
197                         index * VNIC_RES_STRIDE;
198         default:
199                 return (char __iomem *)vdev->res[type].vaddr;
200         }
201 }
202
203 unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
204         unsigned int desc_count, unsigned int desc_size)
205 {
206         /* The base address of the desc rings must be 512 byte aligned.
207          * Descriptor count is aligned to groups of 32 descriptors.  A
208          * count of 0 means the maximum 4096 descriptors.  Descriptor
209          * size is aligned to 16 bytes.
210          */
211
212         unsigned int count_align = 32;
213         unsigned int desc_align = 16;
214
215         ring->base_align = 512;
216
217         if (desc_count == 0)
218                 desc_count = 4096;
219
220         ring->desc_count = VNIC_ALIGN(desc_count, count_align);
221
222         ring->desc_size = VNIC_ALIGN(desc_size, desc_align);
223
224         ring->size = ring->desc_count * ring->desc_size;
225         ring->size_unaligned = ring->size + ring->base_align;
226
227         return ring->size_unaligned;
228 }
229
230 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
231 {
232         memset(ring->descs, 0, ring->size);
233 }
234
235 int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev,
236         struct vnic_dev_ring *ring,
237         unsigned int desc_count, unsigned int desc_size,
238         __attribute__((unused)) unsigned int socket_id,
239         char *z_name)
240 {
241         void *alloc_addr;
242         dma_addr_t alloc_pa = 0;
243
244         vnic_dev_desc_ring_size(ring, desc_count, desc_size);
245         alloc_addr = vdev->alloc_consistent(vdev->priv,
246                                             ring->size_unaligned,
247                                             &alloc_pa, (u8 *)z_name);
248         if (!alloc_addr) {
249                 pr_err("Failed to allocate ring (size=%d), aborting\n",
250                         (int)ring->size);
251                 return -ENOMEM;
252         }
253         ring->descs_unaligned = alloc_addr;
254         if (!alloc_pa) {
255                 pr_err("Failed to map allocated ring (size=%d), aborting\n",
256                         (int)ring->size);
257                 vdev->free_consistent(vdev->priv,
258                                       ring->size_unaligned,
259                                       alloc_addr,
260                                       alloc_pa);
261                 return -ENOMEM;
262         }
263         ring->base_addr_unaligned = alloc_pa;
264
265         ring->base_addr = VNIC_ALIGN(ring->base_addr_unaligned,
266                 ring->base_align);
267         ring->descs = (u8 *)ring->descs_unaligned +
268             (ring->base_addr - ring->base_addr_unaligned);
269
270         vnic_dev_clear_desc_ring(ring);
271
272         ring->desc_avail = ring->desc_count - 1;
273
274         return 0;
275 }
276
277 void vnic_dev_free_desc_ring(__attribute__((unused))  struct vnic_dev *vdev,
278         struct vnic_dev_ring *ring)
279 {
280         if (ring->descs) {
281                 vdev->free_consistent(vdev->priv,
282                                       ring->size_unaligned,
283                                       ring->descs_unaligned,
284                                       ring->base_addr_unaligned);
285                 ring->descs = NULL;
286         }
287 }
288
289 static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
290         int wait)
291 {
292         struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
293         unsigned int i;
294         int delay;
295         u32 status;
296         int err;
297
298         status = ioread32(&devcmd->status);
299         if (status == 0xFFFFFFFF) {
300                 /* PCI-e target device is gone */
301                 return -ENODEV;
302         }
303         if (status & STAT_BUSY) {
304
305                 pr_err("Busy devcmd %d\n",  _CMD_N(cmd));
306                 return -EBUSY;
307         }
308
309         if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
310                 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
311                         writeq(vdev->args[i], &devcmd->args[i]);
312                 wmb(); /* complete all writes initiated till now */
313         }
314
315         iowrite32(cmd, &devcmd->cmd);
316
317         if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
318                 return 0;
319
320         for (delay = 0; delay < wait; delay++) {
321
322                 udelay(100);
323
324                 status = ioread32(&devcmd->status);
325                 if (status == 0xFFFFFFFF) {
326                         /* PCI-e target device is gone */
327                         return -ENODEV;
328                 }
329
330                 if (!(status & STAT_BUSY)) {
331                         if (status & STAT_ERROR) {
332                                 err = -(int)readq(&devcmd->args[0]);
333                                 if (cmd != CMD_CAPABILITY)
334                                         pr_err("Devcmd %d failed " \
335                                                 "with error code %d\n",
336                                                 _CMD_N(cmd), err);
337                                 return err;
338                         }
339
340                         if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
341                                 rmb();/* finish all reads initiated till now */
342                                 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
343                                         vdev->args[i] = readq(&devcmd->args[i]);
344                         }
345
346                         return 0;
347                 }
348         }
349
350         pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
351         return -ETIMEDOUT;
352 }
353
354 static int vnic_dev_cmd_proxy(struct vnic_dev *vdev,
355         enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd,
356         u64 *args, int nargs, int wait)
357 {
358         u32 status;
359         int err;
360
361         /*
362          * Proxy command consumes 2 arguments. One for proxy index,
363          * the other is for command to be proxied
364          */
365         if (nargs > VNIC_DEVCMD_NARGS - 2) {
366                 pr_err("number of args %d exceeds the maximum\n", nargs);
367                 return -EINVAL;
368         }
369         memset(vdev->args, 0, sizeof(vdev->args));
370
371         vdev->args[0] = vdev->proxy_index;
372         vdev->args[1] = cmd;
373         memcpy(&vdev->args[2], args, nargs * sizeof(args[0]));
374
375         err = _vnic_dev_cmd(vdev, proxy_cmd, wait);
376         if (err)
377                 return err;
378
379         status = (u32)vdev->args[0];
380         if (status & STAT_ERROR) {
381                 err = (int)vdev->args[1];
382                 if (err != ERR_ECMDUNKNOWN ||
383                     cmd != CMD_CAPABILITY)
384                         pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd));
385                 return err;
386         }
387
388         memcpy(args, &vdev->args[1], nargs * sizeof(args[0]));
389
390         return 0;
391 }
392
393 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
394         enum vnic_devcmd_cmd cmd, u64 *args, int nargs, int wait)
395 {
396         int err;
397
398         if (nargs > VNIC_DEVCMD_NARGS) {
399                 pr_err("number of args %d exceeds the maximum\n", nargs);
400                 return -EINVAL;
401         }
402         memset(vdev->args, 0, sizeof(vdev->args));
403         memcpy(vdev->args, args, nargs * sizeof(args[0]));
404
405         err = _vnic_dev_cmd(vdev, cmd, wait);
406
407         memcpy(args, vdev->args, nargs * sizeof(args[0]));
408
409         return err;
410 }
411
412 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
413         u64 *a0, u64 *a1, int wait)
414 {
415         u64 args[2];
416         int err;
417
418         args[0] = *a0;
419         args[1] = *a1;
420         memset(vdev->args, 0, sizeof(vdev->args));
421
422         switch (vdev->proxy) {
423         case PROXY_BY_INDEX:
424                 err =  vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
425                                 args, ARRAY_SIZE(args), wait);
426                 break;
427         case PROXY_BY_BDF:
428                 err =  vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
429                                 args, ARRAY_SIZE(args), wait);
430                 break;
431         case PROXY_NONE:
432         default:
433                 err = vnic_dev_cmd_no_proxy(vdev, cmd, args, 2, wait);
434                 break;
435         }
436
437         if (err == 0) {
438                 *a0 = args[0];
439                 *a1 = args[1];
440         }
441
442         return err;
443 }
444
445 int vnic_dev_cmd_args(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
446                       u64 *args, int nargs, int wait)
447 {
448         switch (vdev->proxy) {
449         case PROXY_BY_INDEX:
450                 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
451                                 args, nargs, wait);
452         case PROXY_BY_BDF:
453                 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
454                                 args, nargs, wait);
455         case PROXY_NONE:
456         default:
457                 return vnic_dev_cmd_no_proxy(vdev, cmd, args, nargs, wait);
458         }
459 }
460
461 static int vnic_dev_advanced_filters_cap(struct vnic_dev *vdev, u64 *args,
462                 int nargs)
463 {
464         memset(args, 0, nargs * sizeof(*args));
465         args[0] = CMD_ADD_ADV_FILTER;
466         args[1] = FILTER_CAP_MODE_V1_FLAG;
467         return vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, nargs, 1000);
468 }
469
470 int vnic_dev_capable_adv_filters(struct vnic_dev *vdev)
471 {
472         u64 a0 = CMD_ADD_ADV_FILTER, a1 = 0;
473         int wait = 1000;
474         int err;
475
476         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
477         if (err)
478                 return 0;
479         return (a1 >= (u32)FILTER_DPDK_1);
480 }
481
482 /*  Determine the "best" filtering mode VIC is capaible of. Returns one of 3
483  *  value or 0 on error:
484  *      FILTER_DPDK_1- advanced filters availabile
485  *      FILTER_USNIC_IP_FLAG - advanced filters but with the restriction that
486  *              the IP layer must explicitly specified. I.e. cannot have a UDP
487  *              filter that matches both IPv4 and IPv6.
488  *      FILTER_IPV4_5TUPLE - fallback if either of the 2 above aren't available.
489  *              all other filter types are not available.
490  *   Retrun true in filter_tags if supported
491  */
492 int vnic_dev_capable_filter_mode(struct vnic_dev *vdev, u32 *mode,
493                                  u8 *filter_actions)
494 {
495         u64 args[4];
496         int err;
497         u32 max_level = 0;
498
499         err = vnic_dev_advanced_filters_cap(vdev, args, 4);
500
501         /* determine supported filter actions */
502         *filter_actions = FILTER_ACTION_RQ_STEERING_FLAG; /* always available */
503         if (args[2] == FILTER_CAP_MODE_V1)
504                 *filter_actions = args[3];
505
506         if (err || ((args[0] == 1) && (args[1] == 0))) {
507                 /* Adv filter Command not supported or adv filters available but
508                  * not enabled. Try the normal filter capability command.
509                  */
510                 args[0] = CMD_ADD_FILTER;
511                 args[1] = 0;
512                 err = vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, 2, 1000);
513                 if (err)
514                         return err;
515                 max_level = args[1];
516                 goto parse_max_level;
517         } else if (args[2] == FILTER_CAP_MODE_V1) {
518                 /* parse filter capability mask in args[1] */
519                 if (args[1] & FILTER_DPDK_1_FLAG)
520                         *mode = FILTER_DPDK_1;
521                 else if (args[1] & FILTER_USNIC_IP_FLAG)
522                         *mode = FILTER_USNIC_IP;
523                 else if (args[1] & FILTER_IPV4_5TUPLE_FLAG)
524                         *mode = FILTER_IPV4_5TUPLE;
525                 return 0;
526         }
527         max_level = args[1];
528 parse_max_level:
529         if (max_level >= (u32)FILTER_USNIC_IP)
530                 *mode = FILTER_USNIC_IP;
531         else
532                 *mode = FILTER_IPV4_5TUPLE;
533         return 0;
534 }
535
536 void vnic_dev_capable_udp_rss_weak(struct vnic_dev *vdev, bool *cfg_chk,
537                                    bool *weak)
538 {
539         u64 a0 = CMD_NIC_CFG, a1 = 0;
540         int wait = 1000;
541         int err;
542
543         *cfg_chk = false;
544         *weak = false;
545         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
546         if (err == 0 && a0 != 0 && a1 != 0) {
547                 *cfg_chk = true;
548                 *weak = !!((a1 >> 32) & CMD_NIC_CFG_CAPF_UDP_WEAK);
549         }
550 }
551
552 int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
553 {
554         u64 a0 = (u32)cmd, a1 = 0;
555         int wait = 1000;
556         int err;
557
558         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
559
560         return !(err || a0);
561 }
562
563 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, size_t size,
564         void *value)
565 {
566         u64 a0, a1;
567         int wait = 1000;
568         int err;
569
570         a0 = offset;
571         a1 = size;
572
573         err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
574
575         switch (size) {
576         case 1:
577                 *(u8 *)value = (u8)a0;
578                 break;
579         case 2:
580                 *(u16 *)value = (u16)a0;
581                 break;
582         case 4:
583                 *(u32 *)value = (u32)a0;
584                 break;
585         case 8:
586                 *(u64 *)value = a0;
587                 break;
588         default:
589                 BUG();
590                 break;
591         }
592
593         return err;
594 }
595
596 int vnic_dev_stats_clear(struct vnic_dev *vdev)
597 {
598         u64 a0 = 0, a1 = 0;
599         int wait = 1000;
600
601         return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
602 }
603
604 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
605 {
606         u64 a0, a1;
607         int wait = 1000;
608
609         if (!vdev->stats)
610                 return -ENOMEM;
611
612         *stats = vdev->stats;
613         a0 = vdev->stats_pa;
614         a1 = sizeof(struct vnic_stats);
615
616         return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
617 }
618
619 /*
620  * Configure counter DMA
621  */
622 int vnic_dev_counter_dma_cfg(struct vnic_dev *vdev, u32 period,
623                              u32 num_counters)
624 {
625         u64 args[3];
626         int wait = 1000;
627         int err;
628
629         if (num_counters > VNIC_MAX_FLOW_COUNTERS)
630                 return -ENOMEM;
631         if (period > 0 && (period < VNIC_COUNTER_DMA_MIN_PERIOD ||
632             num_counters == 0))
633                 return -EINVAL;
634
635         args[0] = num_counters;
636         args[1] = vdev->flow_counters_pa;
637         args[2] = period;
638         err =  vnic_dev_cmd_args(vdev, CMD_COUNTER_DMA_CONFIG, args, 3, wait);
639
640         /* record if DMAs need to be stopped on close */
641         if (!err)
642                 vdev->flow_counters_dma_active = (num_counters != 0 &&
643                                                   period != 0);
644
645         return err;
646 }
647
648 int vnic_dev_close(struct vnic_dev *vdev)
649 {
650         u64 a0 = 0, a1 = 0;
651         int wait = 1000;
652
653         return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
654 }
655
656 int vnic_dev_enable_wait(struct vnic_dev *vdev)
657 {
658         u64 a0 = 0, a1 = 0;
659         int wait = 1000;
660
661         if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
662                 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
663         else
664                 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
665 }
666
667 int vnic_dev_disable(struct vnic_dev *vdev)
668 {
669         u64 a0 = 0, a1 = 0;
670         int wait = 1000;
671
672         return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
673 }
674
675 int vnic_dev_open(struct vnic_dev *vdev, int arg)
676 {
677         u64 a0 = (u32)arg, a1 = 0;
678         int wait = 1000;
679
680         return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
681 }
682
683 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
684 {
685         u64 a0 = 0, a1 = 0;
686         int wait = 1000;
687         int err;
688
689         *done = 0;
690
691         err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
692         if (err)
693                 return err;
694
695         *done = (a0 == 0);
696
697         return 0;
698 }
699
700 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
701 {
702         u64 a0 = 0, a1 = 0;
703         int wait = 1000;
704         int err, i;
705
706         for (i = 0; i < ETH_ALEN; i++)
707                 mac_addr[i] = 0;
708
709         err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
710         if (err)
711                 return err;
712
713         for (i = 0; i < ETH_ALEN; i++)
714                 mac_addr[i] = ((u8 *)&a0)[i];
715
716         return 0;
717 }
718
719 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
720         int broadcast, int promisc, int allmulti)
721 {
722         u64 a0, a1 = 0;
723         int wait = 1000;
724         int err;
725
726         a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
727              (multicast ? CMD_PFILTER_MULTICAST : 0) |
728              (broadcast ? CMD_PFILTER_BROADCAST : 0) |
729              (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
730              (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
731
732         err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
733         if (err)
734                 pr_err("Can't set packet filter\n");
735
736         return err;
737 }
738
739 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
740 {
741         u64 a0 = 0, a1 = 0;
742         int wait = 1000;
743         int err;
744         int i;
745
746         for (i = 0; i < ETH_ALEN; i++)
747                 ((u8 *)&a0)[i] = addr[i];
748
749         err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
750         if (err)
751                 pr_err("Can't add addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
752                         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
753                         err);
754
755         return err;
756 }
757
758 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
759 {
760         u64 a0 = 0, a1 = 0;
761         int wait = 1000;
762         int err;
763         int i;
764
765         for (i = 0; i < ETH_ALEN; i++)
766                 ((u8 *)&a0)[i] = addr[i];
767
768         err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
769         if (err)
770                 pr_err("Can't del addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
771                         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
772                         err);
773
774         return err;
775 }
776
777 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
778         u8 ig_vlan_rewrite_mode)
779 {
780         u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
781         int wait = 1000;
782
783         if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
784                 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
785                                 &a0, &a1, wait);
786         else
787                 return 0;
788 }
789
790 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state)
791 {
792         vdev->in_reset = state;
793 }
794
795 static inline int vnic_dev_in_reset(struct vnic_dev *vdev)
796 {
797         return vdev->in_reset;
798 }
799
800 int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
801         void *notify_addr, dma_addr_t notify_pa, u16 intr)
802 {
803         u64 a0, a1;
804         int wait = 1000;
805         int r;
806
807         memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
808         if (!vnic_dev_in_reset(vdev)) {
809                 vdev->notify = notify_addr;
810                 vdev->notify_pa = notify_pa;
811         }
812
813         a0 = (u64)notify_pa;
814         a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
815         a1 += sizeof(struct vnic_devcmd_notify);
816
817         r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
818         if (!vnic_dev_in_reset(vdev))
819                 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
820
821         return r;
822 }
823
824 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
825 {
826         void *notify_addr = NULL;
827         dma_addr_t notify_pa = 0;
828         char name[NAME_MAX];
829         static u32 instance;
830
831         if (vdev->notify || vdev->notify_pa) {
832                 return vnic_dev_notify_setcmd(vdev, vdev->notify,
833                                               vdev->notify_pa, intr);
834         }
835         if (!vnic_dev_in_reset(vdev)) {
836                 snprintf((char *)name, sizeof(name),
837                         "vnic_notify-%u", instance++);
838                 notify_addr = vdev->alloc_consistent(vdev->priv,
839                         sizeof(struct vnic_devcmd_notify),
840                         &notify_pa, (u8 *)name);
841                 if (!notify_addr)
842                         return -ENOMEM;
843         }
844
845         return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
846 }
847
848 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
849 {
850         u64 a0, a1;
851         int wait = 1000;
852         int err;
853
854         a0 = 0;  /* paddr = 0 to unset notify buffer */
855         a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
856         a1 += sizeof(struct vnic_devcmd_notify);
857
858         err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
859         if (!vnic_dev_in_reset(vdev)) {
860                 vdev->notify = NULL;
861                 vdev->notify_pa = 0;
862                 vdev->notify_sz = 0;
863         }
864
865         return err;
866 }
867
868 int vnic_dev_notify_unset(struct vnic_dev *vdev)
869 {
870         if (vdev->notify && !vnic_dev_in_reset(vdev)) {
871                 vdev->free_consistent(vdev->priv,
872                         sizeof(struct vnic_devcmd_notify),
873                         vdev->notify,
874                         vdev->notify_pa);
875         }
876
877         return vnic_dev_notify_unsetcmd(vdev);
878 }
879
880 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
881 {
882         u32 *words;
883         unsigned int nwords = vdev->notify_sz / 4;
884         unsigned int i;
885         u32 csum;
886
887         if (!vdev->notify || !vdev->notify_sz)
888                 return 0;
889
890         do {
891                 csum = 0;
892                 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
893                 words = (u32 *)&vdev->notify_copy;
894                 for (i = 1; i < nwords; i++)
895                         csum += words[i];
896         } while (csum != words[0]);
897
898         return 1;
899 }
900
901 int vnic_dev_init(struct vnic_dev *vdev, int arg)
902 {
903         u64 a0 = (u32)arg, a1 = 0;
904         int wait = 1000;
905         int r = 0;
906
907         if (vnic_dev_capable(vdev, CMD_INIT))
908                 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
909         else {
910                 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
911                 if (a0 & CMD_INITF_DEFAULT_MAC) {
912                         /* Emulate these for old CMD_INIT_v1 which
913                          * didn't pass a0 so no CMD_INITF_*.
914                          */
915                         vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
916                         vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
917                 }
918         }
919         return r;
920 }
921
922 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
923 {
924         /* Default: hardware intr coal timer is in units of 1.5 usecs */
925         vdev->intr_coal_timer_info.mul = 2;
926         vdev->intr_coal_timer_info.div = 3;
927         vdev->intr_coal_timer_info.max_usec =
928                 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
929 }
930
931 int vnic_dev_link_status(struct vnic_dev *vdev)
932 {
933         if (!vnic_dev_notify_ready(vdev))
934                 return 0;
935
936         return vdev->notify_copy.link_state;
937 }
938
939 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
940 {
941         if (!vnic_dev_notify_ready(vdev))
942                 return 0;
943
944         return vdev->notify_copy.port_speed;
945 }
946
947 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
948 {
949         return (usec * vdev->intr_coal_timer_info.mul) /
950                 vdev->intr_coal_timer_info.div;
951 }
952
953 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
954 {
955         return (hw_cycles * vdev->intr_coal_timer_info.div) /
956                 vdev->intr_coal_timer_info.mul;
957 }
958
959 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
960 {
961         return vdev->intr_coal_timer_info.max_usec;
962 }
963
964 int vnic_dev_alloc_stats_mem(struct vnic_dev *vdev)
965 {
966         char name[NAME_MAX];
967         static u32 instance;
968
969         snprintf((char *)name, sizeof(name), "vnic_stats-%u", instance++);
970         vdev->stats = vdev->alloc_consistent(vdev->priv,
971                                              sizeof(struct vnic_stats),
972                                              &vdev->stats_pa, (u8 *)name);
973         return vdev->stats == NULL ? -ENOMEM : 0;
974 }
975
976 /*
977  * Initialize for up to VNIC_MAX_FLOW_COUNTERS
978  */
979 int vnic_dev_alloc_counter_mem(struct vnic_dev *vdev)
980 {
981         char name[NAME_MAX];
982         static u32 instance;
983
984         snprintf((char *)name, sizeof(name), "vnic_flow_ctrs-%u", instance++);
985         vdev->flow_counters = vdev->alloc_consistent(vdev->priv,
986                                              sizeof(struct vnic_counter_counts)
987                                              * VNIC_MAX_FLOW_COUNTERS,
988                                              &vdev->flow_counters_pa,
989                                              (u8 *)name);
990         vdev->flow_counters_dma_active = 0;
991         return vdev->flow_counters == NULL ? -ENOMEM : 0;
992 }
993
994 void vnic_dev_unregister(struct vnic_dev *vdev)
995 {
996         if (vdev) {
997                 if (vdev->notify)
998                         vdev->free_consistent(vdev->priv,
999                                 sizeof(struct vnic_devcmd_notify),
1000                                 vdev->notify,
1001                                 vdev->notify_pa);
1002                 if (vdev->stats)
1003                         vdev->free_consistent(vdev->priv,
1004                                 sizeof(struct vnic_stats),
1005                                 vdev->stats, vdev->stats_pa);
1006                 if (vdev->flow_counters) {
1007                         /* turn off counter DMAs before freeing memory */
1008                         if (vdev->flow_counters_dma_active)
1009                                 vnic_dev_counter_dma_cfg(vdev, 0, 0);
1010
1011                         vdev->free_consistent(vdev->priv,
1012                                 sizeof(struct vnic_counter_counts)
1013                                 * VNIC_MAX_FLOW_COUNTERS,
1014                                 vdev->flow_counters, vdev->flow_counters_pa);
1015                 }
1016                 if (vdev->fw_info)
1017                         vdev->free_consistent(vdev->priv,
1018                                 sizeof(struct vnic_devcmd_fw_info),
1019                                 vdev->fw_info, vdev->fw_info_pa);
1020                 rte_free(vdev);
1021         }
1022 }
1023
1024 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
1025         void *priv, struct rte_pci_device *pdev, struct vnic_dev_bar *bar,
1026         unsigned int num_bars)
1027 {
1028         if (!vdev) {
1029                 char name[NAME_MAX];
1030                 snprintf((char *)name, sizeof(name), "%s-vnic",
1031                           pdev->device.name);
1032                 vdev = (struct vnic_dev *)rte_zmalloc_socket(name,
1033                                         sizeof(struct vnic_dev),
1034                                         RTE_CACHE_LINE_SIZE,
1035                                         pdev->device.numa_node);
1036                 if (!vdev)
1037                         return NULL;
1038         }
1039
1040         vdev->priv = priv;
1041         vdev->pdev = pdev;
1042
1043         if (vnic_dev_discover_res(vdev, bar, num_bars))
1044                 goto err_out;
1045
1046         vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
1047         if (!vdev->devcmd)
1048                 goto err_out;
1049
1050         return vdev;
1051
1052 err_out:
1053         vnic_dev_unregister(vdev);
1054         return NULL;
1055 }
1056
1057 /*
1058  *  vnic_dev_classifier: Add/Delete classifier entries
1059  *  @vdev: vdev of the device
1060  *  @cmd: CLSF_ADD for Add filter
1061  *        CLSF_DEL for Delete filter
1062  *  @entry: In case of ADD filter, the caller passes the RQ number in this
1063  *          variable.
1064  *          This function stores the filter_id returned by the
1065  *          firmware in the same variable before return;
1066  *
1067  *          In case of DEL filter, the caller passes the RQ number. Return
1068  *          value is irrelevant.
1069  * @data: filter data
1070  * @action: action data
1071  */
1072 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
1073         struct filter_v2 *data, struct filter_action_v2 *action_v2)
1074 {
1075         u64 a0 = 0, a1 = 0;
1076         int wait = 1000;
1077         dma_addr_t tlv_pa;
1078         int ret = -EINVAL;
1079         struct filter_tlv *tlv, *tlv_va;
1080         u64 tlv_size;
1081         u32 filter_size, action_size;
1082         static unsigned int unique_id;
1083         char z_name[RTE_MEMZONE_NAMESIZE];
1084         enum vnic_devcmd_cmd dev_cmd;
1085
1086         if (cmd == CLSF_ADD) {
1087                 dev_cmd = (data->type >= FILTER_DPDK_1) ?
1088                           CMD_ADD_ADV_FILTER : CMD_ADD_FILTER;
1089
1090                 filter_size = vnic_filter_size(data);
1091                 action_size = vnic_action_size(action_v2);
1092
1093                 tlv_size = filter_size + action_size +
1094                     2*sizeof(struct filter_tlv);
1095                 snprintf((char *)z_name, sizeof(z_name),
1096                         "vnic_clsf_%u", unique_id++);
1097                 tlv_va = vdev->alloc_consistent(vdev->priv,
1098                         tlv_size, &tlv_pa, (u8 *)z_name);
1099                 if (!tlv_va)
1100                         return -ENOMEM;
1101                 tlv = tlv_va;
1102                 a0 = tlv_pa;
1103                 a1 = tlv_size;
1104                 memset(tlv, 0, tlv_size);
1105                 tlv->type = CLSF_TLV_FILTER;
1106                 tlv->length = filter_size;
1107                 memcpy(&tlv->val, (void *)data, filter_size);
1108
1109                 tlv = (struct filter_tlv *)((char *)tlv +
1110                                          sizeof(struct filter_tlv) +
1111                                          filter_size);
1112
1113                 tlv->type = CLSF_TLV_ACTION;
1114                 tlv->length = action_size;
1115                 memcpy(&tlv->val, (void *)action_v2, action_size);
1116                 ret = vnic_dev_cmd(vdev, dev_cmd, &a0, &a1, wait);
1117                 *entry = (u16)a0;
1118                 vdev->free_consistent(vdev->priv, tlv_size, tlv_va, tlv_pa);
1119         } else if (cmd == CLSF_DEL) {
1120                 a0 = *entry;
1121                 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);
1122         }
1123
1124         return ret;
1125 }
1126
1127 int vnic_dev_overlay_offload_ctrl(struct vnic_dev *vdev, u8 overlay, u8 config)
1128 {
1129         u64 a0 = overlay;
1130         u64 a1 = config;
1131         int wait = 1000;
1132
1133         return vnic_dev_cmd(vdev, CMD_OVERLAY_OFFLOAD_CTRL, &a0, &a1, wait);
1134 }
1135
1136 int vnic_dev_overlay_offload_cfg(struct vnic_dev *vdev, u8 overlay,
1137                                  u16 vxlan_udp_port_number)
1138 {
1139         u64 a1 = vxlan_udp_port_number;
1140         u64 a0 = overlay;
1141         int wait = 1000;
1142
1143         return vnic_dev_cmd(vdev, CMD_OVERLAY_OFFLOAD_CFG, &a0, &a1, wait);
1144 }
1145
1146 int vnic_dev_capable_vxlan(struct vnic_dev *vdev)
1147 {
1148         u64 a0 = VIC_FEATURE_VXLAN;
1149         u64 a1 = 0;
1150         int wait = 1000;
1151         int ret;
1152
1153         ret = vnic_dev_cmd(vdev, CMD_GET_SUPP_FEATURE_VER, &a0, &a1, wait);
1154         /* 1 if the NIC can do VXLAN for both IPv4 and IPv6 with multiple WQs */
1155         return ret == 0 &&
1156                 (a1 & (FEATURE_VXLAN_IPV6 | FEATURE_VXLAN_MULTI_WQ)) ==
1157                 (FEATURE_VXLAN_IPV6 | FEATURE_VXLAN_MULTI_WQ);
1158 }
1159
1160 bool vnic_dev_counter_alloc(struct vnic_dev *vdev, uint32_t *idx)
1161 {
1162         u64 a0 = 0;
1163         u64 a1 = 0;
1164         int wait = 1000;
1165
1166         if (vnic_dev_cmd(vdev, CMD_COUNTER_ALLOC, &a0, &a1, wait))
1167                 return false;
1168         *idx = (uint32_t)a0;
1169         return true;
1170 }
1171
1172 bool vnic_dev_counter_free(struct vnic_dev *vdev, uint32_t idx)
1173 {
1174         u64 a0 = idx;
1175         u64 a1 = 0;
1176         int wait = 1000;
1177
1178         return vnic_dev_cmd(vdev, CMD_COUNTER_FREE, &a0, &a1,
1179                             wait) == 0;
1180 }
1181
1182 bool vnic_dev_counter_query(struct vnic_dev *vdev, uint32_t idx,
1183                             bool reset, uint64_t *packets, uint64_t *bytes)
1184 {
1185         u64 a0 = idx;
1186         u64 a1 = reset ? 1 : 0;
1187         int wait = 1000;
1188
1189         if (reset) {
1190                 /* query/reset returns updated counters */
1191                 if (vnic_dev_cmd(vdev, CMD_COUNTER_QUERY, &a0, &a1, wait))
1192                         return false;
1193                 *packets = a0;
1194                 *bytes = a1;
1195         } else {
1196                 /* Get values DMA'd from the adapter */
1197                 *packets = vdev->flow_counters[idx].vcc_packets;
1198                 *bytes = vdev->flow_counters[idx].vcc_bytes;
1199         }
1200         return true;
1201 }