qp.c 79 KB

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