net/enic: replace some PMD macros with standard API
[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 #include <rte_ether.h>
10
11 #include "vnic_dev.h"
12 #include "vnic_resource.h"
13 #include "vnic_devcmd.h"
14 #include "vnic_nic.h"
15 #include "vnic_stats.h"
16 #include "vnic_flowman.h"
17
18
19 enum vnic_proxy_type {
20         PROXY_NONE,
21         PROXY_BY_BDF,
22         PROXY_BY_INDEX,
23 };
24
25 struct vnic_res {
26         void __iomem *vaddr;
27         dma_addr_t bus_addr;
28         unsigned int count;
29 };
30
31 struct vnic_intr_coal_timer_info {
32         u32 mul;
33         u32 div;
34         u32 max_usec;
35 };
36
37 struct vnic_dev {
38         void *priv;
39         struct rte_pci_device *pdev;
40         struct vnic_res res[RES_TYPE_MAX];
41         enum vnic_dev_intr_mode intr_mode;
42         struct vnic_devcmd __iomem *devcmd;
43         struct vnic_devcmd_notify *notify;
44         struct vnic_devcmd_notify notify_copy;
45         dma_addr_t notify_pa;
46         u32 notify_sz;
47         dma_addr_t linkstatus_pa;
48         struct vnic_stats *stats;
49         dma_addr_t stats_pa;
50         struct vnic_devcmd_fw_info *fw_info;
51         dma_addr_t fw_info_pa;
52         struct fm_info *flowman_info;
53         dma_addr_t flowman_info_pa;
54         enum vnic_proxy_type proxy;
55         u32 proxy_index;
56         u64 args[VNIC_DEVCMD_NARGS];
57         int in_reset;
58         struct vnic_intr_coal_timer_info intr_coal_timer_info;
59         void *(*alloc_consistent)(void *priv, size_t size,
60                 dma_addr_t *dma_handle, u8 *name);
61         void (*free_consistent)(void *priv,
62                 size_t size, void *vaddr,
63                 dma_addr_t dma_handle);
64 };
65
66 #define VNIC_MAX_RES_HDR_SIZE \
67         (sizeof(struct vnic_resource_header) + \
68         sizeof(struct vnic_resource) * RES_TYPE_MAX)
69 #define VNIC_RES_STRIDE 128
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                                     cmd != CMD_OVERLAY_OFFLOAD_CTRL &&
334                                     cmd != CMD_GET_SUPP_FEATURE_VER)
335                                         pr_err("Devcmd %d failed " \
336                                                 "with error code %d\n",
337                                                 _CMD_N(cmd), err);
338                                 return err;
339                         }
340
341                         if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
342                                 rmb();/* finish all reads initiated till now */
343                                 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
344                                         vdev->args[i] = readq(&devcmd->args[i]);
345                         }
346
347                         return 0;
348                 }
349         }
350
351         pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
352         return -ETIMEDOUT;
353 }
354
355 static int vnic_dev_cmd_proxy(struct vnic_dev *vdev,
356         enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd,
357         u64 *args, int nargs, int wait)
358 {
359         u32 status;
360         int err;
361
362         /*
363          * Proxy command consumes 2 arguments. One for proxy index,
364          * the other is for command to be proxied
365          */
366         if (nargs > VNIC_DEVCMD_NARGS - 2) {
367                 pr_err("number of args %d exceeds the maximum\n", nargs);
368                 return -EINVAL;
369         }
370         memset(vdev->args, 0, sizeof(vdev->args));
371
372         vdev->args[0] = vdev->proxy_index;
373         vdev->args[1] = cmd;
374         memcpy(&vdev->args[2], args, nargs * sizeof(args[0]));
375
376         err = _vnic_dev_cmd(vdev, proxy_cmd, wait);
377         if (err)
378                 return err;
379
380         status = (u32)vdev->args[0];
381         if (status & STAT_ERROR) {
382                 err = (int)vdev->args[1];
383                 if (err != ERR_ECMDUNKNOWN ||
384                     cmd != CMD_CAPABILITY)
385                         pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd));
386                 return err;
387         }
388
389         memcpy(args, &vdev->args[1], nargs * sizeof(args[0]));
390
391         return 0;
392 }
393
394 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
395         enum vnic_devcmd_cmd cmd, u64 *args, int nargs, int wait)
396 {
397         int err;
398
399         if (nargs > VNIC_DEVCMD_NARGS) {
400                 pr_err("number of args %d exceeds the maximum\n", nargs);
401                 return -EINVAL;
402         }
403         memset(vdev->args, 0, sizeof(vdev->args));
404         memcpy(vdev->args, args, nargs * sizeof(args[0]));
405
406         err = _vnic_dev_cmd(vdev, cmd, wait);
407
408         memcpy(args, vdev->args, nargs * sizeof(args[0]));
409
410         return err;
411 }
412
413 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
414         u64 *a0, u64 *a1, int wait)
415 {
416         u64 args[2];
417         int err;
418
419         args[0] = *a0;
420         args[1] = *a1;
421         memset(vdev->args, 0, sizeof(vdev->args));
422
423         switch (vdev->proxy) {
424         case PROXY_BY_INDEX:
425                 err =  vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
426                                 args, ARRAY_SIZE(args), wait);
427                 break;
428         case PROXY_BY_BDF:
429                 err =  vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
430                                 args, ARRAY_SIZE(args), wait);
431                 break;
432         case PROXY_NONE:
433         default:
434                 err = vnic_dev_cmd_no_proxy(vdev, cmd, args, 2, wait);
435                 break;
436         }
437
438         if (err == 0) {
439                 *a0 = args[0];
440                 *a1 = args[1];
441         }
442
443         return err;
444 }
445
446 int vnic_dev_cmd_args(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
447                       u64 *args, int nargs, int wait)
448 {
449         switch (vdev->proxy) {
450         case PROXY_BY_INDEX:
451                 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
452                                 args, nargs, wait);
453         case PROXY_BY_BDF:
454                 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
455                                 args, nargs, wait);
456         case PROXY_NONE:
457         default:
458                 return vnic_dev_cmd_no_proxy(vdev, cmd, args, nargs, wait);
459         }
460 }
461
462 int vnic_dev_fw_info(struct vnic_dev *vdev,
463                      struct vnic_devcmd_fw_info **fw_info)
464 {
465         char name[RTE_MEMZONE_NAMESIZE];
466         u64 a0, a1 = 0;
467         int wait = 1000;
468         int err = 0;
469         static u32 instance;
470
471         if (!vdev->fw_info) {
472                 snprintf((char *)name, sizeof(name), "vnic_fw_info-%u",
473                          instance++);
474                 vdev->fw_info = vdev->alloc_consistent(vdev->priv,
475                         sizeof(struct vnic_devcmd_fw_info),
476                         &vdev->fw_info_pa, (u8 *)name);
477                 if (!vdev->fw_info)
478                         return -ENOMEM;
479                 a0 = vdev->fw_info_pa;
480                 a1 = sizeof(struct vnic_devcmd_fw_info);
481                 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO,
482                                    &a0, &a1, wait);
483         }
484         *fw_info = vdev->fw_info;
485         return err;
486 }
487
488 static int vnic_dev_advanced_filters_cap(struct vnic_dev *vdev, u64 *args,
489                 int nargs)
490 {
491         memset(args, 0, nargs * sizeof(*args));
492         args[0] = CMD_ADD_ADV_FILTER;
493         args[1] = FILTER_CAP_MODE_V1_FLAG;
494         return vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, nargs, 1000);
495 }
496
497 int vnic_dev_capable_adv_filters(struct vnic_dev *vdev)
498 {
499         u64 a0 = CMD_ADD_ADV_FILTER, a1 = 0;
500         int wait = 1000;
501         int err;
502
503         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
504         if (err)
505                 return 0;
506         return (a1 >= (u32)FILTER_DPDK_1);
507 }
508
509 int vnic_dev_flowman_cmd(struct vnic_dev *vdev, u64 *args, int nargs)
510 {
511         int wait = 1000;
512
513         return vnic_dev_cmd_args(vdev, CMD_FLOW_MANAGER_OP, args, nargs, wait);
514 }
515
516 static int vnic_dev_flowman_enable(struct vnic_dev *vdev, u32 *mode,
517                                    u8 *filter_actions)
518 {
519         char name[RTE_MEMZONE_NAMESIZE];
520         u64 args[3];
521         u64 ops;
522         static u32 instance;
523
524         /* flowman devcmd available? */
525         if (!vnic_dev_capable(vdev, CMD_FLOW_MANAGER_OP))
526                 return 0;
527         /* Have the version we are using? */
528         args[0] = FM_API_VERSION_QUERY;
529         if (vnic_dev_flowman_cmd(vdev, args, 1))
530                 return 0;
531         if ((args[0] & (1ULL << FM_VERSION)) == 0)
532                 return 0;
533         /* Select the version */
534         args[0] = FM_API_VERSION_SELECT;
535         args[1] = FM_VERSION;
536         if (vnic_dev_flowman_cmd(vdev, args, 2))
537                 return 0;
538         /* Can we get fm_info? */
539         if (!vdev->flowman_info) {
540                 snprintf((char *)name, sizeof(name), "vnic_fm_info-%u",
541                          instance++);
542                 vdev->flowman_info = vdev->alloc_consistent(vdev->priv,
543                         sizeof(struct fm_info),
544                         &vdev->flowman_info_pa, (u8 *)name);
545                 if (!vdev->flowman_info)
546                         return 0;
547         }
548         args[0] = FM_INFO_QUERY;
549         args[1] = vdev->flowman_info_pa;
550         args[2] = sizeof(struct fm_info);
551         if (vnic_dev_flowman_cmd(vdev, args, 3))
552                 return 0;
553         /* Have required operations? */
554         ops = (1ULL << FMOP_END) |
555                 (1ULL << FMOP_DROP) |
556                 (1ULL << FMOP_RQ_STEER) |
557                 (1ULL << FMOP_EXACT_MATCH) |
558                 (1ULL << FMOP_MARK) |
559                 (1ULL << FMOP_TAG) |
560                 (1ULL << FMOP_EG_HAIRPIN) |
561                 (1ULL << FMOP_ENCAP) |
562                 (1ULL << FMOP_DECAP_NOSTRIP);
563         if ((vdev->flowman_info->fm_op_mask & ops) != ops)
564                 return 0;
565         /* Good to use flowman now */
566         *mode = FILTER_FLOWMAN;
567         *filter_actions = FILTER_ACTION_RQ_STEERING_FLAG |
568                 FILTER_ACTION_FILTER_ID_FLAG |
569                 FILTER_ACTION_COUNTER_FLAG |
570                 FILTER_ACTION_DROP_FLAG;
571         return 1;
572 }
573
574 /*  Determine the "best" filtering mode VIC is capaible of. Returns one of 4
575  *  value or 0 on error:
576  *      FILTER_FLOWMAN- flowman api capable
577  *      FILTER_DPDK_1- advanced filters availabile
578  *      FILTER_USNIC_IP_FLAG - advanced filters but with the restriction that
579  *              the IP layer must explicitly specified. I.e. cannot have a UDP
580  *              filter that matches both IPv4 and IPv6.
581  *      FILTER_IPV4_5TUPLE - fallback if either of the 2 above aren't available.
582  *              all other filter types are not available.
583  *   Retrun true in filter_tags if supported
584  */
585 int vnic_dev_capable_filter_mode(struct vnic_dev *vdev, u32 *mode,
586                                  u8 *filter_actions)
587 {
588         u64 args[4];
589         int err;
590         u32 max_level = 0;
591
592         /* If flowman is available, use it as it is the most capable API */
593         if (vnic_dev_flowman_enable(vdev, mode, filter_actions))
594                 return 0;
595
596         err = vnic_dev_advanced_filters_cap(vdev, args, 4);
597
598         /* determine supported filter actions */
599         *filter_actions = FILTER_ACTION_RQ_STEERING_FLAG; /* always available */
600         if (args[2] == FILTER_CAP_MODE_V1)
601                 *filter_actions = args[3];
602
603         if (err || ((args[0] == 1) && (args[1] == 0))) {
604                 /* Adv filter Command not supported or adv filters available but
605                  * not enabled. Try the normal filter capability command.
606                  */
607                 args[0] = CMD_ADD_FILTER;
608                 args[1] = 0;
609                 err = vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, 2, 1000);
610                 if (err)
611                         return err;
612                 max_level = args[1];
613                 goto parse_max_level;
614         } else if (args[2] == FILTER_CAP_MODE_V1) {
615                 /* parse filter capability mask in args[1] */
616                 if (args[1] & FILTER_DPDK_1_FLAG)
617                         *mode = FILTER_DPDK_1;
618                 else if (args[1] & FILTER_USNIC_IP_FLAG)
619                         *mode = FILTER_USNIC_IP;
620                 else if (args[1] & FILTER_IPV4_5TUPLE_FLAG)
621                         *mode = FILTER_IPV4_5TUPLE;
622                 return 0;
623         }
624         max_level = args[1];
625 parse_max_level:
626         if (max_level >= (u32)FILTER_USNIC_IP)
627                 *mode = FILTER_USNIC_IP;
628         else
629                 *mode = FILTER_IPV4_5TUPLE;
630         return 0;
631 }
632
633 void vnic_dev_capable_udp_rss_weak(struct vnic_dev *vdev, bool *cfg_chk,
634                                    bool *weak)
635 {
636         u64 a0 = CMD_NIC_CFG, a1 = 0;
637         int wait = 1000;
638         int err;
639
640         *cfg_chk = false;
641         *weak = false;
642         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
643         if (err == 0 && a0 != 0 && a1 != 0) {
644                 *cfg_chk = true;
645                 *weak = !!((a1 >> 32) & CMD_NIC_CFG_CAPF_UDP_WEAK);
646         }
647 }
648
649 int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
650 {
651         u64 a0 = (u32)cmd, a1 = 0;
652         int wait = 1000;
653         int err;
654
655         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
656
657         return !(err || a0);
658 }
659
660 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, size_t size,
661         void *value)
662 {
663         u64 a0, a1;
664         int wait = 1000;
665         int err;
666
667         a0 = offset;
668         a1 = size;
669
670         err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
671
672         switch (size) {
673         case 1:
674                 *(u8 *)value = (u8)a0;
675                 break;
676         case 2:
677                 *(u16 *)value = (u16)a0;
678                 break;
679         case 4:
680                 *(u32 *)value = (u32)a0;
681                 break;
682         case 8:
683                 *(u64 *)value = a0;
684                 break;
685         default:
686                 BUG();
687                 break;
688         }
689
690         return err;
691 }
692
693 int vnic_dev_stats_clear(struct vnic_dev *vdev)
694 {
695         u64 a0 = 0, a1 = 0;
696         int wait = 1000;
697
698         return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
699 }
700
701 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
702 {
703         u64 a0, a1;
704         int wait = 1000;
705
706         if (!vdev->stats)
707                 return -ENOMEM;
708
709         *stats = vdev->stats;
710         a0 = vdev->stats_pa;
711         a1 = sizeof(struct vnic_stats);
712
713         return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
714 }
715
716 int vnic_dev_close(struct vnic_dev *vdev)
717 {
718         u64 a0 = 0, a1 = 0;
719         int wait = 1000;
720
721         return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
722 }
723
724 int vnic_dev_enable_wait(struct vnic_dev *vdev)
725 {
726         u64 a0 = 0, a1 = 0;
727         int wait = 1000;
728
729         if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
730                 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
731         else
732                 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
733 }
734
735 int vnic_dev_disable(struct vnic_dev *vdev)
736 {
737         u64 a0 = 0, a1 = 0;
738         int wait = 1000;
739
740         return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
741 }
742
743 int vnic_dev_open(struct vnic_dev *vdev, int arg)
744 {
745         u64 a0 = (u32)arg, a1 = 0;
746         int wait = 1000;
747
748         return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
749 }
750
751 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
752 {
753         u64 a0 = 0, a1 = 0;
754         int wait = 1000;
755         int err;
756
757         *done = 0;
758
759         err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
760         if (err)
761                 return err;
762
763         *done = (a0 == 0);
764
765         return 0;
766 }
767
768 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
769 {
770         u64 a0 = 0, a1 = 0;
771         int wait = 1000;
772         int err, i;
773
774         for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
775                 mac_addr[i] = 0;
776
777         err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
778         if (err)
779                 return err;
780
781         for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
782                 mac_addr[i] = ((u8 *)&a0)[i];
783
784         return 0;
785 }
786
787 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
788         int broadcast, int promisc, int allmulti)
789 {
790         u64 a0, a1 = 0;
791         int wait = 1000;
792         int err;
793
794         a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
795              (multicast ? CMD_PFILTER_MULTICAST : 0) |
796              (broadcast ? CMD_PFILTER_BROADCAST : 0) |
797              (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
798              (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
799
800         err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
801         if (err)
802                 pr_err("Can't set packet filter\n");
803
804         return err;
805 }
806
807 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
808 {
809         u64 a0 = 0, a1 = 0;
810         int wait = 1000;
811         int err;
812         int i;
813
814         for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
815                 ((u8 *)&a0)[i] = addr[i];
816
817         err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
818         if (err)
819                 pr_err("Can't add addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
820                         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
821                         err);
822
823         return err;
824 }
825
826 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
827 {
828         u64 a0 = 0, a1 = 0;
829         int wait = 1000;
830         int err;
831         int i;
832
833         for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
834                 ((u8 *)&a0)[i] = addr[i];
835
836         err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
837         if (err)
838                 pr_err("Can't del addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
839                         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
840                         err);
841
842         return err;
843 }
844
845 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
846         u8 ig_vlan_rewrite_mode)
847 {
848         u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
849         int wait = 1000;
850
851         if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
852                 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
853                                 &a0, &a1, wait);
854         else
855                 return 0;
856 }
857
858 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state)
859 {
860         vdev->in_reset = state;
861 }
862
863 static inline int vnic_dev_in_reset(struct vnic_dev *vdev)
864 {
865         return vdev->in_reset;
866 }
867
868 int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
869         void *notify_addr, dma_addr_t notify_pa, u16 intr)
870 {
871         u64 a0, a1;
872         int wait = 1000;
873         int r;
874
875         memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
876         if (!vnic_dev_in_reset(vdev)) {
877                 vdev->notify = notify_addr;
878                 vdev->notify_pa = notify_pa;
879         }
880
881         a0 = (u64)notify_pa;
882         a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
883         a1 += sizeof(struct vnic_devcmd_notify);
884
885         r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
886         if (!vnic_dev_in_reset(vdev))
887                 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
888
889         return r;
890 }
891
892 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
893 {
894         void *notify_addr = NULL;
895         dma_addr_t notify_pa = 0;
896         char name[RTE_MEMZONE_NAMESIZE];
897         static u32 instance;
898
899         if (vdev->notify || vdev->notify_pa) {
900                 return vnic_dev_notify_setcmd(vdev, vdev->notify,
901                                               vdev->notify_pa, intr);
902         }
903         if (!vnic_dev_in_reset(vdev)) {
904                 snprintf((char *)name, sizeof(name),
905                         "vnic_notify-%u", instance++);
906                 notify_addr = vdev->alloc_consistent(vdev->priv,
907                         sizeof(struct vnic_devcmd_notify),
908                         &notify_pa, (u8 *)name);
909                 if (!notify_addr)
910                         return -ENOMEM;
911         }
912
913         return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
914 }
915
916 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
917 {
918         u64 a0, a1;
919         int wait = 1000;
920         int err;
921
922         a0 = 0;  /* paddr = 0 to unset notify buffer */
923         a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
924         a1 += sizeof(struct vnic_devcmd_notify);
925
926         err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
927         if (!vnic_dev_in_reset(vdev)) {
928                 vdev->notify = NULL;
929                 vdev->notify_pa = 0;
930                 vdev->notify_sz = 0;
931         }
932
933         return err;
934 }
935
936 int vnic_dev_notify_unset(struct vnic_dev *vdev)
937 {
938         if (vdev->notify && !vnic_dev_in_reset(vdev)) {
939                 vdev->free_consistent(vdev->priv,
940                         sizeof(struct vnic_devcmd_notify),
941                         vdev->notify,
942                         vdev->notify_pa);
943         }
944
945         return vnic_dev_notify_unsetcmd(vdev);
946 }
947
948 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
949 {
950         u32 *words;
951         unsigned int nwords = vdev->notify_sz / 4;
952         unsigned int i;
953         u32 csum;
954
955         if (!vdev->notify || !vdev->notify_sz)
956                 return 0;
957
958         do {
959                 csum = 0;
960                 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
961                 words = (u32 *)&vdev->notify_copy;
962                 for (i = 1; i < nwords; i++)
963                         csum += words[i];
964         } while (csum != words[0]);
965
966         return 1;
967 }
968
969 int vnic_dev_init(struct vnic_dev *vdev, int arg)
970 {
971         u64 a0 = (u32)arg, a1 = 0;
972         int wait = 1000;
973         int r = 0;
974
975         if (vnic_dev_capable(vdev, CMD_INIT))
976                 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
977         else {
978                 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
979                 if (a0 & CMD_INITF_DEFAULT_MAC) {
980                         /* Emulate these for old CMD_INIT_v1 which
981                          * didn't pass a0 so no CMD_INITF_*.
982                          */
983                         vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
984                         vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
985                 }
986         }
987         return r;
988 }
989
990 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
991 {
992         /* Default: hardware intr coal timer is in units of 1.5 usecs */
993         vdev->intr_coal_timer_info.mul = 2;
994         vdev->intr_coal_timer_info.div = 3;
995         vdev->intr_coal_timer_info.max_usec =
996                 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
997 }
998
999 int vnic_dev_link_status(struct vnic_dev *vdev)
1000 {
1001         if (!vnic_dev_notify_ready(vdev))
1002                 return 0;
1003
1004         return vdev->notify_copy.link_state;
1005 }
1006
1007 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
1008 {
1009         if (!vnic_dev_notify_ready(vdev))
1010                 return 0;
1011
1012         return vdev->notify_copy.port_speed;
1013 }
1014
1015 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
1016 {
1017         return (usec * vdev->intr_coal_timer_info.mul) /
1018                 vdev->intr_coal_timer_info.div;
1019 }
1020
1021 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
1022 {
1023         return (hw_cycles * vdev->intr_coal_timer_info.div) /
1024                 vdev->intr_coal_timer_info.mul;
1025 }
1026
1027 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
1028 {
1029         return vdev->intr_coal_timer_info.max_usec;
1030 }
1031
1032 int vnic_dev_alloc_stats_mem(struct vnic_dev *vdev)
1033 {
1034         char name[RTE_MEMZONE_NAMESIZE];
1035         static u32 instance;
1036
1037         snprintf((char *)name, sizeof(name), "vnic_stats-%u", instance++);
1038         vdev->stats = vdev->alloc_consistent(vdev->priv,
1039                                              sizeof(struct vnic_stats),
1040                                              &vdev->stats_pa, (u8 *)name);
1041         return vdev->stats == NULL ? -ENOMEM : 0;
1042 }
1043
1044 void vnic_dev_unregister(struct vnic_dev *vdev)
1045 {
1046         if (vdev) {
1047                 if (vdev->notify)
1048                         vdev->free_consistent(vdev->priv,
1049                                 sizeof(struct vnic_devcmd_notify),
1050                                 vdev->notify,
1051                                 vdev->notify_pa);
1052                 if (vdev->stats)
1053                         vdev->free_consistent(vdev->priv,
1054                                 sizeof(struct vnic_stats),
1055                                 vdev->stats, vdev->stats_pa);
1056                 if (vdev->flowman_info)
1057                         vdev->free_consistent(vdev->priv,
1058                                 sizeof(struct fm_info),
1059                                 vdev->flowman_info, vdev->flowman_info_pa);
1060                 if (vdev->fw_info)
1061                         vdev->free_consistent(vdev->priv,
1062                                 sizeof(struct vnic_devcmd_fw_info),
1063                                 vdev->fw_info, vdev->fw_info_pa);
1064                 rte_free(vdev);
1065         }
1066 }
1067
1068 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
1069         void *priv, struct rte_pci_device *pdev, struct vnic_dev_bar *bar,
1070         unsigned int num_bars)
1071 {
1072         if (!vdev) {
1073                 char name[RTE_MEMZONE_NAMESIZE];
1074                 snprintf((char *)name, sizeof(name), "%s-vnic",
1075                           pdev->device.name);
1076                 vdev = (struct vnic_dev *)rte_zmalloc_socket(name,
1077                                         sizeof(struct vnic_dev),
1078                                         RTE_CACHE_LINE_SIZE,
1079                                         pdev->device.numa_node);
1080                 if (!vdev)
1081                         return NULL;
1082         }
1083
1084         vdev->priv = priv;
1085         vdev->pdev = pdev;
1086
1087         if (vnic_dev_discover_res(vdev, bar, num_bars))
1088                 goto err_out;
1089
1090         vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
1091         if (!vdev->devcmd)
1092                 goto err_out;
1093
1094         return vdev;
1095
1096 err_out:
1097         vnic_dev_unregister(vdev);
1098         return NULL;
1099 }
1100
1101 /*
1102  *  vnic_dev_classifier: Add/Delete classifier entries
1103  *  @vdev: vdev of the device
1104  *  @cmd: CLSF_ADD for Add filter
1105  *        CLSF_DEL for Delete filter
1106  *  @entry: In case of ADD filter, the caller passes the RQ number in this
1107  *          variable.
1108  *          This function stores the filter_id returned by the
1109  *          firmware in the same variable before return;
1110  *
1111  *          In case of DEL filter, the caller passes the RQ number. Return
1112  *          value is irrelevant.
1113  * @data: filter data
1114  * @action: action data
1115  */
1116 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
1117         struct filter_v2 *data, struct filter_action_v2 *action_v2)
1118 {
1119         u64 a0 = 0, a1 = 0;
1120         int wait = 1000;
1121         dma_addr_t tlv_pa;
1122         int ret = -EINVAL;
1123         struct filter_tlv *tlv, *tlv_va;
1124         u64 tlv_size;
1125         u32 filter_size, action_size;
1126         static unsigned int unique_id;
1127         char z_name[RTE_MEMZONE_NAMESIZE];
1128         enum vnic_devcmd_cmd dev_cmd;
1129
1130         if (cmd == CLSF_ADD) {
1131                 dev_cmd = (data->type >= FILTER_DPDK_1) ?
1132                           CMD_ADD_ADV_FILTER : CMD_ADD_FILTER;
1133
1134                 filter_size = vnic_filter_size(data);
1135                 action_size = vnic_action_size(action_v2);
1136
1137                 tlv_size = filter_size + action_size +
1138                     2*sizeof(struct filter_tlv);
1139                 snprintf((char *)z_name, sizeof(z_name),
1140                         "vnic_clsf_%u", unique_id++);
1141                 tlv_va = vdev->alloc_consistent(vdev->priv,
1142                         tlv_size, &tlv_pa, (u8 *)z_name);
1143                 if (!tlv_va)
1144                         return -ENOMEM;
1145                 tlv = tlv_va;
1146                 a0 = tlv_pa;
1147                 a1 = tlv_size;
1148                 memset(tlv, 0, tlv_size);
1149                 tlv->type = CLSF_TLV_FILTER;
1150                 tlv->length = filter_size;
1151                 memcpy(&tlv->val, (void *)data, filter_size);
1152
1153                 tlv = (struct filter_tlv *)((char *)tlv +
1154                                          sizeof(struct filter_tlv) +
1155                                          filter_size);
1156
1157                 tlv->type = CLSF_TLV_ACTION;
1158                 tlv->length = action_size;
1159                 memcpy(&tlv->val, (void *)action_v2, action_size);
1160                 ret = vnic_dev_cmd(vdev, dev_cmd, &a0, &a1, wait);
1161                 *entry = (u16)a0;
1162                 vdev->free_consistent(vdev->priv, tlv_size, tlv_va, tlv_pa);
1163         } else if (cmd == CLSF_DEL) {
1164                 a0 = *entry;
1165                 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);
1166         }
1167
1168         return ret;
1169 }
1170
1171 int vnic_dev_overlay_offload_ctrl(struct vnic_dev *vdev, u8 overlay, u8 config)
1172 {
1173         u64 a0 = overlay;
1174         u64 a1 = config;
1175         int wait = 1000;
1176
1177         return vnic_dev_cmd(vdev, CMD_OVERLAY_OFFLOAD_CTRL, &a0, &a1, wait);
1178 }
1179
1180 int vnic_dev_overlay_offload_cfg(struct vnic_dev *vdev, u8 overlay,
1181                                  u16 vxlan_udp_port_number)
1182 {
1183         u64 a1 = vxlan_udp_port_number;
1184         u64 a0 = overlay;
1185         int wait = 1000;
1186
1187         return vnic_dev_cmd(vdev, CMD_OVERLAY_OFFLOAD_CFG, &a0, &a1, wait);
1188 }
1189
1190 int vnic_dev_capable_vxlan(struct vnic_dev *vdev)
1191 {
1192         u64 a0 = VIC_FEATURE_VXLAN;
1193         u64 a1 = 0;
1194         int wait = 1000;
1195         int ret;
1196
1197         ret = vnic_dev_cmd(vdev, CMD_GET_SUPP_FEATURE_VER, &a0, &a1, wait);
1198         /* 1 if the NIC can do VXLAN for both IPv4 and IPv6 with multiple WQs */
1199         return ret == 0 &&
1200                 (a1 & (FEATURE_VXLAN_IPV6 | FEATURE_VXLAN_MULTI_WQ)) ==
1201                 (FEATURE_VXLAN_IPV6 | FEATURE_VXLAN_MULTI_WQ);
1202 }
1203
1204 int vnic_dev_capable_geneve(struct vnic_dev *vdev)
1205 {
1206         u64 a0 = VIC_FEATURE_GENEVE;
1207         u64 a1 = 0;
1208         int wait = 1000;
1209         int ret;
1210
1211         ret = vnic_dev_cmd(vdev, CMD_GET_SUPP_FEATURE_VER, &a0, &a1, wait);
1212         return ret == 0 && (a1 & FEATURE_GENEVE_OPTIONS);
1213 }