1*dfb99b05SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2*dfb99b05SThomas Gleixner // Copyright 2014 Cisco Systems, Inc. All rights reserved.
3c8806b6cSNarsimhulu Musini
4c8806b6cSNarsimhulu Musini #include <linux/kernel.h>
5c8806b6cSNarsimhulu Musini #include <linux/errno.h>
6c8806b6cSNarsimhulu Musini #include <linux/types.h>
7c8806b6cSNarsimhulu Musini #include <linux/pci.h>
8c8806b6cSNarsimhulu Musini #include <linux/delay.h>
9c8806b6cSNarsimhulu Musini #include <linux/if_ether.h>
10c8806b6cSNarsimhulu Musini #include <linux/slab.h>
11c8806b6cSNarsimhulu Musini #include "vnic_resource.h"
12c8806b6cSNarsimhulu Musini #include "vnic_devcmd.h"
13c8806b6cSNarsimhulu Musini #include "vnic_dev.h"
14c8806b6cSNarsimhulu Musini #include "vnic_stats.h"
15c8806b6cSNarsimhulu Musini #include "vnic_wq.h"
16c8806b6cSNarsimhulu Musini
17c8806b6cSNarsimhulu Musini #define VNIC_DVCMD_TMO 10000 /* Devcmd Timeout value */
18c8806b6cSNarsimhulu Musini #define VNIC_NOTIFY_INTR_MASK 0x0000ffff00000000ULL
19c8806b6cSNarsimhulu Musini
20c8806b6cSNarsimhulu Musini struct devcmd2_controller {
21c8806b6cSNarsimhulu Musini struct vnic_wq_ctrl __iomem *wq_ctrl;
22c8806b6cSNarsimhulu Musini struct vnic_dev_ring results_ring;
23c8806b6cSNarsimhulu Musini struct vnic_wq wq;
24c8806b6cSNarsimhulu Musini struct vnic_devcmd2 *cmd_ring;
25c8806b6cSNarsimhulu Musini struct devcmd2_result *result;
26c8806b6cSNarsimhulu Musini u16 next_result;
27c8806b6cSNarsimhulu Musini u16 result_size;
28c8806b6cSNarsimhulu Musini int color;
29c8806b6cSNarsimhulu Musini };
30c8806b6cSNarsimhulu Musini
31c8806b6cSNarsimhulu Musini struct vnic_res {
32c8806b6cSNarsimhulu Musini void __iomem *vaddr;
33c8806b6cSNarsimhulu Musini unsigned int count;
34c8806b6cSNarsimhulu Musini };
35c8806b6cSNarsimhulu Musini
36c8806b6cSNarsimhulu Musini struct vnic_dev {
37c8806b6cSNarsimhulu Musini void *priv;
38c8806b6cSNarsimhulu Musini struct pci_dev *pdev;
39c8806b6cSNarsimhulu Musini struct vnic_res res[RES_TYPE_MAX];
40c8806b6cSNarsimhulu Musini enum vnic_dev_intr_mode intr_mode;
41c8806b6cSNarsimhulu Musini struct vnic_devcmd __iomem *devcmd;
42c8806b6cSNarsimhulu Musini struct vnic_devcmd_notify *notify;
43c8806b6cSNarsimhulu Musini struct vnic_devcmd_notify notify_copy;
44c8806b6cSNarsimhulu Musini dma_addr_t notify_pa;
45c8806b6cSNarsimhulu Musini u32 *linkstatus;
46c8806b6cSNarsimhulu Musini dma_addr_t linkstatus_pa;
47c8806b6cSNarsimhulu Musini struct vnic_stats *stats;
48c8806b6cSNarsimhulu Musini dma_addr_t stats_pa;
49c8806b6cSNarsimhulu Musini struct vnic_devcmd_fw_info *fw_info;
50c8806b6cSNarsimhulu Musini dma_addr_t fw_info_pa;
51c8806b6cSNarsimhulu Musini u64 args[VNIC_DEVCMD_NARGS];
52c8806b6cSNarsimhulu Musini struct devcmd2_controller *devcmd2;
53c8806b6cSNarsimhulu Musini
54c8806b6cSNarsimhulu Musini int (*devcmd_rtn)(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
55c8806b6cSNarsimhulu Musini int wait);
56c8806b6cSNarsimhulu Musini };
57c8806b6cSNarsimhulu Musini
58c8806b6cSNarsimhulu Musini #define VNIC_MAX_RES_HDR_SIZE \
59c8806b6cSNarsimhulu Musini (sizeof(struct vnic_resource_header) + \
60c8806b6cSNarsimhulu Musini sizeof(struct vnic_resource) * RES_TYPE_MAX)
61c8806b6cSNarsimhulu Musini #define VNIC_RES_STRIDE 128
62c8806b6cSNarsimhulu Musini
svnic_dev_priv(struct vnic_dev * vdev)63c8806b6cSNarsimhulu Musini void *svnic_dev_priv(struct vnic_dev *vdev)
64c8806b6cSNarsimhulu Musini {
65c8806b6cSNarsimhulu Musini return vdev->priv;
66c8806b6cSNarsimhulu Musini }
67c8806b6cSNarsimhulu Musini
vnic_dev_discover_res(struct vnic_dev * vdev,struct vnic_dev_bar * bar,unsigned int num_bars)68c8806b6cSNarsimhulu Musini static int vnic_dev_discover_res(struct vnic_dev *vdev,
69c8806b6cSNarsimhulu Musini struct vnic_dev_bar *bar, unsigned int num_bars)
70c8806b6cSNarsimhulu Musini {
71c8806b6cSNarsimhulu Musini struct vnic_resource_header __iomem *rh;
72c8806b6cSNarsimhulu Musini struct vnic_resource __iomem *r;
73c8806b6cSNarsimhulu Musini u8 type;
74c8806b6cSNarsimhulu Musini
75c8806b6cSNarsimhulu Musini if (num_bars == 0)
76c8806b6cSNarsimhulu Musini return -EINVAL;
77c8806b6cSNarsimhulu Musini
78c8806b6cSNarsimhulu Musini if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
79c8806b6cSNarsimhulu Musini pr_err("vNIC BAR0 res hdr length error\n");
80c8806b6cSNarsimhulu Musini
81c8806b6cSNarsimhulu Musini return -EINVAL;
82c8806b6cSNarsimhulu Musini }
83c8806b6cSNarsimhulu Musini
84c8806b6cSNarsimhulu Musini rh = bar->vaddr;
85c8806b6cSNarsimhulu Musini if (!rh) {
86c8806b6cSNarsimhulu Musini pr_err("vNIC BAR0 res hdr not mem-mapped\n");
87c8806b6cSNarsimhulu Musini
88c8806b6cSNarsimhulu Musini return -EINVAL;
89c8806b6cSNarsimhulu Musini }
90c8806b6cSNarsimhulu Musini
91c8806b6cSNarsimhulu Musini if (ioread32(&rh->magic) != VNIC_RES_MAGIC ||
92c8806b6cSNarsimhulu Musini ioread32(&rh->version) != VNIC_RES_VERSION) {
93c8806b6cSNarsimhulu Musini pr_err("vNIC BAR0 res magic/version error exp (%lx/%lx) curr (%x/%x)\n",
94c8806b6cSNarsimhulu Musini VNIC_RES_MAGIC, VNIC_RES_VERSION,
95c8806b6cSNarsimhulu Musini ioread32(&rh->magic), ioread32(&rh->version));
96c8806b6cSNarsimhulu Musini
97c8806b6cSNarsimhulu Musini return -EINVAL;
98c8806b6cSNarsimhulu Musini }
99c8806b6cSNarsimhulu Musini
100c8806b6cSNarsimhulu Musini r = (struct vnic_resource __iomem *)(rh + 1);
101c8806b6cSNarsimhulu Musini
102c8806b6cSNarsimhulu Musini while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
103c8806b6cSNarsimhulu Musini
104c8806b6cSNarsimhulu Musini u8 bar_num = ioread8(&r->bar);
105c8806b6cSNarsimhulu Musini u32 bar_offset = ioread32(&r->bar_offset);
106c8806b6cSNarsimhulu Musini u32 count = ioread32(&r->count);
107c8806b6cSNarsimhulu Musini u32 len;
108c8806b6cSNarsimhulu Musini
109c8806b6cSNarsimhulu Musini r++;
110c8806b6cSNarsimhulu Musini
111c8806b6cSNarsimhulu Musini if (bar_num >= num_bars)
112c8806b6cSNarsimhulu Musini continue;
113c8806b6cSNarsimhulu Musini
114c8806b6cSNarsimhulu Musini if (!bar[bar_num].len || !bar[bar_num].vaddr)
115c8806b6cSNarsimhulu Musini continue;
116c8806b6cSNarsimhulu Musini
117c8806b6cSNarsimhulu Musini switch (type) {
118c8806b6cSNarsimhulu Musini case RES_TYPE_WQ:
119c8806b6cSNarsimhulu Musini case RES_TYPE_RQ:
120c8806b6cSNarsimhulu Musini case RES_TYPE_CQ:
121c8806b6cSNarsimhulu Musini case RES_TYPE_INTR_CTRL:
122c8806b6cSNarsimhulu Musini /* each count is stride bytes long */
123c8806b6cSNarsimhulu Musini len = count * VNIC_RES_STRIDE;
124c8806b6cSNarsimhulu Musini if (len + bar_offset > bar->len) {
125c8806b6cSNarsimhulu Musini pr_err("vNIC BAR0 resource %d out-of-bounds, offset 0x%x + size 0x%x > bar len 0x%lx\n",
126c8806b6cSNarsimhulu Musini type, bar_offset,
127c8806b6cSNarsimhulu Musini len,
128c8806b6cSNarsimhulu Musini bar->len);
129c8806b6cSNarsimhulu Musini
130c8806b6cSNarsimhulu Musini return -EINVAL;
131c8806b6cSNarsimhulu Musini }
132c8806b6cSNarsimhulu Musini break;
133c8806b6cSNarsimhulu Musini
134c8806b6cSNarsimhulu Musini case RES_TYPE_INTR_PBA_LEGACY:
135c8806b6cSNarsimhulu Musini case RES_TYPE_DEVCMD:
136c8806b6cSNarsimhulu Musini case RES_TYPE_DEVCMD2:
137c8806b6cSNarsimhulu Musini len = count;
138c8806b6cSNarsimhulu Musini break;
139c8806b6cSNarsimhulu Musini
140c8806b6cSNarsimhulu Musini default:
141c8806b6cSNarsimhulu Musini continue;
142c8806b6cSNarsimhulu Musini }
143c8806b6cSNarsimhulu Musini
144c8806b6cSNarsimhulu Musini vdev->res[type].count = count;
145c8806b6cSNarsimhulu Musini vdev->res[type].vaddr = (char __iomem *)bar->vaddr + bar_offset;
146c8806b6cSNarsimhulu Musini }
147c8806b6cSNarsimhulu Musini
148c8806b6cSNarsimhulu Musini return 0;
149c8806b6cSNarsimhulu Musini }
150c8806b6cSNarsimhulu Musini
svnic_dev_get_res_count(struct vnic_dev * vdev,enum vnic_res_type type)151c8806b6cSNarsimhulu Musini unsigned int svnic_dev_get_res_count(struct vnic_dev *vdev,
152c8806b6cSNarsimhulu Musini enum vnic_res_type type)
153c8806b6cSNarsimhulu Musini {
154c8806b6cSNarsimhulu Musini return vdev->res[type].count;
155c8806b6cSNarsimhulu Musini }
156c8806b6cSNarsimhulu Musini
svnic_dev_get_res(struct vnic_dev * vdev,enum vnic_res_type type,unsigned int index)157c8806b6cSNarsimhulu Musini void __iomem *svnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
158c8806b6cSNarsimhulu Musini unsigned int index)
159c8806b6cSNarsimhulu Musini {
160c8806b6cSNarsimhulu Musini if (!vdev->res[type].vaddr)
161c8806b6cSNarsimhulu Musini return NULL;
162c8806b6cSNarsimhulu Musini
163c8806b6cSNarsimhulu Musini switch (type) {
164c8806b6cSNarsimhulu Musini case RES_TYPE_WQ:
165c8806b6cSNarsimhulu Musini case RES_TYPE_RQ:
166c8806b6cSNarsimhulu Musini case RES_TYPE_CQ:
167c8806b6cSNarsimhulu Musini case RES_TYPE_INTR_CTRL:
168c8806b6cSNarsimhulu Musini return (char __iomem *)vdev->res[type].vaddr +
169c8806b6cSNarsimhulu Musini index * VNIC_RES_STRIDE;
170c8806b6cSNarsimhulu Musini
171c8806b6cSNarsimhulu Musini default:
172c8806b6cSNarsimhulu Musini return (char __iomem *)vdev->res[type].vaddr;
173c8806b6cSNarsimhulu Musini }
174c8806b6cSNarsimhulu Musini }
175c8806b6cSNarsimhulu Musini
svnic_dev_desc_ring_size(struct vnic_dev_ring * ring,unsigned int desc_count,unsigned int desc_size)176c8806b6cSNarsimhulu Musini unsigned int svnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
177c8806b6cSNarsimhulu Musini unsigned int desc_count,
178c8806b6cSNarsimhulu Musini unsigned int desc_size)
179c8806b6cSNarsimhulu Musini {
180c8806b6cSNarsimhulu Musini /* The base address of the desc rings must be 512 byte aligned.
181c8806b6cSNarsimhulu Musini * Descriptor count is aligned to groups of 32 descriptors. A
182c8806b6cSNarsimhulu Musini * count of 0 means the maximum 4096 descriptors. Descriptor
183c8806b6cSNarsimhulu Musini * size is aligned to 16 bytes.
184c8806b6cSNarsimhulu Musini */
185c8806b6cSNarsimhulu Musini
186c8806b6cSNarsimhulu Musini unsigned int count_align = 32;
187c8806b6cSNarsimhulu Musini unsigned int desc_align = 16;
188c8806b6cSNarsimhulu Musini
189c8806b6cSNarsimhulu Musini ring->base_align = 512;
190c8806b6cSNarsimhulu Musini
191c8806b6cSNarsimhulu Musini if (desc_count == 0)
192c8806b6cSNarsimhulu Musini desc_count = 4096;
193c8806b6cSNarsimhulu Musini
194c8806b6cSNarsimhulu Musini ring->desc_count = ALIGN(desc_count, count_align);
195c8806b6cSNarsimhulu Musini
196c8806b6cSNarsimhulu Musini ring->desc_size = ALIGN(desc_size, desc_align);
197c8806b6cSNarsimhulu Musini
198c8806b6cSNarsimhulu Musini ring->size = ring->desc_count * ring->desc_size;
199c8806b6cSNarsimhulu Musini ring->size_unaligned = ring->size + ring->base_align;
200c8806b6cSNarsimhulu Musini
201c8806b6cSNarsimhulu Musini return ring->size_unaligned;
202c8806b6cSNarsimhulu Musini }
203c8806b6cSNarsimhulu Musini
svnic_dev_clear_desc_ring(struct vnic_dev_ring * ring)204c8806b6cSNarsimhulu Musini void svnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
205c8806b6cSNarsimhulu Musini {
206c8806b6cSNarsimhulu Musini memset(ring->descs, 0, ring->size);
207c8806b6cSNarsimhulu Musini }
208c8806b6cSNarsimhulu Musini
svnic_dev_alloc_desc_ring(struct vnic_dev * vdev,struct vnic_dev_ring * ring,unsigned int desc_count,unsigned int desc_size)209c8806b6cSNarsimhulu Musini int svnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring,
210c8806b6cSNarsimhulu Musini unsigned int desc_count, unsigned int desc_size)
211c8806b6cSNarsimhulu Musini {
212c8806b6cSNarsimhulu Musini svnic_dev_desc_ring_size(ring, desc_count, desc_size);
213c8806b6cSNarsimhulu Musini
214cecfed31SChristoph Hellwig ring->descs_unaligned = dma_alloc_coherent(&vdev->pdev->dev,
215cecfed31SChristoph Hellwig ring->size_unaligned, &ring->base_addr_unaligned,
216cecfed31SChristoph Hellwig GFP_KERNEL);
217c8806b6cSNarsimhulu Musini if (!ring->descs_unaligned) {
218c8806b6cSNarsimhulu Musini pr_err("Failed to allocate ring (size=%d), aborting\n",
219c8806b6cSNarsimhulu Musini (int)ring->size);
220c8806b6cSNarsimhulu Musini
221c8806b6cSNarsimhulu Musini return -ENOMEM;
222c8806b6cSNarsimhulu Musini }
223c8806b6cSNarsimhulu Musini
224c8806b6cSNarsimhulu Musini ring->base_addr = ALIGN(ring->base_addr_unaligned,
225c8806b6cSNarsimhulu Musini ring->base_align);
226c8806b6cSNarsimhulu Musini ring->descs = (u8 *)ring->descs_unaligned +
227c8806b6cSNarsimhulu Musini (ring->base_addr - ring->base_addr_unaligned);
228c8806b6cSNarsimhulu Musini
229c8806b6cSNarsimhulu Musini svnic_dev_clear_desc_ring(ring);
230c8806b6cSNarsimhulu Musini
231c8806b6cSNarsimhulu Musini ring->desc_avail = ring->desc_count - 1;
232c8806b6cSNarsimhulu Musini
233c8806b6cSNarsimhulu Musini return 0;
234c8806b6cSNarsimhulu Musini }
235c8806b6cSNarsimhulu Musini
svnic_dev_free_desc_ring(struct vnic_dev * vdev,struct vnic_dev_ring * ring)236c8806b6cSNarsimhulu Musini void svnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring)
237c8806b6cSNarsimhulu Musini {
238c8806b6cSNarsimhulu Musini if (ring->descs) {
239cecfed31SChristoph Hellwig dma_free_coherent(&vdev->pdev->dev,
240c8806b6cSNarsimhulu Musini ring->size_unaligned,
241c8806b6cSNarsimhulu Musini ring->descs_unaligned,
242c8806b6cSNarsimhulu Musini ring->base_addr_unaligned);
243c8806b6cSNarsimhulu Musini ring->descs = NULL;
244c8806b6cSNarsimhulu Musini }
245c8806b6cSNarsimhulu Musini }
246c8806b6cSNarsimhulu Musini
_svnic_dev_cmd2(struct vnic_dev * vdev,enum vnic_devcmd_cmd cmd,int wait)247c8806b6cSNarsimhulu Musini static int _svnic_dev_cmd2(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
248c8806b6cSNarsimhulu Musini int wait)
249c8806b6cSNarsimhulu Musini {
250c8806b6cSNarsimhulu Musini struct devcmd2_controller *dc2c = vdev->devcmd2;
2516e0ae74bSNarsimhulu Musini struct devcmd2_result *result = NULL;
252c8806b6cSNarsimhulu Musini unsigned int i;
253c8806b6cSNarsimhulu Musini int delay;
254c8806b6cSNarsimhulu Musini int err;
255c8806b6cSNarsimhulu Musini u32 posted;
2566e0ae74bSNarsimhulu Musini u32 fetch_idx;
257c8806b6cSNarsimhulu Musini u32 new_posted;
2586e0ae74bSNarsimhulu Musini u8 color;
2596e0ae74bSNarsimhulu Musini
2606e0ae74bSNarsimhulu Musini fetch_idx = ioread32(&dc2c->wq_ctrl->fetch_index);
2616e0ae74bSNarsimhulu Musini if (fetch_idx == 0xFFFFFFFF) { /* check for hardware gone */
2626e0ae74bSNarsimhulu Musini /* Hardware surprise removal: return error */
2636e0ae74bSNarsimhulu Musini return -ENODEV;
2646e0ae74bSNarsimhulu Musini }
265c8806b6cSNarsimhulu Musini
266c8806b6cSNarsimhulu Musini posted = ioread32(&dc2c->wq_ctrl->posted_index);
267c8806b6cSNarsimhulu Musini
268c8806b6cSNarsimhulu Musini if (posted == 0xFFFFFFFF) { /* check for hardware gone */
269c8806b6cSNarsimhulu Musini /* Hardware surprise removal: return error */
270c8806b6cSNarsimhulu Musini return -ENODEV;
271c8806b6cSNarsimhulu Musini }
272c8806b6cSNarsimhulu Musini
273c8806b6cSNarsimhulu Musini new_posted = (posted + 1) % DEVCMD2_RING_SIZE;
2746e0ae74bSNarsimhulu Musini if (new_posted == fetch_idx) {
2756e0ae74bSNarsimhulu Musini pr_err("%s: wq is full while issuing devcmd2 command %d, fetch index: %u, posted index: %u\n",
2766e0ae74bSNarsimhulu Musini pci_name(vdev->pdev), _CMD_N(cmd), fetch_idx, posted);
2776e0ae74bSNarsimhulu Musini
2786e0ae74bSNarsimhulu Musini return -EBUSY;
2796e0ae74bSNarsimhulu Musini }
2806e0ae74bSNarsimhulu Musini
281c8806b6cSNarsimhulu Musini dc2c->cmd_ring[posted].cmd = cmd;
282c8806b6cSNarsimhulu Musini dc2c->cmd_ring[posted].flags = 0;
283c8806b6cSNarsimhulu Musini
284c8806b6cSNarsimhulu Musini if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
285c8806b6cSNarsimhulu Musini dc2c->cmd_ring[posted].flags |= DEVCMD2_FNORESULT;
286c8806b6cSNarsimhulu Musini
287c8806b6cSNarsimhulu Musini if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
288c8806b6cSNarsimhulu Musini for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
289c8806b6cSNarsimhulu Musini dc2c->cmd_ring[posted].args[i] = vdev->args[i];
290c8806b6cSNarsimhulu Musini }
291c8806b6cSNarsimhulu Musini /* Adding write memory barrier prevents compiler and/or CPU
292c8806b6cSNarsimhulu Musini * reordering, thus avoiding descriptor posting before
293c8806b6cSNarsimhulu Musini * descriptor is initialized. Otherwise, hardware can read
294c8806b6cSNarsimhulu Musini * stale descriptor fields.
295c8806b6cSNarsimhulu Musini */
296c8806b6cSNarsimhulu Musini wmb();
297c8806b6cSNarsimhulu Musini iowrite32(new_posted, &dc2c->wq_ctrl->posted_index);
298c8806b6cSNarsimhulu Musini
299c8806b6cSNarsimhulu Musini if (dc2c->cmd_ring[posted].flags & DEVCMD2_FNORESULT)
300c8806b6cSNarsimhulu Musini return 0;
301c8806b6cSNarsimhulu Musini
3026e0ae74bSNarsimhulu Musini result = dc2c->result + dc2c->next_result;
3036e0ae74bSNarsimhulu Musini color = dc2c->color;
3046e0ae74bSNarsimhulu Musini
3056e0ae74bSNarsimhulu Musini /*
3066e0ae74bSNarsimhulu Musini * Increment next_result, after posting the devcmd, irrespective of
3076e0ae74bSNarsimhulu Musini * devcmd result, and it should be done only once.
3086e0ae74bSNarsimhulu Musini */
309c8806b6cSNarsimhulu Musini dc2c->next_result++;
310c8806b6cSNarsimhulu Musini if (dc2c->next_result == dc2c->result_size) {
311c8806b6cSNarsimhulu Musini dc2c->next_result = 0;
312c8806b6cSNarsimhulu Musini dc2c->color = dc2c->color ? 0 : 1;
313c8806b6cSNarsimhulu Musini }
3146e0ae74bSNarsimhulu Musini
3156e0ae74bSNarsimhulu Musini for (delay = 0; delay < wait; delay++) {
3166e0ae74bSNarsimhulu Musini udelay(100);
3176e0ae74bSNarsimhulu Musini if (result->color == color) {
318c8806b6cSNarsimhulu Musini if (result->error) {
319c8806b6cSNarsimhulu Musini err = (int) result->error;
320c8806b6cSNarsimhulu Musini if (err != ERR_ECMDUNKNOWN ||
321c8806b6cSNarsimhulu Musini cmd != CMD_CAPABILITY)
322c8806b6cSNarsimhulu Musini pr_err("Error %d devcmd %d\n",
323c8806b6cSNarsimhulu Musini err, _CMD_N(cmd));
324c8806b6cSNarsimhulu Musini
325c8806b6cSNarsimhulu Musini return err;
326c8806b6cSNarsimhulu Musini }
327c8806b6cSNarsimhulu Musini if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
328c8806b6cSNarsimhulu Musini for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
329c8806b6cSNarsimhulu Musini vdev->args[i] = result->results[i];
330c8806b6cSNarsimhulu Musini }
331c8806b6cSNarsimhulu Musini
332c8806b6cSNarsimhulu Musini return 0;
333c8806b6cSNarsimhulu Musini }
334c8806b6cSNarsimhulu Musini }
335c8806b6cSNarsimhulu Musini
336c8806b6cSNarsimhulu Musini pr_err("Timed out devcmd %d\n", _CMD_N(cmd));
337c8806b6cSNarsimhulu Musini
338c8806b6cSNarsimhulu Musini return -ETIMEDOUT;
339c8806b6cSNarsimhulu Musini }
340c8806b6cSNarsimhulu Musini
svnic_dev_init_devcmd2(struct vnic_dev * vdev)341c8806b6cSNarsimhulu Musini static int svnic_dev_init_devcmd2(struct vnic_dev *vdev)
342c8806b6cSNarsimhulu Musini {
343c8806b6cSNarsimhulu Musini struct devcmd2_controller *dc2c = NULL;
344c8806b6cSNarsimhulu Musini unsigned int fetch_idx;
345c8806b6cSNarsimhulu Musini int ret;
346c8806b6cSNarsimhulu Musini void __iomem *p;
347c8806b6cSNarsimhulu Musini
348c8806b6cSNarsimhulu Musini if (vdev->devcmd2)
349c8806b6cSNarsimhulu Musini return 0;
350c8806b6cSNarsimhulu Musini
351c8806b6cSNarsimhulu Musini p = svnic_dev_get_res(vdev, RES_TYPE_DEVCMD2, 0);
352c8806b6cSNarsimhulu Musini if (!p)
353c8806b6cSNarsimhulu Musini return -ENODEV;
354c8806b6cSNarsimhulu Musini
355c8806b6cSNarsimhulu Musini dc2c = kzalloc(sizeof(*dc2c), GFP_ATOMIC);
356c8806b6cSNarsimhulu Musini if (!dc2c)
357c8806b6cSNarsimhulu Musini return -ENOMEM;
358c8806b6cSNarsimhulu Musini
359c8806b6cSNarsimhulu Musini vdev->devcmd2 = dc2c;
360c8806b6cSNarsimhulu Musini
361c8806b6cSNarsimhulu Musini dc2c->color = 1;
362c8806b6cSNarsimhulu Musini dc2c->result_size = DEVCMD2_RING_SIZE;
363c8806b6cSNarsimhulu Musini
364c8806b6cSNarsimhulu Musini ret = vnic_wq_devcmd2_alloc(vdev,
365c8806b6cSNarsimhulu Musini &dc2c->wq,
366c8806b6cSNarsimhulu Musini DEVCMD2_RING_SIZE,
367c8806b6cSNarsimhulu Musini DEVCMD2_DESC_SIZE);
368c8806b6cSNarsimhulu Musini if (ret)
369c8806b6cSNarsimhulu Musini goto err_free_devcmd2;
370c8806b6cSNarsimhulu Musini
371c8806b6cSNarsimhulu Musini fetch_idx = ioread32(&dc2c->wq.ctrl->fetch_index);
372c8806b6cSNarsimhulu Musini if (fetch_idx == 0xFFFFFFFF) { /* check for hardware gone */
373c8806b6cSNarsimhulu Musini /* Hardware surprise removal: reset fetch_index */
374c8806b6cSNarsimhulu Musini fetch_idx = 0;
375c8806b6cSNarsimhulu Musini }
376c8806b6cSNarsimhulu Musini
377c8806b6cSNarsimhulu Musini /*
378c8806b6cSNarsimhulu Musini * Don't change fetch_index ever and
379c8806b6cSNarsimhulu Musini * set posted_index same as fetch_index
380c8806b6cSNarsimhulu Musini * when setting up the WQ for devcmd2.
381c8806b6cSNarsimhulu Musini */
382c8806b6cSNarsimhulu Musini vnic_wq_init_start(&dc2c->wq, 0, fetch_idx, fetch_idx, 0, 0);
383c8806b6cSNarsimhulu Musini svnic_wq_enable(&dc2c->wq);
384c8806b6cSNarsimhulu Musini ret = svnic_dev_alloc_desc_ring(vdev,
385c8806b6cSNarsimhulu Musini &dc2c->results_ring,
386c8806b6cSNarsimhulu Musini DEVCMD2_RING_SIZE,
387c8806b6cSNarsimhulu Musini DEVCMD2_DESC_SIZE);
388c8806b6cSNarsimhulu Musini if (ret)
389c8806b6cSNarsimhulu Musini goto err_free_wq;
390c8806b6cSNarsimhulu Musini
391c8806b6cSNarsimhulu Musini dc2c->result = (struct devcmd2_result *) dc2c->results_ring.descs;
392c8806b6cSNarsimhulu Musini dc2c->cmd_ring = (struct vnic_devcmd2 *) dc2c->wq.ring.descs;
393c8806b6cSNarsimhulu Musini dc2c->wq_ctrl = dc2c->wq.ctrl;
394c8806b6cSNarsimhulu Musini vdev->args[0] = (u64) dc2c->results_ring.base_addr | VNIC_PADDR_TARGET;
395c8806b6cSNarsimhulu Musini vdev->args[1] = DEVCMD2_RING_SIZE;
396c8806b6cSNarsimhulu Musini
397c8806b6cSNarsimhulu Musini ret = _svnic_dev_cmd2(vdev, CMD_INITIALIZE_DEVCMD2, VNIC_DVCMD_TMO);
398c8806b6cSNarsimhulu Musini if (ret < 0)
399c8806b6cSNarsimhulu Musini goto err_free_desc_ring;
400c8806b6cSNarsimhulu Musini
401c8806b6cSNarsimhulu Musini vdev->devcmd_rtn = &_svnic_dev_cmd2;
402c8806b6cSNarsimhulu Musini pr_info("DEVCMD2 Initialized.\n");
403c8806b6cSNarsimhulu Musini
404c8806b6cSNarsimhulu Musini return ret;
405c8806b6cSNarsimhulu Musini
406c8806b6cSNarsimhulu Musini err_free_desc_ring:
407c8806b6cSNarsimhulu Musini svnic_dev_free_desc_ring(vdev, &dc2c->results_ring);
408c8806b6cSNarsimhulu Musini
409c8806b6cSNarsimhulu Musini err_free_wq:
410c8806b6cSNarsimhulu Musini svnic_wq_disable(&dc2c->wq);
411c8806b6cSNarsimhulu Musini svnic_wq_free(&dc2c->wq);
412c8806b6cSNarsimhulu Musini
413c8806b6cSNarsimhulu Musini err_free_devcmd2:
414c8806b6cSNarsimhulu Musini kfree(dc2c);
415c8806b6cSNarsimhulu Musini vdev->devcmd2 = NULL;
416c8806b6cSNarsimhulu Musini
417c8806b6cSNarsimhulu Musini return ret;
418c8806b6cSNarsimhulu Musini } /* end of svnic_dev_init_devcmd2 */
419c8806b6cSNarsimhulu Musini
vnic_dev_deinit_devcmd2(struct vnic_dev * vdev)420c8806b6cSNarsimhulu Musini static void vnic_dev_deinit_devcmd2(struct vnic_dev *vdev)
421c8806b6cSNarsimhulu Musini {
422c8806b6cSNarsimhulu Musini struct devcmd2_controller *dc2c = vdev->devcmd2;
423c8806b6cSNarsimhulu Musini
424c8806b6cSNarsimhulu Musini vdev->devcmd2 = NULL;
425c8806b6cSNarsimhulu Musini vdev->devcmd_rtn = NULL;
426c8806b6cSNarsimhulu Musini
427c8806b6cSNarsimhulu Musini svnic_dev_free_desc_ring(vdev, &dc2c->results_ring);
428c8806b6cSNarsimhulu Musini svnic_wq_disable(&dc2c->wq);
429c8806b6cSNarsimhulu Musini svnic_wq_free(&dc2c->wq);
430c8806b6cSNarsimhulu Musini kfree(dc2c);
431c8806b6cSNarsimhulu Musini }
432c8806b6cSNarsimhulu Musini
svnic_dev_cmd(struct vnic_dev * vdev,enum vnic_devcmd_cmd cmd,u64 * a0,u64 * a1,int wait)433c8806b6cSNarsimhulu Musini int svnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
434c8806b6cSNarsimhulu Musini u64 *a0, u64 *a1, int wait)
435c8806b6cSNarsimhulu Musini {
436c8806b6cSNarsimhulu Musini int err;
437c8806b6cSNarsimhulu Musini
438c8806b6cSNarsimhulu Musini memset(vdev->args, 0, sizeof(vdev->args));
439c8806b6cSNarsimhulu Musini vdev->args[0] = *a0;
440c8806b6cSNarsimhulu Musini vdev->args[1] = *a1;
441c8806b6cSNarsimhulu Musini
442c8806b6cSNarsimhulu Musini err = (*vdev->devcmd_rtn)(vdev, cmd, wait);
443c8806b6cSNarsimhulu Musini
444c8806b6cSNarsimhulu Musini *a0 = vdev->args[0];
445c8806b6cSNarsimhulu Musini *a1 = vdev->args[1];
446c8806b6cSNarsimhulu Musini
447c8806b6cSNarsimhulu Musini return err;
448c8806b6cSNarsimhulu Musini }
449c8806b6cSNarsimhulu Musini
svnic_dev_fw_info(struct vnic_dev * vdev,struct vnic_devcmd_fw_info ** fw_info)450c8806b6cSNarsimhulu Musini int svnic_dev_fw_info(struct vnic_dev *vdev,
451c8806b6cSNarsimhulu Musini struct vnic_devcmd_fw_info **fw_info)
452c8806b6cSNarsimhulu Musini {
453c8806b6cSNarsimhulu Musini u64 a0, a1 = 0;
454c8806b6cSNarsimhulu Musini int wait = VNIC_DVCMD_TMO;
455c8806b6cSNarsimhulu Musini int err = 0;
456c8806b6cSNarsimhulu Musini
457c8806b6cSNarsimhulu Musini if (!vdev->fw_info) {
458cecfed31SChristoph Hellwig vdev->fw_info = dma_alloc_coherent(&vdev->pdev->dev,
459c8806b6cSNarsimhulu Musini sizeof(struct vnic_devcmd_fw_info),
460cecfed31SChristoph Hellwig &vdev->fw_info_pa, GFP_KERNEL);
461c8806b6cSNarsimhulu Musini if (!vdev->fw_info)
462c8806b6cSNarsimhulu Musini return -ENOMEM;
463c8806b6cSNarsimhulu Musini
464c8806b6cSNarsimhulu Musini a0 = vdev->fw_info_pa;
465c8806b6cSNarsimhulu Musini
466c8806b6cSNarsimhulu Musini /* only get fw_info once and cache it */
467c8806b6cSNarsimhulu Musini err = svnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait);
468c8806b6cSNarsimhulu Musini }
469c8806b6cSNarsimhulu Musini
470c8806b6cSNarsimhulu Musini *fw_info = vdev->fw_info;
471c8806b6cSNarsimhulu Musini
472c8806b6cSNarsimhulu Musini return err;
473c8806b6cSNarsimhulu Musini }
474c8806b6cSNarsimhulu Musini
svnic_dev_spec(struct vnic_dev * vdev,unsigned int offset,unsigned int size,void * value)475c8806b6cSNarsimhulu Musini int svnic_dev_spec(struct vnic_dev *vdev, unsigned int offset,
476c8806b6cSNarsimhulu Musini unsigned int size, void *value)
477c8806b6cSNarsimhulu Musini {
478c8806b6cSNarsimhulu Musini u64 a0, a1;
479c8806b6cSNarsimhulu Musini int wait = VNIC_DVCMD_TMO;
480c8806b6cSNarsimhulu Musini int err;
481c8806b6cSNarsimhulu Musini
482c8806b6cSNarsimhulu Musini a0 = offset;
483c8806b6cSNarsimhulu Musini a1 = size;
484c8806b6cSNarsimhulu Musini
485c8806b6cSNarsimhulu Musini err = svnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
486c8806b6cSNarsimhulu Musini
487c8806b6cSNarsimhulu Musini switch (size) {
488c8806b6cSNarsimhulu Musini case 1:
489c8806b6cSNarsimhulu Musini *(u8 *)value = (u8)a0;
490c8806b6cSNarsimhulu Musini break;
491c8806b6cSNarsimhulu Musini case 2:
492c8806b6cSNarsimhulu Musini *(u16 *)value = (u16)a0;
493c8806b6cSNarsimhulu Musini break;
494c8806b6cSNarsimhulu Musini case 4:
495c8806b6cSNarsimhulu Musini *(u32 *)value = (u32)a0;
496c8806b6cSNarsimhulu Musini break;
497c8806b6cSNarsimhulu Musini case 8:
498c8806b6cSNarsimhulu Musini *(u64 *)value = a0;
499c8806b6cSNarsimhulu Musini break;
500c8806b6cSNarsimhulu Musini default:
501c8806b6cSNarsimhulu Musini BUG();
502c8806b6cSNarsimhulu Musini break;
503c8806b6cSNarsimhulu Musini }
504c8806b6cSNarsimhulu Musini
505c8806b6cSNarsimhulu Musini return err;
506c8806b6cSNarsimhulu Musini }
507c8806b6cSNarsimhulu Musini
svnic_dev_stats_clear(struct vnic_dev * vdev)508c8806b6cSNarsimhulu Musini int svnic_dev_stats_clear(struct vnic_dev *vdev)
509c8806b6cSNarsimhulu Musini {
510c8806b6cSNarsimhulu Musini u64 a0 = 0, a1 = 0;
511c8806b6cSNarsimhulu Musini int wait = VNIC_DVCMD_TMO;
512c8806b6cSNarsimhulu Musini
513c8806b6cSNarsimhulu Musini return svnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
514c8806b6cSNarsimhulu Musini }
515c8806b6cSNarsimhulu Musini
svnic_dev_stats_dump(struct vnic_dev * vdev,struct vnic_stats ** stats)516c8806b6cSNarsimhulu Musini int svnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
517c8806b6cSNarsimhulu Musini {
518c8806b6cSNarsimhulu Musini u64 a0, a1;
519c8806b6cSNarsimhulu Musini int wait = VNIC_DVCMD_TMO;
520c8806b6cSNarsimhulu Musini
521c8806b6cSNarsimhulu Musini if (!vdev->stats) {
522cecfed31SChristoph Hellwig vdev->stats = dma_alloc_coherent(&vdev->pdev->dev,
523cecfed31SChristoph Hellwig sizeof(struct vnic_stats), &vdev->stats_pa, GFP_KERNEL);
524c8806b6cSNarsimhulu Musini if (!vdev->stats)
525c8806b6cSNarsimhulu Musini return -ENOMEM;
526c8806b6cSNarsimhulu Musini }
527c8806b6cSNarsimhulu Musini
528c8806b6cSNarsimhulu Musini *stats = vdev->stats;
529c8806b6cSNarsimhulu Musini a0 = vdev->stats_pa;
530c8806b6cSNarsimhulu Musini a1 = sizeof(struct vnic_stats);
531c8806b6cSNarsimhulu Musini
532c8806b6cSNarsimhulu Musini return svnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
533c8806b6cSNarsimhulu Musini }
534c8806b6cSNarsimhulu Musini
svnic_dev_close(struct vnic_dev * vdev)535c8806b6cSNarsimhulu Musini int svnic_dev_close(struct vnic_dev *vdev)
536c8806b6cSNarsimhulu Musini {
537c8806b6cSNarsimhulu Musini u64 a0 = 0, a1 = 0;
538c8806b6cSNarsimhulu Musini int wait = VNIC_DVCMD_TMO;
539c8806b6cSNarsimhulu Musini
540c8806b6cSNarsimhulu Musini return svnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
541c8806b6cSNarsimhulu Musini }
542c8806b6cSNarsimhulu Musini
svnic_dev_enable_wait(struct vnic_dev * vdev)543c8806b6cSNarsimhulu Musini int svnic_dev_enable_wait(struct vnic_dev *vdev)
544c8806b6cSNarsimhulu Musini {
545c8806b6cSNarsimhulu Musini u64 a0 = 0, a1 = 0;
546c8806b6cSNarsimhulu Musini int wait = VNIC_DVCMD_TMO;
547c8806b6cSNarsimhulu Musini int err = 0;
548c8806b6cSNarsimhulu Musini
549c8806b6cSNarsimhulu Musini err = svnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
550c8806b6cSNarsimhulu Musini if (err == ERR_ECMDUNKNOWN)
551c8806b6cSNarsimhulu Musini return svnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
552c8806b6cSNarsimhulu Musini
553c8806b6cSNarsimhulu Musini return err;
554c8806b6cSNarsimhulu Musini }
555c8806b6cSNarsimhulu Musini
svnic_dev_disable(struct vnic_dev * vdev)556c8806b6cSNarsimhulu Musini int svnic_dev_disable(struct vnic_dev *vdev)
557c8806b6cSNarsimhulu Musini {
558c8806b6cSNarsimhulu Musini u64 a0 = 0, a1 = 0;
559c8806b6cSNarsimhulu Musini int wait = VNIC_DVCMD_TMO;
560c8806b6cSNarsimhulu Musini
561c8806b6cSNarsimhulu Musini return svnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
562c8806b6cSNarsimhulu Musini }
563c8806b6cSNarsimhulu Musini
svnic_dev_open(struct vnic_dev * vdev,int arg)564c8806b6cSNarsimhulu Musini int svnic_dev_open(struct vnic_dev *vdev, int arg)
565c8806b6cSNarsimhulu Musini {
566c8806b6cSNarsimhulu Musini u64 a0 = (u32)arg, a1 = 0;
567c8806b6cSNarsimhulu Musini int wait = VNIC_DVCMD_TMO;
568c8806b6cSNarsimhulu Musini
569c8806b6cSNarsimhulu Musini return svnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
570c8806b6cSNarsimhulu Musini }
571c8806b6cSNarsimhulu Musini
svnic_dev_open_done(struct vnic_dev * vdev,int * done)572c8806b6cSNarsimhulu Musini int svnic_dev_open_done(struct vnic_dev *vdev, int *done)
573c8806b6cSNarsimhulu Musini {
574c8806b6cSNarsimhulu Musini u64 a0 = 0, a1 = 0;
575c8806b6cSNarsimhulu Musini int wait = VNIC_DVCMD_TMO;
576c8806b6cSNarsimhulu Musini int err;
577c8806b6cSNarsimhulu Musini
578c8806b6cSNarsimhulu Musini *done = 0;
579c8806b6cSNarsimhulu Musini
580c8806b6cSNarsimhulu Musini err = svnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
581c8806b6cSNarsimhulu Musini if (err)
582c8806b6cSNarsimhulu Musini return err;
583c8806b6cSNarsimhulu Musini
584c8806b6cSNarsimhulu Musini *done = (a0 == 0);
585c8806b6cSNarsimhulu Musini
586c8806b6cSNarsimhulu Musini return 0;
587c8806b6cSNarsimhulu Musini }
588c8806b6cSNarsimhulu Musini
svnic_dev_notify_set(struct vnic_dev * vdev,u16 intr)589c8806b6cSNarsimhulu Musini int svnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
590c8806b6cSNarsimhulu Musini {
591c8806b6cSNarsimhulu Musini u64 a0, a1;
592c8806b6cSNarsimhulu Musini int wait = VNIC_DVCMD_TMO;
593c8806b6cSNarsimhulu Musini
594c8806b6cSNarsimhulu Musini if (!vdev->notify) {
595cecfed31SChristoph Hellwig vdev->notify = dma_alloc_coherent(&vdev->pdev->dev,
596c8806b6cSNarsimhulu Musini sizeof(struct vnic_devcmd_notify),
597cecfed31SChristoph Hellwig &vdev->notify_pa, GFP_KERNEL);
598c8806b6cSNarsimhulu Musini if (!vdev->notify)
599c8806b6cSNarsimhulu Musini return -ENOMEM;
600c8806b6cSNarsimhulu Musini }
601c8806b6cSNarsimhulu Musini
602c8806b6cSNarsimhulu Musini a0 = vdev->notify_pa;
603c8806b6cSNarsimhulu Musini a1 = ((u64)intr << 32) & VNIC_NOTIFY_INTR_MASK;
604c8806b6cSNarsimhulu Musini a1 += sizeof(struct vnic_devcmd_notify);
605c8806b6cSNarsimhulu Musini
606c8806b6cSNarsimhulu Musini return svnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
607c8806b6cSNarsimhulu Musini }
608c8806b6cSNarsimhulu Musini
svnic_dev_notify_unset(struct vnic_dev * vdev)609c8806b6cSNarsimhulu Musini void svnic_dev_notify_unset(struct vnic_dev *vdev)
610c8806b6cSNarsimhulu Musini {
611c8806b6cSNarsimhulu Musini u64 a0, a1;
612c8806b6cSNarsimhulu Musini int wait = VNIC_DVCMD_TMO;
613c8806b6cSNarsimhulu Musini
614c8806b6cSNarsimhulu Musini a0 = 0; /* paddr = 0 to unset notify buffer */
615c8806b6cSNarsimhulu Musini a1 = VNIC_NOTIFY_INTR_MASK; /* intr num = -1 to unreg for intr */
616c8806b6cSNarsimhulu Musini a1 += sizeof(struct vnic_devcmd_notify);
617c8806b6cSNarsimhulu Musini
618c8806b6cSNarsimhulu Musini svnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
619c8806b6cSNarsimhulu Musini }
620c8806b6cSNarsimhulu Musini
vnic_dev_notify_ready(struct vnic_dev * vdev)621c8806b6cSNarsimhulu Musini static int vnic_dev_notify_ready(struct vnic_dev *vdev)
622c8806b6cSNarsimhulu Musini {
623c8806b6cSNarsimhulu Musini u32 *words;
624c8806b6cSNarsimhulu Musini unsigned int nwords = sizeof(struct vnic_devcmd_notify) / 4;
625c8806b6cSNarsimhulu Musini unsigned int i;
626c8806b6cSNarsimhulu Musini u32 csum;
627c8806b6cSNarsimhulu Musini
628c8806b6cSNarsimhulu Musini if (!vdev->notify)
629c8806b6cSNarsimhulu Musini return 0;
630c8806b6cSNarsimhulu Musini
631c8806b6cSNarsimhulu Musini do {
632c8806b6cSNarsimhulu Musini csum = 0;
633c8806b6cSNarsimhulu Musini memcpy(&vdev->notify_copy, vdev->notify,
634c8806b6cSNarsimhulu Musini sizeof(struct vnic_devcmd_notify));
635c8806b6cSNarsimhulu Musini words = (u32 *)&vdev->notify_copy;
636c8806b6cSNarsimhulu Musini for (i = 1; i < nwords; i++)
637c8806b6cSNarsimhulu Musini csum += words[i];
638c8806b6cSNarsimhulu Musini } while (csum != words[0]);
639c8806b6cSNarsimhulu Musini
640c8806b6cSNarsimhulu Musini return 1;
641c8806b6cSNarsimhulu Musini }
642c8806b6cSNarsimhulu Musini
svnic_dev_init(struct vnic_dev * vdev,int arg)643c8806b6cSNarsimhulu Musini int svnic_dev_init(struct vnic_dev *vdev, int arg)
644c8806b6cSNarsimhulu Musini {
645c8806b6cSNarsimhulu Musini u64 a0 = (u32)arg, a1 = 0;
646c8806b6cSNarsimhulu Musini int wait = VNIC_DVCMD_TMO;
647c8806b6cSNarsimhulu Musini
648c8806b6cSNarsimhulu Musini return svnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
649c8806b6cSNarsimhulu Musini }
650c8806b6cSNarsimhulu Musini
svnic_dev_link_status(struct vnic_dev * vdev)651c8806b6cSNarsimhulu Musini int svnic_dev_link_status(struct vnic_dev *vdev)
652c8806b6cSNarsimhulu Musini {
653c8806b6cSNarsimhulu Musini if (vdev->linkstatus)
654c8806b6cSNarsimhulu Musini return *vdev->linkstatus;
655c8806b6cSNarsimhulu Musini
656c8806b6cSNarsimhulu Musini if (!vnic_dev_notify_ready(vdev))
657c8806b6cSNarsimhulu Musini return 0;
658c8806b6cSNarsimhulu Musini
659c8806b6cSNarsimhulu Musini return vdev->notify_copy.link_state;
660c8806b6cSNarsimhulu Musini }
661c8806b6cSNarsimhulu Musini
svnic_dev_link_down_cnt(struct vnic_dev * vdev)662c8806b6cSNarsimhulu Musini u32 svnic_dev_link_down_cnt(struct vnic_dev *vdev)
663c8806b6cSNarsimhulu Musini {
664c8806b6cSNarsimhulu Musini if (!vnic_dev_notify_ready(vdev))
665c8806b6cSNarsimhulu Musini return 0;
666c8806b6cSNarsimhulu Musini
667c8806b6cSNarsimhulu Musini return vdev->notify_copy.link_down_cnt;
668c8806b6cSNarsimhulu Musini }
669c8806b6cSNarsimhulu Musini
svnic_dev_set_intr_mode(struct vnic_dev * vdev,enum vnic_dev_intr_mode intr_mode)670c8806b6cSNarsimhulu Musini void svnic_dev_set_intr_mode(struct vnic_dev *vdev,
671c8806b6cSNarsimhulu Musini enum vnic_dev_intr_mode intr_mode)
672c8806b6cSNarsimhulu Musini {
673c8806b6cSNarsimhulu Musini vdev->intr_mode = intr_mode;
674c8806b6cSNarsimhulu Musini }
675c8806b6cSNarsimhulu Musini
svnic_dev_get_intr_mode(struct vnic_dev * vdev)676c8806b6cSNarsimhulu Musini enum vnic_dev_intr_mode svnic_dev_get_intr_mode(struct vnic_dev *vdev)
677c8806b6cSNarsimhulu Musini {
678c8806b6cSNarsimhulu Musini return vdev->intr_mode;
679c8806b6cSNarsimhulu Musini }
680c8806b6cSNarsimhulu Musini
svnic_dev_unregister(struct vnic_dev * vdev)681c8806b6cSNarsimhulu Musini void svnic_dev_unregister(struct vnic_dev *vdev)
682c8806b6cSNarsimhulu Musini {
683c8806b6cSNarsimhulu Musini if (vdev) {
684c8806b6cSNarsimhulu Musini if (vdev->notify)
685cecfed31SChristoph Hellwig dma_free_coherent(&vdev->pdev->dev,
686c8806b6cSNarsimhulu Musini sizeof(struct vnic_devcmd_notify),
687c8806b6cSNarsimhulu Musini vdev->notify,
688c8806b6cSNarsimhulu Musini vdev->notify_pa);
689c8806b6cSNarsimhulu Musini if (vdev->linkstatus)
690cecfed31SChristoph Hellwig dma_free_coherent(&vdev->pdev->dev,
691c8806b6cSNarsimhulu Musini sizeof(u32),
692c8806b6cSNarsimhulu Musini vdev->linkstatus,
693c8806b6cSNarsimhulu Musini vdev->linkstatus_pa);
694c8806b6cSNarsimhulu Musini if (vdev->stats)
695cecfed31SChristoph Hellwig dma_free_coherent(&vdev->pdev->dev,
696c8806b6cSNarsimhulu Musini sizeof(struct vnic_stats),
697c8806b6cSNarsimhulu Musini vdev->stats, vdev->stats_pa);
698c8806b6cSNarsimhulu Musini if (vdev->fw_info)
699cecfed31SChristoph Hellwig dma_free_coherent(&vdev->pdev->dev,
700c8806b6cSNarsimhulu Musini sizeof(struct vnic_devcmd_fw_info),
701c8806b6cSNarsimhulu Musini vdev->fw_info, vdev->fw_info_pa);
702c8806b6cSNarsimhulu Musini if (vdev->devcmd2)
703c8806b6cSNarsimhulu Musini vnic_dev_deinit_devcmd2(vdev);
704c8806b6cSNarsimhulu Musini kfree(vdev);
705c8806b6cSNarsimhulu Musini }
706c8806b6cSNarsimhulu Musini }
707c8806b6cSNarsimhulu Musini
svnic_dev_alloc_discover(struct vnic_dev * vdev,void * priv,struct pci_dev * pdev,struct vnic_dev_bar * bar,unsigned int num_bars)708c8806b6cSNarsimhulu Musini struct vnic_dev *svnic_dev_alloc_discover(struct vnic_dev *vdev,
709c8806b6cSNarsimhulu Musini void *priv,
710c8806b6cSNarsimhulu Musini struct pci_dev *pdev,
711c8806b6cSNarsimhulu Musini struct vnic_dev_bar *bar,
712c8806b6cSNarsimhulu Musini unsigned int num_bars)
713c8806b6cSNarsimhulu Musini {
714c8806b6cSNarsimhulu Musini if (!vdev) {
715c8806b6cSNarsimhulu Musini vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
716c8806b6cSNarsimhulu Musini if (!vdev)
717c8806b6cSNarsimhulu Musini return NULL;
718c8806b6cSNarsimhulu Musini }
719c8806b6cSNarsimhulu Musini
720c8806b6cSNarsimhulu Musini vdev->priv = priv;
721c8806b6cSNarsimhulu Musini vdev->pdev = pdev;
722c8806b6cSNarsimhulu Musini
723c8806b6cSNarsimhulu Musini if (vnic_dev_discover_res(vdev, bar, num_bars))
724c8806b6cSNarsimhulu Musini goto err_out;
725c8806b6cSNarsimhulu Musini
726c8806b6cSNarsimhulu Musini return vdev;
727c8806b6cSNarsimhulu Musini
728c8806b6cSNarsimhulu Musini err_out:
729c8806b6cSNarsimhulu Musini svnic_dev_unregister(vdev);
730c8806b6cSNarsimhulu Musini
731c8806b6cSNarsimhulu Musini return NULL;
732c8806b6cSNarsimhulu Musini } /* end of svnic_dev_alloc_discover */
733c8806b6cSNarsimhulu Musini
734c8806b6cSNarsimhulu Musini /*
735c8806b6cSNarsimhulu Musini * fallback option is left to keep the interface common for other vnics.
736c8806b6cSNarsimhulu Musini */
svnic_dev_cmd_init(struct vnic_dev * vdev,int fallback)737c8806b6cSNarsimhulu Musini int svnic_dev_cmd_init(struct vnic_dev *vdev, int fallback)
738c8806b6cSNarsimhulu Musini {
739c8806b6cSNarsimhulu Musini int err = -ENODEV;
740c8806b6cSNarsimhulu Musini void __iomem *p;
741c8806b6cSNarsimhulu Musini
742c8806b6cSNarsimhulu Musini p = svnic_dev_get_res(vdev, RES_TYPE_DEVCMD2, 0);
743c8806b6cSNarsimhulu Musini if (p)
744c8806b6cSNarsimhulu Musini err = svnic_dev_init_devcmd2(vdev);
745c8806b6cSNarsimhulu Musini else
746c8806b6cSNarsimhulu Musini pr_err("DEVCMD2 resource not found.\n");
747c8806b6cSNarsimhulu Musini
748c8806b6cSNarsimhulu Musini return err;
749c8806b6cSNarsimhulu Musini } /* end of svnic_dev_cmd_init */
750