blob: 71a34bee453d8fa4b3f75748e6589cc8f3763633 [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
Matan Barak03db3a22015-07-30 18:33:26 +030062enum gid_attr_find_mask {
63 GID_ATTR_FIND_MASK_GID = 1UL << 0,
64 GID_ATTR_FIND_MASK_NETDEV = 1UL << 1,
65 GID_ATTR_FIND_MASK_DEFAULT = 1UL << 2,
Matan Barakb39ffa12015-12-23 14:56:47 +020066 GID_ATTR_FIND_MASK_GID_TYPE = 1UL << 3,
Matan Barak03db3a22015-07-30 18:33:26 +030067};
68
69enum gid_table_entry_props {
70 GID_TABLE_ENTRY_INVALID = 1UL << 0,
71 GID_TABLE_ENTRY_DEFAULT = 1UL << 1,
72};
73
Matan Barak03db3a22015-07-30 18:33:26 +030074struct ib_gid_table_entry {
Matan Barak03db3a22015-07-30 18:33:26 +030075 unsigned long props;
76 union ib_gid gid;
77 struct ib_gid_attr attr;
78 void *context;
79};
80
81struct ib_gid_table {
82 int sz;
83 /* In RoCE, adding a GID to the table requires:
84 * (a) Find if this GID is already exists.
85 * (b) Find a free space.
86 * (c) Write the new GID
87 *
88 * Delete requires different set of operations:
89 * (a) Find the GID
90 * (b) Delete it.
91 *
Matan Barak03db3a22015-07-30 18:33:26 +030092 **/
Parav Pandit598ff6b2018-04-01 15:08:21 +030093 /* Any writer to data_vec must hold this lock and the write side of
94 * rwlock. readers must hold only rwlock. All writers must be in a
95 * sleepable context.
Matan Barak9c584f02015-10-28 16:52:40 +020096 */
Parav Pandit598ff6b2018-04-01 15:08:21 +030097 struct mutex lock;
98 /* rwlock protects data_vec[ix]->props. */
Matan Barak9c584f02015-10-28 16:52:40 +020099 rwlock_t rwlock;
Matan Barak03db3a22015-07-30 18:33:26 +0300100 struct ib_gid_table_entry *data_vec;
101};
102
Matan Barakf3906bd2015-10-28 16:52:39 +0200103static void dispatch_gid_change_event(struct ib_device *ib_dev, u8 port)
104{
Parav Pandit34018572018-03-25 13:40:20 +0300105 struct ib_event event;
Matan Barakf3906bd2015-10-28 16:52:39 +0200106
Parav Pandit34018572018-03-25 13:40:20 +0300107 event.device = ib_dev;
108 event.element.port_num = port;
109 event.event = IB_EVENT_GID_CHANGE;
Matan Barakf3906bd2015-10-28 16:52:39 +0200110
Parav Pandit34018572018-03-25 13:40:20 +0300111 ib_dispatch_event(&event);
Matan Barakf3906bd2015-10-28 16:52:39 +0200112}
113
Matan Barakb39ffa12015-12-23 14:56:47 +0200114static const char * const gid_type_str[] = {
115 [IB_GID_TYPE_IB] = "IB/RoCE v1",
Matan Barak7766a992015-12-23 14:56:50 +0200116 [IB_GID_TYPE_ROCE_UDP_ENCAP] = "RoCE v2",
Matan Barakb39ffa12015-12-23 14:56:47 +0200117};
118
119const char *ib_cache_gid_type_str(enum ib_gid_type gid_type)
120{
121 if (gid_type < ARRAY_SIZE(gid_type_str) && gid_type_str[gid_type])
122 return gid_type_str[gid_type];
123
124 return "Invalid GID type";
125}
126EXPORT_SYMBOL(ib_cache_gid_type_str);
127
Parav Pandit25e62652018-05-22 20:33:45 +0300128/** rdma_is_zero_gid - Check if given GID is zero or not.
129 * @gid: GID to check
130 * Returns true if given GID is zero, returns false otherwise.
131 */
132bool rdma_is_zero_gid(const union ib_gid *gid)
133{
134 return !memcmp(gid, &zgid, sizeof(*gid));
135}
136EXPORT_SYMBOL(rdma_is_zero_gid);
137
Matan Barak045959d2015-12-23 14:56:55 +0200138int ib_cache_gid_parse_type_str(const char *buf)
139{
140 unsigned int i;
141 size_t len;
142 int err = -EINVAL;
143
144 len = strlen(buf);
145 if (len == 0)
146 return -EINVAL;
147
148 if (buf[len - 1] == '\n')
149 len--;
150
151 for (i = 0; i < ARRAY_SIZE(gid_type_str); ++i)
152 if (gid_type_str[i] && !strncmp(buf, gid_type_str[i], len) &&
153 len == strlen(gid_type_str[i])) {
154 err = i;
155 break;
156 }
157
158 return err;
159}
160EXPORT_SYMBOL(ib_cache_gid_parse_type_str);
161
Parav Pandit724631a2018-05-22 20:33:46 +0300162static struct ib_gid_table *rdma_gid_table(struct ib_device *device, u8 port)
163{
164 return device->cache.ports[port - rdma_start_port(device)].gid;
165}
166
Parav Pandit598ff6b2018-04-01 15:08:21 +0300167static void del_roce_gid(struct ib_device *device, u8 port_num,
168 struct ib_gid_table *table, int ix)
Matan Barak03db3a22015-07-30 18:33:26 +0300169{
Parav Pandit598ff6b2018-04-01 15:08:21 +0300170 pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
171 device->name, port_num, ix,
172 table->data_vec[ix].gid.raw);
173
174 if (rdma_cap_roce_gid_table(device, port_num))
Parav Pandit414448d2018-04-01 15:08:24 +0300175 device->del_gid(&table->data_vec[ix].attr,
Parav Pandit598ff6b2018-04-01 15:08:21 +0300176 &table->data_vec[ix].context);
177 dev_put(table->data_vec[ix].attr.ndev);
178}
179
180static int add_roce_gid(struct ib_gid_table *table,
181 const union ib_gid *gid,
182 const struct ib_gid_attr *attr)
183{
184 struct ib_gid_table_entry *entry;
185 int ix = attr->index;
Matan Barak03db3a22015-07-30 18:33:26 +0300186 int ret = 0;
Matan Barak03db3a22015-07-30 18:33:26 +0300187
Parav Pandit598ff6b2018-04-01 15:08:21 +0300188 if (!attr->ndev) {
189 pr_err("%s NULL netdev device=%s port=%d index=%d\n",
190 __func__, attr->device->name, attr->port_num,
191 attr->index);
192 return -EINVAL;
Matan Barak03db3a22015-07-30 18:33:26 +0300193 }
194
Parav Pandit598ff6b2018-04-01 15:08:21 +0300195 entry = &table->data_vec[ix];
196 if ((entry->props & GID_TABLE_ENTRY_INVALID) == 0) {
197 WARN(1, "GID table corruption device=%s port=%d index=%d\n",
198 attr->device->name, attr->port_num,
199 attr->index);
200 return -EINVAL;
Matan Barak03db3a22015-07-30 18:33:26 +0300201 }
Aviv Heller8e7876462016-06-04 15:15:21 +0300202
Parav Pandit598ff6b2018-04-01 15:08:21 +0300203 if (rdma_cap_roce_gid_table(attr->device, attr->port_num)) {
Parav Pandit414448d2018-04-01 15:08:24 +0300204 ret = attr->device->add_gid(gid, attr, &entry->context);
Parav Pandit598ff6b2018-04-01 15:08:21 +0300205 if (ret) {
206 pr_err("%s GID add failed device=%s port=%d index=%d\n",
207 __func__, attr->device->name, attr->port_num,
208 attr->index);
209 goto add_err;
210 }
Aviv Heller8e7876462016-06-04 15:15:21 +0300211 }
Parav Pandit598ff6b2018-04-01 15:08:21 +0300212 dev_hold(attr->ndev);
Matan Barak03db3a22015-07-30 18:33:26 +0300213
Parav Pandit598ff6b2018-04-01 15:08:21 +0300214add_err:
215 if (!ret)
216 pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
217 attr->device->name, attr->port_num, ix, gid->raw);
Matan Barak03db3a22015-07-30 18:33:26 +0300218 return ret;
219}
220
Parav Pandit598ff6b2018-04-01 15:08:21 +0300221/**
222 * add_modify_gid - Add or modify GID table entry
223 *
224 * @table: GID table in which GID to be added or modified
225 * @gid: GID content
226 * @attr: Attributes of the GID
227 *
228 * Returns 0 on success or appropriate error code. It accepts zero
229 * GID addition for non RoCE ports for HCA's who report them as valid
230 * GID. However such zero GIDs are not added to the cache.
231 */
232static int add_modify_gid(struct ib_gid_table *table,
233 const union ib_gid *gid,
234 const struct ib_gid_attr *attr)
235{
236 int ret;
237
238 if (rdma_protocol_roce(attr->device, attr->port_num)) {
239 ret = add_roce_gid(table, gid, attr);
240 if (ret)
241 return ret;
242 } else {
243 /*
244 * Some HCA's report multiple GID entries with only one
245 * valid GID, but remaining as zero GID.
246 * So ignore such behavior for IB link layer and don't
247 * fail the call, but don't add such entry to GID cache.
248 */
Parav Pandit25e62652018-05-22 20:33:45 +0300249 if (rdma_is_zero_gid(gid))
Parav Pandit598ff6b2018-04-01 15:08:21 +0300250 return 0;
251 }
252
253 lockdep_assert_held(&table->lock);
254 memcpy(&table->data_vec[attr->index].gid, gid, sizeof(*gid));
255 memcpy(&table->data_vec[attr->index].attr, attr, sizeof(*attr));
256
257 write_lock_irq(&table->rwlock);
258 table->data_vec[attr->index].props &= ~GID_TABLE_ENTRY_INVALID;
259 write_unlock_irq(&table->rwlock);
260 return 0;
Matan Barak03db3a22015-07-30 18:33:26 +0300261}
262
Parav Pandit598ff6b2018-04-01 15:08:21 +0300263/**
264 * del_gid - Delete GID table entry
265 *
266 * @ib_dev: IB device whose GID entry to be deleted
267 * @port: Port number of the IB device
268 * @table: GID table of the IB device for a port
269 * @ix: GID entry index to delete
270 *
271 */
272static void del_gid(struct ib_device *ib_dev, u8 port,
273 struct ib_gid_table *table, int ix)
274{
275 lockdep_assert_held(&table->lock);
276 write_lock_irq(&table->rwlock);
277 table->data_vec[ix].props |= GID_TABLE_ENTRY_INVALID;
278 write_unlock_irq(&table->rwlock);
279
280 if (rdma_protocol_roce(ib_dev, port))
281 del_roce_gid(ib_dev, port, table, ix);
Parav Pandit25e62652018-05-22 20:33:45 +0300282 memset(&table->data_vec[ix].gid, 0, sizeof(table->data_vec[ix].gid));
Parav Pandit598ff6b2018-04-01 15:08:21 +0300283 memset(&table->data_vec[ix].attr, 0, sizeof(table->data_vec[ix].attr));
284 table->data_vec[ix].context = NULL;
Matan Barak03db3a22015-07-30 18:33:26 +0300285}
286
Parav Pandit598ff6b2018-04-01 15:08:21 +0300287/* rwlock should be read locked, or lock should be held */
Matan Barak03db3a22015-07-30 18:33:26 +0300288static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,
289 const struct ib_gid_attr *val, bool default_gid,
Matan Barakcee3c4d2015-10-28 16:52:41 +0200290 unsigned long mask, int *pempty)
Matan Barak03db3a22015-07-30 18:33:26 +0300291{
Matan Barakcee3c4d2015-10-28 16:52:41 +0200292 int i = 0;
293 int found = -1;
294 int empty = pempty ? -1 : 0;
Matan Barak03db3a22015-07-30 18:33:26 +0300295
Matan Barakcee3c4d2015-10-28 16:52:41 +0200296 while (i < table->sz && (found < 0 || empty < 0)) {
297 struct ib_gid_table_entry *data = &table->data_vec[i];
298 struct ib_gid_attr *attr = &data->attr;
299 int curr_index = i;
Matan Barak03db3a22015-07-30 18:33:26 +0300300
Matan Barakcee3c4d2015-10-28 16:52:41 +0200301 i++;
Matan Barak03db3a22015-07-30 18:33:26 +0300302
Parav Pandit598ff6b2018-04-01 15:08:21 +0300303 /* find_gid() is used during GID addition where it is expected
304 * to return a free entry slot which is not duplicate.
305 * Free entry slot is requested and returned if pempty is set,
306 * so lookup free slot only if requested.
307 */
308 if (pempty && empty < 0) {
Parav Pandita66ed142018-04-23 16:58:17 +0300309 if (data->props & GID_TABLE_ENTRY_INVALID &&
310 (default_gid ==
311 !!(data->props & GID_TABLE_ENTRY_DEFAULT))) {
312 /*
313 * Found an invalid (free) entry; allocate it.
314 * If default GID is requested, then our
315 * found slot must be one of the DEFAULT
316 * reserved slots or we fail.
317 * This ensures that only DEFAULT reserved
318 * slots are used for default property GIDs.
319 */
320 empty = curr_index;
Parav Pandit598ff6b2018-04-01 15:08:21 +0300321 }
322 }
323
324 /*
325 * Additionally find_gid() is used to find valid entry during
326 * lookup operation, where validity needs to be checked. So
327 * find the empty entry first to continue to search for a free
328 * slot and ignore its INVALID flag.
329 */
Matan Barakcee3c4d2015-10-28 16:52:41 +0200330 if (data->props & GID_TABLE_ENTRY_INVALID)
331 continue;
332
Matan Barakcee3c4d2015-10-28 16:52:41 +0200333 if (found >= 0)
Matan Barak9c584f02015-10-28 16:52:40 +0200334 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300335
Matan Barakb39ffa12015-12-23 14:56:47 +0200336 if (mask & GID_ATTR_FIND_MASK_GID_TYPE &&
337 attr->gid_type != val->gid_type)
338 continue;
339
Matan Barak03db3a22015-07-30 18:33:26 +0300340 if (mask & GID_ATTR_FIND_MASK_GID &&
Matan Barakcee3c4d2015-10-28 16:52:41 +0200341 memcmp(gid, &data->gid, sizeof(*gid)))
Matan Barak9c584f02015-10-28 16:52:40 +0200342 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300343
344 if (mask & GID_ATTR_FIND_MASK_NETDEV &&
345 attr->ndev != val->ndev)
Matan Barak9c584f02015-10-28 16:52:40 +0200346 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300347
348 if (mask & GID_ATTR_FIND_MASK_DEFAULT &&
Matan Barakcee3c4d2015-10-28 16:52:41 +0200349 !!(data->props & GID_TABLE_ENTRY_DEFAULT) !=
Matan Barak03db3a22015-07-30 18:33:26 +0300350 default_gid)
Matan Barak9c584f02015-10-28 16:52:40 +0200351 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300352
Matan Barakcee3c4d2015-10-28 16:52:41 +0200353 found = curr_index;
Matan Barak03db3a22015-07-30 18:33:26 +0300354 }
355
Matan Barakcee3c4d2015-10-28 16:52:41 +0200356 if (pempty)
357 *pempty = empty;
358
359 return found;
Matan Barak03db3a22015-07-30 18:33:26 +0300360}
361
362static void make_default_gid(struct net_device *dev, union ib_gid *gid)
363{
364 gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
365 addrconf_ifid_eui48(&gid->raw[8], dev);
366}
367
Parav Pandit598ff6b2018-04-01 15:08:21 +0300368static int __ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
369 union ib_gid *gid, struct ib_gid_attr *attr,
370 unsigned long mask, bool default_gid)
Matan Barak03db3a22015-07-30 18:33:26 +0300371{
Matan Barak03db3a22015-07-30 18:33:26 +0300372 struct ib_gid_table *table;
Matan Barak03db3a22015-07-30 18:33:26 +0300373 int ret = 0;
Matan Barakcee3c4d2015-10-28 16:52:41 +0200374 int empty;
Parav Pandit598ff6b2018-04-01 15:08:21 +0300375 int ix;
376
377 /* Do not allow adding zero GID in support of
378 * IB spec version 1.3 section 4.1.1 point (6) and
379 * section 12.7.10 and section 12.7.20
380 */
Parav Pandit25e62652018-05-22 20:33:45 +0300381 if (rdma_is_zero_gid(gid))
Parav Pandit598ff6b2018-04-01 15:08:21 +0300382 return -EINVAL;
Matan Barak03db3a22015-07-30 18:33:26 +0300383
Parav Pandit724631a2018-05-22 20:33:46 +0300384 table = rdma_gid_table(ib_dev, port);
Matan Barak03db3a22015-07-30 18:33:26 +0300385
Parav Pandit598ff6b2018-04-01 15:08:21 +0300386 mutex_lock(&table->lock);
387
388 ix = find_gid(table, gid, attr, default_gid, mask, &empty);
389 if (ix >= 0)
390 goto out_unlock;
391
392 if (empty < 0) {
393 ret = -ENOSPC;
394 goto out_unlock;
395 }
396 attr->device = ib_dev;
397 attr->index = empty;
398 attr->port_num = port;
399 ret = add_modify_gid(table, gid, attr);
400 if (!ret)
401 dispatch_gid_change_event(ib_dev, port);
402
403out_unlock:
404 mutex_unlock(&table->lock);
405 if (ret)
406 pr_warn("%s: unable to add gid %pI6 error=%d\n",
407 __func__, gid->raw, ret);
408 return ret;
409}
410
411int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
412 union ib_gid *gid, struct ib_gid_attr *attr)
413{
414 struct net_device *idev;
415 unsigned long mask;
416 int ret;
Matan Barak03db3a22015-07-30 18:33:26 +0300417
418 if (ib_dev->get_netdev) {
419 idev = ib_dev->get_netdev(ib_dev, port);
420 if (idev && attr->ndev != idev) {
421 union ib_gid default_gid;
422
423 /* Adding default GIDs in not permitted */
424 make_default_gid(idev, &default_gid);
425 if (!memcmp(gid, &default_gid, sizeof(*gid))) {
426 dev_put(idev);
427 return -EPERM;
428 }
429 }
430 if (idev)
431 dev_put(idev);
432 }
433
Parav Pandit598ff6b2018-04-01 15:08:21 +0300434 mask = GID_ATTR_FIND_MASK_GID |
435 GID_ATTR_FIND_MASK_GID_TYPE |
436 GID_ATTR_FIND_MASK_NETDEV;
Matan Barak03db3a22015-07-30 18:33:26 +0300437
Parav Pandit598ff6b2018-04-01 15:08:21 +0300438 ret = __ib_cache_gid_add(ib_dev, port, gid, attr, mask, false);
Matan Barak03db3a22015-07-30 18:33:26 +0300439 return ret;
440}
441
Parav Pandit22c01ee2018-04-23 16:58:18 +0300442static int
443_ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
444 union ib_gid *gid, struct ib_gid_attr *attr,
Parav Panditdc5640f2018-04-23 16:58:19 +0300445 unsigned long mask, bool default_gid)
Matan Barak03db3a22015-07-30 18:33:26 +0300446{
Matan Barak03db3a22015-07-30 18:33:26 +0300447 struct ib_gid_table *table;
Parav Pandit598ff6b2018-04-01 15:08:21 +0300448 int ret = 0;
Matan Barak03db3a22015-07-30 18:33:26 +0300449 int ix;
450
Parav Pandit724631a2018-05-22 20:33:46 +0300451 table = rdma_gid_table(ib_dev, port);
Matan Barak03db3a22015-07-30 18:33:26 +0300452
453 mutex_lock(&table->lock);
454
Parav Panditdc5640f2018-04-23 16:58:19 +0300455 ix = find_gid(table, gid, attr, default_gid, mask, NULL);
Parav Pandit598ff6b2018-04-01 15:08:21 +0300456 if (ix < 0) {
457 ret = -EINVAL;
Matan Barak03db3a22015-07-30 18:33:26 +0300458 goto out_unlock;
Parav Pandit598ff6b2018-04-01 15:08:21 +0300459 }
Matan Barak03db3a22015-07-30 18:33:26 +0300460
Parav Pandit598ff6b2018-04-01 15:08:21 +0300461 del_gid(ib_dev, port, table, ix);
462 dispatch_gid_change_event(ib_dev, port);
Matan Barak03db3a22015-07-30 18:33:26 +0300463
464out_unlock:
465 mutex_unlock(&table->lock);
Parav Pandit598ff6b2018-04-01 15:08:21 +0300466 if (ret)
467 pr_debug("%s: can't delete gid %pI6 error=%d\n",
468 __func__, gid->raw, ret);
469 return ret;
Matan Barak03db3a22015-07-30 18:33:26 +0300470}
471
Parav Pandit22c01ee2018-04-23 16:58:18 +0300472int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
473 union ib_gid *gid, struct ib_gid_attr *attr)
474{
Parav Panditdc5640f2018-04-23 16:58:19 +0300475 unsigned long mask = GID_ATTR_FIND_MASK_GID |
476 GID_ATTR_FIND_MASK_GID_TYPE |
477 GID_ATTR_FIND_MASK_DEFAULT |
478 GID_ATTR_FIND_MASK_NETDEV;
479
480 return _ib_cache_gid_del(ib_dev, port, gid, attr, mask, false);
Parav Pandit22c01ee2018-04-23 16:58:18 +0300481}
482
Matan Barak03db3a22015-07-30 18:33:26 +0300483int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
484 struct net_device *ndev)
485{
Matan Barak03db3a22015-07-30 18:33:26 +0300486 struct ib_gid_table *table;
487 int ix;
Matan Barak9c584f02015-10-28 16:52:40 +0200488 bool deleted = false;
Matan Barak03db3a22015-07-30 18:33:26 +0300489
Parav Pandit724631a2018-05-22 20:33:46 +0300490 table = rdma_gid_table(ib_dev, port);
Matan Barak03db3a22015-07-30 18:33:26 +0300491
492 mutex_lock(&table->lock);
493
Parav Pandit598ff6b2018-04-01 15:08:21 +0300494 for (ix = 0; ix < table->sz; ix++) {
495 if (table->data_vec[ix].attr.ndev == ndev) {
496 del_gid(ib_dev, port, table, ix);
497 deleted = true;
498 }
499 }
Matan Barak03db3a22015-07-30 18:33:26 +0300500
501 mutex_unlock(&table->lock);
Matan Barak9c584f02015-10-28 16:52:40 +0200502
503 if (deleted)
504 dispatch_gid_change_event(ib_dev, port);
505
Matan Barak03db3a22015-07-30 18:33:26 +0300506 return 0;
507}
508
509static int __ib_cache_gid_get(struct ib_device *ib_dev, u8 port, int index,
510 union ib_gid *gid, struct ib_gid_attr *attr)
511{
Matan Barak03db3a22015-07-30 18:33:26 +0300512 struct ib_gid_table *table;
Matan Barak03db3a22015-07-30 18:33:26 +0300513
Parav Pandit724631a2018-05-22 20:33:46 +0300514 table = rdma_gid_table(ib_dev, port);
Matan Barak03db3a22015-07-30 18:33:26 +0300515
516 if (index < 0 || index >= table->sz)
517 return -EINVAL;
518
Matan Barak9c584f02015-10-28 16:52:40 +0200519 if (table->data_vec[index].props & GID_TABLE_ENTRY_INVALID)
Parav Pandita840c932018-05-27 14:49:16 +0300520 return -EINVAL;
Matan Barak03db3a22015-07-30 18:33:26 +0300521
522 memcpy(gid, &table->data_vec[index].gid, sizeof(*gid));
523 if (attr) {
524 memcpy(attr, &table->data_vec[index].attr, sizeof(*attr));
525 if (attr->ndev)
526 dev_hold(attr->ndev);
527 }
528
Matan Barak03db3a22015-07-30 18:33:26 +0300529 return 0;
530}
531
532static int _ib_cache_gid_table_find(struct ib_device *ib_dev,
533 const union ib_gid *gid,
534 const struct ib_gid_attr *val,
535 unsigned long mask,
536 u8 *port, u16 *index)
537{
Matan Barak03db3a22015-07-30 18:33:26 +0300538 struct ib_gid_table *table;
539 u8 p;
540 int local_index;
Matan Barak9c584f02015-10-28 16:52:40 +0200541 unsigned long flags;
Matan Barak03db3a22015-07-30 18:33:26 +0300542
543 for (p = 0; p < ib_dev->phys_port_cnt; p++) {
Jack Wang21d64542017-01-17 10:11:12 +0100544 table = ib_dev->cache.ports[p].gid;
Matan Barak9c584f02015-10-28 16:52:40 +0200545 read_lock_irqsave(&table->rwlock, flags);
Matan Barakcee3c4d2015-10-28 16:52:41 +0200546 local_index = find_gid(table, gid, val, false, mask, NULL);
Matan Barak03db3a22015-07-30 18:33:26 +0300547 if (local_index >= 0) {
548 if (index)
549 *index = local_index;
550 if (port)
551 *port = p + rdma_start_port(ib_dev);
Matan Barak9c584f02015-10-28 16:52:40 +0200552 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak03db3a22015-07-30 18:33:26 +0300553 return 0;
554 }
Matan Barak9c584f02015-10-28 16:52:40 +0200555 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak03db3a22015-07-30 18:33:26 +0300556 }
557
558 return -ENOENT;
559}
560
561static int ib_cache_gid_find(struct ib_device *ib_dev,
562 const union ib_gid *gid,
Matan Barakb39ffa12015-12-23 14:56:47 +0200563 enum ib_gid_type gid_type,
Matan Barak03db3a22015-07-30 18:33:26 +0300564 struct net_device *ndev, u8 *port,
565 u16 *index)
566{
Matan Barakb39ffa12015-12-23 14:56:47 +0200567 unsigned long mask = GID_ATTR_FIND_MASK_GID |
568 GID_ATTR_FIND_MASK_GID_TYPE;
569 struct ib_gid_attr gid_attr_val = {.ndev = ndev, .gid_type = gid_type};
Matan Barak03db3a22015-07-30 18:33:26 +0300570
571 if (ndev)
572 mask |= GID_ATTR_FIND_MASK_NETDEV;
573
574 return _ib_cache_gid_table_find(ib_dev, gid, &gid_attr_val,
575 mask, port, index);
576}
577
Parav Pandit6612b492018-03-13 16:06:11 +0200578/**
579 * ib_find_cached_gid_by_port - Returns the GID table index where a specified
580 * GID value occurs. It searches for the specified GID value in the local
581 * software cache.
582 * @device: The device to query.
583 * @gid: The GID value to search for.
584 * @gid_type: The GID type to search for.
585 * @port_num: The port number of the device where the GID value should be
586 * searched.
587 * @ndev: In RoCE, the net device of the device. Null means ignore.
588 * @index: The index into the cached GID table where the GID was found. This
589 * parameter may be NULL.
590 */
Matan Barakd300ec52015-10-15 18:38:46 +0300591int ib_find_cached_gid_by_port(struct ib_device *ib_dev,
592 const union ib_gid *gid,
Matan Barakb39ffa12015-12-23 14:56:47 +0200593 enum ib_gid_type gid_type,
Matan Barakd300ec52015-10-15 18:38:46 +0300594 u8 port, struct net_device *ndev,
595 u16 *index)
Matan Barak03db3a22015-07-30 18:33:26 +0300596{
597 int local_index;
Matan Barak03db3a22015-07-30 18:33:26 +0300598 struct ib_gid_table *table;
Matan Barakb39ffa12015-12-23 14:56:47 +0200599 unsigned long mask = GID_ATTR_FIND_MASK_GID |
600 GID_ATTR_FIND_MASK_GID_TYPE;
601 struct ib_gid_attr val = {.ndev = ndev, .gid_type = gid_type};
Matan Barak9c584f02015-10-28 16:52:40 +0200602 unsigned long flags;
Matan Barak03db3a22015-07-30 18:33:26 +0300603
Yuval Shaia24dc8312017-01-25 18:41:37 +0200604 if (!rdma_is_port_valid(ib_dev, port))
Matan Barak03db3a22015-07-30 18:33:26 +0300605 return -ENOENT;
606
Parav Pandit724631a2018-05-22 20:33:46 +0300607 table = rdma_gid_table(ib_dev, port);
Matan Barak03db3a22015-07-30 18:33:26 +0300608
609 if (ndev)
610 mask |= GID_ATTR_FIND_MASK_NETDEV;
611
Matan Barak9c584f02015-10-28 16:52:40 +0200612 read_lock_irqsave(&table->rwlock, flags);
Matan Barakcee3c4d2015-10-28 16:52:41 +0200613 local_index = find_gid(table, gid, &val, false, mask, NULL);
Matan Barak03db3a22015-07-30 18:33:26 +0300614 if (local_index >= 0) {
615 if (index)
616 *index = local_index;
Matan Barak9c584f02015-10-28 16:52:40 +0200617 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak03db3a22015-07-30 18:33:26 +0300618 return 0;
619 }
620
Matan Barak9c584f02015-10-28 16:52:40 +0200621 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak03db3a22015-07-30 18:33:26 +0300622 return -ENOENT;
623}
Matan Barakd300ec52015-10-15 18:38:46 +0300624EXPORT_SYMBOL(ib_find_cached_gid_by_port);
Matan Barak03db3a22015-07-30 18:33:26 +0300625
Matan Barak99b27e32015-10-15 18:38:50 +0300626/**
Parav Pandit6612b492018-03-13 16:06:11 +0200627 * ib_cache_gid_find_by_filter - Returns the GID table index where a specified
Matan Barak99b27e32015-10-15 18:38:50 +0300628 * GID value occurs
629 * @device: The device to query.
630 * @gid: The GID value to search for.
631 * @port_num: The port number of the device where the GID value could be
632 * searched.
633 * @filter: The filter function is executed on any matching GID in the table.
634 * If the filter function returns true, the corresponding index is returned,
635 * otherwise, we continue searching the GID table. It's guaranteed that
636 * while filter is executed, ndev field is valid and the structure won't
637 * change. filter is executed in an atomic context. filter must not be NULL.
Parav Pandit6612b492018-03-13 16:06:11 +0200638 * @index: The index into the cached GID table where the GID was found. This
Matan Barak99b27e32015-10-15 18:38:50 +0300639 * parameter may be NULL.
640 *
641 * ib_cache_gid_find_by_filter() searches for the specified GID value
642 * of which the filter function returns true in the port's GID table.
643 * This function is only supported on RoCE ports.
644 *
645 */
646static int ib_cache_gid_find_by_filter(struct ib_device *ib_dev,
647 const union ib_gid *gid,
648 u8 port,
649 bool (*filter)(const union ib_gid *,
650 const struct ib_gid_attr *,
651 void *),
652 void *context,
653 u16 *index)
654{
Matan Barak99b27e32015-10-15 18:38:50 +0300655 struct ib_gid_table *table;
656 unsigned int i;
Matan Barak9c584f02015-10-28 16:52:40 +0200657 unsigned long flags;
Matan Barak99b27e32015-10-15 18:38:50 +0300658 bool found = false;
659
Matan Barak99b27e32015-10-15 18:38:50 +0300660
Yuval Shaia24dc8312017-01-25 18:41:37 +0200661 if (!rdma_is_port_valid(ib_dev, port) ||
Matan Barak99b27e32015-10-15 18:38:50 +0300662 !rdma_protocol_roce(ib_dev, port))
663 return -EPROTONOSUPPORT;
664
Parav Pandit724631a2018-05-22 20:33:46 +0300665 table = rdma_gid_table(ib_dev, port);
Matan Barak99b27e32015-10-15 18:38:50 +0300666
Matan Barak9c584f02015-10-28 16:52:40 +0200667 read_lock_irqsave(&table->rwlock, flags);
Matan Barak99b27e32015-10-15 18:38:50 +0300668 for (i = 0; i < table->sz; i++) {
669 struct ib_gid_attr attr;
Matan Barak99b27e32015-10-15 18:38:50 +0300670
Matan Barak99b27e32015-10-15 18:38:50 +0300671 if (table->data_vec[i].props & GID_TABLE_ENTRY_INVALID)
Parav Pandit151ed9d2017-11-14 14:52:05 +0200672 continue;
Matan Barak99b27e32015-10-15 18:38:50 +0300673
674 if (memcmp(gid, &table->data_vec[i].gid, sizeof(*gid)))
Parav Pandit151ed9d2017-11-14 14:52:05 +0200675 continue;
Matan Barak99b27e32015-10-15 18:38:50 +0300676
677 memcpy(&attr, &table->data_vec[i].attr, sizeof(attr));
678
Parav Pandit151ed9d2017-11-14 14:52:05 +0200679 if (filter(gid, &attr, context)) {
Matan Barak99b27e32015-10-15 18:38:50 +0300680 found = true;
Parav Pandit151ed9d2017-11-14 14:52:05 +0200681 if (index)
682 *index = i;
Matan Barak99b27e32015-10-15 18:38:50 +0300683 break;
Parav Pandit151ed9d2017-11-14 14:52:05 +0200684 }
Matan Barak99b27e32015-10-15 18:38:50 +0300685 }
Matan Barak9c584f02015-10-28 16:52:40 +0200686 read_unlock_irqrestore(&table->rwlock, flags);
Matan Barak99b27e32015-10-15 18:38:50 +0300687
688 if (!found)
689 return -ENOENT;
Matan Barak99b27e32015-10-15 18:38:50 +0300690 return 0;
691}
692
Matan Barak03db3a22015-07-30 18:33:26 +0300693static struct ib_gid_table *alloc_gid_table(int sz)
694{
Matan Barak03db3a22015-07-30 18:33:26 +0300695 struct ib_gid_table *table =
696 kzalloc(sizeof(struct ib_gid_table), GFP_KERNEL);
Parav Pandit598ff6b2018-04-01 15:08:21 +0300697 int i;
Matan Barak9c584f02015-10-28 16:52:40 +0200698
Matan Barak03db3a22015-07-30 18:33:26 +0300699 if (!table)
700 return NULL;
701
702 table->data_vec = kcalloc(sz, sizeof(*table->data_vec), GFP_KERNEL);
703 if (!table->data_vec)
704 goto err_free_table;
705
706 mutex_init(&table->lock);
707
708 table->sz = sz;
Matan Barak9c584f02015-10-28 16:52:40 +0200709 rwlock_init(&table->rwlock);
Matan Barak03db3a22015-07-30 18:33:26 +0300710
Parav Pandit598ff6b2018-04-01 15:08:21 +0300711 /* Mark all entries as invalid so that allocator can allocate
712 * one of the invalid (free) entry.
713 */
714 for (i = 0; i < sz; i++)
715 table->data_vec[i].props |= GID_TABLE_ENTRY_INVALID;
Matan Barak03db3a22015-07-30 18:33:26 +0300716 return table;
717
718err_free_table:
719 kfree(table);
720 return NULL;
721}
722
723static void release_gid_table(struct ib_gid_table *table)
724{
725 if (table) {
726 kfree(table->data_vec);
727 kfree(table);
728 }
729}
730
731static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port,
732 struct ib_gid_table *table)
733{
734 int i;
Matan Barak9c584f02015-10-28 16:52:40 +0200735 bool deleted = false;
Matan Barak03db3a22015-07-30 18:33:26 +0300736
737 if (!table)
738 return;
739
Parav Pandit598ff6b2018-04-01 15:08:21 +0300740 mutex_lock(&table->lock);
Matan Barak03db3a22015-07-30 18:33:26 +0300741 for (i = 0; i < table->sz; ++i) {
Parav Pandit25e62652018-05-22 20:33:45 +0300742 if (!rdma_is_zero_gid(&table->data_vec[i].gid)) {
Parav Pandit598ff6b2018-04-01 15:08:21 +0300743 del_gid(ib_dev, port, table, i);
744 deleted = true;
745 }
Matan Barak03db3a22015-07-30 18:33:26 +0300746 }
Parav Pandit598ff6b2018-04-01 15:08:21 +0300747 mutex_unlock(&table->lock);
Matan Barak9c584f02015-10-28 16:52:40 +0200748
749 if (deleted)
750 dispatch_gid_change_event(ib_dev, port);
Matan Barak03db3a22015-07-30 18:33:26 +0300751}
752
753void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
754 struct net_device *ndev,
Matan Barakb39ffa12015-12-23 14:56:47 +0200755 unsigned long gid_type_mask,
Matan Barak03db3a22015-07-30 18:33:26 +0300756 enum ib_cache_gid_default_mode mode)
757{
Parav Panditdc5640f2018-04-23 16:58:19 +0300758 union ib_gid gid = { };
Matan Barak03db3a22015-07-30 18:33:26 +0300759 struct ib_gid_attr gid_attr;
760 struct ib_gid_table *table;
Matan Barakb39ffa12015-12-23 14:56:47 +0200761 unsigned int gid_type;
Parav Pandit598ff6b2018-04-01 15:08:21 +0300762 unsigned long mask;
Matan Barak03db3a22015-07-30 18:33:26 +0300763
Parav Pandit724631a2018-05-22 20:33:46 +0300764 table = rdma_gid_table(ib_dev, port);
Matan Barak03db3a22015-07-30 18:33:26 +0300765
Parav Panditdc5640f2018-04-23 16:58:19 +0300766 mask = GID_ATTR_FIND_MASK_GID_TYPE |
767 GID_ATTR_FIND_MASK_DEFAULT |
768 GID_ATTR_FIND_MASK_NETDEV;
Matan Barak03db3a22015-07-30 18:33:26 +0300769 memset(&gid_attr, 0, sizeof(gid_attr));
770 gid_attr.ndev = ndev;
771
Matan Barakb39ffa12015-12-23 14:56:47 +0200772 for (gid_type = 0; gid_type < IB_GID_TYPE_SIZE; ++gid_type) {
Matan Barakb39ffa12015-12-23 14:56:47 +0200773 if (1UL << gid_type & ~gid_type_mask)
774 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300775
Matan Barakb39ffa12015-12-23 14:56:47 +0200776 gid_attr.gid_type = gid_type;
Matan Barak03db3a22015-07-30 18:33:26 +0300777
Matan Barakb39ffa12015-12-23 14:56:47 +0200778 if (mode == IB_CACHE_GID_DEFAULT_MODE_SET) {
Parav Panditdc5640f2018-04-23 16:58:19 +0300779 make_default_gid(ndev, &gid);
Parav Pandit598ff6b2018-04-01 15:08:21 +0300780 __ib_cache_gid_add(ib_dev, port, &gid,
781 &gid_attr, mask, true);
782 } else if (mode == IB_CACHE_GID_DEFAULT_MODE_DELETE) {
Parav Panditdc5640f2018-04-23 16:58:19 +0300783 _ib_cache_gid_del(ib_dev, port, &gid,
784 &gid_attr, mask, true);
Matan Barak9c584f02015-10-28 16:52:40 +0200785 }
Matan Barakb39ffa12015-12-23 14:56:47 +0200786 }
Matan Barak03db3a22015-07-30 18:33:26 +0300787}
788
Parav Pandit25a1cd32018-05-02 13:12:55 +0300789static void gid_table_reserve_default(struct ib_device *ib_dev, u8 port,
790 struct ib_gid_table *table)
Matan Barak03db3a22015-07-30 18:33:26 +0300791{
Matan Barakb39ffa12015-12-23 14:56:47 +0200792 unsigned int i;
793 unsigned long roce_gid_type_mask;
794 unsigned int num_default_gids;
795 unsigned int current_gid = 0;
796
797 roce_gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
798 num_default_gids = hweight_long(roce_gid_type_mask);
799 for (i = 0; i < num_default_gids && i < table->sz; i++) {
Parav Pandit25a1cd32018-05-02 13:12:55 +0300800 struct ib_gid_table_entry *entry = &table->data_vec[i];
Matan Barak03db3a22015-07-30 18:33:26 +0300801
802 entry->props |= GID_TABLE_ENTRY_DEFAULT;
Matan Barakb39ffa12015-12-23 14:56:47 +0200803 current_gid = find_next_bit(&roce_gid_type_mask,
804 BITS_PER_LONG,
805 current_gid);
806 entry->attr.gid_type = current_gid++;
Matan Barak03db3a22015-07-30 18:33:26 +0300807 }
Matan Barak03db3a22015-07-30 18:33:26 +0300808}
809
Matan Barak03db3a22015-07-30 18:33:26 +0300810
811static void gid_table_release_one(struct ib_device *ib_dev)
812{
Jack Wang21d64542017-01-17 10:11:12 +0100813 struct ib_gid_table *table;
Matan Barak03db3a22015-07-30 18:33:26 +0300814 u8 port;
815
Jack Wang21d64542017-01-17 10:11:12 +0100816 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
817 table = ib_dev->cache.ports[port].gid;
818 release_gid_table(table);
819 ib_dev->cache.ports[port].gid = NULL;
820 }
Matan Barak03db3a22015-07-30 18:33:26 +0300821}
822
Parav Panditbe0e8f32018-05-02 13:12:56 +0300823static int _gid_table_setup_one(struct ib_device *ib_dev)
824{
825 u8 port;
826 struct ib_gid_table *table;
827
828 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
829 u8 rdma_port = port + rdma_start_port(ib_dev);
830
831 table = alloc_gid_table(
832 ib_dev->port_immutable[rdma_port].gid_tbl_len);
833 if (!table)
834 goto rollback_table_setup;
835
836 gid_table_reserve_default(ib_dev, rdma_port, table);
837 ib_dev->cache.ports[port].gid = table;
838 }
839 return 0;
840
841rollback_table_setup:
842 gid_table_release_one(ib_dev);
843 return -ENOMEM;
844}
845
Matan Barak03db3a22015-07-30 18:33:26 +0300846static void gid_table_cleanup_one(struct ib_device *ib_dev)
847{
Jack Wang21d64542017-01-17 10:11:12 +0100848 struct ib_gid_table *table;
Matan Barak03db3a22015-07-30 18:33:26 +0300849 u8 port;
850
Jack Wang21d64542017-01-17 10:11:12 +0100851 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
852 table = ib_dev->cache.ports[port].gid;
Matan Barak03db3a22015-07-30 18:33:26 +0300853 cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
Jack Wang21d64542017-01-17 10:11:12 +0100854 table);
855 }
Matan Barak03db3a22015-07-30 18:33:26 +0300856}
857
858static int gid_table_setup_one(struct ib_device *ib_dev)
859{
860 int err;
861
862 err = _gid_table_setup_one(ib_dev);
863
864 if (err)
865 return err;
866
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200867 rdma_roce_rescan_device(ib_dev);
Matan Barak03db3a22015-07-30 18:33:26 +0300868
869 return err;
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872int ib_get_cached_gid(struct ib_device *device,
873 u8 port_num,
874 int index,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300875 union ib_gid *gid,
876 struct ib_gid_attr *gid_attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Matan Barak9c584f02015-10-28 16:52:40 +0200878 int res;
879 unsigned long flags;
Jack Wang21d64542017-01-17 10:11:12 +0100880 struct ib_gid_table *table;
Matan Barak9c584f02015-10-28 16:52:40 +0200881
Yuval Shaia24dc8312017-01-25 18:41:37 +0200882 if (!rdma_is_port_valid(device, port_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return -EINVAL;
884
Parav Pandit724631a2018-05-22 20:33:46 +0300885 table = rdma_gid_table(device, port_num);
Matan Barak9c584f02015-10-28 16:52:40 +0200886 read_lock_irqsave(&table->rwlock, flags);
887 res = __ib_cache_gid_get(device, port_num, index, gid, gid_attr);
888 read_unlock_irqrestore(&table->rwlock, flags);
889
890 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892EXPORT_SYMBOL(ib_get_cached_gid);
893
Parav Pandit6612b492018-03-13 16:06:11 +0200894/**
895 * ib_find_cached_gid - Returns the port number and GID table index where
896 * a specified GID value occurs.
897 * @device: The device to query.
898 * @gid: The GID value to search for.
899 * @gid_type: The GID type to search for.
900 * @ndev: In RoCE, the net device of the device. NULL means ignore.
901 * @port_num: The port number of the device where the GID value was found.
902 * @index: The index into the cached GID table where the GID was found. This
903 * parameter may be NULL.
904 *
905 * ib_find_cached_gid() searches for the specified GID value in
906 * the local software cache.
907 */
Matan Barak03db3a22015-07-30 18:33:26 +0300908int ib_find_cached_gid(struct ib_device *device,
Ira Weiny73cdaae2015-05-31 17:15:31 -0400909 const union ib_gid *gid,
Matan Barakb39ffa12015-12-23 14:56:47 +0200910 enum ib_gid_type gid_type,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300911 struct net_device *ndev,
Matan Barak03db3a22015-07-30 18:33:26 +0300912 u8 *port_num,
913 u16 *index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Matan Barakb39ffa12015-12-23 14:56:47 +0200915 return ib_cache_gid_find(device, gid, gid_type, ndev, port_num, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917EXPORT_SYMBOL(ib_find_cached_gid);
918
Matan Barak99b27e32015-10-15 18:38:50 +0300919int ib_find_gid_by_filter(struct ib_device *device,
920 const union ib_gid *gid,
921 u8 port_num,
922 bool (*filter)(const union ib_gid *gid,
923 const struct ib_gid_attr *,
924 void *),
925 void *context, u16 *index)
926{
927 /* Only RoCE GID table supports filter function */
Parav Pandit4ab7cb42018-03-25 13:40:21 +0300928 if (!rdma_protocol_roce(device, port_num) && filter)
Matan Barak99b27e32015-10-15 18:38:50 +0300929 return -EPROTONOSUPPORT;
930
931 return ib_cache_gid_find_by_filter(device, gid,
932 port_num, filter,
933 context, index);
934}
Matan Barak99b27e32015-10-15 18:38:50 +0300935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936int ib_get_cached_pkey(struct ib_device *device,
937 u8 port_num,
938 int index,
939 u16 *pkey)
940{
941 struct ib_pkey_cache *cache;
942 unsigned long flags;
943 int ret = 0;
944
Yuval Shaia24dc8312017-01-25 18:41:37 +0200945 if (!rdma_is_port_valid(device, port_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return -EINVAL;
947
948 read_lock_irqsave(&device->cache.lock, flags);
949
Jack Wang21d64542017-01-17 10:11:12 +0100950 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 if (index < 0 || index >= cache->table_len)
953 ret = -EINVAL;
954 else
955 *pkey = cache->table[index];
956
957 read_unlock_irqrestore(&device->cache.lock, flags);
958
959 return ret;
960}
961EXPORT_SYMBOL(ib_get_cached_pkey);
962
Daniel Jurgens883c71f2017-05-19 15:48:51 +0300963int ib_get_cached_subnet_prefix(struct ib_device *device,
964 u8 port_num,
965 u64 *sn_pfx)
966{
967 unsigned long flags;
968 int p;
969
Parav Pandit6d5b2042018-03-19 07:59:59 +0200970 if (!rdma_is_port_valid(device, port_num))
Daniel Jurgens883c71f2017-05-19 15:48:51 +0300971 return -EINVAL;
972
973 p = port_num - rdma_start_port(device);
974 read_lock_irqsave(&device->cache.lock, flags);
975 *sn_pfx = device->cache.ports[p].subnet_prefix;
976 read_unlock_irqrestore(&device->cache.lock, flags);
977
978 return 0;
979}
980EXPORT_SYMBOL(ib_get_cached_subnet_prefix);
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982int ib_find_cached_pkey(struct ib_device *device,
983 u8 port_num,
984 u16 pkey,
985 u16 *index)
986{
987 struct ib_pkey_cache *cache;
988 unsigned long flags;
989 int i;
990 int ret = -ENOENT;
Jack Morgensteinff7166c2012-08-03 08:40:38 +0000991 int partial_ix = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Yuval Shaia24dc8312017-01-25 18:41:37 +0200993 if (!rdma_is_port_valid(device, port_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return -EINVAL;
995
996 read_lock_irqsave(&device->cache.lock, flags);
997
Jack Wang21d64542017-01-17 10:11:12 +0100998 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 *index = -1;
1001
1002 for (i = 0; i < cache->table_len; ++i)
1003 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
Jack Morgensteinff7166c2012-08-03 08:40:38 +00001004 if (cache->table[i] & 0x8000) {
1005 *index = i;
1006 ret = 0;
1007 break;
1008 } else
1009 partial_ix = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011
Jack Morgensteinff7166c2012-08-03 08:40:38 +00001012 if (ret && partial_ix >= 0) {
1013 *index = partial_ix;
1014 ret = 0;
1015 }
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 read_unlock_irqrestore(&device->cache.lock, flags);
1018
1019 return ret;
1020}
1021EXPORT_SYMBOL(ib_find_cached_pkey);
1022
Jack Morgenstein73aaa742012-08-03 08:40:39 +00001023int ib_find_exact_cached_pkey(struct ib_device *device,
1024 u8 port_num,
1025 u16 pkey,
1026 u16 *index)
1027{
1028 struct ib_pkey_cache *cache;
1029 unsigned long flags;
1030 int i;
1031 int ret = -ENOENT;
1032
Yuval Shaia24dc8312017-01-25 18:41:37 +02001033 if (!rdma_is_port_valid(device, port_num))
Jack Morgenstein73aaa742012-08-03 08:40:39 +00001034 return -EINVAL;
1035
1036 read_lock_irqsave(&device->cache.lock, flags);
1037
Jack Wang21d64542017-01-17 10:11:12 +01001038 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
Jack Morgenstein73aaa742012-08-03 08:40:39 +00001039
1040 *index = -1;
1041
1042 for (i = 0; i < cache->table_len; ++i)
1043 if (cache->table[i] == pkey) {
1044 *index = i;
1045 ret = 0;
1046 break;
1047 }
1048
1049 read_unlock_irqrestore(&device->cache.lock, flags);
1050
1051 return ret;
1052}
1053EXPORT_SYMBOL(ib_find_exact_cached_pkey);
1054
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -07001055int ib_get_cached_lmc(struct ib_device *device,
1056 u8 port_num,
1057 u8 *lmc)
1058{
1059 unsigned long flags;
1060 int ret = 0;
1061
Yuval Shaia24dc8312017-01-25 18:41:37 +02001062 if (!rdma_is_port_valid(device, port_num))
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -07001063 return -EINVAL;
1064
1065 read_lock_irqsave(&device->cache.lock, flags);
Jack Wang21d64542017-01-17 10:11:12 +01001066 *lmc = device->cache.ports[port_num - rdma_start_port(device)].lmc;
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -07001067 read_unlock_irqrestore(&device->cache.lock, flags);
1068
1069 return ret;
1070}
1071EXPORT_SYMBOL(ib_get_cached_lmc);
1072
Jack Wang9e2c3f12017-01-02 13:19:24 +01001073int ib_get_cached_port_state(struct ib_device *device,
1074 u8 port_num,
1075 enum ib_port_state *port_state)
1076{
1077 unsigned long flags;
1078 int ret = 0;
1079
Parav Pandit6d5b2042018-03-19 07:59:59 +02001080 if (!rdma_is_port_valid(device, port_num))
Jack Wang9e2c3f12017-01-02 13:19:24 +01001081 return -EINVAL;
1082
1083 read_lock_irqsave(&device->cache.lock, flags);
Jack Wang21d64542017-01-17 10:11:12 +01001084 *port_state = device->cache.ports[port_num
1085 - rdma_start_port(device)].port_state;
Jack Wang9e2c3f12017-01-02 13:19:24 +01001086 read_unlock_irqrestore(&device->cache.lock, flags);
1087
1088 return ret;
1089}
1090EXPORT_SYMBOL(ib_get_cached_port_state);
1091
Parav Pandit598ff6b2018-04-01 15:08:21 +03001092static int config_non_roce_gid_cache(struct ib_device *device,
1093 u8 port, int gid_tbl_len)
1094{
1095 struct ib_gid_attr gid_attr = {};
1096 struct ib_gid_table *table;
1097 union ib_gid gid;
1098 int ret = 0;
1099 int i;
1100
1101 gid_attr.device = device;
1102 gid_attr.port_num = port;
Parav Pandit724631a2018-05-22 20:33:46 +03001103 table = rdma_gid_table(device, port);
Parav Pandit598ff6b2018-04-01 15:08:21 +03001104
1105 mutex_lock(&table->lock);
1106 for (i = 0; i < gid_tbl_len; ++i) {
1107 if (!device->query_gid)
1108 continue;
1109 ret = device->query_gid(device, port, i, &gid);
1110 if (ret) {
1111 pr_warn("query_gid failed (%d) for %s (index %d)\n",
1112 ret, device->name, i);
1113 goto err;
1114 }
1115 gid_attr.index = i;
1116 add_modify_gid(table, &gid, &gid_attr);
1117 }
1118err:
1119 mutex_unlock(&table->lock);
1120 return ret;
1121}
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123static void ib_cache_update(struct ib_device *device,
Daniel Jurgensd291f1a2017-05-19 15:48:52 +03001124 u8 port,
1125 bool enforce_security)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 struct ib_port_attr *tprops = NULL;
1128 struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 int i;
1130 int ret;
Matan Barak03db3a22015-07-30 18:33:26 +03001131 struct ib_gid_table *table;
Matan Barak03db3a22015-07-30 18:33:26 +03001132
Yuval Shaia24dc8312017-01-25 18:41:37 +02001133 if (!rdma_is_port_valid(device, port))
Matan Barak03db3a22015-07-30 18:33:26 +03001134 return;
1135
Parav Pandit724631a2018-05-22 20:33:46 +03001136 table = rdma_gid_table(device, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
1139 if (!tprops)
1140 return;
1141
1142 ret = ib_query_port(device, port, tprops);
1143 if (ret) {
Parav Panditaba25a3e2016-03-02 00:50:29 +05301144 pr_warn("ib_query_port failed (%d) for %s\n",
1145 ret, device->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 goto err;
1147 }
1148
Parav Pandit598ff6b2018-04-01 15:08:21 +03001149 if (!rdma_protocol_roce(device, port)) {
1150 ret = config_non_roce_gid_cache(device, port,
1151 tprops->gid_tbl_len);
1152 if (ret)
1153 goto err;
1154 }
1155
Kees Cookacafe7e2018-05-08 13:45:50 -07001156 pkey_cache = kmalloc(struct_size(pkey_cache, table,
1157 tprops->pkey_tbl_len),
1158 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (!pkey_cache)
1160 goto err;
1161
1162 pkey_cache->table_len = tprops->pkey_tbl_len;
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 for (i = 0; i < pkey_cache->table_len; ++i) {
1165 ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
1166 if (ret) {
Parav Panditaba25a3e2016-03-02 00:50:29 +05301167 pr_warn("ib_query_pkey failed (%d) for %s (index %d)\n",
1168 ret, device->name, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto err;
1170 }
1171 }
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 write_lock_irq(&device->cache.lock);
1174
Jack Wang21d64542017-01-17 10:11:12 +01001175 old_pkey_cache = device->cache.ports[port -
1176 rdma_start_port(device)].pkey;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Jack Wang21d64542017-01-17 10:11:12 +01001178 device->cache.ports[port - rdma_start_port(device)].pkey = pkey_cache;
Jack Wang21d64542017-01-17 10:11:12 +01001179 device->cache.ports[port - rdma_start_port(device)].lmc = tprops->lmc;
1180 device->cache.ports[port - rdma_start_port(device)].port_state =
Jack Wangaaaca122017-01-02 13:17:36 +01001181 tprops->state;
Jack Morgenstein6fb9cdb2006-06-17 20:37:34 -07001182
Daniel Jurgens883c71f2017-05-19 15:48:51 +03001183 device->cache.ports[port - rdma_start_port(device)].subnet_prefix =
1184 tprops->subnet_prefix;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 write_unlock_irq(&device->cache.lock);
1186
Daniel Jurgensd291f1a2017-05-19 15:48:52 +03001187 if (enforce_security)
1188 ib_security_cache_change(device,
1189 port,
1190 tprops->subnet_prefix);
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 kfree(old_pkey_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 kfree(tprops);
1194 return;
1195
1196err:
1197 kfree(pkey_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 kfree(tprops);
1199}
1200
David Howellsc4028952006-11-22 14:57:56 +00001201static void ib_cache_task(struct work_struct *_work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
David Howellsc4028952006-11-22 14:57:56 +00001203 struct ib_update_work *work =
1204 container_of(_work, struct ib_update_work, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Daniel Jurgensd291f1a2017-05-19 15:48:52 +03001206 ib_cache_update(work->device,
1207 work->port_num,
1208 work->enforce_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 kfree(work);
1210}
1211
1212static void ib_cache_event(struct ib_event_handler *handler,
1213 struct ib_event *event)
1214{
1215 struct ib_update_work *work;
1216
1217 if (event->event == IB_EVENT_PORT_ERR ||
1218 event->event == IB_EVENT_PORT_ACTIVE ||
1219 event->event == IB_EVENT_LID_CHANGE ||
1220 event->event == IB_EVENT_PKEY_CHANGE ||
Jack Morgensteinacaea9e2006-08-15 17:20:50 +03001221 event->event == IB_EVENT_SM_CHANGE ||
Or Gerlitz761d90e2011-06-15 14:39:29 +00001222 event->event == IB_EVENT_CLIENT_REREGISTER ||
1223 event->event == IB_EVENT_GID_CHANGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 work = kmalloc(sizeof *work, GFP_ATOMIC);
1225 if (work) {
David Howellsc4028952006-11-22 14:57:56 +00001226 INIT_WORK(&work->work, ib_cache_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 work->device = event->device;
1228 work->port_num = event->element.port_num;
Daniel Jurgensd291f1a2017-05-19 15:48:52 +03001229 if (event->event == IB_EVENT_PKEY_CHANGE ||
1230 event->event == IB_EVENT_GID_CHANGE)
1231 work->enforce_security = true;
1232 else
1233 work->enforce_security = false;
1234
Tejun Heof0626712010-10-19 15:24:36 +00001235 queue_work(ib_wq, &work->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237 }
1238}
1239
Matan Barak03db3a22015-07-30 18:33:26 +03001240int ib_cache_setup_one(struct ib_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 int p;
Matan Barak03db3a22015-07-30 18:33:26 +03001243 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 rwlock_init(&device->cache.lock);
1246
Jack Wang21d64542017-01-17 10:11:12 +01001247 device->cache.ports =
1248 kzalloc(sizeof(*device->cache.ports) *
Ira Weiny0cf18d72015-05-13 20:02:55 -04001249 (rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL);
Leon Romanovskydcc98812017-08-17 15:50:36 +03001250 if (!device->cache.ports)
1251 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Matan Barak03db3a22015-07-30 18:33:26 +03001253 err = gid_table_setup_one(device);
Leon Romanovskydcc98812017-08-17 15:50:36 +03001254 if (err) {
1255 kfree(device->cache.ports);
1256 device->cache.ports = NULL;
1257 return err;
1258 }
Matan Barak03db3a22015-07-30 18:33:26 +03001259
Jason Gunthorpe55aeed02015-08-04 15:23:34 -06001260 for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
Daniel Jurgensd291f1a2017-05-19 15:48:52 +03001261 ib_cache_update(device, p + rdma_start_port(device), true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
1264 device, ib_cache_event);
Leon Romanovskydcc98812017-08-17 15:50:36 +03001265 ib_register_event_handler(&device->cache.event_handler);
Matan Barak03db3a22015-07-30 18:33:26 +03001266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Matan Barak03db3a22015-07-30 18:33:26 +03001269void ib_cache_release_one(struct ib_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 int p;
1272
Matan Barak03db3a22015-07-30 18:33:26 +03001273 /*
1274 * The release function frees all the cache elements.
1275 * This function should be called as part of freeing
1276 * all the device's resources when the cache could no
1277 * longer be accessed.
1278 */
Jack Wang21d64542017-01-17 10:11:12 +01001279 for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
1280 kfree(device->cache.ports[p].pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Matan Barak03db3a22015-07-30 18:33:26 +03001282 gid_table_release_one(device);
Jack Wang21d64542017-01-17 10:11:12 +01001283 kfree(device->cache.ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Matan Barak03db3a22015-07-30 18:33:26 +03001286void ib_cache_cleanup_one(struct ib_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Matan Barak03db3a22015-07-30 18:33:26 +03001288 /* The cleanup function unregisters the event handler,
1289 * waits for all in-progress workqueue elements and cleans
1290 * up the GID cache. This function should be called after
1291 * the device was removed from the devices list and all
1292 * clients were removed, so the cache exists but is
1293 * non-functional and shouldn't be updated anymore.
1294 */
1295 ib_unregister_event_handler(&device->cache.event_handler);
1296 flush_workqueue(ib_wq);
1297 gid_table_cleanup_one(device);
1298}