cache.c 39.4 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
enum gid_table_entry_state {
	GID_TABLE_ENTRY_INVALID		= 1,
	GID_TABLE_ENTRY_VALID		= 2,
	/*
	 * Indicates that entry is pending to be removed, there may
	 * be active users of this GID entry.
	 * When last user of the GID entry releases reference to it,
	 * GID entry is detached from the table.
	 */
	GID_TABLE_ENTRY_PENDING_DEL	= 3,
79 80 81
};

struct ib_gid_table_entry {
82 83 84 85 86
	struct kref			kref;
	struct work_struct		del_work;
	struct ib_gid_attr		attr;
	void				*context;
	enum gid_table_entry_state	state;
87 88 89
};

struct ib_gid_table {
90
	int				sz;
91 92 93 94 95 96 97 98 99 100
	/* 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.
	 *
	 **/
101
	/* Any writer to data_vec must hold this lock and the write side of
102
	 * rwlock. Readers must hold only rwlock. All writers must be in a
103
	 * sleepable context.
104
	 */
105
	struct mutex			lock;
106 107
	/* rwlock protects data_vec[ix]->state and entry pointer.
	 */
108
	rwlock_t			rwlock;
109
	struct ib_gid_table_entry	**data_vec;
110 111
	/* bit field, each bit indicates the index of default GID */
	u32				default_gid_indices;
112 113
};

114 115
static void dispatch_gid_change_event(struct ib_device *ib_dev, u8 port)
{
116
	struct ib_event event;
117

118 119 120
	event.device		= ib_dev;
	event.element.port_num	= port;
	event.event		= IB_EVENT_GID_CHANGE;
121

122
	ib_dispatch_event(&event);
123 124
}

125 126
static const char * const gid_type_str[] = {
	[IB_GID_TYPE_IB]	= "IB/RoCE v1",
127
	[IB_GID_TYPE_ROCE_UDP_ENCAP]	= "RoCE v2",
128 129 130 131 132 133 134 135 136 137 138
};

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);

139 140 141 142 143 144 145 146 147 148
/** 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);

149 150 151 152 153 154 155 156 157 158 159 160 161
/** 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 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
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);

186 187 188 189 190
static struct ib_gid_table *rdma_gid_table(struct ib_device *device, u8 port)
{
	return device->cache.ports[port - rdma_start_port(device)].gid;
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
static bool is_gid_entry_free(const struct ib_gid_table_entry *entry)
{
	return !entry;
}

static bool is_gid_entry_valid(const struct ib_gid_table_entry *entry)
{
	return entry && entry->state == GID_TABLE_ENTRY_VALID;
}

static void schedule_free_gid(struct kref *kref)
{
	struct ib_gid_table_entry *entry =
			container_of(kref, struct ib_gid_table_entry, kref);

	queue_work(ib_wq, &entry->del_work);
}

static void free_gid_entry(struct ib_gid_table_entry *entry)
L
Linus Torvalds 已提交
210
{
211 212 213 214
	struct ib_device *device = entry->attr.device;
	u8 port_num = entry->attr.port_num;
	struct ib_gid_table *table = rdma_gid_table(device, port_num);

215
	pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
216 217 218 219 220 221 222 223
		 device->name, port_num, entry->attr.index,
		 entry->attr.gid.raw);

	mutex_lock(&table->lock);
	if (rdma_cap_roce_gid_table(device, port_num) &&
	    entry->state != GID_TABLE_ENTRY_INVALID)
		device->del_gid(&entry->attr, &entry->context);
	write_lock_irq(&table->rwlock);
224

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	/*
	 * The only way to avoid overwriting NULL in table is
	 * by comparing if it is same entry in table or not!
	 * If new entry in table is added by the time we free here,
	 * don't overwrite the table entry.
	 */
	if (entry == table->data_vec[entry->attr.index])
		table->data_vec[entry->attr.index] = NULL;
	/* Now this index is ready to be allocated */
	write_unlock_irq(&table->rwlock);
	mutex_unlock(&table->lock);

	if (entry->attr.ndev)
		dev_put(entry->attr.ndev);
	kfree(entry);
}

/**
 * free_gid_work - Release reference to the GID entry
 * @work: Work structure to refer to GID entry which needs to be
 * deleted.
 *
 * free_gid_work() frees the entry from the HCA's hardware table
 * if provider supports it. It releases reference to netdevice.
 */
static void free_gid_work(struct work_struct *work)
{
	struct ib_gid_table_entry *entry =
		container_of(work, struct ib_gid_table_entry, del_work);
	free_gid_entry(entry);
255
}
256

257 258
static struct ib_gid_table_entry *
alloc_gid_entry(const struct ib_gid_attr *attr)
259 260
{
	struct ib_gid_table_entry *entry;
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
	if (!entry)
		return NULL;
	kref_init(&entry->kref);
	memcpy(&entry->attr, attr, sizeof(*attr));
	if (entry->attr.ndev)
		dev_hold(entry->attr.ndev);
	INIT_WORK(&entry->del_work, free_gid_work);
	entry->state = GID_TABLE_ENTRY_INVALID;
	return entry;
}

static void store_gid_entry(struct ib_gid_table *table,
			    struct ib_gid_table_entry *entry)
{
	entry->state = GID_TABLE_ENTRY_VALID;

	pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
		 entry->attr.device->name, entry->attr.port_num,
		 entry->attr.index, entry->attr.gid.raw);

	lockdep_assert_held(&table->lock);
	write_lock_irq(&table->rwlock);
	table->data_vec[entry->attr.index] = entry;
	write_unlock_irq(&table->rwlock);
}

289 290 291 292 293
static void get_gid_entry(struct ib_gid_table_entry *entry)
{
	kref_get(&entry->kref);
}

294 295 296 297 298 299 300 301 302
static void put_gid_entry(struct ib_gid_table_entry *entry)
{
	kref_put(&entry->kref, schedule_free_gid);
}

static int add_roce_gid(struct ib_gid_table_entry *entry)
{
	const struct ib_gid_attr *attr = &entry->attr;
	int ret;
303

304 305 306 307 308
	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;
309
	}
310
	if (rdma_cap_roce_gid_table(attr->device, attr->port_num)) {
311
		ret = attr->device->add_gid(attr, &entry->context);
312 313 314 315
		if (ret) {
			pr_err("%s GID add failed device=%s port=%d index=%d\n",
			       __func__, attr->device->name, attr->port_num,
			       attr->index);
316
			return ret;
317
		}
318
	}
319
	return 0;
320 321
}

322 323 324 325 326 327 328 329 330 331 332 333 334
/**
 * add_modify_gid - Add or modify GID table entry
 *
 * @table:	GID table in which GID to be added or modified
 * @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 struct ib_gid_attr *attr)
{
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	struct ib_gid_table_entry *entry;
	int ret = 0;

	/*
	 * Invalidate any old entry in the table to make it safe to write to
	 * this index.
	 */
	if (is_gid_entry_valid(table->data_vec[attr->index]))
		put_gid_entry(table->data_vec[attr->index]);

	/*
	 * Some HCA's report multiple GID entries with only one valid GID, and
	 * leave other unused entries as the zero GID. Convert zero GIDs to
	 * empty table entries instead of storing them.
	 */
	if (rdma_is_zero_gid(&attr->gid))
		return 0;

	entry = alloc_gid_entry(attr);
	if (!entry)
		return -ENOMEM;
356 357

	if (rdma_protocol_roce(attr->device, attr->port_num)) {
358
		ret = add_roce_gid(entry);
359
		if (ret)
360
			goto done;
361 362
	}

363
	store_gid_entry(table, entry);
364
	return 0;
365 366 367 368

done:
	put_gid_entry(entry);
	return ret;
369 370
}

371 372 373 374 375 376 377 378 379 380 381 382
/**
 * 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)
{
383 384
	struct ib_gid_table_entry *entry;

385
	lockdep_assert_held(&table->lock);
386 387 388 389 390

	pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
		 ib_dev->name, port, ix,
		 table->data_vec[ix]->attr.gid.raw);

391
	write_lock_irq(&table->rwlock);
392 393 394 395 396 397 398
	entry = table->data_vec[ix];
	entry->state = GID_TABLE_ENTRY_PENDING_DEL;
	/*
	 * For non RoCE protocol, GID entry slot is ready to use.
	 */
	if (!rdma_protocol_roce(ib_dev, port))
		table->data_vec[ix] = NULL;
399 400
	write_unlock_irq(&table->rwlock);

401
	put_gid_entry(entry);
402 403
}

404
/* rwlock should be read locked, or lock should be held */
405 406
static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,
		    const struct ib_gid_attr *val, bool default_gid,
407
		    unsigned long mask, int *pempty)
408
{
409 410 411
	int i = 0;
	int found = -1;
	int empty = pempty ? -1 : 0;
412

413
	while (i < table->sz && (found < 0 || empty < 0)) {
414 415
		struct ib_gid_table_entry *data = table->data_vec[i];
		struct ib_gid_attr *attr;
416
		int curr_index = i;
417

418
		i++;
419

420 421 422 423 424 425
		/* 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) {
426 427 428
			if (is_gid_entry_free(data) &&
			    default_gid ==
				is_gid_index_default(table, curr_index)) {
429 430 431 432 433 434 435 436 437
				/*
				 * 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;
438 439 440 441 442
			}
		}

		/*
		 * Additionally find_gid() is used to find valid entry during
443 444 445
		 * lookup operation; so ignore the entries which are marked as
		 * pending for removal and the entries which are marked as
		 * invalid.
446
		 */
447
		if (!is_gid_entry_valid(data))
448 449 450
			continue;

		if (found >= 0)
451
			continue;
452

453
		attr = &data->attr;
454 455 456 457
		if (mask & GID_ATTR_FIND_MASK_GID_TYPE &&
		    attr->gid_type != val->gid_type)
			continue;

458
		if (mask & GID_ATTR_FIND_MASK_GID &&
459
		    memcmp(gid, &data->attr.gid, sizeof(*gid)))
460
			continue;
461 462 463

		if (mask & GID_ATTR_FIND_MASK_NETDEV &&
		    attr->ndev != val->ndev)
464
			continue;
465 466

		if (mask & GID_ATTR_FIND_MASK_DEFAULT &&
467
		    is_gid_index_default(table, curr_index) != default_gid)
468
			continue;
469

470
		found = curr_index;
471 472
	}

473 474 475 476
	if (pempty)
		*pempty = empty;

	return found;
477 478 479 480 481 482 483 484
}

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);
}

485 486 487
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)
488 489
{
	struct ib_gid_table *table;
L
Linus Torvalds 已提交
490
	int ret = 0;
491
	int empty;
492
	int ix;
L
Linus Torvalds 已提交
493

494 495 496 497
	/* 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
	 */
498
	if (rdma_is_zero_gid(gid))
L
Linus Torvalds 已提交
499 500
		return -EINVAL;

501
	table = rdma_gid_table(ib_dev, port);
502 503 504 505 506 507 508 509 510 511 512 513 514 515

	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;
516 517
	attr->gid = *gid;
	ret = add_modify_gid(table, attr);
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
	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;

536 537 538 539
	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 已提交
540

541 542 543 544 545 546 547 548 549 550
			/* 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 已提交
551

552 553 554
	mask = GID_ATTR_FIND_MASK_GID |
	       GID_ATTR_FIND_MASK_GID_TYPE |
	       GID_ATTR_FIND_MASK_NETDEV;
555

556
	ret = __ib_cache_gid_add(ib_dev, port, gid, attr, mask, false);
L
Linus Torvalds 已提交
557 558 559
	return ret;
}

560 561 562
static int
_ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
		  union ib_gid *gid, struct ib_gid_attr *attr,
563
		  unsigned long mask, bool default_gid)
L
Linus Torvalds 已提交
564
{
565
	struct ib_gid_table *table;
566
	int ret = 0;
567 568
	int ix;

569
	table = rdma_gid_table(ib_dev, port);
570 571 572

	mutex_lock(&table->lock);

573
	ix = find_gid(table, gid, attr, default_gid, mask, NULL);
574 575
	if (ix < 0) {
		ret = -EINVAL;
576
		goto out_unlock;
577
	}
578

579 580
	del_gid(ib_dev, port, table, ix);
	dispatch_gid_change_event(ib_dev, port);
581 582 583

out_unlock:
	mutex_unlock(&table->lock);
584 585 586 587
	if (ret)
		pr_debug("%s: can't delete gid %pI6 error=%d\n",
			 __func__, gid->raw, ret);
	return ret;
588 589
}

590 591 592
int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
		     union ib_gid *gid, struct ib_gid_attr *attr)
{
593 594 595 596 597 598
	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);
599 600
}

601 602 603 604 605
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;
606
	bool deleted = false;
607

608
	table = rdma_gid_table(ib_dev, port);
609 610 611

	mutex_lock(&table->lock);

612
	for (ix = 0; ix < table->sz; ix++) {
613 614
		if (is_gid_entry_valid(table->data_vec[ix]) &&
		    table->data_vec[ix]->attr.ndev == ndev) {
615 616 617 618
			del_gid(ib_dev, port, table, ix);
			deleted = true;
		}
	}
619 620

	mutex_unlock(&table->lock);
621 622 623 624

	if (deleted)
		dispatch_gid_change_event(ib_dev, port);

625 626 627 628 629 630 631
	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 已提交
632

633
	table = rdma_gid_table(ib_dev, port);
L
Linus Torvalds 已提交
634

635 636
	if (index < 0 || index >= table->sz)
		return -EINVAL;
L
Linus Torvalds 已提交
637

638
	if (!is_gid_entry_valid(table->data_vec[index]))
639
		return -EINVAL;
640

641
	memcpy(gid, &table->data_vec[index]->attr.gid, sizeof(*gid));
642
	if (attr) {
643 644
		memcpy(attr, &table->data_vec[index]->attr,
		       sizeof(*attr));
645 646 647 648 649 650 651
		if (attr->ndev)
			dev_hold(attr->ndev);
	}

	return 0;
}

652
/**
653 654 655
 * rdma_find_gid_by_port - Returns the GID entry attributes when it finds
 * a valid GID entry for given search parameters. It searches for the specified
 * GID value in the local software cache.
656 657 658 659 660
 * @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.
661 662 663 664 665
 * @ndev: In RoCE, the net device of the device. NULL means ignore.
 *
 * Returns sgid attributes if the GID is found with valid reference or
 * returns ERR_PTR for the error.
 * The caller must invoke rdma_put_gid_attr() to release the reference.
666
 */
667 668 669 670 671
const struct ib_gid_attr *
rdma_find_gid_by_port(struct ib_device *ib_dev,
		      const union ib_gid *gid,
		      enum ib_gid_type gid_type,
		      u8 port, struct net_device *ndev)
672 673 674
{
	int local_index;
	struct ib_gid_table *table;
675 676 677
	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};
678
	const struct ib_gid_attr *attr;
679
	unsigned long flags;
680

681
	if (!rdma_is_port_valid(ib_dev, port))
682
		return ERR_PTR(-ENOENT);
683

684
	table = rdma_gid_table(ib_dev, port);
685 686 687 688

	if (ndev)
		mask |= GID_ATTR_FIND_MASK_NETDEV;

689
	read_lock_irqsave(&table->rwlock, flags);
690
	local_index = find_gid(table, gid, &val, false, mask, NULL);
691
	if (local_index >= 0) {
692 693
		get_gid_entry(table->data_vec[local_index]);
		attr = &table->data_vec[local_index]->attr;
694
		read_unlock_irqrestore(&table->rwlock, flags);
695
		return attr;
696 697
	}

698
	read_unlock_irqrestore(&table->rwlock, flags);
699
	return ERR_PTR(-ENOENT);
700
}
701
EXPORT_SYMBOL(rdma_find_gid_by_port);
702

703
/**
704 705
 * rdma_find_gid_by_filter - Returns the GID table attribute where a
 * specified GID value occurs
706 707
 * @device: The device to query.
 * @gid: The GID value to search for.
708
 * @port: The port number of the device where the GID value could be
709 710 711 712 713 714 715
 *   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.
 *
716
 * rdma_find_gid_by_filter() searches for the specified GID value
717 718 719 720
 * of which the filter function returns true in the port's GID table.
 * This function is only supported on RoCE ports.
 *
 */
721 722 723 724 725
const struct ib_gid_attr *rdma_find_gid_by_filter(
	struct ib_device *ib_dev, const union ib_gid *gid, u8 port,
	bool (*filter)(const union ib_gid *gid, const struct ib_gid_attr *,
		       void *),
	void *context)
726
{
727
	const struct ib_gid_attr *res = ERR_PTR(-ENOENT);
728
	struct ib_gid_table *table;
729
	unsigned long flags;
730
	unsigned int i;
731

732
	if (!rdma_is_port_valid(ib_dev, port) ||
733
	    !rdma_protocol_roce(ib_dev, port))
734
		return ERR_PTR(-EPROTONOSUPPORT);
735

736
	table = rdma_gid_table(ib_dev, port);
737

738
	read_lock_irqsave(&table->rwlock, flags);
739 740 741
	for (i = 0; i < table->sz; i++) {
		struct ib_gid_attr attr;

742
		if (!is_gid_entry_valid(table->data_vec[i]))
743
			continue;
744

745 746
		if (memcmp(gid, &table->data_vec[i]->attr.gid,
			   sizeof(*gid)))
747
			continue;
748

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

751
		if (filter(gid, &attr, context)) {
752 753
			get_gid_entry(table->data_vec[i]);
			res = &table->data_vec[i]->attr;
754
			break;
755
		}
756
	}
757
	read_unlock_irqrestore(&table->rwlock, flags);
758 759 760 761 762 763 764 765 766 767
	return res;
}

int ib_find_cached_gid_by_port(struct ib_device *ib_dev,
			       const union ib_gid *gid,
			       enum ib_gid_type gid_type,
			       u8 port, struct net_device *ndev,
			       u16 *index)
{
	const struct ib_gid_attr *res;
768

769 770 771 772 773 774 775
	res = rdma_find_gid_by_port(ib_dev, gid, gid_type, port, ndev);
	if (IS_ERR(res))
		return PTR_ERR(res);

	if (index)
		*index = res->index;
	rdma_put_gid_attr(res);
776
	return 0;
777

778
}
779
EXPORT_SYMBOL(ib_find_cached_gid_by_port);
780

781 782
static struct ib_gid_table *alloc_gid_table(int sz)
{
783
	struct ib_gid_table *table = kzalloc(sizeof(*table), GFP_KERNEL);
784

785 786 787 788 789 790 791 792 793 794
	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;
795
	rwlock_init(&table->rwlock);
796 797 798 799 800 801 802
	return table;

err_free_table:
	kfree(table);
	return NULL;
}

803 804
static void release_gid_table(struct ib_device *device, u8 port,
			      struct ib_gid_table *table)
805
{
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
	bool leak = false;
	int i;

	if (!table)
		return;

	for (i = 0; i < table->sz; i++) {
		if (is_gid_entry_free(table->data_vec[i]))
			continue;
		if (kref_read(&table->data_vec[i]->kref) > 1) {
			pr_err("GID entry ref leak for %s (index %d) ref=%d\n",
			       device->name, i,
			       kref_read(&table->data_vec[i]->kref));
			leak = true;
		}
821
	}
822 823 824 825 826
	if (leak)
		return;

	kfree(table->data_vec);
	kfree(table);
827 828 829 830 831 832
}

static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port,
				   struct ib_gid_table *table)
{
	int i;
833
	bool deleted = false;
834 835 836 837

	if (!table)
		return;

838
	mutex_lock(&table->lock);
839
	for (i = 0; i < table->sz; ++i) {
840
		if (is_gid_entry_valid(table->data_vec[i])) {
841 842 843
			del_gid(ib_dev, port, table, i);
			deleted = true;
		}
844
	}
845
	mutex_unlock(&table->lock);
846 847 848

	if (deleted)
		dispatch_gid_change_event(ib_dev, port);
849 850 851 852
}

void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
				  struct net_device *ndev,
853
				  unsigned long gid_type_mask,
854 855
				  enum ib_cache_gid_default_mode mode)
{
856
	union ib_gid gid = { };
857 858
	struct ib_gid_attr gid_attr;
	struct ib_gid_table *table;
859
	unsigned int gid_type;
860
	unsigned long mask;
861

862
	table = rdma_gid_table(ib_dev, port);
863

864 865 866
	mask = GID_ATTR_FIND_MASK_GID_TYPE |
	       GID_ATTR_FIND_MASK_DEFAULT |
	       GID_ATTR_FIND_MASK_NETDEV;
867 868 869
	memset(&gid_attr, 0, sizeof(gid_attr));
	gid_attr.ndev = ndev;

870 871 872 873 874 875 876
	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) {
877
			make_default_gid(ndev, &gid);
878 879 880
			__ib_cache_gid_add(ib_dev, port, &gid,
					   &gid_attr, mask, true);
		} else if (mode == IB_CACHE_GID_DEFAULT_MODE_DELETE) {
881 882
			_ib_cache_gid_del(ib_dev, port, &gid,
					  &gid_attr, mask, true);
883
		}
884
	}
885 886
}

887 888
static void gid_table_reserve_default(struct ib_device *ib_dev, u8 port,
				      struct ib_gid_table *table)
889
{
890 891 892 893 894 895
	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);
896 897 898
	/* Reserve starting indices for default GIDs */
	for (i = 0; i < num_default_gids && i < table->sz; i++)
		table->default_gid_indices |= BIT(i);
899 900
}

901 902 903 904 905 906 907 908

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;
909
		release_gid_table(ib_dev, port, table);
910 911
		ib_dev->cache.ports[port].gid = NULL;
	}
912 913 914 915 916
}

static int _gid_table_setup_one(struct ib_device *ib_dev)
{
	u8 port;
917
	struct ib_gid_table *table;
918 919 920 921

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

922
		table =	alloc_gid_table(
923
				ib_dev->port_immutable[rdma_port].gid_tbl_len);
924
		if (!table)
925 926
			goto rollback_table_setup;

927
		gid_table_reserve_default(ib_dev, rdma_port, table);
928
		ib_dev->cache.ports[port].gid = table;
929 930 931 932
	}
	return 0;

rollback_table_setup:
933 934
	gid_table_release_one(ib_dev);
	return -ENOMEM;
935 936 937 938
}

static void gid_table_cleanup_one(struct ib_device *ib_dev)
{
939
	struct ib_gid_table *table;
940 941
	u8 port;

942 943
	for (port = 0; port < ib_dev->phys_port_cnt; port++) {
		table = ib_dev->cache.ports[port].gid;
944
		cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
945 946
				       table);
	}
947 948 949 950 951 952 953 954 955 956 957
}

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

	err = _gid_table_setup_one(ib_dev);

	if (err)
		return err;

958
	rdma_roce_rescan_device(ib_dev);
959 960 961 962 963 964 965

	return err;
}

int ib_get_cached_gid(struct ib_device *device,
		      u8                port_num,
		      int               index,
966 967
		      union ib_gid     *gid,
		      struct ib_gid_attr *gid_attr)
968
{
969 970
	int res;
	unsigned long flags;
971
	struct ib_gid_table *table;
972

973
	if (!rdma_is_port_valid(device, port_num))
974 975
		return -EINVAL;

976
	table = rdma_gid_table(device, port_num);
977 978 979 980 981
	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;
982 983 984
}
EXPORT_SYMBOL(ib_get_cached_gid);

985
/**
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
 * rdma_query_gid - Read the GID content from the GID software cache
 * @device:		Device to query the GID
 * @port_num:		Port number of the device
 * @index:		Index of the GID table entry to read
 * @gid:		Pointer to GID where to store the entry's GID
 *
 * rdma_query_gid() only reads the GID entry content for requested device,
 * port and index. It reads for IB, RoCE and iWarp link layers.  It doesn't
 * hold any reference to the GID table entry in the HCA or software cache.
 *
 * Returns 0 on success or appropriate error code.
 *
 */
int rdma_query_gid(struct ib_device *device, u8 port_num,
		   int index, union ib_gid *gid)
{
	struct ib_gid_table *table;
	unsigned long flags;
	int res = -EINVAL;

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

	table = rdma_gid_table(device, port_num);
	read_lock_irqsave(&table->rwlock, flags);

	if (index < 0 || index >= table->sz ||
	    !is_gid_entry_valid(table->data_vec[index]))
		goto done;

	memcpy(gid, &table->data_vec[index]->attr.gid, sizeof(*gid));
	res = 0;

done:
	read_unlock_irqrestore(&table->rwlock, flags);
	return res;
}
EXPORT_SYMBOL(rdma_query_gid);

/**
 * rdma_find_gid - Returns SGID attributes if the matching GID is found.
1027 1028 1029 1030 1031
 * @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.
 *
1032 1033 1034 1035 1036
 * rdma_find_gid() searches for the specified GID value in the software cache.
 *
 * Returns GID attributes if a valid GID is found or returns ERR_PTR for the
 * error. The caller must invoke rdma_put_gid_attr() to release the reference.
 *
1037
 */
1038 1039 1040 1041
const struct ib_gid_attr *rdma_find_gid(struct ib_device *device,
					const union ib_gid *gid,
					enum ib_gid_type gid_type,
					struct net_device *ndev)
1042
{
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
	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};
	u8 p;

	if (ndev)
		mask |= GID_ATTR_FIND_MASK_NETDEV;

	for (p = 0; p < device->phys_port_cnt; p++) {
		struct ib_gid_table *table;
		unsigned long flags;
		int index;

		table = device->cache.ports[p].gid;
		read_lock_irqsave(&table->rwlock, flags);
		index = find_gid(table, gid, &gid_attr_val, false, mask, NULL);
		if (index >= 0) {
			const struct ib_gid_attr *attr;

			get_gid_entry(table->data_vec[index]);
			attr = &table->data_vec[index]->attr;
			read_unlock_irqrestore(&table->rwlock, flags);
			return attr;
		}
		read_unlock_irqrestore(&table->rwlock, flags);
	}

	return ERR_PTR(-ENOENT);
}
EXPORT_SYMBOL(rdma_find_gid);

int ib_find_cached_gid(struct ib_device *device, const union ib_gid *gid,
		       enum ib_gid_type gid_type, struct net_device *ndev,
		       u8 *port_num, u16 *index)
{
	const struct ib_gid_attr *res;

	res = rdma_find_gid(device, gid, gid_type, ndev);
	if (IS_ERR(res))
		return PTR_ERR(res);
	if (port_num)
		*port_num = res->port_num;
	if (index)
		*index = res->index;
	rdma_put_gid_attr(res);
	return 0;
L
Linus Torvalds 已提交
1089 1090 1091
}
EXPORT_SYMBOL(ib_find_cached_gid);

1092 1093 1094 1095 1096 1097 1098 1099
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)
{
1100 1101
	const struct ib_gid_attr *res;

1102
	/* Only RoCE GID table supports filter function */
1103
	if (!rdma_protocol_roce(device, port_num) && filter)
1104 1105
		return -EPROTONOSUPPORT;

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	res = rdma_find_gid_by_filter(device, gid, port_num, filter,
				      context);
	if (IS_ERR(res))
		return PTR_ERR(res);

	if (index)
		*index = res->index;

	rdma_put_gid_attr(res);
	return 0;
1116 1117
}

L
Linus Torvalds 已提交
1118 1119 1120 1121 1122 1123 1124 1125 1126
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;

1127
	if (!rdma_is_port_valid(device, port_num))
L
Linus Torvalds 已提交
1128 1129 1130 1131
		return -EINVAL;

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

1132
	cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
L
Linus Torvalds 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144

	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);

1145 1146 1147 1148 1149 1150 1151
int ib_get_cached_subnet_prefix(struct ib_device *device,
				u8                port_num,
				u64              *sn_pfx)
{
	unsigned long flags;
	int p;

P
Parav Pandit 已提交
1152
	if (!rdma_is_port_valid(device, port_num))
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
		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 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172
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;
1173
	int partial_ix = -1;
L
Linus Torvalds 已提交
1174

1175
	if (!rdma_is_port_valid(device, port_num))
L
Linus Torvalds 已提交
1176 1177 1178 1179
		return -EINVAL;

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

1180
	cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
L
Linus Torvalds 已提交
1181 1182 1183 1184 1185

	*index = -1;

	for (i = 0; i < cache->table_len; ++i)
		if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
1186 1187 1188 1189 1190 1191
			if (cache->table[i] & 0x8000) {
				*index = i;
				ret = 0;
				break;
			} else
				partial_ix = i;
L
Linus Torvalds 已提交
1192 1193
		}

1194 1195 1196 1197 1198
	if (ret && partial_ix >= 0) {
		*index = partial_ix;
		ret = 0;
	}

L
Linus Torvalds 已提交
1199 1200 1201 1202 1203 1204
	read_unlock_irqrestore(&device->cache.lock, flags);

	return ret;
}
EXPORT_SYMBOL(ib_find_cached_pkey);

1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
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;

1215
	if (!rdma_is_port_valid(device, port_num))
1216 1217 1218 1219
		return -EINVAL;

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

1220
	cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236

	*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 已提交
1237 1238 1239 1240 1241 1242 1243
int ib_get_cached_lmc(struct ib_device *device,
		      u8                port_num,
		      u8                *lmc)
{
	unsigned long flags;
	int ret = 0;

1244
	if (!rdma_is_port_valid(device, port_num))
J
Jack Morgenstein 已提交
1245 1246 1247
		return -EINVAL;

	read_lock_irqsave(&device->cache.lock, flags);
1248
	*lmc = device->cache.ports[port_num - rdma_start_port(device)].lmc;
J
Jack Morgenstein 已提交
1249 1250 1251 1252 1253 1254
	read_unlock_irqrestore(&device->cache.lock, flags);

	return ret;
}
EXPORT_SYMBOL(ib_get_cached_lmc);

1255 1256 1257 1258 1259 1260 1261
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 已提交
1262
	if (!rdma_is_port_valid(device, port_num))
1263 1264 1265
		return -EINVAL;

	read_lock_irqsave(&device->cache.lock, flags);
1266 1267
	*port_state = device->cache.ports[port_num
		- rdma_start_port(device)].port_state;
1268 1269 1270 1271 1272 1273
	read_unlock_irqrestore(&device->cache.lock, flags);

	return ret;
}
EXPORT_SYMBOL(ib_get_cached_port_state);

1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
/**
 * rdma_get_gid_attr - Returns GID attributes for a port of a device
 * at a requested gid_index, if a valid GID entry exists.
 * @device:		The device to query.
 * @port_num:		The port number on the device where the GID value
 *			is to be queried.
 * @index:		Index of the GID table entry whose attributes are to
 *                      be queried.
 *
 * rdma_get_gid_attr() acquires reference count of gid attributes from the
 * cached GID table. Caller must invoke rdma_put_gid_attr() to release
 * reference to gid attribute regardless of link layer.
 *
 * Returns pointer to valid gid attribute or ERR_PTR for the appropriate error
 * code.
 */
const struct ib_gid_attr *
rdma_get_gid_attr(struct ib_device *device, u8 port_num, int index)
{
	const struct ib_gid_attr *attr = ERR_PTR(-EINVAL);
	struct ib_gid_table *table;
	unsigned long flags;

	if (!rdma_is_port_valid(device, port_num))
		return ERR_PTR(-EINVAL);

	table = rdma_gid_table(device, port_num);
	if (index < 0 || index >= table->sz)
		return ERR_PTR(-EINVAL);

	read_lock_irqsave(&table->rwlock, flags);
	if (!is_gid_entry_valid(table->data_vec[index]))
		goto done;

	get_gid_entry(table->data_vec[index]);
	attr = &table->data_vec[index]->attr;
done:
	read_unlock_irqrestore(&table->rwlock, flags);
	return attr;
}
EXPORT_SYMBOL(rdma_get_gid_attr);

/**
 * rdma_put_gid_attr - Release reference to the GID attribute
 * @attr:		Pointer to the GID attribute whose reference
 *			needs to be released.
 *
 * rdma_put_gid_attr() must be used to release reference whose
 * reference is acquired using rdma_get_gid_attr() or any APIs
 * which returns a pointer to the ib_gid_attr regardless of link layer
 * of IB or RoCE.
 *
 */
void rdma_put_gid_attr(const struct ib_gid_attr *attr)
{
	struct ib_gid_table_entry *entry =
		container_of(attr, struct ib_gid_table_entry, attr);

	put_gid_entry(entry);
}
EXPORT_SYMBOL(rdma_put_gid_attr);

/**
 * rdma_hold_gid_attr - Get reference to existing GID attribute
 *
 * @attr:		Pointer to the GID attribute whose reference
 *			needs to be taken.
 *
 * Increase the reference count to a GID attribute to keep it from being
 * freed. Callers are required to already be holding a reference to attribute.
 *
 */
void rdma_hold_gid_attr(const struct ib_gid_attr *attr)
{
	struct ib_gid_table_entry *entry =
		container_of(attr, struct ib_gid_table_entry, attr);

	get_gid_entry(entry);
}
EXPORT_SYMBOL(rdma_hold_gid_attr);

1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
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;
	int ret = 0;
	int i;

	gid_attr.device = device;
	gid_attr.port_num = port;
1365
	table = rdma_gid_table(device, port);
1366 1367 1368 1369 1370

	mutex_lock(&table->lock);
	for (i = 0; i < gid_tbl_len; ++i) {
		if (!device->query_gid)
			continue;
1371
		ret = device->query_gid(device, port, i, &gid_attr.gid);
1372 1373 1374 1375 1376 1377
		if (ret) {
			pr_warn("query_gid failed (%d) for %s (index %d)\n",
				ret, device->name, i);
			goto err;
		}
		gid_attr.index = i;
1378
		add_modify_gid(table, &gid_attr);
1379 1380 1381 1382 1383 1384
	}
err:
	mutex_unlock(&table->lock);
	return ret;
}

L
Linus Torvalds 已提交
1385
static void ib_cache_update(struct ib_device *device,
1386 1387
			    u8                port,
			    bool	      enforce_security)
L
Linus Torvalds 已提交
1388 1389 1390 1391 1392
{
	struct ib_port_attr       *tprops = NULL;
	struct ib_pkey_cache      *pkey_cache = NULL, *old_pkey_cache;
	int                        i;
	int                        ret;
1393 1394
	struct ib_gid_table	  *table;

1395
	if (!rdma_is_port_valid(device, port))
1396 1397
		return;

1398
	table = rdma_gid_table(device, port);
L
Linus Torvalds 已提交
1399 1400 1401 1402 1403 1404 1405

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

	ret = ib_query_port(device, port, tprops);
	if (ret) {
P
Parav Pandit 已提交
1406 1407
		pr_warn("ib_query_port failed (%d) for %s\n",
			ret, device->name);
L
Linus Torvalds 已提交
1408 1409 1410
		goto err;
	}

1411 1412 1413 1414 1415 1416 1417
	if (!rdma_protocol_roce(device, port)) {
		ret = config_non_roce_gid_cache(device, port,
						tprops->gid_tbl_len);
		if (ret)
			goto err;
	}

1418 1419 1420
	pkey_cache = kmalloc(struct_size(pkey_cache, table,
					 tprops->pkey_tbl_len),
			     GFP_KERNEL);
L
Linus Torvalds 已提交
1421 1422 1423 1424 1425 1426 1427 1428
	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 已提交
1429 1430
			pr_warn("ib_query_pkey failed (%d) for %s (index %d)\n",
				ret, device->name, i);
L
Linus Torvalds 已提交
1431 1432 1433 1434 1435 1436
			goto err;
		}
	}

	write_lock_irq(&device->cache.lock);

1437 1438
	old_pkey_cache = device->cache.ports[port -
		rdma_start_port(device)].pkey;
L
Linus Torvalds 已提交
1439

1440 1441 1442
	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 已提交
1443
		tprops->state;
J
Jack Morgenstein 已提交
1444

1445 1446
	device->cache.ports[port - rdma_start_port(device)].subnet_prefix =
							tprops->subnet_prefix;
L
Linus Torvalds 已提交
1447 1448
	write_unlock_irq(&device->cache.lock);

1449 1450 1451 1452 1453
	if (enforce_security)
		ib_security_cache_change(device,
					 port,
					 tprops->subnet_prefix);

L
Linus Torvalds 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462
	kfree(old_pkey_cache);
	kfree(tprops);
	return;

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

D
David Howells 已提交
1463
static void ib_cache_task(struct work_struct *_work)
L
Linus Torvalds 已提交
1464
{
D
David Howells 已提交
1465 1466
	struct ib_update_work *work =
		container_of(_work, struct ib_update_work, work);
L
Linus Torvalds 已提交
1467

1468 1469 1470
	ib_cache_update(work->device,
			work->port_num,
			work->enforce_security);
L
Linus Torvalds 已提交
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
	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 ||
1483
	    event->event == IB_EVENT_SM_CHANGE   ||
O
Or Gerlitz 已提交
1484 1485
	    event->event == IB_EVENT_CLIENT_REREGISTER ||
	    event->event == IB_EVENT_GID_CHANGE) {
L
Linus Torvalds 已提交
1486 1487
		work = kmalloc(sizeof *work, GFP_ATOMIC);
		if (work) {
D
David Howells 已提交
1488
			INIT_WORK(&work->work, ib_cache_task);
L
Linus Torvalds 已提交
1489 1490
			work->device   = event->device;
			work->port_num = event->element.port_num;
1491 1492 1493 1494 1495 1496
			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 已提交
1497
			queue_work(ib_wq, &work->work);
L
Linus Torvalds 已提交
1498 1499 1500 1501
		}
	}
}

1502
int ib_cache_setup_one(struct ib_device *device)
L
Linus Torvalds 已提交
1503 1504
{
	int p;
1505
	int err;
L
Linus Torvalds 已提交
1506 1507 1508

	rwlock_init(&device->cache.lock);

1509
	device->cache.ports =
K
Kees Cook 已提交
1510 1511 1512
		kcalloc(rdma_end_port(device) - rdma_start_port(device) + 1,
			sizeof(*device->cache.ports),
			GFP_KERNEL);
1513 1514
	if (!device->cache.ports)
		return -ENOMEM;
L
Linus Torvalds 已提交
1515

1516
	err = gid_table_setup_one(device);
1517 1518 1519 1520 1521
	if (err) {
		kfree(device->cache.ports);
		device->cache.ports = NULL;
		return err;
	}
1522

1523
	for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
1524
		ib_cache_update(device, p + rdma_start_port(device), true);
L
Linus Torvalds 已提交
1525 1526 1527

	INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
			      device, ib_cache_event);
1528
	ib_register_event_handler(&device->cache.event_handler);
1529
	return 0;
L
Linus Torvalds 已提交
1530 1531
}

1532
void ib_cache_release_one(struct ib_device *device)
L
Linus Torvalds 已提交
1533 1534 1535
{
	int p;

1536 1537 1538 1539 1540 1541
	/*
	 * 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.
	 */
1542 1543
	for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
		kfree(device->cache.ports[p].pkey);
1544 1545

	gid_table_release_one(device);
1546
	kfree(device->cache.ports);
L
Linus Torvalds 已提交
1547 1548
}

1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
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);
1561 1562 1563 1564 1565

	/*
	 * Flush the wq second time for any pending GID delete work.
	 */
	flush_workqueue(ib_wq);
1566
}