cache.c 37 KB

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