ehca_qp.c 41 KB

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