Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved. |
Roland Dreier | 2a1d9b7 | 2005-08-10 23:03:10 -0700 | [diff] [blame] | 3 | * Copyright (c) 2005 Mellanox Technologies. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/moduleparam.h> |
| 38 | |
| 39 | #include "mthca_profile.h" |
| 40 | |
| 41 | enum { |
| 42 | MTHCA_RES_QP, |
| 43 | MTHCA_RES_EEC, |
| 44 | MTHCA_RES_SRQ, |
| 45 | MTHCA_RES_CQ, |
| 46 | MTHCA_RES_EQP, |
| 47 | MTHCA_RES_EEEC, |
| 48 | MTHCA_RES_EQ, |
| 49 | MTHCA_RES_RDB, |
| 50 | MTHCA_RES_MCG, |
| 51 | MTHCA_RES_MPT, |
| 52 | MTHCA_RES_MTT, |
| 53 | MTHCA_RES_UAR, |
| 54 | MTHCA_RES_UDAV, |
| 55 | MTHCA_RES_UARC, |
| 56 | MTHCA_RES_NUM |
| 57 | }; |
| 58 | |
| 59 | enum { |
| 60 | MTHCA_NUM_EQS = 32, |
| 61 | MTHCA_NUM_PDS = 1 << 15 |
| 62 | }; |
| 63 | |
| 64 | u64 mthca_make_profile(struct mthca_dev *dev, |
| 65 | struct mthca_profile *request, |
| 66 | struct mthca_dev_lim *dev_lim, |
| 67 | struct mthca_init_hca_param *init_hca) |
| 68 | { |
| 69 | struct mthca_resource { |
| 70 | u64 size; |
| 71 | u64 start; |
| 72 | int type; |
| 73 | int num; |
| 74 | int log_num; |
| 75 | }; |
| 76 | |
| 77 | u64 mem_base, mem_avail; |
| 78 | u64 total_size = 0; |
| 79 | struct mthca_resource *profile; |
| 80 | struct mthca_resource tmp; |
| 81 | int i, j; |
| 82 | |
| 83 | profile = kmalloc(MTHCA_RES_NUM * sizeof *profile, GFP_KERNEL); |
| 84 | if (!profile) |
| 85 | return -ENOMEM; |
| 86 | |
| 87 | memset(profile, 0, MTHCA_RES_NUM * sizeof *profile); |
| 88 | |
| 89 | profile[MTHCA_RES_QP].size = dev_lim->qpc_entry_sz; |
| 90 | profile[MTHCA_RES_EEC].size = dev_lim->eec_entry_sz; |
| 91 | profile[MTHCA_RES_SRQ].size = dev_lim->srq_entry_sz; |
| 92 | profile[MTHCA_RES_CQ].size = dev_lim->cqc_entry_sz; |
| 93 | profile[MTHCA_RES_EQP].size = dev_lim->eqpc_entry_sz; |
| 94 | profile[MTHCA_RES_EEEC].size = dev_lim->eeec_entry_sz; |
| 95 | profile[MTHCA_RES_EQ].size = dev_lim->eqc_entry_sz; |
| 96 | profile[MTHCA_RES_RDB].size = MTHCA_RDB_ENTRY_SIZE; |
| 97 | profile[MTHCA_RES_MCG].size = MTHCA_MGM_ENTRY_SIZE; |
| 98 | profile[MTHCA_RES_MPT].size = dev_lim->mpt_entry_sz; |
Roland Dreier | 44ea668 | 2005-04-16 15:26:24 -0700 | [diff] [blame] | 99 | profile[MTHCA_RES_MTT].size = MTHCA_MTT_SEG_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | profile[MTHCA_RES_UAR].size = dev_lim->uar_scratch_entry_sz; |
| 101 | profile[MTHCA_RES_UDAV].size = MTHCA_AV_SIZE; |
| 102 | profile[MTHCA_RES_UARC].size = request->uarc_size; |
| 103 | |
| 104 | profile[MTHCA_RES_QP].num = request->num_qp; |
Roland Dreier | ec34a92 | 2005-08-19 10:59:31 -0700 | [diff] [blame^] | 105 | profile[MTHCA_RES_SRQ].num = request->num_srq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | profile[MTHCA_RES_EQP].num = request->num_qp; |
| 107 | profile[MTHCA_RES_RDB].num = request->num_qp * request->rdb_per_qp; |
| 108 | profile[MTHCA_RES_CQ].num = request->num_cq; |
| 109 | profile[MTHCA_RES_EQ].num = MTHCA_NUM_EQS; |
| 110 | profile[MTHCA_RES_MCG].num = request->num_mcg; |
| 111 | profile[MTHCA_RES_MPT].num = request->num_mpt; |
| 112 | profile[MTHCA_RES_MTT].num = request->num_mtt; |
| 113 | profile[MTHCA_RES_UAR].num = request->num_uar; |
| 114 | profile[MTHCA_RES_UARC].num = request->num_uar; |
| 115 | profile[MTHCA_RES_UDAV].num = request->num_udav; |
| 116 | |
| 117 | for (i = 0; i < MTHCA_RES_NUM; ++i) { |
| 118 | profile[i].type = i; |
| 119 | profile[i].log_num = max(ffs(profile[i].num) - 1, 0); |
| 120 | profile[i].size *= profile[i].num; |
Roland Dreier | d10ddbf | 2005-04-16 15:26:32 -0700 | [diff] [blame] | 121 | if (mthca_is_memfree(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | profile[i].size = max(profile[i].size, (u64) PAGE_SIZE); |
| 123 | } |
| 124 | |
Roland Dreier | d10ddbf | 2005-04-16 15:26:32 -0700 | [diff] [blame] | 125 | if (mthca_is_memfree(dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | mem_base = 0; |
| 127 | mem_avail = dev_lim->hca.arbel.max_icm_sz; |
| 128 | } else { |
| 129 | mem_base = dev->ddr_start; |
| 130 | mem_avail = dev->fw.tavor.fw_start - dev->ddr_start; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Sort the resources in decreasing order of size. Since they |
| 135 | * all have sizes that are powers of 2, we'll be able to keep |
| 136 | * resources aligned to their size and pack them without gaps |
| 137 | * using the sorted order. |
| 138 | */ |
| 139 | for (i = MTHCA_RES_NUM; i > 0; --i) |
| 140 | for (j = 1; j < i; ++j) { |
| 141 | if (profile[j].size > profile[j - 1].size) { |
| 142 | tmp = profile[j]; |
| 143 | profile[j] = profile[j - 1]; |
| 144 | profile[j - 1] = tmp; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | for (i = 0; i < MTHCA_RES_NUM; ++i) { |
| 149 | if (profile[i].size) { |
| 150 | profile[i].start = mem_base + total_size; |
| 151 | total_size += profile[i].size; |
| 152 | } |
| 153 | if (total_size > mem_avail) { |
| 154 | mthca_err(dev, "Profile requires 0x%llx bytes; " |
| 155 | "won't in 0x%llx bytes of context memory.\n", |
| 156 | (unsigned long long) total_size, |
| 157 | (unsigned long long) mem_avail); |
| 158 | kfree(profile); |
| 159 | return -ENOMEM; |
| 160 | } |
| 161 | |
| 162 | if (profile[i].size) |
| 163 | mthca_dbg(dev, "profile[%2d]--%2d/%2d @ 0x%16llx " |
| 164 | "(size 0x%8llx)\n", |
| 165 | i, profile[i].type, profile[i].log_num, |
| 166 | (unsigned long long) profile[i].start, |
| 167 | (unsigned long long) profile[i].size); |
| 168 | } |
| 169 | |
Roland Dreier | d10ddbf | 2005-04-16 15:26:32 -0700 | [diff] [blame] | 170 | if (mthca_is_memfree(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | mthca_dbg(dev, "HCA context memory: reserving %d KB\n", |
| 172 | (int) (total_size >> 10)); |
| 173 | else |
| 174 | mthca_dbg(dev, "HCA memory: allocated %d KB/%d KB (%d KB free)\n", |
| 175 | (int) (total_size >> 10), (int) (mem_avail >> 10), |
| 176 | (int) ((mem_avail - total_size) >> 10)); |
| 177 | |
| 178 | for (i = 0; i < MTHCA_RES_NUM; ++i) { |
| 179 | switch (profile[i].type) { |
| 180 | case MTHCA_RES_QP: |
| 181 | dev->limits.num_qps = profile[i].num; |
| 182 | init_hca->qpc_base = profile[i].start; |
| 183 | init_hca->log_num_qps = profile[i].log_num; |
| 184 | break; |
| 185 | case MTHCA_RES_EEC: |
| 186 | dev->limits.num_eecs = profile[i].num; |
| 187 | init_hca->eec_base = profile[i].start; |
| 188 | init_hca->log_num_eecs = profile[i].log_num; |
| 189 | break; |
| 190 | case MTHCA_RES_SRQ: |
| 191 | dev->limits.num_srqs = profile[i].num; |
| 192 | init_hca->srqc_base = profile[i].start; |
| 193 | init_hca->log_num_srqs = profile[i].log_num; |
| 194 | break; |
| 195 | case MTHCA_RES_CQ: |
| 196 | dev->limits.num_cqs = profile[i].num; |
| 197 | init_hca->cqc_base = profile[i].start; |
| 198 | init_hca->log_num_cqs = profile[i].log_num; |
| 199 | break; |
| 200 | case MTHCA_RES_EQP: |
| 201 | init_hca->eqpc_base = profile[i].start; |
| 202 | break; |
| 203 | case MTHCA_RES_EEEC: |
| 204 | init_hca->eeec_base = profile[i].start; |
| 205 | break; |
| 206 | case MTHCA_RES_EQ: |
| 207 | dev->limits.num_eqs = profile[i].num; |
| 208 | init_hca->eqc_base = profile[i].start; |
| 209 | init_hca->log_num_eqs = profile[i].log_num; |
| 210 | break; |
| 211 | case MTHCA_RES_RDB: |
| 212 | for (dev->qp_table.rdb_shift = 0; |
Roland Dreier | 17ead2f | 2005-04-16 15:26:15 -0700 | [diff] [blame] | 213 | request->num_qp << dev->qp_table.rdb_shift < profile[i].num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | ++dev->qp_table.rdb_shift) |
| 215 | ; /* nothing */ |
| 216 | dev->qp_table.rdb_base = (u32) profile[i].start; |
| 217 | init_hca->rdb_base = profile[i].start; |
| 218 | break; |
| 219 | case MTHCA_RES_MCG: |
| 220 | dev->limits.num_mgms = profile[i].num >> 1; |
| 221 | dev->limits.num_amgms = profile[i].num >> 1; |
| 222 | init_hca->mc_base = profile[i].start; |
| 223 | init_hca->log_mc_entry_sz = ffs(MTHCA_MGM_ENTRY_SIZE) - 1; |
| 224 | init_hca->log_mc_table_sz = profile[i].log_num; |
| 225 | init_hca->mc_hash_sz = 1 << (profile[i].log_num - 1); |
| 226 | break; |
| 227 | case MTHCA_RES_MPT: |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 228 | dev->limits.num_mpts = profile[i].num; |
| 229 | dev->mr_table.mpt_base = profile[i].start; |
| 230 | init_hca->mpt_base = profile[i].start; |
| 231 | init_hca->log_mpt_sz = profile[i].log_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | break; |
| 233 | case MTHCA_RES_MTT: |
| 234 | dev->limits.num_mtt_segs = profile[i].num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | dev->mr_table.mtt_base = profile[i].start; |
| 236 | init_hca->mtt_base = profile[i].start; |
Roland Dreier | 44ea668 | 2005-04-16 15:26:24 -0700 | [diff] [blame] | 237 | init_hca->mtt_seg_sz = ffs(MTHCA_MTT_SEG_SIZE) - 7; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | break; |
| 239 | case MTHCA_RES_UAR: |
| 240 | dev->limits.num_uars = profile[i].num; |
| 241 | init_hca->uar_scratch_base = profile[i].start; |
| 242 | break; |
| 243 | case MTHCA_RES_UDAV: |
| 244 | dev->av_table.ddr_av_base = profile[i].start; |
| 245 | dev->av_table.num_ddr_avs = profile[i].num; |
| 246 | break; |
| 247 | case MTHCA_RES_UARC: |
| 248 | dev->uar_table.uarc_size = request->uarc_size; |
| 249 | dev->uar_table.uarc_base = profile[i].start; |
| 250 | init_hca->uarc_base = profile[i].start; |
| 251 | init_hca->log_uarc_sz = ffs(request->uarc_size) - 13; |
| 252 | init_hca->log_uar_sz = ffs(request->num_uar) - 1; |
| 253 | break; |
| 254 | default: |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * PDs don't take any HCA memory, but we assign them as part |
| 261 | * of the HCA profile anyway. |
| 262 | */ |
| 263 | dev->limits.num_pds = MTHCA_NUM_PDS; |
| 264 | |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 265 | /* |
| 266 | * For Tavor, FMRs use ioremapped PCI memory. For 32 bit |
| 267 | * systems it may use too much vmalloc space to map all MTT |
| 268 | * memory, so we reserve some MTTs for FMR access, taking them |
| 269 | * out of the MR pool. They don't use additional memory, but |
| 270 | * we assign them as part of the HCA profile anyway. |
| 271 | */ |
Roland Dreier | d10ddbf | 2005-04-16 15:26:32 -0700 | [diff] [blame] | 272 | if (mthca_is_memfree(dev)) |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 273 | dev->limits.fmr_reserved_mtts = 0; |
| 274 | else |
| 275 | dev->limits.fmr_reserved_mtts = request->fmr_reserved_mtts; |
| 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | kfree(profile); |
| 278 | return total_size; |
| 279 | } |