mad.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350
  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. if (grh->dgid.global.interface_id ==
  609. cpu_to_be64(IB_SA_WELL_KNOWN_GUID) &&
  610. grh->dgid.global.subnet_prefix == cpu_to_be64(
  611. atomic64_read(&dev->sriov.demux[port - 1].subnet_prefix))) {
  612. slave = 0;
  613. } else {
  614. slave = mlx4_ib_find_real_gid(ibdev, port,
  615. grh->dgid.global.interface_id);
  616. if (slave < 0) {
  617. mlx4_ib_warn(ibdev, "failed matching grh\n");
  618. return -ENOENT;
  619. }
  620. }
  621. }
  622. /* Class-specific handling */
  623. switch (mad->mad_hdr.mgmt_class) {
  624. case IB_MGMT_CLASS_SUBN_LID_ROUTED:
  625. case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
  626. /* 255 indicates the dom0 */
  627. if (slave != 255 && slave != mlx4_master_func_num(dev->dev)) {
  628. if (!mlx4_vf_smi_enabled(dev->dev, slave, port))
  629. return -EPERM;
  630. /* for a VF. drop unsolicited MADs */
  631. if (!(mad->mad_hdr.method & IB_MGMT_METHOD_RESP)) {
  632. mlx4_ib_warn(ibdev, "demux QP0. rejecting unsolicited mad for slave %d class 0x%x, method 0x%x\n",
  633. slave, mad->mad_hdr.mgmt_class,
  634. mad->mad_hdr.method);
  635. return -EINVAL;
  636. }
  637. }
  638. break;
  639. case IB_MGMT_CLASS_SUBN_ADM:
  640. if (mlx4_ib_demux_sa_handler(ibdev, port, slave,
  641. (struct ib_sa_mad *) mad))
  642. return 0;
  643. break;
  644. case IB_MGMT_CLASS_CM:
  645. if (mlx4_ib_demux_cm_handler(ibdev, port, &slave, mad))
  646. return 0;
  647. break;
  648. case IB_MGMT_CLASS_DEVICE_MGMT:
  649. if (mad->mad_hdr.method != IB_MGMT_METHOD_GET_RESP)
  650. return 0;
  651. break;
  652. default:
  653. /* Drop unsupported classes for slaves in tunnel mode */
  654. if (slave != mlx4_master_func_num(dev->dev)) {
  655. pr_debug("dropping unsupported ingress mad from class:%d "
  656. "for slave:%d\n", mad->mad_hdr.mgmt_class, slave);
  657. return 0;
  658. }
  659. }
  660. /*make sure that no slave==255 was not handled yet.*/
  661. if (slave >= dev->dev->caps.sqp_demux) {
  662. mlx4_ib_warn(ibdev, "slave id: %d is bigger than allowed:%d\n",
  663. slave, dev->dev->caps.sqp_demux);
  664. return -ENOENT;
  665. }
  666. err = mlx4_ib_send_to_slave(dev, slave, port, wc->qp->qp_type, wc, grh, mad);
  667. if (err)
  668. pr_debug("failed sending to slave %d via tunnel qp (%d)\n",
  669. slave, err);
  670. return 0;
  671. }
  672. static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  673. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  674. const struct ib_mad *in_mad, struct ib_mad *out_mad)
  675. {
  676. u16 slid, prev_lid = 0;
  677. int err;
  678. struct ib_port_attr pattr;
  679. if (in_wc && in_wc->qp->qp_num) {
  680. pr_debug("received MAD: slid:%d sqpn:%d "
  681. "dlid_bits:%d dqpn:%d wc_flags:0x%x, cls %x, mtd %x, atr %x\n",
  682. in_wc->slid, in_wc->src_qp,
  683. in_wc->dlid_path_bits,
  684. in_wc->qp->qp_num,
  685. in_wc->wc_flags,
  686. in_mad->mad_hdr.mgmt_class, in_mad->mad_hdr.method,
  687. be16_to_cpu(in_mad->mad_hdr.attr_id));
  688. if (in_wc->wc_flags & IB_WC_GRH) {
  689. pr_debug("sgid_hi:0x%016llx sgid_lo:0x%016llx\n",
  690. be64_to_cpu(in_grh->sgid.global.subnet_prefix),
  691. be64_to_cpu(in_grh->sgid.global.interface_id));
  692. pr_debug("dgid_hi:0x%016llx dgid_lo:0x%016llx\n",
  693. be64_to_cpu(in_grh->dgid.global.subnet_prefix),
  694. be64_to_cpu(in_grh->dgid.global.interface_id));
  695. }
  696. }
  697. slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
  698. if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
  699. forward_trap(to_mdev(ibdev), port_num, in_mad);
  700. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  701. }
  702. if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  703. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  704. if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
  705. in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&
  706. in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
  707. return IB_MAD_RESULT_SUCCESS;
  708. /*
  709. * Don't process SMInfo queries -- the SMA can't handle them.
  710. */
  711. if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
  712. return IB_MAD_RESULT_SUCCESS;
  713. } else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
  714. in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1 ||
  715. in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2 ||
  716. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
  717. if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
  718. in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)
  719. return IB_MAD_RESULT_SUCCESS;
  720. } else
  721. return IB_MAD_RESULT_SUCCESS;
  722. if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
  723. in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
  724. in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
  725. in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
  726. !ib_query_port(ibdev, port_num, &pattr))
  727. prev_lid = pattr.lid;
  728. err = mlx4_MAD_IFC(to_mdev(ibdev),
  729. (mad_flags & IB_MAD_IGNORE_MKEY ? MLX4_MAD_IFC_IGNORE_MKEY : 0) |
  730. (mad_flags & IB_MAD_IGNORE_BKEY ? MLX4_MAD_IFC_IGNORE_BKEY : 0) |
  731. MLX4_MAD_IFC_NET_VIEW,
  732. port_num, in_wc, in_grh, in_mad, out_mad);
  733. if (err)
  734. return IB_MAD_RESULT_FAILURE;
  735. if (!out_mad->mad_hdr.status) {
  736. smp_snoop(ibdev, port_num, in_mad, prev_lid);
  737. /* slaves get node desc from FW */
  738. if (!mlx4_is_slave(to_mdev(ibdev)->dev))
  739. node_desc_override(ibdev, out_mad);
  740. }
  741. /* set return bit in status of directed route responses */
  742. if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  743. out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
  744. if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
  745. /* no response for trap repress */
  746. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
  747. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  748. }
  749. static void edit_counter(struct mlx4_counter *cnt, void *counters,
  750. __be16 attr_id)
  751. {
  752. switch (attr_id) {
  753. case IB_PMA_PORT_COUNTERS:
  754. {
  755. struct ib_pma_portcounters *pma_cnt =
  756. (struct ib_pma_portcounters *)counters;
  757. ASSIGN_32BIT_COUNTER(pma_cnt->port_xmit_data,
  758. (be64_to_cpu(cnt->tx_bytes) >> 2));
  759. ASSIGN_32BIT_COUNTER(pma_cnt->port_rcv_data,
  760. (be64_to_cpu(cnt->rx_bytes) >> 2));
  761. ASSIGN_32BIT_COUNTER(pma_cnt->port_xmit_packets,
  762. be64_to_cpu(cnt->tx_frames));
  763. ASSIGN_32BIT_COUNTER(pma_cnt->port_rcv_packets,
  764. be64_to_cpu(cnt->rx_frames));
  765. break;
  766. }
  767. case IB_PMA_PORT_COUNTERS_EXT:
  768. {
  769. struct ib_pma_portcounters_ext *pma_cnt_ext =
  770. (struct ib_pma_portcounters_ext *)counters;
  771. pma_cnt_ext->port_xmit_data =
  772. cpu_to_be64(be64_to_cpu(cnt->tx_bytes) >> 2);
  773. pma_cnt_ext->port_rcv_data =
  774. cpu_to_be64(be64_to_cpu(cnt->rx_bytes) >> 2);
  775. pma_cnt_ext->port_xmit_packets = cnt->tx_frames;
  776. pma_cnt_ext->port_rcv_packets = cnt->rx_frames;
  777. break;
  778. }
  779. }
  780. }
  781. static int iboe_process_mad_port_info(void *out_mad)
  782. {
  783. struct ib_class_port_info cpi = {};
  784. cpi.capability_mask = IB_PMA_CLASS_CAP_EXT_WIDTH;
  785. memcpy(out_mad, &cpi, sizeof(cpi));
  786. return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  787. }
  788. static int iboe_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  789. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  790. const struct ib_mad *in_mad, struct ib_mad *out_mad)
  791. {
  792. struct mlx4_counter counter_stats;
  793. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  794. struct counter_index *tmp_counter;
  795. int err = IB_MAD_RESULT_FAILURE, stats_avail = 0;
  796. if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_PERF_MGMT)
  797. return -EINVAL;
  798. if (in_mad->mad_hdr.attr_id == IB_PMA_CLASS_PORT_INFO)
  799. return iboe_process_mad_port_info((void *)(out_mad->data + 40));
  800. memset(&counter_stats, 0, sizeof(counter_stats));
  801. mutex_lock(&dev->counters_table[port_num - 1].mutex);
  802. list_for_each_entry(tmp_counter,
  803. &dev->counters_table[port_num - 1].counters_list,
  804. list) {
  805. err = mlx4_get_counter_stats(dev->dev,
  806. tmp_counter->index,
  807. &counter_stats, 0);
  808. if (err) {
  809. err = IB_MAD_RESULT_FAILURE;
  810. stats_avail = 0;
  811. break;
  812. }
  813. stats_avail = 1;
  814. }
  815. mutex_unlock(&dev->counters_table[port_num - 1].mutex);
  816. if (stats_avail) {
  817. memset(out_mad->data, 0, sizeof out_mad->data);
  818. switch (counter_stats.counter_mode & 0xf) {
  819. case 0:
  820. edit_counter(&counter_stats,
  821. (void *)(out_mad->data + 40),
  822. in_mad->mad_hdr.attr_id);
  823. err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
  824. break;
  825. default:
  826. err = IB_MAD_RESULT_FAILURE;
  827. }
  828. }
  829. return err;
  830. }
  831. int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
  832. const struct ib_wc *in_wc, const struct ib_grh *in_grh,
  833. const struct ib_mad_hdr *in, size_t in_mad_size,
  834. struct ib_mad_hdr *out, size_t *out_mad_size,
  835. u16 *out_mad_pkey_index)
  836. {
  837. struct mlx4_ib_dev *dev = to_mdev(ibdev);
  838. const struct ib_mad *in_mad = (const struct ib_mad *)in;
  839. struct ib_mad *out_mad = (struct ib_mad *)out;
  840. enum rdma_link_layer link = rdma_port_get_link_layer(ibdev, port_num);
  841. if (WARN_ON_ONCE(in_mad_size != sizeof(*in_mad) ||
  842. *out_mad_size != sizeof(*out_mad)))
  843. return IB_MAD_RESULT_FAILURE;
  844. /* iboe_process_mad() which uses the HCA flow-counters to implement IB PMA
  845. * queries, should be called only by VFs and for that specific purpose
  846. */
  847. if (link == IB_LINK_LAYER_INFINIBAND) {
  848. if (mlx4_is_slave(dev->dev) &&
  849. (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT &&
  850. (in_mad->mad_hdr.attr_id == IB_PMA_PORT_COUNTERS ||
  851. in_mad->mad_hdr.attr_id == IB_PMA_PORT_COUNTERS_EXT ||
  852. in_mad->mad_hdr.attr_id == IB_PMA_CLASS_PORT_INFO)))
  853. return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
  854. in_grh, in_mad, out_mad);
  855. return ib_process_mad(ibdev, mad_flags, port_num, in_wc,
  856. in_grh, in_mad, out_mad);
  857. }
  858. if (link == IB_LINK_LAYER_ETHERNET)
  859. return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
  860. in_grh, in_mad, out_mad);
  861. return -EINVAL;
  862. }
  863. static void send_handler(struct ib_mad_agent *agent,
  864. struct ib_mad_send_wc *mad_send_wc)
  865. {
  866. if (mad_send_wc->send_buf->context[0])
  867. ib_destroy_ah(mad_send_wc->send_buf->context[0]);
  868. ib_free_send_mad(mad_send_wc->send_buf);
  869. }
  870. int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
  871. {
  872. struct ib_mad_agent *agent;
  873. int p, q;
  874. int ret;
  875. enum rdma_link_layer ll;
  876. for (p = 0; p < dev->num_ports; ++p) {
  877. ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);
  878. for (q = 0; q <= 1; ++q) {
  879. if (ll == IB_LINK_LAYER_INFINIBAND) {
  880. agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
  881. q ? IB_QPT_GSI : IB_QPT_SMI,
  882. NULL, 0, send_handler,
  883. NULL, NULL, 0);
  884. if (IS_ERR(agent)) {
  885. ret = PTR_ERR(agent);
  886. goto err;
  887. }
  888. dev->send_agent[p][q] = agent;
  889. } else
  890. dev->send_agent[p][q] = NULL;
  891. }
  892. }
  893. return 0;
  894. err:
  895. for (p = 0; p < dev->num_ports; ++p)
  896. for (q = 0; q <= 1; ++q)
  897. if (dev->send_agent[p][q])
  898. ib_unregister_mad_agent(dev->send_agent[p][q]);
  899. return ret;
  900. }
  901. void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
  902. {
  903. struct ib_mad_agent *agent;
  904. int p, q;
  905. for (p = 0; p < dev->num_ports; ++p) {
  906. for (q = 0; q <= 1; ++q) {
  907. agent = dev->send_agent[p][q];
  908. if (agent) {
  909. dev->send_agent[p][q] = NULL;
  910. ib_unregister_mad_agent(agent);
  911. }
  912. }
  913. if (dev->sm_ah[p])
  914. ib_destroy_ah(dev->sm_ah[p]);
  915. }
  916. }
  917. static void handle_lid_change_event(struct mlx4_ib_dev *dev, u8 port_num)
  918. {
  919. mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_LID_CHANGE);
  920. if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
  921. mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
  922. MLX4_EQ_PORT_INFO_LID_CHANGE_MASK);
  923. }
  924. static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num)
  925. {
  926. /* re-configure the alias-guid and mcg's */
  927. if (mlx4_is_master(dev->dev)) {
  928. mlx4_ib_invalidate_all_guid_record(dev, port_num);
  929. if (!dev->sriov.is_going_down) {
  930. mlx4_ib_mcg_port_cleanup(&dev->sriov.demux[port_num - 1], 0);
  931. mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
  932. MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK);
  933. }
  934. }
  935. /* Update the sl to vl table from inside client rereg
  936. * only if in secure-host mode (snooping is not possible)
  937. * and the sl-to-vl change event is not generated by FW.
  938. */
  939. if (!mlx4_is_slave(dev->dev) &&
  940. dev->dev->flags & MLX4_FLAG_SECURE_HOST &&
  941. !(dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SL_TO_VL_CHANGE_EVENT)) {
  942. if (mlx4_is_master(dev->dev))
  943. /* already in work queue from mlx4_ib_event queueing
  944. * mlx4_handle_port_mgmt_change_event, which calls
  945. * this procedure. Therefore, call sl2vl_update directly.
  946. */
  947. mlx4_ib_sl2vl_update(dev, port_num);
  948. else
  949. mlx4_sched_ib_sl2vl_update_work(dev, port_num);
  950. }
  951. mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_CLIENT_REREGISTER);
  952. }
  953. static void propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
  954. struct mlx4_eqe *eqe)
  955. {
  956. __propagate_pkey_ev(dev, port_num, GET_BLK_PTR_FROM_EQE(eqe),
  957. GET_MASK_FROM_EQE(eqe));
  958. }
  959. static void handle_slaves_guid_change(struct mlx4_ib_dev *dev, u8 port_num,
  960. u32 guid_tbl_blk_num, u32 change_bitmap)
  961. {
  962. struct ib_smp *in_mad = NULL;
  963. struct ib_smp *out_mad = NULL;
  964. u16 i;
  965. if (!mlx4_is_mfunc(dev->dev) || !mlx4_is_master(dev->dev))
  966. return;
  967. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  968. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  969. if (!in_mad || !out_mad) {
  970. mlx4_ib_warn(&dev->ib_dev, "failed to allocate memory for guid info mads\n");
  971. goto out;
  972. }
  973. guid_tbl_blk_num *= 4;
  974. for (i = 0; i < 4; i++) {
  975. if (change_bitmap && (!((change_bitmap >> (8 * i)) & 0xff)))
  976. continue;
  977. memset(in_mad, 0, sizeof *in_mad);
  978. memset(out_mad, 0, sizeof *out_mad);
  979. in_mad->base_version = 1;
  980. in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  981. in_mad->class_version = 1;
  982. in_mad->method = IB_MGMT_METHOD_GET;
  983. in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
  984. in_mad->attr_mod = cpu_to_be32(guid_tbl_blk_num + i);
  985. if (mlx4_MAD_IFC(dev,
  986. MLX4_MAD_IFC_IGNORE_KEYS | MLX4_MAD_IFC_NET_VIEW,
  987. port_num, NULL, NULL, in_mad, out_mad)) {
  988. mlx4_ib_warn(&dev->ib_dev, "Failed in get GUID INFO MAD_IFC\n");
  989. goto out;
  990. }
  991. mlx4_ib_update_cache_on_guid_change(dev, guid_tbl_blk_num + i,
  992. port_num,
  993. (u8 *)(&((struct ib_smp *)out_mad)->data));
  994. mlx4_ib_notify_slaves_on_guid_change(dev, guid_tbl_blk_num + i,
  995. port_num,
  996. (u8 *)(&((struct ib_smp *)out_mad)->data));
  997. }
  998. out:
  999. kfree(in_mad);
  1000. kfree(out_mad);
  1001. return;
  1002. }
  1003. void handle_port_mgmt_change_event(struct work_struct *work)
  1004. {
  1005. struct ib_event_work *ew = container_of(work, struct ib_event_work, work);
  1006. struct mlx4_ib_dev *dev = ew->ib_dev;
  1007. struct mlx4_eqe *eqe = &(ew->ib_eqe);
  1008. u8 port = eqe->event.port_mgmt_change.port;
  1009. u32 changed_attr;
  1010. u32 tbl_block;
  1011. u32 change_bitmap;
  1012. switch (eqe->subtype) {
  1013. case MLX4_DEV_PMC_SUBTYPE_PORT_INFO:
  1014. changed_attr = be32_to_cpu(eqe->event.port_mgmt_change.params.port_info.changed_attr);
  1015. /* Update the SM ah - This should be done before handling
  1016. the other changed attributes so that MADs can be sent to the SM */
  1017. if (changed_attr & MSTR_SM_CHANGE_MASK) {
  1018. u16 lid = be16_to_cpu(eqe->event.port_mgmt_change.params.port_info.mstr_sm_lid);
  1019. u8 sl = eqe->event.port_mgmt_change.params.port_info.mstr_sm_sl & 0xf;
  1020. update_sm_ah(dev, port, lid, sl);
  1021. }
  1022. /* Check if it is a lid change event */
  1023. if (changed_attr & MLX4_EQ_PORT_INFO_LID_CHANGE_MASK)
  1024. handle_lid_change_event(dev, port);
  1025. /* Generate GUID changed event */
  1026. if (changed_attr & MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK) {
  1027. if (mlx4_is_master(dev->dev)) {
  1028. union ib_gid gid;
  1029. int err = 0;
  1030. if (!eqe->event.port_mgmt_change.params.port_info.gid_prefix)
  1031. err = __mlx4_ib_query_gid(&dev->ib_dev, port, 0, &gid, 1);
  1032. else
  1033. gid.global.subnet_prefix =
  1034. eqe->event.port_mgmt_change.params.port_info.gid_prefix;
  1035. if (err) {
  1036. pr_warn("Could not change QP1 subnet prefix for port %d: query_gid error (%d)\n",
  1037. port, err);
  1038. } else {
  1039. pr_debug("Changing QP1 subnet prefix for port %d. old=0x%llx. new=0x%llx\n",
  1040. port,
  1041. (u64)atomic64_read(&dev->sriov.demux[port - 1].subnet_prefix),
  1042. be64_to_cpu(gid.global.subnet_prefix));
  1043. atomic64_set(&dev->sriov.demux[port - 1].subnet_prefix,
  1044. be64_to_cpu(gid.global.subnet_prefix));
  1045. }
  1046. }
  1047. mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
  1048. /*if master, notify all slaves*/
  1049. if (mlx4_is_master(dev->dev))
  1050. mlx4_gen_slaves_port_mgt_ev(dev->dev, port,
  1051. MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK);
  1052. }
  1053. if (changed_attr & MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK)
  1054. handle_client_rereg_event(dev, port);
  1055. break;
  1056. case MLX4_DEV_PMC_SUBTYPE_PKEY_TABLE:
  1057. mlx4_ib_dispatch_event(dev, port, IB_EVENT_PKEY_CHANGE);
  1058. if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
  1059. propagate_pkey_ev(dev, port, eqe);
  1060. break;
  1061. case MLX4_DEV_PMC_SUBTYPE_GUID_INFO:
  1062. /* paravirtualized master's guid is guid 0 -- does not change */
  1063. if (!mlx4_is_master(dev->dev))
  1064. mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
  1065. /*if master, notify relevant slaves*/
  1066. else if (!dev->sriov.is_going_down) {
  1067. tbl_block = GET_BLK_PTR_FROM_EQE(eqe);
  1068. change_bitmap = GET_MASK_FROM_EQE(eqe);
  1069. handle_slaves_guid_change(dev, port, tbl_block, change_bitmap);
  1070. }
  1071. break;
  1072. case MLX4_DEV_PMC_SUBTYPE_SL_TO_VL_MAP:
  1073. /* cache sl to vl mapping changes for use in
  1074. * filling QP1 LRH VL field when sending packets
  1075. */
  1076. if (!mlx4_is_slave(dev->dev)) {
  1077. union sl2vl_tbl_to_u64 sl2vl64;
  1078. int jj;
  1079. for (jj = 0; jj < 8; jj++) {
  1080. sl2vl64.sl8[jj] =
  1081. eqe->event.port_mgmt_change.params.sl2vl_tbl_change_info.sl2vl_table[jj];
  1082. pr_debug("port %u, sl2vl[%d] = %02x\n",
  1083. port, jj, sl2vl64.sl8[jj]);
  1084. }
  1085. atomic64_set(&dev->sl2vl[port - 1], sl2vl64.sl64);
  1086. }
  1087. break;
  1088. default:
  1089. pr_warn("Unsupported subtype 0x%x for "
  1090. "Port Management Change event\n", eqe->subtype);
  1091. }
  1092. kfree(ew);
  1093. }
  1094. void mlx4_ib_dispatch_event(struct mlx4_ib_dev *dev, u8 port_num,
  1095. enum ib_event_type type)
  1096. {
  1097. struct ib_event event;
  1098. event.device = &dev->ib_dev;
  1099. event.element.port_num = port_num;
  1100. event.event = type;
  1101. ib_dispatch_event(&event);
  1102. }
  1103. static void mlx4_ib_tunnel_comp_handler(struct ib_cq *cq, void *arg)
  1104. {
  1105. unsigned long flags;
  1106. struct mlx4_ib_demux_pv_ctx *ctx = cq->cq_context;
  1107. struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
  1108. spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
  1109. if (!dev->sriov.is_going_down && ctx->state == DEMUX_PV_STATE_ACTIVE)
  1110. queue_work(ctx->wq, &ctx->work);
  1111. spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
  1112. }
  1113. static int mlx4_ib_post_pv_qp_buf(struct mlx4_ib_demux_pv_ctx *ctx,
  1114. struct mlx4_ib_demux_pv_qp *tun_qp,
  1115. int index)
  1116. {
  1117. struct ib_sge sg_list;
  1118. struct ib_recv_wr recv_wr, *bad_recv_wr;
  1119. int size;
  1120. size = (tun_qp->qp->qp_type == IB_QPT_UD) ?
  1121. sizeof (struct mlx4_tunnel_mad) : sizeof (struct mlx4_mad_rcv_buf);
  1122. sg_list.addr = tun_qp->ring[index].map;
  1123. sg_list.length = size;
  1124. sg_list.lkey = ctx->pd->local_dma_lkey;
  1125. recv_wr.next = NULL;
  1126. recv_wr.sg_list = &sg_list;
  1127. recv_wr.num_sge = 1;
  1128. recv_wr.wr_id = (u64) index | MLX4_TUN_WRID_RECV |
  1129. MLX4_TUN_SET_WRID_QPN(tun_qp->proxy_qpt);
  1130. ib_dma_sync_single_for_device(ctx->ib_dev, tun_qp->ring[index].map,
  1131. size, DMA_FROM_DEVICE);
  1132. return ib_post_recv(tun_qp->qp, &recv_wr, &bad_recv_wr);
  1133. }
  1134. static int mlx4_ib_multiplex_sa_handler(struct ib_device *ibdev, int port,
  1135. int slave, struct ib_sa_mad *sa_mad)
  1136. {
  1137. int ret = 0;
  1138. /* dispatch to different sa handlers */
  1139. switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
  1140. case IB_SA_ATTR_MC_MEMBER_REC:
  1141. ret = mlx4_ib_mcg_multiplex_handler(ibdev, port, slave, sa_mad);
  1142. break;
  1143. default:
  1144. break;
  1145. }
  1146. return ret;
  1147. }
  1148. static int is_proxy_qp0(struct mlx4_ib_dev *dev, int qpn, int slave)
  1149. {
  1150. int proxy_start = dev->dev->phys_caps.base_proxy_sqpn + 8 * slave;
  1151. return (qpn >= proxy_start && qpn <= proxy_start + 1);
  1152. }
  1153. int mlx4_ib_send_to_wire(struct mlx4_ib_dev *dev, int slave, u8 port,
  1154. enum ib_qp_type dest_qpt, u16 pkey_index,
  1155. u32 remote_qpn, u32 qkey, struct ib_ah_attr *attr,
  1156. u8 *s_mac, u16 vlan_id, struct ib_mad *mad)
  1157. {
  1158. struct ib_sge list;
  1159. struct ib_ud_wr wr;
  1160. struct ib_send_wr *bad_wr;
  1161. struct mlx4_ib_demux_pv_ctx *sqp_ctx;
  1162. struct mlx4_ib_demux_pv_qp *sqp;
  1163. struct mlx4_mad_snd_buf *sqp_mad;
  1164. struct ib_ah *ah;
  1165. struct ib_qp *send_qp = NULL;
  1166. unsigned wire_tx_ix = 0;
  1167. int ret = 0;
  1168. u16 wire_pkey_ix;
  1169. int src_qpnum;
  1170. u8 sgid_index;
  1171. sqp_ctx = dev->sriov.sqps[port-1];
  1172. /* check if proxy qp created */
  1173. if (!sqp_ctx || sqp_ctx->state != DEMUX_PV_STATE_ACTIVE)
  1174. return -EAGAIN;
  1175. if (dest_qpt == IB_QPT_SMI) {
  1176. src_qpnum = 0;
  1177. sqp = &sqp_ctx->qp[0];
  1178. wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
  1179. } else {
  1180. src_qpnum = 1;
  1181. sqp = &sqp_ctx->qp[1];
  1182. wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][pkey_index];
  1183. }
  1184. send_qp = sqp->qp;
  1185. /* create ah */
  1186. sgid_index = attr->grh.sgid_index;
  1187. attr->grh.sgid_index = 0;
  1188. ah = ib_create_ah(sqp_ctx->pd, attr);
  1189. if (IS_ERR(ah))
  1190. return -ENOMEM;
  1191. attr->grh.sgid_index = sgid_index;
  1192. to_mah(ah)->av.ib.gid_index = sgid_index;
  1193. /* get rid of force-loopback bit */
  1194. to_mah(ah)->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF);
  1195. spin_lock(&sqp->tx_lock);
  1196. if (sqp->tx_ix_head - sqp->tx_ix_tail >=
  1197. (MLX4_NUM_TUNNEL_BUFS - 1))
  1198. ret = -EAGAIN;
  1199. else
  1200. wire_tx_ix = (++sqp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
  1201. spin_unlock(&sqp->tx_lock);
  1202. if (ret)
  1203. goto out;
  1204. sqp_mad = (struct mlx4_mad_snd_buf *) (sqp->tx_ring[wire_tx_ix].buf.addr);
  1205. if (sqp->tx_ring[wire_tx_ix].ah)
  1206. ib_destroy_ah(sqp->tx_ring[wire_tx_ix].ah);
  1207. sqp->tx_ring[wire_tx_ix].ah = ah;
  1208. ib_dma_sync_single_for_cpu(&dev->ib_dev,
  1209. sqp->tx_ring[wire_tx_ix].buf.map,
  1210. sizeof (struct mlx4_mad_snd_buf),
  1211. DMA_TO_DEVICE);
  1212. memcpy(&sqp_mad->payload, mad, sizeof *mad);
  1213. ib_dma_sync_single_for_device(&dev->ib_dev,
  1214. sqp->tx_ring[wire_tx_ix].buf.map,
  1215. sizeof (struct mlx4_mad_snd_buf),
  1216. DMA_TO_DEVICE);
  1217. list.addr = sqp->tx_ring[wire_tx_ix].buf.map;
  1218. list.length = sizeof (struct mlx4_mad_snd_buf);
  1219. list.lkey = sqp_ctx->pd->local_dma_lkey;
  1220. wr.ah = ah;
  1221. wr.port_num = port;
  1222. wr.pkey_index = wire_pkey_ix;
  1223. wr.remote_qkey = qkey;
  1224. wr.remote_qpn = remote_qpn;
  1225. wr.wr.next = NULL;
  1226. wr.wr.wr_id = ((u64) wire_tx_ix) | MLX4_TUN_SET_WRID_QPN(src_qpnum);
  1227. wr.wr.sg_list = &list;
  1228. wr.wr.num_sge = 1;
  1229. wr.wr.opcode = IB_WR_SEND;
  1230. wr.wr.send_flags = IB_SEND_SIGNALED;
  1231. if (s_mac)
  1232. memcpy(to_mah(ah)->av.eth.s_mac, s_mac, 6);
  1233. if (vlan_id < 0x1000)
  1234. vlan_id |= (attr->sl & 7) << 13;
  1235. to_mah(ah)->av.eth.vlan = cpu_to_be16(vlan_id);
  1236. ret = ib_post_send(send_qp, &wr.wr, &bad_wr);
  1237. if (!ret)
  1238. return 0;
  1239. spin_lock(&sqp->tx_lock);
  1240. sqp->tx_ix_tail++;
  1241. spin_unlock(&sqp->tx_lock);
  1242. sqp->tx_ring[wire_tx_ix].ah = NULL;
  1243. out:
  1244. ib_destroy_ah(ah);
  1245. return ret;
  1246. }
  1247. static int get_slave_base_gid_ix(struct mlx4_ib_dev *dev, int slave, int port)
  1248. {
  1249. if (rdma_port_get_link_layer(&dev->ib_dev, port) == IB_LINK_LAYER_INFINIBAND)
  1250. return slave;
  1251. return mlx4_get_base_gid_ix(dev->dev, slave, port);
  1252. }
  1253. static void fill_in_real_sgid_index(struct mlx4_ib_dev *dev, int slave, int port,
  1254. struct ib_ah_attr *ah_attr)
  1255. {
  1256. if (rdma_port_get_link_layer(&dev->ib_dev, port) == IB_LINK_LAYER_INFINIBAND)
  1257. ah_attr->grh.sgid_index = slave;
  1258. else
  1259. ah_attr->grh.sgid_index += get_slave_base_gid_ix(dev, slave, port);
  1260. }
  1261. static void mlx4_ib_multiplex_mad(struct mlx4_ib_demux_pv_ctx *ctx, struct ib_wc *wc)
  1262. {
  1263. struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
  1264. struct mlx4_ib_demux_pv_qp *tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc->wr_id)];
  1265. int wr_ix = wc->wr_id & (MLX4_NUM_TUNNEL_BUFS - 1);
  1266. struct mlx4_tunnel_mad *tunnel = tun_qp->ring[wr_ix].addr;
  1267. struct mlx4_ib_ah ah;
  1268. struct ib_ah_attr ah_attr;
  1269. u8 *slave_id;
  1270. int slave;
  1271. int port;
  1272. u16 vlan_id;
  1273. /* Get slave that sent this packet */
  1274. if (wc->src_qp < dev->dev->phys_caps.base_proxy_sqpn ||
  1275. wc->src_qp >= dev->dev->phys_caps.base_proxy_sqpn + 8 * MLX4_MFUNC_MAX ||
  1276. (wc->src_qp & 0x1) != ctx->port - 1 ||
  1277. wc->src_qp & 0x4) {
  1278. mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d\n", wc->src_qp);
  1279. return;
  1280. }
  1281. slave = ((wc->src_qp & ~0x7) - dev->dev->phys_caps.base_proxy_sqpn) / 8;
  1282. if (slave != ctx->slave) {
  1283. mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d: "
  1284. "belongs to another slave\n", wc->src_qp);
  1285. return;
  1286. }
  1287. /* Map transaction ID */
  1288. ib_dma_sync_single_for_cpu(ctx->ib_dev, tun_qp->ring[wr_ix].map,
  1289. sizeof (struct mlx4_tunnel_mad),
  1290. DMA_FROM_DEVICE);
  1291. switch (tunnel->mad.mad_hdr.method) {
  1292. case IB_MGMT_METHOD_SET:
  1293. case IB_MGMT_METHOD_GET:
  1294. case IB_MGMT_METHOD_REPORT:
  1295. case IB_SA_METHOD_GET_TABLE:
  1296. case IB_SA_METHOD_DELETE:
  1297. case IB_SA_METHOD_GET_MULTI:
  1298. case IB_SA_METHOD_GET_TRACE_TBL:
  1299. slave_id = (u8 *) &tunnel->mad.mad_hdr.tid;
  1300. if (*slave_id) {
  1301. mlx4_ib_warn(ctx->ib_dev, "egress mad has non-null tid msb:%d "
  1302. "class:%d slave:%d\n", *slave_id,
  1303. tunnel->mad.mad_hdr.mgmt_class, slave);
  1304. return;
  1305. } else
  1306. *slave_id = slave;
  1307. default:
  1308. /* nothing */;
  1309. }
  1310. /* Class-specific handling */
  1311. switch (tunnel->mad.mad_hdr.mgmt_class) {
  1312. case IB_MGMT_CLASS_SUBN_LID_ROUTED:
  1313. case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
  1314. if (slave != mlx4_master_func_num(dev->dev) &&
  1315. !mlx4_vf_smi_enabled(dev->dev, slave, ctx->port))
  1316. return;
  1317. break;
  1318. case IB_MGMT_CLASS_SUBN_ADM:
  1319. if (mlx4_ib_multiplex_sa_handler(ctx->ib_dev, ctx->port, slave,
  1320. (struct ib_sa_mad *) &tunnel->mad))
  1321. return;
  1322. break;
  1323. case IB_MGMT_CLASS_CM:
  1324. if (mlx4_ib_multiplex_cm_handler(ctx->ib_dev, ctx->port, slave,
  1325. (struct ib_mad *) &tunnel->mad))
  1326. return;
  1327. break;
  1328. case IB_MGMT_CLASS_DEVICE_MGMT:
  1329. if (tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_GET &&
  1330. tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_SET)
  1331. return;
  1332. break;
  1333. default:
  1334. /* Drop unsupported classes for slaves in tunnel mode */
  1335. if (slave != mlx4_master_func_num(dev->dev)) {
  1336. mlx4_ib_warn(ctx->ib_dev, "dropping unsupported egress mad from class:%d "
  1337. "for slave:%d\n", tunnel->mad.mad_hdr.mgmt_class, slave);
  1338. return;
  1339. }
  1340. }
  1341. /* We are using standard ib_core services to send the mad, so generate a
  1342. * stadard address handle by decoding the tunnelled mlx4_ah fields */
  1343. memcpy(&ah.av, &tunnel->hdr.av, sizeof (struct mlx4_av));
  1344. ah.ibah.device = ctx->ib_dev;
  1345. port = be32_to_cpu(ah.av.ib.port_pd) >> 24;
  1346. port = mlx4_slave_convert_port(dev->dev, slave, port);
  1347. if (port < 0)
  1348. return;
  1349. ah.av.ib.port_pd = cpu_to_be32(port << 24 | (be32_to_cpu(ah.av.ib.port_pd) & 0xffffff));
  1350. mlx4_ib_query_ah(&ah.ibah, &ah_attr);
  1351. if (ah_attr.ah_flags & IB_AH_GRH)
  1352. fill_in_real_sgid_index(dev, slave, ctx->port, &ah_attr);
  1353. memcpy(ah_attr.dmac, tunnel->hdr.mac, 6);
  1354. vlan_id = be16_to_cpu(tunnel->hdr.vlan);
  1355. /* if slave have default vlan use it */
  1356. mlx4_get_slave_default_vlan(dev->dev, ctx->port, slave,
  1357. &vlan_id, &ah_attr.sl);
  1358. mlx4_ib_send_to_wire(dev, slave, ctx->port,
  1359. is_proxy_qp0(dev, wc->src_qp, slave) ?
  1360. IB_QPT_SMI : IB_QPT_GSI,
  1361. be16_to_cpu(tunnel->hdr.pkey_index),
  1362. be32_to_cpu(tunnel->hdr.remote_qpn),
  1363. be32_to_cpu(tunnel->hdr.qkey),
  1364. &ah_attr, wc->smac, vlan_id, &tunnel->mad);
  1365. }
  1366. static int mlx4_ib_alloc_pv_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
  1367. enum ib_qp_type qp_type, int is_tun)
  1368. {
  1369. int i;
  1370. struct mlx4_ib_demux_pv_qp *tun_qp;
  1371. int rx_buf_size, tx_buf_size;
  1372. if (qp_type > IB_QPT_GSI)
  1373. return -EINVAL;
  1374. tun_qp = &ctx->qp[qp_type];
  1375. tun_qp->ring = kzalloc(sizeof (struct mlx4_ib_buf) * MLX4_NUM_TUNNEL_BUFS,
  1376. GFP_KERNEL);
  1377. if (!tun_qp->ring)
  1378. return -ENOMEM;
  1379. tun_qp->tx_ring = kcalloc(MLX4_NUM_TUNNEL_BUFS,
  1380. sizeof (struct mlx4_ib_tun_tx_buf),
  1381. GFP_KERNEL);
  1382. if (!tun_qp->tx_ring) {
  1383. kfree(tun_qp->ring);
  1384. tun_qp->ring = NULL;
  1385. return -ENOMEM;
  1386. }
  1387. if (is_tun) {
  1388. rx_buf_size = sizeof (struct mlx4_tunnel_mad);
  1389. tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
  1390. } else {
  1391. rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
  1392. tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
  1393. }
  1394. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  1395. tun_qp->ring[i].addr = kmalloc(rx_buf_size, GFP_KERNEL);
  1396. if (!tun_qp->ring[i].addr)
  1397. goto err;
  1398. tun_qp->ring[i].map = ib_dma_map_single(ctx->ib_dev,
  1399. tun_qp->ring[i].addr,
  1400. rx_buf_size,
  1401. DMA_FROM_DEVICE);
  1402. if (ib_dma_mapping_error(ctx->ib_dev, tun_qp->ring[i].map)) {
  1403. kfree(tun_qp->ring[i].addr);
  1404. goto err;
  1405. }
  1406. }
  1407. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  1408. tun_qp->tx_ring[i].buf.addr =
  1409. kmalloc(tx_buf_size, GFP_KERNEL);
  1410. if (!tun_qp->tx_ring[i].buf.addr)
  1411. goto tx_err;
  1412. tun_qp->tx_ring[i].buf.map =
  1413. ib_dma_map_single(ctx->ib_dev,
  1414. tun_qp->tx_ring[i].buf.addr,
  1415. tx_buf_size,
  1416. DMA_TO_DEVICE);
  1417. if (ib_dma_mapping_error(ctx->ib_dev,
  1418. tun_qp->tx_ring[i].buf.map)) {
  1419. kfree(tun_qp->tx_ring[i].buf.addr);
  1420. goto tx_err;
  1421. }
  1422. tun_qp->tx_ring[i].ah = NULL;
  1423. }
  1424. spin_lock_init(&tun_qp->tx_lock);
  1425. tun_qp->tx_ix_head = 0;
  1426. tun_qp->tx_ix_tail = 0;
  1427. tun_qp->proxy_qpt = qp_type;
  1428. return 0;
  1429. tx_err:
  1430. while (i > 0) {
  1431. --i;
  1432. ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
  1433. tx_buf_size, DMA_TO_DEVICE);
  1434. kfree(tun_qp->tx_ring[i].buf.addr);
  1435. }
  1436. kfree(tun_qp->tx_ring);
  1437. tun_qp->tx_ring = NULL;
  1438. i = MLX4_NUM_TUNNEL_BUFS;
  1439. err:
  1440. while (i > 0) {
  1441. --i;
  1442. ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
  1443. rx_buf_size, DMA_FROM_DEVICE);
  1444. kfree(tun_qp->ring[i].addr);
  1445. }
  1446. kfree(tun_qp->ring);
  1447. tun_qp->ring = NULL;
  1448. return -ENOMEM;
  1449. }
  1450. static void mlx4_ib_free_pv_qp_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
  1451. enum ib_qp_type qp_type, int is_tun)
  1452. {
  1453. int i;
  1454. struct mlx4_ib_demux_pv_qp *tun_qp;
  1455. int rx_buf_size, tx_buf_size;
  1456. if (qp_type > IB_QPT_GSI)
  1457. return;
  1458. tun_qp = &ctx->qp[qp_type];
  1459. if (is_tun) {
  1460. rx_buf_size = sizeof (struct mlx4_tunnel_mad);
  1461. tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
  1462. } else {
  1463. rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
  1464. tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
  1465. }
  1466. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  1467. ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
  1468. rx_buf_size, DMA_FROM_DEVICE);
  1469. kfree(tun_qp->ring[i].addr);
  1470. }
  1471. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  1472. ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
  1473. tx_buf_size, DMA_TO_DEVICE);
  1474. kfree(tun_qp->tx_ring[i].buf.addr);
  1475. if (tun_qp->tx_ring[i].ah)
  1476. ib_destroy_ah(tun_qp->tx_ring[i].ah);
  1477. }
  1478. kfree(tun_qp->tx_ring);
  1479. kfree(tun_qp->ring);
  1480. }
  1481. static void mlx4_ib_tunnel_comp_worker(struct work_struct *work)
  1482. {
  1483. struct mlx4_ib_demux_pv_ctx *ctx;
  1484. struct mlx4_ib_demux_pv_qp *tun_qp;
  1485. struct ib_wc wc;
  1486. int ret;
  1487. ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
  1488. ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
  1489. while (ib_poll_cq(ctx->cq, 1, &wc) == 1) {
  1490. tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
  1491. if (wc.status == IB_WC_SUCCESS) {
  1492. switch (wc.opcode) {
  1493. case IB_WC_RECV:
  1494. mlx4_ib_multiplex_mad(ctx, &wc);
  1495. ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp,
  1496. wc.wr_id &
  1497. (MLX4_NUM_TUNNEL_BUFS - 1));
  1498. if (ret)
  1499. pr_err("Failed reposting tunnel "
  1500. "buf:%lld\n", wc.wr_id);
  1501. break;
  1502. case IB_WC_SEND:
  1503. pr_debug("received tunnel send completion:"
  1504. "wrid=0x%llx, status=0x%x\n",
  1505. wc.wr_id, wc.status);
  1506. ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
  1507. (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
  1508. tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
  1509. = NULL;
  1510. spin_lock(&tun_qp->tx_lock);
  1511. tun_qp->tx_ix_tail++;
  1512. spin_unlock(&tun_qp->tx_lock);
  1513. break;
  1514. default:
  1515. break;
  1516. }
  1517. } else {
  1518. pr_debug("mlx4_ib: completion error in tunnel: %d."
  1519. " status = %d, wrid = 0x%llx\n",
  1520. ctx->slave, wc.status, wc.wr_id);
  1521. if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
  1522. ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
  1523. (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
  1524. tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
  1525. = NULL;
  1526. spin_lock(&tun_qp->tx_lock);
  1527. tun_qp->tx_ix_tail++;
  1528. spin_unlock(&tun_qp->tx_lock);
  1529. }
  1530. }
  1531. }
  1532. }
  1533. static void pv_qp_event_handler(struct ib_event *event, void *qp_context)
  1534. {
  1535. struct mlx4_ib_demux_pv_ctx *sqp = qp_context;
  1536. /* It's worse than that! He's dead, Jim! */
  1537. pr_err("Fatal error (%d) on a MAD QP on port %d\n",
  1538. event->event, sqp->port);
  1539. }
  1540. static int create_pv_sqp(struct mlx4_ib_demux_pv_ctx *ctx,
  1541. enum ib_qp_type qp_type, int create_tun)
  1542. {
  1543. int i, ret;
  1544. struct mlx4_ib_demux_pv_qp *tun_qp;
  1545. struct mlx4_ib_qp_tunnel_init_attr qp_init_attr;
  1546. struct ib_qp_attr attr;
  1547. int qp_attr_mask_INIT;
  1548. if (qp_type > IB_QPT_GSI)
  1549. return -EINVAL;
  1550. tun_qp = &ctx->qp[qp_type];
  1551. memset(&qp_init_attr, 0, sizeof qp_init_attr);
  1552. qp_init_attr.init_attr.send_cq = ctx->cq;
  1553. qp_init_attr.init_attr.recv_cq = ctx->cq;
  1554. qp_init_attr.init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
  1555. qp_init_attr.init_attr.cap.max_send_wr = MLX4_NUM_TUNNEL_BUFS;
  1556. qp_init_attr.init_attr.cap.max_recv_wr = MLX4_NUM_TUNNEL_BUFS;
  1557. qp_init_attr.init_attr.cap.max_send_sge = 1;
  1558. qp_init_attr.init_attr.cap.max_recv_sge = 1;
  1559. if (create_tun) {
  1560. qp_init_attr.init_attr.qp_type = IB_QPT_UD;
  1561. qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_TUNNEL_QP;
  1562. qp_init_attr.port = ctx->port;
  1563. qp_init_attr.slave = ctx->slave;
  1564. qp_init_attr.proxy_qp_type = qp_type;
  1565. qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX |
  1566. IB_QP_QKEY | IB_QP_PORT;
  1567. } else {
  1568. qp_init_attr.init_attr.qp_type = qp_type;
  1569. qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_SQP;
  1570. qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_QKEY;
  1571. }
  1572. qp_init_attr.init_attr.port_num = ctx->port;
  1573. qp_init_attr.init_attr.qp_context = ctx;
  1574. qp_init_attr.init_attr.event_handler = pv_qp_event_handler;
  1575. tun_qp->qp = ib_create_qp(ctx->pd, &qp_init_attr.init_attr);
  1576. if (IS_ERR(tun_qp->qp)) {
  1577. ret = PTR_ERR(tun_qp->qp);
  1578. tun_qp->qp = NULL;
  1579. pr_err("Couldn't create %s QP (%d)\n",
  1580. create_tun ? "tunnel" : "special", ret);
  1581. return ret;
  1582. }
  1583. memset(&attr, 0, sizeof attr);
  1584. attr.qp_state = IB_QPS_INIT;
  1585. ret = 0;
  1586. if (create_tun)
  1587. ret = find_slave_port_pkey_ix(to_mdev(ctx->ib_dev), ctx->slave,
  1588. ctx->port, IB_DEFAULT_PKEY_FULL,
  1589. &attr.pkey_index);
  1590. if (ret || !create_tun)
  1591. attr.pkey_index =
  1592. to_mdev(ctx->ib_dev)->pkeys.virt2phys_pkey[ctx->slave][ctx->port - 1][0];
  1593. attr.qkey = IB_QP1_QKEY;
  1594. attr.port_num = ctx->port;
  1595. ret = ib_modify_qp(tun_qp->qp, &attr, qp_attr_mask_INIT);
  1596. if (ret) {
  1597. pr_err("Couldn't change %s qp state to INIT (%d)\n",
  1598. create_tun ? "tunnel" : "special", ret);
  1599. goto err_qp;
  1600. }
  1601. attr.qp_state = IB_QPS_RTR;
  1602. ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE);
  1603. if (ret) {
  1604. pr_err("Couldn't change %s qp state to RTR (%d)\n",
  1605. create_tun ? "tunnel" : "special", ret);
  1606. goto err_qp;
  1607. }
  1608. attr.qp_state = IB_QPS_RTS;
  1609. attr.sq_psn = 0;
  1610. ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE | IB_QP_SQ_PSN);
  1611. if (ret) {
  1612. pr_err("Couldn't change %s qp state to RTS (%d)\n",
  1613. create_tun ? "tunnel" : "special", ret);
  1614. goto err_qp;
  1615. }
  1616. for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
  1617. ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp, i);
  1618. if (ret) {
  1619. pr_err(" mlx4_ib_post_pv_buf error"
  1620. " (err = %d, i = %d)\n", ret, i);
  1621. goto err_qp;
  1622. }
  1623. }
  1624. return 0;
  1625. err_qp:
  1626. ib_destroy_qp(tun_qp->qp);
  1627. tun_qp->qp = NULL;
  1628. return ret;
  1629. }
  1630. /*
  1631. * IB MAD completion callback for real SQPs
  1632. */
  1633. static void mlx4_ib_sqp_comp_worker(struct work_struct *work)
  1634. {
  1635. struct mlx4_ib_demux_pv_ctx *ctx;
  1636. struct mlx4_ib_demux_pv_qp *sqp;
  1637. struct ib_wc wc;
  1638. struct ib_grh *grh;
  1639. struct ib_mad *mad;
  1640. ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
  1641. ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
  1642. while (mlx4_ib_poll_cq(ctx->cq, 1, &wc) == 1) {
  1643. sqp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
  1644. if (wc.status == IB_WC_SUCCESS) {
  1645. switch (wc.opcode) {
  1646. case IB_WC_SEND:
  1647. ib_destroy_ah(sqp->tx_ring[wc.wr_id &
  1648. (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
  1649. sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
  1650. = NULL;
  1651. spin_lock(&sqp->tx_lock);
  1652. sqp->tx_ix_tail++;
  1653. spin_unlock(&sqp->tx_lock);
  1654. break;
  1655. case IB_WC_RECV:
  1656. mad = (struct ib_mad *) &(((struct mlx4_mad_rcv_buf *)
  1657. (sqp->ring[wc.wr_id &
  1658. (MLX4_NUM_TUNNEL_BUFS - 1)].addr))->payload);
  1659. grh = &(((struct mlx4_mad_rcv_buf *)
  1660. (sqp->ring[wc.wr_id &
  1661. (MLX4_NUM_TUNNEL_BUFS - 1)].addr))->grh);
  1662. mlx4_ib_demux_mad(ctx->ib_dev, ctx->port, &wc, grh, mad);
  1663. if (mlx4_ib_post_pv_qp_buf(ctx, sqp, wc.wr_id &
  1664. (MLX4_NUM_TUNNEL_BUFS - 1)))
  1665. pr_err("Failed reposting SQP "
  1666. "buf:%lld\n", wc.wr_id);
  1667. break;
  1668. default:
  1669. BUG_ON(1);
  1670. break;
  1671. }
  1672. } else {
  1673. pr_debug("mlx4_ib: completion error in tunnel: %d."
  1674. " status = %d, wrid = 0x%llx\n",
  1675. ctx->slave, wc.status, wc.wr_id);
  1676. if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
  1677. ib_destroy_ah(sqp->tx_ring[wc.wr_id &
  1678. (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
  1679. sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
  1680. = NULL;
  1681. spin_lock(&sqp->tx_lock);
  1682. sqp->tx_ix_tail++;
  1683. spin_unlock(&sqp->tx_lock);
  1684. }
  1685. }
  1686. }
  1687. }
  1688. static int alloc_pv_object(struct mlx4_ib_dev *dev, int slave, int port,
  1689. struct mlx4_ib_demux_pv_ctx **ret_ctx)
  1690. {
  1691. struct mlx4_ib_demux_pv_ctx *ctx;
  1692. *ret_ctx = NULL;
  1693. ctx = kzalloc(sizeof (struct mlx4_ib_demux_pv_ctx), GFP_KERNEL);
  1694. if (!ctx) {
  1695. pr_err("failed allocating pv resource context "
  1696. "for port %d, slave %d\n", port, slave);
  1697. return -ENOMEM;
  1698. }
  1699. ctx->ib_dev = &dev->ib_dev;
  1700. ctx->port = port;
  1701. ctx->slave = slave;
  1702. *ret_ctx = ctx;
  1703. return 0;
  1704. }
  1705. static void free_pv_object(struct mlx4_ib_dev *dev, int slave, int port)
  1706. {
  1707. if (dev->sriov.demux[port - 1].tun[slave]) {
  1708. kfree(dev->sriov.demux[port - 1].tun[slave]);
  1709. dev->sriov.demux[port - 1].tun[slave] = NULL;
  1710. }
  1711. }
  1712. static int create_pv_resources(struct ib_device *ibdev, int slave, int port,
  1713. int create_tun, struct mlx4_ib_demux_pv_ctx *ctx)
  1714. {
  1715. int ret, cq_size;
  1716. struct ib_cq_init_attr cq_attr = {};
  1717. if (ctx->state != DEMUX_PV_STATE_DOWN)
  1718. return -EEXIST;
  1719. ctx->state = DEMUX_PV_STATE_STARTING;
  1720. /* have QP0 only if link layer is IB */
  1721. if (rdma_port_get_link_layer(ibdev, ctx->port) ==
  1722. IB_LINK_LAYER_INFINIBAND)
  1723. ctx->has_smi = 1;
  1724. if (ctx->has_smi) {
  1725. ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_SMI, create_tun);
  1726. if (ret) {
  1727. pr_err("Failed allocating qp0 tunnel bufs (%d)\n", ret);
  1728. goto err_out;
  1729. }
  1730. }
  1731. ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_GSI, create_tun);
  1732. if (ret) {
  1733. pr_err("Failed allocating qp1 tunnel bufs (%d)\n", ret);
  1734. goto err_out_qp0;
  1735. }
  1736. cq_size = 2 * MLX4_NUM_TUNNEL_BUFS;
  1737. if (ctx->has_smi)
  1738. cq_size *= 2;
  1739. cq_attr.cqe = cq_size;
  1740. ctx->cq = ib_create_cq(ctx->ib_dev, mlx4_ib_tunnel_comp_handler,
  1741. NULL, ctx, &cq_attr);
  1742. if (IS_ERR(ctx->cq)) {
  1743. ret = PTR_ERR(ctx->cq);
  1744. pr_err("Couldn't create tunnel CQ (%d)\n", ret);
  1745. goto err_buf;
  1746. }
  1747. ctx->pd = ib_alloc_pd(ctx->ib_dev, 0);
  1748. if (IS_ERR(ctx->pd)) {
  1749. ret = PTR_ERR(ctx->pd);
  1750. pr_err("Couldn't create tunnel PD (%d)\n", ret);
  1751. goto err_cq;
  1752. }
  1753. if (ctx->has_smi) {
  1754. ret = create_pv_sqp(ctx, IB_QPT_SMI, create_tun);
  1755. if (ret) {
  1756. pr_err("Couldn't create %s QP0 (%d)\n",
  1757. create_tun ? "tunnel for" : "", ret);
  1758. goto err_pd;
  1759. }
  1760. }
  1761. ret = create_pv_sqp(ctx, IB_QPT_GSI, create_tun);
  1762. if (ret) {
  1763. pr_err("Couldn't create %s QP1 (%d)\n",
  1764. create_tun ? "tunnel for" : "", ret);
  1765. goto err_qp0;
  1766. }
  1767. if (create_tun)
  1768. INIT_WORK(&ctx->work, mlx4_ib_tunnel_comp_worker);
  1769. else
  1770. INIT_WORK(&ctx->work, mlx4_ib_sqp_comp_worker);
  1771. ctx->wq = to_mdev(ibdev)->sriov.demux[port - 1].wq;
  1772. ret = ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
  1773. if (ret) {
  1774. pr_err("Couldn't arm tunnel cq (%d)\n", ret);
  1775. goto err_wq;
  1776. }
  1777. ctx->state = DEMUX_PV_STATE_ACTIVE;
  1778. return 0;
  1779. err_wq:
  1780. ctx->wq = NULL;
  1781. ib_destroy_qp(ctx->qp[1].qp);
  1782. ctx->qp[1].qp = NULL;
  1783. err_qp0:
  1784. if (ctx->has_smi)
  1785. ib_destroy_qp(ctx->qp[0].qp);
  1786. ctx->qp[0].qp = NULL;
  1787. err_pd:
  1788. ib_dealloc_pd(ctx->pd);
  1789. ctx->pd = NULL;
  1790. err_cq:
  1791. ib_destroy_cq(ctx->cq);
  1792. ctx->cq = NULL;
  1793. err_buf:
  1794. mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, create_tun);
  1795. err_out_qp0:
  1796. if (ctx->has_smi)
  1797. mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, create_tun);
  1798. err_out:
  1799. ctx->state = DEMUX_PV_STATE_DOWN;
  1800. return ret;
  1801. }
  1802. static void destroy_pv_resources(struct mlx4_ib_dev *dev, int slave, int port,
  1803. struct mlx4_ib_demux_pv_ctx *ctx, int flush)
  1804. {
  1805. if (!ctx)
  1806. return;
  1807. if (ctx->state > DEMUX_PV_STATE_DOWN) {
  1808. ctx->state = DEMUX_PV_STATE_DOWNING;
  1809. if (flush)
  1810. flush_workqueue(ctx->wq);
  1811. if (ctx->has_smi) {
  1812. ib_destroy_qp(ctx->qp[0].qp);
  1813. ctx->qp[0].qp = NULL;
  1814. mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, 1);
  1815. }
  1816. ib_destroy_qp(ctx->qp[1].qp);
  1817. ctx->qp[1].qp = NULL;
  1818. mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, 1);
  1819. ib_dealloc_pd(ctx->pd);
  1820. ctx->pd = NULL;
  1821. ib_destroy_cq(ctx->cq);
  1822. ctx->cq = NULL;
  1823. ctx->state = DEMUX_PV_STATE_DOWN;
  1824. }
  1825. }
  1826. static int mlx4_ib_tunnels_update(struct mlx4_ib_dev *dev, int slave,
  1827. int port, int do_init)
  1828. {
  1829. int ret = 0;
  1830. if (!do_init) {
  1831. clean_vf_mcast(&dev->sriov.demux[port - 1], slave);
  1832. /* for master, destroy real sqp resources */
  1833. if (slave == mlx4_master_func_num(dev->dev))
  1834. destroy_pv_resources(dev, slave, port,
  1835. dev->sriov.sqps[port - 1], 1);
  1836. /* destroy the tunnel qp resources */
  1837. destroy_pv_resources(dev, slave, port,
  1838. dev->sriov.demux[port - 1].tun[slave], 1);
  1839. return 0;
  1840. }
  1841. /* create the tunnel qp resources */
  1842. ret = create_pv_resources(&dev->ib_dev, slave, port, 1,
  1843. dev->sriov.demux[port - 1].tun[slave]);
  1844. /* for master, create the real sqp resources */
  1845. if (!ret && slave == mlx4_master_func_num(dev->dev))
  1846. ret = create_pv_resources(&dev->ib_dev, slave, port, 0,
  1847. dev->sriov.sqps[port - 1]);
  1848. return ret;
  1849. }
  1850. void mlx4_ib_tunnels_update_work(struct work_struct *work)
  1851. {
  1852. struct mlx4_ib_demux_work *dmxw;
  1853. dmxw = container_of(work, struct mlx4_ib_demux_work, work);
  1854. mlx4_ib_tunnels_update(dmxw->dev, dmxw->slave, (int) dmxw->port,
  1855. dmxw->do_init);
  1856. kfree(dmxw);
  1857. return;
  1858. }
  1859. static int mlx4_ib_alloc_demux_ctx(struct mlx4_ib_dev *dev,
  1860. struct mlx4_ib_demux_ctx *ctx,
  1861. int port)
  1862. {
  1863. char name[12];
  1864. int ret = 0;
  1865. int i;
  1866. ctx->tun = kcalloc(dev->dev->caps.sqp_demux,
  1867. sizeof (struct mlx4_ib_demux_pv_ctx *), GFP_KERNEL);
  1868. if (!ctx->tun)
  1869. return -ENOMEM;
  1870. ctx->dev = dev;
  1871. ctx->port = port;
  1872. ctx->ib_dev = &dev->ib_dev;
  1873. for (i = 0;
  1874. i < min(dev->dev->caps.sqp_demux,
  1875. (u16)(dev->dev->persist->num_vfs + 1));
  1876. i++) {
  1877. struct mlx4_active_ports actv_ports =
  1878. mlx4_get_active_ports(dev->dev, i);
  1879. if (!test_bit(port - 1, actv_ports.ports))
  1880. continue;
  1881. ret = alloc_pv_object(dev, i, port, &ctx->tun[i]);
  1882. if (ret) {
  1883. ret = -ENOMEM;
  1884. goto err_mcg;
  1885. }
  1886. }
  1887. ret = mlx4_ib_mcg_port_init(ctx);
  1888. if (ret) {
  1889. pr_err("Failed initializing mcg para-virt (%d)\n", ret);
  1890. goto err_mcg;
  1891. }
  1892. snprintf(name, sizeof name, "mlx4_ibt%d", port);
  1893. ctx->wq = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
  1894. if (!ctx->wq) {
  1895. pr_err("Failed to create tunnelling WQ for port %d\n", port);
  1896. ret = -ENOMEM;
  1897. goto err_wq;
  1898. }
  1899. snprintf(name, sizeof name, "mlx4_ibud%d", port);
  1900. ctx->ud_wq = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
  1901. if (!ctx->ud_wq) {
  1902. pr_err("Failed to create up/down WQ for port %d\n", port);
  1903. ret = -ENOMEM;
  1904. goto err_udwq;
  1905. }
  1906. return 0;
  1907. err_udwq:
  1908. destroy_workqueue(ctx->wq);
  1909. ctx->wq = NULL;
  1910. err_wq:
  1911. mlx4_ib_mcg_port_cleanup(ctx, 1);
  1912. err_mcg:
  1913. for (i = 0; i < dev->dev->caps.sqp_demux; i++)
  1914. free_pv_object(dev, i, port);
  1915. kfree(ctx->tun);
  1916. ctx->tun = NULL;
  1917. return ret;
  1918. }
  1919. static void mlx4_ib_free_sqp_ctx(struct mlx4_ib_demux_pv_ctx *sqp_ctx)
  1920. {
  1921. if (sqp_ctx->state > DEMUX_PV_STATE_DOWN) {
  1922. sqp_ctx->state = DEMUX_PV_STATE_DOWNING;
  1923. flush_workqueue(sqp_ctx->wq);
  1924. if (sqp_ctx->has_smi) {
  1925. ib_destroy_qp(sqp_ctx->qp[0].qp);
  1926. sqp_ctx->qp[0].qp = NULL;
  1927. mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_SMI, 0);
  1928. }
  1929. ib_destroy_qp(sqp_ctx->qp[1].qp);
  1930. sqp_ctx->qp[1].qp = NULL;
  1931. mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_GSI, 0);
  1932. ib_dealloc_pd(sqp_ctx->pd);
  1933. sqp_ctx->pd = NULL;
  1934. ib_destroy_cq(sqp_ctx->cq);
  1935. sqp_ctx->cq = NULL;
  1936. sqp_ctx->state = DEMUX_PV_STATE_DOWN;
  1937. }
  1938. }
  1939. static void mlx4_ib_free_demux_ctx(struct mlx4_ib_demux_ctx *ctx)
  1940. {
  1941. int i;
  1942. if (ctx) {
  1943. struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
  1944. mlx4_ib_mcg_port_cleanup(ctx, 1);
  1945. for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
  1946. if (!ctx->tun[i])
  1947. continue;
  1948. if (ctx->tun[i]->state > DEMUX_PV_STATE_DOWN)
  1949. ctx->tun[i]->state = DEMUX_PV_STATE_DOWNING;
  1950. }
  1951. flush_workqueue(ctx->wq);
  1952. for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
  1953. destroy_pv_resources(dev, i, ctx->port, ctx->tun[i], 0);
  1954. free_pv_object(dev, i, ctx->port);
  1955. }
  1956. kfree(ctx->tun);
  1957. destroy_workqueue(ctx->ud_wq);
  1958. destroy_workqueue(ctx->wq);
  1959. }
  1960. }
  1961. static void mlx4_ib_master_tunnels(struct mlx4_ib_dev *dev, int do_init)
  1962. {
  1963. int i;
  1964. if (!mlx4_is_master(dev->dev))
  1965. return;
  1966. /* initialize or tear down tunnel QPs for the master */
  1967. for (i = 0; i < dev->dev->caps.num_ports; i++)
  1968. mlx4_ib_tunnels_update(dev, mlx4_master_func_num(dev->dev), i + 1, do_init);
  1969. return;
  1970. }
  1971. int mlx4_ib_init_sriov(struct mlx4_ib_dev *dev)
  1972. {
  1973. int i = 0;
  1974. int err;
  1975. if (!mlx4_is_mfunc(dev->dev))
  1976. return 0;
  1977. dev->sriov.is_going_down = 0;
  1978. spin_lock_init(&dev->sriov.going_down_lock);
  1979. mlx4_ib_cm_paravirt_init(dev);
  1980. mlx4_ib_warn(&dev->ib_dev, "multi-function enabled\n");
  1981. if (mlx4_is_slave(dev->dev)) {
  1982. mlx4_ib_warn(&dev->ib_dev, "operating in qp1 tunnel mode\n");
  1983. return 0;
  1984. }
  1985. for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
  1986. if (i == mlx4_master_func_num(dev->dev))
  1987. mlx4_put_slave_node_guid(dev->dev, i, dev->ib_dev.node_guid);
  1988. else
  1989. mlx4_put_slave_node_guid(dev->dev, i, mlx4_ib_gen_node_guid());
  1990. }
  1991. err = mlx4_ib_init_alias_guid_service(dev);
  1992. if (err) {
  1993. mlx4_ib_warn(&dev->ib_dev, "Failed init alias guid process.\n");
  1994. goto paravirt_err;
  1995. }
  1996. err = mlx4_ib_device_register_sysfs(dev);
  1997. if (err) {
  1998. mlx4_ib_warn(&dev->ib_dev, "Failed to register sysfs\n");
  1999. goto sysfs_err;
  2000. }
  2001. mlx4_ib_warn(&dev->ib_dev, "initializing demux service for %d qp1 clients\n",
  2002. dev->dev->caps.sqp_demux);
  2003. for (i = 0; i < dev->num_ports; i++) {
  2004. union ib_gid gid;
  2005. err = __mlx4_ib_query_gid(&dev->ib_dev, i + 1, 0, &gid, 1);
  2006. if (err)
  2007. goto demux_err;
  2008. dev->sriov.demux[i].guid_cache[0] = gid.global.interface_id;
  2009. atomic64_set(&dev->sriov.demux[i].subnet_prefix,
  2010. be64_to_cpu(gid.global.subnet_prefix));
  2011. err = alloc_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1,
  2012. &dev->sriov.sqps[i]);
  2013. if (err)
  2014. goto demux_err;
  2015. err = mlx4_ib_alloc_demux_ctx(dev, &dev->sriov.demux[i], i + 1);
  2016. if (err)
  2017. goto free_pv;
  2018. }
  2019. mlx4_ib_master_tunnels(dev, 1);
  2020. return 0;
  2021. free_pv:
  2022. free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
  2023. demux_err:
  2024. while (--i >= 0) {
  2025. free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
  2026. mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
  2027. }
  2028. mlx4_ib_device_unregister_sysfs(dev);
  2029. sysfs_err:
  2030. mlx4_ib_destroy_alias_guid_service(dev);
  2031. paravirt_err:
  2032. mlx4_ib_cm_paravirt_clean(dev, -1);
  2033. return err;
  2034. }
  2035. void mlx4_ib_close_sriov(struct mlx4_ib_dev *dev)
  2036. {
  2037. int i;
  2038. unsigned long flags;
  2039. if (!mlx4_is_mfunc(dev->dev))
  2040. return;
  2041. spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
  2042. dev->sriov.is_going_down = 1;
  2043. spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
  2044. if (mlx4_is_master(dev->dev)) {
  2045. for (i = 0; i < dev->num_ports; i++) {
  2046. flush_workqueue(dev->sriov.demux[i].ud_wq);
  2047. mlx4_ib_free_sqp_ctx(dev->sriov.sqps[i]);
  2048. kfree(dev->sriov.sqps[i]);
  2049. dev->sriov.sqps[i] = NULL;
  2050. mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
  2051. }
  2052. mlx4_ib_cm_paravirt_clean(dev, -1);
  2053. mlx4_ib_destroy_alias_guid_service(dev);
  2054. mlx4_ib_device_unregister_sysfs(dev);
  2055. }
  2056. }