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
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
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
35 #include <linux/init.h>
36 #include <linux/errno.h>
38 #include <linux/mlx4/cmd.h>
44 * Must be packed because mtt_seg is 64 bits but only aligned to 32 bits.
46 struct mlx4_mpt_entry {
60 __be32 first_byte_offset;
61 } __attribute__((packed));
63 #define MLX4_MPT_FLAG_SW_OWNS (0xfUL << 28)
64 #define MLX4_MPT_FLAG_FREE (0x3UL << 28)
65 #define MLX4_MPT_FLAG_MIO (1 << 17)
66 #define MLX4_MPT_FLAG_BIND_ENABLE (1 << 15)
67 #define MLX4_MPT_FLAG_PHYSICAL (1 << 9)
68 #define MLX4_MPT_FLAG_REGION (1 << 8)
70 #define MLX4_MPT_PD_FLAG_FAST_REG (1 << 26)
71 #define MLX4_MPT_PD_FLAG_EN_INV (3 << 24)
73 #define MLX4_MTT_FLAG_PRESENT 1
75 #define MLX4_MPT_STATUS_SW 0xF0
76 #define MLX4_MPT_STATUS_HW 0x00
78 static u32 mlx4_buddy_alloc(struct mlx4_buddy *buddy, int order)
84 spin_lock(&buddy->lock);
86 for (o = order; o <= buddy->max_order; ++o)
87 if (buddy->num_free[o]) {
88 m = 1 << (buddy->max_order - o);
89 seg = find_first_bit(buddy->bits[o], m);
94 spin_unlock(&buddy->lock);
98 clear_bit(seg, buddy->bits[o]);
104 set_bit(seg ^ 1, buddy->bits[o]);
105 ++buddy->num_free[o];
108 spin_unlock(&buddy->lock);
115 static void mlx4_buddy_free(struct mlx4_buddy *buddy, u32 seg, int order)
119 spin_lock(&buddy->lock);
121 while (test_bit(seg ^ 1, buddy->bits[order])) {
122 clear_bit(seg ^ 1, buddy->bits[order]);
123 --buddy->num_free[order];
128 set_bit(seg, buddy->bits[order]);
129 ++buddy->num_free[order];
131 spin_unlock(&buddy->lock);
134 static int mlx4_buddy_init(struct mlx4_buddy *buddy, int max_order)
138 buddy->max_order = max_order;
139 spin_lock_init(&buddy->lock);
141 buddy->bits = kzalloc((buddy->max_order + 1) * sizeof (long *),
143 buddy->num_free = kzalloc((buddy->max_order + 1) * sizeof (int *),
145 if (!buddy->bits || !buddy->num_free)
148 for (i = 0; i <= buddy->max_order; ++i) {
149 s = BITS_TO_LONGS(1 << (buddy->max_order - i));
150 buddy->bits[i] = kmalloc(s * sizeof (long), GFP_KERNEL);
153 bitmap_zero(buddy->bits[i], 1 << (buddy->max_order - i));
156 set_bit(0, buddy->bits[buddy->max_order]);
157 buddy->num_free[buddy->max_order] = 1;
162 for (i = 0; i <= buddy->max_order; ++i)
163 kfree(buddy->bits[i]);
167 kfree(buddy->num_free);
172 static void mlx4_buddy_cleanup(struct mlx4_buddy *buddy)
176 for (i = 0; i <= buddy->max_order; ++i)
177 kfree(buddy->bits[i]);
180 kfree(buddy->num_free);
183 static u32 mlx4_alloc_mtt_range(struct mlx4_dev *dev, int order)
185 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
188 seg = mlx4_buddy_alloc(&mr_table->mtt_buddy, order);
192 if (mlx4_table_get_range(dev, &mr_table->mtt_table, seg,
193 seg + (1 << order) - 1)) {
194 mlx4_buddy_free(&mr_table->mtt_buddy, seg, order);
201 int mlx4_mtt_init(struct mlx4_dev *dev, int npages, int page_shift,
202 struct mlx4_mtt *mtt)
208 mtt->page_shift = MLX4_ICM_PAGE_SHIFT;
211 mtt->page_shift = page_shift;
213 for (mtt->order = 0, i = MLX4_MTT_ENTRY_PER_SEG; i < npages; i <<= 1)
216 mtt->first_seg = mlx4_alloc_mtt_range(dev, mtt->order);
217 if (mtt->first_seg == -1)
222 EXPORT_SYMBOL_GPL(mlx4_mtt_init);
224 void mlx4_mtt_cleanup(struct mlx4_dev *dev, struct mlx4_mtt *mtt)
226 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
231 mlx4_buddy_free(&mr_table->mtt_buddy, mtt->first_seg, mtt->order);
232 mlx4_table_put_range(dev, &mr_table->mtt_table, mtt->first_seg,
233 mtt->first_seg + (1 << mtt->order) - 1);
235 EXPORT_SYMBOL_GPL(mlx4_mtt_cleanup);
237 u64 mlx4_mtt_addr(struct mlx4_dev *dev, struct mlx4_mtt *mtt)
239 return (u64) mtt->first_seg * dev->caps.mtt_entry_sz;
241 EXPORT_SYMBOL_GPL(mlx4_mtt_addr);
243 static u32 hw_index_to_key(u32 ind)
245 return (ind >> 24) | (ind << 8);
248 static u32 key_to_hw_index(u32 key)
250 return (key << 24) | (key >> 8);
253 static int mlx4_SW2HW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
256 return mlx4_cmd(dev, mailbox->dma, mpt_index, 0, MLX4_CMD_SW2HW_MPT,
257 MLX4_CMD_TIME_CLASS_B);
260 static int mlx4_HW2SW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
263 return mlx4_cmd_box(dev, 0, mailbox ? mailbox->dma : 0, mpt_index,
264 !mailbox, MLX4_CMD_HW2SW_MPT, MLX4_CMD_TIME_CLASS_B);
267 int mlx4_mr_alloc(struct mlx4_dev *dev, u32 pd, u64 iova, u64 size, u32 access,
268 int npages, int page_shift, struct mlx4_mr *mr)
270 struct mlx4_priv *priv = mlx4_priv(dev);
274 index = mlx4_bitmap_alloc(&priv->mr_table.mpt_bitmap);
283 mr->key = hw_index_to_key(index);
285 err = mlx4_mtt_init(dev, npages, page_shift, &mr->mtt);
287 mlx4_bitmap_free(&priv->mr_table.mpt_bitmap, index);
291 EXPORT_SYMBOL_GPL(mlx4_mr_alloc);
293 void mlx4_mr_free(struct mlx4_dev *dev, struct mlx4_mr *mr)
295 struct mlx4_priv *priv = mlx4_priv(dev);
299 err = mlx4_HW2SW_MPT(dev, NULL,
300 key_to_hw_index(mr->key) &
301 (dev->caps.num_mpts - 1));
303 mlx4_warn(dev, "HW2SW_MPT failed (%d)\n", err);
306 mlx4_mtt_cleanup(dev, &mr->mtt);
307 mlx4_bitmap_free(&priv->mr_table.mpt_bitmap, key_to_hw_index(mr->key));
309 EXPORT_SYMBOL_GPL(mlx4_mr_free);
311 int mlx4_mr_enable(struct mlx4_dev *dev, struct mlx4_mr *mr)
313 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
314 struct mlx4_cmd_mailbox *mailbox;
315 struct mlx4_mpt_entry *mpt_entry;
318 err = mlx4_table_get(dev, &mr_table->dmpt_table, key_to_hw_index(mr->key));
322 mailbox = mlx4_alloc_cmd_mailbox(dev);
323 if (IS_ERR(mailbox)) {
324 err = PTR_ERR(mailbox);
327 mpt_entry = mailbox->buf;
329 memset(mpt_entry, 0, sizeof *mpt_entry);
331 mpt_entry->flags = cpu_to_be32(MLX4_MPT_FLAG_MIO |
332 MLX4_MPT_FLAG_REGION |
335 mpt_entry->key = cpu_to_be32(key_to_hw_index(mr->key));
336 mpt_entry->pd_flags = cpu_to_be32(mr->pd | MLX4_MPT_PD_FLAG_EN_INV);
337 mpt_entry->start = cpu_to_be64(mr->iova);
338 mpt_entry->length = cpu_to_be64(mr->size);
339 mpt_entry->entity_size = cpu_to_be32(mr->mtt.page_shift);
341 if (mr->mtt.order < 0) {
342 mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_PHYSICAL);
343 mpt_entry->mtt_seg = 0;
345 mpt_entry->mtt_seg = cpu_to_be64(mlx4_mtt_addr(dev, &mr->mtt));
348 if (mr->mtt.order >= 0 && mr->mtt.page_shift == 0) {
349 /* fast register MR in free state */
350 mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_FREE);
351 mpt_entry->pd_flags |= cpu_to_be32(MLX4_MPT_PD_FLAG_FAST_REG);
353 mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_SW_OWNS);
356 err = mlx4_SW2HW_MPT(dev, mailbox,
357 key_to_hw_index(mr->key) & (dev->caps.num_mpts - 1));
359 mlx4_warn(dev, "SW2HW_MPT failed (%d)\n", err);
365 mlx4_free_cmd_mailbox(dev, mailbox);
370 mlx4_free_cmd_mailbox(dev, mailbox);
373 mlx4_table_put(dev, &mr_table->dmpt_table, key_to_hw_index(mr->key));
376 EXPORT_SYMBOL_GPL(mlx4_mr_enable);
378 static int mlx4_write_mtt_chunk(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
379 int start_index, int npages, u64 *page_list)
381 struct mlx4_priv *priv = mlx4_priv(dev);
383 dma_addr_t dma_handle;
385 int s = start_index * sizeof (u64);
387 /* All MTTs must fit in the same page */
388 if (start_index / (PAGE_SIZE / sizeof (u64)) !=
389 (start_index + npages - 1) / (PAGE_SIZE / sizeof (u64)))
392 if (start_index & (MLX4_MTT_ENTRY_PER_SEG - 1))
395 mtts = mlx4_table_find(&priv->mr_table.mtt_table, mtt->first_seg +
396 s / dev->caps.mtt_entry_sz, &dma_handle);
400 for (i = 0; i < npages; ++i)
401 mtts[i] = cpu_to_be64(page_list[i] | MLX4_MTT_FLAG_PRESENT);
403 dma_sync_single(&dev->pdev->dev, dma_handle, npages * sizeof (u64), DMA_TO_DEVICE);
408 int mlx4_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
409 int start_index, int npages, u64 *page_list)
418 chunk = min_t(int, PAGE_SIZE / sizeof(u64), npages);
419 err = mlx4_write_mtt_chunk(dev, mtt, start_index, chunk, page_list);
424 start_index += chunk;
430 EXPORT_SYMBOL_GPL(mlx4_write_mtt);
432 int mlx4_buf_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
433 struct mlx4_buf *buf)
439 page_list = kmalloc(buf->npages * sizeof *page_list, GFP_KERNEL);
443 for (i = 0; i < buf->npages; ++i)
445 page_list[i] = buf->direct.map + (i << buf->page_shift);
447 page_list[i] = buf->page_list[i].map;
449 err = mlx4_write_mtt(dev, mtt, 0, buf->npages, page_list);
454 EXPORT_SYMBOL_GPL(mlx4_buf_write_mtt);
456 int mlx4_init_mr_table(struct mlx4_dev *dev)
458 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
461 err = mlx4_bitmap_init(&mr_table->mpt_bitmap, dev->caps.num_mpts,
462 ~0, dev->caps.reserved_mrws);
466 err = mlx4_buddy_init(&mr_table->mtt_buddy,
467 ilog2(dev->caps.num_mtt_segs));
471 if (dev->caps.reserved_mtts) {
472 if (mlx4_alloc_mtt_range(dev, fls(dev->caps.reserved_mtts - 1)) == -1) {
473 mlx4_warn(dev, "MTT table of order %d is too small.\n",
474 mr_table->mtt_buddy.max_order);
476 goto err_reserve_mtts;
483 mlx4_buddy_cleanup(&mr_table->mtt_buddy);
486 mlx4_bitmap_cleanup(&mr_table->mpt_bitmap);
491 void mlx4_cleanup_mr_table(struct mlx4_dev *dev)
493 struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
495 mlx4_buddy_cleanup(&mr_table->mtt_buddy);
496 mlx4_bitmap_cleanup(&mr_table->mpt_bitmap);
499 static inline int mlx4_check_fmr(struct mlx4_fmr *fmr, u64 *page_list,
500 int npages, u64 iova)
504 if (npages > fmr->max_pages)
507 page_mask = (1 << fmr->page_shift) - 1;
509 /* We are getting page lists, so va must be page aligned. */
510 if (iova & page_mask)
513 /* Trust the user not to pass misaligned data in page_list */
515 for (i = 0; i < npages; ++i) {
516 if (page_list[i] & ~page_mask)
520 if (fmr->maps >= fmr->max_maps)
526 int mlx4_map_phys_fmr(struct mlx4_dev *dev, struct mlx4_fmr *fmr, u64 *page_list,
527 int npages, u64 iova, u32 *lkey, u32 *rkey)
532 err = mlx4_check_fmr(fmr, page_list, npages, iova);
538 key = key_to_hw_index(fmr->mr.key);
539 key += dev->caps.num_mpts;
540 *lkey = *rkey = fmr->mr.key = hw_index_to_key(key);
542 *(u8 *) fmr->mpt = MLX4_MPT_STATUS_SW;
544 /* Make sure MPT status is visible before writing MTT entries */
547 for (i = 0; i < npages; ++i)
548 fmr->mtts[i] = cpu_to_be64(page_list[i] | MLX4_MTT_FLAG_PRESENT);
550 dma_sync_single(&dev->pdev->dev, fmr->dma_handle,
551 npages * sizeof(u64), DMA_TO_DEVICE);
553 fmr->mpt->key = cpu_to_be32(key);
554 fmr->mpt->lkey = cpu_to_be32(key);
555 fmr->mpt->length = cpu_to_be64(npages * (1ull << fmr->page_shift));
556 fmr->mpt->start = cpu_to_be64(iova);
558 /* Make MTT entries are visible before setting MPT status */
561 *(u8 *) fmr->mpt = MLX4_MPT_STATUS_HW;
563 /* Make sure MPT status is visible before consumer can use FMR */
568 EXPORT_SYMBOL_GPL(mlx4_map_phys_fmr);
570 int mlx4_fmr_alloc(struct mlx4_dev *dev, u32 pd, u32 access, int max_pages,
571 int max_maps, u8 page_shift, struct mlx4_fmr *fmr)
573 struct mlx4_priv *priv = mlx4_priv(dev);
577 if (page_shift < (ffs(dev->caps.page_size_cap) - 1) || page_shift >= 32)
580 /* All MTTs must fit in the same page */
581 if (max_pages * sizeof *fmr->mtts > PAGE_SIZE)
584 fmr->page_shift = page_shift;
585 fmr->max_pages = max_pages;
586 fmr->max_maps = max_maps;
589 err = mlx4_mr_alloc(dev, pd, 0, 0, access, max_pages,
590 page_shift, &fmr->mr);
594 mtt_seg = fmr->mr.mtt.first_seg * dev->caps.mtt_entry_sz;
596 fmr->mtts = mlx4_table_find(&priv->mr_table.mtt_table,
597 fmr->mr.mtt.first_seg,
607 mlx4_mr_free(dev, &fmr->mr);
610 EXPORT_SYMBOL_GPL(mlx4_fmr_alloc);
612 int mlx4_fmr_enable(struct mlx4_dev *dev, struct mlx4_fmr *fmr)
614 struct mlx4_priv *priv = mlx4_priv(dev);
617 err = mlx4_mr_enable(dev, &fmr->mr);
621 fmr->mpt = mlx4_table_find(&priv->mr_table.dmpt_table,
622 key_to_hw_index(fmr->mr.key), NULL);
628 EXPORT_SYMBOL_GPL(mlx4_fmr_enable);
630 void mlx4_fmr_unmap(struct mlx4_dev *dev, struct mlx4_fmr *fmr,
631 u32 *lkey, u32 *rkey)
638 *(u8 *) fmr->mpt = MLX4_MPT_STATUS_SW;
640 EXPORT_SYMBOL_GPL(mlx4_fmr_unmap);
642 int mlx4_fmr_free(struct mlx4_dev *dev, struct mlx4_fmr *fmr)
648 mlx4_mr_free(dev, &fmr->mr);
652 EXPORT_SYMBOL_GPL(mlx4_fmr_free);
654 int mlx4_SYNC_TPT(struct mlx4_dev *dev)
656 return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_SYNC_TPT, 1000);
658 EXPORT_SYMBOL_GPL(mlx4_SYNC_TPT);