mad.c 66 KB

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