2 * Copyright (c) 2016 Hisilicon Limited.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/platform_device.h>
35 #include <rdma/ib_umem.h>
36 #include "hns_roce_device.h"
37 #include "hns_roce_cmd.h"
38 #include "hns_roce_hem.h"
40 static u32 hw_index_to_key(unsigned long ind)
42 return (u32)(ind >> 24) | (ind << 8);
45 static unsigned long key_to_hw_index(u32 key)
47 return (key << 24) | (key >> 8);
50 static int hns_roce_sw2hw_mpt(struct hns_roce_dev *hr_dev,
51 struct hns_roce_cmd_mailbox *mailbox,
52 unsigned long mpt_index)
54 return hns_roce_cmd_mbox(hr_dev, mailbox->dma, 0, mpt_index, 0,
55 HNS_ROCE_CMD_SW2HW_MPT,
56 HNS_ROCE_CMD_TIME_CLASS_B);
59 static int hns_roce_hw2sw_mpt(struct hns_roce_dev *hr_dev,
60 struct hns_roce_cmd_mailbox *mailbox,
61 unsigned long mpt_index)
63 return hns_roce_cmd_mbox(hr_dev, 0, mailbox ? mailbox->dma : 0,
64 mpt_index, !mailbox, HNS_ROCE_CMD_HW2SW_MPT,
65 HNS_ROCE_CMD_TIME_CLASS_B);
68 static int hns_roce_buddy_alloc(struct hns_roce_buddy *buddy, int order,
74 spin_lock(&buddy->lock);
76 for (o = order; o <= buddy->max_order; ++o) {
77 if (buddy->num_free[o]) {
78 m = 1 << (buddy->max_order - o);
79 *seg = find_first_bit(buddy->bits[o], m);
84 spin_unlock(&buddy->lock);
88 clear_bit(*seg, buddy->bits[o]);
94 set_bit(*seg ^ 1, buddy->bits[o]);
98 spin_unlock(&buddy->lock);
104 static void hns_roce_buddy_free(struct hns_roce_buddy *buddy, unsigned long seg,
109 spin_lock(&buddy->lock);
111 while (test_bit(seg ^ 1, buddy->bits[order])) {
112 clear_bit(seg ^ 1, buddy->bits[order]);
113 --buddy->num_free[order];
118 set_bit(seg, buddy->bits[order]);
119 ++buddy->num_free[order];
121 spin_unlock(&buddy->lock);
124 static int hns_roce_buddy_init(struct hns_roce_buddy *buddy, int max_order)
128 buddy->max_order = max_order;
129 spin_lock_init(&buddy->lock);
131 buddy->bits = kzalloc((buddy->max_order + 1) * sizeof(long *),
133 buddy->num_free = kzalloc((buddy->max_order + 1) * sizeof(int *),
135 if (!buddy->bits || !buddy->num_free)
138 for (i = 0; i <= buddy->max_order; ++i) {
139 s = BITS_TO_LONGS(1 << (buddy->max_order - i));
140 buddy->bits[i] = kmalloc_array(s, sizeof(long), GFP_KERNEL);
144 bitmap_zero(buddy->bits[i], 1 << (buddy->max_order - i));
147 set_bit(0, buddy->bits[buddy->max_order]);
148 buddy->num_free[buddy->max_order] = 1;
153 for (i = 0; i <= buddy->max_order; ++i)
154 kfree(buddy->bits[i]);
158 kfree(buddy->num_free);
162 static void hns_roce_buddy_cleanup(struct hns_roce_buddy *buddy)
166 for (i = 0; i <= buddy->max_order; ++i)
167 kfree(buddy->bits[i]);
170 kfree(buddy->num_free);
173 static int hns_roce_alloc_mtt_range(struct hns_roce_dev *hr_dev, int order,
176 struct hns_roce_mr_table *mr_table = &hr_dev->mr_table;
179 ret = hns_roce_buddy_alloc(&mr_table->mtt_buddy, order, seg);
183 if (hns_roce_table_get_range(hr_dev, &mr_table->mtt_table, *seg,
184 *seg + (1 << order) - 1)) {
185 hns_roce_buddy_free(&mr_table->mtt_buddy, *seg, order);
192 int hns_roce_mtt_init(struct hns_roce_dev *hr_dev, int npages, int page_shift,
193 struct hns_roce_mtt *mtt)
198 /* Page num is zero, correspond to DMA memory register */
201 mtt->page_shift = HNS_ROCE_HEM_PAGE_SHIFT;
205 /* Note: if page_shift is zero, FAST memory regsiter */
206 mtt->page_shift = page_shift;
208 /* Compute MTT entry necessary */
209 for (mtt->order = 0, i = HNS_ROCE_MTT_ENTRY_PER_SEG; i < npages;
213 /* Allocate MTT entry */
214 ret = hns_roce_alloc_mtt_range(hr_dev, mtt->order, &mtt->first_seg);
221 void hns_roce_mtt_cleanup(struct hns_roce_dev *hr_dev, struct hns_roce_mtt *mtt)
223 struct hns_roce_mr_table *mr_table = &hr_dev->mr_table;
228 hns_roce_buddy_free(&mr_table->mtt_buddy, mtt->first_seg, mtt->order);
229 hns_roce_table_put_range(hr_dev, &mr_table->mtt_table, mtt->first_seg,
230 mtt->first_seg + (1 << mtt->order) - 1);
233 static int hns_roce_mr_alloc(struct hns_roce_dev *hr_dev, u32 pd, u64 iova,
234 u64 size, u32 access, int npages,
235 struct hns_roce_mr *mr)
237 unsigned long index = 0;
239 struct device *dev = &hr_dev->pdev->dev;
241 /* Allocate a key for mr from mr_table */
242 ret = hns_roce_bitmap_alloc(&hr_dev->mr_table.mtpt_bitmap, &index);
246 mr->iova = iova; /* MR va starting addr */
247 mr->size = size; /* MR addr range */
248 mr->pd = pd; /* MR num */
249 mr->access = access; /* MR access permit */
250 mr->enabled = 0; /* MR active status */
251 mr->key = hw_index_to_key(index); /* MR key */
254 mr->type = MR_TYPE_DMA;
256 mr->pbl_dma_addr = 0;
258 mr->type = MR_TYPE_MR;
259 mr->pbl_buf = dma_alloc_coherent(dev, npages * 8,
269 static void hns_roce_mr_free(struct hns_roce_dev *hr_dev,
270 struct hns_roce_mr *mr)
272 struct device *dev = &hr_dev->pdev->dev;
277 ret = hns_roce_hw2sw_mpt(hr_dev, NULL, key_to_hw_index(mr->key)
278 & (hr_dev->caps.num_mtpts - 1));
280 dev_warn(dev, "HW2SW_MPT failed (%d)\n", ret);
283 if (mr->size != ~0ULL) {
284 npages = ib_umem_page_count(mr->umem);
285 dma_free_coherent(dev, (unsigned int)(npages * 8), mr->pbl_buf,
289 hns_roce_bitmap_free(&hr_dev->mr_table.mtpt_bitmap,
290 key_to_hw_index(mr->key));
293 static int hns_roce_mr_enable(struct hns_roce_dev *hr_dev,
294 struct hns_roce_mr *mr)
297 unsigned long mtpt_idx = key_to_hw_index(mr->key);
298 struct device *dev = &hr_dev->pdev->dev;
299 struct hns_roce_cmd_mailbox *mailbox;
300 struct hns_roce_mr_table *mr_table = &hr_dev->mr_table;
302 /* Prepare HEM entry memory */
303 ret = hns_roce_table_get(hr_dev, &mr_table->mtpt_table, mtpt_idx);
307 /* Allocate mailbox memory */
308 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
309 if (IS_ERR(mailbox)) {
310 ret = PTR_ERR(mailbox);
314 ret = hr_dev->hw->write_mtpt(mailbox->buf, mr, mtpt_idx);
316 dev_err(dev, "Write mtpt fail!\n");
320 ret = hns_roce_sw2hw_mpt(hr_dev, mailbox,
321 mtpt_idx & (hr_dev->caps.num_mtpts - 1));
323 dev_err(dev, "SW2HW_MPT failed (%d)\n", ret);
328 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
333 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
336 hns_roce_table_put(hr_dev, &mr_table->mtpt_table, mtpt_idx);
340 static int hns_roce_write_mtt_chunk(struct hns_roce_dev *hr_dev,
341 struct hns_roce_mtt *mtt, u32 start_index,
342 u32 npages, u64 *page_list)
346 dma_addr_t dma_handle;
347 u32 s = start_index * sizeof(u64);
349 /* All MTTs must fit in the same page */
350 if (start_index / (PAGE_SIZE / sizeof(u64)) !=
351 (start_index + npages - 1) / (PAGE_SIZE / sizeof(u64)))
354 if (start_index & (HNS_ROCE_MTT_ENTRY_PER_SEG - 1))
357 mtts = hns_roce_table_find(&hr_dev->mr_table.mtt_table,
358 mtt->first_seg + s / hr_dev->caps.mtt_entry_sz,
363 /* Save page addr, low 12 bits : 0 */
364 for (i = 0; i < npages; ++i)
365 mtts[i] = (cpu_to_le64(page_list[i])) >> PAGE_ADDR_SHIFT;
370 static int hns_roce_write_mtt(struct hns_roce_dev *hr_dev,
371 struct hns_roce_mtt *mtt, u32 start_index,
372 u32 npages, u64 *page_list)
381 chunk = min_t(int, PAGE_SIZE / sizeof(u64), npages);
383 ret = hns_roce_write_mtt_chunk(hr_dev, mtt, start_index, chunk,
389 start_index += chunk;
396 int hns_roce_buf_write_mtt(struct hns_roce_dev *hr_dev,
397 struct hns_roce_mtt *mtt, struct hns_roce_buf *buf)
401 u64 *page_list = NULL;
403 page_list = kmalloc_array(buf->npages, sizeof(*page_list), GFP_KERNEL);
407 for (i = 0; i < buf->npages; ++i) {
409 page_list[i] = buf->direct.map + (i << buf->page_shift);
411 page_list[i] = buf->page_list[i].map;
414 ret = hns_roce_write_mtt(hr_dev, mtt, 0, buf->npages, page_list);
421 int hns_roce_init_mr_table(struct hns_roce_dev *hr_dev)
423 struct hns_roce_mr_table *mr_table = &hr_dev->mr_table;
426 ret = hns_roce_bitmap_init(&mr_table->mtpt_bitmap,
427 hr_dev->caps.num_mtpts,
428 hr_dev->caps.num_mtpts - 1,
429 hr_dev->caps.reserved_mrws, 0);
433 ret = hns_roce_buddy_init(&mr_table->mtt_buddy,
434 ilog2(hr_dev->caps.num_mtt_segs));
441 hns_roce_bitmap_cleanup(&mr_table->mtpt_bitmap);
445 void hns_roce_cleanup_mr_table(struct hns_roce_dev *hr_dev)
447 struct hns_roce_mr_table *mr_table = &hr_dev->mr_table;
449 hns_roce_buddy_cleanup(&mr_table->mtt_buddy);
450 hns_roce_bitmap_cleanup(&mr_table->mtpt_bitmap);
453 struct ib_mr *hns_roce_get_dma_mr(struct ib_pd *pd, int acc)
456 struct hns_roce_mr *mr = NULL;
458 mr = kmalloc(sizeof(*mr), GFP_KERNEL);
460 return ERR_PTR(-ENOMEM);
462 /* Allocate memory region key */
463 ret = hns_roce_mr_alloc(to_hr_dev(pd->device), to_hr_pd(pd)->pdn, 0,
468 ret = hns_roce_mr_enable(to_hr_dev(pd->device), mr);
472 mr->ibmr.rkey = mr->ibmr.lkey = mr->key;
478 hns_roce_mr_free(to_hr_dev(pd->device), mr);
485 int hns_roce_ib_umem_write_mtt(struct hns_roce_dev *hr_dev,
486 struct hns_roce_mtt *mtt, struct ib_umem *umem)
488 struct scatterlist *sg;
495 pages = (u64 *) __get_free_page(GFP_KERNEL);
501 for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
502 len = sg_dma_len(sg) >> mtt->page_shift;
503 for (k = 0; k < len; ++k) {
504 pages[i++] = sg_dma_address(sg) + umem->page_size * k;
505 if (i == PAGE_SIZE / sizeof(u64)) {
506 ret = hns_roce_write_mtt(hr_dev, mtt, n, i,
517 ret = hns_roce_write_mtt(hr_dev, mtt, n, i, pages);
520 free_page((unsigned long) pages);
524 static int hns_roce_ib_umem_write_mr(struct hns_roce_mr *mr,
525 struct ib_umem *umem)
529 struct scatterlist *sg;
531 for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
532 mr->pbl_buf[i] = ((u64)sg_dma_address(sg)) >> 12;
542 struct ib_mr *hns_roce_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
543 u64 virt_addr, int access_flags,
544 struct ib_udata *udata)
546 struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
547 struct device *dev = &hr_dev->pdev->dev;
548 struct hns_roce_mr *mr = NULL;
552 mr = kmalloc(sizeof(*mr), GFP_KERNEL);
554 return ERR_PTR(-ENOMEM);
556 mr->umem = ib_umem_get(pd->uobject->context, start, length,
558 if (IS_ERR(mr->umem)) {
559 ret = PTR_ERR(mr->umem);
563 n = ib_umem_page_count(mr->umem);
564 if (mr->umem->page_size != HNS_ROCE_HEM_PAGE_SIZE) {
565 dev_err(dev, "Just support 4K page size but is 0x%x now!\n",
566 mr->umem->page_size);
571 if (n > HNS_ROCE_MAX_MTPT_PBL_NUM) {
572 dev_err(dev, " MR len %lld err. MR is limited to 4G at most!\n",
578 ret = hns_roce_mr_alloc(hr_dev, to_hr_pd(pd)->pdn, virt_addr, length,
579 access_flags, n, mr);
583 ret = hns_roce_ib_umem_write_mr(mr, mr->umem);
587 ret = hns_roce_mr_enable(hr_dev, mr);
591 mr->ibmr.rkey = mr->ibmr.lkey = mr->key;
596 hns_roce_mr_free(hr_dev, mr);
599 ib_umem_release(mr->umem);
606 int hns_roce_dereg_mr(struct ib_mr *ibmr)
608 struct hns_roce_mr *mr = to_hr_mr(ibmr);
610 hns_roce_mr_free(to_hr_dev(ibmr->device), mr);
612 ib_umem_release(mr->umem);