2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * $Id: mthca_mr.c 1349 2004-12-16 21:09:43Z roland $
35 #include <linux/slab.h>
36 #include <linux/init.h>
37 #include <linux/errno.h>
39 #include "mthca_dev.h"
40 #include "mthca_cmd.h"
41 #include "mthca_memfree.h"
44 * Must be packed because mtt_seg is 64 bits but only aligned to 32 bits.
46 struct mthca_mpt_entry {
55 u32 window_count_limit;
57 u32 mtt_sz; /* Arbel only */
59 } __attribute__((packed));
61 #define MTHCA_MPT_FLAG_SW_OWNS (0xfUL << 28)
62 #define MTHCA_MPT_FLAG_MIO (1 << 17)
63 #define MTHCA_MPT_FLAG_BIND_ENABLE (1 << 15)
64 #define MTHCA_MPT_FLAG_PHYSICAL (1 << 9)
65 #define MTHCA_MPT_FLAG_REGION (1 << 8)
67 #define MTHCA_MTT_FLAG_PRESENT 1
70 * Buddy allocator for MTT segments (currently not very efficient
71 * since it doesn't keep a free list and just searches linearly
72 * through the bitmaps)
75 static u32 mthca_buddy_alloc(struct mthca_buddy *buddy, int order)
81 spin_lock(&buddy->lock);
83 for (o = order; o <= buddy->max_order; ++o) {
84 m = 1 << (buddy->max_order - o);
85 seg = find_first_bit(buddy->bits[o], m);
90 spin_unlock(&buddy->lock);
94 clear_bit(seg, buddy->bits[o]);
99 set_bit(seg ^ 1, buddy->bits[o]);
102 spin_unlock(&buddy->lock);
109 static void mthca_buddy_free(struct mthca_buddy *buddy, u32 seg, int order)
113 spin_lock(&buddy->lock);
115 while (test_bit(seg ^ 1, buddy->bits[order])) {
116 clear_bit(seg ^ 1, buddy->bits[order]);
121 set_bit(seg, buddy->bits[order]);
123 spin_unlock(&buddy->lock);
126 static int __devinit mthca_buddy_init(struct mthca_buddy *buddy, int max_order)
130 buddy->max_order = max_order;
131 spin_lock_init(&buddy->lock);
133 buddy->bits = kmalloc((buddy->max_order + 1) * sizeof (long *),
138 memset(buddy->bits, 0, (buddy->max_order + 1) * sizeof (long *));
140 for (i = 0; i <= buddy->max_order; ++i) {
141 s = BITS_TO_LONGS(1 << (buddy->max_order - i));
142 buddy->bits[i] = kmalloc(s * sizeof (long), GFP_KERNEL);
145 bitmap_zero(buddy->bits[i],
146 1 << (buddy->max_order - i));
149 set_bit(0, buddy->bits[buddy->max_order]);
154 for (i = 0; i <= buddy->max_order; ++i)
155 kfree(buddy->bits[i]);
163 static void __devexit mthca_buddy_cleanup(struct mthca_buddy *buddy)
167 for (i = 0; i <= buddy->max_order; ++i)
168 kfree(buddy->bits[i]);
173 static u32 mthca_alloc_mtt(struct mthca_dev *dev, int order,
174 struct mthca_buddy *buddy)
176 u32 seg = mthca_buddy_alloc(buddy, order);
181 if (dev->hca_type == ARBEL_NATIVE)
182 if (mthca_table_get_range(dev, dev->mr_table.mtt_table, seg,
183 seg + (1 << order) - 1)) {
184 mthca_buddy_free(buddy, seg, order);
191 static void mthca_free_mtt(struct mthca_dev *dev, u32 seg, int order,
192 struct mthca_buddy* buddy)
194 mthca_buddy_free(buddy, seg, order);
196 if (dev->hca_type == ARBEL_NATIVE)
197 mthca_table_put_range(dev, dev->mr_table.mtt_table, seg,
198 seg + (1 << order) - 1);
201 static inline u32 tavor_hw_index_to_key(u32 ind)
206 static inline u32 tavor_key_to_hw_index(u32 key)
211 static inline u32 arbel_hw_index_to_key(u32 ind)
213 return (ind >> 24) | (ind << 8);
216 static inline u32 arbel_key_to_hw_index(u32 key)
218 return (key << 24) | (key >> 8);
221 static inline u32 hw_index_to_key(struct mthca_dev *dev, u32 ind)
223 if (dev->hca_type == ARBEL_NATIVE)
224 return arbel_hw_index_to_key(ind);
226 return tavor_hw_index_to_key(ind);
229 static inline u32 key_to_hw_index(struct mthca_dev *dev, u32 key)
231 if (dev->hca_type == ARBEL_NATIVE)
232 return arbel_key_to_hw_index(key);
234 return tavor_key_to_hw_index(key);
237 int mthca_mr_alloc_notrans(struct mthca_dev *dev, u32 pd,
238 u32 access, struct mthca_mr *mr)
240 void *mailbox = NULL;
241 struct mthca_mpt_entry *mpt_entry;
249 key = mthca_alloc(&dev->mr_table.mpt_alloc);
252 mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);
254 if (dev->hca_type == ARBEL_NATIVE) {
255 err = mthca_table_get(dev, dev->mr_table.mpt_table, key);
257 goto err_out_mpt_free;
260 mailbox = kmalloc(sizeof *mpt_entry + MTHCA_CMD_MAILBOX_EXTRA,
266 mpt_entry = MAILBOX_ALIGN(mailbox);
268 mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS |
270 MTHCA_MPT_FLAG_PHYSICAL |
271 MTHCA_MPT_FLAG_REGION |
273 mpt_entry->page_size = 0;
274 mpt_entry->key = cpu_to_be32(key);
275 mpt_entry->pd = cpu_to_be32(pd);
276 mpt_entry->start = 0;
277 mpt_entry->length = ~0ULL;
279 memset(&mpt_entry->lkey, 0,
280 sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, lkey));
282 err = mthca_SW2HW_MPT(dev, mpt_entry,
283 key & (dev->limits.num_mpts - 1),
286 mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
289 mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n",
299 if (dev->hca_type == ARBEL_NATIVE)
300 mthca_table_put(dev, dev->mr_table.mpt_table, key);
303 mthca_free(&dev->mr_table.mpt_alloc, key);
308 int mthca_mr_alloc_phys(struct mthca_dev *dev, u32 pd,
309 u64 *buffer_list, int buffer_size_shift,
310 int list_len, u64 iova, u64 total_size,
311 u32 access, struct mthca_mr *mr)
315 struct mthca_mpt_entry *mpt_entry;
322 WARN_ON(buffer_size_shift >= 32);
324 key = mthca_alloc(&dev->mr_table.mpt_alloc);
327 mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);
329 if (dev->hca_type == ARBEL_NATIVE) {
330 err = mthca_table_get(dev, dev->mr_table.mpt_table, key);
332 goto err_out_mpt_free;
335 for (i = MTHCA_MTT_SEG_SIZE / 8, mr->order = 0;
337 i <<= 1, ++mr->order)
340 mr->first_seg = mthca_alloc_mtt(dev, mr->order,
341 &dev->mr_table.mtt_buddy);
342 if (mr->first_seg == -1)
346 * If list_len is odd, we add one more dummy entry for
347 * firmware efficiency.
349 mailbox = kmalloc(max(sizeof *mpt_entry,
350 (size_t) 8 * (list_len + (list_len & 1) + 2)) +
351 MTHCA_CMD_MAILBOX_EXTRA,
354 goto err_out_free_mtt;
356 mtt_entry = MAILBOX_ALIGN(mailbox);
358 mtt_entry[0] = cpu_to_be64(dev->mr_table.mtt_base +
359 mr->first_seg * MTHCA_MTT_SEG_SIZE);
361 for (i = 0; i < list_len; ++i)
362 mtt_entry[i + 2] = cpu_to_be64(buffer_list[i] |
363 MTHCA_MTT_FLAG_PRESENT);
365 mtt_entry[i + 2] = 0;
370 mthca_dbg(dev, "Dumping MPT entry\n");
371 for (i = 0; i < list_len + 2; ++i)
372 printk(KERN_ERR "[%2d] %016llx\n",
373 i, (unsigned long long) be64_to_cpu(mtt_entry[i]));
376 err = mthca_WRITE_MTT(dev, mtt_entry, list_len, &status);
378 mthca_warn(dev, "WRITE_MTT failed (%d)\n", err);
379 goto err_out_mailbox_free;
382 mthca_warn(dev, "WRITE_MTT returned status 0x%02x\n",
385 goto err_out_mailbox_free;
388 mpt_entry = MAILBOX_ALIGN(mailbox);
390 mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS |
392 MTHCA_MPT_FLAG_REGION |
395 mpt_entry->page_size = cpu_to_be32(buffer_size_shift - 12);
396 mpt_entry->key = cpu_to_be32(key);
397 mpt_entry->pd = cpu_to_be32(pd);
398 mpt_entry->start = cpu_to_be64(iova);
399 mpt_entry->length = cpu_to_be64(total_size);
400 memset(&mpt_entry->lkey, 0,
401 sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, lkey));
402 mpt_entry->mtt_seg = cpu_to_be64(dev->mr_table.mtt_base +
403 mr->first_seg * MTHCA_MTT_SEG_SIZE);
406 mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey);
407 for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) {
409 printk("[%02x] ", i * 4);
410 printk(" %08x", be32_to_cpu(((u32 *) mpt_entry)[i]));
411 if ((i + 1) % 4 == 0)
416 err = mthca_SW2HW_MPT(dev, mpt_entry,
417 key & (dev->limits.num_mpts - 1),
420 mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
422 mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n",
430 err_out_mailbox_free:
434 mthca_free_mtt(dev, mr->first_seg, mr->order, &dev->mr_table.mtt_buddy);
437 if (dev->hca_type == ARBEL_NATIVE)
438 mthca_table_put(dev, dev->mr_table.mpt_table, key);
441 mthca_free(&dev->mr_table.mpt_alloc, key);
445 void mthca_free_mr(struct mthca_dev *dev, struct mthca_mr *mr)
452 err = mthca_HW2SW_MPT(dev, NULL,
453 key_to_hw_index(dev, mr->ibmr.lkey) &
454 (dev->limits.num_mpts - 1),
457 mthca_warn(dev, "HW2SW_MPT failed (%d)\n", err);
459 mthca_warn(dev, "HW2SW_MPT returned status 0x%02x\n",
463 mthca_free_mtt(dev, mr->first_seg, mr->order, &dev->mr_table.mtt_buddy);
465 if (dev->hca_type == ARBEL_NATIVE)
466 mthca_table_put(dev, dev->mr_table.mpt_table,
467 key_to_hw_index(dev, mr->ibmr.lkey));
468 mthca_free(&dev->mr_table.mpt_alloc, key_to_hw_index(dev, mr->ibmr.lkey));
471 int __devinit mthca_init_mr_table(struct mthca_dev *dev)
475 err = mthca_alloc_init(&dev->mr_table.mpt_alloc,
476 dev->limits.num_mpts,
477 ~0, dev->limits.reserved_mrws);
481 err = mthca_buddy_init(&dev->mr_table.mtt_buddy,
482 fls(dev->limits.num_mtt_segs - 1));
486 if (dev->limits.reserved_mtts) {
487 if (mthca_alloc_mtt(dev, fls(dev->limits.reserved_mtts - 1),
488 &dev->mr_table.mtt_buddy) == -1) {
489 mthca_warn(dev, "MTT table of order %d is too small.\n",
490 dev->mr_table.mtt_buddy.max_order);
499 mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);
504 void __devexit mthca_cleanup_mr_table(struct mthca_dev *dev)
506 /* XXX check if any MRs are still allocated? */
507 mthca_buddy_cleanup(&dev->mr_table.mtt_buddy);
508 mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);