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 static void mthca_tavor_write_mtt_seg(struct mthca_dev *dev,
314 struct mthca_mtt *mtt, int start_index,
315 u64 *buffer_list, int list_len)
320 mtts = dev->mr_table.tavor_fmr.mtt_base + mtt->first_seg * MTHCA_MTT_SEG_SIZE +
321 start_index * sizeof (u64);
322 for (i = 0; i < list_len; ++i)
323 mthca_write64_raw(cpu_to_be64(buffer_list[i] | MTHCA_MTT_FLAG_PRESENT),
327 static void mthca_arbel_write_mtt_seg(struct mthca_dev *dev,
328 struct mthca_mtt *mtt, int start_index,
329 u64 *buffer_list, int list_len)
332 dma_addr_t dma_handle;
334 int s = start_index * sizeof (u64);
336 /* For Arbel, all MTTs must fit in the same page. */
337 BUG_ON(s / PAGE_SIZE != (s + list_len * sizeof(u64) - 1) / PAGE_SIZE);
338 /* Require full segments */
339 BUG_ON(s % MTHCA_MTT_SEG_SIZE);
341 mtts = mthca_table_find(dev->mr_table.mtt_table, mtt->first_seg +
342 s / MTHCA_MTT_SEG_SIZE, &dma_handle);
346 for (i = 0; i < list_len; ++i)
347 mtts[i] = cpu_to_be64(buffer_list[i] | MTHCA_MTT_FLAG_PRESENT);
349 dma_sync_single(&dev->pdev->dev, dma_handle, list_len * sizeof (u64), DMA_TO_DEVICE);
352 int mthca_write_mtt(struct mthca_dev *dev, struct mthca_mtt *mtt,
353 int start_index, u64 *buffer_list, int list_len)
355 int size = mthca_write_mtt_size(dev);
358 if (dev->mr_table.fmr_mtt_buddy != &dev->mr_table.mtt_buddy)
359 return __mthca_write_mtt(dev, mtt, start_index, buffer_list, list_len);
361 while (list_len > 0) {
362 chunk = min(size, list_len);
363 if (mthca_is_memfree(dev))
364 mthca_arbel_write_mtt_seg(dev, mtt, start_index,
367 mthca_tavor_write_mtt_seg(dev, mtt, start_index,
371 start_index += chunk;
372 buffer_list += chunk;
378 static inline u32 tavor_hw_index_to_key(u32 ind)
383 static inline u32 tavor_key_to_hw_index(u32 key)
388 static inline u32 arbel_hw_index_to_key(u32 ind)
390 return (ind >> 24) | (ind << 8);
393 static inline u32 arbel_key_to_hw_index(u32 key)
395 return (key << 24) | (key >> 8);
398 static inline u32 hw_index_to_key(struct mthca_dev *dev, u32 ind)
400 if (mthca_is_memfree(dev))
401 return arbel_hw_index_to_key(ind);
403 return tavor_hw_index_to_key(ind);
406 static inline u32 key_to_hw_index(struct mthca_dev *dev, u32 key)
408 if (mthca_is_memfree(dev))
409 return arbel_key_to_hw_index(key);
411 return tavor_key_to_hw_index(key);
414 static inline u32 adjust_key(struct mthca_dev *dev, u32 key)
416 if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
417 return ((key << 20) & 0x800000) | (key & 0x7fffff);
422 int mthca_mr_alloc(struct mthca_dev *dev, u32 pd, int buffer_size_shift,
423 u64 iova, u64 total_size, u32 access, struct mthca_mr *mr)
425 struct mthca_mailbox *mailbox;
426 struct mthca_mpt_entry *mpt_entry;
432 WARN_ON(buffer_size_shift >= 32);
434 key = mthca_alloc(&dev->mr_table.mpt_alloc);
437 key = adjust_key(dev, key);
438 mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);
440 if (mthca_is_memfree(dev)) {
441 err = mthca_table_get(dev, dev->mr_table.mpt_table, key);
443 goto err_out_mpt_free;
446 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
447 if (IS_ERR(mailbox)) {
448 err = PTR_ERR(mailbox);
451 mpt_entry = mailbox->buf;
453 mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS |
455 MTHCA_MPT_FLAG_REGION |
458 mpt_entry->flags |= cpu_to_be32(MTHCA_MPT_FLAG_PHYSICAL);
460 mpt_entry->page_size = cpu_to_be32(buffer_size_shift - 12);
461 mpt_entry->key = cpu_to_be32(key);
462 mpt_entry->pd = cpu_to_be32(pd);
463 mpt_entry->start = cpu_to_be64(iova);
464 mpt_entry->length = cpu_to_be64(total_size);
466 memset(&mpt_entry->lkey, 0,
467 sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, lkey));
471 cpu_to_be64(dev->mr_table.mtt_base +
472 mr->mtt->first_seg * MTHCA_MTT_SEG_SIZE);
475 mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey);
476 for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) {
478 printk("[%02x] ", i * 4);
479 printk(" %08x", be32_to_cpu(((__be32 *) mpt_entry)[i]));
480 if ((i + 1) % 4 == 0)
485 err = mthca_SW2HW_MPT(dev, mailbox,
486 key & (dev->limits.num_mpts - 1),
489 mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
490 goto err_out_mailbox;
492 mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n",
495 goto err_out_mailbox;
498 mthca_free_mailbox(dev, mailbox);
502 mthca_free_mailbox(dev, mailbox);
505 mthca_table_put(dev, dev->mr_table.mpt_table, key);
508 mthca_free(&dev->mr_table.mpt_alloc, key);
512 int mthca_mr_alloc_notrans(struct mthca_dev *dev, u32 pd,
513 u32 access, struct mthca_mr *mr)
516 return mthca_mr_alloc(dev, pd, 12, 0, ~0ULL, access, mr);
519 int mthca_mr_alloc_phys(struct mthca_dev *dev, u32 pd,
520 u64 *buffer_list, int buffer_size_shift,
521 int list_len, u64 iova, u64 total_size,
522 u32 access, struct mthca_mr *mr)
526 mr->mtt = mthca_alloc_mtt(dev, list_len);
528 return PTR_ERR(mr->mtt);
530 err = mthca_write_mtt(dev, mr->mtt, 0, buffer_list, list_len);
532 mthca_free_mtt(dev, mr->mtt);
536 err = mthca_mr_alloc(dev, pd, buffer_size_shift, iova,
537 total_size, access, mr);
539 mthca_free_mtt(dev, mr->mtt);
545 static void mthca_free_region(struct mthca_dev *dev, u32 lkey)
547 mthca_table_put(dev, dev->mr_table.mpt_table,
548 key_to_hw_index(dev, lkey));
550 mthca_free(&dev->mr_table.mpt_alloc, key_to_hw_index(dev, lkey));
553 void mthca_free_mr(struct mthca_dev *dev, struct mthca_mr *mr)
558 err = mthca_HW2SW_MPT(dev, NULL,
559 key_to_hw_index(dev, mr->ibmr.lkey) &
560 (dev->limits.num_mpts - 1),
563 mthca_warn(dev, "HW2SW_MPT failed (%d)\n", err);
565 mthca_warn(dev, "HW2SW_MPT returned status 0x%02x\n",
568 mthca_free_region(dev, mr->ibmr.lkey);
569 mthca_free_mtt(dev, mr->mtt);
572 int mthca_fmr_alloc(struct mthca_dev *dev, u32 pd,
573 u32 access, struct mthca_fmr *mr)
575 struct mthca_mpt_entry *mpt_entry;
576 struct mthca_mailbox *mailbox;
580 int list_len = mr->attr.max_pages;
584 if (mr->attr.page_shift < 12 || mr->attr.page_shift >= 32)
587 /* For Arbel, all MTTs must fit in the same page. */
588 if (mthca_is_memfree(dev) &&
589 mr->attr.max_pages * sizeof *mr->mem.arbel.mtts > PAGE_SIZE)
594 key = mthca_alloc(&dev->mr_table.mpt_alloc);
597 key = adjust_key(dev, key);
599 idx = key & (dev->limits.num_mpts - 1);
600 mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);
602 if (mthca_is_memfree(dev)) {
603 err = mthca_table_get(dev, dev->mr_table.mpt_table, key);
605 goto err_out_mpt_free;
607 mr->mem.arbel.mpt = mthca_table_find(dev->mr_table.mpt_table, key, NULL);
608 BUG_ON(!mr->mem.arbel.mpt);
610 mr->mem.tavor.mpt = dev->mr_table.tavor_fmr.mpt_base +
611 sizeof *(mr->mem.tavor.mpt) * idx;
613 mr->mtt = __mthca_alloc_mtt(dev, list_len, dev->mr_table.fmr_mtt_buddy);
617 mtt_seg = mr->mtt->first_seg * MTHCA_MTT_SEG_SIZE;
619 if (mthca_is_memfree(dev)) {
620 mr->mem.arbel.mtts = mthca_table_find(dev->mr_table.mtt_table,
622 &mr->mem.arbel.dma_handle);
623 BUG_ON(!mr->mem.arbel.mtts);
625 mr->mem.tavor.mtts = dev->mr_table.tavor_fmr.mtt_base + mtt_seg;
627 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
629 goto err_out_free_mtt;
631 mpt_entry = mailbox->buf;
633 mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS |
635 MTHCA_MPT_FLAG_REGION |
638 mpt_entry->page_size = cpu_to_be32(mr->attr.page_shift - 12);
639 mpt_entry->key = cpu_to_be32(key);
640 mpt_entry->pd = cpu_to_be32(pd);
641 memset(&mpt_entry->start, 0,
642 sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, start));
643 mpt_entry->mtt_seg = cpu_to_be64(dev->mr_table.mtt_base + mtt_seg);
646 mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey);
647 for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) {
649 printk("[%02x] ", i * 4);
650 printk(" %08x", be32_to_cpu(((__be32 *) mpt_entry)[i]));
651 if ((i + 1) % 4 == 0)
656 err = mthca_SW2HW_MPT(dev, mailbox,
657 key & (dev->limits.num_mpts - 1),
660 mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
661 goto err_out_mailbox_free;
664 mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n",
667 goto err_out_mailbox_free;
670 mthca_free_mailbox(dev, mailbox);
673 err_out_mailbox_free:
674 mthca_free_mailbox(dev, mailbox);
677 mthca_free_mtt(dev, mr->mtt);
680 mthca_table_put(dev, dev->mr_table.mpt_table, key);
683 mthca_free(&dev->mr_table.mpt_alloc, mr->ibmr.lkey);
687 int mthca_free_fmr(struct mthca_dev *dev, struct mthca_fmr *fmr)
692 mthca_free_region(dev, fmr->ibmr.lkey);
693 mthca_free_mtt(dev, fmr->mtt);
698 static inline int mthca_check_fmr(struct mthca_fmr *fmr, u64 *page_list,
699 int list_len, u64 iova)
703 if (list_len > fmr->attr.max_pages)
706 page_mask = (1 << fmr->attr.page_shift) - 1;
708 /* We are getting page lists, so va must be page aligned. */
709 if (iova & page_mask)
712 /* Trust the user not to pass misaligned data in page_list */
714 for (i = 0; i < list_len; ++i) {
715 if (page_list[i] & ~page_mask)
719 if (fmr->maps >= fmr->attr.max_maps)
726 int mthca_tavor_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
727 int list_len, u64 iova)
729 struct mthca_fmr *fmr = to_mfmr(ibfmr);
730 struct mthca_dev *dev = to_mdev(ibfmr->device);
731 struct mthca_mpt_entry mpt_entry;
735 err = mthca_check_fmr(fmr, page_list, list_len, iova);
741 key = tavor_key_to_hw_index(fmr->ibmr.lkey);
742 key += dev->limits.num_mpts;
743 fmr->ibmr.lkey = fmr->ibmr.rkey = tavor_hw_index_to_key(key);
745 writeb(MTHCA_MPT_STATUS_SW, fmr->mem.tavor.mpt);
747 for (i = 0; i < list_len; ++i) {
748 __be64 mtt_entry = cpu_to_be64(page_list[i] |
749 MTHCA_MTT_FLAG_PRESENT);
750 mthca_write64_raw(mtt_entry, fmr->mem.tavor.mtts + i);
753 mpt_entry.lkey = cpu_to_be32(key);
754 mpt_entry.length = cpu_to_be64(list_len * (1ull << fmr->attr.page_shift));
755 mpt_entry.start = cpu_to_be64(iova);
757 __raw_writel((__force u32) mpt_entry.lkey, &fmr->mem.tavor.mpt->key);
758 memcpy_toio(&fmr->mem.tavor.mpt->start, &mpt_entry.start,
759 offsetof(struct mthca_mpt_entry, window_count) -
760 offsetof(struct mthca_mpt_entry, start));
762 writeb(MTHCA_MPT_STATUS_HW, fmr->mem.tavor.mpt);
767 int mthca_arbel_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
768 int list_len, u64 iova)
770 struct mthca_fmr *fmr = to_mfmr(ibfmr);
771 struct mthca_dev *dev = to_mdev(ibfmr->device);
775 err = mthca_check_fmr(fmr, page_list, list_len, iova);
781 key = arbel_key_to_hw_index(fmr->ibmr.lkey);
782 if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
783 key += SINAI_FMR_KEY_INC;
785 key += dev->limits.num_mpts;
786 fmr->ibmr.lkey = fmr->ibmr.rkey = arbel_hw_index_to_key(key);
788 *(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_SW;
792 for (i = 0; i < list_len; ++i)
793 fmr->mem.arbel.mtts[i] = cpu_to_be64(page_list[i] |
794 MTHCA_MTT_FLAG_PRESENT);
796 dma_sync_single(&dev->pdev->dev, fmr->mem.arbel.dma_handle,
797 list_len * sizeof(u64), DMA_TO_DEVICE);
799 fmr->mem.arbel.mpt->key = cpu_to_be32(key);
800 fmr->mem.arbel.mpt->lkey = cpu_to_be32(key);
801 fmr->mem.arbel.mpt->length = cpu_to_be64(list_len * (1ull << fmr->attr.page_shift));
802 fmr->mem.arbel.mpt->start = cpu_to_be64(iova);
806 *(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_HW;
813 void mthca_tavor_fmr_unmap(struct mthca_dev *dev, struct mthca_fmr *fmr)
820 key = tavor_key_to_hw_index(fmr->ibmr.lkey);
821 key &= dev->limits.num_mpts - 1;
822 fmr->ibmr.lkey = fmr->ibmr.rkey = tavor_hw_index_to_key(key);
826 writeb(MTHCA_MPT_STATUS_SW, fmr->mem.tavor.mpt);
829 void mthca_arbel_fmr_unmap(struct mthca_dev *dev, struct mthca_fmr *fmr)
836 key = arbel_key_to_hw_index(fmr->ibmr.lkey);
837 key &= dev->limits.num_mpts - 1;
838 fmr->ibmr.lkey = fmr->ibmr.rkey = arbel_hw_index_to_key(key);
842 *(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_SW;
845 int mthca_init_mr_table(struct mthca_dev *dev)
848 int mpts, mtts, err, i;
850 err = mthca_alloc_init(&dev->mr_table.mpt_alloc,
851 dev->limits.num_mpts,
852 ~0, dev->limits.reserved_mrws);
856 if (!mthca_is_memfree(dev) &&
857 (dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN))
858 dev->limits.fmr_reserved_mtts = 0;
860 dev->mthca_flags |= MTHCA_FLAG_FMR;
862 if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
863 mthca_dbg(dev, "Memory key throughput optimization activated.\n");
865 err = mthca_buddy_init(&dev->mr_table.mtt_buddy,
866 fls(dev->limits.num_mtt_segs - 1));
871 dev->mr_table.tavor_fmr.mpt_base = NULL;
872 dev->mr_table.tavor_fmr.mtt_base = NULL;
874 if (dev->limits.fmr_reserved_mtts) {
875 i = fls(dev->limits.fmr_reserved_mtts - 1);
878 mthca_warn(dev, "Unable to reserve 2^31 FMR MTTs.\n");
882 mpts = mtts = 1 << i;
884 mpts = dev->limits.num_mtt_segs;
885 mtts = dev->limits.num_mpts;
888 if (!mthca_is_memfree(dev) &&
889 (dev->mthca_flags & MTHCA_FLAG_FMR)) {
891 addr = pci_resource_start(dev->pdev, 4) +
892 ((pci_resource_len(dev->pdev, 4) - 1) &
893 dev->mr_table.mpt_base);
895 dev->mr_table.tavor_fmr.mpt_base =
896 ioremap(addr, mpts * sizeof(struct mthca_mpt_entry));
898 if (!dev->mr_table.tavor_fmr.mpt_base) {
899 mthca_warn(dev, "MPT ioremap for FMR failed.\n");
904 addr = pci_resource_start(dev->pdev, 4) +
905 ((pci_resource_len(dev->pdev, 4) - 1) &
906 dev->mr_table.mtt_base);
908 dev->mr_table.tavor_fmr.mtt_base =
909 ioremap(addr, mtts * MTHCA_MTT_SEG_SIZE);
910 if (!dev->mr_table.tavor_fmr.mtt_base) {
911 mthca_warn(dev, "MTT ioremap for FMR failed.\n");
917 if (dev->limits.fmr_reserved_mtts) {
918 err = mthca_buddy_init(&dev->mr_table.tavor_fmr.mtt_buddy, fls(mtts - 1));
920 goto err_fmr_mtt_buddy;
922 /* Prevent regular MRs from using FMR keys */
923 err = mthca_buddy_alloc(&dev->mr_table.mtt_buddy, fls(mtts - 1));
925 goto err_reserve_fmr;
927 dev->mr_table.fmr_mtt_buddy =
928 &dev->mr_table.tavor_fmr.mtt_buddy;
930 dev->mr_table.fmr_mtt_buddy = &dev->mr_table.mtt_buddy;
932 /* FMR table is always the first, take reserved MTTs out of there */
933 if (dev->limits.reserved_mtts) {
934 i = fls(dev->limits.reserved_mtts - 1);
936 if (mthca_alloc_mtt_range(dev, i,
937 dev->mr_table.fmr_mtt_buddy) == -1) {
938 mthca_warn(dev, "MTT table of order %d is too small.\n",
939 dev->mr_table.fmr_mtt_buddy->max_order);
941 goto err_reserve_mtts;
949 if (dev->limits.fmr_reserved_mtts)
950 mthca_buddy_cleanup(&dev->mr_table.tavor_fmr.mtt_buddy);
953 if (dev->mr_table.tavor_fmr.mtt_base)
954 iounmap(dev->mr_table.tavor_fmr.mtt_base);
957 if (dev->mr_table.tavor_fmr.mpt_base)
958 iounmap(dev->mr_table.tavor_fmr.mpt_base);
961 mthca_buddy_cleanup(&dev->mr_table.mtt_buddy);
964 mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);
969 void mthca_cleanup_mr_table(struct mthca_dev *dev)
971 /* XXX check if any MRs are still allocated? */
972 if (dev->limits.fmr_reserved_mtts)
973 mthca_buddy_cleanup(&dev->mr_table.tavor_fmr.mtt_buddy);
975 mthca_buddy_cleanup(&dev->mr_table.mtt_buddy);
977 if (dev->mr_table.tavor_fmr.mtt_base)
978 iounmap(dev->mr_table.tavor_fmr.mtt_base);
979 if (dev->mr_table.tavor_fmr.mpt_base)
980 iounmap(dev->mr_table.tavor_fmr.mpt_base);
982 mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);