e1000: move to drivers/net/
[dpdk.git] / lib / librte_pmd_enic / vnic / vnic_wq.c
1 /*
2  * Copyright 2008-2010 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * Copyright (c) 2014, Cisco Systems, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
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
18  * distribution.
19  *
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.
32  *
33  */
34 #ident "$Id: vnic_wq.c 183023 2014-07-22 23:47:25Z xuywang $"
35
36 #include "vnic_dev.h"
37 #include "vnic_wq.h"
38
39 static inline
40 int vnic_wq_get_ctrl(struct vnic_dev *vdev, struct vnic_wq *wq,
41                                 unsigned int index, enum vnic_res_type res_type)
42 {
43         wq->ctrl = vnic_dev_get_res(vdev, res_type, index);
44         if (!wq->ctrl)
45                 return -EINVAL;
46         return 0;
47 }
48
49 static inline
50 int vnic_wq_alloc_ring(struct vnic_dev *vdev, struct vnic_wq *wq,
51                                 unsigned int desc_count, unsigned int desc_size)
52 {
53         char res_name[NAME_MAX];
54         static int instance;
55
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);
59 }
60
61 static int vnic_wq_alloc_bufs(struct vnic_wq *wq)
62 {
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);
66
67         for (i = 0; i < blks; i++) {
68                 wq->bufs[i] = kzalloc(VNIC_WQ_BUF_BLK_SZ(count), GFP_ATOMIC);
69                 if (!wq->bufs[i])
70                         return -ENOMEM;
71         }
72
73         for (i = 0; i < blks; i++) {
74                 buf = wq->bufs[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];
81                                 break;
82                         } else if (j + 1 == VNIC_WQ_BUF_BLK_ENTRIES(count)) {
83                                 buf->next = wq->bufs[i + 1];
84                         } else {
85                                 buf->next = buf + 1;
86                                 buf++;
87                         }
88                 }
89         }
90
91         wq->to_use = wq->to_clean = wq->bufs[0];
92
93         return 0;
94 }
95
96 void vnic_wq_free(struct vnic_wq *wq)
97 {
98         struct vnic_dev *vdev;
99         unsigned int i;
100
101         vdev = wq->vdev;
102
103         vnic_dev_free_desc_ring(vdev, &wq->ring);
104
105         for (i = 0; i < VNIC_WQ_BUF_BLKS_MAX; i++) {
106                 if (wq->bufs[i]) {
107                         kfree(wq->bufs[i]);
108                         wq->bufs[i] = NULL;
109                 }
110         }
111
112         wq->ctrl = NULL;
113 }
114
115 int vnic_wq_mem_size(struct vnic_wq *wq, unsigned int desc_count,
116         unsigned int desc_size)
117 {
118         int mem_size = 0;
119
120         mem_size += vnic_dev_desc_ring_size(&wq->ring, desc_count, desc_size);
121
122         mem_size += VNIC_WQ_BUF_BLKS_NEEDED(wq->ring.desc_count) *
123                 VNIC_WQ_BUF_BLK_SZ(wq->ring.desc_count);
124
125         return mem_size;
126 }
127
128
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)
131 {
132         int err;
133
134         wq->index = index;
135         wq->vdev = vdev;
136
137         err = vnic_wq_get_ctrl(vdev, wq, index, RES_TYPE_WQ);
138         if (err) {
139                 pr_err("Failed to hook WQ[%d] resource, err %d\n", index, err);
140                 return err;
141         }
142
143         vnic_wq_disable(wq);
144
145         err = vnic_wq_alloc_ring(vdev, wq, desc_count, desc_size);
146         if (err)
147                 return err;
148
149         err = vnic_wq_alloc_bufs(wq);
150         if (err) {
151                 vnic_wq_free(wq);
152                 return err;
153         }
154
155         return 0;
156 }
157
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)
162 {
163         u64 paddr;
164         unsigned int count = wq->ring.desc_count;
165
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);
175
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)];
179 }
180
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)
184 {
185         vnic_wq_init_start(wq, cq_index, 0, 0,
186                 error_interrupt_enable,
187                 error_interrupt_offset);
188 }
189
190 void vnic_wq_error_out(struct vnic_wq *wq, unsigned int error)
191 {
192         iowrite32(error, &wq->ctrl->error_status);
193 }
194
195 unsigned int vnic_wq_error_status(struct vnic_wq *wq)
196 {
197         return ioread32(&wq->ctrl->error_status);
198 }
199
200 void vnic_wq_enable(struct vnic_wq *wq)
201 {
202         iowrite32(1, &wq->ctrl->enable);
203 }
204
205 int vnic_wq_disable(struct vnic_wq *wq)
206 {
207         unsigned int wait;
208
209         iowrite32(0, &wq->ctrl->enable);
210
211         /* Wait for HW to ACK disable request */
212         for (wait = 0; wait < 1000; wait++) {
213                 if (!(ioread32(&wq->ctrl->running)))
214                         return 0;
215                 udelay(10);
216         }
217
218         pr_err("Failed to disable WQ[%d]\n", wq->index);
219
220         return -ETIMEDOUT;
221 }
222
223 void vnic_wq_clean(struct vnic_wq *wq,
224         void (*buf_clean)(struct vnic_wq *wq, struct vnic_wq_buf *buf))
225 {
226         struct vnic_wq_buf *buf;
227
228         buf = wq->to_clean;
229
230         while (vnic_wq_desc_used(wq) > 0) {
231
232                 (*buf_clean)(wq, buf);
233
234                 buf = wq->to_clean = buf->next;
235                 wq->ring.desc_avail++;
236         }
237
238         wq->to_use = wq->to_clean = wq->bufs[0];
239
240         iowrite32(0, &wq->ctrl->fetch_index);
241         iowrite32(0, &wq->ctrl->posted_index);
242         iowrite32(0, &wq->ctrl->error_status);
243
244         vnic_dev_clear_desc_ring(&wq->ring);
245 }