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