qp.c 123 KB

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