mthca_srq.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. * Copyright (c) 2005 Cisco Systems. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/slab.h>
  33. #include <linux/string.h>
  34. #include <linux/sched.h>
  35. #include <asm/io.h>
  36. #include "mthca_dev.h"
  37. #include "mthca_cmd.h"
  38. #include "mthca_memfree.h"
  39. #include "mthca_wqe.h"
  40. enum {
  41. MTHCA_MAX_DIRECT_SRQ_SIZE = 4 * PAGE_SIZE
  42. };
  43. struct mthca_tavor_srq_context {
  44. __be64 wqe_base_ds; /* low 6 bits is descriptor size */
  45. __be32 state_pd;
  46. __be32 lkey;
  47. __be32 uar;
  48. __be16 limit_watermark;
  49. __be16 wqe_cnt;
  50. u32 reserved[2];
  51. };
  52. struct mthca_arbel_srq_context {
  53. __be32 state_logsize_srqn;
  54. __be32 lkey;
  55. __be32 db_index;
  56. __be32 logstride_usrpage;
  57. __be64 wqe_base;
  58. __be32 eq_pd;
  59. __be16 limit_watermark;
  60. __be16 wqe_cnt;
  61. u16 reserved1;
  62. __be16 wqe_counter;
  63. u32 reserved2[3];
  64. };
  65. static void *get_wqe(struct mthca_srq *srq, int n)
  66. {
  67. if (srq->is_direct)
  68. return srq->queue.direct.buf + (n << srq->wqe_shift);
  69. else
  70. return srq->queue.page_list[(n << srq->wqe_shift) >> PAGE_SHIFT].buf +
  71. ((n << srq->wqe_shift) & (PAGE_SIZE - 1));
  72. }
  73. /*
  74. * Return a pointer to the location within a WQE that we're using as a
  75. * link when the WQE is in the free list. We use the imm field
  76. * because in the Tavor case, posting a WQE may overwrite the next
  77. * segment of the previous WQE, but a receive WQE will never touch the
  78. * imm field. This avoids corrupting our free list if the previous
  79. * WQE has already completed and been put on the free list when we
  80. * post the next WQE.
  81. */
  82. static inline int *wqe_to_link(void *wqe)
  83. {
  84. return (int *) (wqe + offsetof(struct mthca_next_seg, imm));
  85. }
  86. static void mthca_tavor_init_srq_context(struct mthca_dev *dev,
  87. struct mthca_pd *pd,
  88. struct mthca_srq *srq,
  89. struct mthca_tavor_srq_context *context)
  90. {
  91. memset(context, 0, sizeof *context);
  92. context->wqe_base_ds = cpu_to_be64(1 << (srq->wqe_shift - 4));
  93. context->state_pd = cpu_to_be32(pd->pd_num);
  94. context->lkey = cpu_to_be32(srq->mr.ibmr.lkey);
  95. if (pd->ibpd.uobject)
  96. context->uar =
  97. cpu_to_be32(to_mucontext(pd->ibpd.uobject->context)->uar.index);
  98. else
  99. context->uar = cpu_to_be32(dev->driver_uar.index);
  100. }
  101. static void mthca_arbel_init_srq_context(struct mthca_dev *dev,
  102. struct mthca_pd *pd,
  103. struct mthca_srq *srq,
  104. struct mthca_arbel_srq_context *context)
  105. {
  106. int logsize, max;
  107. memset(context, 0, sizeof *context);
  108. /*
  109. * Put max in a temporary variable to work around gcc bug
  110. * triggered by ilog2() on sparc64.
  111. */
  112. max = srq->max;
  113. logsize = ilog2(max);
  114. context->state_logsize_srqn = cpu_to_be32(logsize << 24 | srq->srqn);
  115. context->lkey = cpu_to_be32(srq->mr.ibmr.lkey);
  116. context->db_index = cpu_to_be32(srq->db_index);
  117. context->logstride_usrpage = cpu_to_be32((srq->wqe_shift - 4) << 29);
  118. if (pd->ibpd.uobject)
  119. context->logstride_usrpage |=
  120. cpu_to_be32(to_mucontext(pd->ibpd.uobject->context)->uar.index);
  121. else
  122. context->logstride_usrpage |= cpu_to_be32(dev->driver_uar.index);
  123. context->eq_pd = cpu_to_be32(MTHCA_EQ_ASYNC << 24 | pd->pd_num);
  124. }
  125. static void mthca_free_srq_buf(struct mthca_dev *dev, struct mthca_srq *srq)
  126. {
  127. mthca_buf_free(dev, srq->max << srq->wqe_shift, &srq->queue,
  128. srq->is_direct, &srq->mr);
  129. kfree(srq->wrid);
  130. }
  131. static int mthca_alloc_srq_buf(struct mthca_dev *dev, struct mthca_pd *pd,
  132. struct mthca_srq *srq)
  133. {
  134. struct mthca_data_seg *scatter;
  135. void *wqe;
  136. int err;
  137. int i;
  138. if (pd->ibpd.uobject)
  139. return 0;
  140. srq->wrid = kmalloc(srq->max * sizeof (u64), GFP_KERNEL);
  141. if (!srq->wrid)
  142. return -ENOMEM;
  143. err = mthca_buf_alloc(dev, srq->max << srq->wqe_shift,
  144. MTHCA_MAX_DIRECT_SRQ_SIZE,
  145. &srq->queue, &srq->is_direct, pd, 1, &srq->mr);
  146. if (err) {
  147. kfree(srq->wrid);
  148. return err;
  149. }
  150. /*
  151. * Now initialize the SRQ buffer so that all of the WQEs are
  152. * linked into the list of free WQEs. In addition, set the
  153. * scatter list L_Keys to the sentry value of 0x100.
  154. */
  155. for (i = 0; i < srq->max; ++i) {
  156. struct mthca_next_seg *next;
  157. next = wqe = get_wqe(srq, i);
  158. if (i < srq->max - 1) {
  159. *wqe_to_link(wqe) = i + 1;
  160. next->nda_op = htonl(((i + 1) << srq->wqe_shift) | 1);
  161. } else {
  162. *wqe_to_link(wqe) = -1;
  163. next->nda_op = 0;
  164. }
  165. for (scatter = wqe + sizeof (struct mthca_next_seg);
  166. (void *) scatter < wqe + (1 << srq->wqe_shift);
  167. ++scatter)
  168. scatter->lkey = cpu_to_be32(MTHCA_INVAL_LKEY);
  169. }
  170. srq->last = get_wqe(srq, srq->max - 1);
  171. return 0;
  172. }
  173. int mthca_alloc_srq(struct mthca_dev *dev, struct mthca_pd *pd,
  174. struct ib_srq_attr *attr, struct mthca_srq *srq)
  175. {
  176. struct mthca_mailbox *mailbox;
  177. int ds;
  178. int err;
  179. /* Sanity check SRQ size before proceeding */
  180. if (attr->max_wr > dev->limits.max_srq_wqes ||
  181. attr->max_sge > dev->limits.max_srq_sge)
  182. return -EINVAL;
  183. srq->max = attr->max_wr;
  184. srq->max_gs = attr->max_sge;
  185. srq->counter = 0;
  186. if (mthca_is_memfree(dev))
  187. srq->max = roundup_pow_of_two(srq->max + 1);
  188. else
  189. srq->max = srq->max + 1;
  190. ds = max(64UL,
  191. roundup_pow_of_two(sizeof (struct mthca_next_seg) +
  192. srq->max_gs * sizeof (struct mthca_data_seg)));
  193. if (!mthca_is_memfree(dev) && (ds > dev->limits.max_desc_sz))
  194. return -EINVAL;
  195. srq->wqe_shift = ilog2(ds);
  196. srq->srqn = mthca_alloc(&dev->srq_table.alloc);
  197. if (srq->srqn == -1)
  198. return -ENOMEM;
  199. if (mthca_is_memfree(dev)) {
  200. err = mthca_table_get(dev, dev->srq_table.table, srq->srqn);
  201. if (err)
  202. goto err_out;
  203. if (!pd->ibpd.uobject) {
  204. srq->db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_SRQ,
  205. srq->srqn, &srq->db);
  206. if (srq->db_index < 0) {
  207. err = -ENOMEM;
  208. goto err_out_icm;
  209. }
  210. }
  211. }
  212. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  213. if (IS_ERR(mailbox)) {
  214. err = PTR_ERR(mailbox);
  215. goto err_out_db;
  216. }
  217. err = mthca_alloc_srq_buf(dev, pd, srq);
  218. if (err)
  219. goto err_out_mailbox;
  220. spin_lock_init(&srq->lock);
  221. srq->refcount = 1;
  222. init_waitqueue_head(&srq->wait);
  223. mutex_init(&srq->mutex);
  224. if (mthca_is_memfree(dev))
  225. mthca_arbel_init_srq_context(dev, pd, srq, mailbox->buf);
  226. else
  227. mthca_tavor_init_srq_context(dev, pd, srq, mailbox->buf);
  228. err = mthca_SW2HW_SRQ(dev, mailbox, srq->srqn);
  229. if (err) {
  230. mthca_warn(dev, "SW2HW_SRQ failed (%d)\n", err);
  231. goto err_out_free_buf;
  232. }
  233. spin_lock_irq(&dev->srq_table.lock);
  234. if (mthca_array_set(&dev->srq_table.srq,
  235. srq->srqn & (dev->limits.num_srqs - 1),
  236. srq)) {
  237. spin_unlock_irq(&dev->srq_table.lock);
  238. goto err_out_free_srq;
  239. }
  240. spin_unlock_irq(&dev->srq_table.lock);
  241. mthca_free_mailbox(dev, mailbox);
  242. srq->first_free = 0;
  243. srq->last_free = srq->max - 1;
  244. attr->max_wr = srq->max - 1;
  245. attr->max_sge = srq->max_gs;
  246. return 0;
  247. err_out_free_srq:
  248. err = mthca_HW2SW_SRQ(dev, mailbox, srq->srqn);
  249. if (err)
  250. mthca_warn(dev, "HW2SW_SRQ failed (%d)\n", err);
  251. err_out_free_buf:
  252. if (!pd->ibpd.uobject)
  253. mthca_free_srq_buf(dev, srq);
  254. err_out_mailbox:
  255. mthca_free_mailbox(dev, mailbox);
  256. err_out_db:
  257. if (!pd->ibpd.uobject && mthca_is_memfree(dev))
  258. mthca_free_db(dev, MTHCA_DB_TYPE_SRQ, srq->db_index);
  259. err_out_icm:
  260. mthca_table_put(dev, dev->srq_table.table, srq->srqn);
  261. err_out:
  262. mthca_free(&dev->srq_table.alloc, srq->srqn);
  263. return err;
  264. }
  265. static inline int get_srq_refcount(struct mthca_dev *dev, struct mthca_srq *srq)
  266. {
  267. int c;
  268. spin_lock_irq(&dev->srq_table.lock);
  269. c = srq->refcount;
  270. spin_unlock_irq(&dev->srq_table.lock);
  271. return c;
  272. }
  273. void mthca_free_srq(struct mthca_dev *dev, struct mthca_srq *srq)
  274. {
  275. struct mthca_mailbox *mailbox;
  276. int err;
  277. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  278. if (IS_ERR(mailbox)) {
  279. mthca_warn(dev, "No memory for mailbox to free SRQ.\n");
  280. return;
  281. }
  282. err = mthca_HW2SW_SRQ(dev, mailbox, srq->srqn);
  283. if (err)
  284. mthca_warn(dev, "HW2SW_SRQ failed (%d)\n", err);
  285. spin_lock_irq(&dev->srq_table.lock);
  286. mthca_array_clear(&dev->srq_table.srq,
  287. srq->srqn & (dev->limits.num_srqs - 1));
  288. --srq->refcount;
  289. spin_unlock_irq(&dev->srq_table.lock);
  290. wait_event(srq->wait, !get_srq_refcount(dev, srq));
  291. if (!srq->ibsrq.uobject) {
  292. mthca_free_srq_buf(dev, srq);
  293. if (mthca_is_memfree(dev))
  294. mthca_free_db(dev, MTHCA_DB_TYPE_SRQ, srq->db_index);
  295. }
  296. mthca_table_put(dev, dev->srq_table.table, srq->srqn);
  297. mthca_free(&dev->srq_table.alloc, srq->srqn);
  298. mthca_free_mailbox(dev, mailbox);
  299. }
  300. int mthca_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  301. enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
  302. {
  303. struct mthca_dev *dev = to_mdev(ibsrq->device);
  304. struct mthca_srq *srq = to_msrq(ibsrq);
  305. int ret = 0;
  306. /* We don't support resizing SRQs (yet?) */
  307. if (attr_mask & IB_SRQ_MAX_WR)
  308. return -EINVAL;
  309. if (attr_mask & IB_SRQ_LIMIT) {
  310. u32 max_wr = mthca_is_memfree(dev) ? srq->max - 1 : srq->max;
  311. if (attr->srq_limit > max_wr)
  312. return -EINVAL;
  313. mutex_lock(&srq->mutex);
  314. ret = mthca_ARM_SRQ(dev, srq->srqn, attr->srq_limit);
  315. mutex_unlock(&srq->mutex);
  316. }
  317. return ret;
  318. }
  319. int mthca_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
  320. {
  321. struct mthca_dev *dev = to_mdev(ibsrq->device);
  322. struct mthca_srq *srq = to_msrq(ibsrq);
  323. struct mthca_mailbox *mailbox;
  324. struct mthca_arbel_srq_context *arbel_ctx;
  325. struct mthca_tavor_srq_context *tavor_ctx;
  326. int err;
  327. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  328. if (IS_ERR(mailbox))
  329. return PTR_ERR(mailbox);
  330. err = mthca_QUERY_SRQ(dev, srq->srqn, mailbox);
  331. if (err)
  332. goto out;
  333. if (mthca_is_memfree(dev)) {
  334. arbel_ctx = mailbox->buf;
  335. srq_attr->srq_limit = be16_to_cpu(arbel_ctx->limit_watermark);
  336. } else {
  337. tavor_ctx = mailbox->buf;
  338. srq_attr->srq_limit = be16_to_cpu(tavor_ctx->limit_watermark);
  339. }
  340. srq_attr->max_wr = srq->max - 1;
  341. srq_attr->max_sge = srq->max_gs;
  342. out:
  343. mthca_free_mailbox(dev, mailbox);
  344. return err;
  345. }
  346. void mthca_srq_event(struct mthca_dev *dev, u32 srqn,
  347. enum ib_event_type event_type)
  348. {
  349. struct mthca_srq *srq;
  350. struct ib_event event;
  351. spin_lock(&dev->srq_table.lock);
  352. srq = mthca_array_get(&dev->srq_table.srq, srqn & (dev->limits.num_srqs - 1));
  353. if (srq)
  354. ++srq->refcount;
  355. spin_unlock(&dev->srq_table.lock);
  356. if (!srq) {
  357. mthca_warn(dev, "Async event for bogus SRQ %08x\n", srqn);
  358. return;
  359. }
  360. if (!srq->ibsrq.event_handler)
  361. goto out;
  362. event.device = &dev->ib_dev;
  363. event.event = event_type;
  364. event.element.srq = &srq->ibsrq;
  365. srq->ibsrq.event_handler(&event, srq->ibsrq.srq_context);
  366. out:
  367. spin_lock(&dev->srq_table.lock);
  368. if (!--srq->refcount)
  369. wake_up(&srq->wait);
  370. spin_unlock(&dev->srq_table.lock);
  371. }
  372. /*
  373. * This function must be called with IRQs disabled.
  374. */
  375. void mthca_free_srq_wqe(struct mthca_srq *srq, u32 wqe_addr)
  376. {
  377. int ind;
  378. struct mthca_next_seg *last_free;
  379. ind = wqe_addr >> srq->wqe_shift;
  380. spin_lock(&srq->lock);
  381. last_free = get_wqe(srq, srq->last_free);
  382. *wqe_to_link(last_free) = ind;
  383. last_free->nda_op = htonl((ind << srq->wqe_shift) | 1);
  384. *wqe_to_link(get_wqe(srq, ind)) = -1;
  385. srq->last_free = ind;
  386. spin_unlock(&srq->lock);
  387. }
  388. int mthca_tavor_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  389. struct ib_recv_wr **bad_wr)
  390. {
  391. struct mthca_dev *dev = to_mdev(ibsrq->device);
  392. struct mthca_srq *srq = to_msrq(ibsrq);
  393. unsigned long flags;
  394. int err = 0;
  395. int first_ind;
  396. int ind;
  397. int next_ind;
  398. int nreq;
  399. int i;
  400. void *wqe;
  401. void *prev_wqe;
  402. spin_lock_irqsave(&srq->lock, flags);
  403. first_ind = srq->first_free;
  404. for (nreq = 0; wr; wr = wr->next) {
  405. ind = srq->first_free;
  406. wqe = get_wqe(srq, ind);
  407. next_ind = *wqe_to_link(wqe);
  408. if (unlikely(next_ind < 0)) {
  409. mthca_err(dev, "SRQ %06x full\n", srq->srqn);
  410. err = -ENOMEM;
  411. *bad_wr = wr;
  412. break;
  413. }
  414. prev_wqe = srq->last;
  415. srq->last = wqe;
  416. ((struct mthca_next_seg *) wqe)->ee_nds = 0;
  417. /* flags field will always remain 0 */
  418. wqe += sizeof (struct mthca_next_seg);
  419. if (unlikely(wr->num_sge > srq->max_gs)) {
  420. err = -EINVAL;
  421. *bad_wr = wr;
  422. srq->last = prev_wqe;
  423. break;
  424. }
  425. for (i = 0; i < wr->num_sge; ++i) {
  426. mthca_set_data_seg(wqe, wr->sg_list + i);
  427. wqe += sizeof (struct mthca_data_seg);
  428. }
  429. if (i < srq->max_gs)
  430. mthca_set_data_seg_inval(wqe);
  431. ((struct mthca_next_seg *) prev_wqe)->ee_nds =
  432. cpu_to_be32(MTHCA_NEXT_DBD);
  433. srq->wrid[ind] = wr->wr_id;
  434. srq->first_free = next_ind;
  435. ++nreq;
  436. if (unlikely(nreq == MTHCA_TAVOR_MAX_WQES_PER_RECV_DB)) {
  437. nreq = 0;
  438. /*
  439. * Make sure that descriptors are written
  440. * before doorbell is rung.
  441. */
  442. wmb();
  443. mthca_write64(first_ind << srq->wqe_shift, srq->srqn << 8,
  444. dev->kar + MTHCA_RECEIVE_DOORBELL,
  445. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  446. first_ind = srq->first_free;
  447. }
  448. }
  449. if (likely(nreq)) {
  450. /*
  451. * Make sure that descriptors are written before
  452. * doorbell is rung.
  453. */
  454. wmb();
  455. mthca_write64(first_ind << srq->wqe_shift, (srq->srqn << 8) | nreq,
  456. dev->kar + MTHCA_RECEIVE_DOORBELL,
  457. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  458. }
  459. /*
  460. * Make sure doorbells don't leak out of SRQ spinlock and
  461. * reach the HCA out of order:
  462. */
  463. mmiowb();
  464. spin_unlock_irqrestore(&srq->lock, flags);
  465. return err;
  466. }
  467. int mthca_arbel_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  468. struct ib_recv_wr **bad_wr)
  469. {
  470. struct mthca_dev *dev = to_mdev(ibsrq->device);
  471. struct mthca_srq *srq = to_msrq(ibsrq);
  472. unsigned long flags;
  473. int err = 0;
  474. int ind;
  475. int next_ind;
  476. int nreq;
  477. int i;
  478. void *wqe;
  479. spin_lock_irqsave(&srq->lock, flags);
  480. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  481. ind = srq->first_free;
  482. wqe = get_wqe(srq, ind);
  483. next_ind = *wqe_to_link(wqe);
  484. if (unlikely(next_ind < 0)) {
  485. mthca_err(dev, "SRQ %06x full\n", srq->srqn);
  486. err = -ENOMEM;
  487. *bad_wr = wr;
  488. break;
  489. }
  490. ((struct mthca_next_seg *) wqe)->ee_nds = 0;
  491. /* flags field will always remain 0 */
  492. wqe += sizeof (struct mthca_next_seg);
  493. if (unlikely(wr->num_sge > srq->max_gs)) {
  494. err = -EINVAL;
  495. *bad_wr = wr;
  496. break;
  497. }
  498. for (i = 0; i < wr->num_sge; ++i) {
  499. mthca_set_data_seg(wqe, wr->sg_list + i);
  500. wqe += sizeof (struct mthca_data_seg);
  501. }
  502. if (i < srq->max_gs)
  503. mthca_set_data_seg_inval(wqe);
  504. srq->wrid[ind] = wr->wr_id;
  505. srq->first_free = next_ind;
  506. }
  507. if (likely(nreq)) {
  508. srq->counter += nreq;
  509. /*
  510. * Make sure that descriptors are written before
  511. * we write doorbell record.
  512. */
  513. wmb();
  514. *srq->db = cpu_to_be32(srq->counter);
  515. }
  516. spin_unlock_irqrestore(&srq->lock, flags);
  517. return err;
  518. }
  519. int mthca_max_srq_sge(struct mthca_dev *dev)
  520. {
  521. if (mthca_is_memfree(dev))
  522. return dev->limits.max_sg;
  523. /*
  524. * SRQ allocations are based on powers of 2 for Tavor,
  525. * (although they only need to be multiples of 16 bytes).
  526. *
  527. * Therefore, we need to base the max number of sg entries on
  528. * the largest power of 2 descriptor size that is <= to the
  529. * actual max WQE descriptor size, rather than return the
  530. * max_sg value given by the firmware (which is based on WQE
  531. * sizes as multiples of 16, not powers of 2).
  532. *
  533. * If SRQ implementation is changed for Tavor to be based on
  534. * multiples of 16, the calculation below can be deleted and
  535. * the FW max_sg value returned.
  536. */
  537. return min_t(int, dev->limits.max_sg,
  538. ((1 << (fls(dev->limits.max_desc_sz) - 1)) -
  539. sizeof (struct mthca_next_seg)) /
  540. sizeof (struct mthca_data_seg));
  541. }
  542. int mthca_init_srq_table(struct mthca_dev *dev)
  543. {
  544. int err;
  545. if (!(dev->mthca_flags & MTHCA_FLAG_SRQ))
  546. return 0;
  547. spin_lock_init(&dev->srq_table.lock);
  548. err = mthca_alloc_init(&dev->srq_table.alloc,
  549. dev->limits.num_srqs,
  550. dev->limits.num_srqs - 1,
  551. dev->limits.reserved_srqs);
  552. if (err)
  553. return err;
  554. err = mthca_array_init(&dev->srq_table.srq,
  555. dev->limits.num_srqs);
  556. if (err)
  557. mthca_alloc_cleanup(&dev->srq_table.alloc);
  558. return err;
  559. }
  560. void mthca_cleanup_srq_table(struct mthca_dev *dev)
  561. {
  562. if (!(dev->mthca_flags & MTHCA_FLAG_SRQ))
  563. return;
  564. mthca_array_cleanup(&dev->srq_table.srq, dev->limits.num_srqs);
  565. mthca_alloc_cleanup(&dev->srq_table.alloc);
  566. }