blob: 9f693e120025b76c94845b0a0b50f857dd2dbaa4 [file] [log] [blame]
Roland Dreier225c7b12007-05-08 18:00:38 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
Jack Morgenstein51a379d2008-07-25 10:32:52 -07003 * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
Roland Dreier225c7b12007-05-08 18:00:38 -07004 * 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
Jack Morgensteinea51b372011-12-13 04:13:48 +000035#include <linux/init.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070036#include <linux/errno.h>
Paul Gortmakeree40fa02011-05-27 16:14:23 -040037#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Jack Morgensteinea51b372011-12-13 04:13:48 +000039#include <linux/kernel.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070040
41#include <linux/mlx4/cmd.h>
42
43#include "mlx4.h"
44#include "icm.h"
45
Roland Dreier225c7b12007-05-08 18:00:38 -070046#define MLX4_MPT_FLAG_SW_OWNS (0xfUL << 28)
Roland Dreier95d04f02008-07-23 08:12:26 -070047#define MLX4_MPT_FLAG_FREE (0x3UL << 28)
Roland Dreier225c7b12007-05-08 18:00:38 -070048#define MLX4_MPT_FLAG_MIO (1 << 17)
49#define MLX4_MPT_FLAG_BIND_ENABLE (1 << 15)
50#define MLX4_MPT_FLAG_PHYSICAL (1 << 9)
51#define MLX4_MPT_FLAG_REGION (1 << 8)
52
Vladimir Sokolovskyc9257432008-09-02 13:38:29 -070053#define MLX4_MPT_PD_FLAG_FAST_REG (1 << 27)
54#define MLX4_MPT_PD_FLAG_RAE (1 << 28)
Roland Dreier95d04f02008-07-23 08:12:26 -070055#define MLX4_MPT_PD_FLAG_EN_INV (3 << 24)
56
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +030057#define MLX4_MPT_STATUS_SW 0xF0
58#define MLX4_MPT_STATUS_HW 0x00
59
Roland Dreier225c7b12007-05-08 18:00:38 -070060static u32 mlx4_buddy_alloc(struct mlx4_buddy *buddy, int order)
61{
62 int o;
63 int m;
64 u32 seg;
65
66 spin_lock(&buddy->lock);
67
Roland Dreiere4044cf2008-07-22 14:19:40 -070068 for (o = order; o <= buddy->max_order; ++o)
69 if (buddy->num_free[o]) {
70 m = 1 << (buddy->max_order - o);
71 seg = find_first_bit(buddy->bits[o], m);
72 if (seg < m)
73 goto found;
74 }
Roland Dreier225c7b12007-05-08 18:00:38 -070075
76 spin_unlock(&buddy->lock);
77 return -1;
78
79 found:
80 clear_bit(seg, buddy->bits[o]);
Roland Dreiere4044cf2008-07-22 14:19:40 -070081 --buddy->num_free[o];
Roland Dreier225c7b12007-05-08 18:00:38 -070082
83 while (o > order) {
84 --o;
85 seg <<= 1;
86 set_bit(seg ^ 1, buddy->bits[o]);
Roland Dreiere4044cf2008-07-22 14:19:40 -070087 ++buddy->num_free[o];
Roland Dreier225c7b12007-05-08 18:00:38 -070088 }
89
90 spin_unlock(&buddy->lock);
91
92 seg <<= order;
93
94 return seg;
95}
96
97static void mlx4_buddy_free(struct mlx4_buddy *buddy, u32 seg, int order)
98{
99 seg >>= order;
100
101 spin_lock(&buddy->lock);
102
103 while (test_bit(seg ^ 1, buddy->bits[order])) {
104 clear_bit(seg ^ 1, buddy->bits[order]);
Roland Dreiere4044cf2008-07-22 14:19:40 -0700105 --buddy->num_free[order];
Roland Dreier225c7b12007-05-08 18:00:38 -0700106 seg >>= 1;
107 ++order;
108 }
109
110 set_bit(seg, buddy->bits[order]);
Roland Dreiere4044cf2008-07-22 14:19:40 -0700111 ++buddy->num_free[order];
Roland Dreier225c7b12007-05-08 18:00:38 -0700112
113 spin_unlock(&buddy->lock);
114}
115
Roland Dreiere8f9b2e2008-02-04 20:20:41 -0800116static int mlx4_buddy_init(struct mlx4_buddy *buddy, int max_order)
Roland Dreier225c7b12007-05-08 18:00:38 -0700117{
118 int i, s;
119
120 buddy->max_order = max_order;
121 spin_lock_init(&buddy->lock);
122
123 buddy->bits = kzalloc((buddy->max_order + 1) * sizeof (long *),
124 GFP_KERNEL);
Eli Cohena8312752011-10-06 09:33:12 -0700125 buddy->num_free = kcalloc((buddy->max_order + 1), sizeof *buddy->num_free,
Roland Dreiere4044cf2008-07-22 14:19:40 -0700126 GFP_KERNEL);
127 if (!buddy->bits || !buddy->num_free)
Roland Dreier225c7b12007-05-08 18:00:38 -0700128 goto err_out;
129
130 for (i = 0; i <= buddy->max_order; ++i) {
131 s = BITS_TO_LONGS(1 << (buddy->max_order - i));
132 buddy->bits[i] = kmalloc(s * sizeof (long), GFP_KERNEL);
133 if (!buddy->bits[i])
134 goto err_out_free;
135 bitmap_zero(buddy->bits[i], 1 << (buddy->max_order - i));
136 }
137
138 set_bit(0, buddy->bits[buddy->max_order]);
Roland Dreiere4044cf2008-07-22 14:19:40 -0700139 buddy->num_free[buddy->max_order] = 1;
Roland Dreier225c7b12007-05-08 18:00:38 -0700140
141 return 0;
142
143err_out_free:
144 for (i = 0; i <= buddy->max_order; ++i)
145 kfree(buddy->bits[i]);
146
Roland Dreier225c7b12007-05-08 18:00:38 -0700147err_out:
Roland Dreiere4044cf2008-07-22 14:19:40 -0700148 kfree(buddy->bits);
149 kfree(buddy->num_free);
150
Roland Dreier225c7b12007-05-08 18:00:38 -0700151 return -ENOMEM;
152}
153
154static void mlx4_buddy_cleanup(struct mlx4_buddy *buddy)
155{
156 int i;
157
158 for (i = 0; i <= buddy->max_order; ++i)
159 kfree(buddy->bits[i]);
160
161 kfree(buddy->bits);
Roland Dreiere4044cf2008-07-22 14:19:40 -0700162 kfree(buddy->num_free);
Roland Dreier225c7b12007-05-08 18:00:38 -0700163}
164
Eli Cohenc82e9aa2011-12-13 04:15:24 +0000165u32 __mlx4_alloc_mtt_range(struct mlx4_dev *dev, int order)
Roland Dreier225c7b12007-05-08 18:00:38 -0700166{
167 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
168 u32 seg;
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000169 int seg_order;
170 u32 offset;
Roland Dreier225c7b12007-05-08 18:00:38 -0700171
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000172 seg_order = max_t(int, order - log_mtts_per_seg, 0);
173
174 seg = mlx4_buddy_alloc(&mr_table->mtt_buddy, seg_order);
Roland Dreier225c7b12007-05-08 18:00:38 -0700175 if (seg == -1)
176 return -1;
177
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000178 offset = seg * (1 << log_mtts_per_seg);
179
180 if (mlx4_table_get_range(dev, &mr_table->mtt_table, offset,
181 offset + (1 << order) - 1)) {
182 mlx4_buddy_free(&mr_table->mtt_buddy, seg, seg_order);
Roland Dreier225c7b12007-05-08 18:00:38 -0700183 return -1;
184 }
185
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000186 return offset;
Roland Dreier225c7b12007-05-08 18:00:38 -0700187}
188
Jack Morgensteinea51b372011-12-13 04:13:48 +0000189static u32 mlx4_alloc_mtt_range(struct mlx4_dev *dev, int order)
190{
191 u64 in_param;
192 u64 out_param;
193 int err;
194
195 if (mlx4_is_mfunc(dev)) {
196 set_param_l(&in_param, order);
197 err = mlx4_cmd_imm(dev, in_param, &out_param, RES_MTT,
198 RES_OP_RESERVE_AND_MAP,
199 MLX4_CMD_ALLOC_RES,
200 MLX4_CMD_TIME_CLASS_A,
201 MLX4_CMD_WRAPPED);
202 if (err)
203 return -1;
204 return get_param_l(&out_param);
205 }
206 return __mlx4_alloc_mtt_range(dev, order);
207}
208
Roland Dreier225c7b12007-05-08 18:00:38 -0700209int mlx4_mtt_init(struct mlx4_dev *dev, int npages, int page_shift,
210 struct mlx4_mtt *mtt)
211{
212 int i;
213
214 if (!npages) {
215 mtt->order = -1;
216 mtt->page_shift = MLX4_ICM_PAGE_SHIFT;
217 return 0;
218 } else
219 mtt->page_shift = page_shift;
220
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000221 for (mtt->order = 0, i = 1; i < npages; i <<= 1)
Roland Dreier225c7b12007-05-08 18:00:38 -0700222 ++mtt->order;
223
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000224 mtt->offset = mlx4_alloc_mtt_range(dev, mtt->order);
225 if (mtt->offset == -1)
Roland Dreier225c7b12007-05-08 18:00:38 -0700226 return -ENOMEM;
227
228 return 0;
229}
230EXPORT_SYMBOL_GPL(mlx4_mtt_init);
231
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000232void __mlx4_free_mtt_range(struct mlx4_dev *dev, u32 offset, int order)
Roland Dreier225c7b12007-05-08 18:00:38 -0700233{
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000234 u32 first_seg;
235 int seg_order;
Roland Dreier225c7b12007-05-08 18:00:38 -0700236 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
237
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000238 seg_order = max_t(int, order - log_mtts_per_seg, 0);
239 first_seg = offset / (1 << log_mtts_per_seg);
240
241 mlx4_buddy_free(&mr_table->mtt_buddy, first_seg, seg_order);
Marcel Apfelbaum1e27ca62012-01-02 04:07:39 +0000242 mlx4_table_put_range(dev, &mr_table->mtt_table, offset,
243 offset + (1 << order) - 1);
Jack Morgensteinea51b372011-12-13 04:13:48 +0000244}
245
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000246static void mlx4_free_mtt_range(struct mlx4_dev *dev, u32 offset, int order)
Jack Morgensteinea51b372011-12-13 04:13:48 +0000247{
248 u64 in_param;
249 int err;
250
251 if (mlx4_is_mfunc(dev)) {
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000252 set_param_l(&in_param, offset);
Jack Morgensteinea51b372011-12-13 04:13:48 +0000253 set_param_h(&in_param, order);
254 err = mlx4_cmd(dev, in_param, RES_MTT, RES_OP_RESERVE_AND_MAP,
255 MLX4_CMD_FREE_RES,
256 MLX4_CMD_TIME_CLASS_A,
257 MLX4_CMD_WRAPPED);
258 if (err)
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000259 mlx4_warn(dev, "Failed to free mtt range at:"
260 "%d order:%d\n", offset, order);
Jack Morgensteinea51b372011-12-13 04:13:48 +0000261 return;
262 }
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000263 __mlx4_free_mtt_range(dev, offset, order);
Jack Morgensteinea51b372011-12-13 04:13:48 +0000264}
265
266void mlx4_mtt_cleanup(struct mlx4_dev *dev, struct mlx4_mtt *mtt)
267{
Roland Dreier225c7b12007-05-08 18:00:38 -0700268 if (mtt->order < 0)
269 return;
270
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000271 mlx4_free_mtt_range(dev, mtt->offset, mtt->order);
Roland Dreier225c7b12007-05-08 18:00:38 -0700272}
273EXPORT_SYMBOL_GPL(mlx4_mtt_cleanup);
274
275u64 mlx4_mtt_addr(struct mlx4_dev *dev, struct mlx4_mtt *mtt)
276{
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000277 return (u64) mtt->offset * dev->caps.mtt_entry_sz;
Roland Dreier225c7b12007-05-08 18:00:38 -0700278}
279EXPORT_SYMBOL_GPL(mlx4_mtt_addr);
280
281static u32 hw_index_to_key(u32 ind)
282{
283 return (ind >> 24) | (ind << 8);
284}
285
286static u32 key_to_hw_index(u32 key)
287{
288 return (key << 24) | (key >> 8);
289}
290
291static int mlx4_SW2HW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
292 int mpt_index)
293{
Marcel Apfelbaumeb410492012-01-19 09:45:19 +0000294 return mlx4_cmd(dev, mailbox->dma, mpt_index,
Jack Morgensteinea51b372011-12-13 04:13:48 +0000295 0, MLX4_CMD_SW2HW_MPT, MLX4_CMD_TIME_CLASS_B,
296 MLX4_CMD_WRAPPED);
Roland Dreier225c7b12007-05-08 18:00:38 -0700297}
298
299static int mlx4_HW2SW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
300 int mpt_index)
301{
302 return mlx4_cmd_box(dev, 0, mailbox ? mailbox->dma : 0, mpt_index,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000303 !mailbox, MLX4_CMD_HW2SW_MPT,
304 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
Roland Dreier225c7b12007-05-08 18:00:38 -0700305}
306
Doug Ledford22c8bff2012-02-20 12:19:03 -0500307int mlx4_mr_alloc_reserved(struct mlx4_dev *dev, u32 mridx, u32 pd,
Jack Morgensteinea51b372011-12-13 04:13:48 +0000308 u64 iova, u64 size, u32 access, int npages,
309 int page_shift, struct mlx4_mr *mr)
310{
Roland Dreier225c7b12007-05-08 18:00:38 -0700311 mr->iova = iova;
312 mr->size = size;
313 mr->pd = pd;
314 mr->access = access;
Jack Morgensteinea51b372011-12-13 04:13:48 +0000315 mr->enabled = MLX4_MR_DISABLED;
316 mr->key = hw_index_to_key(mridx);
Roland Dreier225c7b12007-05-08 18:00:38 -0700317
Jack Morgensteinea51b372011-12-13 04:13:48 +0000318 return mlx4_mtt_init(dev, npages, page_shift, &mr->mtt);
319}
320EXPORT_SYMBOL_GPL(mlx4_mr_alloc_reserved);
321
322static int mlx4_WRITE_MTT(struct mlx4_dev *dev,
323 struct mlx4_cmd_mailbox *mailbox,
324 int num_entries)
325{
326 return mlx4_cmd(dev, mailbox->dma, num_entries, 0, MLX4_CMD_WRITE_MTT,
327 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
328}
329
Eli Cohenc82e9aa2011-12-13 04:15:24 +0000330int __mlx4_mr_reserve(struct mlx4_dev *dev)
Jack Morgensteinea51b372011-12-13 04:13:48 +0000331{
332 struct mlx4_priv *priv = mlx4_priv(dev);
333
334 return mlx4_bitmap_alloc(&priv->mr_table.mpt_bitmap);
335}
336
337static int mlx4_mr_reserve(struct mlx4_dev *dev)
338{
339 u64 out_param;
340
341 if (mlx4_is_mfunc(dev)) {
342 if (mlx4_cmd_imm(dev, 0, &out_param, RES_MPT, RES_OP_RESERVE,
343 MLX4_CMD_ALLOC_RES,
344 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED))
345 return -1;
346 return get_param_l(&out_param);
347 }
348 return __mlx4_mr_reserve(dev);
349}
350
Eli Cohenc82e9aa2011-12-13 04:15:24 +0000351void __mlx4_mr_release(struct mlx4_dev *dev, u32 index)
Jack Morgensteinea51b372011-12-13 04:13:48 +0000352{
353 struct mlx4_priv *priv = mlx4_priv(dev);
354
355 mlx4_bitmap_free(&priv->mr_table.mpt_bitmap, index);
356}
357
358static void mlx4_mr_release(struct mlx4_dev *dev, u32 index)
359{
360 u64 in_param;
361
362 if (mlx4_is_mfunc(dev)) {
363 set_param_l(&in_param, index);
364 if (mlx4_cmd(dev, in_param, RES_MPT, RES_OP_RESERVE,
365 MLX4_CMD_FREE_RES,
366 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED))
367 mlx4_warn(dev, "Failed to release mr index:%d\n",
368 index);
369 return;
370 }
371 __mlx4_mr_release(dev, index);
372}
373
Eli Cohenc82e9aa2011-12-13 04:15:24 +0000374int __mlx4_mr_alloc_icm(struct mlx4_dev *dev, u32 index)
Jack Morgensteinea51b372011-12-13 04:13:48 +0000375{
376 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
377
378 return mlx4_table_get(dev, &mr_table->dmpt_table, index);
379}
380
381static int mlx4_mr_alloc_icm(struct mlx4_dev *dev, u32 index)
382{
383 u64 param;
384
385 if (mlx4_is_mfunc(dev)) {
386 set_param_l(&param, index);
387 return mlx4_cmd_imm(dev, param, &param, RES_MPT, RES_OP_MAP_ICM,
388 MLX4_CMD_ALLOC_RES,
389 MLX4_CMD_TIME_CLASS_A,
390 MLX4_CMD_WRAPPED);
391 }
392 return __mlx4_mr_alloc_icm(dev, index);
393}
394
Eli Cohenc82e9aa2011-12-13 04:15:24 +0000395void __mlx4_mr_free_icm(struct mlx4_dev *dev, u32 index)
Jack Morgensteinea51b372011-12-13 04:13:48 +0000396{
397 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
398
399 mlx4_table_put(dev, &mr_table->dmpt_table, index);
400}
401
402static void mlx4_mr_free_icm(struct mlx4_dev *dev, u32 index)
403{
404 u64 in_param;
405
406 if (mlx4_is_mfunc(dev)) {
407 set_param_l(&in_param, index);
408 if (mlx4_cmd(dev, in_param, RES_MPT, RES_OP_MAP_ICM,
409 MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
410 MLX4_CMD_WRAPPED))
411 mlx4_warn(dev, "Failed to free icm of mr index:%d\n",
412 index);
413 return;
414 }
415 return __mlx4_mr_free_icm(dev, index);
416}
417
418int mlx4_mr_alloc(struct mlx4_dev *dev, u32 pd, u64 iova, u64 size, u32 access,
419 int npages, int page_shift, struct mlx4_mr *mr)
420{
421 u32 index;
422 int err;
423
424 index = mlx4_mr_reserve(dev);
425 if (index == -1)
426 return -ENOMEM;
427
428 err = mlx4_mr_alloc_reserved(dev, index, pd, iova, size,
429 access, npages, page_shift, mr);
Roland Dreier225c7b12007-05-08 18:00:38 -0700430 if (err)
Jack Morgensteinea51b372011-12-13 04:13:48 +0000431 mlx4_mr_release(dev, index);
Roland Dreier225c7b12007-05-08 18:00:38 -0700432
Roland Dreier225c7b12007-05-08 18:00:38 -0700433 return err;
434}
435EXPORT_SYMBOL_GPL(mlx4_mr_alloc);
436
Doug Ledford22c8bff2012-02-20 12:19:03 -0500437void mlx4_mr_free_reserved(struct mlx4_dev *dev, struct mlx4_mr *mr)
Roland Dreier225c7b12007-05-08 18:00:38 -0700438{
Roland Dreier225c7b12007-05-08 18:00:38 -0700439 int err;
440
Jack Morgensteinea51b372011-12-13 04:13:48 +0000441 if (mr->enabled == MLX4_MR_EN_HW) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700442 err = mlx4_HW2SW_MPT(dev, NULL,
443 key_to_hw_index(mr->key) &
444 (dev->caps.num_mpts - 1));
445 if (err)
Jack Morgensteinea51b372011-12-13 04:13:48 +0000446 mlx4_warn(dev, "xxx HW2SW_MPT failed (%d)\n", err);
Roland Dreier225c7b12007-05-08 18:00:38 -0700447
Jack Morgensteinea51b372011-12-13 04:13:48 +0000448 mr->enabled = MLX4_MR_EN_SW;
449 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700450 mlx4_mtt_cleanup(dev, &mr->mtt);
Jack Morgensteinea51b372011-12-13 04:13:48 +0000451}
452EXPORT_SYMBOL_GPL(mlx4_mr_free_reserved);
453
454void mlx4_mr_free(struct mlx4_dev *dev, struct mlx4_mr *mr)
455{
456 mlx4_mr_free_reserved(dev, mr);
457 if (mr->enabled)
458 mlx4_mr_free_icm(dev, key_to_hw_index(mr->key));
459 mlx4_mr_release(dev, key_to_hw_index(mr->key));
Roland Dreier225c7b12007-05-08 18:00:38 -0700460}
461EXPORT_SYMBOL_GPL(mlx4_mr_free);
462
463int mlx4_mr_enable(struct mlx4_dev *dev, struct mlx4_mr *mr)
464{
Roland Dreier225c7b12007-05-08 18:00:38 -0700465 struct mlx4_cmd_mailbox *mailbox;
466 struct mlx4_mpt_entry *mpt_entry;
467 int err;
468
Jack Morgensteinea51b372011-12-13 04:13:48 +0000469 err = mlx4_mr_alloc_icm(dev, key_to_hw_index(mr->key));
Roland Dreier225c7b12007-05-08 18:00:38 -0700470 if (err)
471 return err;
472
473 mailbox = mlx4_alloc_cmd_mailbox(dev);
474 if (IS_ERR(mailbox)) {
475 err = PTR_ERR(mailbox);
476 goto err_table;
477 }
478 mpt_entry = mailbox->buf;
479
480 memset(mpt_entry, 0, sizeof *mpt_entry);
481
Roland Dreier95d04f02008-07-23 08:12:26 -0700482 mpt_entry->flags = cpu_to_be32(MLX4_MPT_FLAG_MIO |
Roland Dreier225c7b12007-05-08 18:00:38 -0700483 MLX4_MPT_FLAG_REGION |
484 mr->access);
Roland Dreier225c7b12007-05-08 18:00:38 -0700485
486 mpt_entry->key = cpu_to_be32(key_to_hw_index(mr->key));
Roland Dreier95d04f02008-07-23 08:12:26 -0700487 mpt_entry->pd_flags = cpu_to_be32(mr->pd | MLX4_MPT_PD_FLAG_EN_INV);
Roland Dreier225c7b12007-05-08 18:00:38 -0700488 mpt_entry->start = cpu_to_be64(mr->iova);
489 mpt_entry->length = cpu_to_be64(mr->size);
490 mpt_entry->entity_size = cpu_to_be32(mr->mtt.page_shift);
Roland Dreier95d04f02008-07-23 08:12:26 -0700491
Jack Morgensteinb2d93082007-06-07 23:24:38 -0700492 if (mr->mtt.order < 0) {
493 mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_PHYSICAL);
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000494 mpt_entry->mtt_addr = 0;
Roland Dreier95d04f02008-07-23 08:12:26 -0700495 } else {
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000496 mpt_entry->mtt_addr = cpu_to_be64(mlx4_mtt_addr(dev,
497 &mr->mtt));
Roland Dreier95d04f02008-07-23 08:12:26 -0700498 }
499
500 if (mr->mtt.order >= 0 && mr->mtt.page_shift == 0) {
501 /* fast register MR in free state */
502 mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_FREE);
Vladimir Sokolovskyc9257432008-09-02 13:38:29 -0700503 mpt_entry->pd_flags |= cpu_to_be32(MLX4_MPT_PD_FLAG_FAST_REG |
504 MLX4_MPT_PD_FLAG_RAE);
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000505 mpt_entry->mtt_sz = cpu_to_be32(1 << mr->mtt.order);
Roland Dreier95d04f02008-07-23 08:12:26 -0700506 } else {
507 mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_SW_OWNS);
508 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700509
510 err = mlx4_SW2HW_MPT(dev, mailbox,
511 key_to_hw_index(mr->key) & (dev->caps.num_mpts - 1));
512 if (err) {
513 mlx4_warn(dev, "SW2HW_MPT failed (%d)\n", err);
514 goto err_cmd;
515 }
Jack Morgensteinea51b372011-12-13 04:13:48 +0000516 mr->enabled = MLX4_MR_EN_HW;
Roland Dreier225c7b12007-05-08 18:00:38 -0700517
518 mlx4_free_cmd_mailbox(dev, mailbox);
519
520 return 0;
521
522err_cmd:
523 mlx4_free_cmd_mailbox(dev, mailbox);
524
525err_table:
Jack Morgensteinea51b372011-12-13 04:13:48 +0000526 mlx4_mr_free_icm(dev, key_to_hw_index(mr->key));
Roland Dreier225c7b12007-05-08 18:00:38 -0700527 return err;
528}
529EXPORT_SYMBOL_GPL(mlx4_mr_enable);
530
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300531static int mlx4_write_mtt_chunk(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
532 int start_index, int npages, u64 *page_list)
Roland Dreier225c7b12007-05-08 18:00:38 -0700533{
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300534 struct mlx4_priv *priv = mlx4_priv(dev);
535 __be64 *mtts;
536 dma_addr_t dma_handle;
537 int i;
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300538
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000539 mtts = mlx4_table_find(&priv->mr_table.mtt_table, mtt->offset +
540 start_index, &dma_handle);
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300541
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300542 if (!mtts)
543 return -ENOMEM;
544
Roland Dreiere727f5c2009-06-22 23:07:56 -0700545 dma_sync_single_for_cpu(&dev->pdev->dev, dma_handle,
546 npages * sizeof (u64), DMA_TO_DEVICE);
547
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300548 for (i = 0; i < npages; ++i)
549 mtts[i] = cpu_to_be64(page_list[i] | MLX4_MTT_FLAG_PRESENT);
550
Roland Dreiere727f5c2009-06-22 23:07:56 -0700551 dma_sync_single_for_device(&dev->pdev->dev, dma_handle,
552 npages * sizeof (u64), DMA_TO_DEVICE);
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300553
554 return 0;
Roland Dreier225c7b12007-05-08 18:00:38 -0700555}
556
Eli Cohenc82e9aa2011-12-13 04:15:24 +0000557int __mlx4_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
Jack Morgensteinea51b372011-12-13 04:13:48 +0000558 int start_index, int npages, u64 *page_list)
Roland Dreier225c7b12007-05-08 18:00:38 -0700559{
Jack Morgensteinea51b372011-12-13 04:13:48 +0000560 int err = 0;
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300561 int chunk;
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000562 int mtts_per_page;
563 int max_mtts_first_page;
564
565 /* compute how may mtts fit in the first page */
566 mtts_per_page = PAGE_SIZE / sizeof(u64);
567 max_mtts_first_page = mtts_per_page - (mtt->offset + start_index)
568 % mtts_per_page;
569
570 chunk = min_t(int, max_mtts_first_page, npages);
Roland Dreier225c7b12007-05-08 18:00:38 -0700571
Roland Dreier225c7b12007-05-08 18:00:38 -0700572 while (npages > 0) {
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300573 err = mlx4_write_mtt_chunk(dev, mtt, start_index, chunk, page_list);
Roland Dreier225c7b12007-05-08 18:00:38 -0700574 if (err)
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300575 return err;
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300576 npages -= chunk;
577 start_index += chunk;
578 page_list += chunk;
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000579
580 chunk = min_t(int, mtts_per_page, npages);
Roland Dreier225c7b12007-05-08 18:00:38 -0700581 }
Jack Morgensteinea51b372011-12-13 04:13:48 +0000582 return err;
583}
Roland Dreier225c7b12007-05-08 18:00:38 -0700584
Jack Morgensteinea51b372011-12-13 04:13:48 +0000585int mlx4_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
586 int start_index, int npages, u64 *page_list)
587{
588 struct mlx4_cmd_mailbox *mailbox = NULL;
589 __be64 *inbox = NULL;
590 int chunk;
591 int err = 0;
592 int i;
593
594 if (mtt->order < 0)
595 return -EINVAL;
596
597 if (mlx4_is_mfunc(dev)) {
598 mailbox = mlx4_alloc_cmd_mailbox(dev);
599 if (IS_ERR(mailbox))
600 return PTR_ERR(mailbox);
601 inbox = mailbox->buf;
602
603 while (npages > 0) {
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000604 chunk = min_t(int, MLX4_MAILBOX_SIZE / sizeof(u64) - 2,
605 npages);
606 inbox[0] = cpu_to_be64(mtt->offset + start_index);
Jack Morgensteinea51b372011-12-13 04:13:48 +0000607 inbox[1] = 0;
608 for (i = 0; i < chunk; ++i)
609 inbox[i + 2] = cpu_to_be64(page_list[i] |
610 MLX4_MTT_FLAG_PRESENT);
611 err = mlx4_WRITE_MTT(dev, mailbox, chunk);
612 if (err) {
613 mlx4_free_cmd_mailbox(dev, mailbox);
614 return err;
615 }
616
617 npages -= chunk;
618 start_index += chunk;
619 page_list += chunk;
620 }
621 mlx4_free_cmd_mailbox(dev, mailbox);
622 return err;
623 }
624
625 return __mlx4_write_mtt(dev, mtt, start_index, npages, page_list);
Roland Dreier225c7b12007-05-08 18:00:38 -0700626}
627EXPORT_SYMBOL_GPL(mlx4_write_mtt);
628
629int mlx4_buf_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
630 struct mlx4_buf *buf)
631{
632 u64 *page_list;
633 int err;
634 int i;
635
636 page_list = kmalloc(buf->npages * sizeof *page_list, GFP_KERNEL);
637 if (!page_list)
638 return -ENOMEM;
639
640 for (i = 0; i < buf->npages; ++i)
641 if (buf->nbufs == 1)
Roland Dreierb57aacf2008-02-06 21:17:59 -0800642 page_list[i] = buf->direct.map + (i << buf->page_shift);
Roland Dreier225c7b12007-05-08 18:00:38 -0700643 else
Roland Dreierb57aacf2008-02-06 21:17:59 -0800644 page_list[i] = buf->page_list[i].map;
Roland Dreier225c7b12007-05-08 18:00:38 -0700645
646 err = mlx4_write_mtt(dev, mtt, 0, buf->npages, page_list);
647
648 kfree(page_list);
649 return err;
650}
651EXPORT_SYMBOL_GPL(mlx4_buf_write_mtt);
652
Roland Dreier3d73c282007-10-10 15:43:54 -0700653int mlx4_init_mr_table(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -0700654{
Jack Morgensteinea51b372011-12-13 04:13:48 +0000655 struct mlx4_priv *priv = mlx4_priv(dev);
656 struct mlx4_mr_table *mr_table = &priv->mr_table;
Roland Dreier225c7b12007-05-08 18:00:38 -0700657 int err;
658
Jack Morgensteinea51b372011-12-13 04:13:48 +0000659 if (!is_power_of_2(dev->caps.num_mpts))
660 return -EINVAL;
661
662 /* Nothing to do for slaves - all MR handling is forwarded
663 * to the master */
664 if (mlx4_is_slave(dev))
665 return 0;
666
Roland Dreier225c7b12007-05-08 18:00:38 -0700667 err = mlx4_bitmap_init(&mr_table->mpt_bitmap, dev->caps.num_mpts,
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700668 ~0, dev->caps.reserved_mrws, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700669 if (err)
670 return err;
671
672 err = mlx4_buddy_init(&mr_table->mtt_buddy,
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000673 ilog2(dev->caps.num_mtts /
674 (1 << log_mtts_per_seg)));
Roland Dreier225c7b12007-05-08 18:00:38 -0700675 if (err)
676 goto err_buddy;
677
678 if (dev->caps.reserved_mtts) {
Jack Morgensteinea51b372011-12-13 04:13:48 +0000679 priv->reserved_mtts =
680 mlx4_alloc_mtt_range(dev,
681 fls(dev->caps.reserved_mtts - 1));
682 if (priv->reserved_mtts < 0) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700683 mlx4_warn(dev, "MTT table of order %d is too small.\n",
684 mr_table->mtt_buddy.max_order);
685 err = -ENOMEM;
686 goto err_reserve_mtts;
687 }
688 }
689
690 return 0;
691
692err_reserve_mtts:
693 mlx4_buddy_cleanup(&mr_table->mtt_buddy);
694
695err_buddy:
696 mlx4_bitmap_cleanup(&mr_table->mpt_bitmap);
697
698 return err;
699}
700
701void mlx4_cleanup_mr_table(struct mlx4_dev *dev)
702{
Jack Morgensteinea51b372011-12-13 04:13:48 +0000703 struct mlx4_priv *priv = mlx4_priv(dev);
704 struct mlx4_mr_table *mr_table = &priv->mr_table;
Roland Dreier225c7b12007-05-08 18:00:38 -0700705
Jack Morgensteinea51b372011-12-13 04:13:48 +0000706 if (mlx4_is_slave(dev))
707 return;
708 if (priv->reserved_mtts >= 0)
709 mlx4_free_mtt_range(dev, priv->reserved_mtts,
710 fls(dev->caps.reserved_mtts - 1));
Roland Dreier225c7b12007-05-08 18:00:38 -0700711 mlx4_buddy_cleanup(&mr_table->mtt_buddy);
712 mlx4_bitmap_cleanup(&mr_table->mpt_bitmap);
713}
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300714
715static inline int mlx4_check_fmr(struct mlx4_fmr *fmr, u64 *page_list,
716 int npages, u64 iova)
717{
718 int i, page_mask;
719
720 if (npages > fmr->max_pages)
721 return -EINVAL;
722
723 page_mask = (1 << fmr->page_shift) - 1;
724
725 /* We are getting page lists, so va must be page aligned. */
726 if (iova & page_mask)
727 return -EINVAL;
728
729 /* Trust the user not to pass misaligned data in page_list */
730 if (0)
731 for (i = 0; i < npages; ++i) {
732 if (page_list[i] & ~page_mask)
733 return -EINVAL;
734 }
735
736 if (fmr->maps >= fmr->max_maps)
737 return -EINVAL;
738
739 return 0;
740}
741
742int mlx4_map_phys_fmr(struct mlx4_dev *dev, struct mlx4_fmr *fmr, u64 *page_list,
743 int npages, u64 iova, u32 *lkey, u32 *rkey)
744{
745 u32 key;
746 int i, err;
747
748 err = mlx4_check_fmr(fmr, page_list, npages, iova);
749 if (err)
750 return err;
751
752 ++fmr->maps;
753
754 key = key_to_hw_index(fmr->mr.key);
755 key += dev->caps.num_mpts;
756 *lkey = *rkey = fmr->mr.key = hw_index_to_key(key);
757
758 *(u8 *) fmr->mpt = MLX4_MPT_STATUS_SW;
759
760 /* Make sure MPT status is visible before writing MTT entries */
761 wmb();
762
Roland Dreiere727f5c2009-06-22 23:07:56 -0700763 dma_sync_single_for_cpu(&dev->pdev->dev, fmr->dma_handle,
764 npages * sizeof(u64), DMA_TO_DEVICE);
765
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300766 for (i = 0; i < npages; ++i)
767 fmr->mtts[i] = cpu_to_be64(page_list[i] | MLX4_MTT_FLAG_PRESENT);
768
Roland Dreiere727f5c2009-06-22 23:07:56 -0700769 dma_sync_single_for_device(&dev->pdev->dev, fmr->dma_handle,
770 npages * sizeof(u64), DMA_TO_DEVICE);
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300771
772 fmr->mpt->key = cpu_to_be32(key);
773 fmr->mpt->lkey = cpu_to_be32(key);
774 fmr->mpt->length = cpu_to_be64(npages * (1ull << fmr->page_shift));
775 fmr->mpt->start = cpu_to_be64(iova);
776
777 /* Make MTT entries are visible before setting MPT status */
778 wmb();
779
780 *(u8 *) fmr->mpt = MLX4_MPT_STATUS_HW;
781
782 /* Make sure MPT status is visible before consumer can use FMR */
783 wmb();
784
785 return 0;
786}
787EXPORT_SYMBOL_GPL(mlx4_map_phys_fmr);
788
789int mlx4_fmr_alloc(struct mlx4_dev *dev, u32 pd, u32 access, int max_pages,
790 int max_maps, u8 page_shift, struct mlx4_fmr *fmr)
791{
792 struct mlx4_priv *priv = mlx4_priv(dev);
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000793 u64 mtt_offset;
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300794 int err = -ENOMEM;
795
Oren Duerc5057dd2008-05-05 15:56:52 -0700796 if (page_shift < (ffs(dev->caps.page_size_cap) - 1) || page_shift >= 32)
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300797 return -EINVAL;
798
799 /* All MTTs must fit in the same page */
800 if (max_pages * sizeof *fmr->mtts > PAGE_SIZE)
801 return -EINVAL;
802
803 fmr->page_shift = page_shift;
804 fmr->max_pages = max_pages;
805 fmr->max_maps = max_maps;
806 fmr->maps = 0;
807
808 err = mlx4_mr_alloc(dev, pd, 0, 0, access, max_pages,
809 page_shift, &fmr->mr);
810 if (err)
811 return err;
812
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000813 mtt_offset = fmr->mr.mtt.offset * dev->caps.mtt_entry_sz;
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300814
815 fmr->mtts = mlx4_table_find(&priv->mr_table.mtt_table,
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000816 fmr->mr.mtt.offset,
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300817 &fmr->dma_handle);
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000818
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300819 if (!fmr->mtts) {
820 err = -ENOMEM;
821 goto err_free;
822 }
823
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300824 return 0;
825
826err_free:
827 mlx4_mr_free(dev, &fmr->mr);
828 return err;
829}
830EXPORT_SYMBOL_GPL(mlx4_fmr_alloc);
831
832int mlx4_fmr_enable(struct mlx4_dev *dev, struct mlx4_fmr *fmr)
833{
Jack Morgenstein11e75a72008-02-14 13:41:29 +0200834 struct mlx4_priv *priv = mlx4_priv(dev);
835 int err;
836
837 err = mlx4_mr_enable(dev, &fmr->mr);
838 if (err)
839 return err;
840
841 fmr->mpt = mlx4_table_find(&priv->mr_table.dmpt_table,
842 key_to_hw_index(fmr->mr.key), NULL);
843 if (!fmr->mpt)
844 return -ENOMEM;
845
846 return 0;
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300847}
848EXPORT_SYMBOL_GPL(mlx4_fmr_enable);
849
850void mlx4_fmr_unmap(struct mlx4_dev *dev, struct mlx4_fmr *fmr,
851 u32 *lkey, u32 *rkey)
852{
Jack Morgensteinea51b372011-12-13 04:13:48 +0000853 struct mlx4_cmd_mailbox *mailbox;
854 int err;
855
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300856 if (!fmr->maps)
857 return;
858
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300859 fmr->maps = 0;
860
Jack Morgensteinea51b372011-12-13 04:13:48 +0000861 mailbox = mlx4_alloc_cmd_mailbox(dev);
862 if (IS_ERR(mailbox)) {
863 err = PTR_ERR(mailbox);
864 printk(KERN_WARNING "mlx4_ib: mlx4_alloc_cmd_mailbox"
865 " failed (%d)\n", err);
866 return;
867 }
868
869 err = mlx4_HW2SW_MPT(dev, NULL,
870 key_to_hw_index(fmr->mr.key) &
871 (dev->caps.num_mpts - 1));
872 mlx4_free_cmd_mailbox(dev, mailbox);
873 if (err) {
874 printk(KERN_WARNING "mlx4_ib: mlx4_HW2SW_MPT failed (%d)\n",
875 err);
876 return;
877 }
878 fmr->mr.enabled = MLX4_MR_EN_SW;
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300879}
880EXPORT_SYMBOL_GPL(mlx4_fmr_unmap);
881
882int mlx4_fmr_free(struct mlx4_dev *dev, struct mlx4_fmr *fmr)
883{
884 if (fmr->maps)
885 return -EBUSY;
886
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300887 mlx4_mr_free(dev, &fmr->mr);
Jack Morgensteinea51b372011-12-13 04:13:48 +0000888 fmr->mr.enabled = MLX4_MR_DISABLED;
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300889
890 return 0;
891}
892EXPORT_SYMBOL_GPL(mlx4_fmr_free);
893
894int mlx4_SYNC_TPT(struct mlx4_dev *dev)
895{
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000896 return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_SYNC_TPT, 1000,
897 MLX4_CMD_WRAPPED);
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300898}
899EXPORT_SYMBOL_GPL(mlx4_SYNC_TPT);