ehca_qp.c 63 KB

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