2 * Copyright (c) 2004-2009 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2014 Intel Corporation. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
42 #include <sys/types.h>
45 #include <sys/ioctl.h>
51 #include <infiniband/umad.h>
53 #define IB_OPENIB_OUI (0x001405)
57 typedef struct ib_user_mad_reg_req {
59 uint32_t method_mask[4];
62 uint8_t mgmt_class_version;
65 } ib_user_mad_reg_req_t;
67 static_assert(sizeof(struct ib_user_mad_reg_req) == IOCPARM_LEN(IB_USER_MAD_REGISTER_AGENT),
68 "Invalid structure size");
70 struct ib_user_mad_reg_req2 {
74 uint8_t mgmt_class_version;
77 uint64_t method_mask[2];
83 static_assert(sizeof(struct ib_user_mad_reg_req2) == IOCPARM_LEN(IB_USER_MAD_REGISTER_AGENT2),
84 "Invalid structure size");
86 #define IBWARN(fmt, args...) fprintf(stderr, "ibwarn: [%d] %s: " fmt "\n", getpid(), __func__, ## args)
88 #define TRACE if (umaddebug) IBWARN
89 #define DEBUG if (umaddebug) IBWARN
91 static int umaddebug = 0;
93 #define UMAD_DEV_FILE_SZ 256
95 static const char *def_ca_name = "mthca0";
96 static int def_ca_port = 1;
98 static unsigned abi_version;
99 static unsigned new_user_mad_api;
101 /*************************************
104 static int find_cached_ca(const char *ca_name, umad_ca_t * ca)
106 return 0; /* caching not implemented yet */
109 static int put_ca(umad_ca_t * ca)
111 return 0; /* caching not implemented yet */
114 static int release_port(umad_port_t * port)
118 port->pkeys_size = 0;
122 static int check_for_digit_name(const struct dirent *dent)
124 const char *p = dent->d_name;
125 while (*p && isdigit(*p))
130 static int get_port(const char *ca_name, const char *dir, int portnum, umad_port_t * port)
134 struct dirent **namelist = NULL;
135 int i, len, num_pkeys = 0;
138 strncpy(port->ca_name, ca_name, sizeof port->ca_name - 1);
139 port->portnum = portnum;
143 len = snprintf(port_dir, sizeof(port_dir), "%s/%d", dir, portnum);
144 if (len < 0 || len > sizeof(port_dir))
147 if (sys_read_uint(port_dir, SYS_PORT_LMC, &port->lmc) < 0)
149 if (sys_read_uint(port_dir, SYS_PORT_SMLID, &port->sm_lid) < 0)
151 if (sys_read_uint(port_dir, SYS_PORT_SMSL, &port->sm_sl) < 0)
153 if (sys_read_uint(port_dir, SYS_PORT_LID, &port->base_lid) < 0)
155 if (sys_read_uint(port_dir, SYS_PORT_STATE, &port->state) < 0)
157 if (sys_read_uint(port_dir, SYS_PORT_PHY_STATE, &port->phys_state) < 0)
160 * If width was not set properly this read may fail.
161 * Instead of failing everything, we will just skip the check
162 * and it will be set to 0.
164 sys_read_uint(port_dir, SYS_PORT_RATE, &port->rate);
165 if (sys_read_uint(port_dir, SYS_PORT_CAPMASK, &capmask) < 0)
168 if (sys_read_string(port_dir, SYS_PORT_LINK_LAYER,
169 port->link_layer, UMAD_CA_NAME_LEN) < 0)
170 /* assume IB by default */
171 sprintf(port->link_layer, "IB");
173 port->capmask = htobe32(capmask);
175 if (sys_read_gid(port_dir, SYS_PORT_GID, &gid) < 0)
178 port->gid_prefix = gid.global.subnet_prefix;
179 port->port_guid = gid.global.interface_id;
181 snprintf(port_dir + len, sizeof(port_dir) - len, "/pkeys");
182 num_pkeys = sys_scandir(port_dir, &namelist, check_for_digit_name, NULL);
183 if (num_pkeys <= 0) {
184 IBWARN("no pkeys found for %s:%u (at dir %s)...",
185 port->ca_name, port->portnum, port_dir);
188 port->pkeys = calloc(num_pkeys, sizeof(port->pkeys[0]));
190 IBWARN("get_port: calloc failed: %s", strerror(errno));
193 for (i = 0; i < num_pkeys; i++) {
195 idx = strtoul(namelist[i]->d_name, NULL, 0);
196 sys_read_uint(port_dir, namelist[i]->d_name, &val);
197 port->pkeys[idx] = val;
200 port->pkeys_size = num_pkeys;
203 port_dir[len] = '\0';
205 /* FIXME: handle gids */
211 for (i = 0; i < num_pkeys; i++)
220 static int release_ca(umad_ca_t * ca)
224 for (i = 0; i <= ca->numports; i++) {
227 release_port(ca->ports[i]);
235 * if *port > 0, check ca[port] state. Otherwise set *port to
236 * the first port that is active, and if such is not found, to
237 * the first port that is link up and if none are linkup, then
238 * the first port that is not disabled. Otherwise return -1.
240 static int resolve_ca_port(const char *ca_name, int *port)
243 int active = -1, up = -1;
246 TRACE("checking ca '%s'", ca_name);
248 if (umad_get_ca(ca_name, &ca) < 0)
251 if (ca.node_type == 2) {
252 *port = 0; /* switch sma port 0 */
257 if (*port > 0) { /* check only the port the user wants */
258 if (*port > ca.numports) {
262 if (!ca.ports[*port]) {
266 if (strcmp(ca.ports[*port]->link_layer, "InfiniBand") &&
267 strcmp(ca.ports[*port]->link_layer, "IB")) {
271 if (ca.ports[*port]->state == 4) {
275 if (ca.ports[*port]->phys_state != 3)
281 for (i = 0; i <= ca.numports; i++) {
282 DEBUG("checking port %d", i);
285 if (strcmp(ca.ports[i]->link_layer, "InfiniBand") &&
286 strcmp(ca.ports[i]->link_layer, "IB"))
288 if (up < 0 && ca.ports[i]->phys_state == 5)
290 if (ca.ports[i]->state == 4) {
292 DEBUG("found active port %d", i);
297 if (active == -1 && up == -1) { /* no active or linkup port found */
298 for (i = 0; i <= ca.numports; i++) {
299 DEBUG("checking port %d", i);
302 if (ca.ports[i]->phys_state != 3) {
323 static const char *resolve_ca_name(const char *ca_name, int *best_port)
325 static char names[UMAD_MAX_DEVICES][UMAD_CA_NAME_LEN];
326 int phys_found = -1, port_found = 0, port, port_type;
329 if (ca_name && (!best_port || *best_port))
333 if (resolve_ca_port(ca_name, best_port) < 0)
338 /* Get the list of CA names */
339 if ((n = umad_get_cas_names((void *)names, UMAD_MAX_DEVICES)) < 0)
342 /* Find the first existing CA with an active port */
343 for (caidx = 0; caidx < n; caidx++) {
344 TRACE("checking ca '%s'", names[caidx]);
346 port = best_port ? *best_port : 0;
347 if ((port_type = resolve_ca_port(names[caidx], &port)) < 0)
350 DEBUG("found ca %s with port %d type %d",
351 names[caidx], port, port_type);
356 DEBUG("found ca %s with active port %d",
358 return (char *)(names + caidx);
361 if (phys_found == -1) {
367 DEBUG("phys found %d on %s port %d",
368 phys_found, phys_found >= 0 ? names[phys_found] : NULL,
370 if (phys_found >= 0) {
372 *best_port = port_found;
373 return names[phys_found];
377 *best_port = def_ca_port;
381 static int get_ca(const char *ca_name, umad_ca_t * ca)
384 struct dirent **namelist;
389 memset(ca->ports, 0, sizeof ca->ports);
390 strncpy(ca->ca_name, ca_name, sizeof(ca->ca_name) - 1);
392 snprintf(dir_name, sizeof(dir_name), "%s/%s", SYS_INFINIBAND,
395 if ((r = sys_read_uint(dir_name, SYS_NODE_TYPE, &ca->node_type)) < 0)
397 if (sys_read_string(dir_name, SYS_CA_FW_VERS, ca->fw_ver,
398 sizeof ca->fw_ver) < 0)
399 ca->fw_ver[0] = '\0';
400 if (sys_read_string(dir_name, SYS_CA_HW_VERS, ca->hw_ver,
401 sizeof ca->hw_ver) < 0)
402 ca->hw_ver[0] = '\0';
403 if ((r = sys_read_string(dir_name, SYS_CA_TYPE, ca->ca_type,
404 sizeof ca->ca_type)) < 0)
405 ca->ca_type[0] = '\0';
406 if ((r = sys_read_guid(dir_name, SYS_CA_NODE_GUID, &ca->node_guid)) < 0)
409 sys_read_guid(dir_name, SYS_CA_SYS_GUID, &ca->system_guid)) < 0)
412 snprintf(dir_name, sizeof(dir_name), "%s/%s/%s",
413 SYS_INFINIBAND, ca->ca_name, SYS_CA_PORTS_DIR);
415 if ((r = sys_scandir(dir_name, &namelist, NULL, alphasort)) < 0) {
416 ret = errno < 0 ? errno : -EIO;
421 for (i = 0; i < r; i++) {
423 if (!strcmp(".", namelist[i]->d_name) ||
424 !strcmp("..", namelist[i]->d_name))
426 if (strcmp("0", namelist[i]->d_name) &&
427 ((portnum = atoi(namelist[i]->d_name)) <= 0 ||
428 portnum >= UMAD_CA_MAX_PORTS)) {
432 if (!(ca->ports[portnum] =
433 calloc(1, sizeof(*ca->ports[portnum])))) {
437 if (get_port(ca_name, dir_name, portnum, ca->ports[portnum]) <
439 free(ca->ports[portnum]);
440 ca->ports[portnum] = NULL;
444 if (ca->numports < portnum)
445 ca->numports = portnum;
448 for (i = 0; i < r; i++)
456 for (i = 0; i < r; i++)
465 static int umad_id_to_dev(int umad_id, char *dev, unsigned *port)
470 snprintf(path, sizeof(path), SYS_INFINIBAND_MAD "/umad%d", umad_id);
473 sys_read_string(path, SYS_IB_MAD_DEV, dev, UMAD_CA_NAME_LEN)) < 0)
476 if ((r = sys_read_uint(path, SYS_IB_MAD_PORT, port)) < 0)
482 static int dev_to_umad_id(const char *dev, unsigned port)
484 char umad_dev[UMAD_CA_NAME_LEN];
488 for (id = 0; id < UMAD_MAX_PORTS; id++) {
489 if (umad_id_to_dev(id, umad_dev, &umad_port) < 0)
491 if (strncmp(dev, umad_dev, UMAD_CA_NAME_LEN))
493 if (port != umad_port)
496 DEBUG("mapped %s %d to %d", dev, port, id);
500 return -1; /* not found */
503 /*******************************
510 if (sys_read_uint(IB_UMAD_ABI_DIR, IB_UMAD_ABI_FILE, &abi_version) < 0) {
512 ("can't read ABI version from %s (%m): is ibcore module loaded?",
513 PATH_TO_SYS(IB_UMAD_ABI_DIR "/" IB_UMAD_ABI_FILE));
516 if (abi_version < IB_UMAD_ABI_VERSION) {
518 ("wrong ABI version: %s is %d but library minimal ABI is %d",
519 PATH_TO_SYS(IB_UMAD_ABI_DIR "/" IB_UMAD_ABI_FILE), abi_version,
520 IB_UMAD_ABI_VERSION);
529 /* FIXME - verify that all ports are closed */
533 static unsigned is_ib_type(const char *ca_name)
538 snprintf(dir_name, sizeof(dir_name), "%s/%s", SYS_INFINIBAND, ca_name);
540 if (sys_read_uint(dir_name, SYS_NODE_TYPE, &type) < 0)
543 return type >= 1 && type <= 3 ? 1 : 0;
546 int umad_get_cas_names(char cas[][UMAD_CA_NAME_LEN], int max)
548 struct dirent **namelist;
551 TRACE("max %d", max);
553 n = sys_scandir(SYS_INFINIBAND, &namelist, NULL, alphasort);
555 for (i = 0; i < n; i++) {
556 if (strcmp(namelist[i]->d_name, ".") &&
557 strcmp(namelist[i]->d_name, "..")) {
558 if (j < max && is_ib_type(namelist[i]->d_name))
559 strncpy(cas[j++], namelist[i]->d_name,
564 DEBUG("return %d cas", j);
566 /* Is this still needed ? */
567 strncpy((char *)cas, def_ca_name, UMAD_CA_NAME_LEN);
568 DEBUG("return 1 ca");
576 int umad_get_ca_portguids(const char *ca_name, __be64 *portguids, int max)
581 TRACE("ca name %s max port guids %d", ca_name, max);
582 if (!(ca_name = resolve_ca_name(ca_name, NULL)))
585 if (umad_get_ca(ca_name, &ca) < 0)
589 if (ca.numports + 1 > max) {
594 for (i = 0; i <= ca.numports; i++)
595 portguids[ports++] = ca.ports[i] ?
596 ca.ports[i]->port_guid : htobe64(0);
600 DEBUG("%s: %d ports", ca_name, ports);
605 int umad_get_issm_path(const char *ca_name, int portnum, char path[], int max)
609 TRACE("ca %s port %d", ca_name, portnum);
611 if (!(ca_name = resolve_ca_name(ca_name, &portnum)))
614 if ((umad_id = dev_to_umad_id(ca_name, portnum)) < 0)
617 snprintf(path, max, "%s/issm%u", UMAD_DEV_DIR, umad_id);
622 int umad_open_port(const char *ca_name, int portnum)
624 char dev_file[UMAD_DEV_FILE_SZ];
627 TRACE("ca %s port %d", ca_name, portnum);
629 if (!(ca_name = resolve_ca_name(ca_name, &portnum)))
632 DEBUG("opening %s port %d", ca_name, portnum);
634 if ((umad_id = dev_to_umad_id(ca_name, portnum)) < 0)
637 snprintf(dev_file, sizeof(dev_file), "%s/umad%d",
638 UMAD_DEV_DIR, umad_id);
640 if ((fd = open(dev_file, O_RDWR | O_NONBLOCK)) < 0) {
641 DEBUG("open %s failed: %s", dev_file, strerror(errno));
645 if (abi_version > 5 || !ioctl(fd, IB_USER_MAD_ENABLE_PKEY, NULL))
646 new_user_mad_api = 1;
648 new_user_mad_api = 0;
650 DEBUG("opened %s fd %d portid %d", dev_file, fd, umad_id);
654 int umad_get_ca(const char *ca_name, umad_ca_t * ca)
658 TRACE("ca_name %s", ca_name);
659 if (!(ca_name = resolve_ca_name(ca_name, NULL)))
662 if (find_cached_ca(ca_name, ca) > 0)
665 if ((r = get_ca(ca_name, ca)) < 0)
668 DEBUG("opened %s", ca_name);
672 int umad_release_ca(umad_ca_t * ca)
676 TRACE("ca_name %s", ca->ca_name);
680 if ((r = release_ca(ca)) < 0)
683 DEBUG("releasing %s", ca->ca_name);
687 int umad_get_port(const char *ca_name, int portnum, umad_port_t * port)
691 TRACE("ca_name %s portnum %d", ca_name, portnum);
693 if (!(ca_name = resolve_ca_name(ca_name, &portnum)))
696 snprintf(dir_name, sizeof(dir_name), "%s/%s/%s",
697 SYS_INFINIBAND, ca_name, SYS_CA_PORTS_DIR);
699 return get_port(ca_name, dir_name, portnum, port);
702 int umad_release_port(umad_port_t * port)
706 TRACE("port %s:%d", port->ca_name, port->portnum);
710 if ((r = release_port(port)) < 0)
713 DEBUG("releasing %s:%d", port->ca_name, port->portnum);
717 int umad_close_port(int fd)
720 DEBUG("closed fd %d", fd);
724 void *umad_get_mad(void *umad)
726 return new_user_mad_api ? ((struct ib_user_mad *)umad)->data :
727 (void *)&((struct ib_user_mad *)umad)->addr.pkey_index;
730 size_t umad_size(void)
732 return new_user_mad_api ? sizeof(struct ib_user_mad) :
733 sizeof(struct ib_user_mad) - 8;
736 int umad_set_grh(void *umad, void *mad_addr)
738 struct ib_user_mad *mad = umad;
739 struct ib_mad_addr *addr = mad_addr;
742 mad->addr.grh_present = 1;
743 mad->addr.ib_gid = addr->ib_gid;
744 /* The definition for umad_set_grh requires that the input be
746 mad->addr.flow_label = htobe32((uint32_t)addr->flow_label);
747 mad->addr.hop_limit = addr->hop_limit;
748 mad->addr.traffic_class = addr->traffic_class;
750 mad->addr.grh_present = 0;
754 int umad_set_pkey(void *umad, int pkey_index)
756 struct ib_user_mad *mad = umad;
758 if (new_user_mad_api)
759 mad->addr.pkey_index = pkey_index;
764 int umad_get_pkey(void *umad)
766 struct ib_user_mad *mad = umad;
768 if (new_user_mad_api)
769 return mad->addr.pkey_index;
774 int umad_set_addr(void *umad, int dlid, int dqp, int sl, int qkey)
776 struct ib_user_mad *mad = umad;
778 TRACE("umad %p dlid %u dqp %d sl %d, qkey %x",
779 umad, dlid, dqp, sl, qkey);
780 mad->addr.qpn = htobe32(dqp);
781 mad->addr.lid = htobe16(dlid);
782 mad->addr.qkey = htobe32(qkey);
788 int umad_set_addr_net(void *umad, __be16 dlid, __be32 dqp, int sl, __be32 qkey)
790 struct ib_user_mad *mad = umad;
792 TRACE("umad %p dlid %u dqp %d sl %d qkey %x",
793 umad, be16toh(dlid), be32toh(dqp), sl, be32toh(qkey));
795 mad->addr.lid = dlid;
796 mad->addr.qkey = qkey;
802 int umad_send(int fd, int agentid, void *umad, int length,
803 int timeout_ms, int retries)
805 struct ib_user_mad *mad = umad;
808 TRACE("fd %d agentid %d umad %p timeout %u",
809 fd, agentid, umad, timeout_ms);
812 mad->timeout_ms = timeout_ms;
813 mad->retries = retries;
814 mad->agent_id = agentid;
819 n = write(fd, mad, length + umad_size());
820 if (n == length + umad_size())
823 DEBUG("write returned %d != sizeof umad %zu + length %d (%m)",
824 n, umad_size(), length);
830 static int dev_poll(int fd, int timeout_ms)
836 ufds.events = POLLIN;
838 if ((n = poll(&ufds, 1, timeout_ms)) == 1)
847 int umad_recv(int fd, void *umad, int *length, int timeout_ms)
849 struct ib_user_mad *mad = umad;
853 TRACE("fd %d umad %p timeout %u", fd, umad, timeout_ms);
855 if (!umad || !length) {
860 if (timeout_ms && (n = dev_poll(fd, timeout_ms)) < 0) {
866 n = read(fd, umad, umad_size() + *length);
868 VALGRIND_MAKE_MEM_DEFINED(umad, umad_size() + *length);
870 if ((n >= 0) && (n <= umad_size() + *length)) {
871 DEBUG("mad received by agent %d length %d", mad->agent_id, n);
873 *length = n - umad_size();
876 return mad->agent_id;
879 if (n == -EWOULDBLOCK) {
885 DEBUG("read returned %zu > sizeof umad %zu + length %d (%m)",
886 mad->length - umad_size(), umad_size(), *length);
888 *length = mad->length - umad_size();
894 int umad_poll(int fd, int timeout_ms)
896 TRACE("fd %d timeout %u", fd, timeout_ms);
897 return dev_poll(fd, timeout_ms);
900 int umad_get_fd(int fd)
906 int umad_register_oui(int fd, int mgmt_class, uint8_t rmpp_version,
907 uint8_t oui[3], long method_mask[])
909 struct ib_user_mad_reg_req req;
911 TRACE("fd %d mgmt_class %u rmpp_version %d oui 0x%x%x%x method_mask %p",
912 fd, mgmt_class, (int)rmpp_version, (int)oui[0], (int)oui[1],
913 (int)oui[2], method_mask);
915 if (mgmt_class < 0x30 || mgmt_class > 0x4f) {
916 DEBUG("mgmt class %d not in vendor range 2", mgmt_class);
921 req.mgmt_class = mgmt_class;
922 req.mgmt_class_version = 1;
923 memcpy(req.oui, oui, sizeof req.oui);
924 req.rmpp_version = rmpp_version;
927 memcpy(req.method_mask, method_mask, sizeof req.method_mask);
929 memset(req.method_mask, 0, sizeof req.method_mask);
931 VALGRIND_MAKE_MEM_DEFINED(&req, sizeof req);
933 if (!ioctl(fd, IB_USER_MAD_REGISTER_AGENT, (void *)&req)) {
935 ("fd %d registered to use agent %d qp %d class 0x%x oui %p",
936 fd, req.id, req.qpn, req.mgmt_class, oui);
937 return req.id; /* return agentid */
940 DEBUG("fd %d registering qp %d class 0x%x version %d oui %p failed: %m",
941 fd, req.qpn, req.mgmt_class, req.mgmt_class_version, oui);
945 int umad_register(int fd, int mgmt_class, int mgmt_version,
946 uint8_t rmpp_version, long method_mask[])
948 struct ib_user_mad_reg_req req;
949 __be32 oui = htobe32(IB_OPENIB_OUI);
953 ("fd %d mgmt_class %u mgmt_version %u rmpp_version %d method_mask %p",
954 fd, mgmt_class, mgmt_version, rmpp_version, method_mask);
956 req.qpn = qp = (mgmt_class == 0x1 || mgmt_class == 0x81) ? 0 : 1;
957 req.mgmt_class = mgmt_class;
958 req.mgmt_class_version = mgmt_version;
959 req.rmpp_version = rmpp_version;
962 memcpy(req.method_mask, method_mask, sizeof req.method_mask);
964 memset(req.method_mask, 0, sizeof req.method_mask);
966 memcpy(&req.oui, (char *)&oui + 1, sizeof req.oui);
968 VALGRIND_MAKE_MEM_DEFINED(&req, sizeof req);
970 if (!ioctl(fd, IB_USER_MAD_REGISTER_AGENT, (void *)&req)) {
971 DEBUG("fd %d registered to use agent %d qp %d", fd, req.id, qp);
972 return req.id; /* return agentid */
975 DEBUG("fd %d registering qp %d class 0x%x version %d failed: %m",
976 fd, qp, mgmt_class, mgmt_version);
980 int umad_register2(int port_fd, struct umad_reg_attr *attr, uint32_t *agent_id)
982 struct ib_user_mad_reg_req2 req;
985 if (!attr || !agent_id)
988 TRACE("fd %d mgmt_class %u mgmt_class_version %u flags 0x%08x "
989 "method_mask 0x%016" PRIx64 " %016" PRIx64
990 "oui 0x%06x rmpp_version %u ",
991 port_fd, attr->mgmt_class, attr->mgmt_class_version,
992 attr->flags, attr->method_mask[0], attr->method_mask[1],
993 attr->oui, attr->rmpp_version);
995 if (attr->mgmt_class >= 0x30 && attr->mgmt_class <= 0x4f &&
996 ((attr->oui & 0x00ffffff) == 0 || (attr->oui & 0xff000000) != 0)) {
997 DEBUG("mgmt class %d is in vendor range 2 but oui (0x%08x) is invalid",
998 attr->mgmt_class, attr->oui);
1002 memset(&req, 0, sizeof(req));
1004 req.mgmt_class = attr->mgmt_class;
1005 req.mgmt_class_version = attr->mgmt_class_version;
1006 req.qpn = (attr->mgmt_class == 0x1 || attr->mgmt_class == 0x81) ? 0 : 1;
1007 req.flags = attr->flags;
1008 memcpy(req.method_mask, attr->method_mask, sizeof req.method_mask);
1009 req.oui = attr->oui;
1010 req.rmpp_version = attr->rmpp_version;
1012 VALGRIND_MAKE_MEM_DEFINED(&req, sizeof req);
1014 if ((rc = ioctl(port_fd, IB_USER_MAD_REGISTER_AGENT2, (void *)&req)) == 0) {
1015 DEBUG("fd %d registered to use agent %d qp %d class 0x%x oui 0x%06x",
1016 port_fd, req.id, req.qpn, req.mgmt_class, attr->oui);
1021 if (errno == ENOTTY || errno == EINVAL) {
1023 TRACE("no kernel support for registration flags");
1026 if (attr->flags == 0) {
1027 struct ib_user_mad_reg_req req_v1;
1029 TRACE("attempting original register ioctl");
1031 memset(&req_v1, 0, sizeof(req_v1));
1032 req_v1.mgmt_class = req.mgmt_class;
1033 req_v1.mgmt_class_version = req.mgmt_class_version;
1034 req_v1.qpn = req.qpn;
1035 req_v1.rmpp_version = req.rmpp_version;
1036 req_v1.oui[0] = (req.oui & 0xff0000) >> 16;
1037 req_v1.oui[1] = (req.oui & 0x00ff00) >> 8;
1038 req_v1.oui[2] = req.oui & 0x0000ff;
1040 memcpy(req_v1.method_mask, req.method_mask, sizeof req_v1.method_mask);
1042 if ((rc = ioctl(port_fd, IB_USER_MAD_REGISTER_AGENT,
1043 (void *)&req_v1)) == 0) {
1044 DEBUG("fd %d registered to use agent %d qp %d class 0x%x oui 0x%06x",
1045 port_fd, req_v1.id, req_v1.qpn, req_v1.mgmt_class, attr->oui);
1046 *agent_id = req_v1.id;
1053 attr->flags = req.flags;
1055 DEBUG("fd %d registering qp %d class 0x%x version %d "
1056 "oui 0x%06x failed flags returned 0x%x : %m",
1057 port_fd, req.qpn, req.mgmt_class, req.mgmt_class_version,
1058 attr->oui, req.flags);
1063 int umad_unregister(int fd, int agentid)
1065 TRACE("fd %d unregistering agent %d", fd, agentid);
1066 return ioctl(fd, IB_USER_MAD_UNREGISTER_AGENT, &agentid);
1069 int umad_status(void *umad)
1071 struct ib_user_mad *mad = umad;
1076 ib_mad_addr_t *umad_get_mad_addr(void *umad)
1078 struct ib_user_mad *mad = umad;
1083 int umad_debug(int level)
1090 void umad_addr_dump(ib_mad_addr_t * addr)
1092 #define HEX(x) ((x) < 10 ? '0' + (x) : 'a' + ((x) -10))
1096 for (i = 0; i < sizeof addr->gid; i++) {
1097 gid_str[i * 2] = HEX(addr->gid[i] >> 4);
1098 gid_str[i * 2 + 1] = HEX(addr->gid[i] & 0xf);
1101 IBWARN("qpn %d qkey 0x%x lid %u sl %d\n"
1102 "grh_present %d gid_index %d hop_limit %d traffic_class %d flow_label 0x%x pkey_index 0x%x\n"
1104 be32toh(addr->qpn), be32toh(addr->qkey), be16toh(addr->lid), addr->sl,
1105 addr->grh_present, (int)addr->gid_index, (int)addr->hop_limit,
1106 (int)addr->traffic_class, addr->flow_label, addr->pkey_index,
1110 void umad_dump(void *umad)
1112 struct ib_user_mad *mad = umad;
1114 IBWARN("agent id %d status %x timeout %d",
1115 mad->agent_id, mad->status, mad->timeout_ms);
1116 umad_addr_dump(&mad->addr);