xref: /onnv-gate/usr/src/uts/common/io/ib/ibtl/ibtl_mem.c (revision 12965:b65a8427f8fe)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58580SBill.Taylor@Sun.COM  * Common Development and Distribution License (the "License").
68580SBill.Taylor@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2212574SWilliam.Taylor@Oracle.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/ib/ibtl/impl/ibtl.h>
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * ibtl_mem.c
300Sstevel@tonic-gate  *    These routines implement all of the Memory Region verbs and the alloc/
310Sstevel@tonic-gate  *    query/free Memory Window verbs at the TI interface.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate static char ibtl_mem[] = "ibtl_mem";
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * Function:
380Sstevel@tonic-gate  *	ibt_register_mr()
390Sstevel@tonic-gate  * Input:
400Sstevel@tonic-gate  *	hca_hdl   - HCA Handle.
410Sstevel@tonic-gate  *	pd        - Protection Domain Handle.
420Sstevel@tonic-gate  *	mem_attr  - Requested memory region attributes.
430Sstevel@tonic-gate  * Output:
440Sstevel@tonic-gate  *	mr_hdl_p  - The returned IBT memory region handle.
450Sstevel@tonic-gate  *	mem_desc  - Returned memory descriptor.
460Sstevel@tonic-gate  * Returns:
470Sstevel@tonic-gate  *      IBT_SUCCESS
480Sstevel@tonic-gate  *	IBT_CHAN_HDL_INVALID
490Sstevel@tonic-gate  *	IBT_MR_VA_INVALID
500Sstevel@tonic-gate  *	IBT_MR_LEN_INVALID
510Sstevel@tonic-gate  *	IBT_MR_ACCESS_REQ_INVALID
520Sstevel@tonic-gate  *	IBT_PD_HDL_INVALID
530Sstevel@tonic-gate  *	IBT_INSUFF_RESOURCE
540Sstevel@tonic-gate  * Description:
550Sstevel@tonic-gate  *    Prepares a virtually addressed memory region for use by a HCA. A
560Sstevel@tonic-gate  *    description of the registered memory suitable for use in Work Requests
570Sstevel@tonic-gate  *    (WRs) is returned in the ibt_mr_desc_t parameter.
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate ibt_status_t
ibt_register_mr(ibt_hca_hdl_t hca_hdl,ibt_pd_hdl_t pd,ibt_mr_attr_t * mem_attr,ibt_mr_hdl_t * mr_hdl_p,ibt_mr_desc_t * mem_desc)600Sstevel@tonic-gate ibt_register_mr(ibt_hca_hdl_t hca_hdl, ibt_pd_hdl_t pd, ibt_mr_attr_t *mem_attr,
610Sstevel@tonic-gate     ibt_mr_hdl_t *mr_hdl_p, ibt_mr_desc_t *mem_desc)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	ib_vaddr_t 	vaddr;
640Sstevel@tonic-gate 	ibt_status_t 	status;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_register_mr(%p, %p, %p)",
670Sstevel@tonic-gate 	    hca_hdl, pd, mem_attr);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	vaddr = mem_attr->mr_vaddr;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_register_mr(
720Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), pd, mem_attr, IBTL_HCA2CLNT(hca_hdl),
730Sstevel@tonic-gate 	    mr_hdl_p, mem_desc);
740Sstevel@tonic-gate 	if (status == IBT_SUCCESS) {
750Sstevel@tonic-gate 		mem_desc->md_vaddr = vaddr;
7612574SWilliam.Taylor@Oracle.COM 		atomic_inc_32(&hca_hdl->ha_mr_cnt);
770Sstevel@tonic-gate 	}
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	return (status);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * Function:
850Sstevel@tonic-gate  *	ibt_register_buf()
860Sstevel@tonic-gate  * Input:
870Sstevel@tonic-gate  *	hca_hdl		HCA Handle.
880Sstevel@tonic-gate  *	pd		Protection Domain Handle.
890Sstevel@tonic-gate  *	mem_bpattr	Memory Registration attributes (IOVA and flags).
900Sstevel@tonic-gate  *	bp		A pointer to a buf(9S) struct.
910Sstevel@tonic-gate  * Output:
920Sstevel@tonic-gate  *	mr_hdl_p	The returned IBT memory region handle.
930Sstevel@tonic-gate  *	mem_desc	Returned memory descriptor.
940Sstevel@tonic-gate  * Returns:
950Sstevel@tonic-gate  *      IBT_SUCCESS
960Sstevel@tonic-gate  *	IBT_CHAN_HDL_INVALID
970Sstevel@tonic-gate  *	IBT_MR_VA_INVALID
980Sstevel@tonic-gate  *	IBT_MR_LEN_INVALID
990Sstevel@tonic-gate  *	IBT_MR_ACCESS_REQ_INVALID
1000Sstevel@tonic-gate  *	IBT_PD_HDL_INVALID
1010Sstevel@tonic-gate  *	IBT_INSUFF_RESOURCE
1020Sstevel@tonic-gate  * Description:
1030Sstevel@tonic-gate  *	Prepares a memory region described by a buf(9S) struct for use by a HCA.
1040Sstevel@tonic-gate  *	A description of the registered memory suitable for use in
1050Sstevel@tonic-gate  *	Work Requests (WRs) is returned in the ibt_mr_desc_t parameter.
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate ibt_status_t
ibt_register_buf(ibt_hca_hdl_t hca_hdl,ibt_pd_hdl_t pd,ibt_smr_attr_t * mem_bpattr,struct buf * bp,ibt_mr_hdl_t * mr_hdl_p,ibt_mr_desc_t * mem_desc)1080Sstevel@tonic-gate ibt_register_buf(ibt_hca_hdl_t hca_hdl, ibt_pd_hdl_t pd,
1090Sstevel@tonic-gate     ibt_smr_attr_t *mem_bpattr, struct buf *bp, ibt_mr_hdl_t *mr_hdl_p,
1100Sstevel@tonic-gate     ibt_mr_desc_t *mem_desc)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	ibt_status_t 	status;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_register_buf(%p, %p, %p, %p)",
1150Sstevel@tonic-gate 	    hca_hdl, pd, mem_bpattr, bp);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_register_buf(
1180Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), pd, mem_bpattr, bp, IBTL_HCA2CLNT(hca_hdl),
1190Sstevel@tonic-gate 	    mr_hdl_p, mem_desc);
1200Sstevel@tonic-gate 	if (status == IBT_SUCCESS) {
12112574SWilliam.Taylor@Oracle.COM 		atomic_inc_32(&hca_hdl->ha_mr_cnt);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	return (status);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  * Function:
1300Sstevel@tonic-gate  *	ibt_query_mr()
1310Sstevel@tonic-gate  * Input:
1320Sstevel@tonic-gate  *	hca_hdl   - HCA Handle.
1330Sstevel@tonic-gate  *	mr_hdl    - The IBT Memory Region handle.
1340Sstevel@tonic-gate  * Output:
1350Sstevel@tonic-gate  *      attr      - The pointer to Memory region attributes structure.
1360Sstevel@tonic-gate  * Returns:
1370Sstevel@tonic-gate  *      IBT_SUCCESS
1380Sstevel@tonic-gate  *	IBT_CHAN_HDL_INVALID
1390Sstevel@tonic-gate  *	IBT_MR_HDL_INVALID
1400Sstevel@tonic-gate  * Description:
1410Sstevel@tonic-gate  *    Retrieves information about a specified memory region.
1420Sstevel@tonic-gate  */
1430Sstevel@tonic-gate ibt_status_t
ibt_query_mr(ibt_hca_hdl_t hca_hdl,ibt_mr_hdl_t mr_hdl,ibt_mr_query_attr_t * attr)1440Sstevel@tonic-gate ibt_query_mr(ibt_hca_hdl_t hca_hdl, ibt_mr_hdl_t mr_hdl,
1450Sstevel@tonic-gate     ibt_mr_query_attr_t *attr)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_query_mr(%p, %p)", hca_hdl, mr_hdl);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	return (IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_query_mr(
1500Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), mr_hdl, attr));
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate  * Function:
1560Sstevel@tonic-gate  *	ibt_deregister_mr()
1570Sstevel@tonic-gate  * Input:
1580Sstevel@tonic-gate  *	hca_hdl   - HCA Handle.
1590Sstevel@tonic-gate  *	mr_hdl    - The IBT Memory Region handle.
1600Sstevel@tonic-gate  * Output:
1610Sstevel@tonic-gate  *      none.
1620Sstevel@tonic-gate  * Returns:
1630Sstevel@tonic-gate  *      IBT_SUCCESS
1640Sstevel@tonic-gate  *	IBT_CHAN_HDL_INVALID
1650Sstevel@tonic-gate  *	IBT_MR_HDL_INVALID
1660Sstevel@tonic-gate  *	IBT_MR_IN_USE
1670Sstevel@tonic-gate  * Description:
1680Sstevel@tonic-gate  *    De-register the registered memory region. Remove a memory region from a
1690Sstevel@tonic-gate  *    HCA translation table, and free all resources associated with the
1700Sstevel@tonic-gate  *    memory region.
1710Sstevel@tonic-gate  */
1720Sstevel@tonic-gate ibt_status_t
ibt_deregister_mr(ibt_hca_hdl_t hca_hdl,ibt_mr_hdl_t mr_hdl)1730Sstevel@tonic-gate ibt_deregister_mr(ibt_hca_hdl_t hca_hdl, ibt_mr_hdl_t mr_hdl)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	ibt_status_t 	status;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_deregister_mr(%p, %p)", hca_hdl, mr_hdl);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_deregister_mr(
1800Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), mr_hdl);
1810Sstevel@tonic-gate 	if (status == IBT_SUCCESS) {
18212574SWilliam.Taylor@Oracle.COM 		atomic_dec_32(&hca_hdl->ha_mr_cnt);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 	return (status);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate  * Function:
1900Sstevel@tonic-gate  *	ibt_reregister_mr()
1910Sstevel@tonic-gate  * Input:
1920Sstevel@tonic-gate  *	hca_hdl   - HCA Handle.
1930Sstevel@tonic-gate  *	mr_hdl    - The IBT Memory Region handle.
1940Sstevel@tonic-gate  *	pd        - Optional Protection Domain Handle.
1950Sstevel@tonic-gate  *	mem_attr  - Requested memory region attributes.
1960Sstevel@tonic-gate  * Output:
1970Sstevel@tonic-gate  *	mr_hdl_p  - The reregistered IBT memory region handle.
1980Sstevel@tonic-gate  *	mem_desc  - Returned memory descriptor for the new memory region.
1990Sstevel@tonic-gate  * Returns:
2000Sstevel@tonic-gate  *      IBT_SUCCESS
2010Sstevel@tonic-gate  *	IBT_CHAN_HDL_INVALID
2020Sstevel@tonic-gate  *	IBT_MR_HDL_INVALID
2030Sstevel@tonic-gate  *	IBT_MR_VA_INVALID
2040Sstevel@tonic-gate  *	IBT_MR_LEN_INVALID
2050Sstevel@tonic-gate  *	IBT_MR_ACCESS_REQ_INVALID
2060Sstevel@tonic-gate  *	IBT_PD_HDL_INVALID
2070Sstevel@tonic-gate  *	IBT_INSUFF_RESOURCE
2080Sstevel@tonic-gate  *	IBT_MR_IN_USE
2090Sstevel@tonic-gate  * Description:
2100Sstevel@tonic-gate  *    Modify the attributes of an existing memory region.
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate ibt_status_t
ibt_reregister_mr(ibt_hca_hdl_t hca_hdl,ibt_mr_hdl_t mr_hdl,ibt_pd_hdl_t pd,ibt_mr_attr_t * mem_attr,ibt_mr_hdl_t * mr_hdl_p,ibt_mr_desc_t * mem_desc)2130Sstevel@tonic-gate ibt_reregister_mr(ibt_hca_hdl_t hca_hdl, ibt_mr_hdl_t mr_hdl, ibt_pd_hdl_t pd,
2140Sstevel@tonic-gate     ibt_mr_attr_t *mem_attr, ibt_mr_hdl_t *mr_hdl_p, ibt_mr_desc_t *mem_desc)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	ibt_status_t 	status;
2170Sstevel@tonic-gate 	ib_vaddr_t 	vaddr = mem_attr->mr_vaddr;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_reregister_mr(%p, %p, %p, %p)",
2200Sstevel@tonic-gate 	    hca_hdl, mr_hdl, pd, mem_attr);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_reregister_mr(
2230Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), mr_hdl, pd, mem_attr,
2240Sstevel@tonic-gate 	    IBTL_HCA2CLNT(hca_hdl), mr_hdl_p, mem_desc);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	if (status == IBT_SUCCESS)
2270Sstevel@tonic-gate 		mem_desc->md_vaddr = vaddr;
2280Sstevel@tonic-gate 	else if (!(status == IBT_MR_IN_USE || status == IBT_HCA_HDL_INVALID ||
2290Sstevel@tonic-gate 	    status == IBT_MR_HDL_INVALID)) {
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 		IBTF_DPRINTF_L2(ibtl_mem, "ibt_reregister_mr: "
2320Sstevel@tonic-gate 		    "Re-registration Failed: %d", status);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 		/* we lost one memory region resource */
23512574SWilliam.Taylor@Oracle.COM 		atomic_dec_32(&hca_hdl->ha_mr_cnt);
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	return (status);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate  * Function:
2440Sstevel@tonic-gate  *	ibt_reregister_buf()
2450Sstevel@tonic-gate  * Input:
2460Sstevel@tonic-gate  *	hca_hdl		HCA Handle.
2470Sstevel@tonic-gate  *	mr_hdl		The IBT Memory Region handle.
2480Sstevel@tonic-gate  *	pd		Optional Protection Domain Handle.
2490Sstevel@tonic-gate  *	mem_bpattr	Memory Registration attributes (IOVA and flags).
2500Sstevel@tonic-gate  *	bp		A pointer to a buf(9S) struct.
2510Sstevel@tonic-gate  * Output:
2520Sstevel@tonic-gate  *	mr_hdl_p	The reregistered IBT memory region handle.
2530Sstevel@tonic-gate  *	mem_desc	Returned memory descriptor for the new memory region.
2540Sstevel@tonic-gate  * Returns:
2550Sstevel@tonic-gate  *      IBT_SUCCESS
2560Sstevel@tonic-gate  *	IBT_CHAN_HDL_INVALID
2570Sstevel@tonic-gate  *	IBT_MR_HDL_INVALID
2580Sstevel@tonic-gate  *	IBT_MR_VA_INVALID
2590Sstevel@tonic-gate  *	IBT_MR_LEN_INVALID
2600Sstevel@tonic-gate  *	IBT_MR_ACCESS_REQ_INVALID
2610Sstevel@tonic-gate  *	IBT_PD_HDL_INVALID
2620Sstevel@tonic-gate  *	IBT_INSUFF_RESOURCE
2630Sstevel@tonic-gate  *	IBT_MR_IN_USE
2640Sstevel@tonic-gate  * Description:
2650Sstevel@tonic-gate  *	Modify the attributes of an existing memory region as described by a
2660Sstevel@tonic-gate  *	buf(9S) struct for use by a HCA.  A description of the registered
2670Sstevel@tonic-gate  *	memory suitable for use in Work Requests (WRs) is returned in the
2680Sstevel@tonic-gate  *	ibt_mr_desc_t parameter.
2690Sstevel@tonic-gate  */
2700Sstevel@tonic-gate ibt_status_t
ibt_reregister_buf(ibt_hca_hdl_t hca_hdl,ibt_mr_hdl_t mr_hdl,ibt_pd_hdl_t pd,ibt_smr_attr_t * mem_bpattr,struct buf * bp,ibt_mr_hdl_t * mr_hdl_p,ibt_mr_desc_t * mem_desc)2710Sstevel@tonic-gate ibt_reregister_buf(ibt_hca_hdl_t hca_hdl, ibt_mr_hdl_t mr_hdl,
2720Sstevel@tonic-gate     ibt_pd_hdl_t pd, ibt_smr_attr_t *mem_bpattr, struct buf *bp,
2730Sstevel@tonic-gate     ibt_mr_hdl_t *mr_hdl_p, ibt_mr_desc_t *mem_desc)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	ibt_status_t 		status;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_reregister_buf(%p, %p, %p, %p, %p)",
2780Sstevel@tonic-gate 	    hca_hdl, mr_hdl, pd, mem_bpattr, bp);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_reregister_buf(
2810Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), mr_hdl, pd, mem_bpattr, bp,
2820Sstevel@tonic-gate 	    IBTL_HCA2CLNT(hca_hdl), mr_hdl_p, mem_desc);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if (!(status == IBT_SUCCESS || status == IBT_MR_IN_USE ||
2850Sstevel@tonic-gate 	    status == IBT_HCA_HDL_INVALID || status == IBT_MR_HDL_INVALID)) {
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 		IBTF_DPRINTF_L2(ibtl_mem, "ibt_reregister_buf: "
2880Sstevel@tonic-gate 		    "Re-registration Mem Failed: %d", status);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 		/* we lost one memory region resource */
29112574SWilliam.Taylor@Oracle.COM 		atomic_dec_32(&hca_hdl->ha_mr_cnt);
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 	return (status);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate  * Function:
2990Sstevel@tonic-gate  *	ibt_register_shared_mr()
3000Sstevel@tonic-gate  * Input:
3010Sstevel@tonic-gate  *	hca_hdl   - HCA Handle.
3020Sstevel@tonic-gate  *	mr_hdl    - The IBT Memory Region handle.
3030Sstevel@tonic-gate  *	pd        - Protection Domain Handle.
3040Sstevel@tonic-gate  *	mem_sattr - Requested memory region shared attributes.
3050Sstevel@tonic-gate  * Output:
3060Sstevel@tonic-gate  *	mr_hdl_p  - The reregistered IBT memory region handle.
3070Sstevel@tonic-gate  *	mem_desc  - Returned memory descriptor for the new memory region.
3080Sstevel@tonic-gate  * Returns:
3090Sstevel@tonic-gate  *      IBT_SUCCESS
3100Sstevel@tonic-gate  *	IBT_INSUFF_RESOURCE
3110Sstevel@tonic-gate  *	IBT_CHAN_HDL_INVALID
3120Sstevel@tonic-gate  *	IBT_MR_HDL_INVALID
3130Sstevel@tonic-gate  *	IBT_PD_HDL_INVALID
3140Sstevel@tonic-gate  *	IBT_MR_ACCESS_REQ_INVALID
3150Sstevel@tonic-gate  * Description:
3160Sstevel@tonic-gate  *    Given an existing memory region, a new memory region associated with
3170Sstevel@tonic-gate  *    the same physical locations is created.
3180Sstevel@tonic-gate  */
3190Sstevel@tonic-gate ibt_status_t
ibt_register_shared_mr(ibt_hca_hdl_t hca_hdl,ibt_mr_hdl_t mr_hdl,ibt_pd_hdl_t pd,ibt_smr_attr_t * mem_sattr,ibt_mr_hdl_t * mr_hdl_p,ibt_mr_desc_t * mem_desc)3200Sstevel@tonic-gate ibt_register_shared_mr(ibt_hca_hdl_t hca_hdl, ibt_mr_hdl_t mr_hdl,
3210Sstevel@tonic-gate     ibt_pd_hdl_t pd, ibt_smr_attr_t *mem_sattr, ibt_mr_hdl_t *mr_hdl_p,
3220Sstevel@tonic-gate     ibt_mr_desc_t *mem_desc)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate 	ibt_status_t		status;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_register_shared_mr(%p, %p, %p, %p)",
3270Sstevel@tonic-gate 	    hca_hdl, mr_hdl, pd, mem_sattr);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_register_shared_mr(
3300Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), mr_hdl, pd, mem_sattr,
3310Sstevel@tonic-gate 	    IBTL_HCA2CLNT(hca_hdl), mr_hdl_p, mem_desc);
3320Sstevel@tonic-gate 	if (status == IBT_SUCCESS) {
33312574SWilliam.Taylor@Oracle.COM 		atomic_inc_32(&hca_hdl->ha_mr_cnt);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 	return (status);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate /*
3390Sstevel@tonic-gate  * Function:
3400Sstevel@tonic-gate  *	ibt_sync_mr()
3410Sstevel@tonic-gate  * Input:
3420Sstevel@tonic-gate  *	hca_hdl		- HCA Handle.
3430Sstevel@tonic-gate  *	mr_segments	- A pointer to an array of ibt_mr_sync_t that describes
3440Sstevel@tonic-gate  *			  the memory regions to sync.
3450Sstevel@tonic-gate  *	num_segments	- The length of the mr_segments array.
3460Sstevel@tonic-gate  * Output:
3470Sstevel@tonic-gate  *	NONE
3480Sstevel@tonic-gate  * Returns:
3490Sstevel@tonic-gate  *      IBT_SUCCESS
3500Sstevel@tonic-gate  *	IBT_HCA_HDL_INVALID
3510Sstevel@tonic-gate  *	IBT_MR_HDL_INVALID
3520Sstevel@tonic-gate  *	IBT_INVALID_PARAM
3530Sstevel@tonic-gate  *	IBT_MR_VA_INVALID
3540Sstevel@tonic-gate  *	IBT_MR_LEN_INVALID
3550Sstevel@tonic-gate  * Description:
3560Sstevel@tonic-gate  *	Make memory changes visible to incoming RDMA reads, or make the affects
3570Sstevel@tonic-gate  *	of an incoming RDMA writes visible to the consumer.
3580Sstevel@tonic-gate  */
3590Sstevel@tonic-gate ibt_status_t
ibt_sync_mr(ibt_hca_hdl_t hca_hdl,ibt_mr_sync_t * mr_segments,size_t num_segments)3600Sstevel@tonic-gate ibt_sync_mr(ibt_hca_hdl_t hca_hdl, ibt_mr_sync_t *mr_segments,
3610Sstevel@tonic-gate     size_t num_segments)
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_sync_mr(%p, %p, %d)", hca_hdl,
3650Sstevel@tonic-gate 	    mr_segments, num_segments);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	return (IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_sync_mr(
3680Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), mr_segments, num_segments));
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate /*
3730Sstevel@tonic-gate  * Function:
3740Sstevel@tonic-gate  *	ibt_alloc_mw()
3750Sstevel@tonic-gate  * Input:
3760Sstevel@tonic-gate  *	hca_hdl   - HCA Handle.
3770Sstevel@tonic-gate  *	pd        - Protection Domain Handle.
3780Sstevel@tonic-gate  *	flags     - Memory Window alloc flags.
3790Sstevel@tonic-gate  * Output:
3800Sstevel@tonic-gate  *	mw_hdl_p  - The returned IBT Memory Window handle.
3810Sstevel@tonic-gate  *	rkey      - The IBT R_Key handle.
3820Sstevel@tonic-gate  * Returns:
3830Sstevel@tonic-gate  *      IBT_SUCCESS
3840Sstevel@tonic-gate  *	IBT_INSUFF_RESOURCE
3850Sstevel@tonic-gate  *	IBT_CHAN_HDL_INVALID
3860Sstevel@tonic-gate  *	IBT_PD_HDL_INVALID
3870Sstevel@tonic-gate  * Description:
3880Sstevel@tonic-gate  *    Allocate a memory window from the HCA.
3890Sstevel@tonic-gate  */
3900Sstevel@tonic-gate ibt_status_t
ibt_alloc_mw(ibt_hca_hdl_t hca_hdl,ibt_pd_hdl_t pd,ibt_mw_flags_t flags,ibt_mw_hdl_t * mw_hdl_p,ibt_rkey_t * rkey)3910Sstevel@tonic-gate ibt_alloc_mw(ibt_hca_hdl_t hca_hdl, ibt_pd_hdl_t pd, ibt_mw_flags_t flags,
3920Sstevel@tonic-gate     ibt_mw_hdl_t *mw_hdl_p, ibt_rkey_t *rkey)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	ibt_status_t		status;
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_alloc_mw(%p, %p, 0x%x)",
3970Sstevel@tonic-gate 	    hca_hdl, pd, flags);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_alloc_mw(
4000Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), pd, flags, mw_hdl_p, rkey);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	/*
4030Sstevel@tonic-gate 	 * XXX - We should be able to allocate state and have a IBTF Memory
4040Sstevel@tonic-gate 	 * Window Handle. Memory Windows are meant to be rebound on the fly
4050Sstevel@tonic-gate 	 * (using a post) to make them fast. It is expected that alloc memory
4060Sstevel@tonic-gate 	 * window will be done in a relatively static manner. But, we don't have
4070Sstevel@tonic-gate 	 * a good reason to have local MW state at this point, so we won't.
4080Sstevel@tonic-gate 	 */
4090Sstevel@tonic-gate 	if (status == IBT_SUCCESS) {
41012574SWilliam.Taylor@Oracle.COM 		atomic_inc_32(&hca_hdl->ha_mw_cnt);
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 	return (status);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate /*
4170Sstevel@tonic-gate  * Function:
4180Sstevel@tonic-gate  *	ibt_query_mw()
4190Sstevel@tonic-gate  * Input:
4200Sstevel@tonic-gate  *	hca_hdl   - HCA Handle.
4210Sstevel@tonic-gate  *	mw_hdl    - The IBT Memory Window handle.
4220Sstevel@tonic-gate  * Output:
4230Sstevel@tonic-gate  *	pd        - Protection Domain Handle.
4240Sstevel@tonic-gate  *	rkey      - The IBT R_Key handle.
4250Sstevel@tonic-gate  * Returns:
4260Sstevel@tonic-gate  *      IBT_SUCCESS
4270Sstevel@tonic-gate  *	IBT_CHAN_HDL_INVALID
4280Sstevel@tonic-gate  *	IBT_MW_HDL_INVALID
4290Sstevel@tonic-gate  * Description:
4300Sstevel@tonic-gate  *    Retrieves information about a specified memory region.
4310Sstevel@tonic-gate  */
4320Sstevel@tonic-gate ibt_status_t
ibt_query_mw(ibt_hca_hdl_t hca_hdl,ibt_mw_hdl_t mw_hdl,ibt_mw_query_attr_t * mw_attr_p)4330Sstevel@tonic-gate ibt_query_mw(ibt_hca_hdl_t hca_hdl, ibt_mw_hdl_t mw_hdl,
4340Sstevel@tonic-gate     ibt_mw_query_attr_t *mw_attr_p)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_query_mw(%p, %p)", hca_hdl, mw_hdl);
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	return (IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_query_mw(
4390Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), mw_hdl, mw_attr_p));
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate  * Function:
4450Sstevel@tonic-gate  *	ibt_free_mw()
4460Sstevel@tonic-gate  * Input:
4470Sstevel@tonic-gate  *      hca_hdl   - HCA Handle
4480Sstevel@tonic-gate  *	mw_hdl    - The IBT Memory Window handle.
4490Sstevel@tonic-gate  * Output:
4500Sstevel@tonic-gate  *	none.
4510Sstevel@tonic-gate  * Returns:
4520Sstevel@tonic-gate  *      IBT_SUCCESS
4530Sstevel@tonic-gate  *	IBT_CHAN_HDL_INVALID
4540Sstevel@tonic-gate  *	IBT_MW_HDL_INVALID
4550Sstevel@tonic-gate  * Description:
4560Sstevel@tonic-gate  *    De-allocate the Memory Window.
4570Sstevel@tonic-gate  */
4580Sstevel@tonic-gate ibt_status_t
ibt_free_mw(ibt_hca_hdl_t hca_hdl,ibt_mw_hdl_t mw_hdl)4590Sstevel@tonic-gate ibt_free_mw(ibt_hca_hdl_t hca_hdl, ibt_mw_hdl_t mw_hdl)
4600Sstevel@tonic-gate {
4610Sstevel@tonic-gate 	ibt_status_t		status;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_free_mw(%p, %p)", hca_hdl, mw_hdl);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_free_mw(
4660Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), mw_hdl);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	if (status == IBT_SUCCESS) {
46912574SWilliam.Taylor@Oracle.COM 		atomic_dec_32(&hca_hdl->ha_mw_cnt);
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 	return (status);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate /*
4760Sstevel@tonic-gate  * Function:
4770Sstevel@tonic-gate  *	ibt_map_mem_area()
4780Sstevel@tonic-gate  * Input:
4790Sstevel@tonic-gate  *      hca_hdl		HCA Handle
4800Sstevel@tonic-gate  *	va_attrs	A pointer to an ibt_va_attr_t that describes the
4810Sstevel@tonic-gate  *			VA to be translated.
4820Sstevel@tonic-gate  *	paddr_list_len	The number of entries in the 'paddr_list_p' array.
4830Sstevel@tonic-gate  * Output:
4840Sstevel@tonic-gate  *	paddr_list_p	Array of ibt_phys_buf_t (allocated by the caller),
4850Sstevel@tonic-gate  *			in which the physical buffers that map the virtual
4860Sstevel@tonic-gate  *			buffer are returned.
4870Sstevel@tonic-gate  *	num_paddr_p	The actual number of ibt_phys_buf_t that were
4880Sstevel@tonic-gate  *			returned in the 'paddr_list_p' array.
4890Sstevel@tonic-gate  *	ma_hdl_p	Memory Area Handle.
4900Sstevel@tonic-gate  * Returns:
4910Sstevel@tonic-gate  *      IBT_SUCCESS
4920Sstevel@tonic-gate  * Description:
4930Sstevel@tonic-gate  * 	Translate a kernel virtual address range into HCA physical addresses.
4940Sstevel@tonic-gate  *	A set of physical addresses, that can be used with "Reserved L_Key",
4950Sstevel@tonic-gate  *	register physical,  and "Fast Registration Work Request" operations
4960Sstevel@tonic-gate  *	is returned.
4970Sstevel@tonic-gate  */
4980Sstevel@tonic-gate ibt_status_t
ibt_map_mem_area(ibt_hca_hdl_t hca_hdl,ibt_va_attr_t * va_attrs,uint_t paddr_list_len,ibt_reg_req_t * reg_req,ibt_ma_hdl_t * ma_hdl_p)4990Sstevel@tonic-gate ibt_map_mem_area(ibt_hca_hdl_t hca_hdl, ibt_va_attr_t *va_attrs,
50011972SBill.Taylor@Sun.COM     uint_t paddr_list_len, ibt_reg_req_t *reg_req, ibt_ma_hdl_t *ma_hdl_p)
5010Sstevel@tonic-gate {
50211972SBill.Taylor@Sun.COM 	ibt_status_t	status;
503929Ssrust 
5040Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_map_mem_area(%p, %p, %d)",
5050Sstevel@tonic-gate 	    hca_hdl, va_attrs, paddr_list_len);
5060Sstevel@tonic-gate 
507929Ssrust 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_map_mem_area(
5080Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), va_attrs,
5090Sstevel@tonic-gate 	    NULL, /* IBTL_HCA2MODI_P(hca_hdl)->mi_reserved */
51011972SBill.Taylor@Sun.COM 	    paddr_list_len, reg_req, ma_hdl_p);
51112574SWilliam.Taylor@Oracle.COM 	/* Not doing reference counting, which adversely effects performance */
51212574SWilliam.Taylor@Oracle.COM 
513929Ssrust 	return (status);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate /*
5180Sstevel@tonic-gate  * Function:
5190Sstevel@tonic-gate  *	ibt_unmap_mem_area()
5200Sstevel@tonic-gate  * Input:
5210Sstevel@tonic-gate  *      hca_hdl		HCA Handle
5220Sstevel@tonic-gate  *	ma_hdl		Memory Area Handle.
5230Sstevel@tonic-gate  * Output:
5240Sstevel@tonic-gate  *	None.
5250Sstevel@tonic-gate  * Returns:
5260Sstevel@tonic-gate  *      IBT_SUCCESS
5270Sstevel@tonic-gate  * Description:
5280Sstevel@tonic-gate  * 	Un pin physical pages pinned during an ibt_map_mem_area() call.
5290Sstevel@tonic-gate  */
5300Sstevel@tonic-gate ibt_status_t
ibt_unmap_mem_area(ibt_hca_hdl_t hca_hdl,ibt_ma_hdl_t ma_hdl)5310Sstevel@tonic-gate ibt_unmap_mem_area(ibt_hca_hdl_t hca_hdl, ibt_ma_hdl_t ma_hdl)
5320Sstevel@tonic-gate {
533929Ssrust 	ibt_status_t 	status;
534929Ssrust 
5350Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_unmap_mem_area(%p, %p)",
5360Sstevel@tonic-gate 	    hca_hdl, ma_hdl);
5370Sstevel@tonic-gate 
538929Ssrust 	status = (IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_unmap_mem_area(
5390Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), ma_hdl));
54012574SWilliam.Taylor@Oracle.COM 	/* Not doing reference counting, which adversely effects performance */
541929Ssrust 
542929Ssrust 	return (status);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate 
5458580SBill.Taylor@Sun.COM /*
5468580SBill.Taylor@Sun.COM  * Function:
5478580SBill.Taylor@Sun.COM  *	ibt_map_mem_iov()
5488580SBill.Taylor@Sun.COM  * Input:
5498580SBill.Taylor@Sun.COM  *      hca_hdl		HCA Handle
5508580SBill.Taylor@Sun.COM  *	iov_attr	A pointer to an ibt_iov_attr_t that describes the
5518580SBill.Taylor@Sun.COM  *			virtual ranges to be translated.
5528580SBill.Taylor@Sun.COM  * Output:
5538580SBill.Taylor@Sun.COM  *	wr		A pointer to the work request where the output
5548580SBill.Taylor@Sun.COM  *			sgl (reserved_lkey, size, paddr) will be written.
5558580SBill.Taylor@Sun.COM  *	mi_hdl_p	Memory IOV Handle.
5568580SBill.Taylor@Sun.COM  * Returns:
5578580SBill.Taylor@Sun.COM  *      IBT_SUCCESS
5588580SBill.Taylor@Sun.COM  * Description:
5598580SBill.Taylor@Sun.COM  * 	Translate an array of virtual address ranges into HCA physical
5608580SBill.Taylor@Sun.COM  *	addresses, sizes, and reserved_lkey.
5618580SBill.Taylor@Sun.COM  */
5628580SBill.Taylor@Sun.COM ibt_status_t
ibt_map_mem_iov(ibt_hca_hdl_t hca_hdl,ibt_iov_attr_t * iov_attr,ibt_all_wr_t * wr,ibt_mi_hdl_t * mi_hdl_p)5638580SBill.Taylor@Sun.COM ibt_map_mem_iov(ibt_hca_hdl_t hca_hdl, ibt_iov_attr_t *iov_attr,
5648580SBill.Taylor@Sun.COM     ibt_all_wr_t *wr, ibt_mi_hdl_t *mi_hdl_p)
5658580SBill.Taylor@Sun.COM {
5668580SBill.Taylor@Sun.COM 	ibt_status_t 	status;
5678580SBill.Taylor@Sun.COM 
5688580SBill.Taylor@Sun.COM 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_map_mem_iov(%p, %p, %p)",
5698580SBill.Taylor@Sun.COM 	    hca_hdl, iov_attr, wr);
5708580SBill.Taylor@Sun.COM 
5718580SBill.Taylor@Sun.COM 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_map_mem_iov(
5728580SBill.Taylor@Sun.COM 	    IBTL_HCA2CIHCA(hca_hdl), iov_attr, wr, mi_hdl_p);
57312574SWilliam.Taylor@Oracle.COM 	/* Not doing reference counting, which adversely effects performance */
5748580SBill.Taylor@Sun.COM 
5758580SBill.Taylor@Sun.COM 	return (status);
5768580SBill.Taylor@Sun.COM }
5778580SBill.Taylor@Sun.COM 
5788580SBill.Taylor@Sun.COM 
5798580SBill.Taylor@Sun.COM /*
5808580SBill.Taylor@Sun.COM  * Function:
5818580SBill.Taylor@Sun.COM  *	ibt_unmap_mem_iov()
5828580SBill.Taylor@Sun.COM  * Input:
5838580SBill.Taylor@Sun.COM  *      hca_hdl		HCA Handle
5848580SBill.Taylor@Sun.COM  *	mi_hdl		Memory IOV Handle.
5858580SBill.Taylor@Sun.COM  * Output:
5868580SBill.Taylor@Sun.COM  *	None.
5878580SBill.Taylor@Sun.COM  * Returns:
5888580SBill.Taylor@Sun.COM  *      IBT_SUCCESS
5898580SBill.Taylor@Sun.COM  * Description:
5908580SBill.Taylor@Sun.COM  * 	Un pin physical pages pinned during an ibt_map_mem_iov() call.
5918580SBill.Taylor@Sun.COM  */
5928580SBill.Taylor@Sun.COM ibt_status_t
ibt_unmap_mem_iov(ibt_hca_hdl_t hca_hdl,ibt_mi_hdl_t mi_hdl)5938580SBill.Taylor@Sun.COM ibt_unmap_mem_iov(ibt_hca_hdl_t hca_hdl, ibt_mi_hdl_t mi_hdl)
5948580SBill.Taylor@Sun.COM {
5958580SBill.Taylor@Sun.COM 	ibt_status_t 	status;
5968580SBill.Taylor@Sun.COM 
5978580SBill.Taylor@Sun.COM 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_unmap_mem_iov(%p, %p)",
5988580SBill.Taylor@Sun.COM 	    hca_hdl, mi_hdl);
5998580SBill.Taylor@Sun.COM 
6008580SBill.Taylor@Sun.COM 	status = (IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_unmap_mem_iov(
6018580SBill.Taylor@Sun.COM 	    IBTL_HCA2CIHCA(hca_hdl), mi_hdl));
60212574SWilliam.Taylor@Oracle.COM 	/* Not doing reference counting, which adversely effects performance */
6038580SBill.Taylor@Sun.COM 
6048580SBill.Taylor@Sun.COM 	return (status);
6058580SBill.Taylor@Sun.COM }
6068580SBill.Taylor@Sun.COM 
6078580SBill.Taylor@Sun.COM /*
6088580SBill.Taylor@Sun.COM  * Function:
6098580SBill.Taylor@Sun.COM  *	ibt_alloc_io_mem()
6108580SBill.Taylor@Sun.COM  * Input:
6118580SBill.Taylor@Sun.COM  *      hca_hdl		HCA Handle
6128580SBill.Taylor@Sun.COM  *	size		Number of bytes to allocate
6138580SBill.Taylor@Sun.COM  *	mr_flag		Possible values: IBT_MR_SLEEP, IBT_MR_NONCOHERENT
6148580SBill.Taylor@Sun.COM  * Output:
6158580SBill.Taylor@Sun.COM  *     	kaddrp 		Contains pointer to the virtual address of the
6168580SBill.Taylor@Sun.COM  *			memory allocated by this call.  (Set to NULL if
6178580SBill.Taylor@Sun.COM  *			memory allocation fails).
6188580SBill.Taylor@Sun.COM  *	mem_alloc_hdl	Memory access handle returned by ibt_mem_alloc()
6198580SBill.Taylor@Sun.COM  *
6208580SBill.Taylor@Sun.COM  * Returns:
6218580SBill.Taylor@Sun.COM  *      IBT_SUCCESS
6228580SBill.Taylor@Sun.COM  *	IBT_INSUFF_RESOURCE
6238580SBill.Taylor@Sun.COM  * 	IBT_HCA_HDL_INVALID
6248580SBill.Taylor@Sun.COM  *	IBT_MR_ACCESS_REQ_INVALID
6258580SBill.Taylor@Sun.COM  *	IBT_INVALID_PARAM
6268580SBill.Taylor@Sun.COM  * Description:
6278580SBill.Taylor@Sun.COM  *	Wrapper for ddi_dma_mem_alloc()
6288580SBill.Taylor@Sun.COM  */
6298580SBill.Taylor@Sun.COM ibt_status_t
ibt_alloc_io_mem(ibt_hca_hdl_t hca_hdl,size_t size,ibt_mr_flags_t mr_flag,caddr_t * kaddrp,ibt_mem_alloc_hdl_t * mem_alloc_hdl)6308580SBill.Taylor@Sun.COM ibt_alloc_io_mem(ibt_hca_hdl_t hca_hdl, size_t size, ibt_mr_flags_t mr_flag,
6318580SBill.Taylor@Sun.COM     caddr_t *kaddrp, ibt_mem_alloc_hdl_t *mem_alloc_hdl)
6328580SBill.Taylor@Sun.COM {
6338580SBill.Taylor@Sun.COM 	return (IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_alloc_io_mem(
6348580SBill.Taylor@Sun.COM 	    IBTL_HCA2CIHCA(hca_hdl), size, mr_flag, kaddrp,
6358580SBill.Taylor@Sun.COM 	    (ibc_mem_alloc_hdl_t *)mem_alloc_hdl));
6368580SBill.Taylor@Sun.COM }
6378580SBill.Taylor@Sun.COM 
6388580SBill.Taylor@Sun.COM /*
6398580SBill.Taylor@Sun.COM  * Function:
6408580SBill.Taylor@Sun.COM  *	ibt_free_io_mem()
6418580SBill.Taylor@Sun.COM  * Input:
6428580SBill.Taylor@Sun.COM  *      hca_hdl		HCA Handle
6438580SBill.Taylor@Sun.COM  *	mem_alloc_hdl	Memory access handle returned by ibt_mem_alloc()
6448580SBill.Taylor@Sun.COM  * Output:
6458580SBill.Taylor@Sun.COM  *	None
6468580SBill.Taylor@Sun.COM  *
6478580SBill.Taylor@Sun.COM  * Returns:
6488580SBill.Taylor@Sun.COM  *      IBT_SUCCESS
6498580SBill.Taylor@Sun.COM  * Description:
6508580SBill.Taylor@Sun.COM  *	Wrapper for ddi_dma_mem_free()
6518580SBill.Taylor@Sun.COM  */
6528580SBill.Taylor@Sun.COM ibt_status_t
ibt_free_io_mem(ibt_hca_hdl_t hca_hdl,ibt_mem_alloc_hdl_t mem_alloc_hdl)6538580SBill.Taylor@Sun.COM ibt_free_io_mem(ibt_hca_hdl_t hca_hdl, ibt_mem_alloc_hdl_t mem_alloc_hdl)
6548580SBill.Taylor@Sun.COM {
6558580SBill.Taylor@Sun.COM 	return (IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_free_io_mem(
6568580SBill.Taylor@Sun.COM 	    IBTL_HCA2CIHCA(hca_hdl), (ibc_mem_alloc_hdl_t)mem_alloc_hdl));
6578580SBill.Taylor@Sun.COM }
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate /*
6600Sstevel@tonic-gate  * Function:
6610Sstevel@tonic-gate  *	ibt_alloc_lkey()
6620Sstevel@tonic-gate  * Input:
6630Sstevel@tonic-gate  *      hca_hdl			HCA Handle
6640Sstevel@tonic-gate  *	pd			A protection domain handle.
6650Sstevel@tonic-gate  *	flags			Access control.
6660Sstevel@tonic-gate  *	phys_buf_list_sz	Requested size of Physical Buffer List (PBL)
6670Sstevel@tonic-gate  *				resources to be allocated.
6680Sstevel@tonic-gate  * Output:
6690Sstevel@tonic-gate  *	mr_hdl_p		The returned IBT memory region handle.
6700Sstevel@tonic-gate  *	mem_desc_p		Returned memory descriptor.
6710Sstevel@tonic-gate  * Returns:
6720Sstevel@tonic-gate  *      IBT_SUCCESS
6730Sstevel@tonic-gate  * Description:
6740Sstevel@tonic-gate  * 	Allocates physical buffer list resources for use in memory
6750Sstevel@tonic-gate  *	registrations.
6760Sstevel@tonic-gate  */
6770Sstevel@tonic-gate ibt_status_t
ibt_alloc_lkey(ibt_hca_hdl_t hca_hdl,ibt_pd_hdl_t pd,ibt_lkey_flags_t flags,uint_t phys_buf_list_sz,ibt_mr_hdl_t * mr_hdl_p,ibt_pmr_desc_t * mem_desc_p)6780Sstevel@tonic-gate ibt_alloc_lkey(ibt_hca_hdl_t hca_hdl, ibt_pd_hdl_t pd, ibt_lkey_flags_t flags,
6790Sstevel@tonic-gate     uint_t phys_buf_list_sz, ibt_mr_hdl_t *mr_hdl_p,
6800Sstevel@tonic-gate     ibt_pmr_desc_t *mem_desc_p)
6810Sstevel@tonic-gate {
682929Ssrust 	ibt_status_t 	status;
683929Ssrust 
6840Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_alloc_lkey(%p, %p, 0x%X, %d)",
6850Sstevel@tonic-gate 	    hca_hdl, pd, flags, phys_buf_list_sz);
6860Sstevel@tonic-gate 
687929Ssrust 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_alloc_lkey(
6880Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), pd, flags, phys_buf_list_sz, mr_hdl_p,
689929Ssrust 	    mem_desc_p);
690929Ssrust 	if (status == IBT_SUCCESS) {
69112574SWilliam.Taylor@Oracle.COM 		atomic_inc_32(&hca_hdl->ha_mr_cnt);
692929Ssrust 	}
693929Ssrust 
694929Ssrust 	return (status);
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate /*
6990Sstevel@tonic-gate  * Function:
7000Sstevel@tonic-gate  *	ibt_register_phys_mr()
7010Sstevel@tonic-gate  * Input:
7020Sstevel@tonic-gate  *      hca_hdl		HCA Handle
7030Sstevel@tonic-gate  *	pd		A protection domain handle.
7040Sstevel@tonic-gate  *	mem_pattr	Requested memory region physical attributes.
7050Sstevel@tonic-gate  * Output:
7060Sstevel@tonic-gate  *	mr_hdl_p	The returned IBT memory region handle.
7070Sstevel@tonic-gate  *	mem_desc_p	Returned memory descriptor.
7080Sstevel@tonic-gate  * Returns:
7090Sstevel@tonic-gate  *      IBT_SUCCESS
7100Sstevel@tonic-gate  * Description:
7110Sstevel@tonic-gate  * 	Prepares a physically addressed memory region for use by a HCA.
7120Sstevel@tonic-gate  */
7130Sstevel@tonic-gate ibt_status_t
ibt_register_phys_mr(ibt_hca_hdl_t hca_hdl,ibt_pd_hdl_t pd,ibt_pmr_attr_t * mem_pattr,ibt_mr_hdl_t * mr_hdl_p,ibt_pmr_desc_t * mem_desc_p)7140Sstevel@tonic-gate ibt_register_phys_mr(ibt_hca_hdl_t hca_hdl, ibt_pd_hdl_t pd,
7150Sstevel@tonic-gate     ibt_pmr_attr_t *mem_pattr, ibt_mr_hdl_t *mr_hdl_p,
7160Sstevel@tonic-gate     ibt_pmr_desc_t *mem_desc_p)
7170Sstevel@tonic-gate {
718929Ssrust 	ibt_status_t 	status;
719929Ssrust 
7200Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_register_phys_mr(%p, %p, %p)",
7210Sstevel@tonic-gate 	    hca_hdl, pd, mem_pattr);
7220Sstevel@tonic-gate 
723929Ssrust 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_register_physical_mr(
7240Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), pd, mem_pattr,
7250Sstevel@tonic-gate 	    NULL, /* IBTL_HCA2MODI_P(hca_hdl)->mi_reserved */
726929Ssrust 	    mr_hdl_p, mem_desc_p);
727929Ssrust 	if (status == IBT_SUCCESS) {
72812574SWilliam.Taylor@Oracle.COM 		atomic_inc_32(&hca_hdl->ha_mr_cnt);
729929Ssrust 	}
730929Ssrust 
731929Ssrust 	return (status);
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate /*
7360Sstevel@tonic-gate  * Function:
7370Sstevel@tonic-gate  *	ibt_reregister_phys_mr()
7380Sstevel@tonic-gate  * Input:
7390Sstevel@tonic-gate  *      hca_hdl		HCA Handle
7400Sstevel@tonic-gate  *	mr_hdl		The IBT memory region handle.
7410Sstevel@tonic-gate  *	pd		A protection domain handle.
7420Sstevel@tonic-gate  *	mem_pattr	Requested memory region physical attributes.
7430Sstevel@tonic-gate  * Output:
7440Sstevel@tonic-gate  *	mr_hdl_p	The returned IBT memory region handle.
7450Sstevel@tonic-gate  *	mem_desc_p	Returned memory descriptor.
7460Sstevel@tonic-gate  * Returns:
7470Sstevel@tonic-gate  *      IBT_SUCCESS
7480Sstevel@tonic-gate  * Description:
7490Sstevel@tonic-gate  * 	Prepares a physically addressed memory region for use by a HCA.
7500Sstevel@tonic-gate  */
7510Sstevel@tonic-gate ibt_status_t
ibt_reregister_phys_mr(ibt_hca_hdl_t hca_hdl,ibt_mr_hdl_t mr_hdl,ibt_pd_hdl_t pd,ibt_pmr_attr_t * mem_pattr,ibt_mr_hdl_t * mr_hdl_p,ibt_pmr_desc_t * mem_desc_p)7520Sstevel@tonic-gate ibt_reregister_phys_mr(ibt_hca_hdl_t hca_hdl, ibt_mr_hdl_t mr_hdl,
7530Sstevel@tonic-gate     ibt_pd_hdl_t pd, ibt_pmr_attr_t *mem_pattr, ibt_mr_hdl_t *mr_hdl_p,
7540Sstevel@tonic-gate     ibt_pmr_desc_t *mem_desc_p)
7550Sstevel@tonic-gate {
756929Ssrust 	ibt_status_t 	status;
757929Ssrust 
7580Sstevel@tonic-gate 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_reregister_phys_mr(%p, %p, %p, %p)",
7590Sstevel@tonic-gate 	    hca_hdl, mr_hdl, pd, mem_pattr);
7600Sstevel@tonic-gate 
761929Ssrust 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_reregister_physical_mr(
7620Sstevel@tonic-gate 	    IBTL_HCA2CIHCA(hca_hdl), mr_hdl, pd, mem_pattr,
7630Sstevel@tonic-gate 	    NULL, /* IBTL_HCA2MODI_P(hca_hdl)->mi_reserved */
764929Ssrust 	    mr_hdl_p, mem_desc_p);
765929Ssrust 
766929Ssrust 	if (!(status == IBT_SUCCESS || status == IBT_MR_IN_USE ||
767929Ssrust 	    status == IBT_HCA_HDL_INVALID || status == IBT_MR_HDL_INVALID)) {
768929Ssrust 		IBTF_DPRINTF_L2(ibtl_mem, "ibt_reregister_phys_mr: "
769929Ssrust 		    "Re-registration Mem Failed: %d", status);
770929Ssrust 
771929Ssrust 		/* we lost one memory region resource */
77212574SWilliam.Taylor@Oracle.COM 		atomic_dec_32(&hca_hdl->ha_mr_cnt);
773929Ssrust 
774929Ssrust 	}
775929Ssrust 	return (status);
776929Ssrust }
777929Ssrust 
778929Ssrust 
779929Ssrust /*
780929Ssrust  * Fast Memory Registration (FMR).
781929Ssrust  *
782929Ssrust  * ibt_create_fmr_pool
783929Ssrust  *      Not fast-path.
784929Ssrust  *      ibt_create_fmr_pool() verifies that the HCA supports FMR and allocates
785929Ssrust  *      and initializes an "FMR pool".  This pool contains state specific to
786929Ssrust  *      this registration, including the watermark setting to determine when
787929Ssrust  *      to sync, and the total number of FMR regions available within this pool.
788929Ssrust  *
789929Ssrust  */
790929Ssrust ibt_status_t
ibt_create_fmr_pool(ibt_hca_hdl_t hca_hdl,ibt_pd_hdl_t pd,ibt_fmr_pool_attr_t * fmr_params,ibt_fmr_pool_hdl_t * fmr_pool_p)791929Ssrust ibt_create_fmr_pool(ibt_hca_hdl_t hca_hdl, ibt_pd_hdl_t pd,
792929Ssrust     ibt_fmr_pool_attr_t *fmr_params, ibt_fmr_pool_hdl_t *fmr_pool_p)
793929Ssrust {
794929Ssrust 	ibt_status_t 		status;
795929Ssrust 
796929Ssrust 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_create_fmr_pool(%p, %p, %p)",
797929Ssrust 	    hca_hdl, pd, fmr_params);
798929Ssrust 
799929Ssrust 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_create_fmr_pool(
800929Ssrust 	    IBTL_HCA2CIHCA(hca_hdl), pd, fmr_params, fmr_pool_p);
801929Ssrust 	if (status != IBT_SUCCESS) {
802929Ssrust 		*fmr_pool_p = NULL;
803929Ssrust 		return (status);
804929Ssrust 	}
805929Ssrust 
806929Ssrust 	/* Update the FMR resource count */
80712574SWilliam.Taylor@Oracle.COM 	atomic_inc_32(&hca_hdl->ha_fmr_pool_cnt);
808929Ssrust 
809929Ssrust 	return (status);
810929Ssrust }
811929Ssrust 
812929Ssrust 
813929Ssrust /*
814929Ssrust  * ibt_destroy_fmr_pool
815929Ssrust  *      ibt_destroy_fmr_pool() deallocates all of the FMR regions in a specific
816929Ssrust  *      pool.  All state and information regarding the pool are destroyed and
817929Ssrust  *      returned as free space once again.  No more use of FMR regions in this
818929Ssrust  *      pool are possible without a subsequent call to ibt_create_fmr_pool().
819929Ssrust  */
820929Ssrust ibt_status_t
ibt_destroy_fmr_pool(ibt_hca_hdl_t hca_hdl,ibt_fmr_pool_hdl_t fmr_pool)821929Ssrust ibt_destroy_fmr_pool(ibt_hca_hdl_t hca_hdl, ibt_fmr_pool_hdl_t fmr_pool)
822929Ssrust {
823929Ssrust 	ibt_status_t 	status;
824929Ssrust 
825929Ssrust 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_destroy_fmr_pool(%p, %p)",
826929Ssrust 	    hca_hdl, fmr_pool);
827929Ssrust 
828929Ssrust 	status = IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_destroy_fmr_pool(
829929Ssrust 	    IBTL_HCA2CIHCA(hca_hdl), fmr_pool);
830929Ssrust 	if (status != IBT_SUCCESS) {
831929Ssrust 		IBTF_DPRINTF_L2(ibtl_mem, "ibt_destroy_fmr_pool: "
832929Ssrust 		    "CI FMR Pool destroy failed (%d)", status);
833929Ssrust 		return (status);
834929Ssrust 	}
835929Ssrust 
83612574SWilliam.Taylor@Oracle.COM 	atomic_dec_32(&hca_hdl->ha_fmr_pool_cnt);
837929Ssrust 
838929Ssrust 	return (status);
839929Ssrust }
840929Ssrust 
841929Ssrust /*
842929Ssrust  * ibt_flush_fmr_pool
843929Ssrust  *      ibt_flush_fmr_pool forces a flush to occur.  At the client's request,
844929Ssrust  *      any unmapped FMR regions (See 'ibt_deregister_mr())') are returned to
845929Ssrust  *      a free state.  This function allows for an asynchronous cleanup of
846929Ssrust  *      formerly used FMR regions.  Sync operation is also performed internally
847929Ssrust  *      by HCA driver, when 'watermark' settings for the number of free FMR
848929Ssrust  *      regions left in the "pool" is reached.
849929Ssrust  */
850929Ssrust ibt_status_t
ibt_flush_fmr_pool(ibt_hca_hdl_t hca_hdl,ibt_fmr_pool_hdl_t fmr_pool)851929Ssrust ibt_flush_fmr_pool(ibt_hca_hdl_t hca_hdl, ibt_fmr_pool_hdl_t fmr_pool)
852929Ssrust {
853929Ssrust 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_flush_fmr_pool(%p, %p)",
854929Ssrust 	    hca_hdl, fmr_pool);
855929Ssrust 
856929Ssrust 	return (IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_flush_fmr_pool(
857929Ssrust 	    IBTL_HCA2CIHCA(hca_hdl), fmr_pool));
858929Ssrust }
859929Ssrust 
860929Ssrust /*
861929Ssrust  * ibt_register_physical_fmr
862929Ssrust  *      ibt_register_physical_fmr() assigns a "free" entry from the FMR Pool.
863929Ssrust  *      It first consults the "FMR cache" to see if this is a duplicate memory
864929Ssrust  *      registration to something already in use.  If not, then a free entry
865929Ssrust  *      in the "pool" is marked used.
866929Ssrust  */
867929Ssrust ibt_status_t
ibt_register_physical_fmr(ibt_hca_hdl_t hca_hdl,ibt_fmr_pool_hdl_t fmr_pool,ibt_pmr_attr_t * mem_pattr,ibt_mr_hdl_t * mr_hdl_p,ibt_pmr_desc_t * mem_desc_p)868929Ssrust ibt_register_physical_fmr(ibt_hca_hdl_t hca_hdl, ibt_fmr_pool_hdl_t fmr_pool,
869929Ssrust     ibt_pmr_attr_t *mem_pattr, ibt_mr_hdl_t *mr_hdl_p,
870929Ssrust     ibt_pmr_desc_t *mem_desc_p)
871929Ssrust {
872929Ssrust 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_register_physical_fmr(%p, %p, %p, %p)",
873929Ssrust 	    hca_hdl, fmr_pool, mem_pattr, mem_desc_p);
874929Ssrust 
875929Ssrust 	return (IBTL_HCA2CIHCAOPS_P(hca_hdl)->ibc_register_physical_fmr(
876929Ssrust 	    IBTL_HCA2CIHCA(hca_hdl), fmr_pool, mem_pattr,
877929Ssrust 	    NULL, /* IBTL_HCA2MODI_P(hca_hdl)->mi_reserved */
8780Sstevel@tonic-gate 	    mr_hdl_p, mem_desc_p));
8790Sstevel@tonic-gate }
880929Ssrust 
881929Ssrust /*
882929Ssrust  * ibt_deregister_fmr
883929Ssrust  *	The ibt_deregister_fmr un-maps the resources reserved from the FMR
884929Ssrust  *	pool by ibt_register_physical_fmr().   The ibt_deregister_fmr() will
885929Ssrust  *	mark the region as free in the FMR Pool.
886929Ssrust  */
887929Ssrust ibt_status_t
ibt_deregister_fmr(ibt_hca_hdl_t hca,ibt_mr_hdl_t mr_hdl)888929Ssrust ibt_deregister_fmr(ibt_hca_hdl_t hca, ibt_mr_hdl_t mr_hdl)
889929Ssrust {
890929Ssrust 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_deregister_fmr(%p, %p)", hca, mr_hdl);
891929Ssrust 
892929Ssrust 	return (IBTL_HCA2CIHCAOPS_P(hca)->ibc_deregister_fmr(
893929Ssrust 	    IBTL_HCA2CIHCA(hca), mr_hdl));
894929Ssrust }
895*12965SWilliam.Taylor@Oracle.COM 
896*12965SWilliam.Taylor@Oracle.COM /*
897*12965SWilliam.Taylor@Oracle.COM  * ibt_register_dma_mr
898*12965SWilliam.Taylor@Oracle.COM  */
899*12965SWilliam.Taylor@Oracle.COM ibt_status_t
ibt_register_dma_mr(ibt_hca_hdl_t hca,ibt_pd_hdl_t pd,ibt_dmr_attr_t * mem_attr,ibt_mr_hdl_t * mr_hdl_p,ibt_mr_desc_t * mem_desc)900*12965SWilliam.Taylor@Oracle.COM ibt_register_dma_mr(ibt_hca_hdl_t hca, ibt_pd_hdl_t pd,
901*12965SWilliam.Taylor@Oracle.COM     ibt_dmr_attr_t *mem_attr, ibt_mr_hdl_t *mr_hdl_p, ibt_mr_desc_t *mem_desc)
902*12965SWilliam.Taylor@Oracle.COM {
903*12965SWilliam.Taylor@Oracle.COM 	ibt_status_t 	status;
904*12965SWilliam.Taylor@Oracle.COM 
905*12965SWilliam.Taylor@Oracle.COM 	IBTF_DPRINTF_L3(ibtl_mem, "ibt_register_dma_mr(%p, %p, %p)",
906*12965SWilliam.Taylor@Oracle.COM 	    hca, pd, mem_attr);
907*12965SWilliam.Taylor@Oracle.COM 
908*12965SWilliam.Taylor@Oracle.COM 	status = IBTL_HCA2CIHCAOPS_P(hca)->ibc_register_dma_mr(
909*12965SWilliam.Taylor@Oracle.COM 	    IBTL_HCA2CIHCA(hca), pd, mem_attr, NULL, mr_hdl_p, mem_desc);
910*12965SWilliam.Taylor@Oracle.COM 	if (status == IBT_SUCCESS) {
911*12965SWilliam.Taylor@Oracle.COM 		atomic_inc_32(&hca->ha_mr_cnt);
912*12965SWilliam.Taylor@Oracle.COM 	}
913*12965SWilliam.Taylor@Oracle.COM 	return (status);
914*12965SWilliam.Taylor@Oracle.COM }
915