blob: a486dec1707e27a648b1c6276c0bddb8fe48da99 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 *
33 * $Id: mthca_mr.c 1349 2004-12-16 21:09:43Z roland $
34 */
35
36#include <linux/slab.h>
37#include <linux/init.h>
38#include <linux/errno.h>
39
40#include "mthca_dev.h"
41#include "mthca_cmd.h"
Roland Dreier86562a12005-04-16 15:26:13 -070042#include "mthca_memfree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Roland Dreierd56d6f92005-06-27 14:36:43 -070044struct mthca_mtt {
45 struct mthca_buddy *buddy;
46 int order;
47 u32 first_seg;
48};
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * Must be packed because mtt_seg is 64 bits but only aligned to 32 bits.
52 */
53struct mthca_mpt_entry {
Sean Hefty97f52eb2005-08-13 21:05:57 -070054 __be32 flags;
55 __be32 page_size;
56 __be32 key;
57 __be32 pd;
58 __be64 start;
59 __be64 length;
60 __be32 lkey;
61 __be32 window_count;
62 __be32 window_count_limit;
63 __be64 mtt_seg;
64 __be32 mtt_sz; /* Arbel only */
65 u32 reserved[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070066} __attribute__((packed));
67
68#define MTHCA_MPT_FLAG_SW_OWNS (0xfUL << 28)
69#define MTHCA_MPT_FLAG_MIO (1 << 17)
70#define MTHCA_MPT_FLAG_BIND_ENABLE (1 << 15)
71#define MTHCA_MPT_FLAG_PHYSICAL (1 << 9)
72#define MTHCA_MPT_FLAG_REGION (1 << 8)
73
74#define MTHCA_MTT_FLAG_PRESENT 1
75
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -070076#define MTHCA_MPT_STATUS_SW 0xF0
77#define MTHCA_MPT_STATUS_HW 0x00
78
Eli Cohen651eaac2006-03-02 12:40:46 -080079#define SINAI_FMR_KEY_INC 0x1000000
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
82 * Buddy allocator for MTT segments (currently not very efficient
83 * since it doesn't keep a free list and just searches linearly
84 * through the bitmaps)
85 */
86
Michael S. Tsirkin9095e202005-04-16 15:26:26 -070087static u32 mthca_buddy_alloc(struct mthca_buddy *buddy, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 int o;
90 int m;
91 u32 seg;
92
Michael S. Tsirkin9095e202005-04-16 15:26:26 -070093 spin_lock(&buddy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Michael S. Tsirkin9095e202005-04-16 15:26:26 -070095 for (o = order; o <= buddy->max_order; ++o) {
96 m = 1 << (buddy->max_order - o);
97 seg = find_first_bit(buddy->bits[o], m);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (seg < m)
99 goto found;
100 }
101
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700102 spin_unlock(&buddy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return -1;
104
105 found:
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700106 clear_bit(seg, buddy->bits[o]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 while (o > order) {
109 --o;
110 seg <<= 1;
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700111 set_bit(seg ^ 1, buddy->bits[o]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700114 spin_unlock(&buddy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 seg <<= order;
117
118 return seg;
119}
120
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700121static void mthca_buddy_free(struct mthca_buddy *buddy, u32 seg, int order)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 seg >>= order;
124
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700125 spin_lock(&buddy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700127 while (test_bit(seg ^ 1, buddy->bits[order])) {
128 clear_bit(seg ^ 1, buddy->bits[order]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 seg >>= 1;
130 ++order;
131 }
132
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700133 set_bit(seg, buddy->bits[order]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700135 spin_unlock(&buddy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700138static int __devinit mthca_buddy_init(struct mthca_buddy *buddy, int max_order)
Roland Dreier86562a12005-04-16 15:26:13 -0700139{
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700140 int i, s;
141
142 buddy->max_order = max_order;
143 spin_lock_init(&buddy->lock);
144
Roland Dreierde6eb662005-11-02 07:23:14 -0800145 buddy->bits = kzalloc((buddy->max_order + 1) * sizeof (long *),
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700146 GFP_KERNEL);
147 if (!buddy->bits)
148 goto err_out;
149
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700150 for (i = 0; i <= buddy->max_order; ++i) {
151 s = BITS_TO_LONGS(1 << (buddy->max_order - i));
152 buddy->bits[i] = kmalloc(s * sizeof (long), GFP_KERNEL);
153 if (!buddy->bits[i])
154 goto err_out_free;
155 bitmap_zero(buddy->bits[i],
156 1 << (buddy->max_order - i));
157 }
158
159 set_bit(0, buddy->bits[buddy->max_order]);
160
161 return 0;
162
163err_out_free:
164 for (i = 0; i <= buddy->max_order; ++i)
165 kfree(buddy->bits[i]);
166
167 kfree(buddy->bits);
168
169err_out:
170 return -ENOMEM;
171}
172
Roland Dreiere1f78682006-03-29 09:36:46 -0800173static void mthca_buddy_cleanup(struct mthca_buddy *buddy)
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700174{
175 int i;
176
177 for (i = 0; i <= buddy->max_order; ++i)
178 kfree(buddy->bits[i]);
179
180 kfree(buddy->bits);
181}
182
Roland Dreierd56d6f92005-06-27 14:36:43 -0700183static u32 mthca_alloc_mtt_range(struct mthca_dev *dev, int order,
184 struct mthca_buddy *buddy)
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700185{
186 u32 seg = mthca_buddy_alloc(buddy, order);
Roland Dreier86562a12005-04-16 15:26:13 -0700187
188 if (seg == -1)
189 return -1;
190
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700191 if (mthca_is_memfree(dev))
Roland Dreier86562a12005-04-16 15:26:13 -0700192 if (mthca_table_get_range(dev, dev->mr_table.mtt_table, seg,
193 seg + (1 << order) - 1)) {
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700194 mthca_buddy_free(buddy, seg, order);
Roland Dreier86562a12005-04-16 15:26:13 -0700195 seg = -1;
196 }
197
198 return seg;
199}
200
Roland Dreierd56d6f92005-06-27 14:36:43 -0700201static struct mthca_mtt *__mthca_alloc_mtt(struct mthca_dev *dev, int size,
202 struct mthca_buddy *buddy)
Roland Dreier86562a12005-04-16 15:26:13 -0700203{
Roland Dreierd56d6f92005-06-27 14:36:43 -0700204 struct mthca_mtt *mtt;
205 int i;
206
207 if (size <= 0)
208 return ERR_PTR(-EINVAL);
209
210 mtt = kmalloc(sizeof *mtt, GFP_KERNEL);
211 if (!mtt)
212 return ERR_PTR(-ENOMEM);
213
214 mtt->buddy = buddy;
215 mtt->order = 0;
216 for (i = MTHCA_MTT_SEG_SIZE / 8; i < size; i <<= 1)
217 ++mtt->order;
218
219 mtt->first_seg = mthca_alloc_mtt_range(dev, mtt->order, buddy);
220 if (mtt->first_seg == -1) {
221 kfree(mtt);
222 return ERR_PTR(-ENOMEM);
223 }
224
225 return mtt;
226}
227
228struct mthca_mtt *mthca_alloc_mtt(struct mthca_dev *dev, int size)
229{
230 return __mthca_alloc_mtt(dev, size, &dev->mr_table.mtt_buddy);
231}
232
233void mthca_free_mtt(struct mthca_dev *dev, struct mthca_mtt *mtt)
234{
235 if (!mtt)
236 return;
237
238 mthca_buddy_free(mtt->buddy, mtt->first_seg, mtt->order);
239
240 mthca_table_put_range(dev, dev->mr_table.mtt_table,
241 mtt->first_seg,
242 mtt->first_seg + (1 << mtt->order) - 1);
243
244 kfree(mtt);
245}
246
247int mthca_write_mtt(struct mthca_dev *dev, struct mthca_mtt *mtt,
248 int start_index, u64 *buffer_list, int list_len)
249{
Roland Dreiered878452005-06-27 14:36:45 -0700250 struct mthca_mailbox *mailbox;
Sean Hefty97f52eb2005-08-13 21:05:57 -0700251 __be64 *mtt_entry;
Roland Dreierd56d6f92005-06-27 14:36:43 -0700252 int err = 0;
253 u8 status;
254 int i;
255
Roland Dreiered878452005-06-27 14:36:45 -0700256 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
257 if (IS_ERR(mailbox))
258 return PTR_ERR(mailbox);
259 mtt_entry = mailbox->buf;
Roland Dreierd56d6f92005-06-27 14:36:43 -0700260
261 while (list_len > 0) {
262 mtt_entry[0] = cpu_to_be64(dev->mr_table.mtt_base +
263 mtt->first_seg * MTHCA_MTT_SEG_SIZE +
264 start_index * 8);
265 mtt_entry[1] = 0;
Roland Dreiered878452005-06-27 14:36:45 -0700266 for (i = 0; i < list_len && i < MTHCA_MAILBOX_SIZE / 8 - 2; ++i)
Roland Dreierd56d6f92005-06-27 14:36:43 -0700267 mtt_entry[i + 2] = cpu_to_be64(buffer_list[i] |
268 MTHCA_MTT_FLAG_PRESENT);
269
270 /*
271 * If we have an odd number of entries to write, add
272 * one more dummy entry for firmware efficiency.
273 */
274 if (i & 1)
275 mtt_entry[i + 2] = 0;
276
Roland Dreiered878452005-06-27 14:36:45 -0700277 err = mthca_WRITE_MTT(dev, mailbox, (i + 1) & ~1, &status);
Roland Dreierd56d6f92005-06-27 14:36:43 -0700278 if (err) {
279 mthca_warn(dev, "WRITE_MTT failed (%d)\n", err);
280 goto out;
281 }
282 if (status) {
283 mthca_warn(dev, "WRITE_MTT returned status 0x%02x\n",
284 status);
285 err = -EINVAL;
286 goto out;
287 }
288
289 list_len -= i;
290 start_index += i;
291 buffer_list += i;
292 }
293
294out:
Roland Dreiered878452005-06-27 14:36:45 -0700295 mthca_free_mailbox(dev, mailbox);
Roland Dreierd56d6f92005-06-27 14:36:43 -0700296 return err;
Roland Dreier86562a12005-04-16 15:26:13 -0700297}
298
Michael S. Tsirkind0a9d252005-04-16 15:26:30 -0700299static inline u32 tavor_hw_index_to_key(u32 ind)
300{
301 return ind;
302}
303
304static inline u32 tavor_key_to_hw_index(u32 key)
305{
306 return key;
307}
308
309static inline u32 arbel_hw_index_to_key(u32 ind)
310{
311 return (ind >> 24) | (ind << 8);
312}
313
314static inline u32 arbel_key_to_hw_index(u32 key)
315{
316 return (key << 24) | (key >> 8);
317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static inline u32 hw_index_to_key(struct mthca_dev *dev, u32 ind)
320{
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700321 if (mthca_is_memfree(dev))
Michael S. Tsirkind0a9d252005-04-16 15:26:30 -0700322 return arbel_hw_index_to_key(ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 else
Michael S. Tsirkind0a9d252005-04-16 15:26:30 -0700324 return tavor_hw_index_to_key(ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327static inline u32 key_to_hw_index(struct mthca_dev *dev, u32 key)
328{
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700329 if (mthca_is_memfree(dev))
Michael S. Tsirkind0a9d252005-04-16 15:26:30 -0700330 return arbel_key_to_hw_index(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 else
Michael S. Tsirkind0a9d252005-04-16 15:26:30 -0700332 return tavor_key_to_hw_index(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Eli Cohen651eaac2006-03-02 12:40:46 -0800335static inline u32 adjust_key(struct mthca_dev *dev, u32 key)
336{
337 if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
338 return ((key << 20) & 0x800000) | (key & 0x7fffff);
339 else
340 return key;
341}
342
Roland Dreierd56d6f92005-06-27 14:36:43 -0700343int mthca_mr_alloc(struct mthca_dev *dev, u32 pd, int buffer_size_shift,
344 u64 iova, u64 total_size, u32 access, struct mthca_mr *mr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Roland Dreiered878452005-06-27 14:36:45 -0700346 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct mthca_mpt_entry *mpt_entry;
348 u32 key;
Roland Dreierd56d6f92005-06-27 14:36:43 -0700349 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int err;
351 u8 status;
352
Roland Dreierd56d6f92005-06-27 14:36:43 -0700353 WARN_ON(buffer_size_shift >= 32);
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 key = mthca_alloc(&dev->mr_table.mpt_alloc);
356 if (key == -1)
357 return -ENOMEM;
Eli Cohen651eaac2006-03-02 12:40:46 -0800358 key = adjust_key(dev, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);
360
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700361 if (mthca_is_memfree(dev)) {
Roland Dreier86562a12005-04-16 15:26:13 -0700362 err = mthca_table_get(dev, dev->mr_table.mpt_table, key);
363 if (err)
364 goto err_out_mpt_free;
365 }
366
Roland Dreiered878452005-06-27 14:36:45 -0700367 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
368 if (IS_ERR(mailbox)) {
369 err = PTR_ERR(mailbox);
Roland Dreier86562a12005-04-16 15:26:13 -0700370 goto err_out_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Roland Dreiered878452005-06-27 14:36:45 -0700372 mpt_entry = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS |
375 MTHCA_MPT_FLAG_MIO |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 MTHCA_MPT_FLAG_REGION |
377 access);
Roland Dreierd56d6f92005-06-27 14:36:43 -0700378 if (!mr->mtt)
379 mpt_entry->flags |= cpu_to_be32(MTHCA_MPT_FLAG_PHYSICAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 mpt_entry->page_size = cpu_to_be32(buffer_size_shift - 12);
382 mpt_entry->key = cpu_to_be32(key);
383 mpt_entry->pd = cpu_to_be32(pd);
384 mpt_entry->start = cpu_to_be64(iova);
385 mpt_entry->length = cpu_to_be64(total_size);
Roland Dreierd56d6f92005-06-27 14:36:43 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 memset(&mpt_entry->lkey, 0,
388 sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, lkey));
Roland Dreierd56d6f92005-06-27 14:36:43 -0700389
390 if (mr->mtt)
391 mpt_entry->mtt_seg =
392 cpu_to_be64(dev->mr_table.mtt_base +
393 mr->mtt->first_seg * MTHCA_MTT_SEG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (0) {
396 mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey);
397 for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) {
398 if (i % 4 == 0)
399 printk("[%02x] ", i * 4);
Sean Hefty97f52eb2005-08-13 21:05:57 -0700400 printk(" %08x", be32_to_cpu(((__be32 *) mpt_entry)[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if ((i + 1) % 4 == 0)
402 printk("\n");
403 }
404 }
405
Roland Dreiered878452005-06-27 14:36:45 -0700406 err = mthca_SW2HW_MPT(dev, mailbox,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 key & (dev->limits.num_mpts - 1),
408 &status);
Roland Dreierd56d6f92005-06-27 14:36:43 -0700409 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
Roland Dreierd56d6f92005-06-27 14:36:43 -0700411 goto err_out_mailbox;
412 } else if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n",
414 status);
415 err = -EINVAL;
Roland Dreierd56d6f92005-06-27 14:36:43 -0700416 goto err_out_mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Roland Dreiered878452005-06-27 14:36:45 -0700419 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return err;
421
Roland Dreierd56d6f92005-06-27 14:36:43 -0700422err_out_mailbox:
Roland Dreiered878452005-06-27 14:36:45 -0700423 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Roland Dreier86562a12005-04-16 15:26:13 -0700425err_out_table:
Roland Dreiera03a5a62005-06-27 14:36:43 -0700426 mthca_table_put(dev, dev->mr_table.mpt_table, key);
Roland Dreier86562a12005-04-16 15:26:13 -0700427
428err_out_mpt_free:
Michael S. Tsirkin55645e92005-04-16 15:26:19 -0700429 mthca_free(&dev->mr_table.mpt_alloc, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return err;
431}
432
Roland Dreierd56d6f92005-06-27 14:36:43 -0700433int mthca_mr_alloc_notrans(struct mthca_dev *dev, u32 pd,
434 u32 access, struct mthca_mr *mr)
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700435{
Roland Dreierd56d6f92005-06-27 14:36:43 -0700436 mr->mtt = NULL;
437 return mthca_mr_alloc(dev, pd, 12, 0, ~0ULL, access, mr);
438}
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700439
Roland Dreierd56d6f92005-06-27 14:36:43 -0700440int mthca_mr_alloc_phys(struct mthca_dev *dev, u32 pd,
441 u64 *buffer_list, int buffer_size_shift,
442 int list_len, u64 iova, u64 total_size,
443 u32 access, struct mthca_mr *mr)
444{
445 int err;
446
447 mr->mtt = mthca_alloc_mtt(dev, list_len);
448 if (IS_ERR(mr->mtt))
449 return PTR_ERR(mr->mtt);
450
451 err = mthca_write_mtt(dev, mr->mtt, 0, buffer_list, list_len);
452 if (err) {
453 mthca_free_mtt(dev, mr->mtt);
454 return err;
455 }
456
457 err = mthca_mr_alloc(dev, pd, buffer_size_shift, iova,
458 total_size, access, mr);
459 if (err)
460 mthca_free_mtt(dev, mr->mtt);
461
462 return err;
463}
464
465/* Free mr or fmr */
466static void mthca_free_region(struct mthca_dev *dev, u32 lkey)
467{
Roland Dreiera03a5a62005-06-27 14:36:43 -0700468 mthca_table_put(dev, dev->mr_table.mpt_table,
Guy German7f9f2db2005-08-15 07:38:50 -0700469 key_to_hw_index(dev, lkey));
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700470
471 mthca_free(&dev->mr_table.mpt_alloc, key_to_hw_index(dev, lkey));
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474void mthca_free_mr(struct mthca_dev *dev, struct mthca_mr *mr)
475{
476 int err;
477 u8 status;
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 err = mthca_HW2SW_MPT(dev, NULL,
480 key_to_hw_index(dev, mr->ibmr.lkey) &
481 (dev->limits.num_mpts - 1),
482 &status);
483 if (err)
484 mthca_warn(dev, "HW2SW_MPT failed (%d)\n", err);
485 else if (status)
486 mthca_warn(dev, "HW2SW_MPT returned status 0x%02x\n",
487 status);
488
Roland Dreierd56d6f92005-06-27 14:36:43 -0700489 mthca_free_region(dev, mr->ibmr.lkey);
490 mthca_free_mtt(dev, mr->mtt);
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700491}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700493int mthca_fmr_alloc(struct mthca_dev *dev, u32 pd,
494 u32 access, struct mthca_fmr *mr)
495{
496 struct mthca_mpt_entry *mpt_entry;
Roland Dreiered878452005-06-27 14:36:45 -0700497 struct mthca_mailbox *mailbox;
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700498 u64 mtt_seg;
499 u32 key, idx;
500 u8 status;
501 int list_len = mr->attr.max_pages;
502 int err = -ENOMEM;
503 int i;
504
Or Gerlitzd36f34a2006-02-02 10:43:45 -0800505 if (mr->attr.page_shift < 12 || mr->attr.page_shift >= 32)
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700506 return -EINVAL;
507
508 /* For Arbel, all MTTs must fit in the same page. */
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700509 if (mthca_is_memfree(dev) &&
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700510 mr->attr.max_pages * sizeof *mr->mem.arbel.mtts > PAGE_SIZE)
511 return -EINVAL;
512
513 mr->maps = 0;
514
515 key = mthca_alloc(&dev->mr_table.mpt_alloc);
516 if (key == -1)
517 return -ENOMEM;
Eli Cohen651eaac2006-03-02 12:40:46 -0800518 key = adjust_key(dev, key);
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700519
520 idx = key & (dev->limits.num_mpts - 1);
521 mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key);
522
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700523 if (mthca_is_memfree(dev)) {
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700524 err = mthca_table_get(dev, dev->mr_table.mpt_table, key);
525 if (err)
526 goto err_out_mpt_free;
527
528 mr->mem.arbel.mpt = mthca_table_find(dev->mr_table.mpt_table, key);
529 BUG_ON(!mr->mem.arbel.mpt);
530 } else
531 mr->mem.tavor.mpt = dev->mr_table.tavor_fmr.mpt_base +
Roland Dreier2fa5e2e2006-02-01 13:38:24 -0800532 sizeof *(mr->mem.tavor.mpt) * idx;
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700533
Roland Dreierd56d6f92005-06-27 14:36:43 -0700534 mr->mtt = __mthca_alloc_mtt(dev, list_len, dev->mr_table.fmr_mtt_buddy);
535 if (IS_ERR(mr->mtt))
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700536 goto err_out_table;
537
Roland Dreierd56d6f92005-06-27 14:36:43 -0700538 mtt_seg = mr->mtt->first_seg * MTHCA_MTT_SEG_SIZE;
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700539
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700540 if (mthca_is_memfree(dev)) {
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700541 mr->mem.arbel.mtts = mthca_table_find(dev->mr_table.mtt_table,
Roland Dreierd56d6f92005-06-27 14:36:43 -0700542 mr->mtt->first_seg);
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700543 BUG_ON(!mr->mem.arbel.mtts);
544 } else
545 mr->mem.tavor.mtts = dev->mr_table.tavor_fmr.mtt_base + mtt_seg;
546
Roland Dreiered878452005-06-27 14:36:45 -0700547 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
548 if (IS_ERR(mailbox))
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700549 goto err_out_free_mtt;
550
Roland Dreiered878452005-06-27 14:36:45 -0700551 mpt_entry = mailbox->buf;
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700552
553 mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS |
554 MTHCA_MPT_FLAG_MIO |
555 MTHCA_MPT_FLAG_REGION |
556 access);
557
Or Gerlitzd36f34a2006-02-02 10:43:45 -0800558 mpt_entry->page_size = cpu_to_be32(mr->attr.page_shift - 12);
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700559 mpt_entry->key = cpu_to_be32(key);
560 mpt_entry->pd = cpu_to_be32(pd);
561 memset(&mpt_entry->start, 0,
562 sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, start));
563 mpt_entry->mtt_seg = cpu_to_be64(dev->mr_table.mtt_base + mtt_seg);
564
565 if (0) {
566 mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey);
567 for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) {
568 if (i % 4 == 0)
569 printk("[%02x] ", i * 4);
Sean Hefty97f52eb2005-08-13 21:05:57 -0700570 printk(" %08x", be32_to_cpu(((__be32 *) mpt_entry)[i]));
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700571 if ((i + 1) % 4 == 0)
572 printk("\n");
573 }
574 }
575
Roland Dreiered878452005-06-27 14:36:45 -0700576 err = mthca_SW2HW_MPT(dev, mailbox,
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700577 key & (dev->limits.num_mpts - 1),
578 &status);
579 if (err) {
580 mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err);
581 goto err_out_mailbox_free;
582 }
583 if (status) {
584 mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n",
585 status);
586 err = -EINVAL;
587 goto err_out_mailbox_free;
588 }
589
Roland Dreiered878452005-06-27 14:36:45 -0700590 mthca_free_mailbox(dev, mailbox);
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700591 return 0;
592
593err_out_mailbox_free:
Roland Dreiered878452005-06-27 14:36:45 -0700594 mthca_free_mailbox(dev, mailbox);
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700595
596err_out_free_mtt:
Roland Dreierd56d6f92005-06-27 14:36:43 -0700597 mthca_free_mtt(dev, mr->mtt);
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700598
599err_out_table:
Roland Dreiera03a5a62005-06-27 14:36:43 -0700600 mthca_table_put(dev, dev->mr_table.mpt_table, key);
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700601
602err_out_mpt_free:
603 mthca_free(&dev->mr_table.mpt_alloc, mr->ibmr.lkey);
604 return err;
605}
606
607int mthca_free_fmr(struct mthca_dev *dev, struct mthca_fmr *fmr)
608{
609 if (fmr->maps)
610 return -EBUSY;
611
Roland Dreierd56d6f92005-06-27 14:36:43 -0700612 mthca_free_region(dev, fmr->ibmr.lkey);
613 mthca_free_mtt(dev, fmr->mtt);
614
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700615 return 0;
616}
617
618static inline int mthca_check_fmr(struct mthca_fmr *fmr, u64 *page_list,
619 int list_len, u64 iova)
620{
621 int i, page_mask;
622
623 if (list_len > fmr->attr.max_pages)
624 return -EINVAL;
625
Or Gerlitzd36f34a2006-02-02 10:43:45 -0800626 page_mask = (1 << fmr->attr.page_shift) - 1;
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700627
628 /* We are getting page lists, so va must be page aligned. */
629 if (iova & page_mask)
630 return -EINVAL;
631
632 /* Trust the user not to pass misaligned data in page_list */
633 if (0)
634 for (i = 0; i < list_len; ++i) {
635 if (page_list[i] & ~page_mask)
636 return -EINVAL;
637 }
638
639 if (fmr->maps >= fmr->attr.max_maps)
640 return -EINVAL;
641
642 return 0;
643}
644
645
646int mthca_tavor_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
647 int list_len, u64 iova)
648{
649 struct mthca_fmr *fmr = to_mfmr(ibfmr);
650 struct mthca_dev *dev = to_mdev(ibfmr->device);
651 struct mthca_mpt_entry mpt_entry;
652 u32 key;
653 int i, err;
654
655 err = mthca_check_fmr(fmr, page_list, list_len, iova);
656 if (err)
657 return err;
658
659 ++fmr->maps;
660
661 key = tavor_key_to_hw_index(fmr->ibmr.lkey);
662 key += dev->limits.num_mpts;
663 fmr->ibmr.lkey = fmr->ibmr.rkey = tavor_hw_index_to_key(key);
664
665 writeb(MTHCA_MPT_STATUS_SW, fmr->mem.tavor.mpt);
666
667 for (i = 0; i < list_len; ++i) {
668 __be64 mtt_entry = cpu_to_be64(page_list[i] |
669 MTHCA_MTT_FLAG_PRESENT);
670 mthca_write64_raw(mtt_entry, fmr->mem.tavor.mtts + i);
671 }
672
673 mpt_entry.lkey = cpu_to_be32(key);
Or Gerlitzd36f34a2006-02-02 10:43:45 -0800674 mpt_entry.length = cpu_to_be64(list_len * (1ull << fmr->attr.page_shift));
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700675 mpt_entry.start = cpu_to_be64(iova);
676
Sean Hefty97f52eb2005-08-13 21:05:57 -0700677 __raw_writel((__force u32) mpt_entry.lkey, &fmr->mem.tavor.mpt->key);
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700678 memcpy_toio(&fmr->mem.tavor.mpt->start, &mpt_entry.start,
679 offsetof(struct mthca_mpt_entry, window_count) -
680 offsetof(struct mthca_mpt_entry, start));
681
682 writeb(MTHCA_MPT_STATUS_HW, fmr->mem.tavor.mpt);
683
684 return 0;
685}
686
687int mthca_arbel_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
688 int list_len, u64 iova)
689{
690 struct mthca_fmr *fmr = to_mfmr(ibfmr);
691 struct mthca_dev *dev = to_mdev(ibfmr->device);
692 u32 key;
693 int i, err;
694
695 err = mthca_check_fmr(fmr, page_list, list_len, iova);
696 if (err)
697 return err;
698
699 ++fmr->maps;
700
701 key = arbel_key_to_hw_index(fmr->ibmr.lkey);
Eli Cohen651eaac2006-03-02 12:40:46 -0800702 if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
703 key += SINAI_FMR_KEY_INC;
704 else
705 key += dev->limits.num_mpts;
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700706 fmr->ibmr.lkey = fmr->ibmr.rkey = arbel_hw_index_to_key(key);
707
708 *(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_SW;
709
710 wmb();
711
712 for (i = 0; i < list_len; ++i)
713 fmr->mem.arbel.mtts[i] = cpu_to_be64(page_list[i] |
714 MTHCA_MTT_FLAG_PRESENT);
715
716 fmr->mem.arbel.mpt->key = cpu_to_be32(key);
717 fmr->mem.arbel.mpt->lkey = cpu_to_be32(key);
Or Gerlitzd36f34a2006-02-02 10:43:45 -0800718 fmr->mem.arbel.mpt->length = cpu_to_be64(list_len * (1ull << fmr->attr.page_shift));
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700719 fmr->mem.arbel.mpt->start = cpu_to_be64(iova);
720
721 wmb();
722
723 *(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_HW;
724
725 wmb();
726
727 return 0;
728}
729
730void mthca_tavor_fmr_unmap(struct mthca_dev *dev, struct mthca_fmr *fmr)
731{
732 u32 key;
733
734 if (!fmr->maps)
735 return;
736
737 key = tavor_key_to_hw_index(fmr->ibmr.lkey);
738 key &= dev->limits.num_mpts - 1;
739 fmr->ibmr.lkey = fmr->ibmr.rkey = tavor_hw_index_to_key(key);
740
741 fmr->maps = 0;
742
743 writeb(MTHCA_MPT_STATUS_SW, fmr->mem.tavor.mpt);
744}
745
746void mthca_arbel_fmr_unmap(struct mthca_dev *dev, struct mthca_fmr *fmr)
747{
748 u32 key;
749
750 if (!fmr->maps)
751 return;
752
753 key = arbel_key_to_hw_index(fmr->ibmr.lkey);
754 key &= dev->limits.num_mpts - 1;
755 fmr->ibmr.lkey = fmr->ibmr.rkey = arbel_hw_index_to_key(key);
756
757 fmr->maps = 0;
758
759 *(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_SW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762int __devinit mthca_init_mr_table(struct mthca_dev *dev)
763{
Michael S. Tsirkince477ae2006-05-10 17:58:41 +0300764 unsigned long addr;
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700765 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 err = mthca_alloc_init(&dev->mr_table.mpt_alloc,
768 dev->limits.num_mpts,
769 ~0, dev->limits.reserved_mrws);
770 if (err)
771 return err;
772
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700773 if (!mthca_is_memfree(dev) &&
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700774 (dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN))
775 dev->limits.fmr_reserved_mtts = 0;
776 else
777 dev->mthca_flags |= MTHCA_FLAG_FMR;
778
Eli Cohen651eaac2006-03-02 12:40:46 -0800779 if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
780 mthca_dbg(dev, "Memory key throughput optimization activated.\n");
781
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700782 err = mthca_buddy_init(&dev->mr_table.mtt_buddy,
783 fls(dev->limits.num_mtt_segs - 1));
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700784
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700785 if (err)
786 goto err_mtt_buddy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700788 dev->mr_table.tavor_fmr.mpt_base = NULL;
789 dev->mr_table.tavor_fmr.mtt_base = NULL;
790
791 if (dev->limits.fmr_reserved_mtts) {
792 i = fls(dev->limits.fmr_reserved_mtts - 1);
793
794 if (i >= 31) {
795 mthca_warn(dev, "Unable to reserve 2^31 FMR MTTs.\n");
796 err = -EINVAL;
797 goto err_fmr_mpt;
798 }
799
Michael S. Tsirkince477ae2006-05-10 17:58:41 +0300800 addr = pci_resource_start(dev->pdev, 4) +
801 ((pci_resource_len(dev->pdev, 4) - 1) &
802 dev->mr_table.mpt_base);
803
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700804 dev->mr_table.tavor_fmr.mpt_base =
Michael S. Tsirkince477ae2006-05-10 17:58:41 +0300805 ioremap(addr, (1 << i) * sizeof(struct mthca_mpt_entry));
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700806
807 if (!dev->mr_table.tavor_fmr.mpt_base) {
808 mthca_warn(dev, "MPT ioremap for FMR failed.\n");
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700809 err = -ENOMEM;
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700810 goto err_fmr_mpt;
811 }
812
Michael S. Tsirkince477ae2006-05-10 17:58:41 +0300813 addr = pci_resource_start(dev->pdev, 4) +
814 ((pci_resource_len(dev->pdev, 4) - 1) &
815 dev->mr_table.mtt_base);
816
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700817 dev->mr_table.tavor_fmr.mtt_base =
Michael S. Tsirkince477ae2006-05-10 17:58:41 +0300818 ioremap(addr, (1 << i) * MTHCA_MTT_SEG_SIZE);
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700819 if (!dev->mr_table.tavor_fmr.mtt_base) {
820 mthca_warn(dev, "MTT ioremap for FMR failed.\n");
821 err = -ENOMEM;
822 goto err_fmr_mtt;
823 }
824
825 err = mthca_buddy_init(&dev->mr_table.tavor_fmr.mtt_buddy, i);
826 if (err)
827 goto err_fmr_mtt_buddy;
828
829 /* Prevent regular MRs from using FMR keys */
830 err = mthca_buddy_alloc(&dev->mr_table.mtt_buddy, i);
831 if (err)
832 goto err_reserve_fmr;
833
834 dev->mr_table.fmr_mtt_buddy =
Roland Dreier2fa5e2e2006-02-01 13:38:24 -0800835 &dev->mr_table.tavor_fmr.mtt_buddy;
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700836 } else
837 dev->mr_table.fmr_mtt_buddy = &dev->mr_table.mtt_buddy;
838
839 /* FMR table is always the first, take reserved MTTs out of there */
840 if (dev->limits.reserved_mtts) {
841 i = fls(dev->limits.reserved_mtts - 1);
842
Roland Dreierd56d6f92005-06-27 14:36:43 -0700843 if (mthca_alloc_mtt_range(dev, i,
844 dev->mr_table.fmr_mtt_buddy) == -1) {
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700845 mthca_warn(dev, "MTT table of order %d is too small.\n",
846 dev->mr_table.fmr_mtt_buddy->max_order);
847 err = -ENOMEM;
848 goto err_reserve_mtts;
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return 0;
853
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700854err_reserve_mtts:
855err_reserve_fmr:
856 if (dev->limits.fmr_reserved_mtts)
857 mthca_buddy_cleanup(&dev->mr_table.tavor_fmr.mtt_buddy);
858
859err_fmr_mtt_buddy:
860 if (dev->mr_table.tavor_fmr.mtt_base)
861 iounmap(dev->mr_table.tavor_fmr.mtt_base);
862
863err_fmr_mtt:
864 if (dev->mr_table.tavor_fmr.mpt_base)
865 iounmap(dev->mr_table.tavor_fmr.mpt_base);
866
867err_fmr_mpt:
868 mthca_buddy_cleanup(&dev->mr_table.mtt_buddy);
869
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700870err_mtt_buddy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);
872
873 return err;
874}
875
Roland Dreiere1f78682006-03-29 09:36:46 -0800876void mthca_cleanup_mr_table(struct mthca_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /* XXX check if any MRs are still allocated? */
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700879 if (dev->limits.fmr_reserved_mtts)
880 mthca_buddy_cleanup(&dev->mr_table.tavor_fmr.mtt_buddy);
881
Michael S. Tsirkin9095e202005-04-16 15:26:26 -0700882 mthca_buddy_cleanup(&dev->mr_table.mtt_buddy);
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -0700883
884 if (dev->mr_table.tavor_fmr.mtt_base)
885 iounmap(dev->mr_table.tavor_fmr.mtt_base);
886 if (dev->mr_table.tavor_fmr.mpt_base)
887 iounmap(dev->mr_table.tavor_fmr.mpt_base);
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 mthca_alloc_cleanup(&dev->mr_table.mpt_alloc);
890}