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_CMD_H
28 #define	_SYS_IB_ADAPTERS_HERMON_CMD_H
29 
30 /*
31  * hermon_cmd.h
32  *    Contains all of the prototypes, #defines, and structures necessary
33  *    for the Hermon Firmware Command routines.
34  *    Specifically it contains the command types, command statuses and flags,
35  *    structures used for managing Hermon mailboxes and outstanding commands,
36  *    and prototypes for most of the functions consumed by other parts of
37  *    the Hermon driver.
38  */
39 
40 #include <sys/types.h>
41 #include <sys/conf.h>
42 #include <sys/ddi.h>
43 #include <sys/sunddi.h>
44 
45 #include <sys/ib/mgt/sm_attr.h>
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 
52 /*
53  * Defines used hermon_write_hcr() to determine the duration and number of
54  * times (at maximum) to poll while waiting for a Hermon firmware command to
55  * release the HCR resource (i.e. waiting for the command to complete)
56  */
57 #define	HERMON_CMD_POLL_DELAY		1
58 #define	HERMON_CMD_POLL_MAX		3000000
59 
60 /*
61  * The following defines specify the default number of mailboxes (log 2) of
62  * each type and their size and alignment restrictions.  By default the number
63  * of both "In" and "Out" mailboxes is set to 1024 (with each mailbox being
64  * 4KB in size), but both the number and sizes of each are controllable
65  * through the "hermon_log_num_inmbox", "hermon_log_num_outmbox",
66  * "hermon_log_inmbox_size" and "hermon_log_outmbox_size" configuration
67  * variables. Also, we have a define that is used to allocate interrupt
68  * mailboxes (1 in, 1 out).
69  */
70 #define	HERMON_NUM_MAILBOXES_SHIFT	0xA
71 #define	HERMON_NUM_INTR_MAILBOXES_SHIFT	0
72 #define	HERMON_MBOX_SIZE_SHIFT		0xC
73 #define	HERMON_MBOX_SIZE			(1 << HERMON_MBOX_SIZE_SHIFT)
74 #define	HERMON_MBOX_ALIGN		HERMON_MBOX_SIZE
75 
76 /*
77  * These are the defines for the Hermon command type (opcodes).  They are
78  * specified by the Hermon PRM
79  */
80 
81 /* Init Commands */
82 #define	QUERY_DEV_LIM			0x3
83 #define	QUERY_DEV_CAP			0x3
84 #define	QUERY_FW			0x4
85 #define	QUERY_ADAPTER			0x6
86 #define	INIT_HCA			0x7
87 #define	CLOSE_HCA			0x8
88 #define	INIT_IB				0x9
89 #define	INIT_PORT			0x9
90 #define	CLOSE_IB			0xA
91 #define	CLOSE_PORT			0xA
92 #define	QUERY_HCA			0xB
93 #define	SET_IB				0xC
94 #define	SET_PORT			0xC
95 /* added late in tavor for SRQ support */
96 #define	MOD_STAT_CFG			0x34
97 /* added late in Hermon (PRM 0.35) */
98 #define	QUERY_PORT			0x43
99 
100 
101 /* TPT Commands */
102 #define	SW2HW_MPT			0xD
103 #define	QUERY_MPT			0xE
104 #define	HW2SW_MPT			0xF
105 #define	READ_MTT			0x10
106 #define	WRITE_MTT			0x11
107 #define	SYNC_TPT			0x2F
108 #define	MODIFY_MPT			0x39
109 
110 /* EQ Commands */
111 #define	MAP_EQ				0x12
112 #define	SW2HW_EQ			0x13
113 #define	HW2SW_EQ			0x14
114 #define	QUERY_EQ			0x15
115 
116 /* CQ Commands */
117 #define	SW2HW_CQ			0x16
118 #define	HW2SW_CQ			0x17
119 #define	QUERY_CQ			0x18
120 #define	MODIFY_CQ			0x2C
121 
122 /* Modify CQ Command - opcode modifiers */
123 #define	RESIZE_CQ			0x0
124 #define	MODIFY_MODERATION_CQ		0x1
125 #define	MODIFY_EQN			0x2
126 
127 /* QP Commands */
128 #define	RST2INIT_QP			0x19
129 #define	INIT2INIT_QP			0x2D
130 #define	INIT2RTR_QP			0x1A
131 #define	RTR2RTS_QP			0x1B
132 #define	RTS2RTS_QP			0x1C
133 #define	SQERR2RTS_QP			0x1D
134 #define	TOERR_QP			0x1E
135 #define	RTS2SQD_QP			0x1F
136 #define	SQD2SQD_QP			0x38
137 #define	SQD2RTS_QP			0x20
138 #define	TORST_QP			0x21
139 #define	QUERY_QP			0x22
140 #define	SUSPEND_QP			0x32	/* new w/ hermon driver */
141 #define	UNSUSPEND_QP			0x33	/* new w/ hermon driver */
142 
143 /* SPECIAL QPs Commands */
144 #define	CONF_SPECIAL_QP			0x23
145 #define	MAD_IFC				0x24
146 
147 /* added late in tavor for SRQ support */
148 /* SRQ Commands */
149 #define	SW2HW_SRQ			0x35
150 #define	HW2SW_SRQ			0x36
151 #define	QUERY_SRQ			0x37
152 /* new in hermon, replaces part of modifyMPT */
153 #define	RESIZE_SRQ			0X44
154 /* new in hermon, set limit water mark */
155 #define	ARM_RQ				0X40
156 
157 /* Multicast Group Commands */
158 #define	READ_MGM			0x25
159 #define	READ_MCG			0x25
160 #define	WRITE_MGM			0x26
161 #define	WRITE_MCG			0x26
162 #define	MGID_HASH			0x27
163 
164 /* Debug/Diagnostic Commands */
165 #define	QUERY_DEBUG_MSG			0x2A
166 #define	SET_DEBUG_MSG			0x2B
167 #define	DIAG_RPRT			0x30
168 #define	CMD_NOP				0x31
169 
170 
171 /* ICM and related commands - w/out LAM commands from Arbel */
172 #define	RUN_FW				0xFF6
173 #define	UNMAP_ICM			0xFF9
174 #define	MAP_ICM				0xFFA
175 #define	UNMAP_ICM_AUX			0xFFB
176 #define	MAP_ICM_AUX			0xFFC
177 #define	SET_ICM_SIZE			0xFFD
178 #define	UNMAP_FA			0xFFE
179 #define	MAP_FA				0xFFF
180 
181 /*
182  * Commands mentioned but not defined in PRM v35
183  *	REL_ICM_AUX
184  *	INIT_VM
185  * 	HEART_BEAT_RQ
186  */
187 
188 /*
189  * These are the defines for the Hermon command completion statuses.  They are
190  * also specified (in part) by the Hermon PRM.  However,
191  * the HERMON_CMD_INSUFF_RSRC, HERMON_CMD_TIMEOUT and HERMON_CMD_INVALID_STATUS
192  * codes were added for this driver specifically to indicate the conditions
193  * when insufficient resources are available for a command, when a command has
194  * timed out (failure in the Hermon firmware) or when some other invalid result
195  * was received.
196  */
197 #define	HERMON_CMD_TIMEOUT_TOGGLE	0xFFFC 	/* -4 */
198 #define	HERMON_CMD_INSUFF_RSRC		0xFFFD	/* -3 */
199 #define	HERMON_CMD_TIMEOUT_GOBIT	0xFFFE 	/* -2 */
200 #define	HERMON_CMD_INVALID_STATUS	0xFFFF  /* -1 */
201 #define	HERMON_CMD_SUCCESS		0x00
202 #define	HERMON_CMD_INTERNAL_ERR		0x01
203 #define	HERMON_CMD_BAD_OP		0x02
204 #define	HERMON_CMD_BAD_PARAM		0x03
205 #define	HERMON_CMD_BAD_SYS_STATE		0x04
206 #define	HERMON_CMD_BAD_RESOURCE		0x05
207 #define	HERMON_CMD_RESOURCE_BUSY		0x06
208 #define	HERMON_CMD_EXCEED_LIM		0x08
209 #define	HERMON_CMD_BAD_RES_STATE		0x09
210 #define	HERMON_CMD_BAD_INDEX		0x0A
211 #define	HERMON_CMD_BAD_NVMEM		0x0B
212 #define	HERMON_CMD_ICM_ERROR		0x0C
213 #define	HERMON_CMD_BAD_QP_STATE		0x10
214 #define	HERMON_CMD_BAD_SEG_PARAM		0x20
215 #define	HERMON_CMD_REG_BOUND		0x21
216 #define	HERMON_CMD_BAD_PKT		0x30
217 #define	HERMON_CMD_BAD_SIZE		0x40
218 
219 /*
220  * These defines are used in the "special QP" allocation to indicate the type
221  * of special QP (SMI, GSI, or one of the raw types).  These values are
222  * specified by the Hermon PRM
223  */
224 #define	HERMON_CMD_QP_SMI		0
225 #define	HERMON_CMD_QP_GSI		1
226 #define	HERMON_CMD_QP_RAW_IPV6		2
227 #define	HERMON_CMD_QP_RAW_ETH		3
228 
229 #define	HERMON_CMD_SPEC_QP_OPMOD(smi, gsi) \
230 	((smi & 0x01) | ((gsi & 0x01) << 1))
231 
232 /*
233  * For certain Hermon QP state transition commands some optional flags are
234  * allowed.  These "opmask" flags are defined by the Hermon PRM
235  * as a bitmask.
236  */
237 
238 #define	HERMON_CMD_OP_ALT_PATH		(1 << 0)
239 #define	HERMON_CMD_OP_RRE		(1 << 1)
240 #define	HERMON_CMD_OP_RAE		(1 << 2)
241 #define	HERMON_CMD_OP_RWE		(1 << 3)
242 #define	HERMON_CMD_OP_PKEYINDX		(1 << 4) /* primary path */
243 #define	HERMON_CMD_OP_QKEY		(1 << 5)
244 #define	HERMON_CMD_OP_MINRNRNAK		(1 << 6)
245 #define	HERMON_CMD_OP_PRIM_PATH		(1 << 7)
246 #define	HERMON_CMD_OP_SRA_SET		(1 << 8)
247 #define	HERMON_CMD_OP_RRA_SET		(1 << 9)
248 #define	HERMON_CMD_OP_PM_STATE		(1 << 10) /* migration */
249 /* HERMON_CMD_OP_PRIM_PORT is obsolete, instead use HERMON_CMD_OP_SCHEDQUEUE */
250 #define	HERMON_CMD_OP_PRIM_PORT		(1 << 11)
251 #define	HERMON_CMD_OP_RETRYCNT		(1 << 12) /* Global */
252 #define	HERMON_CMD_OP_ALT_RNRRETRY	(1 << 13)
253 #define	HERMON_CMD_OP_ACKTIMEOUT	(1 << 14) /* primary path */
254 #define	HERMON_CMD_OP_PRIM_RNRRETRY	(1 << 15) /* reserved in HERMON */
255 #define	HERMON_CMD_OP_SCHEDQUEUE	(1 << 16)
256 #define	HERMON_CMD_OP_RSSCONTEXT	(1 << 17)
257 #define	HERMON_CMD_OP_SRQN		(1 << 18) /* for rss balancing */
258 #define	HERMON_CMD_OP_CQN_RCV		(1 << 19) /* for rss balancing */
259 /* Bits 20 - 31 RESERVED - per PRM 0.35c */
260 
261 
262 
263 /*
264  * The Hermon RTS2SQD command can take the following flag as part of its
265  * input modifier to request the Send Queue Drained event
266  */
267 #define	HERMON_CMD_REQ_SQD_EVENT		0x80000000
268 
269 /*
270  * The Hermon TORST command can take the following flag (as part of a bitmask)
271  * in its opcode modifier to request that the transition to reset should
272  * not go through the Error state (and, hence, should not generate "flushed-
273  * in-error" completions
274  */
275 #define	HERMON_CMD_DIRECT_TO_RESET	(1 << 1)
276 
277 /*
278  * Some Hermon commands write an OUT mailbox entry, depending on the value of
279  * the 'opmod' parameter.  These defines provide the correct opmod value to
280  * write depending on whether to write an entry or not.
281  */
282 #define	HERMON_CMD_DO_OUTMBOX		(0)
283 #define	HERMON_CMD_NO_OUTMBOX		(1 << 0)
284 
285 
286 /*
287  * The Hermon MAP_EQ command can take the following flags (and use the
288  * HERMON_CMD_UNMAP_EQ_MASK input modifier) to indicate whether the given
289  * event queue should mapped to or unmapped from the given event type.
290  */
291 
292 
293 #define	HERMON_CMD_MAP_EQ_EVT_MAP	0
294 #define	HERMON_CMD_MAP_EQ_EVT_UNMAP	1
295 #define	HERMON_CMD_UNMAP_EQ_MASK	0x80000000
296 
297 /*
298  * The following defines are used by the MAD_IFC command and the helper
299  * routines that get PortInfo, NodeInfo, GUIDInfo, and PKeyTable entries.
300  *
301  * The first indicates whether of not MKey checking should be enforced.
302  * This is passed in the opcode modifier field for MAD_IFC commands.
303  *
304  * The next set are used to define certain hardcoded management datagram (MAD)
305  * sizes, offsets, and header formats for each of the helper operations.
306  */
307 #define	HERMON_CMD_MKEY_CHECK		0
308 #define	HERMON_CMD_MKEY_DONTCHECK	1
309 #define	HERMON_CMD_BKEY_DONTCHECK	2
310 
311 #define	HERMON_CMD_MAD_IFC_SIZE		0x100
312 #define	HERMON_CMD_MADDATA_OFFSET	0x40
313 #define	HERMON_CMD_MADHDR0		0x01010101
314 #define	HERMON_CMD_MADHDR1		0x00000000
315 #define	HERMON_CMD_MADHDR2		0x00000000
316 #define	HERMON_CMD_MADHDR3		0x00000000
317 
318 #define	HERMON_CMD_PORTINFO		0x00150000
319 #define	HERMON_CMD_NODEINFO		0x00110000
320 #define	HERMON_CMD_NODEDESC		0x00100000
321 #define	HERMON_CMD_GUIDINFO		0x00140000
322 #define	HERMON_CMD_PKEYTBLE		0x00160000
323 
324 #define	HERMON_CMD_PERF_GET		0x01040101
325 #define	HERMON_CMD_PERF_SET		0x01040102
326 #define	HERMON_CMD_CLASSPORTINFO	0x00010000
327 #define	HERMON_CMD_PERFCNTRS		0x00120000
328 #define	HERMON_CMD_EXTPERFCNTRS		0x001D0000
329 #define	HERMON_CMD_PERFATTR		0x00000000
330 
331 #define	HERMON_IS_EXT_WIDTH_SUPPORTED		0x0000020000000000
332 #define	HERMON_IS_EXT_WIDTH_SUPPORTED_NOIETF	0x0000040000000000
333 
334 
335 /*
336  * The next few defines are used to indicate the size of the "reserved" area
337  * in the WRITE_MTT command, and the respective sizes of the SET_PORT and
338  * MGID_HASH commands
339  */
340 #define	HERMON_CMD_WRITEMTT_RSVD_SZ	0x10
341 #define	HERMON_CMD_SETPORT_SZ		0x8
342 #define	HERMON_CMD_MGIDHASH_SZ		0x10
343 
344 /*
345  * This last define is used by hermon_cmn_ownership_cmd_post() to keep track
346  * of the direction (from hardware ownership to software, or vice versa) of
347  * the requested operation
348  */
349 #define	HERMON_CMD_RSRC_HW2SW		0
350 #define	HERMON_CMD_RSRC_SW2HW		1
351 
352 /*
353  * The following macros are used for handling any endianness related issues
354  * that might arise from the Hermon driver's internal use of MADs.
355  *
356  *    HERMON_GETPORTINFO_SWAP	- All the necessary swapping to handle the
357  *				    response to a GetPortInfo MAD
358  *    HERMON_GETNODEINFO_SWAP	- All the necessary swapping to handle the
359  *				    response to a GetNodeInfo MAD
360  *    HERMON_GETGUIDINFO_SWAP	- All the necessary swapping to handle the
361  *				    response to a GetGUIDInfo MAD
362  *    HERMON_GETPKEYTABLE_SWAP	- All the necessary swapping to handle the
363  *				    response to a GetPKeyTable MAD
364  */
365 
366 
367 #ifdef	_LITTLE_ENDIAN
368 #define	HERMON_GETPORTINFO_SWAP(portinfo)				\
369 {									\
370 	(portinfo)->M_Key = ddi_swap64((portinfo)->M_Key);		\
371 	(portinfo)->GidPrefix = ddi_swap64((portinfo)->GidPrefix);	\
372 	(portinfo)->LID = ddi_swap16((portinfo)->LID);			\
373 	(portinfo)->MasterSMLID = ddi_swap16((portinfo)->MasterSMLID);	\
374 	(portinfo)->CapabilityMask =					\
375 	    ddi_swap32((portinfo)->CapabilityMask);			\
376 	(portinfo)->DiagCode = ddi_swap16((portinfo)->DiagCode);	\
377 	(portinfo)->M_KeyLeasePeriod =					\
378 	    ddi_swap16((portinfo)->M_KeyLeasePeriod);			\
379 	(portinfo)->M_KeyViolations =					\
380 	    ddi_swap16((portinfo)->M_KeyViolations);			\
381 	(portinfo)->P_KeyViolations =					\
382 	    ddi_swap16((portinfo)->P_KeyViolations);			\
383 	(portinfo)->Q_KeyViolations =					\
384 	    ddi_swap16((portinfo)->Q_KeyViolations);			\
385 }
386 #else
387 #define	HERMON_GETPORTINFO_SWAP(portinfo)
388 #endif
389 
390 #ifdef	_LITTLE_ENDIAN
391 #define	HERMON_GETNODEINFO_SWAP(nodeinfo)				\
392 {									\
393 	uint32_t	tmp;						\
394 									\
395 	tmp = ddi_swap32(((uint32_t *)nodeinfo)[9]);			\
396 	(nodeinfo)->VendorID	 = tmp & 0xFFFFFF;			\
397 	(nodeinfo)->LocalPortNum = tmp >> 24;				\
398 	(nodeinfo)->Revision	 =					\
399 	    ddi_swap32(((uint32_t *)nodeinfo)[8]);			\
400 	tmp = ddi_swap32(((uint32_t *)nodeinfo)[7]);			\
401 	(nodeinfo)->PartitionCap = tmp >> 16;				\
402 	(nodeinfo)->DeviceID	 = tmp & 0xFFFF;			\
403 	(nodeinfo)->PortGUID = ddi_swap64((((uint64_t)			\
404 	    (((uint32_t *)nodeinfo)[6]) << 32) |			\
405 	    ((uint32_t *)nodeinfo)[5]));				\
406 	(nodeinfo)->NodeGUID = ddi_swap64((((uint64_t)			\
407 	    (((uint32_t *)nodeinfo)[4]) << 32) |			\
408 	    ((uint32_t *)nodeinfo)[3]));				\
409 	(nodeinfo)->SystemImageGUID = ddi_swap64((((uint64_t)		\
410 	    (((uint32_t *)nodeinfo)[2]) << 32) |			\
411 	    ((uint32_t *)nodeinfo)[1]));				\
412 }
413 #else
414 #define	HERMON_GETNODEINFO_SWAP(nodeinfo)				\
415 {									\
416 	uint32_t	tmp;						\
417 									\
418 	tmp = ((uint32_t *)nodeinfo)[9];				\
419 	(nodeinfo)->VendorID	 = tmp & 0xFFFFFF;			\
420 	(nodeinfo)->LocalPortNum = tmp >> 24;				\
421 	(nodeinfo)->Revision	 = ((uint32_t *)nodeinfo)[8];		\
422 	tmp = ((uint32_t *)nodeinfo)[7];				\
423 	(nodeinfo)->PartitionCap = tmp >> 16;				\
424 	(nodeinfo)->DeviceID	 = tmp & 0xFFFF;			\
425 	(nodeinfo)->PortGUID = (((uint64_t)				\
426 	    (((uint32_t *)nodeinfo)[5]) << 32) |			\
427 	    ((uint32_t *)nodeinfo)[6]);					\
428 	(nodeinfo)->NodeGUID = (((uint64_t)				\
429 	    (((uint32_t *)nodeinfo)[3]) << 32) |			\
430 	    ((uint32_t *)nodeinfo)[4]);					\
431 	(nodeinfo)->SystemImageGUID = (((uint64_t)			\
432 	    (((uint32_t *)nodeinfo)[1]) << 32) |			\
433 	    ((uint32_t *)nodeinfo)[2]);					\
434 }
435 #endif
436 
437 #ifdef	_LITTLE_ENDIAN
438 #define	HERMON_GETGUIDINFO_SWAP(guidinfo)				\
439 {									\
440 	int	i;							\
441 									\
442 	for (i = 0; i < 8; i++) {					\
443 		(guidinfo)->GUIDBlocks[i] =				\
444 		    ddi_swap64((guidinfo)->GUIDBlocks[i]);		\
445 	}								\
446 }
447 #else
448 #define	HERMON_GETGUIDINFO_SWAP(guidinfo)
449 #endif
450 
451 #ifdef	_LITTLE_ENDIAN
452 #define	HERMON_GETPKEYTABLE_SWAP(pkeytable)				\
453 {									\
454 	int	i;							\
455 									\
456 	for (i = 0; i < 32; i++) {					\
457 		(pkeytable)->P_KeyTableBlocks[i] =			\
458 		    ddi_swap16((pkeytable)->P_KeyTableBlocks[i]);	\
459 	}								\
460 }
461 #else
462 #define	HERMON_GETPKEYTABLE_SWAP(pkeytable)
463 #endif
464 
465 /*
466  * The Hermon MODIFY_MPT command can take the following opcode modifier
467  * options to specify whether to modify for ResizeSRQ() or to swap the
468  * full MPT entry.
469  */
470 #define	HERMON_CMD_MODIFY_MPT_RESIZESRQ	3
471 #define	HERMON_CMD_MODIFY_MPT_SWAPFULL	5
472 
473 /*
474  * Hermon MOD_STAT_CFG Opcode Modifier
475  */
476 #define	HERMON_MOD_STAT_CFG_PTR		0x0
477 #define	HERMON_MOD_STAT_CFG_INLINE	0x1
478 #define	HERMON_MOD_STAT_CFG_DEFAULTS	0xF
479 
480 
481 /*
482  * The hermon_mbox_t structure is used internally by the Hermon driver to track
483  * all the information necessary to manage mailboxes for the Hermon command
484  * interface.  Specifically, by containing a pointer to the buffer, the
485  * PCI mapped address, the access handle, and a back pointer to the
486  * hermon_rsrc_t structure used to track this resource, it provides enough
487  * information allocate, use, and free any type of mailbox.
488  *
489  * The mb_indx, mb_next, and mb_prev fields are used only by the mailbox
490  * alloc/free routines (see hermon_impl_mbox_alloc/free() for more details)
491  * and are not read or modified by any mailbox consumers.  They are used
492  * to implement a fast allocation mechanism.
493  */
494 typedef struct hermon_mbox_s {
495 	void			*mb_addr;
496 	uint64_t		mb_mapaddr;
497 	ddi_acc_handle_t	mb_acchdl;
498 	hermon_rsrc_t		*mb_rsrcptr;
499 	uint_t			mb_indx;
500 	uint_t			mb_next;
501 	uint_t			mb_prev;
502 } hermon_mbox_t;
503 
504 /*
505  * The hermon_mboxlist_t structure is used to track all the information
506  * relevant to the pools of Hermon mailboxes.  Specifically, it has a pointer
507  * to an array of hermon_mbox_t structures, a lock and cv used for blocking
508  * on alloc when mailboxes are not available, and a head, tail, and entries
509  * free counter to keep track of which (if any) mailboxes are currently free.
510  * This is used (along with the mb_indx, mb_next, and mb_prev fields in the
511  * hermon_mbox_t) to implement the fast allocation mechanism.
512  */
513 typedef struct hermon_mboxlist_s {
514 	kmutex_t		mbl_lock;
515 	kcondvar_t		mbl_cv;
516 	hermon_mbox_t		*mbl_mbox;
517 	uint_t			mbl_list_sz;
518 	uint_t			mbl_num_alloc;
519 	uint_t			mbl_head_indx;
520 	uint_t			mbl_tail_indx;
521 	uint_t			mbl_entries_free;
522 	uint_t			mbl_waiters;
523 	uint_t			mbl_pollers;
524 	uint_t			mbl_signal;
525 } hermon_mboxlist_t;
526 _NOTE(MUTEX_PROTECTS_DATA(hermon_mboxlist_t::mbl_lock,
527     hermon_mboxlist_t::mbl_mbox
528     hermon_mboxlist_t::mbl_list_sz
529     hermon_mboxlist_t::mbl_num_alloc
530     hermon_mboxlist_t::mbl_cv
531     hermon_mboxlist_t::mbl_head_indx
532     hermon_mboxlist_t::mbl_tail_indx
533     hermon_mboxlist_t::mbl_entries_free
534     hermon_mboxlist_t::mbl_waiters
535     hermon_mboxlist_t::mbl_pollers
536     hermon_mboxlist_t::mbl_signal
537     hermon_mbox_t::mb_next
538     hermon_mbox_t::mb_prev))
539 
540 /*
541  * The hermon_mbox_info_t structure is used by mailbox allocators to specify
542  * the type of mailbox(es) being requested.  On a call to hermon_mbox_alloc()
543  * the mbi_alloc_flags may be set to HERMON_ALLOC_INMBOX, HERMON_ALLOC_OUTMBOX,
544  * or both.  If it is able to allocate the request type(s) of mailboxes,
545  * hermon_mbox_alloc() will fill in the "mbi_in" and/or "mbi_out" pointers
546  * to point to valid hermon_mbox_t structures from the appropriate
547  * hermon_mboxlist_t (see above).
548  * This same structure is also passed to hermon_mbox_free().  It is the
549  * responsibility of the caller to hermon_mbox_alloc() to return this exact
550  * structure (unmodified) to hermon_mbox_free().
551  *
552  * Note: If both "In" and "Out" mailboxes are requested, it is assured that
553  * no deadlock can result (from holding one mailbox while attempting to get
554  * the other).  This is assured by the fact that the "In" mailbox will always
555  * be allocated first before attempting to allocate the "Out"
556  */
557 typedef struct hermon_mbox_info_s {
558 	uint_t			mbi_alloc_flags;
559 	uint_t			mbi_sleep_context;
560 	hermon_mbox_t		*mbi_in;
561 	hermon_mbox_t		*mbi_out;
562 } hermon_mbox_info_t;
563 #define	HERMON_ALLOC_INMBOX	(1 << 0)
564 #define	HERMON_ALLOC_OUTMBOX	(1 << 1)
565 
566 
567 /*
568  * The hermon_cmd_t structure is used internally by the Hermon driver to track
569  * all the information necessary to manage outstanding firmware commands on
570  * the Hermon command interface.
571  *
572  * Each hermon_cmd_t structure contains a cv and lock which are used by the
573  * posting thread to block for completion (with cmd_status being overloaded
574  * to indicate the condition variable).  The cmd_outparam field is used to
575  * return additional status from those Hermon commands that specifically
576  * require it.
577  *
578  * The cmd_indx, cmd_next, and cmd_prev fields are used by the outstanding
579  * command alloc/free routines (see hermon_outstanding_cmd_alloc/free() for
580  * more details).  They are used (in much the same way as the mb_indx,
581  * mb_next, and mb_prev fields in hermon_mbox_t above) to implement a fast
582  * allocation mechanism.
583  */
584 typedef struct hermon_cmd_s {
585 	kmutex_t		cmd_comp_lock;
586 	kcondvar_t		cmd_comp_cv;
587 	uint64_t		cmd_outparm;
588 	uint_t			cmd_status;
589 	uint_t			cmd_indx;
590 	uint_t			cmd_next;
591 	uint_t			cmd_prev;
592 } hermon_cmd_t;
593 _NOTE(MUTEX_PROTECTS_DATA(hermon_cmd_t::cmd_comp_lock,
594     hermon_cmd_t::cmd_comp_cv
595     hermon_cmd_t::cmd_status))
596 
597 /*
598  * The hermon_cmdlist_t structure is used in almost exactly the same way as
599  * the hermon_mboxlist_t above, but instead to track all the information
600  * relevant to the pool of outstanding Hermon commands.  Specifically, it has
601  * a pointer to an array of hermon_cmd_t structures, a lock and cv used for
602  * blocking on alloc when outstanding command slots are not available, and a
603  * head, tail, and entries free counter to keep track of which (if any)
604  * command slots are currently free.  This is used (along with the cmd_indx,
605  * cmd_next, and cmd_prev fields in the hermon_cmd_t) to implement the fast
606  * allocation mechanism.
607  */
608 typedef struct hermon_cmdlist_s {
609 	kmutex_t		cml_lock;
610 	kcondvar_t		cml_cv;
611 	hermon_cmd_t		*cml_cmd;
612 	uint_t			cml_list_sz;
613 	uint_t			cml_num_alloc;
614 	uint_t			cml_head_indx;
615 	uint_t			cml_tail_indx;
616 	uint_t			cml_entries_free;
617 	uint_t			cml_waiters;
618 } hermon_cmdlist_t;
619 _NOTE(MUTEX_PROTECTS_DATA(hermon_cmdlist_t::cml_lock,
620     hermon_cmdlist_t::cml_cv
621     hermon_cmdlist_t::cml_cmd
622     hermon_cmdlist_t::cml_list_sz
623     hermon_cmdlist_t::cml_num_alloc
624     hermon_cmdlist_t::cml_head_indx
625     hermon_cmdlist_t::cml_tail_indx
626     hermon_cmdlist_t::cml_entries_free
627     hermon_cmdlist_t::cml_waiters
628     hermon_cmd_t::cmd_next
629     hermon_cmd_t::cmd_prev))
630 _NOTE(LOCK_ORDER(hermon_cmdlist_t::cml_lock
631     hermon_cmd_t::cmd_comp_lock))
632 
633 /*
634  * The hermon_cmd_post_t structure is used by all the Hermon Firmware Command
635  * routines to post to Hermon firmware.  The fields almost exactly mimic
636  * the fields in the Hermon HCR registers.  The notable exception is the
637  * addition of the "cp_flags" field (which can be set to HERMON_CMD_SPIN or
638  * HERMON_CMD_NOSPIN).  This flag really controls the value of the "e" bit
639  * in the HCR (i.e. the bit to indicate whether command should complete
640  * "in place" - in the HCR - or whether they should have their completions
641  * written to the command completion event queue.  HERMON_CMD_SPIN means
642  * to allow commands to complete "in place" and to poll the "go" bit in
643  * the HCR to determine completion.
644  *
645  * We use HERMON_SLEEP and HERMON_NOSLEEP for our HERMON_CMD_ #defines.  This is
646  * to maintain consistency with the rest of the SLEEP flags.  Additionally,
647  * because HERMON_SLEEPFLAG_FOR_CONTEXT() in hermon_rsrc.h returns HERMON_SLEEP
648  * or NOSLEEP we must be compatible with this macro.
649  */
650 typedef struct hermon_cmd_post_s {
651 	uint64_t		cp_inparm;
652 	uint64_t		cp_outparm;
653 	uint32_t		cp_inmod;
654 	uint16_t		cp_opcode;
655 	uint16_t		cp_opmod;
656 	uint32_t		cp_flags;
657 } hermon_cmd_post_t;
658 #define	HERMON_CMD_SLEEP_NOSPIN		HERMON_SLEEP
659 #define	HERMON_CMD_NOSLEEP_SPIN		HERMON_NOSLEEP
660 
661 
662 /*
663  * The following are the Hermon Firmware Command routines that accessible
664  * externally (i.e. throughout the rest of the Hermon driver software).
665  * These include the all the alloc/free routines, some initialization
666  * and cleanup routines, and the various specific Hermon firmware commands.
667  */
668 int hermon_cmd_post(hermon_state_t *state, hermon_cmd_post_t *cmdpost);
669 int hermon_mbox_alloc(hermon_state_t *state, hermon_mbox_info_t *mbox_info,
670     uint_t mbox_wait);
671 void hermon_mbox_free(hermon_state_t *state, hermon_mbox_info_t *mbox_info);
672 int hermon_cmd_complete_handler(hermon_state_t *state, hermon_eqhdl_t eq,
673     hermon_hw_eqe_t *eqe);
674 int hermon_inmbox_list_init(hermon_state_t *state);
675 int hermon_intr_inmbox_list_init(hermon_state_t *state);
676 int hermon_outmbox_list_init(hermon_state_t *state);
677 int hermon_intr_outmbox_list_init(hermon_state_t *state);
678 void hermon_inmbox_list_fini(hermon_state_t *state);
679 void hermon_intr_inmbox_list_fini(hermon_state_t *state);
680 void hermon_outmbox_list_fini(hermon_state_t *state);
681 void hermon_intr_outmbox_list_fini(hermon_state_t *state);
682 int hermon_outstanding_cmdlist_init(hermon_state_t *state);
683 void hermon_outstanding_cmdlist_fini(hermon_state_t *state);
684 
685 /* Added for MemFree */
686 int hermon_map_cmd_post(hermon_state_t *state, hermon_dma_info_t *dinfo,
687     uint16_t opcode, ddi_dma_cookie_t cookie, uint_t ccount);
688 int hermon_map_fa_cmd_post(hermon_state_t *state);
689 int hermon_run_fw_cmd_post(hermon_state_t *state);
690 int hermon_set_icm_size_cmd_post(hermon_state_t *state);
691 int hermon_map_icm_aux_cmd_post(hermon_state_t *state);
692 int hermon_map_icm_cmd_post(hermon_state_t *state);
693 int hermon_disable_lam_cmd_post(hermon_state_t *state);
694 int hermon_unmap_icm_cmd_post(hermon_state_t *state,
695     hermon_dma_info_t *dma_info);
696 int hermon_unmap_icm_aux_cmd_post(hermon_state_t *state);
697 int hermon_unmap_fa_cmd_post(hermon_state_t *state);
698 
699 /*
700  * INIT_HCA and CLOSE_HCA - used for initialization and teardown of Hermon
701  * device configuration
702  */
703 int hermon_init_hca_cmd_post(hermon_state_t *state,
704     hermon_hw_initqueryhca_t *inithca, uint_t sleepflag);
705 int hermon_close_hca_cmd_post(hermon_state_t *state, uint_t sleepflag);
706 
707 /*
708  * INIT_PORT, CLOSE_PORT, and SET_PORT - used for bring Hermon ports up and
709  * down, and to set properties of each port (e.g. PortInfo capability mask)
710  * NOTE:  New names for the commands in Hermon (previously init_ close_ and
711  * set_ib
712  */
713 int hermon_set_port_cmd_post(hermon_state_t *state,
714     hermon_hw_set_port_t *initport, uint_t port, uint_t sleepflag);
715 int hermon_init_port_cmd_post(hermon_state_t *state, uint_t port,
716     uint_t sleepflag);
717 int hermon_close_port_cmd_post(hermon_state_t *state, uint_t port,
718     uint_t sleepflag);
719 
720 /*
721  * This common function is used to post the following Hermon QP state
722  * transition firmware commands:
723  * RTS2SQD, TOERR, TORST, RST2INIT, INIT2INIT, INIT2RTR, RTR2RTS, RTS2RTS,
724  * SQD2SQD, SQD2RTS, and SQERR2RTS.
725  */
726 int hermon_cmn_qp_cmd_post(hermon_state_t *state, uint_t opcode,
727     hermon_hw_qpc_t *qp, uint_t qpindx, uint32_t opmask, uint_t sleepflag);
728 
729 /*
730  * This common function is used to post the following Hermon query firmware
731  * commands:
732  * QUERY_DEV_LIM/CAP, QUERY_FW, QUERY_ADAPTER, QUERY_HCA, QUERY_MPT,
733  * QUERY_EQ, QUERY_CQ, and QUERY_QP.
734  */
735 int hermon_cmn_query_cmd_post(hermon_state_t *state, uint_t opcode,
736     uint_t opmod, uint_t queryindx, void *query, uint_t size, uint_t sleepflag);
737 
738 /*
739  * This common function is used to post the following Hermon resource ownership
740  * firmware commands:
741  * HW2SW_MPT, HW2SW_EQ, HW2SW_CQ, SW2HW_MPT, SW2HW_EQ, and SW2HW_CQ
742  */
743 int hermon_cmn_ownership_cmd_post(hermon_state_t *state, uint_t opcode,
744     void *hwrsrc, uint_t size, uint_t hwrsrcindx, uint_t sleepflag);
745 
746 /*
747  * MAD_IFC and helper functions - used for posting IB MADs to Hermon firmware.
748  * The helper functions are for the MADs most frequently used by the Hermon
749  * driver (internally).
750  */
751 int hermon_mad_ifc_cmd_post(hermon_state_t *state, uint_t port,
752     uint_t sleepflag, uint32_t *mad, uint32_t *resp);
753 int hermon_getportinfo_cmd_post(hermon_state_t *state, uint_t port,
754     uint_t sleepflag, sm_portinfo_t *portinfo);
755 int hermon_getnodeinfo_cmd_post(hermon_state_t *state, uint_t sleepflag,
756     sm_nodeinfo_t *nodeinfo);
757 int hermon_getnodedesc_cmd_post(hermon_state_t *state, uint_t sleepflag,
758     sm_nodedesc_t *nodedesc);
759 int hermon_getguidinfo_cmd_post(hermon_state_t *state, uint_t port,
760     uint_t guidblock, uint_t sleepflag, sm_guidinfo_t *guidinfo);
761 int hermon_getpkeytable_cmd_post(hermon_state_t *state, uint_t port,
762     uint_t pkeyblock, uint_t sleepflag, sm_pkey_table_t *pkeytable);
763 int hermon_is_ext_port_counters_supported(hermon_state_t *state, uint_t port,
764     uint_t sleepflag, int *ext_width_supported);
765 int hermon_getextperfcntr_cmd_post(hermon_state_t *state, uint_t port,
766     uint_t sleepflag, hermon_hw_sm_extperfcntr_t *perfinfo);
767 int hermon_getperfcntr_cmd_post(hermon_state_t *state, uint_t port,
768     uint_t sleepflag, hermon_hw_sm_perfcntr_t *perfinfo, int reset);
769 /*
770  * WRITE_MTT - used for write MTT entries to the Hermon MTT table
771  */
772 int hermon_write_mtt_cmd_post(hermon_state_t *state, hermon_rsrc_t *mtt,
773     uint64_t start_addr, uint_t nummtt, uint_t sleepflag);
774 
775 /*
776  * SYNC_TPT - used to sync Hermon TPT caches
777  */
778 int hermon_sync_tpt_cmd_post(hermon_state_t *state, uint_t sleepflag);
779 
780 /*
781  * MAP_EQ - used for map classes of events to Hermon event queues (EQ)
782  */
783 int hermon_map_eq_cmd_post(hermon_state_t *state, uint_t map,
784     uint_t eqcindx, uint64_t eqmapmask, uint_t sleepflag);
785 
786 /*
787  * RESIZE_CQ - used for resize completion queue (CQ)
788  *	opmod 0 is resize cq.  opmod 1 is modify interrupt moderation.
789  */
790 int hermon_resize_cq_cmd_post(hermon_state_t *state, hermon_hw_cqc_t *cqc,
791     uint_t cqcindx, uint32_t *prod_indx, uint_t sleepflag);
792 int hermon_modify_cq_cmd_post(hermon_state_t *state, hermon_hw_cqc_t *cqc,
793     uint_t cqcindx, uint_t opmod, uint_t sleepflag);
794 
795 /*
796  * CONF_SPECIAL_QP - used to configure a pair of queue pairs for use as
797  * special QP.  Necessary to enable full QP0 and/or QP1 operation.
798  */
799 int hermon_conf_special_qp_cmd_post(hermon_state_t *state, uint_t qpindx,
800     uint_t qptype, uint_t sleepflag, uint_t opmod);
801 
802 /*
803  * MGID_HASH, READ_MGM, and WRITE_MGM - used for manipulation of the
804  * hardware resource tables for multicast groups.
805  *	NOTE: for intial implementation these functions retain their original
806  *		names, though the proper hermon terminology is READ_MCG and
807  *		WRITE_MCG - MGID_HASH retains its original name
808  */
809 int hermon_mgid_hash_cmd_post(hermon_state_t *state, uint64_t mgid_h,
810     uint64_t mgid_l, uint64_t *mgid_hash, uint_t sleepflag);
811 int hermon_read_mgm_cmd_post(hermon_state_t *state, hermon_hw_mcg_t *mcg,
812     uint_t mcgindx, uint_t sleepflag);
813 int hermon_write_mgm_cmd_post(hermon_state_t *state, hermon_hw_mcg_t *mcg,
814     uint_t mcgindx, uint_t sleepflag);
815 
816 /*
817  * MOD_STAT_CFG - used to configure (override) settings set in NVRAM before
818  * a call to QUERY_DEV_LIM.  This is primarily used for SRQ settings in
819  * the firmware.
820  */
821 int hermon_mod_stat_cfg_cmd_post(hermon_state_t *state);
822 
823 /*
824  * MODIFY_MPT - used to change MPT attributes of a memory region.  This
825  * was (Tavor/Arbel) primarily used for Resizing SRQs -- now may be used
826  * to modify MPT paramters
827  */
828 int hermon_modify_mpt_cmd_post(hermon_state_t *state, hermon_hw_dmpt_t *mpt,
829     uint_t mptindx, uint_t flags, uint_t sleepflag);
830 
831 /*
832  * RESIZE_SRQ is new in hermon, replacing opcodes in modify_mpt.  It is used
833  * to resize the SRQ, by passing the new information in the same format as
834  * the original srqc, which the HCA will update appropriately
835  */
836 
837 int hermon_resize_srq_cmd_post(hermon_state_t *state, hermon_hw_srqc_t *srq,
838     uint_t srqnum, uint_t sleepflag);
839 
840 /*
841  * CMD_NOP - used to test the interrupt/Event Queue mechanism.
842  */
843 int hermon_nop_post(hermon_state_t *state, uint_t interval, uint_t sleep);
844 int hermon_setdebug_post(hermon_state_t *state);
845 /*
846  * READ_MTT - used to read an mtt entry at address.
847  */
848 int hermon_read_mtt_cmd_post(hermon_state_t *state, uint64_t mtt_addr,
849     hermon_hw_mtt_t *mtt);
850 
851 #ifdef __cplusplus
852 }
853 #endif
854 
855 #endif	/* _SYS_IB_ADAPTERS_HERMON_CMD_H */
856