]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/net/enic/vnic_dev.c
Merge tag 'v2.6.38' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[mv-sheeva.git] / drivers / net / 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 enum vnic_proxy_type {
33         PROXY_NONE,
34         PROXY_BY_BDF,
35 };
36
37 struct vnic_res {
38         void __iomem *vaddr;
39         dma_addr_t bus_addr;
40         unsigned int count;
41 };
42
43 struct vnic_dev {
44         void *priv;
45         struct pci_dev *pdev;
46         struct vnic_res res[RES_TYPE_MAX];
47         enum vnic_dev_intr_mode intr_mode;
48         struct vnic_devcmd __iomem *devcmd;
49         struct vnic_devcmd_notify *notify;
50         struct vnic_devcmd_notify notify_copy;
51         dma_addr_t notify_pa;
52         u32 notify_sz;
53         dma_addr_t linkstatus_pa;
54         struct vnic_stats *stats;
55         dma_addr_t stats_pa;
56         struct vnic_devcmd_fw_info *fw_info;
57         dma_addr_t fw_info_pa;
58         enum vnic_proxy_type proxy;
59         u32 proxy_index;
60         u64 args[VNIC_DEVCMD_NARGS];
61 };
62
63 #define VNIC_MAX_RES_HDR_SIZE \
64         (sizeof(struct vnic_resource_header) + \
65         sizeof(struct vnic_resource) * RES_TYPE_MAX)
66 #define VNIC_RES_STRIDE 128
67
68 void *vnic_dev_priv(struct vnic_dev *vdev)
69 {
70         return vdev->priv;
71 }
72
73 static int vnic_dev_discover_res(struct vnic_dev *vdev,
74         struct vnic_dev_bar *bar, unsigned int num_bars)
75 {
76         struct vnic_resource_header __iomem *rh;
77         struct mgmt_barmap_hdr __iomem *mrh;
78         struct vnic_resource __iomem *r;
79         u8 type;
80
81         if (num_bars == 0)
82                 return -EINVAL;
83
84         if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
85                 pr_err("vNIC BAR0 res hdr length error\n");
86                 return -EINVAL;
87         }
88
89         rh  = bar->vaddr;
90         mrh = bar->vaddr;
91         if (!rh) {
92                 pr_err("vNIC BAR0 res hdr not mem-mapped\n");
93                 return -EINVAL;
94         }
95
96         /* Check for mgmt vnic in addition to normal vnic */
97         if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) ||
98                 (ioread32(&rh->version) != VNIC_RES_VERSION)) {
99                 if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) ||
100                         (ioread32(&mrh->version) != MGMTVNIC_VERSION)) {
101                         pr_err("vNIC BAR0 res magic/version error "
102                         "exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n",
103                         VNIC_RES_MAGIC, VNIC_RES_VERSION,
104                         MGMTVNIC_MAGIC, MGMTVNIC_VERSION,
105                         ioread32(&rh->magic), ioread32(&rh->version));
106                         return -EINVAL;
107                 }
108         }
109
110         if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
111                 r = (struct vnic_resource __iomem *)(mrh + 1);
112         else
113                 r = (struct vnic_resource __iomem *)(rh + 1);
114
115
116         while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
117
118                 u8 bar_num = ioread8(&r->bar);
119                 u32 bar_offset = ioread32(&r->bar_offset);
120                 u32 count = ioread32(&r->count);
121                 u32 len;
122
123                 r++;
124
125                 if (bar_num >= num_bars)
126                         continue;
127
128                 if (!bar[bar_num].len || !bar[bar_num].vaddr)
129                         continue;
130
131                 switch (type) {
132                 case RES_TYPE_WQ:
133                 case RES_TYPE_RQ:
134                 case RES_TYPE_CQ:
135                 case RES_TYPE_INTR_CTRL:
136                         /* each count is stride bytes long */
137                         len = count * VNIC_RES_STRIDE;
138                         if (len + bar_offset > bar[bar_num].len) {
139                                 pr_err("vNIC BAR0 resource %d "
140                                         "out-of-bounds, offset 0x%x + "
141                                         "size 0x%x > bar len 0x%lx\n",
142                                         type, bar_offset,
143                                         len,
144                                         bar[bar_num].len);
145                                 return -EINVAL;
146                         }
147                         break;
148                 case RES_TYPE_INTR_PBA_LEGACY:
149                 case RES_TYPE_DEVCMD:
150                         len = count;
151                         break;
152                 default:
153                         continue;
154                 }
155
156                 vdev->res[type].count = count;
157                 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
158                         bar_offset;
159                 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
160         }
161
162         return 0;
163 }
164
165 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
166         enum vnic_res_type type)
167 {
168         return vdev->res[type].count;
169 }
170
171 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
172         unsigned int index)
173 {
174         if (!vdev->res[type].vaddr)
175                 return NULL;
176
177         switch (type) {
178         case RES_TYPE_WQ:
179         case RES_TYPE_RQ:
180         case RES_TYPE_CQ:
181         case RES_TYPE_INTR_CTRL:
182                 return (char __iomem *)vdev->res[type].vaddr +
183                         index * VNIC_RES_STRIDE;
184         default:
185                 return (char __iomem *)vdev->res[type].vaddr;
186         }
187 }
188
189 static unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
190         unsigned int desc_count, unsigned int desc_size)
191 {
192         /* The base address of the desc rings must be 512 byte aligned.
193          * Descriptor count is aligned to groups of 32 descriptors.  A
194          * count of 0 means the maximum 4096 descriptors.  Descriptor
195          * size is aligned to 16 bytes.
196          */
197
198         unsigned int count_align = 32;
199         unsigned int desc_align = 16;
200
201         ring->base_align = 512;
202
203         if (desc_count == 0)
204                 desc_count = 4096;
205
206         ring->desc_count = ALIGN(desc_count, count_align);
207
208         ring->desc_size = ALIGN(desc_size, desc_align);
209
210         ring->size = ring->desc_count * ring->desc_size;
211         ring->size_unaligned = ring->size + ring->base_align;
212
213         return ring->size_unaligned;
214 }
215
216 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
217 {
218         memset(ring->descs, 0, ring->size);
219 }
220
221 int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring,
222         unsigned int desc_count, unsigned int desc_size)
223 {
224         vnic_dev_desc_ring_size(ring, desc_count, desc_size);
225
226         ring->descs_unaligned = pci_alloc_consistent(vdev->pdev,
227                 ring->size_unaligned,
228                 &ring->base_addr_unaligned);
229
230         if (!ring->descs_unaligned) {
231                 pr_err("Failed to allocate ring (size=%d), aborting\n",
232                         (int)ring->size);
233                 return -ENOMEM;
234         }
235
236         ring->base_addr = ALIGN(ring->base_addr_unaligned,
237                 ring->base_align);
238         ring->descs = (u8 *)ring->descs_unaligned +
239                 (ring->base_addr - ring->base_addr_unaligned);
240
241         vnic_dev_clear_desc_ring(ring);
242
243         ring->desc_avail = ring->desc_count - 1;
244
245         return 0;
246 }
247
248 void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring)
249 {
250         if (ring->descs) {
251                 pci_free_consistent(vdev->pdev,
252                         ring->size_unaligned,
253                         ring->descs_unaligned,
254                         ring->base_addr_unaligned);
255                 ring->descs = NULL;
256         }
257 }
258
259 static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
260         int wait)
261 {
262         struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
263         unsigned int i;
264         int delay;
265         u32 status;
266         int err;
267
268         status = ioread32(&devcmd->status);
269         if (status == 0xFFFFFFFF) {
270                 /* PCI-e target device is gone */
271                 return -ENODEV;
272         }
273         if (status & STAT_BUSY) {
274                 pr_err("Busy devcmd %d\n", _CMD_N(cmd));
275                 return -EBUSY;
276         }
277
278         if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
279                 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
280                         writeq(vdev->args[i], &devcmd->args[i]);
281                 wmb();
282         }
283
284         iowrite32(cmd, &devcmd->cmd);
285
286         if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
287                 return 0;
288
289         for (delay = 0; delay < wait; delay++) {
290
291                 udelay(100);
292
293                 status = ioread32(&devcmd->status);
294                 if (status == 0xFFFFFFFF) {
295                         /* PCI-e target device is gone */
296                         return -ENODEV;
297                 }
298
299                 if (!(status & STAT_BUSY)) {
300
301                         if (status & STAT_ERROR) {
302                                 err = (int)readq(&devcmd->args[0]);
303                                 if (err != ERR_ECMDUNKNOWN ||
304                                     cmd != CMD_CAPABILITY)
305                                         pr_err("Error %d devcmd %d\n",
306                                                 err, _CMD_N(cmd));
307                                 return err;
308                         }
309
310                         if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
311                                 rmb();
312                                 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
313                                         vdev->args[i] = readq(&devcmd->args[i]);
314                         }
315
316                         return 0;
317                 }
318         }
319
320         pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
321         return -ETIMEDOUT;
322 }
323
324 static int vnic_dev_cmd_proxy_by_bdf(struct vnic_dev *vdev,
325         enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait)
326 {
327         u32 status;
328         int err;
329
330         memset(vdev->args, 0, sizeof(vdev->args));
331
332         vdev->args[0] = vdev->proxy_index; /* bdf */
333         vdev->args[1] = cmd;
334         vdev->args[2] = *a0;
335         vdev->args[3] = *a1;
336
337         err = _vnic_dev_cmd(vdev, CMD_PROXY_BY_BDF, wait);
338         if (err)
339                 return err;
340
341         status = (u32)vdev->args[0];
342         if (status & STAT_ERROR) {
343                 err = (int)vdev->args[1];
344                 if (err != ERR_ECMDUNKNOWN ||
345                     cmd != CMD_CAPABILITY)
346                         pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd));
347                 return err;
348         }
349
350         *a0 = vdev->args[1];
351         *a1 = vdev->args[2];
352
353         return 0;
354 }
355
356 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
357         enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait)
358 {
359         int err;
360
361         vdev->args[0] = *a0;
362         vdev->args[1] = *a1;
363
364         err = _vnic_dev_cmd(vdev, cmd, wait);
365
366         *a0 = vdev->args[0];
367         *a1 = vdev->args[1];
368
369         return err;
370 }
371
372 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
373         u64 *a0, u64 *a1, int wait)
374 {
375         memset(vdev->args, 0, sizeof(vdev->args));
376
377         switch (vdev->proxy) {
378         case PROXY_BY_BDF:
379                 return vnic_dev_cmd_proxy_by_bdf(vdev, cmd, a0, a1, wait);
380         case PROXY_NONE:
381         default:
382                 return vnic_dev_cmd_no_proxy(vdev, cmd, a0, a1, wait);
383         }
384 }
385
386 static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
387 {
388         u64 a0 = (u32)cmd, a1 = 0;
389         int wait = 1000;
390         int err;
391
392         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
393
394         return !(err || a0);
395 }
396
397 int vnic_dev_fw_info(struct vnic_dev *vdev,
398         struct vnic_devcmd_fw_info **fw_info)
399 {
400         u64 a0, a1 = 0;
401         int wait = 1000;
402         int err = 0;
403
404         if (!vdev->fw_info) {
405                 vdev->fw_info = pci_alloc_consistent(vdev->pdev,
406                         sizeof(struct vnic_devcmd_fw_info),
407                         &vdev->fw_info_pa);
408                 if (!vdev->fw_info)
409                         return -ENOMEM;
410
411                 a0 = vdev->fw_info_pa;
412
413                 /* only get fw_info once and cache it */
414                 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait);
415         }
416
417         *fw_info = vdev->fw_info;
418
419         return err;
420 }
421
422 int vnic_dev_hw_version(struct vnic_dev *vdev, enum vnic_dev_hw_version *hw_ver)
423 {
424         struct vnic_devcmd_fw_info *fw_info;
425         int err;
426
427         err = vnic_dev_fw_info(vdev, &fw_info);
428         if (err)
429                 return err;
430
431         if (strncmp(fw_info->hw_version, "A1", sizeof("A1")) == 0)
432                 *hw_ver = VNIC_DEV_HW_VER_A1;
433         else if (strncmp(fw_info->hw_version, "A2", sizeof("A2")) == 0)
434                 *hw_ver = VNIC_DEV_HW_VER_A2;
435         else
436                 *hw_ver = VNIC_DEV_HW_VER_UNKNOWN;
437
438         return 0;
439 }
440
441 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size,
442         void *value)
443 {
444         u64 a0, a1;
445         int wait = 1000;
446         int err;
447
448         a0 = offset;
449         a1 = size;
450
451         err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
452
453         switch (size) {
454         case 1: *(u8 *)value = (u8)a0; break;
455         case 2: *(u16 *)value = (u16)a0; break;
456         case 4: *(u32 *)value = (u32)a0; break;
457         case 8: *(u64 *)value = a0; break;
458         default: BUG(); break;
459         }
460
461         return err;
462 }
463
464 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
465 {
466         u64 a0, a1;
467         int wait = 1000;
468
469         if (!vdev->stats) {
470                 vdev->stats = pci_alloc_consistent(vdev->pdev,
471                         sizeof(struct vnic_stats), &vdev->stats_pa);
472                 if (!vdev->stats)
473                         return -ENOMEM;
474         }
475
476         *stats = vdev->stats;
477         a0 = vdev->stats_pa;
478         a1 = sizeof(struct vnic_stats);
479
480         return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
481 }
482
483 int vnic_dev_close(struct vnic_dev *vdev)
484 {
485         u64 a0 = 0, a1 = 0;
486         int wait = 1000;
487         return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
488 }
489
490 int vnic_dev_enable_wait(struct vnic_dev *vdev)
491 {
492         u64 a0 = 0, a1 = 0;
493         int wait = 1000;
494         int err;
495
496         err = vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
497         if (err == ERR_ECMDUNKNOWN)
498                 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
499
500         return err;
501 }
502
503 int vnic_dev_disable(struct vnic_dev *vdev)
504 {
505         u64 a0 = 0, a1 = 0;
506         int wait = 1000;
507         return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
508 }
509
510 int vnic_dev_open(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_OPEN, &a0, &a1, wait);
515 }
516
517 int vnic_dev_open_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_OPEN_STATUS, &a0, &a1, wait);
526         if (err)
527                 return err;
528
529         *done = (a0 == 0);
530
531         return 0;
532 }
533
534 static int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
535 {
536         u64 a0 = (u32)arg, a1 = 0;
537         int wait = 1000;
538         return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
539 }
540
541 static int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
542 {
543         u64 a0 = 0, a1 = 0;
544         int wait = 1000;
545         int err;
546
547         *done = 0;
548
549         err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
550         if (err)
551                 return err;
552
553         *done = (a0 == 0);
554
555         return 0;
556 }
557
558 int vnic_dev_hang_reset(struct vnic_dev *vdev, int arg)
559 {
560         u64 a0 = (u32)arg, a1 = 0;
561         int wait = 1000;
562         int err;
563
564         err = vnic_dev_cmd(vdev, CMD_HANG_RESET, &a0, &a1, wait);
565         if (err == ERR_ECMDUNKNOWN) {
566                 err = vnic_dev_soft_reset(vdev, arg);
567                 if (err)
568                         return err;
569
570                 return vnic_dev_init(vdev, 0);
571         }
572
573         return err;
574 }
575
576 int vnic_dev_hang_reset_done(struct vnic_dev *vdev, int *done)
577 {
578         u64 a0 = 0, a1 = 0;
579         int wait = 1000;
580         int err;
581
582         *done = 0;
583
584         err = vnic_dev_cmd(vdev, CMD_HANG_RESET_STATUS, &a0, &a1, wait);
585         if (err) {
586                 if (err == ERR_ECMDUNKNOWN)
587                         return vnic_dev_soft_reset_done(vdev, done);
588                 return err;
589         }
590
591         *done = (a0 == 0);
592
593         return 0;
594 }
595
596 int vnic_dev_hang_notify(struct vnic_dev *vdev)
597 {
598         u64 a0, a1;
599         int wait = 1000;
600         return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait);
601 }
602
603 int vnic_dev_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
604 {
605         u64 a0, a1;
606         int wait = 1000;
607         int err, i;
608
609         for (i = 0; i < ETH_ALEN; i++)
610                 mac_addr[i] = 0;
611
612         err = vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
613         if (err)
614                 return err;
615
616         for (i = 0; i < ETH_ALEN; i++)
617                 mac_addr[i] = ((u8 *)&a0)[i];
618
619         return 0;
620 }
621
622 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
623         int broadcast, int promisc, int allmulti)
624 {
625         u64 a0, a1 = 0;
626         int wait = 1000;
627         int err;
628
629         a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
630              (multicast ? CMD_PFILTER_MULTICAST : 0) |
631              (broadcast ? CMD_PFILTER_BROADCAST : 0) |
632              (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
633              (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
634
635         err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
636         if (err)
637                 pr_err("Can't set packet filter\n");
638
639         return err;
640 }
641
642 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
643 {
644         u64 a0 = 0, a1 = 0;
645         int wait = 1000;
646         int err;
647         int i;
648
649         for (i = 0; i < ETH_ALEN; i++)
650                 ((u8 *)&a0)[i] = addr[i];
651
652         err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
653         if (err)
654                 pr_err("Can't add addr [%pM], %d\n", addr, err);
655
656         return err;
657 }
658
659 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
660 {
661         u64 a0 = 0, a1 = 0;
662         int wait = 1000;
663         int err;
664         int i;
665
666         for (i = 0; i < ETH_ALEN; i++)
667                 ((u8 *)&a0)[i] = addr[i];
668
669         err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
670         if (err)
671                 pr_err("Can't del addr [%pM], %d\n", addr, err);
672
673         return err;
674 }
675
676 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
677         u8 ig_vlan_rewrite_mode)
678 {
679         u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
680         int wait = 1000;
681         int err;
682
683         err = vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE, &a0, &a1, wait);
684         if (err == ERR_ECMDUNKNOWN)
685                 return 0;
686
687         return err;
688 }
689
690 static int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
691         void *notify_addr, dma_addr_t notify_pa, u16 intr)
692 {
693         u64 a0, a1;
694         int wait = 1000;
695         int r;
696
697         memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
698         vdev->notify = notify_addr;
699         vdev->notify_pa = notify_pa;
700
701         a0 = (u64)notify_pa;
702         a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
703         a1 += sizeof(struct vnic_devcmd_notify);
704
705         r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
706         vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
707         return r;
708 }
709
710 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
711 {
712         void *notify_addr;
713         dma_addr_t notify_pa;
714
715         if (vdev->notify || vdev->notify_pa) {
716                 pr_err("notify block %p still allocated", vdev->notify);
717                 return -EINVAL;
718         }
719
720         notify_addr = pci_alloc_consistent(vdev->pdev,
721                         sizeof(struct vnic_devcmd_notify),
722                         &notify_pa);
723         if (!notify_addr)
724                 return -ENOMEM;
725
726         return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
727 }
728
729 static int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
730 {
731         u64 a0, a1;
732         int wait = 1000;
733         int err;
734
735         a0 = 0;  /* paddr = 0 to unset notify buffer */
736         a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
737         a1 += sizeof(struct vnic_devcmd_notify);
738
739         err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
740         vdev->notify = NULL;
741         vdev->notify_pa = 0;
742         vdev->notify_sz = 0;
743
744         return err;
745 }
746
747 int vnic_dev_notify_unset(struct vnic_dev *vdev)
748 {
749         if (vdev->notify) {
750                 pci_free_consistent(vdev->pdev,
751                         sizeof(struct vnic_devcmd_notify),
752                         vdev->notify,
753                         vdev->notify_pa);
754         }
755
756         return vnic_dev_notify_unsetcmd(vdev);
757 }
758
759 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
760 {
761         u32 *words;
762         unsigned int nwords = vdev->notify_sz / 4;
763         unsigned int i;
764         u32 csum;
765
766         if (!vdev->notify || !vdev->notify_sz)
767                 return 0;
768
769         do {
770                 csum = 0;
771                 memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
772                 words = (u32 *)&vdev->notify_copy;
773                 for (i = 1; i < nwords; i++)
774                         csum += words[i];
775         } while (csum != words[0]);
776
777         return 1;
778 }
779
780 int vnic_dev_init(struct vnic_dev *vdev, int arg)
781 {
782         u64 a0 = (u32)arg, a1 = 0;
783         int wait = 1000;
784         int r = 0;
785
786         if (vnic_dev_capable(vdev, CMD_INIT))
787                 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
788         else {
789                 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
790                 if (a0 & CMD_INITF_DEFAULT_MAC) {
791                         /* Emulate these for old CMD_INIT_v1 which
792                          * didn't pass a0 so no CMD_INITF_*.
793                          */
794                         vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
795                         vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
796                 }
797         }
798         return r;
799 }
800
801 int vnic_dev_init_done(struct vnic_dev *vdev, int *done, int *err)
802 {
803         u64 a0 = 0, a1 = 0;
804         int wait = 1000;
805         int ret;
806
807         *done = 0;
808
809         ret = vnic_dev_cmd(vdev, CMD_INIT_STATUS, &a0, &a1, wait);
810         if (ret)
811                 return ret;
812
813         *done = (a0 == 0);
814
815         *err = (a0 == 0) ? (int)a1:0;
816
817         return 0;
818 }
819
820 int vnic_dev_init_prov(struct vnic_dev *vdev, u8 *buf, u32 len)
821 {
822         u64 a0, a1 = len;
823         int wait = 1000;
824         dma_addr_t prov_pa;
825         void *prov_buf;
826         int ret;
827
828         prov_buf = pci_alloc_consistent(vdev->pdev, len, &prov_pa);
829         if (!prov_buf)
830                 return -ENOMEM;
831
832         memcpy(prov_buf, buf, len);
833
834         a0 = prov_pa;
835
836         ret = vnic_dev_cmd(vdev, CMD_INIT_PROV_INFO, &a0, &a1, wait);
837
838         pci_free_consistent(vdev->pdev, len, prov_buf, prov_pa);
839
840         return ret;
841 }
842
843 int vnic_dev_deinit(struct vnic_dev *vdev)
844 {
845         u64 a0 = 0, a1 = 0;
846         int wait = 1000;
847
848         return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait);
849 }
850
851 int vnic_dev_link_status(struct vnic_dev *vdev)
852 {
853         if (!vnic_dev_notify_ready(vdev))
854                 return 0;
855
856         return vdev->notify_copy.link_state;
857 }
858
859 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
860 {
861         if (!vnic_dev_notify_ready(vdev))
862                 return 0;
863
864         return vdev->notify_copy.port_speed;
865 }
866
867 u32 vnic_dev_msg_lvl(struct vnic_dev *vdev)
868 {
869         if (!vnic_dev_notify_ready(vdev))
870                 return 0;
871
872         return vdev->notify_copy.msglvl;
873 }
874
875 u32 vnic_dev_mtu(struct vnic_dev *vdev)
876 {
877         if (!vnic_dev_notify_ready(vdev))
878                 return 0;
879
880         return vdev->notify_copy.mtu;
881 }
882
883 void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
884         enum vnic_dev_intr_mode intr_mode)
885 {
886         vdev->intr_mode = intr_mode;
887 }
888
889 enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
890         struct vnic_dev *vdev)
891 {
892         return vdev->intr_mode;
893 }
894
895 void vnic_dev_unregister(struct vnic_dev *vdev)
896 {
897         if (vdev) {
898                 if (vdev->notify)
899                         pci_free_consistent(vdev->pdev,
900                                 sizeof(struct vnic_devcmd_notify),
901                                 vdev->notify,
902                                 vdev->notify_pa);
903                 if (vdev->stats)
904                         pci_free_consistent(vdev->pdev,
905                                 sizeof(struct vnic_stats),
906                                 vdev->stats, vdev->stats_pa);
907                 if (vdev->fw_info)
908                         pci_free_consistent(vdev->pdev,
909                                 sizeof(struct vnic_devcmd_fw_info),
910                                 vdev->fw_info, vdev->fw_info_pa);
911                 kfree(vdev);
912         }
913 }
914
915 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
916         void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar,
917         unsigned int num_bars)
918 {
919         if (!vdev) {
920                 vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
921                 if (!vdev)
922                         return NULL;
923         }
924
925         vdev->priv = priv;
926         vdev->pdev = pdev;
927
928         if (vnic_dev_discover_res(vdev, bar, num_bars))
929                 goto err_out;
930
931         vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
932         if (!vdev->devcmd)
933                 goto err_out;
934
935         return vdev;
936
937 err_out:
938         vnic_dev_unregister(vdev);
939         return NULL;
940 }
941
942