New upstream version 17.11.3
[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
631         if (!vdev->stats)
632                 return -ENOMEM;
633
634         *stats = vdev->stats;
635         a0 = vdev->stats_pa;
636         a1 = sizeof(struct vnic_stats);
637
638         return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
639 }
640
641 int vnic_dev_close(struct vnic_dev *vdev)
642 {
643         u64 a0 = 0, a1 = 0;
644         int wait = 1000;
645
646         return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
647 }
648
649 int vnic_dev_enable_wait(struct vnic_dev *vdev)
650 {
651         u64 a0 = 0, a1 = 0;
652         int wait = 1000;
653
654         if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
655                 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
656         else
657                 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
658 }
659
660 int vnic_dev_disable(struct vnic_dev *vdev)
661 {
662         u64 a0 = 0, a1 = 0;
663         int wait = 1000;
664
665         return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
666 }
667
668 int vnic_dev_open(struct vnic_dev *vdev, int arg)
669 {
670         u64 a0 = (u32)arg, a1 = 0;
671         int wait = 1000;
672
673         return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
674 }
675
676 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
677 {
678         u64 a0 = 0, a1 = 0;
679         int wait = 1000;
680         int err;
681
682         *done = 0;
683
684         err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
685         if (err)
686                 return err;
687
688         *done = (a0 == 0);
689
690         return 0;
691 }
692
693 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
694 {
695         u64 a0 = 0, a1 = 0;
696         int wait = 1000;
697         int err, i;
698
699         for (i = 0; i < ETH_ALEN; i++)
700                 mac_addr[i] = 0;
701
702         err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
703         if (err)
704                 return err;
705
706         for (i = 0; i < ETH_ALEN; i++)
707                 mac_addr[i] = ((u8 *)&a0)[i];
708
709         return 0;
710 }
711
712 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
713         int broadcast, int promisc, int allmulti)
714 {
715         u64 a0, a1 = 0;
716         int wait = 1000;
717         int err;
718
719         a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
720              (multicast ? CMD_PFILTER_MULTICAST : 0) |
721              (broadcast ? CMD_PFILTER_BROADCAST : 0) |
722              (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
723              (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
724
725         err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
726         if (err)
727                 pr_err("Can't set packet filter\n");
728
729         return err;
730 }
731
732 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
733 {
734         u64 a0 = 0, a1 = 0;
735         int wait = 1000;
736         int err;
737         int i;
738
739         for (i = 0; i < ETH_ALEN; i++)
740                 ((u8 *)&a0)[i] = addr[i];
741
742         err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
743         if (err)
744                 pr_err("Can't add addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
745                         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
746                         err);
747
748         return err;
749 }
750
751 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
752 {
753         u64 a0 = 0, a1 = 0;
754         int wait = 1000;
755         int err;
756         int i;
757
758         for (i = 0; i < ETH_ALEN; i++)
759                 ((u8 *)&a0)[i] = addr[i];
760
761         err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
762         if (err)
763                 pr_err("Can't del addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
764                         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
765                         err);
766
767         return err;
768 }
769
770 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
771         u8 ig_vlan_rewrite_mode)
772 {
773         u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
774         int wait = 1000;
775
776         if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
777                 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
778                                 &a0, &a1, wait);
779         else
780                 return 0;
781 }
782
783 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state)
784 {
785         vdev->in_reset = state;
786 }
787
788 static inline int vnic_dev_in_reset(struct vnic_dev *vdev)
789 {
790         return vdev->in_reset;
791 }
792
793 int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
794         void *notify_addr, dma_addr_t notify_pa, u16 intr)
795 {
796         u64 a0, a1;
797         int wait = 1000;
798         int r;
799
800         memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
801         if (!vnic_dev_in_reset(vdev)) {
802                 vdev->notify = notify_addr;
803                 vdev->notify_pa = notify_pa;
804         }
805
806         a0 = (u64)notify_pa;
807         a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
808         a1 += sizeof(struct vnic_devcmd_notify);
809
810         r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
811         if (!vnic_dev_in_reset(vdev))
812                 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
813
814         return r;
815 }
816
817 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
818 {
819         void *notify_addr = NULL;
820         dma_addr_t notify_pa = 0;
821         char name[NAME_MAX];
822         static u32 instance;
823
824         if (vdev->notify || vdev->notify_pa) {
825                 return vnic_dev_notify_setcmd(vdev, vdev->notify,
826                                               vdev->notify_pa, intr);
827         }
828         if (!vnic_dev_in_reset(vdev)) {
829                 snprintf((char *)name, sizeof(name),
830                         "vnic_notify-%u", instance++);
831                 notify_addr = vdev->alloc_consistent(vdev->priv,
832                         sizeof(struct vnic_devcmd_notify),
833                         &notify_pa, (u8 *)name);
834                 if (!notify_addr)
835                         return -ENOMEM;
836         }
837
838         return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
839 }
840
841 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
842 {
843         u64 a0, a1;
844         int wait = 1000;
845         int err;
846
847         a0 = 0;  /* paddr = 0 to unset notify buffer */
848         a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
849         a1 += sizeof(struct vnic_devcmd_notify);
850
851         err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
852         if (!vnic_dev_in_reset(vdev)) {
853                 vdev->notify = NULL;
854                 vdev->notify_pa = 0;
855                 vdev->notify_sz = 0;
856         }
857
858         return err;
859 }
860
861 int vnic_dev_notify_unset(struct vnic_dev *vdev)
862 {
863         if (vdev->notify && !vnic_dev_in_reset(vdev)) {
864                 vdev->free_consistent(vdev->priv,
865                         sizeof(struct vnic_devcmd_notify),
866                         vdev->notify,
867                         vdev->notify_pa);
868         }
869
870         return vnic_dev_notify_unsetcmd(vdev);
871 }
872
873 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
874 {
875         u32 *words;
876         unsigned int nwords = vdev->notify_sz / 4;
877         unsigned int i;
878         u32 csum;
879
880         if (!vdev->notify || !vdev->notify_sz)
881                 return 0;
882
883         do {
884                 csum = 0;
885                 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
886                 words = (u32 *)&vdev->notify_copy;
887                 for (i = 1; i < nwords; i++)
888                         csum += words[i];
889         } while (csum != words[0]);
890
891         return 1;
892 }
893
894 int vnic_dev_init(struct vnic_dev *vdev, int arg)
895 {
896         u64 a0 = (u32)arg, a1 = 0;
897         int wait = 1000;
898         int r = 0;
899
900         if (vnic_dev_capable(vdev, CMD_INIT))
901                 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
902         else {
903                 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
904                 if (a0 & CMD_INITF_DEFAULT_MAC) {
905                         /* Emulate these for old CMD_INIT_v1 which
906                          * didn't pass a0 so no CMD_INITF_*.
907                          */
908                         vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
909                         vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
910                 }
911         }
912         return r;
913 }
914
915 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
916 {
917         /* Default: hardware intr coal timer is in units of 1.5 usecs */
918         vdev->intr_coal_timer_info.mul = 2;
919         vdev->intr_coal_timer_info.div = 3;
920         vdev->intr_coal_timer_info.max_usec =
921                 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
922 }
923
924 int vnic_dev_link_status(struct vnic_dev *vdev)
925 {
926         if (!vnic_dev_notify_ready(vdev))
927                 return 0;
928
929         return vdev->notify_copy.link_state;
930 }
931
932 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
933 {
934         if (!vnic_dev_notify_ready(vdev))
935                 return 0;
936
937         return vdev->notify_copy.port_speed;
938 }
939
940 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
941 {
942         return (usec * vdev->intr_coal_timer_info.mul) /
943                 vdev->intr_coal_timer_info.div;
944 }
945
946 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
947 {
948         return (hw_cycles * vdev->intr_coal_timer_info.div) /
949                 vdev->intr_coal_timer_info.mul;
950 }
951
952 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
953 {
954         return vdev->intr_coal_timer_info.max_usec;
955 }
956
957 int vnic_dev_alloc_stats_mem(struct vnic_dev *vdev)
958 {
959         char name[NAME_MAX];
960         static u32 instance;
961
962         snprintf((char *)name, sizeof(name), "vnic_stats-%u", instance++);
963         vdev->stats = vdev->alloc_consistent(vdev->priv,
964                                              sizeof(struct vnic_stats),
965                                              &vdev->stats_pa, (u8 *)name);
966         return vdev->stats == NULL ? -ENOMEM : 0;
967 }
968
969 void vnic_dev_unregister(struct vnic_dev *vdev)
970 {
971         if (vdev) {
972                 if (vdev->notify)
973                         vdev->free_consistent(vdev->priv,
974                                 sizeof(struct vnic_devcmd_notify),
975                                 vdev->notify,
976                                 vdev->notify_pa);
977                 if (vdev->stats)
978                         vdev->free_consistent(vdev->priv,
979                                 sizeof(struct vnic_stats),
980                                 vdev->stats, vdev->stats_pa);
981                 if (vdev->fw_info)
982                         vdev->free_consistent(vdev->priv,
983                                 sizeof(struct vnic_devcmd_fw_info),
984                                 vdev->fw_info, vdev->fw_info_pa);
985                 rte_free(vdev);
986         }
987 }
988
989 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
990         void *priv, struct rte_pci_device *pdev, struct vnic_dev_bar *bar,
991         unsigned int num_bars)
992 {
993         if (!vdev) {
994                 char name[NAME_MAX];
995                 snprintf((char *)name, sizeof(name), "%s-vnic",
996                           pdev->device.name);
997                 vdev = (struct vnic_dev *)rte_zmalloc_socket(name,
998                                         sizeof(struct vnic_dev),
999                                         RTE_CACHE_LINE_SIZE,
1000                                         pdev->device.numa_node);
1001                 if (!vdev)
1002                         return NULL;
1003         }
1004
1005         vdev->priv = priv;
1006         vdev->pdev = pdev;
1007
1008         if (vnic_dev_discover_res(vdev, bar, num_bars))
1009                 goto err_out;
1010
1011         vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
1012         if (!vdev->devcmd)
1013                 goto err_out;
1014
1015         return vdev;
1016
1017 err_out:
1018         vnic_dev_unregister(vdev);
1019         return NULL;
1020 }
1021
1022 /*
1023  *  vnic_dev_classifier: Add/Delete classifier entries
1024  *  @vdev: vdev of the device
1025  *  @cmd: CLSF_ADD for Add filter
1026  *        CLSF_DEL for Delete filter
1027  *  @entry: In case of ADD filter, the caller passes the RQ number in this
1028  *          variable.
1029  *          This function stores the filter_id returned by the
1030  *          firmware in the same variable before return;
1031  *
1032  *          In case of DEL filter, the caller passes the RQ number. Return
1033  *          value is irrelevant.
1034  * @data: filter data
1035  * @action: action data
1036  */
1037 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
1038         struct filter_v2 *data, struct filter_action_v2 *action_v2)
1039 {
1040         u64 a0 = 0, a1 = 0;
1041         int wait = 1000;
1042         dma_addr_t tlv_pa;
1043         int ret = -EINVAL;
1044         struct filter_tlv *tlv, *tlv_va;
1045         u64 tlv_size;
1046         u32 filter_size, action_size;
1047         static unsigned int unique_id;
1048         char z_name[RTE_MEMZONE_NAMESIZE];
1049         enum vnic_devcmd_cmd dev_cmd;
1050
1051         if (cmd == CLSF_ADD) {
1052                 dev_cmd = (data->type >= FILTER_DPDK_1) ?
1053                           CMD_ADD_ADV_FILTER : CMD_ADD_FILTER;
1054
1055                 filter_size = vnic_filter_size(data);
1056                 action_size = vnic_action_size(action_v2);
1057
1058                 tlv_size = filter_size + action_size +
1059                     2*sizeof(struct filter_tlv);
1060                 snprintf((char *)z_name, sizeof(z_name),
1061                         "vnic_clsf_%u", unique_id++);
1062                 tlv_va = vdev->alloc_consistent(vdev->priv,
1063                         tlv_size, &tlv_pa, (u8 *)z_name);
1064                 if (!tlv_va)
1065                         return -ENOMEM;
1066                 tlv = tlv_va;
1067                 a0 = tlv_pa;
1068                 a1 = tlv_size;
1069                 memset(tlv, 0, tlv_size);
1070                 tlv->type = CLSF_TLV_FILTER;
1071                 tlv->length = filter_size;
1072                 memcpy(&tlv->val, (void *)data, filter_size);
1073
1074                 tlv = (struct filter_tlv *)((char *)tlv +
1075                                          sizeof(struct filter_tlv) +
1076                                          filter_size);
1077
1078                 tlv->type = CLSF_TLV_ACTION;
1079                 tlv->length = action_size;
1080                 memcpy(&tlv->val, (void *)action_v2, action_size);
1081                 ret = vnic_dev_cmd(vdev, dev_cmd, &a0, &a1, wait);
1082                 *entry = (u16)a0;
1083                 vdev->free_consistent(vdev->priv, tlv_size, tlv_va, tlv_pa);
1084         } else if (cmd == CLSF_DEL) {
1085                 a0 = *entry;
1086                 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);
1087         }
1088
1089         return ret;
1090 }