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