qib_cq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. * @entries: the minimum size of the completion queue
  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, int entries,
  197. int comp_vector, struct ib_ucontext *context,
  198. struct ib_udata *udata)
  199. {
  200. struct qib_ibdev *dev = to_idev(ibdev);
  201. struct qib_cq *cq;
  202. struct qib_cq_wc *wc;
  203. struct ib_cq *ret;
  204. u32 sz;
  205. if (entries < 1 || entries > ib_qib_max_cqes) {
  206. ret = ERR_PTR(-EINVAL);
  207. goto done;
  208. }
  209. /* Allocate the completion queue structure. */
  210. cq = kmalloc(sizeof(*cq), GFP_KERNEL);
  211. if (!cq) {
  212. ret = ERR_PTR(-ENOMEM);
  213. goto done;
  214. }
  215. /*
  216. * Allocate the completion queue entries and head/tail pointers.
  217. * This is allocated separately so that it can be resized and
  218. * also mapped into user space.
  219. * We need to use vmalloc() in order to support mmap and large
  220. * numbers of entries.
  221. */
  222. sz = sizeof(*wc);
  223. if (udata && udata->outlen >= sizeof(__u64))
  224. sz += sizeof(struct ib_uverbs_wc) * (entries + 1);
  225. else
  226. sz += sizeof(struct ib_wc) * (entries + 1);
  227. wc = vmalloc_user(sz);
  228. if (!wc) {
  229. ret = ERR_PTR(-ENOMEM);
  230. goto bail_cq;
  231. }
  232. /*
  233. * Return the address of the WC as the offset to mmap.
  234. * See qib_mmap() for details.
  235. */
  236. if (udata && udata->outlen >= sizeof(__u64)) {
  237. int err;
  238. cq->ip = qib_create_mmap_info(dev, sz, context, wc);
  239. if (!cq->ip) {
  240. ret = ERR_PTR(-ENOMEM);
  241. goto bail_wc;
  242. }
  243. err = ib_copy_to_udata(udata, &cq->ip->offset,
  244. sizeof(cq->ip->offset));
  245. if (err) {
  246. ret = ERR_PTR(err);
  247. goto bail_ip;
  248. }
  249. } else
  250. cq->ip = NULL;
  251. spin_lock(&dev->n_cqs_lock);
  252. if (dev->n_cqs_allocated == ib_qib_max_cqs) {
  253. spin_unlock(&dev->n_cqs_lock);
  254. ret = ERR_PTR(-ENOMEM);
  255. goto bail_ip;
  256. }
  257. dev->n_cqs_allocated++;
  258. spin_unlock(&dev->n_cqs_lock);
  259. if (cq->ip) {
  260. spin_lock_irq(&dev->pending_lock);
  261. list_add(&cq->ip->pending_mmaps, &dev->pending_mmaps);
  262. spin_unlock_irq(&dev->pending_lock);
  263. }
  264. /*
  265. * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
  266. * The number of entries should be >= the number requested or return
  267. * an error.
  268. */
  269. cq->dd = dd_from_dev(dev);
  270. cq->ibcq.cqe = entries;
  271. cq->notify = IB_CQ_NONE;
  272. cq->triggered = 0;
  273. spin_lock_init(&cq->lock);
  274. init_kthread_work(&cq->comptask, send_complete);
  275. wc->head = 0;
  276. wc->tail = 0;
  277. cq->queue = wc;
  278. ret = &cq->ibcq;
  279. goto done;
  280. bail_ip:
  281. kfree(cq->ip);
  282. bail_wc:
  283. vfree(wc);
  284. bail_cq:
  285. kfree(cq);
  286. done:
  287. return ret;
  288. }
  289. /**
  290. * qib_destroy_cq - destroy a completion queue
  291. * @ibcq: the completion queue to destroy.
  292. *
  293. * Returns 0 for success.
  294. *
  295. * Called by ib_destroy_cq() in the generic verbs code.
  296. */
  297. int qib_destroy_cq(struct ib_cq *ibcq)
  298. {
  299. struct qib_ibdev *dev = to_idev(ibcq->device);
  300. struct qib_cq *cq = to_icq(ibcq);
  301. flush_kthread_work(&cq->comptask);
  302. spin_lock(&dev->n_cqs_lock);
  303. dev->n_cqs_allocated--;
  304. spin_unlock(&dev->n_cqs_lock);
  305. if (cq->ip)
  306. kref_put(&cq->ip->ref, qib_release_mmap_info);
  307. else
  308. vfree(cq->queue);
  309. kfree(cq);
  310. return 0;
  311. }
  312. /**
  313. * qib_req_notify_cq - change the notification type for a completion queue
  314. * @ibcq: the completion queue
  315. * @notify_flags: the type of notification to request
  316. *
  317. * Returns 0 for success.
  318. *
  319. * This may be called from interrupt context. Also called by
  320. * ib_req_notify_cq() in the generic verbs code.
  321. */
  322. int qib_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
  323. {
  324. struct qib_cq *cq = to_icq(ibcq);
  325. unsigned long flags;
  326. int ret = 0;
  327. spin_lock_irqsave(&cq->lock, flags);
  328. /*
  329. * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
  330. * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
  331. */
  332. if (cq->notify != IB_CQ_NEXT_COMP)
  333. cq->notify = notify_flags & IB_CQ_SOLICITED_MASK;
  334. if ((notify_flags & IB_CQ_REPORT_MISSED_EVENTS) &&
  335. cq->queue->head != cq->queue->tail)
  336. ret = 1;
  337. spin_unlock_irqrestore(&cq->lock, flags);
  338. return ret;
  339. }
  340. /**
  341. * qib_resize_cq - change the size of the CQ
  342. * @ibcq: the completion queue
  343. *
  344. * Returns 0 for success.
  345. */
  346. int qib_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
  347. {
  348. struct qib_cq *cq = to_icq(ibcq);
  349. struct qib_cq_wc *old_wc;
  350. struct qib_cq_wc *wc;
  351. u32 head, tail, n;
  352. int ret;
  353. u32 sz;
  354. if (cqe < 1 || cqe > ib_qib_max_cqes) {
  355. ret = -EINVAL;
  356. goto bail;
  357. }
  358. /*
  359. * Need to use vmalloc() if we want to support large #s of entries.
  360. */
  361. sz = sizeof(*wc);
  362. if (udata && udata->outlen >= sizeof(__u64))
  363. sz += sizeof(struct ib_uverbs_wc) * (cqe + 1);
  364. else
  365. sz += sizeof(struct ib_wc) * (cqe + 1);
  366. wc = vmalloc_user(sz);
  367. if (!wc) {
  368. ret = -ENOMEM;
  369. goto bail;
  370. }
  371. /* Check that we can write the offset to mmap. */
  372. if (udata && udata->outlen >= sizeof(__u64)) {
  373. __u64 offset = 0;
  374. ret = ib_copy_to_udata(udata, &offset, sizeof(offset));
  375. if (ret)
  376. goto bail_free;
  377. }
  378. spin_lock_irq(&cq->lock);
  379. /*
  380. * Make sure head and tail are sane since they
  381. * might be user writable.
  382. */
  383. old_wc = cq->queue;
  384. head = old_wc->head;
  385. if (head > (u32) cq->ibcq.cqe)
  386. head = (u32) cq->ibcq.cqe;
  387. tail = old_wc->tail;
  388. if (tail > (u32) cq->ibcq.cqe)
  389. tail = (u32) cq->ibcq.cqe;
  390. if (head < tail)
  391. n = cq->ibcq.cqe + 1 + head - tail;
  392. else
  393. n = head - tail;
  394. if (unlikely((u32)cqe < n)) {
  395. ret = -EINVAL;
  396. goto bail_unlock;
  397. }
  398. for (n = 0; tail != head; n++) {
  399. if (cq->ip)
  400. wc->uqueue[n] = old_wc->uqueue[tail];
  401. else
  402. wc->kqueue[n] = old_wc->kqueue[tail];
  403. if (tail == (u32) cq->ibcq.cqe)
  404. tail = 0;
  405. else
  406. tail++;
  407. }
  408. cq->ibcq.cqe = cqe;
  409. wc->head = n;
  410. wc->tail = 0;
  411. cq->queue = wc;
  412. spin_unlock_irq(&cq->lock);
  413. vfree(old_wc);
  414. if (cq->ip) {
  415. struct qib_ibdev *dev = to_idev(ibcq->device);
  416. struct qib_mmap_info *ip = cq->ip;
  417. qib_update_mmap_info(dev, ip, sz, wc);
  418. /*
  419. * Return the offset to mmap.
  420. * See qib_mmap() for details.
  421. */
  422. if (udata && udata->outlen >= sizeof(__u64)) {
  423. ret = ib_copy_to_udata(udata, &ip->offset,
  424. sizeof(ip->offset));
  425. if (ret)
  426. goto bail;
  427. }
  428. spin_lock_irq(&dev->pending_lock);
  429. if (list_empty(&ip->pending_mmaps))
  430. list_add(&ip->pending_mmaps, &dev->pending_mmaps);
  431. spin_unlock_irq(&dev->pending_lock);
  432. }
  433. ret = 0;
  434. goto bail;
  435. bail_unlock:
  436. spin_unlock_irq(&cq->lock);
  437. bail_free:
  438. vfree(wc);
  439. bail:
  440. return ret;
  441. }
  442. int qib_cq_init(struct qib_devdata *dd)
  443. {
  444. int ret = 0;
  445. int cpu;
  446. struct task_struct *task;
  447. if (dd->worker)
  448. return 0;
  449. dd->worker = kzalloc(sizeof(*dd->worker), GFP_KERNEL);
  450. if (!dd->worker)
  451. return -ENOMEM;
  452. init_kthread_worker(dd->worker);
  453. task = kthread_create_on_node(
  454. kthread_worker_fn,
  455. dd->worker,
  456. dd->assigned_node_id,
  457. "qib_cq%d", dd->unit);
  458. if (IS_ERR(task))
  459. goto task_fail;
  460. cpu = cpumask_first(cpumask_of_node(dd->assigned_node_id));
  461. kthread_bind(task, cpu);
  462. wake_up_process(task);
  463. out:
  464. return ret;
  465. task_fail:
  466. ret = PTR_ERR(task);
  467. kfree(dd->worker);
  468. dd->worker = NULL;
  469. goto out;
  470. }
  471. void qib_cq_exit(struct qib_devdata *dd)
  472. {
  473. struct kthread_worker *worker;
  474. worker = dd->worker;
  475. if (!worker)
  476. return;
  477. /* blocks future queuing from send_complete() */
  478. dd->worker = NULL;
  479. smp_wmb();
  480. flush_kthread_worker(worker);
  481. kthread_stop(worker->task);
  482. kfree(worker);
  483. }