1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <string.h>
27 #include <syslog.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <stropts.h>
31 
32 
33 #include "mp_utils.h"
34 
35 
36 /*
37  *	Called by the common layer to request the plugin to set the
38  *	access state for a list of target port groups.
39  */
40 
41 MP_STATUS
MP_SetTPGAccess(MP_OID oid,MP_UINT32 count,MP_TPG_STATE_PAIR * pTpgStateList)42 MP_SetTPGAccess(MP_OID oid, MP_UINT32 count,
43 	MP_TPG_STATE_PAIR *pTpgStateList)
44 {
45 
46 	MP_TPG_STATE_PAIR *head = pTpgStateList;
47 
48 	mp_iocdata_t		mp_ioctl;
49 	mp_set_tpg_state_req_t	setTpgStateRequest;
50 
51 	int r = 0;
52 
53 	int ioctlStatus = 0;
54 
55 	MP_STATUS mpStatus = MP_STATUS_SUCCESS;
56 
57 
58 
59 	log(LOG_INFO, "MP_SetTPGAccess()", " - enter");
60 
61 
62 	if (NULL == pTpgStateList) {
63 
64 		log(LOG_INFO, "MP_SetTPGAccess()",
65 			"pTpgStateList is NULL");
66 
67 		return (MP_STATUS_INVALID_PARAMETER);
68 	}
69 
70 
71 	if (g_scsi_vhci_fd < 0) {
72 		log(LOG_INFO, "MP_SetTPGAccess()",
73 		    "invalid driver file handle");
74 		log(LOG_INFO, "MP_SetTPGAccess()", " - error exit");
75 		return (MP_STATUS_FAILED);
76 	}
77 
78 
79 	log(LOG_INFO, "MP_SetTPGAccess()",
80 		"oid.ownerId = %d",
81 		oid.ownerId);
82 
83 	log(LOG_INFO, "MP_SetTPGAccess()",
84 		"oid.objectType = %d",
85 		oid.objectType);
86 
87 	log(LOG_INFO, "MP_SetTPGAccess()",
88 		"oid.objectSequenceNumber = %llx",
89 		oid.objectSequenceNumber);
90 
91 
92 	log(LOG_INFO, "MP_SetTPGAccess()",
93 		"count = %d",
94 		count);
95 
96 
97 	for (r = 0; r < count; r++) {
98 
99 		if (head->tpgOid.ownerId != g_pluginOwnerID) {
100 
101 			log(LOG_INFO, "MP_SetTPGAccess()",
102 				"pTpgStateList->tpgOid.ownerId is not for"
103 				" this plugin");
104 
105 			log(LOG_INFO, "MP_SetTPGAccess()",
106 				"error exit");
107 
108 			return (MP_STATUS_INVALID_PARAMETER);
109 		}
110 
111 		if (head->tpgOid.objectType !=
112 		    MP_OBJECT_TYPE_TARGET_PORT_GROUP) {
113 
114 			log(LOG_INFO, "MP_SetTPGAccess()",
115 				"pTpgStateList->tpgOid.objectType is not"
116 				" MP_OBJECT_TYPE_TARGET_PORT_GROUP");
117 
118 			log(LOG_INFO, "MP_SetTPGAccess()",
119 				"error exit");
120 
121 			return (MP_STATUS_INVALID_PARAMETER);
122 		}
123 
124 
125 		head++;
126 	}
127 
128 
129 	head = pTpgStateList;
130 
131 	for (r = 0; r < count; r++) {
132 
133 		(void) memset(&mp_ioctl, 0, sizeof (mp_iocdata_t));
134 		(void) memset(&setTpgStateRequest, 0,
135 		    sizeof (mp_set_tpg_state_req_t));
136 
137 		setTpgStateRequest.desiredState
138 			= head->desiredState;
139 		setTpgStateRequest.luTpgPair.luId
140 			= oid.objectSequenceNumber;
141 		setTpgStateRequest.luTpgPair.tpgId
142 			= head->tpgOid.objectSequenceNumber;
143 
144 		mp_ioctl.mp_cmd  = MP_SET_TPG_ACCESS_STATE;
145 		mp_ioctl.mp_ibuf = (caddr_t)&setTpgStateRequest;
146 		mp_ioctl.mp_ilen = sizeof (mp_set_tpg_state_req_t);
147 		mp_ioctl.mp_xfer  = MP_XFER_WRITE;
148 
149 		log(LOG_INFO, "MP_SetTPGAccess()",
150 			"mp_ioctl.mp_cmd (MP_SET_TPG_ACCESS_STATE) : %d",
151 			mp_ioctl.mp_cmd);
152 
153 		log(LOG_INFO, "MP_SetTPGAccess()",
154 			"setTpgStateRequest.luTpgPair.luId  = %llx",
155 			setTpgStateRequest.luTpgPair.luId);
156 
157 		log(LOG_INFO, "MP_SetTPGAccess()",
158 			"setTpgStateRequest.luTpgPair.tpgId = %llx",
159 			setTpgStateRequest.luTpgPair.tpgId);
160 
161 		log(LOG_INFO, "MP_SetTPGAccess()",
162 			"setTpgStateRequest.desiredState    = %d",
163 			setTpgStateRequest.desiredState);
164 
165 		ioctlStatus = ioctl(g_scsi_vhci_fd, MP_CMD, &mp_ioctl);
166 
167 		log(LOG_INFO, "MP_SetTPGAccess()",
168 			" IOCTL call returned: %d", ioctlStatus);
169 
170 		if (ioctlStatus < 0) {
171 			ioctlStatus = errno;
172 		}
173 
174 		if (ioctlStatus != 0) {
175 			log(LOG_INFO, "MP_SetTPGAccess()",
176 				"IOCTL call failed.  IOCTL error is: %d",
177 				ioctlStatus);
178 			log(LOG_INFO, "MP_SetTPGAccess()",
179 				"IOCTL call failed.  IOCTL error is: %s",
180 				strerror(ioctlStatus));
181 			log(LOG_INFO, "MP_SetTPGAccess()",
182 				"IOCTL call failed.  mp_ioctl.mp_errno: %x",
183 				mp_ioctl.mp_errno);
184 
185 			if (ENOTSUP == ioctlStatus) {
186 				mpStatus = MP_STATUS_UNSUPPORTED;
187 			} else if (0 == mp_ioctl.mp_errno) {
188 				mpStatus = MP_STATUS_FAILED;
189 			} else {
190 				mpStatus =
191 					getStatus4ErrorCode(mp_ioctl.mp_errno);
192 			}
193 
194 			log(LOG_INFO, "MP_SetTPGAccess()",
195 				" - error exit");
196 
197 			return (mpStatus);
198 		}
199 
200 		head++;
201 	}
202 
203 
204 	log(LOG_INFO, "MP_SetTPGAccess()", " - exit");
205 
206 	return (MP_STATUS_SUCCESS);
207 }
208