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