xref: /onnv-gate/usr/src/lib/libsun_ima/common/ima.c (revision 7836:4e95154b5b7a)
1*7836SJohn.Forte@Sun.COM /*
2*7836SJohn.Forte@Sun.COM  * CDDL HEADER START
3*7836SJohn.Forte@Sun.COM  *
4*7836SJohn.Forte@Sun.COM  * The contents of this file are subject to the terms of the
5*7836SJohn.Forte@Sun.COM  * Common Development and Distribution License (the "License").
6*7836SJohn.Forte@Sun.COM  * You may not use this file except in compliance with the License.
7*7836SJohn.Forte@Sun.COM  *
8*7836SJohn.Forte@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*7836SJohn.Forte@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*7836SJohn.Forte@Sun.COM  * See the License for the specific language governing permissions
11*7836SJohn.Forte@Sun.COM  * and limitations under the License.
12*7836SJohn.Forte@Sun.COM  *
13*7836SJohn.Forte@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*7836SJohn.Forte@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*7836SJohn.Forte@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*7836SJohn.Forte@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*7836SJohn.Forte@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*7836SJohn.Forte@Sun.COM  *
19*7836SJohn.Forte@Sun.COM  * CDDL HEADER END
20*7836SJohn.Forte@Sun.COM  */
21*7836SJohn.Forte@Sun.COM /*
22*7836SJohn.Forte@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*7836SJohn.Forte@Sun.COM  * Use is subject to license terms.
24*7836SJohn.Forte@Sun.COM  */
25*7836SJohn.Forte@Sun.COM 
26*7836SJohn.Forte@Sun.COM #include <arpa/inet.h>
27*7836SJohn.Forte@Sun.COM #include <sys/socket.h>
28*7836SJohn.Forte@Sun.COM #include <sys/types.h>
29*7836SJohn.Forte@Sun.COM #include <stdarg.h>
30*7836SJohn.Forte@Sun.COM #include <stdlib.h>
31*7836SJohn.Forte@Sun.COM #include <string.h>
32*7836SJohn.Forte@Sun.COM #include <strings.h>
33*7836SJohn.Forte@Sun.COM #include <unistd.h>
34*7836SJohn.Forte@Sun.COM #include <syslog.h>
35*7836SJohn.Forte@Sun.COM #include <errno.h>
36*7836SJohn.Forte@Sun.COM #include <wchar.h>
37*7836SJohn.Forte@Sun.COM #include <widec.h>
38*7836SJohn.Forte@Sun.COM #include <libsysevent.h>
39*7836SJohn.Forte@Sun.COM #include <sys/nvpair.h>
40*7836SJohn.Forte@Sun.COM #include <fcntl.h>
41*7836SJohn.Forte@Sun.COM #include <stdio.h>
42*7836SJohn.Forte@Sun.COM #include <time.h>
43*7836SJohn.Forte@Sun.COM #include <libdevinfo.h>
44*7836SJohn.Forte@Sun.COM #include <sys/scsi/adapters/iscsi_if.h>
45*7836SJohn.Forte@Sun.COM #include <sys/scsi/adapters/iscsi_protocol.h>
46*7836SJohn.Forte@Sun.COM #include <ima.h>
47*7836SJohn.Forte@Sun.COM 
48*7836SJohn.Forte@Sun.COM #define	LIBRARY_PROPERTY_IMPLEMENTATION_VERSION	L"1.0.0"
49*7836SJohn.Forte@Sun.COM #define	LIBRARY_PROPERTY_VENDOR			L"Sun Microsystems, Inc."
50*7836SJohn.Forte@Sun.COM #define	OS_DEVICE_NAME 				"/devices/iscsi"
51*7836SJohn.Forte@Sun.COM #define	LIBRARY_FILE_NAME			L"libsun_ima.so"
52*7836SJohn.Forte@Sun.COM 
53*7836SJohn.Forte@Sun.COM #define	OS_DEVICE_NAME_LEN		256
54*7836SJohn.Forte@Sun.COM #define	INQUIRY_CMD			0x12
55*7836SJohn.Forte@Sun.COM #define	GETCAPACITY_CMD  		0x25
56*7836SJohn.Forte@Sun.COM #define	INQUIRY_CMDLEN			6
57*7836SJohn.Forte@Sun.COM #define	INQUIRY_REPLY_LEN		96
58*7836SJohn.Forte@Sun.COM #define	USCSI_TIMEOUT_IN_SEC		10
59*7836SJohn.Forte@Sun.COM #define	MAX_AUTHMETHODS			10
60*7836SJohn.Forte@Sun.COM #define	NUM_SUPPORTED_AUTH_METHODS	2
61*7836SJohn.Forte@Sun.COM #define	SUN_IMA_MAX_DIGEST_ALGORITHMS	2	/* NONE and CRC 32 */
62*7836SJohn.Forte@Sun.COM #define	SUN_IMA_IP_ADDRESS_LEN		256
63*7836SJohn.Forte@Sun.COM #define	SUN_IMA_IP_PORT_LEN		64
64*7836SJohn.Forte@Sun.COM #define	SUN_IMA_MAX_RADIUS_SECRET_LEN	128
65*7836SJohn.Forte@Sun.COM 
66*7836SJohn.Forte@Sun.COM /* Forward declaration */
67*7836SJohn.Forte@Sun.COM #define	BOOL_PARAM			1
68*7836SJohn.Forte@Sun.COM #define	MIN_MAX_PARAM			2
69*7836SJohn.Forte@Sun.COM 
70*7836SJohn.Forte@Sun.COM /* OK */
71*7836SJohn.Forte@Sun.COM #define	DISC_ADDR_OK			0
72*7836SJohn.Forte@Sun.COM /* Incorrect IP address */
73*7836SJohn.Forte@Sun.COM #define	DISC_ADDR_INTEGRITY_ERROR   	1
74*7836SJohn.Forte@Sun.COM /* Error converting text IP address to numeric binary form */
75*7836SJohn.Forte@Sun.COM #define	DISC_ADDR_IP_CONV_ERROR		2
76*7836SJohn.Forte@Sun.COM 
77*7836SJohn.Forte@Sun.COM /* Currently not defined in  IMA_TARGET_DISCOVERY_METHOD enum */
78*7836SJohn.Forte@Sun.COM #define	IMA_TARGET_DISCOVERY_METHOD_UNKNOWN  0
79*7836SJohn.Forte@Sun.COM 
80*7836SJohn.Forte@Sun.COM static IMA_OID		lhbaObjectId;
81*7836SJohn.Forte@Sun.COM static IMA_UINT32	pluginOwnerId;
82*7836SJohn.Forte@Sun.COM static sysevent_handle_t *shp;
83*7836SJohn.Forte@Sun.COM 
84*7836SJohn.Forte@Sun.COM 
85*7836SJohn.Forte@Sun.COM 
86*7836SJohn.Forte@Sun.COM /*
87*7836SJohn.Forte@Sun.COM  * Custom struct to allow tgpt to be specified.
88*7836SJohn.Forte@Sun.COM  */
89*7836SJohn.Forte@Sun.COM typedef struct _SUN_IMA_DISC_ADDRESS_KEY
90*7836SJohn.Forte@Sun.COM {
91*7836SJohn.Forte@Sun.COM 	IMA_NODE_NAME name;
92*7836SJohn.Forte@Sun.COM 	IMA_ADDRESS_KEY	address;
93*7836SJohn.Forte@Sun.COM 	IMA_UINT16 tpgt;
94*7836SJohn.Forte@Sun.COM } SUN_IMA_DISC_ADDRESS_KEY;
95*7836SJohn.Forte@Sun.COM 
96*7836SJohn.Forte@Sun.COM /*
97*7836SJohn.Forte@Sun.COM  * Custom struct to allow tgpt to be specified.
98*7836SJohn.Forte@Sun.COM  */
99*7836SJohn.Forte@Sun.COM typedef struct _SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES
100*7836SJohn.Forte@Sun.COM {
101*7836SJohn.Forte@Sun.COM 	IMA_UINT keyCount;
102*7836SJohn.Forte@Sun.COM 	SUN_IMA_DISC_ADDRESS_KEY keys[1];
103*7836SJohn.Forte@Sun.COM } SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES;
104*7836SJohn.Forte@Sun.COM 
105*7836SJohn.Forte@Sun.COM /*
106*7836SJohn.Forte@Sun.COM  * Custom struct to allow tgpt to be specified.
107*7836SJohn.Forte@Sun.COM  */
108*7836SJohn.Forte@Sun.COM typedef struct _SUN_IMA_DISC_ADDR_PROP_LIST
109*7836SJohn.Forte@Sun.COM {
110*7836SJohn.Forte@Sun.COM 	IMA_UINT discAddrCount;
111*7836SJohn.Forte@Sun.COM 	IMA_DISCOVERY_ADDRESS_PROPERTIES props[1];
112*7836SJohn.Forte@Sun.COM } SUN_IMA_DISC_ADDR_PROP_LIST;
113*7836SJohn.Forte@Sun.COM 
114*7836SJohn.Forte@Sun.COM 
115*7836SJohn.Forte@Sun.COM static IMA_OBJECT_VISIBILITY_FN pObjectVisibilityCallback = NULL;
116*7836SJohn.Forte@Sun.COM static IMA_OBJECT_PROPERTY_FN pObjectPropertyCallback = NULL;
117*7836SJohn.Forte@Sun.COM 
118*7836SJohn.Forte@Sun.COM static IMA_STATUS getISCSINodeParameter(int paramType, IMA_OID *oid,
119*7836SJohn.Forte@Sun.COM     void *pProps, uint32_t paramIndex);
120*7836SJohn.Forte@Sun.COM static IMA_STATUS setISCSINodeParameter(int paramType, IMA_OID *oid,
121*7836SJohn.Forte@Sun.COM     void *pProps, uint32_t paramIndex);
122*7836SJohn.Forte@Sun.COM static IMA_STATUS setAuthMethods(IMA_OID oid, IMA_UINT *pMethodCount,
123*7836SJohn.Forte@Sun.COM     const IMA_AUTHMETHOD *pMethodList);
124*7836SJohn.Forte@Sun.COM static IMA_STATUS getAuthMethods(IMA_OID oid, IMA_UINT *pMethodCount,
125*7836SJohn.Forte@Sun.COM     IMA_AUTHMETHOD *pMethodList);
126*7836SJohn.Forte@Sun.COM 
127*7836SJohn.Forte@Sun.COM static int prepare_discovery_entry(IMA_TARGET_ADDRESS discoveryAddress,
128*7836SJohn.Forte@Sun.COM     entry_t *entry);
129*7836SJohn.Forte@Sun.COM static IMA_STATUS configure_discovery_method(IMA_BOOL enable,
130*7836SJohn.Forte@Sun.COM     iSCSIDiscoveryMethod_t method);
131*7836SJohn.Forte@Sun.COM static IMA_STATUS get_target_oid_list(uint32_t targetListType,
132*7836SJohn.Forte@Sun.COM     IMA_OID_LIST **ppList);
133*7836SJohn.Forte@Sun.COM static IMA_STATUS get_target_lun_oid_list(IMA_OID * targetOid,
134*7836SJohn.Forte@Sun.COM 		iscsi_lun_list_t  **ppLunList);
135*7836SJohn.Forte@Sun.COM static int get_lun_devlink(di_devlink_t link, void *osDeviceName);
136*7836SJohn.Forte@Sun.COM static IMA_STATUS getDiscoveryAddressPropertiesList(
137*7836SJohn.Forte@Sun.COM 	SUN_IMA_DISC_ADDR_PROP_LIST **ppList
138*7836SJohn.Forte@Sun.COM );
139*7836SJohn.Forte@Sun.COM static IMA_STATUS sendTargets(IMA_TARGET_ADDRESS address,
140*7836SJohn.Forte@Sun.COM     SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES **ppList
141*7836SJohn.Forte@Sun.COM );
142*7836SJohn.Forte@Sun.COM 
143*7836SJohn.Forte@Sun.COM static IMA_STATUS getSupportedAuthMethods(IMA_OID lhbaOid,
144*7836SJohn.Forte@Sun.COM     IMA_BOOL getSettableMethods, IMA_UINT *pMethodCount,
145*7836SJohn.Forte@Sun.COM     IMA_AUTHMETHOD *pMethodList);
146*7836SJohn.Forte@Sun.COM static IMA_STATUS getLuProperties(IMA_OID luId, IMA_LU_PROPERTIES *pProps);
147*7836SJohn.Forte@Sun.COM static IMA_STATUS getTargetProperties(IMA_OID targetId,
148*7836SJohn.Forte@Sun.COM     IMA_TARGET_PROPERTIES *pProps);
149*7836SJohn.Forte@Sun.COM 
150*7836SJohn.Forte@Sun.COM void InitLibrary();
151*7836SJohn.Forte@Sun.COM 
152*7836SJohn.Forte@Sun.COM static void libSwprintf(wchar_t *wcs, const wchar_t *lpszFormat, ...)
153*7836SJohn.Forte@Sun.COM {
154*7836SJohn.Forte@Sun.COM 	va_list args;
155*7836SJohn.Forte@Sun.COM 	va_start(args, lpszFormat);
156*7836SJohn.Forte@Sun.COM 	(void) vswprintf(wcs, OS_DEVICE_NAME_LEN - 1, lpszFormat, args);
157*7836SJohn.Forte@Sun.COM 	va_end(args);
158*7836SJohn.Forte@Sun.COM }
159*7836SJohn.Forte@Sun.COM 
160*7836SJohn.Forte@Sun.COM static void
161*7836SJohn.Forte@Sun.COM sysevent_handler(sysevent_t *ev)
162*7836SJohn.Forte@Sun.COM {
163*7836SJohn.Forte@Sun.COM 	IMA_OID tmpOid;
164*7836SJohn.Forte@Sun.COM 	IMA_BOOL becomingVisible = IMA_FALSE;
165*7836SJohn.Forte@Sun.COM 	IMA_UINT i;
166*7836SJohn.Forte@Sun.COM 
167*7836SJohn.Forte@Sun.COM 	const char *visibility_subclasses[] = {
168*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_STATIC_START,
169*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_STATIC_END,
170*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_SEND_TARGETS_START,
171*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_SEND_TARGETS_END,
172*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_SLP_START,
173*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_SLP_END,
174*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_ISNS_START,
175*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_ISNS_END,
176*7836SJohn.Forte@Sun.COM 		NULL
177*7836SJohn.Forte@Sun.COM 	};
178*7836SJohn.Forte@Sun.COM 
179*7836SJohn.Forte@Sun.COM 	tmpOid.ownerId = pluginOwnerId;
180*7836SJohn.Forte@Sun.COM 	tmpOid.objectType = IMA_OBJECT_TYPE_TARGET;
181*7836SJohn.Forte@Sun.COM 	tmpOid.objectSequenceNumber = 0;
182*7836SJohn.Forte@Sun.COM 
183*7836SJohn.Forte@Sun.COM 	/* Make sure our event class matches what we are looking for */
184*7836SJohn.Forte@Sun.COM 	if (strncmp(EC_ISCSI, sysevent_get_class_name(ev),
185*7836SJohn.Forte@Sun.COM 	    strlen(EC_ISCSI)) != 0) {
186*7836SJohn.Forte@Sun.COM 		return;
187*7836SJohn.Forte@Sun.COM 	}
188*7836SJohn.Forte@Sun.COM 
189*7836SJohn.Forte@Sun.COM 
190*7836SJohn.Forte@Sun.COM 	/* Check for object property changes */
191*7836SJohn.Forte@Sun.COM 	if ((strncmp(ESC_ISCSI_PROP_CHANGE,
192*7836SJohn.Forte@Sun.COM 	    sysevent_get_subclass_name(ev),
193*7836SJohn.Forte@Sun.COM 	    strlen(ESC_ISCSI_PROP_CHANGE)) == 0)) {
194*7836SJohn.Forte@Sun.COM 		if (pObjectPropertyCallback != NULL)
195*7836SJohn.Forte@Sun.COM 			pObjectPropertyCallback(tmpOid);
196*7836SJohn.Forte@Sun.COM 	} else {
197*7836SJohn.Forte@Sun.COM 		i = 0;
198*7836SJohn.Forte@Sun.COM 		while (visibility_subclasses[i] != NULL) {
199*7836SJohn.Forte@Sun.COM 			if ((strncmp(visibility_subclasses[i],
200*7836SJohn.Forte@Sun.COM 			    sysevent_get_subclass_name(ev),
201*7836SJohn.Forte@Sun.COM 			    strlen(visibility_subclasses[i])) == 0) &&
202*7836SJohn.Forte@Sun.COM 			    pObjectVisibilityCallback != NULL) {
203*7836SJohn.Forte@Sun.COM 				becomingVisible = IMA_TRUE;
204*7836SJohn.Forte@Sun.COM 				pObjectVisibilityCallback(becomingVisible,
205*7836SJohn.Forte@Sun.COM 				    tmpOid);
206*7836SJohn.Forte@Sun.COM 			}
207*7836SJohn.Forte@Sun.COM 			i++;
208*7836SJohn.Forte@Sun.COM 		}
209*7836SJohn.Forte@Sun.COM 	}
210*7836SJohn.Forte@Sun.COM }
211*7836SJohn.Forte@Sun.COM 
212*7836SJohn.Forte@Sun.COM IMA_STATUS init_sysevents() {
213*7836SJohn.Forte@Sun.COM 	const char *subclass_list[] = {
214*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_STATIC_START,
215*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_STATIC_END,
216*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_SEND_TARGETS_START,
217*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_SEND_TARGETS_END,
218*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_SLP_START,
219*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_SLP_END,
220*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_ISNS_START,
221*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_ISNS_END,
222*7836SJohn.Forte@Sun.COM 		ESC_ISCSI_PROP_CHANGE,
223*7836SJohn.Forte@Sun.COM 	};
224*7836SJohn.Forte@Sun.COM 
225*7836SJohn.Forte@Sun.COM 	/* Bind event handler and create subscriber handle */
226*7836SJohn.Forte@Sun.COM 	shp = sysevent_bind_handle(sysevent_handler);
227*7836SJohn.Forte@Sun.COM 	if (shp == NULL) {
228*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
229*7836SJohn.Forte@Sun.COM 	}
230*7836SJohn.Forte@Sun.COM 
231*7836SJohn.Forte@Sun.COM 	if (sysevent_subscribe_event(shp, EC_ISCSI, subclass_list, 9) != 0) {
232*7836SJohn.Forte@Sun.COM 		sysevent_unbind_handle(shp);
233*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
234*7836SJohn.Forte@Sun.COM 	}
235*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
236*7836SJohn.Forte@Sun.COM }
237*7836SJohn.Forte@Sun.COM 
238*7836SJohn.Forte@Sun.COM IMA_STATUS Initialize(IMA_UINT32 pluginOid) {
239*7836SJohn.Forte@Sun.COM 	pluginOwnerId = pluginOid;
240*7836SJohn.Forte@Sun.COM 	return (init_sysevents());
241*7836SJohn.Forte@Sun.COM }
242*7836SJohn.Forte@Sun.COM 
243*7836SJohn.Forte@Sun.COM void Terminate() {
244*7836SJohn.Forte@Sun.COM 	if (shp != NULL) {
245*7836SJohn.Forte@Sun.COM 		sysevent_unsubscribe_event(shp, EC_ISCSI);
246*7836SJohn.Forte@Sun.COM 	}
247*7836SJohn.Forte@Sun.COM 
248*7836SJohn.Forte@Sun.COM }
249*7836SJohn.Forte@Sun.COM 
250*7836SJohn.Forte@Sun.COM void InitLibrary() {
251*7836SJohn.Forte@Sun.COM }
252*7836SJohn.Forte@Sun.COM 
253*7836SJohn.Forte@Sun.COM static void GetBuildTime(IMA_DATETIME* pdatetime)
254*7836SJohn.Forte@Sun.COM {
255*7836SJohn.Forte@Sun.COM 	(void) memset(pdatetime, 0, sizeof (IMA_DATETIME));
256*7836SJohn.Forte@Sun.COM }
257*7836SJohn.Forte@Sun.COM 
258*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
259*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetNodeProperties(
260*7836SJohn.Forte@Sun.COM 	IMA_OID nodeOid,
261*7836SJohn.Forte@Sun.COM 	IMA_NODE_PROPERTIES *pProps
262*7836SJohn.Forte@Sun.COM )
263*7836SJohn.Forte@Sun.COM {
264*7836SJohn.Forte@Sun.COM 	int fd;
265*7836SJohn.Forte@Sun.COM 	iscsi_param_get_t pg;
266*7836SJohn.Forte@Sun.COM 
267*7836SJohn.Forte@Sun.COM 	pProps->runningInInitiatorMode = IMA_TRUE;
268*7836SJohn.Forte@Sun.COM 	pProps->runningInTargetMode = IMA_FALSE;
269*7836SJohn.Forte@Sun.COM 	pProps->nameAndAliasSettable = IMA_FALSE;
270*7836SJohn.Forte@Sun.COM 
271*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
272*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
273*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
274*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
275*7836SJohn.Forte@Sun.COM 	}
276*7836SJohn.Forte@Sun.COM 
277*7836SJohn.Forte@Sun.COM 	(void) memset(&pg, 0, sizeof (iscsi_param_get_t));
278*7836SJohn.Forte@Sun.COM 	pg.g_vers = ISCSI_INTERFACE_VERSION;
279*7836SJohn.Forte@Sun.COM 	pg.g_param = ISCSI_LOGIN_PARAM_INITIATOR_NAME;
280*7836SJohn.Forte@Sun.COM 
281*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_PARAM_GET, &pg) == -1) {
282*7836SJohn.Forte@Sun.COM 		pProps->nameValid = IMA_FALSE;
283*7836SJohn.Forte@Sun.COM 	} else {
284*7836SJohn.Forte@Sun.COM 		if (strlen((char *)pg.g_value.v_name) > 0) {
285*7836SJohn.Forte@Sun.COM 			(void) mbstowcs(pProps->name,
286*7836SJohn.Forte@Sun.COM 			    (char *)pg.g_value.v_name,
287*7836SJohn.Forte@Sun.COM 			    IMA_NODE_NAME_LEN);
288*7836SJohn.Forte@Sun.COM 			pProps->nameValid = IMA_TRUE;
289*7836SJohn.Forte@Sun.COM 		} else {
290*7836SJohn.Forte@Sun.COM 			pProps->nameValid = IMA_FALSE;
291*7836SJohn.Forte@Sun.COM 		}
292*7836SJohn.Forte@Sun.COM 	}
293*7836SJohn.Forte@Sun.COM 
294*7836SJohn.Forte@Sun.COM 	(void) memset(&pg, 0, sizeof (iscsi_param_get_t));
295*7836SJohn.Forte@Sun.COM 	pg.g_vers = ISCSI_INTERFACE_VERSION;
296*7836SJohn.Forte@Sun.COM 	pg.g_param = ISCSI_LOGIN_PARAM_INITIATOR_ALIAS;
297*7836SJohn.Forte@Sun.COM 	(void) memset(pProps->alias, 0,
298*7836SJohn.Forte@Sun.COM 	    sizeof (IMA_WCHAR) * IMA_NODE_ALIAS_LEN);
299*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_PARAM_GET, &pg) == -1) {
300*7836SJohn.Forte@Sun.COM 		pProps->aliasValid = IMA_FALSE;
301*7836SJohn.Forte@Sun.COM 	} else {
302*7836SJohn.Forte@Sun.COM 		if (strlen((char *)pg.g_value.v_name) > 0) {
303*7836SJohn.Forte@Sun.COM 			(void) mbstowcs(pProps->alias,
304*7836SJohn.Forte@Sun.COM 			    (char *)pg.g_value.v_name,
305*7836SJohn.Forte@Sun.COM 			    IMA_NODE_ALIAS_LEN);
306*7836SJohn.Forte@Sun.COM 			pProps->aliasValid = IMA_TRUE;
307*7836SJohn.Forte@Sun.COM 		}
308*7836SJohn.Forte@Sun.COM 	}
309*7836SJohn.Forte@Sun.COM 
310*7836SJohn.Forte@Sun.COM 	(void) close(fd);
311*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
312*7836SJohn.Forte@Sun.COM }
313*7836SJohn.Forte@Sun.COM 
314*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_SetNodeName(
315*7836SJohn.Forte@Sun.COM 	IMA_OID nodeOid,
316*7836SJohn.Forte@Sun.COM 	const IMA_NODE_NAME newName
317*7836SJohn.Forte@Sun.COM )
318*7836SJohn.Forte@Sun.COM {
319*7836SJohn.Forte@Sun.COM 	int fd;
320*7836SJohn.Forte@Sun.COM 	iscsi_param_set_t ps;
321*7836SJohn.Forte@Sun.COM 
322*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
323*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
324*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
325*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
326*7836SJohn.Forte@Sun.COM 	}
327*7836SJohn.Forte@Sun.COM 
328*7836SJohn.Forte@Sun.COM 	(void) memset(&ps, 0, sizeof (iscsi_param_set_t));
329*7836SJohn.Forte@Sun.COM 	ps.s_oid = nodeOid.objectSequenceNumber;
330*7836SJohn.Forte@Sun.COM 	ps.s_vers = ISCSI_INTERFACE_VERSION;
331*7836SJohn.Forte@Sun.COM 	ps.s_param = ISCSI_LOGIN_PARAM_INITIATOR_NAME;
332*7836SJohn.Forte@Sun.COM 	(void) wcstombs((char *)ps.s_value.v_name, newName, ISCSI_MAX_NAME_LEN);
333*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_INIT_NODE_NAME_SET, &ps)) {
334*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
335*7836SJohn.Forte@Sun.COM 		    "ISCSI_PARAM_SET ioctl failed, errno: %d", errno);
336*7836SJohn.Forte@Sun.COM 		(void) close(fd);
337*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
338*7836SJohn.Forte@Sun.COM 	}
339*7836SJohn.Forte@Sun.COM 
340*7836SJohn.Forte@Sun.COM 	(void) close(fd);
341*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
342*7836SJohn.Forte@Sun.COM }
343*7836SJohn.Forte@Sun.COM 
344*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_SetNodeAlias(
345*7836SJohn.Forte@Sun.COM 	IMA_OID nodeOid,
346*7836SJohn.Forte@Sun.COM 	const IMA_NODE_ALIAS newAlias
347*7836SJohn.Forte@Sun.COM )
348*7836SJohn.Forte@Sun.COM {
349*7836SJohn.Forte@Sun.COM 	int fd;
350*7836SJohn.Forte@Sun.COM 	iscsi_param_set_t ps;
351*7836SJohn.Forte@Sun.COM 
352*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
353*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
354*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
355*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
356*7836SJohn.Forte@Sun.COM 	}
357*7836SJohn.Forte@Sun.COM 
358*7836SJohn.Forte@Sun.COM 	(void) memset(&ps, 0, sizeof (iscsi_param_set_t));
359*7836SJohn.Forte@Sun.COM 	ps.s_oid = nodeOid.objectSequenceNumber;
360*7836SJohn.Forte@Sun.COM 	ps.s_vers = ISCSI_INTERFACE_VERSION;
361*7836SJohn.Forte@Sun.COM 	ps.s_param = ISCSI_LOGIN_PARAM_INITIATOR_ALIAS;
362*7836SJohn.Forte@Sun.COM 
363*7836SJohn.Forte@Sun.COM 	/* newAlias = NULL specifies that the alias should be deleted. */
364*7836SJohn.Forte@Sun.COM 	if (newAlias != NULL)
365*7836SJohn.Forte@Sun.COM 		(void) wcstombs((char *)ps.s_value.v_name, newAlias,
366*7836SJohn.Forte@Sun.COM 		    ISCSI_MAX_NAME_LEN);
367*7836SJohn.Forte@Sun.COM 	else
368*7836SJohn.Forte@Sun.COM 		(void) wcstombs((char *)ps.s_value.v_name,
369*7836SJohn.Forte@Sun.COM 		    L"", ISCSI_MAX_NAME_LEN);
370*7836SJohn.Forte@Sun.COM 
371*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_PARAM_SET, &ps)) {
372*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
373*7836SJohn.Forte@Sun.COM 		    "ISCSI_PARAM_SET ioctl failed, errno: %d", errno);
374*7836SJohn.Forte@Sun.COM 		(void) close(fd);
375*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
376*7836SJohn.Forte@Sun.COM 	}
377*7836SJohn.Forte@Sun.COM 
378*7836SJohn.Forte@Sun.COM 	(void) close(fd);
379*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
380*7836SJohn.Forte@Sun.COM }
381*7836SJohn.Forte@Sun.COM 
382*7836SJohn.Forte@Sun.COM 
383*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetLhbaOidList(
384*7836SJohn.Forte@Sun.COM 	IMA_OID_LIST **ppList
385*7836SJohn.Forte@Sun.COM )
386*7836SJohn.Forte@Sun.COM {
387*7836SJohn.Forte@Sun.COM 	/* Always return the same object ID for the lhba */
388*7836SJohn.Forte@Sun.COM 	lhbaObjectId.objectType = IMA_OBJECT_TYPE_LHBA;
389*7836SJohn.Forte@Sun.COM 	lhbaObjectId.ownerId = pluginOwnerId;
390*7836SJohn.Forte@Sun.COM 	lhbaObjectId.objectSequenceNumber = ISCSI_INITIATOR_OID;
391*7836SJohn.Forte@Sun.COM 
392*7836SJohn.Forte@Sun.COM 	*ppList = (IMA_OID_LIST*)calloc(1, sizeof (IMA_OID_LIST));
393*7836SJohn.Forte@Sun.COM 	if (*ppList == NULL) {
394*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
395*7836SJohn.Forte@Sun.COM 	}
396*7836SJohn.Forte@Sun.COM 
397*7836SJohn.Forte@Sun.COM 	(*ppList)->oidCount = 1;
398*7836SJohn.Forte@Sun.COM 	(void) memcpy(&(*ppList)->oids[0],
399*7836SJohn.Forte@Sun.COM 	    &lhbaObjectId, sizeof (lhbaObjectId));
400*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
401*7836SJohn.Forte@Sun.COM }
402*7836SJohn.Forte@Sun.COM 
403*7836SJohn.Forte@Sun.COM 
404*7836SJohn.Forte@Sun.COM /*
405*7836SJohn.Forte@Sun.COM  * Get the discovery properties of the LHBA
406*7836SJohn.Forte@Sun.COM  */
407*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
408*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetDiscoveryProperties(
409*7836SJohn.Forte@Sun.COM 	IMA_OID oid,
410*7836SJohn.Forte@Sun.COM 	IMA_DISCOVERY_PROPERTIES *pProps
411*7836SJohn.Forte@Sun.COM )
412*7836SJohn.Forte@Sun.COM {
413*7836SJohn.Forte@Sun.COM 	int fd;
414*7836SJohn.Forte@Sun.COM 	iSCSIDiscoveryProperties_t discoveryProps;
415*7836SJohn.Forte@Sun.COM 
416*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
417*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
418*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
419*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
420*7836SJohn.Forte@Sun.COM 	}
421*7836SJohn.Forte@Sun.COM 
422*7836SJohn.Forte@Sun.COM 	(void) memset(&discoveryProps, 0, sizeof (discoveryProps));
423*7836SJohn.Forte@Sun.COM 	discoveryProps.vers = ISCSI_INTERFACE_VERSION;
424*7836SJohn.Forte@Sun.COM 
425*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_DISCOVERY_PROPS, &discoveryProps) != 0) {
426*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
427*7836SJohn.Forte@Sun.COM 		    "ISCSI_DISCOVERY_PROPS ioctl failed, errno: %d", errno);
428*7836SJohn.Forte@Sun.COM 		(void) close(fd);
429*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
430*7836SJohn.Forte@Sun.COM 	}
431*7836SJohn.Forte@Sun.COM 
432*7836SJohn.Forte@Sun.COM 	pProps->iSnsDiscoverySettable = discoveryProps.iSNSDiscoverySettable;
433*7836SJohn.Forte@Sun.COM 	pProps->iSnsDiscoveryEnabled = discoveryProps.iSNSDiscoveryEnabled;
434*7836SJohn.Forte@Sun.COM 	/*
435*7836SJohn.Forte@Sun.COM 	 * Set the iSNS discovery method - The IMA specification indicates
436*7836SJohn.Forte@Sun.COM 	 * this field is valid only if iSNS discovery is enabled.
437*7836SJohn.Forte@Sun.COM 	 */
438*7836SJohn.Forte@Sun.COM 	if (pProps->iSnsDiscoveryEnabled == IMA_TRUE) {
439*7836SJohn.Forte@Sun.COM 		switch (discoveryProps.iSNSDiscoveryMethod) {
440*7836SJohn.Forte@Sun.COM 			case iSNSDiscoveryMethodStatic:
441*7836SJohn.Forte@Sun.COM 				pProps->iSnsDiscoveryMethod =
442*7836SJohn.Forte@Sun.COM 				    IMA_ISNS_DISCOVERY_METHOD_STATIC;
443*7836SJohn.Forte@Sun.COM 				break;
444*7836SJohn.Forte@Sun.COM 			case iSNSDiscoveryMethodDHCP:
445*7836SJohn.Forte@Sun.COM 				pProps->iSnsDiscoveryMethod =
446*7836SJohn.Forte@Sun.COM 				    IMA_ISNS_DISCOVERY_METHOD_DHCP;
447*7836SJohn.Forte@Sun.COM 				break;
448*7836SJohn.Forte@Sun.COM 			case iSNSDiscoveryMethodSLP:
449*7836SJohn.Forte@Sun.COM 				pProps->iSnsDiscoveryMethod =
450*7836SJohn.Forte@Sun.COM 				    IMA_ISNS_DISCOVERY_METHOD_SLP;
451*7836SJohn.Forte@Sun.COM 				break;
452*7836SJohn.Forte@Sun.COM 			default:
453*7836SJohn.Forte@Sun.COM 				(void) close(fd);
454*7836SJohn.Forte@Sun.COM 				return (IMA_ERROR_UNEXPECTED_OS_ERROR);
455*7836SJohn.Forte@Sun.COM 		}
456*7836SJohn.Forte@Sun.COM 	}
457*7836SJohn.Forte@Sun.COM 	(void) memcpy(pProps->iSnsHost.id.hostname,
458*7836SJohn.Forte@Sun.COM 	    discoveryProps.iSNSDomainName,
459*7836SJohn.Forte@Sun.COM 	    sizeof (pProps->iSnsHost.id.hostname));
460*7836SJohn.Forte@Sun.COM 	pProps->slpDiscoverySettable = discoveryProps.SLPDiscoverySettable;
461*7836SJohn.Forte@Sun.COM 	pProps->slpDiscoveryEnabled = discoveryProps.SLPDiscoveryEnabled;
462*7836SJohn.Forte@Sun.COM 	pProps->staticDiscoverySettable =
463*7836SJohn.Forte@Sun.COM 	    discoveryProps.StaticDiscoverySettable;
464*7836SJohn.Forte@Sun.COM 	pProps->staticDiscoveryEnabled = discoveryProps.StaticDiscoveryEnabled;
465*7836SJohn.Forte@Sun.COM 	pProps->sendTargetsDiscoverySettable =
466*7836SJohn.Forte@Sun.COM 	    discoveryProps.SendTargetsDiscoverySettable;
467*7836SJohn.Forte@Sun.COM 	pProps->sendTargetsDiscoveryEnabled =
468*7836SJohn.Forte@Sun.COM 	    discoveryProps.SendTargetsDiscoveryEnabled;
469*7836SJohn.Forte@Sun.COM 
470*7836SJohn.Forte@Sun.COM 	(void) close(fd);
471*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
472*7836SJohn.Forte@Sun.COM }
473*7836SJohn.Forte@Sun.COM 
474*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_FreeMemory(
475*7836SJohn.Forte@Sun.COM 	void *pMemory
476*7836SJohn.Forte@Sun.COM )
477*7836SJohn.Forte@Sun.COM {
478*7836SJohn.Forte@Sun.COM 	if (pMemory != NULL)
479*7836SJohn.Forte@Sun.COM 		free(pMemory);
480*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
481*7836SJohn.Forte@Sun.COM }
482*7836SJohn.Forte@Sun.COM 
483*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetNonSharedNodeOidList(
484*7836SJohn.Forte@Sun.COM 		IMA_OID_LIST **ppList
485*7836SJohn.Forte@Sun.COM )
486*7836SJohn.Forte@Sun.COM {
487*7836SJohn.Forte@Sun.COM 	if (ppList == NULL)
488*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INVALID_PARAMETER);
489*7836SJohn.Forte@Sun.COM 
490*7836SJohn.Forte@Sun.COM 	*ppList = (IMA_OID_LIST*) calloc(1, sizeof (IMA_OID_LIST));
491*7836SJohn.Forte@Sun.COM 	if (*ppList == NULL) {
492*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
493*7836SJohn.Forte@Sun.COM 	}
494*7836SJohn.Forte@Sun.COM 	(*ppList)->oidCount = 0;
495*7836SJohn.Forte@Sun.COM 
496*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
497*7836SJohn.Forte@Sun.COM }
498*7836SJohn.Forte@Sun.COM 
499*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetFirstBurstLengthProperties(
500*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
501*7836SJohn.Forte@Sun.COM 		IMA_MIN_MAX_VALUE *pProps
502*7836SJohn.Forte@Sun.COM )
503*7836SJohn.Forte@Sun.COM {
504*7836SJohn.Forte@Sun.COM 	return (getISCSINodeParameter(MIN_MAX_PARAM, &Oid, pProps,
505*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH));
506*7836SJohn.Forte@Sun.COM }
507*7836SJohn.Forte@Sun.COM 
508*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetMaxBurstLengthProperties(
509*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
510*7836SJohn.Forte@Sun.COM 		IMA_MIN_MAX_VALUE *pProps
511*7836SJohn.Forte@Sun.COM )
512*7836SJohn.Forte@Sun.COM {
513*7836SJohn.Forte@Sun.COM 	return (getISCSINodeParameter(MIN_MAX_PARAM, &Oid, pProps,
514*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH));
515*7836SJohn.Forte@Sun.COM }
516*7836SJohn.Forte@Sun.COM 
517*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetMaxRecvDataSegmentLengthProperties(
518*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
519*7836SJohn.Forte@Sun.COM 		IMA_MIN_MAX_VALUE *pProps
520*7836SJohn.Forte@Sun.COM )
521*7836SJohn.Forte@Sun.COM {
522*7836SJohn.Forte@Sun.COM 	return (getISCSINodeParameter(MIN_MAX_PARAM, &Oid, pProps,
523*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH));
524*7836SJohn.Forte@Sun.COM }
525*7836SJohn.Forte@Sun.COM 
526*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
527*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_PluginIOCtl(
528*7836SJohn.Forte@Sun.COM 		IMA_OID pluginOid,
529*7836SJohn.Forte@Sun.COM 		IMA_UINT command,
530*7836SJohn.Forte@Sun.COM 		const void *pInputBuffer,
531*7836SJohn.Forte@Sun.COM 		IMA_UINT inputBufferLength,
532*7836SJohn.Forte@Sun.COM 		void *pOutputBuffer,
533*7836SJohn.Forte@Sun.COM 		IMA_UINT *pOutputBufferLength
534*7836SJohn.Forte@Sun.COM )
535*7836SJohn.Forte@Sun.COM {
536*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_NOT_SUPPORTED);
537*7836SJohn.Forte@Sun.COM }
538*7836SJohn.Forte@Sun.COM 
539*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetFirstBurstLength(
540*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaId,
541*7836SJohn.Forte@Sun.COM 		IMA_UINT firstBurstLength
542*7836SJohn.Forte@Sun.COM )
543*7836SJohn.Forte@Sun.COM {
544*7836SJohn.Forte@Sun.COM 	IMA_MIN_MAX_VALUE mv;
545*7836SJohn.Forte@Sun.COM 
546*7836SJohn.Forte@Sun.COM 	mv.currentValue = firstBurstLength;
547*7836SJohn.Forte@Sun.COM 	return (setISCSINodeParameter(MIN_MAX_PARAM, &lhbaId, &mv,
548*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH));
549*7836SJohn.Forte@Sun.COM }
550*7836SJohn.Forte@Sun.COM 
551*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetMaxBurstLength(
552*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaId,
553*7836SJohn.Forte@Sun.COM 		IMA_UINT maxBurstLength
554*7836SJohn.Forte@Sun.COM )
555*7836SJohn.Forte@Sun.COM {
556*7836SJohn.Forte@Sun.COM 	IMA_MIN_MAX_VALUE mv;
557*7836SJohn.Forte@Sun.COM 
558*7836SJohn.Forte@Sun.COM 	mv.currentValue = maxBurstLength;
559*7836SJohn.Forte@Sun.COM 	return (setISCSINodeParameter(MIN_MAX_PARAM, &lhbaId, &mv,
560*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH));
561*7836SJohn.Forte@Sun.COM }
562*7836SJohn.Forte@Sun.COM 
563*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetMaxRecvDataSegmentLength(
564*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaId,
565*7836SJohn.Forte@Sun.COM 		IMA_UINT maxRecvDataSegmentLength
566*7836SJohn.Forte@Sun.COM )
567*7836SJohn.Forte@Sun.COM {
568*7836SJohn.Forte@Sun.COM 	IMA_MIN_MAX_VALUE mv;
569*7836SJohn.Forte@Sun.COM 
570*7836SJohn.Forte@Sun.COM 	mv.currentValue = maxRecvDataSegmentLength;
571*7836SJohn.Forte@Sun.COM 	return (setISCSINodeParameter(MIN_MAX_PARAM, &lhbaId, &mv,
572*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH));
573*7836SJohn.Forte@Sun.COM }
574*7836SJohn.Forte@Sun.COM 
575*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetMaxConnectionsProperties(
576*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
577*7836SJohn.Forte@Sun.COM 		IMA_MIN_MAX_VALUE *pProps
578*7836SJohn.Forte@Sun.COM )
579*7836SJohn.Forte@Sun.COM {
580*7836SJohn.Forte@Sun.COM 	return (getISCSINodeParameter(MIN_MAX_PARAM, &Oid, pProps,
581*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_MAX_CONNECTIONS));
582*7836SJohn.Forte@Sun.COM }
583*7836SJohn.Forte@Sun.COM 
584*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetMaxConnections(
585*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaId,
586*7836SJohn.Forte@Sun.COM 		IMA_UINT maxConnections
587*7836SJohn.Forte@Sun.COM )
588*7836SJohn.Forte@Sun.COM {
589*7836SJohn.Forte@Sun.COM 	IMA_MIN_MAX_VALUE mv;
590*7836SJohn.Forte@Sun.COM 
591*7836SJohn.Forte@Sun.COM 	mv.currentValue = maxConnections;
592*7836SJohn.Forte@Sun.COM 	return (setISCSINodeParameter(MIN_MAX_PARAM, &lhbaId, &mv,
593*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_MAX_CONNECTIONS));
594*7836SJohn.Forte@Sun.COM }
595*7836SJohn.Forte@Sun.COM 
596*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetDefaultTime2RetainProperties(
597*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaId,
598*7836SJohn.Forte@Sun.COM 		IMA_MIN_MAX_VALUE *pProps
599*7836SJohn.Forte@Sun.COM )
600*7836SJohn.Forte@Sun.COM {
601*7836SJohn.Forte@Sun.COM 	return (getISCSINodeParameter(MIN_MAX_PARAM, &lhbaId, pProps,
602*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN));
603*7836SJohn.Forte@Sun.COM }
604*7836SJohn.Forte@Sun.COM 
605*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetDefaultTime2Retain(
606*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaId,
607*7836SJohn.Forte@Sun.COM 		IMA_UINT defaultTime2Retain
608*7836SJohn.Forte@Sun.COM )
609*7836SJohn.Forte@Sun.COM {
610*7836SJohn.Forte@Sun.COM 	IMA_MIN_MAX_VALUE mv;
611*7836SJohn.Forte@Sun.COM 
612*7836SJohn.Forte@Sun.COM 	mv.currentValue = defaultTime2Retain;
613*7836SJohn.Forte@Sun.COM 	return (setISCSINodeParameter(MIN_MAX_PARAM, &lhbaId, &mv,
614*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN));
615*7836SJohn.Forte@Sun.COM }
616*7836SJohn.Forte@Sun.COM 
617*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetDefaultTime2WaitProperties(
618*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaId,
619*7836SJohn.Forte@Sun.COM 		IMA_MIN_MAX_VALUE *pProps
620*7836SJohn.Forte@Sun.COM )
621*7836SJohn.Forte@Sun.COM {
622*7836SJohn.Forte@Sun.COM 	return (getISCSINodeParameter(MIN_MAX_PARAM, &lhbaId, pProps,
623*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT));
624*7836SJohn.Forte@Sun.COM }
625*7836SJohn.Forte@Sun.COM 
626*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetDefaultTime2Wait(
627*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaId,
628*7836SJohn.Forte@Sun.COM 		IMA_UINT defaultTime2Wait
629*7836SJohn.Forte@Sun.COM )
630*7836SJohn.Forte@Sun.COM {
631*7836SJohn.Forte@Sun.COM 	IMA_MIN_MAX_VALUE mv;
632*7836SJohn.Forte@Sun.COM 
633*7836SJohn.Forte@Sun.COM 	mv.currentValue = defaultTime2Wait;
634*7836SJohn.Forte@Sun.COM 	return (setISCSINodeParameter(MIN_MAX_PARAM, &lhbaId, &mv,
635*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT));
636*7836SJohn.Forte@Sun.COM }
637*7836SJohn.Forte@Sun.COM 
638*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetMaxOutstandingR2TProperties(
639*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
640*7836SJohn.Forte@Sun.COM 		IMA_MIN_MAX_VALUE *pProps
641*7836SJohn.Forte@Sun.COM )
642*7836SJohn.Forte@Sun.COM {
643*7836SJohn.Forte@Sun.COM 	return (getISCSINodeParameter(MIN_MAX_PARAM, &Oid, pProps,
644*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_OUTSTANDING_R2T));
645*7836SJohn.Forte@Sun.COM }
646*7836SJohn.Forte@Sun.COM 
647*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetMaxOutstandingR2T(
648*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaId,
649*7836SJohn.Forte@Sun.COM 		IMA_UINT maxOutstandingR2T
650*7836SJohn.Forte@Sun.COM )
651*7836SJohn.Forte@Sun.COM {
652*7836SJohn.Forte@Sun.COM 	IMA_MIN_MAX_VALUE mv;
653*7836SJohn.Forte@Sun.COM 
654*7836SJohn.Forte@Sun.COM 	mv.currentValue = maxOutstandingR2T;
655*7836SJohn.Forte@Sun.COM 	return (setISCSINodeParameter(MIN_MAX_PARAM, &lhbaId, &mv,
656*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_OUTSTANDING_R2T));
657*7836SJohn.Forte@Sun.COM }
658*7836SJohn.Forte@Sun.COM 
659*7836SJohn.Forte@Sun.COM 
660*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetErrorRecoveryLevelProperties(
661*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
662*7836SJohn.Forte@Sun.COM 		IMA_MIN_MAX_VALUE *pProps
663*7836SJohn.Forte@Sun.COM )
664*7836SJohn.Forte@Sun.COM {
665*7836SJohn.Forte@Sun.COM 	return (getISCSINodeParameter(MIN_MAX_PARAM, &Oid, pProps,
666*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL));
667*7836SJohn.Forte@Sun.COM }
668*7836SJohn.Forte@Sun.COM 
669*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetErrorRecoveryLevel(
670*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
671*7836SJohn.Forte@Sun.COM 		IMA_UINT errorRecoveryLevel
672*7836SJohn.Forte@Sun.COM )
673*7836SJohn.Forte@Sun.COM {
674*7836SJohn.Forte@Sun.COM 	IMA_MIN_MAX_VALUE mv;
675*7836SJohn.Forte@Sun.COM 
676*7836SJohn.Forte@Sun.COM 	mv.currentValue = errorRecoveryLevel;
677*7836SJohn.Forte@Sun.COM 	return (setISCSINodeParameter(MIN_MAX_PARAM, &Oid, &mv,
678*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL));
679*7836SJohn.Forte@Sun.COM }
680*7836SJohn.Forte@Sun.COM 
681*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetInitialR2TProperties(
682*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
683*7836SJohn.Forte@Sun.COM 		IMA_BOOL_VALUE *pProps
684*7836SJohn.Forte@Sun.COM )
685*7836SJohn.Forte@Sun.COM {
686*7836SJohn.Forte@Sun.COM 	return (getISCSINodeParameter(BOOL_PARAM, &Oid, pProps,
687*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_INITIAL_R2T));
688*7836SJohn.Forte@Sun.COM }
689*7836SJohn.Forte@Sun.COM 
690*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetInitialR2T(
691*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
692*7836SJohn.Forte@Sun.COM 		IMA_BOOL initialR2T
693*7836SJohn.Forte@Sun.COM )
694*7836SJohn.Forte@Sun.COM {
695*7836SJohn.Forte@Sun.COM 	IMA_BOOL_VALUE bv;
696*7836SJohn.Forte@Sun.COM 
697*7836SJohn.Forte@Sun.COM 	bv.currentValue = initialR2T;
698*7836SJohn.Forte@Sun.COM 	return (setISCSINodeParameter(BOOL_PARAM, &Oid, &bv,
699*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_INITIAL_R2T));
700*7836SJohn.Forte@Sun.COM }
701*7836SJohn.Forte@Sun.COM 
702*7836SJohn.Forte@Sun.COM 
703*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetImmediateDataProperties(
704*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
705*7836SJohn.Forte@Sun.COM 		IMA_BOOL_VALUE *pProps
706*7836SJohn.Forte@Sun.COM )
707*7836SJohn.Forte@Sun.COM {
708*7836SJohn.Forte@Sun.COM 	return (getISCSINodeParameter(BOOL_PARAM, &Oid, pProps,
709*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_IMMEDIATE_DATA));
710*7836SJohn.Forte@Sun.COM }
711*7836SJohn.Forte@Sun.COM 
712*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetImmediateData(
713*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
714*7836SJohn.Forte@Sun.COM 		IMA_BOOL immediateData
715*7836SJohn.Forte@Sun.COM )
716*7836SJohn.Forte@Sun.COM {
717*7836SJohn.Forte@Sun.COM 	IMA_BOOL_VALUE bv;
718*7836SJohn.Forte@Sun.COM 
719*7836SJohn.Forte@Sun.COM 	bv.currentValue = immediateData;
720*7836SJohn.Forte@Sun.COM 	return (setISCSINodeParameter(BOOL_PARAM, &Oid, &bv,
721*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_IMMEDIATE_DATA));
722*7836SJohn.Forte@Sun.COM }
723*7836SJohn.Forte@Sun.COM 
724*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetDataPduInOrderProperties(
725*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
726*7836SJohn.Forte@Sun.COM 		IMA_BOOL_VALUE *pProps
727*7836SJohn.Forte@Sun.COM )
728*7836SJohn.Forte@Sun.COM {
729*7836SJohn.Forte@Sun.COM 	return (getISCSINodeParameter(BOOL_PARAM, &Oid, pProps,
730*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER));
731*7836SJohn.Forte@Sun.COM }
732*7836SJohn.Forte@Sun.COM 
733*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetDataPduInOrder(
734*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
735*7836SJohn.Forte@Sun.COM 		IMA_BOOL dataPduInOrder
736*7836SJohn.Forte@Sun.COM )
737*7836SJohn.Forte@Sun.COM {
738*7836SJohn.Forte@Sun.COM 	IMA_BOOL_VALUE bv;
739*7836SJohn.Forte@Sun.COM 
740*7836SJohn.Forte@Sun.COM 	bv.currentValue = dataPduInOrder;
741*7836SJohn.Forte@Sun.COM 	return (setISCSINodeParameter(BOOL_PARAM, &Oid, &bv,
742*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER));
743*7836SJohn.Forte@Sun.COM }
744*7836SJohn.Forte@Sun.COM 
745*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetDataSequenceInOrderProperties(
746*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
747*7836SJohn.Forte@Sun.COM 		IMA_BOOL_VALUE *pProps
748*7836SJohn.Forte@Sun.COM )
749*7836SJohn.Forte@Sun.COM {
750*7836SJohn.Forte@Sun.COM 	return (getISCSINodeParameter(BOOL_PARAM, &Oid, pProps,
751*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER));
752*7836SJohn.Forte@Sun.COM }
753*7836SJohn.Forte@Sun.COM 
754*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetDataSequenceInOrder(
755*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
756*7836SJohn.Forte@Sun.COM 		IMA_BOOL dataSequenceInOrder
757*7836SJohn.Forte@Sun.COM )
758*7836SJohn.Forte@Sun.COM {
759*7836SJohn.Forte@Sun.COM 	IMA_BOOL_VALUE bv;
760*7836SJohn.Forte@Sun.COM 
761*7836SJohn.Forte@Sun.COM 	bv.currentValue = dataSequenceInOrder;
762*7836SJohn.Forte@Sun.COM 	return (setISCSINodeParameter(BOOL_PARAM, &Oid, &bv,
763*7836SJohn.Forte@Sun.COM 	    ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER));
764*7836SJohn.Forte@Sun.COM }
765*7836SJohn.Forte@Sun.COM 
766*7836SJohn.Forte@Sun.COM 
767*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
768*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetStatisticsCollection(
769*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
770*7836SJohn.Forte@Sun.COM 		IMA_BOOL enableStatisticsCollection
771*7836SJohn.Forte@Sun.COM )
772*7836SJohn.Forte@Sun.COM {
773*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_NOT_SUPPORTED);
774*7836SJohn.Forte@Sun.COM }
775*7836SJohn.Forte@Sun.COM 
776*7836SJohn.Forte@Sun.COM 
777*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
778*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetDiscoveryAddressOidList(
779*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
780*7836SJohn.Forte@Sun.COM 		IMA_OID_LIST **ppList
781*7836SJohn.Forte@Sun.COM )
782*7836SJohn.Forte@Sun.COM {
783*7836SJohn.Forte@Sun.COM 	int fd, i, addr_list_size;
784*7836SJohn.Forte@Sun.COM 	iscsi_addr_list_t *idlp, al_info;
785*7836SJohn.Forte@Sun.COM 
786*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
787*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
788*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
789*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
790*7836SJohn.Forte@Sun.COM 	}
791*7836SJohn.Forte@Sun.COM 
792*7836SJohn.Forte@Sun.COM 	(void) memset(&al_info, 0, sizeof (al_info));
793*7836SJohn.Forte@Sun.COM 	al_info.al_vers = ISCSI_INTERFACE_VERSION;
794*7836SJohn.Forte@Sun.COM 	al_info.al_in_cnt = 0;
795*7836SJohn.Forte@Sun.COM 
796*7836SJohn.Forte@Sun.COM 	/*
797*7836SJohn.Forte@Sun.COM 	 * Issue ioctl to obtain the number of targets.
798*7836SJohn.Forte@Sun.COM 	 */
799*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_DISCOVERY_ADDR_LIST_GET, &al_info) != 0) {
800*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
801*7836SJohn.Forte@Sun.COM 		    "ISCSI_DISCOVERY_ADDR_LIST_GET ioctl %d failed, errno: %d",
802*7836SJohn.Forte@Sun.COM 		    ISCSI_DISCOVERY_ADDR_LIST_GET, errno);
803*7836SJohn.Forte@Sun.COM 		(void) close(fd);
804*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
805*7836SJohn.Forte@Sun.COM 	}
806*7836SJohn.Forte@Sun.COM 
807*7836SJohn.Forte@Sun.COM 	addr_list_size = sizeof (iscsi_addr_list_t);
808*7836SJohn.Forte@Sun.COM 	if (al_info.al_out_cnt > 1) {
809*7836SJohn.Forte@Sun.COM 		addr_list_size += (sizeof (iscsi_addr_list_t) *
810*7836SJohn.Forte@Sun.COM 		    al_info.al_out_cnt - 1);
811*7836SJohn.Forte@Sun.COM 	}
812*7836SJohn.Forte@Sun.COM 
813*7836SJohn.Forte@Sun.COM 	idlp = (iscsi_addr_list_t *)calloc(1, addr_list_size);
814*7836SJohn.Forte@Sun.COM 	if (idlp == NULL) {
815*7836SJohn.Forte@Sun.COM 		(void) close(fd);
816*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
817*7836SJohn.Forte@Sun.COM 	}
818*7836SJohn.Forte@Sun.COM 
819*7836SJohn.Forte@Sun.COM 	idlp->al_vers = ISCSI_INTERFACE_VERSION;
820*7836SJohn.Forte@Sun.COM 	idlp->al_in_cnt = al_info.al_out_cnt;
821*7836SJohn.Forte@Sun.COM 	/* Issue the same ioctl again to obtain the OIDs. */
822*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_DISCOVERY_ADDR_LIST_GET, idlp) != 0) {
823*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
824*7836SJohn.Forte@Sun.COM 		    "ISCSI_TARGET_OID_LIST_GET ioctl %d failed, errno: %d",
825*7836SJohn.Forte@Sun.COM 		    ISCSI_DISCOVERY_ADDR_LIST_GET, errno);
826*7836SJohn.Forte@Sun.COM 		free(idlp);
827*7836SJohn.Forte@Sun.COM 		(void) close(fd);
828*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
829*7836SJohn.Forte@Sun.COM 	}
830*7836SJohn.Forte@Sun.COM 
831*7836SJohn.Forte@Sun.COM 	*ppList = (IMA_OID_LIST *)calloc(1, sizeof (IMA_OID_LIST) +
832*7836SJohn.Forte@Sun.COM 	    idlp->al_out_cnt * sizeof (IMA_OID));
833*7836SJohn.Forte@Sun.COM 	if (*ppList == NULL) {
834*7836SJohn.Forte@Sun.COM 		free(idlp);
835*7836SJohn.Forte@Sun.COM 		(void) close(fd);
836*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
837*7836SJohn.Forte@Sun.COM 	}
838*7836SJohn.Forte@Sun.COM 	(*ppList)->oidCount = idlp->al_out_cnt;
839*7836SJohn.Forte@Sun.COM 
840*7836SJohn.Forte@Sun.COM 	for (i = 0; i < idlp->al_out_cnt; i++) {
841*7836SJohn.Forte@Sun.COM 		(*ppList)->oids[i].objectType =
842*7836SJohn.Forte@Sun.COM 		    IMA_OBJECT_TYPE_DISCOVERY_ADDRESS;
843*7836SJohn.Forte@Sun.COM 		(*ppList)->oids[i].ownerId = pluginOwnerId;
844*7836SJohn.Forte@Sun.COM 		(*ppList)->oids[i].objectSequenceNumber =
845*7836SJohn.Forte@Sun.COM 		    idlp->al_addrs[i].a_oid;
846*7836SJohn.Forte@Sun.COM 	}
847*7836SJohn.Forte@Sun.COM 
848*7836SJohn.Forte@Sun.COM 	free(idlp);
849*7836SJohn.Forte@Sun.COM 	(void) close(fd);
850*7836SJohn.Forte@Sun.COM 
851*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
852*7836SJohn.Forte@Sun.COM }
853*7836SJohn.Forte@Sun.COM 
854*7836SJohn.Forte@Sun.COM 
855*7836SJohn.Forte@Sun.COM /* ARGSUSED */
856*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetStaticDiscoveryTargetOidList(
857*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
858*7836SJohn.Forte@Sun.COM 		IMA_OID_LIST **ppList
859*7836SJohn.Forte@Sun.COM )
860*7836SJohn.Forte@Sun.COM {
861*7836SJohn.Forte@Sun.COM 	if (Oid.objectType == IMA_OBJECT_TYPE_PNP) {
862*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_OBJECT_NOT_FOUND);
863*7836SJohn.Forte@Sun.COM 	}
864*7836SJohn.Forte@Sun.COM 
865*7836SJohn.Forte@Sun.COM 	return (get_target_oid_list(ISCSI_STATIC_TGT_OID_LIST, ppList));
866*7836SJohn.Forte@Sun.COM }
867*7836SJohn.Forte@Sun.COM 
868*7836SJohn.Forte@Sun.COM /* ARGSUSED */
869*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetTargetOidList(
870*7836SJohn.Forte@Sun.COM 		IMA_OID Oid,
871*7836SJohn.Forte@Sun.COM 		IMA_OID_LIST **ppList
872*7836SJohn.Forte@Sun.COM )
873*7836SJohn.Forte@Sun.COM {
874*7836SJohn.Forte@Sun.COM 	return (get_target_oid_list(ISCSI_TGT_PARAM_OID_LIST, ppList));
875*7836SJohn.Forte@Sun.COM }
876*7836SJohn.Forte@Sun.COM 
877*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
878*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetIsnsDiscovery(
879*7836SJohn.Forte@Sun.COM 		IMA_OID phbaId,
880*7836SJohn.Forte@Sun.COM 		IMA_BOOL enableIsnsDiscovery,
881*7836SJohn.Forte@Sun.COM 		IMA_ISNS_DISCOVERY_METHOD discoveryMethod,
882*7836SJohn.Forte@Sun.COM 		const IMA_HOST_ID *iSnsHost
883*7836SJohn.Forte@Sun.COM )
884*7836SJohn.Forte@Sun.COM {
885*7836SJohn.Forte@Sun.COM 	/* XXX need to set discovery Method and domaineName */
886*7836SJohn.Forte@Sun.COM 	return (configure_discovery_method(enableIsnsDiscovery,
887*7836SJohn.Forte@Sun.COM 	    iSCSIDiscoveryMethodISNS));
888*7836SJohn.Forte@Sun.COM }
889*7836SJohn.Forte@Sun.COM 
890*7836SJohn.Forte@Sun.COM 
891*7836SJohn.Forte@Sun.COM /* ARGSUSED */
892*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetSlpDiscovery(
893*7836SJohn.Forte@Sun.COM 		IMA_OID phbaId,
894*7836SJohn.Forte@Sun.COM 		IMA_BOOL enableSlpDiscovery
895*7836SJohn.Forte@Sun.COM )
896*7836SJohn.Forte@Sun.COM {
897*7836SJohn.Forte@Sun.COM 	return (configure_discovery_method(enableSlpDiscovery,
898*7836SJohn.Forte@Sun.COM 	    iSCSIDiscoveryMethodSLP));
899*7836SJohn.Forte@Sun.COM }
900*7836SJohn.Forte@Sun.COM 
901*7836SJohn.Forte@Sun.COM 
902*7836SJohn.Forte@Sun.COM /* ARGSUSED */
903*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetStaticDiscovery(
904*7836SJohn.Forte@Sun.COM 		IMA_OID phbaId,
905*7836SJohn.Forte@Sun.COM 		IMA_BOOL enableStaticDiscovery
906*7836SJohn.Forte@Sun.COM )
907*7836SJohn.Forte@Sun.COM {
908*7836SJohn.Forte@Sun.COM 	return (configure_discovery_method(enableStaticDiscovery,
909*7836SJohn.Forte@Sun.COM 	    iSCSIDiscoveryMethodStatic));
910*7836SJohn.Forte@Sun.COM }
911*7836SJohn.Forte@Sun.COM 
912*7836SJohn.Forte@Sun.COM /* ARGSUSED */
913*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetSendTargetsDiscovery(
914*7836SJohn.Forte@Sun.COM 		IMA_OID phbaId,
915*7836SJohn.Forte@Sun.COM 		IMA_BOOL enableSendTargetsDiscovery
916*7836SJohn.Forte@Sun.COM )
917*7836SJohn.Forte@Sun.COM {
918*7836SJohn.Forte@Sun.COM 	return (configure_discovery_method(enableSendTargetsDiscovery,
919*7836SJohn.Forte@Sun.COM 	    iSCSIDiscoveryMethodSendTargets));
920*7836SJohn.Forte@Sun.COM }
921*7836SJohn.Forte@Sun.COM 
922*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
923*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_RemoveDiscoveryAddress(
924*7836SJohn.Forte@Sun.COM 		IMA_OID	discoveryAddressOid
925*7836SJohn.Forte@Sun.COM )
926*7836SJohn.Forte@Sun.COM {
927*7836SJohn.Forte@Sun.COM 	int status, fd, i, addr_list_size;
928*7836SJohn.Forte@Sun.COM 	iscsi_addr_list_t *idlp, al_info;
929*7836SJohn.Forte@Sun.COM 	iscsi_addr_t *matched_addr = NULL;
930*7836SJohn.Forte@Sun.COM 	entry_t	entry;
931*7836SJohn.Forte@Sun.COM 
932*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
933*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
934*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
935*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
936*7836SJohn.Forte@Sun.COM 	}
937*7836SJohn.Forte@Sun.COM 
938*7836SJohn.Forte@Sun.COM 	(void) memset(&al_info, 0, sizeof (al_info));
939*7836SJohn.Forte@Sun.COM 	al_info.al_vers = ISCSI_INTERFACE_VERSION;
940*7836SJohn.Forte@Sun.COM 	al_info.al_in_cnt = 0;
941*7836SJohn.Forte@Sun.COM 
942*7836SJohn.Forte@Sun.COM 	/*
943*7836SJohn.Forte@Sun.COM 	 * Issue ioctl to obtain the number of discovery address.
944*7836SJohn.Forte@Sun.COM 	 */
945*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_DISCOVERY_ADDR_LIST_GET, &al_info) != 0) {
946*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
947*7836SJohn.Forte@Sun.COM 		    "ISCSI_DISCOVERY_ADDR_LIST_GET ioctl %d failed, errno: %d",
948*7836SJohn.Forte@Sun.COM 		    ISCSI_DISCOVERY_ADDR_LIST_GET, errno);
949*7836SJohn.Forte@Sun.COM 		(void) close(fd);
950*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
951*7836SJohn.Forte@Sun.COM 	}
952*7836SJohn.Forte@Sun.COM 
953*7836SJohn.Forte@Sun.COM 	if (al_info.al_out_cnt == 0) {
954*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_OBJECT_NOT_FOUND);
955*7836SJohn.Forte@Sun.COM 	}
956*7836SJohn.Forte@Sun.COM 
957*7836SJohn.Forte@Sun.COM 	addr_list_size = sizeof (iscsi_addr_list_t);
958*7836SJohn.Forte@Sun.COM 	if (al_info.al_out_cnt > 1) {
959*7836SJohn.Forte@Sun.COM 		addr_list_size += (sizeof (iscsi_addr_list_t) *
960*7836SJohn.Forte@Sun.COM 		    al_info.al_out_cnt - 1);
961*7836SJohn.Forte@Sun.COM 	}
962*7836SJohn.Forte@Sun.COM 
963*7836SJohn.Forte@Sun.COM 	idlp = (iscsi_addr_list_t *)calloc(1, addr_list_size);
964*7836SJohn.Forte@Sun.COM 	if (idlp == NULL) {
965*7836SJohn.Forte@Sun.COM 		(void) close(fd);
966*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
967*7836SJohn.Forte@Sun.COM 	}
968*7836SJohn.Forte@Sun.COM 
969*7836SJohn.Forte@Sun.COM 	idlp->al_vers = ISCSI_INTERFACE_VERSION;
970*7836SJohn.Forte@Sun.COM 	idlp->al_in_cnt = al_info.al_out_cnt;
971*7836SJohn.Forte@Sun.COM 
972*7836SJohn.Forte@Sun.COM 	/* Issue the same ioctl again to obtain the OIDs. */
973*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_DISCOVERY_ADDR_LIST_GET, idlp) != 0) {
974*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
975*7836SJohn.Forte@Sun.COM 		    "ISCSI_TARGET_OID_LIST_GET ioctl %d failed, errno: %d",
976*7836SJohn.Forte@Sun.COM 		    ISCSI_DISCOVERY_ADDR_LIST_GET, errno);
977*7836SJohn.Forte@Sun.COM 		free(idlp);
978*7836SJohn.Forte@Sun.COM 		(void) close(fd);
979*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
980*7836SJohn.Forte@Sun.COM 	}
981*7836SJohn.Forte@Sun.COM 
982*7836SJohn.Forte@Sun.COM 	for (i = 0; i < idlp->al_out_cnt; i++) {
983*7836SJohn.Forte@Sun.COM 		if (discoveryAddressOid.objectSequenceNumber !=
984*7836SJohn.Forte@Sun.COM 		    idlp->al_addrs[i].a_oid)
985*7836SJohn.Forte@Sun.COM 			continue;
986*7836SJohn.Forte@Sun.COM 		matched_addr = &(idlp->al_addrs[i]);
987*7836SJohn.Forte@Sun.COM 	}
988*7836SJohn.Forte@Sun.COM 
989*7836SJohn.Forte@Sun.COM 	if (matched_addr == NULL) {
990*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_OBJECT_NOT_FOUND);
991*7836SJohn.Forte@Sun.COM 	}
992*7836SJohn.Forte@Sun.COM 
993*7836SJohn.Forte@Sun.COM 
994*7836SJohn.Forte@Sun.COM 	(void) memset(&entry, 0, sizeof (entry_t));
995*7836SJohn.Forte@Sun.COM 	entry.e_vers = ISCSI_INTERFACE_VERSION;
996*7836SJohn.Forte@Sun.COM 	entry.e_oid  = discoveryAddressOid.objectSequenceNumber;
997*7836SJohn.Forte@Sun.COM 	if (matched_addr->a_addr.i_insize == sizeof (struct in_addr)) {
998*7836SJohn.Forte@Sun.COM 		bcopy(&matched_addr->a_addr.i_addr.in4,
999*7836SJohn.Forte@Sun.COM 		    &entry.e_u.u_in4, sizeof (entry.e_u.u_in4));
1000*7836SJohn.Forte@Sun.COM 		entry.e_insize = sizeof (struct in_addr);
1001*7836SJohn.Forte@Sun.COM 	} else if (matched_addr->a_addr.i_insize == sizeof (struct in6_addr)) {
1002*7836SJohn.Forte@Sun.COM 		bcopy(&matched_addr->a_addr.i_addr.in6,
1003*7836SJohn.Forte@Sun.COM 		    &entry.e_u.u_in6, sizeof (entry.e_u.u_in6));
1004*7836SJohn.Forte@Sun.COM 		entry.e_insize = sizeof (struct in6_addr);
1005*7836SJohn.Forte@Sun.COM 	} else {
1006*7836SJohn.Forte@Sun.COM 		/* Should not happen */
1007*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1008*7836SJohn.Forte@Sun.COM 		    "ISCSI_STATIC_GET returned bad address");
1009*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1010*7836SJohn.Forte@Sun.COM 	}
1011*7836SJohn.Forte@Sun.COM 
1012*7836SJohn.Forte@Sun.COM 	entry.e_port = matched_addr->a_port;
1013*7836SJohn.Forte@Sun.COM 	entry.e_tpgt = 0;
1014*7836SJohn.Forte@Sun.COM 	entry.e_oid = discoveryAddressOid.objectSequenceNumber;
1015*7836SJohn.Forte@Sun.COM 
1016*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_DISCOVERY_ADDR_CLEAR, &entry)) {
1017*7836SJohn.Forte@Sun.COM 		status = errno;
1018*7836SJohn.Forte@Sun.COM 		(void) close(fd);
1019*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1020*7836SJohn.Forte@Sun.COM 		    "ISCSI_DISCOVERY_ADDR_CLEAR ioctl failed, errno: %d",
1021*7836SJohn.Forte@Sun.COM 		    errno);
1022*7836SJohn.Forte@Sun.COM 		if (status == EBUSY) {
1023*7836SJohn.Forte@Sun.COM 			return (IMA_ERROR_LU_IN_USE);
1024*7836SJohn.Forte@Sun.COM 		} else {
1025*7836SJohn.Forte@Sun.COM 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1026*7836SJohn.Forte@Sun.COM 		}
1027*7836SJohn.Forte@Sun.COM 	}
1028*7836SJohn.Forte@Sun.COM 
1029*7836SJohn.Forte@Sun.COM 	free(idlp);
1030*7836SJohn.Forte@Sun.COM 	(void) close(fd);
1031*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
1032*7836SJohn.Forte@Sun.COM }
1033*7836SJohn.Forte@Sun.COM 
1034*7836SJohn.Forte@Sun.COM 
1035*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
1036*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_AddDiscoveryAddress(
1037*7836SJohn.Forte@Sun.COM 		IMA_OID	oid,
1038*7836SJohn.Forte@Sun.COM 		const IMA_TARGET_ADDRESS discoveryAddress,
1039*7836SJohn.Forte@Sun.COM 		IMA_OID	*pDiscoveryAddressOid
1040*7836SJohn.Forte@Sun.COM )
1041*7836SJohn.Forte@Sun.COM {
1042*7836SJohn.Forte@Sun.COM 	entry_t	    entry;
1043*7836SJohn.Forte@Sun.COM 	int	    fd;
1044*7836SJohn.Forte@Sun.COM 
1045*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
1046*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
1047*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
1048*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1049*7836SJohn.Forte@Sun.COM 	}
1050*7836SJohn.Forte@Sun.COM 
1051*7836SJohn.Forte@Sun.COM 	if (prepare_discovery_entry(discoveryAddress, &entry) !=
1052*7836SJohn.Forte@Sun.COM 	    DISC_ADDR_OK) {
1053*7836SJohn.Forte@Sun.COM 		(void) close(fd);
1054*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INVALID_PARAMETER);
1055*7836SJohn.Forte@Sun.COM 	}
1056*7836SJohn.Forte@Sun.COM 
1057*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_DISCOVERY_ADDR_SET, &entry)) {
1058*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1059*7836SJohn.Forte@Sun.COM 		    "ISCSI_DISCOVERY_ADDR_SET ioctl failed, errno: %d",
1060*7836SJohn.Forte@Sun.COM 		    errno);
1061*7836SJohn.Forte@Sun.COM 		(void) close(fd);
1062*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1063*7836SJohn.Forte@Sun.COM 	}
1064*7836SJohn.Forte@Sun.COM 
1065*7836SJohn.Forte@Sun.COM 	pDiscoveryAddressOid->ownerId = pluginOwnerId;
1066*7836SJohn.Forte@Sun.COM 	pDiscoveryAddressOid->objectType = IMA_OBJECT_TYPE_DISCOVERY_ADDRESS;
1067*7836SJohn.Forte@Sun.COM 	pDiscoveryAddressOid->objectSequenceNumber = entry.e_oid;
1068*7836SJohn.Forte@Sun.COM 
1069*7836SJohn.Forte@Sun.COM 	(void) close(fd);
1070*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
1071*7836SJohn.Forte@Sun.COM }
1072*7836SJohn.Forte@Sun.COM 
1073*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetStaticDiscoveryTargetProperties(
1074*7836SJohn.Forte@Sun.COM 		IMA_OID	staticTargetOid,
1075*7836SJohn.Forte@Sun.COM 		IMA_STATIC_DISCOVERY_TARGET_PROPERTIES *pProps
1076*7836SJohn.Forte@Sun.COM )
1077*7836SJohn.Forte@Sun.COM {
1078*7836SJohn.Forte@Sun.COM 	char static_target_addr_str[SUN_IMA_IP_ADDRESS_LEN];
1079*7836SJohn.Forte@Sun.COM 	char static_target_addr_port_str[SUN_IMA_IP_ADDRESS_LEN];
1080*7836SJohn.Forte@Sun.COM 	int af, fd, status;
1081*7836SJohn.Forte@Sun.COM 	iscsi_static_property_t prop;
1082*7836SJohn.Forte@Sun.COM 	/* LINTED */
1083*7836SJohn.Forte@Sun.COM 	IMA_HOST_ID *host;
1084*7836SJohn.Forte@Sun.COM 
1085*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
1086*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
1087*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
1088*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1089*7836SJohn.Forte@Sun.COM 	}
1090*7836SJohn.Forte@Sun.COM 
1091*7836SJohn.Forte@Sun.COM 	(void) memset(&prop, 0, sizeof (iscsi_static_property_t));
1092*7836SJohn.Forte@Sun.COM 	prop.p_vers = ISCSI_INTERFACE_VERSION;
1093*7836SJohn.Forte@Sun.COM 	prop.p_oid = (uint32_t)staticTargetOid.objectSequenceNumber;
1094*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_STATIC_GET, &prop) != 0) {
1095*7836SJohn.Forte@Sun.COM 		status = errno;
1096*7836SJohn.Forte@Sun.COM 		(void) close(fd);
1097*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1098*7836SJohn.Forte@Sun.COM 		    "ISCSI_STATIC_GET ioctl failed, errno: %d", status);
1099*7836SJohn.Forte@Sun.COM 		if (status == ENOENT) {
1100*7836SJohn.Forte@Sun.COM 			return (IMA_ERROR_OBJECT_NOT_FOUND);
1101*7836SJohn.Forte@Sun.COM 
1102*7836SJohn.Forte@Sun.COM 		} else {
1103*7836SJohn.Forte@Sun.COM 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1104*7836SJohn.Forte@Sun.COM 		}
1105*7836SJohn.Forte@Sun.COM 	}
1106*7836SJohn.Forte@Sun.COM 	(void) close(fd);
1107*7836SJohn.Forte@Sun.COM 
1108*7836SJohn.Forte@Sun.COM 	(void) mbstowcs(pProps->staticTarget.targetName, (char *)prop.p_name,
1109*7836SJohn.Forte@Sun.COM 	    sizeof (pProps->staticTarget.targetName)/sizeof (IMA_WCHAR));
1110*7836SJohn.Forte@Sun.COM 
1111*7836SJohn.Forte@Sun.COM 	if (prop.p_addr_list.al_addrs[0].a_addr.i_insize ==
1112*7836SJohn.Forte@Sun.COM 	    sizeof (struct in_addr)) {
1113*7836SJohn.Forte@Sun.COM 		/* IPv4 */
1114*7836SJohn.Forte@Sun.COM 		af = AF_INET;
1115*7836SJohn.Forte@Sun.COM 	} else if (prop.p_addr_list.al_addrs[0].a_addr.i_insize ==
1116*7836SJohn.Forte@Sun.COM 	    sizeof (struct in6_addr)) {
1117*7836SJohn.Forte@Sun.COM 		/* IPv6 */
1118*7836SJohn.Forte@Sun.COM 		af = AF_INET6;
1119*7836SJohn.Forte@Sun.COM 	} else {
1120*7836SJohn.Forte@Sun.COM 		/* Should not happen */
1121*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1122*7836SJohn.Forte@Sun.COM 		    "ISCSI_STATIC_GET returned bad address");
1123*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1124*7836SJohn.Forte@Sun.COM 	}
1125*7836SJohn.Forte@Sun.COM 
1126*7836SJohn.Forte@Sun.COM 	if (inet_ntop(af, &prop.p_addr_list.al_addrs[0].a_addr.i_addr,
1127*7836SJohn.Forte@Sun.COM 	    static_target_addr_str, sizeof (static_target_addr_str)) == NULL) {
1128*7836SJohn.Forte@Sun.COM 		/* Should not happen */
1129*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1130*7836SJohn.Forte@Sun.COM 		    "ISCSI_STATIC_GET returned address that cannot "
1131*7836SJohn.Forte@Sun.COM 		    "be inet_ntop");
1132*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1133*7836SJohn.Forte@Sun.COM 	} else {
1134*7836SJohn.Forte@Sun.COM 		if (af == AF_INET) {
1135*7836SJohn.Forte@Sun.COM 			(void) snprintf(static_target_addr_port_str,
1136*7836SJohn.Forte@Sun.COM 			    SUN_IMA_IP_ADDRESS_LEN,
1137*7836SJohn.Forte@Sun.COM 			    "%s:%ld",
1138*7836SJohn.Forte@Sun.COM 			    static_target_addr_str,
1139*7836SJohn.Forte@Sun.COM 			    prop.p_addr_list.al_addrs[0].a_port);
1140*7836SJohn.Forte@Sun.COM 		} else {
1141*7836SJohn.Forte@Sun.COM 			(void) snprintf(static_target_addr_port_str,
1142*7836SJohn.Forte@Sun.COM 			    SUN_IMA_IP_ADDRESS_LEN,
1143*7836SJohn.Forte@Sun.COM 			    "[%s]:%ld",
1144*7836SJohn.Forte@Sun.COM 			    static_target_addr_str,
1145*7836SJohn.Forte@Sun.COM 			    prop.p_addr_list.al_addrs[0].a_port);
1146*7836SJohn.Forte@Sun.COM 		}
1147*7836SJohn.Forte@Sun.COM 		host = &pProps->staticTarget.targetAddress.hostnameIpAddress;
1148*7836SJohn.Forte@Sun.COM 		(void) mbstowcs(pProps->staticTarget.
1149*7836SJohn.Forte@Sun.COM 		    targetAddress.hostnameIpAddress.
1150*7836SJohn.Forte@Sun.COM 		    id.hostname, static_target_addr_port_str,
1151*7836SJohn.Forte@Sun.COM 		    sizeof (host->id.hostname) / sizeof (IMA_WCHAR));
1152*7836SJohn.Forte@Sun.COM 	}
1153*7836SJohn.Forte@Sun.COM 
1154*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
1155*7836SJohn.Forte@Sun.COM }
1156*7836SJohn.Forte@Sun.COM 
1157*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
1158*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetDiscoveryAddressProperties(
1159*7836SJohn.Forte@Sun.COM 		IMA_OID	discoveryAddressOid,
1160*7836SJohn.Forte@Sun.COM 		IMA_DISCOVERY_ADDRESS_PROPERTIES *pProps
1161*7836SJohn.Forte@Sun.COM )
1162*7836SJohn.Forte@Sun.COM {
1163*7836SJohn.Forte@Sun.COM 	int fd;
1164*7836SJohn.Forte@Sun.COM 	int i;
1165*7836SJohn.Forte@Sun.COM 	int addr_list_size;
1166*7836SJohn.Forte@Sun.COM 	iscsi_addr_list_t *idlp, al_info;
1167*7836SJohn.Forte@Sun.COM 	iscsi_addr_t *matched_addr = NULL;
1168*7836SJohn.Forte@Sun.COM 	/* LINTED */
1169*7836SJohn.Forte@Sun.COM 	IMA_TARGET_ADDRESS *addr;
1170*7836SJohn.Forte@Sun.COM 
1171*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
1172*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
1173*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
1174*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1175*7836SJohn.Forte@Sun.COM 	}
1176*7836SJohn.Forte@Sun.COM 
1177*7836SJohn.Forte@Sun.COM 	(void) memset(&al_info, 0, sizeof (al_info));
1178*7836SJohn.Forte@Sun.COM 	al_info.al_vers = ISCSI_INTERFACE_VERSION;
1179*7836SJohn.Forte@Sun.COM 	al_info.al_in_cnt = 0;
1180*7836SJohn.Forte@Sun.COM 
1181*7836SJohn.Forte@Sun.COM 	/*
1182*7836SJohn.Forte@Sun.COM 	 * Issue ioctl to obtain the number of discovery addresses.
1183*7836SJohn.Forte@Sun.COM 	 */
1184*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_DISCOVERY_ADDR_LIST_GET, &al_info) != 0) {
1185*7836SJohn.Forte@Sun.COM 		(void) close(fd);
1186*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1187*7836SJohn.Forte@Sun.COM 		    "ISCSI_DISCOVERY_ADDR_LIST_GET ioctl %d failed, errno: %d",
1188*7836SJohn.Forte@Sun.COM 		    ISCSI_DISCOVERY_ADDR_LIST_GET, errno);
1189*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1190*7836SJohn.Forte@Sun.COM 	}
1191*7836SJohn.Forte@Sun.COM 
1192*7836SJohn.Forte@Sun.COM 	if (al_info.al_out_cnt == 0) {
1193*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_OBJECT_NOT_FOUND);
1194*7836SJohn.Forte@Sun.COM 	}
1195*7836SJohn.Forte@Sun.COM 
1196*7836SJohn.Forte@Sun.COM 	addr_list_size = sizeof (iscsi_addr_list_t);
1197*7836SJohn.Forte@Sun.COM 	if (al_info.al_out_cnt > 1) {
1198*7836SJohn.Forte@Sun.COM 		addr_list_size += (sizeof (iscsi_addr_list_t) *
1199*7836SJohn.Forte@Sun.COM 		    al_info.al_out_cnt - 1);
1200*7836SJohn.Forte@Sun.COM 	}
1201*7836SJohn.Forte@Sun.COM 
1202*7836SJohn.Forte@Sun.COM 	idlp = (iscsi_addr_list_t *)calloc(1, addr_list_size);
1203*7836SJohn.Forte@Sun.COM 	if (idlp == NULL) {
1204*7836SJohn.Forte@Sun.COM 		(void) close(fd);
1205*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
1206*7836SJohn.Forte@Sun.COM 	}
1207*7836SJohn.Forte@Sun.COM 
1208*7836SJohn.Forte@Sun.COM 	idlp->al_vers = ISCSI_INTERFACE_VERSION;
1209*7836SJohn.Forte@Sun.COM 	idlp->al_in_cnt = al_info.al_out_cnt;
1210*7836SJohn.Forte@Sun.COM 
1211*7836SJohn.Forte@Sun.COM 	/* Issue the same ioctl again to obtain the OIDs. */
1212*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_DISCOVERY_ADDR_LIST_GET, idlp) != 0) {
1213*7836SJohn.Forte@Sun.COM 		free(idlp);
1214*7836SJohn.Forte@Sun.COM 		(void) close(fd);
1215*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1216*7836SJohn.Forte@Sun.COM 		    "ISCSI_TARGET_OID_LIST_GET ioctl %d failed, errno: %d",
1217*7836SJohn.Forte@Sun.COM 		    ISCSI_DISCOVERY_ADDR_LIST_GET, errno);
1218*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1219*7836SJohn.Forte@Sun.COM 	}
1220*7836SJohn.Forte@Sun.COM 
1221*7836SJohn.Forte@Sun.COM 	for (i = 0; i < idlp->al_out_cnt; i++) {
1222*7836SJohn.Forte@Sun.COM 		if (discoveryAddressOid.objectSequenceNumber !=
1223*7836SJohn.Forte@Sun.COM 		    idlp->al_addrs[i].a_oid)
1224*7836SJohn.Forte@Sun.COM 			continue;
1225*7836SJohn.Forte@Sun.COM 		matched_addr = &(idlp->al_addrs[i]);
1226*7836SJohn.Forte@Sun.COM 	}
1227*7836SJohn.Forte@Sun.COM 
1228*7836SJohn.Forte@Sun.COM 	if (matched_addr == NULL) {
1229*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_OBJECT_NOT_FOUND);
1230*7836SJohn.Forte@Sun.COM 	}
1231*7836SJohn.Forte@Sun.COM 
1232*7836SJohn.Forte@Sun.COM 	if (matched_addr->a_addr.i_insize == sizeof (struct in_addr)) {
1233*7836SJohn.Forte@Sun.COM 		pProps->discoveryAddress.hostnameIpAddress.id.
1234*7836SJohn.Forte@Sun.COM 		    ipAddress.ipv4Address = IMA_TRUE;
1235*7836SJohn.Forte@Sun.COM 	} else if (matched_addr->a_addr.i_insize == sizeof (struct in6_addr)) {
1236*7836SJohn.Forte@Sun.COM 		pProps->discoveryAddress.hostnameIpAddress.id.
1237*7836SJohn.Forte@Sun.COM 		    ipAddress.ipv4Address = IMA_FALSE;
1238*7836SJohn.Forte@Sun.COM 	} else {
1239*7836SJohn.Forte@Sun.COM 		/* Should not happen */
1240*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1241*7836SJohn.Forte@Sun.COM 		    "ISCSI_STATIC_GET returned bad address");
1242*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1243*7836SJohn.Forte@Sun.COM 	}
1244*7836SJohn.Forte@Sun.COM 
1245*7836SJohn.Forte@Sun.COM 	addr = &pProps->discoveryAddress;
1246*7836SJohn.Forte@Sun.COM 	bcopy(&(matched_addr->a_addr.i_addr), pProps->discoveryAddress.
1247*7836SJohn.Forte@Sun.COM 	    hostnameIpAddress.id.ipAddress.ipAddress,
1248*7836SJohn.Forte@Sun.COM 	    sizeof (addr->hostnameIpAddress.id.ipAddress.ipAddress));
1249*7836SJohn.Forte@Sun.COM 
1250*7836SJohn.Forte@Sun.COM 	pProps->discoveryAddress.portNumber = matched_addr->a_port;
1251*7836SJohn.Forte@Sun.COM 
1252*7836SJohn.Forte@Sun.COM 	pProps->associatedLhbaOid.objectType = IMA_OBJECT_TYPE_LHBA;
1253*7836SJohn.Forte@Sun.COM 	pProps->associatedLhbaOid.ownerId = pluginOwnerId;
1254*7836SJohn.Forte@Sun.COM 	pProps->associatedLhbaOid.objectSequenceNumber = ISCSI_INITIATOR_OID;
1255*7836SJohn.Forte@Sun.COM 
1256*7836SJohn.Forte@Sun.COM 	free(idlp);
1257*7836SJohn.Forte@Sun.COM 	(void) close(fd);
1258*7836SJohn.Forte@Sun.COM 
1259*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
1260*7836SJohn.Forte@Sun.COM }
1261*7836SJohn.Forte@Sun.COM 
1262*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_RemoveStaticDiscoveryTarget(
1263*7836SJohn.Forte@Sun.COM 		IMA_OID staticTargetOid
1264*7836SJohn.Forte@Sun.COM )
1265*7836SJohn.Forte@Sun.COM {
1266*7836SJohn.Forte@Sun.COM 	entry_t	entry;
1267*7836SJohn.Forte@Sun.COM 	int	status, fd;
1268*7836SJohn.Forte@Sun.COM 
1269*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
1270*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
1271*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
1272*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1273*7836SJohn.Forte@Sun.COM 	}
1274*7836SJohn.Forte@Sun.COM 
1275*7836SJohn.Forte@Sun.COM 	(void) memset(&entry, 0, sizeof (entry_t));
1276*7836SJohn.Forte@Sun.COM 	entry.e_vers = ISCSI_INTERFACE_VERSION;
1277*7836SJohn.Forte@Sun.COM 	entry.e_oid = (uint32_t)staticTargetOid.objectSequenceNumber;
1278*7836SJohn.Forte@Sun.COM 
1279*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_STATIC_CLEAR, &entry)) {
1280*7836SJohn.Forte@Sun.COM 		status = errno;
1281*7836SJohn.Forte@Sun.COM 		(void) close(fd);
1282*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1283*7836SJohn.Forte@Sun.COM 		    "ISCSI_STATIC_CLEAR ioctl failed, errno: %d", errno);
1284*7836SJohn.Forte@Sun.COM 		if (status == EBUSY) {
1285*7836SJohn.Forte@Sun.COM 			return (IMA_ERROR_LU_IN_USE);
1286*7836SJohn.Forte@Sun.COM 		} else {
1287*7836SJohn.Forte@Sun.COM 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1288*7836SJohn.Forte@Sun.COM 		}
1289*7836SJohn.Forte@Sun.COM 	}
1290*7836SJohn.Forte@Sun.COM 
1291*7836SJohn.Forte@Sun.COM 	(void) close(fd);
1292*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
1293*7836SJohn.Forte@Sun.COM }
1294*7836SJohn.Forte@Sun.COM 
1295*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
1296*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_AddStaticDiscoveryTarget(
1297*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaOid,
1298*7836SJohn.Forte@Sun.COM 		const IMA_STATIC_DISCOVERY_TARGET staticConfig,
1299*7836SJohn.Forte@Sun.COM 		IMA_OID *pTargetOid
1300*7836SJohn.Forte@Sun.COM )
1301*7836SJohn.Forte@Sun.COM {
1302*7836SJohn.Forte@Sun.COM 	char			tmp_target_str[SUN_IMA_IP_ADDRESS_LEN];
1303*7836SJohn.Forte@Sun.COM 	char			target_addr_str[SUN_IMA_IP_ADDRESS_LEN];
1304*7836SJohn.Forte@Sun.COM 	char			target_port_str[SUN_IMA_IP_PORT_LEN];
1305*7836SJohn.Forte@Sun.COM 	iscsi_target_entry_t	target;
1306*7836SJohn.Forte@Sun.COM 	int			fd;
1307*7836SJohn.Forte@Sun.COM 	int			target_in_addr_size;
1308*7836SJohn.Forte@Sun.COM 	int			target_port;
1309*7836SJohn.Forte@Sun.COM 	union {
1310*7836SJohn.Forte@Sun.COM 		struct in_addr	u_in4;
1311*7836SJohn.Forte@Sun.COM 		struct in6_addr	u_in6;
1312*7836SJohn.Forte@Sun.COM 	}			target_in;
1313*7836SJohn.Forte@Sun.COM 
1314*7836SJohn.Forte@Sun.COM 	/*
1315*7836SJohn.Forte@Sun.COM 	 * staticConfig.address may come in with port number at its trailer.
1316*7836SJohn.Forte@Sun.COM 	 * Parse it to separate the IP address and port number.
1317*7836SJohn.Forte@Sun.COM 	 * Also translate the hostname to IP address if needed.
1318*7836SJohn.Forte@Sun.COM 	 */
1319*7836SJohn.Forte@Sun.COM 	(void) wcstombs(tmp_target_str,
1320*7836SJohn.Forte@Sun.COM 	    staticConfig.targetAddress.hostnameIpAddress.
1321*7836SJohn.Forte@Sun.COM 	    id.hostname, sizeof (tmp_target_str));
1322*7836SJohn.Forte@Sun.COM 
1323*7836SJohn.Forte@Sun.COM 	if (tmp_target_str[0] == '[') {
1324*7836SJohn.Forte@Sun.COM 		/* IPv6 address */
1325*7836SJohn.Forte@Sun.COM 		char *closeBracketPos;
1326*7836SJohn.Forte@Sun.COM 		closeBracketPos = strchr(tmp_target_str, ']');
1327*7836SJohn.Forte@Sun.COM 		if (!closeBracketPos) {
1328*7836SJohn.Forte@Sun.COM 			return (IMA_ERROR_INVALID_PARAMETER);
1329*7836SJohn.Forte@Sun.COM 		}
1330*7836SJohn.Forte@Sun.COM 
1331*7836SJohn.Forte@Sun.COM 		*closeBracketPos = NULL;
1332*7836SJohn.Forte@Sun.COM 		(void) strlcpy(target_addr_str, &tmp_target_str[1],
1333*7836SJohn.Forte@Sun.COM 		    sizeof (target_addr_str));
1334*7836SJohn.Forte@Sun.COM 
1335*7836SJohn.Forte@Sun.COM 		if (inet_pton(AF_INET6, target_addr_str,
1336*7836SJohn.Forte@Sun.COM 		    &target_in.u_in6) != 1) {
1337*7836SJohn.Forte@Sun.COM 			return (IMA_ERROR_INVALID_PARAMETER);
1338*7836SJohn.Forte@Sun.COM 		}
1339*7836SJohn.Forte@Sun.COM 		target_in_addr_size = sizeof (struct in6_addr);
1340*7836SJohn.Forte@Sun.COM 
1341*7836SJohn.Forte@Sun.COM 		/* Extract the port number */
1342*7836SJohn.Forte@Sun.COM 		closeBracketPos++;
1343*7836SJohn.Forte@Sun.COM 		if (*closeBracketPos == ':') {
1344*7836SJohn.Forte@Sun.COM 			closeBracketPos++;
1345*7836SJohn.Forte@Sun.COM 
1346*7836SJohn.Forte@Sun.COM 			if (*closeBracketPos != NULL) {
1347*7836SJohn.Forte@Sun.COM 				(void) strlcpy(target_port_str, closeBracketPos,
1348*7836SJohn.Forte@Sun.COM 				    sizeof (target_port_str));
1349*7836SJohn.Forte@Sun.COM 				target_port = atoi(target_port_str);
1350*7836SJohn.Forte@Sun.COM 			} else {
1351*7836SJohn.Forte@Sun.COM 				target_port = ISCSI_LISTEN_PORT;
1352*7836SJohn.Forte@Sun.COM 			}
1353*7836SJohn.Forte@Sun.COM 		} else {
1354*7836SJohn.Forte@Sun.COM 			/* No port number specified; use default port */
1355*7836SJohn.Forte@Sun.COM 			target_port = ISCSI_LISTEN_PORT;
1356*7836SJohn.Forte@Sun.COM 		}
1357*7836SJohn.Forte@Sun.COM 	} else {
1358*7836SJohn.Forte@Sun.COM 		/* IPv4 address */
1359*7836SJohn.Forte@Sun.COM 		char *colonPos;
1360*7836SJohn.Forte@Sun.COM 		colonPos = strchr(tmp_target_str, ':');
1361*7836SJohn.Forte@Sun.COM 		if (!colonPos) {
1362*7836SJohn.Forte@Sun.COM 			/* No port number specified; use default port */
1363*7836SJohn.Forte@Sun.COM 			target_port = ISCSI_LISTEN_PORT;
1364*7836SJohn.Forte@Sun.COM 			(void) strlcpy(target_addr_str, tmp_target_str,
1365*7836SJohn.Forte@Sun.COM 			    sizeof (target_addr_str));
1366*7836SJohn.Forte@Sun.COM 		} else {
1367*7836SJohn.Forte@Sun.COM 			*colonPos = NULL;
1368*7836SJohn.Forte@Sun.COM 			(void) strlcpy(target_addr_str, tmp_target_str,
1369*7836SJohn.Forte@Sun.COM 			    sizeof (target_addr_str));
1370*7836SJohn.Forte@Sun.COM 			/* Extract the port number */
1371*7836SJohn.Forte@Sun.COM 			colonPos++;
1372*7836SJohn.Forte@Sun.COM 			if (*colonPos != NULL) {
1373*7836SJohn.Forte@Sun.COM 				(void) strlcpy(target_port_str, colonPos,
1374*7836SJohn.Forte@Sun.COM 				    sizeof (target_port_str));
1375*7836SJohn.Forte@Sun.COM 				target_port = atoi(target_port_str);
1376*7836SJohn.Forte@Sun.COM 			} else {
1377*7836SJohn.Forte@Sun.COM 				target_port = ISCSI_LISTEN_PORT;
1378*7836SJohn.Forte@Sun.COM 			}
1379*7836SJohn.Forte@Sun.COM 		}
1380*7836SJohn.Forte@Sun.COM 
1381*7836SJohn.Forte@Sun.COM 		if (inet_pton(AF_INET, target_addr_str,
1382*7836SJohn.Forte@Sun.COM 		    &target_in.u_in4) != 1) {
1383*7836SJohn.Forte@Sun.COM 			return (IMA_ERROR_INVALID_PARAMETER);
1384*7836SJohn.Forte@Sun.COM 		}
1385*7836SJohn.Forte@Sun.COM 
1386*7836SJohn.Forte@Sun.COM 		target_in_addr_size = sizeof (struct in_addr);
1387*7836SJohn.Forte@Sun.COM 	}
1388*7836SJohn.Forte@Sun.COM 
1389*7836SJohn.Forte@Sun.COM 
1390*7836SJohn.Forte@Sun.COM 	(void) memset(&target, 0, sizeof (iscsi_target_entry_t));
1391*7836SJohn.Forte@Sun.COM 	target.te_entry.e_vers = ISCSI_INTERFACE_VERSION;
1392*7836SJohn.Forte@Sun.COM 	target.te_entry.e_oid = ISCSI_OID_NOTSET;
1393*7836SJohn.Forte@Sun.COM 	target.te_entry.e_tpgt = ISCSI_DEFAULT_TPGT;
1394*7836SJohn.Forte@Sun.COM 
1395*7836SJohn.Forte@Sun.COM 	(void) wcstombs((char *)target.te_name, staticConfig.targetName,
1396*7836SJohn.Forte@Sun.COM 	    ISCSI_MAX_NAME_LEN);
1397*7836SJohn.Forte@Sun.COM 
1398*7836SJohn.Forte@Sun.COM 	target.te_entry.e_insize = target_in_addr_size;
1399*7836SJohn.Forte@Sun.COM 	if (target.te_entry.e_insize == sizeof (struct in_addr)) {
1400*7836SJohn.Forte@Sun.COM 		target.te_entry.e_u.u_in4.s_addr = target_in.u_in4.s_addr;
1401*7836SJohn.Forte@Sun.COM 	} else if (target.te_entry.e_insize == sizeof (struct in6_addr)) {
1402*7836SJohn.Forte@Sun.COM 		bcopy(target_in.u_in6.s6_addr,
1403*7836SJohn.Forte@Sun.COM 		    target.te_entry.e_u.u_in6.s6_addr,
1404*7836SJohn.Forte@Sun.COM 		    sizeof (struct in6_addr));
1405*7836SJohn.Forte@Sun.COM 	} else {
1406*7836SJohn.Forte@Sun.COM 		/* Should not happen */
1407*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1408*7836SJohn.Forte@Sun.COM 		    "ISCSI_STATIC_GET returned bad address");
1409*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1410*7836SJohn.Forte@Sun.COM 	}
1411*7836SJohn.Forte@Sun.COM 
1412*7836SJohn.Forte@Sun.COM 	target.te_entry.e_port = target_port;
1413*7836SJohn.Forte@Sun.COM 
1414*7836SJohn.Forte@Sun.COM 	/* No target portal group specified. Default to -1. */
1415*7836SJohn.Forte@Sun.COM 	target.te_entry.e_tpgt = ISCSI_DEFAULT_TPGT;
1416*7836SJohn.Forte@Sun.COM 
1417*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
1418*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
1419*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
1420*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1421*7836SJohn.Forte@Sun.COM 	}
1422*7836SJohn.Forte@Sun.COM 
1423*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_STATIC_SET, &target)) {
1424*7836SJohn.Forte@Sun.COM 		/*
1425*7836SJohn.Forte@Sun.COM 		 * Encountered problem setting the IP address and port for
1426*7836SJohn.Forte@Sun.COM 		 * the target just added.
1427*7836SJohn.Forte@Sun.COM 		 */
1428*7836SJohn.Forte@Sun.COM 		(void) close(fd);
1429*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1430*7836SJohn.Forte@Sun.COM 		    "ISCSI_STATIC_SET ioctl failed, errno: %d", errno);
1431*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1432*7836SJohn.Forte@Sun.COM 	}
1433*7836SJohn.Forte@Sun.COM 
1434*7836SJohn.Forte@Sun.COM 	pTargetOid->objectType = IMA_OBJECT_TYPE_TARGET;
1435*7836SJohn.Forte@Sun.COM 	pTargetOid->ownerId = pluginOwnerId;
1436*7836SJohn.Forte@Sun.COM 	pTargetOid->objectSequenceNumber = target.te_entry.e_oid;
1437*7836SJohn.Forte@Sun.COM 
1438*7836SJohn.Forte@Sun.COM 	(void) close(fd);
1439*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
1440*7836SJohn.Forte@Sun.COM }
1441*7836SJohn.Forte@Sun.COM 
1442*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetTargetProperties(
1443*7836SJohn.Forte@Sun.COM 		IMA_OID targetId,
1444*7836SJohn.Forte@Sun.COM 		IMA_TARGET_PROPERTIES *pProps
1445*7836SJohn.Forte@Sun.COM )
1446*7836SJohn.Forte@Sun.COM {
1447*7836SJohn.Forte@Sun.COM 	return (getTargetProperties(targetId, pProps));
1448*7836SJohn.Forte@Sun.COM }
1449*7836SJohn.Forte@Sun.COM 
1450*7836SJohn.Forte@Sun.COM static IMA_STATUS getTargetProperties(
1451*7836SJohn.Forte@Sun.COM 		IMA_OID targetId,
1452*7836SJohn.Forte@Sun.COM 		IMA_TARGET_PROPERTIES *pProps
1453*7836SJohn.Forte@Sun.COM )
1454*7836SJohn.Forte@Sun.COM {
1455*7836SJohn.Forte@Sun.COM 	int		    fd;
1456*7836SJohn.Forte@Sun.COM 	iscsi_property_t    prop;
1457*7836SJohn.Forte@Sun.COM 
1458*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
1459*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
1460*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
1461*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1462*7836SJohn.Forte@Sun.COM 	}
1463*7836SJohn.Forte@Sun.COM 
1464*7836SJohn.Forte@Sun.COM 	(void) memset(&prop, 0, sizeof (iscsi_property_t));
1465*7836SJohn.Forte@Sun.COM 	prop.p_vers = ISCSI_INTERFACE_VERSION;
1466*7836SJohn.Forte@Sun.COM 	prop.p_oid = (uint32_t)targetId.objectSequenceNumber;
1467*7836SJohn.Forte@Sun.COM 
1468*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_TARGET_PROPS_GET, &prop) != 0) {
1469*7836SJohn.Forte@Sun.COM 		(void) close(fd);
1470*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1471*7836SJohn.Forte@Sun.COM 		    "ISCSI_TARGET_PROPS_GET ioctl failed, errno: %d", errno);
1472*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1473*7836SJohn.Forte@Sun.COM 	}
1474*7836SJohn.Forte@Sun.COM 
1475*7836SJohn.Forte@Sun.COM 	(void) mbstowcs(pProps->name, (char *)prop.p_name, IMA_NODE_NAME_LEN);
1476*7836SJohn.Forte@Sun.COM 	(void) memset(pProps->alias, 0,
1477*7836SJohn.Forte@Sun.COM 	    sizeof (IMA_WCHAR) * IMA_NODE_ALIAS_LEN);
1478*7836SJohn.Forte@Sun.COM 	if (prop.p_alias_len > 0) {
1479*7836SJohn.Forte@Sun.COM 		(void) mbstowcs(pProps->alias, (char *)prop.p_alias,
1480*7836SJohn.Forte@Sun.COM 		    IMA_NODE_ALIAS_LEN);
1481*7836SJohn.Forte@Sun.COM 	}
1482*7836SJohn.Forte@Sun.COM 
1483*7836SJohn.Forte@Sun.COM 	/* Initialize the discovery method to unknown method. */
1484*7836SJohn.Forte@Sun.COM 	pProps->discoveryMethodFlags = IMA_TARGET_DISCOVERY_METHOD_UNKNOWN;
1485*7836SJohn.Forte@Sun.COM 	if (!((prop.p_discovery & iSCSIDiscoveryMethodStatic) ^
1486*7836SJohn.Forte@Sun.COM 	    iSCSIDiscoveryMethodStatic)) {
1487*7836SJohn.Forte@Sun.COM 		pProps->discoveryMethodFlags |=
1488*7836SJohn.Forte@Sun.COM 		    IMA_TARGET_DISCOVERY_METHOD_STATIC;
1489*7836SJohn.Forte@Sun.COM 	}
1490*7836SJohn.Forte@Sun.COM 
1491*7836SJohn.Forte@Sun.COM 	if (!((prop.p_discovery & iSCSIDiscoveryMethodSLP) ^
1492*7836SJohn.Forte@Sun.COM 	    iSCSIDiscoveryMethodSLP)) {
1493*7836SJohn.Forte@Sun.COM 		pProps->discoveryMethodFlags |=	IMA_TARGET_DISCOVERY_METHOD_SLP;
1494*7836SJohn.Forte@Sun.COM 	}
1495*7836SJohn.Forte@Sun.COM 
1496*7836SJohn.Forte@Sun.COM 	if (!((prop.p_discovery & iSCSIDiscoveryMethodISNS) ^
1497*7836SJohn.Forte@Sun.COM 	    iSCSIDiscoveryMethodISNS)) {
1498*7836SJohn.Forte@Sun.COM 		pProps->discoveryMethodFlags |=	iSCSIDiscoveryMethodISNS;
1499*7836SJohn.Forte@Sun.COM 	}
1500*7836SJohn.Forte@Sun.COM 
1501*7836SJohn.Forte@Sun.COM 	if (!((prop.p_discovery & iSCSIDiscoveryMethodSendTargets) ^
1502*7836SJohn.Forte@Sun.COM 	    iSCSIDiscoveryMethodSendTargets)) {
1503*7836SJohn.Forte@Sun.COM 		pProps->discoveryMethodFlags |= iSCSIDiscoveryMethodSendTargets;
1504*7836SJohn.Forte@Sun.COM 	}
1505*7836SJohn.Forte@Sun.COM 
1506*7836SJohn.Forte@Sun.COM 	(void) close(fd);
1507*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
1508*7836SJohn.Forte@Sun.COM }
1509*7836SJohn.Forte@Sun.COM 
1510*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
1511*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetTargetErrorStatistics(
1512*7836SJohn.Forte@Sun.COM 		IMA_OID targetId,
1513*7836SJohn.Forte@Sun.COM 		IMA_TARGET_ERROR_STATISTICS *pStats
1514*7836SJohn.Forte@Sun.COM )
1515*7836SJohn.Forte@Sun.COM {
1516*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_NOT_SUPPORTED);
1517*7836SJohn.Forte@Sun.COM }
1518*7836SJohn.Forte@Sun.COM 
1519*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetLuOidList(
1520*7836SJohn.Forte@Sun.COM 		IMA_OID oid,
1521*7836SJohn.Forte@Sun.COM 		IMA_OID_LIST **ppList
1522*7836SJohn.Forte@Sun.COM )
1523*7836SJohn.Forte@Sun.COM {
1524*7836SJohn.Forte@Sun.COM 	IMA_STATUS		status;
1525*7836SJohn.Forte@Sun.COM 	int			i;
1526*7836SJohn.Forte@Sun.COM 	iscsi_lun_list_t	*pLunList;
1527*7836SJohn.Forte@Sun.COM 
1528*7836SJohn.Forte@Sun.COM 	if (oid.objectType == IMA_OBJECT_TYPE_LHBA) {
1529*7836SJohn.Forte@Sun.COM 		status = get_target_lun_oid_list(NULL, &pLunList);
1530*7836SJohn.Forte@Sun.COM 	} else {
1531*7836SJohn.Forte@Sun.COM 		status = get_target_lun_oid_list(&oid, &pLunList);
1532*7836SJohn.Forte@Sun.COM 	}
1533*7836SJohn.Forte@Sun.COM 
1534*7836SJohn.Forte@Sun.COM 	if (!IMA_SUCCESS(status)) {
1535*7836SJohn.Forte@Sun.COM 		return (status);
1536*7836SJohn.Forte@Sun.COM 	}
1537*7836SJohn.Forte@Sun.COM 
1538*7836SJohn.Forte@Sun.COM 	*ppList = (IMA_OID_LIST *) calloc(1, (sizeof (IMA_OID_LIST) +
1539*7836SJohn.Forte@Sun.COM 	    (pLunList->ll_out_cnt * sizeof (IMA_OID))));
1540*7836SJohn.Forte@Sun.COM 	if (*ppList == NULL) {
1541*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
1542*7836SJohn.Forte@Sun.COM 	}
1543*7836SJohn.Forte@Sun.COM 	(*ppList)->oidCount = pLunList->ll_out_cnt;
1544*7836SJohn.Forte@Sun.COM 	for (i = 0; i < pLunList->ll_out_cnt; i++) {
1545*7836SJohn.Forte@Sun.COM 		(*ppList)->oids[i].objectType = IMA_OBJECT_TYPE_LU;
1546*7836SJohn.Forte@Sun.COM 		(*ppList)->oids[i].ownerId = pluginOwnerId;
1547*7836SJohn.Forte@Sun.COM 		(*ppList)->oids[i].objectSequenceNumber =
1548*7836SJohn.Forte@Sun.COM 		    pLunList->ll_luns[i].l_oid;
1549*7836SJohn.Forte@Sun.COM 	}
1550*7836SJohn.Forte@Sun.COM 
1551*7836SJohn.Forte@Sun.COM 	free(pLunList);
1552*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
1553*7836SJohn.Forte@Sun.COM }
1554*7836SJohn.Forte@Sun.COM 
1555*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetLuOid(
1556*7836SJohn.Forte@Sun.COM 		IMA_OID targetId,
1557*7836SJohn.Forte@Sun.COM 		IMA_UINT64 lun,
1558*7836SJohn.Forte@Sun.COM 		IMA_OID *pluId
1559*7836SJohn.Forte@Sun.COM )
1560*7836SJohn.Forte@Sun.COM {
1561*7836SJohn.Forte@Sun.COM 	IMA_STATUS		status;
1562*7836SJohn.Forte@Sun.COM 	int			i;
1563*7836SJohn.Forte@Sun.COM 	iscsi_lun_list_t	*pLunList;
1564*7836SJohn.Forte@Sun.COM 
1565*7836SJohn.Forte@Sun.COM 	status = get_target_lun_oid_list(&targetId, &pLunList);
1566*7836SJohn.Forte@Sun.COM 	if (!IMA_SUCCESS(status)) {
1567*7836SJohn.Forte@Sun.COM 		return (status);
1568*7836SJohn.Forte@Sun.COM 	}
1569*7836SJohn.Forte@Sun.COM 
1570*7836SJohn.Forte@Sun.COM 	for (i = 0; i < pLunList->ll_out_cnt; i++) {
1571*7836SJohn.Forte@Sun.COM 		if (pLunList->ll_luns[i].l_num == lun) {
1572*7836SJohn.Forte@Sun.COM 			pluId->objectType = IMA_OBJECT_TYPE_LU;
1573*7836SJohn.Forte@Sun.COM 			pluId->ownerId = pluginOwnerId;
1574*7836SJohn.Forte@Sun.COM 			pluId->objectSequenceNumber =
1575*7836SJohn.Forte@Sun.COM 			    pLunList->ll_luns[i].l_oid;
1576*7836SJohn.Forte@Sun.COM 			free(pLunList);
1577*7836SJohn.Forte@Sun.COM 			return (IMA_STATUS_SUCCESS);
1578*7836SJohn.Forte@Sun.COM 		}
1579*7836SJohn.Forte@Sun.COM 	}
1580*7836SJohn.Forte@Sun.COM 
1581*7836SJohn.Forte@Sun.COM 	free(pLunList);
1582*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
1583*7836SJohn.Forte@Sun.COM }
1584*7836SJohn.Forte@Sun.COM 
1585*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetLuProperties(
1586*7836SJohn.Forte@Sun.COM 		IMA_OID luId,
1587*7836SJohn.Forte@Sun.COM 		IMA_LU_PROPERTIES *pProps
1588*7836SJohn.Forte@Sun.COM )
1589*7836SJohn.Forte@Sun.COM {
1590*7836SJohn.Forte@Sun.COM 	return (getLuProperties(luId, pProps));
1591*7836SJohn.Forte@Sun.COM }
1592*7836SJohn.Forte@Sun.COM 
1593*7836SJohn.Forte@Sun.COM static IMA_STATUS getLuProperties(
1594*7836SJohn.Forte@Sun.COM 		IMA_OID luId,
1595*7836SJohn.Forte@Sun.COM 		IMA_LU_PROPERTIES *pProps
1596*7836SJohn.Forte@Sun.COM )
1597*7836SJohn.Forte@Sun.COM {
1598*7836SJohn.Forte@Sun.COM 	IMA_STATUS		status;
1599*7836SJohn.Forte@Sun.COM 	iscsi_lun_list_t	*pLunList;
1600*7836SJohn.Forte@Sun.COM 	int			j;
1601*7836SJohn.Forte@Sun.COM 	IMA_BOOL		lunMatch = IMA_FALSE;
1602*7836SJohn.Forte@Sun.COM 	int			fd;
1603*7836SJohn.Forte@Sun.COM 	iscsi_lun_props_t	lun;
1604*7836SJohn.Forte@Sun.COM 	di_devlink_handle_t	hdl;
1605*7836SJohn.Forte@Sun.COM 
1606*7836SJohn.Forte@Sun.COM 	if (luId.objectType != IMA_OBJECT_TYPE_LU) {
1607*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INCORRECT_OBJECT_TYPE);
1608*7836SJohn.Forte@Sun.COM 	}
1609*7836SJohn.Forte@Sun.COM 
1610*7836SJohn.Forte@Sun.COM 	/*
1611*7836SJohn.Forte@Sun.COM 	 * get list of lun oids for all targets
1612*7836SJohn.Forte@Sun.COM 	 */
1613*7836SJohn.Forte@Sun.COM 	status = get_target_lun_oid_list(NULL, &pLunList);
1614*7836SJohn.Forte@Sun.COM 	if (!IMA_SUCCESS(status)) {
1615*7836SJohn.Forte@Sun.COM 		return (status);
1616*7836SJohn.Forte@Sun.COM 	}
1617*7836SJohn.Forte@Sun.COM 	for (j = 0; j < pLunList->ll_out_cnt; j++) {
1618*7836SJohn.Forte@Sun.COM 		/*
1619*7836SJohn.Forte@Sun.COM 		 * for each lun, check if match is found
1620*7836SJohn.Forte@Sun.COM 		 */
1621*7836SJohn.Forte@Sun.COM 		if (pLunList->ll_luns[j].l_oid == luId.objectSequenceNumber) {
1622*7836SJohn.Forte@Sun.COM 			/*
1623*7836SJohn.Forte@Sun.COM 			 * match found, break out of lun loop
1624*7836SJohn.Forte@Sun.COM 			 */
1625*7836SJohn.Forte@Sun.COM 			lunMatch = IMA_TRUE;
1626*7836SJohn.Forte@Sun.COM 			break;
1627*7836SJohn.Forte@Sun.COM 		}
1628*7836SJohn.Forte@Sun.COM 	}
1629*7836SJohn.Forte@Sun.COM 
1630*7836SJohn.Forte@Sun.COM 	if (lunMatch == IMA_TRUE) {
1631*7836SJohn.Forte@Sun.COM 		(void) memset(&lun, 0, sizeof (iscsi_lun_props_t));
1632*7836SJohn.Forte@Sun.COM 		lun.lp_vers = ISCSI_INTERFACE_VERSION;
1633*7836SJohn.Forte@Sun.COM 		lun.lp_tgt_oid = pLunList->ll_luns[j].l_tgt_oid;
1634*7836SJohn.Forte@Sun.COM 		lun.lp_oid = pLunList->ll_luns[j].l_oid;
1635*7836SJohn.Forte@Sun.COM 	}
1636*7836SJohn.Forte@Sun.COM 
1637*7836SJohn.Forte@Sun.COM 	free(pLunList);
1638*7836SJohn.Forte@Sun.COM 
1639*7836SJohn.Forte@Sun.COM 	if (lunMatch == IMA_FALSE) {
1640*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_OBJECT_NOT_FOUND);
1641*7836SJohn.Forte@Sun.COM 	}
1642*7836SJohn.Forte@Sun.COM 
1643*7836SJohn.Forte@Sun.COM 	/*
1644*7836SJohn.Forte@Sun.COM 	 * get lun properties
1645*7836SJohn.Forte@Sun.COM 	 */
1646*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
1647*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
1648*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
1649*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1650*7836SJohn.Forte@Sun.COM 	}
1651*7836SJohn.Forte@Sun.COM 
1652*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_LUN_PROPS_GET, &lun)) {
1653*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1654*7836SJohn.Forte@Sun.COM 		    "ISCSI_LUN_PROPS_GET ioctl failed, errno: %d", errno);
1655*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1656*7836SJohn.Forte@Sun.COM 	}
1657*7836SJohn.Forte@Sun.COM 	(void) close(fd);
1658*7836SJohn.Forte@Sun.COM 
1659*7836SJohn.Forte@Sun.COM 	/*
1660*7836SJohn.Forte@Sun.COM 	 * set property values
1661*7836SJohn.Forte@Sun.COM 	 */
1662*7836SJohn.Forte@Sun.COM 	pProps->associatedTargetOid.objectType = IMA_OBJECT_TYPE_TARGET;
1663*7836SJohn.Forte@Sun.COM 	pProps->associatedTargetOid.ownerId = pluginOwnerId;
1664*7836SJohn.Forte@Sun.COM 	pProps->associatedTargetOid.objectSequenceNumber = lun.lp_tgt_oid;
1665*7836SJohn.Forte@Sun.COM 	pProps->targetLun = (IMA_UINT64)lun.lp_num;
1666*7836SJohn.Forte@Sun.COM 	pProps->exposedToOs = IMA_TRUE;
1667*7836SJohn.Forte@Sun.COM 	(void) memset(&pProps->timeExposedToOs, 0,
1668*7836SJohn.Forte@Sun.COM 	    sizeof (pProps->timeExposedToOs));
1669*7836SJohn.Forte@Sun.COM 
1670*7836SJohn.Forte@Sun.COM 	if (lun.lp_status == LunValid) {
1671*7836SJohn.Forte@Sun.COM 
1672*7836SJohn.Forte@Sun.COM 		/* add minor device delimiter */
1673*7836SJohn.Forte@Sun.COM 		(void) strcat(lun.lp_pathname, ":");
1674*7836SJohn.Forte@Sun.COM 
1675*7836SJohn.Forte@Sun.COM 		if ((strstr(lun.lp_pathname, "sd@") != NULL) ||
1676*7836SJohn.Forte@Sun.COM 		    (strstr(lun.lp_pathname, "ssd@") != NULL) ||
1677*7836SJohn.Forte@Sun.COM 		    (strstr(lun.lp_pathname, "disk@") != NULL)) {
1678*7836SJohn.Forte@Sun.COM 			/*
1679*7836SJohn.Forte@Sun.COM 			 * modify returned pathname to obtain the 2nd slice
1680*7836SJohn.Forte@Sun.COM 			 * of the raw disk
1681*7836SJohn.Forte@Sun.COM 			 */
1682*7836SJohn.Forte@Sun.COM 			(void) strcat(lun.lp_pathname, "c,raw");
1683*7836SJohn.Forte@Sun.COM 		}
1684*7836SJohn.Forte@Sun.COM 
1685*7836SJohn.Forte@Sun.COM 		/*
1686*7836SJohn.Forte@Sun.COM 		 * Pathname returned by driver is the physical device path.
1687*7836SJohn.Forte@Sun.COM 		 * This name needs to be converted to the OS device name.
1688*7836SJohn.Forte@Sun.COM 		 */
1689*7836SJohn.Forte@Sun.COM 		if (hdl = di_devlink_init(lun.lp_pathname, DI_MAKE_LINK)) {
1690*7836SJohn.Forte@Sun.COM 			pProps->osDeviceName[0] = L'\0';
1691*7836SJohn.Forte@Sun.COM 			(void) di_devlink_walk(hdl, NULL, lun.lp_pathname,
1692*7836SJohn.Forte@Sun.COM 			    DI_PRIMARY_LINK, (void *)pProps->osDeviceName,
1693*7836SJohn.Forte@Sun.COM 			    get_lun_devlink);
1694*7836SJohn.Forte@Sun.COM 			if (pProps->osDeviceName[0] != L'\0') {
1695*7836SJohn.Forte@Sun.COM 				/* OS device name synchronously made */
1696*7836SJohn.Forte@Sun.COM 				pProps->osDeviceNameValid = IMA_TRUE;
1697*7836SJohn.Forte@Sun.COM 			} else {
1698*7836SJohn.Forte@Sun.COM 				pProps->osDeviceNameValid = IMA_FALSE;
1699*7836SJohn.Forte@Sun.COM 			}
1700*7836SJohn.Forte@Sun.COM 
1701*7836SJohn.Forte@Sun.COM 			(void) di_devlink_fini(&hdl);
1702*7836SJohn.Forte@Sun.COM 		} else {
1703*7836SJohn.Forte@Sun.COM 			pProps->osDeviceNameValid = IMA_FALSE;
1704*7836SJohn.Forte@Sun.COM 		}
1705*7836SJohn.Forte@Sun.COM 
1706*7836SJohn.Forte@Sun.COM 	} else {
1707*7836SJohn.Forte@Sun.COM 		pProps->osDeviceNameValid = IMA_FALSE;
1708*7836SJohn.Forte@Sun.COM 	}
1709*7836SJohn.Forte@Sun.COM 
1710*7836SJohn.Forte@Sun.COM 	pProps->osParallelIdsValid = IMA_FALSE;
1711*7836SJohn.Forte@Sun.COM 
1712*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
1713*7836SJohn.Forte@Sun.COM }
1714*7836SJohn.Forte@Sun.COM 
1715*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
1716*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetStatisticsProperties(
1717*7836SJohn.Forte@Sun.COM 		IMA_OID oid,
1718*7836SJohn.Forte@Sun.COM 		IMA_STATISTICS_PROPERTIES *pProps
1719*7836SJohn.Forte@Sun.COM )
1720*7836SJohn.Forte@Sun.COM {
1721*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_NOT_SUPPORTED);
1722*7836SJohn.Forte@Sun.COM }
1723*7836SJohn.Forte@Sun.COM 
1724*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
1725*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetDeviceStatistics(
1726*7836SJohn.Forte@Sun.COM 		IMA_OID luId,
1727*7836SJohn.Forte@Sun.COM 		IMA_DEVICE_STATISTICS *pStats
1728*7836SJohn.Forte@Sun.COM )
1729*7836SJohn.Forte@Sun.COM {
1730*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_NOT_SUPPORTED);
1731*7836SJohn.Forte@Sun.COM }
1732*7836SJohn.Forte@Sun.COM 
1733*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
1734*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_LuInquiry(
1735*7836SJohn.Forte@Sun.COM 	IMA_OID deviceId,
1736*7836SJohn.Forte@Sun.COM 	IMA_BOOL evpd,
1737*7836SJohn.Forte@Sun.COM 	IMA_BOOL cmddt,
1738*7836SJohn.Forte@Sun.COM 	IMA_BYTE pageCode,
1739*7836SJohn.Forte@Sun.COM 	IMA_BYTE *pOutputBuffer,
1740*7836SJohn.Forte@Sun.COM 	IMA_UINT *pOutputBufferLength,
1741*7836SJohn.Forte@Sun.COM 	IMA_BYTE *pSenseBuffer,
1742*7836SJohn.Forte@Sun.COM 	IMA_UINT *pSenseBufferLength
1743*7836SJohn.Forte@Sun.COM )
1744*7836SJohn.Forte@Sun.COM {
1745*7836SJohn.Forte@Sun.COM 	IMA_LU_PROPERTIES luProps;
1746*7836SJohn.Forte@Sun.COM 	IMA_STATUS status;
1747*7836SJohn.Forte@Sun.COM 
1748*7836SJohn.Forte@Sun.COM 	char cmdblk [ INQUIRY_CMDLEN ] =
1749*7836SJohn.Forte@Sun.COM 	    { INQUIRY_CMD,   /* command */
1750*7836SJohn.Forte@Sun.COM 			0,   /* lun/reserved */
1751*7836SJohn.Forte@Sun.COM 			0,   /* page code */
1752*7836SJohn.Forte@Sun.COM 			0,   /* reserved */
1753*7836SJohn.Forte@Sun.COM 	INQUIRY_REPLY_LEN,   /* allocation length */
1754*7836SJohn.Forte@Sun.COM 			0 }; /* reserved/flag/link */
1755*7836SJohn.Forte@Sun.COM 
1756*7836SJohn.Forte@Sun.COM 
1757*7836SJohn.Forte@Sun.COM 	int fd;
1758*7836SJohn.Forte@Sun.COM 	iscsi_uscsi_t uscsi;
1759*7836SJohn.Forte@Sun.COM 
1760*7836SJohn.Forte@Sun.COM 	cmdblk[2] = pageCode;
1761*7836SJohn.Forte@Sun.COM 
1762*7836SJohn.Forte@Sun.COM 	(void) memset(&uscsi, 0, sizeof (iscsi_uscsi_t));
1763*7836SJohn.Forte@Sun.COM 	uscsi.iu_vers 	= ISCSI_INTERFACE_VERSION;
1764*7836SJohn.Forte@Sun.COM 
1765*7836SJohn.Forte@Sun.COM 	/* iu_oid is a session oid in the driver */
1766*7836SJohn.Forte@Sun.COM 	if (deviceId.objectType == IMA_OBJECT_TYPE_TARGET) {
1767*7836SJohn.Forte@Sun.COM 		uscsi.iu_oid	= deviceId.objectSequenceNumber;
1768*7836SJohn.Forte@Sun.COM 		uscsi.iu_lun	= 0;
1769*7836SJohn.Forte@Sun.COM 	} else {
1770*7836SJohn.Forte@Sun.COM 		/*
1771*7836SJohn.Forte@Sun.COM 		 * Get LU properties and associated session oid
1772*7836SJohn.Forte@Sun.COM 		 * for this lun(deviceId) and put in uscsi.iu_oid
1773*7836SJohn.Forte@Sun.COM 		 */
1774*7836SJohn.Forte@Sun.COM 		status = getLuProperties(deviceId, &luProps);
1775*7836SJohn.Forte@Sun.COM 		if (status != IMA_STATUS_SUCCESS) {
1776*7836SJohn.Forte@Sun.COM 			return (status);
1777*7836SJohn.Forte@Sun.COM 		}
1778*7836SJohn.Forte@Sun.COM 		uscsi.iu_oid = (uint32_t)luProps.associatedTargetOid.
1779*7836SJohn.Forte@Sun.COM 		    objectSequenceNumber;
1780*7836SJohn.Forte@Sun.COM 		uscsi.iu_lun = luProps.targetLun;
1781*7836SJohn.Forte@Sun.COM 	}
1782*7836SJohn.Forte@Sun.COM 
1783*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_flags = USCSI_READ;
1784*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_timeout = USCSI_TIMEOUT_IN_SEC;
1785*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_bufaddr = (char *)pOutputBuffer;
1786*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_buflen = *pOutputBufferLength;
1787*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_rqbuf = (char *)pSenseBuffer;
1788*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_rqlen = *pSenseBufferLength;
1789*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_cdb = &cmdblk[0];
1790*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_cdblen = INQUIRY_CMDLEN;
1791*7836SJohn.Forte@Sun.COM 
1792*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
1793*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
1794*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
1795*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1796*7836SJohn.Forte@Sun.COM 	}
1797*7836SJohn.Forte@Sun.COM 
1798*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_USCSI, &uscsi) != 0) {
1799*7836SJohn.Forte@Sun.COM 		(void) close(fd);
1800*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1801*7836SJohn.Forte@Sun.COM 		    "ISCSI_TARGET_PROPS_GET ioctl failed, errno: %d", errno);
1802*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1803*7836SJohn.Forte@Sun.COM 	}
1804*7836SJohn.Forte@Sun.COM 
1805*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
1806*7836SJohn.Forte@Sun.COM }
1807*7836SJohn.Forte@Sun.COM 
1808*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
1809*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_LuReadCapacity(
1810*7836SJohn.Forte@Sun.COM 		IMA_OID deviceId,
1811*7836SJohn.Forte@Sun.COM 		IMA_UINT cdbLength,
1812*7836SJohn.Forte@Sun.COM 		IMA_BYTE *pOutputBuffer,
1813*7836SJohn.Forte@Sun.COM 		IMA_UINT *pOutputBufferLength,
1814*7836SJohn.Forte@Sun.COM 
1815*7836SJohn.Forte@Sun.COM 		IMA_BYTE *pSenseBuffer,
1816*7836SJohn.Forte@Sun.COM 		IMA_UINT *pSenseBufferLength
1817*7836SJohn.Forte@Sun.COM )
1818*7836SJohn.Forte@Sun.COM {
1819*7836SJohn.Forte@Sun.COM 	IMA_LU_PROPERTIES luProps;
1820*7836SJohn.Forte@Sun.COM 	IMA_STATUS status;
1821*7836SJohn.Forte@Sun.COM 	int fd;
1822*7836SJohn.Forte@Sun.COM 	iscsi_uscsi_t uscsi;
1823*7836SJohn.Forte@Sun.COM 
1824*7836SJohn.Forte@Sun.COM 	char cmdblk [ INQUIRY_CMDLEN ] =
1825*7836SJohn.Forte@Sun.COM 		{ GETCAPACITY_CMD,   /* command */
1826*7836SJohn.Forte@Sun.COM 				0,   /* lun/reserved */
1827*7836SJohn.Forte@Sun.COM 				0,   /* page code */
1828*7836SJohn.Forte@Sun.COM 				0,   /* reserved */
1829*7836SJohn.Forte@Sun.COM 		INQUIRY_REPLY_LEN,   /* allocation length */
1830*7836SJohn.Forte@Sun.COM 				0 }; /* reserved/flag/link */
1831*7836SJohn.Forte@Sun.COM 
1832*7836SJohn.Forte@Sun.COM 
1833*7836SJohn.Forte@Sun.COM 
1834*7836SJohn.Forte@Sun.COM 	(void) memset(&uscsi, 0, sizeof (iscsi_uscsi_t));
1835*7836SJohn.Forte@Sun.COM 	uscsi.iu_vers 	= ISCSI_INTERFACE_VERSION;
1836*7836SJohn.Forte@Sun.COM 
1837*7836SJohn.Forte@Sun.COM 	/* iu_oid is a session oid in the driver */
1838*7836SJohn.Forte@Sun.COM 	if (deviceId.objectType == IMA_OBJECT_TYPE_TARGET) {
1839*7836SJohn.Forte@Sun.COM 		uscsi.iu_oid	= deviceId.objectSequenceNumber;
1840*7836SJohn.Forte@Sun.COM 		uscsi.iu_lun	= 0;
1841*7836SJohn.Forte@Sun.COM 	} else {
1842*7836SJohn.Forte@Sun.COM 		/*
1843*7836SJohn.Forte@Sun.COM 		 * Get LU properties and associated session oid
1844*7836SJohn.Forte@Sun.COM 		 * for this lun(deviceId) and put in uscsi.iu_oid
1845*7836SJohn.Forte@Sun.COM 		 */
1846*7836SJohn.Forte@Sun.COM 		status = getLuProperties(deviceId, &luProps);
1847*7836SJohn.Forte@Sun.COM 		if (status != IMA_STATUS_SUCCESS) {
1848*7836SJohn.Forte@Sun.COM 			return (status);
1849*7836SJohn.Forte@Sun.COM 		}
1850*7836SJohn.Forte@Sun.COM 		uscsi.iu_oid = (uint32_t)luProps.associatedTargetOid.
1851*7836SJohn.Forte@Sun.COM 		    objectSequenceNumber;
1852*7836SJohn.Forte@Sun.COM 		uscsi.iu_lun = luProps.targetLun;
1853*7836SJohn.Forte@Sun.COM 	}
1854*7836SJohn.Forte@Sun.COM 
1855*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_flags = USCSI_READ;
1856*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_timeout = 10;
1857*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_bufaddr = (char *)pOutputBuffer;
1858*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_buflen = *pOutputBufferLength;
1859*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_rqbuf = (char *)pSenseBuffer;
1860*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_rqlen = *pSenseBufferLength;
1861*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_cdb = &cmdblk[0];
1862*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_cdblen = INQUIRY_CMDLEN;
1863*7836SJohn.Forte@Sun.COM 
1864*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
1865*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
1866*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
1867*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1868*7836SJohn.Forte@Sun.COM 	}
1869*7836SJohn.Forte@Sun.COM 
1870*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_USCSI, &uscsi) != 0) {
1871*7836SJohn.Forte@Sun.COM 		(void) close(fd);
1872*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1873*7836SJohn.Forte@Sun.COM 		    "ISCSI_TARGET_PROPS_GET ioctl failed, errno: %d", errno);
1874*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1875*7836SJohn.Forte@Sun.COM 	}
1876*7836SJohn.Forte@Sun.COM 
1877*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
1878*7836SJohn.Forte@Sun.COM }
1879*7836SJohn.Forte@Sun.COM 
1880*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
1881*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_LuReportLuns(
1882*7836SJohn.Forte@Sun.COM 		IMA_OID deviceId,
1883*7836SJohn.Forte@Sun.COM 		IMA_BOOL sendToWellKnownLun,
1884*7836SJohn.Forte@Sun.COM 		IMA_BYTE selectReport,
1885*7836SJohn.Forte@Sun.COM 
1886*7836SJohn.Forte@Sun.COM 		IMA_BYTE *pOutputBuffer,
1887*7836SJohn.Forte@Sun.COM 		IMA_UINT *pOutputBufferLength,
1888*7836SJohn.Forte@Sun.COM 
1889*7836SJohn.Forte@Sun.COM 		IMA_BYTE *pSenseBuffer,
1890*7836SJohn.Forte@Sun.COM 		IMA_UINT *pSenseBufferLength
1891*7836SJohn.Forte@Sun.COM )
1892*7836SJohn.Forte@Sun.COM {
1893*7836SJohn.Forte@Sun.COM 	IMA_LU_PROPERTIES luProps;
1894*7836SJohn.Forte@Sun.COM 	IMA_STATUS status;
1895*7836SJohn.Forte@Sun.COM 	int fd;
1896*7836SJohn.Forte@Sun.COM 	iscsi_uscsi_t uscsi;
1897*7836SJohn.Forte@Sun.COM 
1898*7836SJohn.Forte@Sun.COM 	char cmdblk [ INQUIRY_CMDLEN ] =
1899*7836SJohn.Forte@Sun.COM 	    { INQUIRY_CMD,   /* command */
1900*7836SJohn.Forte@Sun.COM 			0,   /* lun/reserved */
1901*7836SJohn.Forte@Sun.COM 			0,   /* page code */
1902*7836SJohn.Forte@Sun.COM 			0,   /* reserved */
1903*7836SJohn.Forte@Sun.COM 	INQUIRY_REPLY_LEN,   /* allocation length */
1904*7836SJohn.Forte@Sun.COM 			0 }; /* reserved/flag/link */
1905*7836SJohn.Forte@Sun.COM 
1906*7836SJohn.Forte@Sun.COM 	(void) memset(&uscsi, 0, sizeof (iscsi_uscsi_t));
1907*7836SJohn.Forte@Sun.COM 	uscsi.iu_vers 	= ISCSI_INTERFACE_VERSION;
1908*7836SJohn.Forte@Sun.COM 
1909*7836SJohn.Forte@Sun.COM 	/* iu_oid is a session oid in the driver */
1910*7836SJohn.Forte@Sun.COM 	if (deviceId.objectType == IMA_OBJECT_TYPE_TARGET) {
1911*7836SJohn.Forte@Sun.COM 		uscsi.iu_oid	= deviceId.objectSequenceNumber;
1912*7836SJohn.Forte@Sun.COM 		uscsi.iu_lun	= 0;
1913*7836SJohn.Forte@Sun.COM 	} else {
1914*7836SJohn.Forte@Sun.COM 		/*
1915*7836SJohn.Forte@Sun.COM 		 * Get LU properties and associated session oid
1916*7836SJohn.Forte@Sun.COM 		 * for this lun(deviceId) and put in uscsi.iu_oid
1917*7836SJohn.Forte@Sun.COM 		 */
1918*7836SJohn.Forte@Sun.COM 		status = getLuProperties(deviceId, &luProps);
1919*7836SJohn.Forte@Sun.COM 		if (status != IMA_STATUS_SUCCESS) {
1920*7836SJohn.Forte@Sun.COM 			return (status);
1921*7836SJohn.Forte@Sun.COM 		}
1922*7836SJohn.Forte@Sun.COM 		uscsi.iu_oid = (uint32_t)luProps.associatedTargetOid.
1923*7836SJohn.Forte@Sun.COM 		    objectSequenceNumber;
1924*7836SJohn.Forte@Sun.COM 		uscsi.iu_lun = luProps.targetLun;
1925*7836SJohn.Forte@Sun.COM 	}
1926*7836SJohn.Forte@Sun.COM 
1927*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_flags = USCSI_READ;
1928*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_timeout = 10;
1929*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_bufaddr = (char *)pOutputBuffer;
1930*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_buflen = *pOutputBufferLength;
1931*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_rqbuf = (char *)pSenseBuffer;
1932*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_rqlen = *pSenseBufferLength;
1933*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_cdb = &cmdblk[0];
1934*7836SJohn.Forte@Sun.COM 	uscsi.iu_ucmd.uscsi_cdblen = INQUIRY_CMDLEN;
1935*7836SJohn.Forte@Sun.COM 
1936*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
1937*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
1938*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
1939*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1940*7836SJohn.Forte@Sun.COM 	}
1941*7836SJohn.Forte@Sun.COM 
1942*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_USCSI, &uscsi) != 0) {
1943*7836SJohn.Forte@Sun.COM 		(void) close(fd);
1944*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
1945*7836SJohn.Forte@Sun.COM 		    "ISCSI_TARGET_PROPS_GET ioctl failed, errno: %d", errno);
1946*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
1947*7836SJohn.Forte@Sun.COM 	}
1948*7836SJohn.Forte@Sun.COM 
1949*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
1950*7836SJohn.Forte@Sun.COM }
1951*7836SJohn.Forte@Sun.COM 
1952*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
1953*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_ExposeLu(
1954*7836SJohn.Forte@Sun.COM 		IMA_OID luId
1955*7836SJohn.Forte@Sun.COM )
1956*7836SJohn.Forte@Sun.COM {
1957*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_NOT_SUPPORTED);
1958*7836SJohn.Forte@Sun.COM }
1959*7836SJohn.Forte@Sun.COM 
1960*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
1961*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_UnexposeLu(
1962*7836SJohn.Forte@Sun.COM 		IMA_OID luId
1963*7836SJohn.Forte@Sun.COM )
1964*7836SJohn.Forte@Sun.COM {
1965*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_NOT_SUPPORTED);
1966*7836SJohn.Forte@Sun.COM }
1967*7836SJohn.Forte@Sun.COM 
1968*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetAddressKeys(
1969*7836SJohn.Forte@Sun.COM 		IMA_OID targetOid,
1970*7836SJohn.Forte@Sun.COM 		IMA_ADDRESS_KEYS **ppKeys
1971*7836SJohn.Forte@Sun.COM )
1972*7836SJohn.Forte@Sun.COM {
1973*7836SJohn.Forte@Sun.COM 	IMA_STATUS status;
1974*7836SJohn.Forte@Sun.COM 	IMA_TARGET_PROPERTIES targetProps;
1975*7836SJohn.Forte@Sun.COM 	SUN_IMA_DISC_ADDR_PROP_LIST *discAddressList;
1976*7836SJohn.Forte@Sun.COM 	SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES *pList;
1977*7836SJohn.Forte@Sun.COM 	int i, j, addressKeyCount = 0;
1978*7836SJohn.Forte@Sun.COM 	int addressKeyIdx = 0;
1979*7836SJohn.Forte@Sun.COM 
1980*7836SJohn.Forte@Sun.COM 	status = getTargetProperties(targetOid, &targetProps);
1981*7836SJohn.Forte@Sun.COM 	if (status != IMA_STATUS_SUCCESS) {
1982*7836SJohn.Forte@Sun.COM 		return (status);
1983*7836SJohn.Forte@Sun.COM 	}
1984*7836SJohn.Forte@Sun.COM 
1985*7836SJohn.Forte@Sun.COM 	status = getDiscoveryAddressPropertiesList(&discAddressList);
1986*7836SJohn.Forte@Sun.COM 	if (status != IMA_STATUS_SUCCESS) {
1987*7836SJohn.Forte@Sun.COM 		return (status);
1988*7836SJohn.Forte@Sun.COM 	}
1989*7836SJohn.Forte@Sun.COM 
1990*7836SJohn.Forte@Sun.COM 	/* Get the number of addresses to allocate */
1991*7836SJohn.Forte@Sun.COM 	for (i = 0; i < discAddressList->discAddrCount; i++) {
1992*7836SJohn.Forte@Sun.COM 		(void) sendTargets(discAddressList->props[i].discoveryAddress,
1993*7836SJohn.Forte@Sun.COM 		    &pList);
1994*7836SJohn.Forte@Sun.COM 		for (j = 0; j < pList->keyCount; j++) {
1995*7836SJohn.Forte@Sun.COM 			if (wcsncmp(pList->keys[j].name, targetProps.name,
1996*7836SJohn.Forte@Sun.COM 			    wslen(pList->keys[j].name)) == 0) {
1997*7836SJohn.Forte@Sun.COM 				addressKeyCount++;
1998*7836SJohn.Forte@Sun.COM 			}
1999*7836SJohn.Forte@Sun.COM 		}
2000*7836SJohn.Forte@Sun.COM 		(void) IMA_FreeMemory(pList);
2001*7836SJohn.Forte@Sun.COM 	}
2002*7836SJohn.Forte@Sun.COM 
2003*7836SJohn.Forte@Sun.COM 	*ppKeys = (IMA_ADDRESS_KEYS *)calloc(1, sizeof (IMA_ADDRESS_KEYS) +
2004*7836SJohn.Forte@Sun.COM 	    addressKeyCount * sizeof (IMA_ADDRESS_KEY));
2005*7836SJohn.Forte@Sun.COM 	if (*ppKeys == NULL) {
2006*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2007*7836SJohn.Forte@Sun.COM 	}
2008*7836SJohn.Forte@Sun.COM 	(*ppKeys)->addressKeyCount = addressKeyCount;
2009*7836SJohn.Forte@Sun.COM 	addressKeyIdx = 0;
2010*7836SJohn.Forte@Sun.COM 
2011*7836SJohn.Forte@Sun.COM 	for (i = 0; i < discAddressList->discAddrCount; i++) {
2012*7836SJohn.Forte@Sun.COM 		(void) sendTargets(discAddressList->props[i].discoveryAddress,
2013*7836SJohn.Forte@Sun.COM 		    &pList);
2014*7836SJohn.Forte@Sun.COM 		for (j = 0; j < pList->keyCount; j++) {
2015*7836SJohn.Forte@Sun.COM 			if (wcsncmp(pList->keys[j].name, targetProps.name,
2016*7836SJohn.Forte@Sun.COM 			    wslen(pList->keys[j].name)) != 0) {
2017*7836SJohn.Forte@Sun.COM 				continue;
2018*7836SJohn.Forte@Sun.COM 			}
2019*7836SJohn.Forte@Sun.COM 
2020*7836SJohn.Forte@Sun.COM 			bcopy(&(pList->keys[j].address.ipAddress),
2021*7836SJohn.Forte@Sun.COM 			    &((*ppKeys)->addressKeys[addressKeyIdx].
2022*7836SJohn.Forte@Sun.COM 			    ipAddress), sizeof (IMA_IP_ADDRESS));
2023*7836SJohn.Forte@Sun.COM 
2024*7836SJohn.Forte@Sun.COM 			(*ppKeys)->addressKeys[addressKeyIdx++].portNumber =
2025*7836SJohn.Forte@Sun.COM 			    pList->keys[j].address.portNumber;
2026*7836SJohn.Forte@Sun.COM 
2027*7836SJohn.Forte@Sun.COM 		}
2028*7836SJohn.Forte@Sun.COM 		(void) IMA_FreeMemory(pList);
2029*7836SJohn.Forte@Sun.COM 	}
2030*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2031*7836SJohn.Forte@Sun.COM }
2032*7836SJohn.Forte@Sun.COM 
2033*7836SJohn.Forte@Sun.COM IMA_BOOL isAuthMethodValid(IMA_OID oid, IMA_AUTHMETHOD method) {
2034*7836SJohn.Forte@Sun.COM 	IMA_STATUS status;
2035*7836SJohn.Forte@Sun.COM 	IMA_AUTHMETHOD supportedList[MAX_AUTHMETHODS];
2036*7836SJohn.Forte@Sun.COM 	IMA_UINT i, supportedCount;
2037*7836SJohn.Forte@Sun.COM 	IMA_BOOL supported;
2038*7836SJohn.Forte@Sun.COM 	status = getSupportedAuthMethods(oid, IMA_FALSE, &supportedCount,
2039*7836SJohn.Forte@Sun.COM 			supportedList);
2040*7836SJohn.Forte@Sun.COM 	if (status != IMA_STATUS_SUCCESS)
2041*7836SJohn.Forte@Sun.COM 		return (IMA_FALSE);
2042*7836SJohn.Forte@Sun.COM 
2043*7836SJohn.Forte@Sun.COM 	supported = IMA_FALSE;
2044*7836SJohn.Forte@Sun.COM 	for (i = 0; i < supportedCount; i++) {
2045*7836SJohn.Forte@Sun.COM 		if (method == supportedList[i]) {
2046*7836SJohn.Forte@Sun.COM 			supported = IMA_TRUE;
2047*7836SJohn.Forte@Sun.COM 		}
2048*7836SJohn.Forte@Sun.COM 	}
2049*7836SJohn.Forte@Sun.COM 
2050*7836SJohn.Forte@Sun.COM 	return (supported);
2051*7836SJohn.Forte@Sun.COM }
2052*7836SJohn.Forte@Sun.COM 
2053*7836SJohn.Forte@Sun.COM IMA_BOOL isAuthMethodListValid(IMA_OID oid, const IMA_AUTHMETHOD *pMethodList,
2054*7836SJohn.Forte@Sun.COM 				IMA_UINT methodCount) {
2055*7836SJohn.Forte@Sun.COM 	IMA_UINT i, j;
2056*7836SJohn.Forte@Sun.COM 
2057*7836SJohn.Forte@Sun.COM 	if (pMethodList == NULL) {
2058*7836SJohn.Forte@Sun.COM 		return (IMA_FALSE);
2059*7836SJohn.Forte@Sun.COM 	}
2060*7836SJohn.Forte@Sun.COM 	/* Check list for duplicates */
2061*7836SJohn.Forte@Sun.COM 	for (i = 0; i < methodCount; i++) {
2062*7836SJohn.Forte@Sun.COM 		for (j = i + 1; j < methodCount; j++) {
2063*7836SJohn.Forte@Sun.COM 			if (pMethodList[i] == pMethodList[j]) {
2064*7836SJohn.Forte@Sun.COM 				return (IMA_FALSE);
2065*7836SJohn.Forte@Sun.COM 			}
2066*7836SJohn.Forte@Sun.COM 		}
2067*7836SJohn.Forte@Sun.COM 
2068*7836SJohn.Forte@Sun.COM 		if (isAuthMethodValid(oid, pMethodList[i]) == IMA_FALSE) {
2069*7836SJohn.Forte@Sun.COM 			return (IMA_FALSE);
2070*7836SJohn.Forte@Sun.COM 		}
2071*7836SJohn.Forte@Sun.COM 	}
2072*7836SJohn.Forte@Sun.COM 	return (IMA_TRUE);
2073*7836SJohn.Forte@Sun.COM }
2074*7836SJohn.Forte@Sun.COM 
2075*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetSupportedAuthMethods(
2076*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaOid,
2077*7836SJohn.Forte@Sun.COM 		IMA_BOOL getSettableMethods,
2078*7836SJohn.Forte@Sun.COM 		IMA_UINT *pMethodCount,
2079*7836SJohn.Forte@Sun.COM 		IMA_AUTHMETHOD *pMethodList
2080*7836SJohn.Forte@Sun.COM )
2081*7836SJohn.Forte@Sun.COM {
2082*7836SJohn.Forte@Sun.COM 	return (getSupportedAuthMethods(lhbaOid, getSettableMethods,
2083*7836SJohn.Forte@Sun.COM 	    pMethodCount, pMethodList));
2084*7836SJohn.Forte@Sun.COM }
2085*7836SJohn.Forte@Sun.COM 
2086*7836SJohn.Forte@Sun.COM 
2087*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
2088*7836SJohn.Forte@Sun.COM static IMA_STATUS getSupportedAuthMethods(
2089*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaOid,
2090*7836SJohn.Forte@Sun.COM 		IMA_BOOL getSettableMethods,
2091*7836SJohn.Forte@Sun.COM 		IMA_UINT *pMethodCount,
2092*7836SJohn.Forte@Sun.COM 		IMA_AUTHMETHOD *pMethodList
2093*7836SJohn.Forte@Sun.COM )
2094*7836SJohn.Forte@Sun.COM {
2095*7836SJohn.Forte@Sun.COM 	if (pMethodList == NULL) {
2096*7836SJohn.Forte@Sun.COM 		*pMethodCount = 0;
2097*7836SJohn.Forte@Sun.COM 		return (IMA_STATUS_SUCCESS);
2098*7836SJohn.Forte@Sun.COM 	}
2099*7836SJohn.Forte@Sun.COM 
2100*7836SJohn.Forte@Sun.COM 	*pMethodCount = NUM_SUPPORTED_AUTH_METHODS;
2101*7836SJohn.Forte@Sun.COM 	if (*pMethodCount > 1) {
2102*7836SJohn.Forte@Sun.COM 		pMethodList[0] = IMA_AUTHMETHOD_NONE;
2103*7836SJohn.Forte@Sun.COM 		pMethodList[1] = IMA_AUTHMETHOD_CHAP;
2104*7836SJohn.Forte@Sun.COM 	}
2105*7836SJohn.Forte@Sun.COM 
2106*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2107*7836SJohn.Forte@Sun.COM }
2108*7836SJohn.Forte@Sun.COM 
2109*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetInUseInitiatorAuthMethods(
2110*7836SJohn.Forte@Sun.COM 		IMA_OID		lhbaOid,
2111*7836SJohn.Forte@Sun.COM 		IMA_UINT	*pMethodCount,
2112*7836SJohn.Forte@Sun.COM 		IMA_AUTHMETHOD *pMethodList
2113*7836SJohn.Forte@Sun.COM )
2114*7836SJohn.Forte@Sun.COM {
2115*7836SJohn.Forte@Sun.COM 	return (getAuthMethods(lhbaOid, pMethodCount, pMethodList));
2116*7836SJohn.Forte@Sun.COM }
2117*7836SJohn.Forte@Sun.COM 
2118*7836SJohn.Forte@Sun.COM /*ARGSUSED*/
2119*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_GetInitiatorAuthParms(
2120*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaOid,
2121*7836SJohn.Forte@Sun.COM 		IMA_AUTHMETHOD method,
2122*7836SJohn.Forte@Sun.COM 		IMA_INITIATOR_AUTHPARMS *pParms
2123*7836SJohn.Forte@Sun.COM )
2124*7836SJohn.Forte@Sun.COM {
2125*7836SJohn.Forte@Sun.COM 	int fd;
2126*7836SJohn.Forte@Sun.COM 	iscsi_chap_props_t  chap_p;
2127*7836SJohn.Forte@Sun.COM 
2128*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
2129*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
2130*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
2131*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2132*7836SJohn.Forte@Sun.COM 	}
2133*7836SJohn.Forte@Sun.COM 
2134*7836SJohn.Forte@Sun.COM 	(void) memset(&chap_p, 0, sizeof (iscsi_chap_props_t));
2135*7836SJohn.Forte@Sun.COM 	chap_p.c_vers = ISCSI_INTERFACE_VERSION;
2136*7836SJohn.Forte@Sun.COM 	chap_p.c_oid = (uint32_t)lhbaOid.objectSequenceNumber;
2137*7836SJohn.Forte@Sun.COM 
2138*7836SJohn.Forte@Sun.COM 	if (method == IMA_AUTHMETHOD_CHAP) {
2139*7836SJohn.Forte@Sun.COM 		if (ioctl(fd, ISCSI_CHAP_GET, &chap_p) != 0) {
2140*7836SJohn.Forte@Sun.COM 			syslog(LOG_USER|LOG_DEBUG,
2141*7836SJohn.Forte@Sun.COM 			"ISCSI_CHAP_GET ioctl failed, errno: %d", errno);
2142*7836SJohn.Forte@Sun.COM 			(void) close(fd);
2143*7836SJohn.Forte@Sun.COM 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2144*7836SJohn.Forte@Sun.COM 		}
2145*7836SJohn.Forte@Sun.COM 	} else {
2146*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INVALID_PARAMETER);
2147*7836SJohn.Forte@Sun.COM 	}
2148*7836SJohn.Forte@Sun.COM 
2149*7836SJohn.Forte@Sun.COM 	(void) memcpy(pParms->chapParms.name, chap_p.c_user,
2150*7836SJohn.Forte@Sun.COM 	    chap_p.c_user_len);
2151*7836SJohn.Forte@Sun.COM 	pParms->chapParms.nameLength = chap_p.c_user_len;
2152*7836SJohn.Forte@Sun.COM 	(void) memcpy(pParms->chapParms.challengeSecret, chap_p.c_secret,
2153*7836SJohn.Forte@Sun.COM 	    chap_p.c_secret_len);
2154*7836SJohn.Forte@Sun.COM 	pParms->chapParms.challengeSecretLength = chap_p.c_secret_len;
2155*7836SJohn.Forte@Sun.COM 
2156*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2157*7836SJohn.Forte@Sun.COM }
2158*7836SJohn.Forte@Sun.COM 
2159*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetInitiatorAuthMethods(
2160*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaOid,
2161*7836SJohn.Forte@Sun.COM 		IMA_UINT methodCount,
2162*7836SJohn.Forte@Sun.COM 		const IMA_AUTHMETHOD *pMethodList
2163*7836SJohn.Forte@Sun.COM )
2164*7836SJohn.Forte@Sun.COM {
2165*7836SJohn.Forte@Sun.COM 	if (isAuthMethodListValid(lhbaOid, pMethodList,
2166*7836SJohn.Forte@Sun.COM 	    methodCount) == IMA_FALSE)
2167*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INVALID_PARAMETER);
2168*7836SJohn.Forte@Sun.COM 	return (setAuthMethods(lhbaOid, &methodCount, pMethodList));
2169*7836SJohn.Forte@Sun.COM }
2170*7836SJohn.Forte@Sun.COM 
2171*7836SJohn.Forte@Sun.COM /*
2172*7836SJohn.Forte@Sun.COM  * This function only sets CHAP params since we only support CHAP for now.
2173*7836SJohn.Forte@Sun.COM  */
2174*7836SJohn.Forte@Sun.COM IMA_API	IMA_STATUS IMA_SetInitiatorAuthParms(
2175*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaOid,
2176*7836SJohn.Forte@Sun.COM 		IMA_AUTHMETHOD method,
2177*7836SJohn.Forte@Sun.COM 		const IMA_INITIATOR_AUTHPARMS *pParms
2178*7836SJohn.Forte@Sun.COM )
2179*7836SJohn.Forte@Sun.COM {
2180*7836SJohn.Forte@Sun.COM 	int fd;
2181*7836SJohn.Forte@Sun.COM 	iscsi_chap_props_t  chap_p;
2182*7836SJohn.Forte@Sun.COM 
2183*7836SJohn.Forte@Sun.COM 	if (method != IMA_AUTHMETHOD_CHAP)
2184*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INVALID_PARAMETER);
2185*7836SJohn.Forte@Sun.COM 
2186*7836SJohn.Forte@Sun.COM 	if (isAuthMethodValid(lhbaOid, method) == IMA_FALSE) {
2187*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INVALID_PARAMETER);
2188*7836SJohn.Forte@Sun.COM 	}
2189*7836SJohn.Forte@Sun.COM 
2190*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
2191*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
2192*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
2193*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2194*7836SJohn.Forte@Sun.COM 	}
2195*7836SJohn.Forte@Sun.COM 
2196*7836SJohn.Forte@Sun.COM 	(void) memset(&chap_p, 0, sizeof (iscsi_chap_props_t));
2197*7836SJohn.Forte@Sun.COM 	chap_p.c_vers = ISCSI_INTERFACE_VERSION;
2198*7836SJohn.Forte@Sun.COM 	chap_p.c_oid = (uint32_t)lhbaOid.objectSequenceNumber;
2199*7836SJohn.Forte@Sun.COM 
2200*7836SJohn.Forte@Sun.COM 	chap_p.c_user_len = pParms->chapParms.nameLength;
2201*7836SJohn.Forte@Sun.COM 	(void) memcpy(chap_p.c_user, pParms->chapParms.name, chap_p.c_user_len);
2202*7836SJohn.Forte@Sun.COM 
2203*7836SJohn.Forte@Sun.COM 	chap_p.c_secret_len = pParms->chapParms.challengeSecretLength;
2204*7836SJohn.Forte@Sun.COM 	(void) memcpy(chap_p.c_secret, pParms->chapParms.challengeSecret,
2205*7836SJohn.Forte@Sun.COM 	    chap_p.c_secret_len);
2206*7836SJohn.Forte@Sun.COM 
2207*7836SJohn.Forte@Sun.COM 	if (method == IMA_AUTHMETHOD_CHAP) {
2208*7836SJohn.Forte@Sun.COM 		if (ioctl(fd, ISCSI_CHAP_SET, &chap_p) != 0) {
2209*7836SJohn.Forte@Sun.COM 			(void) close(fd);
2210*7836SJohn.Forte@Sun.COM 			syslog(LOG_USER|LOG_DEBUG,
2211*7836SJohn.Forte@Sun.COM 			    "ISCSI_CHAP_SET ioctl failed, errno: %d", errno);
2212*7836SJohn.Forte@Sun.COM 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2213*7836SJohn.Forte@Sun.COM 		}
2214*7836SJohn.Forte@Sun.COM 	}
2215*7836SJohn.Forte@Sun.COM 
2216*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2217*7836SJohn.Forte@Sun.COM }
2218*7836SJohn.Forte@Sun.COM 
2219*7836SJohn.Forte@Sun.COM /* A helper function to obtain iSCSI node parameters. */
2220*7836SJohn.Forte@Sun.COM static IMA_STATUS
2221*7836SJohn.Forte@Sun.COM getISCSINodeParameter(
2222*7836SJohn.Forte@Sun.COM     int paramType,
2223*7836SJohn.Forte@Sun.COM     IMA_OID *oid,
2224*7836SJohn.Forte@Sun.COM     void *pProps,
2225*7836SJohn.Forte@Sun.COM     uint32_t paramIndex
2226*7836SJohn.Forte@Sun.COM )
2227*7836SJohn.Forte@Sun.COM {
2228*7836SJohn.Forte@Sun.COM 	int		    fd;
2229*7836SJohn.Forte@Sun.COM 	iscsi_param_get_t   pg;
2230*7836SJohn.Forte@Sun.COM 
2231*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
2232*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
2233*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
2234*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2235*7836SJohn.Forte@Sun.COM 	}
2236*7836SJohn.Forte@Sun.COM 
2237*7836SJohn.Forte@Sun.COM 	(void) memset(&pg, 0, sizeof (iscsi_param_get_t));
2238*7836SJohn.Forte@Sun.COM 	pg.g_vers = ISCSI_INTERFACE_VERSION;
2239*7836SJohn.Forte@Sun.COM 	pg.g_oid = (uint32_t)oid->objectSequenceNumber;
2240*7836SJohn.Forte@Sun.COM 	pg.g_param = paramIndex;
2241*7836SJohn.Forte@Sun.COM 	pg.g_param_type = ISCSI_SESS_PARAM;
2242*7836SJohn.Forte@Sun.COM 
2243*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_PARAM_GET, &pg) != 0) {
2244*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
2245*7836SJohn.Forte@Sun.COM 		    "ISCSI_PARAM_GET ioctl failed, errno: %d", errno);
2246*7836SJohn.Forte@Sun.COM 		(void) close(fd);
2247*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2248*7836SJohn.Forte@Sun.COM 	}
2249*7836SJohn.Forte@Sun.COM 
2250*7836SJohn.Forte@Sun.COM 	switch (paramType) {
2251*7836SJohn.Forte@Sun.COM 		IMA_BOOL_VALUE *bp;
2252*7836SJohn.Forte@Sun.COM 		IMA_MIN_MAX_VALUE *mp;
2253*7836SJohn.Forte@Sun.COM 
2254*7836SJohn.Forte@Sun.COM 		case MIN_MAX_PARAM:
2255*7836SJohn.Forte@Sun.COM 			mp = (IMA_MIN_MAX_VALUE *)pProps;
2256*7836SJohn.Forte@Sun.COM 
2257*7836SJohn.Forte@Sun.COM 			mp->currentValueValid =
2258*7836SJohn.Forte@Sun.COM 			    (pg.g_value.v_valid == B_TRUE) ?
2259*7836SJohn.Forte@Sun.COM 			    IMA_TRUE : IMA_FALSE;
2260*7836SJohn.Forte@Sun.COM 			mp->currentValue = pg.g_value.v_integer.i_current;
2261*7836SJohn.Forte@Sun.COM 			mp->defaultValue = pg.g_value.v_integer.i_default;
2262*7836SJohn.Forte@Sun.COM 			mp->minimumValue = pg.g_value.v_integer.i_min;
2263*7836SJohn.Forte@Sun.COM 			mp->maximumValue = pg.g_value.v_integer.i_max;
2264*7836SJohn.Forte@Sun.COM 			mp->incrementValue = pg.g_value.v_integer.i_incr;
2265*7836SJohn.Forte@Sun.COM 			break;
2266*7836SJohn.Forte@Sun.COM 
2267*7836SJohn.Forte@Sun.COM 		case BOOL_PARAM:
2268*7836SJohn.Forte@Sun.COM 			bp = (IMA_BOOL_VALUE *)pProps;
2269*7836SJohn.Forte@Sun.COM 			bp->currentValueValid =
2270*7836SJohn.Forte@Sun.COM 			    (pg.g_value.v_valid == B_TRUE) ?
2271*7836SJohn.Forte@Sun.COM 			    IMA_TRUE : IMA_FALSE;
2272*7836SJohn.Forte@Sun.COM 			bp->currentValue = pg.g_value.v_bool.b_current;
2273*7836SJohn.Forte@Sun.COM 			bp->defaultValue = pg.g_value.v_bool.b_default;
2274*7836SJohn.Forte@Sun.COM 			break;
2275*7836SJohn.Forte@Sun.COM 
2276*7836SJohn.Forte@Sun.COM 		default:
2277*7836SJohn.Forte@Sun.COM 			break;
2278*7836SJohn.Forte@Sun.COM 	}
2279*7836SJohn.Forte@Sun.COM 
2280*7836SJohn.Forte@Sun.COM 	(void) close(fd);
2281*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2282*7836SJohn.Forte@Sun.COM }
2283*7836SJohn.Forte@Sun.COM 
2284*7836SJohn.Forte@Sun.COM /* A helper function to set iSCSI node parameters. */
2285*7836SJohn.Forte@Sun.COM static IMA_STATUS
2286*7836SJohn.Forte@Sun.COM setISCSINodeParameter(
2287*7836SJohn.Forte@Sun.COM     int paramType,
2288*7836SJohn.Forte@Sun.COM     IMA_OID *oid,
2289*7836SJohn.Forte@Sun.COM     void *pProp,
2290*7836SJohn.Forte@Sun.COM     uint32_t paramIndex
2291*7836SJohn.Forte@Sun.COM )
2292*7836SJohn.Forte@Sun.COM {
2293*7836SJohn.Forte@Sun.COM 	int		    fd;
2294*7836SJohn.Forte@Sun.COM 	iscsi_param_set_t   ps;
2295*7836SJohn.Forte@Sun.COM 
2296*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
2297*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
2298*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
2299*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2300*7836SJohn.Forte@Sun.COM 	}
2301*7836SJohn.Forte@Sun.COM 
2302*7836SJohn.Forte@Sun.COM 	(void) memset(&ps, 0, sizeof (iscsi_param_set_t));
2303*7836SJohn.Forte@Sun.COM 	ps.s_vers = ISCSI_INTERFACE_VERSION;
2304*7836SJohn.Forte@Sun.COM 	ps.s_oid = (uint32_t)oid->objectSequenceNumber;
2305*7836SJohn.Forte@Sun.COM 	ps.s_param = paramIndex;
2306*7836SJohn.Forte@Sun.COM 
2307*7836SJohn.Forte@Sun.COM 	switch (paramType) {
2308*7836SJohn.Forte@Sun.COM 		IMA_BOOL_VALUE *bp;
2309*7836SJohn.Forte@Sun.COM 		IMA_MIN_MAX_VALUE *mp;
2310*7836SJohn.Forte@Sun.COM 
2311*7836SJohn.Forte@Sun.COM 		case MIN_MAX_PARAM:
2312*7836SJohn.Forte@Sun.COM 			mp = (IMA_MIN_MAX_VALUE *)pProp;
2313*7836SJohn.Forte@Sun.COM 			ps.s_value.v_integer = mp->currentValue;
2314*7836SJohn.Forte@Sun.COM 			break;
2315*7836SJohn.Forte@Sun.COM 		case BOOL_PARAM:
2316*7836SJohn.Forte@Sun.COM 			bp = (IMA_BOOL_VALUE *)pProp;
2317*7836SJohn.Forte@Sun.COM 			ps.s_value.v_bool =
2318*7836SJohn.Forte@Sun.COM 			    (bp->currentValue == IMA_TRUE) ?
2319*7836SJohn.Forte@Sun.COM 			    B_TRUE : B_FALSE;
2320*7836SJohn.Forte@Sun.COM 			break;
2321*7836SJohn.Forte@Sun.COM 
2322*7836SJohn.Forte@Sun.COM 		default:
2323*7836SJohn.Forte@Sun.COM 			break;
2324*7836SJohn.Forte@Sun.COM 	}
2325*7836SJohn.Forte@Sun.COM 
2326*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_PARAM_SET, &ps)) {
2327*7836SJohn.Forte@Sun.COM 		int tmpErrno = errno;
2328*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
2329*7836SJohn.Forte@Sun.COM 		    "ISCSI_PARAM_SET ioctl failed, errno: %d", errno);
2330*7836SJohn.Forte@Sun.COM 		(void) close(fd);
2331*7836SJohn.Forte@Sun.COM 		switch (tmpErrno) {
2332*7836SJohn.Forte@Sun.COM 			case ENOTSUP :
2333*7836SJohn.Forte@Sun.COM 				return (IMA_ERROR_NOT_SUPPORTED);
2334*7836SJohn.Forte@Sun.COM 			default :
2335*7836SJohn.Forte@Sun.COM 				return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2336*7836SJohn.Forte@Sun.COM 		}
2337*7836SJohn.Forte@Sun.COM 	}
2338*7836SJohn.Forte@Sun.COM 
2339*7836SJohn.Forte@Sun.COM 	(void) close(fd);
2340*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2341*7836SJohn.Forte@Sun.COM }
2342*7836SJohn.Forte@Sun.COM 
2343*7836SJohn.Forte@Sun.COM static int
2344*7836SJohn.Forte@Sun.COM prepare_discovery_entry(
2345*7836SJohn.Forte@Sun.COM     IMA_TARGET_ADDRESS discoveryAddress,
2346*7836SJohn.Forte@Sun.COM     entry_t *entry
2347*7836SJohn.Forte@Sun.COM )
2348*7836SJohn.Forte@Sun.COM {
2349*7836SJohn.Forte@Sun.COM 	(void) memset(entry, 0, sizeof (entry_t));
2350*7836SJohn.Forte@Sun.COM 	entry->e_vers = ISCSI_INTERFACE_VERSION;
2351*7836SJohn.Forte@Sun.COM 	entry->e_oid = ISCSI_OID_NOTSET;
2352*7836SJohn.Forte@Sun.COM 
2353*7836SJohn.Forte@Sun.COM 	if (discoveryAddress.hostnameIpAddress.id.ipAddress.ipv4Address ==
2354*7836SJohn.Forte@Sun.COM 	    IMA_FALSE) {
2355*7836SJohn.Forte@Sun.COM 		bcopy(discoveryAddress.hostnameIpAddress.id.ipAddress.ipAddress,
2356*7836SJohn.Forte@Sun.COM 		    entry->e_u.u_in6.s6_addr,
2357*7836SJohn.Forte@Sun.COM 		    sizeof (entry->e_u.u_in6.s6_addr));
2358*7836SJohn.Forte@Sun.COM 		entry->e_insize = sizeof (struct in6_addr);
2359*7836SJohn.Forte@Sun.COM 	} else {
2360*7836SJohn.Forte@Sun.COM 		bcopy(discoveryAddress.hostnameIpAddress.id.ipAddress.ipAddress,
2361*7836SJohn.Forte@Sun.COM 		    &entry->e_u.u_in4.s_addr,
2362*7836SJohn.Forte@Sun.COM 		    sizeof (entry->e_u.u_in4.s_addr));
2363*7836SJohn.Forte@Sun.COM 		entry->e_insize = sizeof (struct in_addr);
2364*7836SJohn.Forte@Sun.COM 	}
2365*7836SJohn.Forte@Sun.COM 
2366*7836SJohn.Forte@Sun.COM 	entry->e_port = discoveryAddress.portNumber;
2367*7836SJohn.Forte@Sun.COM 	entry->e_tpgt = 0;
2368*7836SJohn.Forte@Sun.COM 	return (DISC_ADDR_OK);
2369*7836SJohn.Forte@Sun.COM }
2370*7836SJohn.Forte@Sun.COM 
2371*7836SJohn.Forte@Sun.COM static IMA_STATUS configure_discovery_method(
2372*7836SJohn.Forte@Sun.COM     IMA_BOOL enable,
2373*7836SJohn.Forte@Sun.COM     iSCSIDiscoveryMethod_t method
2374*7836SJohn.Forte@Sun.COM )
2375*7836SJohn.Forte@Sun.COM {
2376*7836SJohn.Forte@Sun.COM 	int fd, status;
2377*7836SJohn.Forte@Sun.COM 
2378*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
2379*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
2380*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
2381*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2382*7836SJohn.Forte@Sun.COM 	}
2383*7836SJohn.Forte@Sun.COM 
2384*7836SJohn.Forte@Sun.COM 	if (enable == IMA_FALSE) {
2385*7836SJohn.Forte@Sun.COM 		if (ioctl(fd, ISCSI_DISCOVERY_CLEAR, &method)) {
2386*7836SJohn.Forte@Sun.COM 			status = errno;
2387*7836SJohn.Forte@Sun.COM 			(void) close(fd);
2388*7836SJohn.Forte@Sun.COM 			syslog(LOG_USER|LOG_DEBUG,
2389*7836SJohn.Forte@Sun.COM 			    "ISCSI_DISCOVERY_CLEAR ioctl failed, errno: %d",
2390*7836SJohn.Forte@Sun.COM 			    status);
2391*7836SJohn.Forte@Sun.COM 			if (status == EBUSY) {
2392*7836SJohn.Forte@Sun.COM 				return (IMA_ERROR_LU_IN_USE);
2393*7836SJohn.Forte@Sun.COM 			} else {
2394*7836SJohn.Forte@Sun.COM 				return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2395*7836SJohn.Forte@Sun.COM 			}
2396*7836SJohn.Forte@Sun.COM 		}
2397*7836SJohn.Forte@Sun.COM 
2398*7836SJohn.Forte@Sun.COM 		(void) close(fd);
2399*7836SJohn.Forte@Sun.COM 		return (IMA_STATUS_SUCCESS);
2400*7836SJohn.Forte@Sun.COM 	} else {
2401*7836SJohn.Forte@Sun.COM 		/* Set the discovery method */
2402*7836SJohn.Forte@Sun.COM 		if (ioctl(fd, ISCSI_DISCOVERY_SET, &method)) {
2403*7836SJohn.Forte@Sun.COM 			(void) close(fd);
2404*7836SJohn.Forte@Sun.COM 			syslog(LOG_USER|LOG_DEBUG,
2405*7836SJohn.Forte@Sun.COM 			    "ISCSI_DISCOVERY_SET ioctl failed, errno: %d",
2406*7836SJohn.Forte@Sun.COM 			    errno);
2407*7836SJohn.Forte@Sun.COM 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2408*7836SJohn.Forte@Sun.COM 		}
2409*7836SJohn.Forte@Sun.COM 
2410*7836SJohn.Forte@Sun.COM 		(void) close(fd);
2411*7836SJohn.Forte@Sun.COM 		return (IMA_STATUS_SUCCESS);
2412*7836SJohn.Forte@Sun.COM 	}
2413*7836SJohn.Forte@Sun.COM }
2414*7836SJohn.Forte@Sun.COM 
2415*7836SJohn.Forte@Sun.COM static IMA_STATUS get_target_oid_list(
2416*7836SJohn.Forte@Sun.COM     uint32_t targetListType,
2417*7836SJohn.Forte@Sun.COM     IMA_OID_LIST **ppList)
2418*7836SJohn.Forte@Sun.COM {
2419*7836SJohn.Forte@Sun.COM 	int		    fd;
2420*7836SJohn.Forte@Sun.COM 	int		    i;
2421*7836SJohn.Forte@Sun.COM 	int		    target_list_size;
2422*7836SJohn.Forte@Sun.COM 	iscsi_target_list_t *idlp, tl_info;
2423*7836SJohn.Forte@Sun.COM 
2424*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
2425*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
2426*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
2427*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2428*7836SJohn.Forte@Sun.COM 	}
2429*7836SJohn.Forte@Sun.COM 
2430*7836SJohn.Forte@Sun.COM 	(void) memset(&tl_info, 0, sizeof (tl_info));
2431*7836SJohn.Forte@Sun.COM 	tl_info.tl_vers = ISCSI_INTERFACE_VERSION;
2432*7836SJohn.Forte@Sun.COM 	tl_info.tl_in_cnt = 0;
2433*7836SJohn.Forte@Sun.COM 	tl_info.tl_tgt_list_type = targetListType;
2434*7836SJohn.Forte@Sun.COM 
2435*7836SJohn.Forte@Sun.COM 	/*
2436*7836SJohn.Forte@Sun.COM 	 * Issue ioctl to obtain the number of targets.
2437*7836SJohn.Forte@Sun.COM 	 */
2438*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_TARGET_OID_LIST_GET, &tl_info) != 0) {
2439*7836SJohn.Forte@Sun.COM 		(void) close(fd);
2440*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
2441*7836SJohn.Forte@Sun.COM 		    "ISCSI_TARGET_OID_LIST_GET ioctl %d failed, errno: %d",
2442*7836SJohn.Forte@Sun.COM 		    targetListType, errno);
2443*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2444*7836SJohn.Forte@Sun.COM 	}
2445*7836SJohn.Forte@Sun.COM 
2446*7836SJohn.Forte@Sun.COM 	target_list_size = sizeof (iscsi_target_list_t);
2447*7836SJohn.Forte@Sun.COM 	if (tl_info.tl_out_cnt > 1) {
2448*7836SJohn.Forte@Sun.COM 		target_list_size += (sizeof (uint32_t) *
2449*7836SJohn.Forte@Sun.COM 		    tl_info.tl_out_cnt - 1);
2450*7836SJohn.Forte@Sun.COM 	}
2451*7836SJohn.Forte@Sun.COM 
2452*7836SJohn.Forte@Sun.COM 	idlp = (iscsi_target_list_t *)calloc(1, target_list_size);
2453*7836SJohn.Forte@Sun.COM 	if (idlp == NULL) {
2454*7836SJohn.Forte@Sun.COM 		(void) close(fd);
2455*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2456*7836SJohn.Forte@Sun.COM 	}
2457*7836SJohn.Forte@Sun.COM 
2458*7836SJohn.Forte@Sun.COM 	idlp->tl_vers = ISCSI_INTERFACE_VERSION;
2459*7836SJohn.Forte@Sun.COM 	idlp->tl_in_cnt = tl_info.tl_out_cnt;
2460*7836SJohn.Forte@Sun.COM 	idlp->tl_tgt_list_type = targetListType;
2461*7836SJohn.Forte@Sun.COM 
2462*7836SJohn.Forte@Sun.COM 	/* Issue the same ioctl again to obtain the OIDs. */
2463*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_TARGET_OID_LIST_GET, idlp) != 0) {
2464*7836SJohn.Forte@Sun.COM 		free(idlp);
2465*7836SJohn.Forte@Sun.COM 		(void) close(fd);
2466*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
2467*7836SJohn.Forte@Sun.COM 		    "ISCSI_TARGET_OID_LIST_GET ioctl %d failed, errno: %d",
2468*7836SJohn.Forte@Sun.COM 		    targetListType, errno);
2469*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2470*7836SJohn.Forte@Sun.COM 	}
2471*7836SJohn.Forte@Sun.COM 
2472*7836SJohn.Forte@Sun.COM 	*ppList = (IMA_OID_LIST *)calloc(1, sizeof (IMA_OID_LIST) +
2473*7836SJohn.Forte@Sun.COM 	    idlp->tl_out_cnt * sizeof (IMA_OID));
2474*7836SJohn.Forte@Sun.COM 	if (*ppList == NULL) {
2475*7836SJohn.Forte@Sun.COM 		free(idlp);
2476*7836SJohn.Forte@Sun.COM 		(void) close(fd);
2477*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2478*7836SJohn.Forte@Sun.COM 	}
2479*7836SJohn.Forte@Sun.COM 	(*ppList)->oidCount = idlp->tl_out_cnt;
2480*7836SJohn.Forte@Sun.COM 
2481*7836SJohn.Forte@Sun.COM 	for (i = 0; i < idlp->tl_out_cnt; i++) {
2482*7836SJohn.Forte@Sun.COM 
2483*7836SJohn.Forte@Sun.COM 		if (targetListType == ISCSI_STATIC_TGT_OID_LIST)
2484*7836SJohn.Forte@Sun.COM 			(*ppList)->oids[i].objectType =
2485*7836SJohn.Forte@Sun.COM 			    IMA_OBJECT_TYPE_STATIC_DISCOVERY_TARGET;
2486*7836SJohn.Forte@Sun.COM 		else
2487*7836SJohn.Forte@Sun.COM 			(*ppList)->oids[i].objectType = IMA_OBJECT_TYPE_TARGET;
2488*7836SJohn.Forte@Sun.COM 
2489*7836SJohn.Forte@Sun.COM 		(*ppList)->oids[i].ownerId = pluginOwnerId;
2490*7836SJohn.Forte@Sun.COM 		(*ppList)->oids[i].objectSequenceNumber = idlp->tl_oid_list[i];
2491*7836SJohn.Forte@Sun.COM 	}
2492*7836SJohn.Forte@Sun.COM 
2493*7836SJohn.Forte@Sun.COM 	free(idlp);
2494*7836SJohn.Forte@Sun.COM 	(void) close(fd);
2495*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2496*7836SJohn.Forte@Sun.COM }
2497*7836SJohn.Forte@Sun.COM 
2498*7836SJohn.Forte@Sun.COM static IMA_STATUS get_target_lun_oid_list(
2499*7836SJohn.Forte@Sun.COM     IMA_OID * targetOid,
2500*7836SJohn.Forte@Sun.COM     iscsi_lun_list_t  **ppLunList)
2501*7836SJohn.Forte@Sun.COM {
2502*7836SJohn.Forte@Sun.COM 	int			fd;
2503*7836SJohn.Forte@Sun.COM 	iscsi_lun_list_t	*illp, ll_info;
2504*7836SJohn.Forte@Sun.COM 	int			lun_list_size;
2505*7836SJohn.Forte@Sun.COM 
2506*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
2507*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
2508*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
2509*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2510*7836SJohn.Forte@Sun.COM 	}
2511*7836SJohn.Forte@Sun.COM 
2512*7836SJohn.Forte@Sun.COM 	(void) memset(&ll_info, 0, sizeof (ll_info));
2513*7836SJohn.Forte@Sun.COM 	ll_info.ll_vers = ISCSI_INTERFACE_VERSION;
2514*7836SJohn.Forte@Sun.COM 	if (targetOid == NULL) {
2515*7836SJohn.Forte@Sun.COM 		/* get lun oid list for all targets */
2516*7836SJohn.Forte@Sun.COM 		ll_info.ll_all_tgts = B_TRUE;
2517*7836SJohn.Forte@Sun.COM 	} else {
2518*7836SJohn.Forte@Sun.COM 		/* get lun oid list for single target */
2519*7836SJohn.Forte@Sun.COM 		ll_info.ll_all_tgts = B_FALSE;
2520*7836SJohn.Forte@Sun.COM 		ll_info.ll_tgt_oid = (uint32_t)targetOid->objectSequenceNumber;
2521*7836SJohn.Forte@Sun.COM 	}
2522*7836SJohn.Forte@Sun.COM 	ll_info.ll_in_cnt = 0;
2523*7836SJohn.Forte@Sun.COM 
2524*7836SJohn.Forte@Sun.COM 	/*
2525*7836SJohn.Forte@Sun.COM 	 * Issue ioctl to obtain the number of target LUNs.
2526*7836SJohn.Forte@Sun.COM 	 */
2527*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_LUN_OID_LIST_GET, &ll_info) != 0) {
2528*7836SJohn.Forte@Sun.COM 		(void) close(fd);
2529*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
2530*7836SJohn.Forte@Sun.COM 		    "ISCSI_LUN_LIST_GET ioctl failed, errno: %d", errno);
2531*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2532*7836SJohn.Forte@Sun.COM 	}
2533*7836SJohn.Forte@Sun.COM 
2534*7836SJohn.Forte@Sun.COM 	lun_list_size = sizeof (iscsi_lun_list_t);
2535*7836SJohn.Forte@Sun.COM 	if (ll_info.ll_out_cnt > 1) {
2536*7836SJohn.Forte@Sun.COM 		lun_list_size += (sizeof (iscsi_if_lun_t) *
2537*7836SJohn.Forte@Sun.COM 		    (ll_info.ll_out_cnt - 1));
2538*7836SJohn.Forte@Sun.COM 	}
2539*7836SJohn.Forte@Sun.COM 
2540*7836SJohn.Forte@Sun.COM 	illp = (iscsi_lun_list_t *)calloc(1, lun_list_size);
2541*7836SJohn.Forte@Sun.COM 	if (illp == NULL) {
2542*7836SJohn.Forte@Sun.COM 		(void) close(fd);
2543*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2544*7836SJohn.Forte@Sun.COM 	}
2545*7836SJohn.Forte@Sun.COM 	illp->ll_vers = ISCSI_INTERFACE_VERSION;
2546*7836SJohn.Forte@Sun.COM 	illp->ll_all_tgts = ll_info.ll_all_tgts;
2547*7836SJohn.Forte@Sun.COM 	illp->ll_tgt_oid = ll_info.ll_tgt_oid;
2548*7836SJohn.Forte@Sun.COM 	illp->ll_in_cnt = ll_info.ll_out_cnt;
2549*7836SJohn.Forte@Sun.COM 
2550*7836SJohn.Forte@Sun.COM 	/* Issue the same ioctl again to get the target LUN list */
2551*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_LUN_OID_LIST_GET, illp) != 0) {
2552*7836SJohn.Forte@Sun.COM 		free(illp);
2553*7836SJohn.Forte@Sun.COM 		(void) close(fd);
2554*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
2555*7836SJohn.Forte@Sun.COM 		    "ISCSI_LUN_LIST_GET ioctl failed, errno: %d", errno);
2556*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2557*7836SJohn.Forte@Sun.COM 	}
2558*7836SJohn.Forte@Sun.COM 
2559*7836SJohn.Forte@Sun.COM 	*ppLunList = illp;
2560*7836SJohn.Forte@Sun.COM 
2561*7836SJohn.Forte@Sun.COM 	(void) close(fd);
2562*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2563*7836SJohn.Forte@Sun.COM }
2564*7836SJohn.Forte@Sun.COM 
2565*7836SJohn.Forte@Sun.COM 
2566*7836SJohn.Forte@Sun.COM /* A helper function to set authentication method. */
2567*7836SJohn.Forte@Sun.COM static IMA_STATUS
2568*7836SJohn.Forte@Sun.COM setAuthMethods(
2569*7836SJohn.Forte@Sun.COM     IMA_OID oid,
2570*7836SJohn.Forte@Sun.COM     IMA_UINT *pMethodCount,
2571*7836SJohn.Forte@Sun.COM     const IMA_AUTHMETHOD *pMethodList
2572*7836SJohn.Forte@Sun.COM )
2573*7836SJohn.Forte@Sun.COM {
2574*7836SJohn.Forte@Sun.COM 	int fd;
2575*7836SJohn.Forte@Sun.COM 	int i;
2576*7836SJohn.Forte@Sun.COM 	iscsi_auth_props_t auth;
2577*7836SJohn.Forte@Sun.COM 
2578*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
2579*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
2580*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
2581*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2582*7836SJohn.Forte@Sun.COM 	}
2583*7836SJohn.Forte@Sun.COM 	(void) memset(&auth, 0, sizeof (iscsi_auth_props_t));
2584*7836SJohn.Forte@Sun.COM 	auth.a_vers = ISCSI_INTERFACE_VERSION;
2585*7836SJohn.Forte@Sun.COM 	auth.a_oid = (uint32_t)oid.objectSequenceNumber;
2586*7836SJohn.Forte@Sun.COM 	/* First do a get because other data fields may exist */
2587*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_AUTH_GET, &auth) != 0) {
2588*7836SJohn.Forte@Sun.COM 		/* EMPTY */
2589*7836SJohn.Forte@Sun.COM 		/* It is fine if there is no other data fields. */
2590*7836SJohn.Forte@Sun.COM 	}
2591*7836SJohn.Forte@Sun.COM 	auth.a_auth_method = authMethodNone;
2592*7836SJohn.Forte@Sun.COM 
2593*7836SJohn.Forte@Sun.COM 	for (i = 0; i < *pMethodCount; i++) {
2594*7836SJohn.Forte@Sun.COM 		switch (pMethodList[i]) {
2595*7836SJohn.Forte@Sun.COM 			case IMA_AUTHMETHOD_CHAP:
2596*7836SJohn.Forte@Sun.COM 				auth.a_auth_method |= authMethodCHAP;
2597*7836SJohn.Forte@Sun.COM 				break;
2598*7836SJohn.Forte@Sun.COM 			default:
2599*7836SJohn.Forte@Sun.COM 				break;
2600*7836SJohn.Forte@Sun.COM 		}
2601*7836SJohn.Forte@Sun.COM 	}
2602*7836SJohn.Forte@Sun.COM 
2603*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_AUTH_SET, &auth) != 0) {
2604*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
2605*7836SJohn.Forte@Sun.COM 		    "ISCSI_AUTH_SET failed, errno: %d", errno);
2606*7836SJohn.Forte@Sun.COM 		(void) close(fd);
2607*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2608*7836SJohn.Forte@Sun.COM 	}
2609*7836SJohn.Forte@Sun.COM 
2610*7836SJohn.Forte@Sun.COM 	(void) close(fd);
2611*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2612*7836SJohn.Forte@Sun.COM }
2613*7836SJohn.Forte@Sun.COM 
2614*7836SJohn.Forte@Sun.COM /* A helper function to get authentication method. */
2615*7836SJohn.Forte@Sun.COM static IMA_STATUS
2616*7836SJohn.Forte@Sun.COM getAuthMethods(
2617*7836SJohn.Forte@Sun.COM     IMA_OID oid,
2618*7836SJohn.Forte@Sun.COM     IMA_UINT	*pMethodCount,
2619*7836SJohn.Forte@Sun.COM     IMA_AUTHMETHOD *pMethodList
2620*7836SJohn.Forte@Sun.COM )
2621*7836SJohn.Forte@Sun.COM {
2622*7836SJohn.Forte@Sun.COM 	int fd, i;
2623*7836SJohn.Forte@Sun.COM 	iscsi_auth_props_t auth;
2624*7836SJohn.Forte@Sun.COM 
2625*7836SJohn.Forte@Sun.COM 	if (pMethodList == NULL) {
2626*7836SJohn.Forte@Sun.COM 		*pMethodCount = 0;
2627*7836SJohn.Forte@Sun.COM 		return (IMA_STATUS_SUCCESS);
2628*7836SJohn.Forte@Sun.COM 	}
2629*7836SJohn.Forte@Sun.COM 
2630*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
2631*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
2632*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
2633*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2634*7836SJohn.Forte@Sun.COM 	}
2635*7836SJohn.Forte@Sun.COM 
2636*7836SJohn.Forte@Sun.COM 	(void) memset(&auth, 0, sizeof (iscsi_auth_props_t));
2637*7836SJohn.Forte@Sun.COM 	auth.a_vers = ISCSI_INTERFACE_VERSION;
2638*7836SJohn.Forte@Sun.COM 	auth.a_oid = (uint32_t)oid.objectSequenceNumber;
2639*7836SJohn.Forte@Sun.COM 
2640*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_AUTH_GET, &auth) != 0) {
2641*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
2642*7836SJohn.Forte@Sun.COM 		    "ISCSI_AUTH_GET failed, errno: %d", errno);
2643*7836SJohn.Forte@Sun.COM 		(void) close(fd);
2644*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2645*7836SJohn.Forte@Sun.COM 	}
2646*7836SJohn.Forte@Sun.COM 
2647*7836SJohn.Forte@Sun.COM 	i = 0;
2648*7836SJohn.Forte@Sun.COM 	if (auth.a_auth_method == IMA_AUTHMETHOD_NONE) {
2649*7836SJohn.Forte@Sun.COM 		pMethodList[i++] = IMA_AUTHMETHOD_NONE;
2650*7836SJohn.Forte@Sun.COM 	} else if (auth.a_auth_method & authMethodCHAP) {
2651*7836SJohn.Forte@Sun.COM 		pMethodList[i++] = IMA_AUTHMETHOD_CHAP;
2652*7836SJohn.Forte@Sun.COM 	}
2653*7836SJohn.Forte@Sun.COM 	*pMethodCount = i;
2654*7836SJohn.Forte@Sun.COM 
2655*7836SJohn.Forte@Sun.COM 	(void) close(fd);
2656*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2657*7836SJohn.Forte@Sun.COM }
2658*7836SJohn.Forte@Sun.COM 
2659*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetPhbaOidList(
2660*7836SJohn.Forte@Sun.COM 		IMA_OID_LIST **ppList
2661*7836SJohn.Forte@Sun.COM )
2662*7836SJohn.Forte@Sun.COM {
2663*7836SJohn.Forte@Sun.COM 	*ppList = (IMA_OID_LIST*)calloc(1, sizeof (IMA_OID_LIST));
2664*7836SJohn.Forte@Sun.COM 	if (*ppList == NULL) {
2665*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2666*7836SJohn.Forte@Sun.COM 	}
2667*7836SJohn.Forte@Sun.COM 	(*ppList)->oidCount = 0;
2668*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2669*7836SJohn.Forte@Sun.COM }
2670*7836SJohn.Forte@Sun.COM 
2671*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2672*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetPhbaProperties(
2673*7836SJohn.Forte@Sun.COM 		IMA_OID phbaOid,
2674*7836SJohn.Forte@Sun.COM 		IMA_PHBA_PROPERTIES *pProps
2675*7836SJohn.Forte@Sun.COM )
2676*7836SJohn.Forte@Sun.COM {
2677*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2678*7836SJohn.Forte@Sun.COM }
2679*7836SJohn.Forte@Sun.COM 
2680*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2681*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetPhbaStatus(
2682*7836SJohn.Forte@Sun.COM 		IMA_OID phbaOid,
2683*7836SJohn.Forte@Sun.COM 		IMA_PHBA_STATUS *pStatus
2684*7836SJohn.Forte@Sun.COM )
2685*7836SJohn.Forte@Sun.COM {
2686*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2687*7836SJohn.Forte@Sun.COM }
2688*7836SJohn.Forte@Sun.COM 
2689*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2690*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetPhbaDownloadProperties(
2691*7836SJohn.Forte@Sun.COM 		IMA_OID phbaOid,
2692*7836SJohn.Forte@Sun.COM 		IMA_PHBA_DOWNLOAD_PROPERTIES *pProps
2693*7836SJohn.Forte@Sun.COM )
2694*7836SJohn.Forte@Sun.COM {
2695*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2696*7836SJohn.Forte@Sun.COM }
2697*7836SJohn.Forte@Sun.COM 
2698*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2699*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_IsPhbaDownloadFile(
2700*7836SJohn.Forte@Sun.COM 		IMA_OID phbaOid,
2701*7836SJohn.Forte@Sun.COM 		const IMA_WCHAR *pFileName,
2702*7836SJohn.Forte@Sun.COM 		IMA_PHBA_DOWNLOAD_IMAGE_PROPERTIES *pProps
2703*7836SJohn.Forte@Sun.COM )
2704*7836SJohn.Forte@Sun.COM {
2705*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2706*7836SJohn.Forte@Sun.COM }
2707*7836SJohn.Forte@Sun.COM 
2708*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2709*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_PhbaDownload(
2710*7836SJohn.Forte@Sun.COM 		IMA_OID phbaOid,
2711*7836SJohn.Forte@Sun.COM 		IMA_PHBA_DOWNLOAD_IMAGE_TYPE imageType,
2712*7836SJohn.Forte@Sun.COM 		const IMA_WCHAR *pFileName
2713*7836SJohn.Forte@Sun.COM )
2714*7836SJohn.Forte@Sun.COM {
2715*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2716*7836SJohn.Forte@Sun.COM }
2717*7836SJohn.Forte@Sun.COM 
2718*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetPnpOidList(
2719*7836SJohn.Forte@Sun.COM 		IMA_OID pnpOid,
2720*7836SJohn.Forte@Sun.COM 		IMA_OID_LIST **ppList
2721*7836SJohn.Forte@Sun.COM )
2722*7836SJohn.Forte@Sun.COM {
2723*7836SJohn.Forte@Sun.COM 	/*
2724*7836SJohn.Forte@Sun.COM 	 * Always return the same object ID for the pnp as the spec
2725*7836SJohn.Forte@Sun.COM 	 * states that this function will always return a list of at least
2726*7836SJohn.Forte@Sun.COM 	 * one element
2727*7836SJohn.Forte@Sun.COM 	 */
2728*7836SJohn.Forte@Sun.COM 	pnpOid.objectType = IMA_OBJECT_TYPE_PNP;
2729*7836SJohn.Forte@Sun.COM 	pnpOid.ownerId = pluginOwnerId;
2730*7836SJohn.Forte@Sun.COM 	pnpOid.objectSequenceNumber = ISCSI_INITIATOR_OID;
2731*7836SJohn.Forte@Sun.COM 
2732*7836SJohn.Forte@Sun.COM 	*ppList = (IMA_OID_LIST*)calloc(1, sizeof (IMA_OID_LIST) +
2733*7836SJohn.Forte@Sun.COM 	    (1* sizeof (IMA_OID)));
2734*7836SJohn.Forte@Sun.COM 
2735*7836SJohn.Forte@Sun.COM 	if (*ppList == NULL) {
2736*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2737*7836SJohn.Forte@Sun.COM 	}
2738*7836SJohn.Forte@Sun.COM 
2739*7836SJohn.Forte@Sun.COM 	(*ppList)->oidCount = 1;
2740*7836SJohn.Forte@Sun.COM 	(void) memcpy(&(*ppList)->oids[0], &pnpOid, sizeof (pnpOid));
2741*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2742*7836SJohn.Forte@Sun.COM }
2743*7836SJohn.Forte@Sun.COM 
2744*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2745*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetPnpProperties(
2746*7836SJohn.Forte@Sun.COM 		IMA_OID pnpOid,
2747*7836SJohn.Forte@Sun.COM 		IMA_PNP_PROPERTIES *pProps
2748*7836SJohn.Forte@Sun.COM )
2749*7836SJohn.Forte@Sun.COM {
2750*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2751*7836SJohn.Forte@Sun.COM }
2752*7836SJohn.Forte@Sun.COM 
2753*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2754*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetPnpStatistics(
2755*7836SJohn.Forte@Sun.COM 		IMA_OID pnpOid,
2756*7836SJohn.Forte@Sun.COM 		IMA_PNP_STATISTICS *pStats
2757*7836SJohn.Forte@Sun.COM )
2758*7836SJohn.Forte@Sun.COM {
2759*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2760*7836SJohn.Forte@Sun.COM }
2761*7836SJohn.Forte@Sun.COM 
2762*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2763*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetIpProperties(
2764*7836SJohn.Forte@Sun.COM 		IMA_OID oid,
2765*7836SJohn.Forte@Sun.COM 		IMA_IP_PROPERTIES *pProps
2766*7836SJohn.Forte@Sun.COM )
2767*7836SJohn.Forte@Sun.COM {
2768*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2769*7836SJohn.Forte@Sun.COM }
2770*7836SJohn.Forte@Sun.COM 
2771*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2772*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_SetDefaultGateway(
2773*7836SJohn.Forte@Sun.COM 		IMA_OID oid,
2774*7836SJohn.Forte@Sun.COM 		IMA_IP_ADDRESS defaultGateway
2775*7836SJohn.Forte@Sun.COM )
2776*7836SJohn.Forte@Sun.COM {
2777*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2778*7836SJohn.Forte@Sun.COM }
2779*7836SJohn.Forte@Sun.COM 
2780*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2781*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_SetDnsServerAddress(
2782*7836SJohn.Forte@Sun.COM 		IMA_OID oid,
2783*7836SJohn.Forte@Sun.COM 		const IMA_IP_ADDRESS *pPrimaryDnsServerAddress,
2784*7836SJohn.Forte@Sun.COM 		const IMA_IP_ADDRESS *pAlternateDnsServerAddress
2785*7836SJohn.Forte@Sun.COM )
2786*7836SJohn.Forte@Sun.COM {
2787*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2788*7836SJohn.Forte@Sun.COM }
2789*7836SJohn.Forte@Sun.COM 
2790*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2791*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_SetSubnetMask(
2792*7836SJohn.Forte@Sun.COM 		IMA_OID oid,
2793*7836SJohn.Forte@Sun.COM 		IMA_IP_ADDRESS subnetMask
2794*7836SJohn.Forte@Sun.COM )
2795*7836SJohn.Forte@Sun.COM {
2796*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2797*7836SJohn.Forte@Sun.COM }
2798*7836SJohn.Forte@Sun.COM 
2799*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2800*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_SetIpConfigMethod(
2801*7836SJohn.Forte@Sun.COM 		IMA_OID oid,
2802*7836SJohn.Forte@Sun.COM 		IMA_BOOL enableDhcpIpConfiguration
2803*7836SJohn.Forte@Sun.COM )
2804*7836SJohn.Forte@Sun.COM {
2805*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2806*7836SJohn.Forte@Sun.COM }
2807*7836SJohn.Forte@Sun.COM 
2808*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_RegisterForObjectPropertyChanges(
2809*7836SJohn.Forte@Sun.COM 		IMA_OBJECT_PROPERTY_FN pClientFn
2810*7836SJohn.Forte@Sun.COM )
2811*7836SJohn.Forte@Sun.COM {
2812*7836SJohn.Forte@Sun.COM 	pObjectPropertyCallback = pClientFn;
2813*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2814*7836SJohn.Forte@Sun.COM }
2815*7836SJohn.Forte@Sun.COM 
2816*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2817*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_DeregisterForObjectPropertyChanges(
2818*7836SJohn.Forte@Sun.COM 		IMA_OBJECT_PROPERTY_FN pClientFn
2819*7836SJohn.Forte@Sun.COM )
2820*7836SJohn.Forte@Sun.COM {
2821*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2822*7836SJohn.Forte@Sun.COM }
2823*7836SJohn.Forte@Sun.COM 
2824*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_RegisterForObjectVisibilityChanges(
2825*7836SJohn.Forte@Sun.COM 		IMA_OBJECT_VISIBILITY_FN pClientFn
2826*7836SJohn.Forte@Sun.COM )
2827*7836SJohn.Forte@Sun.COM {
2828*7836SJohn.Forte@Sun.COM 	pObjectVisibilityCallback = pClientFn;
2829*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2830*7836SJohn.Forte@Sun.COM }
2831*7836SJohn.Forte@Sun.COM 
2832*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2833*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_DeregisterForObjectVisibilityChanges(
2834*7836SJohn.Forte@Sun.COM 		IMA_OBJECT_VISIBILITY_FN pClientFn
2835*7836SJohn.Forte@Sun.COM )
2836*7836SJohn.Forte@Sun.COM {
2837*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2838*7836SJohn.Forte@Sun.COM }
2839*7836SJohn.Forte@Sun.COM 
2840*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2841*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetNetworkPortStatus(
2842*7836SJohn.Forte@Sun.COM 		IMA_OID portOid,
2843*7836SJohn.Forte@Sun.COM 		IMA_NETWORK_PORT_STATUS *pStaus
2844*7836SJohn.Forte@Sun.COM )
2845*7836SJohn.Forte@Sun.COM {
2846*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2847*7836SJohn.Forte@Sun.COM }
2848*7836SJohn.Forte@Sun.COM 
2849*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2850*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetNetworkPortalOidList(
2851*7836SJohn.Forte@Sun.COM 		IMA_OID pnpOid,
2852*7836SJohn.Forte@Sun.COM 		IMA_OID_LIST **ppList
2853*7836SJohn.Forte@Sun.COM )
2854*7836SJohn.Forte@Sun.COM {
2855*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2856*7836SJohn.Forte@Sun.COM }
2857*7836SJohn.Forte@Sun.COM 
2858*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2859*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetNetworkPortalProperties(
2860*7836SJohn.Forte@Sun.COM 		IMA_OID networkPortalOid,
2861*7836SJohn.Forte@Sun.COM 		IMA_NETWORK_PORTAL_PROPERTIES *pProps
2862*7836SJohn.Forte@Sun.COM )
2863*7836SJohn.Forte@Sun.COM {
2864*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2865*7836SJohn.Forte@Sun.COM }
2866*7836SJohn.Forte@Sun.COM 
2867*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2868*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_SetNetworkPortalIpAddress(
2869*7836SJohn.Forte@Sun.COM 		IMA_OID networkPortalOid,
2870*7836SJohn.Forte@Sun.COM 		const IMA_IP_ADDRESS NewIpAddress
2871*7836SJohn.Forte@Sun.COM )
2872*7836SJohn.Forte@Sun.COM {
2873*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2874*7836SJohn.Forte@Sun.COM }
2875*7836SJohn.Forte@Sun.COM 
2876*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2877*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_RemoveStaleData(
2878*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaOid
2879*7836SJohn.Forte@Sun.COM )
2880*7836SJohn.Forte@Sun.COM {
2881*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_NOT_SUPPORTED);
2882*7836SJohn.Forte@Sun.COM }
2883*7836SJohn.Forte@Sun.COM 
2884*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2885*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetIpsecProperties(
2886*7836SJohn.Forte@Sun.COM 		IMA_OID oid,
2887*7836SJohn.Forte@Sun.COM 		IMA_IPSEC_PROPERTIES *pProps
2888*7836SJohn.Forte@Sun.COM )
2889*7836SJohn.Forte@Sun.COM {
2890*7836SJohn.Forte@Sun.COM 	pProps->ipsecSupported = IMA_TRUE;
2891*7836SJohn.Forte@Sun.COM 	pProps->implementedInHardware = IMA_FALSE;
2892*7836SJohn.Forte@Sun.COM 	pProps->implementedInSoftware = IMA_TRUE;
2893*7836SJohn.Forte@Sun.COM 
2894*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2895*7836SJohn.Forte@Sun.COM }
2896*7836SJohn.Forte@Sun.COM 
2897*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2898*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetLhbaProperties(
2899*7836SJohn.Forte@Sun.COM 		IMA_OID lhbaOid,
2900*7836SJohn.Forte@Sun.COM 		IMA_LHBA_PROPERTIES *pProps
2901*7836SJohn.Forte@Sun.COM )
2902*7836SJohn.Forte@Sun.COM {
2903*7836SJohn.Forte@Sun.COM 
2904*7836SJohn.Forte@Sun.COM 	if (pProps == NULL) {
2905*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INVALID_PARAMETER);
2906*7836SJohn.Forte@Sun.COM 	}
2907*7836SJohn.Forte@Sun.COM 
2908*7836SJohn.Forte@Sun.COM 	if (lhbaObjectId.objectSequenceNumber != ISCSI_INITIATOR_OID) {
2909*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_OBJECT_NOT_FOUND);
2910*7836SJohn.Forte@Sun.COM 	}
2911*7836SJohn.Forte@Sun.COM 
2912*7836SJohn.Forte@Sun.COM 	(void) memset(pProps, 0, sizeof (IMA_LHBA_PROPERTIES));
2913*7836SJohn.Forte@Sun.COM 	(void) mbstowcs(pProps->osDeviceName, OS_DEVICE_NAME,
2914*7836SJohn.Forte@Sun.COM 	    OS_DEVICE_NAME_LEN);
2915*7836SJohn.Forte@Sun.COM 	pProps->luExposingSupported = IMA_FALSE;
2916*7836SJohn.Forte@Sun.COM 	pProps->isDestroyable = IMA_FALSE;
2917*7836SJohn.Forte@Sun.COM 	pProps->staleDataRemovable = IMA_FALSE;
2918*7836SJohn.Forte@Sun.COM 	pProps->staleDataSize = 0;
2919*7836SJohn.Forte@Sun.COM 	pProps->initiatorAuthMethodsSettable = IMA_TRUE;
2920*7836SJohn.Forte@Sun.COM 	pProps->targetAuthMethodsSettable = IMA_FALSE;
2921*7836SJohn.Forte@Sun.COM 
2922*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2923*7836SJohn.Forte@Sun.COM }
2924*7836SJohn.Forte@Sun.COM 
2925*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetLnpOidList(
2926*7836SJohn.Forte@Sun.COM 		IMA_OID_LIST **ppList
2927*7836SJohn.Forte@Sun.COM )
2928*7836SJohn.Forte@Sun.COM {
2929*7836SJohn.Forte@Sun.COM 	*ppList = (IMA_OID_LIST *) calloc(1, (sizeof (IMA_OID_LIST)));
2930*7836SJohn.Forte@Sun.COM 	if (*ppList == NULL) {
2931*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
2932*7836SJohn.Forte@Sun.COM 	}
2933*7836SJohn.Forte@Sun.COM 	(*ppList)->oidCount = 0;
2934*7836SJohn.Forte@Sun.COM 
2935*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2936*7836SJohn.Forte@Sun.COM }
2937*7836SJohn.Forte@Sun.COM 
2938*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2939*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetLnpProperties(
2940*7836SJohn.Forte@Sun.COM 		IMA_OID lnpOid,
2941*7836SJohn.Forte@Sun.COM 		IMA_LNP_PROPERTIES *pProps
2942*7836SJohn.Forte@Sun.COM )
2943*7836SJohn.Forte@Sun.COM {
2944*7836SJohn.Forte@Sun.COM 	return (IMA_ERROR_OBJECT_NOT_FOUND);
2945*7836SJohn.Forte@Sun.COM }
2946*7836SJohn.Forte@Sun.COM 
2947*7836SJohn.Forte@Sun.COM #define	IMA_DISK_DEVICE_NAME_PREFIX	"/dev/rdsk/"
2948*7836SJohn.Forte@Sun.COM #define	IMA_TAPE_DEVICE_NAME_PREFIX	"/dev/rmt/"
2949*7836SJohn.Forte@Sun.COM static int
2950*7836SJohn.Forte@Sun.COM get_lun_devlink(di_devlink_t link, void *osDeviceName)
2951*7836SJohn.Forte@Sun.COM {
2952*7836SJohn.Forte@Sun.COM 	if ((strncmp(IMA_DISK_DEVICE_NAME_PREFIX, di_devlink_path(link),
2953*7836SJohn.Forte@Sun.COM 	    strlen(IMA_DISK_DEVICE_NAME_PREFIX)) == 0) ||
2954*7836SJohn.Forte@Sun.COM 	    (strncmp(IMA_TAPE_DEVICE_NAME_PREFIX, di_devlink_path(link),
2955*7836SJohn.Forte@Sun.COM 	    strlen(IMA_TAPE_DEVICE_NAME_PREFIX)) == 0)) {
2956*7836SJohn.Forte@Sun.COM 		(void) mbstowcs((wchar_t *)osDeviceName, di_devlink_path(link),
2957*7836SJohn.Forte@Sun.COM 		    MAXPATHLEN);
2958*7836SJohn.Forte@Sun.COM 		return (DI_WALK_TERMINATE);
2959*7836SJohn.Forte@Sun.COM 	}
2960*7836SJohn.Forte@Sun.COM 
2961*7836SJohn.Forte@Sun.COM 	return (DI_WALK_CONTINUE);
2962*7836SJohn.Forte@Sun.COM }
2963*7836SJohn.Forte@Sun.COM 
2964*7836SJohn.Forte@Sun.COM /* ARGSUSED */
2965*7836SJohn.Forte@Sun.COM IMA_API IMA_STATUS IMA_GetPluginProperties(
2966*7836SJohn.Forte@Sun.COM 	IMA_OID pluginOid,
2967*7836SJohn.Forte@Sun.COM 	IMA_PLUGIN_PROPERTIES *pProps
2968*7836SJohn.Forte@Sun.COM )
2969*7836SJohn.Forte@Sun.COM {
2970*7836SJohn.Forte@Sun.COM 	pProps->supportedImaVersion = 1;
2971*7836SJohn.Forte@Sun.COM 	libSwprintf(pProps->vendor, L"%ls", LIBRARY_PROPERTY_VENDOR);
2972*7836SJohn.Forte@Sun.COM 	libSwprintf(pProps->implementationVersion, L"%ls",
2973*7836SJohn.Forte@Sun.COM 	    LIBRARY_PROPERTY_IMPLEMENTATION_VERSION);
2974*7836SJohn.Forte@Sun.COM 	libSwprintf(pProps->fileName, L"%ls", LIBRARY_FILE_NAME);
2975*7836SJohn.Forte@Sun.COM 	GetBuildTime(&(pProps->buildTime));
2976*7836SJohn.Forte@Sun.COM 	pProps->lhbasCanBeCreatedAndDestroyed = IMA_FALSE;
2977*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
2978*7836SJohn.Forte@Sun.COM }
2979*7836SJohn.Forte@Sun.COM 
2980*7836SJohn.Forte@Sun.COM IMA_STATUS getDiscoveryAddressPropertiesList(
2981*7836SJohn.Forte@Sun.COM     SUN_IMA_DISC_ADDR_PROP_LIST **ppList
2982*7836SJohn.Forte@Sun.COM )
2983*7836SJohn.Forte@Sun.COM {
2984*7836SJohn.Forte@Sun.COM 	int		    fd;
2985*7836SJohn.Forte@Sun.COM 	int		    i;
2986*7836SJohn.Forte@Sun.COM 	int		    discovery_addr_list_size;
2987*7836SJohn.Forte@Sun.COM 	iscsi_addr_list_t   *ialp, al_info;
2988*7836SJohn.Forte@Sun.COM 
2989*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
2990*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
2991*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
2992*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
2993*7836SJohn.Forte@Sun.COM 	}
2994*7836SJohn.Forte@Sun.COM 
2995*7836SJohn.Forte@Sun.COM 	(void) memset(&al_info, 0, sizeof (al_info));
2996*7836SJohn.Forte@Sun.COM 	al_info.al_vers = ISCSI_INTERFACE_VERSION;
2997*7836SJohn.Forte@Sun.COM 	al_info.al_in_cnt = 0;
2998*7836SJohn.Forte@Sun.COM 
2999*7836SJohn.Forte@Sun.COM 	/*
3000*7836SJohn.Forte@Sun.COM 	 * Issue ISCSI_DISCOVERY_ADDR_LIST_GET ioctl to obtain the number of
3001*7836SJohn.Forte@Sun.COM 	 * discovery addresses.
3002*7836SJohn.Forte@Sun.COM 	 */
3003*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_DISCOVERY_ADDR_LIST_GET, &al_info) != 0) {
3004*7836SJohn.Forte@Sun.COM 		(void) close(fd);
3005*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
3006*7836SJohn.Forte@Sun.COM 		    "ISCSI_DISCOVERY_ADDR_LIST_GET ioctl failed, errno: %d",
3007*7836SJohn.Forte@Sun.COM 		    errno);
3008*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
3009*7836SJohn.Forte@Sun.COM 	}
3010*7836SJohn.Forte@Sun.COM 
3011*7836SJohn.Forte@Sun.COM 	discovery_addr_list_size = sizeof (iscsi_addr_list_t);
3012*7836SJohn.Forte@Sun.COM 	if (al_info.al_out_cnt > 1) {
3013*7836SJohn.Forte@Sun.COM 		discovery_addr_list_size += (sizeof (iscsi_addr_t) *
3014*7836SJohn.Forte@Sun.COM 		    al_info.al_out_cnt - 1);
3015*7836SJohn.Forte@Sun.COM 	}
3016*7836SJohn.Forte@Sun.COM 
3017*7836SJohn.Forte@Sun.COM 	ialp = (iscsi_addr_list_t *)calloc(1, discovery_addr_list_size);
3018*7836SJohn.Forte@Sun.COM 	if (ialp == NULL) {
3019*7836SJohn.Forte@Sun.COM 		(void) close(fd);
3020*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
3021*7836SJohn.Forte@Sun.COM 	}
3022*7836SJohn.Forte@Sun.COM 	ialp->al_vers = ISCSI_INTERFACE_VERSION;
3023*7836SJohn.Forte@Sun.COM 	ialp->al_in_cnt = al_info.al_out_cnt;
3024*7836SJohn.Forte@Sun.COM 
3025*7836SJohn.Forte@Sun.COM 	/*
3026*7836SJohn.Forte@Sun.COM 	 * Issue ISCSI_DISCOVERY_ADDR_LIST_GET ioctl again to obtain the
3027*7836SJohn.Forte@Sun.COM 	 * discovery addresses.
3028*7836SJohn.Forte@Sun.COM 	 */
3029*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_DISCOVERY_ADDR_LIST_GET, ialp) != 0) {
3030*7836SJohn.Forte@Sun.COM 		free(ialp);
3031*7836SJohn.Forte@Sun.COM 		(void) close(fd);
3032*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
3033*7836SJohn.Forte@Sun.COM 		    "ISCSI_DISCOVERY_ADDR_LIST_GET ioctl failed, errno: %d",
3034*7836SJohn.Forte@Sun.COM 		    errno);
3035*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
3036*7836SJohn.Forte@Sun.COM 	}
3037*7836SJohn.Forte@Sun.COM 
3038*7836SJohn.Forte@Sun.COM 	*ppList = (SUN_IMA_DISC_ADDR_PROP_LIST *)
3039*7836SJohn.Forte@Sun.COM 	    calloc(1, sizeof (SUN_IMA_DISC_ADDR_PROP_LIST) +
3040*7836SJohn.Forte@Sun.COM 	    ialp->al_out_cnt * sizeof (IMA_DISCOVERY_ADDRESS_PROPERTIES));
3041*7836SJohn.Forte@Sun.COM 
3042*7836SJohn.Forte@Sun.COM 	if (*ppList == NULL) {
3043*7836SJohn.Forte@Sun.COM 		free(ialp);
3044*7836SJohn.Forte@Sun.COM 		(void) close(fd);
3045*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
3046*7836SJohn.Forte@Sun.COM 	}
3047*7836SJohn.Forte@Sun.COM 	(*ppList)->discAddrCount = ialp->al_out_cnt;
3048*7836SJohn.Forte@Sun.COM 
3049*7836SJohn.Forte@Sun.COM 	for (i = 0; i < ialp->al_out_cnt; i++) {
3050*7836SJohn.Forte@Sun.COM 		if (ialp->al_addrs[i].a_addr.i_insize ==
3051*7836SJohn.Forte@Sun.COM 		    sizeof (struct in_addr)) {
3052*7836SJohn.Forte@Sun.COM 			(*ppList)->props[i].discoveryAddress.hostnameIpAddress.
3053*7836SJohn.Forte@Sun.COM 			id.ipAddress.ipv4Address = IMA_TRUE;
3054*7836SJohn.Forte@Sun.COM 		} else if (ialp->al_addrs[i].a_addr.i_insize ==
3055*7836SJohn.Forte@Sun.COM 		    sizeof (struct in6_addr)) {
3056*7836SJohn.Forte@Sun.COM 			(*ppList)->props[i].discoveryAddress.
3057*7836SJohn.Forte@Sun.COM 			hostnameIpAddress.id.ipAddress.ipv4Address = IMA_FALSE;
3058*7836SJohn.Forte@Sun.COM 		} else {
3059*7836SJohn.Forte@Sun.COM 			/* Should not happen */
3060*7836SJohn.Forte@Sun.COM 			syslog(LOG_USER|LOG_DEBUG,
3061*7836SJohn.Forte@Sun.COM 			"ISCSI_STATIC_GET returned bad address");
3062*7836SJohn.Forte@Sun.COM 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
3063*7836SJohn.Forte@Sun.COM 		}
3064*7836SJohn.Forte@Sun.COM 
3065*7836SJohn.Forte@Sun.COM 		bcopy(&ialp->al_addrs[i].a_addr.i_addr,	(*ppList)->props[i].
3066*7836SJohn.Forte@Sun.COM 		    discoveryAddress.hostnameIpAddress.id.ipAddress.ipAddress,
3067*7836SJohn.Forte@Sun.COM 		    sizeof ((*ppList)->props[i].discoveryAddress.
3068*7836SJohn.Forte@Sun.COM 		    hostnameIpAddress.id.ipAddress.ipAddress));
3069*7836SJohn.Forte@Sun.COM 
3070*7836SJohn.Forte@Sun.COM 		(*ppList)->props[i].discoveryAddress.portNumber =
3071*7836SJohn.Forte@Sun.COM 		    ialp->al_addrs[i].a_port;
3072*7836SJohn.Forte@Sun.COM 	}
3073*7836SJohn.Forte@Sun.COM 
3074*7836SJohn.Forte@Sun.COM 	free(ialp);
3075*7836SJohn.Forte@Sun.COM 	(void) close(fd);
3076*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
3077*7836SJohn.Forte@Sun.COM }
3078*7836SJohn.Forte@Sun.COM 
3079*7836SJohn.Forte@Sun.COM 
3080*7836SJohn.Forte@Sun.COM /* ARGSUSED */
3081*7836SJohn.Forte@Sun.COM IMA_STATUS sendTargets(
3082*7836SJohn.Forte@Sun.COM     IMA_TARGET_ADDRESS address,
3083*7836SJohn.Forte@Sun.COM     SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES **ppList
3084*7836SJohn.Forte@Sun.COM )
3085*7836SJohn.Forte@Sun.COM {
3086*7836SJohn.Forte@Sun.COM 	char	*colonPos;
3087*7836SJohn.Forte@Sun.COM 	char	discAddrStr[SUN_IMA_IP_ADDRESS_LEN];
3088*7836SJohn.Forte@Sun.COM 	int	fd;
3089*7836SJohn.Forte@Sun.COM 	int	ctr;
3090*7836SJohn.Forte@Sun.COM 	int	stl_sz;
3091*7836SJohn.Forte@Sun.COM 	iscsi_sendtgts_list_t	*stl_hdr = NULL;
3092*7836SJohn.Forte@Sun.COM 	IMA_BOOL		retry = IMA_TRUE;
3093*7836SJohn.Forte@Sun.COM 
3094*7836SJohn.Forte@Sun.COM #define	SENDTGTS_DEFAULT_NUM_TARGETS	10
3095*7836SJohn.Forte@Sun.COM 
3096*7836SJohn.Forte@Sun.COM 	stl_sz = sizeof (*stl_hdr) + ((SENDTGTS_DEFAULT_NUM_TARGETS - 1) *
3097*7836SJohn.Forte@Sun.COM 	    sizeof (iscsi_sendtgts_entry_t));
3098*7836SJohn.Forte@Sun.COM 	stl_hdr = (iscsi_sendtgts_list_t *)calloc(1, stl_sz);
3099*7836SJohn.Forte@Sun.COM 	if (stl_hdr == NULL) {
3100*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
3101*7836SJohn.Forte@Sun.COM 	}
3102*7836SJohn.Forte@Sun.COM 	stl_hdr->stl_entry.e_vers = ISCSI_INTERFACE_VERSION;
3103*7836SJohn.Forte@Sun.COM 	stl_hdr->stl_in_cnt = SENDTGTS_DEFAULT_NUM_TARGETS;
3104*7836SJohn.Forte@Sun.COM 
3105*7836SJohn.Forte@Sun.COM 	colonPos = strchr(discAddrStr, ':');
3106*7836SJohn.Forte@Sun.COM 	if (colonPos == NULL) {
3107*7836SJohn.Forte@Sun.COM 		/* IPv4 */
3108*7836SJohn.Forte@Sun.COM 		stl_hdr->stl_entry.e_insize = sizeof (struct in_addr);
3109*7836SJohn.Forte@Sun.COM 	} else {
3110*7836SJohn.Forte@Sun.COM 		/* IPv6 */
3111*7836SJohn.Forte@Sun.COM 		stl_hdr->stl_entry.e_insize = sizeof (struct in6_addr);
3112*7836SJohn.Forte@Sun.COM 	}
3113*7836SJohn.Forte@Sun.COM 
3114*7836SJohn.Forte@Sun.COM 
3115*7836SJohn.Forte@Sun.COM 	bcopy(address.hostnameIpAddress.id.ipAddress.ipAddress,
3116*7836SJohn.Forte@Sun.COM 	    &stl_hdr->stl_entry.e_u,
3117*7836SJohn.Forte@Sun.COM 	    sizeof (address.hostnameIpAddress.id.ipAddress.ipAddress));
3118*7836SJohn.Forte@Sun.COM 	stl_hdr->stl_entry.e_port = address.portNumber;
3119*7836SJohn.Forte@Sun.COM 
3120*7836SJohn.Forte@Sun.COM 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) == -1) {
3121*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG, "Cannot open %s (%d)",
3122*7836SJohn.Forte@Sun.COM 		    ISCSI_DRIVER_DEVCTL, errno);
3123*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
3124*7836SJohn.Forte@Sun.COM 	}
3125*7836SJohn.Forte@Sun.COM 
3126*7836SJohn.Forte@Sun.COM retry_sendtgts:
3127*7836SJohn.Forte@Sun.COM 	/*
3128*7836SJohn.Forte@Sun.COM 	 * Issue ioctl to obtain the SendTargets list
3129*7836SJohn.Forte@Sun.COM 	 */
3130*7836SJohn.Forte@Sun.COM 	if (ioctl(fd, ISCSI_SENDTGTS_GET, stl_hdr) != 0) {
3131*7836SJohn.Forte@Sun.COM 		syslog(LOG_USER|LOG_DEBUG,
3132*7836SJohn.Forte@Sun.COM 		    "ISCSI_SENDTGTS_GET ioctl failed, errno: %d", errno);
3133*7836SJohn.Forte@Sun.COM 		(void) close(fd);
3134*7836SJohn.Forte@Sun.COM 		free(stl_hdr);
3135*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_UNEXPECTED_OS_ERROR);
3136*7836SJohn.Forte@Sun.COM 	}
3137*7836SJohn.Forte@Sun.COM 
3138*7836SJohn.Forte@Sun.COM 	/* check if all targets received */
3139*7836SJohn.Forte@Sun.COM 	if (stl_hdr->stl_in_cnt < stl_hdr->stl_out_cnt) {
3140*7836SJohn.Forte@Sun.COM 		if (retry == IMA_TRUE) {
3141*7836SJohn.Forte@Sun.COM 			stl_sz = sizeof (*stl_hdr) +
3142*7836SJohn.Forte@Sun.COM 			    ((stl_hdr->stl_out_cnt - 1) *
3143*7836SJohn.Forte@Sun.COM 			    sizeof (iscsi_sendtgts_entry_t));
3144*7836SJohn.Forte@Sun.COM 			stl_hdr = (iscsi_sendtgts_list_t *)
3145*7836SJohn.Forte@Sun.COM 			    realloc(stl_hdr, stl_sz);
3146*7836SJohn.Forte@Sun.COM 			if (stl_hdr == NULL) {
3147*7836SJohn.Forte@Sun.COM 				(void) close(fd);
3148*7836SJohn.Forte@Sun.COM 				return (IMA_ERROR_INSUFFICIENT_MEMORY);
3149*7836SJohn.Forte@Sun.COM 			}
3150*7836SJohn.Forte@Sun.COM 			stl_hdr->stl_in_cnt = stl_hdr->stl_out_cnt;
3151*7836SJohn.Forte@Sun.COM 			retry = IMA_FALSE;
3152*7836SJohn.Forte@Sun.COM 			goto retry_sendtgts;
3153*7836SJohn.Forte@Sun.COM 		} else {
3154*7836SJohn.Forte@Sun.COM 			/*
3155*7836SJohn.Forte@Sun.COM 			 * don't retry after 2 attempts.  The target list
3156*7836SJohn.Forte@Sun.COM 			 * shouldn't continue to growing. Justs continue
3157*7836SJohn.Forte@Sun.COM 			 * on and display what was found.
3158*7836SJohn.Forte@Sun.COM 			 */
3159*7836SJohn.Forte@Sun.COM 			syslog(LOG_USER|LOG_DEBUG,
3160*7836SJohn.Forte@Sun.COM 			    "ISCSI_SENDTGTS_GET overflow: "
3161*7836SJohn.Forte@Sun.COM 			    "failed to obtain all targets");
3162*7836SJohn.Forte@Sun.COM 			stl_hdr->stl_out_cnt = stl_hdr->stl_in_cnt;
3163*7836SJohn.Forte@Sun.COM 		}
3164*7836SJohn.Forte@Sun.COM 	}
3165*7836SJohn.Forte@Sun.COM 
3166*7836SJohn.Forte@Sun.COM 	(void) close(fd);
3167*7836SJohn.Forte@Sun.COM 
3168*7836SJohn.Forte@Sun.COM 	/* allocate for caller return buffer */
3169*7836SJohn.Forte@Sun.COM 	*ppList = (SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES *)calloc(1,
3170*7836SJohn.Forte@Sun.COM 	    sizeof (SUN_IMA_DISC_ADDRESS_KEY_PROPERTIES) +
3171*7836SJohn.Forte@Sun.COM 	    stl_hdr->stl_out_cnt * sizeof (SUN_IMA_DISC_ADDRESS_KEY));
3172*7836SJohn.Forte@Sun.COM 	if (*ppList == NULL) {
3173*7836SJohn.Forte@Sun.COM 		free(stl_hdr);
3174*7836SJohn.Forte@Sun.COM 		return (IMA_ERROR_INSUFFICIENT_MEMORY);
3175*7836SJohn.Forte@Sun.COM 	}
3176*7836SJohn.Forte@Sun.COM 
3177*7836SJohn.Forte@Sun.COM 	(*ppList)->keyCount = stl_hdr->stl_out_cnt;
3178*7836SJohn.Forte@Sun.COM 
3179*7836SJohn.Forte@Sun.COM 	for (ctr = 0; ctr < stl_hdr->stl_out_cnt; ctr++) {
3180*7836SJohn.Forte@Sun.COM 		(void) mbstowcs((*ppList)->keys[ctr].name,
3181*7836SJohn.Forte@Sun.COM 		    (char *)stl_hdr->stl_list[ctr].ste_name,
3182*7836SJohn.Forte@Sun.COM 		    IMA_NODE_NAME_LEN);
3183*7836SJohn.Forte@Sun.COM 
3184*7836SJohn.Forte@Sun.COM 		(*ppList)->keys[ctr].tpgt = stl_hdr->stl_list[ctr].ste_tpgt;
3185*7836SJohn.Forte@Sun.COM 
3186*7836SJohn.Forte@Sun.COM 		(*ppList)->keys[ctr].address.portNumber =
3187*7836SJohn.Forte@Sun.COM 		    stl_hdr->stl_list[ctr].ste_ipaddr.a_port;
3188*7836SJohn.Forte@Sun.COM 
3189*7836SJohn.Forte@Sun.COM 		if (stl_hdr->stl_list[ctr].ste_ipaddr.a_addr.i_insize ==
3190*7836SJohn.Forte@Sun.COM 		    sizeof (struct in_addr)) {
3191*7836SJohn.Forte@Sun.COM 			(*ppList)->keys[ctr].address.ipAddress.ipv4Address =
3192*7836SJohn.Forte@Sun.COM 			    IMA_TRUE;
3193*7836SJohn.Forte@Sun.COM 		} else if (stl_hdr->stl_list[ctr].ste_ipaddr.a_addr.i_insize ==
3194*7836SJohn.Forte@Sun.COM 		    sizeof (struct in6_addr)) {
3195*7836SJohn.Forte@Sun.COM 			(*ppList)->keys[ctr].address.ipAddress.ipv4Address =
3196*7836SJohn.Forte@Sun.COM 			    IMA_FALSE;
3197*7836SJohn.Forte@Sun.COM 		} else {
3198*7836SJohn.Forte@Sun.COM 			free(stl_hdr);
3199*7836SJohn.Forte@Sun.COM 			syslog(LOG_USER|LOG_DEBUG,
3200*7836SJohn.Forte@Sun.COM 			"ISCSI_STATIC_GET returned bad address");
3201*7836SJohn.Forte@Sun.COM 			return (IMA_ERROR_UNEXPECTED_OS_ERROR);
3202*7836SJohn.Forte@Sun.COM 		}
3203*7836SJohn.Forte@Sun.COM 
3204*7836SJohn.Forte@Sun.COM 
3205*7836SJohn.Forte@Sun.COM 		(void) memcpy(&(*ppList)->keys[ctr].address.ipAddress.ipAddress,
3206*7836SJohn.Forte@Sun.COM 		    &(stl_hdr->stl_list[ctr].ste_ipaddr.a_addr.i_addr),
3207*7836SJohn.Forte@Sun.COM 		    stl_hdr->stl_list[ctr].ste_ipaddr.a_addr.i_insize);
3208*7836SJohn.Forte@Sun.COM 	}
3209*7836SJohn.Forte@Sun.COM 	free(stl_hdr);
3210*7836SJohn.Forte@Sun.COM 
3211*7836SJohn.Forte@Sun.COM 	return (IMA_STATUS_SUCCESS);
3212*7836SJohn.Forte@Sun.COM }
3213