2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * $Id: mthca_mr.c 1349 2004-12-16 21:09:43Z roland $
36 #include <linux/slab.h>
37 #include <linux/errno.h>
39 #include "mthca_dev.h"
40 #include "mthca_cmd.h"
41 #include "mthca_memfree.h"
44 struct mthca_buddy *buddy;
50 * Must be packed because mtt_seg is 64 bits but only aligned to 32 bits.
52 struct mthca_mpt_entry {
61 __be32 window_count_limit;
63 __be32 mtt_sz; /* Arbel only */
65 } __attribute__((packed));
67 #define MTHCA_MPT_FLAG_SW_OWNS (0xfUL << 28)
68 #define MTHCA_MPT_FLAG_MIO (1 << 17)
69 #define MTHCA_MPT_FLAG_BIND_ENABLE (1 << 15)
70 #define MTHCA_MPT_FLAG_PHYSICAL (1 << 9)
71 #define MTHCA_MPT_FLAG_REGION (1 << 8)
73 #define MTHCA_MTT_FLAG_PRESENT 1
75 #define MTHCA_MPT_STATUS_SW 0xF0
76 #define MTHCA_MPT_STATUS_HW 0x00
78 #define SINAI_FMR_KEY_INC 0x1000000
81 * Buddy allocator for MTT segments (currently not very efficient
82 * since it doesn't keep a free list and just searches linearly
83 * through the bitmaps)
86 static u32 mthca_buddy_alloc(struct mthca_buddy *buddy, int order)
92 spin_lock(&buddy->lock);
94 for (o = order; o <= buddy->max_order; ++o) {
95 m = 1 << (buddy->max_order - o);
96 seg = find_first_bit(buddy->bits[o], m);
101 spin_unlock(&buddy->lock);
105 clear_bit(seg, buddy->bits[o]);
110 set_bit(seg ^ 1, buddy->bits[o]);
113 spin_unlock(&buddy->lock);
120 static void mthca_buddy_free(struct mthca_buddy *buddy, u32 seg, int order)
124 spin_lock(&buddy->lock);
126 while (test_bit(seg ^ 1, buddy->bits[order])) {
127 clear_bit(seg ^ 1, buddy->bits[order]);
132 set_bit(seg, buddy->bits[order]);
134 spin_unlock(&buddy->lock);
137 static int mthca_buddy_init(struct mthca_buddy *buddy, int max_order)
141 buddy->max_order = max_order;
142 spin_lock_init(&buddy->lock);
144 buddy->bits = kzalloc((buddy->max_order + 1) * sizeof (long *),
149 for (i = 0; i <= buddy->max_order; ++i) {
150 s = BITS_TO_LONGS(1 << (buddy->max_order - i));
151 buddy->bits[i] = kmalloc(s * sizeof (long), GFP_KERNEL);
154 bitmap_zero(buddy->bits[i],
155 1 << (buddy->max_order - i));
158 set_bit(0, buddy->bits[buddy->max_order]);
163 for (i = 0; i <= buddy->max_order; ++i)
164 kfree(buddy->bits[i]);
172 static void mthca_buddy_cleanup(struct mthca_buddy *buddy)
176 for (i = 0; i <= buddy->max_order; ++i)
177 kfree(buddy->bits[i]);
182 static u32 mthca_alloc_mtt_range(struct mthca_dev *dev, int order,
183 struct mthca_buddy *buddy)
185 u32 seg = mthca_buddy_alloc(buddy, order);
190 if (mthca_is_memfree(dev))
191 if (mthca_table_get_range(dev, dev->mr_table.mtt_table, seg,
192 seg + (1 << order) - 1)) {
193 mthca_buddy_free(buddy, seg, order);
200 static struct mthca_mtt *__mthca_alloc_mtt(struct mthca_dev *dev, int size,
201 struct mthca_buddy *buddy)
203 struct mthca_mtt *mtt;
207 return ERR_PTR(-EINVAL);
209 mtt = kmalloc(sizeof *mtt, GFP_KERNEL);
211 return ERR_PTR(-ENOMEM);
215 for (i = MTHCA_MTT_SEG_SIZE / 8; i < size; i <<= 1)
218 mtt->first_seg = mthca_alloc_mtt_range(dev, mtt->order, buddy);
219 if (mtt->first_seg == -1) {
221 return ERR_PTR(-ENOMEM);
227 struct mthca_mtt *mthca_alloc_mtt(struct mthca_dev *dev, int size)
229 return __mthca_alloc_mtt(dev, size, &dev->mr_table.mtt_buddy);
232 void mthca_free_mtt(struct mthca_dev *dev, struct mthca_mtt *mtt)
237 mthca_buddy_free(mtt->buddy, mtt->first_seg, mtt->order);
239 mthca_table_put_range(dev, dev->mr_table.mtt_table,
241 mtt->first_seg + (1 << mtt->order) - 1);
246 static int __mthca_write_mtt(struct mthca_dev *dev, struct mthca_mtt *mtt,
247 int start_index, u64 *buffer_list, int list_len)
249 struct mthca_mailbox *mailbox;
255 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
257 return PTR_ERR(mailbox);
258 mtt_entry = mailbox->buf;
260 while (list_len > 0) {
261 mtt_entry[0] = cpu_to_be64(dev->mr_table.mtt_base +
262 mtt->first_seg * MTHCA_MTT_SEG_SIZE +
265 for (i = 0; i < list_len && i < MTHCA_MAILBOX_SIZE / 8 - 2; ++i)
266 mtt_entry[i + 2] = cpu_to_be64(buffer_list[i] |
267 MTHCA_MTT_FLAG_PRESENT);
270 * If we have an odd number of entries to write, add
271 * one more dummy entry for firmware efficiency.
274 mtt_entry[i + 2] = 0;
276 err = mthca_WRITE_MTT(dev, mailbox, (i + 1) & ~1, &status);
278 mthca_warn(dev, "WRITE_MTT failed (%d)\n", err);
282 mthca_warn(dev, "WRITE_MTT returned status 0x%02x\n",
294 mthca_free_mailbox(dev, mailbox);
298 int mthca_write_mtt_size(struct mthca_dev *dev)
300 if (dev->mr_table.fmr_mtt_buddy != &dev->mr_table.mtt_buddy)
302 * Be friendly to WRITE_MTT command
303 * and leave two empty slots for the
304 * index and reserved fields of the
307 return PAGE_SIZE / sizeof (u64) - 2;
309 /* For Arbel, all MTTs must fit in the same page. */
310 return mthca_is_memfree(dev) ? (PAGE_SIZE / sizeof (u64)) : 0x7ffffff;
313 void mthca_tavor_write_mtt_seg(struct mthca_dev *dev, struct mthca_mtt *mtt,
314 int start_index, u64 *buffer_list, int list_len)
319 mtts = dev->mr_table.tavor_fmr.mtt_base + mtt->first_seg * MTHCA_MTT_SEG_SIZE +
320 start_index * sizeof (u64);
321 for (i = 0; i < list_len; ++i)
322 mthca_write64_raw(cpu_to_be64(buffer_list[i] | MTHCA_MTT_FLAG_PRESENT),
326 void mthca_arbel_write_mtt_seg(struct mthca_dev *dev, struct mthca_mtt *mtt,
327 int start_index, u64 *buffer_list, int list_len)
330 dma_addr_t dma_handle;
332 int s = start_index * sizeof (u64);
334 /* For Arbel, all MTTs must fit in the same page. */
335 BUG_ON(s / PAGE_SIZE != (s + list_len * sizeof(u64) - 1) / PAGE_SIZE);
336 /* Require full segments */
337 BUG_ON(s % MTHCA_MTT_SEG_SIZE);
339 mtts = mthca_table_find(dev->mr_table.mtt_table, mtt->first_seg +
340 s / MTHCA_MTT_SEG_SIZE, &dma_handle);
344 for (i = 0; i < list_len; ++i)
345 mtts[i] = cpu_to_be64(buffer_list[i] | MTHCA_MTT_FLAG_PRESENT);
347 dma_sync_single(&dev->pdev->dev, dma_handle, list_len * sizeof (u64), DMA_TO_DEVICE);
350 int mthca_write_mtt(struct mthca_dev *dev, struct mthca_mtt *mtt,
351 int start_index, u64 *buffer_list, int list_len)
353 int size = mthca_write_mtt_size(dev);
356 if (dev->mr_table.fmr_mtt_buddy != &dev->mr_table.mtt_buddy)
357 return __mthca_write_mtt(dev, mtt, start_index, buffer_list, list_len);
359 while (list_len > 0) {
360 chunk = min(size, list_len);
361 if (mthca_is_memfree(dev))
362 mthca_arbel_write_mtt_seg(dev, mtt, start_index,
365 mthca_tavor_write_mtt_seg(dev, mtt, start_index,
369 start_index += chunk;
370 buffer_list += chunk;
376 static inline u32 tavor_hw_index_to_key(u32 ind)
381 static inline u32 tavor_key_to_hw_index(u32 key)
386 static inline u32 arbel_hw_index_to_key(u32 ind)
388 return (ind >> 24) | (ind << 8);
391 static inline u32 arbel_key_to_hw_index(u32 key)
393 return (key << 24) | (key >> 8);
396 static inline u32 hw_index_to_key(struct mthca_dev *dev, u32 ind)
398 if (mthca_is_memfree(dev))
399 return arbel_hw_index_to_key(ind);
401 return tavor_hw_index_to_key(ind);
404 static inline u32 key_to_hw_index(struct mthca_dev *dev, u32 key)
406 if (mthca_is_memfree(dev))
407 return arbel_key_to_hw_index(key);
409 return tavor_key_to_hw_index(key);
412 static inline u32 adjust_key(struct mthca_dev *dev, u32 key)
414 if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
415 return ((key << 20) & 0x800000) | (key & 0x7fffff);
420 int mthca_mr_alloc(struct mthca_dev *dev, u32 pd, int buffer_size_shift,
421 u64 iova, u64 total_size, u32 access, struct mthca_mr *mr)
423 struct mthca_mailbox *mailbox;
424 struct mthca_mpt_entry *mpt_entry;
430 WARN_ON(buffer_size_shift >= 32);
432 key = mthca_alloc(&dev->mr_table.mpt_alloc);
435 key = adjust_key(dev, key);
436 mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);
438 if (mthca_is_memfree(dev)) {
439 err = mthca_table_get(dev, dev->mr_table.mpt_table, key);
441 goto err_out_mpt_free;
444 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
445 if (IS_ERR(mailbox)) {
446 err = PTR_ERR(mailbox);
449 mpt_entry = mailbox->buf;
451 mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS |
453 MTHCA_MPT_FLAG_REGION |
456 mpt_entry->flags |= cpu_to_be32(MTHCA_MPT_FLAG_PHYSICAL);
458 mpt_entry->page_size = cpu_to_be32(buffer_size_shift - 12);
459 mpt_entry->key = cpu_to_be32(key);
460 mpt_entry->pd = cpu_to_be32(pd);
461 mpt_entry->start = cpu_to_be64(iova);
462 mpt_entry->length = cpu_to_be64(total_size);
464 memset(&mpt_entry->lkey, 0,
465 sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, lkey));
469 cpu_to_be64(dev->mr_table.mtt_base +
470 mr->mtt->first_seg * MTHCA_MTT_SEG_SIZE);
473 mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey);
474 for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) {
476 printk("[%02x] ", i * 4);
477 printk(" %08x", be32_to_cpu(((__be32 *) mpt_entry)[i]));
478 if ((i + 1) % 4 == 0)
483 err = mthca_SW2HW_MPT(dev, mailbox,
484 key & (dev->limits.num_mpts - 1),
487 mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
488 goto err_out_mailbox;
490 mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n",
493 goto err_out_mailbox;
496 mthca_free_mailbox(dev, mailbox);
500 mthca_free_mailbox(dev, mailbox);
503 mthca_table_put(dev, dev->mr_table.mpt_table, key);
506 mthca_free(&dev->mr_table.mpt_alloc, key);
510 int mthca_mr_alloc_notrans(struct mthca_dev *dev, u32 pd,
511 u32 access, struct mthca_mr *mr)
514 return mthca_mr_alloc(dev, pd, 12, 0, ~0ULL, access, mr);
517 int mthca_mr_alloc_phys(struct mthca_dev *dev, u32 pd,
518 u64 *buffer_list, int buffer_size_shift,
519 int list_len, u64 iova, u64 total_size,
520 u32 access, struct mthca_mr *mr)
524 mr->mtt = mthca_alloc_mtt(dev, list_len);
526 return PTR_ERR(mr->mtt);
528 err = mthca_write_mtt(dev, mr->mtt, 0, buffer_list, list_len);
530 mthca_free_mtt(dev, mr->mtt);
534 err = mthca_mr_alloc(dev, pd, buffer_size_shift, iova,
535 total_size, access, mr);
537 mthca_free_mtt(dev, mr->mtt);
543 static void mthca_free_region(struct mthca_dev *dev, u32 lkey)
545 mthca_table_put(dev, dev->mr_table.mpt_table,
546 key_to_hw_index(dev, lkey));
548 mthca_free(&dev->mr_table.mpt_alloc, key_to_hw_index(dev, lkey));
551 void mthca_free_mr(struct mthca_dev *dev, struct mthca_mr *mr)
556 err = mthca_HW2SW_MPT(dev, NULL,
557 key_to_hw_index(dev, mr->ibmr.lkey) &
558 (dev->limits.num_mpts - 1),
561 mthca_warn(dev, "HW2SW_MPT failed (%d)\n", err);
563 mthca_warn(dev, "HW2SW_MPT returned status 0x%02x\n",
566 mthca_free_region(dev, mr->ibmr.lkey);
567 mthca_free_mtt(dev, mr->mtt);
570 int mthca_fmr_alloc(struct mthca_dev *dev, u32 pd,
571 u32 access, struct mthca_fmr *mr)
573 struct mthca_mpt_entry *mpt_entry;
574 struct mthca_mailbox *mailbox;
578 int list_len = mr->attr.max_pages;
582 if (mr->attr.page_shift < 12 || mr->attr.page_shift >= 32)
585 /* For Arbel, all MTTs must fit in the same page. */
586 if (mthca_is_memfree(dev) &&
587 mr->attr.max_pages * sizeof *mr->mem.arbel.mtts > PAGE_SIZE)
592 key = mthca_alloc(&dev->mr_table.mpt_alloc);
595 key = adjust_key(dev, key);
597 idx = key & (dev->limits.num_mpts - 1);
598 mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);
600 if (mthca_is_memfree(dev)) {
601 err = mthca_table_get(dev, dev->mr_table.mpt_table, key);
603 goto err_out_mpt_free;
605 mr->mem.arbel.mpt = mthca_table_find(dev->mr_table.mpt_table, key, NULL);
606 BUG_ON(!mr->mem.arbel.mpt);
608 mr->mem.tavor.mpt = dev->mr_table.tavor_fmr.mpt_base +
609 sizeof *(mr->mem.tavor.mpt) * idx;
611 mr->mtt = __mthca_alloc_mtt(dev, list_len, dev->mr_table.fmr_mtt_buddy);
615 mtt_seg = mr->mtt->first_seg * MTHCA_MTT_SEG_SIZE;
617 if (mthca_is_memfree(dev)) {
618 mr->mem.arbel.mtts = mthca_table_find(dev->mr_table.mtt_table,
620 &mr->mem.arbel.dma_handle);
621 BUG_ON(!mr->mem.arbel.mtts);
623 mr->mem.tavor.mtts = dev->mr_table.tavor_fmr.mtt_base + mtt_seg;
625 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
627 goto err_out_free_mtt;
629 mpt_entry = mailbox->buf;
631 mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS |
633 MTHCA_MPT_FLAG_REGION |
636 mpt_entry->page_size = cpu_to_be32(mr->attr.page_shift - 12);
637 mpt_entry->key = cpu_to_be32(key);
638 mpt_entry->pd = cpu_to_be32(pd);
639 memset(&mpt_entry->start, 0,
640 sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, start));
641 mpt_entry->mtt_seg = cpu_to_be64(dev->mr_table.mtt_base + mtt_seg);
644 mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey);
645 for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) {
647 printk("[%02x] ", i * 4);
648 printk(" %08x", be32_to_cpu(((__be32 *) mpt_entry)[i]));
649 if ((i + 1) % 4 == 0)
654 err = mthca_SW2HW_MPT(dev, mailbox,
655 key & (dev->limits.num_mpts - 1),
658 mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
659 goto err_out_mailbox_free;
662 mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n",
665 goto err_out_mailbox_free;
668 mthca_free_mailbox(dev, mailbox);
671 err_out_mailbox_free:
672 mthca_free_mailbox(dev, mailbox);
675 mthca_free_mtt(dev, mr->mtt);
678 mthca_table_put(dev, dev->mr_table.mpt_table, key);
681 mthca_free(&dev->mr_table.mpt_alloc, mr->ibmr.lkey);
685 int mthca_free_fmr(struct mthca_dev *dev, struct mthca_fmr *fmr)
690 mthca_free_region(dev, fmr->ibmr.lkey);
691 mthca_free_mtt(dev, fmr->mtt);
696 static inline int mthca_check_fmr(struct mthca_fmr *fmr, u64 *page_list,
697 int list_len, u64 iova)
701 if (list_len > fmr->attr.max_pages)
704 page_mask = (1 << fmr->attr.page_shift) - 1;
706 /* We are getting page lists, so va must be page aligned. */
707 if (iova & page_mask)
710 /* Trust the user not to pass misaligned data in page_list */
712 for (i = 0; i < list_len; ++i) {
713 if (page_list[i] & ~page_mask)
717 if (fmr->maps >= fmr->attr.max_maps)
724 int mthca_tavor_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
725 int list_len, u64 iova)
727 struct mthca_fmr *fmr = to_mfmr(ibfmr);
728 struct mthca_dev *dev = to_mdev(ibfmr->device);
729 struct mthca_mpt_entry mpt_entry;
733 err = mthca_check_fmr(fmr, page_list, list_len, iova);
739 key = tavor_key_to_hw_index(fmr->ibmr.lkey);
740 key += dev->limits.num_mpts;
741 fmr->ibmr.lkey = fmr->ibmr.rkey = tavor_hw_index_to_key(key);
743 writeb(MTHCA_MPT_STATUS_SW, fmr->mem.tavor.mpt);
745 for (i = 0; i < list_len; ++i) {
746 __be64 mtt_entry = cpu_to_be64(page_list[i] |
747 MTHCA_MTT_FLAG_PRESENT);
748 mthca_write64_raw(mtt_entry, fmr->mem.tavor.mtts + i);
751 mpt_entry.lkey = cpu_to_be32(key);
752 mpt_entry.length = cpu_to_be64(list_len * (1ull << fmr->attr.page_shift));
753 mpt_entry.start = cpu_to_be64(iova);
755 __raw_writel((__force u32) mpt_entry.lkey, &fmr->mem.tavor.mpt->key);
756 memcpy_toio(&fmr->mem.tavor.mpt->start, &mpt_entry.start,
757 offsetof(struct mthca_mpt_entry, window_count) -
758 offsetof(struct mthca_mpt_entry, start));
760 writeb(MTHCA_MPT_STATUS_HW, fmr->mem.tavor.mpt);
765 int mthca_arbel_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
766 int list_len, u64 iova)
768 struct mthca_fmr *fmr = to_mfmr(ibfmr);
769 struct mthca_dev *dev = to_mdev(ibfmr->device);
773 err = mthca_check_fmr(fmr, page_list, list_len, iova);
779 key = arbel_key_to_hw_index(fmr->ibmr.lkey);
780 if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
781 key += SINAI_FMR_KEY_INC;
783 key += dev->limits.num_mpts;
784 fmr->ibmr.lkey = fmr->ibmr.rkey = arbel_hw_index_to_key(key);
786 *(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_SW;
790 for (i = 0; i < list_len; ++i)
791 fmr->mem.arbel.mtts[i] = cpu_to_be64(page_list[i] |
792 MTHCA_MTT_FLAG_PRESENT);
794 dma_sync_single(&dev->pdev->dev, fmr->mem.arbel.dma_handle,
795 list_len * sizeof(u64), DMA_TO_DEVICE);
797 fmr->mem.arbel.mpt->key = cpu_to_be32(key);
798 fmr->mem.arbel.mpt->lkey = cpu_to_be32(key);
799 fmr->mem.arbel.mpt->length = cpu_to_be64(list_len * (1ull << fmr->attr.page_shift));
800 fmr->mem.arbel.mpt->start = cpu_to_be64(iova);
804 *(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_HW;
811 void mthca_tavor_fmr_unmap(struct mthca_dev *dev, struct mthca_fmr *fmr)
818 key = tavor_key_to_hw_index(fmr->ibmr.lkey);
819 key &= dev->limits.num_mpts - 1;
820 fmr->ibmr.lkey = fmr->ibmr.rkey = tavor_hw_index_to_key(key);
824 writeb(MTHCA_MPT_STATUS_SW, fmr->mem.tavor.mpt);
827 void mthca_arbel_fmr_unmap(struct mthca_dev *dev, struct mthca_fmr *fmr)
834 key = arbel_key_to_hw_index(fmr->ibmr.lkey);
835 key &= dev->limits.num_mpts - 1;
836 fmr->ibmr.lkey = fmr->ibmr.rkey = arbel_hw_index_to_key(key);
840 *(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_SW;
843 int mthca_init_mr_table(struct mthca_dev *dev)
846 int mpts, mtts, err, i;
848 err = mthca_alloc_init(&dev->mr_table.mpt_alloc,
849 dev->limits.num_mpts,
850 ~0, dev->limits.reserved_mrws);
854 if (!mthca_is_memfree(dev) &&
855 (dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN))
856 dev->limits.fmr_reserved_mtts = 0;
858 dev->mthca_flags |= MTHCA_FLAG_FMR;
860 if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
861 mthca_dbg(dev, "Memory key throughput optimization activated.\n");
863 err = mthca_buddy_init(&dev->mr_table.mtt_buddy,
864 fls(dev->limits.num_mtt_segs - 1));
869 dev->mr_table.tavor_fmr.mpt_base = NULL;
870 dev->mr_table.tavor_fmr.mtt_base = NULL;
872 if (dev->limits.fmr_reserved_mtts) {
873 i = fls(dev->limits.fmr_reserved_mtts - 1);
876 mthca_warn(dev, "Unable to reserve 2^31 FMR MTTs.\n");
880 mpts = mtts = 1 << i;
882 mpts = dev->limits.num_mtt_segs;
883 mtts = dev->limits.num_mpts;
886 if (!mthca_is_memfree(dev) &&
887 (dev->mthca_flags & MTHCA_FLAG_FMR)) {
889 addr = pci_resource_start(dev->pdev, 4) +
890 ((pci_resource_len(dev->pdev, 4) - 1) &
891 dev->mr_table.mpt_base);
893 dev->mr_table.tavor_fmr.mpt_base =
894 ioremap(addr, mpts * sizeof(struct mthca_mpt_entry));
896 if (!dev->mr_table.tavor_fmr.mpt_base) {
897 mthca_warn(dev, "MPT ioremap for FMR failed.\n");
902 addr = pci_resource_start(dev->pdev, 4) +
903 ((pci_resource_len(dev->pdev, 4) - 1) &
904 dev->mr_table.mtt_base);
906 dev->mr_table.tavor_fmr.mtt_base =
907 ioremap(addr, mtts * MTHCA_MTT_SEG_SIZE);
908 if (!dev->mr_table.tavor_fmr.mtt_base) {
909 mthca_warn(dev, "MTT ioremap for FMR failed.\n");
915 if (dev->limits.fmr_reserved_mtts) {
916 err = mthca_buddy_init(&dev->mr_table.tavor_fmr.mtt_buddy, fls(mtts - 1));
918 goto err_fmr_mtt_buddy;
920 /* Prevent regular MRs from using FMR keys */
921 err = mthca_buddy_alloc(&dev->mr_table.mtt_buddy, fls(mtts - 1));
923 goto err_reserve_fmr;
925 dev->mr_table.fmr_mtt_buddy =
926 &dev->mr_table.tavor_fmr.mtt_buddy;
928 dev->mr_table.fmr_mtt_buddy = &dev->mr_table.mtt_buddy;
930 /* FMR table is always the first, take reserved MTTs out of there */
931 if (dev->limits.reserved_mtts) {
932 i = fls(dev->limits.reserved_mtts - 1);
934 if (mthca_alloc_mtt_range(dev, i,
935 dev->mr_table.fmr_mtt_buddy) == -1) {
936 mthca_warn(dev, "MTT table of order %d is too small.\n",
937 dev->mr_table.fmr_mtt_buddy->max_order);
939 goto err_reserve_mtts;
947 if (dev->limits.fmr_reserved_mtts)
948 mthca_buddy_cleanup(&dev->mr_table.tavor_fmr.mtt_buddy);
951 if (dev->mr_table.tavor_fmr.mtt_base)
952 iounmap(dev->mr_table.tavor_fmr.mtt_base);
955 if (dev->mr_table.tavor_fmr.mpt_base)
956 iounmap(dev->mr_table.tavor_fmr.mpt_base);
959 mthca_buddy_cleanup(&dev->mr_table.mtt_buddy);
962 mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);
967 void mthca_cleanup_mr_table(struct mthca_dev *dev)
969 /* XXX check if any MRs are still allocated? */
970 if (dev->limits.fmr_reserved_mtts)
971 mthca_buddy_cleanup(&dev->mr_table.tavor_fmr.mtt_buddy);
973 mthca_buddy_cleanup(&dev->mr_table.mtt_buddy);
975 if (dev->mr_table.tavor_fmr.mtt_base)
976 iounmap(dev->mr_table.tavor_fmr.mtt_base);
977 if (dev->mr_table.tavor_fmr.mpt_base)
978 iounmap(dev->mr_table.tavor_fmr.mpt_base);
980 mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);