cq.c 14 KB

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