ipath_cq.c 10 KB

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