ipath_cq.c 12 KB

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