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"
43 * Must be packed because mtt_seg is 64 bits but only aligned to 32 bits.
45 struct mthca_mpt_entry {
54 u32 window_count_limit;
56 u32 mtt_sz; /* Arbel only */
58 } __attribute__((packed));
60 #define MTHCA_MPT_FLAG_SW_OWNS (0xfUL << 28)
61 #define MTHCA_MPT_FLAG_MIO (1 << 17)
62 #define MTHCA_MPT_FLAG_BIND_ENABLE (1 << 15)
63 #define MTHCA_MPT_FLAG_PHYSICAL (1 << 9)
64 #define MTHCA_MPT_FLAG_REGION (1 << 8)
66 #define MTHCA_MTT_FLAG_PRESENT 1
69 * Buddy allocator for MTT segments (currently not very efficient
70 * since it doesn't keep a free list and just searches linearly
71 * through the bitmaps)
74 static u32 mthca_alloc_mtt(struct mthca_dev *dev, int order)
80 spin_lock(&dev->mr_table.mpt_alloc.lock);
82 for (o = order; o <= dev->mr_table.max_mtt_order; ++o) {
83 m = 1 << (dev->mr_table.max_mtt_order - o);
84 seg = find_first_bit(dev->mr_table.mtt_buddy[o], m);
89 spin_unlock(&dev->mr_table.mpt_alloc.lock);
93 clear_bit(seg, dev->mr_table.mtt_buddy[o]);
98 set_bit(seg ^ 1, dev->mr_table.mtt_buddy[o]);
101 spin_unlock(&dev->mr_table.mpt_alloc.lock);
108 static void mthca_free_mtt(struct mthca_dev *dev, u32 seg, int order)
112 spin_lock(&dev->mr_table.mpt_alloc.lock);
114 while (test_bit(seg ^ 1, dev->mr_table.mtt_buddy[order])) {
115 clear_bit(seg ^ 1, dev->mr_table.mtt_buddy[order]);
120 set_bit(seg, dev->mr_table.mtt_buddy[order]);
122 spin_unlock(&dev->mr_table.mpt_alloc.lock);
125 static inline u32 hw_index_to_key(struct mthca_dev *dev, u32 ind)
127 if (dev->hca_type == ARBEL_NATIVE)
128 return (ind >> 24) | (ind << 8);
133 static inline u32 key_to_hw_index(struct mthca_dev *dev, u32 key)
135 if (dev->hca_type == ARBEL_NATIVE)
136 return (key << 24) | (key >> 8);
141 int mthca_mr_alloc_notrans(struct mthca_dev *dev, u32 pd,
142 u32 access, struct mthca_mr *mr)
145 struct mthca_mpt_entry *mpt_entry;
153 key = mthca_alloc(&dev->mr_table.mpt_alloc);
156 mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);
158 mailbox = kmalloc(sizeof *mpt_entry + MTHCA_CMD_MAILBOX_EXTRA,
161 mthca_free(&dev->mr_table.mpt_alloc, mr->ibmr.lkey);
164 mpt_entry = MAILBOX_ALIGN(mailbox);
166 mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS |
168 MTHCA_MPT_FLAG_PHYSICAL |
169 MTHCA_MPT_FLAG_REGION |
171 mpt_entry->page_size = 0;
172 mpt_entry->key = cpu_to_be32(key);
173 mpt_entry->pd = cpu_to_be32(pd);
174 mpt_entry->start = 0;
175 mpt_entry->length = ~0ULL;
177 memset(&mpt_entry->lkey, 0,
178 sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, lkey));
180 err = mthca_SW2HW_MPT(dev, mpt_entry,
181 key & (dev->limits.num_mpts - 1),
184 mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
186 mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n",
195 int mthca_mr_alloc_phys(struct mthca_dev *dev, u32 pd,
196 u64 *buffer_list, int buffer_size_shift,
197 int list_len, u64 iova, u64 total_size,
198 u32 access, struct mthca_mr *mr)
202 struct mthca_mpt_entry *mpt_entry;
209 WARN_ON(buffer_size_shift >= 32);
211 key = mthca_alloc(&dev->mr_table.mpt_alloc);
214 mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);
216 for (i = dev->limits.mtt_seg_size / 8, mr->order = 0;
218 i <<= 1, ++mr->order)
221 mr->first_seg = mthca_alloc_mtt(dev, mr->order);
222 if (mr->first_seg == -1)
223 goto err_out_mpt_free;
226 * If list_len is odd, we add one more dummy entry for
227 * firmware efficiency.
229 mailbox = kmalloc(max(sizeof *mpt_entry,
230 (size_t) 8 * (list_len + (list_len & 1) + 2)) +
231 MTHCA_CMD_MAILBOX_EXTRA,
234 goto err_out_free_mtt;
236 mtt_entry = MAILBOX_ALIGN(mailbox);
238 mtt_entry[0] = cpu_to_be64(dev->mr_table.mtt_base +
239 mr->first_seg * dev->limits.mtt_seg_size);
241 for (i = 0; i < list_len; ++i)
242 mtt_entry[i + 2] = cpu_to_be64(buffer_list[i] |
243 MTHCA_MTT_FLAG_PRESENT);
245 mtt_entry[i + 2] = 0;
250 mthca_dbg(dev, "Dumping MPT entry\n");
251 for (i = 0; i < list_len + 2; ++i)
252 printk(KERN_ERR "[%2d] %016llx\n",
253 i, (unsigned long long) be64_to_cpu(mtt_entry[i]));
256 err = mthca_WRITE_MTT(dev, mtt_entry, list_len, &status);
258 mthca_warn(dev, "WRITE_MTT failed (%d)\n", err);
259 goto err_out_mailbox_free;
262 mthca_warn(dev, "WRITE_MTT returned status 0x%02x\n",
265 goto err_out_mailbox_free;
268 mpt_entry = MAILBOX_ALIGN(mailbox);
270 mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS |
272 MTHCA_MPT_FLAG_REGION |
275 mpt_entry->page_size = cpu_to_be32(buffer_size_shift - 12);
276 mpt_entry->key = cpu_to_be32(key);
277 mpt_entry->pd = cpu_to_be32(pd);
278 mpt_entry->start = cpu_to_be64(iova);
279 mpt_entry->length = cpu_to_be64(total_size);
280 memset(&mpt_entry->lkey, 0,
281 sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, lkey));
282 mpt_entry->mtt_seg = cpu_to_be64(dev->mr_table.mtt_base +
283 mr->first_seg * dev->limits.mtt_seg_size);
286 mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey);
287 for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) {
289 printk("[%02x] ", i * 4);
290 printk(" %08x", be32_to_cpu(((u32 *) mpt_entry)[i]));
291 if ((i + 1) % 4 == 0)
296 err = mthca_SW2HW_MPT(dev, mpt_entry,
297 key & (dev->limits.num_mpts - 1),
300 mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
302 mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n",
310 err_out_mailbox_free:
314 mthca_free_mtt(dev, mr->first_seg, mr->order);
317 mthca_free(&dev->mr_table.mpt_alloc, mr->ibmr.lkey);
321 void mthca_free_mr(struct mthca_dev *dev, struct mthca_mr *mr)
328 err = mthca_HW2SW_MPT(dev, NULL,
329 key_to_hw_index(dev, mr->ibmr.lkey) &
330 (dev->limits.num_mpts - 1),
333 mthca_warn(dev, "HW2SW_MPT failed (%d)\n", err);
335 mthca_warn(dev, "HW2SW_MPT returned status 0x%02x\n",
339 mthca_free_mtt(dev, mr->first_seg, mr->order);
341 mthca_free(&dev->mr_table.mpt_alloc, key_to_hw_index(dev, mr->ibmr.lkey));
344 int __devinit mthca_init_mr_table(struct mthca_dev *dev)
349 err = mthca_alloc_init(&dev->mr_table.mpt_alloc,
350 dev->limits.num_mpts,
351 ~0, dev->limits.reserved_mrws);
357 for (i = 1, dev->mr_table.max_mtt_order = 0;
358 i < dev->limits.num_mtt_segs;
359 i <<= 1, ++dev->mr_table.max_mtt_order)
362 dev->mr_table.mtt_buddy = kmalloc((dev->mr_table.max_mtt_order + 1) *
365 if (!dev->mr_table.mtt_buddy)
368 for (i = 0; i <= dev->mr_table.max_mtt_order; ++i)
369 dev->mr_table.mtt_buddy[i] = NULL;
371 for (i = 0; i <= dev->mr_table.max_mtt_order; ++i) {
372 s = BITS_TO_LONGS(1 << (dev->mr_table.max_mtt_order - i));
373 dev->mr_table.mtt_buddy[i] = kmalloc(s * sizeof (long),
375 if (!dev->mr_table.mtt_buddy[i])
377 bitmap_zero(dev->mr_table.mtt_buddy[i],
378 1 << (dev->mr_table.max_mtt_order - i));
381 set_bit(0, dev->mr_table.mtt_buddy[dev->mr_table.max_mtt_order]);
383 for (i = 0; i < dev->mr_table.max_mtt_order; ++i)
384 if (1 << i >= dev->limits.reserved_mtts)
387 if (i == dev->mr_table.max_mtt_order) {
388 mthca_err(dev, "MTT table of order %d is "
393 (void) mthca_alloc_mtt(dev, i);
398 for (i = 0; i <= dev->mr_table.max_mtt_order; ++i)
399 kfree(dev->mr_table.mtt_buddy[i]);
402 mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);
407 void __devexit mthca_cleanup_mr_table(struct mthca_dev *dev)
411 /* XXX check if any MRs are still allocated? */
412 for (i = 0; i <= dev->mr_table.max_mtt_order; ++i)
413 kfree(dev->mr_table.mtt_buddy[i]);
414 kfree(dev->mr_table.mtt_buddy);
415 mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);