ehca_qp.c 63 KB

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