multicast.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*
  2. * Copyright (c) 2006 Intel Corporation. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/completion.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/err.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/export.h>
  37. #include <linux/slab.h>
  38. #include <linux/bitops.h>
  39. #include <linux/random.h>
  40. #include <rdma/ib_cache.h>
  41. #include "sa.h"
  42. static void mcast_add_one(struct ib_device *device);
  43. static void mcast_remove_one(struct ib_device *device, void *client_data);
  44. static struct ib_client mcast_client = {
  45. .name = "ib_multicast",
  46. .add = mcast_add_one,
  47. .remove = mcast_remove_one
  48. };
  49. static struct ib_sa_client sa_client;
  50. static struct workqueue_struct *mcast_wq;
  51. static union ib_gid mgid0;
  52. struct mcast_device;
  53. struct mcast_port {
  54. struct mcast_device *dev;
  55. spinlock_t lock;
  56. struct rb_root table;
  57. atomic_t refcount;
  58. struct completion comp;
  59. u8 port_num;
  60. };
  61. struct mcast_device {
  62. struct ib_device *device;
  63. struct ib_event_handler event_handler;
  64. int start_port;
  65. int end_port;
  66. struct mcast_port port[0];
  67. };
  68. enum mcast_state {
  69. MCAST_JOINING,
  70. MCAST_MEMBER,
  71. MCAST_ERROR,
  72. };
  73. enum mcast_group_state {
  74. MCAST_IDLE,
  75. MCAST_BUSY,
  76. MCAST_GROUP_ERROR,
  77. MCAST_PKEY_EVENT
  78. };
  79. enum {
  80. MCAST_INVALID_PKEY_INDEX = 0xFFFF
  81. };
  82. struct mcast_member;
  83. struct mcast_group {
  84. struct ib_sa_mcmember_rec rec;
  85. struct rb_node node;
  86. struct mcast_port *port;
  87. spinlock_t lock;
  88. struct work_struct work;
  89. struct list_head pending_list;
  90. struct list_head active_list;
  91. struct mcast_member *last_join;
  92. int members[NUM_JOIN_MEMBERSHIP_TYPES];
  93. atomic_t refcount;
  94. enum mcast_group_state state;
  95. struct ib_sa_query *query;
  96. u16 pkey_index;
  97. u8 leave_state;
  98. int retries;
  99. };
  100. struct mcast_member {
  101. struct ib_sa_multicast multicast;
  102. struct ib_sa_client *client;
  103. struct mcast_group *group;
  104. struct list_head list;
  105. enum mcast_state state;
  106. atomic_t refcount;
  107. struct completion comp;
  108. };
  109. static void join_handler(int status, struct ib_sa_mcmember_rec *rec,
  110. void *context);
  111. static void leave_handler(int status, struct ib_sa_mcmember_rec *rec,
  112. void *context);
  113. static struct mcast_group *mcast_find(struct mcast_port *port,
  114. union ib_gid *mgid)
  115. {
  116. struct rb_node *node = port->table.rb_node;
  117. struct mcast_group *group;
  118. int ret;
  119. while (node) {
  120. group = rb_entry(node, struct mcast_group, node);
  121. ret = memcmp(mgid->raw, group->rec.mgid.raw, sizeof *mgid);
  122. if (!ret)
  123. return group;
  124. if (ret < 0)
  125. node = node->rb_left;
  126. else
  127. node = node->rb_right;
  128. }
  129. return NULL;
  130. }
  131. static struct mcast_group *mcast_insert(struct mcast_port *port,
  132. struct mcast_group *group,
  133. int allow_duplicates)
  134. {
  135. struct rb_node **link = &port->table.rb_node;
  136. struct rb_node *parent = NULL;
  137. struct mcast_group *cur_group;
  138. int ret;
  139. while (*link) {
  140. parent = *link;
  141. cur_group = rb_entry(parent, struct mcast_group, node);
  142. ret = memcmp(group->rec.mgid.raw, cur_group->rec.mgid.raw,
  143. sizeof group->rec.mgid);
  144. if (ret < 0)
  145. link = &(*link)->rb_left;
  146. else if (ret > 0)
  147. link = &(*link)->rb_right;
  148. else if (allow_duplicates)
  149. link = &(*link)->rb_left;
  150. else
  151. return cur_group;
  152. }
  153. rb_link_node(&group->node, parent, link);
  154. rb_insert_color(&group->node, &port->table);
  155. return NULL;
  156. }
  157. static void deref_port(struct mcast_port *port)
  158. {
  159. if (atomic_dec_and_test(&port->refcount))
  160. complete(&port->comp);
  161. }
  162. static void release_group(struct mcast_group *group)
  163. {
  164. struct mcast_port *port = group->port;
  165. unsigned long flags;
  166. spin_lock_irqsave(&port->lock, flags);
  167. if (atomic_dec_and_test(&group->refcount)) {
  168. rb_erase(&group->node, &port->table);
  169. spin_unlock_irqrestore(&port->lock, flags);
  170. kfree(group);
  171. deref_port(port);
  172. } else
  173. spin_unlock_irqrestore(&port->lock, flags);
  174. }
  175. static void deref_member(struct mcast_member *member)
  176. {
  177. if (atomic_dec_and_test(&member->refcount))
  178. complete(&member->comp);
  179. }
  180. static void queue_join(struct mcast_member *member)
  181. {
  182. struct mcast_group *group = member->group;
  183. unsigned long flags;
  184. spin_lock_irqsave(&group->lock, flags);
  185. list_add_tail(&member->list, &group->pending_list);
  186. if (group->state == MCAST_IDLE) {
  187. group->state = MCAST_BUSY;
  188. atomic_inc(&group->refcount);
  189. queue_work(mcast_wq, &group->work);
  190. }
  191. spin_unlock_irqrestore(&group->lock, flags);
  192. }
  193. /*
  194. * A multicast group has four types of members: full member, non member,
  195. * sendonly non member and sendonly full member.
  196. * We need to keep track of the number of members of each
  197. * type based on their join state. Adjust the number of members the belong to
  198. * the specified join states.
  199. */
  200. static void adjust_membership(struct mcast_group *group, u8 join_state, int inc)
  201. {
  202. int i;
  203. for (i = 0; i < NUM_JOIN_MEMBERSHIP_TYPES; i++, join_state >>= 1)
  204. if (join_state & 0x1)
  205. group->members[i] += inc;
  206. }
  207. /*
  208. * If a multicast group has zero members left for a particular join state, but
  209. * the group is still a member with the SA, we need to leave that join state.
  210. * Determine which join states we still belong to, but that do not have any
  211. * active members.
  212. */
  213. static u8 get_leave_state(struct mcast_group *group)
  214. {
  215. u8 leave_state = 0;
  216. int i;
  217. for (i = 0; i < NUM_JOIN_MEMBERSHIP_TYPES; i++)
  218. if (!group->members[i])
  219. leave_state |= (0x1 << i);
  220. return leave_state & group->rec.join_state;
  221. }
  222. static int check_selector(ib_sa_comp_mask comp_mask,
  223. ib_sa_comp_mask selector_mask,
  224. ib_sa_comp_mask value_mask,
  225. u8 selector, u8 src_value, u8 dst_value)
  226. {
  227. int err;
  228. if (!(comp_mask & selector_mask) || !(comp_mask & value_mask))
  229. return 0;
  230. switch (selector) {
  231. case IB_SA_GT:
  232. err = (src_value <= dst_value);
  233. break;
  234. case IB_SA_LT:
  235. err = (src_value >= dst_value);
  236. break;
  237. case IB_SA_EQ:
  238. err = (src_value != dst_value);
  239. break;
  240. default:
  241. err = 0;
  242. break;
  243. }
  244. return err;
  245. }
  246. static int cmp_rec(struct ib_sa_mcmember_rec *src,
  247. struct ib_sa_mcmember_rec *dst, ib_sa_comp_mask comp_mask)
  248. {
  249. /* MGID must already match */
  250. if (comp_mask & IB_SA_MCMEMBER_REC_PORT_GID &&
  251. memcmp(&src->port_gid, &dst->port_gid, sizeof src->port_gid))
  252. return -EINVAL;
  253. if (comp_mask & IB_SA_MCMEMBER_REC_QKEY && src->qkey != dst->qkey)
  254. return -EINVAL;
  255. if (comp_mask & IB_SA_MCMEMBER_REC_MLID && src->mlid != dst->mlid)
  256. return -EINVAL;
  257. if (check_selector(comp_mask, IB_SA_MCMEMBER_REC_MTU_SELECTOR,
  258. IB_SA_MCMEMBER_REC_MTU, dst->mtu_selector,
  259. src->mtu, dst->mtu))
  260. return -EINVAL;
  261. if (comp_mask & IB_SA_MCMEMBER_REC_TRAFFIC_CLASS &&
  262. src->traffic_class != dst->traffic_class)
  263. return -EINVAL;
  264. if (comp_mask & IB_SA_MCMEMBER_REC_PKEY && src->pkey != dst->pkey)
  265. return -EINVAL;
  266. if (check_selector(comp_mask, IB_SA_MCMEMBER_REC_RATE_SELECTOR,
  267. IB_SA_MCMEMBER_REC_RATE, dst->rate_selector,
  268. src->rate, dst->rate))
  269. return -EINVAL;
  270. if (check_selector(comp_mask,
  271. IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME_SELECTOR,
  272. IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME,
  273. dst->packet_life_time_selector,
  274. src->packet_life_time, dst->packet_life_time))
  275. return -EINVAL;
  276. if (comp_mask & IB_SA_MCMEMBER_REC_SL && src->sl != dst->sl)
  277. return -EINVAL;
  278. if (comp_mask & IB_SA_MCMEMBER_REC_FLOW_LABEL &&
  279. src->flow_label != dst->flow_label)
  280. return -EINVAL;
  281. if (comp_mask & IB_SA_MCMEMBER_REC_HOP_LIMIT &&
  282. src->hop_limit != dst->hop_limit)
  283. return -EINVAL;
  284. if (comp_mask & IB_SA_MCMEMBER_REC_SCOPE && src->scope != dst->scope)
  285. return -EINVAL;
  286. /* join_state checked separately, proxy_join ignored */
  287. return 0;
  288. }
  289. static int send_join(struct mcast_group *group, struct mcast_member *member)
  290. {
  291. struct mcast_port *port = group->port;
  292. int ret;
  293. group->last_join = member;
  294. ret = ib_sa_mcmember_rec_query(&sa_client, port->dev->device,
  295. port->port_num, IB_MGMT_METHOD_SET,
  296. &member->multicast.rec,
  297. member->multicast.comp_mask,
  298. 3000, GFP_KERNEL, join_handler, group,
  299. &group->query);
  300. return (ret > 0) ? 0 : ret;
  301. }
  302. static int send_leave(struct mcast_group *group, u8 leave_state)
  303. {
  304. struct mcast_port *port = group->port;
  305. struct ib_sa_mcmember_rec rec;
  306. int ret;
  307. rec = group->rec;
  308. rec.join_state = leave_state;
  309. group->leave_state = leave_state;
  310. ret = ib_sa_mcmember_rec_query(&sa_client, port->dev->device,
  311. port->port_num, IB_SA_METHOD_DELETE, &rec,
  312. IB_SA_MCMEMBER_REC_MGID |
  313. IB_SA_MCMEMBER_REC_PORT_GID |
  314. IB_SA_MCMEMBER_REC_JOIN_STATE,
  315. 3000, GFP_KERNEL, leave_handler,
  316. group, &group->query);
  317. return (ret > 0) ? 0 : ret;
  318. }
  319. static void join_group(struct mcast_group *group, struct mcast_member *member,
  320. u8 join_state)
  321. {
  322. member->state = MCAST_MEMBER;
  323. adjust_membership(group, join_state, 1);
  324. group->rec.join_state |= join_state;
  325. member->multicast.rec = group->rec;
  326. member->multicast.rec.join_state = join_state;
  327. list_move(&member->list, &group->active_list);
  328. }
  329. static int fail_join(struct mcast_group *group, struct mcast_member *member,
  330. int status)
  331. {
  332. spin_lock_irq(&group->lock);
  333. list_del_init(&member->list);
  334. spin_unlock_irq(&group->lock);
  335. return member->multicast.callback(status, &member->multicast);
  336. }
  337. static void process_group_error(struct mcast_group *group)
  338. {
  339. struct mcast_member *member;
  340. int ret = 0;
  341. u16 pkey_index;
  342. if (group->state == MCAST_PKEY_EVENT)
  343. ret = ib_find_pkey(group->port->dev->device,
  344. group->port->port_num,
  345. be16_to_cpu(group->rec.pkey), &pkey_index);
  346. spin_lock_irq(&group->lock);
  347. if (group->state == MCAST_PKEY_EVENT && !ret &&
  348. group->pkey_index == pkey_index)
  349. goto out;
  350. while (!list_empty(&group->active_list)) {
  351. member = list_entry(group->active_list.next,
  352. struct mcast_member, list);
  353. atomic_inc(&member->refcount);
  354. list_del_init(&member->list);
  355. adjust_membership(group, member->multicast.rec.join_state, -1);
  356. member->state = MCAST_ERROR;
  357. spin_unlock_irq(&group->lock);
  358. ret = member->multicast.callback(-ENETRESET,
  359. &member->multicast);
  360. deref_member(member);
  361. if (ret)
  362. ib_sa_free_multicast(&member->multicast);
  363. spin_lock_irq(&group->lock);
  364. }
  365. group->rec.join_state = 0;
  366. out:
  367. group->state = MCAST_BUSY;
  368. spin_unlock_irq(&group->lock);
  369. }
  370. static void mcast_work_handler(struct work_struct *work)
  371. {
  372. struct mcast_group *group;
  373. struct mcast_member *member;
  374. struct ib_sa_multicast *multicast;
  375. int status, ret;
  376. u8 join_state;
  377. group = container_of(work, typeof(*group), work);
  378. retest:
  379. spin_lock_irq(&group->lock);
  380. while (!list_empty(&group->pending_list) ||
  381. (group->state != MCAST_BUSY)) {
  382. if (group->state != MCAST_BUSY) {
  383. spin_unlock_irq(&group->lock);
  384. process_group_error(group);
  385. goto retest;
  386. }
  387. member = list_entry(group->pending_list.next,
  388. struct mcast_member, list);
  389. multicast = &member->multicast;
  390. join_state = multicast->rec.join_state;
  391. atomic_inc(&member->refcount);
  392. if (join_state == (group->rec.join_state & join_state)) {
  393. status = cmp_rec(&group->rec, &multicast->rec,
  394. multicast->comp_mask);
  395. if (!status)
  396. join_group(group, member, join_state);
  397. else
  398. list_del_init(&member->list);
  399. spin_unlock_irq(&group->lock);
  400. ret = multicast->callback(status, multicast);
  401. } else {
  402. spin_unlock_irq(&group->lock);
  403. status = send_join(group, member);
  404. if (!status) {
  405. deref_member(member);
  406. return;
  407. }
  408. ret = fail_join(group, member, status);
  409. }
  410. deref_member(member);
  411. if (ret)
  412. ib_sa_free_multicast(&member->multicast);
  413. spin_lock_irq(&group->lock);
  414. }
  415. join_state = get_leave_state(group);
  416. if (join_state) {
  417. group->rec.join_state &= ~join_state;
  418. spin_unlock_irq(&group->lock);
  419. if (send_leave(group, join_state))
  420. goto retest;
  421. } else {
  422. group->state = MCAST_IDLE;
  423. spin_unlock_irq(&group->lock);
  424. release_group(group);
  425. }
  426. }
  427. /*
  428. * Fail a join request if it is still active - at the head of the pending queue.
  429. */
  430. static void process_join_error(struct mcast_group *group, int status)
  431. {
  432. struct mcast_member *member;
  433. int ret;
  434. spin_lock_irq(&group->lock);
  435. member = list_entry(group->pending_list.next,
  436. struct mcast_member, list);
  437. if (group->last_join == member) {
  438. atomic_inc(&member->refcount);
  439. list_del_init(&member->list);
  440. spin_unlock_irq(&group->lock);
  441. ret = member->multicast.callback(status, &member->multicast);
  442. deref_member(member);
  443. if (ret)
  444. ib_sa_free_multicast(&member->multicast);
  445. } else
  446. spin_unlock_irq(&group->lock);
  447. }
  448. static void join_handler(int status, struct ib_sa_mcmember_rec *rec,
  449. void *context)
  450. {
  451. struct mcast_group *group = context;
  452. u16 pkey_index = MCAST_INVALID_PKEY_INDEX;
  453. if (status)
  454. process_join_error(group, status);
  455. else {
  456. int mgids_changed, is_mgid0;
  457. if (ib_find_pkey(group->port->dev->device,
  458. group->port->port_num, be16_to_cpu(rec->pkey),
  459. &pkey_index))
  460. pkey_index = MCAST_INVALID_PKEY_INDEX;
  461. spin_lock_irq(&group->port->lock);
  462. if (group->state == MCAST_BUSY &&
  463. group->pkey_index == MCAST_INVALID_PKEY_INDEX)
  464. group->pkey_index = pkey_index;
  465. mgids_changed = memcmp(&rec->mgid, &group->rec.mgid,
  466. sizeof(group->rec.mgid));
  467. group->rec = *rec;
  468. if (mgids_changed) {
  469. rb_erase(&group->node, &group->port->table);
  470. is_mgid0 = !memcmp(&mgid0, &group->rec.mgid,
  471. sizeof(mgid0));
  472. mcast_insert(group->port, group, is_mgid0);
  473. }
  474. spin_unlock_irq(&group->port->lock);
  475. }
  476. mcast_work_handler(&group->work);
  477. }
  478. static void leave_handler(int status, struct ib_sa_mcmember_rec *rec,
  479. void *context)
  480. {
  481. struct mcast_group *group = context;
  482. if (status && group->retries > 0 &&
  483. !send_leave(group, group->leave_state))
  484. group->retries--;
  485. else
  486. mcast_work_handler(&group->work);
  487. }
  488. static struct mcast_group *acquire_group(struct mcast_port *port,
  489. union ib_gid *mgid, gfp_t gfp_mask)
  490. {
  491. struct mcast_group *group, *cur_group;
  492. unsigned long flags;
  493. int is_mgid0;
  494. is_mgid0 = !memcmp(&mgid0, mgid, sizeof mgid0);
  495. if (!is_mgid0) {
  496. spin_lock_irqsave(&port->lock, flags);
  497. group = mcast_find(port, mgid);
  498. if (group)
  499. goto found;
  500. spin_unlock_irqrestore(&port->lock, flags);
  501. }
  502. group = kzalloc(sizeof *group, gfp_mask);
  503. if (!group)
  504. return NULL;
  505. group->retries = 3;
  506. group->port = port;
  507. group->rec.mgid = *mgid;
  508. group->pkey_index = MCAST_INVALID_PKEY_INDEX;
  509. INIT_LIST_HEAD(&group->pending_list);
  510. INIT_LIST_HEAD(&group->active_list);
  511. INIT_WORK(&group->work, mcast_work_handler);
  512. spin_lock_init(&group->lock);
  513. spin_lock_irqsave(&port->lock, flags);
  514. cur_group = mcast_insert(port, group, is_mgid0);
  515. if (cur_group) {
  516. kfree(group);
  517. group = cur_group;
  518. } else
  519. atomic_inc(&port->refcount);
  520. found:
  521. atomic_inc(&group->refcount);
  522. spin_unlock_irqrestore(&port->lock, flags);
  523. return group;
  524. }
  525. /*
  526. * We serialize all join requests to a single group to make our lives much
  527. * easier. Otherwise, two users could try to join the same group
  528. * simultaneously, with different configurations, one could leave while the
  529. * join is in progress, etc., which makes locking around error recovery
  530. * difficult.
  531. */
  532. struct ib_sa_multicast *
  533. ib_sa_join_multicast(struct ib_sa_client *client,
  534. struct ib_device *device, u8 port_num,
  535. struct ib_sa_mcmember_rec *rec,
  536. ib_sa_comp_mask comp_mask, gfp_t gfp_mask,
  537. int (*callback)(int status,
  538. struct ib_sa_multicast *multicast),
  539. void *context)
  540. {
  541. struct mcast_device *dev;
  542. struct mcast_member *member;
  543. struct ib_sa_multicast *multicast;
  544. int ret;
  545. dev = ib_get_client_data(device, &mcast_client);
  546. if (!dev)
  547. return ERR_PTR(-ENODEV);
  548. member = kmalloc(sizeof *member, gfp_mask);
  549. if (!member)
  550. return ERR_PTR(-ENOMEM);
  551. ib_sa_client_get(client);
  552. member->client = client;
  553. member->multicast.rec = *rec;
  554. member->multicast.comp_mask = comp_mask;
  555. member->multicast.callback = callback;
  556. member->multicast.context = context;
  557. init_completion(&member->comp);
  558. atomic_set(&member->refcount, 1);
  559. member->state = MCAST_JOINING;
  560. member->group = acquire_group(&dev->port[port_num - dev->start_port],
  561. &rec->mgid, gfp_mask);
  562. if (!member->group) {
  563. ret = -ENOMEM;
  564. goto err;
  565. }
  566. /*
  567. * The user will get the multicast structure in their callback. They
  568. * could then free the multicast structure before we can return from
  569. * this routine. So we save the pointer to return before queuing
  570. * any callback.
  571. */
  572. multicast = &member->multicast;
  573. queue_join(member);
  574. return multicast;
  575. err:
  576. ib_sa_client_put(client);
  577. kfree(member);
  578. return ERR_PTR(ret);
  579. }
  580. EXPORT_SYMBOL(ib_sa_join_multicast);
  581. void ib_sa_free_multicast(struct ib_sa_multicast *multicast)
  582. {
  583. struct mcast_member *member;
  584. struct mcast_group *group;
  585. member = container_of(multicast, struct mcast_member, multicast);
  586. group = member->group;
  587. spin_lock_irq(&group->lock);
  588. if (member->state == MCAST_MEMBER)
  589. adjust_membership(group, multicast->rec.join_state, -1);
  590. list_del_init(&member->list);
  591. if (group->state == MCAST_IDLE) {
  592. group->state = MCAST_BUSY;
  593. spin_unlock_irq(&group->lock);
  594. /* Continue to hold reference on group until callback */
  595. queue_work(mcast_wq, &group->work);
  596. } else {
  597. spin_unlock_irq(&group->lock);
  598. release_group(group);
  599. }
  600. deref_member(member);
  601. wait_for_completion(&member->comp);
  602. ib_sa_client_put(member->client);
  603. kfree(member);
  604. }
  605. EXPORT_SYMBOL(ib_sa_free_multicast);
  606. int ib_sa_get_mcmember_rec(struct ib_device *device, u8 port_num,
  607. union ib_gid *mgid, struct ib_sa_mcmember_rec *rec)
  608. {
  609. struct mcast_device *dev;
  610. struct mcast_port *port;
  611. struct mcast_group *group;
  612. unsigned long flags;
  613. int ret = 0;
  614. dev = ib_get_client_data(device, &mcast_client);
  615. if (!dev)
  616. return -ENODEV;
  617. port = &dev->port[port_num - dev->start_port];
  618. spin_lock_irqsave(&port->lock, flags);
  619. group = mcast_find(port, mgid);
  620. if (group)
  621. *rec = group->rec;
  622. else
  623. ret = -EADDRNOTAVAIL;
  624. spin_unlock_irqrestore(&port->lock, flags);
  625. return ret;
  626. }
  627. EXPORT_SYMBOL(ib_sa_get_mcmember_rec);
  628. /**
  629. * ib_init_ah_from_mcmember - Initialize AH attribute from multicast
  630. * member record and gid of the device.
  631. * @device: RDMA device
  632. * @port_num: Port of the rdma device to consider
  633. * @ndev: Optional netdevice, applicable only for RoCE
  634. * @gid_type: GID type to consider
  635. * @ah_attr: AH attribute to fillup on successful completion
  636. *
  637. * ib_init_ah_from_mcmember() initializes AH attribute based on multicast
  638. * member record and other device properties. On success the caller is
  639. * responsible to call rdma_destroy_ah_attr on the ah_attr. Returns 0 on
  640. * success or appropriate error code.
  641. *
  642. */
  643. int ib_init_ah_from_mcmember(struct ib_device *device, u8 port_num,
  644. struct ib_sa_mcmember_rec *rec,
  645. struct net_device *ndev,
  646. enum ib_gid_type gid_type,
  647. struct rdma_ah_attr *ah_attr)
  648. {
  649. const struct ib_gid_attr *sgid_attr;
  650. /* GID table is not based on the netdevice for IB link layer,
  651. * so ignore ndev during search.
  652. */
  653. if (rdma_protocol_ib(device, port_num))
  654. ndev = NULL;
  655. else if (!rdma_protocol_roce(device, port_num))
  656. return -EINVAL;
  657. sgid_attr = rdma_find_gid_by_port(device, &rec->port_gid,
  658. gid_type, port_num, ndev);
  659. if (IS_ERR(sgid_attr))
  660. return PTR_ERR(sgid_attr);
  661. memset(ah_attr, 0, sizeof(*ah_attr));
  662. ah_attr->type = rdma_ah_find_type(device, port_num);
  663. rdma_ah_set_dlid(ah_attr, be16_to_cpu(rec->mlid));
  664. rdma_ah_set_sl(ah_attr, rec->sl);
  665. rdma_ah_set_port_num(ah_attr, port_num);
  666. rdma_ah_set_static_rate(ah_attr, rec->rate);
  667. rdma_move_grh_sgid_attr(ah_attr, &rec->mgid,
  668. be32_to_cpu(rec->flow_label),
  669. rec->hop_limit, rec->traffic_class,
  670. sgid_attr);
  671. return 0;
  672. }
  673. EXPORT_SYMBOL(ib_init_ah_from_mcmember);
  674. static void mcast_groups_event(struct mcast_port *port,
  675. enum mcast_group_state state)
  676. {
  677. struct mcast_group *group;
  678. struct rb_node *node;
  679. unsigned long flags;
  680. spin_lock_irqsave(&port->lock, flags);
  681. for (node = rb_first(&port->table); node; node = rb_next(node)) {
  682. group = rb_entry(node, struct mcast_group, node);
  683. spin_lock(&group->lock);
  684. if (group->state == MCAST_IDLE) {
  685. atomic_inc(&group->refcount);
  686. queue_work(mcast_wq, &group->work);
  687. }
  688. if (group->state != MCAST_GROUP_ERROR)
  689. group->state = state;
  690. spin_unlock(&group->lock);
  691. }
  692. spin_unlock_irqrestore(&port->lock, flags);
  693. }
  694. static void mcast_event_handler(struct ib_event_handler *handler,
  695. struct ib_event *event)
  696. {
  697. struct mcast_device *dev;
  698. int index;
  699. dev = container_of(handler, struct mcast_device, event_handler);
  700. if (!rdma_cap_ib_mcast(dev->device, event->element.port_num))
  701. return;
  702. index = event->element.port_num - dev->start_port;
  703. switch (event->event) {
  704. case IB_EVENT_PORT_ERR:
  705. case IB_EVENT_LID_CHANGE:
  706. case IB_EVENT_SM_CHANGE:
  707. case IB_EVENT_CLIENT_REREGISTER:
  708. mcast_groups_event(&dev->port[index], MCAST_GROUP_ERROR);
  709. break;
  710. case IB_EVENT_PKEY_CHANGE:
  711. mcast_groups_event(&dev->port[index], MCAST_PKEY_EVENT);
  712. break;
  713. default:
  714. break;
  715. }
  716. }
  717. static void mcast_add_one(struct ib_device *device)
  718. {
  719. struct mcast_device *dev;
  720. struct mcast_port *port;
  721. int i;
  722. int count = 0;
  723. dev = kmalloc(struct_size(dev, port, device->phys_port_cnt),
  724. GFP_KERNEL);
  725. if (!dev)
  726. return;
  727. dev->start_port = rdma_start_port(device);
  728. dev->end_port = rdma_end_port(device);
  729. for (i = 0; i <= dev->end_port - dev->start_port; i++) {
  730. if (!rdma_cap_ib_mcast(device, dev->start_port + i))
  731. continue;
  732. port = &dev->port[i];
  733. port->dev = dev;
  734. port->port_num = dev->start_port + i;
  735. spin_lock_init(&port->lock);
  736. port->table = RB_ROOT;
  737. init_completion(&port->comp);
  738. atomic_set(&port->refcount, 1);
  739. ++count;
  740. }
  741. if (!count) {
  742. kfree(dev);
  743. return;
  744. }
  745. dev->device = device;
  746. ib_set_client_data(device, &mcast_client, dev);
  747. INIT_IB_EVENT_HANDLER(&dev->event_handler, device, mcast_event_handler);
  748. ib_register_event_handler(&dev->event_handler);
  749. }
  750. static void mcast_remove_one(struct ib_device *device, void *client_data)
  751. {
  752. struct mcast_device *dev = client_data;
  753. struct mcast_port *port;
  754. int i;
  755. if (!dev)
  756. return;
  757. ib_unregister_event_handler(&dev->event_handler);
  758. flush_workqueue(mcast_wq);
  759. for (i = 0; i <= dev->end_port - dev->start_port; i++) {
  760. if (rdma_cap_ib_mcast(device, dev->start_port + i)) {
  761. port = &dev->port[i];
  762. deref_port(port);
  763. wait_for_completion(&port->comp);
  764. }
  765. }
  766. kfree(dev);
  767. }
  768. int mcast_init(void)
  769. {
  770. int ret;
  771. mcast_wq = alloc_ordered_workqueue("ib_mcast", WQ_MEM_RECLAIM);
  772. if (!mcast_wq)
  773. return -ENOMEM;
  774. ib_sa_register_client(&sa_client);
  775. ret = ib_register_client(&mcast_client);
  776. if (ret)
  777. goto err;
  778. return 0;
  779. err:
  780. ib_sa_unregister_client(&sa_client);
  781. destroy_workqueue(mcast_wq);
  782. return ret;
  783. }
  784. void mcast_cleanup(void)
  785. {
  786. ib_unregister_client(&mcast_client);
  787. ib_sa_unregister_client(&sa_client);
  788. destroy_workqueue(mcast_wq);
  789. }