49b36555b709fa0ed45148de0bbe1528b3bd38fe
[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 = NULL;
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 void vnic_dev_cmd_proxy_by_index_start(struct vnic_dev *vdev, u16 index)
447 {
448         vdev->proxy = PROXY_BY_INDEX;
449         vdev->proxy_index = index;
450 }
451
452 void vnic_dev_cmd_proxy_by_bdf_start(struct vnic_dev *vdev, u16 bdf)
453 {
454         vdev->proxy = PROXY_BY_BDF;
455         vdev->proxy_index = bdf;
456 }
457
458 void vnic_dev_cmd_proxy_end(struct vnic_dev *vdev)
459 {
460         vdev->proxy = PROXY_NONE;
461         vdev->proxy_index = 0;
462 }
463
464 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
465         u64 *a0, u64 *a1, int wait)
466 {
467         u64 args[2];
468         int err;
469
470         args[0] = *a0;
471         args[1] = *a1;
472         memset(vdev->args, 0, sizeof(vdev->args));
473
474         switch (vdev->proxy) {
475         case PROXY_BY_INDEX:
476                 err =  vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
477                                 args, ARRAY_SIZE(args), wait);
478                 break;
479         case PROXY_BY_BDF:
480                 err =  vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
481                                 args, ARRAY_SIZE(args), wait);
482                 break;
483         case PROXY_NONE:
484         default:
485                 err = vnic_dev_cmd_no_proxy(vdev, cmd, args, 2, wait);
486                 break;
487         }
488
489         if (err == 0) {
490                 *a0 = args[0];
491                 *a1 = args[1];
492         }
493
494         return err;
495 }
496
497 int vnic_dev_cmd_args(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
498                       u64 *args, int nargs, int wait)
499 {
500         switch (vdev->proxy) {
501         case PROXY_BY_INDEX:
502                 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
503                                 args, nargs, wait);
504         case PROXY_BY_BDF:
505                 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
506                                 args, nargs, wait);
507         case PROXY_NONE:
508         default:
509                 return vnic_dev_cmd_no_proxy(vdev, cmd, args, nargs, wait);
510         }
511 }
512
513 static int vnic_dev_advanced_filters_cap(struct vnic_dev *vdev, u64 *args,
514                 int nargs)
515 {
516         memset(args, 0, nargs * sizeof(*args));
517         args[0] = CMD_ADD_ADV_FILTER;
518         args[1] = FILTER_CAP_MODE_V1_FLAG;
519         return vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, nargs, 1000);
520 }
521
522 int vnic_dev_capable_adv_filters(struct vnic_dev *vdev)
523 {
524         u64 a0 = CMD_ADD_ADV_FILTER, a1 = 0;
525         int wait = 1000;
526         int err;
527
528         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
529         if (err)
530                 return 0;
531         return (a1 >= (u32)FILTER_DPDK_1);
532 }
533
534 /*  Determine the "best" filtering mode VIC is capaible of. Returns one of 3
535  *  value or 0 on error:
536  *      FILTER_DPDK_1- advanced filters availabile
537  *      FILTER_USNIC_IP_FLAG - advanced filters but with the restriction that
538  *              the IP layer must explicitly specified. I.e. cannot have a UDP
539  *              filter that matches both IPv4 and IPv6.
540  *      FILTER_IPV4_5TUPLE - fallback if either of the 2 above aren't available.
541  *              all other filter types are not available.
542  *   Retrun true in filter_tags if supported
543  */
544 int vnic_dev_capable_filter_mode(struct vnic_dev *vdev, u32 *mode,
545                                  u8 *filter_tags)
546 {
547         u64 args[4];
548         int err;
549         u32 max_level = 0;
550
551         err = vnic_dev_advanced_filters_cap(vdev, args, 4);
552
553         /* determine if filter tags are available */
554         if (err)
555                 *filter_tags = 0;
556         if ((args[2] == FILTER_CAP_MODE_V1) &&
557             (args[3] & FILTER_ACTION_FILTER_ID_FLAG))
558                 *filter_tags = 1;
559         else
560                 *filter_tags = 0;
561
562         if (err || ((args[0] == 1) && (args[1] == 0))) {
563                 /* Adv filter Command not supported or adv filters available but
564                  * not enabled. Try the normal filter capability command.
565                  */
566                 args[0] = CMD_ADD_FILTER;
567                 args[1] = 0;
568                 err = vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, 2, 1000);
569                 if (err)
570                         return err;
571                 max_level = args[1];
572                 goto parse_max_level;
573         } else if (args[2] == FILTER_CAP_MODE_V1) {
574                 /* parse filter capability mask in args[1] */
575                 if (args[1] & FILTER_DPDK_1_FLAG)
576                         *mode = FILTER_DPDK_1;
577                 else if (args[1] & FILTER_USNIC_IP_FLAG)
578                         *mode = FILTER_USNIC_IP;
579                 else if (args[1] & FILTER_IPV4_5TUPLE_FLAG)
580                         *mode = FILTER_IPV4_5TUPLE;
581                 return 0;
582         }
583         max_level = args[1];
584 parse_max_level:
585         if (max_level >= (u32)FILTER_USNIC_IP)
586                 *mode = FILTER_USNIC_IP;
587         else
588                 *mode = FILTER_IPV4_5TUPLE;
589         return 0;
590 }
591
592 int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
593 {
594         u64 a0 = (u32)cmd, a1 = 0;
595         int wait = 1000;
596         int err;
597
598         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
599
600         return !(err || a0);
601 }
602
603 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, size_t size,
604         void *value)
605 {
606         u64 a0, a1;
607         int wait = 1000;
608         int err;
609
610         a0 = offset;
611         a1 = size;
612
613         err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
614
615         switch (size) {
616         case 1:
617                 *(u8 *)value = (u8)a0;
618                 break;
619         case 2:
620                 *(u16 *)value = (u16)a0;
621                 break;
622         case 4:
623                 *(u32 *)value = (u32)a0;
624                 break;
625         case 8:
626                 *(u64 *)value = a0;
627                 break;
628         default:
629                 BUG();
630                 break;
631         }
632
633         return err;
634 }
635
636 int vnic_dev_stats_clear(struct vnic_dev *vdev)
637 {
638         u64 a0 = 0, a1 = 0;
639         int wait = 1000;
640
641         return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
642 }
643
644 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
645 {
646         u64 a0, a1;
647         int wait = 1000;
648         static u32 instance;
649         char name[NAME_MAX];
650
651         if (!vdev->stats) {
652                 snprintf((char *)name, sizeof(name),
653                         "vnic_stats-%d", instance++);
654                 vdev->stats = vdev->alloc_consistent(vdev->priv,
655                         sizeof(struct vnic_stats), &vdev->stats_pa, (u8 *)name);
656                 if (!vdev->stats)
657                         return -ENOMEM;
658         }
659
660         *stats = vdev->stats;
661         a0 = vdev->stats_pa;
662         a1 = sizeof(struct vnic_stats);
663
664         return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
665 }
666
667 int vnic_dev_close(struct vnic_dev *vdev)
668 {
669         u64 a0 = 0, a1 = 0;
670         int wait = 1000;
671
672         return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
673 }
674
675 /** Deprecated.  @see vnic_dev_enable_wait */
676 int vnic_dev_enable(struct vnic_dev *vdev)
677 {
678         u64 a0 = 0, a1 = 0;
679         int wait = 1000;
680
681         return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
682 }
683
684 int vnic_dev_enable_wait(struct vnic_dev *vdev)
685 {
686         u64 a0 = 0, a1 = 0;
687         int wait = 1000;
688
689         if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
690                 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
691         else
692                 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
693 }
694
695 int vnic_dev_disable(struct vnic_dev *vdev)
696 {
697         u64 a0 = 0, a1 = 0;
698         int wait = 1000;
699
700         return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
701 }
702
703 int vnic_dev_open(struct vnic_dev *vdev, int arg)
704 {
705         u64 a0 = (u32)arg, a1 = 0;
706         int wait = 1000;
707
708         return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
709 }
710
711 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
712 {
713         u64 a0 = 0, a1 = 0;
714         int wait = 1000;
715         int err;
716
717         *done = 0;
718
719         err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
720         if (err)
721                 return err;
722
723         *done = (a0 == 0);
724
725         return 0;
726 }
727
728 int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
729 {
730         u64 a0 = (u32)arg, a1 = 0;
731         int wait = 1000;
732
733         return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
734 }
735
736 int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
737 {
738         u64 a0 = 0, a1 = 0;
739         int wait = 1000;
740         int err;
741
742         *done = 0;
743
744         err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
745         if (err)
746                 return err;
747
748         *done = (a0 == 0);
749
750         return 0;
751 }
752
753 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
754 {
755         u64 a0 = 0, a1 = 0;
756         int wait = 1000;
757         int err, i;
758
759         for (i = 0; i < ETH_ALEN; i++)
760                 mac_addr[i] = 0;
761
762         err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
763         if (err)
764                 return err;
765
766         for (i = 0; i < ETH_ALEN; i++)
767                 mac_addr[i] = ((u8 *)&a0)[i];
768
769         return 0;
770 }
771
772 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
773         int broadcast, int promisc, int allmulti)
774 {
775         u64 a0, a1 = 0;
776         int wait = 1000;
777         int err;
778
779         a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
780              (multicast ? CMD_PFILTER_MULTICAST : 0) |
781              (broadcast ? CMD_PFILTER_BROADCAST : 0) |
782              (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
783              (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
784
785         err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
786         if (err)
787                 pr_err("Can't set packet filter\n");
788
789         return err;
790 }
791
792 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
793 {
794         u64 a0 = 0, a1 = 0;
795         int wait = 1000;
796         int err;
797         int i;
798
799         for (i = 0; i < ETH_ALEN; i++)
800                 ((u8 *)&a0)[i] = addr[i];
801
802         err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
803         if (err)
804                 pr_err("Can't add addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
805                         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
806                         err);
807
808         return err;
809 }
810
811 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
812 {
813         u64 a0 = 0, a1 = 0;
814         int wait = 1000;
815         int err;
816         int i;
817
818         for (i = 0; i < ETH_ALEN; i++)
819                 ((u8 *)&a0)[i] = addr[i];
820
821         err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
822         if (err)
823                 pr_err("Can't del addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
824                         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
825                         err);
826
827         return err;
828 }
829
830 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
831         u8 ig_vlan_rewrite_mode)
832 {
833         u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
834         int wait = 1000;
835
836         if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
837                 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
838                                 &a0, &a1, wait);
839         else
840                 return 0;
841 }
842
843 int vnic_dev_raise_intr(struct vnic_dev *vdev, u16 intr)
844 {
845         u64 a0 = intr, a1 = 0;
846         int wait = 1000;
847         int err;
848
849         err = vnic_dev_cmd(vdev, CMD_IAR, &a0, &a1, wait);
850         if (err)
851                 pr_err("Failed to raise INTR[%d], err %d\n", intr, err);
852
853         return err;
854 }
855
856 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state)
857 {
858         vdev->in_reset = state;
859 }
860
861 static inline int vnic_dev_in_reset(struct vnic_dev *vdev)
862 {
863         return vdev->in_reset;
864 }
865
866 int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
867         void *notify_addr, dma_addr_t notify_pa, u16 intr)
868 {
869         u64 a0, a1;
870         int wait = 1000;
871         int r;
872
873         memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
874         if (!vnic_dev_in_reset(vdev)) {
875                 vdev->notify = notify_addr;
876                 vdev->notify_pa = notify_pa;
877         }
878
879         a0 = (u64)notify_pa;
880         a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
881         a1 += sizeof(struct vnic_devcmd_notify);
882
883         r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
884         if (!vnic_dev_in_reset(vdev))
885                 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
886
887         return r;
888 }
889
890 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
891 {
892         void *notify_addr = NULL;
893         dma_addr_t notify_pa = 0;
894         char name[NAME_MAX];
895         static u32 instance;
896
897         if (vdev->notify || vdev->notify_pa) {
898                 return vnic_dev_notify_setcmd(vdev, vdev->notify,
899                                               vdev->notify_pa, intr);
900         }
901         if (!vnic_dev_in_reset(vdev)) {
902                 snprintf((char *)name, sizeof(name),
903                         "vnic_notify-%d", instance++);
904                 notify_addr = vdev->alloc_consistent(vdev->priv,
905                         sizeof(struct vnic_devcmd_notify),
906                         &notify_pa, (u8 *)name);
907                 if (!notify_addr)
908                         return -ENOMEM;
909         }
910
911         return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
912 }
913
914 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
915 {
916         u64 a0, a1;
917         int wait = 1000;
918         int err;
919
920         a0 = 0;  /* paddr = 0 to unset notify buffer */
921         a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
922         a1 += sizeof(struct vnic_devcmd_notify);
923
924         err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
925         if (!vnic_dev_in_reset(vdev)) {
926                 vdev->notify = NULL;
927                 vdev->notify_pa = 0;
928                 vdev->notify_sz = 0;
929         }
930
931         return err;
932 }
933
934 int vnic_dev_notify_unset(struct vnic_dev *vdev)
935 {
936         if (vdev->notify && !vnic_dev_in_reset(vdev)) {
937                 vdev->free_consistent(vdev->priv,
938                         sizeof(struct vnic_devcmd_notify),
939                         vdev->notify,
940                         vdev->notify_pa);
941         }
942
943         return vnic_dev_notify_unsetcmd(vdev);
944 }
945
946 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
947 {
948         u32 *words;
949         unsigned int nwords = vdev->notify_sz / 4;
950         unsigned int i;
951         u32 csum;
952
953         if (!vdev->notify || !vdev->notify_sz)
954                 return 0;
955
956         do {
957                 csum = 0;
958                 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
959                 words = (u32 *)&vdev->notify_copy;
960                 for (i = 1; i < nwords; i++)
961                         csum += words[i];
962         } while (csum != words[0]);
963
964         return 1;
965 }
966
967 int vnic_dev_init(struct vnic_dev *vdev, int arg)
968 {
969         u64 a0 = (u32)arg, a1 = 0;
970         int wait = 1000;
971         int r = 0;
972
973         if (vnic_dev_capable(vdev, CMD_INIT))
974                 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
975         else {
976                 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
977                 if (a0 & CMD_INITF_DEFAULT_MAC) {
978                         /* Emulate these for old CMD_INIT_v1 which
979                          * didn't pass a0 so no CMD_INITF_*.
980                          */
981                         vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
982                         vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
983                 }
984         }
985         return r;
986 }
987
988 int vnic_dev_deinit(struct vnic_dev *vdev)
989 {
990         u64 a0 = 0, a1 = 0;
991         int wait = 1000;
992
993         return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait);
994 }
995
996 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
997 {
998         /* Default: hardware intr coal timer is in units of 1.5 usecs */
999         vdev->intr_coal_timer_info.mul = 2;
1000         vdev->intr_coal_timer_info.div = 3;
1001         vdev->intr_coal_timer_info.max_usec =
1002                 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
1003 }
1004
1005 int vnic_dev_link_status(struct vnic_dev *vdev)
1006 {
1007         if (!vnic_dev_notify_ready(vdev))
1008                 return 0;
1009
1010         return vdev->notify_copy.link_state;
1011 }
1012
1013 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
1014 {
1015         if (!vnic_dev_notify_ready(vdev))
1016                 return 0;
1017
1018         return vdev->notify_copy.port_speed;
1019 }
1020
1021 void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
1022         enum vnic_dev_intr_mode intr_mode)
1023 {
1024         vdev->intr_mode = intr_mode;
1025 }
1026
1027 enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
1028         struct vnic_dev *vdev)
1029 {
1030         return vdev->intr_mode;
1031 }
1032
1033 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
1034 {
1035         return (usec * vdev->intr_coal_timer_info.mul) /
1036                 vdev->intr_coal_timer_info.div;
1037 }
1038
1039 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
1040 {
1041         return (hw_cycles * vdev->intr_coal_timer_info.div) /
1042                 vdev->intr_coal_timer_info.mul;
1043 }
1044
1045 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
1046 {
1047         return vdev->intr_coal_timer_info.max_usec;
1048 }
1049
1050 void vnic_dev_unregister(struct vnic_dev *vdev)
1051 {
1052         if (vdev) {
1053                 if (vdev->notify)
1054                         vdev->free_consistent(vdev->priv,
1055                                 sizeof(struct vnic_devcmd_notify),
1056                                 vdev->notify,
1057                                 vdev->notify_pa);
1058                 if (vdev->stats)
1059                         vdev->free_consistent(vdev->priv,
1060                                 sizeof(struct vnic_stats),
1061                                 vdev->stats, vdev->stats_pa);
1062                 if (vdev->fw_info)
1063                         vdev->free_consistent(vdev->priv,
1064                                 sizeof(struct vnic_devcmd_fw_info),
1065                                 vdev->fw_info, vdev->fw_info_pa);
1066                 kfree(vdev);
1067         }
1068 }
1069
1070 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
1071         void *priv, struct rte_pci_device *pdev, struct vnic_dev_bar *bar,
1072         unsigned int num_bars)
1073 {
1074         if (!vdev) {
1075                 vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
1076                 if (!vdev)
1077                         return NULL;
1078         }
1079
1080         vdev->priv = priv;
1081         vdev->pdev = pdev;
1082
1083         if (vnic_dev_discover_res(vdev, bar, num_bars))
1084                 goto err_out;
1085
1086         vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
1087         if (!vdev->devcmd)
1088                 goto err_out;
1089
1090         return vdev;
1091
1092 err_out:
1093         vnic_dev_unregister(vdev);
1094         return NULL;
1095 }
1096
1097 struct rte_pci_device *vnic_dev_get_pdev(struct vnic_dev *vdev)
1098 {
1099         return vdev->pdev;
1100 }
1101
1102 int vnic_dev_set_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
1103 {
1104         u64 a0, a1 = 0;
1105         int wait = 1000;
1106         int i;
1107
1108         for (i = 0; i < ETH_ALEN; i++)
1109                 ((u8 *)&a0)[i] = mac_addr[i];
1110
1111         return vnic_dev_cmd(vdev, CMD_SET_MAC_ADDR, &a0, &a1, wait);
1112 }
1113
1114 /*
1115  *  vnic_dev_classifier: Add/Delete classifier entries
1116  *  @vdev: vdev of the device
1117  *  @cmd: CLSF_ADD for Add filter
1118  *        CLSF_DEL for Delete filter
1119  *  @entry: In case of ADD filter, the caller passes the RQ number in this
1120  *          variable.
1121  *          This function stores the filter_id returned by the
1122  *          firmware in the same variable before return;
1123  *
1124  *          In case of DEL filter, the caller passes the RQ number. Return
1125  *          value is irrelevant.
1126  * @data: filter data
1127  * @action: action data
1128  */
1129 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
1130         struct filter_v2 *data, struct filter_action_v2 *action_v2)
1131 {
1132         u64 a0 = 0, a1 = 0;
1133         int wait = 1000;
1134         dma_addr_t tlv_pa;
1135         int ret = -EINVAL;
1136         struct filter_tlv *tlv, *tlv_va;
1137         u64 tlv_size;
1138         u32 filter_size, action_size;
1139         static unsigned int unique_id;
1140         char z_name[RTE_MEMZONE_NAMESIZE];
1141         enum vnic_devcmd_cmd dev_cmd;
1142
1143         if (cmd == CLSF_ADD) {
1144                 dev_cmd = (data->type >= FILTER_DPDK_1) ?
1145                           CMD_ADD_ADV_FILTER : CMD_ADD_FILTER;
1146
1147                 filter_size = vnic_filter_size(data);
1148                 action_size = vnic_action_size(action_v2);
1149
1150                 tlv_size = filter_size + action_size +
1151                     2*sizeof(struct filter_tlv);
1152                 snprintf((char *)z_name, sizeof(z_name),
1153                         "vnic_clsf_%d", unique_id++);
1154                 tlv_va = vdev->alloc_consistent(vdev->priv,
1155                         tlv_size, &tlv_pa, (u8 *)z_name);
1156                 if (!tlv_va)
1157                         return -ENOMEM;
1158                 tlv = tlv_va;
1159                 a0 = tlv_pa;
1160                 a1 = tlv_size;
1161                 memset(tlv, 0, tlv_size);
1162                 tlv->type = CLSF_TLV_FILTER;
1163                 tlv->length = filter_size;
1164                 memcpy(&tlv->val, (void *)data, filter_size);
1165
1166                 tlv = (struct filter_tlv *)((char *)tlv +
1167                                          sizeof(struct filter_tlv) +
1168                                          filter_size);
1169
1170                 tlv->type = CLSF_TLV_ACTION;
1171                 tlv->length = action_size;
1172                 memcpy(&tlv->val, (void *)action_v2, action_size);
1173                 ret = vnic_dev_cmd(vdev, dev_cmd, &a0, &a1, wait);
1174                 *entry = (u16)a0;
1175                 vdev->free_consistent(vdev->priv, tlv_size, tlv_va, tlv_pa);
1176         } else if (cmd == CLSF_DEL) {
1177                 a0 = *entry;
1178                 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);
1179         }
1180
1181         return ret;
1182 }