qp.c 93 KB

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