ehca_qp.c 63 KB

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