ehca_qp.c 62 KB

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