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 
38 struct mlx5_wq_param {
39 	int		linear;
40 	int		buf_numa_node;
41 	int		db_numa_node;
42 };
43 
44 struct mlx5_wq_ctrl {
45 	struct mlx5_core_dev	*mdev;
46 	struct mlx5_buf		buf;
47 	struct mlx5_db		db;
48 };
49 
50 struct mlx5_wq_cyc {
51 	void			*buf;
52 	__be32			*db;
53 	u16			sz_m1;
54 	u8			log_stride;
55 };
56 
57 struct mlx5_cqwq {
58 	void			*buf;
59 	__be32			*db;
60 	u32			sz_m1;
61 	u32			cc; /* consumer counter */
62 	u8			log_sz;
63 	u8			log_stride;
64 };
65 
66 struct mlx5_wq_ll {
67 	void			*buf;
68 	__be32			*db;
69 	__be16			*tail_next;
70 	u16			sz_m1;
71 	u16			head;
72 	u16			wqe_ctr;
73 	u16			cur_sz;
74 	u8			log_stride;
75 };
76 
77 int mlx5_wq_cyc_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
78 		       void *wqc, struct mlx5_wq_cyc *wq,
79 		       struct mlx5_wq_ctrl *wq_ctrl);
80 u32 mlx5_wq_cyc_get_size(struct mlx5_wq_cyc *wq);
81 
82 int mlx5_cqwq_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
83 		     void *cqc, struct mlx5_cqwq *wq,
84 		     struct mlx5_wq_ctrl *wq_ctrl);
85 u32 mlx5_cqwq_get_size(struct mlx5_cqwq *wq);
86 
87 int mlx5_wq_ll_create(struct mlx5_core_dev *mdev, struct mlx5_wq_param *param,
88 		      void *wqc, struct mlx5_wq_ll *wq,
89 		      struct mlx5_wq_ctrl *wq_ctrl);
90 u32 mlx5_wq_ll_get_size(struct mlx5_wq_ll *wq);
91 
92 void mlx5_wq_destroy(struct mlx5_wq_ctrl *wq_ctrl);
93 
94 static inline u16 mlx5_wq_cyc_ctr2ix(struct mlx5_wq_cyc *wq, u16 ctr)
95 {
96 	return ctr & wq->sz_m1;
97 }
98 
99 static inline void *mlx5_wq_cyc_get_wqe(struct mlx5_wq_cyc *wq, u16 ix)
100 {
101 	return wq->buf + (ix << wq->log_stride);
102 }
103 
104 static inline int mlx5_wq_cyc_cc_bigger(u16 cc1, u16 cc2)
105 {
106 	int equal   = (cc1 == cc2);
107 	int smaller = 0x8000 & (cc1 - cc2);
108 
109 	return !equal && !smaller;
110 }
111 
112 static inline u32 mlx5_cqwq_get_ci(struct mlx5_cqwq *wq)
113 {
114 	return wq->cc & wq->sz_m1;
115 }
116 
117 static inline void *mlx5_cqwq_get_wqe(struct mlx5_cqwq *wq, u32 ix)
118 {
119 	return wq->buf + (ix << wq->log_stride);
120 }
121 
122 static inline u32 mlx5_cqwq_get_wrap_cnt(struct mlx5_cqwq *wq)
123 {
124 	return wq->cc >> wq->log_sz;
125 }
126 
127 static inline void mlx5_cqwq_pop(struct mlx5_cqwq *wq)
128 {
129 	wq->cc++;
130 }
131 
132 static inline void mlx5_cqwq_update_db_record(struct mlx5_cqwq *wq)
133 {
134 	*wq->db = cpu_to_be32(wq->cc & 0xffffff);
135 }
136 
137 static inline int mlx5_wq_ll_is_full(struct mlx5_wq_ll *wq)
138 {
139 	return wq->cur_sz == wq->sz_m1;
140 }
141 
142 static inline int mlx5_wq_ll_is_empty(struct mlx5_wq_ll *wq)
143 {
144 	return !wq->cur_sz;
145 }
146 
147 static inline void *mlx5_wq_ll_get_wqe(struct mlx5_wq_ll *wq, u16 ix)
148 {
149 	return wq->buf + (ix << wq->log_stride);
150 }
151 
152 static inline void mlx5_wq_ll_push(struct mlx5_wq_ll *wq, u16 head_next)
153 {
154 	wq->head = head_next;
155 	wq->wqe_ctr++;
156 	wq->cur_sz++;
157 }
158 
159 static inline void mlx5_wq_ll_pop(struct mlx5_wq_ll *wq, __be16 ix,
160 				  __be16 *next_tail_next)
161 {
162 	*wq->tail_next = ix;
163 	wq->tail_next = next_tail_next;
164 	wq->cur_sz--;
165 }
166 
167 static inline void mlx5_wq_ll_update_db_record(struct mlx5_wq_ll *wq)
168 {
169 	*wq->db = cpu_to_be32(wq->wqe_ctr);
170 }
171 
172 #endif /* __MLX5_WQ_H__ */
173