]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/infiniband/sw/rdmavt/cq.c
Merge mlx5-next into rdma for-next
[linux.git] / drivers / infiniband / sw / rdmavt / cq.c
1 /*
2  * Copyright(c) 2016 - 2018 Intel Corporation.
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * BSD LICENSE
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  *
24  *  - Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  *  - Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *  - Neither the name of Intel Corporation nor the names of its
31  *    contributors may be used to endorse or promote products derived
32  *    from this software without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  */
47
48 #include <linux/slab.h>
49 #include <linux/vmalloc.h>
50 #include "cq.h"
51 #include "vt.h"
52 #include "trace.h"
53
54 static struct workqueue_struct *comp_vector_wq;
55
56 /**
57  * rvt_cq_enter - add a new entry to the completion queue
58  * @cq: completion queue
59  * @entry: work completion entry to add
60  * @solicited: true if @entry is solicited
61  *
62  * This may be called with qp->s_lock held.
63  *
64  * Return: return true on success, else return
65  * false if cq is full.
66  */
67 bool rvt_cq_enter(struct rvt_cq *cq, struct ib_wc *entry, bool solicited)
68 {
69         struct ib_uverbs_wc *uqueue = NULL;
70         struct ib_wc *kqueue = NULL;
71         struct rvt_cq_wc *u_wc = NULL;
72         struct rvt_k_cq_wc *k_wc = NULL;
73         unsigned long flags;
74         u32 head;
75         u32 next;
76         u32 tail;
77
78         spin_lock_irqsave(&cq->lock, flags);
79
80         if (cq->ip) {
81                 u_wc = cq->queue;
82                 uqueue = &u_wc->uqueue[0];
83                 head = RDMA_READ_UAPI_ATOMIC(u_wc->head);
84                 tail = RDMA_READ_UAPI_ATOMIC(u_wc->tail);
85         } else {
86                 k_wc = cq->kqueue;
87                 kqueue = &k_wc->kqueue[0];
88                 head = k_wc->head;
89                 tail = k_wc->tail;
90         }
91
92         /*
93          * Note that the head pointer might be writable by
94          * user processes.Take care to verify it is a sane value.
95          */
96         if (head >= (unsigned)cq->ibcq.cqe) {
97                 head = cq->ibcq.cqe;
98                 next = 0;
99         } else {
100                 next = head + 1;
101         }
102
103         if (unlikely(next == tail || cq->cq_full)) {
104                 struct rvt_dev_info *rdi = cq->rdi;
105
106                 if (!cq->cq_full)
107                         rvt_pr_err_ratelimited(rdi, "CQ is full!\n");
108                 cq->cq_full = true;
109                 spin_unlock_irqrestore(&cq->lock, flags);
110                 if (cq->ibcq.event_handler) {
111                         struct ib_event ev;
112
113                         ev.device = cq->ibcq.device;
114                         ev.element.cq = &cq->ibcq;
115                         ev.event = IB_EVENT_CQ_ERR;
116                         cq->ibcq.event_handler(&ev, cq->ibcq.cq_context);
117                 }
118                 return false;
119         }
120         trace_rvt_cq_enter(cq, entry, head);
121         if (uqueue) {
122                 uqueue[head].wr_id = entry->wr_id;
123                 uqueue[head].status = entry->status;
124                 uqueue[head].opcode = entry->opcode;
125                 uqueue[head].vendor_err = entry->vendor_err;
126                 uqueue[head].byte_len = entry->byte_len;
127                 uqueue[head].ex.imm_data = entry->ex.imm_data;
128                 uqueue[head].qp_num = entry->qp->qp_num;
129                 uqueue[head].src_qp = entry->src_qp;
130                 uqueue[head].wc_flags = entry->wc_flags;
131                 uqueue[head].pkey_index = entry->pkey_index;
132                 uqueue[head].slid = ib_lid_cpu16(entry->slid);
133                 uqueue[head].sl = entry->sl;
134                 uqueue[head].dlid_path_bits = entry->dlid_path_bits;
135                 uqueue[head].port_num = entry->port_num;
136                 /* Make sure entry is written before the head index. */
137                 RDMA_WRITE_UAPI_ATOMIC(u_wc->head, next);
138         } else {
139                 kqueue[head] = *entry;
140                 k_wc->head = next;
141         }
142
143         if (cq->notify == IB_CQ_NEXT_COMP ||
144             (cq->notify == IB_CQ_SOLICITED &&
145              (solicited || entry->status != IB_WC_SUCCESS))) {
146                 /*
147                  * This will cause send_complete() to be called in
148                  * another thread.
149                  */
150                 cq->notify = RVT_CQ_NONE;
151                 cq->triggered++;
152                 queue_work_on(cq->comp_vector_cpu, comp_vector_wq,
153                               &cq->comptask);
154         }
155
156         spin_unlock_irqrestore(&cq->lock, flags);
157         return true;
158 }
159 EXPORT_SYMBOL(rvt_cq_enter);
160
161 static void send_complete(struct work_struct *work)
162 {
163         struct rvt_cq *cq = container_of(work, struct rvt_cq, comptask);
164
165         /*
166          * The completion handler will most likely rearm the notification
167          * and poll for all pending entries.  If a new completion entry
168          * is added while we are in this routine, queue_work()
169          * won't call us again until we return so we check triggered to
170          * see if we need to call the handler again.
171          */
172         for (;;) {
173                 u8 triggered = cq->triggered;
174
175                 /*
176                  * IPoIB connected mode assumes the callback is from a
177                  * soft IRQ. We simulate this by blocking "bottom halves".
178                  * See the implementation for ipoib_cm_handle_tx_wc(),
179                  * netif_tx_lock_bh() and netif_tx_lock().
180                  */
181                 local_bh_disable();
182                 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
183                 local_bh_enable();
184
185                 if (cq->triggered == triggered)
186                         return;
187         }
188 }
189
190 /**
191  * rvt_create_cq - create a completion queue
192  * @ibcq: Allocated CQ
193  * @attr: creation attributes
194  * @udata: user data for libibverbs.so
195  *
196  * Called by ib_create_cq() in the generic verbs code.
197  *
198  * Return: 0 on success
199  */
200 int rvt_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
201                   struct ib_udata *udata)
202 {
203         struct ib_device *ibdev = ibcq->device;
204         struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
205         struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
206         struct rvt_cq_wc *u_wc = NULL;
207         struct rvt_k_cq_wc *k_wc = NULL;
208         u32 sz;
209         unsigned int entries = attr->cqe;
210         int comp_vector = attr->comp_vector;
211         int err;
212
213         if (attr->flags)
214                 return -EINVAL;
215
216         if (entries < 1 || entries > rdi->dparms.props.max_cqe)
217                 return -EINVAL;
218
219         if (comp_vector < 0)
220                 comp_vector = 0;
221
222         comp_vector = comp_vector % rdi->ibdev.num_comp_vectors;
223
224         /*
225          * Allocate the completion queue entries and head/tail pointers.
226          * This is allocated separately so that it can be resized and
227          * also mapped into user space.
228          * We need to use vmalloc() in order to support mmap and large
229          * numbers of entries.
230          */
231         if (udata && udata->outlen >= sizeof(__u64)) {
232                 sz = sizeof(struct ib_uverbs_wc) * (entries + 1);
233                 sz += sizeof(*u_wc);
234                 u_wc = vmalloc_user(sz);
235                 if (!u_wc)
236                         return -ENOMEM;
237         } else {
238                 sz = sizeof(struct ib_wc) * (entries + 1);
239                 sz += sizeof(*k_wc);
240                 k_wc = vzalloc_node(sz, rdi->dparms.node);
241                 if (!k_wc)
242                         return -ENOMEM;
243         }
244
245         /*
246          * Return the address of the WC as the offset to mmap.
247          * See rvt_mmap() for details.
248          */
249         if (udata && udata->outlen >= sizeof(__u64)) {
250                 int err;
251
252                 cq->ip = rvt_create_mmap_info(rdi, sz, udata, u_wc);
253                 if (!cq->ip) {
254                         err = -ENOMEM;
255                         goto bail_wc;
256                 }
257
258                 err = ib_copy_to_udata(udata, &cq->ip->offset,
259                                        sizeof(cq->ip->offset));
260                 if (err)
261                         goto bail_ip;
262         }
263
264         spin_lock_irq(&rdi->n_cqs_lock);
265         if (rdi->n_cqs_allocated == rdi->dparms.props.max_cq) {
266                 spin_unlock_irq(&rdi->n_cqs_lock);
267                 err = -ENOMEM;
268                 goto bail_ip;
269         }
270
271         rdi->n_cqs_allocated++;
272         spin_unlock_irq(&rdi->n_cqs_lock);
273
274         if (cq->ip) {
275                 spin_lock_irq(&rdi->pending_lock);
276                 list_add(&cq->ip->pending_mmaps, &rdi->pending_mmaps);
277                 spin_unlock_irq(&rdi->pending_lock);
278         }
279
280         /*
281          * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
282          * The number of entries should be >= the number requested or return
283          * an error.
284          */
285         cq->rdi = rdi;
286         if (rdi->driver_f.comp_vect_cpu_lookup)
287                 cq->comp_vector_cpu =
288                         rdi->driver_f.comp_vect_cpu_lookup(rdi, comp_vector);
289         else
290                 cq->comp_vector_cpu =
291                         cpumask_first(cpumask_of_node(rdi->dparms.node));
292
293         cq->ibcq.cqe = entries;
294         cq->notify = RVT_CQ_NONE;
295         spin_lock_init(&cq->lock);
296         INIT_WORK(&cq->comptask, send_complete);
297         if (u_wc)
298                 cq->queue = u_wc;
299         else
300                 cq->kqueue = k_wc;
301
302         trace_rvt_create_cq(cq, attr);
303         return 0;
304
305 bail_ip:
306         kfree(cq->ip);
307 bail_wc:
308         vfree(u_wc);
309         vfree(k_wc);
310         return err;
311 }
312
313 /**
314  * rvt_destroy_cq - destroy a completion queue
315  * @ibcq: the completion queue to destroy.
316  * @udata: user data or NULL for kernel object
317  *
318  * Called by ib_destroy_cq() in the generic verbs code.
319  */
320 void rvt_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata)
321 {
322         struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
323         struct rvt_dev_info *rdi = cq->rdi;
324
325         flush_work(&cq->comptask);
326         spin_lock_irq(&rdi->n_cqs_lock);
327         rdi->n_cqs_allocated--;
328         spin_unlock_irq(&rdi->n_cqs_lock);
329         if (cq->ip)
330                 kref_put(&cq->ip->ref, rvt_release_mmap_info);
331         else
332                 vfree(cq->queue);
333 }
334
335 /**
336  * rvt_req_notify_cq - change the notification type for a completion queue
337  * @ibcq: the completion queue
338  * @notify_flags: the type of notification to request
339  *
340  * This may be called from interrupt context.  Also called by
341  * ib_req_notify_cq() in the generic verbs code.
342  *
343  * Return: 0 for success.
344  */
345 int rvt_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
346 {
347         struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
348         unsigned long flags;
349         int ret = 0;
350
351         spin_lock_irqsave(&cq->lock, flags);
352         /*
353          * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
354          * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
355          */
356         if (cq->notify != IB_CQ_NEXT_COMP)
357                 cq->notify = notify_flags & IB_CQ_SOLICITED_MASK;
358
359         if (notify_flags & IB_CQ_REPORT_MISSED_EVENTS) {
360                 if (cq->queue) {
361                         if (RDMA_READ_UAPI_ATOMIC(cq->queue->head) !=
362                                 RDMA_READ_UAPI_ATOMIC(cq->queue->tail))
363                                 ret = 1;
364                 } else {
365                         if (cq->kqueue->head != cq->kqueue->tail)
366                                 ret = 1;
367                 }
368         }
369
370         spin_unlock_irqrestore(&cq->lock, flags);
371
372         return ret;
373 }
374
375 /**
376  * rvt_resize_cq - change the size of the CQ
377  * @ibcq: the completion queue
378  *
379  * Return: 0 for success.
380  */
381 int rvt_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
382 {
383         struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
384         u32 head, tail, n;
385         int ret;
386         u32 sz;
387         struct rvt_dev_info *rdi = cq->rdi;
388         struct rvt_cq_wc *u_wc = NULL;
389         struct rvt_cq_wc *old_u_wc = NULL;
390         struct rvt_k_cq_wc *k_wc = NULL;
391         struct rvt_k_cq_wc *old_k_wc = NULL;
392
393         if (cqe < 1 || cqe > rdi->dparms.props.max_cqe)
394                 return -EINVAL;
395
396         /*
397          * Need to use vmalloc() if we want to support large #s of entries.
398          */
399         if (udata && udata->outlen >= sizeof(__u64)) {
400                 sz = sizeof(struct ib_uverbs_wc) * (cqe + 1);
401                 sz += sizeof(*u_wc);
402                 u_wc = vmalloc_user(sz);
403                 if (!u_wc)
404                         return -ENOMEM;
405         } else {
406                 sz = sizeof(struct ib_wc) * (cqe + 1);
407                 sz += sizeof(*k_wc);
408                 k_wc = vzalloc_node(sz, rdi->dparms.node);
409                 if (!k_wc)
410                         return -ENOMEM;
411         }
412         /* Check that we can write the offset to mmap. */
413         if (udata && udata->outlen >= sizeof(__u64)) {
414                 __u64 offset = 0;
415
416                 ret = ib_copy_to_udata(udata, &offset, sizeof(offset));
417                 if (ret)
418                         goto bail_free;
419         }
420
421         spin_lock_irq(&cq->lock);
422         /*
423          * Make sure head and tail are sane since they
424          * might be user writable.
425          */
426         if (u_wc) {
427                 old_u_wc = cq->queue;
428                 head = RDMA_READ_UAPI_ATOMIC(old_u_wc->head);
429                 tail = RDMA_READ_UAPI_ATOMIC(old_u_wc->tail);
430         } else {
431                 old_k_wc = cq->kqueue;
432                 head = old_k_wc->head;
433                 tail = old_k_wc->tail;
434         }
435
436         if (head > (u32)cq->ibcq.cqe)
437                 head = (u32)cq->ibcq.cqe;
438         if (tail > (u32)cq->ibcq.cqe)
439                 tail = (u32)cq->ibcq.cqe;
440         if (head < tail)
441                 n = cq->ibcq.cqe + 1 + head - tail;
442         else
443                 n = head - tail;
444         if (unlikely((u32)cqe < n)) {
445                 ret = -EINVAL;
446                 goto bail_unlock;
447         }
448         for (n = 0; tail != head; n++) {
449                 if (u_wc)
450                         u_wc->uqueue[n] = old_u_wc->uqueue[tail];
451                 else
452                         k_wc->kqueue[n] = old_k_wc->kqueue[tail];
453                 if (tail == (u32)cq->ibcq.cqe)
454                         tail = 0;
455                 else
456                         tail++;
457         }
458         cq->ibcq.cqe = cqe;
459         if (u_wc) {
460                 RDMA_WRITE_UAPI_ATOMIC(u_wc->head, n);
461                 RDMA_WRITE_UAPI_ATOMIC(u_wc->tail, 0);
462                 cq->queue = u_wc;
463         } else {
464                 k_wc->head = n;
465                 k_wc->tail = 0;
466                 cq->kqueue = k_wc;
467         }
468         spin_unlock_irq(&cq->lock);
469
470         if (u_wc)
471                 vfree(old_u_wc);
472         else
473                 vfree(old_k_wc);
474
475         if (cq->ip) {
476                 struct rvt_mmap_info *ip = cq->ip;
477
478                 rvt_update_mmap_info(rdi, ip, sz, u_wc);
479
480                 /*
481                  * Return the offset to mmap.
482                  * See rvt_mmap() for details.
483                  */
484                 if (udata && udata->outlen >= sizeof(__u64)) {
485                         ret = ib_copy_to_udata(udata, &ip->offset,
486                                                sizeof(ip->offset));
487                         if (ret)
488                                 return ret;
489                 }
490
491                 spin_lock_irq(&rdi->pending_lock);
492                 if (list_empty(&ip->pending_mmaps))
493                         list_add(&ip->pending_mmaps, &rdi->pending_mmaps);
494                 spin_unlock_irq(&rdi->pending_lock);
495         }
496
497         return 0;
498
499 bail_unlock:
500         spin_unlock_irq(&cq->lock);
501 bail_free:
502         vfree(u_wc);
503         vfree(k_wc);
504
505         return ret;
506 }
507
508 /**
509  * rvt_poll_cq - poll for work completion entries
510  * @ibcq: the completion queue to poll
511  * @num_entries: the maximum number of entries to return
512  * @entry: pointer to array where work completions are placed
513  *
514  * This may be called from interrupt context.  Also called by ib_poll_cq()
515  * in the generic verbs code.
516  *
517  * Return: the number of completion entries polled.
518  */
519 int rvt_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
520 {
521         struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
522         struct rvt_k_cq_wc *wc;
523         unsigned long flags;
524         int npolled;
525         u32 tail;
526
527         /* The kernel can only poll a kernel completion queue */
528         if (cq->ip)
529                 return -EINVAL;
530
531         spin_lock_irqsave(&cq->lock, flags);
532
533         wc = cq->kqueue;
534         tail = wc->tail;
535         if (tail > (u32)cq->ibcq.cqe)
536                 tail = (u32)cq->ibcq.cqe;
537         for (npolled = 0; npolled < num_entries; ++npolled, ++entry) {
538                 if (tail == wc->head)
539                         break;
540                 /* The kernel doesn't need a RMB since it has the lock. */
541                 trace_rvt_cq_poll(cq, &wc->kqueue[tail], npolled);
542                 *entry = wc->kqueue[tail];
543                 if (tail >= cq->ibcq.cqe)
544                         tail = 0;
545                 else
546                         tail++;
547         }
548         wc->tail = tail;
549
550         spin_unlock_irqrestore(&cq->lock, flags);
551
552         return npolled;
553 }
554
555 /**
556  * rvt_driver_cq_init - Init cq resources on behalf of driver
557  * @rdi: rvt dev structure
558  *
559  * Return: 0 on success
560  */
561 int rvt_driver_cq_init(void)
562 {
563         comp_vector_wq = alloc_workqueue("%s", WQ_HIGHPRI | WQ_CPU_INTENSIVE,
564                                          0, "rdmavt_cq");
565         if (!comp_vector_wq)
566                 return -ENOMEM;
567
568         return 0;
569 }
570
571 /**
572  * rvt_cq_exit - tear down cq reources
573  * @rdi: rvt dev structure
574  */
575 void rvt_cq_exit(void)
576 {
577         destroy_workqueue(comp_vector_wq);
578         comp_vector_wq = NULL;
579 }