1/* 2 * Copyright (c) 2005 Topspin Communications. 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#if HAVE_CONFIG_H 34# include <config.h> 35#endif /* HAVE_CONFIG_H */ 36 37#include <stdlib.h> 38#include <netinet/in.h> 39#include <pthread.h> 40#include <string.h> 41 42#include "mthca.h" 43 44struct mthca_ah_page { 45 struct mthca_ah_page *prev, *next; 46 struct mthca_buf buf; 47 struct ibv_mr *mr; 48 int use_cnt; 49 unsigned free[0]; 50}; 51 52static struct mthca_ah_page *__add_page(struct mthca_pd *pd, int page_size, int per_page) 53{ 54 struct mthca_ah_page *page; 55 int i; 56 57 page = malloc(sizeof *page + per_page * sizeof (int)); 58 if (!page) 59 return NULL; 60 61 if (mthca_alloc_buf(&page->buf, page_size, page_size)) { 62 free(page); 63 return NULL; 64 } 65 66 page->mr = mthca_reg_mr(&pd->ibv_pd, page->buf.buf, page_size, 0); 67 if (!page->mr) { 68 mthca_free_buf(&page->buf); 69 free(page); 70 return NULL; 71 } 72 73 page->mr->context = pd->ibv_pd.context; 74 75 page->use_cnt = 0; 76 for (i = 0; i < per_page; ++i) 77 page->free[i] = ~0; 78 79 page->prev = NULL; 80 page->next = pd->ah_list; 81 pd->ah_list = page; 82 if (page->next) 83 page->next->prev = page; 84 85 return page; 86} 87 88int mthca_alloc_av(struct mthca_pd *pd, struct ibv_ah_attr *attr, 89 struct mthca_ah *ah) 90{ 91 if (mthca_is_memfree(pd->ibv_pd.context)) { 92 ah->av = malloc(sizeof *ah->av); 93 if (!ah->av) 94 return -1; 95 } else { 96 struct mthca_ah_page *page; 97 int ps; 98 int pp; 99 int i, j; 100 101 ps = to_mdev(pd->ibv_pd.context->device)->page_size; 102 pp = ps / (sizeof *ah->av * 8 * sizeof (int)); 103 104 pthread_mutex_lock(&pd->ah_mutex); 105 for (page = pd->ah_list; page; page = page->next) 106 if (page->use_cnt < ps / sizeof *ah->av) 107 for (i = 0; i < pp; ++i) 108 if (page->free[i]) 109 goto found; 110 111 page = __add_page(pd, ps, pp); 112 if (!page) { 113 pthread_mutex_unlock(&pd->ah_mutex); 114 return -1; 115 } 116 117 found: 118 ++page->use_cnt; 119 120 for (i = 0, j = -1; i < pp; ++i) 121 if (page->free[i]) { 122 j = ffs(page->free[i]); 123 page->free[i] &= ~(1 << (j - 1)); 124 ah->av = page->buf.buf + 125 (i * 8 * sizeof (int) + (j - 1)) * sizeof *ah->av; 126 break; 127 } 128 129 ah->key = page->mr->lkey; 130 ah->page = page; 131 132 pthread_mutex_unlock(&pd->ah_mutex); 133 } 134 135 memset(ah->av, 0, sizeof *ah->av); 136 137 ah->av->port_pd = htonl(pd->pdn | (attr->port_num << 24)); 138 ah->av->g_slid = attr->src_path_bits; 139 ah->av->dlid = htons(attr->dlid); 140 ah->av->msg_sr = (3 << 4) | /* 2K message */ 141 attr->static_rate; 142 ah->av->sl_tclass_flowlabel = htonl(attr->sl << 28); 143 if (attr->is_global) { 144 ah->av->g_slid |= 0x80; 145 /* XXX get gid_table length */ 146 ah->av->gid_index = (attr->port_num - 1) * 32 + 147 attr->grh.sgid_index; 148 ah->av->hop_limit = attr->grh.hop_limit; 149 ah->av->sl_tclass_flowlabel |= 150 htonl((attr->grh.traffic_class << 20) | 151 attr->grh.flow_label); 152 memcpy(ah->av->dgid, attr->grh.dgid.raw, 16); 153 } else { 154 /* Arbel workaround -- low byte of GID must be 2 */ 155 ah->av->dgid[3] = htonl(2); 156 } 157 158 return 0; 159} 160 161void mthca_free_av(struct mthca_ah *ah) 162{ 163 if (mthca_is_memfree(ah->ibv_ah.context)) { 164 free(ah->av); 165 } else { 166 struct mthca_pd *pd = to_mpd(ah->ibv_ah.pd); 167 struct mthca_ah_page *page; 168 int i; 169 170 pthread_mutex_lock(&pd->ah_mutex); 171 172 page = ah->page; 173 i = ((void *) ah->av - page->buf.buf) / sizeof *ah->av; 174 page->free[i / (8 * sizeof (int))] |= 1 << (i % (8 * sizeof (int))); 175 176 if (!--page->use_cnt) { 177 if (page->prev) 178 page->prev->next = page->next; 179 else 180 pd->ah_list = page->next; 181 if (page->next) 182 page->next->prev = page->prev; 183 184 mthca_dereg_mr(page->mr); 185 mthca_free_buf(&page->buf); 186 free(page); 187 } 188 189 pthread_mutex_unlock(&pd->ah_mutex); 190 } 191} 192