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 (c) 2002-2003, Network Appliance, Inc. All rights reserved.
24 */
25
26/*
27 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28 * Use is subject to license terms.
29 */
30
31/*
32 *
33 * MODULE: dapl_psp_create.c
34 *
35 * PURPOSE: Connection management
36 * Description: Interfaces in this file are completely described in
37 *		the DAPL 1.1 API, Chapter 6, section 4
38 *
39 * $Id: dapl_psp_create_any.c,v 1.4 2003/06/23 12:28:05 sjs2 Exp $
40 */
41
42#include "dapl.h"
43#include "dapl_sp_util.h"
44#include "dapl_ia_util.h"
45#include "dapl_adapter_util.h"
46
47/*
48 * dapl_psp_create_any
49 *
50 * uDAPL: User Direct Access Program Library Version 1.1, 6.4.3.3
51 *
52 * Create a persistent Public Service Point that can recieve multiple
53 * requests for connections and generate multiple connection request
54 * instances that wil be delivered to the specified Event Dispatcher
55 * in a notification event. Differs from dapl_psp_create() in that
56 * the conn_qual is selected by the implementation and returned to
57 * the user.
58 *
59 * Input:
60 * 	ia_handle
61 * 	evd_handle
62 * 	psp_flags
63 *
64 * Output:
65 * 	conn_qual
66 * 	psp_handle
67 *
68 * Returns:
69 * 	DAT_SUCCESS
70 * 	DAT_INSUFFICIENT_RESOURCES
71 * 	DAT_INVALID_HANDLE
72 * 	DAT_INVALID_PARAMETER
73 * 	DAT_CONN_QUAL_IN_USE
74 * 	DAT_MODEL_NOT_SUPPORTED
75 */
76DAT_RETURN
77dapl_psp_create_any(
78	IN DAT_IA_HANDLE ia_handle,
79	OUT DAT_CONN_QUAL *conn_qual,
80	IN DAT_EVD_HANDLE evd_handle,
81	IN DAT_PSP_FLAGS psp_flags,
82	OUT DAT_PSP_HANDLE *psp_handle)
83{
84	DAPL_IA	*ia_ptr;
85	DAPL_SP	*sp_ptr;
86	DAPL_EVD *evd_ptr;
87	DAT_RETURN dat_status;
88
89	ia_ptr = (DAPL_IA *)ia_handle;
90	dat_status = DAT_SUCCESS;
91
92	if (DAPL_BAD_HANDLE(ia_ptr, DAPL_MAGIC_IA)) {
93		dat_status = DAT_ERROR(DAT_INVALID_HANDLE,
94		    DAT_INVALID_HANDLE_IA);
95		goto bail;
96	}
97
98	if (DAPL_BAD_HANDLE(evd_handle, DAPL_MAGIC_EVD)) {
99		dat_status = DAT_ERROR(DAT_INVALID_HANDLE,
100		    DAT_INVALID_HANDLE_EVD_CR);
101		goto bail;
102	}
103
104	if (psp_handle == NULL) {
105		dat_status = DAT_ERROR(DAT_INVALID_PARAMETER, DAT_INVALID_ARG5);
106		goto bail;
107	}
108
109	if (conn_qual == NULL) {
110		dat_status = DAT_ERROR(DAT_INVALID_PARAMETER, DAT_INVALID_ARG2);
111		goto bail;
112	}
113
114	/* check for invalid psp flags */
115	if ((psp_flags != DAT_PSP_CONSUMER_FLAG) &&
116	    (psp_flags != DAT_PSP_PROVIDER_FLAG)) {
117		dat_status = DAT_ERROR(DAT_INVALID_PARAMETER, DAT_INVALID_ARG4);
118		goto bail;
119	}
120
121	evd_ptr = (DAPL_EVD *)evd_handle;
122	if (!(evd_ptr->evd_flags & DAT_EVD_CR_FLAG)) {
123		dat_status = DAT_ERROR(DAT_INVALID_HANDLE,
124		    DAT_INVALID_HANDLE_EVD_CR);
125		goto bail;
126	}
127
128	/* Allocate PSP */
129	sp_ptr = dapls_sp_alloc(ia_ptr, DAT_TRUE);
130	if (sp_ptr == NULL) {
131		dat_status = DAT_ERROR(DAT_INSUFFICIENT_RESOURCES,
132		    DAT_RESOURCE_MEMORY);
133		goto bail;
134	}
135
136	/*
137	 * Fill out the args for a PSP
138	 */
139	sp_ptr->ia_handle  = ia_handle;
140	sp_ptr->evd_handle = evd_handle;
141	sp_ptr->psp_flags  = psp_flags;
142	sp_ptr->ep_handle  = NULL;
143
144	/*
145	 * Take a reference on the EVD handle
146	 */
147	(void) dapl_os_atomic_inc(&((DAPL_EVD *)evd_handle)->evd_ref_count);
148
149	/* Link it onto the IA */
150	dapl_ia_link_psp(ia_ptr, sp_ptr);
151
152	/*
153	 * Set up a listener for a connection. Connections can arrive
154	 * even before this call returns!
155	 */
156	sp_ptr->state = DAPL_SP_STATE_PSP_LISTENING;
157	sp_ptr->listening = DAT_TRUE;
158
159	dat_status = dapls_ib_setup_conn_listener(ia_ptr, 0, sp_ptr);
160
161	if (dat_status != DAT_SUCCESS) {
162	/*
163	 * Have a problem setting up the connection, something wrong!
164	 */
165		dapl_os_atomic_dec(&((DAPL_EVD *)evd_handle)->evd_ref_count);
166		sp_ptr->state = DAPL_SP_STATE_FREE;
167		sp_ptr->listening = DAT_FALSE;
168		(void) dapl_psp_free((DAT_PSP_HANDLE)sp_ptr);
169
170		dapl_dbg_log(DAPL_DBG_TYPE_ERR,
171		    "--> dapl_psp_create cannot set up conn listener: %x\n",
172		    dat_status);
173
174		goto bail;
175	}
176
177	/*
178	 * Return handle to the user
179	 */
180	*conn_qual  = sp_ptr->conn_qual;
181	*psp_handle = (DAT_PSP_HANDLE)sp_ptr;
182
183bail:
184	return (dat_status);
185}
186