aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/infiniband/core/cache.c
diff options
context:
space:
mode:
authorParav Pandit <parav@mellanox.com>2018-04-01 08:08:21 -0400
committerJason Gunthorpe <jgg@mellanox.com>2018-04-03 23:33:50 -0400
commit598ff6bae689453aa894bc38f3f1bb78eb131a61 (patch)
treed002fd3de0d63c34662fd88fbc50e06db15b5abb /drivers/infiniband/core/cache.c
parentf35faa4ba9568138eea1c58abb92e8ef415dce41 (diff)
IB/core: Refactor GID modify code for RoCE
Code is refactored to prepare separate functions for RoCE which can do more complex operations related to reference counting, while still maintainining code readability. This includes (a) Simplification to not perform netdevice checks and modifications for IB link layer. (b) Do not add RoCE GID entry which has NULL netdevice; instead return an error. (c) If GID addition fails at provider level add_gid(), do not add the entry in the cache and keep the entry marked as INVALID. (d) Simplify and reuse the ib_cache_gid_add()/del() routines so that they can be used even for modifying default GIDs. This avoid some code duplication in modifying default GIDs. (e) find_gid() routine refers to the data entry flags to qualify a GID as valid or invalid GID rather than depending on attributes and zeroness of the GID content. (f) gid_table_reserve_default() sets the GID default attribute at beginning while setting up the GID table. There is no need to use default_gid flag in low level functions such as write_gid(), add_gid(), del_gid(), as they never need to update the DEFAULT property of the GID entry while during GID table update. As as result of this refactor, reserved GID 0:0:0:0:0:0:0:0 is no longer searchable as described below. A unicast GID entry of 0:0:0:0:0:0:0:0 is Reserved GID as per the IB spec version 1.3 section 4.1.1, point (6) whose snippet is below. "The unicast GID address 0:0:0:0:0:0:0:0 is reserved - referred to as the Reserved GID. It shall never be assigned to any endport. It shall not be used as a destination address or in a global routing header (GRH)." GID table cache now only stores valid GID entries. Before this patch, Reserved GID 0:0:0:0:0:0:0:0 was searchable in the GID table using ib_find_cached_gid_by_port() and other similar find routines. Zero GID is no longer searchable as it shall not to be present in GRH or path recored entry as described in IB spec version 1.3 section 4.1.1, point (6), section 12.7.10 and section 12.7.20. ib_cache_update() is simplified to check link layer once, use unified locking scheme for all link layers, removed temporary gid table allocation/free logic. Additionally, (a) Expand ib_gid_attr to store port and index so that GID query routines can get port and index information from the attribute structure. (b) Expand ib_gid_attr to store device as well so that in future code when GID reference counting is done, device is used to reach back to the GID table entry. Signed-off-by: Parav Pandit <parav@mellanox.com> Signed-off-by: Leon Romanovsky <leonro@mellanox.com> Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
Diffstat (limited to 'drivers/infiniband/core/cache.c')
-rw-r--r--drivers/infiniband/core/cache.c484
1 files changed, 253 insertions, 231 deletions
diff --git a/drivers/infiniband/core/cache.c b/drivers/infiniband/core/cache.c
index e03eaf0c7527..045ca11fa135 100644
--- a/drivers/infiniband/core/cache.c
+++ b/drivers/infiniband/core/cache.c
@@ -59,8 +59,6 @@ struct ib_update_work {
59union ib_gid zgid; 59union ib_gid zgid;
60EXPORT_SYMBOL(zgid); 60EXPORT_SYMBOL(zgid);
61 61
62static const struct ib_gid_attr zattr;
63
64enum gid_attr_find_mask { 62enum gid_attr_find_mask {
65 GID_ATTR_FIND_MASK_GID = 1UL << 0, 63 GID_ATTR_FIND_MASK_GID = 1UL << 0,
66 GID_ATTR_FIND_MASK_NETDEV = 1UL << 1, 64 GID_ATTR_FIND_MASK_NETDEV = 1UL << 1,
@@ -73,15 +71,6 @@ enum gid_table_entry_props {
73 GID_TABLE_ENTRY_DEFAULT = 1UL << 1, 71 GID_TABLE_ENTRY_DEFAULT = 1UL << 1,
74}; 72};
75 73
76enum gid_table_write_action {
77 GID_TABLE_WRITE_ACTION_ADD,
78 GID_TABLE_WRITE_ACTION_DEL,
79 /* MODIFY only updates the GID table. Currently only used by
80 * ib_cache_update.
81 */
82 GID_TABLE_WRITE_ACTION_MODIFY
83};
84
85struct ib_gid_table_entry { 74struct ib_gid_table_entry {
86 unsigned long props; 75 unsigned long props;
87 union ib_gid gid; 76 union ib_gid gid;
@@ -100,16 +89,13 @@ struct ib_gid_table {
100 * (a) Find the GID 89 * (a) Find the GID
101 * (b) Delete it. 90 * (b) Delete it.
102 * 91 *
103 * Add/delete should be carried out atomically.
104 * This is done by locking this mutex from multiple
105 * writers. We don't need this lock for IB, as the MAD
106 * layer replaces all entries. All data_vec entries
107 * are locked by this lock.
108 **/ 92 **/
109 struct mutex lock; 93 /* Any writer to data_vec must hold this lock and the write side of
110 /* This lock protects the table entries from being 94 * rwlock. readers must hold only rwlock. All writers must be in a
111 * read and written simultaneously. 95 * sleepable context.
112 */ 96 */
97 struct mutex lock;
98 /* rwlock protects data_vec[ix]->props. */
113 rwlock_t rwlock; 99 rwlock_t rwlock;
114 struct ib_gid_table_entry *data_vec; 100 struct ib_gid_table_entry *data_vec;
115}; 101};
@@ -163,94 +149,128 @@ int ib_cache_gid_parse_type_str(const char *buf)
163} 149}
164EXPORT_SYMBOL(ib_cache_gid_parse_type_str); 150EXPORT_SYMBOL(ib_cache_gid_parse_type_str);
165 151
166/* This function expects that rwlock will be write locked in all 152static void del_roce_gid(struct ib_device *device, u8 port_num,
167 * scenarios and that lock will be locked in sleep-able (RoCE) 153 struct ib_gid_table *table, int ix)
168 * scenarios.
169 */
170static int write_gid(struct ib_device *ib_dev, u8 port,
171 struct ib_gid_table *table, int ix,
172 const union ib_gid *gid,
173 const struct ib_gid_attr *attr,
174 enum gid_table_write_action action,
175 bool default_gid)
176 __releases(&table->rwlock) __acquires(&table->rwlock)
177{ 154{
178 int ret = 0; 155 pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
179 struct net_device *old_net_dev; 156 device->name, port_num, ix,
180 enum ib_gid_type old_gid_type; 157 table->data_vec[ix].gid.raw);
158
159 if (rdma_cap_roce_gid_table(device, port_num))
160 device->del_gid(device, port_num, ix,
161 &table->data_vec[ix].context);
162 dev_put(table->data_vec[ix].attr.ndev);
163}
181 164
182 /* in rdma_cap_roce_gid_table, this funciton should be protected by a 165static int add_roce_gid(struct ib_gid_table *table,
183 * sleep-able lock. 166 const union ib_gid *gid,
184 */ 167 const struct ib_gid_attr *attr)
168{
169 struct ib_gid_table_entry *entry;
170 int ix = attr->index;
171 int ret = 0;
185 172
186 if (rdma_cap_roce_gid_table(ib_dev, port)) { 173 if (!attr->ndev) {
187 table->data_vec[ix].props |= GID_TABLE_ENTRY_INVALID; 174 pr_err("%s NULL netdev device=%s port=%d index=%d\n",
188 write_unlock_irq(&table->rwlock); 175 __func__, attr->device->name, attr->port_num,
189 /* GID_TABLE_WRITE_ACTION_MODIFY currently isn't supported by 176 attr->index);
190 * RoCE providers and thus only updates the cache. 177 return -EINVAL;
191 */
192 if (action == GID_TABLE_WRITE_ACTION_ADD)
193 ret = ib_dev->add_gid(ib_dev, port, ix, gid, attr,
194 &table->data_vec[ix].context);
195 else if (action == GID_TABLE_WRITE_ACTION_DEL)
196 ret = ib_dev->del_gid(ib_dev, port, ix,
197 &table->data_vec[ix].context);
198 write_lock_irq(&table->rwlock);
199 } 178 }
200 179
201 old_net_dev = table->data_vec[ix].attr.ndev; 180 entry = &table->data_vec[ix];
202 old_gid_type = table->data_vec[ix].attr.gid_type; 181 if ((entry->props & GID_TABLE_ENTRY_INVALID) == 0) {
203 if (old_net_dev && old_net_dev != attr->ndev) 182 WARN(1, "GID table corruption device=%s port=%d index=%d\n",
204 dev_put(old_net_dev); 183 attr->device->name, attr->port_num,
205 /* if modify_gid failed, just delete the old gid */ 184 attr->index);
206 if (ret || action == GID_TABLE_WRITE_ACTION_DEL) { 185 return -EINVAL;
207 gid = &zgid;
208 attr = &zattr;
209 table->data_vec[ix].context = NULL;
210 } 186 }
211 187
212 memcpy(&table->data_vec[ix].gid, gid, sizeof(*gid)); 188 if (rdma_cap_roce_gid_table(attr->device, attr->port_num)) {
213 memcpy(&table->data_vec[ix].attr, attr, sizeof(*attr)); 189 ret = attr->device->add_gid(attr->device, attr->port_num,
214 if (default_gid) { 190 ix, gid, attr, &entry->context);
215 table->data_vec[ix].props |= GID_TABLE_ENTRY_DEFAULT; 191 if (ret) {
216 if (action == GID_TABLE_WRITE_ACTION_DEL) 192 pr_err("%s GID add failed device=%s port=%d index=%d\n",
217 table->data_vec[ix].attr.gid_type = old_gid_type; 193 __func__, attr->device->name, attr->port_num,
194 attr->index);
195 goto add_err;
196 }
218 } 197 }
219 if (table->data_vec[ix].attr.ndev && 198 dev_hold(attr->ndev);
220 table->data_vec[ix].attr.ndev != old_net_dev)
221 dev_hold(table->data_vec[ix].attr.ndev);
222
223 table->data_vec[ix].props &= ~GID_TABLE_ENTRY_INVALID;
224 199
200add_err:
201 if (!ret)
202 pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
203 attr->device->name, attr->port_num, ix, gid->raw);
225 return ret; 204 return ret;
226} 205}
227 206
228static int add_gid(struct ib_device *ib_dev, u8 port, 207/**
229 struct ib_gid_table *table, int ix, 208 * add_modify_gid - Add or modify GID table entry
230 const union ib_gid *gid, 209 *
231 const struct ib_gid_attr *attr, 210 * @table: GID table in which GID to be added or modified
232 bool default_gid) { 211 * @gid: GID content
233 return write_gid(ib_dev, port, table, ix, gid, attr, 212 * @attr: Attributes of the GID
234 GID_TABLE_WRITE_ACTION_ADD, default_gid); 213 *
235} 214 * Returns 0 on success or appropriate error code. It accepts zero
215 * GID addition for non RoCE ports for HCA's who report them as valid
216 * GID. However such zero GIDs are not added to the cache.
217 */
218static int add_modify_gid(struct ib_gid_table *table,
219 const union ib_gid *gid,
220 const struct ib_gid_attr *attr)
221{
222 int ret;
223
224 if (rdma_protocol_roce(attr->device, attr->port_num)) {
225 ret = add_roce_gid(table, gid, attr);
226 if (ret)
227 return ret;
228 } else {
229 /*
230 * Some HCA's report multiple GID entries with only one
231 * valid GID, but remaining as zero GID.
232 * So ignore such behavior for IB link layer and don't
233 * fail the call, but don't add such entry to GID cache.
234 */
235 if (!memcmp(gid, &zgid, sizeof(*gid)))
236 return 0;
237 }
238
239 lockdep_assert_held(&table->lock);
240 memcpy(&table->data_vec[attr->index].gid, gid, sizeof(*gid));
241 memcpy(&table->data_vec[attr->index].attr, attr, sizeof(*attr));
236 242
237static int modify_gid(struct ib_device *ib_dev, u8 port, 243 write_lock_irq(&table->rwlock);
238 struct ib_gid_table *table, int ix, 244 table->data_vec[attr->index].props &= ~GID_TABLE_ENTRY_INVALID;
239 const union ib_gid *gid, 245 write_unlock_irq(&table->rwlock);
240 const struct ib_gid_attr *attr, 246 return 0;
241 bool default_gid) {
242 return write_gid(ib_dev, port, table, ix, gid, attr,
243 GID_TABLE_WRITE_ACTION_MODIFY, default_gid);
244} 247}
245 248
246static int del_gid(struct ib_device *ib_dev, u8 port, 249/**
247 struct ib_gid_table *table, int ix, 250 * del_gid - Delete GID table entry
248 bool default_gid) { 251 *
249 return write_gid(ib_dev, port, table, ix, &zgid, &zattr, 252 * @ib_dev: IB device whose GID entry to be deleted
250 GID_TABLE_WRITE_ACTION_DEL, default_gid); 253 * @port: Port number of the IB device
254 * @table: GID table of the IB device for a port
255 * @ix: GID entry index to delete
256 *
257 */
258static void del_gid(struct ib_device *ib_dev, u8 port,
259 struct ib_gid_table *table, int ix)
260{
261 lockdep_assert_held(&table->lock);
262 write_lock_irq(&table->rwlock);
263 table->data_vec[ix].props |= GID_TABLE_ENTRY_INVALID;
264 write_unlock_irq(&table->rwlock);
265
266 if (rdma_protocol_roce(ib_dev, port))
267 del_roce_gid(ib_dev, port, table, ix);
268 memcpy(&table->data_vec[ix].gid, &zgid, sizeof(zgid));
269 memset(&table->data_vec[ix].attr, 0, sizeof(table->data_vec[ix].attr));
270 table->data_vec[ix].context = NULL;
251} 271}
252 272
253/* rwlock should be read locked */ 273/* rwlock should be read locked, or lock should be held */
254static int find_gid(struct ib_gid_table *table, const union ib_gid *gid, 274static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,
255 const struct ib_gid_attr *val, bool default_gid, 275 const struct ib_gid_attr *val, bool default_gid,
256 unsigned long mask, int *pempty) 276 unsigned long mask, int *pempty)
@@ -266,15 +286,32 @@ static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,
266 286
267 i++; 287 i++;
268 288
289 /* find_gid() is used during GID addition where it is expected
290 * to return a free entry slot which is not duplicate.
291 * Free entry slot is requested and returned if pempty is set,
292 * so lookup free slot only if requested.
293 */
294 if (pempty && empty < 0) {
295 if (data->props & GID_TABLE_ENTRY_INVALID) {
296 /* Found an invalid (free) entry; allocate it */
297 if (data->props & GID_TABLE_ENTRY_DEFAULT) {
298 if (default_gid)
299 empty = curr_index;
300 } else {
301 empty = curr_index;
302 }
303 }
304 }
305
306 /*
307 * Additionally find_gid() is used to find valid entry during
308 * lookup operation, where validity needs to be checked. So
309 * find the empty entry first to continue to search for a free
310 * slot and ignore its INVALID flag.
311 */
269 if (data->props & GID_TABLE_ENTRY_INVALID) 312 if (data->props & GID_TABLE_ENTRY_INVALID)
270 continue; 313 continue;
271 314
272 if (empty < 0)
273 if (!memcmp(&data->gid, &zgid, sizeof(*gid)) &&
274 !memcmp(attr, &zattr, sizeof(*attr)) &&
275 !data->props)
276 empty = curr_index;
277
278 if (found >= 0) 315 if (found >= 0)
279 continue; 316 continue;
280 317
@@ -310,20 +347,56 @@ static void make_default_gid(struct net_device *dev, union ib_gid *gid)
310 addrconf_ifid_eui48(&gid->raw[8], dev); 347 addrconf_ifid_eui48(&gid->raw[8], dev);
311} 348}
312 349
313int ib_cache_gid_add(struct ib_device *ib_dev, u8 port, 350static int __ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
314 union ib_gid *gid, struct ib_gid_attr *attr) 351 union ib_gid *gid, struct ib_gid_attr *attr,
352 unsigned long mask, bool default_gid)
315{ 353{
316 struct ib_gid_table *table; 354 struct ib_gid_table *table;
317 int ix;
318 int ret = 0; 355 int ret = 0;
319 struct net_device *idev;
320 int empty; 356 int empty;
357 int ix;
321 358
322 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 359 /* Do not allow adding zero GID in support of
323 360 * IB spec version 1.3 section 4.1.1 point (6) and
361 * section 12.7.10 and section 12.7.20
362 */
324 if (!memcmp(gid, &zgid, sizeof(*gid))) 363 if (!memcmp(gid, &zgid, sizeof(*gid)))
325 return -EINVAL; 364 return -EINVAL;
326 365
366 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
367
368 mutex_lock(&table->lock);
369
370 ix = find_gid(table, gid, attr, default_gid, mask, &empty);
371 if (ix >= 0)
372 goto out_unlock;
373
374 if (empty < 0) {
375 ret = -ENOSPC;
376 goto out_unlock;
377 }
378 attr->device = ib_dev;
379 attr->index = empty;
380 attr->port_num = port;
381 ret = add_modify_gid(table, gid, attr);
382 if (!ret)
383 dispatch_gid_change_event(ib_dev, port);
384
385out_unlock:
386 mutex_unlock(&table->lock);
387 if (ret)
388 pr_warn("%s: unable to add gid %pI6 error=%d\n",
389 __func__, gid->raw, ret);
390 return ret;
391}
392
393int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
394 union ib_gid *gid, struct ib_gid_attr *attr)
395{
396 struct net_device *idev;
397 unsigned long mask;
398 int ret;
399
327 if (ib_dev->get_netdev) { 400 if (ib_dev->get_netdev) {
328 idev = ib_dev->get_netdev(ib_dev, port); 401 idev = ib_dev->get_netdev(ib_dev, port);
329 if (idev && attr->ndev != idev) { 402 if (idev && attr->ndev != idev) {
@@ -340,27 +413,11 @@ int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
340 dev_put(idev); 413 dev_put(idev);
341 } 414 }
342 415
343 mutex_lock(&table->lock); 416 mask = GID_ATTR_FIND_MASK_GID |
344 write_lock_irq(&table->rwlock); 417 GID_ATTR_FIND_MASK_GID_TYPE |
345 418 GID_ATTR_FIND_MASK_NETDEV;
346 ix = find_gid(table, gid, attr, false, GID_ATTR_FIND_MASK_GID |
347 GID_ATTR_FIND_MASK_GID_TYPE |
348 GID_ATTR_FIND_MASK_NETDEV, &empty);
349 if (ix >= 0)
350 goto out_unlock;
351
352 if (empty < 0) {
353 ret = -ENOSPC;
354 goto out_unlock;
355 }
356
357 ret = add_gid(ib_dev, port, table, empty, gid, attr, false);
358 if (!ret)
359 dispatch_gid_change_event(ib_dev, port);
360 419
361out_unlock: 420 ret = __ib_cache_gid_add(ib_dev, port, gid, attr, mask, false);
362 write_unlock_irq(&table->rwlock);
363 mutex_unlock(&table->lock);
364 return ret; 421 return ret;
365} 422}
366 423
@@ -368,29 +425,32 @@ int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
368 union ib_gid *gid, struct ib_gid_attr *attr) 425 union ib_gid *gid, struct ib_gid_attr *attr)
369{ 426{
370 struct ib_gid_table *table; 427 struct ib_gid_table *table;
428 int ret = 0;
371 int ix; 429 int ix;
372 430
373 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 431 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
374 432
375 mutex_lock(&table->lock); 433 mutex_lock(&table->lock);
376 write_lock_irq(&table->rwlock);
377 434
378 ix = find_gid(table, gid, attr, false, 435 ix = find_gid(table, gid, attr, false,
379 GID_ATTR_FIND_MASK_GID | 436 GID_ATTR_FIND_MASK_GID |
380 GID_ATTR_FIND_MASK_GID_TYPE | 437 GID_ATTR_FIND_MASK_GID_TYPE |
381 GID_ATTR_FIND_MASK_NETDEV | 438 GID_ATTR_FIND_MASK_NETDEV,
382 GID_ATTR_FIND_MASK_DEFAULT,
383 NULL); 439 NULL);
384 if (ix < 0) 440 if (ix < 0) {
441 ret = -EINVAL;
385 goto out_unlock; 442 goto out_unlock;
443 }
386 444
387 if (!del_gid(ib_dev, port, table, ix, false)) 445 del_gid(ib_dev, port, table, ix);
388 dispatch_gid_change_event(ib_dev, port); 446 dispatch_gid_change_event(ib_dev, port);
389 447
390out_unlock: 448out_unlock:
391 write_unlock_irq(&table->rwlock);
392 mutex_unlock(&table->lock); 449 mutex_unlock(&table->lock);
393 return 0; 450 if (ret)
451 pr_debug("%s: can't delete gid %pI6 error=%d\n",
452 __func__, gid->raw, ret);
453 return ret;
394} 454}
395 455
396int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port, 456int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
@@ -403,16 +463,14 @@ int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
403 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 463 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
404 464
405 mutex_lock(&table->lock); 465 mutex_lock(&table->lock);
406 write_lock_irq(&table->rwlock);
407 466
408 for (ix = 0; ix < table->sz; ix++) 467 for (ix = 0; ix < table->sz; ix++) {
409 if (table->data_vec[ix].attr.ndev == ndev) 468 if (table->data_vec[ix].attr.ndev == ndev) {
410 if (!del_gid(ib_dev, port, table, ix, 469 del_gid(ib_dev, port, table, ix);
411 !!(table->data_vec[ix].props & 470 deleted = true;
412 GID_TABLE_ENTRY_DEFAULT))) 471 }
413 deleted = true; 472 }
414 473
415 write_unlock_irq(&table->rwlock);
416 mutex_unlock(&table->lock); 474 mutex_unlock(&table->lock);
417 475
418 if (deleted) 476 if (deleted)
@@ -609,6 +667,7 @@ static struct ib_gid_table *alloc_gid_table(int sz)
609{ 667{
610 struct ib_gid_table *table = 668 struct ib_gid_table *table =
611 kzalloc(sizeof(struct ib_gid_table), GFP_KERNEL); 669 kzalloc(sizeof(struct ib_gid_table), GFP_KERNEL);
670 int i;
612 671
613 if (!table) 672 if (!table)
614 return NULL; 673 return NULL;
@@ -622,6 +681,11 @@ static struct ib_gid_table *alloc_gid_table(int sz)
622 table->sz = sz; 681 table->sz = sz;
623 rwlock_init(&table->rwlock); 682 rwlock_init(&table->rwlock);
624 683
684 /* Mark all entries as invalid so that allocator can allocate
685 * one of the invalid (free) entry.
686 */
687 for (i = 0; i < sz; i++)
688 table->data_vec[i].props |= GID_TABLE_ENTRY_INVALID;
625 return table; 689 return table;
626 690
627err_free_table: 691err_free_table:
@@ -646,16 +710,15 @@ static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port,
646 if (!table) 710 if (!table)
647 return; 711 return;
648 712
649 write_lock_irq(&table->rwlock); 713 mutex_lock(&table->lock);
650 for (i = 0; i < table->sz; ++i) { 714 for (i = 0; i < table->sz; ++i) {
651 if (memcmp(&table->data_vec[i].gid, &zgid, 715 if (memcmp(&table->data_vec[i].gid, &zgid,
652 sizeof(table->data_vec[i].gid))) 716 sizeof(table->data_vec[i].gid))) {
653 if (!del_gid(ib_dev, port, table, i, 717 del_gid(ib_dev, port, table, i);
654 table->data_vec[i].props & 718 deleted = true;
655 GID_ATTR_FIND_MASK_DEFAULT)) 719 }
656 deleted = true;
657 } 720 }
658 write_unlock_irq(&table->rwlock); 721 mutex_unlock(&table->lock);
659 722
660 if (deleted) 723 if (deleted)
661 dispatch_gid_change_event(ib_dev, port); 724 dispatch_gid_change_event(ib_dev, port);
@@ -668,9 +731,9 @@ void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
668{ 731{
669 union ib_gid gid; 732 union ib_gid gid;
670 struct ib_gid_attr gid_attr; 733 struct ib_gid_attr gid_attr;
671 struct ib_gid_attr zattr_type = zattr;
672 struct ib_gid_table *table; 734 struct ib_gid_table *table;
673 unsigned int gid_type; 735 unsigned int gid_type;
736 unsigned long mask;
674 737
675 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 738 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid;
676 739
@@ -679,60 +742,19 @@ void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
679 gid_attr.ndev = ndev; 742 gid_attr.ndev = ndev;
680 743
681 for (gid_type = 0; gid_type < IB_GID_TYPE_SIZE; ++gid_type) { 744 for (gid_type = 0; gid_type < IB_GID_TYPE_SIZE; ++gid_type) {
682 int ix;
683 union ib_gid current_gid;
684 struct ib_gid_attr current_gid_attr = {};
685
686 if (1UL << gid_type & ~gid_type_mask) 745 if (1UL << gid_type & ~gid_type_mask)
687 continue; 746 continue;
688 747
689 gid_attr.gid_type = gid_type; 748 gid_attr.gid_type = gid_type;
690 749
691 mutex_lock(&table->lock);
692 write_lock_irq(&table->rwlock);
693 ix = find_gid(table, NULL, &gid_attr, true,
694 GID_ATTR_FIND_MASK_GID_TYPE |
695 GID_ATTR_FIND_MASK_DEFAULT,
696 NULL);
697
698 /* Coudn't find default GID location */
699 if (WARN_ON(ix < 0))
700 goto release;
701
702 zattr_type.gid_type = gid_type;
703
704 if (!__ib_cache_gid_get(ib_dev, port, ix,
705 &current_gid, &current_gid_attr) &&
706 mode == IB_CACHE_GID_DEFAULT_MODE_SET &&
707 !memcmp(&gid, &current_gid, sizeof(gid)) &&
708 !memcmp(&gid_attr, &current_gid_attr, sizeof(gid_attr)))
709 goto release;
710
711 if (memcmp(&current_gid, &zgid, sizeof(current_gid)) ||
712 memcmp(&current_gid_attr, &zattr_type,
713 sizeof(current_gid_attr))) {
714 if (del_gid(ib_dev, port, table, ix, true)) {
715 pr_warn("ib_cache_gid: can't delete index %d for default gid %pI6\n",
716 ix, gid.raw);
717 goto release;
718 } else {
719 dispatch_gid_change_event(ib_dev, port);
720 }
721 }
722
723 if (mode == IB_CACHE_GID_DEFAULT_MODE_SET) { 750 if (mode == IB_CACHE_GID_DEFAULT_MODE_SET) {
724 if (add_gid(ib_dev, port, table, ix, &gid, &gid_attr, true)) 751 mask = GID_ATTR_FIND_MASK_GID_TYPE |
725 pr_warn("ib_cache_gid: unable to add default gid %pI6\n", 752 GID_ATTR_FIND_MASK_DEFAULT;
726 gid.raw); 753 __ib_cache_gid_add(ib_dev, port, &gid,
727 else 754 &gid_attr, mask, true);
728 dispatch_gid_change_event(ib_dev, port); 755 } else if (mode == IB_CACHE_GID_DEFAULT_MODE_DELETE) {
756 ib_cache_gid_del(ib_dev, port, &gid, &gid_attr);
729 } 757 }
730
731release:
732 if (current_gid_attr.ndev)
733 dev_put(current_gid_attr.ndev);
734 write_unlock_irq(&table->rwlock);
735 mutex_unlock(&table->lock);
736 } 758 }
737} 759}
738 760
@@ -1057,25 +1079,50 @@ int ib_get_cached_port_state(struct ib_device *device,
1057} 1079}
1058EXPORT_SYMBOL(ib_get_cached_port_state); 1080EXPORT_SYMBOL(ib_get_cached_port_state);
1059 1081
1082static int config_non_roce_gid_cache(struct ib_device *device,
1083 u8 port, int gid_tbl_len)
1084{
1085 struct ib_gid_attr gid_attr = {};
1086 struct ib_gid_table *table;
1087 union ib_gid gid;
1088 int ret = 0;
1089 int i;
1090
1091 gid_attr.device = device;
1092 gid_attr.port_num = port;
1093 table = device->cache.ports[port - rdma_start_port(device)].gid;
1094
1095 mutex_lock(&table->lock);
1096 for (i = 0; i < gid_tbl_len; ++i) {
1097 if (!device->query_gid)
1098 continue;
1099 ret = device->query_gid(device, port, i, &gid);
1100 if (ret) {
1101 pr_warn("query_gid failed (%d) for %s (index %d)\n",
1102 ret, device->name, i);
1103 goto err;
1104 }
1105 gid_attr.index = i;
1106 add_modify_gid(table, &gid, &gid_attr);
1107 }
1108err:
1109 mutex_unlock(&table->lock);
1110 return ret;
1111}
1112
1060static void ib_cache_update(struct ib_device *device, 1113static void ib_cache_update(struct ib_device *device,
1061 u8 port, 1114 u8 port,
1062 bool enforce_security) 1115 bool enforce_security)
1063{ 1116{
1064 struct ib_port_attr *tprops = NULL; 1117 struct ib_port_attr *tprops = NULL;
1065 struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache; 1118 struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache;
1066 struct ib_gid_cache {
1067 int table_len;
1068 union ib_gid table[0];
1069 } *gid_cache = NULL;
1070 int i; 1119 int i;
1071 int ret; 1120 int ret;
1072 struct ib_gid_table *table; 1121 struct ib_gid_table *table;
1073 bool use_roce_gid_table;
1074 1122
1075 if (!rdma_is_port_valid(device, port)) 1123 if (!rdma_is_port_valid(device, port))
1076 return; 1124 return;
1077 1125
1078 use_roce_gid_table = rdma_protocol_roce(device, port);
1079 table = device->cache.ports[port - rdma_start_port(device)].gid; 1126 table = device->cache.ports[port - rdma_start_port(device)].gid;
1080 1127
1081 tprops = kmalloc(sizeof *tprops, GFP_KERNEL); 1128 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
@@ -1089,6 +1136,13 @@ static void ib_cache_update(struct ib_device *device,
1089 goto err; 1136 goto err;
1090 } 1137 }
1091 1138
1139 if (!rdma_protocol_roce(device, port)) {
1140 ret = config_non_roce_gid_cache(device, port,
1141 tprops->gid_tbl_len);
1142 if (ret)
1143 goto err;
1144 }
1145
1092 pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len * 1146 pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
1093 sizeof *pkey_cache->table, GFP_KERNEL); 1147 sizeof *pkey_cache->table, GFP_KERNEL);
1094 if (!pkey_cache) 1148 if (!pkey_cache)
@@ -1096,15 +1150,6 @@ static void ib_cache_update(struct ib_device *device,
1096 1150
1097 pkey_cache->table_len = tprops->pkey_tbl_len; 1151 pkey_cache->table_len = tprops->pkey_tbl_len;
1098 1152
1099 if (!use_roce_gid_table) {
1100 gid_cache = kmalloc(sizeof(*gid_cache) + tprops->gid_tbl_len *
1101 sizeof(*gid_cache->table), GFP_KERNEL);
1102 if (!gid_cache)
1103 goto err;
1104
1105 gid_cache->table_len = tprops->gid_tbl_len;
1106 }
1107
1108 for (i = 0; i < pkey_cache->table_len; ++i) { 1153 for (i = 0; i < pkey_cache->table_len; ++i) {
1109 ret = ib_query_pkey(device, port, i, pkey_cache->table + i); 1154 ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
1110 if (ret) { 1155 if (ret) {
@@ -1114,33 +1159,12 @@ static void ib_cache_update(struct ib_device *device,
1114 } 1159 }
1115 } 1160 }
1116 1161
1117 if (!use_roce_gid_table) {
1118 for (i = 0; i < gid_cache->table_len; ++i) {
1119 ret = device->query_gid(device, port, i,
1120 gid_cache->table + i);
1121 if (ret) {
1122 pr_warn("ib_query_gid failed (%d) for %s (index %d)\n",
1123 ret, device->name, i);
1124 goto err;
1125 }
1126 }
1127 }
1128
1129 write_lock_irq(&device->cache.lock); 1162 write_lock_irq(&device->cache.lock);
1130 1163
1131 old_pkey_cache = device->cache.ports[port - 1164 old_pkey_cache = device->cache.ports[port -
1132 rdma_start_port(device)].pkey; 1165 rdma_start_port(device)].pkey;
1133 1166
1134 device->cache.ports[port - rdma_start_port(device)].pkey = pkey_cache; 1167 device->cache.ports[port - rdma_start_port(device)].pkey = pkey_cache;
1135 if (!use_roce_gid_table) {
1136 write_lock(&table->rwlock);
1137 for (i = 0; i < gid_cache->table_len; i++) {
1138 modify_gid(device, port, table, i, gid_cache->table + i,
1139 &zattr, false);
1140 }
1141 write_unlock(&table->rwlock);
1142 }
1143
1144 device->cache.ports[port - rdma_start_port(device)].lmc = tprops->lmc; 1168 device->cache.ports[port - rdma_start_port(device)].lmc = tprops->lmc;
1145 device->cache.ports[port - rdma_start_port(device)].port_state = 1169 device->cache.ports[port - rdma_start_port(device)].port_state =
1146 tprops->state; 1170 tprops->state;
@@ -1154,14 +1178,12 @@ static void ib_cache_update(struct ib_device *device,
1154 port, 1178 port,
1155 tprops->subnet_prefix); 1179 tprops->subnet_prefix);
1156 1180
1157 kfree(gid_cache);
1158 kfree(old_pkey_cache); 1181 kfree(old_pkey_cache);
1159 kfree(tprops); 1182 kfree(tprops);
1160 return; 1183 return;
1161 1184
1162err: 1185err:
1163 kfree(pkey_cache); 1186 kfree(pkey_cache);
1164 kfree(gid_cache);
1165 kfree(tprops); 1187 kfree(tprops);
1166} 1188}
1167 1189