qib_cq.c 12 KB

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