1*fcf3ce44SJohn Forte /*
2*fcf3ce44SJohn Forte  * CDDL HEADER START
3*fcf3ce44SJohn Forte  *
4*fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5*fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6*fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7*fcf3ce44SJohn Forte  *
8*fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10*fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11*fcf3ce44SJohn Forte  * and limitations under the License.
12*fcf3ce44SJohn Forte  *
13*fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14*fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16*fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17*fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18*fcf3ce44SJohn Forte  *
19*fcf3ce44SJohn Forte  * CDDL HEADER END
20*fcf3ce44SJohn Forte  */
21*fcf3ce44SJohn Forte /*
22*fcf3ce44SJohn Forte  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*fcf3ce44SJohn Forte  * Use is subject to license terms.
24*fcf3ce44SJohn Forte  */
25*fcf3ce44SJohn Forte 
26*fcf3ce44SJohn Forte #include <string.h>
27*fcf3ce44SJohn Forte #include <syslog.h>
28*fcf3ce44SJohn Forte #include <errno.h>
29*fcf3ce44SJohn Forte #include <unistd.h>
30*fcf3ce44SJohn Forte #include <stropts.h>
31*fcf3ce44SJohn Forte 
32*fcf3ce44SJohn Forte 
33*fcf3ce44SJohn Forte #include "mp_utils.h"
34*fcf3ce44SJohn Forte 
35*fcf3ce44SJohn Forte 
36*fcf3ce44SJohn Forte MP_STATUS
MP_GetTargetPortOidList(MP_OID oid,MP_OID_LIST ** ppList)37*fcf3ce44SJohn Forte MP_GetTargetPortOidList(MP_OID oid, MP_OID_LIST **ppList)
38*fcf3ce44SJohn Forte {
39*fcf3ce44SJohn Forte 	MP_STATUS mpStatus = MP_STATUS_SUCCESS;
40*fcf3ce44SJohn Forte 
41*fcf3ce44SJohn Forte 
42*fcf3ce44SJohn Forte 	log(LOG_INFO, "MP_GetTargetPortOidList()", " - enter");
43*fcf3ce44SJohn Forte 
44*fcf3ce44SJohn Forte 
45*fcf3ce44SJohn Forte 	mpStatus = getTargetPortOidList(oid, ppList);
46*fcf3ce44SJohn Forte 
47*fcf3ce44SJohn Forte 
48*fcf3ce44SJohn Forte 	log(LOG_INFO, "MP_GetTargetPortOidList()", " - exit");
49*fcf3ce44SJohn Forte 
50*fcf3ce44SJohn Forte 	return (mpStatus);
51*fcf3ce44SJohn Forte }
52*fcf3ce44SJohn Forte 
53*fcf3ce44SJohn Forte 
54*fcf3ce44SJohn Forte 
55*fcf3ce44SJohn Forte MP_STATUS
getTargetPortOidList(MP_OID oid,MP_OID_LIST ** ppList)56*fcf3ce44SJohn Forte getTargetPortOidList(MP_OID oid, MP_OID_LIST **ppList)
57*fcf3ce44SJohn Forte {
58*fcf3ce44SJohn Forte 	mp_iocdata_t mp_ioctl;
59*fcf3ce44SJohn Forte 
60*fcf3ce44SJohn Forte 	uint64_t *objList = NULL;
61*fcf3ce44SJohn Forte 
62*fcf3ce44SJohn Forte 	int numOBJ = 0;
63*fcf3ce44SJohn Forte 	int i = 0;
64*fcf3ce44SJohn Forte 	int ioctlStatus = 0;
65*fcf3ce44SJohn Forte 
66*fcf3ce44SJohn Forte 	MP_STATUS mpStatus = MP_STATUS_SUCCESS;
67*fcf3ce44SJohn Forte 
68*fcf3ce44SJohn Forte 
69*fcf3ce44SJohn Forte 	log(LOG_INFO, "getTargetPortOidList()", " - enter");
70*fcf3ce44SJohn Forte 
71*fcf3ce44SJohn Forte 
72*fcf3ce44SJohn Forte 	log(LOG_INFO, "getTargetPortOidList()",
73*fcf3ce44SJohn Forte 		"oid.objectSequenceNumber = %llx",
74*fcf3ce44SJohn Forte 		oid.objectSequenceNumber);
75*fcf3ce44SJohn Forte 
76*fcf3ce44SJohn Forte 	if (g_scsi_vhci_fd < 0) {
77*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
78*fcf3ce44SJohn Forte 		    "invalid driver file handle");
79*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()", " - error exit");
80*fcf3ce44SJohn Forte 		return (MP_STATUS_FAILED);
81*fcf3ce44SJohn Forte 	}
82*fcf3ce44SJohn Forte 
83*fcf3ce44SJohn Forte 	objList = (uint64_t *)calloc(1, DEFAULT_BUFFER_SIZE_TPG);
84*fcf3ce44SJohn Forte 	if (NULL == objList) {
85*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
86*fcf3ce44SJohn Forte 			"no memory for objList(1)");
87*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
88*fcf3ce44SJohn Forte 			" - error exit");
89*fcf3ce44SJohn Forte 		return (MP_STATUS_INSUFFICIENT_MEMORY);
90*fcf3ce44SJohn Forte 	}
91*fcf3ce44SJohn Forte 
92*fcf3ce44SJohn Forte 	(void) memset(&mp_ioctl, 0, sizeof (mp_iocdata_t));
93*fcf3ce44SJohn Forte 
94*fcf3ce44SJohn Forte 	mp_ioctl.mp_cmd  = MP_GET_TARGET_PORT_LIST_FOR_TPG;
95*fcf3ce44SJohn Forte 	mp_ioctl.mp_ibuf = (caddr_t)&oid.objectSequenceNumber;
96*fcf3ce44SJohn Forte 	mp_ioctl.mp_ilen = sizeof (oid.objectSequenceNumber);
97*fcf3ce44SJohn Forte 	mp_ioctl.mp_obuf = (caddr_t)objList;
98*fcf3ce44SJohn Forte 	mp_ioctl.mp_olen = DEFAULT_BUFFER_SIZE_TPG;
99*fcf3ce44SJohn Forte 	mp_ioctl.mp_xfer = MP_XFER_READ;
100*fcf3ce44SJohn Forte 
101*fcf3ce44SJohn Forte 	log(LOG_INFO, "getTargetPortOidList()",
102*fcf3ce44SJohn Forte 		"mp_ioctl.mp_cmd (MP_GET_TARGET_PORT_LIST_FOR_TPG): %d",
103*fcf3ce44SJohn Forte 		mp_ioctl.mp_cmd);
104*fcf3ce44SJohn Forte 	log(LOG_INFO, "getTargetPortOidList()",
105*fcf3ce44SJohn Forte 		"mp_ioctl.mp_obuf: %x", mp_ioctl.mp_obuf);
106*fcf3ce44SJohn Forte 	log(LOG_INFO, "getTargetPortOidList()",
107*fcf3ce44SJohn Forte 		"mp_ioctl.mp_olen: %d", mp_ioctl.mp_olen);
108*fcf3ce44SJohn Forte 	log(LOG_INFO, "getTargetPortOidList()",
109*fcf3ce44SJohn Forte 		"mp_ioctl.mp_xfer: %d (MP_XFER_READ)",
110*fcf3ce44SJohn Forte 		mp_ioctl.mp_xfer);
111*fcf3ce44SJohn Forte 
112*fcf3ce44SJohn Forte 	ioctlStatus = ioctl(g_scsi_vhci_fd, MP_CMD, &mp_ioctl);
113*fcf3ce44SJohn Forte 	log(LOG_INFO, "getTargetPortOidList()",
114*fcf3ce44SJohn Forte 		"ioctl call returned ioctlStatus: %d",
115*fcf3ce44SJohn Forte 		ioctlStatus);
116*fcf3ce44SJohn Forte 
117*fcf3ce44SJohn Forte 	if (ioctlStatus < 0) {
118*fcf3ce44SJohn Forte 		ioctlStatus = errno;
119*fcf3ce44SJohn Forte 	}
120*fcf3ce44SJohn Forte 
121*fcf3ce44SJohn Forte 	if ((ioctlStatus != 0) && (MP_MORE_DATA != mp_ioctl.mp_errno)) {
122*fcf3ce44SJohn Forte 
123*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
124*fcf3ce44SJohn Forte 		    "IOCTL call failed.  IOCTL error is: %d",
125*fcf3ce44SJohn Forte 			ioctlStatus);
126*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
127*fcf3ce44SJohn Forte 		    "IOCTL call failed.  IOCTL error is: %s",
128*fcf3ce44SJohn Forte 			strerror(ioctlStatus));
129*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
130*fcf3ce44SJohn Forte 		    "IOCTL call failed.  mp_ioctl.mp_errno: %x",
131*fcf3ce44SJohn Forte 			mp_ioctl.mp_errno);
132*fcf3ce44SJohn Forte 
133*fcf3ce44SJohn Forte 
134*fcf3ce44SJohn Forte 		free(objList);
135*fcf3ce44SJohn Forte 
136*fcf3ce44SJohn Forte 		if (ENOTSUP == ioctlStatus) {
137*fcf3ce44SJohn Forte 			mpStatus = MP_STATUS_UNSUPPORTED;
138*fcf3ce44SJohn Forte 		} else if (0 == mp_ioctl.mp_errno) {
139*fcf3ce44SJohn Forte 			mpStatus = MP_STATUS_FAILED;
140*fcf3ce44SJohn Forte 		} else {
141*fcf3ce44SJohn Forte 			mpStatus = getStatus4ErrorCode(mp_ioctl.mp_errno);
142*fcf3ce44SJohn Forte 		}
143*fcf3ce44SJohn Forte 
144*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
145*fcf3ce44SJohn Forte 			" - error exit");
146*fcf3ce44SJohn Forte 
147*fcf3ce44SJohn Forte 		return (mpStatus);
148*fcf3ce44SJohn Forte 	}
149*fcf3ce44SJohn Forte 
150*fcf3ce44SJohn Forte 	log(LOG_INFO, "getTargetPortOidList()",
151*fcf3ce44SJohn Forte 		" - mp_ioctl.mp_alen : %d",
152*fcf3ce44SJohn Forte 		mp_ioctl.mp_alen);
153*fcf3ce44SJohn Forte 	log(LOG_INFO, "getTargetPortOidList()",
154*fcf3ce44SJohn Forte 		" - sizeof (uint64_t): %d",
155*fcf3ce44SJohn Forte 		sizeof (uint64_t));
156*fcf3ce44SJohn Forte 
157*fcf3ce44SJohn Forte 	numOBJ = mp_ioctl.mp_alen / sizeof (uint64_t);
158*fcf3ce44SJohn Forte 	log(LOG_INFO, "getTargetPortOidList()",
159*fcf3ce44SJohn Forte 	    "Length of list: %d", numOBJ);
160*fcf3ce44SJohn Forte 
161*fcf3ce44SJohn Forte 	if (numOBJ < 1) {
162*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
163*fcf3ce44SJohn Forte 			"driver returned empty list.");
164*fcf3ce44SJohn Forte 
165*fcf3ce44SJohn Forte 		free(objList);
166*fcf3ce44SJohn Forte 
167*fcf3ce44SJohn Forte 		*ppList = createOidList(1);
168*fcf3ce44SJohn Forte 		if (NULL == *ppList) {
169*fcf3ce44SJohn Forte 			log(LOG_INFO,
170*fcf3ce44SJohn Forte 				"getTargetPortOidList()",
171*fcf3ce44SJohn Forte 				"no memory for MP_OID_LIST");
172*fcf3ce44SJohn Forte 			log(LOG_INFO,
173*fcf3ce44SJohn Forte 				"getTargetPortOidList()",
174*fcf3ce44SJohn Forte 				" - error exit");
175*fcf3ce44SJohn Forte 			return (MP_STATUS_INSUFFICIENT_MEMORY);
176*fcf3ce44SJohn Forte 		}
177*fcf3ce44SJohn Forte 
178*fcf3ce44SJohn Forte 		return (MP_STATUS_SUCCESS);
179*fcf3ce44SJohn Forte 	}
180*fcf3ce44SJohn Forte 
181*fcf3ce44SJohn Forte 	if (mp_ioctl.mp_alen > DEFAULT_BUFFER_SIZE_TPG) {
182*fcf3ce44SJohn Forte 
183*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
184*fcf3ce44SJohn Forte 			"buffer size too small, need : %d",
185*fcf3ce44SJohn Forte 			mp_ioctl.mp_alen);
186*fcf3ce44SJohn Forte 
187*fcf3ce44SJohn Forte 		free(objList);
188*fcf3ce44SJohn Forte 
189*fcf3ce44SJohn Forte 		objList = (uint64_t *)calloc(1, numOBJ * sizeof (uint64_t));
190*fcf3ce44SJohn Forte 		if (NULL == objList) {
191*fcf3ce44SJohn Forte 			log(LOG_INFO, "getTargetPortOidList()",
192*fcf3ce44SJohn Forte 				"no memory for objList(2)");
193*fcf3ce44SJohn Forte 			log(LOG_INFO, "getTargetPortOidList()",
194*fcf3ce44SJohn Forte 				" - error exit");
195*fcf3ce44SJohn Forte 			return (MP_STATUS_INSUFFICIENT_MEMORY);
196*fcf3ce44SJohn Forte 		}
197*fcf3ce44SJohn Forte 
198*fcf3ce44SJohn Forte 		(void) memset(&mp_ioctl, 0, sizeof (mp_iocdata_t));
199*fcf3ce44SJohn Forte 
200*fcf3ce44SJohn Forte 		mp_ioctl.mp_cmd  = MP_GET_TARGET_PORT_LIST_FOR_TPG;
201*fcf3ce44SJohn Forte 		mp_ioctl.mp_ibuf = (caddr_t)&oid.objectSequenceNumber;
202*fcf3ce44SJohn Forte 		mp_ioctl.mp_ilen = sizeof (oid.objectSequenceNumber);
203*fcf3ce44SJohn Forte 		mp_ioctl.mp_obuf = (caddr_t)objList;
204*fcf3ce44SJohn Forte 		mp_ioctl.mp_olen = numOBJ * sizeof (uint64_t);
205*fcf3ce44SJohn Forte 		mp_ioctl.mp_xfer = MP_XFER_READ;
206*fcf3ce44SJohn Forte 
207*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
208*fcf3ce44SJohn Forte 			"mp_ioctl.mp_cmd (MP_GET_TARGET_PORT_LIST_FOR_TPG): %d",
209*fcf3ce44SJohn Forte 			mp_ioctl.mp_cmd);
210*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
211*fcf3ce44SJohn Forte 			"mp_ioctl.mp_obuf: %x", mp_ioctl.mp_obuf);
212*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
213*fcf3ce44SJohn Forte 			"mp_ioctl.mp_olen: %d", mp_ioctl.mp_olen);
214*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
215*fcf3ce44SJohn Forte 			"mp_ioctl.mp_xfer: %d (MP_XFER_READ)",
216*fcf3ce44SJohn Forte 			mp_ioctl.mp_xfer);
217*fcf3ce44SJohn Forte 
218*fcf3ce44SJohn Forte 		ioctlStatus = ioctl(g_scsi_vhci_fd, MP_CMD, &mp_ioctl);
219*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
220*fcf3ce44SJohn Forte 			"ioctl call returned ioctlStatus: %d",
221*fcf3ce44SJohn Forte 			ioctlStatus);
222*fcf3ce44SJohn Forte 
223*fcf3ce44SJohn Forte 		if (ioctlStatus < 0) {
224*fcf3ce44SJohn Forte 			ioctlStatus = errno;
225*fcf3ce44SJohn Forte 		}
226*fcf3ce44SJohn Forte 
227*fcf3ce44SJohn Forte 		if (ioctlStatus != 0) {
228*fcf3ce44SJohn Forte 
229*fcf3ce44SJohn Forte 			log(LOG_INFO, "getTargetPortOidList()",
230*fcf3ce44SJohn Forte 				"IOCTL call failed.  IOCTL error is: %d",
231*fcf3ce44SJohn Forte 				ioctlStatus);
232*fcf3ce44SJohn Forte 			log(LOG_INFO, "getTargetPortOidList()",
233*fcf3ce44SJohn Forte 				"IOCTL call failed.  IOCTL error is: %s",
234*fcf3ce44SJohn Forte 				strerror(ioctlStatus));
235*fcf3ce44SJohn Forte 			log(LOG_INFO, "getTargetPortOidList()",
236*fcf3ce44SJohn Forte 				"IOCTL call failed.  mp_ioctl.mp_errno: %x",
237*fcf3ce44SJohn Forte 				mp_ioctl.mp_errno);
238*fcf3ce44SJohn Forte 
239*fcf3ce44SJohn Forte 
240*fcf3ce44SJohn Forte 			free(objList);
241*fcf3ce44SJohn Forte 
242*fcf3ce44SJohn Forte 			if (ENOTSUP == ioctlStatus) {
243*fcf3ce44SJohn Forte 				mpStatus = MP_STATUS_UNSUPPORTED;
244*fcf3ce44SJohn Forte 			} else if (0 == mp_ioctl.mp_errno) {
245*fcf3ce44SJohn Forte 				mpStatus = MP_STATUS_FAILED;
246*fcf3ce44SJohn Forte 			} else {
247*fcf3ce44SJohn Forte 				mpStatus =
248*fcf3ce44SJohn Forte 					getStatus4ErrorCode(mp_ioctl.mp_errno);
249*fcf3ce44SJohn Forte 			}
250*fcf3ce44SJohn Forte 
251*fcf3ce44SJohn Forte 			log(LOG_INFO, "getTargetPortOidList()",
252*fcf3ce44SJohn Forte 				" - error exit");
253*fcf3ce44SJohn Forte 
254*fcf3ce44SJohn Forte 			return (mpStatus);
255*fcf3ce44SJohn Forte 		}
256*fcf3ce44SJohn Forte 	}
257*fcf3ce44SJohn Forte 
258*fcf3ce44SJohn Forte 
259*fcf3ce44SJohn Forte 	*ppList = createOidList(numOBJ);
260*fcf3ce44SJohn Forte 	if (NULL == *ppList) {
261*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
262*fcf3ce44SJohn Forte 			"no memory for *ppList");
263*fcf3ce44SJohn Forte 		free(objList);
264*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
265*fcf3ce44SJohn Forte 			" - error exit");
266*fcf3ce44SJohn Forte 		return (MP_STATUS_INSUFFICIENT_MEMORY);
267*fcf3ce44SJohn Forte 	}
268*fcf3ce44SJohn Forte 
269*fcf3ce44SJohn Forte 	(*ppList)->oidCount = numOBJ;
270*fcf3ce44SJohn Forte 
271*fcf3ce44SJohn Forte 	log(LOG_INFO, "getTargetPortOidList()",
272*fcf3ce44SJohn Forte 		"(*ppList)->oidCount = %d",
273*fcf3ce44SJohn Forte 		(*ppList)->oidCount);
274*fcf3ce44SJohn Forte 
275*fcf3ce44SJohn Forte 	for (i = 0; i < numOBJ; i++) {
276*fcf3ce44SJohn Forte 		(*ppList)->oids[i].objectType = MP_OBJECT_TYPE_TARGET_PORT;
277*fcf3ce44SJohn Forte 		(*ppList)->oids[i].ownerId = g_pluginOwnerID;
278*fcf3ce44SJohn Forte 		(*ppList)->oids[i].objectSequenceNumber = objList[i];
279*fcf3ce44SJohn Forte 
280*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
281*fcf3ce44SJohn Forte 			"(*ppList)->oids[%d].objectType           = %d",
282*fcf3ce44SJohn Forte 			i, (*ppList)->oids[i].objectType);
283*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
284*fcf3ce44SJohn Forte 			"(*ppList)->oids[%d].ownerId              = %d",
285*fcf3ce44SJohn Forte 			i, (*ppList)->oids[i].ownerId);
286*fcf3ce44SJohn Forte 		log(LOG_INFO, "getTargetPortOidList()",
287*fcf3ce44SJohn Forte 			"(*ppList)->oids[%d].objectSequenceNumber = %llx",
288*fcf3ce44SJohn Forte 			i, (*ppList)->oids[i].objectSequenceNumber);
289*fcf3ce44SJohn Forte 	}
290*fcf3ce44SJohn Forte 
291*fcf3ce44SJohn Forte 	free(objList);
292*fcf3ce44SJohn Forte 
293*fcf3ce44SJohn Forte 
294*fcf3ce44SJohn Forte 	log(LOG_INFO, "getTargetPortOidList()", " - exit");
295*fcf3ce44SJohn Forte 
296*fcf3ce44SJohn Forte 	return (MP_STATUS_SUCCESS);
297*fcf3ce44SJohn Forte }
298