mcg.c 35 KB

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