xref: /linux/drivers/infiniband/core/cache.c (revision af50e4ba34f4c45e92535364133d4deb5931c1c5)
1 /*
2  * Copyright (c) 2004 Topspin Communications.  All rights reserved.
3  * 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.
6  *
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.
34  */
35 
36 #include <linux/module.h>
37 #include <linux/errno.h>
38 #include <linux/slab.h>
39 #include <linux/workqueue.h>
40 #include <linux/netdevice.h>
41 #include <net/addrconf.h>
42 
43 #include <rdma/ib_cache.h>
44 
45 #include "core_priv.h"
46 
47 struct ib_pkey_cache {
48 	int             table_len;
49 	u16             table[0];
50 };
51 
52 struct ib_update_work {
53 	struct work_struct work;
54 	struct ib_device  *device;
55 	u8                 port_num;
56 	bool		   enforce_security;
57 };
58 
59 union ib_gid zgid;
60 EXPORT_SYMBOL(zgid);
61 
62 enum 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,
66 	GID_ATTR_FIND_MASK_GID_TYPE	= 1UL << 3,
67 };
68 
69 enum gid_table_entry_props {
70 	GID_TABLE_ENTRY_INVALID		= 1UL << 0,
71 	GID_TABLE_ENTRY_DEFAULT		= 1UL << 1,
72 };
73 
74 struct ib_gid_table_entry {
75 	unsigned long	    props;
76 	union ib_gid        gid;
77 	struct ib_gid_attr  attr;
78 	void		   *context;
79 };
80 
81 struct 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 	 *
92 	 **/
93 	/* 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.
96 	 */
97 	struct mutex         lock;
98 	/* rwlock protects data_vec[ix]->props. */
99 	rwlock_t	     rwlock;
100 	struct ib_gid_table_entry *data_vec;
101 };
102 
103 static void dispatch_gid_change_event(struct ib_device *ib_dev, u8 port)
104 {
105 	struct ib_event event;
106 
107 	event.device		= ib_dev;
108 	event.element.port_num	= port;
109 	event.event		= IB_EVENT_GID_CHANGE;
110 
111 	ib_dispatch_event(&event);
112 }
113 
114 static const char * const gid_type_str[] = {
115 	[IB_GID_TYPE_IB]	= "IB/RoCE v1",
116 	[IB_GID_TYPE_ROCE_UDP_ENCAP]	= "RoCE v2",
117 };
118 
119 const 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 }
126 EXPORT_SYMBOL(ib_cache_gid_type_str);
127 
128 int ib_cache_gid_parse_type_str(const char *buf)
129 {
130 	unsigned int i;
131 	size_t len;
132 	int err = -EINVAL;
133 
134 	len = strlen(buf);
135 	if (len == 0)
136 		return -EINVAL;
137 
138 	if (buf[len - 1] == '\n')
139 		len--;
140 
141 	for (i = 0; i < ARRAY_SIZE(gid_type_str); ++i)
142 		if (gid_type_str[i] && !strncmp(buf, gid_type_str[i], len) &&
143 		    len == strlen(gid_type_str[i])) {
144 			err = i;
145 			break;
146 		}
147 
148 	return err;
149 }
150 EXPORT_SYMBOL(ib_cache_gid_parse_type_str);
151 
152 static void del_roce_gid(struct ib_device *device, u8 port_num,
153 			 struct ib_gid_table *table, int ix)
154 {
155 	pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
156 		 device->name, port_num, ix,
157 		 table->data_vec[ix].gid.raw);
158 
159 	if (rdma_cap_roce_gid_table(device, port_num))
160 		device->del_gid(&table->data_vec[ix].attr,
161 				&table->data_vec[ix].context);
162 	dev_put(table->data_vec[ix].attr.ndev);
163 }
164 
165 static int add_roce_gid(struct ib_gid_table *table,
166 			const union ib_gid *gid,
167 			const struct ib_gid_attr *attr)
168 {
169 	struct ib_gid_table_entry *entry;
170 	int ix = attr->index;
171 	int ret = 0;
172 
173 	if (!attr->ndev) {
174 		pr_err("%s NULL netdev device=%s port=%d index=%d\n",
175 		       __func__, attr->device->name, attr->port_num,
176 		       attr->index);
177 		return -EINVAL;
178 	}
179 
180 	entry = &table->data_vec[ix];
181 	if ((entry->props & GID_TABLE_ENTRY_INVALID) == 0) {
182 		WARN(1, "GID table corruption device=%s port=%d index=%d\n",
183 		     attr->device->name, attr->port_num,
184 		     attr->index);
185 		return -EINVAL;
186 	}
187 
188 	if (rdma_cap_roce_gid_table(attr->device, attr->port_num)) {
189 		ret = attr->device->add_gid(gid, attr, &entry->context);
190 		if (ret) {
191 			pr_err("%s GID add failed device=%s port=%d index=%d\n",
192 			       __func__, attr->device->name, attr->port_num,
193 			       attr->index);
194 			goto add_err;
195 		}
196 	}
197 	dev_hold(attr->ndev);
198 
199 add_err:
200 	if (!ret)
201 		pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
202 			 attr->device->name, attr->port_num, ix, gid->raw);
203 	return ret;
204 }
205 
206 /**
207  * add_modify_gid - Add or modify GID table entry
208  *
209  * @table:	GID table in which GID to be added or modified
210  * @gid:	GID content
211  * @attr:	Attributes of the GID
212  *
213  * Returns 0 on success or appropriate error code. It accepts zero
214  * GID addition for non RoCE ports for HCA's who report them as valid
215  * GID. However such zero GIDs are not added to the cache.
216  */
217 static int add_modify_gid(struct ib_gid_table *table,
218 			  const union ib_gid *gid,
219 			  const struct ib_gid_attr *attr)
220 {
221 	int ret;
222 
223 	if (rdma_protocol_roce(attr->device, attr->port_num)) {
224 		ret = add_roce_gid(table, gid, attr);
225 		if (ret)
226 			return ret;
227 	} else {
228 		/*
229 		 * Some HCA's report multiple GID entries with only one
230 		 * valid GID, but remaining as zero GID.
231 		 * So ignore such behavior for IB link layer and don't
232 		 * fail the call, but don't add such entry to GID cache.
233 		 */
234 		if (!memcmp(gid, &zgid, sizeof(*gid)))
235 			return 0;
236 	}
237 
238 	lockdep_assert_held(&table->lock);
239 	memcpy(&table->data_vec[attr->index].gid, gid, sizeof(*gid));
240 	memcpy(&table->data_vec[attr->index].attr, attr, sizeof(*attr));
241 
242 	write_lock_irq(&table->rwlock);
243 	table->data_vec[attr->index].props &= ~GID_TABLE_ENTRY_INVALID;
244 	write_unlock_irq(&table->rwlock);
245 	return 0;
246 }
247 
248 /**
249  * del_gid - Delete GID table entry
250  *
251  * @ib_dev:	IB device whose GID entry to be deleted
252  * @port:	Port number of the IB device
253  * @table:	GID table of the IB device for a port
254  * @ix:		GID entry index to delete
255  *
256  */
257 static void del_gid(struct ib_device *ib_dev, u8 port,
258 		    struct ib_gid_table *table, int ix)
259 {
260 	lockdep_assert_held(&table->lock);
261 	write_lock_irq(&table->rwlock);
262 	table->data_vec[ix].props |= GID_TABLE_ENTRY_INVALID;
263 	write_unlock_irq(&table->rwlock);
264 
265 	if (rdma_protocol_roce(ib_dev, port))
266 		del_roce_gid(ib_dev, port, table, ix);
267 	memcpy(&table->data_vec[ix].gid, &zgid, sizeof(zgid));
268 	memset(&table->data_vec[ix].attr, 0, sizeof(table->data_vec[ix].attr));
269 	table->data_vec[ix].context = NULL;
270 }
271 
272 /* rwlock should be read locked, or lock should be held */
273 static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,
274 		    const struct ib_gid_attr *val, bool default_gid,
275 		    unsigned long mask, int *pempty)
276 {
277 	int i = 0;
278 	int found = -1;
279 	int empty = pempty ? -1 : 0;
280 
281 	while (i < table->sz && (found < 0 || empty < 0)) {
282 		struct ib_gid_table_entry *data = &table->data_vec[i];
283 		struct ib_gid_attr *attr = &data->attr;
284 		int curr_index = i;
285 
286 		i++;
287 
288 		/* find_gid() is used during GID addition where it is expected
289 		 * to return a free entry slot which is not duplicate.
290 		 * Free entry slot is requested and returned if pempty is set,
291 		 * so lookup free slot only if requested.
292 		 */
293 		if (pempty && empty < 0) {
294 			if (data->props & GID_TABLE_ENTRY_INVALID) {
295 				/* Found an invalid (free) entry; allocate it */
296 				if (data->props & GID_TABLE_ENTRY_DEFAULT) {
297 					if (default_gid)
298 						empty = curr_index;
299 				} else {
300 					empty = curr_index;
301 				}
302 			}
303 		}
304 
305 		/*
306 		 * Additionally find_gid() is used to find valid entry during
307 		 * lookup operation, where validity needs to be checked. So
308 		 * find the empty entry first to continue to search for a free
309 		 * slot and ignore its INVALID flag.
310 		 */
311 		if (data->props & GID_TABLE_ENTRY_INVALID)
312 			continue;
313 
314 		if (found >= 0)
315 			continue;
316 
317 		if (mask & GID_ATTR_FIND_MASK_GID_TYPE &&
318 		    attr->gid_type != val->gid_type)
319 			continue;
320 
321 		if (mask & GID_ATTR_FIND_MASK_GID &&
322 		    memcmp(gid, &data->gid, sizeof(*gid)))
323 			continue;
324 
325 		if (mask & GID_ATTR_FIND_MASK_NETDEV &&
326 		    attr->ndev != val->ndev)
327 			continue;
328 
329 		if (mask & GID_ATTR_FIND_MASK_DEFAULT &&
330 		    !!(data->props & GID_TABLE_ENTRY_DEFAULT) !=
331 		    default_gid)
332 			continue;
333 
334 		found = curr_index;
335 	}
336 
337 	if (pempty)
338 		*pempty = empty;
339 
340 	return found;
341 }
342 
343 static void make_default_gid(struct  net_device *dev, union ib_gid *gid)
344 {
345 	gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
346 	addrconf_ifid_eui48(&gid->raw[8], dev);
347 }
348 
349 static int __ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
350 			      union ib_gid *gid, struct ib_gid_attr *attr,
351 			      unsigned long mask, bool default_gid)
352 {
353 	struct ib_gid_table *table;
354 	int ret = 0;
355 	int empty;
356 	int ix;
357 
358 	/* Do not allow adding zero GID in support of
359 	 * IB spec version 1.3 section 4.1.1 point (6) and
360 	 * section 12.7.10 and section 12.7.20
361 	 */
362 	if (!memcmp(gid, &zgid, sizeof(*gid)))
363 		return -EINVAL;
364 
365 	table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
366 
367 	mutex_lock(&table->lock);
368 
369 	ix = find_gid(table, gid, attr, default_gid, mask, &empty);
370 	if (ix >= 0)
371 		goto out_unlock;
372 
373 	if (empty < 0) {
374 		ret = -ENOSPC;
375 		goto out_unlock;
376 	}
377 	attr->device = ib_dev;
378 	attr->index = empty;
379 	attr->port_num = port;
380 	ret = add_modify_gid(table, gid, attr);
381 	if (!ret)
382 		dispatch_gid_change_event(ib_dev, port);
383 
384 out_unlock:
385 	mutex_unlock(&table->lock);
386 	if (ret)
387 		pr_warn("%s: unable to add gid %pI6 error=%d\n",
388 			__func__, gid->raw, ret);
389 	return ret;
390 }
391 
392 int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
393 		     union ib_gid *gid, struct ib_gid_attr *attr)
394 {
395 	struct net_device *idev;
396 	unsigned long mask;
397 	int ret;
398 
399 	if (ib_dev->get_netdev) {
400 		idev = ib_dev->get_netdev(ib_dev, port);
401 		if (idev && attr->ndev != idev) {
402 			union ib_gid default_gid;
403 
404 			/* Adding default GIDs in not permitted */
405 			make_default_gid(idev, &default_gid);
406 			if (!memcmp(gid, &default_gid, sizeof(*gid))) {
407 				dev_put(idev);
408 				return -EPERM;
409 			}
410 		}
411 		if (idev)
412 			dev_put(idev);
413 	}
414 
415 	mask = GID_ATTR_FIND_MASK_GID |
416 	       GID_ATTR_FIND_MASK_GID_TYPE |
417 	       GID_ATTR_FIND_MASK_NETDEV;
418 
419 	ret = __ib_cache_gid_add(ib_dev, port, gid, attr, mask, false);
420 	return ret;
421 }
422 
423 int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
424 		     union ib_gid *gid, struct ib_gid_attr *attr)
425 {
426 	struct ib_gid_table *table;
427 	int ret = 0;
428 	int ix;
429 
430 	table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
431 
432 	mutex_lock(&table->lock);
433 
434 	ix = find_gid(table, gid, attr, false,
435 		      GID_ATTR_FIND_MASK_GID	  |
436 		      GID_ATTR_FIND_MASK_GID_TYPE |
437 		      GID_ATTR_FIND_MASK_NETDEV,
438 		      NULL);
439 	if (ix < 0) {
440 		ret = -EINVAL;
441 		goto out_unlock;
442 	}
443 
444 	del_gid(ib_dev, port, table, ix);
445 	dispatch_gid_change_event(ib_dev, port);
446 
447 out_unlock:
448 	mutex_unlock(&table->lock);
449 	if (ret)
450 		pr_debug("%s: can't delete gid %pI6 error=%d\n",
451 			 __func__, gid->raw, ret);
452 	return ret;
453 }
454 
455 int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
456 				     struct net_device *ndev)
457 {
458 	struct ib_gid_table *table;
459 	int ix;
460 	bool deleted = false;
461 
462 	table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
463 
464 	mutex_lock(&table->lock);
465 
466 	for (ix = 0; ix < table->sz; ix++) {
467 		if (table->data_vec[ix].attr.ndev == ndev) {
468 			del_gid(ib_dev, port, table, ix);
469 			deleted = true;
470 		}
471 	}
472 
473 	mutex_unlock(&table->lock);
474 
475 	if (deleted)
476 		dispatch_gid_change_event(ib_dev, port);
477 
478 	return 0;
479 }
480 
481 static int __ib_cache_gid_get(struct ib_device *ib_dev, u8 port, int index,
482 			      union ib_gid *gid, struct ib_gid_attr *attr)
483 {
484 	struct ib_gid_table *table;
485 
486 	table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
487 
488 	if (index < 0 || index >= table->sz)
489 		return -EINVAL;
490 
491 	if (table->data_vec[index].props & GID_TABLE_ENTRY_INVALID)
492 		return -EAGAIN;
493 
494 	memcpy(gid, &table->data_vec[index].gid, sizeof(*gid));
495 	if (attr) {
496 		memcpy(attr, &table->data_vec[index].attr, sizeof(*attr));
497 		if (attr->ndev)
498 			dev_hold(attr->ndev);
499 	}
500 
501 	return 0;
502 }
503 
504 static int _ib_cache_gid_table_find(struct ib_device *ib_dev,
505 				    const union ib_gid *gid,
506 				    const struct ib_gid_attr *val,
507 				    unsigned long mask,
508 				    u8 *port, u16 *index)
509 {
510 	struct ib_gid_table *table;
511 	u8 p;
512 	int local_index;
513 	unsigned long flags;
514 
515 	for (p = 0; p < ib_dev->phys_port_cnt; p++) {
516 		table = ib_dev->cache.ports[p].gid;
517 		read_lock_irqsave(&table->rwlock, flags);
518 		local_index = find_gid(table, gid, val, false, mask, NULL);
519 		if (local_index >= 0) {
520 			if (index)
521 				*index = local_index;
522 			if (port)
523 				*port = p + rdma_start_port(ib_dev);
524 			read_unlock_irqrestore(&table->rwlock, flags);
525 			return 0;
526 		}
527 		read_unlock_irqrestore(&table->rwlock, flags);
528 	}
529 
530 	return -ENOENT;
531 }
532 
533 static int ib_cache_gid_find(struct ib_device *ib_dev,
534 			     const union ib_gid *gid,
535 			     enum ib_gid_type gid_type,
536 			     struct net_device *ndev, u8 *port,
537 			     u16 *index)
538 {
539 	unsigned long mask = GID_ATTR_FIND_MASK_GID |
540 			     GID_ATTR_FIND_MASK_GID_TYPE;
541 	struct ib_gid_attr gid_attr_val = {.ndev = ndev, .gid_type = gid_type};
542 
543 	if (ndev)
544 		mask |= GID_ATTR_FIND_MASK_NETDEV;
545 
546 	return _ib_cache_gid_table_find(ib_dev, gid, &gid_attr_val,
547 					mask, port, index);
548 }
549 
550 /**
551  * ib_find_cached_gid_by_port - Returns the GID table index where a specified
552  * GID value occurs. It searches for the specified GID value in the local
553  * software cache.
554  * @device: The device to query.
555  * @gid: The GID value to search for.
556  * @gid_type: The GID type to search for.
557  * @port_num: The port number of the device where the GID value should be
558  *   searched.
559  * @ndev: In RoCE, the net device of the device. Null means ignore.
560  * @index: The index into the cached GID table where the GID was found. This
561  *   parameter may be NULL.
562  */
563 int ib_find_cached_gid_by_port(struct ib_device *ib_dev,
564 			       const union ib_gid *gid,
565 			       enum ib_gid_type gid_type,
566 			       u8 port, struct net_device *ndev,
567 			       u16 *index)
568 {
569 	int local_index;
570 	struct ib_gid_table *table;
571 	unsigned long mask = GID_ATTR_FIND_MASK_GID |
572 			     GID_ATTR_FIND_MASK_GID_TYPE;
573 	struct ib_gid_attr val = {.ndev = ndev, .gid_type = gid_type};
574 	unsigned long flags;
575 
576 	if (!rdma_is_port_valid(ib_dev, port))
577 		return -ENOENT;
578 
579 	table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
580 
581 	if (ndev)
582 		mask |= GID_ATTR_FIND_MASK_NETDEV;
583 
584 	read_lock_irqsave(&table->rwlock, flags);
585 	local_index = find_gid(table, gid, &val, false, mask, NULL);
586 	if (local_index >= 0) {
587 		if (index)
588 			*index = local_index;
589 		read_unlock_irqrestore(&table->rwlock, flags);
590 		return 0;
591 	}
592 
593 	read_unlock_irqrestore(&table->rwlock, flags);
594 	return -ENOENT;
595 }
596 EXPORT_SYMBOL(ib_find_cached_gid_by_port);
597 
598 /**
599  * ib_cache_gid_find_by_filter - Returns the GID table index where a specified
600  * GID value occurs
601  * @device: The device to query.
602  * @gid: The GID value to search for.
603  * @port_num: The port number of the device where the GID value could be
604  *   searched.
605  * @filter: The filter function is executed on any matching GID in the table.
606  *   If the filter function returns true, the corresponding index is returned,
607  *   otherwise, we continue searching the GID table. It's guaranteed that
608  *   while filter is executed, ndev field is valid and the structure won't
609  *   change. filter is executed in an atomic context. filter must not be NULL.
610  * @index: The index into the cached GID table where the GID was found. This
611  *   parameter may be NULL.
612  *
613  * ib_cache_gid_find_by_filter() searches for the specified GID value
614  * of which the filter function returns true in the port's GID table.
615  * This function is only supported on RoCE ports.
616  *
617  */
618 static int ib_cache_gid_find_by_filter(struct ib_device *ib_dev,
619 				       const union ib_gid *gid,
620 				       u8 port,
621 				       bool (*filter)(const union ib_gid *,
622 						      const struct ib_gid_attr *,
623 						      void *),
624 				       void *context,
625 				       u16 *index)
626 {
627 	struct ib_gid_table *table;
628 	unsigned int i;
629 	unsigned long flags;
630 	bool found = false;
631 
632 
633 	if (!rdma_is_port_valid(ib_dev, port) ||
634 	    !rdma_protocol_roce(ib_dev, port))
635 		return -EPROTONOSUPPORT;
636 
637 	table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
638 
639 	read_lock_irqsave(&table->rwlock, flags);
640 	for (i = 0; i < table->sz; i++) {
641 		struct ib_gid_attr attr;
642 
643 		if (table->data_vec[i].props & GID_TABLE_ENTRY_INVALID)
644 			continue;
645 
646 		if (memcmp(gid, &table->data_vec[i].gid, sizeof(*gid)))
647 			continue;
648 
649 		memcpy(&attr, &table->data_vec[i].attr, sizeof(attr));
650 
651 		if (filter(gid, &attr, context)) {
652 			found = true;
653 			if (index)
654 				*index = i;
655 			break;
656 		}
657 	}
658 	read_unlock_irqrestore(&table->rwlock, flags);
659 
660 	if (!found)
661 		return -ENOENT;
662 	return 0;
663 }
664 
665 static struct ib_gid_table *alloc_gid_table(int sz)
666 {
667 	struct ib_gid_table *table =
668 		kzalloc(sizeof(struct ib_gid_table), GFP_KERNEL);
669 	int i;
670 
671 	if (!table)
672 		return NULL;
673 
674 	table->data_vec = kcalloc(sz, sizeof(*table->data_vec), GFP_KERNEL);
675 	if (!table->data_vec)
676 		goto err_free_table;
677 
678 	mutex_init(&table->lock);
679 
680 	table->sz = sz;
681 	rwlock_init(&table->rwlock);
682 
683 	/* Mark all entries as invalid so that allocator can allocate
684 	 * one of the invalid (free) entry.
685 	 */
686 	for (i = 0; i < sz; i++)
687 		table->data_vec[i].props |= GID_TABLE_ENTRY_INVALID;
688 	return table;
689 
690 err_free_table:
691 	kfree(table);
692 	return NULL;
693 }
694 
695 static void release_gid_table(struct ib_gid_table *table)
696 {
697 	if (table) {
698 		kfree(table->data_vec);
699 		kfree(table);
700 	}
701 }
702 
703 static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port,
704 				   struct ib_gid_table *table)
705 {
706 	int i;
707 	bool deleted = false;
708 
709 	if (!table)
710 		return;
711 
712 	mutex_lock(&table->lock);
713 	for (i = 0; i < table->sz; ++i) {
714 		if (memcmp(&table->data_vec[i].gid, &zgid,
715 			   sizeof(table->data_vec[i].gid))) {
716 			del_gid(ib_dev, port, table, i);
717 			deleted = true;
718 		}
719 	}
720 	mutex_unlock(&table->lock);
721 
722 	if (deleted)
723 		dispatch_gid_change_event(ib_dev, port);
724 }
725 
726 void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
727 				  struct net_device *ndev,
728 				  unsigned long gid_type_mask,
729 				  enum ib_cache_gid_default_mode mode)
730 {
731 	union ib_gid gid;
732 	struct ib_gid_attr gid_attr;
733 	struct ib_gid_table *table;
734 	unsigned int gid_type;
735 	unsigned long mask;
736 
737 	table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
738 
739 	make_default_gid(ndev, &gid);
740 	memset(&gid_attr, 0, sizeof(gid_attr));
741 	gid_attr.ndev = ndev;
742 
743 	for (gid_type = 0; gid_type < IB_GID_TYPE_SIZE; ++gid_type) {
744 		if (1UL << gid_type & ~gid_type_mask)
745 			continue;
746 
747 		gid_attr.gid_type = gid_type;
748 
749 		if (mode == IB_CACHE_GID_DEFAULT_MODE_SET) {
750 			mask = GID_ATTR_FIND_MASK_GID_TYPE |
751 			       GID_ATTR_FIND_MASK_DEFAULT;
752 			__ib_cache_gid_add(ib_dev, port, &gid,
753 					   &gid_attr, mask, true);
754 		} else if (mode == IB_CACHE_GID_DEFAULT_MODE_DELETE) {
755 			ib_cache_gid_del(ib_dev, port, &gid, &gid_attr);
756 		}
757 	}
758 }
759 
760 static int gid_table_reserve_default(struct ib_device *ib_dev, u8 port,
761 				     struct ib_gid_table *table)
762 {
763 	unsigned int i;
764 	unsigned long roce_gid_type_mask;
765 	unsigned int num_default_gids;
766 	unsigned int current_gid = 0;
767 
768 	roce_gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
769 	num_default_gids = hweight_long(roce_gid_type_mask);
770 	for (i = 0; i < num_default_gids && i < table->sz; i++) {
771 		struct ib_gid_table_entry *entry =
772 			&table->data_vec[i];
773 
774 		entry->props |= GID_TABLE_ENTRY_DEFAULT;
775 		current_gid = find_next_bit(&roce_gid_type_mask,
776 					    BITS_PER_LONG,
777 					    current_gid);
778 		entry->attr.gid_type = current_gid++;
779 	}
780 
781 	return 0;
782 }
783 
784 static int _gid_table_setup_one(struct ib_device *ib_dev)
785 {
786 	u8 port;
787 	struct ib_gid_table *table;
788 	int err = 0;
789 
790 	for (port = 0; port < ib_dev->phys_port_cnt; port++) {
791 		u8 rdma_port = port + rdma_start_port(ib_dev);
792 
793 		table =
794 			alloc_gid_table(
795 				ib_dev->port_immutable[rdma_port].gid_tbl_len);
796 		if (!table) {
797 			err = -ENOMEM;
798 			goto rollback_table_setup;
799 		}
800 
801 		err = gid_table_reserve_default(ib_dev,
802 						port + rdma_start_port(ib_dev),
803 						table);
804 		if (err)
805 			goto rollback_table_setup;
806 		ib_dev->cache.ports[port].gid = table;
807 	}
808 
809 	return 0;
810 
811 rollback_table_setup:
812 	for (port = 0; port < ib_dev->phys_port_cnt; port++) {
813 		table = ib_dev->cache.ports[port].gid;
814 
815 		cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
816 				       table);
817 		release_gid_table(table);
818 	}
819 
820 	return err;
821 }
822 
823 static void gid_table_release_one(struct ib_device *ib_dev)
824 {
825 	struct ib_gid_table *table;
826 	u8 port;
827 
828 	for (port = 0; port < ib_dev->phys_port_cnt; port++) {
829 		table = ib_dev->cache.ports[port].gid;
830 		release_gid_table(table);
831 		ib_dev->cache.ports[port].gid = NULL;
832 	}
833 }
834 
835 static void gid_table_cleanup_one(struct ib_device *ib_dev)
836 {
837 	struct ib_gid_table *table;
838 	u8 port;
839 
840 	for (port = 0; port < ib_dev->phys_port_cnt; port++) {
841 		table = ib_dev->cache.ports[port].gid;
842 		cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
843 				       table);
844 	}
845 }
846 
847 static int gid_table_setup_one(struct ib_device *ib_dev)
848 {
849 	int err;
850 
851 	err = _gid_table_setup_one(ib_dev);
852 
853 	if (err)
854 		return err;
855 
856 	rdma_roce_rescan_device(ib_dev);
857 
858 	return err;
859 }
860 
861 int ib_get_cached_gid(struct ib_device *device,
862 		      u8                port_num,
863 		      int               index,
864 		      union ib_gid     *gid,
865 		      struct ib_gid_attr *gid_attr)
866 {
867 	int res;
868 	unsigned long flags;
869 	struct ib_gid_table *table;
870 
871 	if (!rdma_is_port_valid(device, port_num))
872 		return -EINVAL;
873 
874 	table = device->cache.ports[port_num - rdma_start_port(device)].gid;
875 	read_lock_irqsave(&table->rwlock, flags);
876 	res = __ib_cache_gid_get(device, port_num, index, gid, gid_attr);
877 	read_unlock_irqrestore(&table->rwlock, flags);
878 
879 	return res;
880 }
881 EXPORT_SYMBOL(ib_get_cached_gid);
882 
883 /**
884  * ib_find_cached_gid - Returns the port number and GID table index where
885  *   a specified GID value occurs.
886  * @device: The device to query.
887  * @gid: The GID value to search for.
888  * @gid_type: The GID type to search for.
889  * @ndev: In RoCE, the net device of the device. NULL means ignore.
890  * @port_num: The port number of the device where the GID value was found.
891  * @index: The index into the cached GID table where the GID was found.  This
892  *   parameter may be NULL.
893  *
894  * ib_find_cached_gid() searches for the specified GID value in
895  * the local software cache.
896  */
897 int ib_find_cached_gid(struct ib_device *device,
898 		       const union ib_gid *gid,
899 		       enum ib_gid_type gid_type,
900 		       struct net_device *ndev,
901 		       u8               *port_num,
902 		       u16              *index)
903 {
904 	return ib_cache_gid_find(device, gid, gid_type, ndev, port_num, index);
905 }
906 EXPORT_SYMBOL(ib_find_cached_gid);
907 
908 int ib_find_gid_by_filter(struct ib_device *device,
909 			  const union ib_gid *gid,
910 			  u8 port_num,
911 			  bool (*filter)(const union ib_gid *gid,
912 					 const struct ib_gid_attr *,
913 					 void *),
914 			  void *context, u16 *index)
915 {
916 	/* Only RoCE GID table supports filter function */
917 	if (!rdma_protocol_roce(device, port_num) && filter)
918 		return -EPROTONOSUPPORT;
919 
920 	return ib_cache_gid_find_by_filter(device, gid,
921 					   port_num, filter,
922 					   context, index);
923 }
924 
925 int ib_get_cached_pkey(struct ib_device *device,
926 		       u8                port_num,
927 		       int               index,
928 		       u16              *pkey)
929 {
930 	struct ib_pkey_cache *cache;
931 	unsigned long flags;
932 	int ret = 0;
933 
934 	if (!rdma_is_port_valid(device, port_num))
935 		return -EINVAL;
936 
937 	read_lock_irqsave(&device->cache.lock, flags);
938 
939 	cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
940 
941 	if (index < 0 || index >= cache->table_len)
942 		ret = -EINVAL;
943 	else
944 		*pkey = cache->table[index];
945 
946 	read_unlock_irqrestore(&device->cache.lock, flags);
947 
948 	return ret;
949 }
950 EXPORT_SYMBOL(ib_get_cached_pkey);
951 
952 int ib_get_cached_subnet_prefix(struct ib_device *device,
953 				u8                port_num,
954 				u64              *sn_pfx)
955 {
956 	unsigned long flags;
957 	int p;
958 
959 	if (!rdma_is_port_valid(device, port_num))
960 		return -EINVAL;
961 
962 	p = port_num - rdma_start_port(device);
963 	read_lock_irqsave(&device->cache.lock, flags);
964 	*sn_pfx = device->cache.ports[p].subnet_prefix;
965 	read_unlock_irqrestore(&device->cache.lock, flags);
966 
967 	return 0;
968 }
969 EXPORT_SYMBOL(ib_get_cached_subnet_prefix);
970 
971 int ib_find_cached_pkey(struct ib_device *device,
972 			u8                port_num,
973 			u16               pkey,
974 			u16              *index)
975 {
976 	struct ib_pkey_cache *cache;
977 	unsigned long flags;
978 	int i;
979 	int ret = -ENOENT;
980 	int partial_ix = -1;
981 
982 	if (!rdma_is_port_valid(device, port_num))
983 		return -EINVAL;
984 
985 	read_lock_irqsave(&device->cache.lock, flags);
986 
987 	cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
988 
989 	*index = -1;
990 
991 	for (i = 0; i < cache->table_len; ++i)
992 		if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
993 			if (cache->table[i] & 0x8000) {
994 				*index = i;
995 				ret = 0;
996 				break;
997 			} else
998 				partial_ix = i;
999 		}
1000 
1001 	if (ret && partial_ix >= 0) {
1002 		*index = partial_ix;
1003 		ret = 0;
1004 	}
1005 
1006 	read_unlock_irqrestore(&device->cache.lock, flags);
1007 
1008 	return ret;
1009 }
1010 EXPORT_SYMBOL(ib_find_cached_pkey);
1011 
1012 int ib_find_exact_cached_pkey(struct ib_device *device,
1013 			      u8                port_num,
1014 			      u16               pkey,
1015 			      u16              *index)
1016 {
1017 	struct ib_pkey_cache *cache;
1018 	unsigned long flags;
1019 	int i;
1020 	int ret = -ENOENT;
1021 
1022 	if (!rdma_is_port_valid(device, port_num))
1023 		return -EINVAL;
1024 
1025 	read_lock_irqsave(&device->cache.lock, flags);
1026 
1027 	cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
1028 
1029 	*index = -1;
1030 
1031 	for (i = 0; i < cache->table_len; ++i)
1032 		if (cache->table[i] == pkey) {
1033 			*index = i;
1034 			ret = 0;
1035 			break;
1036 		}
1037 
1038 	read_unlock_irqrestore(&device->cache.lock, flags);
1039 
1040 	return ret;
1041 }
1042 EXPORT_SYMBOL(ib_find_exact_cached_pkey);
1043 
1044 int ib_get_cached_lmc(struct ib_device *device,
1045 		      u8                port_num,
1046 		      u8                *lmc)
1047 {
1048 	unsigned long flags;
1049 	int ret = 0;
1050 
1051 	if (!rdma_is_port_valid(device, port_num))
1052 		return -EINVAL;
1053 
1054 	read_lock_irqsave(&device->cache.lock, flags);
1055 	*lmc = device->cache.ports[port_num - rdma_start_port(device)].lmc;
1056 	read_unlock_irqrestore(&device->cache.lock, flags);
1057 
1058 	return ret;
1059 }
1060 EXPORT_SYMBOL(ib_get_cached_lmc);
1061 
1062 int ib_get_cached_port_state(struct ib_device   *device,
1063 			     u8                  port_num,
1064 			     enum ib_port_state *port_state)
1065 {
1066 	unsigned long flags;
1067 	int ret = 0;
1068 
1069 	if (!rdma_is_port_valid(device, port_num))
1070 		return -EINVAL;
1071 
1072 	read_lock_irqsave(&device->cache.lock, flags);
1073 	*port_state = device->cache.ports[port_num
1074 		- rdma_start_port(device)].port_state;
1075 	read_unlock_irqrestore(&device->cache.lock, flags);
1076 
1077 	return ret;
1078 }
1079 EXPORT_SYMBOL(ib_get_cached_port_state);
1080 
1081 static int config_non_roce_gid_cache(struct ib_device *device,
1082 				     u8 port, int gid_tbl_len)
1083 {
1084 	struct ib_gid_attr gid_attr = {};
1085 	struct ib_gid_table *table;
1086 	union ib_gid gid;
1087 	int ret = 0;
1088 	int i;
1089 
1090 	gid_attr.device = device;
1091 	gid_attr.port_num = port;
1092 	table = device->cache.ports[port - rdma_start_port(device)].gid;
1093 
1094 	mutex_lock(&table->lock);
1095 	for (i = 0; i < gid_tbl_len; ++i) {
1096 		if (!device->query_gid)
1097 			continue;
1098 		ret = device->query_gid(device, port, i, &gid);
1099 		if (ret) {
1100 			pr_warn("query_gid failed (%d) for %s (index %d)\n",
1101 				ret, device->name, i);
1102 			goto err;
1103 		}
1104 		gid_attr.index = i;
1105 		add_modify_gid(table, &gid, &gid_attr);
1106 	}
1107 err:
1108 	mutex_unlock(&table->lock);
1109 	return ret;
1110 }
1111 
1112 static void ib_cache_update(struct ib_device *device,
1113 			    u8                port,
1114 			    bool	      enforce_security)
1115 {
1116 	struct ib_port_attr       *tprops = NULL;
1117 	struct ib_pkey_cache      *pkey_cache = NULL, *old_pkey_cache;
1118 	int                        i;
1119 	int                        ret;
1120 	struct ib_gid_table	  *table;
1121 
1122 	if (!rdma_is_port_valid(device, port))
1123 		return;
1124 
1125 	table = device->cache.ports[port - rdma_start_port(device)].gid;
1126 
1127 	tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
1128 	if (!tprops)
1129 		return;
1130 
1131 	ret = ib_query_port(device, port, tprops);
1132 	if (ret) {
1133 		pr_warn("ib_query_port failed (%d) for %s\n",
1134 			ret, device->name);
1135 		goto err;
1136 	}
1137 
1138 	if (!rdma_protocol_roce(device, port)) {
1139 		ret = config_non_roce_gid_cache(device, port,
1140 						tprops->gid_tbl_len);
1141 		if (ret)
1142 			goto err;
1143 	}
1144 
1145 	pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
1146 			     sizeof *pkey_cache->table, GFP_KERNEL);
1147 	if (!pkey_cache)
1148 		goto err;
1149 
1150 	pkey_cache->table_len = tprops->pkey_tbl_len;
1151 
1152 	for (i = 0; i < pkey_cache->table_len; ++i) {
1153 		ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
1154 		if (ret) {
1155 			pr_warn("ib_query_pkey failed (%d) for %s (index %d)\n",
1156 				ret, device->name, i);
1157 			goto err;
1158 		}
1159 	}
1160 
1161 	write_lock_irq(&device->cache.lock);
1162 
1163 	old_pkey_cache = device->cache.ports[port -
1164 		rdma_start_port(device)].pkey;
1165 
1166 	device->cache.ports[port - rdma_start_port(device)].pkey = pkey_cache;
1167 	device->cache.ports[port - rdma_start_port(device)].lmc = tprops->lmc;
1168 	device->cache.ports[port - rdma_start_port(device)].port_state =
1169 		tprops->state;
1170 
1171 	device->cache.ports[port - rdma_start_port(device)].subnet_prefix =
1172 							tprops->subnet_prefix;
1173 	write_unlock_irq(&device->cache.lock);
1174 
1175 	if (enforce_security)
1176 		ib_security_cache_change(device,
1177 					 port,
1178 					 tprops->subnet_prefix);
1179 
1180 	kfree(old_pkey_cache);
1181 	kfree(tprops);
1182 	return;
1183 
1184 err:
1185 	kfree(pkey_cache);
1186 	kfree(tprops);
1187 }
1188 
1189 static void ib_cache_task(struct work_struct *_work)
1190 {
1191 	struct ib_update_work *work =
1192 		container_of(_work, struct ib_update_work, work);
1193 
1194 	ib_cache_update(work->device,
1195 			work->port_num,
1196 			work->enforce_security);
1197 	kfree(work);
1198 }
1199 
1200 static void ib_cache_event(struct ib_event_handler *handler,
1201 			   struct ib_event *event)
1202 {
1203 	struct ib_update_work *work;
1204 
1205 	if (event->event == IB_EVENT_PORT_ERR    ||
1206 	    event->event == IB_EVENT_PORT_ACTIVE ||
1207 	    event->event == IB_EVENT_LID_CHANGE  ||
1208 	    event->event == IB_EVENT_PKEY_CHANGE ||
1209 	    event->event == IB_EVENT_SM_CHANGE   ||
1210 	    event->event == IB_EVENT_CLIENT_REREGISTER ||
1211 	    event->event == IB_EVENT_GID_CHANGE) {
1212 		work = kmalloc(sizeof *work, GFP_ATOMIC);
1213 		if (work) {
1214 			INIT_WORK(&work->work, ib_cache_task);
1215 			work->device   = event->device;
1216 			work->port_num = event->element.port_num;
1217 			if (event->event == IB_EVENT_PKEY_CHANGE ||
1218 			    event->event == IB_EVENT_GID_CHANGE)
1219 				work->enforce_security = true;
1220 			else
1221 				work->enforce_security = false;
1222 
1223 			queue_work(ib_wq, &work->work);
1224 		}
1225 	}
1226 }
1227 
1228 int ib_cache_setup_one(struct ib_device *device)
1229 {
1230 	int p;
1231 	int err;
1232 
1233 	rwlock_init(&device->cache.lock);
1234 
1235 	device->cache.ports =
1236 		kzalloc(sizeof(*device->cache.ports) *
1237 			(rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL);
1238 	if (!device->cache.ports)
1239 		return -ENOMEM;
1240 
1241 	err = gid_table_setup_one(device);
1242 	if (err) {
1243 		kfree(device->cache.ports);
1244 		device->cache.ports = NULL;
1245 		return err;
1246 	}
1247 
1248 	for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
1249 		ib_cache_update(device, p + rdma_start_port(device), true);
1250 
1251 	INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
1252 			      device, ib_cache_event);
1253 	ib_register_event_handler(&device->cache.event_handler);
1254 	return 0;
1255 }
1256 
1257 void ib_cache_release_one(struct ib_device *device)
1258 {
1259 	int p;
1260 
1261 	/*
1262 	 * The release function frees all the cache elements.
1263 	 * This function should be called as part of freeing
1264 	 * all the device's resources when the cache could no
1265 	 * longer be accessed.
1266 	 */
1267 	for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
1268 		kfree(device->cache.ports[p].pkey);
1269 
1270 	gid_table_release_one(device);
1271 	kfree(device->cache.ports);
1272 }
1273 
1274 void ib_cache_cleanup_one(struct ib_device *device)
1275 {
1276 	/* The cleanup function unregisters the event handler,
1277 	 * waits for all in-progress workqueue elements and cleans
1278 	 * up the GID cache. This function should be called after
1279 	 * the device was removed from the devices list and all
1280 	 * clients were removed, so the cache exists but is
1281 	 * non-functional and shouldn't be updated anymore.
1282 	 */
1283 	ib_unregister_event_handler(&device->cache.event_handler);
1284 	flush_workqueue(ib_wq);
1285 	gid_table_cleanup_one(device);
1286 }
1287 
1288 void __init ib_cache_setup(void)
1289 {
1290 	roce_gid_mgmt_init();
1291 }
1292 
1293 void __exit ib_cache_cleanup(void)
1294 {
1295 	roce_gid_mgmt_cleanup();
1296 }
1297