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