]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/cxgbe/iw_cxgbe/cm.c
Sync to HEAD@r271160.
[FreeBSD/FreeBSD.git] / sys / dev / cxgbe / iw_cxgbe / cm.c
1 /*
2  * Copyright (c) 2009-2013 Chelsio, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_inet.h"
36
37 #ifdef TCP_OFFLOAD
38 #include <sys/types.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42 #include <sys/sockio.h>
43 #include <sys/taskqueue.h>
44 #include <netinet/in.h>
45 #include <net/route.h>
46
47 #include <netinet/in_systm.h>
48 #include <netinet/in_pcb.h>
49 #include <netinet/ip.h>
50 #include <netinet/ip_var.h>
51 #include <netinet/tcp_var.h>
52 #include <netinet/tcp.h>
53 #include <netinet/tcpip.h>
54
55 #include <netinet/toecore.h>
56
57 struct sge_iq;
58 struct rss_header;
59 #include <linux/types.h>
60 #include "offload.h"
61 #include "tom/t4_tom.h"
62
63 #define TOEPCB(so)  ((struct toepcb *)(so_sototcpcb((so))->t_toe))
64
65 #include "iw_cxgbe.h"
66 #include <linux/module.h>
67 #include <linux/workqueue.h>
68 #include <linux/notifier.h>
69 #include <linux/inetdevice.h>
70 #include <linux/if_vlan.h>
71 #include <net/netevent.h>
72
73 static spinlock_t req_lock;
74 static TAILQ_HEAD(c4iw_ep_list, c4iw_ep_common) req_list;
75 static struct work_struct c4iw_task;
76 static struct workqueue_struct *c4iw_taskq;
77 static LIST_HEAD(timeout_list);
78 static spinlock_t timeout_lock;
79
80 static void process_req(struct work_struct *ctx);
81 static void start_ep_timer(struct c4iw_ep *ep);
82 static void stop_ep_timer(struct c4iw_ep *ep);
83 static int set_tcpinfo(struct c4iw_ep *ep);
84 static enum c4iw_ep_state state_read(struct c4iw_ep_common *epc);
85 static void __state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state tostate);
86 static void state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state tostate);
87 static void *alloc_ep(int size, gfp_t flags);
88 void __free_ep(struct c4iw_ep_common *epc);
89 static struct rtentry * find_route(__be32 local_ip, __be32 peer_ip, __be16 local_port,
90                 __be16 peer_port, u8 tos);
91 static int close_socket(struct c4iw_ep_common *epc, int close);
92 static int shutdown_socket(struct c4iw_ep_common *epc);
93 static void abort_socket(struct c4iw_ep *ep);
94 static void send_mpa_req(struct c4iw_ep *ep);
95 static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen);
96 static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen);
97 static void close_complete_upcall(struct c4iw_ep *ep);
98 static int abort_connection(struct c4iw_ep *ep);
99 static void peer_close_upcall(struct c4iw_ep *ep);
100 static void peer_abort_upcall(struct c4iw_ep *ep);
101 static void connect_reply_upcall(struct c4iw_ep *ep, int status);
102 static void connect_request_upcall(struct c4iw_ep *ep);
103 static void established_upcall(struct c4iw_ep *ep);
104 static void process_mpa_reply(struct c4iw_ep *ep);
105 static void process_mpa_request(struct c4iw_ep *ep);
106 static void process_peer_close(struct c4iw_ep *ep);
107 static void process_conn_error(struct c4iw_ep *ep);
108 static void process_close_complete(struct c4iw_ep *ep);
109 static void ep_timeout(unsigned long arg);
110 static void init_sock(struct c4iw_ep_common *epc);
111 static void process_data(struct c4iw_ep *ep);
112 static void process_connected(struct c4iw_ep *ep);
113 static struct socket * dequeue_socket(struct socket *head, struct sockaddr_in **remote, struct c4iw_ep *child_ep);
114 static void process_newconn(struct c4iw_ep *parent_ep);
115 static int c4iw_so_upcall(struct socket *so, void *arg, int waitflag);
116 static void process_socket_event(struct c4iw_ep *ep);
117 static void release_ep_resources(struct c4iw_ep *ep);
118
119 #define START_EP_TIMER(ep) \
120     do { \
121             CTR3(KTR_IW_CXGBE, "start_ep_timer (%s:%d) ep %p", \
122                 __func__, __LINE__, (ep)); \
123             start_ep_timer(ep); \
124     } while (0)
125
126 #define STOP_EP_TIMER(ep) \
127     do { \
128             CTR3(KTR_IW_CXGBE, "stop_ep_timer (%s:%d) ep %p", \
129                 __func__, __LINE__, (ep)); \
130             stop_ep_timer(ep); \
131     } while (0)
132
133 #ifdef KTR
134 static char *states[] = {
135         "idle",
136         "listen",
137         "connecting",
138         "mpa_wait_req",
139         "mpa_req_sent",
140         "mpa_req_rcvd",
141         "mpa_rep_sent",
142         "fpdu_mode",
143         "aborting",
144         "closing",
145         "moribund",
146         "dead",
147         NULL,
148 };
149 #endif
150
151 static void
152 process_req(struct work_struct *ctx)
153 {
154         struct c4iw_ep_common *epc;
155
156         spin_lock(&req_lock);
157         while (!TAILQ_EMPTY(&req_list)) {
158                 epc = TAILQ_FIRST(&req_list);
159                 TAILQ_REMOVE(&req_list, epc, entry);
160                 epc->entry.tqe_prev = NULL;
161                 spin_unlock(&req_lock);
162                 if (epc->so)
163                         process_socket_event((struct c4iw_ep *)epc);
164                 c4iw_put_ep(epc);
165                 spin_lock(&req_lock);
166         }
167         spin_unlock(&req_lock);
168 }
169
170 /*
171  * XXX: doesn't belong here in the iWARP driver.
172  * XXX: assumes that the connection was offloaded by cxgbe/t4_tom if TF_TOE is
173  *      set.  Is this a valid assumption for active open?
174  */
175 static int
176 set_tcpinfo(struct c4iw_ep *ep)
177 {
178         struct socket *so = ep->com.so;
179         struct inpcb *inp = sotoinpcb(so);
180         struct tcpcb *tp;
181         struct toepcb *toep;
182         int rc = 0;
183
184         INP_WLOCK(inp);
185         tp = intotcpcb(inp);
186         if ((tp->t_flags & TF_TOE) == 0) {
187                 rc = EINVAL;
188                 log(LOG_ERR, "%s: connection not offloaded (so %p, ep %p)\n",
189                     __func__, so, ep);
190                 goto done;
191         }
192         toep = TOEPCB(so);
193
194         ep->hwtid = toep->tid;
195         ep->snd_seq = tp->snd_nxt;
196         ep->rcv_seq = tp->rcv_nxt;
197         ep->emss = max(tp->t_maxseg, 128);
198 done:
199         INP_WUNLOCK(inp);
200         return (rc);
201
202 }
203
204 static struct rtentry *
205 find_route(__be32 local_ip, __be32 peer_ip, __be16 local_port,
206                 __be16 peer_port, u8 tos)
207 {
208         struct route iproute;
209         struct sockaddr_in *dst = (struct sockaddr_in *)&iproute.ro_dst;
210
211         CTR5(KTR_IW_CXGBE, "%s:frtB %x, %x, %d, %d", __func__, local_ip,
212             peer_ip, ntohs(local_port), ntohs(peer_port));
213         bzero(&iproute, sizeof iproute);
214         dst->sin_family = AF_INET;
215         dst->sin_len = sizeof *dst;
216         dst->sin_addr.s_addr = peer_ip;
217
218         rtalloc(&iproute);
219         CTR2(KTR_IW_CXGBE, "%s:frtE %p", __func__, (uint64_t)iproute.ro_rt);
220         return iproute.ro_rt;
221 }
222
223 static int
224 close_socket(struct c4iw_ep_common *epc, int close)
225 {
226         struct socket *so = epc->so;
227         int rc;
228
229         CTR4(KTR_IW_CXGBE, "%s: so %p, ep %p, state %s", __func__, epc, so,
230             states[epc->state]);
231
232         SOCK_LOCK(so);
233         soupcall_clear(so, SO_RCV);
234         SOCK_UNLOCK(so);
235
236         if (close)
237                 rc = soclose(so);
238         else
239                 rc = soshutdown(so, SHUT_WR | SHUT_RD);
240         epc->so = NULL;
241
242         return (rc);
243 }
244
245 static int
246 shutdown_socket(struct c4iw_ep_common *epc)
247 {
248
249         CTR4(KTR_IW_CXGBE, "%s: so %p, ep %p, state %s", __func__, epc->so, epc,
250             states[epc->state]);
251
252         return (soshutdown(epc->so, SHUT_WR));
253 }
254
255 static void
256 abort_socket(struct c4iw_ep *ep)
257 {
258         struct sockopt sopt;
259         int rc;
260         struct linger l;
261
262         CTR4(KTR_IW_CXGBE, "%s ep %p so %p state %s", __func__, ep, ep->com.so,
263             states[ep->com.state]);
264
265         l.l_onoff = 1;
266         l.l_linger = 0;
267
268         /* linger_time of 0 forces RST to be sent */
269         sopt.sopt_dir = SOPT_SET;
270         sopt.sopt_level = SOL_SOCKET;
271         sopt.sopt_name = SO_LINGER;
272         sopt.sopt_val = (caddr_t)&l;
273         sopt.sopt_valsize = sizeof l;
274         sopt.sopt_td = NULL;
275         rc = sosetopt(ep->com.so, &sopt);
276         if (rc) {
277                 log(LOG_ERR, "%s: can't set linger to 0, no RST! err %d\n",
278                     __func__, rc);
279         }
280 }
281
282 static void
283 process_peer_close(struct c4iw_ep *ep)
284 {
285         struct c4iw_qp_attributes attrs;
286         int disconnect = 1;
287         int release = 0;
288
289         CTR4(KTR_IW_CXGBE, "%s:ppcB ep %p so %p state %s", __func__, ep,
290             ep->com.so, states[ep->com.state]);
291
292         mutex_lock(&ep->com.mutex);
293         switch (ep->com.state) {
294
295                 case MPA_REQ_WAIT:
296                         CTR2(KTR_IW_CXGBE, "%s:ppc1 %p MPA_REQ_WAIT CLOSING",
297                             __func__, ep);
298                         __state_set(&ep->com, CLOSING);
299                         break;
300
301                 case MPA_REQ_SENT:
302                         CTR2(KTR_IW_CXGBE, "%s:ppc2 %p MPA_REQ_SENT CLOSING",
303                             __func__, ep);
304                         __state_set(&ep->com, DEAD);
305                         connect_reply_upcall(ep, -ECONNABORTED);
306
307                         disconnect = 0;
308                         STOP_EP_TIMER(ep);
309                         close_socket(&ep->com, 0);
310                         ep->com.cm_id->rem_ref(ep->com.cm_id);
311                         ep->com.cm_id = NULL;
312                         ep->com.qp = NULL;
313                         release = 1;
314                         break;
315
316                 case MPA_REQ_RCVD:
317
318                         /*
319                          * We're gonna mark this puppy DEAD, but keep
320                          * the reference on it until the ULP accepts or
321                          * rejects the CR.
322                          */
323                         CTR2(KTR_IW_CXGBE, "%s:ppc3 %p MPA_REQ_RCVD CLOSING",
324                             __func__, ep);
325                         __state_set(&ep->com, CLOSING);
326                         c4iw_get_ep(&ep->com);
327                         break;
328
329                 case MPA_REP_SENT:
330                         CTR2(KTR_IW_CXGBE, "%s:ppc4 %p MPA_REP_SENT CLOSING",
331                             __func__, ep);
332                         __state_set(&ep->com, CLOSING);
333                         break;
334
335                 case FPDU_MODE:
336                         CTR2(KTR_IW_CXGBE, "%s:ppc5 %p FPDU_MODE CLOSING",
337                             __func__, ep);
338                         START_EP_TIMER(ep);
339                         __state_set(&ep->com, CLOSING);
340                         attrs.next_state = C4IW_QP_STATE_CLOSING;
341                         c4iw_modify_qp(ep->com.dev, ep->com.qp,
342                                         C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
343                         peer_close_upcall(ep);
344                         break;
345
346                 case ABORTING:
347                         CTR2(KTR_IW_CXGBE, "%s:ppc6 %p ABORTING (disconn)",
348                             __func__, ep);
349                         disconnect = 0;
350                         break;
351
352                 case CLOSING:
353                         CTR2(KTR_IW_CXGBE, "%s:ppc7 %p CLOSING MORIBUND",
354                             __func__, ep);
355                         __state_set(&ep->com, MORIBUND);
356                         disconnect = 0;
357                         break;
358
359                 case MORIBUND:
360                         CTR2(KTR_IW_CXGBE, "%s:ppc8 %p MORIBUND DEAD", __func__,
361                             ep);
362                         STOP_EP_TIMER(ep);
363                         if (ep->com.cm_id && ep->com.qp) {
364                                 attrs.next_state = C4IW_QP_STATE_IDLE;
365                                 c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
366                                                 C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
367                         }
368                         close_socket(&ep->com, 0);
369                         close_complete_upcall(ep);
370                         __state_set(&ep->com, DEAD);
371                         release = 1;
372                         disconnect = 0;
373                         break;
374
375                 case DEAD:
376                         CTR2(KTR_IW_CXGBE, "%s:ppc9 %p DEAD (disconn)",
377                             __func__, ep);
378                         disconnect = 0;
379                         break;
380
381                 default:
382                         panic("%s: ep %p state %d", __func__, ep,
383                             ep->com.state);
384                         break;
385         }
386
387         mutex_unlock(&ep->com.mutex);
388
389         if (disconnect) {
390
391                 CTR2(KTR_IW_CXGBE, "%s:ppca %p", __func__, ep);
392                 c4iw_ep_disconnect(ep, 0, M_NOWAIT);
393         }
394         if (release) {
395
396                 CTR2(KTR_IW_CXGBE, "%s:ppcb %p", __func__, ep);
397                 c4iw_put_ep(&ep->com);
398         }
399         CTR2(KTR_IW_CXGBE, "%s:ppcE %p", __func__, ep);
400         return;
401 }
402
403 static void
404 process_conn_error(struct c4iw_ep *ep)
405 {
406         struct c4iw_qp_attributes attrs;
407         int ret;
408         int state;
409
410         state = state_read(&ep->com);
411         CTR5(KTR_IW_CXGBE, "%s:pceB ep %p so %p so->so_error %u state %s",
412             __func__, ep, ep->com.so, ep->com.so->so_error,
413             states[ep->com.state]);
414
415         switch (state) {
416
417                 case MPA_REQ_WAIT:
418                         STOP_EP_TIMER(ep);
419                         break;
420
421                 case MPA_REQ_SENT:
422                         STOP_EP_TIMER(ep);
423                         connect_reply_upcall(ep, -ECONNRESET);
424                         break;
425
426                 case MPA_REP_SENT:
427                         ep->com.rpl_err = ECONNRESET;
428                         CTR1(KTR_IW_CXGBE, "waking up ep %p", ep);
429                         break;
430
431                 case MPA_REQ_RCVD:
432
433                         /*
434                          * We're gonna mark this puppy DEAD, but keep
435                          * the reference on it until the ULP accepts or
436                          * rejects the CR.
437                          */
438                         c4iw_get_ep(&ep->com);
439                         break;
440
441                 case MORIBUND:
442                 case CLOSING:
443                         STOP_EP_TIMER(ep);
444                         /*FALLTHROUGH*/
445                 case FPDU_MODE:
446
447                         if (ep->com.cm_id && ep->com.qp) {
448
449                                 attrs.next_state = C4IW_QP_STATE_ERROR;
450                                 ret = c4iw_modify_qp(ep->com.qp->rhp,
451                                         ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
452                                         &attrs, 1);
453                                 if (ret)
454                                         log(LOG_ERR,
455                                                         "%s - qp <- error failed!\n",
456                                                         __func__);
457                         }
458                         peer_abort_upcall(ep);
459                         break;
460
461                 case ABORTING:
462                         break;
463
464                 case DEAD:
465                         CTR2(KTR_IW_CXGBE, "%s so_error %d IN DEAD STATE!!!!",
466                             __func__, ep->com.so->so_error);
467                         return;
468
469                 default:
470                         panic("%s: ep %p state %d", __func__, ep, state);
471                         break;
472         }
473
474         if (state != ABORTING) {
475
476                 CTR2(KTR_IW_CXGBE, "%s:pce1 %p", __func__, ep);
477                 close_socket(&ep->com, 0);
478                 state_set(&ep->com, DEAD);
479                 c4iw_put_ep(&ep->com);
480         }
481         CTR2(KTR_IW_CXGBE, "%s:pceE %p", __func__, ep);
482         return;
483 }
484
485 static void
486 process_close_complete(struct c4iw_ep *ep)
487 {
488         struct c4iw_qp_attributes attrs;
489         int release = 0;
490
491         CTR4(KTR_IW_CXGBE, "%s:pccB ep %p so %p state %s", __func__, ep,
492             ep->com.so, states[ep->com.state]);
493
494         /* The cm_id may be null if we failed to connect */
495         mutex_lock(&ep->com.mutex);
496
497         switch (ep->com.state) {
498
499                 case CLOSING:
500                         CTR2(KTR_IW_CXGBE, "%s:pcc1 %p CLOSING MORIBUND",
501                             __func__, ep);
502                         __state_set(&ep->com, MORIBUND);
503                         break;
504
505                 case MORIBUND:
506                         CTR2(KTR_IW_CXGBE, "%s:pcc1 %p MORIBUND DEAD", __func__,
507                             ep);
508                         STOP_EP_TIMER(ep);
509
510                         if ((ep->com.cm_id) && (ep->com.qp)) {
511
512                                 CTR2(KTR_IW_CXGBE, "%s:pcc2 %p QP_STATE_IDLE",
513                                     __func__, ep);
514                                 attrs.next_state = C4IW_QP_STATE_IDLE;
515                                 c4iw_modify_qp(ep->com.dev,
516                                                 ep->com.qp,
517                                                 C4IW_QP_ATTR_NEXT_STATE,
518                                                 &attrs, 1);
519                         }
520
521                         if (ep->parent_ep) {
522
523                                 CTR2(KTR_IW_CXGBE, "%s:pcc3 %p", __func__, ep);
524                                 close_socket(&ep->com, 1);
525                         }
526                         else {
527
528                                 CTR2(KTR_IW_CXGBE, "%s:pcc4 %p", __func__, ep);
529                                 close_socket(&ep->com, 0);
530                         }
531                         close_complete_upcall(ep);
532                         __state_set(&ep->com, DEAD);
533                         release = 1;
534                         break;
535
536                 case ABORTING:
537                         CTR2(KTR_IW_CXGBE, "%s:pcc5 %p ABORTING", __func__, ep);
538                         break;
539
540                 case DEAD:
541                 default:
542                         CTR2(KTR_IW_CXGBE, "%s:pcc6 %p DEAD", __func__, ep);
543                         panic("%s:pcc6 %p DEAD", __func__, ep);
544                         break;
545         }
546         mutex_unlock(&ep->com.mutex);
547
548         if (release) {
549
550                 CTR2(KTR_IW_CXGBE, "%s:pcc7 %p", __func__, ep);
551                 c4iw_put_ep(&ep->com);
552         }
553         CTR2(KTR_IW_CXGBE, "%s:pccE %p", __func__, ep);
554         return;
555 }
556
557 static void
558 init_sock(struct c4iw_ep_common *epc)
559 {
560         int rc;
561         struct sockopt sopt;
562         struct socket *so = epc->so;
563         int on = 1;
564
565         SOCK_LOCK(so);
566         soupcall_set(so, SO_RCV, c4iw_so_upcall, epc);
567         so->so_state |= SS_NBIO;
568         SOCK_UNLOCK(so);
569         sopt.sopt_dir = SOPT_SET;
570         sopt.sopt_level = IPPROTO_TCP;
571         sopt.sopt_name = TCP_NODELAY;
572         sopt.sopt_val = (caddr_t)&on;
573         sopt.sopt_valsize = sizeof on;
574         sopt.sopt_td = NULL;
575         rc = sosetopt(so, &sopt);
576         if (rc) {
577                 log(LOG_ERR, "%s: can't set TCP_NODELAY on so %p (%d)\n",
578                     __func__, so, rc);
579         }
580 }
581
582 static void
583 process_data(struct c4iw_ep *ep)
584 {
585         struct sockaddr_in *local, *remote;
586
587         CTR5(KTR_IW_CXGBE, "%s: so %p, ep %p, state %s, sb_cc %d", __func__,
588             ep->com.so, ep, states[ep->com.state], ep->com.so->so_rcv.sb_cc);
589
590         switch (state_read(&ep->com)) {
591         case MPA_REQ_SENT:
592                 process_mpa_reply(ep);
593                 break;
594         case MPA_REQ_WAIT:
595                 in_getsockaddr(ep->com.so, (struct sockaddr **)&local);
596                 in_getpeeraddr(ep->com.so, (struct sockaddr **)&remote);
597                 ep->com.local_addr = *local;
598                 ep->com.remote_addr = *remote;
599                 free(local, M_SONAME);
600                 free(remote, M_SONAME);
601                 process_mpa_request(ep);
602                 break;
603         default:
604                 if (ep->com.so->so_rcv.sb_cc)
605                         log(LOG_ERR, "%s: Unexpected streaming data.  "
606                             "ep %p, state %d, so %p, so_state 0x%x, sb_cc %u\n",
607                             __func__, ep, state_read(&ep->com), ep->com.so,
608                             ep->com.so->so_state, ep->com.so->so_rcv.sb_cc);
609                 break;
610         }
611 }
612
613 static void
614 process_connected(struct c4iw_ep *ep)
615 {
616
617         if ((ep->com.so->so_state & SS_ISCONNECTED) && !ep->com.so->so_error)
618                 send_mpa_req(ep);
619         else {
620                 connect_reply_upcall(ep, -ep->com.so->so_error);
621                 close_socket(&ep->com, 0);
622                 state_set(&ep->com, DEAD);
623                 c4iw_put_ep(&ep->com);
624         }
625 }
626
627 static struct socket *
628 dequeue_socket(struct socket *head, struct sockaddr_in **remote,
629     struct c4iw_ep *child_ep)
630 {
631         struct socket *so;
632
633         ACCEPT_LOCK();
634         so = TAILQ_FIRST(&head->so_comp);
635         if (!so) {
636                 ACCEPT_UNLOCK();
637                 return (NULL);
638         }
639         TAILQ_REMOVE(&head->so_comp, so, so_list);
640         head->so_qlen--;
641         SOCK_LOCK(so);
642         so->so_qstate &= ~SQ_COMP;
643         so->so_head = NULL;
644         soref(so);
645         soupcall_set(so, SO_RCV, c4iw_so_upcall, child_ep);
646         so->so_state |= SS_NBIO;
647         SOCK_UNLOCK(so);
648         ACCEPT_UNLOCK();
649         soaccept(so, (struct sockaddr **)remote);
650
651         return (so);
652 }
653
654 static void
655 process_newconn(struct c4iw_ep *parent_ep)
656 {
657         struct socket *child_so;
658         struct c4iw_ep *child_ep;
659         struct sockaddr_in *remote;
660
661         child_ep = alloc_ep(sizeof(*child_ep), M_NOWAIT);
662         if (!child_ep) {
663                 CTR3(KTR_IW_CXGBE, "%s: parent so %p, parent ep %p, ENOMEM",
664                     __func__, parent_ep->com.so, parent_ep);
665                 log(LOG_ERR, "%s: failed to allocate ep entry\n", __func__);
666                 return;
667         }
668
669         child_so = dequeue_socket(parent_ep->com.so, &remote, child_ep);
670         if (!child_so) {
671                 CTR4(KTR_IW_CXGBE,
672                     "%s: parent so %p, parent ep %p, child ep %p, dequeue err",
673                     __func__, parent_ep->com.so, parent_ep, child_ep);
674                 log(LOG_ERR, "%s: failed to dequeue child socket\n", __func__);
675                 __free_ep(&child_ep->com);
676                 return;
677
678         }
679
680         CTR5(KTR_IW_CXGBE,
681             "%s: parent so %p, parent ep %p, child so %p, child ep %p",
682              __func__, parent_ep->com.so, parent_ep, child_so, child_ep);
683
684         child_ep->com.local_addr = parent_ep->com.local_addr;
685         child_ep->com.remote_addr = *remote;
686         child_ep->com.dev = parent_ep->com.dev;
687         child_ep->com.so = child_so;
688         child_ep->com.cm_id = NULL;
689         child_ep->com.thread = parent_ep->com.thread;
690         child_ep->parent_ep = parent_ep;
691
692         free(remote, M_SONAME);
693         c4iw_get_ep(&parent_ep->com);
694         child_ep->parent_ep = parent_ep;
695         init_timer(&child_ep->timer);
696         state_set(&child_ep->com, MPA_REQ_WAIT);
697         START_EP_TIMER(child_ep);
698
699         /* maybe the request has already been queued up on the socket... */
700         process_mpa_request(child_ep);
701 }
702
703 static int
704 c4iw_so_upcall(struct socket *so, void *arg, int waitflag)
705 {
706         struct c4iw_ep *ep = arg;
707
708         spin_lock(&req_lock);
709
710         CTR6(KTR_IW_CXGBE,
711             "%s: so %p, so_state 0x%x, ep %p, ep_state %s, tqe_prev %p",
712             __func__, so, so->so_state, ep, states[ep->com.state],
713             ep->com.entry.tqe_prev);
714
715         if (ep && ep->com.so && !ep->com.entry.tqe_prev) {
716                 KASSERT(ep->com.so == so, ("%s: XXX review.", __func__));
717                 c4iw_get_ep(&ep->com);
718                 TAILQ_INSERT_TAIL(&req_list, &ep->com, entry);
719                 queue_work(c4iw_taskq, &c4iw_task);
720         }
721
722         spin_unlock(&req_lock);
723         return (SU_OK);
724 }
725
726 static void
727 process_socket_event(struct c4iw_ep *ep)
728 {
729         int state = state_read(&ep->com);
730         struct socket *so = ep->com.so;
731
732         CTR6(KTR_IW_CXGBE, "process_socket_event: so %p, so_state 0x%x, "
733             "so_err %d, sb_state 0x%x, ep %p, ep_state %s", so, so->so_state,
734             so->so_error, so->so_rcv.sb_state, ep, states[state]);
735
736         if (state == CONNECTING) {
737                 process_connected(ep);
738                 return;
739         }
740
741         if (state == LISTEN) {
742                 process_newconn(ep);
743                 return;
744         }
745
746         /* connection error */
747         if (so->so_error) {
748                 process_conn_error(ep);
749                 return;
750         }
751
752         /* peer close */
753         if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && state < CLOSING) {
754                 process_peer_close(ep);
755                 return;
756         }
757
758         /* close complete */
759         if (so->so_state & SS_ISDISCONNECTED) {
760                 process_close_complete(ep);
761                 return;
762         }
763
764         /* rx data */
765         process_data(ep);
766 }
767
768 SYSCTL_NODE(_hw, OID_AUTO, iw_cxgbe, CTLFLAG_RD, 0, "iw_cxgbe driver parameters");
769
770 int db_delay_usecs = 1;
771 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, db_delay_usecs, CTLFLAG_RWTUN, &db_delay_usecs, 0,
772                 "Usecs to delay awaiting db fifo to drain");
773
774 static int dack_mode = 1;
775 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, dack_mode, CTLFLAG_RWTUN, &dack_mode, 0,
776                 "Delayed ack mode (default = 1)");
777
778 int c4iw_max_read_depth = 8;
779 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, c4iw_max_read_depth, CTLFLAG_RWTUN, &c4iw_max_read_depth, 0,
780                 "Per-connection max ORD/IRD (default = 8)");
781
782 static int enable_tcp_timestamps;
783 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_timestamps, CTLFLAG_RWTUN, &enable_tcp_timestamps, 0,
784                 "Enable tcp timestamps (default = 0)");
785
786 static int enable_tcp_sack;
787 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_sack, CTLFLAG_RWTUN, &enable_tcp_sack, 0,
788                 "Enable tcp SACK (default = 0)");
789
790 static int enable_tcp_window_scaling = 1;
791 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, enable_tcp_window_scaling, CTLFLAG_RWTUN, &enable_tcp_window_scaling, 0,
792                 "Enable tcp window scaling (default = 1)");
793
794 int c4iw_debug = 1;
795 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, c4iw_debug, CTLFLAG_RWTUN, &c4iw_debug, 0,
796                 "Enable debug logging (default = 0)");
797
798 static int peer2peer;
799 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, peer2peer, CTLFLAG_RWTUN, &peer2peer, 0,
800                 "Support peer2peer ULPs (default = 0)");
801
802 static int p2p_type = FW_RI_INIT_P2PTYPE_READ_REQ;
803 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, p2p_type, CTLFLAG_RWTUN, &p2p_type, 0,
804                 "RDMAP opcode to use for the RTR message: 1 = RDMA_READ 0 = RDMA_WRITE (default 1)");
805
806 static int ep_timeout_secs = 60;
807 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, ep_timeout_secs, CTLFLAG_RWTUN, &ep_timeout_secs, 0,
808                 "CM Endpoint operation timeout in seconds (default = 60)");
809
810 static int mpa_rev = 1;
811 #ifdef IW_CM_MPAV2
812 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, mpa_rev, CTLFLAG_RWTUN, &mpa_rev, 0,
813                 "MPA Revision, 0 supports amso1100, 1 is RFC0544 spec compliant, 2 is IETF MPA Peer Connect Draft compliant (default = 1)");
814 #else
815 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, mpa_rev, CTLFLAG_RWTUN, &mpa_rev, 0,
816                 "MPA Revision, 0 supports amso1100, 1 is RFC0544 spec compliant (default = 1)");
817 #endif
818
819 static int markers_enabled;
820 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, markers_enabled, CTLFLAG_RWTUN, &markers_enabled, 0,
821                 "Enable MPA MARKERS (default(0) = disabled)");
822
823 static int crc_enabled = 1;
824 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, crc_enabled, CTLFLAG_RWTUN, &crc_enabled, 0,
825                 "Enable MPA CRC (default(1) = enabled)");
826
827 static int rcv_win = 256 * 1024;
828 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, rcv_win, CTLFLAG_RWTUN, &rcv_win, 0,
829                 "TCP receive window in bytes (default = 256KB)");
830
831 static int snd_win = 128 * 1024;
832 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, snd_win, CTLFLAG_RWTUN, &snd_win, 0,
833                 "TCP send window in bytes (default = 128KB)");
834
835 int db_fc_threshold = 2000;
836 SYSCTL_INT(_hw_iw_cxgbe, OID_AUTO, db_fc_threshold, CTLFLAG_RWTUN, &db_fc_threshold, 0,
837                 "QP count/threshold that triggers automatic");
838
839 static void
840 start_ep_timer(struct c4iw_ep *ep)
841 {
842
843         if (timer_pending(&ep->timer)) {
844                 CTR2(KTR_IW_CXGBE, "%s: ep %p, already started", __func__, ep);
845                 printk(KERN_ERR "%s timer already started! ep %p\n", __func__,
846                     ep);
847                 return;
848         }
849         clear_bit(TIMEOUT, &ep->com.flags);
850         c4iw_get_ep(&ep->com);
851         ep->timer.expires = jiffies + ep_timeout_secs * HZ;
852         ep->timer.data = (unsigned long)ep;
853         ep->timer.function = ep_timeout;
854         add_timer(&ep->timer);
855 }
856
857 static void
858 stop_ep_timer(struct c4iw_ep *ep)
859 {
860
861         del_timer_sync(&ep->timer);
862         if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
863                 c4iw_put_ep(&ep->com);
864         }
865 }
866
867 static enum
868 c4iw_ep_state state_read(struct c4iw_ep_common *epc)
869 {
870         enum c4iw_ep_state state;
871
872         mutex_lock(&epc->mutex);
873         state = epc->state;
874         mutex_unlock(&epc->mutex);
875
876         return (state);
877 }
878
879 static void
880 __state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
881 {
882
883         epc->state = new;
884 }
885
886 static void
887 state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
888 {
889
890         mutex_lock(&epc->mutex);
891         __state_set(epc, new);
892         mutex_unlock(&epc->mutex);
893 }
894
895 static void *
896 alloc_ep(int size, gfp_t gfp)
897 {
898         struct c4iw_ep_common *epc;
899
900         epc = kzalloc(size, gfp);
901         if (epc == NULL)
902                 return (NULL);
903
904         kref_init(&epc->kref);
905         mutex_init(&epc->mutex);
906         c4iw_init_wr_wait(&epc->wr_wait);
907
908         return (epc);
909 }
910
911 void
912 __free_ep(struct c4iw_ep_common *epc)
913 {
914         CTR2(KTR_IW_CXGBE, "%s:feB %p", __func__, epc);
915         KASSERT(!epc->so, ("%s warning ep->so %p \n", __func__, epc->so));
916         KASSERT(!epc->entry.tqe_prev, ("%s epc %p still on req list!\n", __func__, epc));
917         free(epc, M_DEVBUF);
918         CTR2(KTR_IW_CXGBE, "%s:feE %p", __func__, epc);
919 }
920
921 void _c4iw_free_ep(struct kref *kref)
922 {
923         struct c4iw_ep *ep;
924         struct c4iw_ep_common *epc;
925
926         ep = container_of(kref, struct c4iw_ep, com.kref);
927         epc = &ep->com;
928         KASSERT(!epc->so, ("%s ep->so %p", __func__, epc->so));
929         KASSERT(!epc->entry.tqe_prev, ("%s epc %p still on req list",
930             __func__, epc));
931         kfree(ep);
932 }
933
934 static void release_ep_resources(struct c4iw_ep *ep)
935 {
936         CTR2(KTR_IW_CXGBE, "%s:rerB %p", __func__, ep);
937         set_bit(RELEASE_RESOURCES, &ep->com.flags);
938         c4iw_put_ep(&ep->com);
939         CTR2(KTR_IW_CXGBE, "%s:rerE %p", __func__, ep);
940 }
941
942 static void
943 send_mpa_req(struct c4iw_ep *ep)
944 {
945         int mpalen;
946         struct mpa_message *mpa;
947         struct mpa_v2_conn_params mpa_v2_params;
948         struct mbuf *m;
949         char mpa_rev_to_use = mpa_rev;
950         int err;
951
952         if (ep->retry_with_mpa_v1)
953                 mpa_rev_to_use = 1;
954         mpalen = sizeof(*mpa) + ep->plen;
955         if (mpa_rev_to_use == 2)
956                 mpalen += sizeof(struct mpa_v2_conn_params);
957
958         if (mpalen > MHLEN)
959                 CXGBE_UNIMPLEMENTED(__func__);
960
961         m = m_gethdr(M_NOWAIT, MT_DATA);
962         if (m == NULL) {
963                 connect_reply_upcall(ep, -ENOMEM);
964                 return;
965         }
966
967         mpa = mtod(m, struct mpa_message *);
968         m->m_len = mpalen;
969         m->m_pkthdr.len = mpalen;
970         memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
971         mpa->flags = (crc_enabled ? MPA_CRC : 0) |
972                 (markers_enabled ? MPA_MARKERS : 0) |
973                 (mpa_rev_to_use == 2 ? MPA_ENHANCED_RDMA_CONN : 0);
974         mpa->private_data_size = htons(ep->plen);
975         mpa->revision = mpa_rev_to_use;
976
977         if (mpa_rev_to_use == 1) {
978                 ep->tried_with_mpa_v1 = 1;
979                 ep->retry_with_mpa_v1 = 0;
980         }
981
982         if (mpa_rev_to_use == 2) {
983                 mpa->private_data_size +=
984                         htons(sizeof(struct mpa_v2_conn_params));
985                 mpa_v2_params.ird = htons((u16)ep->ird);
986                 mpa_v2_params.ord = htons((u16)ep->ord);
987
988                 if (peer2peer) {
989                         mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
990
991                         if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE) {
992                                 mpa_v2_params.ord |=
993                                     htons(MPA_V2_RDMA_WRITE_RTR);
994                         } else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) {
995                                 mpa_v2_params.ord |=
996                                         htons(MPA_V2_RDMA_READ_RTR);
997                         }
998                 }
999                 memcpy(mpa->private_data, &mpa_v2_params,
1000                         sizeof(struct mpa_v2_conn_params));
1001
1002                 if (ep->plen) {
1003
1004                         memcpy(mpa->private_data +
1005                                 sizeof(struct mpa_v2_conn_params),
1006                                 ep->mpa_pkt + sizeof(*mpa), ep->plen);
1007                 }
1008         } else {
1009
1010                 if (ep->plen)
1011                         memcpy(mpa->private_data,
1012                                         ep->mpa_pkt + sizeof(*mpa), ep->plen);
1013                 CTR2(KTR_IW_CXGBE, "%s:smr7 %p", __func__, ep);
1014         }
1015
1016         err = sosend(ep->com.so, NULL, NULL, m, NULL, MSG_DONTWAIT, ep->com.thread);
1017         if (err) {
1018                 connect_reply_upcall(ep, -ENOMEM);
1019                 return;
1020         }
1021
1022         START_EP_TIMER(ep);
1023         state_set(&ep->com, MPA_REQ_SENT);
1024         ep->mpa_attr.initiator = 1;
1025 }
1026
1027 static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen)
1028 {
1029         int mpalen ;
1030         struct mpa_message *mpa;
1031         struct mpa_v2_conn_params mpa_v2_params;
1032         struct mbuf *m;
1033         int err;
1034
1035         CTR4(KTR_IW_CXGBE, "%s:smrejB %p %u %d", __func__, ep, ep->hwtid,
1036             ep->plen);
1037
1038         mpalen = sizeof(*mpa) + plen;
1039
1040         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1041
1042                 mpalen += sizeof(struct mpa_v2_conn_params);
1043                 CTR4(KTR_IW_CXGBE, "%s:smrej1 %p %u %d", __func__, ep,
1044                     ep->mpa_attr.version, mpalen);
1045         }
1046
1047         if (mpalen > MHLEN)
1048                 CXGBE_UNIMPLEMENTED(__func__);
1049
1050         m = m_gethdr(M_NOWAIT, MT_DATA);
1051         if (m == NULL) {
1052
1053                 printf("%s - cannot alloc mbuf!\n", __func__);
1054                 CTR2(KTR_IW_CXGBE, "%s:smrej2 %p", __func__, ep);
1055                 return (-ENOMEM);
1056         }
1057
1058
1059         mpa = mtod(m, struct mpa_message *);
1060         m->m_len = mpalen;
1061         m->m_pkthdr.len = mpalen;
1062         memset(mpa, 0, sizeof(*mpa));
1063         memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
1064         mpa->flags = MPA_REJECT;
1065         mpa->revision = mpa_rev;
1066         mpa->private_data_size = htons(plen);
1067
1068         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1069
1070                 mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1071                 mpa->private_data_size +=
1072                         htons(sizeof(struct mpa_v2_conn_params));
1073                 mpa_v2_params.ird = htons(((u16)ep->ird) |
1074                                 (peer2peer ? MPA_V2_PEER2PEER_MODEL :
1075                                  0));
1076                 mpa_v2_params.ord = htons(((u16)ep->ord) | (peer2peer ?
1077                                         (p2p_type ==
1078                                          FW_RI_INIT_P2PTYPE_RDMA_WRITE ?
1079                                          MPA_V2_RDMA_WRITE_RTR : p2p_type ==
1080                                          FW_RI_INIT_P2PTYPE_READ_REQ ?
1081                                          MPA_V2_RDMA_READ_RTR : 0) : 0));
1082                 memcpy(mpa->private_data, &mpa_v2_params,
1083                                 sizeof(struct mpa_v2_conn_params));
1084
1085                 if (ep->plen)
1086                         memcpy(mpa->private_data +
1087                                         sizeof(struct mpa_v2_conn_params), pdata, plen);
1088                 CTR5(KTR_IW_CXGBE, "%s:smrej3 %p %d %d %d", __func__, ep,
1089                     mpa_v2_params.ird, mpa_v2_params.ord, ep->plen);
1090         } else
1091                 if (plen)
1092                         memcpy(mpa->private_data, pdata, plen);
1093
1094         err = sosend(ep->com.so, NULL, NULL, m, NULL, MSG_DONTWAIT, ep->com.thread);
1095         if (!err)
1096                 ep->snd_seq += mpalen;
1097         CTR4(KTR_IW_CXGBE, "%s:smrejE %p %u %d", __func__, ep, ep->hwtid, err);
1098         return err;
1099 }
1100
1101 static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen)
1102 {
1103         int mpalen;
1104         struct mpa_message *mpa;
1105         struct mbuf *m;
1106         struct mpa_v2_conn_params mpa_v2_params;
1107         int err;
1108
1109         CTR2(KTR_IW_CXGBE, "%s:smrepB %p", __func__, ep);
1110
1111         mpalen = sizeof(*mpa) + plen;
1112
1113         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1114
1115                 CTR3(KTR_IW_CXGBE, "%s:smrep1 %p %d", __func__, ep,
1116                     ep->mpa_attr.version);
1117                 mpalen += sizeof(struct mpa_v2_conn_params);
1118         }
1119
1120         if (mpalen > MHLEN)
1121                 CXGBE_UNIMPLEMENTED(__func__);
1122
1123         m = m_gethdr(M_NOWAIT, MT_DATA);
1124         if (m == NULL) {
1125
1126                 CTR2(KTR_IW_CXGBE, "%s:smrep2 %p", __func__, ep);
1127                 printf("%s - cannot alloc mbuf!\n", __func__);
1128                 return (-ENOMEM);
1129         }
1130
1131
1132         mpa = mtod(m, struct mpa_message *);
1133         m->m_len = mpalen;
1134         m->m_pkthdr.len = mpalen;
1135         memset(mpa, 0, sizeof(*mpa));
1136         memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
1137         mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
1138                 (markers_enabled ? MPA_MARKERS : 0);
1139         mpa->revision = ep->mpa_attr.version;
1140         mpa->private_data_size = htons(plen);
1141
1142         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1143
1144                 mpa->flags |= MPA_ENHANCED_RDMA_CONN;
1145                 mpa->private_data_size +=
1146                         htons(sizeof(struct mpa_v2_conn_params));
1147                 mpa_v2_params.ird = htons((u16)ep->ird);
1148                 mpa_v2_params.ord = htons((u16)ep->ord);
1149                 CTR5(KTR_IW_CXGBE, "%s:smrep3 %p %d %d %d", __func__, ep,
1150                     ep->mpa_attr.version, mpa_v2_params.ird, mpa_v2_params.ord);
1151
1152                 if (peer2peer && (ep->mpa_attr.p2p_type !=
1153                         FW_RI_INIT_P2PTYPE_DISABLED)) {
1154
1155                         mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
1156
1157                         if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE) {
1158
1159                                 mpa_v2_params.ord |=
1160                                         htons(MPA_V2_RDMA_WRITE_RTR);
1161                                 CTR5(KTR_IW_CXGBE, "%s:smrep4 %p %d %d %d",
1162                                     __func__, ep, p2p_type, mpa_v2_params.ird,
1163                                     mpa_v2_params.ord);
1164                         }
1165                         else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ) {
1166
1167                                 mpa_v2_params.ord |=
1168                                         htons(MPA_V2_RDMA_READ_RTR);
1169                                 CTR5(KTR_IW_CXGBE, "%s:smrep5 %p %d %d %d",
1170                                     __func__, ep, p2p_type, mpa_v2_params.ird,
1171                                     mpa_v2_params.ord);
1172                         }
1173                 }
1174
1175                 memcpy(mpa->private_data, &mpa_v2_params,
1176                         sizeof(struct mpa_v2_conn_params));
1177
1178                 if (ep->plen)
1179                         memcpy(mpa->private_data +
1180                                 sizeof(struct mpa_v2_conn_params), pdata, plen);
1181         } else
1182                 if (plen)
1183                         memcpy(mpa->private_data, pdata, plen);
1184
1185         state_set(&ep->com, MPA_REP_SENT);
1186         ep->snd_seq += mpalen;
1187         err = sosend(ep->com.so, NULL, NULL, m, NULL, MSG_DONTWAIT,
1188                         ep->com.thread);
1189         CTR3(KTR_IW_CXGBE, "%s:smrepE %p %d", __func__, ep, err);
1190         return err;
1191 }
1192
1193
1194
1195 static void close_complete_upcall(struct c4iw_ep *ep)
1196 {
1197         struct iw_cm_event event;
1198
1199         CTR2(KTR_IW_CXGBE, "%s:ccuB %p", __func__, ep);
1200         memset(&event, 0, sizeof(event));
1201         event.event = IW_CM_EVENT_CLOSE;
1202
1203         if (ep->com.cm_id) {
1204
1205                 CTR2(KTR_IW_CXGBE, "%s:ccu1 %1", __func__, ep);
1206                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1207                 ep->com.cm_id->rem_ref(ep->com.cm_id);
1208                 ep->com.cm_id = NULL;
1209                 ep->com.qp = NULL;
1210                 set_bit(CLOSE_UPCALL, &ep->com.history);
1211         }
1212         CTR2(KTR_IW_CXGBE, "%s:ccuE %p", __func__, ep);
1213 }
1214
1215 static int abort_connection(struct c4iw_ep *ep)
1216 {
1217         int err;
1218
1219         CTR2(KTR_IW_CXGBE, "%s:abB %p", __func__, ep);
1220         close_complete_upcall(ep);
1221         state_set(&ep->com, ABORTING);
1222         abort_socket(ep);
1223         err = close_socket(&ep->com, 0);
1224         set_bit(ABORT_CONN, &ep->com.history);
1225         CTR2(KTR_IW_CXGBE, "%s:abE %p", __func__, ep);
1226         return err;
1227 }
1228
1229 static void peer_close_upcall(struct c4iw_ep *ep)
1230 {
1231         struct iw_cm_event event;
1232
1233         CTR2(KTR_IW_CXGBE, "%s:pcuB %p", __func__, ep);
1234         memset(&event, 0, sizeof(event));
1235         event.event = IW_CM_EVENT_DISCONNECT;
1236
1237         if (ep->com.cm_id) {
1238
1239                 CTR2(KTR_IW_CXGBE, "%s:pcu1 %p", __func__, ep);
1240                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1241                 set_bit(DISCONN_UPCALL, &ep->com.history);
1242         }
1243         CTR2(KTR_IW_CXGBE, "%s:pcuE %p", __func__, ep);
1244 }
1245
1246 static void peer_abort_upcall(struct c4iw_ep *ep)
1247 {
1248         struct iw_cm_event event;
1249
1250         CTR2(KTR_IW_CXGBE, "%s:pauB %p", __func__, ep);
1251         memset(&event, 0, sizeof(event));
1252         event.event = IW_CM_EVENT_CLOSE;
1253         event.status = -ECONNRESET;
1254
1255         if (ep->com.cm_id) {
1256
1257                 CTR2(KTR_IW_CXGBE, "%s:pau1 %p", __func__, ep);
1258                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1259                 ep->com.cm_id->rem_ref(ep->com.cm_id);
1260                 ep->com.cm_id = NULL;
1261                 ep->com.qp = NULL;
1262                 set_bit(ABORT_UPCALL, &ep->com.history);
1263         }
1264         CTR2(KTR_IW_CXGBE, "%s:pauE %p", __func__, ep);
1265 }
1266
1267 static void connect_reply_upcall(struct c4iw_ep *ep, int status)
1268 {
1269         struct iw_cm_event event;
1270
1271         CTR3(KTR_IW_CXGBE, "%s:cruB %p", __func__, ep, status);
1272         memset(&event, 0, sizeof(event));
1273         event.event = IW_CM_EVENT_CONNECT_REPLY;
1274         event.status = (status ==-ECONNABORTED)?-ECONNRESET: status;
1275         event.local_addr = ep->com.local_addr;
1276         event.remote_addr = ep->com.remote_addr;
1277
1278         if ((status == 0) || (status == -ECONNREFUSED)) {
1279
1280                 if (!ep->tried_with_mpa_v1) {
1281
1282                         CTR2(KTR_IW_CXGBE, "%s:cru1 %p", __func__, ep);
1283                         /* this means MPA_v2 is used */
1284                         event.private_data_len = ep->plen -
1285                                 sizeof(struct mpa_v2_conn_params);
1286                         event.private_data = ep->mpa_pkt +
1287                                 sizeof(struct mpa_message) +
1288                                 sizeof(struct mpa_v2_conn_params);
1289                 } else {
1290
1291                         CTR2(KTR_IW_CXGBE, "%s:cru2 %p", __func__, ep);
1292                         /* this means MPA_v1 is used */
1293                         event.private_data_len = ep->plen;
1294                         event.private_data = ep->mpa_pkt +
1295                                 sizeof(struct mpa_message);
1296                 }
1297         }
1298
1299         if (ep->com.cm_id) {
1300
1301                 CTR2(KTR_IW_CXGBE, "%s:cru3 %p", __func__, ep);
1302                 set_bit(CONN_RPL_UPCALL, &ep->com.history);
1303                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1304         }
1305
1306         if(status == -ECONNABORTED) {
1307
1308                 CTR3(KTR_IW_CXGBE, "%s:cruE %p %d", __func__, ep, status);
1309                 return;
1310         }
1311
1312         if (status < 0) {
1313
1314                 CTR3(KTR_IW_CXGBE, "%s:cru4 %p %d", __func__, ep, status);
1315                 ep->com.cm_id->rem_ref(ep->com.cm_id);
1316                 ep->com.cm_id = NULL;
1317                 ep->com.qp = NULL;
1318         }
1319
1320         CTR2(KTR_IW_CXGBE, "%s:cruE %p", __func__, ep);
1321 }
1322
1323 static void connect_request_upcall(struct c4iw_ep *ep)
1324 {
1325         struct iw_cm_event event;
1326
1327         CTR3(KTR_IW_CXGBE, "%s: ep %p, mpa_v1 %d", __func__, ep,
1328             ep->tried_with_mpa_v1);
1329
1330         memset(&event, 0, sizeof(event));
1331         event.event = IW_CM_EVENT_CONNECT_REQUEST;
1332         event.local_addr = ep->com.local_addr;
1333         event.remote_addr = ep->com.remote_addr;
1334         event.provider_data = ep;
1335         event.so = ep->com.so;
1336
1337         if (!ep->tried_with_mpa_v1) {
1338                 /* this means MPA_v2 is used */
1339 #ifdef IW_CM_MPAV2
1340                 event.ord = ep->ord;
1341                 event.ird = ep->ird;
1342 #endif
1343                 event.private_data_len = ep->plen -
1344                         sizeof(struct mpa_v2_conn_params);
1345                 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message) +
1346                         sizeof(struct mpa_v2_conn_params);
1347         } else {
1348
1349                 /* this means MPA_v1 is used. Send max supported */
1350 #ifdef IW_CM_MPAV2
1351                 event.ord = c4iw_max_read_depth;
1352                 event.ird = c4iw_max_read_depth;
1353 #endif
1354                 event.private_data_len = ep->plen;
1355                 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
1356         }
1357
1358         c4iw_get_ep(&ep->com);
1359         ep->parent_ep->com.cm_id->event_handler(ep->parent_ep->com.cm_id,
1360             &event);
1361         set_bit(CONNREQ_UPCALL, &ep->com.history);
1362         c4iw_put_ep(&ep->parent_ep->com);
1363 }
1364
1365 static void established_upcall(struct c4iw_ep *ep)
1366 {
1367         struct iw_cm_event event;
1368
1369         CTR2(KTR_IW_CXGBE, "%s:euB %p", __func__, ep);
1370         memset(&event, 0, sizeof(event));
1371         event.event = IW_CM_EVENT_ESTABLISHED;
1372 #ifdef IW_CM_MPAV2
1373         event.ird = ep->ird;
1374         event.ord = ep->ord;
1375 #endif
1376         if (ep->com.cm_id) {
1377
1378                 CTR2(KTR_IW_CXGBE, "%s:eu1 %p", __func__, ep);
1379                 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
1380                 set_bit(ESTAB_UPCALL, &ep->com.history);
1381         }
1382         CTR2(KTR_IW_CXGBE, "%s:euE %p", __func__, ep);
1383 }
1384
1385
1386
1387 static void process_mpa_reply(struct c4iw_ep *ep)
1388 {
1389         struct mpa_message *mpa;
1390         struct mpa_v2_conn_params *mpa_v2_params;
1391         u16 plen;
1392         u16 resp_ird, resp_ord;
1393         u8 rtr_mismatch = 0, insuff_ird = 0;
1394         struct c4iw_qp_attributes attrs;
1395         enum c4iw_qp_attr_mask mask;
1396         int err;
1397         struct mbuf *top, *m;
1398         int flags = MSG_DONTWAIT;
1399         struct uio uio;
1400
1401         CTR2(KTR_IW_CXGBE, "%s:pmrB %p", __func__, ep);
1402
1403         /*
1404          * Stop mpa timer.  If it expired, then the state has
1405          * changed and we bail since ep_timeout already aborted
1406          * the connection.
1407          */
1408         STOP_EP_TIMER(ep);
1409         if (state_read(&ep->com) != MPA_REQ_SENT)
1410                 return;
1411
1412         uio.uio_resid = 1000000;
1413         uio.uio_td = ep->com.thread;
1414         err = soreceive(ep->com.so, NULL, &uio, &top, NULL, &flags);
1415
1416         if (err) {
1417
1418                 if (err == EWOULDBLOCK) {
1419
1420                         CTR2(KTR_IW_CXGBE, "%s:pmr1 %p", __func__, ep);
1421                         START_EP_TIMER(ep);
1422                         return;
1423                 }
1424                 err = -err;
1425                 CTR2(KTR_IW_CXGBE, "%s:pmr2 %p", __func__, ep);
1426                 goto err;
1427         }
1428
1429         if (ep->com.so->so_rcv.sb_mb) {
1430
1431                 CTR2(KTR_IW_CXGBE, "%s:pmr3 %p", __func__, ep);
1432                 printf("%s data after soreceive called! so %p sb_mb %p top %p\n",
1433                        __func__, ep->com.so, ep->com.so->so_rcv.sb_mb, top);
1434         }
1435
1436         m = top;
1437
1438         do {
1439
1440                 CTR2(KTR_IW_CXGBE, "%s:pmr4 %p", __func__, ep);
1441                 /*
1442                  * If we get more than the supported amount of private data
1443                  * then we must fail this connection.
1444                  */
1445                 if (ep->mpa_pkt_len + m->m_len > sizeof(ep->mpa_pkt)) {
1446
1447                         CTR3(KTR_IW_CXGBE, "%s:pmr5 %p %d", __func__, ep,
1448                             ep->mpa_pkt_len + m->m_len);
1449                         err = (-EINVAL);
1450                         goto err;
1451                 }
1452
1453                 /*
1454                  * copy the new data into our accumulation buffer.
1455                  */
1456                 m_copydata(m, 0, m->m_len, &(ep->mpa_pkt[ep->mpa_pkt_len]));
1457                 ep->mpa_pkt_len += m->m_len;
1458                 if (!m->m_next)
1459                         m = m->m_nextpkt;
1460                 else
1461                         m = m->m_next;
1462         } while (m);
1463
1464         m_freem(top);
1465         /*
1466          * if we don't even have the mpa message, then bail.
1467          */
1468         if (ep->mpa_pkt_len < sizeof(*mpa))
1469                 return;
1470         mpa = (struct mpa_message *) ep->mpa_pkt;
1471
1472         /* Validate MPA header. */
1473         if (mpa->revision > mpa_rev) {
1474
1475                 CTR4(KTR_IW_CXGBE, "%s:pmr6 %p %d %d", __func__, ep,
1476                     mpa->revision, mpa_rev);
1477                 printk(KERN_ERR MOD "%s MPA version mismatch. Local = %d, "
1478                                 " Received = %d\n", __func__, mpa_rev, mpa->revision);
1479                 err = -EPROTO;
1480                 goto err;
1481         }
1482
1483         if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
1484
1485                 CTR2(KTR_IW_CXGBE, "%s:pmr7 %p", __func__, ep);
1486                 err = -EPROTO;
1487                 goto err;
1488         }
1489
1490         plen = ntohs(mpa->private_data_size);
1491
1492         /*
1493          * Fail if there's too much private data.
1494          */
1495         if (plen > MPA_MAX_PRIVATE_DATA) {
1496
1497                 CTR2(KTR_IW_CXGBE, "%s:pmr8 %p", __func__, ep);
1498                 err = -EPROTO;
1499                 goto err;
1500         }
1501
1502         /*
1503          * If plen does not account for pkt size
1504          */
1505         if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1506
1507                 CTR2(KTR_IW_CXGBE, "%s:pmr9 %p", __func__, ep);
1508                 err = -EPROTO;
1509                 goto err;
1510         }
1511
1512         ep->plen = (u8) plen;
1513
1514         /*
1515          * If we don't have all the pdata yet, then bail.
1516          * We'll continue process when more data arrives.
1517          */
1518         if (ep->mpa_pkt_len < (sizeof(*mpa) + plen)) {
1519
1520                 CTR2(KTR_IW_CXGBE, "%s:pmra %p", __func__, ep);
1521                 return;
1522         }
1523
1524         if (mpa->flags & MPA_REJECT) {
1525
1526                 CTR2(KTR_IW_CXGBE, "%s:pmrb %p", __func__, ep);
1527                 err = -ECONNREFUSED;
1528                 goto err;
1529         }
1530
1531         /*
1532          * If we get here we have accumulated the entire mpa
1533          * start reply message including private data. And
1534          * the MPA header is valid.
1535          */
1536         state_set(&ep->com, FPDU_MODE);
1537         ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1538         ep->mpa_attr.recv_marker_enabled = markers_enabled;
1539         ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1540         ep->mpa_attr.version = mpa->revision;
1541         ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1542
1543         if (mpa->revision == 2) {
1544
1545                 CTR2(KTR_IW_CXGBE, "%s:pmrc %p", __func__, ep);
1546                 ep->mpa_attr.enhanced_rdma_conn =
1547                         mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
1548
1549                 if (ep->mpa_attr.enhanced_rdma_conn) {
1550
1551                         CTR2(KTR_IW_CXGBE, "%s:pmrd %p", __func__, ep);
1552                         mpa_v2_params = (struct mpa_v2_conn_params *)
1553                                 (ep->mpa_pkt + sizeof(*mpa));
1554                         resp_ird = ntohs(mpa_v2_params->ird) &
1555                                 MPA_V2_IRD_ORD_MASK;
1556                         resp_ord = ntohs(mpa_v2_params->ord) &
1557                                 MPA_V2_IRD_ORD_MASK;
1558
1559                         /*
1560                          * This is a double-check. Ideally, below checks are
1561                          * not required since ird/ord stuff has been taken
1562                          * care of in c4iw_accept_cr
1563                          */
1564                         if ((ep->ird < resp_ord) || (ep->ord > resp_ird)) {
1565
1566                                 CTR2(KTR_IW_CXGBE, "%s:pmre %p", __func__, ep);
1567                                 err = -ENOMEM;
1568                                 ep->ird = resp_ord;
1569                                 ep->ord = resp_ird;
1570                                 insuff_ird = 1;
1571                         }
1572
1573                         if (ntohs(mpa_v2_params->ird) &
1574                                 MPA_V2_PEER2PEER_MODEL) {
1575
1576                                 CTR2(KTR_IW_CXGBE, "%s:pmrf %p", __func__, ep);
1577                                 if (ntohs(mpa_v2_params->ord) &
1578                                         MPA_V2_RDMA_WRITE_RTR) {
1579
1580                                         CTR2(KTR_IW_CXGBE, "%s:pmrg %p", __func__, ep);
1581                                         ep->mpa_attr.p2p_type =
1582                                                 FW_RI_INIT_P2PTYPE_RDMA_WRITE;
1583                                 }
1584                                 else if (ntohs(mpa_v2_params->ord) &
1585                                         MPA_V2_RDMA_READ_RTR) {
1586
1587                                         CTR2(KTR_IW_CXGBE, "%s:pmrh %p", __func__, ep);
1588                                         ep->mpa_attr.p2p_type =
1589                                                 FW_RI_INIT_P2PTYPE_READ_REQ;
1590                                 }
1591                         }
1592                 }
1593         } else {
1594
1595                 CTR2(KTR_IW_CXGBE, "%s:pmri %p", __func__, ep);
1596
1597                 if (mpa->revision == 1) {
1598
1599                         CTR2(KTR_IW_CXGBE, "%s:pmrj %p", __func__, ep);
1600
1601                         if (peer2peer) {
1602
1603                                 CTR2(KTR_IW_CXGBE, "%s:pmrk %p", __func__, ep);
1604                                 ep->mpa_attr.p2p_type = p2p_type;
1605                         }
1606                 }
1607         }
1608
1609         if (set_tcpinfo(ep)) {
1610
1611                 CTR2(KTR_IW_CXGBE, "%s:pmrl %p", __func__, ep);
1612                 printf("%s set_tcpinfo error\n", __func__);
1613                 goto err;
1614         }
1615
1616         CTR6(KTR_IW_CXGBE, "%s - crc_enabled = %d, recv_marker_enabled = %d, "
1617             "xmit_marker_enabled = %d, version = %d p2p_type = %d", __func__,
1618             ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1619             ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
1620             ep->mpa_attr.p2p_type);
1621
1622         /*
1623          * If responder's RTR does not match with that of initiator, assign
1624          * FW_RI_INIT_P2PTYPE_DISABLED in mpa attributes so that RTR is not
1625          * generated when moving QP to RTS state.
1626          * A TERM message will be sent after QP has moved to RTS state
1627          */
1628         if ((ep->mpa_attr.version == 2) && peer2peer &&
1629                 (ep->mpa_attr.p2p_type != p2p_type)) {
1630
1631                 CTR2(KTR_IW_CXGBE, "%s:pmrm %p", __func__, ep);
1632                 ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1633                 rtr_mismatch = 1;
1634         }
1635
1636
1637         //ep->ofld_txq = TOEPCB(ep->com.so)->ofld_txq;
1638         attrs.mpa_attr = ep->mpa_attr;
1639         attrs.max_ird = ep->ird;
1640         attrs.max_ord = ep->ord;
1641         attrs.llp_stream_handle = ep;
1642         attrs.next_state = C4IW_QP_STATE_RTS;
1643
1644         mask = C4IW_QP_ATTR_NEXT_STATE |
1645                 C4IW_QP_ATTR_LLP_STREAM_HANDLE | C4IW_QP_ATTR_MPA_ATTR |
1646                 C4IW_QP_ATTR_MAX_IRD | C4IW_QP_ATTR_MAX_ORD;
1647
1648         /* bind QP and TID with INIT_WR */
1649         err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp, mask, &attrs, 1);
1650
1651         if (err) {
1652
1653                 CTR2(KTR_IW_CXGBE, "%s:pmrn %p", __func__, ep);
1654                 goto err;
1655         }
1656
1657         /*
1658          * If responder's RTR requirement did not match with what initiator
1659          * supports, generate TERM message
1660          */
1661         if (rtr_mismatch) {
1662
1663                 CTR2(KTR_IW_CXGBE, "%s:pmro %p", __func__, ep);
1664                 printk(KERN_ERR "%s: RTR mismatch, sending TERM\n", __func__);
1665                 attrs.layer_etype = LAYER_MPA | DDP_LLP;
1666                 attrs.ecode = MPA_NOMATCH_RTR;
1667                 attrs.next_state = C4IW_QP_STATE_TERMINATE;
1668                 err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1669                         C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
1670                 err = -ENOMEM;
1671                 goto out;
1672         }
1673
1674         /*
1675          * Generate TERM if initiator IRD is not sufficient for responder
1676          * provided ORD. Currently, we do the same behaviour even when
1677          * responder provided IRD is also not sufficient as regards to
1678          * initiator ORD.
1679          */
1680         if (insuff_ird) {
1681
1682                 CTR2(KTR_IW_CXGBE, "%s:pmrp %p", __func__, ep);
1683                 printk(KERN_ERR "%s: Insufficient IRD, sending TERM\n",
1684                                 __func__);
1685                 attrs.layer_etype = LAYER_MPA | DDP_LLP;
1686                 attrs.ecode = MPA_INSUFF_IRD;
1687                 attrs.next_state = C4IW_QP_STATE_TERMINATE;
1688                 err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1689                         C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
1690                 err = -ENOMEM;
1691                 goto out;
1692         }
1693         goto out;
1694 err:
1695         state_set(&ep->com, ABORTING);
1696         abort_connection(ep);
1697 out:
1698         connect_reply_upcall(ep, err);
1699         CTR2(KTR_IW_CXGBE, "%s:pmrE %p", __func__, ep);
1700         return;
1701 }
1702
1703 static void
1704 process_mpa_request(struct c4iw_ep *ep)
1705 {
1706         struct mpa_message *mpa;
1707         u16 plen;
1708         int flags = MSG_DONTWAIT;
1709         int rc;
1710         struct iovec iov;
1711         struct uio uio;
1712         enum c4iw_ep_state state = state_read(&ep->com);
1713
1714         CTR3(KTR_IW_CXGBE, "%s: ep %p, state %s", __func__, ep, states[state]);
1715
1716         if (state != MPA_REQ_WAIT)
1717                 return;
1718
1719         iov.iov_base = &ep->mpa_pkt[ep->mpa_pkt_len];
1720         iov.iov_len = sizeof(ep->mpa_pkt) - ep->mpa_pkt_len;
1721         uio.uio_iov = &iov;
1722         uio.uio_iovcnt = 1;
1723         uio.uio_offset = 0;
1724         uio.uio_resid = sizeof(ep->mpa_pkt) - ep->mpa_pkt_len;
1725         uio.uio_segflg = UIO_SYSSPACE;
1726         uio.uio_rw = UIO_READ;
1727         uio.uio_td = NULL; /* uio.uio_td = ep->com.thread; */
1728
1729         rc = soreceive(ep->com.so, NULL, &uio, NULL, NULL, &flags);
1730         if (rc == EAGAIN)
1731                 return;
1732         else if (rc) {
1733 abort:
1734                 STOP_EP_TIMER(ep);
1735                 abort_connection(ep);
1736                 return;
1737         }
1738         KASSERT(uio.uio_offset > 0, ("%s: sorecieve on so %p read no data",
1739             __func__, ep->com.so));
1740         ep->mpa_pkt_len += uio.uio_offset;
1741
1742         /*
1743          * If we get more than the supported amount of private data then we must
1744          * fail this connection.  XXX: check so_rcv->sb_cc, or peek with another
1745          * soreceive, or increase the size of mpa_pkt by 1 and abort if the last
1746          * byte is filled by the soreceive above.
1747          */
1748
1749         /* Don't even have the MPA message.  Wait for more data to arrive. */
1750         if (ep->mpa_pkt_len < sizeof(*mpa))
1751                 return;
1752         mpa = (struct mpa_message *) ep->mpa_pkt;
1753
1754         /*
1755          * Validate MPA Header.
1756          */
1757         if (mpa->revision > mpa_rev) {
1758                 log(LOG_ERR, "%s: MPA version mismatch. Local = %d,"
1759                     " Received = %d\n", __func__, mpa_rev, mpa->revision);
1760                 goto abort;
1761         }
1762
1763         if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key)))
1764                 goto abort;
1765
1766         /*
1767          * Fail if there's too much private data.
1768          */
1769         plen = ntohs(mpa->private_data_size);
1770         if (plen > MPA_MAX_PRIVATE_DATA)
1771                 goto abort;
1772
1773         /*
1774          * If plen does not account for pkt size
1775          */
1776         if (ep->mpa_pkt_len > (sizeof(*mpa) + plen))
1777                 goto abort;
1778
1779         ep->plen = (u8) plen;
1780
1781         /*
1782          * If we don't have all the pdata yet, then bail.
1783          */
1784         if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1785                 return;
1786
1787         /*
1788          * If we get here we have accumulated the entire mpa
1789          * start reply message including private data.
1790          */
1791         ep->mpa_attr.initiator = 0;
1792         ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1793         ep->mpa_attr.recv_marker_enabled = markers_enabled;
1794         ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1795         ep->mpa_attr.version = mpa->revision;
1796         if (mpa->revision == 1)
1797                 ep->tried_with_mpa_v1 = 1;
1798         ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1799
1800         if (mpa->revision == 2) {
1801                 ep->mpa_attr.enhanced_rdma_conn =
1802                     mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
1803                 if (ep->mpa_attr.enhanced_rdma_conn) {
1804                         struct mpa_v2_conn_params *mpa_v2_params;
1805                         u16 ird, ord;
1806
1807                         mpa_v2_params = (void *)&ep->mpa_pkt[sizeof(*mpa)];
1808                         ird = ntohs(mpa_v2_params->ird);
1809                         ord = ntohs(mpa_v2_params->ord);
1810
1811                         ep->ird = ird & MPA_V2_IRD_ORD_MASK;
1812                         ep->ord = ord & MPA_V2_IRD_ORD_MASK;
1813                         if (ird & MPA_V2_PEER2PEER_MODEL && peer2peer) {
1814                                 if (ord & MPA_V2_RDMA_WRITE_RTR) {
1815                                         ep->mpa_attr.p2p_type =
1816                                             FW_RI_INIT_P2PTYPE_RDMA_WRITE;
1817                                 } else if (ord & MPA_V2_RDMA_READ_RTR) {
1818                                         ep->mpa_attr.p2p_type =
1819                                             FW_RI_INIT_P2PTYPE_READ_REQ;
1820                                 }
1821                         }
1822                 }
1823         } else if (mpa->revision == 1 && peer2peer)
1824                 ep->mpa_attr.p2p_type = p2p_type;
1825
1826         if (set_tcpinfo(ep))
1827                 goto abort;
1828
1829         CTR5(KTR_IW_CXGBE, "%s: crc_enabled = %d, recv_marker_enabled = %d, "
1830             "xmit_marker_enabled = %d, version = %d", __func__,
1831             ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1832             ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version);
1833
1834         state_set(&ep->com, MPA_REQ_RCVD);
1835         STOP_EP_TIMER(ep);
1836
1837         /* drive upcall */
1838         mutex_lock(&ep->parent_ep->com.mutex);
1839         if (ep->parent_ep->com.state != DEAD)
1840                 connect_request_upcall(ep);
1841         else
1842                 abort_connection(ep);
1843         mutex_unlock(&ep->parent_ep->com.mutex);
1844 }
1845
1846 /*
1847  * Upcall from the adapter indicating data has been transmitted.
1848  * For us its just the single MPA request or reply.  We can now free
1849  * the skb holding the mpa message.
1850  */
1851 int c4iw_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
1852 {
1853         int err;
1854         struct c4iw_ep *ep = to_ep(cm_id);
1855         CTR2(KTR_IW_CXGBE, "%s:crcB %p", __func__, ep);
1856
1857         if (state_read(&ep->com) == DEAD) {
1858
1859                 CTR2(KTR_IW_CXGBE, "%s:crc1 %p", __func__, ep);
1860                 c4iw_put_ep(&ep->com);
1861                 return -ECONNRESET;
1862         }
1863         set_bit(ULP_REJECT, &ep->com.history);
1864         BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1865
1866         if (mpa_rev == 0) {
1867
1868                 CTR2(KTR_IW_CXGBE, "%s:crc2 %p", __func__, ep);
1869                 abort_connection(ep);
1870         }
1871         else {
1872
1873                 CTR2(KTR_IW_CXGBE, "%s:crc3 %p", __func__, ep);
1874                 err = send_mpa_reject(ep, pdata, pdata_len);
1875                 err = soshutdown(ep->com.so, 3);
1876         }
1877         c4iw_put_ep(&ep->com);
1878         CTR2(KTR_IW_CXGBE, "%s:crc4 %p", __func__, ep);
1879         return 0;
1880 }
1881
1882 int c4iw_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
1883 {
1884         int err;
1885         struct c4iw_qp_attributes attrs;
1886         enum c4iw_qp_attr_mask mask;
1887         struct c4iw_ep *ep = to_ep(cm_id);
1888         struct c4iw_dev *h = to_c4iw_dev(cm_id->device);
1889         struct c4iw_qp *qp = get_qhp(h, conn_param->qpn);
1890
1891         CTR2(KTR_IW_CXGBE, "%s:cacB %p", __func__, ep);
1892
1893         if (state_read(&ep->com) == DEAD) {
1894
1895                 CTR2(KTR_IW_CXGBE, "%s:cac1 %p", __func__, ep);
1896                 err = -ECONNRESET;
1897                 goto err;
1898         }
1899
1900         BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
1901         BUG_ON(!qp);
1902
1903         set_bit(ULP_ACCEPT, &ep->com.history);
1904
1905         if ((conn_param->ord > c4iw_max_read_depth) ||
1906                 (conn_param->ird > c4iw_max_read_depth)) {
1907
1908                 CTR2(KTR_IW_CXGBE, "%s:cac2 %p", __func__, ep);
1909                 abort_connection(ep);
1910                 err = -EINVAL;
1911                 goto err;
1912         }
1913
1914         if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
1915
1916                 CTR2(KTR_IW_CXGBE, "%s:cac3 %p", __func__, ep);
1917
1918                 if (conn_param->ord > ep->ird) {
1919
1920                         CTR2(KTR_IW_CXGBE, "%s:cac4 %p", __func__, ep);
1921                         ep->ird = conn_param->ird;
1922                         ep->ord = conn_param->ord;
1923                         send_mpa_reject(ep, conn_param->private_data,
1924                                         conn_param->private_data_len);
1925                         abort_connection(ep);
1926                         err = -ENOMEM;
1927                         goto err;
1928                 }
1929
1930                 if (conn_param->ird > ep->ord) {
1931
1932                         CTR2(KTR_IW_CXGBE, "%s:cac5 %p", __func__, ep);
1933
1934                         if (!ep->ord) {
1935
1936                                 CTR2(KTR_IW_CXGBE, "%s:cac6 %p", __func__, ep);
1937                                 conn_param->ird = 1;
1938                         }
1939                         else {
1940                                 CTR2(KTR_IW_CXGBE, "%s:cac7 %p", __func__, ep);
1941                                 abort_connection(ep);
1942                                 err = -ENOMEM;
1943                                 goto err;
1944                         }
1945                 }
1946
1947         }
1948         ep->ird = conn_param->ird;
1949         ep->ord = conn_param->ord;
1950
1951         if (ep->mpa_attr.version != 2) {
1952
1953                 CTR2(KTR_IW_CXGBE, "%s:cac8 %p", __func__, ep);
1954
1955                 if (peer2peer && ep->ird == 0) {
1956
1957                         CTR2(KTR_IW_CXGBE, "%s:cac9 %p", __func__, ep);
1958                         ep->ird = 1;
1959                 }
1960         }
1961
1962
1963         cm_id->add_ref(cm_id);
1964         ep->com.cm_id = cm_id;
1965         ep->com.qp = qp;
1966         //ep->ofld_txq = TOEPCB(ep->com.so)->ofld_txq;
1967
1968         /* bind QP to EP and move to RTS */
1969         attrs.mpa_attr = ep->mpa_attr;
1970         attrs.max_ird = ep->ird;
1971         attrs.max_ord = ep->ord;
1972         attrs.llp_stream_handle = ep;
1973         attrs.next_state = C4IW_QP_STATE_RTS;
1974
1975         /* bind QP and TID with INIT_WR */
1976         mask = C4IW_QP_ATTR_NEXT_STATE |
1977                 C4IW_QP_ATTR_LLP_STREAM_HANDLE |
1978                 C4IW_QP_ATTR_MPA_ATTR |
1979                 C4IW_QP_ATTR_MAX_IRD |
1980                 C4IW_QP_ATTR_MAX_ORD;
1981
1982         err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp, mask, &attrs, 1);
1983
1984         if (err) {
1985
1986                 CTR2(KTR_IW_CXGBE, "%s:caca %p", __func__, ep);
1987                 goto err1;
1988         }
1989         err = send_mpa_reply(ep, conn_param->private_data,
1990                         conn_param->private_data_len);
1991
1992         if (err) {
1993
1994                 CTR2(KTR_IW_CXGBE, "%s:caca %p", __func__, ep);
1995                 goto err1;
1996         }
1997
1998         state_set(&ep->com, FPDU_MODE);
1999         established_upcall(ep);
2000         c4iw_put_ep(&ep->com);
2001         CTR2(KTR_IW_CXGBE, "%s:cacE %p", __func__, ep);
2002         return 0;
2003 err1:
2004         ep->com.cm_id = NULL;
2005         ep->com.qp = NULL;
2006         cm_id->rem_ref(cm_id);
2007 err:
2008         c4iw_put_ep(&ep->com);
2009         CTR2(KTR_IW_CXGBE, "%s:cacE err %p", __func__, ep);
2010         return err;
2011 }
2012
2013
2014
2015 int c4iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2016 {
2017         int err = 0;
2018         struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
2019         struct c4iw_ep *ep = NULL;
2020         struct rtentry *rt;
2021         struct toedev *tdev;
2022
2023         CTR2(KTR_IW_CXGBE, "%s:ccB %p", __func__, cm_id);
2024
2025         if ((conn_param->ord > c4iw_max_read_depth) ||
2026                 (conn_param->ird > c4iw_max_read_depth)) {
2027
2028                 CTR2(KTR_IW_CXGBE, "%s:cc1 %p", __func__, cm_id);
2029                 err = -EINVAL;
2030                 goto out;
2031         }
2032         ep = alloc_ep(sizeof(*ep), M_NOWAIT);
2033
2034         if (!ep) {
2035
2036                 CTR2(KTR_IW_CXGBE, "%s:cc2 %p", __func__, cm_id);
2037                 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
2038                 err = -ENOMEM;
2039                 goto out;
2040         }
2041         init_timer(&ep->timer);
2042         ep->plen = conn_param->private_data_len;
2043
2044         if (ep->plen) {
2045
2046                 CTR2(KTR_IW_CXGBE, "%s:cc3 %p", __func__, ep);
2047                 memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
2048                                 conn_param->private_data, ep->plen);
2049         }
2050         ep->ird = conn_param->ird;
2051         ep->ord = conn_param->ord;
2052
2053         if (peer2peer && ep->ord == 0) {
2054
2055                 CTR2(KTR_IW_CXGBE, "%s:cc4 %p", __func__, ep);
2056                 ep->ord = 1;
2057         }
2058
2059         cm_id->add_ref(cm_id);
2060         ep->com.dev = dev;
2061         ep->com.cm_id = cm_id;
2062         ep->com.qp = get_qhp(dev, conn_param->qpn);
2063
2064         if (!ep->com.qp) {
2065
2066                 CTR2(KTR_IW_CXGBE, "%s:cc5 %p", __func__, ep);
2067                 err = -EINVAL;
2068                 goto fail2;
2069         }
2070         ep->com.thread = curthread;
2071         ep->com.so = cm_id->so;
2072
2073         init_sock(&ep->com);
2074
2075         /* find a route */
2076         rt = find_route(
2077                 cm_id->local_addr.sin_addr.s_addr,
2078                 cm_id->remote_addr.sin_addr.s_addr,
2079                 cm_id->local_addr.sin_port,
2080                 cm_id->remote_addr.sin_port, 0);
2081
2082         if (!rt) {
2083
2084                 CTR2(KTR_IW_CXGBE, "%s:cc7 %p", __func__, ep);
2085                 printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
2086                 err = -EHOSTUNREACH;
2087                 goto fail3;
2088         }
2089
2090
2091         if (!(rt->rt_ifp->if_flags & IFCAP_TOE)) {
2092
2093                 CTR2(KTR_IW_CXGBE, "%s:cc8 %p", __func__, ep);
2094                 printf("%s - interface not TOE capable.\n", __func__);
2095                 goto fail3;
2096         }
2097         tdev = TOEDEV(rt->rt_ifp);
2098
2099         if (tdev == NULL) {
2100
2101                 CTR2(KTR_IW_CXGBE, "%s:cc9 %p", __func__, ep);
2102                 printf("%s - No toedev for interface.\n", __func__);
2103                 goto fail3;
2104         }
2105         RTFREE(rt);
2106
2107         state_set(&ep->com, CONNECTING);
2108         ep->tos = 0;
2109         ep->com.local_addr = cm_id->local_addr;
2110         ep->com.remote_addr = cm_id->remote_addr;
2111         err = soconnect(ep->com.so, (struct sockaddr *)&ep->com.remote_addr,
2112                 ep->com.thread);
2113
2114         if (!err) {
2115
2116                 CTR2(KTR_IW_CXGBE, "%s:cca %p", __func__, ep);
2117                 goto out;
2118         }
2119
2120 fail3:
2121         CTR2(KTR_IW_CXGBE, "%s:ccb %p", __func__, ep);
2122         RTFREE(rt);
2123 fail2:
2124         cm_id->rem_ref(cm_id);
2125         c4iw_put_ep(&ep->com);
2126 out:
2127         CTR2(KTR_IW_CXGBE, "%s:ccE %p", __func__, ep);
2128         return err;
2129 }
2130
2131 /*
2132  * iwcm->create_listen.  Returns -errno on failure.
2133  */
2134 int
2135 c4iw_create_listen(struct iw_cm_id *cm_id, int backlog)
2136 {
2137         int rc;
2138         struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
2139         struct c4iw_listen_ep *ep;
2140         struct socket *so = cm_id->so;
2141
2142         ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
2143         CTR5(KTR_IW_CXGBE, "%s: cm_id %p, lso %p, ep %p, inp %p", __func__,
2144             cm_id, so, ep, so->so_pcb);
2145         if (ep == NULL) {
2146                 log(LOG_ERR, "%s: failed to alloc memory for endpoint\n",
2147                     __func__);
2148                 rc = ENOMEM;
2149                 goto failed;
2150         }
2151
2152         cm_id->add_ref(cm_id);
2153         ep->com.cm_id = cm_id;
2154         ep->com.dev = dev;
2155         ep->backlog = backlog;
2156         ep->com.local_addr = cm_id->local_addr;
2157         ep->com.thread = curthread;
2158         state_set(&ep->com, LISTEN);
2159         ep->com.so = so;
2160         init_sock(&ep->com);
2161
2162         rc = solisten(so, ep->backlog, ep->com.thread);
2163         if (rc != 0) {
2164                 log(LOG_ERR, "%s: failed to start listener: %d\n", __func__,
2165                     rc);
2166                 close_socket(&ep->com, 0);
2167                 cm_id->rem_ref(cm_id);
2168                 c4iw_put_ep(&ep->com);
2169                 goto failed;
2170         }
2171
2172         cm_id->provider_data = ep;
2173         return (0);
2174
2175 failed:
2176         CTR3(KTR_IW_CXGBE, "%s: cm_id %p, FAILED (%d)", __func__, cm_id, rc);
2177         return (-rc);
2178 }
2179
2180 int
2181 c4iw_destroy_listen(struct iw_cm_id *cm_id)
2182 {
2183         int rc;
2184         struct c4iw_listen_ep *ep = to_listen_ep(cm_id);
2185
2186         CTR4(KTR_IW_CXGBE, "%s: cm_id %p, so %p, inp %p", __func__, cm_id,
2187             cm_id->so, cm_id->so->so_pcb);
2188
2189         state_set(&ep->com, DEAD);
2190         rc = close_socket(&ep->com, 0);
2191         cm_id->rem_ref(cm_id);
2192         c4iw_put_ep(&ep->com);
2193
2194         return (rc);
2195 }
2196
2197 int c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
2198 {
2199         int ret = 0;
2200         int close = 0;
2201         int fatal = 0;
2202         struct c4iw_rdev *rdev;
2203
2204         mutex_lock(&ep->com.mutex);
2205
2206         CTR2(KTR_IW_CXGBE, "%s:cedB %p", __func__, ep);
2207
2208         rdev = &ep->com.dev->rdev;
2209
2210         if (c4iw_fatal_error(rdev)) {
2211
2212                 CTR2(KTR_IW_CXGBE, "%s:ced1 %p", __func__, ep);
2213                 fatal = 1;
2214                 close_complete_upcall(ep);
2215                 ep->com.state = DEAD;
2216         }
2217         CTR3(KTR_IW_CXGBE, "%s:ced2 %p %s", __func__, ep,
2218             states[ep->com.state]);
2219
2220         switch (ep->com.state) {
2221
2222                 case MPA_REQ_WAIT:
2223                 case MPA_REQ_SENT:
2224                 case MPA_REQ_RCVD:
2225                 case MPA_REP_SENT:
2226                 case FPDU_MODE:
2227                         close = 1;
2228                         if (abrupt)
2229                                 ep->com.state = ABORTING;
2230                         else {
2231                                 ep->com.state = CLOSING;
2232                                 START_EP_TIMER(ep);
2233                         }
2234                         set_bit(CLOSE_SENT, &ep->com.flags);
2235                         break;
2236
2237                 case CLOSING:
2238
2239                         if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
2240
2241                                 close = 1;
2242                                 if (abrupt) {
2243                                         STOP_EP_TIMER(ep);
2244                                         ep->com.state = ABORTING;
2245                                 } else
2246                                         ep->com.state = MORIBUND;
2247                         }
2248                         break;
2249
2250                 case MORIBUND:
2251                 case ABORTING:
2252                 case DEAD:
2253                         CTR3(KTR_IW_CXGBE,
2254                             "%s ignoring disconnect ep %p state %u", __func__,
2255                             ep, ep->com.state);
2256                         break;
2257
2258                 default:
2259                         BUG();
2260                         break;
2261         }
2262
2263         mutex_unlock(&ep->com.mutex);
2264
2265         if (close) {
2266
2267                 CTR2(KTR_IW_CXGBE, "%s:ced3 %p", __func__, ep);
2268
2269                 if (abrupt) {
2270
2271                         CTR2(KTR_IW_CXGBE, "%s:ced4 %p", __func__, ep);
2272                         set_bit(EP_DISC_ABORT, &ep->com.history);
2273                         ret = abort_connection(ep);
2274                 } else {
2275
2276                         CTR2(KTR_IW_CXGBE, "%s:ced5 %p", __func__, ep);
2277                         set_bit(EP_DISC_CLOSE, &ep->com.history);
2278
2279                         if (!ep->parent_ep)
2280                                 __state_set(&ep->com, MORIBUND);
2281                         ret = shutdown_socket(&ep->com);
2282                 }
2283
2284                 if (ret) {
2285
2286                         fatal = 1;
2287                 }
2288         }
2289
2290         if (fatal) {
2291
2292                 release_ep_resources(ep);
2293                 CTR2(KTR_IW_CXGBE, "%s:ced6 %p", __func__, ep);
2294         }
2295         CTR2(KTR_IW_CXGBE, "%s:cedE %p", __func__, ep);
2296         return ret;
2297 }
2298
2299 #ifdef C4IW_EP_REDIRECT
2300 int c4iw_ep_redirect(void *ctx, struct dst_entry *old, struct dst_entry *new,
2301                 struct l2t_entry *l2t)
2302 {
2303         struct c4iw_ep *ep = ctx;
2304
2305         if (ep->dst != old)
2306                 return 0;
2307
2308         PDBG("%s ep %p redirect to dst %p l2t %p\n", __func__, ep, new,
2309                         l2t);
2310         dst_hold(new);
2311         cxgb4_l2t_release(ep->l2t);
2312         ep->l2t = l2t;
2313         dst_release(old);
2314         ep->dst = new;
2315         return 1;
2316 }
2317 #endif
2318
2319
2320
2321 static void ep_timeout(unsigned long arg)
2322 {
2323         struct c4iw_ep *ep = (struct c4iw_ep *)arg;
2324         int kickit = 0;
2325
2326         CTR2(KTR_IW_CXGBE, "%s:etB %p", __func__, ep);
2327         spin_lock(&timeout_lock);
2328
2329         if (!test_and_set_bit(TIMEOUT, &ep->com.flags)) {
2330
2331                 list_add_tail(&ep->entry, &timeout_list);
2332                 kickit = 1;
2333         }
2334         spin_unlock(&timeout_lock);
2335
2336         if (kickit) {
2337
2338                 CTR2(KTR_IW_CXGBE, "%s:et1 %p", __func__, ep);
2339                 queue_work(c4iw_taskq, &c4iw_task);
2340         }
2341         CTR2(KTR_IW_CXGBE, "%s:etE %p", __func__, ep);
2342 }
2343
2344 static int fw6_wr_rpl(struct adapter *sc, const __be64 *rpl)
2345 {
2346         uint64_t val = be64toh(*rpl);
2347         int ret;
2348         struct c4iw_wr_wait *wr_waitp;
2349
2350         ret = (int)((val >> 8) & 0xff);
2351         wr_waitp = (struct c4iw_wr_wait *)rpl[1];
2352         CTR3(KTR_IW_CXGBE, "%s wr_waitp %p ret %u", __func__, wr_waitp, ret);
2353         if (wr_waitp)
2354                 c4iw_wake_up(wr_waitp, ret ? -ret : 0);
2355
2356         return (0);
2357 }
2358
2359 static int fw6_cqe_handler(struct adapter *sc, const __be64 *rpl)
2360 {
2361         struct t4_cqe cqe =*(const struct t4_cqe *)(&rpl[0]);
2362
2363         CTR2(KTR_IW_CXGBE, "%s rpl %p", __func__, rpl);
2364         c4iw_ev_dispatch(sc->iwarp_softc, &cqe);
2365
2366         return (0);
2367 }
2368
2369 static int terminate(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
2370 {
2371
2372         struct adapter *sc = iq->adapter;
2373
2374         const struct cpl_rdma_terminate *rpl = (const void *)(rss + 1);
2375         unsigned int tid = GET_TID(rpl);
2376         struct c4iw_qp_attributes attrs;
2377         struct toepcb *toep = lookup_tid(sc, tid);
2378         struct socket *so = inp_inpcbtosocket(toep->inp);
2379         struct c4iw_ep *ep = so->so_rcv.sb_upcallarg;
2380
2381         CTR2(KTR_IW_CXGBE, "%s:tB %p %d", __func__, ep);
2382
2383         if (ep && ep->com.qp) {
2384
2385                 printk(KERN_WARNING MOD "TERM received tid %u qpid %u\n", tid,
2386                                 ep->com.qp->wq.sq.qid);
2387                 attrs.next_state = C4IW_QP_STATE_TERMINATE;
2388                 c4iw_modify_qp(ep->com.dev, ep->com.qp, C4IW_QP_ATTR_NEXT_STATE, &attrs,
2389                                 1);
2390         } else
2391                 printk(KERN_WARNING MOD "TERM received tid %u no ep/qp\n", tid);
2392         CTR2(KTR_IW_CXGBE, "%s:tE %p %d", __func__, ep);
2393
2394         return 0;
2395 }
2396
2397         void
2398 c4iw_cm_init_cpl(struct adapter *sc)
2399 {
2400
2401         t4_register_cpl_handler(sc, CPL_RDMA_TERMINATE, terminate);
2402         t4_register_fw_msg_handler(sc, FW6_TYPE_WR_RPL, fw6_wr_rpl);
2403         t4_register_fw_msg_handler(sc, FW6_TYPE_CQE, fw6_cqe_handler);
2404         t4_register_an_handler(sc, c4iw_ev_handler);
2405 }
2406
2407         void
2408 c4iw_cm_term_cpl(struct adapter *sc)
2409 {
2410
2411         t4_register_cpl_handler(sc, CPL_RDMA_TERMINATE, NULL);
2412         t4_register_fw_msg_handler(sc, FW6_TYPE_WR_RPL, NULL);
2413         t4_register_fw_msg_handler(sc, FW6_TYPE_CQE, NULL);
2414 }
2415
2416 int __init c4iw_cm_init(void)
2417 {
2418
2419         TAILQ_INIT(&req_list);
2420         spin_lock_init(&req_lock);
2421         INIT_LIST_HEAD(&timeout_list);
2422         spin_lock_init(&timeout_lock);
2423
2424         INIT_WORK(&c4iw_task, process_req);
2425
2426         c4iw_taskq = create_singlethread_workqueue("iw_cxgbe");
2427         if (!c4iw_taskq)
2428                 return -ENOMEM;
2429
2430
2431         return 0;
2432 }
2433
2434 void __exit c4iw_cm_term(void)
2435 {
2436         WARN_ON(!TAILQ_EMPTY(&req_list));
2437         WARN_ON(!list_empty(&timeout_list));
2438         flush_workqueue(c4iw_taskq);
2439         destroy_workqueue(c4iw_taskq);
2440 }
2441 #endif