141444Sdillon/* 237Srgrimes * Copyright (c) 2004-2009 Voltaire Inc. All rights reserved. 337Srgrimes * 437Srgrimes * This software is available to you under a choice of one of two 537Srgrimes * licenses. You may choose to be licensed under the terms of the GNU 637Srgrimes * General Public License (GPL) Version 2, available from the file 737Srgrimes * COPYING in the main directory of this source tree, or the 837Srgrimes * OpenIB.org BSD license below: 937Srgrimes * 1037Srgrimes * Redistribution and use in source and binary forms, with or 1139825Swosch * without modification, are permitted provided that the following 123190Spst * conditions are met: 1318639Spst * 1437Srgrimes * - Redistributions of source code must retain the above 1541444Sdillon * copyright notice, this list of conditions and the following 1641444Sdillon * disclaimer. 1741441Sdillon * 1841441Sdillon * - Redistributions in binary form must reproduce the above 1918639Spst * copyright notice, this list of conditions and the following 2018639Spst * disclaimer in the documentation and/or other materials 2118639Spst * provided with the distribution. 2218639Spst * 2318639Spst * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 2418639Spst * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2518639Spst * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 2618639Spst * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 2718639Spst * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 2818639Spst * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 2918639Spst * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 3018639Spst * SOFTWARE. 315183Swollman * 3218639Spst */ 3318639Spst 3418639Spst#if HAVE_CONFIG_H 3518639Spst# include <config.h> 3618639Spst#endif /* HAVE_CONFIG_H */ 3737Srgrimes 3818639Spst#include <stdio.h> 3919607Speter#include <stdlib.h> 4019607Speter#include <string.h> 4119607Speter#include <errno.h> 4238337Smarkm 4318639Spst#include <infiniband/umad.h> 4438337Smarkm#include <infiniband/mad.h> 4518639Spst 4638337Smarkm#include "mad_internal.h" 4738337Smarkm 48591Srgrimes#undef DEBUG 4918639Spst#define DEBUG if (ibdebug) IBWARN 50831Sats 5118639Spstint mad_send(ib_rpc_t * rpc, ib_portid_t * dport, ib_rmpp_hdr_t * rmpp, 5218639Spst void *data) 5318639Spst{ 5418639Spst return mad_send_via(rpc, dport, rmpp, data, ibmp); 5518639Spst} 5618639Spst 5718639Spstint mad_send_via(ib_rpc_t * rpc, ib_portid_t * dport, ib_rmpp_hdr_t * rmpp, 5818639Spst void *data, struct ibmad_port *srcport) 5918639Spst{ 604652Sats uint8_t pktbuf[1024]; 61831Sats void *umad = pktbuf; 6221613Sache 6321613Sache memset(pktbuf, 0, umad_size() + IB_MAD_SIZE); 6421613Sache 6521613Sache DEBUG("rmpp %p data %p", rmpp, data); 6640911Sphk 6740911Sphk if (mad_build_pkt(umad, rpc, dport, rmpp, data) < 0) 6840911Sphk return -1; 6940911Sphk 7018639Spst if (ibdebug) { 7118639Spst IBWARN("data offs %d sz %d", rpc->dataoffs, rpc->datasz); 7241441Sdillon xdump(stderr, "mad send data\n", 7329951Sjkh (char *)umad_get_mad(umad) + rpc->dataoffs, rpc->datasz); 7437741Shoek } 7537741Shoek 7637741Shoek if (umad_send(srcport->port_id, srcport->class_agents[rpc->mgtclass & 0xff], 7737741Shoek umad, IB_MAD_SIZE, mad_get_timeout(srcport, rpc->timeout), 7829951Sjkh 0) < 0) { 7929951Sjkh IBWARN("send failed; %s", strerror(errno)); 8029951Sjkh return -1; 8129951Sjkh } 8229951Sjkh 83 return 0; 84} 85 86int mad_respond(void *umad, ib_portid_t * portid, uint32_t rstatus) 87{ 88 return mad_respond_via(umad, portid, rstatus, ibmp); 89} 90 91int mad_respond_via(void *umad, ib_portid_t * portid, uint32_t rstatus, 92 struct ibmad_port *srcport) 93{ 94 uint8_t *mad = umad_get_mad(umad); 95 ib_mad_addr_t *mad_addr; 96 ib_rpc_t rpc = { 0 }; 97 ib_portid_t rport; 98 int is_smi; 99 100 if (!portid) { 101 if (!(mad_addr = umad_get_mad_addr(umad))) { 102 errno = EINVAL; 103 return -1; 104 } 105 106 memset(&rport, 0, sizeof(rport)); 107 108 rport.lid = ntohs(mad_addr->lid); 109 rport.qp = ntohl(mad_addr->qpn); 110 rport.qkey = ntohl(mad_addr->qkey); 111 rport.sl = mad_addr->sl; 112 113 if (mad_addr->grh_present) { 114 rport.grh_present = 1; 115 memcpy(&rport.gid, &mad_addr->gid, sizeof(rport.gid)); 116 } 117 118 portid = &rport; 119 } 120 121 DEBUG("dest %s", portid2str(portid)); 122 123 rpc.mgtclass = mad_get_field(mad, 0, IB_MAD_MGMTCLASS_F); 124 125 rpc.method = mad_get_field(mad, 0, IB_MAD_METHOD_F); 126 if (rpc.method == IB_MAD_METHOD_SET) 127 rpc.method = IB_MAD_METHOD_GET; 128 if (rpc.method != IB_MAD_METHOD_SEND) 129 rpc.method |= IB_MAD_RESPONSE; 130 131 rpc.attr.id = mad_get_field(mad, 0, IB_MAD_ATTRID_F); 132 rpc.attr.mod = mad_get_field(mad, 0, IB_MAD_ATTRMOD_F); 133 if (rpc.mgtclass == IB_SA_CLASS) 134 rpc.recsz = mad_get_field(mad, 0, IB_SA_ATTROFFS_F); 135 if (mad_is_vendor_range2(rpc.mgtclass)) 136 rpc.oui = mad_get_field(mad, 0, IB_VEND2_OUI_F); 137 138 rpc.trid = mad_get_field64(mad, 0, IB_MAD_TRID_F); 139 rpc.rstatus = rstatus; 140 141 /* cleared by default: timeout, datasz, dataoffs, mkey, mask */ 142 143 is_smi = rpc.mgtclass == IB_SMI_CLASS || 144 rpc.mgtclass == IB_SMI_DIRECT_CLASS; 145 146 if (is_smi) 147 portid->qp = 0; 148 else if (!portid->qp) 149 portid->qp = 1; 150 151 if (!portid->qkey && portid->qp == 1) 152 portid->qkey = IB_DEFAULT_QP1_QKEY; 153 154 DEBUG 155 ("qp 0x%x class 0x%x method %d attr 0x%x mod 0x%x datasz %d off %d qkey %x", 156 portid->qp, rpc.mgtclass, rpc.method, rpc.attr.id, rpc.attr.mod, 157 rpc.datasz, rpc.dataoffs, portid->qkey); 158 159 if (mad_build_pkt(umad, &rpc, portid, 0, 0) < 0) 160 return -1; 161 162 if (ibdebug > 1) 163 xdump(stderr, "mad respond pkt\n", mad, IB_MAD_SIZE); 164 165 if (umad_send 166 (srcport->port_id, srcport->class_agents[rpc.mgtclass], umad, 167 IB_MAD_SIZE, mad_get_timeout(srcport, rpc.timeout), 0) < 0) { 168 DEBUG("send failed; %s", strerror(errno)); 169 return -1; 170 } 171 172 return 0; 173} 174 175void *mad_receive(void *umad, int timeout) 176{ 177 return mad_receive_via(umad, timeout, ibmp); 178} 179 180void *mad_receive_via(void *umad, int timeout, struct ibmad_port *srcport) 181{ 182 void *mad = umad ? umad : umad_alloc(1, umad_size() + IB_MAD_SIZE); 183 int agent; 184 int length = IB_MAD_SIZE; 185 186 if ((agent = umad_recv(srcport->port_id, mad, &length, 187 mad_get_timeout(srcport, timeout))) < 0) { 188 if (!umad) 189 umad_free(mad); 190 DEBUG("recv failed: %s", strerror(errno)); 191 return 0; 192 } 193 194 return mad; 195} 196 197void *mad_alloc(void) 198{ 199 return umad_alloc(1, umad_size() + IB_MAD_SIZE); 200} 201 202void mad_free(void *umad) 203{ 204 umad_free(umad); 205} 206