multicast.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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/bitops.h>
  37. #include <linux/random.h>
  38. #include <rdma/ib_cache.h>
  39. #include "sa.h"
  40. static void mcast_add_one(struct ib_device *device);
  41. static void mcast_remove_one(struct ib_device *device);
  42. static struct ib_client mcast_client = {
  43. .name = "ib_multicast",
  44. .add = mcast_add_one,
  45. .remove = mcast_remove_one
  46. };
  47. static struct ib_sa_client sa_client;
  48. static struct workqueue_struct *mcast_wq;
  49. static union ib_gid mgid0;
  50. struct mcast_device;
  51. struct mcast_port {
  52. struct mcast_device *dev;
  53. spinlock_t lock;
  54. struct rb_root table;
  55. atomic_t refcount;
  56. struct completion comp;
  57. u8 port_num;
  58. };
  59. struct mcast_device {
  60. struct ib_device *device;
  61. struct ib_event_handler event_handler;
  62. int start_port;
  63. int end_port;
  64. struct mcast_port port[0];
  65. };
  66. enum mcast_state {
  67. MCAST_IDLE,
  68. MCAST_JOINING,
  69. MCAST_MEMBER,
  70. MCAST_BUSY,
  71. MCAST_ERROR
  72. };
  73. struct mcast_member;
  74. struct mcast_group {
  75. struct ib_sa_mcmember_rec rec;
  76. struct rb_node node;
  77. struct mcast_port *port;
  78. spinlock_t lock;
  79. struct work_struct work;
  80. struct list_head pending_list;
  81. struct list_head active_list;
  82. struct mcast_member *last_join;
  83. int members[3];
  84. atomic_t refcount;
  85. enum mcast_state state;
  86. struct ib_sa_query *query;
  87. int query_id;
  88. };
  89. struct mcast_member {
  90. struct ib_sa_multicast multicast;
  91. struct ib_sa_client *client;
  92. struct mcast_group *group;
  93. struct list_head list;
  94. enum mcast_state state;
  95. atomic_t refcount;
  96. struct completion comp;
  97. };
  98. static void join_handler(int status, struct ib_sa_mcmember_rec *rec,
  99. void *context);
  100. static void leave_handler(int status, struct ib_sa_mcmember_rec *rec,
  101. void *context);
  102. static struct mcast_group *mcast_find(struct mcast_port *port,
  103. union ib_gid *mgid)
  104. {
  105. struct rb_node *node = port->table.rb_node;
  106. struct mcast_group *group;
  107. int ret;
  108. while (node) {
  109. group = rb_entry(node, struct mcast_group, node);
  110. ret = memcmp(mgid->raw, group->rec.mgid.raw, sizeof *mgid);
  111. if (!ret)
  112. return group;
  113. if (ret < 0)
  114. node = node->rb_left;
  115. else
  116. node = node->rb_right;
  117. }
  118. return NULL;
  119. }
  120. static struct mcast_group *mcast_insert(struct mcast_port *port,
  121. struct mcast_group *group,
  122. int allow_duplicates)
  123. {
  124. struct rb_node **link = &port->table.rb_node;
  125. struct rb_node *parent = NULL;
  126. struct mcast_group *cur_group;
  127. int ret;
  128. while (*link) {
  129. parent = *link;
  130. cur_group = rb_entry(parent, struct mcast_group, node);
  131. ret = memcmp(group->rec.mgid.raw, cur_group->rec.mgid.raw,
  132. sizeof group->rec.mgid);
  133. if (ret < 0)
  134. link = &(*link)->rb_left;
  135. else if (ret > 0)
  136. link = &(*link)->rb_right;
  137. else if (allow_duplicates)
  138. link = &(*link)->rb_left;
  139. else
  140. return cur_group;
  141. }
  142. rb_link_node(&group->node, parent, link);
  143. rb_insert_color(&group->node, &port->table);
  144. return NULL;
  145. }
  146. static void deref_port(struct mcast_port *port)
  147. {
  148. if (atomic_dec_and_test(&port->refcount))
  149. complete(&port->comp);
  150. }
  151. static void release_group(struct mcast_group *group)
  152. {
  153. struct mcast_port *port = group->port;
  154. unsigned long flags;
  155. spin_lock_irqsave(&port->lock, flags);
  156. if (atomic_dec_and_test(&group->refcount)) {
  157. rb_erase(&group->node, &port->table);
  158. spin_unlock_irqrestore(&port->lock, flags);
  159. kfree(group);
  160. deref_port(port);
  161. } else
  162. spin_unlock_irqrestore(&port->lock, flags);
  163. }
  164. static void deref_member(struct mcast_member *member)
  165. {
  166. if (atomic_dec_and_test(&member->refcount))
  167. complete(&member->comp);
  168. }
  169. static void queue_join(struct mcast_member *member)
  170. {
  171. struct mcast_group *group = member->group;
  172. unsigned long flags;
  173. spin_lock_irqsave(&group->lock, flags);
  174. list_add(&member->list, &group->pending_list);
  175. if (group->state == MCAST_IDLE) {
  176. group->state = MCAST_BUSY;
  177. atomic_inc(&group->refcount);
  178. queue_work(mcast_wq, &group->work);
  179. }
  180. spin_unlock_irqrestore(&group->lock, flags);
  181. }
  182. /*
  183. * A multicast group has three types of members: full member, non member, and
  184. * send only member. We need to keep track of the number of members of each
  185. * type based on their join state. Adjust the number of members the belong to
  186. * the specified join states.
  187. */
  188. static void adjust_membership(struct mcast_group *group, u8 join_state, int inc)
  189. {
  190. int i;
  191. for (i = 0; i < 3; i++, join_state >>= 1)
  192. if (join_state & 0x1)
  193. group->members[i] += inc;
  194. }
  195. /*
  196. * If a multicast group has zero members left for a particular join state, but
  197. * the group is still a member with the SA, we need to leave that join state.
  198. * Determine which join states we still belong to, but that do not have any
  199. * active members.
  200. */
  201. static u8 get_leave_state(struct mcast_group *group)
  202. {
  203. u8 leave_state = 0;
  204. int i;
  205. for (i = 0; i < 3; i++)
  206. if (!group->members[i])
  207. leave_state |= (0x1 << i);
  208. return leave_state & group->rec.join_state;
  209. }
  210. static int check_selector(ib_sa_comp_mask comp_mask,
  211. ib_sa_comp_mask selector_mask,
  212. ib_sa_comp_mask value_mask,
  213. u8 selector, u8 src_value, u8 dst_value)
  214. {
  215. int err;
  216. if (!(comp_mask & selector_mask) || !(comp_mask & value_mask))
  217. return 0;
  218. switch (selector) {
  219. case IB_SA_GT:
  220. err = (src_value <= dst_value);
  221. break;
  222. case IB_SA_LT:
  223. err = (src_value >= dst_value);
  224. break;
  225. case IB_SA_EQ:
  226. err = (src_value != dst_value);
  227. break;
  228. default:
  229. err = 0;
  230. break;
  231. }
  232. return err;
  233. }
  234. static int cmp_rec(struct ib_sa_mcmember_rec *src,
  235. struct ib_sa_mcmember_rec *dst, ib_sa_comp_mask comp_mask)
  236. {
  237. /* MGID must already match */
  238. if (comp_mask & IB_SA_MCMEMBER_REC_PORT_GID &&
  239. memcmp(&src->port_gid, &dst->port_gid, sizeof src->port_gid))
  240. return -EINVAL;
  241. if (comp_mask & IB_SA_MCMEMBER_REC_QKEY && src->qkey != dst->qkey)
  242. return -EINVAL;
  243. if (comp_mask & IB_SA_MCMEMBER_REC_MLID && src->mlid != dst->mlid)
  244. return -EINVAL;
  245. if (check_selector(comp_mask, IB_SA_MCMEMBER_REC_MTU_SELECTOR,
  246. IB_SA_MCMEMBER_REC_MTU, dst->mtu_selector,
  247. src->mtu, dst->mtu))
  248. return -EINVAL;
  249. if (comp_mask & IB_SA_MCMEMBER_REC_TRAFFIC_CLASS &&
  250. src->traffic_class != dst->traffic_class)
  251. return -EINVAL;
  252. if (comp_mask & IB_SA_MCMEMBER_REC_PKEY && src->pkey != dst->pkey)
  253. return -EINVAL;
  254. if (check_selector(comp_mask, IB_SA_MCMEMBER_REC_RATE_SELECTOR,
  255. IB_SA_MCMEMBER_REC_RATE, dst->rate_selector,
  256. src->rate, dst->rate))
  257. return -EINVAL;
  258. if (check_selector(comp_mask,
  259. IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME_SELECTOR,
  260. IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME,
  261. dst->packet_life_time_selector,
  262. src->packet_life_time, dst->packet_life_time))
  263. return -EINVAL;
  264. if (comp_mask & IB_SA_MCMEMBER_REC_SL && src->sl != dst->sl)
  265. return -EINVAL;
  266. if (comp_mask & IB_SA_MCMEMBER_REC_FLOW_LABEL &&
  267. src->flow_label != dst->flow_label)
  268. return -EINVAL;
  269. if (comp_mask & IB_SA_MCMEMBER_REC_HOP_LIMIT &&
  270. src->hop_limit != dst->hop_limit)
  271. return -EINVAL;
  272. if (comp_mask & IB_SA_MCMEMBER_REC_SCOPE && src->scope != dst->scope)
  273. return -EINVAL;
  274. /* join_state checked separately, proxy_join ignored */
  275. return 0;
  276. }
  277. static int send_join(struct mcast_group *group, struct mcast_member *member)
  278. {
  279. struct mcast_port *port = group->port;
  280. int ret;
  281. group->last_join = member;
  282. ret = ib_sa_mcmember_rec_query(&sa_client, port->dev->device,
  283. port->port_num, IB_MGMT_METHOD_SET,
  284. &member->multicast.rec,
  285. member->multicast.comp_mask,
  286. 3000, GFP_KERNEL, join_handler, group,
  287. &group->query);
  288. if (ret >= 0) {
  289. group->query_id = ret;
  290. ret = 0;
  291. }
  292. return ret;
  293. }
  294. static int send_leave(struct mcast_group *group, u8 leave_state)
  295. {
  296. struct mcast_port *port = group->port;
  297. struct ib_sa_mcmember_rec rec;
  298. int ret;
  299. rec = group->rec;
  300. rec.join_state = leave_state;
  301. ret = ib_sa_mcmember_rec_query(&sa_client, port->dev->device,
  302. port->port_num, IB_SA_METHOD_DELETE, &rec,
  303. IB_SA_MCMEMBER_REC_MGID |
  304. IB_SA_MCMEMBER_REC_PORT_GID |
  305. IB_SA_MCMEMBER_REC_JOIN_STATE,
  306. 3000, GFP_KERNEL, leave_handler,
  307. group, &group->query);
  308. if (ret >= 0) {
  309. group->query_id = ret;
  310. ret = 0;
  311. }
  312. return ret;
  313. }
  314. static void join_group(struct mcast_group *group, struct mcast_member *member,
  315. u8 join_state)
  316. {
  317. member->state = MCAST_MEMBER;
  318. adjust_membership(group, join_state, 1);
  319. group->rec.join_state |= join_state;
  320. member->multicast.rec = group->rec;
  321. member->multicast.rec.join_state = join_state;
  322. list_move(&member->list, &group->active_list);
  323. }
  324. static int fail_join(struct mcast_group *group, struct mcast_member *member,
  325. int status)
  326. {
  327. spin_lock_irq(&group->lock);
  328. list_del_init(&member->list);
  329. spin_unlock_irq(&group->lock);
  330. return member->multicast.callback(status, &member->multicast);
  331. }
  332. static void process_group_error(struct mcast_group *group)
  333. {
  334. struct mcast_member *member;
  335. int ret;
  336. spin_lock_irq(&group->lock);
  337. while (!list_empty(&group->active_list)) {
  338. member = list_entry(group->active_list.next,
  339. struct mcast_member, list);
  340. atomic_inc(&member->refcount);
  341. list_del_init(&member->list);
  342. adjust_membership(group, member->multicast.rec.join_state, -1);
  343. member->state = MCAST_ERROR;
  344. spin_unlock_irq(&group->lock);
  345. ret = member->multicast.callback(-ENETRESET,
  346. &member->multicast);
  347. deref_member(member);
  348. if (ret)
  349. ib_sa_free_multicast(&member->multicast);
  350. spin_lock_irq(&group->lock);
  351. }
  352. group->rec.join_state = 0;
  353. group->state = MCAST_BUSY;
  354. spin_unlock_irq(&group->lock);
  355. }
  356. static void mcast_work_handler(struct work_struct *work)
  357. {
  358. struct mcast_group *group;
  359. struct mcast_member *member;
  360. struct ib_sa_multicast *multicast;
  361. int status, ret;
  362. u8 join_state;
  363. group = container_of(work, typeof(*group), work);
  364. retest:
  365. spin_lock_irq(&group->lock);
  366. while (!list_empty(&group->pending_list) ||
  367. (group->state == MCAST_ERROR)) {
  368. if (group->state == MCAST_ERROR) {
  369. spin_unlock_irq(&group->lock);
  370. process_group_error(group);
  371. goto retest;
  372. }
  373. member = list_entry(group->pending_list.next,
  374. struct mcast_member, list);
  375. multicast = &member->multicast;
  376. join_state = multicast->rec.join_state;
  377. atomic_inc(&member->refcount);
  378. if (join_state == (group->rec.join_state & join_state)) {
  379. status = cmp_rec(&group->rec, &multicast->rec,
  380. multicast->comp_mask);
  381. if (!status)
  382. join_group(group, member, join_state);
  383. else
  384. list_del_init(&member->list);
  385. spin_unlock_irq(&group->lock);
  386. ret = multicast->callback(status, multicast);
  387. } else {
  388. spin_unlock_irq(&group->lock);
  389. status = send_join(group, member);
  390. if (!status) {
  391. deref_member(member);
  392. return;
  393. }
  394. ret = fail_join(group, member, status);
  395. }
  396. deref_member(member);
  397. if (ret)
  398. ib_sa_free_multicast(&member->multicast);
  399. spin_lock_irq(&group->lock);
  400. }
  401. join_state = get_leave_state(group);
  402. if (join_state) {
  403. group->rec.join_state &= ~join_state;
  404. spin_unlock_irq(&group->lock);
  405. if (send_leave(group, join_state))
  406. goto retest;
  407. } else {
  408. group->state = MCAST_IDLE;
  409. spin_unlock_irq(&group->lock);
  410. release_group(group);
  411. }
  412. }
  413. /*
  414. * Fail a join request if it is still active - at the head of the pending queue.
  415. */
  416. static void process_join_error(struct mcast_group *group, int status)
  417. {
  418. struct mcast_member *member;
  419. int ret;
  420. spin_lock_irq(&group->lock);
  421. member = list_entry(group->pending_list.next,
  422. struct mcast_member, list);
  423. if (group->last_join == member) {
  424. atomic_inc(&member->refcount);
  425. list_del_init(&member->list);
  426. spin_unlock_irq(&group->lock);
  427. ret = member->multicast.callback(status, &member->multicast);
  428. deref_member(member);
  429. if (ret)
  430. ib_sa_free_multicast(&member->multicast);
  431. } else
  432. spin_unlock_irq(&group->lock);
  433. }
  434. static void join_handler(int status, struct ib_sa_mcmember_rec *rec,
  435. void *context)
  436. {
  437. struct mcast_group *group = context;
  438. if (status)
  439. process_join_error(group, status);
  440. else {
  441. spin_lock_irq(&group->port->lock);
  442. group->rec = *rec;
  443. if (!memcmp(&mgid0, &group->rec.mgid, sizeof mgid0)) {
  444. rb_erase(&group->node, &group->port->table);
  445. mcast_insert(group->port, group, 1);
  446. }
  447. spin_unlock_irq(&group->port->lock);
  448. }
  449. mcast_work_handler(&group->work);
  450. }
  451. static void leave_handler(int status, struct ib_sa_mcmember_rec *rec,
  452. void *context)
  453. {
  454. struct mcast_group *group = context;
  455. mcast_work_handler(&group->work);
  456. }
  457. static struct mcast_group *acquire_group(struct mcast_port *port,
  458. union ib_gid *mgid, gfp_t gfp_mask)
  459. {
  460. struct mcast_group *group, *cur_group;
  461. unsigned long flags;
  462. int is_mgid0;
  463. is_mgid0 = !memcmp(&mgid0, mgid, sizeof mgid0);
  464. if (!is_mgid0) {
  465. spin_lock_irqsave(&port->lock, flags);
  466. group = mcast_find(port, mgid);
  467. if (group)
  468. goto found;
  469. spin_unlock_irqrestore(&port->lock, flags);
  470. }
  471. group = kzalloc(sizeof *group, gfp_mask);
  472. if (!group)
  473. return NULL;
  474. group->port = port;
  475. group->rec.mgid = *mgid;
  476. INIT_LIST_HEAD(&group->pending_list);
  477. INIT_LIST_HEAD(&group->active_list);
  478. INIT_WORK(&group->work, mcast_work_handler);
  479. spin_lock_init(&group->lock);
  480. spin_lock_irqsave(&port->lock, flags);
  481. cur_group = mcast_insert(port, group, is_mgid0);
  482. if (cur_group) {
  483. kfree(group);
  484. group = cur_group;
  485. } else
  486. atomic_inc(&port->refcount);
  487. found:
  488. atomic_inc(&group->refcount);
  489. spin_unlock_irqrestore(&port->lock, flags);
  490. return group;
  491. }
  492. /*
  493. * We serialize all join requests to a single group to make our lives much
  494. * easier. Otherwise, two users could try to join the same group
  495. * simultaneously, with different configurations, one could leave while the
  496. * join is in progress, etc., which makes locking around error recovery
  497. * difficult.
  498. */
  499. struct ib_sa_multicast *
  500. ib_sa_join_multicast(struct ib_sa_client *client,
  501. struct ib_device *device, u8 port_num,
  502. struct ib_sa_mcmember_rec *rec,
  503. ib_sa_comp_mask comp_mask, gfp_t gfp_mask,
  504. int (*callback)(int status,
  505. struct ib_sa_multicast *multicast),
  506. void *context)
  507. {
  508. struct mcast_device *dev;
  509. struct mcast_member *member;
  510. struct ib_sa_multicast *multicast;
  511. int ret;
  512. dev = ib_get_client_data(device, &mcast_client);
  513. if (!dev)
  514. return ERR_PTR(-ENODEV);
  515. member = kmalloc(sizeof *member, gfp_mask);
  516. if (!member)
  517. return ERR_PTR(-ENOMEM);
  518. ib_sa_client_get(client);
  519. member->client = client;
  520. member->multicast.rec = *rec;
  521. member->multicast.comp_mask = comp_mask;
  522. member->multicast.callback = callback;
  523. member->multicast.context = context;
  524. init_completion(&member->comp);
  525. atomic_set(&member->refcount, 1);
  526. member->state = MCAST_JOINING;
  527. member->group = acquire_group(&dev->port[port_num - dev->start_port],
  528. &rec->mgid, gfp_mask);
  529. if (!member->group) {
  530. ret = -ENOMEM;
  531. goto err;
  532. }
  533. /*
  534. * The user will get the multicast structure in their callback. They
  535. * could then free the multicast structure before we can return from
  536. * this routine. So we save the pointer to return before queuing
  537. * any callback.
  538. */
  539. multicast = &member->multicast;
  540. queue_join(member);
  541. return multicast;
  542. err:
  543. ib_sa_client_put(client);
  544. kfree(member);
  545. return ERR_PTR(ret);
  546. }
  547. EXPORT_SYMBOL(ib_sa_join_multicast);
  548. void ib_sa_free_multicast(struct ib_sa_multicast *multicast)
  549. {
  550. struct mcast_member *member;
  551. struct mcast_group *group;
  552. member = container_of(multicast, struct mcast_member, multicast);
  553. group = member->group;
  554. spin_lock_irq(&group->lock);
  555. if (member->state == MCAST_MEMBER)
  556. adjust_membership(group, multicast->rec.join_state, -1);
  557. list_del_init(&member->list);
  558. if (group->state == MCAST_IDLE) {
  559. group->state = MCAST_BUSY;
  560. spin_unlock_irq(&group->lock);
  561. /* Continue to hold reference on group until callback */
  562. queue_work(mcast_wq, &group->work);
  563. } else {
  564. spin_unlock_irq(&group->lock);
  565. release_group(group);
  566. }
  567. deref_member(member);
  568. wait_for_completion(&member->comp);
  569. ib_sa_client_put(member->client);
  570. kfree(member);
  571. }
  572. EXPORT_SYMBOL(ib_sa_free_multicast);
  573. int ib_sa_get_mcmember_rec(struct ib_device *device, u8 port_num,
  574. union ib_gid *mgid, struct ib_sa_mcmember_rec *rec)
  575. {
  576. struct mcast_device *dev;
  577. struct mcast_port *port;
  578. struct mcast_group *group;
  579. unsigned long flags;
  580. int ret = 0;
  581. dev = ib_get_client_data(device, &mcast_client);
  582. if (!dev)
  583. return -ENODEV;
  584. port = &dev->port[port_num - dev->start_port];
  585. spin_lock_irqsave(&port->lock, flags);
  586. group = mcast_find(port, mgid);
  587. if (group)
  588. *rec = group->rec;
  589. else
  590. ret = -EADDRNOTAVAIL;
  591. spin_unlock_irqrestore(&port->lock, flags);
  592. return ret;
  593. }
  594. EXPORT_SYMBOL(ib_sa_get_mcmember_rec);
  595. int ib_init_ah_from_mcmember(struct ib_device *device, u8 port_num,
  596. struct ib_sa_mcmember_rec *rec,
  597. struct ib_ah_attr *ah_attr)
  598. {
  599. int ret;
  600. u16 gid_index;
  601. u8 p;
  602. ret = ib_find_cached_gid(device, &rec->port_gid, &p, &gid_index);
  603. if (ret)
  604. return ret;
  605. memset(ah_attr, 0, sizeof *ah_attr);
  606. ah_attr->dlid = be16_to_cpu(rec->mlid);
  607. ah_attr->sl = rec->sl;
  608. ah_attr->port_num = port_num;
  609. ah_attr->static_rate = rec->rate;
  610. ah_attr->ah_flags = IB_AH_GRH;
  611. ah_attr->grh.dgid = rec->mgid;
  612. ah_attr->grh.sgid_index = (u8) gid_index;
  613. ah_attr->grh.flow_label = be32_to_cpu(rec->flow_label);
  614. ah_attr->grh.hop_limit = rec->hop_limit;
  615. ah_attr->grh.traffic_class = rec->traffic_class;
  616. return 0;
  617. }
  618. EXPORT_SYMBOL(ib_init_ah_from_mcmember);
  619. static void mcast_groups_lost(struct mcast_port *port)
  620. {
  621. struct mcast_group *group;
  622. struct rb_node *node;
  623. unsigned long flags;
  624. spin_lock_irqsave(&port->lock, flags);
  625. for (node = rb_first(&port->table); node; node = rb_next(node)) {
  626. group = rb_entry(node, struct mcast_group, node);
  627. spin_lock(&group->lock);
  628. if (group->state == MCAST_IDLE) {
  629. atomic_inc(&group->refcount);
  630. queue_work(mcast_wq, &group->work);
  631. }
  632. group->state = MCAST_ERROR;
  633. spin_unlock(&group->lock);
  634. }
  635. spin_unlock_irqrestore(&port->lock, flags);
  636. }
  637. static void mcast_event_handler(struct ib_event_handler *handler,
  638. struct ib_event *event)
  639. {
  640. struct mcast_device *dev;
  641. dev = container_of(handler, struct mcast_device, event_handler);
  642. switch (event->event) {
  643. case IB_EVENT_PORT_ERR:
  644. case IB_EVENT_LID_CHANGE:
  645. case IB_EVENT_SM_CHANGE:
  646. case IB_EVENT_CLIENT_REREGISTER:
  647. mcast_groups_lost(&dev->port[event->element.port_num -
  648. dev->start_port]);
  649. break;
  650. default:
  651. break;
  652. }
  653. }
  654. static void mcast_add_one(struct ib_device *device)
  655. {
  656. struct mcast_device *dev;
  657. struct mcast_port *port;
  658. int i;
  659. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  660. return;
  661. dev = kmalloc(sizeof *dev + device->phys_port_cnt * sizeof *port,
  662. GFP_KERNEL);
  663. if (!dev)
  664. return;
  665. if (device->node_type == RDMA_NODE_IB_SWITCH)
  666. dev->start_port = dev->end_port = 0;
  667. else {
  668. dev->start_port = 1;
  669. dev->end_port = device->phys_port_cnt;
  670. }
  671. for (i = 0; i <= dev->end_port - dev->start_port; i++) {
  672. port = &dev->port[i];
  673. port->dev = dev;
  674. port->port_num = dev->start_port + i;
  675. spin_lock_init(&port->lock);
  676. port->table = RB_ROOT;
  677. init_completion(&port->comp);
  678. atomic_set(&port->refcount, 1);
  679. }
  680. dev->device = device;
  681. ib_set_client_data(device, &mcast_client, dev);
  682. INIT_IB_EVENT_HANDLER(&dev->event_handler, device, mcast_event_handler);
  683. ib_register_event_handler(&dev->event_handler);
  684. }
  685. static void mcast_remove_one(struct ib_device *device)
  686. {
  687. struct mcast_device *dev;
  688. struct mcast_port *port;
  689. int i;
  690. dev = ib_get_client_data(device, &mcast_client);
  691. if (!dev)
  692. return;
  693. ib_unregister_event_handler(&dev->event_handler);
  694. flush_workqueue(mcast_wq);
  695. for (i = 0; i <= dev->end_port - dev->start_port; i++) {
  696. port = &dev->port[i];
  697. deref_port(port);
  698. wait_for_completion(&port->comp);
  699. }
  700. kfree(dev);
  701. }
  702. int mcast_init(void)
  703. {
  704. int ret;
  705. mcast_wq = create_singlethread_workqueue("ib_mcast");
  706. if (!mcast_wq)
  707. return -ENOMEM;
  708. ib_sa_register_client(&sa_client);
  709. ret = ib_register_client(&mcast_client);
  710. if (ret)
  711. goto err;
  712. return 0;
  713. err:
  714. ib_sa_unregister_client(&sa_client);
  715. destroy_workqueue(mcast_wq);
  716. return ret;
  717. }
  718. void mcast_cleanup(void)
  719. {
  720. ib_unregister_client(&mcast_client);
  721. ib_sa_unregister_client(&sa_client);
  722. destroy_workqueue(mcast_wq);
  723. }