2 * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
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.
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
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>
27 #include "vnic_resource.h"
28 #include "vnic_devcmd.h"
31 #include "vnic_stats.h"
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
39 void *vnic_dev_priv(struct vnic_dev *vdev)
44 static int vnic_dev_discover_res(struct vnic_dev *vdev,
45 struct vnic_dev_bar *bar, unsigned int num_bars)
47 struct vnic_resource_header __iomem *rh;
48 struct mgmt_barmap_hdr __iomem *mrh;
49 struct vnic_resource __iomem *r;
55 if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
56 vdev_err("vNIC BAR0 res hdr length error\n");
63 vdev_err("vNIC BAR0 res hdr not mem-mapped\n");
67 /* Check for mgmt vnic in addition to normal vnic */
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));
80 if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
81 r = (struct vnic_resource __iomem *)(mrh + 1);
83 r = (struct vnic_resource __iomem *)(rh + 1);
86 while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
88 u8 bar_num = ioread8(&r->bar);
89 u32 bar_offset = ioread32(&r->bar_offset);
90 u32 count = ioread32(&r->count);
95 if (bar_num >= num_bars)
98 if (!bar[bar_num].len || !bar[bar_num].vaddr)
105 case RES_TYPE_INTR_CTRL:
106 /* each count is stride bytes long */
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,
115 case RES_TYPE_INTR_PBA_LEGACY:
116 case RES_TYPE_DEVCMD:
117 case RES_TYPE_DEVCMD2:
124 vdev->res[type].count = count;
125 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
127 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
133 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
134 enum vnic_res_type type)
136 return vdev->res[type].count;
138 EXPORT_SYMBOL(vnic_dev_get_res_count);
140 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
143 if (!vdev->res[type].vaddr)
150 case RES_TYPE_INTR_CTRL:
151 return (char __iomem *)vdev->res[type].vaddr +
152 index * VNIC_RES_STRIDE;
154 return (char __iomem *)vdev->res[type].vaddr;
157 EXPORT_SYMBOL(vnic_dev_get_res);
159 static unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
160 unsigned int desc_count, unsigned int desc_size)
162 /* The base address of the desc rings must be 512 byte aligned.
163 * Descriptor count is aligned to groups of 32 descriptors. A
164 * count of 0 means the maximum 4096 descriptors. Descriptor
165 * size is aligned to 16 bytes.
168 unsigned int count_align = 32;
169 unsigned int desc_align = 16;
171 ring->base_align = 512;
176 ring->desc_count = ALIGN(desc_count, count_align);
178 ring->desc_size = ALIGN(desc_size, desc_align);
180 ring->size = ring->desc_count * ring->desc_size;
181 ring->size_unaligned = ring->size + ring->base_align;
183 return ring->size_unaligned;
186 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
188 memset(ring->descs, 0, ring->size);
191 int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring,
192 unsigned int desc_count, unsigned int desc_size)
194 vnic_dev_desc_ring_size(ring, desc_count, desc_size);
196 ring->descs_unaligned = pci_alloc_consistent(vdev->pdev,
197 ring->size_unaligned,
198 &ring->base_addr_unaligned);
200 if (!ring->descs_unaligned) {
201 vdev_err("Failed to allocate ring (size=%d), aborting\n",
206 ring->base_addr = ALIGN(ring->base_addr_unaligned,
208 ring->descs = (u8 *)ring->descs_unaligned +
209 (ring->base_addr - ring->base_addr_unaligned);
211 vnic_dev_clear_desc_ring(ring);
213 ring->desc_avail = ring->desc_count - 1;
218 void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring)
221 pci_free_consistent(vdev->pdev,
222 ring->size_unaligned,
223 ring->descs_unaligned,
224 ring->base_addr_unaligned);
229 static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
232 struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
238 status = ioread32(&devcmd->status);
239 if (status == 0xFFFFFFFF) {
240 /* PCI-e target device is gone */
243 if (status & STAT_BUSY) {
244 vdev_neterr("Busy devcmd %d\n", _CMD_N(cmd));
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]);
254 iowrite32(cmd, &devcmd->cmd);
256 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
259 for (delay = 0; delay < wait; delay++) {
263 status = ioread32(&devcmd->status);
264 if (status == 0xFFFFFFFF) {
265 /* PCI-e target device is gone */
269 if (!(status & STAT_BUSY)) {
271 if (status & STAT_ERROR) {
272 err = (int)readq(&devcmd->args[0]);
273 if (err == ERR_EINVAL &&
274 cmd == CMD_CAPABILITY)
276 if (err != ERR_ECMDUNKNOWN ||
277 cmd != CMD_CAPABILITY)
278 vdev_neterr("Error %d devcmd %d\n",
283 if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
285 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
286 vdev->args[i] = readq(&devcmd->args[i]);
293 vdev_neterr("Timedout devcmd %d\n", _CMD_N(cmd));
297 static int _vnic_dev_cmd2(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
300 struct devcmd2_controller *dc2c = vdev->devcmd2;
301 struct devcmd2_result *result = dc2c->result + dc2c->next_result;
304 u32 fetch_index, new_posted;
305 u32 posted = dc2c->posted;
307 fetch_index = ioread32(&dc2c->wq_ctrl->fetch_index);
309 if (fetch_index == 0xFFFFFFFF)
312 new_posted = (posted + 1) % DEVCMD2_RING_SIZE;
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);
319 dc2c->cmd_ring[posted].cmd = cmd;
320 dc2c->cmd_ring[posted].flags = 0;
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];
328 /* Adding write memory barrier prevents compiler and/or CPU reordering,
329 * thus avoiding descriptor posting before descriptor is initialized.
330 * Otherwise, hardware can read stale descriptor fields.
333 iowrite32(new_posted, &dc2c->wq_ctrl->posted_index);
334 dc2c->posted = new_posted;
336 if (dc2c->cmd_ring[posted].flags & DEVCMD2_FNORESULT)
339 for (delay = 0; delay < wait; delay++) {
340 if (result->color == dc2c->color) {
342 if (dc2c->next_result == dc2c->result_size) {
343 dc2c->next_result = 0;
344 dc2c->color = dc2c->color ? 0 : 1;
348 if (err != ERR_ECMDUNKNOWN ||
349 cmd != CMD_CAPABILITY)
350 vdev_neterr("Error %d devcmd %d\n",
354 if (_CMD_DIR(cmd) & _CMD_DIR_READ)
355 for (i = 0; i < VNIC_DEVCMD2_NARGS; i++)
356 vdev->args[i] = result->results[i];
363 vdev_neterr("devcmd %d timed out\n", _CMD_N(cmd));
368 static int vnic_dev_init_devcmd1(struct vnic_dev *vdev)
370 vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
373 vdev->devcmd_rtn = _vnic_dev_cmd;
378 static int vnic_dev_init_devcmd2(struct vnic_dev *vdev)
381 unsigned int fetch_index;
386 vdev->devcmd2 = kzalloc(sizeof(*vdev->devcmd2), GFP_KERNEL);
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,
395 goto err_free_devcmd2;
397 fetch_index = ioread32(&vdev->devcmd2->wq.ctrl->fetch_index);
398 if (fetch_index == 0xFFFFFFFF) { /* check for hardware gone */
399 vdev_err("Fatal error in devcmd2 init - hardware surprise removal");
404 enic_wq_init_start(&vdev->devcmd2->wq, 0, fetch_index, fetch_index, 0,
406 vdev->devcmd2->posted = fetch_index;
407 vnic_wq_enable(&vdev->devcmd2->wq);
409 err = vnic_dev_alloc_desc_ring(vdev, &vdev->devcmd2->results_ring,
410 DEVCMD2_RING_SIZE, DEVCMD2_DESC_SIZE);
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 |
419 vdev->args[1] = DEVCMD2_RING_SIZE;
421 err = _vnic_dev_cmd2(vdev, CMD_INITIALIZE_DEVCMD2, 1000);
423 goto err_free_desc_ring;
425 vdev->devcmd_rtn = _vnic_dev_cmd2;
430 vnic_dev_free_desc_ring(vdev, &vdev->devcmd2->results_ring);
432 vnic_wq_disable(&vdev->devcmd2->wq);
433 vnic_wq_free(&vdev->devcmd2->wq);
435 kfree(vdev->devcmd2);
436 vdev->devcmd2 = NULL;
441 static void vnic_dev_deinit_devcmd2(struct vnic_dev *vdev)
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);
449 static 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)
456 memset(vdev->args, 0, sizeof(vdev->args));
458 vdev->args[0] = vdev->proxy_index;
463 err = vdev->devcmd_rtn(vdev, proxy_cmd, wait);
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,
483 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
484 enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait)
491 err = vdev->devcmd_rtn(vdev, cmd, wait);
499 void vnic_dev_cmd_proxy_by_index_start(struct vnic_dev *vdev, u16 index)
501 vdev->proxy = PROXY_BY_INDEX;
502 vdev->proxy_index = index;
505 void vnic_dev_cmd_proxy_end(struct vnic_dev *vdev)
507 vdev->proxy = PROXY_NONE;
508 vdev->proxy_index = 0;
511 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
512 u64 *a0, u64 *a1, int wait)
514 memset(vdev->args, 0, sizeof(vdev->args));
516 switch (vdev->proxy) {
518 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
521 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
525 return vnic_dev_cmd_no_proxy(vdev, cmd, a0, a1, wait);
529 static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
531 u64 a0 = (u32)cmd, a1 = 0;
535 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
540 int vnic_dev_fw_info(struct vnic_dev *vdev,
541 struct vnic_devcmd_fw_info **fw_info)
547 if (!vdev->fw_info) {
548 vdev->fw_info = pci_zalloc_consistent(vdev->pdev,
549 sizeof(struct vnic_devcmd_fw_info),
554 a0 = vdev->fw_info_pa;
555 a1 = sizeof(struct vnic_devcmd_fw_info);
557 /* only get fw_info once and cache it */
558 if (vnic_dev_capable(vdev, CMD_MCPU_FW_INFO))
559 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO,
562 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO_OLD,
566 *fw_info = vdev->fw_info;
571 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size,
581 err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
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;
594 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
600 vdev->stats = pci_alloc_consistent(vdev->pdev,
601 sizeof(struct vnic_stats), &vdev->stats_pa);
606 *stats = vdev->stats;
608 a1 = sizeof(struct vnic_stats);
610 return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
613 int vnic_dev_close(struct vnic_dev *vdev)
617 return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
620 int vnic_dev_enable_wait(struct vnic_dev *vdev)
625 if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
626 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
628 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
631 int vnic_dev_disable(struct vnic_dev *vdev)
635 return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
638 int vnic_dev_open(struct vnic_dev *vdev, int arg)
640 u64 a0 = (u32)arg, a1 = 0;
642 return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
645 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
653 err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
662 int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
664 u64 a0 = (u32)arg, a1 = 0;
666 return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
669 int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
677 err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
686 int vnic_dev_hang_reset(struct vnic_dev *vdev, int arg)
688 u64 a0 = (u32)arg, a1 = 0;
692 if (vnic_dev_capable(vdev, CMD_HANG_RESET)) {
693 return vnic_dev_cmd(vdev, CMD_HANG_RESET,
696 err = vnic_dev_soft_reset(vdev, arg);
699 return vnic_dev_init(vdev, 0);
703 int vnic_dev_hang_reset_done(struct vnic_dev *vdev, int *done)
711 if (vnic_dev_capable(vdev, CMD_HANG_RESET_STATUS)) {
712 err = vnic_dev_cmd(vdev, CMD_HANG_RESET_STATUS,
717 return vnic_dev_soft_reset_done(vdev, done);
725 int vnic_dev_hang_notify(struct vnic_dev *vdev)
729 return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait);
732 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
738 for (i = 0; i < ETH_ALEN; i++)
741 err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
745 for (i = 0; i < ETH_ALEN; i++)
746 mac_addr[i] = ((u8 *)&a0)[i];
751 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
752 int broadcast, int promisc, int allmulti)
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);
764 err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
766 vdev_neterr("Can't set packet filter\n");
771 int vnic_dev_add_addr(struct vnic_dev *vdev, const u8 *addr)
778 for (i = 0; i < ETH_ALEN; i++)
779 ((u8 *)&a0)[i] = addr[i];
781 err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
783 vdev_neterr("Can't add addr [%pM], %d\n", addr, err);
788 int vnic_dev_del_addr(struct vnic_dev *vdev, const u8 *addr)
795 for (i = 0; i < ETH_ALEN; i++)
796 ((u8 *)&a0)[i] = addr[i];
798 err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
800 vdev_neterr("Can't del addr [%pM], %d\n", addr, err);
805 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
806 u8 ig_vlan_rewrite_mode)
808 u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
811 if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
812 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
818 static int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
819 void *notify_addr, dma_addr_t notify_pa, u16 intr)
825 memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
826 vdev->notify = notify_addr;
827 vdev->notify_pa = notify_pa;
830 a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
831 a1 += sizeof(struct vnic_devcmd_notify);
833 r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
834 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
838 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
841 dma_addr_t notify_pa;
843 if (vdev->notify || vdev->notify_pa) {
844 vdev_neterr("notify block %p still allocated", vdev->notify);
848 notify_addr = pci_alloc_consistent(vdev->pdev,
849 sizeof(struct vnic_devcmd_notify),
854 return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
857 static int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
863 a0 = 0; /* paddr = 0 to unset notify buffer */
864 a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
865 a1 += sizeof(struct vnic_devcmd_notify);
867 err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
875 int vnic_dev_notify_unset(struct vnic_dev *vdev)
878 pci_free_consistent(vdev->pdev,
879 sizeof(struct vnic_devcmd_notify),
884 return vnic_dev_notify_unsetcmd(vdev);
887 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
890 unsigned int nwords = vdev->notify_sz / 4;
894 if (!vdev->notify || !vdev->notify_sz)
899 memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
900 words = (u32 *)&vdev->notify_copy;
901 for (i = 1; i < nwords; i++)
903 } while (csum != words[0]);
908 int vnic_dev_init(struct vnic_dev *vdev, int arg)
910 u64 a0 = (u32)arg, a1 = 0;
914 if (vnic_dev_capable(vdev, CMD_INIT))
915 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
917 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
918 if (a0 & CMD_INITF_DEFAULT_MAC) {
919 /* Emulate these for old CMD_INIT_v1 which
920 * didn't pass a0 so no CMD_INITF_*.
922 vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
923 vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
929 int vnic_dev_deinit(struct vnic_dev *vdev)
934 return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait);
937 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
939 /* Default: hardware intr coal timer is in units of 1.5 usecs */
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);
946 int vnic_dev_intr_coal_timer_info(struct vnic_dev *vdev)
951 memset(vdev->args, 0, sizeof(vdev->args));
953 if (vnic_dev_capable(vdev, CMD_INTR_COAL_CONVERT))
954 err = vdev->devcmd_rtn(vdev, CMD_INTR_COAL_CONVERT, wait);
956 err = ERR_ECMDUNKNOWN;
958 /* Use defaults when firmware doesn't support the devcmd at all or
959 * supports it for only specific hardware
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);
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];
977 int vnic_dev_link_status(struct vnic_dev *vdev)
979 if (!vnic_dev_notify_ready(vdev))
982 return vdev->notify_copy.link_state;
985 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
987 if (!vnic_dev_notify_ready(vdev))
990 return vdev->notify_copy.port_speed;
993 u32 vnic_dev_msg_lvl(struct vnic_dev *vdev)
995 if (!vnic_dev_notify_ready(vdev))
998 return vdev->notify_copy.msglvl;
1001 u32 vnic_dev_mtu(struct vnic_dev *vdev)
1003 if (!vnic_dev_notify_ready(vdev))
1006 return vdev->notify_copy.mtu;
1009 void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
1010 enum vnic_dev_intr_mode intr_mode)
1012 vdev->intr_mode = intr_mode;
1015 enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
1016 struct vnic_dev *vdev)
1018 return vdev->intr_mode;
1021 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
1023 return (usec * vdev->intr_coal_timer_info.mul) /
1024 vdev->intr_coal_timer_info.div;
1027 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
1029 return (hw_cycles * vdev->intr_coal_timer_info.div) /
1030 vdev->intr_coal_timer_info.mul;
1033 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
1035 return vdev->intr_coal_timer_info.max_usec;
1038 void vnic_dev_unregister(struct vnic_dev *vdev)
1042 pci_free_consistent(vdev->pdev,
1043 sizeof(struct vnic_devcmd_notify),
1047 pci_free_consistent(vdev->pdev,
1048 sizeof(struct vnic_stats),
1049 vdev->stats, vdev->stats_pa);
1051 pci_free_consistent(vdev->pdev,
1052 sizeof(struct vnic_devcmd_fw_info),
1053 vdev->fw_info, vdev->fw_info_pa);
1055 vnic_dev_deinit_devcmd2(vdev);
1060 EXPORT_SYMBOL(vnic_dev_unregister);
1062 struct 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)
1067 vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
1075 if (vnic_dev_discover_res(vdev, bar, num_bars))
1081 vnic_dev_unregister(vdev);
1084 EXPORT_SYMBOL(vnic_dev_register);
1086 struct pci_dev *vnic_dev_get_pdev(struct vnic_dev *vdev)
1090 EXPORT_SYMBOL(vnic_dev_get_pdev);
1092 int vnic_devcmd_init(struct vnic_dev *vdev)
1097 res = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD2, 0);
1099 err = vnic_dev_init_devcmd2(vdev);
1101 vdev_warn("DEVCMD2 init failed: %d, Using DEVCMD1",
1106 vdev_warn("DEVCMD2 resource not found (old firmware?) Using DEVCMD1\n");
1108 err = vnic_dev_init_devcmd1(vdev);
1110 vdev_err("DEVCMD1 initialization failed: %d", err);
1115 int vnic_dev_init_prov2(struct vnic_dev *vdev, u8 *buf, u32 len)
1123 prov_buf = pci_alloc_consistent(vdev->pdev, len, &prov_pa);
1127 memcpy(prov_buf, buf, len);
1131 ret = vnic_dev_cmd(vdev, CMD_INIT_PROV_INFO2, &a0, &a1, wait);
1133 pci_free_consistent(vdev->pdev, len, prov_buf, prov_pa);
1138 int vnic_dev_enable2(struct vnic_dev *vdev, int active)
1143 a0 = (active ? CMD_ENABLE2_ACTIVE : 0);
1145 return vnic_dev_cmd(vdev, CMD_ENABLE2, &a0, &a1, wait);
1148 static int vnic_dev_cmd_status(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
1151 u64 a0 = cmd, a1 = 0;
1155 ret = vnic_dev_cmd(vdev, CMD_STATUS, &a0, &a1, wait);
1162 int vnic_dev_enable2_done(struct vnic_dev *vdev, int *status)
1164 return vnic_dev_cmd_status(vdev, CMD_ENABLE2, status);
1167 int vnic_dev_deinit_done(struct vnic_dev *vdev, int *status)
1169 return vnic_dev_cmd_status(vdev, CMD_DEINIT, status);
1172 int vnic_dev_set_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
1178 for (i = 0; i < ETH_ALEN; i++)
1179 ((u8 *)&a0)[i] = mac_addr[i];
1181 return vnic_dev_cmd(vdev, CMD_SET_MAC_ADDR, &a0, &a1, wait);
1184 /* vnic_dev_classifier: Add/Delete classifier entries
1185 * @vdev: vdev of the device
1186 * @cmd: CLSF_ADD for Add filter
1187 * CLSF_DEL for Delete filter
1188 * @entry: In case of ADD filter, the caller passes the RQ number in this
1191 * This function stores the filter_id returned by the firmware in the
1192 * same variable before return;
1194 * In case of DEL filter, the caller passes the RQ number. Return
1195 * value is irrelevant.
1196 * @data: filter data
1198 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
1199 struct filter *data)
1205 struct filter_tlv *tlv, *tlv_va;
1206 struct filter_action *action;
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);
1219 memset(tlv, 0, tlv_size);
1220 tlv->type = CLSF_TLV_FILTER;
1221 tlv->length = sizeof(struct filter);
1222 *(struct filter *)&tlv->val = *data;
1224 tlv = (struct filter_tlv *)((char *)tlv +
1225 sizeof(struct filter_tlv) +
1226 sizeof(struct filter));
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;
1234 ret = vnic_dev_cmd(vdev, CMD_ADD_FILTER, &a0, &a1, wait);
1236 pci_free_consistent(vdev->pdev, tlv_size, tlv_va, tlv_pa);
1237 } else if (cmd == CLSF_DEL) {
1239 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);