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 
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;
855a2cc190SJeff Kirsher 	struct mlx4_resource tmp;
86db5a7a65SRoland Dreier 	struct sysinfo si;
875a2cc190SJeff Kirsher 	int i, j;
885a2cc190SJeff Kirsher 
895a2cc190SJeff Kirsher 	profile = kcalloc(MLX4_RES_NUM, sizeof(*profile), GFP_KERNEL);
905a2cc190SJeff Kirsher 	if (!profile)
915a2cc190SJeff Kirsher 		return -ENOMEM;
925a2cc190SJeff Kirsher 
93db5a7a65SRoland Dreier 	/*
94db5a7a65SRoland Dreier 	 * We want to scale the number of MTTs with the size of the
95db5a7a65SRoland Dreier 	 * system memory, since it makes sense to register a lot of
96db5a7a65SRoland Dreier 	 * memory on a system with a lot of memory.  As a heuristic,
97db5a7a65SRoland Dreier 	 * make sure we have enough MTTs to cover twice the system
98db5a7a65SRoland Dreier 	 * memory (with PAGE_SIZE entries).
99db5a7a65SRoland Dreier 	 *
100db5a7a65SRoland Dreier 	 * This number has to be a power of two and fit into 32 bits
101db5a7a65SRoland Dreier 	 * due to device limitations, so cap this at 2^31 as well.
102db5a7a65SRoland Dreier 	 * That limits us to 8TB of memory registration per HCA with
103db5a7a65SRoland Dreier 	 * 4KB pages, which is probably OK for the next few months.
104db5a7a65SRoland Dreier 	 */
105db5a7a65SRoland Dreier 	si_meminfo(&si);
106db5a7a65SRoland Dreier 	request->num_mtt =
107db5a7a65SRoland Dreier 		roundup_pow_of_two(max_t(unsigned, request->num_mtt,
1083de819e6SYishai Hadas 					 min(1UL << (31 - log_mtts_per_seg),
109db5a7a65SRoland Dreier 					     si.totalram >> (log_mtts_per_seg - 1))));
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;
1293fc929e2SMarcel Apfelbaum 	profile[MLX4_RES_EQ].num      = mlx4_is_mfunc(dev) ?
1303fc929e2SMarcel Apfelbaum 					dev->phys_caps.num_phys_eqs :
1313fc929e2SMarcel Apfelbaum 					min_t(unsigned, dev_cap->max_eqs, MAX_MSIX);
1325a2cc190SJeff Kirsher 	profile[MLX4_RES_DMPT].num    = request->num_mpt;
1335a2cc190SJeff Kirsher 	profile[MLX4_RES_CMPT].num    = MLX4_NUM_CMPTS;
1349fd7a1e1SMarcel Apfelbaum 	profile[MLX4_RES_MTT].num     = request->num_mtt * (1 << log_mtts_per_seg);
1355a2cc190SJeff Kirsher 	profile[MLX4_RES_MCG].num     = request->num_mcg;
1365a2cc190SJeff Kirsher 
1375a2cc190SJeff Kirsher 	for (i = 0; i < MLX4_RES_NUM; ++i) {
1385a2cc190SJeff Kirsher 		profile[i].type     = i;
1395a2cc190SJeff Kirsher 		profile[i].num      = roundup_pow_of_two(profile[i].num);
1405a2cc190SJeff Kirsher 		profile[i].log_num  = ilog2(profile[i].num);
1415a2cc190SJeff Kirsher 		profile[i].size    *= profile[i].num;
1425a2cc190SJeff Kirsher 		profile[i].size     = max(profile[i].size, (u64) PAGE_SIZE);
1435a2cc190SJeff Kirsher 	}
1445a2cc190SJeff Kirsher 
1455a2cc190SJeff Kirsher 	/*
1465a2cc190SJeff Kirsher 	 * Sort the resources in decreasing order of size.  Since they
1475a2cc190SJeff Kirsher 	 * all have sizes that are powers of 2, we'll be able to keep
1485a2cc190SJeff Kirsher 	 * resources aligned to their size and pack them without gaps
1495a2cc190SJeff Kirsher 	 * using the sorted order.
1505a2cc190SJeff Kirsher 	 */
1515a2cc190SJeff Kirsher 	for (i = MLX4_RES_NUM; i > 0; --i)
1525a2cc190SJeff Kirsher 		for (j = 1; j < i; ++j) {
1535a2cc190SJeff Kirsher 			if (profile[j].size > profile[j - 1].size) {
1545a2cc190SJeff Kirsher 				tmp	       = profile[j];
1555a2cc190SJeff Kirsher 				profile[j]     = profile[j - 1];
1565a2cc190SJeff Kirsher 				profile[j - 1] = tmp;
1575a2cc190SJeff Kirsher 			}
1585a2cc190SJeff Kirsher 		}
1595a2cc190SJeff Kirsher 
1605a2cc190SJeff Kirsher 	for (i = 0; i < MLX4_RES_NUM; ++i) {
1615a2cc190SJeff Kirsher 		if (profile[i].size) {
1625a2cc190SJeff Kirsher 			profile[i].start = total_size;
1635a2cc190SJeff Kirsher 			total_size	+= profile[i].size;
1645a2cc190SJeff Kirsher 		}
1655a2cc190SJeff Kirsher 
1665a2cc190SJeff Kirsher 		if (total_size > dev_cap->max_icm_sz) {
1671a91de28SJoe Perches 			mlx4_err(dev, "Profile requires 0x%llx bytes; won't fit in 0x%llx bytes of context memory\n",
1685a2cc190SJeff Kirsher 				 (unsigned long long) total_size,
1695a2cc190SJeff Kirsher 				 (unsigned long long) dev_cap->max_icm_sz);
1705a2cc190SJeff Kirsher 			kfree(profile);
1715a2cc190SJeff Kirsher 			return -ENOMEM;
1725a2cc190SJeff Kirsher 		}
1735a2cc190SJeff Kirsher 
1745a2cc190SJeff Kirsher 		if (profile[i].size)
1751a91de28SJoe Perches 			mlx4_dbg(dev, "  profile[%2d] (%6s): 2^%02d entries @ 0x%10llx, size 0x%10llx\n",
1761a91de28SJoe Perches 				 i, res_name[profile[i].type],
1771a91de28SJoe Perches 				 profile[i].log_num,
1785a2cc190SJeff Kirsher 				 (unsigned long long) profile[i].start,
1795a2cc190SJeff Kirsher 				 (unsigned long long) profile[i].size);
1805a2cc190SJeff Kirsher 	}
1815a2cc190SJeff Kirsher 
1825a2cc190SJeff Kirsher 	mlx4_dbg(dev, "HCA context memory: reserving %d KB\n",
1835a2cc190SJeff Kirsher 		 (int) (total_size >> 10));
1845a2cc190SJeff Kirsher 
1855a2cc190SJeff Kirsher 	for (i = 0; i < MLX4_RES_NUM; ++i) {
1865a2cc190SJeff Kirsher 		switch (profile[i].type) {
1875a2cc190SJeff Kirsher 		case MLX4_RES_QP:
1885a2cc190SJeff Kirsher 			dev->caps.num_qps     = profile[i].num;
1895a2cc190SJeff Kirsher 			init_hca->qpc_base    = profile[i].start;
1905a2cc190SJeff Kirsher 			init_hca->log_num_qps = profile[i].log_num;
1915a2cc190SJeff Kirsher 			break;
1925a2cc190SJeff Kirsher 		case MLX4_RES_RDMARC:
1935a2cc190SJeff Kirsher 			for (priv->qp_table.rdmarc_shift = 0;
1945a2cc190SJeff Kirsher 			     request->num_qp << priv->qp_table.rdmarc_shift < profile[i].num;
1955a2cc190SJeff Kirsher 			     ++priv->qp_table.rdmarc_shift)
1965a2cc190SJeff Kirsher 				; /* nothing */
1975a2cc190SJeff Kirsher 			dev->caps.max_qp_dest_rdma = 1 << priv->qp_table.rdmarc_shift;
1985a2cc190SJeff Kirsher 			priv->qp_table.rdmarc_base   = (u32) profile[i].start;
1995a2cc190SJeff Kirsher 			init_hca->rdmarc_base	     = profile[i].start;
2005a2cc190SJeff Kirsher 			init_hca->log_rd_per_qp	     = priv->qp_table.rdmarc_shift;
2015a2cc190SJeff Kirsher 			break;
2025a2cc190SJeff Kirsher 		case MLX4_RES_ALTC:
2035a2cc190SJeff Kirsher 			init_hca->altc_base = profile[i].start;
2045a2cc190SJeff Kirsher 			break;
2055a2cc190SJeff Kirsher 		case MLX4_RES_AUXC:
2065a2cc190SJeff Kirsher 			init_hca->auxc_base = profile[i].start;
2075a2cc190SJeff Kirsher 			break;
2085a2cc190SJeff Kirsher 		case MLX4_RES_SRQ:
2095a2cc190SJeff Kirsher 			dev->caps.num_srqs     = profile[i].num;
2105a2cc190SJeff Kirsher 			init_hca->srqc_base    = profile[i].start;
2115a2cc190SJeff Kirsher 			init_hca->log_num_srqs = profile[i].log_num;
2125a2cc190SJeff Kirsher 			break;
2135a2cc190SJeff Kirsher 		case MLX4_RES_CQ:
2145a2cc190SJeff Kirsher 			dev->caps.num_cqs     = profile[i].num;
2155a2cc190SJeff Kirsher 			init_hca->cqc_base    = profile[i].start;
2165a2cc190SJeff Kirsher 			init_hca->log_num_cqs = profile[i].log_num;
2175a2cc190SJeff Kirsher 			break;
2185a2cc190SJeff Kirsher 		case MLX4_RES_EQ:
2193fc929e2SMarcel Apfelbaum 			dev->caps.num_eqs     = roundup_pow_of_two(min_t(unsigned, dev_cap->max_eqs,
2203fc929e2SMarcel Apfelbaum 									 MAX_MSIX));
2215a2cc190SJeff Kirsher 			init_hca->eqc_base    = profile[i].start;
2223fc929e2SMarcel Apfelbaum 			init_hca->log_num_eqs = ilog2(dev->caps.num_eqs);
2235a2cc190SJeff Kirsher 			break;
2245a2cc190SJeff Kirsher 		case MLX4_RES_DMPT:
2255a2cc190SJeff Kirsher 			dev->caps.num_mpts	= profile[i].num;
2265a2cc190SJeff Kirsher 			priv->mr_table.mpt_base = profile[i].start;
2275a2cc190SJeff Kirsher 			init_hca->dmpt_base	= profile[i].start;
2285a2cc190SJeff Kirsher 			init_hca->log_mpt_sz	= profile[i].log_num;
2295a2cc190SJeff Kirsher 			break;
2305a2cc190SJeff Kirsher 		case MLX4_RES_CMPT:
2315a2cc190SJeff Kirsher 			init_hca->cmpt_base	 = profile[i].start;
2325a2cc190SJeff Kirsher 			break;
2335a2cc190SJeff Kirsher 		case MLX4_RES_MTT:
2342b8fb286SMarcel Apfelbaum 			dev->caps.num_mtts	 = profile[i].num;
2355a2cc190SJeff Kirsher 			priv->mr_table.mtt_base	 = profile[i].start;
2365a2cc190SJeff Kirsher 			init_hca->mtt_base	 = profile[i].start;
2375a2cc190SJeff Kirsher 			break;
2385a2cc190SJeff Kirsher 		case MLX4_RES_MCG:
2395a2cc190SJeff Kirsher 			init_hca->mc_base	  = profile[i].start;
2400ec2c0f8SEugenia Emantayev 			init_hca->log_mc_entry_sz =
2410ec2c0f8SEugenia Emantayev 					ilog2(mlx4_get_mgm_entry_size(dev));
2425a2cc190SJeff Kirsher 			init_hca->log_mc_table_sz = profile[i].log_num;
2430ff1fb65SHadar Hen Zion 			if (dev->caps.steering_mode ==
2440ff1fb65SHadar Hen Zion 			    MLX4_STEERING_MODE_DEVICE_MANAGED) {
2450ff1fb65SHadar Hen Zion 				dev->caps.num_mgms = profile[i].num;
2460ff1fb65SHadar Hen Zion 			} else {
2470ff1fb65SHadar Hen Zion 				init_hca->log_mc_hash_sz =
2480ff1fb65SHadar Hen Zion 						profile[i].log_num - 1;
2490ff1fb65SHadar Hen Zion 				dev->caps.num_mgms = profile[i].num >> 1;
2500ff1fb65SHadar Hen Zion 				dev->caps.num_amgms = profile[i].num >> 1;
2510ff1fb65SHadar Hen Zion 			}
2525a2cc190SJeff Kirsher 			break;
2535a2cc190SJeff Kirsher 		default:
2545a2cc190SJeff Kirsher 			break;
2555a2cc190SJeff Kirsher 		}
2565a2cc190SJeff Kirsher 	}
2575a2cc190SJeff Kirsher 
2585a2cc190SJeff Kirsher 	/*
2595a2cc190SJeff Kirsher 	 * PDs don't take any HCA memory, but we assign them as part
2605a2cc190SJeff Kirsher 	 * of the HCA profile anyway.
2615a2cc190SJeff Kirsher 	 */
2625a2cc190SJeff Kirsher 	dev->caps.num_pds = MLX4_NUM_PDS;
2635a2cc190SJeff Kirsher 
2645a2cc190SJeff Kirsher 	kfree(profile);
2655a2cc190SJeff Kirsher 	return total_size;
2665a2cc190SJeff Kirsher }
267