cache.c 32 KB

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