IB/cma: cma_validate_port should verify the port and netdevice
[linux-2.6-block.git] / drivers / infiniband / core / cache.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
2a1d9b7f
RD
3 * Copyright (c) 2005 Intel Corporation. All rights reserved.
4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
5 * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
1da177e4
LT
6 *
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
12 *
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
16 *
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
1da177e4
LT
34 */
35
1da177e4
LT
36#include <linux/module.h>
37#include <linux/errno.h>
38#include <linux/slab.h>
e8edc6e0 39#include <linux/workqueue.h>
03db3a2d
MB
40#include <linux/netdevice.h>
41#include <net/addrconf.h>
1da177e4 42
a4d61e84 43#include <rdma/ib_cache.h>
1da177e4
LT
44
45#include "core_priv.h"
46
47struct ib_pkey_cache {
48 int table_len;
49 u16 table[0];
50};
51
1da177e4
LT
52struct ib_update_work {
53 struct work_struct work;
54 struct ib_device *device;
55 u8 port_num;
56};
57
e26be1bf
MS
58union ib_gid zgid;
59EXPORT_SYMBOL(zgid);
03db3a2d
MB
60
61static const struct ib_gid_attr zattr;
62
63enum gid_attr_find_mask {
64 GID_ATTR_FIND_MASK_GID = 1UL << 0,
65 GID_ATTR_FIND_MASK_NETDEV = 1UL << 1,
66 GID_ATTR_FIND_MASK_DEFAULT = 1UL << 2,
67};
68
69enum gid_table_entry_props {
70 GID_TABLE_ENTRY_INVALID = 1UL << 0,
71 GID_TABLE_ENTRY_DEFAULT = 1UL << 1,
72};
73
74enum gid_table_write_action {
75 GID_TABLE_WRITE_ACTION_ADD,
76 GID_TABLE_WRITE_ACTION_DEL,
77 /* MODIFY only updates the GID table. Currently only used by
78 * ib_cache_update.
79 */
80 GID_TABLE_WRITE_ACTION_MODIFY
81};
82
83struct ib_gid_table_entry {
84 /* This lock protects an entry from being
85 * read and written simultaneously.
86 */
87 rwlock_t lock;
88 unsigned long props;
89 union ib_gid gid;
90 struct ib_gid_attr attr;
91 void *context;
92};
93
94struct ib_gid_table {
95 int sz;
96 /* In RoCE, adding a GID to the table requires:
97 * (a) Find if this GID is already exists.
98 * (b) Find a free space.
99 * (c) Write the new GID
100 *
101 * Delete requires different set of operations:
102 * (a) Find the GID
103 * (b) Delete it.
104 *
105 * Add/delete should be carried out atomically.
106 * This is done by locking this mutex from multiple
107 * writers. We don't need this lock for IB, as the MAD
108 * layer replaces all entries. All data_vec entries
109 * are locked by this lock.
110 **/
111 struct mutex lock;
112 struct ib_gid_table_entry *data_vec;
113};
114
115static int write_gid(struct ib_device *ib_dev, u8 port,
116 struct ib_gid_table *table, int ix,
117 const union ib_gid *gid,
118 const struct ib_gid_attr *attr,
119 enum gid_table_write_action action,
120 bool default_gid)
1da177e4 121{
03db3a2d
MB
122 int ret = 0;
123 struct net_device *old_net_dev;
1da177e4 124 unsigned long flags;
03db3a2d
MB
125
126 /* in rdma_cap_roce_gid_table, this funciton should be protected by a
127 * sleep-able lock.
128 */
129 write_lock_irqsave(&table->data_vec[ix].lock, flags);
130
131 if (rdma_cap_roce_gid_table(ib_dev, port)) {
132 table->data_vec[ix].props |= GID_TABLE_ENTRY_INVALID;
133 write_unlock_irqrestore(&table->data_vec[ix].lock, flags);
134 /* GID_TABLE_WRITE_ACTION_MODIFY currently isn't supported by
135 * RoCE providers and thus only updates the cache.
136 */
137 if (action == GID_TABLE_WRITE_ACTION_ADD)
138 ret = ib_dev->add_gid(ib_dev, port, ix, gid, attr,
139 &table->data_vec[ix].context);
140 else if (action == GID_TABLE_WRITE_ACTION_DEL)
141 ret = ib_dev->del_gid(ib_dev, port, ix,
142 &table->data_vec[ix].context);
143 write_lock_irqsave(&table->data_vec[ix].lock, flags);
144 }
145
146 old_net_dev = table->data_vec[ix].attr.ndev;
147 if (old_net_dev && old_net_dev != attr->ndev)
148 dev_put(old_net_dev);
149 /* if modify_gid failed, just delete the old gid */
150 if (ret || action == GID_TABLE_WRITE_ACTION_DEL) {
151 gid = &zgid;
152 attr = &zattr;
153 table->data_vec[ix].context = NULL;
154 }
155 if (default_gid)
156 table->data_vec[ix].props |= GID_TABLE_ENTRY_DEFAULT;
157 memcpy(&table->data_vec[ix].gid, gid, sizeof(*gid));
158 memcpy(&table->data_vec[ix].attr, attr, sizeof(*attr));
159 if (table->data_vec[ix].attr.ndev &&
160 table->data_vec[ix].attr.ndev != old_net_dev)
161 dev_hold(table->data_vec[ix].attr.ndev);
162
163 table->data_vec[ix].props &= ~GID_TABLE_ENTRY_INVALID;
164
165 write_unlock_irqrestore(&table->data_vec[ix].lock, flags);
166
167 if (!ret && rdma_cap_roce_gid_table(ib_dev, port)) {
168 struct ib_event event;
169
170 event.device = ib_dev;
171 event.element.port_num = port;
172 event.event = IB_EVENT_GID_CHANGE;
173
174 ib_dispatch_event(&event);
175 }
176 return ret;
177}
178
179static int add_gid(struct ib_device *ib_dev, u8 port,
180 struct ib_gid_table *table, int ix,
181 const union ib_gid *gid,
182 const struct ib_gid_attr *attr,
183 bool default_gid) {
184 return write_gid(ib_dev, port, table, ix, gid, attr,
185 GID_TABLE_WRITE_ACTION_ADD, default_gid);
186}
187
188static int modify_gid(struct ib_device *ib_dev, u8 port,
189 struct ib_gid_table *table, int ix,
190 const union ib_gid *gid,
191 const struct ib_gid_attr *attr,
192 bool default_gid) {
193 return write_gid(ib_dev, port, table, ix, gid, attr,
194 GID_TABLE_WRITE_ACTION_MODIFY, default_gid);
195}
196
197static int del_gid(struct ib_device *ib_dev, u8 port,
198 struct ib_gid_table *table, int ix,
199 bool default_gid) {
200 return write_gid(ib_dev, port, table, ix, &zgid, &zattr,
201 GID_TABLE_WRITE_ACTION_DEL, default_gid);
202}
203
204static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,
205 const struct ib_gid_attr *val, bool default_gid,
206 unsigned long mask)
207{
208 int i;
209
210 for (i = 0; i < table->sz; i++) {
211 unsigned long flags;
212 struct ib_gid_attr *attr = &table->data_vec[i].attr;
213
214 read_lock_irqsave(&table->data_vec[i].lock, flags);
215
216 if (table->data_vec[i].props & GID_TABLE_ENTRY_INVALID)
217 goto next;
218
219 if (mask & GID_ATTR_FIND_MASK_GID &&
220 memcmp(gid, &table->data_vec[i].gid, sizeof(*gid)))
221 goto next;
222
223 if (mask & GID_ATTR_FIND_MASK_NETDEV &&
224 attr->ndev != val->ndev)
225 goto next;
226
227 if (mask & GID_ATTR_FIND_MASK_DEFAULT &&
228 !!(table->data_vec[i].props & GID_TABLE_ENTRY_DEFAULT) !=
229 default_gid)
230 goto next;
231
232 read_unlock_irqrestore(&table->data_vec[i].lock, flags);
233 return i;
234next:
235 read_unlock_irqrestore(&table->data_vec[i].lock, flags);
236 }
237
238 return -1;
239}
240
241static void make_default_gid(struct net_device *dev, union ib_gid *gid)
242{
243 gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
244 addrconf_ifid_eui48(&gid->raw[8], dev);
245}
246
247int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
248 union ib_gid *gid, struct ib_gid_attr *attr)
249{
250 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
251 struct ib_gid_table *table;
252 int ix;
1da177e4 253 int ret = 0;
03db3a2d 254 struct net_device *idev;
1da177e4 255
03db3a2d
MB
256 table = ports_table[port - rdma_start_port(ib_dev)];
257
258 if (!memcmp(gid, &zgid, sizeof(*gid)))
1da177e4
LT
259 return -EINVAL;
260
03db3a2d
MB
261 if (ib_dev->get_netdev) {
262 idev = ib_dev->get_netdev(ib_dev, port);
263 if (idev && attr->ndev != idev) {
264 union ib_gid default_gid;
1da177e4 265
03db3a2d
MB
266 /* Adding default GIDs in not permitted */
267 make_default_gid(idev, &default_gid);
268 if (!memcmp(gid, &default_gid, sizeof(*gid))) {
269 dev_put(idev);
270 return -EPERM;
271 }
272 }
273 if (idev)
274 dev_put(idev);
275 }
1da177e4 276
03db3a2d 277 mutex_lock(&table->lock);
1da177e4 278
03db3a2d
MB
279 ix = find_gid(table, gid, attr, false, GID_ATTR_FIND_MASK_GID |
280 GID_ATTR_FIND_MASK_NETDEV);
281 if (ix >= 0)
282 goto out_unlock;
1da177e4 283
03db3a2d
MB
284 ix = find_gid(table, &zgid, NULL, false, GID_ATTR_FIND_MASK_GID |
285 GID_ATTR_FIND_MASK_DEFAULT);
286 if (ix < 0) {
287 ret = -ENOSPC;
288 goto out_unlock;
289 }
290
291 add_gid(ib_dev, port, table, ix, gid, attr, false);
292
293out_unlock:
294 mutex_unlock(&table->lock);
1da177e4
LT
295 return ret;
296}
1da177e4 297
03db3a2d
MB
298int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
299 union ib_gid *gid, struct ib_gid_attr *attr)
1da177e4 300{
03db3a2d
MB
301 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
302 struct ib_gid_table *table;
303 int ix;
304
305 table = ports_table[port - rdma_start_port(ib_dev)];
306
307 mutex_lock(&table->lock);
308
309 ix = find_gid(table, gid, attr, false,
310 GID_ATTR_FIND_MASK_GID |
311 GID_ATTR_FIND_MASK_NETDEV |
312 GID_ATTR_FIND_MASK_DEFAULT);
313 if (ix < 0)
314 goto out_unlock;
315
316 del_gid(ib_dev, port, table, ix, false);
317
318out_unlock:
319 mutex_unlock(&table->lock);
320 return 0;
321}
322
323int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
324 struct net_device *ndev)
325{
326 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
327 struct ib_gid_table *table;
328 int ix;
329
330 table = ports_table[port - rdma_start_port(ib_dev)];
331
332 mutex_lock(&table->lock);
333
334 for (ix = 0; ix < table->sz; ix++)
335 if (table->data_vec[ix].attr.ndev == ndev)
336 del_gid(ib_dev, port, table, ix, false);
337
338 mutex_unlock(&table->lock);
339 return 0;
340}
341
342static int __ib_cache_gid_get(struct ib_device *ib_dev, u8 port, int index,
343 union ib_gid *gid, struct ib_gid_attr *attr)
344{
345 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
346 struct ib_gid_table *table;
1da177e4 347 unsigned long flags;
1da177e4 348
03db3a2d 349 table = ports_table[port - rdma_start_port(ib_dev)];
1da177e4 350
03db3a2d
MB
351 if (index < 0 || index >= table->sz)
352 return -EINVAL;
1da177e4 353
03db3a2d
MB
354 read_lock_irqsave(&table->data_vec[index].lock, flags);
355 if (table->data_vec[index].props & GID_TABLE_ENTRY_INVALID) {
356 read_unlock_irqrestore(&table->data_vec[index].lock, flags);
357 return -EAGAIN;
358 }
359
360 memcpy(gid, &table->data_vec[index].gid, sizeof(*gid));
361 if (attr) {
362 memcpy(attr, &table->data_vec[index].attr, sizeof(*attr));
363 if (attr->ndev)
364 dev_hold(attr->ndev);
365 }
366
367 read_unlock_irqrestore(&table->data_vec[index].lock, flags);
368 return 0;
369}
370
371static int _ib_cache_gid_table_find(struct ib_device *ib_dev,
372 const union ib_gid *gid,
373 const struct ib_gid_attr *val,
374 unsigned long mask,
375 u8 *port, u16 *index)
376{
377 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
378 struct ib_gid_table *table;
379 u8 p;
380 int local_index;
381
382 for (p = 0; p < ib_dev->phys_port_cnt; p++) {
383 table = ports_table[p];
384 local_index = find_gid(table, gid, val, false, mask);
385 if (local_index >= 0) {
386 if (index)
387 *index = local_index;
388 if (port)
389 *port = p + rdma_start_port(ib_dev);
390 return 0;
1da177e4
LT
391 }
392 }
1da177e4 393
03db3a2d
MB
394 return -ENOENT;
395}
396
397static int ib_cache_gid_find(struct ib_device *ib_dev,
398 const union ib_gid *gid,
399 struct net_device *ndev, u8 *port,
400 u16 *index)
401{
402 unsigned long mask = GID_ATTR_FIND_MASK_GID;
403 struct ib_gid_attr gid_attr_val = {.ndev = ndev};
404
405 if (ndev)
406 mask |= GID_ATTR_FIND_MASK_NETDEV;
407
408 return _ib_cache_gid_table_find(ib_dev, gid, &gid_attr_val,
409 mask, port, index);
410}
411
d300ec52
MB
412int ib_find_cached_gid_by_port(struct ib_device *ib_dev,
413 const union ib_gid *gid,
414 u8 port, struct net_device *ndev,
415 u16 *index)
03db3a2d
MB
416{
417 int local_index;
418 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
419 struct ib_gid_table *table;
420 unsigned long mask = GID_ATTR_FIND_MASK_GID;
421 struct ib_gid_attr val = {.ndev = ndev};
422
423 if (port < rdma_start_port(ib_dev) ||
424 port > rdma_end_port(ib_dev))
425 return -ENOENT;
426
427 table = ports_table[port - rdma_start_port(ib_dev)];
428
429 if (ndev)
430 mask |= GID_ATTR_FIND_MASK_NETDEV;
431
432 local_index = find_gid(table, gid, &val, false, mask);
433 if (local_index >= 0) {
434 if (index)
435 *index = local_index;
436 return 0;
437 }
438
439 return -ENOENT;
440}
d300ec52 441EXPORT_SYMBOL(ib_find_cached_gid_by_port);
03db3a2d
MB
442
443static struct ib_gid_table *alloc_gid_table(int sz)
444{
445 unsigned int i;
446 struct ib_gid_table *table =
447 kzalloc(sizeof(struct ib_gid_table), GFP_KERNEL);
448 if (!table)
449 return NULL;
450
451 table->data_vec = kcalloc(sz, sizeof(*table->data_vec), GFP_KERNEL);
452 if (!table->data_vec)
453 goto err_free_table;
454
455 mutex_init(&table->lock);
456
457 table->sz = sz;
458
459 for (i = 0; i < sz; i++)
460 rwlock_init(&table->data_vec[i].lock);
461
462 return table;
463
464err_free_table:
465 kfree(table);
466 return NULL;
467}
468
469static void release_gid_table(struct ib_gid_table *table)
470{
471 if (table) {
472 kfree(table->data_vec);
473 kfree(table);
474 }
475}
476
477static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port,
478 struct ib_gid_table *table)
479{
480 int i;
481
482 if (!table)
483 return;
484
485 for (i = 0; i < table->sz; ++i) {
486 if (memcmp(&table->data_vec[i].gid, &zgid,
487 sizeof(table->data_vec[i].gid)))
488 del_gid(ib_dev, port, table, i,
489 table->data_vec[i].props &
490 GID_ATTR_FIND_MASK_DEFAULT);
491 }
492}
493
494void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
495 struct net_device *ndev,
496 enum ib_cache_gid_default_mode mode)
497{
498 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
499 union ib_gid gid;
500 struct ib_gid_attr gid_attr;
501 struct ib_gid_table *table;
502 int ix;
503 union ib_gid current_gid;
504 struct ib_gid_attr current_gid_attr = {};
505
506 table = ports_table[port - rdma_start_port(ib_dev)];
507
508 make_default_gid(ndev, &gid);
509 memset(&gid_attr, 0, sizeof(gid_attr));
510 gid_attr.ndev = ndev;
511
17b38fb8 512 mutex_lock(&table->lock);
03db3a2d
MB
513 ix = find_gid(table, NULL, NULL, true, GID_ATTR_FIND_MASK_DEFAULT);
514
515 /* Coudn't find default GID location */
516 WARN_ON(ix < 0);
517
03db3a2d
MB
518 if (!__ib_cache_gid_get(ib_dev, port, ix,
519 &current_gid, &current_gid_attr) &&
520 mode == IB_CACHE_GID_DEFAULT_MODE_SET &&
521 !memcmp(&gid, &current_gid, sizeof(gid)) &&
522 !memcmp(&gid_attr, &current_gid_attr, sizeof(gid_attr)))
523 goto unlock;
524
525 if ((memcmp(&current_gid, &zgid, sizeof(current_gid)) ||
526 memcmp(&current_gid_attr, &zattr,
527 sizeof(current_gid_attr))) &&
528 del_gid(ib_dev, port, table, ix, true)) {
529 pr_warn("ib_cache_gid: can't delete index %d for default gid %pI6\n",
530 ix, gid.raw);
531 goto unlock;
532 }
533
534 if (mode == IB_CACHE_GID_DEFAULT_MODE_SET)
535 if (add_gid(ib_dev, port, table, ix, &gid, &gid_attr, true))
536 pr_warn("ib_cache_gid: unable to add default gid %pI6\n",
537 gid.raw);
538
539unlock:
540 if (current_gid_attr.ndev)
541 dev_put(current_gid_attr.ndev);
542 mutex_unlock(&table->lock);
543}
544
545static int gid_table_reserve_default(struct ib_device *ib_dev, u8 port,
546 struct ib_gid_table *table)
547{
548 if (rdma_protocol_roce(ib_dev, port)) {
549 struct ib_gid_table_entry *entry = &table->data_vec[0];
550
551 entry->props |= GID_TABLE_ENTRY_DEFAULT;
552 }
553
554 return 0;
555}
556
557static int _gid_table_setup_one(struct ib_device *ib_dev)
558{
559 u8 port;
560 struct ib_gid_table **table;
561 int err = 0;
562
563 table = kcalloc(ib_dev->phys_port_cnt, sizeof(*table), GFP_KERNEL);
564
565 if (!table) {
566 pr_warn("failed to allocate ib gid cache for %s\n",
567 ib_dev->name);
568 return -ENOMEM;
569 }
570
571 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
572 u8 rdma_port = port + rdma_start_port(ib_dev);
573
574 table[port] =
575 alloc_gid_table(
576 ib_dev->port_immutable[rdma_port].gid_tbl_len);
577 if (!table[port]) {
578 err = -ENOMEM;
579 goto rollback_table_setup;
580 }
581
582 err = gid_table_reserve_default(ib_dev,
583 port + rdma_start_port(ib_dev),
584 table[port]);
585 if (err)
586 goto rollback_table_setup;
587 }
588
589 ib_dev->cache.gid_cache = table;
590 return 0;
591
592rollback_table_setup:
593 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
594 cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
595 table[port]);
596 release_gid_table(table[port]);
597 }
598
599 kfree(table);
600 return err;
601}
602
603static void gid_table_release_one(struct ib_device *ib_dev)
604{
605 struct ib_gid_table **table = ib_dev->cache.gid_cache;
606 u8 port;
607
608 if (!table)
609 return;
610
611 for (port = 0; port < ib_dev->phys_port_cnt; port++)
612 release_gid_table(table[port]);
613
614 kfree(table);
615 ib_dev->cache.gid_cache = NULL;
616}
617
618static void gid_table_cleanup_one(struct ib_device *ib_dev)
619{
620 struct ib_gid_table **table = ib_dev->cache.gid_cache;
621 u8 port;
622
623 if (!table)
624 return;
625
626 for (port = 0; port < ib_dev->phys_port_cnt; port++)
627 cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
628 table[port]);
629}
630
631static int gid_table_setup_one(struct ib_device *ib_dev)
632{
633 int err;
634
635 err = _gid_table_setup_one(ib_dev);
636
637 if (err)
638 return err;
639
640 err = roce_rescan_device(ib_dev);
641
642 if (err) {
643 gid_table_cleanup_one(ib_dev);
644 gid_table_release_one(ib_dev);
645 }
646
647 return err;
648}
649
650int ib_get_cached_gid(struct ib_device *device,
651 u8 port_num,
652 int index,
55ee3ab2
MB
653 union ib_gid *gid,
654 struct ib_gid_attr *gid_attr)
03db3a2d
MB
655{
656 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
657 return -EINVAL;
658
55ee3ab2 659 return __ib_cache_gid_get(device, port_num, index, gid, gid_attr);
03db3a2d
MB
660}
661EXPORT_SYMBOL(ib_get_cached_gid);
662
663int ib_find_cached_gid(struct ib_device *device,
664 const union ib_gid *gid,
55ee3ab2 665 struct net_device *ndev,
03db3a2d
MB
666 u8 *port_num,
667 u16 *index)
668{
55ee3ab2 669 return ib_cache_gid_find(device, gid, ndev, port_num, index);
1da177e4
LT
670}
671EXPORT_SYMBOL(ib_find_cached_gid);
672
673int ib_get_cached_pkey(struct ib_device *device,
674 u8 port_num,
675 int index,
676 u16 *pkey)
677{
678 struct ib_pkey_cache *cache;
679 unsigned long flags;
680 int ret = 0;
681
0cf18d77 682 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
1da177e4
LT
683 return -EINVAL;
684
685 read_lock_irqsave(&device->cache.lock, flags);
686
0cf18d77 687 cache = device->cache.pkey_cache[port_num - rdma_start_port(device)];
1da177e4
LT
688
689 if (index < 0 || index >= cache->table_len)
690 ret = -EINVAL;
691 else
692 *pkey = cache->table[index];
693
694 read_unlock_irqrestore(&device->cache.lock, flags);
695
696 return ret;
697}
698EXPORT_SYMBOL(ib_get_cached_pkey);
699
700int ib_find_cached_pkey(struct ib_device *device,
701 u8 port_num,
702 u16 pkey,
703 u16 *index)
704{
705 struct ib_pkey_cache *cache;
706 unsigned long flags;
707 int i;
708 int ret = -ENOENT;
ff7166c4 709 int partial_ix = -1;
1da177e4 710
0cf18d77 711 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
1da177e4
LT
712 return -EINVAL;
713
714 read_lock_irqsave(&device->cache.lock, flags);
715
0cf18d77 716 cache = device->cache.pkey_cache[port_num - rdma_start_port(device)];
1da177e4
LT
717
718 *index = -1;
719
720 for (i = 0; i < cache->table_len; ++i)
721 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
ff7166c4
JM
722 if (cache->table[i] & 0x8000) {
723 *index = i;
724 ret = 0;
725 break;
726 } else
727 partial_ix = i;
1da177e4
LT
728 }
729
ff7166c4
JM
730 if (ret && partial_ix >= 0) {
731 *index = partial_ix;
732 ret = 0;
733 }
734
1da177e4
LT
735 read_unlock_irqrestore(&device->cache.lock, flags);
736
737 return ret;
738}
739EXPORT_SYMBOL(ib_find_cached_pkey);
740
73aaa741
JM
741int ib_find_exact_cached_pkey(struct ib_device *device,
742 u8 port_num,
743 u16 pkey,
744 u16 *index)
745{
746 struct ib_pkey_cache *cache;
747 unsigned long flags;
748 int i;
749 int ret = -ENOENT;
750
0cf18d77 751 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
73aaa741
JM
752 return -EINVAL;
753
754 read_lock_irqsave(&device->cache.lock, flags);
755
0cf18d77 756 cache = device->cache.pkey_cache[port_num - rdma_start_port(device)];
73aaa741
JM
757
758 *index = -1;
759
760 for (i = 0; i < cache->table_len; ++i)
761 if (cache->table[i] == pkey) {
762 *index = i;
763 ret = 0;
764 break;
765 }
766
767 read_unlock_irqrestore(&device->cache.lock, flags);
768
769 return ret;
770}
771EXPORT_SYMBOL(ib_find_exact_cached_pkey);
772
6fb9cdbf
JM
773int ib_get_cached_lmc(struct ib_device *device,
774 u8 port_num,
775 u8 *lmc)
776{
777 unsigned long flags;
778 int ret = 0;
779
0cf18d77 780 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
6fb9cdbf
JM
781 return -EINVAL;
782
783 read_lock_irqsave(&device->cache.lock, flags);
0cf18d77 784 *lmc = device->cache.lmc_cache[port_num - rdma_start_port(device)];
6fb9cdbf
JM
785 read_unlock_irqrestore(&device->cache.lock, flags);
786
787 return ret;
788}
789EXPORT_SYMBOL(ib_get_cached_lmc);
790
1da177e4
LT
791static void ib_cache_update(struct ib_device *device,
792 u8 port)
793{
794 struct ib_port_attr *tprops = NULL;
795 struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache;
03db3a2d
MB
796 struct ib_gid_cache {
797 int table_len;
798 union ib_gid table[0];
799 } *gid_cache = NULL;
1da177e4
LT
800 int i;
801 int ret;
03db3a2d
MB
802 struct ib_gid_table *table;
803 struct ib_gid_table **ports_table = device->cache.gid_cache;
804 bool use_roce_gid_table =
805 rdma_cap_roce_gid_table(device, port);
806
807 if (port < rdma_start_port(device) || port > rdma_end_port(device))
808 return;
809
810 table = ports_table[port - rdma_start_port(device)];
1da177e4
LT
811
812 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
813 if (!tprops)
814 return;
815
816 ret = ib_query_port(device, port, tprops);
817 if (ret) {
818 printk(KERN_WARNING "ib_query_port failed (%d) for %s\n",
819 ret, device->name);
820 goto err;
821 }
822
823 pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
824 sizeof *pkey_cache->table, GFP_KERNEL);
825 if (!pkey_cache)
826 goto err;
827
828 pkey_cache->table_len = tprops->pkey_tbl_len;
829
03db3a2d
MB
830 if (!use_roce_gid_table) {
831 gid_cache = kmalloc(sizeof(*gid_cache) + tprops->gid_tbl_len *
832 sizeof(*gid_cache->table), GFP_KERNEL);
833 if (!gid_cache)
834 goto err;
1da177e4 835
03db3a2d
MB
836 gid_cache->table_len = tprops->gid_tbl_len;
837 }
1da177e4
LT
838
839 for (i = 0; i < pkey_cache->table_len; ++i) {
840 ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
841 if (ret) {
842 printk(KERN_WARNING "ib_query_pkey failed (%d) for %s (index %d)\n",
843 ret, device->name, i);
844 goto err;
845 }
846 }
847
03db3a2d
MB
848 if (!use_roce_gid_table) {
849 for (i = 0; i < gid_cache->table_len; ++i) {
850 ret = ib_query_gid(device, port, i,
55ee3ab2 851 gid_cache->table + i, NULL);
03db3a2d
MB
852 if (ret) {
853 printk(KERN_WARNING "ib_query_gid failed (%d) for %s (index %d)\n",
854 ret, device->name, i);
855 goto err;
856 }
1da177e4
LT
857 }
858 }
859
860 write_lock_irq(&device->cache.lock);
861
0cf18d77 862 old_pkey_cache = device->cache.pkey_cache[port - rdma_start_port(device)];
1da177e4 863
0cf18d77 864 device->cache.pkey_cache[port - rdma_start_port(device)] = pkey_cache;
03db3a2d
MB
865 if (!use_roce_gid_table) {
866 for (i = 0; i < gid_cache->table_len; i++) {
867 modify_gid(device, port, table, i, gid_cache->table + i,
868 &zattr, false);
869 }
870 }
1da177e4 871
0cf18d77 872 device->cache.lmc_cache[port - rdma_start_port(device)] = tprops->lmc;
6fb9cdbf 873
1da177e4
LT
874 write_unlock_irq(&device->cache.lock);
875
03db3a2d 876 kfree(gid_cache);
1da177e4 877 kfree(old_pkey_cache);
1da177e4
LT
878 kfree(tprops);
879 return;
880
881err:
882 kfree(pkey_cache);
883 kfree(gid_cache);
884 kfree(tprops);
885}
886
c4028958 887static void ib_cache_task(struct work_struct *_work)
1da177e4 888{
c4028958
DH
889 struct ib_update_work *work =
890 container_of(_work, struct ib_update_work, work);
1da177e4
LT
891
892 ib_cache_update(work->device, work->port_num);
893 kfree(work);
894}
895
896static void ib_cache_event(struct ib_event_handler *handler,
897 struct ib_event *event)
898{
899 struct ib_update_work *work;
900
901 if (event->event == IB_EVENT_PORT_ERR ||
902 event->event == IB_EVENT_PORT_ACTIVE ||
903 event->event == IB_EVENT_LID_CHANGE ||
904 event->event == IB_EVENT_PKEY_CHANGE ||
acaea9ee 905 event->event == IB_EVENT_SM_CHANGE ||
761d90ed
OG
906 event->event == IB_EVENT_CLIENT_REREGISTER ||
907 event->event == IB_EVENT_GID_CHANGE) {
1da177e4
LT
908 work = kmalloc(sizeof *work, GFP_ATOMIC);
909 if (work) {
c4028958 910 INIT_WORK(&work->work, ib_cache_task);
1da177e4
LT
911 work->device = event->device;
912 work->port_num = event->element.port_num;
f0626710 913 queue_work(ib_wq, &work->work);
1da177e4
LT
914 }
915 }
916}
917
03db3a2d 918int ib_cache_setup_one(struct ib_device *device)
1da177e4
LT
919{
920 int p;
03db3a2d 921 int err;
1da177e4
LT
922
923 rwlock_init(&device->cache.lock);
924
925 device->cache.pkey_cache =
55aeed06 926 kzalloc(sizeof *device->cache.pkey_cache *
0cf18d77 927 (rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL);
6fb9cdbf 928 device->cache.lmc_cache = kmalloc(sizeof *device->cache.lmc_cache *
0cf18d77
IW
929 (rdma_end_port(device) -
930 rdma_start_port(device) + 1),
6fb9cdbf 931 GFP_KERNEL);
03db3a2d 932 if (!device->cache.pkey_cache ||
6fb9cdbf 933 !device->cache.lmc_cache) {
1da177e4
LT
934 printk(KERN_WARNING "Couldn't allocate cache "
935 "for %s\n", device->name);
03db3a2d 936 return -ENOMEM;
1da177e4
LT
937 }
938
03db3a2d
MB
939 err = gid_table_setup_one(device);
940 if (err)
941 /* Allocated memory will be cleaned in the release function */
942 return err;
943
55aeed06 944 for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
0cf18d77 945 ib_cache_update(device, p + rdma_start_port(device));
1da177e4
LT
946
947 INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
948 device, ib_cache_event);
03db3a2d
MB
949 err = ib_register_event_handler(&device->cache.event_handler);
950 if (err)
951 goto err;
1da177e4 952
03db3a2d 953 return 0;
1da177e4
LT
954
955err:
03db3a2d
MB
956 gid_table_cleanup_one(device);
957 return err;
1da177e4
LT
958}
959
03db3a2d 960void ib_cache_release_one(struct ib_device *device)
1da177e4
LT
961{
962 int p;
963
03db3a2d
MB
964 /*
965 * The release function frees all the cache elements.
966 * This function should be called as part of freeing
967 * all the device's resources when the cache could no
968 * longer be accessed.
969 */
970 if (device->cache.pkey_cache)
971 for (p = 0;
972 p <= rdma_end_port(device) - rdma_start_port(device); ++p)
973 kfree(device->cache.pkey_cache[p]);
974
975 gid_table_release_one(device);
1da177e4 976 kfree(device->cache.pkey_cache);
6fb9cdbf 977 kfree(device->cache.lmc_cache);
1da177e4
LT
978}
979
03db3a2d
MB
980void ib_cache_cleanup_one(struct ib_device *device)
981{
982 /* The cleanup function unregisters the event handler,
983 * waits for all in-progress workqueue elements and cleans
984 * up the GID cache. This function should be called after
985 * the device was removed from the devices list and all
986 * clients were removed, so the cache exists but is
987 * non-functional and shouldn't be updated anymore.
988 */
989 ib_unregister_event_handler(&device->cache.event_handler);
990 flush_workqueue(ib_wq);
991 gid_table_cleanup_one(device);
992}
1da177e4 993
03db3a2d 994void __init ib_cache_setup(void)
1da177e4 995{
03db3a2d 996 roce_gid_mgmt_init();
1da177e4
LT
997}
998
999void __exit ib_cache_cleanup(void)
1000{
03db3a2d 1001 roce_gid_mgmt_cleanup();
1da177e4 1002}