qp.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535
  1. /*
  2. * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/log2.h>
  34. #include <linux/etherdevice.h>
  35. #include <net/ip.h>
  36. #include <linux/slab.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/vmalloc.h>
  39. #include <rdma/ib_cache.h>
  40. #include <rdma/ib_pack.h>
  41. #include <rdma/ib_addr.h>
  42. #include <rdma/ib_mad.h>
  43. #include <linux/mlx4/driver.h>
  44. #include <linux/mlx4/qp.h>
  45. #include "mlx4_ib.h"
  46. #include <rdma/mlx4-abi.h>
  47. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq,
  48. struct mlx4_ib_cq *recv_cq);
  49. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq,
  50. struct mlx4_ib_cq *recv_cq);
  51. enum {
  52. MLX4_IB_ACK_REQ_FREQ = 8,
  53. };
  54. enum {
  55. MLX4_IB_DEFAULT_SCHED_QUEUE = 0x83,
  56. MLX4_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
  57. MLX4_IB_LINK_TYPE_IB = 0,
  58. MLX4_IB_LINK_TYPE_ETH = 1
  59. };
  60. enum {
  61. /*
  62. * Largest possible UD header: send with GRH and immediate
  63. * data plus 18 bytes for an Ethernet header with VLAN/802.1Q
  64. * tag. (LRH would only use 8 bytes, so Ethernet is the
  65. * biggest case)
  66. */
  67. MLX4_IB_UD_HEADER_SIZE = 82,
  68. MLX4_IB_LSO_HEADER_SPARE = 128,
  69. };
  70. struct mlx4_ib_sqp {
  71. struct mlx4_ib_qp qp;
  72. int pkey_index;
  73. u32 qkey;
  74. u32 send_psn;
  75. struct ib_ud_header ud_header;
  76. u8 header_buf[MLX4_IB_UD_HEADER_SIZE];
  77. struct ib_qp *roce_v2_gsi;
  78. };
  79. enum {
  80. MLX4_IB_MIN_SQ_STRIDE = 6,
  81. MLX4_IB_CACHE_LINE_SIZE = 64,
  82. };
  83. enum {
  84. MLX4_RAW_QP_MTU = 7,
  85. MLX4_RAW_QP_MSGMAX = 31,
  86. };
  87. #ifndef ETH_ALEN
  88. #define ETH_ALEN 6
  89. #endif
  90. static const __be32 mlx4_ib_opcode[] = {
  91. [IB_WR_SEND] = cpu_to_be32(MLX4_OPCODE_SEND),
  92. [IB_WR_LSO] = cpu_to_be32(MLX4_OPCODE_LSO),
  93. [IB_WR_SEND_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_SEND_IMM),
  94. [IB_WR_RDMA_WRITE] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE),
  95. [IB_WR_RDMA_WRITE_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE_IMM),
  96. [IB_WR_RDMA_READ] = cpu_to_be32(MLX4_OPCODE_RDMA_READ),
  97. [IB_WR_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_ATOMIC_CS),
  98. [IB_WR_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_ATOMIC_FA),
  99. [IB_WR_SEND_WITH_INV] = cpu_to_be32(MLX4_OPCODE_SEND_INVAL),
  100. [IB_WR_LOCAL_INV] = cpu_to_be32(MLX4_OPCODE_LOCAL_INVAL),
  101. [IB_WR_REG_MR] = cpu_to_be32(MLX4_OPCODE_FMR),
  102. [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_CS),
  103. [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_FA),
  104. };
  105. static struct mlx4_ib_sqp *to_msqp(struct mlx4_ib_qp *mqp)
  106. {
  107. return container_of(mqp, struct mlx4_ib_sqp, qp);
  108. }
  109. static int is_tunnel_qp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  110. {
  111. if (!mlx4_is_master(dev->dev))
  112. return 0;
  113. return qp->mqp.qpn >= dev->dev->phys_caps.base_tunnel_sqpn &&
  114. qp->mqp.qpn < dev->dev->phys_caps.base_tunnel_sqpn +
  115. 8 * MLX4_MFUNC_MAX;
  116. }
  117. static int is_sqp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  118. {
  119. int proxy_sqp = 0;
  120. int real_sqp = 0;
  121. int i;
  122. /* PPF or Native -- real SQP */
  123. real_sqp = ((mlx4_is_master(dev->dev) || !mlx4_is_mfunc(dev->dev)) &&
  124. qp->mqp.qpn >= dev->dev->phys_caps.base_sqpn &&
  125. qp->mqp.qpn <= dev->dev->phys_caps.base_sqpn + 3);
  126. if (real_sqp)
  127. return 1;
  128. /* VF or PF -- proxy SQP */
  129. if (mlx4_is_mfunc(dev->dev)) {
  130. for (i = 0; i < dev->dev->caps.num_ports; i++) {
  131. if (qp->mqp.qpn == dev->dev->caps.qp0_proxy[i] ||
  132. qp->mqp.qpn == dev->dev->caps.qp1_proxy[i]) {
  133. proxy_sqp = 1;
  134. break;
  135. }
  136. }
  137. }
  138. if (proxy_sqp)
  139. return 1;
  140. return !!(qp->flags & MLX4_IB_ROCE_V2_GSI_QP);
  141. }
  142. /* used for INIT/CLOSE port logic */
  143. static int is_qp0(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  144. {
  145. int proxy_qp0 = 0;
  146. int real_qp0 = 0;
  147. int i;
  148. /* PPF or Native -- real QP0 */
  149. real_qp0 = ((mlx4_is_master(dev->dev) || !mlx4_is_mfunc(dev->dev)) &&
  150. qp->mqp.qpn >= dev->dev->phys_caps.base_sqpn &&
  151. qp->mqp.qpn <= dev->dev->phys_caps.base_sqpn + 1);
  152. if (real_qp0)
  153. return 1;
  154. /* VF or PF -- proxy QP0 */
  155. if (mlx4_is_mfunc(dev->dev)) {
  156. for (i = 0; i < dev->dev->caps.num_ports; i++) {
  157. if (qp->mqp.qpn == dev->dev->caps.qp0_proxy[i]) {
  158. proxy_qp0 = 1;
  159. break;
  160. }
  161. }
  162. }
  163. return proxy_qp0;
  164. }
  165. static void *get_wqe(struct mlx4_ib_qp *qp, int offset)
  166. {
  167. return mlx4_buf_offset(&qp->buf, offset);
  168. }
  169. static void *get_recv_wqe(struct mlx4_ib_qp *qp, int n)
  170. {
  171. return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
  172. }
  173. static void *get_send_wqe(struct mlx4_ib_qp *qp, int n)
  174. {
  175. return get_wqe(qp, qp->sq.offset + (n << qp->sq.wqe_shift));
  176. }
  177. /*
  178. * Stamp a SQ WQE so that it is invalid if prefetched by marking the
  179. * first four bytes of every 64 byte chunk with
  180. * 0x7FFFFFF | (invalid_ownership_value << 31).
  181. *
  182. * When the max work request size is less than or equal to the WQE
  183. * basic block size, as an optimization, we can stamp all WQEs with
  184. * 0xffffffff, and skip the very first chunk of each WQE.
  185. */
  186. static void stamp_send_wqe(struct mlx4_ib_qp *qp, int n, int size)
  187. {
  188. __be32 *wqe;
  189. int i;
  190. int s;
  191. int ind;
  192. void *buf;
  193. __be32 stamp;
  194. struct mlx4_wqe_ctrl_seg *ctrl;
  195. if (qp->sq_max_wqes_per_wr > 1) {
  196. s = roundup(size, 1U << qp->sq.wqe_shift);
  197. for (i = 0; i < s; i += 64) {
  198. ind = (i >> qp->sq.wqe_shift) + n;
  199. stamp = ind & qp->sq.wqe_cnt ? cpu_to_be32(0x7fffffff) :
  200. cpu_to_be32(0xffffffff);
  201. buf = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  202. wqe = buf + (i & ((1 << qp->sq.wqe_shift) - 1));
  203. *wqe = stamp;
  204. }
  205. } else {
  206. ctrl = buf = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
  207. s = (ctrl->qpn_vlan.fence_size & 0x3f) << 4;
  208. for (i = 64; i < s; i += 64) {
  209. wqe = buf + i;
  210. *wqe = cpu_to_be32(0xffffffff);
  211. }
  212. }
  213. }
  214. static void post_nop_wqe(struct mlx4_ib_qp *qp, int n, int size)
  215. {
  216. struct mlx4_wqe_ctrl_seg *ctrl;
  217. struct mlx4_wqe_inline_seg *inl;
  218. void *wqe;
  219. int s;
  220. ctrl = wqe = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
  221. s = sizeof(struct mlx4_wqe_ctrl_seg);
  222. if (qp->ibqp.qp_type == IB_QPT_UD) {
  223. struct mlx4_wqe_datagram_seg *dgram = wqe + sizeof *ctrl;
  224. struct mlx4_av *av = (struct mlx4_av *)dgram->av;
  225. memset(dgram, 0, sizeof *dgram);
  226. av->port_pd = cpu_to_be32((qp->port << 24) | to_mpd(qp->ibqp.pd)->pdn);
  227. s += sizeof(struct mlx4_wqe_datagram_seg);
  228. }
  229. /* Pad the remainder of the WQE with an inline data segment. */
  230. if (size > s) {
  231. inl = wqe + s;
  232. inl->byte_count = cpu_to_be32(1 << 31 | (size - s - sizeof *inl));
  233. }
  234. ctrl->srcrb_flags = 0;
  235. ctrl->qpn_vlan.fence_size = size / 16;
  236. /*
  237. * Make sure descriptor is fully written before setting ownership bit
  238. * (because HW can start executing as soon as we do).
  239. */
  240. wmb();
  241. ctrl->owner_opcode = cpu_to_be32(MLX4_OPCODE_NOP | MLX4_WQE_CTRL_NEC) |
  242. (n & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0);
  243. stamp_send_wqe(qp, n + qp->sq_spare_wqes, size);
  244. }
  245. /* Post NOP WQE to prevent wrap-around in the middle of WR */
  246. static inline unsigned pad_wraparound(struct mlx4_ib_qp *qp, int ind)
  247. {
  248. unsigned s = qp->sq.wqe_cnt - (ind & (qp->sq.wqe_cnt - 1));
  249. if (unlikely(s < qp->sq_max_wqes_per_wr)) {
  250. post_nop_wqe(qp, ind, s << qp->sq.wqe_shift);
  251. ind += s;
  252. }
  253. return ind;
  254. }
  255. static void mlx4_ib_qp_event(struct mlx4_qp *qp, enum mlx4_event type)
  256. {
  257. struct ib_event event;
  258. struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
  259. if (type == MLX4_EVENT_TYPE_PATH_MIG)
  260. to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
  261. if (ibqp->event_handler) {
  262. event.device = ibqp->device;
  263. event.element.qp = ibqp;
  264. switch (type) {
  265. case MLX4_EVENT_TYPE_PATH_MIG:
  266. event.event = IB_EVENT_PATH_MIG;
  267. break;
  268. case MLX4_EVENT_TYPE_COMM_EST:
  269. event.event = IB_EVENT_COMM_EST;
  270. break;
  271. case MLX4_EVENT_TYPE_SQ_DRAINED:
  272. event.event = IB_EVENT_SQ_DRAINED;
  273. break;
  274. case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
  275. event.event = IB_EVENT_QP_LAST_WQE_REACHED;
  276. break;
  277. case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
  278. event.event = IB_EVENT_QP_FATAL;
  279. break;
  280. case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
  281. event.event = IB_EVENT_PATH_MIG_ERR;
  282. break;
  283. case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  284. event.event = IB_EVENT_QP_REQ_ERR;
  285. break;
  286. case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
  287. event.event = IB_EVENT_QP_ACCESS_ERR;
  288. break;
  289. default:
  290. pr_warn("Unexpected event type %d "
  291. "on QP %06x\n", type, qp->qpn);
  292. return;
  293. }
  294. ibqp->event_handler(&event, ibqp->qp_context);
  295. }
  296. }
  297. static int send_wqe_overhead(enum mlx4_ib_qp_type type, u32 flags)
  298. {
  299. /*
  300. * UD WQEs must have a datagram segment.
  301. * RC and UC WQEs might have a remote address segment.
  302. * MLX WQEs need two extra inline data segments (for the UD
  303. * header and space for the ICRC).
  304. */
  305. switch (type) {
  306. case MLX4_IB_QPT_UD:
  307. return sizeof (struct mlx4_wqe_ctrl_seg) +
  308. sizeof (struct mlx4_wqe_datagram_seg) +
  309. ((flags & MLX4_IB_QP_LSO) ? MLX4_IB_LSO_HEADER_SPARE : 0);
  310. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  311. case MLX4_IB_QPT_PROXY_SMI:
  312. case MLX4_IB_QPT_PROXY_GSI:
  313. return sizeof (struct mlx4_wqe_ctrl_seg) +
  314. sizeof (struct mlx4_wqe_datagram_seg) + 64;
  315. case MLX4_IB_QPT_TUN_SMI_OWNER:
  316. case MLX4_IB_QPT_TUN_GSI:
  317. return sizeof (struct mlx4_wqe_ctrl_seg) +
  318. sizeof (struct mlx4_wqe_datagram_seg);
  319. case MLX4_IB_QPT_UC:
  320. return sizeof (struct mlx4_wqe_ctrl_seg) +
  321. sizeof (struct mlx4_wqe_raddr_seg);
  322. case MLX4_IB_QPT_RC:
  323. return sizeof (struct mlx4_wqe_ctrl_seg) +
  324. sizeof (struct mlx4_wqe_masked_atomic_seg) +
  325. sizeof (struct mlx4_wqe_raddr_seg);
  326. case MLX4_IB_QPT_SMI:
  327. case MLX4_IB_QPT_GSI:
  328. return sizeof (struct mlx4_wqe_ctrl_seg) +
  329. ALIGN(MLX4_IB_UD_HEADER_SIZE +
  330. DIV_ROUND_UP(MLX4_IB_UD_HEADER_SIZE,
  331. MLX4_INLINE_ALIGN) *
  332. sizeof (struct mlx4_wqe_inline_seg),
  333. sizeof (struct mlx4_wqe_data_seg)) +
  334. ALIGN(4 +
  335. sizeof (struct mlx4_wqe_inline_seg),
  336. sizeof (struct mlx4_wqe_data_seg));
  337. default:
  338. return sizeof (struct mlx4_wqe_ctrl_seg);
  339. }
  340. }
  341. static int set_rq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  342. int is_user, int has_rq, struct mlx4_ib_qp *qp)
  343. {
  344. /* Sanity check RQ size before proceeding */
  345. if (cap->max_recv_wr > dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE ||
  346. cap->max_recv_sge > min(dev->dev->caps.max_sq_sg, dev->dev->caps.max_rq_sg))
  347. return -EINVAL;
  348. if (!has_rq) {
  349. if (cap->max_recv_wr)
  350. return -EINVAL;
  351. qp->rq.wqe_cnt = qp->rq.max_gs = 0;
  352. } else {
  353. /* HW requires >= 1 RQ entry with >= 1 gather entry */
  354. if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge))
  355. return -EINVAL;
  356. qp->rq.wqe_cnt = roundup_pow_of_two(max(1U, cap->max_recv_wr));
  357. qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge));
  358. qp->rq.wqe_shift = ilog2(qp->rq.max_gs * sizeof (struct mlx4_wqe_data_seg));
  359. }
  360. /* leave userspace return values as they were, so as not to break ABI */
  361. if (is_user) {
  362. cap->max_recv_wr = qp->rq.max_post = qp->rq.wqe_cnt;
  363. cap->max_recv_sge = qp->rq.max_gs;
  364. } else {
  365. cap->max_recv_wr = qp->rq.max_post =
  366. min(dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE, qp->rq.wqe_cnt);
  367. cap->max_recv_sge = min(qp->rq.max_gs,
  368. min(dev->dev->caps.max_sq_sg,
  369. dev->dev->caps.max_rq_sg));
  370. }
  371. return 0;
  372. }
  373. static int set_kernel_sq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  374. enum mlx4_ib_qp_type type, struct mlx4_ib_qp *qp,
  375. bool shrink_wqe)
  376. {
  377. int s;
  378. /* Sanity check SQ size before proceeding */
  379. if (cap->max_send_wr > (dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE) ||
  380. cap->max_send_sge > min(dev->dev->caps.max_sq_sg, dev->dev->caps.max_rq_sg) ||
  381. cap->max_inline_data + send_wqe_overhead(type, qp->flags) +
  382. sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz)
  383. return -EINVAL;
  384. /*
  385. * For MLX transport we need 2 extra S/G entries:
  386. * one for the header and one for the checksum at the end
  387. */
  388. if ((type == MLX4_IB_QPT_SMI || type == MLX4_IB_QPT_GSI ||
  389. type & (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) &&
  390. cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg)
  391. return -EINVAL;
  392. s = max(cap->max_send_sge * sizeof (struct mlx4_wqe_data_seg),
  393. cap->max_inline_data + sizeof (struct mlx4_wqe_inline_seg)) +
  394. send_wqe_overhead(type, qp->flags);
  395. if (s > dev->dev->caps.max_sq_desc_sz)
  396. return -EINVAL;
  397. /*
  398. * Hermon supports shrinking WQEs, such that a single work
  399. * request can include multiple units of 1 << wqe_shift. This
  400. * way, work requests can differ in size, and do not have to
  401. * be a power of 2 in size, saving memory and speeding up send
  402. * WR posting. Unfortunately, if we do this then the
  403. * wqe_index field in CQEs can't be used to look up the WR ID
  404. * anymore, so we do this only if selective signaling is off.
  405. *
  406. * Further, on 32-bit platforms, we can't use vmap() to make
  407. * the QP buffer virtually contiguous. Thus we have to use
  408. * constant-sized WRs to make sure a WR is always fully within
  409. * a single page-sized chunk.
  410. *
  411. * Finally, we use NOP work requests to pad the end of the
  412. * work queue, to avoid wrap-around in the middle of WR. We
  413. * set NEC bit to avoid getting completions with error for
  414. * these NOP WRs, but since NEC is only supported starting
  415. * with firmware 2.2.232, we use constant-sized WRs for older
  416. * firmware.
  417. *
  418. * And, since MLX QPs only support SEND, we use constant-sized
  419. * WRs in this case.
  420. *
  421. * We look for the smallest value of wqe_shift such that the
  422. * resulting number of wqes does not exceed device
  423. * capabilities.
  424. *
  425. * We set WQE size to at least 64 bytes, this way stamping
  426. * invalidates each WQE.
  427. */
  428. if (shrink_wqe && dev->dev->caps.fw_ver >= MLX4_FW_VER_WQE_CTRL_NEC &&
  429. qp->sq_signal_bits && BITS_PER_LONG == 64 &&
  430. type != MLX4_IB_QPT_SMI && type != MLX4_IB_QPT_GSI &&
  431. !(type & (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_PROXY_SMI |
  432. MLX4_IB_QPT_PROXY_GSI | MLX4_IB_QPT_TUN_SMI_OWNER)))
  433. qp->sq.wqe_shift = ilog2(64);
  434. else
  435. qp->sq.wqe_shift = ilog2(roundup_pow_of_two(s));
  436. for (;;) {
  437. qp->sq_max_wqes_per_wr = DIV_ROUND_UP(s, 1U << qp->sq.wqe_shift);
  438. /*
  439. * We need to leave 2 KB + 1 WR of headroom in the SQ to
  440. * allow HW to prefetch.
  441. */
  442. qp->sq_spare_wqes = (2048 >> qp->sq.wqe_shift) + qp->sq_max_wqes_per_wr;
  443. qp->sq.wqe_cnt = roundup_pow_of_two(cap->max_send_wr *
  444. qp->sq_max_wqes_per_wr +
  445. qp->sq_spare_wqes);
  446. if (qp->sq.wqe_cnt <= dev->dev->caps.max_wqes)
  447. break;
  448. if (qp->sq_max_wqes_per_wr <= 1)
  449. return -EINVAL;
  450. ++qp->sq.wqe_shift;
  451. }
  452. qp->sq.max_gs = (min(dev->dev->caps.max_sq_desc_sz,
  453. (qp->sq_max_wqes_per_wr << qp->sq.wqe_shift)) -
  454. send_wqe_overhead(type, qp->flags)) /
  455. sizeof (struct mlx4_wqe_data_seg);
  456. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  457. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  458. if (qp->rq.wqe_shift > qp->sq.wqe_shift) {
  459. qp->rq.offset = 0;
  460. qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
  461. } else {
  462. qp->rq.offset = qp->sq.wqe_cnt << qp->sq.wqe_shift;
  463. qp->sq.offset = 0;
  464. }
  465. cap->max_send_wr = qp->sq.max_post =
  466. (qp->sq.wqe_cnt - qp->sq_spare_wqes) / qp->sq_max_wqes_per_wr;
  467. cap->max_send_sge = min(qp->sq.max_gs,
  468. min(dev->dev->caps.max_sq_sg,
  469. dev->dev->caps.max_rq_sg));
  470. /* We don't support inline sends for kernel QPs (yet) */
  471. cap->max_inline_data = 0;
  472. return 0;
  473. }
  474. static int set_user_sq_size(struct mlx4_ib_dev *dev,
  475. struct mlx4_ib_qp *qp,
  476. struct mlx4_ib_create_qp *ucmd)
  477. {
  478. /* Sanity check SQ size before proceeding */
  479. if ((1 << ucmd->log_sq_bb_count) > dev->dev->caps.max_wqes ||
  480. ucmd->log_sq_stride >
  481. ilog2(roundup_pow_of_two(dev->dev->caps.max_sq_desc_sz)) ||
  482. ucmd->log_sq_stride < MLX4_IB_MIN_SQ_STRIDE)
  483. return -EINVAL;
  484. qp->sq.wqe_cnt = 1 << ucmd->log_sq_bb_count;
  485. qp->sq.wqe_shift = ucmd->log_sq_stride;
  486. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  487. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  488. return 0;
  489. }
  490. static int alloc_proxy_bufs(struct ib_device *dev, struct mlx4_ib_qp *qp)
  491. {
  492. int i;
  493. qp->sqp_proxy_rcv =
  494. kmalloc(sizeof (struct mlx4_ib_buf) * qp->rq.wqe_cnt,
  495. GFP_KERNEL);
  496. if (!qp->sqp_proxy_rcv)
  497. return -ENOMEM;
  498. for (i = 0; i < qp->rq.wqe_cnt; i++) {
  499. qp->sqp_proxy_rcv[i].addr =
  500. kmalloc(sizeof (struct mlx4_ib_proxy_sqp_hdr),
  501. GFP_KERNEL);
  502. if (!qp->sqp_proxy_rcv[i].addr)
  503. goto err;
  504. qp->sqp_proxy_rcv[i].map =
  505. ib_dma_map_single(dev, qp->sqp_proxy_rcv[i].addr,
  506. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  507. DMA_FROM_DEVICE);
  508. if (ib_dma_mapping_error(dev, qp->sqp_proxy_rcv[i].map)) {
  509. kfree(qp->sqp_proxy_rcv[i].addr);
  510. goto err;
  511. }
  512. }
  513. return 0;
  514. err:
  515. while (i > 0) {
  516. --i;
  517. ib_dma_unmap_single(dev, qp->sqp_proxy_rcv[i].map,
  518. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  519. DMA_FROM_DEVICE);
  520. kfree(qp->sqp_proxy_rcv[i].addr);
  521. }
  522. kfree(qp->sqp_proxy_rcv);
  523. qp->sqp_proxy_rcv = NULL;
  524. return -ENOMEM;
  525. }
  526. static void free_proxy_bufs(struct ib_device *dev, struct mlx4_ib_qp *qp)
  527. {
  528. int i;
  529. for (i = 0; i < qp->rq.wqe_cnt; i++) {
  530. ib_dma_unmap_single(dev, qp->sqp_proxy_rcv[i].map,
  531. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  532. DMA_FROM_DEVICE);
  533. kfree(qp->sqp_proxy_rcv[i].addr);
  534. }
  535. kfree(qp->sqp_proxy_rcv);
  536. }
  537. static int qp_has_rq(struct ib_qp_init_attr *attr)
  538. {
  539. if (attr->qp_type == IB_QPT_XRC_INI || attr->qp_type == IB_QPT_XRC_TGT)
  540. return 0;
  541. return !attr->srq;
  542. }
  543. static int qp0_enabled_vf(struct mlx4_dev *dev, int qpn)
  544. {
  545. int i;
  546. for (i = 0; i < dev->caps.num_ports; i++) {
  547. if (qpn == dev->caps.qp0_proxy[i])
  548. return !!dev->caps.qp0_qkey[i];
  549. }
  550. return 0;
  551. }
  552. static void mlx4_ib_free_qp_counter(struct mlx4_ib_dev *dev,
  553. struct mlx4_ib_qp *qp)
  554. {
  555. mutex_lock(&dev->counters_table[qp->port - 1].mutex);
  556. mlx4_counter_free(dev->dev, qp->counter_index->index);
  557. list_del(&qp->counter_index->list);
  558. mutex_unlock(&dev->counters_table[qp->port - 1].mutex);
  559. kfree(qp->counter_index);
  560. qp->counter_index = NULL;
  561. }
  562. static int create_qp_common(struct mlx4_ib_dev *dev, struct ib_pd *pd,
  563. struct ib_qp_init_attr *init_attr,
  564. struct ib_udata *udata, int sqpn, struct mlx4_ib_qp **caller_qp,
  565. gfp_t gfp)
  566. {
  567. int qpn;
  568. int err;
  569. struct ib_qp_cap backup_cap;
  570. struct mlx4_ib_sqp *sqp = NULL;
  571. struct mlx4_ib_qp *qp;
  572. enum mlx4_ib_qp_type qp_type = (enum mlx4_ib_qp_type) init_attr->qp_type;
  573. struct mlx4_ib_cq *mcq;
  574. unsigned long flags;
  575. /* When tunneling special qps, we use a plain UD qp */
  576. if (sqpn) {
  577. if (mlx4_is_mfunc(dev->dev) &&
  578. (!mlx4_is_master(dev->dev) ||
  579. !(init_attr->create_flags & MLX4_IB_SRIOV_SQP))) {
  580. if (init_attr->qp_type == IB_QPT_GSI)
  581. qp_type = MLX4_IB_QPT_PROXY_GSI;
  582. else {
  583. if (mlx4_is_master(dev->dev) ||
  584. qp0_enabled_vf(dev->dev, sqpn))
  585. qp_type = MLX4_IB_QPT_PROXY_SMI_OWNER;
  586. else
  587. qp_type = MLX4_IB_QPT_PROXY_SMI;
  588. }
  589. }
  590. qpn = sqpn;
  591. /* add extra sg entry for tunneling */
  592. init_attr->cap.max_recv_sge++;
  593. } else if (init_attr->create_flags & MLX4_IB_SRIOV_TUNNEL_QP) {
  594. struct mlx4_ib_qp_tunnel_init_attr *tnl_init =
  595. container_of(init_attr,
  596. struct mlx4_ib_qp_tunnel_init_attr, init_attr);
  597. if ((tnl_init->proxy_qp_type != IB_QPT_SMI &&
  598. tnl_init->proxy_qp_type != IB_QPT_GSI) ||
  599. !mlx4_is_master(dev->dev))
  600. return -EINVAL;
  601. if (tnl_init->proxy_qp_type == IB_QPT_GSI)
  602. qp_type = MLX4_IB_QPT_TUN_GSI;
  603. else if (tnl_init->slave == mlx4_master_func_num(dev->dev) ||
  604. mlx4_vf_smi_enabled(dev->dev, tnl_init->slave,
  605. tnl_init->port))
  606. qp_type = MLX4_IB_QPT_TUN_SMI_OWNER;
  607. else
  608. qp_type = MLX4_IB_QPT_TUN_SMI;
  609. /* we are definitely in the PPF here, since we are creating
  610. * tunnel QPs. base_tunnel_sqpn is therefore valid. */
  611. qpn = dev->dev->phys_caps.base_tunnel_sqpn + 8 * tnl_init->slave
  612. + tnl_init->proxy_qp_type * 2 + tnl_init->port - 1;
  613. sqpn = qpn;
  614. }
  615. if (!*caller_qp) {
  616. if (qp_type == MLX4_IB_QPT_SMI || qp_type == MLX4_IB_QPT_GSI ||
  617. (qp_type & (MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_SMI_OWNER |
  618. MLX4_IB_QPT_PROXY_GSI | MLX4_IB_QPT_TUN_SMI_OWNER))) {
  619. sqp = kzalloc(sizeof (struct mlx4_ib_sqp), gfp);
  620. if (!sqp)
  621. return -ENOMEM;
  622. qp = &sqp->qp;
  623. qp->pri.vid = 0xFFFF;
  624. qp->alt.vid = 0xFFFF;
  625. } else {
  626. qp = kzalloc(sizeof (struct mlx4_ib_qp), gfp);
  627. if (!qp)
  628. return -ENOMEM;
  629. qp->pri.vid = 0xFFFF;
  630. qp->alt.vid = 0xFFFF;
  631. }
  632. } else
  633. qp = *caller_qp;
  634. qp->mlx4_ib_qp_type = qp_type;
  635. mutex_init(&qp->mutex);
  636. spin_lock_init(&qp->sq.lock);
  637. spin_lock_init(&qp->rq.lock);
  638. INIT_LIST_HEAD(&qp->gid_list);
  639. INIT_LIST_HEAD(&qp->steering_rules);
  640. qp->state = IB_QPS_RESET;
  641. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  642. qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  643. err = set_rq_size(dev, &init_attr->cap, !!pd->uobject, qp_has_rq(init_attr), qp);
  644. if (err)
  645. goto err;
  646. if (pd->uobject) {
  647. struct mlx4_ib_create_qp ucmd;
  648. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  649. err = -EFAULT;
  650. goto err;
  651. }
  652. qp->sq_no_prefetch = ucmd.sq_no_prefetch;
  653. err = set_user_sq_size(dev, qp, &ucmd);
  654. if (err)
  655. goto err;
  656. qp->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
  657. qp->buf_size, 0, 0);
  658. if (IS_ERR(qp->umem)) {
  659. err = PTR_ERR(qp->umem);
  660. goto err;
  661. }
  662. err = mlx4_mtt_init(dev->dev, ib_umem_page_count(qp->umem),
  663. qp->umem->page_shift, &qp->mtt);
  664. if (err)
  665. goto err_buf;
  666. err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
  667. if (err)
  668. goto err_mtt;
  669. if (qp_has_rq(init_attr)) {
  670. err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
  671. ucmd.db_addr, &qp->db);
  672. if (err)
  673. goto err_mtt;
  674. }
  675. } else {
  676. qp->sq_no_prefetch = 0;
  677. if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
  678. qp->flags |= MLX4_IB_QP_LSO;
  679. if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) {
  680. if (dev->steering_support ==
  681. MLX4_STEERING_MODE_DEVICE_MANAGED)
  682. qp->flags |= MLX4_IB_QP_NETIF;
  683. else
  684. goto err;
  685. }
  686. memcpy(&backup_cap, &init_attr->cap, sizeof(backup_cap));
  687. err = set_kernel_sq_size(dev, &init_attr->cap,
  688. qp_type, qp, true);
  689. if (err)
  690. goto err;
  691. if (qp_has_rq(init_attr)) {
  692. err = mlx4_db_alloc(dev->dev, &qp->db, 0, gfp);
  693. if (err)
  694. goto err;
  695. *qp->db.db = 0;
  696. }
  697. if (mlx4_buf_alloc(dev->dev, qp->buf_size, qp->buf_size,
  698. &qp->buf, gfp)) {
  699. memcpy(&init_attr->cap, &backup_cap,
  700. sizeof(backup_cap));
  701. err = set_kernel_sq_size(dev, &init_attr->cap, qp_type,
  702. qp, false);
  703. if (err)
  704. goto err_db;
  705. if (mlx4_buf_alloc(dev->dev, qp->buf_size,
  706. PAGE_SIZE * 2, &qp->buf, gfp)) {
  707. err = -ENOMEM;
  708. goto err_db;
  709. }
  710. }
  711. err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift,
  712. &qp->mtt);
  713. if (err)
  714. goto err_buf;
  715. err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf, gfp);
  716. if (err)
  717. goto err_mtt;
  718. qp->sq.wrid = kmalloc_array(qp->sq.wqe_cnt, sizeof(u64),
  719. gfp | __GFP_NOWARN);
  720. if (!qp->sq.wrid)
  721. qp->sq.wrid = __vmalloc(qp->sq.wqe_cnt * sizeof(u64),
  722. gfp, PAGE_KERNEL);
  723. qp->rq.wrid = kmalloc_array(qp->rq.wqe_cnt, sizeof(u64),
  724. gfp | __GFP_NOWARN);
  725. if (!qp->rq.wrid)
  726. qp->rq.wrid = __vmalloc(qp->rq.wqe_cnt * sizeof(u64),
  727. gfp, PAGE_KERNEL);
  728. if (!qp->sq.wrid || !qp->rq.wrid) {
  729. err = -ENOMEM;
  730. goto err_wrid;
  731. }
  732. }
  733. if (sqpn) {
  734. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  735. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
  736. if (alloc_proxy_bufs(pd->device, qp)) {
  737. err = -ENOMEM;
  738. goto err_wrid;
  739. }
  740. }
  741. } else {
  742. /* Raw packet QPNs may not have bits 6,7 set in their qp_num;
  743. * otherwise, the WQE BlueFlame setup flow wrongly causes
  744. * VLAN insertion. */
  745. if (init_attr->qp_type == IB_QPT_RAW_PACKET)
  746. err = mlx4_qp_reserve_range(dev->dev, 1, 1, &qpn,
  747. (init_attr->cap.max_send_wr ?
  748. MLX4_RESERVE_ETH_BF_QP : 0) |
  749. (init_attr->cap.max_recv_wr ?
  750. MLX4_RESERVE_A0_QP : 0));
  751. else
  752. if (qp->flags & MLX4_IB_QP_NETIF)
  753. err = mlx4_ib_steer_qp_alloc(dev, 1, &qpn);
  754. else
  755. err = mlx4_qp_reserve_range(dev->dev, 1, 1,
  756. &qpn, 0);
  757. if (err)
  758. goto err_proxy;
  759. }
  760. if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
  761. qp->flags |= MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
  762. err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp, gfp);
  763. if (err)
  764. goto err_qpn;
  765. if (init_attr->qp_type == IB_QPT_XRC_TGT)
  766. qp->mqp.qpn |= (1 << 23);
  767. /*
  768. * Hardware wants QPN written in big-endian order (after
  769. * shifting) for send doorbell. Precompute this value to save
  770. * a little bit when posting sends.
  771. */
  772. qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
  773. qp->mqp.event = mlx4_ib_qp_event;
  774. if (!*caller_qp)
  775. *caller_qp = qp;
  776. spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
  777. mlx4_ib_lock_cqs(to_mcq(init_attr->send_cq),
  778. to_mcq(init_attr->recv_cq));
  779. /* Maintain device to QPs access, needed for further handling
  780. * via reset flow
  781. */
  782. list_add_tail(&qp->qps_list, &dev->qp_list);
  783. /* Maintain CQ to QPs access, needed for further handling
  784. * via reset flow
  785. */
  786. mcq = to_mcq(init_attr->send_cq);
  787. list_add_tail(&qp->cq_send_list, &mcq->send_qp_list);
  788. mcq = to_mcq(init_attr->recv_cq);
  789. list_add_tail(&qp->cq_recv_list, &mcq->recv_qp_list);
  790. mlx4_ib_unlock_cqs(to_mcq(init_attr->send_cq),
  791. to_mcq(init_attr->recv_cq));
  792. spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
  793. return 0;
  794. err_qpn:
  795. if (!sqpn) {
  796. if (qp->flags & MLX4_IB_QP_NETIF)
  797. mlx4_ib_steer_qp_free(dev, qpn, 1);
  798. else
  799. mlx4_qp_release_range(dev->dev, qpn, 1);
  800. }
  801. err_proxy:
  802. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI)
  803. free_proxy_bufs(pd->device, qp);
  804. err_wrid:
  805. if (pd->uobject) {
  806. if (qp_has_rq(init_attr))
  807. mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &qp->db);
  808. } else {
  809. kvfree(qp->sq.wrid);
  810. kvfree(qp->rq.wrid);
  811. }
  812. err_mtt:
  813. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  814. err_buf:
  815. if (pd->uobject)
  816. ib_umem_release(qp->umem);
  817. else
  818. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  819. err_db:
  820. if (!pd->uobject && qp_has_rq(init_attr))
  821. mlx4_db_free(dev->dev, &qp->db);
  822. err:
  823. if (sqp)
  824. kfree(sqp);
  825. else if (!*caller_qp)
  826. kfree(qp);
  827. return err;
  828. }
  829. static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
  830. {
  831. switch (state) {
  832. case IB_QPS_RESET: return MLX4_QP_STATE_RST;
  833. case IB_QPS_INIT: return MLX4_QP_STATE_INIT;
  834. case IB_QPS_RTR: return MLX4_QP_STATE_RTR;
  835. case IB_QPS_RTS: return MLX4_QP_STATE_RTS;
  836. case IB_QPS_SQD: return MLX4_QP_STATE_SQD;
  837. case IB_QPS_SQE: return MLX4_QP_STATE_SQER;
  838. case IB_QPS_ERR: return MLX4_QP_STATE_ERR;
  839. default: return -1;
  840. }
  841. }
  842. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  843. __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
  844. {
  845. if (send_cq == recv_cq) {
  846. spin_lock(&send_cq->lock);
  847. __acquire(&recv_cq->lock);
  848. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  849. spin_lock(&send_cq->lock);
  850. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  851. } else {
  852. spin_lock(&recv_cq->lock);
  853. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  854. }
  855. }
  856. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  857. __releases(&send_cq->lock) __releases(&recv_cq->lock)
  858. {
  859. if (send_cq == recv_cq) {
  860. __release(&recv_cq->lock);
  861. spin_unlock(&send_cq->lock);
  862. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  863. spin_unlock(&recv_cq->lock);
  864. spin_unlock(&send_cq->lock);
  865. } else {
  866. spin_unlock(&send_cq->lock);
  867. spin_unlock(&recv_cq->lock);
  868. }
  869. }
  870. static void del_gid_entries(struct mlx4_ib_qp *qp)
  871. {
  872. struct mlx4_ib_gid_entry *ge, *tmp;
  873. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  874. list_del(&ge->list);
  875. kfree(ge);
  876. }
  877. }
  878. static struct mlx4_ib_pd *get_pd(struct mlx4_ib_qp *qp)
  879. {
  880. if (qp->ibqp.qp_type == IB_QPT_XRC_TGT)
  881. return to_mpd(to_mxrcd(qp->ibqp.xrcd)->pd);
  882. else
  883. return to_mpd(qp->ibqp.pd);
  884. }
  885. static void get_cqs(struct mlx4_ib_qp *qp,
  886. struct mlx4_ib_cq **send_cq, struct mlx4_ib_cq **recv_cq)
  887. {
  888. switch (qp->ibqp.qp_type) {
  889. case IB_QPT_XRC_TGT:
  890. *send_cq = to_mcq(to_mxrcd(qp->ibqp.xrcd)->cq);
  891. *recv_cq = *send_cq;
  892. break;
  893. case IB_QPT_XRC_INI:
  894. *send_cq = to_mcq(qp->ibqp.send_cq);
  895. *recv_cq = *send_cq;
  896. break;
  897. default:
  898. *send_cq = to_mcq(qp->ibqp.send_cq);
  899. *recv_cq = to_mcq(qp->ibqp.recv_cq);
  900. break;
  901. }
  902. }
  903. static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
  904. int is_user)
  905. {
  906. struct mlx4_ib_cq *send_cq, *recv_cq;
  907. unsigned long flags;
  908. if (qp->state != IB_QPS_RESET) {
  909. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  910. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  911. pr_warn("modify QP %06x to RESET failed.\n",
  912. qp->mqp.qpn);
  913. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) {
  914. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  915. qp->pri.smac = 0;
  916. qp->pri.smac_port = 0;
  917. }
  918. if (qp->alt.smac) {
  919. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  920. qp->alt.smac = 0;
  921. }
  922. if (qp->pri.vid < 0x1000) {
  923. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, qp->pri.vid);
  924. qp->pri.vid = 0xFFFF;
  925. qp->pri.candidate_vid = 0xFFFF;
  926. qp->pri.update_vid = 0;
  927. }
  928. if (qp->alt.vid < 0x1000) {
  929. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, qp->alt.vid);
  930. qp->alt.vid = 0xFFFF;
  931. qp->alt.candidate_vid = 0xFFFF;
  932. qp->alt.update_vid = 0;
  933. }
  934. }
  935. get_cqs(qp, &send_cq, &recv_cq);
  936. spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
  937. mlx4_ib_lock_cqs(send_cq, recv_cq);
  938. /* del from lists under both locks above to protect reset flow paths */
  939. list_del(&qp->qps_list);
  940. list_del(&qp->cq_send_list);
  941. list_del(&qp->cq_recv_list);
  942. if (!is_user) {
  943. __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  944. qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
  945. if (send_cq != recv_cq)
  946. __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  947. }
  948. mlx4_qp_remove(dev->dev, &qp->mqp);
  949. mlx4_ib_unlock_cqs(send_cq, recv_cq);
  950. spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
  951. mlx4_qp_free(dev->dev, &qp->mqp);
  952. if (!is_sqp(dev, qp) && !is_tunnel_qp(dev, qp)) {
  953. if (qp->flags & MLX4_IB_QP_NETIF)
  954. mlx4_ib_steer_qp_free(dev, qp->mqp.qpn, 1);
  955. else
  956. mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1);
  957. }
  958. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  959. if (is_user) {
  960. if (qp->rq.wqe_cnt)
  961. mlx4_ib_db_unmap_user(to_mucontext(qp->ibqp.uobject->context),
  962. &qp->db);
  963. ib_umem_release(qp->umem);
  964. } else {
  965. kvfree(qp->sq.wrid);
  966. kvfree(qp->rq.wrid);
  967. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  968. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI))
  969. free_proxy_bufs(&dev->ib_dev, qp);
  970. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  971. if (qp->rq.wqe_cnt)
  972. mlx4_db_free(dev->dev, &qp->db);
  973. }
  974. del_gid_entries(qp);
  975. }
  976. static u32 get_sqp_num(struct mlx4_ib_dev *dev, struct ib_qp_init_attr *attr)
  977. {
  978. /* Native or PPF */
  979. if (!mlx4_is_mfunc(dev->dev) ||
  980. (mlx4_is_master(dev->dev) &&
  981. attr->create_flags & MLX4_IB_SRIOV_SQP)) {
  982. return dev->dev->phys_caps.base_sqpn +
  983. (attr->qp_type == IB_QPT_SMI ? 0 : 2) +
  984. attr->port_num - 1;
  985. }
  986. /* PF or VF -- creating proxies */
  987. if (attr->qp_type == IB_QPT_SMI)
  988. return dev->dev->caps.qp0_proxy[attr->port_num - 1];
  989. else
  990. return dev->dev->caps.qp1_proxy[attr->port_num - 1];
  991. }
  992. static struct ib_qp *_mlx4_ib_create_qp(struct ib_pd *pd,
  993. struct ib_qp_init_attr *init_attr,
  994. struct ib_udata *udata)
  995. {
  996. struct mlx4_ib_qp *qp = NULL;
  997. int err;
  998. int sup_u_create_flags = MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
  999. u16 xrcdn = 0;
  1000. gfp_t gfp;
  1001. gfp = (init_attr->create_flags & MLX4_IB_QP_CREATE_USE_GFP_NOIO) ?
  1002. GFP_NOIO : GFP_KERNEL;
  1003. /*
  1004. * We only support LSO, vendor flag1, and multicast loopback blocking,
  1005. * and only for kernel UD QPs.
  1006. */
  1007. if (init_attr->create_flags & ~(MLX4_IB_QP_LSO |
  1008. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK |
  1009. MLX4_IB_SRIOV_TUNNEL_QP |
  1010. MLX4_IB_SRIOV_SQP |
  1011. MLX4_IB_QP_NETIF |
  1012. MLX4_IB_QP_CREATE_ROCE_V2_GSI |
  1013. MLX4_IB_QP_CREATE_USE_GFP_NOIO))
  1014. return ERR_PTR(-EINVAL);
  1015. if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) {
  1016. if (init_attr->qp_type != IB_QPT_UD)
  1017. return ERR_PTR(-EINVAL);
  1018. }
  1019. if (init_attr->create_flags) {
  1020. if (udata && init_attr->create_flags & ~(sup_u_create_flags))
  1021. return ERR_PTR(-EINVAL);
  1022. if ((init_attr->create_flags & ~(MLX4_IB_SRIOV_SQP |
  1023. MLX4_IB_QP_CREATE_USE_GFP_NOIO |
  1024. MLX4_IB_QP_CREATE_ROCE_V2_GSI |
  1025. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) &&
  1026. init_attr->qp_type != IB_QPT_UD) ||
  1027. (init_attr->create_flags & MLX4_IB_SRIOV_SQP &&
  1028. init_attr->qp_type > IB_QPT_GSI) ||
  1029. (init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI &&
  1030. init_attr->qp_type != IB_QPT_GSI))
  1031. return ERR_PTR(-EINVAL);
  1032. }
  1033. switch (init_attr->qp_type) {
  1034. case IB_QPT_XRC_TGT:
  1035. pd = to_mxrcd(init_attr->xrcd)->pd;
  1036. xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
  1037. init_attr->send_cq = to_mxrcd(init_attr->xrcd)->cq;
  1038. /* fall through */
  1039. case IB_QPT_XRC_INI:
  1040. if (!(to_mdev(pd->device)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
  1041. return ERR_PTR(-ENOSYS);
  1042. init_attr->recv_cq = init_attr->send_cq;
  1043. /* fall through */
  1044. case IB_QPT_RC:
  1045. case IB_QPT_UC:
  1046. case IB_QPT_RAW_PACKET:
  1047. qp = kzalloc(sizeof *qp, gfp);
  1048. if (!qp)
  1049. return ERR_PTR(-ENOMEM);
  1050. qp->pri.vid = 0xFFFF;
  1051. qp->alt.vid = 0xFFFF;
  1052. /* fall through */
  1053. case IB_QPT_UD:
  1054. {
  1055. err = create_qp_common(to_mdev(pd->device), pd, init_attr,
  1056. udata, 0, &qp, gfp);
  1057. if (err) {
  1058. kfree(qp);
  1059. return ERR_PTR(err);
  1060. }
  1061. qp->ibqp.qp_num = qp->mqp.qpn;
  1062. qp->xrcdn = xrcdn;
  1063. break;
  1064. }
  1065. case IB_QPT_SMI:
  1066. case IB_QPT_GSI:
  1067. {
  1068. int sqpn;
  1069. /* Userspace is not allowed to create special QPs: */
  1070. if (udata)
  1071. return ERR_PTR(-EINVAL);
  1072. if (init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI) {
  1073. int res = mlx4_qp_reserve_range(to_mdev(pd->device)->dev, 1, 1, &sqpn, 0);
  1074. if (res)
  1075. return ERR_PTR(res);
  1076. } else {
  1077. sqpn = get_sqp_num(to_mdev(pd->device), init_attr);
  1078. }
  1079. err = create_qp_common(to_mdev(pd->device), pd, init_attr, udata,
  1080. sqpn,
  1081. &qp, gfp);
  1082. if (err)
  1083. return ERR_PTR(err);
  1084. qp->port = init_attr->port_num;
  1085. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 :
  1086. init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI ? sqpn : 1;
  1087. break;
  1088. }
  1089. default:
  1090. /* Don't support raw QPs */
  1091. return ERR_PTR(-EINVAL);
  1092. }
  1093. return &qp->ibqp;
  1094. }
  1095. struct ib_qp *mlx4_ib_create_qp(struct ib_pd *pd,
  1096. struct ib_qp_init_attr *init_attr,
  1097. struct ib_udata *udata) {
  1098. struct ib_device *device = pd ? pd->device : init_attr->xrcd->device;
  1099. struct ib_qp *ibqp;
  1100. struct mlx4_ib_dev *dev = to_mdev(device);
  1101. ibqp = _mlx4_ib_create_qp(pd, init_attr, udata);
  1102. if (!IS_ERR(ibqp) &&
  1103. (init_attr->qp_type == IB_QPT_GSI) &&
  1104. !(init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI)) {
  1105. struct mlx4_ib_sqp *sqp = to_msqp((to_mqp(ibqp)));
  1106. int is_eth = rdma_cap_eth_ah(&dev->ib_dev, init_attr->port_num);
  1107. if (is_eth &&
  1108. dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) {
  1109. init_attr->create_flags |= MLX4_IB_QP_CREATE_ROCE_V2_GSI;
  1110. sqp->roce_v2_gsi = ib_create_qp(pd, init_attr);
  1111. if (IS_ERR(sqp->roce_v2_gsi)) {
  1112. pr_err("Failed to create GSI QP for RoCEv2 (%ld)\n", PTR_ERR(sqp->roce_v2_gsi));
  1113. sqp->roce_v2_gsi = NULL;
  1114. } else {
  1115. sqp = to_msqp(to_mqp(sqp->roce_v2_gsi));
  1116. sqp->qp.flags |= MLX4_IB_ROCE_V2_GSI_QP;
  1117. }
  1118. init_attr->create_flags &= ~MLX4_IB_QP_CREATE_ROCE_V2_GSI;
  1119. }
  1120. }
  1121. return ibqp;
  1122. }
  1123. static int _mlx4_ib_destroy_qp(struct ib_qp *qp)
  1124. {
  1125. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  1126. struct mlx4_ib_qp *mqp = to_mqp(qp);
  1127. struct mlx4_ib_pd *pd;
  1128. if (is_qp0(dev, mqp))
  1129. mlx4_CLOSE_PORT(dev->dev, mqp->port);
  1130. if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI &&
  1131. dev->qp1_proxy[mqp->port - 1] == mqp) {
  1132. mutex_lock(&dev->qp1_proxy_lock[mqp->port - 1]);
  1133. dev->qp1_proxy[mqp->port - 1] = NULL;
  1134. mutex_unlock(&dev->qp1_proxy_lock[mqp->port - 1]);
  1135. }
  1136. if (mqp->counter_index)
  1137. mlx4_ib_free_qp_counter(dev, mqp);
  1138. pd = get_pd(mqp);
  1139. destroy_qp_common(dev, mqp, !!pd->ibpd.uobject);
  1140. if (is_sqp(dev, mqp))
  1141. kfree(to_msqp(mqp));
  1142. else
  1143. kfree(mqp);
  1144. return 0;
  1145. }
  1146. int mlx4_ib_destroy_qp(struct ib_qp *qp)
  1147. {
  1148. struct mlx4_ib_qp *mqp = to_mqp(qp);
  1149. if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  1150. struct mlx4_ib_sqp *sqp = to_msqp(mqp);
  1151. if (sqp->roce_v2_gsi)
  1152. ib_destroy_qp(sqp->roce_v2_gsi);
  1153. }
  1154. return _mlx4_ib_destroy_qp(qp);
  1155. }
  1156. static int to_mlx4_st(struct mlx4_ib_dev *dev, enum mlx4_ib_qp_type type)
  1157. {
  1158. switch (type) {
  1159. case MLX4_IB_QPT_RC: return MLX4_QP_ST_RC;
  1160. case MLX4_IB_QPT_UC: return MLX4_QP_ST_UC;
  1161. case MLX4_IB_QPT_UD: return MLX4_QP_ST_UD;
  1162. case MLX4_IB_QPT_XRC_INI:
  1163. case MLX4_IB_QPT_XRC_TGT: return MLX4_QP_ST_XRC;
  1164. case MLX4_IB_QPT_SMI:
  1165. case MLX4_IB_QPT_GSI:
  1166. case MLX4_IB_QPT_RAW_PACKET: return MLX4_QP_ST_MLX;
  1167. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  1168. case MLX4_IB_QPT_TUN_SMI_OWNER: return (mlx4_is_mfunc(dev->dev) ?
  1169. MLX4_QP_ST_MLX : -1);
  1170. case MLX4_IB_QPT_PROXY_SMI:
  1171. case MLX4_IB_QPT_TUN_SMI:
  1172. case MLX4_IB_QPT_PROXY_GSI:
  1173. case MLX4_IB_QPT_TUN_GSI: return (mlx4_is_mfunc(dev->dev) ?
  1174. MLX4_QP_ST_UD : -1);
  1175. default: return -1;
  1176. }
  1177. }
  1178. static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
  1179. int attr_mask)
  1180. {
  1181. u8 dest_rd_atomic;
  1182. u32 access_flags;
  1183. u32 hw_access_flags = 0;
  1184. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1185. dest_rd_atomic = attr->max_dest_rd_atomic;
  1186. else
  1187. dest_rd_atomic = qp->resp_depth;
  1188. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1189. access_flags = attr->qp_access_flags;
  1190. else
  1191. access_flags = qp->atomic_rd_en;
  1192. if (!dest_rd_atomic)
  1193. access_flags &= IB_ACCESS_REMOTE_WRITE;
  1194. if (access_flags & IB_ACCESS_REMOTE_READ)
  1195. hw_access_flags |= MLX4_QP_BIT_RRE;
  1196. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  1197. hw_access_flags |= MLX4_QP_BIT_RAE;
  1198. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  1199. hw_access_flags |= MLX4_QP_BIT_RWE;
  1200. return cpu_to_be32(hw_access_flags);
  1201. }
  1202. static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
  1203. int attr_mask)
  1204. {
  1205. if (attr_mask & IB_QP_PKEY_INDEX)
  1206. sqp->pkey_index = attr->pkey_index;
  1207. if (attr_mask & IB_QP_QKEY)
  1208. sqp->qkey = attr->qkey;
  1209. if (attr_mask & IB_QP_SQ_PSN)
  1210. sqp->send_psn = attr->sq_psn;
  1211. }
  1212. static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
  1213. {
  1214. path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
  1215. }
  1216. static int _mlx4_set_path(struct mlx4_ib_dev *dev,
  1217. const struct rdma_ah_attr *ah,
  1218. u64 smac, u16 vlan_tag, struct mlx4_qp_path *path,
  1219. struct mlx4_roce_smac_vlan_info *smac_info, u8 port)
  1220. {
  1221. int vidx;
  1222. int smac_index;
  1223. int err;
  1224. path->grh_mylmc = rdma_ah_get_path_bits(ah) & 0x7f;
  1225. path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
  1226. if (rdma_ah_get_static_rate(ah)) {
  1227. path->static_rate = rdma_ah_get_static_rate(ah) +
  1228. MLX4_STAT_RATE_OFFSET;
  1229. while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  1230. !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
  1231. --path->static_rate;
  1232. } else
  1233. path->static_rate = 0;
  1234. if (rdma_ah_get_ah_flags(ah) & IB_AH_GRH) {
  1235. const struct ib_global_route *grh = rdma_ah_read_grh(ah);
  1236. int real_sgid_index =
  1237. mlx4_ib_gid_index_to_real_index(dev, port,
  1238. grh->sgid_index);
  1239. if (real_sgid_index >= dev->dev->caps.gid_table_len[port]) {
  1240. pr_err("sgid_index (%u) too large. max is %d\n",
  1241. real_sgid_index, dev->dev->caps.gid_table_len[port] - 1);
  1242. return -1;
  1243. }
  1244. path->grh_mylmc |= 1 << 7;
  1245. path->mgid_index = real_sgid_index;
  1246. path->hop_limit = grh->hop_limit;
  1247. path->tclass_flowlabel =
  1248. cpu_to_be32((grh->traffic_class << 20) |
  1249. (grh->flow_label));
  1250. memcpy(path->rgid, grh->dgid.raw, 16);
  1251. }
  1252. if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
  1253. if (!(rdma_ah_get_ah_flags(ah) & IB_AH_GRH))
  1254. return -1;
  1255. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  1256. ((port - 1) << 6) | ((rdma_ah_get_sl(ah) & 7) << 3);
  1257. path->feup |= MLX4_FEUP_FORCE_ETH_UP;
  1258. if (vlan_tag < 0x1000) {
  1259. if (smac_info->vid < 0x1000) {
  1260. /* both valid vlan ids */
  1261. if (smac_info->vid != vlan_tag) {
  1262. /* different VIDs. unreg old and reg new */
  1263. err = mlx4_register_vlan(dev->dev, port, vlan_tag, &vidx);
  1264. if (err)
  1265. return err;
  1266. smac_info->candidate_vid = vlan_tag;
  1267. smac_info->candidate_vlan_index = vidx;
  1268. smac_info->candidate_vlan_port = port;
  1269. smac_info->update_vid = 1;
  1270. path->vlan_index = vidx;
  1271. } else {
  1272. path->vlan_index = smac_info->vlan_index;
  1273. }
  1274. } else {
  1275. /* no current vlan tag in qp */
  1276. err = mlx4_register_vlan(dev->dev, port, vlan_tag, &vidx);
  1277. if (err)
  1278. return err;
  1279. smac_info->candidate_vid = vlan_tag;
  1280. smac_info->candidate_vlan_index = vidx;
  1281. smac_info->candidate_vlan_port = port;
  1282. smac_info->update_vid = 1;
  1283. path->vlan_index = vidx;
  1284. }
  1285. path->feup |= MLX4_FVL_FORCE_ETH_VLAN;
  1286. path->fl = 1 << 6;
  1287. } else {
  1288. /* have current vlan tag. unregister it at modify-qp success */
  1289. if (smac_info->vid < 0x1000) {
  1290. smac_info->candidate_vid = 0xFFFF;
  1291. smac_info->update_vid = 1;
  1292. }
  1293. }
  1294. /* get smac_index for RoCE use.
  1295. * If no smac was yet assigned, register one.
  1296. * If one was already assigned, but the new mac differs,
  1297. * unregister the old one and register the new one.
  1298. */
  1299. if ((!smac_info->smac && !smac_info->smac_port) ||
  1300. smac_info->smac != smac) {
  1301. /* register candidate now, unreg if needed, after success */
  1302. smac_index = mlx4_register_mac(dev->dev, port, smac);
  1303. if (smac_index >= 0) {
  1304. smac_info->candidate_smac_index = smac_index;
  1305. smac_info->candidate_smac = smac;
  1306. smac_info->candidate_smac_port = port;
  1307. } else {
  1308. return -EINVAL;
  1309. }
  1310. } else {
  1311. smac_index = smac_info->smac_index;
  1312. }
  1313. memcpy(path->dmac, ah->roce.dmac, 6);
  1314. path->ackto = MLX4_IB_LINK_TYPE_ETH;
  1315. /* put MAC table smac index for IBoE */
  1316. path->grh_mylmc = (u8) (smac_index) | 0x80;
  1317. } else {
  1318. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  1319. ((port - 1) << 6) | ((rdma_ah_get_sl(ah) & 0xf) << 2);
  1320. }
  1321. return 0;
  1322. }
  1323. static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_qp_attr *qp,
  1324. enum ib_qp_attr_mask qp_attr_mask,
  1325. struct mlx4_ib_qp *mqp,
  1326. struct mlx4_qp_path *path, u8 port,
  1327. u16 vlan_id, u8 *smac)
  1328. {
  1329. return _mlx4_set_path(dev, &qp->ah_attr,
  1330. mlx4_mac_to_u64(smac),
  1331. vlan_id,
  1332. path, &mqp->pri, port);
  1333. }
  1334. static int mlx4_set_alt_path(struct mlx4_ib_dev *dev,
  1335. const struct ib_qp_attr *qp,
  1336. enum ib_qp_attr_mask qp_attr_mask,
  1337. struct mlx4_ib_qp *mqp,
  1338. struct mlx4_qp_path *path, u8 port)
  1339. {
  1340. return _mlx4_set_path(dev, &qp->alt_ah_attr,
  1341. 0,
  1342. 0xffff,
  1343. path, &mqp->alt, port);
  1344. }
  1345. static void update_mcg_macs(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  1346. {
  1347. struct mlx4_ib_gid_entry *ge, *tmp;
  1348. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  1349. if (!ge->added && mlx4_ib_add_mc(dev, qp, &ge->gid)) {
  1350. ge->added = 1;
  1351. ge->port = qp->port;
  1352. }
  1353. }
  1354. }
  1355. static int handle_eth_ud_smac_index(struct mlx4_ib_dev *dev,
  1356. struct mlx4_ib_qp *qp,
  1357. struct mlx4_qp_context *context)
  1358. {
  1359. u64 u64_mac;
  1360. int smac_index;
  1361. u64_mac = atomic64_read(&dev->iboe.mac[qp->port - 1]);
  1362. context->pri_path.sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE | ((qp->port - 1) << 6);
  1363. if (!qp->pri.smac && !qp->pri.smac_port) {
  1364. smac_index = mlx4_register_mac(dev->dev, qp->port, u64_mac);
  1365. if (smac_index >= 0) {
  1366. qp->pri.candidate_smac_index = smac_index;
  1367. qp->pri.candidate_smac = u64_mac;
  1368. qp->pri.candidate_smac_port = qp->port;
  1369. context->pri_path.grh_mylmc = 0x80 | (u8) smac_index;
  1370. } else {
  1371. return -ENOENT;
  1372. }
  1373. }
  1374. return 0;
  1375. }
  1376. static int create_qp_lb_counter(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  1377. {
  1378. struct counter_index *new_counter_index;
  1379. int err;
  1380. u32 tmp_idx;
  1381. if (rdma_port_get_link_layer(&dev->ib_dev, qp->port) !=
  1382. IB_LINK_LAYER_ETHERNET ||
  1383. !(qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) ||
  1384. !(dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_LB_SRC_CHK))
  1385. return 0;
  1386. err = mlx4_counter_alloc(dev->dev, &tmp_idx);
  1387. if (err)
  1388. return err;
  1389. new_counter_index = kmalloc(sizeof(*new_counter_index), GFP_KERNEL);
  1390. if (!new_counter_index) {
  1391. mlx4_counter_free(dev->dev, tmp_idx);
  1392. return -ENOMEM;
  1393. }
  1394. new_counter_index->index = tmp_idx;
  1395. new_counter_index->allocated = 1;
  1396. qp->counter_index = new_counter_index;
  1397. mutex_lock(&dev->counters_table[qp->port - 1].mutex);
  1398. list_add_tail(&new_counter_index->list,
  1399. &dev->counters_table[qp->port - 1].counters_list);
  1400. mutex_unlock(&dev->counters_table[qp->port - 1].mutex);
  1401. return 0;
  1402. }
  1403. enum {
  1404. MLX4_QPC_ROCE_MODE_1 = 0,
  1405. MLX4_QPC_ROCE_MODE_2 = 2,
  1406. MLX4_QPC_ROCE_MODE_UNDEFINED = 0xff
  1407. };
  1408. static u8 gid_type_to_qpc(enum ib_gid_type gid_type)
  1409. {
  1410. switch (gid_type) {
  1411. case IB_GID_TYPE_ROCE:
  1412. return MLX4_QPC_ROCE_MODE_1;
  1413. case IB_GID_TYPE_ROCE_UDP_ENCAP:
  1414. return MLX4_QPC_ROCE_MODE_2;
  1415. default:
  1416. return MLX4_QPC_ROCE_MODE_UNDEFINED;
  1417. }
  1418. }
  1419. static int __mlx4_ib_modify_qp(struct ib_qp *ibqp,
  1420. const struct ib_qp_attr *attr, int attr_mask,
  1421. enum ib_qp_state cur_state, enum ib_qp_state new_state)
  1422. {
  1423. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1424. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1425. struct mlx4_ib_pd *pd;
  1426. struct mlx4_ib_cq *send_cq, *recv_cq;
  1427. struct mlx4_qp_context *context;
  1428. enum mlx4_qp_optpar optpar = 0;
  1429. int sqd_event;
  1430. int steer_qp = 0;
  1431. int err = -EINVAL;
  1432. int counter_index;
  1433. /* APM is not supported under RoCE */
  1434. if (attr_mask & IB_QP_ALT_PATH &&
  1435. rdma_port_get_link_layer(&dev->ib_dev, qp->port) ==
  1436. IB_LINK_LAYER_ETHERNET)
  1437. return -ENOTSUPP;
  1438. context = kzalloc(sizeof *context, GFP_KERNEL);
  1439. if (!context)
  1440. return -ENOMEM;
  1441. context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
  1442. (to_mlx4_st(dev, qp->mlx4_ib_qp_type) << 16));
  1443. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  1444. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  1445. else {
  1446. optpar |= MLX4_QP_OPTPAR_PM_STATE;
  1447. switch (attr->path_mig_state) {
  1448. case IB_MIG_MIGRATED:
  1449. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  1450. break;
  1451. case IB_MIG_REARM:
  1452. context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
  1453. break;
  1454. case IB_MIG_ARMED:
  1455. context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
  1456. break;
  1457. }
  1458. }
  1459. if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI)
  1460. context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
  1461. else if (ibqp->qp_type == IB_QPT_RAW_PACKET)
  1462. context->mtu_msgmax = (MLX4_RAW_QP_MTU << 5) | MLX4_RAW_QP_MSGMAX;
  1463. else if (ibqp->qp_type == IB_QPT_UD) {
  1464. if (qp->flags & MLX4_IB_QP_LSO)
  1465. context->mtu_msgmax = (IB_MTU_4096 << 5) |
  1466. ilog2(dev->dev->caps.max_gso_sz);
  1467. else
  1468. context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
  1469. } else if (attr_mask & IB_QP_PATH_MTU) {
  1470. if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
  1471. pr_err("path MTU (%u) is invalid\n",
  1472. attr->path_mtu);
  1473. goto out;
  1474. }
  1475. context->mtu_msgmax = (attr->path_mtu << 5) |
  1476. ilog2(dev->dev->caps.max_msg_sz);
  1477. }
  1478. if (qp->rq.wqe_cnt)
  1479. context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3;
  1480. context->rq_size_stride |= qp->rq.wqe_shift - 4;
  1481. if (qp->sq.wqe_cnt)
  1482. context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3;
  1483. context->sq_size_stride |= qp->sq.wqe_shift - 4;
  1484. if (new_state == IB_QPS_RESET && qp->counter_index)
  1485. mlx4_ib_free_qp_counter(dev, qp);
  1486. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  1487. context->sq_size_stride |= !!qp->sq_no_prefetch << 7;
  1488. context->xrcd = cpu_to_be32((u32) qp->xrcdn);
  1489. if (ibqp->qp_type == IB_QPT_RAW_PACKET)
  1490. context->param3 |= cpu_to_be32(1 << 30);
  1491. }
  1492. if (qp->ibqp.uobject)
  1493. context->usr_page = cpu_to_be32(
  1494. mlx4_to_hw_uar_index(dev->dev,
  1495. to_mucontext(ibqp->uobject->context)->uar.index));
  1496. else
  1497. context->usr_page = cpu_to_be32(
  1498. mlx4_to_hw_uar_index(dev->dev, dev->priv_uar.index));
  1499. if (attr_mask & IB_QP_DEST_QPN)
  1500. context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  1501. if (attr_mask & IB_QP_PORT) {
  1502. if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
  1503. !(attr_mask & IB_QP_AV)) {
  1504. mlx4_set_sched(&context->pri_path, attr->port_num);
  1505. optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
  1506. }
  1507. }
  1508. if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
  1509. err = create_qp_lb_counter(dev, qp);
  1510. if (err)
  1511. goto out;
  1512. counter_index =
  1513. dev->counters_table[qp->port - 1].default_counter;
  1514. if (qp->counter_index)
  1515. counter_index = qp->counter_index->index;
  1516. if (counter_index != -1) {
  1517. context->pri_path.counter_index = counter_index;
  1518. optpar |= MLX4_QP_OPTPAR_COUNTER_INDEX;
  1519. if (qp->counter_index) {
  1520. context->pri_path.fl |=
  1521. MLX4_FL_ETH_SRC_CHECK_MC_LB;
  1522. context->pri_path.vlan_control |=
  1523. MLX4_CTRL_ETH_SRC_CHECK_IF_COUNTER;
  1524. }
  1525. } else
  1526. context->pri_path.counter_index =
  1527. MLX4_SINK_COUNTER_INDEX(dev->dev);
  1528. if (qp->flags & MLX4_IB_QP_NETIF) {
  1529. mlx4_ib_steer_qp_reg(dev, qp, 1);
  1530. steer_qp = 1;
  1531. }
  1532. if (ibqp->qp_type == IB_QPT_GSI) {
  1533. enum ib_gid_type gid_type = qp->flags & MLX4_IB_ROCE_V2_GSI_QP ?
  1534. IB_GID_TYPE_ROCE_UDP_ENCAP : IB_GID_TYPE_ROCE;
  1535. u8 qpc_roce_mode = gid_type_to_qpc(gid_type);
  1536. context->rlkey_roce_mode |= (qpc_roce_mode << 6);
  1537. }
  1538. }
  1539. if (attr_mask & IB_QP_PKEY_INDEX) {
  1540. if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV)
  1541. context->pri_path.disable_pkey_check = 0x40;
  1542. context->pri_path.pkey_index = attr->pkey_index;
  1543. optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
  1544. }
  1545. if (attr_mask & IB_QP_AV) {
  1546. u8 port_num = mlx4_is_bonded(to_mdev(ibqp->device)->dev) ? 1 :
  1547. attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1548. union ib_gid gid;
  1549. struct ib_gid_attr gid_attr = {.gid_type = IB_GID_TYPE_IB};
  1550. u16 vlan = 0xffff;
  1551. u8 smac[ETH_ALEN];
  1552. int status = 0;
  1553. int is_eth =
  1554. rdma_cap_eth_ah(&dev->ib_dev, port_num) &&
  1555. rdma_ah_get_ah_flags(&attr->ah_attr) & IB_AH_GRH;
  1556. if (is_eth) {
  1557. int index =
  1558. rdma_ah_read_grh(&attr->ah_attr)->sgid_index;
  1559. status = ib_get_cached_gid(ibqp->device, port_num,
  1560. index, &gid, &gid_attr);
  1561. if (!status && !memcmp(&gid, &zgid, sizeof(gid)))
  1562. status = -ENOENT;
  1563. if (!status && gid_attr.ndev) {
  1564. vlan = rdma_vlan_dev_vlan_id(gid_attr.ndev);
  1565. memcpy(smac, gid_attr.ndev->dev_addr, ETH_ALEN);
  1566. dev_put(gid_attr.ndev);
  1567. }
  1568. }
  1569. if (status)
  1570. goto out;
  1571. if (mlx4_set_path(dev, attr, attr_mask, qp, &context->pri_path,
  1572. port_num, vlan, smac))
  1573. goto out;
  1574. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  1575. MLX4_QP_OPTPAR_SCHED_QUEUE);
  1576. if (is_eth &&
  1577. (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR)) {
  1578. u8 qpc_roce_mode = gid_type_to_qpc(gid_attr.gid_type);
  1579. if (qpc_roce_mode == MLX4_QPC_ROCE_MODE_UNDEFINED) {
  1580. err = -EINVAL;
  1581. goto out;
  1582. }
  1583. context->rlkey_roce_mode |= (qpc_roce_mode << 6);
  1584. }
  1585. }
  1586. if (attr_mask & IB_QP_TIMEOUT) {
  1587. context->pri_path.ackto |= attr->timeout << 3;
  1588. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  1589. }
  1590. if (attr_mask & IB_QP_ALT_PATH) {
  1591. if (attr->alt_port_num == 0 ||
  1592. attr->alt_port_num > dev->dev->caps.num_ports)
  1593. goto out;
  1594. if (attr->alt_pkey_index >=
  1595. dev->dev->caps.pkey_table_len[attr->alt_port_num])
  1596. goto out;
  1597. if (mlx4_set_alt_path(dev, attr, attr_mask, qp,
  1598. &context->alt_path,
  1599. attr->alt_port_num))
  1600. goto out;
  1601. context->alt_path.pkey_index = attr->alt_pkey_index;
  1602. context->alt_path.ackto = attr->alt_timeout << 3;
  1603. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  1604. }
  1605. pd = get_pd(qp);
  1606. get_cqs(qp, &send_cq, &recv_cq);
  1607. context->pd = cpu_to_be32(pd->pdn);
  1608. context->cqn_send = cpu_to_be32(send_cq->mcq.cqn);
  1609. context->cqn_recv = cpu_to_be32(recv_cq->mcq.cqn);
  1610. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  1611. /* Set "fast registration enabled" for all kernel QPs */
  1612. if (!qp->ibqp.uobject)
  1613. context->params1 |= cpu_to_be32(1 << 11);
  1614. if (attr_mask & IB_QP_RNR_RETRY) {
  1615. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  1616. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  1617. }
  1618. if (attr_mask & IB_QP_RETRY_CNT) {
  1619. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  1620. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  1621. }
  1622. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  1623. if (attr->max_rd_atomic)
  1624. context->params1 |=
  1625. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  1626. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  1627. }
  1628. if (attr_mask & IB_QP_SQ_PSN)
  1629. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  1630. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  1631. if (attr->max_dest_rd_atomic)
  1632. context->params2 |=
  1633. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  1634. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  1635. }
  1636. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  1637. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  1638. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  1639. }
  1640. if (ibqp->srq)
  1641. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  1642. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  1643. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  1644. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  1645. }
  1646. if (attr_mask & IB_QP_RQ_PSN)
  1647. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  1648. /* proxy and tunnel qp qkeys will be changed in modify-qp wrappers */
  1649. if (attr_mask & IB_QP_QKEY) {
  1650. if (qp->mlx4_ib_qp_type &
  1651. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))
  1652. context->qkey = cpu_to_be32(IB_QP_SET_QKEY);
  1653. else {
  1654. if (mlx4_is_mfunc(dev->dev) &&
  1655. !(qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV) &&
  1656. (attr->qkey & MLX4_RESERVED_QKEY_MASK) ==
  1657. MLX4_RESERVED_QKEY_BASE) {
  1658. pr_err("Cannot use reserved QKEY"
  1659. " 0x%x (range 0xffff0000..0xffffffff"
  1660. " is reserved)\n", attr->qkey);
  1661. err = -EINVAL;
  1662. goto out;
  1663. }
  1664. context->qkey = cpu_to_be32(attr->qkey);
  1665. }
  1666. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  1667. }
  1668. if (ibqp->srq)
  1669. context->srqn = cpu_to_be32(1 << 24 | to_msrq(ibqp->srq)->msrq.srqn);
  1670. if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1671. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  1672. if (cur_state == IB_QPS_INIT &&
  1673. new_state == IB_QPS_RTR &&
  1674. (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  1675. ibqp->qp_type == IB_QPT_UD ||
  1676. ibqp->qp_type == IB_QPT_RAW_PACKET)) {
  1677. context->pri_path.sched_queue = (qp->port - 1) << 6;
  1678. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
  1679. qp->mlx4_ib_qp_type &
  1680. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) {
  1681. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  1682. if (qp->mlx4_ib_qp_type != MLX4_IB_QPT_SMI)
  1683. context->pri_path.fl = 0x80;
  1684. } else {
  1685. if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV)
  1686. context->pri_path.fl = 0x80;
  1687. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  1688. }
  1689. if (rdma_port_get_link_layer(&dev->ib_dev, qp->port) ==
  1690. IB_LINK_LAYER_ETHERNET) {
  1691. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_GSI ||
  1692. qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI)
  1693. context->pri_path.feup = 1 << 7; /* don't fsm */
  1694. /* handle smac_index */
  1695. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_UD ||
  1696. qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI ||
  1697. qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_GSI) {
  1698. err = handle_eth_ud_smac_index(dev, qp, context);
  1699. if (err) {
  1700. err = -EINVAL;
  1701. goto out;
  1702. }
  1703. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI)
  1704. dev->qp1_proxy[qp->port - 1] = qp;
  1705. }
  1706. }
  1707. }
  1708. if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
  1709. context->pri_path.ackto = (context->pri_path.ackto & 0xf8) |
  1710. MLX4_IB_LINK_TYPE_ETH;
  1711. if (dev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
  1712. /* set QP to receive both tunneled & non-tunneled packets */
  1713. if (!(context->flags & cpu_to_be32(1 << MLX4_RSS_QPC_FLAG_OFFSET)))
  1714. context->srqn = cpu_to_be32(7 << 28);
  1715. }
  1716. }
  1717. if (ibqp->qp_type == IB_QPT_UD && (new_state == IB_QPS_RTR)) {
  1718. int is_eth = rdma_port_get_link_layer(
  1719. &dev->ib_dev, qp->port) ==
  1720. IB_LINK_LAYER_ETHERNET;
  1721. if (is_eth) {
  1722. context->pri_path.ackto = MLX4_IB_LINK_TYPE_ETH;
  1723. optpar |= MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH;
  1724. }
  1725. }
  1726. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  1727. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  1728. sqd_event = 1;
  1729. else
  1730. sqd_event = 0;
  1731. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1732. context->rlkey_roce_mode |= (1 << 4);
  1733. /*
  1734. * Before passing a kernel QP to the HW, make sure that the
  1735. * ownership bits of the send queue are set and the SQ
  1736. * headroom is stamped so that the hardware doesn't start
  1737. * processing stale work requests.
  1738. */
  1739. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  1740. struct mlx4_wqe_ctrl_seg *ctrl;
  1741. int i;
  1742. for (i = 0; i < qp->sq.wqe_cnt; ++i) {
  1743. ctrl = get_send_wqe(qp, i);
  1744. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  1745. if (qp->sq_max_wqes_per_wr == 1)
  1746. ctrl->qpn_vlan.fence_size =
  1747. 1 << (qp->sq.wqe_shift - 4);
  1748. stamp_send_wqe(qp, i, 1 << qp->sq.wqe_shift);
  1749. }
  1750. }
  1751. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  1752. to_mlx4_state(new_state), context, optpar,
  1753. sqd_event, &qp->mqp);
  1754. if (err)
  1755. goto out;
  1756. qp->state = new_state;
  1757. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1758. qp->atomic_rd_en = attr->qp_access_flags;
  1759. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1760. qp->resp_depth = attr->max_dest_rd_atomic;
  1761. if (attr_mask & IB_QP_PORT) {
  1762. qp->port = attr->port_num;
  1763. update_mcg_macs(dev, qp);
  1764. }
  1765. if (attr_mask & IB_QP_ALT_PATH)
  1766. qp->alt_port = attr->alt_port_num;
  1767. if (is_sqp(dev, qp))
  1768. store_sqp_attrs(to_msqp(qp), attr, attr_mask);
  1769. /*
  1770. * If we moved QP0 to RTR, bring the IB link up; if we moved
  1771. * QP0 to RESET or ERROR, bring the link back down.
  1772. */
  1773. if (is_qp0(dev, qp)) {
  1774. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  1775. if (mlx4_INIT_PORT(dev->dev, qp->port))
  1776. pr_warn("INIT_PORT failed for port %d\n",
  1777. qp->port);
  1778. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  1779. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  1780. mlx4_CLOSE_PORT(dev->dev, qp->port);
  1781. }
  1782. /*
  1783. * If we moved a kernel QP to RESET, clean up all old CQ
  1784. * entries and reinitialize the QP.
  1785. */
  1786. if (new_state == IB_QPS_RESET) {
  1787. if (!ibqp->uobject) {
  1788. mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  1789. ibqp->srq ? to_msrq(ibqp->srq) : NULL);
  1790. if (send_cq != recv_cq)
  1791. mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  1792. qp->rq.head = 0;
  1793. qp->rq.tail = 0;
  1794. qp->sq.head = 0;
  1795. qp->sq.tail = 0;
  1796. qp->sq_next_wqe = 0;
  1797. if (qp->rq.wqe_cnt)
  1798. *qp->db.db = 0;
  1799. if (qp->flags & MLX4_IB_QP_NETIF)
  1800. mlx4_ib_steer_qp_reg(dev, qp, 0);
  1801. }
  1802. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) {
  1803. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  1804. qp->pri.smac = 0;
  1805. qp->pri.smac_port = 0;
  1806. }
  1807. if (qp->alt.smac) {
  1808. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  1809. qp->alt.smac = 0;
  1810. }
  1811. if (qp->pri.vid < 0x1000) {
  1812. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, qp->pri.vid);
  1813. qp->pri.vid = 0xFFFF;
  1814. qp->pri.candidate_vid = 0xFFFF;
  1815. qp->pri.update_vid = 0;
  1816. }
  1817. if (qp->alt.vid < 0x1000) {
  1818. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, qp->alt.vid);
  1819. qp->alt.vid = 0xFFFF;
  1820. qp->alt.candidate_vid = 0xFFFF;
  1821. qp->alt.update_vid = 0;
  1822. }
  1823. }
  1824. out:
  1825. if (err && qp->counter_index)
  1826. mlx4_ib_free_qp_counter(dev, qp);
  1827. if (err && steer_qp)
  1828. mlx4_ib_steer_qp_reg(dev, qp, 0);
  1829. kfree(context);
  1830. if (qp->pri.candidate_smac ||
  1831. (!qp->pri.candidate_smac && qp->pri.candidate_smac_port)) {
  1832. if (err) {
  1833. mlx4_unregister_mac(dev->dev, qp->pri.candidate_smac_port, qp->pri.candidate_smac);
  1834. } else {
  1835. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port))
  1836. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  1837. qp->pri.smac = qp->pri.candidate_smac;
  1838. qp->pri.smac_index = qp->pri.candidate_smac_index;
  1839. qp->pri.smac_port = qp->pri.candidate_smac_port;
  1840. }
  1841. qp->pri.candidate_smac = 0;
  1842. qp->pri.candidate_smac_index = 0;
  1843. qp->pri.candidate_smac_port = 0;
  1844. }
  1845. if (qp->alt.candidate_smac) {
  1846. if (err) {
  1847. mlx4_unregister_mac(dev->dev, qp->alt.candidate_smac_port, qp->alt.candidate_smac);
  1848. } else {
  1849. if (qp->alt.smac)
  1850. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  1851. qp->alt.smac = qp->alt.candidate_smac;
  1852. qp->alt.smac_index = qp->alt.candidate_smac_index;
  1853. qp->alt.smac_port = qp->alt.candidate_smac_port;
  1854. }
  1855. qp->alt.candidate_smac = 0;
  1856. qp->alt.candidate_smac_index = 0;
  1857. qp->alt.candidate_smac_port = 0;
  1858. }
  1859. if (qp->pri.update_vid) {
  1860. if (err) {
  1861. if (qp->pri.candidate_vid < 0x1000)
  1862. mlx4_unregister_vlan(dev->dev, qp->pri.candidate_vlan_port,
  1863. qp->pri.candidate_vid);
  1864. } else {
  1865. if (qp->pri.vid < 0x1000)
  1866. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port,
  1867. qp->pri.vid);
  1868. qp->pri.vid = qp->pri.candidate_vid;
  1869. qp->pri.vlan_port = qp->pri.candidate_vlan_port;
  1870. qp->pri.vlan_index = qp->pri.candidate_vlan_index;
  1871. }
  1872. qp->pri.candidate_vid = 0xFFFF;
  1873. qp->pri.update_vid = 0;
  1874. }
  1875. if (qp->alt.update_vid) {
  1876. if (err) {
  1877. if (qp->alt.candidate_vid < 0x1000)
  1878. mlx4_unregister_vlan(dev->dev, qp->alt.candidate_vlan_port,
  1879. qp->alt.candidate_vid);
  1880. } else {
  1881. if (qp->alt.vid < 0x1000)
  1882. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port,
  1883. qp->alt.vid);
  1884. qp->alt.vid = qp->alt.candidate_vid;
  1885. qp->alt.vlan_port = qp->alt.candidate_vlan_port;
  1886. qp->alt.vlan_index = qp->alt.candidate_vlan_index;
  1887. }
  1888. qp->alt.candidate_vid = 0xFFFF;
  1889. qp->alt.update_vid = 0;
  1890. }
  1891. return err;
  1892. }
  1893. static int _mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1894. int attr_mask, struct ib_udata *udata)
  1895. {
  1896. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1897. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1898. enum ib_qp_state cur_state, new_state;
  1899. int err = -EINVAL;
  1900. int ll;
  1901. mutex_lock(&qp->mutex);
  1902. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  1903. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  1904. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  1905. ll = IB_LINK_LAYER_UNSPECIFIED;
  1906. } else {
  1907. int port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1908. ll = rdma_port_get_link_layer(&dev->ib_dev, port);
  1909. }
  1910. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  1911. attr_mask, ll)) {
  1912. pr_debug("qpn 0x%x: invalid attribute mask specified "
  1913. "for transition %d to %d. qp_type %d,"
  1914. " attr_mask 0x%x\n",
  1915. ibqp->qp_num, cur_state, new_state,
  1916. ibqp->qp_type, attr_mask);
  1917. goto out;
  1918. }
  1919. if (mlx4_is_bonded(dev->dev) && (attr_mask & IB_QP_PORT)) {
  1920. if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
  1921. if ((ibqp->qp_type == IB_QPT_RC) ||
  1922. (ibqp->qp_type == IB_QPT_UD) ||
  1923. (ibqp->qp_type == IB_QPT_UC) ||
  1924. (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
  1925. (ibqp->qp_type == IB_QPT_XRC_INI)) {
  1926. attr->port_num = mlx4_ib_bond_next_port(dev);
  1927. }
  1928. } else {
  1929. /* no sense in changing port_num
  1930. * when ports are bonded */
  1931. attr_mask &= ~IB_QP_PORT;
  1932. }
  1933. }
  1934. if ((attr_mask & IB_QP_PORT) &&
  1935. (attr->port_num == 0 || attr->port_num > dev->num_ports)) {
  1936. pr_debug("qpn 0x%x: invalid port number (%d) specified "
  1937. "for transition %d to %d. qp_type %d\n",
  1938. ibqp->qp_num, attr->port_num, cur_state,
  1939. new_state, ibqp->qp_type);
  1940. goto out;
  1941. }
  1942. if ((attr_mask & IB_QP_PORT) && (ibqp->qp_type == IB_QPT_RAW_PACKET) &&
  1943. (rdma_port_get_link_layer(&dev->ib_dev, attr->port_num) !=
  1944. IB_LINK_LAYER_ETHERNET))
  1945. goto out;
  1946. if (attr_mask & IB_QP_PKEY_INDEX) {
  1947. int p = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1948. if (attr->pkey_index >= dev->dev->caps.pkey_table_len[p]) {
  1949. pr_debug("qpn 0x%x: invalid pkey index (%d) specified "
  1950. "for transition %d to %d. qp_type %d\n",
  1951. ibqp->qp_num, attr->pkey_index, cur_state,
  1952. new_state, ibqp->qp_type);
  1953. goto out;
  1954. }
  1955. }
  1956. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  1957. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  1958. pr_debug("qpn 0x%x: max_rd_atomic (%d) too large. "
  1959. "Transition %d to %d. qp_type %d\n",
  1960. ibqp->qp_num, attr->max_rd_atomic, cur_state,
  1961. new_state, ibqp->qp_type);
  1962. goto out;
  1963. }
  1964. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  1965. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  1966. pr_debug("qpn 0x%x: max_dest_rd_atomic (%d) too large. "
  1967. "Transition %d to %d. qp_type %d\n",
  1968. ibqp->qp_num, attr->max_dest_rd_atomic, cur_state,
  1969. new_state, ibqp->qp_type);
  1970. goto out;
  1971. }
  1972. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  1973. err = 0;
  1974. goto out;
  1975. }
  1976. err = __mlx4_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
  1977. if (mlx4_is_bonded(dev->dev) && (attr_mask & IB_QP_PORT))
  1978. attr->port_num = 1;
  1979. out:
  1980. mutex_unlock(&qp->mutex);
  1981. return err;
  1982. }
  1983. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1984. int attr_mask, struct ib_udata *udata)
  1985. {
  1986. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  1987. int ret;
  1988. ret = _mlx4_ib_modify_qp(ibqp, attr, attr_mask, udata);
  1989. if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  1990. struct mlx4_ib_sqp *sqp = to_msqp(mqp);
  1991. int err = 0;
  1992. if (sqp->roce_v2_gsi)
  1993. err = ib_modify_qp(sqp->roce_v2_gsi, attr, attr_mask);
  1994. if (err)
  1995. pr_err("Failed to modify GSI QP for RoCEv2 (%d)\n",
  1996. err);
  1997. }
  1998. return ret;
  1999. }
  2000. static int vf_get_qp0_qkey(struct mlx4_dev *dev, int qpn, u32 *qkey)
  2001. {
  2002. int i;
  2003. for (i = 0; i < dev->caps.num_ports; i++) {
  2004. if (qpn == dev->caps.qp0_proxy[i] ||
  2005. qpn == dev->caps.qp0_tunnel[i]) {
  2006. *qkey = dev->caps.qp0_qkey[i];
  2007. return 0;
  2008. }
  2009. }
  2010. return -EINVAL;
  2011. }
  2012. static int build_sriov_qp0_header(struct mlx4_ib_sqp *sqp,
  2013. struct ib_ud_wr *wr,
  2014. void *wqe, unsigned *mlx_seg_len)
  2015. {
  2016. struct mlx4_ib_dev *mdev = to_mdev(sqp->qp.ibqp.device);
  2017. struct ib_device *ib_dev = &mdev->ib_dev;
  2018. struct mlx4_wqe_mlx_seg *mlx = wqe;
  2019. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  2020. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2021. u16 pkey;
  2022. u32 qkey;
  2023. int send_size;
  2024. int header_size;
  2025. int spc;
  2026. int i;
  2027. if (wr->wr.opcode != IB_WR_SEND)
  2028. return -EINVAL;
  2029. send_size = 0;
  2030. for (i = 0; i < wr->wr.num_sge; ++i)
  2031. send_size += wr->wr.sg_list[i].length;
  2032. /* for proxy-qp0 sends, need to add in size of tunnel header */
  2033. /* for tunnel-qp0 sends, tunnel header is already in s/g list */
  2034. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER)
  2035. send_size += sizeof (struct mlx4_ib_tunnel_header);
  2036. ib_ud_header_init(send_size, 1, 0, 0, 0, 0, 0, 0, &sqp->ud_header);
  2037. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER) {
  2038. sqp->ud_header.lrh.service_level =
  2039. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  2040. sqp->ud_header.lrh.destination_lid =
  2041. cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2042. sqp->ud_header.lrh.source_lid =
  2043. cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2044. }
  2045. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  2046. /* force loopback */
  2047. mlx->flags |= cpu_to_be32(MLX4_WQE_MLX_VL15 | 0x1 | MLX4_WQE_MLX_SLR);
  2048. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  2049. sqp->ud_header.lrh.virtual_lane = 0;
  2050. sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED);
  2051. ib_get_cached_pkey(ib_dev, sqp->qp.port, 0, &pkey);
  2052. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  2053. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_TUN_SMI_OWNER)
  2054. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn);
  2055. else
  2056. sqp->ud_header.bth.destination_qpn =
  2057. cpu_to_be32(mdev->dev->caps.qp0_tunnel[sqp->qp.port - 1]);
  2058. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  2059. if (mlx4_is_master(mdev->dev)) {
  2060. if (mlx4_get_parav_qkey(mdev->dev, sqp->qp.mqp.qpn, &qkey))
  2061. return -EINVAL;
  2062. } else {
  2063. if (vf_get_qp0_qkey(mdev->dev, sqp->qp.mqp.qpn, &qkey))
  2064. return -EINVAL;
  2065. }
  2066. sqp->ud_header.deth.qkey = cpu_to_be32(qkey);
  2067. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.mqp.qpn);
  2068. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  2069. sqp->ud_header.immediate_present = 0;
  2070. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  2071. /*
  2072. * Inline data segments may not cross a 64 byte boundary. If
  2073. * our UD header is bigger than the space available up to the
  2074. * next 64 byte boundary in the WQE, use two inline data
  2075. * segments to hold the UD header.
  2076. */
  2077. spc = MLX4_INLINE_ALIGN -
  2078. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2079. if (header_size <= spc) {
  2080. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  2081. memcpy(inl + 1, sqp->header_buf, header_size);
  2082. i = 1;
  2083. } else {
  2084. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2085. memcpy(inl + 1, sqp->header_buf, spc);
  2086. inl = (void *) (inl + 1) + spc;
  2087. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  2088. /*
  2089. * Need a barrier here to make sure all the data is
  2090. * visible before the byte_count field is set.
  2091. * Otherwise the HCA prefetcher could grab the 64-byte
  2092. * chunk with this inline segment and get a valid (!=
  2093. * 0xffffffff) byte count but stale data, and end up
  2094. * generating a packet with bad headers.
  2095. *
  2096. * The first inline segment's byte_count field doesn't
  2097. * need a barrier, because it comes after a
  2098. * control/MLX segment and therefore is at an offset
  2099. * of 16 mod 64.
  2100. */
  2101. wmb();
  2102. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  2103. i = 2;
  2104. }
  2105. *mlx_seg_len =
  2106. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  2107. return 0;
  2108. }
  2109. static u8 sl_to_vl(struct mlx4_ib_dev *dev, u8 sl, int port_num)
  2110. {
  2111. union sl2vl_tbl_to_u64 tmp_vltab;
  2112. u8 vl;
  2113. if (sl > 15)
  2114. return 0xf;
  2115. tmp_vltab.sl64 = atomic64_read(&dev->sl2vl[port_num - 1]);
  2116. vl = tmp_vltab.sl8[sl >> 1];
  2117. if (sl & 1)
  2118. vl &= 0x0f;
  2119. else
  2120. vl >>= 4;
  2121. return vl;
  2122. }
  2123. static int fill_gid_by_hw_index(struct mlx4_ib_dev *ibdev, u8 port_num,
  2124. int index, union ib_gid *gid,
  2125. enum ib_gid_type *gid_type)
  2126. {
  2127. struct mlx4_ib_iboe *iboe = &ibdev->iboe;
  2128. struct mlx4_port_gid_table *port_gid_table;
  2129. unsigned long flags;
  2130. port_gid_table = &iboe->gids[port_num - 1];
  2131. spin_lock_irqsave(&iboe->lock, flags);
  2132. memcpy(gid, &port_gid_table->gids[index].gid, sizeof(*gid));
  2133. *gid_type = port_gid_table->gids[index].gid_type;
  2134. spin_unlock_irqrestore(&iboe->lock, flags);
  2135. if (!memcmp(gid, &zgid, sizeof(*gid)))
  2136. return -ENOENT;
  2137. return 0;
  2138. }
  2139. #define MLX4_ROCEV2_QP1_SPORT 0xC000
  2140. static int build_mlx_header(struct mlx4_ib_sqp *sqp, struct ib_ud_wr *wr,
  2141. void *wqe, unsigned *mlx_seg_len)
  2142. {
  2143. struct ib_device *ib_dev = sqp->qp.ibqp.device;
  2144. struct mlx4_ib_dev *ibdev = to_mdev(ib_dev);
  2145. struct mlx4_wqe_mlx_seg *mlx = wqe;
  2146. struct mlx4_wqe_ctrl_seg *ctrl = wqe;
  2147. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  2148. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2149. union ib_gid sgid;
  2150. u16 pkey;
  2151. int send_size;
  2152. int header_size;
  2153. int spc;
  2154. int i;
  2155. int err = 0;
  2156. u16 vlan = 0xffff;
  2157. bool is_eth;
  2158. bool is_vlan = false;
  2159. bool is_grh;
  2160. bool is_udp = false;
  2161. int ip_version = 0;
  2162. send_size = 0;
  2163. for (i = 0; i < wr->wr.num_sge; ++i)
  2164. send_size += wr->wr.sg_list[i].length;
  2165. is_eth = rdma_port_get_link_layer(sqp->qp.ibqp.device, sqp->qp.port) == IB_LINK_LAYER_ETHERNET;
  2166. is_grh = mlx4_ib_ah_grh_present(ah);
  2167. if (is_eth) {
  2168. enum ib_gid_type gid_type;
  2169. if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
  2170. /* When multi-function is enabled, the ib_core gid
  2171. * indexes don't necessarily match the hw ones, so
  2172. * we must use our own cache */
  2173. err = mlx4_get_roce_gid_from_slave(to_mdev(ib_dev)->dev,
  2174. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  2175. ah->av.ib.gid_index, &sgid.raw[0]);
  2176. if (err)
  2177. return err;
  2178. } else {
  2179. err = fill_gid_by_hw_index(ibdev, sqp->qp.port,
  2180. ah->av.ib.gid_index,
  2181. &sgid, &gid_type);
  2182. if (!err) {
  2183. is_udp = gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP;
  2184. if (is_udp) {
  2185. if (ipv6_addr_v4mapped((struct in6_addr *)&sgid))
  2186. ip_version = 4;
  2187. else
  2188. ip_version = 6;
  2189. is_grh = false;
  2190. }
  2191. } else {
  2192. return err;
  2193. }
  2194. }
  2195. if (ah->av.eth.vlan != cpu_to_be16(0xffff)) {
  2196. vlan = be16_to_cpu(ah->av.eth.vlan) & 0x0fff;
  2197. is_vlan = 1;
  2198. }
  2199. }
  2200. err = ib_ud_header_init(send_size, !is_eth, is_eth, is_vlan, is_grh,
  2201. ip_version, is_udp, 0, &sqp->ud_header);
  2202. if (err)
  2203. return err;
  2204. if (!is_eth) {
  2205. sqp->ud_header.lrh.service_level =
  2206. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  2207. sqp->ud_header.lrh.destination_lid = ah->av.ib.dlid;
  2208. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2209. }
  2210. if (is_grh || (ip_version == 6)) {
  2211. sqp->ud_header.grh.traffic_class =
  2212. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  2213. sqp->ud_header.grh.flow_label =
  2214. ah->av.ib.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  2215. sqp->ud_header.grh.hop_limit = ah->av.ib.hop_limit;
  2216. if (is_eth) {
  2217. memcpy(sqp->ud_header.grh.source_gid.raw, sgid.raw, 16);
  2218. } else {
  2219. if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
  2220. /* When multi-function is enabled, the ib_core gid
  2221. * indexes don't necessarily match the hw ones, so
  2222. * we must use our own cache
  2223. */
  2224. sqp->ud_header.grh.source_gid.global.subnet_prefix =
  2225. cpu_to_be64(atomic64_read(&(to_mdev(ib_dev)->sriov.
  2226. demux[sqp->qp.port - 1].
  2227. subnet_prefix)));
  2228. sqp->ud_header.grh.source_gid.global.interface_id =
  2229. to_mdev(ib_dev)->sriov.demux[sqp->qp.port - 1].
  2230. guid_cache[ah->av.ib.gid_index];
  2231. } else {
  2232. ib_get_cached_gid(ib_dev,
  2233. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  2234. ah->av.ib.gid_index,
  2235. &sqp->ud_header.grh.source_gid, NULL);
  2236. }
  2237. }
  2238. memcpy(sqp->ud_header.grh.destination_gid.raw,
  2239. ah->av.ib.dgid, 16);
  2240. }
  2241. if (ip_version == 4) {
  2242. sqp->ud_header.ip4.tos =
  2243. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  2244. sqp->ud_header.ip4.id = 0;
  2245. sqp->ud_header.ip4.frag_off = htons(IP_DF);
  2246. sqp->ud_header.ip4.ttl = ah->av.eth.hop_limit;
  2247. memcpy(&sqp->ud_header.ip4.saddr,
  2248. sgid.raw + 12, 4);
  2249. memcpy(&sqp->ud_header.ip4.daddr, ah->av.ib.dgid + 12, 4);
  2250. sqp->ud_header.ip4.check = ib_ud_ip4_csum(&sqp->ud_header);
  2251. }
  2252. if (is_udp) {
  2253. sqp->ud_header.udp.dport = htons(ROCE_V2_UDP_DPORT);
  2254. sqp->ud_header.udp.sport = htons(MLX4_ROCEV2_QP1_SPORT);
  2255. sqp->ud_header.udp.csum = 0;
  2256. }
  2257. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  2258. if (!is_eth) {
  2259. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  2260. (sqp->ud_header.lrh.destination_lid ==
  2261. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  2262. (sqp->ud_header.lrh.service_level << 8));
  2263. if (ah->av.ib.port_pd & cpu_to_be32(0x80000000))
  2264. mlx->flags |= cpu_to_be32(0x1); /* force loopback */
  2265. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  2266. }
  2267. switch (wr->wr.opcode) {
  2268. case IB_WR_SEND:
  2269. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  2270. sqp->ud_header.immediate_present = 0;
  2271. break;
  2272. case IB_WR_SEND_WITH_IMM:
  2273. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  2274. sqp->ud_header.immediate_present = 1;
  2275. sqp->ud_header.immediate_data = wr->wr.ex.imm_data;
  2276. break;
  2277. default:
  2278. return -EINVAL;
  2279. }
  2280. if (is_eth) {
  2281. struct in6_addr in6;
  2282. u16 ether_type;
  2283. u16 pcp = (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 29) << 13;
  2284. ether_type = (!is_udp) ? ETH_P_IBOE:
  2285. (ip_version == 4 ? ETH_P_IP : ETH_P_IPV6);
  2286. mlx->sched_prio = cpu_to_be16(pcp);
  2287. ether_addr_copy(sqp->ud_header.eth.smac_h, ah->av.eth.s_mac);
  2288. memcpy(sqp->ud_header.eth.dmac_h, ah->av.eth.mac, 6);
  2289. memcpy(&ctrl->srcrb_flags16[0], ah->av.eth.mac, 2);
  2290. memcpy(&ctrl->imm, ah->av.eth.mac + 2, 4);
  2291. memcpy(&in6, sgid.raw, sizeof(in6));
  2292. if (!memcmp(sqp->ud_header.eth.smac_h, sqp->ud_header.eth.dmac_h, 6))
  2293. mlx->flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
  2294. if (!is_vlan) {
  2295. sqp->ud_header.eth.type = cpu_to_be16(ether_type);
  2296. } else {
  2297. sqp->ud_header.vlan.type = cpu_to_be16(ether_type);
  2298. sqp->ud_header.vlan.tag = cpu_to_be16(vlan | pcp);
  2299. }
  2300. } else {
  2301. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 :
  2302. sl_to_vl(to_mdev(ib_dev),
  2303. sqp->ud_header.lrh.service_level,
  2304. sqp->qp.port);
  2305. if (sqp->qp.ibqp.qp_num && sqp->ud_header.lrh.virtual_lane == 15)
  2306. return -EINVAL;
  2307. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  2308. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  2309. }
  2310. sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED);
  2311. if (!sqp->qp.ibqp.qp_num)
  2312. ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
  2313. else
  2314. ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->pkey_index, &pkey);
  2315. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  2316. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn);
  2317. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  2318. sqp->ud_header.deth.qkey = cpu_to_be32(wr->remote_qkey & 0x80000000 ?
  2319. sqp->qkey : wr->remote_qkey);
  2320. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  2321. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  2322. if (0) {
  2323. pr_err("built UD header of size %d:\n", header_size);
  2324. for (i = 0; i < header_size / 4; ++i) {
  2325. if (i % 8 == 0)
  2326. pr_err(" [%02x] ", i * 4);
  2327. pr_cont(" %08x",
  2328. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  2329. if ((i + 1) % 8 == 0)
  2330. pr_cont("\n");
  2331. }
  2332. pr_err("\n");
  2333. }
  2334. /*
  2335. * Inline data segments may not cross a 64 byte boundary. If
  2336. * our UD header is bigger than the space available up to the
  2337. * next 64 byte boundary in the WQE, use two inline data
  2338. * segments to hold the UD header.
  2339. */
  2340. spc = MLX4_INLINE_ALIGN -
  2341. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2342. if (header_size <= spc) {
  2343. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  2344. memcpy(inl + 1, sqp->header_buf, header_size);
  2345. i = 1;
  2346. } else {
  2347. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2348. memcpy(inl + 1, sqp->header_buf, spc);
  2349. inl = (void *) (inl + 1) + spc;
  2350. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  2351. /*
  2352. * Need a barrier here to make sure all the data is
  2353. * visible before the byte_count field is set.
  2354. * Otherwise the HCA prefetcher could grab the 64-byte
  2355. * chunk with this inline segment and get a valid (!=
  2356. * 0xffffffff) byte count but stale data, and end up
  2357. * generating a packet with bad headers.
  2358. *
  2359. * The first inline segment's byte_count field doesn't
  2360. * need a barrier, because it comes after a
  2361. * control/MLX segment and therefore is at an offset
  2362. * of 16 mod 64.
  2363. */
  2364. wmb();
  2365. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  2366. i = 2;
  2367. }
  2368. *mlx_seg_len =
  2369. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  2370. return 0;
  2371. }
  2372. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  2373. {
  2374. unsigned cur;
  2375. struct mlx4_ib_cq *cq;
  2376. cur = wq->head - wq->tail;
  2377. if (likely(cur + nreq < wq->max_post))
  2378. return 0;
  2379. cq = to_mcq(ib_cq);
  2380. spin_lock(&cq->lock);
  2381. cur = wq->head - wq->tail;
  2382. spin_unlock(&cq->lock);
  2383. return cur + nreq >= wq->max_post;
  2384. }
  2385. static __be32 convert_access(int acc)
  2386. {
  2387. return (acc & IB_ACCESS_REMOTE_ATOMIC ?
  2388. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_ATOMIC) : 0) |
  2389. (acc & IB_ACCESS_REMOTE_WRITE ?
  2390. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_WRITE) : 0) |
  2391. (acc & IB_ACCESS_REMOTE_READ ?
  2392. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_READ) : 0) |
  2393. (acc & IB_ACCESS_LOCAL_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_WRITE) : 0) |
  2394. cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_READ);
  2395. }
  2396. static void set_reg_seg(struct mlx4_wqe_fmr_seg *fseg,
  2397. struct ib_reg_wr *wr)
  2398. {
  2399. struct mlx4_ib_mr *mr = to_mmr(wr->mr);
  2400. fseg->flags = convert_access(wr->access);
  2401. fseg->mem_key = cpu_to_be32(wr->key);
  2402. fseg->buf_list = cpu_to_be64(mr->page_map);
  2403. fseg->start_addr = cpu_to_be64(mr->ibmr.iova);
  2404. fseg->reg_len = cpu_to_be64(mr->ibmr.length);
  2405. fseg->offset = 0; /* XXX -- is this just for ZBVA? */
  2406. fseg->page_size = cpu_to_be32(ilog2(mr->ibmr.page_size));
  2407. fseg->reserved[0] = 0;
  2408. fseg->reserved[1] = 0;
  2409. }
  2410. static void set_local_inv_seg(struct mlx4_wqe_local_inval_seg *iseg, u32 rkey)
  2411. {
  2412. memset(iseg, 0, sizeof(*iseg));
  2413. iseg->mem_key = cpu_to_be32(rkey);
  2414. }
  2415. static __always_inline void set_raddr_seg(struct mlx4_wqe_raddr_seg *rseg,
  2416. u64 remote_addr, u32 rkey)
  2417. {
  2418. rseg->raddr = cpu_to_be64(remote_addr);
  2419. rseg->rkey = cpu_to_be32(rkey);
  2420. rseg->reserved = 0;
  2421. }
  2422. static void set_atomic_seg(struct mlx4_wqe_atomic_seg *aseg,
  2423. struct ib_atomic_wr *wr)
  2424. {
  2425. if (wr->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  2426. aseg->swap_add = cpu_to_be64(wr->swap);
  2427. aseg->compare = cpu_to_be64(wr->compare_add);
  2428. } else if (wr->wr.opcode == IB_WR_MASKED_ATOMIC_FETCH_AND_ADD) {
  2429. aseg->swap_add = cpu_to_be64(wr->compare_add);
  2430. aseg->compare = cpu_to_be64(wr->compare_add_mask);
  2431. } else {
  2432. aseg->swap_add = cpu_to_be64(wr->compare_add);
  2433. aseg->compare = 0;
  2434. }
  2435. }
  2436. static void set_masked_atomic_seg(struct mlx4_wqe_masked_atomic_seg *aseg,
  2437. struct ib_atomic_wr *wr)
  2438. {
  2439. aseg->swap_add = cpu_to_be64(wr->swap);
  2440. aseg->swap_add_mask = cpu_to_be64(wr->swap_mask);
  2441. aseg->compare = cpu_to_be64(wr->compare_add);
  2442. aseg->compare_mask = cpu_to_be64(wr->compare_add_mask);
  2443. }
  2444. static void set_datagram_seg(struct mlx4_wqe_datagram_seg *dseg,
  2445. struct ib_ud_wr *wr)
  2446. {
  2447. memcpy(dseg->av, &to_mah(wr->ah)->av, sizeof (struct mlx4_av));
  2448. dseg->dqpn = cpu_to_be32(wr->remote_qpn);
  2449. dseg->qkey = cpu_to_be32(wr->remote_qkey);
  2450. dseg->vlan = to_mah(wr->ah)->av.eth.vlan;
  2451. memcpy(dseg->mac, to_mah(wr->ah)->av.eth.mac, 6);
  2452. }
  2453. static void set_tunnel_datagram_seg(struct mlx4_ib_dev *dev,
  2454. struct mlx4_wqe_datagram_seg *dseg,
  2455. struct ib_ud_wr *wr,
  2456. enum mlx4_ib_qp_type qpt)
  2457. {
  2458. union mlx4_ext_av *av = &to_mah(wr->ah)->av;
  2459. struct mlx4_av sqp_av = {0};
  2460. int port = *((u8 *) &av->ib.port_pd) & 0x3;
  2461. /* force loopback */
  2462. sqp_av.port_pd = av->ib.port_pd | cpu_to_be32(0x80000000);
  2463. sqp_av.g_slid = av->ib.g_slid & 0x7f; /* no GRH */
  2464. sqp_av.sl_tclass_flowlabel = av->ib.sl_tclass_flowlabel &
  2465. cpu_to_be32(0xf0000000);
  2466. memcpy(dseg->av, &sqp_av, sizeof (struct mlx4_av));
  2467. if (qpt == MLX4_IB_QPT_PROXY_GSI)
  2468. dseg->dqpn = cpu_to_be32(dev->dev->caps.qp1_tunnel[port - 1]);
  2469. else
  2470. dseg->dqpn = cpu_to_be32(dev->dev->caps.qp0_tunnel[port - 1]);
  2471. /* Use QKEY from the QP context, which is set by master */
  2472. dseg->qkey = cpu_to_be32(IB_QP_SET_QKEY);
  2473. }
  2474. static void build_tunnel_header(struct ib_ud_wr *wr, void *wqe, unsigned *mlx_seg_len)
  2475. {
  2476. struct mlx4_wqe_inline_seg *inl = wqe;
  2477. struct mlx4_ib_tunnel_header hdr;
  2478. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2479. int spc;
  2480. int i;
  2481. memcpy(&hdr.av, &ah->av, sizeof hdr.av);
  2482. hdr.remote_qpn = cpu_to_be32(wr->remote_qpn);
  2483. hdr.pkey_index = cpu_to_be16(wr->pkey_index);
  2484. hdr.qkey = cpu_to_be32(wr->remote_qkey);
  2485. memcpy(hdr.mac, ah->av.eth.mac, 6);
  2486. hdr.vlan = ah->av.eth.vlan;
  2487. spc = MLX4_INLINE_ALIGN -
  2488. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2489. if (sizeof (hdr) <= spc) {
  2490. memcpy(inl + 1, &hdr, sizeof (hdr));
  2491. wmb();
  2492. inl->byte_count = cpu_to_be32(1 << 31 | sizeof (hdr));
  2493. i = 1;
  2494. } else {
  2495. memcpy(inl + 1, &hdr, spc);
  2496. wmb();
  2497. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2498. inl = (void *) (inl + 1) + spc;
  2499. memcpy(inl + 1, (void *) &hdr + spc, sizeof (hdr) - spc);
  2500. wmb();
  2501. inl->byte_count = cpu_to_be32(1 << 31 | (sizeof (hdr) - spc));
  2502. i = 2;
  2503. }
  2504. *mlx_seg_len =
  2505. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + sizeof (hdr), 16);
  2506. }
  2507. static void set_mlx_icrc_seg(void *dseg)
  2508. {
  2509. u32 *t = dseg;
  2510. struct mlx4_wqe_inline_seg *iseg = dseg;
  2511. t[1] = 0;
  2512. /*
  2513. * Need a barrier here before writing the byte_count field to
  2514. * make sure that all the data is visible before the
  2515. * byte_count field is set. Otherwise, if the segment begins
  2516. * a new cacheline, the HCA prefetcher could grab the 64-byte
  2517. * chunk and get a valid (!= * 0xffffffff) byte count but
  2518. * stale data, and end up sending the wrong data.
  2519. */
  2520. wmb();
  2521. iseg->byte_count = cpu_to_be32((1 << 31) | 4);
  2522. }
  2523. static void set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  2524. {
  2525. dseg->lkey = cpu_to_be32(sg->lkey);
  2526. dseg->addr = cpu_to_be64(sg->addr);
  2527. /*
  2528. * Need a barrier here before writing the byte_count field to
  2529. * make sure that all the data is visible before the
  2530. * byte_count field is set. Otherwise, if the segment begins
  2531. * a new cacheline, the HCA prefetcher could grab the 64-byte
  2532. * chunk and get a valid (!= * 0xffffffff) byte count but
  2533. * stale data, and end up sending the wrong data.
  2534. */
  2535. wmb();
  2536. dseg->byte_count = cpu_to_be32(sg->length);
  2537. }
  2538. static void __set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  2539. {
  2540. dseg->byte_count = cpu_to_be32(sg->length);
  2541. dseg->lkey = cpu_to_be32(sg->lkey);
  2542. dseg->addr = cpu_to_be64(sg->addr);
  2543. }
  2544. static int build_lso_seg(struct mlx4_wqe_lso_seg *wqe, struct ib_ud_wr *wr,
  2545. struct mlx4_ib_qp *qp, unsigned *lso_seg_len,
  2546. __be32 *lso_hdr_sz, __be32 *blh)
  2547. {
  2548. unsigned halign = ALIGN(sizeof *wqe + wr->hlen, 16);
  2549. if (unlikely(halign > MLX4_IB_CACHE_LINE_SIZE))
  2550. *blh = cpu_to_be32(1 << 6);
  2551. if (unlikely(!(qp->flags & MLX4_IB_QP_LSO) &&
  2552. wr->wr.num_sge > qp->sq.max_gs - (halign >> 4)))
  2553. return -EINVAL;
  2554. memcpy(wqe->header, wr->header, wr->hlen);
  2555. *lso_hdr_sz = cpu_to_be32(wr->mss << 16 | wr->hlen);
  2556. *lso_seg_len = halign;
  2557. return 0;
  2558. }
  2559. static __be32 send_ieth(struct ib_send_wr *wr)
  2560. {
  2561. switch (wr->opcode) {
  2562. case IB_WR_SEND_WITH_IMM:
  2563. case IB_WR_RDMA_WRITE_WITH_IMM:
  2564. return wr->ex.imm_data;
  2565. case IB_WR_SEND_WITH_INV:
  2566. return cpu_to_be32(wr->ex.invalidate_rkey);
  2567. default:
  2568. return 0;
  2569. }
  2570. }
  2571. static void add_zero_len_inline(void *wqe)
  2572. {
  2573. struct mlx4_wqe_inline_seg *inl = wqe;
  2574. memset(wqe, 0, 16);
  2575. inl->byte_count = cpu_to_be32(1 << 31);
  2576. }
  2577. int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  2578. struct ib_send_wr **bad_wr)
  2579. {
  2580. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2581. void *wqe;
  2582. struct mlx4_wqe_ctrl_seg *ctrl;
  2583. struct mlx4_wqe_data_seg *dseg;
  2584. unsigned long flags;
  2585. int nreq;
  2586. int err = 0;
  2587. unsigned ind;
  2588. int uninitialized_var(stamp);
  2589. int uninitialized_var(size);
  2590. unsigned uninitialized_var(seglen);
  2591. __be32 dummy;
  2592. __be32 *lso_wqe;
  2593. __be32 uninitialized_var(lso_hdr_sz);
  2594. __be32 blh;
  2595. int i;
  2596. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  2597. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  2598. struct mlx4_ib_sqp *sqp = to_msqp(qp);
  2599. if (sqp->roce_v2_gsi) {
  2600. struct mlx4_ib_ah *ah = to_mah(ud_wr(wr)->ah);
  2601. enum ib_gid_type gid_type;
  2602. union ib_gid gid;
  2603. if (!fill_gid_by_hw_index(mdev, sqp->qp.port,
  2604. ah->av.ib.gid_index,
  2605. &gid, &gid_type))
  2606. qp = (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ?
  2607. to_mqp(sqp->roce_v2_gsi) : qp;
  2608. else
  2609. pr_err("Failed to get gid at index %d. RoCEv2 will not work properly\n",
  2610. ah->av.ib.gid_index);
  2611. }
  2612. }
  2613. spin_lock_irqsave(&qp->sq.lock, flags);
  2614. if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  2615. err = -EIO;
  2616. *bad_wr = wr;
  2617. nreq = 0;
  2618. goto out;
  2619. }
  2620. ind = qp->sq_next_wqe;
  2621. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  2622. lso_wqe = &dummy;
  2623. blh = 0;
  2624. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  2625. err = -ENOMEM;
  2626. *bad_wr = wr;
  2627. goto out;
  2628. }
  2629. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  2630. err = -EINVAL;
  2631. *bad_wr = wr;
  2632. goto out;
  2633. }
  2634. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  2635. qp->sq.wrid[(qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
  2636. ctrl->srcrb_flags =
  2637. (wr->send_flags & IB_SEND_SIGNALED ?
  2638. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  2639. (wr->send_flags & IB_SEND_SOLICITED ?
  2640. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  2641. ((wr->send_flags & IB_SEND_IP_CSUM) ?
  2642. cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  2643. MLX4_WQE_CTRL_TCP_UDP_CSUM) : 0) |
  2644. qp->sq_signal_bits;
  2645. ctrl->imm = send_ieth(wr);
  2646. wqe += sizeof *ctrl;
  2647. size = sizeof *ctrl / 16;
  2648. switch (qp->mlx4_ib_qp_type) {
  2649. case MLX4_IB_QPT_RC:
  2650. case MLX4_IB_QPT_UC:
  2651. switch (wr->opcode) {
  2652. case IB_WR_ATOMIC_CMP_AND_SWP:
  2653. case IB_WR_ATOMIC_FETCH_AND_ADD:
  2654. case IB_WR_MASKED_ATOMIC_FETCH_AND_ADD:
  2655. set_raddr_seg(wqe, atomic_wr(wr)->remote_addr,
  2656. atomic_wr(wr)->rkey);
  2657. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  2658. set_atomic_seg(wqe, atomic_wr(wr));
  2659. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  2660. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  2661. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  2662. break;
  2663. case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
  2664. set_raddr_seg(wqe, atomic_wr(wr)->remote_addr,
  2665. atomic_wr(wr)->rkey);
  2666. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  2667. set_masked_atomic_seg(wqe, atomic_wr(wr));
  2668. wqe += sizeof (struct mlx4_wqe_masked_atomic_seg);
  2669. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  2670. sizeof (struct mlx4_wqe_masked_atomic_seg)) / 16;
  2671. break;
  2672. case IB_WR_RDMA_READ:
  2673. case IB_WR_RDMA_WRITE:
  2674. case IB_WR_RDMA_WRITE_WITH_IMM:
  2675. set_raddr_seg(wqe, rdma_wr(wr)->remote_addr,
  2676. rdma_wr(wr)->rkey);
  2677. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  2678. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  2679. break;
  2680. case IB_WR_LOCAL_INV:
  2681. ctrl->srcrb_flags |=
  2682. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  2683. set_local_inv_seg(wqe, wr->ex.invalidate_rkey);
  2684. wqe += sizeof (struct mlx4_wqe_local_inval_seg);
  2685. size += sizeof (struct mlx4_wqe_local_inval_seg) / 16;
  2686. break;
  2687. case IB_WR_REG_MR:
  2688. ctrl->srcrb_flags |=
  2689. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  2690. set_reg_seg(wqe, reg_wr(wr));
  2691. wqe += sizeof(struct mlx4_wqe_fmr_seg);
  2692. size += sizeof(struct mlx4_wqe_fmr_seg) / 16;
  2693. break;
  2694. default:
  2695. /* No extra segments required for sends */
  2696. break;
  2697. }
  2698. break;
  2699. case MLX4_IB_QPT_TUN_SMI_OWNER:
  2700. err = build_sriov_qp0_header(to_msqp(qp), ud_wr(wr),
  2701. ctrl, &seglen);
  2702. if (unlikely(err)) {
  2703. *bad_wr = wr;
  2704. goto out;
  2705. }
  2706. wqe += seglen;
  2707. size += seglen / 16;
  2708. break;
  2709. case MLX4_IB_QPT_TUN_SMI:
  2710. case MLX4_IB_QPT_TUN_GSI:
  2711. /* this is a UD qp used in MAD responses to slaves. */
  2712. set_datagram_seg(wqe, ud_wr(wr));
  2713. /* set the forced-loopback bit in the data seg av */
  2714. *(__be32 *) wqe |= cpu_to_be32(0x80000000);
  2715. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  2716. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  2717. break;
  2718. case MLX4_IB_QPT_UD:
  2719. set_datagram_seg(wqe, ud_wr(wr));
  2720. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  2721. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  2722. if (wr->opcode == IB_WR_LSO) {
  2723. err = build_lso_seg(wqe, ud_wr(wr), qp, &seglen,
  2724. &lso_hdr_sz, &blh);
  2725. if (unlikely(err)) {
  2726. *bad_wr = wr;
  2727. goto out;
  2728. }
  2729. lso_wqe = (__be32 *) wqe;
  2730. wqe += seglen;
  2731. size += seglen / 16;
  2732. }
  2733. break;
  2734. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  2735. err = build_sriov_qp0_header(to_msqp(qp), ud_wr(wr),
  2736. ctrl, &seglen);
  2737. if (unlikely(err)) {
  2738. *bad_wr = wr;
  2739. goto out;
  2740. }
  2741. wqe += seglen;
  2742. size += seglen / 16;
  2743. /* to start tunnel header on a cache-line boundary */
  2744. add_zero_len_inline(wqe);
  2745. wqe += 16;
  2746. size++;
  2747. build_tunnel_header(ud_wr(wr), wqe, &seglen);
  2748. wqe += seglen;
  2749. size += seglen / 16;
  2750. break;
  2751. case MLX4_IB_QPT_PROXY_SMI:
  2752. case MLX4_IB_QPT_PROXY_GSI:
  2753. /* If we are tunneling special qps, this is a UD qp.
  2754. * In this case we first add a UD segment targeting
  2755. * the tunnel qp, and then add a header with address
  2756. * information */
  2757. set_tunnel_datagram_seg(to_mdev(ibqp->device), wqe,
  2758. ud_wr(wr),
  2759. qp->mlx4_ib_qp_type);
  2760. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  2761. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  2762. build_tunnel_header(ud_wr(wr), wqe, &seglen);
  2763. wqe += seglen;
  2764. size += seglen / 16;
  2765. break;
  2766. case MLX4_IB_QPT_SMI:
  2767. case MLX4_IB_QPT_GSI:
  2768. err = build_mlx_header(to_msqp(qp), ud_wr(wr), ctrl,
  2769. &seglen);
  2770. if (unlikely(err)) {
  2771. *bad_wr = wr;
  2772. goto out;
  2773. }
  2774. wqe += seglen;
  2775. size += seglen / 16;
  2776. break;
  2777. default:
  2778. break;
  2779. }
  2780. /*
  2781. * Write data segments in reverse order, so as to
  2782. * overwrite cacheline stamp last within each
  2783. * cacheline. This avoids issues with WQE
  2784. * prefetching.
  2785. */
  2786. dseg = wqe;
  2787. dseg += wr->num_sge - 1;
  2788. size += wr->num_sge * (sizeof (struct mlx4_wqe_data_seg) / 16);
  2789. /* Add one more inline data segment for ICRC for MLX sends */
  2790. if (unlikely(qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
  2791. qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI ||
  2792. qp->mlx4_ib_qp_type &
  2793. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))) {
  2794. set_mlx_icrc_seg(dseg + 1);
  2795. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  2796. }
  2797. for (i = wr->num_sge - 1; i >= 0; --i, --dseg)
  2798. set_data_seg(dseg, wr->sg_list + i);
  2799. /*
  2800. * Possibly overwrite stamping in cacheline with LSO
  2801. * segment only after making sure all data segments
  2802. * are written.
  2803. */
  2804. wmb();
  2805. *lso_wqe = lso_hdr_sz;
  2806. ctrl->qpn_vlan.fence_size = (wr->send_flags & IB_SEND_FENCE ?
  2807. MLX4_WQE_CTRL_FENCE : 0) | size;
  2808. /*
  2809. * Make sure descriptor is fully written before
  2810. * setting ownership bit (because HW can start
  2811. * executing as soon as we do).
  2812. */
  2813. wmb();
  2814. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  2815. *bad_wr = wr;
  2816. err = -EINVAL;
  2817. goto out;
  2818. }
  2819. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  2820. (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0) | blh;
  2821. stamp = ind + qp->sq_spare_wqes;
  2822. ind += DIV_ROUND_UP(size * 16, 1U << qp->sq.wqe_shift);
  2823. /*
  2824. * We can improve latency by not stamping the last
  2825. * send queue WQE until after ringing the doorbell, so
  2826. * only stamp here if there are still more WQEs to post.
  2827. *
  2828. * Same optimization applies to padding with NOP wqe
  2829. * in case of WQE shrinking (used to prevent wrap-around
  2830. * in the middle of WR).
  2831. */
  2832. if (wr->next) {
  2833. stamp_send_wqe(qp, stamp, size * 16);
  2834. ind = pad_wraparound(qp, ind);
  2835. }
  2836. }
  2837. out:
  2838. if (likely(nreq)) {
  2839. qp->sq.head += nreq;
  2840. /*
  2841. * Make sure that descriptors are written before
  2842. * doorbell record.
  2843. */
  2844. wmb();
  2845. writel(qp->doorbell_qpn,
  2846. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  2847. /*
  2848. * Make sure doorbells don't leak out of SQ spinlock
  2849. * and reach the HCA out of order.
  2850. */
  2851. mmiowb();
  2852. stamp_send_wqe(qp, stamp, size * 16);
  2853. ind = pad_wraparound(qp, ind);
  2854. qp->sq_next_wqe = ind;
  2855. }
  2856. spin_unlock_irqrestore(&qp->sq.lock, flags);
  2857. return err;
  2858. }
  2859. int mlx4_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  2860. struct ib_recv_wr **bad_wr)
  2861. {
  2862. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2863. struct mlx4_wqe_data_seg *scat;
  2864. unsigned long flags;
  2865. int err = 0;
  2866. int nreq;
  2867. int ind;
  2868. int max_gs;
  2869. int i;
  2870. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  2871. max_gs = qp->rq.max_gs;
  2872. spin_lock_irqsave(&qp->rq.lock, flags);
  2873. if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  2874. err = -EIO;
  2875. *bad_wr = wr;
  2876. nreq = 0;
  2877. goto out;
  2878. }
  2879. ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
  2880. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  2881. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
  2882. err = -ENOMEM;
  2883. *bad_wr = wr;
  2884. goto out;
  2885. }
  2886. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  2887. err = -EINVAL;
  2888. *bad_wr = wr;
  2889. goto out;
  2890. }
  2891. scat = get_recv_wqe(qp, ind);
  2892. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  2893. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
  2894. ib_dma_sync_single_for_device(ibqp->device,
  2895. qp->sqp_proxy_rcv[ind].map,
  2896. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  2897. DMA_FROM_DEVICE);
  2898. scat->byte_count =
  2899. cpu_to_be32(sizeof (struct mlx4_ib_proxy_sqp_hdr));
  2900. /* use dma lkey from upper layer entry */
  2901. scat->lkey = cpu_to_be32(wr->sg_list->lkey);
  2902. scat->addr = cpu_to_be64(qp->sqp_proxy_rcv[ind].map);
  2903. scat++;
  2904. max_gs--;
  2905. }
  2906. for (i = 0; i < wr->num_sge; ++i)
  2907. __set_data_seg(scat + i, wr->sg_list + i);
  2908. if (i < max_gs) {
  2909. scat[i].byte_count = 0;
  2910. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  2911. scat[i].addr = 0;
  2912. }
  2913. qp->rq.wrid[ind] = wr->wr_id;
  2914. ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
  2915. }
  2916. out:
  2917. if (likely(nreq)) {
  2918. qp->rq.head += nreq;
  2919. /*
  2920. * Make sure that descriptors are written before
  2921. * doorbell record.
  2922. */
  2923. wmb();
  2924. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  2925. }
  2926. spin_unlock_irqrestore(&qp->rq.lock, flags);
  2927. return err;
  2928. }
  2929. static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state)
  2930. {
  2931. switch (mlx4_state) {
  2932. case MLX4_QP_STATE_RST: return IB_QPS_RESET;
  2933. case MLX4_QP_STATE_INIT: return IB_QPS_INIT;
  2934. case MLX4_QP_STATE_RTR: return IB_QPS_RTR;
  2935. case MLX4_QP_STATE_RTS: return IB_QPS_RTS;
  2936. case MLX4_QP_STATE_SQ_DRAINING:
  2937. case MLX4_QP_STATE_SQD: return IB_QPS_SQD;
  2938. case MLX4_QP_STATE_SQER: return IB_QPS_SQE;
  2939. case MLX4_QP_STATE_ERR: return IB_QPS_ERR;
  2940. default: return -1;
  2941. }
  2942. }
  2943. static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state)
  2944. {
  2945. switch (mlx4_mig_state) {
  2946. case MLX4_QP_PM_ARMED: return IB_MIG_ARMED;
  2947. case MLX4_QP_PM_REARM: return IB_MIG_REARM;
  2948. case MLX4_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
  2949. default: return -1;
  2950. }
  2951. }
  2952. static int to_ib_qp_access_flags(int mlx4_flags)
  2953. {
  2954. int ib_flags = 0;
  2955. if (mlx4_flags & MLX4_QP_BIT_RRE)
  2956. ib_flags |= IB_ACCESS_REMOTE_READ;
  2957. if (mlx4_flags & MLX4_QP_BIT_RWE)
  2958. ib_flags |= IB_ACCESS_REMOTE_WRITE;
  2959. if (mlx4_flags & MLX4_QP_BIT_RAE)
  2960. ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
  2961. return ib_flags;
  2962. }
  2963. static void to_rdma_ah_attr(struct mlx4_ib_dev *ibdev,
  2964. struct rdma_ah_attr *ah_attr,
  2965. struct mlx4_qp_path *path)
  2966. {
  2967. struct mlx4_dev *dev = ibdev->dev;
  2968. u8 port_num = path->sched_queue & 0x40 ? 2 : 1;
  2969. memset(ah_attr, 0, sizeof(*ah_attr));
  2970. ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port_num);
  2971. if (port_num == 0 || port_num > dev->caps.num_ports)
  2972. return;
  2973. if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE)
  2974. rdma_ah_set_sl(ah_attr, ((path->sched_queue >> 3) & 0x7) |
  2975. ((path->sched_queue & 4) << 1));
  2976. else
  2977. rdma_ah_set_sl(ah_attr, (path->sched_queue >> 2) & 0xf);
  2978. rdma_ah_set_port_num(ah_attr, port_num);
  2979. rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
  2980. rdma_ah_set_path_bits(ah_attr, path->grh_mylmc & 0x7f);
  2981. rdma_ah_set_static_rate(ah_attr,
  2982. path->static_rate ? path->static_rate - 5 : 0);
  2983. if (path->grh_mylmc & (1 << 7)) {
  2984. rdma_ah_set_grh(ah_attr, NULL,
  2985. be32_to_cpu(path->tclass_flowlabel) & 0xfffff,
  2986. path->mgid_index,
  2987. path->hop_limit,
  2988. (be32_to_cpu(path->tclass_flowlabel)
  2989. >> 20) & 0xff);
  2990. rdma_ah_set_dgid_raw(ah_attr, path->rgid);
  2991. }
  2992. }
  2993. int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
  2994. struct ib_qp_init_attr *qp_init_attr)
  2995. {
  2996. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  2997. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2998. struct mlx4_qp_context context;
  2999. int mlx4_state;
  3000. int err = 0;
  3001. mutex_lock(&qp->mutex);
  3002. if (qp->state == IB_QPS_RESET) {
  3003. qp_attr->qp_state = IB_QPS_RESET;
  3004. goto done;
  3005. }
  3006. err = mlx4_qp_query(dev->dev, &qp->mqp, &context);
  3007. if (err) {
  3008. err = -EINVAL;
  3009. goto out;
  3010. }
  3011. mlx4_state = be32_to_cpu(context.flags) >> 28;
  3012. qp->state = to_ib_qp_state(mlx4_state);
  3013. qp_attr->qp_state = qp->state;
  3014. qp_attr->path_mtu = context.mtu_msgmax >> 5;
  3015. qp_attr->path_mig_state =
  3016. to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3);
  3017. qp_attr->qkey = be32_to_cpu(context.qkey);
  3018. qp_attr->rq_psn = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff;
  3019. qp_attr->sq_psn = be32_to_cpu(context.next_send_psn) & 0xffffff;
  3020. qp_attr->dest_qp_num = be32_to_cpu(context.remote_qpn) & 0xffffff;
  3021. qp_attr->qp_access_flags =
  3022. to_ib_qp_access_flags(be32_to_cpu(context.params2));
  3023. if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
  3024. to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context.pri_path);
  3025. to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context.alt_path);
  3026. qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f;
  3027. qp_attr->alt_port_num =
  3028. rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
  3029. }
  3030. qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f;
  3031. if (qp_attr->qp_state == IB_QPS_INIT)
  3032. qp_attr->port_num = qp->port;
  3033. else
  3034. qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1;
  3035. /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
  3036. qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING;
  3037. qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7);
  3038. qp_attr->max_dest_rd_atomic =
  3039. 1 << ((be32_to_cpu(context.params2) >> 21) & 0x7);
  3040. qp_attr->min_rnr_timer =
  3041. (be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f;
  3042. qp_attr->timeout = context.pri_path.ackto >> 3;
  3043. qp_attr->retry_cnt = (be32_to_cpu(context.params1) >> 16) & 0x7;
  3044. qp_attr->rnr_retry = (be32_to_cpu(context.params1) >> 13) & 0x7;
  3045. qp_attr->alt_timeout = context.alt_path.ackto >> 3;
  3046. done:
  3047. qp_attr->cur_qp_state = qp_attr->qp_state;
  3048. qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
  3049. qp_attr->cap.max_recv_sge = qp->rq.max_gs;
  3050. if (!ibqp->uobject) {
  3051. qp_attr->cap.max_send_wr = qp->sq.wqe_cnt;
  3052. qp_attr->cap.max_send_sge = qp->sq.max_gs;
  3053. } else {
  3054. qp_attr->cap.max_send_wr = 0;
  3055. qp_attr->cap.max_send_sge = 0;
  3056. }
  3057. /*
  3058. * We don't support inline sends for kernel QPs (yet), and we
  3059. * don't know what userspace's value should be.
  3060. */
  3061. qp_attr->cap.max_inline_data = 0;
  3062. qp_init_attr->cap = qp_attr->cap;
  3063. qp_init_attr->create_flags = 0;
  3064. if (qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK)
  3065. qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
  3066. if (qp->flags & MLX4_IB_QP_LSO)
  3067. qp_init_attr->create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;
  3068. if (qp->flags & MLX4_IB_QP_NETIF)
  3069. qp_init_attr->create_flags |= IB_QP_CREATE_NETIF_QP;
  3070. qp_init_attr->sq_sig_type =
  3071. qp->sq_signal_bits == cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) ?
  3072. IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
  3073. out:
  3074. mutex_unlock(&qp->mutex);
  3075. return err;
  3076. }