xref: /freebsd/sys/dev/mlx4/mlx4_core/mlx4_profile.c (revision b0b1dbdd)
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005, 2014 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #include <linux/slab.h>
36 
37 #include "mlx4.h"
38 #include "fw.h"
39 
40 enum {
41 	MLX4_RES_QP,
42 	MLX4_RES_RDMARC,
43 	MLX4_RES_ALTC,
44 	MLX4_RES_AUXC,
45 	MLX4_RES_SRQ,
46 	MLX4_RES_CQ,
47 	MLX4_RES_EQ,
48 	MLX4_RES_DMPT,
49 	MLX4_RES_CMPT,
50 	MLX4_RES_MTT,
51 	MLX4_RES_MCG,
52 	MLX4_RES_NUM
53 };
54 
55 static const char *res_name[] = {
56 	[MLX4_RES_QP]		= "QP",
57 	[MLX4_RES_RDMARC]	= "RDMARC",
58 	[MLX4_RES_ALTC]		= "ALTC",
59 	[MLX4_RES_AUXC]		= "AUXC",
60 	[MLX4_RES_SRQ]		= "SRQ",
61 	[MLX4_RES_CQ]		= "CQ",
62 	[MLX4_RES_EQ]		= "EQ",
63 	[MLX4_RES_DMPT]		= "DMPT",
64 	[MLX4_RES_CMPT]		= "CMPT",
65 	[MLX4_RES_MTT]		= "MTT",
66 	[MLX4_RES_MCG]		= "MCG",
67 };
68 
69 u64 mlx4_make_profile(struct mlx4_dev *dev,
70 		      struct mlx4_profile *request,
71 		      struct mlx4_dev_cap *dev_cap,
72 		      struct mlx4_init_hca_param *init_hca)
73 {
74 	struct mlx4_priv *priv = mlx4_priv(dev);
75 	struct mlx4_resource {
76 		u64 size;
77 		u64 start;
78 		int type;
79 		u64 num;
80 		int log_num;
81 	};
82 
83 	u64 total_size = 0;
84 	struct mlx4_resource *profile;
85 	struct mlx4_resource tmp;
86 	int i, j;
87 
88 	profile = kcalloc(MLX4_RES_NUM, sizeof(*profile), GFP_KERNEL);
89 	if (!profile)
90 		return -ENOMEM;
91 
92 	profile[MLX4_RES_QP].size     = dev_cap->qpc_entry_sz;
93 	profile[MLX4_RES_RDMARC].size = dev_cap->rdmarc_entry_sz;
94 	profile[MLX4_RES_ALTC].size   = dev_cap->altc_entry_sz;
95 	profile[MLX4_RES_AUXC].size   = dev_cap->aux_entry_sz;
96 	profile[MLX4_RES_SRQ].size    = dev_cap->srq_entry_sz;
97 	profile[MLX4_RES_CQ].size     = dev_cap->cqc_entry_sz;
98 	profile[MLX4_RES_EQ].size     = dev_cap->eqc_entry_sz;
99 	profile[MLX4_RES_DMPT].size   = dev_cap->dmpt_entry_sz;
100 	profile[MLX4_RES_CMPT].size   = dev_cap->cmpt_entry_sz;
101 	profile[MLX4_RES_MTT].size    = dev_cap->mtt_entry_sz;
102 	profile[MLX4_RES_MCG].size    = mlx4_get_mgm_entry_size(dev);
103 
104 	profile[MLX4_RES_QP].num      = request->num_qp;
105 	profile[MLX4_RES_RDMARC].num  = request->num_qp * request->rdmarc_per_qp;
106 	profile[MLX4_RES_ALTC].num    = request->num_qp;
107 	profile[MLX4_RES_AUXC].num    = request->num_qp;
108 	profile[MLX4_RES_SRQ].num     = request->num_srq;
109 	profile[MLX4_RES_CQ].num      = request->num_cq;
110 	profile[MLX4_RES_EQ].num      = mlx4_is_mfunc(dev) ?
111 					dev->phys_caps.num_phys_eqs :
112 					min_t(unsigned, dev_cap->max_eqs, MAX_MSIX);
113 	profile[MLX4_RES_DMPT].num    = request->num_mpt;
114 	profile[MLX4_RES_CMPT].num    = MLX4_NUM_CMPTS;
115 	profile[MLX4_RES_MTT].num     = ((u64)request->num_mtt_segs) *
116 					(1 << log_mtts_per_seg);
117 	profile[MLX4_RES_MCG].num     = request->num_mcg;
118 
119 	for (i = 0; i < MLX4_RES_NUM; ++i) {
120 		profile[i].type     = i;
121 		profile[i].num      = roundup_pow_of_two(profile[i].num);
122 		profile[i].log_num  = ilog2(profile[i].num);
123 		profile[i].size    *= profile[i].num;
124 		profile[i].size     = max(profile[i].size, (u64) PAGE_SIZE);
125 	}
126 
127 	/*
128 	 * Sort the resources in decreasing order of size.  Since they
129 	 * all have sizes that are powers of 2, we'll be able to keep
130 	 * resources aligned to their size and pack them without gaps
131 	 * using the sorted order.
132 	 */
133 	for (i = MLX4_RES_NUM; i > 0; --i)
134 		for (j = 1; j < i; ++j) {
135 			if (profile[j].size > profile[j - 1].size) {
136 				tmp	       = profile[j];
137 				profile[j]     = profile[j - 1];
138 				profile[j - 1] = tmp;
139 			}
140 		}
141 
142 	for (i = 0; i < MLX4_RES_NUM; ++i) {
143 		if (profile[i].size) {
144 			profile[i].start = total_size;
145 			total_size	+= profile[i].size;
146 		}
147 
148 		if (total_size > dev_cap->max_icm_sz) {
149 			mlx4_err(dev, "Profile requires 0x%llx bytes; "
150 				  "won't fit in 0x%llx bytes of context memory.\n",
151 				  (unsigned long long) total_size,
152 				  (unsigned long long) dev_cap->max_icm_sz);
153 			kfree(profile);
154 			return -ENOMEM;
155 		}
156 
157 		if (profile[i].size)
158 			mlx4_dbg(dev, "  profile[%2d] (%6s): 2^%02d entries @ 0x%10llx, "
159 				  "size 0x%10llx\n",
160 				 i, res_name[profile[i].type], profile[i].log_num,
161 				 (unsigned long long) profile[i].start,
162 				 (unsigned long long) profile[i].size);
163 	}
164 
165 	mlx4_dbg(dev, "HCA context memory: reserving %d KB\n",
166 		 (int) (total_size >> 10));
167 
168 	for (i = 0; i < MLX4_RES_NUM; ++i) {
169 		switch (profile[i].type) {
170 		case MLX4_RES_QP:
171 			dev->caps.num_qps     = profile[i].num;
172 			init_hca->qpc_base    = profile[i].start;
173 			init_hca->log_num_qps = profile[i].log_num;
174 			break;
175 		case MLX4_RES_RDMARC:
176 			for (priv->qp_table.rdmarc_shift = 0;
177 			     request->num_qp << priv->qp_table.rdmarc_shift < profile[i].num;
178 			     ++priv->qp_table.rdmarc_shift)
179 				; /* nothing */
180 			dev->caps.max_qp_dest_rdma = 1 << priv->qp_table.rdmarc_shift;
181 			priv->qp_table.rdmarc_base   = (u32) profile[i].start;
182 			init_hca->rdmarc_base	     = profile[i].start;
183 			init_hca->log_rd_per_qp	     = priv->qp_table.rdmarc_shift;
184 			break;
185 		case MLX4_RES_ALTC:
186 			init_hca->altc_base = profile[i].start;
187 			break;
188 		case MLX4_RES_AUXC:
189 			init_hca->auxc_base = profile[i].start;
190 			break;
191 		case MLX4_RES_SRQ:
192 			dev->caps.num_srqs     = profile[i].num;
193 			init_hca->srqc_base    = profile[i].start;
194 			init_hca->log_num_srqs = profile[i].log_num;
195 			break;
196 		case MLX4_RES_CQ:
197 			dev->caps.num_cqs     = profile[i].num;
198 			init_hca->cqc_base    = profile[i].start;
199 			init_hca->log_num_cqs = profile[i].log_num;
200 			break;
201 		case MLX4_RES_EQ:
202 			if (dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_SYS_EQS) {
203 				init_hca->log_num_eqs	= 0x1f;
204 				init_hca->eqc_base	= profile[i].start;
205 				init_hca->num_sys_eqs	= dev_cap->num_sys_eqs;
206 			} else {
207 				 dev->caps.num_eqs	= roundup_pow_of_two(
208 					min_t(unsigned,
209 						dev_cap->max_eqs, MAX_MSIX));
210 				init_hca->eqc_base	= profile[i].start;
211 				init_hca->log_num_eqs	= ilog2(dev->caps.num_eqs);
212 			}
213 			break;
214 		case MLX4_RES_DMPT:
215 			dev->caps.num_mpts	= profile[i].num;
216 			priv->mr_table.mpt_base = profile[i].start;
217 			init_hca->dmpt_base	= profile[i].start;
218 			init_hca->log_mpt_sz	= profile[i].log_num;
219 			break;
220 		case MLX4_RES_CMPT:
221 			init_hca->cmpt_base	 = profile[i].start;
222 			break;
223 		case MLX4_RES_MTT:
224 			dev->caps.num_mtts	 = profile[i].num;
225 			priv->mr_table.mtt_base	 = profile[i].start;
226 			init_hca->mtt_base	 = profile[i].start;
227 			break;
228 		case MLX4_RES_MCG:
229 			init_hca->mc_base	  = profile[i].start;
230 			init_hca->log_mc_entry_sz =
231 					ilog2(mlx4_get_mgm_entry_size(dev));
232 			init_hca->log_mc_table_sz = profile[i].log_num;
233 			if (dev->caps.steering_mode ==
234 			    MLX4_STEERING_MODE_DEVICE_MANAGED) {
235 				dev->caps.num_mgms = profile[i].num;
236 			} else {
237 				init_hca->log_mc_hash_sz =
238 						profile[i].log_num - 1;
239 				dev->caps.num_mgms = profile[i].num >> 1;
240 				dev->caps.num_amgms = profile[i].num >> 1;
241 			}
242 			break;
243 		default:
244 			break;
245 		}
246 	}
247 
248 	/*
249 	 * PDs don't take any HCA memory, but we assign them as part
250 	 * of the HCA profile anyway.
251 	 */
252 	dev->caps.num_pds = MLX4_NUM_PDS;
253 
254 	kfree(profile);
255 	return total_size;
256 }
257