2 * Copyright (c) 2004-2009 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2007 Xsigo Systems Inc. All rights reserved.
4 * Copyright (c) 2008 Lawrence Livermore National Laboratory
5 * Copyright (c) 2010-2011 Mellanox Technologies LTD. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39 #endif /* HAVE_CONFIG_H */
49 #include <infiniband/umad.h>
50 #include <infiniband/mad.h>
52 #include <infiniband/ibnetdisc.h>
53 #include <complib/cl_nodenamemap.h>
58 /* forward declarations */
64 static int query_node_info(smp_engine_t * engine, ib_portid_t * portid,
65 struct ni_cbdata * cbdata);
66 static int query_port_info(smp_engine_t * engine, ib_portid_t * portid,
67 ibnd_node_t * node, int portnum);
68 ibnd_port_t *ibnd_find_port_dr(ibnd_fabric_t * fabric, char *dr_str);
70 static int recv_switch_info(smp_engine_t * engine, ibnd_smp_t * smp,
71 uint8_t * mad, void *cb_data)
73 uint8_t *switch_info = mad + IB_SMP_DATA_OFFS;
74 ibnd_node_t *node = cb_data;
75 memcpy(node->switchinfo, switch_info, sizeof(node->switchinfo));
76 mad_decode_field(node->switchinfo, IB_SW_ENHANCED_PORT0_F,
81 static int query_switch_info(smp_engine_t * engine, ib_portid_t * portid,
84 node->smaenhsp0 = 0; /* assume base SP0 */
85 return issue_smp(engine, portid, IB_ATTR_SWITCH_INFO, 0,
86 recv_switch_info, node);
89 static int add_port_to_dpath(ib_dr_path_t * path, int nextport)
91 if (path->cnt > sizeof(path->p) - 2)
94 path->p[path->cnt] = (uint8_t) nextport;
98 static int retract_dpath(smp_engine_t * engine, ib_portid_t * portid)
100 ibnd_scan_t *scan = engine->user_data;
101 f_internal_t *f_int = scan->f_int;
103 if (scan->cfg->max_hops &&
104 f_int->fabric.maxhops_discovered > scan->cfg->max_hops)
107 /* this may seem wrong but the only time we would retract the path is
108 * if the user specified a CA for the DR path and we are retracting
109 * from that to find the node it is connected to. This counts as a
110 * positive hop discovered
112 f_int->fabric.maxhops_discovered++;
113 portid->drpath.p[portid->drpath.cnt] = 0;
114 portid->drpath.cnt--;
118 static int extend_dpath(smp_engine_t * engine, ib_portid_t * portid,
121 ibnd_scan_t *scan = engine->user_data;
122 f_internal_t *f_int = scan->f_int;
124 if (scan->cfg->max_hops &&
125 f_int->fabric.maxhops_discovered > scan->cfg->max_hops)
129 /* If we were LID routed we need to set up the drslid */
130 portid->drpath.drslid = (uint16_t) scan->selfportid.lid;
131 portid->drpath.drdlid = 0xFFFF;
134 if (add_port_to_dpath(&portid->drpath, nextport) < 0) {
135 IBND_ERROR("add port %d to DR path failed; %s\n", nextport,
140 if (((unsigned) portid->drpath.cnt - scan->initial_hops) >
141 f_int->fabric.maxhops_discovered)
142 f_int->fabric.maxhops_discovered++;
147 static int recv_node_desc(smp_engine_t * engine, ibnd_smp_t * smp,
148 uint8_t * mad, void *cb_data)
150 uint8_t *node_desc = mad + IB_SMP_DATA_OFFS;
151 ibnd_node_t *node = cb_data;
152 memcpy(node->nodedesc, node_desc, sizeof(node->nodedesc));
156 static int query_node_desc(smp_engine_t * engine, ib_portid_t * portid,
159 return issue_smp(engine, portid, IB_ATTR_NODE_DESC, 0,
160 recv_node_desc, node);
163 static void debug_port(ib_portid_t * portid, ibnd_port_t * port)
165 char width[64], speed[64];
167 int ispeed, fdr10, espeed;
171 iwidth = mad_get_field(port->info, 0, IB_PORT_LINK_WIDTH_ACTIVE_F);
172 ispeed = mad_get_field(port->info, 0, IB_PORT_LINK_SPEED_ACTIVE_F);
173 fdr10 = mad_get_field(port->ext_info, 0,
174 IB_MLNX_EXT_PORT_LINK_SPEED_ACTIVE_F);
176 if (port->node->type == IB_NODE_SWITCH)
177 info = (uint8_t *)&port->node->ports[0]->info;
179 info = (uint8_t *)&port->info;
180 cap_mask = mad_get_field(info, 0, IB_PORT_CAPMASK_F);
181 if (cap_mask & CL_NTOH32(IB_PORT_CAP_HAS_EXT_SPEEDS))
182 espeed = mad_get_field(port->info, 0, IB_PORT_LINK_SPEED_EXT_ACTIVE_F);
186 ("portid %s portnum %d: base lid %d state %d physstate %d %s %s %s %s\n",
187 portid2str(portid), port->portnum, port->base_lid,
188 mad_get_field(port->info, 0, IB_PORT_STATE_F),
189 mad_get_field(port->info, 0, IB_PORT_PHYS_STATE_F),
190 mad_dump_val(IB_PORT_LINK_WIDTH_ACTIVE_F, width, 64, &iwidth),
191 mad_dump_val(IB_PORT_LINK_SPEED_ACTIVE_F, speed, 64, &ispeed),
192 (fdr10 & FDR10) ? "FDR10" : "",
193 mad_dump_val(IB_PORT_LINK_SPEED_EXT_ACTIVE_F, speed, 64, &espeed));
196 static int is_mlnx_ext_port_info_supported(ibnd_port_t * port)
198 uint16_t devid = (uint16_t) mad_get_field(port->node->info, 0, IB_NODE_DEVID_F);
199 uint32_t vendorid = (uint32_t) mad_get_field(port->node->info, 0, IB_NODE_VENDORID_F);
201 if ((devid >= 0xc738 && devid <= 0xc73b) || devid == 0xcb20 || devid == 0xcf08 ||
202 ((vendorid == 0x119f) &&
204 (devid == 0x1b02 || devid == 0x1b50 ||
205 /* Bull SwitchIB and SwitchIB2 */
207 (devid >= 0x1bd0 && devid <= 0x1bd5))))
209 if ((devid >= 0x1003 && devid <= 0x1017) ||
210 ((vendorid == 0x119f) &&
212 (devid == 0x1b33 || devid == 0x1b73 ||
213 devid == 0x1b40 || devid == 0x1b41 ||
214 devid == 0x1b60 || devid == 0x1b61 ||
217 devid == 0x1b93 || devid == 0x1b94 ||
219 devid == 0x1bb4 || devid == 0x1bb5 ||
225 int mlnx_ext_port_info_err(smp_engine_t * engine, ibnd_smp_t * smp,
226 uint8_t * mad, void *cb_data)
228 f_internal_t *f_int = ((ibnd_scan_t *) engine->user_data)->f_int;
229 ibnd_node_t *node = cb_data;
231 uint8_t port_num, local_port;
233 port_num = (uint8_t) mad_get_field(mad, 0, IB_MAD_ATTRMOD_F);
234 port = node->ports[port_num];
236 IBND_ERROR("Failed to find 0x%" PRIx64 " port %u\n",
237 node->guid, port_num);
241 local_port = (uint8_t) mad_get_field(port->info, 0, IB_PORT_LOCAL_PORT_F);
242 debug_port(&smp->path, port);
244 if (port_num && mad_get_field(port->info, 0, IB_PORT_PHYS_STATE_F)
245 == IB_PORT_PHYS_STATE_LINKUP
246 && ((node->type == IB_NODE_SWITCH && port_num != local_port) ||
247 (node == f_int->fabric.from_node && port_num == f_int->fabric.from_portnum))) {
249 ib_portid_t path = smp->path;
251 if (node->type != IB_NODE_SWITCH &&
252 node == f_int->fabric.from_node &&
254 rc = retract_dpath(engine, &path);
256 /* we can't proceed through an HCA with DR */
257 if (path.lid == 0 || node->type == IB_NODE_SWITCH)
258 rc = extend_dpath(engine, &path, port_num);
262 struct ni_cbdata * cbdata = malloc(sizeof(*cbdata));
264 cbdata->port_num = port_num;
265 query_node_info(engine, &path, cbdata);
272 static int recv_mlnx_ext_port_info(smp_engine_t * engine, ibnd_smp_t * smp,
273 uint8_t * mad, void *cb_data)
275 f_internal_t *f_int = ((ibnd_scan_t *) engine->user_data)->f_int;
276 ibnd_node_t *node = cb_data;
278 uint8_t *ext_port_info = mad + IB_SMP_DATA_OFFS;
279 uint8_t port_num, local_port;
281 port_num = (uint8_t) mad_get_field(mad, 0, IB_MAD_ATTRMOD_F);
282 port = node->ports[port_num];
284 IBND_ERROR("Failed to find 0x%" PRIx64 " port %u\n",
285 node->guid, port_num);
289 memcpy(port->ext_info, ext_port_info, sizeof(port->ext_info));
290 local_port = (uint8_t) mad_get_field(port->info, 0, IB_PORT_LOCAL_PORT_F);
291 debug_port(&smp->path, port);
293 if (port_num && mad_get_field(port->info, 0, IB_PORT_PHYS_STATE_F)
294 == IB_PORT_PHYS_STATE_LINKUP
295 && ((node->type == IB_NODE_SWITCH && port_num != local_port) ||
296 (node == f_int->fabric.from_node && port_num == f_int->fabric.from_portnum))) {
298 ib_portid_t path = smp->path;
300 if (node->type != IB_NODE_SWITCH &&
301 node == f_int->fabric.from_node &&
303 rc = retract_dpath(engine, &path);
305 /* we can't proceed through an HCA with DR */
306 if (path.lid == 0 || node->type == IB_NODE_SWITCH)
307 rc = extend_dpath(engine, &path, port_num);
311 struct ni_cbdata * cbdata = malloc(sizeof(*cbdata));
313 cbdata->port_num = port_num;
314 query_node_info(engine, &path, cbdata);
321 static int query_mlnx_ext_port_info(smp_engine_t * engine, ib_portid_t * portid,
322 ibnd_node_t * node, int portnum)
324 IBND_DEBUG("Query MLNX Extended Port Info; %s (0x%" PRIx64 "):%d\n",
325 portid2str(portid), node->guid, portnum);
326 return issue_smp(engine, portid, IB_ATTR_MLNX_EXT_PORT_INFO, portnum,
327 recv_mlnx_ext_port_info, node);
330 static int recv_port_info(smp_engine_t * engine, ibnd_smp_t * smp,
331 uint8_t * mad, void *cb_data)
333 ibnd_scan_t *scan = (ibnd_scan_t *)engine->user_data;
334 f_internal_t *f_int = scan->f_int;
335 ibnd_node_t *node = cb_data;
337 uint8_t *port_info = mad + IB_SMP_DATA_OFFS;
338 uint8_t port_num, local_port;
339 int phystate, ispeed, espeed;
343 port_num = (uint8_t) mad_get_field(mad, 0, IB_MAD_ATTRMOD_F);
344 local_port = (uint8_t) mad_get_field(port_info, 0, IB_PORT_LOCAL_PORT_F);
346 /* this may have been created before */
347 port = node->ports[port_num];
349 port = node->ports[port_num] = calloc(1, sizeof(*port));
351 IBND_ERROR("Failed to allocate 0x%" PRIx64 " port %u\n",
352 node->guid, port_num);
356 mad_get_field64(node->info, 0, IB_NODE_PORT_GUID_F);
359 memcpy(port->info, port_info, sizeof(port->info));
361 port->portnum = port_num;
362 port->ext_portnum = 0;
363 port->base_lid = (uint16_t) mad_get_field(port->info, 0, IB_PORT_LID_F);
364 port->lmc = (uint8_t) mad_get_field(port->info, 0, IB_PORT_LMC_F);
367 node->smalid = port->base_lid;
368 node->smalmc = port->lmc;
369 } else if (node->type == IB_NODE_SWITCH) {
370 port->base_lid = node->smalid;
371 port->lmc = node->smalmc;
374 int rc1 = add_to_portguid_hash(port, f_int->fabric.portstbl);
376 IBND_ERROR("Error Occurred when trying"
377 " to insert new port guid 0x%016" PRIx64 " to DB\n",
380 add_to_portlid_hash(port, f_int->lid2guid);
382 if ((scan->cfg->flags & IBND_CONFIG_MLX_EPI)
383 && is_mlnx_ext_port_info_supported(port)) {
384 phystate = mad_get_field(port->info, 0, IB_PORT_PHYS_STATE_F);
385 ispeed = mad_get_field(port->info, 0, IB_PORT_LINK_SPEED_ACTIVE_F);
386 if (port->node->type == IB_NODE_SWITCH)
387 info = (uint8_t *)&port->node->ports[0]->info;
389 info = (uint8_t *)&port->info;
390 cap_mask = mad_get_field(info, 0, IB_PORT_CAPMASK_F);
391 if (cap_mask & CL_NTOH32(IB_PORT_CAP_HAS_EXT_SPEEDS))
392 espeed = mad_get_field(port->info, 0, IB_PORT_LINK_SPEED_EXT_ACTIVE_F);
396 if (phystate == IB_PORT_PHYS_STATE_LINKUP &&
397 ispeed == IB_LINK_SPEED_ACTIVE_10 &&
398 espeed == IB_LINK_SPEED_EXT_ACTIVE_NONE) { /* LinkUp/QDR */
399 query_mlnx_ext_port_info(engine, &smp->path,
405 debug_port(&smp->path, port);
407 if (port_num && mad_get_field(port->info, 0, IB_PORT_PHYS_STATE_F)
408 == IB_PORT_PHYS_STATE_LINKUP
409 && ((node->type == IB_NODE_SWITCH && port_num != local_port) ||
410 (node == f_int->fabric.from_node && port_num == f_int->fabric.from_portnum))) {
413 ib_portid_t path = smp->path;
415 if (node->type != IB_NODE_SWITCH &&
416 node == f_int->fabric.from_node &&
418 rc = retract_dpath(engine, &path);
420 /* we can't proceed through an HCA with DR */
421 if (path.lid == 0 || node->type == IB_NODE_SWITCH)
422 rc = extend_dpath(engine, &path, port_num);
426 struct ni_cbdata * cbdata = malloc(sizeof(*cbdata));
428 cbdata->port_num = port_num;
429 query_node_info(engine, &path, cbdata);
436 static int recv_port0_info(smp_engine_t * engine, ibnd_smp_t * smp,
437 uint8_t * mad, void *cb_data)
439 ibnd_node_t *node = cb_data;
442 status = recv_port_info(engine, smp, mad, cb_data);
443 /* Query PortInfo on switch external/physical ports */
444 for (i = 1; i <= node->numports; i++)
445 query_port_info(engine, &smp->path, node, i);
450 static int query_port_info(smp_engine_t * engine, ib_portid_t * portid,
451 ibnd_node_t * node, int portnum)
453 IBND_DEBUG("Query Port Info; %s (0x%" PRIx64 "):%d\n",
454 portid2str(portid), node->guid, portnum);
455 return issue_smp(engine, portid, IB_ATTR_PORT_INFO, portnum,
456 portnum ? recv_port_info : recv_port0_info, node);
459 static ibnd_node_t *create_node(smp_engine_t * engine, ib_portid_t * path,
462 f_internal_t *f_int = ((ibnd_scan_t *) engine->user_data)->f_int;
463 ibnd_node_t *rc = calloc(1, sizeof(*rc));
465 IBND_ERROR("OOM: node creation failed\n");
469 /* decode just a couple of fields for quicker reference. */
470 mad_decode_field(node_info, IB_NODE_GUID_F, &rc->guid);
471 mad_decode_field(node_info, IB_NODE_TYPE_F, &rc->type);
472 mad_decode_field(node_info, IB_NODE_NPORTS_F, &rc->numports);
474 rc->ports = calloc(rc->numports + 1, sizeof(*rc->ports));
477 IBND_ERROR("OOM: Failed to allocate the ports array\n");
481 rc->path_portid = *path;
482 memcpy(rc->info, node_info, sizeof(rc->info));
484 int rc1 = add_to_nodeguid_hash(rc, f_int->fabric.nodestbl);
486 IBND_ERROR("Error Occurred when trying"
487 " to insert new node guid 0x%016" PRIx64 " to DB\n",
490 /* add this to the all nodes list */
491 rc->next = f_int->fabric.nodes;
492 f_int->fabric.nodes = rc;
494 add_to_type_list(rc, f_int);
499 static void link_ports(ibnd_node_t * node, ibnd_port_t * port,
500 ibnd_node_t * remotenode, ibnd_port_t * remoteport)
502 IBND_DEBUG("linking: 0x%" PRIx64 " %p->%p:%u and 0x%" PRIx64
503 " %p->%p:%u\n", node->guid, node, port, port->portnum,
504 remotenode->guid, remotenode, remoteport,
505 remoteport->portnum);
506 if (port->remoteport)
507 port->remoteport->remoteport = NULL;
508 if (remoteport->remoteport)
509 remoteport->remoteport->remoteport = NULL;
510 port->remoteport = remoteport;
511 remoteport->remoteport = port;
514 static void dump_endnode(ib_portid_t * path, char *prompt,
515 ibnd_node_t * node, ibnd_port_t * port)
518 mad_dump_node_type(type, sizeof(type), &node->type, sizeof(int));
519 printf("%s -> %s %s {%016" PRIx64 "} portnum %d lid %d-%d \"%s\"\n",
520 portid2str(path), prompt, type, node->guid,
521 node->type == IB_NODE_SWITCH ? 0 : port->portnum,
522 port->base_lid, port->base_lid + (1 << port->lmc) - 1,
526 static int recv_node_info(smp_engine_t * engine, ibnd_smp_t * smp,
527 uint8_t * mad, void *cb_data)
529 ibnd_scan_t *scan = engine->user_data;
530 f_internal_t *f_int = scan->f_int;
531 uint8_t *node_info = mad + IB_SMP_DATA_OFFS;
532 struct ni_cbdata *ni_cbdata = (struct ni_cbdata *)cb_data;
533 ibnd_node_t *rem_node = NULL;
534 int rem_port_num = 0;
537 uint64_t node_guid = mad_get_field64(node_info, 0, IB_NODE_GUID_F);
538 uint64_t port_guid = mad_get_field64(node_info, 0, IB_NODE_PORT_GUID_F);
539 int port_num = mad_get_field(node_info, 0, IB_NODE_LOCAL_PORT_F);
540 ibnd_port_t *port = NULL;
543 rem_node = ni_cbdata->node;
544 rem_port_num = ni_cbdata->port_num;
548 node = ibnd_find_node_guid(&f_int->fabric, node_guid);
550 node = create_node(engine, &smp->path, node_info);
555 IBND_DEBUG("Found %s node GUID 0x%" PRIx64 " (%s)\n",
556 node_is_new ? "new" : "old", node->guid,
557 portid2str(&smp->path));
559 port = node->ports[port_num];
561 /* If we have not see this port before create a shell for it */
562 port = node->ports[port_num] = calloc(1, sizeof(*port));
566 port->portnum = port_num;
568 port->guid = port_guid;
570 if (scan->cfg->show_progress)
571 dump_endnode(&smp->path, node_is_new ? "new" : "known",
574 if (rem_node == NULL) { /* this is the start node */
575 f_int->fabric.from_node = node;
576 f_int->fabric.from_portnum = port_num;
579 if (!rem_node->ports[rem_port_num]) {
580 IBND_ERROR("Internal Error; "
581 "Node(%p) 0x%" PRIx64
582 " Port %d no port created!?!?!?\n\n",
583 rem_node, rem_node->guid, rem_port_num);
587 link_ports(node, port, rem_node, rem_node->ports[rem_port_num]);
591 query_node_desc(engine, &smp->path, node);
593 if (node->type == IB_NODE_SWITCH) {
594 query_switch_info(engine, &smp->path, node);
595 /* Query PortInfo on Switch Port 0 first */
596 query_port_info(engine, &smp->path, node, 0);
600 if (node->type != IB_NODE_SWITCH)
601 query_port_info(engine, &smp->path, node, port_num);
606 static int query_node_info(smp_engine_t * engine, ib_portid_t * portid,
607 struct ni_cbdata * cbdata)
609 IBND_DEBUG("Query Node Info; %s\n", portid2str(portid));
610 return issue_smp(engine, portid, IB_ATTR_NODE_INFO, 0,
611 recv_node_info, (void *)cbdata);
614 ibnd_node_t *ibnd_find_node_guid(ibnd_fabric_t * fabric, uint64_t guid)
616 int hash = HASHGUID(guid) % HTSZ;
620 IBND_DEBUG("fabric parameter NULL\n");
624 for (node = fabric->nodestbl[hash]; node; node = node->htnext)
625 if (node->guid == guid)
631 ibnd_node_t *ibnd_find_node_dr(ibnd_fabric_t * fabric, char *dr_str)
633 ibnd_port_t *rc = ibnd_find_port_dr(fabric, dr_str);
637 int add_to_nodeguid_hash(ibnd_node_t * node, ibnd_node_t * hash[])
640 ibnd_node_t *tblnode;
641 int hash_idx = HASHGUID(node->guid) % HTSZ;
643 for (tblnode = hash[hash_idx]; tblnode; tblnode = tblnode->htnext) {
644 if (tblnode == node) {
645 IBND_ERROR("Duplicate Node: Node with guid 0x%016"
646 PRIx64 " already exists in nodes DB\n",
651 node->htnext = hash[hash_idx];
652 hash[hash_idx] = node;
656 int add_to_portguid_hash(ibnd_port_t * port, ibnd_port_t * hash[])
659 ibnd_port_t *tblport;
660 int hash_idx = HASHGUID(port->guid) % HTSZ;
662 for (tblport = hash[hash_idx]; tblport; tblport = tblport->htnext) {
663 if (tblport == port) {
664 IBND_ERROR("Duplicate Port: Port with guid 0x%016"
665 PRIx64 " already exists in ports DB\n",
670 port->htnext = hash[hash_idx];
671 hash[hash_idx] = port;
675 void create_lid2guid(f_internal_t *f_int)
677 f_int->lid2guid = g_hash_table_new_full(g_direct_hash, g_direct_equal,
681 void destroy_lid2guid(f_internal_t *f_int)
683 if (f_int->lid2guid) {
684 g_hash_table_destroy(f_int->lid2guid);
688 void add_to_portlid_hash(ibnd_port_t * port, GHashTable *htable)
690 uint16_t base_lid = port->base_lid;
691 uint16_t lid_mask = ((1 << port->lmc) -1);
693 /* 0 < valid lid <= 0xbfff */
694 if (base_lid > 0 && base_lid <= 0xbfff) {
695 /* We add the port for all lids
696 * so it is easier to find any "random" lid specified */
697 for (lid = base_lid; lid <= (base_lid + lid_mask); lid++) {
698 g_hash_table_insert(htable, GINT_TO_POINTER(lid), port);
703 void add_to_type_list(ibnd_node_t * node, f_internal_t * f_int)
705 ibnd_fabric_t *fabric = &f_int->fabric;
706 switch (node->type) {
708 node->type_next = fabric->ch_adapters;
709 fabric->ch_adapters = node;
712 node->type_next = fabric->switches;
713 fabric->switches = node;
716 node->type_next = fabric->routers;
717 fabric->routers = node;
722 static int set_config(struct ibnd_config *config, struct ibnd_config *cfg)
728 memcpy(config, cfg, sizeof(*config));
730 if (!config->max_smps)
731 config->max_smps = DEFAULT_MAX_SMP_ON_WIRE;
732 if (!config->timeout_ms)
733 config->timeout_ms = DEFAULT_TIMEOUT;
734 if (!config->retries)
735 config->retries = DEFAULT_RETRIES;
740 f_internal_t *allocate_fabric_internal(void)
742 f_internal_t *f = calloc(1, sizeof(*f));
749 ibnd_fabric_t *ibnd_discover_fabric(char * ca_name, int ca_port,
751 struct ibnd_config *cfg)
753 struct ibnd_config config = { 0 };
754 f_internal_t *f_int = NULL;
755 ib_portid_t my_portid = { 0 };
758 struct ibmad_port *ibmad_port;
760 int mc[2] = { IB_SMI_CLASS, IB_SMI_DIRECT_CLASS };
762 /* If not specified start from "my" port */
766 if (set_config(&config, cfg)) {
767 IBND_ERROR("Invalid ibnd_config\n");
771 f_int = allocate_fabric_internal();
773 IBND_ERROR("OOM: failed to calloc ibnd_fabric_t\n");
777 memset(&scan.selfportid, 0, sizeof(scan.selfportid));
780 scan.initial_hops = from->drpath.cnt;
782 ibmad_port = mad_rpc_open_port(ca_name, ca_port, mc, nc);
784 IBND_ERROR("can't open MAD port (%s:%d)\n", ca_name, ca_port);
787 mad_rpc_set_timeout(ibmad_port, cfg->timeout_ms);
788 mad_rpc_set_retries(ibmad_port, cfg->retries);
789 smp_mkey_set(ibmad_port, cfg->mkey);
791 if (ib_resolve_self_via(&scan.selfportid,
792 NULL, NULL, ibmad_port) < 0) {
793 IBND_ERROR("Failed to resolve self\n");
794 mad_rpc_close_port(ibmad_port);
797 mad_rpc_close_port(ibmad_port);
799 if (smp_engine_init(&engine, ca_name, ca_port, &scan, &config)) {
804 IBND_DEBUG("from %s\n", portid2str(from));
806 if (!query_node_info(&engine, from, NULL))
807 if (process_mads(&engine) != 0)
810 f_int->fabric.total_mads_used = engine.total_smps;
811 f_int->fabric.maxhops_discovered += scan.initial_hops;
813 if (group_nodes(&f_int->fabric))
816 smp_engine_destroy(&engine);
817 return (ibnd_fabric_t *)f_int;
819 smp_engine_destroy(&engine);
820 ibnd_destroy_fabric(&f_int->fabric);
824 void destroy_node(ibnd_node_t * node)
829 for (p = 0; p <= node->numports; p++)
830 free(node->ports[p]);
836 void ibnd_destroy_fabric(ibnd_fabric_t * fabric)
838 ibnd_node_t *node = NULL;
839 ibnd_node_t *next = NULL;
840 ibnd_chassis_t *ch, *ch_next;
845 ch = fabric->chassis;
851 node = fabric->nodes;
857 destroy_lid2guid((f_internal_t *)fabric);
861 void ibnd_iter_nodes(ibnd_fabric_t * fabric, ibnd_iter_node_func_t func,
864 ibnd_node_t *cur = NULL;
867 IBND_DEBUG("fabric parameter NULL\n");
872 IBND_DEBUG("func parameter NULL\n");
876 for (cur = fabric->nodes; cur; cur = cur->next)
877 func(cur, user_data);
880 void ibnd_iter_nodes_type(ibnd_fabric_t * fabric, ibnd_iter_node_func_t func,
881 int node_type, void *user_data)
883 ibnd_node_t *list = NULL;
884 ibnd_node_t *cur = NULL;
887 IBND_DEBUG("fabric parameter NULL\n");
892 IBND_DEBUG("func parameter NULL\n");
898 list = fabric->switches;
901 list = fabric->ch_adapters;
904 list = fabric->routers;
907 IBND_DEBUG("Invalid node_type specified %d\n", node_type);
911 for (cur = list; cur; cur = cur->type_next)
912 func(cur, user_data);
915 ibnd_port_t *ibnd_find_port_lid(ibnd_fabric_t * fabric,
919 f_internal_t *f = (f_internal_t *)fabric;
921 port = (ibnd_port_t *)g_hash_table_lookup(f->lid2guid,
922 GINT_TO_POINTER(lid));
927 ibnd_port_t *ibnd_find_port_guid(ibnd_fabric_t * fabric, uint64_t guid)
929 int hash = HASHGUID(guid) % HTSZ;
933 IBND_DEBUG("fabric parameter NULL\n");
937 for (port = fabric->portstbl[hash]; port; port = port->htnext)
938 if (port->guid == guid)
944 ibnd_port_t *ibnd_find_port_dr(ibnd_fabric_t * fabric, char *dr_str)
947 ibnd_node_t *cur_node;
948 ibnd_port_t *rc = NULL;
952 IBND_DEBUG("fabric parameter NULL\n");
957 IBND_DEBUG("dr_str parameter NULL\n");
961 cur_node = fabric->from_node;
963 if (str2drpath(&path, dr_str, 0, 0) == -1)
966 for (i = 0; i <= path.cnt; i++) {
967 ibnd_port_t *remote_port = NULL;
970 if (!cur_node->ports)
973 remote_port = cur_node->ports[path.p[i]]->remoteport;
978 cur_node = remote_port->node;
984 void ibnd_iter_ports(ibnd_fabric_t * fabric, ibnd_iter_port_func_t func,
988 ibnd_port_t *cur = NULL;
991 IBND_DEBUG("fabric parameter NULL\n");
996 IBND_DEBUG("func parameter NULL\n");
1000 for (i = 0; i<HTSZ; i++)
1001 for (cur = fabric->portstbl[i]; cur; cur = cur->htnext)
1002 func(cur, user_data);