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