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