xref: /titanic_51/usr/src/uts/sun4v/sys/ldc.h (revision 134a1f4e3289b54e0f980e9cf05352e419a60bee)
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 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _LDC_H
28 #define	_LDC_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #include <sys/types.h>
35 #include <sys/ddi.h>
36 #include <sys/sunddi.h>
37 #include <sys/ioctl.h>
38 #include <sys/processor.h>
39 #include <sys/ontrap.h>
40 
41 /* Types */
42 typedef uint64_t ldc_handle_t;		/* Channel handle */
43 typedef uint64_t ldc_mem_handle_t;	/* Channel memory handle */
44 typedef uint64_t ldc_dring_handle_t;	/* Descriptor ring handle */
45 
46 /* LDC transport mode */
47 typedef enum {
48 	LDC_MODE_RAW,			/* Raw mode */
49 	LDC_MODE_UNRELIABLE,		/* Unreliable packet mode */
50 	_LDC_MODE_RESERVED_,		/* reserved */
51 	LDC_MODE_RELIABLE		/* Reliable packet mode */
52 } ldc_mode_t;
53 
54 /* LDC message payload sizes */
55 #define	LDC_ELEM_SIZE			8		/* size in bytes */
56 #define	LDC_PACKET_SIZE			(LDC_ELEM_SIZE * 8)
57 #define	LDC_PAYLOAD_SIZE_RAW		(LDC_PACKET_SIZE)
58 #define	LDC_PAYLOAD_SIZE_UNRELIABLE	(LDC_PACKET_SIZE - LDC_ELEM_SIZE)
59 #define	LDC_PAYLOAD_SIZE_RELIABLE	(LDC_PACKET_SIZE - (LDC_ELEM_SIZE * 2))
60 
61 /* LDC Channel Status */
62 typedef enum {
63 	LDC_INIT = 1,			/* Channel initialized */
64 	LDC_OPEN,			/* Channel open */
65 	LDC_READY,			/* Channel peer opened (hw-link-up) */
66 	LDC_UP				/* Channel UP - ready for data xfer */
67 } ldc_status_t;
68 
69 /* Callback return values */
70 #define	LDC_SUCCESS	0
71 #define	LDC_FAILURE	1
72 
73 /* LDC callback mode */
74 typedef enum {
75 	LDC_CB_ENABLE,			/* Enable callbacks */
76 	LDC_CB_DISABLE			/* Disable callbacks */
77 } ldc_cb_mode_t;
78 
79 /* Callback events */
80 #define	LDC_EVT_DOWN		0x1	/* Channel DOWN, status = OPEN */
81 #define	LDC_EVT_RESET		0x2	/* Channel RESET, status = READY */
82 #define	LDC_EVT_UP		0x4	/* Channel UP, status = UP */
83 #define	LDC_EVT_READ		0x8	/* Channel has data for read */
84 #define	LDC_EVT_WRITE		0x10	/* Channel has space for write */
85 
86 /* LDC device classes */
87 typedef enum {
88 	LDC_DEV_GENERIC = 1,		/* generic device */
89 	LDC_DEV_BLK,			/* block device, eg. vdc */
90 	LDC_DEV_BLK_SVC,		/* block device service, eg. vds */
91 	LDC_DEV_NT,			/* network device, eg. vnet */
92 	LDC_DEV_NT_SVC,			/* network service eg. vsw */
93 	LDC_DEV_SERIAL			/* serial device eg. vldc, vcc */
94 } ldc_dev_t;
95 
96 /* Channel nexus registration */
97 typedef struct ldc_cnex {
98 	dev_info_t	*dip;		/* dip of channel nexus */
99 	int		(*reg_chan)();	/* interface for channel register */
100 	int		(*unreg_chan)(); /* interface for channel unregister */
101 	int		(*add_intr)();	/* interface for adding interrupts */
102 	int		(*rem_intr)();	/* interface for removing interrupts */
103 	int		(*clr_intr)();	/* interface for clearing interrupts */
104 } ldc_cnex_t;
105 
106 /* LDC attribute structure */
107 typedef struct ldc_attr {
108 	ldc_dev_t	devclass;	/* device class */
109 	uint64_t	instance;	/* device class instance */
110 	ldc_mode_t	mode;		/* channel mode */
111 	uint64_t	mtu;		/* channel mtu */
112 } ldc_attr_t;
113 
114 /* LDC memory cookie */
115 typedef struct ldc_mem_cookie {
116 	uint64_t	addr;		/* cookie address */
117 	uint64_t	size;		/* size @ offset */
118 } ldc_mem_cookie_t;
119 
120 /*
121  * LDC Memory Map Type
122  * Specifies how shared memory being created is shared with its
123  * peer and/or how the peer has mapped in the exported memory.
124  */
125 #define	LDC_SHADOW_MAP		0x1	/* share mem via shadow copy only */
126 #define	LDC_DIRECT_MAP		0x2	/* share mem direct access */
127 #define	LDC_IO_MAP		0x4	/* share mem for IOMMU/DMA access */
128 
129 /* LDC Memory Access Permissions  */
130 #define	LDC_MEM_R		0x1	/* Memory region is read only */
131 #define	LDC_MEM_W		0x2	/* Memory region is write only */
132 #define	LDC_MEM_X		0x4	/* Memory region is execute only */
133 #define	LDC_MEM_RW		(LDC_MEM_R|LDC_MEM_W)
134 #define	LDC_MEM_RWX		(LDC_MEM_R|LDC_MEM_W|LDC_MEM_X)
135 
136 /* LDC Memory Copy Direction */
137 #define	LDC_COPY_IN		0x0	/* Copy data to VA from cookie mem */
138 #define	LDC_COPY_OUT		0x1	/* Copy data from VA to cookie mem */
139 
140 /* LDC memory/dring (handle) status */
141 typedef enum {
142 	LDC_UNBOUND,			/* Memory handle is unbound */
143 	LDC_BOUND,			/* Memory handle is bound */
144 	LDC_MAPPED			/* Memory handle is mapped */
145 } ldc_mstatus_t;
146 
147 /* LDC [dring] memory info */
148 typedef struct ldc_mem_info {
149 	uint8_t		mtype;		/* map type */
150 	uint8_t		perm;		/* RWX permissions */
151 	caddr_t		vaddr;		/* base VA */
152 	uintptr_t	raddr;		/* base RA */
153 	ldc_mstatus_t	status;		/* dring/mem handle status */
154 } ldc_mem_info_t;
155 
156 /* API functions */
157 int ldc_register(ldc_cnex_t *cinfo);
158 int ldc_unregister(ldc_cnex_t *cinfo);
159 
160 int ldc_init(uint64_t id, ldc_attr_t *attr, ldc_handle_t *handle);
161 int ldc_fini(ldc_handle_t handle);
162 int ldc_open(ldc_handle_t handle);
163 int ldc_close(ldc_handle_t handle);
164 int ldc_up(ldc_handle_t handle);
165 int ldc_down(ldc_handle_t handle);
166 int ldc_reg_callback(ldc_handle_t handle,
167     uint_t(*callback)(uint64_t event, caddr_t arg), caddr_t arg);
168 int ldc_unreg_callback(ldc_handle_t handle);
169 int ldc_set_cb_mode(ldc_handle_t handle, ldc_cb_mode_t imode);
170 int ldc_chkq(ldc_handle_t handle, boolean_t *hasdata);
171 int ldc_read(ldc_handle_t handle, caddr_t buf, size_t *size);
172 int ldc_write(ldc_handle_t handle, caddr_t buf, size_t *size);
173 int ldc_status(ldc_handle_t handle, ldc_status_t *status);
174 
175 int ldc_mem_alloc_handle(ldc_handle_t handle, ldc_mem_handle_t *mhandle);
176 int ldc_mem_free_handle(ldc_mem_handle_t mhandle);
177 int ldc_mem_bind_handle(ldc_mem_handle_t mhandle, caddr_t vaddr, size_t len,
178     uint8_t mtype, uint8_t perm, ldc_mem_cookie_t *cookie, uint32_t *ccount);
179 int ldc_mem_unbind_handle(ldc_mem_handle_t mhandle);
180 int ldc_mem_info(ldc_mem_handle_t mhandle, ldc_mem_info_t *minfo);
181 int ldc_mem_nextcookie(ldc_mem_handle_t mhandle, ldc_mem_cookie_t *cookie);
182 int ldc_mem_copy(ldc_handle_t handle, caddr_t vaddr, uint64_t off, size_t *len,
183     ldc_mem_cookie_t *cookies, uint32_t ccount, uint8_t direction);
184 int ldc_mem_rdwr_cookie(ldc_handle_t handle, caddr_t vaddr, size_t *size,
185     caddr_t paddr, uint8_t  direction);
186 int ldc_mem_map(ldc_mem_handle_t mhandle, ldc_mem_cookie_t *cookie,
187     uint32_t ccount, uint8_t mtype, uint8_t perm, caddr_t *vaddr,
188     caddr_t *raddr);
189 int ldc_mem_unmap(ldc_mem_handle_t mhandle);
190 int ldc_mem_acquire(ldc_mem_handle_t mhandle, uint64_t offset, uint64_t size);
191 int ldc_mem_release(ldc_mem_handle_t mhandle, uint64_t offset, uint64_t size);
192 
193 int ldc_mem_dring_create(uint32_t len, uint32_t dsize,
194     ldc_dring_handle_t *dhandle);
195 int ldc_mem_dring_destroy(ldc_dring_handle_t dhandle);
196 int ldc_mem_dring_bind(ldc_handle_t handle, ldc_dring_handle_t dhandle,
197     uint8_t mtype, uint8_t perm, ldc_mem_cookie_t *dcookie, uint32_t *ccount);
198 int ldc_mem_dring_nextcookie(ldc_dring_handle_t mhandle,
199     ldc_mem_cookie_t *cookie);
200 int ldc_mem_dring_unbind(ldc_dring_handle_t dhandle);
201 int ldc_mem_dring_info(ldc_dring_handle_t dhandle, ldc_mem_info_t *minfo);
202 int ldc_mem_dring_map(ldc_handle_t handle, ldc_mem_cookie_t *cookie,
203     uint32_t ccount, uint32_t len, uint32_t dsize, uint8_t mtype,
204     ldc_dring_handle_t *dhandle);
205 int ldc_mem_dring_unmap(ldc_dring_handle_t dhandle);
206 int ldc_mem_dring_acquire(ldc_dring_handle_t dhandle, uint64_t start,
207     uint64_t end);
208 int ldc_mem_dring_release(ldc_dring_handle_t dhandle, uint64_t start,
209     uint64_t end);
210 
211 /*
212  * Shared Memory (Direct Map) Acquire and Release API
213  *
214  * LDC_ON_TRAP and LDC_NO_TRAP provide on_trap protection for clients accessing
215  * imported LDC_DIRECT_MAP'd shared memory segments. Use of these macros is
216  * analogous to the ldc_mem_acquire/release and ldc_mem_dring_acquire/release
217  * interfaces for LDC_SHADOW_MAP'd segments. After LDC_ON_TRAP is called,
218  * unless an error is returned, LDC_NO_TRAP must be called.
219  *
220  * LDC_ON_TRAP returns zero on success and EACCES if a data access exception
221  * occurs after enabling protection, but before it is disabled. If EACCES is
222  * returned, the caller must not call LDC_NO_TRAP. In order to handle the
223  * EACCES error return, callers should take the same precautions that apply
224  * when calling on_trap() when calling LDC_ON_TRAP.
225  *
226  * LDC_ON_TRAP is implemented as a macro so that on_trap protection can be
227  * enabled without first executing a save instruction and obtaining a new
228  * register window. Aside from LDC clients calling on_trap() directly, one
229  * alternative approach is to implement the LDC_ON_TRAP function in assembly
230  * language without a save instruction and to then call on_trap() as a tail
231  * call.
232  */
233 #define	LDC_ON_TRAP(otd)					\
234 	(on_trap((otd), OT_DATA_ACCESS) != 0 ?			\
235 	(no_trap(), EACCES) : 0)
236 
237 #define	LDC_NO_TRAP()						\
238 	(no_trap(), 0)
239 
240 #ifdef __cplusplus
241 }
242 #endif
243 
244 #endif /* _LDC_H */
245