cq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. /**
  53. * rvt_cq_enter - add a new entry to the completion queue
  54. * @cq: completion queue
  55. * @entry: work completion entry to add
  56. * @sig: true if @entry is solicited
  57. *
  58. * This may be called with qp->s_lock held.
  59. */
  60. void rvt_cq_enter(struct rvt_cq *cq, struct ib_wc *entry, bool solicited)
  61. {
  62. struct rvt_cq_wc *wc;
  63. unsigned long flags;
  64. u32 head;
  65. u32 next;
  66. spin_lock_irqsave(&cq->lock, flags);
  67. /*
  68. * Note that the head pointer might be writable by user processes.
  69. * Take care to verify it is a sane value.
  70. */
  71. wc = cq->queue;
  72. head = wc->head;
  73. if (head >= (unsigned)cq->ibcq.cqe) {
  74. head = cq->ibcq.cqe;
  75. next = 0;
  76. } else {
  77. next = head + 1;
  78. }
  79. if (unlikely(next == wc->tail)) {
  80. spin_unlock_irqrestore(&cq->lock, flags);
  81. if (cq->ibcq.event_handler) {
  82. struct ib_event ev;
  83. ev.device = cq->ibcq.device;
  84. ev.element.cq = &cq->ibcq;
  85. ev.event = IB_EVENT_CQ_ERR;
  86. cq->ibcq.event_handler(&ev, cq->ibcq.cq_context);
  87. }
  88. return;
  89. }
  90. if (cq->ip) {
  91. wc->uqueue[head].wr_id = entry->wr_id;
  92. wc->uqueue[head].status = entry->status;
  93. wc->uqueue[head].opcode = entry->opcode;
  94. wc->uqueue[head].vendor_err = entry->vendor_err;
  95. wc->uqueue[head].byte_len = entry->byte_len;
  96. wc->uqueue[head].ex.imm_data =
  97. (__u32 __force)entry->ex.imm_data;
  98. wc->uqueue[head].qp_num = entry->qp->qp_num;
  99. wc->uqueue[head].src_qp = entry->src_qp;
  100. wc->uqueue[head].wc_flags = entry->wc_flags;
  101. wc->uqueue[head].pkey_index = entry->pkey_index;
  102. wc->uqueue[head].slid = entry->slid;
  103. wc->uqueue[head].sl = entry->sl;
  104. wc->uqueue[head].dlid_path_bits = entry->dlid_path_bits;
  105. wc->uqueue[head].port_num = entry->port_num;
  106. /* Make sure entry is written before the head index. */
  107. smp_wmb();
  108. } else {
  109. wc->kqueue[head] = *entry;
  110. }
  111. wc->head = next;
  112. if (cq->notify == IB_CQ_NEXT_COMP ||
  113. (cq->notify == IB_CQ_SOLICITED &&
  114. (solicited || entry->status != IB_WC_SUCCESS))) {
  115. struct kthread_worker *worker;
  116. /*
  117. * This will cause send_complete() to be called in
  118. * another thread.
  119. */
  120. smp_read_barrier_depends(); /* see rvt_cq_exit */
  121. worker = cq->rdi->worker;
  122. if (likely(worker)) {
  123. cq->notify = RVT_CQ_NONE;
  124. cq->triggered++;
  125. kthread_queue_work(worker, &cq->comptask);
  126. }
  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(sizeof(*cq), GFP_KERNEL);
  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 = vmalloc_user(sz);
  200. if (!wc) {
  201. ret = ERR_PTR(-ENOMEM);
  202. goto bail_cq;
  203. }
  204. /*
  205. * Return the address of the WC as the offset to mmap.
  206. * See rvt_mmap() for details.
  207. */
  208. if (udata && udata->outlen >= sizeof(__u64)) {
  209. int err;
  210. cq->ip = rvt_create_mmap_info(rdi, sz, context, wc);
  211. if (!cq->ip) {
  212. ret = ERR_PTR(-ENOMEM);
  213. goto bail_wc;
  214. }
  215. err = ib_copy_to_udata(udata, &cq->ip->offset,
  216. sizeof(cq->ip->offset));
  217. if (err) {
  218. ret = ERR_PTR(err);
  219. goto bail_ip;
  220. }
  221. }
  222. spin_lock(&rdi->n_cqs_lock);
  223. if (rdi->n_cqs_allocated == rdi->dparms.props.max_cq) {
  224. spin_unlock(&rdi->n_cqs_lock);
  225. ret = ERR_PTR(-ENOMEM);
  226. goto bail_ip;
  227. }
  228. rdi->n_cqs_allocated++;
  229. spin_unlock(&rdi->n_cqs_lock);
  230. if (cq->ip) {
  231. spin_lock_irq(&rdi->pending_lock);
  232. list_add(&cq->ip->pending_mmaps, &rdi->pending_mmaps);
  233. spin_unlock_irq(&rdi->pending_lock);
  234. }
  235. /*
  236. * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
  237. * The number of entries should be >= the number requested or return
  238. * an error.
  239. */
  240. cq->rdi = rdi;
  241. cq->ibcq.cqe = entries;
  242. cq->notify = RVT_CQ_NONE;
  243. spin_lock_init(&cq->lock);
  244. kthread_init_work(&cq->comptask, send_complete);
  245. cq->queue = wc;
  246. ret = &cq->ibcq;
  247. goto done;
  248. bail_ip:
  249. kfree(cq->ip);
  250. bail_wc:
  251. vfree(wc);
  252. bail_cq:
  253. kfree(cq);
  254. done:
  255. return ret;
  256. }
  257. /**
  258. * rvt_destroy_cq - destroy a completion queue
  259. * @ibcq: the completion queue to destroy.
  260. *
  261. * Called by ib_destroy_cq() in the generic verbs code.
  262. *
  263. * Return: always 0
  264. */
  265. int rvt_destroy_cq(struct ib_cq *ibcq)
  266. {
  267. struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
  268. struct rvt_dev_info *rdi = cq->rdi;
  269. kthread_flush_work(&cq->comptask);
  270. spin_lock(&rdi->n_cqs_lock);
  271. rdi->n_cqs_allocated--;
  272. spin_unlock(&rdi->n_cqs_lock);
  273. if (cq->ip)
  274. kref_put(&cq->ip->ref, rvt_release_mmap_info);
  275. else
  276. vfree(cq->queue);
  277. kfree(cq);
  278. return 0;
  279. }
  280. /**
  281. * rvt_req_notify_cq - change the notification type for a completion queue
  282. * @ibcq: the completion queue
  283. * @notify_flags: the type of notification to request
  284. *
  285. * This may be called from interrupt context. Also called by
  286. * ib_req_notify_cq() in the generic verbs code.
  287. *
  288. * Return: 0 for success.
  289. */
  290. int rvt_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
  291. {
  292. struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
  293. unsigned long flags;
  294. int ret = 0;
  295. spin_lock_irqsave(&cq->lock, flags);
  296. /*
  297. * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
  298. * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
  299. */
  300. if (cq->notify != IB_CQ_NEXT_COMP)
  301. cq->notify = notify_flags & IB_CQ_SOLICITED_MASK;
  302. if ((notify_flags & IB_CQ_REPORT_MISSED_EVENTS) &&
  303. cq->queue->head != cq->queue->tail)
  304. ret = 1;
  305. spin_unlock_irqrestore(&cq->lock, flags);
  306. return ret;
  307. }
  308. /**
  309. * rvt_resize_cq - change the size of the CQ
  310. * @ibcq: the completion queue
  311. *
  312. * Return: 0 for success.
  313. */
  314. int rvt_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
  315. {
  316. struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
  317. struct rvt_cq_wc *old_wc;
  318. struct rvt_cq_wc *wc;
  319. u32 head, tail, n;
  320. int ret;
  321. u32 sz;
  322. struct rvt_dev_info *rdi = cq->rdi;
  323. if (cqe < 1 || cqe > rdi->dparms.props.max_cqe)
  324. return -EINVAL;
  325. /*
  326. * Need to use vmalloc() if we want to support large #s of entries.
  327. */
  328. sz = sizeof(*wc);
  329. if (udata && udata->outlen >= sizeof(__u64))
  330. sz += sizeof(struct ib_uverbs_wc) * (cqe + 1);
  331. else
  332. sz += sizeof(struct ib_wc) * (cqe + 1);
  333. wc = vmalloc_user(sz);
  334. if (!wc)
  335. return -ENOMEM;
  336. /* Check that we can write the offset to mmap. */
  337. if (udata && udata->outlen >= sizeof(__u64)) {
  338. __u64 offset = 0;
  339. ret = ib_copy_to_udata(udata, &offset, sizeof(offset));
  340. if (ret)
  341. goto bail_free;
  342. }
  343. spin_lock_irq(&cq->lock);
  344. /*
  345. * Make sure head and tail are sane since they
  346. * might be user writable.
  347. */
  348. old_wc = cq->queue;
  349. head = old_wc->head;
  350. if (head > (u32)cq->ibcq.cqe)
  351. head = (u32)cq->ibcq.cqe;
  352. tail = old_wc->tail;
  353. if (tail > (u32)cq->ibcq.cqe)
  354. tail = (u32)cq->ibcq.cqe;
  355. if (head < tail)
  356. n = cq->ibcq.cqe + 1 + head - tail;
  357. else
  358. n = head - tail;
  359. if (unlikely((u32)cqe < n)) {
  360. ret = -EINVAL;
  361. goto bail_unlock;
  362. }
  363. for (n = 0; tail != head; n++) {
  364. if (cq->ip)
  365. wc->uqueue[n] = old_wc->uqueue[tail];
  366. else
  367. wc->kqueue[n] = old_wc->kqueue[tail];
  368. if (tail == (u32)cq->ibcq.cqe)
  369. tail = 0;
  370. else
  371. tail++;
  372. }
  373. cq->ibcq.cqe = cqe;
  374. wc->head = n;
  375. wc->tail = 0;
  376. cq->queue = wc;
  377. spin_unlock_irq(&cq->lock);
  378. vfree(old_wc);
  379. if (cq->ip) {
  380. struct rvt_mmap_info *ip = cq->ip;
  381. rvt_update_mmap_info(rdi, ip, sz, wc);
  382. /*
  383. * Return the offset to mmap.
  384. * See rvt_mmap() for details.
  385. */
  386. if (udata && udata->outlen >= sizeof(__u64)) {
  387. ret = ib_copy_to_udata(udata, &ip->offset,
  388. sizeof(ip->offset));
  389. if (ret)
  390. return ret;
  391. }
  392. spin_lock_irq(&rdi->pending_lock);
  393. if (list_empty(&ip->pending_mmaps))
  394. list_add(&ip->pending_mmaps, &rdi->pending_mmaps);
  395. spin_unlock_irq(&rdi->pending_lock);
  396. }
  397. return 0;
  398. bail_unlock:
  399. spin_unlock_irq(&cq->lock);
  400. bail_free:
  401. vfree(wc);
  402. return ret;
  403. }
  404. /**
  405. * rvt_poll_cq - poll for work completion entries
  406. * @ibcq: the completion queue to poll
  407. * @num_entries: the maximum number of entries to return
  408. * @entry: pointer to array where work completions are placed
  409. *
  410. * This may be called from interrupt context. Also called by ib_poll_cq()
  411. * in the generic verbs code.
  412. *
  413. * Return: the number of completion entries polled.
  414. */
  415. int rvt_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
  416. {
  417. struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
  418. struct rvt_cq_wc *wc;
  419. unsigned long flags;
  420. int npolled;
  421. u32 tail;
  422. /* The kernel can only poll a kernel completion queue */
  423. if (cq->ip)
  424. return -EINVAL;
  425. spin_lock_irqsave(&cq->lock, flags);
  426. wc = cq->queue;
  427. tail = wc->tail;
  428. if (tail > (u32)cq->ibcq.cqe)
  429. tail = (u32)cq->ibcq.cqe;
  430. for (npolled = 0; npolled < num_entries; ++npolled, ++entry) {
  431. if (tail == wc->head)
  432. break;
  433. /* The kernel doesn't need a RMB since it has the lock. */
  434. *entry = wc->kqueue[tail];
  435. if (tail >= cq->ibcq.cqe)
  436. tail = 0;
  437. else
  438. tail++;
  439. }
  440. wc->tail = tail;
  441. spin_unlock_irqrestore(&cq->lock, flags);
  442. return npolled;
  443. }
  444. /**
  445. * rvt_driver_cq_init - Init cq resources on behalf of driver
  446. * @rdi: rvt dev structure
  447. *
  448. * Return: 0 on success
  449. */
  450. int rvt_driver_cq_init(struct rvt_dev_info *rdi)
  451. {
  452. int ret = 0;
  453. int cpu;
  454. struct task_struct *task;
  455. if (rdi->worker)
  456. return 0;
  457. spin_lock_init(&rdi->n_cqs_lock);
  458. rdi->worker = kzalloc(sizeof(*rdi->worker), GFP_KERNEL);
  459. if (!rdi->worker)
  460. return -ENOMEM;
  461. kthread_init_worker(rdi->worker);
  462. task = kthread_create_on_node(
  463. kthread_worker_fn,
  464. rdi->worker,
  465. rdi->dparms.node,
  466. "%s", rdi->dparms.cq_name);
  467. if (IS_ERR(task)) {
  468. kfree(rdi->worker);
  469. rdi->worker = NULL;
  470. return PTR_ERR(task);
  471. }
  472. set_user_nice(task, MIN_NICE);
  473. cpu = cpumask_first(cpumask_of_node(rdi->dparms.node));
  474. kthread_bind(task, cpu);
  475. wake_up_process(task);
  476. return ret;
  477. }
  478. /**
  479. * rvt_cq_exit - tear down cq reources
  480. * @rdi: rvt dev structure
  481. */
  482. void rvt_cq_exit(struct rvt_dev_info *rdi)
  483. {
  484. struct kthread_worker *worker;
  485. worker = rdi->worker;
  486. if (!worker)
  487. return;
  488. /* blocks future queuing from send_complete() */
  489. rdi->worker = NULL;
  490. smp_wmb(); /* See rdi_cq_enter */
  491. kthread_flush_worker(worker);
  492. kthread_stop(worker->task);
  493. kfree(worker);
  494. }