Merge tag 'armsoc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[linux-2.6-block.git] / drivers / net / ethernet / mellanox / mlx5 / core / wq.c
1 /*
2  * Copyright (c) 2013-2015, Mellanox Technologies, Ltd.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/mlx5/driver.h>
34 #include "wq.h"
35 #include "mlx5_core.h"
36
37 u32 mlx5_wq_cyc_get_size(struct mlx5_wq_cyc *wq)
38 {
39         return (u32)wq->fbc.sz_m1 + 1;
40 }
41
42 u32 mlx5_wq_cyc_get_frag_size(struct mlx5_wq_cyc *wq)
43 {
44         return (u32)wq->fbc.frag_sz_m1 + 1;
45 }
46
47 u32 mlx5_cqwq_get_size(struct mlx5_cqwq *wq)
48 {
49         return wq->fbc.sz_m1 + 1;
50 }
51
52 u32 mlx5_wq_ll_get_size(struct mlx5_wq_ll *wq)
53 {
54         return (u32)wq->fbc.sz_m1 + 1;
55 }
56
57 static u32 mlx5_wq_cyc_get_byte_size(struct mlx5_wq_cyc *wq)
58 {
59         return mlx5_wq_cyc_get_size(wq) << wq->fbc.log_stride;
60 }
61
62 static u32 mlx5_wq_qp_get_byte_size(struct mlx5_wq_qp *wq)
63 {
64         return mlx5_wq_cyc_get_byte_size(&wq->rq) +
65                mlx5_wq_cyc_get_byte_size(&wq->sq);
66 }
67
68 static u32 mlx5_cqwq_get_byte_size(struct mlx5_cqwq *wq)
69 {
70         return mlx5_cqwq_get_size(wq) << wq->fbc.log_stride;
71 }
72
73 static u32 mlx5_wq_ll_get_byte_size(struct mlx5_wq_ll *wq)
74 {
75         return mlx5_wq_ll_get_size(wq) << wq->fbc.log_stride;
76 }
77
78 int mlx5_wq_cyc_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
79                        void *wqc, struct mlx5_wq_cyc *wq,
80                        struct mlx5_wq_ctrl *wq_ctrl)
81 {
82         struct mlx5_frag_buf_ctrl *fbc = &wq->fbc;
83         int err;
84
85         mlx5_fill_fbc(MLX5_GET(wq, wqc, log_wq_stride),
86                       MLX5_GET(wq, wqc, log_wq_sz),
87                       fbc);
88         wq->sz    = wq->fbc.sz_m1 + 1;
89
90         err = mlx5_db_alloc_node(mdev, &wq_ctrl->db, param->db_numa_node);
91         if (err) {
92                 mlx5_core_warn(mdev, "mlx5_db_alloc_node() failed, %d\n", err);
93                 return err;
94         }
95
96         err = mlx5_frag_buf_alloc_node(mdev, mlx5_wq_cyc_get_byte_size(wq),
97                                        &wq_ctrl->buf, param->buf_numa_node);
98         if (err) {
99                 mlx5_core_warn(mdev, "mlx5_frag_buf_alloc_node() failed, %d\n", err);
100                 goto err_db_free;
101         }
102
103         fbc->frag_buf = wq_ctrl->buf;
104         wq->db  = wq_ctrl->db.db;
105
106         wq_ctrl->mdev = mdev;
107
108         return 0;
109
110 err_db_free:
111         mlx5_db_free(mdev, &wq_ctrl->db);
112
113         return err;
114 }
115
116 static void mlx5_qp_set_frag_buf(struct mlx5_frag_buf *buf,
117                                  struct mlx5_wq_qp *qp)
118 {
119         struct mlx5_frag_buf_ctrl *sq_fbc;
120         struct mlx5_frag_buf *rqb, *sqb;
121
122         rqb  = &qp->rq.fbc.frag_buf;
123         *rqb = *buf;
124         rqb->size   = mlx5_wq_cyc_get_byte_size(&qp->rq);
125         rqb->npages = DIV_ROUND_UP(rqb->size, PAGE_SIZE);
126
127         sq_fbc = &qp->sq.fbc;
128         sqb    = &sq_fbc->frag_buf;
129         *sqb   = *buf;
130         sqb->size   = mlx5_wq_cyc_get_byte_size(&qp->sq);
131         sqb->npages = DIV_ROUND_UP(sqb->size, PAGE_SIZE);
132         sqb->frags += rqb->npages; /* first part is for the rq */
133         if (sq_fbc->strides_offset)
134                 sqb->frags--;
135 }
136
137 int mlx5_wq_qp_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
138                       void *qpc, struct mlx5_wq_qp *wq,
139                       struct mlx5_wq_ctrl *wq_ctrl)
140 {
141         u32 sq_strides_offset;
142         u32 rq_pg_remainder;
143         int err;
144
145         mlx5_fill_fbc(MLX5_GET(qpc, qpc, log_rq_stride) + 4,
146                       MLX5_GET(qpc, qpc, log_rq_size),
147                       &wq->rq.fbc);
148
149         rq_pg_remainder   = mlx5_wq_cyc_get_byte_size(&wq->rq) % PAGE_SIZE;
150         sq_strides_offset = rq_pg_remainder / MLX5_SEND_WQE_BB;
151
152         mlx5_fill_fbc_offset(ilog2(MLX5_SEND_WQE_BB),
153                              MLX5_GET(qpc, qpc, log_sq_size),
154                              sq_strides_offset,
155                              &wq->sq.fbc);
156
157         err = mlx5_db_alloc_node(mdev, &wq_ctrl->db, param->db_numa_node);
158         if (err) {
159                 mlx5_core_warn(mdev, "mlx5_db_alloc_node() failed, %d\n", err);
160                 return err;
161         }
162
163         err = mlx5_frag_buf_alloc_node(mdev, mlx5_wq_qp_get_byte_size(wq),
164                                        &wq_ctrl->buf, param->buf_numa_node);
165         if (err) {
166                 mlx5_core_warn(mdev, "mlx5_frag_buf_alloc_node() failed, %d\n", err);
167                 goto err_db_free;
168         }
169
170         mlx5_qp_set_frag_buf(&wq_ctrl->buf, wq);
171
172         wq->rq.db  = &wq_ctrl->db.db[MLX5_RCV_DBR];
173         wq->sq.db  = &wq_ctrl->db.db[MLX5_SND_DBR];
174
175         wq_ctrl->mdev = mdev;
176
177         return 0;
178
179 err_db_free:
180         mlx5_db_free(mdev, &wq_ctrl->db);
181
182         return err;
183 }
184
185 int mlx5_cqwq_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
186                      void *cqc, struct mlx5_cqwq *wq,
187                      struct mlx5_wq_ctrl *wq_ctrl)
188 {
189         int err;
190
191         mlx5_core_init_cq_frag_buf(&wq->fbc, cqc);
192
193         err = mlx5_db_alloc_node(mdev, &wq_ctrl->db, param->db_numa_node);
194         if (err) {
195                 mlx5_core_warn(mdev, "mlx5_db_alloc_node() failed, %d\n", err);
196                 return err;
197         }
198
199         err = mlx5_frag_buf_alloc_node(mdev, mlx5_cqwq_get_byte_size(wq),
200                                        &wq_ctrl->buf,
201                                        param->buf_numa_node);
202         if (err) {
203                 mlx5_core_warn(mdev, "mlx5_frag_buf_alloc_node() failed, %d\n",
204                                err);
205                 goto err_db_free;
206         }
207
208         wq->fbc.frag_buf = wq_ctrl->buf;
209         wq->db  = wq_ctrl->db.db;
210
211         wq_ctrl->mdev = mdev;
212
213         return 0;
214
215 err_db_free:
216         mlx5_db_free(mdev, &wq_ctrl->db);
217
218         return err;
219 }
220
221 int mlx5_wq_ll_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
222                       void *wqc, struct mlx5_wq_ll *wq,
223                       struct mlx5_wq_ctrl *wq_ctrl)
224 {
225         struct mlx5_frag_buf_ctrl *fbc = &wq->fbc;
226         struct mlx5_wqe_srq_next_seg *next_seg;
227         int err;
228         int i;
229
230         mlx5_fill_fbc(MLX5_GET(wq, wqc, log_wq_stride),
231                       MLX5_GET(wq, wqc, log_wq_sz),
232                       fbc);
233
234         err = mlx5_db_alloc_node(mdev, &wq_ctrl->db, param->db_numa_node);
235         if (err) {
236                 mlx5_core_warn(mdev, "mlx5_db_alloc_node() failed, %d\n", err);
237                 return err;
238         }
239
240         err = mlx5_frag_buf_alloc_node(mdev, mlx5_wq_ll_get_byte_size(wq),
241                                        &wq_ctrl->buf, param->buf_numa_node);
242         if (err) {
243                 mlx5_core_warn(mdev, "mlx5_frag_buf_alloc_node() failed, %d\n", err);
244                 goto err_db_free;
245         }
246
247         wq->fbc.frag_buf = wq_ctrl->buf;
248         wq->db  = wq_ctrl->db.db;
249
250         for (i = 0; i < fbc->sz_m1; i++) {
251                 next_seg = mlx5_wq_ll_get_wqe(wq, i);
252                 next_seg->next_wqe_index = cpu_to_be16(i + 1);
253         }
254         next_seg = mlx5_wq_ll_get_wqe(wq, i);
255         wq->tail_next = &next_seg->next_wqe_index;
256
257         wq_ctrl->mdev = mdev;
258
259         return 0;
260
261 err_db_free:
262         mlx5_db_free(mdev, &wq_ctrl->db);
263
264         return err;
265 }
266
267 void mlx5_wq_destroy(struct mlx5_wq_ctrl *wq_ctrl)
268 {
269         mlx5_frag_buf_free(wq_ctrl->mdev, &wq_ctrl->buf);
270         mlx5_db_free(wq_ctrl->mdev, &wq_ctrl->db);
271 }
272