xref: /freebsd/sys/dev/mthca/mthca_mr.c (revision 3cb02df9ae0f230d8306c65535291d4db85393d3)
1*33ec1ccbSHans Petter Selasky /*
2*33ec1ccbSHans Petter Selasky  * Copyright (c) 2004 Topspin Communications.  All rights reserved.
3*33ec1ccbSHans Petter Selasky  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
4*33ec1ccbSHans Petter Selasky  *
5*33ec1ccbSHans Petter Selasky  * This software is available to you under a choice of one of two
6*33ec1ccbSHans Petter Selasky  * licenses.  You may choose to be licensed under the terms of the GNU
7*33ec1ccbSHans Petter Selasky  * General Public License (GPL) Version 2, available from the file
8*33ec1ccbSHans Petter Selasky  * COPYING in the main directory of this source tree, or the
9*33ec1ccbSHans Petter Selasky  * OpenIB.org BSD license below:
10*33ec1ccbSHans Petter Selasky  *
11*33ec1ccbSHans Petter Selasky  *     Redistribution and use in source and binary forms, with or
12*33ec1ccbSHans Petter Selasky  *     without modification, are permitted provided that the following
13*33ec1ccbSHans Petter Selasky  *     conditions are met:
14*33ec1ccbSHans Petter Selasky  *
15*33ec1ccbSHans Petter Selasky  *      - Redistributions of source code must retain the above
16*33ec1ccbSHans Petter Selasky  *        copyright notice, this list of conditions and the following
17*33ec1ccbSHans Petter Selasky  *        disclaimer.
18*33ec1ccbSHans Petter Selasky  *
19*33ec1ccbSHans Petter Selasky  *      - Redistributions in binary form must reproduce the above
20*33ec1ccbSHans Petter Selasky  *        copyright notice, this list of conditions and the following
21*33ec1ccbSHans Petter Selasky  *        disclaimer in the documentation and/or other materials
22*33ec1ccbSHans Petter Selasky  *        provided with the distribution.
23*33ec1ccbSHans Petter Selasky  *
24*33ec1ccbSHans Petter Selasky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25*33ec1ccbSHans Petter Selasky  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26*33ec1ccbSHans Petter Selasky  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27*33ec1ccbSHans Petter Selasky  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28*33ec1ccbSHans Petter Selasky  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29*33ec1ccbSHans Petter Selasky  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30*33ec1ccbSHans Petter Selasky  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31*33ec1ccbSHans Petter Selasky  * SOFTWARE.
32*33ec1ccbSHans Petter Selasky  */
33*33ec1ccbSHans Petter Selasky 
34*33ec1ccbSHans Petter Selasky #include <linux/slab.h>
35*33ec1ccbSHans Petter Selasky #include <linux/errno.h>
36*33ec1ccbSHans Petter Selasky 
37*33ec1ccbSHans Petter Selasky #include "mthca_dev.h"
38*33ec1ccbSHans Petter Selasky #include "mthca_cmd.h"
39*33ec1ccbSHans Petter Selasky #include "mthca_memfree.h"
40*33ec1ccbSHans Petter Selasky 
41*33ec1ccbSHans Petter Selasky struct mthca_mtt {
42*33ec1ccbSHans Petter Selasky 	struct mthca_buddy *buddy;
43*33ec1ccbSHans Petter Selasky 	int                 order;
44*33ec1ccbSHans Petter Selasky 	u32                 first_seg;
45*33ec1ccbSHans Petter Selasky };
46*33ec1ccbSHans Petter Selasky 
47*33ec1ccbSHans Petter Selasky /*
48*33ec1ccbSHans Petter Selasky  * Must be packed because mtt_seg is 64 bits but only aligned to 32 bits.
49*33ec1ccbSHans Petter Selasky  */
50*33ec1ccbSHans Petter Selasky struct mthca_mpt_entry {
51*33ec1ccbSHans Petter Selasky 	__be32 flags;
52*33ec1ccbSHans Petter Selasky 	__be32 page_size;
53*33ec1ccbSHans Petter Selasky 	__be32 key;
54*33ec1ccbSHans Petter Selasky 	__be32 pd;
55*33ec1ccbSHans Petter Selasky 	__be64 start;
56*33ec1ccbSHans Petter Selasky 	__be64 length;
57*33ec1ccbSHans Petter Selasky 	__be32 lkey;
58*33ec1ccbSHans Petter Selasky 	__be32 window_count;
59*33ec1ccbSHans Petter Selasky 	__be32 window_count_limit;
60*33ec1ccbSHans Petter Selasky 	__be64 mtt_seg;
61*33ec1ccbSHans Petter Selasky 	__be32 mtt_sz;		/* Arbel only */
62*33ec1ccbSHans Petter Selasky 	u32    reserved[2];
63*33ec1ccbSHans Petter Selasky } __attribute__((packed));
64*33ec1ccbSHans Petter Selasky 
65*33ec1ccbSHans Petter Selasky #define MTHCA_MPT_FLAG_SW_OWNS       (0xfUL << 28)
66*33ec1ccbSHans Petter Selasky #define MTHCA_MPT_FLAG_MIO           (1 << 17)
67*33ec1ccbSHans Petter Selasky #define MTHCA_MPT_FLAG_BIND_ENABLE   (1 << 15)
68*33ec1ccbSHans Petter Selasky #define MTHCA_MPT_FLAG_PHYSICAL      (1 <<  9)
69*33ec1ccbSHans Petter Selasky #define MTHCA_MPT_FLAG_REGION        (1 <<  8)
70*33ec1ccbSHans Petter Selasky 
71*33ec1ccbSHans Petter Selasky #define MTHCA_MTT_FLAG_PRESENT       1
72*33ec1ccbSHans Petter Selasky 
73*33ec1ccbSHans Petter Selasky #define MTHCA_MPT_STATUS_SW 0xF0
74*33ec1ccbSHans Petter Selasky #define MTHCA_MPT_STATUS_HW 0x00
75*33ec1ccbSHans Petter Selasky 
76*33ec1ccbSHans Petter Selasky #define SINAI_FMR_KEY_INC 0x1000000
77*33ec1ccbSHans Petter Selasky 
78*33ec1ccbSHans Petter Selasky /*
79*33ec1ccbSHans Petter Selasky  * Buddy allocator for MTT segments (currently not very efficient
80*33ec1ccbSHans Petter Selasky  * since it doesn't keep a free list and just searches linearly
81*33ec1ccbSHans Petter Selasky  * through the bitmaps)
82*33ec1ccbSHans Petter Selasky  */
83*33ec1ccbSHans Petter Selasky 
mthca_buddy_alloc(struct mthca_buddy * buddy,int order)84*33ec1ccbSHans Petter Selasky static u32 mthca_buddy_alloc(struct mthca_buddy *buddy, int order)
85*33ec1ccbSHans Petter Selasky {
86*33ec1ccbSHans Petter Selasky 	int o;
87*33ec1ccbSHans Petter Selasky 	int m;
88*33ec1ccbSHans Petter Selasky 	u32 seg;
89*33ec1ccbSHans Petter Selasky 
90*33ec1ccbSHans Petter Selasky 	spin_lock(&buddy->lock);
91*33ec1ccbSHans Petter Selasky 
92*33ec1ccbSHans Petter Selasky 	for (o = order; o <= buddy->max_order; ++o)
93*33ec1ccbSHans Petter Selasky 		if (buddy->num_free[o]) {
94*33ec1ccbSHans Petter Selasky 			m = 1 << (buddy->max_order - o);
95*33ec1ccbSHans Petter Selasky 			seg = find_first_bit(buddy->bits[o], m);
96*33ec1ccbSHans Petter Selasky 			if (seg < m)
97*33ec1ccbSHans Petter Selasky 				goto found;
98*33ec1ccbSHans Petter Selasky 		}
99*33ec1ccbSHans Petter Selasky 
100*33ec1ccbSHans Petter Selasky 	spin_unlock(&buddy->lock);
101*33ec1ccbSHans Petter Selasky 	return -1;
102*33ec1ccbSHans Petter Selasky 
103*33ec1ccbSHans Petter Selasky  found:
104*33ec1ccbSHans Petter Selasky 	clear_bit(seg, buddy->bits[o]);
105*33ec1ccbSHans Petter Selasky 	--buddy->num_free[o];
106*33ec1ccbSHans Petter Selasky 
107*33ec1ccbSHans Petter Selasky 	while (o > order) {
108*33ec1ccbSHans Petter Selasky 		--o;
109*33ec1ccbSHans Petter Selasky 		seg <<= 1;
110*33ec1ccbSHans Petter Selasky 		set_bit(seg ^ 1, buddy->bits[o]);
111*33ec1ccbSHans Petter Selasky 		++buddy->num_free[o];
112*33ec1ccbSHans Petter Selasky 	}
113*33ec1ccbSHans Petter Selasky 
114*33ec1ccbSHans Petter Selasky 	spin_unlock(&buddy->lock);
115*33ec1ccbSHans Petter Selasky 
116*33ec1ccbSHans Petter Selasky 	seg <<= order;
117*33ec1ccbSHans Petter Selasky 
118*33ec1ccbSHans Petter Selasky 	return seg;
119*33ec1ccbSHans Petter Selasky }
120*33ec1ccbSHans Petter Selasky 
mthca_buddy_free(struct mthca_buddy * buddy,u32 seg,int order)121*33ec1ccbSHans Petter Selasky static void mthca_buddy_free(struct mthca_buddy *buddy, u32 seg, int order)
122*33ec1ccbSHans Petter Selasky {
123*33ec1ccbSHans Petter Selasky 	seg >>= order;
124*33ec1ccbSHans Petter Selasky 
125*33ec1ccbSHans Petter Selasky 	spin_lock(&buddy->lock);
126*33ec1ccbSHans Petter Selasky 
127*33ec1ccbSHans Petter Selasky 	while (test_bit(seg ^ 1, buddy->bits[order])) {
128*33ec1ccbSHans Petter Selasky 		clear_bit(seg ^ 1, buddy->bits[order]);
129*33ec1ccbSHans Petter Selasky 		--buddy->num_free[order];
130*33ec1ccbSHans Petter Selasky 		seg >>= 1;
131*33ec1ccbSHans Petter Selasky 		++order;
132*33ec1ccbSHans Petter Selasky 	}
133*33ec1ccbSHans Petter Selasky 
134*33ec1ccbSHans Petter Selasky 	set_bit(seg, buddy->bits[order]);
135*33ec1ccbSHans Petter Selasky 	++buddy->num_free[order];
136*33ec1ccbSHans Petter Selasky 
137*33ec1ccbSHans Petter Selasky 	spin_unlock(&buddy->lock);
138*33ec1ccbSHans Petter Selasky }
139*33ec1ccbSHans Petter Selasky 
mthca_buddy_init(struct mthca_buddy * buddy,int max_order)140*33ec1ccbSHans Petter Selasky static int mthca_buddy_init(struct mthca_buddy *buddy, int max_order)
141*33ec1ccbSHans Petter Selasky {
142*33ec1ccbSHans Petter Selasky 	int i, s;
143*33ec1ccbSHans Petter Selasky 
144*33ec1ccbSHans Petter Selasky 	buddy->max_order = max_order;
145*33ec1ccbSHans Petter Selasky 	spin_lock_init(&buddy->lock);
146*33ec1ccbSHans Petter Selasky 
147*33ec1ccbSHans Petter Selasky 	buddy->bits = kzalloc((buddy->max_order + 1) * sizeof (long *),
148*33ec1ccbSHans Petter Selasky 			      GFP_KERNEL);
149*33ec1ccbSHans Petter Selasky 	buddy->num_free = kcalloc((buddy->max_order + 1), sizeof *buddy->num_free,
150*33ec1ccbSHans Petter Selasky 				  GFP_KERNEL);
151*33ec1ccbSHans Petter Selasky 	if (!buddy->bits || !buddy->num_free)
152*33ec1ccbSHans Petter Selasky 		goto err_out;
153*33ec1ccbSHans Petter Selasky 
154*33ec1ccbSHans Petter Selasky 	for (i = 0; i <= buddy->max_order; ++i) {
155*33ec1ccbSHans Petter Selasky 		s = BITS_TO_LONGS(1 << (buddy->max_order - i));
156*33ec1ccbSHans Petter Selasky 		buddy->bits[i] = kmalloc(s * sizeof (long), GFP_KERNEL);
157*33ec1ccbSHans Petter Selasky 		if (!buddy->bits[i])
158*33ec1ccbSHans Petter Selasky 			goto err_out_free;
159*33ec1ccbSHans Petter Selasky 		bitmap_zero(buddy->bits[i],
160*33ec1ccbSHans Petter Selasky 			    1 << (buddy->max_order - i));
161*33ec1ccbSHans Petter Selasky 	}
162*33ec1ccbSHans Petter Selasky 
163*33ec1ccbSHans Petter Selasky 	set_bit(0, buddy->bits[buddy->max_order]);
164*33ec1ccbSHans Petter Selasky 	buddy->num_free[buddy->max_order] = 1;
165*33ec1ccbSHans Petter Selasky 
166*33ec1ccbSHans Petter Selasky 	return 0;
167*33ec1ccbSHans Petter Selasky 
168*33ec1ccbSHans Petter Selasky err_out_free:
169*33ec1ccbSHans Petter Selasky 	for (i = 0; i <= buddy->max_order; ++i)
170*33ec1ccbSHans Petter Selasky 		kfree(buddy->bits[i]);
171*33ec1ccbSHans Petter Selasky 
172*33ec1ccbSHans Petter Selasky err_out:
173*33ec1ccbSHans Petter Selasky 	kfree(buddy->bits);
174*33ec1ccbSHans Petter Selasky 	kfree(buddy->num_free);
175*33ec1ccbSHans Petter Selasky 
176*33ec1ccbSHans Petter Selasky 	return -ENOMEM;
177*33ec1ccbSHans Petter Selasky }
178*33ec1ccbSHans Petter Selasky 
mthca_buddy_cleanup(struct mthca_buddy * buddy)179*33ec1ccbSHans Petter Selasky static void mthca_buddy_cleanup(struct mthca_buddy *buddy)
180*33ec1ccbSHans Petter Selasky {
181*33ec1ccbSHans Petter Selasky 	int i;
182*33ec1ccbSHans Petter Selasky 
183*33ec1ccbSHans Petter Selasky 	for (i = 0; i <= buddy->max_order; ++i)
184*33ec1ccbSHans Petter Selasky 		kfree(buddy->bits[i]);
185*33ec1ccbSHans Petter Selasky 
186*33ec1ccbSHans Petter Selasky 	kfree(buddy->bits);
187*33ec1ccbSHans Petter Selasky 	kfree(buddy->num_free);
188*33ec1ccbSHans Petter Selasky }
189*33ec1ccbSHans Petter Selasky 
mthca_alloc_mtt_range(struct mthca_dev * dev,int order,struct mthca_buddy * buddy)190*33ec1ccbSHans Petter Selasky static u32 mthca_alloc_mtt_range(struct mthca_dev *dev, int order,
191*33ec1ccbSHans Petter Selasky 				 struct mthca_buddy *buddy)
192*33ec1ccbSHans Petter Selasky {
193*33ec1ccbSHans Petter Selasky 	u32 seg = mthca_buddy_alloc(buddy, order);
194*33ec1ccbSHans Petter Selasky 
195*33ec1ccbSHans Petter Selasky 	if (seg == -1)
196*33ec1ccbSHans Petter Selasky 		return -1;
197*33ec1ccbSHans Petter Selasky 
198*33ec1ccbSHans Petter Selasky 	if (mthca_is_memfree(dev))
199*33ec1ccbSHans Petter Selasky 		if (mthca_table_get_range(dev, dev->mr_table.mtt_table, seg,
200*33ec1ccbSHans Petter Selasky 					  seg + (1 << order) - 1)) {
201*33ec1ccbSHans Petter Selasky 			mthca_buddy_free(buddy, seg, order);
202*33ec1ccbSHans Petter Selasky 			seg = -1;
203*33ec1ccbSHans Petter Selasky 		}
204*33ec1ccbSHans Petter Selasky 
205*33ec1ccbSHans Petter Selasky 	return seg;
206*33ec1ccbSHans Petter Selasky }
207*33ec1ccbSHans Petter Selasky 
__mthca_alloc_mtt(struct mthca_dev * dev,int size,struct mthca_buddy * buddy)208*33ec1ccbSHans Petter Selasky static struct mthca_mtt *__mthca_alloc_mtt(struct mthca_dev *dev, int size,
209*33ec1ccbSHans Petter Selasky 					   struct mthca_buddy *buddy)
210*33ec1ccbSHans Petter Selasky {
211*33ec1ccbSHans Petter Selasky 	struct mthca_mtt *mtt;
212*33ec1ccbSHans Petter Selasky 	int i;
213*33ec1ccbSHans Petter Selasky 
214*33ec1ccbSHans Petter Selasky 	if (size <= 0)
215*33ec1ccbSHans Petter Selasky 		return ERR_PTR(-EINVAL);
216*33ec1ccbSHans Petter Selasky 
217*33ec1ccbSHans Petter Selasky 	mtt = kmalloc(sizeof *mtt, GFP_KERNEL);
218*33ec1ccbSHans Petter Selasky 	if (!mtt)
219*33ec1ccbSHans Petter Selasky 		return ERR_PTR(-ENOMEM);
220*33ec1ccbSHans Petter Selasky 
221*33ec1ccbSHans Petter Selasky 	mtt->buddy = buddy;
222*33ec1ccbSHans Petter Selasky 	mtt->order = 0;
223*33ec1ccbSHans Petter Selasky 	for (i = dev->limits.mtt_seg_size / 8; i < size; i <<= 1)
224*33ec1ccbSHans Petter Selasky 		++mtt->order;
225*33ec1ccbSHans Petter Selasky 
226*33ec1ccbSHans Petter Selasky 	mtt->first_seg = mthca_alloc_mtt_range(dev, mtt->order, buddy);
227*33ec1ccbSHans Petter Selasky 	if (mtt->first_seg == -1) {
228*33ec1ccbSHans Petter Selasky 		kfree(mtt);
229*33ec1ccbSHans Petter Selasky 		return ERR_PTR(-ENOMEM);
230*33ec1ccbSHans Petter Selasky 	}
231*33ec1ccbSHans Petter Selasky 
232*33ec1ccbSHans Petter Selasky 	return mtt;
233*33ec1ccbSHans Petter Selasky }
234*33ec1ccbSHans Petter Selasky 
mthca_alloc_mtt(struct mthca_dev * dev,int size)235*33ec1ccbSHans Petter Selasky struct mthca_mtt *mthca_alloc_mtt(struct mthca_dev *dev, int size)
236*33ec1ccbSHans Petter Selasky {
237*33ec1ccbSHans Petter Selasky 	return __mthca_alloc_mtt(dev, size, &dev->mr_table.mtt_buddy);
238*33ec1ccbSHans Petter Selasky }
239*33ec1ccbSHans Petter Selasky 
mthca_free_mtt(struct mthca_dev * dev,struct mthca_mtt * mtt)240*33ec1ccbSHans Petter Selasky void mthca_free_mtt(struct mthca_dev *dev, struct mthca_mtt *mtt)
241*33ec1ccbSHans Petter Selasky {
242*33ec1ccbSHans Petter Selasky 	if (!mtt)
243*33ec1ccbSHans Petter Selasky 		return;
244*33ec1ccbSHans Petter Selasky 
245*33ec1ccbSHans Petter Selasky 	mthca_buddy_free(mtt->buddy, mtt->first_seg, mtt->order);
246*33ec1ccbSHans Petter Selasky 
247*33ec1ccbSHans Petter Selasky 	mthca_table_put_range(dev, dev->mr_table.mtt_table,
248*33ec1ccbSHans Petter Selasky 			      mtt->first_seg,
249*33ec1ccbSHans Petter Selasky 			      mtt->first_seg + (1 << mtt->order) - 1);
250*33ec1ccbSHans Petter Selasky 
251*33ec1ccbSHans Petter Selasky 	kfree(mtt);
252*33ec1ccbSHans Petter Selasky }
253*33ec1ccbSHans Petter Selasky 
__mthca_write_mtt(struct mthca_dev * dev,struct mthca_mtt * mtt,int start_index,u64 * buffer_list,int list_len)254*33ec1ccbSHans Petter Selasky static int __mthca_write_mtt(struct mthca_dev *dev, struct mthca_mtt *mtt,
255*33ec1ccbSHans Petter Selasky 			     int start_index, u64 *buffer_list, int list_len)
256*33ec1ccbSHans Petter Selasky {
257*33ec1ccbSHans Petter Selasky 	struct mthca_mailbox *mailbox;
258*33ec1ccbSHans Petter Selasky 	__be64 *mtt_entry;
259*33ec1ccbSHans Petter Selasky 	int err = 0;
260*33ec1ccbSHans Petter Selasky 	int i;
261*33ec1ccbSHans Petter Selasky 
262*33ec1ccbSHans Petter Selasky 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
263*33ec1ccbSHans Petter Selasky 	if (IS_ERR(mailbox))
264*33ec1ccbSHans Petter Selasky 		return PTR_ERR(mailbox);
265*33ec1ccbSHans Petter Selasky 	mtt_entry = mailbox->buf;
266*33ec1ccbSHans Petter Selasky 
267*33ec1ccbSHans Petter Selasky 	while (list_len > 0) {
268*33ec1ccbSHans Petter Selasky 		mtt_entry[0] = cpu_to_be64(dev->mr_table.mtt_base +
269*33ec1ccbSHans Petter Selasky 					   mtt->first_seg * dev->limits.mtt_seg_size +
270*33ec1ccbSHans Petter Selasky 					   start_index * 8);
271*33ec1ccbSHans Petter Selasky 		mtt_entry[1] = 0;
272*33ec1ccbSHans Petter Selasky 		for (i = 0; i < list_len && i < MTHCA_MAILBOX_SIZE / 8 - 2; ++i)
273*33ec1ccbSHans Petter Selasky 			mtt_entry[i + 2] = cpu_to_be64(buffer_list[i] |
274*33ec1ccbSHans Petter Selasky 						       MTHCA_MTT_FLAG_PRESENT);
275*33ec1ccbSHans Petter Selasky 
276*33ec1ccbSHans Petter Selasky 		/*
277*33ec1ccbSHans Petter Selasky 		 * If we have an odd number of entries to write, add
278*33ec1ccbSHans Petter Selasky 		 * one more dummy entry for firmware efficiency.
279*33ec1ccbSHans Petter Selasky 		 */
280*33ec1ccbSHans Petter Selasky 		if (i & 1)
281*33ec1ccbSHans Petter Selasky 			mtt_entry[i + 2] = 0;
282*33ec1ccbSHans Petter Selasky 
283*33ec1ccbSHans Petter Selasky 		err = mthca_WRITE_MTT(dev, mailbox, (i + 1) & ~1);
284*33ec1ccbSHans Petter Selasky 		if (err) {
285*33ec1ccbSHans Petter Selasky 			mthca_warn(dev, "WRITE_MTT failed (%d)\n", err);
286*33ec1ccbSHans Petter Selasky 			goto out;
287*33ec1ccbSHans Petter Selasky 		}
288*33ec1ccbSHans Petter Selasky 
289*33ec1ccbSHans Petter Selasky 		list_len    -= i;
290*33ec1ccbSHans Petter Selasky 		start_index += i;
291*33ec1ccbSHans Petter Selasky 		buffer_list += i;
292*33ec1ccbSHans Petter Selasky 	}
293*33ec1ccbSHans Petter Selasky 
294*33ec1ccbSHans Petter Selasky out:
295*33ec1ccbSHans Petter Selasky 	mthca_free_mailbox(dev, mailbox);
296*33ec1ccbSHans Petter Selasky 	return err;
297*33ec1ccbSHans Petter Selasky }
298*33ec1ccbSHans Petter Selasky 
mthca_write_mtt_size(struct mthca_dev * dev)299*33ec1ccbSHans Petter Selasky int mthca_write_mtt_size(struct mthca_dev *dev)
300*33ec1ccbSHans Petter Selasky {
301*33ec1ccbSHans Petter Selasky 	if (dev->mr_table.fmr_mtt_buddy != &dev->mr_table.mtt_buddy ||
302*33ec1ccbSHans Petter Selasky 	    !(dev->mthca_flags & MTHCA_FLAG_FMR))
303*33ec1ccbSHans Petter Selasky 		/*
304*33ec1ccbSHans Petter Selasky 		 * Be friendly to WRITE_MTT command
305*33ec1ccbSHans Petter Selasky 		 * and leave two empty slots for the
306*33ec1ccbSHans Petter Selasky 		 * index and reserved fields of the
307*33ec1ccbSHans Petter Selasky 		 * mailbox.
308*33ec1ccbSHans Petter Selasky 		 */
309*33ec1ccbSHans Petter Selasky 		return PAGE_SIZE / sizeof (u64) - 2;
310*33ec1ccbSHans Petter Selasky 
311*33ec1ccbSHans Petter Selasky 	/* For Arbel, all MTTs must fit in the same page. */
312*33ec1ccbSHans Petter Selasky 	return mthca_is_memfree(dev) ? (PAGE_SIZE / sizeof (u64)) : 0x7ffffff;
313*33ec1ccbSHans Petter Selasky }
314*33ec1ccbSHans Petter Selasky 
mthca_tavor_write_mtt_seg(struct mthca_dev * dev,struct mthca_mtt * mtt,int start_index,u64 * buffer_list,int list_len)315*33ec1ccbSHans Petter Selasky static void mthca_tavor_write_mtt_seg(struct mthca_dev *dev,
316*33ec1ccbSHans Petter Selasky 				      struct mthca_mtt *mtt, int start_index,
317*33ec1ccbSHans Petter Selasky 				      u64 *buffer_list, int list_len)
318*33ec1ccbSHans Petter Selasky {
319*33ec1ccbSHans Petter Selasky 	u64 __iomem *mtts;
320*33ec1ccbSHans Petter Selasky 	int i;
321*33ec1ccbSHans Petter Selasky 
322*33ec1ccbSHans Petter Selasky 	mtts = dev->mr_table.tavor_fmr.mtt_base + mtt->first_seg * dev->limits.mtt_seg_size +
323*33ec1ccbSHans Petter Selasky 		start_index * sizeof (u64);
324*33ec1ccbSHans Petter Selasky 	for (i = 0; i < list_len; ++i)
325*33ec1ccbSHans Petter Selasky 		mthca_write64_raw(cpu_to_be64(buffer_list[i] | MTHCA_MTT_FLAG_PRESENT),
326*33ec1ccbSHans Petter Selasky 				  mtts + i);
327*33ec1ccbSHans Petter Selasky }
328*33ec1ccbSHans Petter Selasky 
mthca_arbel_write_mtt_seg(struct mthca_dev * dev,struct mthca_mtt * mtt,int start_index,u64 * buffer_list,int list_len)329*33ec1ccbSHans Petter Selasky static void mthca_arbel_write_mtt_seg(struct mthca_dev *dev,
330*33ec1ccbSHans Petter Selasky 				      struct mthca_mtt *mtt, int start_index,
331*33ec1ccbSHans Petter Selasky 				      u64 *buffer_list, int list_len)
332*33ec1ccbSHans Petter Selasky {
333*33ec1ccbSHans Petter Selasky 	__be64 *mtts;
334*33ec1ccbSHans Petter Selasky 	dma_addr_t dma_handle;
335*33ec1ccbSHans Petter Selasky 	int i;
336*33ec1ccbSHans Petter Selasky 	int s = start_index * sizeof (u64);
337*33ec1ccbSHans Petter Selasky 
338*33ec1ccbSHans Petter Selasky 	/* For Arbel, all MTTs must fit in the same page. */
339*33ec1ccbSHans Petter Selasky 	BUG_ON(s / PAGE_SIZE != (s + list_len * sizeof(u64) - 1) / PAGE_SIZE);
340*33ec1ccbSHans Petter Selasky 	/* Require full segments */
341*33ec1ccbSHans Petter Selasky 	BUG_ON(s % dev->limits.mtt_seg_size);
342*33ec1ccbSHans Petter Selasky 
343*33ec1ccbSHans Petter Selasky 	mtts = mthca_table_find(dev->mr_table.mtt_table, mtt->first_seg +
344*33ec1ccbSHans Petter Selasky 				s / dev->limits.mtt_seg_size, &dma_handle);
345*33ec1ccbSHans Petter Selasky 
346*33ec1ccbSHans Petter Selasky 	BUG_ON(!mtts);
347*33ec1ccbSHans Petter Selasky 
348*33ec1ccbSHans Petter Selasky 	dma_sync_single_for_cpu(&dev->pdev->dev, dma_handle,
349*33ec1ccbSHans Petter Selasky 				list_len * sizeof (u64), DMA_TO_DEVICE);
350*33ec1ccbSHans Petter Selasky 
351*33ec1ccbSHans Petter Selasky 	for (i = 0; i < list_len; ++i)
352*33ec1ccbSHans Petter Selasky 		mtts[i] = cpu_to_be64(buffer_list[i] | MTHCA_MTT_FLAG_PRESENT);
353*33ec1ccbSHans Petter Selasky 
354*33ec1ccbSHans Petter Selasky 	dma_sync_single_for_device(&dev->pdev->dev, dma_handle,
355*33ec1ccbSHans Petter Selasky 				   list_len * sizeof (u64), DMA_TO_DEVICE);
356*33ec1ccbSHans Petter Selasky }
357*33ec1ccbSHans Petter Selasky 
mthca_write_mtt(struct mthca_dev * dev,struct mthca_mtt * mtt,int start_index,u64 * buffer_list,int list_len)358*33ec1ccbSHans Petter Selasky int mthca_write_mtt(struct mthca_dev *dev, struct mthca_mtt *mtt,
359*33ec1ccbSHans Petter Selasky 		    int start_index, u64 *buffer_list, int list_len)
360*33ec1ccbSHans Petter Selasky {
361*33ec1ccbSHans Petter Selasky 	int size = mthca_write_mtt_size(dev);
362*33ec1ccbSHans Petter Selasky 	int chunk;
363*33ec1ccbSHans Petter Selasky 
364*33ec1ccbSHans Petter Selasky 	if (dev->mr_table.fmr_mtt_buddy != &dev->mr_table.mtt_buddy ||
365*33ec1ccbSHans Petter Selasky 	    !(dev->mthca_flags & MTHCA_FLAG_FMR))
366*33ec1ccbSHans Petter Selasky 		return __mthca_write_mtt(dev, mtt, start_index, buffer_list, list_len);
367*33ec1ccbSHans Petter Selasky 
368*33ec1ccbSHans Petter Selasky 	while (list_len > 0) {
369*33ec1ccbSHans Petter Selasky 		chunk = min(size, list_len);
370*33ec1ccbSHans Petter Selasky 		if (mthca_is_memfree(dev))
371*33ec1ccbSHans Petter Selasky 			mthca_arbel_write_mtt_seg(dev, mtt, start_index,
372*33ec1ccbSHans Petter Selasky 						  buffer_list, chunk);
373*33ec1ccbSHans Petter Selasky 		else
374*33ec1ccbSHans Petter Selasky 			mthca_tavor_write_mtt_seg(dev, mtt, start_index,
375*33ec1ccbSHans Petter Selasky 						  buffer_list, chunk);
376*33ec1ccbSHans Petter Selasky 
377*33ec1ccbSHans Petter Selasky 		list_len    -= chunk;
378*33ec1ccbSHans Petter Selasky 		start_index += chunk;
379*33ec1ccbSHans Petter Selasky 		buffer_list += chunk;
380*33ec1ccbSHans Petter Selasky 	}
381*33ec1ccbSHans Petter Selasky 
382*33ec1ccbSHans Petter Selasky 	return 0;
383*33ec1ccbSHans Petter Selasky }
384*33ec1ccbSHans Petter Selasky 
tavor_hw_index_to_key(u32 ind)385*33ec1ccbSHans Petter Selasky static inline u32 tavor_hw_index_to_key(u32 ind)
386*33ec1ccbSHans Petter Selasky {
387*33ec1ccbSHans Petter Selasky 	return ind;
388*33ec1ccbSHans Petter Selasky }
389*33ec1ccbSHans Petter Selasky 
tavor_key_to_hw_index(u32 key)390*33ec1ccbSHans Petter Selasky static inline u32 tavor_key_to_hw_index(u32 key)
391*33ec1ccbSHans Petter Selasky {
392*33ec1ccbSHans Petter Selasky 	return key;
393*33ec1ccbSHans Petter Selasky }
394*33ec1ccbSHans Petter Selasky 
arbel_hw_index_to_key(u32 ind)395*33ec1ccbSHans Petter Selasky static inline u32 arbel_hw_index_to_key(u32 ind)
396*33ec1ccbSHans Petter Selasky {
397*33ec1ccbSHans Petter Selasky 	return (ind >> 24) | (ind << 8);
398*33ec1ccbSHans Petter Selasky }
399*33ec1ccbSHans Petter Selasky 
arbel_key_to_hw_index(u32 key)400*33ec1ccbSHans Petter Selasky static inline u32 arbel_key_to_hw_index(u32 key)
401*33ec1ccbSHans Petter Selasky {
402*33ec1ccbSHans Petter Selasky 	return (key << 24) | (key >> 8);
403*33ec1ccbSHans Petter Selasky }
404*33ec1ccbSHans Petter Selasky 
hw_index_to_key(struct mthca_dev * dev,u32 ind)405*33ec1ccbSHans Petter Selasky static inline u32 hw_index_to_key(struct mthca_dev *dev, u32 ind)
406*33ec1ccbSHans Petter Selasky {
407*33ec1ccbSHans Petter Selasky 	if (mthca_is_memfree(dev))
408*33ec1ccbSHans Petter Selasky 		return arbel_hw_index_to_key(ind);
409*33ec1ccbSHans Petter Selasky 	else
410*33ec1ccbSHans Petter Selasky 		return tavor_hw_index_to_key(ind);
411*33ec1ccbSHans Petter Selasky }
412*33ec1ccbSHans Petter Selasky 
key_to_hw_index(struct mthca_dev * dev,u32 key)413*33ec1ccbSHans Petter Selasky static inline u32 key_to_hw_index(struct mthca_dev *dev, u32 key)
414*33ec1ccbSHans Petter Selasky {
415*33ec1ccbSHans Petter Selasky 	if (mthca_is_memfree(dev))
416*33ec1ccbSHans Petter Selasky 		return arbel_key_to_hw_index(key);
417*33ec1ccbSHans Petter Selasky 	else
418*33ec1ccbSHans Petter Selasky 		return tavor_key_to_hw_index(key);
419*33ec1ccbSHans Petter Selasky }
420*33ec1ccbSHans Petter Selasky 
adjust_key(struct mthca_dev * dev,u32 key)421*33ec1ccbSHans Petter Selasky static inline u32 adjust_key(struct mthca_dev *dev, u32 key)
422*33ec1ccbSHans Petter Selasky {
423*33ec1ccbSHans Petter Selasky 	if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
424*33ec1ccbSHans Petter Selasky 		return ((key << 20) & 0x800000) | (key & 0x7fffff);
425*33ec1ccbSHans Petter Selasky 	else
426*33ec1ccbSHans Petter Selasky 		return key;
427*33ec1ccbSHans Petter Selasky }
428*33ec1ccbSHans Petter Selasky 
mthca_mr_alloc(struct mthca_dev * dev,u32 pd,int buffer_size_shift,u64 iova,u64 total_size,u32 access,struct mthca_mr * mr)429*33ec1ccbSHans Petter Selasky int mthca_mr_alloc(struct mthca_dev *dev, u32 pd, int buffer_size_shift,
430*33ec1ccbSHans Petter Selasky 		   u64 iova, u64 total_size, u32 access, struct mthca_mr *mr)
431*33ec1ccbSHans Petter Selasky {
432*33ec1ccbSHans Petter Selasky 	struct mthca_mailbox *mailbox;
433*33ec1ccbSHans Petter Selasky 	struct mthca_mpt_entry *mpt_entry;
434*33ec1ccbSHans Petter Selasky 	u32 key;
435*33ec1ccbSHans Petter Selasky 	int i;
436*33ec1ccbSHans Petter Selasky 	int err;
437*33ec1ccbSHans Petter Selasky 
438*33ec1ccbSHans Petter Selasky 	WARN_ON(buffer_size_shift >= 32);
439*33ec1ccbSHans Petter Selasky 
440*33ec1ccbSHans Petter Selasky 	key = mthca_alloc(&dev->mr_table.mpt_alloc);
441*33ec1ccbSHans Petter Selasky 	if (key == -1)
442*33ec1ccbSHans Petter Selasky 		return -ENOMEM;
443*33ec1ccbSHans Petter Selasky 	key = adjust_key(dev, key);
444*33ec1ccbSHans Petter Selasky 	mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);
445*33ec1ccbSHans Petter Selasky 
446*33ec1ccbSHans Petter Selasky 	if (mthca_is_memfree(dev)) {
447*33ec1ccbSHans Petter Selasky 		err = mthca_table_get(dev, dev->mr_table.mpt_table, key);
448*33ec1ccbSHans Petter Selasky 		if (err)
449*33ec1ccbSHans Petter Selasky 			goto err_out_mpt_free;
450*33ec1ccbSHans Petter Selasky 	}
451*33ec1ccbSHans Petter Selasky 
452*33ec1ccbSHans Petter Selasky 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
453*33ec1ccbSHans Petter Selasky 	if (IS_ERR(mailbox)) {
454*33ec1ccbSHans Petter Selasky 		err = PTR_ERR(mailbox);
455*33ec1ccbSHans Petter Selasky 		goto err_out_table;
456*33ec1ccbSHans Petter Selasky 	}
457*33ec1ccbSHans Petter Selasky 	mpt_entry = mailbox->buf;
458*33ec1ccbSHans Petter Selasky 
459*33ec1ccbSHans Petter Selasky 	mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS     |
460*33ec1ccbSHans Petter Selasky 				       MTHCA_MPT_FLAG_MIO         |
461*33ec1ccbSHans Petter Selasky 				       MTHCA_MPT_FLAG_REGION      |
462*33ec1ccbSHans Petter Selasky 				       access);
463*33ec1ccbSHans Petter Selasky 	if (!mr->mtt)
464*33ec1ccbSHans Petter Selasky 		mpt_entry->flags |= cpu_to_be32(MTHCA_MPT_FLAG_PHYSICAL);
465*33ec1ccbSHans Petter Selasky 
466*33ec1ccbSHans Petter Selasky 	mpt_entry->page_size = cpu_to_be32(buffer_size_shift - 12);
467*33ec1ccbSHans Petter Selasky 	mpt_entry->key       = cpu_to_be32(key);
468*33ec1ccbSHans Petter Selasky 	mpt_entry->pd        = cpu_to_be32(pd);
469*33ec1ccbSHans Petter Selasky 	mpt_entry->start     = cpu_to_be64(iova);
470*33ec1ccbSHans Petter Selasky 	mpt_entry->length    = cpu_to_be64(total_size);
471*33ec1ccbSHans Petter Selasky 
472*33ec1ccbSHans Petter Selasky 	memset(&mpt_entry->lkey, 0,
473*33ec1ccbSHans Petter Selasky 	       sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, lkey));
474*33ec1ccbSHans Petter Selasky 
475*33ec1ccbSHans Petter Selasky 	if (mr->mtt)
476*33ec1ccbSHans Petter Selasky 		mpt_entry->mtt_seg =
477*33ec1ccbSHans Petter Selasky 			cpu_to_be64(dev->mr_table.mtt_base +
478*33ec1ccbSHans Petter Selasky 				    mr->mtt->first_seg * dev->limits.mtt_seg_size);
479*33ec1ccbSHans Petter Selasky 
480*33ec1ccbSHans Petter Selasky 	if (0) {
481*33ec1ccbSHans Petter Selasky 		mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey);
482*33ec1ccbSHans Petter Selasky 		for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) {
483*33ec1ccbSHans Petter Selasky 			if (i % 4 == 0)
484*33ec1ccbSHans Petter Selasky 				printk("[%02x] ", i * 4);
485*33ec1ccbSHans Petter Selasky 			printk(" %08x", be32_to_cpu(((__be32 *) mpt_entry)[i]));
486*33ec1ccbSHans Petter Selasky 			if ((i + 1) % 4 == 0)
487*33ec1ccbSHans Petter Selasky 				printk("\n");
488*33ec1ccbSHans Petter Selasky 		}
489*33ec1ccbSHans Petter Selasky 	}
490*33ec1ccbSHans Petter Selasky 
491*33ec1ccbSHans Petter Selasky 	err = mthca_SW2HW_MPT(dev, mailbox,
492*33ec1ccbSHans Petter Selasky 			      key & (dev->limits.num_mpts - 1));
493*33ec1ccbSHans Petter Selasky 	if (err) {
494*33ec1ccbSHans Petter Selasky 		mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
495*33ec1ccbSHans Petter Selasky 		goto err_out_mailbox;
496*33ec1ccbSHans Petter Selasky 	}
497*33ec1ccbSHans Petter Selasky 
498*33ec1ccbSHans Petter Selasky 	mthca_free_mailbox(dev, mailbox);
499*33ec1ccbSHans Petter Selasky 	return err;
500*33ec1ccbSHans Petter Selasky 
501*33ec1ccbSHans Petter Selasky err_out_mailbox:
502*33ec1ccbSHans Petter Selasky 	mthca_free_mailbox(dev, mailbox);
503*33ec1ccbSHans Petter Selasky 
504*33ec1ccbSHans Petter Selasky err_out_table:
505*33ec1ccbSHans Petter Selasky 	mthca_table_put(dev, dev->mr_table.mpt_table, key);
506*33ec1ccbSHans Petter Selasky 
507*33ec1ccbSHans Petter Selasky err_out_mpt_free:
508*33ec1ccbSHans Petter Selasky 	mthca_free(&dev->mr_table.mpt_alloc, key);
509*33ec1ccbSHans Petter Selasky 	return err;
510*33ec1ccbSHans Petter Selasky }
511*33ec1ccbSHans Petter Selasky 
mthca_mr_alloc_notrans(struct mthca_dev * dev,u32 pd,u32 access,struct mthca_mr * mr)512*33ec1ccbSHans Petter Selasky int mthca_mr_alloc_notrans(struct mthca_dev *dev, u32 pd,
513*33ec1ccbSHans Petter Selasky 			   u32 access, struct mthca_mr *mr)
514*33ec1ccbSHans Petter Selasky {
515*33ec1ccbSHans Petter Selasky 	mr->mtt = NULL;
516*33ec1ccbSHans Petter Selasky 	return mthca_mr_alloc(dev, pd, 12, 0, ~0ULL, access, mr);
517*33ec1ccbSHans Petter Selasky }
518*33ec1ccbSHans Petter Selasky 
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)519*33ec1ccbSHans Petter Selasky int mthca_mr_alloc_phys(struct mthca_dev *dev, u32 pd,
520*33ec1ccbSHans Petter Selasky 			u64 *buffer_list, int buffer_size_shift,
521*33ec1ccbSHans Petter Selasky 			int list_len, u64 iova, u64 total_size,
522*33ec1ccbSHans Petter Selasky 			u32 access, struct mthca_mr *mr)
523*33ec1ccbSHans Petter Selasky {
524*33ec1ccbSHans Petter Selasky 	int err;
525*33ec1ccbSHans Petter Selasky 
526*33ec1ccbSHans Petter Selasky 	mr->mtt = mthca_alloc_mtt(dev, list_len);
527*33ec1ccbSHans Petter Selasky 	if (IS_ERR(mr->mtt))
528*33ec1ccbSHans Petter Selasky 		return PTR_ERR(mr->mtt);
529*33ec1ccbSHans Petter Selasky 
530*33ec1ccbSHans Petter Selasky 	err = mthca_write_mtt(dev, mr->mtt, 0, buffer_list, list_len);
531*33ec1ccbSHans Petter Selasky 	if (err) {
532*33ec1ccbSHans Petter Selasky 		mthca_free_mtt(dev, mr->mtt);
533*33ec1ccbSHans Petter Selasky 		return err;
534*33ec1ccbSHans Petter Selasky 	}
535*33ec1ccbSHans Petter Selasky 
536*33ec1ccbSHans Petter Selasky 	err = mthca_mr_alloc(dev, pd, buffer_size_shift, iova,
537*33ec1ccbSHans Petter Selasky 			     total_size, access, mr);
538*33ec1ccbSHans Petter Selasky 	if (err)
539*33ec1ccbSHans Petter Selasky 		mthca_free_mtt(dev, mr->mtt);
540*33ec1ccbSHans Petter Selasky 
541*33ec1ccbSHans Petter Selasky 	return err;
542*33ec1ccbSHans Petter Selasky }
543*33ec1ccbSHans Petter Selasky 
544*33ec1ccbSHans Petter Selasky /* Free mr or fmr */
mthca_free_region(struct mthca_dev * dev,u32 lkey)545*33ec1ccbSHans Petter Selasky static void mthca_free_region(struct mthca_dev *dev, u32 lkey)
546*33ec1ccbSHans Petter Selasky {
547*33ec1ccbSHans Petter Selasky 	mthca_table_put(dev, dev->mr_table.mpt_table,
548*33ec1ccbSHans Petter Selasky 			key_to_hw_index(dev, lkey));
549*33ec1ccbSHans Petter Selasky 
550*33ec1ccbSHans Petter Selasky 	mthca_free(&dev->mr_table.mpt_alloc, key_to_hw_index(dev, lkey));
551*33ec1ccbSHans Petter Selasky }
552*33ec1ccbSHans Petter Selasky 
mthca_free_mr(struct mthca_dev * dev,struct mthca_mr * mr)553*33ec1ccbSHans Petter Selasky void mthca_free_mr(struct mthca_dev *dev, struct mthca_mr *mr)
554*33ec1ccbSHans Petter Selasky {
555*33ec1ccbSHans Petter Selasky 	int err;
556*33ec1ccbSHans Petter Selasky 
557*33ec1ccbSHans Petter Selasky 	err = mthca_HW2SW_MPT(dev, NULL,
558*33ec1ccbSHans Petter Selasky 			      key_to_hw_index(dev, mr->ibmr.lkey) &
559*33ec1ccbSHans Petter Selasky 			      (dev->limits.num_mpts - 1));
560*33ec1ccbSHans Petter Selasky 	if (err)
561*33ec1ccbSHans Petter Selasky 		mthca_warn(dev, "HW2SW_MPT failed (%d)\n", err);
562*33ec1ccbSHans Petter Selasky 
563*33ec1ccbSHans Petter Selasky 	mthca_free_region(dev, mr->ibmr.lkey);
564*33ec1ccbSHans Petter Selasky 	mthca_free_mtt(dev, mr->mtt);
565*33ec1ccbSHans Petter Selasky }
566*33ec1ccbSHans Petter Selasky 
mthca_fmr_alloc(struct mthca_dev * dev,u32 pd,u32 access,struct mthca_fmr * mr)567*33ec1ccbSHans Petter Selasky int mthca_fmr_alloc(struct mthca_dev *dev, u32 pd,
568*33ec1ccbSHans Petter Selasky 		    u32 access, struct mthca_fmr *mr)
569*33ec1ccbSHans Petter Selasky {
570*33ec1ccbSHans Petter Selasky 	struct mthca_mpt_entry *mpt_entry;
571*33ec1ccbSHans Petter Selasky 	struct mthca_mailbox *mailbox;
572*33ec1ccbSHans Petter Selasky 	u64 mtt_seg;
573*33ec1ccbSHans Petter Selasky 	u32 key, idx;
574*33ec1ccbSHans Petter Selasky 	int list_len = mr->attr.max_pages;
575*33ec1ccbSHans Petter Selasky 	int err = -ENOMEM;
576*33ec1ccbSHans Petter Selasky 	int i;
577*33ec1ccbSHans Petter Selasky 
578*33ec1ccbSHans Petter Selasky 	if (mr->attr.page_shift < 12 || mr->attr.page_shift >= 32)
579*33ec1ccbSHans Petter Selasky 		return -EINVAL;
580*33ec1ccbSHans Petter Selasky 
581*33ec1ccbSHans Petter Selasky 	/* For Arbel, all MTTs must fit in the same page. */
582*33ec1ccbSHans Petter Selasky 	if (mthca_is_memfree(dev) &&
583*33ec1ccbSHans Petter Selasky 	    mr->attr.max_pages * sizeof *mr->mem.arbel.mtts > PAGE_SIZE)
584*33ec1ccbSHans Petter Selasky 		return -EINVAL;
585*33ec1ccbSHans Petter Selasky 
586*33ec1ccbSHans Petter Selasky 	mr->maps = 0;
587*33ec1ccbSHans Petter Selasky 
588*33ec1ccbSHans Petter Selasky 	key = mthca_alloc(&dev->mr_table.mpt_alloc);
589*33ec1ccbSHans Petter Selasky 	if (key == -1)
590*33ec1ccbSHans Petter Selasky 		return -ENOMEM;
591*33ec1ccbSHans Petter Selasky 	key = adjust_key(dev, key);
592*33ec1ccbSHans Petter Selasky 
593*33ec1ccbSHans Petter Selasky 	idx = key & (dev->limits.num_mpts - 1);
594*33ec1ccbSHans Petter Selasky 	mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);
595*33ec1ccbSHans Petter Selasky 
596*33ec1ccbSHans Petter Selasky 	if (mthca_is_memfree(dev)) {
597*33ec1ccbSHans Petter Selasky 		err = mthca_table_get(dev, dev->mr_table.mpt_table, key);
598*33ec1ccbSHans Petter Selasky 		if (err)
599*33ec1ccbSHans Petter Selasky 			goto err_out_mpt_free;
600*33ec1ccbSHans Petter Selasky 
601*33ec1ccbSHans Petter Selasky 		mr->mem.arbel.mpt = mthca_table_find(dev->mr_table.mpt_table, key, NULL);
602*33ec1ccbSHans Petter Selasky 		BUG_ON(!mr->mem.arbel.mpt);
603*33ec1ccbSHans Petter Selasky 	} else
604*33ec1ccbSHans Petter Selasky 		mr->mem.tavor.mpt = dev->mr_table.tavor_fmr.mpt_base +
605*33ec1ccbSHans Petter Selasky 			sizeof *(mr->mem.tavor.mpt) * idx;
606*33ec1ccbSHans Petter Selasky 
607*33ec1ccbSHans Petter Selasky 	mr->mtt = __mthca_alloc_mtt(dev, list_len, dev->mr_table.fmr_mtt_buddy);
608*33ec1ccbSHans Petter Selasky 	if (IS_ERR(mr->mtt)) {
609*33ec1ccbSHans Petter Selasky 		err = PTR_ERR(mr->mtt);
610*33ec1ccbSHans Petter Selasky 		goto err_out_table;
611*33ec1ccbSHans Petter Selasky 	}
612*33ec1ccbSHans Petter Selasky 
613*33ec1ccbSHans Petter Selasky 	mtt_seg = mr->mtt->first_seg * dev->limits.mtt_seg_size;
614*33ec1ccbSHans Petter Selasky 
615*33ec1ccbSHans Petter Selasky 	if (mthca_is_memfree(dev)) {
616*33ec1ccbSHans Petter Selasky 		mr->mem.arbel.mtts = mthca_table_find(dev->mr_table.mtt_table,
617*33ec1ccbSHans Petter Selasky 						      mr->mtt->first_seg,
618*33ec1ccbSHans Petter Selasky 						      &mr->mem.arbel.dma_handle);
619*33ec1ccbSHans Petter Selasky 		BUG_ON(!mr->mem.arbel.mtts);
620*33ec1ccbSHans Petter Selasky 	} else
621*33ec1ccbSHans Petter Selasky 		mr->mem.tavor.mtts = dev->mr_table.tavor_fmr.mtt_base + mtt_seg;
622*33ec1ccbSHans Petter Selasky 
623*33ec1ccbSHans Petter Selasky 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
624*33ec1ccbSHans Petter Selasky 	if (IS_ERR(mailbox)) {
625*33ec1ccbSHans Petter Selasky 		err = PTR_ERR(mailbox);
626*33ec1ccbSHans Petter Selasky 		goto err_out_free_mtt;
627*33ec1ccbSHans Petter Selasky 	}
628*33ec1ccbSHans Petter Selasky 
629*33ec1ccbSHans Petter Selasky 	mpt_entry = mailbox->buf;
630*33ec1ccbSHans Petter Selasky 
631*33ec1ccbSHans Petter Selasky 	mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS     |
632*33ec1ccbSHans Petter Selasky 				       MTHCA_MPT_FLAG_MIO         |
633*33ec1ccbSHans Petter Selasky 				       MTHCA_MPT_FLAG_REGION      |
634*33ec1ccbSHans Petter Selasky 				       access);
635*33ec1ccbSHans Petter Selasky 
636*33ec1ccbSHans Petter Selasky 	mpt_entry->page_size = cpu_to_be32(mr->attr.page_shift - 12);
637*33ec1ccbSHans Petter Selasky 	mpt_entry->key       = cpu_to_be32(key);
638*33ec1ccbSHans Petter Selasky 	mpt_entry->pd        = cpu_to_be32(pd);
639*33ec1ccbSHans Petter Selasky 	memset(&mpt_entry->start, 0,
640*33ec1ccbSHans Petter Selasky 	       sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, start));
641*33ec1ccbSHans Petter Selasky 	mpt_entry->mtt_seg   = cpu_to_be64(dev->mr_table.mtt_base + mtt_seg);
642*33ec1ccbSHans Petter Selasky 
643*33ec1ccbSHans Petter Selasky 	if (0) {
644*33ec1ccbSHans Petter Selasky 		mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey);
645*33ec1ccbSHans Petter Selasky 		for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) {
646*33ec1ccbSHans Petter Selasky 			if (i % 4 == 0)
647*33ec1ccbSHans Petter Selasky 				printk("[%02x] ", i * 4);
648*33ec1ccbSHans Petter Selasky 			printk(" %08x", be32_to_cpu(((__be32 *) mpt_entry)[i]));
649*33ec1ccbSHans Petter Selasky 			if ((i + 1) % 4 == 0)
650*33ec1ccbSHans Petter Selasky 				printk("\n");
651*33ec1ccbSHans Petter Selasky 		}
652*33ec1ccbSHans Petter Selasky 	}
653*33ec1ccbSHans Petter Selasky 
654*33ec1ccbSHans Petter Selasky 	err = mthca_SW2HW_MPT(dev, mailbox,
655*33ec1ccbSHans Petter Selasky 			      key & (dev->limits.num_mpts - 1));
656*33ec1ccbSHans Petter Selasky 	if (err) {
657*33ec1ccbSHans Petter Selasky 		mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
658*33ec1ccbSHans Petter Selasky 		goto err_out_mailbox_free;
659*33ec1ccbSHans Petter Selasky 	}
660*33ec1ccbSHans Petter Selasky 
661*33ec1ccbSHans Petter Selasky 	mthca_free_mailbox(dev, mailbox);
662*33ec1ccbSHans Petter Selasky 	return 0;
663*33ec1ccbSHans Petter Selasky 
664*33ec1ccbSHans Petter Selasky err_out_mailbox_free:
665*33ec1ccbSHans Petter Selasky 	mthca_free_mailbox(dev, mailbox);
666*33ec1ccbSHans Petter Selasky 
667*33ec1ccbSHans Petter Selasky err_out_free_mtt:
668*33ec1ccbSHans Petter Selasky 	mthca_free_mtt(dev, mr->mtt);
669*33ec1ccbSHans Petter Selasky 
670*33ec1ccbSHans Petter Selasky err_out_table:
671*33ec1ccbSHans Petter Selasky 	mthca_table_put(dev, dev->mr_table.mpt_table, key);
672*33ec1ccbSHans Petter Selasky 
673*33ec1ccbSHans Petter Selasky err_out_mpt_free:
674*33ec1ccbSHans Petter Selasky 	mthca_free(&dev->mr_table.mpt_alloc, key);
675*33ec1ccbSHans Petter Selasky 	return err;
676*33ec1ccbSHans Petter Selasky }
677*33ec1ccbSHans Petter Selasky 
mthca_free_fmr(struct mthca_dev * dev,struct mthca_fmr * fmr)678*33ec1ccbSHans Petter Selasky int mthca_free_fmr(struct mthca_dev *dev, struct mthca_fmr *fmr)
679*33ec1ccbSHans Petter Selasky {
680*33ec1ccbSHans Petter Selasky 	if (fmr->maps)
681*33ec1ccbSHans Petter Selasky 		return -EBUSY;
682*33ec1ccbSHans Petter Selasky 
683*33ec1ccbSHans Petter Selasky 	mthca_free_region(dev, fmr->ibmr.lkey);
684*33ec1ccbSHans Petter Selasky 	mthca_free_mtt(dev, fmr->mtt);
685*33ec1ccbSHans Petter Selasky 
686*33ec1ccbSHans Petter Selasky 	return 0;
687*33ec1ccbSHans Petter Selasky }
688*33ec1ccbSHans Petter Selasky 
mthca_check_fmr(struct mthca_fmr * fmr,u64 * page_list,int list_len,u64 iova)689*33ec1ccbSHans Petter Selasky static inline int mthca_check_fmr(struct mthca_fmr *fmr, u64 *page_list,
690*33ec1ccbSHans Petter Selasky 				  int list_len, u64 iova)
691*33ec1ccbSHans Petter Selasky {
692*33ec1ccbSHans Petter Selasky 	int i, page_mask;
693*33ec1ccbSHans Petter Selasky 
694*33ec1ccbSHans Petter Selasky 	if (list_len > fmr->attr.max_pages)
695*33ec1ccbSHans Petter Selasky 		return -EINVAL;
696*33ec1ccbSHans Petter Selasky 
697*33ec1ccbSHans Petter Selasky 	page_mask = (1 << fmr->attr.page_shift) - 1;
698*33ec1ccbSHans Petter Selasky 
699*33ec1ccbSHans Petter Selasky 	/* We are getting page lists, so va must be page aligned. */
700*33ec1ccbSHans Petter Selasky 	if (iova & page_mask)
701*33ec1ccbSHans Petter Selasky 		return -EINVAL;
702*33ec1ccbSHans Petter Selasky 
703*33ec1ccbSHans Petter Selasky 	/* Trust the user not to pass misaligned data in page_list */
704*33ec1ccbSHans Petter Selasky 	if (0)
705*33ec1ccbSHans Petter Selasky 		for (i = 0; i < list_len; ++i) {
706*33ec1ccbSHans Petter Selasky 			if (page_list[i] & ~page_mask)
707*33ec1ccbSHans Petter Selasky 				return -EINVAL;
708*33ec1ccbSHans Petter Selasky 		}
709*33ec1ccbSHans Petter Selasky 
710*33ec1ccbSHans Petter Selasky 	if (fmr->maps >= fmr->attr.max_maps)
711*33ec1ccbSHans Petter Selasky 		return -EINVAL;
712*33ec1ccbSHans Petter Selasky 
713*33ec1ccbSHans Petter Selasky 	return 0;
714*33ec1ccbSHans Petter Selasky }
715*33ec1ccbSHans Petter Selasky 
mthca_tavor_map_phys_fmr(struct ib_fmr * ibfmr,u64 * page_list,int list_len,u64 iova)716*33ec1ccbSHans Petter Selasky int mthca_tavor_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
717*33ec1ccbSHans Petter Selasky 			     int list_len, u64 iova)
718*33ec1ccbSHans Petter Selasky {
719*33ec1ccbSHans Petter Selasky 	struct mthca_fmr *fmr = to_mfmr(ibfmr);
720*33ec1ccbSHans Petter Selasky 	struct mthca_dev *dev = to_mdev(ibfmr->device);
721*33ec1ccbSHans Petter Selasky 	struct mthca_mpt_entry mpt_entry;
722*33ec1ccbSHans Petter Selasky 	u32 key;
723*33ec1ccbSHans Petter Selasky 	int i, err;
724*33ec1ccbSHans Petter Selasky 
725*33ec1ccbSHans Petter Selasky 	err = mthca_check_fmr(fmr, page_list, list_len, iova);
726*33ec1ccbSHans Petter Selasky 	if (err)
727*33ec1ccbSHans Petter Selasky 		return err;
728*33ec1ccbSHans Petter Selasky 
729*33ec1ccbSHans Petter Selasky 	++fmr->maps;
730*33ec1ccbSHans Petter Selasky 
731*33ec1ccbSHans Petter Selasky 	key = tavor_key_to_hw_index(fmr->ibmr.lkey);
732*33ec1ccbSHans Petter Selasky 	key += dev->limits.num_mpts;
733*33ec1ccbSHans Petter Selasky 	fmr->ibmr.lkey = fmr->ibmr.rkey = tavor_hw_index_to_key(key);
734*33ec1ccbSHans Petter Selasky 
735*33ec1ccbSHans Petter Selasky 	writeb(MTHCA_MPT_STATUS_SW, fmr->mem.tavor.mpt);
736*33ec1ccbSHans Petter Selasky 
737*33ec1ccbSHans Petter Selasky 	for (i = 0; i < list_len; ++i) {
738*33ec1ccbSHans Petter Selasky 		__be64 mtt_entry = cpu_to_be64(page_list[i] |
739*33ec1ccbSHans Petter Selasky 					       MTHCA_MTT_FLAG_PRESENT);
740*33ec1ccbSHans Petter Selasky 		mthca_write64_raw(mtt_entry, fmr->mem.tavor.mtts + i);
741*33ec1ccbSHans Petter Selasky 	}
742*33ec1ccbSHans Petter Selasky 
743*33ec1ccbSHans Petter Selasky 	mpt_entry.lkey   = cpu_to_be32(key);
744*33ec1ccbSHans Petter Selasky 	mpt_entry.length = cpu_to_be64(list_len * (1ull << fmr->attr.page_shift));
745*33ec1ccbSHans Petter Selasky 	mpt_entry.start  = cpu_to_be64(iova);
746*33ec1ccbSHans Petter Selasky 
747*33ec1ccbSHans Petter Selasky 	__raw_writel((__force u32) mpt_entry.lkey, &fmr->mem.tavor.mpt->key);
748*33ec1ccbSHans Petter Selasky 	memcpy_toio(&fmr->mem.tavor.mpt->start, &mpt_entry.start,
749*33ec1ccbSHans Petter Selasky 		    offsetof(struct mthca_mpt_entry, window_count) -
750*33ec1ccbSHans Petter Selasky 		    offsetof(struct mthca_mpt_entry, start));
751*33ec1ccbSHans Petter Selasky 
752*33ec1ccbSHans Petter Selasky 	writeb(MTHCA_MPT_STATUS_HW, fmr->mem.tavor.mpt);
753*33ec1ccbSHans Petter Selasky 
754*33ec1ccbSHans Petter Selasky 	return 0;
755*33ec1ccbSHans Petter Selasky }
756*33ec1ccbSHans Petter Selasky 
mthca_arbel_map_phys_fmr(struct ib_fmr * ibfmr,u64 * page_list,int list_len,u64 iova)757*33ec1ccbSHans Petter Selasky int mthca_arbel_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
758*33ec1ccbSHans Petter Selasky 			     int list_len, u64 iova)
759*33ec1ccbSHans Petter Selasky {
760*33ec1ccbSHans Petter Selasky 	struct mthca_fmr *fmr = to_mfmr(ibfmr);
761*33ec1ccbSHans Petter Selasky 	struct mthca_dev *dev = to_mdev(ibfmr->device);
762*33ec1ccbSHans Petter Selasky 	u32 key;
763*33ec1ccbSHans Petter Selasky 	int i, err;
764*33ec1ccbSHans Petter Selasky 
765*33ec1ccbSHans Petter Selasky 	err = mthca_check_fmr(fmr, page_list, list_len, iova);
766*33ec1ccbSHans Petter Selasky 	if (err)
767*33ec1ccbSHans Petter Selasky 		return err;
768*33ec1ccbSHans Petter Selasky 
769*33ec1ccbSHans Petter Selasky 	++fmr->maps;
770*33ec1ccbSHans Petter Selasky 
771*33ec1ccbSHans Petter Selasky 	key = arbel_key_to_hw_index(fmr->ibmr.lkey);
772*33ec1ccbSHans Petter Selasky 	if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
773*33ec1ccbSHans Petter Selasky 		key += SINAI_FMR_KEY_INC;
774*33ec1ccbSHans Petter Selasky 	else
775*33ec1ccbSHans Petter Selasky 		key += dev->limits.num_mpts;
776*33ec1ccbSHans Petter Selasky 	fmr->ibmr.lkey = fmr->ibmr.rkey = arbel_hw_index_to_key(key);
777*33ec1ccbSHans Petter Selasky 
778*33ec1ccbSHans Petter Selasky 	*(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_SW;
779*33ec1ccbSHans Petter Selasky 
780*33ec1ccbSHans Petter Selasky 	wmb();
781*33ec1ccbSHans Petter Selasky 
782*33ec1ccbSHans Petter Selasky 	dma_sync_single_for_cpu(&dev->pdev->dev, fmr->mem.arbel.dma_handle,
783*33ec1ccbSHans Petter Selasky 				list_len * sizeof(u64), DMA_TO_DEVICE);
784*33ec1ccbSHans Petter Selasky 
785*33ec1ccbSHans Petter Selasky 	for (i = 0; i < list_len; ++i)
786*33ec1ccbSHans Petter Selasky 		fmr->mem.arbel.mtts[i] = cpu_to_be64(page_list[i] |
787*33ec1ccbSHans Petter Selasky 						     MTHCA_MTT_FLAG_PRESENT);
788*33ec1ccbSHans Petter Selasky 
789*33ec1ccbSHans Petter Selasky 	dma_sync_single_for_device(&dev->pdev->dev, fmr->mem.arbel.dma_handle,
790*33ec1ccbSHans Petter Selasky 				   list_len * sizeof(u64), DMA_TO_DEVICE);
791*33ec1ccbSHans Petter Selasky 
792*33ec1ccbSHans Petter Selasky 	fmr->mem.arbel.mpt->key    = cpu_to_be32(key);
793*33ec1ccbSHans Petter Selasky 	fmr->mem.arbel.mpt->lkey   = cpu_to_be32(key);
794*33ec1ccbSHans Petter Selasky 	fmr->mem.arbel.mpt->length = cpu_to_be64(list_len * (1ull << fmr->attr.page_shift));
795*33ec1ccbSHans Petter Selasky 	fmr->mem.arbel.mpt->start  = cpu_to_be64(iova);
796*33ec1ccbSHans Petter Selasky 
797*33ec1ccbSHans Petter Selasky 	wmb();
798*33ec1ccbSHans Petter Selasky 
799*33ec1ccbSHans Petter Selasky 	*(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_HW;
800*33ec1ccbSHans Petter Selasky 
801*33ec1ccbSHans Petter Selasky 	wmb();
802*33ec1ccbSHans Petter Selasky 
803*33ec1ccbSHans Petter Selasky 	return 0;
804*33ec1ccbSHans Petter Selasky }
805*33ec1ccbSHans Petter Selasky 
mthca_tavor_fmr_unmap(struct mthca_dev * dev,struct mthca_fmr * fmr)806*33ec1ccbSHans Petter Selasky void mthca_tavor_fmr_unmap(struct mthca_dev *dev, struct mthca_fmr *fmr)
807*33ec1ccbSHans Petter Selasky {
808*33ec1ccbSHans Petter Selasky 	if (!fmr->maps)
809*33ec1ccbSHans Petter Selasky 		return;
810*33ec1ccbSHans Petter Selasky 
811*33ec1ccbSHans Petter Selasky 	fmr->maps = 0;
812*33ec1ccbSHans Petter Selasky 
813*33ec1ccbSHans Petter Selasky 	writeb(MTHCA_MPT_STATUS_SW, fmr->mem.tavor.mpt);
814*33ec1ccbSHans Petter Selasky }
815*33ec1ccbSHans Petter Selasky 
mthca_arbel_fmr_unmap(struct mthca_dev * dev,struct mthca_fmr * fmr)816*33ec1ccbSHans Petter Selasky void mthca_arbel_fmr_unmap(struct mthca_dev *dev, struct mthca_fmr *fmr)
817*33ec1ccbSHans Petter Selasky {
818*33ec1ccbSHans Petter Selasky 	if (!fmr->maps)
819*33ec1ccbSHans Petter Selasky 		return;
820*33ec1ccbSHans Petter Selasky 
821*33ec1ccbSHans Petter Selasky 	fmr->maps = 0;
822*33ec1ccbSHans Petter Selasky 
823*33ec1ccbSHans Petter Selasky 	*(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_SW;
824*33ec1ccbSHans Petter Selasky }
825*33ec1ccbSHans Petter Selasky 
mthca_init_mr_table(struct mthca_dev * dev)826*33ec1ccbSHans Petter Selasky int mthca_init_mr_table(struct mthca_dev *dev)
827*33ec1ccbSHans Petter Selasky {
828*33ec1ccbSHans Petter Selasky 	phys_addr_t addr;
829*33ec1ccbSHans Petter Selasky 	int mpts, mtts, err, i;
830*33ec1ccbSHans Petter Selasky 
831*33ec1ccbSHans Petter Selasky 	err = mthca_alloc_init(&dev->mr_table.mpt_alloc,
832*33ec1ccbSHans Petter Selasky 			       dev->limits.num_mpts,
833*33ec1ccbSHans Petter Selasky 			       ~0, dev->limits.reserved_mrws);
834*33ec1ccbSHans Petter Selasky 	if (err)
835*33ec1ccbSHans Petter Selasky 		return err;
836*33ec1ccbSHans Petter Selasky 
837*33ec1ccbSHans Petter Selasky 	if (!mthca_is_memfree(dev) &&
838*33ec1ccbSHans Petter Selasky 	    (dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN))
839*33ec1ccbSHans Petter Selasky 		dev->limits.fmr_reserved_mtts = 0;
840*33ec1ccbSHans Petter Selasky 	else
841*33ec1ccbSHans Petter Selasky 		dev->mthca_flags |= MTHCA_FLAG_FMR;
842*33ec1ccbSHans Petter Selasky 
843*33ec1ccbSHans Petter Selasky 	if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
844*33ec1ccbSHans Petter Selasky 		mthca_dbg(dev, "Memory key throughput optimization activated.\n");
845*33ec1ccbSHans Petter Selasky 
846*33ec1ccbSHans Petter Selasky 	err = mthca_buddy_init(&dev->mr_table.mtt_buddy,
847*33ec1ccbSHans Petter Selasky 			       fls(dev->limits.num_mtt_segs - 1));
848*33ec1ccbSHans Petter Selasky 
849*33ec1ccbSHans Petter Selasky 	if (err)
850*33ec1ccbSHans Petter Selasky 		goto err_mtt_buddy;
851*33ec1ccbSHans Petter Selasky 
852*33ec1ccbSHans Petter Selasky 	dev->mr_table.tavor_fmr.mpt_base = NULL;
853*33ec1ccbSHans Petter Selasky 	dev->mr_table.tavor_fmr.mtt_base = NULL;
854*33ec1ccbSHans Petter Selasky 
855*33ec1ccbSHans Petter Selasky 	if (dev->limits.fmr_reserved_mtts) {
856*33ec1ccbSHans Petter Selasky 		i = fls(dev->limits.fmr_reserved_mtts - 1);
857*33ec1ccbSHans Petter Selasky 
858*33ec1ccbSHans Petter Selasky 		if (i >= 31) {
859*33ec1ccbSHans Petter Selasky 			mthca_warn(dev, "Unable to reserve 2^31 FMR MTTs.\n");
860*33ec1ccbSHans Petter Selasky 			err = -EINVAL;
861*33ec1ccbSHans Petter Selasky 			goto err_fmr_mpt;
862*33ec1ccbSHans Petter Selasky 		}
863*33ec1ccbSHans Petter Selasky 		mpts = mtts = 1 << i;
864*33ec1ccbSHans Petter Selasky 	} else {
865*33ec1ccbSHans Petter Selasky 		mtts = dev->limits.num_mtt_segs;
866*33ec1ccbSHans Petter Selasky 		mpts = dev->limits.num_mpts;
867*33ec1ccbSHans Petter Selasky 	}
868*33ec1ccbSHans Petter Selasky 
869*33ec1ccbSHans Petter Selasky 	if (!mthca_is_memfree(dev) &&
870*33ec1ccbSHans Petter Selasky 	    (dev->mthca_flags & MTHCA_FLAG_FMR)) {
871*33ec1ccbSHans Petter Selasky 		addr = pci_resource_start(dev->pdev, 4) +
872*33ec1ccbSHans Petter Selasky 			((pci_resource_len(dev->pdev, 4) - 1) &
873*33ec1ccbSHans Petter Selasky 			 dev->mr_table.mpt_base);
874*33ec1ccbSHans Petter Selasky 
875*33ec1ccbSHans Petter Selasky 		dev->mr_table.tavor_fmr.mpt_base =
876*33ec1ccbSHans Petter Selasky 			ioremap(addr, mpts * sizeof(struct mthca_mpt_entry));
877*33ec1ccbSHans Petter Selasky 
878*33ec1ccbSHans Petter Selasky 		if (!dev->mr_table.tavor_fmr.mpt_base) {
879*33ec1ccbSHans Petter Selasky 			mthca_warn(dev, "MPT ioremap for FMR failed.\n");
880*33ec1ccbSHans Petter Selasky 			err = -ENOMEM;
881*33ec1ccbSHans Petter Selasky 			goto err_fmr_mpt;
882*33ec1ccbSHans Petter Selasky 		}
883*33ec1ccbSHans Petter Selasky 
884*33ec1ccbSHans Petter Selasky 		addr = pci_resource_start(dev->pdev, 4) +
885*33ec1ccbSHans Petter Selasky 			((pci_resource_len(dev->pdev, 4) - 1) &
886*33ec1ccbSHans Petter Selasky 			 dev->mr_table.mtt_base);
887*33ec1ccbSHans Petter Selasky 
888*33ec1ccbSHans Petter Selasky 		dev->mr_table.tavor_fmr.mtt_base =
889*33ec1ccbSHans Petter Selasky 			ioremap(addr, mtts * dev->limits.mtt_seg_size);
890*33ec1ccbSHans Petter Selasky 		if (!dev->mr_table.tavor_fmr.mtt_base) {
891*33ec1ccbSHans Petter Selasky 			mthca_warn(dev, "MTT ioremap for FMR failed.\n");
892*33ec1ccbSHans Petter Selasky 			err = -ENOMEM;
893*33ec1ccbSHans Petter Selasky 			goto err_fmr_mtt;
894*33ec1ccbSHans Petter Selasky 		}
895*33ec1ccbSHans Petter Selasky 	}
896*33ec1ccbSHans Petter Selasky 
897*33ec1ccbSHans Petter Selasky 	if (dev->limits.fmr_reserved_mtts) {
898*33ec1ccbSHans Petter Selasky 		err = mthca_buddy_init(&dev->mr_table.tavor_fmr.mtt_buddy, fls(mtts - 1));
899*33ec1ccbSHans Petter Selasky 		if (err)
900*33ec1ccbSHans Petter Selasky 			goto err_fmr_mtt_buddy;
901*33ec1ccbSHans Petter Selasky 
902*33ec1ccbSHans Petter Selasky 		/* Prevent regular MRs from using FMR keys */
903*33ec1ccbSHans Petter Selasky 		err = mthca_buddy_alloc(&dev->mr_table.mtt_buddy, fls(mtts - 1));
904*33ec1ccbSHans Petter Selasky 		if (err)
905*33ec1ccbSHans Petter Selasky 			goto err_reserve_fmr;
906*33ec1ccbSHans Petter Selasky 
907*33ec1ccbSHans Petter Selasky 		dev->mr_table.fmr_mtt_buddy =
908*33ec1ccbSHans Petter Selasky 			&dev->mr_table.tavor_fmr.mtt_buddy;
909*33ec1ccbSHans Petter Selasky 	} else
910*33ec1ccbSHans Petter Selasky 		dev->mr_table.fmr_mtt_buddy = &dev->mr_table.mtt_buddy;
911*33ec1ccbSHans Petter Selasky 
912*33ec1ccbSHans Petter Selasky 	/* FMR table is always the first, take reserved MTTs out of there */
913*33ec1ccbSHans Petter Selasky 	if (dev->limits.reserved_mtts) {
914*33ec1ccbSHans Petter Selasky 		i = fls(dev->limits.reserved_mtts - 1);
915*33ec1ccbSHans Petter Selasky 
916*33ec1ccbSHans Petter Selasky 		if (mthca_alloc_mtt_range(dev, i,
917*33ec1ccbSHans Petter Selasky 					  dev->mr_table.fmr_mtt_buddy) == -1) {
918*33ec1ccbSHans Petter Selasky 			mthca_warn(dev, "MTT table of order %d is too small.\n",
919*33ec1ccbSHans Petter Selasky 				  dev->mr_table.fmr_mtt_buddy->max_order);
920*33ec1ccbSHans Petter Selasky 			err = -ENOMEM;
921*33ec1ccbSHans Petter Selasky 			goto err_reserve_mtts;
922*33ec1ccbSHans Petter Selasky 		}
923*33ec1ccbSHans Petter Selasky 	}
924*33ec1ccbSHans Petter Selasky 
925*33ec1ccbSHans Petter Selasky 	return 0;
926*33ec1ccbSHans Petter Selasky 
927*33ec1ccbSHans Petter Selasky err_reserve_mtts:
928*33ec1ccbSHans Petter Selasky err_reserve_fmr:
929*33ec1ccbSHans Petter Selasky 	if (dev->limits.fmr_reserved_mtts)
930*33ec1ccbSHans Petter Selasky 		mthca_buddy_cleanup(&dev->mr_table.tavor_fmr.mtt_buddy);
931*33ec1ccbSHans Petter Selasky 
932*33ec1ccbSHans Petter Selasky err_fmr_mtt_buddy:
933*33ec1ccbSHans Petter Selasky 	if (dev->mr_table.tavor_fmr.mtt_base)
934*33ec1ccbSHans Petter Selasky 		iounmap(dev->mr_table.tavor_fmr.mtt_base);
935*33ec1ccbSHans Petter Selasky 
936*33ec1ccbSHans Petter Selasky err_fmr_mtt:
937*33ec1ccbSHans Petter Selasky 	if (dev->mr_table.tavor_fmr.mpt_base)
938*33ec1ccbSHans Petter Selasky 		iounmap(dev->mr_table.tavor_fmr.mpt_base);
939*33ec1ccbSHans Petter Selasky 
940*33ec1ccbSHans Petter Selasky err_fmr_mpt:
941*33ec1ccbSHans Petter Selasky 	mthca_buddy_cleanup(&dev->mr_table.mtt_buddy);
942*33ec1ccbSHans Petter Selasky 
943*33ec1ccbSHans Petter Selasky err_mtt_buddy:
944*33ec1ccbSHans Petter Selasky 	mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);
945*33ec1ccbSHans Petter Selasky 
946*33ec1ccbSHans Petter Selasky 	return err;
947*33ec1ccbSHans Petter Selasky }
948*33ec1ccbSHans Petter Selasky 
mthca_cleanup_mr_table(struct mthca_dev * dev)949*33ec1ccbSHans Petter Selasky void mthca_cleanup_mr_table(struct mthca_dev *dev)
950*33ec1ccbSHans Petter Selasky {
951*33ec1ccbSHans Petter Selasky 	/* XXX check if any MRs are still allocated? */
952*33ec1ccbSHans Petter Selasky 	if (dev->limits.fmr_reserved_mtts)
953*33ec1ccbSHans Petter Selasky 		mthca_buddy_cleanup(&dev->mr_table.tavor_fmr.mtt_buddy);
954*33ec1ccbSHans Petter Selasky 
955*33ec1ccbSHans Petter Selasky 	mthca_buddy_cleanup(&dev->mr_table.mtt_buddy);
956*33ec1ccbSHans Petter Selasky 
957*33ec1ccbSHans Petter Selasky 	if (dev->mr_table.tavor_fmr.mtt_base)
958*33ec1ccbSHans Petter Selasky 		iounmap(dev->mr_table.tavor_fmr.mtt_base);
959*33ec1ccbSHans Petter Selasky 	if (dev->mr_table.tavor_fmr.mpt_base)
960*33ec1ccbSHans Petter Selasky 		iounmap(dev->mr_table.tavor_fmr.mpt_base);
961*33ec1ccbSHans Petter Selasky 
962*33ec1ccbSHans Petter Selasky 	mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);
963*33ec1ccbSHans Petter Selasky }
964