ipath_cq.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (c) 2006 QLogic, Inc. 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/vmalloc.h>
  35. #include "ipath_verbs.h"
  36. /**
  37. * ipath_cq_enter - add a new entry to the completion queue
  38. * @cq: completion queue
  39. * @entry: work completion entry to add
  40. * @sig: true if @entry is a solicitated entry
  41. *
  42. * This may be called with qp->s_lock held.
  43. */
  44. void ipath_cq_enter(struct ipath_cq *cq, struct ib_wc *entry, int solicited)
  45. {
  46. struct ipath_cq_wc *wc = cq->queue;
  47. unsigned long flags;
  48. u32 head;
  49. u32 next;
  50. spin_lock_irqsave(&cq->lock, flags);
  51. /*
  52. * Note that the head pointer might be writable by user processes.
  53. * Take care to verify it is a sane value.
  54. */
  55. head = wc->head;
  56. if (head >= (unsigned) cq->ibcq.cqe) {
  57. head = cq->ibcq.cqe;
  58. next = 0;
  59. } else
  60. next = head + 1;
  61. if (unlikely(next == wc->tail)) {
  62. spin_unlock_irqrestore(&cq->lock, flags);
  63. if (cq->ibcq.event_handler) {
  64. struct ib_event ev;
  65. ev.device = cq->ibcq.device;
  66. ev.element.cq = &cq->ibcq;
  67. ev.event = IB_EVENT_CQ_ERR;
  68. cq->ibcq.event_handler(&ev, cq->ibcq.cq_context);
  69. }
  70. return;
  71. }
  72. wc->queue[head] = *entry;
  73. wc->head = next;
  74. if (cq->notify == IB_CQ_NEXT_COMP ||
  75. (cq->notify == IB_CQ_SOLICITED && solicited)) {
  76. cq->notify = IB_CQ_NONE;
  77. cq->triggered++;
  78. /*
  79. * This will cause send_complete() to be called in
  80. * another thread.
  81. */
  82. tasklet_hi_schedule(&cq->comptask);
  83. }
  84. spin_unlock_irqrestore(&cq->lock, flags);
  85. if (entry->status != IB_WC_SUCCESS)
  86. to_idev(cq->ibcq.device)->n_wqe_errs++;
  87. }
  88. /**
  89. * ipath_poll_cq - poll for work completion entries
  90. * @ibcq: the completion queue to poll
  91. * @num_entries: the maximum number of entries to return
  92. * @entry: pointer to array where work completions are placed
  93. *
  94. * Returns the number of completion entries polled.
  95. *
  96. * This may be called from interrupt context. Also called by ib_poll_cq()
  97. * in the generic verbs code.
  98. */
  99. int ipath_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
  100. {
  101. struct ipath_cq *cq = to_icq(ibcq);
  102. struct ipath_cq_wc *wc = cq->queue;
  103. unsigned long flags;
  104. int npolled;
  105. spin_lock_irqsave(&cq->lock, flags);
  106. for (npolled = 0; npolled < num_entries; ++npolled, ++entry) {
  107. if (wc->tail == wc->head)
  108. break;
  109. *entry = wc->queue[wc->tail];
  110. if (wc->tail >= cq->ibcq.cqe)
  111. wc->tail = 0;
  112. else
  113. wc->tail++;
  114. }
  115. spin_unlock_irqrestore(&cq->lock, flags);
  116. return npolled;
  117. }
  118. static void send_complete(unsigned long data)
  119. {
  120. struct ipath_cq *cq = (struct ipath_cq *)data;
  121. /*
  122. * The completion handler will most likely rearm the notification
  123. * and poll for all pending entries. If a new completion entry
  124. * is added while we are in this routine, tasklet_hi_schedule()
  125. * won't call us again until we return so we check triggered to
  126. * see if we need to call the handler again.
  127. */
  128. for (;;) {
  129. u8 triggered = cq->triggered;
  130. cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
  131. if (cq->triggered == triggered)
  132. return;
  133. }
  134. }
  135. /**
  136. * ipath_create_cq - create a completion queue
  137. * @ibdev: the device this completion queue is attached to
  138. * @entries: the minimum size of the completion queue
  139. * @context: unused by the InfiniPath driver
  140. * @udata: unused by the InfiniPath driver
  141. *
  142. * Returns a pointer to the completion queue or negative errno values
  143. * for failure.
  144. *
  145. * Called by ib_create_cq() in the generic verbs code.
  146. */
  147. struct ib_cq *ipath_create_cq(struct ib_device *ibdev, int entries,
  148. struct ib_ucontext *context,
  149. struct ib_udata *udata)
  150. {
  151. struct ipath_ibdev *dev = to_idev(ibdev);
  152. struct ipath_cq *cq;
  153. struct ipath_cq_wc *wc;
  154. struct ib_cq *ret;
  155. if (entries < 1 || entries > ib_ipath_max_cqes) {
  156. ret = ERR_PTR(-EINVAL);
  157. goto done;
  158. }
  159. if (dev->n_cqs_allocated == ib_ipath_max_cqs) {
  160. ret = ERR_PTR(-ENOMEM);
  161. goto done;
  162. }
  163. /* Allocate the completion queue structure. */
  164. cq = kmalloc(sizeof(*cq), GFP_KERNEL);
  165. if (!cq) {
  166. ret = ERR_PTR(-ENOMEM);
  167. goto done;
  168. }
  169. /*
  170. * Allocate the completion queue entries and head/tail pointers.
  171. * This is allocated separately so that it can be resized and
  172. * also mapped into user space.
  173. * We need to use vmalloc() in order to support mmap and large
  174. * numbers of entries.
  175. */
  176. wc = vmalloc_user(sizeof(*wc) + sizeof(struct ib_wc) * entries);
  177. if (!wc) {
  178. ret = ERR_PTR(-ENOMEM);
  179. goto bail_cq;
  180. }
  181. /*
  182. * Return the address of the WC as the offset to mmap.
  183. * See ipath_mmap() for details.
  184. */
  185. if (udata && udata->outlen >= sizeof(__u64)) {
  186. struct ipath_mmap_info *ip;
  187. __u64 offset = (__u64) wc;
  188. int err;
  189. err = ib_copy_to_udata(udata, &offset, sizeof(offset));
  190. if (err) {
  191. ret = ERR_PTR(err);
  192. goto bail_wc;
  193. }
  194. /* Allocate info for ipath_mmap(). */
  195. ip = kmalloc(sizeof(*ip), GFP_KERNEL);
  196. if (!ip) {
  197. ret = ERR_PTR(-ENOMEM);
  198. goto bail_wc;
  199. }
  200. cq->ip = ip;
  201. ip->context = context;
  202. ip->obj = wc;
  203. kref_init(&ip->ref);
  204. ip->mmap_cnt = 0;
  205. ip->size = PAGE_ALIGN(sizeof(*wc) +
  206. sizeof(struct ib_wc) * entries);
  207. spin_lock_irq(&dev->pending_lock);
  208. ip->next = dev->pending_mmaps;
  209. dev->pending_mmaps = ip;
  210. spin_unlock_irq(&dev->pending_lock);
  211. } else
  212. cq->ip = NULL;
  213. /*
  214. * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
  215. * The number of entries should be >= the number requested or return
  216. * an error.
  217. */
  218. cq->ibcq.cqe = entries;
  219. cq->notify = IB_CQ_NONE;
  220. cq->triggered = 0;
  221. spin_lock_init(&cq->lock);
  222. tasklet_init(&cq->comptask, send_complete, (unsigned long)cq);
  223. wc->head = 0;
  224. wc->tail = 0;
  225. cq->queue = wc;
  226. ret = &cq->ibcq;
  227. dev->n_cqs_allocated++;
  228. goto done;
  229. bail_wc:
  230. vfree(wc);
  231. bail_cq:
  232. kfree(cq);
  233. done:
  234. return ret;
  235. }
  236. /**
  237. * ipath_destroy_cq - destroy a completion queue
  238. * @ibcq: the completion queue to destroy.
  239. *
  240. * Returns 0 for success.
  241. *
  242. * Called by ib_destroy_cq() in the generic verbs code.
  243. */
  244. int ipath_destroy_cq(struct ib_cq *ibcq)
  245. {
  246. struct ipath_ibdev *dev = to_idev(ibcq->device);
  247. struct ipath_cq *cq = to_icq(ibcq);
  248. tasklet_kill(&cq->comptask);
  249. dev->n_cqs_allocated--;
  250. if (cq->ip)
  251. kref_put(&cq->ip->ref, ipath_release_mmap_info);
  252. else
  253. vfree(cq->queue);
  254. kfree(cq);
  255. return 0;
  256. }
  257. /**
  258. * ipath_req_notify_cq - change the notification type for a completion queue
  259. * @ibcq: the completion queue
  260. * @notify: the type of notification to request
  261. *
  262. * Returns 0 for success.
  263. *
  264. * This may be called from interrupt context. Also called by
  265. * ib_req_notify_cq() in the generic verbs code.
  266. */
  267. int ipath_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify notify)
  268. {
  269. struct ipath_cq *cq = to_icq(ibcq);
  270. unsigned long flags;
  271. spin_lock_irqsave(&cq->lock, flags);
  272. /*
  273. * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
  274. * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
  275. */
  276. if (cq->notify != IB_CQ_NEXT_COMP)
  277. cq->notify = notify;
  278. spin_unlock_irqrestore(&cq->lock, flags);
  279. return 0;
  280. }
  281. int ipath_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
  282. {
  283. struct ipath_cq *cq = to_icq(ibcq);
  284. struct ipath_cq_wc *old_wc = cq->queue;
  285. struct ipath_cq_wc *wc;
  286. u32 head, tail, n;
  287. int ret;
  288. if (cqe < 1 || cqe > ib_ipath_max_cqes) {
  289. ret = -EINVAL;
  290. goto bail;
  291. }
  292. /*
  293. * Need to use vmalloc() if we want to support large #s of entries.
  294. */
  295. wc = vmalloc_user(sizeof(*wc) + sizeof(struct ib_wc) * cqe);
  296. if (!wc) {
  297. ret = -ENOMEM;
  298. goto bail;
  299. }
  300. /*
  301. * Return the address of the WC as the offset to mmap.
  302. * See ipath_mmap() for details.
  303. */
  304. if (udata && udata->outlen >= sizeof(__u64)) {
  305. __u64 offset = (__u64) wc;
  306. ret = ib_copy_to_udata(udata, &offset, sizeof(offset));
  307. if (ret)
  308. goto bail;
  309. }
  310. spin_lock_irq(&cq->lock);
  311. /*
  312. * Make sure head and tail are sane since they
  313. * might be user writable.
  314. */
  315. head = old_wc->head;
  316. if (head > (u32) cq->ibcq.cqe)
  317. head = (u32) cq->ibcq.cqe;
  318. tail = old_wc->tail;
  319. if (tail > (u32) cq->ibcq.cqe)
  320. tail = (u32) cq->ibcq.cqe;
  321. if (head < tail)
  322. n = cq->ibcq.cqe + 1 + head - tail;
  323. else
  324. n = head - tail;
  325. if (unlikely((u32)cqe < n)) {
  326. spin_unlock_irq(&cq->lock);
  327. vfree(wc);
  328. ret = -EOVERFLOW;
  329. goto bail;
  330. }
  331. for (n = 0; tail != head; n++) {
  332. wc->queue[n] = old_wc->queue[tail];
  333. if (tail == (u32) cq->ibcq.cqe)
  334. tail = 0;
  335. else
  336. tail++;
  337. }
  338. cq->ibcq.cqe = cqe;
  339. wc->head = n;
  340. wc->tail = 0;
  341. cq->queue = wc;
  342. spin_unlock_irq(&cq->lock);
  343. vfree(old_wc);
  344. if (cq->ip) {
  345. struct ipath_ibdev *dev = to_idev(ibcq->device);
  346. struct ipath_mmap_info *ip = cq->ip;
  347. ip->obj = wc;
  348. ip->size = PAGE_ALIGN(sizeof(*wc) +
  349. sizeof(struct ib_wc) * cqe);
  350. spin_lock_irq(&dev->pending_lock);
  351. ip->next = dev->pending_mmaps;
  352. dev->pending_mmaps = ip;
  353. spin_unlock_irq(&dev->pending_lock);
  354. }
  355. ret = 0;
  356. bail:
  357. return ret;
  358. }