Path: blob/master/drivers/infiniband/hw/mthca/mthca_profile.c
15112 views
/*1* Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.2* Copyright (c) 2005 Mellanox Technologies. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* 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 OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233#include <linux/module.h>34#include <linux/moduleparam.h>35#include <linux/string.h>36#include <linux/slab.h>3738#include "mthca_profile.h"3940enum {41MTHCA_RES_QP,42MTHCA_RES_EEC,43MTHCA_RES_SRQ,44MTHCA_RES_CQ,45MTHCA_RES_EQP,46MTHCA_RES_EEEC,47MTHCA_RES_EQ,48MTHCA_RES_RDB,49MTHCA_RES_MCG,50MTHCA_RES_MPT,51MTHCA_RES_MTT,52MTHCA_RES_UAR,53MTHCA_RES_UDAV,54MTHCA_RES_UARC,55MTHCA_RES_NUM56};5758enum {59MTHCA_NUM_EQS = 32,60MTHCA_NUM_PDS = 1 << 1561};6263s64 mthca_make_profile(struct mthca_dev *dev,64struct mthca_profile *request,65struct mthca_dev_lim *dev_lim,66struct mthca_init_hca_param *init_hca)67{68struct mthca_resource {69u64 size;70u64 start;71int type;72int num;73int log_num;74};7576u64 mem_base, mem_avail;77s64 total_size = 0;78struct mthca_resource *profile;79struct mthca_resource tmp;80int i, j;8182profile = kzalloc(MTHCA_RES_NUM * sizeof *profile, GFP_KERNEL);83if (!profile)84return -ENOMEM;8586profile[MTHCA_RES_QP].size = dev_lim->qpc_entry_sz;87profile[MTHCA_RES_EEC].size = dev_lim->eec_entry_sz;88profile[MTHCA_RES_SRQ].size = dev_lim->srq_entry_sz;89profile[MTHCA_RES_CQ].size = dev_lim->cqc_entry_sz;90profile[MTHCA_RES_EQP].size = dev_lim->eqpc_entry_sz;91profile[MTHCA_RES_EEEC].size = dev_lim->eeec_entry_sz;92profile[MTHCA_RES_EQ].size = dev_lim->eqc_entry_sz;93profile[MTHCA_RES_RDB].size = MTHCA_RDB_ENTRY_SIZE;94profile[MTHCA_RES_MCG].size = MTHCA_MGM_ENTRY_SIZE;95profile[MTHCA_RES_MPT].size = dev_lim->mpt_entry_sz;96profile[MTHCA_RES_MTT].size = dev->limits.mtt_seg_size;97profile[MTHCA_RES_UAR].size = dev_lim->uar_scratch_entry_sz;98profile[MTHCA_RES_UDAV].size = MTHCA_AV_SIZE;99profile[MTHCA_RES_UARC].size = request->uarc_size;100101profile[MTHCA_RES_QP].num = request->num_qp;102profile[MTHCA_RES_SRQ].num = request->num_srq;103profile[MTHCA_RES_EQP].num = request->num_qp;104profile[MTHCA_RES_RDB].num = request->num_qp * request->rdb_per_qp;105profile[MTHCA_RES_CQ].num = request->num_cq;106profile[MTHCA_RES_EQ].num = MTHCA_NUM_EQS;107profile[MTHCA_RES_MCG].num = request->num_mcg;108profile[MTHCA_RES_MPT].num = request->num_mpt;109profile[MTHCA_RES_MTT].num = request->num_mtt;110profile[MTHCA_RES_UAR].num = request->num_uar;111profile[MTHCA_RES_UARC].num = request->num_uar;112profile[MTHCA_RES_UDAV].num = request->num_udav;113114for (i = 0; i < MTHCA_RES_NUM; ++i) {115profile[i].type = i;116profile[i].log_num = max(ffs(profile[i].num) - 1, 0);117profile[i].size *= profile[i].num;118if (mthca_is_memfree(dev))119profile[i].size = max(profile[i].size, (u64) PAGE_SIZE);120}121122if (mthca_is_memfree(dev)) {123mem_base = 0;124mem_avail = dev_lim->hca.arbel.max_icm_sz;125} else {126mem_base = dev->ddr_start;127mem_avail = dev->fw.tavor.fw_start - dev->ddr_start;128}129130/*131* Sort the resources in decreasing order of size. Since they132* all have sizes that are powers of 2, we'll be able to keep133* resources aligned to their size and pack them without gaps134* using the sorted order.135*/136for (i = MTHCA_RES_NUM; i > 0; --i)137for (j = 1; j < i; ++j) {138if (profile[j].size > profile[j - 1].size) {139tmp = profile[j];140profile[j] = profile[j - 1];141profile[j - 1] = tmp;142}143}144145for (i = 0; i < MTHCA_RES_NUM; ++i) {146if (profile[i].size) {147profile[i].start = mem_base + total_size;148total_size += profile[i].size;149}150if (total_size > mem_avail) {151mthca_err(dev, "Profile requires 0x%llx bytes; "152"won't fit in 0x%llx bytes of context memory.\n",153(unsigned long long) total_size,154(unsigned long long) mem_avail);155kfree(profile);156return -ENOMEM;157}158159if (profile[i].size)160mthca_dbg(dev, "profile[%2d]--%2d/%2d @ 0x%16llx "161"(size 0x%8llx)\n",162i, profile[i].type, profile[i].log_num,163(unsigned long long) profile[i].start,164(unsigned long long) profile[i].size);165}166167if (mthca_is_memfree(dev))168mthca_dbg(dev, "HCA context memory: reserving %d KB\n",169(int) (total_size >> 10));170else171mthca_dbg(dev, "HCA memory: allocated %d KB/%d KB (%d KB free)\n",172(int) (total_size >> 10), (int) (mem_avail >> 10),173(int) ((mem_avail - total_size) >> 10));174175for (i = 0; i < MTHCA_RES_NUM; ++i) {176switch (profile[i].type) {177case MTHCA_RES_QP:178dev->limits.num_qps = profile[i].num;179init_hca->qpc_base = profile[i].start;180init_hca->log_num_qps = profile[i].log_num;181break;182case MTHCA_RES_EEC:183dev->limits.num_eecs = profile[i].num;184init_hca->eec_base = profile[i].start;185init_hca->log_num_eecs = profile[i].log_num;186break;187case MTHCA_RES_SRQ:188dev->limits.num_srqs = profile[i].num;189init_hca->srqc_base = profile[i].start;190init_hca->log_num_srqs = profile[i].log_num;191break;192case MTHCA_RES_CQ:193dev->limits.num_cqs = profile[i].num;194init_hca->cqc_base = profile[i].start;195init_hca->log_num_cqs = profile[i].log_num;196break;197case MTHCA_RES_EQP:198init_hca->eqpc_base = profile[i].start;199break;200case MTHCA_RES_EEEC:201init_hca->eeec_base = profile[i].start;202break;203case MTHCA_RES_EQ:204dev->limits.num_eqs = profile[i].num;205init_hca->eqc_base = profile[i].start;206init_hca->log_num_eqs = profile[i].log_num;207break;208case MTHCA_RES_RDB:209for (dev->qp_table.rdb_shift = 0;210request->num_qp << dev->qp_table.rdb_shift < profile[i].num;211++dev->qp_table.rdb_shift)212; /* nothing */213dev->qp_table.rdb_base = (u32) profile[i].start;214init_hca->rdb_base = profile[i].start;215break;216case MTHCA_RES_MCG:217dev->limits.num_mgms = profile[i].num >> 1;218dev->limits.num_amgms = profile[i].num >> 1;219init_hca->mc_base = profile[i].start;220init_hca->log_mc_entry_sz = ffs(MTHCA_MGM_ENTRY_SIZE) - 1;221init_hca->log_mc_table_sz = profile[i].log_num;222init_hca->mc_hash_sz = 1 << (profile[i].log_num - 1);223break;224case MTHCA_RES_MPT:225dev->limits.num_mpts = profile[i].num;226dev->mr_table.mpt_base = profile[i].start;227init_hca->mpt_base = profile[i].start;228init_hca->log_mpt_sz = profile[i].log_num;229break;230case MTHCA_RES_MTT:231dev->limits.num_mtt_segs = profile[i].num;232dev->mr_table.mtt_base = profile[i].start;233init_hca->mtt_base = profile[i].start;234init_hca->mtt_seg_sz = ffs(dev->limits.mtt_seg_size) - 7;235break;236case MTHCA_RES_UAR:237dev->limits.num_uars = profile[i].num;238init_hca->uar_scratch_base = profile[i].start;239break;240case MTHCA_RES_UDAV:241dev->av_table.ddr_av_base = profile[i].start;242dev->av_table.num_ddr_avs = profile[i].num;243break;244case MTHCA_RES_UARC:245dev->uar_table.uarc_size = request->uarc_size;246dev->uar_table.uarc_base = profile[i].start;247init_hca->uarc_base = profile[i].start;248init_hca->log_uarc_sz = ffs(request->uarc_size) - 13;249init_hca->log_uar_sz = ffs(request->num_uar) - 1;250break;251default:252break;253}254}255256/*257* PDs don't take any HCA memory, but we assign them as part258* of the HCA profile anyway.259*/260dev->limits.num_pds = MTHCA_NUM_PDS;261262if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT &&263init_hca->log_mpt_sz > 23) {264mthca_warn(dev, "MPT table too large (requested size 2^%d >= 2^24)\n",265init_hca->log_mpt_sz);266mthca_warn(dev, "Disabling memory key throughput optimization.\n");267dev->mthca_flags &= ~MTHCA_FLAG_SINAI_OPT;268}269270/*271* For Tavor, FMRs use ioremapped PCI memory. For 32 bit272* systems it may use too much vmalloc space to map all MTT273* memory, so we reserve some MTTs for FMR access, taking them274* out of the MR pool. They don't use additional memory, but275* we assign them as part of the HCA profile anyway.276*/277if (mthca_is_memfree(dev) || BITS_PER_LONG == 64)278dev->limits.fmr_reserved_mtts = 0;279else280dev->limits.fmr_reserved_mtts = request->fmr_reserved_mtts;281282kfree(profile);283return total_size;284}285286287