cq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Copyright(c) 2016 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 <linux/kthread.h>
  50. #include "cq.h"
  51. #include "vt.h"
  52. #include "trace.h"
  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. spin_lock(&cq->rdi->n_cqs_lock);
  121. if (likely(cq->rdi->worker)) {
  122. cq->notify = RVT_CQ_NONE;
  123. cq->triggered++;
  124. kthread_queue_work(cq->rdi->worker, &cq->comptask);
  125. }
  126. spin_unlock(&cq->rdi->n_cqs_lock);
  127. }
  128. spin_unlock_irqrestore(&cq->lock, flags);
  129. }
  130. EXPORT_SYMBOL(rvt_cq_enter);
  131. static void send_complete(struct kthread_work *work)
  132. {
  133. struct rvt_cq *cq = container_of(work, struct rvt_cq, comptask);
  134. /*
  135. * The completion handler will most likely rearm the notification
  136. * and poll for all pending entries. If a new completion entry
  137. * is added while we are in this routine, queue_work()
  138. * won't call us again until we return so we check triggered to
  139. * see if we need to call the handler again.
  140. */
  141. for (;;) {
  142. u8 triggered = cq->triggered;
  143. /*
  144. * IPoIB connected mode assumes the callback is from a
  145. * soft IRQ. We simulate this by blocking "bottom halves".
  146. * See the implementation for ipoib_cm_handle_tx_wc(),
  147. * netif_tx_lock_bh() and netif_tx_lock().
  148. */
  149. local_bh_disable();
  150. cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
  151. local_bh_enable();
  152. if (cq->triggered == triggered)
  153. return;
  154. }
  155. }
  156. /**
  157. * rvt_create_cq - create a completion queue
  158. * @ibdev: the device this completion queue is attached to
  159. * @attr: creation attributes
  160. * @context: unused by the QLogic_IB driver
  161. * @udata: user data for libibverbs.so
  162. *
  163. * Called by ib_create_cq() in the generic verbs code.
  164. *
  165. * Return: pointer to the completion queue or negative errno values
  166. * for failure.
  167. */
  168. struct ib_cq *rvt_create_cq(struct ib_device *ibdev,
  169. const struct ib_cq_init_attr *attr,
  170. struct ib_ucontext *context,
  171. struct ib_udata *udata)
  172. {
  173. struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
  174. struct rvt_cq *cq;
  175. struct rvt_cq_wc *wc;
  176. struct ib_cq *ret;
  177. u32 sz;
  178. unsigned int entries = attr->cqe;
  179. if (attr->flags)
  180. return ERR_PTR(-EINVAL);
  181. if (entries < 1 || entries > rdi->dparms.props.max_cqe)
  182. return ERR_PTR(-EINVAL);
  183. /* Allocate the completion queue structure. */
  184. cq = kzalloc_node(sizeof(*cq), GFP_KERNEL, rdi->dparms.node);
  185. if (!cq)
  186. return ERR_PTR(-ENOMEM);
  187. /*
  188. * Allocate the completion queue entries and head/tail pointers.
  189. * This is allocated separately so that it can be resized and
  190. * also mapped into user space.
  191. * We need to use vmalloc() in order to support mmap and large
  192. * numbers of entries.
  193. */
  194. sz = sizeof(*wc);
  195. if (udata && udata->outlen >= sizeof(__u64))
  196. sz += sizeof(struct ib_uverbs_wc) * (entries + 1);
  197. else
  198. sz += sizeof(struct ib_wc) * (entries + 1);
  199. wc = udata ?
  200. vmalloc_user(sz) :
  201. vzalloc_node(sz, rdi->dparms.node);
  202. if (!wc) {
  203. ret = ERR_PTR(-ENOMEM);
  204. goto bail_cq;
  205. }
  206. /*
  207. * Return the address of the WC as the offset to mmap.
  208. * See rvt_mmap() for details.
  209. */
  210. if (udata && udata->outlen >= sizeof(__u64)) {
  211. int err;
  212. cq->ip = rvt_create_mmap_info(rdi, sz, context, wc);
  213. if (!cq->ip) {
  214. ret = ERR_PTR(-ENOMEM);
  215. goto bail_wc;
  216. }
  217. err = ib_copy_to_udata(udata, &cq->ip->offset,
  218. sizeof(cq->ip->offset));
  219. if (err) {
  220. ret = ERR_PTR(err);
  221. goto bail_ip;
  222. }
  223. }
  224. spin_lock_irq(&rdi->n_cqs_lock);
  225. if (rdi->n_cqs_allocated == rdi->dparms.props.max_cq) {
  226. spin_unlock_irq(&rdi->n_cqs_lock);
  227. ret = ERR_PTR(-ENOMEM);
  228. goto bail_ip;
  229. }
  230. rdi->n_cqs_allocated++;
  231. spin_unlock_irq(&rdi->n_cqs_lock);
  232. if (cq->ip) {
  233. spin_lock_irq(&rdi->pending_lock);
  234. list_add(&cq->ip->pending_mmaps, &rdi->pending_mmaps);
  235. spin_unlock_irq(&rdi->pending_lock);
  236. }
  237. /*
  238. * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
  239. * The number of entries should be >= the number requested or return
  240. * an error.
  241. */
  242. cq->rdi = rdi;
  243. cq->ibcq.cqe = entries;
  244. cq->notify = RVT_CQ_NONE;
  245. spin_lock_init(&cq->lock);
  246. kthread_init_work(&cq->comptask, send_complete);
  247. cq->queue = wc;
  248. ret = &cq->ibcq;
  249. goto done;
  250. bail_ip:
  251. kfree(cq->ip);
  252. bail_wc:
  253. vfree(wc);
  254. bail_cq:
  255. kfree(cq);
  256. done:
  257. return ret;
  258. }
  259. /**
  260. * rvt_destroy_cq - destroy a completion queue
  261. * @ibcq: the completion queue to destroy.
  262. *
  263. * Called by ib_destroy_cq() in the generic verbs code.
  264. *
  265. * Return: always 0
  266. */
  267. int rvt_destroy_cq(struct ib_cq *ibcq)
  268. {
  269. struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
  270. struct rvt_dev_info *rdi = cq->rdi;
  271. kthread_flush_work(&cq->comptask);
  272. spin_lock_irq(&rdi->n_cqs_lock);
  273. rdi->n_cqs_allocated--;
  274. spin_unlock_irq(&rdi->n_cqs_lock);
  275. if (cq->ip)
  276. kref_put(&cq->ip->ref, rvt_release_mmap_info);
  277. else
  278. vfree(cq->queue);
  279. kfree(cq);
  280. return 0;
  281. }
  282. /**
  283. * rvt_req_notify_cq - change the notification type for a completion queue
  284. * @ibcq: the completion queue
  285. * @notify_flags: the type of notification to request
  286. *
  287. * This may be called from interrupt context. Also called by
  288. * ib_req_notify_cq() in the generic verbs code.
  289. *
  290. * Return: 0 for success.
  291. */
  292. int rvt_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
  293. {
  294. struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
  295. unsigned long flags;
  296. int ret = 0;
  297. spin_lock_irqsave(&cq->lock, flags);
  298. /*
  299. * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
  300. * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
  301. */
  302. if (cq->notify != IB_CQ_NEXT_COMP)
  303. cq->notify = notify_flags & IB_CQ_SOLICITED_MASK;
  304. if ((notify_flags & IB_CQ_REPORT_MISSED_EVENTS) &&
  305. cq->queue->head != cq->queue->tail)
  306. ret = 1;
  307. spin_unlock_irqrestore(&cq->lock, flags);
  308. return ret;
  309. }
  310. /**
  311. * rvt_resize_cq - change the size of the CQ
  312. * @ibcq: the completion queue
  313. *
  314. * Return: 0 for success.
  315. */
  316. int rvt_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
  317. {
  318. struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
  319. struct rvt_cq_wc *old_wc;
  320. struct rvt_cq_wc *wc;
  321. u32 head, tail, n;
  322. int ret;
  323. u32 sz;
  324. struct rvt_dev_info *rdi = cq->rdi;
  325. if (cqe < 1 || cqe > rdi->dparms.props.max_cqe)
  326. return -EINVAL;
  327. /*
  328. * Need to use vmalloc() if we want to support large #s of entries.
  329. */
  330. sz = sizeof(*wc);
  331. if (udata && udata->outlen >= sizeof(__u64))
  332. sz += sizeof(struct ib_uverbs_wc) * (cqe + 1);
  333. else
  334. sz += sizeof(struct ib_wc) * (cqe + 1);
  335. wc = udata ?
  336. vmalloc_user(sz) :
  337. vzalloc_node(sz, rdi->dparms.node);
  338. if (!wc)
  339. return -ENOMEM;
  340. /* Check that we can write the offset to mmap. */
  341. if (udata && udata->outlen >= sizeof(__u64)) {
  342. __u64 offset = 0;
  343. ret = ib_copy_to_udata(udata, &offset, sizeof(offset));
  344. if (ret)
  345. goto bail_free;
  346. }
  347. spin_lock_irq(&cq->lock);
  348. /*
  349. * Make sure head and tail are sane since they
  350. * might be user writable.
  351. */
  352. old_wc = cq->queue;
  353. head = old_wc->head;
  354. if (head > (u32)cq->ibcq.cqe)
  355. head = (u32)cq->ibcq.cqe;
  356. tail = old_wc->tail;
  357. if (tail > (u32)cq->ibcq.cqe)
  358. tail = (u32)cq->ibcq.cqe;
  359. if (head < tail)
  360. n = cq->ibcq.cqe + 1 + head - tail;
  361. else
  362. n = head - tail;
  363. if (unlikely((u32)cqe < n)) {
  364. ret = -EINVAL;
  365. goto bail_unlock;
  366. }
  367. for (n = 0; tail != head; n++) {
  368. if (cq->ip)
  369. wc->uqueue[n] = old_wc->uqueue[tail];
  370. else
  371. wc->kqueue[n] = old_wc->kqueue[tail];
  372. if (tail == (u32)cq->ibcq.cqe)
  373. tail = 0;
  374. else
  375. tail++;
  376. }
  377. cq->ibcq.cqe = cqe;
  378. wc->head = n;
  379. wc->tail = 0;
  380. cq->queue = wc;
  381. spin_unlock_irq(&cq->lock);
  382. vfree(old_wc);
  383. if (cq->ip) {
  384. struct rvt_mmap_info *ip = cq->ip;
  385. rvt_update_mmap_info(rdi, ip, sz, wc);
  386. /*
  387. * Return the offset to mmap.
  388. * See rvt_mmap() for details.
  389. */
  390. if (udata && udata->outlen >= sizeof(__u64)) {
  391. ret = ib_copy_to_udata(udata, &ip->offset,
  392. sizeof(ip->offset));
  393. if (ret)
  394. return ret;
  395. }
  396. spin_lock_irq(&rdi->pending_lock);
  397. if (list_empty(&ip->pending_mmaps))
  398. list_add(&ip->pending_mmaps, &rdi->pending_mmaps);
  399. spin_unlock_irq(&rdi->pending_lock);
  400. }
  401. return 0;
  402. bail_unlock:
  403. spin_unlock_irq(&cq->lock);
  404. bail_free:
  405. vfree(wc);
  406. return ret;
  407. }
  408. /**
  409. * rvt_poll_cq - poll for work completion entries
  410. * @ibcq: the completion queue to poll
  411. * @num_entries: the maximum number of entries to return
  412. * @entry: pointer to array where work completions are placed
  413. *
  414. * This may be called from interrupt context. Also called by ib_poll_cq()
  415. * in the generic verbs code.
  416. *
  417. * Return: the number of completion entries polled.
  418. */
  419. int rvt_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
  420. {
  421. struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
  422. struct rvt_cq_wc *wc;
  423. unsigned long flags;
  424. int npolled;
  425. u32 tail;
  426. /* The kernel can only poll a kernel completion queue */
  427. if (cq->ip)
  428. return -EINVAL;
  429. spin_lock_irqsave(&cq->lock, flags);
  430. wc = cq->queue;
  431. tail = wc->tail;
  432. if (tail > (u32)cq->ibcq.cqe)
  433. tail = (u32)cq->ibcq.cqe;
  434. for (npolled = 0; npolled < num_entries; ++npolled, ++entry) {
  435. if (tail == wc->head)
  436. break;
  437. /* The kernel doesn't need a RMB since it has the lock. */
  438. trace_rvt_cq_poll(cq, &wc->kqueue[tail], npolled);
  439. *entry = wc->kqueue[tail];
  440. if (tail >= cq->ibcq.cqe)
  441. tail = 0;
  442. else
  443. tail++;
  444. }
  445. wc->tail = tail;
  446. spin_unlock_irqrestore(&cq->lock, flags);
  447. return npolled;
  448. }
  449. /**
  450. * rvt_driver_cq_init - Init cq resources on behalf of driver
  451. * @rdi: rvt dev structure
  452. *
  453. * Return: 0 on success
  454. */
  455. int rvt_driver_cq_init(struct rvt_dev_info *rdi)
  456. {
  457. int cpu;
  458. struct kthread_worker *worker;
  459. if (rdi->worker)
  460. return 0;
  461. spin_lock_init(&rdi->n_cqs_lock);
  462. cpu = cpumask_first(cpumask_of_node(rdi->dparms.node));
  463. worker = kthread_create_worker_on_cpu(cpu, 0,
  464. "%s", rdi->dparms.cq_name);
  465. if (IS_ERR(worker))
  466. return PTR_ERR(worker);
  467. set_user_nice(worker->task, MIN_NICE);
  468. rdi->worker = worker;
  469. return 0;
  470. }
  471. /**
  472. * rvt_cq_exit - tear down cq reources
  473. * @rdi: rvt dev structure
  474. */
  475. void rvt_cq_exit(struct rvt_dev_info *rdi)
  476. {
  477. struct kthread_worker *worker;
  478. /* block future queuing from send_complete() */
  479. spin_lock_irq(&rdi->n_cqs_lock);
  480. worker = rdi->worker;
  481. if (!worker) {
  482. spin_unlock_irq(&rdi->n_cqs_lock);
  483. return;
  484. }
  485. rdi->worker = NULL;
  486. spin_unlock_irq(&rdi->n_cqs_lock);
  487. kthread_destroy_worker(worker);
  488. }