cq.c 14 KB

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