2 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/skbuff.h>
35 #include <linux/delay.h>
36 #include <linux/sched.h>
37 #include <linux/vmalloc.h>
41 #include "rxe_queue.h"
44 static int rxe_qp_chk_cap(struct rxe_dev *rxe, struct ib_qp_cap *cap,
47 if (cap->max_send_wr > rxe->attr.max_qp_wr) {
48 pr_warn("invalid send wr = %d > %d\n",
49 cap->max_send_wr, rxe->attr.max_qp_wr);
53 if (cap->max_send_sge > rxe->attr.max_send_sge) {
54 pr_warn("invalid send sge = %d > %d\n",
55 cap->max_send_sge, rxe->attr.max_send_sge);
60 if (cap->max_recv_wr > rxe->attr.max_qp_wr) {
61 pr_warn("invalid recv wr = %d > %d\n",
62 cap->max_recv_wr, rxe->attr.max_qp_wr);
66 if (cap->max_recv_sge > rxe->attr.max_recv_sge) {
67 pr_warn("invalid recv sge = %d > %d\n",
68 cap->max_recv_sge, rxe->attr.max_recv_sge);
73 if (cap->max_inline_data > rxe->max_inline_data) {
74 pr_warn("invalid max inline data = %d > %d\n",
75 cap->max_inline_data, rxe->max_inline_data);
85 int rxe_qp_chk_init(struct rxe_dev *rxe, struct ib_qp_init_attr *init)
87 struct ib_qp_cap *cap = &init->cap;
88 struct rxe_port *port;
89 int port_num = init->port_num;
91 if (!init->recv_cq || !init->send_cq) {
92 pr_warn("missing cq\n");
96 if (rxe_qp_chk_cap(rxe, cap, !!init->srq))
99 if (init->qp_type == IB_QPT_SMI || init->qp_type == IB_QPT_GSI) {
101 pr_warn("invalid port = %d\n", port_num);
107 if (init->qp_type == IB_QPT_SMI && port->qp_smi_index) {
108 pr_warn("SMI QP exists for port %d\n", port_num);
112 if (init->qp_type == IB_QPT_GSI && port->qp_gsi_index) {
113 pr_warn("GSI QP exists for port %d\n", port_num);
124 static int alloc_rd_atomic_resources(struct rxe_qp *qp, unsigned int n)
126 qp->resp.res_head = 0;
127 qp->resp.res_tail = 0;
128 qp->resp.resources = kcalloc(n, sizeof(struct resp_res), GFP_KERNEL);
130 if (!qp->resp.resources)
136 static void free_rd_atomic_resources(struct rxe_qp *qp)
138 if (qp->resp.resources) {
141 for (i = 0; i < qp->attr.max_dest_rd_atomic; i++) {
142 struct resp_res *res = &qp->resp.resources[i];
144 free_rd_atomic_resource(qp, res);
146 kfree(qp->resp.resources);
147 qp->resp.resources = NULL;
151 void free_rd_atomic_resource(struct rxe_qp *qp, struct resp_res *res)
153 if (res->type == RXE_ATOMIC_MASK) {
154 kfree_skb(res->atomic.skb);
155 } else if (res->type == RXE_READ_MASK) {
157 rxe_drop_ref(res->read.mr);
162 static void cleanup_rd_atomic_resources(struct rxe_qp *qp)
165 struct resp_res *res;
167 if (qp->resp.resources) {
168 for (i = 0; i < qp->attr.max_dest_rd_atomic; i++) {
169 res = &qp->resp.resources[i];
170 free_rd_atomic_resource(qp, res);
175 static void rxe_qp_init_misc(struct rxe_dev *rxe, struct rxe_qp *qp,
176 struct ib_qp_init_attr *init)
178 struct rxe_port *port;
181 qp->sq_sig_type = init->sq_sig_type;
182 qp->attr.path_mtu = 1;
183 qp->mtu = ib_mtu_enum_to_int(qp->attr.path_mtu);
185 qpn = qp->pelem.index;
188 switch (init->qp_type) {
191 port->qp_smi_index = qpn;
192 qp->attr.port_num = init->port_num;
197 port->qp_gsi_index = qpn;
198 qp->attr.port_num = init->port_num;
202 qp->ibqp.qp_num = qpn;
206 INIT_LIST_HEAD(&qp->grp_list);
208 skb_queue_head_init(&qp->send_pkts);
210 spin_lock_init(&qp->grp_lock);
211 spin_lock_init(&qp->state_lock);
213 atomic_set(&qp->ssn, 0);
214 atomic_set(&qp->skb_out, 0);
217 static int rxe_qp_init_req(struct rxe_dev *rxe, struct rxe_qp *qp,
218 struct ib_qp_init_attr *init,
219 struct ib_ucontext *context,
220 struct rxe_create_qp_resp __user *uresp)
225 err = sock_create_kern(&init_net, AF_INET, SOCK_DGRAM, 0, &qp->sk);
228 qp->sk->sk->sk_user_data = qp;
230 qp->sq.max_wr = init->cap.max_send_wr;
231 qp->sq.max_sge = init->cap.max_send_sge;
232 qp->sq.max_inline = init->cap.max_inline_data;
234 wqe_size = max_t(int, sizeof(struct rxe_send_wqe) +
235 qp->sq.max_sge * sizeof(struct ib_sge),
236 sizeof(struct rxe_send_wqe) +
239 qp->sq.queue = rxe_queue_init(rxe,
245 err = do_mmap_info(rxe, uresp ? &uresp->sq_mi : NULL, context,
246 qp->sq.queue->buf, qp->sq.queue->buf_size,
250 vfree(qp->sq.queue->buf);
256 qp->req.wqe_index = producer_index(qp->sq.queue);
257 qp->req.state = QP_STATE_RESET;
259 qp->comp.opcode = -1;
261 spin_lock_init(&qp->sq.sq_lock);
262 skb_queue_head_init(&qp->req_pkts);
264 rxe_init_task(rxe, &qp->req.task, qp,
265 rxe_requester, "req");
266 rxe_init_task(rxe, &qp->comp.task, qp,
267 rxe_completer, "comp");
269 qp->qp_timeout_jiffies = 0; /* Can't be set for UD/UC in modify_qp */
270 if (init->qp_type == IB_QPT_RC) {
271 timer_setup(&qp->rnr_nak_timer, rnr_nak_timer, 0);
272 timer_setup(&qp->retrans_timer, retransmit_timer, 0);
277 static int rxe_qp_init_resp(struct rxe_dev *rxe, struct rxe_qp *qp,
278 struct ib_qp_init_attr *init,
279 struct ib_ucontext *context,
280 struct rxe_create_qp_resp __user *uresp)
286 qp->rq.max_wr = init->cap.max_recv_wr;
287 qp->rq.max_sge = init->cap.max_recv_sge;
289 wqe_size = rcv_wqe_size(qp->rq.max_sge);
291 pr_debug("qp#%d max_wr = %d, max_sge = %d, wqe_size = %d\n",
292 qp_num(qp), qp->rq.max_wr, qp->rq.max_sge, wqe_size);
294 qp->rq.queue = rxe_queue_init(rxe,
300 err = do_mmap_info(rxe, uresp ? &uresp->rq_mi : NULL, context,
301 qp->rq.queue->buf, qp->rq.queue->buf_size,
304 vfree(qp->rq.queue->buf);
311 spin_lock_init(&qp->rq.producer_lock);
312 spin_lock_init(&qp->rq.consumer_lock);
314 skb_queue_head_init(&qp->resp_pkts);
316 rxe_init_task(rxe, &qp->resp.task, qp,
317 rxe_responder, "resp");
319 qp->resp.opcode = OPCODE_NONE;
321 qp->resp.state = QP_STATE_RESET;
326 /* called by the create qp verb */
327 int rxe_qp_from_init(struct rxe_dev *rxe, struct rxe_qp *qp, struct rxe_pd *pd,
328 struct ib_qp_init_attr *init,
329 struct rxe_create_qp_resp __user *uresp,
333 struct rxe_cq *rcq = to_rcq(init->recv_cq);
334 struct rxe_cq *scq = to_rcq(init->send_cq);
335 struct rxe_srq *srq = init->srq ? to_rsrq(init->srq) : NULL;
336 struct ib_ucontext *context = ibpd->uobject ? ibpd->uobject->context : NULL;
349 rxe_qp_init_misc(rxe, qp, init);
351 err = rxe_qp_init_req(rxe, qp, init, context, uresp);
355 err = rxe_qp_init_resp(rxe, qp, init, context, uresp);
359 qp->attr.qp_state = IB_QPS_RESET;
365 rxe_queue_cleanup(qp->sq.queue);
381 /* called by the query qp verb */
382 int rxe_qp_to_init(struct rxe_qp *qp, struct ib_qp_init_attr *init)
384 init->event_handler = qp->ibqp.event_handler;
385 init->qp_context = qp->ibqp.qp_context;
386 init->send_cq = qp->ibqp.send_cq;
387 init->recv_cq = qp->ibqp.recv_cq;
388 init->srq = qp->ibqp.srq;
390 init->cap.max_send_wr = qp->sq.max_wr;
391 init->cap.max_send_sge = qp->sq.max_sge;
392 init->cap.max_inline_data = qp->sq.max_inline;
395 init->cap.max_recv_wr = qp->rq.max_wr;
396 init->cap.max_recv_sge = qp->rq.max_sge;
399 init->sq_sig_type = qp->sq_sig_type;
401 init->qp_type = qp->ibqp.qp_type;
407 /* called by the modify qp verb, this routine checks all the parameters before
410 int rxe_qp_chk_attr(struct rxe_dev *rxe, struct rxe_qp *qp,
411 struct ib_qp_attr *attr, int mask)
413 enum ib_qp_state cur_state = (mask & IB_QP_CUR_STATE) ?
414 attr->cur_qp_state : qp->attr.qp_state;
415 enum ib_qp_state new_state = (mask & IB_QP_STATE) ?
416 attr->qp_state : cur_state;
418 if (!ib_modify_qp_is_ok(cur_state, new_state, qp_type(qp), mask,
419 IB_LINK_LAYER_ETHERNET)) {
420 pr_warn("invalid mask or state for qp\n");
424 if (mask & IB_QP_STATE) {
425 if (cur_state == IB_QPS_SQD) {
426 if (qp->req.state == QP_STATE_DRAIN &&
427 new_state != IB_QPS_ERR)
432 if (mask & IB_QP_PORT) {
433 if (attr->port_num != 1) {
434 pr_warn("invalid port %d\n", attr->port_num);
439 if (mask & IB_QP_CAP && rxe_qp_chk_cap(rxe, &attr->cap, !!qp->srq))
442 if (mask & IB_QP_AV && rxe_av_chk_attr(rxe, &attr->ah_attr))
445 if (mask & IB_QP_ALT_PATH) {
446 if (rxe_av_chk_attr(rxe, &attr->alt_ah_attr))
448 if (attr->alt_port_num != 1) {
449 pr_warn("invalid alt port %d\n", attr->alt_port_num);
452 if (attr->alt_timeout > 31) {
453 pr_warn("invalid QP alt timeout %d > 31\n",
459 if (mask & IB_QP_PATH_MTU) {
460 struct rxe_port *port = &rxe->port;
462 enum ib_mtu max_mtu = port->attr.max_mtu;
463 enum ib_mtu mtu = attr->path_mtu;
466 pr_debug("invalid mtu (%d) > (%d)\n",
467 ib_mtu_enum_to_int(mtu),
468 ib_mtu_enum_to_int(max_mtu));
473 if (mask & IB_QP_MAX_QP_RD_ATOMIC) {
474 if (attr->max_rd_atomic > rxe->attr.max_qp_rd_atom) {
475 pr_warn("invalid max_rd_atomic %d > %d\n",
477 rxe->attr.max_qp_rd_atom);
482 if (mask & IB_QP_TIMEOUT) {
483 if (attr->timeout > 31) {
484 pr_warn("invalid QP timeout %d > 31\n",
496 /* move the qp to the reset state */
497 static void rxe_qp_reset(struct rxe_qp *qp)
499 /* stop tasks from running */
500 rxe_disable_task(&qp->resp.task);
502 /* stop request/comp */
504 if (qp_type(qp) == IB_QPT_RC)
505 rxe_disable_task(&qp->comp.task);
506 rxe_disable_task(&qp->req.task);
509 /* move qp to the reset state */
510 qp->req.state = QP_STATE_RESET;
511 qp->resp.state = QP_STATE_RESET;
513 /* let state machines reset themselves drain work and packet queues
516 __rxe_do_task(&qp->resp.task);
519 __rxe_do_task(&qp->comp.task);
520 __rxe_do_task(&qp->req.task);
521 rxe_queue_reset(qp->sq.queue);
524 /* cleanup attributes */
525 atomic_set(&qp->ssn, 0);
527 qp->req.need_retry = 0;
528 qp->req.noack_pkts = 0;
530 qp->resp.opcode = -1;
531 qp->resp.drop_msg = 0;
532 qp->resp.goto_error = 0;
533 qp->resp.sent_psn_nak = 0;
536 rxe_drop_ref(qp->resp.mr);
540 cleanup_rd_atomic_resources(qp);
543 rxe_enable_task(&qp->resp.task);
546 if (qp_type(qp) == IB_QPT_RC)
547 rxe_enable_task(&qp->comp.task);
549 rxe_enable_task(&qp->req.task);
553 /* drain the send queue */
554 static void rxe_qp_drain(struct rxe_qp *qp)
557 if (qp->req.state != QP_STATE_DRAINED) {
558 qp->req.state = QP_STATE_DRAIN;
559 if (qp_type(qp) == IB_QPT_RC)
560 rxe_run_task(&qp->comp.task, 1);
562 __rxe_do_task(&qp->comp.task);
563 rxe_run_task(&qp->req.task, 1);
568 /* move the qp to the error state */
569 void rxe_qp_error(struct rxe_qp *qp)
571 qp->req.state = QP_STATE_ERROR;
572 qp->resp.state = QP_STATE_ERROR;
573 qp->attr.qp_state = IB_QPS_ERR;
575 /* drain work and packet queues */
576 rxe_run_task(&qp->resp.task, 1);
578 if (qp_type(qp) == IB_QPT_RC)
579 rxe_run_task(&qp->comp.task, 1);
581 __rxe_do_task(&qp->comp.task);
582 rxe_run_task(&qp->req.task, 1);
585 /* called by the modify qp verb */
586 int rxe_qp_from_attr(struct rxe_qp *qp, struct ib_qp_attr *attr, int mask,
587 struct ib_udata *udata)
591 if (mask & IB_QP_MAX_QP_RD_ATOMIC) {
592 int max_rd_atomic = attr->max_rd_atomic ?
593 roundup_pow_of_two(attr->max_rd_atomic) : 0;
595 qp->attr.max_rd_atomic = max_rd_atomic;
596 atomic_set(&qp->req.rd_atomic, max_rd_atomic);
599 if (mask & IB_QP_MAX_DEST_RD_ATOMIC) {
600 int max_dest_rd_atomic = attr->max_dest_rd_atomic ?
601 roundup_pow_of_two(attr->max_dest_rd_atomic) : 0;
603 qp->attr.max_dest_rd_atomic = max_dest_rd_atomic;
605 free_rd_atomic_resources(qp);
607 err = alloc_rd_atomic_resources(qp, max_dest_rd_atomic);
612 if (mask & IB_QP_CUR_STATE)
613 qp->attr.cur_qp_state = attr->qp_state;
615 if (mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
616 qp->attr.en_sqd_async_notify = attr->en_sqd_async_notify;
618 if (mask & IB_QP_ACCESS_FLAGS)
619 qp->attr.qp_access_flags = attr->qp_access_flags;
621 if (mask & IB_QP_PKEY_INDEX)
622 qp->attr.pkey_index = attr->pkey_index;
624 if (mask & IB_QP_PORT)
625 qp->attr.port_num = attr->port_num;
627 if (mask & IB_QP_QKEY)
628 qp->attr.qkey = attr->qkey;
630 if (mask & IB_QP_AV) {
631 rxe_av_from_attr(attr->port_num, &qp->pri_av, &attr->ah_attr);
632 rxe_av_fill_ip_info(&qp->pri_av, &attr->ah_attr);
635 if (mask & IB_QP_ALT_PATH) {
636 rxe_av_from_attr(attr->alt_port_num, &qp->alt_av,
638 rxe_av_fill_ip_info(&qp->alt_av, &attr->alt_ah_attr);
639 qp->attr.alt_port_num = attr->alt_port_num;
640 qp->attr.alt_pkey_index = attr->alt_pkey_index;
641 qp->attr.alt_timeout = attr->alt_timeout;
644 if (mask & IB_QP_PATH_MTU) {
645 qp->attr.path_mtu = attr->path_mtu;
646 qp->mtu = ib_mtu_enum_to_int(attr->path_mtu);
649 if (mask & IB_QP_TIMEOUT) {
650 qp->attr.timeout = attr->timeout;
651 if (attr->timeout == 0) {
652 qp->qp_timeout_jiffies = 0;
654 /* According to the spec, timeout = 4.096 * 2 ^ attr->timeout [us] */
655 int j = nsecs_to_jiffies(4096ULL << attr->timeout);
657 qp->qp_timeout_jiffies = j ? j : 1;
661 if (mask & IB_QP_RETRY_CNT) {
662 qp->attr.retry_cnt = attr->retry_cnt;
663 qp->comp.retry_cnt = attr->retry_cnt;
664 pr_debug("qp#%d set retry count = %d\n", qp_num(qp),
668 if (mask & IB_QP_RNR_RETRY) {
669 qp->attr.rnr_retry = attr->rnr_retry;
670 qp->comp.rnr_retry = attr->rnr_retry;
671 pr_debug("qp#%d set rnr retry count = %d\n", qp_num(qp),
675 if (mask & IB_QP_RQ_PSN) {
676 qp->attr.rq_psn = (attr->rq_psn & BTH_PSN_MASK);
677 qp->resp.psn = qp->attr.rq_psn;
678 pr_debug("qp#%d set resp psn = 0x%x\n", qp_num(qp),
682 if (mask & IB_QP_MIN_RNR_TIMER) {
683 qp->attr.min_rnr_timer = attr->min_rnr_timer;
684 pr_debug("qp#%d set min rnr timer = 0x%x\n", qp_num(qp),
685 attr->min_rnr_timer);
688 if (mask & IB_QP_SQ_PSN) {
689 qp->attr.sq_psn = (attr->sq_psn & BTH_PSN_MASK);
690 qp->req.psn = qp->attr.sq_psn;
691 qp->comp.psn = qp->attr.sq_psn;
692 pr_debug("qp#%d set req psn = 0x%x\n", qp_num(qp), qp->req.psn);
695 if (mask & IB_QP_PATH_MIG_STATE)
696 qp->attr.path_mig_state = attr->path_mig_state;
698 if (mask & IB_QP_DEST_QPN)
699 qp->attr.dest_qp_num = attr->dest_qp_num;
701 if (mask & IB_QP_STATE) {
702 qp->attr.qp_state = attr->qp_state;
704 switch (attr->qp_state) {
706 pr_debug("qp#%d state -> RESET\n", qp_num(qp));
711 pr_debug("qp#%d state -> INIT\n", qp_num(qp));
712 qp->req.state = QP_STATE_INIT;
713 qp->resp.state = QP_STATE_INIT;
717 pr_debug("qp#%d state -> RTR\n", qp_num(qp));
718 qp->resp.state = QP_STATE_READY;
722 pr_debug("qp#%d state -> RTS\n", qp_num(qp));
723 qp->req.state = QP_STATE_READY;
727 pr_debug("qp#%d state -> SQD\n", qp_num(qp));
732 pr_warn("qp#%d state -> SQE !!?\n", qp_num(qp));
733 /* Not possible from modify_qp. */
737 pr_debug("qp#%d state -> ERR\n", qp_num(qp));
746 /* called by the query qp verb */
747 int rxe_qp_to_attr(struct rxe_qp *qp, struct ib_qp_attr *attr, int mask)
751 attr->rq_psn = qp->resp.psn;
752 attr->sq_psn = qp->req.psn;
754 attr->cap.max_send_wr = qp->sq.max_wr;
755 attr->cap.max_send_sge = qp->sq.max_sge;
756 attr->cap.max_inline_data = qp->sq.max_inline;
759 attr->cap.max_recv_wr = qp->rq.max_wr;
760 attr->cap.max_recv_sge = qp->rq.max_sge;
763 rxe_av_to_attr(&qp->pri_av, &attr->ah_attr);
764 rxe_av_to_attr(&qp->alt_av, &attr->alt_ah_attr);
766 if (qp->req.state == QP_STATE_DRAIN) {
767 attr->sq_draining = 1;
768 /* applications that get this state
769 * typically spin on it. yield the
774 attr->sq_draining = 0;
777 pr_debug("attr->sq_draining = %d\n", attr->sq_draining);
782 /* called by the destroy qp verb */
783 void rxe_qp_destroy(struct rxe_qp *qp)
786 qp->qp_timeout_jiffies = 0;
787 rxe_cleanup_task(&qp->resp.task);
789 if (qp_type(qp) == IB_QPT_RC) {
790 del_timer_sync(&qp->retrans_timer);
791 del_timer_sync(&qp->rnr_nak_timer);
794 rxe_cleanup_task(&qp->req.task);
795 rxe_cleanup_task(&qp->comp.task);
797 /* flush out any receive wr's or pending requests */
798 __rxe_do_task(&qp->req.task);
800 __rxe_do_task(&qp->comp.task);
801 __rxe_do_task(&qp->req.task);
805 /* called when the last reference to the qp is dropped */
806 static void rxe_qp_do_cleanup(struct work_struct *work)
808 struct rxe_qp *qp = container_of(work, typeof(*qp), cleanup_work.work);
810 rxe_drop_all_mcast_groups(qp);
813 rxe_queue_cleanup(qp->sq.queue);
816 rxe_drop_ref(qp->srq);
819 rxe_queue_cleanup(qp->rq.queue);
822 rxe_drop_ref(qp->scq);
824 rxe_drop_ref(qp->rcq);
826 rxe_drop_ref(qp->pd);
829 rxe_drop_ref(qp->resp.mr);
833 if (qp_type(qp) == IB_QPT_RC)
834 sk_dst_reset(qp->sk->sk);
836 free_rd_atomic_resources(qp);
838 kernel_sock_shutdown(qp->sk, SHUT_RDWR);
839 sock_release(qp->sk);
842 /* called when the last reference to the qp is dropped */
843 void rxe_qp_cleanup(struct rxe_pool_entry *arg)
845 struct rxe_qp *qp = container_of(arg, typeof(*qp), pelem);
847 execute_in_process_context(rxe_qp_do_cleanup, &qp->cleanup_work);