cache.c 30 KB

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