qp.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876
  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/slab.h>
  35. #include <linux/netdevice.h>
  36. #include <rdma/ib_cache.h>
  37. #include <rdma/ib_pack.h>
  38. #include <rdma/ib_addr.h>
  39. #include <rdma/ib_mad.h>
  40. #include <linux/mlx4/qp.h>
  41. #include "mlx4_ib.h"
  42. #include "user.h"
  43. enum {
  44. MLX4_IB_ACK_REQ_FREQ = 8,
  45. };
  46. enum {
  47. MLX4_IB_DEFAULT_SCHED_QUEUE = 0x83,
  48. MLX4_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
  49. MLX4_IB_LINK_TYPE_IB = 0,
  50. MLX4_IB_LINK_TYPE_ETH = 1
  51. };
  52. enum {
  53. /*
  54. * Largest possible UD header: send with GRH and immediate
  55. * data plus 18 bytes for an Ethernet header with VLAN/802.1Q
  56. * tag. (LRH would only use 8 bytes, so Ethernet is the
  57. * biggest case)
  58. */
  59. MLX4_IB_UD_HEADER_SIZE = 82,
  60. MLX4_IB_LSO_HEADER_SPARE = 128,
  61. };
  62. enum {
  63. MLX4_IB_IBOE_ETHERTYPE = 0x8915
  64. };
  65. struct mlx4_ib_sqp {
  66. struct mlx4_ib_qp qp;
  67. int pkey_index;
  68. u32 qkey;
  69. u32 send_psn;
  70. struct ib_ud_header ud_header;
  71. u8 header_buf[MLX4_IB_UD_HEADER_SIZE];
  72. };
  73. enum {
  74. MLX4_IB_MIN_SQ_STRIDE = 6,
  75. MLX4_IB_CACHE_LINE_SIZE = 64,
  76. };
  77. enum {
  78. MLX4_RAW_QP_MTU = 7,
  79. MLX4_RAW_QP_MSGMAX = 31,
  80. };
  81. #ifndef ETH_ALEN
  82. #define ETH_ALEN 6
  83. #endif
  84. static inline u64 mlx4_mac_to_u64(u8 *addr)
  85. {
  86. u64 mac = 0;
  87. int i;
  88. for (i = 0; i < ETH_ALEN; i++) {
  89. mac <<= 8;
  90. mac |= addr[i];
  91. }
  92. return mac;
  93. }
  94. static const __be32 mlx4_ib_opcode[] = {
  95. [IB_WR_SEND] = cpu_to_be32(MLX4_OPCODE_SEND),
  96. [IB_WR_LSO] = cpu_to_be32(MLX4_OPCODE_LSO),
  97. [IB_WR_SEND_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_SEND_IMM),
  98. [IB_WR_RDMA_WRITE] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE),
  99. [IB_WR_RDMA_WRITE_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE_IMM),
  100. [IB_WR_RDMA_READ] = cpu_to_be32(MLX4_OPCODE_RDMA_READ),
  101. [IB_WR_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_ATOMIC_CS),
  102. [IB_WR_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_ATOMIC_FA),
  103. [IB_WR_SEND_WITH_INV] = cpu_to_be32(MLX4_OPCODE_SEND_INVAL),
  104. [IB_WR_LOCAL_INV] = cpu_to_be32(MLX4_OPCODE_LOCAL_INVAL),
  105. [IB_WR_FAST_REG_MR] = cpu_to_be32(MLX4_OPCODE_FMR),
  106. [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_CS),
  107. [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_FA),
  108. [IB_WR_BIND_MW] = cpu_to_be32(MLX4_OPCODE_BIND_MW),
  109. };
  110. static struct mlx4_ib_sqp *to_msqp(struct mlx4_ib_qp *mqp)
  111. {
  112. return container_of(mqp, struct mlx4_ib_sqp, qp);
  113. }
  114. static int is_tunnel_qp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  115. {
  116. if (!mlx4_is_master(dev->dev))
  117. return 0;
  118. return qp->mqp.qpn >= dev->dev->phys_caps.base_tunnel_sqpn &&
  119. qp->mqp.qpn < dev->dev->phys_caps.base_tunnel_sqpn +
  120. 8 * MLX4_MFUNC_MAX;
  121. }
  122. static int is_sqp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  123. {
  124. int proxy_sqp = 0;
  125. int real_sqp = 0;
  126. int i;
  127. /* PPF or Native -- real SQP */
  128. real_sqp = ((mlx4_is_master(dev->dev) || !mlx4_is_mfunc(dev->dev)) &&
  129. qp->mqp.qpn >= dev->dev->phys_caps.base_sqpn &&
  130. qp->mqp.qpn <= dev->dev->phys_caps.base_sqpn + 3);
  131. if (real_sqp)
  132. return 1;
  133. /* VF or PF -- proxy SQP */
  134. if (mlx4_is_mfunc(dev->dev)) {
  135. for (i = 0; i < dev->dev->caps.num_ports; i++) {
  136. if (qp->mqp.qpn == dev->dev->caps.qp0_proxy[i] ||
  137. qp->mqp.qpn == dev->dev->caps.qp1_proxy[i]) {
  138. proxy_sqp = 1;
  139. break;
  140. }
  141. }
  142. }
  143. return proxy_sqp;
  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->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->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_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. {
  379. int s;
  380. /* Sanity check SQ size before proceeding */
  381. if (cap->max_send_wr > (dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE) ||
  382. cap->max_send_sge > min(dev->dev->caps.max_sq_sg, dev->dev->caps.max_rq_sg) ||
  383. cap->max_inline_data + send_wqe_overhead(type, qp->flags) +
  384. sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz)
  385. return -EINVAL;
  386. /*
  387. * For MLX transport we need 2 extra S/G entries:
  388. * one for the header and one for the checksum at the end
  389. */
  390. if ((type == MLX4_IB_QPT_SMI || type == MLX4_IB_QPT_GSI ||
  391. type & (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) &&
  392. cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg)
  393. return -EINVAL;
  394. s = max(cap->max_send_sge * sizeof (struct mlx4_wqe_data_seg),
  395. cap->max_inline_data + sizeof (struct mlx4_wqe_inline_seg)) +
  396. send_wqe_overhead(type, qp->flags);
  397. if (s > dev->dev->caps.max_sq_desc_sz)
  398. return -EINVAL;
  399. /*
  400. * Hermon supports shrinking WQEs, such that a single work
  401. * request can include multiple units of 1 << wqe_shift. This
  402. * way, work requests can differ in size, and do not have to
  403. * be a power of 2 in size, saving memory and speeding up send
  404. * WR posting. Unfortunately, if we do this then the
  405. * wqe_index field in CQEs can't be used to look up the WR ID
  406. * anymore, so we do this only if selective signaling is off.
  407. *
  408. * Further, on 32-bit platforms, we can't use vmap() to make
  409. * the QP buffer virtually contiguous. Thus we have to use
  410. * constant-sized WRs to make sure a WR is always fully within
  411. * a single page-sized chunk.
  412. *
  413. * Finally, we use NOP work requests to pad the end of the
  414. * work queue, to avoid wrap-around in the middle of WR. We
  415. * set NEC bit to avoid getting completions with error for
  416. * these NOP WRs, but since NEC is only supported starting
  417. * with firmware 2.2.232, we use constant-sized WRs for older
  418. * firmware.
  419. *
  420. * And, since MLX QPs only support SEND, we use constant-sized
  421. * WRs in this case.
  422. *
  423. * We look for the smallest value of wqe_shift such that the
  424. * resulting number of wqes does not exceed device
  425. * capabilities.
  426. *
  427. * We set WQE size to at least 64 bytes, this way stamping
  428. * invalidates each WQE.
  429. */
  430. if (dev->dev->caps.fw_ver >= MLX4_FW_VER_WQE_CTRL_NEC &&
  431. qp->sq_signal_bits && BITS_PER_LONG == 64 &&
  432. type != MLX4_IB_QPT_SMI && type != MLX4_IB_QPT_GSI &&
  433. !(type & (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_PROXY_SMI |
  434. MLX4_IB_QPT_PROXY_GSI | MLX4_IB_QPT_TUN_SMI_OWNER)))
  435. qp->sq.wqe_shift = ilog2(64);
  436. else
  437. qp->sq.wqe_shift = ilog2(roundup_pow_of_two(s));
  438. for (;;) {
  439. qp->sq_max_wqes_per_wr = DIV_ROUND_UP(s, 1U << qp->sq.wqe_shift);
  440. /*
  441. * We need to leave 2 KB + 1 WR of headroom in the SQ to
  442. * allow HW to prefetch.
  443. */
  444. qp->sq_spare_wqes = (2048 >> qp->sq.wqe_shift) + qp->sq_max_wqes_per_wr;
  445. qp->sq.wqe_cnt = roundup_pow_of_two(cap->max_send_wr *
  446. qp->sq_max_wqes_per_wr +
  447. qp->sq_spare_wqes);
  448. if (qp->sq.wqe_cnt <= dev->dev->caps.max_wqes)
  449. break;
  450. if (qp->sq_max_wqes_per_wr <= 1)
  451. return -EINVAL;
  452. ++qp->sq.wqe_shift;
  453. }
  454. qp->sq.max_gs = (min(dev->dev->caps.max_sq_desc_sz,
  455. (qp->sq_max_wqes_per_wr << qp->sq.wqe_shift)) -
  456. send_wqe_overhead(type, qp->flags)) /
  457. sizeof (struct mlx4_wqe_data_seg);
  458. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  459. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  460. if (qp->rq.wqe_shift > qp->sq.wqe_shift) {
  461. qp->rq.offset = 0;
  462. qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
  463. } else {
  464. qp->rq.offset = qp->sq.wqe_cnt << qp->sq.wqe_shift;
  465. qp->sq.offset = 0;
  466. }
  467. cap->max_send_wr = qp->sq.max_post =
  468. (qp->sq.wqe_cnt - qp->sq_spare_wqes) / qp->sq_max_wqes_per_wr;
  469. cap->max_send_sge = min(qp->sq.max_gs,
  470. min(dev->dev->caps.max_sq_sg,
  471. dev->dev->caps.max_rq_sg));
  472. /* We don't support inline sends for kernel QPs (yet) */
  473. cap->max_inline_data = 0;
  474. return 0;
  475. }
  476. static int set_user_sq_size(struct mlx4_ib_dev *dev,
  477. struct mlx4_ib_qp *qp,
  478. struct mlx4_ib_create_qp *ucmd)
  479. {
  480. /* Sanity check SQ size before proceeding */
  481. if ((1 << ucmd->log_sq_bb_count) > dev->dev->caps.max_wqes ||
  482. ucmd->log_sq_stride >
  483. ilog2(roundup_pow_of_two(dev->dev->caps.max_sq_desc_sz)) ||
  484. ucmd->log_sq_stride < MLX4_IB_MIN_SQ_STRIDE)
  485. return -EINVAL;
  486. qp->sq.wqe_cnt = 1 << ucmd->log_sq_bb_count;
  487. qp->sq.wqe_shift = ucmd->log_sq_stride;
  488. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  489. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  490. return 0;
  491. }
  492. static int alloc_proxy_bufs(struct ib_device *dev, struct mlx4_ib_qp *qp)
  493. {
  494. int i;
  495. qp->sqp_proxy_rcv =
  496. kmalloc(sizeof (struct mlx4_ib_buf) * qp->rq.wqe_cnt,
  497. GFP_KERNEL);
  498. if (!qp->sqp_proxy_rcv)
  499. return -ENOMEM;
  500. for (i = 0; i < qp->rq.wqe_cnt; i++) {
  501. qp->sqp_proxy_rcv[i].addr =
  502. kmalloc(sizeof (struct mlx4_ib_proxy_sqp_hdr),
  503. GFP_KERNEL);
  504. if (!qp->sqp_proxy_rcv[i].addr)
  505. goto err;
  506. qp->sqp_proxy_rcv[i].map =
  507. ib_dma_map_single(dev, qp->sqp_proxy_rcv[i].addr,
  508. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  509. DMA_FROM_DEVICE);
  510. }
  511. return 0;
  512. err:
  513. while (i > 0) {
  514. --i;
  515. ib_dma_unmap_single(dev, qp->sqp_proxy_rcv[i].map,
  516. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  517. DMA_FROM_DEVICE);
  518. kfree(qp->sqp_proxy_rcv[i].addr);
  519. }
  520. kfree(qp->sqp_proxy_rcv);
  521. qp->sqp_proxy_rcv = NULL;
  522. return -ENOMEM;
  523. }
  524. static void free_proxy_bufs(struct ib_device *dev, struct mlx4_ib_qp *qp)
  525. {
  526. int i;
  527. for (i = 0; i < qp->rq.wqe_cnt; i++) {
  528. ib_dma_unmap_single(dev, qp->sqp_proxy_rcv[i].map,
  529. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  530. DMA_FROM_DEVICE);
  531. kfree(qp->sqp_proxy_rcv[i].addr);
  532. }
  533. kfree(qp->sqp_proxy_rcv);
  534. }
  535. static int qp_has_rq(struct ib_qp_init_attr *attr)
  536. {
  537. if (attr->qp_type == IB_QPT_XRC_INI || attr->qp_type == IB_QPT_XRC_TGT)
  538. return 0;
  539. return !attr->srq;
  540. }
  541. static int create_qp_common(struct mlx4_ib_dev *dev, struct ib_pd *pd,
  542. struct ib_qp_init_attr *init_attr,
  543. struct ib_udata *udata, int sqpn, struct mlx4_ib_qp **caller_qp)
  544. {
  545. int qpn;
  546. int err;
  547. struct mlx4_ib_sqp *sqp;
  548. struct mlx4_ib_qp *qp;
  549. enum mlx4_ib_qp_type qp_type = (enum mlx4_ib_qp_type) init_attr->qp_type;
  550. /* When tunneling special qps, we use a plain UD qp */
  551. if (sqpn) {
  552. if (mlx4_is_mfunc(dev->dev) &&
  553. (!mlx4_is_master(dev->dev) ||
  554. !(init_attr->create_flags & MLX4_IB_SRIOV_SQP))) {
  555. if (init_attr->qp_type == IB_QPT_GSI)
  556. qp_type = MLX4_IB_QPT_PROXY_GSI;
  557. else if (mlx4_is_master(dev->dev))
  558. qp_type = MLX4_IB_QPT_PROXY_SMI_OWNER;
  559. else
  560. qp_type = MLX4_IB_QPT_PROXY_SMI;
  561. }
  562. qpn = sqpn;
  563. /* add extra sg entry for tunneling */
  564. init_attr->cap.max_recv_sge++;
  565. } else if (init_attr->create_flags & MLX4_IB_SRIOV_TUNNEL_QP) {
  566. struct mlx4_ib_qp_tunnel_init_attr *tnl_init =
  567. container_of(init_attr,
  568. struct mlx4_ib_qp_tunnel_init_attr, init_attr);
  569. if ((tnl_init->proxy_qp_type != IB_QPT_SMI &&
  570. tnl_init->proxy_qp_type != IB_QPT_GSI) ||
  571. !mlx4_is_master(dev->dev))
  572. return -EINVAL;
  573. if (tnl_init->proxy_qp_type == IB_QPT_GSI)
  574. qp_type = MLX4_IB_QPT_TUN_GSI;
  575. else if (tnl_init->slave == mlx4_master_func_num(dev->dev))
  576. qp_type = MLX4_IB_QPT_TUN_SMI_OWNER;
  577. else
  578. qp_type = MLX4_IB_QPT_TUN_SMI;
  579. /* we are definitely in the PPF here, since we are creating
  580. * tunnel QPs. base_tunnel_sqpn is therefore valid. */
  581. qpn = dev->dev->phys_caps.base_tunnel_sqpn + 8 * tnl_init->slave
  582. + tnl_init->proxy_qp_type * 2 + tnl_init->port - 1;
  583. sqpn = qpn;
  584. }
  585. if (!*caller_qp) {
  586. if (qp_type == MLX4_IB_QPT_SMI || qp_type == MLX4_IB_QPT_GSI ||
  587. (qp_type & (MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_SMI_OWNER |
  588. MLX4_IB_QPT_PROXY_GSI | MLX4_IB_QPT_TUN_SMI_OWNER))) {
  589. sqp = kzalloc(sizeof (struct mlx4_ib_sqp), GFP_KERNEL);
  590. if (!sqp)
  591. return -ENOMEM;
  592. qp = &sqp->qp;
  593. } else {
  594. qp = kzalloc(sizeof (struct mlx4_ib_qp), GFP_KERNEL);
  595. if (!qp)
  596. return -ENOMEM;
  597. }
  598. } else
  599. qp = *caller_qp;
  600. qp->mlx4_ib_qp_type = qp_type;
  601. mutex_init(&qp->mutex);
  602. spin_lock_init(&qp->sq.lock);
  603. spin_lock_init(&qp->rq.lock);
  604. INIT_LIST_HEAD(&qp->gid_list);
  605. INIT_LIST_HEAD(&qp->steering_rules);
  606. qp->state = IB_QPS_RESET;
  607. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  608. qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  609. err = set_rq_size(dev, &init_attr->cap, !!pd->uobject, qp_has_rq(init_attr), qp);
  610. if (err)
  611. goto err;
  612. if (pd->uobject) {
  613. struct mlx4_ib_create_qp ucmd;
  614. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  615. err = -EFAULT;
  616. goto err;
  617. }
  618. qp->sq_no_prefetch = ucmd.sq_no_prefetch;
  619. err = set_user_sq_size(dev, qp, &ucmd);
  620. if (err)
  621. goto err;
  622. qp->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
  623. qp->buf_size, 0, 0);
  624. if (IS_ERR(qp->umem)) {
  625. err = PTR_ERR(qp->umem);
  626. goto err;
  627. }
  628. err = mlx4_mtt_init(dev->dev, ib_umem_page_count(qp->umem),
  629. ilog2(qp->umem->page_size), &qp->mtt);
  630. if (err)
  631. goto err_buf;
  632. err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
  633. if (err)
  634. goto err_mtt;
  635. if (qp_has_rq(init_attr)) {
  636. err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
  637. ucmd.db_addr, &qp->db);
  638. if (err)
  639. goto err_mtt;
  640. }
  641. } else {
  642. qp->sq_no_prefetch = 0;
  643. if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
  644. qp->flags |= MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
  645. if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
  646. qp->flags |= MLX4_IB_QP_LSO;
  647. if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) {
  648. if (dev->steering_support ==
  649. MLX4_STEERING_MODE_DEVICE_MANAGED)
  650. qp->flags |= MLX4_IB_QP_NETIF;
  651. else
  652. goto err;
  653. }
  654. err = set_kernel_sq_size(dev, &init_attr->cap, qp_type, qp);
  655. if (err)
  656. goto err;
  657. if (qp_has_rq(init_attr)) {
  658. err = mlx4_db_alloc(dev->dev, &qp->db, 0);
  659. if (err)
  660. goto err;
  661. *qp->db.db = 0;
  662. }
  663. if (mlx4_buf_alloc(dev->dev, qp->buf_size, PAGE_SIZE * 2, &qp->buf)) {
  664. err = -ENOMEM;
  665. goto err_db;
  666. }
  667. err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift,
  668. &qp->mtt);
  669. if (err)
  670. goto err_buf;
  671. err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf);
  672. if (err)
  673. goto err_mtt;
  674. qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof (u64), GFP_KERNEL);
  675. qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof (u64), GFP_KERNEL);
  676. if (!qp->sq.wrid || !qp->rq.wrid) {
  677. err = -ENOMEM;
  678. goto err_wrid;
  679. }
  680. }
  681. if (sqpn) {
  682. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  683. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
  684. if (alloc_proxy_bufs(pd->device, qp)) {
  685. err = -ENOMEM;
  686. goto err_wrid;
  687. }
  688. }
  689. } else {
  690. /* Raw packet QPNs must be aligned to 8 bits. If not, the WQE
  691. * BlueFlame setup flow wrongly causes VLAN insertion. */
  692. if (init_attr->qp_type == IB_QPT_RAW_PACKET)
  693. err = mlx4_qp_reserve_range(dev->dev, 1, 1 << 8, &qpn);
  694. else
  695. if (qp->flags & MLX4_IB_QP_NETIF)
  696. err = mlx4_ib_steer_qp_alloc(dev, 1, &qpn);
  697. else
  698. err = mlx4_qp_reserve_range(dev->dev, 1, 1,
  699. &qpn);
  700. if (err)
  701. goto err_proxy;
  702. }
  703. err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp);
  704. if (err)
  705. goto err_qpn;
  706. if (init_attr->qp_type == IB_QPT_XRC_TGT)
  707. qp->mqp.qpn |= (1 << 23);
  708. /*
  709. * Hardware wants QPN written in big-endian order (after
  710. * shifting) for send doorbell. Precompute this value to save
  711. * a little bit when posting sends.
  712. */
  713. qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
  714. qp->mqp.event = mlx4_ib_qp_event;
  715. if (!*caller_qp)
  716. *caller_qp = qp;
  717. return 0;
  718. err_qpn:
  719. if (!sqpn) {
  720. if (qp->flags & MLX4_IB_QP_NETIF)
  721. mlx4_ib_steer_qp_free(dev, qpn, 1);
  722. else
  723. mlx4_qp_release_range(dev->dev, qpn, 1);
  724. }
  725. err_proxy:
  726. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI)
  727. free_proxy_bufs(pd->device, qp);
  728. err_wrid:
  729. if (pd->uobject) {
  730. if (qp_has_rq(init_attr))
  731. mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &qp->db);
  732. } else {
  733. kfree(qp->sq.wrid);
  734. kfree(qp->rq.wrid);
  735. }
  736. err_mtt:
  737. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  738. err_buf:
  739. if (pd->uobject)
  740. ib_umem_release(qp->umem);
  741. else
  742. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  743. err_db:
  744. if (!pd->uobject && qp_has_rq(init_attr))
  745. mlx4_db_free(dev->dev, &qp->db);
  746. err:
  747. if (!*caller_qp)
  748. kfree(qp);
  749. return err;
  750. }
  751. static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
  752. {
  753. switch (state) {
  754. case IB_QPS_RESET: return MLX4_QP_STATE_RST;
  755. case IB_QPS_INIT: return MLX4_QP_STATE_INIT;
  756. case IB_QPS_RTR: return MLX4_QP_STATE_RTR;
  757. case IB_QPS_RTS: return MLX4_QP_STATE_RTS;
  758. case IB_QPS_SQD: return MLX4_QP_STATE_SQD;
  759. case IB_QPS_SQE: return MLX4_QP_STATE_SQER;
  760. case IB_QPS_ERR: return MLX4_QP_STATE_ERR;
  761. default: return -1;
  762. }
  763. }
  764. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  765. __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
  766. {
  767. if (send_cq == recv_cq) {
  768. spin_lock_irq(&send_cq->lock);
  769. __acquire(&recv_cq->lock);
  770. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  771. spin_lock_irq(&send_cq->lock);
  772. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  773. } else {
  774. spin_lock_irq(&recv_cq->lock);
  775. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  776. }
  777. }
  778. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  779. __releases(&send_cq->lock) __releases(&recv_cq->lock)
  780. {
  781. if (send_cq == recv_cq) {
  782. __release(&recv_cq->lock);
  783. spin_unlock_irq(&send_cq->lock);
  784. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  785. spin_unlock(&recv_cq->lock);
  786. spin_unlock_irq(&send_cq->lock);
  787. } else {
  788. spin_unlock(&send_cq->lock);
  789. spin_unlock_irq(&recv_cq->lock);
  790. }
  791. }
  792. static void del_gid_entries(struct mlx4_ib_qp *qp)
  793. {
  794. struct mlx4_ib_gid_entry *ge, *tmp;
  795. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  796. list_del(&ge->list);
  797. kfree(ge);
  798. }
  799. }
  800. static struct mlx4_ib_pd *get_pd(struct mlx4_ib_qp *qp)
  801. {
  802. if (qp->ibqp.qp_type == IB_QPT_XRC_TGT)
  803. return to_mpd(to_mxrcd(qp->ibqp.xrcd)->pd);
  804. else
  805. return to_mpd(qp->ibqp.pd);
  806. }
  807. static void get_cqs(struct mlx4_ib_qp *qp,
  808. struct mlx4_ib_cq **send_cq, struct mlx4_ib_cq **recv_cq)
  809. {
  810. switch (qp->ibqp.qp_type) {
  811. case IB_QPT_XRC_TGT:
  812. *send_cq = to_mcq(to_mxrcd(qp->ibqp.xrcd)->cq);
  813. *recv_cq = *send_cq;
  814. break;
  815. case IB_QPT_XRC_INI:
  816. *send_cq = to_mcq(qp->ibqp.send_cq);
  817. *recv_cq = *send_cq;
  818. break;
  819. default:
  820. *send_cq = to_mcq(qp->ibqp.send_cq);
  821. *recv_cq = to_mcq(qp->ibqp.recv_cq);
  822. break;
  823. }
  824. }
  825. static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
  826. int is_user)
  827. {
  828. struct mlx4_ib_cq *send_cq, *recv_cq;
  829. if (qp->state != IB_QPS_RESET)
  830. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  831. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  832. pr_warn("modify QP %06x to RESET failed.\n",
  833. qp->mqp.qpn);
  834. get_cqs(qp, &send_cq, &recv_cq);
  835. mlx4_ib_lock_cqs(send_cq, recv_cq);
  836. if (!is_user) {
  837. __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  838. qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
  839. if (send_cq != recv_cq)
  840. __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  841. }
  842. mlx4_qp_remove(dev->dev, &qp->mqp);
  843. mlx4_ib_unlock_cqs(send_cq, recv_cq);
  844. mlx4_qp_free(dev->dev, &qp->mqp);
  845. if (!is_sqp(dev, qp) && !is_tunnel_qp(dev, qp)) {
  846. if (qp->flags & MLX4_IB_QP_NETIF)
  847. mlx4_ib_steer_qp_free(dev, qp->mqp.qpn, 1);
  848. else
  849. mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1);
  850. }
  851. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  852. if (is_user) {
  853. if (qp->rq.wqe_cnt)
  854. mlx4_ib_db_unmap_user(to_mucontext(qp->ibqp.uobject->context),
  855. &qp->db);
  856. ib_umem_release(qp->umem);
  857. } else {
  858. kfree(qp->sq.wrid);
  859. kfree(qp->rq.wrid);
  860. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  861. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI))
  862. free_proxy_bufs(&dev->ib_dev, qp);
  863. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  864. if (qp->rq.wqe_cnt)
  865. mlx4_db_free(dev->dev, &qp->db);
  866. }
  867. del_gid_entries(qp);
  868. }
  869. static u32 get_sqp_num(struct mlx4_ib_dev *dev, struct ib_qp_init_attr *attr)
  870. {
  871. /* Native or PPF */
  872. if (!mlx4_is_mfunc(dev->dev) ||
  873. (mlx4_is_master(dev->dev) &&
  874. attr->create_flags & MLX4_IB_SRIOV_SQP)) {
  875. return dev->dev->phys_caps.base_sqpn +
  876. (attr->qp_type == IB_QPT_SMI ? 0 : 2) +
  877. attr->port_num - 1;
  878. }
  879. /* PF or VF -- creating proxies */
  880. if (attr->qp_type == IB_QPT_SMI)
  881. return dev->dev->caps.qp0_proxy[attr->port_num - 1];
  882. else
  883. return dev->dev->caps.qp1_proxy[attr->port_num - 1];
  884. }
  885. struct ib_qp *mlx4_ib_create_qp(struct ib_pd *pd,
  886. struct ib_qp_init_attr *init_attr,
  887. struct ib_udata *udata)
  888. {
  889. struct mlx4_ib_qp *qp = NULL;
  890. int err;
  891. u16 xrcdn = 0;
  892. /*
  893. * We only support LSO, vendor flag1, and multicast loopback blocking,
  894. * and only for kernel UD QPs.
  895. */
  896. if (init_attr->create_flags & ~(MLX4_IB_QP_LSO |
  897. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK |
  898. MLX4_IB_SRIOV_TUNNEL_QP |
  899. MLX4_IB_SRIOV_SQP |
  900. MLX4_IB_QP_NETIF))
  901. return ERR_PTR(-EINVAL);
  902. if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) {
  903. if (init_attr->qp_type != IB_QPT_UD)
  904. return ERR_PTR(-EINVAL);
  905. }
  906. if (init_attr->create_flags &&
  907. (udata ||
  908. ((init_attr->create_flags & ~MLX4_IB_SRIOV_SQP) &&
  909. init_attr->qp_type != IB_QPT_UD) ||
  910. ((init_attr->create_flags & MLX4_IB_SRIOV_SQP) &&
  911. init_attr->qp_type > IB_QPT_GSI)))
  912. return ERR_PTR(-EINVAL);
  913. switch (init_attr->qp_type) {
  914. case IB_QPT_XRC_TGT:
  915. pd = to_mxrcd(init_attr->xrcd)->pd;
  916. xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
  917. init_attr->send_cq = to_mxrcd(init_attr->xrcd)->cq;
  918. /* fall through */
  919. case IB_QPT_XRC_INI:
  920. if (!(to_mdev(pd->device)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
  921. return ERR_PTR(-ENOSYS);
  922. init_attr->recv_cq = init_attr->send_cq;
  923. /* fall through */
  924. case IB_QPT_RC:
  925. case IB_QPT_UC:
  926. case IB_QPT_RAW_PACKET:
  927. qp = kzalloc(sizeof *qp, GFP_KERNEL);
  928. if (!qp)
  929. return ERR_PTR(-ENOMEM);
  930. /* fall through */
  931. case IB_QPT_UD:
  932. {
  933. err = create_qp_common(to_mdev(pd->device), pd, init_attr,
  934. udata, 0, &qp);
  935. if (err)
  936. return ERR_PTR(err);
  937. qp->ibqp.qp_num = qp->mqp.qpn;
  938. qp->xrcdn = xrcdn;
  939. break;
  940. }
  941. case IB_QPT_SMI:
  942. case IB_QPT_GSI:
  943. {
  944. /* Userspace is not allowed to create special QPs: */
  945. if (udata)
  946. return ERR_PTR(-EINVAL);
  947. err = create_qp_common(to_mdev(pd->device), pd, init_attr, udata,
  948. get_sqp_num(to_mdev(pd->device), init_attr),
  949. &qp);
  950. if (err)
  951. return ERR_PTR(err);
  952. qp->port = init_attr->port_num;
  953. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
  954. break;
  955. }
  956. default:
  957. /* Don't support raw QPs */
  958. return ERR_PTR(-EINVAL);
  959. }
  960. return &qp->ibqp;
  961. }
  962. int mlx4_ib_destroy_qp(struct ib_qp *qp)
  963. {
  964. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  965. struct mlx4_ib_qp *mqp = to_mqp(qp);
  966. struct mlx4_ib_pd *pd;
  967. if (is_qp0(dev, mqp))
  968. mlx4_CLOSE_PORT(dev->dev, mqp->port);
  969. pd = get_pd(mqp);
  970. destroy_qp_common(dev, mqp, !!pd->ibpd.uobject);
  971. if (is_sqp(dev, mqp))
  972. kfree(to_msqp(mqp));
  973. else
  974. kfree(mqp);
  975. return 0;
  976. }
  977. static int to_mlx4_st(struct mlx4_ib_dev *dev, enum mlx4_ib_qp_type type)
  978. {
  979. switch (type) {
  980. case MLX4_IB_QPT_RC: return MLX4_QP_ST_RC;
  981. case MLX4_IB_QPT_UC: return MLX4_QP_ST_UC;
  982. case MLX4_IB_QPT_UD: return MLX4_QP_ST_UD;
  983. case MLX4_IB_QPT_XRC_INI:
  984. case MLX4_IB_QPT_XRC_TGT: return MLX4_QP_ST_XRC;
  985. case MLX4_IB_QPT_SMI:
  986. case MLX4_IB_QPT_GSI:
  987. case MLX4_IB_QPT_RAW_PACKET: return MLX4_QP_ST_MLX;
  988. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  989. case MLX4_IB_QPT_TUN_SMI_OWNER: return (mlx4_is_mfunc(dev->dev) ?
  990. MLX4_QP_ST_MLX : -1);
  991. case MLX4_IB_QPT_PROXY_SMI:
  992. case MLX4_IB_QPT_TUN_SMI:
  993. case MLX4_IB_QPT_PROXY_GSI:
  994. case MLX4_IB_QPT_TUN_GSI: return (mlx4_is_mfunc(dev->dev) ?
  995. MLX4_QP_ST_UD : -1);
  996. default: return -1;
  997. }
  998. }
  999. static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
  1000. int attr_mask)
  1001. {
  1002. u8 dest_rd_atomic;
  1003. u32 access_flags;
  1004. u32 hw_access_flags = 0;
  1005. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1006. dest_rd_atomic = attr->max_dest_rd_atomic;
  1007. else
  1008. dest_rd_atomic = qp->resp_depth;
  1009. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1010. access_flags = attr->qp_access_flags;
  1011. else
  1012. access_flags = qp->atomic_rd_en;
  1013. if (!dest_rd_atomic)
  1014. access_flags &= IB_ACCESS_REMOTE_WRITE;
  1015. if (access_flags & IB_ACCESS_REMOTE_READ)
  1016. hw_access_flags |= MLX4_QP_BIT_RRE;
  1017. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  1018. hw_access_flags |= MLX4_QP_BIT_RAE;
  1019. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  1020. hw_access_flags |= MLX4_QP_BIT_RWE;
  1021. return cpu_to_be32(hw_access_flags);
  1022. }
  1023. static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
  1024. int attr_mask)
  1025. {
  1026. if (attr_mask & IB_QP_PKEY_INDEX)
  1027. sqp->pkey_index = attr->pkey_index;
  1028. if (attr_mask & IB_QP_QKEY)
  1029. sqp->qkey = attr->qkey;
  1030. if (attr_mask & IB_QP_SQ_PSN)
  1031. sqp->send_psn = attr->sq_psn;
  1032. }
  1033. static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
  1034. {
  1035. path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
  1036. }
  1037. static int _mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_ah_attr *ah,
  1038. u64 smac, u16 vlan_tag, struct mlx4_qp_path *path,
  1039. u8 port)
  1040. {
  1041. int is_eth = rdma_port_get_link_layer(&dev->ib_dev, port) ==
  1042. IB_LINK_LAYER_ETHERNET;
  1043. int vidx;
  1044. int smac_index;
  1045. path->grh_mylmc = ah->src_path_bits & 0x7f;
  1046. path->rlid = cpu_to_be16(ah->dlid);
  1047. if (ah->static_rate) {
  1048. path->static_rate = ah->static_rate + MLX4_STAT_RATE_OFFSET;
  1049. while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  1050. !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
  1051. --path->static_rate;
  1052. } else
  1053. path->static_rate = 0;
  1054. if (ah->ah_flags & IB_AH_GRH) {
  1055. if (ah->grh.sgid_index >= dev->dev->caps.gid_table_len[port]) {
  1056. pr_err("sgid_index (%u) too large. max is %d\n",
  1057. ah->grh.sgid_index, dev->dev->caps.gid_table_len[port] - 1);
  1058. return -1;
  1059. }
  1060. path->grh_mylmc |= 1 << 7;
  1061. path->mgid_index = ah->grh.sgid_index;
  1062. path->hop_limit = ah->grh.hop_limit;
  1063. path->tclass_flowlabel =
  1064. cpu_to_be32((ah->grh.traffic_class << 20) |
  1065. (ah->grh.flow_label));
  1066. memcpy(path->rgid, ah->grh.dgid.raw, 16);
  1067. }
  1068. if (is_eth) {
  1069. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  1070. ((port - 1) << 6) | ((ah->sl & 7) << 3);
  1071. if (!(ah->ah_flags & IB_AH_GRH))
  1072. return -1;
  1073. memcpy(path->dmac, ah->dmac, ETH_ALEN);
  1074. path->ackto = MLX4_IB_LINK_TYPE_ETH;
  1075. /* find the index into MAC table for IBoE */
  1076. if (!is_zero_ether_addr((const u8 *)&smac)) {
  1077. if (mlx4_find_cached_mac(dev->dev, port, smac,
  1078. &smac_index))
  1079. return -ENOENT;
  1080. } else {
  1081. smac_index = 0;
  1082. }
  1083. path->grh_mylmc &= 0x80 | smac_index;
  1084. path->feup |= MLX4_FEUP_FORCE_ETH_UP;
  1085. if (vlan_tag < 0x1000) {
  1086. if (mlx4_find_cached_vlan(dev->dev, port, vlan_tag, &vidx))
  1087. return -ENOENT;
  1088. path->vlan_index = vidx;
  1089. path->fl = 1 << 6;
  1090. path->feup |= MLX4_FVL_FORCE_ETH_VLAN;
  1091. }
  1092. } else
  1093. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  1094. ((port - 1) << 6) | ((ah->sl & 0xf) << 2);
  1095. return 0;
  1096. }
  1097. static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_qp_attr *qp,
  1098. enum ib_qp_attr_mask qp_attr_mask,
  1099. struct mlx4_qp_path *path, u8 port)
  1100. {
  1101. return _mlx4_set_path(dev, &qp->ah_attr,
  1102. mlx4_mac_to_u64((u8 *)qp->smac),
  1103. (qp_attr_mask & IB_QP_VID) ? qp->vlan_id : 0xffff,
  1104. path, port);
  1105. }
  1106. static int mlx4_set_alt_path(struct mlx4_ib_dev *dev,
  1107. const struct ib_qp_attr *qp,
  1108. enum ib_qp_attr_mask qp_attr_mask,
  1109. struct mlx4_qp_path *path, u8 port)
  1110. {
  1111. return _mlx4_set_path(dev, &qp->alt_ah_attr,
  1112. mlx4_mac_to_u64((u8 *)qp->alt_smac),
  1113. (qp_attr_mask & IB_QP_ALT_VID) ?
  1114. qp->alt_vlan_id : 0xffff,
  1115. path, port);
  1116. }
  1117. static void update_mcg_macs(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  1118. {
  1119. struct mlx4_ib_gid_entry *ge, *tmp;
  1120. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  1121. if (!ge->added && mlx4_ib_add_mc(dev, qp, &ge->gid)) {
  1122. ge->added = 1;
  1123. ge->port = qp->port;
  1124. }
  1125. }
  1126. }
  1127. static int __mlx4_ib_modify_qp(struct ib_qp *ibqp,
  1128. const struct ib_qp_attr *attr, int attr_mask,
  1129. enum ib_qp_state cur_state, enum ib_qp_state new_state)
  1130. {
  1131. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1132. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1133. struct mlx4_ib_pd *pd;
  1134. struct mlx4_ib_cq *send_cq, *recv_cq;
  1135. struct mlx4_qp_context *context;
  1136. enum mlx4_qp_optpar optpar = 0;
  1137. int sqd_event;
  1138. int steer_qp = 0;
  1139. int err = -EINVAL;
  1140. context = kzalloc(sizeof *context, GFP_KERNEL);
  1141. if (!context)
  1142. return -ENOMEM;
  1143. context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
  1144. (to_mlx4_st(dev, qp->mlx4_ib_qp_type) << 16));
  1145. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  1146. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  1147. else {
  1148. optpar |= MLX4_QP_OPTPAR_PM_STATE;
  1149. switch (attr->path_mig_state) {
  1150. case IB_MIG_MIGRATED:
  1151. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  1152. break;
  1153. case IB_MIG_REARM:
  1154. context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
  1155. break;
  1156. case IB_MIG_ARMED:
  1157. context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
  1158. break;
  1159. }
  1160. }
  1161. if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI)
  1162. context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
  1163. else if (ibqp->qp_type == IB_QPT_RAW_PACKET)
  1164. context->mtu_msgmax = (MLX4_RAW_QP_MTU << 5) | MLX4_RAW_QP_MSGMAX;
  1165. else if (ibqp->qp_type == IB_QPT_UD) {
  1166. if (qp->flags & MLX4_IB_QP_LSO)
  1167. context->mtu_msgmax = (IB_MTU_4096 << 5) |
  1168. ilog2(dev->dev->caps.max_gso_sz);
  1169. else
  1170. context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
  1171. } else if (attr_mask & IB_QP_PATH_MTU) {
  1172. if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
  1173. pr_err("path MTU (%u) is invalid\n",
  1174. attr->path_mtu);
  1175. goto out;
  1176. }
  1177. context->mtu_msgmax = (attr->path_mtu << 5) |
  1178. ilog2(dev->dev->caps.max_msg_sz);
  1179. }
  1180. if (qp->rq.wqe_cnt)
  1181. context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3;
  1182. context->rq_size_stride |= qp->rq.wqe_shift - 4;
  1183. if (qp->sq.wqe_cnt)
  1184. context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3;
  1185. context->sq_size_stride |= qp->sq.wqe_shift - 4;
  1186. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  1187. context->sq_size_stride |= !!qp->sq_no_prefetch << 7;
  1188. context->xrcd = cpu_to_be32((u32) qp->xrcdn);
  1189. if (ibqp->qp_type == IB_QPT_RAW_PACKET)
  1190. context->param3 |= cpu_to_be32(1 << 30);
  1191. }
  1192. if (qp->ibqp.uobject)
  1193. context->usr_page = cpu_to_be32(to_mucontext(ibqp->uobject->context)->uar.index);
  1194. else
  1195. context->usr_page = cpu_to_be32(dev->priv_uar.index);
  1196. if (attr_mask & IB_QP_DEST_QPN)
  1197. context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  1198. if (attr_mask & IB_QP_PORT) {
  1199. if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
  1200. !(attr_mask & IB_QP_AV)) {
  1201. mlx4_set_sched(&context->pri_path, attr->port_num);
  1202. optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
  1203. }
  1204. }
  1205. if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
  1206. if (dev->counters[qp->port - 1] != -1) {
  1207. context->pri_path.counter_index =
  1208. dev->counters[qp->port - 1];
  1209. optpar |= MLX4_QP_OPTPAR_COUNTER_INDEX;
  1210. } else
  1211. context->pri_path.counter_index = 0xff;
  1212. if (qp->flags & MLX4_IB_QP_NETIF) {
  1213. mlx4_ib_steer_qp_reg(dev, qp, 1);
  1214. steer_qp = 1;
  1215. }
  1216. }
  1217. if (attr_mask & IB_QP_PKEY_INDEX) {
  1218. if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV)
  1219. context->pri_path.disable_pkey_check = 0x40;
  1220. context->pri_path.pkey_index = attr->pkey_index;
  1221. optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
  1222. }
  1223. if (attr_mask & IB_QP_AV) {
  1224. if (mlx4_set_path(dev, attr, attr_mask, &context->pri_path,
  1225. attr_mask & IB_QP_PORT ?
  1226. attr->port_num : qp->port))
  1227. goto out;
  1228. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  1229. MLX4_QP_OPTPAR_SCHED_QUEUE);
  1230. }
  1231. if (attr_mask & IB_QP_TIMEOUT) {
  1232. context->pri_path.ackto |= attr->timeout << 3;
  1233. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  1234. }
  1235. if (attr_mask & IB_QP_ALT_PATH) {
  1236. if (attr->alt_port_num == 0 ||
  1237. attr->alt_port_num > dev->dev->caps.num_ports)
  1238. goto out;
  1239. if (attr->alt_pkey_index >=
  1240. dev->dev->caps.pkey_table_len[attr->alt_port_num])
  1241. goto out;
  1242. if (mlx4_set_alt_path(dev, attr, attr_mask, &context->alt_path,
  1243. attr->alt_port_num))
  1244. goto out;
  1245. context->alt_path.pkey_index = attr->alt_pkey_index;
  1246. context->alt_path.ackto = attr->alt_timeout << 3;
  1247. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  1248. }
  1249. pd = get_pd(qp);
  1250. get_cqs(qp, &send_cq, &recv_cq);
  1251. context->pd = cpu_to_be32(pd->pdn);
  1252. context->cqn_send = cpu_to_be32(send_cq->mcq.cqn);
  1253. context->cqn_recv = cpu_to_be32(recv_cq->mcq.cqn);
  1254. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  1255. /* Set "fast registration enabled" for all kernel QPs */
  1256. if (!qp->ibqp.uobject)
  1257. context->params1 |= cpu_to_be32(1 << 11);
  1258. if (attr_mask & IB_QP_RNR_RETRY) {
  1259. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  1260. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  1261. }
  1262. if (attr_mask & IB_QP_RETRY_CNT) {
  1263. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  1264. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  1265. }
  1266. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  1267. if (attr->max_rd_atomic)
  1268. context->params1 |=
  1269. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  1270. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  1271. }
  1272. if (attr_mask & IB_QP_SQ_PSN)
  1273. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  1274. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  1275. if (attr->max_dest_rd_atomic)
  1276. context->params2 |=
  1277. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  1278. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  1279. }
  1280. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  1281. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  1282. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  1283. }
  1284. if (ibqp->srq)
  1285. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  1286. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  1287. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  1288. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  1289. }
  1290. if (attr_mask & IB_QP_RQ_PSN)
  1291. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  1292. /* proxy and tunnel qp qkeys will be changed in modify-qp wrappers */
  1293. if (attr_mask & IB_QP_QKEY) {
  1294. if (qp->mlx4_ib_qp_type &
  1295. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))
  1296. context->qkey = cpu_to_be32(IB_QP_SET_QKEY);
  1297. else {
  1298. if (mlx4_is_mfunc(dev->dev) &&
  1299. !(qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV) &&
  1300. (attr->qkey & MLX4_RESERVED_QKEY_MASK) ==
  1301. MLX4_RESERVED_QKEY_BASE) {
  1302. pr_err("Cannot use reserved QKEY"
  1303. " 0x%x (range 0xffff0000..0xffffffff"
  1304. " is reserved)\n", attr->qkey);
  1305. err = -EINVAL;
  1306. goto out;
  1307. }
  1308. context->qkey = cpu_to_be32(attr->qkey);
  1309. }
  1310. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  1311. }
  1312. if (ibqp->srq)
  1313. context->srqn = cpu_to_be32(1 << 24 | to_msrq(ibqp->srq)->msrq.srqn);
  1314. if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1315. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  1316. if (cur_state == IB_QPS_INIT &&
  1317. new_state == IB_QPS_RTR &&
  1318. (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  1319. ibqp->qp_type == IB_QPT_UD ||
  1320. ibqp->qp_type == IB_QPT_RAW_PACKET)) {
  1321. context->pri_path.sched_queue = (qp->port - 1) << 6;
  1322. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
  1323. qp->mlx4_ib_qp_type &
  1324. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) {
  1325. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  1326. if (qp->mlx4_ib_qp_type != MLX4_IB_QPT_SMI)
  1327. context->pri_path.fl = 0x80;
  1328. } else {
  1329. if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV)
  1330. context->pri_path.fl = 0x80;
  1331. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  1332. }
  1333. }
  1334. if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET)
  1335. context->pri_path.ackto = (context->pri_path.ackto & 0xf8) |
  1336. MLX4_IB_LINK_TYPE_ETH;
  1337. if (ibqp->qp_type == IB_QPT_UD && (new_state == IB_QPS_RTR)) {
  1338. int is_eth = rdma_port_get_link_layer(
  1339. &dev->ib_dev, qp->port) ==
  1340. IB_LINK_LAYER_ETHERNET;
  1341. if (is_eth) {
  1342. context->pri_path.ackto = MLX4_IB_LINK_TYPE_ETH;
  1343. optpar |= MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH;
  1344. }
  1345. }
  1346. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  1347. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  1348. sqd_event = 1;
  1349. else
  1350. sqd_event = 0;
  1351. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1352. context->rlkey |= (1 << 4);
  1353. /*
  1354. * Before passing a kernel QP to the HW, make sure that the
  1355. * ownership bits of the send queue are set and the SQ
  1356. * headroom is stamped so that the hardware doesn't start
  1357. * processing stale work requests.
  1358. */
  1359. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  1360. struct mlx4_wqe_ctrl_seg *ctrl;
  1361. int i;
  1362. for (i = 0; i < qp->sq.wqe_cnt; ++i) {
  1363. ctrl = get_send_wqe(qp, i);
  1364. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  1365. if (qp->sq_max_wqes_per_wr == 1)
  1366. ctrl->fence_size = 1 << (qp->sq.wqe_shift - 4);
  1367. stamp_send_wqe(qp, i, 1 << qp->sq.wqe_shift);
  1368. }
  1369. }
  1370. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  1371. to_mlx4_state(new_state), context, optpar,
  1372. sqd_event, &qp->mqp);
  1373. if (err)
  1374. goto out;
  1375. qp->state = new_state;
  1376. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1377. qp->atomic_rd_en = attr->qp_access_flags;
  1378. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1379. qp->resp_depth = attr->max_dest_rd_atomic;
  1380. if (attr_mask & IB_QP_PORT) {
  1381. qp->port = attr->port_num;
  1382. update_mcg_macs(dev, qp);
  1383. }
  1384. if (attr_mask & IB_QP_ALT_PATH)
  1385. qp->alt_port = attr->alt_port_num;
  1386. if (is_sqp(dev, qp))
  1387. store_sqp_attrs(to_msqp(qp), attr, attr_mask);
  1388. /*
  1389. * If we moved QP0 to RTR, bring the IB link up; if we moved
  1390. * QP0 to RESET or ERROR, bring the link back down.
  1391. */
  1392. if (is_qp0(dev, qp)) {
  1393. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  1394. if (mlx4_INIT_PORT(dev->dev, qp->port))
  1395. pr_warn("INIT_PORT failed for port %d\n",
  1396. qp->port);
  1397. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  1398. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  1399. mlx4_CLOSE_PORT(dev->dev, qp->port);
  1400. }
  1401. /*
  1402. * If we moved a kernel QP to RESET, clean up all old CQ
  1403. * entries and reinitialize the QP.
  1404. */
  1405. if (new_state == IB_QPS_RESET && !ibqp->uobject) {
  1406. mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  1407. ibqp->srq ? to_msrq(ibqp->srq): NULL);
  1408. if (send_cq != recv_cq)
  1409. mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  1410. qp->rq.head = 0;
  1411. qp->rq.tail = 0;
  1412. qp->sq.head = 0;
  1413. qp->sq.tail = 0;
  1414. qp->sq_next_wqe = 0;
  1415. if (qp->rq.wqe_cnt)
  1416. *qp->db.db = 0;
  1417. if (qp->flags & MLX4_IB_QP_NETIF)
  1418. mlx4_ib_steer_qp_reg(dev, qp, 0);
  1419. }
  1420. out:
  1421. if (err && steer_qp)
  1422. mlx4_ib_steer_qp_reg(dev, qp, 0);
  1423. kfree(context);
  1424. return err;
  1425. }
  1426. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1427. int attr_mask, struct ib_udata *udata)
  1428. {
  1429. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1430. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1431. enum ib_qp_state cur_state, new_state;
  1432. int err = -EINVAL;
  1433. int ll;
  1434. mutex_lock(&qp->mutex);
  1435. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  1436. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  1437. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  1438. ll = IB_LINK_LAYER_UNSPECIFIED;
  1439. } else {
  1440. int port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1441. ll = rdma_port_get_link_layer(&dev->ib_dev, port);
  1442. }
  1443. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  1444. attr_mask, ll)) {
  1445. pr_debug("qpn 0x%x: invalid attribute mask specified "
  1446. "for transition %d to %d. qp_type %d,"
  1447. " attr_mask 0x%x\n",
  1448. ibqp->qp_num, cur_state, new_state,
  1449. ibqp->qp_type, attr_mask);
  1450. goto out;
  1451. }
  1452. if ((attr_mask & IB_QP_PORT) &&
  1453. (attr->port_num == 0 || attr->port_num > dev->num_ports)) {
  1454. pr_debug("qpn 0x%x: invalid port number (%d) specified "
  1455. "for transition %d to %d. qp_type %d\n",
  1456. ibqp->qp_num, attr->port_num, cur_state,
  1457. new_state, ibqp->qp_type);
  1458. goto out;
  1459. }
  1460. if ((attr_mask & IB_QP_PORT) && (ibqp->qp_type == IB_QPT_RAW_PACKET) &&
  1461. (rdma_port_get_link_layer(&dev->ib_dev, attr->port_num) !=
  1462. IB_LINK_LAYER_ETHERNET))
  1463. goto out;
  1464. if (attr_mask & IB_QP_PKEY_INDEX) {
  1465. int p = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1466. if (attr->pkey_index >= dev->dev->caps.pkey_table_len[p]) {
  1467. pr_debug("qpn 0x%x: invalid pkey index (%d) specified "
  1468. "for transition %d to %d. qp_type %d\n",
  1469. ibqp->qp_num, attr->pkey_index, cur_state,
  1470. new_state, ibqp->qp_type);
  1471. goto out;
  1472. }
  1473. }
  1474. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  1475. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  1476. pr_debug("qpn 0x%x: max_rd_atomic (%d) too large. "
  1477. "Transition %d to %d. qp_type %d\n",
  1478. ibqp->qp_num, attr->max_rd_atomic, cur_state,
  1479. new_state, ibqp->qp_type);
  1480. goto out;
  1481. }
  1482. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  1483. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  1484. pr_debug("qpn 0x%x: max_dest_rd_atomic (%d) too large. "
  1485. "Transition %d to %d. qp_type %d\n",
  1486. ibqp->qp_num, attr->max_dest_rd_atomic, cur_state,
  1487. new_state, ibqp->qp_type);
  1488. goto out;
  1489. }
  1490. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  1491. err = 0;
  1492. goto out;
  1493. }
  1494. err = __mlx4_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
  1495. out:
  1496. mutex_unlock(&qp->mutex);
  1497. return err;
  1498. }
  1499. static int build_sriov_qp0_header(struct mlx4_ib_sqp *sqp,
  1500. struct ib_send_wr *wr,
  1501. void *wqe, unsigned *mlx_seg_len)
  1502. {
  1503. struct mlx4_ib_dev *mdev = to_mdev(sqp->qp.ibqp.device);
  1504. struct ib_device *ib_dev = &mdev->ib_dev;
  1505. struct mlx4_wqe_mlx_seg *mlx = wqe;
  1506. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  1507. struct mlx4_ib_ah *ah = to_mah(wr->wr.ud.ah);
  1508. u16 pkey;
  1509. u32 qkey;
  1510. int send_size;
  1511. int header_size;
  1512. int spc;
  1513. int i;
  1514. if (wr->opcode != IB_WR_SEND)
  1515. return -EINVAL;
  1516. send_size = 0;
  1517. for (i = 0; i < wr->num_sge; ++i)
  1518. send_size += wr->sg_list[i].length;
  1519. /* for proxy-qp0 sends, need to add in size of tunnel header */
  1520. /* for tunnel-qp0 sends, tunnel header is already in s/g list */
  1521. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER)
  1522. send_size += sizeof (struct mlx4_ib_tunnel_header);
  1523. ib_ud_header_init(send_size, 1, 0, 0, 0, 0, &sqp->ud_header);
  1524. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER) {
  1525. sqp->ud_header.lrh.service_level =
  1526. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  1527. sqp->ud_header.lrh.destination_lid =
  1528. cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  1529. sqp->ud_header.lrh.source_lid =
  1530. cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  1531. }
  1532. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  1533. /* force loopback */
  1534. mlx->flags |= cpu_to_be32(MLX4_WQE_MLX_VL15 | 0x1 | MLX4_WQE_MLX_SLR);
  1535. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  1536. sqp->ud_header.lrh.virtual_lane = 0;
  1537. sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
  1538. ib_get_cached_pkey(ib_dev, sqp->qp.port, 0, &pkey);
  1539. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  1540. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_TUN_SMI_OWNER)
  1541. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  1542. else
  1543. sqp->ud_header.bth.destination_qpn =
  1544. cpu_to_be32(mdev->dev->caps.qp0_tunnel[sqp->qp.port - 1]);
  1545. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  1546. if (mlx4_get_parav_qkey(mdev->dev, sqp->qp.mqp.qpn, &qkey))
  1547. return -EINVAL;
  1548. sqp->ud_header.deth.qkey = cpu_to_be32(qkey);
  1549. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.mqp.qpn);
  1550. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  1551. sqp->ud_header.immediate_present = 0;
  1552. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  1553. /*
  1554. * Inline data segments may not cross a 64 byte boundary. If
  1555. * our UD header is bigger than the space available up to the
  1556. * next 64 byte boundary in the WQE, use two inline data
  1557. * segments to hold the UD header.
  1558. */
  1559. spc = MLX4_INLINE_ALIGN -
  1560. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  1561. if (header_size <= spc) {
  1562. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  1563. memcpy(inl + 1, sqp->header_buf, header_size);
  1564. i = 1;
  1565. } else {
  1566. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  1567. memcpy(inl + 1, sqp->header_buf, spc);
  1568. inl = (void *) (inl + 1) + spc;
  1569. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  1570. /*
  1571. * Need a barrier here to make sure all the data is
  1572. * visible before the byte_count field is set.
  1573. * Otherwise the HCA prefetcher could grab the 64-byte
  1574. * chunk with this inline segment and get a valid (!=
  1575. * 0xffffffff) byte count but stale data, and end up
  1576. * generating a packet with bad headers.
  1577. *
  1578. * The first inline segment's byte_count field doesn't
  1579. * need a barrier, because it comes after a
  1580. * control/MLX segment and therefore is at an offset
  1581. * of 16 mod 64.
  1582. */
  1583. wmb();
  1584. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  1585. i = 2;
  1586. }
  1587. *mlx_seg_len =
  1588. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  1589. return 0;
  1590. }
  1591. static int build_mlx_header(struct mlx4_ib_sqp *sqp, struct ib_send_wr *wr,
  1592. void *wqe, unsigned *mlx_seg_len)
  1593. {
  1594. struct ib_device *ib_dev = sqp->qp.ibqp.device;
  1595. struct mlx4_wqe_mlx_seg *mlx = wqe;
  1596. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  1597. struct mlx4_ib_ah *ah = to_mah(wr->wr.ud.ah);
  1598. struct net_device *ndev;
  1599. union ib_gid sgid;
  1600. u16 pkey;
  1601. int send_size;
  1602. int header_size;
  1603. int spc;
  1604. int i;
  1605. int err = 0;
  1606. u16 vlan = 0xffff;
  1607. bool is_eth;
  1608. bool is_vlan = false;
  1609. bool is_grh;
  1610. send_size = 0;
  1611. for (i = 0; i < wr->num_sge; ++i)
  1612. send_size += wr->sg_list[i].length;
  1613. is_eth = rdma_port_get_link_layer(sqp->qp.ibqp.device, sqp->qp.port) == IB_LINK_LAYER_ETHERNET;
  1614. is_grh = mlx4_ib_ah_grh_present(ah);
  1615. if (is_eth) {
  1616. if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
  1617. /* When multi-function is enabled, the ib_core gid
  1618. * indexes don't necessarily match the hw ones, so
  1619. * we must use our own cache */
  1620. sgid.global.subnet_prefix =
  1621. to_mdev(ib_dev)->sriov.demux[sqp->qp.port - 1].
  1622. subnet_prefix;
  1623. sgid.global.interface_id =
  1624. to_mdev(ib_dev)->sriov.demux[sqp->qp.port - 1].
  1625. guid_cache[ah->av.ib.gid_index];
  1626. } else {
  1627. err = ib_get_cached_gid(ib_dev,
  1628. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  1629. ah->av.ib.gid_index, &sgid);
  1630. if (err)
  1631. return err;
  1632. }
  1633. if (ah->av.eth.vlan != 0xffff) {
  1634. vlan = be16_to_cpu(ah->av.eth.vlan) & 0x0fff;
  1635. is_vlan = 1;
  1636. }
  1637. }
  1638. ib_ud_header_init(send_size, !is_eth, is_eth, is_vlan, is_grh, 0, &sqp->ud_header);
  1639. if (!is_eth) {
  1640. sqp->ud_header.lrh.service_level =
  1641. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  1642. sqp->ud_header.lrh.destination_lid = ah->av.ib.dlid;
  1643. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  1644. }
  1645. if (is_grh) {
  1646. sqp->ud_header.grh.traffic_class =
  1647. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  1648. sqp->ud_header.grh.flow_label =
  1649. ah->av.ib.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  1650. sqp->ud_header.grh.hop_limit = ah->av.ib.hop_limit;
  1651. if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
  1652. /* When multi-function is enabled, the ib_core gid
  1653. * indexes don't necessarily match the hw ones, so
  1654. * we must use our own cache */
  1655. sqp->ud_header.grh.source_gid.global.subnet_prefix =
  1656. to_mdev(ib_dev)->sriov.demux[sqp->qp.port - 1].
  1657. subnet_prefix;
  1658. sqp->ud_header.grh.source_gid.global.interface_id =
  1659. to_mdev(ib_dev)->sriov.demux[sqp->qp.port - 1].
  1660. guid_cache[ah->av.ib.gid_index];
  1661. } else
  1662. ib_get_cached_gid(ib_dev,
  1663. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  1664. ah->av.ib.gid_index,
  1665. &sqp->ud_header.grh.source_gid);
  1666. memcpy(sqp->ud_header.grh.destination_gid.raw,
  1667. ah->av.ib.dgid, 16);
  1668. }
  1669. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  1670. if (!is_eth) {
  1671. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  1672. (sqp->ud_header.lrh.destination_lid ==
  1673. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  1674. (sqp->ud_header.lrh.service_level << 8));
  1675. if (ah->av.ib.port_pd & cpu_to_be32(0x80000000))
  1676. mlx->flags |= cpu_to_be32(0x1); /* force loopback */
  1677. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  1678. }
  1679. switch (wr->opcode) {
  1680. case IB_WR_SEND:
  1681. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  1682. sqp->ud_header.immediate_present = 0;
  1683. break;
  1684. case IB_WR_SEND_WITH_IMM:
  1685. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  1686. sqp->ud_header.immediate_present = 1;
  1687. sqp->ud_header.immediate_data = wr->ex.imm_data;
  1688. break;
  1689. default:
  1690. return -EINVAL;
  1691. }
  1692. if (is_eth) {
  1693. u8 *smac;
  1694. u16 pcp = (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 29) << 13;
  1695. mlx->sched_prio = cpu_to_be16(pcp);
  1696. memcpy(sqp->ud_header.eth.dmac_h, ah->av.eth.mac, 6);
  1697. /* FIXME: cache smac value? */
  1698. ndev = to_mdev(sqp->qp.ibqp.device)->iboe.netdevs[sqp->qp.port - 1];
  1699. if (!ndev)
  1700. return -ENODEV;
  1701. smac = ndev->dev_addr;
  1702. memcpy(sqp->ud_header.eth.smac_h, smac, 6);
  1703. if (!memcmp(sqp->ud_header.eth.smac_h, sqp->ud_header.eth.dmac_h, 6))
  1704. mlx->flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
  1705. if (!is_vlan) {
  1706. sqp->ud_header.eth.type = cpu_to_be16(MLX4_IB_IBOE_ETHERTYPE);
  1707. } else {
  1708. sqp->ud_header.vlan.type = cpu_to_be16(MLX4_IB_IBOE_ETHERTYPE);
  1709. sqp->ud_header.vlan.tag = cpu_to_be16(vlan | pcp);
  1710. }
  1711. } else {
  1712. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
  1713. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  1714. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  1715. }
  1716. sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
  1717. if (!sqp->qp.ibqp.qp_num)
  1718. ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
  1719. else
  1720. ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->wr.ud.pkey_index, &pkey);
  1721. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  1722. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  1723. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  1724. sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
  1725. sqp->qkey : wr->wr.ud.remote_qkey);
  1726. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  1727. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  1728. if (0) {
  1729. pr_err("built UD header of size %d:\n", header_size);
  1730. for (i = 0; i < header_size / 4; ++i) {
  1731. if (i % 8 == 0)
  1732. pr_err(" [%02x] ", i * 4);
  1733. pr_cont(" %08x",
  1734. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  1735. if ((i + 1) % 8 == 0)
  1736. pr_cont("\n");
  1737. }
  1738. pr_err("\n");
  1739. }
  1740. /*
  1741. * Inline data segments may not cross a 64 byte boundary. If
  1742. * our UD header is bigger than the space available up to the
  1743. * next 64 byte boundary in the WQE, use two inline data
  1744. * segments to hold the UD header.
  1745. */
  1746. spc = MLX4_INLINE_ALIGN -
  1747. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  1748. if (header_size <= spc) {
  1749. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  1750. memcpy(inl + 1, sqp->header_buf, header_size);
  1751. i = 1;
  1752. } else {
  1753. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  1754. memcpy(inl + 1, sqp->header_buf, spc);
  1755. inl = (void *) (inl + 1) + spc;
  1756. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  1757. /*
  1758. * Need a barrier here to make sure all the data is
  1759. * visible before the byte_count field is set.
  1760. * Otherwise the HCA prefetcher could grab the 64-byte
  1761. * chunk with this inline segment and get a valid (!=
  1762. * 0xffffffff) byte count but stale data, and end up
  1763. * generating a packet with bad headers.
  1764. *
  1765. * The first inline segment's byte_count field doesn't
  1766. * need a barrier, because it comes after a
  1767. * control/MLX segment and therefore is at an offset
  1768. * of 16 mod 64.
  1769. */
  1770. wmb();
  1771. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  1772. i = 2;
  1773. }
  1774. *mlx_seg_len =
  1775. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  1776. return 0;
  1777. }
  1778. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  1779. {
  1780. unsigned cur;
  1781. struct mlx4_ib_cq *cq;
  1782. cur = wq->head - wq->tail;
  1783. if (likely(cur + nreq < wq->max_post))
  1784. return 0;
  1785. cq = to_mcq(ib_cq);
  1786. spin_lock(&cq->lock);
  1787. cur = wq->head - wq->tail;
  1788. spin_unlock(&cq->lock);
  1789. return cur + nreq >= wq->max_post;
  1790. }
  1791. static __be32 convert_access(int acc)
  1792. {
  1793. return (acc & IB_ACCESS_REMOTE_ATOMIC ?
  1794. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_ATOMIC) : 0) |
  1795. (acc & IB_ACCESS_REMOTE_WRITE ?
  1796. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_WRITE) : 0) |
  1797. (acc & IB_ACCESS_REMOTE_READ ?
  1798. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_READ) : 0) |
  1799. (acc & IB_ACCESS_LOCAL_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_WRITE) : 0) |
  1800. cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_READ);
  1801. }
  1802. static void set_fmr_seg(struct mlx4_wqe_fmr_seg *fseg, struct ib_send_wr *wr)
  1803. {
  1804. struct mlx4_ib_fast_reg_page_list *mfrpl = to_mfrpl(wr->wr.fast_reg.page_list);
  1805. int i;
  1806. for (i = 0; i < wr->wr.fast_reg.page_list_len; ++i)
  1807. mfrpl->mapped_page_list[i] =
  1808. cpu_to_be64(wr->wr.fast_reg.page_list->page_list[i] |
  1809. MLX4_MTT_FLAG_PRESENT);
  1810. fseg->flags = convert_access(wr->wr.fast_reg.access_flags);
  1811. fseg->mem_key = cpu_to_be32(wr->wr.fast_reg.rkey);
  1812. fseg->buf_list = cpu_to_be64(mfrpl->map);
  1813. fseg->start_addr = cpu_to_be64(wr->wr.fast_reg.iova_start);
  1814. fseg->reg_len = cpu_to_be64(wr->wr.fast_reg.length);
  1815. fseg->offset = 0; /* XXX -- is this just for ZBVA? */
  1816. fseg->page_size = cpu_to_be32(wr->wr.fast_reg.page_shift);
  1817. fseg->reserved[0] = 0;
  1818. fseg->reserved[1] = 0;
  1819. }
  1820. static void set_bind_seg(struct mlx4_wqe_bind_seg *bseg, struct ib_send_wr *wr)
  1821. {
  1822. bseg->flags1 =
  1823. convert_access(wr->wr.bind_mw.bind_info.mw_access_flags) &
  1824. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_READ |
  1825. MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_WRITE |
  1826. MLX4_WQE_FMR_AND_BIND_PERM_ATOMIC);
  1827. bseg->flags2 = 0;
  1828. if (wr->wr.bind_mw.mw->type == IB_MW_TYPE_2)
  1829. bseg->flags2 |= cpu_to_be32(MLX4_WQE_BIND_TYPE_2);
  1830. if (wr->wr.bind_mw.bind_info.mw_access_flags & IB_ZERO_BASED)
  1831. bseg->flags2 |= cpu_to_be32(MLX4_WQE_BIND_ZERO_BASED);
  1832. bseg->new_rkey = cpu_to_be32(wr->wr.bind_mw.rkey);
  1833. bseg->lkey = cpu_to_be32(wr->wr.bind_mw.bind_info.mr->lkey);
  1834. bseg->addr = cpu_to_be64(wr->wr.bind_mw.bind_info.addr);
  1835. bseg->length = cpu_to_be64(wr->wr.bind_mw.bind_info.length);
  1836. }
  1837. static void set_local_inv_seg(struct mlx4_wqe_local_inval_seg *iseg, u32 rkey)
  1838. {
  1839. memset(iseg, 0, sizeof(*iseg));
  1840. iseg->mem_key = cpu_to_be32(rkey);
  1841. }
  1842. static __always_inline void set_raddr_seg(struct mlx4_wqe_raddr_seg *rseg,
  1843. u64 remote_addr, u32 rkey)
  1844. {
  1845. rseg->raddr = cpu_to_be64(remote_addr);
  1846. rseg->rkey = cpu_to_be32(rkey);
  1847. rseg->reserved = 0;
  1848. }
  1849. static void set_atomic_seg(struct mlx4_wqe_atomic_seg *aseg, struct ib_send_wr *wr)
  1850. {
  1851. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  1852. aseg->swap_add = cpu_to_be64(wr->wr.atomic.swap);
  1853. aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add);
  1854. } else if (wr->opcode == IB_WR_MASKED_ATOMIC_FETCH_AND_ADD) {
  1855. aseg->swap_add = cpu_to_be64(wr->wr.atomic.compare_add);
  1856. aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add_mask);
  1857. } else {
  1858. aseg->swap_add = cpu_to_be64(wr->wr.atomic.compare_add);
  1859. aseg->compare = 0;
  1860. }
  1861. }
  1862. static void set_masked_atomic_seg(struct mlx4_wqe_masked_atomic_seg *aseg,
  1863. struct ib_send_wr *wr)
  1864. {
  1865. aseg->swap_add = cpu_to_be64(wr->wr.atomic.swap);
  1866. aseg->swap_add_mask = cpu_to_be64(wr->wr.atomic.swap_mask);
  1867. aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add);
  1868. aseg->compare_mask = cpu_to_be64(wr->wr.atomic.compare_add_mask);
  1869. }
  1870. static void set_datagram_seg(struct mlx4_wqe_datagram_seg *dseg,
  1871. struct ib_send_wr *wr)
  1872. {
  1873. memcpy(dseg->av, &to_mah(wr->wr.ud.ah)->av, sizeof (struct mlx4_av));
  1874. dseg->dqpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  1875. dseg->qkey = cpu_to_be32(wr->wr.ud.remote_qkey);
  1876. dseg->vlan = to_mah(wr->wr.ud.ah)->av.eth.vlan;
  1877. memcpy(dseg->mac, to_mah(wr->wr.ud.ah)->av.eth.mac, 6);
  1878. }
  1879. static void set_tunnel_datagram_seg(struct mlx4_ib_dev *dev,
  1880. struct mlx4_wqe_datagram_seg *dseg,
  1881. struct ib_send_wr *wr, enum ib_qp_type qpt)
  1882. {
  1883. union mlx4_ext_av *av = &to_mah(wr->wr.ud.ah)->av;
  1884. struct mlx4_av sqp_av = {0};
  1885. int port = *((u8 *) &av->ib.port_pd) & 0x3;
  1886. /* force loopback */
  1887. sqp_av.port_pd = av->ib.port_pd | cpu_to_be32(0x80000000);
  1888. sqp_av.g_slid = av->ib.g_slid & 0x7f; /* no GRH */
  1889. sqp_av.sl_tclass_flowlabel = av->ib.sl_tclass_flowlabel &
  1890. cpu_to_be32(0xf0000000);
  1891. memcpy(dseg->av, &sqp_av, sizeof (struct mlx4_av));
  1892. /* This function used only for sending on QP1 proxies */
  1893. dseg->dqpn = cpu_to_be32(dev->dev->caps.qp1_tunnel[port - 1]);
  1894. /* Use QKEY from the QP context, which is set by master */
  1895. dseg->qkey = cpu_to_be32(IB_QP_SET_QKEY);
  1896. }
  1897. static void build_tunnel_header(struct ib_send_wr *wr, void *wqe, unsigned *mlx_seg_len)
  1898. {
  1899. struct mlx4_wqe_inline_seg *inl = wqe;
  1900. struct mlx4_ib_tunnel_header hdr;
  1901. struct mlx4_ib_ah *ah = to_mah(wr->wr.ud.ah);
  1902. int spc;
  1903. int i;
  1904. memcpy(&hdr.av, &ah->av, sizeof hdr.av);
  1905. hdr.remote_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  1906. hdr.pkey_index = cpu_to_be16(wr->wr.ud.pkey_index);
  1907. hdr.qkey = cpu_to_be32(wr->wr.ud.remote_qkey);
  1908. spc = MLX4_INLINE_ALIGN -
  1909. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  1910. if (sizeof (hdr) <= spc) {
  1911. memcpy(inl + 1, &hdr, sizeof (hdr));
  1912. wmb();
  1913. inl->byte_count = cpu_to_be32(1 << 31 | sizeof (hdr));
  1914. i = 1;
  1915. } else {
  1916. memcpy(inl + 1, &hdr, spc);
  1917. wmb();
  1918. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  1919. inl = (void *) (inl + 1) + spc;
  1920. memcpy(inl + 1, (void *) &hdr + spc, sizeof (hdr) - spc);
  1921. wmb();
  1922. inl->byte_count = cpu_to_be32(1 << 31 | (sizeof (hdr) - spc));
  1923. i = 2;
  1924. }
  1925. *mlx_seg_len =
  1926. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + sizeof (hdr), 16);
  1927. }
  1928. static void set_mlx_icrc_seg(void *dseg)
  1929. {
  1930. u32 *t = dseg;
  1931. struct mlx4_wqe_inline_seg *iseg = dseg;
  1932. t[1] = 0;
  1933. /*
  1934. * Need a barrier here before writing the byte_count field to
  1935. * make sure that all the data is visible before the
  1936. * byte_count field is set. Otherwise, if the segment begins
  1937. * a new cacheline, the HCA prefetcher could grab the 64-byte
  1938. * chunk and get a valid (!= * 0xffffffff) byte count but
  1939. * stale data, and end up sending the wrong data.
  1940. */
  1941. wmb();
  1942. iseg->byte_count = cpu_to_be32((1 << 31) | 4);
  1943. }
  1944. static void set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  1945. {
  1946. dseg->lkey = cpu_to_be32(sg->lkey);
  1947. dseg->addr = cpu_to_be64(sg->addr);
  1948. /*
  1949. * Need a barrier here before writing the byte_count field to
  1950. * make sure that all the data is visible before the
  1951. * byte_count field is set. Otherwise, if the segment begins
  1952. * a new cacheline, the HCA prefetcher could grab the 64-byte
  1953. * chunk and get a valid (!= * 0xffffffff) byte count but
  1954. * stale data, and end up sending the wrong data.
  1955. */
  1956. wmb();
  1957. dseg->byte_count = cpu_to_be32(sg->length);
  1958. }
  1959. static void __set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  1960. {
  1961. dseg->byte_count = cpu_to_be32(sg->length);
  1962. dseg->lkey = cpu_to_be32(sg->lkey);
  1963. dseg->addr = cpu_to_be64(sg->addr);
  1964. }
  1965. static int build_lso_seg(struct mlx4_wqe_lso_seg *wqe, struct ib_send_wr *wr,
  1966. struct mlx4_ib_qp *qp, unsigned *lso_seg_len,
  1967. __be32 *lso_hdr_sz, __be32 *blh)
  1968. {
  1969. unsigned halign = ALIGN(sizeof *wqe + wr->wr.ud.hlen, 16);
  1970. if (unlikely(halign > MLX4_IB_CACHE_LINE_SIZE))
  1971. *blh = cpu_to_be32(1 << 6);
  1972. if (unlikely(!(qp->flags & MLX4_IB_QP_LSO) &&
  1973. wr->num_sge > qp->sq.max_gs - (halign >> 4)))
  1974. return -EINVAL;
  1975. memcpy(wqe->header, wr->wr.ud.header, wr->wr.ud.hlen);
  1976. *lso_hdr_sz = cpu_to_be32((wr->wr.ud.mss - wr->wr.ud.hlen) << 16 |
  1977. wr->wr.ud.hlen);
  1978. *lso_seg_len = halign;
  1979. return 0;
  1980. }
  1981. static __be32 send_ieth(struct ib_send_wr *wr)
  1982. {
  1983. switch (wr->opcode) {
  1984. case IB_WR_SEND_WITH_IMM:
  1985. case IB_WR_RDMA_WRITE_WITH_IMM:
  1986. return wr->ex.imm_data;
  1987. case IB_WR_SEND_WITH_INV:
  1988. return cpu_to_be32(wr->ex.invalidate_rkey);
  1989. default:
  1990. return 0;
  1991. }
  1992. }
  1993. static void add_zero_len_inline(void *wqe)
  1994. {
  1995. struct mlx4_wqe_inline_seg *inl = wqe;
  1996. memset(wqe, 0, 16);
  1997. inl->byte_count = cpu_to_be32(1 << 31);
  1998. }
  1999. int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  2000. struct ib_send_wr **bad_wr)
  2001. {
  2002. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2003. void *wqe;
  2004. struct mlx4_wqe_ctrl_seg *ctrl;
  2005. struct mlx4_wqe_data_seg *dseg;
  2006. unsigned long flags;
  2007. int nreq;
  2008. int err = 0;
  2009. unsigned ind;
  2010. int uninitialized_var(stamp);
  2011. int uninitialized_var(size);
  2012. unsigned uninitialized_var(seglen);
  2013. __be32 dummy;
  2014. __be32 *lso_wqe;
  2015. __be32 uninitialized_var(lso_hdr_sz);
  2016. __be32 blh;
  2017. int i;
  2018. spin_lock_irqsave(&qp->sq.lock, flags);
  2019. ind = qp->sq_next_wqe;
  2020. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  2021. lso_wqe = &dummy;
  2022. blh = 0;
  2023. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  2024. err = -ENOMEM;
  2025. *bad_wr = wr;
  2026. goto out;
  2027. }
  2028. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  2029. err = -EINVAL;
  2030. *bad_wr = wr;
  2031. goto out;
  2032. }
  2033. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  2034. qp->sq.wrid[(qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
  2035. ctrl->srcrb_flags =
  2036. (wr->send_flags & IB_SEND_SIGNALED ?
  2037. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  2038. (wr->send_flags & IB_SEND_SOLICITED ?
  2039. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  2040. ((wr->send_flags & IB_SEND_IP_CSUM) ?
  2041. cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  2042. MLX4_WQE_CTRL_TCP_UDP_CSUM) : 0) |
  2043. qp->sq_signal_bits;
  2044. ctrl->imm = send_ieth(wr);
  2045. wqe += sizeof *ctrl;
  2046. size = sizeof *ctrl / 16;
  2047. switch (qp->mlx4_ib_qp_type) {
  2048. case MLX4_IB_QPT_RC:
  2049. case MLX4_IB_QPT_UC:
  2050. switch (wr->opcode) {
  2051. case IB_WR_ATOMIC_CMP_AND_SWP:
  2052. case IB_WR_ATOMIC_FETCH_AND_ADD:
  2053. case IB_WR_MASKED_ATOMIC_FETCH_AND_ADD:
  2054. set_raddr_seg(wqe, wr->wr.atomic.remote_addr,
  2055. wr->wr.atomic.rkey);
  2056. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  2057. set_atomic_seg(wqe, wr);
  2058. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  2059. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  2060. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  2061. break;
  2062. case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
  2063. set_raddr_seg(wqe, wr->wr.atomic.remote_addr,
  2064. wr->wr.atomic.rkey);
  2065. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  2066. set_masked_atomic_seg(wqe, wr);
  2067. wqe += sizeof (struct mlx4_wqe_masked_atomic_seg);
  2068. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  2069. sizeof (struct mlx4_wqe_masked_atomic_seg)) / 16;
  2070. break;
  2071. case IB_WR_RDMA_READ:
  2072. case IB_WR_RDMA_WRITE:
  2073. case IB_WR_RDMA_WRITE_WITH_IMM:
  2074. set_raddr_seg(wqe, wr->wr.rdma.remote_addr,
  2075. wr->wr.rdma.rkey);
  2076. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  2077. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  2078. break;
  2079. case IB_WR_LOCAL_INV:
  2080. ctrl->srcrb_flags |=
  2081. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  2082. set_local_inv_seg(wqe, wr->ex.invalidate_rkey);
  2083. wqe += sizeof (struct mlx4_wqe_local_inval_seg);
  2084. size += sizeof (struct mlx4_wqe_local_inval_seg) / 16;
  2085. break;
  2086. case IB_WR_FAST_REG_MR:
  2087. ctrl->srcrb_flags |=
  2088. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  2089. set_fmr_seg(wqe, wr);
  2090. wqe += sizeof (struct mlx4_wqe_fmr_seg);
  2091. size += sizeof (struct mlx4_wqe_fmr_seg) / 16;
  2092. break;
  2093. case IB_WR_BIND_MW:
  2094. ctrl->srcrb_flags |=
  2095. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  2096. set_bind_seg(wqe, wr);
  2097. wqe += sizeof(struct mlx4_wqe_bind_seg);
  2098. size += sizeof(struct mlx4_wqe_bind_seg) / 16;
  2099. break;
  2100. default:
  2101. /* No extra segments required for sends */
  2102. break;
  2103. }
  2104. break;
  2105. case MLX4_IB_QPT_TUN_SMI_OWNER:
  2106. err = build_sriov_qp0_header(to_msqp(qp), wr, ctrl, &seglen);
  2107. if (unlikely(err)) {
  2108. *bad_wr = wr;
  2109. goto out;
  2110. }
  2111. wqe += seglen;
  2112. size += seglen / 16;
  2113. break;
  2114. case MLX4_IB_QPT_TUN_SMI:
  2115. case MLX4_IB_QPT_TUN_GSI:
  2116. /* this is a UD qp used in MAD responses to slaves. */
  2117. set_datagram_seg(wqe, wr);
  2118. /* set the forced-loopback bit in the data seg av */
  2119. *(__be32 *) wqe |= cpu_to_be32(0x80000000);
  2120. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  2121. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  2122. break;
  2123. case MLX4_IB_QPT_UD:
  2124. set_datagram_seg(wqe, wr);
  2125. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  2126. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  2127. if (wr->opcode == IB_WR_LSO) {
  2128. err = build_lso_seg(wqe, wr, qp, &seglen, &lso_hdr_sz, &blh);
  2129. if (unlikely(err)) {
  2130. *bad_wr = wr;
  2131. goto out;
  2132. }
  2133. lso_wqe = (__be32 *) wqe;
  2134. wqe += seglen;
  2135. size += seglen / 16;
  2136. }
  2137. break;
  2138. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  2139. if (unlikely(!mlx4_is_master(to_mdev(ibqp->device)->dev))) {
  2140. err = -ENOSYS;
  2141. *bad_wr = wr;
  2142. goto out;
  2143. }
  2144. err = build_sriov_qp0_header(to_msqp(qp), wr, ctrl, &seglen);
  2145. if (unlikely(err)) {
  2146. *bad_wr = wr;
  2147. goto out;
  2148. }
  2149. wqe += seglen;
  2150. size += seglen / 16;
  2151. /* to start tunnel header on a cache-line boundary */
  2152. add_zero_len_inline(wqe);
  2153. wqe += 16;
  2154. size++;
  2155. build_tunnel_header(wr, wqe, &seglen);
  2156. wqe += seglen;
  2157. size += seglen / 16;
  2158. break;
  2159. case MLX4_IB_QPT_PROXY_SMI:
  2160. /* don't allow QP0 sends on guests */
  2161. err = -ENOSYS;
  2162. *bad_wr = wr;
  2163. goto out;
  2164. case MLX4_IB_QPT_PROXY_GSI:
  2165. /* If we are tunneling special qps, this is a UD qp.
  2166. * In this case we first add a UD segment targeting
  2167. * the tunnel qp, and then add a header with address
  2168. * information */
  2169. set_tunnel_datagram_seg(to_mdev(ibqp->device), wqe, wr, ibqp->qp_type);
  2170. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  2171. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  2172. build_tunnel_header(wr, wqe, &seglen);
  2173. wqe += seglen;
  2174. size += seglen / 16;
  2175. break;
  2176. case MLX4_IB_QPT_SMI:
  2177. case MLX4_IB_QPT_GSI:
  2178. err = build_mlx_header(to_msqp(qp), wr, ctrl, &seglen);
  2179. if (unlikely(err)) {
  2180. *bad_wr = wr;
  2181. goto out;
  2182. }
  2183. wqe += seglen;
  2184. size += seglen / 16;
  2185. break;
  2186. default:
  2187. break;
  2188. }
  2189. /*
  2190. * Write data segments in reverse order, so as to
  2191. * overwrite cacheline stamp last within each
  2192. * cacheline. This avoids issues with WQE
  2193. * prefetching.
  2194. */
  2195. dseg = wqe;
  2196. dseg += wr->num_sge - 1;
  2197. size += wr->num_sge * (sizeof (struct mlx4_wqe_data_seg) / 16);
  2198. /* Add one more inline data segment for ICRC for MLX sends */
  2199. if (unlikely(qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
  2200. qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI ||
  2201. qp->mlx4_ib_qp_type &
  2202. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))) {
  2203. set_mlx_icrc_seg(dseg + 1);
  2204. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  2205. }
  2206. for (i = wr->num_sge - 1; i >= 0; --i, --dseg)
  2207. set_data_seg(dseg, wr->sg_list + i);
  2208. /*
  2209. * Possibly overwrite stamping in cacheline with LSO
  2210. * segment only after making sure all data segments
  2211. * are written.
  2212. */
  2213. wmb();
  2214. *lso_wqe = lso_hdr_sz;
  2215. ctrl->fence_size = (wr->send_flags & IB_SEND_FENCE ?
  2216. MLX4_WQE_CTRL_FENCE : 0) | size;
  2217. /*
  2218. * Make sure descriptor is fully written before
  2219. * setting ownership bit (because HW can start
  2220. * executing as soon as we do).
  2221. */
  2222. wmb();
  2223. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  2224. *bad_wr = wr;
  2225. err = -EINVAL;
  2226. goto out;
  2227. }
  2228. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  2229. (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0) | blh;
  2230. stamp = ind + qp->sq_spare_wqes;
  2231. ind += DIV_ROUND_UP(size * 16, 1U << qp->sq.wqe_shift);
  2232. /*
  2233. * We can improve latency by not stamping the last
  2234. * send queue WQE until after ringing the doorbell, so
  2235. * only stamp here if there are still more WQEs to post.
  2236. *
  2237. * Same optimization applies to padding with NOP wqe
  2238. * in case of WQE shrinking (used to prevent wrap-around
  2239. * in the middle of WR).
  2240. */
  2241. if (wr->next) {
  2242. stamp_send_wqe(qp, stamp, size * 16);
  2243. ind = pad_wraparound(qp, ind);
  2244. }
  2245. }
  2246. out:
  2247. if (likely(nreq)) {
  2248. qp->sq.head += nreq;
  2249. /*
  2250. * Make sure that descriptors are written before
  2251. * doorbell record.
  2252. */
  2253. wmb();
  2254. writel(qp->doorbell_qpn,
  2255. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  2256. /*
  2257. * Make sure doorbells don't leak out of SQ spinlock
  2258. * and reach the HCA out of order.
  2259. */
  2260. mmiowb();
  2261. stamp_send_wqe(qp, stamp, size * 16);
  2262. ind = pad_wraparound(qp, ind);
  2263. qp->sq_next_wqe = ind;
  2264. }
  2265. spin_unlock_irqrestore(&qp->sq.lock, flags);
  2266. return err;
  2267. }
  2268. int mlx4_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  2269. struct ib_recv_wr **bad_wr)
  2270. {
  2271. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2272. struct mlx4_wqe_data_seg *scat;
  2273. unsigned long flags;
  2274. int err = 0;
  2275. int nreq;
  2276. int ind;
  2277. int max_gs;
  2278. int i;
  2279. max_gs = qp->rq.max_gs;
  2280. spin_lock_irqsave(&qp->rq.lock, flags);
  2281. ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
  2282. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  2283. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
  2284. err = -ENOMEM;
  2285. *bad_wr = wr;
  2286. goto out;
  2287. }
  2288. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  2289. err = -EINVAL;
  2290. *bad_wr = wr;
  2291. goto out;
  2292. }
  2293. scat = get_recv_wqe(qp, ind);
  2294. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  2295. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
  2296. ib_dma_sync_single_for_device(ibqp->device,
  2297. qp->sqp_proxy_rcv[ind].map,
  2298. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  2299. DMA_FROM_DEVICE);
  2300. scat->byte_count =
  2301. cpu_to_be32(sizeof (struct mlx4_ib_proxy_sqp_hdr));
  2302. /* use dma lkey from upper layer entry */
  2303. scat->lkey = cpu_to_be32(wr->sg_list->lkey);
  2304. scat->addr = cpu_to_be64(qp->sqp_proxy_rcv[ind].map);
  2305. scat++;
  2306. max_gs--;
  2307. }
  2308. for (i = 0; i < wr->num_sge; ++i)
  2309. __set_data_seg(scat + i, wr->sg_list + i);
  2310. if (i < max_gs) {
  2311. scat[i].byte_count = 0;
  2312. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  2313. scat[i].addr = 0;
  2314. }
  2315. qp->rq.wrid[ind] = wr->wr_id;
  2316. ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
  2317. }
  2318. out:
  2319. if (likely(nreq)) {
  2320. qp->rq.head += nreq;
  2321. /*
  2322. * Make sure that descriptors are written before
  2323. * doorbell record.
  2324. */
  2325. wmb();
  2326. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  2327. }
  2328. spin_unlock_irqrestore(&qp->rq.lock, flags);
  2329. return err;
  2330. }
  2331. static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state)
  2332. {
  2333. switch (mlx4_state) {
  2334. case MLX4_QP_STATE_RST: return IB_QPS_RESET;
  2335. case MLX4_QP_STATE_INIT: return IB_QPS_INIT;
  2336. case MLX4_QP_STATE_RTR: return IB_QPS_RTR;
  2337. case MLX4_QP_STATE_RTS: return IB_QPS_RTS;
  2338. case MLX4_QP_STATE_SQ_DRAINING:
  2339. case MLX4_QP_STATE_SQD: return IB_QPS_SQD;
  2340. case MLX4_QP_STATE_SQER: return IB_QPS_SQE;
  2341. case MLX4_QP_STATE_ERR: return IB_QPS_ERR;
  2342. default: return -1;
  2343. }
  2344. }
  2345. static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state)
  2346. {
  2347. switch (mlx4_mig_state) {
  2348. case MLX4_QP_PM_ARMED: return IB_MIG_ARMED;
  2349. case MLX4_QP_PM_REARM: return IB_MIG_REARM;
  2350. case MLX4_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
  2351. default: return -1;
  2352. }
  2353. }
  2354. static int to_ib_qp_access_flags(int mlx4_flags)
  2355. {
  2356. int ib_flags = 0;
  2357. if (mlx4_flags & MLX4_QP_BIT_RRE)
  2358. ib_flags |= IB_ACCESS_REMOTE_READ;
  2359. if (mlx4_flags & MLX4_QP_BIT_RWE)
  2360. ib_flags |= IB_ACCESS_REMOTE_WRITE;
  2361. if (mlx4_flags & MLX4_QP_BIT_RAE)
  2362. ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
  2363. return ib_flags;
  2364. }
  2365. static void to_ib_ah_attr(struct mlx4_ib_dev *ibdev, struct ib_ah_attr *ib_ah_attr,
  2366. struct mlx4_qp_path *path)
  2367. {
  2368. struct mlx4_dev *dev = ibdev->dev;
  2369. int is_eth;
  2370. memset(ib_ah_attr, 0, sizeof *ib_ah_attr);
  2371. ib_ah_attr->port_num = path->sched_queue & 0x40 ? 2 : 1;
  2372. if (ib_ah_attr->port_num == 0 || ib_ah_attr->port_num > dev->caps.num_ports)
  2373. return;
  2374. is_eth = rdma_port_get_link_layer(&ibdev->ib_dev, ib_ah_attr->port_num) ==
  2375. IB_LINK_LAYER_ETHERNET;
  2376. if (is_eth)
  2377. ib_ah_attr->sl = ((path->sched_queue >> 3) & 0x7) |
  2378. ((path->sched_queue & 4) << 1);
  2379. else
  2380. ib_ah_attr->sl = (path->sched_queue >> 2) & 0xf;
  2381. ib_ah_attr->dlid = be16_to_cpu(path->rlid);
  2382. ib_ah_attr->src_path_bits = path->grh_mylmc & 0x7f;
  2383. ib_ah_attr->static_rate = path->static_rate ? path->static_rate - 5 : 0;
  2384. ib_ah_attr->ah_flags = (path->grh_mylmc & (1 << 7)) ? IB_AH_GRH : 0;
  2385. if (ib_ah_attr->ah_flags) {
  2386. ib_ah_attr->grh.sgid_index = path->mgid_index;
  2387. ib_ah_attr->grh.hop_limit = path->hop_limit;
  2388. ib_ah_attr->grh.traffic_class =
  2389. (be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
  2390. ib_ah_attr->grh.flow_label =
  2391. be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
  2392. memcpy(ib_ah_attr->grh.dgid.raw,
  2393. path->rgid, sizeof ib_ah_attr->grh.dgid.raw);
  2394. }
  2395. }
  2396. int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
  2397. struct ib_qp_init_attr *qp_init_attr)
  2398. {
  2399. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  2400. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2401. struct mlx4_qp_context context;
  2402. int mlx4_state;
  2403. int err = 0;
  2404. mutex_lock(&qp->mutex);
  2405. if (qp->state == IB_QPS_RESET) {
  2406. qp_attr->qp_state = IB_QPS_RESET;
  2407. goto done;
  2408. }
  2409. err = mlx4_qp_query(dev->dev, &qp->mqp, &context);
  2410. if (err) {
  2411. err = -EINVAL;
  2412. goto out;
  2413. }
  2414. mlx4_state = be32_to_cpu(context.flags) >> 28;
  2415. qp->state = to_ib_qp_state(mlx4_state);
  2416. qp_attr->qp_state = qp->state;
  2417. qp_attr->path_mtu = context.mtu_msgmax >> 5;
  2418. qp_attr->path_mig_state =
  2419. to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3);
  2420. qp_attr->qkey = be32_to_cpu(context.qkey);
  2421. qp_attr->rq_psn = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff;
  2422. qp_attr->sq_psn = be32_to_cpu(context.next_send_psn) & 0xffffff;
  2423. qp_attr->dest_qp_num = be32_to_cpu(context.remote_qpn) & 0xffffff;
  2424. qp_attr->qp_access_flags =
  2425. to_ib_qp_access_flags(be32_to_cpu(context.params2));
  2426. if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
  2427. to_ib_ah_attr(dev, &qp_attr->ah_attr, &context.pri_path);
  2428. to_ib_ah_attr(dev, &qp_attr->alt_ah_attr, &context.alt_path);
  2429. qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f;
  2430. qp_attr->alt_port_num = qp_attr->alt_ah_attr.port_num;
  2431. }
  2432. qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f;
  2433. if (qp_attr->qp_state == IB_QPS_INIT)
  2434. qp_attr->port_num = qp->port;
  2435. else
  2436. qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1;
  2437. /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
  2438. qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING;
  2439. qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7);
  2440. qp_attr->max_dest_rd_atomic =
  2441. 1 << ((be32_to_cpu(context.params2) >> 21) & 0x7);
  2442. qp_attr->min_rnr_timer =
  2443. (be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f;
  2444. qp_attr->timeout = context.pri_path.ackto >> 3;
  2445. qp_attr->retry_cnt = (be32_to_cpu(context.params1) >> 16) & 0x7;
  2446. qp_attr->rnr_retry = (be32_to_cpu(context.params1) >> 13) & 0x7;
  2447. qp_attr->alt_timeout = context.alt_path.ackto >> 3;
  2448. done:
  2449. qp_attr->cur_qp_state = qp_attr->qp_state;
  2450. qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
  2451. qp_attr->cap.max_recv_sge = qp->rq.max_gs;
  2452. if (!ibqp->uobject) {
  2453. qp_attr->cap.max_send_wr = qp->sq.wqe_cnt;
  2454. qp_attr->cap.max_send_sge = qp->sq.max_gs;
  2455. } else {
  2456. qp_attr->cap.max_send_wr = 0;
  2457. qp_attr->cap.max_send_sge = 0;
  2458. }
  2459. /*
  2460. * We don't support inline sends for kernel QPs (yet), and we
  2461. * don't know what userspace's value should be.
  2462. */
  2463. qp_attr->cap.max_inline_data = 0;
  2464. qp_init_attr->cap = qp_attr->cap;
  2465. qp_init_attr->create_flags = 0;
  2466. if (qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK)
  2467. qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
  2468. if (qp->flags & MLX4_IB_QP_LSO)
  2469. qp_init_attr->create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;
  2470. if (qp->flags & MLX4_IB_QP_NETIF)
  2471. qp_init_attr->create_flags |= IB_QP_CREATE_NETIF_QP;
  2472. qp_init_attr->sq_sig_type =
  2473. qp->sq_signal_bits == cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) ?
  2474. IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
  2475. out:
  2476. mutex_unlock(&qp->mutex);
  2477. return err;
  2478. }