ehca_qp.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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_zalloc(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 (&parms, 0, sizeof(struct ehca_alloc_qp_parms));
  427. spin_lock_init(&my_qp->spinlock_s);
  428. spin_lock_init(&my_qp->spinlock_r);
  429. my_qp->recv_cq =
  430. container_of(init_attr->recv_cq, struct ehca_cq, ib_cq);
  431. my_qp->send_cq =
  432. container_of(init_attr->send_cq, struct ehca_cq, ib_cq);
  433. my_qp->init_attr = *init_attr;
  434. do {
  435. if (!idr_pre_get(&ehca_qp_idr, GFP_KERNEL)) {
  436. ret = -ENOMEM;
  437. ehca_err(pd->device, "Can't reserve idr resources.");
  438. goto create_qp_exit0;
  439. }
  440. spin_lock_irqsave(&ehca_qp_idr_lock, flags);
  441. ret = idr_get_new(&ehca_qp_idr, my_qp, &my_qp->token);
  442. spin_unlock_irqrestore(&ehca_qp_idr_lock, flags);
  443. } while (ret == -EAGAIN);
  444. if (ret) {
  445. ret = -ENOMEM;
  446. ehca_err(pd->device, "Can't allocate new idr entry.");
  447. goto create_qp_exit0;
  448. }
  449. parms.servicetype = ibqptype2servicetype(init_attr->qp_type);
  450. if (parms.servicetype < 0) {
  451. ret = -EINVAL;
  452. ehca_err(pd->device, "Invalid qp_type=%x", init_attr->qp_type);
  453. goto create_qp_exit0;
  454. }
  455. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  456. parms.sigtype = HCALL_SIGT_EVERY;
  457. else
  458. parms.sigtype = HCALL_SIGT_BY_WQE;
  459. /* UD_AV CIRCUMVENTION */
  460. max_send_sge = init_attr->cap.max_send_sge;
  461. max_recv_sge = init_attr->cap.max_recv_sge;
  462. if (IB_QPT_UD == init_attr->qp_type ||
  463. IB_QPT_GSI == init_attr->qp_type ||
  464. IB_QPT_SMI == init_attr->qp_type) {
  465. max_send_sge += 2;
  466. max_recv_sge += 2;
  467. }
  468. parms.ipz_eq_handle = shca->eq.ipz_eq_handle;
  469. parms.daqp_ctrl = isdaqp | daqp_completion;
  470. parms.pd = my_pd->fw_pd;
  471. parms.max_recv_sge = max_recv_sge;
  472. parms.max_send_sge = max_send_sge;
  473. h_ret = hipz_h_alloc_resource_qp(shca->ipz_hca_handle, my_qp, &parms);
  474. if (h_ret != H_SUCCESS) {
  475. ehca_err(pd->device, "h_alloc_resource_qp() failed h_ret=%lx",
  476. h_ret);
  477. ret = ehca2ib_return_code(h_ret);
  478. goto create_qp_exit1;
  479. }
  480. my_qp->ib_qp.qp_num = my_qp->real_qp_num;
  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->ib_qp.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_type = init_attr->qp_type;
  546. my_qp->qp_type = init_attr->qp_type;
  547. my_qp->ib_qp.srq = init_attr->srq;
  548. my_qp->ib_qp.qp_context = init_attr->qp_context;
  549. my_qp->ib_qp.event_handler = init_attr->event_handler;
  550. init_attr->cap.max_inline_data = 0; /* not supported yet */
  551. init_attr->cap.max_recv_sge = parms.act_nr_recv_sges;
  552. init_attr->cap.max_recv_wr = parms.act_nr_recv_wqes;
  553. init_attr->cap.max_send_sge = parms.act_nr_send_sges;
  554. init_attr->cap.max_send_wr = parms.act_nr_send_wqes;
  555. /* NOTE: define_apq0() not supported yet */
  556. if (init_attr->qp_type == IB_QPT_GSI) {
  557. h_ret = ehca_define_sqp(shca, my_qp, init_attr);
  558. if (h_ret != H_SUCCESS) {
  559. ehca_err(pd->device, "ehca_define_sqp() failed rc=%lx",
  560. h_ret);
  561. ret = ehca2ib_return_code(h_ret);
  562. goto create_qp_exit3;
  563. }
  564. }
  565. if (init_attr->send_cq) {
  566. struct ehca_cq *cq = container_of(init_attr->send_cq,
  567. struct ehca_cq, ib_cq);
  568. ret = ehca_cq_assign_qp(cq, my_qp);
  569. if (ret) {
  570. ehca_err(pd->device, "Couldn't assign qp to send_cq ret=%x",
  571. ret);
  572. goto create_qp_exit3;
  573. }
  574. my_qp->send_cq = cq;
  575. }
  576. /* copy queues, galpa data to user space */
  577. if (context && udata) {
  578. struct ipz_queue *ipz_rqueue = &my_qp->ipz_rqueue;
  579. struct ipz_queue *ipz_squeue = &my_qp->ipz_squeue;
  580. struct ehca_create_qp_resp resp;
  581. memset(&resp, 0, sizeof(resp));
  582. resp.qp_num = my_qp->real_qp_num;
  583. resp.token = my_qp->token;
  584. resp.qp_type = my_qp->qp_type;
  585. resp.qkey = my_qp->qkey;
  586. resp.real_qp_num = my_qp->real_qp_num;
  587. /* rqueue properties */
  588. resp.ipz_rqueue.qe_size = ipz_rqueue->qe_size;
  589. resp.ipz_rqueue.act_nr_of_sg = ipz_rqueue->act_nr_of_sg;
  590. resp.ipz_rqueue.queue_length = ipz_rqueue->queue_length;
  591. resp.ipz_rqueue.pagesize = ipz_rqueue->pagesize;
  592. resp.ipz_rqueue.toggle_state = ipz_rqueue->toggle_state;
  593. /* squeue properties */
  594. resp.ipz_squeue.qe_size = ipz_squeue->qe_size;
  595. resp.ipz_squeue.act_nr_of_sg = ipz_squeue->act_nr_of_sg;
  596. resp.ipz_squeue.queue_length = ipz_squeue->queue_length;
  597. resp.ipz_squeue.pagesize = ipz_squeue->pagesize;
  598. resp.ipz_squeue.toggle_state = ipz_squeue->toggle_state;
  599. if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
  600. ehca_err(pd->device, "Copy to udata failed");
  601. ret = -EINVAL;
  602. goto create_qp_exit3;
  603. }
  604. }
  605. return &my_qp->ib_qp;
  606. create_qp_exit3:
  607. ipz_queue_dtor(&my_qp->ipz_rqueue);
  608. ipz_queue_dtor(&my_qp->ipz_squeue);
  609. create_qp_exit2:
  610. hipz_h_destroy_qp(shca->ipz_hca_handle, my_qp);
  611. create_qp_exit1:
  612. spin_lock_irqsave(&ehca_qp_idr_lock, flags);
  613. idr_remove(&ehca_qp_idr, my_qp->token);
  614. spin_unlock_irqrestore(&ehca_qp_idr_lock, flags);
  615. create_qp_exit0:
  616. kmem_cache_free(qp_cache, my_qp);
  617. return ERR_PTR(ret);
  618. }
  619. /*
  620. * prepare_sqe_rts called by internal_modify_qp() at trans sqe -> rts
  621. * set purge bit of bad wqe and subsequent wqes to avoid reentering sqe
  622. * returns total number of bad wqes in bad_wqe_cnt
  623. */
  624. static int prepare_sqe_rts(struct ehca_qp *my_qp, struct ehca_shca *shca,
  625. int *bad_wqe_cnt)
  626. {
  627. u64 h_ret;
  628. struct ipz_queue *squeue;
  629. void *bad_send_wqe_p, *bad_send_wqe_v;
  630. u64 q_ofs;
  631. struct ehca_wqe *wqe;
  632. int qp_num = my_qp->ib_qp.qp_num;
  633. /* get send wqe pointer */
  634. h_ret = hipz_h_disable_and_get_wqe(shca->ipz_hca_handle,
  635. my_qp->ipz_qp_handle, &my_qp->pf,
  636. &bad_send_wqe_p, NULL, 2);
  637. if (h_ret != H_SUCCESS) {
  638. ehca_err(&shca->ib_device, "hipz_h_disable_and_get_wqe() failed"
  639. " ehca_qp=%p qp_num=%x h_ret=%lx",
  640. my_qp, qp_num, h_ret);
  641. return ehca2ib_return_code(h_ret);
  642. }
  643. bad_send_wqe_p = (void*)((u64)bad_send_wqe_p & (~(1L<<63)));
  644. ehca_dbg(&shca->ib_device, "qp_num=%x bad_send_wqe_p=%p",
  645. qp_num, bad_send_wqe_p);
  646. /* convert wqe pointer to vadr */
  647. bad_send_wqe_v = abs_to_virt((u64)bad_send_wqe_p);
  648. if (ehca_debug_level)
  649. ehca_dmp(bad_send_wqe_v, 32, "qp_num=%x bad_wqe", qp_num);
  650. squeue = &my_qp->ipz_squeue;
  651. if (ipz_queue_abs_to_offset(squeue, (u64)bad_send_wqe_p, &q_ofs)) {
  652. ehca_err(&shca->ib_device, "failed to get wqe offset qp_num=%x"
  653. " bad_send_wqe_p=%p", qp_num, bad_send_wqe_p);
  654. return -EFAULT;
  655. }
  656. /* loop sets wqe's purge bit */
  657. wqe = (struct ehca_wqe*)ipz_qeit_calc(squeue, q_ofs);
  658. *bad_wqe_cnt = 0;
  659. while (wqe->optype != 0xff && wqe->wqef != 0xff) {
  660. if (ehca_debug_level)
  661. ehca_dmp(wqe, 32, "qp_num=%x wqe", qp_num);
  662. wqe->nr_of_data_seg = 0; /* suppress data access */
  663. wqe->wqef = WQEF_PURGE; /* WQE to be purged */
  664. q_ofs = ipz_queue_advance_offset(squeue, q_ofs);
  665. wqe = (struct ehca_wqe*)ipz_qeit_calc(squeue, q_ofs);
  666. *bad_wqe_cnt = (*bad_wqe_cnt)+1;
  667. }
  668. /*
  669. * bad wqe will be reprocessed and ignored when pol_cq() is called,
  670. * i.e. nr of wqes with flush error status is one less
  671. */
  672. ehca_dbg(&shca->ib_device, "qp_num=%x flusherr_wqe_cnt=%x",
  673. qp_num, (*bad_wqe_cnt)-1);
  674. wqe->wqef = 0;
  675. return 0;
  676. }
  677. /*
  678. * internal_modify_qp with circumvention to handle aqp0 properly
  679. * smi_reset2init indicates if this is an internal reset-to-init-call for
  680. * smi. This flag must always be zero if called from ehca_modify_qp()!
  681. * This internal func was intorduced to avoid recursion of ehca_modify_qp()!
  682. */
  683. static int internal_modify_qp(struct ib_qp *ibqp,
  684. struct ib_qp_attr *attr,
  685. int attr_mask, int smi_reset2init)
  686. {
  687. enum ib_qp_state qp_cur_state, qp_new_state;
  688. int cnt, qp_attr_idx, ret = 0;
  689. enum ib_qp_statetrans statetrans;
  690. struct hcp_modify_qp_control_block *mqpcb;
  691. struct ehca_qp *my_qp = container_of(ibqp, struct ehca_qp, ib_qp);
  692. struct ehca_shca *shca =
  693. container_of(ibqp->pd->device, struct ehca_shca, ib_device);
  694. u64 update_mask;
  695. u64 h_ret;
  696. int bad_wqe_cnt = 0;
  697. int squeue_locked = 0;
  698. unsigned long spl_flags = 0;
  699. /* do query_qp to obtain current attr values */
  700. mqpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  701. if (!mqpcb) {
  702. ehca_err(ibqp->device, "Could not get zeroed page for mqpcb "
  703. "ehca_qp=%p qp_num=%x ", my_qp, ibqp->qp_num);
  704. return -ENOMEM;
  705. }
  706. h_ret = hipz_h_query_qp(shca->ipz_hca_handle,
  707. my_qp->ipz_qp_handle,
  708. &my_qp->pf,
  709. mqpcb, my_qp->galpas.kernel);
  710. if (h_ret != H_SUCCESS) {
  711. ehca_err(ibqp->device, "hipz_h_query_qp() failed "
  712. "ehca_qp=%p qp_num=%x h_ret=%lx",
  713. my_qp, ibqp->qp_num, h_ret);
  714. ret = ehca2ib_return_code(h_ret);
  715. goto modify_qp_exit1;
  716. }
  717. qp_cur_state = ehca2ib_qp_state(mqpcb->qp_state);
  718. if (qp_cur_state == -EINVAL) { /* invalid qp state */
  719. ret = -EINVAL;
  720. ehca_err(ibqp->device, "Invalid current ehca_qp_state=%x "
  721. "ehca_qp=%p qp_num=%x",
  722. mqpcb->qp_state, my_qp, ibqp->qp_num);
  723. goto modify_qp_exit1;
  724. }
  725. /*
  726. * circumvention to set aqp0 initial state to init
  727. * as expected by IB spec
  728. */
  729. if (smi_reset2init == 0 &&
  730. ibqp->qp_type == IB_QPT_SMI &&
  731. qp_cur_state == IB_QPS_RESET &&
  732. (attr_mask & IB_QP_STATE) &&
  733. attr->qp_state == IB_QPS_INIT) { /* RESET -> INIT */
  734. struct ib_qp_attr smiqp_attr = {
  735. .qp_state = IB_QPS_INIT,
  736. .port_num = my_qp->init_attr.port_num,
  737. .pkey_index = 0,
  738. .qkey = 0
  739. };
  740. int smiqp_attr_mask = IB_QP_STATE | IB_QP_PORT |
  741. IB_QP_PKEY_INDEX | IB_QP_QKEY;
  742. int smirc = internal_modify_qp(
  743. ibqp, &smiqp_attr, smiqp_attr_mask, 1);
  744. if (smirc) {
  745. ehca_err(ibqp->device, "SMI RESET -> INIT failed. "
  746. "ehca_modify_qp() rc=%x", smirc);
  747. ret = H_PARAMETER;
  748. goto modify_qp_exit1;
  749. }
  750. qp_cur_state = IB_QPS_INIT;
  751. ehca_dbg(ibqp->device, "SMI RESET -> INIT succeeded");
  752. }
  753. /* is transmitted current state equal to "real" current state */
  754. if ((attr_mask & IB_QP_CUR_STATE) &&
  755. qp_cur_state != attr->cur_qp_state) {
  756. ret = -EINVAL;
  757. ehca_err(ibqp->device,
  758. "Invalid IB_QP_CUR_STATE attr->curr_qp_state=%x <>"
  759. " actual cur_qp_state=%x. ehca_qp=%p qp_num=%x",
  760. attr->cur_qp_state, qp_cur_state, my_qp, ibqp->qp_num);
  761. goto modify_qp_exit1;
  762. }
  763. ehca_dbg(ibqp->device,"ehca_qp=%p qp_num=%x current qp_state=%x "
  764. "new qp_state=%x attribute_mask=%x",
  765. my_qp, ibqp->qp_num, qp_cur_state, attr->qp_state, attr_mask);
  766. qp_new_state = attr_mask & IB_QP_STATE ? attr->qp_state : qp_cur_state;
  767. if (!smi_reset2init &&
  768. !ib_modify_qp_is_ok(qp_cur_state, qp_new_state, ibqp->qp_type,
  769. attr_mask)) {
  770. ret = -EINVAL;
  771. ehca_err(ibqp->device,
  772. "Invalid qp transition new_state=%x cur_state=%x "
  773. "ehca_qp=%p qp_num=%x attr_mask=%x", qp_new_state,
  774. qp_cur_state, my_qp, ibqp->qp_num, attr_mask);
  775. goto modify_qp_exit1;
  776. }
  777. if ((mqpcb->qp_state = ib2ehca_qp_state(qp_new_state)))
  778. update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_STATE, 1);
  779. else {
  780. ret = -EINVAL;
  781. ehca_err(ibqp->device, "Invalid new qp state=%x "
  782. "ehca_qp=%p qp_num=%x",
  783. qp_new_state, my_qp, ibqp->qp_num);
  784. goto modify_qp_exit1;
  785. }
  786. /* retrieve state transition struct to get req and opt attrs */
  787. statetrans = get_modqp_statetrans(qp_cur_state, qp_new_state);
  788. if (statetrans < 0) {
  789. ret = -EINVAL;
  790. ehca_err(ibqp->device, "<INVALID STATE CHANGE> qp_cur_state=%x "
  791. "new_qp_state=%x State_xsition=%x ehca_qp=%p "
  792. "qp_num=%x", qp_cur_state, qp_new_state,
  793. statetrans, my_qp, ibqp->qp_num);
  794. goto modify_qp_exit1;
  795. }
  796. qp_attr_idx = ib2ehcaqptype(ibqp->qp_type);
  797. if (qp_attr_idx < 0) {
  798. ret = qp_attr_idx;
  799. ehca_err(ibqp->device,
  800. "Invalid QP type=%x ehca_qp=%p qp_num=%x",
  801. ibqp->qp_type, my_qp, ibqp->qp_num);
  802. goto modify_qp_exit1;
  803. }
  804. ehca_dbg(ibqp->device,
  805. "ehca_qp=%p qp_num=%x <VALID STATE CHANGE> qp_state_xsit=%x",
  806. my_qp, ibqp->qp_num, statetrans);
  807. /* sqe -> rts: set purge bit of bad wqe before actual trans */
  808. if ((my_qp->qp_type == IB_QPT_UD ||
  809. my_qp->qp_type == IB_QPT_GSI ||
  810. my_qp->qp_type == IB_QPT_SMI) &&
  811. statetrans == IB_QPST_SQE2RTS) {
  812. /* mark next free wqe if kernel */
  813. if (!ibqp->uobject) {
  814. struct ehca_wqe *wqe;
  815. /* lock send queue */
  816. spin_lock_irqsave(&my_qp->spinlock_s, spl_flags);
  817. squeue_locked = 1;
  818. /* mark next free wqe */
  819. wqe = (struct ehca_wqe*)
  820. ipz_qeit_get(&my_qp->ipz_squeue);
  821. wqe->optype = wqe->wqef = 0xff;
  822. ehca_dbg(ibqp->device, "qp_num=%x next_free_wqe=%p",
  823. ibqp->qp_num, wqe);
  824. }
  825. ret = prepare_sqe_rts(my_qp, shca, &bad_wqe_cnt);
  826. if (ret) {
  827. ehca_err(ibqp->device, "prepare_sqe_rts() failed "
  828. "ehca_qp=%p qp_num=%x ret=%x",
  829. my_qp, ibqp->qp_num, ret);
  830. goto modify_qp_exit2;
  831. }
  832. }
  833. /*
  834. * enable RDMA_Atomic_Control if reset->init und reliable con
  835. * this is necessary since gen2 does not provide that flag,
  836. * but pHyp requires it
  837. */
  838. if (statetrans == IB_QPST_RESET2INIT &&
  839. (ibqp->qp_type == IB_QPT_RC || ibqp->qp_type == IB_QPT_UC)) {
  840. mqpcb->rdma_atomic_ctrl = 3;
  841. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RDMA_ATOMIC_CTRL, 1);
  842. }
  843. /* circ. pHyp requires #RDMA/Atomic Resp Res for UC INIT -> RTR */
  844. if (statetrans == IB_QPST_INIT2RTR &&
  845. (ibqp->qp_type == IB_QPT_UC) &&
  846. !(attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)) {
  847. mqpcb->rdma_nr_atomic_resp_res = 1; /* default to 1 */
  848. update_mask |=
  849. EHCA_BMASK_SET(MQPCB_MASK_RDMA_NR_ATOMIC_RESP_RES, 1);
  850. }
  851. if (attr_mask & IB_QP_PKEY_INDEX) {
  852. mqpcb->prim_p_key_idx = attr->pkey_index;
  853. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PRIM_P_KEY_IDX, 1);
  854. }
  855. if (attr_mask & IB_QP_PORT) {
  856. if (attr->port_num < 1 || attr->port_num > shca->num_ports) {
  857. ret = -EINVAL;
  858. ehca_err(ibqp->device, "Invalid port=%x. "
  859. "ehca_qp=%p qp_num=%x num_ports=%x",
  860. attr->port_num, my_qp, ibqp->qp_num,
  861. shca->num_ports);
  862. goto modify_qp_exit2;
  863. }
  864. mqpcb->prim_phys_port = attr->port_num;
  865. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PRIM_PHYS_PORT, 1);
  866. }
  867. if (attr_mask & IB_QP_QKEY) {
  868. mqpcb->qkey = attr->qkey;
  869. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_QKEY, 1);
  870. }
  871. if (attr_mask & IB_QP_AV) {
  872. int ah_mult = ib_rate_to_mult(attr->ah_attr.static_rate);
  873. int ehca_mult = ib_rate_to_mult(shca->sport[my_qp->
  874. init_attr.port_num].rate);
  875. mqpcb->dlid = attr->ah_attr.dlid;
  876. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DLID, 1);
  877. mqpcb->source_path_bits = attr->ah_attr.src_path_bits;
  878. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SOURCE_PATH_BITS, 1);
  879. mqpcb->service_level = attr->ah_attr.sl;
  880. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SERVICE_LEVEL, 1);
  881. if (ah_mult < ehca_mult)
  882. mqpcb->max_static_rate = (ah_mult > 0) ?
  883. ((ehca_mult - 1) / ah_mult) : 0;
  884. else
  885. mqpcb->max_static_rate = 0;
  886. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_MAX_STATIC_RATE, 1);
  887. /*
  888. * Always supply the GRH flag, even if it's zero, to give the
  889. * hypervisor a clear "yes" or "no" instead of a "perhaps"
  890. */
  891. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_GRH_FLAG, 1);
  892. /*
  893. * only if GRH is TRUE we might consider SOURCE_GID_IDX
  894. * and DEST_GID otherwise phype will return H_ATTR_PARM!!!
  895. */
  896. if (attr->ah_attr.ah_flags == IB_AH_GRH) {
  897. mqpcb->send_grh_flag = 1;
  898. mqpcb->source_gid_idx = attr->ah_attr.grh.sgid_index;
  899. update_mask |=
  900. EHCA_BMASK_SET(MQPCB_MASK_SOURCE_GID_IDX, 1);
  901. for (cnt = 0; cnt < 16; cnt++)
  902. mqpcb->dest_gid.byte[cnt] =
  903. attr->ah_attr.grh.dgid.raw[cnt];
  904. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DEST_GID, 1);
  905. mqpcb->flow_label = attr->ah_attr.grh.flow_label;
  906. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_FLOW_LABEL, 1);
  907. mqpcb->hop_limit = attr->ah_attr.grh.hop_limit;
  908. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_HOP_LIMIT, 1);
  909. mqpcb->traffic_class = attr->ah_attr.grh.traffic_class;
  910. update_mask |=
  911. EHCA_BMASK_SET(MQPCB_MASK_TRAFFIC_CLASS, 1);
  912. }
  913. }
  914. if (attr_mask & IB_QP_PATH_MTU) {
  915. mqpcb->path_mtu = attr->path_mtu;
  916. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PATH_MTU, 1);
  917. }
  918. if (attr_mask & IB_QP_TIMEOUT) {
  919. mqpcb->timeout = attr->timeout;
  920. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_TIMEOUT, 1);
  921. }
  922. if (attr_mask & IB_QP_RETRY_CNT) {
  923. mqpcb->retry_count = attr->retry_cnt;
  924. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RETRY_COUNT, 1);
  925. }
  926. if (attr_mask & IB_QP_RNR_RETRY) {
  927. mqpcb->rnr_retry_count = attr->rnr_retry;
  928. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RNR_RETRY_COUNT, 1);
  929. }
  930. if (attr_mask & IB_QP_RQ_PSN) {
  931. mqpcb->receive_psn = attr->rq_psn;
  932. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RECEIVE_PSN, 1);
  933. }
  934. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  935. mqpcb->rdma_nr_atomic_resp_res = attr->max_dest_rd_atomic < 3 ?
  936. attr->max_dest_rd_atomic : 2;
  937. update_mask |=
  938. EHCA_BMASK_SET(MQPCB_MASK_RDMA_NR_ATOMIC_RESP_RES, 1);
  939. }
  940. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  941. mqpcb->rdma_atomic_outst_dest_qp = attr->max_rd_atomic < 3 ?
  942. attr->max_rd_atomic : 2;
  943. update_mask |=
  944. EHCA_BMASK_SET
  945. (MQPCB_MASK_RDMA_ATOMIC_OUTST_DEST_QP, 1);
  946. }
  947. if (attr_mask & IB_QP_ALT_PATH) {
  948. int ah_mult = ib_rate_to_mult(attr->alt_ah_attr.static_rate);
  949. int ehca_mult = ib_rate_to_mult(
  950. shca->sport[my_qp->init_attr.port_num].rate);
  951. mqpcb->dlid_al = attr->alt_ah_attr.dlid;
  952. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DLID_AL, 1);
  953. mqpcb->source_path_bits_al = attr->alt_ah_attr.src_path_bits;
  954. update_mask |=
  955. EHCA_BMASK_SET(MQPCB_MASK_SOURCE_PATH_BITS_AL, 1);
  956. mqpcb->service_level_al = attr->alt_ah_attr.sl;
  957. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SERVICE_LEVEL_AL, 1);
  958. if (ah_mult < ehca_mult)
  959. mqpcb->max_static_rate = (ah_mult > 0) ?
  960. ((ehca_mult - 1) / ah_mult) : 0;
  961. else
  962. mqpcb->max_static_rate_al = 0;
  963. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_MAX_STATIC_RATE_AL, 1);
  964. /*
  965. * only if GRH is TRUE we might consider SOURCE_GID_IDX
  966. * and DEST_GID otherwise phype will return H_ATTR_PARM!!!
  967. */
  968. if (attr->alt_ah_attr.ah_flags == IB_AH_GRH) {
  969. mqpcb->send_grh_flag_al = 1 << 31;
  970. update_mask |=
  971. EHCA_BMASK_SET(MQPCB_MASK_SEND_GRH_FLAG_AL, 1);
  972. mqpcb->source_gid_idx_al =
  973. attr->alt_ah_attr.grh.sgid_index;
  974. update_mask |=
  975. EHCA_BMASK_SET(MQPCB_MASK_SOURCE_GID_IDX_AL, 1);
  976. for (cnt = 0; cnt < 16; cnt++)
  977. mqpcb->dest_gid_al.byte[cnt] =
  978. attr->alt_ah_attr.grh.dgid.raw[cnt];
  979. update_mask |=
  980. EHCA_BMASK_SET(MQPCB_MASK_DEST_GID_AL, 1);
  981. mqpcb->flow_label_al = attr->alt_ah_attr.grh.flow_label;
  982. update_mask |=
  983. EHCA_BMASK_SET(MQPCB_MASK_FLOW_LABEL_AL, 1);
  984. mqpcb->hop_limit_al = attr->alt_ah_attr.grh.hop_limit;
  985. update_mask |=
  986. EHCA_BMASK_SET(MQPCB_MASK_HOP_LIMIT_AL, 1);
  987. mqpcb->traffic_class_al =
  988. attr->alt_ah_attr.grh.traffic_class;
  989. update_mask |=
  990. EHCA_BMASK_SET(MQPCB_MASK_TRAFFIC_CLASS_AL, 1);
  991. }
  992. }
  993. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  994. mqpcb->min_rnr_nak_timer_field = attr->min_rnr_timer;
  995. update_mask |=
  996. EHCA_BMASK_SET(MQPCB_MASK_MIN_RNR_NAK_TIMER_FIELD, 1);
  997. }
  998. if (attr_mask & IB_QP_SQ_PSN) {
  999. mqpcb->send_psn = attr->sq_psn;
  1000. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_PSN, 1);
  1001. }
  1002. if (attr_mask & IB_QP_DEST_QPN) {
  1003. mqpcb->dest_qp_nr = attr->dest_qp_num;
  1004. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DEST_QP_NR, 1);
  1005. }
  1006. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1007. mqpcb->path_migration_state = attr->path_mig_state;
  1008. update_mask |=
  1009. EHCA_BMASK_SET(MQPCB_MASK_PATH_MIGRATION_STATE, 1);
  1010. }
  1011. if (attr_mask & IB_QP_CAP) {
  1012. mqpcb->max_nr_outst_send_wr = attr->cap.max_send_wr+1;
  1013. update_mask |=
  1014. EHCA_BMASK_SET(MQPCB_MASK_MAX_NR_OUTST_SEND_WR, 1);
  1015. mqpcb->max_nr_outst_recv_wr = attr->cap.max_recv_wr+1;
  1016. update_mask |=
  1017. EHCA_BMASK_SET(MQPCB_MASK_MAX_NR_OUTST_RECV_WR, 1);
  1018. /* no support for max_send/recv_sge yet */
  1019. }
  1020. if (ehca_debug_level)
  1021. ehca_dmp(mqpcb, 4*70, "qp_num=%x", ibqp->qp_num);
  1022. h_ret = hipz_h_modify_qp(shca->ipz_hca_handle,
  1023. my_qp->ipz_qp_handle,
  1024. &my_qp->pf,
  1025. update_mask,
  1026. mqpcb, my_qp->galpas.kernel);
  1027. if (h_ret != H_SUCCESS) {
  1028. ret = ehca2ib_return_code(h_ret);
  1029. ehca_err(ibqp->device, "hipz_h_modify_qp() failed rc=%lx "
  1030. "ehca_qp=%p qp_num=%x",h_ret, my_qp, ibqp->qp_num);
  1031. goto modify_qp_exit2;
  1032. }
  1033. if ((my_qp->qp_type == IB_QPT_UD ||
  1034. my_qp->qp_type == IB_QPT_GSI ||
  1035. my_qp->qp_type == IB_QPT_SMI) &&
  1036. statetrans == IB_QPST_SQE2RTS) {
  1037. /* doorbell to reprocessing wqes */
  1038. iosync(); /* serialize GAL register access */
  1039. hipz_update_sqa(my_qp, bad_wqe_cnt-1);
  1040. ehca_gen_dbg("doorbell for %x wqes", bad_wqe_cnt);
  1041. }
  1042. if (statetrans == IB_QPST_RESET2INIT ||
  1043. statetrans == IB_QPST_INIT2INIT) {
  1044. mqpcb->qp_enable = 1;
  1045. mqpcb->qp_state = EHCA_QPS_INIT;
  1046. update_mask = 0;
  1047. update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_ENABLE, 1);
  1048. h_ret = hipz_h_modify_qp(shca->ipz_hca_handle,
  1049. my_qp->ipz_qp_handle,
  1050. &my_qp->pf,
  1051. update_mask,
  1052. mqpcb,
  1053. my_qp->galpas.kernel);
  1054. if (h_ret != H_SUCCESS) {
  1055. ret = ehca2ib_return_code(h_ret);
  1056. ehca_err(ibqp->device, "ENABLE in context of "
  1057. "RESET_2_INIT failed! Maybe you didn't get "
  1058. "a LID h_ret=%lx ehca_qp=%p qp_num=%x",
  1059. h_ret, my_qp, ibqp->qp_num);
  1060. goto modify_qp_exit2;
  1061. }
  1062. }
  1063. if (statetrans == IB_QPST_ANY2RESET) {
  1064. ipz_qeit_reset(&my_qp->ipz_rqueue);
  1065. ipz_qeit_reset(&my_qp->ipz_squeue);
  1066. }
  1067. if (attr_mask & IB_QP_QKEY)
  1068. my_qp->qkey = attr->qkey;
  1069. modify_qp_exit2:
  1070. if (squeue_locked) { /* this means: sqe -> rts */
  1071. spin_unlock_irqrestore(&my_qp->spinlock_s, spl_flags);
  1072. my_qp->sqerr_purgeflag = 1;
  1073. }
  1074. modify_qp_exit1:
  1075. ehca_free_fw_ctrlblock(mqpcb);
  1076. return ret;
  1077. }
  1078. int ehca_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int attr_mask,
  1079. struct ib_udata *udata)
  1080. {
  1081. struct ehca_qp *my_qp = container_of(ibqp, struct ehca_qp, ib_qp);
  1082. struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd,
  1083. ib_pd);
  1084. u32 cur_pid = current->tgid;
  1085. if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
  1086. my_pd->ownpid != cur_pid) {
  1087. ehca_err(ibqp->pd->device, "Invalid caller pid=%x ownpid=%x",
  1088. cur_pid, my_pd->ownpid);
  1089. return -EINVAL;
  1090. }
  1091. return internal_modify_qp(ibqp, attr, attr_mask, 0);
  1092. }
  1093. int ehca_query_qp(struct ib_qp *qp,
  1094. struct ib_qp_attr *qp_attr,
  1095. int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
  1096. {
  1097. struct ehca_qp *my_qp = container_of(qp, struct ehca_qp, ib_qp);
  1098. struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd,
  1099. ib_pd);
  1100. struct ehca_shca *shca = container_of(qp->device, struct ehca_shca,
  1101. ib_device);
  1102. struct ipz_adapter_handle adapter_handle = shca->ipz_hca_handle;
  1103. struct hcp_modify_qp_control_block *qpcb;
  1104. u32 cur_pid = current->tgid;
  1105. int cnt, ret = 0;
  1106. u64 h_ret;
  1107. if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
  1108. my_pd->ownpid != cur_pid) {
  1109. ehca_err(qp->device, "Invalid caller pid=%x ownpid=%x",
  1110. cur_pid, my_pd->ownpid);
  1111. return -EINVAL;
  1112. }
  1113. if (qp_attr_mask & QP_ATTR_QUERY_NOT_SUPPORTED) {
  1114. ehca_err(qp->device,"Invalid attribute mask "
  1115. "ehca_qp=%p qp_num=%x qp_attr_mask=%x ",
  1116. my_qp, qp->qp_num, qp_attr_mask);
  1117. return -EINVAL;
  1118. }
  1119. qpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  1120. if (!qpcb) {
  1121. ehca_err(qp->device,"Out of memory for qpcb "
  1122. "ehca_qp=%p qp_num=%x", my_qp, qp->qp_num);
  1123. return -ENOMEM;
  1124. }
  1125. h_ret = hipz_h_query_qp(adapter_handle,
  1126. my_qp->ipz_qp_handle,
  1127. &my_qp->pf,
  1128. qpcb, my_qp->galpas.kernel);
  1129. if (h_ret != H_SUCCESS) {
  1130. ret = ehca2ib_return_code(h_ret);
  1131. ehca_err(qp->device,"hipz_h_query_qp() failed "
  1132. "ehca_qp=%p qp_num=%x h_ret=%lx",
  1133. my_qp, qp->qp_num, h_ret);
  1134. goto query_qp_exit1;
  1135. }
  1136. qp_attr->cur_qp_state = ehca2ib_qp_state(qpcb->qp_state);
  1137. qp_attr->qp_state = qp_attr->cur_qp_state;
  1138. if (qp_attr->cur_qp_state == -EINVAL) {
  1139. ret = -EINVAL;
  1140. ehca_err(qp->device,"Got invalid ehca_qp_state=%x "
  1141. "ehca_qp=%p qp_num=%x",
  1142. qpcb->qp_state, my_qp, qp->qp_num);
  1143. goto query_qp_exit1;
  1144. }
  1145. if (qp_attr->qp_state == IB_QPS_SQD)
  1146. qp_attr->sq_draining = 1;
  1147. qp_attr->qkey = qpcb->qkey;
  1148. qp_attr->path_mtu = qpcb->path_mtu;
  1149. qp_attr->path_mig_state = qpcb->path_migration_state;
  1150. qp_attr->rq_psn = qpcb->receive_psn;
  1151. qp_attr->sq_psn = qpcb->send_psn;
  1152. qp_attr->min_rnr_timer = qpcb->min_rnr_nak_timer_field;
  1153. qp_attr->cap.max_send_wr = qpcb->max_nr_outst_send_wr-1;
  1154. qp_attr->cap.max_recv_wr = qpcb->max_nr_outst_recv_wr-1;
  1155. /* UD_AV CIRCUMVENTION */
  1156. if (my_qp->qp_type == IB_QPT_UD) {
  1157. qp_attr->cap.max_send_sge =
  1158. qpcb->actual_nr_sges_in_sq_wqe - 2;
  1159. qp_attr->cap.max_recv_sge =
  1160. qpcb->actual_nr_sges_in_rq_wqe - 2;
  1161. } else {
  1162. qp_attr->cap.max_send_sge =
  1163. qpcb->actual_nr_sges_in_sq_wqe;
  1164. qp_attr->cap.max_recv_sge =
  1165. qpcb->actual_nr_sges_in_rq_wqe;
  1166. }
  1167. qp_attr->cap.max_inline_data = my_qp->sq_max_inline_data_size;
  1168. qp_attr->dest_qp_num = qpcb->dest_qp_nr;
  1169. qp_attr->pkey_index =
  1170. EHCA_BMASK_GET(MQPCB_PRIM_P_KEY_IDX, qpcb->prim_p_key_idx);
  1171. qp_attr->port_num =
  1172. EHCA_BMASK_GET(MQPCB_PRIM_PHYS_PORT, qpcb->prim_phys_port);
  1173. qp_attr->timeout = qpcb->timeout;
  1174. qp_attr->retry_cnt = qpcb->retry_count;
  1175. qp_attr->rnr_retry = qpcb->rnr_retry_count;
  1176. qp_attr->alt_pkey_index =
  1177. EHCA_BMASK_GET(MQPCB_PRIM_P_KEY_IDX, qpcb->alt_p_key_idx);
  1178. qp_attr->alt_port_num = qpcb->alt_phys_port;
  1179. qp_attr->alt_timeout = qpcb->timeout_al;
  1180. /* primary av */
  1181. qp_attr->ah_attr.sl = qpcb->service_level;
  1182. if (qpcb->send_grh_flag) {
  1183. qp_attr->ah_attr.ah_flags = IB_AH_GRH;
  1184. }
  1185. qp_attr->ah_attr.static_rate = qpcb->max_static_rate;
  1186. qp_attr->ah_attr.dlid = qpcb->dlid;
  1187. qp_attr->ah_attr.src_path_bits = qpcb->source_path_bits;
  1188. qp_attr->ah_attr.port_num = qp_attr->port_num;
  1189. /* primary GRH */
  1190. qp_attr->ah_attr.grh.traffic_class = qpcb->traffic_class;
  1191. qp_attr->ah_attr.grh.hop_limit = qpcb->hop_limit;
  1192. qp_attr->ah_attr.grh.sgid_index = qpcb->source_gid_idx;
  1193. qp_attr->ah_attr.grh.flow_label = qpcb->flow_label;
  1194. for (cnt = 0; cnt < 16; cnt++)
  1195. qp_attr->ah_attr.grh.dgid.raw[cnt] =
  1196. qpcb->dest_gid.byte[cnt];
  1197. /* alternate AV */
  1198. qp_attr->alt_ah_attr.sl = qpcb->service_level_al;
  1199. if (qpcb->send_grh_flag_al) {
  1200. qp_attr->alt_ah_attr.ah_flags = IB_AH_GRH;
  1201. }
  1202. qp_attr->alt_ah_attr.static_rate = qpcb->max_static_rate_al;
  1203. qp_attr->alt_ah_attr.dlid = qpcb->dlid_al;
  1204. qp_attr->alt_ah_attr.src_path_bits = qpcb->source_path_bits_al;
  1205. /* alternate GRH */
  1206. qp_attr->alt_ah_attr.grh.traffic_class = qpcb->traffic_class_al;
  1207. qp_attr->alt_ah_attr.grh.hop_limit = qpcb->hop_limit_al;
  1208. qp_attr->alt_ah_attr.grh.sgid_index = qpcb->source_gid_idx_al;
  1209. qp_attr->alt_ah_attr.grh.flow_label = qpcb->flow_label_al;
  1210. for (cnt = 0; cnt < 16; cnt++)
  1211. qp_attr->alt_ah_attr.grh.dgid.raw[cnt] =
  1212. qpcb->dest_gid_al.byte[cnt];
  1213. /* return init attributes given in ehca_create_qp */
  1214. if (qp_init_attr)
  1215. *qp_init_attr = my_qp->init_attr;
  1216. if (ehca_debug_level)
  1217. ehca_dmp(qpcb, 4*70, "qp_num=%x", qp->qp_num);
  1218. query_qp_exit1:
  1219. ehca_free_fw_ctrlblock(qpcb);
  1220. return ret;
  1221. }
  1222. int ehca_destroy_qp(struct ib_qp *ibqp)
  1223. {
  1224. struct ehca_qp *my_qp = container_of(ibqp, struct ehca_qp, ib_qp);
  1225. struct ehca_shca *shca = container_of(ibqp->device, struct ehca_shca,
  1226. ib_device);
  1227. struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd,
  1228. ib_pd);
  1229. u32 cur_pid = current->tgid;
  1230. u32 qp_num = ibqp->qp_num;
  1231. int ret;
  1232. u64 h_ret;
  1233. u8 port_num;
  1234. enum ib_qp_type qp_type;
  1235. unsigned long flags;
  1236. if (ibqp->uobject) {
  1237. if (my_qp->mm_count_galpa ||
  1238. my_qp->mm_count_rqueue || my_qp->mm_count_squeue) {
  1239. ehca_err(ibqp->device, "Resources still referenced in "
  1240. "user space qp_num=%x", ibqp->qp_num);
  1241. return -EINVAL;
  1242. }
  1243. if (my_pd->ownpid != cur_pid) {
  1244. ehca_err(ibqp->device, "Invalid caller pid=%x ownpid=%x",
  1245. cur_pid, my_pd->ownpid);
  1246. return -EINVAL;
  1247. }
  1248. }
  1249. if (my_qp->send_cq) {
  1250. ret = ehca_cq_unassign_qp(my_qp->send_cq,
  1251. my_qp->real_qp_num);
  1252. if (ret) {
  1253. ehca_err(ibqp->device, "Couldn't unassign qp from "
  1254. "send_cq ret=%x qp_num=%x cq_num=%x", ret,
  1255. my_qp->ib_qp.qp_num, my_qp->send_cq->cq_number);
  1256. return ret;
  1257. }
  1258. }
  1259. spin_lock_irqsave(&ehca_qp_idr_lock, flags);
  1260. idr_remove(&ehca_qp_idr, my_qp->token);
  1261. spin_unlock_irqrestore(&ehca_qp_idr_lock, flags);
  1262. h_ret = hipz_h_destroy_qp(shca->ipz_hca_handle, my_qp);
  1263. if (h_ret != H_SUCCESS) {
  1264. ehca_err(ibqp->device, "hipz_h_destroy_qp() failed rc=%lx "
  1265. "ehca_qp=%p qp_num=%x", h_ret, my_qp, qp_num);
  1266. return ehca2ib_return_code(h_ret);
  1267. }
  1268. port_num = my_qp->init_attr.port_num;
  1269. qp_type = my_qp->init_attr.qp_type;
  1270. /* no support for IB_QPT_SMI yet */
  1271. if (qp_type == IB_QPT_GSI) {
  1272. struct ib_event event;
  1273. ehca_info(ibqp->device, "device %s: port %x is inactive.",
  1274. shca->ib_device.name, port_num);
  1275. event.device = &shca->ib_device;
  1276. event.event = IB_EVENT_PORT_ERR;
  1277. event.element.port_num = port_num;
  1278. shca->sport[port_num - 1].port_state = IB_PORT_DOWN;
  1279. ib_dispatch_event(&event);
  1280. }
  1281. ipz_queue_dtor(&my_qp->ipz_rqueue);
  1282. ipz_queue_dtor(&my_qp->ipz_squeue);
  1283. kmem_cache_free(qp_cache, my_qp);
  1284. return 0;
  1285. }
  1286. int ehca_init_qp_cache(void)
  1287. {
  1288. qp_cache = kmem_cache_create("ehca_cache_qp",
  1289. sizeof(struct ehca_qp), 0,
  1290. SLAB_HWCACHE_ALIGN,
  1291. NULL, NULL);
  1292. if (!qp_cache)
  1293. return -ENOMEM;
  1294. return 0;
  1295. }
  1296. void ehca_cleanup_qp_cache(void)
  1297. {
  1298. if (qp_cache)
  1299. kmem_cache_destroy(qp_cache);
  1300. }