xref: /illumos-gate/usr/src/uts/sun4v/sys/ldc.h (revision 6a634c9d)
11ae08745Sheppo /*
21ae08745Sheppo  * CDDL HEADER START
31ae08745Sheppo  *
41ae08745Sheppo  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
71ae08745Sheppo  *
81ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
101ae08745Sheppo  * See the License for the specific language governing permissions
111ae08745Sheppo  * and limitations under the License.
121ae08745Sheppo  *
131ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181ae08745Sheppo  *
191ae08745Sheppo  * CDDL HEADER END
201ae08745Sheppo  */
211ae08745Sheppo 
221ae08745Sheppo /*
23*34f94fbcSWENTAO YANG  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
241ae08745Sheppo  */
251ae08745Sheppo 
261ae08745Sheppo #ifndef _LDC_H
271ae08745Sheppo #define	_LDC_H
281ae08745Sheppo 
291ae08745Sheppo #ifdef __cplusplus
301ae08745Sheppo extern "C" {
311ae08745Sheppo #endif
321ae08745Sheppo 
331ae08745Sheppo #include <sys/types.h>
341ae08745Sheppo #include <sys/ddi.h>
351ae08745Sheppo #include <sys/sunddi.h>
361ae08745Sheppo #include <sys/ioctl.h>
371ae08745Sheppo #include <sys/processor.h>
38bbfa0259Sha #include <sys/ontrap.h>
391ae08745Sheppo 
401ae08745Sheppo /* Types */
411ae08745Sheppo typedef uint64_t ldc_handle_t;		/* Channel handle */
421ae08745Sheppo typedef uint64_t ldc_mem_handle_t;	/* Channel memory handle */
431ae08745Sheppo typedef uint64_t ldc_dring_handle_t;	/* Descriptor ring handle */
441ae08745Sheppo 
451ae08745Sheppo /* LDC transport mode */
461ae08745Sheppo typedef enum {
471ae08745Sheppo 	LDC_MODE_RAW,			/* Raw mode */
481ae08745Sheppo 	LDC_MODE_UNRELIABLE,		/* Unreliable packet mode */
4920ae46ebSha 	_LDC_MODE_RESERVED_,		/* reserved */
5020ae46ebSha 	LDC_MODE_RELIABLE		/* Reliable packet mode */
511ae08745Sheppo } ldc_mode_t;
521ae08745Sheppo 
531ae08745Sheppo /* LDC message payload sizes */
541ae08745Sheppo #define	LDC_ELEM_SIZE			8		/* size in bytes */
551ae08745Sheppo #define	LDC_PACKET_SIZE			(LDC_ELEM_SIZE * 8)
561ae08745Sheppo #define	LDC_PAYLOAD_SIZE_RAW		(LDC_PACKET_SIZE)
571ae08745Sheppo #define	LDC_PAYLOAD_SIZE_UNRELIABLE	(LDC_PACKET_SIZE - LDC_ELEM_SIZE)
581ae08745Sheppo #define	LDC_PAYLOAD_SIZE_RELIABLE	(LDC_PACKET_SIZE - (LDC_ELEM_SIZE * 2))
591ae08745Sheppo 
601ae08745Sheppo /* LDC Channel Status */
611ae08745Sheppo typedef enum {
621ae08745Sheppo 	LDC_INIT = 1,			/* Channel initialized */
631ae08745Sheppo 	LDC_OPEN,			/* Channel open */
641ae08745Sheppo 	LDC_READY,			/* Channel peer opened (hw-link-up) */
651ae08745Sheppo 	LDC_UP				/* Channel UP - ready for data xfer */
661ae08745Sheppo } ldc_status_t;
671ae08745Sheppo 
681ae08745Sheppo /* Callback return values */
691ae08745Sheppo #define	LDC_SUCCESS	0
701ae08745Sheppo #define	LDC_FAILURE	1
711ae08745Sheppo 
721ae08745Sheppo /* LDC callback mode */
731ae08745Sheppo typedef enum {
741ae08745Sheppo 	LDC_CB_ENABLE,			/* Enable callbacks */
751ae08745Sheppo 	LDC_CB_DISABLE			/* Disable callbacks */
761ae08745Sheppo } ldc_cb_mode_t;
771ae08745Sheppo 
781ae08745Sheppo /* Callback events */
791ae08745Sheppo #define	LDC_EVT_DOWN		0x1	/* Channel DOWN, status = OPEN */
801ae08745Sheppo #define	LDC_EVT_RESET		0x2	/* Channel RESET, status = READY */
811ae08745Sheppo #define	LDC_EVT_UP		0x4	/* Channel UP, status = UP */
821ae08745Sheppo #define	LDC_EVT_READ		0x8	/* Channel has data for read */
831ae08745Sheppo #define	LDC_EVT_WRITE		0x10	/* Channel has space for write */
841ae08745Sheppo 
851ae08745Sheppo /* LDC device classes */
861ae08745Sheppo typedef enum {
871ae08745Sheppo 	LDC_DEV_GENERIC = 1,		/* generic device */
881ae08745Sheppo 	LDC_DEV_BLK,			/* block device, eg. vdc */
891ae08745Sheppo 	LDC_DEV_BLK_SVC,		/* block device service, eg. vds */
901ae08745Sheppo 	LDC_DEV_NT,			/* network device, eg. vnet */
911ae08745Sheppo 	LDC_DEV_NT_SVC,			/* network service eg. vsw */
921ae08745Sheppo 	LDC_DEV_SERIAL			/* serial device eg. vldc, vcc */
931ae08745Sheppo } ldc_dev_t;
941ae08745Sheppo 
951ae08745Sheppo /* Channel nexus registration */
961ae08745Sheppo typedef struct ldc_cnex {
971ae08745Sheppo 	dev_info_t	*dip;		/* dip of channel nexus */
981ae08745Sheppo 	int		(*reg_chan)();	/* interface for channel register */
991ae08745Sheppo 	int		(*unreg_chan)(); /* interface for channel unregister */
1001ae08745Sheppo 	int		(*add_intr)();	/* interface for adding interrupts */
1011ae08745Sheppo 	int		(*rem_intr)();	/* interface for removing interrupts */
1021ae08745Sheppo 	int		(*clr_intr)();	/* interface for clearing interrupts */
1031ae08745Sheppo } ldc_cnex_t;
1041ae08745Sheppo 
1051ae08745Sheppo /* LDC attribute structure */
1061ae08745Sheppo typedef struct ldc_attr {
1071ae08745Sheppo 	ldc_dev_t	devclass;	/* device class */
1081ae08745Sheppo 	uint64_t	instance;	/* device class instance */
1091ae08745Sheppo 	ldc_mode_t	mode;		/* channel mode */
110e1ebb9ecSlm 	uint64_t	mtu;		/* channel mtu */
1111ae08745Sheppo } ldc_attr_t;
1121ae08745Sheppo 
1131ae08745Sheppo /* LDC memory cookie */
1141ae08745Sheppo typedef struct ldc_mem_cookie {
1151ae08745Sheppo 	uint64_t	addr;		/* cookie address */
1161ae08745Sheppo 	uint64_t	size;		/* size @ offset */
1171ae08745Sheppo } ldc_mem_cookie_t;
1181ae08745Sheppo 
1191ae08745Sheppo /*
1201ae08745Sheppo  * LDC Memory Map Type
1211ae08745Sheppo  * Specifies how shared memory being created is shared with its
1221ae08745Sheppo  * peer and/or how the peer has mapped in the exported memory.
1231ae08745Sheppo  */
1241ae08745Sheppo #define	LDC_SHADOW_MAP		0x1	/* share mem via shadow copy only */
1251ae08745Sheppo #define	LDC_DIRECT_MAP		0x2	/* share mem direct access */
1261ae08745Sheppo #define	LDC_IO_MAP		0x4	/* share mem for IOMMU/DMA access */
1271ae08745Sheppo 
128*34f94fbcSWENTAO YANG /*
129*34f94fbcSWENTAO YANG  * Default mapin size supported with legacy f/w.
130*34f94fbcSWENTAO YANG  */
131*34f94fbcSWENTAO YANG #define	LDC_DIRECT_MAP_SIZE_DEFAULT	(64 * 1024 * 1024)
132*34f94fbcSWENTAO YANG 
1331ae08745Sheppo /* LDC Memory Access Permissions  */
1341ae08745Sheppo #define	LDC_MEM_R		0x1	/* Memory region is read only */
1351ae08745Sheppo #define	LDC_MEM_W		0x2	/* Memory region is write only */
1361ae08745Sheppo #define	LDC_MEM_X		0x4	/* Memory region is execute only */
1371ae08745Sheppo #define	LDC_MEM_RW		(LDC_MEM_R|LDC_MEM_W)
1381ae08745Sheppo #define	LDC_MEM_RWX		(LDC_MEM_R|LDC_MEM_W|LDC_MEM_X)
1391ae08745Sheppo 
1401ae08745Sheppo /* LDC Memory Copy Direction */
1411ae08745Sheppo #define	LDC_COPY_IN		0x0	/* Copy data to VA from cookie mem */
1421ae08745Sheppo #define	LDC_COPY_OUT		0x1	/* Copy data from VA to cookie mem */
1431ae08745Sheppo 
1441ae08745Sheppo /* LDC memory/dring (handle) status */
1451ae08745Sheppo typedef enum {
1461ae08745Sheppo 	LDC_UNBOUND,			/* Memory handle is unbound */
1471ae08745Sheppo 	LDC_BOUND,			/* Memory handle is bound */
1481ae08745Sheppo 	LDC_MAPPED			/* Memory handle is mapped */
1491ae08745Sheppo } ldc_mstatus_t;
1501ae08745Sheppo 
1511ae08745Sheppo /* LDC [dring] memory info */
1521ae08745Sheppo typedef struct ldc_mem_info {
1531ae08745Sheppo 	uint8_t		mtype;		/* map type */
1541ae08745Sheppo 	uint8_t		perm;		/* RWX permissions */
1551ae08745Sheppo 	caddr_t		vaddr;		/* base VA */
1561ae08745Sheppo 	uintptr_t	raddr;		/* base RA */
1571ae08745Sheppo 	ldc_mstatus_t	status;		/* dring/mem handle status */
1581ae08745Sheppo } ldc_mem_info_t;
1591ae08745Sheppo 
160*34f94fbcSWENTAO YANG /* LDC channel info */
161*34f94fbcSWENTAO YANG typedef struct ldc_info {
162*34f94fbcSWENTAO YANG 	uint64_t direct_map_size_max;	/* Max direct mapin space size */
163*34f94fbcSWENTAO YANG } ldc_info_t;
164*34f94fbcSWENTAO YANG 
1651ae08745Sheppo /* API functions */
1661ae08745Sheppo int ldc_register(ldc_cnex_t *cinfo);
1671ae08745Sheppo int ldc_unregister(ldc_cnex_t *cinfo);
1681ae08745Sheppo 
1691ae08745Sheppo int ldc_init(uint64_t id, ldc_attr_t *attr, ldc_handle_t *handle);
1701ae08745Sheppo int ldc_fini(ldc_handle_t handle);
1711ae08745Sheppo int ldc_open(ldc_handle_t handle);
1721ae08745Sheppo int ldc_close(ldc_handle_t handle);
1731ae08745Sheppo int ldc_up(ldc_handle_t handle);
174e1ebb9ecSlm int ldc_down(ldc_handle_t handle);
1751ae08745Sheppo int ldc_reg_callback(ldc_handle_t handle,
1761ae08745Sheppo     uint_t(*callback)(uint64_t event, caddr_t arg), caddr_t arg);
1771ae08745Sheppo int ldc_unreg_callback(ldc_handle_t handle);
1781ae08745Sheppo int ldc_set_cb_mode(ldc_handle_t handle, ldc_cb_mode_t imode);
179e1ebb9ecSlm int ldc_chkq(ldc_handle_t handle, boolean_t *hasdata);
1801ae08745Sheppo int ldc_read(ldc_handle_t handle, caddr_t buf, size_t *size);
1811ae08745Sheppo int ldc_write(ldc_handle_t handle, caddr_t buf, size_t *size);
1821ae08745Sheppo int ldc_status(ldc_handle_t handle, ldc_status_t *status);
1831ae08745Sheppo 
1841ae08745Sheppo int ldc_mem_alloc_handle(ldc_handle_t handle, ldc_mem_handle_t *mhandle);
1851ae08745Sheppo int ldc_mem_free_handle(ldc_mem_handle_t mhandle);
1861ae08745Sheppo int ldc_mem_bind_handle(ldc_mem_handle_t mhandle, caddr_t vaddr, size_t len,
1871ae08745Sheppo     uint8_t mtype, uint8_t perm, ldc_mem_cookie_t *cookie, uint32_t *ccount);
1881ae08745Sheppo int ldc_mem_unbind_handle(ldc_mem_handle_t mhandle);
1891ae08745Sheppo int ldc_mem_info(ldc_mem_handle_t mhandle, ldc_mem_info_t *minfo);
1901ae08745Sheppo int ldc_mem_nextcookie(ldc_mem_handle_t mhandle, ldc_mem_cookie_t *cookie);
1911ae08745Sheppo int ldc_mem_copy(ldc_handle_t handle, caddr_t vaddr, uint64_t off, size_t *len,
1921ae08745Sheppo     ldc_mem_cookie_t *cookies, uint32_t ccount, uint8_t direction);
1933af08d82Slm int ldc_mem_rdwr_cookie(ldc_handle_t handle, caddr_t vaddr, size_t *size,
1941ae08745Sheppo     caddr_t paddr, uint8_t  direction);
1951ae08745Sheppo int ldc_mem_map(ldc_mem_handle_t mhandle, ldc_mem_cookie_t *cookie,
1964bac2208Snarayan     uint32_t ccount, uint8_t mtype, uint8_t perm, caddr_t *vaddr,
1974bac2208Snarayan     caddr_t *raddr);
1984bac2208Snarayan int ldc_mem_unmap(ldc_mem_handle_t mhandle);
1991ae08745Sheppo int ldc_mem_acquire(ldc_mem_handle_t mhandle, uint64_t offset, uint64_t size);
2001ae08745Sheppo int ldc_mem_release(ldc_mem_handle_t mhandle, uint64_t offset, uint64_t size);
2011ae08745Sheppo 
2021ae08745Sheppo int ldc_mem_dring_create(uint32_t len, uint32_t dsize,
2031ae08745Sheppo     ldc_dring_handle_t *dhandle);
2041ae08745Sheppo int ldc_mem_dring_destroy(ldc_dring_handle_t dhandle);
2051ae08745Sheppo int ldc_mem_dring_bind(ldc_handle_t handle, ldc_dring_handle_t dhandle,
2061ae08745Sheppo     uint8_t mtype, uint8_t perm, ldc_mem_cookie_t *dcookie, uint32_t *ccount);
2071ae08745Sheppo int ldc_mem_dring_nextcookie(ldc_dring_handle_t mhandle,
2081ae08745Sheppo     ldc_mem_cookie_t *cookie);
2091ae08745Sheppo int ldc_mem_dring_unbind(ldc_dring_handle_t dhandle);
2101ae08745Sheppo int ldc_mem_dring_info(ldc_dring_handle_t dhandle, ldc_mem_info_t *minfo);
2111ae08745Sheppo int ldc_mem_dring_map(ldc_handle_t handle, ldc_mem_cookie_t *cookie,
2121ae08745Sheppo     uint32_t ccount, uint32_t len, uint32_t dsize, uint8_t mtype,
2131ae08745Sheppo     ldc_dring_handle_t *dhandle);
2141ae08745Sheppo int ldc_mem_dring_unmap(ldc_dring_handle_t dhandle);
2151ae08745Sheppo int ldc_mem_dring_acquire(ldc_dring_handle_t dhandle, uint64_t start,
2161ae08745Sheppo     uint64_t end);
2171ae08745Sheppo int ldc_mem_dring_release(ldc_dring_handle_t dhandle, uint64_t start,
2181ae08745Sheppo     uint64_t end);
219*34f94fbcSWENTAO YANG int ldc_info(ldc_handle_t handle, ldc_info_t *info);
2201ae08745Sheppo 
221bbfa0259Sha /*
222bbfa0259Sha  * Shared Memory (Direct Map) Acquire and Release API
223bbfa0259Sha  *
2247bd3a2e2SSriharsha Basavapatna  * LDC_ON_TRAP and LDC_NO_TRAP provide on_trap protection for clients accessing
2257bd3a2e2SSriharsha Basavapatna  * imported LDC_DIRECT_MAP'd shared memory segments. Use of these macros is
2267bd3a2e2SSriharsha Basavapatna  * analogous to the ldc_mem_acquire/release and ldc_mem_dring_acquire/release
2277bd3a2e2SSriharsha Basavapatna  * interfaces for LDC_SHADOW_MAP'd segments. After LDC_ON_TRAP is called,
2287bd3a2e2SSriharsha Basavapatna  * unless an error is returned, LDC_NO_TRAP must be called.
229bbfa0259Sha  *
2307bd3a2e2SSriharsha Basavapatna  * LDC_ON_TRAP returns zero on success and EACCES if a data access exception
2317bd3a2e2SSriharsha Basavapatna  * occurs after enabling protection, but before it is disabled. If EACCES is
2327bd3a2e2SSriharsha Basavapatna  * returned, the caller must not call LDC_NO_TRAP. In order to handle the
2337bd3a2e2SSriharsha Basavapatna  * EACCES error return, callers should take the same precautions that apply
2347bd3a2e2SSriharsha Basavapatna  * when calling on_trap() when calling LDC_ON_TRAP.
235bbfa0259Sha  *
2367bd3a2e2SSriharsha Basavapatna  * LDC_ON_TRAP is implemented as a macro so that on_trap protection can be
2377bd3a2e2SSriharsha Basavapatna  * enabled without first executing a save instruction and obtaining a new
2387bd3a2e2SSriharsha Basavapatna  * register window. Aside from LDC clients calling on_trap() directly, one
2397bd3a2e2SSriharsha Basavapatna  * alternative approach is to implement the LDC_ON_TRAP function in assembly
2407bd3a2e2SSriharsha Basavapatna  * language without a save instruction and to then call on_trap() as a tail
2417bd3a2e2SSriharsha Basavapatna  * call.
242bbfa0259Sha  */
2437bd3a2e2SSriharsha Basavapatna #define	LDC_ON_TRAP(otd)					\
2447bd3a2e2SSriharsha Basavapatna 	(on_trap((otd), OT_DATA_ACCESS) != 0 ?			\
245bbfa0259Sha 	(no_trap(), EACCES) : 0)
246bbfa0259Sha 
2477bd3a2e2SSriharsha Basavapatna #define	LDC_NO_TRAP()						\
248bbfa0259Sha 	(no_trap(), 0)
249bbfa0259Sha 
2501ae08745Sheppo #ifdef __cplusplus
2511ae08745Sheppo }
2521ae08745Sheppo #endif
2531ae08745Sheppo 
2541ae08745Sheppo #endif /* _LDC_H */
255