New upstream version 16.11.7
[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 *a0, u64 *a1, int wait)
391 {
392         u32 status;
393         int err;
394
395         memset(vdev->args, 0, sizeof(vdev->args));
396
397         vdev->args[0] = vdev->proxy_index;
398         vdev->args[1] = cmd;
399         vdev->args[2] = *a0;
400         vdev->args[3] = *a1;
401
402         err = _vnic_dev_cmd(vdev, proxy_cmd, wait);
403         if (err)
404                 return err;
405
406         status = (u32)vdev->args[0];
407         if (status & STAT_ERROR) {
408                 err = (int)vdev->args[1];
409                 if (err != ERR_ECMDUNKNOWN ||
410                     cmd != CMD_CAPABILITY)
411                         pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd));
412                 return err;
413         }
414
415         *a0 = vdev->args[1];
416         *a1 = vdev->args[2];
417
418         return 0;
419 }
420
421 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
422         enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait)
423 {
424         int err;
425
426         vdev->args[0] = *a0;
427         vdev->args[1] = *a1;
428
429         err = _vnic_dev_cmd(vdev, cmd, wait);
430
431         *a0 = vdev->args[0];
432         *a1 = vdev->args[1];
433
434         return err;
435 }
436
437 void vnic_dev_cmd_proxy_by_index_start(struct vnic_dev *vdev, u16 index)
438 {
439         vdev->proxy = PROXY_BY_INDEX;
440         vdev->proxy_index = index;
441 }
442
443 void vnic_dev_cmd_proxy_by_bdf_start(struct vnic_dev *vdev, u16 bdf)
444 {
445         vdev->proxy = PROXY_BY_BDF;
446         vdev->proxy_index = bdf;
447 }
448
449 void vnic_dev_cmd_proxy_end(struct vnic_dev *vdev)
450 {
451         vdev->proxy = PROXY_NONE;
452         vdev->proxy_index = 0;
453 }
454
455 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
456         u64 *a0, u64 *a1, int wait)
457 {
458         memset(vdev->args, 0, sizeof(vdev->args));
459
460         switch (vdev->proxy) {
461         case PROXY_BY_INDEX:
462                 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
463                                 a0, a1, wait);
464         case PROXY_BY_BDF:
465                 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
466                                 a0, a1, wait);
467         case PROXY_NONE:
468         default:
469                 return vnic_dev_cmd_no_proxy(vdev, cmd, a0, a1, wait);
470         }
471 }
472
473 int vnic_dev_capable_adv_filters(struct vnic_dev *vdev)
474 {
475         u64 a0 = (u32)CMD_ADD_ADV_FILTER, a1 = 0;
476         int wait = 1000;
477         int err;
478
479         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
480         if (err)
481                 return 0;
482         return (a1 >= (u32)FILTER_DPDK_1);
483 }
484
485 static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
486 {
487         u64 a0 = (u32)cmd, a1 = 0;
488         int wait = 1000;
489         int err;
490
491         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
492
493         return !(err || a0);
494 }
495
496 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, size_t size,
497         void *value)
498 {
499         u64 a0, a1;
500         int wait = 1000;
501         int err;
502
503         a0 = offset;
504         a1 = size;
505
506         err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
507
508         switch (size) {
509         case 1:
510                 *(u8 *)value = (u8)a0;
511                 break;
512         case 2:
513                 *(u16 *)value = (u16)a0;
514                 break;
515         case 4:
516                 *(u32 *)value = (u32)a0;
517                 break;
518         case 8:
519                 *(u64 *)value = a0;
520                 break;
521         default:
522                 BUG();
523                 break;
524         }
525
526         return err;
527 }
528
529 int vnic_dev_stats_clear(struct vnic_dev *vdev)
530 {
531         u64 a0 = 0, a1 = 0;
532         int wait = 1000;
533
534         return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
535 }
536
537 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
538 {
539         u64 a0, a1;
540         int wait = 1000;
541
542         if (!vdev->stats)
543                 return -ENOMEM;
544
545         *stats = vdev->stats;
546         a0 = vdev->stats_pa;
547         a1 = sizeof(struct vnic_stats);
548
549         return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
550 }
551
552 int vnic_dev_close(struct vnic_dev *vdev)
553 {
554         u64 a0 = 0, a1 = 0;
555         int wait = 1000;
556
557         return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
558 }
559
560 /** Deprecated.  @see vnic_dev_enable_wait */
561 int vnic_dev_enable(struct vnic_dev *vdev)
562 {
563         u64 a0 = 0, a1 = 0;
564         int wait = 1000;
565
566         return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
567 }
568
569 int vnic_dev_enable_wait(struct vnic_dev *vdev)
570 {
571         u64 a0 = 0, a1 = 0;
572         int wait = 1000;
573
574         if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
575                 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
576         else
577                 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
578 }
579
580 int vnic_dev_disable(struct vnic_dev *vdev)
581 {
582         u64 a0 = 0, a1 = 0;
583         int wait = 1000;
584
585         return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
586 }
587
588 int vnic_dev_open(struct vnic_dev *vdev, int arg)
589 {
590         u64 a0 = (u32)arg, a1 = 0;
591         int wait = 1000;
592
593         return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
594 }
595
596 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
597 {
598         u64 a0 = 0, a1 = 0;
599         int wait = 1000;
600         int err;
601
602         *done = 0;
603
604         err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
605         if (err)
606                 return err;
607
608         *done = (a0 == 0);
609
610         return 0;
611 }
612
613 int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
614 {
615         u64 a0 = (u32)arg, a1 = 0;
616         int wait = 1000;
617
618         return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
619 }
620
621 int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
622 {
623         u64 a0 = 0, a1 = 0;
624         int wait = 1000;
625         int err;
626
627         *done = 0;
628
629         err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
630         if (err)
631                 return err;
632
633         *done = (a0 == 0);
634
635         return 0;
636 }
637
638 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
639 {
640         u64 a0 = 0, a1 = 0;
641         int wait = 1000;
642         int err, i;
643
644         for (i = 0; i < ETH_ALEN; i++)
645                 mac_addr[i] = 0;
646
647         err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
648         if (err)
649                 return err;
650
651         for (i = 0; i < ETH_ALEN; i++)
652                 mac_addr[i] = ((u8 *)&a0)[i];
653
654         return 0;
655 }
656
657 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
658         int broadcast, int promisc, int allmulti)
659 {
660         u64 a0, a1 = 0;
661         int wait = 1000;
662         int err;
663
664         a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
665              (multicast ? CMD_PFILTER_MULTICAST : 0) |
666              (broadcast ? CMD_PFILTER_BROADCAST : 0) |
667              (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
668              (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
669
670         err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
671         if (err)
672                 pr_err("Can't set packet filter\n");
673
674         return err;
675 }
676
677 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
678 {
679         u64 a0 = 0, a1 = 0;
680         int wait = 1000;
681         int err;
682         int i;
683
684         for (i = 0; i < ETH_ALEN; i++)
685                 ((u8 *)&a0)[i] = addr[i];
686
687         err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
688         if (err)
689                 pr_err("Can't add addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
690                         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
691                         err);
692
693         return err;
694 }
695
696 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
697 {
698         u64 a0 = 0, a1 = 0;
699         int wait = 1000;
700         int err;
701         int i;
702
703         for (i = 0; i < ETH_ALEN; i++)
704                 ((u8 *)&a0)[i] = addr[i];
705
706         err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
707         if (err)
708                 pr_err("Can't del addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
709                         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
710                         err);
711
712         return err;
713 }
714
715 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
716         u8 ig_vlan_rewrite_mode)
717 {
718         u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
719         int wait = 1000;
720
721         if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
722                 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
723                                 &a0, &a1, wait);
724         else
725                 return 0;
726 }
727
728 int vnic_dev_raise_intr(struct vnic_dev *vdev, u16 intr)
729 {
730         u64 a0 = intr, a1 = 0;
731         int wait = 1000;
732         int err;
733
734         err = vnic_dev_cmd(vdev, CMD_IAR, &a0, &a1, wait);
735         if (err)
736                 pr_err("Failed to raise INTR[%d], err %d\n", intr, err);
737
738         return err;
739 }
740
741 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state)
742 {
743         vdev->in_reset = state;
744 }
745
746 static inline int vnic_dev_in_reset(struct vnic_dev *vdev)
747 {
748         return vdev->in_reset;
749 }
750
751 int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
752         void *notify_addr, dma_addr_t notify_pa, u16 intr)
753 {
754         u64 a0, a1;
755         int wait = 1000;
756         int r;
757
758         memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
759         if (!vnic_dev_in_reset(vdev)) {
760                 vdev->notify = notify_addr;
761                 vdev->notify_pa = notify_pa;
762         }
763
764         a0 = (u64)notify_pa;
765         a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
766         a1 += sizeof(struct vnic_devcmd_notify);
767
768         r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
769         if (!vnic_dev_in_reset(vdev))
770                 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
771
772         return r;
773 }
774
775 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
776 {
777         void *notify_addr = NULL;
778         dma_addr_t notify_pa = 0;
779         char name[NAME_MAX];
780         static u32 instance;
781
782         if (vdev->notify || vdev->notify_pa) {
783                 return vnic_dev_notify_setcmd(vdev, vdev->notify,
784                                               vdev->notify_pa, intr);
785         }
786         if (!vnic_dev_in_reset(vdev)) {
787                 snprintf((char *)name, sizeof(name),
788                         "vnic_notify-%d", instance++);
789                 notify_addr = vdev->alloc_consistent(vdev->priv,
790                         sizeof(struct vnic_devcmd_notify),
791                         &notify_pa, (u8 *)name);
792                 if (!notify_addr)
793                         return -ENOMEM;
794         }
795
796         return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
797 }
798
799 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
800 {
801         u64 a0, a1;
802         int wait = 1000;
803         int err;
804
805         a0 = 0;  /* paddr = 0 to unset notify buffer */
806         a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
807         a1 += sizeof(struct vnic_devcmd_notify);
808
809         err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
810         if (!vnic_dev_in_reset(vdev)) {
811                 vdev->notify = NULL;
812                 vdev->notify_pa = 0;
813                 vdev->notify_sz = 0;
814         }
815
816         return err;
817 }
818
819 int vnic_dev_notify_unset(struct vnic_dev *vdev)
820 {
821         if (vdev->notify && !vnic_dev_in_reset(vdev)) {
822                 vdev->free_consistent(vdev->priv,
823                         sizeof(struct vnic_devcmd_notify),
824                         vdev->notify,
825                         vdev->notify_pa);
826         }
827
828         return vnic_dev_notify_unsetcmd(vdev);
829 }
830
831 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
832 {
833         u32 *words;
834         unsigned int nwords = vdev->notify_sz / 4;
835         unsigned int i;
836         u32 csum;
837
838         if (!vdev->notify || !vdev->notify_sz)
839                 return 0;
840
841         do {
842                 csum = 0;
843                 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
844                 words = (u32 *)&vdev->notify_copy;
845                 for (i = 1; i < nwords; i++)
846                         csum += words[i];
847         } while (csum != words[0]);
848
849         return 1;
850 }
851
852 int vnic_dev_init(struct vnic_dev *vdev, int arg)
853 {
854         u64 a0 = (u32)arg, a1 = 0;
855         int wait = 1000;
856         int r = 0;
857
858         if (vnic_dev_capable(vdev, CMD_INIT))
859                 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
860         else {
861                 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
862                 if (a0 & CMD_INITF_DEFAULT_MAC) {
863                         /* Emulate these for old CMD_INIT_v1 which
864                          * didn't pass a0 so no CMD_INITF_*.
865                          */
866                         vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
867                         vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
868                 }
869         }
870         return r;
871 }
872
873 int vnic_dev_deinit(struct vnic_dev *vdev)
874 {
875         u64 a0 = 0, a1 = 0;
876         int wait = 1000;
877
878         return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait);
879 }
880
881 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
882 {
883         /* Default: hardware intr coal timer is in units of 1.5 usecs */
884         vdev->intr_coal_timer_info.mul = 2;
885         vdev->intr_coal_timer_info.div = 3;
886         vdev->intr_coal_timer_info.max_usec =
887                 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
888 }
889
890 int vnic_dev_link_status(struct vnic_dev *vdev)
891 {
892         if (!vnic_dev_notify_ready(vdev))
893                 return 0;
894
895         return vdev->notify_copy.link_state;
896 }
897
898 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
899 {
900         if (!vnic_dev_notify_ready(vdev))
901                 return 0;
902
903         return vdev->notify_copy.port_speed;
904 }
905
906 void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
907         enum vnic_dev_intr_mode intr_mode)
908 {
909         vdev->intr_mode = intr_mode;
910 }
911
912 enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
913         struct vnic_dev *vdev)
914 {
915         return vdev->intr_mode;
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                 kfree(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                 vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
973                 if (!vdev)
974                         return NULL;
975         }
976
977         vdev->priv = priv;
978         vdev->pdev = pdev;
979
980         if (vnic_dev_discover_res(vdev, bar, num_bars))
981                 goto err_out;
982
983         vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
984         if (!vdev->devcmd)
985                 goto err_out;
986
987         return vdev;
988
989 err_out:
990         vnic_dev_unregister(vdev);
991         return NULL;
992 }
993
994 struct rte_pci_device *vnic_dev_get_pdev(struct vnic_dev *vdev)
995 {
996         return vdev->pdev;
997 }
998
999 int vnic_dev_set_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
1000 {
1001         u64 a0, a1 = 0;
1002         int wait = 1000;
1003         int i;
1004
1005         for (i = 0; i < ETH_ALEN; i++)
1006                 ((u8 *)&a0)[i] = mac_addr[i];
1007
1008         return vnic_dev_cmd(vdev, CMD_SET_MAC_ADDR, &a0, &a1, wait);
1009 }
1010
1011 /*
1012  *  vnic_dev_classifier: Add/Delete classifier entries
1013  *  @vdev: vdev of the device
1014  *  @cmd: CLSF_ADD for Add filter
1015  *        CLSF_DEL for Delete filter
1016  *  @entry: In case of ADD filter, the caller passes the RQ number in this
1017  *          variable.
1018  *          This function stores the filter_id returned by the
1019  *          firmware in the same variable before return;
1020  *
1021  *          In case of DEL filter, the caller passes the RQ number. Return
1022  *          value is irrelevant.
1023  * @data: filter data
1024  */
1025 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
1026         struct filter_v2 *data)
1027 {
1028         u64 a0 = 0, a1 = 0;
1029         int wait = 1000;
1030         dma_addr_t tlv_pa;
1031         int ret = -EINVAL;
1032         struct filter_tlv *tlv, *tlv_va;
1033         struct filter_action *action;
1034         u64 tlv_size;
1035         u32 filter_size;
1036         static unsigned int unique_id;
1037         char z_name[RTE_MEMZONE_NAMESIZE];
1038         enum vnic_devcmd_cmd dev_cmd;
1039
1040
1041         if (cmd == CLSF_ADD) {
1042                 if (data->type == FILTER_DPDK_1)
1043                         dev_cmd = CMD_ADD_ADV_FILTER;
1044                 else
1045                         dev_cmd = CMD_ADD_FILTER;
1046
1047                 filter_size = vnic_filter_size(data);
1048                 tlv_size = filter_size +
1049                     sizeof(struct filter_action) +
1050                     2*sizeof(struct filter_tlv);
1051                 snprintf((char *)z_name, sizeof(z_name),
1052                         "vnic_clsf_%d", unique_id++);
1053                 tlv_va = vdev->alloc_consistent(vdev->priv,
1054                         tlv_size, &tlv_pa, (u8 *)z_name);
1055                 if (!tlv_va)
1056                         return -ENOMEM;
1057                 tlv = tlv_va;
1058                 a0 = tlv_pa;
1059                 a1 = tlv_size;
1060                 memset(tlv, 0, tlv_size);
1061                 tlv->type = CLSF_TLV_FILTER;
1062                 tlv->length = filter_size;
1063                 memcpy(&tlv->val, (void *)data, filter_size);
1064
1065                 tlv = (struct filter_tlv *)((char *)tlv +
1066                                          sizeof(struct filter_tlv) +
1067                                          filter_size);
1068
1069                 tlv->type = CLSF_TLV_ACTION;
1070                 tlv->length = sizeof(struct filter_action);
1071                 action = (struct filter_action *)&tlv->val;
1072                 action->type = FILTER_ACTION_RQ_STEERING;
1073                 action->u.rq_idx = *entry;
1074
1075                 ret = vnic_dev_cmd(vdev, dev_cmd, &a0, &a1, wait);
1076                 *entry = (u16)a0;
1077                 vdev->free_consistent(vdev->priv, tlv_size, tlv_va, tlv_pa);
1078         } else if (cmd == CLSF_DEL) {
1079                 a0 = *entry;
1080                 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);
1081         }
1082
1083         return ret;
1084 }