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 struct vnic_wq_buf *buf;
63 unsigned int i, j, count = wq->ring.desc_count;
64 unsigned int blks = VNIC_WQ_BUF_BLKS_NEEDED(count);
66 for (i = 0; i < blks; i++) {
67 wq->bufs[i] = kzalloc(VNIC_WQ_BUF_BLK_SZ(count), GFP_ATOMIC);
72 for (i = 0; i < blks; i++) {
74 for (j = 0; j < VNIC_WQ_BUF_BLK_ENTRIES(count); j++) {
75 buf->index = i * VNIC_WQ_BUF_BLK_ENTRIES(count) + j;
76 buf->desc = (u8 *)wq->ring.descs +
77 wq->ring.desc_size * buf->index;
78 if (buf->index + 1 == count) {
79 buf->next = wq->bufs[0];
81 } else if (j + 1 == VNIC_WQ_BUF_BLK_ENTRIES(count)) {
82 buf->next = wq->bufs[i + 1];
90 wq->to_use = wq->to_clean = wq->bufs[0];
95 void vnic_wq_free(struct vnic_wq *wq)
97 struct vnic_dev *vdev;
102 vnic_dev_free_desc_ring(vdev, &wq->ring);
104 for (i = 0; i < VNIC_WQ_BUF_BLKS_MAX; i++) {
114 int vnic_wq_mem_size(struct vnic_wq *wq, unsigned int desc_count,
115 unsigned int desc_size)
119 mem_size += vnic_dev_desc_ring_size(&wq->ring, desc_count, desc_size);
121 mem_size += VNIC_WQ_BUF_BLKS_NEEDED(wq->ring.desc_count) *
122 VNIC_WQ_BUF_BLK_SZ(wq->ring.desc_count);
128 int vnic_wq_alloc(struct vnic_dev *vdev, struct vnic_wq *wq, unsigned int index,
129 unsigned int desc_count, unsigned int desc_size)
136 err = vnic_wq_get_ctrl(vdev, wq, index, RES_TYPE_WQ);
138 pr_err("Failed to hook WQ[%d] resource, err %d\n", index, err);
144 err = vnic_wq_alloc_ring(vdev, wq, desc_count, desc_size);
148 err = vnic_wq_alloc_bufs(wq);
157 void vnic_wq_init_start(struct vnic_wq *wq, unsigned int cq_index,
158 unsigned int fetch_index, unsigned int posted_index,
159 unsigned int error_interrupt_enable,
160 unsigned int error_interrupt_offset)
163 unsigned int count = wq->ring.desc_count;
165 paddr = (u64)wq->ring.base_addr | VNIC_PADDR_TARGET;
166 writeq(paddr, &wq->ctrl->ring_base);
167 iowrite32(count, &wq->ctrl->ring_size);
168 iowrite32(fetch_index, &wq->ctrl->fetch_index);
169 iowrite32(posted_index, &wq->ctrl->posted_index);
170 iowrite32(cq_index, &wq->ctrl->cq_index);
171 iowrite32(error_interrupt_enable, &wq->ctrl->error_interrupt_enable);
172 iowrite32(error_interrupt_offset, &wq->ctrl->error_interrupt_offset);
173 iowrite32(0, &wq->ctrl->error_status);
175 wq->to_use = wq->to_clean =
176 &wq->bufs[fetch_index / VNIC_WQ_BUF_BLK_ENTRIES(count)]
177 [fetch_index % VNIC_WQ_BUF_BLK_ENTRIES(count)];
180 void vnic_wq_init(struct vnic_wq *wq, unsigned int cq_index,
181 unsigned int error_interrupt_enable,
182 unsigned int error_interrupt_offset)
184 vnic_wq_init_start(wq, cq_index, 0, 0,
185 error_interrupt_enable,
186 error_interrupt_offset);
189 void vnic_wq_error_out(struct vnic_wq *wq, unsigned int error)
191 iowrite32(error, &wq->ctrl->error_status);
194 unsigned int vnic_wq_error_status(struct vnic_wq *wq)
196 return ioread32(&wq->ctrl->error_status);
199 void vnic_wq_enable(struct vnic_wq *wq)
201 iowrite32(1, &wq->ctrl->enable);
204 int vnic_wq_disable(struct vnic_wq *wq)
208 iowrite32(0, &wq->ctrl->enable);
210 /* Wait for HW to ACK disable request */
211 for (wait = 0; wait < 1000; wait++) {
212 if (!(ioread32(&wq->ctrl->running)))
217 pr_err("Failed to disable WQ[%d]\n", wq->index);
222 void vnic_wq_clean(struct vnic_wq *wq,
223 void (*buf_clean)(struct vnic_wq *wq, struct vnic_wq_buf *buf))
225 struct vnic_wq_buf *buf;
229 while (vnic_wq_desc_used(wq) > 0) {
231 (*buf_clean)(wq, buf);
233 buf = wq->to_clean = buf->next;
234 wq->ring.desc_avail++;
237 wq->to_use = wq->to_clean = wq->bufs[0];
239 iowrite32(0, &wq->ctrl->fetch_index);
240 iowrite32(0, &wq->ctrl->posted_index);
241 iowrite32(0, &wq->ctrl->error_status);
243 vnic_dev_clear_desc_ring(&wq->ring);