mthca_srq.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. * $Id: mthca_srq.c 3047 2005-08-10 03:59:35Z roland $
  33. */
  34. #include <linux/slab.h>
  35. #include <linux/string.h>
  36. #include <asm/io.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. {
  92. memset(context, 0, sizeof *context);
  93. context->wqe_base_ds = cpu_to_be64(1 << (srq->wqe_shift - 4));
  94. context->state_pd = cpu_to_be32(pd->pd_num);
  95. context->lkey = cpu_to_be32(srq->mr.ibmr.lkey);
  96. if (pd->ibpd.uobject)
  97. context->uar =
  98. cpu_to_be32(to_mucontext(pd->ibpd.uobject->context)->uar.index);
  99. else
  100. context->uar = cpu_to_be32(dev->driver_uar.index);
  101. }
  102. static void mthca_arbel_init_srq_context(struct mthca_dev *dev,
  103. struct mthca_pd *pd,
  104. struct mthca_srq *srq,
  105. struct mthca_arbel_srq_context *context)
  106. {
  107. int logsize;
  108. memset(context, 0, sizeof *context);
  109. logsize = long_log2(srq->max) + srq->wqe_shift;
  110. context->state_logsize_srqn = cpu_to_be32(logsize << 24 | srq->srqn);
  111. context->lkey = cpu_to_be32(srq->mr.ibmr.lkey);
  112. context->db_index = cpu_to_be32(srq->db_index);
  113. context->logstride_usrpage = cpu_to_be32((srq->wqe_shift - 4) << 29);
  114. if (pd->ibpd.uobject)
  115. context->logstride_usrpage |=
  116. cpu_to_be32(to_mucontext(pd->ibpd.uobject->context)->uar.index);
  117. else
  118. context->logstride_usrpage |= cpu_to_be32(dev->driver_uar.index);
  119. context->eq_pd = cpu_to_be32(MTHCA_EQ_ASYNC << 24 | pd->pd_num);
  120. }
  121. static void mthca_free_srq_buf(struct mthca_dev *dev, struct mthca_srq *srq)
  122. {
  123. mthca_buf_free(dev, srq->max << srq->wqe_shift, &srq->queue,
  124. srq->is_direct, &srq->mr);
  125. kfree(srq->wrid);
  126. }
  127. static int mthca_alloc_srq_buf(struct mthca_dev *dev, struct mthca_pd *pd,
  128. struct mthca_srq *srq)
  129. {
  130. struct mthca_data_seg *scatter;
  131. void *wqe;
  132. int err;
  133. int i;
  134. if (pd->ibpd.uobject)
  135. return 0;
  136. srq->wrid = kmalloc(srq->max * sizeof (u64), GFP_KERNEL);
  137. if (!srq->wrid)
  138. return -ENOMEM;
  139. err = mthca_buf_alloc(dev, srq->max << srq->wqe_shift,
  140. MTHCA_MAX_DIRECT_SRQ_SIZE,
  141. &srq->queue, &srq->is_direct, pd, 1, &srq->mr);
  142. if (err) {
  143. kfree(srq->wrid);
  144. return err;
  145. }
  146. /*
  147. * Now initialize the SRQ buffer so that all of the WQEs are
  148. * linked into the list of free WQEs. In addition, set the
  149. * scatter list L_Keys to the sentry value of 0x100.
  150. */
  151. for (i = 0; i < srq->max; ++i) {
  152. wqe = get_wqe(srq, i);
  153. *wqe_to_link(wqe) = i < srq->max - 1 ? i + 1 : -1;
  154. for (scatter = wqe + sizeof (struct mthca_next_seg);
  155. (void *) scatter < wqe + (1 << srq->wqe_shift);
  156. ++scatter)
  157. scatter->lkey = cpu_to_be32(MTHCA_INVAL_LKEY);
  158. }
  159. srq->last = get_wqe(srq, srq->max - 1);
  160. return 0;
  161. }
  162. int mthca_alloc_srq(struct mthca_dev *dev, struct mthca_pd *pd,
  163. struct ib_srq_attr *attr, struct mthca_srq *srq)
  164. {
  165. struct mthca_mailbox *mailbox;
  166. u8 status;
  167. int ds;
  168. int err;
  169. /* Sanity check SRQ size before proceeding */
  170. if (attr->max_wr > dev->limits.max_srq_wqes ||
  171. attr->max_sge > dev->limits.max_srq_sge)
  172. return -EINVAL;
  173. srq->max = attr->max_wr;
  174. srq->max_gs = attr->max_sge;
  175. srq->counter = 0;
  176. if (mthca_is_memfree(dev))
  177. srq->max = roundup_pow_of_two(srq->max + 1);
  178. else
  179. srq->max = srq->max + 1;
  180. ds = max(64UL,
  181. roundup_pow_of_two(sizeof (struct mthca_next_seg) +
  182. srq->max_gs * sizeof (struct mthca_data_seg)));
  183. if (!mthca_is_memfree(dev) && (ds > dev->limits.max_desc_sz))
  184. return -EINVAL;
  185. srq->wqe_shift = long_log2(ds);
  186. srq->srqn = mthca_alloc(&dev->srq_table.alloc);
  187. if (srq->srqn == -1)
  188. return -ENOMEM;
  189. if (mthca_is_memfree(dev)) {
  190. err = mthca_table_get(dev, dev->srq_table.table, srq->srqn);
  191. if (err)
  192. goto err_out;
  193. if (!pd->ibpd.uobject) {
  194. srq->db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_SRQ,
  195. srq->srqn, &srq->db);
  196. if (srq->db_index < 0) {
  197. err = -ENOMEM;
  198. goto err_out_icm;
  199. }
  200. }
  201. }
  202. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  203. if (IS_ERR(mailbox)) {
  204. err = PTR_ERR(mailbox);
  205. goto err_out_db;
  206. }
  207. err = mthca_alloc_srq_buf(dev, pd, srq);
  208. if (err)
  209. goto err_out_mailbox;
  210. spin_lock_init(&srq->lock);
  211. srq->refcount = 1;
  212. init_waitqueue_head(&srq->wait);
  213. mutex_init(&srq->mutex);
  214. if (mthca_is_memfree(dev))
  215. mthca_arbel_init_srq_context(dev, pd, srq, mailbox->buf);
  216. else
  217. mthca_tavor_init_srq_context(dev, pd, srq, mailbox->buf);
  218. err = mthca_SW2HW_SRQ(dev, mailbox, srq->srqn, &status);
  219. if (err) {
  220. mthca_warn(dev, "SW2HW_SRQ failed (%d)\n", err);
  221. goto err_out_free_buf;
  222. }
  223. if (status) {
  224. mthca_warn(dev, "SW2HW_SRQ returned status 0x%02x\n",
  225. status);
  226. err = -EINVAL;
  227. goto err_out_free_buf;
  228. }
  229. spin_lock_irq(&dev->srq_table.lock);
  230. if (mthca_array_set(&dev->srq_table.srq,
  231. srq->srqn & (dev->limits.num_srqs - 1),
  232. srq)) {
  233. spin_unlock_irq(&dev->srq_table.lock);
  234. goto err_out_free_srq;
  235. }
  236. spin_unlock_irq(&dev->srq_table.lock);
  237. mthca_free_mailbox(dev, mailbox);
  238. srq->first_free = 0;
  239. srq->last_free = srq->max - 1;
  240. attr->max_wr = srq->max - 1;
  241. attr->max_sge = srq->max_gs;
  242. return 0;
  243. err_out_free_srq:
  244. err = mthca_HW2SW_SRQ(dev, mailbox, srq->srqn, &status);
  245. if (err)
  246. mthca_warn(dev, "HW2SW_SRQ failed (%d)\n", err);
  247. else if (status)
  248. mthca_warn(dev, "HW2SW_SRQ returned status 0x%02x\n", status);
  249. err_out_free_buf:
  250. if (!pd->ibpd.uobject)
  251. mthca_free_srq_buf(dev, srq);
  252. err_out_mailbox:
  253. mthca_free_mailbox(dev, mailbox);
  254. err_out_db:
  255. if (!pd->ibpd.uobject && mthca_is_memfree(dev))
  256. mthca_free_db(dev, MTHCA_DB_TYPE_SRQ, srq->db_index);
  257. err_out_icm:
  258. mthca_table_put(dev, dev->srq_table.table, srq->srqn);
  259. err_out:
  260. mthca_free(&dev->srq_table.alloc, srq->srqn);
  261. return err;
  262. }
  263. static inline int get_srq_refcount(struct mthca_dev *dev, struct mthca_srq *srq)
  264. {
  265. int c;
  266. spin_lock_irq(&dev->srq_table.lock);
  267. c = srq->refcount;
  268. spin_unlock_irq(&dev->srq_table.lock);
  269. return c;
  270. }
  271. void mthca_free_srq(struct mthca_dev *dev, struct mthca_srq *srq)
  272. {
  273. struct mthca_mailbox *mailbox;
  274. int err;
  275. u8 status;
  276. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  277. if (IS_ERR(mailbox)) {
  278. mthca_warn(dev, "No memory for mailbox to free SRQ.\n");
  279. return;
  280. }
  281. err = mthca_HW2SW_SRQ(dev, mailbox, srq->srqn, &status);
  282. if (err)
  283. mthca_warn(dev, "HW2SW_SRQ failed (%d)\n", err);
  284. else if (status)
  285. mthca_warn(dev, "HW2SW_SRQ returned status 0x%02x\n", status);
  286. spin_lock_irq(&dev->srq_table.lock);
  287. mthca_array_clear(&dev->srq_table.srq,
  288. srq->srqn & (dev->limits.num_srqs - 1));
  289. --srq->refcount;
  290. spin_unlock_irq(&dev->srq_table.lock);
  291. wait_event(srq->wait, !get_srq_refcount(dev, srq));
  292. if (!srq->ibsrq.uobject) {
  293. mthca_free_srq_buf(dev, srq);
  294. if (mthca_is_memfree(dev))
  295. mthca_free_db(dev, MTHCA_DB_TYPE_SRQ, srq->db_index);
  296. }
  297. mthca_table_put(dev, dev->srq_table.table, srq->srqn);
  298. mthca_free(&dev->srq_table.alloc, srq->srqn);
  299. mthca_free_mailbox(dev, mailbox);
  300. }
  301. int mthca_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  302. enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
  303. {
  304. struct mthca_dev *dev = to_mdev(ibsrq->device);
  305. struct mthca_srq *srq = to_msrq(ibsrq);
  306. int ret;
  307. u8 status;
  308. /* We don't support resizing SRQs (yet?) */
  309. if (attr_mask & IB_SRQ_MAX_WR)
  310. return -EINVAL;
  311. if (attr_mask & IB_SRQ_LIMIT) {
  312. u32 max_wr = mthca_is_memfree(dev) ? srq->max - 1 : srq->max;
  313. if (attr->srq_limit > max_wr)
  314. return -EINVAL;
  315. mutex_lock(&srq->mutex);
  316. ret = mthca_ARM_SRQ(dev, srq->srqn, attr->srq_limit, &status);
  317. mutex_unlock(&srq->mutex);
  318. if (ret)
  319. return ret;
  320. if (status)
  321. return -EINVAL;
  322. }
  323. return 0;
  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. u8 status;
  333. int err;
  334. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  335. if (IS_ERR(mailbox))
  336. return PTR_ERR(mailbox);
  337. err = mthca_QUERY_SRQ(dev, srq->srqn, mailbox, &status);
  338. if (err)
  339. goto out;
  340. if (mthca_is_memfree(dev)) {
  341. arbel_ctx = mailbox->buf;
  342. srq_attr->srq_limit = be16_to_cpu(arbel_ctx->limit_watermark);
  343. } else {
  344. tavor_ctx = mailbox->buf;
  345. srq_attr->srq_limit = be16_to_cpu(tavor_ctx->limit_watermark);
  346. }
  347. srq_attr->max_wr = srq->max - 1;
  348. srq_attr->max_sge = srq->max_gs;
  349. out:
  350. mthca_free_mailbox(dev, mailbox);
  351. return err;
  352. }
  353. void mthca_srq_event(struct mthca_dev *dev, u32 srqn,
  354. enum ib_event_type event_type)
  355. {
  356. struct mthca_srq *srq;
  357. struct ib_event event;
  358. spin_lock(&dev->srq_table.lock);
  359. srq = mthca_array_get(&dev->srq_table.srq, srqn & (dev->limits.num_srqs - 1));
  360. if (srq)
  361. ++srq->refcount;
  362. spin_unlock(&dev->srq_table.lock);
  363. if (!srq) {
  364. mthca_warn(dev, "Async event for bogus SRQ %08x\n", srqn);
  365. return;
  366. }
  367. if (!srq->ibsrq.event_handler)
  368. goto out;
  369. event.device = &dev->ib_dev;
  370. event.event = event_type;
  371. event.element.srq = &srq->ibsrq;
  372. srq->ibsrq.event_handler(&event, srq->ibsrq.srq_context);
  373. out:
  374. spin_lock(&dev->srq_table.lock);
  375. if (!--srq->refcount)
  376. wake_up(&srq->wait);
  377. spin_unlock(&dev->srq_table.lock);
  378. }
  379. /*
  380. * This function must be called with IRQs disabled.
  381. */
  382. void mthca_free_srq_wqe(struct mthca_srq *srq, u32 wqe_addr)
  383. {
  384. int ind;
  385. ind = wqe_addr >> srq->wqe_shift;
  386. spin_lock(&srq->lock);
  387. if (likely(srq->first_free >= 0))
  388. *wqe_to_link(get_wqe(srq, srq->last_free)) = ind;
  389. else
  390. srq->first_free = ind;
  391. *wqe_to_link(get_wqe(srq, ind)) = -1;
  392. srq->last_free = ind;
  393. spin_unlock(&srq->lock);
  394. }
  395. int mthca_tavor_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  396. struct ib_recv_wr **bad_wr)
  397. {
  398. struct mthca_dev *dev = to_mdev(ibsrq->device);
  399. struct mthca_srq *srq = to_msrq(ibsrq);
  400. __be32 doorbell[2];
  401. unsigned long flags;
  402. int err = 0;
  403. int first_ind;
  404. int ind;
  405. int next_ind;
  406. int nreq;
  407. int i;
  408. void *wqe;
  409. void *prev_wqe;
  410. spin_lock_irqsave(&srq->lock, flags);
  411. first_ind = srq->first_free;
  412. for (nreq = 0; wr; wr = wr->next) {
  413. ind = srq->first_free;
  414. if (ind < 0) {
  415. mthca_err(dev, "SRQ %06x full\n", srq->srqn);
  416. err = -ENOMEM;
  417. *bad_wr = wr;
  418. break;
  419. }
  420. wqe = get_wqe(srq, ind);
  421. next_ind = *wqe_to_link(wqe);
  422. if (next_ind < 0) {
  423. mthca_err(dev, "SRQ %06x full\n", srq->srqn);
  424. err = -ENOMEM;
  425. *bad_wr = wr;
  426. break;
  427. }
  428. prev_wqe = srq->last;
  429. srq->last = wqe;
  430. ((struct mthca_next_seg *) wqe)->nda_op = 0;
  431. ((struct mthca_next_seg *) wqe)->ee_nds = 0;
  432. /* flags field will always remain 0 */
  433. wqe += sizeof (struct mthca_next_seg);
  434. if (unlikely(wr->num_sge > srq->max_gs)) {
  435. err = -EINVAL;
  436. *bad_wr = wr;
  437. srq->last = prev_wqe;
  438. break;
  439. }
  440. for (i = 0; i < wr->num_sge; ++i) {
  441. ((struct mthca_data_seg *) wqe)->byte_count =
  442. cpu_to_be32(wr->sg_list[i].length);
  443. ((struct mthca_data_seg *) wqe)->lkey =
  444. cpu_to_be32(wr->sg_list[i].lkey);
  445. ((struct mthca_data_seg *) wqe)->addr =
  446. cpu_to_be64(wr->sg_list[i].addr);
  447. wqe += sizeof (struct mthca_data_seg);
  448. }
  449. if (i < srq->max_gs) {
  450. ((struct mthca_data_seg *) wqe)->byte_count = 0;
  451. ((struct mthca_data_seg *) wqe)->lkey = cpu_to_be32(MTHCA_INVAL_LKEY);
  452. ((struct mthca_data_seg *) wqe)->addr = 0;
  453. }
  454. ((struct mthca_next_seg *) prev_wqe)->nda_op =
  455. cpu_to_be32((ind << srq->wqe_shift) | 1);
  456. wmb();
  457. ((struct mthca_next_seg *) prev_wqe)->ee_nds =
  458. cpu_to_be32(MTHCA_NEXT_DBD);
  459. srq->wrid[ind] = wr->wr_id;
  460. srq->first_free = next_ind;
  461. ++nreq;
  462. if (unlikely(nreq == MTHCA_TAVOR_MAX_WQES_PER_RECV_DB)) {
  463. nreq = 0;
  464. doorbell[0] = cpu_to_be32(first_ind << srq->wqe_shift);
  465. doorbell[1] = cpu_to_be32(srq->srqn << 8);
  466. /*
  467. * Make sure that descriptors are written
  468. * before doorbell is rung.
  469. */
  470. wmb();
  471. mthca_write64(doorbell,
  472. dev->kar + MTHCA_RECEIVE_DOORBELL,
  473. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  474. first_ind = srq->first_free;
  475. }
  476. }
  477. if (likely(nreq)) {
  478. doorbell[0] = cpu_to_be32(first_ind << srq->wqe_shift);
  479. doorbell[1] = cpu_to_be32((srq->srqn << 8) | nreq);
  480. /*
  481. * Make sure that descriptors are written before
  482. * doorbell is rung.
  483. */
  484. wmb();
  485. mthca_write64(doorbell,
  486. dev->kar + MTHCA_RECEIVE_DOORBELL,
  487. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  488. }
  489. /*
  490. * Make sure doorbells don't leak out of SRQ spinlock and
  491. * reach the HCA out of order:
  492. */
  493. mmiowb();
  494. spin_unlock_irqrestore(&srq->lock, flags);
  495. return err;
  496. }
  497. int mthca_arbel_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  498. struct ib_recv_wr **bad_wr)
  499. {
  500. struct mthca_dev *dev = to_mdev(ibsrq->device);
  501. struct mthca_srq *srq = to_msrq(ibsrq);
  502. unsigned long flags;
  503. int err = 0;
  504. int ind;
  505. int next_ind;
  506. int nreq;
  507. int i;
  508. void *wqe;
  509. spin_lock_irqsave(&srq->lock, flags);
  510. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  511. ind = srq->first_free;
  512. if (ind < 0) {
  513. mthca_err(dev, "SRQ %06x full\n", srq->srqn);
  514. err = -ENOMEM;
  515. *bad_wr = wr;
  516. break;
  517. }
  518. wqe = get_wqe(srq, ind);
  519. next_ind = *wqe_to_link(wqe);
  520. if (next_ind < 0) {
  521. mthca_err(dev, "SRQ %06x full\n", srq->srqn);
  522. err = -ENOMEM;
  523. *bad_wr = wr;
  524. break;
  525. }
  526. ((struct mthca_next_seg *) wqe)->nda_op =
  527. cpu_to_be32((next_ind << srq->wqe_shift) | 1);
  528. ((struct mthca_next_seg *) wqe)->ee_nds = 0;
  529. /* flags field will always remain 0 */
  530. wqe += sizeof (struct mthca_next_seg);
  531. if (unlikely(wr->num_sge > srq->max_gs)) {
  532. err = -EINVAL;
  533. *bad_wr = wr;
  534. break;
  535. }
  536. for (i = 0; i < wr->num_sge; ++i) {
  537. ((struct mthca_data_seg *) wqe)->byte_count =
  538. cpu_to_be32(wr->sg_list[i].length);
  539. ((struct mthca_data_seg *) wqe)->lkey =
  540. cpu_to_be32(wr->sg_list[i].lkey);
  541. ((struct mthca_data_seg *) wqe)->addr =
  542. cpu_to_be64(wr->sg_list[i].addr);
  543. wqe += sizeof (struct mthca_data_seg);
  544. }
  545. if (i < srq->max_gs) {
  546. ((struct mthca_data_seg *) wqe)->byte_count = 0;
  547. ((struct mthca_data_seg *) wqe)->lkey = cpu_to_be32(MTHCA_INVAL_LKEY);
  548. ((struct mthca_data_seg *) wqe)->addr = 0;
  549. }
  550. srq->wrid[ind] = wr->wr_id;
  551. srq->first_free = next_ind;
  552. }
  553. if (likely(nreq)) {
  554. srq->counter += nreq;
  555. /*
  556. * Make sure that descriptors are written before
  557. * we write doorbell record.
  558. */
  559. wmb();
  560. *srq->db = cpu_to_be32(srq->counter);
  561. }
  562. spin_unlock_irqrestore(&srq->lock, flags);
  563. return err;
  564. }
  565. int mthca_max_srq_sge(struct mthca_dev *dev)
  566. {
  567. if (mthca_is_memfree(dev))
  568. return dev->limits.max_sg;
  569. /*
  570. * SRQ allocations are based on powers of 2 for Tavor,
  571. * (although they only need to be multiples of 16 bytes).
  572. *
  573. * Therefore, we need to base the max number of sg entries on
  574. * the largest power of 2 descriptor size that is <= to the
  575. * actual max WQE descriptor size, rather than return the
  576. * max_sg value given by the firmware (which is based on WQE
  577. * sizes as multiples of 16, not powers of 2).
  578. *
  579. * If SRQ implementation is changed for Tavor to be based on
  580. * multiples of 16, the calculation below can be deleted and
  581. * the FW max_sg value returned.
  582. */
  583. return min_t(int, dev->limits.max_sg,
  584. ((1 << (fls(dev->limits.max_desc_sz) - 1)) -
  585. sizeof (struct mthca_next_seg)) /
  586. sizeof (struct mthca_data_seg));
  587. }
  588. int __devinit mthca_init_srq_table(struct mthca_dev *dev)
  589. {
  590. int err;
  591. if (!(dev->mthca_flags & MTHCA_FLAG_SRQ))
  592. return 0;
  593. spin_lock_init(&dev->srq_table.lock);
  594. err = mthca_alloc_init(&dev->srq_table.alloc,
  595. dev->limits.num_srqs,
  596. dev->limits.num_srqs - 1,
  597. dev->limits.reserved_srqs);
  598. if (err)
  599. return err;
  600. err = mthca_array_init(&dev->srq_table.srq,
  601. dev->limits.num_srqs);
  602. if (err)
  603. mthca_alloc_cleanup(&dev->srq_table.alloc);
  604. return err;
  605. }
  606. void mthca_cleanup_srq_table(struct mthca_dev *dev)
  607. {
  608. if (!(dev->mthca_flags & MTHCA_FLAG_SRQ))
  609. return;
  610. mthca_array_cleanup(&dev->srq_table.srq, dev->limits.num_srqs);
  611. mthca_alloc_cleanup(&dev->srq_table.alloc);
  612. }