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