mcg.c 35 KB

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