1
2
3
4
5
6
7
8
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>
15#include <net/sock.h>
16#include <net/tcp.h>
17#include <linux/inet.h>
18#include <linux/tcp.h>
19
20#include <rdma/iw_cm.h>
21#include <rdma/ib_verbs.h>
22#include <rdma/ib_user_verbs.h>
23
24#include "siw.h"
25#include "siw_cm.h"
26
27
28
29
30
31static __be16 rtr_type = MPA_V2_RDMA_READ_RTR | MPA_V2_RDMA_WRITE_RTR;
32static const bool relaxed_ird_negotiation = true;
33
34static void siw_cm_llp_state_change(struct sock *s);
35static void siw_cm_llp_data_ready(struct sock *s);
36static void siw_cm_llp_write_space(struct sock *s);
37static void siw_cm_llp_error_report(struct sock *s);
38static int siw_cm_upcall(struct siw_cep *cep, enum iw_cm_event_type reason,
39 int status);
40
41static void siw_sk_assign_cm_upcalls(struct sock *sk)
42{
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);
49}
50
51static void siw_sk_save_upcalls(struct sock *sk)
52{
53 struct siw_cep *cep = sk_to_cep(sk);
54
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);
61}
62
63static void siw_sk_restore_upcalls(struct sock *sk, struct siw_cep *cep)
64{
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;
70}
71
72static void siw_qp_socket_assoc(struct siw_cep *cep, struct siw_qp *qp)
73{
74 struct socket *s = cep->sock;
75 struct sock *sk = s->sk;
76
77 write_lock_bh(&sk->sk_callback_lock);
78
79 qp->attrs.sk = s;
80 sk->sk_data_ready = siw_qp_llp_data_ready;
81 sk->sk_write_space = siw_qp_llp_write_space;
82
83 write_unlock_bh(&sk->sk_callback_lock);
84}
85
86static void siw_socket_disassoc(struct socket *s)
87{
88 struct sock *sk = s->sk;
89 struct siw_cep *cep;
90
91 if (sk) {
92 write_lock_bh(&sk->sk_callback_lock);
93 cep = sk_to_cep(sk);
94 if (cep) {
95 siw_sk_restore_upcalls(sk, cep);
96 siw_cep_put(cep);
97 } else {
98 pr_warn("siw: cannot restore sk callbacks: no ep\n");
99 }
100 write_unlock_bh(&sk->sk_callback_lock);
101 } else {
102 pr_warn("siw: cannot restore sk callbacks: no sk\n");
103 }
104}
105
106static void siw_rtr_data_ready(struct sock *sk)
107{
108 struct siw_cep *cep;
109 struct siw_qp *qp = NULL;
110 read_descriptor_t rd_desc;
111
112 read_lock(&sk->sk_callback_lock);
113
114 cep = sk_to_cep(sk);
115 if (!cep) {
116 WARN(1, "No connection endpoint\n");
117 goto out;
118 }
119 qp = sk_to_qp(sk);
120
121 memset(&rd_desc, 0, sizeof(rd_desc));
122 rd_desc.arg.data = qp;
123 rd_desc.count = 1;
124
125 tcp_read_sock(sk, &rd_desc, siw_tcp_rx_data);
126
127
128
129
130
131
132 if (!qp->rx_stream.rx_suspend)
133 siw_cm_upcall(cep, IW_CM_EVENT_ESTABLISHED, 0);
134out:
135 read_unlock(&sk->sk_callback_lock);
136 if (qp)
137 siw_qp_socket_assoc(cep, qp);
138}
139
140static void siw_sk_assign_rtr_upcalls(struct siw_cep *cep)
141{
142 struct sock *sk = cep->sock->sk;
143
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);
148}
149
150static void siw_cep_socket_assoc(struct siw_cep *cep, struct socket *s)
151{
152 cep->sock = s;
153 siw_cep_get(cep);
154 s->sk->sk_user_data = cep;
155
156 siw_sk_save_upcalls(s->sk);
157 siw_sk_assign_cm_upcalls(s->sk);
158}
159
160static struct siw_cep *siw_cep_alloc(struct siw_device *sdev)
161{
162 struct siw_cep *cep = kzalloc(sizeof(*cep), GFP_KERNEL);
163 unsigned long flags;
164
165 if (!cep)
166 return NULL;
167
168 INIT_LIST_HEAD(&cep->listenq);
169 INIT_LIST_HEAD(&cep->devq);
170 INIT_LIST_HEAD(&cep->work_freelist);
171
172 kref_init(&cep->ref);
173 cep->state = SIW_EPSTATE_IDLE;
174 init_waitqueue_head(&cep->waitq);
175 spin_lock_init(&cep->lock);
176 cep->sdev = sdev;
177 cep->enhanced_rdma_conn_est = false;
178
179 spin_lock_irqsave(&sdev->lock, flags);
180 list_add_tail(&cep->devq, &sdev->cep_list);
181 spin_unlock_irqrestore(&sdev->lock, flags);
182
183 siw_dbg_cep(cep, "new endpoint\n");
184 return cep;
185}
186
187static void siw_cm_free_work(struct siw_cep *cep)
188{
189 struct list_head *w, *tmp;
190 struct siw_cm_work *work;
191
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);
195 kfree(work);
196 }
197}
198
199static void siw_cancel_mpatimer(struct siw_cep *cep)
200{
201 spin_lock_bh(&cep->lock);
202 if (cep->mpa_timer) {
203 if (cancel_delayed_work(&cep->mpa_timer->work)) {
204 siw_cep_put(cep);
205 kfree(cep->mpa_timer);
206 }
207 cep->mpa_timer = NULL;
208 }
209 spin_unlock_bh(&cep->lock);
210}
211
212static void siw_put_work(struct siw_cm_work *work)
213{
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);
218}
219
220static void siw_cep_set_inuse(struct siw_cep *cep)
221{
222 unsigned long flags;
223retry:
224 spin_lock_irqsave(&cep->lock, flags);
225
226 if (cep->in_use) {
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);
231 goto retry;
232 } else {
233 cep->in_use = 1;
234 spin_unlock_irqrestore(&cep->lock, flags);
235 }
236}
237
238static void siw_cep_set_free(struct siw_cep *cep)
239{
240 unsigned long flags;
241
242 spin_lock_irqsave(&cep->lock, flags);
243 cep->in_use = 0;
244 spin_unlock_irqrestore(&cep->lock, flags);
245
246 wake_up(&cep->waitq);
247}
248
249static void __siw_cep_dealloc(struct kref *ref)
250{
251 struct siw_cep *cep = container_of(ref, struct siw_cep, ref);
252 struct siw_device *sdev = cep->sdev;
253 unsigned long flags;
254
255 WARN_ON(cep->listen_cep);
256
257
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);
263
264 spin_lock_irqsave(&sdev->lock, flags);
265 list_del(&cep->devq);
266 spin_unlock_irqrestore(&sdev->lock, flags);
267
268 siw_dbg_cep(cep, "free endpoint\n");
269 kfree(cep);
270}
271
272static struct siw_cm_work *siw_get_work(struct siw_cep *cep)
273{
274 struct siw_cm_work *work = NULL;
275
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,
279 list);
280 list_del_init(&work->list);
281 }
282 spin_unlock_bh(&cep->lock);
283 return work;
284}
285
286static int siw_cm_alloc_work(struct siw_cep *cep, int num)
287{
288 struct siw_cm_work *work;
289
290 while (num--) {
291 work = kmalloc(sizeof(*work), GFP_KERNEL);
292 if (!work) {
293 if (!(list_empty(&cep->work_freelist)))
294 siw_cm_free_work(cep);
295 return -ENOMEM;
296 }
297 work->cep = cep;
298 INIT_LIST_HEAD(&work->list);
299 list_add(&work->list, &cep->work_freelist);
300 }
301 return 0;
302}
303
304
305
306
307
308
309static int siw_cm_upcall(struct siw_cep *cep, enum iw_cm_event_type reason,
310 int status)
311{
312 struct iw_cm_event event;
313 struct iw_cm_id *id;
314
315 memset(&event, 0, sizeof(event));
316 event.status = status;
317 event.event = reason;
318
319 if (reason == IW_CM_EVENT_CONNECT_REQUEST) {
320 event.provider_data = cep;
321 id = cep->listen_cep->cm_id;
322 } else {
323 id = cep->cm_id;
324 }
325
326 if (reason == IW_CM_EVENT_ESTABLISHED ||
327 reason == IW_CM_EVENT_CONNECT_REPLY) {
328
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;
334 }
335
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);
339
340 if (pd_len) {
341
342
343
344 event.private_data_len = pd_len;
345 event.private_data = cep->mpa.pdata;
346
347
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);
353 }
354 }
355 getname_local(cep->sock, &event.local_addr);
356 getname_peer(cep->sock, &event.remote_addr);
357 }
358 siw_dbg_cep(cep, "[QP %u]: reason=%d, status=%d\n",
359 cep->qp ? qp_id(cep->qp) : UINT_MAX, reason, status);
360
361 return id->event_handler(id, &event);
362}
363
364
365
366
367
368
369
370
371
372void siw_qp_cm_drop(struct siw_qp *qp, int schedule)
373{
374 struct siw_cep *cep = qp->cep;
375
376 qp->rx_stream.rx_suspend = 1;
377 qp->tx_ctx.tx_suspend = 1;
378
379 if (!qp->cep)
380 return;
381
382 if (schedule) {
383 siw_cm_queue_work(cep, SIW_CM_WORK_CLOSE_LLP);
384 } else {
385 siw_cep_set_inuse(cep);
386
387 if (cep->state == SIW_EPSTATE_CLOSED) {
388 siw_dbg_cep(cep, "already closed\n");
389 goto out;
390 }
391 siw_dbg_cep(cep, "immediate close, state %d\n", cep->state);
392
393 if (qp->term_info.valid)
394 siw_send_terminate(qp);
395
396 if (cep->cm_id) {
397 switch (cep->state) {
398 case SIW_EPSTATE_AWAIT_MPAREP:
399 siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY,
400 -EINVAL);
401 break;
402
403 case SIW_EPSTATE_RDMA_MODE:
404 siw_cm_upcall(cep, IW_CM_EVENT_CLOSE, 0);
405 break;
406
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:
413 default:
414 break;
415 }
416 cep->cm_id->rem_ref(cep->cm_id);
417 cep->cm_id = NULL;
418 siw_cep_put(cep);
419 }
420 cep->state = SIW_EPSTATE_CLOSED;
421
422 if (cep->sock) {
423 siw_socket_disassoc(cep->sock);
424
425
426
427 sock_release(cep->sock);
428 cep->sock = NULL;
429 }
430 if (cep->qp) {
431 cep->qp = NULL;
432 siw_qp_put(qp);
433 }
434out:
435 siw_cep_set_free(cep);
436 }
437}
438
439void siw_cep_put(struct siw_cep *cep)
440{
441 WARN_ON(kref_read(&cep->ref) < 1);
442 kref_put(&cep->ref, __siw_cep_dealloc);
443}
444
445void siw_cep_get(struct siw_cep *cep)
446{
447 kref_get(&cep->ref);
448}
449
450
451
452
453static int siw_send_mpareqrep(struct siw_cep *cep, const void *pdata, u8 pd_len)
454{
455 struct socket *s = cep->sock;
456 struct mpa_rr *rr = &cep->mpa.hdr;
457 struct kvec iov[3];
458 struct msghdr msg;
459 int rv;
460 int iovec_num = 0;
461 int mpa_len;
462
463 memset(&msg, 0, sizeof(msg));
464
465 iov[iovec_num].iov_base = rr;
466 iov[iovec_num].iov_len = sizeof(*rr);
467 mpa_len = sizeof(*rr);
468
469 if (cep->enhanced_rdma_conn_est) {
470 iovec_num++;
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);
474 }
475 if (pd_len) {
476 iovec_num++;
477 iov[iovec_num].iov_base = (char *)pdata;
478 iov[iovec_num].iov_len = pd_len;
479 mpa_len += pd_len;
480 }
481 if (cep->enhanced_rdma_conn_est)
482 pd_len += sizeof(cep->mpa.v2_ctrl);
483
484 rr->params.pd_len = cpu_to_be16(pd_len);
485
486 rv = kernel_sendmsg(s, &msg, iov, iovec_num + 1, mpa_len);
487
488 return rv < 0 ? rv : 0;
489}
490
491
492
493
494
495
496
497
498
499
500static int siw_recv_mpa_rr(struct siw_cep *cep)
501{
502 struct mpa_rr *hdr = &cep->mpa.hdr;
503 struct socket *s = cep->sock;
504 u16 pd_len;
505 int rcvd, to_rcv;
506
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,
510 0);
511 if (rcvd <= 0)
512 return -ECONNABORTED;
513
514 cep->mpa.bytes_rcvd += rcvd;
515
516 if (cep->mpa.bytes_rcvd < sizeof(struct mpa_rr))
517 return -EAGAIN;
518
519 if (be16_to_cpu(hdr->params.pd_len) > MPA_MAX_PRIVDATA)
520 return -EPROTO;
521 }
522 pd_len = be16_to_cpu(hdr->params.pd_len);
523
524
525
526
527
528
529 to_rcv = pd_len - (cep->mpa.bytes_rcvd - sizeof(struct mpa_rr));
530
531 if (!to_rcv) {
532
533
534
535
536
537 u32 word;
538
539 rcvd = ksock_recv(s, (char *)&word, sizeof(word), MSG_DONTWAIT);
540 if (rcvd == -EAGAIN)
541 return 0;
542
543 if (rcvd == 0) {
544 siw_dbg_cep(cep, "peer EOF\n");
545 return -EPIPE;
546 }
547 if (rcvd < 0) {
548 siw_dbg_cep(cep, "error: %d\n", rcvd);
549 return rcvd;
550 }
551 siw_dbg_cep(cep, "peer sent extra data: %d\n", rcvd);
552
553 return -EPROTO;
554 }
555
556
557
558
559
560 if (!cep->mpa.pdata) {
561 cep->mpa.pdata = kmalloc(pd_len + 4, GFP_KERNEL);
562 if (!cep->mpa.pdata)
563 return -ENOMEM;
564 }
565 rcvd = ksock_recv(
566 s, cep->mpa.pdata + cep->mpa.bytes_rcvd - sizeof(struct mpa_rr),
567 to_rcv + 4, MSG_DONTWAIT);
568
569 if (rcvd < 0)
570 return rcvd;
571
572 if (rcvd > to_rcv)
573 return -EPROTO;
574
575 cep->mpa.bytes_rcvd += rcvd;
576
577 if (to_rcv == rcvd) {
578 siw_dbg_cep(cep, "%d bytes private data received\n", pd_len);
579 return 0;
580 }
581 return -EAGAIN;
582}
583
584
585
586
587
588
589
590static int siw_proc_mpareq(struct siw_cep *cep)
591{
592 struct mpa_rr *req;
593 int version, rv;
594 u16 pd_len;
595
596 rv = siw_recv_mpa_rr(cep);
597 if (rv)
598 return rv;
599
600 req = &cep->mpa.hdr;
601
602 version = __mpa_rr_revision(req->params.bits);
603 pd_len = be16_to_cpu(req->params.pd_len);
604
605 if (version > MPA_REVISION_2)
606
607 return -EPROTO;
608
609 if (memcmp(req->key, MPA_KEY_REQ, 16))
610 return -EPROTO;
611
612
613 memcpy(req->key, MPA_KEY_REP, 16);
614
615 if (version == MPA_REVISION_2 &&
616 (req->params.bits & MPA_RR_FLAG_ENHANCED)) {
617
618
619
620
621
622 if (pd_len < sizeof(struct mpa_v2_data))
623 goto reject_conn;
624
625 cep->enhanced_rdma_conn_est = true;
626 }
627
628
629 if (req->params.bits & MPA_RR_FLAG_MARKERS)
630 goto reject_conn;
631
632 if (req->params.bits & MPA_RR_FLAG_CRC) {
633
634
635
636
637
638
639 if (!mpa_crc_required && mpa_crc_strict)
640 goto reject_conn;
641
642
643 if (mpa_crc_required)
644 req->params.bits |= MPA_RR_FLAG_CRC;
645 }
646 if (cep->enhanced_rdma_conn_est) {
647 struct mpa_v2_data *v2 = (struct mpa_v2_data *)cep->mpa.pdata;
648
649
650
651
652
653
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);
658
659
660 cep->mpa.v2_ctrl.ird = htons(cep->ird);
661 cep->mpa.v2_ctrl.ord = htons(cep->ord);
662
663
664
665
666
667
668
669
670
671 if (v2->ird & MPA_V2_PEER_TO_PEER) {
672 cep->mpa.v2_ctrl.ird |= MPA_V2_PEER_TO_PEER;
673
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;
678 else
679 cep->mpa.v2_ctrl.ord |= MPA_V2_RDMA_WRITE_RTR;
680 }
681 }
682
683 cep->state = SIW_EPSTATE_RECVD_MPAREQ;
684
685
686 siw_cep_get(cep);
687 rv = siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REQUEST, 0);
688 if (rv)
689 siw_cep_put(cep);
690
691 return rv;
692
693reject_conn:
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);
698
699 req->params.bits &= ~MPA_RR_FLAG_MARKERS;
700 req->params.bits |= MPA_RR_FLAG_REJECT;
701
702 if (!mpa_crc_required && mpa_crc_strict)
703 req->params.bits &= ~MPA_RR_FLAG_CRC;
704
705 if (pd_len)
706 kfree(cep->mpa.pdata);
707
708 cep->mpa.pdata = NULL;
709
710 siw_send_mpareqrep(cep, NULL, 0);
711
712 return -EOPNOTSUPP;
713}
714
715static int siw_proc_mpareply(struct siw_cep *cep)
716{
717 struct siw_qp_attrs qp_attrs;
718 enum siw_qp_attr_mask qp_attr_mask;
719 struct siw_qp *qp = cep->qp;
720 struct mpa_rr *rep;
721 int rv;
722 u16 rep_ord;
723 u16 rep_ird;
724 bool ird_insufficient = false;
725 enum mpa_v2_ctrl mpa_p2p_mode = MPA_V2_RDMA_NO_RTR;
726
727 rv = siw_recv_mpa_rr(cep);
728 if (rv != -EAGAIN)
729 siw_cancel_mpatimer(cep);
730 if (rv)
731 goto out_err;
732
733 rep = &cep->mpa.hdr;
734
735 if (__mpa_rr_revision(rep->params.bits) > MPA_REVISION_2) {
736
737 rv = -EPROTO;
738 goto out_err;
739 }
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);
744 rv = -EPROTO;
745 goto out_err;
746 }
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);
750
751 return -ECONNRESET;
752 }
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;
756 }
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);
765
766 siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY, -ECONNREFUSED);
767
768 return -EINVAL;
769 }
770 if (cep->enhanced_rdma_conn_est) {
771 struct mpa_v2_data *v2;
772
773 if (__mpa_rr_revision(rep->params.bits) < MPA_REVISION_2 ||
774 !(rep->params.bits & MPA_RR_FLAG_ENHANCED)) {
775
776
777
778
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 ?
782 1 :
783 0);
784
785 siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY,
786 -ECONNRESET);
787 return -EINVAL;
788 }
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;
792
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",
797 cep->ird, rep_ord,
798 cep->sdev->attrs.max_ord);
799 ird_insufficient = true;
800 }
801 if (cep->ord > rep_ird && relaxed_ird_negotiation == false) {
802 siw_dbg_cep(cep, "ord %d, rep_ird %d\n", cep->ord,
803 rep_ird);
804 ird_insufficient = true;
805 }
806
807
808
809
810 cep->ird = rep_ord;
811 cep->ord = rep_ird;
812
813 if (ird_insufficient) {
814
815
816
817
818 siw_init_terminate(qp, TERM_ERROR_LAYER_LLP,
819 LLP_ETYPE_MPA,
820 LLP_ECODE_INSUFFICIENT_IRD, 0);
821 siw_send_terminate(qp);
822 rv = -ENOMEM;
823 goto out_err;
824 }
825 if (cep->mpa.v2_ctrl_req.ird & MPA_V2_PEER_TO_PEER)
826 mpa_p2p_mode =
827 cep->mpa.v2_ctrl_req.ord &
828 (MPA_V2_RDMA_WRITE_RTR | MPA_V2_RDMA_READ_RTR);
829
830
831
832
833 if (mpa_p2p_mode != MPA_V2_RDMA_NO_RTR) {
834 if ((mpa_p2p_mode & v2->ord) == 0) {
835
836
837
838
839 siw_dbg_cep(cep,
840 "rtr mode: req %2x, got %2x\n",
841 mpa_p2p_mode,
842 v2->ord & (MPA_V2_RDMA_WRITE_RTR |
843 MPA_V2_RDMA_READ_RTR));
844
845 siw_init_terminate(qp, TERM_ERROR_LAYER_LLP,
846 LLP_ETYPE_MPA,
847 LLP_ECODE_NO_MATCHING_RTR,
848 0);
849 siw_send_terminate(qp);
850 rv = -EPROTO;
851 goto out_err;
852 }
853 mpa_p2p_mode = v2->ord & (MPA_V2_RDMA_WRITE_RTR |
854 MPA_V2_RDMA_READ_RTR);
855 }
856 }
857 memset(&qp_attrs, 0, sizeof(qp_attrs));
858
859 if (rep->params.bits & MPA_RR_FLAG_CRC)
860 qp_attrs.flags = SIW_MPA_CRC;
861
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;
866
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;
869
870
871 down_write(&qp->state_lock);
872 if (qp->attrs.state > SIW_QP_STATE_RTR) {
873 rv = -EINVAL;
874 up_write(&qp->state_lock);
875 goto out_err;
876 }
877 rv = siw_qp_modify(qp, &qp_attrs, qp_attr_mask);
878
879 siw_qp_socket_assoc(cep, qp);
880
881 up_write(&qp->state_lock);
882
883
884 if (mpa_p2p_mode != MPA_V2_RDMA_NO_RTR) {
885 rv = siw_qp_mpa_rts(qp, mpa_p2p_mode);
886 if (rv)
887 goto out_err;
888 }
889 if (!rv) {
890 rv = siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY, 0);
891 if (!rv)
892 cep->state = SIW_EPSTATE_RDMA_MODE;
893
894 return 0;
895 }
896
897out_err:
898 siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY, -EINVAL);
899
900 return rv;
901}
902
903
904
905
906
907static void siw_accept_newconn(struct siw_cep *cep)
908{
909 struct socket *s = cep->sock;
910 struct socket *new_s = NULL;
911 struct siw_cep *new_cep = NULL;
912 int rv = 0;
913
914 if (cep->state != SIW_EPSTATE_LISTENING)
915 goto error;
916
917 new_cep = siw_cep_alloc(cep->sdev);
918 if (!new_cep)
919 goto error;
920
921
922
923
924
925
926 if (siw_cm_alloc_work(new_cep, 4) != 0)
927 goto error;
928
929
930
931
932
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;
937
938 rv = kernel_accept(s, &new_s, O_NONBLOCK);
939 if (rv != 0) {
940
941
942
943 siw_dbg_cep(cep, "kernel_accept() error: %d\n", rv);
944 goto error;
945 }
946 new_cep->sock = new_s;
947 siw_cep_get(new_cep);
948 new_s->sk->sk_user_data = new_cep;
949
950 if (siw_tcp_nagle == false)
951 tcp_sock_set_nodelay(new_s->sk);
952 new_cep->state = SIW_EPSTATE_AWAIT_MPAREQ;
953
954 rv = siw_cm_queue_work(new_cep, SIW_CM_WORK_MPATIMEOUT);
955 if (rv)
956 goto error;
957
958
959
960 new_cep->listen_cep = cep;
961 siw_cep_get(cep);
962
963 if (atomic_read(&new_s->sk->sk_rmem_alloc)) {
964
965
966
967 siw_dbg_cep(cep, "immediate mpa request\n");
968
969 siw_cep_set_inuse(new_cep);
970 rv = siw_proc_mpareq(new_cep);
971 siw_cep_set_free(new_cep);
972
973 if (rv != -EAGAIN) {
974 siw_cep_put(cep);
975 new_cep->listen_cep = NULL;
976 if (rv)
977 goto error;
978 }
979 }
980 return;
981
982error:
983 if (new_cep)
984 siw_cep_put(new_cep);
985
986 if (new_s) {
987 siw_socket_disassoc(new_s);
988 sock_release(new_s);
989 new_cep->sock = NULL;
990 }
991 siw_dbg_cep(cep, "error %d\n", rv);
992}
993
994static void siw_cm_work_handler(struct work_struct *w)
995{
996 struct siw_cm_work *work;
997 struct siw_cep *cep;
998 int release_cep = 0, rv = 0;
999
1000 work = container_of(w, struct siw_cm_work, work.work);
1001 cep = work->cep;
1002
1003 siw_dbg_cep(cep, "[QP %u]: work type: %d, state %d\n",
1004 cep->qp ? qp_id(cep->qp) : UINT_MAX,
1005 work->type, cep->state);
1006
1007 siw_cep_set_inuse(cep);
1008
1009 switch (work->type) {
1010 case SIW_CM_WORK_ACCEPT:
1011 siw_accept_newconn(cep);
1012 break;
1013
1014 case SIW_CM_WORK_READ_MPAHDR:
1015 if (cep->state == SIW_EPSTATE_AWAIT_MPAREQ) {
1016 if (cep->listen_cep) {
1017 siw_cep_set_inuse(cep->listen_cep);
1018
1019 if (cep->listen_cep->state ==
1020 SIW_EPSTATE_LISTENING)
1021 rv = siw_proc_mpareq(cep);
1022 else
1023 rv = -EFAULT;
1024
1025 siw_cep_set_free(cep->listen_cep);
1026
1027 if (rv != -EAGAIN) {
1028 siw_cep_put(cep->listen_cep);
1029 cep->listen_cep = NULL;
1030 if (rv)
1031 siw_cep_put(cep);
1032 }
1033 }
1034 } else if (cep->state == SIW_EPSTATE_AWAIT_MPAREP) {
1035 rv = siw_proc_mpareply(cep);
1036 } else {
1037
1038
1039
1040
1041
1042 if (cep->state == SIW_EPSTATE_RDMA_MODE) {
1043 cep->sock->sk->sk_data_ready(cep->sock->sk);
1044 siw_dbg_cep(cep, "already in RDMA mode");
1045 } else {
1046 siw_dbg_cep(cep, "out of state: %d\n",
1047 cep->state);
1048 }
1049 }
1050 if (rv && rv != -EAGAIN)
1051 release_cep = 1;
1052 break;
1053
1054 case SIW_CM_WORK_CLOSE_LLP:
1055
1056
1057
1058 if (cep->qp && cep->qp->term_info.valid)
1059 siw_send_terminate(cep->qp);
1060
1061 if (cep->cm_id)
1062 siw_cm_upcall(cep, IW_CM_EVENT_CLOSE, 0);
1063
1064 release_cep = 1;
1065 break;
1066
1067 case SIW_CM_WORK_PEER_CLOSE:
1068 if (cep->cm_id) {
1069 if (cep->state == SIW_EPSTATE_AWAIT_MPAREP) {
1070
1071
1072
1073 siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY,
1074 -ECONNRESET);
1075 } else if (cep->state == SIW_EPSTATE_RDMA_MODE) {
1076
1077
1078
1079
1080 siw_cm_upcall(cep, IW_CM_EVENT_DISCONNECT, 0);
1081 siw_cm_upcall(cep, IW_CM_EVENT_CLOSE, 0);
1082 }
1083
1084
1085
1086
1087 } else {
1088 if (cep->state == SIW_EPSTATE_RECVD_MPAREQ) {
1089
1090
1091
1092 siw_dbg_cep(cep,
1093 "mpa req recvd, wait for ULP\n");
1094 } else if (cep->state == SIW_EPSTATE_AWAIT_MPAREQ) {
1095
1096
1097
1098 siw_dbg_cep(cep, "no mpareq: drop listener\n");
1099 siw_cep_put(cep->listen_cep);
1100 cep->listen_cep = NULL;
1101 }
1102 }
1103 release_cep = 1;
1104 break;
1105
1106 case SIW_CM_WORK_MPATIMEOUT:
1107 cep->mpa_timer = NULL;
1108
1109 if (cep->state == SIW_EPSTATE_AWAIT_MPAREP) {
1110
1111
1112
1113
1114
1115 cep->mpa.hdr.params.pd_len = 0;
1116
1117 if (cep->cm_id)
1118 siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY,
1119 -ETIMEDOUT);
1120 release_cep = 1;
1121
1122 } else if (cep->state == SIW_EPSTATE_AWAIT_MPAREQ) {
1123
1124
1125
1126 if (cep->listen_cep) {
1127 siw_cep_put(cep->listen_cep);
1128 cep->listen_cep = NULL;
1129 }
1130 release_cep = 1;
1131 }
1132 break;
1133
1134 default:
1135 WARN(1, "Undefined CM work type: %d\n", work->type);
1136 }
1137 if (release_cep) {
1138 siw_dbg_cep(cep,
1139 "release: timer=%s, QP[%u]\n",
1140 cep->mpa_timer ? "y" : "n",
1141 cep->qp ? qp_id(cep->qp) : UINT_MAX);
1142
1143 siw_cancel_mpatimer(cep);
1144
1145 cep->state = SIW_EPSTATE_CLOSED;
1146
1147 if (cep->qp) {
1148 struct siw_qp *qp = cep->qp;
1149
1150
1151
1152
1153 siw_qp_get(qp);
1154 siw_cep_set_free(cep);
1155
1156 siw_qp_llp_close(qp);
1157 siw_qp_put(qp);
1158
1159 siw_cep_set_inuse(cep);
1160 cep->qp = NULL;
1161 siw_qp_put(qp);
1162 }
1163 if (cep->sock) {
1164 siw_socket_disassoc(cep->sock);
1165 sock_release(cep->sock);
1166 cep->sock = NULL;
1167 }
1168 if (cep->cm_id) {
1169 cep->cm_id->rem_ref(cep->cm_id);
1170 cep->cm_id = NULL;
1171 siw_cep_put(cep);
1172 }
1173 }
1174 siw_cep_set_free(cep);
1175 siw_put_work(work);
1176 siw_cep_put(cep);
1177}
1178
1179static struct workqueue_struct *siw_cm_wq;
1180
1181int siw_cm_queue_work(struct siw_cep *cep, enum siw_work_type type)
1182{
1183 struct siw_cm_work *work = siw_get_work(cep);
1184 unsigned long delay = 0;
1185
1186 if (!work) {
1187 siw_dbg_cep(cep, "failed with no work available\n");
1188 return -ENOMEM;
1189 }
1190 work->type = type;
1191 work->cep = cep;
1192
1193 siw_cep_get(cep);
1194
1195 INIT_DELAYED_WORK(&work->work, siw_cm_work_handler);
1196
1197 if (type == SIW_CM_WORK_MPATIMEOUT) {
1198 cep->mpa_timer = work;
1199
1200 if (cep->state == SIW_EPSTATE_AWAIT_MPAREP)
1201 delay = MPAREQ_TIMEOUT;
1202 else
1203 delay = MPAREP_TIMEOUT;
1204 }
1205 siw_dbg_cep(cep, "[QP %u]: work type: %d, timeout %lu\n",
1206 cep->qp ? qp_id(cep->qp) : -1, type, delay);
1207
1208 queue_delayed_work(siw_cm_wq, &work->work, delay);
1209
1210 return 0;
1211}
1212
1213static void siw_cm_llp_data_ready(struct sock *sk)
1214{
1215 struct siw_cep *cep;
1216
1217 read_lock(&sk->sk_callback_lock);
1218
1219 cep = sk_to_cep(sk);
1220 if (!cep)
1221 goto out;
1222
1223 siw_dbg_cep(cep, "state: %d\n", cep->state);
1224
1225 switch (cep->state) {
1226 case SIW_EPSTATE_RDMA_MODE:
1227 case SIW_EPSTATE_LISTENING:
1228 break;
1229
1230 case SIW_EPSTATE_AWAIT_MPAREQ:
1231 case SIW_EPSTATE_AWAIT_MPAREP:
1232 siw_cm_queue_work(cep, SIW_CM_WORK_READ_MPAHDR);
1233 break;
1234
1235 default:
1236 siw_dbg_cep(cep, "unexpected data, state %d\n", cep->state);
1237 break;
1238 }
1239out:
1240 read_unlock(&sk->sk_callback_lock);
1241}
1242
1243static void siw_cm_llp_write_space(struct sock *sk)
1244{
1245 struct siw_cep *cep = sk_to_cep(sk);
1246
1247 if (cep)
1248 siw_dbg_cep(cep, "state: %d\n", cep->state);
1249}
1250
1251static void siw_cm_llp_error_report(struct sock *sk)
1252{
1253 struct siw_cep *cep = sk_to_cep(sk);
1254
1255 if (cep) {
1256 siw_dbg_cep(cep, "error %d, socket state: %d, cep state: %d\n",
1257 sk->sk_err, sk->sk_state, cep->state);
1258 cep->sk_error_report(sk);
1259 }
1260}
1261
1262static void siw_cm_llp_state_change(struct sock *sk)
1263{
1264 struct siw_cep *cep;
1265 void (*orig_state_change)(struct sock *s);
1266
1267 read_lock(&sk->sk_callback_lock);
1268
1269 cep = sk_to_cep(sk);
1270 if (!cep) {
1271
1272 read_unlock(&sk->sk_callback_lock);
1273 return;
1274 }
1275 orig_state_change = cep->sk_state_change;
1276
1277 siw_dbg_cep(cep, "state: %d\n", cep->state);
1278
1279 switch (sk->sk_state) {
1280 case TCP_ESTABLISHED:
1281
1282
1283
1284
1285 siw_cm_queue_work(cep, SIW_CM_WORK_ACCEPT);
1286 break;
1287
1288 case TCP_CLOSE:
1289 case TCP_CLOSE_WAIT:
1290 if (cep->qp)
1291 cep->qp->tx_ctx.tx_suspend = 1;
1292 siw_cm_queue_work(cep, SIW_CM_WORK_PEER_CLOSE);
1293 break;
1294
1295 default:
1296 siw_dbg_cep(cep, "unexpected socket state %d\n", sk->sk_state);
1297 }
1298 read_unlock(&sk->sk_callback_lock);
1299 orig_state_change(sk);
1300}
1301
1302static int kernel_bindconnect(struct socket *s, struct sockaddr *laddr,
1303 struct sockaddr *raddr)
1304{
1305 int rv, flags = 0;
1306 size_t size = laddr->sa_family == AF_INET ?
1307 sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
1308
1309
1310
1311
1312 sock_set_reuseaddr(s->sk);
1313
1314 rv = s->ops->bind(s, laddr, size);
1315 if (rv < 0)
1316 return rv;
1317
1318 rv = s->ops->connect(s, raddr, size, flags);
1319
1320 return rv < 0 ? rv : 0;
1321}
1322
1323int siw_connect(struct iw_cm_id *id, struct iw_cm_conn_param *params)
1324{
1325 struct siw_device *sdev = to_siw_dev(id->device);
1326 struct siw_qp *qp;
1327 struct siw_cep *cep = NULL;
1328 struct socket *s = NULL;
1329 struct sockaddr *laddr = (struct sockaddr *)&id->local_addr,
1330 *raddr = (struct sockaddr *)&id->remote_addr;
1331 bool p2p_mode = peer_to_peer, v4 = true;
1332 u16 pd_len = params->private_data_len;
1333 int version = mpa_version, rv;
1334
1335 if (pd_len > MPA_MAX_PRIVDATA)
1336 return -EINVAL;
1337
1338 if (params->ird > sdev->attrs.max_ird ||
1339 params->ord > sdev->attrs.max_ord)
1340 return -ENOMEM;
1341
1342 if (laddr->sa_family == AF_INET6)
1343 v4 = false;
1344 else if (laddr->sa_family != AF_INET)
1345 return -EAFNOSUPPORT;
1346
1347
1348
1349
1350
1351
1352 if ((v4 && to_sockaddr_in(id->remote_addr).sin_port != 0) ||
1353 to_sockaddr_in6(id->remote_addr).sin6_port != 0)
1354 raddr = (struct sockaddr *)&id->m_remote_addr;
1355
1356 qp = siw_qp_id2obj(sdev, params->qpn);
1357 if (!qp) {
1358 WARN(1, "[QP %u] does not exist\n", params->qpn);
1359 rv = -EINVAL;
1360 goto error;
1361 }
1362 siw_dbg_qp(qp, "pd_len %d, laddr %pISp, raddr %pISp\n", pd_len, laddr,
1363 raddr);
1364
1365 rv = sock_create(v4 ? AF_INET : AF_INET6, SOCK_STREAM, IPPROTO_TCP, &s);
1366 if (rv < 0)
1367 goto error;
1368
1369
1370
1371
1372
1373
1374 rv = kernel_bindconnect(s, laddr, raddr);
1375 if (rv != 0) {
1376 siw_dbg_qp(qp, "kernel_bindconnect: error %d\n", rv);
1377 goto error;
1378 }
1379 if (siw_tcp_nagle == false)
1380 tcp_sock_set_nodelay(s->sk);
1381 cep = siw_cep_alloc(sdev);
1382 if (!cep) {
1383 rv = -ENOMEM;
1384 goto error;
1385 }
1386 siw_cep_set_inuse(cep);
1387
1388
1389 siw_cep_get(cep);
1390 qp->cep = cep;
1391
1392
1393 cep->qp = qp;
1394
1395 id->add_ref(id);
1396 cep->cm_id = id;
1397
1398
1399
1400
1401
1402
1403 rv = siw_cm_alloc_work(cep, 4);
1404 if (rv != 0) {
1405 rv = -ENOMEM;
1406 goto error;
1407 }
1408 cep->ird = params->ird;
1409 cep->ord = params->ord;
1410
1411 if (p2p_mode && cep->ord == 0)
1412 cep->ord = 1;
1413
1414 cep->state = SIW_EPSTATE_CONNECTING;
1415
1416
1417
1418
1419 siw_cep_socket_assoc(cep, s);
1420
1421 cep->state = SIW_EPSTATE_AWAIT_MPAREP;
1422
1423
1424
1425
1426
1427 cep->mpa.hdr.params.bits = 0;
1428 if (version > MPA_REVISION_2) {
1429 pr_warn("Setting MPA version to %u\n", MPA_REVISION_2);
1430 version = MPA_REVISION_2;
1431
1432 mpa_version = MPA_REVISION_2;
1433 }
1434 __mpa_rr_set_revision(&cep->mpa.hdr.params.bits, version);
1435
1436 if (try_gso)
1437 cep->mpa.hdr.params.bits |= MPA_RR_FLAG_GSO_EXP;
1438
1439 if (mpa_crc_required)
1440 cep->mpa.hdr.params.bits |= MPA_RR_FLAG_CRC;
1441
1442
1443
1444
1445
1446
1447
1448 if (version == MPA_REVISION_2) {
1449 cep->enhanced_rdma_conn_est = true;
1450 cep->mpa.hdr.params.bits |= MPA_RR_FLAG_ENHANCED;
1451
1452 cep->mpa.v2_ctrl.ird = htons(cep->ird);
1453 cep->mpa.v2_ctrl.ord = htons(cep->ord);
1454
1455 if (p2p_mode) {
1456 cep->mpa.v2_ctrl.ird |= MPA_V2_PEER_TO_PEER;
1457 cep->mpa.v2_ctrl.ord |= rtr_type;
1458 }
1459
1460 cep->mpa.v2_ctrl_req.ird = cep->mpa.v2_ctrl.ird;
1461 cep->mpa.v2_ctrl_req.ord = cep->mpa.v2_ctrl.ord;
1462 }
1463 memcpy(cep->mpa.hdr.key, MPA_KEY_REQ, 16);
1464
1465 rv = siw_send_mpareqrep(cep, params->private_data, pd_len);
1466
1467
1468
1469 cep->mpa.hdr.params.pd_len = 0;
1470
1471 if (rv >= 0) {
1472 rv = siw_cm_queue_work(cep, SIW_CM_WORK_MPATIMEOUT);
1473 if (!rv) {
1474 siw_dbg_cep(cep, "[QP %u]: exit\n", qp_id(qp));
1475 siw_cep_set_free(cep);
1476 return 0;
1477 }
1478 }
1479error:
1480 siw_dbg(id->device, "failed: %d\n", rv);
1481
1482 if (cep) {
1483 siw_socket_disassoc(s);
1484 sock_release(s);
1485 cep->sock = NULL;
1486
1487 cep->qp = NULL;
1488
1489 cep->cm_id = NULL;
1490 id->rem_ref(id);
1491 siw_cep_put(cep);
1492
1493 qp->cep = NULL;
1494 siw_cep_put(cep);
1495
1496 cep->state = SIW_EPSTATE_CLOSED;
1497
1498 siw_cep_set_free(cep);
1499
1500 siw_cep_put(cep);
1501
1502 } else if (s) {
1503 sock_release(s);
1504 }
1505 if (qp)
1506 siw_qp_put(qp);
1507
1508 return rv;
1509}
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525int siw_accept(struct iw_cm_id *id, struct iw_cm_conn_param *params)
1526{
1527 struct siw_device *sdev = to_siw_dev(id->device);
1528 struct siw_cep *cep = (struct siw_cep *)id->provider_data;
1529 struct siw_qp *qp;
1530 struct siw_qp_attrs qp_attrs;
1531 int rv, max_priv_data = MPA_MAX_PRIVDATA;
1532 bool wait_for_peer_rts = false;
1533
1534 siw_cep_set_inuse(cep);
1535 siw_cep_put(cep);
1536
1537
1538 if (cep->mpa.hdr.params.pd_len) {
1539 cep->mpa.hdr.params.pd_len = 0;
1540 kfree(cep->mpa.pdata);
1541 cep->mpa.pdata = NULL;
1542 }
1543 siw_cancel_mpatimer(cep);
1544
1545 if (cep->state != SIW_EPSTATE_RECVD_MPAREQ) {
1546 siw_dbg_cep(cep, "out of state\n");
1547
1548 siw_cep_set_free(cep);
1549 siw_cep_put(cep);
1550
1551 return -ECONNRESET;
1552 }
1553 qp = siw_qp_id2obj(sdev, params->qpn);
1554 if (!qp) {
1555 WARN(1, "[QP %d] does not exist\n", params->qpn);
1556 siw_cep_set_free(cep);
1557 siw_cep_put(cep);
1558
1559 return -EINVAL;
1560 }
1561 down_write(&qp->state_lock);
1562 if (qp->attrs.state > SIW_QP_STATE_RTR) {
1563 rv = -EINVAL;
1564 up_write(&qp->state_lock);
1565 goto error;
1566 }
1567 siw_dbg_cep(cep, "[QP %d]\n", params->qpn);
1568
1569 if (try_gso && cep->mpa.hdr.params.bits & MPA_RR_FLAG_GSO_EXP) {
1570 siw_dbg_cep(cep, "peer allows GSO on TX\n");
1571 qp->tx_ctx.gso_seg_limit = 0;
1572 }
1573 if (params->ord > sdev->attrs.max_ord ||
1574 params->ird > sdev->attrs.max_ird) {
1575 siw_dbg_cep(
1576 cep,
1577 "[QP %u]: ord %d (max %d), ird %d (max %d)\n",
1578 qp_id(qp), params->ord, sdev->attrs.max_ord,
1579 params->ird, sdev->attrs.max_ird);
1580 rv = -EINVAL;
1581 up_write(&qp->state_lock);
1582 goto error;
1583 }
1584 if (cep->enhanced_rdma_conn_est)
1585 max_priv_data -= sizeof(struct mpa_v2_data);
1586
1587 if (params->private_data_len > max_priv_data) {
1588 siw_dbg_cep(
1589 cep,
1590 "[QP %u]: private data length: %d (max %d)\n",
1591 qp_id(qp), params->private_data_len, max_priv_data);
1592 rv = -EINVAL;
1593 up_write(&qp->state_lock);
1594 goto error;
1595 }
1596 if (cep->enhanced_rdma_conn_est) {
1597 if (params->ord > cep->ord) {
1598 if (relaxed_ird_negotiation) {
1599 params->ord = cep->ord;
1600 } else {
1601 cep->ird = params->ird;
1602 cep->ord = params->ord;
1603 rv = -EINVAL;
1604 up_write(&qp->state_lock);
1605 goto error;
1606 }
1607 }
1608 if (params->ird < cep->ird) {
1609 if (relaxed_ird_negotiation &&
1610 cep->ird <= sdev->attrs.max_ird)
1611 params->ird = cep->ird;
1612 else {
1613 rv = -ENOMEM;
1614 up_write(&qp->state_lock);
1615 goto error;
1616 }
1617 }
1618 if (cep->mpa.v2_ctrl.ord &
1619 (MPA_V2_RDMA_WRITE_RTR | MPA_V2_RDMA_READ_RTR))
1620 wait_for_peer_rts = true;
1621
1622
1623
1624 cep->mpa.v2_ctrl.ord =
1625 htons(params->ord & MPA_IRD_ORD_MASK) |
1626 (cep->mpa.v2_ctrl.ord & ~MPA_V2_MASK_IRD_ORD);
1627 cep->mpa.v2_ctrl.ird =
1628 htons(params->ird & MPA_IRD_ORD_MASK) |
1629 (cep->mpa.v2_ctrl.ird & ~MPA_V2_MASK_IRD_ORD);
1630 }
1631 cep->ird = params->ird;
1632 cep->ord = params->ord;
1633
1634 cep->cm_id = id;
1635 id->add_ref(id);
1636
1637 memset(&qp_attrs, 0, sizeof(qp_attrs));
1638 qp_attrs.orq_size = cep->ord;
1639 qp_attrs.irq_size = cep->ird;
1640 qp_attrs.sk = cep->sock;
1641 if (cep->mpa.hdr.params.bits & MPA_RR_FLAG_CRC)
1642 qp_attrs.flags = SIW_MPA_CRC;
1643 qp_attrs.state = SIW_QP_STATE_RTS;
1644
1645 siw_dbg_cep(cep, "[QP%u]: moving to rts\n", qp_id(qp));
1646
1647
1648 siw_cep_get(cep);
1649 qp->cep = cep;
1650
1651
1652 cep->qp = qp;
1653
1654 cep->state = SIW_EPSTATE_RDMA_MODE;
1655
1656
1657 rv = siw_qp_modify(qp, &qp_attrs,
1658 SIW_QP_ATTR_STATE | SIW_QP_ATTR_LLP_HANDLE |
1659 SIW_QP_ATTR_ORD | SIW_QP_ATTR_IRD |
1660 SIW_QP_ATTR_MPA);
1661 up_write(&qp->state_lock);
1662
1663 if (rv)
1664 goto error;
1665
1666 siw_dbg_cep(cep, "[QP %u]: send mpa reply, %d byte pdata\n",
1667 qp_id(qp), params->private_data_len);
1668
1669 rv = siw_send_mpareqrep(cep, params->private_data,
1670 params->private_data_len);
1671 if (rv != 0)
1672 goto error;
1673
1674 if (wait_for_peer_rts) {
1675 siw_sk_assign_rtr_upcalls(cep);
1676 } else {
1677 siw_qp_socket_assoc(cep, qp);
1678 rv = siw_cm_upcall(cep, IW_CM_EVENT_ESTABLISHED, 0);
1679 if (rv)
1680 goto error;
1681 }
1682 siw_cep_set_free(cep);
1683
1684 return 0;
1685error:
1686 siw_socket_disassoc(cep->sock);
1687 sock_release(cep->sock);
1688 cep->sock = NULL;
1689
1690 cep->state = SIW_EPSTATE_CLOSED;
1691
1692 if (cep->cm_id) {
1693 cep->cm_id->rem_ref(id);
1694 cep->cm_id = NULL;
1695 }
1696 if (qp->cep) {
1697 siw_cep_put(cep);
1698 qp->cep = NULL;
1699 }
1700 cep->qp = NULL;
1701 siw_qp_put(qp);
1702
1703 siw_cep_set_free(cep);
1704 siw_cep_put(cep);
1705
1706 return rv;
1707}
1708
1709
1710
1711
1712
1713
1714
1715int siw_reject(struct iw_cm_id *id, const void *pdata, u8 pd_len)
1716{
1717 struct siw_cep *cep = (struct siw_cep *)id->provider_data;
1718
1719 siw_cep_set_inuse(cep);
1720 siw_cep_put(cep);
1721
1722 siw_cancel_mpatimer(cep);
1723
1724 if (cep->state != SIW_EPSTATE_RECVD_MPAREQ) {
1725 siw_dbg_cep(cep, "out of state\n");
1726
1727 siw_cep_set_free(cep);
1728 siw_cep_put(cep);
1729
1730 return -ECONNRESET;
1731 }
1732 siw_dbg_cep(cep, "cep->state %d, pd_len %d\n", cep->state,
1733 pd_len);
1734
1735 if (__mpa_rr_revision(cep->mpa.hdr.params.bits) >= MPA_REVISION_1) {
1736 cep->mpa.hdr.params.bits |= MPA_RR_FLAG_REJECT;
1737 siw_send_mpareqrep(cep, pdata, pd_len);
1738 }
1739 siw_socket_disassoc(cep->sock);
1740 sock_release(cep->sock);
1741 cep->sock = NULL;
1742
1743 cep->state = SIW_EPSTATE_CLOSED;
1744
1745 siw_cep_set_free(cep);
1746 siw_cep_put(cep);
1747
1748 return 0;
1749}
1750
1751
1752
1753
1754
1755
1756
1757int siw_create_listen(struct iw_cm_id *id, int backlog)
1758{
1759 struct socket *s;
1760 struct siw_cep *cep = NULL;
1761 struct siw_device *sdev = to_siw_dev(id->device);
1762 int addr_family = id->local_addr.ss_family;
1763 int rv = 0;
1764
1765 if (addr_family != AF_INET && addr_family != AF_INET6)
1766 return -EAFNOSUPPORT;
1767
1768 rv = sock_create(addr_family, SOCK_STREAM, IPPROTO_TCP, &s);
1769 if (rv < 0)
1770 return rv;
1771
1772
1773
1774
1775 sock_set_reuseaddr(s->sk);
1776
1777 if (addr_family == AF_INET) {
1778 struct sockaddr_in *laddr = &to_sockaddr_in(id->local_addr);
1779
1780
1781 if (ipv4_is_zeronet(laddr->sin_addr.s_addr))
1782 s->sk->sk_bound_dev_if = sdev->netdev->ifindex;
1783
1784 rv = s->ops->bind(s, (struct sockaddr *)laddr,
1785 sizeof(struct sockaddr_in));
1786 } else {
1787 struct sockaddr_in6 *laddr = &to_sockaddr_in6(id->local_addr);
1788
1789
1790 if (ipv6_addr_any(&laddr->sin6_addr))
1791 s->sk->sk_bound_dev_if = sdev->netdev->ifindex;
1792
1793 rv = s->ops->bind(s, (struct sockaddr *)laddr,
1794 sizeof(struct sockaddr_in6));
1795 }
1796 if (rv) {
1797 siw_dbg(id->device, "socket bind error: %d\n", rv);
1798 goto error;
1799 }
1800 cep = siw_cep_alloc(sdev);
1801 if (!cep) {
1802 rv = -ENOMEM;
1803 goto error;
1804 }
1805 siw_cep_socket_assoc(cep, s);
1806
1807 rv = siw_cm_alloc_work(cep, backlog);
1808 if (rv) {
1809 siw_dbg(id->device,
1810 "alloc_work error %d, backlog %d\n",
1811 rv, backlog);
1812 goto error;
1813 }
1814 rv = s->ops->listen(s, backlog);
1815 if (rv) {
1816 siw_dbg(id->device, "listen error %d\n", rv);
1817 goto error;
1818 }
1819 cep->cm_id = id;
1820 id->add_ref(id);
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842 if (!id->provider_data) {
1843 id->provider_data =
1844 kmalloc(sizeof(struct list_head), GFP_KERNEL);
1845 if (!id->provider_data) {
1846 rv = -ENOMEM;
1847 goto error;
1848 }
1849 INIT_LIST_HEAD((struct list_head *)id->provider_data);
1850 }
1851 list_add_tail(&cep->listenq, (struct list_head *)id->provider_data);
1852 cep->state = SIW_EPSTATE_LISTENING;
1853
1854 siw_dbg(id->device, "Listen at laddr %pISp\n", &id->local_addr);
1855
1856 return 0;
1857
1858error:
1859 siw_dbg(id->device, "failed: %d\n", rv);
1860
1861 if (cep) {
1862 siw_cep_set_inuse(cep);
1863
1864 if (cep->cm_id) {
1865 cep->cm_id->rem_ref(cep->cm_id);
1866 cep->cm_id = NULL;
1867 }
1868 cep->sock = NULL;
1869 siw_socket_disassoc(s);
1870 cep->state = SIW_EPSTATE_CLOSED;
1871
1872 siw_cep_set_free(cep);
1873 siw_cep_put(cep);
1874 }
1875 sock_release(s);
1876
1877 return rv;
1878}
1879
1880static void siw_drop_listeners(struct iw_cm_id *id)
1881{
1882 struct list_head *p, *tmp;
1883
1884
1885
1886
1887
1888 list_for_each_safe(p, tmp, (struct list_head *)id->provider_data) {
1889 struct siw_cep *cep = list_entry(p, struct siw_cep, listenq);
1890
1891 list_del(p);
1892
1893 siw_dbg_cep(cep, "drop cep, state %d\n", cep->state);
1894
1895 siw_cep_set_inuse(cep);
1896
1897 if (cep->cm_id) {
1898 cep->cm_id->rem_ref(cep->cm_id);
1899 cep->cm_id = NULL;
1900 }
1901 if (cep->sock) {
1902 siw_socket_disassoc(cep->sock);
1903 sock_release(cep->sock);
1904 cep->sock = NULL;
1905 }
1906 cep->state = SIW_EPSTATE_CLOSED;
1907 siw_cep_set_free(cep);
1908 siw_cep_put(cep);
1909 }
1910}
1911
1912int siw_destroy_listen(struct iw_cm_id *id)
1913{
1914 if (!id->provider_data) {
1915 siw_dbg(id->device, "no cep(s)\n");
1916 return 0;
1917 }
1918 siw_drop_listeners(id);
1919 kfree(id->provider_data);
1920 id->provider_data = NULL;
1921
1922 return 0;
1923}
1924
1925int siw_cm_init(void)
1926{
1927
1928
1929
1930 siw_cm_wq = create_singlethread_workqueue("siw_cm_wq");
1931 if (!siw_cm_wq)
1932 return -ENOMEM;
1933
1934 return 0;
1935}
1936
1937void siw_cm_exit(void)
1938{
1939 if (siw_cm_wq) {
1940 flush_workqueue(siw_cm_wq);
1941 destroy_workqueue(siw_cm_wq);
1942 }
1943}
1944