ehca_qp.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  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. /* needs to be called with cq->spinlock held */
  372. void ehca_add_to_err_list(struct ehca_qp *qp, int on_sq)
  373. {
  374. struct list_head *list, *node;
  375. /* TODO: support low latency QPs */
  376. if (qp->ext_type == EQPT_LLQP)
  377. return;
  378. if (on_sq) {
  379. list = &qp->send_cq->sqp_err_list;
  380. node = &qp->sq_err_node;
  381. } else {
  382. list = &qp->recv_cq->rqp_err_list;
  383. node = &qp->rq_err_node;
  384. }
  385. if (list_empty(node))
  386. list_add_tail(node, list);
  387. return;
  388. }
  389. static void del_from_err_list(struct ehca_cq *cq, struct list_head *node)
  390. {
  391. unsigned long flags;
  392. spin_lock_irqsave(&cq->spinlock, flags);
  393. if (!list_empty(node))
  394. list_del_init(node);
  395. spin_unlock_irqrestore(&cq->spinlock, flags);
  396. }
  397. static void reset_queue_map(struct ehca_queue_map *qmap)
  398. {
  399. int i;
  400. qmap->tail = 0;
  401. for (i = 0; i < qmap->entries; i++)
  402. qmap->map[i].reported = 1;
  403. }
  404. /*
  405. * Create an ib_qp struct that is either a QP or an SRQ, depending on
  406. * the value of the is_srq parameter. If init_attr and srq_init_attr share
  407. * fields, the field out of init_attr is used.
  408. */
  409. static struct ehca_qp *internal_create_qp(
  410. struct ib_pd *pd,
  411. struct ib_qp_init_attr *init_attr,
  412. struct ib_srq_init_attr *srq_init_attr,
  413. struct ib_udata *udata, int is_srq)
  414. {
  415. struct ehca_qp *my_qp, *my_srq = NULL;
  416. struct ehca_pd *my_pd = container_of(pd, struct ehca_pd, ib_pd);
  417. struct ehca_shca *shca = container_of(pd->device, struct ehca_shca,
  418. ib_device);
  419. struct ib_ucontext *context = NULL;
  420. u64 h_ret;
  421. int is_llqp = 0, has_srq = 0;
  422. int qp_type, max_send_sge, max_recv_sge, ret;
  423. /* h_call's out parameters */
  424. struct ehca_alloc_qp_parms parms;
  425. u32 swqe_size = 0, rwqe_size = 0, ib_qp_num;
  426. unsigned long flags;
  427. if (!atomic_add_unless(&shca->num_qps, 1, ehca_max_qp)) {
  428. ehca_err(pd->device, "Unable to create QP, max number of %i "
  429. "QPs reached.", ehca_max_qp);
  430. ehca_err(pd->device, "To increase the maximum number of QPs "
  431. "use the number_of_qps module parameter.\n");
  432. return ERR_PTR(-ENOSPC);
  433. }
  434. if (init_attr->create_flags) {
  435. atomic_dec(&shca->num_qps);
  436. return ERR_PTR(-EINVAL);
  437. }
  438. memset(&parms, 0, sizeof(parms));
  439. qp_type = init_attr->qp_type;
  440. if (init_attr->sq_sig_type != IB_SIGNAL_REQ_WR &&
  441. init_attr->sq_sig_type != IB_SIGNAL_ALL_WR) {
  442. ehca_err(pd->device, "init_attr->sg_sig_type=%x not allowed",
  443. init_attr->sq_sig_type);
  444. atomic_dec(&shca->num_qps);
  445. return ERR_PTR(-EINVAL);
  446. }
  447. /* save LLQP info */
  448. if (qp_type & 0x80) {
  449. is_llqp = 1;
  450. parms.ext_type = EQPT_LLQP;
  451. parms.ll_comp_flags = qp_type & LLQP_COMP_MASK;
  452. }
  453. qp_type &= 0x1F;
  454. init_attr->qp_type &= 0x1F;
  455. /* handle SRQ base QPs */
  456. if (init_attr->srq) {
  457. my_srq = container_of(init_attr->srq, struct ehca_qp, ib_srq);
  458. has_srq = 1;
  459. parms.ext_type = EQPT_SRQBASE;
  460. parms.srq_qpn = my_srq->real_qp_num;
  461. }
  462. if (is_llqp && has_srq) {
  463. ehca_err(pd->device, "LLQPs can't have an SRQ");
  464. atomic_dec(&shca->num_qps);
  465. return ERR_PTR(-EINVAL);
  466. }
  467. /* handle SRQs */
  468. if (is_srq) {
  469. parms.ext_type = EQPT_SRQ;
  470. parms.srq_limit = srq_init_attr->attr.srq_limit;
  471. if (init_attr->cap.max_recv_sge > 3) {
  472. ehca_err(pd->device, "no more than three SGEs "
  473. "supported for SRQ pd=%p max_sge=%x",
  474. pd, init_attr->cap.max_recv_sge);
  475. atomic_dec(&shca->num_qps);
  476. return ERR_PTR(-EINVAL);
  477. }
  478. }
  479. /* check QP type */
  480. if (qp_type != IB_QPT_UD &&
  481. qp_type != IB_QPT_UC &&
  482. qp_type != IB_QPT_RC &&
  483. qp_type != IB_QPT_SMI &&
  484. qp_type != IB_QPT_GSI) {
  485. ehca_err(pd->device, "wrong QP Type=%x", qp_type);
  486. atomic_dec(&shca->num_qps);
  487. return ERR_PTR(-EINVAL);
  488. }
  489. if (is_llqp) {
  490. switch (qp_type) {
  491. case IB_QPT_RC:
  492. if ((init_attr->cap.max_send_wr > 255) ||
  493. (init_attr->cap.max_recv_wr > 255)) {
  494. ehca_err(pd->device,
  495. "Invalid Number of max_sq_wr=%x "
  496. "or max_rq_wr=%x for RC LLQP",
  497. init_attr->cap.max_send_wr,
  498. init_attr->cap.max_recv_wr);
  499. atomic_dec(&shca->num_qps);
  500. return ERR_PTR(-EINVAL);
  501. }
  502. break;
  503. case IB_QPT_UD:
  504. if (!EHCA_BMASK_GET(HCA_CAP_UD_LL_QP, shca->hca_cap)) {
  505. ehca_err(pd->device, "UD LLQP not supported "
  506. "by this adapter");
  507. atomic_dec(&shca->num_qps);
  508. return ERR_PTR(-ENOSYS);
  509. }
  510. if (!(init_attr->cap.max_send_sge <= 5
  511. && init_attr->cap.max_send_sge >= 1
  512. && init_attr->cap.max_recv_sge <= 5
  513. && init_attr->cap.max_recv_sge >= 1)) {
  514. ehca_err(pd->device,
  515. "Invalid Number of max_send_sge=%x "
  516. "or max_recv_sge=%x for UD LLQP",
  517. init_attr->cap.max_send_sge,
  518. init_attr->cap.max_recv_sge);
  519. atomic_dec(&shca->num_qps);
  520. return ERR_PTR(-EINVAL);
  521. } else if (init_attr->cap.max_send_wr > 255) {
  522. ehca_err(pd->device,
  523. "Invalid Number of "
  524. "max_send_wr=%x for UD QP_TYPE=%x",
  525. init_attr->cap.max_send_wr, qp_type);
  526. atomic_dec(&shca->num_qps);
  527. return ERR_PTR(-EINVAL);
  528. }
  529. break;
  530. default:
  531. ehca_err(pd->device, "unsupported LL QP Type=%x",
  532. qp_type);
  533. atomic_dec(&shca->num_qps);
  534. return ERR_PTR(-EINVAL);
  535. }
  536. } else {
  537. int max_sge = (qp_type == IB_QPT_UD || qp_type == IB_QPT_SMI
  538. || qp_type == IB_QPT_GSI) ? 250 : 252;
  539. if (init_attr->cap.max_send_sge > max_sge
  540. || init_attr->cap.max_recv_sge > max_sge) {
  541. ehca_err(pd->device, "Invalid number of SGEs requested "
  542. "send_sge=%x recv_sge=%x max_sge=%x",
  543. init_attr->cap.max_send_sge,
  544. init_attr->cap.max_recv_sge, max_sge);
  545. atomic_dec(&shca->num_qps);
  546. return ERR_PTR(-EINVAL);
  547. }
  548. }
  549. if (pd->uobject && udata)
  550. context = pd->uobject->context;
  551. my_qp = kmem_cache_zalloc(qp_cache, GFP_KERNEL);
  552. if (!my_qp) {
  553. ehca_err(pd->device, "pd=%p not enough memory to alloc qp", pd);
  554. atomic_dec(&shca->num_qps);
  555. return ERR_PTR(-ENOMEM);
  556. }
  557. atomic_set(&my_qp->nr_events, 0);
  558. init_waitqueue_head(&my_qp->wait_completion);
  559. spin_lock_init(&my_qp->spinlock_s);
  560. spin_lock_init(&my_qp->spinlock_r);
  561. my_qp->qp_type = qp_type;
  562. my_qp->ext_type = parms.ext_type;
  563. my_qp->state = IB_QPS_RESET;
  564. if (init_attr->recv_cq)
  565. my_qp->recv_cq =
  566. container_of(init_attr->recv_cq, struct ehca_cq, ib_cq);
  567. if (init_attr->send_cq)
  568. my_qp->send_cq =
  569. container_of(init_attr->send_cq, struct ehca_cq, ib_cq);
  570. do {
  571. if (!idr_pre_get(&ehca_qp_idr, GFP_KERNEL)) {
  572. ret = -ENOMEM;
  573. ehca_err(pd->device, "Can't reserve idr resources.");
  574. goto create_qp_exit0;
  575. }
  576. write_lock_irqsave(&ehca_qp_idr_lock, flags);
  577. ret = idr_get_new(&ehca_qp_idr, my_qp, &my_qp->token);
  578. write_unlock_irqrestore(&ehca_qp_idr_lock, flags);
  579. } while (ret == -EAGAIN);
  580. if (ret) {
  581. ret = -ENOMEM;
  582. ehca_err(pd->device, "Can't allocate new idr entry.");
  583. goto create_qp_exit0;
  584. }
  585. if (my_qp->token > 0x1FFFFFF) {
  586. ret = -EINVAL;
  587. ehca_err(pd->device, "Invalid number of qp");
  588. goto create_qp_exit1;
  589. }
  590. if (has_srq)
  591. parms.srq_token = my_qp->token;
  592. parms.servicetype = ibqptype2servicetype(qp_type);
  593. if (parms.servicetype < 0) {
  594. ret = -EINVAL;
  595. ehca_err(pd->device, "Invalid qp_type=%x", qp_type);
  596. goto create_qp_exit1;
  597. }
  598. /* Always signal by WQE so we can hide circ. WQEs */
  599. parms.sigtype = HCALL_SIGT_BY_WQE;
  600. /* UD_AV CIRCUMVENTION */
  601. max_send_sge = init_attr->cap.max_send_sge;
  602. max_recv_sge = init_attr->cap.max_recv_sge;
  603. if (parms.servicetype == ST_UD && !is_llqp) {
  604. max_send_sge += 2;
  605. max_recv_sge += 2;
  606. }
  607. parms.token = my_qp->token;
  608. parms.eq_handle = shca->eq.ipz_eq_handle;
  609. parms.pd = my_pd->fw_pd;
  610. if (my_qp->send_cq)
  611. parms.send_cq_handle = my_qp->send_cq->ipz_cq_handle;
  612. if (my_qp->recv_cq)
  613. parms.recv_cq_handle = my_qp->recv_cq->ipz_cq_handle;
  614. parms.squeue.max_wr = init_attr->cap.max_send_wr;
  615. parms.rqueue.max_wr = init_attr->cap.max_recv_wr;
  616. parms.squeue.max_sge = max_send_sge;
  617. parms.rqueue.max_sge = max_recv_sge;
  618. /* RC QPs need one more SWQE for unsolicited ack circumvention */
  619. if (qp_type == IB_QPT_RC)
  620. parms.squeue.max_wr++;
  621. if (EHCA_BMASK_GET(HCA_CAP_MINI_QP, shca->hca_cap)) {
  622. if (HAS_SQ(my_qp))
  623. ehca_determine_small_queue(
  624. &parms.squeue, max_send_sge, is_llqp);
  625. if (HAS_RQ(my_qp))
  626. ehca_determine_small_queue(
  627. &parms.rqueue, max_recv_sge, is_llqp);
  628. parms.qp_storage =
  629. (parms.squeue.is_small || parms.rqueue.is_small);
  630. }
  631. h_ret = hipz_h_alloc_resource_qp(shca->ipz_hca_handle, &parms);
  632. if (h_ret != H_SUCCESS) {
  633. ehca_err(pd->device, "h_alloc_resource_qp() failed h_ret=%li",
  634. h_ret);
  635. ret = ehca2ib_return_code(h_ret);
  636. goto create_qp_exit1;
  637. }
  638. ib_qp_num = my_qp->real_qp_num = parms.real_qp_num;
  639. my_qp->ipz_qp_handle = parms.qp_handle;
  640. my_qp->galpas = parms.galpas;
  641. swqe_size = ehca_calc_wqe_size(parms.squeue.act_nr_sges, is_llqp);
  642. rwqe_size = ehca_calc_wqe_size(parms.rqueue.act_nr_sges, is_llqp);
  643. switch (qp_type) {
  644. case IB_QPT_RC:
  645. if (is_llqp) {
  646. parms.squeue.act_nr_sges = 1;
  647. parms.rqueue.act_nr_sges = 1;
  648. }
  649. /* hide the extra WQE */
  650. parms.squeue.act_nr_wqes--;
  651. break;
  652. case IB_QPT_UD:
  653. case IB_QPT_GSI:
  654. case IB_QPT_SMI:
  655. /* UD circumvention */
  656. if (is_llqp) {
  657. parms.squeue.act_nr_sges = 1;
  658. parms.rqueue.act_nr_sges = 1;
  659. } else {
  660. parms.squeue.act_nr_sges -= 2;
  661. parms.rqueue.act_nr_sges -= 2;
  662. }
  663. if (IB_QPT_GSI == qp_type || IB_QPT_SMI == qp_type) {
  664. parms.squeue.act_nr_wqes = init_attr->cap.max_send_wr;
  665. parms.rqueue.act_nr_wqes = init_attr->cap.max_recv_wr;
  666. parms.squeue.act_nr_sges = init_attr->cap.max_send_sge;
  667. parms.rqueue.act_nr_sges = init_attr->cap.max_recv_sge;
  668. ib_qp_num = (qp_type == IB_QPT_SMI) ? 0 : 1;
  669. }
  670. break;
  671. default:
  672. break;
  673. }
  674. /* initialize r/squeue and register queue pages */
  675. if (HAS_SQ(my_qp)) {
  676. ret = init_qp_queue(
  677. shca, my_pd, my_qp, &my_qp->ipz_squeue, 0,
  678. HAS_RQ(my_qp) ? H_PAGE_REGISTERED : H_SUCCESS,
  679. &parms.squeue, swqe_size);
  680. if (ret) {
  681. ehca_err(pd->device, "Couldn't initialize squeue "
  682. "and pages ret=%i", ret);
  683. goto create_qp_exit2;
  684. }
  685. my_qp->sq_map.entries = my_qp->ipz_squeue.queue_length /
  686. my_qp->ipz_squeue.qe_size;
  687. my_qp->sq_map.map = vmalloc(my_qp->sq_map.entries *
  688. sizeof(struct ehca_qmap_entry));
  689. if (!my_qp->sq_map.map) {
  690. ehca_err(pd->device, "Couldn't allocate squeue "
  691. "map ret=%i", ret);
  692. goto create_qp_exit3;
  693. }
  694. INIT_LIST_HEAD(&my_qp->sq_err_node);
  695. /* to avoid the generation of bogus flush CQEs */
  696. reset_queue_map(&my_qp->sq_map);
  697. }
  698. if (HAS_RQ(my_qp)) {
  699. ret = init_qp_queue(
  700. shca, my_pd, my_qp, &my_qp->ipz_rqueue, 1,
  701. H_SUCCESS, &parms.rqueue, rwqe_size);
  702. if (ret) {
  703. ehca_err(pd->device, "Couldn't initialize rqueue "
  704. "and pages ret=%i", ret);
  705. goto create_qp_exit4;
  706. }
  707. my_qp->rq_map.entries = my_qp->ipz_rqueue.queue_length /
  708. my_qp->ipz_rqueue.qe_size;
  709. my_qp->rq_map.map = vmalloc(my_qp->rq_map.entries *
  710. sizeof(struct ehca_qmap_entry));
  711. if (!my_qp->rq_map.map) {
  712. ehca_err(pd->device, "Couldn't allocate squeue "
  713. "map ret=%i", ret);
  714. goto create_qp_exit5;
  715. }
  716. INIT_LIST_HEAD(&my_qp->rq_err_node);
  717. /* to avoid the generation of bogus flush CQEs */
  718. reset_queue_map(&my_qp->rq_map);
  719. } else if (init_attr->srq) {
  720. /* this is a base QP, use the queue map of the SRQ */
  721. my_qp->rq_map = my_srq->rq_map;
  722. INIT_LIST_HEAD(&my_qp->rq_err_node);
  723. my_qp->ipz_rqueue = my_srq->ipz_rqueue;
  724. }
  725. if (is_srq) {
  726. my_qp->ib_srq.pd = &my_pd->ib_pd;
  727. my_qp->ib_srq.device = my_pd->ib_pd.device;
  728. my_qp->ib_srq.srq_context = init_attr->qp_context;
  729. my_qp->ib_srq.event_handler = init_attr->event_handler;
  730. } else {
  731. my_qp->ib_qp.qp_num = ib_qp_num;
  732. my_qp->ib_qp.pd = &my_pd->ib_pd;
  733. my_qp->ib_qp.device = my_pd->ib_pd.device;
  734. my_qp->ib_qp.recv_cq = init_attr->recv_cq;
  735. my_qp->ib_qp.send_cq = init_attr->send_cq;
  736. my_qp->ib_qp.qp_type = qp_type;
  737. my_qp->ib_qp.srq = init_attr->srq;
  738. my_qp->ib_qp.qp_context = init_attr->qp_context;
  739. my_qp->ib_qp.event_handler = init_attr->event_handler;
  740. }
  741. init_attr->cap.max_inline_data = 0; /* not supported yet */
  742. init_attr->cap.max_recv_sge = parms.rqueue.act_nr_sges;
  743. init_attr->cap.max_recv_wr = parms.rqueue.act_nr_wqes;
  744. init_attr->cap.max_send_sge = parms.squeue.act_nr_sges;
  745. init_attr->cap.max_send_wr = parms.squeue.act_nr_wqes;
  746. my_qp->init_attr = *init_attr;
  747. if (qp_type == IB_QPT_SMI || qp_type == IB_QPT_GSI) {
  748. shca->sport[init_attr->port_num - 1].ibqp_sqp[qp_type] =
  749. &my_qp->ib_qp;
  750. if (ehca_nr_ports < 0) {
  751. /* alloc array to cache subsequent modify qp parms
  752. * for autodetect mode
  753. */
  754. my_qp->mod_qp_parm =
  755. kzalloc(EHCA_MOD_QP_PARM_MAX *
  756. sizeof(*my_qp->mod_qp_parm),
  757. GFP_KERNEL);
  758. if (!my_qp->mod_qp_parm) {
  759. ehca_err(pd->device,
  760. "Could not alloc mod_qp_parm");
  761. goto create_qp_exit5;
  762. }
  763. }
  764. }
  765. /* NOTE: define_apq0() not supported yet */
  766. if (qp_type == IB_QPT_GSI) {
  767. h_ret = ehca_define_sqp(shca, my_qp, init_attr);
  768. if (h_ret != H_SUCCESS) {
  769. ret = ehca2ib_return_code(h_ret);
  770. goto create_qp_exit6;
  771. }
  772. }
  773. if (my_qp->send_cq) {
  774. ret = ehca_cq_assign_qp(my_qp->send_cq, my_qp);
  775. if (ret) {
  776. ehca_err(pd->device,
  777. "Couldn't assign qp to send_cq ret=%i", ret);
  778. goto create_qp_exit7;
  779. }
  780. }
  781. /* copy queues, galpa data to user space */
  782. if (context && udata) {
  783. struct ehca_create_qp_resp resp;
  784. memset(&resp, 0, sizeof(resp));
  785. resp.qp_num = my_qp->real_qp_num;
  786. resp.token = my_qp->token;
  787. resp.qp_type = my_qp->qp_type;
  788. resp.ext_type = my_qp->ext_type;
  789. resp.qkey = my_qp->qkey;
  790. resp.real_qp_num = my_qp->real_qp_num;
  791. if (HAS_SQ(my_qp))
  792. queue2resp(&resp.ipz_squeue, &my_qp->ipz_squeue);
  793. if (HAS_RQ(my_qp))
  794. queue2resp(&resp.ipz_rqueue, &my_qp->ipz_rqueue);
  795. resp.fw_handle_ofs = (u32)
  796. (my_qp->galpas.user.fw_handle & (PAGE_SIZE - 1));
  797. if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
  798. ehca_err(pd->device, "Copy to udata failed");
  799. ret = -EINVAL;
  800. goto create_qp_exit8;
  801. }
  802. }
  803. return my_qp;
  804. create_qp_exit8:
  805. ehca_cq_unassign_qp(my_qp->send_cq, my_qp->real_qp_num);
  806. create_qp_exit7:
  807. kfree(my_qp->mod_qp_parm);
  808. create_qp_exit6:
  809. if (HAS_RQ(my_qp))
  810. vfree(my_qp->rq_map.map);
  811. create_qp_exit5:
  812. if (HAS_RQ(my_qp))
  813. ipz_queue_dtor(my_pd, &my_qp->ipz_rqueue);
  814. create_qp_exit4:
  815. if (HAS_SQ(my_qp))
  816. vfree(my_qp->sq_map.map);
  817. create_qp_exit3:
  818. if (HAS_SQ(my_qp))
  819. ipz_queue_dtor(my_pd, &my_qp->ipz_squeue);
  820. create_qp_exit2:
  821. hipz_h_destroy_qp(shca->ipz_hca_handle, my_qp);
  822. create_qp_exit1:
  823. write_lock_irqsave(&ehca_qp_idr_lock, flags);
  824. idr_remove(&ehca_qp_idr, my_qp->token);
  825. write_unlock_irqrestore(&ehca_qp_idr_lock, flags);
  826. create_qp_exit0:
  827. kmem_cache_free(qp_cache, my_qp);
  828. atomic_dec(&shca->num_qps);
  829. return ERR_PTR(ret);
  830. }
  831. struct ib_qp *ehca_create_qp(struct ib_pd *pd,
  832. struct ib_qp_init_attr *qp_init_attr,
  833. struct ib_udata *udata)
  834. {
  835. struct ehca_qp *ret;
  836. ret = internal_create_qp(pd, qp_init_attr, NULL, udata, 0);
  837. return IS_ERR(ret) ? (struct ib_qp *)ret : &ret->ib_qp;
  838. }
  839. static int internal_destroy_qp(struct ib_device *dev, struct ehca_qp *my_qp,
  840. struct ib_uobject *uobject);
  841. struct ib_srq *ehca_create_srq(struct ib_pd *pd,
  842. struct ib_srq_init_attr *srq_init_attr,
  843. struct ib_udata *udata)
  844. {
  845. struct ib_qp_init_attr qp_init_attr;
  846. struct ehca_qp *my_qp;
  847. struct ib_srq *ret;
  848. struct ehca_shca *shca = container_of(pd->device, struct ehca_shca,
  849. ib_device);
  850. struct hcp_modify_qp_control_block *mqpcb;
  851. u64 hret, update_mask;
  852. /* For common attributes, internal_create_qp() takes its info
  853. * out of qp_init_attr, so copy all common attrs there.
  854. */
  855. memset(&qp_init_attr, 0, sizeof(qp_init_attr));
  856. qp_init_attr.event_handler = srq_init_attr->event_handler;
  857. qp_init_attr.qp_context = srq_init_attr->srq_context;
  858. qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
  859. qp_init_attr.qp_type = IB_QPT_RC;
  860. qp_init_attr.cap.max_recv_wr = srq_init_attr->attr.max_wr;
  861. qp_init_attr.cap.max_recv_sge = srq_init_attr->attr.max_sge;
  862. my_qp = internal_create_qp(pd, &qp_init_attr, srq_init_attr, udata, 1);
  863. if (IS_ERR(my_qp))
  864. return (struct ib_srq *)my_qp;
  865. /* copy back return values */
  866. srq_init_attr->attr.max_wr = qp_init_attr.cap.max_recv_wr;
  867. srq_init_attr->attr.max_sge = 3;
  868. /* drive SRQ into RTR state */
  869. mqpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  870. if (!mqpcb) {
  871. ehca_err(pd->device, "Could not get zeroed page for mqpcb "
  872. "ehca_qp=%p qp_num=%x ", my_qp, my_qp->real_qp_num);
  873. ret = ERR_PTR(-ENOMEM);
  874. goto create_srq1;
  875. }
  876. mqpcb->qp_state = EHCA_QPS_INIT;
  877. mqpcb->prim_phys_port = 1;
  878. update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_STATE, 1);
  879. hret = hipz_h_modify_qp(shca->ipz_hca_handle,
  880. my_qp->ipz_qp_handle,
  881. &my_qp->pf,
  882. update_mask,
  883. mqpcb, my_qp->galpas.kernel);
  884. if (hret != H_SUCCESS) {
  885. ehca_err(pd->device, "Could not modify SRQ to INIT "
  886. "ehca_qp=%p qp_num=%x h_ret=%li",
  887. my_qp, my_qp->real_qp_num, hret);
  888. goto create_srq2;
  889. }
  890. mqpcb->qp_enable = 1;
  891. update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_ENABLE, 1);
  892. hret = hipz_h_modify_qp(shca->ipz_hca_handle,
  893. my_qp->ipz_qp_handle,
  894. &my_qp->pf,
  895. update_mask,
  896. mqpcb, my_qp->galpas.kernel);
  897. if (hret != H_SUCCESS) {
  898. ehca_err(pd->device, "Could not enable SRQ "
  899. "ehca_qp=%p qp_num=%x h_ret=%li",
  900. my_qp, my_qp->real_qp_num, hret);
  901. goto create_srq2;
  902. }
  903. mqpcb->qp_state = EHCA_QPS_RTR;
  904. update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_STATE, 1);
  905. hret = hipz_h_modify_qp(shca->ipz_hca_handle,
  906. my_qp->ipz_qp_handle,
  907. &my_qp->pf,
  908. update_mask,
  909. mqpcb, my_qp->galpas.kernel);
  910. if (hret != H_SUCCESS) {
  911. ehca_err(pd->device, "Could not modify SRQ to RTR "
  912. "ehca_qp=%p qp_num=%x h_ret=%li",
  913. my_qp, my_qp->real_qp_num, hret);
  914. goto create_srq2;
  915. }
  916. ehca_free_fw_ctrlblock(mqpcb);
  917. return &my_qp->ib_srq;
  918. create_srq2:
  919. ret = ERR_PTR(ehca2ib_return_code(hret));
  920. ehca_free_fw_ctrlblock(mqpcb);
  921. create_srq1:
  922. internal_destroy_qp(pd->device, my_qp, my_qp->ib_srq.uobject);
  923. return ret;
  924. }
  925. /*
  926. * prepare_sqe_rts called by internal_modify_qp() at trans sqe -> rts
  927. * set purge bit of bad wqe and subsequent wqes to avoid reentering sqe
  928. * returns total number of bad wqes in bad_wqe_cnt
  929. */
  930. static int prepare_sqe_rts(struct ehca_qp *my_qp, struct ehca_shca *shca,
  931. int *bad_wqe_cnt)
  932. {
  933. u64 h_ret;
  934. struct ipz_queue *squeue;
  935. void *bad_send_wqe_p, *bad_send_wqe_v;
  936. u64 q_ofs;
  937. struct ehca_wqe *wqe;
  938. int qp_num = my_qp->ib_qp.qp_num;
  939. /* get send wqe pointer */
  940. h_ret = hipz_h_disable_and_get_wqe(shca->ipz_hca_handle,
  941. my_qp->ipz_qp_handle, &my_qp->pf,
  942. &bad_send_wqe_p, NULL, 2);
  943. if (h_ret != H_SUCCESS) {
  944. ehca_err(&shca->ib_device, "hipz_h_disable_and_get_wqe() failed"
  945. " ehca_qp=%p qp_num=%x h_ret=%li",
  946. my_qp, qp_num, h_ret);
  947. return ehca2ib_return_code(h_ret);
  948. }
  949. bad_send_wqe_p = (void *)((u64)bad_send_wqe_p & (~(1L << 63)));
  950. ehca_dbg(&shca->ib_device, "qp_num=%x bad_send_wqe_p=%p",
  951. qp_num, bad_send_wqe_p);
  952. /* convert wqe pointer to vadr */
  953. bad_send_wqe_v = abs_to_virt((u64)bad_send_wqe_p);
  954. if (ehca_debug_level >= 2)
  955. ehca_dmp(bad_send_wqe_v, 32, "qp_num=%x bad_wqe", qp_num);
  956. squeue = &my_qp->ipz_squeue;
  957. if (ipz_queue_abs_to_offset(squeue, (u64)bad_send_wqe_p, &q_ofs)) {
  958. ehca_err(&shca->ib_device, "failed to get wqe offset qp_num=%x"
  959. " bad_send_wqe_p=%p", qp_num, bad_send_wqe_p);
  960. return -EFAULT;
  961. }
  962. /* loop sets wqe's purge bit */
  963. wqe = (struct ehca_wqe *)ipz_qeit_calc(squeue, q_ofs);
  964. *bad_wqe_cnt = 0;
  965. while (wqe->optype != 0xff && wqe->wqef != 0xff) {
  966. if (ehca_debug_level >= 2)
  967. ehca_dmp(wqe, 32, "qp_num=%x wqe", qp_num);
  968. wqe->nr_of_data_seg = 0; /* suppress data access */
  969. wqe->wqef = WQEF_PURGE; /* WQE to be purged */
  970. q_ofs = ipz_queue_advance_offset(squeue, q_ofs);
  971. wqe = (struct ehca_wqe *)ipz_qeit_calc(squeue, q_ofs);
  972. *bad_wqe_cnt = (*bad_wqe_cnt)+1;
  973. }
  974. /*
  975. * bad wqe will be reprocessed and ignored when pol_cq() is called,
  976. * i.e. nr of wqes with flush error status is one less
  977. */
  978. ehca_dbg(&shca->ib_device, "qp_num=%x flusherr_wqe_cnt=%x",
  979. qp_num, (*bad_wqe_cnt)-1);
  980. wqe->wqef = 0;
  981. return 0;
  982. }
  983. static int calc_left_cqes(u64 wqe_p, struct ipz_queue *ipz_queue,
  984. struct ehca_queue_map *qmap)
  985. {
  986. void *wqe_v;
  987. u64 q_ofs;
  988. u32 wqe_idx;
  989. /* convert real to abs address */
  990. wqe_p = wqe_p & (~(1UL << 63));
  991. wqe_v = abs_to_virt(wqe_p);
  992. if (ipz_queue_abs_to_offset(ipz_queue, wqe_p, &q_ofs)) {
  993. ehca_gen_err("Invalid offset for calculating left cqes "
  994. "wqe_p=%#lx wqe_v=%p\n", wqe_p, wqe_v);
  995. return -EFAULT;
  996. }
  997. wqe_idx = q_ofs / ipz_queue->qe_size;
  998. if (wqe_idx < qmap->tail)
  999. qmap->left_to_poll = (qmap->entries - qmap->tail) + wqe_idx;
  1000. else
  1001. qmap->left_to_poll = wqe_idx - qmap->tail;
  1002. return 0;
  1003. }
  1004. static int check_for_left_cqes(struct ehca_qp *my_qp, struct ehca_shca *shca)
  1005. {
  1006. u64 h_ret;
  1007. void *send_wqe_p, *recv_wqe_p;
  1008. int ret;
  1009. unsigned long flags;
  1010. int qp_num = my_qp->ib_qp.qp_num;
  1011. /* this hcall is not supported on base QPs */
  1012. if (my_qp->ext_type != EQPT_SRQBASE) {
  1013. /* get send and receive wqe pointer */
  1014. h_ret = hipz_h_disable_and_get_wqe(shca->ipz_hca_handle,
  1015. my_qp->ipz_qp_handle, &my_qp->pf,
  1016. &send_wqe_p, &recv_wqe_p, 4);
  1017. if (h_ret != H_SUCCESS) {
  1018. ehca_err(&shca->ib_device, "disable_and_get_wqe() "
  1019. "failed ehca_qp=%p qp_num=%x h_ret=%li",
  1020. my_qp, qp_num, h_ret);
  1021. return ehca2ib_return_code(h_ret);
  1022. }
  1023. /*
  1024. * acquire lock to ensure that nobody is polling the cq which
  1025. * could mean that the qmap->tail pointer is in an
  1026. * inconsistent state.
  1027. */
  1028. spin_lock_irqsave(&my_qp->send_cq->spinlock, flags);
  1029. ret = calc_left_cqes((u64)send_wqe_p, &my_qp->ipz_squeue,
  1030. &my_qp->sq_map);
  1031. spin_unlock_irqrestore(&my_qp->send_cq->spinlock, flags);
  1032. if (ret)
  1033. return ret;
  1034. spin_lock_irqsave(&my_qp->recv_cq->spinlock, flags);
  1035. ret = calc_left_cqes((u64)recv_wqe_p, &my_qp->ipz_rqueue,
  1036. &my_qp->rq_map);
  1037. spin_unlock_irqrestore(&my_qp->recv_cq->spinlock, flags);
  1038. if (ret)
  1039. return ret;
  1040. } else {
  1041. spin_lock_irqsave(&my_qp->send_cq->spinlock, flags);
  1042. my_qp->sq_map.left_to_poll = 0;
  1043. spin_unlock_irqrestore(&my_qp->send_cq->spinlock, flags);
  1044. spin_lock_irqsave(&my_qp->recv_cq->spinlock, flags);
  1045. my_qp->rq_map.left_to_poll = 0;
  1046. spin_unlock_irqrestore(&my_qp->recv_cq->spinlock, flags);
  1047. }
  1048. /* this assures flush cqes being generated only for pending wqes */
  1049. if ((my_qp->sq_map.left_to_poll == 0) &&
  1050. (my_qp->rq_map.left_to_poll == 0)) {
  1051. spin_lock_irqsave(&my_qp->send_cq->spinlock, flags);
  1052. ehca_add_to_err_list(my_qp, 1);
  1053. spin_unlock_irqrestore(&my_qp->send_cq->spinlock, flags);
  1054. if (HAS_RQ(my_qp)) {
  1055. spin_lock_irqsave(&my_qp->recv_cq->spinlock, flags);
  1056. ehca_add_to_err_list(my_qp, 0);
  1057. spin_unlock_irqrestore(&my_qp->recv_cq->spinlock,
  1058. flags);
  1059. }
  1060. }
  1061. return 0;
  1062. }
  1063. /*
  1064. * internal_modify_qp with circumvention to handle aqp0 properly
  1065. * smi_reset2init indicates if this is an internal reset-to-init-call for
  1066. * smi. This flag must always be zero if called from ehca_modify_qp()!
  1067. * This internal func was intorduced to avoid recursion of ehca_modify_qp()!
  1068. */
  1069. static int internal_modify_qp(struct ib_qp *ibqp,
  1070. struct ib_qp_attr *attr,
  1071. int attr_mask, int smi_reset2init)
  1072. {
  1073. enum ib_qp_state qp_cur_state, qp_new_state;
  1074. int cnt, qp_attr_idx, ret = 0;
  1075. enum ib_qp_statetrans statetrans;
  1076. struct hcp_modify_qp_control_block *mqpcb;
  1077. struct ehca_qp *my_qp = container_of(ibqp, struct ehca_qp, ib_qp);
  1078. struct ehca_shca *shca =
  1079. container_of(ibqp->pd->device, struct ehca_shca, ib_device);
  1080. u64 update_mask;
  1081. u64 h_ret;
  1082. int bad_wqe_cnt = 0;
  1083. int squeue_locked = 0;
  1084. unsigned long flags = 0;
  1085. /* do query_qp to obtain current attr values */
  1086. mqpcb = ehca_alloc_fw_ctrlblock(GFP_ATOMIC);
  1087. if (!mqpcb) {
  1088. ehca_err(ibqp->device, "Could not get zeroed page for mqpcb "
  1089. "ehca_qp=%p qp_num=%x ", my_qp, ibqp->qp_num);
  1090. return -ENOMEM;
  1091. }
  1092. h_ret = hipz_h_query_qp(shca->ipz_hca_handle,
  1093. my_qp->ipz_qp_handle,
  1094. &my_qp->pf,
  1095. mqpcb, my_qp->galpas.kernel);
  1096. if (h_ret != H_SUCCESS) {
  1097. ehca_err(ibqp->device, "hipz_h_query_qp() failed "
  1098. "ehca_qp=%p qp_num=%x h_ret=%li",
  1099. my_qp, ibqp->qp_num, h_ret);
  1100. ret = ehca2ib_return_code(h_ret);
  1101. goto modify_qp_exit1;
  1102. }
  1103. qp_cur_state = ehca2ib_qp_state(mqpcb->qp_state);
  1104. if (qp_cur_state == -EINVAL) { /* invalid qp state */
  1105. ret = -EINVAL;
  1106. ehca_err(ibqp->device, "Invalid current ehca_qp_state=%x "
  1107. "ehca_qp=%p qp_num=%x",
  1108. mqpcb->qp_state, my_qp, ibqp->qp_num);
  1109. goto modify_qp_exit1;
  1110. }
  1111. /*
  1112. * circumvention to set aqp0 initial state to init
  1113. * as expected by IB spec
  1114. */
  1115. if (smi_reset2init == 0 &&
  1116. ibqp->qp_type == IB_QPT_SMI &&
  1117. qp_cur_state == IB_QPS_RESET &&
  1118. (attr_mask & IB_QP_STATE) &&
  1119. attr->qp_state == IB_QPS_INIT) { /* RESET -> INIT */
  1120. struct ib_qp_attr smiqp_attr = {
  1121. .qp_state = IB_QPS_INIT,
  1122. .port_num = my_qp->init_attr.port_num,
  1123. .pkey_index = 0,
  1124. .qkey = 0
  1125. };
  1126. int smiqp_attr_mask = IB_QP_STATE | IB_QP_PORT |
  1127. IB_QP_PKEY_INDEX | IB_QP_QKEY;
  1128. int smirc = internal_modify_qp(
  1129. ibqp, &smiqp_attr, smiqp_attr_mask, 1);
  1130. if (smirc) {
  1131. ehca_err(ibqp->device, "SMI RESET -> INIT failed. "
  1132. "ehca_modify_qp() rc=%i", smirc);
  1133. ret = H_PARAMETER;
  1134. goto modify_qp_exit1;
  1135. }
  1136. qp_cur_state = IB_QPS_INIT;
  1137. ehca_dbg(ibqp->device, "SMI RESET -> INIT succeeded");
  1138. }
  1139. /* is transmitted current state equal to "real" current state */
  1140. if ((attr_mask & IB_QP_CUR_STATE) &&
  1141. qp_cur_state != attr->cur_qp_state) {
  1142. ret = -EINVAL;
  1143. ehca_err(ibqp->device,
  1144. "Invalid IB_QP_CUR_STATE attr->curr_qp_state=%x <>"
  1145. " actual cur_qp_state=%x. ehca_qp=%p qp_num=%x",
  1146. attr->cur_qp_state, qp_cur_state, my_qp, ibqp->qp_num);
  1147. goto modify_qp_exit1;
  1148. }
  1149. ehca_dbg(ibqp->device, "ehca_qp=%p qp_num=%x current qp_state=%x "
  1150. "new qp_state=%x attribute_mask=%x",
  1151. my_qp, ibqp->qp_num, qp_cur_state, attr->qp_state, attr_mask);
  1152. qp_new_state = attr_mask & IB_QP_STATE ? attr->qp_state : qp_cur_state;
  1153. if (!smi_reset2init &&
  1154. !ib_modify_qp_is_ok(qp_cur_state, qp_new_state, ibqp->qp_type,
  1155. attr_mask)) {
  1156. ret = -EINVAL;
  1157. ehca_err(ibqp->device,
  1158. "Invalid qp transition new_state=%x cur_state=%x "
  1159. "ehca_qp=%p qp_num=%x attr_mask=%x", qp_new_state,
  1160. qp_cur_state, my_qp, ibqp->qp_num, attr_mask);
  1161. goto modify_qp_exit1;
  1162. }
  1163. mqpcb->qp_state = ib2ehca_qp_state(qp_new_state);
  1164. if (mqpcb->qp_state)
  1165. update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_STATE, 1);
  1166. else {
  1167. ret = -EINVAL;
  1168. ehca_err(ibqp->device, "Invalid new qp state=%x "
  1169. "ehca_qp=%p qp_num=%x",
  1170. qp_new_state, my_qp, ibqp->qp_num);
  1171. goto modify_qp_exit1;
  1172. }
  1173. /* retrieve state transition struct to get req and opt attrs */
  1174. statetrans = get_modqp_statetrans(qp_cur_state, qp_new_state);
  1175. if (statetrans < 0) {
  1176. ret = -EINVAL;
  1177. ehca_err(ibqp->device, "<INVALID STATE CHANGE> qp_cur_state=%x "
  1178. "new_qp_state=%x State_xsition=%x ehca_qp=%p "
  1179. "qp_num=%x", qp_cur_state, qp_new_state,
  1180. statetrans, my_qp, ibqp->qp_num);
  1181. goto modify_qp_exit1;
  1182. }
  1183. qp_attr_idx = ib2ehcaqptype(ibqp->qp_type);
  1184. if (qp_attr_idx < 0) {
  1185. ret = qp_attr_idx;
  1186. ehca_err(ibqp->device,
  1187. "Invalid QP type=%x ehca_qp=%p qp_num=%x",
  1188. ibqp->qp_type, my_qp, ibqp->qp_num);
  1189. goto modify_qp_exit1;
  1190. }
  1191. ehca_dbg(ibqp->device,
  1192. "ehca_qp=%p qp_num=%x <VALID STATE CHANGE> qp_state_xsit=%x",
  1193. my_qp, ibqp->qp_num, statetrans);
  1194. /* eHCA2 rev2 and higher require the SEND_GRH_FLAG to be set
  1195. * in non-LL UD QPs.
  1196. */
  1197. if ((my_qp->qp_type == IB_QPT_UD) &&
  1198. (my_qp->ext_type != EQPT_LLQP) &&
  1199. (statetrans == IB_QPST_INIT2RTR) &&
  1200. (shca->hw_level >= 0x22)) {
  1201. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_GRH_FLAG, 1);
  1202. mqpcb->send_grh_flag = 1;
  1203. }
  1204. /* sqe -> rts: set purge bit of bad wqe before actual trans */
  1205. if ((my_qp->qp_type == IB_QPT_UD ||
  1206. my_qp->qp_type == IB_QPT_GSI ||
  1207. my_qp->qp_type == IB_QPT_SMI) &&
  1208. statetrans == IB_QPST_SQE2RTS) {
  1209. /* mark next free wqe if kernel */
  1210. if (!ibqp->uobject) {
  1211. struct ehca_wqe *wqe;
  1212. /* lock send queue */
  1213. spin_lock_irqsave(&my_qp->spinlock_s, flags);
  1214. squeue_locked = 1;
  1215. /* mark next free wqe */
  1216. wqe = (struct ehca_wqe *)
  1217. ipz_qeit_get(&my_qp->ipz_squeue);
  1218. wqe->optype = wqe->wqef = 0xff;
  1219. ehca_dbg(ibqp->device, "qp_num=%x next_free_wqe=%p",
  1220. ibqp->qp_num, wqe);
  1221. }
  1222. ret = prepare_sqe_rts(my_qp, shca, &bad_wqe_cnt);
  1223. if (ret) {
  1224. ehca_err(ibqp->device, "prepare_sqe_rts() failed "
  1225. "ehca_qp=%p qp_num=%x ret=%i",
  1226. my_qp, ibqp->qp_num, ret);
  1227. goto modify_qp_exit2;
  1228. }
  1229. }
  1230. /*
  1231. * enable RDMA_Atomic_Control if reset->init und reliable con
  1232. * this is necessary since gen2 does not provide that flag,
  1233. * but pHyp requires it
  1234. */
  1235. if (statetrans == IB_QPST_RESET2INIT &&
  1236. (ibqp->qp_type == IB_QPT_RC || ibqp->qp_type == IB_QPT_UC)) {
  1237. mqpcb->rdma_atomic_ctrl = 3;
  1238. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RDMA_ATOMIC_CTRL, 1);
  1239. }
  1240. /* circ. pHyp requires #RDMA/Atomic Resp Res for UC INIT -> RTR */
  1241. if (statetrans == IB_QPST_INIT2RTR &&
  1242. (ibqp->qp_type == IB_QPT_UC) &&
  1243. !(attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)) {
  1244. mqpcb->rdma_nr_atomic_resp_res = 1; /* default to 1 */
  1245. update_mask |=
  1246. EHCA_BMASK_SET(MQPCB_MASK_RDMA_NR_ATOMIC_RESP_RES, 1);
  1247. }
  1248. if (attr_mask & IB_QP_PKEY_INDEX) {
  1249. if (attr->pkey_index >= 16) {
  1250. ret = -EINVAL;
  1251. ehca_err(ibqp->device, "Invalid pkey_index=%x. "
  1252. "ehca_qp=%p qp_num=%x max_pkey_index=f",
  1253. attr->pkey_index, my_qp, ibqp->qp_num);
  1254. goto modify_qp_exit2;
  1255. }
  1256. mqpcb->prim_p_key_idx = attr->pkey_index;
  1257. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PRIM_P_KEY_IDX, 1);
  1258. }
  1259. if (attr_mask & IB_QP_PORT) {
  1260. struct ehca_sport *sport;
  1261. struct ehca_qp *aqp1;
  1262. if (attr->port_num < 1 || attr->port_num > shca->num_ports) {
  1263. ret = -EINVAL;
  1264. ehca_err(ibqp->device, "Invalid port=%x. "
  1265. "ehca_qp=%p qp_num=%x num_ports=%x",
  1266. attr->port_num, my_qp, ibqp->qp_num,
  1267. shca->num_ports);
  1268. goto modify_qp_exit2;
  1269. }
  1270. sport = &shca->sport[attr->port_num - 1];
  1271. if (!sport->ibqp_sqp[IB_QPT_GSI]) {
  1272. /* should not occur */
  1273. ret = -EFAULT;
  1274. ehca_err(ibqp->device, "AQP1 was not created for "
  1275. "port=%x", attr->port_num);
  1276. goto modify_qp_exit2;
  1277. }
  1278. aqp1 = container_of(sport->ibqp_sqp[IB_QPT_GSI],
  1279. struct ehca_qp, ib_qp);
  1280. if (ibqp->qp_type != IB_QPT_GSI &&
  1281. ibqp->qp_type != IB_QPT_SMI &&
  1282. aqp1->mod_qp_parm) {
  1283. /*
  1284. * firmware will reject this modify_qp() because
  1285. * port is not activated/initialized fully
  1286. */
  1287. ret = -EFAULT;
  1288. ehca_warn(ibqp->device, "Couldn't modify qp port=%x: "
  1289. "either port is being activated (try again) "
  1290. "or cabling issue", attr->port_num);
  1291. goto modify_qp_exit2;
  1292. }
  1293. mqpcb->prim_phys_port = attr->port_num;
  1294. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PRIM_PHYS_PORT, 1);
  1295. }
  1296. if (attr_mask & IB_QP_QKEY) {
  1297. mqpcb->qkey = attr->qkey;
  1298. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_QKEY, 1);
  1299. }
  1300. if (attr_mask & IB_QP_AV) {
  1301. mqpcb->dlid = attr->ah_attr.dlid;
  1302. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DLID, 1);
  1303. mqpcb->source_path_bits = attr->ah_attr.src_path_bits;
  1304. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SOURCE_PATH_BITS, 1);
  1305. mqpcb->service_level = attr->ah_attr.sl;
  1306. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SERVICE_LEVEL, 1);
  1307. if (ehca_calc_ipd(shca, mqpcb->prim_phys_port,
  1308. attr->ah_attr.static_rate,
  1309. &mqpcb->max_static_rate)) {
  1310. ret = -EINVAL;
  1311. goto modify_qp_exit2;
  1312. }
  1313. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_MAX_STATIC_RATE, 1);
  1314. /*
  1315. * Always supply the GRH flag, even if it's zero, to give the
  1316. * hypervisor a clear "yes" or "no" instead of a "perhaps"
  1317. */
  1318. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_GRH_FLAG, 1);
  1319. /*
  1320. * only if GRH is TRUE we might consider SOURCE_GID_IDX
  1321. * and DEST_GID otherwise phype will return H_ATTR_PARM!!!
  1322. */
  1323. if (attr->ah_attr.ah_flags == IB_AH_GRH) {
  1324. mqpcb->send_grh_flag = 1;
  1325. mqpcb->source_gid_idx = attr->ah_attr.grh.sgid_index;
  1326. update_mask |=
  1327. EHCA_BMASK_SET(MQPCB_MASK_SOURCE_GID_IDX, 1);
  1328. for (cnt = 0; cnt < 16; cnt++)
  1329. mqpcb->dest_gid.byte[cnt] =
  1330. attr->ah_attr.grh.dgid.raw[cnt];
  1331. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DEST_GID, 1);
  1332. mqpcb->flow_label = attr->ah_attr.grh.flow_label;
  1333. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_FLOW_LABEL, 1);
  1334. mqpcb->hop_limit = attr->ah_attr.grh.hop_limit;
  1335. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_HOP_LIMIT, 1);
  1336. mqpcb->traffic_class = attr->ah_attr.grh.traffic_class;
  1337. update_mask |=
  1338. EHCA_BMASK_SET(MQPCB_MASK_TRAFFIC_CLASS, 1);
  1339. }
  1340. }
  1341. if (attr_mask & IB_QP_PATH_MTU) {
  1342. /* store ld(MTU) */
  1343. my_qp->mtu_shift = attr->path_mtu + 7;
  1344. mqpcb->path_mtu = attr->path_mtu;
  1345. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PATH_MTU, 1);
  1346. }
  1347. if (attr_mask & IB_QP_TIMEOUT) {
  1348. mqpcb->timeout = attr->timeout;
  1349. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_TIMEOUT, 1);
  1350. }
  1351. if (attr_mask & IB_QP_RETRY_CNT) {
  1352. mqpcb->retry_count = attr->retry_cnt;
  1353. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RETRY_COUNT, 1);
  1354. }
  1355. if (attr_mask & IB_QP_RNR_RETRY) {
  1356. mqpcb->rnr_retry_count = attr->rnr_retry;
  1357. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RNR_RETRY_COUNT, 1);
  1358. }
  1359. if (attr_mask & IB_QP_RQ_PSN) {
  1360. mqpcb->receive_psn = attr->rq_psn;
  1361. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RECEIVE_PSN, 1);
  1362. }
  1363. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  1364. mqpcb->rdma_nr_atomic_resp_res = attr->max_dest_rd_atomic < 3 ?
  1365. attr->max_dest_rd_atomic : 2;
  1366. update_mask |=
  1367. EHCA_BMASK_SET(MQPCB_MASK_RDMA_NR_ATOMIC_RESP_RES, 1);
  1368. }
  1369. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  1370. mqpcb->rdma_atomic_outst_dest_qp = attr->max_rd_atomic < 3 ?
  1371. attr->max_rd_atomic : 2;
  1372. update_mask |=
  1373. EHCA_BMASK_SET
  1374. (MQPCB_MASK_RDMA_ATOMIC_OUTST_DEST_QP, 1);
  1375. }
  1376. if (attr_mask & IB_QP_ALT_PATH) {
  1377. if (attr->alt_port_num < 1
  1378. || attr->alt_port_num > shca->num_ports) {
  1379. ret = -EINVAL;
  1380. ehca_err(ibqp->device, "Invalid alt_port=%x. "
  1381. "ehca_qp=%p qp_num=%x num_ports=%x",
  1382. attr->alt_port_num, my_qp, ibqp->qp_num,
  1383. shca->num_ports);
  1384. goto modify_qp_exit2;
  1385. }
  1386. mqpcb->alt_phys_port = attr->alt_port_num;
  1387. if (attr->alt_pkey_index >= 16) {
  1388. ret = -EINVAL;
  1389. ehca_err(ibqp->device, "Invalid alt_pkey_index=%x. "
  1390. "ehca_qp=%p qp_num=%x max_pkey_index=f",
  1391. attr->pkey_index, my_qp, ibqp->qp_num);
  1392. goto modify_qp_exit2;
  1393. }
  1394. mqpcb->alt_p_key_idx = attr->alt_pkey_index;
  1395. mqpcb->timeout_al = attr->alt_timeout;
  1396. mqpcb->dlid_al = attr->alt_ah_attr.dlid;
  1397. mqpcb->source_path_bits_al = attr->alt_ah_attr.src_path_bits;
  1398. mqpcb->service_level_al = attr->alt_ah_attr.sl;
  1399. if (ehca_calc_ipd(shca, mqpcb->alt_phys_port,
  1400. attr->alt_ah_attr.static_rate,
  1401. &mqpcb->max_static_rate_al)) {
  1402. ret = -EINVAL;
  1403. goto modify_qp_exit2;
  1404. }
  1405. /* OpenIB doesn't support alternate retry counts - copy them */
  1406. mqpcb->retry_count_al = mqpcb->retry_count;
  1407. mqpcb->rnr_retry_count_al = mqpcb->rnr_retry_count;
  1408. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_ALT_PHYS_PORT, 1)
  1409. | EHCA_BMASK_SET(MQPCB_MASK_ALT_P_KEY_IDX, 1)
  1410. | EHCA_BMASK_SET(MQPCB_MASK_TIMEOUT_AL, 1)
  1411. | EHCA_BMASK_SET(MQPCB_MASK_DLID_AL, 1)
  1412. | EHCA_BMASK_SET(MQPCB_MASK_SOURCE_PATH_BITS_AL, 1)
  1413. | EHCA_BMASK_SET(MQPCB_MASK_SERVICE_LEVEL_AL, 1)
  1414. | EHCA_BMASK_SET(MQPCB_MASK_MAX_STATIC_RATE_AL, 1)
  1415. | EHCA_BMASK_SET(MQPCB_MASK_RETRY_COUNT_AL, 1)
  1416. | EHCA_BMASK_SET(MQPCB_MASK_RNR_RETRY_COUNT_AL, 1);
  1417. /*
  1418. * Always supply the GRH flag, even if it's zero, to give the
  1419. * hypervisor a clear "yes" or "no" instead of a "perhaps"
  1420. */
  1421. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_GRH_FLAG_AL, 1);
  1422. /*
  1423. * only if GRH is TRUE we might consider SOURCE_GID_IDX
  1424. * and DEST_GID otherwise phype will return H_ATTR_PARM!!!
  1425. */
  1426. if (attr->alt_ah_attr.ah_flags == IB_AH_GRH) {
  1427. mqpcb->send_grh_flag_al = 1;
  1428. for (cnt = 0; cnt < 16; cnt++)
  1429. mqpcb->dest_gid_al.byte[cnt] =
  1430. attr->alt_ah_attr.grh.dgid.raw[cnt];
  1431. mqpcb->source_gid_idx_al =
  1432. attr->alt_ah_attr.grh.sgid_index;
  1433. mqpcb->flow_label_al = attr->alt_ah_attr.grh.flow_label;
  1434. mqpcb->hop_limit_al = attr->alt_ah_attr.grh.hop_limit;
  1435. mqpcb->traffic_class_al =
  1436. attr->alt_ah_attr.grh.traffic_class;
  1437. update_mask |=
  1438. EHCA_BMASK_SET(MQPCB_MASK_SOURCE_GID_IDX_AL, 1)
  1439. | EHCA_BMASK_SET(MQPCB_MASK_DEST_GID_AL, 1)
  1440. | EHCA_BMASK_SET(MQPCB_MASK_FLOW_LABEL_AL, 1)
  1441. | EHCA_BMASK_SET(MQPCB_MASK_HOP_LIMIT_AL, 1) |
  1442. EHCA_BMASK_SET(MQPCB_MASK_TRAFFIC_CLASS_AL, 1);
  1443. }
  1444. }
  1445. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  1446. mqpcb->min_rnr_nak_timer_field = attr->min_rnr_timer;
  1447. update_mask |=
  1448. EHCA_BMASK_SET(MQPCB_MASK_MIN_RNR_NAK_TIMER_FIELD, 1);
  1449. }
  1450. if (attr_mask & IB_QP_SQ_PSN) {
  1451. mqpcb->send_psn = attr->sq_psn;
  1452. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_PSN, 1);
  1453. }
  1454. if (attr_mask & IB_QP_DEST_QPN) {
  1455. mqpcb->dest_qp_nr = attr->dest_qp_num;
  1456. update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DEST_QP_NR, 1);
  1457. }
  1458. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1459. if (attr->path_mig_state != IB_MIG_REARM
  1460. && attr->path_mig_state != IB_MIG_MIGRATED) {
  1461. ret = -EINVAL;
  1462. ehca_err(ibqp->device, "Invalid mig_state=%x",
  1463. attr->path_mig_state);
  1464. goto modify_qp_exit2;
  1465. }
  1466. mqpcb->path_migration_state = attr->path_mig_state + 1;
  1467. if (attr->path_mig_state == IB_MIG_REARM)
  1468. my_qp->mig_armed = 1;
  1469. update_mask |=
  1470. EHCA_BMASK_SET(MQPCB_MASK_PATH_MIGRATION_STATE, 1);
  1471. }
  1472. if (attr_mask & IB_QP_CAP) {
  1473. mqpcb->max_nr_outst_send_wr = attr->cap.max_send_wr+1;
  1474. update_mask |=
  1475. EHCA_BMASK_SET(MQPCB_MASK_MAX_NR_OUTST_SEND_WR, 1);
  1476. mqpcb->max_nr_outst_recv_wr = attr->cap.max_recv_wr+1;
  1477. update_mask |=
  1478. EHCA_BMASK_SET(MQPCB_MASK_MAX_NR_OUTST_RECV_WR, 1);
  1479. /* no support for max_send/recv_sge yet */
  1480. }
  1481. if (ehca_debug_level >= 2)
  1482. ehca_dmp(mqpcb, 4*70, "qp_num=%x", ibqp->qp_num);
  1483. h_ret = hipz_h_modify_qp(shca->ipz_hca_handle,
  1484. my_qp->ipz_qp_handle,
  1485. &my_qp->pf,
  1486. update_mask,
  1487. mqpcb, my_qp->galpas.kernel);
  1488. if (h_ret != H_SUCCESS) {
  1489. ret = ehca2ib_return_code(h_ret);
  1490. ehca_err(ibqp->device, "hipz_h_modify_qp() failed h_ret=%li "
  1491. "ehca_qp=%p qp_num=%x", h_ret, my_qp, ibqp->qp_num);
  1492. goto modify_qp_exit2;
  1493. }
  1494. if ((my_qp->qp_type == IB_QPT_UD ||
  1495. my_qp->qp_type == IB_QPT_GSI ||
  1496. my_qp->qp_type == IB_QPT_SMI) &&
  1497. statetrans == IB_QPST_SQE2RTS) {
  1498. /* doorbell to reprocessing wqes */
  1499. iosync(); /* serialize GAL register access */
  1500. hipz_update_sqa(my_qp, bad_wqe_cnt-1);
  1501. ehca_gen_dbg("doorbell for %x wqes", bad_wqe_cnt);
  1502. }
  1503. if (statetrans == IB_QPST_RESET2INIT ||
  1504. statetrans == IB_QPST_INIT2INIT) {
  1505. mqpcb->qp_enable = 1;
  1506. mqpcb->qp_state = EHCA_QPS_INIT;
  1507. update_mask = 0;
  1508. update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_ENABLE, 1);
  1509. h_ret = hipz_h_modify_qp(shca->ipz_hca_handle,
  1510. my_qp->ipz_qp_handle,
  1511. &my_qp->pf,
  1512. update_mask,
  1513. mqpcb,
  1514. my_qp->galpas.kernel);
  1515. if (h_ret != H_SUCCESS) {
  1516. ret = ehca2ib_return_code(h_ret);
  1517. ehca_err(ibqp->device, "ENABLE in context of "
  1518. "RESET_2_INIT failed! Maybe you didn't get "
  1519. "a LID h_ret=%li ehca_qp=%p qp_num=%x",
  1520. h_ret, my_qp, ibqp->qp_num);
  1521. goto modify_qp_exit2;
  1522. }
  1523. }
  1524. if ((qp_new_state == IB_QPS_ERR) && (qp_cur_state != IB_QPS_ERR)) {
  1525. ret = check_for_left_cqes(my_qp, shca);
  1526. if (ret)
  1527. goto modify_qp_exit2;
  1528. }
  1529. if (statetrans == IB_QPST_ANY2RESET) {
  1530. ipz_qeit_reset(&my_qp->ipz_rqueue);
  1531. ipz_qeit_reset(&my_qp->ipz_squeue);
  1532. if (qp_cur_state == IB_QPS_ERR) {
  1533. del_from_err_list(my_qp->send_cq, &my_qp->sq_err_node);
  1534. if (HAS_RQ(my_qp))
  1535. del_from_err_list(my_qp->recv_cq,
  1536. &my_qp->rq_err_node);
  1537. }
  1538. reset_queue_map(&my_qp->sq_map);
  1539. if (HAS_RQ(my_qp))
  1540. reset_queue_map(&my_qp->rq_map);
  1541. }
  1542. if (attr_mask & IB_QP_QKEY)
  1543. my_qp->qkey = attr->qkey;
  1544. modify_qp_exit2:
  1545. if (squeue_locked) { /* this means: sqe -> rts */
  1546. spin_unlock_irqrestore(&my_qp->spinlock_s, flags);
  1547. my_qp->sqerr_purgeflag = 1;
  1548. }
  1549. modify_qp_exit1:
  1550. ehca_free_fw_ctrlblock(mqpcb);
  1551. return ret;
  1552. }
  1553. int ehca_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int attr_mask,
  1554. struct ib_udata *udata)
  1555. {
  1556. int ret = 0;
  1557. struct ehca_shca *shca = container_of(ibqp->device, struct ehca_shca,
  1558. ib_device);
  1559. struct ehca_qp *my_qp = container_of(ibqp, struct ehca_qp, ib_qp);
  1560. /* The if-block below caches qp_attr to be modified for GSI and SMI
  1561. * qps during the initialization by ib_mad. When the respective port
  1562. * is activated, ie we got an event PORT_ACTIVE, we'll replay the
  1563. * cached modify calls sequence, see ehca_recover_sqs() below.
  1564. * Why that is required:
  1565. * 1) If one port is connected, older code requires that port one
  1566. * to be connected and module option nr_ports=1 to be given by
  1567. * user, which is very inconvenient for end user.
  1568. * 2) Firmware accepts modify_qp() only if respective port has become
  1569. * active. Older code had a wait loop of 30sec create_qp()/
  1570. * define_aqp1(), which is not appropriate in practice. This
  1571. * code now removes that wait loop, see define_aqp1(), and always
  1572. * reports all ports to ib_mad resp. users. Only activated ports
  1573. * will then usable for the users.
  1574. */
  1575. if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI) {
  1576. int port = my_qp->init_attr.port_num;
  1577. struct ehca_sport *sport = &shca->sport[port - 1];
  1578. unsigned long flags;
  1579. spin_lock_irqsave(&sport->mod_sqp_lock, flags);
  1580. /* cache qp_attr only during init */
  1581. if (my_qp->mod_qp_parm) {
  1582. struct ehca_mod_qp_parm *p;
  1583. if (my_qp->mod_qp_parm_idx >= EHCA_MOD_QP_PARM_MAX) {
  1584. ehca_err(&shca->ib_device,
  1585. "mod_qp_parm overflow state=%x port=%x"
  1586. " type=%x", attr->qp_state,
  1587. my_qp->init_attr.port_num,
  1588. ibqp->qp_type);
  1589. spin_unlock_irqrestore(&sport->mod_sqp_lock,
  1590. flags);
  1591. return -EINVAL;
  1592. }
  1593. p = &my_qp->mod_qp_parm[my_qp->mod_qp_parm_idx];
  1594. p->mask = attr_mask;
  1595. p->attr = *attr;
  1596. my_qp->mod_qp_parm_idx++;
  1597. ehca_dbg(&shca->ib_device,
  1598. "Saved qp_attr for state=%x port=%x type=%x",
  1599. attr->qp_state, my_qp->init_attr.port_num,
  1600. ibqp->qp_type);
  1601. spin_unlock_irqrestore(&sport->mod_sqp_lock, flags);
  1602. goto out;
  1603. }
  1604. spin_unlock_irqrestore(&sport->mod_sqp_lock, flags);
  1605. }
  1606. ret = internal_modify_qp(ibqp, attr, attr_mask, 0);
  1607. out:
  1608. if ((ret == 0) && (attr_mask & IB_QP_STATE))
  1609. my_qp->state = attr->qp_state;
  1610. return ret;
  1611. }
  1612. void ehca_recover_sqp(struct ib_qp *sqp)
  1613. {
  1614. struct ehca_qp *my_sqp = container_of(sqp, struct ehca_qp, ib_qp);
  1615. int port = my_sqp->init_attr.port_num;
  1616. struct ib_qp_attr attr;
  1617. struct ehca_mod_qp_parm *qp_parm;
  1618. int i, qp_parm_idx, ret;
  1619. unsigned long flags, wr_cnt;
  1620. if (!my_sqp->mod_qp_parm)
  1621. return;
  1622. ehca_dbg(sqp->device, "SQP port=%x qp_num=%x", port, sqp->qp_num);
  1623. qp_parm = my_sqp->mod_qp_parm;
  1624. qp_parm_idx = my_sqp->mod_qp_parm_idx;
  1625. for (i = 0; i < qp_parm_idx; i++) {
  1626. attr = qp_parm[i].attr;
  1627. ret = internal_modify_qp(sqp, &attr, qp_parm[i].mask, 0);
  1628. if (ret) {
  1629. ehca_err(sqp->device, "Could not modify SQP port=%x "
  1630. "qp_num=%x ret=%x", port, sqp->qp_num, ret);
  1631. goto free_qp_parm;
  1632. }
  1633. ehca_dbg(sqp->device, "SQP port=%x qp_num=%x in state=%x",
  1634. port, sqp->qp_num, attr.qp_state);
  1635. }
  1636. /* re-trigger posted recv wrs */
  1637. wr_cnt = my_sqp->ipz_rqueue.current_q_offset /
  1638. my_sqp->ipz_rqueue.qe_size;
  1639. if (wr_cnt) {
  1640. spin_lock_irqsave(&my_sqp->spinlock_r, flags);
  1641. hipz_update_rqa(my_sqp, wr_cnt);
  1642. spin_unlock_irqrestore(&my_sqp->spinlock_r, flags);
  1643. ehca_dbg(sqp->device, "doorbell port=%x qp_num=%x wr_cnt=%lx",
  1644. port, sqp->qp_num, wr_cnt);
  1645. }
  1646. free_qp_parm:
  1647. kfree(qp_parm);
  1648. /* this prevents subsequent calls to modify_qp() to cache qp_attr */
  1649. my_sqp->mod_qp_parm = NULL;
  1650. }
  1651. int ehca_query_qp(struct ib_qp *qp,
  1652. struct ib_qp_attr *qp_attr,
  1653. int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
  1654. {
  1655. struct ehca_qp *my_qp = container_of(qp, struct ehca_qp, ib_qp);
  1656. struct ehca_shca *shca = container_of(qp->device, struct ehca_shca,
  1657. ib_device);
  1658. struct ipz_adapter_handle adapter_handle = shca->ipz_hca_handle;
  1659. struct hcp_modify_qp_control_block *qpcb;
  1660. int cnt, ret = 0;
  1661. u64 h_ret;
  1662. if (qp_attr_mask & QP_ATTR_QUERY_NOT_SUPPORTED) {
  1663. ehca_err(qp->device, "Invalid attribute mask "
  1664. "ehca_qp=%p qp_num=%x qp_attr_mask=%x ",
  1665. my_qp, qp->qp_num, qp_attr_mask);
  1666. return -EINVAL;
  1667. }
  1668. qpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  1669. if (!qpcb) {
  1670. ehca_err(qp->device, "Out of memory for qpcb "
  1671. "ehca_qp=%p qp_num=%x", my_qp, qp->qp_num);
  1672. return -ENOMEM;
  1673. }
  1674. h_ret = hipz_h_query_qp(adapter_handle,
  1675. my_qp->ipz_qp_handle,
  1676. &my_qp->pf,
  1677. qpcb, my_qp->galpas.kernel);
  1678. if (h_ret != H_SUCCESS) {
  1679. ret = ehca2ib_return_code(h_ret);
  1680. ehca_err(qp->device, "hipz_h_query_qp() failed "
  1681. "ehca_qp=%p qp_num=%x h_ret=%li",
  1682. my_qp, qp->qp_num, h_ret);
  1683. goto query_qp_exit1;
  1684. }
  1685. qp_attr->cur_qp_state = ehca2ib_qp_state(qpcb->qp_state);
  1686. qp_attr->qp_state = qp_attr->cur_qp_state;
  1687. if (qp_attr->cur_qp_state == -EINVAL) {
  1688. ret = -EINVAL;
  1689. ehca_err(qp->device, "Got invalid ehca_qp_state=%x "
  1690. "ehca_qp=%p qp_num=%x",
  1691. qpcb->qp_state, my_qp, qp->qp_num);
  1692. goto query_qp_exit1;
  1693. }
  1694. if (qp_attr->qp_state == IB_QPS_SQD)
  1695. qp_attr->sq_draining = 1;
  1696. qp_attr->qkey = qpcb->qkey;
  1697. qp_attr->path_mtu = qpcb->path_mtu;
  1698. qp_attr->path_mig_state = qpcb->path_migration_state - 1;
  1699. qp_attr->rq_psn = qpcb->receive_psn;
  1700. qp_attr->sq_psn = qpcb->send_psn;
  1701. qp_attr->min_rnr_timer = qpcb->min_rnr_nak_timer_field;
  1702. qp_attr->cap.max_send_wr = qpcb->max_nr_outst_send_wr-1;
  1703. qp_attr->cap.max_recv_wr = qpcb->max_nr_outst_recv_wr-1;
  1704. /* UD_AV CIRCUMVENTION */
  1705. if (my_qp->qp_type == IB_QPT_UD) {
  1706. qp_attr->cap.max_send_sge =
  1707. qpcb->actual_nr_sges_in_sq_wqe - 2;
  1708. qp_attr->cap.max_recv_sge =
  1709. qpcb->actual_nr_sges_in_rq_wqe - 2;
  1710. } else {
  1711. qp_attr->cap.max_send_sge =
  1712. qpcb->actual_nr_sges_in_sq_wqe;
  1713. qp_attr->cap.max_recv_sge =
  1714. qpcb->actual_nr_sges_in_rq_wqe;
  1715. }
  1716. qp_attr->cap.max_inline_data = my_qp->sq_max_inline_data_size;
  1717. qp_attr->dest_qp_num = qpcb->dest_qp_nr;
  1718. qp_attr->pkey_index =
  1719. EHCA_BMASK_GET(MQPCB_PRIM_P_KEY_IDX, qpcb->prim_p_key_idx);
  1720. qp_attr->port_num =
  1721. EHCA_BMASK_GET(MQPCB_PRIM_PHYS_PORT, qpcb->prim_phys_port);
  1722. qp_attr->timeout = qpcb->timeout;
  1723. qp_attr->retry_cnt = qpcb->retry_count;
  1724. qp_attr->rnr_retry = qpcb->rnr_retry_count;
  1725. qp_attr->alt_pkey_index =
  1726. EHCA_BMASK_GET(MQPCB_PRIM_P_KEY_IDX, qpcb->alt_p_key_idx);
  1727. qp_attr->alt_port_num = qpcb->alt_phys_port;
  1728. qp_attr->alt_timeout = qpcb->timeout_al;
  1729. qp_attr->max_dest_rd_atomic = qpcb->rdma_nr_atomic_resp_res;
  1730. qp_attr->max_rd_atomic = qpcb->rdma_atomic_outst_dest_qp;
  1731. /* primary av */
  1732. qp_attr->ah_attr.sl = qpcb->service_level;
  1733. if (qpcb->send_grh_flag) {
  1734. qp_attr->ah_attr.ah_flags = IB_AH_GRH;
  1735. }
  1736. qp_attr->ah_attr.static_rate = qpcb->max_static_rate;
  1737. qp_attr->ah_attr.dlid = qpcb->dlid;
  1738. qp_attr->ah_attr.src_path_bits = qpcb->source_path_bits;
  1739. qp_attr->ah_attr.port_num = qp_attr->port_num;
  1740. /* primary GRH */
  1741. qp_attr->ah_attr.grh.traffic_class = qpcb->traffic_class;
  1742. qp_attr->ah_attr.grh.hop_limit = qpcb->hop_limit;
  1743. qp_attr->ah_attr.grh.sgid_index = qpcb->source_gid_idx;
  1744. qp_attr->ah_attr.grh.flow_label = qpcb->flow_label;
  1745. for (cnt = 0; cnt < 16; cnt++)
  1746. qp_attr->ah_attr.grh.dgid.raw[cnt] =
  1747. qpcb->dest_gid.byte[cnt];
  1748. /* alternate AV */
  1749. qp_attr->alt_ah_attr.sl = qpcb->service_level_al;
  1750. if (qpcb->send_grh_flag_al) {
  1751. qp_attr->alt_ah_attr.ah_flags = IB_AH_GRH;
  1752. }
  1753. qp_attr->alt_ah_attr.static_rate = qpcb->max_static_rate_al;
  1754. qp_attr->alt_ah_attr.dlid = qpcb->dlid_al;
  1755. qp_attr->alt_ah_attr.src_path_bits = qpcb->source_path_bits_al;
  1756. /* alternate GRH */
  1757. qp_attr->alt_ah_attr.grh.traffic_class = qpcb->traffic_class_al;
  1758. qp_attr->alt_ah_attr.grh.hop_limit = qpcb->hop_limit_al;
  1759. qp_attr->alt_ah_attr.grh.sgid_index = qpcb->source_gid_idx_al;
  1760. qp_attr->alt_ah_attr.grh.flow_label = qpcb->flow_label_al;
  1761. for (cnt = 0; cnt < 16; cnt++)
  1762. qp_attr->alt_ah_attr.grh.dgid.raw[cnt] =
  1763. qpcb->dest_gid_al.byte[cnt];
  1764. /* return init attributes given in ehca_create_qp */
  1765. if (qp_init_attr)
  1766. *qp_init_attr = my_qp->init_attr;
  1767. if (ehca_debug_level >= 2)
  1768. ehca_dmp(qpcb, 4*70, "qp_num=%x", qp->qp_num);
  1769. query_qp_exit1:
  1770. ehca_free_fw_ctrlblock(qpcb);
  1771. return ret;
  1772. }
  1773. int ehca_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  1774. enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
  1775. {
  1776. struct ehca_qp *my_qp =
  1777. container_of(ibsrq, struct ehca_qp, ib_srq);
  1778. struct ehca_shca *shca =
  1779. container_of(ibsrq->pd->device, struct ehca_shca, ib_device);
  1780. struct hcp_modify_qp_control_block *mqpcb;
  1781. u64 update_mask;
  1782. u64 h_ret;
  1783. int ret = 0;
  1784. mqpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  1785. if (!mqpcb) {
  1786. ehca_err(ibsrq->device, "Could not get zeroed page for mqpcb "
  1787. "ehca_qp=%p qp_num=%x ", my_qp, my_qp->real_qp_num);
  1788. return -ENOMEM;
  1789. }
  1790. update_mask = 0;
  1791. if (attr_mask & IB_SRQ_LIMIT) {
  1792. attr_mask &= ~IB_SRQ_LIMIT;
  1793. update_mask |=
  1794. EHCA_BMASK_SET(MQPCB_MASK_CURR_SRQ_LIMIT, 1)
  1795. | EHCA_BMASK_SET(MQPCB_MASK_QP_AFF_ASYN_EV_LOG_REG, 1);
  1796. mqpcb->curr_srq_limit =
  1797. EHCA_BMASK_SET(MQPCB_CURR_SRQ_LIMIT, attr->srq_limit);
  1798. mqpcb->qp_aff_asyn_ev_log_reg =
  1799. EHCA_BMASK_SET(QPX_AAELOG_RESET_SRQ_LIMIT, 1);
  1800. }
  1801. /* by now, all bits in attr_mask should have been cleared */
  1802. if (attr_mask) {
  1803. ehca_err(ibsrq->device, "invalid attribute mask bits set "
  1804. "attr_mask=%x", attr_mask);
  1805. ret = -EINVAL;
  1806. goto modify_srq_exit0;
  1807. }
  1808. if (ehca_debug_level >= 2)
  1809. ehca_dmp(mqpcb, 4*70, "qp_num=%x", my_qp->real_qp_num);
  1810. h_ret = hipz_h_modify_qp(shca->ipz_hca_handle, my_qp->ipz_qp_handle,
  1811. NULL, update_mask, mqpcb,
  1812. my_qp->galpas.kernel);
  1813. if (h_ret != H_SUCCESS) {
  1814. ret = ehca2ib_return_code(h_ret);
  1815. ehca_err(ibsrq->device, "hipz_h_modify_qp() failed h_ret=%li "
  1816. "ehca_qp=%p qp_num=%x",
  1817. h_ret, my_qp, my_qp->real_qp_num);
  1818. }
  1819. modify_srq_exit0:
  1820. ehca_free_fw_ctrlblock(mqpcb);
  1821. return ret;
  1822. }
  1823. int ehca_query_srq(struct ib_srq *srq, struct ib_srq_attr *srq_attr)
  1824. {
  1825. struct ehca_qp *my_qp = container_of(srq, struct ehca_qp, ib_srq);
  1826. struct ehca_shca *shca = container_of(srq->device, struct ehca_shca,
  1827. ib_device);
  1828. struct ipz_adapter_handle adapter_handle = shca->ipz_hca_handle;
  1829. struct hcp_modify_qp_control_block *qpcb;
  1830. int ret = 0;
  1831. u64 h_ret;
  1832. qpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
  1833. if (!qpcb) {
  1834. ehca_err(srq->device, "Out of memory for qpcb "
  1835. "ehca_qp=%p qp_num=%x", my_qp, my_qp->real_qp_num);
  1836. return -ENOMEM;
  1837. }
  1838. h_ret = hipz_h_query_qp(adapter_handle, my_qp->ipz_qp_handle,
  1839. NULL, qpcb, my_qp->galpas.kernel);
  1840. if (h_ret != H_SUCCESS) {
  1841. ret = ehca2ib_return_code(h_ret);
  1842. ehca_err(srq->device, "hipz_h_query_qp() failed "
  1843. "ehca_qp=%p qp_num=%x h_ret=%li",
  1844. my_qp, my_qp->real_qp_num, h_ret);
  1845. goto query_srq_exit1;
  1846. }
  1847. srq_attr->max_wr = qpcb->max_nr_outst_recv_wr - 1;
  1848. srq_attr->max_sge = 3;
  1849. srq_attr->srq_limit = EHCA_BMASK_GET(
  1850. MQPCB_CURR_SRQ_LIMIT, qpcb->curr_srq_limit);
  1851. if (ehca_debug_level >= 2)
  1852. ehca_dmp(qpcb, 4*70, "qp_num=%x", my_qp->real_qp_num);
  1853. query_srq_exit1:
  1854. ehca_free_fw_ctrlblock(qpcb);
  1855. return ret;
  1856. }
  1857. static int internal_destroy_qp(struct ib_device *dev, struct ehca_qp *my_qp,
  1858. struct ib_uobject *uobject)
  1859. {
  1860. struct ehca_shca *shca = container_of(dev, struct ehca_shca, ib_device);
  1861. struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd,
  1862. ib_pd);
  1863. struct ehca_sport *sport = &shca->sport[my_qp->init_attr.port_num - 1];
  1864. u32 qp_num = my_qp->real_qp_num;
  1865. int ret;
  1866. u64 h_ret;
  1867. u8 port_num;
  1868. enum ib_qp_type qp_type;
  1869. unsigned long flags;
  1870. if (uobject) {
  1871. if (my_qp->mm_count_galpa ||
  1872. my_qp->mm_count_rqueue || my_qp->mm_count_squeue) {
  1873. ehca_err(dev, "Resources still referenced in "
  1874. "user space qp_num=%x", qp_num);
  1875. return -EINVAL;
  1876. }
  1877. }
  1878. if (my_qp->send_cq) {
  1879. ret = ehca_cq_unassign_qp(my_qp->send_cq, qp_num);
  1880. if (ret) {
  1881. ehca_err(dev, "Couldn't unassign qp from "
  1882. "send_cq ret=%i qp_num=%x cq_num=%x", ret,
  1883. qp_num, my_qp->send_cq->cq_number);
  1884. return ret;
  1885. }
  1886. }
  1887. write_lock_irqsave(&ehca_qp_idr_lock, flags);
  1888. idr_remove(&ehca_qp_idr, my_qp->token);
  1889. write_unlock_irqrestore(&ehca_qp_idr_lock, flags);
  1890. /*
  1891. * SRQs will never get into an error list and do not have a recv_cq,
  1892. * so we need to skip them here.
  1893. */
  1894. if (HAS_RQ(my_qp) && !IS_SRQ(my_qp))
  1895. del_from_err_list(my_qp->recv_cq, &my_qp->rq_err_node);
  1896. if (HAS_SQ(my_qp))
  1897. del_from_err_list(my_qp->send_cq, &my_qp->sq_err_node);
  1898. /* now wait until all pending events have completed */
  1899. wait_event(my_qp->wait_completion, !atomic_read(&my_qp->nr_events));
  1900. h_ret = hipz_h_destroy_qp(shca->ipz_hca_handle, my_qp);
  1901. if (h_ret != H_SUCCESS) {
  1902. ehca_err(dev, "hipz_h_destroy_qp() failed h_ret=%li "
  1903. "ehca_qp=%p qp_num=%x", h_ret, my_qp, qp_num);
  1904. return ehca2ib_return_code(h_ret);
  1905. }
  1906. port_num = my_qp->init_attr.port_num;
  1907. qp_type = my_qp->init_attr.qp_type;
  1908. if (qp_type == IB_QPT_SMI || qp_type == IB_QPT_GSI) {
  1909. spin_lock_irqsave(&sport->mod_sqp_lock, flags);
  1910. kfree(my_qp->mod_qp_parm);
  1911. my_qp->mod_qp_parm = NULL;
  1912. shca->sport[port_num - 1].ibqp_sqp[qp_type] = NULL;
  1913. spin_unlock_irqrestore(&sport->mod_sqp_lock, flags);
  1914. }
  1915. /* no support for IB_QPT_SMI yet */
  1916. if (qp_type == IB_QPT_GSI) {
  1917. struct ib_event event;
  1918. ehca_info(dev, "device %s: port %x is inactive.",
  1919. shca->ib_device.name, port_num);
  1920. event.device = &shca->ib_device;
  1921. event.event = IB_EVENT_PORT_ERR;
  1922. event.element.port_num = port_num;
  1923. shca->sport[port_num - 1].port_state = IB_PORT_DOWN;
  1924. ib_dispatch_event(&event);
  1925. }
  1926. if (HAS_RQ(my_qp)) {
  1927. ipz_queue_dtor(my_pd, &my_qp->ipz_rqueue);
  1928. vfree(my_qp->rq_map.map);
  1929. }
  1930. if (HAS_SQ(my_qp)) {
  1931. ipz_queue_dtor(my_pd, &my_qp->ipz_squeue);
  1932. vfree(my_qp->sq_map.map);
  1933. }
  1934. kmem_cache_free(qp_cache, my_qp);
  1935. atomic_dec(&shca->num_qps);
  1936. return 0;
  1937. }
  1938. int ehca_destroy_qp(struct ib_qp *qp)
  1939. {
  1940. return internal_destroy_qp(qp->device,
  1941. container_of(qp, struct ehca_qp, ib_qp),
  1942. qp->uobject);
  1943. }
  1944. int ehca_destroy_srq(struct ib_srq *srq)
  1945. {
  1946. return internal_destroy_qp(srq->device,
  1947. container_of(srq, struct ehca_qp, ib_srq),
  1948. srq->uobject);
  1949. }
  1950. int ehca_init_qp_cache(void)
  1951. {
  1952. qp_cache = kmem_cache_create("ehca_cache_qp",
  1953. sizeof(struct ehca_qp), 0,
  1954. SLAB_HWCACHE_ALIGN,
  1955. NULL);
  1956. if (!qp_cache)
  1957. return -ENOMEM;
  1958. return 0;
  1959. }
  1960. void ehca_cleanup_qp_cache(void)
  1961. {
  1962. if (qp_cache)
  1963. kmem_cache_destroy(qp_cache);
  1964. }