ipath_cq.c 12 KB

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