1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <unistd.h>
#include <TgtFCHBA.h>
#include <Exceptions.h>
#include <Trace.h>
#include <iostream>
#include <iomanip>
#include <cerrno>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stropts.h>
#include <sys/fctio.h>
#include <sys/fibre-channel/impl/fc_error.h>
#include <TgtFCHBAPort.h>
#include <HBAList.h>
#include <sun_fc.h>
#include <cstdlib>
using namespace std;
const string TgtFCHBA::FCT_DRIVER_PATH = "/devices/pseudo/fct@0:admin";
const string TgtFCHBA::FCT_ADAPTER_NAME_PREFIX = "/devices/pseudo/fct@0";
const string TgtFCHBA::FCT_DRIVER_PKG = "SUNWfct";
const int TgtFCHBA::MAX_FCTIO_MSG_LEN = 256;
TgtFCHBA::TgtFCHBA(string path) : HBA()
{
Trace log("TgtFCHBA::TgtFCHBA");
log.debug("Constructing new Target mode HBA (%s)", path.c_str());
// Add a target FCHBA port. With fct driver architecuture, all target mode
// FCHBA will have a single port regardless of the multiport support on
// FCA layer.
addPort(new TgtFCHBAPort(path));
name = "INTERNAL-FAILURE"; // Just in case things go wrong
try {
HBA_ADAPTERATTRIBUTES attrs = getHBAAttributes();
name = attrs.Manufacturer;
name += "-";
name += attrs.Model;
name += "-Tgt";
} catch (HBAException &e) {
log.debug(
"Failed to get HBA attribute for %s", path.c_str());
throw e;
}
}
std::string TgtFCHBA::getName()
{
Trace log("TgtFCHBA::getName");
return (name);
}
HBA_ADAPTERATTRIBUTES TgtFCHBA::getHBAAttributes()
{
Trace log("TgtFCHBA::getHBAAttributes");
int fd;
errno = 0;
HBAPort *port = getPortByIndex(0);
HBA_ADAPTERATTRIBUTES attributes;
fctio_t fctio;
fc_tgt_hba_adapter_attributes_t attrs;
uint64_t portwwn;
if ((fd = open(FCT_DRIVER_PATH.c_str(), O_NDELAY | O_RDONLY)) == -1) {
// Why did we fail?
if (errno == EBUSY) {
throw BusyException();
} else if (errno == EAGAIN) {
throw TryAgainException();
} else if (errno == ENOTSUP) {
throw NotSupportedException();
} else {
throw IOError(port);
}
}
try {
std::string path = port->getPath();
string::size_type offset = path.find_last_of(".");
if (offset >= 0) {
string portwwnString = path.substr(offset+1);
portwwn = strtoull(portwwnString.c_str(), NULL, 16);
}
} catch (...) {
throw BadArgumentException();
}
uint64_t en_wwn = htonll(portwwn);
memset(&fctio, 0, sizeof (fctio));
fctio.fctio_cmd = FCTIO_GET_ADAPTER_ATTRIBUTES;
fctio.fctio_olen = (uint32_t)(sizeof (attrs));
fctio.fctio_xfer = FCTIO_XFER_READ;
fctio.fctio_obuf = (uint64_t)(uintptr_t)&attrs;
fctio.fctio_ilen = 8;
fctio.fctio_ibuf = (uint64_t)(uintptr_t)&en_wwn;
errno = 0;
if (ioctl(fd, FCTIO_CMD, &fctio) != 0) {
close(fd);
if (errno == EBUSY) {
throw BusyException();
} else if (errno == EAGAIN) {
throw TryAgainException();
} else if (errno == ENOTSUP) {
throw NotSupportedException();
} else {
throw IOError("Unable to fetch adapter attributes");
}
}
close(fd);
/* Now copy over the payload */
attributes.NumberOfPorts = attrs.NumberOfPorts;
attributes.VendorSpecificID = attrs.VendorSpecificID;
memcpy(attributes.Manufacturer, attrs.Manufacturer, 64);
memcpy(attributes.SerialNumber, attrs.SerialNumber, 64);
memcpy(attributes.Model, attrs.Model, 256);
memcpy(attributes.ModelDescription, attrs.ModelDescription, 256);
memcpy(attributes.NodeSymbolicName, attrs.NodeSymbolicName, 256);
memcpy(attributes.HardwareVersion, attrs.HardwareVersion, 256);
memcpy(attributes.DriverVersion, attrs.DriverVersion, 256);
memcpy(attributes.OptionROMVersion, attrs.OptionROMVersion, 256);
memcpy(attributes.FirmwareVersion, attrs.FirmwareVersion, 256);
memcpy(attributes.DriverName, attrs.DriverName, 256);
memcpy(&attributes.NodeWWN, &attrs.NodeWWN, 8);
return (attributes);
}
int TgtFCHBA::doForceLip()
{
Trace log("TgtFCHBA::doForceLip");
int fd;
HBAPort *port = getPortByIndex(0);
fctio_t fctio;
uint64_t portwwn;
errno = 0;
if ((fd = open(FCT_DRIVER_PATH.c_str(), O_NDELAY | O_RDONLY)) == -1) {
if (errno == EBUSY) {
throw BusyException();
} else if (errno == EAGAIN) {
throw TryAgainException();
} else if (errno == ENOTSUP) {
throw NotSupportedException();
} else {
throw IOError(port);
}
}
try {
std::string path = port->getPath();
string::size_type offset = path.find_last_of(".");
if (offset >= 0) {
string portwwnString = path.substr(offset+1);
portwwn = strtoull(portwwnString.c_str(), NULL, 16);
}
} catch (...) {
throw BadArgumentException();
}
uint64_t en_wwn = htonll(portwwn);
memset(&fctio, 0, sizeof (fctio));
fctio.fctio_cmd = FCTIO_FORCE_LIP;
fctio.fctio_xfer = FCTIO_XFER_READ;
fctio.fctio_ilen = 8;
fctio.fctio_ibuf = (uint64_t)(uintptr_t)&en_wwn;
errno = 0;
if (ioctl(fd, FCTIO_CMD, &fctio) != 0) {
close(fd);
if (errno == EBUSY) {
throw BusyException();
} else if (errno == EAGAIN) {
throw TryAgainException();
} else if (errno == ENOTSUP) {
throw NotSupportedException();
} else {
throw IOError("Unable to reinitialize the link");
}
} else {
close(fd);
return ((int)fctio.fctio_errno);
}
}
void TgtFCHBA::loadAdapters(vector<HBA*> &list)
{
Trace log("TgtFCHBA::loadAdapters");
fctio_t fctio;
fc_tgt_hba_list_t *tgthbaList;
int fd;
int size = 64; // default first attempt
bool retry = false;
struct stat sb;
int bufSize;
char wwnStr[17];
/* Before we do anything, let's see if FCT is on the system */
errno = 0;
if (stat(FCT_DRIVER_PATH.c_str(), &sb) != 0) {
if (errno == ENOENT) {
log.genericIOError(
"The %s driver is not present."
" Please install the %s package.",
FCT_DRIVER_PATH.c_str(), FCT_DRIVER_PKG.c_str());
throw NotSupportedException();
} else {
log.genericIOError(
"Can not stat the %s driver for reason \"%s\" "
"Unable to get target mode FC adapters.",
FCT_DRIVER_PATH.c_str(), strerror(errno));
throw IOError("Unable to stat FCSM driver");
}
}
/* construct fcio struct */
memset(&fctio, 0, sizeof (fctio_t));
fctio.fctio_cmd = FCTIO_ADAPTER_LIST;
fctio.fctio_xfer = FCTIO_XFER_RW;
/* open the fcsm node so we can send the ioctl to */
errno = 0;
if ((fd = open(FCT_DRIVER_PATH.c_str(), O_RDONLY)) < 0) {
if (errno == EBUSY) {
throw BusyException();
} else if (errno == EAGAIN) {
throw TryAgainException();
} else if (errno == ENOTSUP) {
throw NotSupportedException();
} else if (errno == ENOENT) {
throw UnavailableException();
} else {
throw IOError("Unable to open FCT driver");
}
}
do {
retry = false;
errno = 0;
bufSize = 8 * (size - 1) + (int) sizeof (fc_tgt_hba_list_t);
tgthbaList = (fc_tgt_hba_list_t *)new uchar_t[bufSize];
tgthbaList->numPorts = size;
fctio.fctio_olen = bufSize;
fctio.fctio_obuf = (uint64_t)(uintptr_t)tgthbaList;
if (ioctl(fd, FCTIO_CMD, &fctio) != 0) {
/* Interpret the fcio error code */
char fcioErrorString[MAX_FCTIO_MSG_LEN] = "";
log.genericIOError(
"TGT_ADAPTER_LIST failed: "
"Errno: \"%s\"",
strerror(errno));
delete (tgthbaList);
close(fd);
if (errno == EBUSY) {
throw BusyException();
} else if (errno == EAGAIN) {
throw TryAgainException();
} else if (errno == ENOTSUP) {
throw NotSupportedException();
} else if (errno == ENOENT) {
throw UnavailableException();
} else {
throw IOError("Unable to build HBA list");
}
}
if (tgthbaList->numPorts > size) {
log.debug(
"Buffer too small for number of target mode HBAs. Retrying.");
size = tgthbaList->numPorts;
retry = true;
delete (tgthbaList);
}
} while (retry);
close(fd);
log.debug("Detected %d target mode adapters", tgthbaList->numPorts);
for (int i = 0; i < tgthbaList->numPorts; i++) {
try {
std::string hbapath = FCT_ADAPTER_NAME_PREFIX.c_str();
hbapath += ".";
// move the row with two dimentional uint8 array for WWN
uint64_t tmp = ntohll(*((uint64_t *)&tgthbaList->port_wwn[i][0]));
sprintf(wwnStr, "%llx", tmp);
hbapath += wwnStr;
HBA *hba = new TgtFCHBA(hbapath);
list.insert(list.begin(), hba);
} catch (...) {
log.debug(
"Ignoring partial failure while loading an HBA");
}
}
if (tgthbaList->numPorts > HBAList::HBA_MAX_PER_LIST) {
delete(tgthbaList);
throw InternalError(
"Exceeds max number of adapters that VSL supports.");
}
delete (tgthbaList);
}
|