ehca_qp.c 49 KB

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