ehca_qp.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  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. resp->offset = queue->offset;
  263. }
  264. /*
  265. * init_qp_queue initializes/constructs r/squeue and registers queue pages.
  266. */
  267. static inline int init_qp_queue(struct ehca_shca *shca,
  268. struct ehca_pd *pd,
  269. struct ehca_qp *my_qp,
  270. struct ipz_queue *queue,
  271. int q_type,
  272. u64 expected_hret,
  273. struct ehca_alloc_queue_parms *parms,
  274. int wqe_size)
  275. {
  276. int ret, cnt, ipz_rc, nr_q_pages;
  277. void *vpage;
  278. u64 rpage, h_ret;
  279. struct ib_device *ib_dev = &shca->ib_device;
  280. struct ipz_adapter_handle ipz_hca_handle = shca->ipz_hca_handle;
  281. if (!parms->queue_size)
  282. return 0;
  283. if (parms->is_small) {
  284. nr_q_pages = 1;
  285. ipz_rc = ipz_queue_ctor(pd, queue, nr_q_pages,
  286. 128 << parms->page_size,
  287. wqe_size, parms->act_nr_sges, 1);
  288. } else {
  289. nr_q_pages = parms->queue_size;
  290. ipz_rc = ipz_queue_ctor(pd, queue, nr_q_pages,
  291. EHCA_PAGESIZE, wqe_size,
  292. parms->act_nr_sges, 0);
  293. }
  294. if (!ipz_rc) {
  295. ehca_err(ib_dev, "Cannot allocate page for queue. ipz_rc=%i",
  296. ipz_rc);
  297. return -EBUSY;
  298. }
  299. /* register queue pages */
  300. for (cnt = 0; cnt < nr_q_pages; cnt++) {
  301. vpage = ipz_qpageit_get_inc(queue);
  302. if (!vpage) {
  303. ehca_err(ib_dev, "ipz_qpageit_get_inc() "
  304. "failed p_vpage= %p", vpage);
  305. ret = -EINVAL;
  306. goto init_qp_queue1;
  307. }
  308. rpage = virt_to_abs(vpage);
  309. h_ret = hipz_h_register_rpage_qp(ipz_hca_handle,
  310. my_qp->ipz_qp_handle,
  311. NULL, 0, q_type,
  312. rpage, parms->is_small ? 0 : 1,
  313. my_qp->galpas.kernel);
  314. if (cnt == (nr_q_pages - 1)) { /* last page! */
  315. if (h_ret != expected_hret) {
  316. ehca_err(ib_dev, "hipz_qp_register_rpage() "
  317. "h_ret=%li", h_ret);
  318. ret = ehca2ib_return_code(h_ret);
  319. goto init_qp_queue1;
  320. }
  321. vpage = ipz_qpageit_get_inc(&my_qp->ipz_rqueue);
  322. if (vpage) {
  323. ehca_err(ib_dev, "ipz_qpageit_get_inc() "
  324. "should not succeed vpage=%p", vpage);
  325. ret = -EINVAL;
  326. goto init_qp_queue1;
  327. }
  328. } else {
  329. if (h_ret != H_PAGE_REGISTERED) {
  330. ehca_err(ib_dev, "hipz_qp_register_rpage() "
  331. "h_ret=%li", h_ret);
  332. ret = ehca2ib_return_code(h_ret);
  333. goto init_qp_queue1;
  334. }
  335. }
  336. }
  337. ipz_qeit_reset(queue);
  338. return 0;
  339. init_qp_queue1:
  340. ipz_queue_dtor(pd, queue);
  341. return ret;
  342. }
  343. static inline int ehca_calc_wqe_size(int act_nr_sge, int is_llqp)
  344. {
  345. if (is_llqp)
  346. return 128 << act_nr_sge;
  347. else
  348. return offsetof(struct ehca_wqe,
  349. u.nud.sg_list[act_nr_sge]);
  350. }
  351. static void ehca_determine_small_queue(struct ehca_alloc_queue_parms *queue,
  352. int req_nr_sge, int is_llqp)
  353. {
  354. u32 wqe_size, q_size;
  355. int act_nr_sge = req_nr_sge;
  356. if (!is_llqp)
  357. /* round up #SGEs so WQE size is a power of 2 */
  358. for (act_nr_sge = 4; act_nr_sge <= 252;
  359. act_nr_sge = 4 + 2 * act_nr_sge)
  360. if (act_nr_sge >= req_nr_sge)
  361. break;
  362. wqe_size = ehca_calc_wqe_size(act_nr_sge, is_llqp);
  363. q_size = wqe_size * (queue->max_wr + 1);
  364. if (q_size <= 512)
  365. queue->page_size = 2;
  366. else if (q_size <= 1024)
  367. queue->page_size = 3;
  368. else
  369. queue->page_size = 0;
  370. queue->is_small = (queue->page_size != 0);
  371. }
  372. /*
  373. * Create an ib_qp struct that is either a QP or an SRQ, depending on
  374. * the value of the is_srq parameter. If init_attr and srq_init_attr share
  375. * fields, the field out of init_attr is used.
  376. */
  377. static struct ehca_qp *internal_create_qp(
  378. struct ib_pd *pd,
  379. struct ib_qp_init_attr *init_attr,
  380. struct ib_srq_init_attr *srq_init_attr,
  381. struct ib_udata *udata, int is_srq)
  382. {
  383. struct ehca_qp *my_qp;
  384. struct ehca_pd *my_pd = container_of(pd, struct ehca_pd, ib_pd);
  385. struct ehca_shca *shca = container_of(pd->device, struct ehca_shca,
  386. ib_device);
  387. struct ib_ucontext *context = NULL;
  388. u64 h_ret;
  389. int is_llqp = 0, has_srq = 0;
  390. int qp_type, max_send_sge, max_recv_sge, ret;
  391. /* h_call's out parameters */
  392. struct ehca_alloc_qp_parms parms;
  393. u32 swqe_size = 0, rwqe_size = 0, ib_qp_num;
  394. unsigned long flags;
  395. memset(&parms, 0, sizeof(parms));
  396. qp_type = init_attr->qp_type;
  397. if (init_attr->sq_sig_type != IB_SIGNAL_REQ_WR &&
  398. init_attr->sq_sig_type != IB_SIGNAL_ALL_WR) {
  399. ehca_err(pd->device, "init_attr->sg_sig_type=%x not allowed",
  400. init_attr->sq_sig_type);
  401. return ERR_PTR(-EINVAL);
  402. }
  403. /* save LLQP info */
  404. if (qp_type & 0x80) {
  405. is_llqp = 1;
  406. parms.ext_type = EQPT_LLQP;
  407. parms.ll_comp_flags = qp_type & LLQP_COMP_MASK;
  408. }
  409. qp_type &= 0x1F;
  410. init_attr->qp_type &= 0x1F;
  411. /* handle SRQ base QPs */
  412. if (init_attr->srq) {
  413. struct ehca_qp *my_srq =
  414. container_of(init_attr->srq, struct ehca_qp, ib_srq);
  415. has_srq = 1;
  416. parms.ext_type = EQPT_SRQBASE;
  417. parms.srq_qpn = my_srq->real_qp_num;
  418. parms.srq_token = my_srq->token;
  419. }
  420. if (is_llqp && has_srq) {
  421. ehca_err(pd->device, "LLQPs can't have an SRQ");
  422. return ERR_PTR(-EINVAL);
  423. }
  424. /* handle SRQs */
  425. if (is_srq) {
  426. parms.ext_type = EQPT_SRQ;
  427. parms.srq_limit = srq_init_attr->attr.srq_limit;
  428. if (init_attr->cap.max_recv_sge > 3) {
  429. ehca_err(pd->device, "no more than three SGEs "
  430. "supported for SRQ pd=%p max_sge=%x",
  431. pd, init_attr->cap.max_recv_sge);
  432. return ERR_PTR(-EINVAL);
  433. }
  434. }
  435. /* check QP type */
  436. if (qp_type != IB_QPT_UD &&
  437. qp_type != IB_QPT_UC &&
  438. qp_type != IB_QPT_RC &&
  439. qp_type != IB_QPT_SMI &&
  440. qp_type != IB_QPT_GSI) {
  441. ehca_err(pd->device, "wrong QP Type=%x", qp_type);
  442. return ERR_PTR(-EINVAL);
  443. }
  444. if (is_llqp) {
  445. switch (qp_type) {
  446. case IB_QPT_RC:
  447. if ((init_attr->cap.max_send_wr > 255) ||
  448. (init_attr->cap.max_recv_wr > 255)) {
  449. ehca_err(pd->device,
  450. "Invalid Number of max_sq_wr=%x "
  451. "or max_rq_wr=%x for RC LLQP",
  452. init_attr->cap.max_send_wr,
  453. init_attr->cap.max_recv_wr);
  454. return ERR_PTR(-EINVAL);
  455. }
  456. break;
  457. case IB_QPT_UD:
  458. if (!EHCA_BMASK_GET(HCA_CAP_UD_LL_QP, shca->hca_cap)) {
  459. ehca_err(pd->device, "UD LLQP not supported "
  460. "by this adapter");
  461. return ERR_PTR(-ENOSYS);
  462. }
  463. if (!(init_attr->cap.max_send_sge <= 5
  464. && init_attr->cap.max_send_sge >= 1
  465. && init_attr->cap.max_recv_sge <= 5
  466. && init_attr->cap.max_recv_sge >= 1)) {
  467. ehca_err(pd->device,
  468. "Invalid Number of max_send_sge=%x "
  469. "or max_recv_sge=%x for UD LLQP",
  470. init_attr->cap.max_send_sge,
  471. init_attr->cap.max_recv_sge);
  472. return ERR_PTR(-EINVAL);
  473. } else if (init_attr->cap.max_send_wr > 255) {
  474. ehca_err(pd->device,
  475. "Invalid Number of "
  476. "max_send_wr=%x for UD QP_TYPE=%x",
  477. init_attr->cap.max_send_wr, qp_type);
  478. return ERR_PTR(-EINVAL);
  479. }
  480. break;
  481. default:
  482. ehca_err(pd->device, "unsupported LL QP Type=%x",
  483. qp_type);
  484. return ERR_PTR(-EINVAL);
  485. break;
  486. }
  487. } else {
  488. int max_sge = (qp_type == IB_QPT_UD || qp_type == IB_QPT_SMI
  489. || qp_type == IB_QPT_GSI) ? 250 : 252;
  490. if (init_attr->cap.max_send_sge > max_sge
  491. || init_attr->cap.max_recv_sge > max_sge) {
  492. ehca_err(pd->device, "Invalid number of SGEs requested "
  493. "send_sge=%x recv_sge=%x max_sge=%x",
  494. init_attr->cap.max_send_sge,
  495. init_attr->cap.max_recv_sge, max_sge);
  496. return ERR_PTR(-EINVAL);
  497. }
  498. }
  499. if (pd->uobject && udata)
  500. context = pd->uobject->context;
  501. my_qp = kmem_cache_zalloc(qp_cache, GFP_KERNEL);
  502. if (!my_qp) {
  503. ehca_err(pd->device, "pd=%p not enough memory to alloc qp", pd);
  504. return ERR_PTR(-ENOMEM);
  505. }
  506. spin_lock_init(&my_qp->spinlock_s);
  507. spin_lock_init(&my_qp->spinlock_r);
  508. my_qp->qp_type = qp_type;
  509. my_qp->ext_type = parms.ext_type;
  510. if (init_attr->recv_cq)
  511. my_qp->recv_cq =
  512. container_of(init_attr->recv_cq, struct ehca_cq, ib_cq);
  513. if (init_attr->send_cq)
  514. my_qp->send_cq =
  515. container_of(init_attr->send_cq, struct ehca_cq, ib_cq);
  516. do {
  517. if (!idr_pre_get(&ehca_qp_idr, GFP_KERNEL)) {
  518. ret = -ENOMEM;
  519. ehca_err(pd->device, "Can't reserve idr resources.");
  520. goto create_qp_exit0;
  521. }
  522. write_lock_irqsave(&ehca_qp_idr_lock, flags);
  523. ret = idr_get_new(&ehca_qp_idr, my_qp, &my_qp->token);
  524. write_unlock_irqrestore(&ehca_qp_idr_lock, flags);
  525. } while (ret == -EAGAIN);
  526. if (ret) {
  527. ret = -ENOMEM;
  528. ehca_err(pd->device, "Can't allocate new idr entry.");
  529. goto create_qp_exit0;
  530. }
  531. if (my_qp->token > 0x1FFFFFF) {
  532. ret = -EINVAL;
  533. ehca_err(pd->device, "Invalid number of qp");
  534. goto create_qp_exit1;
  535. }
  536. parms.servicetype = ibqptype2servicetype(qp_type);
  537. if (parms.servicetype < 0) {
  538. ret = -EINVAL;
  539. ehca_err(pd->device, "Invalid qp_type=%x", qp_type);
  540. goto create_qp_exit1;
  541. }
  542. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  543. parms.sigtype = HCALL_SIGT_EVERY;
  544. else
  545. parms.sigtype = HCALL_SIGT_BY_WQE;
  546. /* UD_AV CIRCUMVENTION */
  547. max_send_sge = init_attr->cap.max_send_sge;
  548. max_recv_sge = init_attr->cap.max_recv_sge;
  549. if (parms.servicetype == ST_UD && !is_llqp) {
  550. max_send_sge += 2;
  551. max_recv_sge += 2;
  552. }
  553. parms.token = my_qp->token;
  554. parms.eq_handle = shca->eq.ipz_eq_handle;
  555. parms.pd = my_pd->fw_pd;
  556. if (my_qp->send_cq)
  557. parms.send_cq_handle = my_qp->send_cq->ipz_cq_handle;
  558. if (my_qp->recv_cq)
  559. parms.recv_cq_handle = my_qp->recv_cq->ipz_cq_handle;
  560. parms.squeue.max_wr = init_attr->cap.max_send_wr;
  561. parms.rqueue.max_wr = init_attr->cap.max_recv_wr;
  562. parms.squeue.max_sge = max_send_sge;
  563. parms.rqueue.max_sge = max_recv_sge;
  564. if (EHCA_BMASK_GET(HCA_CAP_MINI_QP, shca->hca_cap)) {
  565. if (HAS_SQ(my_qp))
  566. ehca_determine_small_queue(
  567. &parms.squeue, max_send_sge, is_llqp);
  568. if (HAS_RQ(my_qp))
  569. ehca_determine_small_queue(
  570. &parms.rqueue, max_recv_sge, is_llqp);
  571. parms.qp_storage =
  572. (parms.squeue.is_small || parms.rqueue.is_small);
  573. }
  574. h_ret = hipz_h_alloc_resource_qp(shca->ipz_hca_handle, &parms);
  575. if (h_ret != H_SUCCESS) {
  576. ehca_err(pd->device, "h_alloc_resource_qp() failed h_ret=%li",
  577. h_ret);
  578. ret = ehca2ib_return_code(h_ret);
  579. goto create_qp_exit1;
  580. }
  581. ib_qp_num = my_qp->real_qp_num = parms.real_qp_num;
  582. my_qp->ipz_qp_handle = parms.qp_handle;
  583. my_qp->galpas = parms.galpas;
  584. swqe_size = ehca_calc_wqe_size(parms.squeue.act_nr_sges, is_llqp);
  585. rwqe_size = ehca_calc_wqe_size(parms.rqueue.act_nr_sges, is_llqp);
  586. switch (qp_type) {
  587. case IB_QPT_RC:
  588. if (is_llqp) {
  589. parms.squeue.act_nr_sges = 1;
  590. parms.rqueue.act_nr_sges = 1;
  591. }
  592. break;
  593. case IB_QPT_UD:
  594. case IB_QPT_GSI:
  595. case IB_QPT_SMI:
  596. /* UD circumvention */
  597. if (is_llqp) {
  598. parms.squeue.act_nr_sges = 1;
  599. parms.rqueue.act_nr_sges = 1;
  600. } else {
  601. parms.squeue.act_nr_sges -= 2;
  602. parms.rqueue.act_nr_sges -= 2;
  603. }
  604. if (IB_QPT_GSI == qp_type || IB_QPT_SMI == qp_type) {
  605. parms.squeue.act_nr_wqes = init_attr->cap.max_send_wr;
  606. parms.rqueue.act_nr_wqes = init_attr->cap.max_recv_wr;
  607. parms.squeue.act_nr_sges = init_attr->cap.max_send_sge;
  608. parms.rqueue.act_nr_sges = init_attr->cap.max_recv_sge;
  609. ib_qp_num = (qp_type == IB_QPT_SMI) ? 0 : 1;
  610. }
  611. break;
  612. default:
  613. break;
  614. }
  615. /* initialize r/squeue and register queue pages */
  616. if (HAS_SQ(my_qp)) {
  617. ret = init_qp_queue(
  618. shca, my_pd, my_qp, &my_qp->ipz_squeue, 0,
  619. HAS_RQ(my_qp) ? H_PAGE_REGISTERED : H_SUCCESS,
  620. &parms.squeue, swqe_size);
  621. if (ret) {
  622. ehca_err(pd->device, "Couldn't initialize squeue "
  623. "and pages ret=%i", ret);
  624. goto create_qp_exit2;
  625. }
  626. }
  627. if (HAS_RQ(my_qp)) {
  628. ret = init_qp_queue(
  629. shca, my_pd, my_qp, &my_qp->ipz_rqueue, 1,
  630. H_SUCCESS, &parms.rqueue, rwqe_size);
  631. if (ret) {
  632. ehca_err(pd->device, "Couldn't initialize rqueue "
  633. "and pages ret=%i", ret);
  634. goto create_qp_exit3;
  635. }
  636. }
  637. if (is_srq) {
  638. my_qp->ib_srq.pd = &my_pd->ib_pd;
  639. my_qp->ib_srq.device = my_pd->ib_pd.device;
  640. my_qp->ib_srq.srq_context = init_attr->qp_context;
  641. my_qp->ib_srq.event_handler = init_attr->event_handler;
  642. } else {
  643. my_qp->ib_qp.qp_num = ib_qp_num;
  644. my_qp->ib_qp.pd = &my_pd->ib_pd;
  645. my_qp->ib_qp.device = my_pd->ib_pd.device;
  646. my_qp->ib_qp.recv_cq = init_attr->recv_cq;
  647. my_qp->ib_qp.send_cq = init_attr->send_cq;
  648. my_qp->ib_qp.qp_type = qp_type;
  649. my_qp->ib_qp.srq = init_attr->srq;
  650. my_qp->ib_qp.qp_context = init_attr->qp_context;
  651. my_qp->ib_qp.event_handler = init_attr->event_handler;
  652. }
  653. init_attr->cap.max_inline_data = 0; /* not supported yet */
  654. init_attr->cap.max_recv_sge = parms.rqueue.act_nr_sges;
  655. init_attr->cap.max_recv_wr = parms.rqueue.act_nr_wqes;
  656. init_attr->cap.max_send_sge = parms.squeue.act_nr_sges;
  657. init_attr->cap.max_send_wr = parms.squeue.act_nr_wqes;
  658. my_qp->init_attr = *init_attr;
  659. /* NOTE: define_apq0() not supported yet */
  660. if (qp_type == IB_QPT_GSI) {
  661. h_ret = ehca_define_sqp(shca, my_qp, init_attr);
  662. if (h_ret != H_SUCCESS) {
  663. ret = ehca2ib_return_code(h_ret);
  664. goto create_qp_exit4;
  665. }
  666. }
  667. if (my_qp->send_cq) {
  668. ret = ehca_cq_assign_qp(my_qp->send_cq, my_qp);
  669. if (ret) {
  670. ehca_err(pd->device,
  671. "Couldn't assign qp to send_cq ret=%i", ret);
  672. goto create_qp_exit4;
  673. }
  674. }
  675. /* copy queues, galpa data to user space */
  676. if (context && udata) {
  677. struct ehca_create_qp_resp resp;
  678. memset(&resp, 0, sizeof(resp));
  679. resp.qp_num = my_qp->real_qp_num;
  680. resp.token = my_qp->token;
  681. resp.qp_type = my_qp->qp_type;
  682. resp.ext_type = my_qp->ext_type;
  683. resp.qkey = my_qp->qkey;
  684. resp.real_qp_num = my_qp->real_qp_num;
  685. if (HAS_SQ(my_qp))
  686. queue2resp(&resp.ipz_squeue, &my_qp->ipz_squeue);
  687. if (HAS_RQ(my_qp))
  688. queue2resp(&resp.ipz_rqueue, &my_qp->ipz_rqueue);
  689. resp.fw_handle_ofs = (u32)
  690. (my_qp->galpas.user.fw_handle & (PAGE_SIZE - 1));
  691. if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
  692. ehca_err(pd->device, "Copy to udata failed");
  693. ret = -EINVAL;
  694. goto create_qp_exit4;
  695. }
  696. }
  697. return my_qp;
  698. create_qp_exit4:
  699. if (HAS_RQ(my_qp))
  700. ipz_queue_dtor(my_pd, &my_qp->ipz_rqueue);
  701. create_qp_exit3:
  702. if (HAS_SQ(my_qp))
  703. ipz_queue_dtor(my_pd, &my_qp->ipz_squeue);
  704. create_qp_exit2:
  705. hipz_h_destroy_qp(shca->ipz_hca_handle, my_qp);
  706. create_qp_exit1:
  707. write_lock_irqsave(&ehca_qp_idr_lock, flags);
  708. idr_remove(&ehca_qp_idr, my_qp->token);
  709. write_unlock_irqrestore(&ehca_qp_idr_lock, flags);
  710. create_qp_exit0:
  711. kmem_cache_free(qp_cache, my_qp);
  712. return ERR_PTR(ret);
  713. }
  714. struct ib_qp *ehca_create_qp(struct ib_pd *pd,
  715. struct ib_qp_init_attr *qp_init_attr,
  716. struct ib_udata *udata)
  717. {
  718. struct ehca_qp *ret;
  719. ret = internal_create_qp(pd, qp_init_attr, NULL, udata, 0);
  720. return IS_ERR(ret) ? (struct ib_qp *)ret : &ret->ib_qp;
  721. }
  722. static int internal_destroy_qp(struct ib_device *dev, struct ehca_qp *my_qp,
  723. struct ib_uobject *uobject);
  724. struct ib_srq *ehca_create_srq(struct ib_pd *pd,
  725. struct ib_srq_init_attr *srq_init_attr,
  726. struct ib_udata *udata)
  727. {
  728. struct ib_qp_init_attr qp_init_attr;
  729. struct ehca_qp *my_qp;
  730. struct ib_srq *ret;
  731. struct ehca_shca *shca = container_of(pd->device, struct ehca_shca,
  732. ib_device);
  733. struct hcp_modify_qp_control_block *mqpcb;
  734. u64 hret, update_mask;
  735. /* For common attributes, internal_create_qp() takes its info
  736. * out of qp_init_attr, so copy all common attrs there.
  737. */
  738. memset(&qp_init_attr, 0, sizeof(qp_init_attr));
  739. qp_init_attr.event_handler = srq_init_attr->event_handler;
  740. qp_init_attr.qp_context = srq_init_attr->srq_context;
  741. qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
  742. qp_init_attr.qp_type = IB_QPT_RC;
  743. qp_init_attr.cap.max_recv_wr = srq_init_attr->attr.max_wr;
  744. qp_init_attr.cap.max_recv_sge = srq_init_attr->attr.max_sge;
  745. my_qp = internal_create_qp(pd, &qp_init_attr, srq_init_attr, udata, 1);
  746. if (IS_ERR(my_qp))
  747. return (struct ib_srq *)my_qp;
  748. /* copy back return values */
  749. srq_init_attr->attr.max_wr = qp_init_attr.cap.max_recv_wr;
  750. srq_init_attr->attr.max_sge = qp_init_attr.cap.max_recv_sge;
  751. /* drive SRQ into RTR state */
  752. mqpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  753. if (!mqpcb) {
  754. ehca_err(pd->device, "Could not get zeroed page for mqpcb "
  755. "ehca_qp=%p qp_num=%x ", my_qp, my_qp->real_qp_num);
  756. ret = ERR_PTR(-ENOMEM);
  757. goto create_srq1;
  758. }
  759. mqpcb->qp_state = EHCA_QPS_INIT;
  760. mqpcb->prim_phys_port = 1;
  761. update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_STATE, 1);
  762. hret = hipz_h_modify_qp(shca->ipz_hca_handle,
  763. my_qp->ipz_qp_handle,
  764. &my_qp->pf,
  765. update_mask,
  766. mqpcb, my_qp->galpas.kernel);
  767. if (hret != H_SUCCESS) {
  768. ehca_err(pd->device, "Could not modify SRQ to INIT"
  769. "ehca_qp=%p qp_num=%x h_ret=%li",
  770. my_qp, my_qp->real_qp_num, hret);
  771. goto create_srq2;
  772. }
  773. mqpcb->qp_enable = 1;
  774. update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_ENABLE, 1);
  775. hret = hipz_h_modify_qp(shca->ipz_hca_handle,
  776. my_qp->ipz_qp_handle,
  777. &my_qp->pf,
  778. update_mask,
  779. mqpcb, my_qp->galpas.kernel);
  780. if (hret != H_SUCCESS) {
  781. ehca_err(pd->device, "Could not enable SRQ"
  782. "ehca_qp=%p qp_num=%x h_ret=%li",
  783. my_qp, my_qp->real_qp_num, hret);
  784. goto create_srq2;
  785. }
  786. mqpcb->qp_state = EHCA_QPS_RTR;
  787. update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_STATE, 1);
  788. hret = hipz_h_modify_qp(shca->ipz_hca_handle,
  789. my_qp->ipz_qp_handle,
  790. &my_qp->pf,
  791. update_mask,
  792. mqpcb, my_qp->galpas.kernel);
  793. if (hret != H_SUCCESS) {
  794. ehca_err(pd->device, "Could not modify SRQ to RTR"
  795. "ehca_qp=%p qp_num=%x h_ret=%li",
  796. my_qp, my_qp->real_qp_num, hret);
  797. goto create_srq2;
  798. }
  799. ehca_free_fw_ctrlblock(mqpcb);
  800. return &my_qp->ib_srq;
  801. create_srq2:
  802. ret = ERR_PTR(ehca2ib_return_code(hret));
  803. ehca_free_fw_ctrlblock(mqpcb);
  804. create_srq1:
  805. internal_destroy_qp(pd->device, my_qp, my_qp->ib_srq.uobject);
  806. return ret;
  807. }
  808. /*
  809. * prepare_sqe_rts called by internal_modify_qp() at trans sqe -> rts
  810. * set purge bit of bad wqe and subsequent wqes to avoid reentering sqe
  811. * returns total number of bad wqes in bad_wqe_cnt
  812. */
  813. static int prepare_sqe_rts(struct ehca_qp *my_qp, struct ehca_shca *shca,
  814. int *bad_wqe_cnt)
  815. {
  816. u64 h_ret;
  817. struct ipz_queue *squeue;
  818. void *bad_send_wqe_p, *bad_send_wqe_v;
  819. u64 q_ofs;
  820. struct ehca_wqe *wqe;
  821. int qp_num = my_qp->ib_qp.qp_num;
  822. /* get send wqe pointer */
  823. h_ret = hipz_h_disable_and_get_wqe(shca->ipz_hca_handle,
  824. my_qp->ipz_qp_handle, &my_qp->pf,
  825. &bad_send_wqe_p, NULL, 2);
  826. if (h_ret != H_SUCCESS) {
  827. ehca_err(&shca->ib_device, "hipz_h_disable_and_get_wqe() failed"
  828. " ehca_qp=%p qp_num=%x h_ret=%li",
  829. my_qp, qp_num, h_ret);
  830. return ehca2ib_return_code(h_ret);
  831. }
  832. bad_send_wqe_p = (void *)((u64)bad_send_wqe_p & (~(1L << 63)));
  833. ehca_dbg(&shca->ib_device, "qp_num=%x bad_send_wqe_p=%p",
  834. qp_num, bad_send_wqe_p);
  835. /* convert wqe pointer to vadr */
  836. bad_send_wqe_v = abs_to_virt((u64)bad_send_wqe_p);
  837. if (ehca_debug_level)
  838. ehca_dmp(bad_send_wqe_v, 32, "qp_num=%x bad_wqe", qp_num);
  839. squeue = &my_qp->ipz_squeue;
  840. if (ipz_queue_abs_to_offset(squeue, (u64)bad_send_wqe_p, &q_ofs)) {
  841. ehca_err(&shca->ib_device, "failed to get wqe offset qp_num=%x"
  842. " bad_send_wqe_p=%p", qp_num, bad_send_wqe_p);
  843. return -EFAULT;
  844. }
  845. /* loop sets wqe's purge bit */
  846. wqe = (struct ehca_wqe *)ipz_qeit_calc(squeue, q_ofs);
  847. *bad_wqe_cnt = 0;
  848. while (wqe->optype != 0xff && wqe->wqef != 0xff) {
  849. if (ehca_debug_level)
  850. ehca_dmp(wqe, 32, "qp_num=%x wqe", qp_num);
  851. wqe->nr_of_data_seg = 0; /* suppress data access */
  852. wqe->wqef = WQEF_PURGE; /* WQE to be purged */
  853. q_ofs = ipz_queue_advance_offset(squeue, q_ofs);
  854. wqe = (struct ehca_wqe *)ipz_qeit_calc(squeue, q_ofs);
  855. *bad_wqe_cnt = (*bad_wqe_cnt)+1;
  856. }
  857. /*
  858. * bad wqe will be reprocessed and ignored when pol_cq() is called,
  859. * i.e. nr of wqes with flush error status is one less
  860. */
  861. ehca_dbg(&shca->ib_device, "qp_num=%x flusherr_wqe_cnt=%x",
  862. qp_num, (*bad_wqe_cnt)-1);
  863. wqe->wqef = 0;
  864. return 0;
  865. }
  866. /*
  867. * internal_modify_qp with circumvention to handle aqp0 properly
  868. * smi_reset2init indicates if this is an internal reset-to-init-call for
  869. * smi. This flag must always be zero if called from ehca_modify_qp()!
  870. * This internal func was intorduced to avoid recursion of ehca_modify_qp()!
  871. */
  872. static int internal_modify_qp(struct ib_qp *ibqp,
  873. struct ib_qp_attr *attr,
  874. int attr_mask, int smi_reset2init)
  875. {
  876. enum ib_qp_state qp_cur_state, qp_new_state;
  877. int cnt, qp_attr_idx, ret = 0;
  878. enum ib_qp_statetrans statetrans;
  879. struct hcp_modify_qp_control_block *mqpcb;
  880. struct ehca_qp *my_qp = container_of(ibqp, struct ehca_qp, ib_qp);
  881. struct ehca_shca *shca =
  882. container_of(ibqp->pd->device, struct ehca_shca, ib_device);
  883. u64 update_mask;
  884. u64 h_ret;
  885. int bad_wqe_cnt = 0;
  886. int squeue_locked = 0;
  887. unsigned long flags = 0;
  888. /* do query_qp to obtain current attr values */
  889. mqpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  890. if (!mqpcb) {
  891. ehca_err(ibqp->device, "Could not get zeroed page for mqpcb "
  892. "ehca_qp=%p qp_num=%x ", my_qp, ibqp->qp_num);
  893. return -ENOMEM;
  894. }
  895. h_ret = hipz_h_query_qp(shca->ipz_hca_handle,
  896. my_qp->ipz_qp_handle,
  897. &my_qp->pf,
  898. mqpcb, my_qp->galpas.kernel);
  899. if (h_ret != H_SUCCESS) {
  900. ehca_err(ibqp->device, "hipz_h_query_qp() failed "
  901. "ehca_qp=%p qp_num=%x h_ret=%li",
  902. my_qp, ibqp->qp_num, h_ret);
  903. ret = ehca2ib_return_code(h_ret);
  904. goto modify_qp_exit1;
  905. }
  906. qp_cur_state = ehca2ib_qp_state(mqpcb->qp_state);
  907. if (qp_cur_state == -EINVAL) { /* invalid qp state */
  908. ret = -EINVAL;
  909. ehca_err(ibqp->device, "Invalid current ehca_qp_state=%x "
  910. "ehca_qp=%p qp_num=%x",
  911. mqpcb->qp_state, my_qp, ibqp->qp_num);
  912. goto modify_qp_exit1;
  913. }
  914. /*
  915. * circumvention to set aqp0 initial state to init
  916. * as expected by IB spec
  917. */
  918. if (smi_reset2init == 0 &&
  919. ibqp->qp_type == IB_QPT_SMI &&
  920. qp_cur_state == IB_QPS_RESET &&
  921. (attr_mask & IB_QP_STATE) &&
  922. attr->qp_state == IB_QPS_INIT) { /* RESET -> INIT */
  923. struct ib_qp_attr smiqp_attr = {
  924. .qp_state = IB_QPS_INIT,
  925. .port_num = my_qp->init_attr.port_num,
  926. .pkey_index = 0,
  927. .qkey = 0
  928. };
  929. int smiqp_attr_mask = IB_QP_STATE | IB_QP_PORT |
  930. IB_QP_PKEY_INDEX | IB_QP_QKEY;
  931. int smirc = internal_modify_qp(
  932. ibqp, &smiqp_attr, smiqp_attr_mask, 1);
  933. if (smirc) {
  934. ehca_err(ibqp->device, "SMI RESET -> INIT failed. "
  935. "ehca_modify_qp() rc=%i", smirc);
  936. ret = H_PARAMETER;
  937. goto modify_qp_exit1;
  938. }
  939. qp_cur_state = IB_QPS_INIT;
  940. ehca_dbg(ibqp->device, "SMI RESET -> INIT succeeded");
  941. }
  942. /* is transmitted current state equal to "real" current state */
  943. if ((attr_mask & IB_QP_CUR_STATE) &&
  944. qp_cur_state != attr->cur_qp_state) {
  945. ret = -EINVAL;
  946. ehca_err(ibqp->device,
  947. "Invalid IB_QP_CUR_STATE attr->curr_qp_state=%x <>"
  948. " actual cur_qp_state=%x. ehca_qp=%p qp_num=%x",
  949. attr->cur_qp_state, qp_cur_state, my_qp, ibqp->qp_num);
  950. goto modify_qp_exit1;
  951. }
  952. ehca_dbg(ibqp->device, "ehca_qp=%p qp_num=%x current qp_state=%x "
  953. "new qp_state=%x attribute_mask=%x",
  954. my_qp, ibqp->qp_num, qp_cur_state, attr->qp_state, attr_mask);
  955. qp_new_state = attr_mask & IB_QP_STATE ? attr->qp_state : qp_cur_state;
  956. if (!smi_reset2init &&
  957. !ib_modify_qp_is_ok(qp_cur_state, qp_new_state, ibqp->qp_type,
  958. attr_mask)) {
  959. ret = -EINVAL;
  960. ehca_err(ibqp->device,
  961. "Invalid qp transition new_state=%x cur_state=%x "
  962. "ehca_qp=%p qp_num=%x attr_mask=%x", qp_new_state,
  963. qp_cur_state, my_qp, ibqp->qp_num, attr_mask);
  964. goto modify_qp_exit1;
  965. }
  966. mqpcb->qp_state = ib2ehca_qp_state(qp_new_state);
  967. if (mqpcb->qp_state)
  968. update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_STATE, 1);
  969. else {
  970. ret = -EINVAL;
  971. ehca_err(ibqp->device, "Invalid new qp state=%x "
  972. "ehca_qp=%p qp_num=%x",
  973. qp_new_state, my_qp, ibqp->qp_num);
  974. goto modify_qp_exit1;
  975. }
  976. /* retrieve state transition struct to get req and opt attrs */
  977. statetrans = get_modqp_statetrans(qp_cur_state, qp_new_state);
  978. if (statetrans < 0) {
  979. ret = -EINVAL;
  980. ehca_err(ibqp->device, "<INVALID STATE CHANGE> qp_cur_state=%x "
  981. "new_qp_state=%x State_xsition=%x ehca_qp=%p "
  982. "qp_num=%x", qp_cur_state, qp_new_state,
  983. statetrans, my_qp, ibqp->qp_num);
  984. goto modify_qp_exit1;
  985. }
  986. qp_attr_idx = ib2ehcaqptype(ibqp->qp_type);
  987. if (qp_attr_idx < 0) {
  988. ret = qp_attr_idx;
  989. ehca_err(ibqp->device,
  990. "Invalid QP type=%x ehca_qp=%p qp_num=%x",
  991. ibqp->qp_type, my_qp, ibqp->qp_num);
  992. goto modify_qp_exit1;
  993. }
  994. ehca_dbg(ibqp->device,
  995. "ehca_qp=%p qp_num=%x <VALID STATE CHANGE> qp_state_xsit=%x",
  996. my_qp, ibqp->qp_num, statetrans);
  997. /* eHCA2 rev2 and higher require the SEND_GRH_FLAG to be set
  998. * in non-LL UD QPs.
  999. */
  1000. if ((my_qp->qp_type == IB_QPT_UD) &&
  1001. (my_qp->ext_type != EQPT_LLQP) &&
  1002. (statetrans == IB_QPST_INIT2RTR) &&
  1003. (shca->hw_level >= 0x22)) {
  1004. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_GRH_FLAG, 1);
  1005. mqpcb->send_grh_flag = 1;
  1006. }
  1007. /* sqe -> rts: set purge bit of bad wqe before actual trans */
  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. /* mark next free wqe if kernel */
  1013. if (!ibqp->uobject) {
  1014. struct ehca_wqe *wqe;
  1015. /* lock send queue */
  1016. spin_lock_irqsave(&my_qp->spinlock_s, flags);
  1017. squeue_locked = 1;
  1018. /* mark next free wqe */
  1019. wqe = (struct ehca_wqe *)
  1020. ipz_qeit_get(&my_qp->ipz_squeue);
  1021. wqe->optype = wqe->wqef = 0xff;
  1022. ehca_dbg(ibqp->device, "qp_num=%x next_free_wqe=%p",
  1023. ibqp->qp_num, wqe);
  1024. }
  1025. ret = prepare_sqe_rts(my_qp, shca, &bad_wqe_cnt);
  1026. if (ret) {
  1027. ehca_err(ibqp->device, "prepare_sqe_rts() failed "
  1028. "ehca_qp=%p qp_num=%x ret=%i",
  1029. my_qp, ibqp->qp_num, ret);
  1030. goto modify_qp_exit2;
  1031. }
  1032. }
  1033. /*
  1034. * enable RDMA_Atomic_Control if reset->init und reliable con
  1035. * this is necessary since gen2 does not provide that flag,
  1036. * but pHyp requires it
  1037. */
  1038. if (statetrans == IB_QPST_RESET2INIT &&
  1039. (ibqp->qp_type == IB_QPT_RC || ibqp->qp_type == IB_QPT_UC)) {
  1040. mqpcb->rdma_atomic_ctrl = 3;
  1041. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RDMA_ATOMIC_CTRL, 1);
  1042. }
  1043. /* circ. pHyp requires #RDMA/Atomic Resp Res for UC INIT -> RTR */
  1044. if (statetrans == IB_QPST_INIT2RTR &&
  1045. (ibqp->qp_type == IB_QPT_UC) &&
  1046. !(attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)) {
  1047. mqpcb->rdma_nr_atomic_resp_res = 1; /* default to 1 */
  1048. update_mask |=
  1049. EHCA_BMASK_SET(MQPCB_MASK_RDMA_NR_ATOMIC_RESP_RES, 1);
  1050. }
  1051. if (attr_mask & IB_QP_PKEY_INDEX) {
  1052. if (attr->pkey_index >= 16) {
  1053. ret = -EINVAL;
  1054. ehca_err(ibqp->device, "Invalid pkey_index=%x. "
  1055. "ehca_qp=%p qp_num=%x max_pkey_index=f",
  1056. attr->pkey_index, my_qp, ibqp->qp_num);
  1057. goto modify_qp_exit2;
  1058. }
  1059. mqpcb->prim_p_key_idx = attr->pkey_index;
  1060. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PRIM_P_KEY_IDX, 1);
  1061. }
  1062. if (attr_mask & IB_QP_PORT) {
  1063. if (attr->port_num < 1 || attr->port_num > shca->num_ports) {
  1064. ret = -EINVAL;
  1065. ehca_err(ibqp->device, "Invalid port=%x. "
  1066. "ehca_qp=%p qp_num=%x num_ports=%x",
  1067. attr->port_num, my_qp, ibqp->qp_num,
  1068. shca->num_ports);
  1069. goto modify_qp_exit2;
  1070. }
  1071. mqpcb->prim_phys_port = attr->port_num;
  1072. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PRIM_PHYS_PORT, 1);
  1073. }
  1074. if (attr_mask & IB_QP_QKEY) {
  1075. mqpcb->qkey = attr->qkey;
  1076. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_QKEY, 1);
  1077. }
  1078. if (attr_mask & IB_QP_AV) {
  1079. int ah_mult = ib_rate_to_mult(attr->ah_attr.static_rate);
  1080. int ehca_mult = ib_rate_to_mult(shca->sport[my_qp->
  1081. init_attr.port_num].rate);
  1082. mqpcb->dlid = attr->ah_attr.dlid;
  1083. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DLID, 1);
  1084. mqpcb->source_path_bits = attr->ah_attr.src_path_bits;
  1085. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SOURCE_PATH_BITS, 1);
  1086. mqpcb->service_level = attr->ah_attr.sl;
  1087. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SERVICE_LEVEL, 1);
  1088. if (ah_mult < ehca_mult)
  1089. mqpcb->max_static_rate = (ah_mult > 0) ?
  1090. ((ehca_mult - 1) / ah_mult) : 0;
  1091. else
  1092. mqpcb->max_static_rate = 0;
  1093. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_MAX_STATIC_RATE, 1);
  1094. /*
  1095. * Always supply the GRH flag, even if it's zero, to give the
  1096. * hypervisor a clear "yes" or "no" instead of a "perhaps"
  1097. */
  1098. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_GRH_FLAG, 1);
  1099. /*
  1100. * only if GRH is TRUE we might consider SOURCE_GID_IDX
  1101. * and DEST_GID otherwise phype will return H_ATTR_PARM!!!
  1102. */
  1103. if (attr->ah_attr.ah_flags == IB_AH_GRH) {
  1104. mqpcb->send_grh_flag = 1;
  1105. mqpcb->source_gid_idx = attr->ah_attr.grh.sgid_index;
  1106. update_mask |=
  1107. EHCA_BMASK_SET(MQPCB_MASK_SOURCE_GID_IDX, 1);
  1108. for (cnt = 0; cnt < 16; cnt++)
  1109. mqpcb->dest_gid.byte[cnt] =
  1110. attr->ah_attr.grh.dgid.raw[cnt];
  1111. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DEST_GID, 1);
  1112. mqpcb->flow_label = attr->ah_attr.grh.flow_label;
  1113. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_FLOW_LABEL, 1);
  1114. mqpcb->hop_limit = attr->ah_attr.grh.hop_limit;
  1115. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_HOP_LIMIT, 1);
  1116. mqpcb->traffic_class = attr->ah_attr.grh.traffic_class;
  1117. update_mask |=
  1118. EHCA_BMASK_SET(MQPCB_MASK_TRAFFIC_CLASS, 1);
  1119. }
  1120. }
  1121. if (attr_mask & IB_QP_PATH_MTU) {
  1122. mqpcb->path_mtu = attr->path_mtu;
  1123. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PATH_MTU, 1);
  1124. }
  1125. if (attr_mask & IB_QP_TIMEOUT) {
  1126. mqpcb->timeout = attr->timeout;
  1127. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_TIMEOUT, 1);
  1128. }
  1129. if (attr_mask & IB_QP_RETRY_CNT) {
  1130. mqpcb->retry_count = attr->retry_cnt;
  1131. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RETRY_COUNT, 1);
  1132. }
  1133. if (attr_mask & IB_QP_RNR_RETRY) {
  1134. mqpcb->rnr_retry_count = attr->rnr_retry;
  1135. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RNR_RETRY_COUNT, 1);
  1136. }
  1137. if (attr_mask & IB_QP_RQ_PSN) {
  1138. mqpcb->receive_psn = attr->rq_psn;
  1139. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RECEIVE_PSN, 1);
  1140. }
  1141. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  1142. mqpcb->rdma_nr_atomic_resp_res = attr->max_dest_rd_atomic < 3 ?
  1143. attr->max_dest_rd_atomic : 2;
  1144. update_mask |=
  1145. EHCA_BMASK_SET(MQPCB_MASK_RDMA_NR_ATOMIC_RESP_RES, 1);
  1146. }
  1147. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  1148. mqpcb->rdma_atomic_outst_dest_qp = attr->max_rd_atomic < 3 ?
  1149. attr->max_rd_atomic : 2;
  1150. update_mask |=
  1151. EHCA_BMASK_SET
  1152. (MQPCB_MASK_RDMA_ATOMIC_OUTST_DEST_QP, 1);
  1153. }
  1154. if (attr_mask & IB_QP_ALT_PATH) {
  1155. int ah_mult = ib_rate_to_mult(attr->alt_ah_attr.static_rate);
  1156. int ehca_mult = ib_rate_to_mult(
  1157. shca->sport[my_qp->init_attr.port_num].rate);
  1158. if (attr->alt_port_num < 1
  1159. || attr->alt_port_num > shca->num_ports) {
  1160. ret = -EINVAL;
  1161. ehca_err(ibqp->device, "Invalid alt_port=%x. "
  1162. "ehca_qp=%p qp_num=%x num_ports=%x",
  1163. attr->alt_port_num, my_qp, ibqp->qp_num,
  1164. shca->num_ports);
  1165. goto modify_qp_exit2;
  1166. }
  1167. mqpcb->alt_phys_port = attr->alt_port_num;
  1168. if (attr->alt_pkey_index >= 16) {
  1169. ret = -EINVAL;
  1170. ehca_err(ibqp->device, "Invalid alt_pkey_index=%x. "
  1171. "ehca_qp=%p qp_num=%x max_pkey_index=f",
  1172. attr->pkey_index, my_qp, ibqp->qp_num);
  1173. goto modify_qp_exit2;
  1174. }
  1175. mqpcb->alt_p_key_idx = attr->alt_pkey_index;
  1176. mqpcb->timeout_al = attr->alt_timeout;
  1177. mqpcb->dlid_al = attr->alt_ah_attr.dlid;
  1178. mqpcb->source_path_bits_al = attr->alt_ah_attr.src_path_bits;
  1179. mqpcb->service_level_al = attr->alt_ah_attr.sl;
  1180. if (ah_mult > 0 && ah_mult < ehca_mult)
  1181. mqpcb->max_static_rate_al = (ehca_mult - 1) / ah_mult;
  1182. else
  1183. mqpcb->max_static_rate_al = 0;
  1184. /* OpenIB doesn't support alternate retry counts - copy them */
  1185. mqpcb->retry_count_al = mqpcb->retry_count;
  1186. mqpcb->rnr_retry_count_al = mqpcb->rnr_retry_count;
  1187. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_ALT_PHYS_PORT, 1)
  1188. | EHCA_BMASK_SET(MQPCB_MASK_ALT_P_KEY_IDX, 1)
  1189. | EHCA_BMASK_SET(MQPCB_MASK_TIMEOUT_AL, 1)
  1190. | EHCA_BMASK_SET(MQPCB_MASK_DLID_AL, 1)
  1191. | EHCA_BMASK_SET(MQPCB_MASK_SOURCE_PATH_BITS_AL, 1)
  1192. | EHCA_BMASK_SET(MQPCB_MASK_SERVICE_LEVEL_AL, 1)
  1193. | EHCA_BMASK_SET(MQPCB_MASK_MAX_STATIC_RATE_AL, 1)
  1194. | EHCA_BMASK_SET(MQPCB_MASK_RETRY_COUNT_AL, 1)
  1195. | EHCA_BMASK_SET(MQPCB_MASK_RNR_RETRY_COUNT_AL, 1);
  1196. /*
  1197. * Always supply the GRH flag, even if it's zero, to give the
  1198. * hypervisor a clear "yes" or "no" instead of a "perhaps"
  1199. */
  1200. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_GRH_FLAG_AL, 1);
  1201. /*
  1202. * only if GRH is TRUE we might consider SOURCE_GID_IDX
  1203. * and DEST_GID otherwise phype will return H_ATTR_PARM!!!
  1204. */
  1205. if (attr->alt_ah_attr.ah_flags == IB_AH_GRH) {
  1206. mqpcb->send_grh_flag_al = 1;
  1207. for (cnt = 0; cnt < 16; cnt++)
  1208. mqpcb->dest_gid_al.byte[cnt] =
  1209. attr->alt_ah_attr.grh.dgid.raw[cnt];
  1210. mqpcb->source_gid_idx_al =
  1211. attr->alt_ah_attr.grh.sgid_index;
  1212. mqpcb->flow_label_al = attr->alt_ah_attr.grh.flow_label;
  1213. mqpcb->hop_limit_al = attr->alt_ah_attr.grh.hop_limit;
  1214. mqpcb->traffic_class_al =
  1215. attr->alt_ah_attr.grh.traffic_class;
  1216. update_mask |=
  1217. EHCA_BMASK_SET(MQPCB_MASK_SOURCE_GID_IDX_AL, 1)
  1218. | EHCA_BMASK_SET(MQPCB_MASK_DEST_GID_AL, 1)
  1219. | EHCA_BMASK_SET(MQPCB_MASK_FLOW_LABEL_AL, 1)
  1220. | EHCA_BMASK_SET(MQPCB_MASK_HOP_LIMIT_AL, 1) |
  1221. EHCA_BMASK_SET(MQPCB_MASK_TRAFFIC_CLASS_AL, 1);
  1222. }
  1223. }
  1224. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  1225. mqpcb->min_rnr_nak_timer_field = attr->min_rnr_timer;
  1226. update_mask |=
  1227. EHCA_BMASK_SET(MQPCB_MASK_MIN_RNR_NAK_TIMER_FIELD, 1);
  1228. }
  1229. if (attr_mask & IB_QP_SQ_PSN) {
  1230. mqpcb->send_psn = attr->sq_psn;
  1231. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_PSN, 1);
  1232. }
  1233. if (attr_mask & IB_QP_DEST_QPN) {
  1234. mqpcb->dest_qp_nr = attr->dest_qp_num;
  1235. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DEST_QP_NR, 1);
  1236. }
  1237. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1238. if (attr->path_mig_state != IB_MIG_REARM
  1239. && attr->path_mig_state != IB_MIG_MIGRATED) {
  1240. ret = -EINVAL;
  1241. ehca_err(ibqp->device, "Invalid mig_state=%x",
  1242. attr->path_mig_state);
  1243. goto modify_qp_exit2;
  1244. }
  1245. mqpcb->path_migration_state = attr->path_mig_state + 1;
  1246. update_mask |=
  1247. EHCA_BMASK_SET(MQPCB_MASK_PATH_MIGRATION_STATE, 1);
  1248. }
  1249. if (attr_mask & IB_QP_CAP) {
  1250. mqpcb->max_nr_outst_send_wr = attr->cap.max_send_wr+1;
  1251. update_mask |=
  1252. EHCA_BMASK_SET(MQPCB_MASK_MAX_NR_OUTST_SEND_WR, 1);
  1253. mqpcb->max_nr_outst_recv_wr = attr->cap.max_recv_wr+1;
  1254. update_mask |=
  1255. EHCA_BMASK_SET(MQPCB_MASK_MAX_NR_OUTST_RECV_WR, 1);
  1256. /* no support for max_send/recv_sge yet */
  1257. }
  1258. if (ehca_debug_level)
  1259. ehca_dmp(mqpcb, 4*70, "qp_num=%x", ibqp->qp_num);
  1260. h_ret = hipz_h_modify_qp(shca->ipz_hca_handle,
  1261. my_qp->ipz_qp_handle,
  1262. &my_qp->pf,
  1263. update_mask,
  1264. mqpcb, my_qp->galpas.kernel);
  1265. if (h_ret != H_SUCCESS) {
  1266. ret = ehca2ib_return_code(h_ret);
  1267. ehca_err(ibqp->device, "hipz_h_modify_qp() failed h_ret=%li "
  1268. "ehca_qp=%p qp_num=%x", h_ret, my_qp, ibqp->qp_num);
  1269. goto modify_qp_exit2;
  1270. }
  1271. if ((my_qp->qp_type == IB_QPT_UD ||
  1272. my_qp->qp_type == IB_QPT_GSI ||
  1273. my_qp->qp_type == IB_QPT_SMI) &&
  1274. statetrans == IB_QPST_SQE2RTS) {
  1275. /* doorbell to reprocessing wqes */
  1276. iosync(); /* serialize GAL register access */
  1277. hipz_update_sqa(my_qp, bad_wqe_cnt-1);
  1278. ehca_gen_dbg("doorbell for %x wqes", bad_wqe_cnt);
  1279. }
  1280. if (statetrans == IB_QPST_RESET2INIT ||
  1281. statetrans == IB_QPST_INIT2INIT) {
  1282. mqpcb->qp_enable = 1;
  1283. mqpcb->qp_state = EHCA_QPS_INIT;
  1284. update_mask = 0;
  1285. update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_ENABLE, 1);
  1286. h_ret = hipz_h_modify_qp(shca->ipz_hca_handle,
  1287. my_qp->ipz_qp_handle,
  1288. &my_qp->pf,
  1289. update_mask,
  1290. mqpcb,
  1291. my_qp->galpas.kernel);
  1292. if (h_ret != H_SUCCESS) {
  1293. ret = ehca2ib_return_code(h_ret);
  1294. ehca_err(ibqp->device, "ENABLE in context of "
  1295. "RESET_2_INIT failed! Maybe you didn't get "
  1296. "a LID h_ret=%li ehca_qp=%p qp_num=%x",
  1297. h_ret, my_qp, ibqp->qp_num);
  1298. goto modify_qp_exit2;
  1299. }
  1300. }
  1301. if (statetrans == IB_QPST_ANY2RESET) {
  1302. ipz_qeit_reset(&my_qp->ipz_rqueue);
  1303. ipz_qeit_reset(&my_qp->ipz_squeue);
  1304. }
  1305. if (attr_mask & IB_QP_QKEY)
  1306. my_qp->qkey = attr->qkey;
  1307. modify_qp_exit2:
  1308. if (squeue_locked) { /* this means: sqe -> rts */
  1309. spin_unlock_irqrestore(&my_qp->spinlock_s, flags);
  1310. my_qp->sqerr_purgeflag = 1;
  1311. }
  1312. modify_qp_exit1:
  1313. ehca_free_fw_ctrlblock(mqpcb);
  1314. return ret;
  1315. }
  1316. int ehca_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int attr_mask,
  1317. struct ib_udata *udata)
  1318. {
  1319. struct ehca_qp *my_qp = container_of(ibqp, struct ehca_qp, ib_qp);
  1320. struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd,
  1321. ib_pd);
  1322. u32 cur_pid = current->tgid;
  1323. if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
  1324. my_pd->ownpid != cur_pid) {
  1325. ehca_err(ibqp->pd->device, "Invalid caller pid=%x ownpid=%x",
  1326. cur_pid, my_pd->ownpid);
  1327. return -EINVAL;
  1328. }
  1329. return internal_modify_qp(ibqp, attr, attr_mask, 0);
  1330. }
  1331. int ehca_query_qp(struct ib_qp *qp,
  1332. struct ib_qp_attr *qp_attr,
  1333. int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
  1334. {
  1335. struct ehca_qp *my_qp = container_of(qp, struct ehca_qp, ib_qp);
  1336. struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd,
  1337. ib_pd);
  1338. struct ehca_shca *shca = container_of(qp->device, struct ehca_shca,
  1339. ib_device);
  1340. struct ipz_adapter_handle adapter_handle = shca->ipz_hca_handle;
  1341. struct hcp_modify_qp_control_block *qpcb;
  1342. u32 cur_pid = current->tgid;
  1343. int cnt, ret = 0;
  1344. u64 h_ret;
  1345. if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
  1346. my_pd->ownpid != cur_pid) {
  1347. ehca_err(qp->device, "Invalid caller pid=%x ownpid=%x",
  1348. cur_pid, my_pd->ownpid);
  1349. return -EINVAL;
  1350. }
  1351. if (qp_attr_mask & QP_ATTR_QUERY_NOT_SUPPORTED) {
  1352. ehca_err(qp->device, "Invalid attribute mask "
  1353. "ehca_qp=%p qp_num=%x qp_attr_mask=%x ",
  1354. my_qp, qp->qp_num, qp_attr_mask);
  1355. return -EINVAL;
  1356. }
  1357. qpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  1358. if (!qpcb) {
  1359. ehca_err(qp->device, "Out of memory for qpcb "
  1360. "ehca_qp=%p qp_num=%x", my_qp, qp->qp_num);
  1361. return -ENOMEM;
  1362. }
  1363. h_ret = hipz_h_query_qp(adapter_handle,
  1364. my_qp->ipz_qp_handle,
  1365. &my_qp->pf,
  1366. qpcb, my_qp->galpas.kernel);
  1367. if (h_ret != H_SUCCESS) {
  1368. ret = ehca2ib_return_code(h_ret);
  1369. ehca_err(qp->device, "hipz_h_query_qp() failed "
  1370. "ehca_qp=%p qp_num=%x h_ret=%li",
  1371. my_qp, qp->qp_num, h_ret);
  1372. goto query_qp_exit1;
  1373. }
  1374. qp_attr->cur_qp_state = ehca2ib_qp_state(qpcb->qp_state);
  1375. qp_attr->qp_state = qp_attr->cur_qp_state;
  1376. if (qp_attr->cur_qp_state == -EINVAL) {
  1377. ret = -EINVAL;
  1378. ehca_err(qp->device, "Got invalid ehca_qp_state=%x "
  1379. "ehca_qp=%p qp_num=%x",
  1380. qpcb->qp_state, my_qp, qp->qp_num);
  1381. goto query_qp_exit1;
  1382. }
  1383. if (qp_attr->qp_state == IB_QPS_SQD)
  1384. qp_attr->sq_draining = 1;
  1385. qp_attr->qkey = qpcb->qkey;
  1386. qp_attr->path_mtu = qpcb->path_mtu;
  1387. qp_attr->path_mig_state = qpcb->path_migration_state - 1;
  1388. qp_attr->rq_psn = qpcb->receive_psn;
  1389. qp_attr->sq_psn = qpcb->send_psn;
  1390. qp_attr->min_rnr_timer = qpcb->min_rnr_nak_timer_field;
  1391. qp_attr->cap.max_send_wr = qpcb->max_nr_outst_send_wr-1;
  1392. qp_attr->cap.max_recv_wr = qpcb->max_nr_outst_recv_wr-1;
  1393. /* UD_AV CIRCUMVENTION */
  1394. if (my_qp->qp_type == IB_QPT_UD) {
  1395. qp_attr->cap.max_send_sge =
  1396. qpcb->actual_nr_sges_in_sq_wqe - 2;
  1397. qp_attr->cap.max_recv_sge =
  1398. qpcb->actual_nr_sges_in_rq_wqe - 2;
  1399. } else {
  1400. qp_attr->cap.max_send_sge =
  1401. qpcb->actual_nr_sges_in_sq_wqe;
  1402. qp_attr->cap.max_recv_sge =
  1403. qpcb->actual_nr_sges_in_rq_wqe;
  1404. }
  1405. qp_attr->cap.max_inline_data = my_qp->sq_max_inline_data_size;
  1406. qp_attr->dest_qp_num = qpcb->dest_qp_nr;
  1407. qp_attr->pkey_index =
  1408. EHCA_BMASK_GET(MQPCB_PRIM_P_KEY_IDX, qpcb->prim_p_key_idx);
  1409. qp_attr->port_num =
  1410. EHCA_BMASK_GET(MQPCB_PRIM_PHYS_PORT, qpcb->prim_phys_port);
  1411. qp_attr->timeout = qpcb->timeout;
  1412. qp_attr->retry_cnt = qpcb->retry_count;
  1413. qp_attr->rnr_retry = qpcb->rnr_retry_count;
  1414. qp_attr->alt_pkey_index =
  1415. EHCA_BMASK_GET(MQPCB_PRIM_P_KEY_IDX, qpcb->alt_p_key_idx);
  1416. qp_attr->alt_port_num = qpcb->alt_phys_port;
  1417. qp_attr->alt_timeout = qpcb->timeout_al;
  1418. qp_attr->max_dest_rd_atomic = qpcb->rdma_nr_atomic_resp_res;
  1419. qp_attr->max_rd_atomic = qpcb->rdma_atomic_outst_dest_qp;
  1420. /* primary av */
  1421. qp_attr->ah_attr.sl = qpcb->service_level;
  1422. if (qpcb->send_grh_flag) {
  1423. qp_attr->ah_attr.ah_flags = IB_AH_GRH;
  1424. }
  1425. qp_attr->ah_attr.static_rate = qpcb->max_static_rate;
  1426. qp_attr->ah_attr.dlid = qpcb->dlid;
  1427. qp_attr->ah_attr.src_path_bits = qpcb->source_path_bits;
  1428. qp_attr->ah_attr.port_num = qp_attr->port_num;
  1429. /* primary GRH */
  1430. qp_attr->ah_attr.grh.traffic_class = qpcb->traffic_class;
  1431. qp_attr->ah_attr.grh.hop_limit = qpcb->hop_limit;
  1432. qp_attr->ah_attr.grh.sgid_index = qpcb->source_gid_idx;
  1433. qp_attr->ah_attr.grh.flow_label = qpcb->flow_label;
  1434. for (cnt = 0; cnt < 16; cnt++)
  1435. qp_attr->ah_attr.grh.dgid.raw[cnt] =
  1436. qpcb->dest_gid.byte[cnt];
  1437. /* alternate AV */
  1438. qp_attr->alt_ah_attr.sl = qpcb->service_level_al;
  1439. if (qpcb->send_grh_flag_al) {
  1440. qp_attr->alt_ah_attr.ah_flags = IB_AH_GRH;
  1441. }
  1442. qp_attr->alt_ah_attr.static_rate = qpcb->max_static_rate_al;
  1443. qp_attr->alt_ah_attr.dlid = qpcb->dlid_al;
  1444. qp_attr->alt_ah_attr.src_path_bits = qpcb->source_path_bits_al;
  1445. /* alternate GRH */
  1446. qp_attr->alt_ah_attr.grh.traffic_class = qpcb->traffic_class_al;
  1447. qp_attr->alt_ah_attr.grh.hop_limit = qpcb->hop_limit_al;
  1448. qp_attr->alt_ah_attr.grh.sgid_index = qpcb->source_gid_idx_al;
  1449. qp_attr->alt_ah_attr.grh.flow_label = qpcb->flow_label_al;
  1450. for (cnt = 0; cnt < 16; cnt++)
  1451. qp_attr->alt_ah_attr.grh.dgid.raw[cnt] =
  1452. qpcb->dest_gid_al.byte[cnt];
  1453. /* return init attributes given in ehca_create_qp */
  1454. if (qp_init_attr)
  1455. *qp_init_attr = my_qp->init_attr;
  1456. if (ehca_debug_level)
  1457. ehca_dmp(qpcb, 4*70, "qp_num=%x", qp->qp_num);
  1458. query_qp_exit1:
  1459. ehca_free_fw_ctrlblock(qpcb);
  1460. return ret;
  1461. }
  1462. int ehca_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  1463. enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
  1464. {
  1465. struct ehca_qp *my_qp =
  1466. container_of(ibsrq, struct ehca_qp, ib_srq);
  1467. struct ehca_pd *my_pd =
  1468. container_of(ibsrq->pd, struct ehca_pd, ib_pd);
  1469. struct ehca_shca *shca =
  1470. container_of(ibsrq->pd->device, struct ehca_shca, ib_device);
  1471. struct hcp_modify_qp_control_block *mqpcb;
  1472. u64 update_mask;
  1473. u64 h_ret;
  1474. int ret = 0;
  1475. u32 cur_pid = current->tgid;
  1476. if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
  1477. my_pd->ownpid != cur_pid) {
  1478. ehca_err(ibsrq->pd->device, "Invalid caller pid=%x ownpid=%x",
  1479. cur_pid, my_pd->ownpid);
  1480. return -EINVAL;
  1481. }
  1482. mqpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  1483. if (!mqpcb) {
  1484. ehca_err(ibsrq->device, "Could not get zeroed page for mqpcb "
  1485. "ehca_qp=%p qp_num=%x ", my_qp, my_qp->real_qp_num);
  1486. return -ENOMEM;
  1487. }
  1488. update_mask = 0;
  1489. if (attr_mask & IB_SRQ_LIMIT) {
  1490. attr_mask &= ~IB_SRQ_LIMIT;
  1491. update_mask |=
  1492. EHCA_BMASK_SET(MQPCB_MASK_CURR_SRQ_LIMIT, 1)
  1493. | EHCA_BMASK_SET(MQPCB_MASK_QP_AFF_ASYN_EV_LOG_REG, 1);
  1494. mqpcb->curr_srq_limit =
  1495. EHCA_BMASK_SET(MQPCB_CURR_SRQ_LIMIT, attr->srq_limit);
  1496. mqpcb->qp_aff_asyn_ev_log_reg =
  1497. EHCA_BMASK_SET(QPX_AAELOG_RESET_SRQ_LIMIT, 1);
  1498. }
  1499. /* by now, all bits in attr_mask should have been cleared */
  1500. if (attr_mask) {
  1501. ehca_err(ibsrq->device, "invalid attribute mask bits set "
  1502. "attr_mask=%x", attr_mask);
  1503. ret = -EINVAL;
  1504. goto modify_srq_exit0;
  1505. }
  1506. if (ehca_debug_level)
  1507. ehca_dmp(mqpcb, 4*70, "qp_num=%x", my_qp->real_qp_num);
  1508. h_ret = hipz_h_modify_qp(shca->ipz_hca_handle, my_qp->ipz_qp_handle,
  1509. NULL, update_mask, mqpcb,
  1510. my_qp->galpas.kernel);
  1511. if (h_ret != H_SUCCESS) {
  1512. ret = ehca2ib_return_code(h_ret);
  1513. ehca_err(ibsrq->device, "hipz_h_modify_qp() failed h_ret=%li "
  1514. "ehca_qp=%p qp_num=%x",
  1515. h_ret, my_qp, my_qp->real_qp_num);
  1516. }
  1517. modify_srq_exit0:
  1518. ehca_free_fw_ctrlblock(mqpcb);
  1519. return ret;
  1520. }
  1521. int ehca_query_srq(struct ib_srq *srq, struct ib_srq_attr *srq_attr)
  1522. {
  1523. struct ehca_qp *my_qp = container_of(srq, struct ehca_qp, ib_srq);
  1524. struct ehca_pd *my_pd = container_of(srq->pd, struct ehca_pd, ib_pd);
  1525. struct ehca_shca *shca = container_of(srq->device, struct ehca_shca,
  1526. ib_device);
  1527. struct ipz_adapter_handle adapter_handle = shca->ipz_hca_handle;
  1528. struct hcp_modify_qp_control_block *qpcb;
  1529. u32 cur_pid = current->tgid;
  1530. int ret = 0;
  1531. u64 h_ret;
  1532. if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
  1533. my_pd->ownpid != cur_pid) {
  1534. ehca_err(srq->device, "Invalid caller pid=%x ownpid=%x",
  1535. cur_pid, my_pd->ownpid);
  1536. return -EINVAL;
  1537. }
  1538. qpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  1539. if (!qpcb) {
  1540. ehca_err(srq->device, "Out of memory for qpcb "
  1541. "ehca_qp=%p qp_num=%x", my_qp, my_qp->real_qp_num);
  1542. return -ENOMEM;
  1543. }
  1544. h_ret = hipz_h_query_qp(adapter_handle, my_qp->ipz_qp_handle,
  1545. NULL, qpcb, my_qp->galpas.kernel);
  1546. if (h_ret != H_SUCCESS) {
  1547. ret = ehca2ib_return_code(h_ret);
  1548. ehca_err(srq->device, "hipz_h_query_qp() failed "
  1549. "ehca_qp=%p qp_num=%x h_ret=%li",
  1550. my_qp, my_qp->real_qp_num, h_ret);
  1551. goto query_srq_exit1;
  1552. }
  1553. srq_attr->max_wr = qpcb->max_nr_outst_recv_wr - 1;
  1554. srq_attr->max_sge = qpcb->actual_nr_sges_in_rq_wqe;
  1555. srq_attr->srq_limit = EHCA_BMASK_GET(
  1556. MQPCB_CURR_SRQ_LIMIT, qpcb->curr_srq_limit);
  1557. if (ehca_debug_level)
  1558. ehca_dmp(qpcb, 4*70, "qp_num=%x", my_qp->real_qp_num);
  1559. query_srq_exit1:
  1560. ehca_free_fw_ctrlblock(qpcb);
  1561. return ret;
  1562. }
  1563. static int internal_destroy_qp(struct ib_device *dev, struct ehca_qp *my_qp,
  1564. struct ib_uobject *uobject)
  1565. {
  1566. struct ehca_shca *shca = container_of(dev, struct ehca_shca, ib_device);
  1567. struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd,
  1568. ib_pd);
  1569. u32 cur_pid = current->tgid;
  1570. u32 qp_num = my_qp->real_qp_num;
  1571. int ret;
  1572. u64 h_ret;
  1573. u8 port_num;
  1574. enum ib_qp_type qp_type;
  1575. unsigned long flags;
  1576. if (uobject) {
  1577. if (my_qp->mm_count_galpa ||
  1578. my_qp->mm_count_rqueue || my_qp->mm_count_squeue) {
  1579. ehca_err(dev, "Resources still referenced in "
  1580. "user space qp_num=%x", qp_num);
  1581. return -EINVAL;
  1582. }
  1583. if (my_pd->ownpid != cur_pid) {
  1584. ehca_err(dev, "Invalid caller pid=%x ownpid=%x",
  1585. cur_pid, my_pd->ownpid);
  1586. return -EINVAL;
  1587. }
  1588. }
  1589. if (my_qp->send_cq) {
  1590. ret = ehca_cq_unassign_qp(my_qp->send_cq, qp_num);
  1591. if (ret) {
  1592. ehca_err(dev, "Couldn't unassign qp from "
  1593. "send_cq ret=%i qp_num=%x cq_num=%x", ret,
  1594. qp_num, my_qp->send_cq->cq_number);
  1595. return ret;
  1596. }
  1597. }
  1598. write_lock_irqsave(&ehca_qp_idr_lock, flags);
  1599. idr_remove(&ehca_qp_idr, my_qp->token);
  1600. write_unlock_irqrestore(&ehca_qp_idr_lock, flags);
  1601. h_ret = hipz_h_destroy_qp(shca->ipz_hca_handle, my_qp);
  1602. if (h_ret != H_SUCCESS) {
  1603. ehca_err(dev, "hipz_h_destroy_qp() failed h_ret=%li "
  1604. "ehca_qp=%p qp_num=%x", h_ret, my_qp, qp_num);
  1605. return ehca2ib_return_code(h_ret);
  1606. }
  1607. port_num = my_qp->init_attr.port_num;
  1608. qp_type = my_qp->init_attr.qp_type;
  1609. /* no support for IB_QPT_SMI yet */
  1610. if (qp_type == IB_QPT_GSI) {
  1611. struct ib_event event;
  1612. ehca_info(dev, "device %s: port %x is inactive.",
  1613. shca->ib_device.name, port_num);
  1614. event.device = &shca->ib_device;
  1615. event.event = IB_EVENT_PORT_ERR;
  1616. event.element.port_num = port_num;
  1617. shca->sport[port_num - 1].port_state = IB_PORT_DOWN;
  1618. ib_dispatch_event(&event);
  1619. }
  1620. if (HAS_RQ(my_qp))
  1621. ipz_queue_dtor(my_pd, &my_qp->ipz_rqueue);
  1622. if (HAS_SQ(my_qp))
  1623. ipz_queue_dtor(my_pd, &my_qp->ipz_squeue);
  1624. kmem_cache_free(qp_cache, my_qp);
  1625. return 0;
  1626. }
  1627. int ehca_destroy_qp(struct ib_qp *qp)
  1628. {
  1629. return internal_destroy_qp(qp->device,
  1630. container_of(qp, struct ehca_qp, ib_qp),
  1631. qp->uobject);
  1632. }
  1633. int ehca_destroy_srq(struct ib_srq *srq)
  1634. {
  1635. return internal_destroy_qp(srq->device,
  1636. container_of(srq, struct ehca_qp, ib_srq),
  1637. srq->uobject);
  1638. }
  1639. int ehca_init_qp_cache(void)
  1640. {
  1641. qp_cache = kmem_cache_create("ehca_cache_qp",
  1642. sizeof(struct ehca_qp), 0,
  1643. SLAB_HWCACHE_ALIGN,
  1644. NULL);
  1645. if (!qp_cache)
  1646. return -ENOMEM;
  1647. return 0;
  1648. }
  1649. void ehca_cleanup_qp_cache(void)
  1650. {
  1651. if (qp_cache)
  1652. kmem_cache_destroy(qp_cache);
  1653. }