Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (c) 2004 Topspin Communications. All rights reserved. | |
2a1d9b7f RD |
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. | |
1da177e4 LT |
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. | |
1da177e4 LT |
34 | */ |
35 | ||
1da177e4 LT |
36 | #include <linux/module.h> |
37 | #include <linux/errno.h> | |
38 | #include <linux/slab.h> | |
e8edc6e0 | 39 | #include <linux/workqueue.h> |
03db3a2d MB |
40 | #include <linux/netdevice.h> |
41 | #include <net/addrconf.h> | |
1da177e4 | 42 | |
a4d61e84 | 43 | #include <rdma/ib_cache.h> |
1da177e4 LT |
44 | |
45 | #include "core_priv.h" | |
46 | ||
47 | struct ib_pkey_cache { | |
48 | int table_len; | |
49 | u16 table[0]; | |
50 | }; | |
51 | ||
1da177e4 LT |
52 | struct ib_update_work { |
53 | struct work_struct work; | |
54 | struct ib_device *device; | |
55 | u8 port_num; | |
d291f1a6 | 56 | bool enforce_security; |
1da177e4 LT |
57 | }; |
58 | ||
e26be1bf MS |
59 | union ib_gid zgid; |
60 | EXPORT_SYMBOL(zgid); | |
03db3a2d | 61 | |
03db3a2d MB |
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, | |
b39ffa1d | 66 | GID_ATTR_FIND_MASK_GID_TYPE = 1UL << 3, |
03db3a2d MB |
67 | }; |
68 | ||
b150c386 PP |
69 | enum gid_table_entry_state { |
70 | GID_TABLE_ENTRY_INVALID = 1, | |
71 | GID_TABLE_ENTRY_VALID = 2, | |
72 | /* | |
73 | * Indicates that entry is pending to be removed, there may | |
74 | * be active users of this GID entry. | |
75 | * When last user of the GID entry releases reference to it, | |
76 | * GID entry is detached from the table. | |
77 | */ | |
78 | GID_TABLE_ENTRY_PENDING_DEL = 3, | |
03db3a2d MB |
79 | }; |
80 | ||
03db3a2d | 81 | struct ib_gid_table_entry { |
b150c386 PP |
82 | struct kref kref; |
83 | struct work_struct del_work; | |
84 | struct ib_gid_attr attr; | |
85 | void *context; | |
86 | enum gid_table_entry_state state; | |
03db3a2d MB |
87 | }; |
88 | ||
89 | struct ib_gid_table { | |
1c36cf91 | 90 | int sz; |
03db3a2d MB |
91 | /* In RoCE, adding a GID to the table requires: |
92 | * (a) Find if this GID is already exists. | |
93 | * (b) Find a free space. | |
94 | * (c) Write the new GID | |
95 | * | |
96 | * Delete requires different set of operations: | |
97 | * (a) Find the GID | |
98 | * (b) Delete it. | |
99 | * | |
03db3a2d | 100 | **/ |
598ff6ba | 101 | /* Any writer to data_vec must hold this lock and the write side of |
b150c386 | 102 | * rwlock. Readers must hold only rwlock. All writers must be in a |
598ff6ba | 103 | * sleepable context. |
9c584f04 | 104 | */ |
1c36cf91 | 105 | struct mutex lock; |
b150c386 PP |
106 | /* rwlock protects data_vec[ix]->state and entry pointer. |
107 | */ | |
1c36cf91 | 108 | rwlock_t rwlock; |
b150c386 | 109 | struct ib_gid_table_entry **data_vec; |
1c36cf91 PP |
110 | /* bit field, each bit indicates the index of default GID */ |
111 | u32 default_gid_indices; | |
03db3a2d MB |
112 | }; |
113 | ||
f3906bd3 MB |
114 | static void dispatch_gid_change_event(struct ib_device *ib_dev, u8 port) |
115 | { | |
3401857e | 116 | struct ib_event event; |
f3906bd3 | 117 | |
3401857e PP |
118 | event.device = ib_dev; |
119 | event.element.port_num = port; | |
120 | event.event = IB_EVENT_GID_CHANGE; | |
f3906bd3 | 121 | |
3401857e | 122 | ib_dispatch_event(&event); |
f3906bd3 MB |
123 | } |
124 | ||
b39ffa1d MB |
125 | static const char * const gid_type_str[] = { |
126 | [IB_GID_TYPE_IB] = "IB/RoCE v1", | |
7766a99f | 127 | [IB_GID_TYPE_ROCE_UDP_ENCAP] = "RoCE v2", |
b39ffa1d MB |
128 | }; |
129 | ||
130 | const char *ib_cache_gid_type_str(enum ib_gid_type gid_type) | |
131 | { | |
132 | if (gid_type < ARRAY_SIZE(gid_type_str) && gid_type_str[gid_type]) | |
133 | return gid_type_str[gid_type]; | |
134 | ||
135 | return "Invalid GID type"; | |
136 | } | |
137 | EXPORT_SYMBOL(ib_cache_gid_type_str); | |
138 | ||
25e62655 PP |
139 | /** rdma_is_zero_gid - Check if given GID is zero or not. |
140 | * @gid: GID to check | |
141 | * Returns true if given GID is zero, returns false otherwise. | |
142 | */ | |
143 | bool rdma_is_zero_gid(const union ib_gid *gid) | |
144 | { | |
145 | return !memcmp(gid, &zgid, sizeof(*gid)); | |
146 | } | |
147 | EXPORT_SYMBOL(rdma_is_zero_gid); | |
148 | ||
1c36cf91 PP |
149 | /** is_gid_index_default - Check if a given index belongs to |
150 | * reserved default GIDs or not. | |
151 | * @table: GID table pointer | |
152 | * @index: Index to check in GID table | |
153 | * Returns true if index is one of the reserved default GID index otherwise | |
154 | * returns false. | |
155 | */ | |
156 | static bool is_gid_index_default(const struct ib_gid_table *table, | |
157 | unsigned int index) | |
158 | { | |
159 | return index < 32 && (BIT(index) & table->default_gid_indices); | |
160 | } | |
161 | ||
045959db MB |
162 | int ib_cache_gid_parse_type_str(const char *buf) |
163 | { | |
164 | unsigned int i; | |
165 | size_t len; | |
166 | int err = -EINVAL; | |
167 | ||
168 | len = strlen(buf); | |
169 | if (len == 0) | |
170 | return -EINVAL; | |
171 | ||
172 | if (buf[len - 1] == '\n') | |
173 | len--; | |
174 | ||
175 | for (i = 0; i < ARRAY_SIZE(gid_type_str); ++i) | |
176 | if (gid_type_str[i] && !strncmp(buf, gid_type_str[i], len) && | |
177 | len == strlen(gid_type_str[i])) { | |
178 | err = i; | |
179 | break; | |
180 | } | |
181 | ||
182 | return err; | |
183 | } | |
184 | EXPORT_SYMBOL(ib_cache_gid_parse_type_str); | |
185 | ||
724631a9 PP |
186 | static struct ib_gid_table *rdma_gid_table(struct ib_device *device, u8 port) |
187 | { | |
188 | return device->cache.ports[port - rdma_start_port(device)].gid; | |
189 | } | |
190 | ||
b150c386 PP |
191 | static bool is_gid_entry_free(const struct ib_gid_table_entry *entry) |
192 | { | |
193 | return !entry; | |
194 | } | |
195 | ||
196 | static bool is_gid_entry_valid(const struct ib_gid_table_entry *entry) | |
197 | { | |
198 | return entry && entry->state == GID_TABLE_ENTRY_VALID; | |
199 | } | |
200 | ||
201 | static void schedule_free_gid(struct kref *kref) | |
202 | { | |
203 | struct ib_gid_table_entry *entry = | |
204 | container_of(kref, struct ib_gid_table_entry, kref); | |
205 | ||
206 | queue_work(ib_wq, &entry->del_work); | |
207 | } | |
208 | ||
59d40813 | 209 | static void free_gid_entry_locked(struct ib_gid_table_entry *entry) |
1da177e4 | 210 | { |
b150c386 PP |
211 | struct ib_device *device = entry->attr.device; |
212 | u8 port_num = entry->attr.port_num; | |
213 | struct ib_gid_table *table = rdma_gid_table(device, port_num); | |
214 | ||
598ff6ba | 215 | pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__, |
b150c386 PP |
216 | device->name, port_num, entry->attr.index, |
217 | entry->attr.gid.raw); | |
218 | ||
b150c386 PP |
219 | if (rdma_cap_roce_gid_table(device, port_num) && |
220 | entry->state != GID_TABLE_ENTRY_INVALID) | |
221 | device->del_gid(&entry->attr, &entry->context); | |
59d40813 | 222 | |
b150c386 | 223 | write_lock_irq(&table->rwlock); |
598ff6ba | 224 | |
b150c386 PP |
225 | /* |
226 | * The only way to avoid overwriting NULL in table is | |
227 | * by comparing if it is same entry in table or not! | |
228 | * If new entry in table is added by the time we free here, | |
229 | * don't overwrite the table entry. | |
230 | */ | |
231 | if (entry == table->data_vec[entry->attr.index]) | |
232 | table->data_vec[entry->attr.index] = NULL; | |
233 | /* Now this index is ready to be allocated */ | |
234 | write_unlock_irq(&table->rwlock); | |
b150c386 PP |
235 | |
236 | if (entry->attr.ndev) | |
237 | dev_put(entry->attr.ndev); | |
238 | kfree(entry); | |
239 | } | |
240 | ||
59d40813 PP |
241 | static void free_gid_entry(struct kref *kref) |
242 | { | |
243 | struct ib_gid_table_entry *entry = | |
244 | container_of(kref, struct ib_gid_table_entry, kref); | |
245 | ||
246 | free_gid_entry_locked(entry); | |
247 | } | |
248 | ||
b150c386 PP |
249 | /** |
250 | * free_gid_work - Release reference to the GID entry | |
251 | * @work: Work structure to refer to GID entry which needs to be | |
252 | * deleted. | |
253 | * | |
254 | * free_gid_work() frees the entry from the HCA's hardware table | |
255 | * if provider supports it. It releases reference to netdevice. | |
256 | */ | |
257 | static void free_gid_work(struct work_struct *work) | |
258 | { | |
259 | struct ib_gid_table_entry *entry = | |
260 | container_of(work, struct ib_gid_table_entry, del_work); | |
59d40813 PP |
261 | struct ib_device *device = entry->attr.device; |
262 | u8 port_num = entry->attr.port_num; | |
263 | struct ib_gid_table *table = rdma_gid_table(device, port_num); | |
264 | ||
265 | mutex_lock(&table->lock); | |
266 | free_gid_entry_locked(entry); | |
267 | mutex_unlock(&table->lock); | |
598ff6ba | 268 | } |
03db3a2d | 269 | |
b150c386 PP |
270 | static struct ib_gid_table_entry * |
271 | alloc_gid_entry(const struct ib_gid_attr *attr) | |
598ff6ba PP |
272 | { |
273 | struct ib_gid_table_entry *entry; | |
b150c386 PP |
274 | |
275 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); | |
276 | if (!entry) | |
277 | return NULL; | |
278 | kref_init(&entry->kref); | |
279 | memcpy(&entry->attr, attr, sizeof(*attr)); | |
280 | if (entry->attr.ndev) | |
281 | dev_hold(entry->attr.ndev); | |
282 | INIT_WORK(&entry->del_work, free_gid_work); | |
283 | entry->state = GID_TABLE_ENTRY_INVALID; | |
284 | return entry; | |
285 | } | |
286 | ||
287 | static void store_gid_entry(struct ib_gid_table *table, | |
288 | struct ib_gid_table_entry *entry) | |
289 | { | |
290 | entry->state = GID_TABLE_ENTRY_VALID; | |
291 | ||
292 | pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__, | |
293 | entry->attr.device->name, entry->attr.port_num, | |
294 | entry->attr.index, entry->attr.gid.raw); | |
295 | ||
296 | lockdep_assert_held(&table->lock); | |
297 | write_lock_irq(&table->rwlock); | |
298 | table->data_vec[entry->attr.index] = entry; | |
299 | write_unlock_irq(&table->rwlock); | |
300 | } | |
301 | ||
bf399c2c PP |
302 | static void get_gid_entry(struct ib_gid_table_entry *entry) |
303 | { | |
304 | kref_get(&entry->kref); | |
305 | } | |
306 | ||
b150c386 PP |
307 | static void put_gid_entry(struct ib_gid_table_entry *entry) |
308 | { | |
309 | kref_put(&entry->kref, schedule_free_gid); | |
310 | } | |
311 | ||
59d40813 PP |
312 | static void put_gid_entry_locked(struct ib_gid_table_entry *entry) |
313 | { | |
314 | kref_put(&entry->kref, free_gid_entry); | |
315 | } | |
316 | ||
b150c386 PP |
317 | static int add_roce_gid(struct ib_gid_table_entry *entry) |
318 | { | |
319 | const struct ib_gid_attr *attr = &entry->attr; | |
320 | int ret; | |
03db3a2d | 321 | |
598ff6ba PP |
322 | if (!attr->ndev) { |
323 | pr_err("%s NULL netdev device=%s port=%d index=%d\n", | |
324 | __func__, attr->device->name, attr->port_num, | |
325 | attr->index); | |
326 | return -EINVAL; | |
03db3a2d | 327 | } |
598ff6ba | 328 | if (rdma_cap_roce_gid_table(attr->device, attr->port_num)) { |
f4df9a7c | 329 | ret = attr->device->add_gid(attr, &entry->context); |
598ff6ba PP |
330 | if (ret) { |
331 | pr_err("%s GID add failed device=%s port=%d index=%d\n", | |
332 | __func__, attr->device->name, attr->port_num, | |
333 | attr->index); | |
b150c386 | 334 | return ret; |
598ff6ba | 335 | } |
8e787646 | 336 | } |
b150c386 | 337 | return 0; |
03db3a2d MB |
338 | } |
339 | ||
5c5702e2 PP |
340 | /** |
341 | * del_gid - Delete GID table entry | |
342 | * | |
343 | * @ib_dev: IB device whose GID entry to be deleted | |
344 | * @port: Port number of the IB device | |
345 | * @table: GID table of the IB device for a port | |
346 | * @ix: GID entry index to delete | |
347 | * | |
348 | */ | |
349 | static void del_gid(struct ib_device *ib_dev, u8 port, | |
350 | struct ib_gid_table *table, int ix) | |
351 | { | |
352 | struct ib_gid_table_entry *entry; | |
353 | ||
354 | lockdep_assert_held(&table->lock); | |
355 | ||
356 | pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__, | |
357 | ib_dev->name, port, ix, | |
358 | table->data_vec[ix]->attr.gid.raw); | |
359 | ||
360 | write_lock_irq(&table->rwlock); | |
361 | entry = table->data_vec[ix]; | |
362 | entry->state = GID_TABLE_ENTRY_PENDING_DEL; | |
363 | /* | |
364 | * For non RoCE protocol, GID entry slot is ready to use. | |
365 | */ | |
366 | if (!rdma_protocol_roce(ib_dev, port)) | |
367 | table->data_vec[ix] = NULL; | |
368 | write_unlock_irq(&table->rwlock); | |
369 | ||
370 | put_gid_entry_locked(entry); | |
371 | } | |
372 | ||
598ff6ba PP |
373 | /** |
374 | * add_modify_gid - Add or modify GID table entry | |
375 | * | |
376 | * @table: GID table in which GID to be added or modified | |
598ff6ba PP |
377 | * @attr: Attributes of the GID |
378 | * | |
379 | * Returns 0 on success or appropriate error code. It accepts zero | |
380 | * GID addition for non RoCE ports for HCA's who report them as valid | |
381 | * GID. However such zero GIDs are not added to the cache. | |
382 | */ | |
383 | static int add_modify_gid(struct ib_gid_table *table, | |
598ff6ba PP |
384 | const struct ib_gid_attr *attr) |
385 | { | |
b150c386 PP |
386 | struct ib_gid_table_entry *entry; |
387 | int ret = 0; | |
388 | ||
389 | /* | |
390 | * Invalidate any old entry in the table to make it safe to write to | |
391 | * this index. | |
392 | */ | |
393 | if (is_gid_entry_valid(table->data_vec[attr->index])) | |
5c5702e2 | 394 | del_gid(attr->device, attr->port_num, table, attr->index); |
b150c386 PP |
395 | |
396 | /* | |
397 | * Some HCA's report multiple GID entries with only one valid GID, and | |
398 | * leave other unused entries as the zero GID. Convert zero GIDs to | |
399 | * empty table entries instead of storing them. | |
400 | */ | |
401 | if (rdma_is_zero_gid(&attr->gid)) | |
402 | return 0; | |
403 | ||
404 | entry = alloc_gid_entry(attr); | |
405 | if (!entry) | |
406 | return -ENOMEM; | |
598ff6ba PP |
407 | |
408 | if (rdma_protocol_roce(attr->device, attr->port_num)) { | |
b150c386 | 409 | ret = add_roce_gid(entry); |
598ff6ba | 410 | if (ret) |
b150c386 | 411 | goto done; |
598ff6ba PP |
412 | } |
413 | ||
b150c386 | 414 | store_gid_entry(table, entry); |
598ff6ba | 415 | return 0; |
b150c386 PP |
416 | |
417 | done: | |
418 | put_gid_entry(entry); | |
419 | return ret; | |
03db3a2d MB |
420 | } |
421 | ||
598ff6ba | 422 | /* rwlock should be read locked, or lock should be held */ |
03db3a2d MB |
423 | static int find_gid(struct ib_gid_table *table, const union ib_gid *gid, |
424 | const struct ib_gid_attr *val, bool default_gid, | |
cee3c4d0 | 425 | unsigned long mask, int *pempty) |
03db3a2d | 426 | { |
cee3c4d0 MB |
427 | int i = 0; |
428 | int found = -1; | |
429 | int empty = pempty ? -1 : 0; | |
03db3a2d | 430 | |
cee3c4d0 | 431 | while (i < table->sz && (found < 0 || empty < 0)) { |
b150c386 PP |
432 | struct ib_gid_table_entry *data = table->data_vec[i]; |
433 | struct ib_gid_attr *attr; | |
cee3c4d0 | 434 | int curr_index = i; |
03db3a2d | 435 | |
cee3c4d0 | 436 | i++; |
03db3a2d | 437 | |
598ff6ba PP |
438 | /* find_gid() is used during GID addition where it is expected |
439 | * to return a free entry slot which is not duplicate. | |
440 | * Free entry slot is requested and returned if pempty is set, | |
441 | * so lookup free slot only if requested. | |
442 | */ | |
443 | if (pempty && empty < 0) { | |
b150c386 PP |
444 | if (is_gid_entry_free(data) && |
445 | default_gid == | |
446 | is_gid_index_default(table, curr_index)) { | |
a66ed149 PP |
447 | /* |
448 | * Found an invalid (free) entry; allocate it. | |
449 | * If default GID is requested, then our | |
450 | * found slot must be one of the DEFAULT | |
451 | * reserved slots or we fail. | |
452 | * This ensures that only DEFAULT reserved | |
453 | * slots are used for default property GIDs. | |
454 | */ | |
455 | empty = curr_index; | |
598ff6ba PP |
456 | } |
457 | } | |
458 | ||
459 | /* | |
460 | * Additionally find_gid() is used to find valid entry during | |
b150c386 PP |
461 | * lookup operation; so ignore the entries which are marked as |
462 | * pending for removal and the entries which are marked as | |
463 | * invalid. | |
598ff6ba | 464 | */ |
b150c386 | 465 | if (!is_gid_entry_valid(data)) |
cee3c4d0 MB |
466 | continue; |
467 | ||
cee3c4d0 | 468 | if (found >= 0) |
9c584f04 | 469 | continue; |
03db3a2d | 470 | |
b150c386 | 471 | attr = &data->attr; |
b39ffa1d MB |
472 | if (mask & GID_ATTR_FIND_MASK_GID_TYPE && |
473 | attr->gid_type != val->gid_type) | |
474 | continue; | |
475 | ||
03db3a2d | 476 | if (mask & GID_ATTR_FIND_MASK_GID && |
b150c386 | 477 | memcmp(gid, &data->attr.gid, sizeof(*gid))) |
9c584f04 | 478 | continue; |
03db3a2d MB |
479 | |
480 | if (mask & GID_ATTR_FIND_MASK_NETDEV && | |
481 | attr->ndev != val->ndev) | |
9c584f04 | 482 | continue; |
03db3a2d MB |
483 | |
484 | if (mask & GID_ATTR_FIND_MASK_DEFAULT && | |
1c36cf91 | 485 | is_gid_index_default(table, curr_index) != default_gid) |
9c584f04 | 486 | continue; |
03db3a2d | 487 | |
cee3c4d0 | 488 | found = curr_index; |
03db3a2d MB |
489 | } |
490 | ||
cee3c4d0 MB |
491 | if (pempty) |
492 | *pempty = empty; | |
493 | ||
494 | return found; | |
03db3a2d MB |
495 | } |
496 | ||
497 | static void make_default_gid(struct net_device *dev, union ib_gid *gid) | |
498 | { | |
499 | gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL); | |
500 | addrconf_ifid_eui48(&gid->raw[8], dev); | |
501 | } | |
502 | ||
598ff6ba PP |
503 | static int __ib_cache_gid_add(struct ib_device *ib_dev, u8 port, |
504 | union ib_gid *gid, struct ib_gid_attr *attr, | |
505 | unsigned long mask, bool default_gid) | |
03db3a2d | 506 | { |
03db3a2d | 507 | struct ib_gid_table *table; |
1da177e4 | 508 | int ret = 0; |
cee3c4d0 | 509 | int empty; |
598ff6ba | 510 | int ix; |
1da177e4 | 511 | |
598ff6ba PP |
512 | /* Do not allow adding zero GID in support of |
513 | * IB spec version 1.3 section 4.1.1 point (6) and | |
514 | * section 12.7.10 and section 12.7.20 | |
515 | */ | |
25e62655 | 516 | if (rdma_is_zero_gid(gid)) |
1da177e4 LT |
517 | return -EINVAL; |
518 | ||
724631a9 | 519 | table = rdma_gid_table(ib_dev, port); |
598ff6ba PP |
520 | |
521 | mutex_lock(&table->lock); | |
522 | ||
523 | ix = find_gid(table, gid, attr, default_gid, mask, &empty); | |
524 | if (ix >= 0) | |
525 | goto out_unlock; | |
526 | ||
527 | if (empty < 0) { | |
528 | ret = -ENOSPC; | |
529 | goto out_unlock; | |
530 | } | |
531 | attr->device = ib_dev; | |
532 | attr->index = empty; | |
533 | attr->port_num = port; | |
b150c386 PP |
534 | attr->gid = *gid; |
535 | ret = add_modify_gid(table, attr); | |
598ff6ba PP |
536 | if (!ret) |
537 | dispatch_gid_change_event(ib_dev, port); | |
538 | ||
539 | out_unlock: | |
540 | mutex_unlock(&table->lock); | |
541 | if (ret) | |
542 | pr_warn("%s: unable to add gid %pI6 error=%d\n", | |
543 | __func__, gid->raw, ret); | |
544 | return ret; | |
545 | } | |
546 | ||
547 | int ib_cache_gid_add(struct ib_device *ib_dev, u8 port, | |
548 | union ib_gid *gid, struct ib_gid_attr *attr) | |
549 | { | |
550 | struct net_device *idev; | |
551 | unsigned long mask; | |
552 | int ret; | |
553 | ||
03db3a2d MB |
554 | if (ib_dev->get_netdev) { |
555 | idev = ib_dev->get_netdev(ib_dev, port); | |
556 | if (idev && attr->ndev != idev) { | |
557 | union ib_gid default_gid; | |
1da177e4 | 558 | |
03db3a2d MB |
559 | /* Adding default GIDs in not permitted */ |
560 | make_default_gid(idev, &default_gid); | |
561 | if (!memcmp(gid, &default_gid, sizeof(*gid))) { | |
562 | dev_put(idev); | |
563 | return -EPERM; | |
564 | } | |
565 | } | |
566 | if (idev) | |
567 | dev_put(idev); | |
568 | } | |
1da177e4 | 569 | |
598ff6ba PP |
570 | mask = GID_ATTR_FIND_MASK_GID | |
571 | GID_ATTR_FIND_MASK_GID_TYPE | | |
572 | GID_ATTR_FIND_MASK_NETDEV; | |
03db3a2d | 573 | |
598ff6ba | 574 | ret = __ib_cache_gid_add(ib_dev, port, gid, attr, mask, false); |
1da177e4 LT |
575 | return ret; |
576 | } | |
1da177e4 | 577 | |
22c01ee4 PP |
578 | static int |
579 | _ib_cache_gid_del(struct ib_device *ib_dev, u8 port, | |
580 | union ib_gid *gid, struct ib_gid_attr *attr, | |
dc5640f2 | 581 | unsigned long mask, bool default_gid) |
1da177e4 | 582 | { |
03db3a2d | 583 | struct ib_gid_table *table; |
598ff6ba | 584 | int ret = 0; |
03db3a2d MB |
585 | int ix; |
586 | ||
724631a9 | 587 | table = rdma_gid_table(ib_dev, port); |
03db3a2d MB |
588 | |
589 | mutex_lock(&table->lock); | |
590 | ||
dc5640f2 | 591 | ix = find_gid(table, gid, attr, default_gid, mask, NULL); |
598ff6ba PP |
592 | if (ix < 0) { |
593 | ret = -EINVAL; | |
03db3a2d | 594 | goto out_unlock; |
598ff6ba | 595 | } |
03db3a2d | 596 | |
598ff6ba PP |
597 | del_gid(ib_dev, port, table, ix); |
598 | dispatch_gid_change_event(ib_dev, port); | |
03db3a2d MB |
599 | |
600 | out_unlock: | |
601 | mutex_unlock(&table->lock); | |
598ff6ba PP |
602 | if (ret) |
603 | pr_debug("%s: can't delete gid %pI6 error=%d\n", | |
604 | __func__, gid->raw, ret); | |
605 | return ret; | |
03db3a2d MB |
606 | } |
607 | ||
22c01ee4 PP |
608 | int ib_cache_gid_del(struct ib_device *ib_dev, u8 port, |
609 | union ib_gid *gid, struct ib_gid_attr *attr) | |
610 | { | |
dc5640f2 PP |
611 | unsigned long mask = GID_ATTR_FIND_MASK_GID | |
612 | GID_ATTR_FIND_MASK_GID_TYPE | | |
613 | GID_ATTR_FIND_MASK_DEFAULT | | |
614 | GID_ATTR_FIND_MASK_NETDEV; | |
615 | ||
616 | return _ib_cache_gid_del(ib_dev, port, gid, attr, mask, false); | |
22c01ee4 PP |
617 | } |
618 | ||
03db3a2d MB |
619 | int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port, |
620 | struct net_device *ndev) | |
621 | { | |
03db3a2d MB |
622 | struct ib_gid_table *table; |
623 | int ix; | |
9c584f04 | 624 | bool deleted = false; |
03db3a2d | 625 | |
724631a9 | 626 | table = rdma_gid_table(ib_dev, port); |
03db3a2d MB |
627 | |
628 | mutex_lock(&table->lock); | |
629 | ||
598ff6ba | 630 | for (ix = 0; ix < table->sz; ix++) { |
b150c386 PP |
631 | if (is_gid_entry_valid(table->data_vec[ix]) && |
632 | table->data_vec[ix]->attr.ndev == ndev) { | |
598ff6ba PP |
633 | del_gid(ib_dev, port, table, ix); |
634 | deleted = true; | |
635 | } | |
636 | } | |
03db3a2d MB |
637 | |
638 | mutex_unlock(&table->lock); | |
9c584f04 MB |
639 | |
640 | if (deleted) | |
641 | dispatch_gid_change_event(ib_dev, port); | |
642 | ||
03db3a2d MB |
643 | return 0; |
644 | } | |
645 | ||
6612b498 | 646 | /** |
c3d71b69 JG |
647 | * rdma_find_gid_by_port - Returns the GID entry attributes when it finds |
648 | * a valid GID entry for given search parameters. It searches for the specified | |
649 | * GID value in the local software cache. | |
6612b498 PP |
650 | * @device: The device to query. |
651 | * @gid: The GID value to search for. | |
652 | * @gid_type: The GID type to search for. | |
653 | * @port_num: The port number of the device where the GID value should be | |
654 | * searched. | |
c3d71b69 JG |
655 | * @ndev: In RoCE, the net device of the device. NULL means ignore. |
656 | * | |
657 | * Returns sgid attributes if the GID is found with valid reference or | |
658 | * returns ERR_PTR for the error. | |
659 | * The caller must invoke rdma_put_gid_attr() to release the reference. | |
6612b498 | 660 | */ |
c3d71b69 JG |
661 | const struct ib_gid_attr * |
662 | rdma_find_gid_by_port(struct ib_device *ib_dev, | |
663 | const union ib_gid *gid, | |
664 | enum ib_gid_type gid_type, | |
665 | u8 port, struct net_device *ndev) | |
03db3a2d MB |
666 | { |
667 | int local_index; | |
03db3a2d | 668 | struct ib_gid_table *table; |
b39ffa1d MB |
669 | unsigned long mask = GID_ATTR_FIND_MASK_GID | |
670 | GID_ATTR_FIND_MASK_GID_TYPE; | |
671 | struct ib_gid_attr val = {.ndev = ndev, .gid_type = gid_type}; | |
c3d71b69 | 672 | const struct ib_gid_attr *attr; |
9c584f04 | 673 | unsigned long flags; |
03db3a2d | 674 | |
24dc831b | 675 | if (!rdma_is_port_valid(ib_dev, port)) |
c3d71b69 | 676 | return ERR_PTR(-ENOENT); |
03db3a2d | 677 | |
724631a9 | 678 | table = rdma_gid_table(ib_dev, port); |
03db3a2d MB |
679 | |
680 | if (ndev) | |
681 | mask |= GID_ATTR_FIND_MASK_NETDEV; | |
682 | ||
9c584f04 | 683 | read_lock_irqsave(&table->rwlock, flags); |
cee3c4d0 | 684 | local_index = find_gid(table, gid, &val, false, mask, NULL); |
03db3a2d | 685 | if (local_index >= 0) { |
c3d71b69 JG |
686 | get_gid_entry(table->data_vec[local_index]); |
687 | attr = &table->data_vec[local_index]->attr; | |
9c584f04 | 688 | read_unlock_irqrestore(&table->rwlock, flags); |
c3d71b69 | 689 | return attr; |
03db3a2d MB |
690 | } |
691 | ||
9c584f04 | 692 | read_unlock_irqrestore(&table->rwlock, flags); |
c3d71b69 | 693 | return ERR_PTR(-ENOENT); |
03db3a2d | 694 | } |
c3d71b69 | 695 | EXPORT_SYMBOL(rdma_find_gid_by_port); |
03db3a2d | 696 | |
99b27e3b | 697 | /** |
c3d71b69 JG |
698 | * rdma_find_gid_by_filter - Returns the GID table attribute where a |
699 | * specified GID value occurs | |
99b27e3b MB |
700 | * @device: The device to query. |
701 | * @gid: The GID value to search for. | |
c3d71b69 | 702 | * @port: The port number of the device where the GID value could be |
99b27e3b MB |
703 | * searched. |
704 | * @filter: The filter function is executed on any matching GID in the table. | |
705 | * If the filter function returns true, the corresponding index is returned, | |
706 | * otherwise, we continue searching the GID table. It's guaranteed that | |
707 | * while filter is executed, ndev field is valid and the structure won't | |
708 | * change. filter is executed in an atomic context. filter must not be NULL. | |
99b27e3b | 709 | * |
c3d71b69 | 710 | * rdma_find_gid_by_filter() searches for the specified GID value |
99b27e3b | 711 | * of which the filter function returns true in the port's GID table. |
99b27e3b MB |
712 | * |
713 | */ | |
c3d71b69 JG |
714 | const struct ib_gid_attr *rdma_find_gid_by_filter( |
715 | struct ib_device *ib_dev, const union ib_gid *gid, u8 port, | |
716 | bool (*filter)(const union ib_gid *gid, const struct ib_gid_attr *, | |
717 | void *), | |
718 | void *context) | |
99b27e3b | 719 | { |
c3d71b69 | 720 | const struct ib_gid_attr *res = ERR_PTR(-ENOENT); |
99b27e3b | 721 | struct ib_gid_table *table; |
9c584f04 | 722 | unsigned long flags; |
c3d71b69 | 723 | unsigned int i; |
99b27e3b | 724 | |
83f6f8d2 JG |
725 | if (!rdma_is_port_valid(ib_dev, port)) |
726 | return ERR_PTR(-EINVAL); | |
99b27e3b | 727 | |
724631a9 | 728 | table = rdma_gid_table(ib_dev, port); |
99b27e3b | 729 | |
9c584f04 | 730 | read_lock_irqsave(&table->rwlock, flags); |
99b27e3b | 731 | for (i = 0; i < table->sz; i++) { |
83f6f8d2 | 732 | struct ib_gid_table_entry *entry = table->data_vec[i]; |
99b27e3b | 733 | |
83f6f8d2 | 734 | if (!is_gid_entry_valid(entry)) |
151ed9d7 | 735 | continue; |
99b27e3b | 736 | |
83f6f8d2 | 737 | if (memcmp(gid, &entry->attr.gid, sizeof(*gid))) |
151ed9d7 | 738 | continue; |
99b27e3b | 739 | |
83f6f8d2 JG |
740 | if (filter(gid, &entry->attr, context)) { |
741 | get_gid_entry(entry); | |
742 | res = &entry->attr; | |
99b27e3b | 743 | break; |
151ed9d7 | 744 | } |
99b27e3b | 745 | } |
9c584f04 | 746 | read_unlock_irqrestore(&table->rwlock, flags); |
c3d71b69 JG |
747 | return res; |
748 | } | |
749 | ||
03db3a2d MB |
750 | static struct ib_gid_table *alloc_gid_table(int sz) |
751 | { | |
b150c386 | 752 | struct ib_gid_table *table = kzalloc(sizeof(*table), GFP_KERNEL); |
9c584f04 | 753 | |
03db3a2d MB |
754 | if (!table) |
755 | return NULL; | |
756 | ||
757 | table->data_vec = kcalloc(sz, sizeof(*table->data_vec), GFP_KERNEL); | |
758 | if (!table->data_vec) | |
759 | goto err_free_table; | |
760 | ||
761 | mutex_init(&table->lock); | |
762 | ||
763 | table->sz = sz; | |
9c584f04 | 764 | rwlock_init(&table->rwlock); |
03db3a2d MB |
765 | return table; |
766 | ||
767 | err_free_table: | |
768 | kfree(table); | |
769 | return NULL; | |
770 | } | |
771 | ||
b150c386 PP |
772 | static void release_gid_table(struct ib_device *device, u8 port, |
773 | struct ib_gid_table *table) | |
03db3a2d | 774 | { |
b150c386 PP |
775 | bool leak = false; |
776 | int i; | |
777 | ||
778 | if (!table) | |
779 | return; | |
780 | ||
781 | for (i = 0; i < table->sz; i++) { | |
782 | if (is_gid_entry_free(table->data_vec[i])) | |
783 | continue; | |
784 | if (kref_read(&table->data_vec[i]->kref) > 1) { | |
785 | pr_err("GID entry ref leak for %s (index %d) ref=%d\n", | |
786 | device->name, i, | |
787 | kref_read(&table->data_vec[i]->kref)); | |
788 | leak = true; | |
789 | } | |
03db3a2d | 790 | } |
b150c386 PP |
791 | if (leak) |
792 | return; | |
793 | ||
794 | kfree(table->data_vec); | |
795 | kfree(table); | |
03db3a2d MB |
796 | } |
797 | ||
798 | static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port, | |
799 | struct ib_gid_table *table) | |
800 | { | |
801 | int i; | |
9c584f04 | 802 | bool deleted = false; |
03db3a2d MB |
803 | |
804 | if (!table) | |
805 | return; | |
806 | ||
598ff6ba | 807 | mutex_lock(&table->lock); |
03db3a2d | 808 | for (i = 0; i < table->sz; ++i) { |
b150c386 | 809 | if (is_gid_entry_valid(table->data_vec[i])) { |
598ff6ba PP |
810 | del_gid(ib_dev, port, table, i); |
811 | deleted = true; | |
812 | } | |
03db3a2d | 813 | } |
598ff6ba | 814 | mutex_unlock(&table->lock); |
9c584f04 MB |
815 | |
816 | if (deleted) | |
817 | dispatch_gid_change_event(ib_dev, port); | |
03db3a2d MB |
818 | } |
819 | ||
820 | void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port, | |
821 | struct net_device *ndev, | |
b39ffa1d | 822 | unsigned long gid_type_mask, |
03db3a2d MB |
823 | enum ib_cache_gid_default_mode mode) |
824 | { | |
dc5640f2 | 825 | union ib_gid gid = { }; |
03db3a2d | 826 | struct ib_gid_attr gid_attr; |
b39ffa1d | 827 | unsigned int gid_type; |
598ff6ba | 828 | unsigned long mask; |
03db3a2d | 829 | |
dc5640f2 PP |
830 | mask = GID_ATTR_FIND_MASK_GID_TYPE | |
831 | GID_ATTR_FIND_MASK_DEFAULT | | |
832 | GID_ATTR_FIND_MASK_NETDEV; | |
03db3a2d MB |
833 | memset(&gid_attr, 0, sizeof(gid_attr)); |
834 | gid_attr.ndev = ndev; | |
835 | ||
b39ffa1d | 836 | for (gid_type = 0; gid_type < IB_GID_TYPE_SIZE; ++gid_type) { |
b39ffa1d MB |
837 | if (1UL << gid_type & ~gid_type_mask) |
838 | continue; | |
839 | ||
840 | gid_attr.gid_type = gid_type; | |
841 | ||
b39ffa1d | 842 | if (mode == IB_CACHE_GID_DEFAULT_MODE_SET) { |
dc5640f2 | 843 | make_default_gid(ndev, &gid); |
598ff6ba PP |
844 | __ib_cache_gid_add(ib_dev, port, &gid, |
845 | &gid_attr, mask, true); | |
846 | } else if (mode == IB_CACHE_GID_DEFAULT_MODE_DELETE) { | |
dc5640f2 PP |
847 | _ib_cache_gid_del(ib_dev, port, &gid, |
848 | &gid_attr, mask, true); | |
9c584f04 | 849 | } |
b39ffa1d | 850 | } |
03db3a2d MB |
851 | } |
852 | ||
25a1cd3f PP |
853 | static void gid_table_reserve_default(struct ib_device *ib_dev, u8 port, |
854 | struct ib_gid_table *table) | |
03db3a2d | 855 | { |
b39ffa1d MB |
856 | unsigned int i; |
857 | unsigned long roce_gid_type_mask; | |
858 | unsigned int num_default_gids; | |
b39ffa1d MB |
859 | |
860 | roce_gid_type_mask = roce_gid_type_mask_support(ib_dev, port); | |
861 | num_default_gids = hweight_long(roce_gid_type_mask); | |
1c36cf91 PP |
862 | /* Reserve starting indices for default GIDs */ |
863 | for (i = 0; i < num_default_gids && i < table->sz; i++) | |
864 | table->default_gid_indices |= BIT(i); | |
03db3a2d | 865 | } |
03db3a2d | 866 | |
be0e8f34 PP |
867 | |
868 | static void gid_table_release_one(struct ib_device *ib_dev) | |
869 | { | |
870 | struct ib_gid_table *table; | |
871 | u8 port; | |
872 | ||
873 | for (port = 0; port < ib_dev->phys_port_cnt; port++) { | |
874 | table = ib_dev->cache.ports[port].gid; | |
b150c386 | 875 | release_gid_table(ib_dev, port, table); |
be0e8f34 PP |
876 | ib_dev->cache.ports[port].gid = NULL; |
877 | } | |
03db3a2d MB |
878 | } |
879 | ||
880 | static int _gid_table_setup_one(struct ib_device *ib_dev) | |
881 | { | |
882 | u8 port; | |
21d6454a | 883 | struct ib_gid_table *table; |
03db3a2d | 884 | |
03db3a2d MB |
885 | for (port = 0; port < ib_dev->phys_port_cnt; port++) { |
886 | u8 rdma_port = port + rdma_start_port(ib_dev); | |
887 | ||
25a1cd3f | 888 | table = alloc_gid_table( |
03db3a2d | 889 | ib_dev->port_immutable[rdma_port].gid_tbl_len); |
be0e8f34 | 890 | if (!table) |
03db3a2d | 891 | goto rollback_table_setup; |
03db3a2d | 892 | |
25a1cd3f | 893 | gid_table_reserve_default(ib_dev, rdma_port, table); |
21d6454a | 894 | ib_dev->cache.ports[port].gid = table; |
03db3a2d | 895 | } |
03db3a2d MB |
896 | return 0; |
897 | ||
898 | rollback_table_setup: | |
be0e8f34 PP |
899 | gid_table_release_one(ib_dev); |
900 | return -ENOMEM; | |
03db3a2d MB |
901 | } |
902 | ||
903 | static void gid_table_cleanup_one(struct ib_device *ib_dev) | |
904 | { | |
21d6454a | 905 | struct ib_gid_table *table; |
03db3a2d MB |
906 | u8 port; |
907 | ||
21d6454a JW |
908 | for (port = 0; port < ib_dev->phys_port_cnt; port++) { |
909 | table = ib_dev->cache.ports[port].gid; | |
03db3a2d | 910 | cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev), |
21d6454a JW |
911 | table); |
912 | } | |
03db3a2d MB |
913 | } |
914 | ||
915 | static int gid_table_setup_one(struct ib_device *ib_dev) | |
916 | { | |
917 | int err; | |
918 | ||
919 | err = _gid_table_setup_one(ib_dev); | |
920 | ||
921 | if (err) | |
922 | return err; | |
923 | ||
32f69e4b | 924 | rdma_roce_rescan_device(ib_dev); |
03db3a2d MB |
925 | |
926 | return err; | |
927 | } | |
928 | ||
6612b498 | 929 | /** |
c3d71b69 JG |
930 | * rdma_query_gid - Read the GID content from the GID software cache |
931 | * @device: Device to query the GID | |
932 | * @port_num: Port number of the device | |
933 | * @index: Index of the GID table entry to read | |
934 | * @gid: Pointer to GID where to store the entry's GID | |
935 | * | |
936 | * rdma_query_gid() only reads the GID entry content for requested device, | |
937 | * port and index. It reads for IB, RoCE and iWarp link layers. It doesn't | |
938 | * hold any reference to the GID table entry in the HCA or software cache. | |
939 | * | |
940 | * Returns 0 on success or appropriate error code. | |
941 | * | |
942 | */ | |
943 | int rdma_query_gid(struct ib_device *device, u8 port_num, | |
944 | int index, union ib_gid *gid) | |
945 | { | |
946 | struct ib_gid_table *table; | |
947 | unsigned long flags; | |
948 | int res = -EINVAL; | |
949 | ||
950 | if (!rdma_is_port_valid(device, port_num)) | |
951 | return -EINVAL; | |
952 | ||
953 | table = rdma_gid_table(device, port_num); | |
954 | read_lock_irqsave(&table->rwlock, flags); | |
955 | ||
956 | if (index < 0 || index >= table->sz || | |
957 | !is_gid_entry_valid(table->data_vec[index])) | |
958 | goto done; | |
959 | ||
960 | memcpy(gid, &table->data_vec[index]->attr.gid, sizeof(*gid)); | |
961 | res = 0; | |
962 | ||
963 | done: | |
964 | read_unlock_irqrestore(&table->rwlock, flags); | |
965 | return res; | |
966 | } | |
967 | EXPORT_SYMBOL(rdma_query_gid); | |
968 | ||
969 | /** | |
970 | * rdma_find_gid - Returns SGID attributes if the matching GID is found. | |
6612b498 PP |
971 | * @device: The device to query. |
972 | * @gid: The GID value to search for. | |
973 | * @gid_type: The GID type to search for. | |
974 | * @ndev: In RoCE, the net device of the device. NULL means ignore. | |
6612b498 | 975 | * |
c3d71b69 JG |
976 | * rdma_find_gid() searches for the specified GID value in the software cache. |
977 | * | |
978 | * Returns GID attributes if a valid GID is found or returns ERR_PTR for the | |
979 | * error. The caller must invoke rdma_put_gid_attr() to release the reference. | |
980 | * | |
6612b498 | 981 | */ |
c3d71b69 JG |
982 | const struct ib_gid_attr *rdma_find_gid(struct ib_device *device, |
983 | const union ib_gid *gid, | |
984 | enum ib_gid_type gid_type, | |
985 | struct net_device *ndev) | |
03db3a2d | 986 | { |
c3d71b69 JG |
987 | unsigned long mask = GID_ATTR_FIND_MASK_GID | |
988 | GID_ATTR_FIND_MASK_GID_TYPE; | |
989 | struct ib_gid_attr gid_attr_val = {.ndev = ndev, .gid_type = gid_type}; | |
990 | u8 p; | |
991 | ||
992 | if (ndev) | |
993 | mask |= GID_ATTR_FIND_MASK_NETDEV; | |
994 | ||
995 | for (p = 0; p < device->phys_port_cnt; p++) { | |
996 | struct ib_gid_table *table; | |
997 | unsigned long flags; | |
998 | int index; | |
999 | ||
1000 | table = device->cache.ports[p].gid; | |
1001 | read_lock_irqsave(&table->rwlock, flags); | |
1002 | index = find_gid(table, gid, &gid_attr_val, false, mask, NULL); | |
1003 | if (index >= 0) { | |
1004 | const struct ib_gid_attr *attr; | |
1005 | ||
1006 | get_gid_entry(table->data_vec[index]); | |
1007 | attr = &table->data_vec[index]->attr; | |
1008 | read_unlock_irqrestore(&table->rwlock, flags); | |
1009 | return attr; | |
1010 | } | |
1011 | read_unlock_irqrestore(&table->rwlock, flags); | |
1012 | } | |
1013 | ||
1014 | return ERR_PTR(-ENOENT); | |
1015 | } | |
1016 | EXPORT_SYMBOL(rdma_find_gid); | |
1017 | ||
1da177e4 LT |
1018 | int ib_get_cached_pkey(struct ib_device *device, |
1019 | u8 port_num, | |
1020 | int index, | |
1021 | u16 *pkey) | |
1022 | { | |
1023 | struct ib_pkey_cache *cache; | |
1024 | unsigned long flags; | |
1025 | int ret = 0; | |
1026 | ||
24dc831b | 1027 | if (!rdma_is_port_valid(device, port_num)) |
1da177e4 LT |
1028 | return -EINVAL; |
1029 | ||
1030 | read_lock_irqsave(&device->cache.lock, flags); | |
1031 | ||
21d6454a | 1032 | cache = device->cache.ports[port_num - rdma_start_port(device)].pkey; |
1da177e4 LT |
1033 | |
1034 | if (index < 0 || index >= cache->table_len) | |
1035 | ret = -EINVAL; | |
1036 | else | |
1037 | *pkey = cache->table[index]; | |
1038 | ||
1039 | read_unlock_irqrestore(&device->cache.lock, flags); | |
1040 | ||
1041 | return ret; | |
1042 | } | |
1043 | EXPORT_SYMBOL(ib_get_cached_pkey); | |
1044 | ||
883c71fe DJ |
1045 | int ib_get_cached_subnet_prefix(struct ib_device *device, |
1046 | u8 port_num, | |
1047 | u64 *sn_pfx) | |
1048 | { | |
1049 | unsigned long flags; | |
1050 | int p; | |
1051 | ||
6d5b2047 | 1052 | if (!rdma_is_port_valid(device, port_num)) |
883c71fe DJ |
1053 | return -EINVAL; |
1054 | ||
1055 | p = port_num - rdma_start_port(device); | |
1056 | read_lock_irqsave(&device->cache.lock, flags); | |
1057 | *sn_pfx = device->cache.ports[p].subnet_prefix; | |
1058 | read_unlock_irqrestore(&device->cache.lock, flags); | |
1059 | ||
1060 | return 0; | |
1061 | } | |
1062 | EXPORT_SYMBOL(ib_get_cached_subnet_prefix); | |
1063 | ||
1da177e4 LT |
1064 | int ib_find_cached_pkey(struct ib_device *device, |
1065 | u8 port_num, | |
1066 | u16 pkey, | |
1067 | u16 *index) | |
1068 | { | |
1069 | struct ib_pkey_cache *cache; | |
1070 | unsigned long flags; | |
1071 | int i; | |
1072 | int ret = -ENOENT; | |
ff7166c4 | 1073 | int partial_ix = -1; |
1da177e4 | 1074 | |
24dc831b | 1075 | if (!rdma_is_port_valid(device, port_num)) |
1da177e4 LT |
1076 | return -EINVAL; |
1077 | ||
1078 | read_lock_irqsave(&device->cache.lock, flags); | |
1079 | ||
21d6454a | 1080 | cache = device->cache.ports[port_num - rdma_start_port(device)].pkey; |
1da177e4 LT |
1081 | |
1082 | *index = -1; | |
1083 | ||
1084 | for (i = 0; i < cache->table_len; ++i) | |
1085 | if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) { | |
ff7166c4 JM |
1086 | if (cache->table[i] & 0x8000) { |
1087 | *index = i; | |
1088 | ret = 0; | |
1089 | break; | |
1090 | } else | |
1091 | partial_ix = i; | |
1da177e4 LT |
1092 | } |
1093 | ||
ff7166c4 JM |
1094 | if (ret && partial_ix >= 0) { |
1095 | *index = partial_ix; | |
1096 | ret = 0; | |
1097 | } | |
1098 | ||
1da177e4 LT |
1099 | read_unlock_irqrestore(&device->cache.lock, flags); |
1100 | ||
1101 | return ret; | |
1102 | } | |
1103 | EXPORT_SYMBOL(ib_find_cached_pkey); | |
1104 | ||
73aaa741 JM |
1105 | int ib_find_exact_cached_pkey(struct ib_device *device, |
1106 | u8 port_num, | |
1107 | u16 pkey, | |
1108 | u16 *index) | |
1109 | { | |
1110 | struct ib_pkey_cache *cache; | |
1111 | unsigned long flags; | |
1112 | int i; | |
1113 | int ret = -ENOENT; | |
1114 | ||
24dc831b | 1115 | if (!rdma_is_port_valid(device, port_num)) |
73aaa741 JM |
1116 | return -EINVAL; |
1117 | ||
1118 | read_lock_irqsave(&device->cache.lock, flags); | |
1119 | ||
21d6454a | 1120 | cache = device->cache.ports[port_num - rdma_start_port(device)].pkey; |
73aaa741 JM |
1121 | |
1122 | *index = -1; | |
1123 | ||
1124 | for (i = 0; i < cache->table_len; ++i) | |
1125 | if (cache->table[i] == pkey) { | |
1126 | *index = i; | |
1127 | ret = 0; | |
1128 | break; | |
1129 | } | |
1130 | ||
1131 | read_unlock_irqrestore(&device->cache.lock, flags); | |
1132 | ||
1133 | return ret; | |
1134 | } | |
1135 | EXPORT_SYMBOL(ib_find_exact_cached_pkey); | |
1136 | ||
6fb9cdbf JM |
1137 | int ib_get_cached_lmc(struct ib_device *device, |
1138 | u8 port_num, | |
1139 | u8 *lmc) | |
1140 | { | |
1141 | unsigned long flags; | |
1142 | int ret = 0; | |
1143 | ||
24dc831b | 1144 | if (!rdma_is_port_valid(device, port_num)) |
6fb9cdbf JM |
1145 | return -EINVAL; |
1146 | ||
1147 | read_lock_irqsave(&device->cache.lock, flags); | |
21d6454a | 1148 | *lmc = device->cache.ports[port_num - rdma_start_port(device)].lmc; |
6fb9cdbf JM |
1149 | read_unlock_irqrestore(&device->cache.lock, flags); |
1150 | ||
1151 | return ret; | |
1152 | } | |
1153 | EXPORT_SYMBOL(ib_get_cached_lmc); | |
1154 | ||
9e2c3f1c JW |
1155 | int ib_get_cached_port_state(struct ib_device *device, |
1156 | u8 port_num, | |
1157 | enum ib_port_state *port_state) | |
1158 | { | |
1159 | unsigned long flags; | |
1160 | int ret = 0; | |
1161 | ||
6d5b2047 | 1162 | if (!rdma_is_port_valid(device, port_num)) |
9e2c3f1c JW |
1163 | return -EINVAL; |
1164 | ||
1165 | read_lock_irqsave(&device->cache.lock, flags); | |
21d6454a JW |
1166 | *port_state = device->cache.ports[port_num |
1167 | - rdma_start_port(device)].port_state; | |
9e2c3f1c JW |
1168 | read_unlock_irqrestore(&device->cache.lock, flags); |
1169 | ||
1170 | return ret; | |
1171 | } | |
1172 | EXPORT_SYMBOL(ib_get_cached_port_state); | |
1173 | ||
bf399c2c PP |
1174 | /** |
1175 | * rdma_get_gid_attr - Returns GID attributes for a port of a device | |
1176 | * at a requested gid_index, if a valid GID entry exists. | |
1177 | * @device: The device to query. | |
1178 | * @port_num: The port number on the device where the GID value | |
1179 | * is to be queried. | |
1180 | * @index: Index of the GID table entry whose attributes are to | |
1181 | * be queried. | |
1182 | * | |
1183 | * rdma_get_gid_attr() acquires reference count of gid attributes from the | |
1184 | * cached GID table. Caller must invoke rdma_put_gid_attr() to release | |
1185 | * reference to gid attribute regardless of link layer. | |
1186 | * | |
1187 | * Returns pointer to valid gid attribute or ERR_PTR for the appropriate error | |
1188 | * code. | |
1189 | */ | |
1190 | const struct ib_gid_attr * | |
1191 | rdma_get_gid_attr(struct ib_device *device, u8 port_num, int index) | |
1192 | { | |
1193 | const struct ib_gid_attr *attr = ERR_PTR(-EINVAL); | |
1194 | struct ib_gid_table *table; | |
1195 | unsigned long flags; | |
1196 | ||
1197 | if (!rdma_is_port_valid(device, port_num)) | |
1198 | return ERR_PTR(-EINVAL); | |
1199 | ||
1200 | table = rdma_gid_table(device, port_num); | |
1201 | if (index < 0 || index >= table->sz) | |
1202 | return ERR_PTR(-EINVAL); | |
1203 | ||
1204 | read_lock_irqsave(&table->rwlock, flags); | |
1205 | if (!is_gid_entry_valid(table->data_vec[index])) | |
1206 | goto done; | |
1207 | ||
1208 | get_gid_entry(table->data_vec[index]); | |
1209 | attr = &table->data_vec[index]->attr; | |
1210 | done: | |
1211 | read_unlock_irqrestore(&table->rwlock, flags); | |
1212 | return attr; | |
1213 | } | |
1214 | EXPORT_SYMBOL(rdma_get_gid_attr); | |
1215 | ||
1216 | /** | |
1217 | * rdma_put_gid_attr - Release reference to the GID attribute | |
1218 | * @attr: Pointer to the GID attribute whose reference | |
1219 | * needs to be released. | |
1220 | * | |
1221 | * rdma_put_gid_attr() must be used to release reference whose | |
1222 | * reference is acquired using rdma_get_gid_attr() or any APIs | |
1223 | * which returns a pointer to the ib_gid_attr regardless of link layer | |
1224 | * of IB or RoCE. | |
1225 | * | |
1226 | */ | |
1227 | void rdma_put_gid_attr(const struct ib_gid_attr *attr) | |
1228 | { | |
1229 | struct ib_gid_table_entry *entry = | |
1230 | container_of(attr, struct ib_gid_table_entry, attr); | |
1231 | ||
1232 | put_gid_entry(entry); | |
1233 | } | |
1234 | EXPORT_SYMBOL(rdma_put_gid_attr); | |
1235 | ||
1236 | /** | |
1237 | * rdma_hold_gid_attr - Get reference to existing GID attribute | |
1238 | * | |
1239 | * @attr: Pointer to the GID attribute whose reference | |
1240 | * needs to be taken. | |
1241 | * | |
1242 | * Increase the reference count to a GID attribute to keep it from being | |
1243 | * freed. Callers are required to already be holding a reference to attribute. | |
1244 | * | |
1245 | */ | |
1246 | void rdma_hold_gid_attr(const struct ib_gid_attr *attr) | |
1247 | { | |
1248 | struct ib_gid_table_entry *entry = | |
1249 | container_of(attr, struct ib_gid_table_entry, attr); | |
1250 | ||
1251 | get_gid_entry(entry); | |
1252 | } | |
1253 | EXPORT_SYMBOL(rdma_hold_gid_attr); | |
1254 | ||
598ff6ba PP |
1255 | static int config_non_roce_gid_cache(struct ib_device *device, |
1256 | u8 port, int gid_tbl_len) | |
1257 | { | |
1258 | struct ib_gid_attr gid_attr = {}; | |
1259 | struct ib_gid_table *table; | |
598ff6ba PP |
1260 | int ret = 0; |
1261 | int i; | |
1262 | ||
1263 | gid_attr.device = device; | |
1264 | gid_attr.port_num = port; | |
724631a9 | 1265 | table = rdma_gid_table(device, port); |
598ff6ba PP |
1266 | |
1267 | mutex_lock(&table->lock); | |
1268 | for (i = 0; i < gid_tbl_len; ++i) { | |
1269 | if (!device->query_gid) | |
1270 | continue; | |
b150c386 | 1271 | ret = device->query_gid(device, port, i, &gid_attr.gid); |
598ff6ba PP |
1272 | if (ret) { |
1273 | pr_warn("query_gid failed (%d) for %s (index %d)\n", | |
1274 | ret, device->name, i); | |
1275 | goto err; | |
1276 | } | |
1277 | gid_attr.index = i; | |
b150c386 | 1278 | add_modify_gid(table, &gid_attr); |
598ff6ba PP |
1279 | } |
1280 | err: | |
1281 | mutex_unlock(&table->lock); | |
1282 | return ret; | |
1283 | } | |
1284 | ||
1da177e4 | 1285 | static void ib_cache_update(struct ib_device *device, |
d291f1a6 DJ |
1286 | u8 port, |
1287 | bool enforce_security) | |
1da177e4 LT |
1288 | { |
1289 | struct ib_port_attr *tprops = NULL; | |
1290 | struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache; | |
1da177e4 LT |
1291 | int i; |
1292 | int ret; | |
03db3a2d | 1293 | |
24dc831b | 1294 | if (!rdma_is_port_valid(device, port)) |
03db3a2d MB |
1295 | return; |
1296 | ||
1da177e4 LT |
1297 | tprops = kmalloc(sizeof *tprops, GFP_KERNEL); |
1298 | if (!tprops) | |
1299 | return; | |
1300 | ||
1301 | ret = ib_query_port(device, port, tprops); | |
1302 | if (ret) { | |
aba25a3e PP |
1303 | pr_warn("ib_query_port failed (%d) for %s\n", |
1304 | ret, device->name); | |
1da177e4 LT |
1305 | goto err; |
1306 | } | |
1307 | ||
598ff6ba PP |
1308 | if (!rdma_protocol_roce(device, port)) { |
1309 | ret = config_non_roce_gid_cache(device, port, | |
1310 | tprops->gid_tbl_len); | |
1311 | if (ret) | |
1312 | goto err; | |
1313 | } | |
1314 | ||
acafe7e3 KC |
1315 | pkey_cache = kmalloc(struct_size(pkey_cache, table, |
1316 | tprops->pkey_tbl_len), | |
1317 | GFP_KERNEL); | |
1da177e4 LT |
1318 | if (!pkey_cache) |
1319 | goto err; | |
1320 | ||
1321 | pkey_cache->table_len = tprops->pkey_tbl_len; | |
1322 | ||
1da177e4 LT |
1323 | for (i = 0; i < pkey_cache->table_len; ++i) { |
1324 | ret = ib_query_pkey(device, port, i, pkey_cache->table + i); | |
1325 | if (ret) { | |
aba25a3e PP |
1326 | pr_warn("ib_query_pkey failed (%d) for %s (index %d)\n", |
1327 | ret, device->name, i); | |
1da177e4 LT |
1328 | goto err; |
1329 | } | |
1330 | } | |
1331 | ||
1da177e4 LT |
1332 | write_lock_irq(&device->cache.lock); |
1333 | ||
21d6454a JW |
1334 | old_pkey_cache = device->cache.ports[port - |
1335 | rdma_start_port(device)].pkey; | |
1da177e4 | 1336 | |
21d6454a | 1337 | device->cache.ports[port - rdma_start_port(device)].pkey = pkey_cache; |
21d6454a JW |
1338 | device->cache.ports[port - rdma_start_port(device)].lmc = tprops->lmc; |
1339 | device->cache.ports[port - rdma_start_port(device)].port_state = | |
aaaca121 | 1340 | tprops->state; |
6fb9cdbf | 1341 | |
883c71fe DJ |
1342 | device->cache.ports[port - rdma_start_port(device)].subnet_prefix = |
1343 | tprops->subnet_prefix; | |
1da177e4 LT |
1344 | write_unlock_irq(&device->cache.lock); |
1345 | ||
d291f1a6 DJ |
1346 | if (enforce_security) |
1347 | ib_security_cache_change(device, | |
1348 | port, | |
1349 | tprops->subnet_prefix); | |
1350 | ||
1da177e4 | 1351 | kfree(old_pkey_cache); |
1da177e4 LT |
1352 | kfree(tprops); |
1353 | return; | |
1354 | ||
1355 | err: | |
1356 | kfree(pkey_cache); | |
1da177e4 LT |
1357 | kfree(tprops); |
1358 | } | |
1359 | ||
c4028958 | 1360 | static void ib_cache_task(struct work_struct *_work) |
1da177e4 | 1361 | { |
c4028958 DH |
1362 | struct ib_update_work *work = |
1363 | container_of(_work, struct ib_update_work, work); | |
1da177e4 | 1364 | |
d291f1a6 DJ |
1365 | ib_cache_update(work->device, |
1366 | work->port_num, | |
1367 | work->enforce_security); | |
1da177e4 LT |
1368 | kfree(work); |
1369 | } | |
1370 | ||
1371 | static void ib_cache_event(struct ib_event_handler *handler, | |
1372 | struct ib_event *event) | |
1373 | { | |
1374 | struct ib_update_work *work; | |
1375 | ||
1376 | if (event->event == IB_EVENT_PORT_ERR || | |
1377 | event->event == IB_EVENT_PORT_ACTIVE || | |
1378 | event->event == IB_EVENT_LID_CHANGE || | |
1379 | event->event == IB_EVENT_PKEY_CHANGE || | |
acaea9ee | 1380 | event->event == IB_EVENT_SM_CHANGE || |
761d90ed OG |
1381 | event->event == IB_EVENT_CLIENT_REREGISTER || |
1382 | event->event == IB_EVENT_GID_CHANGE) { | |
1da177e4 LT |
1383 | work = kmalloc(sizeof *work, GFP_ATOMIC); |
1384 | if (work) { | |
c4028958 | 1385 | INIT_WORK(&work->work, ib_cache_task); |
1da177e4 LT |
1386 | work->device = event->device; |
1387 | work->port_num = event->element.port_num; | |
d291f1a6 DJ |
1388 | if (event->event == IB_EVENT_PKEY_CHANGE || |
1389 | event->event == IB_EVENT_GID_CHANGE) | |
1390 | work->enforce_security = true; | |
1391 | else | |
1392 | work->enforce_security = false; | |
1393 | ||
f0626710 | 1394 | queue_work(ib_wq, &work->work); |
1da177e4 LT |
1395 | } |
1396 | } | |
1397 | } | |
1398 | ||
03db3a2d | 1399 | int ib_cache_setup_one(struct ib_device *device) |
1da177e4 LT |
1400 | { |
1401 | int p; | |
03db3a2d | 1402 | int err; |
1da177e4 LT |
1403 | |
1404 | rwlock_init(&device->cache.lock); | |
1405 | ||
21d6454a | 1406 | device->cache.ports = |
6396bb22 KC |
1407 | kcalloc(rdma_end_port(device) - rdma_start_port(device) + 1, |
1408 | sizeof(*device->cache.ports), | |
1409 | GFP_KERNEL); | |
dcc9881e LR |
1410 | if (!device->cache.ports) |
1411 | return -ENOMEM; | |
1da177e4 | 1412 | |
03db3a2d | 1413 | err = gid_table_setup_one(device); |
dcc9881e LR |
1414 | if (err) { |
1415 | kfree(device->cache.ports); | |
1416 | device->cache.ports = NULL; | |
1417 | return err; | |
1418 | } | |
03db3a2d | 1419 | |
55aeed06 | 1420 | for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p) |
d291f1a6 | 1421 | ib_cache_update(device, p + rdma_start_port(device), true); |
1da177e4 LT |
1422 | |
1423 | INIT_IB_EVENT_HANDLER(&device->cache.event_handler, | |
1424 | device, ib_cache_event); | |
dcc9881e | 1425 | ib_register_event_handler(&device->cache.event_handler); |
03db3a2d | 1426 | return 0; |
1da177e4 LT |
1427 | } |
1428 | ||
03db3a2d | 1429 | void ib_cache_release_one(struct ib_device *device) |
1da177e4 LT |
1430 | { |
1431 | int p; | |
1432 | ||
03db3a2d MB |
1433 | /* |
1434 | * The release function frees all the cache elements. | |
1435 | * This function should be called as part of freeing | |
1436 | * all the device's resources when the cache could no | |
1437 | * longer be accessed. | |
1438 | */ | |
21d6454a JW |
1439 | for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p) |
1440 | kfree(device->cache.ports[p].pkey); | |
03db3a2d MB |
1441 | |
1442 | gid_table_release_one(device); | |
21d6454a | 1443 | kfree(device->cache.ports); |
1da177e4 LT |
1444 | } |
1445 | ||
03db3a2d MB |
1446 | void ib_cache_cleanup_one(struct ib_device *device) |
1447 | { | |
1448 | /* The cleanup function unregisters the event handler, | |
1449 | * waits for all in-progress workqueue elements and cleans | |
1450 | * up the GID cache. This function should be called after | |
1451 | * the device was removed from the devices list and all | |
1452 | * clients were removed, so the cache exists but is | |
1453 | * non-functional and shouldn't be updated anymore. | |
1454 | */ | |
1455 | ib_unregister_event_handler(&device->cache.event_handler); | |
1456 | flush_workqueue(ib_wq); | |
1457 | gid_table_cleanup_one(device); | |
b150c386 PP |
1458 | |
1459 | /* | |
1460 | * Flush the wq second time for any pending GID delete work. | |
1461 | */ | |
1462 | flush_workqueue(ib_wq); | |
03db3a2d | 1463 | } |