| 1 |
/* |
| 2 |
* Copyright (c) 2006-2012. QLogic Corporation. All rights reserved. |
| 3 |
* Copyright (c) 2003-2006, PathScale, Inc. All rights reserved. |
| 4 |
* |
| 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: |
| 10 |
* |
| 11 |
* Redistribution and use in source and binary forms, with or |
| 12 |
* without modification, are permitted provided that the following |
| 13 |
* conditions are met: |
| 14 |
* |
| 15 |
* - Redistributions of source code must retain the above |
| 16 |
* copyright notice, this list of conditions and the following |
| 17 |
* disclaimer. |
| 18 |
* |
| 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. |
| 23 |
* |
| 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 |
| 31 |
* SOFTWARE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "ips_proto.h" |
| 35 |
#include "ips_proto_internal.h" |
| 36 |
#include "ips_epstate.h" |
| 37 |
|
| 38 |
/* The indexes are used to map a particular endpoint to a strcture at the |
| 39 |
* receiver. Although we take extra care to validate the identity of endpoints |
| 40 |
* when packets are received, the communication index is at an offset selected |
| 41 |
* by the endpoint that allocates the index. This narrows the window of two |
| 42 |
* jobs communicated with the same set of indexes from getting crosstalk. |
| 43 |
*/ |
| 44 |
/* Allocate new epaddrs in chunks of 128 */ |
| 45 |
#define PTL_EPADDR_ALLOC_CHUNK 128 |
| 46 |
|
| 47 |
psm_error_t |
| 48 |
ips_epstate_init(struct ips_epstate *eps, const psmi_context_t *context) |
| 49 |
{ |
| 50 |
memset(eps, 0, sizeof(*eps)); |
| 51 |
eps->context = context; |
| 52 |
eps->eps_base_idx = ((ips_epstate_idx)get_cycles()) & |
| 53 |
(IPS_EPSTATE_COMMIDX_MAX-1); |
| 54 |
return PSM_OK; |
| 55 |
} |
| 56 |
|
| 57 |
psm_error_t |
| 58 |
ips_epstate_fini(struct ips_epstate *eps) |
| 59 |
{ |
| 60 |
if (eps->eps_tab) |
| 61 |
psmi_free(eps->eps_tab); |
| 62 |
memset(eps, 0, sizeof(*eps)); |
| 63 |
return PSM_OK; |
| 64 |
} |
| 65 |
|
| 66 |
/* |
| 67 |
* Add ipsaddr with epid to the epstate table, return new index to caller in |
| 68 |
* 'commidx'. |
| 69 |
*/ |
| 70 |
psm_error_t |
| 71 |
ips_epstate_add(struct ips_epstate *eps, struct ptl_epaddr *ipsaddr, |
| 72 |
ips_epstate_idx *commidx_o) |
| 73 |
{ |
| 74 |
int i, j; |
| 75 |
ips_epstate_idx commidx; |
| 76 |
uint16_t lmc_mask = ~((1 << ipsaddr->proto->epinfo.ep_lmc) - 1); |
| 77 |
|
| 78 |
if (++eps->eps_tabsizeused > eps->eps_tabsize) { /* realloc */ |
| 79 |
struct ips_epstate_entry *newtab; |
| 80 |
eps->eps_tabsize += PTL_EPADDR_ALLOC_CHUNK; |
| 81 |
newtab = (struct ips_epstate_entry *) |
| 82 |
psmi_calloc(eps->context->ep, PER_PEER_ENDPOINT, eps->eps_tabsize, |
| 83 |
sizeof(struct ips_epstate_entry)); |
| 84 |
if (newtab == NULL) |
| 85 |
return PSM_NO_MEMORY; |
| 86 |
else if (eps->eps_tab) { /* NOT first alloc */ |
| 87 |
for (i = 0; i < eps->eps_tabsize-PTL_EPADDR_ALLOC_CHUNK; i++) |
| 88 |
newtab[i] = eps->eps_tab[i]; /* deep copy */ |
| 89 |
psmi_free(eps->eps_tab); |
| 90 |
} |
| 91 |
eps->eps_tab = newtab; |
| 92 |
} |
| 93 |
/* Find the next free hole. We can afford to do this since connect is not |
| 94 |
* in the critical path */ |
| 95 |
for (i = 0, j = eps->eps_tab_nextidx; i < eps->eps_tabsize; i++, j++) { |
| 96 |
if (j == eps->eps_tabsize) |
| 97 |
j = 0; |
| 98 |
if (eps->eps_tab[j].epid == 0) { |
| 99 |
eps->eps_tab_nextidx = j + 1; |
| 100 |
if (eps->eps_tab_nextidx == eps->eps_tabsize) |
| 101 |
eps->eps_tab_nextidx = 0; |
| 102 |
break; |
| 103 |
} |
| 104 |
} |
| 105 |
psmi_assert_always(i != eps->eps_tabsize); |
| 106 |
commidx = (j - eps->eps_base_idx) & (IPS_EPSTATE_COMMIDX_MAX-1); |
| 107 |
_IPATH_VDBG("node %s gets commidx=%d (table idx %d)\n", |
| 108 |
psmi_epaddr_get_name(ipsaddr->epaddr->epid), commidx, j); |
| 109 |
eps->eps_tab[j].epid = |
| 110 |
PSMI_EPID_PACK(ipsaddr->epr.epr_base_lid & lmc_mask, |
| 111 |
ipsaddr->epr.epr_context, |
| 112 |
ipsaddr->epr.epr_subcontext); |
| 113 |
eps->eps_tab[j].ipsaddr = ipsaddr; |
| 114 |
if (j >= IPS_EPSTATE_COMMIDX_MAX) { |
| 115 |
return psmi_handle_error(eps->context->ep, PSM_TOO_MANY_ENDPOINTS, |
| 116 |
"Can't connect to more than %d non-local endpoints", |
| 117 |
IPS_EPSTATE_COMMIDX_MAX); |
| 118 |
} |
| 119 |
*commidx_o = commidx; |
| 120 |
return PSM_OK; |
| 121 |
} |
| 122 |
|
| 123 |
psm_error_t |
| 124 |
ips_epstate_del(struct ips_epstate *eps, ips_epstate_idx commidx) |
| 125 |
{ |
| 126 |
ips_epstate_idx idx; |
| 127 |
/* actual table index */ |
| 128 |
idx = (commidx + eps->eps_base_idx) & (IPS_EPSTATE_COMMIDX_MAX-1); |
| 129 |
psmi_assert_always(idx < eps->eps_tabsize); |
| 130 |
_IPATH_VDBG("commidx=%d, table_idx=%d\n", commidx, idx); |
| 131 |
eps->eps_tab[idx].epid = 0; |
| 132 |
eps->eps_tab[idx].ipsaddr = NULL; |
| 133 |
/* We may eventually want to release memory, but probably not */ |
| 134 |
eps->eps_tabsizeused--; |
| 135 |
return PSM_OK; |
| 136 |
} |
| 137 |
|