blob: 31def0f2ac49b218980e6a56cafa55ffccc3e770 [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;
Daniel Jurgensd291f1a2017-05-19 15:48:52 +030056 bool enforce_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
Moni Shouae26be1b2015-07-30 18:33:29 +030059union ib_gid zgid;
60EXPORT_SYMBOL(zgid);
Matan Barak03db3a22015-07-30 18:33:26 +030061
62static const struct ib_gid_attr zattr;
63
64enum gid_attr_find_mask {
65 GID_ATTR_FIND_MASK_GID = 1UL << 0,
66 GID_ATTR_FIND_MASK_NETDEV = 1UL << 1,
67 GID_ATTR_FIND_MASK_DEFAULT = 1UL << 2,
Matan Barakb39ffa12015-12-23 14:56:47 +020068 GID_ATTR_FIND_MASK_GID_TYPE = 1UL << 3,
Matan Barak03db3a22015-07-30 18:33:26 +030069};
70
71enum gid_table_entry_props {
72 GID_TABLE_ENTRY_INVALID = 1UL << 0,
73 GID_TABLE_ENTRY_DEFAULT = 1UL << 1,
74};
75
76enum gid_table_write_action {
77 GID_TABLE_WRITE_ACTION_ADD,
78 GID_TABLE_WRITE_ACTION_DEL,
79 /* MODIFY only updates the GID table. Currently only used by
80 * ib_cache_update.
81 */
82 GID_TABLE_WRITE_ACTION_MODIFY
83};
84
85struct ib_gid_table_entry {
Matan Barak03db3a22015-07-30 18:33:26 +030086 unsigned long props;
87 union ib_gid gid;
88 struct ib_gid_attr attr;
89 void *context;
90};
91
92struct ib_gid_table {
93 int sz;
94 /* In RoCE, adding a GID to the table requires:
95 * (a) Find if this GID is already exists.
96 * (b) Find a free space.
97 * (c) Write the new GID
98 *
99 * Delete requires different set of operations:
100 * (a) Find the GID
101 * (b) Delete it.
102 *
103 * Add/delete should be carried out atomically.
104 * This is done by locking this mutex from multiple
105 * writers. We don't need this lock for IB, as the MAD
106 * layer replaces all entries. All data_vec entries
107 * are locked by this lock.
108 **/
109 struct mutex lock;
Matan Barak9c584f02015-10-28 16:52:40 +0200110 /* This lock protects the table entries from being
111 * read and written simultaneously.
112 */
113 rwlock_t rwlock;
Matan Barak03db3a22015-07-30 18:33:26 +0300114 struct ib_gid_table_entry *data_vec;
115};
116
Matan Barakf3906bd2015-10-28 16:52:39 +0200117static void dispatch_gid_change_event(struct ib_device *ib_dev, u8 port)
118{
119 if (rdma_cap_roce_gid_table(ib_dev, port)) {
120 struct ib_event event;
121
122 event.device = ib_dev;
123 event.element.port_num = port;
124 event.event = IB_EVENT_GID_CHANGE;
125
126 ib_dispatch_event(&event);
127 }
128}
129
Matan Barakb39ffa12015-12-23 14:56:47 +0200130static const char * const gid_type_str[] = {
131 [IB_GID_TYPE_IB] = "IB/RoCE v1",
Matan Barak7766a992015-12-23 14:56:50 +0200132 [IB_GID_TYPE_ROCE_UDP_ENCAP] = "RoCE v2",
Matan Barakb39ffa12015-12-23 14:56:47 +0200133};
134
135const char *ib_cache_gid_type_str(enum ib_gid_type gid_type)
136{
137 if (gid_type < ARRAY_SIZE(gid_type_str) && gid_type_str[gid_type])
138 return gid_type_str[gid_type];
139
140 return "Invalid GID type";
141}
142EXPORT_SYMBOL(ib_cache_gid_type_str);
143
Matan Barak045959d2015-12-23 14:56:55 +0200144int ib_cache_gid_parse_type_str(const char *buf)
145{
146 unsigned int i;
147 size_t len;
148 int err = -EINVAL;
149
150 len = strlen(buf);
151 if (len == 0)
152 return -EINVAL;
153
154 if (buf[len - 1] == '\n')
155 len--;
156
157 for (i = 0; i < ARRAY_SIZE(gid_type_str); ++i)
158 if (gid_type_str[i] && !strncmp(buf, gid_type_str[i], len) &&
159 len == strlen(gid_type_str[i])) {
160 err = i;
161 break;
162 }
163
164 return err;
165}
166EXPORT_SYMBOL(ib_cache_gid_parse_type_str);
167
Matan Barak9c584f02015-10-28 16:52:40 +0200168/* This function expects that rwlock will be write locked in all
169 * scenarios and that lock will be locked in sleep-able (RoCE)
170 * scenarios.
171 */
Matan Barak03db3a22015-07-30 18:33:26 +0300172static int write_gid(struct ib_device *ib_dev, u8 port,
173 struct ib_gid_table *table, int ix,
174 const union ib_gid *gid,
175 const struct ib_gid_attr *attr,
176 enum gid_table_write_action action,
177 bool default_gid)
Matan Barak2e2cdac2015-12-30 16:14:17 +0200178 __releases(&table->rwlock) __acquires(&table->rwlock)
Matan Barak03db3a22015-07-30 18:33:26 +0300179{
180 int ret = 0;
181 struct net_device *old_net_dev;
Aviv Heller8e7876462016-06-04 15:15:21 +0300182 enum ib_gid_type old_gid_type;
Matan Barak03db3a22015-07-30 18:33:26 +0300183
184 /* in rdma_cap_roce_gid_table, this funciton should be protected by a
185 * sleep-able lock.
186 */
Matan Barak03db3a22015-07-30 18:33:26 +0300187
188 if (rdma_cap_roce_gid_table(ib_dev, port)) {
189 table->data_vec[ix].props |= GID_TABLE_ENTRY_INVALID;
Matan Barak9c584f02015-10-28 16:52:40 +0200190 write_unlock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300191 /* GID_TABLE_WRITE_ACTION_MODIFY currently isn't supported by
192 * RoCE providers and thus only updates the cache.
193 */
194 if (action == GID_TABLE_WRITE_ACTION_ADD)
195 ret = ib_dev->add_gid(ib_dev, port, ix, gid, attr,
196 &table->data_vec[ix].context);
197 else if (action == GID_TABLE_WRITE_ACTION_DEL)
198 ret = ib_dev->del_gid(ib_dev, port, ix,
199 &table->data_vec[ix].context);
Matan Barak9c584f02015-10-28 16:52:40 +0200200 write_lock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300201 }
202
203 old_net_dev = table->data_vec[ix].attr.ndev;
Aviv Heller8e7876462016-06-04 15:15:21 +0300204 old_gid_type = table->data_vec[ix].attr.gid_type;
Matan Barak03db3a22015-07-30 18:33:26 +0300205 if (old_net_dev && old_net_dev != attr->ndev)
206 dev_put(old_net_dev);
207 /* if modify_gid failed, just delete the old gid */
208 if (ret || action == GID_TABLE_WRITE_ACTION_DEL) {
209 gid = &zgid;
210 attr = &zattr;
211 table->data_vec[ix].context = NULL;
212 }
Aviv Heller8e7876462016-06-04 15:15:21 +0300213
Matan Barak03db3a22015-07-30 18:33:26 +0300214 memcpy(&table->data_vec[ix].gid, gid, sizeof(*gid));
215 memcpy(&table->data_vec[ix].attr, attr, sizeof(*attr));
Aviv Heller8e7876462016-06-04 15:15:21 +0300216 if (default_gid) {
217 table->data_vec[ix].props |= GID_TABLE_ENTRY_DEFAULT;
218 if (action == GID_TABLE_WRITE_ACTION_DEL)
219 table->data_vec[ix].attr.gid_type = old_gid_type;
220 }
Matan Barak03db3a22015-07-30 18:33:26 +0300221 if (table->data_vec[ix].attr.ndev &&
222 table->data_vec[ix].attr.ndev != old_net_dev)
223 dev_hold(table->data_vec[ix].attr.ndev);
224
225 table->data_vec[ix].props &= ~GID_TABLE_ENTRY_INVALID;
226
Matan Barak03db3a22015-07-30 18:33:26 +0300227 return ret;
228}
229
230static int add_gid(struct ib_device *ib_dev, u8 port,
231 struct ib_gid_table *table, int ix,
232 const union ib_gid *gid,
233 const struct ib_gid_attr *attr,
234 bool default_gid) {
235 return write_gid(ib_dev, port, table, ix, gid, attr,
236 GID_TABLE_WRITE_ACTION_ADD, default_gid);
237}
238
239static int modify_gid(struct ib_device *ib_dev, u8 port,
240 struct ib_gid_table *table, int ix,
241 const union ib_gid *gid,
242 const struct ib_gid_attr *attr,
243 bool default_gid) {
244 return write_gid(ib_dev, port, table, ix, gid, attr,
245 GID_TABLE_WRITE_ACTION_MODIFY, default_gid);
246}
247
248static int del_gid(struct ib_device *ib_dev, u8 port,
249 struct ib_gid_table *table, int ix,
250 bool default_gid) {
251 return write_gid(ib_dev, port, table, ix, &zgid, &zattr,
252 GID_TABLE_WRITE_ACTION_DEL, default_gid);
253}
254
Matan Barak9c584f02015-10-28 16:52:40 +0200255/* rwlock should be read locked */
Matan Barak03db3a22015-07-30 18:33:26 +0300256static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,
257 const struct ib_gid_attr *val, bool default_gid,
Matan Barakcee3c4d2015-10-28 16:52:41 +0200258 unsigned long mask, int *pempty)
Matan Barak03db3a22015-07-30 18:33:26 +0300259{
Matan Barakcee3c4d2015-10-28 16:52:41 +0200260 int i = 0;
261 int found = -1;
262 int empty = pempty ? -1 : 0;
Matan Barak03db3a22015-07-30 18:33:26 +0300263
Matan Barakcee3c4d2015-10-28 16:52:41 +0200264 while (i < table->sz && (found < 0 || empty < 0)) {
265 struct ib_gid_table_entry *data = &table->data_vec[i];
266 struct ib_gid_attr *attr = &data->attr;
267 int curr_index = i;
Matan Barak03db3a22015-07-30 18:33:26 +0300268
Matan Barakcee3c4d2015-10-28 16:52:41 +0200269 i++;
Matan Barak03db3a22015-07-30 18:33:26 +0300270
Matan Barakcee3c4d2015-10-28 16:52:41 +0200271 if (data->props & GID_TABLE_ENTRY_INVALID)
272 continue;
273
274 if (empty < 0)
275 if (!memcmp(&data->gid, &zgid, sizeof(*gid)) &&
276 !memcmp(attr, &zattr, sizeof(*attr)) &&
277 !data->props)
278 empty = curr_index;
279
280 if (found >= 0)
Matan Barak9c584f02015-10-28 16:52:40 +0200281 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300282
Matan Barakb39ffa12015-12-23 14:56:47 +0200283 if (mask & GID_ATTR_FIND_MASK_GID_TYPE &&
284 attr->gid_type != val->gid_type)
285 continue;
286
Matan Barak03db3a22015-07-30 18:33:26 +0300287 if (mask & GID_ATTR_FIND_MASK_GID &&
Matan Barakcee3c4d2015-10-28 16:52:41 +0200288 memcmp(gid, &data->gid, sizeof(*gid)))
Matan Barak9c584f02015-10-28 16:52:40 +0200289 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300290
291 if (mask & GID_ATTR_FIND_MASK_NETDEV &&
292 attr->ndev != val->ndev)
Matan Barak9c584f02015-10-28 16:52:40 +0200293 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300294
295 if (mask & GID_ATTR_FIND_MASK_DEFAULT &&
Matan Barakcee3c4d2015-10-28 16:52:41 +0200296 !!(data->props & GID_TABLE_ENTRY_DEFAULT) !=
Matan Barak03db3a22015-07-30 18:33:26 +0300297 default_gid)
Matan Barak9c584f02015-10-28 16:52:40 +0200298 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300299
Matan Barakcee3c4d2015-10-28 16:52:41 +0200300 found = curr_index;
Matan Barak03db3a22015-07-30 18:33:26 +0300301 }
302
Matan Barakcee3c4d2015-10-28 16:52:41 +0200303 if (pempty)
304 *pempty = empty;
305
306 return found;
Matan Barak03db3a22015-07-30 18:33:26 +0300307}
308
309static void make_default_gid(struct net_device *dev, union ib_gid *gid)
310{
311 gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
312 addrconf_ifid_eui48(&gid->raw[8], dev);
313}
314
315int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
316 union ib_gid *gid, struct ib_gid_attr *attr)
317{
Matan Barak03db3a22015-07-30 18:33:26 +0300318 struct ib_gid_table *table;
319 int ix;
320 int ret = 0;
321 struct net_device *idev;
Matan Barakcee3c4d2015-10-28 16:52:41 +0200322 int empty;
Matan Barak03db3a22015-07-30 18:33:26 +0300323
Jack Wang21d64542017-01-17 10:11:12 +0100324 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
Matan Barak03db3a22015-07-30 18:33:26 +0300325
326 if (!memcmp(gid, &zgid, sizeof(*gid)))
327 return -EINVAL;
328
329 if (ib_dev->get_netdev) {
330 idev = ib_dev->get_netdev(ib_dev, port);
331 if (idev && attr->ndev != idev) {
332 union ib_gid default_gid;
333
334 /* Adding default GIDs in not permitted */
335 make_default_gid(idev, &default_gid);
336 if (!memcmp(gid, &default_gid, sizeof(*gid))) {
337 dev_put(idev);
338 return -EPERM;
339 }
340 }
341 if (idev)
342 dev_put(idev);
343 }
344
345 mutex_lock(&table->lock);
Matan Barak9c584f02015-10-28 16:52:40 +0200346 write_lock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300347
348 ix = find_gid(table, gid, attr, false, GID_ATTR_FIND_MASK_GID |
Matan Barakb39ffa12015-12-23 14:56:47 +0200349 GID_ATTR_FIND_MASK_GID_TYPE |
Matan Barakcee3c4d2015-10-28 16:52:41 +0200350 GID_ATTR_FIND_MASK_NETDEV, &empty);
Matan Barak03db3a22015-07-30 18:33:26 +0300351 if (ix >= 0)
352 goto out_unlock;
353
Matan Barakcee3c4d2015-10-28 16:52:41 +0200354 if (empty < 0) {
Matan Barak03db3a22015-07-30 18:33:26 +0300355 ret = -ENOSPC;
356 goto out_unlock;
357 }
358
Matan Barakcee3c4d2015-10-28 16:52:41 +0200359 ret = add_gid(ib_dev, port, table, empty, gid, attr, false);
Matan Barak9c584f02015-10-28 16:52:40 +0200360 if (!ret)
361 dispatch_gid_change_event(ib_dev, port);
Matan Barak03db3a22015-07-30 18:33:26 +0300362
363out_unlock:
Matan Barak9c584f02015-10-28 16:52:40 +0200364 write_unlock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300365 mutex_unlock(&table->lock);
366 return ret;
367}
368
369int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
370 union ib_gid *gid, struct ib_gid_attr *attr)
371{
Matan Barak03db3a22015-07-30 18:33:26 +0300372 struct ib_gid_table *table;
373 int ix;
374
Jack Wang21d64542017-01-17 10:11:12 +0100375 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
Matan Barak03db3a22015-07-30 18:33:26 +0300376
377 mutex_lock(&table->lock);
Matan Barak9c584f02015-10-28 16:52:40 +0200378 write_lock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300379
380 ix = find_gid(table, gid, attr, false,
381 GID_ATTR_FIND_MASK_GID |
Matan Barakb39ffa12015-12-23 14:56:47 +0200382 GID_ATTR_FIND_MASK_GID_TYPE |
Matan Barak03db3a22015-07-30 18:33:26 +0300383 GID_ATTR_FIND_MASK_NETDEV |
Matan Barakcee3c4d2015-10-28 16:52:41 +0200384 GID_ATTR_FIND_MASK_DEFAULT,
385 NULL);
Matan Barak03db3a22015-07-30 18:33:26 +0300386 if (ix < 0)
387 goto out_unlock;
388
Matan Barak9c584f02015-10-28 16:52:40 +0200389 if (!del_gid(ib_dev, port, table, ix, false))
390 dispatch_gid_change_event(ib_dev, port);
Matan Barak03db3a22015-07-30 18:33:26 +0300391
392out_unlock:
Matan Barak9c584f02015-10-28 16:52:40 +0200393 write_unlock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300394 mutex_unlock(&table->lock);
395 return 0;
396}
397
398int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
399 struct net_device *ndev)
400{
Matan Barak03db3a22015-07-30 18:33:26 +0300401 struct ib_gid_table *table;
402 int ix;
Matan Barak9c584f02015-10-28 16:52:40 +0200403 bool deleted = false;
Matan Barak03db3a22015-07-30 18:33:26 +0300404
Jack Wang21d64542017-01-17 10:11:12 +0100405 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
Matan Barak03db3a22015-07-30 18:33:26 +0300406
407 mutex_lock(&table->lock);
Matan Barak9c584f02015-10-28 16:52:40 +0200408 write_lock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300409
410 for (ix = 0; ix < table->sz; ix++)
411 if (table->data_vec[ix].attr.ndev == ndev)
Talat Batheeshf336ae02016-06-22 17:27:22 +0300412 if (!del_gid(ib_dev, port, table, ix,
413 !!(table->data_vec[ix].props &
414 GID_TABLE_ENTRY_DEFAULT)))
Matan Barak9c584f02015-10-28 16:52:40 +0200415 deleted = true;
Matan Barak03db3a22015-07-30 18:33:26 +0300416
Matan Barak9c584f02015-10-28 16:52:40 +0200417 write_unlock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300418 mutex_unlock(&table->lock);
Matan Barak9c584f02015-10-28 16:52:40 +0200419
420 if (deleted)
421 dispatch_gid_change_event(ib_dev, port);
422
Matan Barak03db3a22015-07-30 18:33:26 +0300423 return 0;
424}
425
426static int __ib_cache_gid_get(struct ib_device *ib_dev, u8 port, int index,
427 union ib_gid *gid, struct ib_gid_attr *attr)
428{
Matan Barak03db3a22015-07-30 18:33:26 +0300429 struct ib_gid_table *table;
Matan Barak03db3a22015-07-30 18:33:26 +0300430
Jack Wang21d64542017-01-17 10:11:12 +0100431 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
Matan Barak03db3a22015-07-30 18:33:26 +0300432
433 if (index < 0 || index >= table->sz)
434 return -EINVAL;
435
Matan Barak9c584f02015-10-28 16:52:40 +0200436 if (table->data_vec[index].props & GID_TABLE_ENTRY_INVALID)
Matan Barak03db3a22015-07-30 18:33:26 +0300437 return -EAGAIN;
Matan Barak03db3a22015-07-30 18:33:26 +0300438
439 memcpy(gid, &table->data_vec[index].gid, sizeof(*gid));
440 if (attr) {
441 memcpy(attr, &table->data_vec[index].attr, sizeof(*attr));
442 if (attr->ndev)
443 dev_hold(attr->ndev);
444 }
445
Matan Barak03db3a22015-07-30 18:33:26 +0300446 return 0;
447}
448
449static int _ib_cache_gid_table_find(struct ib_device *ib_dev,
450 const union ib_gid *gid,
451 const struct ib_gid_attr *val,
452 unsigned long mask,
453 u8 *port, u16 *index)
454{
Matan Barak03db3a22015-07-30 18:33:26 +0300455 struct ib_gid_table *table;
456 u8 p;
457 int local_index;
Matan Barak9c584f02015-10-28 16:52:40 +0200458 unsigned long flags;
Matan Barak03db3a22015-07-30 18:33:26 +0300459
460 for (p = 0; p < ib_dev->phys_port_cnt; p++) {
Jack Wang21d64542017-01-17 10:11:12 +0100461 table = ib_dev->cache.ports[p].gid;
Matan Barak9c584f02015-10-28 16:52:40 +0200462 read_lock_irqsave(&table->rwlock, flags);
Matan Barakcee3c4d2015-10-28 16:52:41 +0200463 local_index = find_gid(table, gid, val, false, mask, NULL);
Matan Barak03db3a22015-07-30 18:33:26 +0300464 if (local_index >= 0) {
465 if (index)
466 *index = local_index;
467 if (port)
468 *port = p + rdma_start_port(ib_dev);
Matan Barak9c584f02015-10-28 16:52:40 +0200469 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak03db3a22015-07-30 18:33:26 +0300470 return 0;
471 }
Matan Barak9c584f02015-10-28 16:52:40 +0200472 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak03db3a22015-07-30 18:33:26 +0300473 }
474
475 return -ENOENT;
476}
477
478static int ib_cache_gid_find(struct ib_device *ib_dev,
479 const union ib_gid *gid,
Matan Barakb39ffa12015-12-23 14:56:47 +0200480 enum ib_gid_type gid_type,
Matan Barak03db3a22015-07-30 18:33:26 +0300481 struct net_device *ndev, u8 *port,
482 u16 *index)
483{
Matan Barakb39ffa12015-12-23 14:56:47 +0200484 unsigned long mask = GID_ATTR_FIND_MASK_GID |
485 GID_ATTR_FIND_MASK_GID_TYPE;
486 struct ib_gid_attr gid_attr_val = {.ndev = ndev, .gid_type = gid_type};
Matan Barak03db3a22015-07-30 18:33:26 +0300487
488 if (ndev)
489 mask |= GID_ATTR_FIND_MASK_NETDEV;
490
491 return _ib_cache_gid_table_find(ib_dev, gid, &gid_attr_val,
492 mask, port, index);
493}
494
Parav Pandit6612b492018-03-13 16:06:11 +0200495/**
496 * ib_find_cached_gid_by_port - Returns the GID table index where a specified
497 * GID value occurs. It searches for the specified GID value in the local
498 * software cache.
499 * @device: The device to query.
500 * @gid: The GID value to search for.
501 * @gid_type: The GID type to search for.
502 * @port_num: The port number of the device where the GID value should be
503 * searched.
504 * @ndev: In RoCE, the net device of the device. Null means ignore.
505 * @index: The index into the cached GID table where the GID was found. This
506 * parameter may be NULL.
507 */
Matan Barakd300ec52015-10-15 18:38:46 +0300508int ib_find_cached_gid_by_port(struct ib_device *ib_dev,
509 const union ib_gid *gid,
Matan Barakb39ffa12015-12-23 14:56:47 +0200510 enum ib_gid_type gid_type,
Matan Barakd300ec52015-10-15 18:38:46 +0300511 u8 port, struct net_device *ndev,
512 u16 *index)
Matan Barak03db3a22015-07-30 18:33:26 +0300513{
514 int local_index;
Matan Barak03db3a22015-07-30 18:33:26 +0300515 struct ib_gid_table *table;
Matan Barakb39ffa12015-12-23 14:56:47 +0200516 unsigned long mask = GID_ATTR_FIND_MASK_GID |
517 GID_ATTR_FIND_MASK_GID_TYPE;
518 struct ib_gid_attr val = {.ndev = ndev, .gid_type = gid_type};
Matan Barak9c584f02015-10-28 16:52:40 +0200519 unsigned long flags;
Matan Barak03db3a22015-07-30 18:33:26 +0300520
Yuval Shaia24dc8312017-01-25 18:41:37 +0200521 if (!rdma_is_port_valid(ib_dev, port))
Matan Barak03db3a22015-07-30 18:33:26 +0300522 return -ENOENT;
523
Jack Wang21d64542017-01-17 10:11:12 +0100524 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
Matan Barak03db3a22015-07-30 18:33:26 +0300525
526 if (ndev)
527 mask |= GID_ATTR_FIND_MASK_NETDEV;
528
Matan Barak9c584f02015-10-28 16:52:40 +0200529 read_lock_irqsave(&table->rwlock, flags);
Matan Barakcee3c4d2015-10-28 16:52:41 +0200530 local_index = find_gid(table, gid, &val, false, mask, NULL);
Matan Barak03db3a22015-07-30 18:33:26 +0300531 if (local_index >= 0) {
532 if (index)
533 *index = local_index;
Matan Barak9c584f02015-10-28 16:52:40 +0200534 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak03db3a22015-07-30 18:33:26 +0300535 return 0;
536 }
537
Matan Barak9c584f02015-10-28 16:52:40 +0200538 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak03db3a22015-07-30 18:33:26 +0300539 return -ENOENT;
540}
Matan Barakd300ec52015-10-15 18:38:46 +0300541EXPORT_SYMBOL(ib_find_cached_gid_by_port);
Matan Barak03db3a22015-07-30 18:33:26 +0300542
Matan Barak99b27e32015-10-15 18:38:50 +0300543/**
Parav Pandit6612b492018-03-13 16:06:11 +0200544 * ib_cache_gid_find_by_filter - Returns the GID table index where a specified
Matan Barak99b27e32015-10-15 18:38:50 +0300545 * GID value occurs
546 * @device: The device to query.
547 * @gid: The GID value to search for.
548 * @port_num: The port number of the device where the GID value could be
549 * searched.
550 * @filter: The filter function is executed on any matching GID in the table.
551 * If the filter function returns true, the corresponding index is returned,
552 * otherwise, we continue searching the GID table. It's guaranteed that
553 * while filter is executed, ndev field is valid and the structure won't
554 * change. filter is executed in an atomic context. filter must not be NULL.
Parav Pandit6612b492018-03-13 16:06:11 +0200555 * @index: The index into the cached GID table where the GID was found. This
Matan Barak99b27e32015-10-15 18:38:50 +0300556 * parameter may be NULL.
557 *
558 * ib_cache_gid_find_by_filter() searches for the specified GID value
559 * of which the filter function returns true in the port's GID table.
560 * This function is only supported on RoCE ports.
561 *
562 */
563static int ib_cache_gid_find_by_filter(struct ib_device *ib_dev,
564 const union ib_gid *gid,
565 u8 port,
566 bool (*filter)(const union ib_gid *,
567 const struct ib_gid_attr *,
568 void *),
569 void *context,
570 u16 *index)
571{
Matan Barak99b27e32015-10-15 18:38:50 +0300572 struct ib_gid_table *table;
573 unsigned int i;
Matan Barak9c584f02015-10-28 16:52:40 +0200574 unsigned long flags;
Matan Barak99b27e32015-10-15 18:38:50 +0300575 bool found = false;
576
Matan Barak99b27e32015-10-15 18:38:50 +0300577
Yuval Shaia24dc8312017-01-25 18:41:37 +0200578 if (!rdma_is_port_valid(ib_dev, port) ||
Matan Barak99b27e32015-10-15 18:38:50 +0300579 !rdma_protocol_roce(ib_dev, port))
580 return -EPROTONOSUPPORT;
581
Jack Wang21d64542017-01-17 10:11:12 +0100582 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
Matan Barak99b27e32015-10-15 18:38:50 +0300583
Matan Barak9c584f02015-10-28 16:52:40 +0200584 read_lock_irqsave(&table->rwlock, flags);
Matan Barak99b27e32015-10-15 18:38:50 +0300585 for (i = 0; i < table->sz; i++) {
586 struct ib_gid_attr attr;
Matan Barak99b27e32015-10-15 18:38:50 +0300587
Matan Barak99b27e32015-10-15 18:38:50 +0300588 if (table->data_vec[i].props & GID_TABLE_ENTRY_INVALID)
Parav Pandit151ed9d2017-11-14 14:52:05 +0200589 continue;
Matan Barak99b27e32015-10-15 18:38:50 +0300590
591 if (memcmp(gid, &table->data_vec[i].gid, sizeof(*gid)))
Parav Pandit151ed9d2017-11-14 14:52:05 +0200592 continue;
Matan Barak99b27e32015-10-15 18:38:50 +0300593
594 memcpy(&attr, &table->data_vec[i].attr, sizeof(attr));
595
Parav Pandit151ed9d2017-11-14 14:52:05 +0200596 if (filter(gid, &attr, context)) {
Matan Barak99b27e32015-10-15 18:38:50 +0300597 found = true;
Parav Pandit151ed9d2017-11-14 14:52:05 +0200598 if (index)
599 *index = i;
Matan Barak99b27e32015-10-15 18:38:50 +0300600 break;
Parav Pandit151ed9d2017-11-14 14:52:05 +0200601 }
Matan Barak99b27e32015-10-15 18:38:50 +0300602 }
Matan Barak9c584f02015-10-28 16:52:40 +0200603 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak99b27e32015-10-15 18:38:50 +0300604
605 if (!found)
606 return -ENOENT;
Matan Barak99b27e32015-10-15 18:38:50 +0300607 return 0;
608}
609
Matan Barak03db3a22015-07-30 18:33:26 +0300610static struct ib_gid_table *alloc_gid_table(int sz)
611{
Matan Barak03db3a22015-07-30 18:33:26 +0300612 struct ib_gid_table *table =
613 kzalloc(sizeof(struct ib_gid_table), GFP_KERNEL);
Matan Barak9c584f02015-10-28 16:52:40 +0200614
Matan Barak03db3a22015-07-30 18:33:26 +0300615 if (!table)
616 return NULL;
617
618 table->data_vec = kcalloc(sz, sizeof(*table->data_vec), GFP_KERNEL);
619 if (!table->data_vec)
620 goto err_free_table;
621
622 mutex_init(&table->lock);
623
624 table->sz = sz;
Matan Barak9c584f02015-10-28 16:52:40 +0200625 rwlock_init(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300626
627 return table;
628
629err_free_table:
630 kfree(table);
631 return NULL;
632}
633
634static void release_gid_table(struct ib_gid_table *table)
635{
636 if (table) {
637 kfree(table->data_vec);
638 kfree(table);
639 }
640}
641
642static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port,
643 struct ib_gid_table *table)
644{
645 int i;
Matan Barak9c584f02015-10-28 16:52:40 +0200646 bool deleted = false;
Matan Barak03db3a22015-07-30 18:33:26 +0300647
648 if (!table)
649 return;
650
Matan Barak9c584f02015-10-28 16:52:40 +0200651 write_lock_irq(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300652 for (i = 0; i < table->sz; ++i) {
653 if (memcmp(&table->data_vec[i].gid, &zgid,
654 sizeof(table->data_vec[i].gid)))
Matan Barak9c584f02015-10-28 16:52:40 +0200655 if (!del_gid(ib_dev, port, table, i,
656 table->data_vec[i].props &
657 GID_ATTR_FIND_MASK_DEFAULT))
658 deleted = true;
Matan Barak03db3a22015-07-30 18:33:26 +0300659 }
Matan Barak9c584f02015-10-28 16:52:40 +0200660 write_unlock_irq(&table->rwlock);
661
662 if (deleted)
663 dispatch_gid_change_event(ib_dev, port);
Matan Barak03db3a22015-07-30 18:33:26 +0300664}
665
666void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
667 struct net_device *ndev,
Matan Barakb39ffa12015-12-23 14:56:47 +0200668 unsigned long gid_type_mask,
Matan Barak03db3a22015-07-30 18:33:26 +0300669 enum ib_cache_gid_default_mode mode)
670{
Matan Barak03db3a22015-07-30 18:33:26 +0300671 union ib_gid gid;
672 struct ib_gid_attr gid_attr;
Matan Barakb39ffa12015-12-23 14:56:47 +0200673 struct ib_gid_attr zattr_type = zattr;
Matan Barak03db3a22015-07-30 18:33:26 +0300674 struct ib_gid_table *table;
Matan Barakb39ffa12015-12-23 14:56:47 +0200675 unsigned int gid_type;
Matan Barak03db3a22015-07-30 18:33:26 +0300676
Jack Wang21d64542017-01-17 10:11:12 +0100677 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
Matan Barak03db3a22015-07-30 18:33:26 +0300678
679 make_default_gid(ndev, &gid);
680 memset(&gid_attr, 0, sizeof(gid_attr));
681 gid_attr.ndev = ndev;
682
Matan Barakb39ffa12015-12-23 14:56:47 +0200683 for (gid_type = 0; gid_type < IB_GID_TYPE_SIZE; ++gid_type) {
684 int ix;
685 union ib_gid current_gid;
686 struct ib_gid_attr current_gid_attr = {};
Matan Barak03db3a22015-07-30 18:33:26 +0300687
Matan Barakb39ffa12015-12-23 14:56:47 +0200688 if (1UL << gid_type & ~gid_type_mask)
689 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300690
Matan Barakb39ffa12015-12-23 14:56:47 +0200691 gid_attr.gid_type = gid_type;
Matan Barak03db3a22015-07-30 18:33:26 +0300692
Matan Barakb39ffa12015-12-23 14:56:47 +0200693 mutex_lock(&table->lock);
694 write_lock_irq(&table->rwlock);
695 ix = find_gid(table, NULL, &gid_attr, true,
696 GID_ATTR_FIND_MASK_GID_TYPE |
697 GID_ATTR_FIND_MASK_DEFAULT,
698 NULL);
699
700 /* Coudn't find default GID location */
Doug Ledfordf4e7de62016-04-22 20:14:58 -0400701 if (WARN_ON(ix < 0))
702 goto release;
Matan Barakb39ffa12015-12-23 14:56:47 +0200703
704 zattr_type.gid_type = gid_type;
705
706 if (!__ib_cache_gid_get(ib_dev, port, ix,
707 &current_gid, &current_gid_attr) &&
708 mode == IB_CACHE_GID_DEFAULT_MODE_SET &&
709 !memcmp(&gid, &current_gid, sizeof(gid)) &&
710 !memcmp(&gid_attr, &current_gid_attr, sizeof(gid_attr)))
711 goto release;
712
713 if (memcmp(&current_gid, &zgid, sizeof(current_gid)) ||
714 memcmp(&current_gid_attr, &zattr_type,
715 sizeof(current_gid_attr))) {
716 if (del_gid(ib_dev, port, table, ix, true)) {
717 pr_warn("ib_cache_gid: can't delete index %d for default gid %pI6\n",
718 ix, gid.raw);
719 goto release;
720 } else {
721 dispatch_gid_change_event(ib_dev, port);
722 }
Matan Barak9c584f02015-10-28 16:52:40 +0200723 }
Matan Barak03db3a22015-07-30 18:33:26 +0300724
Matan Barakb39ffa12015-12-23 14:56:47 +0200725 if (mode == IB_CACHE_GID_DEFAULT_MODE_SET) {
726 if (add_gid(ib_dev, port, table, ix, &gid, &gid_attr, true))
727 pr_warn("ib_cache_gid: unable to add default gid %pI6\n",
728 gid.raw);
729 else
730 dispatch_gid_change_event(ib_dev, port);
Matan Barak9c584f02015-10-28 16:52:40 +0200731 }
Matan Barak03db3a22015-07-30 18:33:26 +0300732
Matan Barakb39ffa12015-12-23 14:56:47 +0200733release:
734 if (current_gid_attr.ndev)
735 dev_put(current_gid_attr.ndev);
736 write_unlock_irq(&table->rwlock);
737 mutex_unlock(&table->lock);
738 }
Matan Barak03db3a22015-07-30 18:33:26 +0300739}
740
741static int gid_table_reserve_default(struct ib_device *ib_dev, u8 port,
742 struct ib_gid_table *table)
743{
Matan Barakb39ffa12015-12-23 14:56:47 +0200744 unsigned int i;
745 unsigned long roce_gid_type_mask;
746 unsigned int num_default_gids;
747 unsigned int current_gid = 0;
748
749 roce_gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
750 num_default_gids = hweight_long(roce_gid_type_mask);
751 for (i = 0; i < num_default_gids && i < table->sz; i++) {
752 struct ib_gid_table_entry *entry =
753 &table->data_vec[i];
Matan Barak03db3a22015-07-30 18:33:26 +0300754
755 entry->props |= GID_TABLE_ENTRY_DEFAULT;
Matan Barakb39ffa12015-12-23 14:56:47 +0200756 current_gid = find_next_bit(&roce_gid_type_mask,
757 BITS_PER_LONG,
758 current_gid);
759 entry->attr.gid_type = current_gid++;
Matan Barak03db3a22015-07-30 18:33:26 +0300760 }
761
762 return 0;
763}
764
765static int _gid_table_setup_one(struct ib_device *ib_dev)
766{
767 u8 port;
Jack Wang21d64542017-01-17 10:11:12 +0100768 struct ib_gid_table *table;
Matan Barak03db3a22015-07-30 18:33:26 +0300769 int err = 0;
770
Matan Barak03db3a22015-07-30 18:33:26 +0300771 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
772 u8 rdma_port = port + rdma_start_port(ib_dev);
773
Jack Wang21d64542017-01-17 10:11:12 +0100774 table =
Matan Barak03db3a22015-07-30 18:33:26 +0300775 alloc_gid_table(
776 ib_dev->port_immutable[rdma_port].gid_tbl_len);
Jack Wang21d64542017-01-17 10:11:12 +0100777 if (!table) {
Matan Barak03db3a22015-07-30 18:33:26 +0300778 err = -ENOMEM;
779 goto rollback_table_setup;
780 }
781
782 err = gid_table_reserve_default(ib_dev,
783 port + rdma_start_port(ib_dev),
Jack Wang21d64542017-01-17 10:11:12 +0100784 table);
Matan Barak03db3a22015-07-30 18:33:26 +0300785 if (err)
786 goto rollback_table_setup;
Jack Wang21d64542017-01-17 10:11:12 +0100787 ib_dev->cache.ports[port].gid = table;
Matan Barak03db3a22015-07-30 18:33:26 +0300788 }
789
Matan Barak03db3a22015-07-30 18:33:26 +0300790 return 0;
791
792rollback_table_setup:
793 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
Jack Wang21d64542017-01-17 10:11:12 +0100794 table = ib_dev->cache.ports[port].gid;
795
Matan Barak03db3a22015-07-30 18:33:26 +0300796 cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
Jack Wang21d64542017-01-17 10:11:12 +0100797 table);
798 release_gid_table(table);
Matan Barak03db3a22015-07-30 18:33:26 +0300799 }
800
Matan Barak03db3a22015-07-30 18:33:26 +0300801 return err;
802}
803
804static void gid_table_release_one(struct ib_device *ib_dev)
805{
Jack Wang21d64542017-01-17 10:11:12 +0100806 struct ib_gid_table *table;
Matan Barak03db3a22015-07-30 18:33:26 +0300807 u8 port;
808
Jack Wang21d64542017-01-17 10:11:12 +0100809 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
810 table = ib_dev->cache.ports[port].gid;
811 release_gid_table(table);
812 ib_dev->cache.ports[port].gid = NULL;
813 }
Matan Barak03db3a22015-07-30 18:33:26 +0300814}
815
816static void gid_table_cleanup_one(struct ib_device *ib_dev)
817{
Jack Wang21d64542017-01-17 10:11:12 +0100818 struct ib_gid_table *table;
Matan Barak03db3a22015-07-30 18:33:26 +0300819 u8 port;
820
Jack Wang21d64542017-01-17 10:11:12 +0100821 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
822 table = ib_dev->cache.ports[port].gid;
Matan Barak03db3a22015-07-30 18:33:26 +0300823 cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
Jack Wang21d64542017-01-17 10:11:12 +0100824 table);
825 }
Matan Barak03db3a22015-07-30 18:33:26 +0300826}
827
828static int gid_table_setup_one(struct ib_device *ib_dev)
829{
830 int err;
831
832 err = _gid_table_setup_one(ib_dev);
833
834 if (err)
835 return err;
836
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200837 rdma_roce_rescan_device(ib_dev);
Matan Barak03db3a22015-07-30 18:33:26 +0300838
839 return err;
840}
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842int ib_get_cached_gid(struct ib_device *device,
843 u8 port_num,
844 int index,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300845 union ib_gid *gid,
846 struct ib_gid_attr *gid_attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Matan Barak9c584f02015-10-28 16:52:40 +0200848 int res;
849 unsigned long flags;
Jack Wang21d64542017-01-17 10:11:12 +0100850 struct ib_gid_table *table;
Matan Barak9c584f02015-10-28 16:52:40 +0200851
Yuval Shaia24dc8312017-01-25 18:41:37 +0200852 if (!rdma_is_port_valid(device, port_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return -EINVAL;
854
Jack Wang21d64542017-01-17 10:11:12 +0100855 table = device->cache.ports[port_num - rdma_start_port(device)].gid;
Matan Barak9c584f02015-10-28 16:52:40 +0200856 read_lock_irqsave(&table->rwlock, flags);
857 res = __ib_cache_gid_get(device, port_num, index, gid, gid_attr);
858 read_unlock_irqrestore(&table->rwlock, flags);
859
860 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862EXPORT_SYMBOL(ib_get_cached_gid);
863
Parav Pandit6612b492018-03-13 16:06:11 +0200864/**
865 * ib_find_cached_gid - Returns the port number and GID table index where
866 * a specified GID value occurs.
867 * @device: The device to query.
868 * @gid: The GID value to search for.
869 * @gid_type: The GID type to search for.
870 * @ndev: In RoCE, the net device of the device. NULL means ignore.
871 * @port_num: The port number of the device where the GID value was found.
872 * @index: The index into the cached GID table where the GID was found. This
873 * parameter may be NULL.
874 *
875 * ib_find_cached_gid() searches for the specified GID value in
876 * the local software cache.
877 */
Matan Barak03db3a22015-07-30 18:33:26 +0300878int ib_find_cached_gid(struct ib_device *device,
Ira Weiny73cdaae2015-05-31 17:15:31 -0400879 const union ib_gid *gid,
Matan Barakb39ffa12015-12-23 14:56:47 +0200880 enum ib_gid_type gid_type,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300881 struct net_device *ndev,
Matan Barak03db3a22015-07-30 18:33:26 +0300882 u8 *port_num,
883 u16 *index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Matan Barakb39ffa12015-12-23 14:56:47 +0200885 return ib_cache_gid_find(device, gid, gid_type, ndev, port_num, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887EXPORT_SYMBOL(ib_find_cached_gid);
888
Matan Barak99b27e32015-10-15 18:38:50 +0300889int ib_find_gid_by_filter(struct ib_device *device,
890 const union ib_gid *gid,
891 u8 port_num,
892 bool (*filter)(const union ib_gid *gid,
893 const struct ib_gid_attr *,
894 void *),
895 void *context, u16 *index)
896{
897 /* Only RoCE GID table supports filter function */
898 if (!rdma_cap_roce_gid_table(device, port_num) && filter)
899 return -EPROTONOSUPPORT;
900
901 return ib_cache_gid_find_by_filter(device, gid,
902 port_num, filter,
903 context, index);
904}
Matan Barak99b27e32015-10-15 18:38:50 +0300905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906int ib_get_cached_pkey(struct ib_device *device,
907 u8 port_num,
908 int index,
909 u16 *pkey)
910{
911 struct ib_pkey_cache *cache;
912 unsigned long flags;
913 int ret = 0;
914
Yuval Shaia24dc8312017-01-25 18:41:37 +0200915 if (!rdma_is_port_valid(device, port_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return -EINVAL;
917
918 read_lock_irqsave(&device->cache.lock, flags);
919
Jack Wang21d64542017-01-17 10:11:12 +0100920 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 if (index < 0 || index >= cache->table_len)
923 ret = -EINVAL;
924 else
925 *pkey = cache->table[index];
926
927 read_unlock_irqrestore(&device->cache.lock, flags);
928
929 return ret;
930}
931EXPORT_SYMBOL(ib_get_cached_pkey);
932
Daniel Jurgens883c71f2017-05-19 15:48:51 +0300933int ib_get_cached_subnet_prefix(struct ib_device *device,
934 u8 port_num,
935 u64 *sn_pfx)
936{
937 unsigned long flags;
938 int p;
939
940 if (port_num < rdma_start_port(device) ||
941 port_num > rdma_end_port(device))
942 return -EINVAL;
943
944 p = port_num - rdma_start_port(device);
945 read_lock_irqsave(&device->cache.lock, flags);
946 *sn_pfx = device->cache.ports[p].subnet_prefix;
947 read_unlock_irqrestore(&device->cache.lock, flags);
948
949 return 0;
950}
951EXPORT_SYMBOL(ib_get_cached_subnet_prefix);
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953int ib_find_cached_pkey(struct ib_device *device,
954 u8 port_num,
955 u16 pkey,
956 u16 *index)
957{
958 struct ib_pkey_cache *cache;
959 unsigned long flags;
960 int i;
961 int ret = -ENOENT;
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000962 int partial_ix = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Yuval Shaia24dc8312017-01-25 18:41:37 +0200964 if (!rdma_is_port_valid(device, port_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return -EINVAL;
966
967 read_lock_irqsave(&device->cache.lock, flags);
968
Jack Wang21d64542017-01-17 10:11:12 +0100969 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 *index = -1;
972
973 for (i = 0; i < cache->table_len; ++i)
974 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000975 if (cache->table[i] & 0x8000) {
976 *index = i;
977 ret = 0;
978 break;
979 } else
980 partial_ix = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000983 if (ret && partial_ix >= 0) {
984 *index = partial_ix;
985 ret = 0;
986 }
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 read_unlock_irqrestore(&device->cache.lock, flags);
989
990 return ret;
991}
992EXPORT_SYMBOL(ib_find_cached_pkey);
993
Jack Morgenstein73aaa742012-08-03 08:40:39 +0000994int ib_find_exact_cached_pkey(struct ib_device *device,
995 u8 port_num,
996 u16 pkey,
997 u16 *index)
998{
999 struct ib_pkey_cache *cache;
1000 unsigned long flags;
1001 int i;
1002 int ret = -ENOENT;
1003
Yuval Shaia24dc8312017-01-25 18:41:37 +02001004 if (!rdma_is_port_valid(device, port_num))
Jack Morgenstein73aaa742012-08-03 08:40:39 +00001005 return -EINVAL;
1006
1007 read_lock_irqsave(&device->cache.lock, flags);
1008
Jack Wang21d64542017-01-17 10:11:12 +01001009 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
Jack Morgenstein73aaa742012-08-03 08:40:39 +00001010
1011 *index = -1;
1012
1013 for (i = 0; i < cache->table_len; ++i)
1014 if (cache->table[i] == pkey) {
1015 *index = i;
1016 ret = 0;
1017 break;
1018 }
1019
1020 read_unlock_irqrestore(&device->cache.lock, flags);
1021
1022 return ret;
1023}
1024EXPORT_SYMBOL(ib_find_exact_cached_pkey);
1025
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -07001026int ib_get_cached_lmc(struct ib_device *device,
1027 u8 port_num,
1028 u8 *lmc)
1029{
1030 unsigned long flags;
1031 int ret = 0;
1032
Yuval Shaia24dc8312017-01-25 18:41:37 +02001033 if (!rdma_is_port_valid(device, port_num))
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -07001034 return -EINVAL;
1035
1036 read_lock_irqsave(&device->cache.lock, flags);
Jack Wang21d64542017-01-17 10:11:12 +01001037 *lmc = device->cache.ports[port_num - rdma_start_port(device)].lmc;
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -07001038 read_unlock_irqrestore(&device->cache.lock, flags);
1039
1040 return ret;
1041}
1042EXPORT_SYMBOL(ib_get_cached_lmc);
1043
Jack Wang9e2c3f12017-01-02 13:19:24 +01001044int ib_get_cached_port_state(struct ib_device *device,
1045 u8 port_num,
1046 enum ib_port_state *port_state)
1047{
1048 unsigned long flags;
1049 int ret = 0;
1050
1051 if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
1052 return -EINVAL;
1053
1054 read_lock_irqsave(&device->cache.lock, flags);
Jack Wang21d64542017-01-17 10:11:12 +01001055 *port_state = device->cache.ports[port_num
1056 - rdma_start_port(device)].port_state;
Jack Wang9e2c3f12017-01-02 13:19:24 +01001057 read_unlock_irqrestore(&device->cache.lock, flags);
1058
1059 return ret;
1060}
1061EXPORT_SYMBOL(ib_get_cached_port_state);
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063static void ib_cache_update(struct ib_device *device,
Daniel Jurgensd291f1a2017-05-19 15:48:52 +03001064 u8 port,
1065 bool enforce_security)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 struct ib_port_attr *tprops = NULL;
1068 struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache;
Matan Barak03db3a22015-07-30 18:33:26 +03001069 struct ib_gid_cache {
1070 int table_len;
1071 union ib_gid table[0];
1072 } *gid_cache = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 int i;
1074 int ret;
Matan Barak03db3a22015-07-30 18:33:26 +03001075 struct ib_gid_table *table;
Matan Barak03db3a22015-07-30 18:33:26 +03001076 bool use_roce_gid_table =
1077 rdma_cap_roce_gid_table(device, port);
1078
Yuval Shaia24dc8312017-01-25 18:41:37 +02001079 if (!rdma_is_port_valid(device, port))
Matan Barak03db3a22015-07-30 18:33:26 +03001080 return;
1081
Jack Wang21d64542017-01-17 10:11:12 +01001082 table = device->cache.ports[port - rdma_start_port(device)].gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
1085 if (!tprops)
1086 return;
1087
1088 ret = ib_query_port(device, port, tprops);
1089 if (ret) {
Parav Panditaba25a3e2016-03-02 00:50:29 +05301090 pr_warn("ib_query_port failed (%d) for %s\n",
1091 ret, device->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 goto err;
1093 }
1094
1095 pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
1096 sizeof *pkey_cache->table, GFP_KERNEL);
1097 if (!pkey_cache)
1098 goto err;
1099
1100 pkey_cache->table_len = tprops->pkey_tbl_len;
1101
Matan Barak03db3a22015-07-30 18:33:26 +03001102 if (!use_roce_gid_table) {
1103 gid_cache = kmalloc(sizeof(*gid_cache) + tprops->gid_tbl_len *
1104 sizeof(*gid_cache->table), GFP_KERNEL);
1105 if (!gid_cache)
1106 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Matan Barak03db3a22015-07-30 18:33:26 +03001108 gid_cache->table_len = tprops->gid_tbl_len;
1109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 for (i = 0; i < pkey_cache->table_len; ++i) {
1112 ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
1113 if (ret) {
Parav Panditaba25a3e2016-03-02 00:50:29 +05301114 pr_warn("ib_query_pkey failed (%d) for %s (index %d)\n",
1115 ret, device->name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 goto err;
1117 }
1118 }
1119
Matan Barak03db3a22015-07-30 18:33:26 +03001120 if (!use_roce_gid_table) {
1121 for (i = 0; i < gid_cache->table_len; ++i) {
1122 ret = ib_query_gid(device, port, i,
Matan Barak55ee3ab2015-10-15 18:38:45 +03001123 gid_cache->table + i, NULL);
Matan Barak03db3a22015-07-30 18:33:26 +03001124 if (ret) {
Parav Panditaba25a3e2016-03-02 00:50:29 +05301125 pr_warn("ib_query_gid failed (%d) for %s (index %d)\n",
1126 ret, device->name, i);
Matan Barak03db3a22015-07-30 18:33:26 +03001127 goto err;
1128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 }
1131
1132 write_lock_irq(&device->cache.lock);
1133
Jack Wang21d64542017-01-17 10:11:12 +01001134 old_pkey_cache = device->cache.ports[port -
1135 rdma_start_port(device)].pkey;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Jack Wang21d64542017-01-17 10:11:12 +01001137 device->cache.ports[port - rdma_start_port(device)].pkey = pkey_cache;
Matan Barak03db3a22015-07-30 18:33:26 +03001138 if (!use_roce_gid_table) {
Matan Barak9c584f02015-10-28 16:52:40 +02001139 write_lock(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +03001140 for (i = 0; i < gid_cache->table_len; i++) {
1141 modify_gid(device, port, table, i, gid_cache->table + i,
1142 &zattr, false);
1143 }
Matan Barak9c584f02015-10-28 16:52:40 +02001144 write_unlock(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +03001145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Jack Wang21d64542017-01-17 10:11:12 +01001147 device->cache.ports[port - rdma_start_port(device)].lmc = tprops->lmc;
1148 device->cache.ports[port - rdma_start_port(device)].port_state =
Jack Wangaaaca122017-01-02 13:17:36 +01001149 tprops->state;
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -07001150
Daniel Jurgens883c71f2017-05-19 15:48:51 +03001151 device->cache.ports[port - rdma_start_port(device)].subnet_prefix =
1152 tprops->subnet_prefix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 write_unlock_irq(&device->cache.lock);
1154
Daniel Jurgensd291f1a2017-05-19 15:48:52 +03001155 if (enforce_security)
1156 ib_security_cache_change(device,
1157 port,
1158 tprops->subnet_prefix);
1159
Matan Barak03db3a22015-07-30 18:33:26 +03001160 kfree(gid_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 kfree(old_pkey_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 kfree(tprops);
1163 return;
1164
1165err:
1166 kfree(pkey_cache);
1167 kfree(gid_cache);
1168 kfree(tprops);
1169}
1170
David Howellsc4028952006-11-22 14:57:56 +00001171static void ib_cache_task(struct work_struct *_work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
David Howellsc4028952006-11-22 14:57:56 +00001173 struct ib_update_work *work =
1174 container_of(_work, struct ib_update_work, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Daniel Jurgensd291f1a2017-05-19 15:48:52 +03001176 ib_cache_update(work->device,
1177 work->port_num,
1178 work->enforce_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 kfree(work);
1180}
1181
1182static void ib_cache_event(struct ib_event_handler *handler,
1183 struct ib_event *event)
1184{
1185 struct ib_update_work *work;
1186
1187 if (event->event == IB_EVENT_PORT_ERR ||
1188 event->event == IB_EVENT_PORT_ACTIVE ||
1189 event->event == IB_EVENT_LID_CHANGE ||
1190 event->event == IB_EVENT_PKEY_CHANGE ||
Jack Morgensteinacaea9e2006-08-15 17:20:50 +03001191 event->event == IB_EVENT_SM_CHANGE ||
Or Gerlitz761d90e2011-06-15 14:39:29 +00001192 event->event == IB_EVENT_CLIENT_REREGISTER ||
1193 event->event == IB_EVENT_GID_CHANGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 work = kmalloc(sizeof *work, GFP_ATOMIC);
1195 if (work) {
David Howellsc4028952006-11-22 14:57:56 +00001196 INIT_WORK(&work->work, ib_cache_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 work->device = event->device;
1198 work->port_num = event->element.port_num;
Daniel Jurgensd291f1a2017-05-19 15:48:52 +03001199 if (event->event == IB_EVENT_PKEY_CHANGE ||
1200 event->event == IB_EVENT_GID_CHANGE)
1201 work->enforce_security = true;
1202 else
1203 work->enforce_security = false;
1204
Tejun Heof0626712010-10-19 15:24:36 +00001205 queue_work(ib_wq, &work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207 }
1208}
1209
Matan Barak03db3a22015-07-30 18:33:26 +03001210int ib_cache_setup_one(struct ib_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 int p;
Matan Barak03db3a22015-07-30 18:33:26 +03001213 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 rwlock_init(&device->cache.lock);
1216
Jack Wang21d64542017-01-17 10:11:12 +01001217 device->cache.ports =
1218 kzalloc(sizeof(*device->cache.ports) *
Ira Weiny0cf18d72015-05-13 20:02:55 -04001219 (rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL);
Leon Romanovskydcc98812017-08-17 15:50:36 +03001220 if (!device->cache.ports)
1221 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Matan Barak03db3a22015-07-30 18:33:26 +03001223 err = gid_table_setup_one(device);
Leon Romanovskydcc98812017-08-17 15:50:36 +03001224 if (err) {
1225 kfree(device->cache.ports);
1226 device->cache.ports = NULL;
1227 return err;
1228 }
Matan Barak03db3a22015-07-30 18:33:26 +03001229
Jason Gunthorpe55aeed02015-08-04 15:23:34 -06001230 for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
Daniel Jurgensd291f1a2017-05-19 15:48:52 +03001231 ib_cache_update(device, p + rdma_start_port(device), true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
1234 device, ib_cache_event);
Leon Romanovskydcc98812017-08-17 15:50:36 +03001235 ib_register_event_handler(&device->cache.event_handler);
Matan Barak03db3a22015-07-30 18:33:26 +03001236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Matan Barak03db3a22015-07-30 18:33:26 +03001239void ib_cache_release_one(struct ib_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 int p;
1242
Matan Barak03db3a22015-07-30 18:33:26 +03001243 /*
1244 * The release function frees all the cache elements.
1245 * This function should be called as part of freeing
1246 * all the device's resources when the cache could no
1247 * longer be accessed.
1248 */
Jack Wang21d64542017-01-17 10:11:12 +01001249 for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
1250 kfree(device->cache.ports[p].pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Matan Barak03db3a22015-07-30 18:33:26 +03001252 gid_table_release_one(device);
Jack Wang21d64542017-01-17 10:11:12 +01001253 kfree(device->cache.ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
Matan Barak03db3a22015-07-30 18:33:26 +03001256void ib_cache_cleanup_one(struct ib_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Matan Barak03db3a22015-07-30 18:33:26 +03001258 /* The cleanup function unregisters the event handler,
1259 * waits for all in-progress workqueue elements and cleans
1260 * up the GID cache. This function should be called after
1261 * the device was removed from the devices list and all
1262 * clients were removed, so the cache exists but is
1263 * non-functional and shouldn't be updated anymore.
1264 */
1265 ib_unregister_event_handler(&device->cache.event_handler);
1266 flush_workqueue(ib_wq);
1267 gid_table_cleanup_one(device);
1268}
1269
1270void __init ib_cache_setup(void)
1271{
1272 roce_gid_mgmt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
1275void __exit ib_cache_cleanup(void)
1276{
Matan Barak03db3a22015-07-30 18:33:26 +03001277 roce_gid_mgmt_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}