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