uint32_t *mlx5_alloc_dbrec(struct mlx5_context *context) { struct mlx5_db_page *page; uint32_t *db = NULL; int i, j; pthread_mutex_lock(&context->db_list_mutex); for (page = context->db_list; page; page = page->next) if (page->use_cnt < page->num_db) goto found; page = __add_page(context); if (!page) goto out; found: ++page->use_cnt; for (i = 0; !page->free[i]; ++i) /* nothing */; j = ffsl(page->free[i]); --j; page->free[i] &= ~(1UL << j); db = page->buf.buf + (i * 8 * sizeof(long) + j) * context->cache_line_size; out: pthread_mutex_unlock(&context->db_list_mutex); return db; }
int mthca_alloc_av(struct mthca_pd *pd, struct ibv_ah_attr *attr, struct mthca_ah *ah) { if (mthca_is_memfree(pd->ibv_pd.context)) { ah->av = malloc(sizeof *ah->av); if (!ah->av) return -1; } else { struct mthca_ah_page *page; int ps; int pp; int i, j; ps = to_mdev(pd->ibv_pd.context->device)->page_size; pp = ps / (sizeof *ah->av * 8 * sizeof (int)); pthread_mutex_lock(&pd->ah_mutex); for (page = pd->ah_list; page; page = page->next) if (page->use_cnt < ps / sizeof *ah->av) for (i = 0; i < pp; ++i) if (page->free[i]) goto found; page = __add_page(pd, ps, pp); if (!page) { pthread_mutex_unlock(&pd->ah_mutex); return -1; } found: ++page->use_cnt; for (i = 0, j = -1; i < pp; ++i) if (page->free[i]) { j = ffs(page->free[i]); page->free[i] &= ~(1 << (j - 1)); ah->av = page->buf.buf + (i * 8 * sizeof (int) + (j - 1)) * sizeof *ah->av; break; } ah->key = page->mr->lkey; ah->page = page; pthread_mutex_unlock(&pd->ah_mutex); } memset(ah->av, 0, sizeof *ah->av); ah->av->port_pd = htonl(pd->pdn | (attr->port_num << 24)); ah->av->g_slid = attr->src_path_bits; ah->av->dlid = htons(attr->dlid); ah->av->msg_sr = (3 << 4) | /* 2K message */ attr->static_rate; ah->av->sl_tclass_flowlabel = htonl(attr->sl << 28); if (attr->is_global) { ah->av->g_slid |= 0x80; /* XXX get gid_table length */ ah->av->gid_index = (attr->port_num - 1) * 32 + attr->grh.sgid_index; ah->av->hop_limit = attr->grh.hop_limit; ah->av->sl_tclass_flowlabel |= htonl((attr->grh.traffic_class << 20) | attr->grh.flow_label); memcpy(ah->av->dgid, attr->grh.dgid.raw, 16); } else { /* Arbel workaround -- low byte of GID must be 2 */ ah->av->dgid[3] = htonl(2); } return 0; }