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