xref: /illumos-gate/usr/src/uts/sun4v/sys/ldc_impl.h (revision a66004590c61514e788e33c934c7fe7c0ec4c1d4)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _LDC_IMPL_H
28 #define	_LDC_IMPL_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <sys/types.h>
37 #include <sys/ddi.h>
38 #include <sys/sunddi.h>
39 #include <sys/ioctl.h>
40 
41 /* Memory map table size */
42 #define	MTBL_MAX_SIZE		65536	/* 64K */
43 
44 /* Define LDC Queue info */
45 #define	LDC_PACKET_SHIFT	6
46 #define	LDC_QUEUE_ENTRIES	128
47 #define	LDC_QUEUE_SIZE		(LDC_QUEUE_ENTRIES << LDC_PACKET_SHIFT)
48 #define	LDC_STREAM_MTU		(LDC_QUEUE_SIZE >> 1)
49 
50 /*
51  * LDC Reliable mode - initial packet seqid
52  * - If peer initiated handshake, RDX should contain init_seqid + 1
53  * - If this endpoint initiated handshake first data packet should
54  *   contain the message init_seqid + 1
55  */
56 #define	LDC_INIT_SEQID	0x0
57 
58 /* LDC Message types */
59 #define	LDC_CTRL	0x01	/* Control Pkt */
60 #define	LDC_DATA	0x02	/* Data Pkt */
61 #define	LDC_ERR		0x10	/* Error Pkt */
62 
63 /* LDC Message Subtypes */
64 #define	LDC_INFO	0x01	/* Control/Data/Error info pkt */
65 #define	LDC_ACK		0x02	/* Control/Data ACK */
66 #define	LDC_NACK	0x04	/* Control/Data NACK */
67 
68 /* LDC Control Messages */
69 #define	LDC_VER		0x01	/* Version message */
70 #define	LDC_RTS		0x02	/* Request to Send */
71 #define	LDC_RTR		0x03	/* Ready To Receive */
72 #define	LDC_RDX		0x04	/* Ready for data exchange */
73 
74 #define	LDC_CTRL_MASK	0x0f	/* Mask to read control bits */
75 
76 /* LDC Channel Transport State (tstate) */
77 #define	TS_TXQ_RDY	0x01	/* allocated TX queue */
78 #define	TS_RXQ_RDY	0x02	/* allocated RX queue */
79 #define	TS_INIT		(TS_TXQ_RDY | TS_RXQ_RDY)
80 #define	TS_QCONF_RDY	0x04	/* registered queues with HV */
81 #define	TS_CNEX_RDY	0x08	/* registered channel with cnex */
82 #define	TS_OPEN		(TS_INIT | TS_QCONF_RDY | TS_CNEX_RDY)
83 #define	TS_LINK_READY	0x10	/* both endpts registered Rx queues */
84 #define	TS_READY	(TS_OPEN | TS_LINK_READY)
85 #define	TS_VER_DONE	0x20	/* negotiated version */
86 #define	TS_VREADY	(TS_READY | TS_VER_DONE)
87 #define	TS_HSHAKE_DONE	0x40	/* completed handshake */
88 #define	TS_UP		(TS_READY | TS_VER_DONE | TS_HSHAKE_DONE)
89 
90 /*  LDC Channel Transport Handshake states */
91 #define	TS_SENT_RTS	0x01	/* Sent RTS */
92 #define	TS_RCVD_RTR	0x02	/* Received RTR */
93 #define	TS_SENT_RDX	0x04	/* Sent RDX */
94 #define	TS_RCVD_RTS	0x10	/* Received RTS */
95 #define	TS_SENT_RTR	0x20	/* Sent RTR */
96 #define	TS_RCVD_RDX	0x40	/* Received RDX */
97 
98 /* LDC MSG Envelope */
99 #define	LDC_LEN_MASK	0x3F
100 #define	LDC_FRAG_MASK	0xC0
101 
102 #define	LDC_FRAG_START	0x40	/* frag_info = 0x01 */
103 #define	LDC_FRAG_STOP	0x80	/* frag_info = 0x02 */
104 #define	LDC_FRAG_CONT	0x00	/* frag_info = 0x00 */
105 
106 /*
107  * LDC fragmented xfer loop wait cnt
108  * When data is arriving in fragments, the read thread will
109  * look for a packet 'LDC_CHK_CNT' times. Between each check
110  * it will loop 'LDC_LOOP_CNT' times
111  */
112 #define	LDC_CHK_CNT	1000
113 #define	LDC_LOOP_CNT	1000
114 
115 /*
116  * LDC Version information
117  */
118 #define	LDC_PAYLOAD_VER_OFF	8	/* offset of version in payload */
119 
120 typedef struct ldc_ver {
121 	uint16_t	major;
122 	uint16_t	minor;
123 } ldc_ver_t;
124 
125 /*
126  * Each guest consists of one or more LDC endpoints represented by a ldc_chan
127  * structure. Each ldc_chan structure points to a ldc_mtbl structure that
128  * contains information about the map table associated with this LDC endpoint.
129  * The map table contains the list of pages being shared by this guest over
130  * this endpoint with the guest at the other end of this endpoint. Each LDC
131  * endpoint also points to a list of memory handles used to bind and export
132  * memory segments from this guest. If a memory segment is bound, it points to
133  * a memory segment structure, which inturn consists of an array of ldc_page
134  * structure for all the pages within that segment. Each ldc_page structure
135  * contains information about the shared page and also points to the
136  * corresponding entry in the map table.
137  *
138  * Each LDC endpoint also points to a list of ldc_dring structures that refer
139  * to both imported and exported descriptor rings. If it is a exported
140  * descriptor ring, it then points to memory handle/memseg corresponding to
141  * the region of memory associated with the descriptor ring.
142  *
143  *     +----------+   +----------+   +----------+
144  *     | ldc_chan |-->| ldc_chan |-->| ldc_chan |-->....
145  *     +----------+   +----------+   +----------+
146  *       |  |  |
147  *       |  |  |
148  *       |  |  |      +-----------+     +-----------+
149  *       |  |  +----->| ldc_dring |---->| ldc_dring |---->......
150  *       |  |         +-----------+     +-----------+
151  *       |  |               |
152  *       |  |               +----------------------------+
153  *       |  |                                            |
154  *       |  |                                            v
155  *       |  |      +----------+     +----------+     +----------+
156  *       |  +----->| ldc_mhdl |---->| ldc_mhdl |---->| ldc_mhdl |---> ....
157  *       |         +----------+     +----------+     +----------+
158  *       v                 |                             |
159  *  +----------+           |    +------------+           |    +------------+
160  *  | ldc_mtbl |--+        +--->| ldc_memseg |-----+     +--->| ldc_memseg |
161  *  +----------+  |             +------------+     |          +------------+
162  *                |                   |            |            |       |
163  *                v                   v            v            |       v
164  *     +--------------+         +----------+  +--------+        |   +--------+
165  *     | ldc_mte_slot |<--------| ldc_page |  | cookie |        |   | cookie |
166  *     +--------------+         +----------+  +--------+        |   +--------+
167  *     | ldc_mte_slot |<--------| ldc_page |  | cookie |        v
168  *     +--------------+         +----------+  +--------+   +----------+
169  *     | ldc_mte_slot |<-----------------------------------| ldc_page |
170  *     +--------------+                                    +----------+
171  *     | ldc_mte_slot |
172  *     +--------------+
173  *     |    ......    |/ +------------+
174  *     +--------------+  |   entry    |
175  *     | ldc_mte_slot |  +------------+
176  *     +--------------+  | inv_cookie |
177  *                     \ +------------+
178  *
179  */
180 
181 /*
182  * Message format of each packet sent over the LDC channel.
183  * Each packet is 64-bytes long.
184  *
185  * Each packet that is sent over LDC can contain either data or acks.
186  * The type will reflect the contents. The len will contain in bytes
187  * the amount of data being sent. In the case of ACKs, the seqid and
188  * data fields will contain the SEQIDs of messages for which ACKs are
189  * being sent.
190  *
191  * Raw pkt format:
192  *
193  *          +------------------------------------------------------+
194  *  0 - 7   |                 data payload                         |
195  *          +------------------------------------------------------+
196  *
197  * Unreliable pkt format:
198  *
199  *          +------------------------------------------------------+
200  *      0   |          seqid          | env  | ctrl | stype | type |
201  *          +------------------------------------------------------+
202  *  1 - 7   |                 data payload                         |
203  *          +------------------------------------------------------+
204  *
205  * Reliable pkt format:
206  *
207  *          +------------------------------------------------------+
208  *      0   |            seqid        | env  | ctrl | stype | type |
209  *          +------------------------------------------------------+
210  *      1   |          ackid          |         unused             |
211  *          +------------------------------------------------------+
212  *  2 - 7   |                 data payload                         |
213  *          +------------------------------------------------------+
214  */
215 
216 typedef struct ldc_msg {
217 	union {
218 		struct {
219 			uint8_t		_type;	/* Message type */
220 			uint8_t		_stype;	/* Message subtype */
221 			uint8_t		_ctrl;	/* Control/Error Message */
222 			uint8_t 	_env;	/* Message Envelope */
223 			uint32_t	_seqid;	/* Sequence ID */
224 
225 			union {
226 				uint8_t	_ud[LDC_PAYLOAD_SIZE_UNRELIABLE];
227 						/* Unreliable data payload */
228 				struct {
229 					uint32_t _unused;	/* unused */
230 					uint32_t _ackid;	/* ACK ID */
231 					uint8_t	_rd[LDC_PAYLOAD_SIZE_RELIABLE];
232 						/* Reliable data payload */
233 				} _rl;
234 			} _data;
235 		} _tpkt;
236 
237 		uint8_t		_raw[LDC_PAYLOAD_SIZE_RAW];
238 	} _pkt;
239 
240 } ldc_msg_t;
241 
242 #define	raw		_pkt._raw
243 #define	type		_pkt._tpkt._type
244 #define	stype		_pkt._tpkt._stype
245 #define	ctrl		_pkt._tpkt._ctrl
246 #define	env		_pkt._tpkt._env
247 #define	seqid		_pkt._tpkt._seqid
248 #define	udata		_pkt._tpkt._data._ud
249 #define	ackid		_pkt._tpkt._data._rl._ackid
250 #define	rdata		_pkt._tpkt._data._rl._rd
251 
252 /*
253  * LDC Map Table Entry (MTE)
254  *
255  *   6    6                               1    1  1
256  *  |3    0|                       psz|   3|   1| 0| 9| 8| 7|6|5|4|      0|
257  *  +------+--------------------------+----+----+--+--+--+--+-+-+-+-------+
258  *  | rsvd |           PFN            | 0  | 0  |CW|CR|IW|IR|X|W|R| pgszc |
259  *  +------+--------------------------+----+----+--+--+--+--+-+-+-+-------+
260  *  |                       hv invalidation cookie                        |
261  *  +---------------------------------------------------------------------+
262  */
263 typedef union {
264 	struct {
265 		uint64_t	_rsvd2:8,	/* <63:56> reserved */
266 				rpfn:43,	/* <55:13> real pfn */
267 				_rsvd1:2,	/* <12:11> reserved */
268 				cw:1,		/* <10> copy write access */
269 				cr:1,		/* <9> copy read perm */
270 				iw:1,		/* <8> iommu write perm */
271 				ir:1,		/* <7> iommu read perm */
272 				x:1,		/* <6> execute perm */
273 				w:1,		/* <5> write perm */
274 				r:1,		/* <4> read perm */
275 				pgszc:4;	/* <3:0> pgsz code */
276 	} mte_bit;
277 
278 	uint64_t 		ll;
279 
280 } ldc_mte_t;
281 
282 #define	mte_rpfn	mte_bit.rpfn
283 #define	mte_cw		mte_bit.cw
284 #define	mte_cr		mte_bit.cr
285 #define	mte_iw		mte_bit.iw
286 #define	mte_ir		mte_bit.ir
287 #define	mte_x		mte_bit.x
288 #define	mte_w		mte_bit.w
289 #define	mte_r		mte_bit.r
290 #define	mte_pgszc	mte_bit.pgszc
291 
292 #define	MTE_BSZS_SHIFT(sz)	((sz) * 3)
293 #define	MTEBYTES(sz)    	(MMU_PAGESIZE << MTE_BSZS_SHIFT(sz))
294 #define	MTEPAGES(sz)    	(1 << MTE_BSZS_SHIFT(sz))
295 #define	MTE_PAGE_SHIFT(sz)	(MMU_PAGESHIFT + MTE_BSZS_SHIFT(sz))
296 #define	MTE_PAGE_OFFSET(sz)	(MTEBYTES(sz) - 1)
297 #define	MTE_PAGEMASK(sz)	(~MTE_PAGE_OFFSET(sz))
298 #define	MTE_PFNMASK(sz)		(~(MTE_PAGE_OFFSET(sz) >> MMU_PAGESHIFT))
299 
300 /*
301  * LDC Map Table Slot
302  */
303 typedef struct ldc_mte_slot {
304 	ldc_mte_t	entry;
305 	uint64_t	cookie;
306 } ldc_mte_slot_t;
307 
308 /*
309  * LDC Memory Map Table
310  *
311  * Each LDC has a memory map table it uses to list all the pages
312  * it exporting to its peer over the channel. This structure
313  * contains information about the map table and is pointed to
314  * by the ldc_chan structure.
315  */
316 typedef struct ldc_mtbl {
317 	kmutex_t		lock;		/* Table lock */
318 	size_t			size;		/* Table size (in bytes) */
319 	uint64_t		next_entry;	/* Next entry to use */
320 	uint64_t		num_entries;	/* Num entries in table */
321 	uint64_t		num_avail;	/* Num of available entries */
322 	ldc_mte_slot_t		*table;		/* The table itself */
323 } ldc_mtbl_t;
324 
325 /*
326  * LDC page and memory segment information
327  */
328 typedef struct ldc_page {
329 	uintptr_t		raddr;		/* Exported page RA */
330 	uint64_t		offset;		/* Exported page offset */
331 	size_t			size;		/* Exported page size */
332 	uint64_t		index;		/* Index in map table */
333 	ldc_mte_slot_t		*mte;		/* Map table entry */
334 } ldc_page_t;
335 
336 typedef struct ldc_memseg {
337 	caddr_t			vaddr;		/* Exported segment VA */
338 	uintptr_t		raddr;		/* Exported segment VA */
339 	size_t			size;		/* Exported segment size */
340 	uint64_t		npages;		/* Number of pages */
341 	ldc_page_t		*pages;		/* Array of exported pages */
342 	uint32_t		ncookies;	/* Number of cookies */
343 	ldc_mem_cookie_t	*cookies;
344 	uint64_t		next_cookie;	/* Index to next cookie */
345 } ldc_memseg_t;
346 
347 /*
348  * LDC Cookie address format
349  *
350  *   6       6          m+n
351  *  |3|      0|          |                  m|                  0|
352  *  +-+-------+----------+-------------------+-------------------+
353  *  |X| pgszc |   rsvd   |      table_idx    |     page_offset   |
354  *  +-+-------+----------+-------------------+-------------------+
355  */
356 #define	LDC_COOKIE_PGSZC_MASK	0x7
357 #define	LDC_COOKIE_PGSZC_SHIFT	60
358 
359 /*
360  * LDC Memory handle
361  */
362 typedef struct ldc_chan ldc_chan_t;
363 
364 typedef struct ldc_mhdl {
365 	kmutex_t		lock;		/* Mutex for memory handle */
366 	ldc_mstatus_t		status;		/* Memory map status */
367 
368 	uint8_t			mtype;		/* Type of sharing */
369 	uint8_t			perm;		/* Access permissions */
370 	boolean_t		myshadow;	/* TRUE=alloc'd shadow mem */
371 
372 	ldc_chan_t		*ldcp;		/* Pointer to channel struct */
373 	ldc_memseg_t		*memseg;	/* Bound memory segment */
374 	struct ldc_mhdl		*next;		/* Next memory handle */
375 } ldc_mhdl_t;
376 
377 /*
378  * LDC Descriptor rings
379  */
380 
381 typedef struct ldc_dring {
382 	kmutex_t		lock;		/* Desc ring lock */
383 	ldc_mstatus_t		status;		/* Desc ring status */
384 
385 	uint32_t		dsize;		/* Descriptor size */
386 	uint32_t		length;		/* Descriptor ring length */
387 	uint64_t		size;		/* Desc ring size (in bytes) */
388 	caddr_t			base;		/* Descriptor ring base addr */
389 
390 	ldc_chan_t		*ldcp;		/* Pointer to bound channel */
391 	ldc_mem_handle_t	mhdl;		/* Mem handle to desc ring */
392 
393 	struct ldc_dring	*ch_next;	/* Next dring in channel */
394 	struct ldc_dring 	*next;		/* Next dring overall */
395 
396 } ldc_dring_t;
397 
398 
399 /*
400  * Channel specific information is kept in a separate
401  * structure. These are then stored on a array indexed
402  * by the channel number.
403  */
404 struct ldc_chan {
405 	ldc_chan_t	*next;		/* Next channel */
406 
407 	kmutex_t	lock;		/* Channel lock */
408 	uint64_t	id;		/* Channel ID */
409 	ldc_status_t	status;		/* Channel status */
410 	uint32_t	tstate;		/* Channel transport state */
411 	uint32_t	hstate;		/* Channel transport handshake state */
412 
413 	ldc_dev_t	devclass;	/* Associated device class */
414 	uint64_t	devinst;	/* Associated device instance */
415 	ldc_mode_t	mode;		/* Channel mode */
416 
417 	uint64_t	mtu;		/* Max TU size (streaming for now) */
418 
419 	ldc_ver_t	version;	/* Channel version */
420 	uint32_t	next_vidx;	/* Next version to match */
421 
422 	uint_t		(*cb)(uint64_t event, caddr_t arg);
423 	caddr_t		cb_arg;		/* Channel callback and arg */
424 	boolean_t	cb_inprogress;	/* Channel callback in progress */
425 	boolean_t	cb_enabled;	/* Channel callbacks are enabled */
426 
427 	boolean_t	intr_pending;	/* TRUE if interrupts are pending */
428 
429 	uint64_t	tx_q_entries;	/* Num entries in transmit queue */
430 	uint64_t	tx_q_va;	/* Virtual addr of transmit queue */
431 	uint64_t	tx_q_ra;	/* Real addr of transmit queue */
432 	uint64_t	tx_head;	/* Tx queue head */
433 	uint64_t	tx_ackd_head;	/* Tx queue ACKd head (Reliable) */
434 	uint64_t	tx_tail;	/* Tx queue tail */
435 
436 	uint64_t	rx_q_entries;	/* Num entries in receive queue */
437 	uint64_t	rx_q_va;	/* Virtual addr of receive queue */
438 	uint64_t	rx_q_ra;	/* Real addr of receive queue */
439 
440 	uint64_t	link_state;	/* Underlying HV channel state */
441 
442 	ldc_mtbl_t	*mtbl;		/* Memory table used by channel */
443 	ldc_mhdl_t	*mhdl_list;	/* List of memory handles */
444 	kmutex_t	mlist_lock;	/* Mem handle list lock */
445 
446 	ldc_dring_t	*exp_dring_list; /* Exported desc ring list */
447 	kmutex_t	exp_dlist_lock;	/* Lock for exported desc ring list */
448 	ldc_dring_t	*imp_dring_list; /* Imported desc ring list */
449 	kmutex_t	imp_dlist_lock;	/* Lock for imported desc ring list */
450 
451 	uint8_t		pkt_payload;	/* Size of packet payload */
452 
453 	uint32_t	first_fragment;	/* Seqid of first msg fragment */
454 	uint32_t	last_msg_snt;	/* Seqid of last packet sent */
455 	uint32_t	last_ack_rcd;	/* Seqid of last ACK recd */
456 	uint32_t	last_msg_rcd;	/* Seqid of last packet received */
457 
458 	uint32_t	stream_remains;	/* Number of bytes in stream */
459 					/* packet buffer */
460 	uint32_t	stream_offset;	/* Offset into packet buffer for */
461 					/* next read */
462 	uint8_t		*stream_bufferp; /* Stream packet buffer */
463 
464 	int		(*read_p)(ldc_chan_t *ldcp, caddr_t bufferp,
465 				size_t *sizep);
466 	int		(*write_p)(ldc_chan_t *ldcp, caddr_t bufferp,
467 				size_t *sizep);
468 };
469 
470 
471 /*
472  * LDC module soft state structure
473  */
474 typedef struct ldc_soft_state {
475 	kmutex_t 	lock;		/* Protects ldc_soft_state_t  */
476 	ldc_cnex_t	cinfo;		/* channel nexus info */
477 	uint64_t	channel_count;	/* Number of channels */
478 	uint64_t	channels_open;	/* Number of open channels */
479 	ldc_chan_t 	*chan_list;	/* List of LDC endpoints */
480 	ldc_dring_t	*dring_list;	/* Descriptor rings (for export) */
481 } ldc_soft_state_t;
482 
483 #ifdef __cplusplus
484 }
485 #endif
486 
487 #endif /* _LDC_IMPL_H */
488