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