1 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
3 /* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
5 /* Greg Joyce <greg@opengridcomputing.com> */
6 /* Copyright (c) 2008-2019, IBM Corporation */
7 /* Copyright (c) 2017, Open Grid Computing, Inc. */
9 #include <linux/errno.h>
10 #include <linux/types.h>
11 #include <linux/net.h>
12 #include <linux/inetdevice.h>
13 #include <net/addrconf.h>
14 #include <linux/workqueue.h>
17 #include <linux/inet.h>
18 #include <linux/tcp.h>
20 #include <rdma/iw_cm.h>
21 #include <rdma/ib_verbs.h>
22 #include <rdma/ib_user_verbs.h>
28 * Set to any combination of
29 * MPA_V2_RDMA_NO_RTR, MPA_V2_RDMA_READ_RTR, MPA_V2_RDMA_WRITE_RTR
31 static __be16 rtr_type
= MPA_V2_RDMA_READ_RTR
| MPA_V2_RDMA_WRITE_RTR
;
32 static const bool relaxed_ird_negotiation
= true;
34 static void siw_cm_llp_state_change(struct sock
*s
);
35 static void siw_cm_llp_data_ready(struct sock
*s
);
36 static void siw_cm_llp_write_space(struct sock
*s
);
37 static void siw_cm_llp_error_report(struct sock
*s
);
38 static int siw_cm_upcall(struct siw_cep
*cep
, enum iw_cm_event_type reason
,
41 static void siw_sk_assign_cm_upcalls(struct sock
*sk
)
43 write_lock_bh(&sk
->sk_callback_lock
);
44 sk
->sk_state_change
= siw_cm_llp_state_change
;
45 sk
->sk_data_ready
= siw_cm_llp_data_ready
;
46 sk
->sk_write_space
= siw_cm_llp_write_space
;
47 sk
->sk_error_report
= siw_cm_llp_error_report
;
48 write_unlock_bh(&sk
->sk_callback_lock
);
51 static void siw_sk_save_upcalls(struct sock
*sk
)
53 struct siw_cep
*cep
= sk_to_cep(sk
);
55 write_lock_bh(&sk
->sk_callback_lock
);
56 cep
->sk_state_change
= sk
->sk_state_change
;
57 cep
->sk_data_ready
= sk
->sk_data_ready
;
58 cep
->sk_write_space
= sk
->sk_write_space
;
59 cep
->sk_error_report
= sk
->sk_error_report
;
60 write_unlock_bh(&sk
->sk_callback_lock
);
63 static void siw_sk_restore_upcalls(struct sock
*sk
, struct siw_cep
*cep
)
65 sk
->sk_state_change
= cep
->sk_state_change
;
66 sk
->sk_data_ready
= cep
->sk_data_ready
;
67 sk
->sk_write_space
= cep
->sk_write_space
;
68 sk
->sk_error_report
= cep
->sk_error_report
;
69 sk
->sk_user_data
= NULL
;
72 static void siw_qp_socket_assoc(struct siw_cep
*cep
, struct siw_qp
*qp
)
74 struct socket
*s
= cep
->sock
;
75 struct sock
*sk
= s
->sk
;
77 write_lock_bh(&sk
->sk_callback_lock
);
80 sk
->sk_data_ready
= siw_qp_llp_data_ready
;
81 sk
->sk_write_space
= siw_qp_llp_write_space
;
83 write_unlock_bh(&sk
->sk_callback_lock
);
86 static void siw_socket_disassoc(struct socket
*s
)
88 struct sock
*sk
= s
->sk
;
92 write_lock_bh(&sk
->sk_callback_lock
);
95 siw_sk_restore_upcalls(sk
, cep
);
98 pr_warn("siw: cannot restore sk callbacks: no ep\n");
100 write_unlock_bh(&sk
->sk_callback_lock
);
102 pr_warn("siw: cannot restore sk callbacks: no sk\n");
106 static void siw_rtr_data_ready(struct sock
*sk
)
109 struct siw_qp
*qp
= NULL
;
110 read_descriptor_t rd_desc
;
112 read_lock(&sk
->sk_callback_lock
);
116 WARN(1, "No connection endpoint\n");
121 memset(&rd_desc
, 0, sizeof(rd_desc
));
122 rd_desc
.arg
.data
= qp
;
125 tcp_read_sock(sk
, &rd_desc
, siw_tcp_rx_data
);
127 * Check if first frame was successfully processed.
128 * Signal connection full establishment if yes.
129 * Failed data processing would have already scheduled
132 if (!qp
->rx_stream
.rx_suspend
)
133 siw_cm_upcall(cep
, IW_CM_EVENT_ESTABLISHED
, 0);
135 read_unlock(&sk
->sk_callback_lock
);
137 siw_qp_socket_assoc(cep
, qp
);
140 static void siw_sk_assign_rtr_upcalls(struct siw_cep
*cep
)
142 struct sock
*sk
= cep
->sock
->sk
;
144 write_lock_bh(&sk
->sk_callback_lock
);
145 sk
->sk_data_ready
= siw_rtr_data_ready
;
146 sk
->sk_write_space
= siw_qp_llp_write_space
;
147 write_unlock_bh(&sk
->sk_callback_lock
);
150 static void siw_cep_socket_assoc(struct siw_cep
*cep
, struct socket
*s
)
154 s
->sk
->sk_user_data
= cep
;
156 siw_sk_save_upcalls(s
->sk
);
157 siw_sk_assign_cm_upcalls(s
->sk
);
160 static struct siw_cep
*siw_cep_alloc(struct siw_device
*sdev
)
162 struct siw_cep
*cep
= kzalloc(sizeof(*cep
), GFP_KERNEL
);
168 INIT_LIST_HEAD(&cep
->listenq
);
169 INIT_LIST_HEAD(&cep
->devq
);
170 INIT_LIST_HEAD(&cep
->work_freelist
);
172 kref_init(&cep
->ref
);
173 cep
->state
= SIW_EPSTATE_IDLE
;
174 init_waitqueue_head(&cep
->waitq
);
175 spin_lock_init(&cep
->lock
);
177 cep
->enhanced_rdma_conn_est
= false;
179 spin_lock_irqsave(&sdev
->lock
, flags
);
180 list_add_tail(&cep
->devq
, &sdev
->cep_list
);
181 spin_unlock_irqrestore(&sdev
->lock
, flags
);
183 siw_dbg_cep(cep
, "new endpoint\n");
187 static void siw_cm_free_work(struct siw_cep
*cep
)
189 struct list_head
*w
, *tmp
;
190 struct siw_cm_work
*work
;
192 list_for_each_safe(w
, tmp
, &cep
->work_freelist
) {
193 work
= list_entry(w
, struct siw_cm_work
, list
);
194 list_del(&work
->list
);
199 static void siw_cancel_mpatimer(struct siw_cep
*cep
)
201 spin_lock_bh(&cep
->lock
);
202 if (cep
->mpa_timer
) {
203 if (cancel_delayed_work(&cep
->mpa_timer
->work
)) {
205 kfree(cep
->mpa_timer
); /* not needed again */
207 cep
->mpa_timer
= NULL
;
209 spin_unlock_bh(&cep
->lock
);
212 static void siw_put_work(struct siw_cm_work
*work
)
214 INIT_LIST_HEAD(&work
->list
);
215 spin_lock_bh(&work
->cep
->lock
);
216 list_add(&work
->list
, &work
->cep
->work_freelist
);
217 spin_unlock_bh(&work
->cep
->lock
);
220 static void siw_cep_set_inuse(struct siw_cep
*cep
)
224 spin_lock_irqsave(&cep
->lock
, flags
);
227 spin_unlock_irqrestore(&cep
->lock
, flags
);
228 wait_event_interruptible(cep
->waitq
, !cep
->in_use
);
229 if (signal_pending(current
))
230 flush_signals(current
);
234 spin_unlock_irqrestore(&cep
->lock
, flags
);
238 static void siw_cep_set_free(struct siw_cep
*cep
)
242 spin_lock_irqsave(&cep
->lock
, flags
);
244 spin_unlock_irqrestore(&cep
->lock
, flags
);
246 wake_up(&cep
->waitq
);
249 static void __siw_cep_dealloc(struct kref
*ref
)
251 struct siw_cep
*cep
= container_of(ref
, struct siw_cep
, ref
);
252 struct siw_device
*sdev
= cep
->sdev
;
255 WARN_ON(cep
->listen_cep
);
257 /* kfree(NULL) is safe */
258 kfree(cep
->mpa
.pdata
);
259 spin_lock_bh(&cep
->lock
);
260 if (!list_empty(&cep
->work_freelist
))
261 siw_cm_free_work(cep
);
262 spin_unlock_bh(&cep
->lock
);
264 spin_lock_irqsave(&sdev
->lock
, flags
);
265 list_del(&cep
->devq
);
266 spin_unlock_irqrestore(&sdev
->lock
, flags
);
268 siw_dbg_cep(cep
, "free endpoint\n");
272 static struct siw_cm_work
*siw_get_work(struct siw_cep
*cep
)
274 struct siw_cm_work
*work
= NULL
;
276 spin_lock_bh(&cep
->lock
);
277 if (!list_empty(&cep
->work_freelist
)) {
278 work
= list_entry(cep
->work_freelist
.next
, struct siw_cm_work
,
280 list_del_init(&work
->list
);
282 spin_unlock_bh(&cep
->lock
);
286 static int siw_cm_alloc_work(struct siw_cep
*cep
, int num
)
288 struct siw_cm_work
*work
;
291 work
= kmalloc(sizeof(*work
), GFP_KERNEL
);
293 if (!(list_empty(&cep
->work_freelist
)))
294 siw_cm_free_work(cep
);
298 INIT_LIST_HEAD(&work
->list
);
299 list_add(&work
->list
, &cep
->work_freelist
);
307 * Upcall to IWCM to inform about async connection events
309 static int siw_cm_upcall(struct siw_cep
*cep
, enum iw_cm_event_type reason
,
312 struct iw_cm_event event
;
315 memset(&event
, 0, sizeof(event
));
316 event
.status
= status
;
317 event
.event
= reason
;
319 if (reason
== IW_CM_EVENT_CONNECT_REQUEST
) {
320 event
.provider_data
= cep
;
321 id
= cep
->listen_cep
->cm_id
;
325 /* Signal IRD and ORD */
326 if (reason
== IW_CM_EVENT_ESTABLISHED
||
327 reason
== IW_CM_EVENT_CONNECT_REPLY
) {
328 /* Signal negotiated IRD/ORD values we will use */
329 event
.ird
= cep
->ird
;
330 event
.ord
= cep
->ord
;
331 } else if (reason
== IW_CM_EVENT_CONNECT_REQUEST
) {
332 event
.ird
= cep
->ord
;
333 event
.ord
= cep
->ird
;
335 /* Signal private data and address information */
336 if (reason
== IW_CM_EVENT_CONNECT_REQUEST
||
337 reason
== IW_CM_EVENT_CONNECT_REPLY
) {
338 u16 pd_len
= be16_to_cpu(cep
->mpa
.hdr
.params
.pd_len
);
342 * hand over MPA private data
344 event
.private_data_len
= pd_len
;
345 event
.private_data
= cep
->mpa
.pdata
;
347 /* Hide MPA V2 IRD/ORD control */
348 if (cep
->enhanced_rdma_conn_est
) {
349 event
.private_data_len
-=
350 sizeof(struct mpa_v2_data
);
351 event
.private_data
+=
352 sizeof(struct mpa_v2_data
);
355 getname_local(cep
->sock
, &event
.local_addr
);
356 getname_peer(cep
->sock
, &event
.remote_addr
);
358 siw_dbg_cep(cep
, "[QP %u]: reason=%d, status=%d\n",
359 cep
->qp
? qp_id(cep
->qp
) : UINT_MAX
, reason
, status
);
361 return id
->event_handler(id
, &event
);
367 * Drops established LLP connection if present and not already
368 * scheduled for dropping. Called from user context, SQ workqueue
369 * or receive IRQ. Caller signals if socket can be immediately
370 * closed (basically, if not in IRQ).
372 void siw_qp_cm_drop(struct siw_qp
*qp
, int schedule
)
374 struct siw_cep
*cep
= qp
->cep
;
376 qp
->rx_stream
.rx_suspend
= 1;
377 qp
->tx_ctx
.tx_suspend
= 1;
383 siw_cm_queue_work(cep
, SIW_CM_WORK_CLOSE_LLP
);
385 siw_cep_set_inuse(cep
);
387 if (cep
->state
== SIW_EPSTATE_CLOSED
) {
388 siw_dbg_cep(cep
, "already closed\n");
391 siw_dbg_cep(cep
, "immediate close, state %d\n", cep
->state
);
393 if (qp
->term_info
.valid
)
394 siw_send_terminate(qp
);
397 switch (cep
->state
) {
398 case SIW_EPSTATE_AWAIT_MPAREP
:
399 siw_cm_upcall(cep
, IW_CM_EVENT_CONNECT_REPLY
,
403 case SIW_EPSTATE_RDMA_MODE
:
404 siw_cm_upcall(cep
, IW_CM_EVENT_CLOSE
, 0);
407 case SIW_EPSTATE_IDLE
:
408 case SIW_EPSTATE_LISTENING
:
409 case SIW_EPSTATE_CONNECTING
:
410 case SIW_EPSTATE_AWAIT_MPAREQ
:
411 case SIW_EPSTATE_RECVD_MPAREQ
:
412 case SIW_EPSTATE_CLOSED
:
416 cep
->cm_id
->rem_ref(cep
->cm_id
);
420 cep
->state
= SIW_EPSTATE_CLOSED
;
423 siw_socket_disassoc(cep
->sock
);
425 * Immediately close socket
427 sock_release(cep
->sock
);
435 siw_cep_set_free(cep
);
439 void siw_cep_put(struct siw_cep
*cep
)
441 WARN_ON(kref_read(&cep
->ref
) < 1);
442 kref_put(&cep
->ref
, __siw_cep_dealloc
);
445 void siw_cep_get(struct siw_cep
*cep
)
451 * Expects params->pd_len in host byte order
453 static int siw_send_mpareqrep(struct siw_cep
*cep
, const void *pdata
, u8 pd_len
)
455 struct socket
*s
= cep
->sock
;
456 struct mpa_rr
*rr
= &cep
->mpa
.hdr
;
463 memset(&msg
, 0, sizeof(msg
));
465 iov
[iovec_num
].iov_base
= rr
;
466 iov
[iovec_num
].iov_len
= sizeof(*rr
);
467 mpa_len
= sizeof(*rr
);
469 if (cep
->enhanced_rdma_conn_est
) {
471 iov
[iovec_num
].iov_base
= &cep
->mpa
.v2_ctrl
;
472 iov
[iovec_num
].iov_len
= sizeof(cep
->mpa
.v2_ctrl
);
473 mpa_len
+= sizeof(cep
->mpa
.v2_ctrl
);
477 iov
[iovec_num
].iov_base
= (char *)pdata
;
478 iov
[iovec_num
].iov_len
= pd_len
;
481 if (cep
->enhanced_rdma_conn_est
)
482 pd_len
+= sizeof(cep
->mpa
.v2_ctrl
);
484 rr
->params
.pd_len
= cpu_to_be16(pd_len
);
486 rv
= kernel_sendmsg(s
, &msg
, iov
, iovec_num
+ 1, mpa_len
);
488 return rv
< 0 ? rv
: 0;
492 * Receive MPA Request/Reply header.
494 * Returns 0 if complete MPA Request/Reply header including
495 * eventual private data was received. Returns -EAGAIN if
496 * header was partially received or negative error code otherwise.
498 * Context: May be called in process context only
500 static int siw_recv_mpa_rr(struct siw_cep
*cep
)
502 struct mpa_rr
*hdr
= &cep
->mpa
.hdr
;
503 struct socket
*s
= cep
->sock
;
507 if (cep
->mpa
.bytes_rcvd
< sizeof(struct mpa_rr
)) {
508 rcvd
= ksock_recv(s
, (char *)hdr
+ cep
->mpa
.bytes_rcvd
,
509 sizeof(struct mpa_rr
) - cep
->mpa
.bytes_rcvd
,
512 return -ECONNABORTED
;
514 cep
->mpa
.bytes_rcvd
+= rcvd
;
516 if (cep
->mpa
.bytes_rcvd
< sizeof(struct mpa_rr
))
519 if (be16_to_cpu(hdr
->params
.pd_len
) > MPA_MAX_PRIVDATA
)
522 pd_len
= be16_to_cpu(hdr
->params
.pd_len
);
525 * At least the MPA Request/Reply header (frame not including
526 * private data) has been received.
527 * Receive (or continue receiving) any private data.
529 to_rcv
= pd_len
- (cep
->mpa
.bytes_rcvd
- sizeof(struct mpa_rr
));
533 * We must have hdr->params.pd_len == 0 and thus received a
534 * complete MPA Request/Reply frame.
535 * Check against peer protocol violation.
539 rcvd
= ksock_recv(s
, (char *)&word
, sizeof(word
), MSG_DONTWAIT
);
544 siw_dbg_cep(cep
, "peer EOF\n");
548 siw_dbg_cep(cep
, "error: %d\n", rcvd
);
551 siw_dbg_cep(cep
, "peer sent extra data: %d\n", rcvd
);
557 * At this point, we must have hdr->params.pd_len != 0.
558 * A private data buffer gets allocated if hdr->params.pd_len != 0.
560 if (!cep
->mpa
.pdata
) {
561 cep
->mpa
.pdata
= kmalloc(pd_len
+ 4, GFP_KERNEL
);
566 s
, cep
->mpa
.pdata
+ cep
->mpa
.bytes_rcvd
- sizeof(struct mpa_rr
),
567 to_rcv
+ 4, MSG_DONTWAIT
);
575 cep
->mpa
.bytes_rcvd
+= rcvd
;
577 if (to_rcv
== rcvd
) {
578 siw_dbg_cep(cep
, "%d bytes private data received\n", pd_len
);
587 * Read MPA Request from socket and signal new connection to IWCM
588 * if success. Caller must hold lock on corresponding listening CEP.
590 static int siw_proc_mpareq(struct siw_cep
*cep
)
596 rv
= siw_recv_mpa_rr(cep
);
602 version
= __mpa_rr_revision(req
->params
.bits
);
603 pd_len
= be16_to_cpu(req
->params
.pd_len
);
605 if (version
> MPA_REVISION_2
)
606 /* allow for 0, 1, and 2 only */
609 if (memcmp(req
->key
, MPA_KEY_REQ
, 16))
612 /* Prepare for sending MPA reply */
613 memcpy(req
->key
, MPA_KEY_REP
, 16);
615 if (version
== MPA_REVISION_2
&&
616 (req
->params
.bits
& MPA_RR_FLAG_ENHANCED
)) {
618 * MPA version 2 must signal IRD/ORD values and P2P mode
619 * in private data if header flag MPA_RR_FLAG_ENHANCED
622 if (pd_len
< sizeof(struct mpa_v2_data
))
625 cep
->enhanced_rdma_conn_est
= true;
628 /* MPA Markers: currently not supported. Marker TX to be added. */
629 if (req
->params
.bits
& MPA_RR_FLAG_MARKERS
)
632 if (req
->params
.bits
& MPA_RR_FLAG_CRC
) {
634 * RFC 5044, page 27: CRC MUST be used if peer requests it.
635 * siw specific: 'mpa_crc_strict' parameter to reject
636 * connection with CRC if local CRC off enforced by
637 * 'mpa_crc_strict' module parameter.
639 if (!mpa_crc_required
&& mpa_crc_strict
)
642 /* Enable CRC if requested by module parameter */
643 if (mpa_crc_required
)
644 req
->params
.bits
|= MPA_RR_FLAG_CRC
;
646 if (cep
->enhanced_rdma_conn_est
) {
647 struct mpa_v2_data
*v2
= (struct mpa_v2_data
*)cep
->mpa
.pdata
;
650 * Peer requested ORD becomes requested local IRD,
651 * peer requested IRD becomes requested local ORD.
652 * IRD and ORD get limited by global maximum values.
654 cep
->ord
= ntohs(v2
->ird
) & MPA_IRD_ORD_MASK
;
655 cep
->ord
= min(cep
->ord
, SIW_MAX_ORD_QP
);
656 cep
->ird
= ntohs(v2
->ord
) & MPA_IRD_ORD_MASK
;
657 cep
->ird
= min(cep
->ird
, SIW_MAX_IRD_QP
);
659 /* May get overwritten by locally negotiated values */
660 cep
->mpa
.v2_ctrl
.ird
= htons(cep
->ird
);
661 cep
->mpa
.v2_ctrl
.ord
= htons(cep
->ord
);
664 * Support for peer sent zero length Write or Read to
665 * let local side enter RTS. Writes are preferred.
666 * Sends would require pre-posting a Receive and are
668 * Propose zero length Write if none of Read and Write
671 if (v2
->ird
& MPA_V2_PEER_TO_PEER
) {
672 cep
->mpa
.v2_ctrl
.ird
|= MPA_V2_PEER_TO_PEER
;
674 if (v2
->ord
& MPA_V2_RDMA_WRITE_RTR
)
675 cep
->mpa
.v2_ctrl
.ord
|= MPA_V2_RDMA_WRITE_RTR
;
676 else if (v2
->ord
& MPA_V2_RDMA_READ_RTR
)
677 cep
->mpa
.v2_ctrl
.ord
|= MPA_V2_RDMA_READ_RTR
;
679 cep
->mpa
.v2_ctrl
.ord
|= MPA_V2_RDMA_WRITE_RTR
;
683 cep
->state
= SIW_EPSTATE_RECVD_MPAREQ
;
685 /* Keep reference until IWCM accepts/rejects */
687 rv
= siw_cm_upcall(cep
, IW_CM_EVENT_CONNECT_REQUEST
, 0);
694 siw_dbg_cep(cep
, "reject: crc %d:%d:%d, m %d:%d\n",
695 req
->params
.bits
& MPA_RR_FLAG_CRC
? 1 : 0,
696 mpa_crc_required
, mpa_crc_strict
,
697 req
->params
.bits
& MPA_RR_FLAG_MARKERS
? 1 : 0, 0);
699 req
->params
.bits
&= ~MPA_RR_FLAG_MARKERS
;
700 req
->params
.bits
|= MPA_RR_FLAG_REJECT
;
702 if (!mpa_crc_required
&& mpa_crc_strict
)
703 req
->params
.bits
&= ~MPA_RR_FLAG_CRC
;
706 kfree(cep
->mpa
.pdata
);
708 cep
->mpa
.pdata
= NULL
;
710 siw_send_mpareqrep(cep
, NULL
, 0);
715 static int siw_proc_mpareply(struct siw_cep
*cep
)
717 struct siw_qp_attrs qp_attrs
;
718 enum siw_qp_attr_mask qp_attr_mask
;
719 struct siw_qp
*qp
= cep
->qp
;
724 bool ird_insufficient
= false;
725 enum mpa_v2_ctrl mpa_p2p_mode
= MPA_V2_RDMA_NO_RTR
;
727 rv
= siw_recv_mpa_rr(cep
);
729 siw_cancel_mpatimer(cep
);
735 if (__mpa_rr_revision(rep
->params
.bits
) > MPA_REVISION_2
) {
736 /* allow for 0, 1, and 2 only */
740 if (memcmp(rep
->key
, MPA_KEY_REP
, 16)) {
741 siw_init_terminate(qp
, TERM_ERROR_LAYER_LLP
, LLP_ETYPE_MPA
,
742 LLP_ECODE_INVALID_REQ_RESP
, 0);
743 siw_send_terminate(qp
);
747 if (rep
->params
.bits
& MPA_RR_FLAG_REJECT
) {
748 siw_dbg_cep(cep
, "got mpa reject\n");
749 siw_cm_upcall(cep
, IW_CM_EVENT_CONNECT_REPLY
, -ECONNRESET
);
753 if (try_gso
&& rep
->params
.bits
& MPA_RR_FLAG_GSO_EXP
) {
754 siw_dbg_cep(cep
, "peer allows GSO on TX\n");
755 qp
->tx_ctx
.gso_seg_limit
= 0;
757 if ((rep
->params
.bits
& MPA_RR_FLAG_MARKERS
) ||
758 (mpa_crc_required
&& !(rep
->params
.bits
& MPA_RR_FLAG_CRC
)) ||
759 (mpa_crc_strict
&& !mpa_crc_required
&&
760 (rep
->params
.bits
& MPA_RR_FLAG_CRC
))) {
761 siw_dbg_cep(cep
, "reply unsupp: crc %d:%d:%d, m %d:%d\n",
762 rep
->params
.bits
& MPA_RR_FLAG_CRC
? 1 : 0,
763 mpa_crc_required
, mpa_crc_strict
,
764 rep
->params
.bits
& MPA_RR_FLAG_MARKERS
? 1 : 0, 0);
766 siw_cm_upcall(cep
, IW_CM_EVENT_CONNECT_REPLY
, -ECONNREFUSED
);
770 if (cep
->enhanced_rdma_conn_est
) {
771 struct mpa_v2_data
*v2
;
773 if (__mpa_rr_revision(rep
->params
.bits
) < MPA_REVISION_2
||
774 !(rep
->params
.bits
& MPA_RR_FLAG_ENHANCED
)) {
776 * Protocol failure: The responder MUST reply with
777 * MPA version 2 and MUST set MPA_RR_FLAG_ENHANCED.
779 siw_dbg_cep(cep
, "mpa reply error: vers %d, enhcd %d\n",
780 __mpa_rr_revision(rep
->params
.bits
),
781 rep
->params
.bits
& MPA_RR_FLAG_ENHANCED
?
785 siw_cm_upcall(cep
, IW_CM_EVENT_CONNECT_REPLY
,
789 v2
= (struct mpa_v2_data
*)cep
->mpa
.pdata
;
790 rep_ird
= ntohs(v2
->ird
) & MPA_IRD_ORD_MASK
;
791 rep_ord
= ntohs(v2
->ord
) & MPA_IRD_ORD_MASK
;
793 if (cep
->ird
< rep_ord
&&
794 (relaxed_ird_negotiation
== false ||
795 rep_ord
> cep
->sdev
->attrs
.max_ird
)) {
796 siw_dbg_cep(cep
, "ird %d, rep_ord %d, max_ord %d\n",
798 cep
->sdev
->attrs
.max_ord
);
799 ird_insufficient
= true;
801 if (cep
->ord
> rep_ird
&& relaxed_ird_negotiation
== false) {
802 siw_dbg_cep(cep
, "ord %d, rep_ird %d\n", cep
->ord
,
804 ird_insufficient
= true;
807 * Always report negotiated peer values to user,
808 * even if IRD/ORD negotiation failed
813 if (ird_insufficient
) {
815 * If the initiator IRD is insuffient for the
816 * responder ORD, send a TERM.
818 siw_init_terminate(qp
, TERM_ERROR_LAYER_LLP
,
820 LLP_ECODE_INSUFFICIENT_IRD
, 0);
821 siw_send_terminate(qp
);
825 if (cep
->mpa
.v2_ctrl_req
.ird
& MPA_V2_PEER_TO_PEER
)
827 cep
->mpa
.v2_ctrl_req
.ord
&
828 (MPA_V2_RDMA_WRITE_RTR
| MPA_V2_RDMA_READ_RTR
);
831 * Check if we requested P2P mode, and if peer agrees
833 if (mpa_p2p_mode
!= MPA_V2_RDMA_NO_RTR
) {
834 if ((mpa_p2p_mode
& v2
->ord
) == 0) {
836 * We requested RTR mode(s), but the peer
837 * did not pick any mode we support.
840 "rtr mode: req %2x, got %2x\n",
842 v2
->ord
& (MPA_V2_RDMA_WRITE_RTR
|
843 MPA_V2_RDMA_READ_RTR
));
845 siw_init_terminate(qp
, TERM_ERROR_LAYER_LLP
,
847 LLP_ECODE_NO_MATCHING_RTR
,
849 siw_send_terminate(qp
);
853 mpa_p2p_mode
= v2
->ord
& (MPA_V2_RDMA_WRITE_RTR
|
854 MPA_V2_RDMA_READ_RTR
);
857 memset(&qp_attrs
, 0, sizeof(qp_attrs
));
859 if (rep
->params
.bits
& MPA_RR_FLAG_CRC
)
860 qp_attrs
.flags
= SIW_MPA_CRC
;
862 qp_attrs
.irq_size
= cep
->ird
;
863 qp_attrs
.orq_size
= cep
->ord
;
864 qp_attrs
.sk
= cep
->sock
;
865 qp_attrs
.state
= SIW_QP_STATE_RTS
;
867 qp_attr_mask
= SIW_QP_ATTR_STATE
| SIW_QP_ATTR_LLP_HANDLE
|
868 SIW_QP_ATTR_ORD
| SIW_QP_ATTR_IRD
| SIW_QP_ATTR_MPA
;
870 /* Move socket RX/TX under QP control */
871 down_write(&qp
->state_lock
);
872 if (qp
->attrs
.state
> SIW_QP_STATE_RTR
) {
874 up_write(&qp
->state_lock
);
877 rv
= siw_qp_modify(qp
, &qp_attrs
, qp_attr_mask
);
879 siw_qp_socket_assoc(cep
, qp
);
881 up_write(&qp
->state_lock
);
883 /* Send extra RDMA frame to trigger peer RTS if negotiated */
884 if (mpa_p2p_mode
!= MPA_V2_RDMA_NO_RTR
) {
885 rv
= siw_qp_mpa_rts(qp
, mpa_p2p_mode
);
890 rv
= siw_cm_upcall(cep
, IW_CM_EVENT_CONNECT_REPLY
, 0);
892 cep
->state
= SIW_EPSTATE_RDMA_MODE
;
898 siw_cm_upcall(cep
, IW_CM_EVENT_CONNECT_REPLY
, -EINVAL
);
904 * siw_accept_newconn - accept an incoming pending connection
907 static void siw_accept_newconn(struct siw_cep
*cep
)
909 struct socket
*s
= cep
->sock
;
910 struct socket
*new_s
= NULL
;
911 struct siw_cep
*new_cep
= NULL
;
912 int rv
= 0; /* debug only. should disappear */
914 if (cep
->state
!= SIW_EPSTATE_LISTENING
)
917 new_cep
= siw_cep_alloc(cep
->sdev
);
922 * 4: Allocate a sufficient number of work elements
923 * to allow concurrent handling of local + peer close
924 * events, MPA header processing + MPA timeout.
926 if (siw_cm_alloc_work(new_cep
, 4) != 0)
930 * Copy saved socket callbacks from listening CEP
931 * and assign new socket with new CEP
933 new_cep
->sk_state_change
= cep
->sk_state_change
;
934 new_cep
->sk_data_ready
= cep
->sk_data_ready
;
935 new_cep
->sk_write_space
= cep
->sk_write_space
;
936 new_cep
->sk_error_report
= cep
->sk_error_report
;
938 rv
= kernel_accept(s
, &new_s
, O_NONBLOCK
);
941 * Connection already aborted by peer..?
943 siw_dbg_cep(cep
, "kernel_accept() error: %d\n", rv
);
946 new_cep
->sock
= new_s
;
947 siw_cep_get(new_cep
);
948 new_s
->sk
->sk_user_data
= new_cep
;
950 if (siw_tcp_nagle
== false) {
953 rv
= kernel_setsockopt(new_s
, SOL_TCP
, TCP_NODELAY
,
954 (char *)&val
, sizeof(val
));
956 siw_dbg_cep(cep
, "setsockopt NODELAY error: %d\n", rv
);
960 new_cep
->state
= SIW_EPSTATE_AWAIT_MPAREQ
;
962 rv
= siw_cm_queue_work(new_cep
, SIW_CM_WORK_MPATIMEOUT
);
966 * See siw_proc_mpareq() etc. for the use of new_cep->listen_cep.
968 new_cep
->listen_cep
= cep
;
971 if (atomic_read(&new_s
->sk
->sk_rmem_alloc
)) {
973 * MPA REQ already queued
975 siw_dbg_cep(cep
, "immediate mpa request\n");
977 siw_cep_set_inuse(new_cep
);
978 rv
= siw_proc_mpareq(new_cep
);
979 siw_cep_set_free(new_cep
);
983 new_cep
->listen_cep
= NULL
;
992 siw_cep_put(new_cep
);
995 siw_socket_disassoc(new_s
);
997 new_cep
->sock
= NULL
;
999 siw_dbg_cep(cep
, "error %d\n", rv
);
1002 static void siw_cm_work_handler(struct work_struct
*w
)
1004 struct siw_cm_work
*work
;
1005 struct siw_cep
*cep
;
1006 int release_cep
= 0, rv
= 0;
1008 work
= container_of(w
, struct siw_cm_work
, work
.work
);
1011 siw_dbg_cep(cep
, "[QP %u]: work type: %d, state %d\n",
1012 cep
->qp
? qp_id(cep
->qp
) : UINT_MAX
,
1013 work
->type
, cep
->state
);
1015 siw_cep_set_inuse(cep
);
1017 switch (work
->type
) {
1018 case SIW_CM_WORK_ACCEPT
:
1019 siw_accept_newconn(cep
);
1022 case SIW_CM_WORK_READ_MPAHDR
:
1023 if (cep
->state
== SIW_EPSTATE_AWAIT_MPAREQ
) {
1024 if (cep
->listen_cep
) {
1025 siw_cep_set_inuse(cep
->listen_cep
);
1027 if (cep
->listen_cep
->state
==
1028 SIW_EPSTATE_LISTENING
)
1029 rv
= siw_proc_mpareq(cep
);
1033 siw_cep_set_free(cep
->listen_cep
);
1035 if (rv
!= -EAGAIN
) {
1036 siw_cep_put(cep
->listen_cep
);
1037 cep
->listen_cep
= NULL
;
1042 } else if (cep
->state
== SIW_EPSTATE_AWAIT_MPAREP
) {
1043 rv
= siw_proc_mpareply(cep
);
1046 * CEP already moved out of MPA handshake.
1047 * any connection management already done.
1048 * silently ignore the mpa packet.
1050 if (cep
->state
== SIW_EPSTATE_RDMA_MODE
) {
1051 cep
->sock
->sk
->sk_data_ready(cep
->sock
->sk
);
1052 siw_dbg_cep(cep
, "already in RDMA mode");
1054 siw_dbg_cep(cep
, "out of state: %d\n",
1058 if (rv
&& rv
!= EAGAIN
)
1062 case SIW_CM_WORK_CLOSE_LLP
:
1064 * QP scheduled LLP close
1066 if (cep
->qp
&& cep
->qp
->term_info
.valid
)
1067 siw_send_terminate(cep
->qp
);
1070 siw_cm_upcall(cep
, IW_CM_EVENT_CLOSE
, 0);
1075 case SIW_CM_WORK_PEER_CLOSE
:
1077 if (cep
->state
== SIW_EPSTATE_AWAIT_MPAREP
) {
1079 * MPA reply not received, but connection drop
1081 siw_cm_upcall(cep
, IW_CM_EVENT_CONNECT_REPLY
,
1083 } else if (cep
->state
== SIW_EPSTATE_RDMA_MODE
) {
1085 * NOTE: IW_CM_EVENT_DISCONNECT is given just
1086 * to transition IWCM into CLOSING.
1088 siw_cm_upcall(cep
, IW_CM_EVENT_DISCONNECT
, 0);
1089 siw_cm_upcall(cep
, IW_CM_EVENT_CLOSE
, 0);
1092 * for other states there is no connection
1093 * known to the IWCM.
1096 if (cep
->state
== SIW_EPSTATE_RECVD_MPAREQ
) {
1098 * Wait for the ulp/CM to call accept/reject
1101 "mpa req recvd, wait for ULP\n");
1102 } else if (cep
->state
== SIW_EPSTATE_AWAIT_MPAREQ
) {
1104 * Socket close before MPA request received.
1106 siw_dbg_cep(cep
, "no mpareq: drop listener\n");
1107 siw_cep_put(cep
->listen_cep
);
1108 cep
->listen_cep
= NULL
;
1114 case SIW_CM_WORK_MPATIMEOUT
:
1115 cep
->mpa_timer
= NULL
;
1117 if (cep
->state
== SIW_EPSTATE_AWAIT_MPAREP
) {
1119 * MPA request timed out:
1120 * Hide any partially received private data and signal
1123 cep
->mpa
.hdr
.params
.pd_len
= 0;
1126 siw_cm_upcall(cep
, IW_CM_EVENT_CONNECT_REPLY
,
1130 } else if (cep
->state
== SIW_EPSTATE_AWAIT_MPAREQ
) {
1132 * No MPA request received after peer TCP stream setup.
1134 if (cep
->listen_cep
) {
1135 siw_cep_put(cep
->listen_cep
);
1136 cep
->listen_cep
= NULL
;
1143 WARN(1, "Undefined CM work type: %d\n", work
->type
);
1147 "release: timer=%s, QP[%u]\n",
1148 cep
->mpa_timer
? "y" : "n",
1149 cep
->qp
? qp_id(cep
->qp
) : UINT_MAX
);
1151 siw_cancel_mpatimer(cep
);
1153 cep
->state
= SIW_EPSTATE_CLOSED
;
1156 struct siw_qp
*qp
= cep
->qp
;
1158 * Serialize a potential race with application
1159 * closing the QP and calling siw_qp_cm_drop()
1162 siw_cep_set_free(cep
);
1164 siw_qp_llp_close(qp
);
1167 siw_cep_set_inuse(cep
);
1172 siw_socket_disassoc(cep
->sock
);
1173 sock_release(cep
->sock
);
1177 cep
->cm_id
->rem_ref(cep
->cm_id
);
1182 siw_cep_set_free(cep
);
1187 static struct workqueue_struct
*siw_cm_wq
;
1189 int siw_cm_queue_work(struct siw_cep
*cep
, enum siw_work_type type
)
1191 struct siw_cm_work
*work
= siw_get_work(cep
);
1192 unsigned long delay
= 0;
1195 siw_dbg_cep(cep
, "failed with no work available\n");
1203 INIT_DELAYED_WORK(&work
->work
, siw_cm_work_handler
);
1205 if (type
== SIW_CM_WORK_MPATIMEOUT
) {
1206 cep
->mpa_timer
= work
;
1208 if (cep
->state
== SIW_EPSTATE_AWAIT_MPAREP
)
1209 delay
= MPAREQ_TIMEOUT
;
1211 delay
= MPAREP_TIMEOUT
;
1213 siw_dbg_cep(cep
, "[QP %u]: work type: %d, timeout %lu\n",
1214 cep
->qp
? qp_id(cep
->qp
) : -1, type
, delay
);
1216 queue_delayed_work(siw_cm_wq
, &work
->work
, delay
);
1221 static void siw_cm_llp_data_ready(struct sock
*sk
)
1223 struct siw_cep
*cep
;
1225 read_lock(&sk
->sk_callback_lock
);
1227 cep
= sk_to_cep(sk
);
1232 siw_dbg_cep(cep
, "state: %d\n", cep
->state
);
1234 switch (cep
->state
) {
1235 case SIW_EPSTATE_RDMA_MODE
:
1237 case SIW_EPSTATE_LISTENING
:
1240 case SIW_EPSTATE_AWAIT_MPAREQ
:
1242 case SIW_EPSTATE_AWAIT_MPAREP
:
1243 siw_cm_queue_work(cep
, SIW_CM_WORK_READ_MPAHDR
);
1247 siw_dbg_cep(cep
, "unexpected data, state %d\n", cep
->state
);
1251 read_unlock(&sk
->sk_callback_lock
);
1254 static void siw_cm_llp_write_space(struct sock
*sk
)
1256 struct siw_cep
*cep
= sk_to_cep(sk
);
1259 siw_dbg_cep(cep
, "state: %d\n", cep
->state
);
1262 static void siw_cm_llp_error_report(struct sock
*sk
)
1264 struct siw_cep
*cep
= sk_to_cep(sk
);
1267 siw_dbg_cep(cep
, "error %d, socket state: %d, cep state: %d\n",
1268 sk
->sk_err
, sk
->sk_state
, cep
->state
);
1269 cep
->sk_error_report(sk
);
1273 static void siw_cm_llp_state_change(struct sock
*sk
)
1275 struct siw_cep
*cep
;
1276 void (*orig_state_change
)(struct sock
*s
);
1278 read_lock(&sk
->sk_callback_lock
);
1280 cep
= sk_to_cep(sk
);
1282 /* endpoint already disassociated */
1283 read_unlock(&sk
->sk_callback_lock
);
1286 orig_state_change
= cep
->sk_state_change
;
1288 siw_dbg_cep(cep
, "state: %d\n", cep
->state
);
1290 switch (sk
->sk_state
) {
1291 case TCP_ESTABLISHED
:
1293 * handle accepting socket as special case where only
1294 * new connection is possible
1296 siw_cm_queue_work(cep
, SIW_CM_WORK_ACCEPT
);
1300 case TCP_CLOSE_WAIT
:
1302 cep
->qp
->tx_ctx
.tx_suspend
= 1;
1303 siw_cm_queue_work(cep
, SIW_CM_WORK_PEER_CLOSE
);
1307 siw_dbg_cep(cep
, "unexpected socket state %d\n", sk
->sk_state
);
1309 read_unlock(&sk
->sk_callback_lock
);
1310 orig_state_change(sk
);
1313 static int kernel_bindconnect(struct socket
*s
, struct sockaddr
*laddr
,
1314 struct sockaddr
*raddr
)
1316 int rv
, flags
= 0, s_val
= 1;
1317 size_t size
= laddr
->sa_family
== AF_INET
?
1318 sizeof(struct sockaddr_in
) : sizeof(struct sockaddr_in6
);
1321 * Make address available again asap.
1323 rv
= kernel_setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&s_val
,
1328 rv
= s
->ops
->bind(s
, laddr
, size
);
1332 rv
= s
->ops
->connect(s
, raddr
, size
, flags
);
1334 return rv
< 0 ? rv
: 0;
1337 int siw_connect(struct iw_cm_id
*id
, struct iw_cm_conn_param
*params
)
1339 struct siw_device
*sdev
= to_siw_dev(id
->device
);
1341 struct siw_cep
*cep
= NULL
;
1342 struct socket
*s
= NULL
;
1343 struct sockaddr
*laddr
= (struct sockaddr
*)&id
->local_addr
,
1344 *raddr
= (struct sockaddr
*)&id
->remote_addr
;
1345 bool p2p_mode
= peer_to_peer
, v4
= true;
1346 u16 pd_len
= params
->private_data_len
;
1347 int version
= mpa_version
, rv
;
1349 if (pd_len
> MPA_MAX_PRIVDATA
)
1352 if (params
->ird
> sdev
->attrs
.max_ird
||
1353 params
->ord
> sdev
->attrs
.max_ord
)
1356 if (laddr
->sa_family
== AF_INET6
)
1358 else if (laddr
->sa_family
!= AF_INET
)
1359 return -EAFNOSUPPORT
;
1362 * Respect any iwarp port mapping: Use mapped remote address
1363 * if valid. Local address must not be mapped, since siw
1364 * uses kernel TCP stack.
1366 if ((v4
&& to_sockaddr_in(id
->remote_addr
).sin_port
!= 0) ||
1367 to_sockaddr_in6(id
->remote_addr
).sin6_port
!= 0)
1368 raddr
= (struct sockaddr
*)&id
->m_remote_addr
;
1370 qp
= siw_qp_id2obj(sdev
, params
->qpn
);
1372 WARN(1, "[QP %u] does not exist\n", params
->qpn
);
1376 siw_dbg_qp(qp
, "pd_len %d, laddr %pISp, raddr %pISp\n", pd_len
, laddr
,
1379 rv
= sock_create(v4
? AF_INET
: AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
, &s
);
1384 * NOTE: For simplification, connect() is called in blocking
1385 * mode. Might be reconsidered for async connection setup at
1388 rv
= kernel_bindconnect(s
, laddr
, raddr
);
1390 siw_dbg_qp(qp
, "kernel_bindconnect: error %d\n", rv
);
1393 if (siw_tcp_nagle
== false) {
1396 rv
= kernel_setsockopt(s
, SOL_TCP
, TCP_NODELAY
, (char *)&val
,
1399 siw_dbg_qp(qp
, "setsockopt NODELAY error: %d\n", rv
);
1403 cep
= siw_cep_alloc(sdev
);
1408 siw_cep_set_inuse(cep
);
1410 /* Associate QP with CEP */
1414 /* siw_qp_get(qp) already done by QP lookup */
1421 * 4: Allocate a sufficient number of work elements
1422 * to allow concurrent handling of local + peer close
1423 * events, MPA header processing + MPA timeout.
1425 rv
= siw_cm_alloc_work(cep
, 4);
1430 cep
->ird
= params
->ird
;
1431 cep
->ord
= params
->ord
;
1433 if (p2p_mode
&& cep
->ord
== 0)
1436 cep
->state
= SIW_EPSTATE_CONNECTING
;
1439 * Associate CEP with socket
1441 siw_cep_socket_assoc(cep
, s
);
1443 cep
->state
= SIW_EPSTATE_AWAIT_MPAREP
;
1446 * Set MPA Request bits: CRC if required, no MPA Markers,
1447 * MPA Rev. according to module parameter 'mpa_version', Key 'Request'.
1449 cep
->mpa
.hdr
.params
.bits
= 0;
1450 if (version
> MPA_REVISION_2
) {
1451 pr_warn("Setting MPA version to %u\n", MPA_REVISION_2
);
1452 version
= MPA_REVISION_2
;
1453 /* Adjust also module parameter */
1454 mpa_version
= MPA_REVISION_2
;
1456 __mpa_rr_set_revision(&cep
->mpa
.hdr
.params
.bits
, version
);
1459 cep
->mpa
.hdr
.params
.bits
|= MPA_RR_FLAG_GSO_EXP
;
1461 if (mpa_crc_required
)
1462 cep
->mpa
.hdr
.params
.bits
|= MPA_RR_FLAG_CRC
;
1465 * If MPA version == 2:
1466 * o Include ORD and IRD.
1467 * o Indicate peer-to-peer mode, if required by module
1468 * parameter 'peer_to_peer'.
1470 if (version
== MPA_REVISION_2
) {
1471 cep
->enhanced_rdma_conn_est
= true;
1472 cep
->mpa
.hdr
.params
.bits
|= MPA_RR_FLAG_ENHANCED
;
1474 cep
->mpa
.v2_ctrl
.ird
= htons(cep
->ird
);
1475 cep
->mpa
.v2_ctrl
.ord
= htons(cep
->ord
);
1478 cep
->mpa
.v2_ctrl
.ird
|= MPA_V2_PEER_TO_PEER
;
1479 cep
->mpa
.v2_ctrl
.ord
|= rtr_type
;
1481 /* Remember own P2P mode requested */
1482 cep
->mpa
.v2_ctrl_req
.ird
= cep
->mpa
.v2_ctrl
.ird
;
1483 cep
->mpa
.v2_ctrl_req
.ord
= cep
->mpa
.v2_ctrl
.ord
;
1485 memcpy(cep
->mpa
.hdr
.key
, MPA_KEY_REQ
, 16);
1487 rv
= siw_send_mpareqrep(cep
, params
->private_data
, pd_len
);
1489 * Reset private data.
1491 cep
->mpa
.hdr
.params
.pd_len
= 0;
1494 rv
= siw_cm_queue_work(cep
, SIW_CM_WORK_MPATIMEOUT
);
1496 siw_dbg_cep(cep
, "[QP %u]: exit\n", qp_id(qp
));
1497 siw_cep_set_free(cep
);
1502 siw_dbg(id
->device
, "failed: %d\n", rv
);
1505 siw_socket_disassoc(s
);
1518 cep
->state
= SIW_EPSTATE_CLOSED
;
1520 siw_cep_set_free(cep
);
1534 * siw_accept - Let SoftiWARP accept an RDMA connection request
1536 * @id: New connection management id to be used for accepted
1537 * connection request
1538 * @params: Connection parameters provided by ULP for accepting connection
1540 * Transition QP to RTS state, associate new CM id @id with accepted CEP
1541 * and get prepared for TCP input by installing socket callbacks.
1542 * Then send MPA Reply and generate the "connection established" event.
1543 * Socket callbacks must be installed before sending MPA Reply, because
1544 * the latter may cause a first RDMA message to arrive from the RDMA Initiator
1545 * side very quickly, at which time the socket callbacks must be ready.
1547 int siw_accept(struct iw_cm_id
*id
, struct iw_cm_conn_param
*params
)
1549 struct siw_device
*sdev
= to_siw_dev(id
->device
);
1550 struct siw_cep
*cep
= (struct siw_cep
*)id
->provider_data
;
1552 struct siw_qp_attrs qp_attrs
;
1553 int rv
, max_priv_data
= MPA_MAX_PRIVDATA
;
1554 bool wait_for_peer_rts
= false;
1556 siw_cep_set_inuse(cep
);
1559 /* Free lingering inbound private data */
1560 if (cep
->mpa
.hdr
.params
.pd_len
) {
1561 cep
->mpa
.hdr
.params
.pd_len
= 0;
1562 kfree(cep
->mpa
.pdata
);
1563 cep
->mpa
.pdata
= NULL
;
1565 siw_cancel_mpatimer(cep
);
1567 if (cep
->state
!= SIW_EPSTATE_RECVD_MPAREQ
) {
1568 siw_dbg_cep(cep
, "out of state\n");
1570 siw_cep_set_free(cep
);
1575 qp
= siw_qp_id2obj(sdev
, params
->qpn
);
1577 WARN(1, "[QP %d] does not exist\n", params
->qpn
);
1578 siw_cep_set_free(cep
);
1583 down_write(&qp
->state_lock
);
1584 if (qp
->attrs
.state
> SIW_QP_STATE_RTR
) {
1586 up_write(&qp
->state_lock
);
1589 siw_dbg_cep(cep
, "[QP %d]\n", params
->qpn
);
1591 if (try_gso
&& cep
->mpa
.hdr
.params
.bits
& MPA_RR_FLAG_GSO_EXP
) {
1592 siw_dbg_cep(cep
, "peer allows GSO on TX\n");
1593 qp
->tx_ctx
.gso_seg_limit
= 0;
1595 if (params
->ord
> sdev
->attrs
.max_ord
||
1596 params
->ird
> sdev
->attrs
.max_ird
) {
1599 "[QP %u]: ord %d (max %d), ird %d (max %d)\n",
1600 qp_id(qp
), params
->ord
, sdev
->attrs
.max_ord
,
1601 params
->ird
, sdev
->attrs
.max_ird
);
1603 up_write(&qp
->state_lock
);
1606 if (cep
->enhanced_rdma_conn_est
)
1607 max_priv_data
-= sizeof(struct mpa_v2_data
);
1609 if (params
->private_data_len
> max_priv_data
) {
1612 "[QP %u]: private data length: %d (max %d)\n",
1613 qp_id(qp
), params
->private_data_len
, max_priv_data
);
1615 up_write(&qp
->state_lock
);
1618 if (cep
->enhanced_rdma_conn_est
) {
1619 if (params
->ord
> cep
->ord
) {
1620 if (relaxed_ird_negotiation
) {
1621 params
->ord
= cep
->ord
;
1623 cep
->ird
= params
->ird
;
1624 cep
->ord
= params
->ord
;
1626 up_write(&qp
->state_lock
);
1630 if (params
->ird
< cep
->ird
) {
1631 if (relaxed_ird_negotiation
&&
1632 cep
->ird
<= sdev
->attrs
.max_ird
)
1633 params
->ird
= cep
->ird
;
1636 up_write(&qp
->state_lock
);
1640 if (cep
->mpa
.v2_ctrl
.ord
&
1641 (MPA_V2_RDMA_WRITE_RTR
| MPA_V2_RDMA_READ_RTR
))
1642 wait_for_peer_rts
= true;
1644 * Signal back negotiated IRD and ORD values
1646 cep
->mpa
.v2_ctrl
.ord
=
1647 htons(params
->ord
& MPA_IRD_ORD_MASK
) |
1648 (cep
->mpa
.v2_ctrl
.ord
& ~MPA_V2_MASK_IRD_ORD
);
1649 cep
->mpa
.v2_ctrl
.ird
=
1650 htons(params
->ird
& MPA_IRD_ORD_MASK
) |
1651 (cep
->mpa
.v2_ctrl
.ird
& ~MPA_V2_MASK_IRD_ORD
);
1653 cep
->ird
= params
->ird
;
1654 cep
->ord
= params
->ord
;
1659 memset(&qp_attrs
, 0, sizeof(qp_attrs
));
1660 qp_attrs
.orq_size
= cep
->ord
;
1661 qp_attrs
.irq_size
= cep
->ird
;
1662 qp_attrs
.sk
= cep
->sock
;
1663 if (cep
->mpa
.hdr
.params
.bits
& MPA_RR_FLAG_CRC
)
1664 qp_attrs
.flags
= SIW_MPA_CRC
;
1665 qp_attrs
.state
= SIW_QP_STATE_RTS
;
1667 siw_dbg_cep(cep
, "[QP%u]: moving to rts\n", qp_id(qp
));
1669 /* Associate QP with CEP */
1673 /* siw_qp_get(qp) already done by QP lookup */
1676 cep
->state
= SIW_EPSTATE_RDMA_MODE
;
1678 /* Move socket RX/TX under QP control */
1679 rv
= siw_qp_modify(qp
, &qp_attrs
,
1680 SIW_QP_ATTR_STATE
| SIW_QP_ATTR_LLP_HANDLE
|
1681 SIW_QP_ATTR_ORD
| SIW_QP_ATTR_IRD
|
1683 up_write(&qp
->state_lock
);
1688 siw_dbg_cep(cep
, "[QP %u]: send mpa reply, %d byte pdata\n",
1689 qp_id(qp
), params
->private_data_len
);
1691 rv
= siw_send_mpareqrep(cep
, params
->private_data
,
1692 params
->private_data_len
);
1696 if (wait_for_peer_rts
) {
1697 siw_sk_assign_rtr_upcalls(cep
);
1699 siw_qp_socket_assoc(cep
, qp
);
1700 rv
= siw_cm_upcall(cep
, IW_CM_EVENT_ESTABLISHED
, 0);
1704 siw_cep_set_free(cep
);
1708 siw_socket_disassoc(cep
->sock
);
1709 sock_release(cep
->sock
);
1712 cep
->state
= SIW_EPSTATE_CLOSED
;
1715 cep
->cm_id
->rem_ref(id
);
1725 siw_cep_set_free(cep
);
1734 * Local connection reject case. Send private data back to peer,
1735 * close connection and dereference connection id.
1737 int siw_reject(struct iw_cm_id
*id
, const void *pdata
, u8 pd_len
)
1739 struct siw_cep
*cep
= (struct siw_cep
*)id
->provider_data
;
1741 siw_cep_set_inuse(cep
);
1744 siw_cancel_mpatimer(cep
);
1746 if (cep
->state
!= SIW_EPSTATE_RECVD_MPAREQ
) {
1747 siw_dbg_cep(cep
, "out of state\n");
1749 siw_cep_set_free(cep
);
1750 siw_cep_put(cep
); /* put last reference */
1754 siw_dbg_cep(cep
, "cep->state %d, pd_len %d\n", cep
->state
,
1757 if (__mpa_rr_revision(cep
->mpa
.hdr
.params
.bits
) >= MPA_REVISION_1
) {
1758 cep
->mpa
.hdr
.params
.bits
|= MPA_RR_FLAG_REJECT
; /* reject */
1759 siw_send_mpareqrep(cep
, pdata
, pd_len
);
1761 siw_socket_disassoc(cep
->sock
);
1762 sock_release(cep
->sock
);
1765 cep
->state
= SIW_EPSTATE_CLOSED
;
1767 siw_cep_set_free(cep
);
1773 static int siw_listen_address(struct iw_cm_id
*id
, int backlog
,
1774 struct sockaddr
*laddr
, int addr_family
)
1777 struct siw_cep
*cep
= NULL
;
1778 struct siw_device
*sdev
= to_siw_dev(id
->device
);
1781 rv
= sock_create(addr_family
, SOCK_STREAM
, IPPROTO_TCP
, &s
);
1786 * Allow binding local port when still in TIME_WAIT from last close.
1789 rv
= kernel_setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&s_val
,
1792 siw_dbg(id
->device
, "setsockopt error: %d\n", rv
);
1795 rv
= s
->ops
->bind(s
, laddr
, addr_family
== AF_INET
?
1796 sizeof(struct sockaddr_in
) :
1797 sizeof(struct sockaddr_in6
));
1799 siw_dbg(id
->device
, "socket bind error: %d\n", rv
);
1802 cep
= siw_cep_alloc(sdev
);
1807 siw_cep_socket_assoc(cep
, s
);
1809 rv
= siw_cm_alloc_work(cep
, backlog
);
1812 "alloc_work error %d, backlog %d\n",
1816 rv
= s
->ops
->listen(s
, backlog
);
1818 siw_dbg(id
->device
, "listen error %d\n", rv
);
1825 * In case of a wildcard rdma_listen on a multi-homed device,
1826 * a listener's IWCM id is associated with more than one listening CEP.
1828 * We currently use id->provider_data in three different ways:
1830 * o For a listener's IWCM id, id->provider_data points to
1831 * the list_head of the list of listening CEPs.
1832 * Uses: siw_create_listen(), siw_destroy_listen()
1834 * o For each accepted passive-side IWCM id, id->provider_data
1835 * points to the CEP itself. This is a consequence of
1836 * - siw_cm_upcall() setting event.provider_data = cep and
1837 * - the IWCM's cm_conn_req_handler() setting provider_data of the
1838 * new passive-side IWCM id equal to event.provider_data
1839 * Uses: siw_accept(), siw_reject()
1841 * o For an active-side IWCM id, id->provider_data is not used at all.
1844 if (!id
->provider_data
) {
1846 kmalloc(sizeof(struct list_head
), GFP_KERNEL
);
1847 if (!id
->provider_data
) {
1851 INIT_LIST_HEAD((struct list_head
*)id
->provider_data
);
1853 list_add_tail(&cep
->listenq
, (struct list_head
*)id
->provider_data
);
1854 cep
->state
= SIW_EPSTATE_LISTENING
;
1856 siw_dbg(id
->device
, "Listen at laddr %pISp\n", laddr
);
1861 siw_dbg(id
->device
, "failed: %d\n", rv
);
1864 siw_cep_set_inuse(cep
);
1867 cep
->cm_id
->rem_ref(cep
->cm_id
);
1871 siw_socket_disassoc(s
);
1872 cep
->state
= SIW_EPSTATE_CLOSED
;
1874 siw_cep_set_free(cep
);
1882 static void siw_drop_listeners(struct iw_cm_id
*id
)
1884 struct list_head
*p
, *tmp
;
1887 * In case of a wildcard rdma_listen on a multi-homed device,
1888 * a listener's IWCM id is associated with more than one listening CEP.
1890 list_for_each_safe(p
, tmp
, (struct list_head
*)id
->provider_data
) {
1891 struct siw_cep
*cep
= list_entry(p
, struct siw_cep
, listenq
);
1895 siw_dbg_cep(cep
, "drop cep, state %d\n", cep
->state
);
1897 siw_cep_set_inuse(cep
);
1900 cep
->cm_id
->rem_ref(cep
->cm_id
);
1904 siw_socket_disassoc(cep
->sock
);
1905 sock_release(cep
->sock
);
1908 cep
->state
= SIW_EPSTATE_CLOSED
;
1909 siw_cep_set_free(cep
);
1915 * siw_create_listen - Create resources for a listener's IWCM ID @id
1917 * Listens on the socket address id->local_addr.
1919 * If the listener's @id provides a specific local IP address, at most one
1920 * listening socket is created and associated with @id.
1922 * If the listener's @id provides the wildcard (zero) local IP address,
1923 * a separate listen is performed for each local IP address of the device
1924 * by creating a listening socket and binding to that local IP address.
1927 int siw_create_listen(struct iw_cm_id
*id
, int backlog
)
1929 struct net_device
*dev
= to_siw_dev(id
->device
)->netdev
;
1930 int rv
= 0, listeners
= 0;
1932 siw_dbg(id
->device
, "backlog %d\n", backlog
);
1935 * For each attached address of the interface, create a
1936 * listening socket, if id->local_addr is the wildcard
1937 * IP address or matches the IP address.
1939 if (id
->local_addr
.ss_family
== AF_INET
) {
1940 struct in_device
*in_dev
= in_dev_get(dev
);
1941 struct sockaddr_in s_laddr
;
1942 const struct in_ifaddr
*ifa
;
1948 memcpy(&s_laddr
, &id
->local_addr
, sizeof(s_laddr
));
1950 siw_dbg(id
->device
, "laddr %pISp\n", &s_laddr
);
1953 in_dev_for_each_ifa_rtnl(ifa
, in_dev
) {
1954 if (ipv4_is_zeronet(s_laddr
.sin_addr
.s_addr
) ||
1955 s_laddr
.sin_addr
.s_addr
== ifa
->ifa_address
) {
1956 s_laddr
.sin_addr
.s_addr
= ifa
->ifa_address
;
1958 rv
= siw_listen_address(id
, backlog
,
1959 (struct sockaddr
*)&s_laddr
,
1967 } else if (id
->local_addr
.ss_family
== AF_INET6
) {
1968 struct inet6_dev
*in6_dev
= in6_dev_get(dev
);
1969 struct inet6_ifaddr
*ifp
;
1970 struct sockaddr_in6
*s_laddr
= &to_sockaddr_in6(id
->local_addr
);
1976 siw_dbg(id
->device
, "laddr %pISp\n", &s_laddr
);
1979 list_for_each_entry(ifp
, &in6_dev
->addr_list
, if_list
) {
1980 if (ifp
->flags
& (IFA_F_TENTATIVE
| IFA_F_DEPRECATED
))
1982 if (ipv6_addr_any(&s_laddr
->sin6_addr
) ||
1983 ipv6_addr_equal(&s_laddr
->sin6_addr
, &ifp
->addr
)) {
1984 struct sockaddr_in6 bind_addr
= {
1985 .sin6_family
= AF_INET6
,
1986 .sin6_port
= s_laddr
->sin6_port
,
1988 .sin6_addr
= ifp
->addr
,
1989 .sin6_scope_id
= dev
->ifindex
};
1991 rv
= siw_listen_address(id
, backlog
,
1992 (struct sockaddr
*)&bind_addr
,
1999 in6_dev_put(in6_dev
);
2009 siw_dbg(id
->device
, "%s\n", rv
? "FAIL" : "OK");
2014 int siw_destroy_listen(struct iw_cm_id
*id
)
2016 if (!id
->provider_data
) {
2017 siw_dbg(id
->device
, "no cep(s)\n");
2020 siw_drop_listeners(id
);
2021 kfree(id
->provider_data
);
2022 id
->provider_data
= NULL
;
2027 int siw_cm_init(void)
2030 * create_single_workqueue for strict ordering
2032 siw_cm_wq
= create_singlethread_workqueue("siw_cm_wq");
2039 void siw_cm_exit(void)
2042 flush_workqueue(siw_cm_wq
);
2043 destroy_workqueue(siw_cm_wq
);