multicast.c 21 KB

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