qp.c 78 KB

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