mcg.c 35 KB

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