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