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.
34 #ident "$Id: vnic_wq.c 183023 2014-07-22 23:47:25Z xuywang $"
40 int vnic_wq_get_ctrl(struct vnic_dev *vdev, struct vnic_wq *wq,
41 unsigned int index, enum vnic_res_type res_type)
43 wq->ctrl = vnic_dev_get_res(vdev, res_type, index);
50 int vnic_wq_alloc_ring(struct vnic_dev *vdev, struct vnic_wq *wq,
51 unsigned int desc_count, unsigned int desc_size)
53 char res_name[NAME_MAX];
56 snprintf(res_name, sizeof(res_name), "%d-wq-%d", instance++, wq->index);
57 return vnic_dev_alloc_desc_ring(vdev, &wq->ring, desc_count, desc_size,
58 wq->socket_id, res_name);
61 static int vnic_wq_alloc_bufs(struct vnic_wq *wq)
63 struct vnic_wq_buf *buf;
64 unsigned int i, j, count = wq->ring.desc_count;
65 unsigned int blks = VNIC_WQ_BUF_BLKS_NEEDED(count);
67 for (i = 0; i < blks; i++) {
68 wq->bufs[i] = kzalloc(VNIC_WQ_BUF_BLK_SZ(count), GFP_ATOMIC);
73 for (i = 0; i < blks; i++) {
75 for (j = 0; j < VNIC_WQ_BUF_BLK_ENTRIES(count); j++) {
76 buf->index = i * VNIC_WQ_BUF_BLK_ENTRIES(count) + j;
77 buf->desc = (u8 *)wq->ring.descs +
78 wq->ring.desc_size * buf->index;
79 if (buf->index + 1 == count) {
80 buf->next = wq->bufs[0];
82 } else if (j + 1 == VNIC_WQ_BUF_BLK_ENTRIES(count)) {
83 buf->next = wq->bufs[i + 1];
91 wq->to_use = wq->to_clean = wq->bufs[0];
96 void vnic_wq_free(struct vnic_wq *wq)
98 struct vnic_dev *vdev;
103 vnic_dev_free_desc_ring(vdev, &wq->ring);
105 for (i = 0; i < VNIC_WQ_BUF_BLKS_MAX; i++) {
115 int vnic_wq_mem_size(struct vnic_wq *wq, unsigned int desc_count,
116 unsigned int desc_size)
120 mem_size += vnic_dev_desc_ring_size(&wq->ring, desc_count, desc_size);
122 mem_size += VNIC_WQ_BUF_BLKS_NEEDED(wq->ring.desc_count) *
123 VNIC_WQ_BUF_BLK_SZ(wq->ring.desc_count);
129 int vnic_wq_alloc(struct vnic_dev *vdev, struct vnic_wq *wq, unsigned int index,
130 unsigned int desc_count, unsigned int desc_size)
137 err = vnic_wq_get_ctrl(vdev, wq, index, RES_TYPE_WQ);
139 pr_err("Failed to hook WQ[%d] resource, err %d\n", index, err);
145 err = vnic_wq_alloc_ring(vdev, wq, desc_count, desc_size);
149 err = vnic_wq_alloc_bufs(wq);
158 void vnic_wq_init_start(struct vnic_wq *wq, unsigned int cq_index,
159 unsigned int fetch_index, unsigned int posted_index,
160 unsigned int error_interrupt_enable,
161 unsigned int error_interrupt_offset)
164 unsigned int count = wq->ring.desc_count;
166 paddr = (u64)wq->ring.base_addr | VNIC_PADDR_TARGET;
167 writeq(paddr, &wq->ctrl->ring_base);
168 iowrite32(count, &wq->ctrl->ring_size);
169 iowrite32(fetch_index, &wq->ctrl->fetch_index);
170 iowrite32(posted_index, &wq->ctrl->posted_index);
171 iowrite32(cq_index, &wq->ctrl->cq_index);
172 iowrite32(error_interrupt_enable, &wq->ctrl->error_interrupt_enable);
173 iowrite32(error_interrupt_offset, &wq->ctrl->error_interrupt_offset);
174 iowrite32(0, &wq->ctrl->error_status);
176 wq->to_use = wq->to_clean =
177 &wq->bufs[fetch_index / VNIC_WQ_BUF_BLK_ENTRIES(count)]
178 [fetch_index % VNIC_WQ_BUF_BLK_ENTRIES(count)];
181 void vnic_wq_init(struct vnic_wq *wq, unsigned int cq_index,
182 unsigned int error_interrupt_enable,
183 unsigned int error_interrupt_offset)
185 vnic_wq_init_start(wq, cq_index, 0, 0,
186 error_interrupt_enable,
187 error_interrupt_offset);
190 void vnic_wq_error_out(struct vnic_wq *wq, unsigned int error)
192 iowrite32(error, &wq->ctrl->error_status);
195 unsigned int vnic_wq_error_status(struct vnic_wq *wq)
197 return ioread32(&wq->ctrl->error_status);
200 void vnic_wq_enable(struct vnic_wq *wq)
202 iowrite32(1, &wq->ctrl->enable);
205 int vnic_wq_disable(struct vnic_wq *wq)
209 iowrite32(0, &wq->ctrl->enable);
211 /* Wait for HW to ACK disable request */
212 for (wait = 0; wait < 1000; wait++) {
213 if (!(ioread32(&wq->ctrl->running)))
218 pr_err("Failed to disable WQ[%d]\n", wq->index);
223 void vnic_wq_clean(struct vnic_wq *wq,
224 void (*buf_clean)(struct vnic_wq *wq, struct vnic_wq_buf *buf))
226 struct vnic_wq_buf *buf;
230 while (vnic_wq_desc_used(wq) > 0) {
232 (*buf_clean)(wq, buf);
234 buf = wq->to_clean = buf->next;
235 wq->ring.desc_avail++;
238 wq->to_use = wq->to_clean = wq->bufs[0];
240 iowrite32(0, &wq->ctrl->fetch_index);
241 iowrite32(0, &wq->ctrl->posted_index);
242 iowrite32(0, &wq->ctrl->error_status);
244 vnic_dev_clear_desc_ring(&wq->ring);