mthca_srq.c 17 KB

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