cache.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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. GID_ATTR_FIND_MASK_GID_TYPE = 1UL << 3,
  60. };
  61. enum gid_table_entry_props {
  62. GID_TABLE_ENTRY_INVALID = 1UL << 0,
  63. GID_TABLE_ENTRY_DEFAULT = 1UL << 1,
  64. };
  65. enum gid_table_write_action {
  66. GID_TABLE_WRITE_ACTION_ADD,
  67. GID_TABLE_WRITE_ACTION_DEL,
  68. /* MODIFY only updates the GID table. Currently only used by
  69. * ib_cache_update.
  70. */
  71. GID_TABLE_WRITE_ACTION_MODIFY
  72. };
  73. struct ib_gid_table_entry {
  74. unsigned long props;
  75. union ib_gid gid;
  76. struct ib_gid_attr attr;
  77. void *context;
  78. };
  79. struct ib_gid_table {
  80. int sz;
  81. /* In RoCE, adding a GID to the table requires:
  82. * (a) Find if this GID is already exists.
  83. * (b) Find a free space.
  84. * (c) Write the new GID
  85. *
  86. * Delete requires different set of operations:
  87. * (a) Find the GID
  88. * (b) Delete it.
  89. *
  90. * Add/delete should be carried out atomically.
  91. * This is done by locking this mutex from multiple
  92. * writers. We don't need this lock for IB, as the MAD
  93. * layer replaces all entries. All data_vec entries
  94. * are locked by this lock.
  95. **/
  96. struct mutex lock;
  97. /* This lock protects the table entries from being
  98. * read and written simultaneously.
  99. */
  100. rwlock_t rwlock;
  101. struct ib_gid_table_entry *data_vec;
  102. };
  103. static void dispatch_gid_change_event(struct ib_device *ib_dev, u8 port)
  104. {
  105. if (rdma_cap_roce_gid_table(ib_dev, port)) {
  106. struct ib_event event;
  107. event.device = ib_dev;
  108. event.element.port_num = port;
  109. event.event = IB_EVENT_GID_CHANGE;
  110. ib_dispatch_event(&event);
  111. }
  112. }
  113. static const char * const gid_type_str[] = {
  114. [IB_GID_TYPE_IB] = "IB/RoCE v1",
  115. [IB_GID_TYPE_ROCE_UDP_ENCAP] = "RoCE v2",
  116. };
  117. const char *ib_cache_gid_type_str(enum ib_gid_type gid_type)
  118. {
  119. if (gid_type < ARRAY_SIZE(gid_type_str) && gid_type_str[gid_type])
  120. return gid_type_str[gid_type];
  121. return "Invalid GID type";
  122. }
  123. EXPORT_SYMBOL(ib_cache_gid_type_str);
  124. int ib_cache_gid_parse_type_str(const char *buf)
  125. {
  126. unsigned int i;
  127. size_t len;
  128. int err = -EINVAL;
  129. len = strlen(buf);
  130. if (len == 0)
  131. return -EINVAL;
  132. if (buf[len - 1] == '\n')
  133. len--;
  134. for (i = 0; i < ARRAY_SIZE(gid_type_str); ++i)
  135. if (gid_type_str[i] && !strncmp(buf, gid_type_str[i], len) &&
  136. len == strlen(gid_type_str[i])) {
  137. err = i;
  138. break;
  139. }
  140. return err;
  141. }
  142. EXPORT_SYMBOL(ib_cache_gid_parse_type_str);
  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. */
  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. __releases(&table->rwlock) __acquires(&table->rwlock)
  154. {
  155. int ret = 0;
  156. struct net_device *old_net_dev;
  157. /* in rdma_cap_roce_gid_table, this funciton should be protected by a
  158. * sleep-able lock.
  159. */
  160. if (rdma_cap_roce_gid_table(ib_dev, port)) {
  161. table->data_vec[ix].props |= GID_TABLE_ENTRY_INVALID;
  162. write_unlock_irq(&table->rwlock);
  163. /* GID_TABLE_WRITE_ACTION_MODIFY currently isn't supported by
  164. * RoCE providers and thus only updates the cache.
  165. */
  166. if (action == GID_TABLE_WRITE_ACTION_ADD)
  167. ret = ib_dev->add_gid(ib_dev, port, ix, gid, attr,
  168. &table->data_vec[ix].context);
  169. else if (action == GID_TABLE_WRITE_ACTION_DEL)
  170. ret = ib_dev->del_gid(ib_dev, port, ix,
  171. &table->data_vec[ix].context);
  172. write_lock_irq(&table->rwlock);
  173. }
  174. old_net_dev = table->data_vec[ix].attr.ndev;
  175. if (old_net_dev && old_net_dev != attr->ndev)
  176. dev_put(old_net_dev);
  177. /* if modify_gid failed, just delete the old gid */
  178. if (ret || action == GID_TABLE_WRITE_ACTION_DEL) {
  179. gid = &zgid;
  180. attr = &zattr;
  181. table->data_vec[ix].context = NULL;
  182. }
  183. if (default_gid)
  184. table->data_vec[ix].props |= GID_TABLE_ENTRY_DEFAULT;
  185. memcpy(&table->data_vec[ix].gid, gid, sizeof(*gid));
  186. memcpy(&table->data_vec[ix].attr, attr, sizeof(*attr));
  187. if (table->data_vec[ix].attr.ndev &&
  188. table->data_vec[ix].attr.ndev != old_net_dev)
  189. dev_hold(table->data_vec[ix].attr.ndev);
  190. table->data_vec[ix].props &= ~GID_TABLE_ENTRY_INVALID;
  191. return ret;
  192. }
  193. static int add_gid(struct ib_device *ib_dev, u8 port,
  194. struct ib_gid_table *table, int ix,
  195. const union ib_gid *gid,
  196. const struct ib_gid_attr *attr,
  197. bool default_gid) {
  198. return write_gid(ib_dev, port, table, ix, gid, attr,
  199. GID_TABLE_WRITE_ACTION_ADD, default_gid);
  200. }
  201. static int modify_gid(struct ib_device *ib_dev, u8 port,
  202. struct ib_gid_table *table, int ix,
  203. const union ib_gid *gid,
  204. const struct ib_gid_attr *attr,
  205. bool default_gid) {
  206. return write_gid(ib_dev, port, table, ix, gid, attr,
  207. GID_TABLE_WRITE_ACTION_MODIFY, default_gid);
  208. }
  209. static int del_gid(struct ib_device *ib_dev, u8 port,
  210. struct ib_gid_table *table, int ix,
  211. bool default_gid) {
  212. return write_gid(ib_dev, port, table, ix, &zgid, &zattr,
  213. GID_TABLE_WRITE_ACTION_DEL, default_gid);
  214. }
  215. /* rwlock should be read locked */
  216. static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,
  217. const struct ib_gid_attr *val, bool default_gid,
  218. unsigned long mask, int *pempty)
  219. {
  220. int i = 0;
  221. int found = -1;
  222. int empty = pempty ? -1 : 0;
  223. while (i < table->sz && (found < 0 || empty < 0)) {
  224. struct ib_gid_table_entry *data = &table->data_vec[i];
  225. struct ib_gid_attr *attr = &data->attr;
  226. int curr_index = i;
  227. i++;
  228. if (data->props & GID_TABLE_ENTRY_INVALID)
  229. continue;
  230. if (empty < 0)
  231. if (!memcmp(&data->gid, &zgid, sizeof(*gid)) &&
  232. !memcmp(attr, &zattr, sizeof(*attr)) &&
  233. !data->props)
  234. empty = curr_index;
  235. if (found >= 0)
  236. continue;
  237. if (mask & GID_ATTR_FIND_MASK_GID_TYPE &&
  238. attr->gid_type != val->gid_type)
  239. continue;
  240. if (mask & GID_ATTR_FIND_MASK_GID &&
  241. memcmp(gid, &data->gid, sizeof(*gid)))
  242. continue;
  243. if (mask & GID_ATTR_FIND_MASK_NETDEV &&
  244. attr->ndev != val->ndev)
  245. continue;
  246. if (mask & GID_ATTR_FIND_MASK_DEFAULT &&
  247. !!(data->props & GID_TABLE_ENTRY_DEFAULT) !=
  248. default_gid)
  249. continue;
  250. found = curr_index;
  251. }
  252. if (pempty)
  253. *pempty = empty;
  254. return found;
  255. }
  256. static void make_default_gid(struct net_device *dev, union ib_gid *gid)
  257. {
  258. gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
  259. addrconf_ifid_eui48(&gid->raw[8], dev);
  260. }
  261. int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
  262. union ib_gid *gid, struct ib_gid_attr *attr)
  263. {
  264. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  265. struct ib_gid_table *table;
  266. int ix;
  267. int ret = 0;
  268. struct net_device *idev;
  269. int empty;
  270. table = ports_table[port - rdma_start_port(ib_dev)];
  271. if (!memcmp(gid, &zgid, sizeof(*gid)))
  272. return -EINVAL;
  273. if (ib_dev->get_netdev) {
  274. idev = ib_dev->get_netdev(ib_dev, port);
  275. if (idev && attr->ndev != idev) {
  276. union ib_gid default_gid;
  277. /* Adding default GIDs in not permitted */
  278. make_default_gid(idev, &default_gid);
  279. if (!memcmp(gid, &default_gid, sizeof(*gid))) {
  280. dev_put(idev);
  281. return -EPERM;
  282. }
  283. }
  284. if (idev)
  285. dev_put(idev);
  286. }
  287. mutex_lock(&table->lock);
  288. write_lock_irq(&table->rwlock);
  289. ix = find_gid(table, gid, attr, false, GID_ATTR_FIND_MASK_GID |
  290. GID_ATTR_FIND_MASK_GID_TYPE |
  291. GID_ATTR_FIND_MASK_NETDEV, &empty);
  292. if (ix >= 0)
  293. goto out_unlock;
  294. if (empty < 0) {
  295. ret = -ENOSPC;
  296. goto out_unlock;
  297. }
  298. ret = add_gid(ib_dev, port, table, empty, gid, attr, false);
  299. if (!ret)
  300. dispatch_gid_change_event(ib_dev, port);
  301. out_unlock:
  302. write_unlock_irq(&table->rwlock);
  303. mutex_unlock(&table->lock);
  304. return ret;
  305. }
  306. int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
  307. union ib_gid *gid, struct ib_gid_attr *attr)
  308. {
  309. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  310. struct ib_gid_table *table;
  311. int ix;
  312. table = ports_table[port - rdma_start_port(ib_dev)];
  313. mutex_lock(&table->lock);
  314. write_lock_irq(&table->rwlock);
  315. ix = find_gid(table, gid, attr, false,
  316. GID_ATTR_FIND_MASK_GID |
  317. GID_ATTR_FIND_MASK_GID_TYPE |
  318. GID_ATTR_FIND_MASK_NETDEV |
  319. GID_ATTR_FIND_MASK_DEFAULT,
  320. NULL);
  321. if (ix < 0)
  322. goto out_unlock;
  323. if (!del_gid(ib_dev, port, table, ix, false))
  324. dispatch_gid_change_event(ib_dev, port);
  325. out_unlock:
  326. write_unlock_irq(&table->rwlock);
  327. mutex_unlock(&table->lock);
  328. return 0;
  329. }
  330. int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
  331. struct net_device *ndev)
  332. {
  333. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  334. struct ib_gid_table *table;
  335. int ix;
  336. bool deleted = false;
  337. table = ports_table[port - rdma_start_port(ib_dev)];
  338. mutex_lock(&table->lock);
  339. write_lock_irq(&table->rwlock);
  340. for (ix = 0; ix < table->sz; ix++)
  341. if (table->data_vec[ix].attr.ndev == ndev)
  342. if (!del_gid(ib_dev, port, table, ix, false))
  343. deleted = true;
  344. write_unlock_irq(&table->rwlock);
  345. mutex_unlock(&table->lock);
  346. if (deleted)
  347. dispatch_gid_change_event(ib_dev, port);
  348. return 0;
  349. }
  350. static int __ib_cache_gid_get(struct ib_device *ib_dev, u8 port, int index,
  351. union ib_gid *gid, struct ib_gid_attr *attr)
  352. {
  353. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  354. struct ib_gid_table *table;
  355. table = ports_table[port - rdma_start_port(ib_dev)];
  356. if (index < 0 || index >= table->sz)
  357. return -EINVAL;
  358. if (table->data_vec[index].props & GID_TABLE_ENTRY_INVALID)
  359. return -EAGAIN;
  360. memcpy(gid, &table->data_vec[index].gid, sizeof(*gid));
  361. if (attr) {
  362. memcpy(attr, &table->data_vec[index].attr, sizeof(*attr));
  363. if (attr->ndev)
  364. dev_hold(attr->ndev);
  365. }
  366. return 0;
  367. }
  368. static int _ib_cache_gid_table_find(struct ib_device *ib_dev,
  369. const union ib_gid *gid,
  370. const struct ib_gid_attr *val,
  371. unsigned long mask,
  372. u8 *port, u16 *index)
  373. {
  374. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  375. struct ib_gid_table *table;
  376. u8 p;
  377. int local_index;
  378. unsigned long flags;
  379. for (p = 0; p < ib_dev->phys_port_cnt; p++) {
  380. table = ports_table[p];
  381. read_lock_irqsave(&table->rwlock, flags);
  382. local_index = find_gid(table, gid, val, false, mask, NULL);
  383. if (local_index >= 0) {
  384. if (index)
  385. *index = local_index;
  386. if (port)
  387. *port = p + rdma_start_port(ib_dev);
  388. read_unlock_irqrestore(&table->rwlock, flags);
  389. return 0;
  390. }
  391. read_unlock_irqrestore(&table->rwlock, flags);
  392. }
  393. return -ENOENT;
  394. }
  395. static int ib_cache_gid_find(struct ib_device *ib_dev,
  396. const union ib_gid *gid,
  397. enum ib_gid_type gid_type,
  398. struct net_device *ndev, u8 *port,
  399. u16 *index)
  400. {
  401. unsigned long mask = GID_ATTR_FIND_MASK_GID |
  402. GID_ATTR_FIND_MASK_GID_TYPE;
  403. struct ib_gid_attr gid_attr_val = {.ndev = ndev, .gid_type = gid_type};
  404. if (ndev)
  405. mask |= GID_ATTR_FIND_MASK_NETDEV;
  406. return _ib_cache_gid_table_find(ib_dev, gid, &gid_attr_val,
  407. mask, port, index);
  408. }
  409. int ib_find_cached_gid_by_port(struct ib_device *ib_dev,
  410. const union ib_gid *gid,
  411. enum ib_gid_type gid_type,
  412. u8 port, struct net_device *ndev,
  413. u16 *index)
  414. {
  415. int local_index;
  416. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  417. struct ib_gid_table *table;
  418. unsigned long mask = GID_ATTR_FIND_MASK_GID |
  419. GID_ATTR_FIND_MASK_GID_TYPE;
  420. struct ib_gid_attr val = {.ndev = ndev, .gid_type = gid_type};
  421. unsigned long flags;
  422. if (port < rdma_start_port(ib_dev) ||
  423. port > rdma_end_port(ib_dev))
  424. return -ENOENT;
  425. table = ports_table[port - rdma_start_port(ib_dev)];
  426. if (ndev)
  427. mask |= GID_ATTR_FIND_MASK_NETDEV;
  428. read_lock_irqsave(&table->rwlock, flags);
  429. local_index = find_gid(table, gid, &val, false, mask, NULL);
  430. if (local_index >= 0) {
  431. if (index)
  432. *index = local_index;
  433. read_unlock_irqrestore(&table->rwlock, flags);
  434. return 0;
  435. }
  436. read_unlock_irqrestore(&table->rwlock, flags);
  437. return -ENOENT;
  438. }
  439. EXPORT_SYMBOL(ib_find_cached_gid_by_port);
  440. /**
  441. * ib_find_gid_by_filter - Returns the GID table index where a specified
  442. * GID value occurs
  443. * @device: The device to query.
  444. * @gid: The GID value to search for.
  445. * @port_num: The port number of the device where the GID value could be
  446. * searched.
  447. * @filter: The filter function is executed on any matching GID in the table.
  448. * If the filter function returns true, the corresponding index is returned,
  449. * otherwise, we continue searching the GID table. It's guaranteed that
  450. * while filter is executed, ndev field is valid and the structure won't
  451. * change. filter is executed in an atomic context. filter must not be NULL.
  452. * @index: The index into the cached GID table where the GID was found. This
  453. * parameter may be NULL.
  454. *
  455. * ib_cache_gid_find_by_filter() searches for the specified GID value
  456. * of which the filter function returns true in the port's GID table.
  457. * This function is only supported on RoCE ports.
  458. *
  459. */
  460. static int ib_cache_gid_find_by_filter(struct ib_device *ib_dev,
  461. const union ib_gid *gid,
  462. u8 port,
  463. bool (*filter)(const union ib_gid *,
  464. const struct ib_gid_attr *,
  465. void *),
  466. void *context,
  467. u16 *index)
  468. {
  469. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  470. struct ib_gid_table *table;
  471. unsigned int i;
  472. unsigned long flags;
  473. bool found = false;
  474. if (!ports_table)
  475. return -EOPNOTSUPP;
  476. if (port < rdma_start_port(ib_dev) ||
  477. port > rdma_end_port(ib_dev) ||
  478. !rdma_protocol_roce(ib_dev, port))
  479. return -EPROTONOSUPPORT;
  480. table = ports_table[port - rdma_start_port(ib_dev)];
  481. read_lock_irqsave(&table->rwlock, flags);
  482. for (i = 0; i < table->sz; i++) {
  483. struct ib_gid_attr attr;
  484. if (table->data_vec[i].props & GID_TABLE_ENTRY_INVALID)
  485. goto next;
  486. if (memcmp(gid, &table->data_vec[i].gid, sizeof(*gid)))
  487. goto next;
  488. memcpy(&attr, &table->data_vec[i].attr, sizeof(attr));
  489. if (filter(gid, &attr, context))
  490. found = true;
  491. next:
  492. if (found)
  493. break;
  494. }
  495. read_unlock_irqrestore(&table->rwlock, flags);
  496. if (!found)
  497. return -ENOENT;
  498. if (index)
  499. *index = i;
  500. return 0;
  501. }
  502. static struct ib_gid_table *alloc_gid_table(int sz)
  503. {
  504. struct ib_gid_table *table =
  505. kzalloc(sizeof(struct ib_gid_table), GFP_KERNEL);
  506. if (!table)
  507. return NULL;
  508. table->data_vec = kcalloc(sz, sizeof(*table->data_vec), GFP_KERNEL);
  509. if (!table->data_vec)
  510. goto err_free_table;
  511. mutex_init(&table->lock);
  512. table->sz = sz;
  513. rwlock_init(&table->rwlock);
  514. return table;
  515. err_free_table:
  516. kfree(table);
  517. return NULL;
  518. }
  519. static void release_gid_table(struct ib_gid_table *table)
  520. {
  521. if (table) {
  522. kfree(table->data_vec);
  523. kfree(table);
  524. }
  525. }
  526. static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port,
  527. struct ib_gid_table *table)
  528. {
  529. int i;
  530. bool deleted = false;
  531. if (!table)
  532. return;
  533. write_lock_irq(&table->rwlock);
  534. for (i = 0; i < table->sz; ++i) {
  535. if (memcmp(&table->data_vec[i].gid, &zgid,
  536. sizeof(table->data_vec[i].gid)))
  537. if (!del_gid(ib_dev, port, table, i,
  538. table->data_vec[i].props &
  539. GID_ATTR_FIND_MASK_DEFAULT))
  540. deleted = true;
  541. }
  542. write_unlock_irq(&table->rwlock);
  543. if (deleted)
  544. dispatch_gid_change_event(ib_dev, port);
  545. }
  546. void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
  547. struct net_device *ndev,
  548. unsigned long gid_type_mask,
  549. enum ib_cache_gid_default_mode mode)
  550. {
  551. struct ib_gid_table **ports_table = ib_dev->cache.gid_cache;
  552. union ib_gid gid;
  553. struct ib_gid_attr gid_attr;
  554. struct ib_gid_attr zattr_type = zattr;
  555. struct ib_gid_table *table;
  556. unsigned int gid_type;
  557. table = ports_table[port - rdma_start_port(ib_dev)];
  558. make_default_gid(ndev, &gid);
  559. memset(&gid_attr, 0, sizeof(gid_attr));
  560. gid_attr.ndev = ndev;
  561. for (gid_type = 0; gid_type < IB_GID_TYPE_SIZE; ++gid_type) {
  562. int ix;
  563. union ib_gid current_gid;
  564. struct ib_gid_attr current_gid_attr = {};
  565. if (1UL << gid_type & ~gid_type_mask)
  566. continue;
  567. gid_attr.gid_type = gid_type;
  568. mutex_lock(&table->lock);
  569. write_lock_irq(&table->rwlock);
  570. ix = find_gid(table, NULL, &gid_attr, true,
  571. GID_ATTR_FIND_MASK_GID_TYPE |
  572. GID_ATTR_FIND_MASK_DEFAULT,
  573. NULL);
  574. /* Coudn't find default GID location */
  575. WARN_ON(ix < 0);
  576. zattr_type.gid_type = gid_type;
  577. if (!__ib_cache_gid_get(ib_dev, port, ix,
  578. &current_gid, &current_gid_attr) &&
  579. mode == IB_CACHE_GID_DEFAULT_MODE_SET &&
  580. !memcmp(&gid, &current_gid, sizeof(gid)) &&
  581. !memcmp(&gid_attr, &current_gid_attr, sizeof(gid_attr)))
  582. goto release;
  583. if (memcmp(&current_gid, &zgid, sizeof(current_gid)) ||
  584. memcmp(&current_gid_attr, &zattr_type,
  585. sizeof(current_gid_attr))) {
  586. if (del_gid(ib_dev, port, table, ix, true)) {
  587. pr_warn("ib_cache_gid: can't delete index %d for default gid %pI6\n",
  588. ix, gid.raw);
  589. goto release;
  590. } else {
  591. dispatch_gid_change_event(ib_dev, port);
  592. }
  593. }
  594. if (mode == IB_CACHE_GID_DEFAULT_MODE_SET) {
  595. if (add_gid(ib_dev, port, table, ix, &gid, &gid_attr, true))
  596. pr_warn("ib_cache_gid: unable to add default gid %pI6\n",
  597. gid.raw);
  598. else
  599. dispatch_gid_change_event(ib_dev, port);
  600. }
  601. release:
  602. if (current_gid_attr.ndev)
  603. dev_put(current_gid_attr.ndev);
  604. write_unlock_irq(&table->rwlock);
  605. mutex_unlock(&table->lock);
  606. }
  607. }
  608. static int gid_table_reserve_default(struct ib_device *ib_dev, u8 port,
  609. struct ib_gid_table *table)
  610. {
  611. unsigned int i;
  612. unsigned long roce_gid_type_mask;
  613. unsigned int num_default_gids;
  614. unsigned int current_gid = 0;
  615. roce_gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
  616. num_default_gids = hweight_long(roce_gid_type_mask);
  617. for (i = 0; i < num_default_gids && i < table->sz; i++) {
  618. struct ib_gid_table_entry *entry =
  619. &table->data_vec[i];
  620. entry->props |= GID_TABLE_ENTRY_DEFAULT;
  621. current_gid = find_next_bit(&roce_gid_type_mask,
  622. BITS_PER_LONG,
  623. current_gid);
  624. entry->attr.gid_type = current_gid++;
  625. }
  626. return 0;
  627. }
  628. static int _gid_table_setup_one(struct ib_device *ib_dev)
  629. {
  630. u8 port;
  631. struct ib_gid_table **table;
  632. int err = 0;
  633. table = kcalloc(ib_dev->phys_port_cnt, sizeof(*table), GFP_KERNEL);
  634. if (!table) {
  635. pr_warn("failed to allocate ib gid cache for %s\n",
  636. ib_dev->name);
  637. return -ENOMEM;
  638. }
  639. for (port = 0; port < ib_dev->phys_port_cnt; port++) {
  640. u8 rdma_port = port + rdma_start_port(ib_dev);
  641. table[port] =
  642. alloc_gid_table(
  643. ib_dev->port_immutable[rdma_port].gid_tbl_len);
  644. if (!table[port]) {
  645. err = -ENOMEM;
  646. goto rollback_table_setup;
  647. }
  648. err = gid_table_reserve_default(ib_dev,
  649. port + rdma_start_port(ib_dev),
  650. table[port]);
  651. if (err)
  652. goto rollback_table_setup;
  653. }
  654. ib_dev->cache.gid_cache = table;
  655. return 0;
  656. rollback_table_setup:
  657. for (port = 0; port < ib_dev->phys_port_cnt; port++) {
  658. cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
  659. table[port]);
  660. release_gid_table(table[port]);
  661. }
  662. kfree(table);
  663. return err;
  664. }
  665. static void gid_table_release_one(struct ib_device *ib_dev)
  666. {
  667. struct ib_gid_table **table = ib_dev->cache.gid_cache;
  668. u8 port;
  669. if (!table)
  670. return;
  671. for (port = 0; port < ib_dev->phys_port_cnt; port++)
  672. release_gid_table(table[port]);
  673. kfree(table);
  674. ib_dev->cache.gid_cache = NULL;
  675. }
  676. static void gid_table_cleanup_one(struct ib_device *ib_dev)
  677. {
  678. struct ib_gid_table **table = ib_dev->cache.gid_cache;
  679. u8 port;
  680. if (!table)
  681. return;
  682. for (port = 0; port < ib_dev->phys_port_cnt; port++)
  683. cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
  684. table[port]);
  685. }
  686. static int gid_table_setup_one(struct ib_device *ib_dev)
  687. {
  688. int err;
  689. err = _gid_table_setup_one(ib_dev);
  690. if (err)
  691. return err;
  692. err = roce_rescan_device(ib_dev);
  693. if (err) {
  694. gid_table_cleanup_one(ib_dev);
  695. gid_table_release_one(ib_dev);
  696. }
  697. return err;
  698. }
  699. int ib_get_cached_gid(struct ib_device *device,
  700. u8 port_num,
  701. int index,
  702. union ib_gid *gid,
  703. struct ib_gid_attr *gid_attr)
  704. {
  705. int res;
  706. unsigned long flags;
  707. struct ib_gid_table **ports_table = device->cache.gid_cache;
  708. struct ib_gid_table *table = ports_table[port_num - rdma_start_port(device)];
  709. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  710. return -EINVAL;
  711. read_lock_irqsave(&table->rwlock, flags);
  712. res = __ib_cache_gid_get(device, port_num, index, gid, gid_attr);
  713. read_unlock_irqrestore(&table->rwlock, flags);
  714. return res;
  715. }
  716. EXPORT_SYMBOL(ib_get_cached_gid);
  717. int ib_find_cached_gid(struct ib_device *device,
  718. const union ib_gid *gid,
  719. enum ib_gid_type gid_type,
  720. struct net_device *ndev,
  721. u8 *port_num,
  722. u16 *index)
  723. {
  724. return ib_cache_gid_find(device, gid, gid_type, ndev, port_num, index);
  725. }
  726. EXPORT_SYMBOL(ib_find_cached_gid);
  727. int ib_find_gid_by_filter(struct ib_device *device,
  728. const union ib_gid *gid,
  729. u8 port_num,
  730. bool (*filter)(const union ib_gid *gid,
  731. const struct ib_gid_attr *,
  732. void *),
  733. void *context, u16 *index)
  734. {
  735. /* Only RoCE GID table supports filter function */
  736. if (!rdma_cap_roce_gid_table(device, port_num) && filter)
  737. return -EPROTONOSUPPORT;
  738. return ib_cache_gid_find_by_filter(device, gid,
  739. port_num, filter,
  740. context, index);
  741. }
  742. EXPORT_SYMBOL(ib_find_gid_by_filter);
  743. int ib_get_cached_pkey(struct ib_device *device,
  744. u8 port_num,
  745. int index,
  746. u16 *pkey)
  747. {
  748. struct ib_pkey_cache *cache;
  749. unsigned long flags;
  750. int ret = 0;
  751. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  752. return -EINVAL;
  753. read_lock_irqsave(&device->cache.lock, flags);
  754. cache = device->cache.pkey_cache[port_num - rdma_start_port(device)];
  755. if (index < 0 || index >= cache->table_len)
  756. ret = -EINVAL;
  757. else
  758. *pkey = cache->table[index];
  759. read_unlock_irqrestore(&device->cache.lock, flags);
  760. return ret;
  761. }
  762. EXPORT_SYMBOL(ib_get_cached_pkey);
  763. int ib_find_cached_pkey(struct ib_device *device,
  764. u8 port_num,
  765. u16 pkey,
  766. u16 *index)
  767. {
  768. struct ib_pkey_cache *cache;
  769. unsigned long flags;
  770. int i;
  771. int ret = -ENOENT;
  772. int partial_ix = -1;
  773. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  774. return -EINVAL;
  775. read_lock_irqsave(&device->cache.lock, flags);
  776. cache = device->cache.pkey_cache[port_num - rdma_start_port(device)];
  777. *index = -1;
  778. for (i = 0; i < cache->table_len; ++i)
  779. if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
  780. if (cache->table[i] & 0x8000) {
  781. *index = i;
  782. ret = 0;
  783. break;
  784. } else
  785. partial_ix = i;
  786. }
  787. if (ret && partial_ix >= 0) {
  788. *index = partial_ix;
  789. ret = 0;
  790. }
  791. read_unlock_irqrestore(&device->cache.lock, flags);
  792. return ret;
  793. }
  794. EXPORT_SYMBOL(ib_find_cached_pkey);
  795. int ib_find_exact_cached_pkey(struct ib_device *device,
  796. u8 port_num,
  797. u16 pkey,
  798. u16 *index)
  799. {
  800. struct ib_pkey_cache *cache;
  801. unsigned long flags;
  802. int i;
  803. int ret = -ENOENT;
  804. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  805. return -EINVAL;
  806. read_lock_irqsave(&device->cache.lock, flags);
  807. cache = device->cache.pkey_cache[port_num - rdma_start_port(device)];
  808. *index = -1;
  809. for (i = 0; i < cache->table_len; ++i)
  810. if (cache->table[i] == pkey) {
  811. *index = i;
  812. ret = 0;
  813. break;
  814. }
  815. read_unlock_irqrestore(&device->cache.lock, flags);
  816. return ret;
  817. }
  818. EXPORT_SYMBOL(ib_find_exact_cached_pkey);
  819. int ib_get_cached_lmc(struct ib_device *device,
  820. u8 port_num,
  821. u8 *lmc)
  822. {
  823. unsigned long flags;
  824. int ret = 0;
  825. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  826. return -EINVAL;
  827. read_lock_irqsave(&device->cache.lock, flags);
  828. *lmc = device->cache.lmc_cache[port_num - rdma_start_port(device)];
  829. read_unlock_irqrestore(&device->cache.lock, flags);
  830. return ret;
  831. }
  832. EXPORT_SYMBOL(ib_get_cached_lmc);
  833. static void ib_cache_update(struct ib_device *device,
  834. u8 port)
  835. {
  836. struct ib_port_attr *tprops = NULL;
  837. struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache;
  838. struct ib_gid_cache {
  839. int table_len;
  840. union ib_gid table[0];
  841. } *gid_cache = NULL;
  842. int i;
  843. int ret;
  844. struct ib_gid_table *table;
  845. struct ib_gid_table **ports_table = device->cache.gid_cache;
  846. bool use_roce_gid_table =
  847. rdma_cap_roce_gid_table(device, port);
  848. if (port < rdma_start_port(device) || port > rdma_end_port(device))
  849. return;
  850. table = ports_table[port - rdma_start_port(device)];
  851. tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
  852. if (!tprops)
  853. return;
  854. ret = ib_query_port(device, port, tprops);
  855. if (ret) {
  856. pr_warn("ib_query_port failed (%d) for %s\n",
  857. ret, device->name);
  858. goto err;
  859. }
  860. pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
  861. sizeof *pkey_cache->table, GFP_KERNEL);
  862. if (!pkey_cache)
  863. goto err;
  864. pkey_cache->table_len = tprops->pkey_tbl_len;
  865. if (!use_roce_gid_table) {
  866. gid_cache = kmalloc(sizeof(*gid_cache) + tprops->gid_tbl_len *
  867. sizeof(*gid_cache->table), GFP_KERNEL);
  868. if (!gid_cache)
  869. goto err;
  870. gid_cache->table_len = tprops->gid_tbl_len;
  871. }
  872. for (i = 0; i < pkey_cache->table_len; ++i) {
  873. ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
  874. if (ret) {
  875. pr_warn("ib_query_pkey failed (%d) for %s (index %d)\n",
  876. ret, device->name, i);
  877. goto err;
  878. }
  879. }
  880. if (!use_roce_gid_table) {
  881. for (i = 0; i < gid_cache->table_len; ++i) {
  882. ret = ib_query_gid(device, port, i,
  883. gid_cache->table + i, NULL);
  884. if (ret) {
  885. pr_warn("ib_query_gid failed (%d) for %s (index %d)\n",
  886. ret, device->name, i);
  887. goto err;
  888. }
  889. }
  890. }
  891. write_lock_irq(&device->cache.lock);
  892. old_pkey_cache = device->cache.pkey_cache[port - rdma_start_port(device)];
  893. device->cache.pkey_cache[port - rdma_start_port(device)] = pkey_cache;
  894. if (!use_roce_gid_table) {
  895. write_lock(&table->rwlock);
  896. for (i = 0; i < gid_cache->table_len; i++) {
  897. modify_gid(device, port, table, i, gid_cache->table + i,
  898. &zattr, false);
  899. }
  900. write_unlock(&table->rwlock);
  901. }
  902. device->cache.lmc_cache[port - rdma_start_port(device)] = tprops->lmc;
  903. write_unlock_irq(&device->cache.lock);
  904. kfree(gid_cache);
  905. kfree(old_pkey_cache);
  906. kfree(tprops);
  907. return;
  908. err:
  909. kfree(pkey_cache);
  910. kfree(gid_cache);
  911. kfree(tprops);
  912. }
  913. static void ib_cache_task(struct work_struct *_work)
  914. {
  915. struct ib_update_work *work =
  916. container_of(_work, struct ib_update_work, work);
  917. ib_cache_update(work->device, work->port_num);
  918. kfree(work);
  919. }
  920. static void ib_cache_event(struct ib_event_handler *handler,
  921. struct ib_event *event)
  922. {
  923. struct ib_update_work *work;
  924. if (event->event == IB_EVENT_PORT_ERR ||
  925. event->event == IB_EVENT_PORT_ACTIVE ||
  926. event->event == IB_EVENT_LID_CHANGE ||
  927. event->event == IB_EVENT_PKEY_CHANGE ||
  928. event->event == IB_EVENT_SM_CHANGE ||
  929. event->event == IB_EVENT_CLIENT_REREGISTER ||
  930. event->event == IB_EVENT_GID_CHANGE) {
  931. work = kmalloc(sizeof *work, GFP_ATOMIC);
  932. if (work) {
  933. INIT_WORK(&work->work, ib_cache_task);
  934. work->device = event->device;
  935. work->port_num = event->element.port_num;
  936. queue_work(ib_wq, &work->work);
  937. }
  938. }
  939. }
  940. int ib_cache_setup_one(struct ib_device *device)
  941. {
  942. int p;
  943. int err;
  944. rwlock_init(&device->cache.lock);
  945. device->cache.pkey_cache =
  946. kzalloc(sizeof *device->cache.pkey_cache *
  947. (rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL);
  948. device->cache.lmc_cache = kmalloc(sizeof *device->cache.lmc_cache *
  949. (rdma_end_port(device) -
  950. rdma_start_port(device) + 1),
  951. GFP_KERNEL);
  952. if (!device->cache.pkey_cache ||
  953. !device->cache.lmc_cache) {
  954. pr_warn("Couldn't allocate cache for %s\n", device->name);
  955. return -ENOMEM;
  956. }
  957. err = gid_table_setup_one(device);
  958. if (err)
  959. /* Allocated memory will be cleaned in the release function */
  960. return err;
  961. for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
  962. ib_cache_update(device, p + rdma_start_port(device));
  963. INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
  964. device, ib_cache_event);
  965. err = ib_register_event_handler(&device->cache.event_handler);
  966. if (err)
  967. goto err;
  968. return 0;
  969. err:
  970. gid_table_cleanup_one(device);
  971. return err;
  972. }
  973. void ib_cache_release_one(struct ib_device *device)
  974. {
  975. int p;
  976. /*
  977. * The release function frees all the cache elements.
  978. * This function should be called as part of freeing
  979. * all the device's resources when the cache could no
  980. * longer be accessed.
  981. */
  982. if (device->cache.pkey_cache)
  983. for (p = 0;
  984. p <= rdma_end_port(device) - rdma_start_port(device); ++p)
  985. kfree(device->cache.pkey_cache[p]);
  986. gid_table_release_one(device);
  987. kfree(device->cache.pkey_cache);
  988. kfree(device->cache.lmc_cache);
  989. }
  990. void ib_cache_cleanup_one(struct ib_device *device)
  991. {
  992. /* The cleanup function unregisters the event handler,
  993. * waits for all in-progress workqueue elements and cleans
  994. * up the GID cache. This function should be called after
  995. * the device was removed from the devices list and all
  996. * clients were removed, so the cache exists but is
  997. * non-functional and shouldn't be updated anymore.
  998. */
  999. ib_unregister_event_handler(&device->cache.event_handler);
  1000. flush_workqueue(ib_wq);
  1001. gid_table_cleanup_one(device);
  1002. }
  1003. void __init ib_cache_setup(void)
  1004. {
  1005. roce_gid_mgmt_init();
  1006. }
  1007. void __exit ib_cache_cleanup(void)
  1008. {
  1009. roce_gid_mgmt_cleanup();
  1010. }