qp.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  1. /*
  2. * Copyright(c) 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/hash.h>
  48. #include <linux/bitops.h>
  49. #include <linux/lockdep.h>
  50. #include <linux/vmalloc.h>
  51. #include <linux/slab.h>
  52. #include <rdma/ib_verbs.h>
  53. #include <rdma/ib_hdrs.h>
  54. #include "qp.h"
  55. #include "vt.h"
  56. #include "trace.h"
  57. static void rvt_rc_timeout(unsigned long arg);
  58. /*
  59. * Convert the AETH RNR timeout code into the number of microseconds.
  60. */
  61. static const u32 ib_rvt_rnr_table[32] = {
  62. 655360, /* 00: 655.36 */
  63. 10, /* 01: .01 */
  64. 20, /* 02 .02 */
  65. 30, /* 03: .03 */
  66. 40, /* 04: .04 */
  67. 60, /* 05: .06 */
  68. 80, /* 06: .08 */
  69. 120, /* 07: .12 */
  70. 160, /* 08: .16 */
  71. 240, /* 09: .24 */
  72. 320, /* 0A: .32 */
  73. 480, /* 0B: .48 */
  74. 640, /* 0C: .64 */
  75. 960, /* 0D: .96 */
  76. 1280, /* 0E: 1.28 */
  77. 1920, /* 0F: 1.92 */
  78. 2560, /* 10: 2.56 */
  79. 3840, /* 11: 3.84 */
  80. 5120, /* 12: 5.12 */
  81. 7680, /* 13: 7.68 */
  82. 10240, /* 14: 10.24 */
  83. 15360, /* 15: 15.36 */
  84. 20480, /* 16: 20.48 */
  85. 30720, /* 17: 30.72 */
  86. 40960, /* 18: 40.96 */
  87. 61440, /* 19: 61.44 */
  88. 81920, /* 1A: 81.92 */
  89. 122880, /* 1B: 122.88 */
  90. 163840, /* 1C: 163.84 */
  91. 245760, /* 1D: 245.76 */
  92. 327680, /* 1E: 327.68 */
  93. 491520 /* 1F: 491.52 */
  94. };
  95. /*
  96. * Note that it is OK to post send work requests in the SQE and ERR
  97. * states; rvt_do_send() will process them and generate error
  98. * completions as per IB 1.2 C10-96.
  99. */
  100. const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
  101. [IB_QPS_RESET] = 0,
  102. [IB_QPS_INIT] = RVT_POST_RECV_OK,
  103. [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
  104. [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  105. RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
  106. RVT_PROCESS_NEXT_SEND_OK,
  107. [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  108. RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
  109. [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  110. RVT_POST_SEND_OK | RVT_FLUSH_SEND,
  111. [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
  112. RVT_POST_SEND_OK | RVT_FLUSH_SEND,
  113. };
  114. EXPORT_SYMBOL(ib_rvt_state_ops);
  115. /*
  116. * Translate ib_wr_opcode into ib_wc_opcode.
  117. */
  118. const enum ib_wc_opcode ib_rvt_wc_opcode[] = {
  119. [IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
  120. [IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
  121. [IB_WR_SEND] = IB_WC_SEND,
  122. [IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
  123. [IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
  124. [IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
  125. [IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD,
  126. [IB_WR_SEND_WITH_INV] = IB_WC_SEND,
  127. [IB_WR_LOCAL_INV] = IB_WC_LOCAL_INV,
  128. [IB_WR_REG_MR] = IB_WC_REG_MR
  129. };
  130. EXPORT_SYMBOL(ib_rvt_wc_opcode);
  131. static void get_map_page(struct rvt_qpn_table *qpt,
  132. struct rvt_qpn_map *map,
  133. gfp_t gfp)
  134. {
  135. unsigned long page = get_zeroed_page(gfp);
  136. /*
  137. * Free the page if someone raced with us installing it.
  138. */
  139. spin_lock(&qpt->lock);
  140. if (map->page)
  141. free_page(page);
  142. else
  143. map->page = (void *)page;
  144. spin_unlock(&qpt->lock);
  145. }
  146. /**
  147. * init_qpn_table - initialize the QP number table for a device
  148. * @qpt: the QPN table
  149. */
  150. static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
  151. {
  152. u32 offset, i;
  153. struct rvt_qpn_map *map;
  154. int ret = 0;
  155. if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
  156. return -EINVAL;
  157. spin_lock_init(&qpt->lock);
  158. qpt->last = rdi->dparms.qpn_start;
  159. qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
  160. /*
  161. * Drivers may want some QPs beyond what we need for verbs let them use
  162. * our qpn table. No need for two. Lets go ahead and mark the bitmaps
  163. * for those. The reserved range must be *after* the range which verbs
  164. * will pick from.
  165. */
  166. /* Figure out number of bit maps needed before reserved range */
  167. qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
  168. /* This should always be zero */
  169. offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
  170. /* Starting with the first reserved bit map */
  171. map = &qpt->map[qpt->nmaps];
  172. rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
  173. rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
  174. for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
  175. if (!map->page) {
  176. get_map_page(qpt, map, GFP_KERNEL);
  177. if (!map->page) {
  178. ret = -ENOMEM;
  179. break;
  180. }
  181. }
  182. set_bit(offset, map->page);
  183. offset++;
  184. if (offset == RVT_BITS_PER_PAGE) {
  185. /* next page */
  186. qpt->nmaps++;
  187. map++;
  188. offset = 0;
  189. }
  190. }
  191. return ret;
  192. }
  193. /**
  194. * free_qpn_table - free the QP number table for a device
  195. * @qpt: the QPN table
  196. */
  197. static void free_qpn_table(struct rvt_qpn_table *qpt)
  198. {
  199. int i;
  200. for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
  201. free_page((unsigned long)qpt->map[i].page);
  202. }
  203. /**
  204. * rvt_driver_qp_init - Init driver qp resources
  205. * @rdi: rvt dev strucutre
  206. *
  207. * Return: 0 on success
  208. */
  209. int rvt_driver_qp_init(struct rvt_dev_info *rdi)
  210. {
  211. int i;
  212. int ret = -ENOMEM;
  213. if (!rdi->dparms.qp_table_size)
  214. return -EINVAL;
  215. /*
  216. * If driver is not doing any QP allocation then make sure it is
  217. * providing the necessary QP functions.
  218. */
  219. if (!rdi->driver_f.free_all_qps ||
  220. !rdi->driver_f.qp_priv_alloc ||
  221. !rdi->driver_f.qp_priv_free ||
  222. !rdi->driver_f.notify_qp_reset ||
  223. !rdi->driver_f.notify_restart_rc)
  224. return -EINVAL;
  225. /* allocate parent object */
  226. rdi->qp_dev = kzalloc_node(sizeof(*rdi->qp_dev), GFP_KERNEL,
  227. rdi->dparms.node);
  228. if (!rdi->qp_dev)
  229. return -ENOMEM;
  230. /* allocate hash table */
  231. rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
  232. rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
  233. rdi->qp_dev->qp_table =
  234. kmalloc_node(rdi->qp_dev->qp_table_size *
  235. sizeof(*rdi->qp_dev->qp_table),
  236. GFP_KERNEL, rdi->dparms.node);
  237. if (!rdi->qp_dev->qp_table)
  238. goto no_qp_table;
  239. for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
  240. RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
  241. spin_lock_init(&rdi->qp_dev->qpt_lock);
  242. /* initialize qpn map */
  243. if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
  244. goto fail_table;
  245. spin_lock_init(&rdi->n_qps_lock);
  246. return 0;
  247. fail_table:
  248. kfree(rdi->qp_dev->qp_table);
  249. free_qpn_table(&rdi->qp_dev->qpn_table);
  250. no_qp_table:
  251. kfree(rdi->qp_dev);
  252. return ret;
  253. }
  254. /**
  255. * free_all_qps - check for QPs still in use
  256. * @qpt: the QP table to empty
  257. *
  258. * There should not be any QPs still in use.
  259. * Free memory for table.
  260. */
  261. static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
  262. {
  263. unsigned long flags;
  264. struct rvt_qp *qp;
  265. unsigned n, qp_inuse = 0;
  266. spinlock_t *ql; /* work around too long line below */
  267. if (rdi->driver_f.free_all_qps)
  268. qp_inuse = rdi->driver_f.free_all_qps(rdi);
  269. qp_inuse += rvt_mcast_tree_empty(rdi);
  270. if (!rdi->qp_dev)
  271. return qp_inuse;
  272. ql = &rdi->qp_dev->qpt_lock;
  273. spin_lock_irqsave(ql, flags);
  274. for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
  275. qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
  276. lockdep_is_held(ql));
  277. RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
  278. for (; qp; qp = rcu_dereference_protected(qp->next,
  279. lockdep_is_held(ql)))
  280. qp_inuse++;
  281. }
  282. spin_unlock_irqrestore(ql, flags);
  283. synchronize_rcu();
  284. return qp_inuse;
  285. }
  286. /**
  287. * rvt_qp_exit - clean up qps on device exit
  288. * @rdi: rvt dev structure
  289. *
  290. * Check for qp leaks and free resources.
  291. */
  292. void rvt_qp_exit(struct rvt_dev_info *rdi)
  293. {
  294. u32 qps_inuse = rvt_free_all_qps(rdi);
  295. if (qps_inuse)
  296. rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
  297. qps_inuse);
  298. if (!rdi->qp_dev)
  299. return;
  300. kfree(rdi->qp_dev->qp_table);
  301. free_qpn_table(&rdi->qp_dev->qpn_table);
  302. kfree(rdi->qp_dev);
  303. }
  304. static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
  305. struct rvt_qpn_map *map, unsigned off)
  306. {
  307. return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
  308. }
  309. /**
  310. * alloc_qpn - Allocate the next available qpn or zero/one for QP type
  311. * IB_QPT_SMI/IB_QPT_GSI
  312. *@rdi: rvt device info structure
  313. *@qpt: queue pair number table pointer
  314. *@port_num: IB port number, 1 based, comes from core
  315. *
  316. * Return: The queue pair number
  317. */
  318. static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
  319. enum ib_qp_type type, u8 port_num, gfp_t gfp)
  320. {
  321. u32 i, offset, max_scan, qpn;
  322. struct rvt_qpn_map *map;
  323. u32 ret;
  324. if (rdi->driver_f.alloc_qpn)
  325. return rdi->driver_f.alloc_qpn(rdi, qpt, type, port_num, gfp);
  326. if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
  327. unsigned n;
  328. ret = type == IB_QPT_GSI;
  329. n = 1 << (ret + 2 * (port_num - 1));
  330. spin_lock(&qpt->lock);
  331. if (qpt->flags & n)
  332. ret = -EINVAL;
  333. else
  334. qpt->flags |= n;
  335. spin_unlock(&qpt->lock);
  336. goto bail;
  337. }
  338. qpn = qpt->last + qpt->incr;
  339. if (qpn >= RVT_QPN_MAX)
  340. qpn = qpt->incr | ((qpt->last & 1) ^ 1);
  341. /* offset carries bit 0 */
  342. offset = qpn & RVT_BITS_PER_PAGE_MASK;
  343. map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
  344. max_scan = qpt->nmaps - !offset;
  345. for (i = 0;;) {
  346. if (unlikely(!map->page)) {
  347. get_map_page(qpt, map, gfp);
  348. if (unlikely(!map->page))
  349. break;
  350. }
  351. do {
  352. if (!test_and_set_bit(offset, map->page)) {
  353. qpt->last = qpn;
  354. ret = qpn;
  355. goto bail;
  356. }
  357. offset += qpt->incr;
  358. /*
  359. * This qpn might be bogus if offset >= BITS_PER_PAGE.
  360. * That is OK. It gets re-assigned below
  361. */
  362. qpn = mk_qpn(qpt, map, offset);
  363. } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
  364. /*
  365. * In order to keep the number of pages allocated to a
  366. * minimum, we scan the all existing pages before increasing
  367. * the size of the bitmap table.
  368. */
  369. if (++i > max_scan) {
  370. if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
  371. break;
  372. map = &qpt->map[qpt->nmaps++];
  373. /* start at incr with current bit 0 */
  374. offset = qpt->incr | (offset & 1);
  375. } else if (map < &qpt->map[qpt->nmaps]) {
  376. ++map;
  377. /* start at incr with current bit 0 */
  378. offset = qpt->incr | (offset & 1);
  379. } else {
  380. map = &qpt->map[0];
  381. /* wrap to first map page, invert bit 0 */
  382. offset = qpt->incr | ((offset & 1) ^ 1);
  383. }
  384. /* there can be no set bits in low-order QoS bits */
  385. WARN_ON(offset & (BIT(rdi->dparms.qos_shift) - 1));
  386. qpn = mk_qpn(qpt, map, offset);
  387. }
  388. ret = -ENOMEM;
  389. bail:
  390. return ret;
  391. }
  392. static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
  393. {
  394. struct rvt_qpn_map *map;
  395. map = qpt->map + qpn / RVT_BITS_PER_PAGE;
  396. if (map->page)
  397. clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
  398. }
  399. /**
  400. * rvt_clear_mr_refs - Drop help mr refs
  401. * @qp: rvt qp data structure
  402. * @clr_sends: If shoudl clear send side or not
  403. */
  404. static void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
  405. {
  406. unsigned n;
  407. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  408. if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
  409. rvt_put_ss(&qp->s_rdma_read_sge);
  410. rvt_put_ss(&qp->r_sge);
  411. if (clr_sends) {
  412. while (qp->s_last != qp->s_head) {
  413. struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  414. unsigned i;
  415. for (i = 0; i < wqe->wr.num_sge; i++) {
  416. struct rvt_sge *sge = &wqe->sg_list[i];
  417. rvt_put_mr(sge->mr);
  418. }
  419. if (qp->ibqp.qp_type == IB_QPT_UD ||
  420. qp->ibqp.qp_type == IB_QPT_SMI ||
  421. qp->ibqp.qp_type == IB_QPT_GSI)
  422. atomic_dec(&ibah_to_rvtah(
  423. wqe->ud_wr.ah)->refcount);
  424. if (++qp->s_last >= qp->s_size)
  425. qp->s_last = 0;
  426. smp_wmb(); /* see qp_set_savail */
  427. }
  428. if (qp->s_rdma_mr) {
  429. rvt_put_mr(qp->s_rdma_mr);
  430. qp->s_rdma_mr = NULL;
  431. }
  432. }
  433. if (qp->ibqp.qp_type != IB_QPT_RC)
  434. return;
  435. for (n = 0; n < rvt_max_atomic(rdi); n++) {
  436. struct rvt_ack_entry *e = &qp->s_ack_queue[n];
  437. if (e->rdma_sge.mr) {
  438. rvt_put_mr(e->rdma_sge.mr);
  439. e->rdma_sge.mr = NULL;
  440. }
  441. }
  442. }
  443. /**
  444. * rvt_remove_qp - remove qp form table
  445. * @rdi: rvt dev struct
  446. * @qp: qp to remove
  447. *
  448. * Remove the QP from the table so it can't be found asynchronously by
  449. * the receive routine.
  450. */
  451. static void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  452. {
  453. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  454. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  455. unsigned long flags;
  456. int removed = 1;
  457. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  458. if (rcu_dereference_protected(rvp->qp[0],
  459. lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
  460. RCU_INIT_POINTER(rvp->qp[0], NULL);
  461. } else if (rcu_dereference_protected(rvp->qp[1],
  462. lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
  463. RCU_INIT_POINTER(rvp->qp[1], NULL);
  464. } else {
  465. struct rvt_qp *q;
  466. struct rvt_qp __rcu **qpp;
  467. removed = 0;
  468. qpp = &rdi->qp_dev->qp_table[n];
  469. for (; (q = rcu_dereference_protected(*qpp,
  470. lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
  471. qpp = &q->next) {
  472. if (q == qp) {
  473. RCU_INIT_POINTER(*qpp,
  474. rcu_dereference_protected(qp->next,
  475. lockdep_is_held(&rdi->qp_dev->qpt_lock)));
  476. removed = 1;
  477. trace_rvt_qpremove(qp, n);
  478. break;
  479. }
  480. }
  481. }
  482. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  483. if (removed) {
  484. synchronize_rcu();
  485. rvt_put_qp(qp);
  486. }
  487. }
  488. /**
  489. * rvt_init_qp - initialize the QP state to the reset state
  490. * @qp: the QP to init or reinit
  491. * @type: the QP type
  492. *
  493. * This function is called from both rvt_create_qp() and
  494. * rvt_reset_qp(). The difference is that the reset
  495. * patch the necessary locks to protect against concurent
  496. * access.
  497. */
  498. static void rvt_init_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  499. enum ib_qp_type type)
  500. {
  501. qp->remote_qpn = 0;
  502. qp->qkey = 0;
  503. qp->qp_access_flags = 0;
  504. qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
  505. qp->s_hdrwords = 0;
  506. qp->s_wqe = NULL;
  507. qp->s_draining = 0;
  508. qp->s_next_psn = 0;
  509. qp->s_last_psn = 0;
  510. qp->s_sending_psn = 0;
  511. qp->s_sending_hpsn = 0;
  512. qp->s_psn = 0;
  513. qp->r_psn = 0;
  514. qp->r_msn = 0;
  515. if (type == IB_QPT_RC) {
  516. qp->s_state = IB_OPCODE_RC_SEND_LAST;
  517. qp->r_state = IB_OPCODE_RC_SEND_LAST;
  518. } else {
  519. qp->s_state = IB_OPCODE_UC_SEND_LAST;
  520. qp->r_state = IB_OPCODE_UC_SEND_LAST;
  521. }
  522. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  523. qp->r_nak_state = 0;
  524. qp->r_aflags = 0;
  525. qp->r_flags = 0;
  526. qp->s_head = 0;
  527. qp->s_tail = 0;
  528. qp->s_cur = 0;
  529. qp->s_acked = 0;
  530. qp->s_last = 0;
  531. qp->s_ssn = 1;
  532. qp->s_lsn = 0;
  533. qp->s_mig_state = IB_MIG_MIGRATED;
  534. qp->r_head_ack_queue = 0;
  535. qp->s_tail_ack_queue = 0;
  536. qp->s_num_rd_atomic = 0;
  537. if (qp->r_rq.wq) {
  538. qp->r_rq.wq->head = 0;
  539. qp->r_rq.wq->tail = 0;
  540. }
  541. qp->r_sge.num_sge = 0;
  542. atomic_set(&qp->s_reserved_used, 0);
  543. }
  544. /**
  545. * rvt_reset_qp - initialize the QP state to the reset state
  546. * @qp: the QP to reset
  547. * @type: the QP type
  548. *
  549. * r_lock, s_hlock, and s_lock are required to be held by the caller
  550. */
  551. static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  552. enum ib_qp_type type)
  553. __must_hold(&qp->s_lock)
  554. __must_hold(&qp->s_hlock)
  555. __must_hold(&qp->r_lock)
  556. {
  557. lockdep_assert_held(&qp->r_lock);
  558. lockdep_assert_held(&qp->s_hlock);
  559. lockdep_assert_held(&qp->s_lock);
  560. if (qp->state != IB_QPS_RESET) {
  561. qp->state = IB_QPS_RESET;
  562. /* Let drivers flush their waitlist */
  563. rdi->driver_f.flush_qp_waiters(qp);
  564. rvt_stop_rc_timers(qp);
  565. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
  566. spin_unlock(&qp->s_lock);
  567. spin_unlock(&qp->s_hlock);
  568. spin_unlock_irq(&qp->r_lock);
  569. /* Stop the send queue and the retry timer */
  570. rdi->driver_f.stop_send_queue(qp);
  571. rvt_del_timers_sync(qp);
  572. /* Wait for things to stop */
  573. rdi->driver_f.quiesce_qp(qp);
  574. /* take qp out the hash and wait for it to be unused */
  575. rvt_remove_qp(rdi, qp);
  576. wait_event(qp->wait, !atomic_read(&qp->refcount));
  577. /* grab the lock b/c it was locked at call time */
  578. spin_lock_irq(&qp->r_lock);
  579. spin_lock(&qp->s_hlock);
  580. spin_lock(&qp->s_lock);
  581. rvt_clear_mr_refs(qp, 1);
  582. /*
  583. * Let the driver do any tear down or re-init it needs to for
  584. * a qp that has been reset
  585. */
  586. rdi->driver_f.notify_qp_reset(qp);
  587. }
  588. rvt_init_qp(rdi, qp, type);
  589. lockdep_assert_held(&qp->r_lock);
  590. lockdep_assert_held(&qp->s_hlock);
  591. lockdep_assert_held(&qp->s_lock);
  592. }
  593. /**
  594. * rvt_create_qp - create a queue pair for a device
  595. * @ibpd: the protection domain who's device we create the queue pair for
  596. * @init_attr: the attributes of the queue pair
  597. * @udata: user data for libibverbs.so
  598. *
  599. * Queue pair creation is mostly an rvt issue. However, drivers have their own
  600. * unique idea of what queue pair numbers mean. For instance there is a reserved
  601. * range for PSM.
  602. *
  603. * Return: the queue pair on success, otherwise returns an errno.
  604. *
  605. * Called by the ib_create_qp() core verbs function.
  606. */
  607. struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
  608. struct ib_qp_init_attr *init_attr,
  609. struct ib_udata *udata)
  610. {
  611. struct rvt_qp *qp;
  612. int err;
  613. struct rvt_swqe *swq = NULL;
  614. size_t sz;
  615. size_t sg_list_sz;
  616. struct ib_qp *ret = ERR_PTR(-ENOMEM);
  617. struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
  618. void *priv = NULL;
  619. gfp_t gfp;
  620. size_t sqsize;
  621. if (!rdi)
  622. return ERR_PTR(-EINVAL);
  623. if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
  624. init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
  625. init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
  626. return ERR_PTR(-EINVAL);
  627. /* GFP_NOIO is applicable to RC QP's only */
  628. if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
  629. init_attr->qp_type != IB_QPT_RC)
  630. return ERR_PTR(-EINVAL);
  631. gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
  632. GFP_NOIO : GFP_KERNEL;
  633. /* Check receive queue parameters if no SRQ is specified. */
  634. if (!init_attr->srq) {
  635. if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
  636. init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
  637. return ERR_PTR(-EINVAL);
  638. if (init_attr->cap.max_send_sge +
  639. init_attr->cap.max_send_wr +
  640. init_attr->cap.max_recv_sge +
  641. init_attr->cap.max_recv_wr == 0)
  642. return ERR_PTR(-EINVAL);
  643. }
  644. sqsize =
  645. init_attr->cap.max_send_wr + 1 +
  646. rdi->dparms.reserved_operations;
  647. switch (init_attr->qp_type) {
  648. case IB_QPT_SMI:
  649. case IB_QPT_GSI:
  650. if (init_attr->port_num == 0 ||
  651. init_attr->port_num > ibpd->device->phys_port_cnt)
  652. return ERR_PTR(-EINVAL);
  653. case IB_QPT_UC:
  654. case IB_QPT_RC:
  655. case IB_QPT_UD:
  656. sz = sizeof(struct rvt_sge) *
  657. init_attr->cap.max_send_sge +
  658. sizeof(struct rvt_swqe);
  659. if (gfp == GFP_NOIO)
  660. swq = __vmalloc(
  661. sqsize * sz,
  662. gfp | __GFP_ZERO, PAGE_KERNEL);
  663. else
  664. swq = vzalloc_node(
  665. sqsize * sz,
  666. rdi->dparms.node);
  667. if (!swq)
  668. return ERR_PTR(-ENOMEM);
  669. sz = sizeof(*qp);
  670. sg_list_sz = 0;
  671. if (init_attr->srq) {
  672. struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
  673. if (srq->rq.max_sge > 1)
  674. sg_list_sz = sizeof(*qp->r_sg_list) *
  675. (srq->rq.max_sge - 1);
  676. } else if (init_attr->cap.max_recv_sge > 1)
  677. sg_list_sz = sizeof(*qp->r_sg_list) *
  678. (init_attr->cap.max_recv_sge - 1);
  679. qp = kzalloc_node(sz + sg_list_sz, gfp, rdi->dparms.node);
  680. if (!qp)
  681. goto bail_swq;
  682. RCU_INIT_POINTER(qp->next, NULL);
  683. if (init_attr->qp_type == IB_QPT_RC) {
  684. qp->s_ack_queue =
  685. kzalloc_node(
  686. sizeof(*qp->s_ack_queue) *
  687. rvt_max_atomic(rdi),
  688. gfp,
  689. rdi->dparms.node);
  690. if (!qp->s_ack_queue)
  691. goto bail_qp;
  692. }
  693. /* initialize timers needed for rc qp */
  694. setup_timer(&qp->s_timer, rvt_rc_timeout, (unsigned long)qp);
  695. hrtimer_init(&qp->s_rnr_timer, CLOCK_MONOTONIC,
  696. HRTIMER_MODE_REL);
  697. qp->s_rnr_timer.function = rvt_rc_rnr_retry;
  698. /*
  699. * Driver needs to set up it's private QP structure and do any
  700. * initialization that is needed.
  701. */
  702. priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
  703. if (IS_ERR(priv)) {
  704. ret = priv;
  705. goto bail_qp;
  706. }
  707. qp->priv = priv;
  708. qp->timeout_jiffies =
  709. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  710. 1000UL);
  711. if (init_attr->srq) {
  712. sz = 0;
  713. } else {
  714. qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
  715. qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
  716. sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
  717. sizeof(struct rvt_rwqe);
  718. if (udata)
  719. qp->r_rq.wq = vmalloc_user(
  720. sizeof(struct rvt_rwq) +
  721. qp->r_rq.size * sz);
  722. else if (gfp == GFP_NOIO)
  723. qp->r_rq.wq = __vmalloc(
  724. sizeof(struct rvt_rwq) +
  725. qp->r_rq.size * sz,
  726. gfp | __GFP_ZERO, PAGE_KERNEL);
  727. else
  728. qp->r_rq.wq = vzalloc_node(
  729. sizeof(struct rvt_rwq) +
  730. qp->r_rq.size * sz,
  731. rdi->dparms.node);
  732. if (!qp->r_rq.wq)
  733. goto bail_driver_priv;
  734. }
  735. /*
  736. * ib_create_qp() will initialize qp->ibqp
  737. * except for qp->ibqp.qp_num.
  738. */
  739. spin_lock_init(&qp->r_lock);
  740. spin_lock_init(&qp->s_hlock);
  741. spin_lock_init(&qp->s_lock);
  742. spin_lock_init(&qp->r_rq.lock);
  743. atomic_set(&qp->refcount, 0);
  744. atomic_set(&qp->local_ops_pending, 0);
  745. init_waitqueue_head(&qp->wait);
  746. init_timer(&qp->s_timer);
  747. qp->s_timer.data = (unsigned long)qp;
  748. INIT_LIST_HEAD(&qp->rspwait);
  749. qp->state = IB_QPS_RESET;
  750. qp->s_wq = swq;
  751. qp->s_size = sqsize;
  752. qp->s_avail = init_attr->cap.max_send_wr;
  753. qp->s_max_sge = init_attr->cap.max_send_sge;
  754. if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
  755. qp->s_flags = RVT_S_SIGNAL_REQ_WR;
  756. err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
  757. init_attr->qp_type,
  758. init_attr->port_num, gfp);
  759. if (err < 0) {
  760. ret = ERR_PTR(err);
  761. goto bail_rq_wq;
  762. }
  763. qp->ibqp.qp_num = err;
  764. qp->port_num = init_attr->port_num;
  765. rvt_init_qp(rdi, qp, init_attr->qp_type);
  766. break;
  767. default:
  768. /* Don't support raw QPs */
  769. return ERR_PTR(-EINVAL);
  770. }
  771. init_attr->cap.max_inline_data = 0;
  772. /*
  773. * Return the address of the RWQ as the offset to mmap.
  774. * See rvt_mmap() for details.
  775. */
  776. if (udata && udata->outlen >= sizeof(__u64)) {
  777. if (!qp->r_rq.wq) {
  778. __u64 offset = 0;
  779. err = ib_copy_to_udata(udata, &offset,
  780. sizeof(offset));
  781. if (err) {
  782. ret = ERR_PTR(err);
  783. goto bail_qpn;
  784. }
  785. } else {
  786. u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
  787. qp->ip = rvt_create_mmap_info(rdi, s,
  788. ibpd->uobject->context,
  789. qp->r_rq.wq);
  790. if (!qp->ip) {
  791. ret = ERR_PTR(-ENOMEM);
  792. goto bail_qpn;
  793. }
  794. err = ib_copy_to_udata(udata, &qp->ip->offset,
  795. sizeof(qp->ip->offset));
  796. if (err) {
  797. ret = ERR_PTR(err);
  798. goto bail_ip;
  799. }
  800. }
  801. qp->pid = current->pid;
  802. }
  803. spin_lock(&rdi->n_qps_lock);
  804. if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
  805. spin_unlock(&rdi->n_qps_lock);
  806. ret = ERR_PTR(-ENOMEM);
  807. goto bail_ip;
  808. }
  809. rdi->n_qps_allocated++;
  810. /*
  811. * Maintain a busy_jiffies variable that will be added to the timeout
  812. * period in mod_retry_timer and add_retry_timer. This busy jiffies
  813. * is scaled by the number of rc qps created for the device to reduce
  814. * the number of timeouts occurring when there is a large number of
  815. * qps. busy_jiffies is incremented every rc qp scaling interval.
  816. * The scaling interval is selected based on extensive performance
  817. * evaluation of targeted workloads.
  818. */
  819. if (init_attr->qp_type == IB_QPT_RC) {
  820. rdi->n_rc_qps++;
  821. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  822. }
  823. spin_unlock(&rdi->n_qps_lock);
  824. if (qp->ip) {
  825. spin_lock_irq(&rdi->pending_lock);
  826. list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
  827. spin_unlock_irq(&rdi->pending_lock);
  828. }
  829. ret = &qp->ibqp;
  830. /*
  831. * We have our QP and its good, now keep track of what types of opcodes
  832. * can be processed on this QP. We do this by keeping track of what the
  833. * 3 high order bits of the opcode are.
  834. */
  835. switch (init_attr->qp_type) {
  836. case IB_QPT_SMI:
  837. case IB_QPT_GSI:
  838. case IB_QPT_UD:
  839. qp->allowed_ops = IB_OPCODE_UD;
  840. break;
  841. case IB_QPT_RC:
  842. qp->allowed_ops = IB_OPCODE_RC;
  843. break;
  844. case IB_QPT_UC:
  845. qp->allowed_ops = IB_OPCODE_UC;
  846. break;
  847. default:
  848. ret = ERR_PTR(-EINVAL);
  849. goto bail_ip;
  850. }
  851. return ret;
  852. bail_ip:
  853. if (qp->ip)
  854. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  855. bail_qpn:
  856. free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  857. bail_rq_wq:
  858. if (!qp->ip)
  859. vfree(qp->r_rq.wq);
  860. bail_driver_priv:
  861. rdi->driver_f.qp_priv_free(rdi, qp);
  862. bail_qp:
  863. kfree(qp->s_ack_queue);
  864. kfree(qp);
  865. bail_swq:
  866. vfree(swq);
  867. return ret;
  868. }
  869. /**
  870. * rvt_error_qp - put a QP into the error state
  871. * @qp: the QP to put into the error state
  872. * @err: the receive completion error to signal if a RWQE is active
  873. *
  874. * Flushes both send and receive work queues.
  875. *
  876. * Return: true if last WQE event should be generated.
  877. * The QP r_lock and s_lock should be held and interrupts disabled.
  878. * If we are already in error state, just return.
  879. */
  880. int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
  881. {
  882. struct ib_wc wc;
  883. int ret = 0;
  884. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  885. lockdep_assert_held(&qp->r_lock);
  886. lockdep_assert_held(&qp->s_lock);
  887. if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
  888. goto bail;
  889. qp->state = IB_QPS_ERR;
  890. if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
  891. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
  892. del_timer(&qp->s_timer);
  893. }
  894. if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
  895. qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
  896. rdi->driver_f.notify_error_qp(qp);
  897. /* Schedule the sending tasklet to drain the send work queue. */
  898. if (ACCESS_ONCE(qp->s_last) != qp->s_head)
  899. rdi->driver_f.schedule_send(qp);
  900. rvt_clear_mr_refs(qp, 0);
  901. memset(&wc, 0, sizeof(wc));
  902. wc.qp = &qp->ibqp;
  903. wc.opcode = IB_WC_RECV;
  904. if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
  905. wc.wr_id = qp->r_wr_id;
  906. wc.status = err;
  907. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  908. }
  909. wc.status = IB_WC_WR_FLUSH_ERR;
  910. if (qp->r_rq.wq) {
  911. struct rvt_rwq *wq;
  912. u32 head;
  913. u32 tail;
  914. spin_lock(&qp->r_rq.lock);
  915. /* sanity check pointers before trusting them */
  916. wq = qp->r_rq.wq;
  917. head = wq->head;
  918. if (head >= qp->r_rq.size)
  919. head = 0;
  920. tail = wq->tail;
  921. if (tail >= qp->r_rq.size)
  922. tail = 0;
  923. while (tail != head) {
  924. wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
  925. if (++tail >= qp->r_rq.size)
  926. tail = 0;
  927. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  928. }
  929. wq->tail = tail;
  930. spin_unlock(&qp->r_rq.lock);
  931. } else if (qp->ibqp.event_handler) {
  932. ret = 1;
  933. }
  934. bail:
  935. return ret;
  936. }
  937. EXPORT_SYMBOL(rvt_error_qp);
  938. /*
  939. * Put the QP into the hash table.
  940. * The hash table holds a reference to the QP.
  941. */
  942. static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  943. {
  944. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  945. unsigned long flags;
  946. rvt_get_qp(qp);
  947. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  948. if (qp->ibqp.qp_num <= 1) {
  949. rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
  950. } else {
  951. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  952. qp->next = rdi->qp_dev->qp_table[n];
  953. rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
  954. trace_rvt_qpinsert(qp, n);
  955. }
  956. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  957. }
  958. /**
  959. * rvt_modify_qp - modify the attributes of a queue pair
  960. * @ibqp: the queue pair who's attributes we're modifying
  961. * @attr: the new attributes
  962. * @attr_mask: the mask of attributes to modify
  963. * @udata: user data for libibverbs.so
  964. *
  965. * Return: 0 on success, otherwise returns an errno.
  966. */
  967. int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  968. int attr_mask, struct ib_udata *udata)
  969. {
  970. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  971. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  972. enum ib_qp_state cur_state, new_state;
  973. struct ib_event ev;
  974. int lastwqe = 0;
  975. int mig = 0;
  976. int pmtu = 0; /* for gcc warning only */
  977. enum rdma_link_layer link;
  978. link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
  979. spin_lock_irq(&qp->r_lock);
  980. spin_lock(&qp->s_hlock);
  981. spin_lock(&qp->s_lock);
  982. cur_state = attr_mask & IB_QP_CUR_STATE ?
  983. attr->cur_qp_state : qp->state;
  984. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  985. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  986. attr_mask, link))
  987. goto inval;
  988. if (rdi->driver_f.check_modify_qp &&
  989. rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
  990. goto inval;
  991. if (attr_mask & IB_QP_AV) {
  992. if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
  993. goto inval;
  994. if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
  995. goto inval;
  996. }
  997. if (attr_mask & IB_QP_ALT_PATH) {
  998. if (attr->alt_ah_attr.dlid >=
  999. be16_to_cpu(IB_MULTICAST_LID_BASE))
  1000. goto inval;
  1001. if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
  1002. goto inval;
  1003. if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
  1004. goto inval;
  1005. }
  1006. if (attr_mask & IB_QP_PKEY_INDEX)
  1007. if (attr->pkey_index >= rvt_get_npkeys(rdi))
  1008. goto inval;
  1009. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  1010. if (attr->min_rnr_timer > 31)
  1011. goto inval;
  1012. if (attr_mask & IB_QP_PORT)
  1013. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  1014. qp->ibqp.qp_type == IB_QPT_GSI ||
  1015. attr->port_num == 0 ||
  1016. attr->port_num > ibqp->device->phys_port_cnt)
  1017. goto inval;
  1018. if (attr_mask & IB_QP_DEST_QPN)
  1019. if (attr->dest_qp_num > RVT_QPN_MASK)
  1020. goto inval;
  1021. if (attr_mask & IB_QP_RETRY_CNT)
  1022. if (attr->retry_cnt > 7)
  1023. goto inval;
  1024. if (attr_mask & IB_QP_RNR_RETRY)
  1025. if (attr->rnr_retry > 7)
  1026. goto inval;
  1027. /*
  1028. * Don't allow invalid path_mtu values. OK to set greater
  1029. * than the active mtu (or even the max_cap, if we have tuned
  1030. * that to a small mtu. We'll set qp->path_mtu
  1031. * to the lesser of requested attribute mtu and active,
  1032. * for packetizing messages.
  1033. * Note that the QP port has to be set in INIT and MTU in RTR.
  1034. */
  1035. if (attr_mask & IB_QP_PATH_MTU) {
  1036. pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
  1037. if (pmtu < 0)
  1038. goto inval;
  1039. }
  1040. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1041. if (attr->path_mig_state == IB_MIG_REARM) {
  1042. if (qp->s_mig_state == IB_MIG_ARMED)
  1043. goto inval;
  1044. if (new_state != IB_QPS_RTS)
  1045. goto inval;
  1046. } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
  1047. if (qp->s_mig_state == IB_MIG_REARM)
  1048. goto inval;
  1049. if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
  1050. goto inval;
  1051. if (qp->s_mig_state == IB_MIG_ARMED)
  1052. mig = 1;
  1053. } else {
  1054. goto inval;
  1055. }
  1056. }
  1057. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1058. if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
  1059. goto inval;
  1060. switch (new_state) {
  1061. case IB_QPS_RESET:
  1062. if (qp->state != IB_QPS_RESET)
  1063. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  1064. break;
  1065. case IB_QPS_RTR:
  1066. /* Allow event to re-trigger if QP set to RTR more than once */
  1067. qp->r_flags &= ~RVT_R_COMM_EST;
  1068. qp->state = new_state;
  1069. break;
  1070. case IB_QPS_SQD:
  1071. qp->s_draining = qp->s_last != qp->s_cur;
  1072. qp->state = new_state;
  1073. break;
  1074. case IB_QPS_SQE:
  1075. if (qp->ibqp.qp_type == IB_QPT_RC)
  1076. goto inval;
  1077. qp->state = new_state;
  1078. break;
  1079. case IB_QPS_ERR:
  1080. lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  1081. break;
  1082. default:
  1083. qp->state = new_state;
  1084. break;
  1085. }
  1086. if (attr_mask & IB_QP_PKEY_INDEX)
  1087. qp->s_pkey_index = attr->pkey_index;
  1088. if (attr_mask & IB_QP_PORT)
  1089. qp->port_num = attr->port_num;
  1090. if (attr_mask & IB_QP_DEST_QPN)
  1091. qp->remote_qpn = attr->dest_qp_num;
  1092. if (attr_mask & IB_QP_SQ_PSN) {
  1093. qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
  1094. qp->s_psn = qp->s_next_psn;
  1095. qp->s_sending_psn = qp->s_next_psn;
  1096. qp->s_last_psn = qp->s_next_psn - 1;
  1097. qp->s_sending_hpsn = qp->s_last_psn;
  1098. }
  1099. if (attr_mask & IB_QP_RQ_PSN)
  1100. qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
  1101. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1102. qp->qp_access_flags = attr->qp_access_flags;
  1103. if (attr_mask & IB_QP_AV) {
  1104. qp->remote_ah_attr = attr->ah_attr;
  1105. qp->s_srate = attr->ah_attr.static_rate;
  1106. qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
  1107. }
  1108. if (attr_mask & IB_QP_ALT_PATH) {
  1109. qp->alt_ah_attr = attr->alt_ah_attr;
  1110. qp->s_alt_pkey_index = attr->alt_pkey_index;
  1111. }
  1112. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1113. qp->s_mig_state = attr->path_mig_state;
  1114. if (mig) {
  1115. qp->remote_ah_attr = qp->alt_ah_attr;
  1116. qp->port_num = qp->alt_ah_attr.port_num;
  1117. qp->s_pkey_index = qp->s_alt_pkey_index;
  1118. }
  1119. }
  1120. if (attr_mask & IB_QP_PATH_MTU) {
  1121. qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
  1122. qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
  1123. qp->log_pmtu = ilog2(qp->pmtu);
  1124. }
  1125. if (attr_mask & IB_QP_RETRY_CNT) {
  1126. qp->s_retry_cnt = attr->retry_cnt;
  1127. qp->s_retry = attr->retry_cnt;
  1128. }
  1129. if (attr_mask & IB_QP_RNR_RETRY) {
  1130. qp->s_rnr_retry_cnt = attr->rnr_retry;
  1131. qp->s_rnr_retry = attr->rnr_retry;
  1132. }
  1133. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  1134. qp->r_min_rnr_timer = attr->min_rnr_timer;
  1135. if (attr_mask & IB_QP_TIMEOUT) {
  1136. qp->timeout = attr->timeout;
  1137. qp->timeout_jiffies =
  1138. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  1139. 1000UL);
  1140. }
  1141. if (attr_mask & IB_QP_QKEY)
  1142. qp->qkey = attr->qkey;
  1143. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1144. qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
  1145. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  1146. qp->s_max_rd_atomic = attr->max_rd_atomic;
  1147. if (rdi->driver_f.modify_qp)
  1148. rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
  1149. spin_unlock(&qp->s_lock);
  1150. spin_unlock(&qp->s_hlock);
  1151. spin_unlock_irq(&qp->r_lock);
  1152. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1153. rvt_insert_qp(rdi, qp);
  1154. if (lastwqe) {
  1155. ev.device = qp->ibqp.device;
  1156. ev.element.qp = &qp->ibqp;
  1157. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1158. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1159. }
  1160. if (mig) {
  1161. ev.device = qp->ibqp.device;
  1162. ev.element.qp = &qp->ibqp;
  1163. ev.event = IB_EVENT_PATH_MIG;
  1164. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1165. }
  1166. return 0;
  1167. inval:
  1168. spin_unlock(&qp->s_lock);
  1169. spin_unlock(&qp->s_hlock);
  1170. spin_unlock_irq(&qp->r_lock);
  1171. return -EINVAL;
  1172. }
  1173. /** rvt_free_qpn - Free a qpn from the bit map
  1174. * @qpt: QP table
  1175. * @qpn: queue pair number to free
  1176. */
  1177. static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
  1178. {
  1179. struct rvt_qpn_map *map;
  1180. map = qpt->map + qpn / RVT_BITS_PER_PAGE;
  1181. if (map->page)
  1182. clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
  1183. }
  1184. /**
  1185. * rvt_destroy_qp - destroy a queue pair
  1186. * @ibqp: the queue pair to destroy
  1187. *
  1188. * Note that this can be called while the QP is actively sending or
  1189. * receiving!
  1190. *
  1191. * Return: 0 on success.
  1192. */
  1193. int rvt_destroy_qp(struct ib_qp *ibqp)
  1194. {
  1195. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1196. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1197. spin_lock_irq(&qp->r_lock);
  1198. spin_lock(&qp->s_hlock);
  1199. spin_lock(&qp->s_lock);
  1200. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  1201. spin_unlock(&qp->s_lock);
  1202. spin_unlock(&qp->s_hlock);
  1203. spin_unlock_irq(&qp->r_lock);
  1204. /* qpn is now available for use again */
  1205. rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  1206. spin_lock(&rdi->n_qps_lock);
  1207. rdi->n_qps_allocated--;
  1208. if (qp->ibqp.qp_type == IB_QPT_RC) {
  1209. rdi->n_rc_qps--;
  1210. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  1211. }
  1212. spin_unlock(&rdi->n_qps_lock);
  1213. if (qp->ip)
  1214. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  1215. else
  1216. vfree(qp->r_rq.wq);
  1217. vfree(qp->s_wq);
  1218. rdi->driver_f.qp_priv_free(rdi, qp);
  1219. kfree(qp->s_ack_queue);
  1220. kfree(qp);
  1221. return 0;
  1222. }
  1223. /**
  1224. * rvt_query_qp - query an ipbq
  1225. * @ibqp: IB qp to query
  1226. * @attr: attr struct to fill in
  1227. * @attr_mask: attr mask ignored
  1228. * @init_attr: struct to fill in
  1229. *
  1230. * Return: always 0
  1231. */
  1232. int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1233. int attr_mask, struct ib_qp_init_attr *init_attr)
  1234. {
  1235. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1236. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1237. attr->qp_state = qp->state;
  1238. attr->cur_qp_state = attr->qp_state;
  1239. attr->path_mtu = qp->path_mtu;
  1240. attr->path_mig_state = qp->s_mig_state;
  1241. attr->qkey = qp->qkey;
  1242. attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
  1243. attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
  1244. attr->dest_qp_num = qp->remote_qpn;
  1245. attr->qp_access_flags = qp->qp_access_flags;
  1246. attr->cap.max_send_wr = qp->s_size - 1 -
  1247. rdi->dparms.reserved_operations;
  1248. attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
  1249. attr->cap.max_send_sge = qp->s_max_sge;
  1250. attr->cap.max_recv_sge = qp->r_rq.max_sge;
  1251. attr->cap.max_inline_data = 0;
  1252. attr->ah_attr = qp->remote_ah_attr;
  1253. attr->alt_ah_attr = qp->alt_ah_attr;
  1254. attr->pkey_index = qp->s_pkey_index;
  1255. attr->alt_pkey_index = qp->s_alt_pkey_index;
  1256. attr->en_sqd_async_notify = 0;
  1257. attr->sq_draining = qp->s_draining;
  1258. attr->max_rd_atomic = qp->s_max_rd_atomic;
  1259. attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
  1260. attr->min_rnr_timer = qp->r_min_rnr_timer;
  1261. attr->port_num = qp->port_num;
  1262. attr->timeout = qp->timeout;
  1263. attr->retry_cnt = qp->s_retry_cnt;
  1264. attr->rnr_retry = qp->s_rnr_retry_cnt;
  1265. attr->alt_port_num = qp->alt_ah_attr.port_num;
  1266. attr->alt_timeout = qp->alt_timeout;
  1267. init_attr->event_handler = qp->ibqp.event_handler;
  1268. init_attr->qp_context = qp->ibqp.qp_context;
  1269. init_attr->send_cq = qp->ibqp.send_cq;
  1270. init_attr->recv_cq = qp->ibqp.recv_cq;
  1271. init_attr->srq = qp->ibqp.srq;
  1272. init_attr->cap = attr->cap;
  1273. if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
  1274. init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  1275. else
  1276. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  1277. init_attr->qp_type = qp->ibqp.qp_type;
  1278. init_attr->port_num = qp->port_num;
  1279. return 0;
  1280. }
  1281. /**
  1282. * rvt_post_receive - post a receive on a QP
  1283. * @ibqp: the QP to post the receive on
  1284. * @wr: the WR to post
  1285. * @bad_wr: the first bad WR is put here
  1286. *
  1287. * This may be called from interrupt context.
  1288. *
  1289. * Return: 0 on success otherwise errno
  1290. */
  1291. int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1292. struct ib_recv_wr **bad_wr)
  1293. {
  1294. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1295. struct rvt_rwq *wq = qp->r_rq.wq;
  1296. unsigned long flags;
  1297. int qp_err_flush = (ib_rvt_state_ops[qp->state] & RVT_FLUSH_RECV) &&
  1298. !qp->ibqp.srq;
  1299. /* Check that state is OK to post receive. */
  1300. if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
  1301. *bad_wr = wr;
  1302. return -EINVAL;
  1303. }
  1304. for (; wr; wr = wr->next) {
  1305. struct rvt_rwqe *wqe;
  1306. u32 next;
  1307. int i;
  1308. if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
  1309. *bad_wr = wr;
  1310. return -EINVAL;
  1311. }
  1312. spin_lock_irqsave(&qp->r_rq.lock, flags);
  1313. next = wq->head + 1;
  1314. if (next >= qp->r_rq.size)
  1315. next = 0;
  1316. if (next == wq->tail) {
  1317. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1318. *bad_wr = wr;
  1319. return -ENOMEM;
  1320. }
  1321. if (unlikely(qp_err_flush)) {
  1322. struct ib_wc wc;
  1323. memset(&wc, 0, sizeof(wc));
  1324. wc.qp = &qp->ibqp;
  1325. wc.opcode = IB_WC_RECV;
  1326. wc.wr_id = wr->wr_id;
  1327. wc.status = IB_WC_WR_FLUSH_ERR;
  1328. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  1329. } else {
  1330. wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
  1331. wqe->wr_id = wr->wr_id;
  1332. wqe->num_sge = wr->num_sge;
  1333. for (i = 0; i < wr->num_sge; i++)
  1334. wqe->sg_list[i] = wr->sg_list[i];
  1335. /*
  1336. * Make sure queue entry is written
  1337. * before the head index.
  1338. */
  1339. smp_wmb();
  1340. wq->head = next;
  1341. }
  1342. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1343. }
  1344. return 0;
  1345. }
  1346. /**
  1347. * rvt_qp_valid_operation - validate post send wr request
  1348. * @qp - the qp
  1349. * @post-parms - the post send table for the driver
  1350. * @wr - the work request
  1351. *
  1352. * The routine validates the operation based on the
  1353. * validation table an returns the length of the operation
  1354. * which can extend beyond the ib_send_bw. Operation
  1355. * dependent flags key atomic operation validation.
  1356. *
  1357. * There is an exception for UD qps that validates the pd and
  1358. * overrides the length to include the additional UD specific
  1359. * length.
  1360. *
  1361. * Returns a negative error or the length of the work request
  1362. * for building the swqe.
  1363. */
  1364. static inline int rvt_qp_valid_operation(
  1365. struct rvt_qp *qp,
  1366. const struct rvt_operation_params *post_parms,
  1367. struct ib_send_wr *wr)
  1368. {
  1369. int len;
  1370. if (wr->opcode >= RVT_OPERATION_MAX || !post_parms[wr->opcode].length)
  1371. return -EINVAL;
  1372. if (!(post_parms[wr->opcode].qpt_support & BIT(qp->ibqp.qp_type)))
  1373. return -EINVAL;
  1374. if ((post_parms[wr->opcode].flags & RVT_OPERATION_PRIV) &&
  1375. ibpd_to_rvtpd(qp->ibqp.pd)->user)
  1376. return -EINVAL;
  1377. if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC_SGE &&
  1378. (wr->num_sge == 0 ||
  1379. wr->sg_list[0].length < sizeof(u64) ||
  1380. wr->sg_list[0].addr & (sizeof(u64) - 1)))
  1381. return -EINVAL;
  1382. if (post_parms[wr->opcode].flags & RVT_OPERATION_ATOMIC &&
  1383. !qp->s_max_rd_atomic)
  1384. return -EINVAL;
  1385. len = post_parms[wr->opcode].length;
  1386. /* UD specific */
  1387. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1388. qp->ibqp.qp_type != IB_QPT_RC) {
  1389. if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
  1390. return -EINVAL;
  1391. len = sizeof(struct ib_ud_wr);
  1392. }
  1393. return len;
  1394. }
  1395. /**
  1396. * rvt_qp_is_avail - determine queue capacity
  1397. * @qp - the qp
  1398. * @rdi - the rdmavt device
  1399. * @reserved_op - is reserved operation
  1400. *
  1401. * This assumes the s_hlock is held but the s_last
  1402. * qp variable is uncontrolled.
  1403. *
  1404. * For non reserved operations, the qp->s_avail
  1405. * may be changed.
  1406. *
  1407. * The return value is zero or a -ENOMEM.
  1408. */
  1409. static inline int rvt_qp_is_avail(
  1410. struct rvt_qp *qp,
  1411. struct rvt_dev_info *rdi,
  1412. bool reserved_op)
  1413. {
  1414. u32 slast;
  1415. u32 avail;
  1416. u32 reserved_used;
  1417. /* see rvt_qp_wqe_unreserve() */
  1418. smp_mb__before_atomic();
  1419. reserved_used = atomic_read(&qp->s_reserved_used);
  1420. if (unlikely(reserved_op)) {
  1421. /* see rvt_qp_wqe_unreserve() */
  1422. smp_mb__before_atomic();
  1423. if (reserved_used >= rdi->dparms.reserved_operations)
  1424. return -ENOMEM;
  1425. return 0;
  1426. }
  1427. /* non-reserved operations */
  1428. if (likely(qp->s_avail))
  1429. return 0;
  1430. smp_read_barrier_depends(); /* see rc.c */
  1431. slast = ACCESS_ONCE(qp->s_last);
  1432. if (qp->s_head >= slast)
  1433. avail = qp->s_size - (qp->s_head - slast);
  1434. else
  1435. avail = slast - qp->s_head;
  1436. /* see rvt_qp_wqe_unreserve() */
  1437. smp_mb__before_atomic();
  1438. reserved_used = atomic_read(&qp->s_reserved_used);
  1439. avail = avail - 1 -
  1440. (rdi->dparms.reserved_operations - reserved_used);
  1441. /* insure we don't assign a negative s_avail */
  1442. if ((s32)avail <= 0)
  1443. return -ENOMEM;
  1444. qp->s_avail = avail;
  1445. if (WARN_ON(qp->s_avail >
  1446. (qp->s_size - 1 - rdi->dparms.reserved_operations)))
  1447. rvt_pr_err(rdi,
  1448. "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
  1449. qp->ibqp.qp_num, qp->s_size, qp->s_avail,
  1450. qp->s_head, qp->s_tail, qp->s_cur,
  1451. qp->s_acked, qp->s_last);
  1452. return 0;
  1453. }
  1454. /**
  1455. * rvt_post_one_wr - post one RC, UC, or UD send work request
  1456. * @qp: the QP to post on
  1457. * @wr: the work request to send
  1458. */
  1459. static int rvt_post_one_wr(struct rvt_qp *qp,
  1460. struct ib_send_wr *wr,
  1461. int *call_send)
  1462. {
  1463. struct rvt_swqe *wqe;
  1464. u32 next;
  1465. int i;
  1466. int j;
  1467. int acc;
  1468. struct rvt_lkey_table *rkt;
  1469. struct rvt_pd *pd;
  1470. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1471. u8 log_pmtu;
  1472. int ret;
  1473. size_t cplen;
  1474. bool reserved_op;
  1475. int local_ops_delayed = 0;
  1476. BUILD_BUG_ON(IB_QPT_MAX >= (sizeof(u32) * BITS_PER_BYTE));
  1477. /* IB spec says that num_sge == 0 is OK. */
  1478. if (unlikely(wr->num_sge > qp->s_max_sge))
  1479. return -EINVAL;
  1480. ret = rvt_qp_valid_operation(qp, rdi->post_parms, wr);
  1481. if (ret < 0)
  1482. return ret;
  1483. cplen = ret;
  1484. /*
  1485. * Local operations include fast register and local invalidate.
  1486. * Fast register needs to be processed immediately because the
  1487. * registered lkey may be used by following work requests and the
  1488. * lkey needs to be valid at the time those requests are posted.
  1489. * Local invalidate can be processed immediately if fencing is
  1490. * not required and no previous local invalidate ops are pending.
  1491. * Signaled local operations that have been processed immediately
  1492. * need to have requests with "completion only" flags set posted
  1493. * to the send queue in order to generate completions.
  1494. */
  1495. if ((rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL)) {
  1496. switch (wr->opcode) {
  1497. case IB_WR_REG_MR:
  1498. ret = rvt_fast_reg_mr(qp,
  1499. reg_wr(wr)->mr,
  1500. reg_wr(wr)->key,
  1501. reg_wr(wr)->access);
  1502. if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
  1503. return ret;
  1504. break;
  1505. case IB_WR_LOCAL_INV:
  1506. if ((wr->send_flags & IB_SEND_FENCE) ||
  1507. atomic_read(&qp->local_ops_pending)) {
  1508. local_ops_delayed = 1;
  1509. } else {
  1510. ret = rvt_invalidate_rkey(
  1511. qp, wr->ex.invalidate_rkey);
  1512. if (ret || !(wr->send_flags & IB_SEND_SIGNALED))
  1513. return ret;
  1514. }
  1515. break;
  1516. default:
  1517. return -EINVAL;
  1518. }
  1519. }
  1520. reserved_op = rdi->post_parms[wr->opcode].flags &
  1521. RVT_OPERATION_USE_RESERVE;
  1522. /* check for avail */
  1523. ret = rvt_qp_is_avail(qp, rdi, reserved_op);
  1524. if (ret)
  1525. return ret;
  1526. next = qp->s_head + 1;
  1527. if (next >= qp->s_size)
  1528. next = 0;
  1529. rkt = &rdi->lkey_table;
  1530. pd = ibpd_to_rvtpd(qp->ibqp.pd);
  1531. wqe = rvt_get_swqe_ptr(qp, qp->s_head);
  1532. /* cplen has length from above */
  1533. memcpy(&wqe->wr, wr, cplen);
  1534. wqe->length = 0;
  1535. j = 0;
  1536. if (wr->num_sge) {
  1537. acc = wr->opcode >= IB_WR_RDMA_READ ?
  1538. IB_ACCESS_LOCAL_WRITE : 0;
  1539. for (i = 0; i < wr->num_sge; i++) {
  1540. u32 length = wr->sg_list[i].length;
  1541. int ok;
  1542. if (length == 0)
  1543. continue;
  1544. ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
  1545. &wr->sg_list[i], acc);
  1546. if (!ok) {
  1547. ret = -EINVAL;
  1548. goto bail_inval_free;
  1549. }
  1550. wqe->length += length;
  1551. j++;
  1552. }
  1553. wqe->wr.num_sge = j;
  1554. }
  1555. /* general part of wqe valid - allow for driver checks */
  1556. if (rdi->driver_f.check_send_wqe) {
  1557. ret = rdi->driver_f.check_send_wqe(qp, wqe);
  1558. if (ret < 0)
  1559. goto bail_inval_free;
  1560. if (ret)
  1561. *call_send = ret;
  1562. }
  1563. log_pmtu = qp->log_pmtu;
  1564. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1565. qp->ibqp.qp_type != IB_QPT_RC) {
  1566. struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
  1567. log_pmtu = ah->log_pmtu;
  1568. atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
  1569. }
  1570. if (rdi->post_parms[wr->opcode].flags & RVT_OPERATION_LOCAL) {
  1571. if (local_ops_delayed)
  1572. atomic_inc(&qp->local_ops_pending);
  1573. else
  1574. wqe->wr.send_flags |= RVT_SEND_COMPLETION_ONLY;
  1575. wqe->ssn = 0;
  1576. wqe->psn = 0;
  1577. wqe->lpsn = 0;
  1578. } else {
  1579. wqe->ssn = qp->s_ssn++;
  1580. wqe->psn = qp->s_next_psn;
  1581. wqe->lpsn = wqe->psn +
  1582. (wqe->length ?
  1583. ((wqe->length - 1) >> log_pmtu) :
  1584. 0);
  1585. qp->s_next_psn = wqe->lpsn + 1;
  1586. }
  1587. trace_rvt_post_one_wr(qp, wqe);
  1588. if (unlikely(reserved_op))
  1589. rvt_qp_wqe_reserve(qp, wqe);
  1590. else
  1591. qp->s_avail--;
  1592. smp_wmb(); /* see request builders */
  1593. qp->s_head = next;
  1594. return 0;
  1595. bail_inval_free:
  1596. /* release mr holds */
  1597. while (j) {
  1598. struct rvt_sge *sge = &wqe->sg_list[--j];
  1599. rvt_put_mr(sge->mr);
  1600. }
  1601. return ret;
  1602. }
  1603. /**
  1604. * rvt_post_send - post a send on a QP
  1605. * @ibqp: the QP to post the send on
  1606. * @wr: the list of work requests to post
  1607. * @bad_wr: the first bad WR is put here
  1608. *
  1609. * This may be called from interrupt context.
  1610. *
  1611. * Return: 0 on success else errno
  1612. */
  1613. int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1614. struct ib_send_wr **bad_wr)
  1615. {
  1616. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1617. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1618. unsigned long flags = 0;
  1619. int call_send;
  1620. unsigned nreq = 0;
  1621. int err = 0;
  1622. spin_lock_irqsave(&qp->s_hlock, flags);
  1623. /*
  1624. * Ensure QP state is such that we can send. If not bail out early,
  1625. * there is no need to do this every time we post a send.
  1626. */
  1627. if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
  1628. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1629. return -EINVAL;
  1630. }
  1631. /*
  1632. * If the send queue is empty, and we only have a single WR then just go
  1633. * ahead and kick the send engine into gear. Otherwise we will always
  1634. * just schedule the send to happen later.
  1635. */
  1636. call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
  1637. for (; wr; wr = wr->next) {
  1638. err = rvt_post_one_wr(qp, wr, &call_send);
  1639. if (unlikely(err)) {
  1640. *bad_wr = wr;
  1641. goto bail;
  1642. }
  1643. nreq++;
  1644. }
  1645. bail:
  1646. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1647. if (nreq) {
  1648. if (call_send)
  1649. rdi->driver_f.do_send(qp);
  1650. else
  1651. rdi->driver_f.schedule_send_no_lock(qp);
  1652. }
  1653. return err;
  1654. }
  1655. /**
  1656. * rvt_post_srq_receive - post a receive on a shared receive queue
  1657. * @ibsrq: the SRQ to post the receive on
  1658. * @wr: the list of work requests to post
  1659. * @bad_wr: A pointer to the first WR to cause a problem is put here
  1660. *
  1661. * This may be called from interrupt context.
  1662. *
  1663. * Return: 0 on success else errno
  1664. */
  1665. int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  1666. struct ib_recv_wr **bad_wr)
  1667. {
  1668. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  1669. struct rvt_rwq *wq;
  1670. unsigned long flags;
  1671. for (; wr; wr = wr->next) {
  1672. struct rvt_rwqe *wqe;
  1673. u32 next;
  1674. int i;
  1675. if ((unsigned)wr->num_sge > srq->rq.max_sge) {
  1676. *bad_wr = wr;
  1677. return -EINVAL;
  1678. }
  1679. spin_lock_irqsave(&srq->rq.lock, flags);
  1680. wq = srq->rq.wq;
  1681. next = wq->head + 1;
  1682. if (next >= srq->rq.size)
  1683. next = 0;
  1684. if (next == wq->tail) {
  1685. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1686. *bad_wr = wr;
  1687. return -ENOMEM;
  1688. }
  1689. wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
  1690. wqe->wr_id = wr->wr_id;
  1691. wqe->num_sge = wr->num_sge;
  1692. for (i = 0; i < wr->num_sge; i++)
  1693. wqe->sg_list[i] = wr->sg_list[i];
  1694. /* Make sure queue entry is written before the head index. */
  1695. smp_wmb();
  1696. wq->head = next;
  1697. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1698. }
  1699. return 0;
  1700. }
  1701. /**
  1702. * qp_comm_est - handle trap with QP established
  1703. * @qp: the QP
  1704. */
  1705. void rvt_comm_est(struct rvt_qp *qp)
  1706. {
  1707. qp->r_flags |= RVT_R_COMM_EST;
  1708. if (qp->ibqp.event_handler) {
  1709. struct ib_event ev;
  1710. ev.device = qp->ibqp.device;
  1711. ev.element.qp = &qp->ibqp;
  1712. ev.event = IB_EVENT_COMM_EST;
  1713. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1714. }
  1715. }
  1716. EXPORT_SYMBOL(rvt_comm_est);
  1717. void rvt_rc_error(struct rvt_qp *qp, enum ib_wc_status err)
  1718. {
  1719. unsigned long flags;
  1720. int lastwqe;
  1721. spin_lock_irqsave(&qp->s_lock, flags);
  1722. lastwqe = rvt_error_qp(qp, err);
  1723. spin_unlock_irqrestore(&qp->s_lock, flags);
  1724. if (lastwqe) {
  1725. struct ib_event ev;
  1726. ev.device = qp->ibqp.device;
  1727. ev.element.qp = &qp->ibqp;
  1728. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1729. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1730. }
  1731. }
  1732. EXPORT_SYMBOL(rvt_rc_error);
  1733. /*
  1734. * rvt_rnr_tbl_to_usec - return index into ib_rvt_rnr_table
  1735. * @index - the index
  1736. * return usec from an index into ib_rvt_rnr_table
  1737. */
  1738. unsigned long rvt_rnr_tbl_to_usec(u32 index)
  1739. {
  1740. return ib_rvt_rnr_table[(index & IB_AETH_CREDIT_MASK)];
  1741. }
  1742. EXPORT_SYMBOL(rvt_rnr_tbl_to_usec);
  1743. static inline unsigned long rvt_aeth_to_usec(u32 aeth)
  1744. {
  1745. return ib_rvt_rnr_table[(aeth >> IB_AETH_CREDIT_SHIFT) &
  1746. IB_AETH_CREDIT_MASK];
  1747. }
  1748. /*
  1749. * rvt_add_retry_timer - add/start a retry timer
  1750. * @qp - the QP
  1751. * add a retry timer on the QP
  1752. */
  1753. void rvt_add_retry_timer(struct rvt_qp *qp)
  1754. {
  1755. struct ib_qp *ibqp = &qp->ibqp;
  1756. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1757. lockdep_assert_held(&qp->s_lock);
  1758. qp->s_flags |= RVT_S_TIMER;
  1759. /* 4.096 usec. * (1 << qp->timeout) */
  1760. qp->s_timer.expires = jiffies + qp->timeout_jiffies +
  1761. rdi->busy_jiffies;
  1762. add_timer(&qp->s_timer);
  1763. }
  1764. EXPORT_SYMBOL(rvt_add_retry_timer);
  1765. /**
  1766. * rvt_add_rnr_timer - add/start an rnr timer
  1767. * @qp - the QP
  1768. * @aeth - aeth of RNR timeout, simulated aeth for loopback
  1769. * add an rnr timer on the QP
  1770. */
  1771. void rvt_add_rnr_timer(struct rvt_qp *qp, u32 aeth)
  1772. {
  1773. u32 to;
  1774. lockdep_assert_held(&qp->s_lock);
  1775. qp->s_flags |= RVT_S_WAIT_RNR;
  1776. to = rvt_aeth_to_usec(aeth);
  1777. hrtimer_start(&qp->s_rnr_timer,
  1778. ns_to_ktime(1000 * to), HRTIMER_MODE_REL);
  1779. }
  1780. EXPORT_SYMBOL(rvt_add_rnr_timer);
  1781. /**
  1782. * rvt_stop_rc_timers - stop all timers
  1783. * @qp - the QP
  1784. * stop any pending timers
  1785. */
  1786. void rvt_stop_rc_timers(struct rvt_qp *qp)
  1787. {
  1788. lockdep_assert_held(&qp->s_lock);
  1789. /* Remove QP from all timers */
  1790. if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
  1791. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
  1792. del_timer(&qp->s_timer);
  1793. hrtimer_try_to_cancel(&qp->s_rnr_timer);
  1794. }
  1795. }
  1796. EXPORT_SYMBOL(rvt_stop_rc_timers);
  1797. /**
  1798. * rvt_stop_rnr_timer - stop an rnr timer
  1799. * @qp - the QP
  1800. *
  1801. * stop an rnr timer and return if the timer
  1802. * had been pending.
  1803. */
  1804. static int rvt_stop_rnr_timer(struct rvt_qp *qp)
  1805. {
  1806. int rval = 0;
  1807. lockdep_assert_held(&qp->s_lock);
  1808. /* Remove QP from rnr timer */
  1809. if (qp->s_flags & RVT_S_WAIT_RNR) {
  1810. qp->s_flags &= ~RVT_S_WAIT_RNR;
  1811. rval = hrtimer_try_to_cancel(&qp->s_rnr_timer);
  1812. }
  1813. return rval;
  1814. }
  1815. /**
  1816. * rvt_del_timers_sync - wait for any timeout routines to exit
  1817. * @qp - the QP
  1818. */
  1819. void rvt_del_timers_sync(struct rvt_qp *qp)
  1820. {
  1821. del_timer_sync(&qp->s_timer);
  1822. hrtimer_cancel(&qp->s_rnr_timer);
  1823. }
  1824. EXPORT_SYMBOL(rvt_del_timers_sync);
  1825. /**
  1826. * This is called from s_timer for missing responses.
  1827. */
  1828. static void rvt_rc_timeout(unsigned long arg)
  1829. {
  1830. struct rvt_qp *qp = (struct rvt_qp *)arg;
  1831. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1832. unsigned long flags;
  1833. spin_lock_irqsave(&qp->r_lock, flags);
  1834. spin_lock(&qp->s_lock);
  1835. if (qp->s_flags & RVT_S_TIMER) {
  1836. qp->s_flags &= ~RVT_S_TIMER;
  1837. del_timer(&qp->s_timer);
  1838. if (rdi->driver_f.notify_restart_rc)
  1839. rdi->driver_f.notify_restart_rc(qp,
  1840. qp->s_last_psn + 1,
  1841. 1);
  1842. rdi->driver_f.schedule_send(qp);
  1843. }
  1844. spin_unlock(&qp->s_lock);
  1845. spin_unlock_irqrestore(&qp->r_lock, flags);
  1846. }
  1847. /*
  1848. * This is called from s_timer for RNR timeouts.
  1849. */
  1850. enum hrtimer_restart rvt_rc_rnr_retry(struct hrtimer *t)
  1851. {
  1852. struct rvt_qp *qp = container_of(t, struct rvt_qp, s_rnr_timer);
  1853. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1854. unsigned long flags;
  1855. spin_lock_irqsave(&qp->s_lock, flags);
  1856. rvt_stop_rnr_timer(qp);
  1857. rdi->driver_f.schedule_send(qp);
  1858. spin_unlock_irqrestore(&qp->s_lock, flags);
  1859. return HRTIMER_NORESTART;
  1860. }
  1861. EXPORT_SYMBOL(rvt_rc_rnr_retry);