1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SYS_IB_ADAPTERS_HERMON_QP_H
28 #define	_SYS_IB_ADAPTERS_HERMON_QP_H
29 
30 /*
31  * hermon_qp.h
32  *    Contains all of the prototypes, #defines, and structures necessary
33  *    for all of the Queue Pair Processing routines.
34  *    Specifically it contains the various flags, structures used for managing
35  *    Hermon queue pairs, and prototypes for many of the functions consumed by
36  *    other parts of the Hermon driver (including those routines directly
37  *    exposed through the IBTF CI interface).
38  *
39  *    Most of the values defined below establish default values which,
40  *    where indicated, can be controlled via their related patchable values,
41  *    if 'hermon_alt_config_enable' is set.
42  */
43 
44 #include <sys/types.h>
45 #include <sys/conf.h>
46 #include <sys/ddi.h>
47 #include <sys/sunddi.h>
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 /*
54  * The following defines the default number of Queue Pairs. This value is
55  * controlled via the "hermon_log_num_qp" configuration variables.
56  * We also have a define for the minimum size of a QP.  QPs allocated
57  * with size 0, 1, 2, or 3 will always get back a QP of size 4.
58  */
59 #define	HERMON_NUM_QP_SHIFT		0x10
60 #define	HERMON_NUM_QPS			(1 << HERMON_NUM_QP_SHIFT) /* 65,536 */
61 #define	HERMON_QP_MIN_SIZE		0xf
62 
63 /*
64  * The following defines the default number of Hermon RDMA Remote read
65  * database (RDB) entries per QP.  This value is controllable through the
66  * "hermon_log_num_rdb_per_qp" configuration variable.
67  */
68 #define	HERMON_LOG_NUM_RDB_PER_QP	0x4
69 
70 /*
71  * This defines the maximum number of SGLs per WQE.  This value is
72  * controllable through the "hermon_wqe_max_sgl" configuration variable
73  * (but should not be set larger than this value).
74  */
75 #define	HERMON_NUM_SGL_PER_WQE		0x10
76 
77 /*
78  * Maximum QP number mask (QP number is 24 bits).
79  * We reserve the most significant bit to indicate an "XRC" QP
80  * as recommended by the PRM.  All XRC QPs will have this bit set.
81  */
82 #define	HERMON_QP_MAXNUMBER_MSK		0x7FFFFF
83 #define	HERMON_QP_XRC			0x800000
84 
85 /*
86  * This define and the following macro are used to find a schedule queue for
87  * a new QP based on its queue pair number.  Note:  This is a rather simple
88  * method that we use today.  We simply choose from the schedule queue based
89  * on the 4 least significant bits of the QP number.
90  */
91 
92 /*
93  * The following defines are used to indicate whether a QP is special or
94  * not (and what type it is).  They are used in the "qp_is_special" field
95  * below in qp_state.  If "qp_is_special" == 0 then an ordinary data qp.
96  */
97 
98 /*
99  * The sl is selected based on the qpnum as it was for Tavor/Arbel, except for
100  * QP0, which is defined as being 0xF
101  */
102 
103 #define	HERMON_QP_SMI			0x1
104 #define	HERMON_QP_GSI			0x2
105 
106 #define	HERMON_DEF_SCHED_POLICY		0x03
107 #define	HERMON_DEF_SCHED_SELECTION	0x0F
108 
109 #define	HERMON_QP_SCHEDQ_GET(port, sl, issmi)   \
110 	(HERMON_DEF_SCHED_POLICY 		\
111 	| (issmi == HERMON_QP_SMI ? (HERMON_DEF_SCHED_SELECTION << 2) : \
112 	((issmi == HERMON_QP_GSI ? 0 : (sl & 0XF)) << 2)) \
113 	| ((port & 0x01) << 6) \
114 	| ((issmi == HERMON_QP_SMI ? 0 : 1) << 7))
115 
116 
117 /*
118  * This define determines the frequency with which the AckReq bit will be
119  * set in outgoing RC packets.  By default it is set to five (5) or 2^5 = 32.
120  * So AckReq will be set once every 32 packets sent.  This value is
121  * controllable through the "hermon_qp_ackreq_freq" configuration variable.
122  */
123 #define	HERMON_QP_ACKREQ_FREQ		0x5
124 
125 /*
126  * Define the maximum message size (log 2).  Note: This value corresponds
127  * to the maximum allowable message sized as defined by the IBA spec.
128  */
129 #define	HERMON_QP_LOG_MAX_MSGSZ		0x1F
130 
131 /*
132  * This macro is used to determine if the hermon QP type (qp_serv) is the
133  * same as the caller passed in IBT type (qp_trans).  This is used in QP modify
134  * to ensure the types match.
135  */
136 #define	HERMON_QP_TYPE_VALID(qp_trans, qp_serv)				\
137 	((qp_trans == IBT_UD_SRV && qp_serv == HERMON_QP_UD) ||		\
138 	(qp_trans == IBT_RC_SRV && qp_serv == HERMON_QP_RC) ||		\
139 	(qp_trans == IBT_UC_SRV && qp_serv == HERMON_QP_UC))
140 
141 /*
142  * The following enumerated type is used to capture all the various types
143  * of Hermon work queue types. It is specifically used as an argument to the
144  * to the hermon_qp_sgl_to_logwqesz() routine to determine the amount of
145  * overhead (in WQE header size) consumed by each of the types. This
146  * information is used to round the WQE size to the next largest power-of-2
147  * (and to determine the number of SGLs that are supported for the given WQE
148  * type).  There is also a define below used to specify the minimum size for a
149  * WQE.  The minimum size is set to 64 bytes (a single cacheline).
150  */
151 
152 typedef enum {
153 	HERMON_QP_WQ_TYPE_SENDQ_UD,
154 	HERMON_QP_WQ_TYPE_SENDQ_CONN,
155 	HERMON_QP_WQ_TYPE_RECVQ,
156 	HERMON_QP_WQ_TYPE_SENDMLX_QP0,
157 	HERMON_QP_WQ_TYPE_SENDMLX_QP1
158 } hermon_qp_wq_type_t;
159 #define	HERMON_QP_WQE_MLX_SND_HDRS	0x40
160 #define	HERMON_QP_WQE_MLX_RCV_HDRS	0x00
161 #define	HERMON_QP_WQE_MLX_SRQ_HDRS	0x10
162 #define	HERMON_QP_WQE_MLX_QP0_HDRS	0x40
163 #define	HERMON_QP_WQE_MLX_QP1_HDRS	0x70
164 #define	HERMON_QP_WQE_LOG_MINIMUM	0x6
165 
166 
167 /*
168  * The hermon_qp_info_t structure is used internally by the Hermon driver to
169  * pass information to and from the hermon_qp_alloc() and
170  * hermon_special_qp_alloc() routines.  It contains placeholders for all of the
171  * potential inputs and outputs that either routine can take.
172  */
173 typedef struct hermon_qp_info_s {
174 	ibt_qp_alloc_attr_t	*qpi_attrp;
175 	uint_t			qpi_type;
176 	uint_t			qpi_port;
177 	ibtl_qp_hdl_t		qpi_ibt_qphdl;
178 	ibt_chan_sizes_t	*qpi_queueszp;
179 	ib_qpn_t		*qpi_qpn;
180 	hermon_qphdl_t		qpi_qphdl;
181 } hermon_qp_info_t;
182 
183 /*
184  * The QPN entry which is stored in the AVL tree
185  */
186 typedef struct hermon_qpn_entry_s {
187 	avl_node_t		qpn_avlnode;
188 	uint_t			qpn_refcnt;
189 	uint_t			qpn_counter;
190 	uint_t			qpn_indx;
191 	hermon_rsrc_t		*qpn_qpc;
192 } hermon_qpn_entry_t;
193 #define	HERMON_QPN_NOFLAG		0x0
194 #define	HERMON_QPN_RELEASE		0x1
195 #define	HERMON_QPN_FREE_ONLY		0x2
196 
197 #define	HERMON_QP_OH_SIZE		0x0800
198 /*
199  * 2KB, fixed per Mnox PRM 0.35c & conversation w/Mnox technical Sep 5, 2007
200  */
201 
202 /*
203  * The hermon_sw_qp_s structure is also referred to using the "hermon_qphdl_t"
204  * typedef (see hermon_typedef.h).  It encodes all the information necessary
205  * to track the various resources needed to allocate, query, modify, and
206  * (later) free both normal QP and special QP.
207  *
208  * Specifically, it has a lock to ensure single threaded access to the QP.
209  * It has QP state, type, and number, pointers to the PD, MR, and CQ handles
210  * associated with the QP, and pointers to the buffer where the work queues
211  * come from.
212  *
213  * It has two pointers (one per work queue) to the workQ headers for the WRID
214  * list, as well as pointers to the last WQE on each chain (used when
215  * connecting a new chain of WQEs to a previously executing chain - see
216  * hermon_wr.c).  It's also got the real WQE size, real number of SGL per WQE,
217  * and the size of each of the work queues (in number of WQEs).
218  *
219  * Additionally, it has pointers to the resources associated with the QP,
220  * including the obligatory backpointer to the resource for the QP handle
221  * itself.  But it also has some flags, like "qp_forward_sqd_event" and
222  * "qp_sqd_still_draining" (which are used to indicate whether a Send Queue
223  * Drained Event should be forwarded to the IBTF) or "qp_is_special",
224  * "qp_portnum", and "qp_pkeyindx" (which are used by special QP to store
225  * necessary information about the type of the QP, which port it's connected
226  * to, and what its current PKey index is set to).
227  */
228 struct hermon_sw_qp_s {
229 	kmutex_t		qp_lock;
230 	uint_t			qp_state;
231 	uint32_t		qp_qpnum;
232 	hermon_pdhdl_t		qp_pdhdl;
233 	uint_t			qp_serv_type;
234 	uint_t			qp_sl;		/* service level */
235 	hermon_mrhdl_t		qp_mrhdl;
236 	uint_t			qp_sq_sigtype;
237 	uint_t			qp_is_special;
238 	uint_t			qp_is_umap;
239 	uint32_t		qp_uarpg;
240 	devmap_cookie_t		qp_umap_dhp;
241 	uint_t			qp_portnum;	/* port 0/1 for HCA */
242 	uint_t			qp_portnum_alt;	/* port 0/1 for HCA */
243 	uint_t			qp_pkeyindx;
244 	uint_t			qp_no_prefetch;
245 				/* prefetch == 0, none == 1, for headroom */
246 	uint_t			qp_rlky;	/* using reserved lkey */
247 
248 	/* Send Work Queue */
249 	kmutex_t		qp_sq_lock;
250 	hermon_cqhdl_t		qp_sq_cqhdl;
251 	hermon_workq_avl_t	qp_sq_wqavl;
252 	hermon_workq_hdr_t	*qp_sq_wqhdr;
253 	uint32_t		*qp_sq_buf;
254 	uint32_t		qp_sq_bufsz;
255 	uint32_t		qp_sq_logqsz;	/* used to check owner valid */
256 	uint32_t		qp_sq_log_wqesz;
257 	uint32_t		qp_sq_headroom; /* #bytes needed for headroom */
258 	uint32_t		qp_sq_hdrmwqes;	/* # wqes needed for headroom */
259 	uint32_t		qp_sq_baseaddr;
260 	uint32_t		qp_sq_sgl;
261 	uint_t			qp_uses_lso;
262 	uint32_t		qp_ring;
263 
264 	/* Receive Work Queue - not used when SRQ is used */
265 	kmutex_t		qp_rq_lock;
266 	hermon_cqhdl_t		qp_rq_cqhdl;
267 	hermon_workq_avl_t	qp_rq_wqavl;	/* needed for srq */
268 	hermon_workq_hdr_t	*qp_rq_wqhdr;
269 	uint32_t		*qp_rq_buf;
270 	uint32_t		qp_rq_bufsz;
271 	uint32_t		qp_rq_logqsz;	/* used to check owner valid */
272 	uint32_t		qp_rq_log_wqesz;
273 	uint32_t		qp_rq_wqecntr;
274 	uint32_t		qp_rq_baseaddr;
275 	uint32_t		qp_rq_sgl;
276 
277 	/* DoorBell Record information */
278 	ddi_acc_handle_t	qp_rq_dbr_acchdl;
279 	hermon_dbr_t		*qp_rq_vdbr;
280 	uint64_t		qp_rq_pdbr;
281 	uint64_t		qp_rdbr_mapoffset;	/* user mode access */
282 
283 	uint64_t		qp_desc_off;
284 
285 	hermon_rsrc_t		*qp_qpcrsrcp;
286 	hermon_rsrc_t		*qp_rsrcp;
287 	void			*qp_hdlrarg;
288 	uint_t			qp_forward_sqd_event;
289 	uint_t			qp_sqd_still_draining;
290 
291 	/* Shared Receive Queue */
292 	hermon_srqhdl_t		qp_srqhdl;
293 	uint_t			qp_srq_en;
294 
295 	/* Refcnt of QP belongs to an MCG */
296 	uint_t			qp_mcg_refcnt;
297 
298 	/* save the mtu from init2rtr for future use */
299 	uint_t			qp_save_mtu;
300 	hermon_qpn_entry_t	*qp_qpn_hdl;
301 
302 	struct hermon_qalloc_info_s qp_wqinfo;
303 
304 	struct hermon_hw_qpc_s qpc;
305 };
306 _NOTE(READ_ONLY_DATA(hermon_sw_qp_s::qp_qpnum
307     hermon_sw_qp_s::qp_sq_buf
308     hermon_sw_qp_s::qp_sq_log_wqesz
309     hermon_sw_qp_s::qp_sq_bufsz
310     hermon_sw_qp_s::qp_sq_sgl
311     hermon_sw_qp_s::qp_rq_buf
312     hermon_sw_qp_s::qp_rq_log_wqesz
313     hermon_sw_qp_s::qp_rq_bufsz
314     hermon_sw_qp_s::qp_rq_sgl
315     hermon_sw_qp_s::qp_desc_off
316     hermon_sw_qp_s::qp_mrhdl
317     hermon_sw_qp_s::qp_wqinfo
318     hermon_sw_qp_s::qp_qpcrsrcp
319     hermon_sw_qp_s::qp_rsrcp
320     hermon_sw_qp_s::qp_hdlrarg
321     hermon_sw_qp_s::qp_pdhdl
322     hermon_sw_qp_s::qp_sq_cqhdl
323     hermon_sw_qp_s::qp_rq_cqhdl
324     hermon_sw_qp_s::qp_sq_sigtype
325     hermon_sw_qp_s::qp_serv_type
326     hermon_sw_qp_s::qp_is_special
327     hermon_sw_qp_s::qp_is_umap
328     hermon_sw_qp_s::qp_uarpg
329     hermon_sw_qp_s::qp_sq_wqhdr
330     hermon_sw_qp_s::qp_rq_wqhdr
331     hermon_sw_qp_s::qp_qpn_hdl))
332 _NOTE(MUTEX_PROTECTS_DATA(hermon_sw_qp_s::qp_lock,
333     hermon_sw_qp_s::qp_state
334     hermon_sw_qp_s::qpc
335     hermon_sw_qp_s::qp_forward_sqd_event
336     hermon_sw_qp_s::qp_sqd_still_draining
337     hermon_sw_qp_s::qp_mcg_refcnt
338     hermon_sw_qp_s::qp_save_mtu
339     hermon_sw_qp_s::qp_umap_dhp))
340 _NOTE(SCHEME_PROTECTS_DATA("safe sharing",
341     hermon_sw_qp_s::qp_pkeyindx
342     hermon_sw_qp_s::qp_portnum))
343 
344 
345 /* Defined in hermon_qp.c */
346 int hermon_qp_alloc(hermon_state_t *state, hermon_qp_info_t *qpinfo,
347     uint_t sleepflag);
348 int hermon_special_qp_alloc(hermon_state_t *state, hermon_qp_info_t *qpinfo,
349     uint_t sleepflag);
350 int hermon_qp_free(hermon_state_t *state, hermon_qphdl_t *qphdl,
351     ibc_free_qp_flags_t free_qp_flags, ibc_qpn_hdl_t *qpnh, uint_t sleepflag);
352 int hermon_qp_query(hermon_state_t *state, hermon_qphdl_t qphdl,
353     ibt_qp_query_attr_t *attr_p);
354 hermon_qphdl_t hermon_qphdl_from_qpnum(hermon_state_t *state, uint_t qpnum);
355 void hermon_qp_release_qpn(hermon_state_t *state, hermon_qpn_entry_t *entry,
356     int flags);
357 void hermon_qpn_avl_init(hermon_state_t *state);
358 void hermon_qpn_avl_fini(hermon_state_t *state);
359 
360 /* Defined in hermon_qpmod.c */
361 int hermon_qp_modify(hermon_state_t *state, hermon_qphdl_t qp,
362     ibt_cep_modify_flags_t flags, ibt_qp_info_t *info_p,
363     ibt_queue_sizes_t *actual_sz);
364 int hermon_qp_to_reset(hermon_state_t *state, hermon_qphdl_t qp);
365 
366 #ifdef __cplusplus
367 }
368 #endif
369 
370 #endif	/* _SYS_IB_ADAPTERS_HERMON_QP_H */
371