1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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_wq.h"
31#include "vnic_stats.h"
32#include "enic.h"
33
34#define VNIC_MAX_RES_HDR_SIZE \
35 (sizeof(struct vnic_resource_header) + \
36 sizeof(struct vnic_resource) * RES_TYPE_MAX)
37#define VNIC_RES_STRIDE 128
38
39void *vnic_dev_priv(struct vnic_dev *vdev)
40{
41 return vdev->priv;
42}
43
44static int vnic_dev_discover_res(struct vnic_dev *vdev,
45 struct vnic_dev_bar *bar, unsigned int num_bars)
46{
47 struct vnic_resource_header __iomem *rh;
48 struct mgmt_barmap_hdr __iomem *mrh;
49 struct vnic_resource __iomem *r;
50 u8 type;
51
52 if (num_bars == 0)
53 return -EINVAL;
54
55 if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
56 vdev_err("vNIC BAR0 res hdr length error\n");
57 return -EINVAL;
58 }
59
60 rh = bar->vaddr;
61 mrh = bar->vaddr;
62 if (!rh) {
63 vdev_err("vNIC BAR0 res hdr not mem-mapped\n");
64 return -EINVAL;
65 }
66
67
68 if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) ||
69 (ioread32(&rh->version) != VNIC_RES_VERSION)) {
70 if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) ||
71 (ioread32(&mrh->version) != MGMTVNIC_VERSION)) {
72 vdev_err("vNIC BAR0 res magic/version error exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n",
73 VNIC_RES_MAGIC, VNIC_RES_VERSION,
74 MGMTVNIC_MAGIC, MGMTVNIC_VERSION,
75 ioread32(&rh->magic), ioread32(&rh->version));
76 return -EINVAL;
77 }
78 }
79
80 if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
81 r = (struct vnic_resource __iomem *)(mrh + 1);
82 else
83 r = (struct vnic_resource __iomem *)(rh + 1);
84
85
86 while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
87
88 u8 bar_num = ioread8(&r->bar);
89 u32 bar_offset = ioread32(&r->bar_offset);
90 u32 count = ioread32(&r->count);
91 u32 len;
92
93 r++;
94
95 if (bar_num >= num_bars)
96 continue;
97
98 if (!bar[bar_num].len || !bar[bar_num].vaddr)
99 continue;
100
101 switch (type) {
102 case RES_TYPE_WQ:
103 case RES_TYPE_RQ:
104 case RES_TYPE_CQ:
105 case RES_TYPE_INTR_CTRL:
106
107 len = count * VNIC_RES_STRIDE;
108 if (len + bar_offset > bar[bar_num].len) {
109 vdev_err("vNIC BAR0 resource %d out-of-bounds, offset 0x%x + size 0x%x > bar len 0x%lx\n",
110 type, bar_offset, len,
111 bar[bar_num].len);
112 return -EINVAL;
113 }
114 break;
115 case RES_TYPE_INTR_PBA_LEGACY:
116 case RES_TYPE_DEVCMD:
117 case RES_TYPE_DEVCMD2:
118 len = count;
119 break;
120 default:
121 continue;
122 }
123
124 vdev->res[type].count = count;
125 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
126 bar_offset;
127 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
128 }
129
130 return 0;
131}
132
133unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
134 enum vnic_res_type type)
135{
136 return vdev->res[type].count;
137}
138EXPORT_SYMBOL(vnic_dev_get_res_count);
139
140void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
141 unsigned int index)
142{
143 if (!vdev->res[type].vaddr)
144 return NULL;
145
146 switch (type) {
147 case RES_TYPE_WQ:
148 case RES_TYPE_RQ:
149 case RES_TYPE_CQ:
150 case RES_TYPE_INTR_CTRL:
151 return (char __iomem *)vdev->res[type].vaddr +
152 index * VNIC_RES_STRIDE;
153 default:
154 return (char __iomem *)vdev->res[type].vaddr;
155 }
156}
157EXPORT_SYMBOL(vnic_dev_get_res);
158
159static unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
160 unsigned int desc_count, unsigned int desc_size)
161{
162
163
164
165
166
167
168 unsigned int count_align = 32;
169 unsigned int desc_align = 16;
170
171 ring->base_align = 512;
172
173 if (desc_count == 0)
174 desc_count = 4096;
175
176 ring->desc_count = ALIGN(desc_count, count_align);
177
178 ring->desc_size = ALIGN(desc_size, desc_align);
179
180 ring->size = ring->desc_count * ring->desc_size;
181 ring->size_unaligned = ring->size + ring->base_align;
182
183 return ring->size_unaligned;
184}
185
186void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
187{
188 memset(ring->descs, 0, ring->size);
189}
190
191int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring,
192 unsigned int desc_count, unsigned int desc_size)
193{
194 vnic_dev_desc_ring_size(ring, desc_count, desc_size);
195
196 ring->descs_unaligned = pci_alloc_consistent(vdev->pdev,
197 ring->size_unaligned,
198 &ring->base_addr_unaligned);
199
200 if (!ring->descs_unaligned) {
201 vdev_err("Failed to allocate ring (size=%d), aborting\n",
202 (int)ring->size);
203 return -ENOMEM;
204 }
205
206 ring->base_addr = ALIGN(ring->base_addr_unaligned,
207 ring->base_align);
208 ring->descs = (u8 *)ring->descs_unaligned +
209 (ring->base_addr - ring->base_addr_unaligned);
210
211 vnic_dev_clear_desc_ring(ring);
212
213 ring->desc_avail = ring->desc_count - 1;
214
215 return 0;
216}
217
218void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring)
219{
220 if (ring->descs) {
221 pci_free_consistent(vdev->pdev,
222 ring->size_unaligned,
223 ring->descs_unaligned,
224 ring->base_addr_unaligned);
225 ring->descs = NULL;
226 }
227}
228
229static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
230 int wait)
231{
232 struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
233 unsigned int i;
234 int delay;
235 u32 status;
236 int err;
237
238 status = ioread32(&devcmd->status);
239 if (status == 0xFFFFFFFF) {
240
241 return -ENODEV;
242 }
243 if (status & STAT_BUSY) {
244 vdev_neterr("Busy devcmd %d\n", _CMD_N(cmd));
245 return -EBUSY;
246 }
247
248 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
249 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
250 writeq(vdev->args[i], &devcmd->args[i]);
251 wmb();
252 }
253
254 iowrite32(cmd, &devcmd->cmd);
255
256 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
257 return 0;
258
259 for (delay = 0; delay < wait; delay++) {
260
261 udelay(100);
262
263 status = ioread32(&devcmd->status);
264 if (status == 0xFFFFFFFF) {
265
266 return -ENODEV;
267 }
268
269 if (!(status & STAT_BUSY)) {
270
271 if (status & STAT_ERROR) {
272 err = (int)readq(&devcmd->args[0]);
273 if (err == ERR_EINVAL &&
274 cmd == CMD_CAPABILITY)
275 return -err;
276 if (err != ERR_ECMDUNKNOWN ||
277 cmd != CMD_CAPABILITY)
278 vdev_neterr("Error %d devcmd %d\n",
279 err, _CMD_N(cmd));
280 return -err;
281 }
282
283 if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
284 rmb();
285 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
286 vdev->args[i] = readq(&devcmd->args[i]);
287 }
288
289 return 0;
290 }
291 }
292
293 vdev_neterr("Timedout devcmd %d\n", _CMD_N(cmd));
294 return -ETIMEDOUT;
295}
296
297static int _vnic_dev_cmd2(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
298 int wait)
299{
300 struct devcmd2_controller *dc2c = vdev->devcmd2;
301 struct devcmd2_result *result = dc2c->result + dc2c->next_result;
302 unsigned int i;
303 int delay, err;
304 u32 fetch_index, new_posted;
305 u32 posted = dc2c->posted;
306
307 fetch_index = ioread32(&dc2c->wq_ctrl->fetch_index);
308
309 if (fetch_index == 0xFFFFFFFF)
310 return -ENODEV;
311
312 new_posted = (posted + 1) % DEVCMD2_RING_SIZE;
313
314 if (new_posted == fetch_index) {
315 vdev_neterr("devcmd2 %d: wq is full. fetch index: %u, posted index: %u\n",
316 _CMD_N(cmd), fetch_index, posted);
317 return -EBUSY;
318 }
319 dc2c->cmd_ring[posted].cmd = cmd;
320 dc2c->cmd_ring[posted].flags = 0;
321
322 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
323 dc2c->cmd_ring[posted].flags |= DEVCMD2_FNORESULT;
324 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE)
325 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
326 dc2c->cmd_ring[posted].args[i] = vdev->args[i];
327
328
329
330
331
332 wmb();
333 iowrite32(new_posted, &dc2c->wq_ctrl->posted_index);
334 dc2c->posted = new_posted;
335
336 if (dc2c->cmd_ring[posted].flags & DEVCMD2_FNORESULT)
337 return 0;
338
339 for (delay = 0; delay < wait; delay++) {
340 if (result->color == dc2c->color) {
341 dc2c->next_result++;
342 if (dc2c->next_result == dc2c->result_size) {
343 dc2c->next_result = 0;
344 dc2c->color = dc2c->color ? 0 : 1;
345 }
346 if (result->error) {
347 err = result->error;
348 if (err != ERR_ECMDUNKNOWN ||
349 cmd != CMD_CAPABILITY)
350 vdev_neterr("Error %d devcmd %d\n",
351 err, _CMD_N(cmd));
352 return -err;
353 }
354 if (_CMD_DIR(cmd) & _CMD_DIR_READ)
355 for (i = 0; i < VNIC_DEVCMD2_NARGS; i++)
356 vdev->args[i] = result->results[i];
357
358 return 0;
359 }
360 udelay(100);
361 }
362
363 vdev_neterr("devcmd %d timed out\n", _CMD_N(cmd));
364
365 return -ETIMEDOUT;
366}
367
368static int vnic_dev_init_devcmd1(struct vnic_dev *vdev)
369{
370 vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
371 if (!vdev->devcmd)
372 return -ENODEV;
373 vdev->devcmd_rtn = _vnic_dev_cmd;
374
375 return 0;
376}
377
378static int vnic_dev_init_devcmd2(struct vnic_dev *vdev)
379{
380 int err;
381 unsigned int fetch_index;
382
383 if (vdev->devcmd2)
384 return 0;
385
386 vdev->devcmd2 = kzalloc(sizeof(*vdev->devcmd2), GFP_KERNEL);
387 if (!vdev->devcmd2)
388 return -ENOMEM;
389
390 vdev->devcmd2->color = 1;
391 vdev->devcmd2->result_size = DEVCMD2_RING_SIZE;
392 err = enic_wq_devcmd2_alloc(vdev, &vdev->devcmd2->wq, DEVCMD2_RING_SIZE,
393 DEVCMD2_DESC_SIZE);
394 if (err)
395 goto err_free_devcmd2;
396
397 fetch_index = ioread32(&vdev->devcmd2->wq.ctrl->fetch_index);
398 if (fetch_index == 0xFFFFFFFF) {
399 vdev_err("Fatal error in devcmd2 init - hardware surprise removal");
400
401 return -ENODEV;
402 }
403
404 enic_wq_init_start(&vdev->devcmd2->wq, 0, fetch_index, fetch_index, 0,
405 0);
406 vdev->devcmd2->posted = fetch_index;
407 vnic_wq_enable(&vdev->devcmd2->wq);
408
409 err = vnic_dev_alloc_desc_ring(vdev, &vdev->devcmd2->results_ring,
410 DEVCMD2_RING_SIZE, DEVCMD2_DESC_SIZE);
411 if (err)
412 goto err_free_wq;
413
414 vdev->devcmd2->result = vdev->devcmd2->results_ring.descs;
415 vdev->devcmd2->cmd_ring = vdev->devcmd2->wq.ring.descs;
416 vdev->devcmd2->wq_ctrl = vdev->devcmd2->wq.ctrl;
417 vdev->args[0] = (u64)vdev->devcmd2->results_ring.base_addr |
418 VNIC_PADDR_TARGET;
419 vdev->args[1] = DEVCMD2_RING_SIZE;
420
421 err = _vnic_dev_cmd2(vdev, CMD_INITIALIZE_DEVCMD2, 1000);
422 if (err)
423 goto err_free_desc_ring;
424
425 vdev->devcmd_rtn = _vnic_dev_cmd2;
426
427 return 0;
428
429err_free_desc_ring:
430 vnic_dev_free_desc_ring(vdev, &vdev->devcmd2->results_ring);
431err_free_wq:
432 vnic_wq_disable(&vdev->devcmd2->wq);
433 vnic_wq_free(&vdev->devcmd2->wq);
434err_free_devcmd2:
435 kfree(vdev->devcmd2);
436 vdev->devcmd2 = NULL;
437
438 return err;
439}
440
441static void vnic_dev_deinit_devcmd2(struct vnic_dev *vdev)
442{
443 vnic_dev_free_desc_ring(vdev, &vdev->devcmd2->results_ring);
444 vnic_wq_disable(&vdev->devcmd2->wq);
445 vnic_wq_free(&vdev->devcmd2->wq);
446 kfree(vdev->devcmd2);
447}
448
449static int vnic_dev_cmd_proxy(struct vnic_dev *vdev,
450 enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd,
451 u64 *a0, u64 *a1, int wait)
452{
453 u32 status;
454 int err;
455
456 memset(vdev->args, 0, sizeof(vdev->args));
457
458 vdev->args[0] = vdev->proxy_index;
459 vdev->args[1] = cmd;
460 vdev->args[2] = *a0;
461 vdev->args[3] = *a1;
462
463 err = vdev->devcmd_rtn(vdev, proxy_cmd, wait);
464 if (err)
465 return err;
466
467 status = (u32)vdev->args[0];
468 if (status & STAT_ERROR) {
469 err = (int)vdev->args[1];
470 if (err != ERR_ECMDUNKNOWN ||
471 cmd != CMD_CAPABILITY)
472 vdev_neterr("Error %d proxy devcmd %d\n", err,
473 _CMD_N(cmd));
474 return err;
475 }
476
477 *a0 = vdev->args[1];
478 *a1 = vdev->args[2];
479
480 return 0;
481}
482
483static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
484 enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait)
485{
486 int err;
487
488 vdev->args[0] = *a0;
489 vdev->args[1] = *a1;
490
491 err = vdev->devcmd_rtn(vdev, cmd, wait);
492
493 *a0 = vdev->args[0];
494 *a1 = vdev->args[1];
495
496 return err;
497}
498
499void vnic_dev_cmd_proxy_by_index_start(struct vnic_dev *vdev, u16 index)
500{
501 vdev->proxy = PROXY_BY_INDEX;
502 vdev->proxy_index = index;
503}
504
505void vnic_dev_cmd_proxy_end(struct vnic_dev *vdev)
506{
507 vdev->proxy = PROXY_NONE;
508 vdev->proxy_index = 0;
509}
510
511int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
512 u64 *a0, u64 *a1, int wait)
513{
514 memset(vdev->args, 0, sizeof(vdev->args));
515
516 switch (vdev->proxy) {
517 case PROXY_BY_INDEX:
518 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
519 a0, a1, wait);
520 case PROXY_BY_BDF:
521 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
522 a0, a1, wait);
523 case PROXY_NONE:
524 default:
525 return vnic_dev_cmd_no_proxy(vdev, cmd, a0, a1, wait);
526 }
527}
528
529static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
530{
531 u64 a0 = (u32)cmd, a1 = 0;
532 int wait = 1000;
533 int err;
534
535 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
536
537 return !(err || a0);
538}
539
540int vnic_dev_fw_info(struct vnic_dev *vdev,
541 struct vnic_devcmd_fw_info **fw_info)
542{
543 u64 a0, a1 = 0;
544 int wait = 1000;
545 int err = 0;
546
547 if (!vdev->fw_info) {
548 vdev->fw_info = pci_zalloc_consistent(vdev->pdev,
549 sizeof(struct vnic_devcmd_fw_info),
550 &vdev->fw_info_pa);
551 if (!vdev->fw_info)
552 return -ENOMEM;
553
554 a0 = vdev->fw_info_pa;
555 a1 = sizeof(struct vnic_devcmd_fw_info);
556
557
558 if (vnic_dev_capable(vdev, CMD_MCPU_FW_INFO))
559 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO,
560 &a0, &a1, wait);
561 else
562 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO_OLD,
563 &a0, &a1, wait);
564 }
565
566 *fw_info = vdev->fw_info;
567
568 return err;
569}
570
571int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size,
572 void *value)
573{
574 u64 a0, a1;
575 int wait = 1000;
576 int err;
577
578 a0 = offset;
579 a1 = size;
580
581 err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
582
583 switch (size) {
584 case 1: *(u8 *)value = (u8)a0; break;
585 case 2: *(u16 *)value = (u16)a0; break;
586 case 4: *(u32 *)value = (u32)a0; break;
587 case 8: *(u64 *)value = a0; break;
588 default: BUG(); break;
589 }
590
591 return err;
592}
593
594int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
595{
596 u64 a0, a1;
597 int wait = 1000;
598
599 if (!vdev->stats) {
600 vdev->stats = pci_alloc_consistent(vdev->pdev,
601 sizeof(struct vnic_stats), &vdev->stats_pa);
602 if (!vdev->stats)
603 return -ENOMEM;
604 }
605
606 *stats = vdev->stats;
607 a0 = vdev->stats_pa;
608 a1 = sizeof(struct vnic_stats);
609
610 return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
611}
612
613int vnic_dev_close(struct vnic_dev *vdev)
614{
615 u64 a0 = 0, a1 = 0;
616 int wait = 1000;
617 return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
618}
619
620int vnic_dev_enable_wait(struct vnic_dev *vdev)
621{
622 u64 a0 = 0, a1 = 0;
623 int wait = 1000;
624
625 if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
626 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
627 else
628 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
629}
630
631int vnic_dev_disable(struct vnic_dev *vdev)
632{
633 u64 a0 = 0, a1 = 0;
634 int wait = 1000;
635 return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
636}
637
638int vnic_dev_open(struct vnic_dev *vdev, int arg)
639{
640 u64 a0 = (u32)arg, a1 = 0;
641 int wait = 1000;
642 return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
643}
644
645int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
646{
647 u64 a0 = 0, a1 = 0;
648 int wait = 1000;
649 int err;
650
651 *done = 0;
652
653 err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
654 if (err)
655 return err;
656
657 *done = (a0 == 0);
658
659 return 0;
660}
661
662static int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
663{
664 u64 a0 = (u32)arg, a1 = 0;
665 int wait = 1000;
666 return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
667}
668
669static int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
670{
671 u64 a0 = 0, a1 = 0;
672 int wait = 1000;
673 int err;
674
675 *done = 0;
676
677 err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
678 if (err)
679 return err;
680
681 *done = (a0 == 0);
682
683 return 0;
684}
685
686int vnic_dev_hang_reset(struct vnic_dev *vdev, int arg)
687{
688 u64 a0 = (u32)arg, a1 = 0;
689 int wait = 1000;
690 int err;
691
692 if (vnic_dev_capable(vdev, CMD_HANG_RESET)) {
693 return vnic_dev_cmd(vdev, CMD_HANG_RESET,
694 &a0, &a1, wait);
695 } else {
696 err = vnic_dev_soft_reset(vdev, arg);
697 if (err)
698 return err;
699 return vnic_dev_init(vdev, 0);
700 }
701}
702
703int vnic_dev_hang_reset_done(struct vnic_dev *vdev, int *done)
704{
705 u64 a0 = 0, a1 = 0;
706 int wait = 1000;
707 int err;
708
709 *done = 0;
710
711 if (vnic_dev_capable(vdev, CMD_HANG_RESET_STATUS)) {
712 err = vnic_dev_cmd(vdev, CMD_HANG_RESET_STATUS,
713 &a0, &a1, wait);
714 if (err)
715 return err;
716 } else {
717 return vnic_dev_soft_reset_done(vdev, done);
718 }
719
720 *done = (a0 == 0);
721
722 return 0;
723}
724
725int vnic_dev_hang_notify(struct vnic_dev *vdev)
726{
727 u64 a0, a1;
728 int wait = 1000;
729 return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait);
730}
731
732int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
733{
734 u64 a0, a1;
735 int wait = 1000;
736 int err, i;
737
738 for (i = 0; i < ETH_ALEN; i++)
739 mac_addr[i] = 0;
740
741 err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
742 if (err)
743 return err;
744
745 for (i = 0; i < ETH_ALEN; i++)
746 mac_addr[i] = ((u8 *)&a0)[i];
747
748 return 0;
749}
750
751int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
752 int broadcast, int promisc, int allmulti)
753{
754 u64 a0, a1 = 0;
755 int wait = 1000;
756 int err;
757
758 a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
759 (multicast ? CMD_PFILTER_MULTICAST : 0) |
760 (broadcast ? CMD_PFILTER_BROADCAST : 0) |
761 (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
762 (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
763
764 err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
765 if (err)
766 vdev_neterr("Can't set packet filter\n");
767
768 return err;
769}
770
771int vnic_dev_add_addr(struct vnic_dev *vdev, const u8 *addr)
772{
773 u64 a0 = 0, a1 = 0;
774 int wait = 1000;
775 int err;
776 int i;
777
778 for (i = 0; i < ETH_ALEN; i++)
779 ((u8 *)&a0)[i] = addr[i];
780
781 err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
782 if (err)
783 vdev_neterr("Can't add addr [%pM], %d\n", addr, err);
784
785 return err;
786}
787
788int vnic_dev_del_addr(struct vnic_dev *vdev, const u8 *addr)
789{
790 u64 a0 = 0, a1 = 0;
791 int wait = 1000;
792 int err;
793 int i;
794
795 for (i = 0; i < ETH_ALEN; i++)
796 ((u8 *)&a0)[i] = addr[i];
797
798 err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
799 if (err)
800 vdev_neterr("Can't del addr [%pM], %d\n", addr, err);
801
802 return err;
803}
804
805int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
806 u8 ig_vlan_rewrite_mode)
807{
808 u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
809 int wait = 1000;
810
811 if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
812 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
813 &a0, &a1, wait);
814 else
815 return 0;
816}
817
818static int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
819 void *notify_addr, dma_addr_t notify_pa, u16 intr)
820{
821 u64 a0, a1;
822 int wait = 1000;
823 int r;
824
825 memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
826 vdev->notify = notify_addr;
827 vdev->notify_pa = notify_pa;
828
829 a0 = (u64)notify_pa;
830 a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
831 a1 += sizeof(struct vnic_devcmd_notify);
832
833 r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
834 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
835 return r;
836}
837
838int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
839{
840 void *notify_addr;
841 dma_addr_t notify_pa;
842
843 if (vdev->notify || vdev->notify_pa) {
844 vdev_neterr("notify block %p still allocated", vdev->notify);
845 return -EINVAL;
846 }
847
848 notify_addr = pci_alloc_consistent(vdev->pdev,
849 sizeof(struct vnic_devcmd_notify),
850 ¬ify_pa);
851 if (!notify_addr)
852 return -ENOMEM;
853
854 return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
855}
856
857static int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
858{
859 u64 a0, a1;
860 int wait = 1000;
861 int err;
862
863 a0 = 0;
864 a1 = 0x0000ffff00000000ULL;
865 a1 += sizeof(struct vnic_devcmd_notify);
866
867 err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
868 vdev->notify = NULL;
869 vdev->notify_pa = 0;
870 vdev->notify_sz = 0;
871
872 return err;
873}
874
875int vnic_dev_notify_unset(struct vnic_dev *vdev)
876{
877 if (vdev->notify) {
878 pci_free_consistent(vdev->pdev,
879 sizeof(struct vnic_devcmd_notify),
880 vdev->notify,
881 vdev->notify_pa);
882 }
883
884 return vnic_dev_notify_unsetcmd(vdev);
885}
886
887static int vnic_dev_notify_ready(struct vnic_dev *vdev)
888{
889 u32 *words;
890 unsigned int nwords = vdev->notify_sz / 4;
891 unsigned int i;
892 u32 csum;
893
894 if (!vdev->notify || !vdev->notify_sz)
895 return 0;
896
897 do {
898 csum = 0;
899 memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
900 words = (u32 *)&vdev->notify_copy;
901 for (i = 1; i < nwords; i++)
902 csum += words[i];
903 } while (csum != words[0]);
904
905 return 1;
906}
907
908int vnic_dev_init(struct vnic_dev *vdev, int arg)
909{
910 u64 a0 = (u32)arg, a1 = 0;
911 int wait = 1000;
912 int r = 0;
913
914 if (vnic_dev_capable(vdev, CMD_INIT))
915 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
916 else {
917 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
918 if (a0 & CMD_INITF_DEFAULT_MAC) {
919
920
921
922 vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
923 vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
924 }
925 }
926 return r;
927}
928
929int vnic_dev_deinit(struct vnic_dev *vdev)
930{
931 u64 a0 = 0, a1 = 0;
932 int wait = 1000;
933
934 return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait);
935}
936
937void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
938{
939
940 vdev->intr_coal_timer_info.mul = 2;
941 vdev->intr_coal_timer_info.div = 3;
942 vdev->intr_coal_timer_info.max_usec =
943 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
944}
945
946int vnic_dev_intr_coal_timer_info(struct vnic_dev *vdev)
947{
948 int wait = 1000;
949 int err;
950
951 memset(vdev->args, 0, sizeof(vdev->args));
952
953 if (vnic_dev_capable(vdev, CMD_INTR_COAL_CONVERT))
954 err = vdev->devcmd_rtn(vdev, CMD_INTR_COAL_CONVERT, wait);
955 else
956 err = ERR_ECMDUNKNOWN;
957
958
959
960
961 if ((err == ERR_ECMDUNKNOWN) ||
962 (!err && !(vdev->args[0] && vdev->args[1] && vdev->args[2]))) {
963 vdev_netwarn("Using default conversion factor for interrupt coalesce timer\n");
964 vnic_dev_intr_coal_timer_info_default(vdev);
965 return 0;
966 }
967
968 if (!err) {
969 vdev->intr_coal_timer_info.mul = (u32) vdev->args[0];
970 vdev->intr_coal_timer_info.div = (u32) vdev->args[1];
971 vdev->intr_coal_timer_info.max_usec = (u32) vdev->args[2];
972 }
973
974 return err;
975}
976
977int vnic_dev_link_status(struct vnic_dev *vdev)
978{
979 if (!vnic_dev_notify_ready(vdev))
980 return 0;
981
982 return vdev->notify_copy.link_state;
983}
984
985u32 vnic_dev_port_speed(struct vnic_dev *vdev)
986{
987 if (!vnic_dev_notify_ready(vdev))
988 return 0;
989
990 return vdev->notify_copy.port_speed;
991}
992
993u32 vnic_dev_msg_lvl(struct vnic_dev *vdev)
994{
995 if (!vnic_dev_notify_ready(vdev))
996 return 0;
997
998 return vdev->notify_copy.msglvl;
999}
1000
1001u32 vnic_dev_mtu(struct vnic_dev *vdev)
1002{
1003 if (!vnic_dev_notify_ready(vdev))
1004 return 0;
1005
1006 return vdev->notify_copy.mtu;
1007}
1008
1009void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
1010 enum vnic_dev_intr_mode intr_mode)
1011{
1012 vdev->intr_mode = intr_mode;
1013}
1014
1015enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
1016 struct vnic_dev *vdev)
1017{
1018 return vdev->intr_mode;
1019}
1020
1021u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
1022{
1023 return (usec * vdev->intr_coal_timer_info.mul) /
1024 vdev->intr_coal_timer_info.div;
1025}
1026
1027u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
1028{
1029 return (hw_cycles * vdev->intr_coal_timer_info.div) /
1030 vdev->intr_coal_timer_info.mul;
1031}
1032
1033u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
1034{
1035 return vdev->intr_coal_timer_info.max_usec;
1036}
1037
1038void vnic_dev_unregister(struct vnic_dev *vdev)
1039{
1040 if (vdev) {
1041 if (vdev->notify)
1042 pci_free_consistent(vdev->pdev,
1043 sizeof(struct vnic_devcmd_notify),
1044 vdev->notify,
1045 vdev->notify_pa);
1046 if (vdev->stats)
1047 pci_free_consistent(vdev->pdev,
1048 sizeof(struct vnic_stats),
1049 vdev->stats, vdev->stats_pa);
1050 if (vdev->fw_info)
1051 pci_free_consistent(vdev->pdev,
1052 sizeof(struct vnic_devcmd_fw_info),
1053 vdev->fw_info, vdev->fw_info_pa);
1054 if (vdev->devcmd2)
1055 vnic_dev_deinit_devcmd2(vdev);
1056
1057 kfree(vdev);
1058 }
1059}
1060EXPORT_SYMBOL(vnic_dev_unregister);
1061
1062struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
1063 void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar,
1064 unsigned int num_bars)
1065{
1066 if (!vdev) {
1067 vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
1068 if (!vdev)
1069 return NULL;
1070 }
1071
1072 vdev->priv = priv;
1073 vdev->pdev = pdev;
1074
1075 if (vnic_dev_discover_res(vdev, bar, num_bars))
1076 goto err_out;
1077
1078 return vdev;
1079
1080err_out:
1081 vnic_dev_unregister(vdev);
1082 return NULL;
1083}
1084EXPORT_SYMBOL(vnic_dev_register);
1085
1086struct pci_dev *vnic_dev_get_pdev(struct vnic_dev *vdev)
1087{
1088 return vdev->pdev;
1089}
1090EXPORT_SYMBOL(vnic_dev_get_pdev);
1091
1092int vnic_devcmd_init(struct vnic_dev *vdev)
1093{
1094 void __iomem *res;
1095 int err;
1096
1097 res = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD2, 0);
1098 if (res) {
1099 err = vnic_dev_init_devcmd2(vdev);
1100 if (err)
1101 vdev_warn("DEVCMD2 init failed: %d, Using DEVCMD1",
1102 err);
1103 else
1104 return 0;
1105 } else {
1106 vdev_warn("DEVCMD2 resource not found (old firmware?) Using DEVCMD1\n");
1107 }
1108 err = vnic_dev_init_devcmd1(vdev);
1109 if (err)
1110 vdev_err("DEVCMD1 initialization failed: %d", err);
1111
1112 return err;
1113}
1114
1115int vnic_dev_init_prov2(struct vnic_dev *vdev, u8 *buf, u32 len)
1116{
1117 u64 a0, a1 = len;
1118 int wait = 1000;
1119 dma_addr_t prov_pa;
1120 void *prov_buf;
1121 int ret;
1122
1123 prov_buf = pci_alloc_consistent(vdev->pdev, len, &prov_pa);
1124 if (!prov_buf)
1125 return -ENOMEM;
1126
1127 memcpy(prov_buf, buf, len);
1128
1129 a0 = prov_pa;
1130
1131 ret = vnic_dev_cmd(vdev, CMD_INIT_PROV_INFO2, &a0, &a1, wait);
1132
1133 pci_free_consistent(vdev->pdev, len, prov_buf, prov_pa);
1134
1135 return ret;
1136}
1137
1138int vnic_dev_enable2(struct vnic_dev *vdev, int active)
1139{
1140 u64 a0, a1 = 0;
1141 int wait = 1000;
1142
1143 a0 = (active ? CMD_ENABLE2_ACTIVE : 0);
1144
1145 return vnic_dev_cmd(vdev, CMD_ENABLE2, &a0, &a1, wait);
1146}
1147
1148static int vnic_dev_cmd_status(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
1149 int *status)
1150{
1151 u64 a0 = cmd, a1 = 0;
1152 int wait = 1000;
1153 int ret;
1154
1155 ret = vnic_dev_cmd(vdev, CMD_STATUS, &a0, &a1, wait);
1156 if (!ret)
1157 *status = (int)a0;
1158
1159 return ret;
1160}
1161
1162int vnic_dev_enable2_done(struct vnic_dev *vdev, int *status)
1163{
1164 return vnic_dev_cmd_status(vdev, CMD_ENABLE2, status);
1165}
1166
1167int vnic_dev_deinit_done(struct vnic_dev *vdev, int *status)
1168{
1169 return vnic_dev_cmd_status(vdev, CMD_DEINIT, status);
1170}
1171
1172int vnic_dev_set_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
1173{
1174 u64 a0, a1;
1175 int wait = 1000;
1176 int i;
1177
1178 for (i = 0; i < ETH_ALEN; i++)
1179 ((u8 *)&a0)[i] = mac_addr[i];
1180
1181 return vnic_dev_cmd(vdev, CMD_SET_MAC_ADDR, &a0, &a1, wait);
1182}
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
1199 struct filter *data)
1200{
1201 u64 a0, a1;
1202 int wait = 1000;
1203 dma_addr_t tlv_pa;
1204 int ret = -EINVAL;
1205 struct filter_tlv *tlv, *tlv_va;
1206 struct filter_action *action;
1207 u64 tlv_size;
1208
1209 if (cmd == CLSF_ADD) {
1210 tlv_size = sizeof(struct filter) +
1211 sizeof(struct filter_action) +
1212 2 * sizeof(struct filter_tlv);
1213 tlv_va = pci_alloc_consistent(vdev->pdev, tlv_size, &tlv_pa);
1214 if (!tlv_va)
1215 return -ENOMEM;
1216 tlv = tlv_va;
1217 a0 = tlv_pa;
1218 a1 = tlv_size;
1219 memset(tlv, 0, tlv_size);
1220 tlv->type = CLSF_TLV_FILTER;
1221 tlv->length = sizeof(struct filter);
1222 *(struct filter *)&tlv->val = *data;
1223
1224 tlv = (struct filter_tlv *)((char *)tlv +
1225 sizeof(struct filter_tlv) +
1226 sizeof(struct filter));
1227
1228 tlv->type = CLSF_TLV_ACTION;
1229 tlv->length = sizeof(struct filter_action);
1230 action = (struct filter_action *)&tlv->val;
1231 action->type = FILTER_ACTION_RQ_STEERING;
1232 action->u.rq_idx = *entry;
1233
1234 ret = vnic_dev_cmd(vdev, CMD_ADD_FILTER, &a0, &a1, wait);
1235 *entry = (u16)a0;
1236 pci_free_consistent(vdev->pdev, tlv_size, tlv_va, tlv_pa);
1237 } else if (cmd == CLSF_DEL) {
1238 a0 = *entry;
1239 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);
1240 }
1241
1242 return ret;
1243}
1244