mthca_srq.c 15 KB

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