mthca_srq.c 18 KB

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