New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / net / enic / base / vnic_dev.c
1 /*
2  * Copyright 2008-2014 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * Copyright (c) 2014, Cisco Systems, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <rte_memzone.h>
36 #include <rte_memcpy.h>
37 #include <rte_string_fns.h>
38
39 #include "vnic_dev.h"
40 #include "vnic_resource.h"
41 #include "vnic_devcmd.h"
42 #include "vnic_stats.h"
43
44
45 enum vnic_proxy_type {
46         PROXY_NONE,
47         PROXY_BY_BDF,
48         PROXY_BY_INDEX,
49 };
50
51 struct vnic_res {
52         void __iomem *vaddr;
53         dma_addr_t bus_addr;
54         unsigned int count;
55 };
56
57 struct vnic_intr_coal_timer_info {
58         u32 mul;
59         u32 div;
60         u32 max_usec;
61 };
62
63 struct vnic_dev {
64         void *priv;
65         struct rte_pci_device *pdev;
66         struct vnic_res res[RES_TYPE_MAX];
67         enum vnic_dev_intr_mode intr_mode;
68         struct vnic_devcmd __iomem *devcmd;
69         struct vnic_devcmd_notify *notify;
70         struct vnic_devcmd_notify notify_copy;
71         dma_addr_t notify_pa;
72         u32 notify_sz;
73         dma_addr_t linkstatus_pa;
74         struct vnic_stats *stats;
75         dma_addr_t stats_pa;
76         struct vnic_devcmd_fw_info *fw_info;
77         dma_addr_t fw_info_pa;
78         enum vnic_proxy_type proxy;
79         u32 proxy_index;
80         u64 args[VNIC_DEVCMD_NARGS];
81         u16 split_hdr_size;
82         int in_reset;
83         struct vnic_intr_coal_timer_info intr_coal_timer_info;
84         void *(*alloc_consistent)(void *priv, size_t size,
85                 dma_addr_t *dma_handle, u8 *name);
86         void (*free_consistent)(void *priv,
87                 size_t size, void *vaddr,
88                 dma_addr_t dma_handle);
89 };
90
91 #define VNIC_MAX_RES_HDR_SIZE \
92         (sizeof(struct vnic_resource_header) + \
93         sizeof(struct vnic_resource) * RES_TYPE_MAX)
94 #define VNIC_RES_STRIDE 128
95
96 void *vnic_dev_priv(struct vnic_dev *vdev)
97 {
98         return vdev->priv;
99 }
100
101 void vnic_register_cbacks(struct vnic_dev *vdev,
102         void *(*alloc_consistent)(void *priv, size_t size,
103             dma_addr_t *dma_handle, u8 *name),
104         void (*free_consistent)(void *priv,
105             size_t size, void *vaddr,
106             dma_addr_t dma_handle))
107 {
108         vdev->alloc_consistent = alloc_consistent;
109         vdev->free_consistent = free_consistent;
110 }
111
112 static int vnic_dev_discover_res(struct vnic_dev *vdev,
113         struct vnic_dev_bar *bar, unsigned int num_bars)
114 {
115         struct vnic_resource_header __iomem *rh;
116         struct mgmt_barmap_hdr __iomem *mrh;
117         struct vnic_resource __iomem *r;
118         u8 type;
119
120         if (num_bars == 0)
121                 return -EINVAL;
122
123         if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
124                 pr_err("vNIC BAR0 res hdr length error\n");
125                 return -EINVAL;
126         }
127
128         rh  = bar->vaddr;
129         mrh = bar->vaddr;
130         if (!rh) {
131                 pr_err("vNIC BAR0 res hdr not mem-mapped\n");
132                 return -EINVAL;
133         }
134
135         /* Check for mgmt vnic in addition to normal vnic */
136         if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) ||
137                 (ioread32(&rh->version) != VNIC_RES_VERSION)) {
138                 if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) ||
139                         (ioread32(&mrh->version) != MGMTVNIC_VERSION)) {
140                         pr_err("vNIC BAR0 res magic/version error " \
141                                 "exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n",
142                                 VNIC_RES_MAGIC, VNIC_RES_VERSION,
143                                 MGMTVNIC_MAGIC, MGMTVNIC_VERSION,
144                                 ioread32(&rh->magic), ioread32(&rh->version));
145                         return -EINVAL;
146                 }
147         }
148
149         if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
150                 r = (struct vnic_resource __iomem *)(mrh + 1);
151         else
152                 r = (struct vnic_resource __iomem *)(rh + 1);
153
154
155         while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
156                 u8 bar_num = ioread8(&r->bar);
157                 u32 bar_offset = ioread32(&r->bar_offset);
158                 u32 count = ioread32(&r->count);
159                 u32 len;
160
161                 r++;
162
163                 if (bar_num >= num_bars)
164                         continue;
165
166                 if (!bar[bar_num].len || !bar[bar_num].vaddr)
167                         continue;
168
169                 switch (type) {
170                 case RES_TYPE_WQ:
171                 case RES_TYPE_RQ:
172                 case RES_TYPE_CQ:
173                 case RES_TYPE_INTR_CTRL:
174                         /* each count is stride bytes long */
175                         len = count * VNIC_RES_STRIDE;
176                         if (len + bar_offset > bar[bar_num].len) {
177                                 pr_err("vNIC BAR0 resource %d " \
178                                         "out-of-bounds, offset 0x%x + " \
179                                         "size 0x%x > bar len 0x%lx\n",
180                                         type, bar_offset,
181                                         len,
182                                         bar[bar_num].len);
183                                 return -EINVAL;
184                         }
185                         break;
186                 case RES_TYPE_INTR_PBA_LEGACY:
187                 case RES_TYPE_DEVCMD:
188                         len = count;
189                         break;
190                 default:
191                         continue;
192                 }
193
194                 vdev->res[type].count = count;
195                 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
196                     bar_offset;
197                 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
198         }
199
200         return 0;
201 }
202
203 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
204         enum vnic_res_type type)
205 {
206         return vdev->res[type].count;
207 }
208
209 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
210         unsigned int index)
211 {
212         if (!vdev->res[type].vaddr)
213                 return NULL;
214
215         switch (type) {
216         case RES_TYPE_WQ:
217         case RES_TYPE_RQ:
218         case RES_TYPE_CQ:
219         case RES_TYPE_INTR_CTRL:
220                 return (char __iomem *)vdev->res[type].vaddr +
221                         index * VNIC_RES_STRIDE;
222         default:
223                 return (char __iomem *)vdev->res[type].vaddr;
224         }
225 }
226
227 unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
228         unsigned int desc_count, unsigned int desc_size)
229 {
230         /* The base address of the desc rings must be 512 byte aligned.
231          * Descriptor count is aligned to groups of 32 descriptors.  A
232          * count of 0 means the maximum 4096 descriptors.  Descriptor
233          * size is aligned to 16 bytes.
234          */
235
236         unsigned int count_align = 32;
237         unsigned int desc_align = 16;
238
239         ring->base_align = 512;
240
241         if (desc_count == 0)
242                 desc_count = 4096;
243
244         ring->desc_count = VNIC_ALIGN(desc_count, count_align);
245
246         ring->desc_size = VNIC_ALIGN(desc_size, desc_align);
247
248         ring->size = ring->desc_count * ring->desc_size;
249         ring->size_unaligned = ring->size + ring->base_align;
250
251         return ring->size_unaligned;
252 }
253
254 void vnic_set_hdr_split_size(struct vnic_dev *vdev, u16 size)
255 {
256         vdev->split_hdr_size = size;
257 }
258
259 u16 vnic_get_hdr_split_size(struct vnic_dev *vdev)
260 {
261         return vdev->split_hdr_size;
262 }
263
264 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
265 {
266         memset(ring->descs, 0, ring->size);
267 }
268
269 int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev,
270         struct vnic_dev_ring *ring,
271         unsigned int desc_count, unsigned int desc_size,
272         __attribute__((unused)) unsigned int socket_id,
273         char *z_name)
274 {
275         void *alloc_addr;
276         dma_addr_t alloc_pa = 0;
277
278         vnic_dev_desc_ring_size(ring, desc_count, desc_size);
279         alloc_addr = vdev->alloc_consistent(vdev->priv,
280                                             ring->size_unaligned,
281                                             &alloc_pa, (u8 *)z_name);
282         if (!alloc_addr) {
283                 pr_err("Failed to allocate ring (size=%d), aborting\n",
284                         (int)ring->size);
285                 return -ENOMEM;
286         }
287         ring->descs_unaligned = alloc_addr;
288         if (!alloc_pa) {
289                 pr_err("Failed to map allocated ring (size=%d), aborting\n",
290                         (int)ring->size);
291                 vdev->free_consistent(vdev->priv,
292                                       ring->size_unaligned,
293                                       alloc_addr,
294                                       alloc_pa);
295                 return -ENOMEM;
296         }
297         ring->base_addr_unaligned = alloc_pa;
298
299         ring->base_addr = VNIC_ALIGN(ring->base_addr_unaligned,
300                 ring->base_align);
301         ring->descs = (u8 *)ring->descs_unaligned +
302             (ring->base_addr - ring->base_addr_unaligned);
303
304         vnic_dev_clear_desc_ring(ring);
305
306         ring->desc_avail = ring->desc_count - 1;
307
308         return 0;
309 }
310
311 void vnic_dev_free_desc_ring(__attribute__((unused))  struct vnic_dev *vdev,
312         struct vnic_dev_ring *ring)
313 {
314         if (ring->descs) {
315                 vdev->free_consistent(vdev->priv,
316                                       ring->size_unaligned,
317                                       ring->descs_unaligned,
318                                       ring->base_addr_unaligned);
319                 ring->descs = NULL;
320         }
321 }
322
323 static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
324         int wait)
325 {
326         struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
327         unsigned int i;
328         int delay;
329         u32 status;
330         int err;
331
332         status = ioread32(&devcmd->status);
333         if (status == 0xFFFFFFFF) {
334                 /* PCI-e target device is gone */
335                 return -ENODEV;
336         }
337         if (status & STAT_BUSY) {
338
339                 pr_err("Busy devcmd %d\n",  _CMD_N(cmd));
340                 return -EBUSY;
341         }
342
343         if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
344                 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
345                         writeq(vdev->args[i], &devcmd->args[i]);
346                 wmb(); /* complete all writes initiated till now */
347         }
348
349         iowrite32(cmd, &devcmd->cmd);
350
351         if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
352                 return 0;
353
354         for (delay = 0; delay < wait; delay++) {
355
356                 udelay(100);
357
358                 status = ioread32(&devcmd->status);
359                 if (status == 0xFFFFFFFF) {
360                         /* PCI-e target device is gone */
361                         return -ENODEV;
362                 }
363
364                 if (!(status & STAT_BUSY)) {
365                         if (status & STAT_ERROR) {
366                                 err = -(int)readq(&devcmd->args[0]);
367                                 if (cmd != CMD_CAPABILITY)
368                                         pr_err("Devcmd %d failed " \
369                                                 "with error code %d\n",
370                                                 _CMD_N(cmd), err);
371                                 return err;
372                         }
373
374                         if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
375                                 rmb();/* finish all reads initiated till now */
376                                 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
377                                         vdev->args[i] = readq(&devcmd->args[i]);
378                         }
379
380                         return 0;
381                 }
382         }
383
384         pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
385         return -ETIMEDOUT;
386 }
387
388 static int vnic_dev_cmd_proxy(struct vnic_dev *vdev,
389         enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd,
390         u64 *args, int nargs, int wait)
391 {
392         u32 status;
393         int err;
394
395         /*
396          * Proxy command consumes 2 arguments. One for proxy index,
397          * the other is for command to be proxied
398          */
399         if (nargs > VNIC_DEVCMD_NARGS - 2) {
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
405         vdev->args[0] = vdev->proxy_index;
406         vdev->args[1] = cmd;
407         memcpy(&vdev->args[2], args, nargs * sizeof(args[0]));
408
409         err = _vnic_dev_cmd(vdev, proxy_cmd, wait);
410         if (err)
411                 return err;
412
413         status = (u32)vdev->args[0];
414         if (status & STAT_ERROR) {
415                 err = (int)vdev->args[1];
416                 if (err != ERR_ECMDUNKNOWN ||
417                     cmd != CMD_CAPABILITY)
418                         pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd));
419                 return err;
420         }
421
422         memcpy(args, &vdev->args[1], nargs * sizeof(args[0]));
423
424         return 0;
425 }
426
427 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
428         enum vnic_devcmd_cmd cmd, u64 *args, int nargs, int wait)
429 {
430         int err;
431
432         if (nargs > VNIC_DEVCMD_NARGS) {
433                 pr_err("number of args %d exceeds the maximum\n", nargs);
434                 return -EINVAL;
435         }
436         memset(vdev->args, 0, sizeof(vdev->args));
437         memcpy(vdev->args, args, nargs * sizeof(args[0]));
438
439         err = _vnic_dev_cmd(vdev, cmd, wait);
440
441         memcpy(args, vdev->args, nargs * sizeof(args[0]));
442
443         return err;
444 }
445
446 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
447         u64 *a0, u64 *a1, int wait)
448 {
449         u64 args[2];
450         int err;
451
452         args[0] = *a0;
453         args[1] = *a1;
454         memset(vdev->args, 0, sizeof(vdev->args));
455
456         switch (vdev->proxy) {
457         case PROXY_BY_INDEX:
458                 err =  vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
459                                 args, ARRAY_SIZE(args), wait);
460                 break;
461         case PROXY_BY_BDF:
462                 err =  vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
463                                 args, ARRAY_SIZE(args), wait);
464                 break;
465         case PROXY_NONE:
466         default:
467                 err = vnic_dev_cmd_no_proxy(vdev, cmd, args, 2, wait);
468                 break;
469         }
470
471         if (err == 0) {
472                 *a0 = args[0];
473                 *a1 = args[1];
474         }
475
476         return err;
477 }
478
479 int vnic_dev_cmd_args(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
480                       u64 *args, int nargs, int wait)
481 {
482         switch (vdev->proxy) {
483         case PROXY_BY_INDEX:
484                 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
485                                 args, nargs, wait);
486         case PROXY_BY_BDF:
487                 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
488                                 args, nargs, wait);
489         case PROXY_NONE:
490         default:
491                 return vnic_dev_cmd_no_proxy(vdev, cmd, args, nargs, wait);
492         }
493 }
494
495 static int vnic_dev_advanced_filters_cap(struct vnic_dev *vdev, u64 *args,
496                 int nargs)
497 {
498         memset(args, 0, nargs * sizeof(*args));
499         args[0] = CMD_ADD_ADV_FILTER;
500         args[1] = FILTER_CAP_MODE_V1_FLAG;
501         return vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, nargs, 1000);
502 }
503
504 int vnic_dev_capable_adv_filters(struct vnic_dev *vdev)
505 {
506         u64 a0 = CMD_ADD_ADV_FILTER, a1 = 0;
507         int wait = 1000;
508         int err;
509
510         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
511         if (err)
512                 return 0;
513         return (a1 >= (u32)FILTER_DPDK_1);
514 }
515
516 /*  Determine the "best" filtering mode VIC is capaible of. Returns one of 3
517  *  value or 0 on error:
518  *      FILTER_DPDK_1- advanced filters availabile
519  *      FILTER_USNIC_IP_FLAG - advanced filters but with the restriction that
520  *              the IP layer must explicitly specified. I.e. cannot have a UDP
521  *              filter that matches both IPv4 and IPv6.
522  *      FILTER_IPV4_5TUPLE - fallback if either of the 2 above aren't available.
523  *              all other filter types are not available.
524  *   Retrun true in filter_tags if supported
525  */
526 int vnic_dev_capable_filter_mode(struct vnic_dev *vdev, u32 *mode,
527                                  u8 *filter_tags)
528 {
529         u64 args[4];
530         int err;
531         u32 max_level = 0;
532
533         err = vnic_dev_advanced_filters_cap(vdev, args, 4);
534
535         /* determine if filter tags are available */
536         if (err)
537                 *filter_tags = 0;
538         if ((args[2] == FILTER_CAP_MODE_V1) &&
539             (args[3] & FILTER_ACTION_FILTER_ID_FLAG))
540                 *filter_tags = 1;
541         else
542                 *filter_tags = 0;
543
544         if (err || ((args[0] == 1) && (args[1] == 0))) {
545                 /* Adv filter Command not supported or adv filters available but
546                  * not enabled. Try the normal filter capability command.
547                  */
548                 args[0] = CMD_ADD_FILTER;
549                 args[1] = 0;
550                 err = vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, 2, 1000);
551                 if (err)
552                         return err;
553                 max_level = args[1];
554                 goto parse_max_level;
555         } else if (args[2] == FILTER_CAP_MODE_V1) {
556                 /* parse filter capability mask in args[1] */
557                 if (args[1] & FILTER_DPDK_1_FLAG)
558                         *mode = FILTER_DPDK_1;
559                 else if (args[1] & FILTER_USNIC_IP_FLAG)
560                         *mode = FILTER_USNIC_IP;
561                 else if (args[1] & FILTER_IPV4_5TUPLE_FLAG)
562                         *mode = FILTER_IPV4_5TUPLE;
563                 return 0;
564         }
565         max_level = args[1];
566 parse_max_level:
567         if (max_level >= (u32)FILTER_USNIC_IP)
568                 *mode = FILTER_USNIC_IP;
569         else
570                 *mode = FILTER_IPV4_5TUPLE;
571         return 0;
572 }
573
574 int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
575 {
576         u64 a0 = (u32)cmd, a1 = 0;
577         int wait = 1000;
578         int err;
579
580         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
581
582         return !(err || a0);
583 }
584
585 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, size_t size,
586         void *value)
587 {
588         u64 a0, a1;
589         int wait = 1000;
590         int err;
591
592         a0 = offset;
593         a1 = size;
594
595         err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
596
597         switch (size) {
598         case 1:
599                 *(u8 *)value = (u8)a0;
600                 break;
601         case 2:
602                 *(u16 *)value = (u16)a0;
603                 break;
604         case 4:
605                 *(u32 *)value = (u32)a0;
606                 break;
607         case 8:
608                 *(u64 *)value = a0;
609                 break;
610         default:
611                 BUG();
612                 break;
613         }
614
615         return err;
616 }
617
618 int vnic_dev_stats_clear(struct vnic_dev *vdev)
619 {
620         u64 a0 = 0, a1 = 0;
621         int wait = 1000;
622
623         return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
624 }
625
626 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
627 {
628         u64 a0, a1;
629         int wait = 1000;
630         static u32 instance;
631         char name[NAME_MAX];
632
633         if (!vdev->stats) {
634                 snprintf((char *)name, sizeof(name),
635                         "vnic_stats-%u", instance++);
636                 vdev->stats = vdev->alloc_consistent(vdev->priv,
637                         sizeof(struct vnic_stats), &vdev->stats_pa, (u8 *)name);
638                 if (!vdev->stats)
639                         return -ENOMEM;
640         }
641
642         *stats = vdev->stats;
643         a0 = vdev->stats_pa;
644         a1 = sizeof(struct vnic_stats);
645
646         return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
647 }
648
649 int vnic_dev_close(struct vnic_dev *vdev)
650 {
651         u64 a0 = 0, a1 = 0;
652         int wait = 1000;
653
654         return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
655 }
656
657 int vnic_dev_enable_wait(struct vnic_dev *vdev)
658 {
659         u64 a0 = 0, a1 = 0;
660         int wait = 1000;
661
662         if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
663                 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
664         else
665                 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
666 }
667
668 int vnic_dev_disable(struct vnic_dev *vdev)
669 {
670         u64 a0 = 0, a1 = 0;
671         int wait = 1000;
672
673         return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
674 }
675
676 int vnic_dev_open(struct vnic_dev *vdev, int arg)
677 {
678         u64 a0 = (u32)arg, a1 = 0;
679         int wait = 1000;
680
681         return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
682 }
683
684 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
685 {
686         u64 a0 = 0, a1 = 0;
687         int wait = 1000;
688         int err;
689
690         *done = 0;
691
692         err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
693         if (err)
694                 return err;
695
696         *done = (a0 == 0);
697
698         return 0;
699 }
700
701 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
702 {
703         u64 a0 = 0, a1 = 0;
704         int wait = 1000;
705         int err, i;
706
707         for (i = 0; i < ETH_ALEN; i++)
708                 mac_addr[i] = 0;
709
710         err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
711         if (err)
712                 return err;
713
714         for (i = 0; i < ETH_ALEN; i++)
715                 mac_addr[i] = ((u8 *)&a0)[i];
716
717         return 0;
718 }
719
720 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
721         int broadcast, int promisc, int allmulti)
722 {
723         u64 a0, a1 = 0;
724         int wait = 1000;
725         int err;
726
727         a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
728              (multicast ? CMD_PFILTER_MULTICAST : 0) |
729              (broadcast ? CMD_PFILTER_BROADCAST : 0) |
730              (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
731              (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
732
733         err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
734         if (err)
735                 pr_err("Can't set packet filter\n");
736
737         return err;
738 }
739
740 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
741 {
742         u64 a0 = 0, a1 = 0;
743         int wait = 1000;
744         int err;
745         int i;
746
747         for (i = 0; i < ETH_ALEN; i++)
748                 ((u8 *)&a0)[i] = addr[i];
749
750         err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
751         if (err)
752                 pr_err("Can't add addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
753                         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
754                         err);
755
756         return err;
757 }
758
759 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
760 {
761         u64 a0 = 0, a1 = 0;
762         int wait = 1000;
763         int err;
764         int i;
765
766         for (i = 0; i < ETH_ALEN; i++)
767                 ((u8 *)&a0)[i] = addr[i];
768
769         err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
770         if (err)
771                 pr_err("Can't del addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
772                         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
773                         err);
774
775         return err;
776 }
777
778 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
779         u8 ig_vlan_rewrite_mode)
780 {
781         u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
782         int wait = 1000;
783
784         if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
785                 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
786                                 &a0, &a1, wait);
787         else
788                 return 0;
789 }
790
791 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state)
792 {
793         vdev->in_reset = state;
794 }
795
796 static inline int vnic_dev_in_reset(struct vnic_dev *vdev)
797 {
798         return vdev->in_reset;
799 }
800
801 int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
802         void *notify_addr, dma_addr_t notify_pa, u16 intr)
803 {
804         u64 a0, a1;
805         int wait = 1000;
806         int r;
807
808         memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
809         if (!vnic_dev_in_reset(vdev)) {
810                 vdev->notify = notify_addr;
811                 vdev->notify_pa = notify_pa;
812         }
813
814         a0 = (u64)notify_pa;
815         a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
816         a1 += sizeof(struct vnic_devcmd_notify);
817
818         r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
819         if (!vnic_dev_in_reset(vdev))
820                 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
821
822         return r;
823 }
824
825 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
826 {
827         void *notify_addr = NULL;
828         dma_addr_t notify_pa = 0;
829         char name[NAME_MAX];
830         static u32 instance;
831
832         if (vdev->notify || vdev->notify_pa) {
833                 return vnic_dev_notify_setcmd(vdev, vdev->notify,
834                                               vdev->notify_pa, intr);
835         }
836         if (!vnic_dev_in_reset(vdev)) {
837                 snprintf((char *)name, sizeof(name),
838                         "vnic_notify-%u", instance++);
839                 notify_addr = vdev->alloc_consistent(vdev->priv,
840                         sizeof(struct vnic_devcmd_notify),
841                         &notify_pa, (u8 *)name);
842                 if (!notify_addr)
843                         return -ENOMEM;
844         }
845
846         return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
847 }
848
849 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
850 {
851         u64 a0, a1;
852         int wait = 1000;
853         int err;
854
855         a0 = 0;  /* paddr = 0 to unset notify buffer */
856         a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
857         a1 += sizeof(struct vnic_devcmd_notify);
858
859         err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
860         if (!vnic_dev_in_reset(vdev)) {
861                 vdev->notify = NULL;
862                 vdev->notify_pa = 0;
863                 vdev->notify_sz = 0;
864         }
865
866         return err;
867 }
868
869 int vnic_dev_notify_unset(struct vnic_dev *vdev)
870 {
871         if (vdev->notify && !vnic_dev_in_reset(vdev)) {
872                 vdev->free_consistent(vdev->priv,
873                         sizeof(struct vnic_devcmd_notify),
874                         vdev->notify,
875                         vdev->notify_pa);
876         }
877
878         return vnic_dev_notify_unsetcmd(vdev);
879 }
880
881 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
882 {
883         u32 *words;
884         unsigned int nwords = vdev->notify_sz / 4;
885         unsigned int i;
886         u32 csum;
887
888         if (!vdev->notify || !vdev->notify_sz)
889                 return 0;
890
891         do {
892                 csum = 0;
893                 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
894                 words = (u32 *)&vdev->notify_copy;
895                 for (i = 1; i < nwords; i++)
896                         csum += words[i];
897         } while (csum != words[0]);
898
899         return 1;
900 }
901
902 int vnic_dev_init(struct vnic_dev *vdev, int arg)
903 {
904         u64 a0 = (u32)arg, a1 = 0;
905         int wait = 1000;
906         int r = 0;
907
908         if (vnic_dev_capable(vdev, CMD_INIT))
909                 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
910         else {
911                 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
912                 if (a0 & CMD_INITF_DEFAULT_MAC) {
913                         /* Emulate these for old CMD_INIT_v1 which
914                          * didn't pass a0 so no CMD_INITF_*.
915                          */
916                         vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
917                         vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
918                 }
919         }
920         return r;
921 }
922
923 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
924 {
925         /* Default: hardware intr coal timer is in units of 1.5 usecs */
926         vdev->intr_coal_timer_info.mul = 2;
927         vdev->intr_coal_timer_info.div = 3;
928         vdev->intr_coal_timer_info.max_usec =
929                 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
930 }
931
932 int vnic_dev_link_status(struct vnic_dev *vdev)
933 {
934         if (!vnic_dev_notify_ready(vdev))
935                 return 0;
936
937         return vdev->notify_copy.link_state;
938 }
939
940 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
941 {
942         if (!vnic_dev_notify_ready(vdev))
943                 return 0;
944
945         return vdev->notify_copy.port_speed;
946 }
947
948 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
949 {
950         return (usec * vdev->intr_coal_timer_info.mul) /
951                 vdev->intr_coal_timer_info.div;
952 }
953
954 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
955 {
956         return (hw_cycles * vdev->intr_coal_timer_info.div) /
957                 vdev->intr_coal_timer_info.mul;
958 }
959
960 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
961 {
962         return vdev->intr_coal_timer_info.max_usec;
963 }
964
965 void vnic_dev_unregister(struct vnic_dev *vdev)
966 {
967         if (vdev) {
968                 if (vdev->notify)
969                         vdev->free_consistent(vdev->priv,
970                                 sizeof(struct vnic_devcmd_notify),
971                                 vdev->notify,
972                                 vdev->notify_pa);
973                 if (vdev->stats)
974                         vdev->free_consistent(vdev->priv,
975                                 sizeof(struct vnic_stats),
976                                 vdev->stats, vdev->stats_pa);
977                 if (vdev->fw_info)
978                         vdev->free_consistent(vdev->priv,
979                                 sizeof(struct vnic_devcmd_fw_info),
980                                 vdev->fw_info, vdev->fw_info_pa);
981                 rte_free(vdev);
982         }
983 }
984
985 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
986         void *priv, struct rte_pci_device *pdev, struct vnic_dev_bar *bar,
987         unsigned int num_bars)
988 {
989         if (!vdev) {
990                 char name[NAME_MAX];
991                 snprintf((char *)name, sizeof(name), "%s-vnic",
992                           pdev->device.name);
993                 vdev = (struct vnic_dev *)rte_zmalloc_socket(name,
994                                         sizeof(struct vnic_dev),
995                                         RTE_CACHE_LINE_SIZE,
996                                         pdev->device.numa_node);
997                 if (!vdev)
998                         return NULL;
999         }
1000
1001         vdev->priv = priv;
1002         vdev->pdev = pdev;
1003
1004         if (vnic_dev_discover_res(vdev, bar, num_bars))
1005                 goto err_out;
1006
1007         vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
1008         if (!vdev->devcmd)
1009                 goto err_out;
1010
1011         return vdev;
1012
1013 err_out:
1014         vnic_dev_unregister(vdev);
1015         return NULL;
1016 }
1017
1018 /*
1019  *  vnic_dev_classifier: Add/Delete classifier entries
1020  *  @vdev: vdev of the device
1021  *  @cmd: CLSF_ADD for Add filter
1022  *        CLSF_DEL for Delete filter
1023  *  @entry: In case of ADD filter, the caller passes the RQ number in this
1024  *          variable.
1025  *          This function stores the filter_id returned by the
1026  *          firmware in the same variable before return;
1027  *
1028  *          In case of DEL filter, the caller passes the RQ number. Return
1029  *          value is irrelevant.
1030  * @data: filter data
1031  * @action: action data
1032  */
1033 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
1034         struct filter_v2 *data, struct filter_action_v2 *action_v2)
1035 {
1036         u64 a0 = 0, a1 = 0;
1037         int wait = 1000;
1038         dma_addr_t tlv_pa;
1039         int ret = -EINVAL;
1040         struct filter_tlv *tlv, *tlv_va;
1041         u64 tlv_size;
1042         u32 filter_size, action_size;
1043         static unsigned int unique_id;
1044         char z_name[RTE_MEMZONE_NAMESIZE];
1045         enum vnic_devcmd_cmd dev_cmd;
1046
1047         if (cmd == CLSF_ADD) {
1048                 dev_cmd = (data->type >= FILTER_DPDK_1) ?
1049                           CMD_ADD_ADV_FILTER : CMD_ADD_FILTER;
1050
1051                 filter_size = vnic_filter_size(data);
1052                 action_size = vnic_action_size(action_v2);
1053
1054                 tlv_size = filter_size + action_size +
1055                     2*sizeof(struct filter_tlv);
1056                 snprintf((char *)z_name, sizeof(z_name),
1057                         "vnic_clsf_%u", unique_id++);
1058                 tlv_va = vdev->alloc_consistent(vdev->priv,
1059                         tlv_size, &tlv_pa, (u8 *)z_name);
1060                 if (!tlv_va)
1061                         return -ENOMEM;
1062                 tlv = tlv_va;
1063                 a0 = tlv_pa;
1064                 a1 = tlv_size;
1065                 memset(tlv, 0, tlv_size);
1066                 tlv->type = CLSF_TLV_FILTER;
1067                 tlv->length = filter_size;
1068                 memcpy(&tlv->val, (void *)data, filter_size);
1069
1070                 tlv = (struct filter_tlv *)((char *)tlv +
1071                                          sizeof(struct filter_tlv) +
1072                                          filter_size);
1073
1074                 tlv->type = CLSF_TLV_ACTION;
1075                 tlv->length = action_size;
1076                 memcpy(&tlv->val, (void *)action_v2, action_size);
1077                 ret = vnic_dev_cmd(vdev, dev_cmd, &a0, &a1, wait);
1078                 *entry = (u16)a0;
1079                 vdev->free_consistent(vdev->priv, tlv_size, tlv_va, tlv_pa);
1080         } else if (cmd == CLSF_DEL) {
1081                 a0 = *entry;
1082                 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);
1083         }
1084
1085         return ret;
1086 }