mad.c 67 KB

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