ehca_qp.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. /*
  2. * IBM eServer eHCA Infiniband device driver for Linux on POWER
  3. *
  4. * QP functions
  5. *
  6. * Authors: Waleri Fomin <fomin@de.ibm.com>
  7. * Hoang-Nam Nguyen <hnguyen@de.ibm.com>
  8. * Reinhard Ernst <rernst@de.ibm.com>
  9. * Heiko J Schick <schickhj@de.ibm.com>
  10. *
  11. * Copyright (c) 2005 IBM Corporation
  12. *
  13. * All rights reserved.
  14. *
  15. * This source code is distributed under a dual license of GPL v2.0 and OpenIB
  16. * BSD.
  17. *
  18. * OpenIB BSD License
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions are met:
  22. *
  23. * Redistributions of source code must retain the above copyright notice, this
  24. * list of conditions and the following disclaimer.
  25. *
  26. * Redistributions in binary form must reproduce the above copyright notice,
  27. * this list of conditions and the following disclaimer in the documentation
  28. * and/or other materials
  29. * provided with the distribution.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  32. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  35. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  36. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  37. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  38. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  39. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. #include <asm/current.h>
  44. #include "ehca_classes.h"
  45. #include "ehca_tools.h"
  46. #include "ehca_qes.h"
  47. #include "ehca_iverbs.h"
  48. #include "hcp_if.h"
  49. #include "hipz_fns.h"
  50. static struct kmem_cache *qp_cache;
  51. /*
  52. * attributes not supported by query qp
  53. */
  54. #define QP_ATTR_QUERY_NOT_SUPPORTED (IB_QP_MAX_DEST_RD_ATOMIC | \
  55. IB_QP_MAX_QP_RD_ATOMIC | \
  56. IB_QP_ACCESS_FLAGS | \
  57. IB_QP_EN_SQD_ASYNC_NOTIFY)
  58. /*
  59. * ehca (internal) qp state values
  60. */
  61. enum ehca_qp_state {
  62. EHCA_QPS_RESET = 1,
  63. EHCA_QPS_INIT = 2,
  64. EHCA_QPS_RTR = 3,
  65. EHCA_QPS_RTS = 5,
  66. EHCA_QPS_SQD = 6,
  67. EHCA_QPS_SQE = 8,
  68. EHCA_QPS_ERR = 128
  69. };
  70. /*
  71. * qp state transitions as defined by IB Arch Rel 1.1 page 431
  72. */
  73. enum ib_qp_statetrans {
  74. IB_QPST_ANY2RESET,
  75. IB_QPST_ANY2ERR,
  76. IB_QPST_RESET2INIT,
  77. IB_QPST_INIT2RTR,
  78. IB_QPST_INIT2INIT,
  79. IB_QPST_RTR2RTS,
  80. IB_QPST_RTS2SQD,
  81. IB_QPST_RTS2RTS,
  82. IB_QPST_SQD2RTS,
  83. IB_QPST_SQE2RTS,
  84. IB_QPST_SQD2SQD,
  85. IB_QPST_MAX /* nr of transitions, this must be last!!! */
  86. };
  87. /*
  88. * ib2ehca_qp_state maps IB to ehca qp_state
  89. * returns ehca qp state corresponding to given ib qp state
  90. */
  91. static inline enum ehca_qp_state ib2ehca_qp_state(enum ib_qp_state ib_qp_state)
  92. {
  93. switch (ib_qp_state) {
  94. case IB_QPS_RESET:
  95. return EHCA_QPS_RESET;
  96. case IB_QPS_INIT:
  97. return EHCA_QPS_INIT;
  98. case IB_QPS_RTR:
  99. return EHCA_QPS_RTR;
  100. case IB_QPS_RTS:
  101. return EHCA_QPS_RTS;
  102. case IB_QPS_SQD:
  103. return EHCA_QPS_SQD;
  104. case IB_QPS_SQE:
  105. return EHCA_QPS_SQE;
  106. case IB_QPS_ERR:
  107. return EHCA_QPS_ERR;
  108. default:
  109. ehca_gen_err("invalid ib_qp_state=%x", ib_qp_state);
  110. return -EINVAL;
  111. }
  112. }
  113. /*
  114. * ehca2ib_qp_state maps ehca to IB qp_state
  115. * returns ib qp state corresponding to given ehca qp state
  116. */
  117. static inline enum ib_qp_state ehca2ib_qp_state(enum ehca_qp_state
  118. ehca_qp_state)
  119. {
  120. switch (ehca_qp_state) {
  121. case EHCA_QPS_RESET:
  122. return IB_QPS_RESET;
  123. case EHCA_QPS_INIT:
  124. return IB_QPS_INIT;
  125. case EHCA_QPS_RTR:
  126. return IB_QPS_RTR;
  127. case EHCA_QPS_RTS:
  128. return IB_QPS_RTS;
  129. case EHCA_QPS_SQD:
  130. return IB_QPS_SQD;
  131. case EHCA_QPS_SQE:
  132. return IB_QPS_SQE;
  133. case EHCA_QPS_ERR:
  134. return IB_QPS_ERR;
  135. default:
  136. ehca_gen_err("invalid ehca_qp_state=%x", ehca_qp_state);
  137. return -EINVAL;
  138. }
  139. }
  140. /*
  141. * ehca_qp_type used as index for req_attr and opt_attr of
  142. * struct ehca_modqp_statetrans
  143. */
  144. enum ehca_qp_type {
  145. QPT_RC = 0,
  146. QPT_UC = 1,
  147. QPT_UD = 2,
  148. QPT_SQP = 3,
  149. QPT_MAX
  150. };
  151. /*
  152. * ib2ehcaqptype maps Ib to ehca qp_type
  153. * returns ehca qp type corresponding to ib qp type
  154. */
  155. static inline enum ehca_qp_type ib2ehcaqptype(enum ib_qp_type ibqptype)
  156. {
  157. switch (ibqptype) {
  158. case IB_QPT_SMI:
  159. case IB_QPT_GSI:
  160. return QPT_SQP;
  161. case IB_QPT_RC:
  162. return QPT_RC;
  163. case IB_QPT_UC:
  164. return QPT_UC;
  165. case IB_QPT_UD:
  166. return QPT_UD;
  167. default:
  168. ehca_gen_err("Invalid ibqptype=%x", ibqptype);
  169. return -EINVAL;
  170. }
  171. }
  172. static inline enum ib_qp_statetrans get_modqp_statetrans(int ib_fromstate,
  173. int ib_tostate)
  174. {
  175. int index = -EINVAL;
  176. switch (ib_tostate) {
  177. case IB_QPS_RESET:
  178. index = IB_QPST_ANY2RESET;
  179. break;
  180. case IB_QPS_INIT:
  181. switch (ib_fromstate) {
  182. case IB_QPS_RESET:
  183. index = IB_QPST_RESET2INIT;
  184. break;
  185. case IB_QPS_INIT:
  186. index = IB_QPST_INIT2INIT;
  187. break;
  188. }
  189. break;
  190. case IB_QPS_RTR:
  191. if (ib_fromstate == IB_QPS_INIT)
  192. index = IB_QPST_INIT2RTR;
  193. break;
  194. case IB_QPS_RTS:
  195. switch (ib_fromstate) {
  196. case IB_QPS_RTR:
  197. index = IB_QPST_RTR2RTS;
  198. break;
  199. case IB_QPS_RTS:
  200. index = IB_QPST_RTS2RTS;
  201. break;
  202. case IB_QPS_SQD:
  203. index = IB_QPST_SQD2RTS;
  204. break;
  205. case IB_QPS_SQE:
  206. index = IB_QPST_SQE2RTS;
  207. break;
  208. }
  209. break;
  210. case IB_QPS_SQD:
  211. if (ib_fromstate == IB_QPS_RTS)
  212. index = IB_QPST_RTS2SQD;
  213. break;
  214. case IB_QPS_SQE:
  215. break;
  216. case IB_QPS_ERR:
  217. index = IB_QPST_ANY2ERR;
  218. break;
  219. default:
  220. break;
  221. }
  222. return index;
  223. }
  224. /*
  225. * ibqptype2servicetype returns hcp service type corresponding to given
  226. * ib qp type used by create_qp()
  227. */
  228. static inline int ibqptype2servicetype(enum ib_qp_type ibqptype)
  229. {
  230. switch (ibqptype) {
  231. case IB_QPT_SMI:
  232. case IB_QPT_GSI:
  233. return ST_UD;
  234. case IB_QPT_RC:
  235. return ST_RC;
  236. case IB_QPT_UC:
  237. return ST_UC;
  238. case IB_QPT_UD:
  239. return ST_UD;
  240. case IB_QPT_RAW_IPV6:
  241. return -EINVAL;
  242. case IB_QPT_RAW_ETY:
  243. return -EINVAL;
  244. default:
  245. ehca_gen_err("Invalid ibqptype=%x", ibqptype);
  246. return -EINVAL;
  247. }
  248. }
  249. /*
  250. * init_qp_queue initializes/constructs r/squeue and registers queue pages.
  251. */
  252. static inline int init_qp_queue(struct ehca_shca *shca,
  253. struct ehca_qp *my_qp,
  254. struct ipz_queue *queue,
  255. int q_type,
  256. u64 expected_hret,
  257. int nr_q_pages,
  258. int wqe_size,
  259. int nr_sges)
  260. {
  261. int ret, cnt, ipz_rc;
  262. void *vpage;
  263. u64 rpage, h_ret;
  264. struct ib_device *ib_dev = &shca->ib_device;
  265. struct ipz_adapter_handle ipz_hca_handle = shca->ipz_hca_handle;
  266. if (!nr_q_pages)
  267. return 0;
  268. ipz_rc = ipz_queue_ctor(queue, nr_q_pages, EHCA_PAGESIZE,
  269. wqe_size, nr_sges);
  270. if (!ipz_rc) {
  271. ehca_err(ib_dev, "Cannot allocate page for queue. ipz_rc=%x",
  272. ipz_rc);
  273. return -EBUSY;
  274. }
  275. /* register queue pages */
  276. for (cnt = 0; cnt < nr_q_pages; cnt++) {
  277. vpage = ipz_qpageit_get_inc(queue);
  278. if (!vpage) {
  279. ehca_err(ib_dev, "ipz_qpageit_get_inc() "
  280. "failed p_vpage= %p", vpage);
  281. ret = -EINVAL;
  282. goto init_qp_queue1;
  283. }
  284. rpage = virt_to_abs(vpage);
  285. h_ret = hipz_h_register_rpage_qp(ipz_hca_handle,
  286. my_qp->ipz_qp_handle,
  287. NULL, 0, q_type,
  288. rpage, 1,
  289. my_qp->galpas.kernel);
  290. if (cnt == (nr_q_pages - 1)) { /* last page! */
  291. if (h_ret != expected_hret) {
  292. ehca_err(ib_dev, "hipz_qp_register_rpage() "
  293. "h_ret= %lx ", h_ret);
  294. ret = ehca2ib_return_code(h_ret);
  295. goto init_qp_queue1;
  296. }
  297. vpage = ipz_qpageit_get_inc(&my_qp->ipz_rqueue);
  298. if (vpage) {
  299. ehca_err(ib_dev, "ipz_qpageit_get_inc() "
  300. "should not succeed vpage=%p", vpage);
  301. ret = -EINVAL;
  302. goto init_qp_queue1;
  303. }
  304. } else {
  305. if (h_ret != H_PAGE_REGISTERED) {
  306. ehca_err(ib_dev, "hipz_qp_register_rpage() "
  307. "h_ret= %lx ", h_ret);
  308. ret = ehca2ib_return_code(h_ret);
  309. goto init_qp_queue1;
  310. }
  311. }
  312. }
  313. ipz_qeit_reset(queue);
  314. return 0;
  315. init_qp_queue1:
  316. ipz_queue_dtor(queue);
  317. return ret;
  318. }
  319. struct ib_qp *ehca_create_qp(struct ib_pd *pd,
  320. struct ib_qp_init_attr *init_attr,
  321. struct ib_udata *udata)
  322. {
  323. static int da_rc_msg_size[]={ 128, 256, 512, 1024, 2048, 4096 };
  324. static int da_ud_sq_msg_size[]={ 128, 384, 896, 1920, 3968 };
  325. struct ehca_qp *my_qp;
  326. struct ehca_pd *my_pd = container_of(pd, struct ehca_pd, ib_pd);
  327. struct ehca_shca *shca = container_of(pd->device, struct ehca_shca,
  328. ib_device);
  329. struct ib_ucontext *context = NULL;
  330. u64 h_ret;
  331. int is_llqp = 0, has_srq = 0;
  332. int qp_type, max_send_sge, max_recv_sge, ret;
  333. /* h_call's out parameters */
  334. struct ehca_alloc_qp_parms parms;
  335. u32 swqe_size = 0, rwqe_size = 0;
  336. unsigned long flags;
  337. memset(&parms, 0, sizeof(parms));
  338. qp_type = init_attr->qp_type;
  339. if (init_attr->sq_sig_type != IB_SIGNAL_REQ_WR &&
  340. init_attr->sq_sig_type != IB_SIGNAL_ALL_WR) {
  341. ehca_err(pd->device, "init_attr->sg_sig_type=%x not allowed",
  342. init_attr->sq_sig_type);
  343. return ERR_PTR(-EINVAL);
  344. }
  345. /* save LLQP info */
  346. if (qp_type & 0x80) {
  347. is_llqp = 1;
  348. parms.ext_type = EQPT_LLQP;
  349. parms.ll_comp_flags = qp_type & LLQP_COMP_MASK;
  350. }
  351. qp_type &= 0x1F;
  352. /* check for SRQ */
  353. has_srq = !!(init_attr->srq);
  354. if (is_llqp && has_srq) {
  355. ehca_err(pd->device, "LLQPs can't have an SRQ");
  356. return ERR_PTR(-EINVAL);
  357. }
  358. /* check QP type */
  359. if (qp_type != IB_QPT_UD &&
  360. qp_type != IB_QPT_UC &&
  361. qp_type != IB_QPT_RC &&
  362. qp_type != IB_QPT_SMI &&
  363. qp_type != IB_QPT_GSI) {
  364. ehca_err(pd->device, "wrong QP Type=%x", qp_type);
  365. return ERR_PTR(-EINVAL);
  366. }
  367. if (is_llqp && (qp_type != IB_QPT_RC && qp_type != IB_QPT_UD)) {
  368. ehca_err(pd->device, "unsupported LL QP Type=%x", qp_type);
  369. return ERR_PTR(-EINVAL);
  370. } else if (is_llqp && qp_type == IB_QPT_RC &&
  371. (init_attr->cap.max_send_wr > 255 ||
  372. init_attr->cap.max_recv_wr > 255 )) {
  373. ehca_err(pd->device, "Invalid Number of max_sq_wr=%x "
  374. "or max_rq_wr=%x for RC LLQP",
  375. init_attr->cap.max_send_wr,
  376. init_attr->cap.max_recv_wr);
  377. return ERR_PTR(-EINVAL);
  378. } else if (is_llqp && qp_type == IB_QPT_UD &&
  379. init_attr->cap.max_send_wr > 255) {
  380. ehca_err(pd->device,
  381. "Invalid Number of max_send_wr=%x for UD QP_TYPE=%x",
  382. init_attr->cap.max_send_wr, qp_type);
  383. return ERR_PTR(-EINVAL);
  384. }
  385. if (pd->uobject && udata)
  386. context = pd->uobject->context;
  387. my_qp = kmem_cache_zalloc(qp_cache, GFP_KERNEL);
  388. if (!my_qp) {
  389. ehca_err(pd->device, "pd=%p not enough memory to alloc qp", pd);
  390. return ERR_PTR(-ENOMEM);
  391. }
  392. spin_lock_init(&my_qp->spinlock_s);
  393. spin_lock_init(&my_qp->spinlock_r);
  394. my_qp->recv_cq =
  395. container_of(init_attr->recv_cq, struct ehca_cq, ib_cq);
  396. my_qp->send_cq =
  397. container_of(init_attr->send_cq, struct ehca_cq, ib_cq);
  398. do {
  399. if (!idr_pre_get(&ehca_qp_idr, GFP_KERNEL)) {
  400. ret = -ENOMEM;
  401. ehca_err(pd->device, "Can't reserve idr resources.");
  402. goto create_qp_exit0;
  403. }
  404. spin_lock_irqsave(&ehca_qp_idr_lock, flags);
  405. ret = idr_get_new(&ehca_qp_idr, my_qp, &my_qp->token);
  406. spin_unlock_irqrestore(&ehca_qp_idr_lock, flags);
  407. } while (ret == -EAGAIN);
  408. if (ret) {
  409. ret = -ENOMEM;
  410. ehca_err(pd->device, "Can't allocate new idr entry.");
  411. goto create_qp_exit0;
  412. }
  413. parms.servicetype = ibqptype2servicetype(qp_type);
  414. if (parms.servicetype < 0) {
  415. ret = -EINVAL;
  416. ehca_err(pd->device, "Invalid qp_type=%x", qp_type);
  417. goto create_qp_exit0;
  418. }
  419. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  420. parms.sigtype = HCALL_SIGT_EVERY;
  421. else
  422. parms.sigtype = HCALL_SIGT_BY_WQE;
  423. /* UD_AV CIRCUMVENTION */
  424. max_send_sge = init_attr->cap.max_send_sge;
  425. max_recv_sge = init_attr->cap.max_recv_sge;
  426. if (parms.servicetype == ST_UD) {
  427. max_send_sge += 2;
  428. max_recv_sge += 2;
  429. }
  430. parms.token = my_qp->token;
  431. parms.eq_handle = shca->eq.ipz_eq_handle;
  432. parms.pd = my_pd->fw_pd;
  433. parms.send_cq_handle = my_qp->send_cq->ipz_cq_handle;
  434. parms.recv_cq_handle = my_qp->recv_cq->ipz_cq_handle;
  435. parms.max_send_wr = init_attr->cap.max_send_wr;
  436. parms.max_recv_wr = init_attr->cap.max_recv_wr;
  437. parms.max_send_sge = max_send_sge;
  438. parms.max_recv_sge = max_recv_sge;
  439. h_ret = hipz_h_alloc_resource_qp(shca->ipz_hca_handle, &parms);
  440. if (h_ret != H_SUCCESS) {
  441. ehca_err(pd->device, "h_alloc_resource_qp() failed h_ret=%lx",
  442. h_ret);
  443. ret = ehca2ib_return_code(h_ret);
  444. goto create_qp_exit1;
  445. }
  446. my_qp->ib_qp.qp_num = my_qp->real_qp_num = parms.real_qp_num;
  447. my_qp->ipz_qp_handle = parms.qp_handle;
  448. my_qp->galpas = parms.galpas;
  449. switch (qp_type) {
  450. case IB_QPT_RC:
  451. if (!is_llqp) {
  452. swqe_size = offsetof(struct ehca_wqe, u.nud.sg_list[
  453. (parms.act_nr_send_sges)]);
  454. rwqe_size = offsetof(struct ehca_wqe, u.nud.sg_list[
  455. (parms.act_nr_recv_sges)]);
  456. } else { /* for LLQP we need to use msg size, not wqe size */
  457. swqe_size = da_rc_msg_size[max_send_sge];
  458. rwqe_size = da_rc_msg_size[max_recv_sge];
  459. parms.act_nr_send_sges = 1;
  460. parms.act_nr_recv_sges = 1;
  461. }
  462. break;
  463. case IB_QPT_UC:
  464. swqe_size = offsetof(struct ehca_wqe,
  465. u.nud.sg_list[parms.act_nr_send_sges]);
  466. rwqe_size = offsetof(struct ehca_wqe,
  467. u.nud.sg_list[parms.act_nr_recv_sges]);
  468. break;
  469. case IB_QPT_UD:
  470. case IB_QPT_GSI:
  471. case IB_QPT_SMI:
  472. /* UD circumvention */
  473. parms.act_nr_recv_sges -= 2;
  474. parms.act_nr_send_sges -= 2;
  475. if (is_llqp) {
  476. swqe_size = da_ud_sq_msg_size[max_send_sge];
  477. rwqe_size = da_rc_msg_size[max_recv_sge];
  478. parms.act_nr_send_sges = 1;
  479. parms.act_nr_recv_sges = 1;
  480. } else {
  481. swqe_size = offsetof(struct ehca_wqe,
  482. u.ud_av.sg_list[parms.act_nr_send_sges]);
  483. rwqe_size = offsetof(struct ehca_wqe,
  484. u.ud_av.sg_list[parms.act_nr_recv_sges]);
  485. }
  486. if (IB_QPT_GSI == qp_type || IB_QPT_SMI == qp_type) {
  487. parms.act_nr_send_wqes = init_attr->cap.max_send_wr;
  488. parms.act_nr_recv_wqes = init_attr->cap.max_recv_wr;
  489. parms.act_nr_send_sges = init_attr->cap.max_send_sge;
  490. parms.act_nr_recv_sges = init_attr->cap.max_recv_sge;
  491. my_qp->ib_qp.qp_num = (qp_type == IB_QPT_SMI) ? 0 : 1;
  492. }
  493. break;
  494. default:
  495. break;
  496. }
  497. /* initialize r/squeue and register queue pages */
  498. ret = init_qp_queue(shca, my_qp, &my_qp->ipz_squeue, 0,
  499. has_srq ? H_SUCCESS : H_PAGE_REGISTERED,
  500. parms.nr_sq_pages, swqe_size,
  501. parms.act_nr_send_sges);
  502. if (ret) {
  503. ehca_err(pd->device,
  504. "Couldn't initialize squeue and pages ret=%x", ret);
  505. goto create_qp_exit2;
  506. }
  507. ret = init_qp_queue(shca, my_qp, &my_qp->ipz_rqueue, 1, H_SUCCESS,
  508. parms.nr_rq_pages, rwqe_size,
  509. parms.act_nr_recv_sges);
  510. if (ret) {
  511. ehca_err(pd->device,
  512. "Couldn't initialize rqueue and pages ret=%x", ret);
  513. goto create_qp_exit3;
  514. }
  515. my_qp->ib_qp.pd = &my_pd->ib_pd;
  516. my_qp->ib_qp.device = my_pd->ib_pd.device;
  517. my_qp->ib_qp.recv_cq = init_attr->recv_cq;
  518. my_qp->ib_qp.send_cq = init_attr->send_cq;
  519. my_qp->ib_qp.qp_type = my_qp->qp_type = qp_type;
  520. my_qp->ib_qp.srq = init_attr->srq;
  521. my_qp->ib_qp.qp_context = init_attr->qp_context;
  522. my_qp->ib_qp.event_handler = init_attr->event_handler;
  523. init_attr->cap.max_inline_data = 0; /* not supported yet */
  524. init_attr->cap.max_recv_sge = parms.act_nr_recv_sges;
  525. init_attr->cap.max_recv_wr = parms.act_nr_recv_wqes;
  526. init_attr->cap.max_send_sge = parms.act_nr_send_sges;
  527. init_attr->cap.max_send_wr = parms.act_nr_send_wqes;
  528. my_qp->init_attr = *init_attr;
  529. /* NOTE: define_apq0() not supported yet */
  530. if (qp_type == IB_QPT_GSI) {
  531. h_ret = ehca_define_sqp(shca, my_qp, init_attr);
  532. if (h_ret != H_SUCCESS) {
  533. ehca_err(pd->device, "ehca_define_sqp() failed rc=%lx",
  534. h_ret);
  535. ret = ehca2ib_return_code(h_ret);
  536. goto create_qp_exit4;
  537. }
  538. }
  539. if (init_attr->send_cq) {
  540. struct ehca_cq *cq = container_of(init_attr->send_cq,
  541. struct ehca_cq, ib_cq);
  542. ret = ehca_cq_assign_qp(cq, my_qp);
  543. if (ret) {
  544. ehca_err(pd->device, "Couldn't assign qp to send_cq ret=%x",
  545. ret);
  546. goto create_qp_exit4;
  547. }
  548. my_qp->send_cq = cq;
  549. }
  550. /* copy queues, galpa data to user space */
  551. if (context && udata) {
  552. struct ipz_queue *ipz_rqueue = &my_qp->ipz_rqueue;
  553. struct ipz_queue *ipz_squeue = &my_qp->ipz_squeue;
  554. struct ehca_create_qp_resp resp;
  555. memset(&resp, 0, sizeof(resp));
  556. resp.qp_num = my_qp->real_qp_num;
  557. resp.token = my_qp->token;
  558. resp.qp_type = my_qp->qp_type;
  559. resp.qkey = my_qp->qkey;
  560. resp.real_qp_num = my_qp->real_qp_num;
  561. /* rqueue properties */
  562. resp.ipz_rqueue.qe_size = ipz_rqueue->qe_size;
  563. resp.ipz_rqueue.act_nr_of_sg = ipz_rqueue->act_nr_of_sg;
  564. resp.ipz_rqueue.queue_length = ipz_rqueue->queue_length;
  565. resp.ipz_rqueue.pagesize = ipz_rqueue->pagesize;
  566. resp.ipz_rqueue.toggle_state = ipz_rqueue->toggle_state;
  567. /* squeue properties */
  568. resp.ipz_squeue.qe_size = ipz_squeue->qe_size;
  569. resp.ipz_squeue.act_nr_of_sg = ipz_squeue->act_nr_of_sg;
  570. resp.ipz_squeue.queue_length = ipz_squeue->queue_length;
  571. resp.ipz_squeue.pagesize = ipz_squeue->pagesize;
  572. resp.ipz_squeue.toggle_state = ipz_squeue->toggle_state;
  573. if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
  574. ehca_err(pd->device, "Copy to udata failed");
  575. ret = -EINVAL;
  576. goto create_qp_exit4;
  577. }
  578. }
  579. return &my_qp->ib_qp;
  580. create_qp_exit4:
  581. ipz_queue_dtor(&my_qp->ipz_rqueue);
  582. create_qp_exit3:
  583. ipz_queue_dtor(&my_qp->ipz_squeue);
  584. create_qp_exit2:
  585. hipz_h_destroy_qp(shca->ipz_hca_handle, my_qp);
  586. create_qp_exit1:
  587. spin_lock_irqsave(&ehca_qp_idr_lock, flags);
  588. idr_remove(&ehca_qp_idr, my_qp->token);
  589. spin_unlock_irqrestore(&ehca_qp_idr_lock, flags);
  590. create_qp_exit0:
  591. kmem_cache_free(qp_cache, my_qp);
  592. return ERR_PTR(ret);
  593. }
  594. /*
  595. * prepare_sqe_rts called by internal_modify_qp() at trans sqe -> rts
  596. * set purge bit of bad wqe and subsequent wqes to avoid reentering sqe
  597. * returns total number of bad wqes in bad_wqe_cnt
  598. */
  599. static int prepare_sqe_rts(struct ehca_qp *my_qp, struct ehca_shca *shca,
  600. int *bad_wqe_cnt)
  601. {
  602. u64 h_ret;
  603. struct ipz_queue *squeue;
  604. void *bad_send_wqe_p, *bad_send_wqe_v;
  605. u64 q_ofs;
  606. struct ehca_wqe *wqe;
  607. int qp_num = my_qp->ib_qp.qp_num;
  608. /* get send wqe pointer */
  609. h_ret = hipz_h_disable_and_get_wqe(shca->ipz_hca_handle,
  610. my_qp->ipz_qp_handle, &my_qp->pf,
  611. &bad_send_wqe_p, NULL, 2);
  612. if (h_ret != H_SUCCESS) {
  613. ehca_err(&shca->ib_device, "hipz_h_disable_and_get_wqe() failed"
  614. " ehca_qp=%p qp_num=%x h_ret=%lx",
  615. my_qp, qp_num, h_ret);
  616. return ehca2ib_return_code(h_ret);
  617. }
  618. bad_send_wqe_p = (void*)((u64)bad_send_wqe_p & (~(1L<<63)));
  619. ehca_dbg(&shca->ib_device, "qp_num=%x bad_send_wqe_p=%p",
  620. qp_num, bad_send_wqe_p);
  621. /* convert wqe pointer to vadr */
  622. bad_send_wqe_v = abs_to_virt((u64)bad_send_wqe_p);
  623. if (ehca_debug_level)
  624. ehca_dmp(bad_send_wqe_v, 32, "qp_num=%x bad_wqe", qp_num);
  625. squeue = &my_qp->ipz_squeue;
  626. if (ipz_queue_abs_to_offset(squeue, (u64)bad_send_wqe_p, &q_ofs)) {
  627. ehca_err(&shca->ib_device, "failed to get wqe offset qp_num=%x"
  628. " bad_send_wqe_p=%p", qp_num, bad_send_wqe_p);
  629. return -EFAULT;
  630. }
  631. /* loop sets wqe's purge bit */
  632. wqe = (struct ehca_wqe*)ipz_qeit_calc(squeue, q_ofs);
  633. *bad_wqe_cnt = 0;
  634. while (wqe->optype != 0xff && wqe->wqef != 0xff) {
  635. if (ehca_debug_level)
  636. ehca_dmp(wqe, 32, "qp_num=%x wqe", qp_num);
  637. wqe->nr_of_data_seg = 0; /* suppress data access */
  638. wqe->wqef = WQEF_PURGE; /* WQE to be purged */
  639. q_ofs = ipz_queue_advance_offset(squeue, q_ofs);
  640. wqe = (struct ehca_wqe*)ipz_qeit_calc(squeue, q_ofs);
  641. *bad_wqe_cnt = (*bad_wqe_cnt)+1;
  642. }
  643. /*
  644. * bad wqe will be reprocessed and ignored when pol_cq() is called,
  645. * i.e. nr of wqes with flush error status is one less
  646. */
  647. ehca_dbg(&shca->ib_device, "qp_num=%x flusherr_wqe_cnt=%x",
  648. qp_num, (*bad_wqe_cnt)-1);
  649. wqe->wqef = 0;
  650. return 0;
  651. }
  652. /*
  653. * internal_modify_qp with circumvention to handle aqp0 properly
  654. * smi_reset2init indicates if this is an internal reset-to-init-call for
  655. * smi. This flag must always be zero if called from ehca_modify_qp()!
  656. * This internal func was intorduced to avoid recursion of ehca_modify_qp()!
  657. */
  658. static int internal_modify_qp(struct ib_qp *ibqp,
  659. struct ib_qp_attr *attr,
  660. int attr_mask, int smi_reset2init)
  661. {
  662. enum ib_qp_state qp_cur_state, qp_new_state;
  663. int cnt, qp_attr_idx, ret = 0;
  664. enum ib_qp_statetrans statetrans;
  665. struct hcp_modify_qp_control_block *mqpcb;
  666. struct ehca_qp *my_qp = container_of(ibqp, struct ehca_qp, ib_qp);
  667. struct ehca_shca *shca =
  668. container_of(ibqp->pd->device, struct ehca_shca, ib_device);
  669. u64 update_mask;
  670. u64 h_ret;
  671. int bad_wqe_cnt = 0;
  672. int squeue_locked = 0;
  673. unsigned long spl_flags = 0;
  674. /* do query_qp to obtain current attr values */
  675. mqpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  676. if (!mqpcb) {
  677. ehca_err(ibqp->device, "Could not get zeroed page for mqpcb "
  678. "ehca_qp=%p qp_num=%x ", my_qp, ibqp->qp_num);
  679. return -ENOMEM;
  680. }
  681. h_ret = hipz_h_query_qp(shca->ipz_hca_handle,
  682. my_qp->ipz_qp_handle,
  683. &my_qp->pf,
  684. mqpcb, my_qp->galpas.kernel);
  685. if (h_ret != H_SUCCESS) {
  686. ehca_err(ibqp->device, "hipz_h_query_qp() failed "
  687. "ehca_qp=%p qp_num=%x h_ret=%lx",
  688. my_qp, ibqp->qp_num, h_ret);
  689. ret = ehca2ib_return_code(h_ret);
  690. goto modify_qp_exit1;
  691. }
  692. qp_cur_state = ehca2ib_qp_state(mqpcb->qp_state);
  693. if (qp_cur_state == -EINVAL) { /* invalid qp state */
  694. ret = -EINVAL;
  695. ehca_err(ibqp->device, "Invalid current ehca_qp_state=%x "
  696. "ehca_qp=%p qp_num=%x",
  697. mqpcb->qp_state, my_qp, ibqp->qp_num);
  698. goto modify_qp_exit1;
  699. }
  700. /*
  701. * circumvention to set aqp0 initial state to init
  702. * as expected by IB spec
  703. */
  704. if (smi_reset2init == 0 &&
  705. ibqp->qp_type == IB_QPT_SMI &&
  706. qp_cur_state == IB_QPS_RESET &&
  707. (attr_mask & IB_QP_STATE) &&
  708. attr->qp_state == IB_QPS_INIT) { /* RESET -> INIT */
  709. struct ib_qp_attr smiqp_attr = {
  710. .qp_state = IB_QPS_INIT,
  711. .port_num = my_qp->init_attr.port_num,
  712. .pkey_index = 0,
  713. .qkey = 0
  714. };
  715. int smiqp_attr_mask = IB_QP_STATE | IB_QP_PORT |
  716. IB_QP_PKEY_INDEX | IB_QP_QKEY;
  717. int smirc = internal_modify_qp(
  718. ibqp, &smiqp_attr, smiqp_attr_mask, 1);
  719. if (smirc) {
  720. ehca_err(ibqp->device, "SMI RESET -> INIT failed. "
  721. "ehca_modify_qp() rc=%x", smirc);
  722. ret = H_PARAMETER;
  723. goto modify_qp_exit1;
  724. }
  725. qp_cur_state = IB_QPS_INIT;
  726. ehca_dbg(ibqp->device, "SMI RESET -> INIT succeeded");
  727. }
  728. /* is transmitted current state equal to "real" current state */
  729. if ((attr_mask & IB_QP_CUR_STATE) &&
  730. qp_cur_state != attr->cur_qp_state) {
  731. ret = -EINVAL;
  732. ehca_err(ibqp->device,
  733. "Invalid IB_QP_CUR_STATE attr->curr_qp_state=%x <>"
  734. " actual cur_qp_state=%x. ehca_qp=%p qp_num=%x",
  735. attr->cur_qp_state, qp_cur_state, my_qp, ibqp->qp_num);
  736. goto modify_qp_exit1;
  737. }
  738. ehca_dbg(ibqp->device,"ehca_qp=%p qp_num=%x current qp_state=%x "
  739. "new qp_state=%x attribute_mask=%x",
  740. my_qp, ibqp->qp_num, qp_cur_state, attr->qp_state, attr_mask);
  741. qp_new_state = attr_mask & IB_QP_STATE ? attr->qp_state : qp_cur_state;
  742. if (!smi_reset2init &&
  743. !ib_modify_qp_is_ok(qp_cur_state, qp_new_state, ibqp->qp_type,
  744. attr_mask)) {
  745. ret = -EINVAL;
  746. ehca_err(ibqp->device,
  747. "Invalid qp transition new_state=%x cur_state=%x "
  748. "ehca_qp=%p qp_num=%x attr_mask=%x", qp_new_state,
  749. qp_cur_state, my_qp, ibqp->qp_num, attr_mask);
  750. goto modify_qp_exit1;
  751. }
  752. if ((mqpcb->qp_state = ib2ehca_qp_state(qp_new_state)))
  753. update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_STATE, 1);
  754. else {
  755. ret = -EINVAL;
  756. ehca_err(ibqp->device, "Invalid new qp state=%x "
  757. "ehca_qp=%p qp_num=%x",
  758. qp_new_state, my_qp, ibqp->qp_num);
  759. goto modify_qp_exit1;
  760. }
  761. /* retrieve state transition struct to get req and opt attrs */
  762. statetrans = get_modqp_statetrans(qp_cur_state, qp_new_state);
  763. if (statetrans < 0) {
  764. ret = -EINVAL;
  765. ehca_err(ibqp->device, "<INVALID STATE CHANGE> qp_cur_state=%x "
  766. "new_qp_state=%x State_xsition=%x ehca_qp=%p "
  767. "qp_num=%x", qp_cur_state, qp_new_state,
  768. statetrans, my_qp, ibqp->qp_num);
  769. goto modify_qp_exit1;
  770. }
  771. qp_attr_idx = ib2ehcaqptype(ibqp->qp_type);
  772. if (qp_attr_idx < 0) {
  773. ret = qp_attr_idx;
  774. ehca_err(ibqp->device,
  775. "Invalid QP type=%x ehca_qp=%p qp_num=%x",
  776. ibqp->qp_type, my_qp, ibqp->qp_num);
  777. goto modify_qp_exit1;
  778. }
  779. ehca_dbg(ibqp->device,
  780. "ehca_qp=%p qp_num=%x <VALID STATE CHANGE> qp_state_xsit=%x",
  781. my_qp, ibqp->qp_num, statetrans);
  782. /* sqe -> rts: set purge bit of bad wqe before actual trans */
  783. if ((my_qp->qp_type == IB_QPT_UD ||
  784. my_qp->qp_type == IB_QPT_GSI ||
  785. my_qp->qp_type == IB_QPT_SMI) &&
  786. statetrans == IB_QPST_SQE2RTS) {
  787. /* mark next free wqe if kernel */
  788. if (!ibqp->uobject) {
  789. struct ehca_wqe *wqe;
  790. /* lock send queue */
  791. spin_lock_irqsave(&my_qp->spinlock_s, spl_flags);
  792. squeue_locked = 1;
  793. /* mark next free wqe */
  794. wqe = (struct ehca_wqe*)
  795. ipz_qeit_get(&my_qp->ipz_squeue);
  796. wqe->optype = wqe->wqef = 0xff;
  797. ehca_dbg(ibqp->device, "qp_num=%x next_free_wqe=%p",
  798. ibqp->qp_num, wqe);
  799. }
  800. ret = prepare_sqe_rts(my_qp, shca, &bad_wqe_cnt);
  801. if (ret) {
  802. ehca_err(ibqp->device, "prepare_sqe_rts() failed "
  803. "ehca_qp=%p qp_num=%x ret=%x",
  804. my_qp, ibqp->qp_num, ret);
  805. goto modify_qp_exit2;
  806. }
  807. }
  808. /*
  809. * enable RDMA_Atomic_Control if reset->init und reliable con
  810. * this is necessary since gen2 does not provide that flag,
  811. * but pHyp requires it
  812. */
  813. if (statetrans == IB_QPST_RESET2INIT &&
  814. (ibqp->qp_type == IB_QPT_RC || ibqp->qp_type == IB_QPT_UC)) {
  815. mqpcb->rdma_atomic_ctrl = 3;
  816. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RDMA_ATOMIC_CTRL, 1);
  817. }
  818. /* circ. pHyp requires #RDMA/Atomic Resp Res for UC INIT -> RTR */
  819. if (statetrans == IB_QPST_INIT2RTR &&
  820. (ibqp->qp_type == IB_QPT_UC) &&
  821. !(attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)) {
  822. mqpcb->rdma_nr_atomic_resp_res = 1; /* default to 1 */
  823. update_mask |=
  824. EHCA_BMASK_SET(MQPCB_MASK_RDMA_NR_ATOMIC_RESP_RES, 1);
  825. }
  826. if (attr_mask & IB_QP_PKEY_INDEX) {
  827. mqpcb->prim_p_key_idx = attr->pkey_index;
  828. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PRIM_P_KEY_IDX, 1);
  829. }
  830. if (attr_mask & IB_QP_PORT) {
  831. if (attr->port_num < 1 || attr->port_num > shca->num_ports) {
  832. ret = -EINVAL;
  833. ehca_err(ibqp->device, "Invalid port=%x. "
  834. "ehca_qp=%p qp_num=%x num_ports=%x",
  835. attr->port_num, my_qp, ibqp->qp_num,
  836. shca->num_ports);
  837. goto modify_qp_exit2;
  838. }
  839. mqpcb->prim_phys_port = attr->port_num;
  840. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PRIM_PHYS_PORT, 1);
  841. }
  842. if (attr_mask & IB_QP_QKEY) {
  843. mqpcb->qkey = attr->qkey;
  844. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_QKEY, 1);
  845. }
  846. if (attr_mask & IB_QP_AV) {
  847. int ah_mult = ib_rate_to_mult(attr->ah_attr.static_rate);
  848. int ehca_mult = ib_rate_to_mult(shca->sport[my_qp->
  849. init_attr.port_num].rate);
  850. mqpcb->dlid = attr->ah_attr.dlid;
  851. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DLID, 1);
  852. mqpcb->source_path_bits = attr->ah_attr.src_path_bits;
  853. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SOURCE_PATH_BITS, 1);
  854. mqpcb->service_level = attr->ah_attr.sl;
  855. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SERVICE_LEVEL, 1);
  856. if (ah_mult < ehca_mult)
  857. mqpcb->max_static_rate = (ah_mult > 0) ?
  858. ((ehca_mult - 1) / ah_mult) : 0;
  859. else
  860. mqpcb->max_static_rate = 0;
  861. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_MAX_STATIC_RATE, 1);
  862. /*
  863. * Always supply the GRH flag, even if it's zero, to give the
  864. * hypervisor a clear "yes" or "no" instead of a "perhaps"
  865. */
  866. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_GRH_FLAG, 1);
  867. /*
  868. * only if GRH is TRUE we might consider SOURCE_GID_IDX
  869. * and DEST_GID otherwise phype will return H_ATTR_PARM!!!
  870. */
  871. if (attr->ah_attr.ah_flags == IB_AH_GRH) {
  872. mqpcb->send_grh_flag = 1;
  873. mqpcb->source_gid_idx = attr->ah_attr.grh.sgid_index;
  874. update_mask |=
  875. EHCA_BMASK_SET(MQPCB_MASK_SOURCE_GID_IDX, 1);
  876. for (cnt = 0; cnt < 16; cnt++)
  877. mqpcb->dest_gid.byte[cnt] =
  878. attr->ah_attr.grh.dgid.raw[cnt];
  879. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DEST_GID, 1);
  880. mqpcb->flow_label = attr->ah_attr.grh.flow_label;
  881. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_FLOW_LABEL, 1);
  882. mqpcb->hop_limit = attr->ah_attr.grh.hop_limit;
  883. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_HOP_LIMIT, 1);
  884. mqpcb->traffic_class = attr->ah_attr.grh.traffic_class;
  885. update_mask |=
  886. EHCA_BMASK_SET(MQPCB_MASK_TRAFFIC_CLASS, 1);
  887. }
  888. }
  889. if (attr_mask & IB_QP_PATH_MTU) {
  890. mqpcb->path_mtu = attr->path_mtu;
  891. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PATH_MTU, 1);
  892. }
  893. if (attr_mask & IB_QP_TIMEOUT) {
  894. mqpcb->timeout = attr->timeout;
  895. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_TIMEOUT, 1);
  896. }
  897. if (attr_mask & IB_QP_RETRY_CNT) {
  898. mqpcb->retry_count = attr->retry_cnt;
  899. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RETRY_COUNT, 1);
  900. }
  901. if (attr_mask & IB_QP_RNR_RETRY) {
  902. mqpcb->rnr_retry_count = attr->rnr_retry;
  903. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RNR_RETRY_COUNT, 1);
  904. }
  905. if (attr_mask & IB_QP_RQ_PSN) {
  906. mqpcb->receive_psn = attr->rq_psn;
  907. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RECEIVE_PSN, 1);
  908. }
  909. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  910. mqpcb->rdma_nr_atomic_resp_res = attr->max_dest_rd_atomic < 3 ?
  911. attr->max_dest_rd_atomic : 2;
  912. update_mask |=
  913. EHCA_BMASK_SET(MQPCB_MASK_RDMA_NR_ATOMIC_RESP_RES, 1);
  914. }
  915. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  916. mqpcb->rdma_atomic_outst_dest_qp = attr->max_rd_atomic < 3 ?
  917. attr->max_rd_atomic : 2;
  918. update_mask |=
  919. EHCA_BMASK_SET
  920. (MQPCB_MASK_RDMA_ATOMIC_OUTST_DEST_QP, 1);
  921. }
  922. if (attr_mask & IB_QP_ALT_PATH) {
  923. int ah_mult = ib_rate_to_mult(attr->alt_ah_attr.static_rate);
  924. int ehca_mult = ib_rate_to_mult(
  925. shca->sport[my_qp->init_attr.port_num].rate);
  926. mqpcb->dlid_al = attr->alt_ah_attr.dlid;
  927. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DLID_AL, 1);
  928. mqpcb->source_path_bits_al = attr->alt_ah_attr.src_path_bits;
  929. update_mask |=
  930. EHCA_BMASK_SET(MQPCB_MASK_SOURCE_PATH_BITS_AL, 1);
  931. mqpcb->service_level_al = attr->alt_ah_attr.sl;
  932. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SERVICE_LEVEL_AL, 1);
  933. if (ah_mult < ehca_mult)
  934. mqpcb->max_static_rate = (ah_mult > 0) ?
  935. ((ehca_mult - 1) / ah_mult) : 0;
  936. else
  937. mqpcb->max_static_rate_al = 0;
  938. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_MAX_STATIC_RATE_AL, 1);
  939. /*
  940. * only if GRH is TRUE we might consider SOURCE_GID_IDX
  941. * and DEST_GID otherwise phype will return H_ATTR_PARM!!!
  942. */
  943. if (attr->alt_ah_attr.ah_flags == IB_AH_GRH) {
  944. mqpcb->send_grh_flag_al = 1 << 31;
  945. update_mask |=
  946. EHCA_BMASK_SET(MQPCB_MASK_SEND_GRH_FLAG_AL, 1);
  947. mqpcb->source_gid_idx_al =
  948. attr->alt_ah_attr.grh.sgid_index;
  949. update_mask |=
  950. EHCA_BMASK_SET(MQPCB_MASK_SOURCE_GID_IDX_AL, 1);
  951. for (cnt = 0; cnt < 16; cnt++)
  952. mqpcb->dest_gid_al.byte[cnt] =
  953. attr->alt_ah_attr.grh.dgid.raw[cnt];
  954. update_mask |=
  955. EHCA_BMASK_SET(MQPCB_MASK_DEST_GID_AL, 1);
  956. mqpcb->flow_label_al = attr->alt_ah_attr.grh.flow_label;
  957. update_mask |=
  958. EHCA_BMASK_SET(MQPCB_MASK_FLOW_LABEL_AL, 1);
  959. mqpcb->hop_limit_al = attr->alt_ah_attr.grh.hop_limit;
  960. update_mask |=
  961. EHCA_BMASK_SET(MQPCB_MASK_HOP_LIMIT_AL, 1);
  962. mqpcb->traffic_class_al =
  963. attr->alt_ah_attr.grh.traffic_class;
  964. update_mask |=
  965. EHCA_BMASK_SET(MQPCB_MASK_TRAFFIC_CLASS_AL, 1);
  966. }
  967. }
  968. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  969. mqpcb->min_rnr_nak_timer_field = attr->min_rnr_timer;
  970. update_mask |=
  971. EHCA_BMASK_SET(MQPCB_MASK_MIN_RNR_NAK_TIMER_FIELD, 1);
  972. }
  973. if (attr_mask & IB_QP_SQ_PSN) {
  974. mqpcb->send_psn = attr->sq_psn;
  975. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_PSN, 1);
  976. }
  977. if (attr_mask & IB_QP_DEST_QPN) {
  978. mqpcb->dest_qp_nr = attr->dest_qp_num;
  979. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DEST_QP_NR, 1);
  980. }
  981. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  982. mqpcb->path_migration_state = attr->path_mig_state;
  983. update_mask |=
  984. EHCA_BMASK_SET(MQPCB_MASK_PATH_MIGRATION_STATE, 1);
  985. }
  986. if (attr_mask & IB_QP_CAP) {
  987. mqpcb->max_nr_outst_send_wr = attr->cap.max_send_wr+1;
  988. update_mask |=
  989. EHCA_BMASK_SET(MQPCB_MASK_MAX_NR_OUTST_SEND_WR, 1);
  990. mqpcb->max_nr_outst_recv_wr = attr->cap.max_recv_wr+1;
  991. update_mask |=
  992. EHCA_BMASK_SET(MQPCB_MASK_MAX_NR_OUTST_RECV_WR, 1);
  993. /* no support for max_send/recv_sge yet */
  994. }
  995. if (ehca_debug_level)
  996. ehca_dmp(mqpcb, 4*70, "qp_num=%x", ibqp->qp_num);
  997. h_ret = hipz_h_modify_qp(shca->ipz_hca_handle,
  998. my_qp->ipz_qp_handle,
  999. &my_qp->pf,
  1000. update_mask,
  1001. mqpcb, my_qp->galpas.kernel);
  1002. if (h_ret != H_SUCCESS) {
  1003. ret = ehca2ib_return_code(h_ret);
  1004. ehca_err(ibqp->device, "hipz_h_modify_qp() failed rc=%lx "
  1005. "ehca_qp=%p qp_num=%x",h_ret, my_qp, ibqp->qp_num);
  1006. goto modify_qp_exit2;
  1007. }
  1008. if ((my_qp->qp_type == IB_QPT_UD ||
  1009. my_qp->qp_type == IB_QPT_GSI ||
  1010. my_qp->qp_type == IB_QPT_SMI) &&
  1011. statetrans == IB_QPST_SQE2RTS) {
  1012. /* doorbell to reprocessing wqes */
  1013. iosync(); /* serialize GAL register access */
  1014. hipz_update_sqa(my_qp, bad_wqe_cnt-1);
  1015. ehca_gen_dbg("doorbell for %x wqes", bad_wqe_cnt);
  1016. }
  1017. if (statetrans == IB_QPST_RESET2INIT ||
  1018. statetrans == IB_QPST_INIT2INIT) {
  1019. mqpcb->qp_enable = 1;
  1020. mqpcb->qp_state = EHCA_QPS_INIT;
  1021. update_mask = 0;
  1022. update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_ENABLE, 1);
  1023. h_ret = hipz_h_modify_qp(shca->ipz_hca_handle,
  1024. my_qp->ipz_qp_handle,
  1025. &my_qp->pf,
  1026. update_mask,
  1027. mqpcb,
  1028. my_qp->galpas.kernel);
  1029. if (h_ret != H_SUCCESS) {
  1030. ret = ehca2ib_return_code(h_ret);
  1031. ehca_err(ibqp->device, "ENABLE in context of "
  1032. "RESET_2_INIT failed! Maybe you didn't get "
  1033. "a LID h_ret=%lx ehca_qp=%p qp_num=%x",
  1034. h_ret, my_qp, ibqp->qp_num);
  1035. goto modify_qp_exit2;
  1036. }
  1037. }
  1038. if (statetrans == IB_QPST_ANY2RESET) {
  1039. ipz_qeit_reset(&my_qp->ipz_rqueue);
  1040. ipz_qeit_reset(&my_qp->ipz_squeue);
  1041. }
  1042. if (attr_mask & IB_QP_QKEY)
  1043. my_qp->qkey = attr->qkey;
  1044. modify_qp_exit2:
  1045. if (squeue_locked) { /* this means: sqe -> rts */
  1046. spin_unlock_irqrestore(&my_qp->spinlock_s, spl_flags);
  1047. my_qp->sqerr_purgeflag = 1;
  1048. }
  1049. modify_qp_exit1:
  1050. ehca_free_fw_ctrlblock(mqpcb);
  1051. return ret;
  1052. }
  1053. int ehca_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int attr_mask,
  1054. struct ib_udata *udata)
  1055. {
  1056. struct ehca_qp *my_qp = container_of(ibqp, struct ehca_qp, ib_qp);
  1057. struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd,
  1058. ib_pd);
  1059. u32 cur_pid = current->tgid;
  1060. if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
  1061. my_pd->ownpid != cur_pid) {
  1062. ehca_err(ibqp->pd->device, "Invalid caller pid=%x ownpid=%x",
  1063. cur_pid, my_pd->ownpid);
  1064. return -EINVAL;
  1065. }
  1066. return internal_modify_qp(ibqp, attr, attr_mask, 0);
  1067. }
  1068. int ehca_query_qp(struct ib_qp *qp,
  1069. struct ib_qp_attr *qp_attr,
  1070. int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
  1071. {
  1072. struct ehca_qp *my_qp = container_of(qp, struct ehca_qp, ib_qp);
  1073. struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd,
  1074. ib_pd);
  1075. struct ehca_shca *shca = container_of(qp->device, struct ehca_shca,
  1076. ib_device);
  1077. struct ipz_adapter_handle adapter_handle = shca->ipz_hca_handle;
  1078. struct hcp_modify_qp_control_block *qpcb;
  1079. u32 cur_pid = current->tgid;
  1080. int cnt, ret = 0;
  1081. u64 h_ret;
  1082. if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
  1083. my_pd->ownpid != cur_pid) {
  1084. ehca_err(qp->device, "Invalid caller pid=%x ownpid=%x",
  1085. cur_pid, my_pd->ownpid);
  1086. return -EINVAL;
  1087. }
  1088. if (qp_attr_mask & QP_ATTR_QUERY_NOT_SUPPORTED) {
  1089. ehca_err(qp->device,"Invalid attribute mask "
  1090. "ehca_qp=%p qp_num=%x qp_attr_mask=%x ",
  1091. my_qp, qp->qp_num, qp_attr_mask);
  1092. return -EINVAL;
  1093. }
  1094. qpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  1095. if (!qpcb) {
  1096. ehca_err(qp->device,"Out of memory for qpcb "
  1097. "ehca_qp=%p qp_num=%x", my_qp, qp->qp_num);
  1098. return -ENOMEM;
  1099. }
  1100. h_ret = hipz_h_query_qp(adapter_handle,
  1101. my_qp->ipz_qp_handle,
  1102. &my_qp->pf,
  1103. qpcb, my_qp->galpas.kernel);
  1104. if (h_ret != H_SUCCESS) {
  1105. ret = ehca2ib_return_code(h_ret);
  1106. ehca_err(qp->device,"hipz_h_query_qp() failed "
  1107. "ehca_qp=%p qp_num=%x h_ret=%lx",
  1108. my_qp, qp->qp_num, h_ret);
  1109. goto query_qp_exit1;
  1110. }
  1111. qp_attr->cur_qp_state = ehca2ib_qp_state(qpcb->qp_state);
  1112. qp_attr->qp_state = qp_attr->cur_qp_state;
  1113. if (qp_attr->cur_qp_state == -EINVAL) {
  1114. ret = -EINVAL;
  1115. ehca_err(qp->device,"Got invalid ehca_qp_state=%x "
  1116. "ehca_qp=%p qp_num=%x",
  1117. qpcb->qp_state, my_qp, qp->qp_num);
  1118. goto query_qp_exit1;
  1119. }
  1120. if (qp_attr->qp_state == IB_QPS_SQD)
  1121. qp_attr->sq_draining = 1;
  1122. qp_attr->qkey = qpcb->qkey;
  1123. qp_attr->path_mtu = qpcb->path_mtu;
  1124. qp_attr->path_mig_state = qpcb->path_migration_state;
  1125. qp_attr->rq_psn = qpcb->receive_psn;
  1126. qp_attr->sq_psn = qpcb->send_psn;
  1127. qp_attr->min_rnr_timer = qpcb->min_rnr_nak_timer_field;
  1128. qp_attr->cap.max_send_wr = qpcb->max_nr_outst_send_wr-1;
  1129. qp_attr->cap.max_recv_wr = qpcb->max_nr_outst_recv_wr-1;
  1130. /* UD_AV CIRCUMVENTION */
  1131. if (my_qp->qp_type == IB_QPT_UD) {
  1132. qp_attr->cap.max_send_sge =
  1133. qpcb->actual_nr_sges_in_sq_wqe - 2;
  1134. qp_attr->cap.max_recv_sge =
  1135. qpcb->actual_nr_sges_in_rq_wqe - 2;
  1136. } else {
  1137. qp_attr->cap.max_send_sge =
  1138. qpcb->actual_nr_sges_in_sq_wqe;
  1139. qp_attr->cap.max_recv_sge =
  1140. qpcb->actual_nr_sges_in_rq_wqe;
  1141. }
  1142. qp_attr->cap.max_inline_data = my_qp->sq_max_inline_data_size;
  1143. qp_attr->dest_qp_num = qpcb->dest_qp_nr;
  1144. qp_attr->pkey_index =
  1145. EHCA_BMASK_GET(MQPCB_PRIM_P_KEY_IDX, qpcb->prim_p_key_idx);
  1146. qp_attr->port_num =
  1147. EHCA_BMASK_GET(MQPCB_PRIM_PHYS_PORT, qpcb->prim_phys_port);
  1148. qp_attr->timeout = qpcb->timeout;
  1149. qp_attr->retry_cnt = qpcb->retry_count;
  1150. qp_attr->rnr_retry = qpcb->rnr_retry_count;
  1151. qp_attr->alt_pkey_index =
  1152. EHCA_BMASK_GET(MQPCB_PRIM_P_KEY_IDX, qpcb->alt_p_key_idx);
  1153. qp_attr->alt_port_num = qpcb->alt_phys_port;
  1154. qp_attr->alt_timeout = qpcb->timeout_al;
  1155. /* primary av */
  1156. qp_attr->ah_attr.sl = qpcb->service_level;
  1157. if (qpcb->send_grh_flag) {
  1158. qp_attr->ah_attr.ah_flags = IB_AH_GRH;
  1159. }
  1160. qp_attr->ah_attr.static_rate = qpcb->max_static_rate;
  1161. qp_attr->ah_attr.dlid = qpcb->dlid;
  1162. qp_attr->ah_attr.src_path_bits = qpcb->source_path_bits;
  1163. qp_attr->ah_attr.port_num = qp_attr->port_num;
  1164. /* primary GRH */
  1165. qp_attr->ah_attr.grh.traffic_class = qpcb->traffic_class;
  1166. qp_attr->ah_attr.grh.hop_limit = qpcb->hop_limit;
  1167. qp_attr->ah_attr.grh.sgid_index = qpcb->source_gid_idx;
  1168. qp_attr->ah_attr.grh.flow_label = qpcb->flow_label;
  1169. for (cnt = 0; cnt < 16; cnt++)
  1170. qp_attr->ah_attr.grh.dgid.raw[cnt] =
  1171. qpcb->dest_gid.byte[cnt];
  1172. /* alternate AV */
  1173. qp_attr->alt_ah_attr.sl = qpcb->service_level_al;
  1174. if (qpcb->send_grh_flag_al) {
  1175. qp_attr->alt_ah_attr.ah_flags = IB_AH_GRH;
  1176. }
  1177. qp_attr->alt_ah_attr.static_rate = qpcb->max_static_rate_al;
  1178. qp_attr->alt_ah_attr.dlid = qpcb->dlid_al;
  1179. qp_attr->alt_ah_attr.src_path_bits = qpcb->source_path_bits_al;
  1180. /* alternate GRH */
  1181. qp_attr->alt_ah_attr.grh.traffic_class = qpcb->traffic_class_al;
  1182. qp_attr->alt_ah_attr.grh.hop_limit = qpcb->hop_limit_al;
  1183. qp_attr->alt_ah_attr.grh.sgid_index = qpcb->source_gid_idx_al;
  1184. qp_attr->alt_ah_attr.grh.flow_label = qpcb->flow_label_al;
  1185. for (cnt = 0; cnt < 16; cnt++)
  1186. qp_attr->alt_ah_attr.grh.dgid.raw[cnt] =
  1187. qpcb->dest_gid_al.byte[cnt];
  1188. /* return init attributes given in ehca_create_qp */
  1189. if (qp_init_attr)
  1190. *qp_init_attr = my_qp->init_attr;
  1191. if (ehca_debug_level)
  1192. ehca_dmp(qpcb, 4*70, "qp_num=%x", qp->qp_num);
  1193. query_qp_exit1:
  1194. ehca_free_fw_ctrlblock(qpcb);
  1195. return ret;
  1196. }
  1197. int ehca_destroy_qp(struct ib_qp *ibqp)
  1198. {
  1199. struct ehca_qp *my_qp = container_of(ibqp, struct ehca_qp, ib_qp);
  1200. struct ehca_shca *shca = container_of(ibqp->device, struct ehca_shca,
  1201. ib_device);
  1202. struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd,
  1203. ib_pd);
  1204. u32 cur_pid = current->tgid;
  1205. u32 qp_num = ibqp->qp_num;
  1206. int ret;
  1207. u64 h_ret;
  1208. u8 port_num;
  1209. enum ib_qp_type qp_type;
  1210. unsigned long flags;
  1211. if (ibqp->uobject) {
  1212. if (my_qp->mm_count_galpa ||
  1213. my_qp->mm_count_rqueue || my_qp->mm_count_squeue) {
  1214. ehca_err(ibqp->device, "Resources still referenced in "
  1215. "user space qp_num=%x", ibqp->qp_num);
  1216. return -EINVAL;
  1217. }
  1218. if (my_pd->ownpid != cur_pid) {
  1219. ehca_err(ibqp->device, "Invalid caller pid=%x ownpid=%x",
  1220. cur_pid, my_pd->ownpid);
  1221. return -EINVAL;
  1222. }
  1223. }
  1224. if (my_qp->send_cq) {
  1225. ret = ehca_cq_unassign_qp(my_qp->send_cq,
  1226. my_qp->real_qp_num);
  1227. if (ret) {
  1228. ehca_err(ibqp->device, "Couldn't unassign qp from "
  1229. "send_cq ret=%x qp_num=%x cq_num=%x", ret,
  1230. my_qp->ib_qp.qp_num, my_qp->send_cq->cq_number);
  1231. return ret;
  1232. }
  1233. }
  1234. spin_lock_irqsave(&ehca_qp_idr_lock, flags);
  1235. idr_remove(&ehca_qp_idr, my_qp->token);
  1236. spin_unlock_irqrestore(&ehca_qp_idr_lock, flags);
  1237. h_ret = hipz_h_destroy_qp(shca->ipz_hca_handle, my_qp);
  1238. if (h_ret != H_SUCCESS) {
  1239. ehca_err(ibqp->device, "hipz_h_destroy_qp() failed rc=%lx "
  1240. "ehca_qp=%p qp_num=%x", h_ret, my_qp, qp_num);
  1241. return ehca2ib_return_code(h_ret);
  1242. }
  1243. port_num = my_qp->init_attr.port_num;
  1244. qp_type = my_qp->init_attr.qp_type;
  1245. /* no support for IB_QPT_SMI yet */
  1246. if (qp_type == IB_QPT_GSI) {
  1247. struct ib_event event;
  1248. ehca_info(ibqp->device, "device %s: port %x is inactive.",
  1249. shca->ib_device.name, port_num);
  1250. event.device = &shca->ib_device;
  1251. event.event = IB_EVENT_PORT_ERR;
  1252. event.element.port_num = port_num;
  1253. shca->sport[port_num - 1].port_state = IB_PORT_DOWN;
  1254. ib_dispatch_event(&event);
  1255. }
  1256. ipz_queue_dtor(&my_qp->ipz_rqueue);
  1257. ipz_queue_dtor(&my_qp->ipz_squeue);
  1258. kmem_cache_free(qp_cache, my_qp);
  1259. return 0;
  1260. }
  1261. int ehca_init_qp_cache(void)
  1262. {
  1263. qp_cache = kmem_cache_create("ehca_cache_qp",
  1264. sizeof(struct ehca_qp), 0,
  1265. SLAB_HWCACHE_ALIGN,
  1266. NULL, NULL);
  1267. if (!qp_cache)
  1268. return -ENOMEM;
  1269. return 0;
  1270. }
  1271. void ehca_cleanup_qp_cache(void)
  1272. {
  1273. if (qp_cache)
  1274. kmem_cache_destroy(qp_cache);
  1275. }