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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
/*
* 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 "Handle.h"
#include "Exceptions.h"
#include "Trace.h"
#include <libdevinfo.h>
#include <iostream>
#include <iomanip>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stropts.h>
#define MAX_INIT_HANDLE_ID 0x7fff
#define MAX_TGT_HANDLE_ID 0xffff
using namespace std;
/**
* Global lock for list of Handles
*/
pthread_mutex_t Handle::staticLock = PTHREAD_MUTEX_INITIALIZER;
/**
* Tracking for the previous handle we have opened
*/
HBA_HANDLE Handle::prevOpen = 0;
/**
* Tracking for the previous target HBA handle we have opened
*/
HBA_HANDLE Handle::prevTgtOpen = 0x8000;
/**
* Global map from HBA_HANDLE to Handle pointers (our global list)
*/
map<HBA_HANDLE, Handle*> Handle::openHandles;
/**
* @memo Create a new open handle for a specified HBA
* @precondition HBA port(s) must be loaded
* @postcondition An open handle will be present in the global tracking list
* and must be closed at some point to prevent leakage. If no
* handle could be assigned (the track list is full), an
* exception will be thrown. Scope for valid ids in the track
* list is [1, MAX_INIT_HANDLE_ID].
* @param myhba The HBA to open a handle for
*/
Handle::Handle(HBA *myhba) {
map<HBA_HANDLE, Handle*>::iterator mapend;
Trace log("Handle::Handle");
modeVal = INITIATOR;
lock(&staticLock);
mapend = openHandles.end();
/* Start the search for a free id from the previously assigned one */
id = prevOpen + 1;
while (id != prevOpen) {
/* Exceeds the max valid value, continue the search from 1 */
if (id > MAX_INIT_HANDLE_ID)
id = 1;
if (openHandles.find(id) == mapend) {
/* the id is not in use */
break;
}
id ++;
}
if (id == prevOpen) {
/* no usable id for now */
unlock(&staticLock);
throw TryAgainException();
}
prevOpen = id;
hba = myhba;
openHandles[id] = this;
unlock(&staticLock);
}
/**
* @memo Create a new open handle for a specified HBA
* @precondition HBA port(s) must be loaded
* @postcondition An open handle will be present in the global tracking list
* and must be closed at some point to prevent leakage. If no
* handle could be assigned (the track list is full), an
* exception will be thrown. Scope for valid ids in the track
* list is [0x8000, MAX_TGT_HANDLE_ID].
* @param myhba The HBA to open a handle for
* m The mode of HBA to open handle for
*/
#if 0
// appears unused
Handle::Handle(HBA *myhba, MODE m) {
map<HBA_HANDLE, Handle*>::iterator mapend;
Trace log("Handle::Handle");
lock(&staticLock);
modeVal = m;
// if initiator mode call constructor for initiator.
if (m == INITIATOR) {
Handle(myhba, TARGET);
}
mapend = openHandles.end();
/* Start the search for a free id from the previously assigned one */
id = prevTgtOpen + 1;
while (id != prevTgtOpen) {
/*
* Exceeds the max valid target id value,
* continue the search from 1.
*/
if (id > MAX_TGT_HANDLE_ID)
id = 0x8001;
if (openHandles.find(id) == mapend) {
/* the id is not in use */
break;
}
id ++;
}
if (id == prevTgtOpen) {
/* no usable id for now */
unlock(&staticLock);
throw TryAgainException();
}
prevTgtOpen = id;
hba = myhba;
openHandles[id] = this;
unlock(&staticLock);
}
#endif
/**
* @memo Free up the handle (aka, close it)
* @postcondition This handle will be removed from the global list
* @exception ... underlying exceptions will be thrown
*/
Handle::~Handle() {
Trace log("Handle::~Handle");
// Remove this handle from the global list
lock(&staticLock);
try {
openHandles.erase(openHandles.find(getHandle()));
unlock(&staticLock);
} catch (...) {
unlock(&staticLock);
throw;
}
// Now nuke all internal dynamic allocations
typedef map<uint64_t, HandlePort *>::const_iterator CI;
lock();
try {
for (CI port = portHandles.begin(); port != portHandles.end();
port++) {
delete port->second;
}
portHandles.clear();
unlock();
} catch (...) {
unlock();
throw;
}
}
/**
* @memo Locate a handle in the global list of open handles
* @precondition The requested handle must already be open
* @exception InvalidHandleException Thrown if the id does not match
* an open handle
* @return The open Handle
* @param id The id of the handle to fetch
*
* @doc The HBA API uses a simple integer type to represent
* an open Handle, but we use an instance of the Handle
* class. This interface allows a caller to quickly convert
* from the API integer value to related the Handle instance.
*/
Handle* Handle::findHandle(HBA_HANDLE id) {
Trace log("Handle::findHandle(id)");
Handle *tmp = NULL;
lock(&staticLock);
try {
if (openHandles.find(id) == openHandles.end()) {
throw InvalidHandleException();
}
tmp = openHandles[id];
unlock(&staticLock);
return (tmp);
} catch (...) {
unlock(&staticLock);
throw;
}
}
/**
* @memo Find an open handle based on Node or Port WWN
* @precondition The given HBA must already be open
* @exception IllegalWWNException Thrown if no matching open Handle found
* @return The open handle matching the wwn argument
* @param wwn The Node or Port WWN of the HBA whos open handle
* is requested.
*
*/
Handle* Handle::findHandle(uint64_t wwn) {
Trace log("Handle::findHandle(wwn)");
Handle *tmp = NULL;
lock(&staticLock);
try {
for (int i = 0; i < openHandles.size(); i++) {
tmp = openHandles[i];
if (tmp->getHBA()->containsWWN(wwn)) {
unlock(&staticLock);
return (tmp);
}
}
tmp = NULL;
} catch (...) { tmp = NULL; }
unlock(&staticLock);
if (tmp == NULL) {
throw IllegalWWNException();
}
return (tmp);
}
/**
* @memo Refresh underlying index values
* @postcondition All HandlePorts will be reset and prior index values
* will be undefined.
* @exception ... underlying exceptions will be thrown
*
* @doc A number of APIs in the standard interface require
* the use of index values for identifying what "thing"
* to operate on. When dynamic reconfiguration occurs
* these indexes may become inconsistent. This routine
* is called to reset the indexes and signify that the caller
* no longer holds or will refer to any old indexes.
*/
void Handle::refresh() {
Trace log("Handle::refresh");
lock();
try {
typedef map<uint64_t, HandlePort *>::const_iterator CI;
for (CI port = portHandles.begin(); port != portHandles.end();
port++) {
port->second->refresh();
}
unlock();
} catch (...) {
unlock();
throw;
}
}
/**
* @memo Close the specified handle
* @precondition The handle must be open
* @postcondition The handle will be closed and should be discarded.
* @param id The handle to close
*/
void Handle::closeHandle(HBA_HANDLE id) {
Trace log("Handle::closeHandle");
Handle *myHandle = findHandle(id);
delete myHandle;
}
/**
* @memo Get the integer value for return to the API
* @exception ... underlying exceptions will be thrown
* @return The integer value representing the handle
*
* @doc The HBA API uses integer values to represent handles.
* Call this routine to convert a Handle instance into
* its representative integer value.
*/
HBA_HANDLE Handle::getHandle() {
Trace log("Handle::getHandle");
HBA_HANDLE tmp;
lock();
try {
tmp = (HBA_HANDLE) id;
unlock();
return (tmp);
} catch (...) {
unlock();
throw;
}
}
/**
* @memo Compare two handles for equality
* @return TRUE if the handles are the same
* @return FALSE if the handles are different
*/
bool Handle::operator==(Handle comp) {
Trace log("Handle::operator==");
return (this->id == comp.id);
}
/**
* @memo Get the underlying Handle port based on index
* @return The Handle port for the given port index
* @param index The index of the desired port
*/
HandlePort* Handle::getHandlePortByIndex(int index) {
Trace log("Handle::getHandlePortByIndex");
HBAPort* port = hba->getPortByIndex(index);
return (getHandlePort(port->getPortWWN()));
}
/**
* @memo Get the underlying Handle port based on Port wwn
* @exception IllegalWWNException thrown if the wwn is not found
* @return The handle port for the specified WWN
* @param wwn The Port WWN of the HBA port
*
*/
HandlePort* Handle::getHandlePort(uint64_t wwn) {
Trace log("Handle::getHandlePort");
lock();
try {
// Check to see if the wwn is in the map
if (portHandles.find(wwn) == portHandles.end()) {
// Not found, add a new one
HBAPort* port = hba->getPort(wwn);
portHandles[wwn] = new HandlePort(this, hba, port);
}
HandlePort *portHandle = portHandles[wwn];
unlock();
return (portHandle);
} catch (...) {
unlock();
throw;
}
}
/**
* @memo Get the HBA attributes from the underlying HBA
*
* @see HBA::getHBAAttributes
*/
HBA_ADAPTERATTRIBUTES Handle::getHBAAttributes() {
Trace log("Handle::getHBAAttributes");
lock();
try {
HBA_ADAPTERATTRIBUTES attributes = hba->getHBAAttributes();
unlock();
return (attributes);
} catch (...) {
unlock();
throw;
}
}
/**
* @memo Do FORCELIP
*
* @see HBA::doForceLip
*/
int Handle::doForceLip() {
Trace log("Handle::doForceLip");
lock();
try {
int rval = hba->doForceLip();
unlock();
return (rval);
} catch (...) {
unlock();
throw;
}
}
HBA_ADAPTERATTRIBUTES Handle::npivGetHBAAttributes() {
Trace log("Handle::npivGetHBAAttributes");
lock();
try {
HBA_ADAPTERATTRIBUTES attributes = hba->npivGetHBAAttributes();
unlock();
return (attributes);
} catch (...) {
unlock();
throw;
}
}
/**
* @memo Get the HBA port attributes from the HBA
* @see HBAPort::getPortAttributes
* @see HBAPort::getDisoveredAttributes
*
* @doc This routine will return either HBA port
* attributes, or discovered port attributes
*
*/
HBA_PORTATTRIBUTES Handle::getPortAttributes(uint64_t wwn) {
Trace log("Handle::getPortAttributes");
uint64_t tmp;
HBA_PORTATTRIBUTES attributes;
lock();
try {
// Is this a WWN for one of the adapter ports?
if (hba->containsWWN(wwn)) {
attributes = hba->getPort(wwn)->getPortAttributes(tmp);
unlock();
return (attributes);
} else { // Is this a target we know about?
// Loop through all ports and look for the first match
for (int i = 0; i < hba->getNumberOfPorts(); i++) {
try {
attributes =
hba->getPortByIndex(i)->getDiscoveredAttributes(
wwn, tmp);
unlock();
return (attributes);
} catch (HBAException &e) {
continue;
}
}
// If we get to here, then we don't see this WWN on this HBA
throw IllegalWWNException();
}
} catch (...) {
unlock();
throw;
}
}
|