Imported Upstream version 16.07.2
[deb_dpdk.git] / drivers / net / enic / base / vnic_rq.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_rq.h"
37
38 void vnic_rq_free(struct vnic_rq *rq)
39 {
40         struct vnic_dev *vdev;
41
42         vdev = rq->vdev;
43
44         vnic_dev_free_desc_ring(vdev, &rq->ring);
45
46         rq->ctrl = NULL;
47 }
48
49 int vnic_rq_alloc(struct vnic_dev *vdev, struct vnic_rq *rq, unsigned int index,
50         unsigned int desc_count, unsigned int desc_size)
51 {
52         int rc;
53         char res_name[NAME_MAX];
54         static int instance;
55
56         rq->index = index;
57         rq->vdev = vdev;
58
59         rq->ctrl = vnic_dev_get_res(vdev, RES_TYPE_RQ, index);
60         if (!rq->ctrl) {
61                 pr_err("Failed to hook RQ[%d] resource\n", index);
62                 return -EINVAL;
63         }
64
65         vnic_rq_disable(rq);
66
67         snprintf(res_name, sizeof(res_name), "%d-rq-%d", instance++, index);
68         rc = vnic_dev_alloc_desc_ring(vdev, &rq->ring, desc_count, desc_size,
69                 rq->socket_id, res_name);
70         return rc;
71 }
72
73 void vnic_rq_init_start(struct vnic_rq *rq, unsigned int cq_index,
74         unsigned int fetch_index, unsigned int posted_index,
75         unsigned int error_interrupt_enable,
76         unsigned int error_interrupt_offset)
77 {
78         u64 paddr;
79         unsigned int count = rq->ring.desc_count;
80
81         paddr = (u64)rq->ring.base_addr | VNIC_PADDR_TARGET;
82         writeq(paddr, &rq->ctrl->ring_base);
83         iowrite32(count, &rq->ctrl->ring_size);
84         iowrite32(cq_index, &rq->ctrl->cq_index);
85         iowrite32(error_interrupt_enable, &rq->ctrl->error_interrupt_enable);
86         iowrite32(error_interrupt_offset, &rq->ctrl->error_interrupt_offset);
87         iowrite32(0, &rq->ctrl->error_status);
88         iowrite32(fetch_index, &rq->ctrl->fetch_index);
89         iowrite32(posted_index, &rq->ctrl->posted_index);
90         if (rq->data_queue_enable)
91                 iowrite32(((1 << 10) | rq->data_queue_idx),
92                           &rq->ctrl->data_ring);
93         else
94                 iowrite32(0, &rq->ctrl->data_ring);
95 }
96
97 void vnic_rq_init(struct vnic_rq *rq, unsigned int cq_index,
98         unsigned int error_interrupt_enable,
99         unsigned int error_interrupt_offset)
100 {
101         u32 fetch_index = 0;
102
103         /* Use current fetch_index as the ring starting point */
104         fetch_index = ioread32(&rq->ctrl->fetch_index);
105
106         if (fetch_index == 0xFFFFFFFF) { /* check for hardware gone  */
107                 /* Hardware surprise removal: reset fetch_index */
108                 fetch_index = 0;
109         }
110
111         vnic_rq_init_start(rq, cq_index,
112                 fetch_index, fetch_index,
113                 error_interrupt_enable,
114                 error_interrupt_offset);
115         rq->rxst_idx = 0;
116         rq->tot_pkts = 0;
117         rq->pkt_first_seg = NULL;
118         rq->pkt_last_seg = NULL;
119 }
120
121 void vnic_rq_error_out(struct vnic_rq *rq, unsigned int error)
122 {
123         iowrite32(error, &rq->ctrl->error_status);
124 }
125
126 unsigned int vnic_rq_error_status(struct vnic_rq *rq)
127 {
128         return ioread32(&rq->ctrl->error_status);
129 }
130
131 void vnic_rq_enable(struct vnic_rq *rq)
132 {
133         iowrite32(1, &rq->ctrl->enable);
134 }
135
136 int vnic_rq_disable(struct vnic_rq *rq)
137 {
138         unsigned int wait;
139
140         iowrite32(0, &rq->ctrl->enable);
141
142         /* Wait for HW to ACK disable request */
143         for (wait = 0; wait < 1000; wait++) {
144                 if (!(ioread32(&rq->ctrl->running)))
145                         return 0;
146                 udelay(10);
147         }
148
149         pr_err("Failed to disable RQ[%d]\n", rq->index);
150
151         return -ETIMEDOUT;
152 }
153
154 void vnic_rq_clean(struct vnic_rq *rq,
155         void (*buf_clean)(struct rte_mbuf **buf))
156 {
157         struct rte_mbuf **buf;
158         u32 fetch_index, i;
159         unsigned int count = rq->ring.desc_count;
160
161         buf = &rq->mbuf_ring[0];
162
163         for (i = 0; i < count; i++) {
164                 (*buf_clean)(buf);
165                 buf++;
166         }
167         rq->ring.desc_avail = count - 1;
168         rq->rx_nb_hold = 0;
169
170         /* Use current fetch_index as the ring starting point */
171         fetch_index = ioread32(&rq->ctrl->fetch_index);
172
173         if (fetch_index == 0xFFFFFFFF) { /* check for hardware gone  */
174                 /* Hardware surprise removal: reset fetch_index */
175                 fetch_index = 0;
176         }
177
178         iowrite32(fetch_index, &rq->ctrl->posted_index);
179
180         vnic_dev_clear_desc_ring(&rq->ring);
181 }