qp.c 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474
  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 <rdma/ib_cache.h>
  39. #include <rdma/ib_pack.h>
  40. #include <rdma/ib_addr.h>
  41. #include <rdma/ib_mad.h>
  42. #include <rdma/uverbs_ioctl.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. static int _mlx4_ib_modify_wq(struct ib_wq *ibwq, enum ib_wq_state new_state,
  52. struct ib_udata *udata);
  53. enum {
  54. MLX4_IB_ACK_REQ_FREQ = 8,
  55. };
  56. enum {
  57. MLX4_IB_DEFAULT_SCHED_QUEUE = 0x83,
  58. MLX4_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
  59. MLX4_IB_LINK_TYPE_IB = 0,
  60. MLX4_IB_LINK_TYPE_ETH = 1
  61. };
  62. enum {
  63. MLX4_IB_MIN_SQ_STRIDE = 6,
  64. MLX4_IB_CACHE_LINE_SIZE = 64,
  65. };
  66. enum {
  67. MLX4_RAW_QP_MTU = 7,
  68. MLX4_RAW_QP_MSGMAX = 31,
  69. };
  70. #ifndef ETH_ALEN
  71. #define ETH_ALEN 6
  72. #endif
  73. static const __be32 mlx4_ib_opcode[] = {
  74. [IB_WR_SEND] = cpu_to_be32(MLX4_OPCODE_SEND),
  75. [IB_WR_LSO] = cpu_to_be32(MLX4_OPCODE_LSO),
  76. [IB_WR_SEND_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_SEND_IMM),
  77. [IB_WR_RDMA_WRITE] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE),
  78. [IB_WR_RDMA_WRITE_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE_IMM),
  79. [IB_WR_RDMA_READ] = cpu_to_be32(MLX4_OPCODE_RDMA_READ),
  80. [IB_WR_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_ATOMIC_CS),
  81. [IB_WR_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_ATOMIC_FA),
  82. [IB_WR_SEND_WITH_INV] = cpu_to_be32(MLX4_OPCODE_SEND_INVAL),
  83. [IB_WR_LOCAL_INV] = cpu_to_be32(MLX4_OPCODE_LOCAL_INVAL),
  84. [IB_WR_REG_MR] = cpu_to_be32(MLX4_OPCODE_FMR),
  85. [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_CS),
  86. [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_FA),
  87. };
  88. enum mlx4_ib_source_type {
  89. MLX4_IB_QP_SRC = 0,
  90. MLX4_IB_RWQ_SRC = 1,
  91. };
  92. static int is_tunnel_qp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  93. {
  94. if (!mlx4_is_master(dev->dev))
  95. return 0;
  96. return qp->mqp.qpn >= dev->dev->phys_caps.base_tunnel_sqpn &&
  97. qp->mqp.qpn < dev->dev->phys_caps.base_tunnel_sqpn +
  98. 8 * MLX4_MFUNC_MAX;
  99. }
  100. static int is_sqp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  101. {
  102. int proxy_sqp = 0;
  103. int real_sqp = 0;
  104. int i;
  105. /* PPF or Native -- real SQP */
  106. real_sqp = ((mlx4_is_master(dev->dev) || !mlx4_is_mfunc(dev->dev)) &&
  107. qp->mqp.qpn >= dev->dev->phys_caps.base_sqpn &&
  108. qp->mqp.qpn <= dev->dev->phys_caps.base_sqpn + 3);
  109. if (real_sqp)
  110. return 1;
  111. /* VF or PF -- proxy SQP */
  112. if (mlx4_is_mfunc(dev->dev)) {
  113. for (i = 0; i < dev->dev->caps.num_ports; i++) {
  114. if (qp->mqp.qpn == dev->dev->caps.spec_qps[i].qp0_proxy ||
  115. qp->mqp.qpn == dev->dev->caps.spec_qps[i].qp1_proxy) {
  116. proxy_sqp = 1;
  117. break;
  118. }
  119. }
  120. }
  121. if (proxy_sqp)
  122. return 1;
  123. return !!(qp->flags & MLX4_IB_ROCE_V2_GSI_QP);
  124. }
  125. /* used for INIT/CLOSE port logic */
  126. static int is_qp0(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  127. {
  128. int proxy_qp0 = 0;
  129. int real_qp0 = 0;
  130. int i;
  131. /* PPF or Native -- real QP0 */
  132. real_qp0 = ((mlx4_is_master(dev->dev) || !mlx4_is_mfunc(dev->dev)) &&
  133. qp->mqp.qpn >= dev->dev->phys_caps.base_sqpn &&
  134. qp->mqp.qpn <= dev->dev->phys_caps.base_sqpn + 1);
  135. if (real_qp0)
  136. return 1;
  137. /* VF or PF -- proxy QP0 */
  138. if (mlx4_is_mfunc(dev->dev)) {
  139. for (i = 0; i < dev->dev->caps.num_ports; i++) {
  140. if (qp->mqp.qpn == dev->dev->caps.spec_qps[i].qp0_proxy) {
  141. proxy_qp0 = 1;
  142. break;
  143. }
  144. }
  145. }
  146. return proxy_qp0;
  147. }
  148. static void *get_wqe(struct mlx4_ib_qp *qp, int offset)
  149. {
  150. return mlx4_buf_offset(&qp->buf, offset);
  151. }
  152. static void *get_recv_wqe(struct mlx4_ib_qp *qp, int n)
  153. {
  154. return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
  155. }
  156. static void *get_send_wqe(struct mlx4_ib_qp *qp, int n)
  157. {
  158. return get_wqe(qp, qp->sq.offset + (n << qp->sq.wqe_shift));
  159. }
  160. /*
  161. * Stamp a SQ WQE so that it is invalid if prefetched by marking the
  162. * first four bytes of every 64 byte chunk with 0xffffffff, except for
  163. * the very first chunk of the WQE.
  164. */
  165. static void stamp_send_wqe(struct mlx4_ib_qp *qp, int n)
  166. {
  167. __be32 *wqe;
  168. int i;
  169. int s;
  170. void *buf;
  171. struct mlx4_wqe_ctrl_seg *ctrl;
  172. buf = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
  173. ctrl = (struct mlx4_wqe_ctrl_seg *)buf;
  174. s = (ctrl->qpn_vlan.fence_size & 0x3f) << 4;
  175. for (i = 64; i < s; i += 64) {
  176. wqe = buf + i;
  177. *wqe = cpu_to_be32(0xffffffff);
  178. }
  179. }
  180. static void mlx4_ib_qp_event(struct mlx4_qp *qp, enum mlx4_event type)
  181. {
  182. struct ib_event event;
  183. struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
  184. if (type == MLX4_EVENT_TYPE_PATH_MIG)
  185. to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
  186. if (ibqp->event_handler) {
  187. event.device = ibqp->device;
  188. event.element.qp = ibqp;
  189. switch (type) {
  190. case MLX4_EVENT_TYPE_PATH_MIG:
  191. event.event = IB_EVENT_PATH_MIG;
  192. break;
  193. case MLX4_EVENT_TYPE_COMM_EST:
  194. event.event = IB_EVENT_COMM_EST;
  195. break;
  196. case MLX4_EVENT_TYPE_SQ_DRAINED:
  197. event.event = IB_EVENT_SQ_DRAINED;
  198. break;
  199. case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
  200. event.event = IB_EVENT_QP_LAST_WQE_REACHED;
  201. break;
  202. case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
  203. event.event = IB_EVENT_QP_FATAL;
  204. break;
  205. case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
  206. event.event = IB_EVENT_PATH_MIG_ERR;
  207. break;
  208. case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  209. event.event = IB_EVENT_QP_REQ_ERR;
  210. break;
  211. case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
  212. event.event = IB_EVENT_QP_ACCESS_ERR;
  213. break;
  214. default:
  215. pr_warn("Unexpected event type %d "
  216. "on QP %06x\n", type, qp->qpn);
  217. return;
  218. }
  219. ibqp->event_handler(&event, ibqp->qp_context);
  220. }
  221. }
  222. static void mlx4_ib_wq_event(struct mlx4_qp *qp, enum mlx4_event type)
  223. {
  224. pr_warn_ratelimited("Unexpected event type %d on WQ 0x%06x. Events are not supported for WQs\n",
  225. type, qp->qpn);
  226. }
  227. static int send_wqe_overhead(enum mlx4_ib_qp_type type, u32 flags)
  228. {
  229. /*
  230. * UD WQEs must have a datagram segment.
  231. * RC and UC WQEs might have a remote address segment.
  232. * MLX WQEs need two extra inline data segments (for the UD
  233. * header and space for the ICRC).
  234. */
  235. switch (type) {
  236. case MLX4_IB_QPT_UD:
  237. return sizeof (struct mlx4_wqe_ctrl_seg) +
  238. sizeof (struct mlx4_wqe_datagram_seg) +
  239. ((flags & MLX4_IB_QP_LSO) ? MLX4_IB_LSO_HEADER_SPARE : 0);
  240. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  241. case MLX4_IB_QPT_PROXY_SMI:
  242. case MLX4_IB_QPT_PROXY_GSI:
  243. return sizeof (struct mlx4_wqe_ctrl_seg) +
  244. sizeof (struct mlx4_wqe_datagram_seg) + 64;
  245. case MLX4_IB_QPT_TUN_SMI_OWNER:
  246. case MLX4_IB_QPT_TUN_GSI:
  247. return sizeof (struct mlx4_wqe_ctrl_seg) +
  248. sizeof (struct mlx4_wqe_datagram_seg);
  249. case MLX4_IB_QPT_UC:
  250. return sizeof (struct mlx4_wqe_ctrl_seg) +
  251. sizeof (struct mlx4_wqe_raddr_seg);
  252. case MLX4_IB_QPT_RC:
  253. return sizeof (struct mlx4_wqe_ctrl_seg) +
  254. sizeof (struct mlx4_wqe_masked_atomic_seg) +
  255. sizeof (struct mlx4_wqe_raddr_seg);
  256. case MLX4_IB_QPT_SMI:
  257. case MLX4_IB_QPT_GSI:
  258. return sizeof (struct mlx4_wqe_ctrl_seg) +
  259. ALIGN(MLX4_IB_UD_HEADER_SIZE +
  260. DIV_ROUND_UP(MLX4_IB_UD_HEADER_SIZE,
  261. MLX4_INLINE_ALIGN) *
  262. sizeof (struct mlx4_wqe_inline_seg),
  263. sizeof (struct mlx4_wqe_data_seg)) +
  264. ALIGN(4 +
  265. sizeof (struct mlx4_wqe_inline_seg),
  266. sizeof (struct mlx4_wqe_data_seg));
  267. default:
  268. return sizeof (struct mlx4_wqe_ctrl_seg);
  269. }
  270. }
  271. static int set_rq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  272. bool is_user, bool has_rq, struct mlx4_ib_qp *qp,
  273. u32 inl_recv_sz)
  274. {
  275. /* Sanity check RQ size before proceeding */
  276. if (cap->max_recv_wr > dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE ||
  277. cap->max_recv_sge > min(dev->dev->caps.max_sq_sg, dev->dev->caps.max_rq_sg))
  278. return -EINVAL;
  279. if (!has_rq) {
  280. if (cap->max_recv_wr || inl_recv_sz)
  281. return -EINVAL;
  282. qp->rq.wqe_cnt = qp->rq.max_gs = 0;
  283. } else {
  284. u32 max_inl_recv_sz = dev->dev->caps.max_rq_sg *
  285. sizeof(struct mlx4_wqe_data_seg);
  286. u32 wqe_size;
  287. /* HW requires >= 1 RQ entry with >= 1 gather entry */
  288. if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge ||
  289. inl_recv_sz > max_inl_recv_sz))
  290. return -EINVAL;
  291. qp->rq.wqe_cnt = roundup_pow_of_two(max(1U, cap->max_recv_wr));
  292. qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge));
  293. wqe_size = qp->rq.max_gs * sizeof(struct mlx4_wqe_data_seg);
  294. qp->rq.wqe_shift = ilog2(max_t(u32, wqe_size, inl_recv_sz));
  295. }
  296. /* leave userspace return values as they were, so as not to break ABI */
  297. if (is_user) {
  298. cap->max_recv_wr = qp->rq.max_post = qp->rq.wqe_cnt;
  299. cap->max_recv_sge = qp->rq.max_gs;
  300. } else {
  301. cap->max_recv_wr = qp->rq.max_post =
  302. min(dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE, qp->rq.wqe_cnt);
  303. cap->max_recv_sge = min(qp->rq.max_gs,
  304. min(dev->dev->caps.max_sq_sg,
  305. dev->dev->caps.max_rq_sg));
  306. }
  307. return 0;
  308. }
  309. static int set_kernel_sq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  310. enum mlx4_ib_qp_type type, struct mlx4_ib_qp *qp)
  311. {
  312. int s;
  313. /* Sanity check SQ size before proceeding */
  314. if (cap->max_send_wr > (dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE) ||
  315. cap->max_send_sge > min(dev->dev->caps.max_sq_sg, dev->dev->caps.max_rq_sg) ||
  316. cap->max_inline_data + send_wqe_overhead(type, qp->flags) +
  317. sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz)
  318. return -EINVAL;
  319. /*
  320. * For MLX transport we need 2 extra S/G entries:
  321. * one for the header and one for the checksum at the end
  322. */
  323. if ((type == MLX4_IB_QPT_SMI || type == MLX4_IB_QPT_GSI ||
  324. type & (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) &&
  325. cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg)
  326. return -EINVAL;
  327. s = max(cap->max_send_sge * sizeof (struct mlx4_wqe_data_seg),
  328. cap->max_inline_data + sizeof (struct mlx4_wqe_inline_seg)) +
  329. send_wqe_overhead(type, qp->flags);
  330. if (s > dev->dev->caps.max_sq_desc_sz)
  331. return -EINVAL;
  332. qp->sq.wqe_shift = ilog2(roundup_pow_of_two(s));
  333. /*
  334. * We need to leave 2 KB + 1 WR of headroom in the SQ to
  335. * allow HW to prefetch.
  336. */
  337. qp->sq_spare_wqes = MLX4_IB_SQ_HEADROOM(qp->sq.wqe_shift);
  338. qp->sq.wqe_cnt = roundup_pow_of_two(cap->max_send_wr +
  339. qp->sq_spare_wqes);
  340. qp->sq.max_gs =
  341. (min(dev->dev->caps.max_sq_desc_sz,
  342. (1 << qp->sq.wqe_shift)) -
  343. send_wqe_overhead(type, qp->flags)) /
  344. sizeof (struct mlx4_wqe_data_seg);
  345. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  346. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  347. if (qp->rq.wqe_shift > qp->sq.wqe_shift) {
  348. qp->rq.offset = 0;
  349. qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
  350. } else {
  351. qp->rq.offset = qp->sq.wqe_cnt << qp->sq.wqe_shift;
  352. qp->sq.offset = 0;
  353. }
  354. cap->max_send_wr = qp->sq.max_post =
  355. qp->sq.wqe_cnt - qp->sq_spare_wqes;
  356. cap->max_send_sge = min(qp->sq.max_gs,
  357. min(dev->dev->caps.max_sq_sg,
  358. dev->dev->caps.max_rq_sg));
  359. /* We don't support inline sends for kernel QPs (yet) */
  360. cap->max_inline_data = 0;
  361. return 0;
  362. }
  363. static int set_user_sq_size(struct mlx4_ib_dev *dev,
  364. struct mlx4_ib_qp *qp,
  365. struct mlx4_ib_create_qp *ucmd)
  366. {
  367. u32 cnt;
  368. /* Sanity check SQ size before proceeding */
  369. if (check_shl_overflow(1, ucmd->log_sq_bb_count, &cnt) ||
  370. cnt > dev->dev->caps.max_wqes)
  371. return -EINVAL;
  372. if (ucmd->log_sq_stride >
  373. ilog2(roundup_pow_of_two(dev->dev->caps.max_sq_desc_sz)) ||
  374. ucmd->log_sq_stride < MLX4_IB_MIN_SQ_STRIDE)
  375. return -EINVAL;
  376. qp->sq.wqe_cnt = 1 << ucmd->log_sq_bb_count;
  377. qp->sq.wqe_shift = ucmd->log_sq_stride;
  378. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  379. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  380. return 0;
  381. }
  382. static int alloc_proxy_bufs(struct ib_device *dev, struct mlx4_ib_qp *qp)
  383. {
  384. int i;
  385. qp->sqp_proxy_rcv =
  386. kmalloc_array(qp->rq.wqe_cnt, sizeof(struct mlx4_ib_buf),
  387. GFP_KERNEL);
  388. if (!qp->sqp_proxy_rcv)
  389. return -ENOMEM;
  390. for (i = 0; i < qp->rq.wqe_cnt; i++) {
  391. qp->sqp_proxy_rcv[i].addr =
  392. kmalloc(sizeof (struct mlx4_ib_proxy_sqp_hdr),
  393. GFP_KERNEL);
  394. if (!qp->sqp_proxy_rcv[i].addr)
  395. goto err;
  396. qp->sqp_proxy_rcv[i].map =
  397. ib_dma_map_single(dev, qp->sqp_proxy_rcv[i].addr,
  398. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  399. DMA_FROM_DEVICE);
  400. if (ib_dma_mapping_error(dev, qp->sqp_proxy_rcv[i].map)) {
  401. kfree(qp->sqp_proxy_rcv[i].addr);
  402. goto err;
  403. }
  404. }
  405. return 0;
  406. err:
  407. while (i > 0) {
  408. --i;
  409. ib_dma_unmap_single(dev, qp->sqp_proxy_rcv[i].map,
  410. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  411. DMA_FROM_DEVICE);
  412. kfree(qp->sqp_proxy_rcv[i].addr);
  413. }
  414. kfree(qp->sqp_proxy_rcv);
  415. qp->sqp_proxy_rcv = NULL;
  416. return -ENOMEM;
  417. }
  418. static void free_proxy_bufs(struct ib_device *dev, struct mlx4_ib_qp *qp)
  419. {
  420. int i;
  421. for (i = 0; i < qp->rq.wqe_cnt; i++) {
  422. ib_dma_unmap_single(dev, qp->sqp_proxy_rcv[i].map,
  423. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  424. DMA_FROM_DEVICE);
  425. kfree(qp->sqp_proxy_rcv[i].addr);
  426. }
  427. kfree(qp->sqp_proxy_rcv);
  428. }
  429. static bool qp_has_rq(struct ib_qp_init_attr *attr)
  430. {
  431. if (attr->qp_type == IB_QPT_XRC_INI || attr->qp_type == IB_QPT_XRC_TGT)
  432. return false;
  433. return !attr->srq;
  434. }
  435. static int qp0_enabled_vf(struct mlx4_dev *dev, int qpn)
  436. {
  437. int i;
  438. for (i = 0; i < dev->caps.num_ports; i++) {
  439. if (qpn == dev->caps.spec_qps[i].qp0_proxy)
  440. return !!dev->caps.spec_qps[i].qp0_qkey;
  441. }
  442. return 0;
  443. }
  444. static void mlx4_ib_free_qp_counter(struct mlx4_ib_dev *dev,
  445. struct mlx4_ib_qp *qp)
  446. {
  447. mutex_lock(&dev->counters_table[qp->port - 1].mutex);
  448. mlx4_counter_free(dev->dev, qp->counter_index->index);
  449. list_del(&qp->counter_index->list);
  450. mutex_unlock(&dev->counters_table[qp->port - 1].mutex);
  451. kfree(qp->counter_index);
  452. qp->counter_index = NULL;
  453. }
  454. static int set_qp_rss(struct mlx4_ib_dev *dev, struct mlx4_ib_rss *rss_ctx,
  455. struct ib_qp_init_attr *init_attr,
  456. struct mlx4_ib_create_qp_rss *ucmd)
  457. {
  458. rss_ctx->base_qpn_tbl_sz = init_attr->rwq_ind_tbl->ind_tbl[0]->wq_num |
  459. (init_attr->rwq_ind_tbl->log_ind_tbl_size << 24);
  460. if ((ucmd->rx_hash_function == MLX4_IB_RX_HASH_FUNC_TOEPLITZ) &&
  461. (dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_TOP)) {
  462. memcpy(rss_ctx->rss_key, ucmd->rx_hash_key,
  463. MLX4_EN_RSS_KEY_SIZE);
  464. } else {
  465. pr_debug("RX Hash function is not supported\n");
  466. return (-EOPNOTSUPP);
  467. }
  468. if (ucmd->rx_hash_fields_mask & ~(u64)(MLX4_IB_RX_HASH_SRC_IPV4 |
  469. MLX4_IB_RX_HASH_DST_IPV4 |
  470. MLX4_IB_RX_HASH_SRC_IPV6 |
  471. MLX4_IB_RX_HASH_DST_IPV6 |
  472. MLX4_IB_RX_HASH_SRC_PORT_TCP |
  473. MLX4_IB_RX_HASH_DST_PORT_TCP |
  474. MLX4_IB_RX_HASH_SRC_PORT_UDP |
  475. MLX4_IB_RX_HASH_DST_PORT_UDP |
  476. MLX4_IB_RX_HASH_INNER)) {
  477. pr_debug("RX Hash fields_mask has unsupported mask (0x%llx)\n",
  478. ucmd->rx_hash_fields_mask);
  479. return (-EOPNOTSUPP);
  480. }
  481. if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV4) &&
  482. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV4)) {
  483. rss_ctx->flags = MLX4_RSS_IPV4;
  484. } else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV4) ||
  485. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV4)) {
  486. pr_debug("RX Hash fields_mask is not supported - both IPv4 SRC and DST must be set\n");
  487. return (-EOPNOTSUPP);
  488. }
  489. if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV6) &&
  490. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV6)) {
  491. rss_ctx->flags |= MLX4_RSS_IPV6;
  492. } else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV6) ||
  493. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV6)) {
  494. pr_debug("RX Hash fields_mask is not supported - both IPv6 SRC and DST must be set\n");
  495. return (-EOPNOTSUPP);
  496. }
  497. if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_UDP) &&
  498. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_UDP)) {
  499. if (!(dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UDP_RSS)) {
  500. pr_debug("RX Hash fields_mask for UDP is not supported\n");
  501. return (-EOPNOTSUPP);
  502. }
  503. if (rss_ctx->flags & MLX4_RSS_IPV4)
  504. rss_ctx->flags |= MLX4_RSS_UDP_IPV4;
  505. if (rss_ctx->flags & MLX4_RSS_IPV6)
  506. rss_ctx->flags |= MLX4_RSS_UDP_IPV6;
  507. if (!(rss_ctx->flags & (MLX4_RSS_IPV6 | MLX4_RSS_IPV4))) {
  508. pr_debug("RX Hash fields_mask is not supported - UDP must be set with IPv4 or IPv6\n");
  509. return (-EOPNOTSUPP);
  510. }
  511. } else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_UDP) ||
  512. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_UDP)) {
  513. pr_debug("RX Hash fields_mask is not supported - both UDP SRC and DST must be set\n");
  514. return (-EOPNOTSUPP);
  515. }
  516. if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_TCP) &&
  517. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_TCP)) {
  518. if (rss_ctx->flags & MLX4_RSS_IPV4)
  519. rss_ctx->flags |= MLX4_RSS_TCP_IPV4;
  520. if (rss_ctx->flags & MLX4_RSS_IPV6)
  521. rss_ctx->flags |= MLX4_RSS_TCP_IPV6;
  522. if (!(rss_ctx->flags & (MLX4_RSS_IPV6 | MLX4_RSS_IPV4))) {
  523. pr_debug("RX Hash fields_mask is not supported - TCP must be set with IPv4 or IPv6\n");
  524. return (-EOPNOTSUPP);
  525. }
  526. } else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_TCP) ||
  527. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_TCP)) {
  528. pr_debug("RX Hash fields_mask is not supported - both TCP SRC and DST must be set\n");
  529. return (-EOPNOTSUPP);
  530. }
  531. if (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_INNER) {
  532. if (dev->dev->caps.tunnel_offload_mode ==
  533. MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
  534. /*
  535. * Hash according to inner headers if exist, otherwise
  536. * according to outer headers.
  537. */
  538. rss_ctx->flags |= MLX4_RSS_BY_INNER_HEADERS_IPONLY;
  539. } else {
  540. pr_debug("RSS Hash for inner headers isn't supported\n");
  541. return (-EOPNOTSUPP);
  542. }
  543. }
  544. return 0;
  545. }
  546. static int create_qp_rss(struct mlx4_ib_dev *dev,
  547. struct ib_qp_init_attr *init_attr,
  548. struct mlx4_ib_create_qp_rss *ucmd,
  549. struct mlx4_ib_qp *qp)
  550. {
  551. int qpn;
  552. int err;
  553. qp->mqp.usage = MLX4_RES_USAGE_USER_VERBS;
  554. err = mlx4_qp_reserve_range(dev->dev, 1, 1, &qpn, 0, qp->mqp.usage);
  555. if (err)
  556. return err;
  557. err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp);
  558. if (err)
  559. goto err_qpn;
  560. INIT_LIST_HEAD(&qp->gid_list);
  561. INIT_LIST_HEAD(&qp->steering_rules);
  562. qp->mlx4_ib_qp_type = MLX4_IB_QPT_RAW_PACKET;
  563. qp->state = IB_QPS_RESET;
  564. /* Set dummy send resources to be compatible with HV and PRM */
  565. qp->sq_no_prefetch = 1;
  566. qp->sq.wqe_cnt = 1;
  567. qp->sq.wqe_shift = MLX4_IB_MIN_SQ_STRIDE;
  568. qp->buf_size = qp->sq.wqe_cnt << MLX4_IB_MIN_SQ_STRIDE;
  569. qp->mtt = (to_mqp(
  570. (struct ib_qp *)init_attr->rwq_ind_tbl->ind_tbl[0]))->mtt;
  571. qp->rss_ctx = kzalloc(sizeof(*qp->rss_ctx), GFP_KERNEL);
  572. if (!qp->rss_ctx) {
  573. err = -ENOMEM;
  574. goto err_qp_alloc;
  575. }
  576. err = set_qp_rss(dev, qp->rss_ctx, init_attr, ucmd);
  577. if (err)
  578. goto err;
  579. return 0;
  580. err:
  581. kfree(qp->rss_ctx);
  582. err_qp_alloc:
  583. mlx4_qp_remove(dev->dev, &qp->mqp);
  584. mlx4_qp_free(dev->dev, &qp->mqp);
  585. err_qpn:
  586. mlx4_qp_release_range(dev->dev, qpn, 1);
  587. return err;
  588. }
  589. static int _mlx4_ib_create_qp_rss(struct ib_pd *pd, struct mlx4_ib_qp *qp,
  590. struct ib_qp_init_attr *init_attr,
  591. struct ib_udata *udata)
  592. {
  593. struct mlx4_ib_create_qp_rss ucmd = {};
  594. size_t required_cmd_sz;
  595. int err;
  596. if (!udata) {
  597. pr_debug("RSS QP with NULL udata\n");
  598. return -EINVAL;
  599. }
  600. if (udata->outlen)
  601. return -EOPNOTSUPP;
  602. required_cmd_sz = offsetof(typeof(ucmd), reserved1) +
  603. sizeof(ucmd.reserved1);
  604. if (udata->inlen < required_cmd_sz) {
  605. pr_debug("invalid inlen\n");
  606. return -EINVAL;
  607. }
  608. if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
  609. pr_debug("copy failed\n");
  610. return -EFAULT;
  611. }
  612. if (memchr_inv(ucmd.reserved, 0, sizeof(ucmd.reserved)))
  613. return -EOPNOTSUPP;
  614. if (ucmd.comp_mask || ucmd.reserved1)
  615. return -EOPNOTSUPP;
  616. if (udata->inlen > sizeof(ucmd) &&
  617. !ib_is_udata_cleared(udata, sizeof(ucmd),
  618. udata->inlen - sizeof(ucmd))) {
  619. pr_debug("inlen is not supported\n");
  620. return -EOPNOTSUPP;
  621. }
  622. if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
  623. pr_debug("RSS QP with unsupported QP type %d\n",
  624. init_attr->qp_type);
  625. return -EOPNOTSUPP;
  626. }
  627. if (init_attr->create_flags) {
  628. pr_debug("RSS QP doesn't support create flags\n");
  629. return -EOPNOTSUPP;
  630. }
  631. if (init_attr->send_cq || init_attr->cap.max_send_wr) {
  632. pr_debug("RSS QP with unsupported send attributes\n");
  633. return -EOPNOTSUPP;
  634. }
  635. qp->pri.vid = 0xFFFF;
  636. qp->alt.vid = 0xFFFF;
  637. err = create_qp_rss(to_mdev(pd->device), init_attr, &ucmd, qp);
  638. if (err)
  639. return err;
  640. qp->ibqp.qp_num = qp->mqp.qpn;
  641. return 0;
  642. }
  643. /*
  644. * This function allocates a WQN from a range which is consecutive and aligned
  645. * to its size. In case the range is full, then it creates a new range and
  646. * allocates WQN from it. The new range will be used for following allocations.
  647. */
  648. static int mlx4_ib_alloc_wqn(struct mlx4_ib_ucontext *context,
  649. struct mlx4_ib_qp *qp, int range_size, int *wqn)
  650. {
  651. struct mlx4_ib_dev *dev = to_mdev(context->ibucontext.device);
  652. struct mlx4_wqn_range *range;
  653. int err = 0;
  654. mutex_lock(&context->wqn_ranges_mutex);
  655. range = list_first_entry_or_null(&context->wqn_ranges_list,
  656. struct mlx4_wqn_range, list);
  657. if (!range || (range->refcount == range->size) || range->dirty) {
  658. range = kzalloc(sizeof(*range), GFP_KERNEL);
  659. if (!range) {
  660. err = -ENOMEM;
  661. goto out;
  662. }
  663. err = mlx4_qp_reserve_range(dev->dev, range_size,
  664. range_size, &range->base_wqn, 0,
  665. qp->mqp.usage);
  666. if (err) {
  667. kfree(range);
  668. goto out;
  669. }
  670. range->size = range_size;
  671. list_add(&range->list, &context->wqn_ranges_list);
  672. } else if (range_size != 1) {
  673. /*
  674. * Requesting a new range (>1) when last range is still open, is
  675. * not valid.
  676. */
  677. err = -EINVAL;
  678. goto out;
  679. }
  680. qp->wqn_range = range;
  681. *wqn = range->base_wqn + range->refcount;
  682. range->refcount++;
  683. out:
  684. mutex_unlock(&context->wqn_ranges_mutex);
  685. return err;
  686. }
  687. static void mlx4_ib_release_wqn(struct mlx4_ib_ucontext *context,
  688. struct mlx4_ib_qp *qp, bool dirty_release)
  689. {
  690. struct mlx4_ib_dev *dev = to_mdev(context->ibucontext.device);
  691. struct mlx4_wqn_range *range;
  692. mutex_lock(&context->wqn_ranges_mutex);
  693. range = qp->wqn_range;
  694. range->refcount--;
  695. if (!range->refcount) {
  696. mlx4_qp_release_range(dev->dev, range->base_wqn,
  697. range->size);
  698. list_del(&range->list);
  699. kfree(range);
  700. } else if (dirty_release) {
  701. /*
  702. * A range which one of its WQNs is destroyed, won't be able to be
  703. * reused for further WQN allocations.
  704. * The next created WQ will allocate a new range.
  705. */
  706. range->dirty = true;
  707. }
  708. mutex_unlock(&context->wqn_ranges_mutex);
  709. }
  710. static int create_rq(struct ib_pd *pd, struct ib_qp_init_attr *init_attr,
  711. struct ib_udata *udata, struct mlx4_ib_qp *qp)
  712. {
  713. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  714. int qpn;
  715. int err;
  716. struct mlx4_ib_ucontext *context = rdma_udata_to_drv_context(
  717. udata, struct mlx4_ib_ucontext, ibucontext);
  718. struct mlx4_ib_cq *mcq;
  719. unsigned long flags;
  720. int range_size;
  721. struct mlx4_ib_create_wq wq;
  722. size_t copy_len;
  723. int shift;
  724. int n;
  725. qp->mlx4_ib_qp_type = MLX4_IB_QPT_RAW_PACKET;
  726. spin_lock_init(&qp->sq.lock);
  727. spin_lock_init(&qp->rq.lock);
  728. INIT_LIST_HEAD(&qp->gid_list);
  729. INIT_LIST_HEAD(&qp->steering_rules);
  730. qp->state = IB_QPS_RESET;
  731. copy_len = min(sizeof(struct mlx4_ib_create_wq), udata->inlen);
  732. if (ib_copy_from_udata(&wq, udata, copy_len)) {
  733. err = -EFAULT;
  734. goto err;
  735. }
  736. if (wq.comp_mask || wq.reserved[0] || wq.reserved[1] ||
  737. wq.reserved[2]) {
  738. pr_debug("user command isn't supported\n");
  739. err = -EOPNOTSUPP;
  740. goto err;
  741. }
  742. if (wq.log_range_size > ilog2(dev->dev->caps.max_rss_tbl_sz)) {
  743. pr_debug("WQN range size must be equal or smaller than %d\n",
  744. dev->dev->caps.max_rss_tbl_sz);
  745. err = -EOPNOTSUPP;
  746. goto err;
  747. }
  748. range_size = 1 << wq.log_range_size;
  749. if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS)
  750. qp->flags |= MLX4_IB_QP_SCATTER_FCS;
  751. err = set_rq_size(dev, &init_attr->cap, true, true, qp, qp->inl_recv_sz);
  752. if (err)
  753. goto err;
  754. qp->sq_no_prefetch = 1;
  755. qp->sq.wqe_cnt = 1;
  756. qp->sq.wqe_shift = MLX4_IB_MIN_SQ_STRIDE;
  757. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  758. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  759. qp->umem = ib_umem_get(pd->device, wq.buf_addr, qp->buf_size, 0);
  760. if (IS_ERR(qp->umem)) {
  761. err = PTR_ERR(qp->umem);
  762. goto err;
  763. }
  764. shift = mlx4_ib_umem_calc_optimal_mtt_size(qp->umem, 0, &n);
  765. err = mlx4_mtt_init(dev->dev, n, shift, &qp->mtt);
  766. if (err)
  767. goto err_buf;
  768. err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
  769. if (err)
  770. goto err_mtt;
  771. err = mlx4_ib_db_map_user(udata, wq.db_addr, &qp->db);
  772. if (err)
  773. goto err_mtt;
  774. qp->mqp.usage = MLX4_RES_USAGE_USER_VERBS;
  775. err = mlx4_ib_alloc_wqn(context, qp, range_size, &qpn);
  776. if (err)
  777. goto err_wrid;
  778. err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp);
  779. if (err)
  780. goto err_qpn;
  781. /*
  782. * Hardware wants QPN written in big-endian order (after
  783. * shifting) for send doorbell. Precompute this value to save
  784. * a little bit when posting sends.
  785. */
  786. qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
  787. qp->mqp.event = mlx4_ib_wq_event;
  788. spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
  789. mlx4_ib_lock_cqs(to_mcq(init_attr->send_cq),
  790. to_mcq(init_attr->recv_cq));
  791. /* Maintain device to QPs access, needed for further handling
  792. * via reset flow
  793. */
  794. list_add_tail(&qp->qps_list, &dev->qp_list);
  795. /* Maintain CQ to QPs access, needed for further handling
  796. * via reset flow
  797. */
  798. mcq = to_mcq(init_attr->send_cq);
  799. list_add_tail(&qp->cq_send_list, &mcq->send_qp_list);
  800. mcq = to_mcq(init_attr->recv_cq);
  801. list_add_tail(&qp->cq_recv_list, &mcq->recv_qp_list);
  802. mlx4_ib_unlock_cqs(to_mcq(init_attr->send_cq),
  803. to_mcq(init_attr->recv_cq));
  804. spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
  805. return 0;
  806. err_qpn:
  807. mlx4_ib_release_wqn(context, qp, 0);
  808. err_wrid:
  809. mlx4_ib_db_unmap_user(context, &qp->db);
  810. err_mtt:
  811. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  812. err_buf:
  813. ib_umem_release(qp->umem);
  814. err:
  815. return err;
  816. }
  817. static int create_qp_common(struct ib_pd *pd, struct ib_qp_init_attr *init_attr,
  818. struct ib_udata *udata, int sqpn,
  819. struct mlx4_ib_qp *qp)
  820. {
  821. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  822. int qpn;
  823. int err;
  824. struct mlx4_ib_ucontext *context = rdma_udata_to_drv_context(
  825. udata, struct mlx4_ib_ucontext, ibucontext);
  826. enum mlx4_ib_qp_type qp_type = (enum mlx4_ib_qp_type) init_attr->qp_type;
  827. struct mlx4_ib_cq *mcq;
  828. unsigned long flags;
  829. /* When tunneling special qps, we use a plain UD qp */
  830. if (sqpn) {
  831. if (mlx4_is_mfunc(dev->dev) &&
  832. (!mlx4_is_master(dev->dev) ||
  833. !(init_attr->create_flags & MLX4_IB_SRIOV_SQP))) {
  834. if (init_attr->qp_type == IB_QPT_GSI)
  835. qp_type = MLX4_IB_QPT_PROXY_GSI;
  836. else {
  837. if (mlx4_is_master(dev->dev) ||
  838. qp0_enabled_vf(dev->dev, sqpn))
  839. qp_type = MLX4_IB_QPT_PROXY_SMI_OWNER;
  840. else
  841. qp_type = MLX4_IB_QPT_PROXY_SMI;
  842. }
  843. }
  844. qpn = sqpn;
  845. /* add extra sg entry for tunneling */
  846. init_attr->cap.max_recv_sge++;
  847. } else if (init_attr->create_flags & MLX4_IB_SRIOV_TUNNEL_QP) {
  848. struct mlx4_ib_qp_tunnel_init_attr *tnl_init =
  849. container_of(init_attr,
  850. struct mlx4_ib_qp_tunnel_init_attr, init_attr);
  851. if ((tnl_init->proxy_qp_type != IB_QPT_SMI &&
  852. tnl_init->proxy_qp_type != IB_QPT_GSI) ||
  853. !mlx4_is_master(dev->dev))
  854. return -EINVAL;
  855. if (tnl_init->proxy_qp_type == IB_QPT_GSI)
  856. qp_type = MLX4_IB_QPT_TUN_GSI;
  857. else if (tnl_init->slave == mlx4_master_func_num(dev->dev) ||
  858. mlx4_vf_smi_enabled(dev->dev, tnl_init->slave,
  859. tnl_init->port))
  860. qp_type = MLX4_IB_QPT_TUN_SMI_OWNER;
  861. else
  862. qp_type = MLX4_IB_QPT_TUN_SMI;
  863. /* we are definitely in the PPF here, since we are creating
  864. * tunnel QPs. base_tunnel_sqpn is therefore valid. */
  865. qpn = dev->dev->phys_caps.base_tunnel_sqpn + 8 * tnl_init->slave
  866. + tnl_init->proxy_qp_type * 2 + tnl_init->port - 1;
  867. sqpn = qpn;
  868. }
  869. if (init_attr->qp_type == IB_QPT_SMI ||
  870. init_attr->qp_type == IB_QPT_GSI || qp_type == MLX4_IB_QPT_SMI ||
  871. qp_type == MLX4_IB_QPT_GSI ||
  872. (qp_type & (MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_SMI_OWNER |
  873. MLX4_IB_QPT_PROXY_GSI | MLX4_IB_QPT_TUN_SMI_OWNER))) {
  874. qp->sqp = kzalloc(sizeof(struct mlx4_ib_sqp), GFP_KERNEL);
  875. if (!qp->sqp)
  876. return -ENOMEM;
  877. }
  878. qp->mlx4_ib_qp_type = qp_type;
  879. spin_lock_init(&qp->sq.lock);
  880. spin_lock_init(&qp->rq.lock);
  881. INIT_LIST_HEAD(&qp->gid_list);
  882. INIT_LIST_HEAD(&qp->steering_rules);
  883. qp->state = IB_QPS_RESET;
  884. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  885. qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  886. if (udata) {
  887. struct mlx4_ib_create_qp ucmd;
  888. size_t copy_len;
  889. int shift;
  890. int n;
  891. copy_len = sizeof(struct mlx4_ib_create_qp);
  892. if (ib_copy_from_udata(&ucmd, udata, copy_len)) {
  893. err = -EFAULT;
  894. goto err;
  895. }
  896. qp->inl_recv_sz = ucmd.inl_recv_sz;
  897. if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
  898. if (!(dev->dev->caps.flags &
  899. MLX4_DEV_CAP_FLAG_FCS_KEEP)) {
  900. pr_debug("scatter FCS is unsupported\n");
  901. err = -EOPNOTSUPP;
  902. goto err;
  903. }
  904. qp->flags |= MLX4_IB_QP_SCATTER_FCS;
  905. }
  906. err = set_rq_size(dev, &init_attr->cap, udata,
  907. qp_has_rq(init_attr), qp, qp->inl_recv_sz);
  908. if (err)
  909. goto err;
  910. qp->sq_no_prefetch = ucmd.sq_no_prefetch;
  911. err = set_user_sq_size(dev, qp, &ucmd);
  912. if (err)
  913. goto err;
  914. qp->umem =
  915. ib_umem_get(pd->device, ucmd.buf_addr, qp->buf_size, 0);
  916. if (IS_ERR(qp->umem)) {
  917. err = PTR_ERR(qp->umem);
  918. goto err;
  919. }
  920. shift = mlx4_ib_umem_calc_optimal_mtt_size(qp->umem, 0, &n);
  921. err = mlx4_mtt_init(dev->dev, n, shift, &qp->mtt);
  922. if (err)
  923. goto err_buf;
  924. err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
  925. if (err)
  926. goto err_mtt;
  927. if (qp_has_rq(init_attr)) {
  928. err = mlx4_ib_db_map_user(udata, ucmd.db_addr, &qp->db);
  929. if (err)
  930. goto err_mtt;
  931. }
  932. qp->mqp.usage = MLX4_RES_USAGE_USER_VERBS;
  933. } else {
  934. err = set_rq_size(dev, &init_attr->cap, udata,
  935. qp_has_rq(init_attr), qp, 0);
  936. if (err)
  937. goto err;
  938. qp->sq_no_prefetch = 0;
  939. if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
  940. qp->flags |= MLX4_IB_QP_LSO;
  941. if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) {
  942. if (dev->steering_support ==
  943. MLX4_STEERING_MODE_DEVICE_MANAGED)
  944. qp->flags |= MLX4_IB_QP_NETIF;
  945. else {
  946. err = -EINVAL;
  947. goto err;
  948. }
  949. }
  950. err = set_kernel_sq_size(dev, &init_attr->cap, qp_type, qp);
  951. if (err)
  952. goto err;
  953. if (qp_has_rq(init_attr)) {
  954. err = mlx4_db_alloc(dev->dev, &qp->db, 0);
  955. if (err)
  956. goto err;
  957. *qp->db.db = 0;
  958. }
  959. if (mlx4_buf_alloc(dev->dev, qp->buf_size, PAGE_SIZE * 2,
  960. &qp->buf)) {
  961. err = -ENOMEM;
  962. goto err_db;
  963. }
  964. err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift,
  965. &qp->mtt);
  966. if (err)
  967. goto err_buf;
  968. err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf);
  969. if (err)
  970. goto err_mtt;
  971. qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
  972. sizeof(u64), GFP_KERNEL);
  973. qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
  974. sizeof(u64), GFP_KERNEL);
  975. if (!qp->sq.wrid || !qp->rq.wrid) {
  976. err = -ENOMEM;
  977. goto err_wrid;
  978. }
  979. qp->mqp.usage = MLX4_RES_USAGE_DRIVER;
  980. }
  981. if (sqpn) {
  982. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  983. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
  984. if (alloc_proxy_bufs(pd->device, qp)) {
  985. err = -ENOMEM;
  986. goto err_wrid;
  987. }
  988. }
  989. } else {
  990. /* Raw packet QPNs may not have bits 6,7 set in their qp_num;
  991. * otherwise, the WQE BlueFlame setup flow wrongly causes
  992. * VLAN insertion. */
  993. if (init_attr->qp_type == IB_QPT_RAW_PACKET)
  994. err = mlx4_qp_reserve_range(dev->dev, 1, 1, &qpn,
  995. (init_attr->cap.max_send_wr ?
  996. MLX4_RESERVE_ETH_BF_QP : 0) |
  997. (init_attr->cap.max_recv_wr ?
  998. MLX4_RESERVE_A0_QP : 0),
  999. qp->mqp.usage);
  1000. else
  1001. if (qp->flags & MLX4_IB_QP_NETIF)
  1002. err = mlx4_ib_steer_qp_alloc(dev, 1, &qpn);
  1003. else
  1004. err = mlx4_qp_reserve_range(dev->dev, 1, 1,
  1005. &qpn, 0, qp->mqp.usage);
  1006. if (err)
  1007. goto err_proxy;
  1008. }
  1009. if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
  1010. qp->flags |= MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
  1011. err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp);
  1012. if (err)
  1013. goto err_qpn;
  1014. if (init_attr->qp_type == IB_QPT_XRC_TGT)
  1015. qp->mqp.qpn |= (1 << 23);
  1016. /*
  1017. * Hardware wants QPN written in big-endian order (after
  1018. * shifting) for send doorbell. Precompute this value to save
  1019. * a little bit when posting sends.
  1020. */
  1021. qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
  1022. qp->mqp.event = mlx4_ib_qp_event;
  1023. spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
  1024. mlx4_ib_lock_cqs(to_mcq(init_attr->send_cq),
  1025. to_mcq(init_attr->recv_cq));
  1026. /* Maintain device to QPs access, needed for further handling
  1027. * via reset flow
  1028. */
  1029. list_add_tail(&qp->qps_list, &dev->qp_list);
  1030. /* Maintain CQ to QPs access, needed for further handling
  1031. * via reset flow
  1032. */
  1033. mcq = to_mcq(init_attr->send_cq);
  1034. list_add_tail(&qp->cq_send_list, &mcq->send_qp_list);
  1035. mcq = to_mcq(init_attr->recv_cq);
  1036. list_add_tail(&qp->cq_recv_list, &mcq->recv_qp_list);
  1037. mlx4_ib_unlock_cqs(to_mcq(init_attr->send_cq),
  1038. to_mcq(init_attr->recv_cq));
  1039. spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
  1040. return 0;
  1041. err_qpn:
  1042. if (!sqpn) {
  1043. if (qp->flags & MLX4_IB_QP_NETIF)
  1044. mlx4_ib_steer_qp_free(dev, qpn, 1);
  1045. else
  1046. mlx4_qp_release_range(dev->dev, qpn, 1);
  1047. }
  1048. err_proxy:
  1049. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI)
  1050. free_proxy_bufs(pd->device, qp);
  1051. err_wrid:
  1052. if (udata) {
  1053. if (qp_has_rq(init_attr))
  1054. mlx4_ib_db_unmap_user(context, &qp->db);
  1055. } else {
  1056. kvfree(qp->sq.wrid);
  1057. kvfree(qp->rq.wrid);
  1058. }
  1059. err_mtt:
  1060. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  1061. err_buf:
  1062. if (!qp->umem)
  1063. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  1064. ib_umem_release(qp->umem);
  1065. err_db:
  1066. if (!udata && qp_has_rq(init_attr))
  1067. mlx4_db_free(dev->dev, &qp->db);
  1068. err:
  1069. kfree(qp->sqp);
  1070. return err;
  1071. }
  1072. static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
  1073. {
  1074. switch (state) {
  1075. case IB_QPS_RESET: return MLX4_QP_STATE_RST;
  1076. case IB_QPS_INIT: return MLX4_QP_STATE_INIT;
  1077. case IB_QPS_RTR: return MLX4_QP_STATE_RTR;
  1078. case IB_QPS_RTS: return MLX4_QP_STATE_RTS;
  1079. case IB_QPS_SQD: return MLX4_QP_STATE_SQD;
  1080. case IB_QPS_SQE: return MLX4_QP_STATE_SQER;
  1081. case IB_QPS_ERR: return MLX4_QP_STATE_ERR;
  1082. default: return -1;
  1083. }
  1084. }
  1085. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  1086. __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
  1087. {
  1088. if (send_cq == recv_cq) {
  1089. spin_lock(&send_cq->lock);
  1090. __acquire(&recv_cq->lock);
  1091. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  1092. spin_lock(&send_cq->lock);
  1093. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  1094. } else {
  1095. spin_lock(&recv_cq->lock);
  1096. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  1097. }
  1098. }
  1099. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  1100. __releases(&send_cq->lock) __releases(&recv_cq->lock)
  1101. {
  1102. if (send_cq == recv_cq) {
  1103. __release(&recv_cq->lock);
  1104. spin_unlock(&send_cq->lock);
  1105. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  1106. spin_unlock(&recv_cq->lock);
  1107. spin_unlock(&send_cq->lock);
  1108. } else {
  1109. spin_unlock(&send_cq->lock);
  1110. spin_unlock(&recv_cq->lock);
  1111. }
  1112. }
  1113. static void del_gid_entries(struct mlx4_ib_qp *qp)
  1114. {
  1115. struct mlx4_ib_gid_entry *ge, *tmp;
  1116. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  1117. list_del(&ge->list);
  1118. kfree(ge);
  1119. }
  1120. }
  1121. static struct mlx4_ib_pd *get_pd(struct mlx4_ib_qp *qp)
  1122. {
  1123. if (qp->ibqp.qp_type == IB_QPT_XRC_TGT)
  1124. return to_mpd(to_mxrcd(qp->ibqp.xrcd)->pd);
  1125. else
  1126. return to_mpd(qp->ibqp.pd);
  1127. }
  1128. static void get_cqs(struct mlx4_ib_qp *qp, enum mlx4_ib_source_type src,
  1129. struct mlx4_ib_cq **send_cq, struct mlx4_ib_cq **recv_cq)
  1130. {
  1131. switch (qp->ibqp.qp_type) {
  1132. case IB_QPT_XRC_TGT:
  1133. *send_cq = to_mcq(to_mxrcd(qp->ibqp.xrcd)->cq);
  1134. *recv_cq = *send_cq;
  1135. break;
  1136. case IB_QPT_XRC_INI:
  1137. *send_cq = to_mcq(qp->ibqp.send_cq);
  1138. *recv_cq = *send_cq;
  1139. break;
  1140. default:
  1141. *recv_cq = (src == MLX4_IB_QP_SRC) ? to_mcq(qp->ibqp.recv_cq) :
  1142. to_mcq(qp->ibwq.cq);
  1143. *send_cq = (src == MLX4_IB_QP_SRC) ? to_mcq(qp->ibqp.send_cq) :
  1144. *recv_cq;
  1145. break;
  1146. }
  1147. }
  1148. static void destroy_qp_rss(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  1149. {
  1150. if (qp->state != IB_QPS_RESET) {
  1151. int i;
  1152. for (i = 0; i < (1 << qp->ibqp.rwq_ind_tbl->log_ind_tbl_size);
  1153. i++) {
  1154. struct ib_wq *ibwq = qp->ibqp.rwq_ind_tbl->ind_tbl[i];
  1155. struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq);
  1156. mutex_lock(&wq->mutex);
  1157. wq->rss_usecnt--;
  1158. mutex_unlock(&wq->mutex);
  1159. }
  1160. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  1161. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  1162. pr_warn("modify QP %06x to RESET failed.\n",
  1163. qp->mqp.qpn);
  1164. }
  1165. mlx4_qp_remove(dev->dev, &qp->mqp);
  1166. mlx4_qp_free(dev->dev, &qp->mqp);
  1167. mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1);
  1168. del_gid_entries(qp);
  1169. }
  1170. static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
  1171. enum mlx4_ib_source_type src,
  1172. struct ib_udata *udata)
  1173. {
  1174. struct mlx4_ib_cq *send_cq, *recv_cq;
  1175. unsigned long flags;
  1176. if (qp->state != IB_QPS_RESET) {
  1177. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  1178. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  1179. pr_warn("modify QP %06x to RESET failed.\n",
  1180. qp->mqp.qpn);
  1181. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) {
  1182. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  1183. qp->pri.smac = 0;
  1184. qp->pri.smac_port = 0;
  1185. }
  1186. if (qp->alt.smac) {
  1187. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  1188. qp->alt.smac = 0;
  1189. }
  1190. if (qp->pri.vid < 0x1000) {
  1191. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, qp->pri.vid);
  1192. qp->pri.vid = 0xFFFF;
  1193. qp->pri.candidate_vid = 0xFFFF;
  1194. qp->pri.update_vid = 0;
  1195. }
  1196. if (qp->alt.vid < 0x1000) {
  1197. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, qp->alt.vid);
  1198. qp->alt.vid = 0xFFFF;
  1199. qp->alt.candidate_vid = 0xFFFF;
  1200. qp->alt.update_vid = 0;
  1201. }
  1202. }
  1203. get_cqs(qp, src, &send_cq, &recv_cq);
  1204. spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
  1205. mlx4_ib_lock_cqs(send_cq, recv_cq);
  1206. /* del from lists under both locks above to protect reset flow paths */
  1207. list_del(&qp->qps_list);
  1208. list_del(&qp->cq_send_list);
  1209. list_del(&qp->cq_recv_list);
  1210. if (!udata) {
  1211. __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  1212. qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
  1213. if (send_cq != recv_cq)
  1214. __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  1215. }
  1216. mlx4_qp_remove(dev->dev, &qp->mqp);
  1217. mlx4_ib_unlock_cqs(send_cq, recv_cq);
  1218. spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
  1219. mlx4_qp_free(dev->dev, &qp->mqp);
  1220. if (!is_sqp(dev, qp) && !is_tunnel_qp(dev, qp)) {
  1221. if (qp->flags & MLX4_IB_QP_NETIF)
  1222. mlx4_ib_steer_qp_free(dev, qp->mqp.qpn, 1);
  1223. else if (src == MLX4_IB_RWQ_SRC)
  1224. mlx4_ib_release_wqn(
  1225. rdma_udata_to_drv_context(
  1226. udata,
  1227. struct mlx4_ib_ucontext,
  1228. ibucontext),
  1229. qp, 1);
  1230. else
  1231. mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1);
  1232. }
  1233. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  1234. if (udata) {
  1235. if (qp->rq.wqe_cnt) {
  1236. struct mlx4_ib_ucontext *mcontext =
  1237. rdma_udata_to_drv_context(
  1238. udata,
  1239. struct mlx4_ib_ucontext,
  1240. ibucontext);
  1241. mlx4_ib_db_unmap_user(mcontext, &qp->db);
  1242. }
  1243. } else {
  1244. kvfree(qp->sq.wrid);
  1245. kvfree(qp->rq.wrid);
  1246. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  1247. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI))
  1248. free_proxy_bufs(&dev->ib_dev, qp);
  1249. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  1250. if (qp->rq.wqe_cnt)
  1251. mlx4_db_free(dev->dev, &qp->db);
  1252. }
  1253. ib_umem_release(qp->umem);
  1254. del_gid_entries(qp);
  1255. }
  1256. static u32 get_sqp_num(struct mlx4_ib_dev *dev, struct ib_qp_init_attr *attr)
  1257. {
  1258. /* Native or PPF */
  1259. if (!mlx4_is_mfunc(dev->dev) ||
  1260. (mlx4_is_master(dev->dev) &&
  1261. attr->create_flags & MLX4_IB_SRIOV_SQP)) {
  1262. return dev->dev->phys_caps.base_sqpn +
  1263. (attr->qp_type == IB_QPT_SMI ? 0 : 2) +
  1264. attr->port_num - 1;
  1265. }
  1266. /* PF or VF -- creating proxies */
  1267. if (attr->qp_type == IB_QPT_SMI)
  1268. return dev->dev->caps.spec_qps[attr->port_num - 1].qp0_proxy;
  1269. else
  1270. return dev->dev->caps.spec_qps[attr->port_num - 1].qp1_proxy;
  1271. }
  1272. static int _mlx4_ib_create_qp(struct ib_pd *pd, struct mlx4_ib_qp *qp,
  1273. struct ib_qp_init_attr *init_attr,
  1274. struct ib_udata *udata)
  1275. {
  1276. int err;
  1277. int sup_u_create_flags = MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
  1278. u16 xrcdn = 0;
  1279. if (init_attr->rwq_ind_tbl)
  1280. return _mlx4_ib_create_qp_rss(pd, qp, init_attr, udata);
  1281. /*
  1282. * We only support LSO, vendor flag1, and multicast loopback blocking,
  1283. * and only for kernel UD QPs.
  1284. */
  1285. if (init_attr->create_flags & ~(MLX4_IB_QP_LSO |
  1286. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK |
  1287. MLX4_IB_SRIOV_TUNNEL_QP |
  1288. MLX4_IB_SRIOV_SQP |
  1289. MLX4_IB_QP_NETIF |
  1290. MLX4_IB_QP_CREATE_ROCE_V2_GSI))
  1291. return -EOPNOTSUPP;
  1292. if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) {
  1293. if (init_attr->qp_type != IB_QPT_UD)
  1294. return -EINVAL;
  1295. }
  1296. if (init_attr->create_flags) {
  1297. if (udata && init_attr->create_flags & ~(sup_u_create_flags))
  1298. return -EINVAL;
  1299. if ((init_attr->create_flags & ~(MLX4_IB_SRIOV_SQP |
  1300. MLX4_IB_QP_CREATE_ROCE_V2_GSI |
  1301. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) &&
  1302. init_attr->qp_type != IB_QPT_UD) ||
  1303. (init_attr->create_flags & MLX4_IB_SRIOV_SQP &&
  1304. init_attr->qp_type > IB_QPT_GSI) ||
  1305. (init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI &&
  1306. init_attr->qp_type != IB_QPT_GSI))
  1307. return -EINVAL;
  1308. }
  1309. switch (init_attr->qp_type) {
  1310. case IB_QPT_XRC_TGT:
  1311. pd = to_mxrcd(init_attr->xrcd)->pd;
  1312. xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
  1313. init_attr->send_cq = to_mxrcd(init_attr->xrcd)->cq;
  1314. fallthrough;
  1315. case IB_QPT_XRC_INI:
  1316. if (!(to_mdev(pd->device)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
  1317. return -ENOSYS;
  1318. init_attr->recv_cq = init_attr->send_cq;
  1319. fallthrough;
  1320. case IB_QPT_RC:
  1321. case IB_QPT_UC:
  1322. case IB_QPT_RAW_PACKET:
  1323. case IB_QPT_UD:
  1324. qp->pri.vid = 0xFFFF;
  1325. qp->alt.vid = 0xFFFF;
  1326. err = create_qp_common(pd, init_attr, udata, 0, qp);
  1327. if (err)
  1328. return err;
  1329. qp->ibqp.qp_num = qp->mqp.qpn;
  1330. qp->xrcdn = xrcdn;
  1331. break;
  1332. case IB_QPT_SMI:
  1333. case IB_QPT_GSI:
  1334. {
  1335. int sqpn;
  1336. if (init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI) {
  1337. int res = mlx4_qp_reserve_range(to_mdev(pd->device)->dev,
  1338. 1, 1, &sqpn, 0,
  1339. MLX4_RES_USAGE_DRIVER);
  1340. if (res)
  1341. return res;
  1342. } else {
  1343. sqpn = get_sqp_num(to_mdev(pd->device), init_attr);
  1344. }
  1345. qp->pri.vid = 0xFFFF;
  1346. qp->alt.vid = 0xFFFF;
  1347. err = create_qp_common(pd, init_attr, udata, sqpn, qp);
  1348. if (err)
  1349. return err;
  1350. if (init_attr->create_flags &
  1351. (MLX4_IB_SRIOV_SQP | MLX4_IB_SRIOV_TUNNEL_QP))
  1352. /* Internal QP created with ib_create_qp */
  1353. rdma_restrack_no_track(&qp->ibqp.res);
  1354. qp->port = init_attr->port_num;
  1355. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 :
  1356. init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI ? sqpn : 1;
  1357. break;
  1358. }
  1359. default:
  1360. /* Don't support raw QPs */
  1361. return -EOPNOTSUPP;
  1362. }
  1363. return 0;
  1364. }
  1365. int mlx4_ib_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init_attr,
  1366. struct ib_udata *udata)
  1367. {
  1368. struct ib_device *device = ibqp->device;
  1369. struct mlx4_ib_dev *dev = to_mdev(device);
  1370. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1371. struct ib_pd *pd = ibqp->pd;
  1372. int ret;
  1373. mutex_init(&qp->mutex);
  1374. ret = _mlx4_ib_create_qp(pd, qp, init_attr, udata);
  1375. if (ret)
  1376. return ret;
  1377. if (init_attr->qp_type == IB_QPT_GSI &&
  1378. !(init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI)) {
  1379. struct mlx4_ib_sqp *sqp = qp->sqp;
  1380. int is_eth = rdma_cap_eth_ah(&dev->ib_dev, init_attr->port_num);
  1381. if (is_eth &&
  1382. dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) {
  1383. init_attr->create_flags |= MLX4_IB_QP_CREATE_ROCE_V2_GSI;
  1384. sqp->roce_v2_gsi = ib_create_qp(pd, init_attr);
  1385. if (IS_ERR(sqp->roce_v2_gsi)) {
  1386. pr_err("Failed to create GSI QP for RoCEv2 (%ld)\n", PTR_ERR(sqp->roce_v2_gsi));
  1387. sqp->roce_v2_gsi = NULL;
  1388. } else {
  1389. to_mqp(sqp->roce_v2_gsi)->flags |=
  1390. MLX4_IB_ROCE_V2_GSI_QP;
  1391. }
  1392. init_attr->create_flags &= ~MLX4_IB_QP_CREATE_ROCE_V2_GSI;
  1393. }
  1394. }
  1395. return 0;
  1396. }
  1397. static int _mlx4_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
  1398. {
  1399. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  1400. struct mlx4_ib_qp *mqp = to_mqp(qp);
  1401. if (is_qp0(dev, mqp))
  1402. mlx4_CLOSE_PORT(dev->dev, mqp->port);
  1403. if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI &&
  1404. dev->qp1_proxy[mqp->port - 1] == mqp) {
  1405. mutex_lock(&dev->qp1_proxy_lock[mqp->port - 1]);
  1406. dev->qp1_proxy[mqp->port - 1] = NULL;
  1407. mutex_unlock(&dev->qp1_proxy_lock[mqp->port - 1]);
  1408. }
  1409. if (mqp->counter_index)
  1410. mlx4_ib_free_qp_counter(dev, mqp);
  1411. if (qp->rwq_ind_tbl) {
  1412. destroy_qp_rss(dev, mqp);
  1413. } else {
  1414. destroy_qp_common(dev, mqp, MLX4_IB_QP_SRC, udata);
  1415. }
  1416. kfree(mqp->sqp);
  1417. return 0;
  1418. }
  1419. int mlx4_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
  1420. {
  1421. struct mlx4_ib_qp *mqp = to_mqp(qp);
  1422. if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  1423. struct mlx4_ib_sqp *sqp = mqp->sqp;
  1424. if (sqp->roce_v2_gsi)
  1425. ib_destroy_qp(sqp->roce_v2_gsi);
  1426. }
  1427. return _mlx4_ib_destroy_qp(qp, udata);
  1428. }
  1429. static int to_mlx4_st(struct mlx4_ib_dev *dev, enum mlx4_ib_qp_type type)
  1430. {
  1431. switch (type) {
  1432. case MLX4_IB_QPT_RC: return MLX4_QP_ST_RC;
  1433. case MLX4_IB_QPT_UC: return MLX4_QP_ST_UC;
  1434. case MLX4_IB_QPT_UD: return MLX4_QP_ST_UD;
  1435. case MLX4_IB_QPT_XRC_INI:
  1436. case MLX4_IB_QPT_XRC_TGT: return MLX4_QP_ST_XRC;
  1437. case MLX4_IB_QPT_SMI:
  1438. case MLX4_IB_QPT_GSI:
  1439. case MLX4_IB_QPT_RAW_PACKET: return MLX4_QP_ST_MLX;
  1440. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  1441. case MLX4_IB_QPT_TUN_SMI_OWNER: return (mlx4_is_mfunc(dev->dev) ?
  1442. MLX4_QP_ST_MLX : -1);
  1443. case MLX4_IB_QPT_PROXY_SMI:
  1444. case MLX4_IB_QPT_TUN_SMI:
  1445. case MLX4_IB_QPT_PROXY_GSI:
  1446. case MLX4_IB_QPT_TUN_GSI: return (mlx4_is_mfunc(dev->dev) ?
  1447. MLX4_QP_ST_UD : -1);
  1448. default: return -1;
  1449. }
  1450. }
  1451. static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
  1452. int attr_mask)
  1453. {
  1454. u8 dest_rd_atomic;
  1455. u32 access_flags;
  1456. u32 hw_access_flags = 0;
  1457. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1458. dest_rd_atomic = attr->max_dest_rd_atomic;
  1459. else
  1460. dest_rd_atomic = qp->resp_depth;
  1461. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1462. access_flags = attr->qp_access_flags;
  1463. else
  1464. access_flags = qp->atomic_rd_en;
  1465. if (!dest_rd_atomic)
  1466. access_flags &= IB_ACCESS_REMOTE_WRITE;
  1467. if (access_flags & IB_ACCESS_REMOTE_READ)
  1468. hw_access_flags |= MLX4_QP_BIT_RRE;
  1469. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  1470. hw_access_flags |= MLX4_QP_BIT_RAE;
  1471. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  1472. hw_access_flags |= MLX4_QP_BIT_RWE;
  1473. return cpu_to_be32(hw_access_flags);
  1474. }
  1475. static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
  1476. int attr_mask)
  1477. {
  1478. if (attr_mask & IB_QP_PKEY_INDEX)
  1479. sqp->pkey_index = attr->pkey_index;
  1480. if (attr_mask & IB_QP_QKEY)
  1481. sqp->qkey = attr->qkey;
  1482. if (attr_mask & IB_QP_SQ_PSN)
  1483. sqp->send_psn = attr->sq_psn;
  1484. }
  1485. static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
  1486. {
  1487. path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
  1488. }
  1489. static int _mlx4_set_path(struct mlx4_ib_dev *dev,
  1490. const struct rdma_ah_attr *ah,
  1491. u64 smac, u16 vlan_tag, struct mlx4_qp_path *path,
  1492. struct mlx4_roce_smac_vlan_info *smac_info, u8 port)
  1493. {
  1494. int vidx;
  1495. int smac_index;
  1496. int err;
  1497. path->grh_mylmc = rdma_ah_get_path_bits(ah) & 0x7f;
  1498. path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
  1499. if (rdma_ah_get_static_rate(ah)) {
  1500. path->static_rate = rdma_ah_get_static_rate(ah) +
  1501. MLX4_STAT_RATE_OFFSET;
  1502. while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  1503. !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
  1504. --path->static_rate;
  1505. } else
  1506. path->static_rate = 0;
  1507. if (rdma_ah_get_ah_flags(ah) & IB_AH_GRH) {
  1508. const struct ib_global_route *grh = rdma_ah_read_grh(ah);
  1509. int real_sgid_index =
  1510. mlx4_ib_gid_index_to_real_index(dev, grh->sgid_attr);
  1511. if (real_sgid_index < 0)
  1512. return real_sgid_index;
  1513. if (real_sgid_index >= dev->dev->caps.gid_table_len[port]) {
  1514. pr_err("sgid_index (%u) too large. max is %d\n",
  1515. real_sgid_index, dev->dev->caps.gid_table_len[port] - 1);
  1516. return -1;
  1517. }
  1518. path->grh_mylmc |= 1 << 7;
  1519. path->mgid_index = real_sgid_index;
  1520. path->hop_limit = grh->hop_limit;
  1521. path->tclass_flowlabel =
  1522. cpu_to_be32((grh->traffic_class << 20) |
  1523. (grh->flow_label));
  1524. memcpy(path->rgid, grh->dgid.raw, 16);
  1525. }
  1526. if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
  1527. if (!(rdma_ah_get_ah_flags(ah) & IB_AH_GRH))
  1528. return -1;
  1529. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  1530. ((port - 1) << 6) | ((rdma_ah_get_sl(ah) & 7) << 3);
  1531. path->feup |= MLX4_FEUP_FORCE_ETH_UP;
  1532. if (vlan_tag < 0x1000) {
  1533. if (smac_info->vid < 0x1000) {
  1534. /* both valid vlan ids */
  1535. if (smac_info->vid != vlan_tag) {
  1536. /* different VIDs. unreg old and reg new */
  1537. err = mlx4_register_vlan(dev->dev, port, vlan_tag, &vidx);
  1538. if (err)
  1539. return err;
  1540. smac_info->candidate_vid = vlan_tag;
  1541. smac_info->candidate_vlan_index = vidx;
  1542. smac_info->candidate_vlan_port = port;
  1543. smac_info->update_vid = 1;
  1544. path->vlan_index = vidx;
  1545. } else {
  1546. path->vlan_index = smac_info->vlan_index;
  1547. }
  1548. } else {
  1549. /* no current vlan tag in qp */
  1550. err = mlx4_register_vlan(dev->dev, port, vlan_tag, &vidx);
  1551. if (err)
  1552. return err;
  1553. smac_info->candidate_vid = vlan_tag;
  1554. smac_info->candidate_vlan_index = vidx;
  1555. smac_info->candidate_vlan_port = port;
  1556. smac_info->update_vid = 1;
  1557. path->vlan_index = vidx;
  1558. }
  1559. path->feup |= MLX4_FVL_FORCE_ETH_VLAN;
  1560. path->fl = 1 << 6;
  1561. } else {
  1562. /* have current vlan tag. unregister it at modify-qp success */
  1563. if (smac_info->vid < 0x1000) {
  1564. smac_info->candidate_vid = 0xFFFF;
  1565. smac_info->update_vid = 1;
  1566. }
  1567. }
  1568. /* get smac_index for RoCE use.
  1569. * If no smac was yet assigned, register one.
  1570. * If one was already assigned, but the new mac differs,
  1571. * unregister the old one and register the new one.
  1572. */
  1573. if ((!smac_info->smac && !smac_info->smac_port) ||
  1574. smac_info->smac != smac) {
  1575. /* register candidate now, unreg if needed, after success */
  1576. smac_index = mlx4_register_mac(dev->dev, port, smac);
  1577. if (smac_index >= 0) {
  1578. smac_info->candidate_smac_index = smac_index;
  1579. smac_info->candidate_smac = smac;
  1580. smac_info->candidate_smac_port = port;
  1581. } else {
  1582. return -EINVAL;
  1583. }
  1584. } else {
  1585. smac_index = smac_info->smac_index;
  1586. }
  1587. memcpy(path->dmac, ah->roce.dmac, 6);
  1588. path->ackto = MLX4_IB_LINK_TYPE_ETH;
  1589. /* put MAC table smac index for IBoE */
  1590. path->grh_mylmc = (u8) (smac_index) | 0x80;
  1591. } else {
  1592. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  1593. ((port - 1) << 6) | ((rdma_ah_get_sl(ah) & 0xf) << 2);
  1594. }
  1595. return 0;
  1596. }
  1597. static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_qp_attr *qp,
  1598. enum ib_qp_attr_mask qp_attr_mask,
  1599. struct mlx4_ib_qp *mqp,
  1600. struct mlx4_qp_path *path, u8 port,
  1601. u16 vlan_id, u8 *smac)
  1602. {
  1603. return _mlx4_set_path(dev, &qp->ah_attr,
  1604. ether_addr_to_u64(smac),
  1605. vlan_id,
  1606. path, &mqp->pri, port);
  1607. }
  1608. static int mlx4_set_alt_path(struct mlx4_ib_dev *dev,
  1609. const struct ib_qp_attr *qp,
  1610. enum ib_qp_attr_mask qp_attr_mask,
  1611. struct mlx4_ib_qp *mqp,
  1612. struct mlx4_qp_path *path, u8 port)
  1613. {
  1614. return _mlx4_set_path(dev, &qp->alt_ah_attr,
  1615. 0,
  1616. 0xffff,
  1617. path, &mqp->alt, port);
  1618. }
  1619. static void update_mcg_macs(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  1620. {
  1621. struct mlx4_ib_gid_entry *ge, *tmp;
  1622. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  1623. if (!ge->added && mlx4_ib_add_mc(dev, qp, &ge->gid)) {
  1624. ge->added = 1;
  1625. ge->port = qp->port;
  1626. }
  1627. }
  1628. }
  1629. static int handle_eth_ud_smac_index(struct mlx4_ib_dev *dev,
  1630. struct mlx4_ib_qp *qp,
  1631. struct mlx4_qp_context *context)
  1632. {
  1633. u64 u64_mac;
  1634. int smac_index;
  1635. u64_mac = atomic64_read(&dev->iboe.mac[qp->port - 1]);
  1636. context->pri_path.sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE | ((qp->port - 1) << 6);
  1637. if (!qp->pri.smac && !qp->pri.smac_port) {
  1638. smac_index = mlx4_register_mac(dev->dev, qp->port, u64_mac);
  1639. if (smac_index >= 0) {
  1640. qp->pri.candidate_smac_index = smac_index;
  1641. qp->pri.candidate_smac = u64_mac;
  1642. qp->pri.candidate_smac_port = qp->port;
  1643. context->pri_path.grh_mylmc = 0x80 | (u8) smac_index;
  1644. } else {
  1645. return -ENOENT;
  1646. }
  1647. }
  1648. return 0;
  1649. }
  1650. static int create_qp_lb_counter(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  1651. {
  1652. struct counter_index *new_counter_index;
  1653. int err;
  1654. u32 tmp_idx;
  1655. if (rdma_port_get_link_layer(&dev->ib_dev, qp->port) !=
  1656. IB_LINK_LAYER_ETHERNET ||
  1657. !(qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) ||
  1658. !(dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_LB_SRC_CHK))
  1659. return 0;
  1660. err = mlx4_counter_alloc(dev->dev, &tmp_idx, MLX4_RES_USAGE_DRIVER);
  1661. if (err)
  1662. return err;
  1663. new_counter_index = kmalloc(sizeof(*new_counter_index), GFP_KERNEL);
  1664. if (!new_counter_index) {
  1665. mlx4_counter_free(dev->dev, tmp_idx);
  1666. return -ENOMEM;
  1667. }
  1668. new_counter_index->index = tmp_idx;
  1669. new_counter_index->allocated = 1;
  1670. qp->counter_index = new_counter_index;
  1671. mutex_lock(&dev->counters_table[qp->port - 1].mutex);
  1672. list_add_tail(&new_counter_index->list,
  1673. &dev->counters_table[qp->port - 1].counters_list);
  1674. mutex_unlock(&dev->counters_table[qp->port - 1].mutex);
  1675. return 0;
  1676. }
  1677. enum {
  1678. MLX4_QPC_ROCE_MODE_1 = 0,
  1679. MLX4_QPC_ROCE_MODE_2 = 2,
  1680. MLX4_QPC_ROCE_MODE_UNDEFINED = 0xff
  1681. };
  1682. static u8 gid_type_to_qpc(enum ib_gid_type gid_type)
  1683. {
  1684. switch (gid_type) {
  1685. case IB_GID_TYPE_ROCE:
  1686. return MLX4_QPC_ROCE_MODE_1;
  1687. case IB_GID_TYPE_ROCE_UDP_ENCAP:
  1688. return MLX4_QPC_ROCE_MODE_2;
  1689. default:
  1690. return MLX4_QPC_ROCE_MODE_UNDEFINED;
  1691. }
  1692. }
  1693. /*
  1694. * Go over all RSS QP's childes (WQs) and apply their HW state according to
  1695. * their logic state if the RSS QP is the first RSS QP associated for the WQ.
  1696. */
  1697. static int bringup_rss_rwqs(struct ib_rwq_ind_table *ind_tbl, u8 port_num,
  1698. struct ib_udata *udata)
  1699. {
  1700. int err = 0;
  1701. int i;
  1702. for (i = 0; i < (1 << ind_tbl->log_ind_tbl_size); i++) {
  1703. struct ib_wq *ibwq = ind_tbl->ind_tbl[i];
  1704. struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq);
  1705. mutex_lock(&wq->mutex);
  1706. /* Mlx4_ib restrictions:
  1707. * WQ's is associated to a port according to the RSS QP it is
  1708. * associates to.
  1709. * In case the WQ is associated to a different port by another
  1710. * RSS QP, return a failure.
  1711. */
  1712. if ((wq->rss_usecnt > 0) && (wq->port != port_num)) {
  1713. err = -EINVAL;
  1714. mutex_unlock(&wq->mutex);
  1715. break;
  1716. }
  1717. wq->port = port_num;
  1718. if ((wq->rss_usecnt == 0) && (ibwq->state == IB_WQS_RDY)) {
  1719. err = _mlx4_ib_modify_wq(ibwq, IB_WQS_RDY, udata);
  1720. if (err) {
  1721. mutex_unlock(&wq->mutex);
  1722. break;
  1723. }
  1724. }
  1725. wq->rss_usecnt++;
  1726. mutex_unlock(&wq->mutex);
  1727. }
  1728. if (i && err) {
  1729. int j;
  1730. for (j = (i - 1); j >= 0; j--) {
  1731. struct ib_wq *ibwq = ind_tbl->ind_tbl[j];
  1732. struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq);
  1733. mutex_lock(&wq->mutex);
  1734. if ((wq->rss_usecnt == 1) &&
  1735. (ibwq->state == IB_WQS_RDY))
  1736. if (_mlx4_ib_modify_wq(ibwq, IB_WQS_RESET,
  1737. udata))
  1738. pr_warn("failed to reverse WQN=0x%06x\n",
  1739. ibwq->wq_num);
  1740. wq->rss_usecnt--;
  1741. mutex_unlock(&wq->mutex);
  1742. }
  1743. }
  1744. return err;
  1745. }
  1746. static void bring_down_rss_rwqs(struct ib_rwq_ind_table *ind_tbl,
  1747. struct ib_udata *udata)
  1748. {
  1749. int i;
  1750. for (i = 0; i < (1 << ind_tbl->log_ind_tbl_size); i++) {
  1751. struct ib_wq *ibwq = ind_tbl->ind_tbl[i];
  1752. struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq);
  1753. mutex_lock(&wq->mutex);
  1754. if ((wq->rss_usecnt == 1) && (ibwq->state == IB_WQS_RDY))
  1755. if (_mlx4_ib_modify_wq(ibwq, IB_WQS_RESET, udata))
  1756. pr_warn("failed to reverse WQN=%x\n",
  1757. ibwq->wq_num);
  1758. wq->rss_usecnt--;
  1759. mutex_unlock(&wq->mutex);
  1760. }
  1761. }
  1762. static void fill_qp_rss_context(struct mlx4_qp_context *context,
  1763. struct mlx4_ib_qp *qp)
  1764. {
  1765. struct mlx4_rss_context *rss_context;
  1766. rss_context = (void *)context + offsetof(struct mlx4_qp_context,
  1767. pri_path) + MLX4_RSS_OFFSET_IN_QPC_PRI_PATH;
  1768. rss_context->base_qpn = cpu_to_be32(qp->rss_ctx->base_qpn_tbl_sz);
  1769. rss_context->default_qpn =
  1770. cpu_to_be32(qp->rss_ctx->base_qpn_tbl_sz & 0xffffff);
  1771. if (qp->rss_ctx->flags & (MLX4_RSS_UDP_IPV4 | MLX4_RSS_UDP_IPV6))
  1772. rss_context->base_qpn_udp = rss_context->default_qpn;
  1773. rss_context->flags = qp->rss_ctx->flags;
  1774. /* Currently support just toeplitz */
  1775. rss_context->hash_fn = MLX4_RSS_HASH_TOP;
  1776. memcpy(rss_context->rss_key, qp->rss_ctx->rss_key,
  1777. MLX4_EN_RSS_KEY_SIZE);
  1778. }
  1779. static int __mlx4_ib_modify_qp(void *src, enum mlx4_ib_source_type src_type,
  1780. const struct ib_qp_attr *attr, int attr_mask,
  1781. enum ib_qp_state cur_state,
  1782. enum ib_qp_state new_state,
  1783. struct ib_udata *udata)
  1784. {
  1785. struct ib_srq *ibsrq;
  1786. const struct ib_gid_attr *gid_attr = NULL;
  1787. struct ib_rwq_ind_table *rwq_ind_tbl;
  1788. enum ib_qp_type qp_type;
  1789. struct mlx4_ib_dev *dev;
  1790. struct mlx4_ib_qp *qp;
  1791. struct mlx4_ib_pd *pd;
  1792. struct mlx4_ib_cq *send_cq, *recv_cq;
  1793. struct mlx4_ib_ucontext *ucontext = rdma_udata_to_drv_context(
  1794. udata, struct mlx4_ib_ucontext, ibucontext);
  1795. struct mlx4_qp_context *context;
  1796. enum mlx4_qp_optpar optpar = 0;
  1797. int sqd_event;
  1798. int steer_qp = 0;
  1799. int err = -EINVAL;
  1800. int counter_index;
  1801. if (src_type == MLX4_IB_RWQ_SRC) {
  1802. struct ib_wq *ibwq;
  1803. ibwq = (struct ib_wq *)src;
  1804. ibsrq = NULL;
  1805. rwq_ind_tbl = NULL;
  1806. qp_type = IB_QPT_RAW_PACKET;
  1807. qp = to_mqp((struct ib_qp *)ibwq);
  1808. dev = to_mdev(ibwq->device);
  1809. pd = to_mpd(ibwq->pd);
  1810. } else {
  1811. struct ib_qp *ibqp;
  1812. ibqp = (struct ib_qp *)src;
  1813. ibsrq = ibqp->srq;
  1814. rwq_ind_tbl = ibqp->rwq_ind_tbl;
  1815. qp_type = ibqp->qp_type;
  1816. qp = to_mqp(ibqp);
  1817. dev = to_mdev(ibqp->device);
  1818. pd = get_pd(qp);
  1819. }
  1820. /* APM is not supported under RoCE */
  1821. if (attr_mask & IB_QP_ALT_PATH &&
  1822. rdma_port_get_link_layer(&dev->ib_dev, qp->port) ==
  1823. IB_LINK_LAYER_ETHERNET)
  1824. return -ENOTSUPP;
  1825. context = kzalloc(sizeof *context, GFP_KERNEL);
  1826. if (!context)
  1827. return -ENOMEM;
  1828. context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
  1829. (to_mlx4_st(dev, qp->mlx4_ib_qp_type) << 16));
  1830. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  1831. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  1832. else {
  1833. optpar |= MLX4_QP_OPTPAR_PM_STATE;
  1834. switch (attr->path_mig_state) {
  1835. case IB_MIG_MIGRATED:
  1836. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  1837. break;
  1838. case IB_MIG_REARM:
  1839. context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
  1840. break;
  1841. case IB_MIG_ARMED:
  1842. context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
  1843. break;
  1844. }
  1845. }
  1846. if (qp->inl_recv_sz)
  1847. context->param3 |= cpu_to_be32(1 << 25);
  1848. if (qp->flags & MLX4_IB_QP_SCATTER_FCS)
  1849. context->param3 |= cpu_to_be32(1 << 29);
  1850. if (qp_type == IB_QPT_GSI || qp_type == IB_QPT_SMI)
  1851. context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
  1852. else if (qp_type == IB_QPT_RAW_PACKET)
  1853. context->mtu_msgmax = (MLX4_RAW_QP_MTU << 5) | MLX4_RAW_QP_MSGMAX;
  1854. else if (qp_type == IB_QPT_UD) {
  1855. if (qp->flags & MLX4_IB_QP_LSO)
  1856. context->mtu_msgmax = (IB_MTU_4096 << 5) |
  1857. ilog2(dev->dev->caps.max_gso_sz);
  1858. else
  1859. context->mtu_msgmax = (IB_MTU_4096 << 5) | 13;
  1860. } else if (attr_mask & IB_QP_PATH_MTU) {
  1861. if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
  1862. pr_err("path MTU (%u) is invalid\n",
  1863. attr->path_mtu);
  1864. goto out;
  1865. }
  1866. context->mtu_msgmax = (attr->path_mtu << 5) |
  1867. ilog2(dev->dev->caps.max_msg_sz);
  1868. }
  1869. if (!rwq_ind_tbl) { /* PRM RSS receive side should be left zeros */
  1870. if (qp->rq.wqe_cnt)
  1871. context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3;
  1872. context->rq_size_stride |= qp->rq.wqe_shift - 4;
  1873. }
  1874. if (qp->sq.wqe_cnt)
  1875. context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3;
  1876. context->sq_size_stride |= qp->sq.wqe_shift - 4;
  1877. if (new_state == IB_QPS_RESET && qp->counter_index)
  1878. mlx4_ib_free_qp_counter(dev, qp);
  1879. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  1880. context->sq_size_stride |= !!qp->sq_no_prefetch << 7;
  1881. context->xrcd = cpu_to_be32((u32) qp->xrcdn);
  1882. if (qp_type == IB_QPT_RAW_PACKET)
  1883. context->param3 |= cpu_to_be32(1 << 30);
  1884. }
  1885. if (ucontext)
  1886. context->usr_page = cpu_to_be32(
  1887. mlx4_to_hw_uar_index(dev->dev, ucontext->uar.index));
  1888. else
  1889. context->usr_page = cpu_to_be32(
  1890. mlx4_to_hw_uar_index(dev->dev, dev->priv_uar.index));
  1891. if (attr_mask & IB_QP_DEST_QPN)
  1892. context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  1893. if (attr_mask & IB_QP_PORT) {
  1894. if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
  1895. !(attr_mask & IB_QP_AV)) {
  1896. mlx4_set_sched(&context->pri_path, attr->port_num);
  1897. optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
  1898. }
  1899. }
  1900. if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
  1901. err = create_qp_lb_counter(dev, qp);
  1902. if (err)
  1903. goto out;
  1904. counter_index =
  1905. dev->counters_table[qp->port - 1].default_counter;
  1906. if (qp->counter_index)
  1907. counter_index = qp->counter_index->index;
  1908. if (counter_index != -1) {
  1909. context->pri_path.counter_index = counter_index;
  1910. optpar |= MLX4_QP_OPTPAR_COUNTER_INDEX;
  1911. if (qp->counter_index) {
  1912. context->pri_path.fl |=
  1913. MLX4_FL_ETH_SRC_CHECK_MC_LB;
  1914. context->pri_path.vlan_control |=
  1915. MLX4_CTRL_ETH_SRC_CHECK_IF_COUNTER;
  1916. }
  1917. } else
  1918. context->pri_path.counter_index =
  1919. MLX4_SINK_COUNTER_INDEX(dev->dev);
  1920. if (qp->flags & MLX4_IB_QP_NETIF) {
  1921. mlx4_ib_steer_qp_reg(dev, qp, 1);
  1922. steer_qp = 1;
  1923. }
  1924. if (qp_type == IB_QPT_GSI) {
  1925. enum ib_gid_type gid_type = qp->flags & MLX4_IB_ROCE_V2_GSI_QP ?
  1926. IB_GID_TYPE_ROCE_UDP_ENCAP : IB_GID_TYPE_ROCE;
  1927. u8 qpc_roce_mode = gid_type_to_qpc(gid_type);
  1928. context->rlkey_roce_mode |= (qpc_roce_mode << 6);
  1929. }
  1930. }
  1931. if (attr_mask & IB_QP_PKEY_INDEX) {
  1932. if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV)
  1933. context->pri_path.disable_pkey_check = 0x40;
  1934. context->pri_path.pkey_index = attr->pkey_index;
  1935. optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
  1936. }
  1937. if (attr_mask & IB_QP_AV) {
  1938. u8 port_num = mlx4_is_bonded(dev->dev) ? 1 :
  1939. attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1940. u16 vlan = 0xffff;
  1941. u8 smac[ETH_ALEN];
  1942. int is_eth =
  1943. rdma_cap_eth_ah(&dev->ib_dev, port_num) &&
  1944. rdma_ah_get_ah_flags(&attr->ah_attr) & IB_AH_GRH;
  1945. if (is_eth) {
  1946. gid_attr = attr->ah_attr.grh.sgid_attr;
  1947. err = rdma_read_gid_l2_fields(gid_attr, &vlan,
  1948. &smac[0]);
  1949. if (err)
  1950. goto out;
  1951. }
  1952. if (mlx4_set_path(dev, attr, attr_mask, qp, &context->pri_path,
  1953. port_num, vlan, smac))
  1954. goto out;
  1955. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  1956. MLX4_QP_OPTPAR_SCHED_QUEUE);
  1957. if (is_eth &&
  1958. (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR)) {
  1959. u8 qpc_roce_mode = gid_type_to_qpc(gid_attr->gid_type);
  1960. if (qpc_roce_mode == MLX4_QPC_ROCE_MODE_UNDEFINED) {
  1961. err = -EINVAL;
  1962. goto out;
  1963. }
  1964. context->rlkey_roce_mode |= (qpc_roce_mode << 6);
  1965. }
  1966. }
  1967. if (attr_mask & IB_QP_TIMEOUT) {
  1968. context->pri_path.ackto |= attr->timeout << 3;
  1969. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  1970. }
  1971. if (attr_mask & IB_QP_ALT_PATH) {
  1972. if (attr->alt_port_num == 0 ||
  1973. attr->alt_port_num > dev->dev->caps.num_ports)
  1974. goto out;
  1975. if (attr->alt_pkey_index >=
  1976. dev->dev->caps.pkey_table_len[attr->alt_port_num])
  1977. goto out;
  1978. if (mlx4_set_alt_path(dev, attr, attr_mask, qp,
  1979. &context->alt_path,
  1980. attr->alt_port_num))
  1981. goto out;
  1982. context->alt_path.pkey_index = attr->alt_pkey_index;
  1983. context->alt_path.ackto = attr->alt_timeout << 3;
  1984. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  1985. }
  1986. context->pd = cpu_to_be32(pd->pdn);
  1987. if (!rwq_ind_tbl) {
  1988. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  1989. get_cqs(qp, src_type, &send_cq, &recv_cq);
  1990. } else { /* Set dummy CQs to be compatible with HV and PRM */
  1991. send_cq = to_mcq(rwq_ind_tbl->ind_tbl[0]->cq);
  1992. recv_cq = send_cq;
  1993. }
  1994. context->cqn_send = cpu_to_be32(send_cq->mcq.cqn);
  1995. context->cqn_recv = cpu_to_be32(recv_cq->mcq.cqn);
  1996. /* Set "fast registration enabled" for all kernel QPs */
  1997. if (!ucontext)
  1998. context->params1 |= cpu_to_be32(1 << 11);
  1999. if (attr_mask & IB_QP_RNR_RETRY) {
  2000. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  2001. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  2002. }
  2003. if (attr_mask & IB_QP_RETRY_CNT) {
  2004. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  2005. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  2006. }
  2007. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  2008. if (attr->max_rd_atomic)
  2009. context->params1 |=
  2010. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  2011. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  2012. }
  2013. if (attr_mask & IB_QP_SQ_PSN)
  2014. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  2015. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  2016. if (attr->max_dest_rd_atomic)
  2017. context->params2 |=
  2018. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  2019. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  2020. }
  2021. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  2022. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  2023. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  2024. }
  2025. if (ibsrq)
  2026. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  2027. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  2028. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  2029. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  2030. }
  2031. if (attr_mask & IB_QP_RQ_PSN)
  2032. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  2033. /* proxy and tunnel qp qkeys will be changed in modify-qp wrappers */
  2034. if (attr_mask & IB_QP_QKEY) {
  2035. if (qp->mlx4_ib_qp_type &
  2036. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))
  2037. context->qkey = cpu_to_be32(IB_QP_SET_QKEY);
  2038. else {
  2039. if (mlx4_is_mfunc(dev->dev) &&
  2040. !(qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV) &&
  2041. (attr->qkey & MLX4_RESERVED_QKEY_MASK) ==
  2042. MLX4_RESERVED_QKEY_BASE) {
  2043. pr_err("Cannot use reserved QKEY"
  2044. " 0x%x (range 0xffff0000..0xffffffff"
  2045. " is reserved)\n", attr->qkey);
  2046. err = -EINVAL;
  2047. goto out;
  2048. }
  2049. context->qkey = cpu_to_be32(attr->qkey);
  2050. }
  2051. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  2052. }
  2053. if (ibsrq)
  2054. context->srqn = cpu_to_be32(1 << 24 |
  2055. to_msrq(ibsrq)->msrq.srqn);
  2056. if (qp->rq.wqe_cnt &&
  2057. cur_state == IB_QPS_RESET &&
  2058. new_state == IB_QPS_INIT)
  2059. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  2060. if (cur_state == IB_QPS_INIT &&
  2061. new_state == IB_QPS_RTR &&
  2062. (qp_type == IB_QPT_GSI || qp_type == IB_QPT_SMI ||
  2063. qp_type == IB_QPT_UD || qp_type == IB_QPT_RAW_PACKET)) {
  2064. context->pri_path.sched_queue = (qp->port - 1) << 6;
  2065. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
  2066. qp->mlx4_ib_qp_type &
  2067. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) {
  2068. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  2069. if (qp->mlx4_ib_qp_type != MLX4_IB_QPT_SMI)
  2070. context->pri_path.fl = 0x80;
  2071. } else {
  2072. if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV)
  2073. context->pri_path.fl = 0x80;
  2074. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  2075. }
  2076. if (rdma_port_get_link_layer(&dev->ib_dev, qp->port) ==
  2077. IB_LINK_LAYER_ETHERNET) {
  2078. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_GSI ||
  2079. qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI)
  2080. context->pri_path.feup = 1 << 7; /* don't fsm */
  2081. /* handle smac_index */
  2082. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_UD ||
  2083. qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI ||
  2084. qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_GSI) {
  2085. err = handle_eth_ud_smac_index(dev, qp, context);
  2086. if (err) {
  2087. err = -EINVAL;
  2088. goto out;
  2089. }
  2090. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI)
  2091. dev->qp1_proxy[qp->port - 1] = qp;
  2092. }
  2093. }
  2094. }
  2095. if (qp_type == IB_QPT_RAW_PACKET) {
  2096. context->pri_path.ackto = (context->pri_path.ackto & 0xf8) |
  2097. MLX4_IB_LINK_TYPE_ETH;
  2098. if (dev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
  2099. /* set QP to receive both tunneled & non-tunneled packets */
  2100. if (!rwq_ind_tbl)
  2101. context->srqn = cpu_to_be32(7 << 28);
  2102. }
  2103. }
  2104. if (qp_type == IB_QPT_UD && (new_state == IB_QPS_RTR)) {
  2105. int is_eth = rdma_port_get_link_layer(
  2106. &dev->ib_dev, qp->port) ==
  2107. IB_LINK_LAYER_ETHERNET;
  2108. if (is_eth) {
  2109. context->pri_path.ackto = MLX4_IB_LINK_TYPE_ETH;
  2110. optpar |= MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH;
  2111. }
  2112. }
  2113. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  2114. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  2115. sqd_event = 1;
  2116. else
  2117. sqd_event = 0;
  2118. if (!ucontext &&
  2119. cur_state == IB_QPS_RESET &&
  2120. new_state == IB_QPS_INIT)
  2121. context->rlkey_roce_mode |= (1 << 4);
  2122. /*
  2123. * Before passing a kernel QP to the HW, make sure that the
  2124. * ownership bits of the send queue are set and the SQ
  2125. * headroom is stamped so that the hardware doesn't start
  2126. * processing stale work requests.
  2127. */
  2128. if (!ucontext &&
  2129. cur_state == IB_QPS_RESET &&
  2130. new_state == IB_QPS_INIT) {
  2131. struct mlx4_wqe_ctrl_seg *ctrl;
  2132. int i;
  2133. for (i = 0; i < qp->sq.wqe_cnt; ++i) {
  2134. ctrl = get_send_wqe(qp, i);
  2135. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  2136. ctrl->qpn_vlan.fence_size =
  2137. 1 << (qp->sq.wqe_shift - 4);
  2138. stamp_send_wqe(qp, i);
  2139. }
  2140. }
  2141. if (rwq_ind_tbl &&
  2142. cur_state == IB_QPS_RESET &&
  2143. new_state == IB_QPS_INIT) {
  2144. fill_qp_rss_context(context, qp);
  2145. context->flags |= cpu_to_be32(1 << MLX4_RSS_QPC_FLAG_OFFSET);
  2146. }
  2147. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  2148. to_mlx4_state(new_state), context, optpar,
  2149. sqd_event, &qp->mqp);
  2150. if (err)
  2151. goto out;
  2152. qp->state = new_state;
  2153. if (attr_mask & IB_QP_ACCESS_FLAGS)
  2154. qp->atomic_rd_en = attr->qp_access_flags;
  2155. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  2156. qp->resp_depth = attr->max_dest_rd_atomic;
  2157. if (attr_mask & IB_QP_PORT) {
  2158. qp->port = attr->port_num;
  2159. update_mcg_macs(dev, qp);
  2160. }
  2161. if (attr_mask & IB_QP_ALT_PATH)
  2162. qp->alt_port = attr->alt_port_num;
  2163. if (is_sqp(dev, qp))
  2164. store_sqp_attrs(qp->sqp, attr, attr_mask);
  2165. /*
  2166. * If we moved QP0 to RTR, bring the IB link up; if we moved
  2167. * QP0 to RESET or ERROR, bring the link back down.
  2168. */
  2169. if (is_qp0(dev, qp)) {
  2170. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  2171. if (mlx4_INIT_PORT(dev->dev, qp->port))
  2172. pr_warn("INIT_PORT failed for port %d\n",
  2173. qp->port);
  2174. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  2175. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  2176. mlx4_CLOSE_PORT(dev->dev, qp->port);
  2177. }
  2178. /*
  2179. * If we moved a kernel QP to RESET, clean up all old CQ
  2180. * entries and reinitialize the QP.
  2181. */
  2182. if (new_state == IB_QPS_RESET) {
  2183. if (!ucontext) {
  2184. mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  2185. ibsrq ? to_msrq(ibsrq) : NULL);
  2186. if (send_cq != recv_cq)
  2187. mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  2188. qp->rq.head = 0;
  2189. qp->rq.tail = 0;
  2190. qp->sq.head = 0;
  2191. qp->sq.tail = 0;
  2192. qp->sq_next_wqe = 0;
  2193. if (qp->rq.wqe_cnt)
  2194. *qp->db.db = 0;
  2195. if (qp->flags & MLX4_IB_QP_NETIF)
  2196. mlx4_ib_steer_qp_reg(dev, qp, 0);
  2197. }
  2198. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) {
  2199. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  2200. qp->pri.smac = 0;
  2201. qp->pri.smac_port = 0;
  2202. }
  2203. if (qp->alt.smac) {
  2204. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  2205. qp->alt.smac = 0;
  2206. }
  2207. if (qp->pri.vid < 0x1000) {
  2208. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, qp->pri.vid);
  2209. qp->pri.vid = 0xFFFF;
  2210. qp->pri.candidate_vid = 0xFFFF;
  2211. qp->pri.update_vid = 0;
  2212. }
  2213. if (qp->alt.vid < 0x1000) {
  2214. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, qp->alt.vid);
  2215. qp->alt.vid = 0xFFFF;
  2216. qp->alt.candidate_vid = 0xFFFF;
  2217. qp->alt.update_vid = 0;
  2218. }
  2219. }
  2220. out:
  2221. if (err && qp->counter_index)
  2222. mlx4_ib_free_qp_counter(dev, qp);
  2223. if (err && steer_qp)
  2224. mlx4_ib_steer_qp_reg(dev, qp, 0);
  2225. kfree(context);
  2226. if (qp->pri.candidate_smac ||
  2227. (!qp->pri.candidate_smac && qp->pri.candidate_smac_port)) {
  2228. if (err) {
  2229. mlx4_unregister_mac(dev->dev, qp->pri.candidate_smac_port, qp->pri.candidate_smac);
  2230. } else {
  2231. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port))
  2232. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  2233. qp->pri.smac = qp->pri.candidate_smac;
  2234. qp->pri.smac_index = qp->pri.candidate_smac_index;
  2235. qp->pri.smac_port = qp->pri.candidate_smac_port;
  2236. }
  2237. qp->pri.candidate_smac = 0;
  2238. qp->pri.candidate_smac_index = 0;
  2239. qp->pri.candidate_smac_port = 0;
  2240. }
  2241. if (qp->alt.candidate_smac) {
  2242. if (err) {
  2243. mlx4_unregister_mac(dev->dev, qp->alt.candidate_smac_port, qp->alt.candidate_smac);
  2244. } else {
  2245. if (qp->alt.smac)
  2246. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  2247. qp->alt.smac = qp->alt.candidate_smac;
  2248. qp->alt.smac_index = qp->alt.candidate_smac_index;
  2249. qp->alt.smac_port = qp->alt.candidate_smac_port;
  2250. }
  2251. qp->alt.candidate_smac = 0;
  2252. qp->alt.candidate_smac_index = 0;
  2253. qp->alt.candidate_smac_port = 0;
  2254. }
  2255. if (qp->pri.update_vid) {
  2256. if (err) {
  2257. if (qp->pri.candidate_vid < 0x1000)
  2258. mlx4_unregister_vlan(dev->dev, qp->pri.candidate_vlan_port,
  2259. qp->pri.candidate_vid);
  2260. } else {
  2261. if (qp->pri.vid < 0x1000)
  2262. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port,
  2263. qp->pri.vid);
  2264. qp->pri.vid = qp->pri.candidate_vid;
  2265. qp->pri.vlan_port = qp->pri.candidate_vlan_port;
  2266. qp->pri.vlan_index = qp->pri.candidate_vlan_index;
  2267. }
  2268. qp->pri.candidate_vid = 0xFFFF;
  2269. qp->pri.update_vid = 0;
  2270. }
  2271. if (qp->alt.update_vid) {
  2272. if (err) {
  2273. if (qp->alt.candidate_vid < 0x1000)
  2274. mlx4_unregister_vlan(dev->dev, qp->alt.candidate_vlan_port,
  2275. qp->alt.candidate_vid);
  2276. } else {
  2277. if (qp->alt.vid < 0x1000)
  2278. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port,
  2279. qp->alt.vid);
  2280. qp->alt.vid = qp->alt.candidate_vid;
  2281. qp->alt.vlan_port = qp->alt.candidate_vlan_port;
  2282. qp->alt.vlan_index = qp->alt.candidate_vlan_index;
  2283. }
  2284. qp->alt.candidate_vid = 0xFFFF;
  2285. qp->alt.update_vid = 0;
  2286. }
  2287. return err;
  2288. }
  2289. enum {
  2290. MLX4_IB_MODIFY_QP_RSS_SUP_ATTR_MSK = (IB_QP_STATE |
  2291. IB_QP_PORT),
  2292. };
  2293. static int _mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  2294. int attr_mask, struct ib_udata *udata)
  2295. {
  2296. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  2297. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2298. enum ib_qp_state cur_state, new_state;
  2299. int err = -EINVAL;
  2300. mutex_lock(&qp->mutex);
  2301. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  2302. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  2303. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  2304. attr_mask)) {
  2305. pr_debug("qpn 0x%x: invalid attribute mask specified "
  2306. "for transition %d to %d. qp_type %d,"
  2307. " attr_mask 0x%x\n",
  2308. ibqp->qp_num, cur_state, new_state,
  2309. ibqp->qp_type, attr_mask);
  2310. goto out;
  2311. }
  2312. if (ibqp->rwq_ind_tbl) {
  2313. if (!(((cur_state == IB_QPS_RESET) &&
  2314. (new_state == IB_QPS_INIT)) ||
  2315. ((cur_state == IB_QPS_INIT) &&
  2316. (new_state == IB_QPS_RTR)))) {
  2317. pr_debug("qpn 0x%x: RSS QP unsupported transition %d to %d\n",
  2318. ibqp->qp_num, cur_state, new_state);
  2319. err = -EOPNOTSUPP;
  2320. goto out;
  2321. }
  2322. if (attr_mask & ~MLX4_IB_MODIFY_QP_RSS_SUP_ATTR_MSK) {
  2323. pr_debug("qpn 0x%x: RSS QP unsupported attribute mask 0x%x for transition %d to %d\n",
  2324. ibqp->qp_num, attr_mask, cur_state, new_state);
  2325. err = -EOPNOTSUPP;
  2326. goto out;
  2327. }
  2328. }
  2329. if (mlx4_is_bonded(dev->dev) && (attr_mask & IB_QP_PORT)) {
  2330. if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
  2331. if ((ibqp->qp_type == IB_QPT_RC) ||
  2332. (ibqp->qp_type == IB_QPT_UD) ||
  2333. (ibqp->qp_type == IB_QPT_UC) ||
  2334. (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
  2335. (ibqp->qp_type == IB_QPT_XRC_INI)) {
  2336. attr->port_num = mlx4_ib_bond_next_port(dev);
  2337. }
  2338. } else {
  2339. /* no sense in changing port_num
  2340. * when ports are bonded */
  2341. attr_mask &= ~IB_QP_PORT;
  2342. }
  2343. }
  2344. if ((attr_mask & IB_QP_PORT) &&
  2345. (attr->port_num == 0 || attr->port_num > dev->num_ports)) {
  2346. pr_debug("qpn 0x%x: invalid port number (%d) specified "
  2347. "for transition %d to %d. qp_type %d\n",
  2348. ibqp->qp_num, attr->port_num, cur_state,
  2349. new_state, ibqp->qp_type);
  2350. goto out;
  2351. }
  2352. if ((attr_mask & IB_QP_PORT) && (ibqp->qp_type == IB_QPT_RAW_PACKET) &&
  2353. (rdma_port_get_link_layer(&dev->ib_dev, attr->port_num) !=
  2354. IB_LINK_LAYER_ETHERNET))
  2355. goto out;
  2356. if (attr_mask & IB_QP_PKEY_INDEX) {
  2357. int p = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  2358. if (attr->pkey_index >= dev->dev->caps.pkey_table_len[p]) {
  2359. pr_debug("qpn 0x%x: invalid pkey index (%d) specified "
  2360. "for transition %d to %d. qp_type %d\n",
  2361. ibqp->qp_num, attr->pkey_index, cur_state,
  2362. new_state, ibqp->qp_type);
  2363. goto out;
  2364. }
  2365. }
  2366. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  2367. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  2368. pr_debug("qpn 0x%x: max_rd_atomic (%d) too large. "
  2369. "Transition %d to %d. qp_type %d\n",
  2370. ibqp->qp_num, attr->max_rd_atomic, cur_state,
  2371. new_state, ibqp->qp_type);
  2372. goto out;
  2373. }
  2374. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  2375. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  2376. pr_debug("qpn 0x%x: max_dest_rd_atomic (%d) too large. "
  2377. "Transition %d to %d. qp_type %d\n",
  2378. ibqp->qp_num, attr->max_dest_rd_atomic, cur_state,
  2379. new_state, ibqp->qp_type);
  2380. goto out;
  2381. }
  2382. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  2383. err = 0;
  2384. goto out;
  2385. }
  2386. if (ibqp->rwq_ind_tbl && (new_state == IB_QPS_INIT)) {
  2387. err = bringup_rss_rwqs(ibqp->rwq_ind_tbl, attr->port_num,
  2388. udata);
  2389. if (err)
  2390. goto out;
  2391. }
  2392. err = __mlx4_ib_modify_qp(ibqp, MLX4_IB_QP_SRC, attr, attr_mask,
  2393. cur_state, new_state, udata);
  2394. if (ibqp->rwq_ind_tbl && err)
  2395. bring_down_rss_rwqs(ibqp->rwq_ind_tbl, udata);
  2396. if (mlx4_is_bonded(dev->dev) && (attr_mask & IB_QP_PORT))
  2397. attr->port_num = 1;
  2398. out:
  2399. mutex_unlock(&qp->mutex);
  2400. return err;
  2401. }
  2402. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  2403. int attr_mask, struct ib_udata *udata)
  2404. {
  2405. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  2406. int ret;
  2407. if (attr_mask & ~IB_QP_ATTR_STANDARD_BITS)
  2408. return -EOPNOTSUPP;
  2409. ret = _mlx4_ib_modify_qp(ibqp, attr, attr_mask, udata);
  2410. if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  2411. struct mlx4_ib_sqp *sqp = mqp->sqp;
  2412. int err = 0;
  2413. if (sqp->roce_v2_gsi)
  2414. err = ib_modify_qp(sqp->roce_v2_gsi, attr, attr_mask);
  2415. if (err)
  2416. pr_err("Failed to modify GSI QP for RoCEv2 (%d)\n",
  2417. err);
  2418. }
  2419. return ret;
  2420. }
  2421. static int vf_get_qp0_qkey(struct mlx4_dev *dev, int qpn, u32 *qkey)
  2422. {
  2423. int i;
  2424. for (i = 0; i < dev->caps.num_ports; i++) {
  2425. if (qpn == dev->caps.spec_qps[i].qp0_proxy ||
  2426. qpn == dev->caps.spec_qps[i].qp0_tunnel) {
  2427. *qkey = dev->caps.spec_qps[i].qp0_qkey;
  2428. return 0;
  2429. }
  2430. }
  2431. return -EINVAL;
  2432. }
  2433. static int build_sriov_qp0_header(struct mlx4_ib_qp *qp,
  2434. const struct ib_ud_wr *wr,
  2435. void *wqe, unsigned *mlx_seg_len)
  2436. {
  2437. struct mlx4_ib_dev *mdev = to_mdev(qp->ibqp.device);
  2438. struct mlx4_ib_sqp *sqp = qp->sqp;
  2439. struct ib_device *ib_dev = qp->ibqp.device;
  2440. struct mlx4_wqe_mlx_seg *mlx = wqe;
  2441. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  2442. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2443. u16 pkey;
  2444. u32 qkey;
  2445. int send_size;
  2446. int header_size;
  2447. int spc;
  2448. int err;
  2449. int i;
  2450. if (wr->wr.opcode != IB_WR_SEND)
  2451. return -EINVAL;
  2452. send_size = 0;
  2453. for (i = 0; i < wr->wr.num_sge; ++i)
  2454. send_size += wr->wr.sg_list[i].length;
  2455. /* for proxy-qp0 sends, need to add in size of tunnel header */
  2456. /* for tunnel-qp0 sends, tunnel header is already in s/g list */
  2457. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER)
  2458. send_size += sizeof (struct mlx4_ib_tunnel_header);
  2459. ib_ud_header_init(send_size, 1, 0, 0, 0, 0, 0, 0, &sqp->ud_header);
  2460. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER) {
  2461. sqp->ud_header.lrh.service_level =
  2462. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  2463. sqp->ud_header.lrh.destination_lid =
  2464. cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2465. sqp->ud_header.lrh.source_lid =
  2466. cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2467. }
  2468. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  2469. /* force loopback */
  2470. mlx->flags |= cpu_to_be32(MLX4_WQE_MLX_VL15 | 0x1 | MLX4_WQE_MLX_SLR);
  2471. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  2472. sqp->ud_header.lrh.virtual_lane = 0;
  2473. sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED);
  2474. err = ib_get_cached_pkey(ib_dev, qp->port, 0, &pkey);
  2475. if (err)
  2476. return err;
  2477. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  2478. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_SMI_OWNER)
  2479. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn);
  2480. else
  2481. sqp->ud_header.bth.destination_qpn =
  2482. cpu_to_be32(mdev->dev->caps.spec_qps[qp->port - 1].qp0_tunnel);
  2483. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  2484. if (mlx4_is_master(mdev->dev)) {
  2485. if (mlx4_get_parav_qkey(mdev->dev, qp->mqp.qpn, &qkey))
  2486. return -EINVAL;
  2487. } else {
  2488. if (vf_get_qp0_qkey(mdev->dev, qp->mqp.qpn, &qkey))
  2489. return -EINVAL;
  2490. }
  2491. sqp->ud_header.deth.qkey = cpu_to_be32(qkey);
  2492. sqp->ud_header.deth.source_qpn = cpu_to_be32(qp->mqp.qpn);
  2493. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  2494. sqp->ud_header.immediate_present = 0;
  2495. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  2496. /*
  2497. * Inline data segments may not cross a 64 byte boundary. If
  2498. * our UD header is bigger than the space available up to the
  2499. * next 64 byte boundary in the WQE, use two inline data
  2500. * segments to hold the UD header.
  2501. */
  2502. spc = MLX4_INLINE_ALIGN -
  2503. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2504. if (header_size <= spc) {
  2505. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  2506. memcpy(inl + 1, sqp->header_buf, header_size);
  2507. i = 1;
  2508. } else {
  2509. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2510. memcpy(inl + 1, sqp->header_buf, spc);
  2511. inl = (void *) (inl + 1) + spc;
  2512. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  2513. /*
  2514. * Need a barrier here to make sure all the data is
  2515. * visible before the byte_count field is set.
  2516. * Otherwise the HCA prefetcher could grab the 64-byte
  2517. * chunk with this inline segment and get a valid (!=
  2518. * 0xffffffff) byte count but stale data, and end up
  2519. * generating a packet with bad headers.
  2520. *
  2521. * The first inline segment's byte_count field doesn't
  2522. * need a barrier, because it comes after a
  2523. * control/MLX segment and therefore is at an offset
  2524. * of 16 mod 64.
  2525. */
  2526. wmb();
  2527. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  2528. i = 2;
  2529. }
  2530. *mlx_seg_len =
  2531. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  2532. return 0;
  2533. }
  2534. static u8 sl_to_vl(struct mlx4_ib_dev *dev, u8 sl, int port_num)
  2535. {
  2536. union sl2vl_tbl_to_u64 tmp_vltab;
  2537. u8 vl;
  2538. if (sl > 15)
  2539. return 0xf;
  2540. tmp_vltab.sl64 = atomic64_read(&dev->sl2vl[port_num - 1]);
  2541. vl = tmp_vltab.sl8[sl >> 1];
  2542. if (sl & 1)
  2543. vl &= 0x0f;
  2544. else
  2545. vl >>= 4;
  2546. return vl;
  2547. }
  2548. static int fill_gid_by_hw_index(struct mlx4_ib_dev *ibdev, u8 port_num,
  2549. int index, union ib_gid *gid,
  2550. enum ib_gid_type *gid_type)
  2551. {
  2552. struct mlx4_ib_iboe *iboe = &ibdev->iboe;
  2553. struct mlx4_port_gid_table *port_gid_table;
  2554. unsigned long flags;
  2555. port_gid_table = &iboe->gids[port_num - 1];
  2556. spin_lock_irqsave(&iboe->lock, flags);
  2557. memcpy(gid, &port_gid_table->gids[index].gid, sizeof(*gid));
  2558. *gid_type = port_gid_table->gids[index].gid_type;
  2559. spin_unlock_irqrestore(&iboe->lock, flags);
  2560. if (rdma_is_zero_gid(gid))
  2561. return -ENOENT;
  2562. return 0;
  2563. }
  2564. #define MLX4_ROCEV2_QP1_SPORT 0xC000
  2565. static int build_mlx_header(struct mlx4_ib_qp *qp, const struct ib_ud_wr *wr,
  2566. void *wqe, unsigned *mlx_seg_len)
  2567. {
  2568. struct mlx4_ib_sqp *sqp = qp->sqp;
  2569. struct ib_device *ib_dev = qp->ibqp.device;
  2570. struct mlx4_ib_dev *ibdev = to_mdev(ib_dev);
  2571. struct mlx4_wqe_mlx_seg *mlx = wqe;
  2572. struct mlx4_wqe_ctrl_seg *ctrl = wqe;
  2573. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  2574. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2575. union ib_gid sgid;
  2576. u16 pkey;
  2577. int send_size;
  2578. int header_size;
  2579. int spc;
  2580. int i;
  2581. int err = 0;
  2582. u16 vlan = 0xffff;
  2583. bool is_eth;
  2584. bool is_vlan = false;
  2585. bool is_grh;
  2586. bool is_udp = false;
  2587. int ip_version = 0;
  2588. send_size = 0;
  2589. for (i = 0; i < wr->wr.num_sge; ++i)
  2590. send_size += wr->wr.sg_list[i].length;
  2591. is_eth = rdma_port_get_link_layer(qp->ibqp.device, qp->port) == IB_LINK_LAYER_ETHERNET;
  2592. is_grh = mlx4_ib_ah_grh_present(ah);
  2593. if (is_eth) {
  2594. enum ib_gid_type gid_type;
  2595. if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
  2596. /* When multi-function is enabled, the ib_core gid
  2597. * indexes don't necessarily match the hw ones, so
  2598. * we must use our own cache */
  2599. err = mlx4_get_roce_gid_from_slave(to_mdev(ib_dev)->dev,
  2600. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  2601. ah->av.ib.gid_index, &sgid.raw[0]);
  2602. if (err)
  2603. return err;
  2604. } else {
  2605. err = fill_gid_by_hw_index(ibdev, qp->port,
  2606. ah->av.ib.gid_index, &sgid,
  2607. &gid_type);
  2608. if (!err) {
  2609. is_udp = gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP;
  2610. if (is_udp) {
  2611. if (ipv6_addr_v4mapped((struct in6_addr *)&sgid))
  2612. ip_version = 4;
  2613. else
  2614. ip_version = 6;
  2615. is_grh = false;
  2616. }
  2617. } else {
  2618. return err;
  2619. }
  2620. }
  2621. if (ah->av.eth.vlan != cpu_to_be16(0xffff)) {
  2622. vlan = be16_to_cpu(ah->av.eth.vlan) & 0x0fff;
  2623. is_vlan = true;
  2624. }
  2625. }
  2626. err = ib_ud_header_init(send_size, !is_eth, is_eth, is_vlan, is_grh,
  2627. ip_version, is_udp, 0, &sqp->ud_header);
  2628. if (err)
  2629. return err;
  2630. if (!is_eth) {
  2631. sqp->ud_header.lrh.service_level =
  2632. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  2633. sqp->ud_header.lrh.destination_lid = ah->av.ib.dlid;
  2634. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2635. }
  2636. if (is_grh || (ip_version == 6)) {
  2637. sqp->ud_header.grh.traffic_class =
  2638. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  2639. sqp->ud_header.grh.flow_label =
  2640. ah->av.ib.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  2641. sqp->ud_header.grh.hop_limit = ah->av.ib.hop_limit;
  2642. if (is_eth) {
  2643. memcpy(sqp->ud_header.grh.source_gid.raw, sgid.raw, 16);
  2644. } else {
  2645. if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
  2646. /* When multi-function is enabled, the ib_core gid
  2647. * indexes don't necessarily match the hw ones, so
  2648. * we must use our own cache
  2649. */
  2650. sqp->ud_header.grh.source_gid.global
  2651. .subnet_prefix =
  2652. cpu_to_be64(atomic64_read(
  2653. &(to_mdev(ib_dev)
  2654. ->sriov
  2655. .demux[qp->port - 1]
  2656. .subnet_prefix)));
  2657. sqp->ud_header.grh.source_gid.global
  2658. .interface_id =
  2659. to_mdev(ib_dev)
  2660. ->sriov.demux[qp->port - 1]
  2661. .guid_cache[ah->av.ib.gid_index];
  2662. } else {
  2663. sqp->ud_header.grh.source_gid =
  2664. ah->ibah.sgid_attr->gid;
  2665. }
  2666. }
  2667. memcpy(sqp->ud_header.grh.destination_gid.raw,
  2668. ah->av.ib.dgid, 16);
  2669. }
  2670. if (ip_version == 4) {
  2671. sqp->ud_header.ip4.tos =
  2672. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  2673. sqp->ud_header.ip4.id = 0;
  2674. sqp->ud_header.ip4.frag_off = htons(IP_DF);
  2675. sqp->ud_header.ip4.ttl = ah->av.eth.hop_limit;
  2676. memcpy(&sqp->ud_header.ip4.saddr,
  2677. sgid.raw + 12, 4);
  2678. memcpy(&sqp->ud_header.ip4.daddr, ah->av.ib.dgid + 12, 4);
  2679. sqp->ud_header.ip4.check = ib_ud_ip4_csum(&sqp->ud_header);
  2680. }
  2681. if (is_udp) {
  2682. sqp->ud_header.udp.dport = htons(ROCE_V2_UDP_DPORT);
  2683. sqp->ud_header.udp.sport = htons(MLX4_ROCEV2_QP1_SPORT);
  2684. sqp->ud_header.udp.csum = 0;
  2685. }
  2686. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  2687. if (!is_eth) {
  2688. mlx->flags |=
  2689. cpu_to_be32((!qp->ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  2690. (sqp->ud_header.lrh.destination_lid ==
  2691. IB_LID_PERMISSIVE ?
  2692. MLX4_WQE_MLX_SLR :
  2693. 0) |
  2694. (sqp->ud_header.lrh.service_level << 8));
  2695. if (ah->av.ib.port_pd & cpu_to_be32(0x80000000))
  2696. mlx->flags |= cpu_to_be32(0x1); /* force loopback */
  2697. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  2698. }
  2699. switch (wr->wr.opcode) {
  2700. case IB_WR_SEND:
  2701. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  2702. sqp->ud_header.immediate_present = 0;
  2703. break;
  2704. case IB_WR_SEND_WITH_IMM:
  2705. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  2706. sqp->ud_header.immediate_present = 1;
  2707. sqp->ud_header.immediate_data = wr->wr.ex.imm_data;
  2708. break;
  2709. default:
  2710. return -EINVAL;
  2711. }
  2712. if (is_eth) {
  2713. u16 ether_type;
  2714. u16 pcp = (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 29) << 13;
  2715. ether_type = (!is_udp) ? ETH_P_IBOE:
  2716. (ip_version == 4 ? ETH_P_IP : ETH_P_IPV6);
  2717. mlx->sched_prio = cpu_to_be16(pcp);
  2718. ether_addr_copy(sqp->ud_header.eth.smac_h, ah->av.eth.s_mac);
  2719. ether_addr_copy(sqp->ud_header.eth.dmac_h, ah->av.eth.mac);
  2720. memcpy(&ctrl->srcrb_flags16[0], ah->av.eth.mac, 2);
  2721. memcpy(&ctrl->imm, ah->av.eth.mac + 2, 4);
  2722. if (!memcmp(sqp->ud_header.eth.smac_h, sqp->ud_header.eth.dmac_h, 6))
  2723. mlx->flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
  2724. if (!is_vlan) {
  2725. sqp->ud_header.eth.type = cpu_to_be16(ether_type);
  2726. } else {
  2727. sqp->ud_header.vlan.type = cpu_to_be16(ether_type);
  2728. sqp->ud_header.vlan.tag = cpu_to_be16(vlan | pcp);
  2729. }
  2730. } else {
  2731. sqp->ud_header.lrh.virtual_lane =
  2732. !qp->ibqp.qp_num ?
  2733. 15 :
  2734. sl_to_vl(to_mdev(ib_dev),
  2735. sqp->ud_header.lrh.service_level,
  2736. qp->port);
  2737. if (qp->ibqp.qp_num && sqp->ud_header.lrh.virtual_lane == 15)
  2738. return -EINVAL;
  2739. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  2740. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  2741. }
  2742. sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED);
  2743. if (!qp->ibqp.qp_num)
  2744. err = ib_get_cached_pkey(ib_dev, qp->port, sqp->pkey_index,
  2745. &pkey);
  2746. else
  2747. err = ib_get_cached_pkey(ib_dev, qp->port, wr->pkey_index,
  2748. &pkey);
  2749. if (err)
  2750. return err;
  2751. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  2752. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn);
  2753. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  2754. sqp->ud_header.deth.qkey = cpu_to_be32(wr->remote_qkey & 0x80000000 ?
  2755. sqp->qkey : wr->remote_qkey);
  2756. sqp->ud_header.deth.source_qpn = cpu_to_be32(qp->ibqp.qp_num);
  2757. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  2758. if (0) {
  2759. pr_err("built UD header of size %d:\n", header_size);
  2760. for (i = 0; i < header_size / 4; ++i) {
  2761. if (i % 8 == 0)
  2762. pr_err(" [%02x] ", i * 4);
  2763. pr_cont(" %08x",
  2764. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  2765. if ((i + 1) % 8 == 0)
  2766. pr_cont("\n");
  2767. }
  2768. pr_err("\n");
  2769. }
  2770. /*
  2771. * Inline data segments may not cross a 64 byte boundary. If
  2772. * our UD header is bigger than the space available up to the
  2773. * next 64 byte boundary in the WQE, use two inline data
  2774. * segments to hold the UD header.
  2775. */
  2776. spc = MLX4_INLINE_ALIGN -
  2777. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2778. if (header_size <= spc) {
  2779. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  2780. memcpy(inl + 1, sqp->header_buf, header_size);
  2781. i = 1;
  2782. } else {
  2783. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2784. memcpy(inl + 1, sqp->header_buf, spc);
  2785. inl = (void *) (inl + 1) + spc;
  2786. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  2787. /*
  2788. * Need a barrier here to make sure all the data is
  2789. * visible before the byte_count field is set.
  2790. * Otherwise the HCA prefetcher could grab the 64-byte
  2791. * chunk with this inline segment and get a valid (!=
  2792. * 0xffffffff) byte count but stale data, and end up
  2793. * generating a packet with bad headers.
  2794. *
  2795. * The first inline segment's byte_count field doesn't
  2796. * need a barrier, because it comes after a
  2797. * control/MLX segment and therefore is at an offset
  2798. * of 16 mod 64.
  2799. */
  2800. wmb();
  2801. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  2802. i = 2;
  2803. }
  2804. *mlx_seg_len =
  2805. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  2806. return 0;
  2807. }
  2808. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  2809. {
  2810. unsigned cur;
  2811. struct mlx4_ib_cq *cq;
  2812. cur = wq->head - wq->tail;
  2813. if (likely(cur + nreq < wq->max_post))
  2814. return 0;
  2815. cq = to_mcq(ib_cq);
  2816. spin_lock(&cq->lock);
  2817. cur = wq->head - wq->tail;
  2818. spin_unlock(&cq->lock);
  2819. return cur + nreq >= wq->max_post;
  2820. }
  2821. static __be32 convert_access(int acc)
  2822. {
  2823. return (acc & IB_ACCESS_REMOTE_ATOMIC ?
  2824. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_ATOMIC) : 0) |
  2825. (acc & IB_ACCESS_REMOTE_WRITE ?
  2826. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_WRITE) : 0) |
  2827. (acc & IB_ACCESS_REMOTE_READ ?
  2828. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_READ) : 0) |
  2829. (acc & IB_ACCESS_LOCAL_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_WRITE) : 0) |
  2830. cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_READ);
  2831. }
  2832. static void set_reg_seg(struct mlx4_wqe_fmr_seg *fseg,
  2833. const struct ib_reg_wr *wr)
  2834. {
  2835. struct mlx4_ib_mr *mr = to_mmr(wr->mr);
  2836. fseg->flags = convert_access(wr->access);
  2837. fseg->mem_key = cpu_to_be32(wr->key);
  2838. fseg->buf_list = cpu_to_be64(mr->page_map);
  2839. fseg->start_addr = cpu_to_be64(mr->ibmr.iova);
  2840. fseg->reg_len = cpu_to_be64(mr->ibmr.length);
  2841. fseg->offset = 0; /* XXX -- is this just for ZBVA? */
  2842. fseg->page_size = cpu_to_be32(ilog2(mr->ibmr.page_size));
  2843. fseg->reserved[0] = 0;
  2844. fseg->reserved[1] = 0;
  2845. }
  2846. static void set_local_inv_seg(struct mlx4_wqe_local_inval_seg *iseg, u32 rkey)
  2847. {
  2848. memset(iseg, 0, sizeof(*iseg));
  2849. iseg->mem_key = cpu_to_be32(rkey);
  2850. }
  2851. static __always_inline void set_raddr_seg(struct mlx4_wqe_raddr_seg *rseg,
  2852. u64 remote_addr, u32 rkey)
  2853. {
  2854. rseg->raddr = cpu_to_be64(remote_addr);
  2855. rseg->rkey = cpu_to_be32(rkey);
  2856. rseg->reserved = 0;
  2857. }
  2858. static void set_atomic_seg(struct mlx4_wqe_atomic_seg *aseg,
  2859. const struct ib_atomic_wr *wr)
  2860. {
  2861. if (wr->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  2862. aseg->swap_add = cpu_to_be64(wr->swap);
  2863. aseg->compare = cpu_to_be64(wr->compare_add);
  2864. } else if (wr->wr.opcode == IB_WR_MASKED_ATOMIC_FETCH_AND_ADD) {
  2865. aseg->swap_add = cpu_to_be64(wr->compare_add);
  2866. aseg->compare = cpu_to_be64(wr->compare_add_mask);
  2867. } else {
  2868. aseg->swap_add = cpu_to_be64(wr->compare_add);
  2869. aseg->compare = 0;
  2870. }
  2871. }
  2872. static void set_masked_atomic_seg(struct mlx4_wqe_masked_atomic_seg *aseg,
  2873. const struct ib_atomic_wr *wr)
  2874. {
  2875. aseg->swap_add = cpu_to_be64(wr->swap);
  2876. aseg->swap_add_mask = cpu_to_be64(wr->swap_mask);
  2877. aseg->compare = cpu_to_be64(wr->compare_add);
  2878. aseg->compare_mask = cpu_to_be64(wr->compare_add_mask);
  2879. }
  2880. static void set_datagram_seg(struct mlx4_wqe_datagram_seg *dseg,
  2881. const struct ib_ud_wr *wr)
  2882. {
  2883. memcpy(dseg->av, &to_mah(wr->ah)->av, sizeof (struct mlx4_av));
  2884. dseg->dqpn = cpu_to_be32(wr->remote_qpn);
  2885. dseg->qkey = cpu_to_be32(wr->remote_qkey);
  2886. dseg->vlan = to_mah(wr->ah)->av.eth.vlan;
  2887. memcpy(dseg->mac, to_mah(wr->ah)->av.eth.mac, 6);
  2888. }
  2889. static void set_tunnel_datagram_seg(struct mlx4_ib_dev *dev,
  2890. struct mlx4_wqe_datagram_seg *dseg,
  2891. const struct ib_ud_wr *wr,
  2892. enum mlx4_ib_qp_type qpt)
  2893. {
  2894. union mlx4_ext_av *av = &to_mah(wr->ah)->av;
  2895. struct mlx4_av sqp_av = {0};
  2896. int port = *((u8 *) &av->ib.port_pd) & 0x3;
  2897. /* force loopback */
  2898. sqp_av.port_pd = av->ib.port_pd | cpu_to_be32(0x80000000);
  2899. sqp_av.g_slid = av->ib.g_slid & 0x7f; /* no GRH */
  2900. sqp_av.sl_tclass_flowlabel = av->ib.sl_tclass_flowlabel &
  2901. cpu_to_be32(0xf0000000);
  2902. memcpy(dseg->av, &sqp_av, sizeof (struct mlx4_av));
  2903. if (qpt == MLX4_IB_QPT_PROXY_GSI)
  2904. dseg->dqpn = cpu_to_be32(dev->dev->caps.spec_qps[port - 1].qp1_tunnel);
  2905. else
  2906. dseg->dqpn = cpu_to_be32(dev->dev->caps.spec_qps[port - 1].qp0_tunnel);
  2907. /* Use QKEY from the QP context, which is set by master */
  2908. dseg->qkey = cpu_to_be32(IB_QP_SET_QKEY);
  2909. }
  2910. static void build_tunnel_header(const struct ib_ud_wr *wr, void *wqe,
  2911. unsigned *mlx_seg_len)
  2912. {
  2913. struct mlx4_wqe_inline_seg *inl = wqe;
  2914. struct mlx4_ib_tunnel_header hdr;
  2915. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2916. int spc;
  2917. int i;
  2918. memcpy(&hdr.av, &ah->av, sizeof hdr.av);
  2919. hdr.remote_qpn = cpu_to_be32(wr->remote_qpn);
  2920. hdr.pkey_index = cpu_to_be16(wr->pkey_index);
  2921. hdr.qkey = cpu_to_be32(wr->remote_qkey);
  2922. memcpy(hdr.mac, ah->av.eth.mac, 6);
  2923. hdr.vlan = ah->av.eth.vlan;
  2924. spc = MLX4_INLINE_ALIGN -
  2925. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2926. if (sizeof (hdr) <= spc) {
  2927. memcpy(inl + 1, &hdr, sizeof (hdr));
  2928. wmb();
  2929. inl->byte_count = cpu_to_be32(1 << 31 | sizeof (hdr));
  2930. i = 1;
  2931. } else {
  2932. memcpy(inl + 1, &hdr, spc);
  2933. wmb();
  2934. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2935. inl = (void *) (inl + 1) + spc;
  2936. memcpy(inl + 1, (void *) &hdr + spc, sizeof (hdr) - spc);
  2937. wmb();
  2938. inl->byte_count = cpu_to_be32(1 << 31 | (sizeof (hdr) - spc));
  2939. i = 2;
  2940. }
  2941. *mlx_seg_len =
  2942. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + sizeof (hdr), 16);
  2943. }
  2944. static void set_mlx_icrc_seg(void *dseg)
  2945. {
  2946. u32 *t = dseg;
  2947. struct mlx4_wqe_inline_seg *iseg = dseg;
  2948. t[1] = 0;
  2949. /*
  2950. * Need a barrier here before writing the byte_count field to
  2951. * make sure that all the data is visible before the
  2952. * byte_count field is set. Otherwise, if the segment begins
  2953. * a new cacheline, the HCA prefetcher could grab the 64-byte
  2954. * chunk and get a valid (!= * 0xffffffff) byte count but
  2955. * stale data, and end up sending the wrong data.
  2956. */
  2957. wmb();
  2958. iseg->byte_count = cpu_to_be32((1 << 31) | 4);
  2959. }
  2960. static void set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  2961. {
  2962. dseg->lkey = cpu_to_be32(sg->lkey);
  2963. dseg->addr = cpu_to_be64(sg->addr);
  2964. /*
  2965. * Need a barrier here before writing the byte_count field to
  2966. * make sure that all the data is visible before the
  2967. * byte_count field is set. Otherwise, if the segment begins
  2968. * a new cacheline, the HCA prefetcher could grab the 64-byte
  2969. * chunk and get a valid (!= * 0xffffffff) byte count but
  2970. * stale data, and end up sending the wrong data.
  2971. */
  2972. wmb();
  2973. dseg->byte_count = cpu_to_be32(sg->length);
  2974. }
  2975. static void __set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  2976. {
  2977. dseg->byte_count = cpu_to_be32(sg->length);
  2978. dseg->lkey = cpu_to_be32(sg->lkey);
  2979. dseg->addr = cpu_to_be64(sg->addr);
  2980. }
  2981. static int build_lso_seg(struct mlx4_wqe_lso_seg *wqe,
  2982. const struct ib_ud_wr *wr, struct mlx4_ib_qp *qp,
  2983. unsigned *lso_seg_len, __be32 *lso_hdr_sz, __be32 *blh)
  2984. {
  2985. unsigned halign = ALIGN(sizeof *wqe + wr->hlen, 16);
  2986. if (unlikely(halign > MLX4_IB_CACHE_LINE_SIZE))
  2987. *blh = cpu_to_be32(1 << 6);
  2988. if (unlikely(!(qp->flags & MLX4_IB_QP_LSO) &&
  2989. wr->wr.num_sge > qp->sq.max_gs - (halign >> 4)))
  2990. return -EINVAL;
  2991. memcpy(wqe->header, wr->header, wr->hlen);
  2992. *lso_hdr_sz = cpu_to_be32(wr->mss << 16 | wr->hlen);
  2993. *lso_seg_len = halign;
  2994. return 0;
  2995. }
  2996. static __be32 send_ieth(const struct ib_send_wr *wr)
  2997. {
  2998. switch (wr->opcode) {
  2999. case IB_WR_SEND_WITH_IMM:
  3000. case IB_WR_RDMA_WRITE_WITH_IMM:
  3001. return wr->ex.imm_data;
  3002. case IB_WR_SEND_WITH_INV:
  3003. return cpu_to_be32(wr->ex.invalidate_rkey);
  3004. default:
  3005. return 0;
  3006. }
  3007. }
  3008. static void add_zero_len_inline(void *wqe)
  3009. {
  3010. struct mlx4_wqe_inline_seg *inl = wqe;
  3011. memset(wqe, 0, 16);
  3012. inl->byte_count = cpu_to_be32(1 << 31);
  3013. }
  3014. static int _mlx4_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
  3015. const struct ib_send_wr **bad_wr, bool drain)
  3016. {
  3017. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  3018. void *wqe;
  3019. struct mlx4_wqe_ctrl_seg *ctrl;
  3020. struct mlx4_wqe_data_seg *dseg;
  3021. unsigned long flags;
  3022. int nreq;
  3023. int err = 0;
  3024. unsigned ind;
  3025. int size;
  3026. unsigned seglen;
  3027. __be32 dummy;
  3028. __be32 *lso_wqe;
  3029. __be32 lso_hdr_sz;
  3030. __be32 blh;
  3031. int i;
  3032. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  3033. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  3034. struct mlx4_ib_sqp *sqp = qp->sqp;
  3035. if (sqp->roce_v2_gsi) {
  3036. struct mlx4_ib_ah *ah = to_mah(ud_wr(wr)->ah);
  3037. enum ib_gid_type gid_type;
  3038. union ib_gid gid;
  3039. if (!fill_gid_by_hw_index(mdev, qp->port,
  3040. ah->av.ib.gid_index,
  3041. &gid, &gid_type))
  3042. qp = (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ?
  3043. to_mqp(sqp->roce_v2_gsi) : qp;
  3044. else
  3045. pr_err("Failed to get gid at index %d. RoCEv2 will not work properly\n",
  3046. ah->av.ib.gid_index);
  3047. }
  3048. }
  3049. spin_lock_irqsave(&qp->sq.lock, flags);
  3050. if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR &&
  3051. !drain) {
  3052. err = -EIO;
  3053. *bad_wr = wr;
  3054. nreq = 0;
  3055. goto out;
  3056. }
  3057. ind = qp->sq_next_wqe;
  3058. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  3059. lso_wqe = &dummy;
  3060. blh = 0;
  3061. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  3062. err = -ENOMEM;
  3063. *bad_wr = wr;
  3064. goto out;
  3065. }
  3066. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  3067. err = -EINVAL;
  3068. *bad_wr = wr;
  3069. goto out;
  3070. }
  3071. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  3072. qp->sq.wrid[(qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
  3073. ctrl->srcrb_flags =
  3074. (wr->send_flags & IB_SEND_SIGNALED ?
  3075. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  3076. (wr->send_flags & IB_SEND_SOLICITED ?
  3077. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  3078. ((wr->send_flags & IB_SEND_IP_CSUM) ?
  3079. cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  3080. MLX4_WQE_CTRL_TCP_UDP_CSUM) : 0) |
  3081. qp->sq_signal_bits;
  3082. ctrl->imm = send_ieth(wr);
  3083. wqe += sizeof *ctrl;
  3084. size = sizeof *ctrl / 16;
  3085. switch (qp->mlx4_ib_qp_type) {
  3086. case MLX4_IB_QPT_RC:
  3087. case MLX4_IB_QPT_UC:
  3088. switch (wr->opcode) {
  3089. case IB_WR_ATOMIC_CMP_AND_SWP:
  3090. case IB_WR_ATOMIC_FETCH_AND_ADD:
  3091. case IB_WR_MASKED_ATOMIC_FETCH_AND_ADD:
  3092. set_raddr_seg(wqe, atomic_wr(wr)->remote_addr,
  3093. atomic_wr(wr)->rkey);
  3094. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  3095. set_atomic_seg(wqe, atomic_wr(wr));
  3096. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  3097. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  3098. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  3099. break;
  3100. case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
  3101. set_raddr_seg(wqe, atomic_wr(wr)->remote_addr,
  3102. atomic_wr(wr)->rkey);
  3103. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  3104. set_masked_atomic_seg(wqe, atomic_wr(wr));
  3105. wqe += sizeof (struct mlx4_wqe_masked_atomic_seg);
  3106. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  3107. sizeof (struct mlx4_wqe_masked_atomic_seg)) / 16;
  3108. break;
  3109. case IB_WR_RDMA_READ:
  3110. case IB_WR_RDMA_WRITE:
  3111. case IB_WR_RDMA_WRITE_WITH_IMM:
  3112. set_raddr_seg(wqe, rdma_wr(wr)->remote_addr,
  3113. rdma_wr(wr)->rkey);
  3114. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  3115. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  3116. break;
  3117. case IB_WR_LOCAL_INV:
  3118. ctrl->srcrb_flags |=
  3119. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  3120. set_local_inv_seg(wqe, wr->ex.invalidate_rkey);
  3121. wqe += sizeof (struct mlx4_wqe_local_inval_seg);
  3122. size += sizeof (struct mlx4_wqe_local_inval_seg) / 16;
  3123. break;
  3124. case IB_WR_REG_MR:
  3125. ctrl->srcrb_flags |=
  3126. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  3127. set_reg_seg(wqe, reg_wr(wr));
  3128. wqe += sizeof(struct mlx4_wqe_fmr_seg);
  3129. size += sizeof(struct mlx4_wqe_fmr_seg) / 16;
  3130. break;
  3131. default:
  3132. /* No extra segments required for sends */
  3133. break;
  3134. }
  3135. break;
  3136. case MLX4_IB_QPT_TUN_SMI_OWNER:
  3137. err = build_sriov_qp0_header(qp, ud_wr(wr), ctrl,
  3138. &seglen);
  3139. if (unlikely(err)) {
  3140. *bad_wr = wr;
  3141. goto out;
  3142. }
  3143. wqe += seglen;
  3144. size += seglen / 16;
  3145. break;
  3146. case MLX4_IB_QPT_TUN_SMI:
  3147. case MLX4_IB_QPT_TUN_GSI:
  3148. /* this is a UD qp used in MAD responses to slaves. */
  3149. set_datagram_seg(wqe, ud_wr(wr));
  3150. /* set the forced-loopback bit in the data seg av */
  3151. *(__be32 *) wqe |= cpu_to_be32(0x80000000);
  3152. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  3153. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  3154. break;
  3155. case MLX4_IB_QPT_UD:
  3156. set_datagram_seg(wqe, ud_wr(wr));
  3157. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  3158. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  3159. if (wr->opcode == IB_WR_LSO) {
  3160. err = build_lso_seg(wqe, ud_wr(wr), qp, &seglen,
  3161. &lso_hdr_sz, &blh);
  3162. if (unlikely(err)) {
  3163. *bad_wr = wr;
  3164. goto out;
  3165. }
  3166. lso_wqe = (__be32 *) wqe;
  3167. wqe += seglen;
  3168. size += seglen / 16;
  3169. }
  3170. break;
  3171. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  3172. err = build_sriov_qp0_header(qp, ud_wr(wr), ctrl,
  3173. &seglen);
  3174. if (unlikely(err)) {
  3175. *bad_wr = wr;
  3176. goto out;
  3177. }
  3178. wqe += seglen;
  3179. size += seglen / 16;
  3180. /* to start tunnel header on a cache-line boundary */
  3181. add_zero_len_inline(wqe);
  3182. wqe += 16;
  3183. size++;
  3184. build_tunnel_header(ud_wr(wr), wqe, &seglen);
  3185. wqe += seglen;
  3186. size += seglen / 16;
  3187. break;
  3188. case MLX4_IB_QPT_PROXY_SMI:
  3189. case MLX4_IB_QPT_PROXY_GSI:
  3190. /* If we are tunneling special qps, this is a UD qp.
  3191. * In this case we first add a UD segment targeting
  3192. * the tunnel qp, and then add a header with address
  3193. * information */
  3194. set_tunnel_datagram_seg(to_mdev(ibqp->device), wqe,
  3195. ud_wr(wr),
  3196. qp->mlx4_ib_qp_type);
  3197. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  3198. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  3199. build_tunnel_header(ud_wr(wr), wqe, &seglen);
  3200. wqe += seglen;
  3201. size += seglen / 16;
  3202. break;
  3203. case MLX4_IB_QPT_SMI:
  3204. case MLX4_IB_QPT_GSI:
  3205. err = build_mlx_header(qp, ud_wr(wr), ctrl, &seglen);
  3206. if (unlikely(err)) {
  3207. *bad_wr = wr;
  3208. goto out;
  3209. }
  3210. wqe += seglen;
  3211. size += seglen / 16;
  3212. break;
  3213. default:
  3214. break;
  3215. }
  3216. /*
  3217. * Write data segments in reverse order, so as to
  3218. * overwrite cacheline stamp last within each
  3219. * cacheline. This avoids issues with WQE
  3220. * prefetching.
  3221. */
  3222. dseg = wqe;
  3223. dseg += wr->num_sge - 1;
  3224. size += wr->num_sge * (sizeof (struct mlx4_wqe_data_seg) / 16);
  3225. /* Add one more inline data segment for ICRC for MLX sends */
  3226. if (unlikely(qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
  3227. qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI ||
  3228. qp->mlx4_ib_qp_type &
  3229. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))) {
  3230. set_mlx_icrc_seg(dseg + 1);
  3231. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  3232. }
  3233. for (i = wr->num_sge - 1; i >= 0; --i, --dseg)
  3234. set_data_seg(dseg, wr->sg_list + i);
  3235. /*
  3236. * Possibly overwrite stamping in cacheline with LSO
  3237. * segment only after making sure all data segments
  3238. * are written.
  3239. */
  3240. wmb();
  3241. *lso_wqe = lso_hdr_sz;
  3242. ctrl->qpn_vlan.fence_size = (wr->send_flags & IB_SEND_FENCE ?
  3243. MLX4_WQE_CTRL_FENCE : 0) | size;
  3244. /*
  3245. * Make sure descriptor is fully written before
  3246. * setting ownership bit (because HW can start
  3247. * executing as soon as we do).
  3248. */
  3249. wmb();
  3250. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  3251. *bad_wr = wr;
  3252. err = -EINVAL;
  3253. goto out;
  3254. }
  3255. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  3256. (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0) | blh;
  3257. /*
  3258. * We can improve latency by not stamping the last
  3259. * send queue WQE until after ringing the doorbell, so
  3260. * only stamp here if there are still more WQEs to post.
  3261. */
  3262. if (wr->next)
  3263. stamp_send_wqe(qp, ind + qp->sq_spare_wqes);
  3264. ind++;
  3265. }
  3266. out:
  3267. if (likely(nreq)) {
  3268. qp->sq.head += nreq;
  3269. /*
  3270. * Make sure that descriptors are written before
  3271. * doorbell record.
  3272. */
  3273. wmb();
  3274. writel_relaxed(qp->doorbell_qpn,
  3275. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  3276. stamp_send_wqe(qp, ind + qp->sq_spare_wqes - 1);
  3277. qp->sq_next_wqe = ind;
  3278. }
  3279. spin_unlock_irqrestore(&qp->sq.lock, flags);
  3280. return err;
  3281. }
  3282. int mlx4_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
  3283. const struct ib_send_wr **bad_wr)
  3284. {
  3285. return _mlx4_ib_post_send(ibqp, wr, bad_wr, false);
  3286. }
  3287. static int _mlx4_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
  3288. const struct ib_recv_wr **bad_wr, bool drain)
  3289. {
  3290. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  3291. struct mlx4_wqe_data_seg *scat;
  3292. unsigned long flags;
  3293. int err = 0;
  3294. int nreq;
  3295. int ind;
  3296. int max_gs;
  3297. int i;
  3298. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  3299. max_gs = qp->rq.max_gs;
  3300. spin_lock_irqsave(&qp->rq.lock, flags);
  3301. if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR &&
  3302. !drain) {
  3303. err = -EIO;
  3304. *bad_wr = wr;
  3305. nreq = 0;
  3306. goto out;
  3307. }
  3308. ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
  3309. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  3310. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
  3311. err = -ENOMEM;
  3312. *bad_wr = wr;
  3313. goto out;
  3314. }
  3315. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  3316. err = -EINVAL;
  3317. *bad_wr = wr;
  3318. goto out;
  3319. }
  3320. scat = get_recv_wqe(qp, ind);
  3321. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  3322. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
  3323. ib_dma_sync_single_for_device(ibqp->device,
  3324. qp->sqp_proxy_rcv[ind].map,
  3325. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  3326. DMA_FROM_DEVICE);
  3327. scat->byte_count =
  3328. cpu_to_be32(sizeof (struct mlx4_ib_proxy_sqp_hdr));
  3329. /* use dma lkey from upper layer entry */
  3330. scat->lkey = cpu_to_be32(wr->sg_list->lkey);
  3331. scat->addr = cpu_to_be64(qp->sqp_proxy_rcv[ind].map);
  3332. scat++;
  3333. max_gs--;
  3334. }
  3335. for (i = 0; i < wr->num_sge; ++i)
  3336. __set_data_seg(scat + i, wr->sg_list + i);
  3337. if (i < max_gs) {
  3338. scat[i].byte_count = 0;
  3339. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  3340. scat[i].addr = 0;
  3341. }
  3342. qp->rq.wrid[ind] = wr->wr_id;
  3343. ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
  3344. }
  3345. out:
  3346. if (likely(nreq)) {
  3347. qp->rq.head += nreq;
  3348. /*
  3349. * Make sure that descriptors are written before
  3350. * doorbell record.
  3351. */
  3352. wmb();
  3353. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  3354. }
  3355. spin_unlock_irqrestore(&qp->rq.lock, flags);
  3356. return err;
  3357. }
  3358. int mlx4_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
  3359. const struct ib_recv_wr **bad_wr)
  3360. {
  3361. return _mlx4_ib_post_recv(ibqp, wr, bad_wr, false);
  3362. }
  3363. static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state)
  3364. {
  3365. switch (mlx4_state) {
  3366. case MLX4_QP_STATE_RST: return IB_QPS_RESET;
  3367. case MLX4_QP_STATE_INIT: return IB_QPS_INIT;
  3368. case MLX4_QP_STATE_RTR: return IB_QPS_RTR;
  3369. case MLX4_QP_STATE_RTS: return IB_QPS_RTS;
  3370. case MLX4_QP_STATE_SQ_DRAINING:
  3371. case MLX4_QP_STATE_SQD: return IB_QPS_SQD;
  3372. case MLX4_QP_STATE_SQER: return IB_QPS_SQE;
  3373. case MLX4_QP_STATE_ERR: return IB_QPS_ERR;
  3374. default: return -1;
  3375. }
  3376. }
  3377. static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state)
  3378. {
  3379. switch (mlx4_mig_state) {
  3380. case MLX4_QP_PM_ARMED: return IB_MIG_ARMED;
  3381. case MLX4_QP_PM_REARM: return IB_MIG_REARM;
  3382. case MLX4_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
  3383. default: return -1;
  3384. }
  3385. }
  3386. static int to_ib_qp_access_flags(int mlx4_flags)
  3387. {
  3388. int ib_flags = 0;
  3389. if (mlx4_flags & MLX4_QP_BIT_RRE)
  3390. ib_flags |= IB_ACCESS_REMOTE_READ;
  3391. if (mlx4_flags & MLX4_QP_BIT_RWE)
  3392. ib_flags |= IB_ACCESS_REMOTE_WRITE;
  3393. if (mlx4_flags & MLX4_QP_BIT_RAE)
  3394. ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
  3395. return ib_flags;
  3396. }
  3397. static void to_rdma_ah_attr(struct mlx4_ib_dev *ibdev,
  3398. struct rdma_ah_attr *ah_attr,
  3399. struct mlx4_qp_path *path)
  3400. {
  3401. struct mlx4_dev *dev = ibdev->dev;
  3402. u8 port_num = path->sched_queue & 0x40 ? 2 : 1;
  3403. memset(ah_attr, 0, sizeof(*ah_attr));
  3404. if (port_num == 0 || port_num > dev->caps.num_ports)
  3405. return;
  3406. ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port_num);
  3407. if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE)
  3408. rdma_ah_set_sl(ah_attr, ((path->sched_queue >> 3) & 0x7) |
  3409. ((path->sched_queue & 4) << 1));
  3410. else
  3411. rdma_ah_set_sl(ah_attr, (path->sched_queue >> 2) & 0xf);
  3412. rdma_ah_set_port_num(ah_attr, port_num);
  3413. rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
  3414. rdma_ah_set_path_bits(ah_attr, path->grh_mylmc & 0x7f);
  3415. rdma_ah_set_static_rate(ah_attr,
  3416. path->static_rate ? path->static_rate - 5 : 0);
  3417. if (path->grh_mylmc & (1 << 7)) {
  3418. rdma_ah_set_grh(ah_attr, NULL,
  3419. be32_to_cpu(path->tclass_flowlabel) & 0xfffff,
  3420. path->mgid_index,
  3421. path->hop_limit,
  3422. (be32_to_cpu(path->tclass_flowlabel)
  3423. >> 20) & 0xff);
  3424. rdma_ah_set_dgid_raw(ah_attr, path->rgid);
  3425. }
  3426. }
  3427. int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
  3428. struct ib_qp_init_attr *qp_init_attr)
  3429. {
  3430. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  3431. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  3432. struct mlx4_qp_context context;
  3433. int mlx4_state;
  3434. int err = 0;
  3435. if (ibqp->rwq_ind_tbl)
  3436. return -EOPNOTSUPP;
  3437. mutex_lock(&qp->mutex);
  3438. if (qp->state == IB_QPS_RESET) {
  3439. qp_attr->qp_state = IB_QPS_RESET;
  3440. goto done;
  3441. }
  3442. err = mlx4_qp_query(dev->dev, &qp->mqp, &context);
  3443. if (err) {
  3444. err = -EINVAL;
  3445. goto out;
  3446. }
  3447. mlx4_state = be32_to_cpu(context.flags) >> 28;
  3448. qp->state = to_ib_qp_state(mlx4_state);
  3449. qp_attr->qp_state = qp->state;
  3450. qp_attr->path_mtu = context.mtu_msgmax >> 5;
  3451. qp_attr->path_mig_state =
  3452. to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3);
  3453. qp_attr->qkey = be32_to_cpu(context.qkey);
  3454. qp_attr->rq_psn = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff;
  3455. qp_attr->sq_psn = be32_to_cpu(context.next_send_psn) & 0xffffff;
  3456. qp_attr->dest_qp_num = be32_to_cpu(context.remote_qpn) & 0xffffff;
  3457. qp_attr->qp_access_flags =
  3458. to_ib_qp_access_flags(be32_to_cpu(context.params2));
  3459. if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC ||
  3460. qp->ibqp.qp_type == IB_QPT_XRC_INI ||
  3461. qp->ibqp.qp_type == IB_QPT_XRC_TGT) {
  3462. to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context.pri_path);
  3463. to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context.alt_path);
  3464. qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f;
  3465. qp_attr->alt_port_num =
  3466. rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
  3467. }
  3468. qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f;
  3469. if (qp_attr->qp_state == IB_QPS_INIT)
  3470. qp_attr->port_num = qp->port;
  3471. else
  3472. qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1;
  3473. /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
  3474. qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING;
  3475. qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7);
  3476. qp_attr->max_dest_rd_atomic =
  3477. 1 << ((be32_to_cpu(context.params2) >> 21) & 0x7);
  3478. qp_attr->min_rnr_timer =
  3479. (be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f;
  3480. qp_attr->timeout = context.pri_path.ackto >> 3;
  3481. qp_attr->retry_cnt = (be32_to_cpu(context.params1) >> 16) & 0x7;
  3482. qp_attr->rnr_retry = (be32_to_cpu(context.params1) >> 13) & 0x7;
  3483. qp_attr->alt_timeout = context.alt_path.ackto >> 3;
  3484. done:
  3485. qp_attr->cur_qp_state = qp_attr->qp_state;
  3486. qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
  3487. qp_attr->cap.max_recv_sge = qp->rq.max_gs;
  3488. if (!ibqp->uobject) {
  3489. qp_attr->cap.max_send_wr = qp->sq.wqe_cnt;
  3490. qp_attr->cap.max_send_sge = qp->sq.max_gs;
  3491. } else {
  3492. qp_attr->cap.max_send_wr = 0;
  3493. qp_attr->cap.max_send_sge = 0;
  3494. }
  3495. /*
  3496. * We don't support inline sends for kernel QPs (yet), and we
  3497. * don't know what userspace's value should be.
  3498. */
  3499. qp_attr->cap.max_inline_data = 0;
  3500. qp_init_attr->cap = qp_attr->cap;
  3501. qp_init_attr->create_flags = 0;
  3502. if (qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK)
  3503. qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
  3504. if (qp->flags & MLX4_IB_QP_LSO)
  3505. qp_init_attr->create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;
  3506. if (qp->flags & MLX4_IB_QP_NETIF)
  3507. qp_init_attr->create_flags |= IB_QP_CREATE_NETIF_QP;
  3508. qp_init_attr->sq_sig_type =
  3509. qp->sq_signal_bits == cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) ?
  3510. IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
  3511. out:
  3512. mutex_unlock(&qp->mutex);
  3513. return err;
  3514. }
  3515. struct ib_wq *mlx4_ib_create_wq(struct ib_pd *pd,
  3516. struct ib_wq_init_attr *init_attr,
  3517. struct ib_udata *udata)
  3518. {
  3519. struct mlx4_dev *dev = to_mdev(pd->device)->dev;
  3520. struct ib_qp_init_attr ib_qp_init_attr = {};
  3521. struct mlx4_ib_qp *qp;
  3522. struct mlx4_ib_create_wq ucmd;
  3523. int err, required_cmd_sz;
  3524. if (!udata)
  3525. return ERR_PTR(-EINVAL);
  3526. required_cmd_sz = offsetof(typeof(ucmd), comp_mask) +
  3527. sizeof(ucmd.comp_mask);
  3528. if (udata->inlen < required_cmd_sz) {
  3529. pr_debug("invalid inlen\n");
  3530. return ERR_PTR(-EINVAL);
  3531. }
  3532. if (udata->inlen > sizeof(ucmd) &&
  3533. !ib_is_udata_cleared(udata, sizeof(ucmd),
  3534. udata->inlen - sizeof(ucmd))) {
  3535. pr_debug("inlen is not supported\n");
  3536. return ERR_PTR(-EOPNOTSUPP);
  3537. }
  3538. if (udata->outlen)
  3539. return ERR_PTR(-EOPNOTSUPP);
  3540. if (init_attr->wq_type != IB_WQT_RQ) {
  3541. pr_debug("unsupported wq type %d\n", init_attr->wq_type);
  3542. return ERR_PTR(-EOPNOTSUPP);
  3543. }
  3544. if (init_attr->create_flags & ~IB_WQ_FLAGS_SCATTER_FCS ||
  3545. !(dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP)) {
  3546. pr_debug("unsupported create_flags %u\n",
  3547. init_attr->create_flags);
  3548. return ERR_PTR(-EOPNOTSUPP);
  3549. }
  3550. qp = kzalloc(sizeof(*qp), GFP_KERNEL);
  3551. if (!qp)
  3552. return ERR_PTR(-ENOMEM);
  3553. mutex_init(&qp->mutex);
  3554. qp->pri.vid = 0xFFFF;
  3555. qp->alt.vid = 0xFFFF;
  3556. ib_qp_init_attr.qp_context = init_attr->wq_context;
  3557. ib_qp_init_attr.qp_type = IB_QPT_RAW_PACKET;
  3558. ib_qp_init_attr.cap.max_recv_wr = init_attr->max_wr;
  3559. ib_qp_init_attr.cap.max_recv_sge = init_attr->max_sge;
  3560. ib_qp_init_attr.recv_cq = init_attr->cq;
  3561. ib_qp_init_attr.send_cq = ib_qp_init_attr.recv_cq; /* Dummy CQ */
  3562. if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS)
  3563. ib_qp_init_attr.create_flags |= IB_QP_CREATE_SCATTER_FCS;
  3564. err = create_rq(pd, &ib_qp_init_attr, udata, qp);
  3565. if (err) {
  3566. kfree(qp);
  3567. return ERR_PTR(err);
  3568. }
  3569. qp->ibwq.event_handler = init_attr->event_handler;
  3570. qp->ibwq.wq_num = qp->mqp.qpn;
  3571. qp->ibwq.state = IB_WQS_RESET;
  3572. return &qp->ibwq;
  3573. }
  3574. static int ib_wq2qp_state(enum ib_wq_state state)
  3575. {
  3576. switch (state) {
  3577. case IB_WQS_RESET:
  3578. return IB_QPS_RESET;
  3579. case IB_WQS_RDY:
  3580. return IB_QPS_RTR;
  3581. default:
  3582. return IB_QPS_ERR;
  3583. }
  3584. }
  3585. static int _mlx4_ib_modify_wq(struct ib_wq *ibwq, enum ib_wq_state new_state,
  3586. struct ib_udata *udata)
  3587. {
  3588. struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq);
  3589. enum ib_qp_state qp_cur_state;
  3590. enum ib_qp_state qp_new_state;
  3591. int attr_mask;
  3592. int err;
  3593. /* ib_qp.state represents the WQ HW state while ib_wq.state represents
  3594. * the WQ logic state.
  3595. */
  3596. qp_cur_state = qp->state;
  3597. qp_new_state = ib_wq2qp_state(new_state);
  3598. if (ib_wq2qp_state(new_state) == qp_cur_state)
  3599. return 0;
  3600. if (new_state == IB_WQS_RDY) {
  3601. struct ib_qp_attr attr = {};
  3602. attr.port_num = qp->port;
  3603. attr_mask = IB_QP_PORT;
  3604. err = __mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, &attr,
  3605. attr_mask, IB_QPS_RESET, IB_QPS_INIT,
  3606. udata);
  3607. if (err) {
  3608. pr_debug("WQN=0x%06x failed to apply RST->INIT on the HW QP\n",
  3609. ibwq->wq_num);
  3610. return err;
  3611. }
  3612. qp_cur_state = IB_QPS_INIT;
  3613. }
  3614. attr_mask = 0;
  3615. err = __mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, NULL, attr_mask,
  3616. qp_cur_state, qp_new_state, udata);
  3617. if (err && (qp_cur_state == IB_QPS_INIT)) {
  3618. qp_new_state = IB_QPS_RESET;
  3619. if (__mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, NULL,
  3620. attr_mask, IB_QPS_INIT, IB_QPS_RESET,
  3621. udata)) {
  3622. pr_warn("WQN=0x%06x failed with reverting HW's resources failure\n",
  3623. ibwq->wq_num);
  3624. qp_new_state = IB_QPS_INIT;
  3625. }
  3626. }
  3627. qp->state = qp_new_state;
  3628. return err;
  3629. }
  3630. int mlx4_ib_modify_wq(struct ib_wq *ibwq, struct ib_wq_attr *wq_attr,
  3631. u32 wq_attr_mask, struct ib_udata *udata)
  3632. {
  3633. struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq);
  3634. struct mlx4_ib_modify_wq ucmd = {};
  3635. size_t required_cmd_sz;
  3636. enum ib_wq_state cur_state, new_state;
  3637. int err = 0;
  3638. required_cmd_sz = offsetof(typeof(ucmd), reserved) +
  3639. sizeof(ucmd.reserved);
  3640. if (udata->inlen < required_cmd_sz)
  3641. return -EINVAL;
  3642. if (udata->inlen > sizeof(ucmd) &&
  3643. !ib_is_udata_cleared(udata, sizeof(ucmd),
  3644. udata->inlen - sizeof(ucmd)))
  3645. return -EOPNOTSUPP;
  3646. if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
  3647. return -EFAULT;
  3648. if (ucmd.comp_mask || ucmd.reserved)
  3649. return -EOPNOTSUPP;
  3650. if (wq_attr_mask & IB_WQ_FLAGS)
  3651. return -EOPNOTSUPP;
  3652. cur_state = wq_attr->curr_wq_state;
  3653. new_state = wq_attr->wq_state;
  3654. if ((new_state == IB_WQS_RDY) && (cur_state == IB_WQS_ERR))
  3655. return -EINVAL;
  3656. if ((new_state == IB_WQS_ERR) && (cur_state == IB_WQS_RESET))
  3657. return -EINVAL;
  3658. /* Need to protect against the parent RSS which also may modify WQ
  3659. * state.
  3660. */
  3661. mutex_lock(&qp->mutex);
  3662. /* Can update HW state only if a RSS QP has already associated to this
  3663. * WQ, so we can apply its port on the WQ.
  3664. */
  3665. if (qp->rss_usecnt)
  3666. err = _mlx4_ib_modify_wq(ibwq, new_state, udata);
  3667. if (!err)
  3668. ibwq->state = new_state;
  3669. mutex_unlock(&qp->mutex);
  3670. return err;
  3671. }
  3672. int mlx4_ib_destroy_wq(struct ib_wq *ibwq, struct ib_udata *udata)
  3673. {
  3674. struct mlx4_ib_dev *dev = to_mdev(ibwq->device);
  3675. struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq);
  3676. if (qp->counter_index)
  3677. mlx4_ib_free_qp_counter(dev, qp);
  3678. destroy_qp_common(dev, qp, MLX4_IB_RWQ_SRC, udata);
  3679. kfree(qp);
  3680. return 0;
  3681. }
  3682. int mlx4_ib_create_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table,
  3683. struct ib_rwq_ind_table_init_attr *init_attr,
  3684. struct ib_udata *udata)
  3685. {
  3686. struct mlx4_ib_create_rwq_ind_tbl_resp resp = {};
  3687. unsigned int ind_tbl_size = 1 << init_attr->log_ind_tbl_size;
  3688. struct ib_device *device = rwq_ind_table->device;
  3689. unsigned int base_wqn;
  3690. size_t min_resp_len;
  3691. int i, err = 0;
  3692. if (udata->inlen > 0 &&
  3693. !ib_is_udata_cleared(udata, 0,
  3694. udata->inlen))
  3695. return -EOPNOTSUPP;
  3696. min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
  3697. if (udata->outlen && udata->outlen < min_resp_len)
  3698. return -EINVAL;
  3699. if (ind_tbl_size >
  3700. device->attrs.rss_caps.max_rwq_indirection_table_size) {
  3701. pr_debug("log_ind_tbl_size = %d is bigger than supported = %d\n",
  3702. ind_tbl_size,
  3703. device->attrs.rss_caps.max_rwq_indirection_table_size);
  3704. return -EINVAL;
  3705. }
  3706. base_wqn = init_attr->ind_tbl[0]->wq_num;
  3707. if (base_wqn % ind_tbl_size) {
  3708. pr_debug("WQN=0x%x isn't aligned with indirection table size\n",
  3709. base_wqn);
  3710. return -EINVAL;
  3711. }
  3712. for (i = 1; i < ind_tbl_size; i++) {
  3713. if (++base_wqn != init_attr->ind_tbl[i]->wq_num) {
  3714. pr_debug("indirection table's WQNs aren't consecutive\n");
  3715. return -EINVAL;
  3716. }
  3717. }
  3718. if (udata->outlen) {
  3719. resp.response_length = offsetof(typeof(resp), response_length) +
  3720. sizeof(resp.response_length);
  3721. err = ib_copy_to_udata(udata, &resp, resp.response_length);
  3722. }
  3723. return err;
  3724. }
  3725. struct mlx4_ib_drain_cqe {
  3726. struct ib_cqe cqe;
  3727. struct completion done;
  3728. };
  3729. static void mlx4_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
  3730. {
  3731. struct mlx4_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
  3732. struct mlx4_ib_drain_cqe,
  3733. cqe);
  3734. complete(&cqe->done);
  3735. }
  3736. /* This function returns only once the drained WR was completed */
  3737. static void handle_drain_completion(struct ib_cq *cq,
  3738. struct mlx4_ib_drain_cqe *sdrain,
  3739. struct mlx4_ib_dev *dev)
  3740. {
  3741. struct mlx4_dev *mdev = dev->dev;
  3742. if (cq->poll_ctx == IB_POLL_DIRECT) {
  3743. while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
  3744. ib_process_cq_direct(cq, -1);
  3745. return;
  3746. }
  3747. if (mdev->persist->state == MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  3748. struct mlx4_ib_cq *mcq = to_mcq(cq);
  3749. bool triggered = false;
  3750. unsigned long flags;
  3751. spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
  3752. /* Make sure that the CQ handler won't run if wasn't run yet */
  3753. if (!mcq->mcq.reset_notify_added)
  3754. mcq->mcq.reset_notify_added = 1;
  3755. else
  3756. triggered = true;
  3757. spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
  3758. if (triggered) {
  3759. /* Wait for any scheduled/running task to be ended */
  3760. switch (cq->poll_ctx) {
  3761. case IB_POLL_SOFTIRQ:
  3762. irq_poll_disable(&cq->iop);
  3763. irq_poll_enable(&cq->iop);
  3764. break;
  3765. case IB_POLL_WORKQUEUE:
  3766. cancel_work_sync(&cq->work);
  3767. break;
  3768. default:
  3769. WARN_ON_ONCE(1);
  3770. }
  3771. }
  3772. /* Run the CQ handler - this makes sure that the drain WR will
  3773. * be processed if wasn't processed yet.
  3774. */
  3775. mcq->mcq.comp(&mcq->mcq);
  3776. }
  3777. wait_for_completion(&sdrain->done);
  3778. }
  3779. void mlx4_ib_drain_sq(struct ib_qp *qp)
  3780. {
  3781. struct ib_cq *cq = qp->send_cq;
  3782. struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
  3783. struct mlx4_ib_drain_cqe sdrain;
  3784. const struct ib_send_wr *bad_swr;
  3785. struct ib_rdma_wr swr = {
  3786. .wr = {
  3787. .next = NULL,
  3788. { .wr_cqe = &sdrain.cqe, },
  3789. .opcode = IB_WR_RDMA_WRITE,
  3790. },
  3791. };
  3792. int ret;
  3793. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  3794. struct mlx4_dev *mdev = dev->dev;
  3795. ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
  3796. if (ret && mdev->persist->state != MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  3797. WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
  3798. return;
  3799. }
  3800. sdrain.cqe.done = mlx4_ib_drain_qp_done;
  3801. init_completion(&sdrain.done);
  3802. ret = _mlx4_ib_post_send(qp, &swr.wr, &bad_swr, true);
  3803. if (ret) {
  3804. WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
  3805. return;
  3806. }
  3807. handle_drain_completion(cq, &sdrain, dev);
  3808. }
  3809. void mlx4_ib_drain_rq(struct ib_qp *qp)
  3810. {
  3811. struct ib_cq *cq = qp->recv_cq;
  3812. struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
  3813. struct mlx4_ib_drain_cqe rdrain;
  3814. struct ib_recv_wr rwr = {};
  3815. const struct ib_recv_wr *bad_rwr;
  3816. int ret;
  3817. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  3818. struct mlx4_dev *mdev = dev->dev;
  3819. ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
  3820. if (ret && mdev->persist->state != MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  3821. WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
  3822. return;
  3823. }
  3824. rwr.wr_cqe = &rdrain.cqe;
  3825. rdrain.cqe.done = mlx4_ib_drain_qp_done;
  3826. init_completion(&rdrain.done);
  3827. ret = _mlx4_ib_post_recv(qp, &rwr, &bad_rwr, true);
  3828. if (ret) {
  3829. WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
  3830. return;
  3831. }
  3832. handle_drain_completion(cq, &rdrain, dev);
  3833. }