ehca_qp.c 56 KB

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