mad.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258
  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 <rdma/ib_sa.h>
  35. #include <rdma/ib_cache.h>
  36. #include <linux/random.h>
  37. #include <linux/mlx4/cmd.h>
  38. #include <linux/gfp.h>
  39. #include <rdma/ib_pma.h>
  40. #include <linux/mlx4/driver.h>
  41. #include "mlx4_ib.h"
  42. enum {
  43. MLX4_IB_VENDOR_CLASS1 = 0x9,
  44. MLX4_IB_VENDOR_CLASS2 = 0xa
  45. };
  46. #define MLX4_TUN_SEND_WRID_SHIFT 34
  47. #define MLX4_TUN_QPN_SHIFT 32
  48. #define MLX4_TUN_WRID_RECV (((u64) 1) << MLX4_TUN_SEND_WRID_SHIFT)
  49. #define MLX4_TUN_SET_WRID_QPN(a) (((u64) ((a) & 0x3)) << MLX4_TUN_QPN_SHIFT)
  50. #define MLX4_TUN_IS_RECV(a) (((a) >> MLX4_TUN_SEND_WRID_SHIFT) & 0x1)
  51. #define MLX4_TUN_WRID_QPN(a) (((a) >> MLX4_TUN_QPN_SHIFT) & 0x3)
  52. /* Port mgmt change event handling */
  53. #define GET_BLK_PTR_FROM_EQE(eqe) be32_to_cpu(eqe->event.port_mgmt_change.params.tbl_change_info.block_ptr)
  54. #define GET_MASK_FROM_EQE(eqe) be32_to_cpu(eqe->event.port_mgmt_change.params.tbl_change_info.tbl_entries_mask)
  55. #define NUM_IDX_IN_PKEY_TBL_BLK 32
  56. #define GUID_TBL_ENTRY_SIZE 8 /* size in bytes */
  57. #define GUID_TBL_BLK_NUM_ENTRIES 8
  58. #define GUID_TBL_BLK_SIZE (GUID_TBL_ENTRY_SIZE * GUID_TBL_BLK_NUM_ENTRIES)
  59. struct mlx4_mad_rcv_buf {
  60. struct ib_grh grh;
  61. u8 payload[256];
  62. } __packed;
  63. struct mlx4_mad_snd_buf {
  64. u8 payload[256];
  65. } __packed;
  66. struct mlx4_tunnel_mad {
  67. struct ib_grh grh;
  68. struct mlx4_ib_tunnel_header hdr;
  69. struct ib_mad mad;
  70. } __packed;
  71. struct mlx4_rcv_tunnel_mad {
  72. struct mlx4_rcv_tunnel_hdr hdr;
  73. struct ib_grh grh;
  74. struct ib_mad mad;
  75. } __packed;
  76. static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num);
  77. static void handle_lid_change_event(struct mlx4_ib_dev *dev, u8 port_num);
  78. static void __propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
  79. int block, u32 change_bitmap);
  80. __be64 mlx4_ib_gen_node_guid(void)
  81. {
  82. #define NODE_GUID_HI ((u64) (((u64)IB_OPENIB_OUI) << 40))
  83. return cpu_to_be64(NODE_GUID_HI | prandom_u32());
  84. }
  85. __be64 mlx4_ib_get_new_demux_tid(struct mlx4_ib_demux_ctx *ctx)
  86. {
  87. return cpu_to_be64(atomic_inc_return(&ctx->tid)) |
  88. cpu_to_be64(0xff00000000000000LL);
  89. }
  90. int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int mad_ifc_flags,
  91. int port, const struct ib_wc *in_wc,
  92. const struct ib_grh *in_grh,
  93. const void *in_mad, void *response_mad)
  94. {
  95. struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
  96. void *inbox;
  97. int err;
  98. u32 in_modifier = port;
  99. u8 op_modifier = 0;
  100. inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  101. if (IS_ERR(inmailbox))
  102. return PTR_ERR(inmailbox);
  103. inbox = inmailbox->buf;
  104. outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
  105. if (IS_ERR(outmailbox)) {
  106. mlx4_free_cmd_mailbox(dev->dev, inmailbox);
  107. return PTR_ERR(outmailbox);
  108. }
  109. memcpy(inbox, in_mad, 256);
  110. /*
  111. * Key check traps can't be generated unless we have in_wc to
  112. * tell us where to send the trap.
  113. */
  114. if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_MKEY) || !in_wc)
  115. op_modifier |= 0x1;
  116. if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_BKEY) || !in_wc)
  117. op_modifier |= 0x2;
  118. if (mlx4_is_mfunc(dev->dev) &&
  119. (mad_ifc_flags & MLX4_MAD_IFC_NET_VIEW || in_wc))
  120. op_modifier |= 0x8;
  121. if (in_wc) {
  122. struct {
  123. __be32 my_qpn;
  124. u32 reserved1;
  125. __be32 rqpn;
  126. u8 sl;
  127. u8 g_path;
  128. u16 reserved2[2];
  129. __be16 pkey;
  130. u32 reserved3[11];
  131. u8 grh[40];
  132. } *ext_info;
  133. memset(inbox + 256, 0, 256);
  134. ext_info = inbox + 256;
  135. ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
  136. ext_info->rqpn = cpu_to_be32(in_wc->src_qp);
  137. ext_info->sl = in_wc->sl << 4;
  138. ext_info->g_path = in_wc->dlid_path_bits |
  139. (in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
  140. ext_info->pkey = cpu_to_be16(in_wc->pkey_index);
  141. if (in_grh)
  142. memcpy(ext_info->grh, in_grh, 40);
  143. op_modifier |= 0x4;
  144. in_modifier |= in_wc->slid << 16;
  145. }
  146. err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma, in_modifier,
  147. mlx4_is_master(dev->dev) ? (op_modifier & ~0x8) : op_modifier,
  148. MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
  149. (op_modifier & 0x8) ? MLX4_CMD_NATIVE : MLX4_CMD_WRAPPED);
  150. if (!err)
  151. memcpy(response_mad, outmailbox->buf, 256);
  152. mlx4_free_cmd_mailbox(dev->dev, inmailbox);
  153. mlx4_free_cmd_mailbox(dev->dev, outmailbox);
  154. return err;
  155. }
  156. static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
  157. {
  158. struct ib_ah *new_ah;
  159. struct ib_ah_attr ah_attr;
  160. unsigned long flags;
  161. if (!dev->send_agent[port_num - 1][0])
  162. return;
  163. memset(&ah_attr, 0, sizeof ah_attr);
  164. ah_attr.dlid = lid;
  165. ah_attr.sl = sl;
  166. ah_attr.port_num = port_num;
  167. new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
  168. &ah_attr);
  169. if (IS_ERR(new_ah))
  170. return;
  171. spin_lock_irqsave(&dev->sm_lock, flags);
  172. if (dev->sm_ah[port_num - 1])
  173. ib_destroy_ah(dev->sm_ah[port_num - 1]);
  174. dev->sm_ah[port_num - 1] = new_ah;
  175. spin_unlock_irqrestore(&dev->sm_lock, flags);
  176. }
  177. /*
  178. * Snoop SM MADs for port info, GUID info, and P_Key table sets, so we can
  179. * synthesize LID change, Client-Rereg, GID change, and P_Key change events.
  180. */
  181. static void smp_snoop(struct ib_device *ibdev, u8 port_num, const struct ib_mad *mad,
  182. u16 prev_lid)
  183. {
  184. struct ib_port_info *pinfo;
  185. u16 lid;
  186. __be16 *base;
  187. u32 bn, pkey_change_bitmap;
  188. int i;
  189. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  190. if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  191. mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
  192. mad->mad_hdr.method == IB_MGMT_METHOD_SET)
  193. switch (mad->mad_hdr.attr_id) {
  194. case IB_SMP_ATTR_PORT_INFO:
  195. pinfo = (struct ib_port_info *) ((struct ib_smp *) mad)->data;
  196. lid = be16_to_cpu(pinfo->lid);
  197. update_sm_ah(dev, port_num,
  198. be16_to_cpu(pinfo->sm_lid),
  199. pinfo->neighbormtu_mastersmsl & 0xf);
  200. if (pinfo->clientrereg_resv_subnetto & 0x80)
  201. handle_client_rereg_event(dev, port_num);
  202. if (prev_lid != lid)
  203. handle_lid_change_event(dev, port_num);
  204. break;
  205. case IB_SMP_ATTR_PKEY_TABLE:
  206. if (!mlx4_is_mfunc(dev->dev)) {
  207. mlx4_ib_dispatch_event(dev, port_num,
  208. IB_EVENT_PKEY_CHANGE);
  209. break;
  210. }
  211. /* at this point, we are running in the master.
  212. * Slaves do not receive SMPs.
  213. */
  214. bn = be32_to_cpu(((struct ib_smp *)mad)->attr_mod) & 0xFFFF;
  215. base = (__be16 *) &(((struct ib_smp *)mad)->data[0]);
  216. pkey_change_bitmap = 0;
  217. for (i = 0; i < 32; i++) {
  218. pr_debug("PKEY[%d] = x%x\n",
  219. i + bn*32, be16_to_cpu(base[i]));
  220. if (be16_to_cpu(base[i]) !=
  221. dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32]) {
  222. pkey_change_bitmap |= (1 << i);
  223. dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32] =
  224. be16_to_cpu(base[i]);
  225. }
  226. }
  227. pr_debug("PKEY Change event: port=%d, "
  228. "block=0x%x, change_bitmap=0x%x\n",
  229. port_num, bn, pkey_change_bitmap);
  230. if (pkey_change_bitmap) {
  231. mlx4_ib_dispatch_event(dev, port_num,
  232. IB_EVENT_PKEY_CHANGE);
  233. if (!dev->sriov.is_going_down)
  234. __propagate_pkey_ev(dev, port_num, bn,
  235. pkey_change_bitmap);
  236. }
  237. break;
  238. case IB_SMP_ATTR_GUID_INFO:
  239. /* paravirtualized master's guid is guid 0 -- does not change */
  240. if (!mlx4_is_master(dev->dev))
  241. mlx4_ib_dispatch_event(dev, port_num,
  242. IB_EVENT_GID_CHANGE);
  243. /*if master, notify relevant slaves*/
  244. if (mlx4_is_master(dev->dev) &&
  245. !dev->sriov.is_going_down) {
  246. bn = be32_to_cpu(((struct ib_smp *)mad)->attr_mod);
  247. mlx4_ib_update_cache_on_guid_change(dev, bn, port_num,
  248. (u8 *)(&((struct ib_smp *)mad)->data));
  249. mlx4_ib_notify_slaves_on_guid_change(dev, bn, port_num,
  250. (u8 *)(&((struct ib_smp *)mad)->data));
  251. }
  252. break;
  253. default:
  254. break;
  255. }
  256. }
  257. static void __propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
  258. int block, u32 change_bitmap)
  259. {
  260. int i, ix, slave, err;
  261. int have_event = 0;
  262. for (slave = 0; slave < dev->dev->caps.sqp_demux; slave++) {
  263. if (slave == mlx4_master_func_num(dev->dev))
  264. continue;
  265. if (!mlx4_is_slave_active(dev->dev, slave))
  266. continue;
  267. have_event = 0;
  268. for (i = 0; i < 32; i++) {
  269. if (!(change_bitmap & (1 << i)))
  270. continue;
  271. for (ix = 0;
  272. ix < dev->dev->caps.pkey_table_len[port_num]; ix++) {
  273. if (dev->pkeys.virt2phys_pkey[slave][port_num - 1]
  274. [ix] == i + 32 * block) {
  275. err = mlx4_gen_pkey_eqe(dev->dev, slave, port_num);
  276. pr_debug("propagate_pkey_ev: slave %d,"
  277. " port %d, ix %d (%d)\n",
  278. slave, port_num, ix, err);
  279. have_event = 1;
  280. break;
  281. }
  282. }
  283. if (have_event)
  284. break;
  285. }
  286. }
  287. }
  288. static void node_desc_override(struct ib_device *dev,
  289. struct ib_mad *mad)
  290. {
  291. unsigned long flags;
  292. if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  293. mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
  294. mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
  295. mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
  296. spin_lock_irqsave(&to_mdev(dev)->sm_lock, flags);
  297. memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
  298. spin_unlock_irqrestore(&to_mdev(dev)->sm_lock, flags);
  299. }
  300. }
  301. static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, const struct ib_mad *mad)
  302. {
  303. int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
  304. struct ib_mad_send_buf *send_buf;
  305. struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
  306. int ret;
  307. unsigned long flags;
  308. if (agent) {
  309. send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
  310. IB_MGMT_MAD_DATA, GFP_ATOMIC,
  311. IB_MGMT_BASE_VERSION);
  312. if (IS_ERR(send_buf))
  313. return;
  314. /*
  315. * We rely here on the fact that MLX QPs don't use the
  316. * address handle after the send is posted (this is
  317. * wrong following the IB spec strictly, but we know
  318. * it's OK for our devices).
  319. */
  320. spin_lock_irqsave(&dev->sm_lock, flags);
  321. memcpy(send_buf->mad, mad, sizeof *mad);
  322. if ((send_buf->ah = dev->sm_ah[port_num - 1]))
  323. ret = ib_post_send_mad(send_buf, NULL);
  324. else
  325. ret = -EINVAL;
  326. spin_unlock_irqrestore(&dev->sm_lock, flags);
  327. if (ret)
  328. ib_free_send_mad(send_buf);
  329. }
  330. }
  331. static int mlx4_ib_demux_sa_handler(struct ib_device *ibdev, int port, int slave,
  332. struct ib_sa_mad *sa_mad)
  333. {
  334. int ret = 0;
  335. /* dispatch to different sa handlers */
  336. switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
  337. case IB_SA_ATTR_MC_MEMBER_REC:
  338. ret = mlx4_ib_mcg_demux_handler(ibdev, port, slave, sa_mad);
  339. break;
  340. default:
  341. break;
  342. }
  343. return ret;
  344. }
  345. int mlx4_ib_find_real_gid(struct ib_device *ibdev, u8 port, __be64 guid)
  346. {
  347. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  348. int i;
  349. for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
  350. if (dev->sriov.demux[port - 1].guid_cache[i] == guid)
  351. return i;
  352. }
  353. return -1;
  354. }
  355. static int find_slave_port_pkey_ix(struct mlx4_ib_dev *dev, int slave,
  356. u8 port, u16 pkey, u16 *ix)
  357. {
  358. int i, ret;
  359. u8 unassigned_pkey_ix, pkey_ix, partial_ix = 0xFF;
  360. u16 slot_pkey;
  361. if (slave == mlx4_master_func_num(dev->dev))
  362. return ib_find_cached_pkey(&dev->ib_dev, port, pkey, ix);
  363. unassigned_pkey_ix = dev->dev->phys_caps.pkey_phys_table_len[port] - 1;
  364. for (i = 0; i < dev->dev->caps.pkey_table_len[port]; i++) {
  365. if (dev->pkeys.virt2phys_pkey[slave][port - 1][i] == unassigned_pkey_ix)
  366. continue;
  367. pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][i];
  368. ret = ib_get_cached_pkey(&dev->ib_dev, port, pkey_ix, &slot_pkey);
  369. if (ret)
  370. continue;
  371. if ((slot_pkey & 0x7FFF) == (pkey & 0x7FFF)) {
  372. if (slot_pkey & 0x8000) {
  373. *ix = (u16) pkey_ix;
  374. return 0;
  375. } else {
  376. /* take first partial pkey index found */
  377. if (partial_ix == 0xFF)
  378. partial_ix = pkey_ix;
  379. }
  380. }
  381. }
  382. if (partial_ix < 0xFF) {
  383. *ix = (u16) partial_ix;
  384. return 0;
  385. }
  386. return -EINVAL;
  387. }
  388. int mlx4_ib_send_to_slave(struct mlx4_ib_dev *dev, int slave, u8 port,
  389. enum ib_qp_type dest_qpt, struct ib_wc *wc,
  390. struct ib_grh *grh, struct ib_mad *mad)
  391. {
  392. struct ib_sge list;
  393. struct ib_ud_wr wr;
  394. struct ib_send_wr *bad_wr;
  395. struct mlx4_ib_demux_pv_ctx *tun_ctx;
  396. struct mlx4_ib_demux_pv_qp *tun_qp;
  397. struct mlx4_rcv_tunnel_mad *tun_mad;
  398. struct ib_ah_attr attr;
  399. struct ib_ah *ah;
  400. struct ib_qp *src_qp = NULL;
  401. unsigned tun_tx_ix = 0;
  402. int dqpn;
  403. int ret = 0;
  404. u16 tun_pkey_ix;
  405. u16 cached_pkey;
  406. u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
  407. if (dest_qpt > IB_QPT_GSI)
  408. return -EINVAL;
  409. tun_ctx = dev->sriov.demux[port-1].tun[slave];
  410. /* check if proxy qp created */
  411. if (!tun_ctx || tun_ctx->state != DEMUX_PV_STATE_ACTIVE)
  412. return -EAGAIN;
  413. if (!dest_qpt)
  414. tun_qp = &tun_ctx->qp[0];
  415. else
  416. tun_qp = &tun_ctx->qp[1];
  417. /* compute P_Key index to put in tunnel header for slave */
  418. if (dest_qpt) {
  419. u16 pkey_ix;
  420. ret = ib_get_cached_pkey(&dev->ib_dev, port, wc->pkey_index, &cached_pkey);
  421. if (ret)
  422. return -EINVAL;
  423. ret = find_slave_port_pkey_ix(dev, slave, port, cached_pkey, &pkey_ix);
  424. if (ret)
  425. return -EINVAL;
  426. tun_pkey_ix = pkey_ix;
  427. } else
  428. tun_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
  429. dqpn = dev->dev->phys_caps.base_proxy_sqpn + 8 * slave + port + (dest_qpt * 2) - 1;
  430. /* get tunnel tx data buf for slave */
  431. src_qp = tun_qp->qp;
  432. /* create ah. Just need an empty one with the port num for the post send.
  433. * The driver will set the force loopback bit in post_send */
  434. memset(&attr, 0, sizeof attr);
  435. attr.port_num = port;
  436. if (is_eth) {
  437. memcpy(&attr.grh.dgid.raw[0], &grh->dgid.raw[0], 16);
  438. attr.ah_flags = IB_AH_GRH;
  439. }
  440. ah = ib_create_ah(tun_ctx->pd, &attr);
  441. if (IS_ERR(ah))
  442. return -ENOMEM;
  443. /* allocate tunnel tx buf after pass failure returns */
  444. spin_lock(&tun_qp->tx_lock);
  445. if (tun_qp->tx_ix_head - tun_qp->tx_ix_tail >=
  446. (MLX4_NUM_TUNNEL_BUFS - 1))
  447. ret = -EAGAIN;
  448. else
  449. tun_tx_ix = (++tun_qp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
  450. spin_unlock(&tun_qp->tx_lock);
  451. if (ret)
  452. goto end;
  453. tun_mad = (struct mlx4_rcv_tunnel_mad *) (tun_qp->tx_ring[tun_tx_ix].buf.addr);
  454. if (tun_qp->tx_ring[tun_tx_ix].ah)
  455. ib_destroy_ah(tun_qp->tx_ring[tun_tx_ix].ah);
  456. tun_qp->tx_ring[tun_tx_ix].ah = ah;
  457. ib_dma_sync_single_for_cpu(&dev->ib_dev,
  458. tun_qp->tx_ring[tun_tx_ix].buf.map,
  459. sizeof (struct mlx4_rcv_tunnel_mad),
  460. DMA_TO_DEVICE);
  461. /* copy over to tunnel buffer */
  462. if (grh)
  463. memcpy(&tun_mad->grh, grh, sizeof *grh);
  464. memcpy(&tun_mad->mad, mad, sizeof *mad);
  465. /* adjust tunnel data */
  466. tun_mad->hdr.pkey_index = cpu_to_be16(tun_pkey_ix);
  467. tun_mad->hdr.flags_src_qp = cpu_to_be32(wc->src_qp & 0xFFFFFF);
  468. tun_mad->hdr.g_ml_path = (grh && (wc->wc_flags & IB_WC_GRH)) ? 0x80 : 0;
  469. if (is_eth) {
  470. u16 vlan = 0;
  471. if (mlx4_get_slave_default_vlan(dev->dev, port, slave, &vlan,
  472. NULL)) {
  473. /* VST mode */
  474. if (vlan != wc->vlan_id)
  475. /* Packet vlan is not the VST-assigned vlan.
  476. * Drop the packet.
  477. */
  478. goto out;
  479. else
  480. /* Remove the vlan tag before forwarding
  481. * the packet to the VF.
  482. */
  483. vlan = 0xffff;
  484. } else {
  485. vlan = wc->vlan_id;
  486. }
  487. tun_mad->hdr.sl_vid = cpu_to_be16(vlan);
  488. memcpy((char *)&tun_mad->hdr.mac_31_0, &(wc->smac[0]), 4);
  489. memcpy((char *)&tun_mad->hdr.slid_mac_47_32, &(wc->smac[4]), 2);
  490. } else {
  491. tun_mad->hdr.sl_vid = cpu_to_be16(((u16)(wc->sl)) << 12);
  492. tun_mad->hdr.slid_mac_47_32 = cpu_to_be16(wc->slid);
  493. }
  494. ib_dma_sync_single_for_device(&dev->ib_dev,
  495. tun_qp->tx_ring[tun_tx_ix].buf.map,
  496. sizeof (struct mlx4_rcv_tunnel_mad),
  497. DMA_TO_DEVICE);
  498. list.addr = tun_qp->tx_ring[tun_tx_ix].buf.map;
  499. list.length = sizeof (struct mlx4_rcv_tunnel_mad);
  500. list.lkey = tun_ctx->pd->local_dma_lkey;
  501. wr.ah = ah;
  502. wr.port_num = port;
  503. wr.remote_qkey = IB_QP_SET_QKEY;
  504. wr.remote_qpn = dqpn;
  505. wr.wr.next = NULL;
  506. wr.wr.wr_id = ((u64) tun_tx_ix) | MLX4_TUN_SET_WRID_QPN(dest_qpt);
  507. wr.wr.sg_list = &list;
  508. wr.wr.num_sge = 1;
  509. wr.wr.opcode = IB_WR_SEND;
  510. wr.wr.send_flags = IB_SEND_SIGNALED;
  511. ret = ib_post_send(src_qp, &wr.wr, &bad_wr);
  512. if (!ret)
  513. return 0;
  514. out:
  515. spin_lock(&tun_qp->tx_lock);
  516. tun_qp->tx_ix_tail++;
  517. spin_unlock(&tun_qp->tx_lock);
  518. tun_qp->tx_ring[tun_tx_ix].ah = NULL;
  519. end:
  520. ib_destroy_ah(ah);
  521. return ret;
  522. }
  523. static int mlx4_ib_demux_mad(struct ib_device *ibdev, u8 port,
  524. struct ib_wc *wc, struct ib_grh *grh,
  525. struct ib_mad *mad)
  526. {
  527. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  528. int err, other_port;
  529. int slave = -1;
  530. u8 *slave_id;
  531. int is_eth = 0;
  532. if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
  533. is_eth = 0;
  534. else
  535. is_eth = 1;
  536. if (is_eth) {
  537. if (!(wc->wc_flags & IB_WC_GRH)) {
  538. mlx4_ib_warn(ibdev, "RoCE grh not present.\n");
  539. return -EINVAL;
  540. }
  541. if (mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_CM) {
  542. mlx4_ib_warn(ibdev, "RoCE mgmt class is not CM\n");
  543. return -EINVAL;
  544. }
  545. err = mlx4_get_slave_from_roce_gid(dev->dev, port, grh->dgid.raw, &slave);
  546. if (err && mlx4_is_mf_bonded(dev->dev)) {
  547. other_port = (port == 1) ? 2 : 1;
  548. err = mlx4_get_slave_from_roce_gid(dev->dev, other_port, grh->dgid.raw, &slave);
  549. if (!err) {
  550. port = other_port;
  551. pr_debug("resolved slave %d from gid %pI6 wire port %d other %d\n",
  552. slave, grh->dgid.raw, port, other_port);
  553. }
  554. }
  555. if (err) {
  556. mlx4_ib_warn(ibdev, "failed matching grh\n");
  557. return -ENOENT;
  558. }
  559. if (slave >= dev->dev->caps.sqp_demux) {
  560. mlx4_ib_warn(ibdev, "slave id: %d is bigger than allowed:%d\n",
  561. slave, dev->dev->caps.sqp_demux);
  562. return -ENOENT;
  563. }
  564. if (mlx4_ib_demux_cm_handler(ibdev, port, NULL, mad))
  565. return 0;
  566. err = mlx4_ib_send_to_slave(dev, slave, port, wc->qp->qp_type, wc, grh, mad);
  567. if (err)
  568. pr_debug("failed sending to slave %d via tunnel qp (%d)\n",
  569. slave, err);
  570. return 0;
  571. }
  572. /* Initially assume that this mad is for us */
  573. slave = mlx4_master_func_num(dev->dev);
  574. /* See if the slave id is encoded in a response mad */
  575. if (mad->mad_hdr.method & 0x80) {
  576. slave_id = (u8 *) &mad->mad_hdr.tid;
  577. slave = *slave_id;
  578. if (slave != 255) /*255 indicates the dom0*/
  579. *slave_id = 0; /* remap tid */
  580. }
  581. /* If a grh is present, we demux according to it */
  582. if (wc->wc_flags & IB_WC_GRH) {
  583. slave = mlx4_ib_find_real_gid(ibdev, port, grh->dgid.global.interface_id);
  584. if (slave < 0) {
  585. mlx4_ib_warn(ibdev, "failed matching grh\n");
  586. return -ENOENT;
  587. }
  588. }
  589. /* Class-specific handling */
  590. switch (mad->mad_hdr.mgmt_class) {
  591. case IB_MGMT_CLASS_SUBN_LID_ROUTED:
  592. case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
  593. /* 255 indicates the dom0 */
  594. if (slave != 255 && slave != mlx4_master_func_num(dev->dev)) {
  595. if (!mlx4_vf_smi_enabled(dev->dev, slave, port))
  596. return -EPERM;
  597. /* for a VF. drop unsolicited MADs */
  598. if (!(mad->mad_hdr.method & IB_MGMT_METHOD_RESP)) {
  599. mlx4_ib_warn(ibdev, "demux QP0. rejecting unsolicited mad for slave %d class 0x%x, method 0x%x\n",
  600. slave, mad->mad_hdr.mgmt_class,
  601. mad->mad_hdr.method);
  602. return -EINVAL;
  603. }
  604. }
  605. break;
  606. case IB_MGMT_CLASS_SUBN_ADM:
  607. if (mlx4_ib_demux_sa_handler(ibdev, port, slave,
  608. (struct ib_sa_mad *) mad))
  609. return 0;
  610. break;
  611. case IB_MGMT_CLASS_CM:
  612. if (mlx4_ib_demux_cm_handler(ibdev, port, &slave, mad))
  613. return 0;
  614. break;
  615. case IB_MGMT_CLASS_DEVICE_MGMT:
  616. if (mad->mad_hdr.method != IB_MGMT_METHOD_GET_RESP)
  617. return 0;
  618. break;
  619. default:
  620. /* Drop unsupported classes for slaves in tunnel mode */
  621. if (slave != mlx4_master_func_num(dev->dev)) {
  622. pr_debug("dropping unsupported ingress mad from class:%d "
  623. "for slave:%d\n", mad->mad_hdr.mgmt_class, slave);
  624. return 0;
  625. }
  626. }
  627. /*make sure that no slave==255 was not handled yet.*/
  628. if (slave >= dev->dev->caps.sqp_demux) {
  629. mlx4_ib_warn(ibdev, "slave id: %d is bigger than allowed:%d\n",
  630. slave, dev->dev->caps.sqp_demux);
  631. return -ENOENT;
  632. }
  633. err = mlx4_ib_send_to_slave(dev, slave, port, wc->qp->qp_type, wc, grh, mad);
  634. if (err)
  635. pr_debug("failed sending to slave %d via tunnel qp (%d)\n",
  636. slave, err);
  637. return 0;
  638. }
  639. static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  640. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  641. const struct ib_mad *in_mad, struct ib_mad *out_mad)
  642. {
  643. u16 slid, prev_lid = 0;
  644. int err;
  645. struct ib_port_attr pattr;
  646. if (in_wc && in_wc->qp->qp_num) {
  647. pr_debug("received MAD: slid:%d sqpn:%d "
  648. "dlid_bits:%d dqpn:%d wc_flags:0x%x, cls %x, mtd %x, atr %x\n",
  649. in_wc->slid, in_wc->src_qp,
  650. in_wc->dlid_path_bits,
  651. in_wc->qp->qp_num,
  652. in_wc->wc_flags,
  653. in_mad->mad_hdr.mgmt_class, in_mad->mad_hdr.method,
  654. be16_to_cpu(in_mad->mad_hdr.attr_id));
  655. if (in_wc->wc_flags & IB_WC_GRH) {
  656. pr_debug("sgid_hi:0x%016llx sgid_lo:0x%016llx\n",
  657. be64_to_cpu(in_grh->sgid.global.subnet_prefix),
  658. be64_to_cpu(in_grh->sgid.global.interface_id));
  659. pr_debug("dgid_hi:0x%016llx dgid_lo:0x%016llx\n",
  660. be64_to_cpu(in_grh->dgid.global.subnet_prefix),
  661. be64_to_cpu(in_grh->dgid.global.interface_id));
  662. }
  663. }
  664. slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
  665. if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
  666. forward_trap(to_mdev(ibdev), port_num, in_mad);
  667. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  668. }
  669. if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  670. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  671. if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
  672. in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&
  673. in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
  674. return IB_MAD_RESULT_SUCCESS;
  675. /*
  676. * Don't process SMInfo queries -- the SMA can't handle them.
  677. */
  678. if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
  679. return IB_MAD_RESULT_SUCCESS;
  680. } else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
  681. in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1 ||
  682. in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2 ||
  683. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
  684. if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
  685. in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)
  686. return IB_MAD_RESULT_SUCCESS;
  687. } else
  688. return IB_MAD_RESULT_SUCCESS;
  689. if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  690. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
  691. in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
  692. in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
  693. !ib_query_port(ibdev, port_num, &pattr))
  694. prev_lid = pattr.lid;
  695. err = mlx4_MAD_IFC(to_mdev(ibdev),
  696. (mad_flags & IB_MAD_IGNORE_MKEY ? MLX4_MAD_IFC_IGNORE_MKEY : 0) |
  697. (mad_flags & IB_MAD_IGNORE_BKEY ? MLX4_MAD_IFC_IGNORE_BKEY : 0) |
  698. MLX4_MAD_IFC_NET_VIEW,
  699. port_num, in_wc, in_grh, in_mad, out_mad);
  700. if (err)
  701. return IB_MAD_RESULT_FAILURE;
  702. if (!out_mad->mad_hdr.status) {
  703. if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_PORT_MNG_CHG_EV))
  704. smp_snoop(ibdev, port_num, in_mad, prev_lid);
  705. /* slaves get node desc from FW */
  706. if (!mlx4_is_slave(to_mdev(ibdev)->dev))
  707. node_desc_override(ibdev, out_mad);
  708. }
  709. /* set return bit in status of directed route responses */
  710. if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  711. out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
  712. if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
  713. /* no response for trap repress */
  714. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  715. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  716. }
  717. static void edit_counter(struct mlx4_counter *cnt, void *counters,
  718. __be16 attr_id)
  719. {
  720. switch (attr_id) {
  721. case IB_PMA_PORT_COUNTERS:
  722. {
  723. struct ib_pma_portcounters *pma_cnt =
  724. (struct ib_pma_portcounters *)counters;
  725. ASSIGN_32BIT_COUNTER(pma_cnt->port_xmit_data,
  726. (be64_to_cpu(cnt->tx_bytes) >> 2));
  727. ASSIGN_32BIT_COUNTER(pma_cnt->port_rcv_data,
  728. (be64_to_cpu(cnt->rx_bytes) >> 2));
  729. ASSIGN_32BIT_COUNTER(pma_cnt->port_xmit_packets,
  730. be64_to_cpu(cnt->tx_frames));
  731. ASSIGN_32BIT_COUNTER(pma_cnt->port_rcv_packets,
  732. be64_to_cpu(cnt->rx_frames));
  733. break;
  734. }
  735. case IB_PMA_PORT_COUNTERS_EXT:
  736. {
  737. struct ib_pma_portcounters_ext *pma_cnt_ext =
  738. (struct ib_pma_portcounters_ext *)counters;
  739. pma_cnt_ext->port_xmit_data =
  740. cpu_to_be64(be64_to_cpu(cnt->tx_bytes) >> 2);
  741. pma_cnt_ext->port_rcv_data =
  742. cpu_to_be64(be64_to_cpu(cnt->rx_bytes) >> 2);
  743. pma_cnt_ext->port_xmit_packets = cnt->tx_frames;
  744. pma_cnt_ext->port_rcv_packets = cnt->rx_frames;
  745. break;
  746. }
  747. }
  748. }
  749. static int iboe_process_mad_port_info(void *out_mad)
  750. {
  751. struct ib_class_port_info cpi = {};
  752. cpi.capability_mask = IB_PMA_CLASS_CAP_EXT_WIDTH;
  753. memcpy(out_mad, &cpi, sizeof(cpi));
  754. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  755. }
  756. static int iboe_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  757. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  758. const struct ib_mad *in_mad, struct ib_mad *out_mad)
  759. {
  760. struct mlx4_counter counter_stats;
  761. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  762. struct counter_index *tmp_counter;
  763. int err = IB_MAD_RESULT_FAILURE, stats_avail = 0;
  764. if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_PERF_MGMT)
  765. return -EINVAL;
  766. if (in_mad->mad_hdr.attr_id == IB_PMA_CLASS_PORT_INFO)
  767. return iboe_process_mad_port_info((void *)(out_mad->data + 40));
  768. memset(&counter_stats, 0, sizeof(counter_stats));
  769. mutex_lock(&dev->counters_table[port_num - 1].mutex);
  770. list_for_each_entry(tmp_counter,
  771. &dev->counters_table[port_num - 1].counters_list,
  772. list) {
  773. err = mlx4_get_counter_stats(dev->dev,
  774. tmp_counter->index,
  775. &counter_stats, 0);
  776. if (err) {
  777. err = IB_MAD_RESULT_FAILURE;
  778. stats_avail = 0;
  779. break;
  780. }
  781. stats_avail = 1;
  782. }
  783. mutex_unlock(&dev->counters_table[port_num - 1].mutex);
  784. if (stats_avail) {
  785. memset(out_mad->data, 0, sizeof out_mad->data);
  786. switch (counter_stats.counter_mode & 0xf) {
  787. case 0:
  788. edit_counter(&counter_stats,
  789. (void *)(out_mad->data + 40),
  790. in_mad->mad_hdr.attr_id);
  791. err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  792. break;
  793. default:
  794. err = IB_MAD_RESULT_FAILURE;
  795. }
  796. }
  797. return err;
  798. }
  799. int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  800. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  801. const struct ib_mad_hdr *in, size_t in_mad_size,
  802. struct ib_mad_hdr *out, size_t *out_mad_size,
  803. u16 *out_mad_pkey_index)
  804. {
  805. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  806. const struct ib_mad *in_mad = (const struct ib_mad *)in;
  807. struct ib_mad *out_mad = (struct ib_mad *)out;
  808. enum rdma_link_layer link = rdma_port_get_link_layer(ibdev, port_num);
  809. if (WARN_ON_ONCE(in_mad_size != sizeof(*in_mad) ||
  810. *out_mad_size != sizeof(*out_mad)))
  811. return IB_MAD_RESULT_FAILURE;
  812. /* iboe_process_mad() which uses the HCA flow-counters to implement IB PMA
  813. * queries, should be called only by VFs and for that specific purpose
  814. */
  815. if (link == IB_LINK_LAYER_INFINIBAND) {
  816. if (mlx4_is_slave(dev->dev) &&
  817. (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT &&
  818. (in_mad->mad_hdr.attr_id == IB_PMA_PORT_COUNTERS ||
  819. in_mad->mad_hdr.attr_id == IB_PMA_PORT_COUNTERS_EXT ||
  820. in_mad->mad_hdr.attr_id == IB_PMA_CLASS_PORT_INFO)))
  821. return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
  822. in_grh, in_mad, out_mad);
  823. return ib_process_mad(ibdev, mad_flags, port_num, in_wc,
  824. in_grh, in_mad, out_mad);
  825. }
  826. if (link == IB_LINK_LAYER_ETHERNET)
  827. return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
  828. in_grh, in_mad, out_mad);
  829. return -EINVAL;
  830. }
  831. static void send_handler(struct ib_mad_agent *agent,
  832. struct ib_mad_send_wc *mad_send_wc)
  833. {
  834. if (mad_send_wc->send_buf->context[0])
  835. ib_destroy_ah(mad_send_wc->send_buf->context[0]);
  836. ib_free_send_mad(mad_send_wc->send_buf);
  837. }
  838. int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
  839. {
  840. struct ib_mad_agent *agent;
  841. int p, q;
  842. int ret;
  843. enum rdma_link_layer ll;
  844. for (p = 0; p < dev->num_ports; ++p) {
  845. ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);
  846. for (q = 0; q <= 1; ++q) {
  847. if (ll == IB_LINK_LAYER_INFINIBAND) {
  848. agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
  849. q ? IB_QPT_GSI : IB_QPT_SMI,
  850. NULL, 0, send_handler,
  851. NULL, NULL, 0);
  852. if (IS_ERR(agent)) {
  853. ret = PTR_ERR(agent);
  854. goto err;
  855. }
  856. dev->send_agent[p][q] = agent;
  857. } else
  858. dev->send_agent[p][q] = NULL;
  859. }
  860. }
  861. return 0;
  862. err:
  863. for (p = 0; p < dev->num_ports; ++p)
  864. for (q = 0; q <= 1; ++q)
  865. if (dev->send_agent[p][q])
  866. ib_unregister_mad_agent(dev->send_agent[p][q]);
  867. return ret;
  868. }
  869. void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
  870. {
  871. struct ib_mad_agent *agent;
  872. int p, q;
  873. for (p = 0; p < dev->num_ports; ++p) {
  874. for (q = 0; q <= 1; ++q) {
  875. agent = dev->send_agent[p][q];
  876. if (agent) {
  877. dev->send_agent[p][q] = NULL;
  878. ib_unregister_mad_agent(agent);
  879. }
  880. }
  881. if (dev->sm_ah[p])
  882. ib_destroy_ah(dev->sm_ah[p]);
  883. }
  884. }
  885. static void handle_lid_change_event(struct mlx4_ib_dev *dev, u8 port_num)
  886. {
  887. mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_LID_CHANGE);
  888. if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
  889. mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
  890. MLX4_EQ_PORT_INFO_LID_CHANGE_MASK);
  891. }
  892. static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num)
  893. {
  894. /* re-configure the alias-guid and mcg's */
  895. if (mlx4_is_master(dev->dev)) {
  896. mlx4_ib_invalidate_all_guid_record(dev, port_num);
  897. if (!dev->sriov.is_going_down) {
  898. mlx4_ib_mcg_port_cleanup(&dev->sriov.demux[port_num - 1], 0);
  899. mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
  900. MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK);
  901. }
  902. }
  903. mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_CLIENT_REREGISTER);
  904. }
  905. static void propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
  906. struct mlx4_eqe *eqe)
  907. {
  908. __propagate_pkey_ev(dev, port_num, GET_BLK_PTR_FROM_EQE(eqe),
  909. GET_MASK_FROM_EQE(eqe));
  910. }
  911. static void handle_slaves_guid_change(struct mlx4_ib_dev *dev, u8 port_num,
  912. u32 guid_tbl_blk_num, u32 change_bitmap)
  913. {
  914. struct ib_smp *in_mad = NULL;
  915. struct ib_smp *out_mad = NULL;
  916. u16 i;
  917. if (!mlx4_is_mfunc(dev->dev) || !mlx4_is_master(dev->dev))
  918. return;
  919. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  920. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  921. if (!in_mad || !out_mad) {
  922. mlx4_ib_warn(&dev->ib_dev, "failed to allocate memory for guid info mads\n");
  923. goto out;
  924. }
  925. guid_tbl_blk_num *= 4;
  926. for (i = 0; i < 4; i++) {
  927. if (change_bitmap && (!((change_bitmap >> (8 * i)) & 0xff)))
  928. continue;
  929. memset(in_mad, 0, sizeof *in_mad);
  930. memset(out_mad, 0, sizeof *out_mad);
  931. in_mad->base_version = 1;
  932. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  933. in_mad->class_version = 1;
  934. in_mad->method = IB_MGMT_METHOD_GET;
  935. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  936. in_mad->attr_mod = cpu_to_be32(guid_tbl_blk_num + i);
  937. if (mlx4_MAD_IFC(dev,
  938. MLX4_MAD_IFC_IGNORE_KEYS | MLX4_MAD_IFC_NET_VIEW,
  939. port_num, NULL, NULL, in_mad, out_mad)) {
  940. mlx4_ib_warn(&dev->ib_dev, "Failed in get GUID INFO MAD_IFC\n");
  941. goto out;
  942. }
  943. mlx4_ib_update_cache_on_guid_change(dev, guid_tbl_blk_num + i,
  944. port_num,
  945. (u8 *)(&((struct ib_smp *)out_mad)->data));
  946. mlx4_ib_notify_slaves_on_guid_change(dev, guid_tbl_blk_num + i,
  947. port_num,
  948. (u8 *)(&((struct ib_smp *)out_mad)->data));
  949. }
  950. out:
  951. kfree(in_mad);
  952. kfree(out_mad);
  953. return;
  954. }
  955. void handle_port_mgmt_change_event(struct work_struct *work)
  956. {
  957. struct ib_event_work *ew = container_of(work, struct ib_event_work, work);
  958. struct mlx4_ib_dev *dev = ew->ib_dev;
  959. struct mlx4_eqe *eqe = &(ew->ib_eqe);
  960. u8 port = eqe->event.port_mgmt_change.port;
  961. u32 changed_attr;
  962. u32 tbl_block;
  963. u32 change_bitmap;
  964. switch (eqe->subtype) {
  965. case MLX4_DEV_PMC_SUBTYPE_PORT_INFO:
  966. changed_attr = be32_to_cpu(eqe->event.port_mgmt_change.params.port_info.changed_attr);
  967. /* Update the SM ah - This should be done before handling
  968. the other changed attributes so that MADs can be sent to the SM */
  969. if (changed_attr & MSTR_SM_CHANGE_MASK) {
  970. u16 lid = be16_to_cpu(eqe->event.port_mgmt_change.params.port_info.mstr_sm_lid);
  971. u8 sl = eqe->event.port_mgmt_change.params.port_info.mstr_sm_sl & 0xf;
  972. update_sm_ah(dev, port, lid, sl);
  973. }
  974. /* Check if it is a lid change event */
  975. if (changed_attr & MLX4_EQ_PORT_INFO_LID_CHANGE_MASK)
  976. handle_lid_change_event(dev, port);
  977. /* Generate GUID changed event */
  978. if (changed_attr & MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK) {
  979. mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
  980. /*if master, notify all slaves*/
  981. if (mlx4_is_master(dev->dev))
  982. mlx4_gen_slaves_port_mgt_ev(dev->dev, port,
  983. MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK);
  984. }
  985. if (changed_attr & MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK)
  986. handle_client_rereg_event(dev, port);
  987. break;
  988. case MLX4_DEV_PMC_SUBTYPE_PKEY_TABLE:
  989. mlx4_ib_dispatch_event(dev, port, IB_EVENT_PKEY_CHANGE);
  990. if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
  991. propagate_pkey_ev(dev, port, eqe);
  992. break;
  993. case MLX4_DEV_PMC_SUBTYPE_GUID_INFO:
  994. /* paravirtualized master's guid is guid 0 -- does not change */
  995. if (!mlx4_is_master(dev->dev))
  996. mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
  997. /*if master, notify relevant slaves*/
  998. else if (!dev->sriov.is_going_down) {
  999. tbl_block = GET_BLK_PTR_FROM_EQE(eqe);
  1000. change_bitmap = GET_MASK_FROM_EQE(eqe);
  1001. handle_slaves_guid_change(dev, port, tbl_block, change_bitmap);
  1002. }
  1003. break;
  1004. default:
  1005. pr_warn("Unsupported subtype 0x%x for "
  1006. "Port Management Change event\n", eqe->subtype);
  1007. }
  1008. kfree(ew);
  1009. }
  1010. void mlx4_ib_dispatch_event(struct mlx4_ib_dev *dev, u8 port_num,
  1011. enum ib_event_type type)
  1012. {
  1013. struct ib_event event;
  1014. event.device = &dev->ib_dev;
  1015. event.element.port_num = port_num;
  1016. event.event = type;
  1017. ib_dispatch_event(&event);
  1018. }
  1019. static void mlx4_ib_tunnel_comp_handler(struct ib_cq *cq, void *arg)
  1020. {
  1021. unsigned long flags;
  1022. struct mlx4_ib_demux_pv_ctx *ctx = cq->cq_context;
  1023. struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
  1024. spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
  1025. if (!dev->sriov.is_going_down && ctx->state == DEMUX_PV_STATE_ACTIVE)
  1026. queue_work(ctx->wq, &ctx->work);
  1027. spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
  1028. }
  1029. static int mlx4_ib_post_pv_qp_buf(struct mlx4_ib_demux_pv_ctx *ctx,
  1030. struct mlx4_ib_demux_pv_qp *tun_qp,
  1031. int index)
  1032. {
  1033. struct ib_sge sg_list;
  1034. struct ib_recv_wr recv_wr, *bad_recv_wr;
  1035. int size;
  1036. size = (tun_qp->qp->qp_type == IB_QPT_UD) ?
  1037. sizeof (struct mlx4_tunnel_mad) : sizeof (struct mlx4_mad_rcv_buf);
  1038. sg_list.addr = tun_qp->ring[index].map;
  1039. sg_list.length = size;
  1040. sg_list.lkey = ctx->pd->local_dma_lkey;
  1041. recv_wr.next = NULL;
  1042. recv_wr.sg_list = &sg_list;
  1043. recv_wr.num_sge = 1;
  1044. recv_wr.wr_id = (u64) index | MLX4_TUN_WRID_RECV |
  1045. MLX4_TUN_SET_WRID_QPN(tun_qp->proxy_qpt);
  1046. ib_dma_sync_single_for_device(ctx->ib_dev, tun_qp->ring[index].map,
  1047. size, DMA_FROM_DEVICE);
  1048. return ib_post_recv(tun_qp->qp, &recv_wr, &bad_recv_wr);
  1049. }
  1050. static int mlx4_ib_multiplex_sa_handler(struct ib_device *ibdev, int port,
  1051. int slave, struct ib_sa_mad *sa_mad)
  1052. {
  1053. int ret = 0;
  1054. /* dispatch to different sa handlers */
  1055. switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
  1056. case IB_SA_ATTR_MC_MEMBER_REC:
  1057. ret = mlx4_ib_mcg_multiplex_handler(ibdev, port, slave, sa_mad);
  1058. break;
  1059. default:
  1060. break;
  1061. }
  1062. return ret;
  1063. }
  1064. static int is_proxy_qp0(struct mlx4_ib_dev *dev, int qpn, int slave)
  1065. {
  1066. int proxy_start = dev->dev->phys_caps.base_proxy_sqpn + 8 * slave;
  1067. return (qpn >= proxy_start && qpn <= proxy_start + 1);
  1068. }
  1069. int mlx4_ib_send_to_wire(struct mlx4_ib_dev *dev, int slave, u8 port,
  1070. enum ib_qp_type dest_qpt, u16 pkey_index,
  1071. u32 remote_qpn, u32 qkey, struct ib_ah_attr *attr,
  1072. u8 *s_mac, u16 vlan_id, struct ib_mad *mad)
  1073. {
  1074. struct ib_sge list;
  1075. struct ib_ud_wr wr;
  1076. struct ib_send_wr *bad_wr;
  1077. struct mlx4_ib_demux_pv_ctx *sqp_ctx;
  1078. struct mlx4_ib_demux_pv_qp *sqp;
  1079. struct mlx4_mad_snd_buf *sqp_mad;
  1080. struct ib_ah *ah;
  1081. struct ib_qp *send_qp = NULL;
  1082. unsigned wire_tx_ix = 0;
  1083. int ret = 0;
  1084. u16 wire_pkey_ix;
  1085. int src_qpnum;
  1086. u8 sgid_index;
  1087. sqp_ctx = dev->sriov.sqps[port-1];
  1088. /* check if proxy qp created */
  1089. if (!sqp_ctx || sqp_ctx->state != DEMUX_PV_STATE_ACTIVE)
  1090. return -EAGAIN;
  1091. if (dest_qpt == IB_QPT_SMI) {
  1092. src_qpnum = 0;
  1093. sqp = &sqp_ctx->qp[0];
  1094. wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
  1095. } else {
  1096. src_qpnum = 1;
  1097. sqp = &sqp_ctx->qp[1];
  1098. wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][pkey_index];
  1099. }
  1100. send_qp = sqp->qp;
  1101. /* create ah */
  1102. sgid_index = attr->grh.sgid_index;
  1103. attr->grh.sgid_index = 0;
  1104. ah = ib_create_ah(sqp_ctx->pd, attr);
  1105. if (IS_ERR(ah))
  1106. return -ENOMEM;
  1107. attr->grh.sgid_index = sgid_index;
  1108. to_mah(ah)->av.ib.gid_index = sgid_index;
  1109. /* get rid of force-loopback bit */
  1110. to_mah(ah)->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF);
  1111. spin_lock(&sqp->tx_lock);
  1112. if (sqp->tx_ix_head - sqp->tx_ix_tail >=
  1113. (MLX4_NUM_TUNNEL_BUFS - 1))
  1114. ret = -EAGAIN;
  1115. else
  1116. wire_tx_ix = (++sqp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
  1117. spin_unlock(&sqp->tx_lock);
  1118. if (ret)
  1119. goto out;
  1120. sqp_mad = (struct mlx4_mad_snd_buf *) (sqp->tx_ring[wire_tx_ix].buf.addr);
  1121. if (sqp->tx_ring[wire_tx_ix].ah)
  1122. ib_destroy_ah(sqp->tx_ring[wire_tx_ix].ah);
  1123. sqp->tx_ring[wire_tx_ix].ah = ah;
  1124. ib_dma_sync_single_for_cpu(&dev->ib_dev,
  1125. sqp->tx_ring[wire_tx_ix].buf.map,
  1126. sizeof (struct mlx4_mad_snd_buf),
  1127. DMA_TO_DEVICE);
  1128. memcpy(&sqp_mad->payload, mad, sizeof *mad);
  1129. ib_dma_sync_single_for_device(&dev->ib_dev,
  1130. sqp->tx_ring[wire_tx_ix].buf.map,
  1131. sizeof (struct mlx4_mad_snd_buf),
  1132. DMA_TO_DEVICE);
  1133. list.addr = sqp->tx_ring[wire_tx_ix].buf.map;
  1134. list.length = sizeof (struct mlx4_mad_snd_buf);
  1135. list.lkey = sqp_ctx->pd->local_dma_lkey;
  1136. wr.ah = ah;
  1137. wr.port_num = port;
  1138. wr.pkey_index = wire_pkey_ix;
  1139. wr.remote_qkey = qkey;
  1140. wr.remote_qpn = remote_qpn;
  1141. wr.wr.next = NULL;
  1142. wr.wr.wr_id = ((u64) wire_tx_ix) | MLX4_TUN_SET_WRID_QPN(src_qpnum);
  1143. wr.wr.sg_list = &list;
  1144. wr.wr.num_sge = 1;
  1145. wr.wr.opcode = IB_WR_SEND;
  1146. wr.wr.send_flags = IB_SEND_SIGNALED;
  1147. if (s_mac)
  1148. memcpy(to_mah(ah)->av.eth.s_mac, s_mac, 6);
  1149. if (vlan_id < 0x1000)
  1150. vlan_id |= (attr->sl & 7) << 13;
  1151. to_mah(ah)->av.eth.vlan = cpu_to_be16(vlan_id);
  1152. ret = ib_post_send(send_qp, &wr.wr, &bad_wr);
  1153. if (!ret)
  1154. return 0;
  1155. spin_lock(&sqp->tx_lock);
  1156. sqp->tx_ix_tail++;
  1157. spin_unlock(&sqp->tx_lock);
  1158. sqp->tx_ring[wire_tx_ix].ah = NULL;
  1159. out:
  1160. ib_destroy_ah(ah);
  1161. return ret;
  1162. }
  1163. static int get_slave_base_gid_ix(struct mlx4_ib_dev *dev, int slave, int port)
  1164. {
  1165. if (rdma_port_get_link_layer(&dev->ib_dev, port) == IB_LINK_LAYER_INFINIBAND)
  1166. return slave;
  1167. return mlx4_get_base_gid_ix(dev->dev, slave, port);
  1168. }
  1169. static void fill_in_real_sgid_index(struct mlx4_ib_dev *dev, int slave, int port,
  1170. struct ib_ah_attr *ah_attr)
  1171. {
  1172. if (rdma_port_get_link_layer(&dev->ib_dev, port) == IB_LINK_LAYER_INFINIBAND)
  1173. ah_attr->grh.sgid_index = slave;
  1174. else
  1175. ah_attr->grh.sgid_index += get_slave_base_gid_ix(dev, slave, port);
  1176. }
  1177. static void mlx4_ib_multiplex_mad(struct mlx4_ib_demux_pv_ctx *ctx, struct ib_wc *wc)
  1178. {
  1179. struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
  1180. struct mlx4_ib_demux_pv_qp *tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc->wr_id)];
  1181. int wr_ix = wc->wr_id & (MLX4_NUM_TUNNEL_BUFS - 1);
  1182. struct mlx4_tunnel_mad *tunnel = tun_qp->ring[wr_ix].addr;
  1183. struct mlx4_ib_ah ah;
  1184. struct ib_ah_attr ah_attr;
  1185. u8 *slave_id;
  1186. int slave;
  1187. int port;
  1188. u16 vlan_id;
  1189. /* Get slave that sent this packet */
  1190. if (wc->src_qp < dev->dev->phys_caps.base_proxy_sqpn ||
  1191. wc->src_qp >= dev->dev->phys_caps.base_proxy_sqpn + 8 * MLX4_MFUNC_MAX ||
  1192. (wc->src_qp & 0x1) != ctx->port - 1 ||
  1193. wc->src_qp & 0x4) {
  1194. mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d\n", wc->src_qp);
  1195. return;
  1196. }
  1197. slave = ((wc->src_qp & ~0x7) - dev->dev->phys_caps.base_proxy_sqpn) / 8;
  1198. if (slave != ctx->slave) {
  1199. mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d: "
  1200. "belongs to another slave\n", wc->src_qp);
  1201. return;
  1202. }
  1203. /* Map transaction ID */
  1204. ib_dma_sync_single_for_cpu(ctx->ib_dev, tun_qp->ring[wr_ix].map,
  1205. sizeof (struct mlx4_tunnel_mad),
  1206. DMA_FROM_DEVICE);
  1207. switch (tunnel->mad.mad_hdr.method) {
  1208. case IB_MGMT_METHOD_SET:
  1209. case IB_MGMT_METHOD_GET:
  1210. case IB_MGMT_METHOD_REPORT:
  1211. case IB_SA_METHOD_GET_TABLE:
  1212. case IB_SA_METHOD_DELETE:
  1213. case IB_SA_METHOD_GET_MULTI:
  1214. case IB_SA_METHOD_GET_TRACE_TBL:
  1215. slave_id = (u8 *) &tunnel->mad.mad_hdr.tid;
  1216. if (*slave_id) {
  1217. mlx4_ib_warn(ctx->ib_dev, "egress mad has non-null tid msb:%d "
  1218. "class:%d slave:%d\n", *slave_id,
  1219. tunnel->mad.mad_hdr.mgmt_class, slave);
  1220. return;
  1221. } else
  1222. *slave_id = slave;
  1223. default:
  1224. /* nothing */;
  1225. }
  1226. /* Class-specific handling */
  1227. switch (tunnel->mad.mad_hdr.mgmt_class) {
  1228. case IB_MGMT_CLASS_SUBN_LID_ROUTED:
  1229. case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
  1230. if (slave != mlx4_master_func_num(dev->dev) &&
  1231. !mlx4_vf_smi_enabled(dev->dev, slave, ctx->port))
  1232. return;
  1233. break;
  1234. case IB_MGMT_CLASS_SUBN_ADM:
  1235. if (mlx4_ib_multiplex_sa_handler(ctx->ib_dev, ctx->port, slave,
  1236. (struct ib_sa_mad *) &tunnel->mad))
  1237. return;
  1238. break;
  1239. case IB_MGMT_CLASS_CM:
  1240. if (mlx4_ib_multiplex_cm_handler(ctx->ib_dev, ctx->port, slave,
  1241. (struct ib_mad *) &tunnel->mad))
  1242. return;
  1243. break;
  1244. case IB_MGMT_CLASS_DEVICE_MGMT:
  1245. if (tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_GET &&
  1246. tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_SET)
  1247. return;
  1248. break;
  1249. default:
  1250. /* Drop unsupported classes for slaves in tunnel mode */
  1251. if (slave != mlx4_master_func_num(dev->dev)) {
  1252. mlx4_ib_warn(ctx->ib_dev, "dropping unsupported egress mad from class:%d "
  1253. "for slave:%d\n", tunnel->mad.mad_hdr.mgmt_class, slave);
  1254. return;
  1255. }
  1256. }
  1257. /* We are using standard ib_core services to send the mad, so generate a
  1258. * stadard address handle by decoding the tunnelled mlx4_ah fields */
  1259. memcpy(&ah.av, &tunnel->hdr.av, sizeof (struct mlx4_av));
  1260. ah.ibah.device = ctx->ib_dev;
  1261. port = be32_to_cpu(ah.av.ib.port_pd) >> 24;
  1262. port = mlx4_slave_convert_port(dev->dev, slave, port);
  1263. if (port < 0)
  1264. return;
  1265. ah.av.ib.port_pd = cpu_to_be32(port << 24 | (be32_to_cpu(ah.av.ib.port_pd) & 0xffffff));
  1266. mlx4_ib_query_ah(&ah.ibah, &ah_attr);
  1267. if (ah_attr.ah_flags & IB_AH_GRH)
  1268. fill_in_real_sgid_index(dev, slave, ctx->port, &ah_attr);
  1269. memcpy(ah_attr.dmac, tunnel->hdr.mac, 6);
  1270. vlan_id = be16_to_cpu(tunnel->hdr.vlan);
  1271. /* if slave have default vlan use it */
  1272. mlx4_get_slave_default_vlan(dev->dev, ctx->port, slave,
  1273. &vlan_id, &ah_attr.sl);
  1274. mlx4_ib_send_to_wire(dev, slave, ctx->port,
  1275. is_proxy_qp0(dev, wc->src_qp, slave) ?
  1276. IB_QPT_SMI : IB_QPT_GSI,
  1277. be16_to_cpu(tunnel->hdr.pkey_index),
  1278. be32_to_cpu(tunnel->hdr.remote_qpn),
  1279. be32_to_cpu(tunnel->hdr.qkey),
  1280. &ah_attr, wc->smac, vlan_id, &tunnel->mad);
  1281. }
  1282. static int mlx4_ib_alloc_pv_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
  1283. enum ib_qp_type qp_type, int is_tun)
  1284. {
  1285. int i;
  1286. struct mlx4_ib_demux_pv_qp *tun_qp;
  1287. int rx_buf_size, tx_buf_size;
  1288. if (qp_type > IB_QPT_GSI)
  1289. return -EINVAL;
  1290. tun_qp = &ctx->qp[qp_type];
  1291. tun_qp->ring = kzalloc(sizeof (struct mlx4_ib_buf) * MLX4_NUM_TUNNEL_BUFS,
  1292. GFP_KERNEL);
  1293. if (!tun_qp->ring)
  1294. return -ENOMEM;
  1295. tun_qp->tx_ring = kcalloc(MLX4_NUM_TUNNEL_BUFS,
  1296. sizeof (struct mlx4_ib_tun_tx_buf),
  1297. GFP_KERNEL);
  1298. if (!tun_qp->tx_ring) {
  1299. kfree(tun_qp->ring);
  1300. tun_qp->ring = NULL;
  1301. return -ENOMEM;
  1302. }
  1303. if (is_tun) {
  1304. rx_buf_size = sizeof (struct mlx4_tunnel_mad);
  1305. tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
  1306. } else {
  1307. rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
  1308. tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
  1309. }
  1310. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  1311. tun_qp->ring[i].addr = kmalloc(rx_buf_size, GFP_KERNEL);
  1312. if (!tun_qp->ring[i].addr)
  1313. goto err;
  1314. tun_qp->ring[i].map = ib_dma_map_single(ctx->ib_dev,
  1315. tun_qp->ring[i].addr,
  1316. rx_buf_size,
  1317. DMA_FROM_DEVICE);
  1318. if (ib_dma_mapping_error(ctx->ib_dev, tun_qp->ring[i].map)) {
  1319. kfree(tun_qp->ring[i].addr);
  1320. goto err;
  1321. }
  1322. }
  1323. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  1324. tun_qp->tx_ring[i].buf.addr =
  1325. kmalloc(tx_buf_size, GFP_KERNEL);
  1326. if (!tun_qp->tx_ring[i].buf.addr)
  1327. goto tx_err;
  1328. tun_qp->tx_ring[i].buf.map =
  1329. ib_dma_map_single(ctx->ib_dev,
  1330. tun_qp->tx_ring[i].buf.addr,
  1331. tx_buf_size,
  1332. DMA_TO_DEVICE);
  1333. if (ib_dma_mapping_error(ctx->ib_dev,
  1334. tun_qp->tx_ring[i].buf.map)) {
  1335. kfree(tun_qp->tx_ring[i].buf.addr);
  1336. goto tx_err;
  1337. }
  1338. tun_qp->tx_ring[i].ah = NULL;
  1339. }
  1340. spin_lock_init(&tun_qp->tx_lock);
  1341. tun_qp->tx_ix_head = 0;
  1342. tun_qp->tx_ix_tail = 0;
  1343. tun_qp->proxy_qpt = qp_type;
  1344. return 0;
  1345. tx_err:
  1346. while (i > 0) {
  1347. --i;
  1348. ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
  1349. tx_buf_size, DMA_TO_DEVICE);
  1350. kfree(tun_qp->tx_ring[i].buf.addr);
  1351. }
  1352. kfree(tun_qp->tx_ring);
  1353. tun_qp->tx_ring = NULL;
  1354. i = MLX4_NUM_TUNNEL_BUFS;
  1355. err:
  1356. while (i > 0) {
  1357. --i;
  1358. ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
  1359. rx_buf_size, DMA_FROM_DEVICE);
  1360. kfree(tun_qp->ring[i].addr);
  1361. }
  1362. kfree(tun_qp->ring);
  1363. tun_qp->ring = NULL;
  1364. return -ENOMEM;
  1365. }
  1366. static void mlx4_ib_free_pv_qp_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
  1367. enum ib_qp_type qp_type, int is_tun)
  1368. {
  1369. int i;
  1370. struct mlx4_ib_demux_pv_qp *tun_qp;
  1371. int rx_buf_size, tx_buf_size;
  1372. if (qp_type > IB_QPT_GSI)
  1373. return;
  1374. tun_qp = &ctx->qp[qp_type];
  1375. if (is_tun) {
  1376. rx_buf_size = sizeof (struct mlx4_tunnel_mad);
  1377. tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
  1378. } else {
  1379. rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
  1380. tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
  1381. }
  1382. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  1383. ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
  1384. rx_buf_size, DMA_FROM_DEVICE);
  1385. kfree(tun_qp->ring[i].addr);
  1386. }
  1387. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  1388. ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
  1389. tx_buf_size, DMA_TO_DEVICE);
  1390. kfree(tun_qp->tx_ring[i].buf.addr);
  1391. if (tun_qp->tx_ring[i].ah)
  1392. ib_destroy_ah(tun_qp->tx_ring[i].ah);
  1393. }
  1394. kfree(tun_qp->tx_ring);
  1395. kfree(tun_qp->ring);
  1396. }
  1397. static void mlx4_ib_tunnel_comp_worker(struct work_struct *work)
  1398. {
  1399. struct mlx4_ib_demux_pv_ctx *ctx;
  1400. struct mlx4_ib_demux_pv_qp *tun_qp;
  1401. struct ib_wc wc;
  1402. int ret;
  1403. ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
  1404. ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
  1405. while (ib_poll_cq(ctx->cq, 1, &wc) == 1) {
  1406. tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
  1407. if (wc.status == IB_WC_SUCCESS) {
  1408. switch (wc.opcode) {
  1409. case IB_WC_RECV:
  1410. mlx4_ib_multiplex_mad(ctx, &wc);
  1411. ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp,
  1412. wc.wr_id &
  1413. (MLX4_NUM_TUNNEL_BUFS - 1));
  1414. if (ret)
  1415. pr_err("Failed reposting tunnel "
  1416. "buf:%lld\n", wc.wr_id);
  1417. break;
  1418. case IB_WC_SEND:
  1419. pr_debug("received tunnel send completion:"
  1420. "wrid=0x%llx, status=0x%x\n",
  1421. wc.wr_id, wc.status);
  1422. ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
  1423. (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
  1424. tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
  1425. = NULL;
  1426. spin_lock(&tun_qp->tx_lock);
  1427. tun_qp->tx_ix_tail++;
  1428. spin_unlock(&tun_qp->tx_lock);
  1429. break;
  1430. default:
  1431. break;
  1432. }
  1433. } else {
  1434. pr_debug("mlx4_ib: completion error in tunnel: %d."
  1435. " status = %d, wrid = 0x%llx\n",
  1436. ctx->slave, wc.status, wc.wr_id);
  1437. if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
  1438. ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
  1439. (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
  1440. tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
  1441. = NULL;
  1442. spin_lock(&tun_qp->tx_lock);
  1443. tun_qp->tx_ix_tail++;
  1444. spin_unlock(&tun_qp->tx_lock);
  1445. }
  1446. }
  1447. }
  1448. }
  1449. static void pv_qp_event_handler(struct ib_event *event, void *qp_context)
  1450. {
  1451. struct mlx4_ib_demux_pv_ctx *sqp = qp_context;
  1452. /* It's worse than that! He's dead, Jim! */
  1453. pr_err("Fatal error (%d) on a MAD QP on port %d\n",
  1454. event->event, sqp->port);
  1455. }
  1456. static int create_pv_sqp(struct mlx4_ib_demux_pv_ctx *ctx,
  1457. enum ib_qp_type qp_type, int create_tun)
  1458. {
  1459. int i, ret;
  1460. struct mlx4_ib_demux_pv_qp *tun_qp;
  1461. struct mlx4_ib_qp_tunnel_init_attr qp_init_attr;
  1462. struct ib_qp_attr attr;
  1463. int qp_attr_mask_INIT;
  1464. if (qp_type > IB_QPT_GSI)
  1465. return -EINVAL;
  1466. tun_qp = &ctx->qp[qp_type];
  1467. memset(&qp_init_attr, 0, sizeof qp_init_attr);
  1468. qp_init_attr.init_attr.send_cq = ctx->cq;
  1469. qp_init_attr.init_attr.recv_cq = ctx->cq;
  1470. qp_init_attr.init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
  1471. qp_init_attr.init_attr.cap.max_send_wr = MLX4_NUM_TUNNEL_BUFS;
  1472. qp_init_attr.init_attr.cap.max_recv_wr = MLX4_NUM_TUNNEL_BUFS;
  1473. qp_init_attr.init_attr.cap.max_send_sge = 1;
  1474. qp_init_attr.init_attr.cap.max_recv_sge = 1;
  1475. if (create_tun) {
  1476. qp_init_attr.init_attr.qp_type = IB_QPT_UD;
  1477. qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_TUNNEL_QP;
  1478. qp_init_attr.port = ctx->port;
  1479. qp_init_attr.slave = ctx->slave;
  1480. qp_init_attr.proxy_qp_type = qp_type;
  1481. qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX |
  1482. IB_QP_QKEY | IB_QP_PORT;
  1483. } else {
  1484. qp_init_attr.init_attr.qp_type = qp_type;
  1485. qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_SQP;
  1486. qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_QKEY;
  1487. }
  1488. qp_init_attr.init_attr.port_num = ctx->port;
  1489. qp_init_attr.init_attr.qp_context = ctx;
  1490. qp_init_attr.init_attr.event_handler = pv_qp_event_handler;
  1491. tun_qp->qp = ib_create_qp(ctx->pd, &qp_init_attr.init_attr);
  1492. if (IS_ERR(tun_qp->qp)) {
  1493. ret = PTR_ERR(tun_qp->qp);
  1494. tun_qp->qp = NULL;
  1495. pr_err("Couldn't create %s QP (%d)\n",
  1496. create_tun ? "tunnel" : "special", ret);
  1497. return ret;
  1498. }
  1499. memset(&attr, 0, sizeof attr);
  1500. attr.qp_state = IB_QPS_INIT;
  1501. ret = 0;
  1502. if (create_tun)
  1503. ret = find_slave_port_pkey_ix(to_mdev(ctx->ib_dev), ctx->slave,
  1504. ctx->port, IB_DEFAULT_PKEY_FULL,
  1505. &attr.pkey_index);
  1506. if (ret || !create_tun)
  1507. attr.pkey_index =
  1508. to_mdev(ctx->ib_dev)->pkeys.virt2phys_pkey[ctx->slave][ctx->port - 1][0];
  1509. attr.qkey = IB_QP1_QKEY;
  1510. attr.port_num = ctx->port;
  1511. ret = ib_modify_qp(tun_qp->qp, &attr, qp_attr_mask_INIT);
  1512. if (ret) {
  1513. pr_err("Couldn't change %s qp state to INIT (%d)\n",
  1514. create_tun ? "tunnel" : "special", ret);
  1515. goto err_qp;
  1516. }
  1517. attr.qp_state = IB_QPS_RTR;
  1518. ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE);
  1519. if (ret) {
  1520. pr_err("Couldn't change %s qp state to RTR (%d)\n",
  1521. create_tun ? "tunnel" : "special", ret);
  1522. goto err_qp;
  1523. }
  1524. attr.qp_state = IB_QPS_RTS;
  1525. attr.sq_psn = 0;
  1526. ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE | IB_QP_SQ_PSN);
  1527. if (ret) {
  1528. pr_err("Couldn't change %s qp state to RTS (%d)\n",
  1529. create_tun ? "tunnel" : "special", ret);
  1530. goto err_qp;
  1531. }
  1532. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  1533. ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp, i);
  1534. if (ret) {
  1535. pr_err(" mlx4_ib_post_pv_buf error"
  1536. " (err = %d, i = %d)\n", ret, i);
  1537. goto err_qp;
  1538. }
  1539. }
  1540. return 0;
  1541. err_qp:
  1542. ib_destroy_qp(tun_qp->qp);
  1543. tun_qp->qp = NULL;
  1544. return ret;
  1545. }
  1546. /*
  1547. * IB MAD completion callback for real SQPs
  1548. */
  1549. static void mlx4_ib_sqp_comp_worker(struct work_struct *work)
  1550. {
  1551. struct mlx4_ib_demux_pv_ctx *ctx;
  1552. struct mlx4_ib_demux_pv_qp *sqp;
  1553. struct ib_wc wc;
  1554. struct ib_grh *grh;
  1555. struct ib_mad *mad;
  1556. ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
  1557. ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
  1558. while (mlx4_ib_poll_cq(ctx->cq, 1, &wc) == 1) {
  1559. sqp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
  1560. if (wc.status == IB_WC_SUCCESS) {
  1561. switch (wc.opcode) {
  1562. case IB_WC_SEND:
  1563. ib_destroy_ah(sqp->tx_ring[wc.wr_id &
  1564. (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
  1565. sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
  1566. = NULL;
  1567. spin_lock(&sqp->tx_lock);
  1568. sqp->tx_ix_tail++;
  1569. spin_unlock(&sqp->tx_lock);
  1570. break;
  1571. case IB_WC_RECV:
  1572. mad = (struct ib_mad *) &(((struct mlx4_mad_rcv_buf *)
  1573. (sqp->ring[wc.wr_id &
  1574. (MLX4_NUM_TUNNEL_BUFS - 1)].addr))->payload);
  1575. grh = &(((struct mlx4_mad_rcv_buf *)
  1576. (sqp->ring[wc.wr_id &
  1577. (MLX4_NUM_TUNNEL_BUFS - 1)].addr))->grh);
  1578. mlx4_ib_demux_mad(ctx->ib_dev, ctx->port, &wc, grh, mad);
  1579. if (mlx4_ib_post_pv_qp_buf(ctx, sqp, wc.wr_id &
  1580. (MLX4_NUM_TUNNEL_BUFS - 1)))
  1581. pr_err("Failed reposting SQP "
  1582. "buf:%lld\n", wc.wr_id);
  1583. break;
  1584. default:
  1585. BUG_ON(1);
  1586. break;
  1587. }
  1588. } else {
  1589. pr_debug("mlx4_ib: completion error in tunnel: %d."
  1590. " status = %d, wrid = 0x%llx\n",
  1591. ctx->slave, wc.status, wc.wr_id);
  1592. if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
  1593. ib_destroy_ah(sqp->tx_ring[wc.wr_id &
  1594. (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
  1595. sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
  1596. = NULL;
  1597. spin_lock(&sqp->tx_lock);
  1598. sqp->tx_ix_tail++;
  1599. spin_unlock(&sqp->tx_lock);
  1600. }
  1601. }
  1602. }
  1603. }
  1604. static int alloc_pv_object(struct mlx4_ib_dev *dev, int slave, int port,
  1605. struct mlx4_ib_demux_pv_ctx **ret_ctx)
  1606. {
  1607. struct mlx4_ib_demux_pv_ctx *ctx;
  1608. *ret_ctx = NULL;
  1609. ctx = kzalloc(sizeof (struct mlx4_ib_demux_pv_ctx), GFP_KERNEL);
  1610. if (!ctx) {
  1611. pr_err("failed allocating pv resource context "
  1612. "for port %d, slave %d\n", port, slave);
  1613. return -ENOMEM;
  1614. }
  1615. ctx->ib_dev = &dev->ib_dev;
  1616. ctx->port = port;
  1617. ctx->slave = slave;
  1618. *ret_ctx = ctx;
  1619. return 0;
  1620. }
  1621. static void free_pv_object(struct mlx4_ib_dev *dev, int slave, int port)
  1622. {
  1623. if (dev->sriov.demux[port - 1].tun[slave]) {
  1624. kfree(dev->sriov.demux[port - 1].tun[slave]);
  1625. dev->sriov.demux[port - 1].tun[slave] = NULL;
  1626. }
  1627. }
  1628. static int create_pv_resources(struct ib_device *ibdev, int slave, int port,
  1629. int create_tun, struct mlx4_ib_demux_pv_ctx *ctx)
  1630. {
  1631. int ret, cq_size;
  1632. struct ib_cq_init_attr cq_attr = {};
  1633. if (ctx->state != DEMUX_PV_STATE_DOWN)
  1634. return -EEXIST;
  1635. ctx->state = DEMUX_PV_STATE_STARTING;
  1636. /* have QP0 only if link layer is IB */
  1637. if (rdma_port_get_link_layer(ibdev, ctx->port) ==
  1638. IB_LINK_LAYER_INFINIBAND)
  1639. ctx->has_smi = 1;
  1640. if (ctx->has_smi) {
  1641. ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_SMI, create_tun);
  1642. if (ret) {
  1643. pr_err("Failed allocating qp0 tunnel bufs (%d)\n", ret);
  1644. goto err_out;
  1645. }
  1646. }
  1647. ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_GSI, create_tun);
  1648. if (ret) {
  1649. pr_err("Failed allocating qp1 tunnel bufs (%d)\n", ret);
  1650. goto err_out_qp0;
  1651. }
  1652. cq_size = 2 * MLX4_NUM_TUNNEL_BUFS;
  1653. if (ctx->has_smi)
  1654. cq_size *= 2;
  1655. cq_attr.cqe = cq_size;
  1656. ctx->cq = ib_create_cq(ctx->ib_dev, mlx4_ib_tunnel_comp_handler,
  1657. NULL, ctx, &cq_attr);
  1658. if (IS_ERR(ctx->cq)) {
  1659. ret = PTR_ERR(ctx->cq);
  1660. pr_err("Couldn't create tunnel CQ (%d)\n", ret);
  1661. goto err_buf;
  1662. }
  1663. ctx->pd = ib_alloc_pd(ctx->ib_dev);
  1664. if (IS_ERR(ctx->pd)) {
  1665. ret = PTR_ERR(ctx->pd);
  1666. pr_err("Couldn't create tunnel PD (%d)\n", ret);
  1667. goto err_cq;
  1668. }
  1669. if (ctx->has_smi) {
  1670. ret = create_pv_sqp(ctx, IB_QPT_SMI, create_tun);
  1671. if (ret) {
  1672. pr_err("Couldn't create %s QP0 (%d)\n",
  1673. create_tun ? "tunnel for" : "", ret);
  1674. goto err_pd;
  1675. }
  1676. }
  1677. ret = create_pv_sqp(ctx, IB_QPT_GSI, create_tun);
  1678. if (ret) {
  1679. pr_err("Couldn't create %s QP1 (%d)\n",
  1680. create_tun ? "tunnel for" : "", ret);
  1681. goto err_qp0;
  1682. }
  1683. if (create_tun)
  1684. INIT_WORK(&ctx->work, mlx4_ib_tunnel_comp_worker);
  1685. else
  1686. INIT_WORK(&ctx->work, mlx4_ib_sqp_comp_worker);
  1687. ctx->wq = to_mdev(ibdev)->sriov.demux[port - 1].wq;
  1688. ret = ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
  1689. if (ret) {
  1690. pr_err("Couldn't arm tunnel cq (%d)\n", ret);
  1691. goto err_wq;
  1692. }
  1693. ctx->state = DEMUX_PV_STATE_ACTIVE;
  1694. return 0;
  1695. err_wq:
  1696. ctx->wq = NULL;
  1697. ib_destroy_qp(ctx->qp[1].qp);
  1698. ctx->qp[1].qp = NULL;
  1699. err_qp0:
  1700. if (ctx->has_smi)
  1701. ib_destroy_qp(ctx->qp[0].qp);
  1702. ctx->qp[0].qp = NULL;
  1703. err_pd:
  1704. ib_dealloc_pd(ctx->pd);
  1705. ctx->pd = NULL;
  1706. err_cq:
  1707. ib_destroy_cq(ctx->cq);
  1708. ctx->cq = NULL;
  1709. err_buf:
  1710. mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, create_tun);
  1711. err_out_qp0:
  1712. if (ctx->has_smi)
  1713. mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, create_tun);
  1714. err_out:
  1715. ctx->state = DEMUX_PV_STATE_DOWN;
  1716. return ret;
  1717. }
  1718. static void destroy_pv_resources(struct mlx4_ib_dev *dev, int slave, int port,
  1719. struct mlx4_ib_demux_pv_ctx *ctx, int flush)
  1720. {
  1721. if (!ctx)
  1722. return;
  1723. if (ctx->state > DEMUX_PV_STATE_DOWN) {
  1724. ctx->state = DEMUX_PV_STATE_DOWNING;
  1725. if (flush)
  1726. flush_workqueue(ctx->wq);
  1727. if (ctx->has_smi) {
  1728. ib_destroy_qp(ctx->qp[0].qp);
  1729. ctx->qp[0].qp = NULL;
  1730. mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, 1);
  1731. }
  1732. ib_destroy_qp(ctx->qp[1].qp);
  1733. ctx->qp[1].qp = NULL;
  1734. mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, 1);
  1735. ib_dealloc_pd(ctx->pd);
  1736. ctx->pd = NULL;
  1737. ib_destroy_cq(ctx->cq);
  1738. ctx->cq = NULL;
  1739. ctx->state = DEMUX_PV_STATE_DOWN;
  1740. }
  1741. }
  1742. static int mlx4_ib_tunnels_update(struct mlx4_ib_dev *dev, int slave,
  1743. int port, int do_init)
  1744. {
  1745. int ret = 0;
  1746. if (!do_init) {
  1747. clean_vf_mcast(&dev->sriov.demux[port - 1], slave);
  1748. /* for master, destroy real sqp resources */
  1749. if (slave == mlx4_master_func_num(dev->dev))
  1750. destroy_pv_resources(dev, slave, port,
  1751. dev->sriov.sqps[port - 1], 1);
  1752. /* destroy the tunnel qp resources */
  1753. destroy_pv_resources(dev, slave, port,
  1754. dev->sriov.demux[port - 1].tun[slave], 1);
  1755. return 0;
  1756. }
  1757. /* create the tunnel qp resources */
  1758. ret = create_pv_resources(&dev->ib_dev, slave, port, 1,
  1759. dev->sriov.demux[port - 1].tun[slave]);
  1760. /* for master, create the real sqp resources */
  1761. if (!ret && slave == mlx4_master_func_num(dev->dev))
  1762. ret = create_pv_resources(&dev->ib_dev, slave, port, 0,
  1763. dev->sriov.sqps[port - 1]);
  1764. return ret;
  1765. }
  1766. void mlx4_ib_tunnels_update_work(struct work_struct *work)
  1767. {
  1768. struct mlx4_ib_demux_work *dmxw;
  1769. dmxw = container_of(work, struct mlx4_ib_demux_work, work);
  1770. mlx4_ib_tunnels_update(dmxw->dev, dmxw->slave, (int) dmxw->port,
  1771. dmxw->do_init);
  1772. kfree(dmxw);
  1773. return;
  1774. }
  1775. static int mlx4_ib_alloc_demux_ctx(struct mlx4_ib_dev *dev,
  1776. struct mlx4_ib_demux_ctx *ctx,
  1777. int port)
  1778. {
  1779. char name[12];
  1780. int ret = 0;
  1781. int i;
  1782. ctx->tun = kcalloc(dev->dev->caps.sqp_demux,
  1783. sizeof (struct mlx4_ib_demux_pv_ctx *), GFP_KERNEL);
  1784. if (!ctx->tun)
  1785. return -ENOMEM;
  1786. ctx->dev = dev;
  1787. ctx->port = port;
  1788. ctx->ib_dev = &dev->ib_dev;
  1789. for (i = 0;
  1790. i < min(dev->dev->caps.sqp_demux,
  1791. (u16)(dev->dev->persist->num_vfs + 1));
  1792. i++) {
  1793. struct mlx4_active_ports actv_ports =
  1794. mlx4_get_active_ports(dev->dev, i);
  1795. if (!test_bit(port - 1, actv_ports.ports))
  1796. continue;
  1797. ret = alloc_pv_object(dev, i, port, &ctx->tun[i]);
  1798. if (ret) {
  1799. ret = -ENOMEM;
  1800. goto err_mcg;
  1801. }
  1802. }
  1803. ret = mlx4_ib_mcg_port_init(ctx);
  1804. if (ret) {
  1805. pr_err("Failed initializing mcg para-virt (%d)\n", ret);
  1806. goto err_mcg;
  1807. }
  1808. snprintf(name, sizeof name, "mlx4_ibt%d", port);
  1809. ctx->wq = create_singlethread_workqueue(name);
  1810. if (!ctx->wq) {
  1811. pr_err("Failed to create tunnelling WQ for port %d\n", port);
  1812. ret = -ENOMEM;
  1813. goto err_wq;
  1814. }
  1815. snprintf(name, sizeof name, "mlx4_ibud%d", port);
  1816. ctx->ud_wq = create_singlethread_workqueue(name);
  1817. if (!ctx->ud_wq) {
  1818. pr_err("Failed to create up/down WQ for port %d\n", port);
  1819. ret = -ENOMEM;
  1820. goto err_udwq;
  1821. }
  1822. return 0;
  1823. err_udwq:
  1824. destroy_workqueue(ctx->wq);
  1825. ctx->wq = NULL;
  1826. err_wq:
  1827. mlx4_ib_mcg_port_cleanup(ctx, 1);
  1828. err_mcg:
  1829. for (i = 0; i < dev->dev->caps.sqp_demux; i++)
  1830. free_pv_object(dev, i, port);
  1831. kfree(ctx->tun);
  1832. ctx->tun = NULL;
  1833. return ret;
  1834. }
  1835. static void mlx4_ib_free_sqp_ctx(struct mlx4_ib_demux_pv_ctx *sqp_ctx)
  1836. {
  1837. if (sqp_ctx->state > DEMUX_PV_STATE_DOWN) {
  1838. sqp_ctx->state = DEMUX_PV_STATE_DOWNING;
  1839. flush_workqueue(sqp_ctx->wq);
  1840. if (sqp_ctx->has_smi) {
  1841. ib_destroy_qp(sqp_ctx->qp[0].qp);
  1842. sqp_ctx->qp[0].qp = NULL;
  1843. mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_SMI, 0);
  1844. }
  1845. ib_destroy_qp(sqp_ctx->qp[1].qp);
  1846. sqp_ctx->qp[1].qp = NULL;
  1847. mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_GSI, 0);
  1848. ib_dealloc_pd(sqp_ctx->pd);
  1849. sqp_ctx->pd = NULL;
  1850. ib_destroy_cq(sqp_ctx->cq);
  1851. sqp_ctx->cq = NULL;
  1852. sqp_ctx->state = DEMUX_PV_STATE_DOWN;
  1853. }
  1854. }
  1855. static void mlx4_ib_free_demux_ctx(struct mlx4_ib_demux_ctx *ctx)
  1856. {
  1857. int i;
  1858. if (ctx) {
  1859. struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
  1860. mlx4_ib_mcg_port_cleanup(ctx, 1);
  1861. for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
  1862. if (!ctx->tun[i])
  1863. continue;
  1864. if (ctx->tun[i]->state > DEMUX_PV_STATE_DOWN)
  1865. ctx->tun[i]->state = DEMUX_PV_STATE_DOWNING;
  1866. }
  1867. flush_workqueue(ctx->wq);
  1868. for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
  1869. destroy_pv_resources(dev, i, ctx->port, ctx->tun[i], 0);
  1870. free_pv_object(dev, i, ctx->port);
  1871. }
  1872. kfree(ctx->tun);
  1873. destroy_workqueue(ctx->ud_wq);
  1874. destroy_workqueue(ctx->wq);
  1875. }
  1876. }
  1877. static void mlx4_ib_master_tunnels(struct mlx4_ib_dev *dev, int do_init)
  1878. {
  1879. int i;
  1880. if (!mlx4_is_master(dev->dev))
  1881. return;
  1882. /* initialize or tear down tunnel QPs for the master */
  1883. for (i = 0; i < dev->dev->caps.num_ports; i++)
  1884. mlx4_ib_tunnels_update(dev, mlx4_master_func_num(dev->dev), i + 1, do_init);
  1885. return;
  1886. }
  1887. int mlx4_ib_init_sriov(struct mlx4_ib_dev *dev)
  1888. {
  1889. int i = 0;
  1890. int err;
  1891. if (!mlx4_is_mfunc(dev->dev))
  1892. return 0;
  1893. dev->sriov.is_going_down = 0;
  1894. spin_lock_init(&dev->sriov.going_down_lock);
  1895. mlx4_ib_cm_paravirt_init(dev);
  1896. mlx4_ib_warn(&dev->ib_dev, "multi-function enabled\n");
  1897. if (mlx4_is_slave(dev->dev)) {
  1898. mlx4_ib_warn(&dev->ib_dev, "operating in qp1 tunnel mode\n");
  1899. return 0;
  1900. }
  1901. for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
  1902. if (i == mlx4_master_func_num(dev->dev))
  1903. mlx4_put_slave_node_guid(dev->dev, i, dev->ib_dev.node_guid);
  1904. else
  1905. mlx4_put_slave_node_guid(dev->dev, i, mlx4_ib_gen_node_guid());
  1906. }
  1907. err = mlx4_ib_init_alias_guid_service(dev);
  1908. if (err) {
  1909. mlx4_ib_warn(&dev->ib_dev, "Failed init alias guid process.\n");
  1910. goto paravirt_err;
  1911. }
  1912. err = mlx4_ib_device_register_sysfs(dev);
  1913. if (err) {
  1914. mlx4_ib_warn(&dev->ib_dev, "Failed to register sysfs\n");
  1915. goto sysfs_err;
  1916. }
  1917. mlx4_ib_warn(&dev->ib_dev, "initializing demux service for %d qp1 clients\n",
  1918. dev->dev->caps.sqp_demux);
  1919. for (i = 0; i < dev->num_ports; i++) {
  1920. union ib_gid gid;
  1921. err = __mlx4_ib_query_gid(&dev->ib_dev, i + 1, 0, &gid, 1);
  1922. if (err)
  1923. goto demux_err;
  1924. dev->sriov.demux[i].guid_cache[0] = gid.global.interface_id;
  1925. err = alloc_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1,
  1926. &dev->sriov.sqps[i]);
  1927. if (err)
  1928. goto demux_err;
  1929. err = mlx4_ib_alloc_demux_ctx(dev, &dev->sriov.demux[i], i + 1);
  1930. if (err)
  1931. goto free_pv;
  1932. }
  1933. mlx4_ib_master_tunnels(dev, 1);
  1934. return 0;
  1935. free_pv:
  1936. free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
  1937. demux_err:
  1938. while (--i >= 0) {
  1939. free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
  1940. mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
  1941. }
  1942. mlx4_ib_device_unregister_sysfs(dev);
  1943. sysfs_err:
  1944. mlx4_ib_destroy_alias_guid_service(dev);
  1945. paravirt_err:
  1946. mlx4_ib_cm_paravirt_clean(dev, -1);
  1947. return err;
  1948. }
  1949. void mlx4_ib_close_sriov(struct mlx4_ib_dev *dev)
  1950. {
  1951. int i;
  1952. unsigned long flags;
  1953. if (!mlx4_is_mfunc(dev->dev))
  1954. return;
  1955. spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
  1956. dev->sriov.is_going_down = 1;
  1957. spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
  1958. if (mlx4_is_master(dev->dev)) {
  1959. for (i = 0; i < dev->num_ports; i++) {
  1960. flush_workqueue(dev->sriov.demux[i].ud_wq);
  1961. mlx4_ib_free_sqp_ctx(dev->sriov.sqps[i]);
  1962. kfree(dev->sriov.sqps[i]);
  1963. dev->sriov.sqps[i] = NULL;
  1964. mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
  1965. }
  1966. mlx4_ib_cm_paravirt_clean(dev, -1);
  1967. mlx4_ib_destroy_alias_guid_service(dev);
  1968. mlx4_ib_device_unregister_sysfs(dev);
  1969. }
  1970. }