cache.c 32.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Copyright (c) 2004 Topspin Communications.  All rights reserved.
3 4 5
 * Copyright (c) 2005 Intel Corporation. All rights reserved.
 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
 * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/slab.h>
A
Alexey Dobriyan 已提交
39
#include <linux/workqueue.h>
40 41
#include <linux/netdevice.h>
#include <net/addrconf.h>
L
Linus Torvalds 已提交
42

43
#include <rdma/ib_cache.h>
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55

#include "core_priv.h"

struct ib_pkey_cache {
	int             table_len;
	u16             table[0];
};

struct ib_update_work {
	struct work_struct work;
	struct ib_device  *device;
	u8                 port_num;
56
	bool		   enforce_security;
L
Linus Torvalds 已提交
57 58
};

59 60
union ib_gid zgid;
EXPORT_SYMBOL(zgid);
61 62 63 64 65

enum gid_attr_find_mask {
	GID_ATTR_FIND_MASK_GID          = 1UL << 0,
	GID_ATTR_FIND_MASK_NETDEV	= 1UL << 1,
	GID_ATTR_FIND_MASK_DEFAULT	= 1UL << 2,
66
	GID_ATTR_FIND_MASK_GID_TYPE	= 1UL << 3,
67 68 69 70 71 72 73 74 75 76 77 78 79 80
};

enum gid_table_entry_props {
	GID_TABLE_ENTRY_INVALID		= 1UL << 0,
};

struct ib_gid_table_entry {
	unsigned long	    props;
	union ib_gid        gid;
	struct ib_gid_attr  attr;
	void		   *context;
};

struct ib_gid_table {
81
	int				sz;
82 83 84 85 86 87 88 89 90 91
	/* In RoCE, adding a GID to the table requires:
	 * (a) Find if this GID is already exists.
	 * (b) Find a free space.
	 * (c) Write the new GID
	 *
	 * Delete requires different set of operations:
	 * (a) Find the GID
	 * (b) Delete it.
	 *
	 **/
92 93 94
	/* Any writer to data_vec must hold this lock and the write side of
	 * rwlock. readers must hold only rwlock. All writers must be in a
	 * sleepable context.
95
	 */
96
	struct mutex			lock;
97
	/* rwlock protects data_vec[ix]->props. */
98 99 100 101
	rwlock_t			rwlock;
	/* bit field, each bit indicates the index of default GID */
	u32				default_gid_indices;
	struct ib_gid_table_entry	*data_vec;
102 103
};

104 105
static void dispatch_gid_change_event(struct ib_device *ib_dev, u8 port)
{
106
	struct ib_event event;
107

108 109 110
	event.device		= ib_dev;
	event.element.port_num	= port;
	event.event		= IB_EVENT_GID_CHANGE;
111

112
	ib_dispatch_event(&event);
113 114
}

115 116
static const char * const gid_type_str[] = {
	[IB_GID_TYPE_IB]	= "IB/RoCE v1",
117
	[IB_GID_TYPE_ROCE_UDP_ENCAP]	= "RoCE v2",
118 119 120 121 122 123 124 125 126 127 128
};

const char *ib_cache_gid_type_str(enum ib_gid_type gid_type)
{
	if (gid_type < ARRAY_SIZE(gid_type_str) && gid_type_str[gid_type])
		return gid_type_str[gid_type];

	return "Invalid GID type";
}
EXPORT_SYMBOL(ib_cache_gid_type_str);

129 130 131 132 133 134 135 136 137 138
/** rdma_is_zero_gid - Check if given GID is zero or not.
 * @gid:	GID to check
 * Returns true if given GID is zero, returns false otherwise.
 */
bool rdma_is_zero_gid(const union ib_gid *gid)
{
	return !memcmp(gid, &zgid, sizeof(*gid));
}
EXPORT_SYMBOL(rdma_is_zero_gid);

139 140 141 142 143 144 145 146 147 148 149 150 151
/** is_gid_index_default - Check if a given index belongs to
 * reserved default GIDs or not.
 * @table:	GID table pointer
 * @index:	Index to check in GID table
 * Returns true if index is one of the reserved default GID index otherwise
 * returns false.
 */
static bool is_gid_index_default(const struct ib_gid_table *table,
				 unsigned int index)
{
	return index < 32 && (BIT(index) & table->default_gid_indices);
}

M
Matan Barak 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
int ib_cache_gid_parse_type_str(const char *buf)
{
	unsigned int i;
	size_t len;
	int err = -EINVAL;

	len = strlen(buf);
	if (len == 0)
		return -EINVAL;

	if (buf[len - 1] == '\n')
		len--;

	for (i = 0; i < ARRAY_SIZE(gid_type_str); ++i)
		if (gid_type_str[i] && !strncmp(buf, gid_type_str[i], len) &&
		    len == strlen(gid_type_str[i])) {
			err = i;
			break;
		}

	return err;
}
EXPORT_SYMBOL(ib_cache_gid_parse_type_str);

176 177 178 179 180
static struct ib_gid_table *rdma_gid_table(struct ib_device *device, u8 port)
{
	return device->cache.ports[port - rdma_start_port(device)].gid;
}

181 182
static void del_roce_gid(struct ib_device *device, u8 port_num,
			 struct ib_gid_table *table, int ix)
L
Linus Torvalds 已提交
183
{
184 185 186 187 188
	pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
		 device->name, port_num, ix,
		 table->data_vec[ix].gid.raw);

	if (rdma_cap_roce_gid_table(device, port_num))
189
		device->del_gid(&table->data_vec[ix].attr,
190 191 192
				&table->data_vec[ix].context);
	dev_put(table->data_vec[ix].attr.ndev);
}
193

194 195 196 197 198 199 200
static int add_roce_gid(struct ib_gid_table *table,
			const union ib_gid *gid,
			const struct ib_gid_attr *attr)
{
	struct ib_gid_table_entry *entry;
	int ix = attr->index;
	int ret = 0;
201

202 203 204 205 206
	if (!attr->ndev) {
		pr_err("%s NULL netdev device=%s port=%d index=%d\n",
		       __func__, attr->device->name, attr->port_num,
		       attr->index);
		return -EINVAL;
207 208
	}

209 210 211 212 213 214
	entry = &table->data_vec[ix];
	if ((entry->props & GID_TABLE_ENTRY_INVALID) == 0) {
		WARN(1, "GID table corruption device=%s port=%d index=%d\n",
		     attr->device->name, attr->port_num,
		     attr->index);
		return -EINVAL;
215
	}
216

217
	if (rdma_cap_roce_gid_table(attr->device, attr->port_num)) {
218
		ret = attr->device->add_gid(gid, attr, &entry->context);
219 220 221 222 223 224
		if (ret) {
			pr_err("%s GID add failed device=%s port=%d index=%d\n",
			       __func__, attr->device->name, attr->port_num,
			       attr->index);
			goto add_err;
		}
225
	}
226
	dev_hold(attr->ndev);
227

228 229 230 231
add_err:
	if (!ret)
		pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
			 attr->device->name, attr->port_num, ix, gid->raw);
232 233 234
	return ret;
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
/**
 * add_modify_gid - Add or modify GID table entry
 *
 * @table:	GID table in which GID to be added or modified
 * @gid:	GID content
 * @attr:	Attributes of the GID
 *
 * Returns 0 on success or appropriate error code. It accepts zero
 * GID addition for non RoCE ports for HCA's who report them as valid
 * GID. However such zero GIDs are not added to the cache.
 */
static int add_modify_gid(struct ib_gid_table *table,
			  const union ib_gid *gid,
			  const struct ib_gid_attr *attr)
{
	int ret;

	if (rdma_protocol_roce(attr->device, attr->port_num)) {
		ret = add_roce_gid(table, gid, attr);
		if (ret)
			return ret;
	} else {
		/*
		 * Some HCA's report multiple GID entries with only one
		 * valid GID, but remaining as zero GID.
		 * So ignore such behavior for IB link layer and don't
		 * fail the call, but don't add such entry to GID cache.
		 */
263
		if (rdma_is_zero_gid(gid))
264 265 266 267 268 269
			return 0;
	}

	lockdep_assert_held(&table->lock);
	memcpy(&table->data_vec[attr->index].gid, gid, sizeof(*gid));
	memcpy(&table->data_vec[attr->index].attr, attr, sizeof(*attr));
270

271 272 273 274
	write_lock_irq(&table->rwlock);
	table->data_vec[attr->index].props &= ~GID_TABLE_ENTRY_INVALID;
	write_unlock_irq(&table->rwlock);
	return 0;
275 276
}

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
/**
 * del_gid - Delete GID table entry
 *
 * @ib_dev:	IB device whose GID entry to be deleted
 * @port:	Port number of the IB device
 * @table:	GID table of the IB device for a port
 * @ix:		GID entry index to delete
 *
 */
static void del_gid(struct ib_device *ib_dev, u8 port,
		    struct ib_gid_table *table, int ix)
{
	lockdep_assert_held(&table->lock);
	write_lock_irq(&table->rwlock);
	table->data_vec[ix].props |= GID_TABLE_ENTRY_INVALID;
	write_unlock_irq(&table->rwlock);

	if (rdma_protocol_roce(ib_dev, port))
		del_roce_gid(ib_dev, port, table, ix);
296
	memset(&table->data_vec[ix].gid, 0, sizeof(table->data_vec[ix].gid));
297 298
	memset(&table->data_vec[ix].attr, 0, sizeof(table->data_vec[ix].attr));
	table->data_vec[ix].context = NULL;
299 300
}

301
/* rwlock should be read locked, or lock should be held */
302 303
static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,
		    const struct ib_gid_attr *val, bool default_gid,
304
		    unsigned long mask, int *pempty)
305
{
306 307 308
	int i = 0;
	int found = -1;
	int empty = pempty ? -1 : 0;
309

310 311 312 313
	while (i < table->sz && (found < 0 || empty < 0)) {
		struct ib_gid_table_entry *data = &table->data_vec[i];
		struct ib_gid_attr *attr = &data->attr;
		int curr_index = i;
314

315
		i++;
316

317 318 319 320 321 322
		/* find_gid() is used during GID addition where it is expected
		 * to return a free entry slot which is not duplicate.
		 * Free entry slot is requested and returned if pempty is set,
		 * so lookup free slot only if requested.
		 */
		if (pempty && empty < 0) {
323 324
			if (data->props & GID_TABLE_ENTRY_INVALID &&
			    (default_gid ==
325
				is_gid_index_default(table, curr_index))) {
326 327 328 329 330 331 332 333 334
				/*
				 * Found an invalid (free) entry; allocate it.
				 * If default GID is requested, then our
				 * found slot must be one of the DEFAULT
				 * reserved slots or we fail.
				 * This ensures that only DEFAULT reserved
				 * slots are used for default property GIDs.
				 */
				empty = curr_index;
335 336 337 338 339 340 341 342 343
			}
		}

		/*
		 * Additionally find_gid() is used to find valid entry during
		 * lookup operation, where validity needs to be checked. So
		 * find the empty entry first to continue to search for a free
		 * slot and ignore its INVALID flag.
		 */
344 345 346 347
		if (data->props & GID_TABLE_ENTRY_INVALID)
			continue;

		if (found >= 0)
348
			continue;
349

350 351 352 353
		if (mask & GID_ATTR_FIND_MASK_GID_TYPE &&
		    attr->gid_type != val->gid_type)
			continue;

354
		if (mask & GID_ATTR_FIND_MASK_GID &&
355
		    memcmp(gid, &data->gid, sizeof(*gid)))
356
			continue;
357 358 359

		if (mask & GID_ATTR_FIND_MASK_NETDEV &&
		    attr->ndev != val->ndev)
360
			continue;
361 362

		if (mask & GID_ATTR_FIND_MASK_DEFAULT &&
363
		    is_gid_index_default(table, curr_index) != default_gid)
364
			continue;
365

366
		found = curr_index;
367 368
	}

369 370 371 372
	if (pempty)
		*pempty = empty;

	return found;
373 374 375 376 377 378 379 380
}

static void make_default_gid(struct  net_device *dev, union ib_gid *gid)
{
	gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
	addrconf_ifid_eui48(&gid->raw[8], dev);
}

381 382 383
static int __ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
			      union ib_gid *gid, struct ib_gid_attr *attr,
			      unsigned long mask, bool default_gid)
384 385
{
	struct ib_gid_table *table;
L
Linus Torvalds 已提交
386
	int ret = 0;
387
	int empty;
388
	int ix;
L
Linus Torvalds 已提交
389

390 391 392 393
	/* Do not allow adding zero GID in support of
	 * IB spec version 1.3 section 4.1.1 point (6) and
	 * section 12.7.10 and section 12.7.20
	 */
394
	if (rdma_is_zero_gid(gid))
L
Linus Torvalds 已提交
395 396
		return -EINVAL;

397
	table = rdma_gid_table(ib_dev, port);
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

	mutex_lock(&table->lock);

	ix = find_gid(table, gid, attr, default_gid, mask, &empty);
	if (ix >= 0)
		goto out_unlock;

	if (empty < 0) {
		ret = -ENOSPC;
		goto out_unlock;
	}
	attr->device = ib_dev;
	attr->index = empty;
	attr->port_num = port;
	ret = add_modify_gid(table, gid, attr);
	if (!ret)
		dispatch_gid_change_event(ib_dev, port);

out_unlock:
	mutex_unlock(&table->lock);
	if (ret)
		pr_warn("%s: unable to add gid %pI6 error=%d\n",
			__func__, gid->raw, ret);
	return ret;
}

int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
		     union ib_gid *gid, struct ib_gid_attr *attr)
{
	struct net_device *idev;
	unsigned long mask;
	int ret;

431 432 433 434
	if (ib_dev->get_netdev) {
		idev = ib_dev->get_netdev(ib_dev, port);
		if (idev && attr->ndev != idev) {
			union ib_gid default_gid;
L
Linus Torvalds 已提交
435

436 437 438 439 440 441 442 443 444 445
			/* Adding default GIDs in not permitted */
			make_default_gid(idev, &default_gid);
			if (!memcmp(gid, &default_gid, sizeof(*gid))) {
				dev_put(idev);
				return -EPERM;
			}
		}
		if (idev)
			dev_put(idev);
	}
L
Linus Torvalds 已提交
446

447 448 449
	mask = GID_ATTR_FIND_MASK_GID |
	       GID_ATTR_FIND_MASK_GID_TYPE |
	       GID_ATTR_FIND_MASK_NETDEV;
450

451
	ret = __ib_cache_gid_add(ib_dev, port, gid, attr, mask, false);
L
Linus Torvalds 已提交
452 453 454
	return ret;
}

455 456 457
static int
_ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
		  union ib_gid *gid, struct ib_gid_attr *attr,
458
		  unsigned long mask, bool default_gid)
L
Linus Torvalds 已提交
459
{
460
	struct ib_gid_table *table;
461
	int ret = 0;
462 463
	int ix;

464
	table = rdma_gid_table(ib_dev, port);
465 466 467

	mutex_lock(&table->lock);

468
	ix = find_gid(table, gid, attr, default_gid, mask, NULL);
469 470
	if (ix < 0) {
		ret = -EINVAL;
471
		goto out_unlock;
472
	}
473

474 475
	del_gid(ib_dev, port, table, ix);
	dispatch_gid_change_event(ib_dev, port);
476 477 478

out_unlock:
	mutex_unlock(&table->lock);
479 480 481 482
	if (ret)
		pr_debug("%s: can't delete gid %pI6 error=%d\n",
			 __func__, gid->raw, ret);
	return ret;
483 484
}

485 486 487
int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
		     union ib_gid *gid, struct ib_gid_attr *attr)
{
488 489 490 491 492 493
	unsigned long mask = GID_ATTR_FIND_MASK_GID	  |
			     GID_ATTR_FIND_MASK_GID_TYPE |
			     GID_ATTR_FIND_MASK_DEFAULT  |
			     GID_ATTR_FIND_MASK_NETDEV;

	return _ib_cache_gid_del(ib_dev, port, gid, attr, mask, false);
494 495
}

496 497 498 499 500
int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
				     struct net_device *ndev)
{
	struct ib_gid_table *table;
	int ix;
501
	bool deleted = false;
502

503
	table = rdma_gid_table(ib_dev, port);
504 505 506

	mutex_lock(&table->lock);

507 508 509 510 511 512
	for (ix = 0; ix < table->sz; ix++) {
		if (table->data_vec[ix].attr.ndev == ndev) {
			del_gid(ib_dev, port, table, ix);
			deleted = true;
		}
	}
513 514

	mutex_unlock(&table->lock);
515 516 517 518

	if (deleted)
		dispatch_gid_change_event(ib_dev, port);

519 520 521 522 523 524 525
	return 0;
}

static int __ib_cache_gid_get(struct ib_device *ib_dev, u8 port, int index,
			      union ib_gid *gid, struct ib_gid_attr *attr)
{
	struct ib_gid_table *table;
L
Linus Torvalds 已提交
526

527
	table = rdma_gid_table(ib_dev, port);
L
Linus Torvalds 已提交
528

529 530
	if (index < 0 || index >= table->sz)
		return -EINVAL;
L
Linus Torvalds 已提交
531

532
	if (table->data_vec[index].props & GID_TABLE_ENTRY_INVALID)
533
		return -EINVAL;
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553

	memcpy(gid, &table->data_vec[index].gid, sizeof(*gid));
	if (attr) {
		memcpy(attr, &table->data_vec[index].attr, sizeof(*attr));
		if (attr->ndev)
			dev_hold(attr->ndev);
	}

	return 0;
}

static int _ib_cache_gid_table_find(struct ib_device *ib_dev,
				    const union ib_gid *gid,
				    const struct ib_gid_attr *val,
				    unsigned long mask,
				    u8 *port, u16 *index)
{
	struct ib_gid_table *table;
	u8 p;
	int local_index;
554
	unsigned long flags;
555 556

	for (p = 0; p < ib_dev->phys_port_cnt; p++) {
557
		table = ib_dev->cache.ports[p].gid;
558
		read_lock_irqsave(&table->rwlock, flags);
559
		local_index = find_gid(table, gid, val, false, mask, NULL);
560 561 562 563 564
		if (local_index >= 0) {
			if (index)
				*index = local_index;
			if (port)
				*port = p + rdma_start_port(ib_dev);
565
			read_unlock_irqrestore(&table->rwlock, flags);
566
			return 0;
L
Linus Torvalds 已提交
567
		}
568
		read_unlock_irqrestore(&table->rwlock, flags);
L
Linus Torvalds 已提交
569 570
	}

571 572 573 574 575
	return -ENOENT;
}

static int ib_cache_gid_find(struct ib_device *ib_dev,
			     const union ib_gid *gid,
576
			     enum ib_gid_type gid_type,
577 578 579
			     struct net_device *ndev, u8 *port,
			     u16 *index)
{
580 581 582
	unsigned long mask = GID_ATTR_FIND_MASK_GID |
			     GID_ATTR_FIND_MASK_GID_TYPE;
	struct ib_gid_attr gid_attr_val = {.ndev = ndev, .gid_type = gid_type};
583 584 585 586 587 588 589 590

	if (ndev)
		mask |= GID_ATTR_FIND_MASK_NETDEV;

	return _ib_cache_gid_table_find(ib_dev, gid, &gid_attr_val,
					mask, port, index);
}

591 592 593 594 595 596 597 598 599 600 601 602 603
/**
 * ib_find_cached_gid_by_port - Returns the GID table index where a specified
 * GID value occurs. It searches for the specified GID value in the local
 * software cache.
 * @device: The device to query.
 * @gid: The GID value to search for.
 * @gid_type: The GID type to search for.
 * @port_num: The port number of the device where the GID value should be
 *   searched.
 * @ndev: In RoCE, the net device of the device. Null means ignore.
 * @index: The index into the cached GID table where the GID was found. This
 *   parameter may be NULL.
 */
604 605
int ib_find_cached_gid_by_port(struct ib_device *ib_dev,
			       const union ib_gid *gid,
606
			       enum ib_gid_type gid_type,
607 608
			       u8 port, struct net_device *ndev,
			       u16 *index)
609 610 611
{
	int local_index;
	struct ib_gid_table *table;
612 613 614
	unsigned long mask = GID_ATTR_FIND_MASK_GID |
			     GID_ATTR_FIND_MASK_GID_TYPE;
	struct ib_gid_attr val = {.ndev = ndev, .gid_type = gid_type};
615
	unsigned long flags;
616

617
	if (!rdma_is_port_valid(ib_dev, port))
618 619
		return -ENOENT;

620
	table = rdma_gid_table(ib_dev, port);
621 622 623 624

	if (ndev)
		mask |= GID_ATTR_FIND_MASK_NETDEV;

625
	read_lock_irqsave(&table->rwlock, flags);
626
	local_index = find_gid(table, gid, &val, false, mask, NULL);
627 628 629
	if (local_index >= 0) {
		if (index)
			*index = local_index;
630
		read_unlock_irqrestore(&table->rwlock, flags);
631 632 633
		return 0;
	}

634
	read_unlock_irqrestore(&table->rwlock, flags);
635 636
	return -ENOENT;
}
637
EXPORT_SYMBOL(ib_find_cached_gid_by_port);
638

639
/**
640
 * ib_cache_gid_find_by_filter - Returns the GID table index where a specified
641 642 643 644 645 646 647 648 649 650
 * GID value occurs
 * @device: The device to query.
 * @gid: The GID value to search for.
 * @port_num: The port number of the device where the GID value could be
 *   searched.
 * @filter: The filter function is executed on any matching GID in the table.
 *   If the filter function returns true, the corresponding index is returned,
 *   otherwise, we continue searching the GID table. It's guaranteed that
 *   while filter is executed, ndev field is valid and the structure won't
 *   change. filter is executed in an atomic context. filter must not be NULL.
651
 * @index: The index into the cached GID table where the GID was found. This
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
 *   parameter may be NULL.
 *
 * ib_cache_gid_find_by_filter() searches for the specified GID value
 * of which the filter function returns true in the port's GID table.
 * This function is only supported on RoCE ports.
 *
 */
static int ib_cache_gid_find_by_filter(struct ib_device *ib_dev,
				       const union ib_gid *gid,
				       u8 port,
				       bool (*filter)(const union ib_gid *,
						      const struct ib_gid_attr *,
						      void *),
				       void *context,
				       u16 *index)
{
	struct ib_gid_table *table;
	unsigned int i;
670
	unsigned long flags;
671 672 673
	bool found = false;


674
	if (!rdma_is_port_valid(ib_dev, port) ||
675 676 677
	    !rdma_protocol_roce(ib_dev, port))
		return -EPROTONOSUPPORT;

678
	table = rdma_gid_table(ib_dev, port);
679

680
	read_lock_irqsave(&table->rwlock, flags);
681 682 683 684
	for (i = 0; i < table->sz; i++) {
		struct ib_gid_attr attr;

		if (table->data_vec[i].props & GID_TABLE_ENTRY_INVALID)
685
			continue;
686 687

		if (memcmp(gid, &table->data_vec[i].gid, sizeof(*gid)))
688
			continue;
689 690 691

		memcpy(&attr, &table->data_vec[i].attr, sizeof(attr));

692
		if (filter(gid, &attr, context)) {
693
			found = true;
694 695
			if (index)
				*index = i;
696
			break;
697
		}
698
	}
699
	read_unlock_irqrestore(&table->rwlock, flags);
700 701 702 703 704 705

	if (!found)
		return -ENOENT;
	return 0;
}

706 707 708 709
static struct ib_gid_table *alloc_gid_table(int sz)
{
	struct ib_gid_table *table =
		kzalloc(sizeof(struct ib_gid_table), GFP_KERNEL);
710
	int i;
711

712 713 714 715 716 717 718 719 720 721
	if (!table)
		return NULL;

	table->data_vec = kcalloc(sz, sizeof(*table->data_vec), GFP_KERNEL);
	if (!table->data_vec)
		goto err_free_table;

	mutex_init(&table->lock);

	table->sz = sz;
722
	rwlock_init(&table->rwlock);
723

724 725 726 727 728
	/* Mark all entries as invalid so that allocator can allocate
	 * one of the invalid (free) entry.
	 */
	for (i = 0; i < sz; i++)
		table->data_vec[i].props |= GID_TABLE_ENTRY_INVALID;
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
	return table;

err_free_table:
	kfree(table);
	return NULL;
}

static void release_gid_table(struct ib_gid_table *table)
{
	if (table) {
		kfree(table->data_vec);
		kfree(table);
	}
}

static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port,
				   struct ib_gid_table *table)
{
	int i;
748
	bool deleted = false;
749 750 751 752

	if (!table)
		return;

753
	mutex_lock(&table->lock);
754
	for (i = 0; i < table->sz; ++i) {
755
		if (!rdma_is_zero_gid(&table->data_vec[i].gid)) {
756 757 758
			del_gid(ib_dev, port, table, i);
			deleted = true;
		}
759
	}
760
	mutex_unlock(&table->lock);
761 762 763

	if (deleted)
		dispatch_gid_change_event(ib_dev, port);
764 765 766 767
}

void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
				  struct net_device *ndev,
768
				  unsigned long gid_type_mask,
769 770
				  enum ib_cache_gid_default_mode mode)
{
771
	union ib_gid gid = { };
772 773
	struct ib_gid_attr gid_attr;
	struct ib_gid_table *table;
774
	unsigned int gid_type;
775
	unsigned long mask;
776

777
	table = rdma_gid_table(ib_dev, port);
778

779 780 781
	mask = GID_ATTR_FIND_MASK_GID_TYPE |
	       GID_ATTR_FIND_MASK_DEFAULT |
	       GID_ATTR_FIND_MASK_NETDEV;
782 783 784
	memset(&gid_attr, 0, sizeof(gid_attr));
	gid_attr.ndev = ndev;

785 786 787 788 789 790 791
	for (gid_type = 0; gid_type < IB_GID_TYPE_SIZE; ++gid_type) {
		if (1UL << gid_type & ~gid_type_mask)
			continue;

		gid_attr.gid_type = gid_type;

		if (mode == IB_CACHE_GID_DEFAULT_MODE_SET) {
792
			make_default_gid(ndev, &gid);
793 794 795
			__ib_cache_gid_add(ib_dev, port, &gid,
					   &gid_attr, mask, true);
		} else if (mode == IB_CACHE_GID_DEFAULT_MODE_DELETE) {
796 797
			_ib_cache_gid_del(ib_dev, port, &gid,
					  &gid_attr, mask, true);
798
		}
799
	}
800 801
}

802 803
static void gid_table_reserve_default(struct ib_device *ib_dev, u8 port,
				      struct ib_gid_table *table)
804
{
805 806 807 808 809 810
	unsigned int i;
	unsigned long roce_gid_type_mask;
	unsigned int num_default_gids;

	roce_gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
	num_default_gids = hweight_long(roce_gid_type_mask);
811 812 813
	/* Reserve starting indices for default GIDs */
	for (i = 0; i < num_default_gids && i < table->sz; i++)
		table->default_gid_indices |= BIT(i);
814 815
}

816 817 818 819 820 821 822 823 824 825 826

static void gid_table_release_one(struct ib_device *ib_dev)
{
	struct ib_gid_table *table;
	u8 port;

	for (port = 0; port < ib_dev->phys_port_cnt; port++) {
		table = ib_dev->cache.ports[port].gid;
		release_gid_table(table);
		ib_dev->cache.ports[port].gid = NULL;
	}
827 828 829 830 831
}

static int _gid_table_setup_one(struct ib_device *ib_dev)
{
	u8 port;
832
	struct ib_gid_table *table;
833 834 835 836

	for (port = 0; port < ib_dev->phys_port_cnt; port++) {
		u8 rdma_port = port + rdma_start_port(ib_dev);

837
		table =	alloc_gid_table(
838
				ib_dev->port_immutable[rdma_port].gid_tbl_len);
839
		if (!table)
840 841
			goto rollback_table_setup;

842
		gid_table_reserve_default(ib_dev, rdma_port, table);
843
		ib_dev->cache.ports[port].gid = table;
844 845 846 847
	}
	return 0;

rollback_table_setup:
848 849
	gid_table_release_one(ib_dev);
	return -ENOMEM;
850 851 852 853
}

static void gid_table_cleanup_one(struct ib_device *ib_dev)
{
854
	struct ib_gid_table *table;
855 856
	u8 port;

857 858
	for (port = 0; port < ib_dev->phys_port_cnt; port++) {
		table = ib_dev->cache.ports[port].gid;
859
		cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
860 861
				       table);
	}
862 863 864 865 866 867 868 869 870 871 872
}

static int gid_table_setup_one(struct ib_device *ib_dev)
{
	int err;

	err = _gid_table_setup_one(ib_dev);

	if (err)
		return err;

873
	rdma_roce_rescan_device(ib_dev);
874 875 876 877 878 879 880

	return err;
}

int ib_get_cached_gid(struct ib_device *device,
		      u8                port_num,
		      int               index,
881 882
		      union ib_gid     *gid,
		      struct ib_gid_attr *gid_attr)
883
{
884 885
	int res;
	unsigned long flags;
886
	struct ib_gid_table *table;
887

888
	if (!rdma_is_port_valid(device, port_num))
889 890
		return -EINVAL;

891
	table = rdma_gid_table(device, port_num);
892 893 894 895 896
	read_lock_irqsave(&table->rwlock, flags);
	res = __ib_cache_gid_get(device, port_num, index, gid, gid_attr);
	read_unlock_irqrestore(&table->rwlock, flags);

	return res;
897 898 899
}
EXPORT_SYMBOL(ib_get_cached_gid);

900 901 902 903 904 905 906 907 908 909 910 911 912 913
/**
 * ib_find_cached_gid - Returns the port number and GID table index where
 *   a specified GID value occurs.
 * @device: The device to query.
 * @gid: The GID value to search for.
 * @gid_type: The GID type to search for.
 * @ndev: In RoCE, the net device of the device. NULL means ignore.
 * @port_num: The port number of the device where the GID value was found.
 * @index: The index into the cached GID table where the GID was found.  This
 *   parameter may be NULL.
 *
 * ib_find_cached_gid() searches for the specified GID value in
 * the local software cache.
 */
914 915
int ib_find_cached_gid(struct ib_device *device,
		       const union ib_gid *gid,
916
		       enum ib_gid_type gid_type,
917
		       struct net_device *ndev,
918 919 920
		       u8               *port_num,
		       u16              *index)
{
921
	return ib_cache_gid_find(device, gid, gid_type, ndev, port_num, index);
L
Linus Torvalds 已提交
922 923 924
}
EXPORT_SYMBOL(ib_find_cached_gid);

925 926 927 928 929 930 931 932 933
int ib_find_gid_by_filter(struct ib_device *device,
			  const union ib_gid *gid,
			  u8 port_num,
			  bool (*filter)(const union ib_gid *gid,
					 const struct ib_gid_attr *,
					 void *),
			  void *context, u16 *index)
{
	/* Only RoCE GID table supports filter function */
934
	if (!rdma_protocol_roce(device, port_num) && filter)
935 936 937 938 939 940 941
		return -EPROTONOSUPPORT;

	return ib_cache_gid_find_by_filter(device, gid,
					   port_num, filter,
					   context, index);
}

L
Linus Torvalds 已提交
942 943 944 945 946 947 948 949 950
int ib_get_cached_pkey(struct ib_device *device,
		       u8                port_num,
		       int               index,
		       u16              *pkey)
{
	struct ib_pkey_cache *cache;
	unsigned long flags;
	int ret = 0;

951
	if (!rdma_is_port_valid(device, port_num))
L
Linus Torvalds 已提交
952 953 954 955
		return -EINVAL;

	read_lock_irqsave(&device->cache.lock, flags);

956
	cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
L
Linus Torvalds 已提交
957 958 959 960 961 962 963 964 965 966 967 968

	if (index < 0 || index >= cache->table_len)
		ret = -EINVAL;
	else
		*pkey = cache->table[index];

	read_unlock_irqrestore(&device->cache.lock, flags);

	return ret;
}
EXPORT_SYMBOL(ib_get_cached_pkey);

969 970 971 972 973 974 975
int ib_get_cached_subnet_prefix(struct ib_device *device,
				u8                port_num,
				u64              *sn_pfx)
{
	unsigned long flags;
	int p;

P
Parav Pandit 已提交
976
	if (!rdma_is_port_valid(device, port_num))
977 978 979 980 981 982 983 984 985 986 987
		return -EINVAL;

	p = port_num - rdma_start_port(device);
	read_lock_irqsave(&device->cache.lock, flags);
	*sn_pfx = device->cache.ports[p].subnet_prefix;
	read_unlock_irqrestore(&device->cache.lock, flags);

	return 0;
}
EXPORT_SYMBOL(ib_get_cached_subnet_prefix);

L
Linus Torvalds 已提交
988 989 990 991 992 993 994 995 996
int ib_find_cached_pkey(struct ib_device *device,
			u8                port_num,
			u16               pkey,
			u16              *index)
{
	struct ib_pkey_cache *cache;
	unsigned long flags;
	int i;
	int ret = -ENOENT;
997
	int partial_ix = -1;
L
Linus Torvalds 已提交
998

999
	if (!rdma_is_port_valid(device, port_num))
L
Linus Torvalds 已提交
1000 1001 1002 1003
		return -EINVAL;

	read_lock_irqsave(&device->cache.lock, flags);

1004
	cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
L
Linus Torvalds 已提交
1005 1006 1007 1008 1009

	*index = -1;

	for (i = 0; i < cache->table_len; ++i)
		if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
1010 1011 1012 1013 1014 1015
			if (cache->table[i] & 0x8000) {
				*index = i;
				ret = 0;
				break;
			} else
				partial_ix = i;
L
Linus Torvalds 已提交
1016 1017
		}

1018 1019 1020 1021 1022
	if (ret && partial_ix >= 0) {
		*index = partial_ix;
		ret = 0;
	}

L
Linus Torvalds 已提交
1023 1024 1025 1026 1027 1028
	read_unlock_irqrestore(&device->cache.lock, flags);

	return ret;
}
EXPORT_SYMBOL(ib_find_cached_pkey);

1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
int ib_find_exact_cached_pkey(struct ib_device *device,
			      u8                port_num,
			      u16               pkey,
			      u16              *index)
{
	struct ib_pkey_cache *cache;
	unsigned long flags;
	int i;
	int ret = -ENOENT;

1039
	if (!rdma_is_port_valid(device, port_num))
1040 1041 1042 1043
		return -EINVAL;

	read_lock_irqsave(&device->cache.lock, flags);

1044
	cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

	*index = -1;

	for (i = 0; i < cache->table_len; ++i)
		if (cache->table[i] == pkey) {
			*index = i;
			ret = 0;
			break;
		}

	read_unlock_irqrestore(&device->cache.lock, flags);

	return ret;
}
EXPORT_SYMBOL(ib_find_exact_cached_pkey);

J
Jack Morgenstein 已提交
1061 1062 1063 1064 1065 1066 1067
int ib_get_cached_lmc(struct ib_device *device,
		      u8                port_num,
		      u8                *lmc)
{
	unsigned long flags;
	int ret = 0;

1068
	if (!rdma_is_port_valid(device, port_num))
J
Jack Morgenstein 已提交
1069 1070 1071
		return -EINVAL;

	read_lock_irqsave(&device->cache.lock, flags);
1072
	*lmc = device->cache.ports[port_num - rdma_start_port(device)].lmc;
J
Jack Morgenstein 已提交
1073 1074 1075 1076 1077 1078
	read_unlock_irqrestore(&device->cache.lock, flags);

	return ret;
}
EXPORT_SYMBOL(ib_get_cached_lmc);

1079 1080 1081 1082 1083 1084 1085
int ib_get_cached_port_state(struct ib_device   *device,
			     u8                  port_num,
			     enum ib_port_state *port_state)
{
	unsigned long flags;
	int ret = 0;

P
Parav Pandit 已提交
1086
	if (!rdma_is_port_valid(device, port_num))
1087 1088 1089
		return -EINVAL;

	read_lock_irqsave(&device->cache.lock, flags);
1090 1091
	*port_state = device->cache.ports[port_num
		- rdma_start_port(device)].port_state;
1092 1093 1094 1095 1096 1097
	read_unlock_irqrestore(&device->cache.lock, flags);

	return ret;
}
EXPORT_SYMBOL(ib_get_cached_port_state);

1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
static int config_non_roce_gid_cache(struct ib_device *device,
				     u8 port, int gid_tbl_len)
{
	struct ib_gid_attr gid_attr = {};
	struct ib_gid_table *table;
	union ib_gid gid;
	int ret = 0;
	int i;

	gid_attr.device = device;
	gid_attr.port_num = port;
1109
	table = rdma_gid_table(device, port);
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128

	mutex_lock(&table->lock);
	for (i = 0; i < gid_tbl_len; ++i) {
		if (!device->query_gid)
			continue;
		ret = device->query_gid(device, port, i, &gid);
		if (ret) {
			pr_warn("query_gid failed (%d) for %s (index %d)\n",
				ret, device->name, i);
			goto err;
		}
		gid_attr.index = i;
		add_modify_gid(table, &gid, &gid_attr);
	}
err:
	mutex_unlock(&table->lock);
	return ret;
}

L
Linus Torvalds 已提交
1129
static void ib_cache_update(struct ib_device *device,
1130 1131
			    u8                port,
			    bool	      enforce_security)
L
Linus Torvalds 已提交
1132 1133 1134 1135 1136
{
	struct ib_port_attr       *tprops = NULL;
	struct ib_pkey_cache      *pkey_cache = NULL, *old_pkey_cache;
	int                        i;
	int                        ret;
1137 1138
	struct ib_gid_table	  *table;

1139
	if (!rdma_is_port_valid(device, port))
1140 1141
		return;

1142
	table = rdma_gid_table(device, port);
L
Linus Torvalds 已提交
1143 1144 1145 1146 1147 1148 1149

	tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
	if (!tprops)
		return;

	ret = ib_query_port(device, port, tprops);
	if (ret) {
P
Parav Pandit 已提交
1150 1151
		pr_warn("ib_query_port failed (%d) for %s\n",
			ret, device->name);
L
Linus Torvalds 已提交
1152 1153 1154
		goto err;
	}

1155 1156 1157 1158 1159 1160 1161
	if (!rdma_protocol_roce(device, port)) {
		ret = config_non_roce_gid_cache(device, port,
						tprops->gid_tbl_len);
		if (ret)
			goto err;
	}

1162 1163 1164
	pkey_cache = kmalloc(struct_size(pkey_cache, table,
					 tprops->pkey_tbl_len),
			     GFP_KERNEL);
L
Linus Torvalds 已提交
1165 1166 1167 1168 1169 1170 1171 1172
	if (!pkey_cache)
		goto err;

	pkey_cache->table_len = tprops->pkey_tbl_len;

	for (i = 0; i < pkey_cache->table_len; ++i) {
		ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
		if (ret) {
P
Parav Pandit 已提交
1173 1174
			pr_warn("ib_query_pkey failed (%d) for %s (index %d)\n",
				ret, device->name, i);
L
Linus Torvalds 已提交
1175 1176 1177 1178 1179 1180
			goto err;
		}
	}

	write_lock_irq(&device->cache.lock);

1181 1182
	old_pkey_cache = device->cache.ports[port -
		rdma_start_port(device)].pkey;
L
Linus Torvalds 已提交
1183

1184 1185 1186
	device->cache.ports[port - rdma_start_port(device)].pkey = pkey_cache;
	device->cache.ports[port - rdma_start_port(device)].lmc = tprops->lmc;
	device->cache.ports[port - rdma_start_port(device)].port_state =
J
Jack Wang 已提交
1187
		tprops->state;
J
Jack Morgenstein 已提交
1188

1189 1190
	device->cache.ports[port - rdma_start_port(device)].subnet_prefix =
							tprops->subnet_prefix;
L
Linus Torvalds 已提交
1191 1192
	write_unlock_irq(&device->cache.lock);

1193 1194 1195 1196 1197
	if (enforce_security)
		ib_security_cache_change(device,
					 port,
					 tprops->subnet_prefix);

L
Linus Torvalds 已提交
1198 1199 1200 1201 1202 1203 1204 1205 1206
	kfree(old_pkey_cache);
	kfree(tprops);
	return;

err:
	kfree(pkey_cache);
	kfree(tprops);
}

D
David Howells 已提交
1207
static void ib_cache_task(struct work_struct *_work)
L
Linus Torvalds 已提交
1208
{
D
David Howells 已提交
1209 1210
	struct ib_update_work *work =
		container_of(_work, struct ib_update_work, work);
L
Linus Torvalds 已提交
1211

1212 1213 1214
	ib_cache_update(work->device,
			work->port_num,
			work->enforce_security);
L
Linus Torvalds 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
	kfree(work);
}

static void ib_cache_event(struct ib_event_handler *handler,
			   struct ib_event *event)
{
	struct ib_update_work *work;

	if (event->event == IB_EVENT_PORT_ERR    ||
	    event->event == IB_EVENT_PORT_ACTIVE ||
	    event->event == IB_EVENT_LID_CHANGE  ||
	    event->event == IB_EVENT_PKEY_CHANGE ||
1227
	    event->event == IB_EVENT_SM_CHANGE   ||
O
Or Gerlitz 已提交
1228 1229
	    event->event == IB_EVENT_CLIENT_REREGISTER ||
	    event->event == IB_EVENT_GID_CHANGE) {
L
Linus Torvalds 已提交
1230 1231
		work = kmalloc(sizeof *work, GFP_ATOMIC);
		if (work) {
D
David Howells 已提交
1232
			INIT_WORK(&work->work, ib_cache_task);
L
Linus Torvalds 已提交
1233 1234
			work->device   = event->device;
			work->port_num = event->element.port_num;
1235 1236 1237 1238 1239 1240
			if (event->event == IB_EVENT_PKEY_CHANGE ||
			    event->event == IB_EVENT_GID_CHANGE)
				work->enforce_security = true;
			else
				work->enforce_security = false;

T
Tejun Heo 已提交
1241
			queue_work(ib_wq, &work->work);
L
Linus Torvalds 已提交
1242 1243 1244 1245
		}
	}
}

1246
int ib_cache_setup_one(struct ib_device *device)
L
Linus Torvalds 已提交
1247 1248
{
	int p;
1249
	int err;
L
Linus Torvalds 已提交
1250 1251 1252

	rwlock_init(&device->cache.lock);

1253
	device->cache.ports =
K
Kees Cook 已提交
1254 1255 1256
		kcalloc(rdma_end_port(device) - rdma_start_port(device) + 1,
			sizeof(*device->cache.ports),
			GFP_KERNEL);
1257 1258
	if (!device->cache.ports)
		return -ENOMEM;
L
Linus Torvalds 已提交
1259

1260
	err = gid_table_setup_one(device);
1261 1262 1263 1264 1265
	if (err) {
		kfree(device->cache.ports);
		device->cache.ports = NULL;
		return err;
	}
1266

1267
	for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
1268
		ib_cache_update(device, p + rdma_start_port(device), true);
L
Linus Torvalds 已提交
1269 1270 1271

	INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
			      device, ib_cache_event);
1272
	ib_register_event_handler(&device->cache.event_handler);
1273
	return 0;
L
Linus Torvalds 已提交
1274 1275
}

1276
void ib_cache_release_one(struct ib_device *device)
L
Linus Torvalds 已提交
1277 1278 1279
{
	int p;

1280 1281 1282 1283 1284 1285
	/*
	 * The release function frees all the cache elements.
	 * This function should be called as part of freeing
	 * all the device's resources when the cache could no
	 * longer be accessed.
	 */
1286 1287
	for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
		kfree(device->cache.ports[p].pkey);
1288 1289

	gid_table_release_one(device);
1290
	kfree(device->cache.ports);
L
Linus Torvalds 已提交
1291 1292
}

1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
void ib_cache_cleanup_one(struct ib_device *device)
{
	/* The cleanup function unregisters the event handler,
	 * waits for all in-progress workqueue elements and cleans
	 * up the GID cache. This function should be called after
	 * the device was removed from the devices list and all
	 * clients were removed, so the cache exists but is
	 * non-functional and shouldn't be updated anymore.
	 */
	ib_unregister_event_handler(&device->cache.event_handler);
	flush_workqueue(ib_wq);
	gid_table_cleanup_one(device);
}