1/* 2 * Copyright (c) 2004,2005 Voltaire Inc. All rights reserved. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 * 32 */ 33 34#if HAVE_CONFIG_H 35# include <config.h> 36#endif /* HAVE_CONFIG_H */ 37 38#include <stdio.h> 39#include <stdlib.h> 40#include <unistd.h> 41#include <pthread.h> 42#include <sys/time.h> 43#include <string.h> 44#include <netinet/in.h> 45 46#include <infiniband/common.h> 47#include <infiniband/umad.h> 48#include <mad.h> 49 50#undef DEBUG 51#define DEBUG if (ibdebug) IBWARN 52 53int 54mad_send(ib_rpc_t *rpc, ib_portid_t *dport, ib_rmpp_hdr_t *rmpp, void *data) 55{ 56 uint8_t pktbuf[1024]; 57 void *umad = pktbuf; 58 59 memset(pktbuf, 0, umad_size()); 60 61 DEBUG("rmpp %p data %p", rmpp, data); 62 63 if (mad_build_pkt(umad, rpc, dport, rmpp, data) < 0) 64 return 0; 65 66 if (ibdebug) { 67 IBWARN("data offs %d sz %d", rpc->dataoffs, rpc->datasz); 68 xdump(stderr, "mad send data\n", 69 (char *)umad_get_mad(umad) + rpc->dataoffs, rpc->datasz); 70 } 71 72 if (umad_send(madrpc_portid(), mad_class_agent(rpc->mgtclass), 73 umad, IB_MAD_SIZE, rpc->timeout, 0) < 0) { 74 IBWARN("send failed; %m"); 75 return -1; 76 } 77 78 return 0; 79} 80 81int 82mad_respond(void *umad, ib_portid_t *portid, uint32_t rstatus) 83{ 84 uint8_t *mad = umad_get_mad(umad); 85 ib_mad_addr_t *mad_addr; 86 ib_rpc_t rpc = {0}; 87 ib_portid_t rport; 88 int is_smi; 89 90 if (!portid) { 91 if (!(mad_addr = umad_get_mad_addr(umad))) 92 return -1; 93 94 memset(&rport, 0, sizeof(rport)); 95 96 rport.lid = ntohs(mad_addr->lid); 97 rport.qp = ntohl(mad_addr->qpn); 98 rport.qkey = ntohl(mad_addr->qkey); 99 rport.sl = mad_addr->sl; 100 101 portid = &rport; 102 } 103 104 DEBUG("dest %s", portid2str(portid)); 105 106 rpc.mgtclass = mad_get_field(mad, 0, IB_MAD_MGMTCLASS_F); 107 108 rpc.method = mad_get_field(mad, 0, IB_MAD_METHOD_F); 109 if (rpc.method == IB_MAD_METHOD_SET) 110 rpc.method = IB_MAD_METHOD_GET; 111 if (rpc.method != IB_MAD_METHOD_SEND) 112 rpc.method |= IB_MAD_RESPONSE; 113 114 rpc.attr.id = mad_get_field(mad, 0, IB_MAD_ATTRID_F); 115 rpc.attr.mod = mad_get_field(mad, 0, IB_MAD_ATTRMOD_F); 116 if (rpc.mgtclass == IB_SA_CLASS) 117 rpc.recsz = mad_get_field(mad, 0, IB_SA_ATTROFFS_F); 118 if (mad_is_vendor_range2(rpc.mgtclass)) 119 rpc.oui = mad_get_field(mad, 0, IB_VEND2_OUI_F); 120 121 rpc.trid = mad_get_field64(mad, 0, IB_MAD_TRID_F); 122 123 /* cleared by default: timeout, datasz, dataoffs, mkey, mask */ 124 125 is_smi = rpc.mgtclass == IB_SMI_CLASS || 126 rpc.mgtclass == IB_SMI_DIRECT_CLASS; 127 128 if (is_smi) 129 portid->qp = 0; 130 else if (!portid->qp) 131 portid->qp = 1; 132 133 if (!portid->qkey && portid->qp == 1) 134 portid->qkey = IB_DEFAULT_QP1_QKEY; 135 136 DEBUG("qp 0x%x class 0x%x method %d attr 0x%x mod 0x%x datasz %d off %d qkey %x", 137 portid->qp, rpc.mgtclass, rpc.method, rpc.attr.id, rpc.attr.mod, 138 rpc.datasz, rpc.dataoffs, portid->qkey); 139 140 if (mad_build_pkt(umad, &rpc, portid, 0, 0) < 0) 141 return -1; 142 143 if (ibdebug > 1) 144 xdump(stderr, "mad respond pkt\n", mad, IB_MAD_SIZE); 145 146 if (umad_send(madrpc_portid(), mad_class_agent(rpc.mgtclass), umad, 147 IB_MAD_SIZE, rpc.timeout, 0) < 0) { 148 DEBUG("send failed; %m"); 149 return -1; 150 } 151 152 return 0; 153} 154 155void * 156mad_receive(void *umad, int timeout) 157{ 158 void *mad = umad ? umad : umad_alloc(1, umad_size() + IB_MAD_SIZE); 159 int agent; 160 int length = IB_MAD_SIZE; 161 162 if ((agent = umad_recv(madrpc_portid(), mad, 163 &length, timeout)) < 0) { 164 if (!umad) 165 umad_free(mad); 166 DEBUG("recv failed: %m"); 167 return 0; 168 } 169 170 return mad; 171} 172 173void * 174mad_alloc(void) 175{ 176 return umad_alloc(1, umad_size() + IB_MAD_SIZE); 177} 178 179void 180mad_free(void *umad) 181{ 182 umad_free(umad); 183} 184