qib_cq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Copyright (c) 2013 Intel Corporation. All rights reserved.
  3. * Copyright (c) 2006, 2007, 2008, 2010 QLogic Corporation. All rights reserved.
  4. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/err.h>
  35. #include <linux/slab.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/kthread.h>
  38. #include "qib_verbs.h"
  39. #include "qib.h"
  40. /**
  41. * qib_cq_enter - add a new entry to the completion queue
  42. * @cq: completion queue
  43. * @entry: work completion entry to add
  44. * @sig: true if @entry is a solicitated entry
  45. *
  46. * This may be called with qp->s_lock held.
  47. */
  48. void qib_cq_enter(struct qib_cq *cq, struct ib_wc *entry, int solicited)
  49. {
  50. struct qib_cq_wc *wc;
  51. unsigned long flags;
  52. u32 head;
  53. u32 next;
  54. spin_lock_irqsave(&cq->lock, flags);
  55. /*
  56. * Note that the head pointer might be writable by user processes.
  57. * Take care to verify it is a sane value.
  58. */
  59. wc = cq->queue;
  60. head = wc->head;
  61. if (head >= (unsigned) cq->ibcq.cqe) {
  62. head = cq->ibcq.cqe;
  63. next = 0;
  64. } else
  65. next = head + 1;
  66. if (unlikely(next == wc->tail)) {
  67. spin_unlock_irqrestore(&cq->lock, flags);
  68. if (cq->ibcq.event_handler) {
  69. struct ib_event ev;
  70. ev.device = cq->ibcq.device;
  71. ev.element.cq = &cq->ibcq;
  72. ev.event = IB_EVENT_CQ_ERR;
  73. cq->ibcq.event_handler(&ev, cq->ibcq.cq_context);
  74. }
  75. return;
  76. }
  77. if (cq->ip) {
  78. wc->uqueue[head].wr_id = entry->wr_id;
  79. wc->uqueue[head].status = entry->status;
  80. wc->uqueue[head].opcode = entry->opcode;
  81. wc->uqueue[head].vendor_err = entry->vendor_err;
  82. wc->uqueue[head].byte_len = entry->byte_len;
  83. wc->uqueue[head].ex.imm_data =
  84. (__u32 __force)entry->ex.imm_data;
  85. wc->uqueue[head].qp_num = entry->qp->qp_num;
  86. wc->uqueue[head].src_qp = entry->src_qp;
  87. wc->uqueue[head].wc_flags = entry->wc_flags;
  88. wc->uqueue[head].pkey_index = entry->pkey_index;
  89. wc->uqueue[head].slid = entry->slid;
  90. wc->uqueue[head].sl = entry->sl;
  91. wc->uqueue[head].dlid_path_bits = entry->dlid_path_bits;
  92. wc->uqueue[head].port_num = entry->port_num;
  93. /* Make sure entry is written before the head index. */
  94. smp_wmb();
  95. } else
  96. wc->kqueue[head] = *entry;
  97. wc->head = next;
  98. if (cq->notify == IB_CQ_NEXT_COMP ||
  99. (cq->notify == IB_CQ_SOLICITED &&
  100. (solicited || entry->status != IB_WC_SUCCESS))) {
  101. struct kthread_worker *worker;
  102. /*
  103. * This will cause send_complete() to be called in
  104. * another thread.
  105. */
  106. smp_rmb();
  107. worker = cq->dd->worker;
  108. if (likely(worker)) {
  109. cq->notify = IB_CQ_NONE;
  110. cq->triggered++;
  111. queue_kthread_work(worker, &cq->comptask);
  112. }
  113. }
  114. spin_unlock_irqrestore(&cq->lock, flags);
  115. }
  116. /**
  117. * qib_poll_cq - poll for work completion entries
  118. * @ibcq: the completion queue to poll
  119. * @num_entries: the maximum number of entries to return
  120. * @entry: pointer to array where work completions are placed
  121. *
  122. * Returns the number of completion entries polled.
  123. *
  124. * This may be called from interrupt context. Also called by ib_poll_cq()
  125. * in the generic verbs code.
  126. */
  127. int qib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
  128. {
  129. struct qib_cq *cq = to_icq(ibcq);
  130. struct qib_cq_wc *wc;
  131. unsigned long flags;
  132. int npolled;
  133. u32 tail;
  134. /* The kernel can only poll a kernel completion queue */
  135. if (cq->ip) {
  136. npolled = -EINVAL;
  137. goto bail;
  138. }
  139. spin_lock_irqsave(&cq->lock, flags);
  140. wc = cq->queue;
  141. tail = wc->tail;
  142. if (tail > (u32) cq->ibcq.cqe)
  143. tail = (u32) cq->ibcq.cqe;
  144. for (npolled = 0; npolled < num_entries; ++npolled, ++entry) {
  145. if (tail == wc->head)
  146. break;
  147. /* The kernel doesn't need a RMB since it has the lock. */
  148. *entry = wc->kqueue[tail];
  149. if (tail >= cq->ibcq.cqe)
  150. tail = 0;
  151. else
  152. tail++;
  153. }
  154. wc->tail = tail;
  155. spin_unlock_irqrestore(&cq->lock, flags);
  156. bail:
  157. return npolled;
  158. }
  159. static void send_complete(struct kthread_work *work)
  160. {
  161. struct qib_cq *cq = container_of(work, struct qib_cq, comptask);
  162. /*
  163. * The completion handler will most likely rearm the notification
  164. * and poll for all pending entries. If a new completion entry
  165. * is added while we are in this routine, queue_work()
  166. * won't call us again until we return so we check triggered to
  167. * see if we need to call the handler again.
  168. */
  169. for (;;) {
  170. u8 triggered = cq->triggered;
  171. /*
  172. * IPoIB connected mode assumes the callback is from a
  173. * soft IRQ. We simulate this by blocking "bottom halves".
  174. * See the implementation for ipoib_cm_handle_tx_wc(),
  175. * netif_tx_lock_bh() and netif_tx_lock().
  176. */
  177. local_bh_disable();
  178. cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
  179. local_bh_enable();
  180. if (cq->triggered == triggered)
  181. return;
  182. }
  183. }
  184. /**
  185. * qib_create_cq - create a completion queue
  186. * @ibdev: the device this completion queue is attached to
  187. * @attr: creation attributes
  188. * @context: unused by the QLogic_IB driver
  189. * @udata: user data for libibverbs.so
  190. *
  191. * Returns a pointer to the completion queue or negative errno values
  192. * for failure.
  193. *
  194. * Called by ib_create_cq() in the generic verbs code.
  195. */
  196. struct ib_cq *qib_create_cq(struct ib_device *ibdev,
  197. const struct ib_cq_init_attr *attr,
  198. struct ib_ucontext *context,
  199. struct ib_udata *udata)
  200. {
  201. int entries = attr->cqe;
  202. struct qib_ibdev *dev = to_idev(ibdev);
  203. struct qib_cq *cq;
  204. struct qib_cq_wc *wc;
  205. struct ib_cq *ret;
  206. u32 sz;
  207. if (attr->flags)
  208. return ERR_PTR(-EINVAL);
  209. if (entries < 1 || entries > ib_qib_max_cqes) {
  210. ret = ERR_PTR(-EINVAL);
  211. goto done;
  212. }
  213. /* Allocate the completion queue structure. */
  214. cq = kmalloc(sizeof(*cq), GFP_KERNEL);
  215. if (!cq) {
  216. ret = ERR_PTR(-ENOMEM);
  217. goto done;
  218. }
  219. /*
  220. * Allocate the completion queue entries and head/tail pointers.
  221. * This is allocated separately so that it can be resized and
  222. * also mapped into user space.
  223. * We need to use vmalloc() in order to support mmap and large
  224. * numbers of entries.
  225. */
  226. sz = sizeof(*wc);
  227. if (udata && udata->outlen >= sizeof(__u64))
  228. sz += sizeof(struct ib_uverbs_wc) * (entries + 1);
  229. else
  230. sz += sizeof(struct ib_wc) * (entries + 1);
  231. wc = vmalloc_user(sz);
  232. if (!wc) {
  233. ret = ERR_PTR(-ENOMEM);
  234. goto bail_cq;
  235. }
  236. /*
  237. * Return the address of the WC as the offset to mmap.
  238. * See qib_mmap() for details.
  239. */
  240. if (udata && udata->outlen >= sizeof(__u64)) {
  241. int err;
  242. cq->ip = qib_create_mmap_info(dev, sz, context, wc);
  243. if (!cq->ip) {
  244. ret = ERR_PTR(-ENOMEM);
  245. goto bail_wc;
  246. }
  247. err = ib_copy_to_udata(udata, &cq->ip->offset,
  248. sizeof(cq->ip->offset));
  249. if (err) {
  250. ret = ERR_PTR(err);
  251. goto bail_ip;
  252. }
  253. } else
  254. cq->ip = NULL;
  255. spin_lock(&dev->n_cqs_lock);
  256. if (dev->n_cqs_allocated == ib_qib_max_cqs) {
  257. spin_unlock(&dev->n_cqs_lock);
  258. ret = ERR_PTR(-ENOMEM);
  259. goto bail_ip;
  260. }
  261. dev->n_cqs_allocated++;
  262. spin_unlock(&dev->n_cqs_lock);
  263. if (cq->ip) {
  264. spin_lock_irq(&dev->pending_lock);
  265. list_add(&cq->ip->pending_mmaps, &dev->pending_mmaps);
  266. spin_unlock_irq(&dev->pending_lock);
  267. }
  268. /*
  269. * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
  270. * The number of entries should be >= the number requested or return
  271. * an error.
  272. */
  273. cq->dd = dd_from_dev(dev);
  274. cq->ibcq.cqe = entries;
  275. cq->notify = IB_CQ_NONE;
  276. cq->triggered = 0;
  277. spin_lock_init(&cq->lock);
  278. init_kthread_work(&cq->comptask, send_complete);
  279. wc->head = 0;
  280. wc->tail = 0;
  281. cq->queue = wc;
  282. ret = &cq->ibcq;
  283. goto done;
  284. bail_ip:
  285. kfree(cq->ip);
  286. bail_wc:
  287. vfree(wc);
  288. bail_cq:
  289. kfree(cq);
  290. done:
  291. return ret;
  292. }
  293. /**
  294. * qib_destroy_cq - destroy a completion queue
  295. * @ibcq: the completion queue to destroy.
  296. *
  297. * Returns 0 for success.
  298. *
  299. * Called by ib_destroy_cq() in the generic verbs code.
  300. */
  301. int qib_destroy_cq(struct ib_cq *ibcq)
  302. {
  303. struct qib_ibdev *dev = to_idev(ibcq->device);
  304. struct qib_cq *cq = to_icq(ibcq);
  305. flush_kthread_work(&cq->comptask);
  306. spin_lock(&dev->n_cqs_lock);
  307. dev->n_cqs_allocated--;
  308. spin_unlock(&dev->n_cqs_lock);
  309. if (cq->ip)
  310. kref_put(&cq->ip->ref, qib_release_mmap_info);
  311. else
  312. vfree(cq->queue);
  313. kfree(cq);
  314. return 0;
  315. }
  316. /**
  317. * qib_req_notify_cq - change the notification type for a completion queue
  318. * @ibcq: the completion queue
  319. * @notify_flags: the type of notification to request
  320. *
  321. * Returns 0 for success.
  322. *
  323. * This may be called from interrupt context. Also called by
  324. * ib_req_notify_cq() in the generic verbs code.
  325. */
  326. int qib_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
  327. {
  328. struct qib_cq *cq = to_icq(ibcq);
  329. unsigned long flags;
  330. int ret = 0;
  331. spin_lock_irqsave(&cq->lock, flags);
  332. /*
  333. * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
  334. * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
  335. */
  336. if (cq->notify != IB_CQ_NEXT_COMP)
  337. cq->notify = notify_flags & IB_CQ_SOLICITED_MASK;
  338. if ((notify_flags & IB_CQ_REPORT_MISSED_EVENTS) &&
  339. cq->queue->head != cq->queue->tail)
  340. ret = 1;
  341. spin_unlock_irqrestore(&cq->lock, flags);
  342. return ret;
  343. }
  344. /**
  345. * qib_resize_cq - change the size of the CQ
  346. * @ibcq: the completion queue
  347. *
  348. * Returns 0 for success.
  349. */
  350. int qib_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
  351. {
  352. struct qib_cq *cq = to_icq(ibcq);
  353. struct qib_cq_wc *old_wc;
  354. struct qib_cq_wc *wc;
  355. u32 head, tail, n;
  356. int ret;
  357. u32 sz;
  358. if (cqe < 1 || cqe > ib_qib_max_cqes) {
  359. ret = -EINVAL;
  360. goto bail;
  361. }
  362. /*
  363. * Need to use vmalloc() if we want to support large #s of entries.
  364. */
  365. sz = sizeof(*wc);
  366. if (udata && udata->outlen >= sizeof(__u64))
  367. sz += sizeof(struct ib_uverbs_wc) * (cqe + 1);
  368. else
  369. sz += sizeof(struct ib_wc) * (cqe + 1);
  370. wc = vmalloc_user(sz);
  371. if (!wc) {
  372. ret = -ENOMEM;
  373. goto bail;
  374. }
  375. /* Check that we can write the offset to mmap. */
  376. if (udata && udata->outlen >= sizeof(__u64)) {
  377. __u64 offset = 0;
  378. ret = ib_copy_to_udata(udata, &offset, sizeof(offset));
  379. if (ret)
  380. goto bail_free;
  381. }
  382. spin_lock_irq(&cq->lock);
  383. /*
  384. * Make sure head and tail are sane since they
  385. * might be user writable.
  386. */
  387. old_wc = cq->queue;
  388. head = old_wc->head;
  389. if (head > (u32) cq->ibcq.cqe)
  390. head = (u32) cq->ibcq.cqe;
  391. tail = old_wc->tail;
  392. if (tail > (u32) cq->ibcq.cqe)
  393. tail = (u32) cq->ibcq.cqe;
  394. if (head < tail)
  395. n = cq->ibcq.cqe + 1 + head - tail;
  396. else
  397. n = head - tail;
  398. if (unlikely((u32)cqe < n)) {
  399. ret = -EINVAL;
  400. goto bail_unlock;
  401. }
  402. for (n = 0; tail != head; n++) {
  403. if (cq->ip)
  404. wc->uqueue[n] = old_wc->uqueue[tail];
  405. else
  406. wc->kqueue[n] = old_wc->kqueue[tail];
  407. if (tail == (u32) cq->ibcq.cqe)
  408. tail = 0;
  409. else
  410. tail++;
  411. }
  412. cq->ibcq.cqe = cqe;
  413. wc->head = n;
  414. wc->tail = 0;
  415. cq->queue = wc;
  416. spin_unlock_irq(&cq->lock);
  417. vfree(old_wc);
  418. if (cq->ip) {
  419. struct qib_ibdev *dev = to_idev(ibcq->device);
  420. struct qib_mmap_info *ip = cq->ip;
  421. qib_update_mmap_info(dev, ip, sz, wc);
  422. /*
  423. * Return the offset to mmap.
  424. * See qib_mmap() for details.
  425. */
  426. if (udata && udata->outlen >= sizeof(__u64)) {
  427. ret = ib_copy_to_udata(udata, &ip->offset,
  428. sizeof(ip->offset));
  429. if (ret)
  430. goto bail;
  431. }
  432. spin_lock_irq(&dev->pending_lock);
  433. if (list_empty(&ip->pending_mmaps))
  434. list_add(&ip->pending_mmaps, &dev->pending_mmaps);
  435. spin_unlock_irq(&dev->pending_lock);
  436. }
  437. ret = 0;
  438. goto bail;
  439. bail_unlock:
  440. spin_unlock_irq(&cq->lock);
  441. bail_free:
  442. vfree(wc);
  443. bail:
  444. return ret;
  445. }
  446. int qib_cq_init(struct qib_devdata *dd)
  447. {
  448. int ret = 0;
  449. int cpu;
  450. struct task_struct *task;
  451. if (dd->worker)
  452. return 0;
  453. dd->worker = kzalloc(sizeof(*dd->worker), GFP_KERNEL);
  454. if (!dd->worker)
  455. return -ENOMEM;
  456. init_kthread_worker(dd->worker);
  457. task = kthread_create_on_node(
  458. kthread_worker_fn,
  459. dd->worker,
  460. dd->assigned_node_id,
  461. "qib_cq%d", dd->unit);
  462. if (IS_ERR(task))
  463. goto task_fail;
  464. cpu = cpumask_first(cpumask_of_node(dd->assigned_node_id));
  465. kthread_bind(task, cpu);
  466. wake_up_process(task);
  467. out:
  468. return ret;
  469. task_fail:
  470. ret = PTR_ERR(task);
  471. kfree(dd->worker);
  472. dd->worker = NULL;
  473. goto out;
  474. }
  475. void qib_cq_exit(struct qib_devdata *dd)
  476. {
  477. struct kthread_worker *worker;
  478. worker = dd->worker;
  479. if (!worker)
  480. return;
  481. /* blocks future queuing from send_complete() */
  482. dd->worker = NULL;
  483. smp_wmb();
  484. flush_kthread_worker(worker);
  485. kthread_stop(worker->task);
  486. kfree(worker);
  487. }