2 * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
5 * Copyright (c) 2014, Cisco Systems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
39 int vnic_wq_get_ctrl(struct vnic_dev *vdev, struct vnic_wq *wq,
40 unsigned int index, enum vnic_res_type res_type)
42 wq->ctrl = vnic_dev_get_res(vdev, res_type, index);
49 int vnic_wq_alloc_ring(struct vnic_dev *vdev, struct vnic_wq *wq,
50 unsigned int desc_count, unsigned int desc_size)
52 char res_name[NAME_MAX];
55 snprintf(res_name, sizeof(res_name), "%d-wq-%d", instance++, wq->index);
56 return vnic_dev_alloc_desc_ring(vdev, &wq->ring, desc_count, desc_size,
57 wq->socket_id, res_name);
60 static int vnic_wq_alloc_bufs(struct vnic_wq *wq)
62 unsigned int count = wq->ring.desc_count;
63 /* Allocate the mbuf ring */
64 wq->bufs = (struct vnic_wq_buf *)rte_zmalloc_socket("wq->bufs",
65 sizeof(struct vnic_wq_buf) * count,
66 RTE_CACHE_LINE_SIZE, wq->socket_id);
74 void vnic_wq_free(struct vnic_wq *wq)
76 struct vnic_dev *vdev;
80 vnic_dev_free_desc_ring(vdev, &wq->ring);
87 int vnic_wq_alloc(struct vnic_dev *vdev, struct vnic_wq *wq, unsigned int index,
88 unsigned int desc_count, unsigned int desc_size)
95 err = vnic_wq_get_ctrl(vdev, wq, index, RES_TYPE_WQ);
97 pr_err("Failed to hook WQ[%d] resource, err %d\n", index, err);
103 err = vnic_wq_alloc_ring(vdev, wq, desc_count, desc_size);
107 err = vnic_wq_alloc_bufs(wq);
116 void vnic_wq_init_start(struct vnic_wq *wq, unsigned int cq_index,
117 unsigned int fetch_index, unsigned int posted_index,
118 unsigned int error_interrupt_enable,
119 unsigned int error_interrupt_offset)
122 unsigned int count = wq->ring.desc_count;
124 paddr = (u64)wq->ring.base_addr | VNIC_PADDR_TARGET;
125 writeq(paddr, &wq->ctrl->ring_base);
126 iowrite32(count, &wq->ctrl->ring_size);
127 iowrite32(fetch_index, &wq->ctrl->fetch_index);
128 iowrite32(posted_index, &wq->ctrl->posted_index);
129 iowrite32(cq_index, &wq->ctrl->cq_index);
130 iowrite32(error_interrupt_enable, &wq->ctrl->error_interrupt_enable);
131 iowrite32(error_interrupt_offset, &wq->ctrl->error_interrupt_offset);
132 iowrite32(0, &wq->ctrl->error_status);
134 wq->head_idx = fetch_index;
135 wq->tail_idx = wq->head_idx;
138 void vnic_wq_init(struct vnic_wq *wq, unsigned int cq_index,
139 unsigned int error_interrupt_enable,
140 unsigned int error_interrupt_offset)
142 vnic_wq_init_start(wq, cq_index, 0, 0,
143 error_interrupt_enable,
144 error_interrupt_offset);
145 wq->last_completed_index = 0;
148 void vnic_wq_error_out(struct vnic_wq *wq, unsigned int error)
150 iowrite32(error, &wq->ctrl->error_status);
153 unsigned int vnic_wq_error_status(struct vnic_wq *wq)
155 return ioread32(&wq->ctrl->error_status);
158 void vnic_wq_enable(struct vnic_wq *wq)
160 iowrite32(1, &wq->ctrl->enable);
163 int vnic_wq_disable(struct vnic_wq *wq)
167 iowrite32(0, &wq->ctrl->enable);
169 /* Wait for HW to ACK disable request */
170 for (wait = 0; wait < 1000; wait++) {
171 if (!(ioread32(&wq->ctrl->running)))
176 pr_err("Failed to disable WQ[%d]\n", wq->index);
181 void vnic_wq_clean(struct vnic_wq *wq,
182 void (*buf_clean)(struct vnic_wq_buf *buf))
184 struct vnic_wq_buf *buf;
185 unsigned int to_clean = wq->tail_idx;
187 buf = &wq->bufs[to_clean];
189 while (vnic_wq_desc_used(wq) > 0) {
192 to_clean = buf_idx_incr(wq->ring.desc_count, to_clean);
194 buf = &wq->bufs[to_clean];
195 wq->ring.desc_avail++;
200 wq->last_completed_index = 0;
201 *((uint32_t *)wq->cqmsg_rz->addr) = 0;
203 iowrite32(0, &wq->ctrl->fetch_index);
204 iowrite32(0, &wq->ctrl->posted_index);
205 iowrite32(0, &wq->ctrl->error_status);
207 vnic_dev_clear_desc_ring(&wq->ring);