blob: 93c0e82aa4913d112ee14b3d1d613563f04f7a86 [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
Saeed Mahameed6cf0a152015-04-02 17:07:30 +03002 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
Eli Cohene126ba92013-07-07 17:25:49 +03003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33
34#include <linux/kref.h>
35#include <linux/random.h>
36#include <linux/debugfs.h>
37#include <linux/export.h>
Eli Cohen746b5582013-10-23 09:53:14 +030038#include <linux/delay.h>
Eli Cohene126ba92013-07-07 17:25:49 +030039#include <rdma/ib_umem.h>
Haggai Eranb4cfe442014-12-11 17:04:26 +020040#include <rdma/ib_umem_odp.h>
Haggai Eran968e78d2014-12-11 17:04:11 +020041#include <rdma/ib_verbs.h>
Eli Cohene126ba92013-07-07 17:25:49 +030042#include "mlx5_ib.h"
43
44enum {
Eli Cohen746b5582013-10-23 09:53:14 +030045 MAX_PENDING_REG_MR = 8,
Eli Cohene126ba92013-07-07 17:25:49 +030046};
47
Haggai Eran832a6b02014-12-11 17:04:22 +020048#define MLX5_UMR_ALIGN 2048
Eli Cohenfe45f822013-09-11 16:35:35 +030049
Haggai Eran6aec21f2014-12-11 17:04:23 +020050static int clean_mr(struct mlx5_ib_mr *mr);
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +020051static int use_umr(struct mlx5_ib_dev *dev, int order);
Artemy Kovalyov49780d42017-01-18 16:58:10 +020052static int unreg_umr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr);
Haggai Eran6aec21f2014-12-11 17:04:23 +020053
Haggai Eranb4cfe442014-12-11 17:04:26 +020054static int destroy_mkey(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
55{
Matan Baraka606b0f2016-02-29 18:05:28 +020056 int err = mlx5_core_destroy_mkey(dev->mdev, &mr->mmkey);
Haggai Eranb4cfe442014-12-11 17:04:26 +020057
58#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
59 /* Wait until all page fault handlers using the mr complete. */
60 synchronize_srcu(&dev->mr_srcu);
61#endif
62
63 return err;
64}
65
Eli Cohene126ba92013-07-07 17:25:49 +030066static int order2idx(struct mlx5_ib_dev *dev, int order)
67{
68 struct mlx5_mr_cache *cache = &dev->cache;
69
70 if (order < cache->ent[0].order)
71 return 0;
72 else
73 return order - cache->ent[0].order;
74}
75
Noa Osherovich56e11d62016-02-29 16:46:51 +020076static bool use_umr_mtt_update(struct mlx5_ib_mr *mr, u64 start, u64 length)
77{
78 return ((u64)1 << mr->order) * MLX5_ADAPTER_PAGE_SIZE >=
79 length + (start & (MLX5_ADAPTER_PAGE_SIZE - 1));
80}
81
Noa Osherovich395a8e42016-02-29 16:46:50 +020082#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
83static void update_odp_mr(struct mlx5_ib_mr *mr)
84{
85 if (mr->umem->odp_data) {
86 /*
87 * This barrier prevents the compiler from moving the
88 * setting of umem->odp_data->private to point to our
89 * MR, before reg_umr finished, to ensure that the MR
90 * initialization have finished before starting to
91 * handle invalidations.
92 */
93 smp_wmb();
94 mr->umem->odp_data->private = mr;
95 /*
96 * Make sure we will see the new
97 * umem->odp_data->private value in the invalidation
98 * routines, before we can get page faults on the
99 * MR. Page faults can happen once we put the MR in
100 * the tree, below this line. Without the barrier,
101 * there can be a fault handling and an invalidation
102 * before umem->odp_data->private == mr is visible to
103 * the invalidation handler.
104 */
105 smp_wmb();
106 }
107}
108#endif
109
Eli Cohen746b5582013-10-23 09:53:14 +0300110static void reg_mr_callback(int status, void *context)
111{
112 struct mlx5_ib_mr *mr = context;
113 struct mlx5_ib_dev *dev = mr->dev;
114 struct mlx5_mr_cache *cache = &dev->cache;
115 int c = order2idx(dev, mr->order);
116 struct mlx5_cache_ent *ent = &cache->ent[c];
117 u8 key;
Eli Cohen746b5582013-10-23 09:53:14 +0300118 unsigned long flags;
Matan Baraka606b0f2016-02-29 18:05:28 +0200119 struct mlx5_mkey_table *table = &dev->mdev->priv.mkey_table;
Haggai Eran86059332014-05-22 14:50:09 +0300120 int err;
Eli Cohen746b5582013-10-23 09:53:14 +0300121
Eli Cohen746b5582013-10-23 09:53:14 +0300122 spin_lock_irqsave(&ent->lock, flags);
123 ent->pending--;
124 spin_unlock_irqrestore(&ent->lock, flags);
125 if (status) {
126 mlx5_ib_warn(dev, "async reg mr failed. status %d\n", status);
127 kfree(mr);
128 dev->fill_delay = 1;
129 mod_timer(&dev->delay_timer, jiffies + HZ);
130 return;
131 }
132
Artemy Kovalyovaa8e08d2017-01-02 11:37:48 +0200133 mr->mmkey.type = MLX5_MKEY_MR;
Jack Morgenstein9603b612014-07-28 23:30:22 +0300134 spin_lock_irqsave(&dev->mdev->priv.mkey_lock, flags);
135 key = dev->mdev->priv.mkey_key++;
136 spin_unlock_irqrestore(&dev->mdev->priv.mkey_lock, flags);
Saeed Mahameedec22eb52016-07-16 06:28:36 +0300137 mr->mmkey.key = mlx5_idx_to_mkey(MLX5_GET(create_mkey_out, mr->out, mkey_index)) | key;
Eli Cohen746b5582013-10-23 09:53:14 +0300138
139 cache->last_add = jiffies;
140
141 spin_lock_irqsave(&ent->lock, flags);
142 list_add_tail(&mr->list, &ent->head);
143 ent->cur++;
144 ent->size++;
145 spin_unlock_irqrestore(&ent->lock, flags);
Haggai Eran86059332014-05-22 14:50:09 +0300146
147 write_lock_irqsave(&table->lock, flags);
Matan Baraka606b0f2016-02-29 18:05:28 +0200148 err = radix_tree_insert(&table->tree, mlx5_base_mkey(mr->mmkey.key),
149 &mr->mmkey);
Haggai Eran86059332014-05-22 14:50:09 +0300150 if (err)
Matan Baraka606b0f2016-02-29 18:05:28 +0200151 pr_err("Error inserting to mkey tree. 0x%x\n", -err);
Haggai Eran86059332014-05-22 14:50:09 +0300152 write_unlock_irqrestore(&table->lock, flags);
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200153
154 if (!completion_done(&ent->compl))
155 complete(&ent->compl);
Eli Cohen746b5582013-10-23 09:53:14 +0300156}
157
Eli Cohene126ba92013-07-07 17:25:49 +0300158static int add_keys(struct mlx5_ib_dev *dev, int c, int num)
159{
Eli Cohene126ba92013-07-07 17:25:49 +0300160 struct mlx5_mr_cache *cache = &dev->cache;
161 struct mlx5_cache_ent *ent = &cache->ent[c];
Saeed Mahameedec22eb52016-07-16 06:28:36 +0300162 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
Eli Cohene126ba92013-07-07 17:25:49 +0300163 struct mlx5_ib_mr *mr;
Saeed Mahameedec22eb52016-07-16 06:28:36 +0300164 void *mkc;
165 u32 *in;
Eli Cohene126ba92013-07-07 17:25:49 +0300166 int err = 0;
167 int i;
168
Saeed Mahameedec22eb52016-07-16 06:28:36 +0300169 in = kzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300170 if (!in)
171 return -ENOMEM;
172
Saeed Mahameedec22eb52016-07-16 06:28:36 +0300173 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
Eli Cohene126ba92013-07-07 17:25:49 +0300174 for (i = 0; i < num; i++) {
Eli Cohen746b5582013-10-23 09:53:14 +0300175 if (ent->pending >= MAX_PENDING_REG_MR) {
176 err = -EAGAIN;
177 break;
178 }
179
Eli Cohene126ba92013-07-07 17:25:49 +0300180 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
181 if (!mr) {
182 err = -ENOMEM;
Eli Cohen746b5582013-10-23 09:53:14 +0300183 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300184 }
185 mr->order = ent->order;
186 mr->umred = 1;
Eli Cohen746b5582013-10-23 09:53:14 +0300187 mr->dev = dev;
Saeed Mahameedec22eb52016-07-16 06:28:36 +0300188
189 MLX5_SET(mkc, mkc, free, 1);
190 MLX5_SET(mkc, mkc, umr_en, 1);
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200191 MLX5_SET(mkc, mkc, access_mode, ent->access_mode);
Saeed Mahameedec22eb52016-07-16 06:28:36 +0300192
193 MLX5_SET(mkc, mkc, qpn, 0xffffff);
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200194 MLX5_SET(mkc, mkc, translations_octword_size, ent->xlt);
195 MLX5_SET(mkc, mkc, log_page_size, ent->page);
Eli Cohene126ba92013-07-07 17:25:49 +0300196
Eli Cohen746b5582013-10-23 09:53:14 +0300197 spin_lock_irq(&ent->lock);
198 ent->pending++;
199 spin_unlock_irq(&ent->lock);
Saeed Mahameedec22eb52016-07-16 06:28:36 +0300200 err = mlx5_core_create_mkey_cb(dev->mdev, &mr->mmkey,
201 in, inlen,
202 mr->out, sizeof(mr->out),
203 reg_mr_callback, mr);
Eli Cohene126ba92013-07-07 17:25:49 +0300204 if (err) {
Eli Cohend14e7112014-12-02 12:26:19 +0200205 spin_lock_irq(&ent->lock);
206 ent->pending--;
207 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300208 mlx5_ib_warn(dev, "create mkey failed %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +0300209 kfree(mr);
Eli Cohen746b5582013-10-23 09:53:14 +0300210 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300211 }
Eli Cohene126ba92013-07-07 17:25:49 +0300212 }
213
Eli Cohene126ba92013-07-07 17:25:49 +0300214 kfree(in);
215 return err;
216}
217
218static void remove_keys(struct mlx5_ib_dev *dev, int c, int num)
219{
Eli Cohene126ba92013-07-07 17:25:49 +0300220 struct mlx5_mr_cache *cache = &dev->cache;
221 struct mlx5_cache_ent *ent = &cache->ent[c];
222 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +0300223 int err;
224 int i;
225
226 for (i = 0; i < num; i++) {
Eli Cohen746b5582013-10-23 09:53:14 +0300227 spin_lock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300228 if (list_empty(&ent->head)) {
Eli Cohen746b5582013-10-23 09:53:14 +0300229 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300230 return;
231 }
232 mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
233 list_del(&mr->list);
234 ent->cur--;
235 ent->size--;
Eli Cohen746b5582013-10-23 09:53:14 +0300236 spin_unlock_irq(&ent->lock);
Haggai Eranb4cfe442014-12-11 17:04:26 +0200237 err = destroy_mkey(dev, mr);
Eli Cohen203099f2013-09-11 16:35:26 +0300238 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +0300239 mlx5_ib_warn(dev, "failed destroy mkey\n");
Eli Cohen203099f2013-09-11 16:35:26 +0300240 else
Eli Cohene126ba92013-07-07 17:25:49 +0300241 kfree(mr);
Eli Cohene126ba92013-07-07 17:25:49 +0300242 }
243}
244
245static ssize_t size_write(struct file *filp, const char __user *buf,
246 size_t count, loff_t *pos)
247{
248 struct mlx5_cache_ent *ent = filp->private_data;
249 struct mlx5_ib_dev *dev = ent->dev;
250 char lbuf[20];
251 u32 var;
252 int err;
253 int c;
254
255 if (copy_from_user(lbuf, buf, sizeof(lbuf)))
Dan Carpenter5e631a02013-07-10 13:58:59 +0300256 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +0300257
258 c = order2idx(dev, ent->order);
259 lbuf[sizeof(lbuf) - 1] = 0;
260
261 if (sscanf(lbuf, "%u", &var) != 1)
262 return -EINVAL;
263
264 if (var < ent->limit)
265 return -EINVAL;
266
267 if (var > ent->size) {
Eli Cohen746b5582013-10-23 09:53:14 +0300268 do {
269 err = add_keys(dev, c, var - ent->size);
270 if (err && err != -EAGAIN)
271 return err;
272
273 usleep_range(3000, 5000);
274 } while (err);
Eli Cohene126ba92013-07-07 17:25:49 +0300275 } else if (var < ent->size) {
276 remove_keys(dev, c, ent->size - var);
277 }
278
279 return count;
280}
281
282static ssize_t size_read(struct file *filp, char __user *buf, size_t count,
283 loff_t *pos)
284{
285 struct mlx5_cache_ent *ent = filp->private_data;
286 char lbuf[20];
287 int err;
288
289 if (*pos)
290 return 0;
291
292 err = snprintf(lbuf, sizeof(lbuf), "%d\n", ent->size);
293 if (err < 0)
294 return err;
295
296 if (copy_to_user(buf, lbuf, err))
Dan Carpenter5e631a02013-07-10 13:58:59 +0300297 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +0300298
299 *pos += err;
300
301 return err;
302}
303
304static const struct file_operations size_fops = {
305 .owner = THIS_MODULE,
306 .open = simple_open,
307 .write = size_write,
308 .read = size_read,
309};
310
311static ssize_t limit_write(struct file *filp, const char __user *buf,
312 size_t count, loff_t *pos)
313{
314 struct mlx5_cache_ent *ent = filp->private_data;
315 struct mlx5_ib_dev *dev = ent->dev;
316 char lbuf[20];
317 u32 var;
318 int err;
319 int c;
320
321 if (copy_from_user(lbuf, buf, sizeof(lbuf)))
Dan Carpenter5e631a02013-07-10 13:58:59 +0300322 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +0300323
324 c = order2idx(dev, ent->order);
325 lbuf[sizeof(lbuf) - 1] = 0;
326
327 if (sscanf(lbuf, "%u", &var) != 1)
328 return -EINVAL;
329
330 if (var > ent->size)
331 return -EINVAL;
332
333 ent->limit = var;
334
335 if (ent->cur < ent->limit) {
336 err = add_keys(dev, c, 2 * ent->limit - ent->cur);
337 if (err)
338 return err;
339 }
340
341 return count;
342}
343
344static ssize_t limit_read(struct file *filp, char __user *buf, size_t count,
345 loff_t *pos)
346{
347 struct mlx5_cache_ent *ent = filp->private_data;
348 char lbuf[20];
349 int err;
350
351 if (*pos)
352 return 0;
353
354 err = snprintf(lbuf, sizeof(lbuf), "%d\n", ent->limit);
355 if (err < 0)
356 return err;
357
358 if (copy_to_user(buf, lbuf, err))
Dan Carpenter5e631a02013-07-10 13:58:59 +0300359 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +0300360
361 *pos += err;
362
363 return err;
364}
365
366static const struct file_operations limit_fops = {
367 .owner = THIS_MODULE,
368 .open = simple_open,
369 .write = limit_write,
370 .read = limit_read,
371};
372
373static int someone_adding(struct mlx5_mr_cache *cache)
374{
375 int i;
376
377 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
378 if (cache->ent[i].cur < cache->ent[i].limit)
379 return 1;
380 }
381
382 return 0;
383}
384
385static void __cache_work_func(struct mlx5_cache_ent *ent)
386{
387 struct mlx5_ib_dev *dev = ent->dev;
388 struct mlx5_mr_cache *cache = &dev->cache;
389 int i = order2idx(dev, ent->order);
Eli Cohen746b5582013-10-23 09:53:14 +0300390 int err;
Eli Cohene126ba92013-07-07 17:25:49 +0300391
392 if (cache->stopped)
393 return;
394
395 ent = &dev->cache.ent[i];
Eli Cohen746b5582013-10-23 09:53:14 +0300396 if (ent->cur < 2 * ent->limit && !dev->fill_delay) {
397 err = add_keys(dev, i, 1);
398 if (ent->cur < 2 * ent->limit) {
399 if (err == -EAGAIN) {
400 mlx5_ib_dbg(dev, "returned eagain, order %d\n",
401 i + 2);
402 queue_delayed_work(cache->wq, &ent->dwork,
403 msecs_to_jiffies(3));
404 } else if (err) {
405 mlx5_ib_warn(dev, "command failed order %d, err %d\n",
406 i + 2, err);
407 queue_delayed_work(cache->wq, &ent->dwork,
408 msecs_to_jiffies(1000));
409 } else {
410 queue_work(cache->wq, &ent->work);
411 }
412 }
Eli Cohene126ba92013-07-07 17:25:49 +0300413 } else if (ent->cur > 2 * ent->limit) {
Leon Romanovskyab5cdc32015-10-21 09:21:17 +0300414 /*
415 * The remove_keys() logic is performed as garbage collection
416 * task. Such task is intended to be run when no other active
417 * processes are running.
418 *
419 * The need_resched() will return TRUE if there are user tasks
420 * to be activated in near future.
421 *
422 * In such case, we don't execute remove_keys() and postpone
423 * the garbage collection work to try to run in next cycle,
424 * in order to free CPU resources to other tasks.
425 */
426 if (!need_resched() && !someone_adding(cache) &&
Eli Cohen746b5582013-10-23 09:53:14 +0300427 time_after(jiffies, cache->last_add + 300 * HZ)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300428 remove_keys(dev, i, 1);
429 if (ent->cur > ent->limit)
430 queue_work(cache->wq, &ent->work);
431 } else {
Eli Cohen746b5582013-10-23 09:53:14 +0300432 queue_delayed_work(cache->wq, &ent->dwork, 300 * HZ);
Eli Cohene126ba92013-07-07 17:25:49 +0300433 }
434 }
435}
436
437static void delayed_cache_work_func(struct work_struct *work)
438{
439 struct mlx5_cache_ent *ent;
440
441 ent = container_of(work, struct mlx5_cache_ent, dwork.work);
442 __cache_work_func(ent);
443}
444
445static void cache_work_func(struct work_struct *work)
446{
447 struct mlx5_cache_ent *ent;
448
449 ent = container_of(work, struct mlx5_cache_ent, work);
450 __cache_work_func(ent);
451}
452
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200453struct mlx5_ib_mr *mlx5_mr_cache_alloc(struct mlx5_ib_dev *dev, int entry)
454{
455 struct mlx5_mr_cache *cache = &dev->cache;
456 struct mlx5_cache_ent *ent;
457 struct mlx5_ib_mr *mr;
458 int err;
459
460 if (entry < 0 || entry >= MAX_MR_CACHE_ENTRIES) {
461 mlx5_ib_err(dev, "cache entry %d is out of range\n", entry);
462 return NULL;
463 }
464
465 ent = &cache->ent[entry];
466 while (1) {
467 spin_lock_irq(&ent->lock);
468 if (list_empty(&ent->head)) {
469 spin_unlock_irq(&ent->lock);
470
471 err = add_keys(dev, entry, 1);
Artemy Kovalyov81713d32017-01-18 16:58:11 +0200472 if (err && err != -EAGAIN)
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200473 return ERR_PTR(err);
474
475 wait_for_completion(&ent->compl);
476 } else {
477 mr = list_first_entry(&ent->head, struct mlx5_ib_mr,
478 list);
479 list_del(&mr->list);
480 ent->cur--;
481 spin_unlock_irq(&ent->lock);
482 if (ent->cur < ent->limit)
483 queue_work(cache->wq, &ent->work);
484 return mr;
485 }
486 }
487}
488
Eli Cohene126ba92013-07-07 17:25:49 +0300489static struct mlx5_ib_mr *alloc_cached_mr(struct mlx5_ib_dev *dev, int order)
490{
491 struct mlx5_mr_cache *cache = &dev->cache;
492 struct mlx5_ib_mr *mr = NULL;
493 struct mlx5_cache_ent *ent;
494 int c;
495 int i;
496
497 c = order2idx(dev, order);
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200498 if (c < 0 || c > MAX_UMR_CACHE_ENTRY) {
Eli Cohene126ba92013-07-07 17:25:49 +0300499 mlx5_ib_warn(dev, "order %d, cache index %d\n", order, c);
500 return NULL;
501 }
502
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200503 for (i = c; i < MAX_UMR_CACHE_ENTRY; i++) {
Eli Cohene126ba92013-07-07 17:25:49 +0300504 ent = &cache->ent[i];
505
506 mlx5_ib_dbg(dev, "order %d, cache index %d\n", ent->order, i);
507
Eli Cohen746b5582013-10-23 09:53:14 +0300508 spin_lock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300509 if (!list_empty(&ent->head)) {
510 mr = list_first_entry(&ent->head, struct mlx5_ib_mr,
511 list);
512 list_del(&mr->list);
513 ent->cur--;
Eli Cohen746b5582013-10-23 09:53:14 +0300514 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300515 if (ent->cur < ent->limit)
516 queue_work(cache->wq, &ent->work);
517 break;
518 }
Eli Cohen746b5582013-10-23 09:53:14 +0300519 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300520
521 queue_work(cache->wq, &ent->work);
Eli Cohene126ba92013-07-07 17:25:49 +0300522 }
523
524 if (!mr)
525 cache->ent[c].miss++;
526
527 return mr;
528}
529
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200530void mlx5_mr_cache_free(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
Eli Cohene126ba92013-07-07 17:25:49 +0300531{
532 struct mlx5_mr_cache *cache = &dev->cache;
533 struct mlx5_cache_ent *ent;
534 int shrink = 0;
535 int c;
536
537 c = order2idx(dev, mr->order);
538 if (c < 0 || c >= MAX_MR_CACHE_ENTRIES) {
539 mlx5_ib_warn(dev, "order %d, cache index %d\n", mr->order, c);
540 return;
541 }
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200542
543 if (unreg_umr(dev, mr))
544 return;
545
Eli Cohene126ba92013-07-07 17:25:49 +0300546 ent = &cache->ent[c];
Eli Cohen746b5582013-10-23 09:53:14 +0300547 spin_lock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300548 list_add_tail(&mr->list, &ent->head);
549 ent->cur++;
550 if (ent->cur > 2 * ent->limit)
551 shrink = 1;
Eli Cohen746b5582013-10-23 09:53:14 +0300552 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300553
554 if (shrink)
555 queue_work(cache->wq, &ent->work);
556}
557
558static void clean_keys(struct mlx5_ib_dev *dev, int c)
559{
Eli Cohene126ba92013-07-07 17:25:49 +0300560 struct mlx5_mr_cache *cache = &dev->cache;
561 struct mlx5_cache_ent *ent = &cache->ent[c];
562 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +0300563 int err;
564
Moshe Lazer3c461912013-09-11 16:35:23 +0300565 cancel_delayed_work(&ent->dwork);
Eli Cohene126ba92013-07-07 17:25:49 +0300566 while (1) {
Eli Cohen746b5582013-10-23 09:53:14 +0300567 spin_lock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300568 if (list_empty(&ent->head)) {
Eli Cohen746b5582013-10-23 09:53:14 +0300569 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300570 return;
571 }
572 mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
573 list_del(&mr->list);
574 ent->cur--;
575 ent->size--;
Eli Cohen746b5582013-10-23 09:53:14 +0300576 spin_unlock_irq(&ent->lock);
Haggai Eranb4cfe442014-12-11 17:04:26 +0200577 err = destroy_mkey(dev, mr);
Eli Cohen203099f2013-09-11 16:35:26 +0300578 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +0300579 mlx5_ib_warn(dev, "failed destroy mkey\n");
Eli Cohen203099f2013-09-11 16:35:26 +0300580 else
Eli Cohene126ba92013-07-07 17:25:49 +0300581 kfree(mr);
Eli Cohene126ba92013-07-07 17:25:49 +0300582 }
583}
584
585static int mlx5_mr_cache_debugfs_init(struct mlx5_ib_dev *dev)
586{
587 struct mlx5_mr_cache *cache = &dev->cache;
588 struct mlx5_cache_ent *ent;
589 int i;
590
591 if (!mlx5_debugfs_root)
592 return 0;
593
Jack Morgenstein9603b612014-07-28 23:30:22 +0300594 cache->root = debugfs_create_dir("mr_cache", dev->mdev->priv.dbg_root);
Eli Cohene126ba92013-07-07 17:25:49 +0300595 if (!cache->root)
596 return -ENOMEM;
597
598 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
599 ent = &cache->ent[i];
600 sprintf(ent->name, "%d", ent->order);
601 ent->dir = debugfs_create_dir(ent->name, cache->root);
602 if (!ent->dir)
603 return -ENOMEM;
604
605 ent->fsize = debugfs_create_file("size", 0600, ent->dir, ent,
606 &size_fops);
607 if (!ent->fsize)
608 return -ENOMEM;
609
610 ent->flimit = debugfs_create_file("limit", 0600, ent->dir, ent,
611 &limit_fops);
612 if (!ent->flimit)
613 return -ENOMEM;
614
615 ent->fcur = debugfs_create_u32("cur", 0400, ent->dir,
616 &ent->cur);
617 if (!ent->fcur)
618 return -ENOMEM;
619
620 ent->fmiss = debugfs_create_u32("miss", 0600, ent->dir,
621 &ent->miss);
622 if (!ent->fmiss)
623 return -ENOMEM;
624 }
625
626 return 0;
627}
628
629static void mlx5_mr_cache_debugfs_cleanup(struct mlx5_ib_dev *dev)
630{
631 if (!mlx5_debugfs_root)
632 return;
633
634 debugfs_remove_recursive(dev->cache.root);
635}
636
Eli Cohen746b5582013-10-23 09:53:14 +0300637static void delay_time_func(unsigned long ctx)
638{
639 struct mlx5_ib_dev *dev = (struct mlx5_ib_dev *)ctx;
640
641 dev->fill_delay = 0;
642}
643
Eli Cohene126ba92013-07-07 17:25:49 +0300644int mlx5_mr_cache_init(struct mlx5_ib_dev *dev)
645{
646 struct mlx5_mr_cache *cache = &dev->cache;
647 struct mlx5_cache_ent *ent;
Eli Cohene126ba92013-07-07 17:25:49 +0300648 int err;
649 int i;
650
Moshe Lazer6bc1a652016-10-27 16:36:42 +0300651 mutex_init(&dev->slow_path_mutex);
Bhaktipriya Shridhar3c856c82016-08-15 23:41:18 +0530652 cache->wq = alloc_ordered_workqueue("mkey_cache", WQ_MEM_RECLAIM);
Eli Cohene126ba92013-07-07 17:25:49 +0300653 if (!cache->wq) {
654 mlx5_ib_warn(dev, "failed to create work queue\n");
655 return -ENOMEM;
656 }
657
Eli Cohen746b5582013-10-23 09:53:14 +0300658 setup_timer(&dev->delay_timer, delay_time_func, (unsigned long)dev);
Eli Cohene126ba92013-07-07 17:25:49 +0300659 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
Eli Cohene126ba92013-07-07 17:25:49 +0300660 ent = &cache->ent[i];
661 INIT_LIST_HEAD(&ent->head);
662 spin_lock_init(&ent->lock);
663 ent->order = i + 2;
664 ent->dev = dev;
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200665 ent->limit = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300666
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200667 init_completion(&ent->compl);
Eli Cohene126ba92013-07-07 17:25:49 +0300668 INIT_WORK(&ent->work, cache_work_func);
669 INIT_DELAYED_WORK(&ent->dwork, delayed_cache_work_func);
Eli Cohene126ba92013-07-07 17:25:49 +0300670 queue_work(cache->wq, &ent->work);
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200671
Artemy Kovalyov81713d32017-01-18 16:58:11 +0200672 if (i > MAX_UMR_CACHE_ENTRY) {
673 mlx5_odp_init_mr_cache_entry(ent);
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200674 continue;
Artemy Kovalyov81713d32017-01-18 16:58:11 +0200675 }
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200676
677 if (!use_umr(dev, ent->order))
678 continue;
679
680 ent->page = PAGE_SHIFT;
681 ent->xlt = (1 << ent->order) * sizeof(struct mlx5_mtt) /
682 MLX5_IB_UMR_OCTOWORD;
683 ent->access_mode = MLX5_MKC_ACCESS_MODE_MTT;
684 if ((dev->mdev->profile->mask & MLX5_PROF_MASK_MR_CACHE) &&
685 mlx5_core_is_pf(dev->mdev))
686 ent->limit = dev->mdev->profile->mr_cache[i].limit;
687 else
688 ent->limit = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300689 }
690
691 err = mlx5_mr_cache_debugfs_init(dev);
692 if (err)
693 mlx5_ib_warn(dev, "cache debugfs failure\n");
694
695 return 0;
696}
697
Eli Cohenacbda522016-10-27 16:36:43 +0300698static void wait_for_async_commands(struct mlx5_ib_dev *dev)
699{
700 struct mlx5_mr_cache *cache = &dev->cache;
701 struct mlx5_cache_ent *ent;
702 int total = 0;
703 int i;
704 int j;
705
706 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
707 ent = &cache->ent[i];
708 for (j = 0 ; j < 1000; j++) {
709 if (!ent->pending)
710 break;
711 msleep(50);
712 }
713 }
714 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
715 ent = &cache->ent[i];
716 total += ent->pending;
717 }
718
719 if (total)
720 mlx5_ib_warn(dev, "aborted while there are %d pending mr requests\n", total);
721 else
722 mlx5_ib_warn(dev, "done with all pending requests\n");
723}
724
Eli Cohene126ba92013-07-07 17:25:49 +0300725int mlx5_mr_cache_cleanup(struct mlx5_ib_dev *dev)
726{
727 int i;
728
729 dev->cache.stopped = 1;
Moshe Lazer3c461912013-09-11 16:35:23 +0300730 flush_workqueue(dev->cache.wq);
Eli Cohene126ba92013-07-07 17:25:49 +0300731
732 mlx5_mr_cache_debugfs_cleanup(dev);
733
734 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++)
735 clean_keys(dev, i);
736
Moshe Lazer3c461912013-09-11 16:35:23 +0300737 destroy_workqueue(dev->cache.wq);
Eli Cohenacbda522016-10-27 16:36:43 +0300738 wait_for_async_commands(dev);
Eli Cohen746b5582013-10-23 09:53:14 +0300739 del_timer_sync(&dev->delay_timer);
Moshe Lazer3c461912013-09-11 16:35:23 +0300740
Eli Cohene126ba92013-07-07 17:25:49 +0300741 return 0;
742}
743
744struct ib_mr *mlx5_ib_get_dma_mr(struct ib_pd *pd, int acc)
745{
746 struct mlx5_ib_dev *dev = to_mdev(pd->device);
Saeed Mahameedec22eb52016-07-16 06:28:36 +0300747 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300748 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300749 struct mlx5_ib_mr *mr;
Saeed Mahameedec22eb52016-07-16 06:28:36 +0300750 void *mkc;
751 u32 *in;
Eli Cohene126ba92013-07-07 17:25:49 +0300752 int err;
753
754 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
755 if (!mr)
756 return ERR_PTR(-ENOMEM);
757
Saeed Mahameedec22eb52016-07-16 06:28:36 +0300758 in = kzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300759 if (!in) {
760 err = -ENOMEM;
761 goto err_free;
762 }
763
Saeed Mahameedec22eb52016-07-16 06:28:36 +0300764 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
Eli Cohene126ba92013-07-07 17:25:49 +0300765
Saeed Mahameedec22eb52016-07-16 06:28:36 +0300766 MLX5_SET(mkc, mkc, access_mode, MLX5_MKC_ACCESS_MODE_PA);
767 MLX5_SET(mkc, mkc, a, !!(acc & IB_ACCESS_REMOTE_ATOMIC));
768 MLX5_SET(mkc, mkc, rw, !!(acc & IB_ACCESS_REMOTE_WRITE));
769 MLX5_SET(mkc, mkc, rr, !!(acc & IB_ACCESS_REMOTE_READ));
770 MLX5_SET(mkc, mkc, lw, !!(acc & IB_ACCESS_LOCAL_WRITE));
771 MLX5_SET(mkc, mkc, lr, 1);
772
773 MLX5_SET(mkc, mkc, length64, 1);
774 MLX5_SET(mkc, mkc, pd, to_mpd(pd)->pdn);
775 MLX5_SET(mkc, mkc, qpn, 0xffffff);
776 MLX5_SET64(mkc, mkc, start_addr, 0);
777
778 err = mlx5_core_create_mkey(mdev, &mr->mmkey, in, inlen);
Eli Cohene126ba92013-07-07 17:25:49 +0300779 if (err)
780 goto err_in;
781
782 kfree(in);
Artemy Kovalyovaa8e08d2017-01-02 11:37:48 +0200783 mr->mmkey.type = MLX5_MKEY_MR;
Matan Baraka606b0f2016-02-29 18:05:28 +0200784 mr->ibmr.lkey = mr->mmkey.key;
785 mr->ibmr.rkey = mr->mmkey.key;
Eli Cohene126ba92013-07-07 17:25:49 +0300786 mr->umem = NULL;
787
788 return &mr->ibmr;
789
790err_in:
791 kfree(in);
792
793err_free:
794 kfree(mr);
795
796 return ERR_PTR(err);
797}
798
799static int get_octo_len(u64 addr, u64 len, int page_size)
800{
801 u64 offset;
802 int npages;
803
804 offset = addr & (page_size - 1);
805 npages = ALIGN(len + offset, page_size) >> ilog2(page_size);
806 return (npages + 1) / 2;
807}
808
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +0200809static int use_umr(struct mlx5_ib_dev *dev, int order)
Eli Cohene126ba92013-07-07 17:25:49 +0300810{
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +0200811 if (MLX5_CAP_GEN(dev->mdev, umr_extended_translation_offset))
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200812 return order <= MAX_UMR_CACHE_ENTRY + 2;
Haggai Erancc149f752014-12-11 17:04:21 +0200813 return order <= MLX5_MAX_UMR_SHIFT;
Eli Cohene126ba92013-07-07 17:25:49 +0300814}
815
Arnd Bergmann14ab8892016-10-24 22:48:21 +0200816static int mr_umem_get(struct ib_pd *pd, u64 start, u64 length,
817 int access_flags, struct ib_umem **umem,
818 int *npages, int *page_shift, int *ncont,
819 int *order)
Noa Osherovich395a8e42016-02-29 16:46:50 +0200820{
821 struct mlx5_ib_dev *dev = to_mdev(pd->device);
Arnd Bergmann14ab8892016-10-24 22:48:21 +0200822 int err;
823
824 *umem = ib_umem_get(pd->uobject->context, start, length,
825 access_flags, 0);
826 err = PTR_ERR_OR_ZERO(*umem);
827 if (err < 0) {
Noa Osherovich395a8e42016-02-29 16:46:50 +0200828 mlx5_ib_err(dev, "umem get failed (%ld)\n", PTR_ERR(umem));
Arnd Bergmann14ab8892016-10-24 22:48:21 +0200829 return err;
Noa Osherovich395a8e42016-02-29 16:46:50 +0200830 }
831
Arnd Bergmann14ab8892016-10-24 22:48:21 +0200832 mlx5_ib_cont_pages(*umem, start, MLX5_MKEY_PAGE_SHIFT_MASK, npages,
Majd Dibbiny762f8992016-10-27 16:36:47 +0300833 page_shift, ncont, order);
Noa Osherovich395a8e42016-02-29 16:46:50 +0200834 if (!*npages) {
835 mlx5_ib_warn(dev, "avoid zero region\n");
Arnd Bergmann14ab8892016-10-24 22:48:21 +0200836 ib_umem_release(*umem);
837 return -EINVAL;
Noa Osherovich395a8e42016-02-29 16:46:50 +0200838 }
839
840 mlx5_ib_dbg(dev, "npages %d, ncont %d, order %d, page_shift %d\n",
841 *npages, *ncont, *order, *page_shift);
842
Arnd Bergmann14ab8892016-10-24 22:48:21 +0200843 return 0;
Noa Osherovich395a8e42016-02-29 16:46:50 +0200844}
845
Christoph Hellwigadd08d72016-03-03 09:38:22 +0100846static void mlx5_ib_umr_done(struct ib_cq *cq, struct ib_wc *wc)
Eli Cohene126ba92013-07-07 17:25:49 +0300847{
Christoph Hellwigadd08d72016-03-03 09:38:22 +0100848 struct mlx5_ib_umr_context *context =
849 container_of(wc->wr_cqe, struct mlx5_ib_umr_context, cqe);
Eli Cohene126ba92013-07-07 17:25:49 +0300850
Christoph Hellwigadd08d72016-03-03 09:38:22 +0100851 context->status = wc->status;
852 complete(&context->done);
853}
Eli Cohene126ba92013-07-07 17:25:49 +0300854
Christoph Hellwigadd08d72016-03-03 09:38:22 +0100855static inline void mlx5_ib_init_umr_context(struct mlx5_ib_umr_context *context)
856{
857 context->cqe.done = mlx5_ib_umr_done;
858 context->status = -1;
859 init_completion(&context->done);
Eli Cohene126ba92013-07-07 17:25:49 +0300860}
861
Binoy Jayand5ea2df2017-01-02 11:37:40 +0200862static int mlx5_ib_post_send_wait(struct mlx5_ib_dev *dev,
863 struct mlx5_umr_wr *umrwr)
864{
865 struct umr_common *umrc = &dev->umrc;
866 struct ib_send_wr *bad;
867 int err;
868 struct mlx5_ib_umr_context umr_context;
869
870 mlx5_ib_init_umr_context(&umr_context);
871 umrwr->wr.wr_cqe = &umr_context.cqe;
872
873 down(&umrc->sem);
874 err = ib_post_send(umrc->qp, &umrwr->wr, &bad);
875 if (err) {
876 mlx5_ib_warn(dev, "UMR post send failed, err %d\n", err);
877 } else {
878 wait_for_completion(&umr_context.done);
879 if (umr_context.status != IB_WC_SUCCESS) {
880 mlx5_ib_warn(dev, "reg umr failed (%u)\n",
881 umr_context.status);
882 err = -EFAULT;
883 }
884 }
885 up(&umrc->sem);
886 return err;
887}
888
Eli Cohene126ba92013-07-07 17:25:49 +0300889static struct mlx5_ib_mr *reg_umr(struct ib_pd *pd, struct ib_umem *umem,
890 u64 virt_addr, u64 len, int npages,
891 int page_shift, int order, int access_flags)
892{
893 struct mlx5_ib_dev *dev = to_mdev(pd->device);
Eli Cohene126ba92013-07-07 17:25:49 +0300894 struct mlx5_ib_mr *mr;
Haggai Eran096f7e72014-05-22 14:50:08 +0300895 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300896 int i;
897
Eli Cohen746b5582013-10-23 09:53:14 +0300898 for (i = 0; i < 1; i++) {
Eli Cohene126ba92013-07-07 17:25:49 +0300899 mr = alloc_cached_mr(dev, order);
900 if (mr)
901 break;
902
903 err = add_keys(dev, order2idx(dev, order), 1);
Eli Cohen746b5582013-10-23 09:53:14 +0300904 if (err && err != -EAGAIN) {
905 mlx5_ib_warn(dev, "add_keys failed, err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +0300906 break;
907 }
908 }
909
910 if (!mr)
911 return ERR_PTR(-EAGAIN);
912
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +0200913 mr->ibmr.pd = pd;
914 mr->umem = umem;
915 mr->access_flags = access_flags;
916 mr->desc_size = sizeof(struct mlx5_mtt);
Matan Baraka606b0f2016-02-29 18:05:28 +0200917 mr->mmkey.iova = virt_addr;
918 mr->mmkey.size = len;
919 mr->mmkey.pd = to_mpd(pd)->pdn;
Haggai Eranb4755982014-05-22 14:50:10 +0300920
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +0200921 err = mlx5_ib_update_xlt(mr, 0, npages, page_shift,
922 MLX5_IB_UPD_XLT_ENABLE);
Haggai Eranb4cfe442014-12-11 17:04:26 +0200923
Haggai Eran096f7e72014-05-22 14:50:08 +0300924 if (err) {
Artemy Kovalyov49780d42017-01-18 16:58:10 +0200925 mlx5_mr_cache_free(dev, mr);
Haggai Eran096f7e72014-05-22 14:50:08 +0300926 return ERR_PTR(err);
Eli Cohene126ba92013-07-07 17:25:49 +0300927 }
928
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +0200929 mr->live = 1;
930
Eli Cohene126ba92013-07-07 17:25:49 +0300931 return mr;
Eli Cohene126ba92013-07-07 17:25:49 +0300932}
933
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +0200934static inline int populate_xlt(struct mlx5_ib_mr *mr, int idx, int npages,
935 void *xlt, int page_shift, size_t size,
936 int flags)
937{
938 struct mlx5_ib_dev *dev = mr->dev;
939 struct ib_umem *umem = mr->umem;
Artemy Kovalyov81713d32017-01-18 16:58:11 +0200940 if (flags & MLX5_IB_UPD_XLT_INDIRECT) {
941 mlx5_odp_populate_klm(xlt, idx, npages, mr, flags);
942 return npages;
943 }
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +0200944
945 npages = min_t(size_t, npages, ib_umem_num_pages(umem) - idx);
946
947 if (!(flags & MLX5_IB_UPD_XLT_ZAP)) {
948 __mlx5_ib_populate_pas(dev, umem, page_shift,
949 idx, npages, xlt,
950 MLX5_IB_MTT_PRESENT);
951 /* Clear padding after the pages
952 * brought from the umem.
953 */
954 memset(xlt + (npages * sizeof(struct mlx5_mtt)), 0,
955 size - npages * sizeof(struct mlx5_mtt));
956 }
957
958 return npages;
959}
960
961#define MLX5_MAX_UMR_CHUNK ((1 << (MLX5_MAX_UMR_SHIFT + 4)) - \
962 MLX5_UMR_MTT_ALIGNMENT)
963#define MLX5_SPARE_UMR_CHUNK 0x10000
964
965int mlx5_ib_update_xlt(struct mlx5_ib_mr *mr, u64 idx, int npages,
966 int page_shift, int flags)
Haggai Eran832a6b02014-12-11 17:04:22 +0200967{
968 struct mlx5_ib_dev *dev = mr->dev;
Bart Van Assche9b0c2892017-01-20 13:04:21 -0800969 struct device *ddev = dev->ib_dev.dev.parent;
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +0200970 struct mlx5_ib_ucontext *uctx = NULL;
Haggai Eran832a6b02014-12-11 17:04:22 +0200971 int size;
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +0200972 void *xlt;
Haggai Eran832a6b02014-12-11 17:04:22 +0200973 dma_addr_t dma;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100974 struct mlx5_umr_wr wr;
Haggai Eran832a6b02014-12-11 17:04:22 +0200975 struct ib_sge sg;
976 int err = 0;
Artemy Kovalyov81713d32017-01-18 16:58:11 +0200977 int desc_size = (flags & MLX5_IB_UPD_XLT_INDIRECT)
978 ? sizeof(struct mlx5_klm)
979 : sizeof(struct mlx5_mtt);
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +0200980 const int page_align = MLX5_UMR_MTT_ALIGNMENT / desc_size;
981 const int page_mask = page_align - 1;
Haggai Eran832a6b02014-12-11 17:04:22 +0200982 size_t pages_mapped = 0;
983 size_t pages_to_map = 0;
984 size_t pages_iter = 0;
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +0200985 gfp_t gfp;
Haggai Eran832a6b02014-12-11 17:04:22 +0200986
987 /* UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes,
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +0200988 * so we need to align the offset and length accordingly
989 */
990 if (idx & page_mask) {
991 npages += idx & page_mask;
992 idx &= ~page_mask;
Haggai Eran832a6b02014-12-11 17:04:22 +0200993 }
994
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +0200995 gfp = flags & MLX5_IB_UPD_XLT_ATOMIC ? GFP_ATOMIC : GFP_KERNEL;
996 gfp |= __GFP_ZERO | __GFP_NOWARN;
Haggai Eran832a6b02014-12-11 17:04:22 +0200997
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +0200998 pages_to_map = ALIGN(npages, page_align);
999 size = desc_size * pages_to_map;
1000 size = min_t(int, size, MLX5_MAX_UMR_CHUNK);
Haggai Eran832a6b02014-12-11 17:04:22 +02001001
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001002 xlt = (void *)__get_free_pages(gfp, get_order(size));
1003 if (!xlt && size > MLX5_SPARE_UMR_CHUNK) {
1004 mlx5_ib_dbg(dev, "Failed to allocate %d bytes of order %d. fallback to spare UMR allocation od %d bytes\n",
1005 size, get_order(size), MLX5_SPARE_UMR_CHUNK);
1006
1007 size = MLX5_SPARE_UMR_CHUNK;
1008 xlt = (void *)__get_free_pages(gfp, get_order(size));
Haggai Eran832a6b02014-12-11 17:04:22 +02001009 }
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001010
1011 if (!xlt) {
Artemy Kovalyovbd174fc2017-04-05 09:23:51 +03001012 uctx = to_mucontext(mr->ibmr.pd->uobject->context);
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001013 mlx5_ib_warn(dev, "Using XLT emergency buffer\n");
1014 size = PAGE_SIZE;
1015 xlt = (void *)uctx->upd_xlt_page;
1016 mutex_lock(&uctx->upd_xlt_page_mutex);
1017 memset(xlt, 0, size);
1018 }
1019 pages_iter = size / desc_size;
1020 dma = dma_map_single(ddev, xlt, size, DMA_TO_DEVICE);
Haggai Eran832a6b02014-12-11 17:04:22 +02001021 if (dma_mapping_error(ddev, dma)) {
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001022 mlx5_ib_err(dev, "unable to map DMA during XLT update.\n");
Haggai Eran832a6b02014-12-11 17:04:22 +02001023 err = -ENOMEM;
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001024 goto free_xlt;
Haggai Eran832a6b02014-12-11 17:04:22 +02001025 }
1026
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001027 sg.addr = dma;
1028 sg.lkey = dev->umrc.pd->local_dma_lkey;
1029
1030 memset(&wr, 0, sizeof(wr));
1031 wr.wr.send_flags = MLX5_IB_SEND_UMR_UPDATE_XLT;
1032 if (!(flags & MLX5_IB_UPD_XLT_ENABLE))
1033 wr.wr.send_flags |= MLX5_IB_SEND_UMR_FAIL_IF_FREE;
1034 wr.wr.sg_list = &sg;
1035 wr.wr.num_sge = 1;
1036 wr.wr.opcode = MLX5_IB_WR_UMR;
1037
1038 wr.pd = mr->ibmr.pd;
1039 wr.mkey = mr->mmkey.key;
1040 wr.length = mr->mmkey.size;
1041 wr.virt_addr = mr->mmkey.iova;
1042 wr.access_flags = mr->access_flags;
1043 wr.page_shift = page_shift;
1044
Haggai Eran832a6b02014-12-11 17:04:22 +02001045 for (pages_mapped = 0;
1046 pages_mapped < pages_to_map && !err;
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001047 pages_mapped += pages_iter, idx += pages_iter) {
Artemy Kovalyov438b2282017-04-05 09:23:52 +03001048 npages = min_t(int, pages_iter, pages_to_map - pages_mapped);
Haggai Eran832a6b02014-12-11 17:04:22 +02001049 dma_sync_single_for_cpu(ddev, dma, size, DMA_TO_DEVICE);
Artemy Kovalyov438b2282017-04-05 09:23:52 +03001050 npages = populate_xlt(mr, idx, npages, xlt,
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001051 page_shift, size, flags);
Haggai Eran832a6b02014-12-11 17:04:22 +02001052
1053 dma_sync_single_for_device(ddev, dma, size, DMA_TO_DEVICE);
1054
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001055 sg.length = ALIGN(npages * desc_size,
1056 MLX5_UMR_MTT_ALIGNMENT);
Haggai Eran832a6b02014-12-11 17:04:22 +02001057
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001058 if (pages_mapped + pages_iter >= pages_to_map) {
1059 if (flags & MLX5_IB_UPD_XLT_ENABLE)
1060 wr.wr.send_flags |=
1061 MLX5_IB_SEND_UMR_ENABLE_MR |
1062 MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS |
1063 MLX5_IB_SEND_UMR_UPDATE_TRANSLATION;
1064 if (flags & MLX5_IB_UPD_XLT_PD ||
1065 flags & MLX5_IB_UPD_XLT_ACCESS)
1066 wr.wr.send_flags |=
1067 MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS;
1068 if (flags & MLX5_IB_UPD_XLT_ADDR)
1069 wr.wr.send_flags |=
1070 MLX5_IB_SEND_UMR_UPDATE_TRANSLATION;
1071 }
Haggai Eran832a6b02014-12-11 17:04:22 +02001072
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001073 wr.offset = idx * desc_size;
Artemy Kovalyov31616252017-01-02 11:37:42 +02001074 wr.xlt_size = sg.length;
Haggai Eran832a6b02014-12-11 17:04:22 +02001075
Binoy Jayand5ea2df2017-01-02 11:37:40 +02001076 err = mlx5_ib_post_send_wait(dev, &wr);
Haggai Eran832a6b02014-12-11 17:04:22 +02001077 }
1078 dma_unmap_single(ddev, dma, size, DMA_TO_DEVICE);
1079
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001080free_xlt:
1081 if (uctx)
1082 mutex_unlock(&uctx->upd_xlt_page_mutex);
Haggai Eran832a6b02014-12-11 17:04:22 +02001083 else
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001084 free_pages((unsigned long)xlt, get_order(size));
Haggai Eran832a6b02014-12-11 17:04:22 +02001085
1086 return err;
1087}
Haggai Eran832a6b02014-12-11 17:04:22 +02001088
Noa Osherovich395a8e42016-02-29 16:46:50 +02001089/*
1090 * If ibmr is NULL it will be allocated by reg_create.
1091 * Else, the given ibmr will be used.
1092 */
1093static struct mlx5_ib_mr *reg_create(struct ib_mr *ibmr, struct ib_pd *pd,
1094 u64 virt_addr, u64 length,
1095 struct ib_umem *umem, int npages,
1096 int page_shift, int access_flags)
Eli Cohene126ba92013-07-07 17:25:49 +03001097{
1098 struct mlx5_ib_dev *dev = to_mdev(pd->device);
Eli Cohene126ba92013-07-07 17:25:49 +03001099 struct mlx5_ib_mr *mr;
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001100 __be64 *pas;
1101 void *mkc;
Eli Cohene126ba92013-07-07 17:25:49 +03001102 int inlen;
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001103 u32 *in;
Eli Cohene126ba92013-07-07 17:25:49 +03001104 int err;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001105 bool pg_cap = !!(MLX5_CAP_GEN(dev->mdev, pg));
Eli Cohene126ba92013-07-07 17:25:49 +03001106
Noa Osherovich395a8e42016-02-29 16:46:50 +02001107 mr = ibmr ? to_mmr(ibmr) : kzalloc(sizeof(*mr), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001108 if (!mr)
1109 return ERR_PTR(-ENOMEM);
1110
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001111 inlen = MLX5_ST_SZ_BYTES(create_mkey_in) +
1112 sizeof(*pas) * ((npages + 1) / 2) * 2;
Eli Cohene126ba92013-07-07 17:25:49 +03001113 in = mlx5_vzalloc(inlen);
1114 if (!in) {
1115 err = -ENOMEM;
1116 goto err_1;
1117 }
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001118 pas = (__be64 *)MLX5_ADDR_OF(create_mkey_in, in, klm_pas_mtt);
Artemy Kovalyovc438fde2017-01-02 11:37:43 +02001119 if (!(access_flags & IB_ACCESS_ON_DEMAND))
1120 mlx5_ib_populate_pas(dev, umem, page_shift, pas,
1121 pg_cap ? MLX5_IB_MTT_PRESENT : 0);
Eli Cohene126ba92013-07-07 17:25:49 +03001122
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001123 /* The pg_access bit allows setting the access flags
Haggai Erancc149f752014-12-11 17:04:21 +02001124 * in the page list submitted with the command. */
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001125 MLX5_SET(create_mkey_in, in, pg_access, !!(pg_cap));
1126
1127 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
1128 MLX5_SET(mkc, mkc, access_mode, MLX5_MKC_ACCESS_MODE_MTT);
1129 MLX5_SET(mkc, mkc, a, !!(access_flags & IB_ACCESS_REMOTE_ATOMIC));
1130 MLX5_SET(mkc, mkc, rw, !!(access_flags & IB_ACCESS_REMOTE_WRITE));
1131 MLX5_SET(mkc, mkc, rr, !!(access_flags & IB_ACCESS_REMOTE_READ));
1132 MLX5_SET(mkc, mkc, lw, !!(access_flags & IB_ACCESS_LOCAL_WRITE));
1133 MLX5_SET(mkc, mkc, lr, 1);
1134
1135 MLX5_SET64(mkc, mkc, start_addr, virt_addr);
1136 MLX5_SET64(mkc, mkc, len, length);
1137 MLX5_SET(mkc, mkc, pd, to_mpd(pd)->pdn);
1138 MLX5_SET(mkc, mkc, bsf_octword_size, 0);
1139 MLX5_SET(mkc, mkc, translations_octword_size,
1140 get_octo_len(virt_addr, length, 1 << page_shift));
1141 MLX5_SET(mkc, mkc, log_page_size, page_shift);
1142 MLX5_SET(mkc, mkc, qpn, 0xffffff);
1143 MLX5_SET(create_mkey_in, in, translations_octword_actual_size,
1144 get_octo_len(virt_addr, length, 1 << page_shift));
1145
1146 err = mlx5_core_create_mkey(dev->mdev, &mr->mmkey, in, inlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001147 if (err) {
1148 mlx5_ib_warn(dev, "create mkey failed\n");
1149 goto err_2;
1150 }
Artemy Kovalyovaa8e08d2017-01-02 11:37:48 +02001151 mr->mmkey.type = MLX5_MKEY_MR;
Artemy Kovalyov49780d42017-01-18 16:58:10 +02001152 mr->desc_size = sizeof(struct mlx5_mtt);
Eli Cohene126ba92013-07-07 17:25:49 +03001153 mr->umem = umem;
Majd Dibbiny7eae20d2015-01-06 13:56:01 +02001154 mr->dev = dev;
Haggai Eranb4cfe442014-12-11 17:04:26 +02001155 mr->live = 1;
Al Viro479163f2014-11-20 08:13:57 +00001156 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001157
Matan Baraka606b0f2016-02-29 18:05:28 +02001158 mlx5_ib_dbg(dev, "mkey = 0x%x\n", mr->mmkey.key);
Eli Cohene126ba92013-07-07 17:25:49 +03001159
1160 return mr;
1161
1162err_2:
Al Viro479163f2014-11-20 08:13:57 +00001163 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001164
1165err_1:
Noa Osherovich395a8e42016-02-29 16:46:50 +02001166 if (!ibmr)
1167 kfree(mr);
Eli Cohene126ba92013-07-07 17:25:49 +03001168
1169 return ERR_PTR(err);
1170}
1171
Noa Osherovich395a8e42016-02-29 16:46:50 +02001172static void set_mr_fileds(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr,
1173 int npages, u64 length, int access_flags)
1174{
1175 mr->npages = npages;
1176 atomic_add(npages, &dev->mdev->priv.reg_pages);
Matan Baraka606b0f2016-02-29 18:05:28 +02001177 mr->ibmr.lkey = mr->mmkey.key;
1178 mr->ibmr.rkey = mr->mmkey.key;
Noa Osherovich395a8e42016-02-29 16:46:50 +02001179 mr->ibmr.length = length;
Noa Osherovich56e11d62016-02-29 16:46:51 +02001180 mr->access_flags = access_flags;
Noa Osherovich395a8e42016-02-29 16:46:50 +02001181}
1182
Eli Cohene126ba92013-07-07 17:25:49 +03001183struct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
1184 u64 virt_addr, int access_flags,
1185 struct ib_udata *udata)
1186{
1187 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1188 struct mlx5_ib_mr *mr = NULL;
1189 struct ib_umem *umem;
1190 int page_shift;
1191 int npages;
1192 int ncont;
1193 int order;
1194 int err;
1195
Eli Cohen900a6d72014-09-14 16:47:51 +03001196 mlx5_ib_dbg(dev, "start 0x%llx, virt_addr 0x%llx, length 0x%llx, access_flags 0x%x\n",
1197 start, virt_addr, length, access_flags);
Artemy Kovalyov81713d32017-01-18 16:58:11 +02001198
1199#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1200 if (!start && length == U64_MAX) {
1201 if (!(access_flags & IB_ACCESS_ON_DEMAND) ||
1202 !(dev->odp_caps.general_caps & IB_ODP_SUPPORT_IMPLICIT))
1203 return ERR_PTR(-EINVAL);
1204
1205 mr = mlx5_ib_alloc_implicit_mr(to_mpd(pd), access_flags);
1206 return &mr->ibmr;
1207 }
1208#endif
1209
Arnd Bergmann14ab8892016-10-24 22:48:21 +02001210 err = mr_umem_get(pd, start, length, access_flags, &umem, &npages,
Noa Osherovich395a8e42016-02-29 16:46:50 +02001211 &page_shift, &ncont, &order);
1212
Arnd Bergmann14ab8892016-10-24 22:48:21 +02001213 if (err < 0)
1214 return ERR_PTR(err);
Eli Cohene126ba92013-07-07 17:25:49 +03001215
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001216 if (use_umr(dev, order)) {
Eli Cohene126ba92013-07-07 17:25:49 +03001217 mr = reg_umr(pd, umem, virt_addr, length, ncont, page_shift,
1218 order, access_flags);
1219 if (PTR_ERR(mr) == -EAGAIN) {
1220 mlx5_ib_dbg(dev, "cache empty for order %d", order);
1221 mr = NULL;
1222 }
Artemy Kovalyovc438fde2017-01-02 11:37:43 +02001223 } else if (access_flags & IB_ACCESS_ON_DEMAND &&
1224 !MLX5_CAP_GEN(dev->mdev, umr_extended_translation_offset)) {
Haggai Eran6aec21f2014-12-11 17:04:23 +02001225 err = -EINVAL;
1226 pr_err("Got MR registration for ODP MR > 512MB, not supported for Connect-IB");
1227 goto error;
Eli Cohene126ba92013-07-07 17:25:49 +03001228 }
1229
Moshe Lazer6bc1a652016-10-27 16:36:42 +03001230 if (!mr) {
1231 mutex_lock(&dev->slow_path_mutex);
Noa Osherovich395a8e42016-02-29 16:46:50 +02001232 mr = reg_create(NULL, pd, virt_addr, length, umem, ncont,
1233 page_shift, access_flags);
Moshe Lazer6bc1a652016-10-27 16:36:42 +03001234 mutex_unlock(&dev->slow_path_mutex);
1235 }
Eli Cohene126ba92013-07-07 17:25:49 +03001236
1237 if (IS_ERR(mr)) {
1238 err = PTR_ERR(mr);
1239 goto error;
1240 }
1241
Matan Baraka606b0f2016-02-29 18:05:28 +02001242 mlx5_ib_dbg(dev, "mkey 0x%x\n", mr->mmkey.key);
Eli Cohene126ba92013-07-07 17:25:49 +03001243
1244 mr->umem = umem;
Noa Osherovich395a8e42016-02-29 16:46:50 +02001245 set_mr_fileds(dev, mr, npages, length, access_flags);
Eli Cohene126ba92013-07-07 17:25:49 +03001246
Haggai Eranb4cfe442014-12-11 17:04:26 +02001247#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Noa Osherovich395a8e42016-02-29 16:46:50 +02001248 update_odp_mr(mr);
Haggai Eranb4cfe442014-12-11 17:04:26 +02001249#endif
1250
Eli Cohene126ba92013-07-07 17:25:49 +03001251 return &mr->ibmr;
1252
1253error:
1254 ib_umem_release(umem);
1255 return ERR_PTR(err);
1256}
1257
1258static int unreg_umr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
1259{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001260 struct mlx5_core_dev *mdev = dev->mdev;
Doug Ledford0025b0b2016-03-03 11:23:37 -05001261 struct mlx5_umr_wr umrwr = {};
Eli Cohene126ba92013-07-07 17:25:49 +03001262
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001263 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR)
1264 return 0;
1265
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001266 umrwr.wr.send_flags = MLX5_IB_SEND_UMR_DISABLE_MR |
1267 MLX5_IB_SEND_UMR_FAIL_IF_FREE;
1268 umrwr.wr.opcode = MLX5_IB_WR_UMR;
1269 umrwr.mkey = mr->mmkey.key;
Eli Cohene126ba92013-07-07 17:25:49 +03001270
Binoy Jayand5ea2df2017-01-02 11:37:40 +02001271 return mlx5_ib_post_send_wait(dev, &umrwr);
Eli Cohene126ba92013-07-07 17:25:49 +03001272}
1273
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001274static int rereg_umr(struct ib_pd *pd, struct mlx5_ib_mr *mr,
Noa Osherovich56e11d62016-02-29 16:46:51 +02001275 int access_flags, int flags)
1276{
1277 struct mlx5_ib_dev *dev = to_mdev(pd->device);
Noa Osherovich56e11d62016-02-29 16:46:51 +02001278 struct mlx5_umr_wr umrwr = {};
Noa Osherovich56e11d62016-02-29 16:46:51 +02001279 int err;
1280
Noa Osherovich56e11d62016-02-29 16:46:51 +02001281 umrwr.wr.send_flags = MLX5_IB_SEND_UMR_FAIL_IF_FREE;
1282
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001283 umrwr.wr.opcode = MLX5_IB_WR_UMR;
1284 umrwr.mkey = mr->mmkey.key;
Noa Osherovich56e11d62016-02-29 16:46:51 +02001285
Artemy Kovalyov31616252017-01-02 11:37:42 +02001286 if (flags & IB_MR_REREG_PD || flags & IB_MR_REREG_ACCESS) {
Noa Osherovich56e11d62016-02-29 16:46:51 +02001287 umrwr.pd = pd;
Noa Osherovich56e11d62016-02-29 16:46:51 +02001288 umrwr.access_flags = access_flags;
Artemy Kovalyov31616252017-01-02 11:37:42 +02001289 umrwr.wr.send_flags |= MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS;
Noa Osherovich56e11d62016-02-29 16:46:51 +02001290 }
1291
Binoy Jayand5ea2df2017-01-02 11:37:40 +02001292 err = mlx5_ib_post_send_wait(dev, &umrwr);
Noa Osherovich56e11d62016-02-29 16:46:51 +02001293
Noa Osherovich56e11d62016-02-29 16:46:51 +02001294 return err;
1295}
1296
1297int mlx5_ib_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start,
1298 u64 length, u64 virt_addr, int new_access_flags,
1299 struct ib_pd *new_pd, struct ib_udata *udata)
1300{
1301 struct mlx5_ib_dev *dev = to_mdev(ib_mr->device);
1302 struct mlx5_ib_mr *mr = to_mmr(ib_mr);
1303 struct ib_pd *pd = (flags & IB_MR_REREG_PD) ? new_pd : ib_mr->pd;
1304 int access_flags = flags & IB_MR_REREG_ACCESS ?
1305 new_access_flags :
1306 mr->access_flags;
1307 u64 addr = (flags & IB_MR_REREG_TRANS) ? virt_addr : mr->umem->address;
1308 u64 len = (flags & IB_MR_REREG_TRANS) ? length : mr->umem->length;
1309 int page_shift = 0;
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001310 int upd_flags = 0;
Noa Osherovich56e11d62016-02-29 16:46:51 +02001311 int npages = 0;
1312 int ncont = 0;
1313 int order = 0;
1314 int err;
1315
1316 mlx5_ib_dbg(dev, "start 0x%llx, virt_addr 0x%llx, length 0x%llx, access_flags 0x%x\n",
1317 start, virt_addr, length, access_flags);
1318
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001319 atomic_sub(mr->npages, &dev->mdev->priv.reg_pages);
1320
Noa Osherovich56e11d62016-02-29 16:46:51 +02001321 if (flags != IB_MR_REREG_PD) {
1322 /*
1323 * Replace umem. This needs to be done whether or not UMR is
1324 * used.
1325 */
1326 flags |= IB_MR_REREG_TRANS;
1327 ib_umem_release(mr->umem);
Arnd Bergmann14ab8892016-10-24 22:48:21 +02001328 err = mr_umem_get(pd, addr, len, access_flags, &mr->umem,
1329 &npages, &page_shift, &ncont, &order);
1330 if (err < 0) {
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001331 clean_mr(mr);
Noa Osherovich56e11d62016-02-29 16:46:51 +02001332 return err;
1333 }
1334 }
1335
1336 if (flags & IB_MR_REREG_TRANS && !use_umr_mtt_update(mr, addr, len)) {
1337 /*
1338 * UMR can't be used - MKey needs to be replaced.
1339 */
1340 if (mr->umred) {
1341 err = unreg_umr(dev, mr);
1342 if (err)
1343 mlx5_ib_warn(dev, "Failed to unregister MR\n");
1344 } else {
1345 err = destroy_mkey(dev, mr);
1346 if (err)
1347 mlx5_ib_warn(dev, "Failed to destroy MKey\n");
1348 }
1349 if (err)
1350 return err;
1351
1352 mr = reg_create(ib_mr, pd, addr, len, mr->umem, ncont,
1353 page_shift, access_flags);
1354
1355 if (IS_ERR(mr))
1356 return PTR_ERR(mr);
1357
1358 mr->umred = 0;
1359 } else {
1360 /*
1361 * Send a UMR WQE
1362 */
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001363 mr->ibmr.pd = pd;
1364 mr->access_flags = access_flags;
1365 mr->mmkey.iova = addr;
1366 mr->mmkey.size = len;
1367 mr->mmkey.pd = to_mpd(pd)->pdn;
1368
1369 if (flags & IB_MR_REREG_TRANS) {
1370 upd_flags = MLX5_IB_UPD_XLT_ADDR;
1371 if (flags & IB_MR_REREG_PD)
1372 upd_flags |= MLX5_IB_UPD_XLT_PD;
1373 if (flags & IB_MR_REREG_ACCESS)
1374 upd_flags |= MLX5_IB_UPD_XLT_ACCESS;
1375 err = mlx5_ib_update_xlt(mr, 0, npages, page_shift,
1376 upd_flags);
1377 } else {
1378 err = rereg_umr(pd, mr, access_flags, flags);
1379 }
1380
Noa Osherovich56e11d62016-02-29 16:46:51 +02001381 if (err) {
1382 mlx5_ib_warn(dev, "Failed to rereg UMR\n");
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001383 ib_umem_release(mr->umem);
1384 clean_mr(mr);
Noa Osherovich56e11d62016-02-29 16:46:51 +02001385 return err;
1386 }
1387 }
1388
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001389 set_mr_fileds(dev, mr, npages, len, access_flags);
Noa Osherovich56e11d62016-02-29 16:46:51 +02001390
Noa Osherovich56e11d62016-02-29 16:46:51 +02001391#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1392 update_odp_mr(mr);
1393#endif
Noa Osherovich56e11d62016-02-29 16:46:51 +02001394 return 0;
1395}
1396
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001397static int
1398mlx5_alloc_priv_descs(struct ib_device *device,
1399 struct mlx5_ib_mr *mr,
1400 int ndescs,
1401 int desc_size)
1402{
1403 int size = ndescs * desc_size;
1404 int add_size;
1405 int ret;
1406
1407 add_size = max_t(int, MLX5_UMR_ALIGN - ARCH_KMALLOC_MINALIGN, 0);
1408
1409 mr->descs_alloc = kzalloc(size + add_size, GFP_KERNEL);
1410 if (!mr->descs_alloc)
1411 return -ENOMEM;
1412
1413 mr->descs = PTR_ALIGN(mr->descs_alloc, MLX5_UMR_ALIGN);
1414
Bart Van Assche9b0c2892017-01-20 13:04:21 -08001415 mr->desc_map = dma_map_single(device->dev.parent, mr->descs,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001416 size, DMA_TO_DEVICE);
Bart Van Assche9b0c2892017-01-20 13:04:21 -08001417 if (dma_mapping_error(device->dev.parent, mr->desc_map)) {
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001418 ret = -ENOMEM;
1419 goto err;
1420 }
1421
1422 return 0;
1423err:
1424 kfree(mr->descs_alloc);
1425
1426 return ret;
1427}
1428
1429static void
1430mlx5_free_priv_descs(struct mlx5_ib_mr *mr)
1431{
1432 if (mr->descs) {
1433 struct ib_device *device = mr->ibmr.device;
1434 int size = mr->max_descs * mr->desc_size;
1435
Bart Van Assche9b0c2892017-01-20 13:04:21 -08001436 dma_unmap_single(device->dev.parent, mr->desc_map,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001437 size, DMA_TO_DEVICE);
1438 kfree(mr->descs_alloc);
1439 mr->descs = NULL;
1440 }
1441}
1442
Haggai Eran6aec21f2014-12-11 17:04:23 +02001443static int clean_mr(struct mlx5_ib_mr *mr)
Eli Cohene126ba92013-07-07 17:25:49 +03001444{
Haggai Eran6aec21f2014-12-11 17:04:23 +02001445 struct mlx5_ib_dev *dev = to_mdev(mr->ibmr.device);
Eli Cohene126ba92013-07-07 17:25:49 +03001446 int umred = mr->umred;
1447 int err;
1448
Sagi Grimberg8b91ffc2015-07-30 10:32:34 +03001449 if (mr->sig) {
1450 if (mlx5_core_destroy_psv(dev->mdev,
1451 mr->sig->psv_memory.psv_idx))
1452 mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
1453 mr->sig->psv_memory.psv_idx);
1454 if (mlx5_core_destroy_psv(dev->mdev,
1455 mr->sig->psv_wire.psv_idx))
1456 mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
1457 mr->sig->psv_wire.psv_idx);
1458 kfree(mr->sig);
1459 mr->sig = NULL;
1460 }
1461
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001462 mlx5_free_priv_descs(mr);
1463
Eli Cohene126ba92013-07-07 17:25:49 +03001464 if (!umred) {
Haggai Eranb4cfe442014-12-11 17:04:26 +02001465 err = destroy_mkey(dev, mr);
Eli Cohene126ba92013-07-07 17:25:49 +03001466 if (err) {
1467 mlx5_ib_warn(dev, "failed to destroy mkey 0x%x (%d)\n",
Matan Baraka606b0f2016-02-29 18:05:28 +02001468 mr->mmkey.key, err);
Eli Cohene126ba92013-07-07 17:25:49 +03001469 return err;
1470 }
1471 } else {
Artemy Kovalyov49780d42017-01-18 16:58:10 +02001472 mlx5_mr_cache_free(dev, mr);
Eli Cohene126ba92013-07-07 17:25:49 +03001473 }
1474
Eli Cohene126ba92013-07-07 17:25:49 +03001475 if (!umred)
1476 kfree(mr);
1477
1478 return 0;
1479}
1480
Haggai Eran6aec21f2014-12-11 17:04:23 +02001481int mlx5_ib_dereg_mr(struct ib_mr *ibmr)
1482{
1483 struct mlx5_ib_dev *dev = to_mdev(ibmr->device);
1484 struct mlx5_ib_mr *mr = to_mmr(ibmr);
1485 int npages = mr->npages;
1486 struct ib_umem *umem = mr->umem;
1487
1488#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Haggai Eranb4cfe442014-12-11 17:04:26 +02001489 if (umem && umem->odp_data) {
1490 /* Prevent new page faults from succeeding */
1491 mr->live = 0;
Haggai Eran6aec21f2014-12-11 17:04:23 +02001492 /* Wait for all running page-fault handlers to finish. */
1493 synchronize_srcu(&dev->mr_srcu);
Haggai Eranb4cfe442014-12-11 17:04:26 +02001494 /* Destroy all page mappings */
Artemy Kovalyov81713d32017-01-18 16:58:11 +02001495 if (umem->odp_data->page_list)
1496 mlx5_ib_invalidate_range(umem, ib_umem_start(umem),
1497 ib_umem_end(umem));
1498 else
1499 mlx5_ib_free_implicit_mr(mr);
Haggai Eranb4cfe442014-12-11 17:04:26 +02001500 /*
1501 * We kill the umem before the MR for ODP,
1502 * so that there will not be any invalidations in
1503 * flight, looking at the *mr struct.
1504 */
1505 ib_umem_release(umem);
1506 atomic_sub(npages, &dev->mdev->priv.reg_pages);
1507
1508 /* Avoid double-freeing the umem. */
1509 umem = NULL;
1510 }
Haggai Eran6aec21f2014-12-11 17:04:23 +02001511#endif
1512
1513 clean_mr(mr);
1514
1515 if (umem) {
1516 ib_umem_release(umem);
1517 atomic_sub(npages, &dev->mdev->priv.reg_pages);
1518 }
1519
1520 return 0;
1521}
1522
Sagi Grimberg9bee1782015-07-30 10:32:35 +03001523struct ib_mr *mlx5_ib_alloc_mr(struct ib_pd *pd,
1524 enum ib_mr_type mr_type,
1525 u32 max_num_sg)
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001526{
1527 struct mlx5_ib_dev *dev = to_mdev(pd->device);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001528 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
Sagi Grimbergb005d312016-02-29 19:07:33 +02001529 int ndescs = ALIGN(max_num_sg, 4);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001530 struct mlx5_ib_mr *mr;
1531 void *mkc;
1532 u32 *in;
Sagi Grimbergb005d312016-02-29 19:07:33 +02001533 int err;
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001534
1535 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1536 if (!mr)
1537 return ERR_PTR(-ENOMEM);
1538
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001539 in = kzalloc(inlen, GFP_KERNEL);
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001540 if (!in) {
1541 err = -ENOMEM;
1542 goto err_free;
1543 }
1544
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001545 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
1546 MLX5_SET(mkc, mkc, free, 1);
1547 MLX5_SET(mkc, mkc, translations_octword_size, ndescs);
1548 MLX5_SET(mkc, mkc, qpn, 0xffffff);
1549 MLX5_SET(mkc, mkc, pd, to_mpd(pd)->pdn);
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001550
Sagi Grimberg9bee1782015-07-30 10:32:35 +03001551 if (mr_type == IB_MR_TYPE_MEM_REG) {
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001552 mr->access_mode = MLX5_MKC_ACCESS_MODE_MTT;
1553 MLX5_SET(mkc, mkc, log_page_size, PAGE_SHIFT);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001554 err = mlx5_alloc_priv_descs(pd->device, mr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02001555 ndescs, sizeof(struct mlx5_mtt));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001556 if (err)
1557 goto err_free_in;
1558
Artemy Kovalyov31616252017-01-02 11:37:42 +02001559 mr->desc_size = sizeof(struct mlx5_mtt);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001560 mr->max_descs = ndescs;
Sagi Grimbergb005d312016-02-29 19:07:33 +02001561 } else if (mr_type == IB_MR_TYPE_SG_GAPS) {
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001562 mr->access_mode = MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimbergb005d312016-02-29 19:07:33 +02001563
1564 err = mlx5_alloc_priv_descs(pd->device, mr,
1565 ndescs, sizeof(struct mlx5_klm));
1566 if (err)
1567 goto err_free_in;
1568 mr->desc_size = sizeof(struct mlx5_klm);
1569 mr->max_descs = ndescs;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03001570 } else if (mr_type == IB_MR_TYPE_SIGNATURE) {
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001571 u32 psv_index[2];
1572
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001573 MLX5_SET(mkc, mkc, bsf_en, 1);
1574 MLX5_SET(mkc, mkc, bsf_octword_size, MLX5_MKEY_BSF_OCTO_SIZE);
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001575 mr->sig = kzalloc(sizeof(*mr->sig), GFP_KERNEL);
1576 if (!mr->sig) {
1577 err = -ENOMEM;
1578 goto err_free_in;
1579 }
1580
1581 /* create mem & wire PSVs */
Jack Morgenstein9603b612014-07-28 23:30:22 +03001582 err = mlx5_core_create_psv(dev->mdev, to_mpd(pd)->pdn,
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001583 2, psv_index);
1584 if (err)
1585 goto err_free_sig;
1586
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001587 mr->access_mode = MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001588 mr->sig->psv_memory.psv_idx = psv_index[0];
1589 mr->sig->psv_wire.psv_idx = psv_index[1];
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02001590
1591 mr->sig->sig_status_checked = true;
1592 mr->sig->sig_err_exists = false;
1593 /* Next UMR, Arm SIGERR */
1594 ++mr->sig->sigerr_count;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03001595 } else {
1596 mlx5_ib_warn(dev, "Invalid mr type %d\n", mr_type);
1597 err = -EINVAL;
1598 goto err_free_in;
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001599 }
1600
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001601 MLX5_SET(mkc, mkc, access_mode, mr->access_mode);
1602 MLX5_SET(mkc, mkc, umr_en, 1);
1603
1604 err = mlx5_core_create_mkey(dev->mdev, &mr->mmkey, in, inlen);
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001605 if (err)
1606 goto err_destroy_psv;
1607
Artemy Kovalyovaa8e08d2017-01-02 11:37:48 +02001608 mr->mmkey.type = MLX5_MKEY_MR;
Matan Baraka606b0f2016-02-29 18:05:28 +02001609 mr->ibmr.lkey = mr->mmkey.key;
1610 mr->ibmr.rkey = mr->mmkey.key;
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001611 mr->umem = NULL;
1612 kfree(in);
1613
1614 return &mr->ibmr;
1615
1616err_destroy_psv:
1617 if (mr->sig) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001618 if (mlx5_core_destroy_psv(dev->mdev,
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001619 mr->sig->psv_memory.psv_idx))
1620 mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
1621 mr->sig->psv_memory.psv_idx);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001622 if (mlx5_core_destroy_psv(dev->mdev,
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001623 mr->sig->psv_wire.psv_idx))
1624 mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
1625 mr->sig->psv_wire.psv_idx);
1626 }
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001627 mlx5_free_priv_descs(mr);
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001628err_free_sig:
1629 kfree(mr->sig);
1630err_free_in:
1631 kfree(in);
1632err_free:
1633 kfree(mr);
1634 return ERR_PTR(err);
1635}
1636
Matan Barakd2370e02016-02-29 18:05:30 +02001637struct ib_mw *mlx5_ib_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
1638 struct ib_udata *udata)
1639{
1640 struct mlx5_ib_dev *dev = to_mdev(pd->device);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001641 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
Matan Barakd2370e02016-02-29 18:05:30 +02001642 struct mlx5_ib_mw *mw = NULL;
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001643 u32 *in = NULL;
1644 void *mkc;
Matan Barakd2370e02016-02-29 18:05:30 +02001645 int ndescs;
1646 int err;
1647 struct mlx5_ib_alloc_mw req = {};
1648 struct {
1649 __u32 comp_mask;
1650 __u32 response_length;
1651 } resp = {};
1652
1653 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1654 if (err)
1655 return ERR_PTR(err);
1656
1657 if (req.comp_mask || req.reserved1 || req.reserved2)
1658 return ERR_PTR(-EOPNOTSUPP);
1659
1660 if (udata->inlen > sizeof(req) &&
1661 !ib_is_udata_cleared(udata, sizeof(req),
1662 udata->inlen - sizeof(req)))
1663 return ERR_PTR(-EOPNOTSUPP);
1664
1665 ndescs = req.num_klms ? roundup(req.num_klms, 4) : roundup(1, 4);
1666
1667 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001668 in = kzalloc(inlen, GFP_KERNEL);
Matan Barakd2370e02016-02-29 18:05:30 +02001669 if (!mw || !in) {
1670 err = -ENOMEM;
1671 goto free;
1672 }
1673
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001674 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
Matan Barakd2370e02016-02-29 18:05:30 +02001675
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001676 MLX5_SET(mkc, mkc, free, 1);
1677 MLX5_SET(mkc, mkc, translations_octword_size, ndescs);
1678 MLX5_SET(mkc, mkc, pd, to_mpd(pd)->pdn);
1679 MLX5_SET(mkc, mkc, umr_en, 1);
1680 MLX5_SET(mkc, mkc, lr, 1);
1681 MLX5_SET(mkc, mkc, access_mode, MLX5_MKC_ACCESS_MODE_KLMS);
1682 MLX5_SET(mkc, mkc, en_rinval, !!((type == IB_MW_TYPE_2)));
1683 MLX5_SET(mkc, mkc, qpn, 0xffffff);
1684
1685 err = mlx5_core_create_mkey(dev->mdev, &mw->mmkey, in, inlen);
Matan Barakd2370e02016-02-29 18:05:30 +02001686 if (err)
1687 goto free;
1688
Artemy Kovalyovaa8e08d2017-01-02 11:37:48 +02001689 mw->mmkey.type = MLX5_MKEY_MW;
Matan Barakd2370e02016-02-29 18:05:30 +02001690 mw->ibmw.rkey = mw->mmkey.key;
Artemy Kovalyovdb570d72017-04-05 09:23:59 +03001691 mw->ndescs = ndescs;
Matan Barakd2370e02016-02-29 18:05:30 +02001692
1693 resp.response_length = min(offsetof(typeof(resp), response_length) +
1694 sizeof(resp.response_length), udata->outlen);
1695 if (resp.response_length) {
1696 err = ib_copy_to_udata(udata, &resp, resp.response_length);
1697 if (err) {
1698 mlx5_core_destroy_mkey(dev->mdev, &mw->mmkey);
1699 goto free;
1700 }
1701 }
1702
1703 kfree(in);
1704 return &mw->ibmw;
1705
1706free:
1707 kfree(mw);
1708 kfree(in);
1709 return ERR_PTR(err);
1710}
1711
1712int mlx5_ib_dealloc_mw(struct ib_mw *mw)
1713{
1714 struct mlx5_ib_mw *mmw = to_mmw(mw);
1715 int err;
1716
1717 err = mlx5_core_destroy_mkey((to_mdev(mw->device))->mdev,
1718 &mmw->mmkey);
1719 if (!err)
1720 kfree(mmw);
1721 return err;
1722}
1723
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02001724int mlx5_ib_check_mr_status(struct ib_mr *ibmr, u32 check_mask,
1725 struct ib_mr_status *mr_status)
1726{
1727 struct mlx5_ib_mr *mmr = to_mmr(ibmr);
1728 int ret = 0;
1729
1730 if (check_mask & ~IB_MR_CHECK_SIG_STATUS) {
1731 pr_err("Invalid status check mask\n");
1732 ret = -EINVAL;
1733 goto done;
1734 }
1735
1736 mr_status->fail_status = 0;
1737 if (check_mask & IB_MR_CHECK_SIG_STATUS) {
1738 if (!mmr->sig) {
1739 ret = -EINVAL;
1740 pr_err("signature status check requested on a non-signature enabled MR\n");
1741 goto done;
1742 }
1743
1744 mmr->sig->sig_status_checked = true;
1745 if (!mmr->sig->sig_err_exists)
1746 goto done;
1747
1748 if (ibmr->lkey == mmr->sig->err_item.key)
1749 memcpy(&mr_status->sig_err, &mmr->sig->err_item,
1750 sizeof(mr_status->sig_err));
1751 else {
1752 mr_status->sig_err.err_type = IB_SIG_BAD_GUARD;
1753 mr_status->sig_err.sig_err_offset = 0;
1754 mr_status->sig_err.key = mmr->sig->err_item.key;
1755 }
1756
1757 mmr->sig->sig_err_exists = false;
1758 mr_status->fail_status |= IB_MR_CHECK_SIG_STATUS;
1759 }
1760
1761done:
1762 return ret;
1763}
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001764
Sagi Grimbergb005d312016-02-29 19:07:33 +02001765static int
1766mlx5_ib_sg_to_klms(struct mlx5_ib_mr *mr,
1767 struct scatterlist *sgl,
Christoph Hellwigff2ba992016-05-03 18:01:04 +02001768 unsigned short sg_nents,
Bart Van Assche9aa8b322016-05-12 10:49:15 -07001769 unsigned int *sg_offset_p)
Sagi Grimbergb005d312016-02-29 19:07:33 +02001770{
1771 struct scatterlist *sg = sgl;
1772 struct mlx5_klm *klms = mr->descs;
Bart Van Assche9aa8b322016-05-12 10:49:15 -07001773 unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
Sagi Grimbergb005d312016-02-29 19:07:33 +02001774 u32 lkey = mr->ibmr.pd->local_dma_lkey;
1775 int i;
1776
Christoph Hellwigff2ba992016-05-03 18:01:04 +02001777 mr->ibmr.iova = sg_dma_address(sg) + sg_offset;
Sagi Grimbergb005d312016-02-29 19:07:33 +02001778 mr->ibmr.length = 0;
1779 mr->ndescs = sg_nents;
1780
1781 for_each_sg(sgl, sg, sg_nents, i) {
1782 if (unlikely(i > mr->max_descs))
1783 break;
Christoph Hellwigff2ba992016-05-03 18:01:04 +02001784 klms[i].va = cpu_to_be64(sg_dma_address(sg) + sg_offset);
1785 klms[i].bcount = cpu_to_be32(sg_dma_len(sg) - sg_offset);
Sagi Grimbergb005d312016-02-29 19:07:33 +02001786 klms[i].key = cpu_to_be32(lkey);
1787 mr->ibmr.length += sg_dma_len(sg);
Christoph Hellwigff2ba992016-05-03 18:01:04 +02001788
1789 sg_offset = 0;
Sagi Grimbergb005d312016-02-29 19:07:33 +02001790 }
1791
Bart Van Assche9aa8b322016-05-12 10:49:15 -07001792 if (sg_offset_p)
1793 *sg_offset_p = sg_offset;
1794
Sagi Grimbergb005d312016-02-29 19:07:33 +02001795 return i;
1796}
1797
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001798static int mlx5_set_page(struct ib_mr *ibmr, u64 addr)
1799{
1800 struct mlx5_ib_mr *mr = to_mmr(ibmr);
1801 __be64 *descs;
1802
1803 if (unlikely(mr->ndescs == mr->max_descs))
1804 return -ENOMEM;
1805
1806 descs = mr->descs;
1807 descs[mr->ndescs++] = cpu_to_be64(addr | MLX5_EN_RD | MLX5_EN_WR);
1808
1809 return 0;
1810}
1811
Christoph Hellwigff2ba992016-05-03 18:01:04 +02001812int mlx5_ib_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
Bart Van Assche9aa8b322016-05-12 10:49:15 -07001813 unsigned int *sg_offset)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001814{
1815 struct mlx5_ib_mr *mr = to_mmr(ibmr);
1816 int n;
1817
1818 mr->ndescs = 0;
1819
1820 ib_dma_sync_single_for_cpu(ibmr->device, mr->desc_map,
1821 mr->desc_size * mr->max_descs,
1822 DMA_TO_DEVICE);
1823
Saeed Mahameedec22eb52016-07-16 06:28:36 +03001824 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Christoph Hellwigff2ba992016-05-03 18:01:04 +02001825 n = mlx5_ib_sg_to_klms(mr, sg, sg_nents, sg_offset);
Sagi Grimbergb005d312016-02-29 19:07:33 +02001826 else
Christoph Hellwigff2ba992016-05-03 18:01:04 +02001827 n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset,
1828 mlx5_set_page);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001829
1830 ib_dma_sync_single_for_device(ibmr->device, mr->desc_map,
1831 mr->desc_size * mr->max_descs,
1832 DMA_TO_DEVICE);
1833
1834 return n;
1835}