Merge branch 'dt-for-linus' of git://sources.calxeda.com/kernel/linux
[pandora-kernel.git] / drivers / infiniband / hw / nes / nes_cm.c
1 /*
2  * Copyright (c) 2006 - 2009 Intel Corporation.  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  */
33
34
35 #define TCPOPT_TIMESTAMP 8
36
37 #include <linux/atomic.h>
38 #include <linux/skbuff.h>
39 #include <linux/ip.h>
40 #include <linux/tcp.h>
41 #include <linux/init.h>
42 #include <linux/if_arp.h>
43 #include <linux/if_vlan.h>
44 #include <linux/notifier.h>
45 #include <linux/net.h>
46 #include <linux/types.h>
47 #include <linux/timer.h>
48 #include <linux/time.h>
49 #include <linux/delay.h>
50 #include <linux/etherdevice.h>
51 #include <linux/netdevice.h>
52 #include <linux/random.h>
53 #include <linux/list.h>
54 #include <linux/threads.h>
55 #include <linux/highmem.h>
56 #include <linux/slab.h>
57 #include <net/arp.h>
58 #include <net/neighbour.h>
59 #include <net/route.h>
60 #include <net/ip_fib.h>
61 #include <net/tcp.h>
62
63 #include "nes.h"
64
65 u32 cm_packets_sent;
66 u32 cm_packets_bounced;
67 u32 cm_packets_dropped;
68 u32 cm_packets_retrans;
69 u32 cm_packets_created;
70 u32 cm_packets_received;
71 atomic_t cm_listens_created;
72 atomic_t cm_listens_destroyed;
73 u32 cm_backlog_drops;
74 atomic_t cm_loopbacks;
75 atomic_t cm_nodes_created;
76 atomic_t cm_nodes_destroyed;
77 atomic_t cm_accel_dropped_pkts;
78 atomic_t cm_resets_recvd;
79
80 static inline int mini_cm_accelerated(struct nes_cm_core *, struct nes_cm_node *);
81 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *, struct nes_vnic *, struct nes_cm_info *);
82 static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
83 static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *, struct nes_vnic *, u16, void *, struct nes_cm_info *);
84 static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
85 static int mini_cm_accept(struct nes_cm_core *, struct nes_cm_node *);
86 static int mini_cm_reject(struct nes_cm_core *, struct nes_cm_node *);
87 static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *, struct sk_buff *);
88 static int mini_cm_dealloc_core(struct nes_cm_core *);
89 static int mini_cm_get(struct nes_cm_core *);
90 static int mini_cm_set(struct nes_cm_core *, u32, u32);
91
92 static void form_cm_frame(struct sk_buff *, struct nes_cm_node *, void *, u32, void *, u32, u8);
93 static int add_ref_cm_node(struct nes_cm_node *);
94 static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
95
96 static int nes_cm_disconn_true(struct nes_qp *);
97 static int nes_cm_post_event(struct nes_cm_event *event);
98 static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
99 static void nes_disconnect_worker(struct work_struct *work);
100
101 static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
102 static int send_mpa_reject(struct nes_cm_node *);
103 static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
104 static int send_reset(struct nes_cm_node *, struct sk_buff *);
105 static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
106 static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
107 static void process_packet(struct nes_cm_node *, struct sk_buff *, struct nes_cm_core *);
108
109 static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
110 static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
111 static void cleanup_retrans_entry(struct nes_cm_node *);
112 static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
113 static void free_retrans_entry(struct nes_cm_node *cm_node);
114 static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph, struct sk_buff *skb, int optionsize, int passive);
115
116 /* CM event handler functions */
117 static void cm_event_connected(struct nes_cm_event *);
118 static void cm_event_connect_error(struct nes_cm_event *);
119 static void cm_event_reset(struct nes_cm_event *);
120 static void cm_event_mpa_req(struct nes_cm_event *);
121 static void cm_event_mpa_reject(struct nes_cm_event *);
122 static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
123
124 /* MPA build functions */
125 static int cm_build_mpa_frame(struct nes_cm_node *, u8 **, u16 *, u8 *, u8);
126 static void build_mpa_v2(struct nes_cm_node *, void *, u8);
127 static void build_mpa_v1(struct nes_cm_node *, void *, u8);
128 static void build_rdma0_msg(struct nes_cm_node *, struct nes_qp **);
129
130 static void print_core(struct nes_cm_core *core);
131
132 /* External CM API Interface */
133 /* instance of function pointers for client API */
134 /* set address of this instance to cm_core->cm_ops at cm_core alloc */
135 static struct nes_cm_ops nes_cm_api = {
136         mini_cm_accelerated,
137         mini_cm_listen,
138         mini_cm_del_listen,
139         mini_cm_connect,
140         mini_cm_close,
141         mini_cm_accept,
142         mini_cm_reject,
143         mini_cm_recv_pkt,
144         mini_cm_dealloc_core,
145         mini_cm_get,
146         mini_cm_set
147 };
148
149 static struct nes_cm_core *g_cm_core;
150
151 atomic_t cm_connects;
152 atomic_t cm_accepts;
153 atomic_t cm_disconnects;
154 atomic_t cm_closes;
155 atomic_t cm_connecteds;
156 atomic_t cm_connect_reqs;
157 atomic_t cm_rejects;
158
159 int nes_add_ref_cm_node(struct nes_cm_node *cm_node)
160 {
161         return add_ref_cm_node(cm_node);
162 }
163
164 int nes_rem_ref_cm_node(struct nes_cm_node *cm_node)
165 {
166         return rem_ref_cm_node(cm_node->cm_core, cm_node);
167 }
168
169 /**
170  * create_event
171  */
172 static struct nes_cm_event *create_event(struct nes_cm_node *   cm_node,
173                                          enum nes_cm_event_type type)
174 {
175         struct nes_cm_event *event;
176
177         if (!cm_node->cm_id)
178                 return NULL;
179
180         /* allocate an empty event */
181         event = kzalloc(sizeof(*event), GFP_ATOMIC);
182
183         if (!event)
184                 return NULL;
185
186         event->type = type;
187         event->cm_node = cm_node;
188         event->cm_info.rem_addr = cm_node->rem_addr;
189         event->cm_info.loc_addr = cm_node->loc_addr;
190         event->cm_info.rem_port = cm_node->rem_port;
191         event->cm_info.loc_port = cm_node->loc_port;
192         event->cm_info.cm_id = cm_node->cm_id;
193
194         nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
195                   "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
196                   cm_node, event, type, event->cm_info.loc_addr,
197                   event->cm_info.loc_port, event->cm_info.rem_addr,
198                   event->cm_info.rem_port);
199
200         nes_cm_post_event(event);
201         return event;
202 }
203
204
205 /**
206  * send_mpa_request
207  */
208 static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
209 {
210         u8 start_addr = 0;
211         u8 *start_ptr = &start_addr;
212         u8 **start_buff = &start_ptr;
213         u16 buff_len = 0;
214
215         if (!skb) {
216                 nes_debug(NES_DBG_CM, "skb set to NULL\n");
217                 return -1;
218         }
219
220         /* send an MPA Request frame */
221         cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REQUEST);
222         form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK);
223
224         return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
225 }
226
227
228
229 static int send_mpa_reject(struct nes_cm_node *cm_node)
230 {
231         struct sk_buff *skb = NULL;
232         u8 start_addr = 0;
233         u8 *start_ptr = &start_addr;
234         u8 **start_buff = &start_ptr;
235         u16 buff_len = 0;
236
237         skb = dev_alloc_skb(MAX_CM_BUFFER);
238         if (!skb) {
239                 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
240                 return -ENOMEM;
241         }
242
243         /* send an MPA reject frame */
244         cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REPLY);
245         form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK | SET_FIN);
246
247         cm_node->state = NES_CM_STATE_FIN_WAIT1;
248         return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
249 }
250
251
252 /**
253  * recv_mpa - process a received TCP pkt, we are expecting an
254  * IETF MPA frame
255  */
256 static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
257                      u32 len)
258 {
259         struct ietf_mpa_v1 *mpa_frame;
260         struct ietf_mpa_v2 *mpa_v2_frame;
261         struct ietf_rtr_msg *rtr_msg;
262         int mpa_hdr_len;
263         int priv_data_len;
264
265         *type = NES_MPA_REQUEST_ACCEPT;
266
267         /* assume req frame is in tcp data payload */
268         if (len < sizeof(struct ietf_mpa_v1)) {
269                 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
270                 return -EINVAL;
271         }
272
273         /* points to the beginning of the frame, which could be MPA V1 or V2 */
274         mpa_frame = (struct ietf_mpa_v1 *)buffer;
275         mpa_hdr_len = sizeof(struct ietf_mpa_v1);
276         priv_data_len = ntohs(mpa_frame->priv_data_len);
277
278         /* make sure mpa private data len is less than 512 bytes */
279         if (priv_data_len > IETF_MAX_PRIV_DATA_LEN) {
280                 nes_debug(NES_DBG_CM, "The received Length of Private"
281                           " Data field exceeds 512 octets\n");
282                 return -EINVAL;
283         }
284         /*
285          * make sure MPA receiver interoperate with the
286          * received MPA version and MPA key information
287          *
288          */
289         if (mpa_frame->rev != IETF_MPA_V1 && mpa_frame->rev != IETF_MPA_V2) {
290                 nes_debug(NES_DBG_CM, "The received mpa version"
291                           " is not supported\n");
292                 return -EINVAL;
293         }
294         /*
295         * backwards compatibility only
296         */
297         if (mpa_frame->rev > cm_node->mpa_frame_rev) {
298                 nes_debug(NES_DBG_CM, "The received mpa version"
299                         " can not be interoperated\n");
300                 return -EINVAL;
301         } else {
302                 cm_node->mpa_frame_rev = mpa_frame->rev;
303         }
304
305         if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
306                 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
307                         nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
308                         return -EINVAL;
309                 }
310         } else {
311                 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
312                         nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
313                         return -EINVAL;
314                 }
315         }
316
317
318         if (priv_data_len + mpa_hdr_len != len) {
319                 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
320                         " complete (%x + %x != %x)\n",
321                         priv_data_len, mpa_hdr_len, len);
322                 return -EINVAL;
323         }
324         /* make sure it does not exceed the max size */
325         if (len > MAX_CM_BUFFER) {
326                 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
327                         " (%x + %x != %x)\n",
328                         priv_data_len, mpa_hdr_len, len);
329                 return -EINVAL;
330         }
331
332         cm_node->mpa_frame_size = priv_data_len;
333
334         switch (mpa_frame->rev) {
335         case IETF_MPA_V2: {
336                 u16 ird_size;
337                 u16 ord_size;
338                 mpa_v2_frame = (struct ietf_mpa_v2 *)buffer;
339                 mpa_hdr_len += IETF_RTR_MSG_SIZE;
340                 cm_node->mpa_frame_size -= IETF_RTR_MSG_SIZE;
341                 rtr_msg = &mpa_v2_frame->rtr_msg;
342
343                 /* parse rtr message */
344                 rtr_msg->ctrl_ird = ntohs(rtr_msg->ctrl_ird);
345                 rtr_msg->ctrl_ord = ntohs(rtr_msg->ctrl_ord);
346                 ird_size = rtr_msg->ctrl_ird & IETF_NO_IRD_ORD;
347                 ord_size = rtr_msg->ctrl_ord & IETF_NO_IRD_ORD;
348
349                 if (!(rtr_msg->ctrl_ird & IETF_PEER_TO_PEER)) {
350                         /* send reset */
351                         return -EINVAL;
352                 }
353
354                 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
355                         /* responder */
356                         if (cm_node->ord_size > ird_size)
357                                 cm_node->ord_size = ird_size;
358                 } else {
359                         /* initiator */
360                         if (cm_node->ord_size > ird_size)
361                                 cm_node->ord_size = ird_size;
362
363                         if (cm_node->ird_size < ord_size) {
364                                 /* no resources available */
365                                 /* send terminate message */
366                                 return -EINVAL;
367                         }
368                 }
369
370                 if (rtr_msg->ctrl_ord & IETF_RDMA0_READ) {
371                         cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
372                 } else if (rtr_msg->ctrl_ord & IETF_RDMA0_WRITE) {
373                         cm_node->send_rdma0_op = SEND_RDMA_WRITE_ZERO;
374                 } else {        /* Not supported RDMA0 operation */
375                         return -EINVAL;
376                 }
377                 break;
378         }
379         case IETF_MPA_V1:
380         default:
381                 break;
382         }
383
384         /* copy entire MPA frame to our cm_node's frame */
385         memcpy(cm_node->mpa_frame_buf, buffer + mpa_hdr_len, cm_node->mpa_frame_size);
386
387         if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
388                 *type = NES_MPA_REQUEST_REJECT;
389         return 0;
390 }
391
392
393 /**
394  * form_cm_frame - get a free packet and build empty frame Use
395  * node info to build.
396  */
397 static void form_cm_frame(struct sk_buff *skb,
398                           struct nes_cm_node *cm_node, void *options, u32 optionsize,
399                           void *data, u32 datasize, u8 flags)
400 {
401         struct tcphdr *tcph;
402         struct iphdr *iph;
403         struct ethhdr *ethh;
404         u8 *buf;
405         u16 packetsize = sizeof(*iph);
406
407         packetsize += sizeof(*tcph);
408         packetsize += optionsize + datasize;
409
410         skb_trim(skb, 0);
411         memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
412
413         buf = skb_put(skb, packetsize + ETH_HLEN);
414
415         ethh = (struct ethhdr *)buf;
416         buf += ETH_HLEN;
417
418         iph = (struct iphdr *)buf;
419         buf += sizeof(*iph);
420         tcph = (struct tcphdr *)buf;
421         skb_reset_mac_header(skb);
422         skb_set_network_header(skb, ETH_HLEN);
423         skb_set_transport_header(skb, ETH_HLEN + sizeof(*iph));
424         buf += sizeof(*tcph);
425
426         skb->ip_summed = CHECKSUM_PARTIAL;
427         skb->protocol = htons(0x800);
428         skb->data_len = 0;
429         skb->mac_len = ETH_HLEN;
430
431         memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
432         memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
433         ethh->h_proto = htons(0x0800);
434
435         iph->version = IPVERSION;
436         iph->ihl = 5;           /* 5 * 4Byte words, IP headr len */
437         iph->tos = 0;
438         iph->tot_len = htons(packetsize);
439         iph->id = htons(++cm_node->tcp_cntxt.loc_id);
440
441         iph->frag_off = htons(0x4000);
442         iph->ttl = 0x40;
443         iph->protocol = 0x06;   /* IPPROTO_TCP */
444
445         iph->saddr = htonl(cm_node->loc_addr);
446         iph->daddr = htonl(cm_node->rem_addr);
447
448         tcph->source = htons(cm_node->loc_port);
449         tcph->dest = htons(cm_node->rem_port);
450         tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
451
452         if (flags & SET_ACK) {
453                 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
454                 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
455                 tcph->ack = 1;
456         } else {
457                 tcph->ack_seq = 0;
458         }
459
460         if (flags & SET_SYN) {
461                 cm_node->tcp_cntxt.loc_seq_num++;
462                 tcph->syn = 1;
463         } else {
464                 cm_node->tcp_cntxt.loc_seq_num += datasize;
465         }
466
467         if (flags & SET_FIN) {
468                 cm_node->tcp_cntxt.loc_seq_num++;
469                 tcph->fin = 1;
470         }
471
472         if (flags & SET_RST)
473                 tcph->rst = 1;
474
475         tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
476         tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
477         tcph->urg_ptr = 0;
478         if (optionsize)
479                 memcpy(buf, options, optionsize);
480         buf += optionsize;
481         if (datasize)
482                 memcpy(buf, data, datasize);
483
484         skb_shinfo(skb)->nr_frags = 0;
485         cm_packets_created++;
486 }
487
488 /**
489  * print_core - dump a cm core
490  */
491 static void print_core(struct nes_cm_core *core)
492 {
493         nes_debug(NES_DBG_CM, "---------------------------------------------\n");
494         nes_debug(NES_DBG_CM, "CM Core  -- (core = %p )\n", core);
495         if (!core)
496                 return;
497         nes_debug(NES_DBG_CM, "---------------------------------------------\n");
498
499         nes_debug(NES_DBG_CM, "State         : %u \n", core->state);
500
501         nes_debug(NES_DBG_CM, "Listen Nodes  : %u \n", atomic_read(&core->listen_node_cnt));
502         nes_debug(NES_DBG_CM, "Active Nodes  : %u \n", atomic_read(&core->node_cnt));
503
504         nes_debug(NES_DBG_CM, "core          : %p \n", core);
505
506         nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
507 }
508
509 /**
510  * cm_build_mpa_frame - build a MPA V1 frame or MPA V2 frame
511  */
512 static int cm_build_mpa_frame(struct nes_cm_node *cm_node, u8 **start_buff,
513                               u16 *buff_len, u8 *pci_mem, u8 mpa_key)
514 {
515         int ret = 0;
516
517         *start_buff = (pci_mem) ? pci_mem : &cm_node->mpa_frame_buf[0];
518
519         switch (cm_node->mpa_frame_rev) {
520         case IETF_MPA_V1:
521                 *start_buff = (u8 *)*start_buff + sizeof(struct ietf_rtr_msg);
522                 *buff_len = sizeof(struct ietf_mpa_v1) + cm_node->mpa_frame_size;
523                 build_mpa_v1(cm_node, *start_buff, mpa_key);
524                 break;
525         case IETF_MPA_V2:
526                 *buff_len = sizeof(struct ietf_mpa_v2) + cm_node->mpa_frame_size;
527                 build_mpa_v2(cm_node, *start_buff, mpa_key);
528                 break;
529         default:
530                 ret = -EINVAL;
531         }
532         return ret;
533 }
534
535 /**
536  * build_mpa_v2 - build a MPA V2 frame
537  */
538 static void build_mpa_v2(struct nes_cm_node *cm_node,
539                          void *start_addr, u8 mpa_key)
540 {
541         struct ietf_mpa_v2 *mpa_frame = (struct ietf_mpa_v2 *)start_addr;
542         struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
543
544         /* initialize the upper 5 bytes of the frame */
545         build_mpa_v1(cm_node, start_addr, mpa_key);
546         mpa_frame->flags |= IETF_MPA_V2_FLAG; /* set a bit to indicate MPA V2 */
547         mpa_frame->priv_data_len += htons(IETF_RTR_MSG_SIZE);
548
549         /* initialize RTR msg */
550         rtr_msg->ctrl_ird = (cm_node->ird_size > IETF_NO_IRD_ORD) ?
551                             IETF_NO_IRD_ORD : cm_node->ird_size;
552         rtr_msg->ctrl_ord = (cm_node->ord_size > IETF_NO_IRD_ORD) ?
553                             IETF_NO_IRD_ORD : cm_node->ord_size;
554
555         rtr_msg->ctrl_ird |= IETF_PEER_TO_PEER;
556         rtr_msg->ctrl_ird |= IETF_FLPDU_ZERO_LEN;
557
558         switch (mpa_key) {
559         case MPA_KEY_REQUEST:
560                 rtr_msg->ctrl_ord |= IETF_RDMA0_WRITE;
561                 rtr_msg->ctrl_ord |= IETF_RDMA0_READ;
562                 break;
563         case MPA_KEY_REPLY:
564                 switch (cm_node->send_rdma0_op) {
565                 case SEND_RDMA_WRITE_ZERO:
566                         rtr_msg->ctrl_ord |= IETF_RDMA0_WRITE;
567                         break;
568                 case SEND_RDMA_READ_ZERO:
569                         rtr_msg->ctrl_ord |= IETF_RDMA0_READ;
570                         break;
571                 }
572         }
573         rtr_msg->ctrl_ird = htons(rtr_msg->ctrl_ird);
574         rtr_msg->ctrl_ord = htons(rtr_msg->ctrl_ord);
575 }
576
577 /**
578  * build_mpa_v1 - build a MPA V1 frame
579  */
580 static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
581 {
582         struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
583
584         switch (mpa_key) {
585         case MPA_KEY_REQUEST:
586                 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
587                 break;
588         case MPA_KEY_REPLY:
589                 memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
590                 break;
591         }
592         mpa_frame->flags = IETF_MPA_FLAGS_CRC;
593         mpa_frame->rev = cm_node->mpa_frame_rev;
594         mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
595 }
596
597 static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
598 {
599         u64 u64temp;
600         struct nes_qp *nesqp = *nesqp_addr;
601         struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
602
603         u64temp = (unsigned long)nesqp;
604         u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
605         set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
606
607         wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
608         wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
609
610         switch (cm_node->send_rdma0_op) {
611         case SEND_RDMA_WRITE_ZERO:
612                 nes_debug(NES_DBG_CM, "Sending first write.\n");
613                 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
614                         cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
615                 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
616                 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
617                 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
618                 break;
619
620         case SEND_RDMA_READ_ZERO:
621         default:
622                 if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO) {
623                         printk(KERN_ERR "%s[%u]: Unsupported RDMA0 len operation=%u\n",
624                                  __func__, __LINE__, cm_node->send_rdma0_op);
625                         WARN_ON(1);
626                 }
627                 nes_debug(NES_DBG_CM, "Sending first rdma operation.\n");
628                 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
629                         cpu_to_le32(NES_IWARP_SQ_OP_RDMAR);
630                 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX] = 1;
631                 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_HIGH_IDX] = 0;
632                 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] = 0;
633                 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_STAG_IDX] = 1;
634                 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 1;
635                 break;
636         }
637
638         if (nesqp->sq_kmapped) {
639                 nesqp->sq_kmapped = 0;
640                 kunmap(nesqp->page);
641         }
642
643         /*use the reserved spot on the WQ for the extra first WQE*/
644         nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
645                                                              NES_QPCONTEXT_ORDIRD_WRPDU |
646                                                              NES_QPCONTEXT_ORDIRD_ALSMM));
647         nesqp->skip_lsmm = 1;
648         nesqp->hwqp.sq_tail = 0;
649 }
650
651 /**
652  * schedule_nes_timer
653  * note - cm_node needs to be protected before calling this. Encase in:
654  *                      rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
655  */
656 int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
657                        enum nes_timer_type type, int send_retrans,
658                        int close_when_complete)
659 {
660         unsigned long flags;
661         struct nes_cm_core *cm_core = cm_node->cm_core;
662         struct nes_timer_entry *new_send;
663         int ret = 0;
664         u32 was_timer_set;
665
666         new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
667         if (!new_send)
668                 return -ENOMEM;
669
670         /* new_send->timetosend = currenttime */
671         new_send->retrycount = NES_DEFAULT_RETRYS;
672         new_send->retranscount = NES_DEFAULT_RETRANS;
673         new_send->skb = skb;
674         new_send->timetosend = jiffies;
675         new_send->type = type;
676         new_send->netdev = cm_node->netdev;
677         new_send->send_retrans = send_retrans;
678         new_send->close_when_complete = close_when_complete;
679
680         if (type == NES_TIMER_TYPE_CLOSE) {
681                 new_send->timetosend += (HZ / 10);
682                 if (cm_node->recv_entry) {
683                         kfree(new_send);
684                         WARN_ON(1);
685                         return -EINVAL;
686                 }
687                 cm_node->recv_entry = new_send;
688         }
689
690         if (type == NES_TIMER_TYPE_SEND) {
691                 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
692                 atomic_inc(&new_send->skb->users);
693                 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
694                 cm_node->send_entry = new_send;
695                 add_ref_cm_node(cm_node);
696                 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
697                 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
698
699                 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
700                 if (ret != NETDEV_TX_OK) {
701                         nes_debug(NES_DBG_CM, "Error sending packet %p "
702                                   "(jiffies = %lu)\n", new_send, jiffies);
703                         new_send->timetosend = jiffies;
704                         ret = NETDEV_TX_OK;
705                 } else {
706                         cm_packets_sent++;
707                         if (!send_retrans) {
708                                 cleanup_retrans_entry(cm_node);
709                                 if (close_when_complete)
710                                         rem_ref_cm_node(cm_core, cm_node);
711                                 return ret;
712                         }
713                 }
714         }
715
716         was_timer_set = timer_pending(&cm_core->tcp_timer);
717
718         if (!was_timer_set) {
719                 cm_core->tcp_timer.expires = new_send->timetosend;
720                 add_timer(&cm_core->tcp_timer);
721         }
722
723         return ret;
724 }
725
726 static void nes_retrans_expired(struct nes_cm_node *cm_node)
727 {
728         struct iw_cm_id *cm_id = cm_node->cm_id;
729         enum nes_cm_node_state state = cm_node->state;
730         cm_node->state = NES_CM_STATE_CLOSED;
731
732         switch (state) {
733         case NES_CM_STATE_SYN_RCVD:
734         case NES_CM_STATE_CLOSING:
735                 rem_ref_cm_node(cm_node->cm_core, cm_node);
736                 break;
737         case NES_CM_STATE_LAST_ACK:
738         case NES_CM_STATE_FIN_WAIT1:
739                 if (cm_node->cm_id)
740                         cm_id->rem_ref(cm_id);
741                 send_reset(cm_node, NULL);
742                 break;
743         default:
744                 add_ref_cm_node(cm_node);
745                 send_reset(cm_node, NULL);
746                 create_event(cm_node, NES_CM_EVENT_ABORTED);
747         }
748 }
749
750 static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
751 {
752         struct nes_timer_entry *recv_entry = cm_node->recv_entry;
753         struct iw_cm_id *cm_id = cm_node->cm_id;
754         struct nes_qp *nesqp;
755         unsigned long qplockflags;
756
757         if (!recv_entry)
758                 return;
759         nesqp = (struct nes_qp *)recv_entry->skb;
760         if (nesqp) {
761                 spin_lock_irqsave(&nesqp->lock, qplockflags);
762                 if (nesqp->cm_id) {
763                         nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
764                                   "refcount = %d: HIT A "
765                                   "NES_TIMER_TYPE_CLOSE with something "
766                                   "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
767                                   atomic_read(&nesqp->refcount));
768                         nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
769                         nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
770                         nesqp->ibqp_state = IB_QPS_ERR;
771                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
772                         nes_cm_disconn(nesqp);
773                 } else {
774                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
775                         nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
776                                   "refcount = %d: HIT A "
777                                   "NES_TIMER_TYPE_CLOSE with nothing "
778                                   "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
779                                   atomic_read(&nesqp->refcount));
780                 }
781         } else if (rem_node) {
782                 /* TIME_WAIT state */
783                 rem_ref_cm_node(cm_node->cm_core, cm_node);
784         }
785         if (cm_node->cm_id)
786                 cm_id->rem_ref(cm_id);
787         kfree(recv_entry);
788         cm_node->recv_entry = NULL;
789 }
790
791 /**
792  * nes_cm_timer_tick
793  */
794 static void nes_cm_timer_tick(unsigned long pass)
795 {
796         unsigned long flags;
797         unsigned long nexttimeout = jiffies + NES_LONG_TIME;
798         struct nes_cm_node *cm_node;
799         struct nes_timer_entry *send_entry, *recv_entry;
800         struct list_head *list_core_temp;
801         struct list_head *list_node;
802         struct nes_cm_core *cm_core = g_cm_core;
803         u32 settimer = 0;
804         unsigned long timetosend;
805         int ret = NETDEV_TX_OK;
806
807         struct list_head timer_list;
808
809         INIT_LIST_HEAD(&timer_list);
810         spin_lock_irqsave(&cm_core->ht_lock, flags);
811
812         list_for_each_safe(list_node, list_core_temp,
813                            &cm_core->connected_nodes) {
814                 cm_node = container_of(list_node, struct nes_cm_node, list);
815                 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
816                         add_ref_cm_node(cm_node);
817                         list_add(&cm_node->timer_entry, &timer_list);
818                 }
819         }
820         spin_unlock_irqrestore(&cm_core->ht_lock, flags);
821
822         list_for_each_safe(list_node, list_core_temp, &timer_list) {
823                 cm_node = container_of(list_node, struct nes_cm_node,
824                                        timer_entry);
825                 recv_entry = cm_node->recv_entry;
826
827                 if (recv_entry) {
828                         if (time_after(recv_entry->timetosend, jiffies)) {
829                                 if (nexttimeout > recv_entry->timetosend ||
830                                     !settimer) {
831                                         nexttimeout = recv_entry->timetosend;
832                                         settimer = 1;
833                                 }
834                         } else {
835                                 handle_recv_entry(cm_node, 1);
836                         }
837                 }
838
839                 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
840                 do {
841                         send_entry = cm_node->send_entry;
842                         if (!send_entry)
843                                 break;
844                         if (time_after(send_entry->timetosend, jiffies)) {
845                                 if (cm_node->state != NES_CM_STATE_TSA) {
846                                         if ((nexttimeout >
847                                              send_entry->timetosend) ||
848                                             !settimer) {
849                                                 nexttimeout =
850                                                         send_entry->timetosend;
851                                                 settimer = 1;
852                                         }
853                                 } else {
854                                         free_retrans_entry(cm_node);
855                                 }
856                                 break;
857                         }
858
859                         if ((cm_node->state == NES_CM_STATE_TSA) ||
860                             (cm_node->state == NES_CM_STATE_CLOSED)) {
861                                 free_retrans_entry(cm_node);
862                                 break;
863                         }
864
865                         if (!send_entry->retranscount ||
866                             !send_entry->retrycount) {
867                                 cm_packets_dropped++;
868                                 free_retrans_entry(cm_node);
869
870                                 spin_unlock_irqrestore(
871                                         &cm_node->retrans_list_lock, flags);
872                                 nes_retrans_expired(cm_node);
873                                 cm_node->state = NES_CM_STATE_CLOSED;
874                                 spin_lock_irqsave(&cm_node->retrans_list_lock,
875                                                   flags);
876                                 break;
877                         }
878                         atomic_inc(&send_entry->skb->users);
879                         cm_packets_retrans++;
880                         nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
881                                   "for node %p, jiffies = %lu, time to send = "
882                                   "%lu, retranscount = %u, send_entry->seq_num = "
883                                   "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
884                                   "0x%08X\n", send_entry, cm_node, jiffies,
885                                   send_entry->timetosend,
886                                   send_entry->retranscount,
887                                   send_entry->seq_num,
888                                   cm_node->tcp_cntxt.rem_ack_num);
889
890                         spin_unlock_irqrestore(&cm_node->retrans_list_lock,
891                                                flags);
892                         ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
893                         spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
894                         if (ret != NETDEV_TX_OK) {
895                                 nes_debug(NES_DBG_CM, "rexmit failed for "
896                                           "node=%p\n", cm_node);
897                                 cm_packets_bounced++;
898                                 send_entry->retrycount--;
899                                 nexttimeout = jiffies + NES_SHORT_TIME;
900                                 settimer = 1;
901                                 break;
902                         } else {
903                                 cm_packets_sent++;
904                         }
905                         nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
906                                   "%u, retry count = %u.\n",
907                                   send_entry->retranscount,
908                                   send_entry->retrycount);
909                         if (send_entry->send_retrans) {
910                                 send_entry->retranscount--;
911                                 timetosend = (NES_RETRY_TIMEOUT <<
912                                               (NES_DEFAULT_RETRANS - send_entry->retranscount));
913
914                                 send_entry->timetosend = jiffies +
915                                                          min(timetosend, NES_MAX_TIMEOUT);
916                                 if (nexttimeout > send_entry->timetosend ||
917                                     !settimer) {
918                                         nexttimeout = send_entry->timetosend;
919                                         settimer = 1;
920                                 }
921                         } else {
922                                 int close_when_complete;
923                                 close_when_complete =
924                                         send_entry->close_when_complete;
925                                 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
926                                           cm_node, cm_node->state);
927                                 free_retrans_entry(cm_node);
928                                 if (close_when_complete)
929                                         rem_ref_cm_node(cm_node->cm_core,
930                                                         cm_node);
931                         }
932                 } while (0);
933
934                 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
935                 rem_ref_cm_node(cm_node->cm_core, cm_node);
936         }
937
938         if (settimer) {
939                 if (!timer_pending(&cm_core->tcp_timer)) {
940                         cm_core->tcp_timer.expires = nexttimeout;
941                         add_timer(&cm_core->tcp_timer);
942                 }
943         }
944 }
945
946
947 /**
948  * send_syn
949  */
950 static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
951                     struct sk_buff *skb)
952 {
953         int ret;
954         int flags = SET_SYN;
955         char optionsbuffer[sizeof(struct option_mss) +
956                            sizeof(struct option_windowscale) + sizeof(struct option_base) +
957                            TCP_OPTIONS_PADDING];
958
959         int optionssize = 0;
960         /* Sending MSS option */
961         union all_known_options *options;
962
963         if (!cm_node)
964                 return -EINVAL;
965
966         options = (union all_known_options *)&optionsbuffer[optionssize];
967         options->as_mss.optionnum = OPTION_NUMBER_MSS;
968         options->as_mss.length = sizeof(struct option_mss);
969         options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
970         optionssize += sizeof(struct option_mss);
971
972         options = (union all_known_options *)&optionsbuffer[optionssize];
973         options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
974         options->as_windowscale.length = sizeof(struct option_windowscale);
975         options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
976         optionssize += sizeof(struct option_windowscale);
977
978         if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
979                 options = (union all_known_options *)&optionsbuffer[optionssize];
980                 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
981                 options->as_base.length = sizeof(struct option_base);
982                 optionssize += sizeof(struct option_base);
983                 /* we need the size to be a multiple of 4 */
984                 options = (union all_known_options *)&optionsbuffer[optionssize];
985                 options->as_end = 1;
986                 optionssize += 1;
987                 options = (union all_known_options *)&optionsbuffer[optionssize];
988                 options->as_end = 1;
989                 optionssize += 1;
990         }
991
992         options = (union all_known_options *)&optionsbuffer[optionssize];
993         options->as_end = OPTION_NUMBER_END;
994         optionssize += 1;
995
996         if (!skb)
997                 skb = dev_alloc_skb(MAX_CM_BUFFER);
998         if (!skb) {
999                 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1000                 return -1;
1001         }
1002
1003         if (sendack)
1004                 flags |= SET_ACK;
1005
1006         form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
1007         ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1008
1009         return ret;
1010 }
1011
1012
1013 /**
1014  * send_reset
1015  */
1016 static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
1017 {
1018         int ret;
1019         int flags = SET_RST | SET_ACK;
1020
1021         if (!skb)
1022                 skb = dev_alloc_skb(MAX_CM_BUFFER);
1023         if (!skb) {
1024                 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1025                 return -ENOMEM;
1026         }
1027
1028         form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
1029         ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
1030
1031         return ret;
1032 }
1033
1034
1035 /**
1036  * send_ack
1037  */
1038 static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
1039 {
1040         int ret;
1041
1042         if (!skb)
1043                 skb = dev_alloc_skb(MAX_CM_BUFFER);
1044
1045         if (!skb) {
1046                 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1047                 return -1;
1048         }
1049
1050         form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
1051         ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
1052
1053         return ret;
1054 }
1055
1056
1057 /**
1058  * send_fin
1059  */
1060 static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
1061 {
1062         int ret;
1063
1064         /* if we didn't get a frame get one */
1065         if (!skb)
1066                 skb = dev_alloc_skb(MAX_CM_BUFFER);
1067
1068         if (!skb) {
1069                 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1070                 return -1;
1071         }
1072
1073         form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
1074         ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1075
1076         return ret;
1077 }
1078
1079
1080 /**
1081  * find_node - find a cm node that matches the reference cm node
1082  */
1083 static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
1084                                      u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
1085 {
1086         unsigned long flags;
1087         struct list_head *hte;
1088         struct nes_cm_node *cm_node;
1089
1090         /* get a handle on the hte */
1091         hte = &cm_core->connected_nodes;
1092
1093         /* walk list and find cm_node associated with this session ID */
1094         spin_lock_irqsave(&cm_core->ht_lock, flags);
1095         list_for_each_entry(cm_node, hte, list) {
1096                 /* compare quad, return node handle if a match */
1097                 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
1098                           cm_node->loc_addr, cm_node->loc_port,
1099                           loc_addr, loc_port,
1100                           cm_node->rem_addr, cm_node->rem_port,
1101                           rem_addr, rem_port);
1102                 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
1103                     (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
1104                         add_ref_cm_node(cm_node);
1105                         spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1106                         return cm_node;
1107                 }
1108         }
1109         spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1110
1111         /* no owner node */
1112         return NULL;
1113 }
1114
1115
1116 /**
1117  * find_listener - find a cm node listening on this addr-port pair
1118  */
1119 static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
1120                                              nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
1121 {
1122         unsigned long flags;
1123         struct nes_cm_listener *listen_node;
1124
1125         /* walk list and find cm_node associated with this session ID */
1126         spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1127         list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
1128                 /* compare node pair, return node handle if a match */
1129                 if (((listen_node->loc_addr == dst_addr) ||
1130                      listen_node->loc_addr == 0x00000000) &&
1131                     (listen_node->loc_port == dst_port) &&
1132                     (listener_state & listen_node->listener_state)) {
1133                         atomic_inc(&listen_node->ref_count);
1134                         spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1135                         return listen_node;
1136                 }
1137         }
1138         spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1139
1140         /* no listener */
1141         return NULL;
1142 }
1143
1144
1145 /**
1146  * add_hte_node - add a cm node to the hash table
1147  */
1148 static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1149 {
1150         unsigned long flags;
1151         struct list_head *hte;
1152
1153         if (!cm_node || !cm_core)
1154                 return -EINVAL;
1155
1156         nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
1157                   cm_node);
1158
1159         spin_lock_irqsave(&cm_core->ht_lock, flags);
1160
1161         /* get a handle on the hash table element (list head for this slot) */
1162         hte = &cm_core->connected_nodes;
1163         list_add_tail(&cm_node->list, hte);
1164         atomic_inc(&cm_core->ht_node_cnt);
1165
1166         spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1167
1168         return 0;
1169 }
1170
1171
1172 /**
1173  * mini_cm_dec_refcnt_listen
1174  */
1175 static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
1176                                      struct nes_cm_listener *listener, int free_hanging_nodes)
1177 {
1178         int ret = -EINVAL;
1179         int err = 0;
1180         unsigned long flags;
1181         struct list_head *list_pos = NULL;
1182         struct list_head *list_temp = NULL;
1183         struct nes_cm_node *cm_node = NULL;
1184         struct list_head reset_list;
1185
1186         nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
1187                   "refcnt=%d\n", listener, free_hanging_nodes,
1188                   atomic_read(&listener->ref_count));
1189         /* free non-accelerated child nodes for this listener */
1190         INIT_LIST_HEAD(&reset_list);
1191         if (free_hanging_nodes) {
1192                 spin_lock_irqsave(&cm_core->ht_lock, flags);
1193                 list_for_each_safe(list_pos, list_temp,
1194                                    &g_cm_core->connected_nodes) {
1195                         cm_node = container_of(list_pos, struct nes_cm_node,
1196                                                list);
1197                         if ((cm_node->listener == listener) &&
1198                             (!cm_node->accelerated)) {
1199                                 add_ref_cm_node(cm_node);
1200                                 list_add(&cm_node->reset_entry, &reset_list);
1201                         }
1202                 }
1203                 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1204         }
1205
1206         list_for_each_safe(list_pos, list_temp, &reset_list) {
1207                 cm_node = container_of(list_pos, struct nes_cm_node,
1208                                        reset_entry);
1209                 {
1210                         struct nes_cm_node *loopback = cm_node->loopbackpartner;
1211                         enum nes_cm_node_state old_state;
1212                         if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
1213                                 rem_ref_cm_node(cm_node->cm_core, cm_node);
1214                         } else {
1215                                 if (!loopback) {
1216                                         cleanup_retrans_entry(cm_node);
1217                                         err = send_reset(cm_node, NULL);
1218                                         if (err) {
1219                                                 cm_node->state =
1220                                                         NES_CM_STATE_CLOSED;
1221                                                 WARN_ON(1);
1222                                         } else {
1223                                                 old_state = cm_node->state;
1224                                                 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1225                                                 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1226                                                         rem_ref_cm_node(
1227                                                                 cm_node->cm_core,
1228                                                                 cm_node);
1229                                         }
1230                                 } else {
1231                                         struct nes_cm_event event;
1232
1233                                         event.cm_node = loopback;
1234                                         event.cm_info.rem_addr =
1235                                                         loopback->rem_addr;
1236                                         event.cm_info.loc_addr =
1237                                                         loopback->loc_addr;
1238                                         event.cm_info.rem_port =
1239                                                         loopback->rem_port;
1240                                         event.cm_info.loc_port =
1241                                                          loopback->loc_port;
1242                                         event.cm_info.cm_id = loopback->cm_id;
1243                                         add_ref_cm_node(loopback);
1244                                         loopback->state = NES_CM_STATE_CLOSED;
1245                                         cm_event_connect_error(&event);
1246                                         cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1247
1248                                         rem_ref_cm_node(cm_node->cm_core,
1249                                                          cm_node);
1250
1251                                 }
1252                         }
1253                 }
1254         }
1255
1256         spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1257         if (!atomic_dec_return(&listener->ref_count)) {
1258                 list_del(&listener->list);
1259
1260                 /* decrement our listen node count */
1261                 atomic_dec(&cm_core->listen_node_cnt);
1262
1263                 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1264
1265                 if (listener->nesvnic)
1266                         nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1267                                          PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1268
1269                 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1270
1271                 kfree(listener);
1272                 listener = NULL;
1273                 ret = 0;
1274                 atomic_inc(&cm_listens_destroyed);
1275         } else {
1276                 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1277         }
1278         if (listener) {
1279                 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1280                         nes_debug(NES_DBG_CM, "destroying listener (%p)"
1281                                   " with non-zero pending accepts=%u\n",
1282                                   listener, atomic_read(&listener->pend_accepts_cnt));
1283         }
1284
1285         return ret;
1286 }
1287
1288
1289 /**
1290  * mini_cm_del_listen
1291  */
1292 static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1293                               struct nes_cm_listener *listener)
1294 {
1295         listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1296         listener->cm_id = NULL; /* going to be destroyed pretty soon */
1297         return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1298 }
1299
1300
1301 /**
1302  * mini_cm_accelerated
1303  */
1304 static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1305                                       struct nes_cm_node *cm_node)
1306 {
1307         u32 was_timer_set;
1308
1309         cm_node->accelerated = 1;
1310
1311         if (cm_node->accept_pend) {
1312                 BUG_ON(!cm_node->listener);
1313                 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1314                 cm_node->accept_pend = 0;
1315                 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1316         }
1317
1318         was_timer_set = timer_pending(&cm_core->tcp_timer);
1319         if (!was_timer_set) {
1320                 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1321                 add_timer(&cm_core->tcp_timer);
1322         }
1323
1324         return 0;
1325 }
1326
1327
1328 /**
1329  * nes_addr_resolve_neigh
1330  */
1331 static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
1332 {
1333         struct rtable *rt;
1334         struct neighbour *neigh;
1335         int rc = arpindex;
1336         struct net_device *netdev;
1337         struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
1338
1339         rt = ip_route_output(&init_net, htonl(dst_ip), 0, 0, 0);
1340         if (IS_ERR(rt)) {
1341                 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
1342                        __func__, dst_ip);
1343                 return rc;
1344         }
1345
1346         if (netif_is_bond_slave(nesvnic->netdev))
1347                 netdev = nesvnic->netdev->master;
1348         else
1349                 netdev = nesvnic->netdev;
1350
1351         neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, netdev);
1352         if (neigh) {
1353                 if (neigh->nud_state & NUD_VALID) {
1354                         nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
1355                                   " is %pM, Gateway is 0x%08X \n", dst_ip,
1356                                   neigh->ha, ntohl(rt->rt_gateway));
1357
1358                         if (arpindex >= 0) {
1359                                 if (!memcmp(nesadapter->arp_table[arpindex].mac_addr,
1360                                             neigh->ha, ETH_ALEN)) {
1361                                         /* Mac address same as in nes_arp_table */
1362                                         neigh_release(neigh);
1363                                         ip_rt_put(rt);
1364                                         return rc;
1365                                 }
1366
1367                                 nes_manage_arp_cache(nesvnic->netdev,
1368                                                      nesadapter->arp_table[arpindex].mac_addr,
1369                                                      dst_ip, NES_ARP_DELETE);
1370                         }
1371
1372                         nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1373                                              dst_ip, NES_ARP_ADD);
1374                         rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1375                                            NES_ARP_RESOLVE);
1376                 }
1377                 neigh_release(neigh);
1378         }
1379
1380         if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1381                 neigh_event_send(dst_get_neighbour(&rt->dst), NULL);
1382
1383         ip_rt_put(rt);
1384         return rc;
1385 }
1386
1387 /**
1388  * make_cm_node - create a new instance of a cm node
1389  */
1390 static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1391                                         struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1392                                         struct nes_cm_listener *listener)
1393 {
1394         struct nes_cm_node *cm_node;
1395         struct timespec ts;
1396         int oldarpindex = 0;
1397         int arpindex = 0;
1398         struct nes_device *nesdev;
1399         struct nes_adapter *nesadapter;
1400
1401         /* create an hte and cm_node for this instance */
1402         cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1403         if (!cm_node)
1404                 return NULL;
1405
1406         /* set our node specific transport info */
1407         cm_node->loc_addr = cm_info->loc_addr;
1408         cm_node->rem_addr = cm_info->rem_addr;
1409         cm_node->loc_port = cm_info->loc_port;
1410         cm_node->rem_port = cm_info->rem_port;
1411
1412         cm_node->mpa_frame_rev = mpa_version;
1413         cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
1414         cm_node->ird_size = IETF_NO_IRD_ORD;
1415         cm_node->ord_size = IETF_NO_IRD_ORD;
1416
1417         nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1418                   &cm_node->loc_addr, cm_node->loc_port,
1419                   &cm_node->rem_addr, cm_node->rem_port);
1420         cm_node->listener = listener;
1421         cm_node->netdev = nesvnic->netdev;
1422         cm_node->cm_id = cm_info->cm_id;
1423         memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1424
1425         nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1426                   cm_node->cm_id);
1427
1428         spin_lock_init(&cm_node->retrans_list_lock);
1429
1430         cm_node->loopbackpartner = NULL;
1431         atomic_set(&cm_node->ref_count, 1);
1432         /* associate our parent CM core */
1433         cm_node->cm_core = cm_core;
1434         cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1435         cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1436         cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1437                                      NES_CM_DEFAULT_RCV_WND_SCALE;
1438         ts = current_kernel_time();
1439         cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1440         cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
1441                                  sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
1442         cm_node->tcp_cntxt.rcv_nxt = 0;
1443         /* get a unique session ID , add thread_id to an upcounter to handle race */
1444         atomic_inc(&cm_core->node_cnt);
1445         cm_node->conn_type = cm_info->conn_type;
1446         cm_node->apbvt_set = 0;
1447         cm_node->accept_pend = 0;
1448
1449         cm_node->nesvnic = nesvnic;
1450         /* get some device handles, for arp lookup */
1451         nesdev = nesvnic->nesdev;
1452         nesadapter = nesdev->nesadapter;
1453
1454         cm_node->loopbackpartner = NULL;
1455
1456         /* get the mac addr for the remote node */
1457         if (ipv4_is_loopback(htonl(cm_node->rem_addr))) {
1458                 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
1459         } else {
1460                 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1461                 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);
1462         }
1463         if (arpindex < 0) {
1464                 kfree(cm_node);
1465                 return NULL;
1466         }
1467
1468         /* copy the mac addr to node context */
1469         memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
1470         nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1471                   cm_node->rem_mac);
1472
1473         add_hte_node(cm_core, cm_node);
1474         atomic_inc(&cm_nodes_created);
1475
1476         return cm_node;
1477 }
1478
1479
1480 /**
1481  * add_ref_cm_node - destroy an instance of a cm node
1482  */
1483 static int add_ref_cm_node(struct nes_cm_node *cm_node)
1484 {
1485         atomic_inc(&cm_node->ref_count);
1486         return 0;
1487 }
1488
1489
1490 /**
1491  * rem_ref_cm_node - destroy an instance of a cm node
1492  */
1493 static int rem_ref_cm_node(struct nes_cm_core *cm_core,
1494                            struct nes_cm_node *cm_node)
1495 {
1496         unsigned long flags;
1497         struct nes_qp *nesqp;
1498
1499         if (!cm_node)
1500                 return -EINVAL;
1501
1502         spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1503         if (atomic_dec_return(&cm_node->ref_count)) {
1504                 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1505                 return 0;
1506         }
1507         list_del(&cm_node->list);
1508         atomic_dec(&cm_core->ht_node_cnt);
1509         spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1510
1511         /* if the node is destroyed before connection was accelerated */
1512         if (!cm_node->accelerated && cm_node->accept_pend) {
1513                 BUG_ON(!cm_node->listener);
1514                 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1515                 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1516         }
1517         WARN_ON(cm_node->send_entry);
1518         if (cm_node->recv_entry)
1519                 handle_recv_entry(cm_node, 0);
1520         if (cm_node->listener) {
1521                 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1522         } else {
1523                 if (cm_node->apbvt_set && cm_node->nesvnic) {
1524                         nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
1525                                          PCI_FUNC(
1526                                                  cm_node->nesvnic->nesdev->pcidev->devfn),
1527                                          NES_MANAGE_APBVT_DEL);
1528                 }
1529         }
1530
1531         atomic_dec(&cm_core->node_cnt);
1532         atomic_inc(&cm_nodes_destroyed);
1533         nesqp = cm_node->nesqp;
1534         if (nesqp) {
1535                 nesqp->cm_node = NULL;
1536                 nes_rem_ref(&nesqp->ibqp);
1537                 cm_node->nesqp = NULL;
1538         }
1539
1540         kfree(cm_node);
1541         return 0;
1542 }
1543
1544 /**
1545  * process_options
1546  */
1547 static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1548                            u32 optionsize, u32 syn_packet)
1549 {
1550         u32 tmp;
1551         u32 offset = 0;
1552         union all_known_options *all_options;
1553         char got_mss_option = 0;
1554
1555         while (offset < optionsize) {
1556                 all_options = (union all_known_options *)(optionsloc + offset);
1557                 switch (all_options->as_base.optionnum) {
1558                 case OPTION_NUMBER_END:
1559                         offset = optionsize;
1560                         break;
1561                 case OPTION_NUMBER_NONE:
1562                         offset += 1;
1563                         continue;
1564                 case OPTION_NUMBER_MSS:
1565                         nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1566                                   "Size: %d\n", __func__,
1567                                   all_options->as_mss.length, offset, optionsize);
1568                         got_mss_option = 1;
1569                         if (all_options->as_mss.length != 4) {
1570                                 return 1;
1571                         } else {
1572                                 tmp = ntohs(all_options->as_mss.mss);
1573                                 if (tmp > 0 && tmp <
1574                                     cm_node->tcp_cntxt.mss)
1575                                         cm_node->tcp_cntxt.mss = tmp;
1576                         }
1577                         break;
1578                 case OPTION_NUMBER_WINDOW_SCALE:
1579                         cm_node->tcp_cntxt.snd_wscale =
1580                                 all_options->as_windowscale.shiftcount;
1581                         break;
1582                 default:
1583                         nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1584                                   all_options->as_base.optionnum);
1585                         break;
1586                 }
1587                 offset += all_options->as_base.length;
1588         }
1589         if ((!got_mss_option) && (syn_packet))
1590                 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1591         return 0;
1592 }
1593
1594 static void drop_packet(struct sk_buff *skb)
1595 {
1596         atomic_inc(&cm_accel_dropped_pkts);
1597         dev_kfree_skb_any(skb);
1598 }
1599
1600 static void handle_fin_pkt(struct nes_cm_node *cm_node)
1601 {
1602         nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1603                   "refcnt=%d\n", cm_node, cm_node->state,
1604                   atomic_read(&cm_node->ref_count));
1605         switch (cm_node->state) {
1606         case NES_CM_STATE_SYN_RCVD:
1607         case NES_CM_STATE_SYN_SENT:
1608         case NES_CM_STATE_ESTABLISHED:
1609         case NES_CM_STATE_MPAREJ_RCVD:
1610                 cm_node->tcp_cntxt.rcv_nxt++;
1611                 cleanup_retrans_entry(cm_node);
1612                 cm_node->state = NES_CM_STATE_LAST_ACK;
1613                 send_fin(cm_node, NULL);
1614                 break;
1615         case NES_CM_STATE_MPAREQ_SENT:
1616                 create_event(cm_node, NES_CM_EVENT_ABORTED);
1617                 cm_node->tcp_cntxt.rcv_nxt++;
1618                 cleanup_retrans_entry(cm_node);
1619                 cm_node->state = NES_CM_STATE_CLOSED;
1620                 add_ref_cm_node(cm_node);
1621                 send_reset(cm_node, NULL);
1622                 break;
1623         case NES_CM_STATE_FIN_WAIT1:
1624                 cm_node->tcp_cntxt.rcv_nxt++;
1625                 cleanup_retrans_entry(cm_node);
1626                 cm_node->state = NES_CM_STATE_CLOSING;
1627                 send_ack(cm_node, NULL);
1628                 /* Wait for ACK as this is simultaneous close..
1629                 * After we receive ACK, do not send anything..
1630                 * Just rm the node.. Done.. */
1631                 break;
1632         case NES_CM_STATE_FIN_WAIT2:
1633                 cm_node->tcp_cntxt.rcv_nxt++;
1634                 cleanup_retrans_entry(cm_node);
1635                 cm_node->state = NES_CM_STATE_TIME_WAIT;
1636                 send_ack(cm_node, NULL);
1637                 schedule_nes_timer(cm_node, NULL,  NES_TIMER_TYPE_CLOSE, 1, 0);
1638                 break;
1639         case NES_CM_STATE_TIME_WAIT:
1640                 cm_node->tcp_cntxt.rcv_nxt++;
1641                 cleanup_retrans_entry(cm_node);
1642                 cm_node->state = NES_CM_STATE_CLOSED;
1643                 rem_ref_cm_node(cm_node->cm_core, cm_node);
1644                 break;
1645         case NES_CM_STATE_TSA:
1646         default:
1647                 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1648                         cm_node, cm_node->state);
1649                 break;
1650         }
1651 }
1652
1653
1654 static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1655         struct tcphdr *tcph)
1656 {
1657
1658         int     reset = 0;      /* whether to send reset in case of err.. */
1659         atomic_inc(&cm_resets_recvd);
1660         nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1661                         " refcnt=%d\n", cm_node, cm_node->state,
1662                         atomic_read(&cm_node->ref_count));
1663         cleanup_retrans_entry(cm_node);
1664         switch (cm_node->state) {
1665         case NES_CM_STATE_SYN_SENT:
1666         case NES_CM_STATE_MPAREQ_SENT:
1667                 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1668                         "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1669                         cm_node->listener, cm_node->state);
1670                 switch (cm_node->mpa_frame_rev) {
1671                 case IETF_MPA_V2:
1672                         cm_node->mpa_frame_rev = IETF_MPA_V1;
1673                         /* send a syn and goto syn sent state */
1674                         cm_node->state = NES_CM_STATE_SYN_SENT;
1675                         if (send_syn(cm_node, 0, NULL)) {
1676                                 active_open_err(cm_node, skb, reset);
1677                         }
1678                         break;
1679                 case IETF_MPA_V1:
1680                 default:
1681                         active_open_err(cm_node, skb, reset);
1682                         break;
1683                 }
1684                 break;
1685         case NES_CM_STATE_MPAREQ_RCVD:
1686                 atomic_inc(&cm_node->passive_state);
1687                 dev_kfree_skb_any(skb);
1688                 break;
1689         case NES_CM_STATE_ESTABLISHED:
1690         case NES_CM_STATE_SYN_RCVD:
1691         case NES_CM_STATE_LISTENING:
1692                 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1693                 passive_open_err(cm_node, skb, reset);
1694                 break;
1695         case NES_CM_STATE_TSA:
1696                 active_open_err(cm_node, skb, reset);
1697                 break;
1698         case NES_CM_STATE_CLOSED:
1699                 drop_packet(skb);
1700                 break;
1701         case NES_CM_STATE_FIN_WAIT2:
1702         case NES_CM_STATE_FIN_WAIT1:
1703         case NES_CM_STATE_LAST_ACK:
1704                 cm_node->cm_id->rem_ref(cm_node->cm_id);
1705         case NES_CM_STATE_TIME_WAIT:
1706                 cm_node->state = NES_CM_STATE_CLOSED;
1707                 rem_ref_cm_node(cm_node->cm_core, cm_node);
1708                 drop_packet(skb);
1709                 break;
1710         default:
1711                 drop_packet(skb);
1712                 break;
1713         }
1714 }
1715
1716
1717 static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
1718 {
1719         int ret = 0;
1720         int datasize = skb->len;
1721         u8 *dataloc = skb->data;
1722
1723         enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1724         u32 res_type;
1725
1726         ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1727         if (ret) {
1728                 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
1729                 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
1730                         nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1731                                   "cm_node=%p listener=%p state=%d\n", __func__,
1732                                   __LINE__, cm_node, cm_node->listener,
1733                                   cm_node->state);
1734                         active_open_err(cm_node, skb, 1);
1735                 } else {
1736                         passive_open_err(cm_node, skb, 1);
1737                 }
1738                 return;
1739         }
1740
1741         switch (cm_node->state) {
1742         case NES_CM_STATE_ESTABLISHED:
1743                 if (res_type == NES_MPA_REQUEST_REJECT)
1744                         /*BIG problem as we are receiving the MPA.. So should
1745                          * not be REJECT.. This is Passive Open.. We can
1746                          * only receive it Reject for Active Open...*/
1747                         WARN_ON(1);
1748                 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1749                 type = NES_CM_EVENT_MPA_REQ;
1750                 atomic_set(&cm_node->passive_state,
1751                            NES_PASSIVE_STATE_INDICATED);
1752                 break;
1753         case NES_CM_STATE_MPAREQ_SENT:
1754                 cleanup_retrans_entry(cm_node);
1755                 if (res_type == NES_MPA_REQUEST_REJECT) {
1756                         type = NES_CM_EVENT_MPA_REJECT;
1757                         cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1758                 } else {
1759                         type = NES_CM_EVENT_CONNECTED;
1760                         cm_node->state = NES_CM_STATE_TSA;
1761                 }
1762
1763                 break;
1764         default:
1765                 WARN_ON(1);
1766                 break;
1767         }
1768         dev_kfree_skb_any(skb);
1769         create_event(cm_node, type);
1770 }
1771
1772 static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1773 {
1774         switch (cm_node->state) {
1775         case NES_CM_STATE_SYN_SENT:
1776         case NES_CM_STATE_MPAREQ_SENT:
1777                 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1778                           "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1779                           cm_node->listener, cm_node->state);
1780                 active_open_err(cm_node, skb, 1);
1781                 break;
1782         case NES_CM_STATE_ESTABLISHED:
1783         case NES_CM_STATE_SYN_RCVD:
1784                 passive_open_err(cm_node, skb, 1);
1785                 break;
1786         case NES_CM_STATE_TSA:
1787         default:
1788                 drop_packet(skb);
1789         }
1790 }
1791
1792 static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1793                      struct sk_buff *skb)
1794 {
1795         int err;
1796
1797         err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
1798         if (err)
1799                 active_open_err(cm_node, skb, 1);
1800
1801         return err;
1802 }
1803
1804 static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1805                      struct sk_buff *skb)
1806 {
1807         int err = 0;
1808         u32 seq;
1809         u32 ack_seq;
1810         u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1811         u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1812         u32 rcv_wnd;
1813
1814         seq = ntohl(tcph->seq);
1815         ack_seq = ntohl(tcph->ack_seq);
1816         rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1817         if (ack_seq != loc_seq_num)
1818                 err = 1;
1819         else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
1820                 err = 1;
1821         if (err) {
1822                 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1823                           "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1824                           cm_node->listener, cm_node->state);
1825                 indicate_pkt_err(cm_node, skb);
1826                 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1827                           "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1828                           rcv_wnd);
1829         }
1830         return err;
1831 }
1832
1833 /*
1834  * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1835  * is created with a listener or it may comein as rexmitted packet which in
1836  * that case will be just dropped.
1837  */
1838 static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1839                            struct tcphdr *tcph)
1840 {
1841         int ret;
1842         u32 inc_sequence;
1843         int optionsize;
1844
1845         optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1846         skb_trim(skb, 0);
1847         inc_sequence = ntohl(tcph->seq);
1848
1849         switch (cm_node->state) {
1850         case NES_CM_STATE_SYN_SENT:
1851         case NES_CM_STATE_MPAREQ_SENT:
1852                 /* Rcvd syn on active open connection*/
1853                 active_open_err(cm_node, skb, 1);
1854                 break;
1855         case NES_CM_STATE_LISTENING:
1856                 /* Passive OPEN */
1857                 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1858                     cm_node->listener->backlog) {
1859                         nes_debug(NES_DBG_CM, "drop syn due to backlog "
1860                                   "pressure \n");
1861                         cm_backlog_drops++;
1862                         passive_open_err(cm_node, skb, 0);
1863                         break;
1864                 }
1865                 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1866                                          1);
1867                 if (ret) {
1868                         passive_open_err(cm_node, skb, 0);
1869                         /* drop pkt */
1870                         break;
1871                 }
1872                 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1873                 BUG_ON(cm_node->send_entry);
1874                 cm_node->accept_pend = 1;
1875                 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1876
1877                 cm_node->state = NES_CM_STATE_SYN_RCVD;
1878                 send_syn(cm_node, 1, skb);
1879                 break;
1880         case NES_CM_STATE_CLOSED:
1881                 cleanup_retrans_entry(cm_node);
1882                 add_ref_cm_node(cm_node);
1883                 send_reset(cm_node, skb);
1884                 break;
1885         case NES_CM_STATE_TSA:
1886         case NES_CM_STATE_ESTABLISHED:
1887         case NES_CM_STATE_FIN_WAIT1:
1888         case NES_CM_STATE_FIN_WAIT2:
1889         case NES_CM_STATE_MPAREQ_RCVD:
1890         case NES_CM_STATE_LAST_ACK:
1891         case NES_CM_STATE_CLOSING:
1892         case NES_CM_STATE_UNKNOWN:
1893         default:
1894                 drop_packet(skb);
1895                 break;
1896         }
1897 }
1898
1899 static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1900                               struct tcphdr *tcph)
1901 {
1902         int ret;
1903         u32 inc_sequence;
1904         int optionsize;
1905
1906         optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1907         skb_trim(skb, 0);
1908         inc_sequence = ntohl(tcph->seq);
1909         switch (cm_node->state) {
1910         case NES_CM_STATE_SYN_SENT:
1911                 cleanup_retrans_entry(cm_node);
1912                 /* active open */
1913                 if (check_syn(cm_node, tcph, skb))
1914                         return;
1915                 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1916                 /* setup options */
1917                 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1918                 if (ret) {
1919                         nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1920                                   cm_node);
1921                         break;
1922                 }
1923                 cleanup_retrans_entry(cm_node);
1924                 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1925                 send_mpa_request(cm_node, skb);
1926                 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1927                 break;
1928         case NES_CM_STATE_MPAREQ_RCVD:
1929                 /* passive open, so should not be here */
1930                 passive_open_err(cm_node, skb, 1);
1931                 break;
1932         case NES_CM_STATE_LISTENING:
1933                 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1934                 cleanup_retrans_entry(cm_node);
1935                 cm_node->state = NES_CM_STATE_CLOSED;
1936                 send_reset(cm_node, skb);
1937                 break;
1938         case NES_CM_STATE_CLOSED:
1939                 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1940                 cleanup_retrans_entry(cm_node);
1941                 add_ref_cm_node(cm_node);
1942                 send_reset(cm_node, skb);
1943                 break;
1944         case NES_CM_STATE_ESTABLISHED:
1945         case NES_CM_STATE_FIN_WAIT1:
1946         case NES_CM_STATE_FIN_WAIT2:
1947         case NES_CM_STATE_LAST_ACK:
1948         case NES_CM_STATE_TSA:
1949         case NES_CM_STATE_CLOSING:
1950         case NES_CM_STATE_UNKNOWN:
1951         case NES_CM_STATE_MPAREQ_SENT:
1952         default:
1953                 drop_packet(skb);
1954                 break;
1955         }
1956 }
1957
1958 static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1959                           struct tcphdr *tcph)
1960 {
1961         int datasize = 0;
1962         u32 inc_sequence;
1963         int ret = 0;
1964         int optionsize;
1965
1966         optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1967
1968         if (check_seq(cm_node, tcph, skb))
1969                 return -EINVAL;
1970
1971         skb_pull(skb, tcph->doff << 2);
1972         inc_sequence = ntohl(tcph->seq);
1973         datasize = skb->len;
1974         switch (cm_node->state) {
1975         case NES_CM_STATE_SYN_RCVD:
1976                 /* Passive OPEN */
1977                 cleanup_retrans_entry(cm_node);
1978                 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1979                 if (ret)
1980                         break;
1981                 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1982                 cm_node->state = NES_CM_STATE_ESTABLISHED;
1983                 if (datasize) {
1984                         cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1985                         handle_rcv_mpa(cm_node, skb);
1986                 } else { /* rcvd ACK only */
1987                         dev_kfree_skb_any(skb);
1988                 }
1989                 break;
1990         case NES_CM_STATE_ESTABLISHED:
1991                 /* Passive OPEN */
1992                 cleanup_retrans_entry(cm_node);
1993                 if (datasize) {
1994                         cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1995                         handle_rcv_mpa(cm_node, skb);
1996                 } else {
1997                         drop_packet(skb);
1998                 }
1999                 break;
2000         case NES_CM_STATE_MPAREQ_SENT:
2001                 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2002                 if (datasize) {
2003                         cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
2004                         handle_rcv_mpa(cm_node, skb);
2005                 } else { /* Could be just an ack pkt.. */
2006                         dev_kfree_skb_any(skb);
2007                 }
2008                 break;
2009         case NES_CM_STATE_LISTENING:
2010                 cleanup_retrans_entry(cm_node);
2011                 cm_node->state = NES_CM_STATE_CLOSED;
2012                 send_reset(cm_node, skb);
2013                 break;
2014         case NES_CM_STATE_CLOSED:
2015                 cleanup_retrans_entry(cm_node);
2016                 add_ref_cm_node(cm_node);
2017                 send_reset(cm_node, skb);
2018                 break;
2019         case NES_CM_STATE_LAST_ACK:
2020         case NES_CM_STATE_CLOSING:
2021                 cleanup_retrans_entry(cm_node);
2022                 cm_node->state = NES_CM_STATE_CLOSED;
2023                 cm_node->cm_id->rem_ref(cm_node->cm_id);
2024                 rem_ref_cm_node(cm_node->cm_core, cm_node);
2025                 drop_packet(skb);
2026                 break;
2027         case NES_CM_STATE_FIN_WAIT1:
2028                 cleanup_retrans_entry(cm_node);
2029                 drop_packet(skb);
2030                 cm_node->state = NES_CM_STATE_FIN_WAIT2;
2031                 break;
2032         case NES_CM_STATE_SYN_SENT:
2033         case NES_CM_STATE_FIN_WAIT2:
2034         case NES_CM_STATE_TSA:
2035         case NES_CM_STATE_MPAREQ_RCVD:
2036         case NES_CM_STATE_UNKNOWN:
2037         default:
2038                 cleanup_retrans_entry(cm_node);
2039                 drop_packet(skb);
2040                 break;
2041         }
2042         return ret;
2043 }
2044
2045
2046
2047 static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
2048                               struct sk_buff *skb, int optionsize, int passive)
2049 {
2050         u8 *optionsloc = (u8 *)&tcph[1];
2051
2052         if (optionsize) {
2053                 if (process_options(cm_node, optionsloc, optionsize,
2054                                     (u32)tcph->syn)) {
2055                         nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
2056                                   __func__, cm_node);
2057                         if (passive)
2058                                 passive_open_err(cm_node, skb, 1);
2059                         else
2060                                 active_open_err(cm_node, skb, 1);
2061                         return 1;
2062                 }
2063         }
2064
2065         cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
2066                                      cm_node->tcp_cntxt.snd_wscale;
2067
2068         if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
2069                 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
2070         return 0;
2071 }
2072
2073 /*
2074  * active_open_err() will send reset() if flag set..
2075  * It will also send ABORT event.
2076  */
2077 static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
2078                             int reset)
2079 {
2080         cleanup_retrans_entry(cm_node);
2081         if (reset) {
2082                 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
2083                           "state=%d\n", cm_node, cm_node->state);
2084                 add_ref_cm_node(cm_node);
2085                 send_reset(cm_node, skb);
2086         } else {
2087                 dev_kfree_skb_any(skb);
2088         }
2089
2090         cm_node->state = NES_CM_STATE_CLOSED;
2091         create_event(cm_node, NES_CM_EVENT_ABORTED);
2092 }
2093
2094 /*
2095  * passive_open_err() will either do a reset() or will free up the skb and
2096  * remove the cm_node.
2097  */
2098 static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
2099                              int reset)
2100 {
2101         cleanup_retrans_entry(cm_node);
2102         cm_node->state = NES_CM_STATE_CLOSED;
2103         if (reset) {
2104                 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
2105                           "cm_node=%p state =%d\n", cm_node, cm_node->state);
2106                 send_reset(cm_node, skb);
2107         } else {
2108                 dev_kfree_skb_any(skb);
2109                 rem_ref_cm_node(cm_node->cm_core, cm_node);
2110         }
2111 }
2112
2113 /*
2114  * free_retrans_entry() routines assumes that the retrans_list_lock has
2115  * been acquired before calling.
2116  */
2117 static void free_retrans_entry(struct nes_cm_node *cm_node)
2118 {
2119         struct nes_timer_entry *send_entry;
2120
2121         send_entry = cm_node->send_entry;
2122         if (send_entry) {
2123                 cm_node->send_entry = NULL;
2124                 dev_kfree_skb_any(send_entry->skb);
2125                 kfree(send_entry);
2126                 rem_ref_cm_node(cm_node->cm_core, cm_node);
2127         }
2128 }
2129
2130 static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
2131 {
2132         unsigned long flags;
2133
2134         spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
2135         free_retrans_entry(cm_node);
2136         spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
2137 }
2138
2139 /**
2140  * process_packet
2141  * Returns skb if to be freed, else it will return NULL if already used..
2142  */
2143 static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
2144                            struct nes_cm_core *cm_core)
2145 {
2146         enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
2147         struct tcphdr *tcph = tcp_hdr(skb);
2148         u32 fin_set = 0;
2149         int ret = 0;
2150
2151         skb_pull(skb, ip_hdr(skb)->ihl << 2);
2152
2153         nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
2154                   "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
2155                   tcph->ack, tcph->rst, tcph->fin);
2156
2157         if (tcph->rst) {
2158                 pkt_type = NES_PKT_TYPE_RST;
2159         } else if (tcph->syn) {
2160                 pkt_type = NES_PKT_TYPE_SYN;
2161                 if (tcph->ack)
2162                         pkt_type = NES_PKT_TYPE_SYNACK;
2163         } else if (tcph->ack) {
2164                 pkt_type = NES_PKT_TYPE_ACK;
2165         }
2166         if (tcph->fin)
2167                 fin_set = 1;
2168
2169         switch (pkt_type) {
2170         case NES_PKT_TYPE_SYN:
2171                 handle_syn_pkt(cm_node, skb, tcph);
2172                 break;
2173         case NES_PKT_TYPE_SYNACK:
2174                 handle_synack_pkt(cm_node, skb, tcph);
2175                 break;
2176         case NES_PKT_TYPE_ACK:
2177                 ret = handle_ack_pkt(cm_node, skb, tcph);
2178                 if (fin_set && !ret)
2179                         handle_fin_pkt(cm_node);
2180                 break;
2181         case NES_PKT_TYPE_RST:
2182                 handle_rst_pkt(cm_node, skb, tcph);
2183                 break;
2184         default:
2185                 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
2186                         handle_fin_pkt(cm_node);
2187                 drop_packet(skb);
2188                 break;
2189         }
2190 }
2191
2192 /**
2193  * mini_cm_listen - create a listen node with params
2194  */
2195 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
2196                                               struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
2197 {
2198         struct nes_cm_listener *listener;
2199         unsigned long flags;
2200
2201         nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
2202                   cm_info->loc_addr, cm_info->loc_port);
2203
2204         /* cannot have multiple matching listeners */
2205         listener = find_listener(cm_core, htonl(cm_info->loc_addr),
2206                                  htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
2207         if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
2208                 /* find automatically incs ref count ??? */
2209                 atomic_dec(&listener->ref_count);
2210                 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
2211                 return NULL;
2212         }
2213
2214         if (!listener) {
2215                 /* create a CM listen node (1/2 node to compare incoming traffic to) */
2216                 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
2217                 if (!listener) {
2218                         nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
2219                         return NULL;
2220                 }
2221
2222                 listener->loc_addr = htonl(cm_info->loc_addr);
2223                 listener->loc_port = htons(cm_info->loc_port);
2224                 listener->reused_node = 0;
2225
2226                 atomic_set(&listener->ref_count, 1);
2227         }
2228         /* pasive case */
2229         /* find already inc'ed the ref count */
2230         else {
2231                 listener->reused_node = 1;
2232         }
2233
2234         listener->cm_id = cm_info->cm_id;
2235         atomic_set(&listener->pend_accepts_cnt, 0);
2236         listener->cm_core = cm_core;
2237         listener->nesvnic = nesvnic;
2238         atomic_inc(&cm_core->node_cnt);
2239
2240         listener->conn_type = cm_info->conn_type;
2241         listener->backlog = cm_info->backlog;
2242         listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
2243
2244         if (!listener->reused_node) {
2245                 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2246                 list_add(&listener->list, &cm_core->listen_list.list);
2247                 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2248                 atomic_inc(&cm_core->listen_node_cnt);
2249         }
2250
2251         nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
2252                   " listener = %p, backlog = %d, cm_id = %p.\n",
2253                   cm_info->loc_addr, cm_info->loc_port,
2254                   listener, listener->backlog, listener->cm_id);
2255
2256         return listener;
2257 }
2258
2259
2260 /**
2261  * mini_cm_connect - make a connection node with params
2262  */
2263 static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
2264                                            struct nes_vnic *nesvnic, u16 private_data_len,
2265                                            void *private_data, struct nes_cm_info *cm_info)
2266 {
2267         int ret = 0;
2268         struct nes_cm_node *cm_node;
2269         struct nes_cm_listener *loopbackremotelistener;
2270         struct nes_cm_node *loopbackremotenode;
2271         struct nes_cm_info loopback_cm_info;
2272         u8 *start_buff;
2273
2274         /* create a CM connection node */
2275         cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2276         if (!cm_node)
2277                 return NULL;
2278
2279         /* set our node side to client (active) side */
2280         cm_node->tcp_cntxt.client = 1;
2281         cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2282
2283         if (cm_info->loc_addr == cm_info->rem_addr) {
2284                 loopbackremotelistener = find_listener(cm_core,
2285                                                        ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2286                                                        NES_CM_LISTENER_ACTIVE_STATE);
2287                 if (loopbackremotelistener == NULL) {
2288                         create_event(cm_node, NES_CM_EVENT_ABORTED);
2289                 } else {
2290                         loopback_cm_info = *cm_info;
2291                         loopback_cm_info.loc_port = cm_info->rem_port;
2292                         loopback_cm_info.rem_port = cm_info->loc_port;
2293                         loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
2294                         loopbackremotenode = make_cm_node(cm_core, nesvnic,
2295                                                           &loopback_cm_info, loopbackremotelistener);
2296                         if (!loopbackremotenode) {
2297                                 rem_ref_cm_node(cm_node->cm_core, cm_node);
2298                                 return NULL;
2299                         }
2300                         atomic_inc(&cm_loopbacks);
2301                         loopbackremotenode->loopbackpartner = cm_node;
2302                         loopbackremotenode->tcp_cntxt.rcv_wscale =
2303                                 NES_CM_DEFAULT_RCV_WND_SCALE;
2304                         cm_node->loopbackpartner = loopbackremotenode;
2305                         memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2306                                private_data_len);
2307                         loopbackremotenode->mpa_frame_size = private_data_len;
2308
2309                         /* we are done handling this state. */
2310                         /* set node to a TSA state */
2311                         cm_node->state = NES_CM_STATE_TSA;
2312                         cm_node->tcp_cntxt.rcv_nxt =
2313                                 loopbackremotenode->tcp_cntxt.loc_seq_num;
2314                         loopbackremotenode->tcp_cntxt.rcv_nxt =
2315                                 cm_node->tcp_cntxt.loc_seq_num;
2316                         cm_node->tcp_cntxt.max_snd_wnd =
2317                                 loopbackremotenode->tcp_cntxt.rcv_wnd;
2318                         loopbackremotenode->tcp_cntxt.max_snd_wnd =
2319                                 cm_node->tcp_cntxt.rcv_wnd;
2320                         cm_node->tcp_cntxt.snd_wnd =
2321                                 loopbackremotenode->tcp_cntxt.rcv_wnd;
2322                         loopbackremotenode->tcp_cntxt.snd_wnd =
2323                                 cm_node->tcp_cntxt.rcv_wnd;
2324                         cm_node->tcp_cntxt.snd_wscale =
2325                                 loopbackremotenode->tcp_cntxt.rcv_wscale;
2326                         loopbackremotenode->tcp_cntxt.snd_wscale =
2327                                 cm_node->tcp_cntxt.rcv_wscale;
2328                         loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
2329                         create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2330                 }
2331                 return cm_node;
2332         }
2333
2334         start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
2335         cm_node->mpa_frame_size = private_data_len;
2336
2337         memcpy(start_buff, private_data, private_data_len);
2338
2339         /* send a syn and goto syn sent state */
2340         cm_node->state = NES_CM_STATE_SYN_SENT;
2341         ret = send_syn(cm_node, 0, NULL);
2342
2343         if (ret) {
2344                 /* error in sending the syn free up the cm_node struct */
2345                 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2346                           "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2347                           cm_node->rem_addr, cm_node->rem_port, cm_node,
2348                           cm_node->cm_id);
2349                 rem_ref_cm_node(cm_node->cm_core, cm_node);
2350                 cm_node = NULL;
2351         }
2352
2353         if (cm_node) {
2354                 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2355                           "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2356                           cm_node->rem_addr, cm_node->rem_port, cm_node,
2357                           cm_node->cm_id);
2358         }
2359
2360         return cm_node;
2361 }
2362
2363
2364 /**
2365  * mini_cm_accept - accept a connection
2366  * This function is never called
2367  */
2368 static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
2369 {
2370         return 0;
2371 }
2372
2373
2374 /**
2375  * mini_cm_reject - reject and teardown a connection
2376  */
2377 static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
2378 {
2379         int ret = 0;
2380         int err = 0;
2381         int passive_state;
2382         struct nes_cm_event event;
2383         struct iw_cm_id *cm_id = cm_node->cm_id;
2384         struct nes_cm_node *loopback = cm_node->loopbackpartner;
2385
2386         nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2387                   __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
2388
2389         if (cm_node->tcp_cntxt.client)
2390                 return ret;
2391         cleanup_retrans_entry(cm_node);
2392
2393         if (!loopback) {
2394                 passive_state = atomic_add_return(1, &cm_node->passive_state);
2395                 if (passive_state == NES_SEND_RESET_EVENT) {
2396                         cm_node->state = NES_CM_STATE_CLOSED;
2397                         rem_ref_cm_node(cm_core, cm_node);
2398                 } else {
2399                         if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2400                                 rem_ref_cm_node(cm_core, cm_node);
2401                         } else {
2402                                 ret = send_mpa_reject(cm_node);
2403                                 if (ret) {
2404                                         cm_node->state = NES_CM_STATE_CLOSED;
2405                                         err = send_reset(cm_node, NULL);
2406                                         if (err)
2407                                                 WARN_ON(1);
2408                                 } else {
2409                                         cm_id->add_ref(cm_id);
2410                                 }
2411                         }
2412                 }
2413         } else {
2414                 cm_node->cm_id = NULL;
2415                 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2416                         rem_ref_cm_node(cm_core, cm_node);
2417                         rem_ref_cm_node(cm_core, loopback);
2418                 } else {
2419                         event.cm_node = loopback;
2420                         event.cm_info.rem_addr = loopback->rem_addr;
2421                         event.cm_info.loc_addr = loopback->loc_addr;
2422                         event.cm_info.rem_port = loopback->rem_port;
2423                         event.cm_info.loc_port = loopback->loc_port;
2424                         event.cm_info.cm_id = loopback->cm_id;
2425                         cm_event_mpa_reject(&event);
2426                         rem_ref_cm_node(cm_core, cm_node);
2427                         loopback->state = NES_CM_STATE_CLOSING;
2428
2429                         cm_id = loopback->cm_id;
2430                         rem_ref_cm_node(cm_core, loopback);
2431                         cm_id->rem_ref(cm_id);
2432                 }
2433         }
2434
2435         return ret;
2436 }
2437
2438
2439 /**
2440  * mini_cm_close
2441  */
2442 static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
2443 {
2444         int ret = 0;
2445
2446         if (!cm_core || !cm_node)
2447                 return -EINVAL;
2448
2449         switch (cm_node->state) {
2450         case NES_CM_STATE_SYN_RCVD:
2451         case NES_CM_STATE_SYN_SENT:
2452         case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2453         case NES_CM_STATE_ESTABLISHED:
2454         case NES_CM_STATE_ACCEPTING:
2455         case NES_CM_STATE_MPAREQ_SENT:
2456         case NES_CM_STATE_MPAREQ_RCVD:
2457                 cleanup_retrans_entry(cm_node);
2458                 send_reset(cm_node, NULL);
2459                 break;
2460         case NES_CM_STATE_CLOSE_WAIT:
2461                 cm_node->state = NES_CM_STATE_LAST_ACK;
2462                 send_fin(cm_node, NULL);
2463                 break;
2464         case NES_CM_STATE_FIN_WAIT1:
2465         case NES_CM_STATE_FIN_WAIT2:
2466         case NES_CM_STATE_LAST_ACK:
2467         case NES_CM_STATE_TIME_WAIT:
2468         case NES_CM_STATE_CLOSING:
2469                 ret = -1;
2470                 break;
2471         case NES_CM_STATE_LISTENING:
2472                 cleanup_retrans_entry(cm_node);
2473                 send_reset(cm_node, NULL);
2474                 break;
2475         case NES_CM_STATE_MPAREJ_RCVD:
2476         case NES_CM_STATE_UNKNOWN:
2477         case NES_CM_STATE_INITED:
2478         case NES_CM_STATE_CLOSED:
2479         case NES_CM_STATE_LISTENER_DESTROYED:
2480                 ret = rem_ref_cm_node(cm_core, cm_node);
2481                 break;
2482         case NES_CM_STATE_TSA:
2483                 if (cm_node->send_entry)
2484                         printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2485                                "send_entry=%p\n", cm_node->send_entry);
2486                 ret = rem_ref_cm_node(cm_core, cm_node);
2487                 break;
2488         }
2489         return ret;
2490 }
2491
2492
2493 /**
2494  * recv_pkt - recv an ETHERNET packet, and process it through CM
2495  * node state machine
2496  */
2497 static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
2498                             struct nes_vnic *nesvnic, struct sk_buff *skb)
2499 {
2500         struct nes_cm_node *cm_node = NULL;
2501         struct nes_cm_listener *listener = NULL;
2502         struct iphdr *iph;
2503         struct tcphdr *tcph;
2504         struct nes_cm_info nfo;
2505         int skb_handled = 1;
2506         __be32 tmp_daddr, tmp_saddr;
2507
2508         if (!skb)
2509                 return 0;
2510         if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
2511                 return 0;
2512
2513         iph = (struct iphdr *)skb->data;
2514         tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
2515
2516         nfo.loc_addr = ntohl(iph->daddr);
2517         nfo.loc_port = ntohs(tcph->dest);
2518         nfo.rem_addr = ntohl(iph->saddr);
2519         nfo.rem_port = ntohs(tcph->source);
2520
2521         tmp_daddr = cpu_to_be32(iph->daddr);
2522         tmp_saddr = cpu_to_be32(iph->saddr);
2523
2524         nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
2525                   &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
2526
2527         do {
2528                 cm_node = find_node(cm_core,
2529                                     nfo.rem_port, nfo.rem_addr,
2530                                     nfo.loc_port, nfo.loc_addr);
2531
2532                 if (!cm_node) {
2533                         /* Only type of packet accepted are for */
2534                         /* the PASSIVE open (syn only) */
2535                         if ((!tcph->syn) || (tcph->ack)) {
2536                                 skb_handled = 0;
2537                                 break;
2538                         }
2539                         listener = find_listener(cm_core, nfo.loc_addr,
2540                                                  nfo.loc_port,
2541                                                  NES_CM_LISTENER_ACTIVE_STATE);
2542                         if (!listener) {
2543                                 nfo.cm_id = NULL;
2544                                 nfo.conn_type = 0;
2545                                 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2546                                 skb_handled = 0;
2547                                 break;
2548                         }
2549                         nfo.cm_id = listener->cm_id;
2550                         nfo.conn_type = listener->conn_type;
2551                         cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2552                                                listener);
2553                         if (!cm_node) {
2554                                 nes_debug(NES_DBG_CM, "Unable to allocate "
2555                                           "node\n");
2556                                 cm_packets_dropped++;
2557                                 atomic_dec(&listener->ref_count);
2558                                 dev_kfree_skb_any(skb);
2559                                 break;
2560                         }
2561                         if (!tcph->rst && !tcph->fin) {
2562                                 cm_node->state = NES_CM_STATE_LISTENING;
2563                         } else {
2564                                 cm_packets_dropped++;
2565                                 rem_ref_cm_node(cm_core, cm_node);
2566                                 dev_kfree_skb_any(skb);
2567                                 break;
2568                         }
2569                         add_ref_cm_node(cm_node);
2570                 } else if (cm_node->state == NES_CM_STATE_TSA) {
2571                         if (cm_node->nesqp->pau_mode)
2572                                 nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
2573                         else {
2574                                 rem_ref_cm_node(cm_core, cm_node);
2575                                 atomic_inc(&cm_accel_dropped_pkts);
2576                                 dev_kfree_skb_any(skb);
2577                         }
2578                         break;
2579                 }
2580                 skb_reset_network_header(skb);
2581                 skb_set_transport_header(skb, sizeof(*tcph));
2582                 skb->len = ntohs(iph->tot_len);
2583                 process_packet(cm_node, skb, cm_core);
2584                 rem_ref_cm_node(cm_core, cm_node);
2585         } while (0);
2586         return skb_handled;
2587 }
2588
2589
2590 /**
2591  * nes_cm_alloc_core - allocate a top level instance of a cm core
2592  */
2593 static struct nes_cm_core *nes_cm_alloc_core(void)
2594 {
2595         struct nes_cm_core *cm_core;
2596
2597         /* setup the CM core */
2598         /* alloc top level core control structure */
2599         cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2600         if (!cm_core)
2601                 return NULL;
2602
2603         INIT_LIST_HEAD(&cm_core->connected_nodes);
2604         init_timer(&cm_core->tcp_timer);
2605         cm_core->tcp_timer.function = nes_cm_timer_tick;
2606
2607         cm_core->mtu = NES_CM_DEFAULT_MTU;
2608         cm_core->state = NES_CM_STATE_INITED;
2609         cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2610
2611         atomic_set(&cm_core->events_posted, 0);
2612
2613         cm_core->api = &nes_cm_api;
2614
2615         spin_lock_init(&cm_core->ht_lock);
2616         spin_lock_init(&cm_core->listen_list_lock);
2617
2618         INIT_LIST_HEAD(&cm_core->listen_list.list);
2619
2620         nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2621
2622         nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2623         cm_core->event_wq = create_singlethread_workqueue("nesewq");
2624         cm_core->post_event = nes_cm_post_event;
2625         nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2626         cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2627
2628         print_core(cm_core);
2629         return cm_core;
2630 }
2631
2632
2633 /**
2634  * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2635  */
2636 static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
2637 {
2638         nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2639
2640         if (!cm_core)
2641                 return -EINVAL;
2642
2643         barrier();
2644
2645         if (timer_pending(&cm_core->tcp_timer))
2646                 del_timer(&cm_core->tcp_timer);
2647
2648         destroy_workqueue(cm_core->event_wq);
2649         destroy_workqueue(cm_core->disconn_wq);
2650         nes_debug(NES_DBG_CM, "\n");
2651         kfree(cm_core);
2652
2653         return 0;
2654 }
2655
2656
2657 /**
2658  * mini_cm_get
2659  */
2660 static int mini_cm_get(struct nes_cm_core *cm_core)
2661 {
2662         return cm_core->state;
2663 }
2664
2665
2666 /**
2667  * mini_cm_set
2668  */
2669 static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
2670 {
2671         int ret = 0;
2672
2673         switch (type) {
2674         case NES_CM_SET_PKT_SIZE:
2675                 cm_core->mtu = value;
2676                 break;
2677         case NES_CM_SET_FREE_PKT_Q_SIZE:
2678                 cm_core->free_tx_pkt_max = value;
2679                 break;
2680         default:
2681                 /* unknown set option */
2682                 ret = -EINVAL;
2683         }
2684
2685         return ret;
2686 }
2687
2688
2689 /**
2690  * nes_cm_init_tsa_conn setup HW; MPA frames must be
2691  * successfully exchanged when this is called
2692  */
2693 static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2694 {
2695         int ret = 0;
2696
2697         if (!nesqp)
2698                 return -EINVAL;
2699
2700         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2701                                                   NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2702                                                   NES_QPCONTEXT_MISC_DROS);
2703
2704         if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2705                 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2706
2707         nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2708
2709         nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2710
2711         nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2712                 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2713
2714         nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2715                 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2716                 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2717
2718         nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2719                 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2720                 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2721
2722         nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2723         nesqp->nesqp_context->ts_recent = 0;
2724         nesqp->nesqp_context->ts_age = 0;
2725         nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2726         nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2727         nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2728         nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2729                                                     cm_node->tcp_cntxt.rcv_wscale);
2730         nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2731         nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2732         nesqp->nesqp_context->srtt = 0;
2733         nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2734         nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2735         nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
2736         nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2737         nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2738         nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2739
2740         nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2741                   " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2742                   nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2743                   le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2744                   cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2745                   le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2746                   le32_to_cpu(nesqp->nesqp_context->misc));
2747         nes_debug(NES_DBG_CM, "  snd_wnd  = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2748         nes_debug(NES_DBG_CM, "  snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2749         nes_debug(NES_DBG_CM, "  max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2750
2751         nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2752         cm_node->state = NES_CM_STATE_TSA;
2753
2754         return ret;
2755 }
2756
2757
2758 /**
2759  * nes_cm_disconn
2760  */
2761 int nes_cm_disconn(struct nes_qp *nesqp)
2762 {
2763         struct disconn_work *work;
2764
2765         work = kzalloc(sizeof *work, GFP_ATOMIC);
2766         if (!work)
2767                 return -ENOMEM;  /* Timer will clean up */
2768
2769         nes_add_ref(&nesqp->ibqp);
2770         work->nesqp = nesqp;
2771         INIT_WORK(&work->work, nes_disconnect_worker);
2772         queue_work(g_cm_core->disconn_wq, &work->work);
2773         return 0;
2774 }
2775
2776
2777 /**
2778  * nes_disconnect_worker
2779  */
2780 static void nes_disconnect_worker(struct work_struct *work)
2781 {
2782         struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2783         struct nes_qp *nesqp = dwork->nesqp;
2784
2785         kfree(dwork);
2786         nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2787                   nesqp->last_aeq, nesqp->hwqp.qp_id);
2788         nes_cm_disconn_true(nesqp);
2789         nes_rem_ref(&nesqp->ibqp);
2790 }
2791
2792
2793 /**
2794  * nes_cm_disconn_true
2795  */
2796 static int nes_cm_disconn_true(struct nes_qp *nesqp)
2797 {
2798         unsigned long flags;
2799         int ret = 0;
2800         struct iw_cm_id *cm_id;
2801         struct iw_cm_event cm_event;
2802         struct nes_vnic *nesvnic;
2803         u16 last_ae;
2804         u8 original_hw_tcp_state;
2805         u8 original_ibqp_state;
2806         int disconn_status = 0;
2807         int issue_disconn = 0;
2808         int issue_close = 0;
2809         int issue_flush = 0;
2810         u32 flush_q = NES_CQP_FLUSH_RQ;
2811         struct ib_event ibevent;
2812
2813         if (!nesqp) {
2814                 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2815                 return -1;
2816         }
2817
2818         spin_lock_irqsave(&nesqp->lock, flags);
2819         cm_id = nesqp->cm_id;
2820         /* make sure we havent already closed this connection */
2821         if (!cm_id) {
2822                 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2823                           nesqp->hwqp.qp_id);
2824                 spin_unlock_irqrestore(&nesqp->lock, flags);
2825                 return -1;
2826         }
2827
2828         nesvnic = to_nesvnic(nesqp->ibqp.device);
2829         nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2830
2831         original_hw_tcp_state = nesqp->hw_tcp_state;
2832         original_ibqp_state = nesqp->ibqp_state;
2833         last_ae = nesqp->last_aeq;
2834
2835         if (nesqp->term_flags) {
2836                 issue_disconn = 1;
2837                 issue_close = 1;
2838                 nesqp->cm_id = NULL;
2839                 if (nesqp->flush_issued == 0) {
2840                         nesqp->flush_issued = 1;
2841                         issue_flush = 1;
2842                 }
2843         } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2844                         ((original_ibqp_state == IB_QPS_RTS) &&
2845                         (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2846                 issue_disconn = 1;
2847                 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
2848                         disconn_status = -ECONNRESET;
2849         }
2850
2851         if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2852                  (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2853                  (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2854                  (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2855                 issue_close = 1;
2856                 nesqp->cm_id = NULL;
2857                 if (nesqp->flush_issued == 0) {
2858                         nesqp->flush_issued = 1;
2859                         issue_flush = 1;
2860                 }
2861         }
2862
2863         spin_unlock_irqrestore(&nesqp->lock, flags);
2864
2865         if ((issue_flush) && (nesqp->destroyed == 0)) {
2866                 /* Flush the queue(s) */
2867                 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2868                         flush_q |= NES_CQP_FLUSH_SQ;
2869                 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2870
2871                 if (nesqp->term_flags) {
2872                         ibevent.device = nesqp->ibqp.device;
2873                         ibevent.event = nesqp->terminate_eventtype;
2874                         ibevent.element.qp = &nesqp->ibqp;
2875                         nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
2876                 }
2877         }
2878
2879         if ((cm_id) && (cm_id->event_handler)) {
2880                 if (issue_disconn) {
2881                         atomic_inc(&cm_disconnects);
2882                         cm_event.event = IW_CM_EVENT_DISCONNECT;
2883                         cm_event.status = disconn_status;
2884                         cm_event.local_addr = cm_id->local_addr;
2885                         cm_event.remote_addr = cm_id->remote_addr;
2886                         cm_event.private_data = NULL;
2887                         cm_event.private_data_len = 0;
2888
2889                         nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2890                                   " for  QP%u, SQ Head = %u, SQ Tail = %u. "
2891                                   "cm_id = %p, refcount = %u.\n",
2892                                   nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2893                                   nesqp->hwqp.sq_tail, cm_id,
2894                                   atomic_read(&nesqp->refcount));
2895
2896                         ret = cm_id->event_handler(cm_id, &cm_event);
2897                         if (ret)
2898                                 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2899                                           "returned, ret=%d\n", ret);
2900                 }
2901
2902                 if (issue_close) {
2903                         atomic_inc(&cm_closes);
2904                         nes_disconnect(nesqp, 1);
2905
2906                         cm_id->provider_data = nesqp;
2907                         /* Send up the close complete event */
2908                         cm_event.event = IW_CM_EVENT_CLOSE;
2909                         cm_event.status = 0;
2910                         cm_event.provider_data = cm_id->provider_data;
2911                         cm_event.local_addr = cm_id->local_addr;
2912                         cm_event.remote_addr = cm_id->remote_addr;
2913                         cm_event.private_data = NULL;
2914                         cm_event.private_data_len = 0;
2915
2916                         ret = cm_id->event_handler(cm_id, &cm_event);
2917                         if (ret)
2918                                 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2919
2920                         cm_id->rem_ref(cm_id);
2921                 }
2922         }
2923
2924         return 0;
2925 }
2926
2927
2928 /**
2929  * nes_disconnect
2930  */
2931 static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
2932 {
2933         int ret = 0;
2934         struct nes_vnic *nesvnic;
2935         struct nes_device *nesdev;
2936         struct nes_ib_device *nesibdev;
2937
2938         nesvnic = to_nesvnic(nesqp->ibqp.device);
2939         if (!nesvnic)
2940                 return -EINVAL;
2941
2942         nesdev = nesvnic->nesdev;
2943         nesibdev = nesvnic->nesibdev;
2944
2945         nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2946                         netdev_refcnt_read(nesvnic->netdev));
2947
2948         if (nesqp->active_conn) {
2949
2950                 /* indicate this connection is NOT active */
2951                 nesqp->active_conn = 0;
2952         } else {
2953                 /* Need to free the Last Streaming Mode Message */
2954                 if (nesqp->ietf_frame) {
2955                         if (nesqp->lsmm_mr)