mad.c 62 KB

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