qp.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  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 bits at shift and below */
  329. WARN_ON(offset & (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. if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
  352. rvt_put_ss(&qp->s_rdma_read_sge);
  353. rvt_put_ss(&qp->r_sge);
  354. if (clr_sends) {
  355. while (qp->s_last != qp->s_head) {
  356. struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  357. unsigned i;
  358. for (i = 0; i < wqe->wr.num_sge; i++) {
  359. struct rvt_sge *sge = &wqe->sg_list[i];
  360. rvt_put_mr(sge->mr);
  361. }
  362. if (qp->ibqp.qp_type == IB_QPT_UD ||
  363. qp->ibqp.qp_type == IB_QPT_SMI ||
  364. qp->ibqp.qp_type == IB_QPT_GSI)
  365. atomic_dec(&ibah_to_rvtah(
  366. wqe->ud_wr.ah)->refcount);
  367. if (++qp->s_last >= qp->s_size)
  368. qp->s_last = 0;
  369. smp_wmb(); /* see qp_set_savail */
  370. }
  371. if (qp->s_rdma_mr) {
  372. rvt_put_mr(qp->s_rdma_mr);
  373. qp->s_rdma_mr = NULL;
  374. }
  375. }
  376. if (qp->ibqp.qp_type != IB_QPT_RC)
  377. return;
  378. for (n = 0; n < ARRAY_SIZE(qp->s_ack_queue); n++) {
  379. struct rvt_ack_entry *e = &qp->s_ack_queue[n];
  380. if (e->opcode == IB_OPCODE_RC_RDMA_READ_REQUEST &&
  381. 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. if (atomic_dec_and_test(&qp->refcount))
  430. wake_up(&qp->wait);
  431. }
  432. }
  433. /**
  434. * reset_qp - initialize the QP state to the reset state
  435. * @qp: the QP to reset
  436. * @type: the QP type
  437. * r and s lock are required to be held by the caller
  438. */
  439. static void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  440. enum ib_qp_type type)
  441. {
  442. if (qp->state != IB_QPS_RESET) {
  443. qp->state = IB_QPS_RESET;
  444. /* Let drivers flush their waitlist */
  445. rdi->driver_f.flush_qp_waiters(qp);
  446. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
  447. spin_unlock(&qp->s_lock);
  448. spin_unlock(&qp->s_hlock);
  449. spin_unlock_irq(&qp->r_lock);
  450. /* Stop the send queue and the retry timer */
  451. rdi->driver_f.stop_send_queue(qp);
  452. /* Wait for things to stop */
  453. rdi->driver_f.quiesce_qp(qp);
  454. /* take qp out the hash and wait for it to be unused */
  455. rvt_remove_qp(rdi, qp);
  456. wait_event(qp->wait, !atomic_read(&qp->refcount));
  457. /* grab the lock b/c it was locked at call time */
  458. spin_lock_irq(&qp->r_lock);
  459. spin_lock(&qp->s_hlock);
  460. spin_lock(&qp->s_lock);
  461. rvt_clear_mr_refs(qp, 1);
  462. }
  463. /*
  464. * Let the driver do any tear down it needs to for a qp
  465. * that has been reset
  466. */
  467. rdi->driver_f.notify_qp_reset(qp);
  468. qp->remote_qpn = 0;
  469. qp->qkey = 0;
  470. qp->qp_access_flags = 0;
  471. qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
  472. qp->s_hdrwords = 0;
  473. qp->s_wqe = NULL;
  474. qp->s_draining = 0;
  475. qp->s_next_psn = 0;
  476. qp->s_last_psn = 0;
  477. qp->s_sending_psn = 0;
  478. qp->s_sending_hpsn = 0;
  479. qp->s_psn = 0;
  480. qp->r_psn = 0;
  481. qp->r_msn = 0;
  482. if (type == IB_QPT_RC) {
  483. qp->s_state = IB_OPCODE_RC_SEND_LAST;
  484. qp->r_state = IB_OPCODE_RC_SEND_LAST;
  485. } else {
  486. qp->s_state = IB_OPCODE_UC_SEND_LAST;
  487. qp->r_state = IB_OPCODE_UC_SEND_LAST;
  488. }
  489. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  490. qp->r_nak_state = 0;
  491. qp->r_aflags = 0;
  492. qp->r_flags = 0;
  493. qp->s_head = 0;
  494. qp->s_tail = 0;
  495. qp->s_cur = 0;
  496. qp->s_acked = 0;
  497. qp->s_last = 0;
  498. qp->s_ssn = 1;
  499. qp->s_lsn = 0;
  500. qp->s_mig_state = IB_MIG_MIGRATED;
  501. memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
  502. qp->r_head_ack_queue = 0;
  503. qp->s_tail_ack_queue = 0;
  504. qp->s_num_rd_atomic = 0;
  505. if (qp->r_rq.wq) {
  506. qp->r_rq.wq->head = 0;
  507. qp->r_rq.wq->tail = 0;
  508. }
  509. qp->r_sge.num_sge = 0;
  510. }
  511. /**
  512. * rvt_create_qp - create a queue pair for a device
  513. * @ibpd: the protection domain who's device we create the queue pair for
  514. * @init_attr: the attributes of the queue pair
  515. * @udata: user data for libibverbs.so
  516. *
  517. * Queue pair creation is mostly an rvt issue. However, drivers have their own
  518. * unique idea of what queue pair numbers mean. For instance there is a reserved
  519. * range for PSM.
  520. *
  521. * Return: the queue pair on success, otherwise returns an errno.
  522. *
  523. * Called by the ib_create_qp() core verbs function.
  524. */
  525. struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
  526. struct ib_qp_init_attr *init_attr,
  527. struct ib_udata *udata)
  528. {
  529. struct rvt_qp *qp;
  530. int err;
  531. struct rvt_swqe *swq = NULL;
  532. size_t sz;
  533. size_t sg_list_sz;
  534. struct ib_qp *ret = ERR_PTR(-ENOMEM);
  535. struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
  536. void *priv = NULL;
  537. gfp_t gfp;
  538. if (!rdi)
  539. return ERR_PTR(-EINVAL);
  540. if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
  541. init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
  542. init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
  543. return ERR_PTR(-EINVAL);
  544. /* GFP_NOIO is applicable to RC QP's only */
  545. if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
  546. init_attr->qp_type != IB_QPT_RC)
  547. return ERR_PTR(-EINVAL);
  548. gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
  549. GFP_NOIO : GFP_KERNEL;
  550. /* Check receive queue parameters if no SRQ is specified. */
  551. if (!init_attr->srq) {
  552. if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
  553. init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
  554. return ERR_PTR(-EINVAL);
  555. if (init_attr->cap.max_send_sge +
  556. init_attr->cap.max_send_wr +
  557. init_attr->cap.max_recv_sge +
  558. init_attr->cap.max_recv_wr == 0)
  559. return ERR_PTR(-EINVAL);
  560. }
  561. switch (init_attr->qp_type) {
  562. case IB_QPT_SMI:
  563. case IB_QPT_GSI:
  564. if (init_attr->port_num == 0 ||
  565. init_attr->port_num > ibpd->device->phys_port_cnt)
  566. return ERR_PTR(-EINVAL);
  567. case IB_QPT_UC:
  568. case IB_QPT_RC:
  569. case IB_QPT_UD:
  570. sz = sizeof(struct rvt_sge) *
  571. init_attr->cap.max_send_sge +
  572. sizeof(struct rvt_swqe);
  573. if (gfp == GFP_NOIO)
  574. swq = __vmalloc(
  575. (init_attr->cap.max_send_wr + 1) * sz,
  576. gfp, PAGE_KERNEL);
  577. else
  578. swq = vmalloc_node(
  579. (init_attr->cap.max_send_wr + 1) * sz,
  580. rdi->dparms.node);
  581. if (!swq)
  582. return ERR_PTR(-ENOMEM);
  583. sz = sizeof(*qp);
  584. sg_list_sz = 0;
  585. if (init_attr->srq) {
  586. struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
  587. if (srq->rq.max_sge > 1)
  588. sg_list_sz = sizeof(*qp->r_sg_list) *
  589. (srq->rq.max_sge - 1);
  590. } else if (init_attr->cap.max_recv_sge > 1)
  591. sg_list_sz = sizeof(*qp->r_sg_list) *
  592. (init_attr->cap.max_recv_sge - 1);
  593. qp = kzalloc_node(sz + sg_list_sz, gfp, rdi->dparms.node);
  594. if (!qp)
  595. goto bail_swq;
  596. RCU_INIT_POINTER(qp->next, NULL);
  597. /*
  598. * Driver needs to set up it's private QP structure and do any
  599. * initialization that is needed.
  600. */
  601. priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
  602. if (!priv)
  603. goto bail_qp;
  604. qp->priv = priv;
  605. qp->timeout_jiffies =
  606. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  607. 1000UL);
  608. if (init_attr->srq) {
  609. sz = 0;
  610. } else {
  611. qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
  612. qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
  613. sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
  614. sizeof(struct rvt_rwqe);
  615. if (udata)
  616. qp->r_rq.wq = vmalloc_user(
  617. sizeof(struct rvt_rwq) +
  618. qp->r_rq.size * sz);
  619. else if (gfp == GFP_NOIO)
  620. qp->r_rq.wq = __vmalloc(
  621. sizeof(struct rvt_rwq) +
  622. qp->r_rq.size * sz,
  623. gfp, PAGE_KERNEL);
  624. else
  625. qp->r_rq.wq = vmalloc_node(
  626. sizeof(struct rvt_rwq) +
  627. qp->r_rq.size * sz,
  628. rdi->dparms.node);
  629. if (!qp->r_rq.wq)
  630. goto bail_driver_priv;
  631. }
  632. /*
  633. * ib_create_qp() will initialize qp->ibqp
  634. * except for qp->ibqp.qp_num.
  635. */
  636. spin_lock_init(&qp->r_lock);
  637. spin_lock_init(&qp->s_hlock);
  638. spin_lock_init(&qp->s_lock);
  639. spin_lock_init(&qp->r_rq.lock);
  640. atomic_set(&qp->refcount, 0);
  641. init_waitqueue_head(&qp->wait);
  642. init_timer(&qp->s_timer);
  643. qp->s_timer.data = (unsigned long)qp;
  644. INIT_LIST_HEAD(&qp->rspwait);
  645. qp->state = IB_QPS_RESET;
  646. qp->s_wq = swq;
  647. qp->s_size = init_attr->cap.max_send_wr + 1;
  648. qp->s_avail = init_attr->cap.max_send_wr;
  649. qp->s_max_sge = init_attr->cap.max_send_sge;
  650. if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
  651. qp->s_flags = RVT_S_SIGNAL_REQ_WR;
  652. err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
  653. init_attr->qp_type,
  654. init_attr->port_num, gfp);
  655. if (err < 0) {
  656. ret = ERR_PTR(err);
  657. goto bail_rq_wq;
  658. }
  659. qp->ibqp.qp_num = err;
  660. qp->port_num = init_attr->port_num;
  661. rvt_reset_qp(rdi, qp, init_attr->qp_type);
  662. break;
  663. default:
  664. /* Don't support raw QPs */
  665. return ERR_PTR(-EINVAL);
  666. }
  667. init_attr->cap.max_inline_data = 0;
  668. /*
  669. * Return the address of the RWQ as the offset to mmap.
  670. * See rvt_mmap() for details.
  671. */
  672. if (udata && udata->outlen >= sizeof(__u64)) {
  673. if (!qp->r_rq.wq) {
  674. __u64 offset = 0;
  675. err = ib_copy_to_udata(udata, &offset,
  676. sizeof(offset));
  677. if (err) {
  678. ret = ERR_PTR(err);
  679. goto bail_qpn;
  680. }
  681. } else {
  682. u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
  683. qp->ip = rvt_create_mmap_info(rdi, s,
  684. ibpd->uobject->context,
  685. qp->r_rq.wq);
  686. if (!qp->ip) {
  687. ret = ERR_PTR(-ENOMEM);
  688. goto bail_qpn;
  689. }
  690. err = ib_copy_to_udata(udata, &qp->ip->offset,
  691. sizeof(qp->ip->offset));
  692. if (err) {
  693. ret = ERR_PTR(err);
  694. goto bail_ip;
  695. }
  696. }
  697. }
  698. spin_lock(&rdi->n_qps_lock);
  699. if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
  700. spin_unlock(&rdi->n_qps_lock);
  701. ret = ERR_PTR(-ENOMEM);
  702. goto bail_ip;
  703. }
  704. rdi->n_qps_allocated++;
  705. /*
  706. * Maintain a busy_jiffies variable that will be added to the timeout
  707. * period in mod_retry_timer and add_retry_timer. This busy jiffies
  708. * is scaled by the number of rc qps created for the device to reduce
  709. * the number of timeouts occurring when there is a large number of
  710. * qps. busy_jiffies is incremented every rc qp scaling interval.
  711. * The scaling interval is selected based on extensive performance
  712. * evaluation of targeted workloads.
  713. */
  714. if (init_attr->qp_type == IB_QPT_RC) {
  715. rdi->n_rc_qps++;
  716. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  717. }
  718. spin_unlock(&rdi->n_qps_lock);
  719. if (qp->ip) {
  720. spin_lock_irq(&rdi->pending_lock);
  721. list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
  722. spin_unlock_irq(&rdi->pending_lock);
  723. }
  724. ret = &qp->ibqp;
  725. /*
  726. * We have our QP and its good, now keep track of what types of opcodes
  727. * can be processed on this QP. We do this by keeping track of what the
  728. * 3 high order bits of the opcode are.
  729. */
  730. switch (init_attr->qp_type) {
  731. case IB_QPT_SMI:
  732. case IB_QPT_GSI:
  733. case IB_QPT_UD:
  734. qp->allowed_ops = IB_OPCODE_UD_SEND_ONLY & RVT_OPCODE_QP_MASK;
  735. break;
  736. case IB_QPT_RC:
  737. qp->allowed_ops = IB_OPCODE_RC_SEND_ONLY & RVT_OPCODE_QP_MASK;
  738. break;
  739. case IB_QPT_UC:
  740. qp->allowed_ops = IB_OPCODE_UC_SEND_ONLY & RVT_OPCODE_QP_MASK;
  741. break;
  742. default:
  743. ret = ERR_PTR(-EINVAL);
  744. goto bail_ip;
  745. }
  746. return ret;
  747. bail_ip:
  748. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  749. bail_qpn:
  750. free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  751. bail_rq_wq:
  752. vfree(qp->r_rq.wq);
  753. bail_driver_priv:
  754. rdi->driver_f.qp_priv_free(rdi, qp);
  755. bail_qp:
  756. kfree(qp);
  757. bail_swq:
  758. vfree(swq);
  759. return ret;
  760. }
  761. /**
  762. * rvt_error_qp - put a QP into the error state
  763. * @qp: the QP to put into the error state
  764. * @err: the receive completion error to signal if a RWQE is active
  765. *
  766. * Flushes both send and receive work queues.
  767. *
  768. * Return: true if last WQE event should be generated.
  769. * The QP r_lock and s_lock should be held and interrupts disabled.
  770. * If we are already in error state, just return.
  771. */
  772. int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
  773. {
  774. struct ib_wc wc;
  775. int ret = 0;
  776. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  777. if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
  778. goto bail;
  779. qp->state = IB_QPS_ERR;
  780. if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
  781. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
  782. del_timer(&qp->s_timer);
  783. }
  784. if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
  785. qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
  786. rdi->driver_f.notify_error_qp(qp);
  787. /* Schedule the sending tasklet to drain the send work queue. */
  788. if (ACCESS_ONCE(qp->s_last) != qp->s_head)
  789. rdi->driver_f.schedule_send(qp);
  790. rvt_clear_mr_refs(qp, 0);
  791. memset(&wc, 0, sizeof(wc));
  792. wc.qp = &qp->ibqp;
  793. wc.opcode = IB_WC_RECV;
  794. if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
  795. wc.wr_id = qp->r_wr_id;
  796. wc.status = err;
  797. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  798. }
  799. wc.status = IB_WC_WR_FLUSH_ERR;
  800. if (qp->r_rq.wq) {
  801. struct rvt_rwq *wq;
  802. u32 head;
  803. u32 tail;
  804. spin_lock(&qp->r_rq.lock);
  805. /* sanity check pointers before trusting them */
  806. wq = qp->r_rq.wq;
  807. head = wq->head;
  808. if (head >= qp->r_rq.size)
  809. head = 0;
  810. tail = wq->tail;
  811. if (tail >= qp->r_rq.size)
  812. tail = 0;
  813. while (tail != head) {
  814. wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
  815. if (++tail >= qp->r_rq.size)
  816. tail = 0;
  817. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  818. }
  819. wq->tail = tail;
  820. spin_unlock(&qp->r_rq.lock);
  821. } else if (qp->ibqp.event_handler) {
  822. ret = 1;
  823. }
  824. bail:
  825. return ret;
  826. }
  827. EXPORT_SYMBOL(rvt_error_qp);
  828. /*
  829. * Put the QP into the hash table.
  830. * The hash table holds a reference to the QP.
  831. */
  832. static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  833. {
  834. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  835. unsigned long flags;
  836. atomic_inc(&qp->refcount);
  837. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  838. if (qp->ibqp.qp_num <= 1) {
  839. rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
  840. } else {
  841. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  842. qp->next = rdi->qp_dev->qp_table[n];
  843. rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
  844. trace_rvt_qpinsert(qp, n);
  845. }
  846. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  847. }
  848. /**
  849. * qib_modify_qp - modify the attributes of a queue pair
  850. * @ibqp: the queue pair who's attributes we're modifying
  851. * @attr: the new attributes
  852. * @attr_mask: the mask of attributes to modify
  853. * @udata: user data for libibverbs.so
  854. *
  855. * Return: 0 on success, otherwise returns an errno.
  856. */
  857. int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  858. int attr_mask, struct ib_udata *udata)
  859. {
  860. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  861. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  862. enum ib_qp_state cur_state, new_state;
  863. struct ib_event ev;
  864. int lastwqe = 0;
  865. int mig = 0;
  866. int pmtu = 0; /* for gcc warning only */
  867. enum rdma_link_layer link;
  868. link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
  869. spin_lock_irq(&qp->r_lock);
  870. spin_lock(&qp->s_hlock);
  871. spin_lock(&qp->s_lock);
  872. cur_state = attr_mask & IB_QP_CUR_STATE ?
  873. attr->cur_qp_state : qp->state;
  874. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  875. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  876. attr_mask, link))
  877. goto inval;
  878. if (rdi->driver_f.check_modify_qp &&
  879. rdi->driver_f.check_modify_qp(qp, attr, attr_mask, udata))
  880. goto inval;
  881. if (attr_mask & IB_QP_AV) {
  882. if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
  883. goto inval;
  884. if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
  885. goto inval;
  886. }
  887. if (attr_mask & IB_QP_ALT_PATH) {
  888. if (attr->alt_ah_attr.dlid >=
  889. be16_to_cpu(IB_MULTICAST_LID_BASE))
  890. goto inval;
  891. if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
  892. goto inval;
  893. if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
  894. goto inval;
  895. }
  896. if (attr_mask & IB_QP_PKEY_INDEX)
  897. if (attr->pkey_index >= rvt_get_npkeys(rdi))
  898. goto inval;
  899. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  900. if (attr->min_rnr_timer > 31)
  901. goto inval;
  902. if (attr_mask & IB_QP_PORT)
  903. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  904. qp->ibqp.qp_type == IB_QPT_GSI ||
  905. attr->port_num == 0 ||
  906. attr->port_num > ibqp->device->phys_port_cnt)
  907. goto inval;
  908. if (attr_mask & IB_QP_DEST_QPN)
  909. if (attr->dest_qp_num > RVT_QPN_MASK)
  910. goto inval;
  911. if (attr_mask & IB_QP_RETRY_CNT)
  912. if (attr->retry_cnt > 7)
  913. goto inval;
  914. if (attr_mask & IB_QP_RNR_RETRY)
  915. if (attr->rnr_retry > 7)
  916. goto inval;
  917. /*
  918. * Don't allow invalid path_mtu values. OK to set greater
  919. * than the active mtu (or even the max_cap, if we have tuned
  920. * that to a small mtu. We'll set qp->path_mtu
  921. * to the lesser of requested attribute mtu and active,
  922. * for packetizing messages.
  923. * Note that the QP port has to be set in INIT and MTU in RTR.
  924. */
  925. if (attr_mask & IB_QP_PATH_MTU) {
  926. pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
  927. if (pmtu < 0)
  928. goto inval;
  929. }
  930. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  931. if (attr->path_mig_state == IB_MIG_REARM) {
  932. if (qp->s_mig_state == IB_MIG_ARMED)
  933. goto inval;
  934. if (new_state != IB_QPS_RTS)
  935. goto inval;
  936. } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
  937. if (qp->s_mig_state == IB_MIG_REARM)
  938. goto inval;
  939. if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
  940. goto inval;
  941. if (qp->s_mig_state == IB_MIG_ARMED)
  942. mig = 1;
  943. } else {
  944. goto inval;
  945. }
  946. }
  947. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  948. if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
  949. goto inval;
  950. switch (new_state) {
  951. case IB_QPS_RESET:
  952. if (qp->state != IB_QPS_RESET)
  953. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  954. break;
  955. case IB_QPS_RTR:
  956. /* Allow event to re-trigger if QP set to RTR more than once */
  957. qp->r_flags &= ~RVT_R_COMM_EST;
  958. qp->state = new_state;
  959. break;
  960. case IB_QPS_SQD:
  961. qp->s_draining = qp->s_last != qp->s_cur;
  962. qp->state = new_state;
  963. break;
  964. case IB_QPS_SQE:
  965. if (qp->ibqp.qp_type == IB_QPT_RC)
  966. goto inval;
  967. qp->state = new_state;
  968. break;
  969. case IB_QPS_ERR:
  970. lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  971. break;
  972. default:
  973. qp->state = new_state;
  974. break;
  975. }
  976. if (attr_mask & IB_QP_PKEY_INDEX)
  977. qp->s_pkey_index = attr->pkey_index;
  978. if (attr_mask & IB_QP_PORT)
  979. qp->port_num = attr->port_num;
  980. if (attr_mask & IB_QP_DEST_QPN)
  981. qp->remote_qpn = attr->dest_qp_num;
  982. if (attr_mask & IB_QP_SQ_PSN) {
  983. qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
  984. qp->s_psn = qp->s_next_psn;
  985. qp->s_sending_psn = qp->s_next_psn;
  986. qp->s_last_psn = qp->s_next_psn - 1;
  987. qp->s_sending_hpsn = qp->s_last_psn;
  988. }
  989. if (attr_mask & IB_QP_RQ_PSN)
  990. qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
  991. if (attr_mask & IB_QP_ACCESS_FLAGS)
  992. qp->qp_access_flags = attr->qp_access_flags;
  993. if (attr_mask & IB_QP_AV) {
  994. qp->remote_ah_attr = attr->ah_attr;
  995. qp->s_srate = attr->ah_attr.static_rate;
  996. qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
  997. }
  998. if (attr_mask & IB_QP_ALT_PATH) {
  999. qp->alt_ah_attr = attr->alt_ah_attr;
  1000. qp->s_alt_pkey_index = attr->alt_pkey_index;
  1001. }
  1002. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  1003. qp->s_mig_state = attr->path_mig_state;
  1004. if (mig) {
  1005. qp->remote_ah_attr = qp->alt_ah_attr;
  1006. qp->port_num = qp->alt_ah_attr.port_num;
  1007. qp->s_pkey_index = qp->s_alt_pkey_index;
  1008. }
  1009. }
  1010. if (attr_mask & IB_QP_PATH_MTU) {
  1011. qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
  1012. qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
  1013. qp->log_pmtu = ilog2(qp->pmtu);
  1014. }
  1015. if (attr_mask & IB_QP_RETRY_CNT) {
  1016. qp->s_retry_cnt = attr->retry_cnt;
  1017. qp->s_retry = attr->retry_cnt;
  1018. }
  1019. if (attr_mask & IB_QP_RNR_RETRY) {
  1020. qp->s_rnr_retry_cnt = attr->rnr_retry;
  1021. qp->s_rnr_retry = attr->rnr_retry;
  1022. }
  1023. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  1024. qp->r_min_rnr_timer = attr->min_rnr_timer;
  1025. if (attr_mask & IB_QP_TIMEOUT) {
  1026. qp->timeout = attr->timeout;
  1027. qp->timeout_jiffies =
  1028. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  1029. 1000UL);
  1030. }
  1031. if (attr_mask & IB_QP_QKEY)
  1032. qp->qkey = attr->qkey;
  1033. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1034. qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
  1035. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  1036. qp->s_max_rd_atomic = attr->max_rd_atomic;
  1037. if (rdi->driver_f.modify_qp)
  1038. rdi->driver_f.modify_qp(qp, attr, attr_mask, udata);
  1039. spin_unlock(&qp->s_lock);
  1040. spin_unlock(&qp->s_hlock);
  1041. spin_unlock_irq(&qp->r_lock);
  1042. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1043. rvt_insert_qp(rdi, qp);
  1044. if (lastwqe) {
  1045. ev.device = qp->ibqp.device;
  1046. ev.element.qp = &qp->ibqp;
  1047. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1048. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1049. }
  1050. if (mig) {
  1051. ev.device = qp->ibqp.device;
  1052. ev.element.qp = &qp->ibqp;
  1053. ev.event = IB_EVENT_PATH_MIG;
  1054. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1055. }
  1056. return 0;
  1057. inval:
  1058. spin_unlock(&qp->s_lock);
  1059. spin_unlock(&qp->s_hlock);
  1060. spin_unlock_irq(&qp->r_lock);
  1061. return -EINVAL;
  1062. }
  1063. /** rvt_free_qpn - Free a qpn from the bit map
  1064. * @qpt: QP table
  1065. * @qpn: queue pair number to free
  1066. */
  1067. static void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
  1068. {
  1069. struct rvt_qpn_map *map;
  1070. map = qpt->map + qpn / RVT_BITS_PER_PAGE;
  1071. if (map->page)
  1072. clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
  1073. }
  1074. /**
  1075. * rvt_destroy_qp - destroy a queue pair
  1076. * @ibqp: the queue pair to destroy
  1077. *
  1078. * Note that this can be called while the QP is actively sending or
  1079. * receiving!
  1080. *
  1081. * Return: 0 on success.
  1082. */
  1083. int rvt_destroy_qp(struct ib_qp *ibqp)
  1084. {
  1085. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1086. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1087. spin_lock_irq(&qp->r_lock);
  1088. spin_lock(&qp->s_hlock);
  1089. spin_lock(&qp->s_lock);
  1090. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  1091. spin_unlock(&qp->s_lock);
  1092. spin_unlock(&qp->s_hlock);
  1093. spin_unlock_irq(&qp->r_lock);
  1094. /* qpn is now available for use again */
  1095. rvt_free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  1096. spin_lock(&rdi->n_qps_lock);
  1097. rdi->n_qps_allocated--;
  1098. if (qp->ibqp.qp_type == IB_QPT_RC) {
  1099. rdi->n_rc_qps--;
  1100. rdi->busy_jiffies = rdi->n_rc_qps / RC_QP_SCALING_INTERVAL;
  1101. }
  1102. spin_unlock(&rdi->n_qps_lock);
  1103. if (qp->ip)
  1104. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  1105. else
  1106. vfree(qp->r_rq.wq);
  1107. vfree(qp->s_wq);
  1108. rdi->driver_f.qp_priv_free(rdi, qp);
  1109. kfree(qp);
  1110. return 0;
  1111. }
  1112. /**
  1113. * rvt_query_qp - query an ipbq
  1114. * @ibqp: IB qp to query
  1115. * @attr: attr struct to fill in
  1116. * @attr_mask: attr mask ignored
  1117. * @init_attr: struct to fill in
  1118. *
  1119. * Return: always 0
  1120. */
  1121. int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1122. int attr_mask, struct ib_qp_init_attr *init_attr)
  1123. {
  1124. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1125. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1126. attr->qp_state = qp->state;
  1127. attr->cur_qp_state = attr->qp_state;
  1128. attr->path_mtu = qp->path_mtu;
  1129. attr->path_mig_state = qp->s_mig_state;
  1130. attr->qkey = qp->qkey;
  1131. attr->rq_psn = qp->r_psn & rdi->dparms.psn_mask;
  1132. attr->sq_psn = qp->s_next_psn & rdi->dparms.psn_mask;
  1133. attr->dest_qp_num = qp->remote_qpn;
  1134. attr->qp_access_flags = qp->qp_access_flags;
  1135. attr->cap.max_send_wr = qp->s_size - 1;
  1136. attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
  1137. attr->cap.max_send_sge = qp->s_max_sge;
  1138. attr->cap.max_recv_sge = qp->r_rq.max_sge;
  1139. attr->cap.max_inline_data = 0;
  1140. attr->ah_attr = qp->remote_ah_attr;
  1141. attr->alt_ah_attr = qp->alt_ah_attr;
  1142. attr->pkey_index = qp->s_pkey_index;
  1143. attr->alt_pkey_index = qp->s_alt_pkey_index;
  1144. attr->en_sqd_async_notify = 0;
  1145. attr->sq_draining = qp->s_draining;
  1146. attr->max_rd_atomic = qp->s_max_rd_atomic;
  1147. attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
  1148. attr->min_rnr_timer = qp->r_min_rnr_timer;
  1149. attr->port_num = qp->port_num;
  1150. attr->timeout = qp->timeout;
  1151. attr->retry_cnt = qp->s_retry_cnt;
  1152. attr->rnr_retry = qp->s_rnr_retry_cnt;
  1153. attr->alt_port_num = qp->alt_ah_attr.port_num;
  1154. attr->alt_timeout = qp->alt_timeout;
  1155. init_attr->event_handler = qp->ibqp.event_handler;
  1156. init_attr->qp_context = qp->ibqp.qp_context;
  1157. init_attr->send_cq = qp->ibqp.send_cq;
  1158. init_attr->recv_cq = qp->ibqp.recv_cq;
  1159. init_attr->srq = qp->ibqp.srq;
  1160. init_attr->cap = attr->cap;
  1161. if (qp->s_flags & RVT_S_SIGNAL_REQ_WR)
  1162. init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  1163. else
  1164. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  1165. init_attr->qp_type = qp->ibqp.qp_type;
  1166. init_attr->port_num = qp->port_num;
  1167. return 0;
  1168. }
  1169. /**
  1170. * rvt_post_receive - post a receive on a QP
  1171. * @ibqp: the QP to post the receive on
  1172. * @wr: the WR to post
  1173. * @bad_wr: the first bad WR is put here
  1174. *
  1175. * This may be called from interrupt context.
  1176. *
  1177. * Return: 0 on success otherwise errno
  1178. */
  1179. int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1180. struct ib_recv_wr **bad_wr)
  1181. {
  1182. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1183. struct rvt_rwq *wq = qp->r_rq.wq;
  1184. unsigned long flags;
  1185. /* Check that state is OK to post receive. */
  1186. if (!(ib_rvt_state_ops[qp->state] & RVT_POST_RECV_OK) || !wq) {
  1187. *bad_wr = wr;
  1188. return -EINVAL;
  1189. }
  1190. for (; wr; wr = wr->next) {
  1191. struct rvt_rwqe *wqe;
  1192. u32 next;
  1193. int i;
  1194. if ((unsigned)wr->num_sge > qp->r_rq.max_sge) {
  1195. *bad_wr = wr;
  1196. return -EINVAL;
  1197. }
  1198. spin_lock_irqsave(&qp->r_rq.lock, flags);
  1199. next = wq->head + 1;
  1200. if (next >= qp->r_rq.size)
  1201. next = 0;
  1202. if (next == wq->tail) {
  1203. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1204. *bad_wr = wr;
  1205. return -ENOMEM;
  1206. }
  1207. wqe = rvt_get_rwqe_ptr(&qp->r_rq, wq->head);
  1208. wqe->wr_id = wr->wr_id;
  1209. wqe->num_sge = wr->num_sge;
  1210. for (i = 0; i < wr->num_sge; i++)
  1211. wqe->sg_list[i] = wr->sg_list[i];
  1212. /* Make sure queue entry is written before the head index. */
  1213. smp_wmb();
  1214. wq->head = next;
  1215. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  1216. }
  1217. return 0;
  1218. }
  1219. /**
  1220. * qp_get_savail - return number of avail send entries
  1221. *
  1222. * @qp - the qp
  1223. *
  1224. * This assumes the s_hlock is held but the s_last
  1225. * qp variable is uncontrolled.
  1226. */
  1227. static inline u32 qp_get_savail(struct rvt_qp *qp)
  1228. {
  1229. u32 slast;
  1230. u32 ret;
  1231. smp_read_barrier_depends(); /* see rc.c */
  1232. slast = ACCESS_ONCE(qp->s_last);
  1233. if (qp->s_head >= slast)
  1234. ret = qp->s_size - (qp->s_head - slast);
  1235. else
  1236. ret = slast - qp->s_head;
  1237. return ret - 1;
  1238. }
  1239. /**
  1240. * rvt_post_one_wr - post one RC, UC, or UD send work request
  1241. * @qp: the QP to post on
  1242. * @wr: the work request to send
  1243. */
  1244. static int rvt_post_one_wr(struct rvt_qp *qp, struct ib_send_wr *wr)
  1245. {
  1246. struct rvt_swqe *wqe;
  1247. u32 next;
  1248. int i;
  1249. int j;
  1250. int acc;
  1251. struct rvt_lkey_table *rkt;
  1252. struct rvt_pd *pd;
  1253. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1254. u8 log_pmtu;
  1255. int ret;
  1256. /* IB spec says that num_sge == 0 is OK. */
  1257. if (unlikely(wr->num_sge > qp->s_max_sge))
  1258. return -EINVAL;
  1259. /*
  1260. * Don't allow RDMA reads or atomic operations on UC or
  1261. * undefined operations.
  1262. * Make sure buffer is large enough to hold the result for atomics.
  1263. */
  1264. if (qp->ibqp.qp_type == IB_QPT_UC) {
  1265. if ((unsigned)wr->opcode >= IB_WR_RDMA_READ)
  1266. return -EINVAL;
  1267. } else if (qp->ibqp.qp_type != IB_QPT_RC) {
  1268. /* Check IB_QPT_SMI, IB_QPT_GSI, IB_QPT_UD opcode */
  1269. if (wr->opcode != IB_WR_SEND &&
  1270. wr->opcode != IB_WR_SEND_WITH_IMM)
  1271. return -EINVAL;
  1272. /* Check UD destination address PD */
  1273. if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
  1274. return -EINVAL;
  1275. } else if ((unsigned)wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
  1276. return -EINVAL;
  1277. } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
  1278. (wr->num_sge == 0 ||
  1279. wr->sg_list[0].length < sizeof(u64) ||
  1280. wr->sg_list[0].addr & (sizeof(u64) - 1))) {
  1281. return -EINVAL;
  1282. } else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic) {
  1283. return -EINVAL;
  1284. }
  1285. /* check for avail */
  1286. if (unlikely(!qp->s_avail)) {
  1287. qp->s_avail = qp_get_savail(qp);
  1288. if (WARN_ON(qp->s_avail > (qp->s_size - 1)))
  1289. rvt_pr_err(rdi,
  1290. "More avail entries than QP RB size.\nQP: %u, size: %u, avail: %u\nhead: %u, tail: %u, cur: %u, acked: %u, last: %u",
  1291. qp->ibqp.qp_num, qp->s_size, qp->s_avail,
  1292. qp->s_head, qp->s_tail, qp->s_cur,
  1293. qp->s_acked, qp->s_last);
  1294. if (!qp->s_avail)
  1295. return -ENOMEM;
  1296. }
  1297. next = qp->s_head + 1;
  1298. if (next >= qp->s_size)
  1299. next = 0;
  1300. rkt = &rdi->lkey_table;
  1301. pd = ibpd_to_rvtpd(qp->ibqp.pd);
  1302. wqe = rvt_get_swqe_ptr(qp, qp->s_head);
  1303. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1304. qp->ibqp.qp_type != IB_QPT_RC)
  1305. memcpy(&wqe->ud_wr, ud_wr(wr), sizeof(wqe->ud_wr));
  1306. else if (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
  1307. wr->opcode == IB_WR_RDMA_WRITE ||
  1308. wr->opcode == IB_WR_RDMA_READ)
  1309. memcpy(&wqe->rdma_wr, rdma_wr(wr), sizeof(wqe->rdma_wr));
  1310. else if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  1311. wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
  1312. memcpy(&wqe->atomic_wr, atomic_wr(wr), sizeof(wqe->atomic_wr));
  1313. else
  1314. memcpy(&wqe->wr, wr, sizeof(wqe->wr));
  1315. wqe->length = 0;
  1316. j = 0;
  1317. if (wr->num_sge) {
  1318. acc = wr->opcode >= IB_WR_RDMA_READ ?
  1319. IB_ACCESS_LOCAL_WRITE : 0;
  1320. for (i = 0; i < wr->num_sge; i++) {
  1321. u32 length = wr->sg_list[i].length;
  1322. int ok;
  1323. if (length == 0)
  1324. continue;
  1325. ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
  1326. &wr->sg_list[i], acc);
  1327. if (!ok) {
  1328. ret = -EINVAL;
  1329. goto bail_inval_free;
  1330. }
  1331. wqe->length += length;
  1332. j++;
  1333. }
  1334. wqe->wr.num_sge = j;
  1335. }
  1336. /* general part of wqe valid - allow for driver checks */
  1337. if (rdi->driver_f.check_send_wqe) {
  1338. ret = rdi->driver_f.check_send_wqe(qp, wqe);
  1339. if (ret)
  1340. goto bail_inval_free;
  1341. }
  1342. log_pmtu = qp->log_pmtu;
  1343. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1344. qp->ibqp.qp_type != IB_QPT_RC) {
  1345. struct rvt_ah *ah = ibah_to_rvtah(wqe->ud_wr.ah);
  1346. log_pmtu = ah->log_pmtu;
  1347. atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
  1348. }
  1349. wqe->ssn = qp->s_ssn++;
  1350. wqe->psn = qp->s_next_psn;
  1351. wqe->lpsn = wqe->psn +
  1352. (wqe->length ? ((wqe->length - 1) >> log_pmtu) : 0);
  1353. qp->s_next_psn = wqe->lpsn + 1;
  1354. trace_rvt_post_one_wr(qp, wqe);
  1355. smp_wmb(); /* see request builders */
  1356. qp->s_avail--;
  1357. qp->s_head = next;
  1358. return 0;
  1359. bail_inval_free:
  1360. /* release mr holds */
  1361. while (j) {
  1362. struct rvt_sge *sge = &wqe->sg_list[--j];
  1363. rvt_put_mr(sge->mr);
  1364. }
  1365. return ret;
  1366. }
  1367. /**
  1368. * rvt_post_send - post a send on a QP
  1369. * @ibqp: the QP to post the send on
  1370. * @wr: the list of work requests to post
  1371. * @bad_wr: the first bad WR is put here
  1372. *
  1373. * This may be called from interrupt context.
  1374. *
  1375. * Return: 0 on success else errno
  1376. */
  1377. int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1378. struct ib_send_wr **bad_wr)
  1379. {
  1380. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1381. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1382. unsigned long flags = 0;
  1383. int call_send;
  1384. unsigned nreq = 0;
  1385. int err = 0;
  1386. spin_lock_irqsave(&qp->s_hlock, flags);
  1387. /*
  1388. * Ensure QP state is such that we can send. If not bail out early,
  1389. * there is no need to do this every time we post a send.
  1390. */
  1391. if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
  1392. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1393. return -EINVAL;
  1394. }
  1395. /*
  1396. * If the send queue is empty, and we only have a single WR then just go
  1397. * ahead and kick the send engine into gear. Otherwise we will always
  1398. * just schedule the send to happen later.
  1399. */
  1400. call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
  1401. for (; wr; wr = wr->next) {
  1402. err = rvt_post_one_wr(qp, wr);
  1403. if (unlikely(err)) {
  1404. *bad_wr = wr;
  1405. goto bail;
  1406. }
  1407. nreq++;
  1408. }
  1409. bail:
  1410. spin_unlock_irqrestore(&qp->s_hlock, flags);
  1411. if (nreq) {
  1412. if (call_send)
  1413. rdi->driver_f.schedule_send_no_lock(qp);
  1414. else
  1415. rdi->driver_f.do_send(qp);
  1416. }
  1417. return err;
  1418. }
  1419. /**
  1420. * rvt_post_srq_receive - post a receive on a shared receive queue
  1421. * @ibsrq: the SRQ to post the receive on
  1422. * @wr: the list of work requests to post
  1423. * @bad_wr: A pointer to the first WR to cause a problem is put here
  1424. *
  1425. * This may be called from interrupt context.
  1426. *
  1427. * Return: 0 on success else errno
  1428. */
  1429. int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  1430. struct ib_recv_wr **bad_wr)
  1431. {
  1432. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  1433. struct rvt_rwq *wq;
  1434. unsigned long flags;
  1435. for (; wr; wr = wr->next) {
  1436. struct rvt_rwqe *wqe;
  1437. u32 next;
  1438. int i;
  1439. if ((unsigned)wr->num_sge > srq->rq.max_sge) {
  1440. *bad_wr = wr;
  1441. return -EINVAL;
  1442. }
  1443. spin_lock_irqsave(&srq->rq.lock, flags);
  1444. wq = srq->rq.wq;
  1445. next = wq->head + 1;
  1446. if (next >= srq->rq.size)
  1447. next = 0;
  1448. if (next == wq->tail) {
  1449. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1450. *bad_wr = wr;
  1451. return -ENOMEM;
  1452. }
  1453. wqe = rvt_get_rwqe_ptr(&srq->rq, wq->head);
  1454. wqe->wr_id = wr->wr_id;
  1455. wqe->num_sge = wr->num_sge;
  1456. for (i = 0; i < wr->num_sge; i++)
  1457. wqe->sg_list[i] = wr->sg_list[i];
  1458. /* Make sure queue entry is written before the head index. */
  1459. smp_wmb();
  1460. wq->head = next;
  1461. spin_unlock_irqrestore(&srq->rq.lock, flags);
  1462. }
  1463. return 0;
  1464. }