mthca_profile.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. * $Id: mthca_profile.c 1349 2004-12-16 21:09:43Z roland $
  34. */
  35. #include <linux/module.h>
  36. #include <linux/moduleparam.h>
  37. #include <linux/string.h>
  38. #include <linux/slab.h>
  39. #include "mthca_profile.h"
  40. enum {
  41. MTHCA_RES_QP,
  42. MTHCA_RES_EEC,
  43. MTHCA_RES_SRQ,
  44. MTHCA_RES_CQ,
  45. MTHCA_RES_EQP,
  46. MTHCA_RES_EEEC,
  47. MTHCA_RES_EQ,
  48. MTHCA_RES_RDB,
  49. MTHCA_RES_MCG,
  50. MTHCA_RES_MPT,
  51. MTHCA_RES_MTT,
  52. MTHCA_RES_UAR,
  53. MTHCA_RES_UDAV,
  54. MTHCA_RES_UARC,
  55. MTHCA_RES_NUM
  56. };
  57. enum {
  58. MTHCA_NUM_EQS = 32,
  59. MTHCA_NUM_PDS = 1 << 15
  60. };
  61. u64 mthca_make_profile(struct mthca_dev *dev,
  62. struct mthca_profile *request,
  63. struct mthca_dev_lim *dev_lim,
  64. struct mthca_init_hca_param *init_hca)
  65. {
  66. struct mthca_resource {
  67. u64 size;
  68. u64 start;
  69. int type;
  70. int num;
  71. int log_num;
  72. };
  73. u64 mem_base, mem_avail;
  74. u64 total_size = 0;
  75. struct mthca_resource *profile;
  76. struct mthca_resource tmp;
  77. int i, j;
  78. profile = kzalloc(MTHCA_RES_NUM * sizeof *profile, GFP_KERNEL);
  79. if (!profile)
  80. return -ENOMEM;
  81. profile[MTHCA_RES_QP].size = dev_lim->qpc_entry_sz;
  82. profile[MTHCA_RES_EEC].size = dev_lim->eec_entry_sz;
  83. profile[MTHCA_RES_SRQ].size = dev_lim->srq_entry_sz;
  84. profile[MTHCA_RES_CQ].size = dev_lim->cqc_entry_sz;
  85. profile[MTHCA_RES_EQP].size = dev_lim->eqpc_entry_sz;
  86. profile[MTHCA_RES_EEEC].size = dev_lim->eeec_entry_sz;
  87. profile[MTHCA_RES_EQ].size = dev_lim->eqc_entry_sz;
  88. profile[MTHCA_RES_RDB].size = MTHCA_RDB_ENTRY_SIZE;
  89. profile[MTHCA_RES_MCG].size = MTHCA_MGM_ENTRY_SIZE;
  90. profile[MTHCA_RES_MPT].size = dev_lim->mpt_entry_sz;
  91. profile[MTHCA_RES_MTT].size = MTHCA_MTT_SEG_SIZE;
  92. profile[MTHCA_RES_UAR].size = dev_lim->uar_scratch_entry_sz;
  93. profile[MTHCA_RES_UDAV].size = MTHCA_AV_SIZE;
  94. profile[MTHCA_RES_UARC].size = request->uarc_size;
  95. profile[MTHCA_RES_QP].num = request->num_qp;
  96. profile[MTHCA_RES_SRQ].num = request->num_srq;
  97. profile[MTHCA_RES_EQP].num = request->num_qp;
  98. profile[MTHCA_RES_RDB].num = request->num_qp * request->rdb_per_qp;
  99. profile[MTHCA_RES_CQ].num = request->num_cq;
  100. profile[MTHCA_RES_EQ].num = MTHCA_NUM_EQS;
  101. profile[MTHCA_RES_MCG].num = request->num_mcg;
  102. profile[MTHCA_RES_MPT].num = request->num_mpt;
  103. profile[MTHCA_RES_MTT].num = request->num_mtt;
  104. profile[MTHCA_RES_UAR].num = request->num_uar;
  105. profile[MTHCA_RES_UARC].num = request->num_uar;
  106. profile[MTHCA_RES_UDAV].num = request->num_udav;
  107. for (i = 0; i < MTHCA_RES_NUM; ++i) {
  108. profile[i].type = i;
  109. profile[i].log_num = max(ffs(profile[i].num) - 1, 0);
  110. profile[i].size *= profile[i].num;
  111. if (mthca_is_memfree(dev))
  112. profile[i].size = max(profile[i].size, (u64) PAGE_SIZE);
  113. }
  114. if (mthca_is_memfree(dev)) {
  115. mem_base = 0;
  116. mem_avail = dev_lim->hca.arbel.max_icm_sz;
  117. } else {
  118. mem_base = dev->ddr_start;
  119. mem_avail = dev->fw.tavor.fw_start - dev->ddr_start;
  120. }
  121. /*
  122. * Sort the resources in decreasing order of size. Since they
  123. * all have sizes that are powers of 2, we'll be able to keep
  124. * resources aligned to their size and pack them without gaps
  125. * using the sorted order.
  126. */
  127. for (i = MTHCA_RES_NUM; i > 0; --i)
  128. for (j = 1; j < i; ++j) {
  129. if (profile[j].size > profile[j - 1].size) {
  130. tmp = profile[j];
  131. profile[j] = profile[j - 1];
  132. profile[j - 1] = tmp;
  133. }
  134. }
  135. for (i = 0; i < MTHCA_RES_NUM; ++i) {
  136. if (profile[i].size) {
  137. profile[i].start = mem_base + total_size;
  138. total_size += profile[i].size;
  139. }
  140. if (total_size > mem_avail) {
  141. mthca_err(dev, "Profile requires 0x%llx bytes; "
  142. "won't fit in 0x%llx bytes of context memory.\n",
  143. (unsigned long long) total_size,
  144. (unsigned long long) mem_avail);
  145. kfree(profile);
  146. return -ENOMEM;
  147. }
  148. if (profile[i].size)
  149. mthca_dbg(dev, "profile[%2d]--%2d/%2d @ 0x%16llx "
  150. "(size 0x%8llx)\n",
  151. i, profile[i].type, profile[i].log_num,
  152. (unsigned long long) profile[i].start,
  153. (unsigned long long) profile[i].size);
  154. }
  155. if (mthca_is_memfree(dev))
  156. mthca_dbg(dev, "HCA context memory: reserving %d KB\n",
  157. (int) (total_size >> 10));
  158. else
  159. mthca_dbg(dev, "HCA memory: allocated %d KB/%d KB (%d KB free)\n",
  160. (int) (total_size >> 10), (int) (mem_avail >> 10),
  161. (int) ((mem_avail - total_size) >> 10));
  162. for (i = 0; i < MTHCA_RES_NUM; ++i) {
  163. switch (profile[i].type) {
  164. case MTHCA_RES_QP:
  165. dev->limits.num_qps = profile[i].num;
  166. init_hca->qpc_base = profile[i].start;
  167. init_hca->log_num_qps = profile[i].log_num;
  168. break;
  169. case MTHCA_RES_EEC:
  170. dev->limits.num_eecs = profile[i].num;
  171. init_hca->eec_base = profile[i].start;
  172. init_hca->log_num_eecs = profile[i].log_num;
  173. break;
  174. case MTHCA_RES_SRQ:
  175. dev->limits.num_srqs = profile[i].num;
  176. init_hca->srqc_base = profile[i].start;
  177. init_hca->log_num_srqs = profile[i].log_num;
  178. break;
  179. case MTHCA_RES_CQ:
  180. dev->limits.num_cqs = profile[i].num;
  181. init_hca->cqc_base = profile[i].start;
  182. init_hca->log_num_cqs = profile[i].log_num;
  183. break;
  184. case MTHCA_RES_EQP:
  185. init_hca->eqpc_base = profile[i].start;
  186. break;
  187. case MTHCA_RES_EEEC:
  188. init_hca->eeec_base = profile[i].start;
  189. break;
  190. case MTHCA_RES_EQ:
  191. dev->limits.num_eqs = profile[i].num;
  192. init_hca->eqc_base = profile[i].start;
  193. init_hca->log_num_eqs = profile[i].log_num;
  194. break;
  195. case MTHCA_RES_RDB:
  196. for (dev->qp_table.rdb_shift = 0;
  197. request->num_qp << dev->qp_table.rdb_shift < profile[i].num;
  198. ++dev->qp_table.rdb_shift)
  199. ; /* nothing */
  200. dev->qp_table.rdb_base = (u32) profile[i].start;
  201. init_hca->rdb_base = profile[i].start;
  202. break;
  203. case MTHCA_RES_MCG:
  204. dev->limits.num_mgms = profile[i].num >> 1;
  205. dev->limits.num_amgms = profile[i].num >> 1;
  206. init_hca->mc_base = profile[i].start;
  207. init_hca->log_mc_entry_sz = ffs(MTHCA_MGM_ENTRY_SIZE) - 1;
  208. init_hca->log_mc_table_sz = profile[i].log_num;
  209. init_hca->mc_hash_sz = 1 << (profile[i].log_num - 1);
  210. break;
  211. case MTHCA_RES_MPT:
  212. dev->limits.num_mpts = profile[i].num;
  213. dev->mr_table.mpt_base = profile[i].start;
  214. init_hca->mpt_base = profile[i].start;
  215. init_hca->log_mpt_sz = profile[i].log_num;
  216. break;
  217. case MTHCA_RES_MTT:
  218. dev->limits.num_mtt_segs = profile[i].num;
  219. dev->mr_table.mtt_base = profile[i].start;
  220. init_hca->mtt_base = profile[i].start;
  221. init_hca->mtt_seg_sz = ffs(MTHCA_MTT_SEG_SIZE) - 7;
  222. break;
  223. case MTHCA_RES_UAR:
  224. dev->limits.num_uars = profile[i].num;
  225. init_hca->uar_scratch_base = profile[i].start;
  226. break;
  227. case MTHCA_RES_UDAV:
  228. dev->av_table.ddr_av_base = profile[i].start;
  229. dev->av_table.num_ddr_avs = profile[i].num;
  230. break;
  231. case MTHCA_RES_UARC:
  232. dev->uar_table.uarc_size = request->uarc_size;
  233. dev->uar_table.uarc_base = profile[i].start;
  234. init_hca->uarc_base = profile[i].start;
  235. init_hca->log_uarc_sz = ffs(request->uarc_size) - 13;
  236. init_hca->log_uar_sz = ffs(request->num_uar) - 1;
  237. break;
  238. default:
  239. break;
  240. }
  241. }
  242. /*
  243. * PDs don't take any HCA memory, but we assign them as part
  244. * of the HCA profile anyway.
  245. */
  246. dev->limits.num_pds = MTHCA_NUM_PDS;
  247. if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT &&
  248. init_hca->log_mpt_sz > 23) {
  249. mthca_warn(dev, "MPT table too large (requested size 2^%d >= 2^24)\n",
  250. init_hca->log_mpt_sz);
  251. mthca_warn(dev, "Disabling memory key throughput optimization.\n");
  252. dev->mthca_flags &= ~MTHCA_FLAG_SINAI_OPT;
  253. }
  254. /*
  255. * For Tavor, FMRs use ioremapped PCI memory. For 32 bit
  256. * systems it may use too much vmalloc space to map all MTT
  257. * memory, so we reserve some MTTs for FMR access, taking them
  258. * out of the MR pool. They don't use additional memory, but
  259. * we assign them as part of the HCA profile anyway.
  260. */
  261. if (mthca_is_memfree(dev))
  262. dev->limits.fmr_reserved_mtts = 0;
  263. else
  264. dev->limits.fmr_reserved_mtts = request->fmr_reserved_mtts;
  265. kfree(profile);
  266. return total_size;
  267. }