7c4119c34a26ddf09a7513503db121fd9bdf380a
[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         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);
67         wq->head_idx = 0;
68         wq->tail_idx = 0;
69         if (wq->bufs == NULL)
70                 return -ENOMEM;
71         return 0;
72 }
73
74 void vnic_wq_free(struct vnic_wq *wq)
75 {
76         struct vnic_dev *vdev;
77
78         vdev = wq->vdev;
79
80         vnic_dev_free_desc_ring(vdev, &wq->ring);
81
82         rte_free(wq->bufs);
83         wq->ctrl = NULL;
84 }
85
86
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)
89 {
90         int err;
91
92         wq->index = index;
93         wq->vdev = vdev;
94
95         err = vnic_wq_get_ctrl(vdev, wq, index, RES_TYPE_WQ);
96         if (err) {
97                 pr_err("Failed to hook WQ[%d] resource, err %d\n", index, err);
98                 return err;
99         }
100
101         vnic_wq_disable(wq);
102
103         err = vnic_wq_alloc_ring(vdev, wq, desc_count, desc_size);
104         if (err)
105                 return err;
106
107         err = vnic_wq_alloc_bufs(wq);
108         if (err) {
109                 vnic_wq_free(wq);
110                 return err;
111         }
112
113         return 0;
114 }
115
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)
120 {
121         u64 paddr;
122         unsigned int count = wq->ring.desc_count;
123
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);
133
134         wq->head_idx = fetch_index;
135         wq->tail_idx = wq->head_idx;
136 }
137
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)
141 {
142         vnic_wq_init_start(wq, cq_index, 0, 0,
143                 error_interrupt_enable,
144                 error_interrupt_offset);
145         wq->last_completed_index = 0;
146 }
147
148 void vnic_wq_error_out(struct vnic_wq *wq, unsigned int error)
149 {
150         iowrite32(error, &wq->ctrl->error_status);
151 }
152
153 unsigned int vnic_wq_error_status(struct vnic_wq *wq)
154 {
155         return ioread32(&wq->ctrl->error_status);
156 }
157
158 void vnic_wq_enable(struct vnic_wq *wq)
159 {
160         iowrite32(1, &wq->ctrl->enable);
161 }
162
163 int vnic_wq_disable(struct vnic_wq *wq)
164 {
165         unsigned int wait;
166
167         iowrite32(0, &wq->ctrl->enable);
168
169         /* Wait for HW to ACK disable request */
170         for (wait = 0; wait < 1000; wait++) {
171                 if (!(ioread32(&wq->ctrl->running)))
172                         return 0;
173                 udelay(10);
174         }
175
176         pr_err("Failed to disable WQ[%d]\n", wq->index);
177
178         return -ETIMEDOUT;
179 }
180
181 void vnic_wq_clean(struct vnic_wq *wq,
182                    void (*buf_clean)(struct vnic_wq_buf *buf))
183 {
184         struct vnic_wq_buf *buf;
185         unsigned int  to_clean = wq->tail_idx;
186
187         buf = &wq->bufs[to_clean];
188
189         while (vnic_wq_desc_used(wq) > 0) {
190
191                 (*buf_clean)(buf);
192                 to_clean = buf_idx_incr(wq->ring.desc_count, to_clean);
193
194                 buf = &wq->bufs[to_clean];
195                 wq->ring.desc_avail++;
196         }
197
198         wq->head_idx = 0;
199         wq->tail_idx = 0;
200         wq->last_completed_index = 0;
201         *((uint32_t *)wq->cqmsg_rz->addr) = 0;
202
203         iowrite32(0, &wq->ctrl->fetch_index);
204         iowrite32(0, &wq->ctrl->posted_index);
205         iowrite32(0, &wq->ctrl->error_status);
206
207         vnic_dev_clear_desc_ring(&wq->ring);
208 }