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