net/mlx5: Use order-0 allocations for all WQ types
[linux-2.6-block.git] / drivers / net / ethernet / mellanox / mlx5 / core / wq.h
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 #ifndef __MLX5_WQ_H__
34 #define __MLX5_WQ_H__
35
36 #include <linux/mlx5/mlx5_ifc.h>
37 #include <linux/mlx5/cq.h>
38 #include <linux/mlx5/qp.h>
39
40 struct mlx5_wq_param {
41         int             buf_numa_node;
42         int             db_numa_node;
43 };
44
45 struct mlx5_wq_ctrl {
46         struct mlx5_core_dev    *mdev;
47         struct mlx5_frag_buf    buf;
48         struct mlx5_db          db;
49 };
50
51 struct mlx5_wq_cyc {
52         struct mlx5_frag_buf_ctrl fbc;
53         __be32                  *db;
54 };
55
56 struct mlx5_wq_qp {
57         struct mlx5_wq_cyc      rq;
58         struct mlx5_wq_cyc      sq;
59 };
60
61 struct mlx5_cqwq {
62         struct mlx5_frag_buf_ctrl fbc;
63         __be32                    *db;
64         u32                       cc; /* consumer counter */
65 };
66
67 struct mlx5_wq_ll {
68         struct mlx5_frag_buf_ctrl fbc;
69         __be32                  *db;
70         __be16                  *tail_next;
71         u16                     head;
72         u16                     wqe_ctr;
73         u16                     cur_sz;
74 };
75
76 int mlx5_wq_cyc_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
77                        void *wqc, struct mlx5_wq_cyc *wq,
78                        struct mlx5_wq_ctrl *wq_ctrl);
79 u32 mlx5_wq_cyc_get_size(struct mlx5_wq_cyc *wq);
80 u32 mlx5_wq_cyc_get_frag_size(struct mlx5_wq_cyc *wq);
81
82 int mlx5_wq_qp_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
83                       void *qpc, struct mlx5_wq_qp *wq,
84                       struct mlx5_wq_ctrl *wq_ctrl);
85
86 int mlx5_cqwq_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
87                      void *cqc, struct mlx5_cqwq *wq,
88                      struct mlx5_wq_ctrl *wq_ctrl);
89 u32 mlx5_cqwq_get_size(struct mlx5_cqwq *wq);
90
91 int mlx5_wq_ll_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
92                       void *wqc, struct mlx5_wq_ll *wq,
93                       struct mlx5_wq_ctrl *wq_ctrl);
94 u32 mlx5_wq_ll_get_size(struct mlx5_wq_ll *wq);
95
96 void mlx5_wq_destroy(struct mlx5_wq_ctrl *wq_ctrl);
97
98 static inline u16 mlx5_wq_cyc_ctr2ix(struct mlx5_wq_cyc *wq, u16 ctr)
99 {
100         return ctr & wq->fbc.sz_m1;
101 }
102
103 static inline u16 mlx5_wq_cyc_ctr2fragix(struct mlx5_wq_cyc *wq, u16 ctr)
104 {
105         return ctr & wq->fbc.frag_sz_m1;
106 }
107
108 static inline void *mlx5_wq_cyc_get_wqe(struct mlx5_wq_cyc *wq, u16 ix)
109 {
110         return mlx5_frag_buf_get_wqe(&wq->fbc, ix);
111 }
112
113 static inline int mlx5_wq_cyc_cc_bigger(u16 cc1, u16 cc2)
114 {
115         int equal   = (cc1 == cc2);
116         int smaller = 0x8000 & (cc1 - cc2);
117
118         return !equal && !smaller;
119 }
120
121 static inline u32 mlx5_cqwq_ctr2ix(struct mlx5_cqwq *wq, u32 ctr)
122 {
123         return ctr & wq->fbc.sz_m1;
124 }
125
126 static inline u32 mlx5_cqwq_get_ci(struct mlx5_cqwq *wq)
127 {
128         return mlx5_cqwq_ctr2ix(wq, wq->cc);
129 }
130
131 static inline void *mlx5_cqwq_get_wqe(struct mlx5_cqwq *wq, u32 ix)
132 {
133         return mlx5_frag_buf_get_wqe(&wq->fbc, ix);
134 }
135
136 static inline u32 mlx5_cqwq_get_ctr_wrap_cnt(struct mlx5_cqwq *wq, u32 ctr)
137 {
138         return ctr >> wq->fbc.log_sz;
139 }
140
141 static inline u32 mlx5_cqwq_get_wrap_cnt(struct mlx5_cqwq *wq)
142 {
143         return mlx5_cqwq_get_ctr_wrap_cnt(wq, wq->cc);
144 }
145
146 static inline void mlx5_cqwq_pop(struct mlx5_cqwq *wq)
147 {
148         wq->cc++;
149 }
150
151 static inline void mlx5_cqwq_update_db_record(struct mlx5_cqwq *wq)
152 {
153         *wq->db = cpu_to_be32(wq->cc & 0xffffff);
154 }
155
156 static inline struct mlx5_cqe64 *mlx5_cqwq_get_cqe(struct mlx5_cqwq *wq)
157 {
158         u32 ci = mlx5_cqwq_get_ci(wq);
159         struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(wq, ci);
160         u8 cqe_ownership_bit = cqe->op_own & MLX5_CQE_OWNER_MASK;
161         u8 sw_ownership_val = mlx5_cqwq_get_wrap_cnt(wq) & 1;
162
163         if (cqe_ownership_bit != sw_ownership_val)
164                 return NULL;
165
166         /* ensure cqe content is read after cqe ownership bit */
167         dma_rmb();
168
169         return cqe;
170 }
171
172 static inline int mlx5_wq_ll_is_full(struct mlx5_wq_ll *wq)
173 {
174         return wq->cur_sz == wq->fbc.sz_m1;
175 }
176
177 static inline int mlx5_wq_ll_is_empty(struct mlx5_wq_ll *wq)
178 {
179         return !wq->cur_sz;
180 }
181
182 static inline u16 mlx5_wq_ll_ctr2ix(struct mlx5_wq_ll *wq, u16 ctr)
183 {
184         return ctr & wq->fbc.sz_m1;
185 }
186
187 static inline void *mlx5_wq_ll_get_wqe(struct mlx5_wq_ll *wq, u16 ix)
188 {
189         return mlx5_frag_buf_get_wqe(&wq->fbc, ix);
190 }
191
192 static inline void mlx5_wq_ll_push(struct mlx5_wq_ll *wq, u16 head_next)
193 {
194         wq->head = head_next;
195         wq->wqe_ctr++;
196         wq->cur_sz++;
197 }
198
199 static inline void mlx5_wq_ll_pop(struct mlx5_wq_ll *wq, __be16 ix,
200                                   __be16 *next_tail_next)
201 {
202         *wq->tail_next = ix;
203         wq->tail_next = next_tail_next;
204         wq->cur_sz--;
205 }
206
207 static inline void mlx5_wq_ll_update_db_record(struct mlx5_wq_ll *wq)
208 {
209         *wq->db = cpu_to_be32(wq->wqe_ctr);
210 }
211
212 #endif /* __MLX5_WQ_H__ */