1 /*
2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3 * Copyright (c) 2020, Intel Corporation. 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
35 #include <linux/kref.h>
36 #include <linux/random.h>
37 #include <linux/debugfs.h>
38 #include <linux/export.h>
39 #include <linux/delay.h>
40 #include <linux/dma-buf.h>
41 #include <linux/dma-resv.h>
42 #include <rdma/ib_umem.h>
43 #include <rdma/ib_umem_odp.h>
44 #include <rdma/ib_verbs.h>
45 #include "dm.h"
46 #include "mlx5_ib.h"
47
48 /*
49 * We can't use an array for xlt_emergency_page because dma_map_single doesn't
50 * work on kernel modules memory
51 */
52 void *xlt_emergency_page;
53 static DEFINE_MUTEX(xlt_emergency_page_mutex);
54
55 enum {
56 MAX_PENDING_REG_MR = 8,
57 };
58
59 #define MLX5_UMR_ALIGN 2048
60
61 static void
62 create_mkey_callback(int status, struct mlx5_async_work *context);
63 static struct mlx5_ib_mr *reg_create(struct ib_pd *pd, struct ib_umem *umem,
64 u64 iova, int access_flags,
65 unsigned int page_size, bool populate);
66
set_mkc_access_pd_addr_fields(void * mkc,int acc,u64 start_addr,struct ib_pd * pd)67 static void set_mkc_access_pd_addr_fields(void *mkc, int acc, u64 start_addr,
68 struct ib_pd *pd)
69 {
70 struct mlx5_ib_dev *dev = to_mdev(pd->device);
71 bool ro_pci_enabled = pcie_relaxed_ordering_enabled(dev->mdev->pdev);
72
73 MLX5_SET(mkc, mkc, a, !!(acc & IB_ACCESS_REMOTE_ATOMIC));
74 MLX5_SET(mkc, mkc, rw, !!(acc & IB_ACCESS_REMOTE_WRITE));
75 MLX5_SET(mkc, mkc, rr, !!(acc & IB_ACCESS_REMOTE_READ));
76 MLX5_SET(mkc, mkc, lw, !!(acc & IB_ACCESS_LOCAL_WRITE));
77 MLX5_SET(mkc, mkc, lr, 1);
78
79 if (MLX5_CAP_GEN(dev->mdev, relaxed_ordering_write))
80 MLX5_SET(mkc, mkc, relaxed_ordering_write,
81 (acc & IB_ACCESS_RELAXED_ORDERING) && ro_pci_enabled);
82 if (MLX5_CAP_GEN(dev->mdev, relaxed_ordering_read))
83 MLX5_SET(mkc, mkc, relaxed_ordering_read,
84 (acc & IB_ACCESS_RELAXED_ORDERING) && ro_pci_enabled);
85
86 MLX5_SET(mkc, mkc, pd, to_mpd(pd)->pdn);
87 MLX5_SET(mkc, mkc, qpn, 0xffffff);
88 MLX5_SET64(mkc, mkc, start_addr, start_addr);
89 }
90
91 static void
assign_mkey_variant(struct mlx5_ib_dev * dev,struct mlx5_core_mkey * mkey,u32 * in)92 assign_mkey_variant(struct mlx5_ib_dev *dev, struct mlx5_core_mkey *mkey,
93 u32 *in)
94 {
95 u8 key = atomic_inc_return(&dev->mkey_var);
96 void *mkc;
97
98 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
99 MLX5_SET(mkc, mkc, mkey_7_0, key);
100 mkey->key = key;
101 }
102
103 static int
mlx5_ib_create_mkey(struct mlx5_ib_dev * dev,struct mlx5_core_mkey * mkey,u32 * in,int inlen)104 mlx5_ib_create_mkey(struct mlx5_ib_dev *dev, struct mlx5_core_mkey *mkey,
105 u32 *in, int inlen)
106 {
107 assign_mkey_variant(dev, mkey, in);
108 return mlx5_core_create_mkey(dev->mdev, mkey, in, inlen);
109 }
110
111 static int
mlx5_ib_create_mkey_cb(struct mlx5_ib_dev * dev,struct mlx5_core_mkey * mkey,struct mlx5_async_ctx * async_ctx,u32 * in,int inlen,u32 * out,int outlen,struct mlx5_async_work * context)112 mlx5_ib_create_mkey_cb(struct mlx5_ib_dev *dev,
113 struct mlx5_core_mkey *mkey,
114 struct mlx5_async_ctx *async_ctx,
115 u32 *in, int inlen, u32 *out, int outlen,
116 struct mlx5_async_work *context)
117 {
118 MLX5_SET(create_mkey_in, in, opcode, MLX5_CMD_OP_CREATE_MKEY);
119 assign_mkey_variant(dev, mkey, in);
120 return mlx5_cmd_exec_cb(async_ctx, in, inlen, out, outlen,
121 create_mkey_callback, context);
122 }
123
124 static int mr_cache_max_order(struct mlx5_ib_dev *dev);
125 static void queue_adjust_cache_locked(struct mlx5_cache_ent *ent);
126
umr_can_use_indirect_mkey(struct mlx5_ib_dev * dev)127 static bool umr_can_use_indirect_mkey(struct mlx5_ib_dev *dev)
128 {
129 return !MLX5_CAP_GEN(dev->mdev, umr_indirect_mkey_disabled);
130 }
131
destroy_mkey(struct mlx5_ib_dev * dev,struct mlx5_ib_mr * mr)132 static int destroy_mkey(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
133 {
134 WARN_ON(xa_load(&dev->odp_mkeys, mlx5_base_mkey(mr->mmkey.key)));
135
136 return mlx5_core_destroy_mkey(dev->mdev, &mr->mmkey);
137 }
138
create_mkey_callback(int status,struct mlx5_async_work * context)139 static void create_mkey_callback(int status, struct mlx5_async_work *context)
140 {
141 struct mlx5_ib_mr *mr =
142 container_of(context, struct mlx5_ib_mr, cb_work);
143 struct mlx5_cache_ent *ent = mr->cache_ent;
144 struct mlx5_ib_dev *dev = ent->dev;
145 unsigned long flags;
146
147 if (status) {
148 mlx5_ib_warn(dev, "async reg mr failed. status %d\n", status);
149 kfree(mr);
150 spin_lock_irqsave(&ent->lock, flags);
151 ent->pending--;
152 WRITE_ONCE(dev->fill_delay, 1);
153 spin_unlock_irqrestore(&ent->lock, flags);
154 mod_timer(&dev->delay_timer, jiffies + HZ);
155 return;
156 }
157
158 mr->mmkey.type = MLX5_MKEY_MR;
159 mr->mmkey.key |= mlx5_idx_to_mkey(
160 MLX5_GET(create_mkey_out, mr->out, mkey_index));
161 init_waitqueue_head(&mr->mmkey.wait);
162
163 WRITE_ONCE(dev->cache.last_add, jiffies);
164
165 spin_lock_irqsave(&ent->lock, flags);
166 list_add_tail(&mr->list, &ent->head);
167 ent->available_mrs++;
168 ent->total_mrs++;
169 /* If we are doing fill_to_high_water then keep going. */
170 queue_adjust_cache_locked(ent);
171 ent->pending--;
172 spin_unlock_irqrestore(&ent->lock, flags);
173 }
174
alloc_cache_mr(struct mlx5_cache_ent * ent,void * mkc)175 static struct mlx5_ib_mr *alloc_cache_mr(struct mlx5_cache_ent *ent, void *mkc)
176 {
177 struct mlx5_ib_mr *mr;
178
179 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
180 if (!mr)
181 return NULL;
182 mr->cache_ent = ent;
183
184 set_mkc_access_pd_addr_fields(mkc, 0, 0, ent->dev->umrc.pd);
185 MLX5_SET(mkc, mkc, free, 1);
186 MLX5_SET(mkc, mkc, umr_en, 1);
187 MLX5_SET(mkc, mkc, access_mode_1_0, ent->access_mode & 0x3);
188 MLX5_SET(mkc, mkc, access_mode_4_2, (ent->access_mode >> 2) & 0x7);
189
190 MLX5_SET(mkc, mkc, translations_octword_size, ent->xlt);
191 MLX5_SET(mkc, mkc, log_page_size, ent->page);
192 return mr;
193 }
194
195 /* Asynchronously schedule new MRs to be populated in the cache. */
add_keys(struct mlx5_cache_ent * ent,unsigned int num)196 static int add_keys(struct mlx5_cache_ent *ent, unsigned int num)
197 {
198 size_t inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
199 struct mlx5_ib_mr *mr;
200 void *mkc;
201 u32 *in;
202 int err = 0;
203 int i;
204
205 in = kzalloc(inlen, GFP_KERNEL);
206 if (!in)
207 return -ENOMEM;
208
209 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
210 for (i = 0; i < num; i++) {
211 mr = alloc_cache_mr(ent, mkc);
212 if (!mr) {
213 err = -ENOMEM;
214 break;
215 }
216 spin_lock_irq(&ent->lock);
217 if (ent->pending >= MAX_PENDING_REG_MR) {
218 err = -EAGAIN;
219 spin_unlock_irq(&ent->lock);
220 kfree(mr);
221 break;
222 }
223 ent->pending++;
224 spin_unlock_irq(&ent->lock);
225 err = mlx5_ib_create_mkey_cb(ent->dev, &mr->mmkey,
226 &ent->dev->async_ctx, in, inlen,
227 mr->out, sizeof(mr->out),
228 &mr->cb_work);
229 if (err) {
230 spin_lock_irq(&ent->lock);
231 ent->pending--;
232 spin_unlock_irq(&ent->lock);
233 mlx5_ib_warn(ent->dev, "create mkey failed %d\n", err);
234 kfree(mr);
235 break;
236 }
237 }
238
239 kfree(in);
240 return err;
241 }
242
243 /* Synchronously create a MR in the cache */
create_cache_mr(struct mlx5_cache_ent * ent)244 static struct mlx5_ib_mr *create_cache_mr(struct mlx5_cache_ent *ent)
245 {
246 size_t inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
247 struct mlx5_ib_mr *mr;
248 void *mkc;
249 u32 *in;
250 int err;
251
252 in = kzalloc(inlen, GFP_KERNEL);
253 if (!in)
254 return ERR_PTR(-ENOMEM);
255 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
256
257 mr = alloc_cache_mr(ent, mkc);
258 if (!mr) {
259 err = -ENOMEM;
260 goto free_in;
261 }
262
263 err = mlx5_core_create_mkey(ent->dev->mdev, &mr->mmkey, in, inlen);
264 if (err)
265 goto free_mr;
266
267 mr->mmkey.type = MLX5_MKEY_MR;
268 WRITE_ONCE(ent->dev->cache.last_add, jiffies);
269 spin_lock_irq(&ent->lock);
270 ent->total_mrs++;
271 spin_unlock_irq(&ent->lock);
272 kfree(in);
273 return mr;
274 free_mr:
275 kfree(mr);
276 free_in:
277 kfree(in);
278 return ERR_PTR(err);
279 }
280
remove_cache_mr_locked(struct mlx5_cache_ent * ent)281 static void remove_cache_mr_locked(struct mlx5_cache_ent *ent)
282 {
283 struct mlx5_ib_mr *mr;
284
285 lockdep_assert_held(&ent->lock);
286 if (list_empty(&ent->head))
287 return;
288 mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
289 list_del(&mr->list);
290 ent->available_mrs--;
291 ent->total_mrs--;
292 spin_unlock_irq(&ent->lock);
293 mlx5_core_destroy_mkey(ent->dev->mdev, &mr->mmkey);
294 kfree(mr);
295 spin_lock_irq(&ent->lock);
296 }
297
resize_available_mrs(struct mlx5_cache_ent * ent,unsigned int target,bool limit_fill)298 static int resize_available_mrs(struct mlx5_cache_ent *ent, unsigned int target,
299 bool limit_fill)
300 {
301 int err;
302
303 lockdep_assert_held(&ent->lock);
304
305 while (true) {
306 if (limit_fill)
307 target = ent->limit * 2;
308 if (target == ent->available_mrs + ent->pending)
309 return 0;
310 if (target > ent->available_mrs + ent->pending) {
311 u32 todo = target - (ent->available_mrs + ent->pending);
312
313 spin_unlock_irq(&ent->lock);
314 err = add_keys(ent, todo);
315 if (err == -EAGAIN)
316 usleep_range(3000, 5000);
317 spin_lock_irq(&ent->lock);
318 if (err) {
319 if (err != -EAGAIN)
320 return err;
321 } else
322 return 0;
323 } else {
324 remove_cache_mr_locked(ent);
325 }
326 }
327 }
328
size_write(struct file * filp,const char __user * buf,size_t count,loff_t * pos)329 static ssize_t size_write(struct file *filp, const char __user *buf,
330 size_t count, loff_t *pos)
331 {
332 struct mlx5_cache_ent *ent = filp->private_data;
333 u32 target;
334 int err;
335
336 err = kstrtou32_from_user(buf, count, 0, &target);
337 if (err)
338 return err;
339
340 /*
341 * Target is the new value of total_mrs the user requests, however we
342 * cannot free MRs that are in use. Compute the target value for
343 * available_mrs.
344 */
345 spin_lock_irq(&ent->lock);
346 if (target < ent->total_mrs - ent->available_mrs) {
347 err = -EINVAL;
348 goto err_unlock;
349 }
350 target = target - (ent->total_mrs - ent->available_mrs);
351 if (target < ent->limit || target > ent->limit*2) {
352 err = -EINVAL;
353 goto err_unlock;
354 }
355 err = resize_available_mrs(ent, target, false);
356 if (err)
357 goto err_unlock;
358 spin_unlock_irq(&ent->lock);
359
360 return count;
361
362 err_unlock:
363 spin_unlock_irq(&ent->lock);
364 return err;
365 }
366
size_read(struct file * filp,char __user * buf,size_t count,loff_t * pos)367 static ssize_t size_read(struct file *filp, char __user *buf, size_t count,
368 loff_t *pos)
369 {
370 struct mlx5_cache_ent *ent = filp->private_data;
371 char lbuf[20];
372 int err;
373
374 err = snprintf(lbuf, sizeof(lbuf), "%d\n", ent->total_mrs);
375 if (err < 0)
376 return err;
377
378 return simple_read_from_buffer(buf, count, pos, lbuf, err);
379 }
380
381 static const struct file_operations size_fops = {
382 .owner = THIS_MODULE,
383 .open = simple_open,
384 .write = size_write,
385 .read = size_read,
386 };
387
limit_write(struct file * filp,const char __user * buf,size_t count,loff_t * pos)388 static ssize_t limit_write(struct file *filp, const char __user *buf,
389 size_t count, loff_t *pos)
390 {
391 struct mlx5_cache_ent *ent = filp->private_data;
392 u32 var;
393 int err;
394
395 err = kstrtou32_from_user(buf, count, 0, &var);
396 if (err)
397 return err;
398
399 /*
400 * Upon set we immediately fill the cache to high water mark implied by
401 * the limit.
402 */
403 spin_lock_irq(&ent->lock);
404 ent->limit = var;
405 err = resize_available_mrs(ent, 0, true);
406 spin_unlock_irq(&ent->lock);
407 if (err)
408 return err;
409 return count;
410 }
411
limit_read(struct file * filp,char __user * buf,size_t count,loff_t * pos)412 static ssize_t limit_read(struct file *filp, char __user *buf, size_t count,
413 loff_t *pos)
414 {
415 struct mlx5_cache_ent *ent = filp->private_data;
416 char lbuf[20];
417 int err;
418
419 err = snprintf(lbuf, sizeof(lbuf), "%d\n", ent->limit);
420 if (err < 0)
421 return err;
422
423 return simple_read_from_buffer(buf, count, pos, lbuf, err);
424 }
425
426 static const struct file_operations limit_fops = {
427 .owner = THIS_MODULE,
428 .open = simple_open,
429 .write = limit_write,
430 .read = limit_read,
431 };
432
someone_adding(struct mlx5_mr_cache * cache)433 static bool someone_adding(struct mlx5_mr_cache *cache)
434 {
435 unsigned int i;
436
437 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
438 struct mlx5_cache_ent *ent = &cache->ent[i];
439 bool ret;
440
441 spin_lock_irq(&ent->lock);
442 ret = ent->available_mrs < ent->limit;
443 spin_unlock_irq(&ent->lock);
444 if (ret)
445 return true;
446 }
447 return false;
448 }
449
450 /*
451 * Check if the bucket is outside the high/low water mark and schedule an async
452 * update. The cache refill has hysteresis, once the low water mark is hit it is
453 * refilled up to the high mark.
454 */
queue_adjust_cache_locked(struct mlx5_cache_ent * ent)455 static void queue_adjust_cache_locked(struct mlx5_cache_ent *ent)
456 {
457 lockdep_assert_held(&ent->lock);
458
459 if (ent->disabled || READ_ONCE(ent->dev->fill_delay))
460 return;
461 if (ent->available_mrs < ent->limit) {
462 ent->fill_to_high_water = true;
463 queue_work(ent->dev->cache.wq, &ent->work);
464 } else if (ent->fill_to_high_water &&
465 ent->available_mrs + ent->pending < 2 * ent->limit) {
466 /*
467 * Once we start populating due to hitting a low water mark
468 * continue until we pass the high water mark.
469 */
470 queue_work(ent->dev->cache.wq, &ent->work);
471 } else if (ent->available_mrs == 2 * ent->limit) {
472 ent->fill_to_high_water = false;
473 } else if (ent->available_mrs > 2 * ent->limit) {
474 /* Queue deletion of excess entries */
475 ent->fill_to_high_water = false;
476 if (ent->pending)
477 queue_delayed_work(ent->dev->cache.wq, &ent->dwork,
478 msecs_to_jiffies(1000));
479 else
480 queue_work(ent->dev->cache.wq, &ent->work);
481 }
482 }
483
__cache_work_func(struct mlx5_cache_ent * ent)484 static void __cache_work_func(struct mlx5_cache_ent *ent)
485 {
486 struct mlx5_ib_dev *dev = ent->dev;
487 struct mlx5_mr_cache *cache = &dev->cache;
488 int err;
489
490 spin_lock_irq(&ent->lock);
491 if (ent->disabled)
492 goto out;
493
494 if (ent->fill_to_high_water &&
495 ent->available_mrs + ent->pending < 2 * ent->limit &&
496 !READ_ONCE(dev->fill_delay)) {
497 spin_unlock_irq(&ent->lock);
498 err = add_keys(ent, 1);
499 spin_lock_irq(&ent->lock);
500 if (ent->disabled)
501 goto out;
502 if (err) {
503 /*
504 * EAGAIN only happens if pending is positive, so we
505 * will be rescheduled from reg_mr_callback(). The only
506 * failure path here is ENOMEM.
507 */
508 if (err != -EAGAIN) {
509 mlx5_ib_warn(
510 dev,
511 "command failed order %d, err %d\n",
512 ent->order, err);
513 queue_delayed_work(cache->wq, &ent->dwork,
514 msecs_to_jiffies(1000));
515 }
516 }
517 } else if (ent->available_mrs > 2 * ent->limit) {
518 bool need_delay;
519
520 /*
521 * The remove_cache_mr() logic is performed as garbage
522 * collection task. Such task is intended to be run when no
523 * other active processes are running.
524 *
525 * The need_resched() will return TRUE if there are user tasks
526 * to be activated in near future.
527 *
528 * In such case, we don't execute remove_cache_mr() and postpone
529 * the garbage collection work to try to run in next cycle, in
530 * order to free CPU resources to other tasks.
531 */
532 spin_unlock_irq(&ent->lock);
533 need_delay = need_resched() || someone_adding(cache) ||
534 !time_after(jiffies,
535 READ_ONCE(cache->last_add) + 300 * HZ);
536 spin_lock_irq(&ent->lock);
537 if (ent->disabled)
538 goto out;
539 if (need_delay) {
540 queue_delayed_work(cache->wq, &ent->dwork, 300 * HZ);
541 goto out;
542 }
543 remove_cache_mr_locked(ent);
544 queue_adjust_cache_locked(ent);
545 }
546 out:
547 spin_unlock_irq(&ent->lock);
548 }
549
delayed_cache_work_func(struct work_struct * work)550 static void delayed_cache_work_func(struct work_struct *work)
551 {
552 struct mlx5_cache_ent *ent;
553
554 ent = container_of(work, struct mlx5_cache_ent, dwork.work);
555 __cache_work_func(ent);
556 }
557
cache_work_func(struct work_struct * work)558 static void cache_work_func(struct work_struct *work)
559 {
560 struct mlx5_cache_ent *ent;
561
562 ent = container_of(work, struct mlx5_cache_ent, work);
563 __cache_work_func(ent);
564 }
565
566 /* Allocate a special entry from the cache */
mlx5_mr_cache_alloc(struct mlx5_ib_dev * dev,unsigned int entry,int access_flags)567 struct mlx5_ib_mr *mlx5_mr_cache_alloc(struct mlx5_ib_dev *dev,
568 unsigned int entry, int access_flags)
569 {
570 struct mlx5_mr_cache *cache = &dev->cache;
571 struct mlx5_cache_ent *ent;
572 struct mlx5_ib_mr *mr;
573
574 if (WARN_ON(entry <= MR_CACHE_LAST_STD_ENTRY ||
575 entry >= ARRAY_SIZE(cache->ent)))
576 return ERR_PTR(-EINVAL);
577
578 /* Matches access in alloc_cache_mr() */
579 if (!mlx5_ib_can_reconfig_with_umr(dev, 0, access_flags))
580 return ERR_PTR(-EOPNOTSUPP);
581
582 ent = &cache->ent[entry];
583 spin_lock_irq(&ent->lock);
584 if (list_empty(&ent->head)) {
585 queue_adjust_cache_locked(ent);
586 ent->miss++;
587 spin_unlock_irq(&ent->lock);
588 mr = create_cache_mr(ent);
589 if (IS_ERR(mr))
590 return mr;
591 } else {
592 mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
593 list_del(&mr->list);
594 ent->available_mrs--;
595 queue_adjust_cache_locked(ent);
596 spin_unlock_irq(&ent->lock);
597
598 mlx5_clear_mr(mr);
599 }
600 mr->access_flags = access_flags;
601 return mr;
602 }
603
604 /* Return a MR already available in the cache */
get_cache_mr(struct mlx5_cache_ent * req_ent)605 static struct mlx5_ib_mr *get_cache_mr(struct mlx5_cache_ent *req_ent)
606 {
607 struct mlx5_ib_dev *dev = req_ent->dev;
608 struct mlx5_ib_mr *mr = NULL;
609 struct mlx5_cache_ent *ent = req_ent;
610
611 /* Try larger MR pools from the cache to satisfy the allocation */
612 for (; ent != &dev->cache.ent[MR_CACHE_LAST_STD_ENTRY + 1]; ent++) {
613 mlx5_ib_dbg(dev, "order %u, cache index %zu\n", ent->order,
614 ent - dev->cache.ent);
615
616 spin_lock_irq(&ent->lock);
617 if (!list_empty(&ent->head)) {
618 mr = list_first_entry(&ent->head, struct mlx5_ib_mr,
619 list);
620 list_del(&mr->list);
621 ent->available_mrs--;
622 queue_adjust_cache_locked(ent);
623 spin_unlock_irq(&ent->lock);
624 mlx5_clear_mr(mr);
625 return mr;
626 }
627 queue_adjust_cache_locked(ent);
628 spin_unlock_irq(&ent->lock);
629 }
630 req_ent->miss++;
631 return NULL;
632 }
633
mlx5_mr_cache_free(struct mlx5_ib_dev * dev,struct mlx5_ib_mr * mr)634 static void mlx5_mr_cache_free(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
635 {
636 struct mlx5_cache_ent *ent = mr->cache_ent;
637
638 WRITE_ONCE(dev->cache.last_add, jiffies);
639 spin_lock_irq(&ent->lock);
640 list_add_tail(&mr->list, &ent->head);
641 ent->available_mrs++;
642 queue_adjust_cache_locked(ent);
643 spin_unlock_irq(&ent->lock);
644 }
645
clean_keys(struct mlx5_ib_dev * dev,int c)646 static void clean_keys(struct mlx5_ib_dev *dev, int c)
647 {
648 struct mlx5_mr_cache *cache = &dev->cache;
649 struct mlx5_cache_ent *ent = &cache->ent[c];
650 struct mlx5_ib_mr *tmp_mr;
651 struct mlx5_ib_mr *mr;
652 LIST_HEAD(del_list);
653
654 cancel_delayed_work(&ent->dwork);
655 while (1) {
656 spin_lock_irq(&ent->lock);
657 if (list_empty(&ent->head)) {
658 spin_unlock_irq(&ent->lock);
659 break;
660 }
661 mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
662 list_move(&mr->list, &del_list);
663 ent->available_mrs--;
664 ent->total_mrs--;
665 spin_unlock_irq(&ent->lock);
666 mlx5_core_destroy_mkey(dev->mdev, &mr->mmkey);
667 }
668
669 list_for_each_entry_safe(mr, tmp_mr, &del_list, list) {
670 list_del(&mr->list);
671 kfree(mr);
672 }
673 }
674
mlx5_mr_cache_debugfs_cleanup(struct mlx5_ib_dev * dev)675 static void mlx5_mr_cache_debugfs_cleanup(struct mlx5_ib_dev *dev)
676 {
677 if (!mlx5_debugfs_root || dev->is_rep)
678 return;
679
680 debugfs_remove_recursive(dev->cache.root);
681 dev->cache.root = NULL;
682 }
683
mlx5_mr_cache_debugfs_init(struct mlx5_ib_dev * dev)684 static void mlx5_mr_cache_debugfs_init(struct mlx5_ib_dev *dev)
685 {
686 struct mlx5_mr_cache *cache = &dev->cache;
687 struct mlx5_cache_ent *ent;
688 struct dentry *dir;
689 int i;
690
691 if (!mlx5_debugfs_root || dev->is_rep)
692 return;
693
694 cache->root = debugfs_create_dir("mr_cache", dev->mdev->priv.dbg_root);
695
696 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
697 ent = &cache->ent[i];
698 sprintf(ent->name, "%d", ent->order);
699 dir = debugfs_create_dir(ent->name, cache->root);
700 debugfs_create_file("size", 0600, dir, ent, &size_fops);
701 debugfs_create_file("limit", 0600, dir, ent, &limit_fops);
702 debugfs_create_u32("cur", 0400, dir, &ent->available_mrs);
703 debugfs_create_u32("miss", 0600, dir, &ent->miss);
704 }
705 }
706
delay_time_func(struct timer_list * t)707 static void delay_time_func(struct timer_list *t)
708 {
709 struct mlx5_ib_dev *dev = from_timer(dev, t, delay_timer);
710
711 WRITE_ONCE(dev->fill_delay, 0);
712 }
713
mlx5_mr_cache_init(struct mlx5_ib_dev * dev)714 int mlx5_mr_cache_init(struct mlx5_ib_dev *dev)
715 {
716 struct mlx5_mr_cache *cache = &dev->cache;
717 struct mlx5_cache_ent *ent;
718 int i;
719
720 mutex_init(&dev->slow_path_mutex);
721 cache->wq = alloc_ordered_workqueue("mkey_cache", WQ_MEM_RECLAIM);
722 if (!cache->wq) {
723 mlx5_ib_warn(dev, "failed to create work queue\n");
724 return -ENOMEM;
725 }
726
727 mlx5_cmd_init_async_ctx(dev->mdev, &dev->async_ctx);
728 timer_setup(&dev->delay_timer, delay_time_func, 0);
729 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
730 ent = &cache->ent[i];
731 INIT_LIST_HEAD(&ent->head);
732 spin_lock_init(&ent->lock);
733 ent->order = i + 2;
734 ent->dev = dev;
735 ent->limit = 0;
736
737 INIT_WORK(&ent->work, cache_work_func);
738 INIT_DELAYED_WORK(&ent->dwork, delayed_cache_work_func);
739
740 if (i > MR_CACHE_LAST_STD_ENTRY) {
741 mlx5_odp_init_mr_cache_entry(ent);
742 continue;
743 }
744
745 if (ent->order > mr_cache_max_order(dev))
746 continue;
747
748 ent->page = PAGE_SHIFT;
749 ent->xlt = (1 << ent->order) * sizeof(struct mlx5_mtt) /
750 MLX5_IB_UMR_OCTOWORD;
751 ent->access_mode = MLX5_MKC_ACCESS_MODE_MTT;
752 if ((dev->mdev->profile.mask & MLX5_PROF_MASK_MR_CACHE) &&
753 !dev->is_rep && mlx5_core_is_pf(dev->mdev) &&
754 mlx5_ib_can_load_pas_with_umr(dev, 0))
755 ent->limit = dev->mdev->profile.mr_cache[i].limit;
756 else
757 ent->limit = 0;
758 spin_lock_irq(&ent->lock);
759 queue_adjust_cache_locked(ent);
760 spin_unlock_irq(&ent->lock);
761 }
762
763 mlx5_mr_cache_debugfs_init(dev);
764
765 return 0;
766 }
767
mlx5_mr_cache_cleanup(struct mlx5_ib_dev * dev)768 int mlx5_mr_cache_cleanup(struct mlx5_ib_dev *dev)
769 {
770 unsigned int i;
771
772 if (!dev->cache.wq)
773 return 0;
774
775 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
776 struct mlx5_cache_ent *ent = &dev->cache.ent[i];
777
778 spin_lock_irq(&ent->lock);
779 ent->disabled = true;
780 spin_unlock_irq(&ent->lock);
781 cancel_work_sync(&ent->work);
782 cancel_delayed_work_sync(&ent->dwork);
783 }
784
785 mlx5_mr_cache_debugfs_cleanup(dev);
786 mlx5_cmd_cleanup_async_ctx(&dev->async_ctx);
787
788 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++)
789 clean_keys(dev, i);
790
791 destroy_workqueue(dev->cache.wq);
792 del_timer_sync(&dev->delay_timer);
793
794 return 0;
795 }
796
mlx5_ib_get_dma_mr(struct ib_pd * pd,int acc)797 struct ib_mr *mlx5_ib_get_dma_mr(struct ib_pd *pd, int acc)
798 {
799 struct mlx5_ib_dev *dev = to_mdev(pd->device);
800 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
801 struct mlx5_ib_mr *mr;
802 void *mkc;
803 u32 *in;
804 int err;
805
806 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
807 if (!mr)
808 return ERR_PTR(-ENOMEM);
809
810 in = kzalloc(inlen, GFP_KERNEL);
811 if (!in) {
812 err = -ENOMEM;
813 goto err_free;
814 }
815
816 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
817
818 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_PA);
819 MLX5_SET(mkc, mkc, length64, 1);
820 set_mkc_access_pd_addr_fields(mkc, acc | IB_ACCESS_RELAXED_ORDERING, 0,
821 pd);
822
823 err = mlx5_ib_create_mkey(dev, &mr->mmkey, in, inlen);
824 if (err)
825 goto err_in;
826
827 kfree(in);
828 mr->mmkey.type = MLX5_MKEY_MR;
829 mr->ibmr.lkey = mr->mmkey.key;
830 mr->ibmr.rkey = mr->mmkey.key;
831 mr->umem = NULL;
832
833 return &mr->ibmr;
834
835 err_in:
836 kfree(in);
837
838 err_free:
839 kfree(mr);
840
841 return ERR_PTR(err);
842 }
843
get_octo_len(u64 addr,u64 len,int page_shift)844 static int get_octo_len(u64 addr, u64 len, int page_shift)
845 {
846 u64 page_size = 1ULL << page_shift;
847 u64 offset;
848 int npages;
849
850 offset = addr & (page_size - 1);
851 npages = ALIGN(len + offset, page_size) >> page_shift;
852 return (npages + 1) / 2;
853 }
854
mr_cache_max_order(struct mlx5_ib_dev * dev)855 static int mr_cache_max_order(struct mlx5_ib_dev *dev)
856 {
857 if (MLX5_CAP_GEN(dev->mdev, umr_extended_translation_offset))
858 return MR_CACHE_LAST_STD_ENTRY + 2;
859 return MLX5_MAX_UMR_SHIFT;
860 }
861
mlx5_ib_umr_done(struct ib_cq * cq,struct ib_wc * wc)862 static void mlx5_ib_umr_done(struct ib_cq *cq, struct ib_wc *wc)
863 {
864 struct mlx5_ib_umr_context *context =
865 container_of(wc->wr_cqe, struct mlx5_ib_umr_context, cqe);
866
867 context->status = wc->status;
868 complete(&context->done);
869 }
870
mlx5_ib_init_umr_context(struct mlx5_ib_umr_context * context)871 static inline void mlx5_ib_init_umr_context(struct mlx5_ib_umr_context *context)
872 {
873 context->cqe.done = mlx5_ib_umr_done;
874 context->status = -1;
875 init_completion(&context->done);
876 }
877
mlx5_ib_post_send_wait(struct mlx5_ib_dev * dev,struct mlx5_umr_wr * umrwr)878 static int mlx5_ib_post_send_wait(struct mlx5_ib_dev *dev,
879 struct mlx5_umr_wr *umrwr)
880 {
881 struct umr_common *umrc = &dev->umrc;
882 const struct ib_send_wr *bad;
883 int err;
884 struct mlx5_ib_umr_context umr_context;
885
886 mlx5_ib_init_umr_context(&umr_context);
887 umrwr->wr.wr_cqe = &umr_context.cqe;
888
889 down(&umrc->sem);
890 err = ib_post_send(umrc->qp, &umrwr->wr, &bad);
891 if (err) {
892 mlx5_ib_warn(dev, "UMR post send failed, err %d\n", err);
893 } else {
894 wait_for_completion(&umr_context.done);
895 if (umr_context.status != IB_WC_SUCCESS) {
896 mlx5_ib_warn(dev, "reg umr failed (%u)\n",
897 umr_context.status);
898 err = -EFAULT;
899 }
900 }
901 up(&umrc->sem);
902 return err;
903 }
904
mr_cache_ent_from_order(struct mlx5_ib_dev * dev,unsigned int order)905 static struct mlx5_cache_ent *mr_cache_ent_from_order(struct mlx5_ib_dev *dev,
906 unsigned int order)
907 {
908 struct mlx5_mr_cache *cache = &dev->cache;
909
910 if (order < cache->ent[0].order)
911 return &cache->ent[0];
912 order = order - cache->ent[0].order;
913 if (order > MR_CACHE_LAST_STD_ENTRY)
914 return NULL;
915 return &cache->ent[order];
916 }
917
set_mr_fields(struct mlx5_ib_dev * dev,struct mlx5_ib_mr * mr,u64 length,int access_flags)918 static void set_mr_fields(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr,
919 u64 length, int access_flags)
920 {
921 mr->ibmr.lkey = mr->mmkey.key;
922 mr->ibmr.rkey = mr->mmkey.key;
923 mr->ibmr.length = length;
924 mr->ibmr.device = &dev->ib_dev;
925 mr->access_flags = access_flags;
926 }
927
mlx5_umem_dmabuf_default_pgsz(struct ib_umem * umem,u64 iova)928 static unsigned int mlx5_umem_dmabuf_default_pgsz(struct ib_umem *umem,
929 u64 iova)
930 {
931 /*
932 * The alignment of iova has already been checked upon entering
933 * UVERBS_METHOD_REG_DMABUF_MR
934 */
935 umem->iova = iova;
936 return PAGE_SIZE;
937 }
938
alloc_cacheable_mr(struct ib_pd * pd,struct ib_umem * umem,u64 iova,int access_flags)939 static struct mlx5_ib_mr *alloc_cacheable_mr(struct ib_pd *pd,
940 struct ib_umem *umem, u64 iova,
941 int access_flags)
942 {
943 struct mlx5_ib_dev *dev = to_mdev(pd->device);
944 struct mlx5_cache_ent *ent;
945 struct mlx5_ib_mr *mr;
946 unsigned int page_size;
947
948 if (umem->is_dmabuf)
949 page_size = mlx5_umem_dmabuf_default_pgsz(umem, iova);
950 else
951 page_size = mlx5_umem_find_best_pgsz(umem, mkc, log_page_size,
952 0, iova);
953 if (WARN_ON(!page_size))
954 return ERR_PTR(-EINVAL);
955 ent = mr_cache_ent_from_order(
956 dev, order_base_2(ib_umem_num_dma_blocks(umem, page_size)));
957 /*
958 * Matches access in alloc_cache_mr(). If the MR can't come from the
959 * cache then synchronously create an uncached one.
960 */
961 if (!ent || ent->limit == 0 ||
962 !mlx5_ib_can_reconfig_with_umr(dev, 0, access_flags)) {
963 mutex_lock(&dev->slow_path_mutex);
964 mr = reg_create(pd, umem, iova, access_flags, page_size, false);
965 mutex_unlock(&dev->slow_path_mutex);
966 return mr;
967 }
968
969 mr = get_cache_mr(ent);
970 if (!mr) {
971 mr = create_cache_mr(ent);
972 /*
973 * The above already tried to do the same stuff as reg_create(),
974 * no reason to try it again.
975 */
976 if (IS_ERR(mr))
977 return mr;
978 }
979
980 mr->ibmr.pd = pd;
981 mr->umem = umem;
982 mr->mmkey.iova = iova;
983 mr->mmkey.size = umem->length;
984 mr->mmkey.pd = to_mpd(pd)->pdn;
985 mr->page_shift = order_base_2(page_size);
986 set_mr_fields(dev, mr, umem->length, access_flags);
987
988 return mr;
989 }
990
991 #define MLX5_MAX_UMR_CHUNK ((1 << (MLX5_MAX_UMR_SHIFT + 4)) - \
992 MLX5_UMR_MTT_ALIGNMENT)
993 #define MLX5_SPARE_UMR_CHUNK 0x10000
994
995 /*
996 * Allocate a temporary buffer to hold the per-page information to transfer to
997 * HW. For efficiency this should be as large as it can be, but buffer
998 * allocation failure is not allowed, so try smaller sizes.
999 */
mlx5_ib_alloc_xlt(size_t * nents,size_t ent_size,gfp_t gfp_mask)1000 static void *mlx5_ib_alloc_xlt(size_t *nents, size_t ent_size, gfp_t gfp_mask)
1001 {
1002 const size_t xlt_chunk_align =
1003 MLX5_UMR_MTT_ALIGNMENT / ent_size;
1004 size_t size;
1005 void *res = NULL;
1006
1007 static_assert(PAGE_SIZE % MLX5_UMR_MTT_ALIGNMENT == 0);
1008
1009 /*
1010 * MLX5_IB_UPD_XLT_ATOMIC doesn't signal an atomic context just that the
1011 * allocation can't trigger any kind of reclaim.
1012 */
1013 might_sleep();
1014
1015 gfp_mask |= __GFP_ZERO | __GFP_NORETRY;
1016
1017 /*
1018 * If the system already has a suitable high order page then just use
1019 * that, but don't try hard to create one. This max is about 1M, so a
1020 * free x86 huge page will satisfy it.
1021 */
1022 size = min_t(size_t, ent_size * ALIGN(*nents, xlt_chunk_align),
1023 MLX5_MAX_UMR_CHUNK);
1024 *nents = size / ent_size;
1025 res = (void *)__get_free_pages(gfp_mask | __GFP_NOWARN,
1026 get_order(size));
1027 if (res)
1028 return res;
1029
1030 if (size > MLX5_SPARE_UMR_CHUNK) {
1031 size = MLX5_SPARE_UMR_CHUNK;
1032 *nents = size / ent_size;
1033 res = (void *)__get_free_pages(gfp_mask | __GFP_NOWARN,
1034 get_order(size));
1035 if (res)
1036 return res;
1037 }
1038
1039 *nents = PAGE_SIZE / ent_size;
1040 res = (void *)__get_free_page(gfp_mask);
1041 if (res)
1042 return res;
1043
1044 mutex_lock(&xlt_emergency_page_mutex);
1045 memset(xlt_emergency_page, 0, PAGE_SIZE);
1046 return xlt_emergency_page;
1047 }
1048
mlx5_ib_free_xlt(void * xlt,size_t length)1049 static void mlx5_ib_free_xlt(void *xlt, size_t length)
1050 {
1051 if (xlt == xlt_emergency_page) {
1052 mutex_unlock(&xlt_emergency_page_mutex);
1053 return;
1054 }
1055
1056 free_pages((unsigned long)xlt, get_order(length));
1057 }
1058
1059 /*
1060 * Create a MLX5_IB_SEND_UMR_UPDATE_XLT work request and XLT buffer ready for
1061 * submission.
1062 */
mlx5_ib_create_xlt_wr(struct mlx5_ib_mr * mr,struct mlx5_umr_wr * wr,struct ib_sge * sg,size_t nents,size_t ent_size,unsigned int flags)1063 static void *mlx5_ib_create_xlt_wr(struct mlx5_ib_mr *mr,
1064 struct mlx5_umr_wr *wr, struct ib_sge *sg,
1065 size_t nents, size_t ent_size,
1066 unsigned int flags)
1067 {
1068 struct mlx5_ib_dev *dev = mr_to_mdev(mr);
1069 struct device *ddev = &dev->mdev->pdev->dev;
1070 dma_addr_t dma;
1071 void *xlt;
1072
1073 xlt = mlx5_ib_alloc_xlt(&nents, ent_size,
1074 flags & MLX5_IB_UPD_XLT_ATOMIC ? GFP_ATOMIC :
1075 GFP_KERNEL);
1076 sg->length = nents * ent_size;
1077 dma = dma_map_single(ddev, xlt, sg->length, DMA_TO_DEVICE);
1078 if (dma_mapping_error(ddev, dma)) {
1079 mlx5_ib_err(dev, "unable to map DMA during XLT update.\n");
1080 mlx5_ib_free_xlt(xlt, sg->length);
1081 return NULL;
1082 }
1083 sg->addr = dma;
1084 sg->lkey = dev->umrc.pd->local_dma_lkey;
1085
1086 memset(wr, 0, sizeof(*wr));
1087 wr->wr.send_flags = MLX5_IB_SEND_UMR_UPDATE_XLT;
1088 if (!(flags & MLX5_IB_UPD_XLT_ENABLE))
1089 wr->wr.send_flags |= MLX5_IB_SEND_UMR_FAIL_IF_FREE;
1090 wr->wr.sg_list = sg;
1091 wr->wr.num_sge = 1;
1092 wr->wr.opcode = MLX5_IB_WR_UMR;
1093 wr->pd = mr->ibmr.pd;
1094 wr->mkey = mr->mmkey.key;
1095 wr->length = mr->mmkey.size;
1096 wr->virt_addr = mr->mmkey.iova;
1097 wr->access_flags = mr->access_flags;
1098 wr->page_shift = mr->page_shift;
1099 wr->xlt_size = sg->length;
1100 return xlt;
1101 }
1102
mlx5_ib_unmap_free_xlt(struct mlx5_ib_dev * dev,void * xlt,struct ib_sge * sg)1103 static void mlx5_ib_unmap_free_xlt(struct mlx5_ib_dev *dev, void *xlt,
1104 struct ib_sge *sg)
1105 {
1106 struct device *ddev = &dev->mdev->pdev->dev;
1107
1108 dma_unmap_single(ddev, sg->addr, sg->length, DMA_TO_DEVICE);
1109 mlx5_ib_free_xlt(xlt, sg->length);
1110 }
1111
xlt_wr_final_send_flags(unsigned int flags)1112 static unsigned int xlt_wr_final_send_flags(unsigned int flags)
1113 {
1114 unsigned int res = 0;
1115
1116 if (flags & MLX5_IB_UPD_XLT_ENABLE)
1117 res |= MLX5_IB_SEND_UMR_ENABLE_MR |
1118 MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS |
1119 MLX5_IB_SEND_UMR_UPDATE_TRANSLATION;
1120 if (flags & MLX5_IB_UPD_XLT_PD || flags & MLX5_IB_UPD_XLT_ACCESS)
1121 res |= MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS;
1122 if (flags & MLX5_IB_UPD_XLT_ADDR)
1123 res |= MLX5_IB_SEND_UMR_UPDATE_TRANSLATION;
1124 return res;
1125 }
1126
mlx5_ib_update_xlt(struct mlx5_ib_mr * mr,u64 idx,int npages,int page_shift,int flags)1127 int mlx5_ib_update_xlt(struct mlx5_ib_mr *mr, u64 idx, int npages,
1128 int page_shift, int flags)
1129 {
1130 struct mlx5_ib_dev *dev = mr_to_mdev(mr);
1131 struct device *ddev = &dev->mdev->pdev->dev;
1132 void *xlt;
1133 struct mlx5_umr_wr wr;
1134 struct ib_sge sg;
1135 int err = 0;
1136 int desc_size = (flags & MLX5_IB_UPD_XLT_INDIRECT)
1137 ? sizeof(struct mlx5_klm)
1138 : sizeof(struct mlx5_mtt);
1139 const int page_align = MLX5_UMR_MTT_ALIGNMENT / desc_size;
1140 const int page_mask = page_align - 1;
1141 size_t pages_mapped = 0;
1142 size_t pages_to_map = 0;
1143 size_t pages_iter;
1144 size_t size_to_map = 0;
1145 size_t orig_sg_length;
1146
1147 if ((flags & MLX5_IB_UPD_XLT_INDIRECT) &&
1148 !umr_can_use_indirect_mkey(dev))
1149 return -EPERM;
1150
1151 if (WARN_ON(!mr->umem->is_odp))
1152 return -EINVAL;
1153
1154 /* UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes,
1155 * so we need to align the offset and length accordingly
1156 */
1157 if (idx & page_mask) {
1158 npages += idx & page_mask;
1159 idx &= ~page_mask;
1160 }
1161 pages_to_map = ALIGN(npages, page_align);
1162
1163 xlt = mlx5_ib_create_xlt_wr(mr, &wr, &sg, npages, desc_size, flags);
1164 if (!xlt)
1165 return -ENOMEM;
1166 pages_iter = sg.length / desc_size;
1167 orig_sg_length = sg.length;
1168
1169 if (!(flags & MLX5_IB_UPD_XLT_INDIRECT)) {
1170 struct ib_umem_odp *odp = to_ib_umem_odp(mr->umem);
1171 size_t max_pages = ib_umem_odp_num_pages(odp) - idx;
1172
1173 pages_to_map = min_t(size_t, pages_to_map, max_pages);
1174 }
1175
1176 wr.page_shift = page_shift;
1177
1178 for (pages_mapped = 0;
1179 pages_mapped < pages_to_map && !err;
1180 pages_mapped += pages_iter, idx += pages_iter) {
1181 npages = min_t(int, pages_iter, pages_to_map - pages_mapped);
1182 size_to_map = npages * desc_size;
1183 dma_sync_single_for_cpu(ddev, sg.addr, sg.length,
1184 DMA_TO_DEVICE);
1185 mlx5_odp_populate_xlt(xlt, idx, npages, mr, flags);
1186 dma_sync_single_for_device(ddev, sg.addr, sg.length,
1187 DMA_TO_DEVICE);
1188
1189 sg.length = ALIGN(size_to_map, MLX5_UMR_MTT_ALIGNMENT);
1190
1191 if (pages_mapped + pages_iter >= pages_to_map)
1192 wr.wr.send_flags |= xlt_wr_final_send_flags(flags);
1193
1194 wr.offset = idx * desc_size;
1195 wr.xlt_size = sg.length;
1196
1197 err = mlx5_ib_post_send_wait(dev, &wr);
1198 }
1199 sg.length = orig_sg_length;
1200 mlx5_ib_unmap_free_xlt(dev, xlt, &sg);
1201 return err;
1202 }
1203
1204 /*
1205 * Send the DMA list to the HW for a normal MR using UMR.
1206 * Dmabuf MR is handled in a similar way, except that the MLX5_IB_UPD_XLT_ZAP
1207 * flag may be used.
1208 */
mlx5_ib_update_mr_pas(struct mlx5_ib_mr * mr,unsigned int flags)1209 int mlx5_ib_update_mr_pas(struct mlx5_ib_mr *mr, unsigned int flags)
1210 {
1211 struct mlx5_ib_dev *dev = mr_to_mdev(mr);
1212 struct device *ddev = &dev->mdev->pdev->dev;
1213 struct ib_block_iter biter;
1214 struct mlx5_mtt *cur_mtt;
1215 struct mlx5_umr_wr wr;
1216 size_t orig_sg_length;
1217 struct mlx5_mtt *mtt;
1218 size_t final_size;
1219 struct ib_sge sg;
1220 int err = 0;
1221
1222 if (WARN_ON(mr->umem->is_odp))
1223 return -EINVAL;
1224
1225 mtt = mlx5_ib_create_xlt_wr(mr, &wr, &sg,
1226 ib_umem_num_dma_blocks(mr->umem,
1227 1 << mr->page_shift),
1228 sizeof(*mtt), flags);
1229 if (!mtt)
1230 return -ENOMEM;
1231 orig_sg_length = sg.length;
1232
1233 cur_mtt = mtt;
1234 rdma_for_each_block (mr->umem->sgt_append.sgt.sgl, &biter,
1235 mr->umem->sgt_append.sgt.nents,
1236 BIT(mr->page_shift)) {
1237 if (cur_mtt == (void *)mtt + sg.length) {
1238 dma_sync_single_for_device(ddev, sg.addr, sg.length,
1239 DMA_TO_DEVICE);
1240 err = mlx5_ib_post_send_wait(dev, &wr);
1241 if (err)
1242 goto err;
1243 dma_sync_single_for_cpu(ddev, sg.addr, sg.length,
1244 DMA_TO_DEVICE);
1245 wr.offset += sg.length;
1246 cur_mtt = mtt;
1247 }
1248
1249 cur_mtt->ptag =
1250 cpu_to_be64(rdma_block_iter_dma_address(&biter) |
1251 MLX5_IB_MTT_PRESENT);
1252
1253 if (mr->umem->is_dmabuf && (flags & MLX5_IB_UPD_XLT_ZAP))
1254 cur_mtt->ptag = 0;
1255
1256 cur_mtt++;
1257 }
1258
1259 final_size = (void *)cur_mtt - (void *)mtt;
1260 sg.length = ALIGN(final_size, MLX5_UMR_MTT_ALIGNMENT);
1261 memset(cur_mtt, 0, sg.length - final_size);
1262 wr.wr.send_flags |= xlt_wr_final_send_flags(flags);
1263 wr.xlt_size = sg.length;
1264
1265 dma_sync_single_for_device(ddev, sg.addr, sg.length, DMA_TO_DEVICE);
1266 err = mlx5_ib_post_send_wait(dev, &wr);
1267
1268 err:
1269 sg.length = orig_sg_length;
1270 mlx5_ib_unmap_free_xlt(dev, mtt, &sg);
1271 return err;
1272 }
1273
1274 /*
1275 * If ibmr is NULL it will be allocated by reg_create.
1276 * Else, the given ibmr will be used.
1277 */
reg_create(struct ib_pd * pd,struct ib_umem * umem,u64 iova,int access_flags,unsigned int page_size,bool populate)1278 static struct mlx5_ib_mr *reg_create(struct ib_pd *pd, struct ib_umem *umem,
1279 u64 iova, int access_flags,
1280 unsigned int page_size, bool populate)
1281 {
1282 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1283 struct mlx5_ib_mr *mr;
1284 __be64 *pas;
1285 void *mkc;
1286 int inlen;
1287 u32 *in;
1288 int err;
1289 bool pg_cap = !!(MLX5_CAP_GEN(dev->mdev, pg));
1290
1291 if (!page_size)
1292 return ERR_PTR(-EINVAL);
1293 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1294 if (!mr)
1295 return ERR_PTR(-ENOMEM);
1296
1297 mr->ibmr.pd = pd;
1298 mr->access_flags = access_flags;
1299 mr->page_shift = order_base_2(page_size);
1300
1301 inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
1302 if (populate)
1303 inlen += sizeof(*pas) *
1304 roundup(ib_umem_num_dma_blocks(umem, page_size), 2);
1305 in = kvzalloc(inlen, GFP_KERNEL);
1306 if (!in) {
1307 err = -ENOMEM;
1308 goto err_1;
1309 }
1310 pas = (__be64 *)MLX5_ADDR_OF(create_mkey_in, in, klm_pas_mtt);
1311 if (populate) {
1312 if (WARN_ON(access_flags & IB_ACCESS_ON_DEMAND)) {
1313 err = -EINVAL;
1314 goto err_2;
1315 }
1316 mlx5_ib_populate_pas(umem, 1UL << mr->page_shift, pas,
1317 pg_cap ? MLX5_IB_MTT_PRESENT : 0);
1318 }
1319
1320 /* The pg_access bit allows setting the access flags
1321 * in the page list submitted with the command. */
1322 MLX5_SET(create_mkey_in, in, pg_access, !!(pg_cap));
1323
1324 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
1325 set_mkc_access_pd_addr_fields(mkc, access_flags, iova,
1326 populate ? pd : dev->umrc.pd);
1327 MLX5_SET(mkc, mkc, free, !populate);
1328 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
1329 MLX5_SET(mkc, mkc, umr_en, 1);
1330
1331 MLX5_SET64(mkc, mkc, len, umem->length);
1332 MLX5_SET(mkc, mkc, bsf_octword_size, 0);
1333 MLX5_SET(mkc, mkc, translations_octword_size,
1334 get_octo_len(iova, umem->length, mr->page_shift));
1335 MLX5_SET(mkc, mkc, log_page_size, mr->page_shift);
1336 if (populate) {
1337 MLX5_SET(create_mkey_in, in, translations_octword_actual_size,
1338 get_octo_len(iova, umem->length, mr->page_shift));
1339 }
1340
1341 err = mlx5_ib_create_mkey(dev, &mr->mmkey, in, inlen);
1342 if (err) {
1343 mlx5_ib_warn(dev, "create mkey failed\n");
1344 goto err_2;
1345 }
1346 mr->mmkey.type = MLX5_MKEY_MR;
1347 mr->umem = umem;
1348 set_mr_fields(dev, mr, umem->length, access_flags);
1349 kvfree(in);
1350
1351 mlx5_ib_dbg(dev, "mkey = 0x%x\n", mr->mmkey.key);
1352
1353 return mr;
1354
1355 err_2:
1356 kvfree(in);
1357 err_1:
1358 kfree(mr);
1359 return ERR_PTR(err);
1360 }
1361
mlx5_ib_get_dm_mr(struct ib_pd * pd,u64 start_addr,u64 length,int acc,int mode)1362 static struct ib_mr *mlx5_ib_get_dm_mr(struct ib_pd *pd, u64 start_addr,
1363 u64 length, int acc, int mode)
1364 {
1365 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1366 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
1367 struct mlx5_ib_mr *mr;
1368 void *mkc;
1369 u32 *in;
1370 int err;
1371
1372 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1373 if (!mr)
1374 return ERR_PTR(-ENOMEM);
1375
1376 in = kzalloc(inlen, GFP_KERNEL);
1377 if (!in) {
1378 err = -ENOMEM;
1379 goto err_free;
1380 }
1381
1382 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
1383
1384 MLX5_SET(mkc, mkc, access_mode_1_0, mode & 0x3);
1385 MLX5_SET(mkc, mkc, access_mode_4_2, (mode >> 2) & 0x7);
1386 MLX5_SET64(mkc, mkc, len, length);
1387 set_mkc_access_pd_addr_fields(mkc, acc, start_addr, pd);
1388
1389 err = mlx5_ib_create_mkey(dev, &mr->mmkey, in, inlen);
1390 if (err)
1391 goto err_in;
1392
1393 kfree(in);
1394
1395 set_mr_fields(dev, mr, length, acc);
1396
1397 return &mr->ibmr;
1398
1399 err_in:
1400 kfree(in);
1401
1402 err_free:
1403 kfree(mr);
1404
1405 return ERR_PTR(err);
1406 }
1407
mlx5_ib_advise_mr(struct ib_pd * pd,enum ib_uverbs_advise_mr_advice advice,u32 flags,struct ib_sge * sg_list,u32 num_sge,struct uverbs_attr_bundle * attrs)1408 int mlx5_ib_advise_mr(struct ib_pd *pd,
1409 enum ib_uverbs_advise_mr_advice advice,
1410 u32 flags,
1411 struct ib_sge *sg_list,
1412 u32 num_sge,
1413 struct uverbs_attr_bundle *attrs)
1414 {
1415 if (advice != IB_UVERBS_ADVISE_MR_ADVICE_PREFETCH &&
1416 advice != IB_UVERBS_ADVISE_MR_ADVICE_PREFETCH_WRITE &&
1417 advice != IB_UVERBS_ADVISE_MR_ADVICE_PREFETCH_NO_FAULT)
1418 return -EOPNOTSUPP;
1419
1420 return mlx5_ib_advise_mr_prefetch(pd, advice, flags,
1421 sg_list, num_sge);
1422 }
1423
mlx5_ib_reg_dm_mr(struct ib_pd * pd,struct ib_dm * dm,struct ib_dm_mr_attr * attr,struct uverbs_attr_bundle * attrs)1424 struct ib_mr *mlx5_ib_reg_dm_mr(struct ib_pd *pd, struct ib_dm *dm,
1425 struct ib_dm_mr_attr *attr,
1426 struct uverbs_attr_bundle *attrs)
1427 {
1428 struct mlx5_ib_dm *mdm = to_mdm(dm);
1429 struct mlx5_core_dev *dev = to_mdev(dm->device)->mdev;
1430 u64 start_addr = mdm->dev_addr + attr->offset;
1431 int mode;
1432
1433 switch (mdm->type) {
1434 case MLX5_IB_UAPI_DM_TYPE_MEMIC:
1435 if (attr->access_flags & ~MLX5_IB_DM_MEMIC_ALLOWED_ACCESS)
1436 return ERR_PTR(-EINVAL);
1437
1438 mode = MLX5_MKC_ACCESS_MODE_MEMIC;
1439 start_addr -= pci_resource_start(dev->pdev, 0);
1440 break;
1441 case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
1442 case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
1443 if (attr->access_flags & ~MLX5_IB_DM_SW_ICM_ALLOWED_ACCESS)
1444 return ERR_PTR(-EINVAL);
1445
1446 mode = MLX5_MKC_ACCESS_MODE_SW_ICM;
1447 break;
1448 default:
1449 return ERR_PTR(-EINVAL);
1450 }
1451
1452 return mlx5_ib_get_dm_mr(pd, start_addr, attr->length,
1453 attr->access_flags, mode);
1454 }
1455
create_real_mr(struct ib_pd * pd,struct ib_umem * umem,u64 iova,int access_flags)1456 static struct ib_mr *create_real_mr(struct ib_pd *pd, struct ib_umem *umem,
1457 u64 iova, int access_flags)
1458 {
1459 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1460 struct mlx5_ib_mr *mr = NULL;
1461 bool xlt_with_umr;
1462 int err;
1463
1464 xlt_with_umr = mlx5_ib_can_load_pas_with_umr(dev, umem->length);
1465 if (xlt_with_umr) {
1466 mr = alloc_cacheable_mr(pd, umem, iova, access_flags);
1467 } else {
1468 unsigned int page_size = mlx5_umem_find_best_pgsz(
1469 umem, mkc, log_page_size, 0, iova);
1470
1471 mutex_lock(&dev->slow_path_mutex);
1472 mr = reg_create(pd, umem, iova, access_flags, page_size, true);
1473 mutex_unlock(&dev->slow_path_mutex);
1474 }
1475 if (IS_ERR(mr)) {
1476 ib_umem_release(umem);
1477 return ERR_CAST(mr);
1478 }
1479
1480 mlx5_ib_dbg(dev, "mkey 0x%x\n", mr->mmkey.key);
1481
1482 atomic_add(ib_umem_num_pages(umem), &dev->mdev->priv.reg_pages);
1483
1484 if (xlt_with_umr) {
1485 /*
1486 * If the MR was created with reg_create then it will be
1487 * configured properly but left disabled. It is safe to go ahead
1488 * and configure it again via UMR while enabling it.
1489 */
1490 err = mlx5_ib_update_mr_pas(mr, MLX5_IB_UPD_XLT_ENABLE);
1491 if (err) {
1492 mlx5_ib_dereg_mr(&mr->ibmr, NULL);
1493 return ERR_PTR(err);
1494 }
1495 }
1496 return &mr->ibmr;
1497 }
1498
create_user_odp_mr(struct ib_pd * pd,u64 start,u64 length,u64 iova,int access_flags,struct ib_udata * udata)1499 static struct ib_mr *create_user_odp_mr(struct ib_pd *pd, u64 start, u64 length,
1500 u64 iova, int access_flags,
1501 struct ib_udata *udata)
1502 {
1503 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1504 struct ib_umem_odp *odp;
1505 struct mlx5_ib_mr *mr;
1506 int err;
1507
1508 if (!IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING))
1509 return ERR_PTR(-EOPNOTSUPP);
1510
1511 err = mlx5r_odp_create_eq(dev, &dev->odp_pf_eq);
1512 if (err)
1513 return ERR_PTR(err);
1514 if (!start && length == U64_MAX) {
1515 if (iova != 0)
1516 return ERR_PTR(-EINVAL);
1517 if (!(dev->odp_caps.general_caps & IB_ODP_SUPPORT_IMPLICIT))
1518 return ERR_PTR(-EINVAL);
1519
1520 mr = mlx5_ib_alloc_implicit_mr(to_mpd(pd), access_flags);
1521 if (IS_ERR(mr))
1522 return ERR_CAST(mr);
1523 return &mr->ibmr;
1524 }
1525
1526 /* ODP requires xlt update via umr to work. */
1527 if (!mlx5_ib_can_load_pas_with_umr(dev, length))
1528 return ERR_PTR(-EINVAL);
1529
1530 odp = ib_umem_odp_get(&dev->ib_dev, start, length, access_flags,
1531 &mlx5_mn_ops);
1532 if (IS_ERR(odp))
1533 return ERR_CAST(odp);
1534
1535 mr = alloc_cacheable_mr(pd, &odp->umem, iova, access_flags);
1536 if (IS_ERR(mr)) {
1537 ib_umem_release(&odp->umem);
1538 return ERR_CAST(mr);
1539 }
1540 xa_init(&mr->implicit_children);
1541
1542 odp->private = mr;
1543 err = mlx5r_store_odp_mkey(dev, &mr->mmkey);
1544 if (err)
1545 goto err_dereg_mr;
1546
1547 err = mlx5_ib_init_odp_mr(mr);
1548 if (err)
1549 goto err_dereg_mr;
1550 return &mr->ibmr;
1551
1552 err_dereg_mr:
1553 mlx5_ib_dereg_mr(&mr->ibmr, NULL);
1554 return ERR_PTR(err);
1555 }
1556
mlx5_ib_reg_user_mr(struct ib_pd * pd,u64 start,u64 length,u64 iova,int access_flags,struct ib_udata * udata)1557 struct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
1558 u64 iova, int access_flags,
1559 struct ib_udata *udata)
1560 {
1561 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1562 struct ib_umem *umem;
1563
1564 if (!IS_ENABLED(CONFIG_INFINIBAND_USER_MEM))
1565 return ERR_PTR(-EOPNOTSUPP);
1566
1567 mlx5_ib_dbg(dev, "start 0x%llx, iova 0x%llx, length 0x%llx, access_flags 0x%x\n",
1568 start, iova, length, access_flags);
1569
1570 if (access_flags & IB_ACCESS_ON_DEMAND)
1571 return create_user_odp_mr(pd, start, length, iova, access_flags,
1572 udata);
1573 umem = ib_umem_get(&dev->ib_dev, start, length, access_flags);
1574 if (IS_ERR(umem))
1575 return ERR_CAST(umem);
1576 return create_real_mr(pd, umem, iova, access_flags);
1577 }
1578
mlx5_ib_dmabuf_invalidate_cb(struct dma_buf_attachment * attach)1579 static void mlx5_ib_dmabuf_invalidate_cb(struct dma_buf_attachment *attach)
1580 {
1581 struct ib_umem_dmabuf *umem_dmabuf = attach->importer_priv;
1582 struct mlx5_ib_mr *mr = umem_dmabuf->private;
1583
1584 dma_resv_assert_held(umem_dmabuf->attach->dmabuf->resv);
1585
1586 if (!umem_dmabuf->sgt)
1587 return;
1588
1589 mlx5_ib_update_mr_pas(mr, MLX5_IB_UPD_XLT_ZAP);
1590 ib_umem_dmabuf_unmap_pages(umem_dmabuf);
1591 }
1592
1593 static struct dma_buf_attach_ops mlx5_ib_dmabuf_attach_ops = {
1594 .allow_peer2peer = 1,
1595 .move_notify = mlx5_ib_dmabuf_invalidate_cb,
1596 };
1597
mlx5_ib_reg_user_mr_dmabuf(struct ib_pd * pd,u64 offset,u64 length,u64 virt_addr,int fd,int access_flags,struct ib_udata * udata)1598 struct ib_mr *mlx5_ib_reg_user_mr_dmabuf(struct ib_pd *pd, u64 offset,
1599 u64 length, u64 virt_addr,
1600 int fd, int access_flags,
1601 struct ib_udata *udata)
1602 {
1603 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1604 struct mlx5_ib_mr *mr = NULL;
1605 struct ib_umem_dmabuf *umem_dmabuf;
1606 int err;
1607
1608 if (!IS_ENABLED(CONFIG_INFINIBAND_USER_MEM) ||
1609 !IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING))
1610 return ERR_PTR(-EOPNOTSUPP);
1611
1612 mlx5_ib_dbg(dev,
1613 "offset 0x%llx, virt_addr 0x%llx, length 0x%llx, fd %d, access_flags 0x%x\n",
1614 offset, virt_addr, length, fd, access_flags);
1615
1616 /* dmabuf requires xlt update via umr to work. */
1617 if (!mlx5_ib_can_load_pas_with_umr(dev, length))
1618 return ERR_PTR(-EINVAL);
1619
1620 umem_dmabuf = ib_umem_dmabuf_get(&dev->ib_dev, offset, length, fd,
1621 access_flags,
1622 &mlx5_ib_dmabuf_attach_ops);
1623 if (IS_ERR(umem_dmabuf)) {
1624 mlx5_ib_dbg(dev, "umem_dmabuf get failed (%ld)\n",
1625 PTR_ERR(umem_dmabuf));
1626 return ERR_CAST(umem_dmabuf);
1627 }
1628
1629 mr = alloc_cacheable_mr(pd, &umem_dmabuf->umem, virt_addr,
1630 access_flags);
1631 if (IS_ERR(mr)) {
1632 ib_umem_release(&umem_dmabuf->umem);
1633 return ERR_CAST(mr);
1634 }
1635
1636 mlx5_ib_dbg(dev, "mkey 0x%x\n", mr->mmkey.key);
1637
1638 atomic_add(ib_umem_num_pages(mr->umem), &dev->mdev->priv.reg_pages);
1639 umem_dmabuf->private = mr;
1640 err = mlx5r_store_odp_mkey(dev, &mr->mmkey);
1641 if (err)
1642 goto err_dereg_mr;
1643
1644 err = mlx5_ib_init_dmabuf_mr(mr);
1645 if (err)
1646 goto err_dereg_mr;
1647 return &mr->ibmr;
1648
1649 err_dereg_mr:
1650 mlx5_ib_dereg_mr(&mr->ibmr, NULL);
1651 return ERR_PTR(err);
1652 }
1653
1654 /**
1655 * revoke_mr - Fence all DMA on the MR
1656 * @mr: The MR to fence
1657 *
1658 * Upon return the NIC will not be doing any DMA to the pages under the MR,
1659 * and any DMA in progress will be completed. Failure of this function
1660 * indicates the HW has failed catastrophically.
1661 */
revoke_mr(struct mlx5_ib_mr * mr)1662 static int revoke_mr(struct mlx5_ib_mr *mr)
1663 {
1664 struct mlx5_umr_wr umrwr = {};
1665
1666 if (mr_to_mdev(mr)->mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR)
1667 return 0;
1668
1669 umrwr.wr.send_flags = MLX5_IB_SEND_UMR_DISABLE_MR |
1670 MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS;
1671 umrwr.wr.opcode = MLX5_IB_WR_UMR;
1672 umrwr.pd = mr_to_mdev(mr)->umrc.pd;
1673 umrwr.mkey = mr->mmkey.key;
1674 umrwr.ignore_free_state = 1;
1675
1676 return mlx5_ib_post_send_wait(mr_to_mdev(mr), &umrwr);
1677 }
1678
1679 /*
1680 * True if the change in access flags can be done via UMR, only some access
1681 * flags can be updated.
1682 */
can_use_umr_rereg_access(struct mlx5_ib_dev * dev,unsigned int current_access_flags,unsigned int target_access_flags)1683 static bool can_use_umr_rereg_access(struct mlx5_ib_dev *dev,
1684 unsigned int current_access_flags,
1685 unsigned int target_access_flags)
1686 {
1687 unsigned int diffs = current_access_flags ^ target_access_flags;
1688
1689 if (diffs & ~(IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_WRITE |
1690 IB_ACCESS_REMOTE_READ | IB_ACCESS_RELAXED_ORDERING))
1691 return false;
1692 return mlx5_ib_can_reconfig_with_umr(dev, current_access_flags,
1693 target_access_flags);
1694 }
1695
umr_rereg_pd_access(struct mlx5_ib_mr * mr,struct ib_pd * pd,int access_flags)1696 static int umr_rereg_pd_access(struct mlx5_ib_mr *mr, struct ib_pd *pd,
1697 int access_flags)
1698 {
1699 struct mlx5_ib_dev *dev = to_mdev(mr->ibmr.device);
1700 struct mlx5_umr_wr umrwr = {
1701 .wr = {
1702 .send_flags = MLX5_IB_SEND_UMR_FAIL_IF_FREE |
1703 MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS,
1704 .opcode = MLX5_IB_WR_UMR,
1705 },
1706 .mkey = mr->mmkey.key,
1707 .pd = pd,
1708 .access_flags = access_flags,
1709 };
1710 int err;
1711
1712 err = mlx5_ib_post_send_wait(dev, &umrwr);
1713 if (err)
1714 return err;
1715
1716 mr->access_flags = access_flags;
1717 mr->mmkey.pd = to_mpd(pd)->pdn;
1718 return 0;
1719 }
1720
can_use_umr_rereg_pas(struct mlx5_ib_mr * mr,struct ib_umem * new_umem,int new_access_flags,u64 iova,unsigned long * page_size)1721 static bool can_use_umr_rereg_pas(struct mlx5_ib_mr *mr,
1722 struct ib_umem *new_umem,
1723 int new_access_flags, u64 iova,
1724 unsigned long *page_size)
1725 {
1726 struct mlx5_ib_dev *dev = to_mdev(mr->ibmr.device);
1727
1728 /* We only track the allocated sizes of MRs from the cache */
1729 if (!mr->cache_ent)
1730 return false;
1731 if (!mlx5_ib_can_load_pas_with_umr(dev, new_umem->length))
1732 return false;
1733
1734 *page_size =
1735 mlx5_umem_find_best_pgsz(new_umem, mkc, log_page_size, 0, iova);
1736 if (WARN_ON(!*page_size))
1737 return false;
1738 return (1ULL << mr->cache_ent->order) >=
1739 ib_umem_num_dma_blocks(new_umem, *page_size);
1740 }
1741
umr_rereg_pas(struct mlx5_ib_mr * mr,struct ib_pd * pd,int access_flags,int flags,struct ib_umem * new_umem,u64 iova,unsigned long page_size)1742 static int umr_rereg_pas(struct mlx5_ib_mr *mr, struct ib_pd *pd,
1743 int access_flags, int flags, struct ib_umem *new_umem,
1744 u64 iova, unsigned long page_size)
1745 {
1746 struct mlx5_ib_dev *dev = to_mdev(mr->ibmr.device);
1747 int upd_flags = MLX5_IB_UPD_XLT_ADDR | MLX5_IB_UPD_XLT_ENABLE;
1748 struct ib_umem *old_umem = mr->umem;
1749 int err;
1750
1751 /*
1752 * To keep everything simple the MR is revoked before we start to mess
1753 * with it. This ensure the change is atomic relative to any use of the
1754 * MR.
1755 */
1756 err = revoke_mr(mr);
1757 if (err)
1758 return err;
1759
1760 if (flags & IB_MR_REREG_PD) {
1761 mr->ibmr.pd = pd;
1762 mr->mmkey.pd = to_mpd(pd)->pdn;
1763 upd_flags |= MLX5_IB_UPD_XLT_PD;
1764 }
1765 if (flags & IB_MR_REREG_ACCESS) {
1766 mr->access_flags = access_flags;
1767 upd_flags |= MLX5_IB_UPD_XLT_ACCESS;
1768 }
1769
1770 mr->ibmr.length = new_umem->length;
1771 mr->mmkey.iova = iova;
1772 mr->mmkey.size = new_umem->length;
1773 mr->page_shift = order_base_2(page_size);
1774 mr->umem = new_umem;
1775 err = mlx5_ib_update_mr_pas(mr, upd_flags);
1776 if (err) {
1777 /*
1778 * The MR is revoked at this point so there is no issue to free
1779 * new_umem.
1780 */
1781 mr->umem = old_umem;
1782 return err;
1783 }
1784
1785 atomic_sub(ib_umem_num_pages(old_umem), &dev->mdev->priv.reg_pages);
1786 ib_umem_release(old_umem);
1787 atomic_add(ib_umem_num_pages(new_umem), &dev->mdev->priv.reg_pages);
1788 return 0;
1789 }
1790
mlx5_ib_rereg_user_mr(struct ib_mr * ib_mr,int flags,u64 start,u64 length,u64 iova,int new_access_flags,struct ib_pd * new_pd,struct ib_udata * udata)1791 struct ib_mr *mlx5_ib_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start,
1792 u64 length, u64 iova, int new_access_flags,
1793 struct ib_pd *new_pd,
1794 struct ib_udata *udata)
1795 {
1796 struct mlx5_ib_dev *dev = to_mdev(ib_mr->device);
1797 struct mlx5_ib_mr *mr = to_mmr(ib_mr);
1798 int err;
1799
1800 if (!IS_ENABLED(CONFIG_INFINIBAND_USER_MEM))
1801 return ERR_PTR(-EOPNOTSUPP);
1802
1803 mlx5_ib_dbg(
1804 dev,
1805 "start 0x%llx, iova 0x%llx, length 0x%llx, access_flags 0x%x\n",
1806 start, iova, length, new_access_flags);
1807
1808 if (flags & ~(IB_MR_REREG_TRANS | IB_MR_REREG_PD | IB_MR_REREG_ACCESS))
1809 return ERR_PTR(-EOPNOTSUPP);
1810
1811 if (!(flags & IB_MR_REREG_ACCESS))
1812 new_access_flags = mr->access_flags;
1813 if (!(flags & IB_MR_REREG_PD))
1814 new_pd = ib_mr->pd;
1815
1816 if (!(flags & IB_MR_REREG_TRANS)) {
1817 struct ib_umem *umem;
1818
1819 /* Fast path for PD/access change */
1820 if (can_use_umr_rereg_access(dev, mr->access_flags,
1821 new_access_flags)) {
1822 err = umr_rereg_pd_access(mr, new_pd, new_access_flags);
1823 if (err)
1824 return ERR_PTR(err);
1825 return NULL;
1826 }
1827 /* DM or ODP MR's don't have a normal umem so we can't re-use it */
1828 if (!mr->umem || is_odp_mr(mr) || is_dmabuf_mr(mr))
1829 goto recreate;
1830
1831 /*
1832 * Only one active MR can refer to a umem at one time, revoke
1833 * the old MR before assigning the umem to the new one.
1834 */
1835 err = revoke_mr(mr);
1836 if (err)
1837 return ERR_PTR(err);
1838 umem = mr->umem;
1839 mr->umem = NULL;
1840 atomic_sub(ib_umem_num_pages(umem), &dev->mdev->priv.reg_pages);
1841
1842 return create_real_mr(new_pd, umem, mr->mmkey.iova,
1843 new_access_flags);
1844 }
1845
1846 /*
1847 * DM doesn't have a PAS list so we can't re-use it, odp/dmabuf does
1848 * but the logic around releasing the umem is different
1849 */
1850 if (!mr->umem || is_odp_mr(mr) || is_dmabuf_mr(mr))
1851 goto recreate;
1852
1853 if (!(new_access_flags & IB_ACCESS_ON_DEMAND) &&
1854 can_use_umr_rereg_access(dev, mr->access_flags, new_access_flags)) {
1855 struct ib_umem *new_umem;
1856 unsigned long page_size;
1857
1858 new_umem = ib_umem_get(&dev->ib_dev, start, length,
1859 new_access_flags);
1860 if (IS_ERR(new_umem))
1861 return ERR_CAST(new_umem);
1862
1863 /* Fast path for PAS change */
1864 if (can_use_umr_rereg_pas(mr, new_umem, new_access_flags, iova,
1865 &page_size)) {
1866 err = umr_rereg_pas(mr, new_pd, new_access_flags, flags,
1867 new_umem, iova, page_size);
1868 if (err) {
1869 ib_umem_release(new_umem);
1870 return ERR_PTR(err);
1871 }
1872 return NULL;
1873 }
1874 return create_real_mr(new_pd, new_umem, iova, new_access_flags);
1875 }
1876
1877 /*
1878 * Everything else has no state we can preserve, just create a new MR
1879 * from scratch
1880 */
1881 recreate:
1882 return mlx5_ib_reg_user_mr(new_pd, start, length, iova,
1883 new_access_flags, udata);
1884 }
1885
1886 static int
mlx5_alloc_priv_descs(struct ib_device * device,struct mlx5_ib_mr * mr,int ndescs,int desc_size)1887 mlx5_alloc_priv_descs(struct ib_device *device,
1888 struct mlx5_ib_mr *mr,
1889 int ndescs,
1890 int desc_size)
1891 {
1892 struct mlx5_ib_dev *dev = to_mdev(device);
1893 struct device *ddev = &dev->mdev->pdev->dev;
1894 int size = ndescs * desc_size;
1895 int add_size;
1896 int ret;
1897
1898 add_size = max_t(int, MLX5_UMR_ALIGN - ARCH_KMALLOC_MINALIGN, 0);
1899
1900 mr->descs_alloc = kzalloc(size + add_size, GFP_KERNEL);
1901 if (!mr->descs_alloc)
1902 return -ENOMEM;
1903
1904 mr->descs = PTR_ALIGN(mr->descs_alloc, MLX5_UMR_ALIGN);
1905
1906 mr->desc_map = dma_map_single(ddev, mr->descs, size, DMA_TO_DEVICE);
1907 if (dma_mapping_error(ddev, mr->desc_map)) {
1908 ret = -ENOMEM;
1909 goto err;
1910 }
1911
1912 return 0;
1913 err:
1914 kfree(mr->descs_alloc);
1915
1916 return ret;
1917 }
1918
1919 static void
mlx5_free_priv_descs(struct mlx5_ib_mr * mr)1920 mlx5_free_priv_descs(struct mlx5_ib_mr *mr)
1921 {
1922 if (!mr->umem && mr->descs) {
1923 struct ib_device *device = mr->ibmr.device;
1924 int size = mr->max_descs * mr->desc_size;
1925 struct mlx5_ib_dev *dev = to_mdev(device);
1926
1927 dma_unmap_single(&dev->mdev->pdev->dev, mr->desc_map, size,
1928 DMA_TO_DEVICE);
1929 kfree(mr->descs_alloc);
1930 mr->descs = NULL;
1931 }
1932 }
1933
mlx5_ib_dereg_mr(struct ib_mr * ibmr,struct ib_udata * udata)1934 int mlx5_ib_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata)
1935 {
1936 struct mlx5_ib_mr *mr = to_mmr(ibmr);
1937 struct mlx5_ib_dev *dev = to_mdev(ibmr->device);
1938 int rc;
1939
1940 /*
1941 * Any async use of the mr must hold the refcount, once the refcount
1942 * goes to zero no other thread, such as ODP page faults, prefetch, any
1943 * UMR activity, etc can touch the mkey. Thus it is safe to destroy it.
1944 */
1945 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING) &&
1946 refcount_read(&mr->mmkey.usecount) != 0 &&
1947 xa_erase(&mr_to_mdev(mr)->odp_mkeys, mlx5_base_mkey(mr->mmkey.key)))
1948 mlx5r_deref_wait_odp_mkey(&mr->mmkey);
1949
1950 if (ibmr->type == IB_MR_TYPE_INTEGRITY) {
1951 xa_cmpxchg(&dev->sig_mrs, mlx5_base_mkey(mr->mmkey.key),
1952 mr->sig, NULL, GFP_KERNEL);
1953
1954 if (mr->mtt_mr) {
1955 rc = mlx5_ib_dereg_mr(&mr->mtt_mr->ibmr, NULL);
1956 if (rc)
1957 return rc;
1958 mr->mtt_mr = NULL;
1959 }
1960 if (mr->klm_mr) {
1961 rc = mlx5_ib_dereg_mr(&mr->klm_mr->ibmr, NULL);
1962 if (rc)
1963 return rc;
1964 mr->klm_mr = NULL;
1965 }
1966
1967 if (mlx5_core_destroy_psv(dev->mdev,
1968 mr->sig->psv_memory.psv_idx))
1969 mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
1970 mr->sig->psv_memory.psv_idx);
1971 if (mlx5_core_destroy_psv(dev->mdev, mr->sig->psv_wire.psv_idx))
1972 mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
1973 mr->sig->psv_wire.psv_idx);
1974 kfree(mr->sig);
1975 mr->sig = NULL;
1976 }
1977
1978 /* Stop DMA */
1979 if (mr->cache_ent) {
1980 if (revoke_mr(mr)) {
1981 spin_lock_irq(&mr->cache_ent->lock);
1982 mr->cache_ent->total_mrs--;
1983 spin_unlock_irq(&mr->cache_ent->lock);
1984 mr->cache_ent = NULL;
1985 }
1986 }
1987 if (!mr->cache_ent) {
1988 rc = destroy_mkey(to_mdev(mr->ibmr.device), mr);
1989 if (rc)
1990 return rc;
1991 }
1992
1993 if (mr->umem) {
1994 bool is_odp = is_odp_mr(mr);
1995
1996 if (!is_odp)
1997 atomic_sub(ib_umem_num_pages(mr->umem),
1998 &dev->mdev->priv.reg_pages);
1999 ib_umem_release(mr->umem);
2000 if (is_odp)
2001 mlx5_ib_free_odp_mr(mr);
2002 }
2003
2004 if (mr->cache_ent) {
2005 mlx5_mr_cache_free(dev, mr);
2006 } else {
2007 mlx5_free_priv_descs(mr);
2008 kfree(mr);
2009 }
2010 return 0;
2011 }
2012
mlx5_set_umr_free_mkey(struct ib_pd * pd,u32 * in,int ndescs,int access_mode,int page_shift)2013 static void mlx5_set_umr_free_mkey(struct ib_pd *pd, u32 *in, int ndescs,
2014 int access_mode, int page_shift)
2015 {
2016 void *mkc;
2017
2018 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
2019
2020 /* This is only used from the kernel, so setting the PD is OK. */
2021 set_mkc_access_pd_addr_fields(mkc, IB_ACCESS_RELAXED_ORDERING, 0, pd);
2022 MLX5_SET(mkc, mkc, free, 1);
2023 MLX5_SET(mkc, mkc, translations_octword_size, ndescs);
2024 MLX5_SET(mkc, mkc, access_mode_1_0, access_mode & 0x3);
2025 MLX5_SET(mkc, mkc, access_mode_4_2, (access_mode >> 2) & 0x7);
2026 MLX5_SET(mkc, mkc, umr_en, 1);
2027 MLX5_SET(mkc, mkc, log_page_size, page_shift);
2028 }
2029
_mlx5_alloc_mkey_descs(struct ib_pd * pd,struct mlx5_ib_mr * mr,int ndescs,int desc_size,int page_shift,int access_mode,u32 * in,int inlen)2030 static int _mlx5_alloc_mkey_descs(struct ib_pd *pd, struct mlx5_ib_mr *mr,
2031 int ndescs, int desc_size, int page_shift,
2032 int access_mode, u32 *in, int inlen)
2033 {
2034 struct mlx5_ib_dev *dev = to_mdev(pd->device);
2035 int err;
2036
2037 mr->access_mode = access_mode;
2038 mr->desc_size = desc_size;
2039 mr->max_descs = ndescs;
2040
2041 err = mlx5_alloc_priv_descs(pd->device, mr, ndescs, desc_size);
2042 if (err)
2043 return err;
2044
2045 mlx5_set_umr_free_mkey(pd, in, ndescs, access_mode, page_shift);
2046
2047 err = mlx5_ib_create_mkey(dev, &mr->mmkey, in, inlen);
2048 if (err)
2049 goto err_free_descs;
2050
2051 mr->mmkey.type = MLX5_MKEY_MR;
2052 mr->ibmr.lkey = mr->mmkey.key;
2053 mr->ibmr.rkey = mr->mmkey.key;
2054
2055 return 0;
2056
2057 err_free_descs:
2058 mlx5_free_priv_descs(mr);
2059 return err;
2060 }
2061
mlx5_ib_alloc_pi_mr(struct ib_pd * pd,u32 max_num_sg,u32 max_num_meta_sg,int desc_size,int access_mode)2062 static struct mlx5_ib_mr *mlx5_ib_alloc_pi_mr(struct ib_pd *pd,
2063 u32 max_num_sg, u32 max_num_meta_sg,
2064 int desc_size, int access_mode)
2065 {
2066 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
2067 int ndescs = ALIGN(max_num_sg + max_num_meta_sg, 4);
2068 int page_shift = 0;
2069 struct mlx5_ib_mr *mr;
2070 u32 *in;
2071 int err;
2072
2073 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
2074 if (!mr)
2075 return ERR_PTR(-ENOMEM);
2076
2077 mr->ibmr.pd = pd;
2078 mr->ibmr.device = pd->device;
2079
2080 in = kzalloc(inlen, GFP_KERNEL);
2081 if (!in) {
2082 err = -ENOMEM;
2083 goto err_free;
2084 }
2085
2086 if (access_mode == MLX5_MKC_ACCESS_MODE_MTT)
2087 page_shift = PAGE_SHIFT;
2088
2089 err = _mlx5_alloc_mkey_descs(pd, mr, ndescs, desc_size, page_shift,
2090 access_mode, in, inlen);
2091 if (err)
2092 goto err_free_in;
2093
2094 mr->umem = NULL;
2095 kfree(in);
2096
2097 return mr;
2098
2099 err_free_in:
2100 kfree(in);
2101 err_free:
2102 kfree(mr);
2103 return ERR_PTR(err);
2104 }
2105
mlx5_alloc_mem_reg_descs(struct ib_pd * pd,struct mlx5_ib_mr * mr,int ndescs,u32 * in,int inlen)2106 static int mlx5_alloc_mem_reg_descs(struct ib_pd *pd, struct mlx5_ib_mr *mr,
2107 int ndescs, u32 *in, int inlen)
2108 {
2109 return _mlx5_alloc_mkey_descs(pd, mr, ndescs, sizeof(struct mlx5_mtt),
2110 PAGE_SHIFT, MLX5_MKC_ACCESS_MODE_MTT, in,
2111 inlen);
2112 }
2113
mlx5_alloc_sg_gaps_descs(struct ib_pd * pd,struct mlx5_ib_mr * mr,int ndescs,u32 * in,int inlen)2114 static int mlx5_alloc_sg_gaps_descs(struct ib_pd *pd, struct mlx5_ib_mr *mr,
2115 int ndescs, u32 *in, int inlen)
2116 {
2117 return _mlx5_alloc_mkey_descs(pd, mr, ndescs, sizeof(struct mlx5_klm),
2118 0, MLX5_MKC_ACCESS_MODE_KLMS, in, inlen);
2119 }
2120
mlx5_alloc_integrity_descs(struct ib_pd * pd,struct mlx5_ib_mr * mr,int max_num_sg,int max_num_meta_sg,u32 * in,int inlen)2121 static int mlx5_alloc_integrity_descs(struct ib_pd *pd, struct mlx5_ib_mr *mr,
2122 int max_num_sg, int max_num_meta_sg,
2123 u32 *in, int inlen)
2124 {
2125 struct mlx5_ib_dev *dev = to_mdev(pd->device);
2126 u32 psv_index[2];
2127 void *mkc;
2128 int err;
2129
2130 mr->sig = kzalloc(sizeof(*mr->sig), GFP_KERNEL);
2131 if (!mr->sig)
2132 return -ENOMEM;
2133
2134 /* create mem & wire PSVs */
2135 err = mlx5_core_create_psv(dev->mdev, to_mpd(pd)->pdn, 2, psv_index);
2136 if (err)
2137 goto err_free_sig;
2138
2139 mr->sig->psv_memory.psv_idx = psv_index[0];
2140 mr->sig->psv_wire.psv_idx = psv_index[1];
2141
2142 mr->sig->sig_status_checked = true;
2143 mr->sig->sig_err_exists = false;
2144 /* Next UMR, Arm SIGERR */
2145 ++mr->sig->sigerr_count;
2146 mr->klm_mr = mlx5_ib_alloc_pi_mr(pd, max_num_sg, max_num_meta_sg,
2147 sizeof(struct mlx5_klm),
2148 MLX5_MKC_ACCESS_MODE_KLMS);
2149 if (IS_ERR(mr->klm_mr)) {
2150 err = PTR_ERR(mr->klm_mr);
2151 goto err_destroy_psv;
2152 }
2153 mr->mtt_mr = mlx5_ib_alloc_pi_mr(pd, max_num_sg, max_num_meta_sg,
2154 sizeof(struct mlx5_mtt),
2155 MLX5_MKC_ACCESS_MODE_MTT);
2156 if (IS_ERR(mr->mtt_mr)) {
2157 err = PTR_ERR(mr->mtt_mr);
2158 goto err_free_klm_mr;
2159 }
2160
2161 /* Set bsf descriptors for mkey */
2162 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
2163 MLX5_SET(mkc, mkc, bsf_en, 1);
2164 MLX5_SET(mkc, mkc, bsf_octword_size, MLX5_MKEY_BSF_OCTO_SIZE);
2165
2166 err = _mlx5_alloc_mkey_descs(pd, mr, 4, sizeof(struct mlx5_klm), 0,
2167 MLX5_MKC_ACCESS_MODE_KLMS, in, inlen);
2168 if (err)
2169 goto err_free_mtt_mr;
2170
2171 err = xa_err(xa_store(&dev->sig_mrs, mlx5_base_mkey(mr->mmkey.key),
2172 mr->sig, GFP_KERNEL));
2173 if (err)
2174 goto err_free_descs;
2175 return 0;
2176
2177 err_free_descs:
2178 destroy_mkey(dev, mr);
2179 mlx5_free_priv_descs(mr);
2180 err_free_mtt_mr:
2181 mlx5_ib_dereg_mr(&mr->mtt_mr->ibmr, NULL);
2182 mr->mtt_mr = NULL;
2183 err_free_klm_mr:
2184 mlx5_ib_dereg_mr(&mr->klm_mr->ibmr, NULL);
2185 mr->klm_mr = NULL;
2186 err_destroy_psv:
2187 if (mlx5_core_destroy_psv(dev->mdev, mr->sig->psv_memory.psv_idx))
2188 mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
2189 mr->sig->psv_memory.psv_idx);
2190 if (mlx5_core_destroy_psv(dev->mdev, mr->sig->psv_wire.psv_idx))
2191 mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
2192 mr->sig->psv_wire.psv_idx);
2193 err_free_sig:
2194 kfree(mr->sig);
2195
2196 return err;
2197 }
2198
__mlx5_ib_alloc_mr(struct ib_pd * pd,enum ib_mr_type mr_type,u32 max_num_sg,u32 max_num_meta_sg)2199 static struct ib_mr *__mlx5_ib_alloc_mr(struct ib_pd *pd,
2200 enum ib_mr_type mr_type, u32 max_num_sg,
2201 u32 max_num_meta_sg)
2202 {
2203 struct mlx5_ib_dev *dev = to_mdev(pd->device);
2204 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
2205 int ndescs = ALIGN(max_num_sg, 4);
2206 struct mlx5_ib_mr *mr;
2207 u32 *in;
2208 int err;
2209
2210 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
2211 if (!mr)
2212 return ERR_PTR(-ENOMEM);
2213
2214 in = kzalloc(inlen, GFP_KERNEL);
2215 if (!in) {
2216 err = -ENOMEM;
2217 goto err_free;
2218 }
2219
2220 mr->ibmr.device = pd->device;
2221 mr->umem = NULL;
2222
2223 switch (mr_type) {
2224 case IB_MR_TYPE_MEM_REG:
2225 err = mlx5_alloc_mem_reg_descs(pd, mr, ndescs, in, inlen);
2226 break;
2227 case IB_MR_TYPE_SG_GAPS:
2228 err = mlx5_alloc_sg_gaps_descs(pd, mr, ndescs, in, inlen);
2229 break;
2230 case IB_MR_TYPE_INTEGRITY:
2231 err = mlx5_alloc_integrity_descs(pd, mr, max_num_sg,
2232 max_num_meta_sg, in, inlen);
2233 break;
2234 default:
2235 mlx5_ib_warn(dev, "Invalid mr type %d\n", mr_type);
2236 err = -EINVAL;
2237 }
2238
2239 if (err)
2240 goto err_free_in;
2241
2242 kfree(in);
2243
2244 return &mr->ibmr;
2245
2246 err_free_in:
2247 kfree(in);
2248 err_free:
2249 kfree(mr);
2250 return ERR_PTR(err);
2251 }
2252
mlx5_ib_alloc_mr(struct ib_pd * pd,enum ib_mr_type mr_type,u32 max_num_sg)2253 struct ib_mr *mlx5_ib_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
2254 u32 max_num_sg)
2255 {
2256 return __mlx5_ib_alloc_mr(pd, mr_type, max_num_sg, 0);
2257 }
2258
mlx5_ib_alloc_mr_integrity(struct ib_pd * pd,u32 max_num_sg,u32 max_num_meta_sg)2259 struct ib_mr *mlx5_ib_alloc_mr_integrity(struct ib_pd *pd,
2260 u32 max_num_sg, u32 max_num_meta_sg)
2261 {
2262 return __mlx5_ib_alloc_mr(pd, IB_MR_TYPE_INTEGRITY, max_num_sg,
2263 max_num_meta_sg);
2264 }
2265
mlx5_ib_alloc_mw(struct ib_mw * ibmw,struct ib_udata * udata)2266 int mlx5_ib_alloc_mw(struct ib_mw *ibmw, struct ib_udata *udata)
2267 {
2268 struct mlx5_ib_dev *dev = to_mdev(ibmw->device);
2269 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
2270 struct mlx5_ib_mw *mw = to_mmw(ibmw);
2271 u32 *in = NULL;
2272 void *mkc;
2273 int ndescs;
2274 int err;
2275 struct mlx5_ib_alloc_mw req = {};
2276 struct {
2277 __u32 comp_mask;
2278 __u32 response_length;
2279 } resp = {};
2280
2281 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
2282 if (err)
2283 return err;
2284
2285 if (req.comp_mask || req.reserved1 || req.reserved2)
2286 return -EOPNOTSUPP;
2287
2288 if (udata->inlen > sizeof(req) &&
2289 !ib_is_udata_cleared(udata, sizeof(req),
2290 udata->inlen - sizeof(req)))
2291 return -EOPNOTSUPP;
2292
2293 ndescs = req.num_klms ? roundup(req.num_klms, 4) : roundup(1, 4);
2294
2295 in = kzalloc(inlen, GFP_KERNEL);
2296 if (!in) {
2297 err = -ENOMEM;
2298 goto free;
2299 }
2300
2301 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
2302
2303 MLX5_SET(mkc, mkc, free, 1);
2304 MLX5_SET(mkc, mkc, translations_octword_size, ndescs);
2305 MLX5_SET(mkc, mkc, pd, to_mpd(ibmw->pd)->pdn);
2306 MLX5_SET(mkc, mkc, umr_en, 1);
2307 MLX5_SET(mkc, mkc, lr, 1);
2308 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_KLMS);
2309 MLX5_SET(mkc, mkc, en_rinval, !!((ibmw->type == IB_MW_TYPE_2)));
2310 MLX5_SET(mkc, mkc, qpn, 0xffffff);
2311
2312 err = mlx5_ib_create_mkey(dev, &mw->mmkey, in, inlen);
2313 if (err)
2314 goto free;
2315
2316 mw->mmkey.type = MLX5_MKEY_MW;
2317 ibmw->rkey = mw->mmkey.key;
2318 mw->ndescs = ndescs;
2319
2320 resp.response_length =
2321 min(offsetofend(typeof(resp), response_length), udata->outlen);
2322 if (resp.response_length) {
2323 err = ib_copy_to_udata(udata, &resp, resp.response_length);
2324 if (err)
2325 goto free_mkey;
2326 }
2327
2328 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
2329 err = mlx5r_store_odp_mkey(dev, &mw->mmkey);
2330 if (err)
2331 goto free_mkey;
2332 }
2333
2334 kfree(in);
2335 return 0;
2336
2337 free_mkey:
2338 mlx5_core_destroy_mkey(dev->mdev, &mw->mmkey);
2339 free:
2340 kfree(in);
2341 return err;
2342 }
2343
mlx5_ib_dealloc_mw(struct ib_mw * mw)2344 int mlx5_ib_dealloc_mw(struct ib_mw *mw)
2345 {
2346 struct mlx5_ib_dev *dev = to_mdev(mw->device);
2347 struct mlx5_ib_mw *mmw = to_mmw(mw);
2348
2349 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING) &&
2350 xa_erase(&dev->odp_mkeys, mlx5_base_mkey(mmw->mmkey.key)))
2351 /*
2352 * pagefault_single_data_segment() may be accessing mmw
2353 * if the user bound an ODP MR to this MW.
2354 */
2355 mlx5r_deref_wait_odp_mkey(&mmw->mmkey);
2356
2357 return mlx5_core_destroy_mkey(dev->mdev, &mmw->mmkey);
2358 }
2359
mlx5_ib_check_mr_status(struct ib_mr * ibmr,u32 check_mask,struct ib_mr_status * mr_status)2360 int mlx5_ib_check_mr_status(struct ib_mr *ibmr, u32 check_mask,
2361 struct ib_mr_status *mr_status)
2362 {
2363 struct mlx5_ib_mr *mmr = to_mmr(ibmr);
2364 int ret = 0;
2365
2366 if (check_mask & ~IB_MR_CHECK_SIG_STATUS) {
2367 pr_err("Invalid status check mask\n");
2368 ret = -EINVAL;
2369 goto done;
2370 }
2371
2372 mr_status->fail_status = 0;
2373 if (check_mask & IB_MR_CHECK_SIG_STATUS) {
2374 if (!mmr->sig) {
2375 ret = -EINVAL;
2376 pr_err("signature status check requested on a non-signature enabled MR\n");
2377 goto done;
2378 }
2379
2380 mmr->sig->sig_status_checked = true;
2381 if (!mmr->sig->sig_err_exists)
2382 goto done;
2383
2384 if (ibmr->lkey == mmr->sig->err_item.key)
2385 memcpy(&mr_status->sig_err, &mmr->sig->err_item,
2386 sizeof(mr_status->sig_err));
2387 else {
2388 mr_status->sig_err.err_type = IB_SIG_BAD_GUARD;
2389 mr_status->sig_err.sig_err_offset = 0;
2390 mr_status->sig_err.key = mmr->sig->err_item.key;
2391 }
2392
2393 mmr->sig->sig_err_exists = false;
2394 mr_status->fail_status |= IB_MR_CHECK_SIG_STATUS;
2395 }
2396
2397 done:
2398 return ret;
2399 }
2400
2401 static int
mlx5_ib_map_pa_mr_sg_pi(struct ib_mr * ibmr,struct scatterlist * data_sg,int data_sg_nents,unsigned int * data_sg_offset,struct scatterlist * meta_sg,int meta_sg_nents,unsigned int * meta_sg_offset)2402 mlx5_ib_map_pa_mr_sg_pi(struct ib_mr *ibmr, struct scatterlist *data_sg,
2403 int data_sg_nents, unsigned int *data_sg_offset,
2404 struct scatterlist *meta_sg, int meta_sg_nents,
2405 unsigned int *meta_sg_offset)
2406 {
2407 struct mlx5_ib_mr *mr = to_mmr(ibmr);
2408 unsigned int sg_offset = 0;
2409 int n = 0;
2410
2411 mr->meta_length = 0;
2412 if (data_sg_nents == 1) {
2413 n++;
2414 mr->ndescs = 1;
2415 if (data_sg_offset)
2416 sg_offset = *data_sg_offset;
2417 mr->data_length = sg_dma_len(data_sg) - sg_offset;
2418 mr->data_iova = sg_dma_address(data_sg) + sg_offset;
2419 if (meta_sg_nents == 1) {
2420 n++;
2421 mr->meta_ndescs = 1;
2422 if (meta_sg_offset)
2423 sg_offset = *meta_sg_offset;
2424 else
2425 sg_offset = 0;
2426 mr->meta_length = sg_dma_len(meta_sg) - sg_offset;
2427 mr->pi_iova = sg_dma_address(meta_sg) + sg_offset;
2428 }
2429 ibmr->length = mr->data_length + mr->meta_length;
2430 }
2431
2432 return n;
2433 }
2434
2435 static int
mlx5_ib_sg_to_klms(struct mlx5_ib_mr * mr,struct scatterlist * sgl,unsigned short sg_nents,unsigned int * sg_offset_p,struct scatterlist * meta_sgl,unsigned short meta_sg_nents,unsigned int * meta_sg_offset_p)2436 mlx5_ib_sg_to_klms(struct mlx5_ib_mr *mr,
2437 struct scatterlist *sgl,
2438 unsigned short sg_nents,
2439 unsigned int *sg_offset_p,
2440 struct scatterlist *meta_sgl,
2441 unsigned short meta_sg_nents,
2442 unsigned int *meta_sg_offset_p)
2443 {
2444 struct scatterlist *sg = sgl;
2445 struct mlx5_klm *klms = mr->descs;
2446 unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
2447 u32 lkey = mr->ibmr.pd->local_dma_lkey;
2448 int i, j = 0;
2449
2450 mr->ibmr.iova = sg_dma_address(sg) + sg_offset;
2451 mr->ibmr.length = 0;
2452
2453 for_each_sg(sgl, sg, sg_nents, i) {
2454 if (unlikely(i >= mr->max_descs))
2455 break;
2456 klms[i].va = cpu_to_be64(sg_dma_address(sg) + sg_offset);
2457 klms[i].bcount = cpu_to_be32(sg_dma_len(sg) - sg_offset);
2458 klms[i].key = cpu_to_be32(lkey);
2459 mr->ibmr.length += sg_dma_len(sg) - sg_offset;
2460
2461 sg_offset = 0;
2462 }
2463
2464 if (sg_offset_p)
2465 *sg_offset_p = sg_offset;
2466
2467 mr->ndescs = i;
2468 mr->data_length = mr->ibmr.length;
2469
2470 if (meta_sg_nents) {
2471 sg = meta_sgl;
2472 sg_offset = meta_sg_offset_p ? *meta_sg_offset_p : 0;
2473 for_each_sg(meta_sgl, sg, meta_sg_nents, j) {
2474 if (unlikely(i + j >= mr->max_descs))
2475 break;
2476 klms[i + j].va = cpu_to_be64(sg_dma_address(sg) +
2477 sg_offset);
2478 klms[i + j].bcount = cpu_to_be32(sg_dma_len(sg) -
2479 sg_offset);
2480 klms[i + j].key = cpu_to_be32(lkey);
2481 mr->ibmr.length += sg_dma_len(sg) - sg_offset;
2482
2483 sg_offset = 0;
2484 }
2485 if (meta_sg_offset_p)
2486 *meta_sg_offset_p = sg_offset;
2487
2488 mr->meta_ndescs = j;
2489 mr->meta_length = mr->ibmr.length - mr->data_length;
2490 }
2491
2492 return i + j;
2493 }
2494
mlx5_set_page(struct ib_mr * ibmr,u64 addr)2495 static int mlx5_set_page(struct ib_mr *ibmr, u64 addr)
2496 {
2497 struct mlx5_ib_mr *mr = to_mmr(ibmr);
2498 __be64 *descs;
2499
2500 if (unlikely(mr->ndescs == mr->max_descs))
2501 return -ENOMEM;
2502
2503 descs = mr->descs;
2504 descs[mr->ndescs++] = cpu_to_be64(addr | MLX5_EN_RD | MLX5_EN_WR);
2505
2506 return 0;
2507 }
2508
mlx5_set_page_pi(struct ib_mr * ibmr,u64 addr)2509 static int mlx5_set_page_pi(struct ib_mr *ibmr, u64 addr)
2510 {
2511 struct mlx5_ib_mr *mr = to_mmr(ibmr);
2512 __be64 *descs;
2513
2514 if (unlikely(mr->ndescs + mr->meta_ndescs == mr->max_descs))
2515 return -ENOMEM;
2516
2517 descs = mr->descs;
2518 descs[mr->ndescs + mr->meta_ndescs++] =
2519 cpu_to_be64(addr | MLX5_EN_RD | MLX5_EN_WR);
2520
2521 return 0;
2522 }
2523
2524 static int
mlx5_ib_map_mtt_mr_sg_pi(struct ib_mr * ibmr,struct scatterlist * data_sg,int data_sg_nents,unsigned int * data_sg_offset,struct scatterlist * meta_sg,int meta_sg_nents,unsigned int * meta_sg_offset)2525 mlx5_ib_map_mtt_mr_sg_pi(struct ib_mr *ibmr, struct scatterlist *data_sg,
2526 int data_sg_nents, unsigned int *data_sg_offset,
2527 struct scatterlist *meta_sg, int meta_sg_nents,
2528 unsigned int *meta_sg_offset)
2529 {
2530 struct mlx5_ib_mr *mr = to_mmr(ibmr);
2531 struct mlx5_ib_mr *pi_mr = mr->mtt_mr;
2532 int n;
2533
2534 pi_mr->ndescs = 0;
2535 pi_mr->meta_ndescs = 0;
2536 pi_mr->meta_length = 0;
2537
2538 ib_dma_sync_single_for_cpu(ibmr->device, pi_mr->desc_map,
2539 pi_mr->desc_size * pi_mr->max_descs,
2540 DMA_TO_DEVICE);
2541
2542 pi_mr->ibmr.page_size = ibmr->page_size;
2543 n = ib_sg_to_pages(&pi_mr->ibmr, data_sg, data_sg_nents, data_sg_offset,
2544 mlx5_set_page);
2545 if (n != data_sg_nents)
2546 return n;
2547
2548 pi_mr->data_iova = pi_mr->ibmr.iova;
2549 pi_mr->data_length = pi_mr->ibmr.length;
2550 pi_mr->ibmr.length = pi_mr->data_length;
2551 ibmr->length = pi_mr->data_length;
2552
2553 if (meta_sg_nents) {
2554 u64 page_mask = ~((u64)ibmr->page_size - 1);
2555 u64 iova = pi_mr->data_iova;
2556
2557 n += ib_sg_to_pages(&pi_mr->ibmr, meta_sg, meta_sg_nents,
2558 meta_sg_offset, mlx5_set_page_pi);
2559
2560 pi_mr->meta_length = pi_mr->ibmr.length;
2561 /*
2562 * PI address for the HW is the offset of the metadata address
2563 * relative to the first data page address.
2564 * It equals to first data page address + size of data pages +
2565 * metadata offset at the first metadata page
2566 */
2567 pi_mr->pi_iova = (iova & page_mask) +
2568 pi_mr->ndescs * ibmr->page_size +
2569 (pi_mr->ibmr.iova & ~page_mask);
2570 /*
2571 * In order to use one MTT MR for data and metadata, we register
2572 * also the gaps between the end of the data and the start of
2573 * the metadata (the sig MR will verify that the HW will access
2574 * to right addresses). This mapping is safe because we use
2575 * internal mkey for the registration.
2576 */
2577 pi_mr->ibmr.length = pi_mr->pi_iova + pi_mr->meta_length - iova;
2578 pi_mr->ibmr.iova = iova;
2579 ibmr->length += pi_mr->meta_length;
2580 }
2581
2582 ib_dma_sync_single_for_device(ibmr->device, pi_mr->desc_map,
2583 pi_mr->desc_size * pi_mr->max_descs,
2584 DMA_TO_DEVICE);
2585
2586 return n;
2587 }
2588
2589 static int
mlx5_ib_map_klm_mr_sg_pi(struct ib_mr * ibmr,struct scatterlist * data_sg,int data_sg_nents,unsigned int * data_sg_offset,struct scatterlist * meta_sg,int meta_sg_nents,unsigned int * meta_sg_offset)2590 mlx5_ib_map_klm_mr_sg_pi(struct ib_mr *ibmr, struct scatterlist *data_sg,
2591 int data_sg_nents, unsigned int *data_sg_offset,
2592 struct scatterlist *meta_sg, int meta_sg_nents,
2593 unsigned int *meta_sg_offset)
2594 {
2595 struct mlx5_ib_mr *mr = to_mmr(ibmr);
2596 struct mlx5_ib_mr *pi_mr = mr->klm_mr;
2597 int n;
2598
2599 pi_mr->ndescs = 0;
2600 pi_mr->meta_ndescs = 0;
2601 pi_mr->meta_length = 0;
2602
2603 ib_dma_sync_single_for_cpu(ibmr->device, pi_mr->desc_map,
2604 pi_mr->desc_size * pi_mr->max_descs,
2605 DMA_TO_DEVICE);
2606
2607 n = mlx5_ib_sg_to_klms(pi_mr, data_sg, data_sg_nents, data_sg_offset,
2608 meta_sg, meta_sg_nents, meta_sg_offset);
2609
2610 ib_dma_sync_single_for_device(ibmr->device, pi_mr->desc_map,
2611 pi_mr->desc_size * pi_mr->max_descs,
2612 DMA_TO_DEVICE);
2613
2614 /* This is zero-based memory region */
2615 pi_mr->data_iova = 0;
2616 pi_mr->ibmr.iova = 0;
2617 pi_mr->pi_iova = pi_mr->data_length;
2618 ibmr->length = pi_mr->ibmr.length;
2619
2620 return n;
2621 }
2622
mlx5_ib_map_mr_sg_pi(struct ib_mr * ibmr,struct scatterlist * data_sg,int data_sg_nents,unsigned int * data_sg_offset,struct scatterlist * meta_sg,int meta_sg_nents,unsigned int * meta_sg_offset)2623 int mlx5_ib_map_mr_sg_pi(struct ib_mr *ibmr, struct scatterlist *data_sg,
2624 int data_sg_nents, unsigned int *data_sg_offset,
2625 struct scatterlist *meta_sg, int meta_sg_nents,
2626 unsigned int *meta_sg_offset)
2627 {
2628 struct mlx5_ib_mr *mr = to_mmr(ibmr);
2629 struct mlx5_ib_mr *pi_mr = NULL;
2630 int n;
2631
2632 WARN_ON(ibmr->type != IB_MR_TYPE_INTEGRITY);
2633
2634 mr->ndescs = 0;
2635 mr->data_length = 0;
2636 mr->data_iova = 0;
2637 mr->meta_ndescs = 0;
2638 mr->pi_iova = 0;
2639 /*
2640 * As a performance optimization, if possible, there is no need to
2641 * perform UMR operation to register the data/metadata buffers.
2642 * First try to map the sg lists to PA descriptors with local_dma_lkey.
2643 * Fallback to UMR only in case of a failure.
2644 */
2645 n = mlx5_ib_map_pa_mr_sg_pi(ibmr, data_sg, data_sg_nents,
2646 data_sg_offset, meta_sg, meta_sg_nents,
2647 meta_sg_offset);
2648 if (n == data_sg_nents + meta_sg_nents)
2649 goto out;
2650 /*
2651 * As a performance optimization, if possible, there is no need to map
2652 * the sg lists to KLM descriptors. First try to map the sg lists to MTT
2653 * descriptors and fallback to KLM only in case of a failure.
2654 * It's more efficient for the HW to work with MTT descriptors
2655 * (especially in high load).
2656 * Use KLM (indirect access) only if it's mandatory.
2657 */
2658 pi_mr = mr->mtt_mr;
2659 n = mlx5_ib_map_mtt_mr_sg_pi(ibmr, data_sg, data_sg_nents,
2660 data_sg_offset, meta_sg, meta_sg_nents,
2661 meta_sg_offset);
2662 if (n == data_sg_nents + meta_sg_nents)
2663 goto out;
2664
2665 pi_mr = mr->klm_mr;
2666 n = mlx5_ib_map_klm_mr_sg_pi(ibmr, data_sg, data_sg_nents,
2667 data_sg_offset, meta_sg, meta_sg_nents,
2668 meta_sg_offset);
2669 if (unlikely(n != data_sg_nents + meta_sg_nents))
2670 return -ENOMEM;
2671
2672 out:
2673 /* This is zero-based memory region */
2674 ibmr->iova = 0;
2675 mr->pi_mr = pi_mr;
2676 if (pi_mr)
2677 ibmr->sig_attrs->meta_length = pi_mr->meta_length;
2678 else
2679 ibmr->sig_attrs->meta_length = mr->meta_length;
2680
2681 return 0;
2682 }
2683
mlx5_ib_map_mr_sg(struct ib_mr * ibmr,struct scatterlist * sg,int sg_nents,unsigned int * sg_offset)2684 int mlx5_ib_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
2685 unsigned int *sg_offset)
2686 {
2687 struct mlx5_ib_mr *mr = to_mmr(ibmr);
2688 int n;
2689
2690 mr->ndescs = 0;
2691
2692 ib_dma_sync_single_for_cpu(ibmr->device, mr->desc_map,
2693 mr->desc_size * mr->max_descs,
2694 DMA_TO_DEVICE);
2695
2696 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
2697 n = mlx5_ib_sg_to_klms(mr, sg, sg_nents, sg_offset, NULL, 0,
2698 NULL);
2699 else
2700 n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset,
2701 mlx5_set_page);
2702
2703 ib_dma_sync_single_for_device(ibmr->device, mr->desc_map,
2704 mr->desc_size * mr->max_descs,
2705 DMA_TO_DEVICE);
2706
2707 return n;
2708 }
2709