cache.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Intel Corporation. All rights reserved.
  4. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  5. * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/module.h>
  36. #include <linux/errno.h>
  37. #include <linux/slab.h>
  38. #include <linux/workqueue.h>
  39. #include <linux/netdevice.h>
  40. #include <net/addrconf.h>
  41. #include <rdma/ib_cache.h>
  42. #include "core_priv.h"
  43. struct ib_pkey_cache {
  44. int table_len;
  45. u16 table[0];
  46. };
  47. struct ib_update_work {
  48. struct work_struct work;
  49. struct ib_device *device;
  50. u8 port_num;
  51. };
  52. union ib_gid zgid;
  53. EXPORT_SYMBOL(zgid);
  54. static const struct ib_gid_attr zattr;
  55. enum gid_attr_find_mask {
  56. GID_ATTR_FIND_MASK_GID = 1UL << 0,
  57. GID_ATTR_FIND_MASK_NETDEV = 1UL << 1,
  58. GID_ATTR_FIND_MASK_DEFAULT = 1UL << 2,
  59. };
  60. enum gid_table_entry_props {
  61. GID_TABLE_ENTRY_INVALID = 1UL << 0,
  62. GID_TABLE_ENTRY_DEFAULT = 1UL << 1,
  63. };
  64. enum gid_table_write_action {
  65. GID_TABLE_WRITE_ACTION_ADD,
  66. GID_TABLE_WRITE_ACTION_DEL,
  67. /* MODIFY only updates the GID table. Currently only used by
  68. * ib_cache_update.
  69. */
  70. GID_TABLE_WRITE_ACTION_MODIFY
  71. };
  72. struct ib_gid_table_entry {
  73. /* This lock protects an entry from being
  74. * read and written simultaneously.
  75. */
  76. rwlock_t lock;
  77. unsigned long props;
  78. union ib_gid gid;
  79. struct ib_gid_attr attr;
  80. void *context;
  81. };
  82. struct ib_gid_table {
  83. int sz;
  84. /* In RoCE, adding a GID to the table requires:
  85. * (a) Find if this GID is already exists.
  86. * (b) Find a free space.
  87. * (c) Write the new GID
  88. *
  89. * Delete requires different set of operations:
  90. * (a) Find the GID
  91. * (b) Delete it.
  92. *
  93. * Add/delete should be carried out atomically.
  94. * This is done by locking this mutex from multiple
  95. * writers. We don't need this lock for IB, as the MAD
  96. * layer replaces all entries. All data_vec entries
  97. * are locked by this lock.
  98. **/
  99. struct mutex lock;
  100. struct ib_gid_table_entry *data_vec;
  101. };
  102. static int write_gid(struct ib_device *ib_dev, u8 port,
  103. struct ib_gid_table *table, int ix,
  104. const union ib_gid *gid,
  105. const struct ib_gid_attr *attr,
  106. enum gid_table_write_action action,
  107. bool default_gid)
  108. {
  109. int ret = 0;
  110. struct net_device *old_net_dev;
  111. unsigned long flags;
  112. /* in rdma_cap_roce_gid_table, this funciton should be protected by a
  113. * sleep-able lock.
  114. */
  115. write_lock_irqsave(&table->data_vec[ix].lock, flags);
  116. if (rdma_cap_roce_gid_table(ib_dev, port)) {
  117. table->data_vec[ix].props |= GID_TABLE_ENTRY_INVALID;
  118. write_unlock_irqrestore(&table->data_vec[ix].lock, flags);
  119. /* GID_TABLE_WRITE_ACTION_MODIFY currently isn't supported by
  120. * RoCE providers and thus only updates the cache.
  121. */
  122. if (action == GID_TABLE_WRITE_ACTION_ADD)
  123. ret = ib_dev->add_gid(ib_dev, port, ix, gid, attr,
  124. &table->data_vec[ix].context);
  125. else if (action == GID_TABLE_WRITE_ACTION_DEL)
  126. ret = ib_dev->del_gid(ib_dev, port, ix,
  127. &table->data_vec[ix].context);
  128. write_lock_irqsave(&table->data_vec[ix].lock, flags);
  129. }
  130. old_net_dev = table->data_vec[ix].attr.ndev;
  131. if (old_net_dev && old_net_dev != attr->ndev)
  132. dev_put(old_net_dev);
  133. /* if modify_gid failed, just delete the old gid */
  134. if (ret || action == GID_TABLE_WRITE_ACTION_DEL) {
  135. gid = &zgid;
  136. attr = &zattr;
  137. table->data_vec[ix].context = NULL;
  138. }
  139. if (default_gid)
  140. table->data_vec[ix].props |= GID_TABLE_ENTRY_DEFAULT;
  141. memcpy(&table->data_vec[ix].gid, gid, sizeof(*gid));
  142. memcpy(&table->data_vec[ix].attr, attr, sizeof(*attr));
  143. if (table->data_vec[ix].attr.ndev &&
  144. table->data_vec[ix].attr.ndev != old_net_dev)
  145. dev_hold(table->data_vec[ix].attr.ndev);
  146. table->data_vec[ix].props &= ~GID_TABLE_ENTRY_INVALID;
  147. write_unlock_irqrestore(&table->data_vec[ix].lock, flags);
  148. if (!ret && rdma_cap_roce_gid_table(ib_dev, port)) {
  149. struct ib_event event;
  150. event.device = ib_dev;
  151. event.element.port_num = port;
  152. event.event = IB_EVENT_GID_CHANGE;
  153. ib_dispatch_event(&event);
  154. }
  155. return ret;
  156. }
  157. static int add_gid(struct ib_device *ib_dev, u8 port,
  158. struct ib_gid_table *table, int ix,
  159. const union ib_gid *gid,
  160. const struct ib_gid_attr *attr,
  161. bool default_gid) {
  162. return write_gid(ib_dev, port, table, ix, gid, attr,
  163. GID_TABLE_WRITE_ACTION_ADD, default_gid);
  164. }
  165. static int modify_gid(struct ib_device *ib_dev, u8 port,
  166. struct ib_gid_table *table, int ix,
  167. const union ib_gid *gid,
  168. const struct ib_gid_attr *attr,
  169. bool default_gid) {
  170. return write_gid(ib_dev, port, table, ix, gid, attr,
  171. GID_TABLE_WRITE_ACTION_MODIFY, default_gid);
  172. }
  173. static int del_gid(struct ib_device *ib_dev, u8 port,
  174. struct ib_gid_table *table, int ix,
  175. bool default_gid) {
  176. return write_gid(ib_dev, port, table, ix, &zgid, &zattr,
  177. GID_TABLE_WRITE_ACTION_DEL, default_gid);
  178. }
  179. static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,
  180. const struct ib_gid_attr *val, bool default_gid,
  181. unsigned long mask)
  182. {
  183. int i;
  184. for (i = 0; i < table->sz; i++) {
  185. unsigned long flags;
  186. struct ib_gid_attr *attr = &table->data_vec[i].attr;
  187. read_lock_irqsave(&table->data_vec[i].lock, flags);
  188. if (table->data_vec[i].props & GID_TABLE_ENTRY_INVALID)
  189. goto next;
  190. if (mask & GID_ATTR_FIND_MASK_GID &&
  191. memcmp(gid, &table->data_vec[i].gid, sizeof(*gid)))
  192. goto next;
  193. if (mask & GID_ATTR_FIND_MASK_NETDEV &&
  194. attr->ndev != val->ndev)
  195. goto next;
  196. if (mask & GID_ATTR_FIND_MASK_DEFAULT &&
  197. !!(table->data_vec[i].props & GID_TABLE_ENTRY_DEFAULT) !=
  198. default_gid)
  199. goto next;
  200. read_unlock_irqrestore(&table->data_vec[i].lock, flags);
  201. return i;
  202. next:
  203. read_unlock_irqrestore(&table->data_vec[i].lock, flags);
  204. }
  205. return -1;
  206. }
  207. static void make_default_gid(struct net_device *dev, union ib_gid *gid)
  208. {
  209. gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
  210. addrconf_ifid_eui48(&gid->raw[8], dev);
  211. }
  212. int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
  213. union ib_gid *gid, struct ib_gid_attr *attr)
  214. {
  215. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  216. struct ib_gid_table *table;
  217. int ix;
  218. int ret = 0;
  219. struct net_device *idev;
  220. table = ports_table[port - rdma_start_port(ib_dev)];
  221. if (!memcmp(gid, &zgid, sizeof(*gid)))
  222. return -EINVAL;
  223. if (ib_dev->get_netdev) {
  224. idev = ib_dev->get_netdev(ib_dev, port);
  225. if (idev && attr->ndev != idev) {
  226. union ib_gid default_gid;
  227. /* Adding default GIDs in not permitted */
  228. make_default_gid(idev, &default_gid);
  229. if (!memcmp(gid, &default_gid, sizeof(*gid))) {
  230. dev_put(idev);
  231. return -EPERM;
  232. }
  233. }
  234. if (idev)
  235. dev_put(idev);
  236. }
  237. mutex_lock(&table->lock);
  238. ix = find_gid(table, gid, attr, false, GID_ATTR_FIND_MASK_GID |
  239. GID_ATTR_FIND_MASK_NETDEV);
  240. if (ix >= 0)
  241. goto out_unlock;
  242. ix = find_gid(table, &zgid, NULL, false, GID_ATTR_FIND_MASK_GID |
  243. GID_ATTR_FIND_MASK_DEFAULT);
  244. if (ix < 0) {
  245. ret = -ENOSPC;
  246. goto out_unlock;
  247. }
  248. add_gid(ib_dev, port, table, ix, gid, attr, false);
  249. out_unlock:
  250. mutex_unlock(&table->lock);
  251. return ret;
  252. }
  253. int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
  254. union ib_gid *gid, struct ib_gid_attr *attr)
  255. {
  256. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  257. struct ib_gid_table *table;
  258. int ix;
  259. table = ports_table[port - rdma_start_port(ib_dev)];
  260. mutex_lock(&table->lock);
  261. ix = find_gid(table, gid, attr, false,
  262. GID_ATTR_FIND_MASK_GID |
  263. GID_ATTR_FIND_MASK_NETDEV |
  264. GID_ATTR_FIND_MASK_DEFAULT);
  265. if (ix < 0)
  266. goto out_unlock;
  267. del_gid(ib_dev, port, table, ix, false);
  268. out_unlock:
  269. mutex_unlock(&table->lock);
  270. return 0;
  271. }
  272. int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
  273. struct net_device *ndev)
  274. {
  275. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  276. struct ib_gid_table *table;
  277. int ix;
  278. table = ports_table[port - rdma_start_port(ib_dev)];
  279. mutex_lock(&table->lock);
  280. for (ix = 0; ix < table->sz; ix++)
  281. if (table->data_vec[ix].attr.ndev == ndev)
  282. del_gid(ib_dev, port, table, ix, false);
  283. mutex_unlock(&table->lock);
  284. return 0;
  285. }
  286. static int __ib_cache_gid_get(struct ib_device *ib_dev, u8 port, int index,
  287. union ib_gid *gid, struct ib_gid_attr *attr)
  288. {
  289. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  290. struct ib_gid_table *table;
  291. unsigned long flags;
  292. table = ports_table[port - rdma_start_port(ib_dev)];
  293. if (index < 0 || index >= table->sz)
  294. return -EINVAL;
  295. read_lock_irqsave(&table->data_vec[index].lock, flags);
  296. if (table->data_vec[index].props & GID_TABLE_ENTRY_INVALID) {
  297. read_unlock_irqrestore(&table->data_vec[index].lock, flags);
  298. return -EAGAIN;
  299. }
  300. memcpy(gid, &table->data_vec[index].gid, sizeof(*gid));
  301. if (attr) {
  302. memcpy(attr, &table->data_vec[index].attr, sizeof(*attr));
  303. if (attr->ndev)
  304. dev_hold(attr->ndev);
  305. }
  306. read_unlock_irqrestore(&table->data_vec[index].lock, flags);
  307. return 0;
  308. }
  309. static int _ib_cache_gid_table_find(struct ib_device *ib_dev,
  310. const union ib_gid *gid,
  311. const struct ib_gid_attr *val,
  312. unsigned long mask,
  313. u8 *port, u16 *index)
  314. {
  315. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  316. struct ib_gid_table *table;
  317. u8 p;
  318. int local_index;
  319. for (p = 0; p < ib_dev->phys_port_cnt; p++) {
  320. table = ports_table[p];
  321. local_index = find_gid(table, gid, val, false, mask);
  322. if (local_index >= 0) {
  323. if (index)
  324. *index = local_index;
  325. if (port)
  326. *port = p + rdma_start_port(ib_dev);
  327. return 0;
  328. }
  329. }
  330. return -ENOENT;
  331. }
  332. static int ib_cache_gid_find(struct ib_device *ib_dev,
  333. const union ib_gid *gid,
  334. struct net_device *ndev, u8 *port,
  335. u16 *index)
  336. {
  337. unsigned long mask = GID_ATTR_FIND_MASK_GID;
  338. struct ib_gid_attr gid_attr_val = {.ndev = ndev};
  339. if (ndev)
  340. mask |= GID_ATTR_FIND_MASK_NETDEV;
  341. return _ib_cache_gid_table_find(ib_dev, gid, &gid_attr_val,
  342. mask, port, index);
  343. }
  344. int ib_cache_gid_find_by_port(struct ib_device *ib_dev,
  345. const union ib_gid *gid,
  346. u8 port, struct net_device *ndev,
  347. u16 *index)
  348. {
  349. int local_index;
  350. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  351. struct ib_gid_table *table;
  352. unsigned long mask = GID_ATTR_FIND_MASK_GID;
  353. struct ib_gid_attr val = {.ndev = ndev};
  354. if (port < rdma_start_port(ib_dev) ||
  355. port > rdma_end_port(ib_dev))
  356. return -ENOENT;
  357. table = ports_table[port - rdma_start_port(ib_dev)];
  358. if (ndev)
  359. mask |= GID_ATTR_FIND_MASK_NETDEV;
  360. local_index = find_gid(table, gid, &val, false, mask);
  361. if (local_index >= 0) {
  362. if (index)
  363. *index = local_index;
  364. return 0;
  365. }
  366. return -ENOENT;
  367. }
  368. static struct ib_gid_table *alloc_gid_table(int sz)
  369. {
  370. unsigned int i;
  371. struct ib_gid_table *table =
  372. kzalloc(sizeof(struct ib_gid_table), GFP_KERNEL);
  373. if (!table)
  374. return NULL;
  375. table->data_vec = kcalloc(sz, sizeof(*table->data_vec), GFP_KERNEL);
  376. if (!table->data_vec)
  377. goto err_free_table;
  378. mutex_init(&table->lock);
  379. table->sz = sz;
  380. for (i = 0; i < sz; i++)
  381. rwlock_init(&table->data_vec[i].lock);
  382. return table;
  383. err_free_table:
  384. kfree(table);
  385. return NULL;
  386. }
  387. static void release_gid_table(struct ib_gid_table *table)
  388. {
  389. if (table) {
  390. kfree(table->data_vec);
  391. kfree(table);
  392. }
  393. }
  394. static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port,
  395. struct ib_gid_table *table)
  396. {
  397. int i;
  398. if (!table)
  399. return;
  400. for (i = 0; i < table->sz; ++i) {
  401. if (memcmp(&table->data_vec[i].gid, &zgid,
  402. sizeof(table->data_vec[i].gid)))
  403. del_gid(ib_dev, port, table, i,
  404. table->data_vec[i].props &
  405. GID_ATTR_FIND_MASK_DEFAULT);
  406. }
  407. }
  408. void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
  409. struct net_device *ndev,
  410. enum ib_cache_gid_default_mode mode)
  411. {
  412. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  413. union ib_gid gid;
  414. struct ib_gid_attr gid_attr;
  415. struct ib_gid_table *table;
  416. int ix;
  417. union ib_gid current_gid;
  418. struct ib_gid_attr current_gid_attr = {};
  419. table = ports_table[port - rdma_start_port(ib_dev)];
  420. make_default_gid(ndev, &gid);
  421. memset(&gid_attr, 0, sizeof(gid_attr));
  422. gid_attr.ndev = ndev;
  423. ix = find_gid(table, NULL, NULL, true, GID_ATTR_FIND_MASK_DEFAULT);
  424. /* Coudn't find default GID location */
  425. WARN_ON(ix < 0);
  426. mutex_lock(&table->lock);
  427. if (!__ib_cache_gid_get(ib_dev, port, ix,
  428. &current_gid, &current_gid_attr) &&
  429. mode == IB_CACHE_GID_DEFAULT_MODE_SET &&
  430. !memcmp(&gid, &current_gid, sizeof(gid)) &&
  431. !memcmp(&gid_attr, &current_gid_attr, sizeof(gid_attr)))
  432. goto unlock;
  433. if ((memcmp(&current_gid, &zgid, sizeof(current_gid)) ||
  434. memcmp(&current_gid_attr, &zattr,
  435. sizeof(current_gid_attr))) &&
  436. del_gid(ib_dev, port, table, ix, true)) {
  437. pr_warn("ib_cache_gid: can't delete index %d for default gid %pI6\n",
  438. ix, gid.raw);
  439. goto unlock;
  440. }
  441. if (mode == IB_CACHE_GID_DEFAULT_MODE_SET)
  442. if (add_gid(ib_dev, port, table, ix, &gid, &gid_attr, true))
  443. pr_warn("ib_cache_gid: unable to add default gid %pI6\n",
  444. gid.raw);
  445. unlock:
  446. if (current_gid_attr.ndev)
  447. dev_put(current_gid_attr.ndev);
  448. mutex_unlock(&table->lock);
  449. }
  450. static int gid_table_reserve_default(struct ib_device *ib_dev, u8 port,
  451. struct ib_gid_table *table)
  452. {
  453. if (rdma_protocol_roce(ib_dev, port)) {
  454. struct ib_gid_table_entry *entry = &table->data_vec[0];
  455. entry->props |= GID_TABLE_ENTRY_DEFAULT;
  456. }
  457. return 0;
  458. }
  459. static int _gid_table_setup_one(struct ib_device *ib_dev)
  460. {
  461. u8 port;
  462. struct ib_gid_table **table;
  463. int err = 0;
  464. table = kcalloc(ib_dev->phys_port_cnt, sizeof(*table), GFP_KERNEL);
  465. if (!table) {
  466. pr_warn("failed to allocate ib gid cache for %s\n",
  467. ib_dev->name);
  468. return -ENOMEM;
  469. }
  470. for (port = 0; port < ib_dev->phys_port_cnt; port++) {
  471. u8 rdma_port = port + rdma_start_port(ib_dev);
  472. table[port] =
  473. alloc_gid_table(
  474. ib_dev->port_immutable[rdma_port].gid_tbl_len);
  475. if (!table[port]) {
  476. err = -ENOMEM;
  477. goto rollback_table_setup;
  478. }
  479. err = gid_table_reserve_default(ib_dev,
  480. port + rdma_start_port(ib_dev),
  481. table[port]);
  482. if (err)
  483. goto rollback_table_setup;
  484. }
  485. ib_dev->cache.gid_cache = table;
  486. return 0;
  487. rollback_table_setup:
  488. for (port = 0; port < ib_dev->phys_port_cnt; port++) {
  489. cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
  490. table[port]);
  491. release_gid_table(table[port]);
  492. }
  493. kfree(table);
  494. return err;
  495. }
  496. static void gid_table_release_one(struct ib_device *ib_dev)
  497. {
  498. struct ib_gid_table **table = ib_dev->cache.gid_cache;
  499. u8 port;
  500. if (!table)
  501. return;
  502. for (port = 0; port < ib_dev->phys_port_cnt; port++)
  503. release_gid_table(table[port]);
  504. kfree(table);
  505. ib_dev->cache.gid_cache = NULL;
  506. }
  507. static void gid_table_cleanup_one(struct ib_device *ib_dev)
  508. {
  509. struct ib_gid_table **table = ib_dev->cache.gid_cache;
  510. u8 port;
  511. if (!table)
  512. return;
  513. for (port = 0; port < ib_dev->phys_port_cnt; port++)
  514. cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
  515. table[port]);
  516. }
  517. static int gid_table_setup_one(struct ib_device *ib_dev)
  518. {
  519. int err;
  520. err = _gid_table_setup_one(ib_dev);
  521. if (err)
  522. return err;
  523. err = roce_rescan_device(ib_dev);
  524. if (err) {
  525. gid_table_cleanup_one(ib_dev);
  526. gid_table_release_one(ib_dev);
  527. }
  528. return err;
  529. }
  530. int ib_get_cached_gid(struct ib_device *device,
  531. u8 port_num,
  532. int index,
  533. union ib_gid *gid)
  534. {
  535. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  536. return -EINVAL;
  537. return __ib_cache_gid_get(device, port_num, index, gid, NULL);
  538. }
  539. EXPORT_SYMBOL(ib_get_cached_gid);
  540. int ib_find_cached_gid(struct ib_device *device,
  541. const union ib_gid *gid,
  542. u8 *port_num,
  543. u16 *index)
  544. {
  545. return ib_cache_gid_find(device, gid, NULL, port_num, index);
  546. }
  547. EXPORT_SYMBOL(ib_find_cached_gid);
  548. int ib_get_cached_pkey(struct ib_device *device,
  549. u8 port_num,
  550. int index,
  551. u16 *pkey)
  552. {
  553. struct ib_pkey_cache *cache;
  554. unsigned long flags;
  555. int ret = 0;
  556. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  557. return -EINVAL;
  558. read_lock_irqsave(&device->cache.lock, flags);
  559. cache = device->cache.pkey_cache[port_num - rdma_start_port(device)];
  560. if (index < 0 || index >= cache->table_len)
  561. ret = -EINVAL;
  562. else
  563. *pkey = cache->table[index];
  564. read_unlock_irqrestore(&device->cache.lock, flags);
  565. return ret;
  566. }
  567. EXPORT_SYMBOL(ib_get_cached_pkey);
  568. int ib_find_cached_pkey(struct ib_device *device,
  569. u8 port_num,
  570. u16 pkey,
  571. u16 *index)
  572. {
  573. struct ib_pkey_cache *cache;
  574. unsigned long flags;
  575. int i;
  576. int ret = -ENOENT;
  577. int partial_ix = -1;
  578. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  579. return -EINVAL;
  580. read_lock_irqsave(&device->cache.lock, flags);
  581. cache = device->cache.pkey_cache[port_num - rdma_start_port(device)];
  582. *index = -1;
  583. for (i = 0; i < cache->table_len; ++i)
  584. if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
  585. if (cache->table[i] & 0x8000) {
  586. *index = i;
  587. ret = 0;
  588. break;
  589. } else
  590. partial_ix = i;
  591. }
  592. if (ret && partial_ix >= 0) {
  593. *index = partial_ix;
  594. ret = 0;
  595. }
  596. read_unlock_irqrestore(&device->cache.lock, flags);
  597. return ret;
  598. }
  599. EXPORT_SYMBOL(ib_find_cached_pkey);
  600. int ib_find_exact_cached_pkey(struct ib_device *device,
  601. u8 port_num,
  602. u16 pkey,
  603. u16 *index)
  604. {
  605. struct ib_pkey_cache *cache;
  606. unsigned long flags;
  607. int i;
  608. int ret = -ENOENT;
  609. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  610. return -EINVAL;
  611. read_lock_irqsave(&device->cache.lock, flags);
  612. cache = device->cache.pkey_cache[port_num - rdma_start_port(device)];
  613. *index = -1;
  614. for (i = 0; i < cache->table_len; ++i)
  615. if (cache->table[i] == pkey) {
  616. *index = i;
  617. ret = 0;
  618. break;
  619. }
  620. read_unlock_irqrestore(&device->cache.lock, flags);
  621. return ret;
  622. }
  623. EXPORT_SYMBOL(ib_find_exact_cached_pkey);
  624. int ib_get_cached_lmc(struct ib_device *device,
  625. u8 port_num,
  626. u8 *lmc)
  627. {
  628. unsigned long flags;
  629. int ret = 0;
  630. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  631. return -EINVAL;
  632. read_lock_irqsave(&device->cache.lock, flags);
  633. *lmc = device->cache.lmc_cache[port_num - rdma_start_port(device)];
  634. read_unlock_irqrestore(&device->cache.lock, flags);
  635. return ret;
  636. }
  637. EXPORT_SYMBOL(ib_get_cached_lmc);
  638. static void ib_cache_update(struct ib_device *device,
  639. u8 port)
  640. {
  641. struct ib_port_attr *tprops = NULL;
  642. struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache;
  643. struct ib_gid_cache {
  644. int table_len;
  645. union ib_gid table[0];
  646. } *gid_cache = NULL;
  647. int i;
  648. int ret;
  649. struct ib_gid_table *table;
  650. struct ib_gid_table **ports_table = device->cache.gid_cache;
  651. bool use_roce_gid_table =
  652. rdma_cap_roce_gid_table(device, port);
  653. if (port < rdma_start_port(device) || port > rdma_end_port(device))
  654. return;
  655. table = ports_table[port - rdma_start_port(device)];
  656. tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
  657. if (!tprops)
  658. return;
  659. ret = ib_query_port(device, port, tprops);
  660. if (ret) {
  661. printk(KERN_WARNING "ib_query_port failed (%d) for %s\n",
  662. ret, device->name);
  663. goto err;
  664. }
  665. pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
  666. sizeof *pkey_cache->table, GFP_KERNEL);
  667. if (!pkey_cache)
  668. goto err;
  669. pkey_cache->table_len = tprops->pkey_tbl_len;
  670. if (!use_roce_gid_table) {
  671. gid_cache = kmalloc(sizeof(*gid_cache) + tprops->gid_tbl_len *
  672. sizeof(*gid_cache->table), GFP_KERNEL);
  673. if (!gid_cache)
  674. goto err;
  675. gid_cache->table_len = tprops->gid_tbl_len;
  676. }
  677. for (i = 0; i < pkey_cache->table_len; ++i) {
  678. ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
  679. if (ret) {
  680. printk(KERN_WARNING "ib_query_pkey failed (%d) for %s (index %d)\n",
  681. ret, device->name, i);
  682. goto err;
  683. }
  684. }
  685. if (!use_roce_gid_table) {
  686. for (i = 0; i < gid_cache->table_len; ++i) {
  687. ret = ib_query_gid(device, port, i,
  688. gid_cache->table + i);
  689. if (ret) {
  690. printk(KERN_WARNING "ib_query_gid failed (%d) for %s (index %d)\n",
  691. ret, device->name, i);
  692. goto err;
  693. }
  694. }
  695. }
  696. write_lock_irq(&device->cache.lock);
  697. old_pkey_cache = device->cache.pkey_cache[port - rdma_start_port(device)];
  698. device->cache.pkey_cache[port - rdma_start_port(device)] = pkey_cache;
  699. if (!use_roce_gid_table) {
  700. for (i = 0; i < gid_cache->table_len; i++) {
  701. modify_gid(device, port, table, i, gid_cache->table + i,
  702. &zattr, false);
  703. }
  704. }
  705. device->cache.lmc_cache[port - rdma_start_port(device)] = tprops->lmc;
  706. write_unlock_irq(&device->cache.lock);
  707. kfree(gid_cache);
  708. kfree(old_pkey_cache);
  709. kfree(tprops);
  710. return;
  711. err:
  712. kfree(pkey_cache);
  713. kfree(gid_cache);
  714. kfree(tprops);
  715. }
  716. static void ib_cache_task(struct work_struct *_work)
  717. {
  718. struct ib_update_work *work =
  719. container_of(_work, struct ib_update_work, work);
  720. ib_cache_update(work->device, work->port_num);
  721. kfree(work);
  722. }
  723. static void ib_cache_event(struct ib_event_handler *handler,
  724. struct ib_event *event)
  725. {
  726. struct ib_update_work *work;
  727. if (event->event == IB_EVENT_PORT_ERR ||
  728. event->event == IB_EVENT_PORT_ACTIVE ||
  729. event->event == IB_EVENT_LID_CHANGE ||
  730. event->event == IB_EVENT_PKEY_CHANGE ||
  731. event->event == IB_EVENT_SM_CHANGE ||
  732. event->event == IB_EVENT_CLIENT_REREGISTER ||
  733. event->event == IB_EVENT_GID_CHANGE) {
  734. work = kmalloc(sizeof *work, GFP_ATOMIC);
  735. if (work) {
  736. INIT_WORK(&work->work, ib_cache_task);
  737. work->device = event->device;
  738. work->port_num = event->element.port_num;
  739. queue_work(ib_wq, &work->work);
  740. }
  741. }
  742. }
  743. int ib_cache_setup_one(struct ib_device *device)
  744. {
  745. int p;
  746. int err;
  747. rwlock_init(&device->cache.lock);
  748. device->cache.pkey_cache =
  749. kzalloc(sizeof *device->cache.pkey_cache *
  750. (rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL);
  751. device->cache.lmc_cache = kmalloc(sizeof *device->cache.lmc_cache *
  752. (rdma_end_port(device) -
  753. rdma_start_port(device) + 1),
  754. GFP_KERNEL);
  755. if (!device->cache.pkey_cache ||
  756. !device->cache.lmc_cache) {
  757. printk(KERN_WARNING "Couldn't allocate cache "
  758. "for %s\n", device->name);
  759. return -ENOMEM;
  760. }
  761. err = gid_table_setup_one(device);
  762. if (err)
  763. /* Allocated memory will be cleaned in the release function */
  764. return err;
  765. for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
  766. ib_cache_update(device, p + rdma_start_port(device));
  767. INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
  768. device, ib_cache_event);
  769. err = ib_register_event_handler(&device->cache.event_handler);
  770. if (err)
  771. goto err;
  772. return 0;
  773. err:
  774. gid_table_cleanup_one(device);
  775. return err;
  776. }
  777. void ib_cache_release_one(struct ib_device *device)
  778. {
  779. int p;
  780. /*
  781. * The release function frees all the cache elements.
  782. * This function should be called as part of freeing
  783. * all the device's resources when the cache could no
  784. * longer be accessed.
  785. */
  786. if (device->cache.pkey_cache)
  787. for (p = 0;
  788. p <= rdma_end_port(device) - rdma_start_port(device); ++p)
  789. kfree(device->cache.pkey_cache[p]);
  790. gid_table_release_one(device);
  791. kfree(device->cache.pkey_cache);
  792. kfree(device->cache.lmc_cache);
  793. }
  794. void ib_cache_cleanup_one(struct ib_device *device)
  795. {
  796. /* The cleanup function unregisters the event handler,
  797. * waits for all in-progress workqueue elements and cleans
  798. * up the GID cache. This function should be called after
  799. * the device was removed from the devices list and all
  800. * clients were removed, so the cache exists but is
  801. * non-functional and shouldn't be updated anymore.
  802. */
  803. ib_unregister_event_handler(&device->cache.event_handler);
  804. flush_workqueue(ib_wq);
  805. gid_table_cleanup_one(device);
  806. }
  807. void __init ib_cache_setup(void)
  808. {
  809. roce_gid_mgmt_init();
  810. }
  811. void __exit ib_cache_cleanup(void)
  812. {
  813. roce_gid_mgmt_cleanup();
  814. }