GNU Linux-libre 4.14.266-gnu1
[releases.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->sz_m1 + 1;
40 }
41
42 u32 mlx5_cqwq_get_size(struct mlx5_cqwq *wq)
43 {
44         return wq->sz_m1 + 1;
45 }
46
47 u32 mlx5_wq_ll_get_size(struct mlx5_wq_ll *wq)
48 {
49         return (u32)wq->sz_m1 + 1;
50 }
51
52 static u32 mlx5_wq_cyc_get_byte_size(struct mlx5_wq_cyc *wq)
53 {
54         return mlx5_wq_cyc_get_size(wq) << wq->log_stride;
55 }
56
57 static u32 mlx5_wq_qp_get_byte_size(struct mlx5_wq_qp *wq)
58 {
59         return mlx5_wq_cyc_get_byte_size(&wq->rq) +
60                mlx5_wq_cyc_get_byte_size(&wq->sq);
61 }
62
63 static u32 mlx5_cqwq_get_byte_size(struct mlx5_cqwq *wq)
64 {
65         return mlx5_cqwq_get_size(wq) << wq->log_stride;
66 }
67
68 static u32 mlx5_wq_ll_get_byte_size(struct mlx5_wq_ll *wq)
69 {
70         return mlx5_wq_ll_get_size(wq) << wq->log_stride;
71 }
72
73 int mlx5_wq_cyc_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
74                        void *wqc, struct mlx5_wq_cyc *wq,
75                        struct mlx5_wq_ctrl *wq_ctrl)
76 {
77         int err;
78
79         wq->log_stride = MLX5_GET(wq, wqc, log_wq_stride);
80         wq->sz_m1 = (1 << MLX5_GET(wq, wqc, log_wq_sz)) - 1;
81
82         err = mlx5_db_alloc_node(mdev, &wq_ctrl->db, param->db_numa_node);
83         if (err) {
84                 mlx5_core_warn(mdev, "mlx5_db_alloc_node() failed, %d\n", err);
85                 return err;
86         }
87
88         err = mlx5_buf_alloc_node(mdev, mlx5_wq_cyc_get_byte_size(wq),
89                                   &wq_ctrl->buf, param->buf_numa_node);
90         if (err) {
91                 mlx5_core_warn(mdev, "mlx5_buf_alloc_node() failed, %d\n", err);
92                 goto err_db_free;
93         }
94
95         wq->buf = wq_ctrl->buf.direct.buf;
96         wq->db  = wq_ctrl->db.db;
97
98         wq_ctrl->mdev = mdev;
99
100         return 0;
101
102 err_db_free:
103         mlx5_db_free(mdev, &wq_ctrl->db);
104
105         return err;
106 }
107
108 int mlx5_wq_qp_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
109                       void *qpc, struct mlx5_wq_qp *wq,
110                       struct mlx5_wq_ctrl *wq_ctrl)
111 {
112         int err;
113
114         wq->rq.log_stride = MLX5_GET(qpc, qpc, log_rq_stride) + 4;
115         wq->rq.sz_m1 = (1 << MLX5_GET(qpc, qpc, log_rq_size)) - 1;
116
117         wq->sq.log_stride = ilog2(MLX5_SEND_WQE_BB);
118         wq->sq.sz_m1 = (1 << MLX5_GET(qpc, qpc, log_sq_size)) - 1;
119
120         err = mlx5_db_alloc_node(mdev, &wq_ctrl->db, param->db_numa_node);
121         if (err) {
122                 mlx5_core_warn(mdev, "mlx5_db_alloc_node() failed, %d\n", err);
123                 return err;
124         }
125
126         err = mlx5_buf_alloc_node(mdev, mlx5_wq_qp_get_byte_size(wq),
127                                   &wq_ctrl->buf, param->buf_numa_node);
128         if (err) {
129                 mlx5_core_warn(mdev, "mlx5_buf_alloc_node() failed, %d\n", err);
130                 goto err_db_free;
131         }
132
133         wq->rq.buf = wq_ctrl->buf.direct.buf;
134         wq->sq.buf = wq->rq.buf + mlx5_wq_cyc_get_byte_size(&wq->rq);
135         wq->rq.db  = &wq_ctrl->db.db[MLX5_RCV_DBR];
136         wq->sq.db  = &wq_ctrl->db.db[MLX5_SND_DBR];
137
138         wq_ctrl->mdev = mdev;
139
140         return 0;
141
142 err_db_free:
143         mlx5_db_free(mdev, &wq_ctrl->db);
144
145         return err;
146 }
147
148 int mlx5_cqwq_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
149                      void *cqc, struct mlx5_cqwq *wq,
150                      struct mlx5_frag_wq_ctrl *wq_ctrl)
151 {
152         int err;
153
154         wq->log_stride  = 6 + MLX5_GET(cqc, cqc, cqe_sz);
155         wq->log_sz      = MLX5_GET(cqc, cqc, log_cq_size);
156         wq->sz_m1       = (1 << wq->log_sz) - 1;
157         wq->log_frag_strides = PAGE_SHIFT - wq->log_stride;
158         wq->frag_sz_m1  = (1 << wq->log_frag_strides) - 1;
159
160         err = mlx5_db_alloc_node(mdev, &wq_ctrl->db, param->db_numa_node);
161         if (err) {
162                 mlx5_core_warn(mdev, "mlx5_db_alloc_node() failed, %d\n", err);
163                 return err;
164         }
165
166         err = mlx5_frag_buf_alloc_node(mdev, mlx5_cqwq_get_byte_size(wq),
167                                        &wq_ctrl->frag_buf,
168                                        param->buf_numa_node);
169         if (err) {
170                 mlx5_core_warn(mdev, "mlx5_frag_buf_alloc_node() failed, %d\n",
171                                err);
172                 goto err_db_free;
173         }
174
175         wq->frag_buf = wq_ctrl->frag_buf;
176         wq->db  = wq_ctrl->db.db;
177
178         wq_ctrl->mdev = mdev;
179
180         return 0;
181
182 err_db_free:
183         mlx5_db_free(mdev, &wq_ctrl->db);
184
185         return err;
186 }
187
188 int mlx5_wq_ll_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
189                       void *wqc, struct mlx5_wq_ll *wq,
190                       struct mlx5_wq_ctrl *wq_ctrl)
191 {
192         struct mlx5_wqe_srq_next_seg *next_seg;
193         int err;
194         int i;
195
196         wq->log_stride = MLX5_GET(wq, wqc, log_wq_stride);
197         wq->sz_m1 = (1 << MLX5_GET(wq, wqc, log_wq_sz)) - 1;
198
199         err = mlx5_db_alloc_node(mdev, &wq_ctrl->db, param->db_numa_node);
200         if (err) {
201                 mlx5_core_warn(mdev, "mlx5_db_alloc_node() failed, %d\n", err);
202                 return err;
203         }
204
205         err = mlx5_buf_alloc_node(mdev, mlx5_wq_ll_get_byte_size(wq),
206                                   &wq_ctrl->buf, param->buf_numa_node);
207         if (err) {
208                 mlx5_core_warn(mdev, "mlx5_buf_alloc_node() failed, %d\n", err);
209                 goto err_db_free;
210         }
211
212         wq->buf = wq_ctrl->buf.direct.buf;
213         wq->db  = wq_ctrl->db.db;
214
215         for (i = 0; i < wq->sz_m1; i++) {
216                 next_seg = mlx5_wq_ll_get_wqe(wq, i);
217                 next_seg->next_wqe_index = cpu_to_be16(i + 1);
218         }
219         next_seg = mlx5_wq_ll_get_wqe(wq, i);
220         wq->tail_next = &next_seg->next_wqe_index;
221
222         wq_ctrl->mdev = mdev;
223
224         return 0;
225
226 err_db_free:
227         mlx5_db_free(mdev, &wq_ctrl->db);
228
229         return err;
230 }
231
232 void mlx5_wq_destroy(struct mlx5_wq_ctrl *wq_ctrl)
233 {
234         mlx5_buf_free(wq_ctrl->mdev, &wq_ctrl->buf);
235         mlx5_db_free(wq_ctrl->mdev, &wq_ctrl->db);
236 }
237
238 void mlx5_cqwq_destroy(struct mlx5_frag_wq_ctrl *wq_ctrl)
239 {
240         mlx5_frag_buf_free(wq_ctrl->mdev, &wq_ctrl->frag_buf);
241         mlx5_db_free(wq_ctrl->mdev, &wq_ctrl->db);
242 }