multicast.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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/slab.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_JOINING,
  69. MCAST_MEMBER,
  70. MCAST_ERROR,
  71. };
  72. enum mcast_group_state {
  73. MCAST_IDLE,
  74. MCAST_BUSY,
  75. MCAST_GROUP_ERROR,
  76. MCAST_PKEY_EVENT
  77. };
  78. enum {
  79. MCAST_INVALID_PKEY_INDEX = 0xFFFF
  80. };
  81. struct mcast_member;
  82. struct mcast_group {
  83. struct ib_sa_mcmember_rec rec;
  84. struct rb_node node;
  85. struct mcast_port *port;
  86. spinlock_t lock;
  87. struct work_struct work;
  88. struct list_head pending_list;
  89. struct list_head active_list;
  90. struct mcast_member *last_join;
  91. int members[3];
  92. atomic_t refcount;
  93. enum mcast_group_state state;
  94. struct ib_sa_query *query;
  95. int query_id;
  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 three types of members: full member, non member, and
  195. * send only member. We need to keep track of the number of members of each
  196. * type based on their join state. Adjust the number of members the belong to
  197. * the specified join states.
  198. */
  199. static void adjust_membership(struct mcast_group *group, u8 join_state, int inc)
  200. {
  201. int i;
  202. for (i = 0; i < 3; i++, join_state >>= 1)
  203. if (join_state & 0x1)
  204. group->members[i] += inc;
  205. }
  206. /*
  207. * If a multicast group has zero members left for a particular join state, but
  208. * the group is still a member with the SA, we need to leave that join state.
  209. * Determine which join states we still belong to, but that do not have any
  210. * active members.
  211. */
  212. static u8 get_leave_state(struct mcast_group *group)
  213. {
  214. u8 leave_state = 0;
  215. int i;
  216. for (i = 0; i < 3; i++)
  217. if (!group->members[i])
  218. leave_state |= (0x1 << i);
  219. return leave_state & group->rec.join_state;
  220. }
  221. static int check_selector(ib_sa_comp_mask comp_mask,
  222. ib_sa_comp_mask selector_mask,
  223. ib_sa_comp_mask value_mask,
  224. u8 selector, u8 src_value, u8 dst_value)
  225. {
  226. int err;
  227. if (!(comp_mask & selector_mask) || !(comp_mask & value_mask))
  228. return 0;
  229. switch (selector) {
  230. case IB_SA_GT:
  231. err = (src_value <= dst_value);
  232. break;
  233. case IB_SA_LT:
  234. err = (src_value >= dst_value);
  235. break;
  236. case IB_SA_EQ:
  237. err = (src_value != dst_value);
  238. break;
  239. default:
  240. err = 0;
  241. break;
  242. }
  243. return err;
  244. }
  245. static int cmp_rec(struct ib_sa_mcmember_rec *src,
  246. struct ib_sa_mcmember_rec *dst, ib_sa_comp_mask comp_mask)
  247. {
  248. /* MGID must already match */
  249. if (comp_mask & IB_SA_MCMEMBER_REC_PORT_GID &&
  250. memcmp(&src->port_gid, &dst->port_gid, sizeof src->port_gid))
  251. return -EINVAL;
  252. if (comp_mask & IB_SA_MCMEMBER_REC_QKEY && src->qkey != dst->qkey)
  253. return -EINVAL;
  254. if (comp_mask & IB_SA_MCMEMBER_REC_MLID && src->mlid != dst->mlid)
  255. return -EINVAL;
  256. if (check_selector(comp_mask, IB_SA_MCMEMBER_REC_MTU_SELECTOR,
  257. IB_SA_MCMEMBER_REC_MTU, dst->mtu_selector,
  258. src->mtu, dst->mtu))
  259. return -EINVAL;
  260. if (comp_mask & IB_SA_MCMEMBER_REC_TRAFFIC_CLASS &&
  261. src->traffic_class != dst->traffic_class)
  262. return -EINVAL;
  263. if (comp_mask & IB_SA_MCMEMBER_REC_PKEY && src->pkey != dst->pkey)
  264. return -EINVAL;
  265. if (check_selector(comp_mask, IB_SA_MCMEMBER_REC_RATE_SELECTOR,
  266. IB_SA_MCMEMBER_REC_RATE, dst->rate_selector,
  267. src->rate, dst->rate))
  268. return -EINVAL;
  269. if (check_selector(comp_mask,
  270. IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME_SELECTOR,
  271. IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME,
  272. dst->packet_life_time_selector,
  273. src->packet_life_time, dst->packet_life_time))
  274. return -EINVAL;
  275. if (comp_mask & IB_SA_MCMEMBER_REC_SL && src->sl != dst->sl)
  276. return -EINVAL;
  277. if (comp_mask & IB_SA_MCMEMBER_REC_FLOW_LABEL &&
  278. src->flow_label != dst->flow_label)
  279. return -EINVAL;
  280. if (comp_mask & IB_SA_MCMEMBER_REC_HOP_LIMIT &&
  281. src->hop_limit != dst->hop_limit)
  282. return -EINVAL;
  283. if (comp_mask & IB_SA_MCMEMBER_REC_SCOPE && src->scope != dst->scope)
  284. return -EINVAL;
  285. /* join_state checked separately, proxy_join ignored */
  286. return 0;
  287. }
  288. static int send_join(struct mcast_group *group, struct mcast_member *member)
  289. {
  290. struct mcast_port *port = group->port;
  291. int ret;
  292. group->last_join = member;
  293. ret = ib_sa_mcmember_rec_query(&sa_client, port->dev->device,
  294. port->port_num, IB_MGMT_METHOD_SET,
  295. &member->multicast.rec,
  296. member->multicast.comp_mask,
  297. 3000, GFP_KERNEL, join_handler, group,
  298. &group->query);
  299. if (ret >= 0) {
  300. group->query_id = ret;
  301. ret = 0;
  302. }
  303. return ret;
  304. }
  305. static int send_leave(struct mcast_group *group, u8 leave_state)
  306. {
  307. struct mcast_port *port = group->port;
  308. struct ib_sa_mcmember_rec rec;
  309. int ret;
  310. rec = group->rec;
  311. rec.join_state = leave_state;
  312. group->leave_state = leave_state;
  313. ret = ib_sa_mcmember_rec_query(&sa_client, port->dev->device,
  314. port->port_num, IB_SA_METHOD_DELETE, &rec,
  315. IB_SA_MCMEMBER_REC_MGID |
  316. IB_SA_MCMEMBER_REC_PORT_GID |
  317. IB_SA_MCMEMBER_REC_JOIN_STATE,
  318. 3000, GFP_KERNEL, leave_handler,
  319. group, &group->query);
  320. if (ret >= 0) {
  321. group->query_id = ret;
  322. ret = 0;
  323. }
  324. return ret;
  325. }
  326. static void join_group(struct mcast_group *group, struct mcast_member *member,
  327. u8 join_state)
  328. {
  329. member->state = MCAST_MEMBER;
  330. adjust_membership(group, join_state, 1);
  331. group->rec.join_state |= join_state;
  332. member->multicast.rec = group->rec;
  333. member->multicast.rec.join_state = join_state;
  334. list_move(&member->list, &group->active_list);
  335. }
  336. static int fail_join(struct mcast_group *group, struct mcast_member *member,
  337. int status)
  338. {
  339. spin_lock_irq(&group->lock);
  340. list_del_init(&member->list);
  341. spin_unlock_irq(&group->lock);
  342. return member->multicast.callback(status, &member->multicast);
  343. }
  344. static void process_group_error(struct mcast_group *group)
  345. {
  346. struct mcast_member *member;
  347. int ret = 0;
  348. u16 pkey_index;
  349. if (group->state == MCAST_PKEY_EVENT)
  350. ret = ib_find_pkey(group->port->dev->device,
  351. group->port->port_num,
  352. be16_to_cpu(group->rec.pkey), &pkey_index);
  353. spin_lock_irq(&group->lock);
  354. if (group->state == MCAST_PKEY_EVENT && !ret &&
  355. group->pkey_index == pkey_index)
  356. goto out;
  357. while (!list_empty(&group->active_list)) {
  358. member = list_entry(group->active_list.next,
  359. struct mcast_member, list);
  360. atomic_inc(&member->refcount);
  361. list_del_init(&member->list);
  362. adjust_membership(group, member->multicast.rec.join_state, -1);
  363. member->state = MCAST_ERROR;
  364. spin_unlock_irq(&group->lock);
  365. ret = member->multicast.callback(-ENETRESET,
  366. &member->multicast);
  367. deref_member(member);
  368. if (ret)
  369. ib_sa_free_multicast(&member->multicast);
  370. spin_lock_irq(&group->lock);
  371. }
  372. group->rec.join_state = 0;
  373. out:
  374. group->state = MCAST_BUSY;
  375. spin_unlock_irq(&group->lock);
  376. }
  377. static void mcast_work_handler(struct work_struct *work)
  378. {
  379. struct mcast_group *group;
  380. struct mcast_member *member;
  381. struct ib_sa_multicast *multicast;
  382. int status, ret;
  383. u8 join_state;
  384. group = container_of(work, typeof(*group), work);
  385. retest:
  386. spin_lock_irq(&group->lock);
  387. while (!list_empty(&group->pending_list) ||
  388. (group->state != MCAST_BUSY)) {
  389. if (group->state != MCAST_BUSY) {
  390. spin_unlock_irq(&group->lock);
  391. process_group_error(group);
  392. goto retest;
  393. }
  394. member = list_entry(group->pending_list.next,
  395. struct mcast_member, list);
  396. multicast = &member->multicast;
  397. join_state = multicast->rec.join_state;
  398. atomic_inc(&member->refcount);
  399. if (join_state == (group->rec.join_state & join_state)) {
  400. status = cmp_rec(&group->rec, &multicast->rec,
  401. multicast->comp_mask);
  402. if (!status)
  403. join_group(group, member, join_state);
  404. else
  405. list_del_init(&member->list);
  406. spin_unlock_irq(&group->lock);
  407. ret = multicast->callback(status, multicast);
  408. } else {
  409. spin_unlock_irq(&group->lock);
  410. status = send_join(group, member);
  411. if (!status) {
  412. deref_member(member);
  413. return;
  414. }
  415. ret = fail_join(group, member, status);
  416. }
  417. deref_member(member);
  418. if (ret)
  419. ib_sa_free_multicast(&member->multicast);
  420. spin_lock_irq(&group->lock);
  421. }
  422. join_state = get_leave_state(group);
  423. if (join_state) {
  424. group->rec.join_state &= ~join_state;
  425. spin_unlock_irq(&group->lock);
  426. if (send_leave(group, join_state))
  427. goto retest;
  428. } else {
  429. group->state = MCAST_IDLE;
  430. spin_unlock_irq(&group->lock);
  431. release_group(group);
  432. }
  433. }
  434. /*
  435. * Fail a join request if it is still active - at the head of the pending queue.
  436. */
  437. static void process_join_error(struct mcast_group *group, int status)
  438. {
  439. struct mcast_member *member;
  440. int ret;
  441. spin_lock_irq(&group->lock);
  442. member = list_entry(group->pending_list.next,
  443. struct mcast_member, list);
  444. if (group->last_join == member) {
  445. atomic_inc(&member->refcount);
  446. list_del_init(&member->list);
  447. spin_unlock_irq(&group->lock);
  448. ret = member->multicast.callback(status, &member->multicast);
  449. deref_member(member);
  450. if (ret)
  451. ib_sa_free_multicast(&member->multicast);
  452. } else
  453. spin_unlock_irq(&group->lock);
  454. }
  455. static void join_handler(int status, struct ib_sa_mcmember_rec *rec,
  456. void *context)
  457. {
  458. struct mcast_group *group = context;
  459. u16 pkey_index = MCAST_INVALID_PKEY_INDEX;
  460. if (status)
  461. process_join_error(group, status);
  462. else {
  463. ib_find_pkey(group->port->dev->device, group->port->port_num,
  464. be16_to_cpu(rec->pkey), &pkey_index);
  465. spin_lock_irq(&group->port->lock);
  466. group->rec = *rec;
  467. if (group->state == MCAST_BUSY &&
  468. group->pkey_index == MCAST_INVALID_PKEY_INDEX)
  469. group->pkey_index = pkey_index;
  470. if (!memcmp(&mgid0, &group->rec.mgid, sizeof mgid0)) {
  471. rb_erase(&group->node, &group->port->table);
  472. mcast_insert(group->port, group, 1);
  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. int ib_init_ah_from_mcmember(struct ib_device *device, u8 port_num,
  629. struct ib_sa_mcmember_rec *rec,
  630. struct ib_ah_attr *ah_attr)
  631. {
  632. int ret;
  633. u16 gid_index;
  634. u8 p;
  635. ret = ib_find_cached_gid(device, &rec->port_gid, &p, &gid_index);
  636. if (ret)
  637. return ret;
  638. memset(ah_attr, 0, sizeof *ah_attr);
  639. ah_attr->dlid = be16_to_cpu(rec->mlid);
  640. ah_attr->sl = rec->sl;
  641. ah_attr->port_num = port_num;
  642. ah_attr->static_rate = rec->rate;
  643. ah_attr->ah_flags = IB_AH_GRH;
  644. ah_attr->grh.dgid = rec->mgid;
  645. ah_attr->grh.sgid_index = (u8) gid_index;
  646. ah_attr->grh.flow_label = be32_to_cpu(rec->flow_label);
  647. ah_attr->grh.hop_limit = rec->hop_limit;
  648. ah_attr->grh.traffic_class = rec->traffic_class;
  649. return 0;
  650. }
  651. EXPORT_SYMBOL(ib_init_ah_from_mcmember);
  652. static void mcast_groups_event(struct mcast_port *port,
  653. enum mcast_group_state state)
  654. {
  655. struct mcast_group *group;
  656. struct rb_node *node;
  657. unsigned long flags;
  658. spin_lock_irqsave(&port->lock, flags);
  659. for (node = rb_first(&port->table); node; node = rb_next(node)) {
  660. group = rb_entry(node, struct mcast_group, node);
  661. spin_lock(&group->lock);
  662. if (group->state == MCAST_IDLE) {
  663. atomic_inc(&group->refcount);
  664. queue_work(mcast_wq, &group->work);
  665. }
  666. if (group->state != MCAST_GROUP_ERROR)
  667. group->state = state;
  668. spin_unlock(&group->lock);
  669. }
  670. spin_unlock_irqrestore(&port->lock, flags);
  671. }
  672. static void mcast_event_handler(struct ib_event_handler *handler,
  673. struct ib_event *event)
  674. {
  675. struct mcast_device *dev;
  676. int index;
  677. dev = container_of(handler, struct mcast_device, event_handler);
  678. if (rdma_port_get_link_layer(dev->device, event->element.port_num) !=
  679. IB_LINK_LAYER_INFINIBAND)
  680. return;
  681. index = event->element.port_num - dev->start_port;
  682. switch (event->event) {
  683. case IB_EVENT_PORT_ERR:
  684. case IB_EVENT_LID_CHANGE:
  685. case IB_EVENT_SM_CHANGE:
  686. case IB_EVENT_CLIENT_REREGISTER:
  687. mcast_groups_event(&dev->port[index], MCAST_GROUP_ERROR);
  688. break;
  689. case IB_EVENT_PKEY_CHANGE:
  690. mcast_groups_event(&dev->port[index], MCAST_PKEY_EVENT);
  691. break;
  692. default:
  693. break;
  694. }
  695. }
  696. static void mcast_add_one(struct ib_device *device)
  697. {
  698. struct mcast_device *dev;
  699. struct mcast_port *port;
  700. int i;
  701. int count = 0;
  702. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  703. return;
  704. dev = kmalloc(sizeof *dev + device->phys_port_cnt * sizeof *port,
  705. GFP_KERNEL);
  706. if (!dev)
  707. return;
  708. if (device->node_type == RDMA_NODE_IB_SWITCH)
  709. dev->start_port = dev->end_port = 0;
  710. else {
  711. dev->start_port = 1;
  712. dev->end_port = device->phys_port_cnt;
  713. }
  714. for (i = 0; i <= dev->end_port - dev->start_port; i++) {
  715. if (rdma_port_get_link_layer(device, dev->start_port + i) !=
  716. IB_LINK_LAYER_INFINIBAND)
  717. continue;
  718. port = &dev->port[i];
  719. port->dev = dev;
  720. port->port_num = dev->start_port + i;
  721. spin_lock_init(&port->lock);
  722. port->table = RB_ROOT;
  723. init_completion(&port->comp);
  724. atomic_set(&port->refcount, 1);
  725. ++count;
  726. }
  727. if (!count) {
  728. kfree(dev);
  729. return;
  730. }
  731. dev->device = device;
  732. ib_set_client_data(device, &mcast_client, dev);
  733. INIT_IB_EVENT_HANDLER(&dev->event_handler, device, mcast_event_handler);
  734. ib_register_event_handler(&dev->event_handler);
  735. }
  736. static void mcast_remove_one(struct ib_device *device)
  737. {
  738. struct mcast_device *dev;
  739. struct mcast_port *port;
  740. int i;
  741. dev = ib_get_client_data(device, &mcast_client);
  742. if (!dev)
  743. return;
  744. ib_unregister_event_handler(&dev->event_handler);
  745. flush_workqueue(mcast_wq);
  746. for (i = 0; i <= dev->end_port - dev->start_port; i++) {
  747. if (rdma_port_get_link_layer(device, dev->start_port + i) ==
  748. IB_LINK_LAYER_INFINIBAND) {
  749. port = &dev->port[i];
  750. deref_port(port);
  751. wait_for_completion(&port->comp);
  752. }
  753. }
  754. kfree(dev);
  755. }
  756. int mcast_init(void)
  757. {
  758. int ret;
  759. mcast_wq = create_singlethread_workqueue("ib_mcast");
  760. if (!mcast_wq)
  761. return -ENOMEM;
  762. ib_sa_register_client(&sa_client);
  763. ret = ib_register_client(&mcast_client);
  764. if (ret)
  765. goto err;
  766. return 0;
  767. err:
  768. ib_sa_unregister_client(&sa_client);
  769. destroy_workqueue(mcast_wq);
  770. return ret;
  771. }
  772. void mcast_cleanup(void)
  773. {
  774. ib_unregister_client(&mcast_client);
  775. ib_sa_unregister_client(&sa_client);
  776. destroy_workqueue(mcast_wq);
  777. }