qp.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175
  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 <linux/mlx4/qp.h>
  40. #include "mlx4_ib.h"
  41. #include "user.h"
  42. enum {
  43. MLX4_IB_ACK_REQ_FREQ = 8,
  44. };
  45. enum {
  46. MLX4_IB_DEFAULT_SCHED_QUEUE = 0x83,
  47. MLX4_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
  48. MLX4_IB_LINK_TYPE_IB = 0,
  49. MLX4_IB_LINK_TYPE_ETH = 1
  50. };
  51. enum {
  52. /*
  53. * Largest possible UD header: send with GRH and immediate
  54. * data plus 18 bytes for an Ethernet header with VLAN/802.1Q
  55. * tag. (LRH would only use 8 bytes, so Ethernet is the
  56. * biggest case)
  57. */
  58. MLX4_IB_UD_HEADER_SIZE = 82,
  59. MLX4_IB_LSO_HEADER_SPARE = 128,
  60. };
  61. enum {
  62. MLX4_IB_IBOE_ETHERTYPE = 0x8915
  63. };
  64. struct mlx4_ib_sqp {
  65. struct mlx4_ib_qp qp;
  66. int pkey_index;
  67. u32 qkey;
  68. u32 send_psn;
  69. struct ib_ud_header ud_header;
  70. u8 header_buf[MLX4_IB_UD_HEADER_SIZE];
  71. };
  72. enum {
  73. MLX4_IB_MIN_SQ_STRIDE = 6,
  74. MLX4_IB_CACHE_LINE_SIZE = 64,
  75. };
  76. static const __be32 mlx4_ib_opcode[] = {
  77. [IB_WR_SEND] = cpu_to_be32(MLX4_OPCODE_SEND),
  78. [IB_WR_LSO] = cpu_to_be32(MLX4_OPCODE_LSO),
  79. [IB_WR_SEND_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_SEND_IMM),
  80. [IB_WR_RDMA_WRITE] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE),
  81. [IB_WR_RDMA_WRITE_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE_IMM),
  82. [IB_WR_RDMA_READ] = cpu_to_be32(MLX4_OPCODE_RDMA_READ),
  83. [IB_WR_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_ATOMIC_CS),
  84. [IB_WR_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_ATOMIC_FA),
  85. [IB_WR_SEND_WITH_INV] = cpu_to_be32(MLX4_OPCODE_SEND_INVAL),
  86. [IB_WR_LOCAL_INV] = cpu_to_be32(MLX4_OPCODE_LOCAL_INVAL),
  87. [IB_WR_FAST_REG_MR] = cpu_to_be32(MLX4_OPCODE_FMR),
  88. [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_CS),
  89. [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_FA),
  90. };
  91. static struct mlx4_ib_sqp *to_msqp(struct mlx4_ib_qp *mqp)
  92. {
  93. return container_of(mqp, struct mlx4_ib_sqp, qp);
  94. }
  95. static int is_sqp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  96. {
  97. return qp->mqp.qpn >= dev->dev->caps.sqp_start &&
  98. qp->mqp.qpn <= dev->dev->caps.sqp_start + 3;
  99. }
  100. static int is_qp0(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  101. {
  102. return qp->mqp.qpn >= dev->dev->caps.sqp_start &&
  103. qp->mqp.qpn <= dev->dev->caps.sqp_start + 1;
  104. }
  105. static void *get_wqe(struct mlx4_ib_qp *qp, int offset)
  106. {
  107. return mlx4_buf_offset(&qp->buf, offset);
  108. }
  109. static void *get_recv_wqe(struct mlx4_ib_qp *qp, int n)
  110. {
  111. return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
  112. }
  113. static void *get_send_wqe(struct mlx4_ib_qp *qp, int n)
  114. {
  115. return get_wqe(qp, qp->sq.offset + (n << qp->sq.wqe_shift));
  116. }
  117. /*
  118. * Stamp a SQ WQE so that it is invalid if prefetched by marking the
  119. * first four bytes of every 64 byte chunk with
  120. * 0x7FFFFFF | (invalid_ownership_value << 31).
  121. *
  122. * When the max work request size is less than or equal to the WQE
  123. * basic block size, as an optimization, we can stamp all WQEs with
  124. * 0xffffffff, and skip the very first chunk of each WQE.
  125. */
  126. static void stamp_send_wqe(struct mlx4_ib_qp *qp, int n, int size)
  127. {
  128. __be32 *wqe;
  129. int i;
  130. int s;
  131. int ind;
  132. void *buf;
  133. __be32 stamp;
  134. struct mlx4_wqe_ctrl_seg *ctrl;
  135. if (qp->sq_max_wqes_per_wr > 1) {
  136. s = roundup(size, 1U << qp->sq.wqe_shift);
  137. for (i = 0; i < s; i += 64) {
  138. ind = (i >> qp->sq.wqe_shift) + n;
  139. stamp = ind & qp->sq.wqe_cnt ? cpu_to_be32(0x7fffffff) :
  140. cpu_to_be32(0xffffffff);
  141. buf = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  142. wqe = buf + (i & ((1 << qp->sq.wqe_shift) - 1));
  143. *wqe = stamp;
  144. }
  145. } else {
  146. ctrl = buf = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
  147. s = (ctrl->fence_size & 0x3f) << 4;
  148. for (i = 64; i < s; i += 64) {
  149. wqe = buf + i;
  150. *wqe = cpu_to_be32(0xffffffff);
  151. }
  152. }
  153. }
  154. static void post_nop_wqe(struct mlx4_ib_qp *qp, int n, int size)
  155. {
  156. struct mlx4_wqe_ctrl_seg *ctrl;
  157. struct mlx4_wqe_inline_seg *inl;
  158. void *wqe;
  159. int s;
  160. ctrl = wqe = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
  161. s = sizeof(struct mlx4_wqe_ctrl_seg);
  162. if (qp->ibqp.qp_type == IB_QPT_UD) {
  163. struct mlx4_wqe_datagram_seg *dgram = wqe + sizeof *ctrl;
  164. struct mlx4_av *av = (struct mlx4_av *)dgram->av;
  165. memset(dgram, 0, sizeof *dgram);
  166. av->port_pd = cpu_to_be32((qp->port << 24) | to_mpd(qp->ibqp.pd)->pdn);
  167. s += sizeof(struct mlx4_wqe_datagram_seg);
  168. }
  169. /* Pad the remainder of the WQE with an inline data segment. */
  170. if (size > s) {
  171. inl = wqe + s;
  172. inl->byte_count = cpu_to_be32(1 << 31 | (size - s - sizeof *inl));
  173. }
  174. ctrl->srcrb_flags = 0;
  175. ctrl->fence_size = size / 16;
  176. /*
  177. * Make sure descriptor is fully written before setting ownership bit
  178. * (because HW can start executing as soon as we do).
  179. */
  180. wmb();
  181. ctrl->owner_opcode = cpu_to_be32(MLX4_OPCODE_NOP | MLX4_WQE_CTRL_NEC) |
  182. (n & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0);
  183. stamp_send_wqe(qp, n + qp->sq_spare_wqes, size);
  184. }
  185. /* Post NOP WQE to prevent wrap-around in the middle of WR */
  186. static inline unsigned pad_wraparound(struct mlx4_ib_qp *qp, int ind)
  187. {
  188. unsigned s = qp->sq.wqe_cnt - (ind & (qp->sq.wqe_cnt - 1));
  189. if (unlikely(s < qp->sq_max_wqes_per_wr)) {
  190. post_nop_wqe(qp, ind, s << qp->sq.wqe_shift);
  191. ind += s;
  192. }
  193. return ind;
  194. }
  195. static void mlx4_ib_qp_event(struct mlx4_qp *qp, enum mlx4_event type)
  196. {
  197. struct ib_event event;
  198. struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
  199. if (type == MLX4_EVENT_TYPE_PATH_MIG)
  200. to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
  201. if (ibqp->event_handler) {
  202. event.device = ibqp->device;
  203. event.element.qp = ibqp;
  204. switch (type) {
  205. case MLX4_EVENT_TYPE_PATH_MIG:
  206. event.event = IB_EVENT_PATH_MIG;
  207. break;
  208. case MLX4_EVENT_TYPE_COMM_EST:
  209. event.event = IB_EVENT_COMM_EST;
  210. break;
  211. case MLX4_EVENT_TYPE_SQ_DRAINED:
  212. event.event = IB_EVENT_SQ_DRAINED;
  213. break;
  214. case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
  215. event.event = IB_EVENT_QP_LAST_WQE_REACHED;
  216. break;
  217. case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
  218. event.event = IB_EVENT_QP_FATAL;
  219. break;
  220. case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
  221. event.event = IB_EVENT_PATH_MIG_ERR;
  222. break;
  223. case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  224. event.event = IB_EVENT_QP_REQ_ERR;
  225. break;
  226. case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
  227. event.event = IB_EVENT_QP_ACCESS_ERR;
  228. break;
  229. default:
  230. printk(KERN_WARNING "mlx4_ib: Unexpected event type %d "
  231. "on QP %06x\n", type, qp->qpn);
  232. return;
  233. }
  234. ibqp->event_handler(&event, ibqp->qp_context);
  235. }
  236. }
  237. static int send_wqe_overhead(enum ib_qp_type type, u32 flags)
  238. {
  239. /*
  240. * UD WQEs must have a datagram segment.
  241. * RC and UC WQEs might have a remote address segment.
  242. * MLX WQEs need two extra inline data segments (for the UD
  243. * header and space for the ICRC).
  244. */
  245. switch (type) {
  246. case IB_QPT_UD:
  247. return sizeof (struct mlx4_wqe_ctrl_seg) +
  248. sizeof (struct mlx4_wqe_datagram_seg) +
  249. ((flags & MLX4_IB_QP_LSO) ? MLX4_IB_LSO_HEADER_SPARE : 0);
  250. case IB_QPT_UC:
  251. return sizeof (struct mlx4_wqe_ctrl_seg) +
  252. sizeof (struct mlx4_wqe_raddr_seg);
  253. case IB_QPT_RC:
  254. return sizeof (struct mlx4_wqe_ctrl_seg) +
  255. sizeof (struct mlx4_wqe_atomic_seg) +
  256. sizeof (struct mlx4_wqe_raddr_seg);
  257. case IB_QPT_SMI:
  258. case IB_QPT_GSI:
  259. return sizeof (struct mlx4_wqe_ctrl_seg) +
  260. ALIGN(MLX4_IB_UD_HEADER_SIZE +
  261. DIV_ROUND_UP(MLX4_IB_UD_HEADER_SIZE,
  262. MLX4_INLINE_ALIGN) *
  263. sizeof (struct mlx4_wqe_inline_seg),
  264. sizeof (struct mlx4_wqe_data_seg)) +
  265. ALIGN(4 +
  266. sizeof (struct mlx4_wqe_inline_seg),
  267. sizeof (struct mlx4_wqe_data_seg));
  268. default:
  269. return sizeof (struct mlx4_wqe_ctrl_seg);
  270. }
  271. }
  272. static int set_rq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  273. int is_user, int has_rq, struct mlx4_ib_qp *qp)
  274. {
  275. /* Sanity check RQ size before proceeding */
  276. if (cap->max_recv_wr > dev->dev->caps.max_wqes ||
  277. cap->max_recv_sge > dev->dev->caps.max_rq_sg)
  278. return -EINVAL;
  279. if (!has_rq) {
  280. if (cap->max_recv_wr)
  281. return -EINVAL;
  282. qp->rq.wqe_cnt = qp->rq.max_gs = 0;
  283. } else {
  284. /* HW requires >= 1 RQ entry with >= 1 gather entry */
  285. if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge))
  286. return -EINVAL;
  287. qp->rq.wqe_cnt = roundup_pow_of_two(max(1U, cap->max_recv_wr));
  288. qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge));
  289. qp->rq.wqe_shift = ilog2(qp->rq.max_gs * sizeof (struct mlx4_wqe_data_seg));
  290. }
  291. cap->max_recv_wr = qp->rq.max_post = qp->rq.wqe_cnt;
  292. cap->max_recv_sge = qp->rq.max_gs;
  293. return 0;
  294. }
  295. static int set_kernel_sq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  296. enum ib_qp_type type, struct mlx4_ib_qp *qp)
  297. {
  298. int s;
  299. /* Sanity check SQ size before proceeding */
  300. if (cap->max_send_wr > dev->dev->caps.max_wqes ||
  301. cap->max_send_sge > dev->dev->caps.max_sq_sg ||
  302. cap->max_inline_data + send_wqe_overhead(type, qp->flags) +
  303. sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz)
  304. return -EINVAL;
  305. /*
  306. * For MLX transport we need 2 extra S/G entries:
  307. * one for the header and one for the checksum at the end
  308. */
  309. if ((type == IB_QPT_SMI || type == IB_QPT_GSI) &&
  310. cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg)
  311. return -EINVAL;
  312. s = max(cap->max_send_sge * sizeof (struct mlx4_wqe_data_seg),
  313. cap->max_inline_data + sizeof (struct mlx4_wqe_inline_seg)) +
  314. send_wqe_overhead(type, qp->flags);
  315. if (s > dev->dev->caps.max_sq_desc_sz)
  316. return -EINVAL;
  317. /*
  318. * Hermon supports shrinking WQEs, such that a single work
  319. * request can include multiple units of 1 << wqe_shift. This
  320. * way, work requests can differ in size, and do not have to
  321. * be a power of 2 in size, saving memory and speeding up send
  322. * WR posting. Unfortunately, if we do this then the
  323. * wqe_index field in CQEs can't be used to look up the WR ID
  324. * anymore, so we do this only if selective signaling is off.
  325. *
  326. * Further, on 32-bit platforms, we can't use vmap() to make
  327. * the QP buffer virtually contiguous. Thus we have to use
  328. * constant-sized WRs to make sure a WR is always fully within
  329. * a single page-sized chunk.
  330. *
  331. * Finally, we use NOP work requests to pad the end of the
  332. * work queue, to avoid wrap-around in the middle of WR. We
  333. * set NEC bit to avoid getting completions with error for
  334. * these NOP WRs, but since NEC is only supported starting
  335. * with firmware 2.2.232, we use constant-sized WRs for older
  336. * firmware.
  337. *
  338. * And, since MLX QPs only support SEND, we use constant-sized
  339. * WRs in this case.
  340. *
  341. * We look for the smallest value of wqe_shift such that the
  342. * resulting number of wqes does not exceed device
  343. * capabilities.
  344. *
  345. * We set WQE size to at least 64 bytes, this way stamping
  346. * invalidates each WQE.
  347. */
  348. if (dev->dev->caps.fw_ver >= MLX4_FW_VER_WQE_CTRL_NEC &&
  349. qp->sq_signal_bits && BITS_PER_LONG == 64 &&
  350. type != IB_QPT_SMI && type != IB_QPT_GSI)
  351. qp->sq.wqe_shift = ilog2(64);
  352. else
  353. qp->sq.wqe_shift = ilog2(roundup_pow_of_two(s));
  354. for (;;) {
  355. qp->sq_max_wqes_per_wr = DIV_ROUND_UP(s, 1U << qp->sq.wqe_shift);
  356. /*
  357. * We need to leave 2 KB + 1 WR of headroom in the SQ to
  358. * allow HW to prefetch.
  359. */
  360. qp->sq_spare_wqes = (2048 >> qp->sq.wqe_shift) + qp->sq_max_wqes_per_wr;
  361. qp->sq.wqe_cnt = roundup_pow_of_two(cap->max_send_wr *
  362. qp->sq_max_wqes_per_wr +
  363. qp->sq_spare_wqes);
  364. if (qp->sq.wqe_cnt <= dev->dev->caps.max_wqes)
  365. break;
  366. if (qp->sq_max_wqes_per_wr <= 1)
  367. return -EINVAL;
  368. ++qp->sq.wqe_shift;
  369. }
  370. qp->sq.max_gs = (min(dev->dev->caps.max_sq_desc_sz,
  371. (qp->sq_max_wqes_per_wr << qp->sq.wqe_shift)) -
  372. send_wqe_overhead(type, qp->flags)) /
  373. sizeof (struct mlx4_wqe_data_seg);
  374. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  375. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  376. if (qp->rq.wqe_shift > qp->sq.wqe_shift) {
  377. qp->rq.offset = 0;
  378. qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
  379. } else {
  380. qp->rq.offset = qp->sq.wqe_cnt << qp->sq.wqe_shift;
  381. qp->sq.offset = 0;
  382. }
  383. cap->max_send_wr = qp->sq.max_post =
  384. (qp->sq.wqe_cnt - qp->sq_spare_wqes) / qp->sq_max_wqes_per_wr;
  385. cap->max_send_sge = min(qp->sq.max_gs,
  386. min(dev->dev->caps.max_sq_sg,
  387. dev->dev->caps.max_rq_sg));
  388. /* We don't support inline sends for kernel QPs (yet) */
  389. cap->max_inline_data = 0;
  390. return 0;
  391. }
  392. static int set_user_sq_size(struct mlx4_ib_dev *dev,
  393. struct mlx4_ib_qp *qp,
  394. struct mlx4_ib_create_qp *ucmd)
  395. {
  396. /* Sanity check SQ size before proceeding */
  397. if ((1 << ucmd->log_sq_bb_count) > dev->dev->caps.max_wqes ||
  398. ucmd->log_sq_stride >
  399. ilog2(roundup_pow_of_two(dev->dev->caps.max_sq_desc_sz)) ||
  400. ucmd->log_sq_stride < MLX4_IB_MIN_SQ_STRIDE)
  401. return -EINVAL;
  402. qp->sq.wqe_cnt = 1 << ucmd->log_sq_bb_count;
  403. qp->sq.wqe_shift = ucmd->log_sq_stride;
  404. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  405. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  406. return 0;
  407. }
  408. static int qp_has_rq(struct ib_qp_init_attr *attr)
  409. {
  410. if (attr->qp_type == IB_QPT_XRC_INI || attr->qp_type == IB_QPT_XRC_TGT)
  411. return 0;
  412. return !attr->srq;
  413. }
  414. static int create_qp_common(struct mlx4_ib_dev *dev, struct ib_pd *pd,
  415. struct ib_qp_init_attr *init_attr,
  416. struct ib_udata *udata, int sqpn, struct mlx4_ib_qp *qp)
  417. {
  418. int qpn;
  419. int err;
  420. mutex_init(&qp->mutex);
  421. spin_lock_init(&qp->sq.lock);
  422. spin_lock_init(&qp->rq.lock);
  423. INIT_LIST_HEAD(&qp->gid_list);
  424. qp->state = IB_QPS_RESET;
  425. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  426. qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  427. err = set_rq_size(dev, &init_attr->cap, !!pd->uobject, qp_has_rq(init_attr), qp);
  428. if (err)
  429. goto err;
  430. if (pd->uobject) {
  431. struct mlx4_ib_create_qp ucmd;
  432. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  433. err = -EFAULT;
  434. goto err;
  435. }
  436. qp->sq_no_prefetch = ucmd.sq_no_prefetch;
  437. err = set_user_sq_size(dev, qp, &ucmd);
  438. if (err)
  439. goto err;
  440. qp->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
  441. qp->buf_size, 0, 0);
  442. if (IS_ERR(qp->umem)) {
  443. err = PTR_ERR(qp->umem);
  444. goto err;
  445. }
  446. err = mlx4_mtt_init(dev->dev, ib_umem_page_count(qp->umem),
  447. ilog2(qp->umem->page_size), &qp->mtt);
  448. if (err)
  449. goto err_buf;
  450. err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
  451. if (err)
  452. goto err_mtt;
  453. if (qp_has_rq(init_attr)) {
  454. err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
  455. ucmd.db_addr, &qp->db);
  456. if (err)
  457. goto err_mtt;
  458. }
  459. } else {
  460. qp->sq_no_prefetch = 0;
  461. if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
  462. qp->flags |= MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
  463. if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
  464. qp->flags |= MLX4_IB_QP_LSO;
  465. err = set_kernel_sq_size(dev, &init_attr->cap, init_attr->qp_type, qp);
  466. if (err)
  467. goto err;
  468. if (qp_has_rq(init_attr)) {
  469. err = mlx4_db_alloc(dev->dev, &qp->db, 0);
  470. if (err)
  471. goto err;
  472. *qp->db.db = 0;
  473. }
  474. if (mlx4_buf_alloc(dev->dev, qp->buf_size, PAGE_SIZE * 2, &qp->buf)) {
  475. err = -ENOMEM;
  476. goto err_db;
  477. }
  478. err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift,
  479. &qp->mtt);
  480. if (err)
  481. goto err_buf;
  482. err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf);
  483. if (err)
  484. goto err_mtt;
  485. qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof (u64), GFP_KERNEL);
  486. qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof (u64), GFP_KERNEL);
  487. if (!qp->sq.wrid || !qp->rq.wrid) {
  488. err = -ENOMEM;
  489. goto err_wrid;
  490. }
  491. }
  492. if (sqpn) {
  493. qpn = sqpn;
  494. } else {
  495. err = mlx4_qp_reserve_range(dev->dev, 1, 1, &qpn);
  496. if (err)
  497. goto err_wrid;
  498. }
  499. err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp);
  500. if (err)
  501. goto err_qpn;
  502. if (init_attr->qp_type == IB_QPT_XRC_TGT)
  503. qp->mqp.qpn |= (1 << 23);
  504. /*
  505. * Hardware wants QPN written in big-endian order (after
  506. * shifting) for send doorbell. Precompute this value to save
  507. * a little bit when posting sends.
  508. */
  509. qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
  510. qp->mqp.event = mlx4_ib_qp_event;
  511. return 0;
  512. err_qpn:
  513. if (!sqpn)
  514. mlx4_qp_release_range(dev->dev, qpn, 1);
  515. err_wrid:
  516. if (pd->uobject) {
  517. if (qp_has_rq(init_attr))
  518. mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &qp->db);
  519. } else {
  520. kfree(qp->sq.wrid);
  521. kfree(qp->rq.wrid);
  522. }
  523. err_mtt:
  524. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  525. err_buf:
  526. if (pd->uobject)
  527. ib_umem_release(qp->umem);
  528. else
  529. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  530. err_db:
  531. if (!pd->uobject && qp_has_rq(init_attr))
  532. mlx4_db_free(dev->dev, &qp->db);
  533. err:
  534. return err;
  535. }
  536. static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
  537. {
  538. switch (state) {
  539. case IB_QPS_RESET: return MLX4_QP_STATE_RST;
  540. case IB_QPS_INIT: return MLX4_QP_STATE_INIT;
  541. case IB_QPS_RTR: return MLX4_QP_STATE_RTR;
  542. case IB_QPS_RTS: return MLX4_QP_STATE_RTS;
  543. case IB_QPS_SQD: return MLX4_QP_STATE_SQD;
  544. case IB_QPS_SQE: return MLX4_QP_STATE_SQER;
  545. case IB_QPS_ERR: return MLX4_QP_STATE_ERR;
  546. default: return -1;
  547. }
  548. }
  549. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  550. __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
  551. {
  552. if (send_cq == recv_cq) {
  553. spin_lock_irq(&send_cq->lock);
  554. __acquire(&recv_cq->lock);
  555. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  556. spin_lock_irq(&send_cq->lock);
  557. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  558. } else {
  559. spin_lock_irq(&recv_cq->lock);
  560. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  561. }
  562. }
  563. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  564. __releases(&send_cq->lock) __releases(&recv_cq->lock)
  565. {
  566. if (send_cq == recv_cq) {
  567. __release(&recv_cq->lock);
  568. spin_unlock_irq(&send_cq->lock);
  569. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  570. spin_unlock(&recv_cq->lock);
  571. spin_unlock_irq(&send_cq->lock);
  572. } else {
  573. spin_unlock(&send_cq->lock);
  574. spin_unlock_irq(&recv_cq->lock);
  575. }
  576. }
  577. static void del_gid_entries(struct mlx4_ib_qp *qp)
  578. {
  579. struct mlx4_ib_gid_entry *ge, *tmp;
  580. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  581. list_del(&ge->list);
  582. kfree(ge);
  583. }
  584. }
  585. static struct mlx4_ib_pd *get_pd(struct mlx4_ib_qp *qp)
  586. {
  587. if (qp->ibqp.qp_type == IB_QPT_XRC_TGT)
  588. return to_mpd(to_mxrcd(qp->ibqp.xrcd)->pd);
  589. else
  590. return to_mpd(qp->ibqp.pd);
  591. }
  592. static void get_cqs(struct mlx4_ib_qp *qp,
  593. struct mlx4_ib_cq **send_cq, struct mlx4_ib_cq **recv_cq)
  594. {
  595. switch (qp->ibqp.qp_type) {
  596. case IB_QPT_XRC_TGT:
  597. *send_cq = to_mcq(to_mxrcd(qp->ibqp.xrcd)->cq);
  598. *recv_cq = *send_cq;
  599. break;
  600. case IB_QPT_XRC_INI:
  601. *send_cq = to_mcq(qp->ibqp.send_cq);
  602. *recv_cq = *send_cq;
  603. break;
  604. default:
  605. *send_cq = to_mcq(qp->ibqp.send_cq);
  606. *recv_cq = to_mcq(qp->ibqp.recv_cq);
  607. break;
  608. }
  609. }
  610. static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
  611. int is_user)
  612. {
  613. struct mlx4_ib_cq *send_cq, *recv_cq;
  614. if (qp->state != IB_QPS_RESET)
  615. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  616. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  617. printk(KERN_WARNING "mlx4_ib: modify QP %06x to RESET failed.\n",
  618. qp->mqp.qpn);
  619. get_cqs(qp, &send_cq, &recv_cq);
  620. mlx4_ib_lock_cqs(send_cq, recv_cq);
  621. if (!is_user) {
  622. __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  623. qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
  624. if (send_cq != recv_cq)
  625. __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  626. }
  627. mlx4_qp_remove(dev->dev, &qp->mqp);
  628. mlx4_ib_unlock_cqs(send_cq, recv_cq);
  629. mlx4_qp_free(dev->dev, &qp->mqp);
  630. if (!is_sqp(dev, qp))
  631. mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1);
  632. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  633. if (is_user) {
  634. if (qp->rq.wqe_cnt)
  635. mlx4_ib_db_unmap_user(to_mucontext(qp->ibqp.uobject->context),
  636. &qp->db);
  637. ib_umem_release(qp->umem);
  638. } else {
  639. kfree(qp->sq.wrid);
  640. kfree(qp->rq.wrid);
  641. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  642. if (qp->rq.wqe_cnt)
  643. mlx4_db_free(dev->dev, &qp->db);
  644. }
  645. del_gid_entries(qp);
  646. }
  647. struct ib_qp *mlx4_ib_create_qp(struct ib_pd *pd,
  648. struct ib_qp_init_attr *init_attr,
  649. struct ib_udata *udata)
  650. {
  651. struct mlx4_ib_sqp *sqp;
  652. struct mlx4_ib_qp *qp;
  653. int err;
  654. u16 xrcdn = 0;
  655. /*
  656. * We only support LSO and multicast loopback blocking, and
  657. * only for kernel UD QPs.
  658. */
  659. if (init_attr->create_flags & ~(IB_QP_CREATE_IPOIB_UD_LSO |
  660. IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK))
  661. return ERR_PTR(-EINVAL);
  662. if (init_attr->create_flags &&
  663. (udata || init_attr->qp_type != IB_QPT_UD))
  664. return ERR_PTR(-EINVAL);
  665. switch (init_attr->qp_type) {
  666. case IB_QPT_XRC_TGT:
  667. pd = to_mxrcd(init_attr->xrcd)->pd;
  668. xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
  669. init_attr->send_cq = to_mxrcd(init_attr->xrcd)->cq;
  670. /* fall through */
  671. case IB_QPT_XRC_INI:
  672. if (!(to_mdev(pd->device)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
  673. return ERR_PTR(-ENOSYS);
  674. init_attr->recv_cq = init_attr->send_cq;
  675. /* fall through */
  676. case IB_QPT_RC:
  677. case IB_QPT_UC:
  678. case IB_QPT_UD:
  679. {
  680. qp = kzalloc(sizeof *qp, GFP_KERNEL);
  681. if (!qp)
  682. return ERR_PTR(-ENOMEM);
  683. err = create_qp_common(to_mdev(pd->device), pd, init_attr, udata, 0, qp);
  684. if (err) {
  685. kfree(qp);
  686. return ERR_PTR(err);
  687. }
  688. qp->ibqp.qp_num = qp->mqp.qpn;
  689. qp->xrcdn = xrcdn;
  690. break;
  691. }
  692. case IB_QPT_SMI:
  693. case IB_QPT_GSI:
  694. {
  695. /* Userspace is not allowed to create special QPs: */
  696. if (udata)
  697. return ERR_PTR(-EINVAL);
  698. sqp = kzalloc(sizeof *sqp, GFP_KERNEL);
  699. if (!sqp)
  700. return ERR_PTR(-ENOMEM);
  701. qp = &sqp->qp;
  702. err = create_qp_common(to_mdev(pd->device), pd, init_attr, udata,
  703. to_mdev(pd->device)->dev->caps.sqp_start +
  704. (init_attr->qp_type == IB_QPT_SMI ? 0 : 2) +
  705. init_attr->port_num - 1,
  706. qp);
  707. if (err) {
  708. kfree(sqp);
  709. return ERR_PTR(err);
  710. }
  711. qp->port = init_attr->port_num;
  712. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
  713. break;
  714. }
  715. default:
  716. /* Don't support raw QPs */
  717. return ERR_PTR(-EINVAL);
  718. }
  719. return &qp->ibqp;
  720. }
  721. int mlx4_ib_destroy_qp(struct ib_qp *qp)
  722. {
  723. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  724. struct mlx4_ib_qp *mqp = to_mqp(qp);
  725. struct mlx4_ib_pd *pd;
  726. if (is_qp0(dev, mqp))
  727. mlx4_CLOSE_PORT(dev->dev, mqp->port);
  728. pd = get_pd(mqp);
  729. destroy_qp_common(dev, mqp, !!pd->ibpd.uobject);
  730. if (is_sqp(dev, mqp))
  731. kfree(to_msqp(mqp));
  732. else
  733. kfree(mqp);
  734. return 0;
  735. }
  736. static int to_mlx4_st(enum ib_qp_type type)
  737. {
  738. switch (type) {
  739. case IB_QPT_RC: return MLX4_QP_ST_RC;
  740. case IB_QPT_UC: return MLX4_QP_ST_UC;
  741. case IB_QPT_UD: return MLX4_QP_ST_UD;
  742. case IB_QPT_XRC_INI:
  743. case IB_QPT_XRC_TGT: return MLX4_QP_ST_XRC;
  744. case IB_QPT_SMI:
  745. case IB_QPT_GSI: return MLX4_QP_ST_MLX;
  746. default: return -1;
  747. }
  748. }
  749. static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
  750. int attr_mask)
  751. {
  752. u8 dest_rd_atomic;
  753. u32 access_flags;
  754. u32 hw_access_flags = 0;
  755. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  756. dest_rd_atomic = attr->max_dest_rd_atomic;
  757. else
  758. dest_rd_atomic = qp->resp_depth;
  759. if (attr_mask & IB_QP_ACCESS_FLAGS)
  760. access_flags = attr->qp_access_flags;
  761. else
  762. access_flags = qp->atomic_rd_en;
  763. if (!dest_rd_atomic)
  764. access_flags &= IB_ACCESS_REMOTE_WRITE;
  765. if (access_flags & IB_ACCESS_REMOTE_READ)
  766. hw_access_flags |= MLX4_QP_BIT_RRE;
  767. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  768. hw_access_flags |= MLX4_QP_BIT_RAE;
  769. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  770. hw_access_flags |= MLX4_QP_BIT_RWE;
  771. return cpu_to_be32(hw_access_flags);
  772. }
  773. static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
  774. int attr_mask)
  775. {
  776. if (attr_mask & IB_QP_PKEY_INDEX)
  777. sqp->pkey_index = attr->pkey_index;
  778. if (attr_mask & IB_QP_QKEY)
  779. sqp->qkey = attr->qkey;
  780. if (attr_mask & IB_QP_SQ_PSN)
  781. sqp->send_psn = attr->sq_psn;
  782. }
  783. static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
  784. {
  785. path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
  786. }
  787. static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_ah_attr *ah,
  788. struct mlx4_qp_path *path, u8 port)
  789. {
  790. int err;
  791. int is_eth = rdma_port_get_link_layer(&dev->ib_dev, port) ==
  792. IB_LINK_LAYER_ETHERNET;
  793. u8 mac[6];
  794. int is_mcast;
  795. u16 vlan_tag;
  796. int vidx;
  797. path->grh_mylmc = ah->src_path_bits & 0x7f;
  798. path->rlid = cpu_to_be16(ah->dlid);
  799. if (ah->static_rate) {
  800. path->static_rate = ah->static_rate + MLX4_STAT_RATE_OFFSET;
  801. while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  802. !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
  803. --path->static_rate;
  804. } else
  805. path->static_rate = 0;
  806. if (ah->ah_flags & IB_AH_GRH) {
  807. if (ah->grh.sgid_index >= dev->dev->caps.gid_table_len[port]) {
  808. printk(KERN_ERR "sgid_index (%u) too large. max is %d\n",
  809. ah->grh.sgid_index, dev->dev->caps.gid_table_len[port] - 1);
  810. return -1;
  811. }
  812. path->grh_mylmc |= 1 << 7;
  813. path->mgid_index = ah->grh.sgid_index;
  814. path->hop_limit = ah->grh.hop_limit;
  815. path->tclass_flowlabel =
  816. cpu_to_be32((ah->grh.traffic_class << 20) |
  817. (ah->grh.flow_label));
  818. memcpy(path->rgid, ah->grh.dgid.raw, 16);
  819. }
  820. if (is_eth) {
  821. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  822. ((port - 1) << 6) | ((ah->sl & 7) << 3);
  823. if (!(ah->ah_flags & IB_AH_GRH))
  824. return -1;
  825. err = mlx4_ib_resolve_grh(dev, ah, mac, &is_mcast, port);
  826. if (err)
  827. return err;
  828. memcpy(path->dmac, mac, 6);
  829. path->ackto = MLX4_IB_LINK_TYPE_ETH;
  830. /* use index 0 into MAC table for IBoE */
  831. path->grh_mylmc &= 0x80;
  832. vlan_tag = rdma_get_vlan_id(&dev->iboe.gid_table[port - 1][ah->grh.sgid_index]);
  833. if (vlan_tag < 0x1000) {
  834. if (mlx4_find_cached_vlan(dev->dev, port, vlan_tag, &vidx))
  835. return -ENOENT;
  836. path->vlan_index = vidx;
  837. path->fl = 1 << 6;
  838. }
  839. } else
  840. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  841. ((port - 1) << 6) | ((ah->sl & 0xf) << 2);
  842. return 0;
  843. }
  844. static void update_mcg_macs(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  845. {
  846. struct mlx4_ib_gid_entry *ge, *tmp;
  847. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  848. if (!ge->added && mlx4_ib_add_mc(dev, qp, &ge->gid)) {
  849. ge->added = 1;
  850. ge->port = qp->port;
  851. }
  852. }
  853. }
  854. static int __mlx4_ib_modify_qp(struct ib_qp *ibqp,
  855. const struct ib_qp_attr *attr, int attr_mask,
  856. enum ib_qp_state cur_state, enum ib_qp_state new_state)
  857. {
  858. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  859. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  860. struct mlx4_ib_pd *pd;
  861. struct mlx4_ib_cq *send_cq, *recv_cq;
  862. struct mlx4_qp_context *context;
  863. enum mlx4_qp_optpar optpar = 0;
  864. int sqd_event;
  865. int err = -EINVAL;
  866. context = kzalloc(sizeof *context, GFP_KERNEL);
  867. if (!context)
  868. return -ENOMEM;
  869. context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
  870. (to_mlx4_st(ibqp->qp_type) << 16));
  871. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  872. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  873. else {
  874. optpar |= MLX4_QP_OPTPAR_PM_STATE;
  875. switch (attr->path_mig_state) {
  876. case IB_MIG_MIGRATED:
  877. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  878. break;
  879. case IB_MIG_REARM:
  880. context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
  881. break;
  882. case IB_MIG_ARMED:
  883. context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
  884. break;
  885. }
  886. }
  887. if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI)
  888. context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
  889. else if (ibqp->qp_type == IB_QPT_UD) {
  890. if (qp->flags & MLX4_IB_QP_LSO)
  891. context->mtu_msgmax = (IB_MTU_4096 << 5) |
  892. ilog2(dev->dev->caps.max_gso_sz);
  893. else
  894. context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
  895. } else if (attr_mask & IB_QP_PATH_MTU) {
  896. if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
  897. printk(KERN_ERR "path MTU (%u) is invalid\n",
  898. attr->path_mtu);
  899. goto out;
  900. }
  901. context->mtu_msgmax = (attr->path_mtu << 5) |
  902. ilog2(dev->dev->caps.max_msg_sz);
  903. }
  904. if (qp->rq.wqe_cnt)
  905. context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3;
  906. context->rq_size_stride |= qp->rq.wqe_shift - 4;
  907. if (qp->sq.wqe_cnt)
  908. context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3;
  909. context->sq_size_stride |= qp->sq.wqe_shift - 4;
  910. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  911. context->sq_size_stride |= !!qp->sq_no_prefetch << 7;
  912. context->xrcd = cpu_to_be32((u32) qp->xrcdn);
  913. }
  914. if (qp->ibqp.uobject)
  915. context->usr_page = cpu_to_be32(to_mucontext(ibqp->uobject->context)->uar.index);
  916. else
  917. context->usr_page = cpu_to_be32(dev->priv_uar.index);
  918. if (attr_mask & IB_QP_DEST_QPN)
  919. context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  920. if (attr_mask & IB_QP_PORT) {
  921. if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
  922. !(attr_mask & IB_QP_AV)) {
  923. mlx4_set_sched(&context->pri_path, attr->port_num);
  924. optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
  925. }
  926. }
  927. if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
  928. if (dev->counters[qp->port - 1] != -1) {
  929. context->pri_path.counter_index =
  930. dev->counters[qp->port - 1];
  931. optpar |= MLX4_QP_OPTPAR_COUNTER_INDEX;
  932. } else
  933. context->pri_path.counter_index = 0xff;
  934. }
  935. if (attr_mask & IB_QP_PKEY_INDEX) {
  936. context->pri_path.pkey_index = attr->pkey_index;
  937. optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
  938. }
  939. if (attr_mask & IB_QP_AV) {
  940. if (mlx4_set_path(dev, &attr->ah_attr, &context->pri_path,
  941. attr_mask & IB_QP_PORT ? attr->port_num : qp->port))
  942. goto out;
  943. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  944. MLX4_QP_OPTPAR_SCHED_QUEUE);
  945. }
  946. if (attr_mask & IB_QP_TIMEOUT) {
  947. context->pri_path.ackto |= attr->timeout << 3;
  948. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  949. }
  950. if (attr_mask & IB_QP_ALT_PATH) {
  951. if (attr->alt_port_num == 0 ||
  952. attr->alt_port_num > dev->dev->caps.num_ports)
  953. goto out;
  954. if (attr->alt_pkey_index >=
  955. dev->dev->caps.pkey_table_len[attr->alt_port_num])
  956. goto out;
  957. if (mlx4_set_path(dev, &attr->alt_ah_attr, &context->alt_path,
  958. attr->alt_port_num))
  959. goto out;
  960. context->alt_path.pkey_index = attr->alt_pkey_index;
  961. context->alt_path.ackto = attr->alt_timeout << 3;
  962. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  963. }
  964. pd = get_pd(qp);
  965. get_cqs(qp, &send_cq, &recv_cq);
  966. context->pd = cpu_to_be32(pd->pdn);
  967. context->cqn_send = cpu_to_be32(send_cq->mcq.cqn);
  968. context->cqn_recv = cpu_to_be32(recv_cq->mcq.cqn);
  969. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  970. /* Set "fast registration enabled" for all kernel QPs */
  971. if (!qp->ibqp.uobject)
  972. context->params1 |= cpu_to_be32(1 << 11);
  973. if (attr_mask & IB_QP_RNR_RETRY) {
  974. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  975. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  976. }
  977. if (attr_mask & IB_QP_RETRY_CNT) {
  978. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  979. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  980. }
  981. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  982. if (attr->max_rd_atomic)
  983. context->params1 |=
  984. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  985. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  986. }
  987. if (attr_mask & IB_QP_SQ_PSN)
  988. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  989. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  990. if (attr->max_dest_rd_atomic)
  991. context->params2 |=
  992. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  993. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  994. }
  995. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  996. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  997. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  998. }
  999. if (ibqp->srq)
  1000. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  1001. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  1002. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  1003. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  1004. }
  1005. if (attr_mask & IB_QP_RQ_PSN)
  1006. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  1007. if (attr_mask & IB_QP_QKEY) {
  1008. context->qkey = cpu_to_be32(attr->qkey);
  1009. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  1010. }
  1011. if (ibqp->srq)
  1012. context->srqn = cpu_to_be32(1 << 24 | to_msrq(ibqp->srq)->msrq.srqn);
  1013. if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1014. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  1015. if (cur_state == IB_QPS_INIT &&
  1016. new_state == IB_QPS_RTR &&
  1017. (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  1018. ibqp->qp_type == IB_QPT_UD)) {
  1019. context->pri_path.sched_queue = (qp->port - 1) << 6;
  1020. if (is_qp0(dev, qp))
  1021. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  1022. else
  1023. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  1024. }
  1025. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  1026. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  1027. sqd_event = 1;
  1028. else
  1029. sqd_event = 0;
  1030. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1031. context->rlkey |= (1 << 4);
  1032. /*
  1033. * Before passing a kernel QP to the HW, make sure that the
  1034. * ownership bits of the send queue are set and the SQ
  1035. * headroom is stamped so that the hardware doesn't start
  1036. * processing stale work requests.
  1037. */
  1038. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  1039. struct mlx4_wqe_ctrl_seg *ctrl;
  1040. int i;
  1041. for (i = 0; i < qp->sq.wqe_cnt; ++i) {
  1042. ctrl = get_send_wqe(qp, i);
  1043. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  1044. if (qp->sq_max_wqes_per_wr == 1)
  1045. ctrl->fence_size = 1 << (qp->sq.wqe_shift - 4);
  1046. stamp_send_wqe(qp, i, 1 << qp->sq.wqe_shift);
  1047. }
  1048. }
  1049. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  1050. to_mlx4_state(new_state), context, optpar,
  1051. sqd_event, &qp->mqp);
  1052. if (err)
  1053. goto out;
  1054. qp->state = new_state;
  1055. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1056. qp->atomic_rd_en = attr->qp_access_flags;
  1057. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1058. qp->resp_depth = attr->max_dest_rd_atomic;
  1059. if (attr_mask & IB_QP_PORT) {
  1060. qp->port = attr->port_num;
  1061. update_mcg_macs(dev, qp);
  1062. }
  1063. if (attr_mask & IB_QP_ALT_PATH)
  1064. qp->alt_port = attr->alt_port_num;
  1065. if (is_sqp(dev, qp))
  1066. store_sqp_attrs(to_msqp(qp), attr, attr_mask);
  1067. /*
  1068. * If we moved QP0 to RTR, bring the IB link up; if we moved
  1069. * QP0 to RESET or ERROR, bring the link back down.
  1070. */
  1071. if (is_qp0(dev, qp)) {
  1072. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  1073. if (mlx4_INIT_PORT(dev->dev, qp->port))
  1074. printk(KERN_WARNING "INIT_PORT failed for port %d\n",
  1075. qp->port);
  1076. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  1077. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  1078. mlx4_CLOSE_PORT(dev->dev, qp->port);
  1079. }
  1080. /*
  1081. * If we moved a kernel QP to RESET, clean up all old CQ
  1082. * entries and reinitialize the QP.
  1083. */
  1084. if (new_state == IB_QPS_RESET && !ibqp->uobject) {
  1085. mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  1086. ibqp->srq ? to_msrq(ibqp->srq): NULL);
  1087. if (send_cq != recv_cq)
  1088. mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  1089. qp->rq.head = 0;
  1090. qp->rq.tail = 0;
  1091. qp->sq.head = 0;
  1092. qp->sq.tail = 0;
  1093. qp->sq_next_wqe = 0;
  1094. if (qp->rq.wqe_cnt)
  1095. *qp->db.db = 0;
  1096. }
  1097. out:
  1098. kfree(context);
  1099. return err;
  1100. }
  1101. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1102. int attr_mask, struct ib_udata *udata)
  1103. {
  1104. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1105. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1106. enum ib_qp_state cur_state, new_state;
  1107. int err = -EINVAL;
  1108. mutex_lock(&qp->mutex);
  1109. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  1110. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  1111. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask))
  1112. goto out;
  1113. if ((attr_mask & IB_QP_PORT) &&
  1114. (attr->port_num == 0 || attr->port_num > dev->dev->caps.num_ports)) {
  1115. goto out;
  1116. }
  1117. if (attr_mask & IB_QP_PKEY_INDEX) {
  1118. int p = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1119. if (attr->pkey_index >= dev->dev->caps.pkey_table_len[p])
  1120. goto out;
  1121. }
  1122. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  1123. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  1124. goto out;
  1125. }
  1126. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  1127. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  1128. goto out;
  1129. }
  1130. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  1131. err = 0;
  1132. goto out;
  1133. }
  1134. err = __mlx4_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
  1135. out:
  1136. mutex_unlock(&qp->mutex);
  1137. return err;
  1138. }
  1139. static int build_mlx_header(struct mlx4_ib_sqp *sqp, struct ib_send_wr *wr,
  1140. void *wqe, unsigned *mlx_seg_len)
  1141. {
  1142. struct ib_device *ib_dev = sqp->qp.ibqp.device;
  1143. struct mlx4_wqe_mlx_seg *mlx = wqe;
  1144. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  1145. struct mlx4_ib_ah *ah = to_mah(wr->wr.ud.ah);
  1146. union ib_gid sgid;
  1147. u16 pkey;
  1148. int send_size;
  1149. int header_size;
  1150. int spc;
  1151. int i;
  1152. int is_eth;
  1153. int is_vlan = 0;
  1154. int is_grh;
  1155. u16 vlan;
  1156. send_size = 0;
  1157. for (i = 0; i < wr->num_sge; ++i)
  1158. send_size += wr->sg_list[i].length;
  1159. is_eth = rdma_port_get_link_layer(sqp->qp.ibqp.device, sqp->qp.port) == IB_LINK_LAYER_ETHERNET;
  1160. is_grh = mlx4_ib_ah_grh_present(ah);
  1161. if (is_eth) {
  1162. ib_get_cached_gid(ib_dev, be32_to_cpu(ah->av.ib.port_pd) >> 24,
  1163. ah->av.ib.gid_index, &sgid);
  1164. vlan = rdma_get_vlan_id(&sgid);
  1165. is_vlan = vlan < 0x1000;
  1166. }
  1167. ib_ud_header_init(send_size, !is_eth, is_eth, is_vlan, is_grh, 0, &sqp->ud_header);
  1168. if (!is_eth) {
  1169. sqp->ud_header.lrh.service_level =
  1170. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  1171. sqp->ud_header.lrh.destination_lid = ah->av.ib.dlid;
  1172. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  1173. }
  1174. if (is_grh) {
  1175. sqp->ud_header.grh.traffic_class =
  1176. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  1177. sqp->ud_header.grh.flow_label =
  1178. ah->av.ib.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  1179. sqp->ud_header.grh.hop_limit = ah->av.ib.hop_limit;
  1180. ib_get_cached_gid(ib_dev, be32_to_cpu(ah->av.ib.port_pd) >> 24,
  1181. ah->av.ib.gid_index, &sqp->ud_header.grh.source_gid);
  1182. memcpy(sqp->ud_header.grh.destination_gid.raw,
  1183. ah->av.ib.dgid, 16);
  1184. }
  1185. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  1186. if (!is_eth) {
  1187. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  1188. (sqp->ud_header.lrh.destination_lid ==
  1189. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  1190. (sqp->ud_header.lrh.service_level << 8));
  1191. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  1192. }
  1193. switch (wr->opcode) {
  1194. case IB_WR_SEND:
  1195. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  1196. sqp->ud_header.immediate_present = 0;
  1197. break;
  1198. case IB_WR_SEND_WITH_IMM:
  1199. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  1200. sqp->ud_header.immediate_present = 1;
  1201. sqp->ud_header.immediate_data = wr->ex.imm_data;
  1202. break;
  1203. default:
  1204. return -EINVAL;
  1205. }
  1206. if (is_eth) {
  1207. u8 *smac;
  1208. memcpy(sqp->ud_header.eth.dmac_h, ah->av.eth.mac, 6);
  1209. /* FIXME: cache smac value? */
  1210. smac = to_mdev(sqp->qp.ibqp.device)->iboe.netdevs[sqp->qp.port - 1]->dev_addr;
  1211. memcpy(sqp->ud_header.eth.smac_h, smac, 6);
  1212. if (!memcmp(sqp->ud_header.eth.smac_h, sqp->ud_header.eth.dmac_h, 6))
  1213. mlx->flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
  1214. if (!is_vlan) {
  1215. sqp->ud_header.eth.type = cpu_to_be16(MLX4_IB_IBOE_ETHERTYPE);
  1216. } else {
  1217. u16 pcp;
  1218. sqp->ud_header.vlan.type = cpu_to_be16(MLX4_IB_IBOE_ETHERTYPE);
  1219. pcp = (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 29) << 13;
  1220. sqp->ud_header.vlan.tag = cpu_to_be16(vlan | pcp);
  1221. }
  1222. } else {
  1223. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
  1224. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  1225. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  1226. }
  1227. sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
  1228. if (!sqp->qp.ibqp.qp_num)
  1229. ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
  1230. else
  1231. ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->wr.ud.pkey_index, &pkey);
  1232. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  1233. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  1234. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  1235. sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
  1236. sqp->qkey : wr->wr.ud.remote_qkey);
  1237. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  1238. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  1239. if (0) {
  1240. printk(KERN_ERR "built UD header of size %d:\n", header_size);
  1241. for (i = 0; i < header_size / 4; ++i) {
  1242. if (i % 8 == 0)
  1243. printk(" [%02x] ", i * 4);
  1244. printk(" %08x",
  1245. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  1246. if ((i + 1) % 8 == 0)
  1247. printk("\n");
  1248. }
  1249. printk("\n");
  1250. }
  1251. /*
  1252. * Inline data segments may not cross a 64 byte boundary. If
  1253. * our UD header is bigger than the space available up to the
  1254. * next 64 byte boundary in the WQE, use two inline data
  1255. * segments to hold the UD header.
  1256. */
  1257. spc = MLX4_INLINE_ALIGN -
  1258. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  1259. if (header_size <= spc) {
  1260. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  1261. memcpy(inl + 1, sqp->header_buf, header_size);
  1262. i = 1;
  1263. } else {
  1264. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  1265. memcpy(inl + 1, sqp->header_buf, spc);
  1266. inl = (void *) (inl + 1) + spc;
  1267. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  1268. /*
  1269. * Need a barrier here to make sure all the data is
  1270. * visible before the byte_count field is set.
  1271. * Otherwise the HCA prefetcher could grab the 64-byte
  1272. * chunk with this inline segment and get a valid (!=
  1273. * 0xffffffff) byte count but stale data, and end up
  1274. * generating a packet with bad headers.
  1275. *
  1276. * The first inline segment's byte_count field doesn't
  1277. * need a barrier, because it comes after a
  1278. * control/MLX segment and therefore is at an offset
  1279. * of 16 mod 64.
  1280. */
  1281. wmb();
  1282. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  1283. i = 2;
  1284. }
  1285. *mlx_seg_len =
  1286. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  1287. return 0;
  1288. }
  1289. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  1290. {
  1291. unsigned cur;
  1292. struct mlx4_ib_cq *cq;
  1293. cur = wq->head - wq->tail;
  1294. if (likely(cur + nreq < wq->max_post))
  1295. return 0;
  1296. cq = to_mcq(ib_cq);
  1297. spin_lock(&cq->lock);
  1298. cur = wq->head - wq->tail;
  1299. spin_unlock(&cq->lock);
  1300. return cur + nreq >= wq->max_post;
  1301. }
  1302. static __be32 convert_access(int acc)
  1303. {
  1304. return (acc & IB_ACCESS_REMOTE_ATOMIC ? cpu_to_be32(MLX4_WQE_FMR_PERM_ATOMIC) : 0) |
  1305. (acc & IB_ACCESS_REMOTE_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_REMOTE_WRITE) : 0) |
  1306. (acc & IB_ACCESS_REMOTE_READ ? cpu_to_be32(MLX4_WQE_FMR_PERM_REMOTE_READ) : 0) |
  1307. (acc & IB_ACCESS_LOCAL_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_WRITE) : 0) |
  1308. cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_READ);
  1309. }
  1310. static void set_fmr_seg(struct mlx4_wqe_fmr_seg *fseg, struct ib_send_wr *wr)
  1311. {
  1312. struct mlx4_ib_fast_reg_page_list *mfrpl = to_mfrpl(wr->wr.fast_reg.page_list);
  1313. int i;
  1314. for (i = 0; i < wr->wr.fast_reg.page_list_len; ++i)
  1315. mfrpl->mapped_page_list[i] =
  1316. cpu_to_be64(wr->wr.fast_reg.page_list->page_list[i] |
  1317. MLX4_MTT_FLAG_PRESENT);
  1318. fseg->flags = convert_access(wr->wr.fast_reg.access_flags);
  1319. fseg->mem_key = cpu_to_be32(wr->wr.fast_reg.rkey);
  1320. fseg->buf_list = cpu_to_be64(mfrpl->map);
  1321. fseg->start_addr = cpu_to_be64(wr->wr.fast_reg.iova_start);
  1322. fseg->reg_len = cpu_to_be64(wr->wr.fast_reg.length);
  1323. fseg->offset = 0; /* XXX -- is this just for ZBVA? */
  1324. fseg->page_size = cpu_to_be32(wr->wr.fast_reg.page_shift);
  1325. fseg->reserved[0] = 0;
  1326. fseg->reserved[1] = 0;
  1327. }
  1328. static void set_local_inv_seg(struct mlx4_wqe_local_inval_seg *iseg, u32 rkey)
  1329. {
  1330. iseg->flags = 0;
  1331. iseg->mem_key = cpu_to_be32(rkey);
  1332. iseg->guest_id = 0;
  1333. iseg->pa = 0;
  1334. }
  1335. static __always_inline void set_raddr_seg(struct mlx4_wqe_raddr_seg *rseg,
  1336. u64 remote_addr, u32 rkey)
  1337. {
  1338. rseg->raddr = cpu_to_be64(remote_addr);
  1339. rseg->rkey = cpu_to_be32(rkey);
  1340. rseg->reserved = 0;
  1341. }
  1342. static void set_atomic_seg(struct mlx4_wqe_atomic_seg *aseg, struct ib_send_wr *wr)
  1343. {
  1344. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  1345. aseg->swap_add = cpu_to_be64(wr->wr.atomic.swap);
  1346. aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add);
  1347. } else if (wr->opcode == IB_WR_MASKED_ATOMIC_FETCH_AND_ADD) {
  1348. aseg->swap_add = cpu_to_be64(wr->wr.atomic.compare_add);
  1349. aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add_mask);
  1350. } else {
  1351. aseg->swap_add = cpu_to_be64(wr->wr.atomic.compare_add);
  1352. aseg->compare = 0;
  1353. }
  1354. }
  1355. static void set_masked_atomic_seg(struct mlx4_wqe_masked_atomic_seg *aseg,
  1356. struct ib_send_wr *wr)
  1357. {
  1358. aseg->swap_add = cpu_to_be64(wr->wr.atomic.swap);
  1359. aseg->swap_add_mask = cpu_to_be64(wr->wr.atomic.swap_mask);
  1360. aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add);
  1361. aseg->compare_mask = cpu_to_be64(wr->wr.atomic.compare_add_mask);
  1362. }
  1363. static void set_datagram_seg(struct mlx4_wqe_datagram_seg *dseg,
  1364. struct ib_send_wr *wr)
  1365. {
  1366. memcpy(dseg->av, &to_mah(wr->wr.ud.ah)->av, sizeof (struct mlx4_av));
  1367. dseg->dqpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  1368. dseg->qkey = cpu_to_be32(wr->wr.ud.remote_qkey);
  1369. dseg->vlan = to_mah(wr->wr.ud.ah)->av.eth.vlan;
  1370. memcpy(dseg->mac, to_mah(wr->wr.ud.ah)->av.eth.mac, 6);
  1371. }
  1372. static void set_mlx_icrc_seg(void *dseg)
  1373. {
  1374. u32 *t = dseg;
  1375. struct mlx4_wqe_inline_seg *iseg = dseg;
  1376. t[1] = 0;
  1377. /*
  1378. * Need a barrier here before writing the byte_count field to
  1379. * make sure that all the data is visible before the
  1380. * byte_count field is set. Otherwise, if the segment begins
  1381. * a new cacheline, the HCA prefetcher could grab the 64-byte
  1382. * chunk and get a valid (!= * 0xffffffff) byte count but
  1383. * stale data, and end up sending the wrong data.
  1384. */
  1385. wmb();
  1386. iseg->byte_count = cpu_to_be32((1 << 31) | 4);
  1387. }
  1388. static void set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  1389. {
  1390. dseg->lkey = cpu_to_be32(sg->lkey);
  1391. dseg->addr = cpu_to_be64(sg->addr);
  1392. /*
  1393. * Need a barrier here before writing the byte_count field to
  1394. * make sure that all the data is visible before the
  1395. * byte_count field is set. Otherwise, if the segment begins
  1396. * a new cacheline, the HCA prefetcher could grab the 64-byte
  1397. * chunk and get a valid (!= * 0xffffffff) byte count but
  1398. * stale data, and end up sending the wrong data.
  1399. */
  1400. wmb();
  1401. dseg->byte_count = cpu_to_be32(sg->length);
  1402. }
  1403. static void __set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  1404. {
  1405. dseg->byte_count = cpu_to_be32(sg->length);
  1406. dseg->lkey = cpu_to_be32(sg->lkey);
  1407. dseg->addr = cpu_to_be64(sg->addr);
  1408. }
  1409. static int build_lso_seg(struct mlx4_wqe_lso_seg *wqe, struct ib_send_wr *wr,
  1410. struct mlx4_ib_qp *qp, unsigned *lso_seg_len,
  1411. __be32 *lso_hdr_sz, __be32 *blh)
  1412. {
  1413. unsigned halign = ALIGN(sizeof *wqe + wr->wr.ud.hlen, 16);
  1414. if (unlikely(halign > MLX4_IB_CACHE_LINE_SIZE))
  1415. *blh = cpu_to_be32(1 << 6);
  1416. if (unlikely(!(qp->flags & MLX4_IB_QP_LSO) &&
  1417. wr->num_sge > qp->sq.max_gs - (halign >> 4)))
  1418. return -EINVAL;
  1419. memcpy(wqe->header, wr->wr.ud.header, wr->wr.ud.hlen);
  1420. *lso_hdr_sz = cpu_to_be32(wr->wr.ud.mss << 16 | wr->wr.ud.hlen);
  1421. *lso_seg_len = halign;
  1422. return 0;
  1423. }
  1424. static __be32 send_ieth(struct ib_send_wr *wr)
  1425. {
  1426. switch (wr->opcode) {
  1427. case IB_WR_SEND_WITH_IMM:
  1428. case IB_WR_RDMA_WRITE_WITH_IMM:
  1429. return wr->ex.imm_data;
  1430. case IB_WR_SEND_WITH_INV:
  1431. return cpu_to_be32(wr->ex.invalidate_rkey);
  1432. default:
  1433. return 0;
  1434. }
  1435. }
  1436. int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1437. struct ib_send_wr **bad_wr)
  1438. {
  1439. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1440. void *wqe;
  1441. struct mlx4_wqe_ctrl_seg *ctrl;
  1442. struct mlx4_wqe_data_seg *dseg;
  1443. unsigned long flags;
  1444. int nreq;
  1445. int err = 0;
  1446. unsigned ind;
  1447. int uninitialized_var(stamp);
  1448. int uninitialized_var(size);
  1449. unsigned uninitialized_var(seglen);
  1450. __be32 dummy;
  1451. __be32 *lso_wqe;
  1452. __be32 uninitialized_var(lso_hdr_sz);
  1453. __be32 blh;
  1454. int i;
  1455. spin_lock_irqsave(&qp->sq.lock, flags);
  1456. ind = qp->sq_next_wqe;
  1457. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1458. lso_wqe = &dummy;
  1459. blh = 0;
  1460. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  1461. err = -ENOMEM;
  1462. *bad_wr = wr;
  1463. goto out;
  1464. }
  1465. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  1466. err = -EINVAL;
  1467. *bad_wr = wr;
  1468. goto out;
  1469. }
  1470. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  1471. qp->sq.wrid[(qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
  1472. ctrl->srcrb_flags =
  1473. (wr->send_flags & IB_SEND_SIGNALED ?
  1474. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  1475. (wr->send_flags & IB_SEND_SOLICITED ?
  1476. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  1477. ((wr->send_flags & IB_SEND_IP_CSUM) ?
  1478. cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  1479. MLX4_WQE_CTRL_TCP_UDP_CSUM) : 0) |
  1480. qp->sq_signal_bits;
  1481. ctrl->imm = send_ieth(wr);
  1482. wqe += sizeof *ctrl;
  1483. size = sizeof *ctrl / 16;
  1484. switch (ibqp->qp_type) {
  1485. case IB_QPT_RC:
  1486. case IB_QPT_UC:
  1487. switch (wr->opcode) {
  1488. case IB_WR_ATOMIC_CMP_AND_SWP:
  1489. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1490. case IB_WR_MASKED_ATOMIC_FETCH_AND_ADD:
  1491. set_raddr_seg(wqe, wr->wr.atomic.remote_addr,
  1492. wr->wr.atomic.rkey);
  1493. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1494. set_atomic_seg(wqe, wr);
  1495. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  1496. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  1497. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  1498. break;
  1499. case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
  1500. set_raddr_seg(wqe, wr->wr.atomic.remote_addr,
  1501. wr->wr.atomic.rkey);
  1502. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1503. set_masked_atomic_seg(wqe, wr);
  1504. wqe += sizeof (struct mlx4_wqe_masked_atomic_seg);
  1505. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  1506. sizeof (struct mlx4_wqe_masked_atomic_seg)) / 16;
  1507. break;
  1508. case IB_WR_RDMA_READ:
  1509. case IB_WR_RDMA_WRITE:
  1510. case IB_WR_RDMA_WRITE_WITH_IMM:
  1511. set_raddr_seg(wqe, wr->wr.rdma.remote_addr,
  1512. wr->wr.rdma.rkey);
  1513. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1514. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  1515. break;
  1516. case IB_WR_LOCAL_INV:
  1517. ctrl->srcrb_flags |=
  1518. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  1519. set_local_inv_seg(wqe, wr->ex.invalidate_rkey);
  1520. wqe += sizeof (struct mlx4_wqe_local_inval_seg);
  1521. size += sizeof (struct mlx4_wqe_local_inval_seg) / 16;
  1522. break;
  1523. case IB_WR_FAST_REG_MR:
  1524. ctrl->srcrb_flags |=
  1525. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  1526. set_fmr_seg(wqe, wr);
  1527. wqe += sizeof (struct mlx4_wqe_fmr_seg);
  1528. size += sizeof (struct mlx4_wqe_fmr_seg) / 16;
  1529. break;
  1530. default:
  1531. /* No extra segments required for sends */
  1532. break;
  1533. }
  1534. break;
  1535. case IB_QPT_UD:
  1536. set_datagram_seg(wqe, wr);
  1537. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  1538. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  1539. if (wr->opcode == IB_WR_LSO) {
  1540. err = build_lso_seg(wqe, wr, qp, &seglen, &lso_hdr_sz, &blh);
  1541. if (unlikely(err)) {
  1542. *bad_wr = wr;
  1543. goto out;
  1544. }
  1545. lso_wqe = (__be32 *) wqe;
  1546. wqe += seglen;
  1547. size += seglen / 16;
  1548. }
  1549. break;
  1550. case IB_QPT_SMI:
  1551. case IB_QPT_GSI:
  1552. err = build_mlx_header(to_msqp(qp), wr, ctrl, &seglen);
  1553. if (unlikely(err)) {
  1554. *bad_wr = wr;
  1555. goto out;
  1556. }
  1557. wqe += seglen;
  1558. size += seglen / 16;
  1559. break;
  1560. default:
  1561. break;
  1562. }
  1563. /*
  1564. * Write data segments in reverse order, so as to
  1565. * overwrite cacheline stamp last within each
  1566. * cacheline. This avoids issues with WQE
  1567. * prefetching.
  1568. */
  1569. dseg = wqe;
  1570. dseg += wr->num_sge - 1;
  1571. size += wr->num_sge * (sizeof (struct mlx4_wqe_data_seg) / 16);
  1572. /* Add one more inline data segment for ICRC for MLX sends */
  1573. if (unlikely(qp->ibqp.qp_type == IB_QPT_SMI ||
  1574. qp->ibqp.qp_type == IB_QPT_GSI)) {
  1575. set_mlx_icrc_seg(dseg + 1);
  1576. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1577. }
  1578. for (i = wr->num_sge - 1; i >= 0; --i, --dseg)
  1579. set_data_seg(dseg, wr->sg_list + i);
  1580. /*
  1581. * Possibly overwrite stamping in cacheline with LSO
  1582. * segment only after making sure all data segments
  1583. * are written.
  1584. */
  1585. wmb();
  1586. *lso_wqe = lso_hdr_sz;
  1587. ctrl->fence_size = (wr->send_flags & IB_SEND_FENCE ?
  1588. MLX4_WQE_CTRL_FENCE : 0) | size;
  1589. /*
  1590. * Make sure descriptor is fully written before
  1591. * setting ownership bit (because HW can start
  1592. * executing as soon as we do).
  1593. */
  1594. wmb();
  1595. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  1596. *bad_wr = wr;
  1597. err = -EINVAL;
  1598. goto out;
  1599. }
  1600. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  1601. (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0) | blh;
  1602. stamp = ind + qp->sq_spare_wqes;
  1603. ind += DIV_ROUND_UP(size * 16, 1U << qp->sq.wqe_shift);
  1604. /*
  1605. * We can improve latency by not stamping the last
  1606. * send queue WQE until after ringing the doorbell, so
  1607. * only stamp here if there are still more WQEs to post.
  1608. *
  1609. * Same optimization applies to padding with NOP wqe
  1610. * in case of WQE shrinking (used to prevent wrap-around
  1611. * in the middle of WR).
  1612. */
  1613. if (wr->next) {
  1614. stamp_send_wqe(qp, stamp, size * 16);
  1615. ind = pad_wraparound(qp, ind);
  1616. }
  1617. }
  1618. out:
  1619. if (likely(nreq)) {
  1620. qp->sq.head += nreq;
  1621. /*
  1622. * Make sure that descriptors are written before
  1623. * doorbell record.
  1624. */
  1625. wmb();
  1626. writel(qp->doorbell_qpn,
  1627. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  1628. /*
  1629. * Make sure doorbells don't leak out of SQ spinlock
  1630. * and reach the HCA out of order.
  1631. */
  1632. mmiowb();
  1633. stamp_send_wqe(qp, stamp, size * 16);
  1634. ind = pad_wraparound(qp, ind);
  1635. qp->sq_next_wqe = ind;
  1636. }
  1637. spin_unlock_irqrestore(&qp->sq.lock, flags);
  1638. return err;
  1639. }
  1640. int mlx4_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1641. struct ib_recv_wr **bad_wr)
  1642. {
  1643. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1644. struct mlx4_wqe_data_seg *scat;
  1645. unsigned long flags;
  1646. int err = 0;
  1647. int nreq;
  1648. int ind;
  1649. int i;
  1650. spin_lock_irqsave(&qp->rq.lock, flags);
  1651. ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
  1652. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1653. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
  1654. err = -ENOMEM;
  1655. *bad_wr = wr;
  1656. goto out;
  1657. }
  1658. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  1659. err = -EINVAL;
  1660. *bad_wr = wr;
  1661. goto out;
  1662. }
  1663. scat = get_recv_wqe(qp, ind);
  1664. for (i = 0; i < wr->num_sge; ++i)
  1665. __set_data_seg(scat + i, wr->sg_list + i);
  1666. if (i < qp->rq.max_gs) {
  1667. scat[i].byte_count = 0;
  1668. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  1669. scat[i].addr = 0;
  1670. }
  1671. qp->rq.wrid[ind] = wr->wr_id;
  1672. ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
  1673. }
  1674. out:
  1675. if (likely(nreq)) {
  1676. qp->rq.head += nreq;
  1677. /*
  1678. * Make sure that descriptors are written before
  1679. * doorbell record.
  1680. */
  1681. wmb();
  1682. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  1683. }
  1684. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1685. return err;
  1686. }
  1687. static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state)
  1688. {
  1689. switch (mlx4_state) {
  1690. case MLX4_QP_STATE_RST: return IB_QPS_RESET;
  1691. case MLX4_QP_STATE_INIT: return IB_QPS_INIT;
  1692. case MLX4_QP_STATE_RTR: return IB_QPS_RTR;
  1693. case MLX4_QP_STATE_RTS: return IB_QPS_RTS;
  1694. case MLX4_QP_STATE_SQ_DRAINING:
  1695. case MLX4_QP_STATE_SQD: return IB_QPS_SQD;
  1696. case MLX4_QP_STATE_SQER: return IB_QPS_SQE;
  1697. case MLX4_QP_STATE_ERR: return IB_QPS_ERR;
  1698. default: return -1;
  1699. }
  1700. }
  1701. static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state)
  1702. {
  1703. switch (mlx4_mig_state) {
  1704. case MLX4_QP_PM_ARMED: return IB_MIG_ARMED;
  1705. case MLX4_QP_PM_REARM: return IB_MIG_REARM;
  1706. case MLX4_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
  1707. default: return -1;
  1708. }
  1709. }
  1710. static int to_ib_qp_access_flags(int mlx4_flags)
  1711. {
  1712. int ib_flags = 0;
  1713. if (mlx4_flags & MLX4_QP_BIT_RRE)
  1714. ib_flags |= IB_ACCESS_REMOTE_READ;
  1715. if (mlx4_flags & MLX4_QP_BIT_RWE)
  1716. ib_flags |= IB_ACCESS_REMOTE_WRITE;
  1717. if (mlx4_flags & MLX4_QP_BIT_RAE)
  1718. ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
  1719. return ib_flags;
  1720. }
  1721. static void to_ib_ah_attr(struct mlx4_ib_dev *ibdev, struct ib_ah_attr *ib_ah_attr,
  1722. struct mlx4_qp_path *path)
  1723. {
  1724. struct mlx4_dev *dev = ibdev->dev;
  1725. int is_eth;
  1726. memset(ib_ah_attr, 0, sizeof *ib_ah_attr);
  1727. ib_ah_attr->port_num = path->sched_queue & 0x40 ? 2 : 1;
  1728. if (ib_ah_attr->port_num == 0 || ib_ah_attr->port_num > dev->caps.num_ports)
  1729. return;
  1730. is_eth = rdma_port_get_link_layer(&ibdev->ib_dev, ib_ah_attr->port_num) ==
  1731. IB_LINK_LAYER_ETHERNET;
  1732. if (is_eth)
  1733. ib_ah_attr->sl = ((path->sched_queue >> 3) & 0x7) |
  1734. ((path->sched_queue & 4) << 1);
  1735. else
  1736. ib_ah_attr->sl = (path->sched_queue >> 2) & 0xf;
  1737. ib_ah_attr->dlid = be16_to_cpu(path->rlid);
  1738. ib_ah_attr->src_path_bits = path->grh_mylmc & 0x7f;
  1739. ib_ah_attr->static_rate = path->static_rate ? path->static_rate - 5 : 0;
  1740. ib_ah_attr->ah_flags = (path->grh_mylmc & (1 << 7)) ? IB_AH_GRH : 0;
  1741. if (ib_ah_attr->ah_flags) {
  1742. ib_ah_attr->grh.sgid_index = path->mgid_index;
  1743. ib_ah_attr->grh.hop_limit = path->hop_limit;
  1744. ib_ah_attr->grh.traffic_class =
  1745. (be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
  1746. ib_ah_attr->grh.flow_label =
  1747. be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
  1748. memcpy(ib_ah_attr->grh.dgid.raw,
  1749. path->rgid, sizeof ib_ah_attr->grh.dgid.raw);
  1750. }
  1751. }
  1752. int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
  1753. struct ib_qp_init_attr *qp_init_attr)
  1754. {
  1755. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1756. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1757. struct mlx4_qp_context context;
  1758. int mlx4_state;
  1759. int err = 0;
  1760. mutex_lock(&qp->mutex);
  1761. if (qp->state == IB_QPS_RESET) {
  1762. qp_attr->qp_state = IB_QPS_RESET;
  1763. goto done;
  1764. }
  1765. err = mlx4_qp_query(dev->dev, &qp->mqp, &context);
  1766. if (err) {
  1767. err = -EINVAL;
  1768. goto out;
  1769. }
  1770. mlx4_state = be32_to_cpu(context.flags) >> 28;
  1771. qp->state = to_ib_qp_state(mlx4_state);
  1772. qp_attr->qp_state = qp->state;
  1773. qp_attr->path_mtu = context.mtu_msgmax >> 5;
  1774. qp_attr->path_mig_state =
  1775. to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3);
  1776. qp_attr->qkey = be32_to_cpu(context.qkey);
  1777. qp_attr->rq_psn = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff;
  1778. qp_attr->sq_psn = be32_to_cpu(context.next_send_psn) & 0xffffff;
  1779. qp_attr->dest_qp_num = be32_to_cpu(context.remote_qpn) & 0xffffff;
  1780. qp_attr->qp_access_flags =
  1781. to_ib_qp_access_flags(be32_to_cpu(context.params2));
  1782. if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
  1783. to_ib_ah_attr(dev, &qp_attr->ah_attr, &context.pri_path);
  1784. to_ib_ah_attr(dev, &qp_attr->alt_ah_attr, &context.alt_path);
  1785. qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f;
  1786. qp_attr->alt_port_num = qp_attr->alt_ah_attr.port_num;
  1787. }
  1788. qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f;
  1789. if (qp_attr->qp_state == IB_QPS_INIT)
  1790. qp_attr->port_num = qp->port;
  1791. else
  1792. qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1;
  1793. /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
  1794. qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING;
  1795. qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7);
  1796. qp_attr->max_dest_rd_atomic =
  1797. 1 << ((be32_to_cpu(context.params2) >> 21) & 0x7);
  1798. qp_attr->min_rnr_timer =
  1799. (be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f;
  1800. qp_attr->timeout = context.pri_path.ackto >> 3;
  1801. qp_attr->retry_cnt = (be32_to_cpu(context.params1) >> 16) & 0x7;
  1802. qp_attr->rnr_retry = (be32_to_cpu(context.params1) >> 13) & 0x7;
  1803. qp_attr->alt_timeout = context.alt_path.ackto >> 3;
  1804. done:
  1805. qp_attr->cur_qp_state = qp_attr->qp_state;
  1806. qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
  1807. qp_attr->cap.max_recv_sge = qp->rq.max_gs;
  1808. if (!ibqp->uobject) {
  1809. qp_attr->cap.max_send_wr = qp->sq.wqe_cnt;
  1810. qp_attr->cap.max_send_sge = qp->sq.max_gs;
  1811. } else {
  1812. qp_attr->cap.max_send_wr = 0;
  1813. qp_attr->cap.max_send_sge = 0;
  1814. }
  1815. /*
  1816. * We don't support inline sends for kernel QPs (yet), and we
  1817. * don't know what userspace's value should be.
  1818. */
  1819. qp_attr->cap.max_inline_data = 0;
  1820. qp_init_attr->cap = qp_attr->cap;
  1821. qp_init_attr->create_flags = 0;
  1822. if (qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK)
  1823. qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
  1824. if (qp->flags & MLX4_IB_QP_LSO)
  1825. qp_init_attr->create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;
  1826. out:
  1827. mutex_unlock(&qp->mutex);
  1828. return err;
  1829. }