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