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