cache.c 31 KB

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