1/*
2 * Copyright 2008 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#include <linux/errno.h>
20#include <linux/types.h>
21#include <linux/pci.h>
22#include <linux/delay.h>
23#include "vnic_wq_copy.h"
24
25void vnic_wq_copy_enable(struct vnic_wq_copy *wq)
26{
27	iowrite32(1, &wq->ctrl->enable);
28}
29
30int vnic_wq_copy_disable(struct vnic_wq_copy *wq)
31{
32	unsigned int wait;
33
34	iowrite32(0, &wq->ctrl->enable);
35
36	/* Wait for HW to ACK disable request */
37	for (wait = 0; wait < 100; wait++) {
38		if (!(ioread32(&wq->ctrl->running)))
39			return 0;
40		udelay(1);
41	}
42
43	printk(KERN_ERR "Failed to disable Copy WQ[%d],"
44	       " fetch index=%d, posted_index=%d\n",
45	       wq->index, ioread32(&wq->ctrl->fetch_index),
46	       ioread32(&wq->ctrl->posted_index));
47
48	return -ENODEV;
49}
50
51void vnic_wq_copy_clean(struct vnic_wq_copy *wq,
52	void (*q_clean)(struct vnic_wq_copy *wq,
53	struct fcpio_host_req *wq_desc))
54{
55	BUG_ON(ioread32(&wq->ctrl->enable));
56
57	if (vnic_wq_copy_desc_in_use(wq))
58		vnic_wq_copy_service(wq, -1, q_clean);
59
60	wq->to_use_index = wq->to_clean_index = 0;
61
62	iowrite32(0, &wq->ctrl->fetch_index);
63	iowrite32(0, &wq->ctrl->posted_index);
64	iowrite32(0, &wq->ctrl->error_status);
65
66	vnic_dev_clear_desc_ring(&wq->ring);
67}
68
69void vnic_wq_copy_free(struct vnic_wq_copy *wq)
70{
71	struct vnic_dev *vdev;
72
73	vdev = wq->vdev;
74	vnic_dev_free_desc_ring(vdev, &wq->ring);
75	wq->ctrl = NULL;
76}
77
78int vnic_wq_copy_alloc(struct vnic_dev *vdev, struct vnic_wq_copy *wq,
79		       unsigned int index, unsigned int desc_count,
80		       unsigned int desc_size)
81{
82	int err;
83
84	wq->index = index;
85	wq->vdev = vdev;
86	wq->to_use_index = wq->to_clean_index = 0;
87	wq->ctrl = vnic_dev_get_res(vdev, RES_TYPE_WQ, index);
88	if (!wq->ctrl) {
89		printk(KERN_ERR "Failed to hook COPY WQ[%d] resource\n", index);
90		return -EINVAL;
91	}
92
93	vnic_wq_copy_disable(wq);
94
95	err = vnic_dev_alloc_desc_ring(vdev, &wq->ring, desc_count, desc_size);
96	if (err)
97		return err;
98
99	return 0;
100}
101
102void vnic_wq_copy_init(struct vnic_wq_copy *wq, unsigned int cq_index,
103	unsigned int error_interrupt_enable,
104	unsigned int error_interrupt_offset)
105{
106	u64 paddr;
107
108	paddr = (u64)wq->ring.base_addr | VNIC_PADDR_TARGET;
109	writeq(paddr, &wq->ctrl->ring_base);
110	iowrite32(wq->ring.desc_count, &wq->ctrl->ring_size);
111	iowrite32(0, &wq->ctrl->fetch_index);
112	iowrite32(0, &wq->ctrl->posted_index);
113	iowrite32(cq_index, &wq->ctrl->cq_index);
114	iowrite32(error_interrupt_enable, &wq->ctrl->error_interrupt_enable);
115	iowrite32(error_interrupt_offset, &wq->ctrl->error_interrupt_offset);
116}
117
118