qp.c 100 KB

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