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