qp.c 90 KB

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