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