cache.c 35 KB

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