18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
38c2ecf20Sopenharmony_ci * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
48c2ecf20Sopenharmony_ci * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
78c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
88c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
98c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
108c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
138c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
148c2ecf20Sopenharmony_ci *     conditions are met:
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
178c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
188c2ecf20Sopenharmony_ci *        disclaimer.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
218c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
228c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
238c2ecf20Sopenharmony_ci *        provided with the distribution.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
268c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
278c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
288c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
298c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
308c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
318c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
328c2ecf20Sopenharmony_ci * SOFTWARE.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <linux/slab.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#include "mlx4.h"
388c2ecf20Sopenharmony_ci#include "fw.h"
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cienum {
418c2ecf20Sopenharmony_ci	MLX4_RES_QP,
428c2ecf20Sopenharmony_ci	MLX4_RES_RDMARC,
438c2ecf20Sopenharmony_ci	MLX4_RES_ALTC,
448c2ecf20Sopenharmony_ci	MLX4_RES_AUXC,
458c2ecf20Sopenharmony_ci	MLX4_RES_SRQ,
468c2ecf20Sopenharmony_ci	MLX4_RES_CQ,
478c2ecf20Sopenharmony_ci	MLX4_RES_EQ,
488c2ecf20Sopenharmony_ci	MLX4_RES_DMPT,
498c2ecf20Sopenharmony_ci	MLX4_RES_CMPT,
508c2ecf20Sopenharmony_ci	MLX4_RES_MTT,
518c2ecf20Sopenharmony_ci	MLX4_RES_MCG,
528c2ecf20Sopenharmony_ci	MLX4_RES_NUM
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic const char *res_name[] = {
568c2ecf20Sopenharmony_ci	[MLX4_RES_QP]		= "QP",
578c2ecf20Sopenharmony_ci	[MLX4_RES_RDMARC]	= "RDMARC",
588c2ecf20Sopenharmony_ci	[MLX4_RES_ALTC]		= "ALTC",
598c2ecf20Sopenharmony_ci	[MLX4_RES_AUXC]		= "AUXC",
608c2ecf20Sopenharmony_ci	[MLX4_RES_SRQ]		= "SRQ",
618c2ecf20Sopenharmony_ci	[MLX4_RES_CQ]		= "CQ",
628c2ecf20Sopenharmony_ci	[MLX4_RES_EQ]		= "EQ",
638c2ecf20Sopenharmony_ci	[MLX4_RES_DMPT]		= "DMPT",
648c2ecf20Sopenharmony_ci	[MLX4_RES_CMPT]		= "CMPT",
658c2ecf20Sopenharmony_ci	[MLX4_RES_MTT]		= "MTT",
668c2ecf20Sopenharmony_ci	[MLX4_RES_MCG]		= "MCG",
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ciu64 mlx4_make_profile(struct mlx4_dev *dev,
708c2ecf20Sopenharmony_ci		      struct mlx4_profile *request,
718c2ecf20Sopenharmony_ci		      struct mlx4_dev_cap *dev_cap,
728c2ecf20Sopenharmony_ci		      struct mlx4_init_hca_param *init_hca)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct mlx4_priv *priv = mlx4_priv(dev);
758c2ecf20Sopenharmony_ci	struct mlx4_resource {
768c2ecf20Sopenharmony_ci		u64 size;
778c2ecf20Sopenharmony_ci		u64 start;
788c2ecf20Sopenharmony_ci		int type;
798c2ecf20Sopenharmony_ci		u32 num;
808c2ecf20Sopenharmony_ci		int log_num;
818c2ecf20Sopenharmony_ci	};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	u64 total_size = 0;
848c2ecf20Sopenharmony_ci	struct mlx4_resource *profile;
858c2ecf20Sopenharmony_ci	struct sysinfo si;
868c2ecf20Sopenharmony_ci	int i, j;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	profile = kcalloc(MLX4_RES_NUM, sizeof(*profile), GFP_KERNEL);
898c2ecf20Sopenharmony_ci	if (!profile)
908c2ecf20Sopenharmony_ci		return -ENOMEM;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	/*
938c2ecf20Sopenharmony_ci	 * We want to scale the number of MTTs with the size of the
948c2ecf20Sopenharmony_ci	 * system memory, since it makes sense to register a lot of
958c2ecf20Sopenharmony_ci	 * memory on a system with a lot of memory.  As a heuristic,
968c2ecf20Sopenharmony_ci	 * make sure we have enough MTTs to cover twice the system
978c2ecf20Sopenharmony_ci	 * memory (with PAGE_SIZE entries).
988c2ecf20Sopenharmony_ci	 *
998c2ecf20Sopenharmony_ci	 * This number has to be a power of two and fit into 32 bits
1008c2ecf20Sopenharmony_ci	 * due to device limitations, so cap this at 2^31 as well.
1018c2ecf20Sopenharmony_ci	 * That limits us to 8TB of memory registration per HCA with
1028c2ecf20Sopenharmony_ci	 * 4KB pages, which is probably OK for the next few months.
1038c2ecf20Sopenharmony_ci	 */
1048c2ecf20Sopenharmony_ci	si_meminfo(&si);
1058c2ecf20Sopenharmony_ci	request->num_mtt =
1068c2ecf20Sopenharmony_ci		roundup_pow_of_two(max_t(unsigned, request->num_mtt,
1078c2ecf20Sopenharmony_ci					 min(1UL << (31 - log_mtts_per_seg),
1088c2ecf20Sopenharmony_ci					     (si.totalram << 1) >> log_mtts_per_seg)));
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	profile[MLX4_RES_QP].size     = dev_cap->qpc_entry_sz;
1128c2ecf20Sopenharmony_ci	profile[MLX4_RES_RDMARC].size = dev_cap->rdmarc_entry_sz;
1138c2ecf20Sopenharmony_ci	profile[MLX4_RES_ALTC].size   = dev_cap->altc_entry_sz;
1148c2ecf20Sopenharmony_ci	profile[MLX4_RES_AUXC].size   = dev_cap->aux_entry_sz;
1158c2ecf20Sopenharmony_ci	profile[MLX4_RES_SRQ].size    = dev_cap->srq_entry_sz;
1168c2ecf20Sopenharmony_ci	profile[MLX4_RES_CQ].size     = dev_cap->cqc_entry_sz;
1178c2ecf20Sopenharmony_ci	profile[MLX4_RES_EQ].size     = dev_cap->eqc_entry_sz;
1188c2ecf20Sopenharmony_ci	profile[MLX4_RES_DMPT].size   = dev_cap->dmpt_entry_sz;
1198c2ecf20Sopenharmony_ci	profile[MLX4_RES_CMPT].size   = dev_cap->cmpt_entry_sz;
1208c2ecf20Sopenharmony_ci	profile[MLX4_RES_MTT].size    = dev_cap->mtt_entry_sz;
1218c2ecf20Sopenharmony_ci	profile[MLX4_RES_MCG].size    = mlx4_get_mgm_entry_size(dev);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	profile[MLX4_RES_QP].num      = request->num_qp;
1248c2ecf20Sopenharmony_ci	profile[MLX4_RES_RDMARC].num  = request->num_qp * request->rdmarc_per_qp;
1258c2ecf20Sopenharmony_ci	profile[MLX4_RES_ALTC].num    = request->num_qp;
1268c2ecf20Sopenharmony_ci	profile[MLX4_RES_AUXC].num    = request->num_qp;
1278c2ecf20Sopenharmony_ci	profile[MLX4_RES_SRQ].num     = request->num_srq;
1288c2ecf20Sopenharmony_ci	profile[MLX4_RES_CQ].num      = request->num_cq;
1298c2ecf20Sopenharmony_ci	profile[MLX4_RES_EQ].num = mlx4_is_mfunc(dev) ? dev->phys_caps.num_phys_eqs :
1308c2ecf20Sopenharmony_ci					min_t(unsigned, dev_cap->max_eqs, MAX_MSIX);
1318c2ecf20Sopenharmony_ci	profile[MLX4_RES_DMPT].num    = request->num_mpt;
1328c2ecf20Sopenharmony_ci	profile[MLX4_RES_CMPT].num    = MLX4_NUM_CMPTS;
1338c2ecf20Sopenharmony_ci	profile[MLX4_RES_MTT].num     = request->num_mtt * (1 << log_mtts_per_seg);
1348c2ecf20Sopenharmony_ci	profile[MLX4_RES_MCG].num     = request->num_mcg;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	for (i = 0; i < MLX4_RES_NUM; ++i) {
1378c2ecf20Sopenharmony_ci		profile[i].type     = i;
1388c2ecf20Sopenharmony_ci		profile[i].num      = roundup_pow_of_two(profile[i].num);
1398c2ecf20Sopenharmony_ci		profile[i].log_num  = ilog2(profile[i].num);
1408c2ecf20Sopenharmony_ci		profile[i].size    *= profile[i].num;
1418c2ecf20Sopenharmony_ci		profile[i].size     = max(profile[i].size, (u64) PAGE_SIZE);
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/*
1458c2ecf20Sopenharmony_ci	 * Sort the resources in decreasing order of size.  Since they
1468c2ecf20Sopenharmony_ci	 * all have sizes that are powers of 2, we'll be able to keep
1478c2ecf20Sopenharmony_ci	 * resources aligned to their size and pack them without gaps
1488c2ecf20Sopenharmony_ci	 * using the sorted order.
1498c2ecf20Sopenharmony_ci	 */
1508c2ecf20Sopenharmony_ci	for (i = MLX4_RES_NUM; i > 0; --i)
1518c2ecf20Sopenharmony_ci		for (j = 1; j < i; ++j) {
1528c2ecf20Sopenharmony_ci			if (profile[j].size > profile[j - 1].size)
1538c2ecf20Sopenharmony_ci				swap(profile[j], profile[j - 1]);
1548c2ecf20Sopenharmony_ci		}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	for (i = 0; i < MLX4_RES_NUM; ++i) {
1578c2ecf20Sopenharmony_ci		if (profile[i].size) {
1588c2ecf20Sopenharmony_ci			profile[i].start = total_size;
1598c2ecf20Sopenharmony_ci			total_size	+= profile[i].size;
1608c2ecf20Sopenharmony_ci		}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci		if (total_size > dev_cap->max_icm_sz) {
1638c2ecf20Sopenharmony_ci			mlx4_err(dev, "Profile requires 0x%llx bytes; won't fit in 0x%llx bytes of context memory\n",
1648c2ecf20Sopenharmony_ci				 (unsigned long long) total_size,
1658c2ecf20Sopenharmony_ci				 (unsigned long long) dev_cap->max_icm_sz);
1668c2ecf20Sopenharmony_ci			kfree(profile);
1678c2ecf20Sopenharmony_ci			return -ENOMEM;
1688c2ecf20Sopenharmony_ci		}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci		if (profile[i].size)
1718c2ecf20Sopenharmony_ci			mlx4_dbg(dev, "  profile[%2d] (%6s): 2^%02d entries @ 0x%10llx, size 0x%10llx\n",
1728c2ecf20Sopenharmony_ci				 i, res_name[profile[i].type],
1738c2ecf20Sopenharmony_ci				 profile[i].log_num,
1748c2ecf20Sopenharmony_ci				 (unsigned long long) profile[i].start,
1758c2ecf20Sopenharmony_ci				 (unsigned long long) profile[i].size);
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	mlx4_dbg(dev, "HCA context memory: reserving %d KB\n",
1798c2ecf20Sopenharmony_ci		 (int) (total_size >> 10));
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	for (i = 0; i < MLX4_RES_NUM; ++i) {
1828c2ecf20Sopenharmony_ci		switch (profile[i].type) {
1838c2ecf20Sopenharmony_ci		case MLX4_RES_QP:
1848c2ecf20Sopenharmony_ci			dev->caps.num_qps     = profile[i].num;
1858c2ecf20Sopenharmony_ci			init_hca->qpc_base    = profile[i].start;
1868c2ecf20Sopenharmony_ci			init_hca->log_num_qps = profile[i].log_num;
1878c2ecf20Sopenharmony_ci			break;
1888c2ecf20Sopenharmony_ci		case MLX4_RES_RDMARC:
1898c2ecf20Sopenharmony_ci			for (priv->qp_table.rdmarc_shift = 0;
1908c2ecf20Sopenharmony_ci			     request->num_qp << priv->qp_table.rdmarc_shift < profile[i].num;
1918c2ecf20Sopenharmony_ci			     ++priv->qp_table.rdmarc_shift)
1928c2ecf20Sopenharmony_ci				; /* nothing */
1938c2ecf20Sopenharmony_ci			dev->caps.max_qp_dest_rdma = 1 << priv->qp_table.rdmarc_shift;
1948c2ecf20Sopenharmony_ci			priv->qp_table.rdmarc_base   = (u32) profile[i].start;
1958c2ecf20Sopenharmony_ci			init_hca->rdmarc_base	     = profile[i].start;
1968c2ecf20Sopenharmony_ci			init_hca->log_rd_per_qp	     = priv->qp_table.rdmarc_shift;
1978c2ecf20Sopenharmony_ci			break;
1988c2ecf20Sopenharmony_ci		case MLX4_RES_ALTC:
1998c2ecf20Sopenharmony_ci			init_hca->altc_base = profile[i].start;
2008c2ecf20Sopenharmony_ci			break;
2018c2ecf20Sopenharmony_ci		case MLX4_RES_AUXC:
2028c2ecf20Sopenharmony_ci			init_hca->auxc_base = profile[i].start;
2038c2ecf20Sopenharmony_ci			break;
2048c2ecf20Sopenharmony_ci		case MLX4_RES_SRQ:
2058c2ecf20Sopenharmony_ci			dev->caps.num_srqs     = profile[i].num;
2068c2ecf20Sopenharmony_ci			init_hca->srqc_base    = profile[i].start;
2078c2ecf20Sopenharmony_ci			init_hca->log_num_srqs = profile[i].log_num;
2088c2ecf20Sopenharmony_ci			break;
2098c2ecf20Sopenharmony_ci		case MLX4_RES_CQ:
2108c2ecf20Sopenharmony_ci			dev->caps.num_cqs     = profile[i].num;
2118c2ecf20Sopenharmony_ci			init_hca->cqc_base    = profile[i].start;
2128c2ecf20Sopenharmony_ci			init_hca->log_num_cqs = profile[i].log_num;
2138c2ecf20Sopenharmony_ci			break;
2148c2ecf20Sopenharmony_ci		case MLX4_RES_EQ:
2158c2ecf20Sopenharmony_ci			if (dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS) {
2168c2ecf20Sopenharmony_ci				init_hca->log_num_eqs = 0x1f;
2178c2ecf20Sopenharmony_ci				init_hca->eqc_base    = profile[i].start;
2188c2ecf20Sopenharmony_ci				init_hca->num_sys_eqs = dev_cap->num_sys_eqs;
2198c2ecf20Sopenharmony_ci			} else {
2208c2ecf20Sopenharmony_ci				dev->caps.num_eqs     = roundup_pow_of_two(
2218c2ecf20Sopenharmony_ci								min_t(unsigned,
2228c2ecf20Sopenharmony_ci								      dev_cap->max_eqs,
2238c2ecf20Sopenharmony_ci								      MAX_MSIX));
2248c2ecf20Sopenharmony_ci				init_hca->eqc_base    = profile[i].start;
2258c2ecf20Sopenharmony_ci				init_hca->log_num_eqs = ilog2(dev->caps.num_eqs);
2268c2ecf20Sopenharmony_ci			}
2278c2ecf20Sopenharmony_ci			break;
2288c2ecf20Sopenharmony_ci		case MLX4_RES_DMPT:
2298c2ecf20Sopenharmony_ci			dev->caps.num_mpts	= profile[i].num;
2308c2ecf20Sopenharmony_ci			priv->mr_table.mpt_base = profile[i].start;
2318c2ecf20Sopenharmony_ci			init_hca->dmpt_base	= profile[i].start;
2328c2ecf20Sopenharmony_ci			init_hca->log_mpt_sz	= profile[i].log_num;
2338c2ecf20Sopenharmony_ci			break;
2348c2ecf20Sopenharmony_ci		case MLX4_RES_CMPT:
2358c2ecf20Sopenharmony_ci			init_hca->cmpt_base	 = profile[i].start;
2368c2ecf20Sopenharmony_ci			break;
2378c2ecf20Sopenharmony_ci		case MLX4_RES_MTT:
2388c2ecf20Sopenharmony_ci			dev->caps.num_mtts	 = profile[i].num;
2398c2ecf20Sopenharmony_ci			priv->mr_table.mtt_base	 = profile[i].start;
2408c2ecf20Sopenharmony_ci			init_hca->mtt_base	 = profile[i].start;
2418c2ecf20Sopenharmony_ci			break;
2428c2ecf20Sopenharmony_ci		case MLX4_RES_MCG:
2438c2ecf20Sopenharmony_ci			init_hca->mc_base	  = profile[i].start;
2448c2ecf20Sopenharmony_ci			init_hca->log_mc_entry_sz =
2458c2ecf20Sopenharmony_ci					ilog2(mlx4_get_mgm_entry_size(dev));
2468c2ecf20Sopenharmony_ci			init_hca->log_mc_table_sz = profile[i].log_num;
2478c2ecf20Sopenharmony_ci			if (dev->caps.steering_mode ==
2488c2ecf20Sopenharmony_ci			    MLX4_STEERING_MODE_DEVICE_MANAGED) {
2498c2ecf20Sopenharmony_ci				dev->caps.num_mgms = profile[i].num;
2508c2ecf20Sopenharmony_ci			} else {
2518c2ecf20Sopenharmony_ci				init_hca->log_mc_hash_sz =
2528c2ecf20Sopenharmony_ci						profile[i].log_num - 1;
2538c2ecf20Sopenharmony_ci				dev->caps.num_mgms = profile[i].num >> 1;
2548c2ecf20Sopenharmony_ci				dev->caps.num_amgms = profile[i].num >> 1;
2558c2ecf20Sopenharmony_ci			}
2568c2ecf20Sopenharmony_ci			break;
2578c2ecf20Sopenharmony_ci		default:
2588c2ecf20Sopenharmony_ci			break;
2598c2ecf20Sopenharmony_ci		}
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	/*
2638c2ecf20Sopenharmony_ci	 * PDs don't take any HCA memory, but we assign them as part
2648c2ecf20Sopenharmony_ci	 * of the HCA profile anyway.
2658c2ecf20Sopenharmony_ci	 */
2668c2ecf20Sopenharmony_ci	dev->caps.num_pds = MLX4_NUM_PDS;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	kfree(profile);
2698c2ecf20Sopenharmony_ci	return total_size;
2708c2ecf20Sopenharmony_ci}
271