blob: 4a2968b1cf56f5f0ccfa90aa0424f26c365c2bba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/module.h>
37#include <linux/errno.h>
38#include <linux/slab.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040039#include <linux/workqueue.h>
Matan Barak03db3a22015-07-30 18:33:26 +030040#include <linux/netdevice.h>
41#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Roland Dreiera4d61e82005-08-25 13:40:04 -070043#include <rdma/ib_cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include "core_priv.h"
46
47struct ib_pkey_cache {
48 int table_len;
49 u16 table[0];
50};
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052struct ib_update_work {
53 struct work_struct work;
54 struct ib_device *device;
55 u8 port_num;
56};
57
Moni Shouae26be1b2015-07-30 18:33:29 +030058union ib_gid zgid;
59EXPORT_SYMBOL(zgid);
Matan Barak03db3a22015-07-30 18:33:26 +030060
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,
Matan Barakb39ffa12015-12-23 14:56:47 +020067 GID_ATTR_FIND_MASK_GID_TYPE = 1UL << 3,
Matan Barak03db3a22015-07-30 18:33:26 +030068};
69
70enum gid_table_entry_props {
71 GID_TABLE_ENTRY_INVALID = 1UL << 0,
72 GID_TABLE_ENTRY_DEFAULT = 1UL << 1,
73};
74
75enum gid_table_write_action {
76 GID_TABLE_WRITE_ACTION_ADD,
77 GID_TABLE_WRITE_ACTION_DEL,
78 /* MODIFY only updates the GID table. Currently only used by
79 * ib_cache_update.
80 */
81 GID_TABLE_WRITE_ACTION_MODIFY
82};
83
84struct ib_gid_table_entry {
Matan Barak03db3a22015-07-30 18:33:26 +030085 unsigned long props;
86 union ib_gid gid;
87 struct ib_gid_attr attr;
88 void *context;
89};
90
91struct ib_gid_table {
92 int sz;
93 /* In RoCE, adding a GID to the table requires:
94 * (a) Find if this GID is already exists.
95 * (b) Find a free space.
96 * (c) Write the new GID
97 *
98 * Delete requires different set of operations:
99 * (a) Find the GID
100 * (b) Delete it.
101 *
102 * Add/delete should be carried out atomically.
103 * This is done by locking this mutex from multiple
104 * writers. We don't need this lock for IB, as the MAD
105 * layer replaces all entries. All data_vec entries
106 * are locked by this lock.
107 **/
108 struct mutex lock;
Matan Barak9c584f02015-10-28 16:52:40 +0200109 /* This lock protects the table entries from being
110 * read and written simultaneously.
111 */
112 rwlock_t rwlock;
Matan Barak03db3a22015-07-30 18:33:26 +0300113 struct ib_gid_table_entry *data_vec;
114};
115
Matan Barakf3906bd2015-10-28 16:52:39 +0200116static void dispatch_gid_change_event(struct ib_device *ib_dev, u8 port)
117{
118 if (rdma_cap_roce_gid_table(ib_dev, port)) {
119 struct ib_event event;
120
121 event.device = ib_dev;
122 event.element.port_num = port;
123 event.event = IB_EVENT_GID_CHANGE;
124
125 ib_dispatch_event(&event);
126 }
127}
128
Matan Barakb39ffa12015-12-23 14:56:47 +0200129static const char * const gid_type_str[] = {
130 [IB_GID_TYPE_IB] = "IB/RoCE v1",
Matan Barak7766a992015-12-23 14:56:50 +0200131 [IB_GID_TYPE_ROCE_UDP_ENCAP] = "RoCE v2",
Matan Barakb39ffa12015-12-23 14:56:47 +0200132};
133
134const char *ib_cache_gid_type_str(enum ib_gid_type gid_type)
135{
136 if (gid_type < ARRAY_SIZE(gid_type_str) && gid_type_str[gid_type])
137 return gid_type_str[gid_type];
138
139 return "Invalid GID type";
140}
141EXPORT_SYMBOL(ib_cache_gid_type_str);
142
Matan Barak9c584f02015-10-28 16:52:40 +0200143/* This function expects that rwlock will be write locked in all
144 * scenarios and that lock will be locked in sleep-able (RoCE)
145 * scenarios.
146 */
Matan Barak03db3a22015-07-30 18:33:26 +0300147static int write_gid(struct ib_device *ib_dev, u8 port,
148 struct ib_gid_table *table, int ix,
149 const union ib_gid *gid,
150 const struct ib_gid_attr *attr,
151 enum gid_table_write_action action,
152 bool default_gid)
153{
154 int ret = 0;
155 struct net_device *old_net_dev;
Matan Barak03db3a22015-07-30 18:33:26 +0300156
157 /* in rdma_cap_roce_gid_table, this funciton should be protected by a
158 * sleep-able lock.
159 */
Matan Barak03db3a22015-07-30 18:33:26 +0300160
161 if (rdma_cap_roce_gid_table(ib_dev, port)) {
162 table->data_vec[ix].props |= GID_TABLE_ENTRY_INVALID;
Matan Barak9c584f02015-10-28 16:52:40 +0200163 write_unlock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300164 /* GID_TABLE_WRITE_ACTION_MODIFY currently isn't supported by
165 * RoCE providers and thus only updates the cache.
166 */
167 if (action == GID_TABLE_WRITE_ACTION_ADD)
168 ret = ib_dev->add_gid(ib_dev, port, ix, gid, attr,
169 &table->data_vec[ix].context);
170 else if (action == GID_TABLE_WRITE_ACTION_DEL)
171 ret = ib_dev->del_gid(ib_dev, port, ix,
172 &table->data_vec[ix].context);
Matan Barak9c584f02015-10-28 16:52:40 +0200173 write_lock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300174 }
175
176 old_net_dev = table->data_vec[ix].attr.ndev;
177 if (old_net_dev && old_net_dev != attr->ndev)
178 dev_put(old_net_dev);
179 /* if modify_gid failed, just delete the old gid */
180 if (ret || action == GID_TABLE_WRITE_ACTION_DEL) {
181 gid = &zgid;
182 attr = &zattr;
183 table->data_vec[ix].context = NULL;
184 }
185 if (default_gid)
186 table->data_vec[ix].props |= GID_TABLE_ENTRY_DEFAULT;
187 memcpy(&table->data_vec[ix].gid, gid, sizeof(*gid));
188 memcpy(&table->data_vec[ix].attr, attr, sizeof(*attr));
189 if (table->data_vec[ix].attr.ndev &&
190 table->data_vec[ix].attr.ndev != old_net_dev)
191 dev_hold(table->data_vec[ix].attr.ndev);
192
193 table->data_vec[ix].props &= ~GID_TABLE_ENTRY_INVALID;
194
Matan Barak03db3a22015-07-30 18:33:26 +0300195 return ret;
196}
197
198static int add_gid(struct ib_device *ib_dev, u8 port,
199 struct ib_gid_table *table, int ix,
200 const union ib_gid *gid,
201 const struct ib_gid_attr *attr,
202 bool default_gid) {
203 return write_gid(ib_dev, port, table, ix, gid, attr,
204 GID_TABLE_WRITE_ACTION_ADD, default_gid);
205}
206
207static int modify_gid(struct ib_device *ib_dev, u8 port,
208 struct ib_gid_table *table, int ix,
209 const union ib_gid *gid,
210 const struct ib_gid_attr *attr,
211 bool default_gid) {
212 return write_gid(ib_dev, port, table, ix, gid, attr,
213 GID_TABLE_WRITE_ACTION_MODIFY, default_gid);
214}
215
216static int del_gid(struct ib_device *ib_dev, u8 port,
217 struct ib_gid_table *table, int ix,
218 bool default_gid) {
219 return write_gid(ib_dev, port, table, ix, &zgid, &zattr,
220 GID_TABLE_WRITE_ACTION_DEL, default_gid);
221}
222
Matan Barak9c584f02015-10-28 16:52:40 +0200223/* rwlock should be read locked */
Matan Barak03db3a22015-07-30 18:33:26 +0300224static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,
225 const struct ib_gid_attr *val, bool default_gid,
Matan Barakcee3c4d2015-10-28 16:52:41 +0200226 unsigned long mask, int *pempty)
Matan Barak03db3a22015-07-30 18:33:26 +0300227{
Matan Barakcee3c4d2015-10-28 16:52:41 +0200228 int i = 0;
229 int found = -1;
230 int empty = pempty ? -1 : 0;
Matan Barak03db3a22015-07-30 18:33:26 +0300231
Matan Barakcee3c4d2015-10-28 16:52:41 +0200232 while (i < table->sz && (found < 0 || empty < 0)) {
233 struct ib_gid_table_entry *data = &table->data_vec[i];
234 struct ib_gid_attr *attr = &data->attr;
235 int curr_index = i;
Matan Barak03db3a22015-07-30 18:33:26 +0300236
Matan Barakcee3c4d2015-10-28 16:52:41 +0200237 i++;
Matan Barak03db3a22015-07-30 18:33:26 +0300238
Matan Barakcee3c4d2015-10-28 16:52:41 +0200239 if (data->props & GID_TABLE_ENTRY_INVALID)
240 continue;
241
242 if (empty < 0)
243 if (!memcmp(&data->gid, &zgid, sizeof(*gid)) &&
244 !memcmp(attr, &zattr, sizeof(*attr)) &&
245 !data->props)
246 empty = curr_index;
247
248 if (found >= 0)
Matan Barak9c584f02015-10-28 16:52:40 +0200249 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300250
Matan Barakb39ffa12015-12-23 14:56:47 +0200251 if (mask & GID_ATTR_FIND_MASK_GID_TYPE &&
252 attr->gid_type != val->gid_type)
253 continue;
254
Matan Barak03db3a22015-07-30 18:33:26 +0300255 if (mask & GID_ATTR_FIND_MASK_GID &&
Matan Barakcee3c4d2015-10-28 16:52:41 +0200256 memcmp(gid, &data->gid, sizeof(*gid)))
Matan Barak9c584f02015-10-28 16:52:40 +0200257 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300258
259 if (mask & GID_ATTR_FIND_MASK_NETDEV &&
260 attr->ndev != val->ndev)
Matan Barak9c584f02015-10-28 16:52:40 +0200261 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300262
263 if (mask & GID_ATTR_FIND_MASK_DEFAULT &&
Matan Barakcee3c4d2015-10-28 16:52:41 +0200264 !!(data->props & GID_TABLE_ENTRY_DEFAULT) !=
Matan Barak03db3a22015-07-30 18:33:26 +0300265 default_gid)
Matan Barak9c584f02015-10-28 16:52:40 +0200266 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300267
Matan Barakcee3c4d2015-10-28 16:52:41 +0200268 found = curr_index;
Matan Barak03db3a22015-07-30 18:33:26 +0300269 }
270
Matan Barakcee3c4d2015-10-28 16:52:41 +0200271 if (pempty)
272 *pempty = empty;
273
274 return found;
Matan Barak03db3a22015-07-30 18:33:26 +0300275}
276
277static void make_default_gid(struct net_device *dev, union ib_gid *gid)
278{
279 gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
280 addrconf_ifid_eui48(&gid->raw[8], dev);
281}
282
283int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
284 union ib_gid *gid, struct ib_gid_attr *attr)
285{
286 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
287 struct ib_gid_table *table;
288 int ix;
289 int ret = 0;
290 struct net_device *idev;
Matan Barakcee3c4d2015-10-28 16:52:41 +0200291 int empty;
Matan Barak03db3a22015-07-30 18:33:26 +0300292
293 table = ports_table[port - rdma_start_port(ib_dev)];
294
295 if (!memcmp(gid, &zgid, sizeof(*gid)))
296 return -EINVAL;
297
298 if (ib_dev->get_netdev) {
299 idev = ib_dev->get_netdev(ib_dev, port);
300 if (idev && attr->ndev != idev) {
301 union ib_gid default_gid;
302
303 /* Adding default GIDs in not permitted */
304 make_default_gid(idev, &default_gid);
305 if (!memcmp(gid, &default_gid, sizeof(*gid))) {
306 dev_put(idev);
307 return -EPERM;
308 }
309 }
310 if (idev)
311 dev_put(idev);
312 }
313
314 mutex_lock(&table->lock);
Matan Barak9c584f02015-10-28 16:52:40 +0200315 write_lock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300316
317 ix = find_gid(table, gid, attr, false, GID_ATTR_FIND_MASK_GID |
Matan Barakb39ffa12015-12-23 14:56:47 +0200318 GID_ATTR_FIND_MASK_GID_TYPE |
Matan Barakcee3c4d2015-10-28 16:52:41 +0200319 GID_ATTR_FIND_MASK_NETDEV, &empty);
Matan Barak03db3a22015-07-30 18:33:26 +0300320 if (ix >= 0)
321 goto out_unlock;
322
Matan Barakcee3c4d2015-10-28 16:52:41 +0200323 if (empty < 0) {
Matan Barak03db3a22015-07-30 18:33:26 +0300324 ret = -ENOSPC;
325 goto out_unlock;
326 }
327
Matan Barakcee3c4d2015-10-28 16:52:41 +0200328 ret = add_gid(ib_dev, port, table, empty, gid, attr, false);
Matan Barak9c584f02015-10-28 16:52:40 +0200329 if (!ret)
330 dispatch_gid_change_event(ib_dev, port);
Matan Barak03db3a22015-07-30 18:33:26 +0300331
332out_unlock:
Matan Barak9c584f02015-10-28 16:52:40 +0200333 write_unlock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300334 mutex_unlock(&table->lock);
335 return ret;
336}
337
338int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
339 union ib_gid *gid, struct ib_gid_attr *attr)
340{
341 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
342 struct ib_gid_table *table;
343 int ix;
344
345 table = ports_table[port - rdma_start_port(ib_dev)];
346
347 mutex_lock(&table->lock);
Matan Barak9c584f02015-10-28 16:52:40 +0200348 write_lock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300349
350 ix = find_gid(table, gid, attr, false,
351 GID_ATTR_FIND_MASK_GID |
Matan Barakb39ffa12015-12-23 14:56:47 +0200352 GID_ATTR_FIND_MASK_GID_TYPE |
Matan Barak03db3a22015-07-30 18:33:26 +0300353 GID_ATTR_FIND_MASK_NETDEV |
Matan Barakcee3c4d2015-10-28 16:52:41 +0200354 GID_ATTR_FIND_MASK_DEFAULT,
355 NULL);
Matan Barak03db3a22015-07-30 18:33:26 +0300356 if (ix < 0)
357 goto out_unlock;
358
Matan Barak9c584f02015-10-28 16:52:40 +0200359 if (!del_gid(ib_dev, port, table, ix, false))
360 dispatch_gid_change_event(ib_dev, port);
Matan Barak03db3a22015-07-30 18:33:26 +0300361
362out_unlock:
Matan Barak9c584f02015-10-28 16:52:40 +0200363 write_unlock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300364 mutex_unlock(&table->lock);
365 return 0;
366}
367
368int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
369 struct net_device *ndev)
370{
371 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
372 struct ib_gid_table *table;
373 int ix;
Matan Barak9c584f02015-10-28 16:52:40 +0200374 bool deleted = false;
Matan Barak03db3a22015-07-30 18:33:26 +0300375
376 table = ports_table[port - rdma_start_port(ib_dev)];
377
378 mutex_lock(&table->lock);
Matan Barak9c584f02015-10-28 16:52:40 +0200379 write_lock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300380
381 for (ix = 0; ix < table->sz; ix++)
382 if (table->data_vec[ix].attr.ndev == ndev)
Matan Barak9c584f02015-10-28 16:52:40 +0200383 if (!del_gid(ib_dev, port, table, ix, false))
384 deleted = true;
Matan Barak03db3a22015-07-30 18:33:26 +0300385
Matan Barak9c584f02015-10-28 16:52:40 +0200386 write_unlock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300387 mutex_unlock(&table->lock);
Matan Barak9c584f02015-10-28 16:52:40 +0200388
389 if (deleted)
390 dispatch_gid_change_event(ib_dev, port);
391
Matan Barak03db3a22015-07-30 18:33:26 +0300392 return 0;
393}
394
395static int __ib_cache_gid_get(struct ib_device *ib_dev, u8 port, int index,
396 union ib_gid *gid, struct ib_gid_attr *attr)
397{
398 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
399 struct ib_gid_table *table;
Matan Barak03db3a22015-07-30 18:33:26 +0300400
401 table = ports_table[port - rdma_start_port(ib_dev)];
402
403 if (index < 0 || index >= table->sz)
404 return -EINVAL;
405
Matan Barak9c584f02015-10-28 16:52:40 +0200406 if (table->data_vec[index].props & GID_TABLE_ENTRY_INVALID)
Matan Barak03db3a22015-07-30 18:33:26 +0300407 return -EAGAIN;
Matan Barak03db3a22015-07-30 18:33:26 +0300408
409 memcpy(gid, &table->data_vec[index].gid, sizeof(*gid));
410 if (attr) {
411 memcpy(attr, &table->data_vec[index].attr, sizeof(*attr));
412 if (attr->ndev)
413 dev_hold(attr->ndev);
414 }
415
Matan Barak03db3a22015-07-30 18:33:26 +0300416 return 0;
417}
418
419static int _ib_cache_gid_table_find(struct ib_device *ib_dev,
420 const union ib_gid *gid,
421 const struct ib_gid_attr *val,
422 unsigned long mask,
423 u8 *port, u16 *index)
424{
425 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
426 struct ib_gid_table *table;
427 u8 p;
428 int local_index;
Matan Barak9c584f02015-10-28 16:52:40 +0200429 unsigned long flags;
Matan Barak03db3a22015-07-30 18:33:26 +0300430
431 for (p = 0; p < ib_dev->phys_port_cnt; p++) {
432 table = ports_table[p];
Matan Barak9c584f02015-10-28 16:52:40 +0200433 read_lock_irqsave(&table->rwlock, flags);
Matan Barakcee3c4d2015-10-28 16:52:41 +0200434 local_index = find_gid(table, gid, val, false, mask, NULL);
Matan Barak03db3a22015-07-30 18:33:26 +0300435 if (local_index >= 0) {
436 if (index)
437 *index = local_index;
438 if (port)
439 *port = p + rdma_start_port(ib_dev);
Matan Barak9c584f02015-10-28 16:52:40 +0200440 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak03db3a22015-07-30 18:33:26 +0300441 return 0;
442 }
Matan Barak9c584f02015-10-28 16:52:40 +0200443 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak03db3a22015-07-30 18:33:26 +0300444 }
445
446 return -ENOENT;
447}
448
449static int ib_cache_gid_find(struct ib_device *ib_dev,
450 const union ib_gid *gid,
Matan Barakb39ffa12015-12-23 14:56:47 +0200451 enum ib_gid_type gid_type,
Matan Barak03db3a22015-07-30 18:33:26 +0300452 struct net_device *ndev, u8 *port,
453 u16 *index)
454{
Matan Barakb39ffa12015-12-23 14:56:47 +0200455 unsigned long mask = GID_ATTR_FIND_MASK_GID |
456 GID_ATTR_FIND_MASK_GID_TYPE;
457 struct ib_gid_attr gid_attr_val = {.ndev = ndev, .gid_type = gid_type};
Matan Barak03db3a22015-07-30 18:33:26 +0300458
459 if (ndev)
460 mask |= GID_ATTR_FIND_MASK_NETDEV;
461
462 return _ib_cache_gid_table_find(ib_dev, gid, &gid_attr_val,
463 mask, port, index);
464}
465
Matan Barakd300ec52015-10-15 18:38:46 +0300466int ib_find_cached_gid_by_port(struct ib_device *ib_dev,
467 const union ib_gid *gid,
Matan Barakb39ffa12015-12-23 14:56:47 +0200468 enum ib_gid_type gid_type,
Matan Barakd300ec52015-10-15 18:38:46 +0300469 u8 port, struct net_device *ndev,
470 u16 *index)
Matan Barak03db3a22015-07-30 18:33:26 +0300471{
472 int local_index;
473 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
474 struct ib_gid_table *table;
Matan Barakb39ffa12015-12-23 14:56:47 +0200475 unsigned long mask = GID_ATTR_FIND_MASK_GID |
476 GID_ATTR_FIND_MASK_GID_TYPE;
477 struct ib_gid_attr val = {.ndev = ndev, .gid_type = gid_type};
Matan Barak9c584f02015-10-28 16:52:40 +0200478 unsigned long flags;
Matan Barak03db3a22015-07-30 18:33:26 +0300479
480 if (port < rdma_start_port(ib_dev) ||
481 port > rdma_end_port(ib_dev))
482 return -ENOENT;
483
484 table = ports_table[port - rdma_start_port(ib_dev)];
485
486 if (ndev)
487 mask |= GID_ATTR_FIND_MASK_NETDEV;
488
Matan Barak9c584f02015-10-28 16:52:40 +0200489 read_lock_irqsave(&table->rwlock, flags);
Matan Barakcee3c4d2015-10-28 16:52:41 +0200490 local_index = find_gid(table, gid, &val, false, mask, NULL);
Matan Barak03db3a22015-07-30 18:33:26 +0300491 if (local_index >= 0) {
492 if (index)
493 *index = local_index;
Matan Barak9c584f02015-10-28 16:52:40 +0200494 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak03db3a22015-07-30 18:33:26 +0300495 return 0;
496 }
497
Matan Barak9c584f02015-10-28 16:52:40 +0200498 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak03db3a22015-07-30 18:33:26 +0300499 return -ENOENT;
500}
Matan Barakd300ec52015-10-15 18:38:46 +0300501EXPORT_SYMBOL(ib_find_cached_gid_by_port);
Matan Barak03db3a22015-07-30 18:33:26 +0300502
Matan Barak99b27e32015-10-15 18:38:50 +0300503/**
504 * ib_find_gid_by_filter - Returns the GID table index where a specified
505 * GID value occurs
506 * @device: The device to query.
507 * @gid: The GID value to search for.
508 * @port_num: The port number of the device where the GID value could be
509 * searched.
510 * @filter: The filter function is executed on any matching GID in the table.
511 * If the filter function returns true, the corresponding index is returned,
512 * otherwise, we continue searching the GID table. It's guaranteed that
513 * while filter is executed, ndev field is valid and the structure won't
514 * change. filter is executed in an atomic context. filter must not be NULL.
515 * @index: The index into the cached GID table where the GID was found. This
516 * parameter may be NULL.
517 *
518 * ib_cache_gid_find_by_filter() searches for the specified GID value
519 * of which the filter function returns true in the port's GID table.
520 * This function is only supported on RoCE ports.
521 *
522 */
523static int ib_cache_gid_find_by_filter(struct ib_device *ib_dev,
524 const union ib_gid *gid,
525 u8 port,
526 bool (*filter)(const union ib_gid *,
527 const struct ib_gid_attr *,
528 void *),
529 void *context,
530 u16 *index)
531{
532 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
533 struct ib_gid_table *table;
534 unsigned int i;
Matan Barak9c584f02015-10-28 16:52:40 +0200535 unsigned long flags;
Matan Barak99b27e32015-10-15 18:38:50 +0300536 bool found = false;
537
538 if (!ports_table)
539 return -EOPNOTSUPP;
540
541 if (port < rdma_start_port(ib_dev) ||
542 port > rdma_end_port(ib_dev) ||
543 !rdma_protocol_roce(ib_dev, port))
544 return -EPROTONOSUPPORT;
545
546 table = ports_table[port - rdma_start_port(ib_dev)];
547
Matan Barak9c584f02015-10-28 16:52:40 +0200548 read_lock_irqsave(&table->rwlock, flags);
Matan Barak99b27e32015-10-15 18:38:50 +0300549 for (i = 0; i < table->sz; i++) {
550 struct ib_gid_attr attr;
Matan Barak99b27e32015-10-15 18:38:50 +0300551
Matan Barak99b27e32015-10-15 18:38:50 +0300552 if (table->data_vec[i].props & GID_TABLE_ENTRY_INVALID)
553 goto next;
554
555 if (memcmp(gid, &table->data_vec[i].gid, sizeof(*gid)))
556 goto next;
557
558 memcpy(&attr, &table->data_vec[i].attr, sizeof(attr));
559
560 if (filter(gid, &attr, context))
561 found = true;
562
563next:
Matan Barak99b27e32015-10-15 18:38:50 +0300564 if (found)
565 break;
566 }
Matan Barak9c584f02015-10-28 16:52:40 +0200567 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak99b27e32015-10-15 18:38:50 +0300568
569 if (!found)
570 return -ENOENT;
571
572 if (index)
573 *index = i;
574 return 0;
575}
576
Matan Barak03db3a22015-07-30 18:33:26 +0300577static struct ib_gid_table *alloc_gid_table(int sz)
578{
Matan Barak03db3a22015-07-30 18:33:26 +0300579 struct ib_gid_table *table =
580 kzalloc(sizeof(struct ib_gid_table), GFP_KERNEL);
Matan Barak9c584f02015-10-28 16:52:40 +0200581
Matan Barak03db3a22015-07-30 18:33:26 +0300582 if (!table)
583 return NULL;
584
585 table->data_vec = kcalloc(sz, sizeof(*table->data_vec), GFP_KERNEL);
586 if (!table->data_vec)
587 goto err_free_table;
588
589 mutex_init(&table->lock);
590
591 table->sz = sz;
Matan Barak9c584f02015-10-28 16:52:40 +0200592 rwlock_init(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300593
594 return table;
595
596err_free_table:
597 kfree(table);
598 return NULL;
599}
600
601static void release_gid_table(struct ib_gid_table *table)
602{
603 if (table) {
604 kfree(table->data_vec);
605 kfree(table);
606 }
607}
608
609static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port,
610 struct ib_gid_table *table)
611{
612 int i;
Matan Barak9c584f02015-10-28 16:52:40 +0200613 bool deleted = false;
Matan Barak03db3a22015-07-30 18:33:26 +0300614
615 if (!table)
616 return;
617
Matan Barak9c584f02015-10-28 16:52:40 +0200618 write_lock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300619 for (i = 0; i < table->sz; ++i) {
620 if (memcmp(&table->data_vec[i].gid, &zgid,
621 sizeof(table->data_vec[i].gid)))
Matan Barak9c584f02015-10-28 16:52:40 +0200622 if (!del_gid(ib_dev, port, table, i,
623 table->data_vec[i].props &
624 GID_ATTR_FIND_MASK_DEFAULT))
625 deleted = true;
Matan Barak03db3a22015-07-30 18:33:26 +0300626 }
Matan Barak9c584f02015-10-28 16:52:40 +0200627 write_unlock_irq(&table->rwlock);
628
629 if (deleted)
630 dispatch_gid_change_event(ib_dev, port);
Matan Barak03db3a22015-07-30 18:33:26 +0300631}
632
633void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
634 struct net_device *ndev,
Matan Barakb39ffa12015-12-23 14:56:47 +0200635 unsigned long gid_type_mask,
Matan Barak03db3a22015-07-30 18:33:26 +0300636 enum ib_cache_gid_default_mode mode)
637{
638 struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
639 union ib_gid gid;
640 struct ib_gid_attr gid_attr;
Matan Barakb39ffa12015-12-23 14:56:47 +0200641 struct ib_gid_attr zattr_type = zattr;
Matan Barak03db3a22015-07-30 18:33:26 +0300642 struct ib_gid_table *table;
Matan Barakb39ffa12015-12-23 14:56:47 +0200643 unsigned int gid_type;
Matan Barak03db3a22015-07-30 18:33:26 +0300644
645 table = ports_table[port - rdma_start_port(ib_dev)];
646
647 make_default_gid(ndev, &gid);
648 memset(&gid_attr, 0, sizeof(gid_attr));
649 gid_attr.ndev = ndev;
650
Matan Barakb39ffa12015-12-23 14:56:47 +0200651 for (gid_type = 0; gid_type < IB_GID_TYPE_SIZE; ++gid_type) {
652 int ix;
653 union ib_gid current_gid;
654 struct ib_gid_attr current_gid_attr = {};
Matan Barak03db3a22015-07-30 18:33:26 +0300655
Matan Barakb39ffa12015-12-23 14:56:47 +0200656 if (1UL << gid_type & ~gid_type_mask)
657 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300658
Matan Barakb39ffa12015-12-23 14:56:47 +0200659 gid_attr.gid_type = gid_type;
Matan Barak03db3a22015-07-30 18:33:26 +0300660
Matan Barakb39ffa12015-12-23 14:56:47 +0200661 mutex_lock(&table->lock);
662 write_lock_irq(&table->rwlock);
663 ix = find_gid(table, NULL, &gid_attr, true,
664 GID_ATTR_FIND_MASK_GID_TYPE |
665 GID_ATTR_FIND_MASK_DEFAULT,
666 NULL);
667
668 /* Coudn't find default GID location */
669 WARN_ON(ix < 0);
670
671 zattr_type.gid_type = gid_type;
672
673 if (!__ib_cache_gid_get(ib_dev, port, ix,
674 &current_gid, &current_gid_attr) &&
675 mode == IB_CACHE_GID_DEFAULT_MODE_SET &&
676 !memcmp(&gid, &current_gid, sizeof(gid)) &&
677 !memcmp(&gid_attr, &current_gid_attr, sizeof(gid_attr)))
678 goto release;
679
680 if (memcmp(&current_gid, &zgid, sizeof(current_gid)) ||
681 memcmp(&current_gid_attr, &zattr_type,
682 sizeof(current_gid_attr))) {
683 if (del_gid(ib_dev, port, table, ix, true)) {
684 pr_warn("ib_cache_gid: can't delete index %d for default gid %pI6\n",
685 ix, gid.raw);
686 goto release;
687 } else {
688 dispatch_gid_change_event(ib_dev, port);
689 }
Matan Barak9c584f02015-10-28 16:52:40 +0200690 }
Matan Barak03db3a22015-07-30 18:33:26 +0300691
Matan Barakb39ffa12015-12-23 14:56:47 +0200692 if (mode == IB_CACHE_GID_DEFAULT_MODE_SET) {
693 if (add_gid(ib_dev, port, table, ix, &gid, &gid_attr, true))
694 pr_warn("ib_cache_gid: unable to add default gid %pI6\n",
695 gid.raw);
696 else
697 dispatch_gid_change_event(ib_dev, port);
Matan Barak9c584f02015-10-28 16:52:40 +0200698 }
Matan Barak03db3a22015-07-30 18:33:26 +0300699
Matan Barakb39ffa12015-12-23 14:56:47 +0200700release:
701 if (current_gid_attr.ndev)
702 dev_put(current_gid_attr.ndev);
703 write_unlock_irq(&table->rwlock);
704 mutex_unlock(&table->lock);
705 }
Matan Barak03db3a22015-07-30 18:33:26 +0300706}
707
708static int gid_table_reserve_default(struct ib_device *ib_dev, u8 port,
709 struct ib_gid_table *table)
710{
Matan Barakb39ffa12015-12-23 14:56:47 +0200711 unsigned int i;
712 unsigned long roce_gid_type_mask;
713 unsigned int num_default_gids;
714 unsigned int current_gid = 0;
715
716 roce_gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
717 num_default_gids = hweight_long(roce_gid_type_mask);
718 for (i = 0; i < num_default_gids && i < table->sz; i++) {
719 struct ib_gid_table_entry *entry =
720 &table->data_vec[i];
Matan Barak03db3a22015-07-30 18:33:26 +0300721
722 entry->props |= GID_TABLE_ENTRY_DEFAULT;
Matan Barakb39ffa12015-12-23 14:56:47 +0200723 current_gid = find_next_bit(&roce_gid_type_mask,
724 BITS_PER_LONG,
725 current_gid);
726 entry->attr.gid_type = current_gid++;
Matan Barak03db3a22015-07-30 18:33:26 +0300727 }
728
729 return 0;
730}
731
732static int _gid_table_setup_one(struct ib_device *ib_dev)
733{
734 u8 port;
735 struct ib_gid_table **table;
736 int err = 0;
737
738 table = kcalloc(ib_dev->phys_port_cnt, sizeof(*table), GFP_KERNEL);
739
740 if (!table) {
741 pr_warn("failed to allocate ib gid cache for %s\n",
742 ib_dev->name);
743 return -ENOMEM;
744 }
745
746 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
747 u8 rdma_port = port + rdma_start_port(ib_dev);
748
749 table[port] =
750 alloc_gid_table(
751 ib_dev->port_immutable[rdma_port].gid_tbl_len);
752 if (!table[port]) {
753 err = -ENOMEM;
754 goto rollback_table_setup;
755 }
756
757 err = gid_table_reserve_default(ib_dev,
758 port + rdma_start_port(ib_dev),
759 table[port]);
760 if (err)
761 goto rollback_table_setup;
762 }
763
764 ib_dev->cache.gid_cache = table;
765 return 0;
766
767rollback_table_setup:
768 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
769 cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
770 table[port]);
771 release_gid_table(table[port]);
772 }
773
774 kfree(table);
775 return err;
776}
777
778static void gid_table_release_one(struct ib_device *ib_dev)
779{
780 struct ib_gid_table **table = ib_dev->cache.gid_cache;
781 u8 port;
782
783 if (!table)
784 return;
785
786 for (port = 0; port < ib_dev->phys_port_cnt; port++)
787 release_gid_table(table[port]);
788
789 kfree(table);
790 ib_dev->cache.gid_cache = NULL;
791}
792
793static void gid_table_cleanup_one(struct ib_device *ib_dev)
794{
795 struct ib_gid_table **table = ib_dev->cache.gid_cache;
796 u8 port;
797
798 if (!table)
799 return;
800
801 for (port = 0; port < ib_dev->phys_port_cnt; port++)
802 cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
803 table[port]);
804}
805
806static int gid_table_setup_one(struct ib_device *ib_dev)
807{
808 int err;
809
810 err = _gid_table_setup_one(ib_dev);
811
812 if (err)
813 return err;
814
815 err = roce_rescan_device(ib_dev);
816
817 if (err) {
818 gid_table_cleanup_one(ib_dev);
819 gid_table_release_one(ib_dev);
820 }
821
822 return err;
823}
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825int ib_get_cached_gid(struct ib_device *device,
826 u8 port_num,
827 int index,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300828 union ib_gid *gid,
829 struct ib_gid_attr *gid_attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Matan Barak9c584f02015-10-28 16:52:40 +0200831 int res;
832 unsigned long flags;
833 struct ib_gid_table **ports_table = device->cache.gid_cache;
834 struct ib_gid_table *table = ports_table[port_num - rdma_start_port(device)];
835
Ira Weiny0cf18d72015-05-13 20:02:55 -0400836 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -EINVAL;
838
Matan Barak9c584f02015-10-28 16:52:40 +0200839 read_lock_irqsave(&table->rwlock, flags);
840 res = __ib_cache_gid_get(device, port_num, index, gid, gid_attr);
841 read_unlock_irqrestore(&table->rwlock, flags);
842
843 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845EXPORT_SYMBOL(ib_get_cached_gid);
846
Matan Barak03db3a22015-07-30 18:33:26 +0300847int ib_find_cached_gid(struct ib_device *device,
Ira Weiny73cdaae2015-05-31 17:15:31 -0400848 const union ib_gid *gid,
Matan Barakb39ffa12015-12-23 14:56:47 +0200849 enum ib_gid_type gid_type,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300850 struct net_device *ndev,
Matan Barak03db3a22015-07-30 18:33:26 +0300851 u8 *port_num,
852 u16 *index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Matan Barakb39ffa12015-12-23 14:56:47 +0200854 return ib_cache_gid_find(device, gid, gid_type, ndev, port_num, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856EXPORT_SYMBOL(ib_find_cached_gid);
857
Matan Barak99b27e32015-10-15 18:38:50 +0300858int ib_find_gid_by_filter(struct ib_device *device,
859 const union ib_gid *gid,
860 u8 port_num,
861 bool (*filter)(const union ib_gid *gid,
862 const struct ib_gid_attr *,
863 void *),
864 void *context, u16 *index)
865{
866 /* Only RoCE GID table supports filter function */
867 if (!rdma_cap_roce_gid_table(device, port_num) && filter)
868 return -EPROTONOSUPPORT;
869
870 return ib_cache_gid_find_by_filter(device, gid,
871 port_num, filter,
872 context, index);
873}
874EXPORT_SYMBOL(ib_find_gid_by_filter);
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876int ib_get_cached_pkey(struct ib_device *device,
877 u8 port_num,
878 int index,
879 u16 *pkey)
880{
881 struct ib_pkey_cache *cache;
882 unsigned long flags;
883 int ret = 0;
884
Ira Weiny0cf18d72015-05-13 20:02:55 -0400885 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return -EINVAL;
887
888 read_lock_irqsave(&device->cache.lock, flags);
889
Ira Weiny0cf18d72015-05-13 20:02:55 -0400890 cache = device->cache.pkey_cache[port_num - rdma_start_port(device)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 if (index < 0 || index >= cache->table_len)
893 ret = -EINVAL;
894 else
895 *pkey = cache->table[index];
896
897 read_unlock_irqrestore(&device->cache.lock, flags);
898
899 return ret;
900}
901EXPORT_SYMBOL(ib_get_cached_pkey);
902
903int ib_find_cached_pkey(struct ib_device *device,
904 u8 port_num,
905 u16 pkey,
906 u16 *index)
907{
908 struct ib_pkey_cache *cache;
909 unsigned long flags;
910 int i;
911 int ret = -ENOENT;
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000912 int partial_ix = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Ira Weiny0cf18d72015-05-13 20:02:55 -0400914 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return -EINVAL;
916
917 read_lock_irqsave(&device->cache.lock, flags);
918
Ira Weiny0cf18d72015-05-13 20:02:55 -0400919 cache = device->cache.pkey_cache[port_num - rdma_start_port(device)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 *index = -1;
922
923 for (i = 0; i < cache->table_len; ++i)
924 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000925 if (cache->table[i] & 0x8000) {
926 *index = i;
927 ret = 0;
928 break;
929 } else
930 partial_ix = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000933 if (ret && partial_ix >= 0) {
934 *index = partial_ix;
935 ret = 0;
936 }
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 read_unlock_irqrestore(&device->cache.lock, flags);
939
940 return ret;
941}
942EXPORT_SYMBOL(ib_find_cached_pkey);
943
Jack Morgenstein73aaa742012-08-03 08:40:39 +0000944int ib_find_exact_cached_pkey(struct ib_device *device,
945 u8 port_num,
946 u16 pkey,
947 u16 *index)
948{
949 struct ib_pkey_cache *cache;
950 unsigned long flags;
951 int i;
952 int ret = -ENOENT;
953
Ira Weiny0cf18d72015-05-13 20:02:55 -0400954 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
Jack Morgenstein73aaa742012-08-03 08:40:39 +0000955 return -EINVAL;
956
957 read_lock_irqsave(&device->cache.lock, flags);
958
Ira Weiny0cf18d72015-05-13 20:02:55 -0400959 cache = device->cache.pkey_cache[port_num - rdma_start_port(device)];
Jack Morgenstein73aaa742012-08-03 08:40:39 +0000960
961 *index = -1;
962
963 for (i = 0; i < cache->table_len; ++i)
964 if (cache->table[i] == pkey) {
965 *index = i;
966 ret = 0;
967 break;
968 }
969
970 read_unlock_irqrestore(&device->cache.lock, flags);
971
972 return ret;
973}
974EXPORT_SYMBOL(ib_find_exact_cached_pkey);
975
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -0700976int ib_get_cached_lmc(struct ib_device *device,
977 u8 port_num,
978 u8 *lmc)
979{
980 unsigned long flags;
981 int ret = 0;
982
Ira Weiny0cf18d72015-05-13 20:02:55 -0400983 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -0700984 return -EINVAL;
985
986 read_lock_irqsave(&device->cache.lock, flags);
Ira Weiny0cf18d72015-05-13 20:02:55 -0400987 *lmc = device->cache.lmc_cache[port_num - rdma_start_port(device)];
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -0700988 read_unlock_irqrestore(&device->cache.lock, flags);
989
990 return ret;
991}
992EXPORT_SYMBOL(ib_get_cached_lmc);
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994static void ib_cache_update(struct ib_device *device,
995 u8 port)
996{
997 struct ib_port_attr *tprops = NULL;
998 struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache;
Matan Barak03db3a22015-07-30 18:33:26 +0300999 struct ib_gid_cache {
1000 int table_len;
1001 union ib_gid table[0];
1002 } *gid_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 int i;
1004 int ret;
Matan Barak03db3a22015-07-30 18:33:26 +03001005 struct ib_gid_table *table;
1006 struct ib_gid_table **ports_table = device->cache.gid_cache;
1007 bool use_roce_gid_table =
1008 rdma_cap_roce_gid_table(device, port);
1009
1010 if (port < rdma_start_port(device) || port > rdma_end_port(device))
1011 return;
1012
1013 table = ports_table[port - rdma_start_port(device)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
1016 if (!tprops)
1017 return;
1018
1019 ret = ib_query_port(device, port, tprops);
1020 if (ret) {
1021 printk(KERN_WARNING "ib_query_port failed (%d) for %s\n",
1022 ret, device->name);
1023 goto err;
1024 }
1025
1026 pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
1027 sizeof *pkey_cache->table, GFP_KERNEL);
1028 if (!pkey_cache)
1029 goto err;
1030
1031 pkey_cache->table_len = tprops->pkey_tbl_len;
1032
Matan Barak03db3a22015-07-30 18:33:26 +03001033 if (!use_roce_gid_table) {
1034 gid_cache = kmalloc(sizeof(*gid_cache) + tprops->gid_tbl_len *
1035 sizeof(*gid_cache->table), GFP_KERNEL);
1036 if (!gid_cache)
1037 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Matan Barak03db3a22015-07-30 18:33:26 +03001039 gid_cache->table_len = tprops->gid_tbl_len;
1040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 for (i = 0; i < pkey_cache->table_len; ++i) {
1043 ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
1044 if (ret) {
1045 printk(KERN_WARNING "ib_query_pkey failed (%d) for %s (index %d)\n",
1046 ret, device->name, i);
1047 goto err;
1048 }
1049 }
1050
Matan Barak03db3a22015-07-30 18:33:26 +03001051 if (!use_roce_gid_table) {
1052 for (i = 0; i < gid_cache->table_len; ++i) {
1053 ret = ib_query_gid(device, port, i,
Matan Barak55ee3ab2015-10-15 18:38:45 +03001054 gid_cache->table + i, NULL);
Matan Barak03db3a22015-07-30 18:33:26 +03001055 if (ret) {
1056 printk(KERN_WARNING "ib_query_gid failed (%d) for %s (index %d)\n",
1057 ret, device->name, i);
1058 goto err;
1059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061 }
1062
1063 write_lock_irq(&device->cache.lock);
1064
Ira Weiny0cf18d72015-05-13 20:02:55 -04001065 old_pkey_cache = device->cache.pkey_cache[port - rdma_start_port(device)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Ira Weiny0cf18d72015-05-13 20:02:55 -04001067 device->cache.pkey_cache[port - rdma_start_port(device)] = pkey_cache;
Matan Barak03db3a22015-07-30 18:33:26 +03001068 if (!use_roce_gid_table) {
Matan Barak9c584f02015-10-28 16:52:40 +02001069 write_lock(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +03001070 for (i = 0; i < gid_cache->table_len; i++) {
1071 modify_gid(device, port, table, i, gid_cache->table + i,
1072 &zattr, false);
1073 }
Matan Barak9c584f02015-10-28 16:52:40 +02001074 write_unlock(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +03001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Ira Weiny0cf18d72015-05-13 20:02:55 -04001077 device->cache.lmc_cache[port - rdma_start_port(device)] = tprops->lmc;
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -07001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 write_unlock_irq(&device->cache.lock);
1080
Matan Barak03db3a22015-07-30 18:33:26 +03001081 kfree(gid_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 kfree(old_pkey_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 kfree(tprops);
1084 return;
1085
1086err:
1087 kfree(pkey_cache);
1088 kfree(gid_cache);
1089 kfree(tprops);
1090}
1091
David Howellsc4028952006-11-22 14:57:56 +00001092static void ib_cache_task(struct work_struct *_work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
David Howellsc4028952006-11-22 14:57:56 +00001094 struct ib_update_work *work =
1095 container_of(_work, struct ib_update_work, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 ib_cache_update(work->device, work->port_num);
1098 kfree(work);
1099}
1100
1101static void ib_cache_event(struct ib_event_handler *handler,
1102 struct ib_event *event)
1103{
1104 struct ib_update_work *work;
1105
1106 if (event->event == IB_EVENT_PORT_ERR ||
1107 event->event == IB_EVENT_PORT_ACTIVE ||
1108 event->event == IB_EVENT_LID_CHANGE ||
1109 event->event == IB_EVENT_PKEY_CHANGE ||
Jack Morgensteinacaea9e2006-08-15 17:20:50 +03001110 event->event == IB_EVENT_SM_CHANGE ||
Or Gerlitz761d90e2011-06-15 14:39:29 +00001111 event->event == IB_EVENT_CLIENT_REREGISTER ||
1112 event->event == IB_EVENT_GID_CHANGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 work = kmalloc(sizeof *work, GFP_ATOMIC);
1114 if (work) {
David Howellsc4028952006-11-22 14:57:56 +00001115 INIT_WORK(&work->work, ib_cache_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 work->device = event->device;
1117 work->port_num = event->element.port_num;
Tejun Heof0626712010-10-19 15:24:36 +00001118 queue_work(ib_wq, &work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120 }
1121}
1122
Matan Barak03db3a22015-07-30 18:33:26 +03001123int ib_cache_setup_one(struct ib_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
1125 int p;
Matan Barak03db3a22015-07-30 18:33:26 +03001126 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 rwlock_init(&device->cache.lock);
1129
1130 device->cache.pkey_cache =
Jason Gunthorpe55aeed02015-08-04 15:23:34 -06001131 kzalloc(sizeof *device->cache.pkey_cache *
Ira Weiny0cf18d72015-05-13 20:02:55 -04001132 (rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL);
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -07001133 device->cache.lmc_cache = kmalloc(sizeof *device->cache.lmc_cache *
Ira Weiny0cf18d72015-05-13 20:02:55 -04001134 (rdma_end_port(device) -
1135 rdma_start_port(device) + 1),
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -07001136 GFP_KERNEL);
Matan Barak03db3a22015-07-30 18:33:26 +03001137 if (!device->cache.pkey_cache ||
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -07001138 !device->cache.lmc_cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 printk(KERN_WARNING "Couldn't allocate cache "
1140 "for %s\n", device->name);
Matan Barak03db3a22015-07-30 18:33:26 +03001141 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
Matan Barak03db3a22015-07-30 18:33:26 +03001144 err = gid_table_setup_one(device);
1145 if (err)
1146 /* Allocated memory will be cleaned in the release function */
1147 return err;
1148
Jason Gunthorpe55aeed02015-08-04 15:23:34 -06001149 for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
Ira Weiny0cf18d72015-05-13 20:02:55 -04001150 ib_cache_update(device, p + rdma_start_port(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
1153 device, ib_cache_event);
Matan Barak03db3a22015-07-30 18:33:26 +03001154 err = ib_register_event_handler(&device->cache.event_handler);
1155 if (err)
1156 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Matan Barak03db3a22015-07-30 18:33:26 +03001158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160err:
Matan Barak03db3a22015-07-30 18:33:26 +03001161 gid_table_cleanup_one(device);
1162 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Matan Barak03db3a22015-07-30 18:33:26 +03001165void ib_cache_release_one(struct ib_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 int p;
1168
Matan Barak03db3a22015-07-30 18:33:26 +03001169 /*
1170 * The release function frees all the cache elements.
1171 * This function should be called as part of freeing
1172 * all the device's resources when the cache could no
1173 * longer be accessed.
1174 */
1175 if (device->cache.pkey_cache)
1176 for (p = 0;
1177 p <= rdma_end_port(device) - rdma_start_port(device); ++p)
1178 kfree(device->cache.pkey_cache[p]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Matan Barak03db3a22015-07-30 18:33:26 +03001180 gid_table_release_one(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 kfree(device->cache.pkey_cache);
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -07001182 kfree(device->cache.lmc_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
Matan Barak03db3a22015-07-30 18:33:26 +03001185void ib_cache_cleanup_one(struct ib_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Matan Barak03db3a22015-07-30 18:33:26 +03001187 /* The cleanup function unregisters the event handler,
1188 * waits for all in-progress workqueue elements and cleans
1189 * up the GID cache. This function should be called after
1190 * the device was removed from the devices list and all
1191 * clients were removed, so the cache exists but is
1192 * non-functional and shouldn't be updated anymore.
1193 */
1194 ib_unregister_event_handler(&device->cache.event_handler);
1195 flush_workqueue(ib_wq);
1196 gid_table_cleanup_one(device);
1197}
1198
1199void __init ib_cache_setup(void)
1200{
1201 roce_gid_mgmt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204void __exit ib_cache_cleanup(void)
1205{
Matan Barak03db3a22015-07-30 18:33:26 +03001206 roce_gid_mgmt_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}