mthca_mr.c 20.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Copyright (c) 2004 Topspin Communications.  All rights reserved.
3
 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * $Id: mthca_mr.c 1349 2004-12-16 21:09:43Z roland $
 */

#include <linux/slab.h>
#include <linux/init.h>
#include <linux/errno.h>

#include "mthca_dev.h"
#include "mthca_cmd.h"
42
#include "mthca_memfree.h"
L
Linus Torvalds 已提交
43

44 45 46 47 48 49
struct mthca_mtt {
	struct mthca_buddy *buddy;
	int                 order;
	u32                 first_seg;
};

L
Linus Torvalds 已提交
50 51 52 53
/*
 * Must be packed because mtt_seg is 64 bits but only aligned to 32 bits.
 */
struct mthca_mpt_entry {
54 55 56 57 58 59 60 61 62 63 64 65
	__be32 flags;
	__be32 page_size;
	__be32 key;
	__be32 pd;
	__be64 start;
	__be64 length;
	__be32 lkey;
	__be32 window_count;
	__be32 window_count_limit;
	__be64 mtt_seg;
	__be32 mtt_sz;		/* Arbel only */
	u32    reserved[2];
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74 75
} __attribute__((packed));

#define MTHCA_MPT_FLAG_SW_OWNS       (0xfUL << 28)
#define MTHCA_MPT_FLAG_MIO           (1 << 17)
#define MTHCA_MPT_FLAG_BIND_ENABLE   (1 << 15)
#define MTHCA_MPT_FLAG_PHYSICAL      (1 <<  9)
#define MTHCA_MPT_FLAG_REGION        (1 <<  8)

#define MTHCA_MTT_FLAG_PRESENT       1

76 77 78
#define MTHCA_MPT_STATUS_SW 0xF0
#define MTHCA_MPT_STATUS_HW 0x00

79 80
#define SINAI_FMR_KEY_INC 0x1000000

L
Linus Torvalds 已提交
81 82 83 84 85 86
/*
 * Buddy allocator for MTT segments (currently not very efficient
 * since it doesn't keep a free list and just searches linearly
 * through the bitmaps)
 */

87
static u32 mthca_buddy_alloc(struct mthca_buddy *buddy, int order)
L
Linus Torvalds 已提交
88 89 90 91 92
{
	int o;
	int m;
	u32 seg;

93
	spin_lock(&buddy->lock);
L
Linus Torvalds 已提交
94

95 96 97
	for (o = order; o <= buddy->max_order; ++o) {
		m = 1 << (buddy->max_order - o);
		seg = find_first_bit(buddy->bits[o], m);
L
Linus Torvalds 已提交
98 99 100 101
		if (seg < m)
			goto found;
	}

102
	spin_unlock(&buddy->lock);
L
Linus Torvalds 已提交
103 104 105
	return -1;

 found:
106
	clear_bit(seg, buddy->bits[o]);
L
Linus Torvalds 已提交
107 108 109 110

	while (o > order) {
		--o;
		seg <<= 1;
111
		set_bit(seg ^ 1, buddy->bits[o]);
L
Linus Torvalds 已提交
112 113
	}

114
	spin_unlock(&buddy->lock);
L
Linus Torvalds 已提交
115 116 117 118 119 120

	seg <<= order;

	return seg;
}

121
static void mthca_buddy_free(struct mthca_buddy *buddy, u32 seg, int order)
L
Linus Torvalds 已提交
122 123 124
{
	seg >>= order;

125
	spin_lock(&buddy->lock);
L
Linus Torvalds 已提交
126

127 128
	while (test_bit(seg ^ 1, buddy->bits[order])) {
		clear_bit(seg ^ 1, buddy->bits[order]);
L
Linus Torvalds 已提交
129 130 131 132
		seg >>= 1;
		++order;
	}

133
	set_bit(seg, buddy->bits[order]);
L
Linus Torvalds 已提交
134

135
	spin_unlock(&buddy->lock);
L
Linus Torvalds 已提交
136 137
}

138
static int __devinit mthca_buddy_init(struct mthca_buddy *buddy, int max_order)
139
{
140 141 142 143 144
	int i, s;

	buddy->max_order = max_order;
	spin_lock_init(&buddy->lock);

R
Roland Dreier 已提交
145
	buddy->bits = kzalloc((buddy->max_order + 1) * sizeof (long *),
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
			      GFP_KERNEL);
	if (!buddy->bits)
		goto err_out;

	for (i = 0; i <= buddy->max_order; ++i) {
		s = BITS_TO_LONGS(1 << (buddy->max_order - i));
		buddy->bits[i] = kmalloc(s * sizeof (long), GFP_KERNEL);
		if (!buddy->bits[i])
			goto err_out_free;
		bitmap_zero(buddy->bits[i],
			    1 << (buddy->max_order - i));
	}

	set_bit(0, buddy->bits[buddy->max_order]);

	return 0;

err_out_free:
	for (i = 0; i <= buddy->max_order; ++i)
		kfree(buddy->bits[i]);

	kfree(buddy->bits);

err_out:
	return -ENOMEM;
}

173
static void mthca_buddy_cleanup(struct mthca_buddy *buddy)
174 175 176 177 178 179 180 181 182
{
	int i;

	for (i = 0; i <= buddy->max_order; ++i)
		kfree(buddy->bits[i]);

	kfree(buddy->bits);
}

183 184
static u32 mthca_alloc_mtt_range(struct mthca_dev *dev, int order,
				 struct mthca_buddy *buddy)
185 186
{
	u32 seg = mthca_buddy_alloc(buddy, order);
187 188 189 190

	if (seg == -1)
		return -1;

191
	if (mthca_is_memfree(dev))
192 193
		if (mthca_table_get_range(dev, dev->mr_table.mtt_table, seg,
					  seg + (1 << order) - 1)) {
194
			mthca_buddy_free(buddy, seg, order);
195 196 197 198 199 200
			seg = -1;
		}

	return seg;
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
static struct mthca_mtt *__mthca_alloc_mtt(struct mthca_dev *dev, int size,
					   struct mthca_buddy *buddy)
{
	struct mthca_mtt *mtt;
	int i;

	if (size <= 0)
		return ERR_PTR(-EINVAL);

	mtt = kmalloc(sizeof *mtt, GFP_KERNEL);
	if (!mtt)
		return ERR_PTR(-ENOMEM);

	mtt->buddy = buddy;
	mtt->order = 0;
	for (i = MTHCA_MTT_SEG_SIZE / 8; i < size; i <<= 1)
		++mtt->order;

	mtt->first_seg = mthca_alloc_mtt_range(dev, mtt->order, buddy);
	if (mtt->first_seg == -1) {
		kfree(mtt);
		return ERR_PTR(-ENOMEM);
	}

	return mtt;
}

struct mthca_mtt *mthca_alloc_mtt(struct mthca_dev *dev, int size)
{
	return __mthca_alloc_mtt(dev, size, &dev->mr_table.mtt_buddy);
}

void mthca_free_mtt(struct mthca_dev *dev, struct mthca_mtt *mtt)
{
	if (!mtt)
		return;

	mthca_buddy_free(mtt->buddy, mtt->first_seg, mtt->order);

	mthca_table_put_range(dev, dev->mr_table.mtt_table,
			      mtt->first_seg,
			      mtt->first_seg + (1 << mtt->order) - 1);

	kfree(mtt);
}

int mthca_write_mtt(struct mthca_dev *dev, struct mthca_mtt *mtt,
		    int start_index, u64 *buffer_list, int list_len)
249
{
250
	struct mthca_mailbox *mailbox;
251
	__be64 *mtt_entry;
252 253 254 255
	int err = 0;
	u8 status;
	int i;

256 257 258 259
	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
	if (IS_ERR(mailbox))
		return PTR_ERR(mailbox);
	mtt_entry = mailbox->buf;
260 261 262 263 264 265

	while (list_len > 0) {
		mtt_entry[0] = cpu_to_be64(dev->mr_table.mtt_base +
					   mtt->first_seg * MTHCA_MTT_SEG_SIZE +
					   start_index * 8);
		mtt_entry[1] = 0;
266
		for (i = 0; i < list_len && i < MTHCA_MAILBOX_SIZE / 8 - 2; ++i)
267 268 269 270 271 272 273 274 275 276
			mtt_entry[i + 2] = cpu_to_be64(buffer_list[i] |
						       MTHCA_MTT_FLAG_PRESENT);

		/*
		 * If we have an odd number of entries to write, add
		 * one more dummy entry for firmware efficiency.
		 */
		if (i & 1)
			mtt_entry[i + 2] = 0;

277
		err = mthca_WRITE_MTT(dev, mailbox, (i + 1) & ~1, &status);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
		if (err) {
			mthca_warn(dev, "WRITE_MTT failed (%d)\n", err);
			goto out;
		}
		if (status) {
			mthca_warn(dev, "WRITE_MTT returned status 0x%02x\n",
				   status);
			err = -EINVAL;
			goto out;
		}

		list_len    -= i;
		start_index += i;
		buffer_list += i;
	}

out:
295
	mthca_free_mailbox(dev, mailbox);
296
	return err;
297 298
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
static inline u32 tavor_hw_index_to_key(u32 ind)
{
	return ind;
}

static inline u32 tavor_key_to_hw_index(u32 key)
{
	return key;
}

static inline u32 arbel_hw_index_to_key(u32 ind)
{
	return (ind >> 24) | (ind << 8);
}

static inline u32 arbel_key_to_hw_index(u32 key)
{
	return (key << 24) | (key >> 8);
}

L
Linus Torvalds 已提交
319 320
static inline u32 hw_index_to_key(struct mthca_dev *dev, u32 ind)
{
321
	if (mthca_is_memfree(dev))
322
		return arbel_hw_index_to_key(ind);
L
Linus Torvalds 已提交
323
	else
324
		return tavor_hw_index_to_key(ind);
L
Linus Torvalds 已提交
325 326 327 328
}

static inline u32 key_to_hw_index(struct mthca_dev *dev, u32 key)
{
329
	if (mthca_is_memfree(dev))
330
		return arbel_key_to_hw_index(key);
L
Linus Torvalds 已提交
331
	else
332
		return tavor_key_to_hw_index(key);
L
Linus Torvalds 已提交
333 334
}

335 336 337 338 339 340 341 342
static inline u32 adjust_key(struct mthca_dev *dev, u32 key)
{
	if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
		return ((key << 20) & 0x800000) | (key & 0x7fffff);
	else
		return key;
}

343 344
int mthca_mr_alloc(struct mthca_dev *dev, u32 pd, int buffer_size_shift,
		   u64 iova, u64 total_size, u32 access, struct mthca_mr *mr)
L
Linus Torvalds 已提交
345
{
346
	struct mthca_mailbox *mailbox;
L
Linus Torvalds 已提交
347 348
	struct mthca_mpt_entry *mpt_entry;
	u32 key;
349
	int i;
L
Linus Torvalds 已提交
350 351 352
	int err;
	u8 status;

353 354
	WARN_ON(buffer_size_shift >= 32);

L
Linus Torvalds 已提交
355 356 357
	key = mthca_alloc(&dev->mr_table.mpt_alloc);
	if (key == -1)
		return -ENOMEM;
358
	key = adjust_key(dev, key);
L
Linus Torvalds 已提交
359 360
	mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);

361
	if (mthca_is_memfree(dev)) {
362 363 364 365 366
		err = mthca_table_get(dev, dev->mr_table.mpt_table, key);
		if (err)
			goto err_out_mpt_free;
	}

367 368 369
	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
	if (IS_ERR(mailbox)) {
		err = PTR_ERR(mailbox);
370
		goto err_out_table;
L
Linus Torvalds 已提交
371
	}
372
	mpt_entry = mailbox->buf;
L
Linus Torvalds 已提交
373 374 375 376 377

	mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS     |
				       MTHCA_MPT_FLAG_MIO         |
				       MTHCA_MPT_FLAG_REGION      |
				       access);
378 379 380 381
	if (!mr->mtt)
		mpt_entry->flags |= cpu_to_be32(MTHCA_MPT_FLAG_PHYSICAL);

	mpt_entry->page_size = cpu_to_be32(buffer_size_shift - 12);
L
Linus Torvalds 已提交
382 383
	mpt_entry->key       = cpu_to_be32(key);
	mpt_entry->pd        = cpu_to_be32(pd);
384 385
	mpt_entry->start     = cpu_to_be64(iova);
	mpt_entry->length    = cpu_to_be64(total_size);
L
Linus Torvalds 已提交
386 387 388 389

	memset(&mpt_entry->lkey, 0,
	       sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, lkey));

390 391 392 393 394 395 396 397 398 399
	if (mr->mtt)
		mpt_entry->mtt_seg =
			cpu_to_be64(dev->mr_table.mtt_base +
				    mr->mtt->first_seg * MTHCA_MTT_SEG_SIZE);

	if (0) {
		mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey);
		for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) {
			if (i % 4 == 0)
				printk("[%02x] ", i * 4);
400
			printk(" %08x", be32_to_cpu(((__be32 *) mpt_entry)[i]));
401 402 403 404 405
			if ((i + 1) % 4 == 0)
				printk("\n");
		}
	}

406
	err = mthca_SW2HW_MPT(dev, mailbox,
L
Linus Torvalds 已提交
407 408
			      key & (dev->limits.num_mpts - 1),
			      &status);
409
	if (err) {
L
Linus Torvalds 已提交
410
		mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
411
		goto err_out_mailbox;
412
	} else if (status) {
L
Linus Torvalds 已提交
413 414 415
		mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n",
			   status);
		err = -EINVAL;
416
		goto err_out_mailbox;
L
Linus Torvalds 已提交
417 418
	}

419
	mthca_free_mailbox(dev, mailbox);
L
Linus Torvalds 已提交
420
	return err;
421

422
err_out_mailbox:
423
	mthca_free_mailbox(dev, mailbox);
424

425
err_out_table:
426
	mthca_table_put(dev, dev->mr_table.mpt_table, key);
427 428

err_out_mpt_free:
429
	mthca_free(&dev->mr_table.mpt_alloc, key);
430
	return err;
L
Linus Torvalds 已提交
431 432
}

433 434 435 436 437 438 439
int mthca_mr_alloc_notrans(struct mthca_dev *dev, u32 pd,
			   u32 access, struct mthca_mr *mr)
{
	mr->mtt = NULL;
	return mthca_mr_alloc(dev, pd, 12, 0, ~0ULL, access, mr);
}

L
Linus Torvalds 已提交
440 441 442 443 444
int mthca_mr_alloc_phys(struct mthca_dev *dev, u32 pd,
			u64 *buffer_list, int buffer_size_shift,
			int list_len, u64 iova, u64 total_size,
			u32 access, struct mthca_mr *mr)
{
445
	int err;
L
Linus Torvalds 已提交
446

447 448 449
	mr->mtt = mthca_alloc_mtt(dev, list_len);
	if (IS_ERR(mr->mtt))
		return PTR_ERR(mr->mtt);
L
Linus Torvalds 已提交
450

451
	err = mthca_write_mtt(dev, mr->mtt, 0, buffer_list, list_len);
L
Linus Torvalds 已提交
452
	if (err) {
453 454
		mthca_free_mtt(dev, mr->mtt);
		return err;
L
Linus Torvalds 已提交
455 456
	}

457 458
	err = mthca_mr_alloc(dev, pd, buffer_size_shift, iova,
			     total_size, access, mr);
L
Linus Torvalds 已提交
459
	if (err)
460
		mthca_free_mtt(dev, mr->mtt);
L
Linus Torvalds 已提交
461 462 463 464

	return err;
}

465
/* Free mr or fmr */
466
static void mthca_free_region(struct mthca_dev *dev, u32 lkey)
467
{
468
	mthca_table_put(dev, dev->mr_table.mpt_table,
469
			key_to_hw_index(dev, lkey));
470 471 472 473

	mthca_free(&dev->mr_table.mpt_alloc, key_to_hw_index(dev, lkey));
}

L
Linus Torvalds 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
void mthca_free_mr(struct mthca_dev *dev, struct mthca_mr *mr)
{
	int err;
	u8 status;

	err = mthca_HW2SW_MPT(dev, NULL,
			      key_to_hw_index(dev, mr->ibmr.lkey) &
			      (dev->limits.num_mpts - 1),
			      &status);
	if (err)
		mthca_warn(dev, "HW2SW_MPT failed (%d)\n", err);
	else if (status)
		mthca_warn(dev, "HW2SW_MPT returned status 0x%02x\n",
			   status);

489 490
	mthca_free_region(dev, mr->ibmr.lkey);
	mthca_free_mtt(dev, mr->mtt);
491 492 493 494 495 496
}

int mthca_fmr_alloc(struct mthca_dev *dev, u32 pd,
		    u32 access, struct mthca_fmr *mr)
{
	struct mthca_mpt_entry *mpt_entry;
497
	struct mthca_mailbox *mailbox;
498 499 500 501 502 503 504
	u64 mtt_seg;
	u32 key, idx;
	u8 status;
	int list_len = mr->attr.max_pages;
	int err = -ENOMEM;
	int i;

505
	if (mr->attr.page_shift < 12 || mr->attr.page_shift >= 32)
506 507 508
		return -EINVAL;

	/* For Arbel, all MTTs must fit in the same page. */
509
	if (mthca_is_memfree(dev) &&
510 511 512 513 514 515 516 517
	    mr->attr.max_pages * sizeof *mr->mem.arbel.mtts > PAGE_SIZE)
		return -EINVAL;

	mr->maps = 0;

	key = mthca_alloc(&dev->mr_table.mpt_alloc);
	if (key == -1)
		return -ENOMEM;
518
	key = adjust_key(dev, key);
519 520 521 522

	idx = key & (dev->limits.num_mpts - 1);
	mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);

523
	if (mthca_is_memfree(dev)) {
524 525 526 527 528 529 530 531
		err = mthca_table_get(dev, dev->mr_table.mpt_table, key);
		if (err)
			goto err_out_mpt_free;

		mr->mem.arbel.mpt = mthca_table_find(dev->mr_table.mpt_table, key);
		BUG_ON(!mr->mem.arbel.mpt);
	} else
		mr->mem.tavor.mpt = dev->mr_table.tavor_fmr.mpt_base +
R
Roland Dreier 已提交
532
			sizeof *(mr->mem.tavor.mpt) * idx;
533

534 535
	mr->mtt = __mthca_alloc_mtt(dev, list_len, dev->mr_table.fmr_mtt_buddy);
	if (IS_ERR(mr->mtt))
536 537
		goto err_out_table;

538
	mtt_seg = mr->mtt->first_seg * MTHCA_MTT_SEG_SIZE;
539

540
	if (mthca_is_memfree(dev)) {
541
		mr->mem.arbel.mtts = mthca_table_find(dev->mr_table.mtt_table,
542
						      mr->mtt->first_seg);
543 544 545 546
		BUG_ON(!mr->mem.arbel.mtts);
	} else
		mr->mem.tavor.mtts = dev->mr_table.tavor_fmr.mtt_base + mtt_seg;

547 548
	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
	if (IS_ERR(mailbox))
549 550
		goto err_out_free_mtt;

551
	mpt_entry = mailbox->buf;
552 553 554 555 556 557

	mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS     |
				       MTHCA_MPT_FLAG_MIO         |
				       MTHCA_MPT_FLAG_REGION      |
				       access);

558
	mpt_entry->page_size = cpu_to_be32(mr->attr.page_shift - 12);
559 560 561 562 563 564 565 566 567 568 569
	mpt_entry->key       = cpu_to_be32(key);
	mpt_entry->pd        = cpu_to_be32(pd);
	memset(&mpt_entry->start, 0,
	       sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, start));
	mpt_entry->mtt_seg   = cpu_to_be64(dev->mr_table.mtt_base + mtt_seg);

	if (0) {
		mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey);
		for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) {
			if (i % 4 == 0)
				printk("[%02x] ", i * 4);
570
			printk(" %08x", be32_to_cpu(((__be32 *) mpt_entry)[i]));
571 572 573 574 575
			if ((i + 1) % 4 == 0)
				printk("\n");
		}
	}

576
	err = mthca_SW2HW_MPT(dev, mailbox,
577 578 579 580 581 582 583 584 585 586 587 588 589
			      key & (dev->limits.num_mpts - 1),
			      &status);
	if (err) {
		mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
		goto err_out_mailbox_free;
	}
	if (status) {
		mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n",
			   status);
		err = -EINVAL;
		goto err_out_mailbox_free;
	}

590
	mthca_free_mailbox(dev, mailbox);
591 592 593
	return 0;

err_out_mailbox_free:
594
	mthca_free_mailbox(dev, mailbox);
595 596

err_out_free_mtt:
597
	mthca_free_mtt(dev, mr->mtt);
L
Linus Torvalds 已提交
598

599
err_out_table:
600
	mthca_table_put(dev, dev->mr_table.mpt_table, key);
601 602 603 604 605 606 607 608 609 610 611

err_out_mpt_free:
	mthca_free(&dev->mr_table.mpt_alloc, mr->ibmr.lkey);
	return err;
}

int mthca_free_fmr(struct mthca_dev *dev, struct mthca_fmr *fmr)
{
	if (fmr->maps)
		return -EBUSY;

612 613 614
	mthca_free_region(dev, fmr->ibmr.lkey);
	mthca_free_mtt(dev, fmr->mtt);

615 616 617 618 619 620 621 622 623 624 625
	return 0;
}

static inline int mthca_check_fmr(struct mthca_fmr *fmr, u64 *page_list,
				  int list_len, u64 iova)
{
	int i, page_mask;

	if (list_len > fmr->attr.max_pages)
		return -EINVAL;

626
	page_mask = (1 << fmr->attr.page_shift) - 1;
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673

	/* We are getting page lists, so va must be page aligned. */
	if (iova & page_mask)
		return -EINVAL;

	/* Trust the user not to pass misaligned data in page_list */
	if (0)
		for (i = 0; i < list_len; ++i) {
			if (page_list[i] & ~page_mask)
				return -EINVAL;
		}

	if (fmr->maps >= fmr->attr.max_maps)
		return -EINVAL;

	return 0;
}


int mthca_tavor_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
			     int list_len, u64 iova)
{
	struct mthca_fmr *fmr = to_mfmr(ibfmr);
	struct mthca_dev *dev = to_mdev(ibfmr->device);
	struct mthca_mpt_entry mpt_entry;
	u32 key;
	int i, err;

	err = mthca_check_fmr(fmr, page_list, list_len, iova);
	if (err)
		return err;

	++fmr->maps;

	key = tavor_key_to_hw_index(fmr->ibmr.lkey);
	key += dev->limits.num_mpts;
	fmr->ibmr.lkey = fmr->ibmr.rkey = tavor_hw_index_to_key(key);

	writeb(MTHCA_MPT_STATUS_SW, fmr->mem.tavor.mpt);

	for (i = 0; i < list_len; ++i) {
		__be64 mtt_entry = cpu_to_be64(page_list[i] |
					       MTHCA_MTT_FLAG_PRESENT);
		mthca_write64_raw(mtt_entry, fmr->mem.tavor.mtts + i);
	}

	mpt_entry.lkey   = cpu_to_be32(key);
674
	mpt_entry.length = cpu_to_be64(list_len * (1ull << fmr->attr.page_shift));
675 676
	mpt_entry.start  = cpu_to_be64(iova);

677
	__raw_writel((__force u32) mpt_entry.lkey, &fmr->mem.tavor.mpt->key);
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
	memcpy_toio(&fmr->mem.tavor.mpt->start, &mpt_entry.start,
		    offsetof(struct mthca_mpt_entry, window_count) -
		    offsetof(struct mthca_mpt_entry, start));

	writeb(MTHCA_MPT_STATUS_HW, fmr->mem.tavor.mpt);

	return 0;
}

int mthca_arbel_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
			     int list_len, u64 iova)
{
	struct mthca_fmr *fmr = to_mfmr(ibfmr);
	struct mthca_dev *dev = to_mdev(ibfmr->device);
	u32 key;
	int i, err;

	err = mthca_check_fmr(fmr, page_list, list_len, iova);
	if (err)
		return err;

	++fmr->maps;

	key = arbel_key_to_hw_index(fmr->ibmr.lkey);
702 703 704 705
	if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
		key += SINAI_FMR_KEY_INC;
	else
		key += dev->limits.num_mpts;
706 707 708 709 710 711 712 713 714 715 716 717
	fmr->ibmr.lkey = fmr->ibmr.rkey = arbel_hw_index_to_key(key);

	*(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_SW;

	wmb();

	for (i = 0; i < list_len; ++i)
		fmr->mem.arbel.mtts[i] = cpu_to_be64(page_list[i] |
						     MTHCA_MTT_FLAG_PRESENT);

	fmr->mem.arbel.mpt->key    = cpu_to_be32(key);
	fmr->mem.arbel.mpt->lkey   = cpu_to_be32(key);
718
	fmr->mem.arbel.mpt->length = cpu_to_be64(list_len * (1ull << fmr->attr.page_shift));
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
	fmr->mem.arbel.mpt->start  = cpu_to_be64(iova);

	wmb();

	*(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_HW;

	wmb();

	return 0;
}

void mthca_tavor_fmr_unmap(struct mthca_dev *dev, struct mthca_fmr *fmr)
{
	u32 key;

	if (!fmr->maps)
		return;

	key = tavor_key_to_hw_index(fmr->ibmr.lkey);
	key &= dev->limits.num_mpts - 1;
	fmr->ibmr.lkey = fmr->ibmr.rkey = tavor_hw_index_to_key(key);

	fmr->maps = 0;

	writeb(MTHCA_MPT_STATUS_SW, fmr->mem.tavor.mpt);
}

void mthca_arbel_fmr_unmap(struct mthca_dev *dev, struct mthca_fmr *fmr)
{
	u32 key;

	if (!fmr->maps)
		return;

	key = arbel_key_to_hw_index(fmr->ibmr.lkey);
	key &= dev->limits.num_mpts - 1;
	fmr->ibmr.lkey = fmr->ibmr.rkey = arbel_hw_index_to_key(key);

	fmr->maps = 0;

	*(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_SW;
L
Linus Torvalds 已提交
760 761 762 763
}

int __devinit mthca_init_mr_table(struct mthca_dev *dev)
{
764
	int err, i;
L
Linus Torvalds 已提交
765 766 767 768 769 770 771

	err = mthca_alloc_init(&dev->mr_table.mpt_alloc,
			       dev->limits.num_mpts,
			       ~0, dev->limits.reserved_mrws);
	if (err)
		return err;

772
	if (!mthca_is_memfree(dev) &&
773 774 775 776 777
	    (dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN))
		dev->limits.fmr_reserved_mtts = 0;
	else
		dev->mthca_flags |= MTHCA_FLAG_FMR;

778 779 780
	if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
		mthca_dbg(dev, "Memory key throughput optimization activated.\n");

781 782
	err = mthca_buddy_init(&dev->mr_table.mtt_buddy,
			       fls(dev->limits.num_mtt_segs - 1));
783

784 785 786
	if (err)
		goto err_mtt_buddy;

787 788 789 790 791 792 793 794 795 796 797 798 799
	dev->mr_table.tavor_fmr.mpt_base = NULL;
	dev->mr_table.tavor_fmr.mtt_base = NULL;

	if (dev->limits.fmr_reserved_mtts) {
		i = fls(dev->limits.fmr_reserved_mtts - 1);

		if (i >= 31) {
			mthca_warn(dev, "Unable to reserve 2^31 FMR MTTs.\n");
			err = -EINVAL;
			goto err_fmr_mpt;
		}

		dev->mr_table.tavor_fmr.mpt_base =
R
Roland Dreier 已提交
800
			ioremap(dev->mr_table.mpt_base,
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
				(1 << i) * sizeof (struct mthca_mpt_entry));

		if (!dev->mr_table.tavor_fmr.mpt_base) {
			mthca_warn(dev, "MPT ioremap for FMR failed.\n");
			err = -ENOMEM;
			goto err_fmr_mpt;
		}

		dev->mr_table.tavor_fmr.mtt_base =
			ioremap(dev->mr_table.mtt_base,
				(1 << i) * MTHCA_MTT_SEG_SIZE);
		if (!dev->mr_table.tavor_fmr.mtt_base) {
			mthca_warn(dev, "MTT ioremap for FMR failed.\n");
			err = -ENOMEM;
			goto err_fmr_mtt;
		}

		err = mthca_buddy_init(&dev->mr_table.tavor_fmr.mtt_buddy, i);
		if (err)
			goto err_fmr_mtt_buddy;

		/* Prevent regular MRs from using FMR keys */
		err = mthca_buddy_alloc(&dev->mr_table.mtt_buddy, i);
		if (err)
			goto err_reserve_fmr;

		dev->mr_table.fmr_mtt_buddy =
R
Roland Dreier 已提交
828
			&dev->mr_table.tavor_fmr.mtt_buddy;
829 830 831 832
	} else
		dev->mr_table.fmr_mtt_buddy = &dev->mr_table.mtt_buddy;

	/* FMR table is always the first, take reserved MTTs out of there */
833
	if (dev->limits.reserved_mtts) {
834 835
		i = fls(dev->limits.reserved_mtts - 1);

836 837
		if (mthca_alloc_mtt_range(dev, i,
					  dev->mr_table.fmr_mtt_buddy) == -1) {
838
			mthca_warn(dev, "MTT table of order %d is too small.\n",
839
				  dev->mr_table.fmr_mtt_buddy->max_order);
840
			err = -ENOMEM;
841
			goto err_reserve_mtts;
842
		}
L
Linus Torvalds 已提交
843 844 845 846
	}

	return 0;

847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
err_reserve_mtts:
err_reserve_fmr:
	if (dev->limits.fmr_reserved_mtts)
		mthca_buddy_cleanup(&dev->mr_table.tavor_fmr.mtt_buddy);

err_fmr_mtt_buddy:
	if (dev->mr_table.tavor_fmr.mtt_base)
		iounmap(dev->mr_table.tavor_fmr.mtt_base);

err_fmr_mtt:
	if (dev->mr_table.tavor_fmr.mpt_base)
		iounmap(dev->mr_table.tavor_fmr.mpt_base);

err_fmr_mpt:
	mthca_buddy_cleanup(&dev->mr_table.mtt_buddy);

863
err_mtt_buddy:
L
Linus Torvalds 已提交
864 865 866 867 868
	mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);

	return err;
}

869
void mthca_cleanup_mr_table(struct mthca_dev *dev)
L
Linus Torvalds 已提交
870 871
{
	/* XXX check if any MRs are still allocated? */
872 873 874
	if (dev->limits.fmr_reserved_mtts)
		mthca_buddy_cleanup(&dev->mr_table.tavor_fmr.mtt_buddy);

875
	mthca_buddy_cleanup(&dev->mr_table.mtt_buddy);
876 877 878 879 880 881

	if (dev->mr_table.tavor_fmr.mtt_base)
		iounmap(dev->mr_table.tavor_fmr.mtt_base);
	if (dev->mr_table.tavor_fmr.mpt_base)
		iounmap(dev->mr_table.tavor_fmr.mpt_base);

L
Linus Torvalds 已提交
882 883
	mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);
}