mcg.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*
  2. * Copyright (c) 2012 Mellanox Technologies. 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 <rdma/ib_mad.h>
  33. #include <rdma/ib_smi.h>
  34. #include <rdma/ib_cache.h>
  35. #include <rdma/ib_sa.h>
  36. #include <linux/mlx4/cmd.h>
  37. #include <linux/rbtree.h>
  38. #include <linux/delay.h>
  39. #include "mlx4_ib.h"
  40. #define MAX_VFS 80
  41. #define MAX_PEND_REQS_PER_FUNC 4
  42. #define MAD_TIMEOUT_MS 2000
  43. #define mcg_warn(fmt, arg...) pr_warn("MCG WARNING: " fmt, ##arg)
  44. #define mcg_error(fmt, arg...) pr_err(fmt, ##arg)
  45. #define mcg_warn_group(group, format, arg...) \
  46. pr_warn("%s-%d: %16s (port %d): WARNING: " format, __func__, __LINE__,\
  47. (group)->name, group->demux->port, ## arg)
  48. #define mcg_error_group(group, format, arg...) \
  49. pr_err(" %16s: " format, (group)->name, ## arg)
  50. static union ib_gid mgid0;
  51. static struct workqueue_struct *clean_wq;
  52. enum mcast_state {
  53. MCAST_NOT_MEMBER = 0,
  54. MCAST_MEMBER,
  55. };
  56. enum mcast_group_state {
  57. MCAST_IDLE,
  58. MCAST_JOIN_SENT,
  59. MCAST_LEAVE_SENT,
  60. MCAST_RESP_READY
  61. };
  62. struct mcast_member {
  63. enum mcast_state state;
  64. uint8_t join_state;
  65. int num_pend_reqs;
  66. struct list_head pending;
  67. };
  68. struct ib_sa_mcmember_data {
  69. union ib_gid mgid;
  70. union ib_gid port_gid;
  71. __be32 qkey;
  72. __be16 mlid;
  73. u8 mtusel_mtu;
  74. u8 tclass;
  75. __be16 pkey;
  76. u8 ratesel_rate;
  77. u8 lifetmsel_lifetm;
  78. __be32 sl_flowlabel_hoplimit;
  79. u8 scope_join_state;
  80. u8 proxy_join;
  81. u8 reserved[2];
  82. };
  83. struct mcast_group {
  84. struct ib_sa_mcmember_data rec;
  85. struct rb_node node;
  86. struct list_head mgid0_list;
  87. struct mlx4_ib_demux_ctx *demux;
  88. struct mcast_member func[MAX_VFS];
  89. struct mutex lock;
  90. struct work_struct work;
  91. struct list_head pending_list;
  92. int members[3];
  93. enum mcast_group_state state;
  94. enum mcast_group_state prev_state;
  95. struct ib_sa_mad response_sa_mad;
  96. __be64 last_req_tid;
  97. char name[33]; /* MGID string */
  98. struct device_attribute dentry;
  99. /* refcount is the reference count for the following:
  100. 1. Each queued request
  101. 2. Each invocation of the worker thread
  102. 3. Membership of the port at the SA
  103. */
  104. atomic_t refcount;
  105. /* delayed work to clean pending SM request */
  106. struct delayed_work timeout_work;
  107. struct list_head cleanup_list;
  108. };
  109. struct mcast_req {
  110. int func;
  111. struct ib_sa_mad sa_mad;
  112. struct list_head group_list;
  113. struct list_head func_list;
  114. struct mcast_group *group;
  115. int clean;
  116. };
  117. #define safe_atomic_dec(ref) \
  118. do {\
  119. if (atomic_dec_and_test(ref)) \
  120. mcg_warn_group(group, "did not expect to reach zero\n"); \
  121. } while (0)
  122. static const char *get_state_string(enum mcast_group_state state)
  123. {
  124. switch (state) {
  125. case MCAST_IDLE:
  126. return "MCAST_IDLE";
  127. case MCAST_JOIN_SENT:
  128. return "MCAST_JOIN_SENT";
  129. case MCAST_LEAVE_SENT:
  130. return "MCAST_LEAVE_SENT";
  131. case MCAST_RESP_READY:
  132. return "MCAST_RESP_READY";
  133. }
  134. return "Invalid State";
  135. }
  136. static struct mcast_group *mcast_find(struct mlx4_ib_demux_ctx *ctx,
  137. union ib_gid *mgid)
  138. {
  139. struct rb_node *node = ctx->mcg_table.rb_node;
  140. struct mcast_group *group;
  141. int ret;
  142. while (node) {
  143. group = rb_entry(node, struct mcast_group, node);
  144. ret = memcmp(mgid->raw, group->rec.mgid.raw, sizeof *mgid);
  145. if (!ret)
  146. return group;
  147. if (ret < 0)
  148. node = node->rb_left;
  149. else
  150. node = node->rb_right;
  151. }
  152. return NULL;
  153. }
  154. static struct mcast_group *mcast_insert(struct mlx4_ib_demux_ctx *ctx,
  155. struct mcast_group *group)
  156. {
  157. struct rb_node **link = &ctx->mcg_table.rb_node;
  158. struct rb_node *parent = NULL;
  159. struct mcast_group *cur_group;
  160. int ret;
  161. while (*link) {
  162. parent = *link;
  163. cur_group = rb_entry(parent, struct mcast_group, node);
  164. ret = memcmp(group->rec.mgid.raw, cur_group->rec.mgid.raw,
  165. sizeof group->rec.mgid);
  166. if (ret < 0)
  167. link = &(*link)->rb_left;
  168. else if (ret > 0)
  169. link = &(*link)->rb_right;
  170. else
  171. return cur_group;
  172. }
  173. rb_link_node(&group->node, parent, link);
  174. rb_insert_color(&group->node, &ctx->mcg_table);
  175. return NULL;
  176. }
  177. static int send_mad_to_wire(struct mlx4_ib_demux_ctx *ctx, struct ib_mad *mad)
  178. {
  179. struct mlx4_ib_dev *dev = ctx->dev;
  180. struct ib_ah_attr ah_attr;
  181. spin_lock(&dev->sm_lock);
  182. if (!dev->sm_ah[ctx->port - 1]) {
  183. /* port is not yet Active, sm_ah not ready */
  184. spin_unlock(&dev->sm_lock);
  185. return -EAGAIN;
  186. }
  187. mlx4_ib_query_ah(dev->sm_ah[ctx->port - 1], &ah_attr);
  188. spin_unlock(&dev->sm_lock);
  189. return mlx4_ib_send_to_wire(dev, mlx4_master_func_num(dev->dev), ctx->port,
  190. IB_QPT_GSI, 0, 1, IB_QP1_QKEY, &ah_attr, mad);
  191. }
  192. static int send_mad_to_slave(int slave, struct mlx4_ib_demux_ctx *ctx,
  193. struct ib_mad *mad)
  194. {
  195. struct mlx4_ib_dev *dev = ctx->dev;
  196. struct ib_mad_agent *agent = dev->send_agent[ctx->port - 1][1];
  197. struct ib_wc wc;
  198. struct ib_ah_attr ah_attr;
  199. /* Our agent might not yet be registered when mads start to arrive */
  200. if (!agent)
  201. return -EAGAIN;
  202. ib_query_ah(dev->sm_ah[ctx->port - 1], &ah_attr);
  203. wc.pkey_index = 0;
  204. wc.sl = 0;
  205. wc.dlid_path_bits = 0;
  206. wc.port_num = ctx->port;
  207. wc.slid = ah_attr.dlid; /* opensm lid */
  208. wc.src_qp = 1;
  209. return mlx4_ib_send_to_slave(dev, slave, ctx->port, IB_QPT_GSI, &wc, NULL, mad);
  210. }
  211. static int send_join_to_wire(struct mcast_group *group, struct ib_sa_mad *sa_mad)
  212. {
  213. struct ib_sa_mad mad;
  214. struct ib_sa_mcmember_data *sa_mad_data = (struct ib_sa_mcmember_data *)&mad.data;
  215. int ret;
  216. /* we rely on a mad request as arrived from a VF */
  217. memcpy(&mad, sa_mad, sizeof mad);
  218. /* fix port GID to be the real one (slave 0) */
  219. sa_mad_data->port_gid.global.interface_id = group->demux->guid_cache[0];
  220. /* assign our own TID */
  221. mad.mad_hdr.tid = mlx4_ib_get_new_demux_tid(group->demux);
  222. group->last_req_tid = mad.mad_hdr.tid; /* keep it for later validation */
  223. ret = send_mad_to_wire(group->demux, (struct ib_mad *)&mad);
  224. /* set timeout handler */
  225. if (!ret) {
  226. /* calls mlx4_ib_mcg_timeout_handler */
  227. queue_delayed_work(group->demux->mcg_wq, &group->timeout_work,
  228. msecs_to_jiffies(MAD_TIMEOUT_MS));
  229. }
  230. return ret;
  231. }
  232. static int send_leave_to_wire(struct mcast_group *group, u8 join_state)
  233. {
  234. struct ib_sa_mad mad;
  235. struct ib_sa_mcmember_data *sa_data = (struct ib_sa_mcmember_data *)&mad.data;
  236. int ret;
  237. memset(&mad, 0, sizeof mad);
  238. mad.mad_hdr.base_version = 1;
  239. mad.mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  240. mad.mad_hdr.class_version = 2;
  241. mad.mad_hdr.method = IB_SA_METHOD_DELETE;
  242. mad.mad_hdr.status = cpu_to_be16(0);
  243. mad.mad_hdr.class_specific = cpu_to_be16(0);
  244. mad.mad_hdr.tid = mlx4_ib_get_new_demux_tid(group->demux);
  245. group->last_req_tid = mad.mad_hdr.tid; /* keep it for later validation */
  246. mad.mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  247. mad.mad_hdr.attr_mod = cpu_to_be32(0);
  248. mad.sa_hdr.sm_key = 0x0;
  249. mad.sa_hdr.attr_offset = cpu_to_be16(7);
  250. mad.sa_hdr.comp_mask = IB_SA_MCMEMBER_REC_MGID |
  251. IB_SA_MCMEMBER_REC_PORT_GID | IB_SA_MCMEMBER_REC_JOIN_STATE;
  252. *sa_data = group->rec;
  253. sa_data->scope_join_state = join_state;
  254. ret = send_mad_to_wire(group->demux, (struct ib_mad *)&mad);
  255. if (ret)
  256. group->state = MCAST_IDLE;
  257. /* set timeout handler */
  258. if (!ret) {
  259. /* calls mlx4_ib_mcg_timeout_handler */
  260. queue_delayed_work(group->demux->mcg_wq, &group->timeout_work,
  261. msecs_to_jiffies(MAD_TIMEOUT_MS));
  262. }
  263. return ret;
  264. }
  265. static int send_reply_to_slave(int slave, struct mcast_group *group,
  266. struct ib_sa_mad *req_sa_mad, u16 status)
  267. {
  268. struct ib_sa_mad mad;
  269. struct ib_sa_mcmember_data *sa_data = (struct ib_sa_mcmember_data *)&mad.data;
  270. struct ib_sa_mcmember_data *req_sa_data = (struct ib_sa_mcmember_data *)&req_sa_mad->data;
  271. int ret;
  272. memset(&mad, 0, sizeof mad);
  273. mad.mad_hdr.base_version = 1;
  274. mad.mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  275. mad.mad_hdr.class_version = 2;
  276. mad.mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
  277. mad.mad_hdr.status = cpu_to_be16(status);
  278. mad.mad_hdr.class_specific = cpu_to_be16(0);
  279. mad.mad_hdr.tid = req_sa_mad->mad_hdr.tid;
  280. *(u8 *)&mad.mad_hdr.tid = 0; /* resetting tid to 0 */
  281. mad.mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  282. mad.mad_hdr.attr_mod = cpu_to_be32(0);
  283. mad.sa_hdr.sm_key = req_sa_mad->sa_hdr.sm_key;
  284. mad.sa_hdr.attr_offset = cpu_to_be16(7);
  285. mad.sa_hdr.comp_mask = 0; /* ignored on responses, see IBTA spec */
  286. *sa_data = group->rec;
  287. /* reconstruct VF's requested join_state and port_gid */
  288. sa_data->scope_join_state &= 0xf0;
  289. sa_data->scope_join_state |= (group->func[slave].join_state & 0x0f);
  290. memcpy(&sa_data->port_gid, &req_sa_data->port_gid, sizeof req_sa_data->port_gid);
  291. ret = send_mad_to_slave(slave, group->demux, (struct ib_mad *)&mad);
  292. return ret;
  293. }
  294. static int check_selector(ib_sa_comp_mask comp_mask,
  295. ib_sa_comp_mask selector_mask,
  296. ib_sa_comp_mask value_mask,
  297. u8 src_value, u8 dst_value)
  298. {
  299. int err;
  300. u8 selector = dst_value >> 6;
  301. dst_value &= 0x3f;
  302. src_value &= 0x3f;
  303. if (!(comp_mask & selector_mask) || !(comp_mask & value_mask))
  304. return 0;
  305. switch (selector) {
  306. case IB_SA_GT:
  307. err = (src_value <= dst_value);
  308. break;
  309. case IB_SA_LT:
  310. err = (src_value >= dst_value);
  311. break;
  312. case IB_SA_EQ:
  313. err = (src_value != dst_value);
  314. break;
  315. default:
  316. err = 0;
  317. break;
  318. }
  319. return err;
  320. }
  321. static u16 cmp_rec(struct ib_sa_mcmember_data *src,
  322. struct ib_sa_mcmember_data *dst, ib_sa_comp_mask comp_mask)
  323. {
  324. /* src is group record, dst is request record */
  325. /* MGID must already match */
  326. /* Port_GID we always replace to our Port_GID, so it is a match */
  327. #define MAD_STATUS_REQ_INVALID 0x0200
  328. if (comp_mask & IB_SA_MCMEMBER_REC_QKEY && src->qkey != dst->qkey)
  329. return MAD_STATUS_REQ_INVALID;
  330. if (comp_mask & IB_SA_MCMEMBER_REC_MLID && src->mlid != dst->mlid)
  331. return MAD_STATUS_REQ_INVALID;
  332. if (check_selector(comp_mask, IB_SA_MCMEMBER_REC_MTU_SELECTOR,
  333. IB_SA_MCMEMBER_REC_MTU,
  334. src->mtusel_mtu, dst->mtusel_mtu))
  335. return MAD_STATUS_REQ_INVALID;
  336. if (comp_mask & IB_SA_MCMEMBER_REC_TRAFFIC_CLASS &&
  337. src->tclass != dst->tclass)
  338. return MAD_STATUS_REQ_INVALID;
  339. if (comp_mask & IB_SA_MCMEMBER_REC_PKEY && src->pkey != dst->pkey)
  340. return MAD_STATUS_REQ_INVALID;
  341. if (check_selector(comp_mask, IB_SA_MCMEMBER_REC_RATE_SELECTOR,
  342. IB_SA_MCMEMBER_REC_RATE,
  343. src->ratesel_rate, dst->ratesel_rate))
  344. return MAD_STATUS_REQ_INVALID;
  345. if (check_selector(comp_mask,
  346. IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME_SELECTOR,
  347. IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME,
  348. src->lifetmsel_lifetm, dst->lifetmsel_lifetm))
  349. return MAD_STATUS_REQ_INVALID;
  350. if (comp_mask & IB_SA_MCMEMBER_REC_SL &&
  351. (be32_to_cpu(src->sl_flowlabel_hoplimit) & 0xf0000000) !=
  352. (be32_to_cpu(dst->sl_flowlabel_hoplimit) & 0xf0000000))
  353. return MAD_STATUS_REQ_INVALID;
  354. if (comp_mask & IB_SA_MCMEMBER_REC_FLOW_LABEL &&
  355. (be32_to_cpu(src->sl_flowlabel_hoplimit) & 0x0fffff00) !=
  356. (be32_to_cpu(dst->sl_flowlabel_hoplimit) & 0x0fffff00))
  357. return MAD_STATUS_REQ_INVALID;
  358. if (comp_mask & IB_SA_MCMEMBER_REC_HOP_LIMIT &&
  359. (be32_to_cpu(src->sl_flowlabel_hoplimit) & 0x000000ff) !=
  360. (be32_to_cpu(dst->sl_flowlabel_hoplimit) & 0x000000ff))
  361. return MAD_STATUS_REQ_INVALID;
  362. if (comp_mask & IB_SA_MCMEMBER_REC_SCOPE &&
  363. (src->scope_join_state & 0xf0) !=
  364. (dst->scope_join_state & 0xf0))
  365. return MAD_STATUS_REQ_INVALID;
  366. /* join_state checked separately, proxy_join ignored */
  367. return 0;
  368. }
  369. /* release group, return 1 if this was last release and group is destroyed
  370. * timout work is canceled sync */
  371. static int release_group(struct mcast_group *group, int from_timeout_handler)
  372. {
  373. struct mlx4_ib_demux_ctx *ctx = group->demux;
  374. int nzgroup;
  375. mutex_lock(&ctx->mcg_table_lock);
  376. mutex_lock(&group->lock);
  377. if (atomic_dec_and_test(&group->refcount)) {
  378. if (!from_timeout_handler) {
  379. if (group->state != MCAST_IDLE &&
  380. !cancel_delayed_work(&group->timeout_work)) {
  381. atomic_inc(&group->refcount);
  382. mutex_unlock(&group->lock);
  383. mutex_unlock(&ctx->mcg_table_lock);
  384. return 0;
  385. }
  386. }
  387. nzgroup = memcmp(&group->rec.mgid, &mgid0, sizeof mgid0);
  388. if (nzgroup)
  389. del_sysfs_port_mcg_attr(ctx->dev, ctx->port, &group->dentry.attr);
  390. if (!list_empty(&group->pending_list))
  391. mcg_warn_group(group, "releasing a group with non empty pending list\n");
  392. if (nzgroup)
  393. rb_erase(&group->node, &ctx->mcg_table);
  394. list_del_init(&group->mgid0_list);
  395. mutex_unlock(&group->lock);
  396. mutex_unlock(&ctx->mcg_table_lock);
  397. kfree(group);
  398. return 1;
  399. } else {
  400. mutex_unlock(&group->lock);
  401. mutex_unlock(&ctx->mcg_table_lock);
  402. }
  403. return 0;
  404. }
  405. static void adjust_membership(struct mcast_group *group, u8 join_state, int inc)
  406. {
  407. int i;
  408. for (i = 0; i < 3; i++, join_state >>= 1)
  409. if (join_state & 0x1)
  410. group->members[i] += inc;
  411. }
  412. static u8 get_leave_state(struct mcast_group *group)
  413. {
  414. u8 leave_state = 0;
  415. int i;
  416. for (i = 0; i < 3; i++)
  417. if (!group->members[i])
  418. leave_state |= (1 << i);
  419. return leave_state & (group->rec.scope_join_state & 7);
  420. }
  421. static int join_group(struct mcast_group *group, int slave, u8 join_mask)
  422. {
  423. int ret = 0;
  424. u8 join_state;
  425. /* remove bits that slave is already member of, and adjust */
  426. join_state = join_mask & (~group->func[slave].join_state);
  427. adjust_membership(group, join_state, 1);
  428. group->func[slave].join_state |= join_state;
  429. if (group->func[slave].state != MCAST_MEMBER && join_state) {
  430. group->func[slave].state = MCAST_MEMBER;
  431. ret = 1;
  432. }
  433. return ret;
  434. }
  435. static int leave_group(struct mcast_group *group, int slave, u8 leave_state)
  436. {
  437. int ret = 0;
  438. adjust_membership(group, leave_state, -1);
  439. group->func[slave].join_state &= ~leave_state;
  440. if (!group->func[slave].join_state) {
  441. group->func[slave].state = MCAST_NOT_MEMBER;
  442. ret = 1;
  443. }
  444. return ret;
  445. }
  446. static int check_leave(struct mcast_group *group, int slave, u8 leave_mask)
  447. {
  448. if (group->func[slave].state != MCAST_MEMBER)
  449. return MAD_STATUS_REQ_INVALID;
  450. /* make sure we're not deleting unset bits */
  451. if (~group->func[slave].join_state & leave_mask)
  452. return MAD_STATUS_REQ_INVALID;
  453. if (!leave_mask)
  454. return MAD_STATUS_REQ_INVALID;
  455. return 0;
  456. }
  457. static void mlx4_ib_mcg_timeout_handler(struct work_struct *work)
  458. {
  459. struct delayed_work *delay = to_delayed_work(work);
  460. struct mcast_group *group;
  461. struct mcast_req *req = NULL;
  462. group = container_of(delay, typeof(*group), timeout_work);
  463. mutex_lock(&group->lock);
  464. if (group->state == MCAST_JOIN_SENT) {
  465. if (!list_empty(&group->pending_list)) {
  466. req = list_first_entry(&group->pending_list, struct mcast_req, group_list);
  467. list_del(&req->group_list);
  468. list_del(&req->func_list);
  469. --group->func[req->func].num_pend_reqs;
  470. mutex_unlock(&group->lock);
  471. kfree(req);
  472. if (memcmp(&group->rec.mgid, &mgid0, sizeof mgid0)) {
  473. if (release_group(group, 1))
  474. return;
  475. } else {
  476. kfree(group);
  477. return;
  478. }
  479. mutex_lock(&group->lock);
  480. } else
  481. mcg_warn_group(group, "DRIVER BUG\n");
  482. } else if (group->state == MCAST_LEAVE_SENT) {
  483. if (group->rec.scope_join_state & 7)
  484. group->rec.scope_join_state &= 0xf8;
  485. group->state = MCAST_IDLE;
  486. mutex_unlock(&group->lock);
  487. if (release_group(group, 1))
  488. return;
  489. mutex_lock(&group->lock);
  490. } else
  491. mcg_warn_group(group, "invalid state %s\n", get_state_string(group->state));
  492. group->state = MCAST_IDLE;
  493. atomic_inc(&group->refcount);
  494. if (!queue_work(group->demux->mcg_wq, &group->work))
  495. safe_atomic_dec(&group->refcount);
  496. mutex_unlock(&group->lock);
  497. }
  498. static int handle_leave_req(struct mcast_group *group, u8 leave_mask,
  499. struct mcast_req *req)
  500. {
  501. u16 status;
  502. if (req->clean)
  503. leave_mask = group->func[req->func].join_state;
  504. status = check_leave(group, req->func, leave_mask);
  505. if (!status)
  506. leave_group(group, req->func, leave_mask);
  507. if (!req->clean)
  508. send_reply_to_slave(req->func, group, &req->sa_mad, status);
  509. --group->func[req->func].num_pend_reqs;
  510. list_del(&req->group_list);
  511. list_del(&req->func_list);
  512. kfree(req);
  513. return 1;
  514. }
  515. static int handle_join_req(struct mcast_group *group, u8 join_mask,
  516. struct mcast_req *req)
  517. {
  518. u8 group_join_state = group->rec.scope_join_state & 7;
  519. int ref = 0;
  520. u16 status;
  521. struct ib_sa_mcmember_data *sa_data = (struct ib_sa_mcmember_data *)req->sa_mad.data;
  522. if (join_mask == (group_join_state & join_mask)) {
  523. /* port's membership need not change */
  524. status = cmp_rec(&group->rec, sa_data, req->sa_mad.sa_hdr.comp_mask);
  525. if (!status)
  526. join_group(group, req->func, join_mask);
  527. --group->func[req->func].num_pend_reqs;
  528. send_reply_to_slave(req->func, group, &req->sa_mad, status);
  529. list_del(&req->group_list);
  530. list_del(&req->func_list);
  531. kfree(req);
  532. ++ref;
  533. } else {
  534. /* port's membership needs to be updated */
  535. group->prev_state = group->state;
  536. if (send_join_to_wire(group, &req->sa_mad)) {
  537. --group->func[req->func].num_pend_reqs;
  538. list_del(&req->group_list);
  539. list_del(&req->func_list);
  540. kfree(req);
  541. ref = 1;
  542. group->state = group->prev_state;
  543. } else
  544. group->state = MCAST_JOIN_SENT;
  545. }
  546. return ref;
  547. }
  548. static void mlx4_ib_mcg_work_handler(struct work_struct *work)
  549. {
  550. struct mcast_group *group;
  551. struct mcast_req *req = NULL;
  552. struct ib_sa_mcmember_data *sa_data;
  553. u8 req_join_state;
  554. int rc = 1; /* release_count - this is for the scheduled work */
  555. u16 status;
  556. u8 method;
  557. group = container_of(work, typeof(*group), work);
  558. mutex_lock(&group->lock);
  559. /* First, let's see if a response from SM is waiting regarding this group.
  560. * If so, we need to update the group's REC. If this is a bad response, we
  561. * may need to send a bad response to a VF waiting for it. If VF is waiting
  562. * and this is a good response, the VF will be answered later in this func. */
  563. if (group->state == MCAST_RESP_READY) {
  564. /* cancels mlx4_ib_mcg_timeout_handler */
  565. cancel_delayed_work(&group->timeout_work);
  566. status = be16_to_cpu(group->response_sa_mad.mad_hdr.status);
  567. method = group->response_sa_mad.mad_hdr.method;
  568. if (group->last_req_tid != group->response_sa_mad.mad_hdr.tid) {
  569. mcg_warn_group(group, "Got MAD response to existing MGID but wrong TID, dropping. Resp TID=%llx, group TID=%llx\n",
  570. be64_to_cpu(group->response_sa_mad.mad_hdr.tid),
  571. be64_to_cpu(group->last_req_tid));
  572. group->state = group->prev_state;
  573. goto process_requests;
  574. }
  575. if (status) {
  576. if (!list_empty(&group->pending_list))
  577. req = list_first_entry(&group->pending_list,
  578. struct mcast_req, group_list);
  579. if ((method == IB_MGMT_METHOD_GET_RESP)) {
  580. if (req) {
  581. send_reply_to_slave(req->func, group, &req->sa_mad, status);
  582. --group->func[req->func].num_pend_reqs;
  583. list_del(&req->group_list);
  584. list_del(&req->func_list);
  585. kfree(req);
  586. ++rc;
  587. } else
  588. mcg_warn_group(group, "no request for failed join\n");
  589. } else if (method == IB_SA_METHOD_DELETE_RESP && group->demux->flushing)
  590. ++rc;
  591. } else {
  592. u8 resp_join_state;
  593. u8 cur_join_state;
  594. resp_join_state = ((struct ib_sa_mcmember_data *)
  595. group->response_sa_mad.data)->scope_join_state & 7;
  596. cur_join_state = group->rec.scope_join_state & 7;
  597. if (method == IB_MGMT_METHOD_GET_RESP) {
  598. /* successfull join */
  599. if (!cur_join_state && resp_join_state)
  600. --rc;
  601. } else if (!resp_join_state)
  602. ++rc;
  603. memcpy(&group->rec, group->response_sa_mad.data, sizeof group->rec);
  604. }
  605. group->state = MCAST_IDLE;
  606. }
  607. process_requests:
  608. /* We should now go over pending join/leave requests, as long as we are idle. */
  609. while (!list_empty(&group->pending_list) && group->state == MCAST_IDLE) {
  610. req = list_first_entry(&group->pending_list, struct mcast_req,
  611. group_list);
  612. sa_data = (struct ib_sa_mcmember_data *)req->sa_mad.data;
  613. req_join_state = sa_data->scope_join_state & 0x7;
  614. /* For a leave request, we will immediately answer the VF, and
  615. * update our internal counters. The actual leave will be sent
  616. * to SM later, if at all needed. We dequeue the request now. */
  617. if (req->sa_mad.mad_hdr.method == IB_SA_METHOD_DELETE)
  618. rc += handle_leave_req(group, req_join_state, req);
  619. else
  620. rc += handle_join_req(group, req_join_state, req);
  621. }
  622. /* Handle leaves */
  623. if (group->state == MCAST_IDLE) {
  624. req_join_state = get_leave_state(group);
  625. if (req_join_state) {
  626. group->rec.scope_join_state &= ~req_join_state;
  627. group->prev_state = group->state;
  628. if (send_leave_to_wire(group, req_join_state)) {
  629. group->state = group->prev_state;
  630. ++rc;
  631. } else
  632. group->state = MCAST_LEAVE_SENT;
  633. }
  634. }
  635. if (!list_empty(&group->pending_list) && group->state == MCAST_IDLE)
  636. goto process_requests;
  637. mutex_unlock(&group->lock);
  638. while (rc--)
  639. release_group(group, 0);
  640. }
  641. static struct mcast_group *search_relocate_mgid0_group(struct mlx4_ib_demux_ctx *ctx,
  642. __be64 tid,
  643. union ib_gid *new_mgid)
  644. {
  645. struct mcast_group *group = NULL, *cur_group;
  646. struct mcast_req *req;
  647. struct list_head *pos;
  648. struct list_head *n;
  649. mutex_lock(&ctx->mcg_table_lock);
  650. list_for_each_safe(pos, n, &ctx->mcg_mgid0_list) {
  651. group = list_entry(pos, struct mcast_group, mgid0_list);
  652. mutex_lock(&group->lock);
  653. if (group->last_req_tid == tid) {
  654. if (memcmp(new_mgid, &mgid0, sizeof mgid0)) {
  655. group->rec.mgid = *new_mgid;
  656. sprintf(group->name, "%016llx%016llx",
  657. be64_to_cpu(group->rec.mgid.global.subnet_prefix),
  658. be64_to_cpu(group->rec.mgid.global.interface_id));
  659. list_del_init(&group->mgid0_list);
  660. cur_group = mcast_insert(ctx, group);
  661. if (cur_group) {
  662. /* A race between our code and SM. Silently cleaning the new one */
  663. req = list_first_entry(&group->pending_list,
  664. struct mcast_req, group_list);
  665. --group->func[req->func].num_pend_reqs;
  666. list_del(&req->group_list);
  667. list_del(&req->func_list);
  668. kfree(req);
  669. mutex_unlock(&group->lock);
  670. mutex_unlock(&ctx->mcg_table_lock);
  671. release_group(group, 0);
  672. return NULL;
  673. }
  674. atomic_inc(&group->refcount);
  675. add_sysfs_port_mcg_attr(ctx->dev, ctx->port, &group->dentry.attr);
  676. mutex_unlock(&group->lock);
  677. mutex_unlock(&ctx->mcg_table_lock);
  678. return group;
  679. } else {
  680. struct mcast_req *tmp1, *tmp2;
  681. list_del(&group->mgid0_list);
  682. if (!list_empty(&group->pending_list) && group->state != MCAST_IDLE)
  683. cancel_delayed_work_sync(&group->timeout_work);
  684. list_for_each_entry_safe(tmp1, tmp2, &group->pending_list, group_list) {
  685. list_del(&tmp1->group_list);
  686. kfree(tmp1);
  687. }
  688. mutex_unlock(&group->lock);
  689. mutex_unlock(&ctx->mcg_table_lock);
  690. kfree(group);
  691. return NULL;
  692. }
  693. }
  694. mutex_unlock(&group->lock);
  695. }
  696. mutex_unlock(&ctx->mcg_table_lock);
  697. return NULL;
  698. }
  699. static ssize_t sysfs_show_group(struct device *dev,
  700. struct device_attribute *attr, char *buf);
  701. static struct mcast_group *acquire_group(struct mlx4_ib_demux_ctx *ctx,
  702. union ib_gid *mgid, int create,
  703. gfp_t gfp_mask)
  704. {
  705. struct mcast_group *group, *cur_group;
  706. int is_mgid0;
  707. int i;
  708. is_mgid0 = !memcmp(&mgid0, mgid, sizeof mgid0);
  709. if (!is_mgid0) {
  710. group = mcast_find(ctx, mgid);
  711. if (group)
  712. goto found;
  713. }
  714. if (!create)
  715. return ERR_PTR(-ENOENT);
  716. group = kzalloc(sizeof *group, gfp_mask);
  717. if (!group)
  718. return ERR_PTR(-ENOMEM);
  719. group->demux = ctx;
  720. group->rec.mgid = *mgid;
  721. INIT_LIST_HEAD(&group->pending_list);
  722. INIT_LIST_HEAD(&group->mgid0_list);
  723. for (i = 0; i < MAX_VFS; ++i)
  724. INIT_LIST_HEAD(&group->func[i].pending);
  725. INIT_WORK(&group->work, mlx4_ib_mcg_work_handler);
  726. INIT_DELAYED_WORK(&group->timeout_work, mlx4_ib_mcg_timeout_handler);
  727. mutex_init(&group->lock);
  728. sprintf(group->name, "%016llx%016llx",
  729. be64_to_cpu(group->rec.mgid.global.subnet_prefix),
  730. be64_to_cpu(group->rec.mgid.global.interface_id));
  731. sysfs_attr_init(&group->dentry.attr);
  732. group->dentry.show = sysfs_show_group;
  733. group->dentry.store = NULL;
  734. group->dentry.attr.name = group->name;
  735. group->dentry.attr.mode = 0400;
  736. group->state = MCAST_IDLE;
  737. if (is_mgid0) {
  738. list_add(&group->mgid0_list, &ctx->mcg_mgid0_list);
  739. goto found;
  740. }
  741. cur_group = mcast_insert(ctx, group);
  742. if (cur_group) {
  743. mcg_warn("group just showed up %s - confused\n", cur_group->name);
  744. kfree(group);
  745. return ERR_PTR(-EINVAL);
  746. }
  747. add_sysfs_port_mcg_attr(ctx->dev, ctx->port, &group->dentry.attr);
  748. found:
  749. atomic_inc(&group->refcount);
  750. return group;
  751. }
  752. static void queue_req(struct mcast_req *req)
  753. {
  754. struct mcast_group *group = req->group;
  755. atomic_inc(&group->refcount); /* for the request */
  756. atomic_inc(&group->refcount); /* for scheduling the work */
  757. list_add_tail(&req->group_list, &group->pending_list);
  758. list_add_tail(&req->func_list, &group->func[req->func].pending);
  759. /* calls mlx4_ib_mcg_work_handler */
  760. if (!queue_work(group->demux->mcg_wq, &group->work))
  761. safe_atomic_dec(&group->refcount);
  762. }
  763. int mlx4_ib_mcg_demux_handler(struct ib_device *ibdev, int port, int slave,
  764. struct ib_sa_mad *mad)
  765. {
  766. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  767. struct ib_sa_mcmember_data *rec = (struct ib_sa_mcmember_data *)mad->data;
  768. struct mlx4_ib_demux_ctx *ctx = &dev->sriov.demux[port - 1];
  769. struct mcast_group *group;
  770. switch (mad->mad_hdr.method) {
  771. case IB_MGMT_METHOD_GET_RESP:
  772. case IB_SA_METHOD_DELETE_RESP:
  773. mutex_lock(&ctx->mcg_table_lock);
  774. group = acquire_group(ctx, &rec->mgid, 0, GFP_KERNEL);
  775. mutex_unlock(&ctx->mcg_table_lock);
  776. if (IS_ERR(group)) {
  777. if (mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP) {
  778. __be64 tid = mad->mad_hdr.tid;
  779. *(u8 *)(&tid) = (u8)slave; /* in group we kept the modified TID */
  780. group = search_relocate_mgid0_group(ctx, tid, &rec->mgid);
  781. } else
  782. group = NULL;
  783. }
  784. if (!group)
  785. return 1;
  786. mutex_lock(&group->lock);
  787. group->response_sa_mad = *mad;
  788. group->prev_state = group->state;
  789. group->state = MCAST_RESP_READY;
  790. /* calls mlx4_ib_mcg_work_handler */
  791. atomic_inc(&group->refcount);
  792. if (!queue_work(ctx->mcg_wq, &group->work))
  793. safe_atomic_dec(&group->refcount);
  794. mutex_unlock(&group->lock);
  795. release_group(group, 0);
  796. return 1; /* consumed */
  797. case IB_MGMT_METHOD_SET:
  798. case IB_SA_METHOD_GET_TABLE:
  799. case IB_SA_METHOD_GET_TABLE_RESP:
  800. case IB_SA_METHOD_DELETE:
  801. return 0; /* not consumed, pass-through to guest over tunnel */
  802. default:
  803. mcg_warn("In demux, port %d: unexpected MCMember method: 0x%x, dropping\n",
  804. port, mad->mad_hdr.method);
  805. return 1; /* consumed */
  806. }
  807. }
  808. int mlx4_ib_mcg_multiplex_handler(struct ib_device *ibdev, int port,
  809. int slave, struct ib_sa_mad *sa_mad)
  810. {
  811. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  812. struct ib_sa_mcmember_data *rec = (struct ib_sa_mcmember_data *)sa_mad->data;
  813. struct mlx4_ib_demux_ctx *ctx = &dev->sriov.demux[port - 1];
  814. struct mcast_group *group;
  815. struct mcast_req *req;
  816. int may_create = 0;
  817. if (ctx->flushing)
  818. return -EAGAIN;
  819. switch (sa_mad->mad_hdr.method) {
  820. case IB_MGMT_METHOD_SET:
  821. may_create = 1;
  822. case IB_SA_METHOD_DELETE:
  823. req = kzalloc(sizeof *req, GFP_KERNEL);
  824. if (!req)
  825. return -ENOMEM;
  826. req->func = slave;
  827. req->sa_mad = *sa_mad;
  828. mutex_lock(&ctx->mcg_table_lock);
  829. group = acquire_group(ctx, &rec->mgid, may_create, GFP_KERNEL);
  830. mutex_unlock(&ctx->mcg_table_lock);
  831. if (IS_ERR(group)) {
  832. kfree(req);
  833. return PTR_ERR(group);
  834. }
  835. mutex_lock(&group->lock);
  836. if (group->func[slave].num_pend_reqs > MAX_PEND_REQS_PER_FUNC) {
  837. mutex_unlock(&group->lock);
  838. mcg_warn_group(group, "Port %d, Func %d has too many pending requests (%d), dropping\n",
  839. port, slave, MAX_PEND_REQS_PER_FUNC);
  840. release_group(group, 0);
  841. kfree(req);
  842. return -ENOMEM;
  843. }
  844. ++group->func[slave].num_pend_reqs;
  845. req->group = group;
  846. queue_req(req);
  847. mutex_unlock(&group->lock);
  848. release_group(group, 0);
  849. return 1; /* consumed */
  850. case IB_SA_METHOD_GET_TABLE:
  851. case IB_MGMT_METHOD_GET_RESP:
  852. case IB_SA_METHOD_GET_TABLE_RESP:
  853. case IB_SA_METHOD_DELETE_RESP:
  854. return 0; /* not consumed, pass-through */
  855. default:
  856. mcg_warn("In multiplex, port %d, func %d: unexpected MCMember method: 0x%x, dropping\n",
  857. port, slave, sa_mad->mad_hdr.method);
  858. return 1; /* consumed */
  859. }
  860. }
  861. static ssize_t sysfs_show_group(struct device *dev,
  862. struct device_attribute *attr, char *buf)
  863. {
  864. struct mcast_group *group =
  865. container_of(attr, struct mcast_group, dentry);
  866. struct mcast_req *req = NULL;
  867. char pending_str[40];
  868. char state_str[40];
  869. ssize_t len = 0;
  870. int f;
  871. if (group->state == MCAST_IDLE)
  872. sprintf(state_str, "%s", get_state_string(group->state));
  873. else
  874. sprintf(state_str, "%s(TID=0x%llx)",
  875. get_state_string(group->state),
  876. be64_to_cpu(group->last_req_tid));
  877. if (list_empty(&group->pending_list)) {
  878. sprintf(pending_str, "No");
  879. } else {
  880. req = list_first_entry(&group->pending_list, struct mcast_req, group_list);
  881. sprintf(pending_str, "Yes(TID=0x%llx)",
  882. be64_to_cpu(req->sa_mad.mad_hdr.tid));
  883. }
  884. len += sprintf(buf + len, "%1d [%02d,%02d,%02d] %4d %4s %5s ",
  885. group->rec.scope_join_state & 0xf,
  886. group->members[2], group->members[1], group->members[0],
  887. atomic_read(&group->refcount),
  888. pending_str,
  889. state_str);
  890. for (f = 0; f < MAX_VFS; ++f)
  891. if (group->func[f].state == MCAST_MEMBER)
  892. len += sprintf(buf + len, "%d[%1x] ",
  893. f, group->func[f].join_state);
  894. len += sprintf(buf + len, "\t\t(%4hx %4x %2x %2x %2x %2x %2x "
  895. "%4x %4x %2x %2x)\n",
  896. be16_to_cpu(group->rec.pkey),
  897. be32_to_cpu(group->rec.qkey),
  898. (group->rec.mtusel_mtu & 0xc0) >> 6,
  899. group->rec.mtusel_mtu & 0x3f,
  900. group->rec.tclass,
  901. (group->rec.ratesel_rate & 0xc0) >> 6,
  902. group->rec.ratesel_rate & 0x3f,
  903. (be32_to_cpu(group->rec.sl_flowlabel_hoplimit) & 0xf0000000) >> 28,
  904. (be32_to_cpu(group->rec.sl_flowlabel_hoplimit) & 0x0fffff00) >> 8,
  905. be32_to_cpu(group->rec.sl_flowlabel_hoplimit) & 0x000000ff,
  906. group->rec.proxy_join);
  907. return len;
  908. }
  909. int mlx4_ib_mcg_port_init(struct mlx4_ib_demux_ctx *ctx)
  910. {
  911. char name[20];
  912. atomic_set(&ctx->tid, 0);
  913. sprintf(name, "mlx4_ib_mcg%d", ctx->port);
  914. ctx->mcg_wq = create_singlethread_workqueue(name);
  915. if (!ctx->mcg_wq)
  916. return -ENOMEM;
  917. mutex_init(&ctx->mcg_table_lock);
  918. ctx->mcg_table = RB_ROOT;
  919. INIT_LIST_HEAD(&ctx->mcg_mgid0_list);
  920. ctx->flushing = 0;
  921. return 0;
  922. }
  923. static void force_clean_group(struct mcast_group *group)
  924. {
  925. struct mcast_req *req, *tmp
  926. ;
  927. list_for_each_entry_safe(req, tmp, &group->pending_list, group_list) {
  928. list_del(&req->group_list);
  929. kfree(req);
  930. }
  931. del_sysfs_port_mcg_attr(group->demux->dev, group->demux->port, &group->dentry.attr);
  932. rb_erase(&group->node, &group->demux->mcg_table);
  933. kfree(group);
  934. }
  935. static void _mlx4_ib_mcg_port_cleanup(struct mlx4_ib_demux_ctx *ctx, int destroy_wq)
  936. {
  937. int i;
  938. struct rb_node *p;
  939. struct mcast_group *group;
  940. unsigned long end;
  941. int count;
  942. if (ctx->flushing)
  943. return;
  944. ctx->flushing = 1;
  945. for (i = 0; i < MAX_VFS; ++i)
  946. clean_vf_mcast(ctx, i);
  947. end = jiffies + msecs_to_jiffies(MAD_TIMEOUT_MS + 3000);
  948. do {
  949. count = 0;
  950. mutex_lock(&ctx->mcg_table_lock);
  951. for (p = rb_first(&ctx->mcg_table); p; p = rb_next(p))
  952. ++count;
  953. mutex_unlock(&ctx->mcg_table_lock);
  954. if (!count)
  955. break;
  956. msleep(1);
  957. } while (time_after(end, jiffies));
  958. flush_workqueue(ctx->mcg_wq);
  959. if (destroy_wq)
  960. destroy_workqueue(ctx->mcg_wq);
  961. mutex_lock(&ctx->mcg_table_lock);
  962. while ((p = rb_first(&ctx->mcg_table)) != NULL) {
  963. group = rb_entry(p, struct mcast_group, node);
  964. if (atomic_read(&group->refcount))
  965. mcg_warn_group(group, "group refcount %d!!! (pointer %p)\n", atomic_read(&group->refcount), group);
  966. force_clean_group(group);
  967. }
  968. mutex_unlock(&ctx->mcg_table_lock);
  969. if (!destroy_wq)
  970. ctx->flushing = 0;
  971. }
  972. struct clean_work {
  973. struct work_struct work;
  974. struct mlx4_ib_demux_ctx *ctx;
  975. int destroy_wq;
  976. };
  977. static void mcg_clean_task(struct work_struct *work)
  978. {
  979. struct clean_work *cw = container_of(work, struct clean_work, work);
  980. _mlx4_ib_mcg_port_cleanup(cw->ctx, cw->destroy_wq);
  981. kfree(cw);
  982. }
  983. void mlx4_ib_mcg_port_cleanup(struct mlx4_ib_demux_ctx *ctx, int destroy_wq)
  984. {
  985. struct clean_work *work;
  986. if (destroy_wq) {
  987. _mlx4_ib_mcg_port_cleanup(ctx, destroy_wq);
  988. return;
  989. }
  990. work = kmalloc(sizeof *work, GFP_KERNEL);
  991. if (!work) {
  992. mcg_warn("failed allocating work for cleanup\n");
  993. return;
  994. }
  995. work->ctx = ctx;
  996. work->destroy_wq = destroy_wq;
  997. INIT_WORK(&work->work, mcg_clean_task);
  998. queue_work(clean_wq, &work->work);
  999. }
  1000. static void build_leave_mad(struct mcast_req *req)
  1001. {
  1002. struct ib_sa_mad *mad = &req->sa_mad;
  1003. mad->mad_hdr.method = IB_SA_METHOD_DELETE;
  1004. }
  1005. static void clear_pending_reqs(struct mcast_group *group, int vf)
  1006. {
  1007. struct mcast_req *req, *tmp, *group_first = NULL;
  1008. int clear;
  1009. int pend = 0;
  1010. if (!list_empty(&group->pending_list))
  1011. group_first = list_first_entry(&group->pending_list, struct mcast_req, group_list);
  1012. list_for_each_entry_safe(req, tmp, &group->func[vf].pending, func_list) {
  1013. clear = 1;
  1014. if (group_first == req &&
  1015. (group->state == MCAST_JOIN_SENT ||
  1016. group->state == MCAST_LEAVE_SENT)) {
  1017. clear = cancel_delayed_work(&group->timeout_work);
  1018. pend = !clear;
  1019. group->state = MCAST_IDLE;
  1020. }
  1021. if (clear) {
  1022. --group->func[vf].num_pend_reqs;
  1023. list_del(&req->group_list);
  1024. list_del(&req->func_list);
  1025. kfree(req);
  1026. atomic_dec(&group->refcount);
  1027. }
  1028. }
  1029. if (!pend && (!list_empty(&group->func[vf].pending) || group->func[vf].num_pend_reqs)) {
  1030. mcg_warn_group(group, "DRIVER BUG: list_empty %d, num_pend_reqs %d\n",
  1031. list_empty(&group->func[vf].pending), group->func[vf].num_pend_reqs);
  1032. }
  1033. }
  1034. static int push_deleteing_req(struct mcast_group *group, int slave)
  1035. {
  1036. struct mcast_req *req;
  1037. struct mcast_req *pend_req;
  1038. if (!group->func[slave].join_state)
  1039. return 0;
  1040. req = kzalloc(sizeof *req, GFP_KERNEL);
  1041. if (!req) {
  1042. mcg_warn_group(group, "failed allocation - may leave stall groups\n");
  1043. return -ENOMEM;
  1044. }
  1045. if (!list_empty(&group->func[slave].pending)) {
  1046. pend_req = list_entry(group->func[slave].pending.prev, struct mcast_req, group_list);
  1047. if (pend_req->clean) {
  1048. kfree(req);
  1049. return 0;
  1050. }
  1051. }
  1052. req->clean = 1;
  1053. req->func = slave;
  1054. req->group = group;
  1055. ++group->func[slave].num_pend_reqs;
  1056. build_leave_mad(req);
  1057. queue_req(req);
  1058. return 0;
  1059. }
  1060. void clean_vf_mcast(struct mlx4_ib_demux_ctx *ctx, int slave)
  1061. {
  1062. struct mcast_group *group;
  1063. struct rb_node *p;
  1064. mutex_lock(&ctx->mcg_table_lock);
  1065. for (p = rb_first(&ctx->mcg_table); p; p = rb_next(p)) {
  1066. group = rb_entry(p, struct mcast_group, node);
  1067. mutex_lock(&group->lock);
  1068. if (atomic_read(&group->refcount)) {
  1069. /* clear pending requests of this VF */
  1070. clear_pending_reqs(group, slave);
  1071. push_deleteing_req(group, slave);
  1072. }
  1073. mutex_unlock(&group->lock);
  1074. }
  1075. mutex_unlock(&ctx->mcg_table_lock);
  1076. }
  1077. int mlx4_ib_mcg_init(void)
  1078. {
  1079. clean_wq = create_singlethread_workqueue("mlx4_ib_mcg");
  1080. if (!clean_wq)
  1081. return -ENOMEM;
  1082. return 0;
  1083. }
  1084. void mlx4_ib_mcg_destroy(void)
  1085. {
  1086. destroy_workqueue(clean_wq);
  1087. }