qp.c 47 KB

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