ehca_qp.c 50 KB

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