mad.c 67 KB

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