blob: 32a28bd50b20ae08c41a9a7086b72045f7934c3d [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
2 * Copyright (c) 2013, Mellanox Technologies inc. All rights reserved.
3 *
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
49#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
50static __be64 mlx5_ib_update_mtt_emergency_buffer[
51 MLX5_UMR_MTT_MIN_CHUNK_SIZE/sizeof(__be64)]
52 __aligned(MLX5_UMR_ALIGN);
53static DEFINE_MUTEX(mlx5_ib_update_mtt_emergency_buffer_mutex);
54#endif
Eli Cohenfe45f822013-09-11 16:35:35 +030055
Haggai Eran6aec21f2014-12-11 17:04:23 +020056static int clean_mr(struct mlx5_ib_mr *mr);
57
Haggai Eranb4cfe442014-12-11 17:04:26 +020058static int destroy_mkey(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
59{
60 int err = mlx5_core_destroy_mkey(dev->mdev, &mr->mmr);
61
62#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
63 /* Wait until all page fault handlers using the mr complete. */
64 synchronize_srcu(&dev->mr_srcu);
65#endif
66
67 return err;
68}
69
Eli Cohene126ba92013-07-07 17:25:49 +030070static int order2idx(struct mlx5_ib_dev *dev, int order)
71{
72 struct mlx5_mr_cache *cache = &dev->cache;
73
74 if (order < cache->ent[0].order)
75 return 0;
76 else
77 return order - cache->ent[0].order;
78}
79
Eli Cohen746b5582013-10-23 09:53:14 +030080static void reg_mr_callback(int status, void *context)
81{
82 struct mlx5_ib_mr *mr = context;
83 struct mlx5_ib_dev *dev = mr->dev;
84 struct mlx5_mr_cache *cache = &dev->cache;
85 int c = order2idx(dev, mr->order);
86 struct mlx5_cache_ent *ent = &cache->ent[c];
87 u8 key;
Eli Cohen746b5582013-10-23 09:53:14 +030088 unsigned long flags;
Jack Morgenstein9603b612014-07-28 23:30:22 +030089 struct mlx5_mr_table *table = &dev->mdev->priv.mr_table;
Haggai Eran86059332014-05-22 14:50:09 +030090 int err;
Eli Cohen746b5582013-10-23 09:53:14 +030091
Eli Cohen746b5582013-10-23 09:53:14 +030092 spin_lock_irqsave(&ent->lock, flags);
93 ent->pending--;
94 spin_unlock_irqrestore(&ent->lock, flags);
95 if (status) {
96 mlx5_ib_warn(dev, "async reg mr failed. status %d\n", status);
97 kfree(mr);
98 dev->fill_delay = 1;
99 mod_timer(&dev->delay_timer, jiffies + HZ);
100 return;
101 }
102
103 if (mr->out.hdr.status) {
104 mlx5_ib_warn(dev, "failed - status %d, syndorme 0x%x\n",
105 mr->out.hdr.status,
106 be32_to_cpu(mr->out.hdr.syndrome));
107 kfree(mr);
108 dev->fill_delay = 1;
109 mod_timer(&dev->delay_timer, jiffies + HZ);
110 return;
111 }
112
Jack Morgenstein9603b612014-07-28 23:30:22 +0300113 spin_lock_irqsave(&dev->mdev->priv.mkey_lock, flags);
114 key = dev->mdev->priv.mkey_key++;
115 spin_unlock_irqrestore(&dev->mdev->priv.mkey_lock, flags);
Eli Cohen746b5582013-10-23 09:53:14 +0300116 mr->mmr.key = mlx5_idx_to_mkey(be32_to_cpu(mr->out.mkey) & 0xffffff) | key;
117
118 cache->last_add = jiffies;
119
120 spin_lock_irqsave(&ent->lock, flags);
121 list_add_tail(&mr->list, &ent->head);
122 ent->cur++;
123 ent->size++;
124 spin_unlock_irqrestore(&ent->lock, flags);
Haggai Eran86059332014-05-22 14:50:09 +0300125
126 write_lock_irqsave(&table->lock, flags);
127 err = radix_tree_insert(&table->tree, mlx5_base_mkey(mr->mmr.key),
128 &mr->mmr);
129 if (err)
130 pr_err("Error inserting to mr tree. 0x%x\n", -err);
131 write_unlock_irqrestore(&table->lock, flags);
Eli Cohen746b5582013-10-23 09:53:14 +0300132}
133
Eli Cohene126ba92013-07-07 17:25:49 +0300134static int add_keys(struct mlx5_ib_dev *dev, int c, int num)
135{
Eli Cohene126ba92013-07-07 17:25:49 +0300136 struct mlx5_mr_cache *cache = &dev->cache;
137 struct mlx5_cache_ent *ent = &cache->ent[c];
138 struct mlx5_create_mkey_mbox_in *in;
139 struct mlx5_ib_mr *mr;
140 int npages = 1 << ent->order;
Eli Cohene126ba92013-07-07 17:25:49 +0300141 int err = 0;
142 int i;
143
144 in = kzalloc(sizeof(*in), GFP_KERNEL);
145 if (!in)
146 return -ENOMEM;
147
148 for (i = 0; i < num; i++) {
Eli Cohen746b5582013-10-23 09:53:14 +0300149 if (ent->pending >= MAX_PENDING_REG_MR) {
150 err = -EAGAIN;
151 break;
152 }
153
Eli Cohene126ba92013-07-07 17:25:49 +0300154 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
155 if (!mr) {
156 err = -ENOMEM;
Eli Cohen746b5582013-10-23 09:53:14 +0300157 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300158 }
159 mr->order = ent->order;
160 mr->umred = 1;
Eli Cohen746b5582013-10-23 09:53:14 +0300161 mr->dev = dev;
Haggai Eran968e78d2014-12-11 17:04:11 +0200162 in->seg.status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +0300163 in->seg.xlt_oct_size = cpu_to_be32((npages + 1) / 2);
164 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
165 in->seg.flags = MLX5_ACCESS_MODE_MTT | MLX5_PERM_UMR_EN;
166 in->seg.log2_page_size = 12;
167
Eli Cohen746b5582013-10-23 09:53:14 +0300168 spin_lock_irq(&ent->lock);
169 ent->pending++;
170 spin_unlock_irq(&ent->lock);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300171 err = mlx5_core_create_mkey(dev->mdev, &mr->mmr, in,
Eli Cohen746b5582013-10-23 09:53:14 +0300172 sizeof(*in), reg_mr_callback,
173 mr, &mr->out);
Eli Cohene126ba92013-07-07 17:25:49 +0300174 if (err) {
Eli Cohend14e7112014-12-02 12:26:19 +0200175 spin_lock_irq(&ent->lock);
176 ent->pending--;
177 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300178 mlx5_ib_warn(dev, "create mkey failed %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +0300179 kfree(mr);
Eli Cohen746b5582013-10-23 09:53:14 +0300180 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300181 }
Eli Cohene126ba92013-07-07 17:25:49 +0300182 }
183
Eli Cohene126ba92013-07-07 17:25:49 +0300184 kfree(in);
185 return err;
186}
187
188static void remove_keys(struct mlx5_ib_dev *dev, int c, int num)
189{
Eli Cohene126ba92013-07-07 17:25:49 +0300190 struct mlx5_mr_cache *cache = &dev->cache;
191 struct mlx5_cache_ent *ent = &cache->ent[c];
192 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +0300193 int err;
194 int i;
195
196 for (i = 0; i < num; i++) {
Eli Cohen746b5582013-10-23 09:53:14 +0300197 spin_lock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300198 if (list_empty(&ent->head)) {
Eli Cohen746b5582013-10-23 09:53:14 +0300199 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300200 return;
201 }
202 mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
203 list_del(&mr->list);
204 ent->cur--;
205 ent->size--;
Eli Cohen746b5582013-10-23 09:53:14 +0300206 spin_unlock_irq(&ent->lock);
Haggai Eranb4cfe442014-12-11 17:04:26 +0200207 err = destroy_mkey(dev, mr);
Eli Cohen203099f2013-09-11 16:35:26 +0300208 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +0300209 mlx5_ib_warn(dev, "failed destroy mkey\n");
Eli Cohen203099f2013-09-11 16:35:26 +0300210 else
Eli Cohene126ba92013-07-07 17:25:49 +0300211 kfree(mr);
Eli Cohene126ba92013-07-07 17:25:49 +0300212 }
213}
214
215static ssize_t size_write(struct file *filp, const char __user *buf,
216 size_t count, loff_t *pos)
217{
218 struct mlx5_cache_ent *ent = filp->private_data;
219 struct mlx5_ib_dev *dev = ent->dev;
220 char lbuf[20];
221 u32 var;
222 int err;
223 int c;
224
225 if (copy_from_user(lbuf, buf, sizeof(lbuf)))
Dan Carpenter5e631a02013-07-10 13:58:59 +0300226 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +0300227
228 c = order2idx(dev, ent->order);
229 lbuf[sizeof(lbuf) - 1] = 0;
230
231 if (sscanf(lbuf, "%u", &var) != 1)
232 return -EINVAL;
233
234 if (var < ent->limit)
235 return -EINVAL;
236
237 if (var > ent->size) {
Eli Cohen746b5582013-10-23 09:53:14 +0300238 do {
239 err = add_keys(dev, c, var - ent->size);
240 if (err && err != -EAGAIN)
241 return err;
242
243 usleep_range(3000, 5000);
244 } while (err);
Eli Cohene126ba92013-07-07 17:25:49 +0300245 } else if (var < ent->size) {
246 remove_keys(dev, c, ent->size - var);
247 }
248
249 return count;
250}
251
252static ssize_t size_read(struct file *filp, char __user *buf, size_t count,
253 loff_t *pos)
254{
255 struct mlx5_cache_ent *ent = filp->private_data;
256 char lbuf[20];
257 int err;
258
259 if (*pos)
260 return 0;
261
262 err = snprintf(lbuf, sizeof(lbuf), "%d\n", ent->size);
263 if (err < 0)
264 return err;
265
266 if (copy_to_user(buf, lbuf, err))
Dan Carpenter5e631a02013-07-10 13:58:59 +0300267 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +0300268
269 *pos += err;
270
271 return err;
272}
273
274static const struct file_operations size_fops = {
275 .owner = THIS_MODULE,
276 .open = simple_open,
277 .write = size_write,
278 .read = size_read,
279};
280
281static ssize_t limit_write(struct file *filp, const char __user *buf,
282 size_t count, loff_t *pos)
283{
284 struct mlx5_cache_ent *ent = filp->private_data;
285 struct mlx5_ib_dev *dev = ent->dev;
286 char lbuf[20];
287 u32 var;
288 int err;
289 int c;
290
291 if (copy_from_user(lbuf, buf, sizeof(lbuf)))
Dan Carpenter5e631a02013-07-10 13:58:59 +0300292 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +0300293
294 c = order2idx(dev, ent->order);
295 lbuf[sizeof(lbuf) - 1] = 0;
296
297 if (sscanf(lbuf, "%u", &var) != 1)
298 return -EINVAL;
299
300 if (var > ent->size)
301 return -EINVAL;
302
303 ent->limit = var;
304
305 if (ent->cur < ent->limit) {
306 err = add_keys(dev, c, 2 * ent->limit - ent->cur);
307 if (err)
308 return err;
309 }
310
311 return count;
312}
313
314static ssize_t limit_read(struct file *filp, char __user *buf, size_t count,
315 loff_t *pos)
316{
317 struct mlx5_cache_ent *ent = filp->private_data;
318 char lbuf[20];
319 int err;
320
321 if (*pos)
322 return 0;
323
324 err = snprintf(lbuf, sizeof(lbuf), "%d\n", ent->limit);
325 if (err < 0)
326 return err;
327
328 if (copy_to_user(buf, lbuf, err))
Dan Carpenter5e631a02013-07-10 13:58:59 +0300329 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +0300330
331 *pos += err;
332
333 return err;
334}
335
336static const struct file_operations limit_fops = {
337 .owner = THIS_MODULE,
338 .open = simple_open,
339 .write = limit_write,
340 .read = limit_read,
341};
342
343static int someone_adding(struct mlx5_mr_cache *cache)
344{
345 int i;
346
347 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
348 if (cache->ent[i].cur < cache->ent[i].limit)
349 return 1;
350 }
351
352 return 0;
353}
354
355static void __cache_work_func(struct mlx5_cache_ent *ent)
356{
357 struct mlx5_ib_dev *dev = ent->dev;
358 struct mlx5_mr_cache *cache = &dev->cache;
359 int i = order2idx(dev, ent->order);
Eli Cohen746b5582013-10-23 09:53:14 +0300360 int err;
Eli Cohene126ba92013-07-07 17:25:49 +0300361
362 if (cache->stopped)
363 return;
364
365 ent = &dev->cache.ent[i];
Eli Cohen746b5582013-10-23 09:53:14 +0300366 if (ent->cur < 2 * ent->limit && !dev->fill_delay) {
367 err = add_keys(dev, i, 1);
368 if (ent->cur < 2 * ent->limit) {
369 if (err == -EAGAIN) {
370 mlx5_ib_dbg(dev, "returned eagain, order %d\n",
371 i + 2);
372 queue_delayed_work(cache->wq, &ent->dwork,
373 msecs_to_jiffies(3));
374 } else if (err) {
375 mlx5_ib_warn(dev, "command failed order %d, err %d\n",
376 i + 2, err);
377 queue_delayed_work(cache->wq, &ent->dwork,
378 msecs_to_jiffies(1000));
379 } else {
380 queue_work(cache->wq, &ent->work);
381 }
382 }
Eli Cohene126ba92013-07-07 17:25:49 +0300383 } else if (ent->cur > 2 * ent->limit) {
384 if (!someone_adding(cache) &&
Eli Cohen746b5582013-10-23 09:53:14 +0300385 time_after(jiffies, cache->last_add + 300 * HZ)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300386 remove_keys(dev, i, 1);
387 if (ent->cur > ent->limit)
388 queue_work(cache->wq, &ent->work);
389 } else {
Eli Cohen746b5582013-10-23 09:53:14 +0300390 queue_delayed_work(cache->wq, &ent->dwork, 300 * HZ);
Eli Cohene126ba92013-07-07 17:25:49 +0300391 }
392 }
393}
394
395static void delayed_cache_work_func(struct work_struct *work)
396{
397 struct mlx5_cache_ent *ent;
398
399 ent = container_of(work, struct mlx5_cache_ent, dwork.work);
400 __cache_work_func(ent);
401}
402
403static void cache_work_func(struct work_struct *work)
404{
405 struct mlx5_cache_ent *ent;
406
407 ent = container_of(work, struct mlx5_cache_ent, work);
408 __cache_work_func(ent);
409}
410
411static struct mlx5_ib_mr *alloc_cached_mr(struct mlx5_ib_dev *dev, int order)
412{
413 struct mlx5_mr_cache *cache = &dev->cache;
414 struct mlx5_ib_mr *mr = NULL;
415 struct mlx5_cache_ent *ent;
416 int c;
417 int i;
418
419 c = order2idx(dev, order);
420 if (c < 0 || c >= MAX_MR_CACHE_ENTRIES) {
421 mlx5_ib_warn(dev, "order %d, cache index %d\n", order, c);
422 return NULL;
423 }
424
425 for (i = c; i < MAX_MR_CACHE_ENTRIES; i++) {
426 ent = &cache->ent[i];
427
428 mlx5_ib_dbg(dev, "order %d, cache index %d\n", ent->order, i);
429
Eli Cohen746b5582013-10-23 09:53:14 +0300430 spin_lock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300431 if (!list_empty(&ent->head)) {
432 mr = list_first_entry(&ent->head, struct mlx5_ib_mr,
433 list);
434 list_del(&mr->list);
435 ent->cur--;
Eli Cohen746b5582013-10-23 09:53:14 +0300436 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300437 if (ent->cur < ent->limit)
438 queue_work(cache->wq, &ent->work);
439 break;
440 }
Eli Cohen746b5582013-10-23 09:53:14 +0300441 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300442
443 queue_work(cache->wq, &ent->work);
444
445 if (mr)
446 break;
447 }
448
449 if (!mr)
450 cache->ent[c].miss++;
451
452 return mr;
453}
454
455static void free_cached_mr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
456{
457 struct mlx5_mr_cache *cache = &dev->cache;
458 struct mlx5_cache_ent *ent;
459 int shrink = 0;
460 int c;
461
462 c = order2idx(dev, mr->order);
463 if (c < 0 || c >= MAX_MR_CACHE_ENTRIES) {
464 mlx5_ib_warn(dev, "order %d, cache index %d\n", mr->order, c);
465 return;
466 }
467 ent = &cache->ent[c];
Eli Cohen746b5582013-10-23 09:53:14 +0300468 spin_lock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300469 list_add_tail(&mr->list, &ent->head);
470 ent->cur++;
471 if (ent->cur > 2 * ent->limit)
472 shrink = 1;
Eli Cohen746b5582013-10-23 09:53:14 +0300473 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300474
475 if (shrink)
476 queue_work(cache->wq, &ent->work);
477}
478
479static void clean_keys(struct mlx5_ib_dev *dev, int c)
480{
Eli Cohene126ba92013-07-07 17:25:49 +0300481 struct mlx5_mr_cache *cache = &dev->cache;
482 struct mlx5_cache_ent *ent = &cache->ent[c];
483 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +0300484 int err;
485
Moshe Lazer3c461912013-09-11 16:35:23 +0300486 cancel_delayed_work(&ent->dwork);
Eli Cohene126ba92013-07-07 17:25:49 +0300487 while (1) {
Eli Cohen746b5582013-10-23 09:53:14 +0300488 spin_lock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300489 if (list_empty(&ent->head)) {
Eli Cohen746b5582013-10-23 09:53:14 +0300490 spin_unlock_irq(&ent->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300491 return;
492 }
493 mr = list_first_entry(&ent->head, struct mlx5_ib_mr, list);
494 list_del(&mr->list);
495 ent->cur--;
496 ent->size--;
Eli Cohen746b5582013-10-23 09:53:14 +0300497 spin_unlock_irq(&ent->lock);
Haggai Eranb4cfe442014-12-11 17:04:26 +0200498 err = destroy_mkey(dev, mr);
Eli Cohen203099f2013-09-11 16:35:26 +0300499 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +0300500 mlx5_ib_warn(dev, "failed destroy mkey\n");
Eli Cohen203099f2013-09-11 16:35:26 +0300501 else
Eli Cohene126ba92013-07-07 17:25:49 +0300502 kfree(mr);
Eli Cohene126ba92013-07-07 17:25:49 +0300503 }
504}
505
506static int mlx5_mr_cache_debugfs_init(struct mlx5_ib_dev *dev)
507{
508 struct mlx5_mr_cache *cache = &dev->cache;
509 struct mlx5_cache_ent *ent;
510 int i;
511
512 if (!mlx5_debugfs_root)
513 return 0;
514
Jack Morgenstein9603b612014-07-28 23:30:22 +0300515 cache->root = debugfs_create_dir("mr_cache", dev->mdev->priv.dbg_root);
Eli Cohene126ba92013-07-07 17:25:49 +0300516 if (!cache->root)
517 return -ENOMEM;
518
519 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
520 ent = &cache->ent[i];
521 sprintf(ent->name, "%d", ent->order);
522 ent->dir = debugfs_create_dir(ent->name, cache->root);
523 if (!ent->dir)
524 return -ENOMEM;
525
526 ent->fsize = debugfs_create_file("size", 0600, ent->dir, ent,
527 &size_fops);
528 if (!ent->fsize)
529 return -ENOMEM;
530
531 ent->flimit = debugfs_create_file("limit", 0600, ent->dir, ent,
532 &limit_fops);
533 if (!ent->flimit)
534 return -ENOMEM;
535
536 ent->fcur = debugfs_create_u32("cur", 0400, ent->dir,
537 &ent->cur);
538 if (!ent->fcur)
539 return -ENOMEM;
540
541 ent->fmiss = debugfs_create_u32("miss", 0600, ent->dir,
542 &ent->miss);
543 if (!ent->fmiss)
544 return -ENOMEM;
545 }
546
547 return 0;
548}
549
550static void mlx5_mr_cache_debugfs_cleanup(struct mlx5_ib_dev *dev)
551{
552 if (!mlx5_debugfs_root)
553 return;
554
555 debugfs_remove_recursive(dev->cache.root);
556}
557
Eli Cohen746b5582013-10-23 09:53:14 +0300558static void delay_time_func(unsigned long ctx)
559{
560 struct mlx5_ib_dev *dev = (struct mlx5_ib_dev *)ctx;
561
562 dev->fill_delay = 0;
563}
564
Eli Cohene126ba92013-07-07 17:25:49 +0300565int mlx5_mr_cache_init(struct mlx5_ib_dev *dev)
566{
567 struct mlx5_mr_cache *cache = &dev->cache;
568 struct mlx5_cache_ent *ent;
569 int limit;
Eli Cohene126ba92013-07-07 17:25:49 +0300570 int err;
571 int i;
572
573 cache->wq = create_singlethread_workqueue("mkey_cache");
574 if (!cache->wq) {
575 mlx5_ib_warn(dev, "failed to create work queue\n");
576 return -ENOMEM;
577 }
578
Eli Cohen746b5582013-10-23 09:53:14 +0300579 setup_timer(&dev->delay_timer, delay_time_func, (unsigned long)dev);
Eli Cohene126ba92013-07-07 17:25:49 +0300580 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++) {
581 INIT_LIST_HEAD(&cache->ent[i].head);
582 spin_lock_init(&cache->ent[i].lock);
583
584 ent = &cache->ent[i];
585 INIT_LIST_HEAD(&ent->head);
586 spin_lock_init(&ent->lock);
587 ent->order = i + 2;
588 ent->dev = dev;
589
Jack Morgenstein9603b612014-07-28 23:30:22 +0300590 if (dev->mdev->profile->mask & MLX5_PROF_MASK_MR_CACHE)
591 limit = dev->mdev->profile->mr_cache[i].limit;
Eli Cohen2d036fa2013-10-24 12:01:00 +0300592 else
Eli Cohene126ba92013-07-07 17:25:49 +0300593 limit = 0;
Eli Cohen2d036fa2013-10-24 12:01:00 +0300594
Eli Cohene126ba92013-07-07 17:25:49 +0300595 INIT_WORK(&ent->work, cache_work_func);
596 INIT_DELAYED_WORK(&ent->dwork, delayed_cache_work_func);
597 ent->limit = limit;
598 queue_work(cache->wq, &ent->work);
599 }
600
601 err = mlx5_mr_cache_debugfs_init(dev);
602 if (err)
603 mlx5_ib_warn(dev, "cache debugfs failure\n");
604
605 return 0;
606}
607
608int mlx5_mr_cache_cleanup(struct mlx5_ib_dev *dev)
609{
610 int i;
611
612 dev->cache.stopped = 1;
Moshe Lazer3c461912013-09-11 16:35:23 +0300613 flush_workqueue(dev->cache.wq);
Eli Cohene126ba92013-07-07 17:25:49 +0300614
615 mlx5_mr_cache_debugfs_cleanup(dev);
616
617 for (i = 0; i < MAX_MR_CACHE_ENTRIES; i++)
618 clean_keys(dev, i);
619
Moshe Lazer3c461912013-09-11 16:35:23 +0300620 destroy_workqueue(dev->cache.wq);
Eli Cohen746b5582013-10-23 09:53:14 +0300621 del_timer_sync(&dev->delay_timer);
Moshe Lazer3c461912013-09-11 16:35:23 +0300622
Eli Cohene126ba92013-07-07 17:25:49 +0300623 return 0;
624}
625
626struct ib_mr *mlx5_ib_get_dma_mr(struct ib_pd *pd, int acc)
627{
628 struct mlx5_ib_dev *dev = to_mdev(pd->device);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300629 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300630 struct mlx5_create_mkey_mbox_in *in;
631 struct mlx5_mkey_seg *seg;
632 struct mlx5_ib_mr *mr;
633 int err;
634
635 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
636 if (!mr)
637 return ERR_PTR(-ENOMEM);
638
639 in = kzalloc(sizeof(*in), GFP_KERNEL);
640 if (!in) {
641 err = -ENOMEM;
642 goto err_free;
643 }
644
645 seg = &in->seg;
646 seg->flags = convert_access(acc) | MLX5_ACCESS_MODE_PA;
647 seg->flags_pd = cpu_to_be32(to_mpd(pd)->pdn | MLX5_MKEY_LEN64);
648 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
649 seg->start_addr = 0;
650
Eli Cohen746b5582013-10-23 09:53:14 +0300651 err = mlx5_core_create_mkey(mdev, &mr->mmr, in, sizeof(*in), NULL, NULL,
652 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +0300653 if (err)
654 goto err_in;
655
656 kfree(in);
657 mr->ibmr.lkey = mr->mmr.key;
658 mr->ibmr.rkey = mr->mmr.key;
659 mr->umem = NULL;
660
661 return &mr->ibmr;
662
663err_in:
664 kfree(in);
665
666err_free:
667 kfree(mr);
668
669 return ERR_PTR(err);
670}
671
672static int get_octo_len(u64 addr, u64 len, int page_size)
673{
674 u64 offset;
675 int npages;
676
677 offset = addr & (page_size - 1);
678 npages = ALIGN(len + offset, page_size) >> ilog2(page_size);
679 return (npages + 1) / 2;
680}
681
682static int use_umr(int order)
683{
Haggai Erancc149f752014-12-11 17:04:21 +0200684 return order <= MLX5_MAX_UMR_SHIFT;
Eli Cohene126ba92013-07-07 17:25:49 +0300685}
686
687static void prep_umr_reg_wqe(struct ib_pd *pd, struct ib_send_wr *wr,
688 struct ib_sge *sg, u64 dma, int n, u32 key,
689 int page_shift, u64 virt_addr, u64 len,
690 int access_flags)
691{
692 struct mlx5_ib_dev *dev = to_mdev(pd->device);
693 struct ib_mr *mr = dev->umrc.mr;
Haggai Eran968e78d2014-12-11 17:04:11 +0200694 struct mlx5_umr_wr *umrwr = (struct mlx5_umr_wr *)&wr->wr.fast_reg;
Eli Cohene126ba92013-07-07 17:25:49 +0300695
696 sg->addr = dma;
697 sg->length = ALIGN(sizeof(u64) * n, 64);
698 sg->lkey = mr->lkey;
699
700 wr->next = NULL;
701 wr->send_flags = 0;
702 wr->sg_list = sg;
703 if (n)
704 wr->num_sge = 1;
705 else
706 wr->num_sge = 0;
707
708 wr->opcode = MLX5_IB_WR_UMR;
Haggai Eran968e78d2014-12-11 17:04:11 +0200709
710 umrwr->npages = n;
711 umrwr->page_shift = page_shift;
712 umrwr->mkey = key;
713 umrwr->target.virt_addr = virt_addr;
714 umrwr->length = len;
715 umrwr->access_flags = access_flags;
716 umrwr->pd = pd;
Eli Cohene126ba92013-07-07 17:25:49 +0300717}
718
719static void prep_umr_unreg_wqe(struct mlx5_ib_dev *dev,
720 struct ib_send_wr *wr, u32 key)
721{
Haggai Eran968e78d2014-12-11 17:04:11 +0200722 struct mlx5_umr_wr *umrwr = (struct mlx5_umr_wr *)&wr->wr.fast_reg;
723
724 wr->send_flags = MLX5_IB_SEND_UMR_UNREG | MLX5_IB_SEND_UMR_FAIL_IF_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +0300725 wr->opcode = MLX5_IB_WR_UMR;
Haggai Eran968e78d2014-12-11 17:04:11 +0200726 umrwr->mkey = key;
Eli Cohene126ba92013-07-07 17:25:49 +0300727}
728
729void mlx5_umr_cq_handler(struct ib_cq *cq, void *cq_context)
730{
Shachar Raindela74d2412014-05-22 14:50:12 +0300731 struct mlx5_ib_umr_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +0300732 struct ib_wc wc;
733 int err;
734
735 while (1) {
736 err = ib_poll_cq(cq, 1, &wc);
737 if (err < 0) {
738 pr_warn("poll cq error %d\n", err);
739 return;
740 }
741 if (err == 0)
742 break;
743
Roland Dreier6c9b5d92014-05-28 09:23:03 -0700744 context = (struct mlx5_ib_umr_context *) (unsigned long) wc.wr_id;
Shachar Raindela74d2412014-05-22 14:50:12 +0300745 context->status = wc.status;
746 complete(&context->done);
Eli Cohene126ba92013-07-07 17:25:49 +0300747 }
748 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
749}
750
751static struct mlx5_ib_mr *reg_umr(struct ib_pd *pd, struct ib_umem *umem,
752 u64 virt_addr, u64 len, int npages,
753 int page_shift, int order, int access_flags)
754{
755 struct mlx5_ib_dev *dev = to_mdev(pd->device);
Eli Cohen203099f2013-09-11 16:35:26 +0300756 struct device *ddev = dev->ib_dev.dma_device;
Eli Cohene126ba92013-07-07 17:25:49 +0300757 struct umr_common *umrc = &dev->umrc;
Shachar Raindela74d2412014-05-22 14:50:12 +0300758 struct mlx5_ib_umr_context umr_context;
Eli Cohene126ba92013-07-07 17:25:49 +0300759 struct ib_send_wr wr, *bad;
760 struct mlx5_ib_mr *mr;
761 struct ib_sge sg;
Haggai Erancc149f752014-12-11 17:04:21 +0200762 int size;
Haggai Eran21af2c32014-12-11 17:04:10 +0200763 __be64 *mr_pas;
Haggai Erancc149f752014-12-11 17:04:21 +0200764 __be64 *pas;
Haggai Eran21af2c32014-12-11 17:04:10 +0200765 dma_addr_t dma;
Haggai Eran096f7e72014-05-22 14:50:08 +0300766 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300767 int i;
768
Eli Cohen746b5582013-10-23 09:53:14 +0300769 for (i = 0; i < 1; i++) {
Eli Cohene126ba92013-07-07 17:25:49 +0300770 mr = alloc_cached_mr(dev, order);
771 if (mr)
772 break;
773
774 err = add_keys(dev, order2idx(dev, order), 1);
Eli Cohen746b5582013-10-23 09:53:14 +0300775 if (err && err != -EAGAIN) {
776 mlx5_ib_warn(dev, "add_keys failed, err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +0300777 break;
778 }
779 }
780
781 if (!mr)
782 return ERR_PTR(-EAGAIN);
783
Haggai Erancc149f752014-12-11 17:04:21 +0200784 /* UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes.
785 * To avoid copying garbage after the pas array, we allocate
786 * a little more. */
787 size = ALIGN(sizeof(u64) * npages, MLX5_UMR_MTT_ALIGNMENT);
Haggai Eran21af2c32014-12-11 17:04:10 +0200788 mr_pas = kmalloc(size + MLX5_UMR_ALIGN - 1, GFP_KERNEL);
789 if (!mr_pas) {
Eli Cohen203099f2013-09-11 16:35:26 +0300790 err = -ENOMEM;
Haggai Eran096f7e72014-05-22 14:50:08 +0300791 goto free_mr;
Eli Cohen203099f2013-09-11 16:35:26 +0300792 }
Eli Cohen543139072013-09-11 16:35:36 +0300793
Haggai Erancc149f752014-12-11 17:04:21 +0200794 pas = PTR_ALIGN(mr_pas, MLX5_UMR_ALIGN);
795 mlx5_ib_populate_pas(dev, umem, page_shift, pas, MLX5_IB_MTT_PRESENT);
796 /* Clear padding after the actual pages. */
797 memset(pas + npages, 0, size - npages * sizeof(u64));
Eli Cohen543139072013-09-11 16:35:36 +0300798
Haggai Erancc149f752014-12-11 17:04:21 +0200799 dma = dma_map_single(ddev, pas, size, DMA_TO_DEVICE);
Haggai Eran21af2c32014-12-11 17:04:10 +0200800 if (dma_mapping_error(ddev, dma)) {
Eli Cohen203099f2013-09-11 16:35:26 +0300801 err = -ENOMEM;
Haggai Eran096f7e72014-05-22 14:50:08 +0300802 goto free_pas;
Eli Cohen203099f2013-09-11 16:35:26 +0300803 }
804
Eli Cohene126ba92013-07-07 17:25:49 +0300805 memset(&wr, 0, sizeof(wr));
Shachar Raindela74d2412014-05-22 14:50:12 +0300806 wr.wr_id = (u64)(unsigned long)&umr_context;
Haggai Eran21af2c32014-12-11 17:04:10 +0200807 prep_umr_reg_wqe(pd, &wr, &sg, dma, npages, mr->mmr.key, page_shift,
808 virt_addr, len, access_flags);
Eli Cohene126ba92013-07-07 17:25:49 +0300809
Shachar Raindela74d2412014-05-22 14:50:12 +0300810 mlx5_ib_init_umr_context(&umr_context);
Eli Cohene126ba92013-07-07 17:25:49 +0300811 down(&umrc->sem);
Eli Cohene126ba92013-07-07 17:25:49 +0300812 err = ib_post_send(umrc->qp, &wr, &bad);
813 if (err) {
814 mlx5_ib_warn(dev, "post send failed, err %d\n", err);
Haggai Eran096f7e72014-05-22 14:50:08 +0300815 goto unmap_dma;
Shachar Raindela74d2412014-05-22 14:50:12 +0300816 } else {
817 wait_for_completion(&umr_context.done);
818 if (umr_context.status != IB_WC_SUCCESS) {
819 mlx5_ib_warn(dev, "reg umr failed\n");
820 err = -EFAULT;
821 }
Haggai Eran096f7e72014-05-22 14:50:08 +0300822 }
823
Haggai Eranb4755982014-05-22 14:50:10 +0300824 mr->mmr.iova = virt_addr;
825 mr->mmr.size = len;
826 mr->mmr.pd = to_mpd(pd)->pdn;
827
Haggai Eranb4cfe442014-12-11 17:04:26 +0200828 mr->live = 1;
829
Haggai Eran096f7e72014-05-22 14:50:08 +0300830unmap_dma:
831 up(&umrc->sem);
Haggai Eran21af2c32014-12-11 17:04:10 +0200832 dma_unmap_single(ddev, dma, size, DMA_TO_DEVICE);
Haggai Eran096f7e72014-05-22 14:50:08 +0300833
834free_pas:
Haggai Eran21af2c32014-12-11 17:04:10 +0200835 kfree(mr_pas);
Haggai Eran096f7e72014-05-22 14:50:08 +0300836
837free_mr:
838 if (err) {
839 free_cached_mr(dev, mr);
840 return ERR_PTR(err);
Eli Cohene126ba92013-07-07 17:25:49 +0300841 }
842
843 return mr;
Eli Cohene126ba92013-07-07 17:25:49 +0300844}
845
Haggai Eran832a6b02014-12-11 17:04:22 +0200846#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
847int mlx5_ib_update_mtt(struct mlx5_ib_mr *mr, u64 start_page_index, int npages,
848 int zap)
849{
850 struct mlx5_ib_dev *dev = mr->dev;
851 struct device *ddev = dev->ib_dev.dma_device;
852 struct umr_common *umrc = &dev->umrc;
853 struct mlx5_ib_umr_context umr_context;
854 struct ib_umem *umem = mr->umem;
855 int size;
856 __be64 *pas;
857 dma_addr_t dma;
858 struct ib_send_wr wr, *bad;
859 struct mlx5_umr_wr *umrwr = (struct mlx5_umr_wr *)&wr.wr.fast_reg;
860 struct ib_sge sg;
861 int err = 0;
862 const int page_index_alignment = MLX5_UMR_MTT_ALIGNMENT / sizeof(u64);
863 const int page_index_mask = page_index_alignment - 1;
864 size_t pages_mapped = 0;
865 size_t pages_to_map = 0;
866 size_t pages_iter = 0;
867 int use_emergency_buf = 0;
868
869 /* UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes,
870 * so we need to align the offset and length accordingly */
871 if (start_page_index & page_index_mask) {
872 npages += start_page_index & page_index_mask;
873 start_page_index &= ~page_index_mask;
874 }
875
876 pages_to_map = ALIGN(npages, page_index_alignment);
877
878 if (start_page_index + pages_to_map > MLX5_MAX_UMR_PAGES)
879 return -EINVAL;
880
881 size = sizeof(u64) * pages_to_map;
882 size = min_t(int, PAGE_SIZE, size);
883 /* We allocate with GFP_ATOMIC to avoid recursion into page-reclaim
884 * code, when we are called from an invalidation. The pas buffer must
885 * be 2k-aligned for Connect-IB. */
886 pas = (__be64 *)get_zeroed_page(GFP_ATOMIC);
887 if (!pas) {
888 mlx5_ib_warn(dev, "unable to allocate memory during MTT update, falling back to slower chunked mechanism.\n");
889 pas = mlx5_ib_update_mtt_emergency_buffer;
890 size = MLX5_UMR_MTT_MIN_CHUNK_SIZE;
891 use_emergency_buf = 1;
892 mutex_lock(&mlx5_ib_update_mtt_emergency_buffer_mutex);
893 memset(pas, 0, size);
894 }
895 pages_iter = size / sizeof(u64);
896 dma = dma_map_single(ddev, pas, size, DMA_TO_DEVICE);
897 if (dma_mapping_error(ddev, dma)) {
898 mlx5_ib_err(dev, "unable to map DMA during MTT update.\n");
899 err = -ENOMEM;
900 goto free_pas;
901 }
902
903 for (pages_mapped = 0;
904 pages_mapped < pages_to_map && !err;
905 pages_mapped += pages_iter, start_page_index += pages_iter) {
906 dma_sync_single_for_cpu(ddev, dma, size, DMA_TO_DEVICE);
907
908 npages = min_t(size_t,
909 pages_iter,
910 ib_umem_num_pages(umem) - start_page_index);
911
912 if (!zap) {
913 __mlx5_ib_populate_pas(dev, umem, PAGE_SHIFT,
914 start_page_index, npages, pas,
915 MLX5_IB_MTT_PRESENT);
916 /* Clear padding after the pages brought from the
917 * umem. */
918 memset(pas + npages, 0, size - npages * sizeof(u64));
919 }
920
921 dma_sync_single_for_device(ddev, dma, size, DMA_TO_DEVICE);
922
923 memset(&wr, 0, sizeof(wr));
924 wr.wr_id = (u64)(unsigned long)&umr_context;
925
926 sg.addr = dma;
927 sg.length = ALIGN(npages * sizeof(u64),
928 MLX5_UMR_MTT_ALIGNMENT);
929 sg.lkey = dev->umrc.mr->lkey;
930
931 wr.send_flags = MLX5_IB_SEND_UMR_FAIL_IF_FREE |
932 MLX5_IB_SEND_UMR_UPDATE_MTT;
933 wr.sg_list = &sg;
934 wr.num_sge = 1;
935 wr.opcode = MLX5_IB_WR_UMR;
936 umrwr->npages = sg.length / sizeof(u64);
937 umrwr->page_shift = PAGE_SHIFT;
938 umrwr->mkey = mr->mmr.key;
939 umrwr->target.offset = start_page_index;
940
941 mlx5_ib_init_umr_context(&umr_context);
942 down(&umrc->sem);
943 err = ib_post_send(umrc->qp, &wr, &bad);
944 if (err) {
945 mlx5_ib_err(dev, "UMR post send failed, err %d\n", err);
946 } else {
947 wait_for_completion(&umr_context.done);
948 if (umr_context.status != IB_WC_SUCCESS) {
949 mlx5_ib_err(dev, "UMR completion failed, code %d\n",
950 umr_context.status);
951 err = -EFAULT;
952 }
953 }
954 up(&umrc->sem);
955 }
956 dma_unmap_single(ddev, dma, size, DMA_TO_DEVICE);
957
958free_pas:
959 if (!use_emergency_buf)
960 free_page((unsigned long)pas);
961 else
962 mutex_unlock(&mlx5_ib_update_mtt_emergency_buffer_mutex);
963
964 return err;
965}
966#endif
967
Eli Cohene126ba92013-07-07 17:25:49 +0300968static struct mlx5_ib_mr *reg_create(struct ib_pd *pd, u64 virt_addr,
969 u64 length, struct ib_umem *umem,
970 int npages, int page_shift,
971 int access_flags)
972{
973 struct mlx5_ib_dev *dev = to_mdev(pd->device);
974 struct mlx5_create_mkey_mbox_in *in;
975 struct mlx5_ib_mr *mr;
976 int inlen;
977 int err;
Haggai Erancc149f752014-12-11 17:04:21 +0200978 bool pg_cap = !!(dev->mdev->caps.gen.flags &
979 MLX5_DEV_CAP_FLAG_ON_DMND_PG);
Eli Cohene126ba92013-07-07 17:25:49 +0300980
981 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
982 if (!mr)
983 return ERR_PTR(-ENOMEM);
984
985 inlen = sizeof(*in) + sizeof(*in->pas) * ((npages + 1) / 2) * 2;
986 in = mlx5_vzalloc(inlen);
987 if (!in) {
988 err = -ENOMEM;
989 goto err_1;
990 }
Haggai Erancc149f752014-12-11 17:04:21 +0200991 mlx5_ib_populate_pas(dev, umem, page_shift, in->pas,
992 pg_cap ? MLX5_IB_MTT_PRESENT : 0);
Eli Cohene126ba92013-07-07 17:25:49 +0300993
Haggai Erancc149f752014-12-11 17:04:21 +0200994 /* The MLX5_MKEY_INBOX_PG_ACCESS bit allows setting the access flags
995 * in the page list submitted with the command. */
996 in->flags = pg_cap ? cpu_to_be32(MLX5_MKEY_INBOX_PG_ACCESS) : 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300997 in->seg.flags = convert_access(access_flags) |
998 MLX5_ACCESS_MODE_MTT;
999 in->seg.flags_pd = cpu_to_be32(to_mpd(pd)->pdn);
1000 in->seg.start_addr = cpu_to_be64(virt_addr);
1001 in->seg.len = cpu_to_be64(length);
1002 in->seg.bsfs_octo_size = 0;
1003 in->seg.xlt_oct_size = cpu_to_be32(get_octo_len(virt_addr, length, 1 << page_shift));
1004 in->seg.log2_page_size = page_shift;
1005 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
Eli Cohen746b5582013-10-23 09:53:14 +03001006 in->xlat_oct_act_size = cpu_to_be32(get_octo_len(virt_addr, length,
1007 1 << page_shift));
Jack Morgenstein9603b612014-07-28 23:30:22 +03001008 err = mlx5_core_create_mkey(dev->mdev, &mr->mmr, in, inlen, NULL,
Eli Cohen746b5582013-10-23 09:53:14 +03001009 NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03001010 if (err) {
1011 mlx5_ib_warn(dev, "create mkey failed\n");
1012 goto err_2;
1013 }
1014 mr->umem = umem;
Haggai Eranb4cfe442014-12-11 17:04:26 +02001015 mr->live = 1;
Al Viro479163f2014-11-20 08:13:57 +00001016 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001017
1018 mlx5_ib_dbg(dev, "mkey = 0x%x\n", mr->mmr.key);
1019
1020 return mr;
1021
1022err_2:
Al Viro479163f2014-11-20 08:13:57 +00001023 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001024
1025err_1:
1026 kfree(mr);
1027
1028 return ERR_PTR(err);
1029}
1030
1031struct ib_mr *mlx5_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
1032 u64 virt_addr, int access_flags,
1033 struct ib_udata *udata)
1034{
1035 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1036 struct mlx5_ib_mr *mr = NULL;
1037 struct ib_umem *umem;
1038 int page_shift;
1039 int npages;
1040 int ncont;
1041 int order;
1042 int err;
1043
Eli Cohen900a6d72014-09-14 16:47:51 +03001044 mlx5_ib_dbg(dev, "start 0x%llx, virt_addr 0x%llx, length 0x%llx, access_flags 0x%x\n",
1045 start, virt_addr, length, access_flags);
Eli Cohene126ba92013-07-07 17:25:49 +03001046 umem = ib_umem_get(pd->uobject->context, start, length, access_flags,
1047 0);
1048 if (IS_ERR(umem)) {
Eli Cohen900a6d72014-09-14 16:47:51 +03001049 mlx5_ib_dbg(dev, "umem get failed (%ld)\n", PTR_ERR(umem));
Eli Cohene126ba92013-07-07 17:25:49 +03001050 return (void *)umem;
1051 }
1052
1053 mlx5_ib_cont_pages(umem, start, &npages, &page_shift, &ncont, &order);
1054 if (!npages) {
1055 mlx5_ib_warn(dev, "avoid zero region\n");
1056 err = -EINVAL;
1057 goto error;
1058 }
1059
1060 mlx5_ib_dbg(dev, "npages %d, ncont %d, order %d, page_shift %d\n",
1061 npages, ncont, order, page_shift);
1062
1063 if (use_umr(order)) {
1064 mr = reg_umr(pd, umem, virt_addr, length, ncont, page_shift,
1065 order, access_flags);
1066 if (PTR_ERR(mr) == -EAGAIN) {
1067 mlx5_ib_dbg(dev, "cache empty for order %d", order);
1068 mr = NULL;
1069 }
Haggai Eran6aec21f2014-12-11 17:04:23 +02001070 } else if (access_flags & IB_ACCESS_ON_DEMAND) {
1071 err = -EINVAL;
1072 pr_err("Got MR registration for ODP MR > 512MB, not supported for Connect-IB");
1073 goto error;
Eli Cohene126ba92013-07-07 17:25:49 +03001074 }
1075
1076 if (!mr)
1077 mr = reg_create(pd, virt_addr, length, umem, ncont, page_shift,
1078 access_flags);
1079
1080 if (IS_ERR(mr)) {
1081 err = PTR_ERR(mr);
1082 goto error;
1083 }
1084
1085 mlx5_ib_dbg(dev, "mkey 0x%x\n", mr->mmr.key);
1086
1087 mr->umem = umem;
1088 mr->npages = npages;
Haggai Eran6aec21f2014-12-11 17:04:23 +02001089 atomic_add(npages, &dev->mdev->priv.reg_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03001090 mr->ibmr.lkey = mr->mmr.key;
1091 mr->ibmr.rkey = mr->mmr.key;
1092
Haggai Eranb4cfe442014-12-11 17:04:26 +02001093#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1094 if (umem->odp_data) {
1095 /*
1096 * This barrier prevents the compiler from moving the
1097 * setting of umem->odp_data->private to point to our
1098 * MR, before reg_umr finished, to ensure that the MR
1099 * initialization have finished before starting to
1100 * handle invalidations.
1101 */
1102 smp_wmb();
1103 mr->umem->odp_data->private = mr;
1104 /*
1105 * Make sure we will see the new
1106 * umem->odp_data->private value in the invalidation
1107 * routines, before we can get page faults on the
1108 * MR. Page faults can happen once we put the MR in
1109 * the tree, below this line. Without the barrier,
1110 * there can be a fault handling and an invalidation
1111 * before umem->odp_data->private == mr is visible to
1112 * the invalidation handler.
1113 */
1114 smp_wmb();
1115 }
1116#endif
1117
Eli Cohene126ba92013-07-07 17:25:49 +03001118 return &mr->ibmr;
1119
1120error:
Haggai Eranb4cfe442014-12-11 17:04:26 +02001121 /*
1122 * Destroy the umem *before* destroying the MR, to ensure we
1123 * will not have any in-flight notifiers when destroying the
1124 * MR.
1125 *
1126 * As the MR is completely invalid to begin with, and this
1127 * error path is only taken if we can't push the mr entry into
1128 * the pagefault tree, this is safe.
1129 */
1130
Eli Cohene126ba92013-07-07 17:25:49 +03001131 ib_umem_release(umem);
Haggai Eranb4cfe442014-12-11 17:04:26 +02001132 /* Kill the MR, and return an error code. */
1133 clean_mr(mr);
Eli Cohene126ba92013-07-07 17:25:49 +03001134 return ERR_PTR(err);
1135}
1136
1137static int unreg_umr(struct mlx5_ib_dev *dev, struct mlx5_ib_mr *mr)
1138{
1139 struct umr_common *umrc = &dev->umrc;
Shachar Raindela74d2412014-05-22 14:50:12 +03001140 struct mlx5_ib_umr_context umr_context;
Eli Cohene126ba92013-07-07 17:25:49 +03001141 struct ib_send_wr wr, *bad;
1142 int err;
1143
1144 memset(&wr, 0, sizeof(wr));
Shachar Raindela74d2412014-05-22 14:50:12 +03001145 wr.wr_id = (u64)(unsigned long)&umr_context;
Eli Cohene126ba92013-07-07 17:25:49 +03001146 prep_umr_unreg_wqe(dev, &wr, mr->mmr.key);
1147
Shachar Raindela74d2412014-05-22 14:50:12 +03001148 mlx5_ib_init_umr_context(&umr_context);
Eli Cohene126ba92013-07-07 17:25:49 +03001149 down(&umrc->sem);
Eli Cohene126ba92013-07-07 17:25:49 +03001150 err = ib_post_send(umrc->qp, &wr, &bad);
1151 if (err) {
1152 up(&umrc->sem);
1153 mlx5_ib_dbg(dev, "err %d\n", err);
1154 goto error;
Shachar Raindela74d2412014-05-22 14:50:12 +03001155 } else {
1156 wait_for_completion(&umr_context.done);
1157 up(&umrc->sem);
Eli Cohene126ba92013-07-07 17:25:49 +03001158 }
Shachar Raindela74d2412014-05-22 14:50:12 +03001159 if (umr_context.status != IB_WC_SUCCESS) {
Eli Cohene126ba92013-07-07 17:25:49 +03001160 mlx5_ib_warn(dev, "unreg umr failed\n");
1161 err = -EFAULT;
1162 goto error;
1163 }
1164 return 0;
1165
1166error:
1167 return err;
1168}
1169
Haggai Eran6aec21f2014-12-11 17:04:23 +02001170static int clean_mr(struct mlx5_ib_mr *mr)
Eli Cohene126ba92013-07-07 17:25:49 +03001171{
Haggai Eran6aec21f2014-12-11 17:04:23 +02001172 struct mlx5_ib_dev *dev = to_mdev(mr->ibmr.device);
Eli Cohene126ba92013-07-07 17:25:49 +03001173 int umred = mr->umred;
1174 int err;
1175
1176 if (!umred) {
Haggai Eranb4cfe442014-12-11 17:04:26 +02001177 err = destroy_mkey(dev, mr);
Eli Cohene126ba92013-07-07 17:25:49 +03001178 if (err) {
1179 mlx5_ib_warn(dev, "failed to destroy mkey 0x%x (%d)\n",
1180 mr->mmr.key, err);
1181 return err;
1182 }
1183 } else {
1184 err = unreg_umr(dev, mr);
1185 if (err) {
1186 mlx5_ib_warn(dev, "failed unregister\n");
1187 return err;
1188 }
1189 free_cached_mr(dev, mr);
1190 }
1191
Eli Cohene126ba92013-07-07 17:25:49 +03001192 if (!umred)
1193 kfree(mr);
1194
1195 return 0;
1196}
1197
Haggai Eran6aec21f2014-12-11 17:04:23 +02001198int mlx5_ib_dereg_mr(struct ib_mr *ibmr)
1199{
1200 struct mlx5_ib_dev *dev = to_mdev(ibmr->device);
1201 struct mlx5_ib_mr *mr = to_mmr(ibmr);
1202 int npages = mr->npages;
1203 struct ib_umem *umem = mr->umem;
1204
1205#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Haggai Eranb4cfe442014-12-11 17:04:26 +02001206 if (umem && umem->odp_data) {
1207 /* Prevent new page faults from succeeding */
1208 mr->live = 0;
Haggai Eran6aec21f2014-12-11 17:04:23 +02001209 /* Wait for all running page-fault handlers to finish. */
1210 synchronize_srcu(&dev->mr_srcu);
Haggai Eranb4cfe442014-12-11 17:04:26 +02001211 /* Destroy all page mappings */
1212 mlx5_ib_invalidate_range(umem, ib_umem_start(umem),
1213 ib_umem_end(umem));
1214 /*
1215 * We kill the umem before the MR for ODP,
1216 * so that there will not be any invalidations in
1217 * flight, looking at the *mr struct.
1218 */
1219 ib_umem_release(umem);
1220 atomic_sub(npages, &dev->mdev->priv.reg_pages);
1221
1222 /* Avoid double-freeing the umem. */
1223 umem = NULL;
1224 }
Haggai Eran6aec21f2014-12-11 17:04:23 +02001225#endif
1226
1227 clean_mr(mr);
1228
1229 if (umem) {
1230 ib_umem_release(umem);
1231 atomic_sub(npages, &dev->mdev->priv.reg_pages);
1232 }
1233
1234 return 0;
1235}
1236
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001237struct ib_mr *mlx5_ib_create_mr(struct ib_pd *pd,
1238 struct ib_mr_init_attr *mr_init_attr)
1239{
1240 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1241 struct mlx5_create_mkey_mbox_in *in;
1242 struct mlx5_ib_mr *mr;
1243 int access_mode, err;
1244 int ndescs = roundup(mr_init_attr->max_reg_descriptors, 4);
1245
1246 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1247 if (!mr)
1248 return ERR_PTR(-ENOMEM);
1249
1250 in = kzalloc(sizeof(*in), GFP_KERNEL);
1251 if (!in) {
1252 err = -ENOMEM;
1253 goto err_free;
1254 }
1255
Haggai Eran968e78d2014-12-11 17:04:11 +02001256 in->seg.status = MLX5_MKEY_STATUS_FREE;
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001257 in->seg.xlt_oct_size = cpu_to_be32(ndescs);
1258 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
1259 in->seg.flags_pd = cpu_to_be32(to_mpd(pd)->pdn);
1260 access_mode = MLX5_ACCESS_MODE_MTT;
1261
1262 if (mr_init_attr->flags & IB_MR_SIGNATURE_EN) {
1263 u32 psv_index[2];
1264
1265 in->seg.flags_pd = cpu_to_be32(be32_to_cpu(in->seg.flags_pd) |
1266 MLX5_MKEY_BSF_EN);
1267 in->seg.bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
1268 mr->sig = kzalloc(sizeof(*mr->sig), GFP_KERNEL);
1269 if (!mr->sig) {
1270 err = -ENOMEM;
1271 goto err_free_in;
1272 }
1273
1274 /* create mem & wire PSVs */
Jack Morgenstein9603b612014-07-28 23:30:22 +03001275 err = mlx5_core_create_psv(dev->mdev, to_mpd(pd)->pdn,
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001276 2, psv_index);
1277 if (err)
1278 goto err_free_sig;
1279
1280 access_mode = MLX5_ACCESS_MODE_KLM;
1281 mr->sig->psv_memory.psv_idx = psv_index[0];
1282 mr->sig->psv_wire.psv_idx = psv_index[1];
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02001283
1284 mr->sig->sig_status_checked = true;
1285 mr->sig->sig_err_exists = false;
1286 /* Next UMR, Arm SIGERR */
1287 ++mr->sig->sigerr_count;
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001288 }
1289
1290 in->seg.flags = MLX5_PERM_UMR_EN | access_mode;
Jack Morgenstein9603b612014-07-28 23:30:22 +03001291 err = mlx5_core_create_mkey(dev->mdev, &mr->mmr, in, sizeof(*in),
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001292 NULL, NULL, NULL);
1293 if (err)
1294 goto err_destroy_psv;
1295
1296 mr->ibmr.lkey = mr->mmr.key;
1297 mr->ibmr.rkey = mr->mmr.key;
1298 mr->umem = NULL;
1299 kfree(in);
1300
1301 return &mr->ibmr;
1302
1303err_destroy_psv:
1304 if (mr->sig) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001305 if (mlx5_core_destroy_psv(dev->mdev,
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001306 mr->sig->psv_memory.psv_idx))
1307 mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
1308 mr->sig->psv_memory.psv_idx);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001309 if (mlx5_core_destroy_psv(dev->mdev,
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001310 mr->sig->psv_wire.psv_idx))
1311 mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
1312 mr->sig->psv_wire.psv_idx);
1313 }
1314err_free_sig:
1315 kfree(mr->sig);
1316err_free_in:
1317 kfree(in);
1318err_free:
1319 kfree(mr);
1320 return ERR_PTR(err);
1321}
1322
1323int mlx5_ib_destroy_mr(struct ib_mr *ibmr)
1324{
1325 struct mlx5_ib_dev *dev = to_mdev(ibmr->device);
1326 struct mlx5_ib_mr *mr = to_mmr(ibmr);
1327 int err;
1328
1329 if (mr->sig) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001330 if (mlx5_core_destroy_psv(dev->mdev,
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001331 mr->sig->psv_memory.psv_idx))
1332 mlx5_ib_warn(dev, "failed to destroy mem psv %d\n",
1333 mr->sig->psv_memory.psv_idx);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001334 if (mlx5_core_destroy_psv(dev->mdev,
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001335 mr->sig->psv_wire.psv_idx))
1336 mlx5_ib_warn(dev, "failed to destroy wire psv %d\n",
1337 mr->sig->psv_wire.psv_idx);
1338 kfree(mr->sig);
1339 }
1340
Haggai Eranb4cfe442014-12-11 17:04:26 +02001341 err = destroy_mkey(dev, mr);
Sagi Grimberg3121e3c2014-02-23 14:19:06 +02001342 if (err) {
1343 mlx5_ib_warn(dev, "failed to destroy mkey 0x%x (%d)\n",
1344 mr->mmr.key, err);
1345 return err;
1346 }
1347
1348 kfree(mr);
1349
1350 return err;
1351}
1352
Eli Cohene126ba92013-07-07 17:25:49 +03001353struct ib_mr *mlx5_ib_alloc_fast_reg_mr(struct ib_pd *pd,
1354 int max_page_list_len)
1355{
1356 struct mlx5_ib_dev *dev = to_mdev(pd->device);
1357 struct mlx5_create_mkey_mbox_in *in;
1358 struct mlx5_ib_mr *mr;
1359 int err;
1360
1361 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1362 if (!mr)
1363 return ERR_PTR(-ENOMEM);
1364
1365 in = kzalloc(sizeof(*in), GFP_KERNEL);
1366 if (!in) {
1367 err = -ENOMEM;
1368 goto err_free;
1369 }
1370
Haggai Eran968e78d2014-12-11 17:04:11 +02001371 in->seg.status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03001372 in->seg.xlt_oct_size = cpu_to_be32((max_page_list_len + 1) / 2);
1373 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
1374 in->seg.flags = MLX5_PERM_UMR_EN | MLX5_ACCESS_MODE_MTT;
1375 in->seg.flags_pd = cpu_to_be32(to_mpd(pd)->pdn);
1376 /*
1377 * TBD not needed - issue 197292 */
1378 in->seg.log2_page_size = PAGE_SHIFT;
1379
Jack Morgenstein9603b612014-07-28 23:30:22 +03001380 err = mlx5_core_create_mkey(dev->mdev, &mr->mmr, in, sizeof(*in), NULL,
Eli Cohen746b5582013-10-23 09:53:14 +03001381 NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03001382 kfree(in);
1383 if (err)
1384 goto err_free;
1385
1386 mr->ibmr.lkey = mr->mmr.key;
1387 mr->ibmr.rkey = mr->mmr.key;
1388 mr->umem = NULL;
1389
1390 return &mr->ibmr;
1391
1392err_free:
1393 kfree(mr);
1394 return ERR_PTR(err);
1395}
1396
1397struct ib_fast_reg_page_list *mlx5_ib_alloc_fast_reg_page_list(struct ib_device *ibdev,
1398 int page_list_len)
1399{
1400 struct mlx5_ib_fast_reg_page_list *mfrpl;
1401 int size = page_list_len * sizeof(u64);
1402
1403 mfrpl = kmalloc(sizeof(*mfrpl), GFP_KERNEL);
1404 if (!mfrpl)
1405 return ERR_PTR(-ENOMEM);
1406
1407 mfrpl->ibfrpl.page_list = kmalloc(size, GFP_KERNEL);
1408 if (!mfrpl->ibfrpl.page_list)
1409 goto err_free;
1410
1411 mfrpl->mapped_page_list = dma_alloc_coherent(ibdev->dma_device,
1412 size, &mfrpl->map,
1413 GFP_KERNEL);
1414 if (!mfrpl->mapped_page_list)
1415 goto err_free;
1416
1417 WARN_ON(mfrpl->map & 0x3f);
1418
1419 return &mfrpl->ibfrpl;
1420
1421err_free:
1422 kfree(mfrpl->ibfrpl.page_list);
1423 kfree(mfrpl);
1424 return ERR_PTR(-ENOMEM);
1425}
1426
1427void mlx5_ib_free_fast_reg_page_list(struct ib_fast_reg_page_list *page_list)
1428{
1429 struct mlx5_ib_fast_reg_page_list *mfrpl = to_mfrpl(page_list);
1430 struct mlx5_ib_dev *dev = to_mdev(page_list->device);
1431 int size = page_list->max_page_list_len * sizeof(u64);
1432
Jack Morgenstein9603b612014-07-28 23:30:22 +03001433 dma_free_coherent(&dev->mdev->pdev->dev, size, mfrpl->mapped_page_list,
Eli Cohene126ba92013-07-07 17:25:49 +03001434 mfrpl->map);
1435 kfree(mfrpl->ibfrpl.page_list);
1436 kfree(mfrpl);
1437}
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02001438
1439int mlx5_ib_check_mr_status(struct ib_mr *ibmr, u32 check_mask,
1440 struct ib_mr_status *mr_status)
1441{
1442 struct mlx5_ib_mr *mmr = to_mmr(ibmr);
1443 int ret = 0;
1444
1445 if (check_mask & ~IB_MR_CHECK_SIG_STATUS) {
1446 pr_err("Invalid status check mask\n");
1447 ret = -EINVAL;
1448 goto done;
1449 }
1450
1451 mr_status->fail_status = 0;
1452 if (check_mask & IB_MR_CHECK_SIG_STATUS) {
1453 if (!mmr->sig) {
1454 ret = -EINVAL;
1455 pr_err("signature status check requested on a non-signature enabled MR\n");
1456 goto done;
1457 }
1458
1459 mmr->sig->sig_status_checked = true;
1460 if (!mmr->sig->sig_err_exists)
1461 goto done;
1462
1463 if (ibmr->lkey == mmr->sig->err_item.key)
1464 memcpy(&mr_status->sig_err, &mmr->sig->err_item,
1465 sizeof(mr_status->sig_err));
1466 else {
1467 mr_status->sig_err.err_type = IB_SIG_BAD_GUARD;
1468 mr_status->sig_err.sig_err_offset = 0;
1469 mr_status->sig_err.key = mmr->sig->err_item.key;
1470 }
1471
1472 mmr->sig->sig_err_exists = false;
1473 mr_status->fail_status |= IB_MR_CHECK_SIG_STATUS;
1474 }
1475
1476done:
1477 return ret;
1478}