hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/infiniband/sw/rxe/rxe_mr.c
....@@ -1,34 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
12 /*
23 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
34 * Copyright (c) 2015 System Fabric Works, Inc. 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.
325 */
336
347 #include "rxe.h"
....@@ -79,13 +52,8 @@
7952 u32 lkey = mem->pelem.index << 8 | rxe_get_key();
8053 u32 rkey = (access & IB_ACCESS_REMOTE) ? lkey : 0;
8154
82
- if (mem->pelem.pool->type == RXE_TYPE_MR) {
83
- mem->ibmr.lkey = lkey;
84
- mem->ibmr.rkey = rkey;
85
- }
86
-
87
- mem->lkey = lkey;
88
- mem->rkey = rkey;
55
+ mem->ibmr.lkey = lkey;
56
+ mem->ibmr.rkey = rkey;
8957 mem->state = RXE_MEM_STATE_INVALID;
9058 mem->type = RXE_MEM_TYPE_NONE;
9159 mem->map_shift = ilog2(RXE_BUF_PER_MAP);
....@@ -96,8 +64,7 @@
9664 struct rxe_mem *mem = container_of(arg, typeof(*mem), pelem);
9765 int i;
9866
99
- if (mem->umem)
100
- ib_umem_release(mem->umem);
67
+ ib_umem_release(mem->umem);
10168
10269 if (mem->map) {
10370 for (i = 0; i < mem->num_map; i++)
....@@ -145,33 +112,30 @@
145112 return -ENOMEM;
146113 }
147114
148
-int rxe_mem_init_dma(struct rxe_pd *pd,
149
- int access, struct rxe_mem *mem)
115
+void rxe_mem_init_dma(struct rxe_pd *pd,
116
+ int access, struct rxe_mem *mem)
150117 {
151118 rxe_mem_init(access, mem);
152119
153
- mem->pd = pd;
120
+ mem->ibmr.pd = &pd->ibpd;
154121 mem->access = access;
155122 mem->state = RXE_MEM_STATE_VALID;
156123 mem->type = RXE_MEM_TYPE_DMA;
157
-
158
- return 0;
159124 }
160125
161126 int rxe_mem_init_user(struct rxe_pd *pd, u64 start,
162127 u64 length, u64 iova, int access, struct ib_udata *udata,
163128 struct rxe_mem *mem)
164129 {
165
- int entry;
166130 struct rxe_map **map;
167131 struct rxe_phys_buf *buf = NULL;
168132 struct ib_umem *umem;
169
- struct scatterlist *sg;
133
+ struct sg_page_iter sg_iter;
170134 int num_buf;
171135 void *vaddr;
172136 int err;
173137
174
- umem = ib_umem_get(pd->ibpd.uobject->context, start, length, access, 0);
138
+ umem = ib_umem_get(pd->ibpd.device, start, length, access);
175139 if (IS_ERR(umem)) {
176140 pr_warn("err %d from rxe_umem_get\n",
177141 (int)PTR_ERR(umem));
....@@ -180,7 +144,7 @@
180144 }
181145
182146 mem->umem = umem;
183
- num_buf = umem->nmap;
147
+ num_buf = ib_umem_num_pages(umem);
184148
185149 rxe_mem_init(access, mem);
186150
....@@ -191,16 +155,22 @@
191155 goto err1;
192156 }
193157
194
- mem->page_shift = umem->page_shift;
195
- mem->page_mask = BIT(umem->page_shift) - 1;
158
+ mem->page_shift = PAGE_SHIFT;
159
+ mem->page_mask = PAGE_SIZE - 1;
196160
197161 num_buf = 0;
198162 map = mem->map;
199163 if (length > 0) {
200164 buf = map[0]->buf;
201165
202
- for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
203
- vaddr = page_address(sg_page(sg));
166
+ for_each_sg_page(umem->sg_head.sgl, &sg_iter, umem->nmap, 0) {
167
+ if (num_buf >= RXE_BUF_PER_MAP) {
168
+ map++;
169
+ buf = map[0]->buf;
170
+ num_buf = 0;
171
+ }
172
+
173
+ vaddr = page_address(sg_page_iter_page(&sg_iter));
204174 if (!vaddr) {
205175 pr_warn("null vaddr\n");
206176 ib_umem_release(umem);
....@@ -209,19 +179,14 @@
209179 }
210180
211181 buf->addr = (uintptr_t)vaddr;
212
- buf->size = BIT(umem->page_shift);
182
+ buf->size = PAGE_SIZE;
213183 num_buf++;
214184 buf++;
215185
216
- if (num_buf >= RXE_BUF_PER_MAP) {
217
- map++;
218
- buf = map[0]->buf;
219
- num_buf = 0;
220
- }
221186 }
222187 }
223188
224
- mem->pd = pd;
189
+ mem->ibmr.pd = &pd->ibpd;
225190 mem->umem = umem;
226191 mem->access = access;
227192 mem->length = length;
....@@ -251,7 +216,7 @@
251216 if (err)
252217 goto err1;
253218
254
- mem->pd = pd;
219
+ mem->ibmr.pd = &pd->ibpd;
255220 mem->max_buf = max_pages;
256221 mem->state = RXE_MEM_STATE_FREE;
257222 mem->type = RXE_MEM_TYPE_MR;
....@@ -371,7 +336,7 @@
371336 memcpy(dest, src, length);
372337
373338 if (crcp)
374
- *crcp = rxe_crc32(to_rdev(mem->pd->ibpd.device),
339
+ *crcp = rxe_crc32(to_rdev(mem->ibmr.device),
375340 *crcp, dest, length);
376341
377342 return 0;
....@@ -405,7 +370,7 @@
405370 memcpy(dest, src, bytes);
406371
407372 if (crcp)
408
- crc = rxe_crc32(to_rdev(mem->pd->ibpd.device),
373
+ crc = rxe_crc32(to_rdev(mem->ibmr.device),
409374 crc, dest, bytes);
410375
411376 length -= bytes;
....@@ -574,75 +539,18 @@
574539 struct rxe_dev *rxe = to_rdev(pd->ibpd.device);
575540 int index = key >> 8;
576541
577
- if (index >= RXE_MIN_MR_INDEX && index <= RXE_MAX_MR_INDEX) {
578
- mem = rxe_pool_get_index(&rxe->mr_pool, index);
579
- if (!mem)
580
- goto err1;
581
- } else {
582
- goto err1;
542
+ mem = rxe_pool_get_index(&rxe->mr_pool, index);
543
+ if (!mem)
544
+ return NULL;
545
+
546
+ if (unlikely((type == lookup_local && mr_lkey(mem) != key) ||
547
+ (type == lookup_remote && mr_rkey(mem) != key) ||
548
+ mr_pd(mem) != pd ||
549
+ (access && !(access & mem->access)) ||
550
+ mem->state != RXE_MEM_STATE_VALID)) {
551
+ rxe_drop_ref(mem);
552
+ mem = NULL;
583553 }
584
-
585
- if ((type == lookup_local && mem->lkey != key) ||
586
- (type == lookup_remote && mem->rkey != key))
587
- goto err2;
588
-
589
- if (mem->pd != pd)
590
- goto err2;
591
-
592
- if (access && !(access & mem->access))
593
- goto err2;
594
-
595
- if (mem->state != RXE_MEM_STATE_VALID)
596
- goto err2;
597554
598555 return mem;
599
-
600
-err2:
601
- rxe_drop_ref(mem);
602
-err1:
603
- return NULL;
604
-}
605
-
606
-int rxe_mem_map_pages(struct rxe_dev *rxe, struct rxe_mem *mem,
607
- u64 *page, int num_pages, u64 iova)
608
-{
609
- int i;
610
- int num_buf;
611
- int err;
612
- struct rxe_map **map;
613
- struct rxe_phys_buf *buf;
614
- int page_size;
615
-
616
- if (num_pages > mem->max_buf) {
617
- err = -EINVAL;
618
- goto err1;
619
- }
620
-
621
- num_buf = 0;
622
- page_size = 1 << mem->page_shift;
623
- map = mem->map;
624
- buf = map[0]->buf;
625
-
626
- for (i = 0; i < num_pages; i++) {
627
- buf->addr = *page++;
628
- buf->size = page_size;
629
- buf++;
630
- num_buf++;
631
-
632
- if (num_buf == RXE_BUF_PER_MAP) {
633
- map++;
634
- buf = map[0]->buf;
635
- num_buf = 0;
636
- }
637
- }
638
-
639
- mem->iova = iova;
640
- mem->va = iova;
641
- mem->length = num_pages << mem->page_shift;
642
- mem->state = RXE_MEM_STATE_VALID;
643
-
644
- return 0;
645
-
646
-err1:
647
- return err;
648556 }