Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2004 Topspin Communications. All rights reserved. |
Jack Morgenstein | 51a379d | 2008-07-25 10:32:52 -0700 | [diff] [blame] | 3 | * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved. |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 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 | |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 35 | #include <linux/errno.h> |
Paul Gortmaker | ee40fa0 | 2011-05-27 16:14:23 -0400 | [diff] [blame] | 36 | #include <linux/export.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 38 | #include <linux/kernel.h> |
Yishai Hadas | 89dd86d | 2012-08-13 08:15:06 +0000 | [diff] [blame] | 39 | #include <linux/vmalloc.h> |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 40 | |
| 41 | #include <linux/mlx4/cmd.h> |
| 42 | |
| 43 | #include "mlx4.h" |
| 44 | #include "icm.h" |
| 45 | |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 46 | static u32 mlx4_buddy_alloc(struct mlx4_buddy *buddy, int order) |
| 47 | { |
| 48 | int o; |
| 49 | int m; |
| 50 | u32 seg; |
| 51 | |
| 52 | spin_lock(&buddy->lock); |
| 53 | |
Roland Dreier | e4044cf | 2008-07-22 14:19:40 -0700 | [diff] [blame] | 54 | for (o = order; o <= buddy->max_order; ++o) |
| 55 | if (buddy->num_free[o]) { |
| 56 | m = 1 << (buddy->max_order - o); |
| 57 | seg = find_first_bit(buddy->bits[o], m); |
| 58 | if (seg < m) |
| 59 | goto found; |
| 60 | } |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 61 | |
| 62 | spin_unlock(&buddy->lock); |
| 63 | return -1; |
| 64 | |
| 65 | found: |
| 66 | clear_bit(seg, buddy->bits[o]); |
Roland Dreier | e4044cf | 2008-07-22 14:19:40 -0700 | [diff] [blame] | 67 | --buddy->num_free[o]; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 68 | |
| 69 | while (o > order) { |
| 70 | --o; |
| 71 | seg <<= 1; |
| 72 | set_bit(seg ^ 1, buddy->bits[o]); |
Roland Dreier | e4044cf | 2008-07-22 14:19:40 -0700 | [diff] [blame] | 73 | ++buddy->num_free[o]; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | spin_unlock(&buddy->lock); |
| 77 | |
| 78 | seg <<= order; |
| 79 | |
| 80 | return seg; |
| 81 | } |
| 82 | |
| 83 | static void mlx4_buddy_free(struct mlx4_buddy *buddy, u32 seg, int order) |
| 84 | { |
| 85 | seg >>= order; |
| 86 | |
| 87 | spin_lock(&buddy->lock); |
| 88 | |
| 89 | while (test_bit(seg ^ 1, buddy->bits[order])) { |
| 90 | clear_bit(seg ^ 1, buddy->bits[order]); |
Roland Dreier | e4044cf | 2008-07-22 14:19:40 -0700 | [diff] [blame] | 91 | --buddy->num_free[order]; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 92 | seg >>= 1; |
| 93 | ++order; |
| 94 | } |
| 95 | |
| 96 | set_bit(seg, buddy->bits[order]); |
Roland Dreier | e4044cf | 2008-07-22 14:19:40 -0700 | [diff] [blame] | 97 | ++buddy->num_free[order]; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 98 | |
| 99 | spin_unlock(&buddy->lock); |
| 100 | } |
| 101 | |
Roland Dreier | e8f9b2e | 2008-02-04 20:20:41 -0800 | [diff] [blame] | 102 | static int mlx4_buddy_init(struct mlx4_buddy *buddy, int max_order) |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 103 | { |
| 104 | int i, s; |
| 105 | |
| 106 | buddy->max_order = max_order; |
| 107 | spin_lock_init(&buddy->lock); |
| 108 | |
Roland Dreier | 96f17d5 | 2012-08-14 15:17:10 -0700 | [diff] [blame] | 109 | buddy->bits = kcalloc(buddy->max_order + 1, sizeof (long *), |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 110 | GFP_KERNEL); |
Eli Cohen | a831275 | 2011-10-06 09:33:12 -0700 | [diff] [blame] | 111 | buddy->num_free = kcalloc((buddy->max_order + 1), sizeof *buddy->num_free, |
Roland Dreier | e4044cf | 2008-07-22 14:19:40 -0700 | [diff] [blame] | 112 | GFP_KERNEL); |
| 113 | if (!buddy->bits || !buddy->num_free) |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 114 | goto err_out; |
| 115 | |
| 116 | for (i = 0; i <= buddy->max_order; ++i) { |
| 117 | s = BITS_TO_LONGS(1 << (buddy->max_order - i)); |
Roland Dreier | 96f17d5 | 2012-08-14 15:17:10 -0700 | [diff] [blame] | 118 | buddy->bits[i] = kcalloc(s, sizeof (long), GFP_KERNEL | __GFP_NOWARN); |
Yishai Hadas | 89dd86d | 2012-08-13 08:15:06 +0000 | [diff] [blame] | 119 | if (!buddy->bits[i]) { |
Roland Dreier | 96f17d5 | 2012-08-14 15:17:10 -0700 | [diff] [blame] | 120 | buddy->bits[i] = vzalloc(s * sizeof(long)); |
Yishai Hadas | 89dd86d | 2012-08-13 08:15:06 +0000 | [diff] [blame] | 121 | if (!buddy->bits[i]) |
| 122 | goto err_out_free; |
| 123 | } |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | set_bit(0, buddy->bits[buddy->max_order]); |
Roland Dreier | e4044cf | 2008-07-22 14:19:40 -0700 | [diff] [blame] | 127 | buddy->num_free[buddy->max_order] = 1; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 128 | |
| 129 | return 0; |
| 130 | |
| 131 | err_out_free: |
| 132 | for (i = 0; i <= buddy->max_order; ++i) |
Yishai Hadas | 89dd86d | 2012-08-13 08:15:06 +0000 | [diff] [blame] | 133 | if (buddy->bits[i] && is_vmalloc_addr(buddy->bits[i])) |
| 134 | vfree(buddy->bits[i]); |
| 135 | else |
| 136 | kfree(buddy->bits[i]); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 137 | |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 138 | err_out: |
Roland Dreier | e4044cf | 2008-07-22 14:19:40 -0700 | [diff] [blame] | 139 | kfree(buddy->bits); |
| 140 | kfree(buddy->num_free); |
| 141 | |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 142 | return -ENOMEM; |
| 143 | } |
| 144 | |
| 145 | static void mlx4_buddy_cleanup(struct mlx4_buddy *buddy) |
| 146 | { |
| 147 | int i; |
| 148 | |
| 149 | for (i = 0; i <= buddy->max_order; ++i) |
Yishai Hadas | 89dd86d | 2012-08-13 08:15:06 +0000 | [diff] [blame] | 150 | if (is_vmalloc_addr(buddy->bits[i])) |
| 151 | vfree(buddy->bits[i]); |
| 152 | else |
| 153 | kfree(buddy->bits[i]); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 154 | |
| 155 | kfree(buddy->bits); |
Roland Dreier | e4044cf | 2008-07-22 14:19:40 -0700 | [diff] [blame] | 156 | kfree(buddy->num_free); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Eli Cohen | c82e9aa | 2011-12-13 04:15:24 +0000 | [diff] [blame] | 159 | u32 __mlx4_alloc_mtt_range(struct mlx4_dev *dev, int order) |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 160 | { |
| 161 | struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table; |
| 162 | u32 seg; |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 163 | int seg_order; |
| 164 | u32 offset; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 165 | |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 166 | seg_order = max_t(int, order - log_mtts_per_seg, 0); |
| 167 | |
| 168 | seg = mlx4_buddy_alloc(&mr_table->mtt_buddy, seg_order); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 169 | if (seg == -1) |
| 170 | return -1; |
| 171 | |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 172 | offset = seg * (1 << log_mtts_per_seg); |
| 173 | |
| 174 | if (mlx4_table_get_range(dev, &mr_table->mtt_table, offset, |
| 175 | offset + (1 << order) - 1)) { |
| 176 | mlx4_buddy_free(&mr_table->mtt_buddy, seg, seg_order); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 177 | return -1; |
| 178 | } |
| 179 | |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 180 | return offset; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 183 | static u32 mlx4_alloc_mtt_range(struct mlx4_dev *dev, int order) |
| 184 | { |
Jack Morgenstein | e7dbeba | 2013-03-07 03:46:54 +0000 | [diff] [blame] | 185 | u64 in_param = 0; |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 186 | u64 out_param; |
| 187 | int err; |
| 188 | |
| 189 | if (mlx4_is_mfunc(dev)) { |
| 190 | set_param_l(&in_param, order); |
| 191 | err = mlx4_cmd_imm(dev, in_param, &out_param, RES_MTT, |
| 192 | RES_OP_RESERVE_AND_MAP, |
| 193 | MLX4_CMD_ALLOC_RES, |
| 194 | MLX4_CMD_TIME_CLASS_A, |
| 195 | MLX4_CMD_WRAPPED); |
| 196 | if (err) |
| 197 | return -1; |
| 198 | return get_param_l(&out_param); |
| 199 | } |
| 200 | return __mlx4_alloc_mtt_range(dev, order); |
| 201 | } |
| 202 | |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 203 | int mlx4_mtt_init(struct mlx4_dev *dev, int npages, int page_shift, |
| 204 | struct mlx4_mtt *mtt) |
| 205 | { |
| 206 | int i; |
| 207 | |
| 208 | if (!npages) { |
| 209 | mtt->order = -1; |
| 210 | mtt->page_shift = MLX4_ICM_PAGE_SHIFT; |
| 211 | return 0; |
| 212 | } else |
| 213 | mtt->page_shift = page_shift; |
| 214 | |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 215 | for (mtt->order = 0, i = 1; i < npages; i <<= 1) |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 216 | ++mtt->order; |
| 217 | |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 218 | mtt->offset = mlx4_alloc_mtt_range(dev, mtt->order); |
| 219 | if (mtt->offset == -1) |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 220 | return -ENOMEM; |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | EXPORT_SYMBOL_GPL(mlx4_mtt_init); |
| 225 | |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 226 | void __mlx4_free_mtt_range(struct mlx4_dev *dev, u32 offset, int order) |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 227 | { |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 228 | u32 first_seg; |
| 229 | int seg_order; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 230 | struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table; |
| 231 | |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 232 | seg_order = max_t(int, order - log_mtts_per_seg, 0); |
| 233 | first_seg = offset / (1 << log_mtts_per_seg); |
| 234 | |
| 235 | mlx4_buddy_free(&mr_table->mtt_buddy, first_seg, seg_order); |
Marcel Apfelbaum | 1e27ca6 | 2012-01-02 04:07:39 +0000 | [diff] [blame] | 236 | mlx4_table_put_range(dev, &mr_table->mtt_table, offset, |
| 237 | offset + (1 << order) - 1); |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 240 | static void mlx4_free_mtt_range(struct mlx4_dev *dev, u32 offset, int order) |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 241 | { |
Jack Morgenstein | e7dbeba | 2013-03-07 03:46:54 +0000 | [diff] [blame] | 242 | u64 in_param = 0; |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 243 | int err; |
| 244 | |
| 245 | if (mlx4_is_mfunc(dev)) { |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 246 | set_param_l(&in_param, offset); |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 247 | set_param_h(&in_param, order); |
| 248 | err = mlx4_cmd(dev, in_param, RES_MTT, RES_OP_RESERVE_AND_MAP, |
| 249 | MLX4_CMD_FREE_RES, |
| 250 | MLX4_CMD_TIME_CLASS_A, |
| 251 | MLX4_CMD_WRAPPED); |
| 252 | if (err) |
Joe Perches | 1a91de2 | 2014-05-07 12:52:57 -0700 | [diff] [blame] | 253 | mlx4_warn(dev, "Failed to free mtt range at:%d order:%d\n", |
| 254 | offset, order); |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 255 | return; |
| 256 | } |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 257 | __mlx4_free_mtt_range(dev, offset, order); |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void mlx4_mtt_cleanup(struct mlx4_dev *dev, struct mlx4_mtt *mtt) |
| 261 | { |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 262 | if (mtt->order < 0) |
| 263 | return; |
| 264 | |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 265 | mlx4_free_mtt_range(dev, mtt->offset, mtt->order); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 266 | } |
| 267 | EXPORT_SYMBOL_GPL(mlx4_mtt_cleanup); |
| 268 | |
| 269 | u64 mlx4_mtt_addr(struct mlx4_dev *dev, struct mlx4_mtt *mtt) |
| 270 | { |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 271 | return (u64) mtt->offset * dev->caps.mtt_entry_sz; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 272 | } |
| 273 | EXPORT_SYMBOL_GPL(mlx4_mtt_addr); |
| 274 | |
| 275 | static u32 hw_index_to_key(u32 ind) |
| 276 | { |
| 277 | return (ind >> 24) | (ind << 8); |
| 278 | } |
| 279 | |
| 280 | static u32 key_to_hw_index(u32 key) |
| 281 | { |
| 282 | return (key << 24) | (key >> 8); |
| 283 | } |
| 284 | |
| 285 | static int mlx4_SW2HW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox, |
| 286 | int mpt_index) |
| 287 | { |
Marcel Apfelbaum | eb41049 | 2012-01-19 09:45:19 +0000 | [diff] [blame] | 288 | return mlx4_cmd(dev, mailbox->dma, mpt_index, |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 289 | 0, MLX4_CMD_SW2HW_MPT, MLX4_CMD_TIME_CLASS_B, |
| 290 | MLX4_CMD_WRAPPED); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static int mlx4_HW2SW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox, |
| 294 | int mpt_index) |
| 295 | { |
| 296 | return mlx4_cmd_box(dev, 0, mailbox ? mailbox->dma : 0, mpt_index, |
Jack Morgenstein | f9baff5 | 2011-12-13 04:10:51 +0000 | [diff] [blame] | 297 | !mailbox, MLX4_CMD_HW2SW_MPT, |
| 298 | MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Matan Barak | 4ff0acc | 2014-09-11 13:18:37 +0300 | [diff] [blame] | 301 | /* Must protect against concurrent access */ |
Matan Barak | e630664 | 2014-07-31 11:01:29 +0300 | [diff] [blame] | 302 | int mlx4_mr_hw_get_mpt(struct mlx4_dev *dev, struct mlx4_mr *mmr, |
| 303 | struct mlx4_mpt_entry ***mpt_entry) |
| 304 | { |
| 305 | int err; |
| 306 | int key = key_to_hw_index(mmr->key) & (dev->caps.num_mpts - 1); |
| 307 | struct mlx4_cmd_mailbox *mailbox = NULL; |
| 308 | |
Matan Barak | e630664 | 2014-07-31 11:01:29 +0300 | [diff] [blame] | 309 | if (mmr->enabled != MLX4_MPT_EN_HW) |
| 310 | return -EINVAL; |
| 311 | |
| 312 | err = mlx4_HW2SW_MPT(dev, NULL, key); |
Matan Barak | e630664 | 2014-07-31 11:01:29 +0300 | [diff] [blame] | 313 | if (err) { |
| 314 | mlx4_warn(dev, "HW2SW_MPT failed (%d).", err); |
| 315 | mlx4_warn(dev, "Most likely the MR has MWs bound to it.\n"); |
| 316 | return err; |
| 317 | } |
| 318 | |
| 319 | mmr->enabled = MLX4_MPT_EN_SW; |
| 320 | |
| 321 | if (!mlx4_is_mfunc(dev)) { |
| 322 | **mpt_entry = mlx4_table_find( |
| 323 | &mlx4_priv(dev)->mr_table.dmpt_table, |
| 324 | key, NULL); |
| 325 | } else { |
| 326 | mailbox = mlx4_alloc_cmd_mailbox(dev); |
| 327 | if (IS_ERR_OR_NULL(mailbox)) |
| 328 | return PTR_ERR(mailbox); |
| 329 | |
| 330 | err = mlx4_cmd_box(dev, 0, mailbox->dma, key, |
| 331 | 0, MLX4_CMD_QUERY_MPT, |
| 332 | MLX4_CMD_TIME_CLASS_B, |
| 333 | MLX4_CMD_WRAPPED); |
Matan Barak | e630664 | 2014-07-31 11:01:29 +0300 | [diff] [blame] | 334 | if (err) |
| 335 | goto free_mailbox; |
| 336 | |
| 337 | *mpt_entry = (struct mlx4_mpt_entry **)&mailbox->buf; |
| 338 | } |
| 339 | |
| 340 | if (!(*mpt_entry) || !(**mpt_entry)) { |
| 341 | err = -ENOMEM; |
| 342 | goto free_mailbox; |
| 343 | } |
| 344 | |
| 345 | return 0; |
| 346 | |
| 347 | free_mailbox: |
| 348 | mlx4_free_cmd_mailbox(dev, mailbox); |
| 349 | return err; |
| 350 | } |
| 351 | EXPORT_SYMBOL_GPL(mlx4_mr_hw_get_mpt); |
| 352 | |
| 353 | int mlx4_mr_hw_write_mpt(struct mlx4_dev *dev, struct mlx4_mr *mmr, |
| 354 | struct mlx4_mpt_entry **mpt_entry) |
| 355 | { |
| 356 | int err; |
| 357 | |
| 358 | if (!mlx4_is_mfunc(dev)) { |
| 359 | /* Make sure any changes to this entry are flushed */ |
| 360 | wmb(); |
| 361 | |
| 362 | *(u8 *)(*mpt_entry) = MLX4_MPT_STATUS_HW; |
| 363 | |
| 364 | /* Make sure the new status is written */ |
| 365 | wmb(); |
| 366 | |
| 367 | err = mlx4_SYNC_TPT(dev); |
| 368 | } else { |
| 369 | int key = key_to_hw_index(mmr->key) & (dev->caps.num_mpts - 1); |
| 370 | |
| 371 | struct mlx4_cmd_mailbox *mailbox = |
| 372 | container_of((void *)mpt_entry, struct mlx4_cmd_mailbox, |
| 373 | buf); |
| 374 | |
| 375 | err = mlx4_SW2HW_MPT(dev, mailbox, key); |
| 376 | } |
| 377 | |
Matan Barak | 4ff0acc | 2014-09-11 13:18:37 +0300 | [diff] [blame] | 378 | if (!err) { |
| 379 | mmr->pd = be32_to_cpu((*mpt_entry)->pd_flags) & MLX4_MPT_PD_MASK; |
Matan Barak | e630664 | 2014-07-31 11:01:29 +0300 | [diff] [blame] | 380 | mmr->enabled = MLX4_MPT_EN_HW; |
Matan Barak | 4ff0acc | 2014-09-11 13:18:37 +0300 | [diff] [blame] | 381 | } |
Matan Barak | e630664 | 2014-07-31 11:01:29 +0300 | [diff] [blame] | 382 | return err; |
| 383 | } |
| 384 | EXPORT_SYMBOL_GPL(mlx4_mr_hw_write_mpt); |
| 385 | |
| 386 | void mlx4_mr_hw_put_mpt(struct mlx4_dev *dev, |
| 387 | struct mlx4_mpt_entry **mpt_entry) |
| 388 | { |
| 389 | if (mlx4_is_mfunc(dev)) { |
| 390 | struct mlx4_cmd_mailbox *mailbox = |
| 391 | container_of((void *)mpt_entry, struct mlx4_cmd_mailbox, |
| 392 | buf); |
| 393 | mlx4_free_cmd_mailbox(dev, mailbox); |
| 394 | } |
| 395 | } |
| 396 | EXPORT_SYMBOL_GPL(mlx4_mr_hw_put_mpt); |
| 397 | |
| 398 | int mlx4_mr_hw_change_pd(struct mlx4_dev *dev, struct mlx4_mpt_entry *mpt_entry, |
| 399 | u32 pdn) |
| 400 | { |
Matan Barak | 4ff0acc | 2014-09-11 13:18:37 +0300 | [diff] [blame] | 401 | u32 pd_flags = be32_to_cpu(mpt_entry->pd_flags) & ~MLX4_MPT_PD_MASK; |
Matan Barak | e630664 | 2014-07-31 11:01:29 +0300 | [diff] [blame] | 402 | /* The wrapper function will put the slave's id here */ |
| 403 | if (mlx4_is_mfunc(dev)) |
| 404 | pd_flags &= ~MLX4_MPT_PD_VF_MASK; |
Matan Barak | 4ff0acc | 2014-09-11 13:18:37 +0300 | [diff] [blame] | 405 | |
| 406 | mpt_entry->pd_flags = cpu_to_be32(pd_flags | |
Matan Barak | e630664 | 2014-07-31 11:01:29 +0300 | [diff] [blame] | 407 | (pdn & MLX4_MPT_PD_MASK) |
| 408 | | MLX4_MPT_PD_FLAG_EN_INV); |
| 409 | return 0; |
| 410 | } |
| 411 | EXPORT_SYMBOL_GPL(mlx4_mr_hw_change_pd); |
| 412 | |
| 413 | int mlx4_mr_hw_change_access(struct mlx4_dev *dev, |
| 414 | struct mlx4_mpt_entry *mpt_entry, |
| 415 | u32 access) |
| 416 | { |
| 417 | u32 flags = (be32_to_cpu(mpt_entry->flags) & ~MLX4_PERM_MASK) | |
| 418 | (access & MLX4_PERM_MASK); |
| 419 | |
| 420 | mpt_entry->flags = cpu_to_be32(flags); |
| 421 | return 0; |
| 422 | } |
| 423 | EXPORT_SYMBOL_GPL(mlx4_mr_hw_change_access); |
| 424 | |
Yevgeny Petrilin | 66431a7 | 2012-03-06 04:05:02 +0000 | [diff] [blame] | 425 | static int mlx4_mr_alloc_reserved(struct mlx4_dev *dev, u32 mridx, u32 pd, |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 426 | u64 iova, u64 size, u32 access, int npages, |
| 427 | int page_shift, struct mlx4_mr *mr) |
| 428 | { |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 429 | mr->iova = iova; |
| 430 | mr->size = size; |
| 431 | mr->pd = pd; |
| 432 | mr->access = access; |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 433 | mr->enabled = MLX4_MPT_DISABLED; |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 434 | mr->key = hw_index_to_key(mridx); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 435 | |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 436 | return mlx4_mtt_init(dev, npages, page_shift, &mr->mtt); |
| 437 | } |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 438 | |
| 439 | static int mlx4_WRITE_MTT(struct mlx4_dev *dev, |
| 440 | struct mlx4_cmd_mailbox *mailbox, |
| 441 | int num_entries) |
| 442 | { |
| 443 | return mlx4_cmd(dev, mailbox->dma, num_entries, 0, MLX4_CMD_WRITE_MTT, |
| 444 | MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED); |
| 445 | } |
| 446 | |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 447 | int __mlx4_mpt_reserve(struct mlx4_dev *dev) |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 448 | { |
| 449 | struct mlx4_priv *priv = mlx4_priv(dev); |
| 450 | |
| 451 | return mlx4_bitmap_alloc(&priv->mr_table.mpt_bitmap); |
| 452 | } |
| 453 | |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 454 | static int mlx4_mpt_reserve(struct mlx4_dev *dev) |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 455 | { |
| 456 | u64 out_param; |
| 457 | |
| 458 | if (mlx4_is_mfunc(dev)) { |
| 459 | if (mlx4_cmd_imm(dev, 0, &out_param, RES_MPT, RES_OP_RESERVE, |
| 460 | MLX4_CMD_ALLOC_RES, |
| 461 | MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED)) |
| 462 | return -1; |
| 463 | return get_param_l(&out_param); |
| 464 | } |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 465 | return __mlx4_mpt_reserve(dev); |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 468 | void __mlx4_mpt_release(struct mlx4_dev *dev, u32 index) |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 469 | { |
| 470 | struct mlx4_priv *priv = mlx4_priv(dev); |
| 471 | |
Jack Morgenstein | 7c6d74d | 2013-12-08 16:50:17 +0200 | [diff] [blame] | 472 | mlx4_bitmap_free(&priv->mr_table.mpt_bitmap, index, MLX4_NO_RR); |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 475 | static void mlx4_mpt_release(struct mlx4_dev *dev, u32 index) |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 476 | { |
Jack Morgenstein | e7dbeba | 2013-03-07 03:46:54 +0000 | [diff] [blame] | 477 | u64 in_param = 0; |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 478 | |
| 479 | if (mlx4_is_mfunc(dev)) { |
| 480 | set_param_l(&in_param, index); |
| 481 | if (mlx4_cmd(dev, in_param, RES_MPT, RES_OP_RESERVE, |
| 482 | MLX4_CMD_FREE_RES, |
| 483 | MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED)) |
| 484 | mlx4_warn(dev, "Failed to release mr index:%d\n", |
| 485 | index); |
| 486 | return; |
| 487 | } |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 488 | __mlx4_mpt_release(dev, index); |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Jiri Kosina | 40f2287 | 2014-05-11 15:15:12 +0300 | [diff] [blame] | 491 | int __mlx4_mpt_alloc_icm(struct mlx4_dev *dev, u32 index, gfp_t gfp) |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 492 | { |
| 493 | struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table; |
| 494 | |
Jiri Kosina | 40f2287 | 2014-05-11 15:15:12 +0300 | [diff] [blame] | 495 | return mlx4_table_get(dev, &mr_table->dmpt_table, index, gfp); |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Jiri Kosina | 40f2287 | 2014-05-11 15:15:12 +0300 | [diff] [blame] | 498 | static int mlx4_mpt_alloc_icm(struct mlx4_dev *dev, u32 index, gfp_t gfp) |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 499 | { |
Jack Morgenstein | e7dbeba | 2013-03-07 03:46:54 +0000 | [diff] [blame] | 500 | u64 param = 0; |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 501 | |
| 502 | if (mlx4_is_mfunc(dev)) { |
| 503 | set_param_l(¶m, index); |
| 504 | return mlx4_cmd_imm(dev, param, ¶m, RES_MPT, RES_OP_MAP_ICM, |
| 505 | MLX4_CMD_ALLOC_RES, |
| 506 | MLX4_CMD_TIME_CLASS_A, |
| 507 | MLX4_CMD_WRAPPED); |
| 508 | } |
Jiri Kosina | 40f2287 | 2014-05-11 15:15:12 +0300 | [diff] [blame] | 509 | return __mlx4_mpt_alloc_icm(dev, index, gfp); |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 512 | void __mlx4_mpt_free_icm(struct mlx4_dev *dev, u32 index) |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 513 | { |
| 514 | struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table; |
| 515 | |
| 516 | mlx4_table_put(dev, &mr_table->dmpt_table, index); |
| 517 | } |
| 518 | |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 519 | static void mlx4_mpt_free_icm(struct mlx4_dev *dev, u32 index) |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 520 | { |
Jack Morgenstein | e7dbeba | 2013-03-07 03:46:54 +0000 | [diff] [blame] | 521 | u64 in_param = 0; |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 522 | |
| 523 | if (mlx4_is_mfunc(dev)) { |
| 524 | set_param_l(&in_param, index); |
| 525 | if (mlx4_cmd(dev, in_param, RES_MPT, RES_OP_MAP_ICM, |
| 526 | MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A, |
| 527 | MLX4_CMD_WRAPPED)) |
| 528 | mlx4_warn(dev, "Failed to free icm of mr index:%d\n", |
| 529 | index); |
| 530 | return; |
| 531 | } |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 532 | return __mlx4_mpt_free_icm(dev, index); |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | int mlx4_mr_alloc(struct mlx4_dev *dev, u32 pd, u64 iova, u64 size, u32 access, |
| 536 | int npages, int page_shift, struct mlx4_mr *mr) |
| 537 | { |
| 538 | u32 index; |
| 539 | int err; |
| 540 | |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 541 | index = mlx4_mpt_reserve(dev); |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 542 | if (index == -1) |
| 543 | return -ENOMEM; |
| 544 | |
| 545 | err = mlx4_mr_alloc_reserved(dev, index, pd, iova, size, |
| 546 | access, npages, page_shift, mr); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 547 | if (err) |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 548 | mlx4_mpt_release(dev, index); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 549 | |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 550 | return err; |
| 551 | } |
| 552 | EXPORT_SYMBOL_GPL(mlx4_mr_alloc); |
| 553 | |
Shani Michaeli | 6108372 | 2013-02-06 16:19:09 +0000 | [diff] [blame] | 554 | static int mlx4_mr_free_reserved(struct mlx4_dev *dev, struct mlx4_mr *mr) |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 555 | { |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 556 | int err; |
| 557 | |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 558 | if (mr->enabled == MLX4_MPT_EN_HW) { |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 559 | err = mlx4_HW2SW_MPT(dev, NULL, |
| 560 | key_to_hw_index(mr->key) & |
| 561 | (dev->caps.num_mpts - 1)); |
Shani Michaeli | 6108372 | 2013-02-06 16:19:09 +0000 | [diff] [blame] | 562 | if (err) { |
Joe Perches | 1a91de2 | 2014-05-07 12:52:57 -0700 | [diff] [blame] | 563 | mlx4_warn(dev, "HW2SW_MPT failed (%d), MR has MWs bound to it\n", |
| 564 | err); |
Shani Michaeli | 6108372 | 2013-02-06 16:19:09 +0000 | [diff] [blame] | 565 | return err; |
| 566 | } |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 567 | |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 568 | mr->enabled = MLX4_MPT_EN_SW; |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 569 | } |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 570 | mlx4_mtt_cleanup(dev, &mr->mtt); |
Shani Michaeli | 6108372 | 2013-02-06 16:19:09 +0000 | [diff] [blame] | 571 | |
| 572 | return 0; |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 573 | } |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 574 | |
Shani Michaeli | 6108372 | 2013-02-06 16:19:09 +0000 | [diff] [blame] | 575 | int mlx4_mr_free(struct mlx4_dev *dev, struct mlx4_mr *mr) |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 576 | { |
Shani Michaeli | 6108372 | 2013-02-06 16:19:09 +0000 | [diff] [blame] | 577 | int ret; |
| 578 | |
| 579 | ret = mlx4_mr_free_reserved(dev, mr); |
| 580 | if (ret) |
| 581 | return ret; |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 582 | if (mr->enabled) |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 583 | mlx4_mpt_free_icm(dev, key_to_hw_index(mr->key)); |
| 584 | mlx4_mpt_release(dev, key_to_hw_index(mr->key)); |
Shani Michaeli | 6108372 | 2013-02-06 16:19:09 +0000 | [diff] [blame] | 585 | |
| 586 | return 0; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 587 | } |
| 588 | EXPORT_SYMBOL_GPL(mlx4_mr_free); |
| 589 | |
Matan Barak | e630664 | 2014-07-31 11:01:29 +0300 | [diff] [blame] | 590 | void mlx4_mr_rereg_mem_cleanup(struct mlx4_dev *dev, struct mlx4_mr *mr) |
| 591 | { |
| 592 | mlx4_mtt_cleanup(dev, &mr->mtt); |
| 593 | } |
| 594 | EXPORT_SYMBOL_GPL(mlx4_mr_rereg_mem_cleanup); |
| 595 | |
| 596 | int mlx4_mr_rereg_mem_write(struct mlx4_dev *dev, struct mlx4_mr *mr, |
| 597 | u64 iova, u64 size, int npages, |
| 598 | int page_shift, struct mlx4_mpt_entry *mpt_entry) |
| 599 | { |
| 600 | int err; |
| 601 | |
Matan Barak | 4ff0acc | 2014-09-11 13:18:37 +0300 | [diff] [blame] | 602 | mpt_entry->start = cpu_to_be64(iova); |
| 603 | mpt_entry->length = cpu_to_be64(size); |
| 604 | mpt_entry->entity_size = cpu_to_be32(page_shift); |
Matan Barak | e630664 | 2014-07-31 11:01:29 +0300 | [diff] [blame] | 605 | |
| 606 | err = mlx4_mtt_init(dev, npages, page_shift, &mr->mtt); |
| 607 | if (err) |
| 608 | return err; |
| 609 | |
Matan Barak | 4ff0acc | 2014-09-11 13:18:37 +0300 | [diff] [blame] | 610 | mpt_entry->pd_flags &= cpu_to_be32(MLX4_MPT_PD_MASK | |
| 611 | MLX4_MPT_PD_FLAG_EN_INV); |
| 612 | mpt_entry->flags &= cpu_to_be32(MLX4_MPT_FLAG_FREE | |
| 613 | MLX4_MPT_FLAG_SW_OWNS); |
Matan Barak | e630664 | 2014-07-31 11:01:29 +0300 | [diff] [blame] | 614 | if (mr->mtt.order < 0) { |
| 615 | mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_PHYSICAL); |
| 616 | mpt_entry->mtt_addr = 0; |
| 617 | } else { |
| 618 | mpt_entry->mtt_addr = cpu_to_be64(mlx4_mtt_addr(dev, |
| 619 | &mr->mtt)); |
| 620 | if (mr->mtt.page_shift == 0) |
| 621 | mpt_entry->mtt_sz = cpu_to_be32(1 << mr->mtt.order); |
| 622 | } |
Matan Barak | 4ff0acc | 2014-09-11 13:18:37 +0300 | [diff] [blame] | 623 | if (mr->mtt.order >= 0 && mr->mtt.page_shift == 0) { |
| 624 | /* fast register MR in free state */ |
| 625 | mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_FREE); |
| 626 | mpt_entry->pd_flags |= cpu_to_be32(MLX4_MPT_PD_FLAG_FAST_REG | |
| 627 | MLX4_MPT_PD_FLAG_RAE); |
| 628 | } else { |
| 629 | mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_SW_OWNS); |
| 630 | } |
Matan Barak | e630664 | 2014-07-31 11:01:29 +0300 | [diff] [blame] | 631 | mr->enabled = MLX4_MPT_EN_SW; |
| 632 | |
| 633 | return 0; |
| 634 | } |
| 635 | EXPORT_SYMBOL_GPL(mlx4_mr_rereg_mem_write); |
| 636 | |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 637 | int mlx4_mr_enable(struct mlx4_dev *dev, struct mlx4_mr *mr) |
| 638 | { |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 639 | struct mlx4_cmd_mailbox *mailbox; |
| 640 | struct mlx4_mpt_entry *mpt_entry; |
| 641 | int err; |
| 642 | |
Jiri Kosina | 40f2287 | 2014-05-11 15:15:12 +0300 | [diff] [blame] | 643 | err = mlx4_mpt_alloc_icm(dev, key_to_hw_index(mr->key), GFP_KERNEL); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 644 | if (err) |
| 645 | return err; |
| 646 | |
| 647 | mailbox = mlx4_alloc_cmd_mailbox(dev); |
| 648 | if (IS_ERR(mailbox)) { |
| 649 | err = PTR_ERR(mailbox); |
| 650 | goto err_table; |
| 651 | } |
| 652 | mpt_entry = mailbox->buf; |
Roland Dreier | 95d04f0 | 2008-07-23 08:12:26 -0700 | [diff] [blame] | 653 | mpt_entry->flags = cpu_to_be32(MLX4_MPT_FLAG_MIO | |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 654 | MLX4_MPT_FLAG_REGION | |
| 655 | mr->access); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 656 | |
| 657 | mpt_entry->key = cpu_to_be32(key_to_hw_index(mr->key)); |
Roland Dreier | 95d04f0 | 2008-07-23 08:12:26 -0700 | [diff] [blame] | 658 | mpt_entry->pd_flags = cpu_to_be32(mr->pd | MLX4_MPT_PD_FLAG_EN_INV); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 659 | mpt_entry->start = cpu_to_be64(mr->iova); |
| 660 | mpt_entry->length = cpu_to_be64(mr->size); |
| 661 | mpt_entry->entity_size = cpu_to_be32(mr->mtt.page_shift); |
Roland Dreier | 95d04f0 | 2008-07-23 08:12:26 -0700 | [diff] [blame] | 662 | |
Jack Morgenstein | b2d9308 | 2007-06-07 23:24:38 -0700 | [diff] [blame] | 663 | if (mr->mtt.order < 0) { |
| 664 | mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_PHYSICAL); |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 665 | mpt_entry->mtt_addr = 0; |
Roland Dreier | 95d04f0 | 2008-07-23 08:12:26 -0700 | [diff] [blame] | 666 | } else { |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 667 | mpt_entry->mtt_addr = cpu_to_be64(mlx4_mtt_addr(dev, |
| 668 | &mr->mtt)); |
Roland Dreier | 95d04f0 | 2008-07-23 08:12:26 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | if (mr->mtt.order >= 0 && mr->mtt.page_shift == 0) { |
| 672 | /* fast register MR in free state */ |
| 673 | mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_FREE); |
Vladimir Sokolovsky | c925743 | 2008-09-02 13:38:29 -0700 | [diff] [blame] | 674 | mpt_entry->pd_flags |= cpu_to_be32(MLX4_MPT_PD_FLAG_FAST_REG | |
| 675 | MLX4_MPT_PD_FLAG_RAE); |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 676 | mpt_entry->mtt_sz = cpu_to_be32(1 << mr->mtt.order); |
Roland Dreier | 95d04f0 | 2008-07-23 08:12:26 -0700 | [diff] [blame] | 677 | } else { |
| 678 | mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_SW_OWNS); |
| 679 | } |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 680 | |
| 681 | err = mlx4_SW2HW_MPT(dev, mailbox, |
| 682 | key_to_hw_index(mr->key) & (dev->caps.num_mpts - 1)); |
| 683 | if (err) { |
| 684 | mlx4_warn(dev, "SW2HW_MPT failed (%d)\n", err); |
| 685 | goto err_cmd; |
| 686 | } |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 687 | mr->enabled = MLX4_MPT_EN_HW; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 688 | |
| 689 | mlx4_free_cmd_mailbox(dev, mailbox); |
| 690 | |
| 691 | return 0; |
| 692 | |
| 693 | err_cmd: |
| 694 | mlx4_free_cmd_mailbox(dev, mailbox); |
| 695 | |
| 696 | err_table: |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 697 | mlx4_mpt_free_icm(dev, key_to_hw_index(mr->key)); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 698 | return err; |
| 699 | } |
| 700 | EXPORT_SYMBOL_GPL(mlx4_mr_enable); |
| 701 | |
Jack Morgenstein | d7bb58f | 2007-08-01 12:28:53 +0300 | [diff] [blame] | 702 | static int mlx4_write_mtt_chunk(struct mlx4_dev *dev, struct mlx4_mtt *mtt, |
| 703 | int start_index, int npages, u64 *page_list) |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 704 | { |
Jack Morgenstein | d7bb58f | 2007-08-01 12:28:53 +0300 | [diff] [blame] | 705 | struct mlx4_priv *priv = mlx4_priv(dev); |
| 706 | __be64 *mtts; |
| 707 | dma_addr_t dma_handle; |
| 708 | int i; |
Jack Morgenstein | d7bb58f | 2007-08-01 12:28:53 +0300 | [diff] [blame] | 709 | |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 710 | mtts = mlx4_table_find(&priv->mr_table.mtt_table, mtt->offset + |
| 711 | start_index, &dma_handle); |
Jack Morgenstein | d7bb58f | 2007-08-01 12:28:53 +0300 | [diff] [blame] | 712 | |
Jack Morgenstein | d7bb58f | 2007-08-01 12:28:53 +0300 | [diff] [blame] | 713 | if (!mtts) |
| 714 | return -ENOMEM; |
| 715 | |
Roland Dreier | e727f5c | 2009-06-22 23:07:56 -0700 | [diff] [blame] | 716 | dma_sync_single_for_cpu(&dev->pdev->dev, dma_handle, |
| 717 | npages * sizeof (u64), DMA_TO_DEVICE); |
| 718 | |
Jack Morgenstein | d7bb58f | 2007-08-01 12:28:53 +0300 | [diff] [blame] | 719 | for (i = 0; i < npages; ++i) |
| 720 | mtts[i] = cpu_to_be64(page_list[i] | MLX4_MTT_FLAG_PRESENT); |
| 721 | |
Roland Dreier | e727f5c | 2009-06-22 23:07:56 -0700 | [diff] [blame] | 722 | dma_sync_single_for_device(&dev->pdev->dev, dma_handle, |
| 723 | npages * sizeof (u64), DMA_TO_DEVICE); |
Jack Morgenstein | d7bb58f | 2007-08-01 12:28:53 +0300 | [diff] [blame] | 724 | |
| 725 | return 0; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Eli Cohen | c82e9aa | 2011-12-13 04:15:24 +0000 | [diff] [blame] | 728 | int __mlx4_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt, |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 729 | int start_index, int npages, u64 *page_list) |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 730 | { |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 731 | int err = 0; |
Jack Morgenstein | d7bb58f | 2007-08-01 12:28:53 +0300 | [diff] [blame] | 732 | int chunk; |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 733 | int mtts_per_page; |
| 734 | int max_mtts_first_page; |
| 735 | |
| 736 | /* compute how may mtts fit in the first page */ |
| 737 | mtts_per_page = PAGE_SIZE / sizeof(u64); |
| 738 | max_mtts_first_page = mtts_per_page - (mtt->offset + start_index) |
| 739 | % mtts_per_page; |
| 740 | |
| 741 | chunk = min_t(int, max_mtts_first_page, npages); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 742 | |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 743 | while (npages > 0) { |
Jack Morgenstein | d7bb58f | 2007-08-01 12:28:53 +0300 | [diff] [blame] | 744 | err = mlx4_write_mtt_chunk(dev, mtt, start_index, chunk, page_list); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 745 | if (err) |
Jack Morgenstein | d7bb58f | 2007-08-01 12:28:53 +0300 | [diff] [blame] | 746 | return err; |
Jack Morgenstein | d7bb58f | 2007-08-01 12:28:53 +0300 | [diff] [blame] | 747 | npages -= chunk; |
| 748 | start_index += chunk; |
| 749 | page_list += chunk; |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 750 | |
| 751 | chunk = min_t(int, mtts_per_page, npages); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 752 | } |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 753 | return err; |
| 754 | } |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 755 | |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 756 | int mlx4_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt, |
| 757 | int start_index, int npages, u64 *page_list) |
| 758 | { |
| 759 | struct mlx4_cmd_mailbox *mailbox = NULL; |
| 760 | __be64 *inbox = NULL; |
| 761 | int chunk; |
| 762 | int err = 0; |
| 763 | int i; |
| 764 | |
| 765 | if (mtt->order < 0) |
| 766 | return -EINVAL; |
| 767 | |
| 768 | if (mlx4_is_mfunc(dev)) { |
| 769 | mailbox = mlx4_alloc_cmd_mailbox(dev); |
| 770 | if (IS_ERR(mailbox)) |
| 771 | return PTR_ERR(mailbox); |
| 772 | inbox = mailbox->buf; |
| 773 | |
| 774 | while (npages > 0) { |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 775 | chunk = min_t(int, MLX4_MAILBOX_SIZE / sizeof(u64) - 2, |
| 776 | npages); |
| 777 | inbox[0] = cpu_to_be64(mtt->offset + start_index); |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 778 | inbox[1] = 0; |
| 779 | for (i = 0; i < chunk; ++i) |
| 780 | inbox[i + 2] = cpu_to_be64(page_list[i] | |
| 781 | MLX4_MTT_FLAG_PRESENT); |
| 782 | err = mlx4_WRITE_MTT(dev, mailbox, chunk); |
| 783 | if (err) { |
| 784 | mlx4_free_cmd_mailbox(dev, mailbox); |
| 785 | return err; |
| 786 | } |
| 787 | |
| 788 | npages -= chunk; |
| 789 | start_index += chunk; |
| 790 | page_list += chunk; |
| 791 | } |
| 792 | mlx4_free_cmd_mailbox(dev, mailbox); |
| 793 | return err; |
| 794 | } |
| 795 | |
| 796 | return __mlx4_write_mtt(dev, mtt, start_index, npages, page_list); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 797 | } |
| 798 | EXPORT_SYMBOL_GPL(mlx4_write_mtt); |
| 799 | |
| 800 | int mlx4_buf_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt, |
Jiri Kosina | 40f2287 | 2014-05-11 15:15:12 +0300 | [diff] [blame] | 801 | struct mlx4_buf *buf, gfp_t gfp) |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 802 | { |
| 803 | u64 *page_list; |
| 804 | int err; |
| 805 | int i; |
| 806 | |
Jiri Kosina | 40f2287 | 2014-05-11 15:15:12 +0300 | [diff] [blame] | 807 | page_list = kmalloc(buf->npages * sizeof *page_list, |
| 808 | gfp); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 809 | if (!page_list) |
| 810 | return -ENOMEM; |
| 811 | |
| 812 | for (i = 0; i < buf->npages; ++i) |
| 813 | if (buf->nbufs == 1) |
Roland Dreier | b57aacf | 2008-02-06 21:17:59 -0800 | [diff] [blame] | 814 | page_list[i] = buf->direct.map + (i << buf->page_shift); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 815 | else |
Roland Dreier | b57aacf | 2008-02-06 21:17:59 -0800 | [diff] [blame] | 816 | page_list[i] = buf->page_list[i].map; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 817 | |
| 818 | err = mlx4_write_mtt(dev, mtt, 0, buf->npages, page_list); |
| 819 | |
| 820 | kfree(page_list); |
| 821 | return err; |
| 822 | } |
| 823 | EXPORT_SYMBOL_GPL(mlx4_buf_write_mtt); |
| 824 | |
Shani Michaeli | 804d6a8 | 2013-02-06 16:19:14 +0000 | [diff] [blame] | 825 | int mlx4_mw_alloc(struct mlx4_dev *dev, u32 pd, enum mlx4_mw_type type, |
| 826 | struct mlx4_mw *mw) |
| 827 | { |
| 828 | u32 index; |
| 829 | |
| 830 | if ((type == MLX4_MW_TYPE_1 && |
| 831 | !(dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW)) || |
| 832 | (type == MLX4_MW_TYPE_2 && |
| 833 | !(dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN))) |
| 834 | return -ENOTSUPP; |
| 835 | |
| 836 | index = mlx4_mpt_reserve(dev); |
| 837 | if (index == -1) |
| 838 | return -ENOMEM; |
| 839 | |
| 840 | mw->key = hw_index_to_key(index); |
| 841 | mw->pd = pd; |
| 842 | mw->type = type; |
| 843 | mw->enabled = MLX4_MPT_DISABLED; |
| 844 | |
| 845 | return 0; |
| 846 | } |
| 847 | EXPORT_SYMBOL_GPL(mlx4_mw_alloc); |
| 848 | |
| 849 | int mlx4_mw_enable(struct mlx4_dev *dev, struct mlx4_mw *mw) |
| 850 | { |
| 851 | struct mlx4_cmd_mailbox *mailbox; |
| 852 | struct mlx4_mpt_entry *mpt_entry; |
| 853 | int err; |
| 854 | |
Jiri Kosina | 40f2287 | 2014-05-11 15:15:12 +0300 | [diff] [blame] | 855 | err = mlx4_mpt_alloc_icm(dev, key_to_hw_index(mw->key), GFP_KERNEL); |
Shani Michaeli | 804d6a8 | 2013-02-06 16:19:14 +0000 | [diff] [blame] | 856 | if (err) |
| 857 | return err; |
| 858 | |
| 859 | mailbox = mlx4_alloc_cmd_mailbox(dev); |
| 860 | if (IS_ERR(mailbox)) { |
| 861 | err = PTR_ERR(mailbox); |
| 862 | goto err_table; |
| 863 | } |
| 864 | mpt_entry = mailbox->buf; |
| 865 | |
Shani Michaeli | 804d6a8 | 2013-02-06 16:19:14 +0000 | [diff] [blame] | 866 | /* Note that the MLX4_MPT_FLAG_REGION bit in mpt_entry->flags is turned |
| 867 | * off, thus creating a memory window and not a memory region. |
| 868 | */ |
| 869 | mpt_entry->key = cpu_to_be32(key_to_hw_index(mw->key)); |
| 870 | mpt_entry->pd_flags = cpu_to_be32(mw->pd); |
| 871 | if (mw->type == MLX4_MW_TYPE_2) { |
| 872 | mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_FREE); |
| 873 | mpt_entry->qpn = cpu_to_be32(MLX4_MPT_QP_FLAG_BOUND_QP); |
| 874 | mpt_entry->pd_flags |= cpu_to_be32(MLX4_MPT_PD_FLAG_EN_INV); |
| 875 | } |
| 876 | |
| 877 | err = mlx4_SW2HW_MPT(dev, mailbox, |
| 878 | key_to_hw_index(mw->key) & |
| 879 | (dev->caps.num_mpts - 1)); |
| 880 | if (err) { |
| 881 | mlx4_warn(dev, "SW2HW_MPT failed (%d)\n", err); |
| 882 | goto err_cmd; |
| 883 | } |
| 884 | mw->enabled = MLX4_MPT_EN_HW; |
| 885 | |
| 886 | mlx4_free_cmd_mailbox(dev, mailbox); |
| 887 | |
| 888 | return 0; |
| 889 | |
| 890 | err_cmd: |
| 891 | mlx4_free_cmd_mailbox(dev, mailbox); |
| 892 | |
| 893 | err_table: |
| 894 | mlx4_mpt_free_icm(dev, key_to_hw_index(mw->key)); |
| 895 | return err; |
| 896 | } |
| 897 | EXPORT_SYMBOL_GPL(mlx4_mw_enable); |
| 898 | |
| 899 | void mlx4_mw_free(struct mlx4_dev *dev, struct mlx4_mw *mw) |
| 900 | { |
| 901 | int err; |
| 902 | |
| 903 | if (mw->enabled == MLX4_MPT_EN_HW) { |
| 904 | err = mlx4_HW2SW_MPT(dev, NULL, |
| 905 | key_to_hw_index(mw->key) & |
| 906 | (dev->caps.num_mpts - 1)); |
| 907 | if (err) |
| 908 | mlx4_warn(dev, "xxx HW2SW_MPT failed (%d)\n", err); |
| 909 | |
| 910 | mw->enabled = MLX4_MPT_EN_SW; |
| 911 | } |
| 912 | if (mw->enabled) |
| 913 | mlx4_mpt_free_icm(dev, key_to_hw_index(mw->key)); |
| 914 | mlx4_mpt_release(dev, key_to_hw_index(mw->key)); |
| 915 | } |
| 916 | EXPORT_SYMBOL_GPL(mlx4_mw_free); |
| 917 | |
Roland Dreier | 3d73c28 | 2007-10-10 15:43:54 -0700 | [diff] [blame] | 918 | int mlx4_init_mr_table(struct mlx4_dev *dev) |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 919 | { |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 920 | struct mlx4_priv *priv = mlx4_priv(dev); |
| 921 | struct mlx4_mr_table *mr_table = &priv->mr_table; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 922 | int err; |
| 923 | |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 924 | /* Nothing to do for slaves - all MR handling is forwarded |
| 925 | * to the master */ |
| 926 | if (mlx4_is_slave(dev)) |
| 927 | return 0; |
| 928 | |
Jack Morgenstein | a30f1bc | 2013-11-03 10:03:22 +0200 | [diff] [blame] | 929 | if (!is_power_of_2(dev->caps.num_mpts)) |
| 930 | return -EINVAL; |
| 931 | |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 932 | err = mlx4_bitmap_init(&mr_table->mpt_bitmap, dev->caps.num_mpts, |
Yevgeny Petrilin | 93fc9e1 | 2008-10-22 10:25:29 -0700 | [diff] [blame] | 933 | ~0, dev->caps.reserved_mrws, 0); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 934 | if (err) |
| 935 | return err; |
| 936 | |
| 937 | err = mlx4_buddy_init(&mr_table->mtt_buddy, |
Yishai Hadas | 3de819e | 2012-08-13 08:15:07 +0000 | [diff] [blame] | 938 | ilog2((u32)dev->caps.num_mtts / |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 939 | (1 << log_mtts_per_seg))); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 940 | if (err) |
| 941 | goto err_buddy; |
| 942 | |
| 943 | if (dev->caps.reserved_mtts) { |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 944 | priv->reserved_mtts = |
| 945 | mlx4_alloc_mtt_range(dev, |
| 946 | fls(dev->caps.reserved_mtts - 1)); |
| 947 | if (priv->reserved_mtts < 0) { |
Joe Perches | 1a91de2 | 2014-05-07 12:52:57 -0700 | [diff] [blame] | 948 | mlx4_warn(dev, "MTT table of order %u is too small\n", |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 949 | mr_table->mtt_buddy.max_order); |
| 950 | err = -ENOMEM; |
| 951 | goto err_reserve_mtts; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | return 0; |
| 956 | |
| 957 | err_reserve_mtts: |
| 958 | mlx4_buddy_cleanup(&mr_table->mtt_buddy); |
| 959 | |
| 960 | err_buddy: |
| 961 | mlx4_bitmap_cleanup(&mr_table->mpt_bitmap); |
| 962 | |
| 963 | return err; |
| 964 | } |
| 965 | |
| 966 | void mlx4_cleanup_mr_table(struct mlx4_dev *dev) |
| 967 | { |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 968 | struct mlx4_priv *priv = mlx4_priv(dev); |
| 969 | struct mlx4_mr_table *mr_table = &priv->mr_table; |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 970 | |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 971 | if (mlx4_is_slave(dev)) |
| 972 | return; |
| 973 | if (priv->reserved_mtts >= 0) |
| 974 | mlx4_free_mtt_range(dev, priv->reserved_mtts, |
| 975 | fls(dev->caps.reserved_mtts - 1)); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 976 | mlx4_buddy_cleanup(&mr_table->mtt_buddy); |
| 977 | mlx4_bitmap_cleanup(&mr_table->mpt_bitmap); |
| 978 | } |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 979 | |
| 980 | static inline int mlx4_check_fmr(struct mlx4_fmr *fmr, u64 *page_list, |
| 981 | int npages, u64 iova) |
| 982 | { |
| 983 | int i, page_mask; |
| 984 | |
| 985 | if (npages > fmr->max_pages) |
| 986 | return -EINVAL; |
| 987 | |
| 988 | page_mask = (1 << fmr->page_shift) - 1; |
| 989 | |
| 990 | /* We are getting page lists, so va must be page aligned. */ |
| 991 | if (iova & page_mask) |
| 992 | return -EINVAL; |
| 993 | |
| 994 | /* Trust the user not to pass misaligned data in page_list */ |
| 995 | if (0) |
| 996 | for (i = 0; i < npages; ++i) { |
| 997 | if (page_list[i] & ~page_mask) |
| 998 | return -EINVAL; |
| 999 | } |
| 1000 | |
| 1001 | if (fmr->maps >= fmr->max_maps) |
| 1002 | return -EINVAL; |
| 1003 | |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | int mlx4_map_phys_fmr(struct mlx4_dev *dev, struct mlx4_fmr *fmr, u64 *page_list, |
| 1008 | int npages, u64 iova, u32 *lkey, u32 *rkey) |
| 1009 | { |
| 1010 | u32 key; |
| 1011 | int i, err; |
| 1012 | |
| 1013 | err = mlx4_check_fmr(fmr, page_list, npages, iova); |
| 1014 | if (err) |
| 1015 | return err; |
| 1016 | |
| 1017 | ++fmr->maps; |
| 1018 | |
| 1019 | key = key_to_hw_index(fmr->mr.key); |
| 1020 | key += dev->caps.num_mpts; |
| 1021 | *lkey = *rkey = fmr->mr.key = hw_index_to_key(key); |
| 1022 | |
| 1023 | *(u8 *) fmr->mpt = MLX4_MPT_STATUS_SW; |
| 1024 | |
| 1025 | /* Make sure MPT status is visible before writing MTT entries */ |
| 1026 | wmb(); |
| 1027 | |
Roland Dreier | e727f5c | 2009-06-22 23:07:56 -0700 | [diff] [blame] | 1028 | dma_sync_single_for_cpu(&dev->pdev->dev, fmr->dma_handle, |
| 1029 | npages * sizeof(u64), DMA_TO_DEVICE); |
| 1030 | |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1031 | for (i = 0; i < npages; ++i) |
| 1032 | fmr->mtts[i] = cpu_to_be64(page_list[i] | MLX4_MTT_FLAG_PRESENT); |
| 1033 | |
Roland Dreier | e727f5c | 2009-06-22 23:07:56 -0700 | [diff] [blame] | 1034 | dma_sync_single_for_device(&dev->pdev->dev, fmr->dma_handle, |
| 1035 | npages * sizeof(u64), DMA_TO_DEVICE); |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1036 | |
| 1037 | fmr->mpt->key = cpu_to_be32(key); |
| 1038 | fmr->mpt->lkey = cpu_to_be32(key); |
| 1039 | fmr->mpt->length = cpu_to_be64(npages * (1ull << fmr->page_shift)); |
| 1040 | fmr->mpt->start = cpu_to_be64(iova); |
| 1041 | |
| 1042 | /* Make MTT entries are visible before setting MPT status */ |
| 1043 | wmb(); |
| 1044 | |
| 1045 | *(u8 *) fmr->mpt = MLX4_MPT_STATUS_HW; |
| 1046 | |
| 1047 | /* Make sure MPT status is visible before consumer can use FMR */ |
| 1048 | wmb(); |
| 1049 | |
| 1050 | return 0; |
| 1051 | } |
| 1052 | EXPORT_SYMBOL_GPL(mlx4_map_phys_fmr); |
| 1053 | |
| 1054 | int mlx4_fmr_alloc(struct mlx4_dev *dev, u32 pd, u32 access, int max_pages, |
| 1055 | int max_maps, u8 page_shift, struct mlx4_fmr *fmr) |
| 1056 | { |
| 1057 | struct mlx4_priv *priv = mlx4_priv(dev); |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1058 | int err = -ENOMEM; |
| 1059 | |
Eli Cohen | a5bbe89 | 2012-02-09 18:10:06 +0200 | [diff] [blame] | 1060 | if (max_maps > dev->caps.max_fmr_maps) |
| 1061 | return -EINVAL; |
| 1062 | |
Oren Duer | c5057dd | 2008-05-05 15:56:52 -0700 | [diff] [blame] | 1063 | if (page_shift < (ffs(dev->caps.page_size_cap) - 1) || page_shift >= 32) |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1064 | return -EINVAL; |
| 1065 | |
| 1066 | /* All MTTs must fit in the same page */ |
| 1067 | if (max_pages * sizeof *fmr->mtts > PAGE_SIZE) |
| 1068 | return -EINVAL; |
| 1069 | |
| 1070 | fmr->page_shift = page_shift; |
| 1071 | fmr->max_pages = max_pages; |
| 1072 | fmr->max_maps = max_maps; |
| 1073 | fmr->maps = 0; |
| 1074 | |
| 1075 | err = mlx4_mr_alloc(dev, pd, 0, 0, access, max_pages, |
| 1076 | page_shift, &fmr->mr); |
| 1077 | if (err) |
| 1078 | return err; |
| 1079 | |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1080 | fmr->mtts = mlx4_table_find(&priv->mr_table.mtt_table, |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 1081 | fmr->mr.mtt.offset, |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1082 | &fmr->dma_handle); |
Marcel Apfelbaum | 2b8fb28 | 2011-12-13 04:16:56 +0000 | [diff] [blame] | 1083 | |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1084 | if (!fmr->mtts) { |
| 1085 | err = -ENOMEM; |
| 1086 | goto err_free; |
| 1087 | } |
| 1088 | |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1089 | return 0; |
| 1090 | |
| 1091 | err_free: |
Shani Michaeli | 6108372 | 2013-02-06 16:19:09 +0000 | [diff] [blame] | 1092 | (void) mlx4_mr_free(dev, &fmr->mr); |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1093 | return err; |
| 1094 | } |
| 1095 | EXPORT_SYMBOL_GPL(mlx4_fmr_alloc); |
| 1096 | |
| 1097 | int mlx4_fmr_enable(struct mlx4_dev *dev, struct mlx4_fmr *fmr) |
| 1098 | { |
Jack Morgenstein | 11e75a7 | 2008-02-14 13:41:29 +0200 | [diff] [blame] | 1099 | struct mlx4_priv *priv = mlx4_priv(dev); |
| 1100 | int err; |
| 1101 | |
| 1102 | err = mlx4_mr_enable(dev, &fmr->mr); |
| 1103 | if (err) |
| 1104 | return err; |
| 1105 | |
| 1106 | fmr->mpt = mlx4_table_find(&priv->mr_table.dmpt_table, |
| 1107 | key_to_hw_index(fmr->mr.key), NULL); |
| 1108 | if (!fmr->mpt) |
| 1109 | return -ENOMEM; |
| 1110 | |
| 1111 | return 0; |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1112 | } |
| 1113 | EXPORT_SYMBOL_GPL(mlx4_fmr_enable); |
| 1114 | |
| 1115 | void mlx4_fmr_unmap(struct mlx4_dev *dev, struct mlx4_fmr *fmr, |
| 1116 | u32 *lkey, u32 *rkey) |
| 1117 | { |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 1118 | struct mlx4_cmd_mailbox *mailbox; |
| 1119 | int err; |
| 1120 | |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1121 | if (!fmr->maps) |
| 1122 | return; |
| 1123 | |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1124 | fmr->maps = 0; |
| 1125 | |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 1126 | mailbox = mlx4_alloc_cmd_mailbox(dev); |
| 1127 | if (IS_ERR(mailbox)) { |
| 1128 | err = PTR_ERR(mailbox); |
Amir Vadai | c20862c | 2014-05-22 15:55:40 +0300 | [diff] [blame] | 1129 | pr_warn("mlx4_ib: mlx4_alloc_cmd_mailbox failed (%d)\n", err); |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 1130 | return; |
| 1131 | } |
| 1132 | |
| 1133 | err = mlx4_HW2SW_MPT(dev, NULL, |
| 1134 | key_to_hw_index(fmr->mr.key) & |
| 1135 | (dev->caps.num_mpts - 1)); |
| 1136 | mlx4_free_cmd_mailbox(dev, mailbox); |
| 1137 | if (err) { |
Amir Vadai | c20862c | 2014-05-22 15:55:40 +0300 | [diff] [blame] | 1138 | pr_warn("mlx4_ib: mlx4_HW2SW_MPT failed (%d)\n", err); |
Jack Morgenstein | ea51b37 | 2011-12-13 04:13:48 +0000 | [diff] [blame] | 1139 | return; |
| 1140 | } |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 1141 | fmr->mr.enabled = MLX4_MPT_EN_SW; |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1142 | } |
| 1143 | EXPORT_SYMBOL_GPL(mlx4_fmr_unmap); |
| 1144 | |
| 1145 | int mlx4_fmr_free(struct mlx4_dev *dev, struct mlx4_fmr *fmr) |
| 1146 | { |
Shani Michaeli | 6108372 | 2013-02-06 16:19:09 +0000 | [diff] [blame] | 1147 | int ret; |
| 1148 | |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1149 | if (fmr->maps) |
| 1150 | return -EBUSY; |
| 1151 | |
Shani Michaeli | 6108372 | 2013-02-06 16:19:09 +0000 | [diff] [blame] | 1152 | ret = mlx4_mr_free(dev, &fmr->mr); |
| 1153 | if (ret) |
| 1154 | return ret; |
Shani Michaeli | b20e519 | 2013-02-06 16:19:08 +0000 | [diff] [blame] | 1155 | fmr->mr.enabled = MLX4_MPT_DISABLED; |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1156 | |
| 1157 | return 0; |
| 1158 | } |
| 1159 | EXPORT_SYMBOL_GPL(mlx4_fmr_free); |
| 1160 | |
| 1161 | int mlx4_SYNC_TPT(struct mlx4_dev *dev) |
| 1162 | { |
Jack Morgenstein | f9baff5 | 2011-12-13 04:10:51 +0000 | [diff] [blame] | 1163 | return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_SYNC_TPT, 1000, |
Jack Morgenstein | 5e92d80 | 2012-05-15 10:34:59 +0000 | [diff] [blame] | 1164 | MLX4_CMD_NATIVE); |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 1165 | } |
| 1166 | EXPORT_SYMBOL_GPL(mlx4_SYNC_TPT); |