qp.c 123 KB

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