15a2cc190SJeff Kirsher /*
25a2cc190SJeff Kirsher * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
35a2cc190SJeff Kirsher * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
45a2cc190SJeff Kirsher * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
55a2cc190SJeff Kirsher *
65a2cc190SJeff Kirsher * This software is available to you under a choice of one of two
75a2cc190SJeff Kirsher * licenses. You may choose to be licensed under the terms of the GNU
85a2cc190SJeff Kirsher * General Public License (GPL) Version 2, available from the file
95a2cc190SJeff Kirsher * COPYING in the main directory of this source tree, or the
105a2cc190SJeff Kirsher * OpenIB.org BSD license below:
115a2cc190SJeff Kirsher *
125a2cc190SJeff Kirsher * Redistribution and use in source and binary forms, with or
135a2cc190SJeff Kirsher * without modification, are permitted provided that the following
145a2cc190SJeff Kirsher * conditions are met:
155a2cc190SJeff Kirsher *
165a2cc190SJeff Kirsher * - Redistributions of source code must retain the above
175a2cc190SJeff Kirsher * copyright notice, this list of conditions and the following
185a2cc190SJeff Kirsher * disclaimer.
195a2cc190SJeff Kirsher *
205a2cc190SJeff Kirsher * - Redistributions in binary form must reproduce the above
215a2cc190SJeff Kirsher * copyright notice, this list of conditions and the following
225a2cc190SJeff Kirsher * disclaimer in the documentation and/or other materials
235a2cc190SJeff Kirsher * provided with the distribution.
245a2cc190SJeff Kirsher *
255a2cc190SJeff Kirsher * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
265a2cc190SJeff Kirsher * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
275a2cc190SJeff Kirsher * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
285a2cc190SJeff Kirsher * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
295a2cc190SJeff Kirsher * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
305a2cc190SJeff Kirsher * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
315a2cc190SJeff Kirsher * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
325a2cc190SJeff Kirsher * SOFTWARE.
335a2cc190SJeff Kirsher */
345a2cc190SJeff Kirsher
355a2cc190SJeff Kirsher #include <linux/slab.h>
365a2cc190SJeff Kirsher
375a2cc190SJeff Kirsher #include "mlx4.h"
385a2cc190SJeff Kirsher #include "fw.h"
395a2cc190SJeff Kirsher
405a2cc190SJeff Kirsher enum {
415a2cc190SJeff Kirsher MLX4_RES_QP,
425a2cc190SJeff Kirsher MLX4_RES_RDMARC,
435a2cc190SJeff Kirsher MLX4_RES_ALTC,
445a2cc190SJeff Kirsher MLX4_RES_AUXC,
455a2cc190SJeff Kirsher MLX4_RES_SRQ,
465a2cc190SJeff Kirsher MLX4_RES_CQ,
475a2cc190SJeff Kirsher MLX4_RES_EQ,
485a2cc190SJeff Kirsher MLX4_RES_DMPT,
495a2cc190SJeff Kirsher MLX4_RES_CMPT,
505a2cc190SJeff Kirsher MLX4_RES_MTT,
515a2cc190SJeff Kirsher MLX4_RES_MCG,
525a2cc190SJeff Kirsher MLX4_RES_NUM
535a2cc190SJeff Kirsher };
545a2cc190SJeff Kirsher
555a2cc190SJeff Kirsher static const char *res_name[] = {
565a2cc190SJeff Kirsher [MLX4_RES_QP] = "QP",
575a2cc190SJeff Kirsher [MLX4_RES_RDMARC] = "RDMARC",
585a2cc190SJeff Kirsher [MLX4_RES_ALTC] = "ALTC",
595a2cc190SJeff Kirsher [MLX4_RES_AUXC] = "AUXC",
605a2cc190SJeff Kirsher [MLX4_RES_SRQ] = "SRQ",
615a2cc190SJeff Kirsher [MLX4_RES_CQ] = "CQ",
625a2cc190SJeff Kirsher [MLX4_RES_EQ] = "EQ",
635a2cc190SJeff Kirsher [MLX4_RES_DMPT] = "DMPT",
645a2cc190SJeff Kirsher [MLX4_RES_CMPT] = "CMPT",
655a2cc190SJeff Kirsher [MLX4_RES_MTT] = "MTT",
665a2cc190SJeff Kirsher [MLX4_RES_MCG] = "MCG",
675a2cc190SJeff Kirsher };
685a2cc190SJeff Kirsher
mlx4_make_profile(struct mlx4_dev * dev,struct mlx4_profile * request,struct mlx4_dev_cap * dev_cap,struct mlx4_init_hca_param * init_hca)695a2cc190SJeff Kirsher u64 mlx4_make_profile(struct mlx4_dev *dev,
705a2cc190SJeff Kirsher struct mlx4_profile *request,
715a2cc190SJeff Kirsher struct mlx4_dev_cap *dev_cap,
725a2cc190SJeff Kirsher struct mlx4_init_hca_param *init_hca)
735a2cc190SJeff Kirsher {
745a2cc190SJeff Kirsher struct mlx4_priv *priv = mlx4_priv(dev);
755a2cc190SJeff Kirsher struct mlx4_resource {
765a2cc190SJeff Kirsher u64 size;
775a2cc190SJeff Kirsher u64 start;
785a2cc190SJeff Kirsher int type;
793de819e6SYishai Hadas u32 num;
805a2cc190SJeff Kirsher int log_num;
815a2cc190SJeff Kirsher };
825a2cc190SJeff Kirsher
835a2cc190SJeff Kirsher u64 total_size = 0;
845a2cc190SJeff Kirsher struct mlx4_resource *profile;
85db5a7a65SRoland Dreier struct sysinfo si;
865a2cc190SJeff Kirsher int i, j;
875a2cc190SJeff Kirsher
885a2cc190SJeff Kirsher profile = kcalloc(MLX4_RES_NUM, sizeof(*profile), GFP_KERNEL);
895a2cc190SJeff Kirsher if (!profile)
905a2cc190SJeff Kirsher return -ENOMEM;
915a2cc190SJeff Kirsher
92db5a7a65SRoland Dreier /*
93db5a7a65SRoland Dreier * We want to scale the number of MTTs with the size of the
94db5a7a65SRoland Dreier * system memory, since it makes sense to register a lot of
95db5a7a65SRoland Dreier * memory on a system with a lot of memory. As a heuristic,
96db5a7a65SRoland Dreier * make sure we have enough MTTs to cover twice the system
97db5a7a65SRoland Dreier * memory (with PAGE_SIZE entries).
98db5a7a65SRoland Dreier *
99db5a7a65SRoland Dreier * This number has to be a power of two and fit into 32 bits
100db5a7a65SRoland Dreier * due to device limitations, so cap this at 2^31 as well.
101db5a7a65SRoland Dreier * That limits us to 8TB of memory registration per HCA with
102db5a7a65SRoland Dreier * 4KB pages, which is probably OK for the next few months.
103db5a7a65SRoland Dreier */
104db5a7a65SRoland Dreier si_meminfo(&si);
105db5a7a65SRoland Dreier request->num_mtt =
106db5a7a65SRoland Dreier roundup_pow_of_two(max_t(unsigned, request->num_mtt,
1073de819e6SYishai Hadas min(1UL << (31 - log_mtts_per_seg),
1087cc77bf4STariq Toukan (si.totalram << 1) >> log_mtts_per_seg)));
1097cc77bf4STariq Toukan
110db5a7a65SRoland Dreier
1115a2cc190SJeff Kirsher profile[MLX4_RES_QP].size = dev_cap->qpc_entry_sz;
1125a2cc190SJeff Kirsher profile[MLX4_RES_RDMARC].size = dev_cap->rdmarc_entry_sz;
1135a2cc190SJeff Kirsher profile[MLX4_RES_ALTC].size = dev_cap->altc_entry_sz;
1145a2cc190SJeff Kirsher profile[MLX4_RES_AUXC].size = dev_cap->aux_entry_sz;
1155a2cc190SJeff Kirsher profile[MLX4_RES_SRQ].size = dev_cap->srq_entry_sz;
1165a2cc190SJeff Kirsher profile[MLX4_RES_CQ].size = dev_cap->cqc_entry_sz;
1175a2cc190SJeff Kirsher profile[MLX4_RES_EQ].size = dev_cap->eqc_entry_sz;
1185a2cc190SJeff Kirsher profile[MLX4_RES_DMPT].size = dev_cap->dmpt_entry_sz;
1195a2cc190SJeff Kirsher profile[MLX4_RES_CMPT].size = dev_cap->cmpt_entry_sz;
1202b8fb286SMarcel Apfelbaum profile[MLX4_RES_MTT].size = dev_cap->mtt_entry_sz;
1210ec2c0f8SEugenia Emantayev profile[MLX4_RES_MCG].size = mlx4_get_mgm_entry_size(dev);
1225a2cc190SJeff Kirsher
1235a2cc190SJeff Kirsher profile[MLX4_RES_QP].num = request->num_qp;
1245a2cc190SJeff Kirsher profile[MLX4_RES_RDMARC].num = request->num_qp * request->rdmarc_per_qp;
1255a2cc190SJeff Kirsher profile[MLX4_RES_ALTC].num = request->num_qp;
1265a2cc190SJeff Kirsher profile[MLX4_RES_AUXC].num = request->num_qp;
1275a2cc190SJeff Kirsher profile[MLX4_RES_SRQ].num = request->num_srq;
1285a2cc190SJeff Kirsher profile[MLX4_RES_CQ].num = request->num_cq;
1297ae0e400SMatan Barak profile[MLX4_RES_EQ].num = mlx4_is_mfunc(dev) ? dev->phys_caps.num_phys_eqs :
1303fc929e2SMarcel Apfelbaum min_t(unsigned, dev_cap->max_eqs, MAX_MSIX);
1315a2cc190SJeff Kirsher profile[MLX4_RES_DMPT].num = request->num_mpt;
1325a2cc190SJeff Kirsher profile[MLX4_RES_CMPT].num = MLX4_NUM_CMPTS;
1339fd7a1e1SMarcel Apfelbaum profile[MLX4_RES_MTT].num = request->num_mtt * (1 << log_mtts_per_seg);
1345a2cc190SJeff Kirsher profile[MLX4_RES_MCG].num = request->num_mcg;
1355a2cc190SJeff Kirsher
1365a2cc190SJeff Kirsher for (i = 0; i < MLX4_RES_NUM; ++i) {
1375a2cc190SJeff Kirsher profile[i].type = i;
1385a2cc190SJeff Kirsher profile[i].num = roundup_pow_of_two(profile[i].num);
1395a2cc190SJeff Kirsher profile[i].log_num = ilog2(profile[i].num);
1405a2cc190SJeff Kirsher profile[i].size *= profile[i].num;
1415a2cc190SJeff Kirsher profile[i].size = max(profile[i].size, (u64) PAGE_SIZE);
1425a2cc190SJeff Kirsher }
1435a2cc190SJeff Kirsher
1445a2cc190SJeff Kirsher /*
1455a2cc190SJeff Kirsher * Sort the resources in decreasing order of size. Since they
1465a2cc190SJeff Kirsher * all have sizes that are powers of 2, we'll be able to keep
1475a2cc190SJeff Kirsher * resources aligned to their size and pack them without gaps
1485a2cc190SJeff Kirsher * using the sorted order.
1495a2cc190SJeff Kirsher */
1505a2cc190SJeff Kirsher for (i = MLX4_RES_NUM; i > 0; --i)
1515a2cc190SJeff Kirsher for (j = 1; j < i; ++j) {
152e55898a7SFabian Frederick if (profile[j].size > profile[j - 1].size)
153e55898a7SFabian Frederick swap(profile[j], profile[j - 1]);
1545a2cc190SJeff Kirsher }
1555a2cc190SJeff Kirsher
1565a2cc190SJeff Kirsher for (i = 0; i < MLX4_RES_NUM; ++i) {
1575a2cc190SJeff Kirsher if (profile[i].size) {
1585a2cc190SJeff Kirsher profile[i].start = total_size;
1595a2cc190SJeff Kirsher total_size += profile[i].size;
1605a2cc190SJeff Kirsher }
1615a2cc190SJeff Kirsher
1625a2cc190SJeff Kirsher if (total_size > dev_cap->max_icm_sz) {
1631a91de28SJoe Perches mlx4_err(dev, "Profile requires 0x%llx bytes; won't fit in 0x%llx bytes of context memory\n",
1645a2cc190SJeff Kirsher (unsigned long long) total_size,
1655a2cc190SJeff Kirsher (unsigned long long) dev_cap->max_icm_sz);
1665a2cc190SJeff Kirsher kfree(profile);
1675a2cc190SJeff Kirsher return -ENOMEM;
1685a2cc190SJeff Kirsher }
1695a2cc190SJeff Kirsher
1705a2cc190SJeff Kirsher if (profile[i].size)
1711a91de28SJoe Perches mlx4_dbg(dev, " profile[%2d] (%6s): 2^%02d entries @ 0x%10llx, size 0x%10llx\n",
1721a91de28SJoe Perches i, res_name[profile[i].type],
1731a91de28SJoe Perches profile[i].log_num,
1745a2cc190SJeff Kirsher (unsigned long long) profile[i].start,
1755a2cc190SJeff Kirsher (unsigned long long) profile[i].size);
1765a2cc190SJeff Kirsher }
1775a2cc190SJeff Kirsher
1785a2cc190SJeff Kirsher mlx4_dbg(dev, "HCA context memory: reserving %d KB\n",
1795a2cc190SJeff Kirsher (int) (total_size >> 10));
1805a2cc190SJeff Kirsher
1815a2cc190SJeff Kirsher for (i = 0; i < MLX4_RES_NUM; ++i) {
1825a2cc190SJeff Kirsher switch (profile[i].type) {
1835a2cc190SJeff Kirsher case MLX4_RES_QP:
1845a2cc190SJeff Kirsher dev->caps.num_qps = profile[i].num;
1855a2cc190SJeff Kirsher init_hca->qpc_base = profile[i].start;
1865a2cc190SJeff Kirsher init_hca->log_num_qps = profile[i].log_num;
1875a2cc190SJeff Kirsher break;
1885a2cc190SJeff Kirsher case MLX4_RES_RDMARC:
1895a2cc190SJeff Kirsher for (priv->qp_table.rdmarc_shift = 0;
1905a2cc190SJeff Kirsher request->num_qp << priv->qp_table.rdmarc_shift < profile[i].num;
1915a2cc190SJeff Kirsher ++priv->qp_table.rdmarc_shift)
1925a2cc190SJeff Kirsher ; /* nothing */
1935a2cc190SJeff Kirsher dev->caps.max_qp_dest_rdma = 1 << priv->qp_table.rdmarc_shift;
1945a2cc190SJeff Kirsher priv->qp_table.rdmarc_base = (u32) profile[i].start;
1955a2cc190SJeff Kirsher init_hca->rdmarc_base = profile[i].start;
1965a2cc190SJeff Kirsher init_hca->log_rd_per_qp = priv->qp_table.rdmarc_shift;
1975a2cc190SJeff Kirsher break;
1985a2cc190SJeff Kirsher case MLX4_RES_ALTC:
1995a2cc190SJeff Kirsher init_hca->altc_base = profile[i].start;
2005a2cc190SJeff Kirsher break;
2015a2cc190SJeff Kirsher case MLX4_RES_AUXC:
2025a2cc190SJeff Kirsher init_hca->auxc_base = profile[i].start;
2035a2cc190SJeff Kirsher break;
2045a2cc190SJeff Kirsher case MLX4_RES_SRQ:
2055a2cc190SJeff Kirsher dev->caps.num_srqs = profile[i].num;
2065a2cc190SJeff Kirsher init_hca->srqc_base = profile[i].start;
2075a2cc190SJeff Kirsher init_hca->log_num_srqs = profile[i].log_num;
2085a2cc190SJeff Kirsher break;
2095a2cc190SJeff Kirsher case MLX4_RES_CQ:
2105a2cc190SJeff Kirsher dev->caps.num_cqs = profile[i].num;
2115a2cc190SJeff Kirsher init_hca->cqc_base = profile[i].start;
2125a2cc190SJeff Kirsher init_hca->log_num_cqs = profile[i].log_num;
2135a2cc190SJeff Kirsher break;
2145a2cc190SJeff Kirsher case MLX4_RES_EQ:
2157ae0e400SMatan Barak if (dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS) {
2167ae0e400SMatan Barak init_hca->log_num_eqs = 0x1f;
2177ae0e400SMatan Barak init_hca->eqc_base = profile[i].start;
2187ae0e400SMatan Barak init_hca->num_sys_eqs = dev_cap->num_sys_eqs;
2197ae0e400SMatan Barak } else {
2207ae0e400SMatan Barak dev->caps.num_eqs = roundup_pow_of_two(
2217ae0e400SMatan Barak min_t(unsigned,
2227ae0e400SMatan Barak dev_cap->max_eqs,
2233fc929e2SMarcel Apfelbaum MAX_MSIX));
2245a2cc190SJeff Kirsher init_hca->eqc_base = profile[i].start;
2253fc929e2SMarcel Apfelbaum init_hca->log_num_eqs = ilog2(dev->caps.num_eqs);
2267ae0e400SMatan Barak }
2275a2cc190SJeff Kirsher break;
2285a2cc190SJeff Kirsher case MLX4_RES_DMPT:
2295a2cc190SJeff Kirsher dev->caps.num_mpts = profile[i].num;
2305a2cc190SJeff Kirsher priv->mr_table.mpt_base = profile[i].start;
2315a2cc190SJeff Kirsher init_hca->dmpt_base = profile[i].start;
2325a2cc190SJeff Kirsher init_hca->log_mpt_sz = profile[i].log_num;
2335a2cc190SJeff Kirsher break;
2345a2cc190SJeff Kirsher case MLX4_RES_CMPT:
2355a2cc190SJeff Kirsher init_hca->cmpt_base = profile[i].start;
2365a2cc190SJeff Kirsher break;
2375a2cc190SJeff Kirsher case MLX4_RES_MTT:
2382b8fb286SMarcel Apfelbaum dev->caps.num_mtts = profile[i].num;
2395a2cc190SJeff Kirsher priv->mr_table.mtt_base = profile[i].start;
2405a2cc190SJeff Kirsher init_hca->mtt_base = profile[i].start;
2415a2cc190SJeff Kirsher break;
2425a2cc190SJeff Kirsher case MLX4_RES_MCG:
2435a2cc190SJeff Kirsher init_hca->mc_base = profile[i].start;
2440ec2c0f8SEugenia Emantayev init_hca->log_mc_entry_sz =
2450ec2c0f8SEugenia Emantayev ilog2(mlx4_get_mgm_entry_size(dev));
2465a2cc190SJeff Kirsher init_hca->log_mc_table_sz = profile[i].log_num;
2470ff1fb65SHadar Hen Zion if (dev->caps.steering_mode ==
2480ff1fb65SHadar Hen Zion MLX4_STEERING_MODE_DEVICE_MANAGED) {
2490ff1fb65SHadar Hen Zion dev->caps.num_mgms = profile[i].num;
2500ff1fb65SHadar Hen Zion } else {
2510ff1fb65SHadar Hen Zion init_hca->log_mc_hash_sz =
2520ff1fb65SHadar Hen Zion profile[i].log_num - 1;
2530ff1fb65SHadar Hen Zion dev->caps.num_mgms = profile[i].num >> 1;
2540ff1fb65SHadar Hen Zion dev->caps.num_amgms = profile[i].num >> 1;
2550ff1fb65SHadar Hen Zion }
2565a2cc190SJeff Kirsher break;
2575a2cc190SJeff Kirsher default:
2585a2cc190SJeff Kirsher break;
2595a2cc190SJeff Kirsher }
2605a2cc190SJeff Kirsher }
2615a2cc190SJeff Kirsher
2625a2cc190SJeff Kirsher /*
2635a2cc190SJeff Kirsher * PDs don't take any HCA memory, but we assign them as part
2645a2cc190SJeff Kirsher * of the HCA profile anyway.
2655a2cc190SJeff Kirsher */
2665a2cc190SJeff Kirsher dev->caps.num_pds = MLX4_NUM_PDS;
2675a2cc190SJeff Kirsher
2685a2cc190SJeff Kirsher kfree(profile);
2695a2cc190SJeff Kirsher return total_size;
2705a2cc190SJeff Kirsher }
271