1 /*
2 * Copyright (c) 2016 Hisilicon Limited.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
4 *
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
34 #include <linux/platform_device.h>
35 #include <linux/vmalloc.h>
36 #include <rdma/ib_umem.h>
37 #include "hns_roce_device.h"
38 #include "hns_roce_cmd.h"
39 #include "hns_roce_hem.h"
40
hw_index_to_key(unsigned long ind)41 static u32 hw_index_to_key(unsigned long ind)
42 {
43 return (u32)(ind >> 24) | (ind << 8);
44 }
45
key_to_hw_index(u32 key)46 unsigned long key_to_hw_index(u32 key)
47 {
48 return (key << 24) | (key >> 8);
49 }
50
hns_roce_hw_create_mpt(struct hns_roce_dev * hr_dev,struct hns_roce_cmd_mailbox * mailbox,unsigned long mpt_index)51 static int hns_roce_hw_create_mpt(struct hns_roce_dev *hr_dev,
52 struct hns_roce_cmd_mailbox *mailbox,
53 unsigned long mpt_index)
54 {
55 return hns_roce_cmd_mbox(hr_dev, mailbox->dma, 0, mpt_index, 0,
56 HNS_ROCE_CMD_CREATE_MPT,
57 HNS_ROCE_CMD_TIMEOUT_MSECS);
58 }
59
hns_roce_hw_destroy_mpt(struct hns_roce_dev * hr_dev,struct hns_roce_cmd_mailbox * mailbox,unsigned long mpt_index)60 int hns_roce_hw_destroy_mpt(struct hns_roce_dev *hr_dev,
61 struct hns_roce_cmd_mailbox *mailbox,
62 unsigned long mpt_index)
63 {
64 return hns_roce_cmd_mbox(hr_dev, 0, mailbox ? mailbox->dma : 0,
65 mpt_index, !mailbox, HNS_ROCE_CMD_DESTROY_MPT,
66 HNS_ROCE_CMD_TIMEOUT_MSECS);
67 }
68
alloc_mr_key(struct hns_roce_dev * hr_dev,struct hns_roce_mr * mr,u32 pd,u64 iova,u64 size,u32 access)69 static int alloc_mr_key(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr,
70 u32 pd, u64 iova, u64 size, u32 access)
71 {
72 struct ib_device *ibdev = &hr_dev->ib_dev;
73 unsigned long obj = 0;
74 int err;
75
76 /* Allocate a key for mr from mr_table */
77 err = hns_roce_bitmap_alloc(&hr_dev->mr_table.mtpt_bitmap, &obj);
78 if (err) {
79 ibdev_err(ibdev,
80 "failed to alloc bitmap for MR key, ret = %d.\n",
81 err);
82 return -ENOMEM;
83 }
84
85 mr->iova = iova; /* MR va starting addr */
86 mr->size = size; /* MR addr range */
87 mr->pd = pd; /* MR num */
88 mr->access = access; /* MR access permit */
89 mr->enabled = 0; /* MR active status */
90 mr->key = hw_index_to_key(obj); /* MR key */
91
92 err = hns_roce_table_get(hr_dev, &hr_dev->mr_table.mtpt_table, obj);
93 if (err) {
94 ibdev_err(ibdev, "failed to alloc mtpt, ret = %d.\n", err);
95 goto err_free_bitmap;
96 }
97
98 return 0;
99 err_free_bitmap:
100 hns_roce_bitmap_free(&hr_dev->mr_table.mtpt_bitmap, obj, BITMAP_NO_RR);
101 return err;
102 }
103
free_mr_key(struct hns_roce_dev * hr_dev,struct hns_roce_mr * mr)104 static void free_mr_key(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr)
105 {
106 unsigned long obj = key_to_hw_index(mr->key);
107
108 hns_roce_table_put(hr_dev, &hr_dev->mr_table.mtpt_table, obj);
109 hns_roce_bitmap_free(&hr_dev->mr_table.mtpt_bitmap, obj, BITMAP_NO_RR);
110 }
111
alloc_mr_pbl(struct hns_roce_dev * hr_dev,struct hns_roce_mr * mr,size_t length,struct ib_udata * udata,u64 start,int access)112 static int alloc_mr_pbl(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr,
113 size_t length, struct ib_udata *udata, u64 start,
114 int access)
115 {
116 struct ib_device *ibdev = &hr_dev->ib_dev;
117 bool is_fast = mr->type == MR_TYPE_FRMR;
118 struct hns_roce_buf_attr buf_attr = {};
119 int err;
120
121 mr->pbl_hop_num = is_fast ? 1 : hr_dev->caps.pbl_hop_num;
122 buf_attr.page_shift = is_fast ? PAGE_SHIFT :
123 hr_dev->caps.pbl_buf_pg_sz + PAGE_SHIFT;
124 buf_attr.region[0].size = length;
125 buf_attr.region[0].hopnum = mr->pbl_hop_num;
126 buf_attr.region_count = 1;
127 buf_attr.fixed_page = true;
128 buf_attr.user_access = access;
129 /* fast MR's buffer is alloced before mapping, not at creation */
130 buf_attr.mtt_only = is_fast;
131
132 err = hns_roce_mtr_create(hr_dev, &mr->pbl_mtr, &buf_attr,
133 hr_dev->caps.pbl_ba_pg_sz + HNS_HW_PAGE_SHIFT,
134 udata, start);
135 if (err)
136 ibdev_err(ibdev, "failed to alloc pbl mtr, ret = %d.\n", err);
137 else
138 mr->npages = mr->pbl_mtr.hem_cfg.buf_pg_count;
139
140 return err;
141 }
142
free_mr_pbl(struct hns_roce_dev * hr_dev,struct hns_roce_mr * mr)143 static void free_mr_pbl(struct hns_roce_dev *hr_dev, struct hns_roce_mr *mr)
144 {
145 hns_roce_mtr_destroy(hr_dev, &mr->pbl_mtr);
146 }
147
hns_roce_mr_free(struct hns_roce_dev * hr_dev,struct hns_roce_mr * mr)148 static void hns_roce_mr_free(struct hns_roce_dev *hr_dev,
149 struct hns_roce_mr *mr)
150 {
151 struct ib_device *ibdev = &hr_dev->ib_dev;
152 int ret;
153
154 if (mr->enabled) {
155 ret = hns_roce_hw_destroy_mpt(hr_dev, NULL,
156 key_to_hw_index(mr->key) &
157 (hr_dev->caps.num_mtpts - 1));
158 if (ret)
159 ibdev_warn(ibdev, "failed to destroy mpt, ret = %d.\n",
160 ret);
161 }
162
163 free_mr_pbl(hr_dev, mr);
164 free_mr_key(hr_dev, mr);
165 }
166
hns_roce_mr_enable(struct hns_roce_dev * hr_dev,struct hns_roce_mr * mr)167 static int hns_roce_mr_enable(struct hns_roce_dev *hr_dev,
168 struct hns_roce_mr *mr)
169 {
170 int ret;
171 unsigned long mtpt_idx = key_to_hw_index(mr->key);
172 struct device *dev = hr_dev->dev;
173 struct hns_roce_cmd_mailbox *mailbox;
174
175 /* Allocate mailbox memory */
176 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
177 if (IS_ERR(mailbox)) {
178 ret = PTR_ERR(mailbox);
179 return ret;
180 }
181
182 if (mr->type != MR_TYPE_FRMR)
183 ret = hr_dev->hw->write_mtpt(hr_dev, mailbox->buf, mr,
184 mtpt_idx);
185 else
186 ret = hr_dev->hw->frmr_write_mtpt(hr_dev, mailbox->buf, mr);
187 if (ret) {
188 dev_err(dev, "failed to write mtpt, ret = %d.\n", ret);
189 goto err_page;
190 }
191
192 ret = hns_roce_hw_create_mpt(hr_dev, mailbox,
193 mtpt_idx & (hr_dev->caps.num_mtpts - 1));
194 if (ret) {
195 dev_err(dev, "failed to create mpt, ret = %d.\n", ret);
196 goto err_page;
197 }
198
199 mr->enabled = 1;
200 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
201
202 return 0;
203
204 err_page:
205 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
206
207 return ret;
208 }
209
hns_roce_init_mr_table(struct hns_roce_dev * hr_dev)210 int hns_roce_init_mr_table(struct hns_roce_dev *hr_dev)
211 {
212 struct hns_roce_mr_table *mr_table = &hr_dev->mr_table;
213 int ret;
214
215 ret = hns_roce_bitmap_init(&mr_table->mtpt_bitmap,
216 hr_dev->caps.num_mtpts,
217 hr_dev->caps.num_mtpts - 1,
218 hr_dev->caps.reserved_mrws, 0);
219 return ret;
220 }
221
hns_roce_cleanup_mr_table(struct hns_roce_dev * hr_dev)222 void hns_roce_cleanup_mr_table(struct hns_roce_dev *hr_dev)
223 {
224 struct hns_roce_mr_table *mr_table = &hr_dev->mr_table;
225
226 hns_roce_bitmap_cleanup(&mr_table->mtpt_bitmap);
227 }
228
hns_roce_get_dma_mr(struct ib_pd * pd,int acc)229 struct ib_mr *hns_roce_get_dma_mr(struct ib_pd *pd, int acc)
230 {
231 struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
232 struct hns_roce_mr *mr;
233 int ret;
234
235 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
236 if (mr == NULL)
237 return ERR_PTR(-ENOMEM);
238
239 mr->type = MR_TYPE_DMA;
240
241 /* Allocate memory region key */
242 hns_roce_hem_list_init(&mr->pbl_mtr.hem_list);
243 ret = alloc_mr_key(hr_dev, mr, to_hr_pd(pd)->pdn, 0, 0, acc);
244 if (ret)
245 goto err_free;
246
247 ret = hns_roce_mr_enable(to_hr_dev(pd->device), mr);
248 if (ret)
249 goto err_mr;
250
251 mr->ibmr.rkey = mr->ibmr.lkey = mr->key;
252
253 return &mr->ibmr;
254 err_mr:
255 free_mr_key(hr_dev, mr);
256
257 err_free:
258 kfree(mr);
259 return ERR_PTR(ret);
260 }
261
hns_roce_reg_user_mr(struct ib_pd * pd,u64 start,u64 length,u64 virt_addr,int access_flags,struct ib_udata * udata)262 struct ib_mr *hns_roce_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
263 u64 virt_addr, int access_flags,
264 struct ib_udata *udata)
265 {
266 struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
267 struct hns_roce_mr *mr;
268 int ret;
269
270 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
271 if (!mr)
272 return ERR_PTR(-ENOMEM);
273
274 mr->type = MR_TYPE_MR;
275 ret = alloc_mr_key(hr_dev, mr, to_hr_pd(pd)->pdn, virt_addr, length,
276 access_flags);
277 if (ret)
278 goto err_alloc_mr;
279
280 ret = alloc_mr_pbl(hr_dev, mr, length, udata, start, access_flags);
281 if (ret)
282 goto err_alloc_key;
283
284 ret = hns_roce_mr_enable(hr_dev, mr);
285 if (ret)
286 goto err_alloc_pbl;
287
288 mr->ibmr.rkey = mr->ibmr.lkey = mr->key;
289
290 return &mr->ibmr;
291
292 err_alloc_pbl:
293 free_mr_pbl(hr_dev, mr);
294 err_alloc_key:
295 free_mr_key(hr_dev, mr);
296 err_alloc_mr:
297 kfree(mr);
298 return ERR_PTR(ret);
299 }
300
rereg_mr_trans(struct ib_mr * ibmr,int flags,u64 start,u64 length,u64 virt_addr,int mr_access_flags,struct hns_roce_cmd_mailbox * mailbox,u32 pdn,struct ib_udata * udata)301 static int rereg_mr_trans(struct ib_mr *ibmr, int flags,
302 u64 start, u64 length,
303 u64 virt_addr, int mr_access_flags,
304 struct hns_roce_cmd_mailbox *mailbox,
305 u32 pdn, struct ib_udata *udata)
306 {
307 struct hns_roce_dev *hr_dev = to_hr_dev(ibmr->device);
308 struct ib_device *ibdev = &hr_dev->ib_dev;
309 struct hns_roce_mr *mr = to_hr_mr(ibmr);
310 int ret;
311
312 free_mr_pbl(hr_dev, mr);
313 ret = alloc_mr_pbl(hr_dev, mr, length, udata, start, mr_access_flags);
314 if (ret) {
315 ibdev_err(ibdev, "failed to create mr PBL, ret = %d.\n", ret);
316 return ret;
317 }
318
319 ret = hr_dev->hw->rereg_write_mtpt(hr_dev, mr, flags, pdn,
320 mr_access_flags, virt_addr,
321 length, mailbox->buf);
322 if (ret) {
323 ibdev_err(ibdev, "failed to write mtpt, ret = %d.\n", ret);
324 free_mr_pbl(hr_dev, mr);
325 }
326
327 return ret;
328 }
329
hns_roce_rereg_user_mr(struct ib_mr * ibmr,int flags,u64 start,u64 length,u64 virt_addr,int mr_access_flags,struct ib_pd * pd,struct ib_udata * udata)330 int hns_roce_rereg_user_mr(struct ib_mr *ibmr, int flags, u64 start, u64 length,
331 u64 virt_addr, int mr_access_flags, struct ib_pd *pd,
332 struct ib_udata *udata)
333 {
334 struct hns_roce_dev *hr_dev = to_hr_dev(ibmr->device);
335 struct ib_device *ib_dev = &hr_dev->ib_dev;
336 struct hns_roce_mr *mr = to_hr_mr(ibmr);
337 struct hns_roce_cmd_mailbox *mailbox;
338 unsigned long mtpt_idx;
339 u32 pdn = 0;
340 int ret;
341
342 if (!mr->enabled)
343 return -EINVAL;
344
345 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
346 if (IS_ERR(mailbox))
347 return PTR_ERR(mailbox);
348
349 mtpt_idx = key_to_hw_index(mr->key) & (hr_dev->caps.num_mtpts - 1);
350 ret = hns_roce_cmd_mbox(hr_dev, 0, mailbox->dma, mtpt_idx, 0,
351 HNS_ROCE_CMD_QUERY_MPT,
352 HNS_ROCE_CMD_TIMEOUT_MSECS);
353 if (ret)
354 goto free_cmd_mbox;
355
356 ret = hns_roce_hw_destroy_mpt(hr_dev, NULL, mtpt_idx);
357 if (ret)
358 ibdev_warn(ib_dev, "failed to destroy MPT, ret = %d.\n", ret);
359
360 mr->enabled = 0;
361
362 if (flags & IB_MR_REREG_PD)
363 pdn = to_hr_pd(pd)->pdn;
364
365 if (flags & IB_MR_REREG_TRANS) {
366 ret = rereg_mr_trans(ibmr, flags,
367 start, length,
368 virt_addr, mr_access_flags,
369 mailbox, pdn, udata);
370 if (ret)
371 goto free_cmd_mbox;
372 } else {
373 ret = hr_dev->hw->rereg_write_mtpt(hr_dev, mr, flags, pdn,
374 mr_access_flags, virt_addr,
375 length, mailbox->buf);
376 if (ret)
377 goto free_cmd_mbox;
378 }
379
380 ret = hns_roce_hw_create_mpt(hr_dev, mailbox, mtpt_idx);
381 if (ret) {
382 ibdev_err(ib_dev, "failed to create MPT, ret = %d.\n", ret);
383 goto free_cmd_mbox;
384 }
385
386 mr->enabled = 1;
387 if (flags & IB_MR_REREG_ACCESS)
388 mr->access = mr_access_flags;
389
390 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
391
392 return 0;
393
394 free_cmd_mbox:
395 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
396
397 return ret;
398 }
399
hns_roce_dereg_mr(struct ib_mr * ibmr,struct ib_udata * udata)400 int hns_roce_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata)
401 {
402 struct hns_roce_dev *hr_dev = to_hr_dev(ibmr->device);
403 struct hns_roce_mr *mr = to_hr_mr(ibmr);
404 int ret = 0;
405
406 if (hr_dev->hw->dereg_mr) {
407 ret = hr_dev->hw->dereg_mr(hr_dev, mr, udata);
408 } else {
409 hns_roce_mr_free(hr_dev, mr);
410 kfree(mr);
411 }
412
413 return ret;
414 }
415
hns_roce_alloc_mr(struct ib_pd * pd,enum ib_mr_type mr_type,u32 max_num_sg)416 struct ib_mr *hns_roce_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
417 u32 max_num_sg)
418 {
419 struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);
420 struct device *dev = hr_dev->dev;
421 struct hns_roce_mr *mr;
422 u64 length;
423 int ret;
424
425 if (mr_type != IB_MR_TYPE_MEM_REG)
426 return ERR_PTR(-EINVAL);
427
428 if (max_num_sg > HNS_ROCE_FRMR_MAX_PA) {
429 dev_err(dev, "max_num_sg larger than %d\n",
430 HNS_ROCE_FRMR_MAX_PA);
431 return ERR_PTR(-EINVAL);
432 }
433
434 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
435 if (!mr)
436 return ERR_PTR(-ENOMEM);
437
438 mr->type = MR_TYPE_FRMR;
439
440 /* Allocate memory region key */
441 length = max_num_sg * (1 << PAGE_SHIFT);
442 ret = alloc_mr_key(hr_dev, mr, to_hr_pd(pd)->pdn, 0, length, 0);
443 if (ret)
444 goto err_free;
445
446 ret = alloc_mr_pbl(hr_dev, mr, length, NULL, 0, 0);
447 if (ret)
448 goto err_key;
449
450 ret = hns_roce_mr_enable(hr_dev, mr);
451 if (ret)
452 goto err_pbl;
453
454 mr->ibmr.rkey = mr->ibmr.lkey = mr->key;
455 mr->ibmr.length = length;
456
457 return &mr->ibmr;
458
459 err_pbl:
460 free_mr_pbl(hr_dev, mr);
461 err_key:
462 free_mr_key(hr_dev, mr);
463 err_free:
464 kfree(mr);
465 return ERR_PTR(ret);
466 }
467
hns_roce_set_page(struct ib_mr * ibmr,u64 addr)468 static int hns_roce_set_page(struct ib_mr *ibmr, u64 addr)
469 {
470 struct hns_roce_mr *mr = to_hr_mr(ibmr);
471
472 if (likely(mr->npages < mr->pbl_mtr.hem_cfg.buf_pg_count)) {
473 mr->page_list[mr->npages++] = addr;
474 return 0;
475 }
476
477 return -ENOBUFS;
478 }
479
hns_roce_map_mr_sg(struct ib_mr * ibmr,struct scatterlist * sg,int sg_nents,unsigned int * sg_offset)480 int hns_roce_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
481 unsigned int *sg_offset)
482 {
483 struct hns_roce_dev *hr_dev = to_hr_dev(ibmr->device);
484 struct ib_device *ibdev = &hr_dev->ib_dev;
485 struct hns_roce_mr *mr = to_hr_mr(ibmr);
486 struct hns_roce_mtr *mtr = &mr->pbl_mtr;
487 int ret = 0;
488
489 mr->npages = 0;
490 mr->page_list = kvcalloc(mr->pbl_mtr.hem_cfg.buf_pg_count,
491 sizeof(dma_addr_t), GFP_KERNEL);
492 if (!mr->page_list)
493 return ret;
494
495 ret = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, hns_roce_set_page);
496 if (ret < 1) {
497 ibdev_err(ibdev, "failed to store sg pages %u %u, cnt = %d.\n",
498 mr->npages, mr->pbl_mtr.hem_cfg.buf_pg_count, ret);
499 goto err_page_list;
500 }
501
502 mtr->hem_cfg.region[0].offset = 0;
503 mtr->hem_cfg.region[0].count = mr->npages;
504 mtr->hem_cfg.region[0].hopnum = mr->pbl_hop_num;
505 mtr->hem_cfg.region_count = 1;
506 ret = hns_roce_mtr_map(hr_dev, mtr, mr->page_list, mr->npages);
507 if (ret) {
508 ibdev_err(ibdev, "failed to map sg mtr, ret = %d.\n", ret);
509 ret = 0;
510 } else {
511 mr->pbl_mtr.hem_cfg.buf_pg_shift = ilog2(ibmr->page_size);
512 ret = mr->npages;
513 }
514
515 err_page_list:
516 kvfree(mr->page_list);
517 mr->page_list = NULL;
518
519 return ret;
520 }
521
hns_roce_mw_free(struct hns_roce_dev * hr_dev,struct hns_roce_mw * mw)522 static void hns_roce_mw_free(struct hns_roce_dev *hr_dev,
523 struct hns_roce_mw *mw)
524 {
525 struct device *dev = hr_dev->dev;
526 int ret;
527
528 if (mw->enabled) {
529 ret = hns_roce_hw_destroy_mpt(hr_dev, NULL,
530 key_to_hw_index(mw->rkey) &
531 (hr_dev->caps.num_mtpts - 1));
532 if (ret)
533 dev_warn(dev, "MW DESTROY_MPT failed (%d)\n", ret);
534
535 hns_roce_table_put(hr_dev, &hr_dev->mr_table.mtpt_table,
536 key_to_hw_index(mw->rkey));
537 }
538
539 hns_roce_bitmap_free(&hr_dev->mr_table.mtpt_bitmap,
540 key_to_hw_index(mw->rkey), BITMAP_NO_RR);
541 }
542
hns_roce_mw_enable(struct hns_roce_dev * hr_dev,struct hns_roce_mw * mw)543 static int hns_roce_mw_enable(struct hns_roce_dev *hr_dev,
544 struct hns_roce_mw *mw)
545 {
546 struct hns_roce_mr_table *mr_table = &hr_dev->mr_table;
547 struct hns_roce_cmd_mailbox *mailbox;
548 struct device *dev = hr_dev->dev;
549 unsigned long mtpt_idx = key_to_hw_index(mw->rkey);
550 int ret;
551
552 /* prepare HEM entry memory */
553 ret = hns_roce_table_get(hr_dev, &mr_table->mtpt_table, mtpt_idx);
554 if (ret)
555 return ret;
556
557 mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
558 if (IS_ERR(mailbox)) {
559 ret = PTR_ERR(mailbox);
560 goto err_table;
561 }
562
563 ret = hr_dev->hw->mw_write_mtpt(mailbox->buf, mw);
564 if (ret) {
565 dev_err(dev, "MW write mtpt fail!\n");
566 goto err_page;
567 }
568
569 ret = hns_roce_hw_create_mpt(hr_dev, mailbox,
570 mtpt_idx & (hr_dev->caps.num_mtpts - 1));
571 if (ret) {
572 dev_err(dev, "MW CREATE_MPT failed (%d)\n", ret);
573 goto err_page;
574 }
575
576 mw->enabled = 1;
577
578 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
579
580 return 0;
581
582 err_page:
583 hns_roce_free_cmd_mailbox(hr_dev, mailbox);
584
585 err_table:
586 hns_roce_table_put(hr_dev, &mr_table->mtpt_table, mtpt_idx);
587
588 return ret;
589 }
590
hns_roce_alloc_mw(struct ib_mw * ibmw,struct ib_udata * udata)591 int hns_roce_alloc_mw(struct ib_mw *ibmw, struct ib_udata *udata)
592 {
593 struct hns_roce_dev *hr_dev = to_hr_dev(ibmw->device);
594 struct hns_roce_mw *mw = to_hr_mw(ibmw);
595 unsigned long index = 0;
596 int ret;
597
598 /* Allocate a key for mw from bitmap */
599 ret = hns_roce_bitmap_alloc(&hr_dev->mr_table.mtpt_bitmap, &index);
600 if (ret)
601 return ret;
602
603 mw->rkey = hw_index_to_key(index);
604
605 ibmw->rkey = mw->rkey;
606 mw->pdn = to_hr_pd(ibmw->pd)->pdn;
607 mw->pbl_hop_num = hr_dev->caps.pbl_hop_num;
608 mw->pbl_ba_pg_sz = hr_dev->caps.pbl_ba_pg_sz;
609 mw->pbl_buf_pg_sz = hr_dev->caps.pbl_buf_pg_sz;
610
611 ret = hns_roce_mw_enable(hr_dev, mw);
612 if (ret)
613 goto err_mw;
614
615 return 0;
616
617 err_mw:
618 hns_roce_mw_free(hr_dev, mw);
619 return ret;
620 }
621
hns_roce_dealloc_mw(struct ib_mw * ibmw)622 int hns_roce_dealloc_mw(struct ib_mw *ibmw)
623 {
624 struct hns_roce_dev *hr_dev = to_hr_dev(ibmw->device);
625 struct hns_roce_mw *mw = to_hr_mw(ibmw);
626
627 hns_roce_mw_free(hr_dev, mw);
628 return 0;
629 }
630
mtr_map_region(struct hns_roce_dev * hr_dev,struct hns_roce_mtr * mtr,dma_addr_t * pages,struct hns_roce_buf_region * region)631 static int mtr_map_region(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
632 dma_addr_t *pages, struct hns_roce_buf_region *region)
633 {
634 __le64 *mtts;
635 int offset;
636 int count;
637 int npage;
638 u64 addr;
639 int end;
640 int i;
641
642 /* if hopnum is 0, buffer cannot store BAs, so skip write mtt */
643 if (!region->hopnum)
644 return 0;
645
646 offset = region->offset;
647 end = offset + region->count;
648 npage = 0;
649 while (offset < end) {
650 mtts = hns_roce_hem_list_find_mtt(hr_dev, &mtr->hem_list,
651 offset, &count, NULL);
652 if (!mtts)
653 return -ENOBUFS;
654
655 for (i = 0; i < count; i++) {
656 if (hr_dev->hw_rev == HNS_ROCE_HW_VER1)
657 addr = to_hr_hw_page_addr(pages[npage]);
658 else
659 addr = pages[npage];
660
661 mtts[i] = cpu_to_le64(addr);
662 npage++;
663 }
664 offset += count;
665 }
666
667 return 0;
668 }
669
mtr_has_mtt(struct hns_roce_buf_attr * attr)670 static inline bool mtr_has_mtt(struct hns_roce_buf_attr *attr)
671 {
672 int i;
673
674 for (i = 0; i < attr->region_count; i++)
675 if (attr->region[i].hopnum != HNS_ROCE_HOP_NUM_0 &&
676 attr->region[i].hopnum > 0)
677 return true;
678
679 /* because the mtr only one root base address, when hopnum is 0 means
680 * root base address equals the first buffer address, thus all alloced
681 * memory must in a continuous space accessed by direct mode.
682 */
683 return false;
684 }
685
mtr_bufs_size(struct hns_roce_buf_attr * attr)686 static inline size_t mtr_bufs_size(struct hns_roce_buf_attr *attr)
687 {
688 size_t size = 0;
689 int i;
690
691 for (i = 0; i < attr->region_count; i++)
692 size += attr->region[i].size;
693
694 return size;
695 }
696
mtr_kmem_direct_size(bool is_direct,size_t alloc_size,unsigned int page_shift)697 static inline size_t mtr_kmem_direct_size(bool is_direct, size_t alloc_size,
698 unsigned int page_shift)
699 {
700 if (is_direct)
701 return ALIGN(alloc_size, 1 << page_shift);
702 else
703 return HNS_HW_DIRECT_PAGE_COUNT << page_shift;
704 }
705
706 /*
707 * check the given pages in continuous address space
708 * Returns 0 on success, or the error page num.
709 */
mtr_check_direct_pages(dma_addr_t * pages,int page_count,unsigned int page_shift)710 static inline int mtr_check_direct_pages(dma_addr_t *pages, int page_count,
711 unsigned int page_shift)
712 {
713 size_t page_size = 1 << page_shift;
714 int i;
715
716 for (i = 1; i < page_count; i++)
717 if (pages[i] - pages[i - 1] != page_size)
718 return i;
719
720 return 0;
721 }
722
mtr_free_bufs(struct hns_roce_dev * hr_dev,struct hns_roce_mtr * mtr)723 static void mtr_free_bufs(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr)
724 {
725 /* release user buffers */
726 if (mtr->umem) {
727 ib_umem_release(mtr->umem);
728 mtr->umem = NULL;
729 }
730
731 /* release kernel buffers */
732 if (mtr->kmem) {
733 hns_roce_buf_free(hr_dev, mtr->kmem);
734 kfree(mtr->kmem);
735 mtr->kmem = NULL;
736 }
737 }
738
mtr_alloc_bufs(struct hns_roce_dev * hr_dev,struct hns_roce_mtr * mtr,struct hns_roce_buf_attr * buf_attr,bool is_direct,struct ib_udata * udata,unsigned long user_addr)739 static int mtr_alloc_bufs(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
740 struct hns_roce_buf_attr *buf_attr, bool is_direct,
741 struct ib_udata *udata, unsigned long user_addr)
742 {
743 struct ib_device *ibdev = &hr_dev->ib_dev;
744 unsigned int best_pg_shift;
745 int all_pg_count = 0;
746 size_t direct_size;
747 size_t total_size;
748 int ret;
749
750 total_size = mtr_bufs_size(buf_attr);
751 if (total_size < 1) {
752 ibdev_err(ibdev, "Failed to check mtr size\n");
753 return -EINVAL;
754 }
755
756 if (udata) {
757 unsigned long pgsz_bitmap;
758 unsigned long page_size;
759
760 mtr->kmem = NULL;
761 mtr->umem = ib_umem_get(ibdev, user_addr, total_size,
762 buf_attr->user_access);
763 if (IS_ERR_OR_NULL(mtr->umem)) {
764 ibdev_err(ibdev, "Failed to get umem, ret %ld\n",
765 PTR_ERR(mtr->umem));
766 return -ENOMEM;
767 }
768 if (buf_attr->fixed_page)
769 pgsz_bitmap = 1 << buf_attr->page_shift;
770 else
771 pgsz_bitmap = GENMASK(buf_attr->page_shift, PAGE_SHIFT);
772
773 page_size = ib_umem_find_best_pgsz(mtr->umem, pgsz_bitmap,
774 user_addr);
775 if (!page_size)
776 return -EINVAL;
777 best_pg_shift = order_base_2(page_size);
778 all_pg_count = ib_umem_num_dma_blocks(mtr->umem, page_size);
779 ret = 0;
780 } else {
781 mtr->umem = NULL;
782 mtr->kmem = kzalloc(sizeof(*mtr->kmem), GFP_KERNEL);
783 if (!mtr->kmem) {
784 ibdev_err(ibdev, "Failed to alloc kmem\n");
785 return -ENOMEM;
786 }
787 direct_size = mtr_kmem_direct_size(is_direct, total_size,
788 buf_attr->page_shift);
789 ret = hns_roce_buf_alloc(hr_dev, total_size, direct_size,
790 mtr->kmem, buf_attr->page_shift);
791 if (ret) {
792 ibdev_err(ibdev, "Failed to alloc kmem, ret %d\n", ret);
793 goto err_alloc_mem;
794 }
795 best_pg_shift = buf_attr->page_shift;
796 all_pg_count = mtr->kmem->npages;
797 }
798
799 /* must bigger than minimum hardware page shift */
800 if (best_pg_shift < HNS_HW_PAGE_SHIFT || all_pg_count < 1) {
801 ret = -EINVAL;
802 ibdev_err(ibdev, "Failed to check mtr page shift %d count %d\n",
803 best_pg_shift, all_pg_count);
804 goto err_alloc_mem;
805 }
806
807 mtr->hem_cfg.buf_pg_shift = best_pg_shift;
808 mtr->hem_cfg.buf_pg_count = all_pg_count;
809
810 return 0;
811 err_alloc_mem:
812 mtr_free_bufs(hr_dev, mtr);
813 return ret;
814 }
815
mtr_get_pages(struct hns_roce_dev * hr_dev,struct hns_roce_mtr * mtr,dma_addr_t * pages,int count,unsigned int page_shift)816 static int mtr_get_pages(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
817 dma_addr_t *pages, int count, unsigned int page_shift)
818 {
819 struct ib_device *ibdev = &hr_dev->ib_dev;
820 int npage;
821 int err;
822
823 if (mtr->umem)
824 npage = hns_roce_get_umem_bufs(hr_dev, pages, count, 0,
825 mtr->umem, page_shift);
826 else
827 npage = hns_roce_get_kmem_bufs(hr_dev, pages, count, 0,
828 mtr->kmem);
829
830 if (mtr->hem_cfg.is_direct && npage > 1) {
831 err = mtr_check_direct_pages(pages, npage, page_shift);
832 if (err) {
833 ibdev_err(ibdev, "Failed to check %s direct page-%d\n",
834 mtr->umem ? "user" : "kernel", err);
835 npage = err;
836 }
837 }
838
839 return npage;
840 }
841
hns_roce_mtr_map(struct hns_roce_dev * hr_dev,struct hns_roce_mtr * mtr,dma_addr_t * pages,int page_cnt)842 int hns_roce_mtr_map(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
843 dma_addr_t *pages, int page_cnt)
844 {
845 struct ib_device *ibdev = &hr_dev->ib_dev;
846 struct hns_roce_buf_region *r;
847 int err;
848 int i;
849
850 /*
851 * Only use the first page address as root ba when hopnum is 0, this
852 * is because the addresses of all pages are consecutive in this case.
853 */
854 if (mtr->hem_cfg.is_direct) {
855 mtr->hem_cfg.root_ba = pages[0];
856 return 0;
857 }
858
859 for (i = 0; i < mtr->hem_cfg.region_count; i++) {
860 r = &mtr->hem_cfg.region[i];
861 if (r->offset + r->count > page_cnt) {
862 err = -EINVAL;
863 ibdev_err(ibdev,
864 "failed to check mtr%u end %u + %u, max %u.\n",
865 i, r->offset, r->count, page_cnt);
866 return err;
867 }
868
869 err = mtr_map_region(hr_dev, mtr, &pages[r->offset], r);
870 if (err) {
871 ibdev_err(ibdev,
872 "failed to map mtr%u offset %u, ret = %d.\n",
873 i, r->offset, err);
874 return err;
875 }
876 }
877
878 return 0;
879 }
880
hns_roce_mtr_find(struct hns_roce_dev * hr_dev,struct hns_roce_mtr * mtr,int offset,u64 * mtt_buf,int mtt_max,u64 * base_addr)881 int hns_roce_mtr_find(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
882 int offset, u64 *mtt_buf, int mtt_max, u64 *base_addr)
883 {
884 struct hns_roce_hem_cfg *cfg = &mtr->hem_cfg;
885 int start_index;
886 int mtt_count;
887 int total = 0;
888 __le64 *mtts;
889 int npage;
890 u64 addr;
891 int left;
892
893 if (!mtt_buf || mtt_max < 1)
894 goto done;
895
896 /* no mtt memory in direct mode, so just return the buffer address */
897 if (cfg->is_direct) {
898 start_index = offset >> HNS_HW_PAGE_SHIFT;
899 for (mtt_count = 0; mtt_count < cfg->region_count &&
900 total < mtt_max; mtt_count++) {
901 npage = cfg->region[mtt_count].offset;
902 if (npage < start_index)
903 continue;
904
905 addr = cfg->root_ba + (npage << HNS_HW_PAGE_SHIFT);
906 if (hr_dev->hw_rev == HNS_ROCE_HW_VER1)
907 mtt_buf[total] = to_hr_hw_page_addr(addr);
908 else
909 mtt_buf[total] = addr;
910
911 total++;
912 }
913
914 goto done;
915 }
916
917 start_index = offset >> cfg->buf_pg_shift;
918 left = mtt_max;
919 while (left > 0) {
920 mtt_count = 0;
921 mtts = hns_roce_hem_list_find_mtt(hr_dev, &mtr->hem_list,
922 start_index + total,
923 &mtt_count, NULL);
924 if (!mtts || !mtt_count)
925 goto done;
926
927 npage = min(mtt_count, left);
928 left -= npage;
929 for (mtt_count = 0; mtt_count < npage; mtt_count++)
930 mtt_buf[total++] = le64_to_cpu(mtts[mtt_count]);
931 }
932
933 done:
934 if (base_addr)
935 *base_addr = cfg->root_ba;
936
937 return total;
938 }
939
mtr_init_buf_cfg(struct hns_roce_dev * hr_dev,struct hns_roce_buf_attr * attr,struct hns_roce_hem_cfg * cfg,unsigned int * buf_page_shift)940 static int mtr_init_buf_cfg(struct hns_roce_dev *hr_dev,
941 struct hns_roce_buf_attr *attr,
942 struct hns_roce_hem_cfg *cfg,
943 unsigned int *buf_page_shift)
944 {
945 struct hns_roce_buf_region *r;
946 unsigned int page_shift;
947 int page_cnt = 0;
948 size_t buf_size;
949 int region_cnt;
950
951 if (cfg->is_direct) {
952 buf_size = cfg->buf_pg_count << cfg->buf_pg_shift;
953 page_cnt = DIV_ROUND_UP(buf_size, HNS_HW_PAGE_SIZE);
954 /*
955 * When HEM buffer use level-0 addressing, the page size equals
956 * the buffer size, and the the page size = 4K * 2^N.
957 */
958 cfg->buf_pg_shift = HNS_HW_PAGE_SHIFT + order_base_2(page_cnt);
959 if (attr->region_count > 1) {
960 cfg->buf_pg_count = page_cnt;
961 page_shift = HNS_HW_PAGE_SHIFT;
962 } else {
963 cfg->buf_pg_count = 1;
964 page_shift = cfg->buf_pg_shift;
965 if (buf_size != 1 << page_shift) {
966 ibdev_err(&hr_dev->ib_dev,
967 "failed to check direct size %zu shift %d.\n",
968 buf_size, page_shift);
969 return -EINVAL;
970 }
971 }
972 } else {
973 page_shift = cfg->buf_pg_shift;
974 }
975
976 /* convert buffer size to page index and page count */
977 for (page_cnt = 0, region_cnt = 0; page_cnt < cfg->buf_pg_count &&
978 region_cnt < attr->region_count &&
979 region_cnt < ARRAY_SIZE(cfg->region); region_cnt++) {
980 r = &cfg->region[region_cnt];
981 r->offset = page_cnt;
982 buf_size = hr_hw_page_align(attr->region[region_cnt].size);
983 r->count = DIV_ROUND_UP(buf_size, 1 << page_shift);
984 page_cnt += r->count;
985 r->hopnum = to_hr_hem_hopnum(attr->region[region_cnt].hopnum,
986 r->count);
987 }
988
989 if (region_cnt < 1) {
990 ibdev_err(&hr_dev->ib_dev,
991 "failed to check mtr region count, pages = %d.\n",
992 cfg->buf_pg_count);
993 return -ENOBUFS;
994 }
995
996 cfg->region_count = region_cnt;
997 *buf_page_shift = page_shift;
998
999 return page_cnt;
1000 }
1001
1002 /**
1003 * hns_roce_mtr_create - Create hns memory translate region.
1004 *
1005 * @mtr: memory translate region
1006 * @buf_attr: buffer attribute for creating mtr
1007 * @ba_page_shift: page shift for multi-hop base address table
1008 * @udata: user space context, if it's NULL, means kernel space
1009 * @user_addr: userspace virtual address to start at
1010 */
hns_roce_mtr_create(struct hns_roce_dev * hr_dev,struct hns_roce_mtr * mtr,struct hns_roce_buf_attr * buf_attr,unsigned int ba_page_shift,struct ib_udata * udata,unsigned long user_addr)1011 int hns_roce_mtr_create(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr,
1012 struct hns_roce_buf_attr *buf_attr,
1013 unsigned int ba_page_shift, struct ib_udata *udata,
1014 unsigned long user_addr)
1015 {
1016 struct hns_roce_hem_cfg *cfg = &mtr->hem_cfg;
1017 struct ib_device *ibdev = &hr_dev->ib_dev;
1018 unsigned int buf_page_shift = 0;
1019 dma_addr_t *pages = NULL;
1020 int all_pg_cnt;
1021 int get_pg_cnt;
1022 int ret = 0;
1023
1024 /* if disable mtt, all pages must in a continuous address range */
1025 cfg->is_direct = !mtr_has_mtt(buf_attr);
1026
1027 /* if buffer only need mtt, just init the hem cfg */
1028 if (buf_attr->mtt_only) {
1029 cfg->buf_pg_shift = buf_attr->page_shift;
1030 cfg->buf_pg_count = mtr_bufs_size(buf_attr) >>
1031 buf_attr->page_shift;
1032 mtr->umem = NULL;
1033 mtr->kmem = NULL;
1034 } else {
1035 ret = mtr_alloc_bufs(hr_dev, mtr, buf_attr, cfg->is_direct,
1036 udata, user_addr);
1037 if (ret) {
1038 ibdev_err(ibdev,
1039 "failed to alloc mtr bufs, ret = %d.\n", ret);
1040 return ret;
1041 }
1042 }
1043
1044 all_pg_cnt = mtr_init_buf_cfg(hr_dev, buf_attr, cfg, &buf_page_shift);
1045 if (all_pg_cnt < 1) {
1046 ret = -ENOBUFS;
1047 ibdev_err(ibdev, "failed to init mtr buf cfg.\n");
1048 goto err_alloc_bufs;
1049 }
1050
1051 hns_roce_hem_list_init(&mtr->hem_list);
1052 if (!cfg->is_direct) {
1053 ret = hns_roce_hem_list_request(hr_dev, &mtr->hem_list,
1054 cfg->region, cfg->region_count,
1055 ba_page_shift);
1056 if (ret) {
1057 ibdev_err(ibdev, "failed to request mtr hem, ret = %d.\n",
1058 ret);
1059 goto err_alloc_bufs;
1060 }
1061 cfg->root_ba = mtr->hem_list.root_ba;
1062 cfg->ba_pg_shift = ba_page_shift;
1063 } else {
1064 cfg->ba_pg_shift = cfg->buf_pg_shift;
1065 }
1066
1067 /* no buffer to map */
1068 if (buf_attr->mtt_only)
1069 return 0;
1070
1071 /* alloc a tmp array to store buffer's dma address */
1072 pages = kvcalloc(all_pg_cnt, sizeof(dma_addr_t), GFP_KERNEL);
1073 if (!pages) {
1074 ret = -ENOMEM;
1075 ibdev_err(ibdev, "failed to alloc mtr page list %d.\n",
1076 all_pg_cnt);
1077 goto err_alloc_hem_list;
1078 }
1079
1080 get_pg_cnt = mtr_get_pages(hr_dev, mtr, pages, all_pg_cnt,
1081 buf_page_shift);
1082 if (get_pg_cnt != all_pg_cnt) {
1083 ibdev_err(ibdev, "failed to get mtr page %d != %d.\n",
1084 get_pg_cnt, all_pg_cnt);
1085 ret = -ENOBUFS;
1086 goto err_alloc_page_list;
1087 }
1088
1089 /* write buffer's dma address to BA table */
1090 ret = hns_roce_mtr_map(hr_dev, mtr, pages, all_pg_cnt);
1091 if (ret) {
1092 ibdev_err(ibdev, "failed to map mtr pages, ret = %d.\n", ret);
1093 goto err_alloc_page_list;
1094 }
1095
1096 /* drop tmp array */
1097 kvfree(pages);
1098 return 0;
1099 err_alloc_page_list:
1100 kvfree(pages);
1101 err_alloc_hem_list:
1102 hns_roce_hem_list_release(hr_dev, &mtr->hem_list);
1103 err_alloc_bufs:
1104 mtr_free_bufs(hr_dev, mtr);
1105 return ret;
1106 }
1107
hns_roce_mtr_destroy(struct hns_roce_dev * hr_dev,struct hns_roce_mtr * mtr)1108 void hns_roce_mtr_destroy(struct hns_roce_dev *hr_dev, struct hns_roce_mtr *mtr)
1109 {
1110 /* release multi-hop addressing resource */
1111 hns_roce_hem_list_release(hr_dev, &mtr->hem_list);
1112
1113 /* free buffers */
1114 mtr_free_bufs(hr_dev, mtr);
1115 }
1116