blob: b3ea93b986892d02978e97cac5d9c4b988a3759d [file] [log] [blame]
Roland Dreier225c7b12007-05-08 18:00:38 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
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
35#include <linux/init.h>
36#include <linux/errno.h>
37
38#include <linux/mlx4/cmd.h>
39
40#include "mlx4.h"
41#include "icm.h"
42
43/*
44 * Must be packed because mtt_seg is 64 bits but only aligned to 32 bits.
45 */
46struct mlx4_mpt_entry {
47 __be32 flags;
48 __be32 qpn;
49 __be32 key;
50 __be32 pd;
51 __be64 start;
52 __be64 length;
53 __be32 lkey;
54 __be32 win_cnt;
55 u8 reserved1[3];
56 u8 mtt_rep;
57 __be64 mtt_seg;
58 __be32 mtt_sz;
59 __be32 entity_size;
60 __be32 first_byte_offset;
61} __attribute__((packed));
62
63#define MLX4_MPT_FLAG_SW_OWNS (0xfUL << 28)
64#define MLX4_MPT_FLAG_MIO (1 << 17)
65#define MLX4_MPT_FLAG_BIND_ENABLE (1 << 15)
66#define MLX4_MPT_FLAG_PHYSICAL (1 << 9)
67#define MLX4_MPT_FLAG_REGION (1 << 8)
68
69#define MLX4_MTT_FLAG_PRESENT 1
70
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +030071#define MLX4_MPT_STATUS_SW 0xF0
72#define MLX4_MPT_STATUS_HW 0x00
73
Roland Dreier225c7b12007-05-08 18:00:38 -070074static u32 mlx4_buddy_alloc(struct mlx4_buddy *buddy, int order)
75{
76 int o;
77 int m;
78 u32 seg;
79
80 spin_lock(&buddy->lock);
81
Roland Dreiere4044cf2008-07-22 14:19:40 -070082 for (o = order; o <= buddy->max_order; ++o)
83 if (buddy->num_free[o]) {
84 m = 1 << (buddy->max_order - o);
85 seg = find_first_bit(buddy->bits[o], m);
86 if (seg < m)
87 goto found;
88 }
Roland Dreier225c7b12007-05-08 18:00:38 -070089
90 spin_unlock(&buddy->lock);
91 return -1;
92
93 found:
94 clear_bit(seg, buddy->bits[o]);
Roland Dreiere4044cf2008-07-22 14:19:40 -070095 --buddy->num_free[o];
Roland Dreier225c7b12007-05-08 18:00:38 -070096
97 while (o > order) {
98 --o;
99 seg <<= 1;
100 set_bit(seg ^ 1, buddy->bits[o]);
Roland Dreiere4044cf2008-07-22 14:19:40 -0700101 ++buddy->num_free[o];
Roland Dreier225c7b12007-05-08 18:00:38 -0700102 }
103
104 spin_unlock(&buddy->lock);
105
106 seg <<= order;
107
108 return seg;
109}
110
111static void mlx4_buddy_free(struct mlx4_buddy *buddy, u32 seg, int order)
112{
113 seg >>= order;
114
115 spin_lock(&buddy->lock);
116
117 while (test_bit(seg ^ 1, buddy->bits[order])) {
118 clear_bit(seg ^ 1, buddy->bits[order]);
Roland Dreiere4044cf2008-07-22 14:19:40 -0700119 --buddy->num_free[order];
Roland Dreier225c7b12007-05-08 18:00:38 -0700120 seg >>= 1;
121 ++order;
122 }
123
124 set_bit(seg, buddy->bits[order]);
Roland Dreiere4044cf2008-07-22 14:19:40 -0700125 ++buddy->num_free[order];
Roland Dreier225c7b12007-05-08 18:00:38 -0700126
127 spin_unlock(&buddy->lock);
128}
129
Roland Dreiere8f9b2e2008-02-04 20:20:41 -0800130static int mlx4_buddy_init(struct mlx4_buddy *buddy, int max_order)
Roland Dreier225c7b12007-05-08 18:00:38 -0700131{
132 int i, s;
133
134 buddy->max_order = max_order;
135 spin_lock_init(&buddy->lock);
136
137 buddy->bits = kzalloc((buddy->max_order + 1) * sizeof (long *),
138 GFP_KERNEL);
Roland Dreiere4044cf2008-07-22 14:19:40 -0700139 buddy->num_free = kzalloc((buddy->max_order + 1) * sizeof (int *),
140 GFP_KERNEL);
141 if (!buddy->bits || !buddy->num_free)
Roland Dreier225c7b12007-05-08 18:00:38 -0700142 goto err_out;
143
144 for (i = 0; i <= buddy->max_order; ++i) {
145 s = BITS_TO_LONGS(1 << (buddy->max_order - i));
146 buddy->bits[i] = kmalloc(s * sizeof (long), GFP_KERNEL);
147 if (!buddy->bits[i])
148 goto err_out_free;
149 bitmap_zero(buddy->bits[i], 1 << (buddy->max_order - i));
150 }
151
152 set_bit(0, buddy->bits[buddy->max_order]);
Roland Dreiere4044cf2008-07-22 14:19:40 -0700153 buddy->num_free[buddy->max_order] = 1;
Roland Dreier225c7b12007-05-08 18:00:38 -0700154
155 return 0;
156
157err_out_free:
158 for (i = 0; i <= buddy->max_order; ++i)
159 kfree(buddy->bits[i]);
160
Roland Dreier225c7b12007-05-08 18:00:38 -0700161err_out:
Roland Dreiere4044cf2008-07-22 14:19:40 -0700162 kfree(buddy->bits);
163 kfree(buddy->num_free);
164
Roland Dreier225c7b12007-05-08 18:00:38 -0700165 return -ENOMEM;
166}
167
168static void mlx4_buddy_cleanup(struct mlx4_buddy *buddy)
169{
170 int i;
171
172 for (i = 0; i <= buddy->max_order; ++i)
173 kfree(buddy->bits[i]);
174
175 kfree(buddy->bits);
Roland Dreiere4044cf2008-07-22 14:19:40 -0700176 kfree(buddy->num_free);
Roland Dreier225c7b12007-05-08 18:00:38 -0700177}
178
179static u32 mlx4_alloc_mtt_range(struct mlx4_dev *dev, int order)
180{
181 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
182 u32 seg;
183
184 seg = mlx4_buddy_alloc(&mr_table->mtt_buddy, order);
185 if (seg == -1)
186 return -1;
187
188 if (mlx4_table_get_range(dev, &mr_table->mtt_table, seg,
189 seg + (1 << order) - 1)) {
190 mlx4_buddy_free(&mr_table->mtt_buddy, seg, order);
191 return -1;
192 }
193
194 return seg;
195}
196
197int 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
209 for (mtt->order = 0, i = MLX4_MTT_ENTRY_PER_SEG; i < npages; i <<= 1)
210 ++mtt->order;
211
212 mtt->first_seg = mlx4_alloc_mtt_range(dev, mtt->order);
213 if (mtt->first_seg == -1)
214 return -ENOMEM;
215
216 return 0;
217}
218EXPORT_SYMBOL_GPL(mlx4_mtt_init);
219
220void mlx4_mtt_cleanup(struct mlx4_dev *dev, struct mlx4_mtt *mtt)
221{
222 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
223
224 if (mtt->order < 0)
225 return;
226
227 mlx4_buddy_free(&mr_table->mtt_buddy, mtt->first_seg, mtt->order);
228 mlx4_table_put_range(dev, &mr_table->mtt_table, mtt->first_seg,
229 mtt->first_seg + (1 << mtt->order) - 1);
230}
231EXPORT_SYMBOL_GPL(mlx4_mtt_cleanup);
232
233u64 mlx4_mtt_addr(struct mlx4_dev *dev, struct mlx4_mtt *mtt)
234{
235 return (u64) mtt->first_seg * dev->caps.mtt_entry_sz;
236}
237EXPORT_SYMBOL_GPL(mlx4_mtt_addr);
238
239static u32 hw_index_to_key(u32 ind)
240{
241 return (ind >> 24) | (ind << 8);
242}
243
244static u32 key_to_hw_index(u32 key)
245{
246 return (key << 24) | (key >> 8);
247}
248
249static int mlx4_SW2HW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
250 int mpt_index)
251{
252 return mlx4_cmd(dev, mailbox->dma, mpt_index, 0, MLX4_CMD_SW2HW_MPT,
253 MLX4_CMD_TIME_CLASS_B);
254}
255
256static int mlx4_HW2SW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
257 int mpt_index)
258{
259 return mlx4_cmd_box(dev, 0, mailbox ? mailbox->dma : 0, mpt_index,
260 !mailbox, MLX4_CMD_HW2SW_MPT, MLX4_CMD_TIME_CLASS_B);
261}
262
263int mlx4_mr_alloc(struct mlx4_dev *dev, u32 pd, u64 iova, u64 size, u32 access,
264 int npages, int page_shift, struct mlx4_mr *mr)
265{
266 struct mlx4_priv *priv = mlx4_priv(dev);
267 u32 index;
268 int err;
269
270 index = mlx4_bitmap_alloc(&priv->mr_table.mpt_bitmap);
Jack Morgenstein0172e2e2007-07-26 11:16:58 +0300271 if (index == -1)
272 return -ENOMEM;
Roland Dreier225c7b12007-05-08 18:00:38 -0700273
274 mr->iova = iova;
275 mr->size = size;
276 mr->pd = pd;
277 mr->access = access;
278 mr->enabled = 0;
279 mr->key = hw_index_to_key(index);
280
281 err = mlx4_mtt_init(dev, npages, page_shift, &mr->mtt);
282 if (err)
Jack Morgenstein0172e2e2007-07-26 11:16:58 +0300283 mlx4_bitmap_free(&priv->mr_table.mpt_bitmap, index);
Roland Dreier225c7b12007-05-08 18:00:38 -0700284
Roland Dreier225c7b12007-05-08 18:00:38 -0700285 return err;
286}
287EXPORT_SYMBOL_GPL(mlx4_mr_alloc);
288
289void mlx4_mr_free(struct mlx4_dev *dev, struct mlx4_mr *mr)
290{
291 struct mlx4_priv *priv = mlx4_priv(dev);
292 int err;
293
294 if (mr->enabled) {
295 err = mlx4_HW2SW_MPT(dev, NULL,
296 key_to_hw_index(mr->key) &
297 (dev->caps.num_mpts - 1));
298 if (err)
299 mlx4_warn(dev, "HW2SW_MPT failed (%d)\n", err);
300 }
301
302 mlx4_mtt_cleanup(dev, &mr->mtt);
303 mlx4_bitmap_free(&priv->mr_table.mpt_bitmap, key_to_hw_index(mr->key));
304}
305EXPORT_SYMBOL_GPL(mlx4_mr_free);
306
307int mlx4_mr_enable(struct mlx4_dev *dev, struct mlx4_mr *mr)
308{
309 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
310 struct mlx4_cmd_mailbox *mailbox;
311 struct mlx4_mpt_entry *mpt_entry;
312 int err;
313
314 err = mlx4_table_get(dev, &mr_table->dmpt_table, key_to_hw_index(mr->key));
315 if (err)
316 return err;
317
318 mailbox = mlx4_alloc_cmd_mailbox(dev);
319 if (IS_ERR(mailbox)) {
320 err = PTR_ERR(mailbox);
321 goto err_table;
322 }
323 mpt_entry = mailbox->buf;
324
325 memset(mpt_entry, 0, sizeof *mpt_entry);
326
327 mpt_entry->flags = cpu_to_be32(MLX4_MPT_FLAG_SW_OWNS |
328 MLX4_MPT_FLAG_MIO |
329 MLX4_MPT_FLAG_REGION |
330 mr->access);
Roland Dreier225c7b12007-05-08 18:00:38 -0700331
332 mpt_entry->key = cpu_to_be32(key_to_hw_index(mr->key));
333 mpt_entry->pd = cpu_to_be32(mr->pd);
334 mpt_entry->start = cpu_to_be64(mr->iova);
335 mpt_entry->length = cpu_to_be64(mr->size);
336 mpt_entry->entity_size = cpu_to_be32(mr->mtt.page_shift);
Jack Morgensteinb2d93082007-06-07 23:24:38 -0700337 if (mr->mtt.order < 0) {
338 mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_PHYSICAL);
339 mpt_entry->mtt_seg = 0;
340 } else
341 mpt_entry->mtt_seg = cpu_to_be64(mlx4_mtt_addr(dev, &mr->mtt));
Roland Dreier225c7b12007-05-08 18:00:38 -0700342
343 err = mlx4_SW2HW_MPT(dev, mailbox,
344 key_to_hw_index(mr->key) & (dev->caps.num_mpts - 1));
345 if (err) {
346 mlx4_warn(dev, "SW2HW_MPT failed (%d)\n", err);
347 goto err_cmd;
348 }
349
350 mr->enabled = 1;
351
352 mlx4_free_cmd_mailbox(dev, mailbox);
353
354 return 0;
355
356err_cmd:
357 mlx4_free_cmd_mailbox(dev, mailbox);
358
359err_table:
360 mlx4_table_put(dev, &mr_table->dmpt_table, key_to_hw_index(mr->key));
361 return err;
362}
363EXPORT_SYMBOL_GPL(mlx4_mr_enable);
364
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300365static int mlx4_write_mtt_chunk(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
366 int start_index, int npages, u64 *page_list)
Roland Dreier225c7b12007-05-08 18:00:38 -0700367{
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300368 struct mlx4_priv *priv = mlx4_priv(dev);
369 __be64 *mtts;
370 dma_addr_t dma_handle;
371 int i;
372 int s = start_index * sizeof (u64);
373
374 /* All MTTs must fit in the same page */
375 if (start_index / (PAGE_SIZE / sizeof (u64)) !=
376 (start_index + npages - 1) / (PAGE_SIZE / sizeof (u64)))
377 return -EINVAL;
378
379 if (start_index & (MLX4_MTT_ENTRY_PER_SEG - 1))
380 return -EINVAL;
381
382 mtts = mlx4_table_find(&priv->mr_table.mtt_table, mtt->first_seg +
383 s / dev->caps.mtt_entry_sz, &dma_handle);
384 if (!mtts)
385 return -ENOMEM;
386
387 for (i = 0; i < npages; ++i)
388 mtts[i] = cpu_to_be64(page_list[i] | MLX4_MTT_FLAG_PRESENT);
389
390 dma_sync_single(&dev->pdev->dev, dma_handle, npages * sizeof (u64), DMA_TO_DEVICE);
391
392 return 0;
Roland Dreier225c7b12007-05-08 18:00:38 -0700393}
394
395int mlx4_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
396 int start_index, int npages, u64 *page_list)
397{
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300398 int chunk;
399 int err;
Roland Dreier225c7b12007-05-08 18:00:38 -0700400
401 if (mtt->order < 0)
402 return -EINVAL;
403
Roland Dreier225c7b12007-05-08 18:00:38 -0700404 while (npages > 0) {
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300405 chunk = min_t(int, PAGE_SIZE / sizeof(u64), npages);
406 err = mlx4_write_mtt_chunk(dev, mtt, start_index, chunk, page_list);
Roland Dreier225c7b12007-05-08 18:00:38 -0700407 if (err)
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300408 return err;
Roland Dreier225c7b12007-05-08 18:00:38 -0700409
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300410 npages -= chunk;
411 start_index += chunk;
412 page_list += chunk;
Roland Dreier225c7b12007-05-08 18:00:38 -0700413 }
414
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300415 return 0;
Roland Dreier225c7b12007-05-08 18:00:38 -0700416}
417EXPORT_SYMBOL_GPL(mlx4_write_mtt);
418
419int mlx4_buf_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
420 struct mlx4_buf *buf)
421{
422 u64 *page_list;
423 int err;
424 int i;
425
426 page_list = kmalloc(buf->npages * sizeof *page_list, GFP_KERNEL);
427 if (!page_list)
428 return -ENOMEM;
429
430 for (i = 0; i < buf->npages; ++i)
431 if (buf->nbufs == 1)
Roland Dreierb57aacf2008-02-06 21:17:59 -0800432 page_list[i] = buf->direct.map + (i << buf->page_shift);
Roland Dreier225c7b12007-05-08 18:00:38 -0700433 else
Roland Dreierb57aacf2008-02-06 21:17:59 -0800434 page_list[i] = buf->page_list[i].map;
Roland Dreier225c7b12007-05-08 18:00:38 -0700435
436 err = mlx4_write_mtt(dev, mtt, 0, buf->npages, page_list);
437
438 kfree(page_list);
439 return err;
440}
441EXPORT_SYMBOL_GPL(mlx4_buf_write_mtt);
442
Roland Dreier3d73c282007-10-10 15:43:54 -0700443int mlx4_init_mr_table(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -0700444{
445 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
446 int err;
447
448 err = mlx4_bitmap_init(&mr_table->mpt_bitmap, dev->caps.num_mpts,
449 ~0, dev->caps.reserved_mrws);
450 if (err)
451 return err;
452
453 err = mlx4_buddy_init(&mr_table->mtt_buddy,
454 ilog2(dev->caps.num_mtt_segs));
455 if (err)
456 goto err_buddy;
457
458 if (dev->caps.reserved_mtts) {
Roland Dreiercf782372007-10-09 19:59:16 -0700459 if (mlx4_alloc_mtt_range(dev, fls(dev->caps.reserved_mtts - 1)) == -1) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700460 mlx4_warn(dev, "MTT table of order %d is too small.\n",
461 mr_table->mtt_buddy.max_order);
462 err = -ENOMEM;
463 goto err_reserve_mtts;
464 }
465 }
466
467 return 0;
468
469err_reserve_mtts:
470 mlx4_buddy_cleanup(&mr_table->mtt_buddy);
471
472err_buddy:
473 mlx4_bitmap_cleanup(&mr_table->mpt_bitmap);
474
475 return err;
476}
477
478void mlx4_cleanup_mr_table(struct mlx4_dev *dev)
479{
480 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
481
482 mlx4_buddy_cleanup(&mr_table->mtt_buddy);
483 mlx4_bitmap_cleanup(&mr_table->mpt_bitmap);
484}
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300485
486static inline int mlx4_check_fmr(struct mlx4_fmr *fmr, u64 *page_list,
487 int npages, u64 iova)
488{
489 int i, page_mask;
490
491 if (npages > fmr->max_pages)
492 return -EINVAL;
493
494 page_mask = (1 << fmr->page_shift) - 1;
495
496 /* We are getting page lists, so va must be page aligned. */
497 if (iova & page_mask)
498 return -EINVAL;
499
500 /* Trust the user not to pass misaligned data in page_list */
501 if (0)
502 for (i = 0; i < npages; ++i) {
503 if (page_list[i] & ~page_mask)
504 return -EINVAL;
505 }
506
507 if (fmr->maps >= fmr->max_maps)
508 return -EINVAL;
509
510 return 0;
511}
512
513int mlx4_map_phys_fmr(struct mlx4_dev *dev, struct mlx4_fmr *fmr, u64 *page_list,
514 int npages, u64 iova, u32 *lkey, u32 *rkey)
515{
516 u32 key;
517 int i, err;
518
519 err = mlx4_check_fmr(fmr, page_list, npages, iova);
520 if (err)
521 return err;
522
523 ++fmr->maps;
524
525 key = key_to_hw_index(fmr->mr.key);
526 key += dev->caps.num_mpts;
527 *lkey = *rkey = fmr->mr.key = hw_index_to_key(key);
528
529 *(u8 *) fmr->mpt = MLX4_MPT_STATUS_SW;
530
531 /* Make sure MPT status is visible before writing MTT entries */
532 wmb();
533
534 for (i = 0; i < npages; ++i)
535 fmr->mtts[i] = cpu_to_be64(page_list[i] | MLX4_MTT_FLAG_PRESENT);
536
537 dma_sync_single(&dev->pdev->dev, fmr->dma_handle,
538 npages * sizeof(u64), DMA_TO_DEVICE);
539
540 fmr->mpt->key = cpu_to_be32(key);
541 fmr->mpt->lkey = cpu_to_be32(key);
542 fmr->mpt->length = cpu_to_be64(npages * (1ull << fmr->page_shift));
543 fmr->mpt->start = cpu_to_be64(iova);
544
545 /* Make MTT entries are visible before setting MPT status */
546 wmb();
547
548 *(u8 *) fmr->mpt = MLX4_MPT_STATUS_HW;
549
550 /* Make sure MPT status is visible before consumer can use FMR */
551 wmb();
552
553 return 0;
554}
555EXPORT_SYMBOL_GPL(mlx4_map_phys_fmr);
556
557int mlx4_fmr_alloc(struct mlx4_dev *dev, u32 pd, u32 access, int max_pages,
558 int max_maps, u8 page_shift, struct mlx4_fmr *fmr)
559{
560 struct mlx4_priv *priv = mlx4_priv(dev);
561 u64 mtt_seg;
562 int err = -ENOMEM;
563
Oren Duerc5057dd2008-05-05 15:56:52 -0700564 if (page_shift < (ffs(dev->caps.page_size_cap) - 1) || page_shift >= 32)
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300565 return -EINVAL;
566
567 /* All MTTs must fit in the same page */
568 if (max_pages * sizeof *fmr->mtts > PAGE_SIZE)
569 return -EINVAL;
570
571 fmr->page_shift = page_shift;
572 fmr->max_pages = max_pages;
573 fmr->max_maps = max_maps;
574 fmr->maps = 0;
575
576 err = mlx4_mr_alloc(dev, pd, 0, 0, access, max_pages,
577 page_shift, &fmr->mr);
578 if (err)
579 return err;
580
581 mtt_seg = fmr->mr.mtt.first_seg * dev->caps.mtt_entry_sz;
582
583 fmr->mtts = mlx4_table_find(&priv->mr_table.mtt_table,
584 fmr->mr.mtt.first_seg,
585 &fmr->dma_handle);
586 if (!fmr->mtts) {
587 err = -ENOMEM;
588 goto err_free;
589 }
590
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300591 return 0;
592
593err_free:
594 mlx4_mr_free(dev, &fmr->mr);
595 return err;
596}
597EXPORT_SYMBOL_GPL(mlx4_fmr_alloc);
598
599int mlx4_fmr_enable(struct mlx4_dev *dev, struct mlx4_fmr *fmr)
600{
Jack Morgenstein11e75a72008-02-14 13:41:29 +0200601 struct mlx4_priv *priv = mlx4_priv(dev);
602 int err;
603
604 err = mlx4_mr_enable(dev, &fmr->mr);
605 if (err)
606 return err;
607
608 fmr->mpt = mlx4_table_find(&priv->mr_table.dmpt_table,
609 key_to_hw_index(fmr->mr.key), NULL);
610 if (!fmr->mpt)
611 return -ENOMEM;
612
613 return 0;
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300614}
615EXPORT_SYMBOL_GPL(mlx4_fmr_enable);
616
617void mlx4_fmr_unmap(struct mlx4_dev *dev, struct mlx4_fmr *fmr,
618 u32 *lkey, u32 *rkey)
619{
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300620 if (!fmr->maps)
621 return;
622
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300623 fmr->maps = 0;
624
625 *(u8 *) fmr->mpt = MLX4_MPT_STATUS_SW;
626}
627EXPORT_SYMBOL_GPL(mlx4_fmr_unmap);
628
629int mlx4_fmr_free(struct mlx4_dev *dev, struct mlx4_fmr *fmr)
630{
631 if (fmr->maps)
632 return -EBUSY;
633
634 fmr->mr.enabled = 0;
635 mlx4_mr_free(dev, &fmr->mr);
636
637 return 0;
638}
639EXPORT_SYMBOL_GPL(mlx4_fmr_free);
640
641int mlx4_SYNC_TPT(struct mlx4_dev *dev)
642{
643 return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_SYNC_TPT, 1000);
644}
645EXPORT_SYMBOL_GPL(mlx4_SYNC_TPT);