Complete the renaming to TuxOnIce with function names, vars etc.
[linux-2.6/suspend2-head.git] / drivers / net / mlx4 / mr.c
blobd0808fa3ec822c0d3b231202c535f673f891b597
1 /*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
35 #include <linux/init.h>
36 #include <linux/errno.h>
38 #include <linux/mlx4/cmd.h>
40 #include "mlx4.h"
41 #include "icm.h"
44 * Must be packed because mtt_seg is 64 bits but only aligned to 32 bits.
46 struct mlx4_mpt_entry {
47 __be32 flags;
48 __be32 qpn;
49 __be32 key;
50 __be32 pd;
51 __be64 start;
52 __be64 length;
53 __be32 lkey;
54 __be32 win_cnt;
55 u8 reserved1[3];
56 u8 mtt_rep;
57 __be64 mtt_seg;
58 __be32 mtt_sz;
59 __be32 entity_size;
60 __be32 first_byte_offset;
61 } __attribute__((packed));
63 #define MLX4_MPT_FLAG_SW_OWNS (0xfUL << 28)
64 #define MLX4_MPT_FLAG_MIO (1 << 17)
65 #define MLX4_MPT_FLAG_BIND_ENABLE (1 << 15)
66 #define MLX4_MPT_FLAG_PHYSICAL (1 << 9)
67 #define MLX4_MPT_FLAG_REGION (1 << 8)
69 #define MLX4_MTT_FLAG_PRESENT 1
71 static u32 mlx4_buddy_alloc(struct mlx4_buddy *buddy, int order)
73 int o;
74 int m;
75 u32 seg;
77 spin_lock(&buddy->lock);
79 for (o = order; o <= buddy->max_order; ++o) {
80 m = 1 << (buddy->max_order - o);
81 seg = find_first_bit(buddy->bits[o], m);
82 if (seg < m)
83 goto found;
86 spin_unlock(&buddy->lock);
87 return -1;
89 found:
90 clear_bit(seg, buddy->bits[o]);
92 while (o > order) {
93 --o;
94 seg <<= 1;
95 set_bit(seg ^ 1, buddy->bits[o]);
98 spin_unlock(&buddy->lock);
100 seg <<= order;
102 return seg;
105 static void mlx4_buddy_free(struct mlx4_buddy *buddy, u32 seg, int order)
107 seg >>= order;
109 spin_lock(&buddy->lock);
111 while (test_bit(seg ^ 1, buddy->bits[order])) {
112 clear_bit(seg ^ 1, buddy->bits[order]);
113 seg >>= 1;
114 ++order;
117 set_bit(seg, buddy->bits[order]);
119 spin_unlock(&buddy->lock);
122 static int __devinit mlx4_buddy_init(struct mlx4_buddy *buddy, int max_order)
124 int i, s;
126 buddy->max_order = max_order;
127 spin_lock_init(&buddy->lock);
129 buddy->bits = kzalloc((buddy->max_order + 1) * sizeof (long *),
130 GFP_KERNEL);
131 if (!buddy->bits)
132 goto err_out;
134 for (i = 0; i <= buddy->max_order; ++i) {
135 s = BITS_TO_LONGS(1 << (buddy->max_order - i));
136 buddy->bits[i] = kmalloc(s * sizeof (long), GFP_KERNEL);
137 if (!buddy->bits[i])
138 goto err_out_free;
139 bitmap_zero(buddy->bits[i], 1 << (buddy->max_order - i));
142 set_bit(0, buddy->bits[buddy->max_order]);
144 return 0;
146 err_out_free:
147 for (i = 0; i <= buddy->max_order; ++i)
148 kfree(buddy->bits[i]);
150 kfree(buddy->bits);
152 err_out:
153 return -ENOMEM;
156 static void mlx4_buddy_cleanup(struct mlx4_buddy *buddy)
158 int i;
160 for (i = 0; i <= buddy->max_order; ++i)
161 kfree(buddy->bits[i]);
163 kfree(buddy->bits);
166 static u32 mlx4_alloc_mtt_range(struct mlx4_dev *dev, int order)
168 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
169 u32 seg;
171 seg = mlx4_buddy_alloc(&mr_table->mtt_buddy, order);
172 if (seg == -1)
173 return -1;
175 if (mlx4_table_get_range(dev, &mr_table->mtt_table, seg,
176 seg + (1 << order) - 1)) {
177 mlx4_buddy_free(&mr_table->mtt_buddy, seg, order);
178 return -1;
181 return seg;
184 int mlx4_mtt_init(struct mlx4_dev *dev, int npages, int page_shift,
185 struct mlx4_mtt *mtt)
187 int i;
189 if (!npages) {
190 mtt->order = -1;
191 mtt->page_shift = MLX4_ICM_PAGE_SHIFT;
192 return 0;
193 } else
194 mtt->page_shift = page_shift;
196 for (mtt->order = 0, i = MLX4_MTT_ENTRY_PER_SEG; i < npages; i <<= 1)
197 ++mtt->order;
199 mtt->first_seg = mlx4_alloc_mtt_range(dev, mtt->order);
200 if (mtt->first_seg == -1)
201 return -ENOMEM;
203 return 0;
205 EXPORT_SYMBOL_GPL(mlx4_mtt_init);
207 void mlx4_mtt_cleanup(struct mlx4_dev *dev, struct mlx4_mtt *mtt)
209 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
211 if (mtt->order < 0)
212 return;
214 mlx4_buddy_free(&mr_table->mtt_buddy, mtt->first_seg, mtt->order);
215 mlx4_table_put_range(dev, &mr_table->mtt_table, mtt->first_seg,
216 mtt->first_seg + (1 << mtt->order) - 1);
218 EXPORT_SYMBOL_GPL(mlx4_mtt_cleanup);
220 u64 mlx4_mtt_addr(struct mlx4_dev *dev, struct mlx4_mtt *mtt)
222 return (u64) mtt->first_seg * dev->caps.mtt_entry_sz;
224 EXPORT_SYMBOL_GPL(mlx4_mtt_addr);
226 static u32 hw_index_to_key(u32 ind)
228 return (ind >> 24) | (ind << 8);
231 static u32 key_to_hw_index(u32 key)
233 return (key << 24) | (key >> 8);
236 static int mlx4_SW2HW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
237 int mpt_index)
239 return mlx4_cmd(dev, mailbox->dma, mpt_index, 0, MLX4_CMD_SW2HW_MPT,
240 MLX4_CMD_TIME_CLASS_B);
243 static int mlx4_HW2SW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
244 int mpt_index)
246 return mlx4_cmd_box(dev, 0, mailbox ? mailbox->dma : 0, mpt_index,
247 !mailbox, MLX4_CMD_HW2SW_MPT, MLX4_CMD_TIME_CLASS_B);
250 int mlx4_mr_alloc(struct mlx4_dev *dev, u32 pd, u64 iova, u64 size, u32 access,
251 int npages, int page_shift, struct mlx4_mr *mr)
253 struct mlx4_priv *priv = mlx4_priv(dev);
254 u32 index;
255 int err;
257 index = mlx4_bitmap_alloc(&priv->mr_table.mpt_bitmap);
258 if (index == -1) {
259 err = -ENOMEM;
260 goto err;
263 mr->iova = iova;
264 mr->size = size;
265 mr->pd = pd;
266 mr->access = access;
267 mr->enabled = 0;
268 mr->key = hw_index_to_key(index);
270 err = mlx4_mtt_init(dev, npages, page_shift, &mr->mtt);
271 if (err)
272 goto err_index;
274 return 0;
276 err_index:
277 mlx4_bitmap_free(&priv->mr_table.mpt_bitmap, index);
279 err:
280 kfree(mr);
281 return err;
283 EXPORT_SYMBOL_GPL(mlx4_mr_alloc);
285 void mlx4_mr_free(struct mlx4_dev *dev, struct mlx4_mr *mr)
287 struct mlx4_priv *priv = mlx4_priv(dev);
288 int err;
290 if (mr->enabled) {
291 err = mlx4_HW2SW_MPT(dev, NULL,
292 key_to_hw_index(mr->key) &
293 (dev->caps.num_mpts - 1));
294 if (err)
295 mlx4_warn(dev, "HW2SW_MPT failed (%d)\n", err);
298 mlx4_mtt_cleanup(dev, &mr->mtt);
299 mlx4_bitmap_free(&priv->mr_table.mpt_bitmap, key_to_hw_index(mr->key));
301 EXPORT_SYMBOL_GPL(mlx4_mr_free);
303 int mlx4_mr_enable(struct mlx4_dev *dev, struct mlx4_mr *mr)
305 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
306 struct mlx4_cmd_mailbox *mailbox;
307 struct mlx4_mpt_entry *mpt_entry;
308 int err;
310 err = mlx4_table_get(dev, &mr_table->dmpt_table, key_to_hw_index(mr->key));
311 if (err)
312 return err;
314 mailbox = mlx4_alloc_cmd_mailbox(dev);
315 if (IS_ERR(mailbox)) {
316 err = PTR_ERR(mailbox);
317 goto err_table;
319 mpt_entry = mailbox->buf;
321 memset(mpt_entry, 0, sizeof *mpt_entry);
323 mpt_entry->flags = cpu_to_be32(MLX4_MPT_FLAG_SW_OWNS |
324 MLX4_MPT_FLAG_MIO |
325 MLX4_MPT_FLAG_REGION |
326 mr->access);
328 mpt_entry->key = cpu_to_be32(key_to_hw_index(mr->key));
329 mpt_entry->pd = cpu_to_be32(mr->pd);
330 mpt_entry->start = cpu_to_be64(mr->iova);
331 mpt_entry->length = cpu_to_be64(mr->size);
332 mpt_entry->entity_size = cpu_to_be32(mr->mtt.page_shift);
333 if (mr->mtt.order < 0) {
334 mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_PHYSICAL);
335 mpt_entry->mtt_seg = 0;
336 } else
337 mpt_entry->mtt_seg = cpu_to_be64(mlx4_mtt_addr(dev, &mr->mtt));
339 err = mlx4_SW2HW_MPT(dev, mailbox,
340 key_to_hw_index(mr->key) & (dev->caps.num_mpts - 1));
341 if (err) {
342 mlx4_warn(dev, "SW2HW_MPT failed (%d)\n", err);
343 goto err_cmd;
346 mr->enabled = 1;
348 mlx4_free_cmd_mailbox(dev, mailbox);
350 return 0;
352 err_cmd:
353 mlx4_free_cmd_mailbox(dev, mailbox);
355 err_table:
356 mlx4_table_put(dev, &mr_table->dmpt_table, key_to_hw_index(mr->key));
357 return err;
359 EXPORT_SYMBOL_GPL(mlx4_mr_enable);
361 static int mlx4_WRITE_MTT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
362 int num_mtt)
364 return mlx4_cmd(dev, mailbox->dma, num_mtt, 0, MLX4_CMD_WRITE_MTT,
365 MLX4_CMD_TIME_CLASS_B);
368 int mlx4_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
369 int start_index, int npages, u64 *page_list)
371 struct mlx4_cmd_mailbox *mailbox;
372 __be64 *mtt_entry;
373 int i;
374 int err = 0;
376 if (mtt->order < 0)
377 return -EINVAL;
379 mailbox = mlx4_alloc_cmd_mailbox(dev);
380 if (IS_ERR(mailbox))
381 return PTR_ERR(mailbox);
383 mtt_entry = mailbox->buf;
385 while (npages > 0) {
386 mtt_entry[0] = cpu_to_be64(mlx4_mtt_addr(dev, mtt) + start_index * 8);
387 mtt_entry[1] = 0;
389 for (i = 0; i < npages && i < MLX4_MAILBOX_SIZE / 8 - 2; ++i)
390 mtt_entry[i + 2] = cpu_to_be64(page_list[i] |
391 MLX4_MTT_FLAG_PRESENT);
394 * If we have an odd number of entries to write, add
395 * one more dummy entry for firmware efficiency.
397 if (i & 1)
398 mtt_entry[i + 2] = 0;
400 err = mlx4_WRITE_MTT(dev, mailbox, (i + 1) & ~1);
401 if (err)
402 goto out;
404 npages -= i;
405 start_index += i;
406 page_list += i;
409 out:
410 mlx4_free_cmd_mailbox(dev, mailbox);
412 return err;
414 EXPORT_SYMBOL_GPL(mlx4_write_mtt);
416 int mlx4_buf_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
417 struct mlx4_buf *buf)
419 u64 *page_list;
420 int err;
421 int i;
423 page_list = kmalloc(buf->npages * sizeof *page_list, GFP_KERNEL);
424 if (!page_list)
425 return -ENOMEM;
427 for (i = 0; i < buf->npages; ++i)
428 if (buf->nbufs == 1)
429 page_list[i] = buf->u.direct.map + (i << buf->page_shift);
430 else
431 page_list[i] = buf->u.page_list[i].map;
433 err = mlx4_write_mtt(dev, mtt, 0, buf->npages, page_list);
435 kfree(page_list);
436 return err;
438 EXPORT_SYMBOL_GPL(mlx4_buf_write_mtt);
440 int __devinit mlx4_init_mr_table(struct mlx4_dev *dev)
442 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
443 int err;
445 err = mlx4_bitmap_init(&mr_table->mpt_bitmap, dev->caps.num_mpts,
446 ~0, dev->caps.reserved_mrws);
447 if (err)
448 return err;
450 err = mlx4_buddy_init(&mr_table->mtt_buddy,
451 ilog2(dev->caps.num_mtt_segs));
452 if (err)
453 goto err_buddy;
455 if (dev->caps.reserved_mtts) {
456 if (mlx4_alloc_mtt_range(dev, ilog2(dev->caps.reserved_mtts)) == -1) {
457 mlx4_warn(dev, "MTT table of order %d is too small.\n",
458 mr_table->mtt_buddy.max_order);
459 err = -ENOMEM;
460 goto err_reserve_mtts;
464 return 0;
466 err_reserve_mtts:
467 mlx4_buddy_cleanup(&mr_table->mtt_buddy);
469 err_buddy:
470 mlx4_bitmap_cleanup(&mr_table->mpt_bitmap);
472 return err;
475 void mlx4_cleanup_mr_table(struct mlx4_dev *dev)
477 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
479 mlx4_buddy_cleanup(&mr_table->mtt_buddy);
480 mlx4_bitmap_cleanup(&mr_table->mpt_bitmap);