Imported Upstream version 16.04
[deb_dpdk.git] / drivers / net / enic / base / 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
35 #include "vnic_dev.h"
36 #include "vnic_wq.h"
37
38 static inline
39 int vnic_wq_get_ctrl(struct vnic_dev *vdev, struct vnic_wq *wq,
40                                 unsigned int index, enum vnic_res_type res_type)
41 {
42         wq->ctrl = vnic_dev_get_res(vdev, res_type, index);
43         if (!wq->ctrl)
44                 return -EINVAL;
45         return 0;
46 }
47
48 static inline
49 int vnic_wq_alloc_ring(struct vnic_dev *vdev, struct vnic_wq *wq,
50                                 unsigned int desc_count, unsigned int desc_size)
51 {
52         char res_name[NAME_MAX];
53         static int instance;
54
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);
58 }
59
60 static int vnic_wq_alloc_bufs(struct vnic_wq *wq)
61 {
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);
65
66         for (i = 0; i < blks; i++) {
67                 wq->bufs[i] = kzalloc(VNIC_WQ_BUF_BLK_SZ(count), GFP_ATOMIC);
68                 if (!wq->bufs[i])
69                         return -ENOMEM;
70         }
71
72         for (i = 0; i < blks; i++) {
73                 buf = wq->bufs[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];
80                                 break;
81                         } else if (j + 1 == VNIC_WQ_BUF_BLK_ENTRIES(count)) {
82                                 buf->next = wq->bufs[i + 1];
83                         } else {
84                                 buf->next = buf + 1;
85                                 buf++;
86                         }
87                 }
88         }
89
90         wq->to_use = wq->to_clean = wq->bufs[0];
91
92         return 0;
93 }
94
95 void vnic_wq_free(struct vnic_wq *wq)
96 {
97         struct vnic_dev *vdev;
98         unsigned int i;
99
100         vdev = wq->vdev;
101
102         vnic_dev_free_desc_ring(vdev, &wq->ring);
103
104         for (i = 0; i < VNIC_WQ_BUF_BLKS_MAX; i++) {
105                 if (wq->bufs[i]) {
106                         kfree(wq->bufs[i]);
107                         wq->bufs[i] = NULL;
108                 }
109         }
110
111         wq->ctrl = NULL;
112 }
113
114 int vnic_wq_mem_size(struct vnic_wq *wq, unsigned int desc_count,
115         unsigned int desc_size)
116 {
117         int mem_size = 0;
118
119         mem_size += vnic_dev_desc_ring_size(&wq->ring, desc_count, desc_size);
120
121         mem_size += VNIC_WQ_BUF_BLKS_NEEDED(wq->ring.desc_count) *
122                 VNIC_WQ_BUF_BLK_SZ(wq->ring.desc_count);
123
124         return mem_size;
125 }
126
127
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)
130 {
131         int err;
132
133         wq->index = index;
134         wq->vdev = vdev;
135
136         err = vnic_wq_get_ctrl(vdev, wq, index, RES_TYPE_WQ);
137         if (err) {
138                 pr_err("Failed to hook WQ[%d] resource, err %d\n", index, err);
139                 return err;
140         }
141
142         vnic_wq_disable(wq);
143
144         err = vnic_wq_alloc_ring(vdev, wq, desc_count, desc_size);
145         if (err)
146                 return err;
147
148         err = vnic_wq_alloc_bufs(wq);
149         if (err) {
150                 vnic_wq_free(wq);
151                 return err;
152         }
153
154         return 0;
155 }
156
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)
161 {
162         u64 paddr;
163         unsigned int count = wq->ring.desc_count;
164
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);
174
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)];
178 }
179
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)
183 {
184         vnic_wq_init_start(wq, cq_index, 0, 0,
185                 error_interrupt_enable,
186                 error_interrupt_offset);
187 }
188
189 void vnic_wq_error_out(struct vnic_wq *wq, unsigned int error)
190 {
191         iowrite32(error, &wq->ctrl->error_status);
192 }
193
194 unsigned int vnic_wq_error_status(struct vnic_wq *wq)
195 {
196         return ioread32(&wq->ctrl->error_status);
197 }
198
199 void vnic_wq_enable(struct vnic_wq *wq)
200 {
201         iowrite32(1, &wq->ctrl->enable);
202 }
203
204 int vnic_wq_disable(struct vnic_wq *wq)
205 {
206         unsigned int wait;
207
208         iowrite32(0, &wq->ctrl->enable);
209
210         /* Wait for HW to ACK disable request */
211         for (wait = 0; wait < 1000; wait++) {
212                 if (!(ioread32(&wq->ctrl->running)))
213                         return 0;
214                 udelay(10);
215         }
216
217         pr_err("Failed to disable WQ[%d]\n", wq->index);
218
219         return -ETIMEDOUT;
220 }
221
222 void vnic_wq_clean(struct vnic_wq *wq,
223         void (*buf_clean)(struct vnic_wq *wq, struct vnic_wq_buf *buf))
224 {
225         struct vnic_wq_buf *buf;
226
227         buf = wq->to_clean;
228
229         while (vnic_wq_desc_used(wq) > 0) {
230
231                 (*buf_clean)(wq, buf);
232
233                 buf = wq->to_clean = buf->next;
234                 wq->ring.desc_avail++;
235         }
236
237         wq->to_use = wq->to_clean = wq->bufs[0];
238
239         iowrite32(0, &wq->ctrl->fetch_index);
240         iowrite32(0, &wq->ctrl->posted_index);
241         iowrite32(0, &wq->ctrl->error_status);
242
243         vnic_dev_clear_desc_ring(&wq->ring);
244 }