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
27*7836SJohn.Forte@Sun.COM #include "FCHBANPIVPort.h"
28*7836SJohn.Forte@Sun.COM #include <Exceptions.h>
29*7836SJohn.Forte@Sun.COM #include <Trace.h>
30*7836SJohn.Forte@Sun.COM #include <sun_fc.h>
31*7836SJohn.Forte@Sun.COM #include <iostream>
32*7836SJohn.Forte@Sun.COM #include <iomanip>
33*7836SJohn.Forte@Sun.COM #include <sys/types.h>
34*7836SJohn.Forte@Sun.COM #include <sys/mkdev.h>
35*7836SJohn.Forte@Sun.COM #include <sys/stat.h>
36*7836SJohn.Forte@Sun.COM #include <fcntl.h>
37*7836SJohn.Forte@Sun.COM #include <unistd.h>
38*7836SJohn.Forte@Sun.COM #include <stropts.h>
39*7836SJohn.Forte@Sun.COM #include <dirent.h>
40*7836SJohn.Forte@Sun.COM #include <sys/fibre-channel/fc.h>
41*7836SJohn.Forte@Sun.COM #include <sys/fibre-channel/fcio.h>
42*7836SJohn.Forte@Sun.COM #include <sys/fibre-channel/ulp/fcp_util.h>
43*7836SJohn.Forte@Sun.COM #include <sys/fibre-channel/ulp/fcsm.h>
44*7836SJohn.Forte@Sun.COM #include <sys/fibre-channel/impl/fc_error.h>
45*7836SJohn.Forte@Sun.COM #include <sys/fibre-channel/fc_appif.h>
46*7836SJohn.Forte@Sun.COM #include <sys/scsi/generic/commands.h>
47*7836SJohn.Forte@Sun.COM #include <sys/scsi/impl/commands.h>
48*7836SJohn.Forte@Sun.COM #include <sys/scsi/impl/sense.h>
49*7836SJohn.Forte@Sun.COM #include <sys/scsi/generic/inquiry.h>
50*7836SJohn.Forte@Sun.COM #include <sys/scsi/generic/status.h>
51*7836SJohn.Forte@Sun.COM #include <errno.h>
52*7836SJohn.Forte@Sun.COM
53*7836SJohn.Forte@Sun.COM using namespace std;
54*7836SJohn.Forte@Sun.COM
55*7836SJohn.Forte@Sun.COM const int FCHBANPIVPort::MAX_FCIO_MSG_LEN = 256;
56*7836SJohn.Forte@Sun.COM
FCHBANPIVPort(string thePath)57*7836SJohn.Forte@Sun.COM FCHBANPIVPort::FCHBANPIVPort(string thePath) : HBANPIVPort() {
58*7836SJohn.Forte@Sun.COM Trace log("FCHBANPIVPort::FCHBANPIVPort");
59*7836SJohn.Forte@Sun.COM log.debug("Initializing HBA NPIV port %s", thePath.c_str());
60*7836SJohn.Forte@Sun.COM
61*7836SJohn.Forte@Sun.COM try {
62*7836SJohn.Forte@Sun.COM path = lookupControllerPath(thePath);
63*7836SJohn.Forte@Sun.COM } catch (...) {
64*7836SJohn.Forte@Sun.COM log.debug("Unable to lookup controller path and number for %s",
65*7836SJohn.Forte@Sun.COM thePath.c_str());
66*7836SJohn.Forte@Sun.COM path = "/devices";
67*7836SJohn.Forte@Sun.COM path += thePath;
68*7836SJohn.Forte@Sun.COM path += ":fc";
69*7836SJohn.Forte@Sun.COM }
70*7836SJohn.Forte@Sun.COM
71*7836SJohn.Forte@Sun.COM uint64_t tmp;
72*7836SJohn.Forte@Sun.COM HBA_NPIVATTRIBUTES attrs = getPortAttributes(tmp);
73*7836SJohn.Forte@Sun.COM memcpy(&tmp, &attrs.PortWWN, 8);
74*7836SJohn.Forte@Sun.COM portWWN = ntohll(tmp);
75*7836SJohn.Forte@Sun.COM memcpy(&tmp, &attrs.NodeWWN, 8);
76*7836SJohn.Forte@Sun.COM nodeWWN = ntohll(tmp);
77*7836SJohn.Forte@Sun.COM }
78*7836SJohn.Forte@Sun.COM
79*7836SJohn.Forte@Sun.COM
getPortAttributes(uint64_t & stateChange)80*7836SJohn.Forte@Sun.COM HBA_NPIVATTRIBUTES FCHBANPIVPort::getPortAttributes(uint64_t &stateChange) {
81*7836SJohn.Forte@Sun.COM Trace log("FCHBANPIVPort::getPortAttributes");
82*7836SJohn.Forte@Sun.COM
83*7836SJohn.Forte@Sun.COM HBA_NPIVATTRIBUTES attributes;
84*7836SJohn.Forte@Sun.COM fcio_t fcio;
85*7836SJohn.Forte@Sun.COM fc_hba_npiv_attributes_t attrs;
86*7836SJohn.Forte@Sun.COM
87*7836SJohn.Forte@Sun.COM memset(&fcio, 0, sizeof (fcio));
88*7836SJohn.Forte@Sun.COM memset(&attributes, 0, sizeof (attributes));
89*7836SJohn.Forte@Sun.COM fcio.fcio_cmd = FCIO_GET_NPIV_ATTRIBUTES;
90*7836SJohn.Forte@Sun.COM fcio.fcio_olen = sizeof (attrs);
91*7836SJohn.Forte@Sun.COM fcio.fcio_xfer = FCIO_XFER_READ;
92*7836SJohn.Forte@Sun.COM fcio.fcio_obuf = (caddr_t)&attrs;
93*7836SJohn.Forte@Sun.COM fp_ioctl(getPath(), FCIO_CMD, &fcio);
94*7836SJohn.Forte@Sun.COM
95*7836SJohn.Forte@Sun.COM stateChange = attrs.lastChange;
96*7836SJohn.Forte@Sun.COM memcpy(&attributes.NodeWWN, &attrs.NodeWWN, 8);
97*7836SJohn.Forte@Sun.COM memcpy(&attributes.PortWWN, &attrs.PortWWN, 8);
98*7836SJohn.Forte@Sun.COM
99*7836SJohn.Forte@Sun.COM return (attributes);
100*7836SJohn.Forte@Sun.COM }
101*7836SJohn.Forte@Sun.COM
102*7836SJohn.Forte@Sun.COM
fp_ioctl(string path,int cmd,fcio_t * fcio)103*7836SJohn.Forte@Sun.COM void FCHBANPIVPort::fp_ioctl(string path, int cmd, fcio_t *fcio) {
104*7836SJohn.Forte@Sun.COM Trace log("FCHBANPIVPort::fp_ioctl");
105*7836SJohn.Forte@Sun.COM
106*7836SJohn.Forte@Sun.COM char fcioErrorString[MAX_FCIO_MSG_LEN] = "";
107*7836SJohn.Forte@Sun.COM int fd = HBA::_open(path, O_NDELAY | O_RDONLY);
108*7836SJohn.Forte@Sun.COM
109*7836SJohn.Forte@Sun.COM try {
110*7836SJohn.Forte@Sun.COM int times = 0;
111*7836SJohn.Forte@Sun.COM HBA::_ioctl(fd, cmd, (uchar_t *)fcio);
112*7836SJohn.Forte@Sun.COM while (fcio->fcio_errno == FC_STATEC_BUSY) {
113*7836SJohn.Forte@Sun.COM (void) sleep(2);
114*7836SJohn.Forte@Sun.COM HBA::_ioctl(fd, cmd, (uchar_t *)fcio);
115*7836SJohn.Forte@Sun.COM if (times++ > 20) {
116*7836SJohn.Forte@Sun.COM break;
117*7836SJohn.Forte@Sun.COM }
118*7836SJohn.Forte@Sun.COM }
119*7836SJohn.Forte@Sun.COM close(fd);
120*7836SJohn.Forte@Sun.COM if (fcio->fcio_errno) {
121*7836SJohn.Forte@Sun.COM throw IOError("IOCTL transport failure");
122*7836SJohn.Forte@Sun.COM }
123*7836SJohn.Forte@Sun.COM } catch (...) {
124*7836SJohn.Forte@Sun.COM close(fd);
125*7836SJohn.Forte@Sun.COM transportError(fcio->fcio_errno, fcioErrorString);
126*7836SJohn.Forte@Sun.COM log.genericIOError("NPIV Port ioctl (0x%x) failed. Transport: \"%s\"", cmd,
127*7836SJohn.Forte@Sun.COM fcioErrorString);
128*7836SJohn.Forte@Sun.COM switch (fcio->fcio_errno) {
129*7836SJohn.Forte@Sun.COM case FC_BADWWN:
130*7836SJohn.Forte@Sun.COM throw IllegalWWNException();
131*7836SJohn.Forte@Sun.COM case FC_BADPORT:
132*7836SJohn.Forte@Sun.COM throw IllegalWWNException();
133*7836SJohn.Forte@Sun.COM case FC_OUTOFBOUNDS:
134*7836SJohn.Forte@Sun.COM throw IllegalIndexException();
135*7836SJohn.Forte@Sun.COM case FC_PBUSY:
136*7836SJohn.Forte@Sun.COM case FC_FBUSY:
137*7836SJohn.Forte@Sun.COM case FC_TRAN_BUSY:
138*7836SJohn.Forte@Sun.COM case FC_STATEC_BUSY:
139*7836SJohn.Forte@Sun.COM case FC_DEVICE_BUSY:
140*7836SJohn.Forte@Sun.COM throw BusyException();
141*7836SJohn.Forte@Sun.COM case FC_SUCCESS:
142*7836SJohn.Forte@Sun.COM default:
143*7836SJohn.Forte@Sun.COM throw;
144*7836SJohn.Forte@Sun.COM }
145*7836SJohn.Forte@Sun.COM }
146*7836SJohn.Forte@Sun.COM }
147*7836SJohn.Forte@Sun.COM
148*7836SJohn.Forte@Sun.COM
149*7836SJohn.Forte@Sun.COM /*
150*7836SJohn.Forte@Sun.COM * Interpret the error code in the fcio_t structure
151*7836SJohn.Forte@Sun.COM *
152*7836SJohn.Forte@Sun.COM * message must be at least MAX_FCIO_MSG_LEN in length.
153*7836SJohn.Forte@Sun.COM */
154*7836SJohn.Forte@Sun.COM void
transportError(uint32_t fcio_errno,char * message)155*7836SJohn.Forte@Sun.COM FCHBANPIVPort::transportError(uint32_t fcio_errno, char *message) {
156*7836SJohn.Forte@Sun.COM Trace log("transportError");
157*7836SJohn.Forte@Sun.COM
158*7836SJohn.Forte@Sun.COM string fcioErrorString;
159*7836SJohn.Forte@Sun.COM if (message == NULL) {
160*7836SJohn.Forte@Sun.COM log.internalError("NULL routine argument");
161*7836SJohn.Forte@Sun.COM return;
162*7836SJohn.Forte@Sun.COM }
163*7836SJohn.Forte@Sun.COM switch (fcio_errno) {
164*7836SJohn.Forte@Sun.COM case (uint32_t)FC_FAILURE:
165*7836SJohn.Forte@Sun.COM fcioErrorString = "general failure";
166*7836SJohn.Forte@Sun.COM break;
167*7836SJohn.Forte@Sun.COM case (uint32_t)FC_FAILURE_SILENT:
168*7836SJohn.Forte@Sun.COM fcioErrorString = "general failure but fail silently";
169*7836SJohn.Forte@Sun.COM break;
170*7836SJohn.Forte@Sun.COM case FC_SUCCESS:
171*7836SJohn.Forte@Sun.COM fcioErrorString = "successful completion";
172*7836SJohn.Forte@Sun.COM break;
173*7836SJohn.Forte@Sun.COM case FC_CAP_ERROR:
174*7836SJohn.Forte@Sun.COM fcioErrorString = "FCA capability error";
175*7836SJohn.Forte@Sun.COM break;
176*7836SJohn.Forte@Sun.COM case FC_CAP_FOUND:
177*7836SJohn.Forte@Sun.COM fcioErrorString = "FCA capability unsettable";
178*7836SJohn.Forte@Sun.COM break;
179*7836SJohn.Forte@Sun.COM case FC_CAP_SETTABLE:
180*7836SJohn.Forte@Sun.COM fcioErrorString = "FCA capability settable";
181*7836SJohn.Forte@Sun.COM break;
182*7836SJohn.Forte@Sun.COM case FC_UNBOUND:
183*7836SJohn.Forte@Sun.COM fcioErrorString = "unbound stuff";
184*7836SJohn.Forte@Sun.COM break;
185*7836SJohn.Forte@Sun.COM case FC_NOMEM:
186*7836SJohn.Forte@Sun.COM fcioErrorString = "allocation error";
187*7836SJohn.Forte@Sun.COM break;
188*7836SJohn.Forte@Sun.COM case FC_BADPACKET:
189*7836SJohn.Forte@Sun.COM fcioErrorString = "invalid packet specified/supplied";
190*7836SJohn.Forte@Sun.COM break;
191*7836SJohn.Forte@Sun.COM case FC_OFFLINE:
192*7836SJohn.Forte@Sun.COM fcioErrorString = "I/O resource unavailable";
193*7836SJohn.Forte@Sun.COM break;
194*7836SJohn.Forte@Sun.COM case FC_OLDPORT:
195*7836SJohn.Forte@Sun.COM fcioErrorString = "operation on non-loop port";
196*7836SJohn.Forte@Sun.COM break;
197*7836SJohn.Forte@Sun.COM case FC_NO_MAP:
198*7836SJohn.Forte@Sun.COM fcioErrorString = "requested map unavailable";
199*7836SJohn.Forte@Sun.COM break;
200*7836SJohn.Forte@Sun.COM case FC_TRANSPORT_ERROR:
201*7836SJohn.Forte@Sun.COM fcioErrorString = "unable to transport I/O";
202*7836SJohn.Forte@Sun.COM break;
203*7836SJohn.Forte@Sun.COM case FC_ELS_FREJECT:
204*7836SJohn.Forte@Sun.COM fcioErrorString = "ELS rejected by a Fabric";
205*7836SJohn.Forte@Sun.COM break;
206*7836SJohn.Forte@Sun.COM case FC_ELS_PREJECT:
207*7836SJohn.Forte@Sun.COM fcioErrorString = "ELS rejected by an N_port";
208*7836SJohn.Forte@Sun.COM break;
209*7836SJohn.Forte@Sun.COM case FC_ELS_BAD:
210*7836SJohn.Forte@Sun.COM fcioErrorString = "ELS rejected by FCA/fctl";
211*7836SJohn.Forte@Sun.COM break;
212*7836SJohn.Forte@Sun.COM case FC_ELS_MALFORMED:
213*7836SJohn.Forte@Sun.COM fcioErrorString = "poorly formed ELS request";
214*7836SJohn.Forte@Sun.COM break;
215*7836SJohn.Forte@Sun.COM case FC_TOOMANY:
216*7836SJohn.Forte@Sun.COM fcioErrorString = "resource request too large";
217*7836SJohn.Forte@Sun.COM break;
218*7836SJohn.Forte@Sun.COM case FC_UB_BADTOKEN:
219*7836SJohn.Forte@Sun.COM fcioErrorString = "invalid unsolicited buffer token";
220*7836SJohn.Forte@Sun.COM break;
221*7836SJohn.Forte@Sun.COM case FC_UB_ERROR:
222*7836SJohn.Forte@Sun.COM fcioErrorString = "invalid unsol buf request";
223*7836SJohn.Forte@Sun.COM break;
224*7836SJohn.Forte@Sun.COM case FC_UB_BUSY:
225*7836SJohn.Forte@Sun.COM fcioErrorString = "buffer already in use";
226*7836SJohn.Forte@Sun.COM break;
227*7836SJohn.Forte@Sun.COM case FC_BADULP:
228*7836SJohn.Forte@Sun.COM fcioErrorString = "Unknown ulp";
229*7836SJohn.Forte@Sun.COM break;
230*7836SJohn.Forte@Sun.COM case FC_BADTYPE:
231*7836SJohn.Forte@Sun.COM fcioErrorString = "ULP not registered to handle this FC4 type";
232*7836SJohn.Forte@Sun.COM break;
233*7836SJohn.Forte@Sun.COM case FC_UNCLAIMED:
234*7836SJohn.Forte@Sun.COM fcioErrorString = "request or data not claimed";
235*7836SJohn.Forte@Sun.COM break;
236*7836SJohn.Forte@Sun.COM case FC_ULP_SAMEMODULE:
237*7836SJohn.Forte@Sun.COM fcioErrorString = "module already in use";
238*7836SJohn.Forte@Sun.COM break;
239*7836SJohn.Forte@Sun.COM case FC_ULP_SAMETYPE:
240*7836SJohn.Forte@Sun.COM fcioErrorString = "FC4 module already in use";
241*7836SJohn.Forte@Sun.COM break;
242*7836SJohn.Forte@Sun.COM case FC_ABORTED:
243*7836SJohn.Forte@Sun.COM fcioErrorString = "request aborted";
244*7836SJohn.Forte@Sun.COM break;
245*7836SJohn.Forte@Sun.COM case FC_ABORT_FAILED:
246*7836SJohn.Forte@Sun.COM fcioErrorString = "abort request failed";
247*7836SJohn.Forte@Sun.COM break;
248*7836SJohn.Forte@Sun.COM case FC_BADEXCHANGE:
249*7836SJohn.Forte@Sun.COM fcioErrorString = "exchange doesn\325t exist";
250*7836SJohn.Forte@Sun.COM break;
251*7836SJohn.Forte@Sun.COM case FC_BADWWN:
252*7836SJohn.Forte@Sun.COM fcioErrorString = "WWN not recognized";
253*7836SJohn.Forte@Sun.COM break;
254*7836SJohn.Forte@Sun.COM case FC_BADDEV:
255*7836SJohn.Forte@Sun.COM fcioErrorString = "device unrecognized";
256*7836SJohn.Forte@Sun.COM break;
257*7836SJohn.Forte@Sun.COM case FC_BADCMD:
258*7836SJohn.Forte@Sun.COM fcioErrorString = "invalid command issued";
259*7836SJohn.Forte@Sun.COM break;
260*7836SJohn.Forte@Sun.COM case FC_BADOBJECT:
261*7836SJohn.Forte@Sun.COM fcioErrorString = "invalid object requested";
262*7836SJohn.Forte@Sun.COM break;
263*7836SJohn.Forte@Sun.COM case FC_BADPORT:
264*7836SJohn.Forte@Sun.COM fcioErrorString = "invalid port specified";
265*7836SJohn.Forte@Sun.COM break;
266*7836SJohn.Forte@Sun.COM case FC_NOTTHISPORT:
267*7836SJohn.Forte@Sun.COM fcioErrorString = "resource not at this port";
268*7836SJohn.Forte@Sun.COM break;
269*7836SJohn.Forte@Sun.COM case FC_PREJECT:
270*7836SJohn.Forte@Sun.COM fcioErrorString = "reject at remote N_Port";
271*7836SJohn.Forte@Sun.COM break;
272*7836SJohn.Forte@Sun.COM case FC_FREJECT:
273*7836SJohn.Forte@Sun.COM fcioErrorString = "reject at remote Fabric";
274*7836SJohn.Forte@Sun.COM break;
275*7836SJohn.Forte@Sun.COM case FC_PBUSY:
276*7836SJohn.Forte@Sun.COM fcioErrorString = "remote N_Port busy";
277*7836SJohn.Forte@Sun.COM break;
278*7836SJohn.Forte@Sun.COM case FC_FBUSY:
279*7836SJohn.Forte@Sun.COM fcioErrorString = "remote Fabric busy";
280*7836SJohn.Forte@Sun.COM break;
281*7836SJohn.Forte@Sun.COM case FC_ALREADY:
282*7836SJohn.Forte@Sun.COM fcioErrorString = "already logged in";
283*7836SJohn.Forte@Sun.COM break;
284*7836SJohn.Forte@Sun.COM case FC_LOGINREQ:
285*7836SJohn.Forte@Sun.COM fcioErrorString = "login required";
286*7836SJohn.Forte@Sun.COM break;
287*7836SJohn.Forte@Sun.COM case FC_RESETFAIL:
288*7836SJohn.Forte@Sun.COM fcioErrorString = "reset failed";
289*7836SJohn.Forte@Sun.COM break;
290*7836SJohn.Forte@Sun.COM case FC_INVALID_REQUEST:
291*7836SJohn.Forte@Sun.COM fcioErrorString = "request is invalid";
292*7836SJohn.Forte@Sun.COM break;
293*7836SJohn.Forte@Sun.COM case FC_OUTOFBOUNDS:
294*7836SJohn.Forte@Sun.COM fcioErrorString = "port number is out of bounds";
295*7836SJohn.Forte@Sun.COM break;
296*7836SJohn.Forte@Sun.COM case FC_TRAN_BUSY:
297*7836SJohn.Forte@Sun.COM fcioErrorString = "command transport busy";
298*7836SJohn.Forte@Sun.COM break;
299*7836SJohn.Forte@Sun.COM case FC_STATEC_BUSY:
300*7836SJohn.Forte@Sun.COM fcioErrorString = "port driver currently busy";
301*7836SJohn.Forte@Sun.COM break;
302*7836SJohn.Forte@Sun.COM case FC_DEVICE_BUSY:
303*7836SJohn.Forte@Sun.COM fcioErrorString = "transport working on this device";
304*7836SJohn.Forte@Sun.COM break;
305*7836SJohn.Forte@Sun.COM case FC_DEVICE_NOT_TGT:
306*7836SJohn.Forte@Sun.COM fcioErrorString = "device is not a SCSI target";
307*7836SJohn.Forte@Sun.COM break;
308*7836SJohn.Forte@Sun.COM default:
309*7836SJohn.Forte@Sun.COM snprintf(message, MAX_FCIO_MSG_LEN, "Unknown error code 0x%x",
310*7836SJohn.Forte@Sun.COM fcio_errno);
311*7836SJohn.Forte@Sun.COM return;
312*7836SJohn.Forte@Sun.COM }
313*7836SJohn.Forte@Sun.COM snprintf(message, MAX_FCIO_MSG_LEN, "%s", fcioErrorString.c_str());
314*7836SJohn.Forte@Sun.COM }
315*7836SJohn.Forte@Sun.COM
316