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