11991Sheppo /* 21991Sheppo * CDDL HEADER START 31991Sheppo * 41991Sheppo * The contents of this file are subject to the terms of the 51991Sheppo * Common Development and Distribution License (the "License"). 61991Sheppo * You may not use this file except in compliance with the License. 71991Sheppo * 81991Sheppo * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 91991Sheppo * or http://www.opensolaris.org/os/licensing. 101991Sheppo * See the License for the specific language governing permissions 111991Sheppo * and limitations under the License. 121991Sheppo * 131991Sheppo * When distributing Covered Code, include this CDDL HEADER in each 141991Sheppo * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 151991Sheppo * If applicable, add the following below this CDDL HEADER, with the 161991Sheppo * fields enclosed by brackets "[]" replaced with your own identifying 171991Sheppo * information: Portions Copyright [yyyy] [name of copyright owner] 181991Sheppo * 191991Sheppo * CDDL HEADER END 201991Sheppo */ 211991Sheppo 221991Sheppo /* 23*13098SWentao.Yang@Sun.COM * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 241991Sheppo */ 251991Sheppo 261991Sheppo #ifndef _LDC_H 271991Sheppo #define _LDC_H 281991Sheppo 291991Sheppo #ifdef __cplusplus 301991Sheppo extern "C" { 311991Sheppo #endif 321991Sheppo 331991Sheppo #include <sys/types.h> 341991Sheppo #include <sys/ddi.h> 351991Sheppo #include <sys/sunddi.h> 361991Sheppo #include <sys/ioctl.h> 371991Sheppo #include <sys/processor.h> 386845Sha137994 #include <sys/ontrap.h> 391991Sheppo 401991Sheppo /* Types */ 411991Sheppo typedef uint64_t ldc_handle_t; /* Channel handle */ 421991Sheppo typedef uint64_t ldc_mem_handle_t; /* Channel memory handle */ 431991Sheppo typedef uint64_t ldc_dring_handle_t; /* Descriptor ring handle */ 441991Sheppo 451991Sheppo /* LDC transport mode */ 461991Sheppo typedef enum { 471991Sheppo LDC_MODE_RAW, /* Raw mode */ 481991Sheppo LDC_MODE_UNRELIABLE, /* Unreliable packet mode */ 496408Sha137994 _LDC_MODE_RESERVED_, /* reserved */ 506408Sha137994 LDC_MODE_RELIABLE /* Reliable packet mode */ 511991Sheppo } ldc_mode_t; 521991Sheppo 531991Sheppo /* LDC message payload sizes */ 541991Sheppo #define LDC_ELEM_SIZE 8 /* size in bytes */ 551991Sheppo #define LDC_PACKET_SIZE (LDC_ELEM_SIZE * 8) 561991Sheppo #define LDC_PAYLOAD_SIZE_RAW (LDC_PACKET_SIZE) 571991Sheppo #define LDC_PAYLOAD_SIZE_UNRELIABLE (LDC_PACKET_SIZE - LDC_ELEM_SIZE) 581991Sheppo #define LDC_PAYLOAD_SIZE_RELIABLE (LDC_PACKET_SIZE - (LDC_ELEM_SIZE * 2)) 591991Sheppo 601991Sheppo /* LDC Channel Status */ 611991Sheppo typedef enum { 621991Sheppo LDC_INIT = 1, /* Channel initialized */ 631991Sheppo LDC_OPEN, /* Channel open */ 641991Sheppo LDC_READY, /* Channel peer opened (hw-link-up) */ 651991Sheppo LDC_UP /* Channel UP - ready for data xfer */ 661991Sheppo } ldc_status_t; 671991Sheppo 681991Sheppo /* Callback return values */ 691991Sheppo #define LDC_SUCCESS 0 701991Sheppo #define LDC_FAILURE 1 711991Sheppo 721991Sheppo /* LDC callback mode */ 731991Sheppo typedef enum { 741991Sheppo LDC_CB_ENABLE, /* Enable callbacks */ 751991Sheppo LDC_CB_DISABLE /* Disable callbacks */ 761991Sheppo } ldc_cb_mode_t; 771991Sheppo 781991Sheppo /* Callback events */ 791991Sheppo #define LDC_EVT_DOWN 0x1 /* Channel DOWN, status = OPEN */ 801991Sheppo #define LDC_EVT_RESET 0x2 /* Channel RESET, status = READY */ 811991Sheppo #define LDC_EVT_UP 0x4 /* Channel UP, status = UP */ 821991Sheppo #define LDC_EVT_READ 0x8 /* Channel has data for read */ 831991Sheppo #define LDC_EVT_WRITE 0x10 /* Channel has space for write */ 841991Sheppo 851991Sheppo /* LDC device classes */ 861991Sheppo typedef enum { 871991Sheppo LDC_DEV_GENERIC = 1, /* generic device */ 881991Sheppo LDC_DEV_BLK, /* block device, eg. vdc */ 891991Sheppo LDC_DEV_BLK_SVC, /* block device service, eg. vds */ 901991Sheppo LDC_DEV_NT, /* network device, eg. vnet */ 911991Sheppo LDC_DEV_NT_SVC, /* network service eg. vsw */ 921991Sheppo LDC_DEV_SERIAL /* serial device eg. vldc, vcc */ 931991Sheppo } ldc_dev_t; 941991Sheppo 951991Sheppo /* Channel nexus registration */ 961991Sheppo typedef struct ldc_cnex { 971991Sheppo dev_info_t *dip; /* dip of channel nexus */ 981991Sheppo int (*reg_chan)(); /* interface for channel register */ 991991Sheppo int (*unreg_chan)(); /* interface for channel unregister */ 1001991Sheppo int (*add_intr)(); /* interface for adding interrupts */ 1011991Sheppo int (*rem_intr)(); /* interface for removing interrupts */ 1021991Sheppo int (*clr_intr)(); /* interface for clearing interrupts */ 1031991Sheppo } ldc_cnex_t; 1041991Sheppo 1051991Sheppo /* LDC attribute structure */ 1061991Sheppo typedef struct ldc_attr { 1071991Sheppo ldc_dev_t devclass; /* device class */ 1081991Sheppo uint64_t instance; /* device class instance */ 1091991Sheppo ldc_mode_t mode; /* channel mode */ 1102410Slm66018 uint64_t mtu; /* channel mtu */ 1111991Sheppo } ldc_attr_t; 1121991Sheppo 1131991Sheppo /* LDC memory cookie */ 1141991Sheppo typedef struct ldc_mem_cookie { 1151991Sheppo uint64_t addr; /* cookie address */ 1161991Sheppo uint64_t size; /* size @ offset */ 1171991Sheppo } ldc_mem_cookie_t; 1181991Sheppo 1191991Sheppo /* 1201991Sheppo * LDC Memory Map Type 1211991Sheppo * Specifies how shared memory being created is shared with its 1221991Sheppo * peer and/or how the peer has mapped in the exported memory. 1231991Sheppo */ 1241991Sheppo #define LDC_SHADOW_MAP 0x1 /* share mem via shadow copy only */ 1251991Sheppo #define LDC_DIRECT_MAP 0x2 /* share mem direct access */ 1261991Sheppo #define LDC_IO_MAP 0x4 /* share mem for IOMMU/DMA access */ 1271991Sheppo 128*13098SWentao.Yang@Sun.COM /* 129*13098SWentao.Yang@Sun.COM * Default mapin size supported with legacy f/w. 130*13098SWentao.Yang@Sun.COM */ 131*13098SWentao.Yang@Sun.COM #define LDC_DIRECT_MAP_SIZE_DEFAULT (64 * 1024 * 1024) 132*13098SWentao.Yang@Sun.COM 1331991Sheppo /* LDC Memory Access Permissions */ 1341991Sheppo #define LDC_MEM_R 0x1 /* Memory region is read only */ 1351991Sheppo #define LDC_MEM_W 0x2 /* Memory region is write only */ 1361991Sheppo #define LDC_MEM_X 0x4 /* Memory region is execute only */ 1371991Sheppo #define LDC_MEM_RW (LDC_MEM_R|LDC_MEM_W) 1381991Sheppo #define LDC_MEM_RWX (LDC_MEM_R|LDC_MEM_W|LDC_MEM_X) 1391991Sheppo 1401991Sheppo /* LDC Memory Copy Direction */ 1411991Sheppo #define LDC_COPY_IN 0x0 /* Copy data to VA from cookie mem */ 1421991Sheppo #define LDC_COPY_OUT 0x1 /* Copy data from VA to cookie mem */ 1431991Sheppo 1441991Sheppo /* LDC memory/dring (handle) status */ 1451991Sheppo typedef enum { 1461991Sheppo LDC_UNBOUND, /* Memory handle is unbound */ 1471991Sheppo LDC_BOUND, /* Memory handle is bound */ 1481991Sheppo LDC_MAPPED /* Memory handle is mapped */ 1491991Sheppo } ldc_mstatus_t; 1501991Sheppo 1511991Sheppo /* LDC [dring] memory info */ 1521991Sheppo typedef struct ldc_mem_info { 1531991Sheppo uint8_t mtype; /* map type */ 1541991Sheppo uint8_t perm; /* RWX permissions */ 1551991Sheppo caddr_t vaddr; /* base VA */ 1561991Sheppo uintptr_t raddr; /* base RA */ 1571991Sheppo ldc_mstatus_t status; /* dring/mem handle status */ 1581991Sheppo } ldc_mem_info_t; 1591991Sheppo 160*13098SWentao.Yang@Sun.COM /* LDC channel info */ 161*13098SWentao.Yang@Sun.COM typedef struct ldc_info { 162*13098SWentao.Yang@Sun.COM uint64_t direct_map_size_max; /* Max direct mapin space size */ 163*13098SWentao.Yang@Sun.COM } ldc_info_t; 164*13098SWentao.Yang@Sun.COM 1651991Sheppo /* API functions */ 1661991Sheppo int ldc_register(ldc_cnex_t *cinfo); 1671991Sheppo int ldc_unregister(ldc_cnex_t *cinfo); 1681991Sheppo 1691991Sheppo int ldc_init(uint64_t id, ldc_attr_t *attr, ldc_handle_t *handle); 1701991Sheppo int ldc_fini(ldc_handle_t handle); 1711991Sheppo int ldc_open(ldc_handle_t handle); 1721991Sheppo int ldc_close(ldc_handle_t handle); 1731991Sheppo int ldc_up(ldc_handle_t handle); 1742410Slm66018 int ldc_down(ldc_handle_t handle); 1751991Sheppo int ldc_reg_callback(ldc_handle_t handle, 1761991Sheppo uint_t(*callback)(uint64_t event, caddr_t arg), caddr_t arg); 1771991Sheppo int ldc_unreg_callback(ldc_handle_t handle); 1781991Sheppo int ldc_set_cb_mode(ldc_handle_t handle, ldc_cb_mode_t imode); 1792410Slm66018 int ldc_chkq(ldc_handle_t handle, boolean_t *hasdata); 1801991Sheppo int ldc_read(ldc_handle_t handle, caddr_t buf, size_t *size); 1811991Sheppo int ldc_write(ldc_handle_t handle, caddr_t buf, size_t *size); 1821991Sheppo int ldc_status(ldc_handle_t handle, ldc_status_t *status); 1831991Sheppo 1841991Sheppo int ldc_mem_alloc_handle(ldc_handle_t handle, ldc_mem_handle_t *mhandle); 1851991Sheppo int ldc_mem_free_handle(ldc_mem_handle_t mhandle); 1861991Sheppo int ldc_mem_bind_handle(ldc_mem_handle_t mhandle, caddr_t vaddr, size_t len, 1871991Sheppo uint8_t mtype, uint8_t perm, ldc_mem_cookie_t *cookie, uint32_t *ccount); 1881991Sheppo int ldc_mem_unbind_handle(ldc_mem_handle_t mhandle); 1891991Sheppo int ldc_mem_info(ldc_mem_handle_t mhandle, ldc_mem_info_t *minfo); 1901991Sheppo int ldc_mem_nextcookie(ldc_mem_handle_t mhandle, ldc_mem_cookie_t *cookie); 1911991Sheppo int ldc_mem_copy(ldc_handle_t handle, caddr_t vaddr, uint64_t off, size_t *len, 1921991Sheppo ldc_mem_cookie_t *cookies, uint32_t ccount, uint8_t direction); 1932793Slm66018 int ldc_mem_rdwr_cookie(ldc_handle_t handle, caddr_t vaddr, size_t *size, 1941991Sheppo caddr_t paddr, uint8_t direction); 1951991Sheppo int ldc_mem_map(ldc_mem_handle_t mhandle, ldc_mem_cookie_t *cookie, 1962531Snarayan uint32_t ccount, uint8_t mtype, uint8_t perm, caddr_t *vaddr, 1972531Snarayan caddr_t *raddr); 1982531Snarayan int ldc_mem_unmap(ldc_mem_handle_t mhandle); 1991991Sheppo int ldc_mem_acquire(ldc_mem_handle_t mhandle, uint64_t offset, uint64_t size); 2001991Sheppo int ldc_mem_release(ldc_mem_handle_t mhandle, uint64_t offset, uint64_t size); 2011991Sheppo 2021991Sheppo int ldc_mem_dring_create(uint32_t len, uint32_t dsize, 2031991Sheppo ldc_dring_handle_t *dhandle); 2041991Sheppo int ldc_mem_dring_destroy(ldc_dring_handle_t dhandle); 2051991Sheppo int ldc_mem_dring_bind(ldc_handle_t handle, ldc_dring_handle_t dhandle, 2061991Sheppo uint8_t mtype, uint8_t perm, ldc_mem_cookie_t *dcookie, uint32_t *ccount); 2071991Sheppo int ldc_mem_dring_nextcookie(ldc_dring_handle_t mhandle, 2081991Sheppo ldc_mem_cookie_t *cookie); 2091991Sheppo int ldc_mem_dring_unbind(ldc_dring_handle_t dhandle); 2101991Sheppo int ldc_mem_dring_info(ldc_dring_handle_t dhandle, ldc_mem_info_t *minfo); 2111991Sheppo int ldc_mem_dring_map(ldc_handle_t handle, ldc_mem_cookie_t *cookie, 2121991Sheppo uint32_t ccount, uint32_t len, uint32_t dsize, uint8_t mtype, 2131991Sheppo ldc_dring_handle_t *dhandle); 2141991Sheppo int ldc_mem_dring_unmap(ldc_dring_handle_t dhandle); 2151991Sheppo int ldc_mem_dring_acquire(ldc_dring_handle_t dhandle, uint64_t start, 2161991Sheppo uint64_t end); 2171991Sheppo int ldc_mem_dring_release(ldc_dring_handle_t dhandle, uint64_t start, 2181991Sheppo uint64_t end); 219*13098SWentao.Yang@Sun.COM int ldc_info(ldc_handle_t handle, ldc_info_t *info); 2201991Sheppo 2216845Sha137994 /* 2226845Sha137994 * Shared Memory (Direct Map) Acquire and Release API 2236845Sha137994 * 22412011SSriharsha.Basavapatna@Sun.COM * LDC_ON_TRAP and LDC_NO_TRAP provide on_trap protection for clients accessing 22512011SSriharsha.Basavapatna@Sun.COM * imported LDC_DIRECT_MAP'd shared memory segments. Use of these macros is 22612011SSriharsha.Basavapatna@Sun.COM * analogous to the ldc_mem_acquire/release and ldc_mem_dring_acquire/release 22712011SSriharsha.Basavapatna@Sun.COM * interfaces for LDC_SHADOW_MAP'd segments. After LDC_ON_TRAP is called, 22812011SSriharsha.Basavapatna@Sun.COM * unless an error is returned, LDC_NO_TRAP must be called. 2296845Sha137994 * 23012011SSriharsha.Basavapatna@Sun.COM * LDC_ON_TRAP returns zero on success and EACCES if a data access exception 23112011SSriharsha.Basavapatna@Sun.COM * occurs after enabling protection, but before it is disabled. If EACCES is 23212011SSriharsha.Basavapatna@Sun.COM * returned, the caller must not call LDC_NO_TRAP. In order to handle the 23312011SSriharsha.Basavapatna@Sun.COM * EACCES error return, callers should take the same precautions that apply 23412011SSriharsha.Basavapatna@Sun.COM * when calling on_trap() when calling LDC_ON_TRAP. 2356845Sha137994 * 23612011SSriharsha.Basavapatna@Sun.COM * LDC_ON_TRAP is implemented as a macro so that on_trap protection can be 23712011SSriharsha.Basavapatna@Sun.COM * enabled without first executing a save instruction and obtaining a new 23812011SSriharsha.Basavapatna@Sun.COM * register window. Aside from LDC clients calling on_trap() directly, one 23912011SSriharsha.Basavapatna@Sun.COM * alternative approach is to implement the LDC_ON_TRAP function in assembly 24012011SSriharsha.Basavapatna@Sun.COM * language without a save instruction and to then call on_trap() as a tail 24112011SSriharsha.Basavapatna@Sun.COM * call. 2426845Sha137994 */ 24312011SSriharsha.Basavapatna@Sun.COM #define LDC_ON_TRAP(otd) \ 24412011SSriharsha.Basavapatna@Sun.COM (on_trap((otd), OT_DATA_ACCESS) != 0 ? \ 2456845Sha137994 (no_trap(), EACCES) : 0) 2466845Sha137994 24712011SSriharsha.Basavapatna@Sun.COM #define LDC_NO_TRAP() \ 2486845Sha137994 (no_trap(), 0) 2496845Sha137994 2501991Sheppo #ifdef __cplusplus 2511991Sheppo } 2521991Sheppo #endif 2531991Sheppo 2541991Sheppo #endif /* _LDC_H */ 255