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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/*
* 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 "HBA.h"
#include "Exceptions.h"
#include "Trace.h"
#include <iostream>
#include <iomanip>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <stropts.h>
#include <errno.h>
#include <climits>
#include <cstring>
#define NSECS_PER_SEC 1000000000l
#define BUSY_SLEEP NSECS_PER_SEC/10 /* 1/10 second */
#define BUSY_RETRY_TIMER 3000000000UL /* Retry for 3 seconds */
using namespace std;
/**
* Max number of Adatper ports per HBA that VSL supports.
*
*/
const uint8_t HBA::HBA_PORT_MAX = UCHAR_MAX;
/**
* @memo Add a new port to this HBA
* @precondition Port must be a valid port on this HBA
* @postcondition Port will be exposed as one of the ports on this HBA
* @exception Throws InternalError when the HBA port count exceeds
* max number of ports and throws any underlying exception
* @param port The Port to add to this HBA
*
* @doc When discovering HBAs and their ports, use this
* routine to add a port to its existing HBA instance.
*/
void HBA::addPort(HBAPort* port) {
Trace log("HBA::addPort");
lock();
// support hba with up to UCHAR_MAX number of ports.
if (portsByIndex.size() + 1 > HBA_PORT_MAX) {
unlock();
throw InternalError("HBA Port count exceeds max number of ports");
}
try {
portsByWWN[port->getPortWWN()] = port;
portsByIndex.insert(portsByIndex.end(), port);
unlock();
} catch (...) {
unlock();
throw;
}
}
/**
* @memo Return number of ports to this HBA
* @exception No exception for this method.
*
* @doc Returns the number of ports on this HBA. The max
* number of ports that VSL support is up to max uint8_t
* size.
*/
uint8_t HBA::getNumberOfPorts() {
Trace log("HBA::getNumberOfPorts");
return (uint8_t)portsByIndex.size();
}
/**
* @memo Retrieve an HBA port based on a Port WWN
* @exception IllegalWWNException Thrown if WWN does not match any
* known HBA port.
* @return HBAPort* to the port with a matching Port WWN
* @param wwn The wwn of the desired HBA port
*
* @doc Fetch an HBA port based on WWN. If the port is not
* found, an exception will be thrown. NULL will never
* be returned.
*/
HBAPort* HBA::getPort(uint64_t wwn) {
Trace log("HBA::getPort");
HBAPort *port = NULL;
lock();
log.debug("getPort(wwn): WWN %016llx", wwn);
try {
// Make sure it is in the map
if (portsByWWN.find(wwn) == portsByWWN.end()) {
throw IllegalWWNException();
}
port = portsByWWN[wwn];
unlock();
return (port);
} catch (...) {
unlock();
throw;
}
}
/**
* Iterator for WWN to HBAPort map type
*/
typedef map<uint64_t, HBAPort *>::const_iterator CI;
/**
* @memo Return true if this HBA contains the stated WWN
* (node or port)
* @exception ... underlying exceptions will be thrown
* @return TRUE if the wwn is found
* @return FALSE if the wwn is not found
* @param wwn The wwn to look for
*
*/
bool HBA::containsWWN(uint64_t wwn) {
Trace log("HBA::containsWWN");
lock();
try {
for (CI port = portsByWWN.begin(); port != portsByWWN.end();
port++) {
if (port->second->getPortWWN() == wwn) {
unlock();
return (true);
}
if (port->second->getNodeWWN() == wwn) {
unlock();
return (true);
}
}
unlock();
return (false);
} catch (...) {
unlock();
throw;
}
}
/**
* @memo Fetch the port based on index.
* @exception IllegalIndexException Thrown if the index is not valid
* @return HBAPort* the port matching the index
* @param index - the zero based index of the port to retrieve
*
*/
HBAPort* HBA::getPortByIndex(int index) {
Trace log("HBA::getPortByIndex");
lock();
try {
log.debug("Port index size %d index %d ", portsByIndex.size(),
index);
if (index >= portsByIndex.size() || index < 0) {
throw IllegalIndexException();
}
HBAPort *tmp = portsByIndex[index];
unlock();
return (tmp);
} catch (...) {
unlock();
throw;
}
}
/**
* @memo Compare two HBAs for equality
* @precondition Both HBAs should be fully discovered (all ports added)
* @exception ... underlying exceptions will be thrown
* @return TRUE The two HBA instances represent the same HBA
* @return FALSE The two HBA instances are different
*
* @doc This routine will compare each port within both
* HBAs and verify they are the same. The ports must
* have been added in the same order.
*/
bool HBA::operator==(HBA &comp) {
Trace log("HBA::operator==");
lock();
try {
bool ret = false;
if (portsByIndex.size() == comp.portsByIndex.size()) {
if (portsByIndex.size() > 0) {
ret = (*portsByIndex[0] == *comp.portsByIndex[0]);
}
}
unlock();
return (ret);
} catch (...) {
unlock();
throw;
}
}
/**
* @memo Set the RNID data for all the ports in this HBA
* @precondition All ports must be added
* @postcondition Each port will have the same RNID value set
* @exception ... underlying exceptions will be thrown. Partial failure
* is possible and will not be cleaned up.
* @param info The RNID information to program for each HBA port
* @see HBAPort::setRNID
*
*/
void HBA::setRNID(HBA_MGMTINFO info) {
Trace log("HBA::setRNID");
lock();
try {
for (CI port = portsByWWN.begin(); port != portsByWWN.end();
port++) {
port->second->setRNID(info);
}
unlock();
} catch (...) {
unlock();
throw;
}
}
/**
* @memo Verify that this HBA is present on the system
* @exception UnavailableException Thrown when HBA not present
* @see HBAPort::validatePresent
*
* @doc This routine is used to verify that a given HBA
* has not been removed through dynamic reconfiguration.
* If the HBA is present, the routine will return.
* If the HBA is not present (if any port is not present)
* an exception will be thrown
*/
void HBA::validatePresent() {
Trace log("HBA::validatePresent");
lock();
try {
for (CI port = portsByWWN.begin(); port != portsByWWN.end();
port++) {
port->second->validatePresent();
}
unlock();
} catch (...) {
unlock();
throw;
}
}
/**
* Opens a file, throwing exceptions on error.
*/
int HBA::_open(std::string path, int flag) {
Trace log("HBA::open");
int fd;
errno = 0;
if ((fd = open(path.c_str(), flag)) < 0) {
log.debug("Unable to open \"%s\" - reason (%d) %s",
path.c_str(), errno, strerror(errno));
if (errno == EBUSY) {
throw BusyException();
} else if (errno == EAGAIN) {
throw TryAgainException();
} else if (errno == ENOTSUP) {
throw NotSupportedException();
} else if (errno == ENOENT) {
throw UnavailableException();
} else {
string msg = "Unable to open ";
msg += path;
throw IOError(msg);
}
}
return (fd);
}
/**
* Issues IOCTL, throwing exceptions on error.
* Note, if the IOCTL succeeds, but some IOCTL specific
* error is recorded in the response, this routine
* will not throw an exception.
*/
void HBA::_ioctl(int fd, int type, uchar_t *arg) {
Trace log("HBA::ioctl");
hrtime_t cur;
int saved_errno = 0;
struct timespec ts;
hrtime_t start = gethrtime();
hrtime_t end = start + BUSY_RETRY_TIMER;
ts.tv_sec = 0;
ts.tv_nsec = BUSY_SLEEP;
for (cur = start; cur < end; cur = gethrtime()) {
errno = 0;
if (ioctl(fd, type, arg) != 0) {
if (errno == EAGAIN) {
saved_errno = errno;
nanosleep(&ts, NULL);
continue;
} else if (errno == EBUSY) {
saved_errno = errno;
nanosleep(&ts, NULL);
continue;
} else if (errno == ENOTSUP) {
throw NotSupportedException();
} else if (errno == ENOENT) {
throw UnavailableException();
} else {
throw IOError("IOCTL failed");
}
} else {
break;
}
}
if (cur >= end) {
if (saved_errno == EAGAIN) {
throw TryAgainException();
} else if (saved_errno == EBUSY) {
throw BusyException();
} else {
throw IOError("IOCTL failed");
}
}
}
HBA::~HBA() {
Trace log("HBA::~HBA");
for (int i = 0; i < getNumberOfPorts(); i++) {
delete (getPortByIndex(i));
}
}
|