cq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Copyright(c) 2016 - 2018 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/slab.h>
  48. #include <linux/vmalloc.h>
  49. #include "cq.h"
  50. #include "vt.h"
  51. #include "trace.h"
  52. static struct workqueue_struct *comp_vector_wq;
  53. /**
  54. * rvt_cq_enter - add a new entry to the completion queue
  55. * @cq: completion queue
  56. * @entry: work completion entry to add
  57. * @solicited: true if @entry is solicited
  58. *
  59. * This may be called with qp->s_lock held.
  60. */
  61. void rvt_cq_enter(struct rvt_cq *cq, struct ib_wc *entry, bool solicited)
  62. {
  63. struct rvt_cq_wc *wc;
  64. unsigned long flags;
  65. u32 head;
  66. u32 next;
  67. spin_lock_irqsave(&cq->lock, flags);
  68. /*
  69. * Note that the head pointer might be writable by user processes.
  70. * Take care to verify it is a sane value.
  71. */
  72. wc = cq->queue;
  73. head = wc->head;
  74. if (head >= (unsigned)cq->ibcq.cqe) {
  75. head = cq->ibcq.cqe;
  76. next = 0;
  77. } else {
  78. next = head + 1;
  79. }
  80. if (unlikely(next == wc->tail)) {
  81. spin_unlock_irqrestore(&cq->lock, flags);
  82. if (cq->ibcq.event_handler) {
  83. struct ib_event ev;
  84. ev.device = cq->ibcq.device;
  85. ev.element.cq = &cq->ibcq;
  86. ev.event = IB_EVENT_CQ_ERR;
  87. cq->ibcq.event_handler(&ev, cq->ibcq.cq_context);
  88. }
  89. return;
  90. }
  91. trace_rvt_cq_enter(cq, entry, head);
  92. if (cq->ip) {
  93. wc->uqueue[head].wr_id = entry->wr_id;
  94. wc->uqueue[head].status = entry->status;
  95. wc->uqueue[head].opcode = entry->opcode;
  96. wc->uqueue[head].vendor_err = entry->vendor_err;
  97. wc->uqueue[head].byte_len = entry->byte_len;
  98. wc->uqueue[head].ex.imm_data = entry->ex.imm_data;
  99. wc->uqueue[head].qp_num = entry->qp->qp_num;
  100. wc->uqueue[head].src_qp = entry->src_qp;
  101. wc->uqueue[head].wc_flags = entry->wc_flags;
  102. wc->uqueue[head].pkey_index = entry->pkey_index;
  103. wc->uqueue[head].slid = ib_lid_cpu16(entry->slid);
  104. wc->uqueue[head].sl = entry->sl;
  105. wc->uqueue[head].dlid_path_bits = entry->dlid_path_bits;
  106. wc->uqueue[head].port_num = entry->port_num;
  107. /* Make sure entry is written before the head index. */
  108. smp_wmb();
  109. } else {
  110. wc->kqueue[head] = *entry;
  111. }
  112. wc->head = next;
  113. if (cq->notify == IB_CQ_NEXT_COMP ||
  114. (cq->notify == IB_CQ_SOLICITED &&
  115. (solicited || entry->status != IB_WC_SUCCESS))) {
  116. /*
  117. * This will cause send_complete() to be called in
  118. * another thread.
  119. */
  120. cq->notify = RVT_CQ_NONE;
  121. cq->triggered++;
  122. queue_work_on(cq->comp_vector_cpu, comp_vector_wq,
  123. &cq->comptask);
  124. }
  125. spin_unlock_irqrestore(&cq->lock, flags);
  126. }
  127. EXPORT_SYMBOL(rvt_cq_enter);
  128. static void send_complete(struct work_struct *work)
  129. {
  130. struct rvt_cq *cq = container_of(work, struct rvt_cq, comptask);
  131. /*
  132. * The completion handler will most likely rearm the notification
  133. * and poll for all pending entries. If a new completion entry
  134. * is added while we are in this routine, queue_work()
  135. * won't call us again until we return so we check triggered to
  136. * see if we need to call the handler again.
  137. */
  138. for (;;) {
  139. u8 triggered = cq->triggered;
  140. /*
  141. * IPoIB connected mode assumes the callback is from a
  142. * soft IRQ. We simulate this by blocking "bottom halves".
  143. * See the implementation for ipoib_cm_handle_tx_wc(),
  144. * netif_tx_lock_bh() and netif_tx_lock().
  145. */
  146. local_bh_disable();
  147. cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
  148. local_bh_enable();
  149. if (cq->triggered == triggered)
  150. return;
  151. }
  152. }
  153. /**
  154. * rvt_create_cq - create a completion queue
  155. * @ibdev: the device this completion queue is attached to
  156. * @attr: creation attributes
  157. * @context: unused by the QLogic_IB driver
  158. * @udata: user data for libibverbs.so
  159. *
  160. * Called by ib_create_cq() in the generic verbs code.
  161. *
  162. * Return: pointer to the completion queue or negative errno values
  163. * for failure.
  164. */
  165. struct ib_cq *rvt_create_cq(struct ib_device *ibdev,
  166. const struct ib_cq_init_attr *attr,
  167. struct ib_ucontext *context,
  168. struct ib_udata *udata)
  169. {
  170. struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
  171. struct rvt_cq *cq;
  172. struct rvt_cq_wc *wc;
  173. struct ib_cq *ret;
  174. u32 sz;
  175. unsigned int entries = attr->cqe;
  176. int comp_vector = attr->comp_vector;
  177. if (attr->flags)
  178. return ERR_PTR(-EINVAL);
  179. if (entries < 1 || entries > rdi->dparms.props.max_cqe)
  180. return ERR_PTR(-EINVAL);
  181. if (comp_vector < 0)
  182. comp_vector = 0;
  183. comp_vector = comp_vector % rdi->ibdev.num_comp_vectors;
  184. /* Allocate the completion queue structure. */
  185. cq = kzalloc_node(sizeof(*cq), GFP_KERNEL, rdi->dparms.node);
  186. if (!cq)
  187. return ERR_PTR(-ENOMEM);
  188. /*
  189. * Allocate the completion queue entries and head/tail pointers.
  190. * This is allocated separately so that it can be resized and
  191. * also mapped into user space.
  192. * We need to use vmalloc() in order to support mmap and large
  193. * numbers of entries.
  194. */
  195. sz = sizeof(*wc);
  196. if (udata && udata->outlen >= sizeof(__u64))
  197. sz += sizeof(struct ib_uverbs_wc) * (entries + 1);
  198. else
  199. sz += sizeof(struct ib_wc) * (entries + 1);
  200. wc = udata ?
  201. vmalloc_user(sz) :
  202. vzalloc_node(sz, rdi->dparms.node);
  203. if (!wc) {
  204. ret = ERR_PTR(-ENOMEM);
  205. goto bail_cq;
  206. }
  207. /*
  208. * Return the address of the WC as the offset to mmap.
  209. * See rvt_mmap() for details.
  210. */
  211. if (udata && udata->outlen >= sizeof(__u64)) {
  212. int err;
  213. cq->ip = rvt_create_mmap_info(rdi, sz, context, wc);
  214. if (!cq->ip) {
  215. ret = ERR_PTR(-ENOMEM);
  216. goto bail_wc;
  217. }
  218. err = ib_copy_to_udata(udata, &cq->ip->offset,
  219. sizeof(cq->ip->offset));
  220. if (err) {
  221. ret = ERR_PTR(err);
  222. goto bail_ip;
  223. }
  224. }
  225. spin_lock_irq(&rdi->n_cqs_lock);
  226. if (rdi->n_cqs_allocated == rdi->dparms.props.max_cq) {
  227. spin_unlock_irq(&rdi->n_cqs_lock);
  228. ret = ERR_PTR(-ENOMEM);
  229. goto bail_ip;
  230. }
  231. rdi->n_cqs_allocated++;
  232. spin_unlock_irq(&rdi->n_cqs_lock);
  233. if (cq->ip) {
  234. spin_lock_irq(&rdi->pending_lock);
  235. list_add(&cq->ip->pending_mmaps, &rdi->pending_mmaps);
  236. spin_unlock_irq(&rdi->pending_lock);
  237. }
  238. /*
  239. * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
  240. * The number of entries should be >= the number requested or return
  241. * an error.
  242. */
  243. cq->rdi = rdi;
  244. if (rdi->driver_f.comp_vect_cpu_lookup)
  245. cq->comp_vector_cpu =
  246. rdi->driver_f.comp_vect_cpu_lookup(rdi, comp_vector);
  247. else
  248. cq->comp_vector_cpu =
  249. cpumask_first(cpumask_of_node(rdi->dparms.node));
  250. cq->ibcq.cqe = entries;
  251. cq->notify = RVT_CQ_NONE;
  252. spin_lock_init(&cq->lock);
  253. INIT_WORK(&cq->comptask, send_complete);
  254. cq->queue = wc;
  255. ret = &cq->ibcq;
  256. trace_rvt_create_cq(cq, attr);
  257. goto done;
  258. bail_ip:
  259. kfree(cq->ip);
  260. bail_wc:
  261. vfree(wc);
  262. bail_cq:
  263. kfree(cq);
  264. done:
  265. return ret;
  266. }
  267. /**
  268. * rvt_destroy_cq - destroy a completion queue
  269. * @ibcq: the completion queue to destroy.
  270. *
  271. * Called by ib_destroy_cq() in the generic verbs code.
  272. *
  273. * Return: always 0
  274. */
  275. int rvt_destroy_cq(struct ib_cq *ibcq)
  276. {
  277. struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
  278. struct rvt_dev_info *rdi = cq->rdi;
  279. flush_work(&cq->comptask);
  280. spin_lock_irq(&rdi->n_cqs_lock);
  281. rdi->n_cqs_allocated--;
  282. spin_unlock_irq(&rdi->n_cqs_lock);
  283. if (cq->ip)
  284. kref_put(&cq->ip->ref, rvt_release_mmap_info);
  285. else
  286. vfree(cq->queue);
  287. kfree(cq);
  288. return 0;
  289. }
  290. /**
  291. * rvt_req_notify_cq - change the notification type for a completion queue
  292. * @ibcq: the completion queue
  293. * @notify_flags: the type of notification to request
  294. *
  295. * This may be called from interrupt context. Also called by
  296. * ib_req_notify_cq() in the generic verbs code.
  297. *
  298. * Return: 0 for success.
  299. */
  300. int rvt_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
  301. {
  302. struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
  303. unsigned long flags;
  304. int ret = 0;
  305. spin_lock_irqsave(&cq->lock, flags);
  306. /*
  307. * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
  308. * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
  309. */
  310. if (cq->notify != IB_CQ_NEXT_COMP)
  311. cq->notify = notify_flags & IB_CQ_SOLICITED_MASK;
  312. if ((notify_flags & IB_CQ_REPORT_MISSED_EVENTS) &&
  313. cq->queue->head != cq->queue->tail)
  314. ret = 1;
  315. spin_unlock_irqrestore(&cq->lock, flags);
  316. return ret;
  317. }
  318. /**
  319. * rvt_resize_cq - change the size of the CQ
  320. * @ibcq: the completion queue
  321. *
  322. * Return: 0 for success.
  323. */
  324. int rvt_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
  325. {
  326. struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
  327. struct rvt_cq_wc *old_wc;
  328. struct rvt_cq_wc *wc;
  329. u32 head, tail, n;
  330. int ret;
  331. u32 sz;
  332. struct rvt_dev_info *rdi = cq->rdi;
  333. if (cqe < 1 || cqe > rdi->dparms.props.max_cqe)
  334. return -EINVAL;
  335. /*
  336. * Need to use vmalloc() if we want to support large #s of entries.
  337. */
  338. sz = sizeof(*wc);
  339. if (udata && udata->outlen >= sizeof(__u64))
  340. sz += sizeof(struct ib_uverbs_wc) * (cqe + 1);
  341. else
  342. sz += sizeof(struct ib_wc) * (cqe + 1);
  343. wc = udata ?
  344. vmalloc_user(sz) :
  345. vzalloc_node(sz, rdi->dparms.node);
  346. if (!wc)
  347. return -ENOMEM;
  348. /* Check that we can write the offset to mmap. */
  349. if (udata && udata->outlen >= sizeof(__u64)) {
  350. __u64 offset = 0;
  351. ret = ib_copy_to_udata(udata, &offset, sizeof(offset));
  352. if (ret)
  353. goto bail_free;
  354. }
  355. spin_lock_irq(&cq->lock);
  356. /*
  357. * Make sure head and tail are sane since they
  358. * might be user writable.
  359. */
  360. old_wc = cq->queue;
  361. head = old_wc->head;
  362. if (head > (u32)cq->ibcq.cqe)
  363. head = (u32)cq->ibcq.cqe;
  364. tail = old_wc->tail;
  365. if (tail > (u32)cq->ibcq.cqe)
  366. tail = (u32)cq->ibcq.cqe;
  367. if (head < tail)
  368. n = cq->ibcq.cqe + 1 + head - tail;
  369. else
  370. n = head - tail;
  371. if (unlikely((u32)cqe < n)) {
  372. ret = -EINVAL;
  373. goto bail_unlock;
  374. }
  375. for (n = 0; tail != head; n++) {
  376. if (cq->ip)
  377. wc->uqueue[n] = old_wc->uqueue[tail];
  378. else
  379. wc->kqueue[n] = old_wc->kqueue[tail];
  380. if (tail == (u32)cq->ibcq.cqe)
  381. tail = 0;
  382. else
  383. tail++;
  384. }
  385. cq->ibcq.cqe = cqe;
  386. wc->head = n;
  387. wc->tail = 0;
  388. cq->queue = wc;
  389. spin_unlock_irq(&cq->lock);
  390. vfree(old_wc);
  391. if (cq->ip) {
  392. struct rvt_mmap_info *ip = cq->ip;
  393. rvt_update_mmap_info(rdi, ip, sz, wc);
  394. /*
  395. * Return the offset to mmap.
  396. * See rvt_mmap() for details.
  397. */
  398. if (udata && udata->outlen >= sizeof(__u64)) {
  399. ret = ib_copy_to_udata(udata, &ip->offset,
  400. sizeof(ip->offset));
  401. if (ret)
  402. return ret;
  403. }
  404. spin_lock_irq(&rdi->pending_lock);
  405. if (list_empty(&ip->pending_mmaps))
  406. list_add(&ip->pending_mmaps, &rdi->pending_mmaps);
  407. spin_unlock_irq(&rdi->pending_lock);
  408. }
  409. return 0;
  410. bail_unlock:
  411. spin_unlock_irq(&cq->lock);
  412. bail_free:
  413. vfree(wc);
  414. return ret;
  415. }
  416. /**
  417. * rvt_poll_cq - poll for work completion entries
  418. * @ibcq: the completion queue to poll
  419. * @num_entries: the maximum number of entries to return
  420. * @entry: pointer to array where work completions are placed
  421. *
  422. * This may be called from interrupt context. Also called by ib_poll_cq()
  423. * in the generic verbs code.
  424. *
  425. * Return: the number of completion entries polled.
  426. */
  427. int rvt_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
  428. {
  429. struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
  430. struct rvt_cq_wc *wc;
  431. unsigned long flags;
  432. int npolled;
  433. u32 tail;
  434. /* The kernel can only poll a kernel completion queue */
  435. if (cq->ip)
  436. return -EINVAL;
  437. spin_lock_irqsave(&cq->lock, flags);
  438. wc = cq->queue;
  439. tail = wc->tail;
  440. if (tail > (u32)cq->ibcq.cqe)
  441. tail = (u32)cq->ibcq.cqe;
  442. for (npolled = 0; npolled < num_entries; ++npolled, ++entry) {
  443. if (tail == wc->head)
  444. break;
  445. /* The kernel doesn't need a RMB since it has the lock. */
  446. trace_rvt_cq_poll(cq, &wc->kqueue[tail], npolled);
  447. *entry = wc->kqueue[tail];
  448. if (tail >= cq->ibcq.cqe)
  449. tail = 0;
  450. else
  451. tail++;
  452. }
  453. wc->tail = tail;
  454. spin_unlock_irqrestore(&cq->lock, flags);
  455. return npolled;
  456. }
  457. /**
  458. * rvt_driver_cq_init - Init cq resources on behalf of driver
  459. * @rdi: rvt dev structure
  460. *
  461. * Return: 0 on success
  462. */
  463. int rvt_driver_cq_init(void)
  464. {
  465. comp_vector_wq = alloc_workqueue("%s", WQ_HIGHPRI | WQ_CPU_INTENSIVE,
  466. 0, "rdmavt_cq");
  467. if (!comp_vector_wq)
  468. return -ENOMEM;
  469. return 0;
  470. }
  471. /**
  472. * rvt_cq_exit - tear down cq reources
  473. * @rdi: rvt dev structure
  474. */
  475. void rvt_cq_exit(void)
  476. {
  477. destroy_workqueue(comp_vector_wq);
  478. comp_vector_wq = NULL;
  479. }