virtio-net: per cpu 64 bit stats (v2)
[deliverable/linux.git] / drivers / net / enic / vnic_dev.c
CommitLineData
01f2e4ea 1/*
29046f9b 2 * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved.
01f2e4ea
SF
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
70feadf3
VK
32enum vnic_proxy_type {
33 PROXY_NONE,
34 PROXY_BY_BDF,
35};
36
01f2e4ea
SF
37struct vnic_res {
38 void __iomem *vaddr;
27e6c7d3 39 dma_addr_t bus_addr;
01f2e4ea
SF
40 unsigned int count;
41};
42
43struct 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;
27372bf5 52 u32 notify_sz;
01f2e4ea
SF
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;
70feadf3
VK
58 enum vnic_proxy_type proxy;
59 u32 proxy_index;
60 u64 args[VNIC_DEVCMD_NARGS];
01f2e4ea
SF
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
68void *vnic_dev_priv(struct vnic_dev *vdev)
69{
70 return vdev->priv;
71}
72
73static int vnic_dev_discover_res(struct vnic_dev *vdev,
27e6c7d3 74 struct vnic_dev_bar *bar, unsigned int num_bars)
01f2e4ea
SF
75{
76 struct vnic_resource_header __iomem *rh;
90cf0b53 77 struct mgmt_barmap_hdr __iomem *mrh;
01f2e4ea
SF
78 struct vnic_resource __iomem *r;
79 u8 type;
80
27e6c7d3
SF
81 if (num_bars == 0)
82 return -EINVAL;
83
01f2e4ea 84 if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
a7a79deb 85 pr_err("vNIC BAR0 res hdr length error\n");
01f2e4ea
SF
86 return -EINVAL;
87 }
88
90cf0b53
RP
89 rh = bar->vaddr;
90 mrh = bar->vaddr;
01f2e4ea 91 if (!rh) {
a7a79deb 92 pr_err("vNIC BAR0 res hdr not mem-mapped\n");
01f2e4ea
SF
93 return -EINVAL;
94 }
95
90cf0b53
RP
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",
01f2e4ea 103 VNIC_RES_MAGIC, VNIC_RES_VERSION,
90cf0b53 104 MGMTVNIC_MAGIC, MGMTVNIC_VERSION,
01f2e4ea 105 ioread32(&rh->magic), ioread32(&rh->version));
90cf0b53
RP
106 return -EINVAL;
107 }
01f2e4ea
SF
108 }
109
90cf0b53
RP
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
01f2e4ea
SF
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
27e6c7d3
SF
125 if (bar_num >= num_bars)
126 continue;
127
128 if (!bar[bar_num].len || !bar[bar_num].vaddr)
01f2e4ea
SF
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;
27e6c7d3 138 if (len + bar_offset > bar[bar_num].len) {
a7a79deb 139 pr_err("vNIC BAR0 resource %d "
01f2e4ea
SF
140 "out-of-bounds, offset 0x%x + "
141 "size 0x%x > bar len 0x%lx\n",
142 type, bar_offset,
143 len,
27e6c7d3 144 bar[bar_num].len);
01f2e4ea
SF
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;
27e6c7d3
SF
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;
01f2e4ea
SF
160 }
161
162 return 0;
163}
164
165unsigned 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
171void __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
2fdba388 189static unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
01f2e4ea
SF
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
216void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
217{
218 memset(ring->descs, 0, ring->size);
219}
220
221int 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) {
a7a79deb 231 pr_err("Failed to allocate ring (size=%d), aborting\n",
01f2e4ea
SF
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
248void 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
70feadf3
VK
259static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
260 int wait)
01f2e4ea
SF
261{
262 struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
70feadf3 263 unsigned int i;
01f2e4ea
SF
264 int delay;
265 u32 status;
01f2e4ea
SF
266 int err;
267
268 status = ioread32(&devcmd->status);
506e1198
VK
269 if (status == 0xFFFFFFFF) {
270 /* PCI-e target device is gone */
271 return -ENODEV;
272 }
01f2e4ea 273 if (status & STAT_BUSY) {
a7a79deb 274 pr_err("Busy devcmd %d\n", _CMD_N(cmd));
01f2e4ea
SF
275 return -EBUSY;
276 }
277
278 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
70feadf3
VK
279 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
280 writeq(vdev->args[i], &devcmd->args[i]);
01f2e4ea
SF
281 wmb();
282 }
283
284 iowrite32(cmd, &devcmd->cmd);
285
286 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
27e6c7d3 287 return 0;
01f2e4ea
SF
288
289 for (delay = 0; delay < wait; delay++) {
290
291 udelay(100);
292
293 status = ioread32(&devcmd->status);
506e1198
VK
294 if (status == 0xFFFFFFFF) {
295 /* PCI-e target device is gone */
296 return -ENODEV;
297 }
70feadf3 298
01f2e4ea
SF
299 if (!(status & STAT_BUSY)) {
300
301 if (status & STAT_ERROR) {
27372bf5
SF
302 err = (int)readq(&devcmd->args[0]);
303 if (err != ERR_ECMDUNKNOWN ||
304 cmd != CMD_CAPABILITY)
a7a79deb 305 pr_err("Error %d devcmd %d\n",
27372bf5
SF
306 err, _CMD_N(cmd));
307 return err;
01f2e4ea
SF
308 }
309
310 if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
311 rmb();
70feadf3
VK
312 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
313 vdev->args[i] = readq(&devcmd->args[i]);
01f2e4ea
SF
314 }
315
316 return 0;
317 }
318 }
319
a7a79deb 320 pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
01f2e4ea
SF
321 return -ETIMEDOUT;
322}
323
70feadf3
VK
324static 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
356static 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
70feadf3
VK
372int 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
5e4232ee 386static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
27372bf5
SF
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
01f2e4ea
SF
397int 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
ea0f0d8b
VK
411 memset(vdev->fw_info, 0, sizeof(struct vnic_devcmd_fw_info));
412
01f2e4ea 413 a0 = vdev->fw_info_pa;
ea0f0d8b 414 a1 = sizeof(struct vnic_devcmd_fw_info);
01f2e4ea
SF
415
416 /* only get fw_info once and cache it */
417 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait);
ea0f0d8b
VK
418 if (err == ERR_ECMDUNKNOWN) {
419 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO_OLD,
420 &a0, &a1, wait);
421 }
01f2e4ea
SF
422 }
423
424 *fw_info = vdev->fw_info;
425
426 return err;
427}
428
429int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size,
430 void *value)
431{
432 u64 a0, a1;
433 int wait = 1000;
434 int err;
435
436 a0 = offset;
437 a1 = size;
438
439 err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
440
441 switch (size) {
442 case 1: *(u8 *)value = (u8)a0; break;
443 case 2: *(u16 *)value = (u16)a0; break;
444 case 4: *(u32 *)value = (u32)a0; break;
445 case 8: *(u64 *)value = a0; break;
446 default: BUG(); break;
447 }
448
449 return err;
450}
451
01f2e4ea
SF
452int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
453{
454 u64 a0, a1;
455 int wait = 1000;
456
457 if (!vdev->stats) {
458 vdev->stats = pci_alloc_consistent(vdev->pdev,
459 sizeof(struct vnic_stats), &vdev->stats_pa);
460 if (!vdev->stats)
461 return -ENOMEM;
462 }
463
464 *stats = vdev->stats;
465 a0 = vdev->stats_pa;
466 a1 = sizeof(struct vnic_stats);
467
468 return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
469}
470
471int vnic_dev_close(struct vnic_dev *vdev)
472{
473 u64 a0 = 0, a1 = 0;
474 int wait = 1000;
475 return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
476}
477
2db77e0f 478int vnic_dev_enable_wait(struct vnic_dev *vdev)
01f2e4ea
SF
479{
480 u64 a0 = 0, a1 = 0;
481 int wait = 1000;
2db77e0f
VK
482 int err;
483
484 err = vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
485 if (err == ERR_ECMDUNKNOWN)
486 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
487
488 return err;
01f2e4ea
SF
489}
490
491int vnic_dev_disable(struct vnic_dev *vdev)
492{
493 u64 a0 = 0, a1 = 0;
494 int wait = 1000;
495 return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
496}
497
498int vnic_dev_open(struct vnic_dev *vdev, int arg)
499{
500 u64 a0 = (u32)arg, a1 = 0;
501 int wait = 1000;
502 return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
503}
504
505int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
506{
507 u64 a0 = 0, a1 = 0;
508 int wait = 1000;
509 int err;
510
511 *done = 0;
512
513 err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
514 if (err)
515 return err;
516
517 *done = (a0 == 0);
518
519 return 0;
520}
521
2fdba388 522static int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
01f2e4ea
SF
523{
524 u64 a0 = (u32)arg, a1 = 0;
525 int wait = 1000;
526 return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
527}
528
2fdba388 529static int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
01f2e4ea
SF
530{
531 u64 a0 = 0, a1 = 0;
532 int wait = 1000;
533 int err;
534
535 *done = 0;
536
537 err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
538 if (err)
539 return err;
540
541 *done = (a0 == 0);
542
543 return 0;
544}
545
99ef5639
VK
546int vnic_dev_hang_reset(struct vnic_dev *vdev, int arg)
547{
548 u64 a0 = (u32)arg, a1 = 0;
549 int wait = 1000;
550 int err;
551
552 err = vnic_dev_cmd(vdev, CMD_HANG_RESET, &a0, &a1, wait);
553 if (err == ERR_ECMDUNKNOWN) {
554 err = vnic_dev_soft_reset(vdev, arg);
555 if (err)
556 return err;
557
558 return vnic_dev_init(vdev, 0);
559 }
560
561 return err;
562}
563
564int vnic_dev_hang_reset_done(struct vnic_dev *vdev, int *done)
565{
566 u64 a0 = 0, a1 = 0;
567 int wait = 1000;
568 int err;
569
570 *done = 0;
571
572 err = vnic_dev_cmd(vdev, CMD_HANG_RESET_STATUS, &a0, &a1, wait);
573 if (err) {
574 if (err == ERR_ECMDUNKNOWN)
575 return vnic_dev_soft_reset_done(vdev, done);
576 return err;
577 }
578
579 *done = (a0 == 0);
580
581 return 0;
582}
583
01f2e4ea
SF
584int vnic_dev_hang_notify(struct vnic_dev *vdev)
585{
586 u64 a0, a1;
587 int wait = 1000;
588 return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait);
589}
590
591int vnic_dev_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
592{
593 u64 a0, a1;
594 int wait = 1000;
595 int err, i;
596
597 for (i = 0; i < ETH_ALEN; i++)
598 mac_addr[i] = 0;
599
600 err = vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
601 if (err)
602 return err;
603
604 for (i = 0; i < ETH_ALEN; i++)
605 mac_addr[i] = ((u8 *)&a0)[i];
606
607 return 0;
608}
609
383ab92f 610int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
01f2e4ea
SF
611 int broadcast, int promisc, int allmulti)
612{
613 u64 a0, a1 = 0;
614 int wait = 1000;
615 int err;
616
617 a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
618 (multicast ? CMD_PFILTER_MULTICAST : 0) |
619 (broadcast ? CMD_PFILTER_BROADCAST : 0) |
620 (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
621 (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
622
623 err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
624 if (err)
a7a79deb 625 pr_err("Can't set packet filter\n");
383ab92f
VK
626
627 return err;
01f2e4ea
SF
628}
629
f8bd9091 630int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
01f2e4ea
SF
631{
632 u64 a0 = 0, a1 = 0;
633 int wait = 1000;
634 int err;
635 int i;
636
637 for (i = 0; i < ETH_ALEN; i++)
638 ((u8 *)&a0)[i] = addr[i];
639
640 err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
641 if (err)
a7a79deb 642 pr_err("Can't add addr [%pM], %d\n", addr, err);
f8bd9091
SF
643
644 return err;
01f2e4ea
SF
645}
646
f8bd9091 647int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
01f2e4ea
SF
648{
649 u64 a0 = 0, a1 = 0;
650 int wait = 1000;
651 int err;
652 int i;
653
654 for (i = 0; i < ETH_ALEN; i++)
655 ((u8 *)&a0)[i] = addr[i];
656
657 err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
658 if (err)
a7a79deb 659 pr_err("Can't del addr [%pM], %d\n", addr, err);
f8bd9091
SF
660
661 return err;
01f2e4ea
SF
662}
663
f8cac14a
VK
664int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
665 u8 ig_vlan_rewrite_mode)
666{
667 u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
668 int wait = 1000;
669 int err;
670
671 err = vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE, &a0, &a1, wait);
672 if (err == ERR_ECMDUNKNOWN)
673 return 0;
674
675 return err;
676}
677
2fdba388 678static int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
d883aa76 679 void *notify_addr, dma_addr_t notify_pa, u16 intr)
01f2e4ea
SF
680{
681 u64 a0, a1;
682 int wait = 1000;
27372bf5 683 int r;
01f2e4ea 684
d883aa76
VK
685 memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
686 vdev->notify = notify_addr;
687 vdev->notify_pa = notify_pa;
01f2e4ea 688
d883aa76 689 a0 = (u64)notify_pa;
01f2e4ea
SF
690 a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
691 a1 += sizeof(struct vnic_devcmd_notify);
692
27372bf5
SF
693 r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
694 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
695 return r;
01f2e4ea
SF
696}
697
d883aa76
VK
698int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
699{
700 void *notify_addr;
701 dma_addr_t notify_pa;
702
703 if (vdev->notify || vdev->notify_pa) {
a7a79deb 704 pr_err("notify block %p still allocated", vdev->notify);
d883aa76
VK
705 return -EINVAL;
706 }
707
708 notify_addr = pci_alloc_consistent(vdev->pdev,
709 sizeof(struct vnic_devcmd_notify),
710 &notify_pa);
711 if (!notify_addr)
712 return -ENOMEM;
713
714 return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
715}
716
2fdba388 717static int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
01f2e4ea
SF
718{
719 u64 a0, a1;
720 int wait = 1000;
383ab92f 721 int err;
01f2e4ea
SF
722
723 a0 = 0; /* paddr = 0 to unset notify buffer */
724 a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
725 a1 += sizeof(struct vnic_devcmd_notify);
726
383ab92f 727 err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
d883aa76
VK
728 vdev->notify = NULL;
729 vdev->notify_pa = 0;
27372bf5 730 vdev->notify_sz = 0;
383ab92f
VK
731
732 return err;
01f2e4ea
SF
733}
734
383ab92f 735int vnic_dev_notify_unset(struct vnic_dev *vdev)
d883aa76
VK
736{
737 if (vdev->notify) {
738 pci_free_consistent(vdev->pdev,
739 sizeof(struct vnic_devcmd_notify),
740 vdev->notify,
741 vdev->notify_pa);
742 }
743
383ab92f 744 return vnic_dev_notify_unsetcmd(vdev);
d883aa76
VK
745}
746
01f2e4ea
SF
747static int vnic_dev_notify_ready(struct vnic_dev *vdev)
748{
749 u32 *words;
27372bf5 750 unsigned int nwords = vdev->notify_sz / 4;
01f2e4ea
SF
751 unsigned int i;
752 u32 csum;
753
27372bf5 754 if (!vdev->notify || !vdev->notify_sz)
01f2e4ea
SF
755 return 0;
756
757 do {
758 csum = 0;
27372bf5 759 memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
01f2e4ea
SF
760 words = (u32 *)&vdev->notify_copy;
761 for (i = 1; i < nwords; i++)
762 csum += words[i];
763 } while (csum != words[0]);
764
765 return 1;
766}
767
768int vnic_dev_init(struct vnic_dev *vdev, int arg)
769{
770 u64 a0 = (u32)arg, a1 = 0;
771 int wait = 1000;
4cdc44a2 772 int r = 0;
27372bf5 773
29046f9b 774 if (vnic_dev_capable(vdev, CMD_INIT))
27372bf5
SF
775 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
776 else {
777 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
778 if (a0 & CMD_INITF_DEFAULT_MAC) {
70feadf3
VK
779 /* Emulate these for old CMD_INIT_v1 which
780 * didn't pass a0 so no CMD_INITF_*.
781 */
27372bf5
SF
782 vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
783 vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
784 }
4cdc44a2
SF
785 }
786 return r;
01f2e4ea
SF
787}
788
f8bd9091
SF
789int vnic_dev_deinit(struct vnic_dev *vdev)
790{
791 u64 a0 = 0, a1 = 0;
792 int wait = 1000;
793
794 return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait);
795}
796
01f2e4ea
SF
797int vnic_dev_link_status(struct vnic_dev *vdev)
798{
01f2e4ea
SF
799 if (!vnic_dev_notify_ready(vdev))
800 return 0;
801
802 return vdev->notify_copy.link_state;
803}
804
805u32 vnic_dev_port_speed(struct vnic_dev *vdev)
806{
807 if (!vnic_dev_notify_ready(vdev))
808 return 0;
809
810 return vdev->notify_copy.port_speed;
811}
812
813u32 vnic_dev_msg_lvl(struct vnic_dev *vdev)
814{
815 if (!vnic_dev_notify_ready(vdev))
816 return 0;
817
818 return vdev->notify_copy.msglvl;
819}
820
821u32 vnic_dev_mtu(struct vnic_dev *vdev)
822{
823 if (!vnic_dev_notify_ready(vdev))
824 return 0;
825
826 return vdev->notify_copy.mtu;
827}
828
829void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
830 enum vnic_dev_intr_mode intr_mode)
831{
832 vdev->intr_mode = intr_mode;
833}
834
835enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
836 struct vnic_dev *vdev)
837{
838 return vdev->intr_mode;
839}
840
841void vnic_dev_unregister(struct vnic_dev *vdev)
842{
843 if (vdev) {
844 if (vdev->notify)
845 pci_free_consistent(vdev->pdev,
846 sizeof(struct vnic_devcmd_notify),
847 vdev->notify,
848 vdev->notify_pa);
01f2e4ea
SF
849 if (vdev->stats)
850 pci_free_consistent(vdev->pdev,
29046f9b 851 sizeof(struct vnic_stats),
01f2e4ea
SF
852 vdev->stats, vdev->stats_pa);
853 if (vdev->fw_info)
854 pci_free_consistent(vdev->pdev,
855 sizeof(struct vnic_devcmd_fw_info),
856 vdev->fw_info, vdev->fw_info_pa);
857 kfree(vdev);
858 }
859}
860
861struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
27e6c7d3
SF
862 void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar,
863 unsigned int num_bars)
01f2e4ea
SF
864{
865 if (!vdev) {
866 vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
867 if (!vdev)
868 return NULL;
869 }
870
871 vdev->priv = priv;
872 vdev->pdev = pdev;
873
27e6c7d3 874 if (vnic_dev_discover_res(vdev, bar, num_bars))
01f2e4ea
SF
875 goto err_out;
876
877 vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
878 if (!vdev->devcmd)
879 goto err_out;
880
881 return vdev;
882
883err_out:
884 vnic_dev_unregister(vdev);
885 return NULL;
886}
887
9085fd09
RP
888int vnic_dev_init_prov2(struct vnic_dev *vdev, u8 *buf, u32 len)
889{
890 u64 a0, a1 = len;
891 int wait = 1000;
892 dma_addr_t prov_pa;
893 void *prov_buf;
894 int ret;
895
896 prov_buf = pci_alloc_consistent(vdev->pdev, len, &prov_pa);
897 if (!prov_buf)
898 return -ENOMEM;
27372bf5 899
9085fd09
RP
900 memcpy(prov_buf, buf, len);
901
902 a0 = prov_pa;
903
904 ret = vnic_dev_cmd(vdev, CMD_INIT_PROV_INFO2, &a0, &a1, wait);
905
906 pci_free_consistent(vdev->pdev, len, prov_buf, prov_pa);
907
908 return ret;
909}
910
911int vnic_dev_enable2(struct vnic_dev *vdev, int active)
912{
913 u64 a0, a1 = 0;
914 int wait = 1000;
915
916 a0 = (active ? CMD_ENABLE2_ACTIVE : 0);
917
918 return vnic_dev_cmd(vdev, CMD_ENABLE2, &a0, &a1, wait);
919}
920
921static int vnic_dev_cmd_status(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
922 int *status)
923{
924 u64 a0 = cmd, a1 = 0;
925 int wait = 1000;
926 int ret;
927
928 ret = vnic_dev_cmd(vdev, CMD_STATUS, &a0, &a1, wait);
929 if (!ret)
930 *status = (int)a0;
931
932 return ret;
933}
934
935int vnic_dev_enable2_done(struct vnic_dev *vdev, int *status)
936{
937 return vnic_dev_cmd_status(vdev, CMD_ENABLE2, status);
938}
939
940int vnic_dev_deinit_done(struct vnic_dev *vdev, int *status)
941{
942 return vnic_dev_cmd_status(vdev, CMD_DEINIT, status);
943}
This page took 0.472938 seconds and 5 git commands to generate.