mthca_srq.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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. u8 status;
  178. int ds;
  179. int err;
  180. /* Sanity check SRQ size before proceeding */
  181. if (attr->max_wr > dev->limits.max_srq_wqes ||
  182. attr->max_sge > dev->limits.max_srq_sge)
  183. return -EINVAL;
  184. srq->max = attr->max_wr;
  185. srq->max_gs = attr->max_sge;
  186. srq->counter = 0;
  187. if (mthca_is_memfree(dev))
  188. srq->max = roundup_pow_of_two(srq->max + 1);
  189. else
  190. srq->max = srq->max + 1;
  191. ds = max(64UL,
  192. roundup_pow_of_two(sizeof (struct mthca_next_seg) +
  193. srq->max_gs * sizeof (struct mthca_data_seg)));
  194. if (!mthca_is_memfree(dev) && (ds > dev->limits.max_desc_sz))
  195. return -EINVAL;
  196. srq->wqe_shift = ilog2(ds);
  197. srq->srqn = mthca_alloc(&dev->srq_table.alloc);
  198. if (srq->srqn == -1)
  199. return -ENOMEM;
  200. if (mthca_is_memfree(dev)) {
  201. err = mthca_table_get(dev, dev->srq_table.table, srq->srqn);
  202. if (err)
  203. goto err_out;
  204. if (!pd->ibpd.uobject) {
  205. srq->db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_SRQ,
  206. srq->srqn, &srq->db);
  207. if (srq->db_index < 0) {
  208. err = -ENOMEM;
  209. goto err_out_icm;
  210. }
  211. }
  212. }
  213. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  214. if (IS_ERR(mailbox)) {
  215. err = PTR_ERR(mailbox);
  216. goto err_out_db;
  217. }
  218. err = mthca_alloc_srq_buf(dev, pd, srq);
  219. if (err)
  220. goto err_out_mailbox;
  221. spin_lock_init(&srq->lock);
  222. srq->refcount = 1;
  223. init_waitqueue_head(&srq->wait);
  224. mutex_init(&srq->mutex);
  225. if (mthca_is_memfree(dev))
  226. mthca_arbel_init_srq_context(dev, pd, srq, mailbox->buf);
  227. else
  228. mthca_tavor_init_srq_context(dev, pd, srq, mailbox->buf);
  229. err = mthca_SW2HW_SRQ(dev, mailbox, srq->srqn, &status);
  230. if (err) {
  231. mthca_warn(dev, "SW2HW_SRQ failed (%d)\n", err);
  232. goto err_out_free_buf;
  233. }
  234. if (status) {
  235. mthca_warn(dev, "SW2HW_SRQ returned status 0x%02x\n",
  236. status);
  237. err = -EINVAL;
  238. goto err_out_free_buf;
  239. }
  240. spin_lock_irq(&dev->srq_table.lock);
  241. if (mthca_array_set(&dev->srq_table.srq,
  242. srq->srqn & (dev->limits.num_srqs - 1),
  243. srq)) {
  244. spin_unlock_irq(&dev->srq_table.lock);
  245. goto err_out_free_srq;
  246. }
  247. spin_unlock_irq(&dev->srq_table.lock);
  248. mthca_free_mailbox(dev, mailbox);
  249. srq->first_free = 0;
  250. srq->last_free = srq->max - 1;
  251. attr->max_wr = srq->max - 1;
  252. attr->max_sge = srq->max_gs;
  253. return 0;
  254. err_out_free_srq:
  255. err = mthca_HW2SW_SRQ(dev, mailbox, srq->srqn, &status);
  256. if (err)
  257. mthca_warn(dev, "HW2SW_SRQ failed (%d)\n", err);
  258. else if (status)
  259. mthca_warn(dev, "HW2SW_SRQ returned status 0x%02x\n", status);
  260. err_out_free_buf:
  261. if (!pd->ibpd.uobject)
  262. mthca_free_srq_buf(dev, srq);
  263. err_out_mailbox:
  264. mthca_free_mailbox(dev, mailbox);
  265. err_out_db:
  266. if (!pd->ibpd.uobject && mthca_is_memfree(dev))
  267. mthca_free_db(dev, MTHCA_DB_TYPE_SRQ, srq->db_index);
  268. err_out_icm:
  269. mthca_table_put(dev, dev->srq_table.table, srq->srqn);
  270. err_out:
  271. mthca_free(&dev->srq_table.alloc, srq->srqn);
  272. return err;
  273. }
  274. static inline int get_srq_refcount(struct mthca_dev *dev, struct mthca_srq *srq)
  275. {
  276. int c;
  277. spin_lock_irq(&dev->srq_table.lock);
  278. c = srq->refcount;
  279. spin_unlock_irq(&dev->srq_table.lock);
  280. return c;
  281. }
  282. void mthca_free_srq(struct mthca_dev *dev, struct mthca_srq *srq)
  283. {
  284. struct mthca_mailbox *mailbox;
  285. int err;
  286. u8 status;
  287. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  288. if (IS_ERR(mailbox)) {
  289. mthca_warn(dev, "No memory for mailbox to free SRQ.\n");
  290. return;
  291. }
  292. err = mthca_HW2SW_SRQ(dev, mailbox, srq->srqn, &status);
  293. if (err)
  294. mthca_warn(dev, "HW2SW_SRQ failed (%d)\n", err);
  295. else if (status)
  296. mthca_warn(dev, "HW2SW_SRQ returned status 0x%02x\n", status);
  297. spin_lock_irq(&dev->srq_table.lock);
  298. mthca_array_clear(&dev->srq_table.srq,
  299. srq->srqn & (dev->limits.num_srqs - 1));
  300. --srq->refcount;
  301. spin_unlock_irq(&dev->srq_table.lock);
  302. wait_event(srq->wait, !get_srq_refcount(dev, srq));
  303. if (!srq->ibsrq.uobject) {
  304. mthca_free_srq_buf(dev, srq);
  305. if (mthca_is_memfree(dev))
  306. mthca_free_db(dev, MTHCA_DB_TYPE_SRQ, srq->db_index);
  307. }
  308. mthca_table_put(dev, dev->srq_table.table, srq->srqn);
  309. mthca_free(&dev->srq_table.alloc, srq->srqn);
  310. mthca_free_mailbox(dev, mailbox);
  311. }
  312. int mthca_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  313. enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
  314. {
  315. struct mthca_dev *dev = to_mdev(ibsrq->device);
  316. struct mthca_srq *srq = to_msrq(ibsrq);
  317. int ret;
  318. u8 status;
  319. /* We don't support resizing SRQs (yet?) */
  320. if (attr_mask & IB_SRQ_MAX_WR)
  321. return -EINVAL;
  322. if (attr_mask & IB_SRQ_LIMIT) {
  323. u32 max_wr = mthca_is_memfree(dev) ? srq->max - 1 : srq->max;
  324. if (attr->srq_limit > max_wr)
  325. return -EINVAL;
  326. mutex_lock(&srq->mutex);
  327. ret = mthca_ARM_SRQ(dev, srq->srqn, attr->srq_limit, &status);
  328. mutex_unlock(&srq->mutex);
  329. if (ret)
  330. return ret;
  331. if (status)
  332. return -EINVAL;
  333. }
  334. return 0;
  335. }
  336. int mthca_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
  337. {
  338. struct mthca_dev *dev = to_mdev(ibsrq->device);
  339. struct mthca_srq *srq = to_msrq(ibsrq);
  340. struct mthca_mailbox *mailbox;
  341. struct mthca_arbel_srq_context *arbel_ctx;
  342. struct mthca_tavor_srq_context *tavor_ctx;
  343. u8 status;
  344. int err;
  345. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  346. if (IS_ERR(mailbox))
  347. return PTR_ERR(mailbox);
  348. err = mthca_QUERY_SRQ(dev, srq->srqn, mailbox, &status);
  349. if (err)
  350. goto out;
  351. if (mthca_is_memfree(dev)) {
  352. arbel_ctx = mailbox->buf;
  353. srq_attr->srq_limit = be16_to_cpu(arbel_ctx->limit_watermark);
  354. } else {
  355. tavor_ctx = mailbox->buf;
  356. srq_attr->srq_limit = be16_to_cpu(tavor_ctx->limit_watermark);
  357. }
  358. srq_attr->max_wr = srq->max - 1;
  359. srq_attr->max_sge = srq->max_gs;
  360. out:
  361. mthca_free_mailbox(dev, mailbox);
  362. return err;
  363. }
  364. void mthca_srq_event(struct mthca_dev *dev, u32 srqn,
  365. enum ib_event_type event_type)
  366. {
  367. struct mthca_srq *srq;
  368. struct ib_event event;
  369. spin_lock(&dev->srq_table.lock);
  370. srq = mthca_array_get(&dev->srq_table.srq, srqn & (dev->limits.num_srqs - 1));
  371. if (srq)
  372. ++srq->refcount;
  373. spin_unlock(&dev->srq_table.lock);
  374. if (!srq) {
  375. mthca_warn(dev, "Async event for bogus SRQ %08x\n", srqn);
  376. return;
  377. }
  378. if (!srq->ibsrq.event_handler)
  379. goto out;
  380. event.device = &dev->ib_dev;
  381. event.event = event_type;
  382. event.element.srq = &srq->ibsrq;
  383. srq->ibsrq.event_handler(&event, srq->ibsrq.srq_context);
  384. out:
  385. spin_lock(&dev->srq_table.lock);
  386. if (!--srq->refcount)
  387. wake_up(&srq->wait);
  388. spin_unlock(&dev->srq_table.lock);
  389. }
  390. /*
  391. * This function must be called with IRQs disabled.
  392. */
  393. void mthca_free_srq_wqe(struct mthca_srq *srq, u32 wqe_addr)
  394. {
  395. int ind;
  396. struct mthca_next_seg *last_free;
  397. ind = wqe_addr >> srq->wqe_shift;
  398. spin_lock(&srq->lock);
  399. last_free = get_wqe(srq, srq->last_free);
  400. *wqe_to_link(last_free) = ind;
  401. last_free->nda_op = htonl((ind << srq->wqe_shift) | 1);
  402. *wqe_to_link(get_wqe(srq, ind)) = -1;
  403. srq->last_free = ind;
  404. spin_unlock(&srq->lock);
  405. }
  406. int mthca_tavor_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  407. struct ib_recv_wr **bad_wr)
  408. {
  409. struct mthca_dev *dev = to_mdev(ibsrq->device);
  410. struct mthca_srq *srq = to_msrq(ibsrq);
  411. unsigned long flags;
  412. int err = 0;
  413. int first_ind;
  414. int ind;
  415. int next_ind;
  416. int nreq;
  417. int i;
  418. void *wqe;
  419. void *prev_wqe;
  420. spin_lock_irqsave(&srq->lock, flags);
  421. first_ind = srq->first_free;
  422. for (nreq = 0; wr; wr = wr->next) {
  423. ind = srq->first_free;
  424. wqe = get_wqe(srq, ind);
  425. next_ind = *wqe_to_link(wqe);
  426. if (unlikely(next_ind < 0)) {
  427. mthca_err(dev, "SRQ %06x full\n", srq->srqn);
  428. err = -ENOMEM;
  429. *bad_wr = wr;
  430. break;
  431. }
  432. prev_wqe = srq->last;
  433. srq->last = wqe;
  434. ((struct mthca_next_seg *) wqe)->ee_nds = 0;
  435. /* flags field will always remain 0 */
  436. wqe += sizeof (struct mthca_next_seg);
  437. if (unlikely(wr->num_sge > srq->max_gs)) {
  438. err = -EINVAL;
  439. *bad_wr = wr;
  440. srq->last = prev_wqe;
  441. break;
  442. }
  443. for (i = 0; i < wr->num_sge; ++i) {
  444. mthca_set_data_seg(wqe, wr->sg_list + i);
  445. wqe += sizeof (struct mthca_data_seg);
  446. }
  447. if (i < srq->max_gs)
  448. mthca_set_data_seg_inval(wqe);
  449. ((struct mthca_next_seg *) prev_wqe)->ee_nds =
  450. cpu_to_be32(MTHCA_NEXT_DBD);
  451. srq->wrid[ind] = wr->wr_id;
  452. srq->first_free = next_ind;
  453. ++nreq;
  454. if (unlikely(nreq == MTHCA_TAVOR_MAX_WQES_PER_RECV_DB)) {
  455. nreq = 0;
  456. /*
  457. * Make sure that descriptors are written
  458. * before doorbell is rung.
  459. */
  460. wmb();
  461. mthca_write64(first_ind << srq->wqe_shift, srq->srqn << 8,
  462. dev->kar + MTHCA_RECEIVE_DOORBELL,
  463. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  464. first_ind = srq->first_free;
  465. }
  466. }
  467. if (likely(nreq)) {
  468. /*
  469. * Make sure that descriptors are written before
  470. * doorbell is rung.
  471. */
  472. wmb();
  473. mthca_write64(first_ind << srq->wqe_shift, (srq->srqn << 8) | nreq,
  474. dev->kar + MTHCA_RECEIVE_DOORBELL,
  475. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  476. }
  477. /*
  478. * Make sure doorbells don't leak out of SRQ spinlock and
  479. * reach the HCA out of order:
  480. */
  481. mmiowb();
  482. spin_unlock_irqrestore(&srq->lock, flags);
  483. return err;
  484. }
  485. int mthca_arbel_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  486. struct ib_recv_wr **bad_wr)
  487. {
  488. struct mthca_dev *dev = to_mdev(ibsrq->device);
  489. struct mthca_srq *srq = to_msrq(ibsrq);
  490. unsigned long flags;
  491. int err = 0;
  492. int ind;
  493. int next_ind;
  494. int nreq;
  495. int i;
  496. void *wqe;
  497. spin_lock_irqsave(&srq->lock, flags);
  498. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  499. ind = srq->first_free;
  500. wqe = get_wqe(srq, ind);
  501. next_ind = *wqe_to_link(wqe);
  502. if (unlikely(next_ind < 0)) {
  503. mthca_err(dev, "SRQ %06x full\n", srq->srqn);
  504. err = -ENOMEM;
  505. *bad_wr = wr;
  506. break;
  507. }
  508. ((struct mthca_next_seg *) wqe)->ee_nds = 0;
  509. /* flags field will always remain 0 */
  510. wqe += sizeof (struct mthca_next_seg);
  511. if (unlikely(wr->num_sge > srq->max_gs)) {
  512. err = -EINVAL;
  513. *bad_wr = wr;
  514. break;
  515. }
  516. for (i = 0; i < wr->num_sge; ++i) {
  517. mthca_set_data_seg(wqe, wr->sg_list + i);
  518. wqe += sizeof (struct mthca_data_seg);
  519. }
  520. if (i < srq->max_gs)
  521. mthca_set_data_seg_inval(wqe);
  522. srq->wrid[ind] = wr->wr_id;
  523. srq->first_free = next_ind;
  524. }
  525. if (likely(nreq)) {
  526. srq->counter += nreq;
  527. /*
  528. * Make sure that descriptors are written before
  529. * we write doorbell record.
  530. */
  531. wmb();
  532. *srq->db = cpu_to_be32(srq->counter);
  533. }
  534. spin_unlock_irqrestore(&srq->lock, flags);
  535. return err;
  536. }
  537. int mthca_max_srq_sge(struct mthca_dev *dev)
  538. {
  539. if (mthca_is_memfree(dev))
  540. return dev->limits.max_sg;
  541. /*
  542. * SRQ allocations are based on powers of 2 for Tavor,
  543. * (although they only need to be multiples of 16 bytes).
  544. *
  545. * Therefore, we need to base the max number of sg entries on
  546. * the largest power of 2 descriptor size that is <= to the
  547. * actual max WQE descriptor size, rather than return the
  548. * max_sg value given by the firmware (which is based on WQE
  549. * sizes as multiples of 16, not powers of 2).
  550. *
  551. * If SRQ implementation is changed for Tavor to be based on
  552. * multiples of 16, the calculation below can be deleted and
  553. * the FW max_sg value returned.
  554. */
  555. return min_t(int, dev->limits.max_sg,
  556. ((1 << (fls(dev->limits.max_desc_sz) - 1)) -
  557. sizeof (struct mthca_next_seg)) /
  558. sizeof (struct mthca_data_seg));
  559. }
  560. int mthca_init_srq_table(struct mthca_dev *dev)
  561. {
  562. int err;
  563. if (!(dev->mthca_flags & MTHCA_FLAG_SRQ))
  564. return 0;
  565. spin_lock_init(&dev->srq_table.lock);
  566. err = mthca_alloc_init(&dev->srq_table.alloc,
  567. dev->limits.num_srqs,
  568. dev->limits.num_srqs - 1,
  569. dev->limits.reserved_srqs);
  570. if (err)
  571. return err;
  572. err = mthca_array_init(&dev->srq_table.srq,
  573. dev->limits.num_srqs);
  574. if (err)
  575. mthca_alloc_cleanup(&dev->srq_table.alloc);
  576. return err;
  577. }
  578. void mthca_cleanup_srq_table(struct mthca_dev *dev)
  579. {
  580. if (!(dev->mthca_flags & MTHCA_FLAG_SRQ))
  581. return;
  582. mthca_array_cleanup(&dev->srq_table.srq, dev->limits.num_srqs);
  583. mthca_alloc_cleanup(&dev->srq_table.alloc);
  584. }