-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapt.c
417 lines (370 loc) · 8.25 KB
/
adapt.c
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
/*
* Copyright (c) 2008, Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU Lesser General Public License,
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "utils.h"
#include "api_lib.h"
#include "adapt_impl.h"
static struct sa_table adapter_table;
static const u_int32_t adapter_handle_offset = 0x100;
#define HBA_SHORT_NAME_LIMIT 64
/*
* Support for adapter information.
*/
HBA_UINT32
adapter_get_count(void)
{
return adapter_table.st_limit;
}
/*
* Get adapter name.
*/
HBA_STATUS
adapter_get_name(HBA_UINT32 index, char *buf)
{
HBA_STATUS status;
struct adapter_info *ap;
status = HBA_STATUS_ERROR_ILLEGAL_INDEX;
ap = sa_table_lookup(&adapter_table, index);
if (ap != NULL) {
snprintf(buf, HBA_SHORT_NAME_LIMIT,
"%s-%u", ap->ad_name, index);
status = HBA_STATUS_OK;
}
return status;
}
/*
* Add an adapter to the table.
*/
HBA_STATUS
adapter_create(struct adapter_info *ap)
{
int index;
index = sa_table_append(&adapter_table, ap);
if (index < 0)
return HBA_STATUS_ERROR;
ap->ad_index = index;
return HBA_STATUS_OK;
}
void
adapter_destroy(struct adapter_info *ap)
{
sa_table_destroy_all(&ap->ad_ports);
free(ap);
}
void
adapter_destroy_all(void)
{
struct adapter_info *ap;
int i;
for (i = 0; i < adapter_table.st_limit; i++) {
ap = adapter_table.st_table[i];
if (ap) {
adapter_table.st_table[i] = NULL;
adapter_destroy(ap);
}
}
sa_table_destroy(&adapter_table);
}
struct adapter_info *
adapter_open_handle(HBA_HANDLE handle)
{
return sa_table_lookup(&adapter_table, handle -
adapter_handle_offset);
}
struct port_info *
adapter_get_port(HBA_HANDLE handle, HBA_UINT32 port)
{
struct adapter_info *ap;
struct port_info *pp = NULL;
ap = adapter_open_handle(handle);
if (ap)
pp = sa_table_lookup(&ap->ad_ports, port);
return pp;
}
struct port_info *
adapter_get_rport(HBA_HANDLE handle, HBA_UINT32 port, HBA_UINT32 rport)
{
struct port_info *pp;
struct port_info *rp = NULL;
pp = adapter_get_port(handle, port);
if (pp) {
get_rport_info(pp);
rp = sa_table_lookup(&pp->ap_rports, rport);
}
return rp;
}
/*
* Get the Nth discovered port information.
*/
struct port_info *
adapter_get_rport_n(HBA_HANDLE handle, HBA_UINT32 port, HBA_UINT32 n)
{
struct port_info *pp;
struct port_info *rp = NULL;
pp = adapter_get_port(handle, port);
if (pp) {
get_rport_info(pp);
rp = sa_table_lookup_n(&pp->ap_rports, n);
}
return rp;
}
static void *
adapter_target_match(void *rp_arg, void *target_arg)
{
struct port_info *rp = rp_arg;
if (rp->ap_scsi_target != *(u_int32_t *)target_arg)
rp_arg = NULL;
return rp_arg;
}
/*
* Get the rport by scsi_target number.
*/
struct port_info *
adapter_get_rport_target(HBA_HANDLE handle, HBA_UINT32 port, HBA_UINT32 n)
{
struct port_info *pp;
struct port_info *rp = NULL;
pp = adapter_get_port(handle, port);
if (pp) {
get_rport_info(pp);
rp = sa_table_search(&pp->ap_rports,
adapter_target_match, &n);
}
return rp;
}
static void *
adapter_wwpn_match(void *rp_arg, void *wwpn_arg)
{
struct port_info *rp = rp_arg;
if (memcmp(&rp->ap_attr.PortWWN, wwpn_arg, sizeof(HBA_WWN)) != 0)
rp_arg = NULL;
return rp_arg;
}
struct port_info *
adapter_get_rport_by_wwn(struct port_info *pp, HBA_WWN wwpn)
{
struct port_info *rp;
get_rport_info(pp);
rp = sa_table_search(&pp->ap_rports, adapter_wwpn_match, &wwpn);
return rp;
}
static void *
adapter_fcid_match(void *rp_arg, void *fcid_arg)
{
struct port_info *rp = rp_arg;
if (rp->ap_attr.PortFcId != *(fc_fid_t *)fcid_arg)
rp_arg = NULL;
return rp_arg;
}
struct port_info *
adapter_get_rport_by_fcid(struct port_info *pp, fc_fid_t fcid)
{
struct port_info *rp;
get_rport_info(pp);
rp = sa_table_search(&pp->ap_rports, adapter_fcid_match, &fcid);
return rp;
}
/*
* Open adapter by name.
*/
HBA_HANDLE
adapter_open(char *name)
{
char buf[256];
HBA_HANDLE i;
HBA_STATUS status;
for (i = 0; i < adapter_table.st_limit; i++) {
status = adapter_get_name(i, buf);
if (status != HBA_STATUS_OK)
return 0;
if (!strcmp(buf, name))
return adapter_handle_offset + i;
}
return 0;
}
/*
* Get port by WWPN.
* Returns NULL if WWN not unique.
* If countp is non-NULL, the int it points to will be set to the
* number found so that the caller can tell if the WWN was ambiguous.
*/
struct port_info *
adapter_get_port_by_wwn(HBA_HANDLE handle, HBA_WWN wwn, int *countp)
{
struct adapter_info *ap;
struct port_info *pp_found = NULL;
struct port_info *pp;
int count = 0;
int p;
ap = adapter_open_handle(handle);
if (ap != NULL) {
for (p = 0; p < ap->ad_ports.st_limit; p++) {
pp = ap->ad_ports.st_table[p];
if (pp &&
!memcmp(&pp->ap_attr.PortWWN, &wwn, sizeof(wwn))) {
count++;
pp_found = pp;
}
}
}
if (count > 1)
pp_found = NULL;
if (countp != NULL)
*countp = count;
return pp_found;
}
/*
* Open adapter by WWN.
*/
HBA_STATUS
adapter_open_by_wwn(HBA_HANDLE *phandle, HBA_WWN wwn)
{
struct adapter_info *ap;
struct port_info *pp;
HBA_HANDLE found_handle = 0;
int count = 0;
HBA_STATUS status;
int i;
int p;
for (i = 0; i < adapter_table.st_limit; i++) {
ap = adapter_table.st_table[i];
if (!ap)
continue;
if (memcmp(&ap->ad_attr.NodeWWN, &wwn, sizeof(wwn)) == 0) {
count++;
found_handle = ap->ad_index + adapter_handle_offset;
} else {
for (p = 0; p < ap->ad_ports.st_limit; p++) {
pp = ap->ad_ports.st_table[p];
if (!pp)
continue;
if (memcmp(&pp->ap_attr.PortWWN,
&wwn, sizeof(wwn)) == 0) {
count++;
found_handle = ap->ad_index +
adapter_handle_offset;
}
}
}
}
*phandle = HBA_HANDLE_INVALID;
if (count == 1) {
status = HBA_STATUS_OK;
*phandle = found_handle;
} else if (count > 1) {
status = HBA_STATUS_ERROR_AMBIGUOUS_WWN;
} else {
status = HBA_STATUS_ERROR_ILLEGAL_WWN;
}
return status;
}
/*
* Close adapter.
*/
void
adapter_close(HBA_HANDLE handle)
{
}
/*
* Get adapter attributes.
*/
HBA_STATUS
adapter_get_attr(HBA_HANDLE handle, HBA_ADAPTERATTRIBUTES *pattr)
{
struct adapter_info *ap;
ap = adapter_open_handle(handle);
if (ap) {
*pattr = ap->ad_attr; /* struct copy */
return HBA_STATUS_OK;
}
return HBA_STATUS_ERROR;
}
/*
* Get adapter port attributes.
*/
HBA_STATUS
adapter_get_port_attr(HBA_HANDLE handle, HBA_UINT32 port,
HBA_PORTATTRIBUTES *pattr)
{
struct port_info *pp;
pp = adapter_get_port(handle, port);
if (pp) {
*pattr = pp->ap_attr; /* struct copy */
return HBA_STATUS_OK;
}
return HBA_STATUS_ERROR;
}
/*
* Get discovered (remote) port attributes.
*/
HBA_STATUS
adapter_get_rport_attr(HBA_HANDLE handle, HBA_UINT32 port, HBA_UINT32 rport,
HBA_PORTATTRIBUTES *pattr)
{
struct port_info *rp;
rp = adapter_get_rport_n(handle, port, rport);
if (rp) {
*pattr = rp->ap_attr; /* struct copy */
return HBA_STATUS_OK;
}
return HBA_STATUS_ERROR;
}
/*
* Get adapter port attributes.
*/
HBA_STATUS
adapter_get_port_attr_by_wwn(HBA_HANDLE handle, HBA_WWN wwn,
HBA_PORTATTRIBUTES *pattr)
{
struct adapter_info *ap;
struct port_info *pp;
struct port_info *pp_found = NULL;
u_int32_t p;
int count = 0;
HBA_STATUS status;
ap = adapter_open_handle(handle);
if (ap != NULL) {
for (p = 0; p < ap->ad_ports.st_limit; p++) {
pp = ap->ad_ports.st_table[p];
if (pp == NULL)
continue;
if (!memcmp(&pp->ap_attr.PortWWN, &wwn, sizeof(wwn))) {
count++;
pp_found = pp;
}
pp = sa_table_search(&pp->ap_rports,
adapter_wwpn_match,
&wwn);
if (pp) {
count++;
pp_found = pp;
}
}
}
pp = pp_found;
if (pp != NULL) {
if (count > 1) {
status = HBA_STATUS_ERROR_AMBIGUOUS_WWN;
} else {
*pattr = pp->ap_attr; /* struct copy */
status = HBA_STATUS_OK;
}
} else {
status = HBA_STATUS_ERROR_ILLEGAL_WWN;
}
return status;
}