blob: 70a047dde69e10575d61cfc98872544d2b1349f7 [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"
Matan Barakd2370e02016-02-29 18:05:30 +020043#include "user.h"
Eli Cohene126ba92013-07-07 17:25:49 +030044
45enum {
Eli Cohen746b5582013-10-23 09:53:14 +030046 MAX_PENDING_REG_MR = 8,
Eli Cohene126ba92013-07-07 17:25:49 +030047};
48
Haggai Eran832a6b02014-12-11 17:04:22 +020049#define MLX5_UMR_ALIGN 2048
50#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
51static __be64 mlx5_ib_update_mtt_emergency_buffer[
52 MLX5_UMR_MTT_MIN_CHUNK_SIZE/sizeof(__be64)]
53 __aligned(MLX5_UMR_ALIGN);
54static DEFINE_MUTEX(mlx5_ib_update_mtt_emergency_buffer_mutex);
55#endif
Eli Cohenfe45f822013-09-11 16:35:35 +030056
Haggai Eran6aec21f2014-12-11 17:04:23 +020057static int clean_mr(struct mlx5_ib_mr *mr);
58
Haggai Eranb4cfe442014-12-11 17:04:26 +020059static int destroy_mkey(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
60{
Matan Baraka606b0f2016-02-29 18:05:28 +020061 int err = mlx5_core_destroy_mkey(dev->mdev, &mr->mmkey);
Haggai Eranb4cfe442014-12-11 17:04:26 +020062
63#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
64 /* Wait until all page fault handlers using the mr complete. */
65 synchronize_srcu(&dev->mr_srcu);
66#endif
67
68 return err;
69}
70
Eli Cohene126ba92013-07-07 17:25:49 +030071static int order2idx(struct mlx5_ib_dev *dev, int order)
72{
73 struct mlx5_mr_cache *cache = &dev->cache;
74
75 if (order < cache->ent[0].order)
76 return 0;
77 else
78 return order - cache->ent[0].order;
79}
80
Noa Osherovich56e11d62016-02-29 16:46:51 +020081static bool use_umr_mtt_update(struct mlx5_ib_mr *mr, u64 start, u64 length)
82{
83 return ((u64)1 << mr->order) * MLX5_ADAPTER_PAGE_SIZE >=
84 length + (start & (MLX5_ADAPTER_PAGE_SIZE - 1));
85}
86
Noa Osherovich395a8e42016-02-29 16:46:50 +020087#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
88static void update_odp_mr(struct mlx5_ib_mr *mr)
89{
90 if (mr->umem->odp_data) {
91 /*
92 * This barrier prevents the compiler from moving the
93 * setting of umem->odp_data->private to point to our
94 * MR, before reg_umr finished, to ensure that the MR
95 * initialization have finished before starting to
96 * handle invalidations.
97 */
98 smp_wmb();
99 mr->umem->odp_data->private = mr;
100 /*
101 * Make sure we will see the new
102 * umem->odp_data->private value in the invalidation
103 * routines, before we can get page faults on the
104 * MR. Page faults can happen once we put the MR in
105 * the tree, below this line. Without the barrier,
106 * there can be a fault handling and an invalidation
107 * before umem->odp_data->private == mr is visible to
108 * the invalidation handler.
109 */
110 smp_wmb();
111 }
112}
113#endif
114
Eli Cohen746b5582013-10-23 09:53:14 +0300115static void reg_mr_callback(int status, void *context)
116{
117 struct mlx5_ib_mr *mr = context;
118 struct mlx5_ib_dev *dev = mr->dev;
119 struct mlx5_mr_cache *cache = &dev->cache;
120 int c = order2idx(dev, mr->order);
121 struct mlx5_cache_ent *ent = &cache->ent[c];
122 u8 key;
Eli Cohen746b5582013-10-23 09:53:14 +0300123 unsigned long flags;
Matan Baraka606b0f2016-02-29 18:05:28 +0200124 struct mlx5_mkey_table *table = &dev->mdev->priv.mkey_table;
Haggai Eran86059332014-05-22 14:50:09 +0300125 int err;
Eli Cohen746b5582013-10-23 09:53:14 +0300126
Eli Cohen746b5582013-10-23 09:53:14 +0300127 spin_lock_irqsave(&ent->lock, flags);
128 ent->pending--;
129 spin_unlock_irqrestore(&ent->lock, flags);
130 if (status) {
131 mlx5_ib_warn(dev, "async reg mr failed. status %d\n", status);
132 kfree(mr);
133 dev->fill_delay = 1;
134 mod_timer(&dev->delay_timer, jiffies + HZ);
135 return;
136 }
137
138 if (mr->out.hdr.status) {
139 mlx5_ib_warn(dev, "failed - status %d, syndorme 0x%x\n",
140 mr->out.hdr.status,
141 be32_to_cpu(mr->out.hdr.syndrome));
142 kfree(mr);
143 dev->fill_delay = 1;
144 mod_timer(&dev->delay_timer, jiffies + HZ);
145 return;
146 }
147
Jack Morgenstein9603b612014-07-28 23:30:22 +0300148 spin_lock_irqsave(&dev->mdev->priv.mkey_lock, flags);
149 key = dev->mdev->priv.mkey_key++;
150 spin_unlock_irqrestore(&dev->mdev->priv.mkey_lock, flags);
Matan Baraka606b0f2016-02-29 18:05:28 +0200151 mr->mmkey.key = mlx5_idx_to_mkey(be32_to_cpu(mr->out.mkey) & 0xffffff) | key;
Eli Cohen746b5582013-10-23 09:53:14 +0300152
153 cache->last_add = jiffies;
154
155 spin_lock_irqsave(&ent->lock, flags);
156 list_add_tail(&mr->list, &ent->head);
157 ent->cur++;
158 ent->size++;
159 spin_unlock_irqrestore(&ent->lock, flags);
Haggai Eran86059332014-05-22 14:50:09 +0300160
161 write_lock_irqsave(&table->lock, flags);
Matan Baraka606b0f2016-02-29 18:05:28 +0200162 err = radix_tree_insert(&table->tree, mlx5_base_mkey(mr->mmkey.key),
163 &mr->mmkey);
Haggai Eran86059332014-05-22 14:50:09 +0300164 if (err)
Matan Baraka606b0f2016-02-29 18:05:28 +0200165 pr_err("Error inserting to mkey tree. 0x%x\n", -err);
Haggai Eran86059332014-05-22 14:50:09 +0300166 write_unlock_irqrestore(&table->lock, flags);
Eli Cohen746b5582013-10-23 09:53:14 +0300167}
168
Eli Cohene126ba92013-07-07 17:25:49 +0300169static int add_keys(struct mlx5_ib_dev *dev, int c, int num)
170{
Eli Cohene126ba92013-07-07 17:25:49 +0300171 struct mlx5_mr_cache *cache = &dev->cache;
172 struct mlx5_cache_ent *ent = &cache->ent[c];
173 struct mlx5_create_mkey_mbox_in *in;
174 struct mlx5_ib_mr *mr;
175 int npages = 1 << ent->order;
Eli Cohene126ba92013-07-07 17:25:49 +0300176 int err = 0;
177 int i;
178
179 in = kzalloc(sizeof(*in), GFP_KERNEL);
180 if (!in)
181 return -ENOMEM;
182
183 for (i = 0; i < num; i++) {
Eli Cohen746b5582013-10-23 09:53:14 +0300184 if (ent->pending >= MAX_PENDING_REG_MR) {
185 err = -EAGAIN;
186 break;
187 }
188
Eli Cohene126ba92013-07-07 17:25:49 +0300189 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
190 if (!mr) {
191 err = -ENOMEM;
Eli Cohen746b5582013-10-23 09:53:14 +0300192 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300193 }
194 mr->order = ent->order;
195 mr->umred = 1;
Eli Cohen746b5582013-10-23 09:53:14 +0300196 mr->dev = dev;
Haggai Eran968e78d2014-12-11 17:04:11 +0200197 in->seg.status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +0300198 in->seg.xlt_oct_size = cpu_to_be32((npages + 1) / 2);
199 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
200 in->seg.flags = MLX5_ACCESS_MODE_MTT | MLX5_PERM_UMR_EN;
201 in->seg.log2_page_size = 12;
202
Eli Cohen746b5582013-10-23 09:53:14 +0300203 spin_lock_irq(&ent->lock);
204 ent->pending++;
205 spin_unlock_irq(&ent->lock);
Matan Baraka606b0f2016-02-29 18:05:28 +0200206 err = mlx5_core_create_mkey(dev->mdev, &mr->mmkey, in,
Eli Cohen746b5582013-10-23 09:53:14 +0300207 sizeof(*in), reg_mr_callback,
208 mr, &mr->out);
Eli Cohene126ba92013-07-07 17:25:49 +0300209 if (err) {
Eli Cohend14e7112014-12-02 12:26:19 +0200210 spin_lock_irq(&ent->lock);
211 ent->pending--;
212 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300213 mlx5_ib_warn(dev, "create mkey failed %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +0300214 kfree(mr);
Eli Cohen746b5582013-10-23 09:53:14 +0300215 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300216 }
Eli Cohene126ba92013-07-07 17:25:49 +0300217 }
218
Eli Cohene126ba92013-07-07 17:25:49 +0300219 kfree(in);
220 return err;
221}
222
223static void remove_keys(struct mlx5_ib_dev *dev, int c, int num)
224{
Eli Cohene126ba92013-07-07 17:25:49 +0300225 struct mlx5_mr_cache *cache = &dev->cache;
226 struct mlx5_cache_ent *ent = &cache->ent[c];
227 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +0300228 int err;
229 int i;
230
231 for (i = 0; i < num; i++) {
Eli Cohen746b5582013-10-23 09:53:14 +0300232 spin_lock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300233 if (list_empty(&ent->head)) {
Eli Cohen746b5582013-10-23 09:53:14 +0300234 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300235 return;
236 }
237 mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
238 list_del(&mr->list);
239 ent->cur--;
240 ent->size--;
Eli Cohen746b5582013-10-23 09:53:14 +0300241 spin_unlock_irq(&ent->lock);
Haggai Eranb4cfe442014-12-11 17:04:26 +0200242 err = destroy_mkey(dev, mr);
Eli Cohen203099f2013-09-11 16:35:26 +0300243 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +0300244 mlx5_ib_warn(dev, "failed destroy mkey\n");
Eli Cohen203099f2013-09-11 16:35:26 +0300245 else
Eli Cohene126ba92013-07-07 17:25:49 +0300246 kfree(mr);
Eli Cohene126ba92013-07-07 17:25:49 +0300247 }
248}
249
250static ssize_t size_write(struct file *filp, const char __user *buf,
251 size_t count, loff_t *pos)
252{
253 struct mlx5_cache_ent *ent = filp->private_data;
254 struct mlx5_ib_dev *dev = ent->dev;
255 char lbuf[20];
256 u32 var;
257 int err;
258 int c;
259
260 if (copy_from_user(lbuf, buf, sizeof(lbuf)))
Dan Carpenter5e631a02013-07-10 13:58:59 +0300261 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +0300262
263 c = order2idx(dev, ent->order);
264 lbuf[sizeof(lbuf) - 1] = 0;
265
266 if (sscanf(lbuf, "%u", &var) != 1)
267 return -EINVAL;
268
269 if (var < ent->limit)
270 return -EINVAL;
271
272 if (var > ent->size) {
Eli Cohen746b5582013-10-23 09:53:14 +0300273 do {
274 err = add_keys(dev, c, var - ent->size);
275 if (err && err != -EAGAIN)
276 return err;
277
278 usleep_range(3000, 5000);
279 } while (err);
Eli Cohene126ba92013-07-07 17:25:49 +0300280 } else if (var < ent->size) {
281 remove_keys(dev, c, ent->size - var);
282 }
283
284 return count;
285}
286
287static ssize_t size_read(struct file *filp, char __user *buf, size_t count,
288 loff_t *pos)
289{
290 struct mlx5_cache_ent *ent = filp->private_data;
291 char lbuf[20];
292 int err;
293
294 if (*pos)
295 return 0;
296
297 err = snprintf(lbuf, sizeof(lbuf), "%d\n", ent->size);
298 if (err < 0)
299 return err;
300
301 if (copy_to_user(buf, lbuf, err))
Dan Carpenter5e631a02013-07-10 13:58:59 +0300302 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +0300303
304 *pos += err;
305
306 return err;
307}
308
309static const struct file_operations size_fops = {
310 .owner = THIS_MODULE,
311 .open = simple_open,
312 .write = size_write,
313 .read = size_read,
314};
315
316static ssize_t limit_write(struct file *filp, const char __user *buf,
317 size_t count, loff_t *pos)
318{
319 struct mlx5_cache_ent *ent = filp->private_data;
320 struct mlx5_ib_dev *dev = ent->dev;
321 char lbuf[20];
322 u32 var;
323 int err;
324 int c;
325
326 if (copy_from_user(lbuf, buf, sizeof(lbuf)))
Dan Carpenter5e631a02013-07-10 13:58:59 +0300327 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +0300328
329 c = order2idx(dev, ent->order);
330 lbuf[sizeof(lbuf) - 1] = 0;
331
332 if (sscanf(lbuf, "%u", &var) != 1)
333 return -EINVAL;
334
335 if (var > ent->size)
336 return -EINVAL;
337
338 ent->limit = var;
339
340 if (ent->cur < ent->limit) {
341 err = add_keys(dev, c, 2 * ent->limit - ent->cur);
342 if (err)
343 return err;
344 }
345
346 return count;
347}
348
349static ssize_t limit_read(struct file *filp, char __user *buf, size_t count,
350 loff_t *pos)
351{
352 struct mlx5_cache_ent *ent = filp->private_data;
353 char lbuf[20];
354 int err;
355
356 if (*pos)
357 return 0;
358
359 err = snprintf(lbuf, sizeof(lbuf), "%d\n", ent->limit);
360 if (err < 0)
361 return err;
362
363 if (copy_to_user(buf, lbuf, err))
Dan Carpenter5e631a02013-07-10 13:58:59 +0300364 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +0300365
366 *pos += err;
367
368 return err;
369}
370
371static const struct file_operations limit_fops = {
372 .owner = THIS_MODULE,
373 .open = simple_open,
374 .write = limit_write,
375 .read = limit_read,
376};
377
378static int someone_adding(struct mlx5_mr_cache *cache)
379{
380 int i;
381
382 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
383 if (cache->ent[i].cur < cache->ent[i].limit)
384 return 1;
385 }
386
387 return 0;
388}
389
390static void __cache_work_func(struct mlx5_cache_ent *ent)
391{
392 struct mlx5_ib_dev *dev = ent->dev;
393 struct mlx5_mr_cache *cache = &dev->cache;
394 int i = order2idx(dev, ent->order);
Eli Cohen746b5582013-10-23 09:53:14 +0300395 int err;
Eli Cohene126ba92013-07-07 17:25:49 +0300396
397 if (cache->stopped)
398 return;
399
400 ent = &dev->cache.ent[i];
Eli Cohen746b5582013-10-23 09:53:14 +0300401 if (ent->cur < 2 * ent->limit && !dev->fill_delay) {
402 err = add_keys(dev, i, 1);
403 if (ent->cur < 2 * ent->limit) {
404 if (err == -EAGAIN) {
405 mlx5_ib_dbg(dev, "returned eagain, order %d\n",
406 i + 2);
407 queue_delayed_work(cache->wq, &ent->dwork,
408 msecs_to_jiffies(3));
409 } else if (err) {
410 mlx5_ib_warn(dev, "command failed order %d, err %d\n",
411 i + 2, err);
412 queue_delayed_work(cache->wq, &ent->dwork,
413 msecs_to_jiffies(1000));
414 } else {
415 queue_work(cache->wq, &ent->work);
416 }
417 }
Eli Cohene126ba92013-07-07 17:25:49 +0300418 } else if (ent->cur > 2 * ent->limit) {
Leon Romanovskyab5cdc32015-10-21 09:21:17 +0300419 /*
420 * The remove_keys() logic is performed as garbage collection
421 * task. Such task is intended to be run when no other active
422 * processes are running.
423 *
424 * The need_resched() will return TRUE if there are user tasks
425 * to be activated in near future.
426 *
427 * In such case, we don't execute remove_keys() and postpone
428 * the garbage collection work to try to run in next cycle,
429 * in order to free CPU resources to other tasks.
430 */
431 if (!need_resched() && !someone_adding(cache) &&
Eli Cohen746b5582013-10-23 09:53:14 +0300432 time_after(jiffies, cache->last_add + 300 * HZ)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300433 remove_keys(dev, i, 1);
434 if (ent->cur > ent->limit)
435 queue_work(cache->wq, &ent->work);
436 } else {
Eli Cohen746b5582013-10-23 09:53:14 +0300437 queue_delayed_work(cache->wq, &ent->dwork, 300 * HZ);
Eli Cohene126ba92013-07-07 17:25:49 +0300438 }
439 }
440}
441
442static void delayed_cache_work_func(struct work_struct *work)
443{
444 struct mlx5_cache_ent *ent;
445
446 ent = container_of(work, struct mlx5_cache_ent, dwork.work);
447 __cache_work_func(ent);
448}
449
450static void cache_work_func(struct work_struct *work)
451{
452 struct mlx5_cache_ent *ent;
453
454 ent = container_of(work, struct mlx5_cache_ent, work);
455 __cache_work_func(ent);
456}
457
458static struct mlx5_ib_mr *alloc_cached_mr(struct mlx5_ib_dev *dev, int order)
459{
460 struct mlx5_mr_cache *cache = &dev->cache;
461 struct mlx5_ib_mr *mr = NULL;
462 struct mlx5_cache_ent *ent;
463 int c;
464 int i;
465
466 c = order2idx(dev, order);
467 if (c < 0 || c >= MAX_MR_CACHE_ENTRIES) {
468 mlx5_ib_warn(dev, "order %d, cache index %d\n", order, c);
469 return NULL;
470 }
471
472 for (i = c; i < MAX_MR_CACHE_ENTRIES; i++) {
473 ent = &cache->ent[i];
474
475 mlx5_ib_dbg(dev, "order %d, cache index %d\n", ent->order, i);
476
Eli Cohen746b5582013-10-23 09:53:14 +0300477 spin_lock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300478 if (!list_empty(&ent->head)) {
479 mr = list_first_entry(&ent->head, struct mlx5_ib_mr,
480 list);
481 list_del(&mr->list);
482 ent->cur--;
Eli Cohen746b5582013-10-23 09:53:14 +0300483 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300484 if (ent->cur < ent->limit)
485 queue_work(cache->wq, &ent->work);
486 break;
487 }
Eli Cohen746b5582013-10-23 09:53:14 +0300488 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300489
490 queue_work(cache->wq, &ent->work);
Eli Cohene126ba92013-07-07 17:25:49 +0300491 }
492
493 if (!mr)
494 cache->ent[c].miss++;
495
496 return mr;
497}
498
499static void free_cached_mr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
500{
501 struct mlx5_mr_cache *cache = &dev->cache;
502 struct mlx5_cache_ent *ent;
503 int shrink = 0;
504 int c;
505
506 c = order2idx(dev, mr->order);
507 if (c < 0 || c >= MAX_MR_CACHE_ENTRIES) {
508 mlx5_ib_warn(dev, "order %d, cache index %d\n", mr->order, c);
509 return;
510 }
511 ent = &cache->ent[c];
Eli Cohen746b5582013-10-23 09:53:14 +0300512 spin_lock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300513 list_add_tail(&mr->list, &ent->head);
514 ent->cur++;
515 if (ent->cur > 2 * ent->limit)
516 shrink = 1;
Eli Cohen746b5582013-10-23 09:53:14 +0300517 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300518
519 if (shrink)
520 queue_work(cache->wq, &ent->work);
521}
522
523static void clean_keys(struct mlx5_ib_dev *dev, int c)
524{
Eli Cohene126ba92013-07-07 17:25:49 +0300525 struct mlx5_mr_cache *cache = &dev->cache;
526 struct mlx5_cache_ent *ent = &cache->ent[c];
527 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +0300528 int err;
529
Moshe Lazer3c461912013-09-11 16:35:23 +0300530 cancel_delayed_work(&ent->dwork);
Eli Cohene126ba92013-07-07 17:25:49 +0300531 while (1) {
Eli Cohen746b5582013-10-23 09:53:14 +0300532 spin_lock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300533 if (list_empty(&ent->head)) {
Eli Cohen746b5582013-10-23 09:53:14 +0300534 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300535 return;
536 }
537 mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
538 list_del(&mr->list);
539 ent->cur--;
540 ent->size--;
Eli Cohen746b5582013-10-23 09:53:14 +0300541 spin_unlock_irq(&ent->lock);
Haggai Eranb4cfe442014-12-11 17:04:26 +0200542 err = destroy_mkey(dev, mr);
Eli Cohen203099f2013-09-11 16:35:26 +0300543 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +0300544 mlx5_ib_warn(dev, "failed destroy mkey\n");
Eli Cohen203099f2013-09-11 16:35:26 +0300545 else
Eli Cohene126ba92013-07-07 17:25:49 +0300546 kfree(mr);
Eli Cohene126ba92013-07-07 17:25:49 +0300547 }
548}
549
550static int mlx5_mr_cache_debugfs_init(struct mlx5_ib_dev *dev)
551{
552 struct mlx5_mr_cache *cache = &dev->cache;
553 struct mlx5_cache_ent *ent;
554 int i;
555
556 if (!mlx5_debugfs_root)
557 return 0;
558
Jack Morgenstein9603b612014-07-28 23:30:22 +0300559 cache->root = debugfs_create_dir("mr_cache", dev->mdev->priv.dbg_root);
Eli Cohene126ba92013-07-07 17:25:49 +0300560 if (!cache->root)
561 return -ENOMEM;
562
563 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
564 ent = &cache->ent[i];
565 sprintf(ent->name, "%d", ent->order);
566 ent->dir = debugfs_create_dir(ent->name, cache->root);
567 if (!ent->dir)
568 return -ENOMEM;
569
570 ent->fsize = debugfs_create_file("size", 0600, ent->dir, ent,
571 &size_fops);
572 if (!ent->fsize)
573 return -ENOMEM;
574
575 ent->flimit = debugfs_create_file("limit", 0600, ent->dir, ent,
576 &limit_fops);
577 if (!ent->flimit)
578 return -ENOMEM;
579
580 ent->fcur = debugfs_create_u32("cur", 0400, ent->dir,
581 &ent->cur);
582 if (!ent->fcur)
583 return -ENOMEM;
584
585 ent->fmiss = debugfs_create_u32("miss", 0600, ent->dir,
586 &ent->miss);
587 if (!ent->fmiss)
588 return -ENOMEM;
589 }
590
591 return 0;
592}
593
594static void mlx5_mr_cache_debugfs_cleanup(struct mlx5_ib_dev *dev)
595{
596 if (!mlx5_debugfs_root)
597 return;
598
599 debugfs_remove_recursive(dev->cache.root);
600}
601
Eli Cohen746b5582013-10-23 09:53:14 +0300602static void delay_time_func(unsigned long ctx)
603{
604 struct mlx5_ib_dev *dev = (struct mlx5_ib_dev *)ctx;
605
606 dev->fill_delay = 0;
607}
608
Eli Cohene126ba92013-07-07 17:25:49 +0300609int mlx5_mr_cache_init(struct mlx5_ib_dev *dev)
610{
611 struct mlx5_mr_cache *cache = &dev->cache;
612 struct mlx5_cache_ent *ent;
613 int limit;
Eli Cohene126ba92013-07-07 17:25:49 +0300614 int err;
615 int i;
616
617 cache->wq = create_singlethread_workqueue("mkey_cache");
618 if (!cache->wq) {
619 mlx5_ib_warn(dev, "failed to create work queue\n");
620 return -ENOMEM;
621 }
622
Eli Cohen746b5582013-10-23 09:53:14 +0300623 setup_timer(&dev->delay_timer, delay_time_func, (unsigned long)dev);
Eli Cohene126ba92013-07-07 17:25:49 +0300624 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
625 INIT_LIST_HEAD(&cache->ent[i].head);
626 spin_lock_init(&cache->ent[i].lock);
627
628 ent = &cache->ent[i];
629 INIT_LIST_HEAD(&ent->head);
630 spin_lock_init(&ent->lock);
631 ent->order = i + 2;
632 ent->dev = dev;
633
Jack Morgenstein9603b612014-07-28 23:30:22 +0300634 if (dev->mdev->profile->mask & MLX5_PROF_MASK_MR_CACHE)
635 limit = dev->mdev->profile->mr_cache[i].limit;
Eli Cohen2d036fa2013-10-24 12:01:00 +0300636 else
Eli Cohene126ba92013-07-07 17:25:49 +0300637 limit = 0;
Eli Cohen2d036fa2013-10-24 12:01:00 +0300638
Eli Cohene126ba92013-07-07 17:25:49 +0300639 INIT_WORK(&ent->work, cache_work_func);
640 INIT_DELAYED_WORK(&ent->dwork, delayed_cache_work_func);
641 ent->limit = limit;
642 queue_work(cache->wq, &ent->work);
643 }
644
645 err = mlx5_mr_cache_debugfs_init(dev);
646 if (err)
647 mlx5_ib_warn(dev, "cache debugfs failure\n");
648
649 return 0;
650}
651
652int mlx5_mr_cache_cleanup(struct mlx5_ib_dev *dev)
653{
654 int i;
655
656 dev->cache.stopped = 1;
Moshe Lazer3c461912013-09-11 16:35:23 +0300657 flush_workqueue(dev->cache.wq);
Eli Cohene126ba92013-07-07 17:25:49 +0300658
659 mlx5_mr_cache_debugfs_cleanup(dev);
660
661 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++)
662 clean_keys(dev, i);
663
Moshe Lazer3c461912013-09-11 16:35:23 +0300664 destroy_workqueue(dev->cache.wq);
Eli Cohen746b5582013-10-23 09:53:14 +0300665 del_timer_sync(&dev->delay_timer);
Moshe Lazer3c461912013-09-11 16:35:23 +0300666
Eli Cohene126ba92013-07-07 17:25:49 +0300667 return 0;
668}
669
670struct ib_mr *mlx5_ib_get_dma_mr(struct ib_pd *pd, int acc)
671{
672 struct mlx5_ib_dev *dev = to_mdev(pd->device);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300673 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300674 struct mlx5_create_mkey_mbox_in *in;
675 struct mlx5_mkey_seg *seg;
676 struct mlx5_ib_mr *mr;
677 int err;
678
679 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
680 if (!mr)
681 return ERR_PTR(-ENOMEM);
682
683 in = kzalloc(sizeof(*in), GFP_KERNEL);
684 if (!in) {
685 err = -ENOMEM;
686 goto err_free;
687 }
688
689 seg = &in->seg;
690 seg->flags = convert_access(acc) | MLX5_ACCESS_MODE_PA;
691 seg->flags_pd = cpu_to_be32(to_mpd(pd)->pdn | MLX5_MKEY_LEN64);
692 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
693 seg->start_addr = 0;
694
Matan Baraka606b0f2016-02-29 18:05:28 +0200695 err = mlx5_core_create_mkey(mdev, &mr->mmkey, in, sizeof(*in), NULL, NULL,
Eli Cohen746b5582013-10-23 09:53:14 +0300696 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +0300697 if (err)
698 goto err_in;
699
700 kfree(in);
Matan Baraka606b0f2016-02-29 18:05:28 +0200701 mr->ibmr.lkey = mr->mmkey.key;
702 mr->ibmr.rkey = mr->mmkey.key;
Eli Cohene126ba92013-07-07 17:25:49 +0300703 mr->umem = NULL;
704
705 return &mr->ibmr;
706
707err_in:
708 kfree(in);
709
710err_free:
711 kfree(mr);
712
713 return ERR_PTR(err);
714}
715
716static int get_octo_len(u64 addr, u64 len, int page_size)
717{
718 u64 offset;
719 int npages;
720
721 offset = addr & (page_size - 1);
722 npages = ALIGN(len + offset, page_size) >> ilog2(page_size);
723 return (npages + 1) / 2;
724}
725
726static int use_umr(int order)
727{
Haggai Erancc149f752014-12-11 17:04:21 +0200728 return order <= MLX5_MAX_UMR_SHIFT;
Eli Cohene126ba92013-07-07 17:25:49 +0300729}
730
Noa Osherovich395a8e42016-02-29 16:46:50 +0200731static int dma_map_mr_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
732 int npages, int page_shift, int *size,
733 __be64 **mr_pas, dma_addr_t *dma)
734{
735 __be64 *pas;
736 struct device *ddev = dev->ib_dev.dma_device;
737
738 /*
739 * UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes.
740 * To avoid copying garbage after the pas array, we allocate
741 * a little more.
742 */
743 *size = ALIGN(sizeof(u64) * npages, MLX5_UMR_MTT_ALIGNMENT);
744 *mr_pas = kmalloc(*size + MLX5_UMR_ALIGN - 1, GFP_KERNEL);
745 if (!(*mr_pas))
746 return -ENOMEM;
747
748 pas = PTR_ALIGN(*mr_pas, MLX5_UMR_ALIGN);
749 mlx5_ib_populate_pas(dev, umem, page_shift, pas, MLX5_IB_MTT_PRESENT);
750 /* Clear padding after the actual pages. */
751 memset(pas + npages, 0, *size - npages * sizeof(u64));
752
753 *dma = dma_map_single(ddev, pas, *size, DMA_TO_DEVICE);
754 if (dma_mapping_error(ddev, *dma)) {
755 kfree(*mr_pas);
756 return -ENOMEM;
757 }
758
759 return 0;
760}
761
762static void prep_umr_wqe_common(struct ib_pd *pd, struct ib_send_wr *wr,
763 struct ib_sge *sg, u64 dma, int n, u32 key,
764 int page_shift)
Eli Cohene126ba92013-07-07 17:25:49 +0300765{
766 struct mlx5_ib_dev *dev = to_mdev(pd->device);
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100767 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +0300768
769 sg->addr = dma;
770 sg->length = ALIGN(sizeof(u64) * n, 64);
Jason Gunthorpeb37c7882015-07-30 17:22:19 -0600771 sg->lkey = dev->umrc.pd->local_dma_lkey;
Eli Cohene126ba92013-07-07 17:25:49 +0300772
773 wr->next = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300774 wr->sg_list = sg;
775 if (n)
776 wr->num_sge = 1;
777 else
778 wr->num_sge = 0;
779
780 wr->opcode = MLX5_IB_WR_UMR;
Haggai Eran968e78d2014-12-11 17:04:11 +0200781
782 umrwr->npages = n;
783 umrwr->page_shift = page_shift;
784 umrwr->mkey = key;
Noa Osherovich395a8e42016-02-29 16:46:50 +0200785}
786
787static void prep_umr_reg_wqe(struct ib_pd *pd, struct ib_send_wr *wr,
788 struct ib_sge *sg, u64 dma, int n, u32 key,
789 int page_shift, u64 virt_addr, u64 len,
790 int access_flags)
791{
792 struct mlx5_umr_wr *umrwr = umr_wr(wr);
793
794 prep_umr_wqe_common(pd, wr, sg, dma, n, key, page_shift);
795
796 wr->send_flags = 0;
797
Haggai Eran968e78d2014-12-11 17:04:11 +0200798 umrwr->target.virt_addr = virt_addr;
799 umrwr->length = len;
800 umrwr->access_flags = access_flags;
801 umrwr->pd = pd;
Eli Cohene126ba92013-07-07 17:25:49 +0300802}
803
804static void prep_umr_unreg_wqe(struct mlx5_ib_dev *dev,
805 struct ib_send_wr *wr, u32 key)
806{
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100807 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +0200808
809 wr->send_flags = MLX5_IB_SEND_UMR_UNREG | MLX5_IB_SEND_UMR_FAIL_IF_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +0300810 wr->opcode = MLX5_IB_WR_UMR;
Haggai Eran968e78d2014-12-11 17:04:11 +0200811 umrwr->mkey = key;
Eli Cohene126ba92013-07-07 17:25:49 +0300812}
813
Noa Osherovich395a8e42016-02-29 16:46:50 +0200814static struct ib_umem *mr_umem_get(struct ib_pd *pd, u64 start, u64 length,
815 int access_flags, int *npages,
816 int *page_shift, int *ncont, int *order)
817{
818 struct mlx5_ib_dev *dev = to_mdev(pd->device);
819 struct ib_umem *umem = ib_umem_get(pd->uobject->context, start, length,
820 access_flags, 0);
821 if (IS_ERR(umem)) {
822 mlx5_ib_err(dev, "umem get failed (%ld)\n", PTR_ERR(umem));
823 return (void *)umem;
824 }
825
826 mlx5_ib_cont_pages(umem, start, npages, page_shift, ncont, order);
827 if (!*npages) {
828 mlx5_ib_warn(dev, "avoid zero region\n");
829 ib_umem_release(umem);
830 return ERR_PTR(-EINVAL);
831 }
832
833 mlx5_ib_dbg(dev, "npages %d, ncont %d, order %d, page_shift %d\n",
834 *npages, *ncont, *order, *page_shift);
835
836 return umem;
837}
838
Eli Cohene126ba92013-07-07 17:25:49 +0300839void mlx5_umr_cq_handler(struct ib_cq *cq, void *cq_context)
840{
Shachar Raindela74d2412014-05-22 14:50:12 +0300841 struct mlx5_ib_umr_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +0300842 struct ib_wc wc;
843 int err;
844
845 while (1) {
846 err = ib_poll_cq(cq, 1, &wc);
847 if (err < 0) {
848 pr_warn("poll cq error %d\n", err);
849 return;
850 }
851 if (err == 0)
852 break;
853
Roland Dreier6c9b5d92014-05-28 09:23:03 -0700854 context = (struct mlx5_ib_umr_context *) (unsigned long) wc.wr_id;
Shachar Raindela74d2412014-05-22 14:50:12 +0300855 context->status = wc.status;
856 complete(&context->done);
Eli Cohene126ba92013-07-07 17:25:49 +0300857 }
858 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
859}
860
861static struct mlx5_ib_mr *reg_umr(struct ib_pd *pd, struct ib_umem *umem,
862 u64 virt_addr, u64 len, int npages,
863 int page_shift, int order, int access_flags)
864{
865 struct mlx5_ib_dev *dev = to_mdev(pd->device);
Eli Cohen203099f2013-09-11 16:35:26 +0300866 struct device *ddev = dev->ib_dev.dma_device;
Eli Cohene126ba92013-07-07 17:25:49 +0300867 struct umr_common *umrc = &dev->umrc;
Shachar Raindela74d2412014-05-22 14:50:12 +0300868 struct mlx5_ib_umr_context umr_context;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100869 struct mlx5_umr_wr umrwr;
870 struct ib_send_wr *bad;
Eli Cohene126ba92013-07-07 17:25:49 +0300871 struct mlx5_ib_mr *mr;
872 struct ib_sge sg;
Haggai Erancc149f752014-12-11 17:04:21 +0200873 int size;
Haggai Eran21af2c32014-12-11 17:04:10 +0200874 __be64 *mr_pas;
875 dma_addr_t dma;
Haggai Eran096f7e72014-05-22 14:50:08 +0300876 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300877 int i;
878
Eli Cohen746b5582013-10-23 09:53:14 +0300879 for (i = 0; i < 1; i++) {
Eli Cohene126ba92013-07-07 17:25:49 +0300880 mr = alloc_cached_mr(dev, order);
881 if (mr)
882 break;
883
884 err = add_keys(dev, order2idx(dev, order), 1);
Eli Cohen746b5582013-10-23 09:53:14 +0300885 if (err && err != -EAGAIN) {
886 mlx5_ib_warn(dev, "add_keys failed, err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +0300887 break;
888 }
889 }
890
891 if (!mr)
892 return ERR_PTR(-EAGAIN);
893
Noa Osherovich395a8e42016-02-29 16:46:50 +0200894 err = dma_map_mr_pas(dev, umem, npages, page_shift, &size, &mr_pas,
895 &dma);
896 if (err)
Haggai Eran096f7e72014-05-22 14:50:08 +0300897 goto free_mr;
Eli Cohen203099f2013-09-11 16:35:26 +0300898
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100899 memset(&umrwr, 0, sizeof(umrwr));
900 umrwr.wr.wr_id = (u64)(unsigned long)&umr_context;
Matan Baraka606b0f2016-02-29 18:05:28 +0200901 prep_umr_reg_wqe(pd, &umrwr.wr, &sg, dma, npages, mr->mmkey.key,
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100902 page_shift, virt_addr, len, access_flags);
Eli Cohene126ba92013-07-07 17:25:49 +0300903
Shachar Raindela74d2412014-05-22 14:50:12 +0300904 mlx5_ib_init_umr_context(&umr_context);
Eli Cohene126ba92013-07-07 17:25:49 +0300905 down(&umrc->sem);
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100906 err = ib_post_send(umrc->qp, &umrwr.wr, &bad);
Eli Cohene126ba92013-07-07 17:25:49 +0300907 if (err) {
908 mlx5_ib_warn(dev, "post send failed, err %d\n", err);
Haggai Eran096f7e72014-05-22 14:50:08 +0300909 goto unmap_dma;
Shachar Raindela74d2412014-05-22 14:50:12 +0300910 } else {
911 wait_for_completion(&umr_context.done);
912 if (umr_context.status != IB_WC_SUCCESS) {
913 mlx5_ib_warn(dev, "reg umr failed\n");
914 err = -EFAULT;
915 }
Haggai Eran096f7e72014-05-22 14:50:08 +0300916 }
917
Matan Baraka606b0f2016-02-29 18:05:28 +0200918 mr->mmkey.iova = virt_addr;
919 mr->mmkey.size = len;
920 mr->mmkey.pd = to_mpd(pd)->pdn;
Haggai Eranb4755982014-05-22 14:50:10 +0300921
Haggai Eranb4cfe442014-12-11 17:04:26 +0200922 mr->live = 1;
923
Haggai Eran096f7e72014-05-22 14:50:08 +0300924unmap_dma:
925 up(&umrc->sem);
Haggai Eran21af2c32014-12-11 17:04:10 +0200926 dma_unmap_single(ddev, dma, size, DMA_TO_DEVICE);
Haggai Eran096f7e72014-05-22 14:50:08 +0300927
Haggai Eran21af2c32014-12-11 17:04:10 +0200928 kfree(mr_pas);
Haggai Eran096f7e72014-05-22 14:50:08 +0300929
930free_mr:
931 if (err) {
932 free_cached_mr(dev, mr);
933 return ERR_PTR(err);
Eli Cohene126ba92013-07-07 17:25:49 +0300934 }
935
936 return mr;
Eli Cohene126ba92013-07-07 17:25:49 +0300937}
938
Haggai Eran832a6b02014-12-11 17:04:22 +0200939#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
940int mlx5_ib_update_mtt(struct mlx5_ib_mr *mr, u64 start_page_index, int npages,
941 int zap)
942{
943 struct mlx5_ib_dev *dev = mr->dev;
944 struct device *ddev = dev->ib_dev.dma_device;
945 struct umr_common *umrc = &dev->umrc;
946 struct mlx5_ib_umr_context umr_context;
947 struct ib_umem *umem = mr->umem;
948 int size;
949 __be64 *pas;
950 dma_addr_t dma;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100951 struct ib_send_wr *bad;
952 struct mlx5_umr_wr wr;
Haggai Eran832a6b02014-12-11 17:04:22 +0200953 struct ib_sge sg;
954 int err = 0;
955 const int page_index_alignment = MLX5_UMR_MTT_ALIGNMENT / sizeof(u64);
956 const int page_index_mask = page_index_alignment - 1;
957 size_t pages_mapped = 0;
958 size_t pages_to_map = 0;
959 size_t pages_iter = 0;
960 int use_emergency_buf = 0;
961
962 /* UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes,
963 * so we need to align the offset and length accordingly */
964 if (start_page_index & page_index_mask) {
965 npages += start_page_index & page_index_mask;
966 start_page_index &= ~page_index_mask;
967 }
968
969 pages_to_map = ALIGN(npages, page_index_alignment);
970
971 if (start_page_index + pages_to_map > MLX5_MAX_UMR_PAGES)
972 return -EINVAL;
973
974 size = sizeof(u64) * pages_to_map;
975 size = min_t(int, PAGE_SIZE, size);
976 /* We allocate with GFP_ATOMIC to avoid recursion into page-reclaim
977 * code, when we are called from an invalidation. The pas buffer must
978 * be 2k-aligned for Connect-IB. */
979 pas = (__be64 *)get_zeroed_page(GFP_ATOMIC);
980 if (!pas) {
981 mlx5_ib_warn(dev, "unable to allocate memory during MTT update, falling back to slower chunked mechanism.\n");
982 pas = mlx5_ib_update_mtt_emergency_buffer;
983 size = MLX5_UMR_MTT_MIN_CHUNK_SIZE;
984 use_emergency_buf = 1;
985 mutex_lock(&mlx5_ib_update_mtt_emergency_buffer_mutex);
986 memset(pas, 0, size);
987 }
988 pages_iter = size / sizeof(u64);
989 dma = dma_map_single(ddev, pas, size, DMA_TO_DEVICE);
990 if (dma_mapping_error(ddev, dma)) {
991 mlx5_ib_err(dev, "unable to map DMA during MTT update.\n");
992 err = -ENOMEM;
993 goto free_pas;
994 }
995
996 for (pages_mapped = 0;
997 pages_mapped < pages_to_map && !err;
998 pages_mapped += pages_iter, start_page_index += pages_iter) {
999 dma_sync_single_for_cpu(ddev, dma, size, DMA_TO_DEVICE);
1000
1001 npages = min_t(size_t,
1002 pages_iter,
1003 ib_umem_num_pages(umem) - start_page_index);
1004
1005 if (!zap) {
1006 __mlx5_ib_populate_pas(dev, umem, PAGE_SHIFT,
1007 start_page_index, npages, pas,
1008 MLX5_IB_MTT_PRESENT);
1009 /* Clear padding after the pages brought from the
1010 * umem. */
1011 memset(pas + npages, 0, size - npages * sizeof(u64));
1012 }
1013
1014 dma_sync_single_for_device(ddev, dma, size, DMA_TO_DEVICE);
1015
1016 memset(&wr, 0, sizeof(wr));
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001017 wr.wr.wr_id = (u64)(unsigned long)&umr_context;
Haggai Eran832a6b02014-12-11 17:04:22 +02001018
1019 sg.addr = dma;
1020 sg.length = ALIGN(npages * sizeof(u64),
1021 MLX5_UMR_MTT_ALIGNMENT);
Jason Gunthorpeb37c7882015-07-30 17:22:19 -06001022 sg.lkey = dev->umrc.pd->local_dma_lkey;
Haggai Eran832a6b02014-12-11 17:04:22 +02001023
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001024 wr.wr.send_flags = MLX5_IB_SEND_UMR_FAIL_IF_FREE |
Haggai Eran832a6b02014-12-11 17:04:22 +02001025 MLX5_IB_SEND_UMR_UPDATE_MTT;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001026 wr.wr.sg_list = &sg;
1027 wr.wr.num_sge = 1;
1028 wr.wr.opcode = MLX5_IB_WR_UMR;
1029 wr.npages = sg.length / sizeof(u64);
1030 wr.page_shift = PAGE_SHIFT;
Matan Baraka606b0f2016-02-29 18:05:28 +02001031 wr.mkey = mr->mmkey.key;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001032 wr.target.offset = start_page_index;
Haggai Eran832a6b02014-12-11 17:04:22 +02001033
1034 mlx5_ib_init_umr_context(&umr_context);
1035 down(&umrc->sem);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001036 err = ib_post_send(umrc->qp, &wr.wr, &bad);
Haggai Eran832a6b02014-12-11 17:04:22 +02001037 if (err) {
1038 mlx5_ib_err(dev, "UMR post send failed, err %d\n", err);
1039 } else {
1040 wait_for_completion(&umr_context.done);
1041 if (umr_context.status != IB_WC_SUCCESS) {
1042 mlx5_ib_err(dev, "UMR completion failed, code %d\n",
1043 umr_context.status);
1044 err = -EFAULT;
1045 }
1046 }
1047 up(&umrc->sem);
1048 }
1049 dma_unmap_single(ddev, dma, size, DMA_TO_DEVICE);
1050
1051free_pas:
1052 if (!use_emergency_buf)
1053 free_page((unsigned long)pas);
1054 else
1055 mutex_unlock(&mlx5_ib_update_mtt_emergency_buffer_mutex);
1056
1057 return err;
1058}
1059#endif
1060
Noa Osherovich395a8e42016-02-29 16:46:50 +02001061/*
1062 * If ibmr is NULL it will be allocated by reg_create.
1063 * Else, the given ibmr will be used.
1064 */
1065static struct mlx5_ib_mr *reg_create(struct ib_mr *ibmr, struct ib_pd *pd,
1066 u64 virt_addr, u64 length,
1067 struct ib_umem *umem, int npages,
1068 int page_shift, int access_flags)
Eli Cohene126ba92013-07-07 17:25:49 +03001069{
1070 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1071 struct mlx5_create_mkey_mbox_in *in;
1072 struct mlx5_ib_mr *mr;
1073 int inlen;
1074 int err;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001075 bool pg_cap = !!(MLX5_CAP_GEN(dev->mdev, pg));
Eli Cohene126ba92013-07-07 17:25:49 +03001076
Noa Osherovich395a8e42016-02-29 16:46:50 +02001077 mr = ibmr ? to_mmr(ibmr) : kzalloc(sizeof(*mr), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001078 if (!mr)
1079 return ERR_PTR(-ENOMEM);
1080
1081 inlen = sizeof(*in) + sizeof(*in->pas) * ((npages + 1) / 2) * 2;
1082 in = mlx5_vzalloc(inlen);
1083 if (!in) {
1084 err = -ENOMEM;
1085 goto err_1;
1086 }
Haggai Erancc149f752014-12-11 17:04:21 +02001087 mlx5_ib_populate_pas(dev, umem, page_shift, in->pas,
1088 pg_cap ? MLX5_IB_MTT_PRESENT : 0);
Eli Cohene126ba92013-07-07 17:25:49 +03001089
Haggai Erancc149f752014-12-11 17:04:21 +02001090 /* The MLX5_MKEY_INBOX_PG_ACCESS bit allows setting the access flags
1091 * in the page list submitted with the command. */
1092 in->flags = pg_cap ? cpu_to_be32(MLX5_MKEY_INBOX_PG_ACCESS) : 0;
Eli Cohene126ba92013-07-07 17:25:49 +03001093 in->seg.flags = convert_access(access_flags) |
1094 MLX5_ACCESS_MODE_MTT;
1095 in->seg.flags_pd = cpu_to_be32(to_mpd(pd)->pdn);
1096 in->seg.start_addr = cpu_to_be64(virt_addr);
1097 in->seg.len = cpu_to_be64(length);
1098 in->seg.bsfs_octo_size = 0;
1099 in->seg.xlt_oct_size = cpu_to_be32(get_octo_len(virt_addr, length, 1 << page_shift));
1100 in->seg.log2_page_size = page_shift;
1101 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
Eli Cohen746b5582013-10-23 09:53:14 +03001102 in->xlat_oct_act_size = cpu_to_be32(get_octo_len(virt_addr, length,
1103 1 << page_shift));
Matan Baraka606b0f2016-02-29 18:05:28 +02001104 err = mlx5_core_create_mkey(dev->mdev, &mr->mmkey, in, inlen, NULL,
Eli Cohen746b5582013-10-23 09:53:14 +03001105 NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03001106 if (err) {
1107 mlx5_ib_warn(dev, "create mkey failed\n");
1108 goto err_2;
1109 }
1110 mr->umem = umem;
Majd Dibbiny7eae20d2015-01-06 13:56:01 +02001111 mr->dev = dev;
Haggai Eranb4cfe442014-12-11 17:04:26 +02001112 mr->live = 1;
Al Viro479163f2014-11-20 08:13:57 +00001113 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001114
Matan Baraka606b0f2016-02-29 18:05:28 +02001115 mlx5_ib_dbg(dev, "mkey = 0x%x\n", mr->mmkey.key);
Eli Cohene126ba92013-07-07 17:25:49 +03001116
1117 return mr;
1118
1119err_2:
Al Viro479163f2014-11-20 08:13:57 +00001120 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001121
1122err_1:
Noa Osherovich395a8e42016-02-29 16:46:50 +02001123 if (!ibmr)
1124 kfree(mr);
Eli Cohene126ba92013-07-07 17:25:49 +03001125
1126 return ERR_PTR(err);
1127}
1128
Noa Osherovich395a8e42016-02-29 16:46:50 +02001129static void set_mr_fileds(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr,
1130 int npages, u64 length, int access_flags)
1131{
1132 mr->npages = npages;
1133 atomic_add(npages, &dev->mdev->priv.reg_pages);
Matan Baraka606b0f2016-02-29 18:05:28 +02001134 mr->ibmr.lkey = mr->mmkey.key;
1135 mr->ibmr.rkey = mr->mmkey.key;
Noa Osherovich395a8e42016-02-29 16:46:50 +02001136 mr->ibmr.length = length;
Noa Osherovich56e11d62016-02-29 16:46:51 +02001137 mr->access_flags = access_flags;
Noa Osherovich395a8e42016-02-29 16:46:50 +02001138}
1139
Eli Cohene126ba92013-07-07 17:25:49 +03001140struct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
1141 u64 virt_addr, int access_flags,
1142 struct ib_udata *udata)
1143{
1144 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1145 struct mlx5_ib_mr *mr = NULL;
1146 struct ib_umem *umem;
1147 int page_shift;
1148 int npages;
1149 int ncont;
1150 int order;
1151 int err;
1152
Eli Cohen900a6d72014-09-14 16:47:51 +03001153 mlx5_ib_dbg(dev, "start 0x%llx, virt_addr 0x%llx, length 0x%llx, access_flags 0x%x\n",
1154 start, virt_addr, length, access_flags);
Noa Osherovich395a8e42016-02-29 16:46:50 +02001155 umem = mr_umem_get(pd, start, length, access_flags, &npages,
1156 &page_shift, &ncont, &order);
1157
1158 if (IS_ERR(umem))
Eli Cohene126ba92013-07-07 17:25:49 +03001159 return (void *)umem;
Eli Cohene126ba92013-07-07 17:25:49 +03001160
1161 if (use_umr(order)) {
1162 mr = reg_umr(pd, umem, virt_addr, length, ncont, page_shift,
1163 order, access_flags);
1164 if (PTR_ERR(mr) == -EAGAIN) {
1165 mlx5_ib_dbg(dev, "cache empty for order %d", order);
1166 mr = NULL;
1167 }
Haggai Eran6aec21f2014-12-11 17:04:23 +02001168 } else if (access_flags & IB_ACCESS_ON_DEMAND) {
1169 err = -EINVAL;
1170 pr_err("Got MR registration for ODP MR > 512MB, not supported for Connect-IB");
1171 goto error;
Eli Cohene126ba92013-07-07 17:25:49 +03001172 }
1173
1174 if (!mr)
Noa Osherovich395a8e42016-02-29 16:46:50 +02001175 mr = reg_create(NULL, pd, virt_addr, length, umem, ncont,
1176 page_shift, access_flags);
Eli Cohene126ba92013-07-07 17:25:49 +03001177
1178 if (IS_ERR(mr)) {
1179 err = PTR_ERR(mr);
1180 goto error;
1181 }
1182
Matan Baraka606b0f2016-02-29 18:05:28 +02001183 mlx5_ib_dbg(dev, "mkey 0x%x\n", mr->mmkey.key);
Eli Cohene126ba92013-07-07 17:25:49 +03001184
1185 mr->umem = umem;
Noa Osherovich395a8e42016-02-29 16:46:50 +02001186 set_mr_fileds(dev, mr, npages, length, access_flags);
Eli Cohene126ba92013-07-07 17:25:49 +03001187
Haggai Eranb4cfe442014-12-11 17:04:26 +02001188#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Noa Osherovich395a8e42016-02-29 16:46:50 +02001189 update_odp_mr(mr);
Haggai Eranb4cfe442014-12-11 17:04:26 +02001190#endif
1191
Eli Cohene126ba92013-07-07 17:25:49 +03001192 return &mr->ibmr;
1193
1194error:
1195 ib_umem_release(umem);
1196 return ERR_PTR(err);
1197}
1198
1199static int unreg_umr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
1200{
1201 struct umr_common *umrc = &dev->umrc;
Shachar Raindela74d2412014-05-22 14:50:12 +03001202 struct mlx5_ib_umr_context umr_context;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001203 struct mlx5_umr_wr umrwr;
1204 struct ib_send_wr *bad;
Eli Cohene126ba92013-07-07 17:25:49 +03001205 int err;
1206
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001207 memset(&umrwr.wr, 0, sizeof(umrwr));
1208 umrwr.wr.wr_id = (u64)(unsigned long)&umr_context;
Matan Baraka606b0f2016-02-29 18:05:28 +02001209 prep_umr_unreg_wqe(dev, &umrwr.wr, mr->mmkey.key);
Eli Cohene126ba92013-07-07 17:25:49 +03001210
Shachar Raindela74d2412014-05-22 14:50:12 +03001211 mlx5_ib_init_umr_context(&umr_context);
Eli Cohene126ba92013-07-07 17:25:49 +03001212 down(&umrc->sem);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001213 err = ib_post_send(umrc->qp, &umrwr.wr, &bad);
Eli Cohene126ba92013-07-07 17:25:49 +03001214 if (err) {
1215 up(&umrc->sem);
1216 mlx5_ib_dbg(dev, "err %d\n", err);
1217 goto error;
Shachar Raindela74d2412014-05-22 14:50:12 +03001218 } else {
1219 wait_for_completion(&umr_context.done);
1220 up(&umrc->sem);
Eli Cohene126ba92013-07-07 17:25:49 +03001221 }
Shachar Raindela74d2412014-05-22 14:50:12 +03001222 if (umr_context.status != IB_WC_SUCCESS) {
Eli Cohene126ba92013-07-07 17:25:49 +03001223 mlx5_ib_warn(dev, "unreg umr failed\n");
1224 err = -EFAULT;
1225 goto error;
1226 }
1227 return 0;
1228
1229error:
1230 return err;
1231}
1232
Noa Osherovich56e11d62016-02-29 16:46:51 +02001233static int rereg_umr(struct ib_pd *pd, struct mlx5_ib_mr *mr, u64 virt_addr,
1234 u64 length, int npages, int page_shift, int order,
1235 int access_flags, int flags)
1236{
1237 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1238 struct device *ddev = dev->ib_dev.dma_device;
1239 struct mlx5_ib_umr_context umr_context;
1240 struct ib_send_wr *bad;
1241 struct mlx5_umr_wr umrwr = {};
1242 struct ib_sge sg;
1243 struct umr_common *umrc = &dev->umrc;
1244 dma_addr_t dma = 0;
1245 __be64 *mr_pas = NULL;
1246 int size;
1247 int err;
1248
1249 umrwr.wr.wr_id = (u64)(unsigned long)&umr_context;
1250 umrwr.wr.send_flags = MLX5_IB_SEND_UMR_FAIL_IF_FREE;
1251
1252 if (flags & IB_MR_REREG_TRANS) {
1253 err = dma_map_mr_pas(dev, mr->umem, npages, page_shift, &size,
1254 &mr_pas, &dma);
1255 if (err)
1256 return err;
1257
1258 umrwr.target.virt_addr = virt_addr;
1259 umrwr.length = length;
1260 umrwr.wr.send_flags |= MLX5_IB_SEND_UMR_UPDATE_TRANSLATION;
1261 }
1262
Matan Baraka606b0f2016-02-29 18:05:28 +02001263 prep_umr_wqe_common(pd, &umrwr.wr, &sg, dma, npages, mr->mmkey.key,
Noa Osherovich56e11d62016-02-29 16:46:51 +02001264 page_shift);
1265
1266 if (flags & IB_MR_REREG_PD) {
1267 umrwr.pd = pd;
1268 umrwr.wr.send_flags |= MLX5_IB_SEND_UMR_UPDATE_PD;
1269 }
1270
1271 if (flags & IB_MR_REREG_ACCESS) {
1272 umrwr.access_flags = access_flags;
1273 umrwr.wr.send_flags |= MLX5_IB_SEND_UMR_UPDATE_ACCESS;
1274 }
1275
1276 mlx5_ib_init_umr_context(&umr_context);
1277
1278 /* post send request to UMR QP */
1279 down(&umrc->sem);
1280 err = ib_post_send(umrc->qp, &umrwr.wr, &bad);
1281
1282 if (err) {
1283 mlx5_ib_warn(dev, "post send failed, err %d\n", err);
1284 } else {
1285 wait_for_completion(&umr_context.done);
1286 if (umr_context.status != IB_WC_SUCCESS) {
1287 mlx5_ib_warn(dev, "reg umr failed (%u)\n",
1288 umr_context.status);
1289 err = -EFAULT;
1290 }
1291 }
1292
1293 up(&umrc->sem);
1294 if (flags & IB_MR_REREG_TRANS) {
1295 dma_unmap_single(ddev, dma, size, DMA_TO_DEVICE);
1296 kfree(mr_pas);
1297 }
1298 return err;
1299}
1300
1301int mlx5_ib_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start,
1302 u64 length, u64 virt_addr, int new_access_flags,
1303 struct ib_pd *new_pd, struct ib_udata *udata)
1304{
1305 struct mlx5_ib_dev *dev = to_mdev(ib_mr->device);
1306 struct mlx5_ib_mr *mr = to_mmr(ib_mr);
1307 struct ib_pd *pd = (flags & IB_MR_REREG_PD) ? new_pd : ib_mr->pd;
1308 int access_flags = flags & IB_MR_REREG_ACCESS ?
1309 new_access_flags :
1310 mr->access_flags;
1311 u64 addr = (flags & IB_MR_REREG_TRANS) ? virt_addr : mr->umem->address;
1312 u64 len = (flags & IB_MR_REREG_TRANS) ? length : mr->umem->length;
1313 int page_shift = 0;
1314 int npages = 0;
1315 int ncont = 0;
1316 int order = 0;
1317 int err;
1318
1319 mlx5_ib_dbg(dev, "start 0x%llx, virt_addr 0x%llx, length 0x%llx, access_flags 0x%x\n",
1320 start, virt_addr, length, access_flags);
1321
1322 if (flags != IB_MR_REREG_PD) {
1323 /*
1324 * Replace umem. This needs to be done whether or not UMR is
1325 * used.
1326 */
1327 flags |= IB_MR_REREG_TRANS;
1328 ib_umem_release(mr->umem);
1329 mr->umem = mr_umem_get(pd, addr, len, access_flags, &npages,
1330 &page_shift, &ncont, &order);
1331 if (IS_ERR(mr->umem)) {
1332 err = PTR_ERR(mr->umem);
1333 mr->umem = NULL;
1334 return err;
1335 }
1336 }
1337
1338 if (flags & IB_MR_REREG_TRANS && !use_umr_mtt_update(mr, addr, len)) {
1339 /*
1340 * UMR can't be used - MKey needs to be replaced.
1341 */
1342 if (mr->umred) {
1343 err = unreg_umr(dev, mr);
1344 if (err)
1345 mlx5_ib_warn(dev, "Failed to unregister MR\n");
1346 } else {
1347 err = destroy_mkey(dev, mr);
1348 if (err)
1349 mlx5_ib_warn(dev, "Failed to destroy MKey\n");
1350 }
1351 if (err)
1352 return err;
1353
1354 mr = reg_create(ib_mr, pd, addr, len, mr->umem, ncont,
1355 page_shift, access_flags);
1356
1357 if (IS_ERR(mr))
1358 return PTR_ERR(mr);
1359
1360 mr->umred = 0;
1361 } else {
1362 /*
1363 * Send a UMR WQE
1364 */
1365 err = rereg_umr(pd, mr, addr, len, npages, page_shift,
1366 order, access_flags, flags);
1367 if (err) {
1368 mlx5_ib_warn(dev, "Failed to rereg UMR\n");
1369 return err;
1370 }
1371 }
1372
1373 if (flags & IB_MR_REREG_PD) {
1374 ib_mr->pd = pd;
Matan Baraka606b0f2016-02-29 18:05:28 +02001375 mr->mmkey.pd = to_mpd(pd)->pdn;
Noa Osherovich56e11d62016-02-29 16:46:51 +02001376 }
1377
1378 if (flags & IB_MR_REREG_ACCESS)
1379 mr->access_flags = access_flags;
1380
1381 if (flags & IB_MR_REREG_TRANS) {
1382 atomic_sub(mr->npages, &dev->mdev->priv.reg_pages);
1383 set_mr_fileds(dev, mr, npages, len, access_flags);
Matan Baraka606b0f2016-02-29 18:05:28 +02001384 mr->mmkey.iova = addr;
1385 mr->mmkey.size = len;
Noa Osherovich56e11d62016-02-29 16:46:51 +02001386 }
1387#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1388 update_odp_mr(mr);
1389#endif
1390
1391 return 0;
1392}
1393
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001394static int
1395mlx5_alloc_priv_descs(struct ib_device *device,
1396 struct mlx5_ib_mr *mr,
1397 int ndescs,
1398 int desc_size)
1399{
1400 int size = ndescs * desc_size;
1401 int add_size;
1402 int ret;
1403
1404 add_size = max_t(int, MLX5_UMR_ALIGN - ARCH_KMALLOC_MINALIGN, 0);
1405
1406 mr->descs_alloc = kzalloc(size + add_size, GFP_KERNEL);
1407 if (!mr->descs_alloc)
1408 return -ENOMEM;
1409
1410 mr->descs = PTR_ALIGN(mr->descs_alloc, MLX5_UMR_ALIGN);
1411
1412 mr->desc_map = dma_map_single(device->dma_device, mr->descs,
1413 size, DMA_TO_DEVICE);
1414 if (dma_mapping_error(device->dma_device, mr->desc_map)) {
1415 ret = -ENOMEM;
1416 goto err;
1417 }
1418
1419 return 0;
1420err:
1421 kfree(mr->descs_alloc);
1422
1423 return ret;
1424}
1425
1426static void
1427mlx5_free_priv_descs(struct mlx5_ib_mr *mr)
1428{
1429 if (mr->descs) {
1430 struct ib_device *device = mr->ibmr.device;
1431 int size = mr->max_descs * mr->desc_size;
1432
1433 dma_unmap_single(device->dma_device, mr->desc_map,
1434 size, DMA_TO_DEVICE);
1435 kfree(mr->descs_alloc);
1436 mr->descs = NULL;
1437 }
1438}
1439
Haggai Eran6aec21f2014-12-11 17:04:23 +02001440static int clean_mr(struct mlx5_ib_mr *mr)
Eli Cohene126ba92013-07-07 17:25:49 +03001441{
Haggai Eran6aec21f2014-12-11 17:04:23 +02001442 struct mlx5_ib_dev *dev = to_mdev(mr->ibmr.device);
Eli Cohene126ba92013-07-07 17:25:49 +03001443 int umred = mr->umred;
1444 int err;
1445
Sagi Grimberg8b91ffc2015-07-30 10:32:34 +03001446 if (mr->sig) {
1447 if (mlx5_core_destroy_psv(dev->mdev,
1448 mr->sig->psv_memory.psv_idx))
1449 mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
1450 mr->sig->psv_memory.psv_idx);
1451 if (mlx5_core_destroy_psv(dev->mdev,
1452 mr->sig->psv_wire.psv_idx))
1453 mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
1454 mr->sig->psv_wire.psv_idx);
1455 kfree(mr->sig);
1456 mr->sig = NULL;
1457 }
1458
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001459 mlx5_free_priv_descs(mr);
1460
Eli Cohene126ba92013-07-07 17:25:49 +03001461 if (!umred) {
Haggai Eranb4cfe442014-12-11 17:04:26 +02001462 err = destroy_mkey(dev, mr);
Eli Cohene126ba92013-07-07 17:25:49 +03001463 if (err) {
1464 mlx5_ib_warn(dev, "failed to destroy mkey 0x%x (%d)\n",
Matan Baraka606b0f2016-02-29 18:05:28 +02001465 mr->mmkey.key, err);
Eli Cohene126ba92013-07-07 17:25:49 +03001466 return err;
1467 }
1468 } else {
1469 err = unreg_umr(dev, mr);
1470 if (err) {
1471 mlx5_ib_warn(dev, "failed unregister\n");
1472 return err;
1473 }
1474 free_cached_mr(dev, mr);
1475 }
1476
Eli Cohene126ba92013-07-07 17:25:49 +03001477 if (!umred)
1478 kfree(mr);
1479
1480 return 0;
1481}
1482
Haggai Eran6aec21f2014-12-11 17:04:23 +02001483int mlx5_ib_dereg_mr(struct ib_mr *ibmr)
1484{
1485 struct mlx5_ib_dev *dev = to_mdev(ibmr->device);
1486 struct mlx5_ib_mr *mr = to_mmr(ibmr);
1487 int npages = mr->npages;
1488 struct ib_umem *umem = mr->umem;
1489
1490#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Haggai Eranb4cfe442014-12-11 17:04:26 +02001491 if (umem && umem->odp_data) {
1492 /* Prevent new page faults from succeeding */
1493 mr->live = 0;
Haggai Eran6aec21f2014-12-11 17:04:23 +02001494 /* Wait for all running page-fault handlers to finish. */
1495 synchronize_srcu(&dev->mr_srcu);
Haggai Eranb4cfe442014-12-11 17:04:26 +02001496 /* Destroy all page mappings */
1497 mlx5_ib_invalidate_range(umem, ib_umem_start(umem),
1498 ib_umem_end(umem));
1499 /*
1500 * We kill the umem before the MR for ODP,
1501 * so that there will not be any invalidations in
1502 * flight, looking at the *mr struct.
1503 */
1504 ib_umem_release(umem);
1505 atomic_sub(npages, &dev->mdev->priv.reg_pages);
1506
1507 /* Avoid double-freeing the umem. */
1508 umem = NULL;
1509 }
Haggai Eran6aec21f2014-12-11 17:04:23 +02001510#endif
1511
1512 clean_mr(mr);
1513
1514 if (umem) {
1515 ib_umem_release(umem);
1516 atomic_sub(npages, &dev->mdev->priv.reg_pages);
1517 }
1518
1519 return 0;
1520}
1521
Sagi Grimberg9bee1782015-07-30 10:32:35 +03001522struct ib_mr *mlx5_ib_alloc_mr(struct ib_pd *pd,
1523 enum ib_mr_type mr_type,
1524 u32 max_num_sg)
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001525{
1526 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1527 struct mlx5_create_mkey_mbox_in *in;
1528 struct mlx5_ib_mr *mr;
1529 int access_mode, err;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03001530 int ndescs = roundup(max_num_sg, 4);
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001531
1532 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1533 if (!mr)
1534 return ERR_PTR(-ENOMEM);
1535
1536 in = kzalloc(sizeof(*in), GFP_KERNEL);
1537 if (!in) {
1538 err = -ENOMEM;
1539 goto err_free;
1540 }
1541
Haggai Eran968e78d2014-12-11 17:04:11 +02001542 in->seg.status = MLX5_MKEY_STATUS_FREE;
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001543 in->seg.xlt_oct_size = cpu_to_be32(ndescs);
1544 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
1545 in->seg.flags_pd = cpu_to_be32(to_mpd(pd)->pdn);
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001546
Sagi Grimberg9bee1782015-07-30 10:32:35 +03001547 if (mr_type == IB_MR_TYPE_MEM_REG) {
1548 access_mode = MLX5_ACCESS_MODE_MTT;
1549 in->seg.log2_page_size = PAGE_SHIFT;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001550
1551 err = mlx5_alloc_priv_descs(pd->device, mr,
1552 ndescs, sizeof(u64));
1553 if (err)
1554 goto err_free_in;
1555
1556 mr->desc_size = sizeof(u64);
1557 mr->max_descs = ndescs;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03001558 } else if (mr_type == IB_MR_TYPE_SIGNATURE) {
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001559 u32 psv_index[2];
1560
1561 in->seg.flags_pd = cpu_to_be32(be32_to_cpu(in->seg.flags_pd) |
1562 MLX5_MKEY_BSF_EN);
1563 in->seg.bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
1564 mr->sig = kzalloc(sizeof(*mr->sig), GFP_KERNEL);
1565 if (!mr->sig) {
1566 err = -ENOMEM;
1567 goto err_free_in;
1568 }
1569
1570 /* create mem & wire PSVs */
Jack Morgenstein9603b612014-07-28 23:30:22 +03001571 err = mlx5_core_create_psv(dev->mdev, to_mpd(pd)->pdn,
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001572 2, psv_index);
1573 if (err)
1574 goto err_free_sig;
1575
1576 access_mode = MLX5_ACCESS_MODE_KLM;
1577 mr->sig->psv_memory.psv_idx = psv_index[0];
1578 mr->sig->psv_wire.psv_idx = psv_index[1];
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02001579
1580 mr->sig->sig_status_checked = true;
1581 mr->sig->sig_err_exists = false;
1582 /* Next UMR, Arm SIGERR */
1583 ++mr->sig->sigerr_count;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03001584 } else {
1585 mlx5_ib_warn(dev, "Invalid mr type %d\n", mr_type);
1586 err = -EINVAL;
1587 goto err_free_in;
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001588 }
1589
1590 in->seg.flags = MLX5_PERM_UMR_EN | access_mode;
Matan Baraka606b0f2016-02-29 18:05:28 +02001591 err = mlx5_core_create_mkey(dev->mdev, &mr->mmkey, in, sizeof(*in),
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001592 NULL, NULL, NULL);
1593 if (err)
1594 goto err_destroy_psv;
1595
Matan Baraka606b0f2016-02-29 18:05:28 +02001596 mr->ibmr.lkey = mr->mmkey.key;
1597 mr->ibmr.rkey = mr->mmkey.key;
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001598 mr->umem = NULL;
1599 kfree(in);
1600
1601 return &mr->ibmr;
1602
1603err_destroy_psv:
1604 if (mr->sig) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001605 if (mlx5_core_destroy_psv(dev->mdev,
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001606 mr->sig->psv_memory.psv_idx))
1607 mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
1608 mr->sig->psv_memory.psv_idx);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001609 if (mlx5_core_destroy_psv(dev->mdev,
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001610 mr->sig->psv_wire.psv_idx))
1611 mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
1612 mr->sig->psv_wire.psv_idx);
1613 }
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001614 mlx5_free_priv_descs(mr);
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001615err_free_sig:
1616 kfree(mr->sig);
1617err_free_in:
1618 kfree(in);
1619err_free:
1620 kfree(mr);
1621 return ERR_PTR(err);
1622}
1623
Matan Barakd2370e02016-02-29 18:05:30 +02001624struct ib_mw *mlx5_ib_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
1625 struct ib_udata *udata)
1626{
1627 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1628 struct mlx5_create_mkey_mbox_in *in = NULL;
1629 struct mlx5_ib_mw *mw = NULL;
1630 int ndescs;
1631 int err;
1632 struct mlx5_ib_alloc_mw req = {};
1633 struct {
1634 __u32 comp_mask;
1635 __u32 response_length;
1636 } resp = {};
1637
1638 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1639 if (err)
1640 return ERR_PTR(err);
1641
1642 if (req.comp_mask || req.reserved1 || req.reserved2)
1643 return ERR_PTR(-EOPNOTSUPP);
1644
1645 if (udata->inlen > sizeof(req) &&
1646 !ib_is_udata_cleared(udata, sizeof(req),
1647 udata->inlen - sizeof(req)))
1648 return ERR_PTR(-EOPNOTSUPP);
1649
1650 ndescs = req.num_klms ? roundup(req.num_klms, 4) : roundup(1, 4);
1651
1652 mw = kzalloc(sizeof(*mw), GFP_KERNEL);
1653 in = kzalloc(sizeof(*in), GFP_KERNEL);
1654 if (!mw || !in) {
1655 err = -ENOMEM;
1656 goto free;
1657 }
1658
1659 in->seg.status = MLX5_MKEY_STATUS_FREE;
1660 in->seg.xlt_oct_size = cpu_to_be32(ndescs);
1661 in->seg.flags_pd = cpu_to_be32(to_mpd(pd)->pdn);
1662 in->seg.flags = MLX5_PERM_UMR_EN | MLX5_ACCESS_MODE_KLM |
1663 MLX5_PERM_LOCAL_READ;
1664 if (type == IB_MW_TYPE_2)
1665 in->seg.flags_pd |= cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
1666 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
1667
1668 err = mlx5_core_create_mkey(dev->mdev, &mw->mmkey, in, sizeof(*in),
1669 NULL, NULL, NULL);
1670 if (err)
1671 goto free;
1672
1673 mw->ibmw.rkey = mw->mmkey.key;
1674
1675 resp.response_length = min(offsetof(typeof(resp), response_length) +
1676 sizeof(resp.response_length), udata->outlen);
1677 if (resp.response_length) {
1678 err = ib_copy_to_udata(udata, &resp, resp.response_length);
1679 if (err) {
1680 mlx5_core_destroy_mkey(dev->mdev, &mw->mmkey);
1681 goto free;
1682 }
1683 }
1684
1685 kfree(in);
1686 return &mw->ibmw;
1687
1688free:
1689 kfree(mw);
1690 kfree(in);
1691 return ERR_PTR(err);
1692}
1693
1694int mlx5_ib_dealloc_mw(struct ib_mw *mw)
1695{
1696 struct mlx5_ib_mw *mmw = to_mmw(mw);
1697 int err;
1698
1699 err = mlx5_core_destroy_mkey((to_mdev(mw->device))->mdev,
1700 &mmw->mmkey);
1701 if (!err)
1702 kfree(mmw);
1703 return err;
1704}
1705
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02001706int mlx5_ib_check_mr_status(struct ib_mr *ibmr, u32 check_mask,
1707 struct ib_mr_status *mr_status)
1708{
1709 struct mlx5_ib_mr *mmr = to_mmr(ibmr);
1710 int ret = 0;
1711
1712 if (check_mask & ~IB_MR_CHECK_SIG_STATUS) {
1713 pr_err("Invalid status check mask\n");
1714 ret = -EINVAL;
1715 goto done;
1716 }
1717
1718 mr_status->fail_status = 0;
1719 if (check_mask & IB_MR_CHECK_SIG_STATUS) {
1720 if (!mmr->sig) {
1721 ret = -EINVAL;
1722 pr_err("signature status check requested on a non-signature enabled MR\n");
1723 goto done;
1724 }
1725
1726 mmr->sig->sig_status_checked = true;
1727 if (!mmr->sig->sig_err_exists)
1728 goto done;
1729
1730 if (ibmr->lkey == mmr->sig->err_item.key)
1731 memcpy(&mr_status->sig_err, &mmr->sig->err_item,
1732 sizeof(mr_status->sig_err));
1733 else {
1734 mr_status->sig_err.err_type = IB_SIG_BAD_GUARD;
1735 mr_status->sig_err.sig_err_offset = 0;
1736 mr_status->sig_err.key = mmr->sig->err_item.key;
1737 }
1738
1739 mmr->sig->sig_err_exists = false;
1740 mr_status->fail_status |= IB_MR_CHECK_SIG_STATUS;
1741 }
1742
1743done:
1744 return ret;
1745}
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001746
1747static int mlx5_set_page(struct ib_mr *ibmr, u64 addr)
1748{
1749 struct mlx5_ib_mr *mr = to_mmr(ibmr);
1750 __be64 *descs;
1751
1752 if (unlikely(mr->ndescs == mr->max_descs))
1753 return -ENOMEM;
1754
1755 descs = mr->descs;
1756 descs[mr->ndescs++] = cpu_to_be64(addr | MLX5_EN_RD | MLX5_EN_WR);
1757
1758 return 0;
1759}
1760
1761int mlx5_ib_map_mr_sg(struct ib_mr *ibmr,
1762 struct scatterlist *sg,
1763 int sg_nents)
1764{
1765 struct mlx5_ib_mr *mr = to_mmr(ibmr);
1766 int n;
1767
1768 mr->ndescs = 0;
1769
1770 ib_dma_sync_single_for_cpu(ibmr->device, mr->desc_map,
1771 mr->desc_size * mr->max_descs,
1772 DMA_TO_DEVICE);
1773
1774 n = ib_sg_to_pages(ibmr, sg, sg_nents, mlx5_set_page);
1775
1776 ib_dma_sync_single_for_device(ibmr->device, mr->desc_map,
1777 mr->desc_size * mr->max_descs,
1778 DMA_TO_DEVICE);
1779
1780 return n;
1781}