mad.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. /*
  2. * Copyright (c) 2007 Cisco Systems, Inc. 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 <linux/mlx4/cmd.h>
  35. #include <linux/gfp.h>
  36. #include <rdma/ib_pma.h>
  37. #include "mlx4_ib.h"
  38. enum {
  39. MLX4_IB_VENDOR_CLASS1 = 0x9,
  40. MLX4_IB_VENDOR_CLASS2 = 0xa
  41. };
  42. #define MLX4_TUN_SEND_WRID_SHIFT 34
  43. #define MLX4_TUN_QPN_SHIFT 32
  44. #define MLX4_TUN_WRID_RECV (((u64) 1) << MLX4_TUN_SEND_WRID_SHIFT)
  45. #define MLX4_TUN_SET_WRID_QPN(a) (((u64) ((a) & 0x3)) << MLX4_TUN_QPN_SHIFT)
  46. #define MLX4_TUN_IS_RECV(a) (((a) >> MLX4_TUN_SEND_WRID_SHIFT) & 0x1)
  47. #define MLX4_TUN_WRID_QPN(a) (((a) >> MLX4_TUN_QPN_SHIFT) & 0x3)
  48. struct mlx4_mad_rcv_buf {
  49. struct ib_grh grh;
  50. u8 payload[256];
  51. } __packed;
  52. struct mlx4_mad_snd_buf {
  53. u8 payload[256];
  54. } __packed;
  55. struct mlx4_tunnel_mad {
  56. struct ib_grh grh;
  57. struct mlx4_ib_tunnel_header hdr;
  58. struct ib_mad mad;
  59. } __packed;
  60. struct mlx4_rcv_tunnel_mad {
  61. struct mlx4_rcv_tunnel_hdr hdr;
  62. struct ib_grh grh;
  63. struct ib_mad mad;
  64. } __packed;
  65. int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int ignore_mkey, int ignore_bkey,
  66. int port, struct ib_wc *in_wc, struct ib_grh *in_grh,
  67. void *in_mad, void *response_mad)
  68. {
  69. struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
  70. void *inbox;
  71. int err;
  72. u32 in_modifier = port;
  73. u8 op_modifier = 0;
  74. inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  75. if (IS_ERR(inmailbox))
  76. return PTR_ERR(inmailbox);
  77. inbox = inmailbox->buf;
  78. outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  79. if (IS_ERR(outmailbox)) {
  80. mlx4_free_cmd_mailbox(dev->dev, inmailbox);
  81. return PTR_ERR(outmailbox);
  82. }
  83. memcpy(inbox, in_mad, 256);
  84. /*
  85. * Key check traps can't be generated unless we have in_wc to
  86. * tell us where to send the trap.
  87. */
  88. if (ignore_mkey || !in_wc)
  89. op_modifier |= 0x1;
  90. if (ignore_bkey || !in_wc)
  91. op_modifier |= 0x2;
  92. if (in_wc) {
  93. struct {
  94. __be32 my_qpn;
  95. u32 reserved1;
  96. __be32 rqpn;
  97. u8 sl;
  98. u8 g_path;
  99. u16 reserved2[2];
  100. __be16 pkey;
  101. u32 reserved3[11];
  102. u8 grh[40];
  103. } *ext_info;
  104. memset(inbox + 256, 0, 256);
  105. ext_info = inbox + 256;
  106. ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
  107. ext_info->rqpn = cpu_to_be32(in_wc->src_qp);
  108. ext_info->sl = in_wc->sl << 4;
  109. ext_info->g_path = in_wc->dlid_path_bits |
  110. (in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
  111. ext_info->pkey = cpu_to_be16(in_wc->pkey_index);
  112. if (in_grh)
  113. memcpy(ext_info->grh, in_grh, 40);
  114. op_modifier |= 0x4;
  115. in_modifier |= in_wc->slid << 16;
  116. }
  117. err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma,
  118. in_modifier, op_modifier,
  119. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
  120. MLX4_CMD_NATIVE);
  121. if (!err)
  122. memcpy(response_mad, outmailbox->buf, 256);
  123. mlx4_free_cmd_mailbox(dev->dev, inmailbox);
  124. mlx4_free_cmd_mailbox(dev->dev, outmailbox);
  125. return err;
  126. }
  127. static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
  128. {
  129. struct ib_ah *new_ah;
  130. struct ib_ah_attr ah_attr;
  131. unsigned long flags;
  132. if (!dev->send_agent[port_num - 1][0])
  133. return;
  134. memset(&ah_attr, 0, sizeof ah_attr);
  135. ah_attr.dlid = lid;
  136. ah_attr.sl = sl;
  137. ah_attr.port_num = port_num;
  138. new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
  139. &ah_attr);
  140. if (IS_ERR(new_ah))
  141. return;
  142. spin_lock_irqsave(&dev->sm_lock, flags);
  143. if (dev->sm_ah[port_num - 1])
  144. ib_destroy_ah(dev->sm_ah[port_num - 1]);
  145. dev->sm_ah[port_num - 1] = new_ah;
  146. spin_unlock_irqrestore(&dev->sm_lock, flags);
  147. }
  148. /*
  149. * Snoop SM MADs for port info, GUID info, and P_Key table sets, so we can
  150. * synthesize LID change, Client-Rereg, GID change, and P_Key change events.
  151. */
  152. static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,
  153. u16 prev_lid)
  154. {
  155. struct ib_port_info *pinfo;
  156. u16 lid;
  157. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  158. if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  159. mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
  160. mad->mad_hdr.method == IB_MGMT_METHOD_SET)
  161. switch (mad->mad_hdr.attr_id) {
  162. case IB_SMP_ATTR_PORT_INFO:
  163. pinfo = (struct ib_port_info *) ((struct ib_smp *) mad)->data;
  164. lid = be16_to_cpu(pinfo->lid);
  165. update_sm_ah(dev, port_num,
  166. be16_to_cpu(pinfo->sm_lid),
  167. pinfo->neighbormtu_mastersmsl & 0xf);
  168. if (pinfo->clientrereg_resv_subnetto & 0x80)
  169. mlx4_ib_dispatch_event(dev, port_num,
  170. IB_EVENT_CLIENT_REREGISTER);
  171. if (prev_lid != lid)
  172. mlx4_ib_dispatch_event(dev, port_num,
  173. IB_EVENT_LID_CHANGE);
  174. break;
  175. case IB_SMP_ATTR_PKEY_TABLE:
  176. mlx4_ib_dispatch_event(dev, port_num,
  177. IB_EVENT_PKEY_CHANGE);
  178. break;
  179. case IB_SMP_ATTR_GUID_INFO:
  180. /* paravirtualized master's guid is guid 0 -- does not change */
  181. if (!mlx4_is_master(dev->dev))
  182. mlx4_ib_dispatch_event(dev, port_num,
  183. IB_EVENT_GID_CHANGE);
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. static void node_desc_override(struct ib_device *dev,
  190. struct ib_mad *mad)
  191. {
  192. unsigned long flags;
  193. if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  194. mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
  195. mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
  196. mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
  197. spin_lock_irqsave(&to_mdev(dev)->sm_lock, flags);
  198. memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
  199. spin_unlock_irqrestore(&to_mdev(dev)->sm_lock, flags);
  200. }
  201. }
  202. static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, struct ib_mad *mad)
  203. {
  204. int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
  205. struct ib_mad_send_buf *send_buf;
  206. struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
  207. int ret;
  208. unsigned long flags;
  209. if (agent) {
  210. send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
  211. IB_MGMT_MAD_DATA, GFP_ATOMIC);
  212. if (IS_ERR(send_buf))
  213. return;
  214. /*
  215. * We rely here on the fact that MLX QPs don't use the
  216. * address handle after the send is posted (this is
  217. * wrong following the IB spec strictly, but we know
  218. * it's OK for our devices).
  219. */
  220. spin_lock_irqsave(&dev->sm_lock, flags);
  221. memcpy(send_buf->mad, mad, sizeof *mad);
  222. if ((send_buf->ah = dev->sm_ah[port_num - 1]))
  223. ret = ib_post_send_mad(send_buf, NULL);
  224. else
  225. ret = -EINVAL;
  226. spin_unlock_irqrestore(&dev->sm_lock, flags);
  227. if (ret)
  228. ib_free_send_mad(send_buf);
  229. }
  230. }
  231. static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  232. struct ib_wc *in_wc, struct ib_grh *in_grh,
  233. struct ib_mad *in_mad, struct ib_mad *out_mad)
  234. {
  235. u16 slid, prev_lid = 0;
  236. int err;
  237. struct ib_port_attr pattr;
  238. if (in_wc && in_wc->qp->qp_num) {
  239. pr_debug("received MAD: slid:%d sqpn:%d "
  240. "dlid_bits:%d dqpn:%d wc_flags:0x%x, cls %x, mtd %x, atr %x\n",
  241. in_wc->slid, in_wc->src_qp,
  242. in_wc->dlid_path_bits,
  243. in_wc->qp->qp_num,
  244. in_wc->wc_flags,
  245. in_mad->mad_hdr.mgmt_class, in_mad->mad_hdr.method,
  246. be16_to_cpu(in_mad->mad_hdr.attr_id));
  247. if (in_wc->wc_flags & IB_WC_GRH) {
  248. pr_debug("sgid_hi:0x%016llx sgid_lo:0x%016llx\n",
  249. be64_to_cpu(in_grh->sgid.global.subnet_prefix),
  250. be64_to_cpu(in_grh->sgid.global.interface_id));
  251. pr_debug("dgid_hi:0x%016llx dgid_lo:0x%016llx\n",
  252. be64_to_cpu(in_grh->dgid.global.subnet_prefix),
  253. be64_to_cpu(in_grh->dgid.global.interface_id));
  254. }
  255. }
  256. slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
  257. if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
  258. forward_trap(to_mdev(ibdev), port_num, in_mad);
  259. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  260. }
  261. if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  262. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  263. if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
  264. in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&
  265. in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
  266. return IB_MAD_RESULT_SUCCESS;
  267. /*
  268. * Don't process SMInfo queries -- the SMA can't handle them.
  269. */
  270. if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
  271. return IB_MAD_RESULT_SUCCESS;
  272. } else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
  273. in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1 ||
  274. in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2 ||
  275. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
  276. if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
  277. in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)
  278. return IB_MAD_RESULT_SUCCESS;
  279. } else
  280. return IB_MAD_RESULT_SUCCESS;
  281. if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  282. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
  283. in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
  284. in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
  285. !ib_query_port(ibdev, port_num, &pattr))
  286. prev_lid = pattr.lid;
  287. err = mlx4_MAD_IFC(to_mdev(ibdev),
  288. mad_flags & IB_MAD_IGNORE_MKEY,
  289. mad_flags & IB_MAD_IGNORE_BKEY,
  290. port_num, in_wc, in_grh, in_mad, out_mad);
  291. if (err)
  292. return IB_MAD_RESULT_FAILURE;
  293. if (!out_mad->mad_hdr.status) {
  294. if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_PORT_MNG_CHG_EV))
  295. smp_snoop(ibdev, port_num, in_mad, prev_lid);
  296. node_desc_override(ibdev, out_mad);
  297. }
  298. /* set return bit in status of directed route responses */
  299. if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  300. out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
  301. if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
  302. /* no response for trap repress */
  303. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  304. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  305. }
  306. static void edit_counter(struct mlx4_counter *cnt,
  307. struct ib_pma_portcounters *pma_cnt)
  308. {
  309. pma_cnt->port_xmit_data = cpu_to_be32((be64_to_cpu(cnt->tx_bytes)>>2));
  310. pma_cnt->port_rcv_data = cpu_to_be32((be64_to_cpu(cnt->rx_bytes)>>2));
  311. pma_cnt->port_xmit_packets = cpu_to_be32(be64_to_cpu(cnt->tx_frames));
  312. pma_cnt->port_rcv_packets = cpu_to_be32(be64_to_cpu(cnt->rx_frames));
  313. }
  314. static int iboe_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  315. struct ib_wc *in_wc, struct ib_grh *in_grh,
  316. struct ib_mad *in_mad, struct ib_mad *out_mad)
  317. {
  318. struct mlx4_cmd_mailbox *mailbox;
  319. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  320. int err;
  321. u32 inmod = dev->counters[port_num - 1] & 0xffff;
  322. u8 mode;
  323. if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_PERF_MGMT)
  324. return -EINVAL;
  325. mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  326. if (IS_ERR(mailbox))
  327. return IB_MAD_RESULT_FAILURE;
  328. err = mlx4_cmd_box(dev->dev, 0, mailbox->dma, inmod, 0,
  329. MLX4_CMD_QUERY_IF_STAT, MLX4_CMD_TIME_CLASS_C,
  330. MLX4_CMD_WRAPPED);
  331. if (err)
  332. err = IB_MAD_RESULT_FAILURE;
  333. else {
  334. memset(out_mad->data, 0, sizeof out_mad->data);
  335. mode = ((struct mlx4_counter *)mailbox->buf)->counter_mode;
  336. switch (mode & 0xf) {
  337. case 0:
  338. edit_counter(mailbox->buf,
  339. (void *)(out_mad->data + 40));
  340. err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  341. break;
  342. default:
  343. err = IB_MAD_RESULT_FAILURE;
  344. }
  345. }
  346. mlx4_free_cmd_mailbox(dev->dev, mailbox);
  347. return err;
  348. }
  349. int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  350. struct ib_wc *in_wc, struct ib_grh *in_grh,
  351. struct ib_mad *in_mad, struct ib_mad *out_mad)
  352. {
  353. switch (rdma_port_get_link_layer(ibdev, port_num)) {
  354. case IB_LINK_LAYER_INFINIBAND:
  355. return ib_process_mad(ibdev, mad_flags, port_num, in_wc,
  356. in_grh, in_mad, out_mad);
  357. case IB_LINK_LAYER_ETHERNET:
  358. return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
  359. in_grh, in_mad, out_mad);
  360. default:
  361. return -EINVAL;
  362. }
  363. }
  364. static void send_handler(struct ib_mad_agent *agent,
  365. struct ib_mad_send_wc *mad_send_wc)
  366. {
  367. ib_free_send_mad(mad_send_wc->send_buf);
  368. }
  369. int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
  370. {
  371. struct ib_mad_agent *agent;
  372. int p, q;
  373. int ret;
  374. enum rdma_link_layer ll;
  375. for (p = 0; p < dev->num_ports; ++p) {
  376. ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);
  377. for (q = 0; q <= 1; ++q) {
  378. if (ll == IB_LINK_LAYER_INFINIBAND) {
  379. agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
  380. q ? IB_QPT_GSI : IB_QPT_SMI,
  381. NULL, 0, send_handler,
  382. NULL, NULL);
  383. if (IS_ERR(agent)) {
  384. ret = PTR_ERR(agent);
  385. goto err;
  386. }
  387. dev->send_agent[p][q] = agent;
  388. } else
  389. dev->send_agent[p][q] = NULL;
  390. }
  391. }
  392. return 0;
  393. err:
  394. for (p = 0; p < dev->num_ports; ++p)
  395. for (q = 0; q <= 1; ++q)
  396. if (dev->send_agent[p][q])
  397. ib_unregister_mad_agent(dev->send_agent[p][q]);
  398. return ret;
  399. }
  400. void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
  401. {
  402. struct ib_mad_agent *agent;
  403. int p, q;
  404. for (p = 0; p < dev->num_ports; ++p) {
  405. for (q = 0; q <= 1; ++q) {
  406. agent = dev->send_agent[p][q];
  407. if (agent) {
  408. dev->send_agent[p][q] = NULL;
  409. ib_unregister_mad_agent(agent);
  410. }
  411. }
  412. if (dev->sm_ah[p])
  413. ib_destroy_ah(dev->sm_ah[p]);
  414. }
  415. }
  416. void handle_port_mgmt_change_event(struct work_struct *work)
  417. {
  418. struct ib_event_work *ew = container_of(work, struct ib_event_work, work);
  419. struct mlx4_ib_dev *dev = ew->ib_dev;
  420. struct mlx4_eqe *eqe = &(ew->ib_eqe);
  421. u8 port = eqe->event.port_mgmt_change.port;
  422. u32 changed_attr;
  423. switch (eqe->subtype) {
  424. case MLX4_DEV_PMC_SUBTYPE_PORT_INFO:
  425. changed_attr = be32_to_cpu(eqe->event.port_mgmt_change.params.port_info.changed_attr);
  426. /* Update the SM ah - This should be done before handling
  427. the other changed attributes so that MADs can be sent to the SM */
  428. if (changed_attr & MSTR_SM_CHANGE_MASK) {
  429. u16 lid = be16_to_cpu(eqe->event.port_mgmt_change.params.port_info.mstr_sm_lid);
  430. u8 sl = eqe->event.port_mgmt_change.params.port_info.mstr_sm_sl & 0xf;
  431. update_sm_ah(dev, port, lid, sl);
  432. }
  433. /* Check if it is a lid change event */
  434. if (changed_attr & MLX4_EQ_PORT_INFO_LID_CHANGE_MASK)
  435. mlx4_ib_dispatch_event(dev, port, IB_EVENT_LID_CHANGE);
  436. /* Generate GUID changed event */
  437. if (changed_attr & MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK)
  438. mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
  439. if (changed_attr & MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK)
  440. mlx4_ib_dispatch_event(dev, port,
  441. IB_EVENT_CLIENT_REREGISTER);
  442. break;
  443. case MLX4_DEV_PMC_SUBTYPE_PKEY_TABLE:
  444. mlx4_ib_dispatch_event(dev, port, IB_EVENT_PKEY_CHANGE);
  445. break;
  446. case MLX4_DEV_PMC_SUBTYPE_GUID_INFO:
  447. /* paravirtualized master's guid is guid 0 -- does not change */
  448. if (!mlx4_is_master(dev->dev))
  449. mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
  450. break;
  451. default:
  452. pr_warn("Unsupported subtype 0x%x for "
  453. "Port Management Change event\n", eqe->subtype);
  454. }
  455. kfree(ew);
  456. }
  457. void mlx4_ib_dispatch_event(struct mlx4_ib_dev *dev, u8 port_num,
  458. enum ib_event_type type)
  459. {
  460. struct ib_event event;
  461. event.device = &dev->ib_dev;
  462. event.element.port_num = port_num;
  463. event.event = type;
  464. ib_dispatch_event(&event);
  465. }
  466. static void mlx4_ib_tunnel_comp_handler(struct ib_cq *cq, void *arg)
  467. {
  468. unsigned long flags;
  469. struct mlx4_ib_demux_pv_ctx *ctx = cq->cq_context;
  470. struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
  471. spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
  472. if (!dev->sriov.is_going_down && ctx->state == DEMUX_PV_STATE_ACTIVE)
  473. queue_work(ctx->wq, &ctx->work);
  474. spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
  475. }
  476. static int mlx4_ib_post_pv_qp_buf(struct mlx4_ib_demux_pv_ctx *ctx,
  477. struct mlx4_ib_demux_pv_qp *tun_qp,
  478. int index)
  479. {
  480. struct ib_sge sg_list;
  481. struct ib_recv_wr recv_wr, *bad_recv_wr;
  482. int size;
  483. size = (tun_qp->qp->qp_type == IB_QPT_UD) ?
  484. sizeof (struct mlx4_tunnel_mad) : sizeof (struct mlx4_mad_rcv_buf);
  485. sg_list.addr = tun_qp->ring[index].map;
  486. sg_list.length = size;
  487. sg_list.lkey = ctx->mr->lkey;
  488. recv_wr.next = NULL;
  489. recv_wr.sg_list = &sg_list;
  490. recv_wr.num_sge = 1;
  491. recv_wr.wr_id = (u64) index | MLX4_TUN_WRID_RECV |
  492. MLX4_TUN_SET_WRID_QPN(tun_qp->proxy_qpt);
  493. ib_dma_sync_single_for_device(ctx->ib_dev, tun_qp->ring[index].map,
  494. size, DMA_FROM_DEVICE);
  495. return ib_post_recv(tun_qp->qp, &recv_wr, &bad_recv_wr);
  496. }
  497. static int mlx4_ib_alloc_pv_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
  498. enum ib_qp_type qp_type, int is_tun)
  499. {
  500. int i;
  501. struct mlx4_ib_demux_pv_qp *tun_qp;
  502. int rx_buf_size, tx_buf_size;
  503. if (qp_type > IB_QPT_GSI)
  504. return -EINVAL;
  505. tun_qp = &ctx->qp[qp_type];
  506. tun_qp->ring = kzalloc(sizeof (struct mlx4_ib_buf) * MLX4_NUM_TUNNEL_BUFS,
  507. GFP_KERNEL);
  508. if (!tun_qp->ring)
  509. return -ENOMEM;
  510. tun_qp->tx_ring = kcalloc(MLX4_NUM_TUNNEL_BUFS,
  511. sizeof (struct mlx4_ib_tun_tx_buf),
  512. GFP_KERNEL);
  513. if (!tun_qp->tx_ring) {
  514. kfree(tun_qp->ring);
  515. tun_qp->ring = NULL;
  516. return -ENOMEM;
  517. }
  518. if (is_tun) {
  519. rx_buf_size = sizeof (struct mlx4_tunnel_mad);
  520. tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
  521. } else {
  522. rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
  523. tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
  524. }
  525. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  526. tun_qp->ring[i].addr = kmalloc(rx_buf_size, GFP_KERNEL);
  527. if (!tun_qp->ring[i].addr)
  528. goto err;
  529. tun_qp->ring[i].map = ib_dma_map_single(ctx->ib_dev,
  530. tun_qp->ring[i].addr,
  531. rx_buf_size,
  532. DMA_FROM_DEVICE);
  533. }
  534. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  535. tun_qp->tx_ring[i].buf.addr =
  536. kmalloc(tx_buf_size, GFP_KERNEL);
  537. if (!tun_qp->tx_ring[i].buf.addr)
  538. goto tx_err;
  539. tun_qp->tx_ring[i].buf.map =
  540. ib_dma_map_single(ctx->ib_dev,
  541. tun_qp->tx_ring[i].buf.addr,
  542. tx_buf_size,
  543. DMA_TO_DEVICE);
  544. tun_qp->tx_ring[i].ah = NULL;
  545. }
  546. spin_lock_init(&tun_qp->tx_lock);
  547. tun_qp->tx_ix_head = 0;
  548. tun_qp->tx_ix_tail = 0;
  549. tun_qp->proxy_qpt = qp_type;
  550. return 0;
  551. tx_err:
  552. while (i > 0) {
  553. --i;
  554. ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
  555. tx_buf_size, DMA_TO_DEVICE);
  556. kfree(tun_qp->tx_ring[i].buf.addr);
  557. }
  558. kfree(tun_qp->tx_ring);
  559. tun_qp->tx_ring = NULL;
  560. i = MLX4_NUM_TUNNEL_BUFS;
  561. err:
  562. while (i > 0) {
  563. --i;
  564. ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
  565. rx_buf_size, DMA_FROM_DEVICE);
  566. kfree(tun_qp->ring[i].addr);
  567. }
  568. kfree(tun_qp->ring);
  569. tun_qp->ring = NULL;
  570. return -ENOMEM;
  571. }
  572. static void mlx4_ib_free_pv_qp_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
  573. enum ib_qp_type qp_type, int is_tun)
  574. {
  575. int i;
  576. struct mlx4_ib_demux_pv_qp *tun_qp;
  577. int rx_buf_size, tx_buf_size;
  578. if (qp_type > IB_QPT_GSI)
  579. return;
  580. tun_qp = &ctx->qp[qp_type];
  581. if (is_tun) {
  582. rx_buf_size = sizeof (struct mlx4_tunnel_mad);
  583. tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
  584. } else {
  585. rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
  586. tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
  587. }
  588. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  589. ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
  590. rx_buf_size, DMA_FROM_DEVICE);
  591. kfree(tun_qp->ring[i].addr);
  592. }
  593. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  594. ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
  595. tx_buf_size, DMA_TO_DEVICE);
  596. kfree(tun_qp->tx_ring[i].buf.addr);
  597. if (tun_qp->tx_ring[i].ah)
  598. ib_destroy_ah(tun_qp->tx_ring[i].ah);
  599. }
  600. kfree(tun_qp->tx_ring);
  601. kfree(tun_qp->ring);
  602. }
  603. static void mlx4_ib_tunnel_comp_worker(struct work_struct *work)
  604. {
  605. /* dummy until next patch in series */
  606. }
  607. static void pv_qp_event_handler(struct ib_event *event, void *qp_context)
  608. {
  609. struct mlx4_ib_demux_pv_ctx *sqp = qp_context;
  610. /* It's worse than that! He's dead, Jim! */
  611. pr_err("Fatal error (%d) on a MAD QP on port %d\n",
  612. event->event, sqp->port);
  613. }
  614. static int create_pv_sqp(struct mlx4_ib_demux_pv_ctx *ctx,
  615. enum ib_qp_type qp_type, int create_tun)
  616. {
  617. int i, ret;
  618. struct mlx4_ib_demux_pv_qp *tun_qp;
  619. struct mlx4_ib_qp_tunnel_init_attr qp_init_attr;
  620. struct ib_qp_attr attr;
  621. int qp_attr_mask_INIT;
  622. if (qp_type > IB_QPT_GSI)
  623. return -EINVAL;
  624. tun_qp = &ctx->qp[qp_type];
  625. memset(&qp_init_attr, 0, sizeof qp_init_attr);
  626. qp_init_attr.init_attr.send_cq = ctx->cq;
  627. qp_init_attr.init_attr.recv_cq = ctx->cq;
  628. qp_init_attr.init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
  629. qp_init_attr.init_attr.cap.max_send_wr = MLX4_NUM_TUNNEL_BUFS;
  630. qp_init_attr.init_attr.cap.max_recv_wr = MLX4_NUM_TUNNEL_BUFS;
  631. qp_init_attr.init_attr.cap.max_send_sge = 1;
  632. qp_init_attr.init_attr.cap.max_recv_sge = 1;
  633. if (create_tun) {
  634. qp_init_attr.init_attr.qp_type = IB_QPT_UD;
  635. qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_TUNNEL_QP;
  636. qp_init_attr.port = ctx->port;
  637. qp_init_attr.slave = ctx->slave;
  638. qp_init_attr.proxy_qp_type = qp_type;
  639. qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX |
  640. IB_QP_QKEY | IB_QP_PORT;
  641. } else {
  642. qp_init_attr.init_attr.qp_type = qp_type;
  643. qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_SQP;
  644. qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_QKEY;
  645. }
  646. qp_init_attr.init_attr.port_num = ctx->port;
  647. qp_init_attr.init_attr.qp_context = ctx;
  648. qp_init_attr.init_attr.event_handler = pv_qp_event_handler;
  649. tun_qp->qp = ib_create_qp(ctx->pd, &qp_init_attr.init_attr);
  650. if (IS_ERR(tun_qp->qp)) {
  651. ret = PTR_ERR(tun_qp->qp);
  652. tun_qp->qp = NULL;
  653. pr_err("Couldn't create %s QP (%d)\n",
  654. create_tun ? "tunnel" : "special", ret);
  655. return ret;
  656. }
  657. memset(&attr, 0, sizeof attr);
  658. attr.qp_state = IB_QPS_INIT;
  659. attr.pkey_index =
  660. to_mdev(ctx->ib_dev)->pkeys.virt2phys_pkey[ctx->slave][ctx->port - 1][0];
  661. attr.qkey = IB_QP1_QKEY;
  662. attr.port_num = ctx->port;
  663. ret = ib_modify_qp(tun_qp->qp, &attr, qp_attr_mask_INIT);
  664. if (ret) {
  665. pr_err("Couldn't change %s qp state to INIT (%d)\n",
  666. create_tun ? "tunnel" : "special", ret);
  667. goto err_qp;
  668. }
  669. attr.qp_state = IB_QPS_RTR;
  670. ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE);
  671. if (ret) {
  672. pr_err("Couldn't change %s qp state to RTR (%d)\n",
  673. create_tun ? "tunnel" : "special", ret);
  674. goto err_qp;
  675. }
  676. attr.qp_state = IB_QPS_RTS;
  677. attr.sq_psn = 0;
  678. ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE | IB_QP_SQ_PSN);
  679. if (ret) {
  680. pr_err("Couldn't change %s qp state to RTS (%d)\n",
  681. create_tun ? "tunnel" : "special", ret);
  682. goto err_qp;
  683. }
  684. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  685. ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp, i);
  686. if (ret) {
  687. pr_err(" mlx4_ib_post_pv_buf error"
  688. " (err = %d, i = %d)\n", ret, i);
  689. goto err_qp;
  690. }
  691. }
  692. return 0;
  693. err_qp:
  694. ib_destroy_qp(tun_qp->qp);
  695. tun_qp->qp = NULL;
  696. return ret;
  697. }
  698. /*
  699. * IB MAD completion callback for real SQPs
  700. */
  701. static void mlx4_ib_sqp_comp_worker(struct work_struct *work)
  702. {
  703. /* dummy until next patch in series */
  704. }
  705. static int alloc_pv_object(struct mlx4_ib_dev *dev, int slave, int port,
  706. struct mlx4_ib_demux_pv_ctx **ret_ctx)
  707. {
  708. struct mlx4_ib_demux_pv_ctx *ctx;
  709. *ret_ctx = NULL;
  710. ctx = kzalloc(sizeof (struct mlx4_ib_demux_pv_ctx), GFP_KERNEL);
  711. if (!ctx) {
  712. pr_err("failed allocating pv resource context "
  713. "for port %d, slave %d\n", port, slave);
  714. return -ENOMEM;
  715. }
  716. ctx->ib_dev = &dev->ib_dev;
  717. ctx->port = port;
  718. ctx->slave = slave;
  719. *ret_ctx = ctx;
  720. return 0;
  721. }
  722. static void free_pv_object(struct mlx4_ib_dev *dev, int slave, int port)
  723. {
  724. if (dev->sriov.demux[port - 1].tun[slave]) {
  725. kfree(dev->sriov.demux[port - 1].tun[slave]);
  726. dev->sriov.demux[port - 1].tun[slave] = NULL;
  727. }
  728. }
  729. static int create_pv_resources(struct ib_device *ibdev, int slave, int port,
  730. int create_tun, struct mlx4_ib_demux_pv_ctx *ctx)
  731. {
  732. int ret, cq_size;
  733. ctx->state = DEMUX_PV_STATE_STARTING;
  734. /* have QP0 only on port owner, and only if link layer is IB */
  735. if (ctx->slave == mlx4_master_func_num(to_mdev(ctx->ib_dev)->dev) &&
  736. rdma_port_get_link_layer(ibdev, ctx->port) == IB_LINK_LAYER_INFINIBAND)
  737. ctx->has_smi = 1;
  738. if (ctx->has_smi) {
  739. ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_SMI, create_tun);
  740. if (ret) {
  741. pr_err("Failed allocating qp0 tunnel bufs (%d)\n", ret);
  742. goto err_out;
  743. }
  744. }
  745. ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_GSI, create_tun);
  746. if (ret) {
  747. pr_err("Failed allocating qp1 tunnel bufs (%d)\n", ret);
  748. goto err_out_qp0;
  749. }
  750. cq_size = 2 * MLX4_NUM_TUNNEL_BUFS;
  751. if (ctx->has_smi)
  752. cq_size *= 2;
  753. ctx->cq = ib_create_cq(ctx->ib_dev, mlx4_ib_tunnel_comp_handler,
  754. NULL, ctx, cq_size, 0);
  755. if (IS_ERR(ctx->cq)) {
  756. ret = PTR_ERR(ctx->cq);
  757. pr_err("Couldn't create tunnel CQ (%d)\n", ret);
  758. goto err_buf;
  759. }
  760. ctx->pd = ib_alloc_pd(ctx->ib_dev);
  761. if (IS_ERR(ctx->pd)) {
  762. ret = PTR_ERR(ctx->pd);
  763. pr_err("Couldn't create tunnel PD (%d)\n", ret);
  764. goto err_cq;
  765. }
  766. ctx->mr = ib_get_dma_mr(ctx->pd, IB_ACCESS_LOCAL_WRITE);
  767. if (IS_ERR(ctx->mr)) {
  768. ret = PTR_ERR(ctx->mr);
  769. pr_err("Couldn't get tunnel DMA MR (%d)\n", ret);
  770. goto err_pd;
  771. }
  772. if (ctx->has_smi) {
  773. ret = create_pv_sqp(ctx, IB_QPT_SMI, create_tun);
  774. if (ret) {
  775. pr_err("Couldn't create %s QP0 (%d)\n",
  776. create_tun ? "tunnel for" : "", ret);
  777. goto err_mr;
  778. }
  779. }
  780. ret = create_pv_sqp(ctx, IB_QPT_GSI, create_tun);
  781. if (ret) {
  782. pr_err("Couldn't create %s QP1 (%d)\n",
  783. create_tun ? "tunnel for" : "", ret);
  784. goto err_qp0;
  785. }
  786. if (create_tun)
  787. INIT_WORK(&ctx->work, mlx4_ib_tunnel_comp_worker);
  788. else
  789. INIT_WORK(&ctx->work, mlx4_ib_sqp_comp_worker);
  790. ctx->wq = to_mdev(ibdev)->sriov.demux[port - 1].wq;
  791. ret = ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
  792. if (ret) {
  793. pr_err("Couldn't arm tunnel cq (%d)\n", ret);
  794. goto err_wq;
  795. }
  796. ctx->state = DEMUX_PV_STATE_ACTIVE;
  797. return 0;
  798. err_wq:
  799. ctx->wq = NULL;
  800. ib_destroy_qp(ctx->qp[1].qp);
  801. ctx->qp[1].qp = NULL;
  802. err_qp0:
  803. if (ctx->has_smi)
  804. ib_destroy_qp(ctx->qp[0].qp);
  805. ctx->qp[0].qp = NULL;
  806. err_mr:
  807. ib_dereg_mr(ctx->mr);
  808. ctx->mr = NULL;
  809. err_pd:
  810. ib_dealloc_pd(ctx->pd);
  811. ctx->pd = NULL;
  812. err_cq:
  813. ib_destroy_cq(ctx->cq);
  814. ctx->cq = NULL;
  815. err_buf:
  816. mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, create_tun);
  817. err_out_qp0:
  818. if (ctx->has_smi)
  819. mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, create_tun);
  820. err_out:
  821. ctx->state = DEMUX_PV_STATE_DOWN;
  822. return ret;
  823. }
  824. static void destroy_pv_resources(struct mlx4_ib_dev *dev, int slave, int port,
  825. struct mlx4_ib_demux_pv_ctx *ctx, int flush)
  826. {
  827. if (!ctx)
  828. return;
  829. if (ctx->state > DEMUX_PV_STATE_DOWN) {
  830. ctx->state = DEMUX_PV_STATE_DOWNING;
  831. if (flush)
  832. flush_workqueue(ctx->wq);
  833. if (ctx->has_smi) {
  834. ib_destroy_qp(ctx->qp[0].qp);
  835. ctx->qp[0].qp = NULL;
  836. mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, 1);
  837. }
  838. ib_destroy_qp(ctx->qp[1].qp);
  839. ctx->qp[1].qp = NULL;
  840. mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, 1);
  841. ib_dereg_mr(ctx->mr);
  842. ctx->mr = NULL;
  843. ib_dealloc_pd(ctx->pd);
  844. ctx->pd = NULL;
  845. ib_destroy_cq(ctx->cq);
  846. ctx->cq = NULL;
  847. ctx->state = DEMUX_PV_STATE_DOWN;
  848. }
  849. }
  850. static int mlx4_ib_tunnels_update(struct mlx4_ib_dev *dev, int slave,
  851. int port, int do_init)
  852. {
  853. int ret = 0;
  854. if (!do_init) {
  855. /* for master, destroy real sqp resources */
  856. if (slave == mlx4_master_func_num(dev->dev))
  857. destroy_pv_resources(dev, slave, port,
  858. dev->sriov.sqps[port - 1], 1);
  859. /* destroy the tunnel qp resources */
  860. destroy_pv_resources(dev, slave, port,
  861. dev->sriov.demux[port - 1].tun[slave], 1);
  862. return 0;
  863. }
  864. /* create the tunnel qp resources */
  865. ret = create_pv_resources(&dev->ib_dev, slave, port, 1,
  866. dev->sriov.demux[port - 1].tun[slave]);
  867. /* for master, create the real sqp resources */
  868. if (!ret && slave == mlx4_master_func_num(dev->dev))
  869. ret = create_pv_resources(&dev->ib_dev, slave, port, 0,
  870. dev->sriov.sqps[port - 1]);
  871. return ret;
  872. }
  873. void mlx4_ib_tunnels_update_work(struct work_struct *work)
  874. {
  875. struct mlx4_ib_demux_work *dmxw;
  876. dmxw = container_of(work, struct mlx4_ib_demux_work, work);
  877. mlx4_ib_tunnels_update(dmxw->dev, dmxw->slave, (int) dmxw->port,
  878. dmxw->do_init);
  879. kfree(dmxw);
  880. return;
  881. }
  882. static int mlx4_ib_alloc_demux_ctx(struct mlx4_ib_dev *dev,
  883. struct mlx4_ib_demux_ctx *ctx,
  884. int port)
  885. {
  886. char name[12];
  887. int ret = 0;
  888. int i;
  889. ctx->tun = kcalloc(dev->dev->caps.sqp_demux,
  890. sizeof (struct mlx4_ib_demux_pv_ctx *), GFP_KERNEL);
  891. if (!ctx->tun)
  892. return -ENOMEM;
  893. ctx->dev = dev;
  894. ctx->port = port;
  895. ctx->ib_dev = &dev->ib_dev;
  896. for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
  897. ret = alloc_pv_object(dev, i, port, &ctx->tun[i]);
  898. if (ret) {
  899. ret = -ENOMEM;
  900. goto err_wq;
  901. }
  902. }
  903. snprintf(name, sizeof name, "mlx4_ibt%d", port);
  904. ctx->wq = create_singlethread_workqueue(name);
  905. if (!ctx->wq) {
  906. pr_err("Failed to create tunnelling WQ for port %d\n", port);
  907. ret = -ENOMEM;
  908. goto err_wq;
  909. }
  910. snprintf(name, sizeof name, "mlx4_ibud%d", port);
  911. ctx->ud_wq = create_singlethread_workqueue(name);
  912. if (!ctx->ud_wq) {
  913. pr_err("Failed to create up/down WQ for port %d\n", port);
  914. ret = -ENOMEM;
  915. goto err_udwq;
  916. }
  917. return 0;
  918. err_udwq:
  919. destroy_workqueue(ctx->wq);
  920. ctx->wq = NULL;
  921. err_wq:
  922. for (i = 0; i < dev->dev->caps.sqp_demux; i++)
  923. free_pv_object(dev, i, port);
  924. kfree(ctx->tun);
  925. ctx->tun = NULL;
  926. return ret;
  927. }
  928. static void mlx4_ib_free_sqp_ctx(struct mlx4_ib_demux_pv_ctx *sqp_ctx)
  929. {
  930. if (sqp_ctx->state > DEMUX_PV_STATE_DOWN) {
  931. sqp_ctx->state = DEMUX_PV_STATE_DOWNING;
  932. flush_workqueue(sqp_ctx->wq);
  933. if (sqp_ctx->has_smi) {
  934. ib_destroy_qp(sqp_ctx->qp[0].qp);
  935. sqp_ctx->qp[0].qp = NULL;
  936. mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_SMI, 0);
  937. }
  938. ib_destroy_qp(sqp_ctx->qp[1].qp);
  939. sqp_ctx->qp[1].qp = NULL;
  940. mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_GSI, 0);
  941. ib_dereg_mr(sqp_ctx->mr);
  942. sqp_ctx->mr = NULL;
  943. ib_dealloc_pd(sqp_ctx->pd);
  944. sqp_ctx->pd = NULL;
  945. ib_destroy_cq(sqp_ctx->cq);
  946. sqp_ctx->cq = NULL;
  947. sqp_ctx->state = DEMUX_PV_STATE_DOWN;
  948. }
  949. }
  950. static void mlx4_ib_free_demux_ctx(struct mlx4_ib_demux_ctx *ctx)
  951. {
  952. int i;
  953. if (ctx) {
  954. struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
  955. for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
  956. if (!ctx->tun[i])
  957. continue;
  958. if (ctx->tun[i]->state > DEMUX_PV_STATE_DOWN)
  959. ctx->tun[i]->state = DEMUX_PV_STATE_DOWNING;
  960. }
  961. flush_workqueue(ctx->wq);
  962. for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
  963. destroy_pv_resources(dev, i, ctx->port, ctx->tun[i], 0);
  964. free_pv_object(dev, i, ctx->port);
  965. }
  966. kfree(ctx->tun);
  967. destroy_workqueue(ctx->ud_wq);
  968. destroy_workqueue(ctx->wq);
  969. }
  970. }
  971. static void mlx4_ib_master_tunnels(struct mlx4_ib_dev *dev, int do_init)
  972. {
  973. int i;
  974. if (!mlx4_is_master(dev->dev))
  975. return;
  976. /* initialize or tear down tunnel QPs for the master */
  977. for (i = 0; i < dev->dev->caps.num_ports; i++)
  978. mlx4_ib_tunnels_update(dev, mlx4_master_func_num(dev->dev), i + 1, do_init);
  979. return;
  980. }
  981. int mlx4_ib_init_sriov(struct mlx4_ib_dev *dev)
  982. {
  983. int i = 0;
  984. int err;
  985. if (!mlx4_is_mfunc(dev->dev))
  986. return 0;
  987. dev->sriov.is_going_down = 0;
  988. spin_lock_init(&dev->sriov.going_down_lock);
  989. mlx4_ib_warn(&dev->ib_dev, "multi-function enabled\n");
  990. if (mlx4_is_slave(dev->dev)) {
  991. mlx4_ib_warn(&dev->ib_dev, "operating in qp1 tunnel mode\n");
  992. return 0;
  993. }
  994. mlx4_ib_warn(&dev->ib_dev, "initializing demux service for %d qp1 clients\n",
  995. dev->dev->caps.sqp_demux);
  996. for (i = 0; i < dev->num_ports; i++) {
  997. err = alloc_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1,
  998. &dev->sriov.sqps[i]);
  999. if (err)
  1000. goto demux_err;
  1001. err = mlx4_ib_alloc_demux_ctx(dev, &dev->sriov.demux[i], i + 1);
  1002. if (err)
  1003. goto demux_err;
  1004. }
  1005. mlx4_ib_master_tunnels(dev, 1);
  1006. return 0;
  1007. demux_err:
  1008. while (i > 0) {
  1009. free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
  1010. mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
  1011. --i;
  1012. }
  1013. return err;
  1014. }
  1015. void mlx4_ib_close_sriov(struct mlx4_ib_dev *dev)
  1016. {
  1017. int i;
  1018. unsigned long flags;
  1019. if (!mlx4_is_mfunc(dev->dev))
  1020. return;
  1021. spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
  1022. dev->sriov.is_going_down = 1;
  1023. spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
  1024. if (mlx4_is_master(dev->dev))
  1025. for (i = 0; i < dev->num_ports; i++) {
  1026. flush_workqueue(dev->sriov.demux[i].ud_wq);
  1027. mlx4_ib_free_sqp_ctx(dev->sriov.sqps[i]);
  1028. kfree(dev->sriov.sqps[i]);
  1029. dev->sriov.sqps[i] = NULL;
  1030. mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
  1031. }
  1032. }