blob: 5eb6e07f35bb2fdbd83c910df56bb70193d0dc76 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 *
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:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
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.
22 *
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
30 * SOFTWARE.
31 *
32 * $Id: mthca_mr.c 1349 2004-12-16 21:09:43Z roland $
33 */
34
35#include <linux/slab.h>
36#include <linux/init.h>
37#include <linux/errno.h>
38
39#include "mthca_dev.h"
40#include "mthca_cmd.h"
Roland Dreier86562a12005-04-16 15:26:13 -070041#include "mthca_memfree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * Must be packed because mtt_seg is 64 bits but only aligned to 32 bits.
45 */
46struct mthca_mpt_entry {
47 u32 flags;
48 u32 page_size;
49 u32 key;
50 u32 pd;
51 u64 start;
52 u64 length;
53 u32 lkey;
54 u32 window_count;
55 u32 window_count_limit;
56 u64 mtt_seg;
57 u32 mtt_sz; /* Arbel only */
58 u32 reserved[2];
59} __attribute__((packed));
60
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)
66
67#define MTHCA_MTT_FLAG_PRESENT 1
68
69/*
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)
73 */
74
Roland Dreier86562a12005-04-16 15:26:13 -070075static u32 __mthca_alloc_mtt(struct mthca_dev *dev, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 int o;
78 int m;
79 u32 seg;
80
81 spin_lock(&dev->mr_table.mpt_alloc.lock);
82
83 for (o = order; o <= dev->mr_table.max_mtt_order; ++o) {
84 m = 1 << (dev->mr_table.max_mtt_order - o);
85 seg = find_first_bit(dev->mr_table.mtt_buddy[o], m);
86 if (seg < m)
87 goto found;
88 }
89
90 spin_unlock(&dev->mr_table.mpt_alloc.lock);
91 return -1;
92
93 found:
94 clear_bit(seg, dev->mr_table.mtt_buddy[o]);
95
96 while (o > order) {
97 --o;
98 seg <<= 1;
99 set_bit(seg ^ 1, dev->mr_table.mtt_buddy[o]);
100 }
101
102 spin_unlock(&dev->mr_table.mpt_alloc.lock);
103
104 seg <<= order;
105
106 return seg;
107}
108
Roland Dreier86562a12005-04-16 15:26:13 -0700109static void __mthca_free_mtt(struct mthca_dev *dev, u32 seg, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 seg >>= order;
112
113 spin_lock(&dev->mr_table.mpt_alloc.lock);
114
115 while (test_bit(seg ^ 1, dev->mr_table.mtt_buddy[order])) {
116 clear_bit(seg ^ 1, dev->mr_table.mtt_buddy[order]);
117 seg >>= 1;
118 ++order;
119 }
120
121 set_bit(seg, dev->mr_table.mtt_buddy[order]);
122
123 spin_unlock(&dev->mr_table.mpt_alloc.lock);
124}
125
Roland Dreier86562a12005-04-16 15:26:13 -0700126static u32 mthca_alloc_mtt(struct mthca_dev *dev, int order)
127{
128 u32 seg = __mthca_alloc_mtt(dev, order);
129
130 if (seg == -1)
131 return -1;
132
133 if (dev->hca_type == ARBEL_NATIVE)
134 if (mthca_table_get_range(dev, dev->mr_table.mtt_table, seg,
135 seg + (1 << order) - 1)) {
136 __mthca_free_mtt(dev, seg, order);
137 seg = -1;
138 }
139
140 return seg;
141}
142
143static void mthca_free_mtt(struct mthca_dev *dev, u32 seg, int order)
144{
145 __mthca_free_mtt(dev, seg, order);
146
147 if (dev->hca_type == ARBEL_NATIVE)
148 mthca_table_put_range(dev, dev->mr_table.mtt_table, seg,
149 seg + (1 << order) - 1);
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static inline u32 hw_index_to_key(struct mthca_dev *dev, u32 ind)
153{
154 if (dev->hca_type == ARBEL_NATIVE)
155 return (ind >> 24) | (ind << 8);
156 else
157 return ind;
158}
159
160static inline u32 key_to_hw_index(struct mthca_dev *dev, u32 key)
161{
162 if (dev->hca_type == ARBEL_NATIVE)
163 return (key << 24) | (key >> 8);
164 else
165 return key;
166}
167
168int mthca_mr_alloc_notrans(struct mthca_dev *dev, u32 pd,
169 u32 access, struct mthca_mr *mr)
170{
Roland Dreier86562a12005-04-16 15:26:13 -0700171 void *mailbox = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct mthca_mpt_entry *mpt_entry;
173 u32 key;
174 int err;
175 u8 status;
176
177 might_sleep();
178
179 mr->order = -1;
180 key = mthca_alloc(&dev->mr_table.mpt_alloc);
181 if (key == -1)
182 return -ENOMEM;
183 mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);
184
Roland Dreier86562a12005-04-16 15:26:13 -0700185 if (dev->hca_type == ARBEL_NATIVE) {
186 err = mthca_table_get(dev, dev->mr_table.mpt_table, key);
187 if (err)
188 goto err_out_mpt_free;
189 }
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 mailbox = kmalloc(sizeof *mpt_entry + MTHCA_CMD_MAILBOX_EXTRA,
192 GFP_KERNEL);
193 if (!mailbox) {
Roland Dreier86562a12005-04-16 15:26:13 -0700194 err = -ENOMEM;
195 goto err_out_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197 mpt_entry = MAILBOX_ALIGN(mailbox);
198
199 mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS |
200 MTHCA_MPT_FLAG_MIO |
201 MTHCA_MPT_FLAG_PHYSICAL |
202 MTHCA_MPT_FLAG_REGION |
203 access);
204 mpt_entry->page_size = 0;
205 mpt_entry->key = cpu_to_be32(key);
206 mpt_entry->pd = cpu_to_be32(pd);
207 mpt_entry->start = 0;
208 mpt_entry->length = ~0ULL;
209
210 memset(&mpt_entry->lkey, 0,
211 sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, lkey));
212
213 err = mthca_SW2HW_MPT(dev, mpt_entry,
214 key & (dev->limits.num_mpts - 1),
215 &status);
Roland Dreier86562a12005-04-16 15:26:13 -0700216 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
Roland Dreier86562a12005-04-16 15:26:13 -0700218 goto err_out_table;
219 } else if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n",
221 status);
222 err = -EINVAL;
Roland Dreier86562a12005-04-16 15:26:13 -0700223 goto err_out_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
226 kfree(mailbox);
227 return err;
Roland Dreier86562a12005-04-16 15:26:13 -0700228
229err_out_table:
230 if (dev->hca_type == ARBEL_NATIVE)
231 mthca_table_put(dev, dev->mr_table.mpt_table, key);
232
233err_out_mpt_free:
234 mthca_free(&dev->mr_table.mpt_alloc, mr->ibmr.lkey);
235 kfree(mailbox);
236 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239int mthca_mr_alloc_phys(struct mthca_dev *dev, u32 pd,
240 u64 *buffer_list, int buffer_size_shift,
241 int list_len, u64 iova, u64 total_size,
242 u32 access, struct mthca_mr *mr)
243{
244 void *mailbox;
245 u64 *mtt_entry;
246 struct mthca_mpt_entry *mpt_entry;
247 u32 key;
248 int err = -ENOMEM;
249 u8 status;
250 int i;
251
252 might_sleep();
253 WARN_ON(buffer_size_shift >= 32);
254
255 key = mthca_alloc(&dev->mr_table.mpt_alloc);
256 if (key == -1)
257 return -ENOMEM;
258 mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);
259
Roland Dreier86562a12005-04-16 15:26:13 -0700260 if (dev->hca_type == ARBEL_NATIVE) {
261 err = mthca_table_get(dev, dev->mr_table.mpt_table, key);
262 if (err)
263 goto err_out_mpt_free;
264 }
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 for (i = dev->limits.mtt_seg_size / 8, mr->order = 0;
267 i < list_len;
268 i <<= 1, ++mr->order)
269 ; /* nothing */
270
271 mr->first_seg = mthca_alloc_mtt(dev, mr->order);
272 if (mr->first_seg == -1)
Roland Dreier86562a12005-04-16 15:26:13 -0700273 goto err_out_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /*
276 * If list_len is odd, we add one more dummy entry for
277 * firmware efficiency.
278 */
279 mailbox = kmalloc(max(sizeof *mpt_entry,
280 (size_t) 8 * (list_len + (list_len & 1) + 2)) +
281 MTHCA_CMD_MAILBOX_EXTRA,
282 GFP_KERNEL);
283 if (!mailbox)
284 goto err_out_free_mtt;
285
286 mtt_entry = MAILBOX_ALIGN(mailbox);
287
288 mtt_entry[0] = cpu_to_be64(dev->mr_table.mtt_base +
289 mr->first_seg * dev->limits.mtt_seg_size);
290 mtt_entry[1] = 0;
291 for (i = 0; i < list_len; ++i)
292 mtt_entry[i + 2] = cpu_to_be64(buffer_list[i] |
293 MTHCA_MTT_FLAG_PRESENT);
294 if (list_len & 1) {
295 mtt_entry[i + 2] = 0;
296 ++list_len;
297 }
298
299 if (0) {
300 mthca_dbg(dev, "Dumping MPT entry\n");
301 for (i = 0; i < list_len + 2; ++i)
302 printk(KERN_ERR "[%2d] %016llx\n",
303 i, (unsigned long long) be64_to_cpu(mtt_entry[i]));
304 }
305
306 err = mthca_WRITE_MTT(dev, mtt_entry, list_len, &status);
307 if (err) {
308 mthca_warn(dev, "WRITE_MTT failed (%d)\n", err);
309 goto err_out_mailbox_free;
310 }
311 if (status) {
312 mthca_warn(dev, "WRITE_MTT returned status 0x%02x\n",
313 status);
314 err = -EINVAL;
315 goto err_out_mailbox_free;
316 }
317
318 mpt_entry = MAILBOX_ALIGN(mailbox);
319
320 mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS |
321 MTHCA_MPT_FLAG_MIO |
322 MTHCA_MPT_FLAG_REGION |
323 access);
324
325 mpt_entry->page_size = cpu_to_be32(buffer_size_shift - 12);
326 mpt_entry->key = cpu_to_be32(key);
327 mpt_entry->pd = cpu_to_be32(pd);
328 mpt_entry->start = cpu_to_be64(iova);
329 mpt_entry->length = cpu_to_be64(total_size);
330 memset(&mpt_entry->lkey, 0,
331 sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, lkey));
332 mpt_entry->mtt_seg = cpu_to_be64(dev->mr_table.mtt_base +
333 mr->first_seg * dev->limits.mtt_seg_size);
334
335 if (0) {
336 mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey);
337 for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) {
338 if (i % 4 == 0)
339 printk("[%02x] ", i * 4);
340 printk(" %08x", be32_to_cpu(((u32 *) mpt_entry)[i]));
341 if ((i + 1) % 4 == 0)
342 printk("\n");
343 }
344 }
345
346 err = mthca_SW2HW_MPT(dev, mpt_entry,
347 key & (dev->limits.num_mpts - 1),
348 &status);
349 if (err)
350 mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
351 else if (status) {
352 mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n",
353 status);
354 err = -EINVAL;
355 }
356
357 kfree(mailbox);
358 return err;
359
Roland Dreier86562a12005-04-16 15:26:13 -0700360err_out_mailbox_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 kfree(mailbox);
362
Roland Dreier86562a12005-04-16 15:26:13 -0700363err_out_free_mtt:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 mthca_free_mtt(dev, mr->first_seg, mr->order);
365
Roland Dreier86562a12005-04-16 15:26:13 -0700366err_out_table:
367 if (dev->hca_type == ARBEL_NATIVE)
368 mthca_table_put(dev, dev->mr_table.mpt_table, key);
369
370err_out_mpt_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 mthca_free(&dev->mr_table.mpt_alloc, mr->ibmr.lkey);
372 return err;
373}
374
375void mthca_free_mr(struct mthca_dev *dev, struct mthca_mr *mr)
376{
377 int err;
378 u8 status;
379
380 might_sleep();
381
382 err = mthca_HW2SW_MPT(dev, NULL,
383 key_to_hw_index(dev, mr->ibmr.lkey) &
384 (dev->limits.num_mpts - 1),
385 &status);
386 if (err)
387 mthca_warn(dev, "HW2SW_MPT failed (%d)\n", err);
388 else if (status)
389 mthca_warn(dev, "HW2SW_MPT returned status 0x%02x\n",
390 status);
391
392 if (mr->order >= 0)
393 mthca_free_mtt(dev, mr->first_seg, mr->order);
394
Roland Dreier86562a12005-04-16 15:26:13 -0700395 if (dev->hca_type == ARBEL_NATIVE)
396 mthca_table_put(dev, dev->mr_table.mpt_table,
397 key_to_hw_index(dev, mr->ibmr.lkey));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 mthca_free(&dev->mr_table.mpt_alloc, key_to_hw_index(dev, mr->ibmr.lkey));
399}
400
401int __devinit mthca_init_mr_table(struct mthca_dev *dev)
402{
403 int err;
404 int i, s;
405
406 err = mthca_alloc_init(&dev->mr_table.mpt_alloc,
407 dev->limits.num_mpts,
408 ~0, dev->limits.reserved_mrws);
409 if (err)
410 return err;
411
412 err = -ENOMEM;
413
414 for (i = 1, dev->mr_table.max_mtt_order = 0;
415 i < dev->limits.num_mtt_segs;
416 i <<= 1, ++dev->mr_table.max_mtt_order)
417 ; /* nothing */
418
419 dev->mr_table.mtt_buddy = kmalloc((dev->mr_table.max_mtt_order + 1) *
420 sizeof (long *),
421 GFP_KERNEL);
422 if (!dev->mr_table.mtt_buddy)
423 goto err_out;
424
425 for (i = 0; i <= dev->mr_table.max_mtt_order; ++i)
426 dev->mr_table.mtt_buddy[i] = NULL;
427
428 for (i = 0; i <= dev->mr_table.max_mtt_order; ++i) {
429 s = BITS_TO_LONGS(1 << (dev->mr_table.max_mtt_order - i));
430 dev->mr_table.mtt_buddy[i] = kmalloc(s * sizeof (long),
431 GFP_KERNEL);
432 if (!dev->mr_table.mtt_buddy[i])
433 goto err_out_free;
434 bitmap_zero(dev->mr_table.mtt_buddy[i],
435 1 << (dev->mr_table.max_mtt_order - i));
436 }
437
438 set_bit(0, dev->mr_table.mtt_buddy[dev->mr_table.max_mtt_order]);
439
440 for (i = 0; i < dev->mr_table.max_mtt_order; ++i)
441 if (1 << i >= dev->limits.reserved_mtts)
442 break;
443
444 if (i == dev->mr_table.max_mtt_order) {
445 mthca_err(dev, "MTT table of order %d is "
446 "too small.\n", i);
447 goto err_out_free;
448 }
449
450 (void) mthca_alloc_mtt(dev, i);
451
452 return 0;
453
454 err_out_free:
455 for (i = 0; i <= dev->mr_table.max_mtt_order; ++i)
456 kfree(dev->mr_table.mtt_buddy[i]);
457
458 err_out:
459 mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);
460
461 return err;
462}
463
464void __devexit mthca_cleanup_mr_table(struct mthca_dev *dev)
465{
466 int i;
467
468 /* XXX check if any MRs are still allocated? */
469 for (i = 0; i <= dev->mr_table.max_mtt_order; ++i)
470 kfree(dev->mr_table.mtt_buddy[i]);
471 kfree(dev->mr_table.mtt_buddy);
472 mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);
473}