986d6f32ddedf8ac0d225aca4a61321469f1d3a1
[linux-2.6.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 <asm/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 *,
81         struct nes_cm_node *);
82 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
83         struct nes_vnic *, struct nes_cm_info *);
84 static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
85 static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
86         struct nes_vnic *, u16, void *, struct nes_cm_info *);
87 static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
88 static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
89         struct nes_cm_node *);
90 static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
91         struct nes_cm_node *);
92 static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
93         struct sk_buff *);
94 static int mini_cm_dealloc_core(struct nes_cm_core *);
95 static int mini_cm_get(struct nes_cm_core *);
96 static int mini_cm_set(struct nes_cm_core *, u32, u32);
97
98 static void form_cm_frame(struct sk_buff *, struct nes_cm_node *,
99         void *, u32, void *, u32, u8);
100 static int add_ref_cm_node(struct nes_cm_node *);
101 static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
102
103 static int nes_cm_disconn_true(struct nes_qp *);
104 static int nes_cm_post_event(struct nes_cm_event *event);
105 static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
106 static void nes_disconnect_worker(struct work_struct *work);
107
108 static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
109 static int send_mpa_reject(struct nes_cm_node *);
110 static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
111 static int send_reset(struct nes_cm_node *, struct sk_buff *);
112 static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
113 static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
114 static void process_packet(struct nes_cm_node *, struct sk_buff *,
115         struct nes_cm_core *);
116
117 static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
118 static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
119 static void cleanup_retrans_entry(struct nes_cm_node *);
120 static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
121 static void free_retrans_entry(struct nes_cm_node *cm_node);
122 static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
123         struct sk_buff *skb, int optionsize, int passive);
124
125 /* CM event handler functions */
126 static void cm_event_connected(struct nes_cm_event *);
127 static void cm_event_connect_error(struct nes_cm_event *);
128 static void cm_event_reset(struct nes_cm_event *);
129 static void cm_event_mpa_req(struct nes_cm_event *);
130 static void cm_event_mpa_reject(struct nes_cm_event *);
131 static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
132
133 static void print_core(struct nes_cm_core *core);
134
135 /* External CM API Interface */
136 /* instance of function pointers for client API */
137 /* set address of this instance to cm_core->cm_ops at cm_core alloc */
138 static struct nes_cm_ops nes_cm_api = {
139         mini_cm_accelerated,
140         mini_cm_listen,
141         mini_cm_del_listen,
142         mini_cm_connect,
143         mini_cm_close,
144         mini_cm_accept,
145         mini_cm_reject,
146         mini_cm_recv_pkt,
147         mini_cm_dealloc_core,
148         mini_cm_get,
149         mini_cm_set
150 };
151
152 static struct nes_cm_core *g_cm_core;
153
154 atomic_t cm_connects;
155 atomic_t cm_accepts;
156 atomic_t cm_disconnects;
157 atomic_t cm_closes;
158 atomic_t cm_connecteds;
159 atomic_t cm_connect_reqs;
160 atomic_t cm_rejects;
161
162
163 /**
164  * create_event
165  */
166 static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
167                 enum nes_cm_event_type type)
168 {
169         struct nes_cm_event *event;
170
171         if (!cm_node->cm_id)
172                 return NULL;
173
174         /* allocate an empty event */
175         event = kzalloc(sizeof(*event), GFP_ATOMIC);
176
177         if (!event)
178                 return NULL;
179
180         event->type = type;
181         event->cm_node = cm_node;
182         event->cm_info.rem_addr = cm_node->rem_addr;
183         event->cm_info.loc_addr = cm_node->loc_addr;
184         event->cm_info.rem_port = cm_node->rem_port;
185         event->cm_info.loc_port = cm_node->loc_port;
186         event->cm_info.cm_id = cm_node->cm_id;
187
188         nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
189                 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
190                 cm_node, event, type, event->cm_info.loc_addr,
191                 event->cm_info.loc_port, event->cm_info.rem_addr,
192                 event->cm_info.rem_port);
193
194         nes_cm_post_event(event);
195         return event;
196 }
197
198
199 /**
200  * send_mpa_request
201  */
202 static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
203 {
204         if (!skb) {
205                 nes_debug(NES_DBG_CM, "skb set to NULL\n");
206                 return -1;
207         }
208
209         /* send an MPA Request frame */
210         form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
211                         cm_node->mpa_frame_size, SET_ACK);
212
213         return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
214 }
215
216
217
218 static int send_mpa_reject(struct nes_cm_node *cm_node)
219 {
220         struct sk_buff  *skb = NULL;
221
222         skb = dev_alloc_skb(MAX_CM_BUFFER);
223         if (!skb) {
224                 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
225                 return -ENOMEM;
226         }
227
228         /* send an MPA reject frame */
229         form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
230                         cm_node->mpa_frame_size, SET_ACK | SET_FIN);
231
232         cm_node->state = NES_CM_STATE_FIN_WAIT1;
233         return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
234 }
235
236
237 /**
238  * recv_mpa - process a received TCP pkt, we are expecting an
239  * IETF MPA frame
240  */
241 static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
242                 u32 len)
243 {
244         struct ietf_mpa_frame *mpa_frame;
245
246         *type = NES_MPA_REQUEST_ACCEPT;
247
248         /* assume req frame is in tcp data payload */
249         if (len < sizeof(struct ietf_mpa_frame)) {
250                 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
251                 return -EINVAL;
252         }
253
254         mpa_frame = (struct ietf_mpa_frame *)buffer;
255         cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
256         /* make sure mpa private data len is less than 512 bytes */
257         if (cm_node->mpa_frame_size > IETF_MAX_PRIV_DATA_LEN) {
258                 nes_debug(NES_DBG_CM, "The received Length of Private"
259                         " Data field exceeds 512 octets\n");
260                 return -EINVAL;
261         }
262         /*
263          * make sure MPA receiver interoperate with the
264          * received MPA version and MPA key information
265          *
266          */
267         if (mpa_frame->rev != mpa_version) {
268                 nes_debug(NES_DBG_CM, "The received mpa version"
269                                 " can not be interoperated\n");
270                 return -EINVAL;
271         }
272         if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
273                 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
274                         nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
275                         return -EINVAL;
276                 }
277         } else {
278                 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
279                         nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
280                         return -EINVAL;
281                 }
282         }
283
284         if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
285                 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
286                                 " complete (%x + %x != %x)\n",
287                                 cm_node->mpa_frame_size,
288                                 (u32)sizeof(struct ietf_mpa_frame), len);
289                 return -EINVAL;
290         }
291         /* make sure it does not exceed the max size */
292         if (len > MAX_CM_BUFFER) {
293                 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
294                                 " (%x + %x != %x)\n",
295                                 cm_node->mpa_frame_size,
296                                 (u32)sizeof(struct ietf_mpa_frame), len);
297                 return -EINVAL;
298         }
299
300         /* copy entire MPA frame to our cm_node's frame */
301         memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
302                         cm_node->mpa_frame_size);
303
304         if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
305                 *type = NES_MPA_REQUEST_REJECT;
306         return 0;
307 }
308
309
310 /**
311  * form_cm_frame - get a free packet and build empty frame Use
312  * node info to build.
313  */
314 static void form_cm_frame(struct sk_buff *skb,
315         struct nes_cm_node *cm_node, void *options, u32 optionsize,
316         void *data, u32 datasize, u8 flags)
317 {
318         struct tcphdr *tcph;
319         struct iphdr *iph;
320         struct ethhdr *ethh;
321         u8 *buf;
322         u16 packetsize = sizeof(*iph);
323
324         packetsize += sizeof(*tcph);
325         packetsize +=  optionsize + datasize;
326
327         memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
328
329         skb->len = 0;
330         buf = skb_put(skb, packetsize + ETH_HLEN);
331
332         ethh = (struct ethhdr *) buf;
333         buf += ETH_HLEN;
334
335         iph = (struct iphdr *)buf;
336         buf += sizeof(*iph);
337         tcph = (struct tcphdr *)buf;
338         skb_reset_mac_header(skb);
339         skb_set_network_header(skb, ETH_HLEN);
340         skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
341         buf += sizeof(*tcph);
342
343         skb->ip_summed = CHECKSUM_PARTIAL;
344         skb->protocol = htons(0x800);
345         skb->data_len = 0;
346         skb->mac_len = ETH_HLEN;
347
348         memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
349         memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
350         ethh->h_proto = htons(0x0800);
351
352         iph->version = IPVERSION;
353         iph->ihl = 5;           /* 5 * 4Byte words, IP headr len */
354         iph->tos = 0;
355         iph->tot_len = htons(packetsize);
356         iph->id = htons(++cm_node->tcp_cntxt.loc_id);
357
358         iph->frag_off = htons(0x4000);
359         iph->ttl = 0x40;
360         iph->protocol = 0x06;   /* IPPROTO_TCP */
361
362         iph->saddr = htonl(cm_node->loc_addr);
363         iph->daddr = htonl(cm_node->rem_addr);
364
365         tcph->source = htons(cm_node->loc_port);
366         tcph->dest = htons(cm_node->rem_port);
367         tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
368
369         if (flags & SET_ACK) {
370                 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
371                 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
372                 tcph->ack = 1;
373         } else
374                 tcph->ack_seq = 0;
375
376         if (flags & SET_SYN) {
377                 cm_node->tcp_cntxt.loc_seq_num++;
378                 tcph->syn = 1;
379         } else
380                 cm_node->tcp_cntxt.loc_seq_num += datasize;
381
382         if (flags & SET_FIN) {
383                 cm_node->tcp_cntxt.loc_seq_num++;
384                 tcph->fin = 1;
385         }
386
387         if (flags & SET_RST)
388                 tcph->rst = 1;
389
390         tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
391         tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
392         tcph->urg_ptr = 0;
393         if (optionsize)
394                 memcpy(buf, options, optionsize);
395         buf += optionsize;
396         if (datasize)
397                 memcpy(buf, data, datasize);
398
399         skb_shinfo(skb)->nr_frags = 0;
400         cm_packets_created++;
401
402 }
403
404
405 /**
406  * print_core - dump a cm core
407  */
408 static void print_core(struct nes_cm_core *core)
409 {
410         nes_debug(NES_DBG_CM, "---------------------------------------------\n");
411         nes_debug(NES_DBG_CM, "CM Core  -- (core = %p )\n", core);
412         if (!core)
413                 return;
414         nes_debug(NES_DBG_CM, "---------------------------------------------\n");
415
416         nes_debug(NES_DBG_CM, "State         : %u \n",  core->state);
417
418         nes_debug(NES_DBG_CM, "Listen Nodes  : %u \n", atomic_read(&core->listen_node_cnt));
419         nes_debug(NES_DBG_CM, "Active Nodes  : %u \n", atomic_read(&core->node_cnt));
420
421         nes_debug(NES_DBG_CM, "core          : %p \n", core);
422
423         nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
424 }
425
426
427 /**
428  * schedule_nes_timer
429  * note - cm_node needs to be protected before calling this. Encase in:
430  *                      rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
431  */
432 int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
433                 enum nes_timer_type type, int send_retrans,
434                 int close_when_complete)
435 {
436         unsigned long  flags;
437         struct nes_cm_core *cm_core = cm_node->cm_core;
438         struct nes_timer_entry *new_send;
439         int ret = 0;
440         u32 was_timer_set;
441
442         new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
443         if (!new_send)
444                 return -ENOMEM;
445
446         /* new_send->timetosend = currenttime */
447         new_send->retrycount = NES_DEFAULT_RETRYS;
448         new_send->retranscount = NES_DEFAULT_RETRANS;
449         new_send->skb = skb;
450         new_send->timetosend = jiffies;
451         new_send->type = type;
452         new_send->netdev = cm_node->netdev;
453         new_send->send_retrans = send_retrans;
454         new_send->close_when_complete = close_when_complete;
455
456         if (type == NES_TIMER_TYPE_CLOSE) {
457                 new_send->timetosend += (HZ/10);
458                 if (cm_node->recv_entry) {
459                         kfree(new_send);
460                         WARN_ON(1);
461                         return -EINVAL;
462                 }
463                 cm_node->recv_entry = new_send;
464         }
465
466         if (type == NES_TIMER_TYPE_SEND) {
467                 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
468                 atomic_inc(&new_send->skb->users);
469                 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
470                 cm_node->send_entry = new_send;
471                 add_ref_cm_node(cm_node);
472                 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
473                 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
474
475                 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
476                 if (ret != NETDEV_TX_OK) {
477                         nes_debug(NES_DBG_CM, "Error sending packet %p "
478                                 "(jiffies = %lu)\n", new_send, jiffies);
479                         new_send->timetosend = jiffies;
480                         ret = NETDEV_TX_OK;
481                 } else {
482                         cm_packets_sent++;
483                         if (!send_retrans) {
484                                 cleanup_retrans_entry(cm_node);
485                                 if (close_when_complete)
486                                         rem_ref_cm_node(cm_core, cm_node);
487                                 return ret;
488                         }
489                 }
490         }
491
492         was_timer_set = timer_pending(&cm_core->tcp_timer);
493
494         if (!was_timer_set) {
495                 cm_core->tcp_timer.expires = new_send->timetosend;
496                 add_timer(&cm_core->tcp_timer);
497         }
498
499         return ret;
500 }
501
502 static void nes_retrans_expired(struct nes_cm_node *cm_node)
503 {
504         struct iw_cm_id *cm_id = cm_node->cm_id;
505         switch (cm_node->state) {
506         case NES_CM_STATE_SYN_RCVD:
507         case NES_CM_STATE_CLOSING:
508                 rem_ref_cm_node(cm_node->cm_core, cm_node);
509                 break;
510         case NES_CM_STATE_LAST_ACK:
511         case NES_CM_STATE_FIN_WAIT1:
512                 if (cm_node->cm_id)
513                         cm_id->rem_ref(cm_id);
514                 cm_node->state = NES_CM_STATE_CLOSED;
515                 send_reset(cm_node, NULL);
516                 break;
517         default:
518                 add_ref_cm_node(cm_node);
519                 send_reset(cm_node, NULL);
520                 create_event(cm_node, NES_CM_EVENT_ABORTED);
521         }
522 }
523
524 static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
525 {
526         struct nes_timer_entry *recv_entry = cm_node->recv_entry;
527         struct iw_cm_id *cm_id = cm_node->cm_id;
528         struct nes_qp *nesqp;
529         unsigned long qplockflags;
530
531         if (!recv_entry)
532                 return;
533         nesqp = (struct nes_qp *)recv_entry->skb;
534         if (nesqp) {
535                 spin_lock_irqsave(&nesqp->lock, qplockflags);
536                 if (nesqp->cm_id) {
537                         nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
538                                 "refcount = %d: HIT A "
539                                 "NES_TIMER_TYPE_CLOSE with something "
540                                 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
541                                 atomic_read(&nesqp->refcount));
542                         nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
543                         nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
544                         nesqp->ibqp_state = IB_QPS_ERR;
545                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
546                         nes_cm_disconn(nesqp);
547                 } else {
548                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
549                         nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
550                                 "refcount = %d: HIT A "
551                                 "NES_TIMER_TYPE_CLOSE with nothing "
552                                 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
553                                 atomic_read(&nesqp->refcount));
554                 }
555         } else if (rem_node) {
556                 /* TIME_WAIT state */
557                 rem_ref_cm_node(cm_node->cm_core, cm_node);
558         }
559         if (cm_node->cm_id)
560                 cm_id->rem_ref(cm_id);
561         kfree(recv_entry);
562         cm_node->recv_entry = NULL;
563 }
564
565 /**
566  * nes_cm_timer_tick
567  */
568 static void nes_cm_timer_tick(unsigned long pass)
569 {
570         unsigned long flags;
571         unsigned long nexttimeout = jiffies + NES_LONG_TIME;
572         struct nes_cm_node *cm_node;
573         struct nes_timer_entry *send_entry, *recv_entry;
574         struct list_head *list_core_temp;
575         struct list_head *list_node;
576         struct nes_cm_core *cm_core = g_cm_core;
577         u32 settimer = 0;
578         unsigned long timetosend;
579         int ret = NETDEV_TX_OK;
580
581         struct list_head timer_list;
582         INIT_LIST_HEAD(&timer_list);
583         spin_lock_irqsave(&cm_core->ht_lock, flags);
584
585         list_for_each_safe(list_node, list_core_temp,
586                                 &cm_core->connected_nodes) {
587                 cm_node = container_of(list_node, struct nes_cm_node, list);
588                 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
589                         add_ref_cm_node(cm_node);
590                         list_add(&cm_node->timer_entry, &timer_list);
591                 }
592         }
593         spin_unlock_irqrestore(&cm_core->ht_lock, flags);
594
595         list_for_each_safe(list_node, list_core_temp, &timer_list) {
596                 cm_node = container_of(list_node, struct nes_cm_node,
597                                         timer_entry);
598                 recv_entry = cm_node->recv_entry;
599
600                 if (recv_entry) {
601                         if (time_after(recv_entry->timetosend, jiffies)) {
602                                 if (nexttimeout > recv_entry->timetosend ||
603                                                 !settimer) {
604                                         nexttimeout = recv_entry->timetosend;
605                                         settimer = 1;
606                                 }
607                         } else
608                                 handle_recv_entry(cm_node, 1);
609                 }
610
611                 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
612                 do {
613                         send_entry = cm_node->send_entry;
614                         if (!send_entry)
615                                 break;
616                         if (time_after(send_entry->timetosend, jiffies)) {
617                                 if (cm_node->state != NES_CM_STATE_TSA) {
618                                         if ((nexttimeout >
619                                                 send_entry->timetosend) ||
620                                                 !settimer) {
621                                                 nexttimeout =
622                                                         send_entry->timetosend;
623                                                 settimer = 1;
624                                         }
625                                 } else {
626                                         free_retrans_entry(cm_node);
627                                 }
628                                 break;
629                         }
630
631                         if ((cm_node->state == NES_CM_STATE_TSA) ||
632                                 (cm_node->state == NES_CM_STATE_CLOSED)) {
633                                 free_retrans_entry(cm_node);
634                                 break;
635                         }
636
637                         if (!send_entry->retranscount ||
638                                 !send_entry->retrycount) {
639                                 cm_packets_dropped++;
640                                 free_retrans_entry(cm_node);
641
642                                 spin_unlock_irqrestore(
643                                         &cm_node->retrans_list_lock, flags);
644                                 nes_retrans_expired(cm_node);
645                                 cm_node->state = NES_CM_STATE_CLOSED;
646                                 spin_lock_irqsave(&cm_node->retrans_list_lock,
647                                         flags);
648                                 break;
649                         }
650                         atomic_inc(&send_entry->skb->users);
651                         cm_packets_retrans++;
652                         nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
653                                 "for node %p, jiffies = %lu, time to send = "
654                                 "%lu, retranscount = %u, send_entry->seq_num = "
655                                 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
656                                 "0x%08X\n", send_entry, cm_node, jiffies,
657                                 send_entry->timetosend,
658                                 send_entry->retranscount,
659                                 send_entry->seq_num,
660                                 cm_node->tcp_cntxt.rem_ack_num);
661
662                         spin_unlock_irqrestore(&cm_node->retrans_list_lock,
663                                 flags);
664                         ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
665                         spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
666                         if (ret != NETDEV_TX_OK) {
667                                 nes_debug(NES_DBG_CM, "rexmit failed for "
668                                         "node=%p\n", cm_node);
669                                 cm_packets_bounced++;
670                                 send_entry->retrycount--;
671                                 nexttimeout = jiffies + NES_SHORT_TIME;
672                                 settimer = 1;
673                                 break;
674                         } else {
675                                 cm_packets_sent++;
676                         }
677                         nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
678                                 "%u, retry count = %u.\n",
679                                 send_entry->retranscount,
680                                 send_entry->retrycount);
681                         if (send_entry->send_retrans) {
682                                 send_entry->retranscount--;
683                                 timetosend = (NES_RETRY_TIMEOUT <<
684                                         (NES_DEFAULT_RETRANS - send_entry->retranscount));
685
686                                 send_entry->timetosend = jiffies +
687                                         min(timetosend, NES_MAX_TIMEOUT);
688                                 if (nexttimeout > send_entry->timetosend ||
689                                         !settimer) {
690                                         nexttimeout = send_entry->timetosend;
691                                         settimer = 1;
692                                 }
693                         } else {
694                                 int close_when_complete;
695                                 close_when_complete =
696                                         send_entry->close_when_complete;
697                                 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
698                                         cm_node, cm_node->state);
699                                 free_retrans_entry(cm_node);
700                                 if (close_when_complete)
701                                         rem_ref_cm_node(cm_node->cm_core,
702                                                 cm_node);
703                         }
704                 } while (0);
705
706                 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
707                 rem_ref_cm_node(cm_node->cm_core, cm_node);
708         }
709
710         if (settimer) {
711                 if (!timer_pending(&cm_core->tcp_timer)) {
712                         cm_core->tcp_timer.expires  = nexttimeout;
713                         add_timer(&cm_core->tcp_timer);
714                 }
715         }
716 }
717
718
719 /**
720  * send_syn
721  */
722 static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
723         struct sk_buff *skb)
724 {
725         int ret;
726         int flags = SET_SYN;
727         char optionsbuffer[sizeof(struct option_mss) +
728                 sizeof(struct option_windowscale) + sizeof(struct option_base) +
729                 TCP_OPTIONS_PADDING];
730
731         int optionssize = 0;
732         /* Sending MSS option */
733         union all_known_options *options;
734
735         if (!cm_node)
736                 return -EINVAL;
737
738         options = (union all_known_options *)&optionsbuffer[optionssize];
739         options->as_mss.optionnum = OPTION_NUMBER_MSS;
740         options->as_mss.length = sizeof(struct option_mss);
741         options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
742         optionssize += sizeof(struct option_mss);
743
744         options = (union all_known_options *)&optionsbuffer[optionssize];
745         options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
746         options->as_windowscale.length = sizeof(struct option_windowscale);
747         options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
748         optionssize += sizeof(struct option_windowscale);
749
750         if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
751                 options = (union all_known_options *)&optionsbuffer[optionssize];
752                 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
753                 options->as_base.length = sizeof(struct option_base);
754                 optionssize += sizeof(struct option_base);
755                 /* we need the size to be a multiple of 4 */
756                 options = (union all_known_options *)&optionsbuffer[optionssize];
757                 options->as_end = 1;
758                 optionssize += 1;
759                 options = (union all_known_options *)&optionsbuffer[optionssize];
760                 options->as_end = 1;
761                 optionssize += 1;
762         }
763
764         options = (union all_known_options *)&optionsbuffer[optionssize];
765         options->as_end = OPTION_NUMBER_END;
766         optionssize += 1;
767
768         if (!skb)
769                 skb = dev_alloc_skb(MAX_CM_BUFFER);
770         if (!skb) {
771                 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
772                 return -1;
773         }
774
775         if (sendack)
776                 flags |= SET_ACK;
777
778         form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
779         ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
780
781         return ret;
782 }
783
784
785 /**
786  * send_reset
787  */
788 static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
789 {
790         int ret;
791         int flags = SET_RST | SET_ACK;
792
793         if (!skb)
794                 skb = dev_alloc_skb(MAX_CM_BUFFER);
795         if (!skb) {
796                 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
797                 return -ENOMEM;
798         }
799
800         form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
801         ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
802
803         return ret;
804 }
805
806
807 /**
808  * send_ack
809  */
810 static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
811 {
812         int ret;
813
814         if (!skb)
815                 skb = dev_alloc_skb(MAX_CM_BUFFER);
816
817         if (!skb) {
818                 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
819                 return -1;
820         }
821
822         form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
823         ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
824
825         return ret;
826 }
827
828
829 /**
830  * send_fin
831  */
832 static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
833 {
834         int ret;
835
836         /* if we didn't get a frame get one */
837         if (!skb)
838                 skb = dev_alloc_skb(MAX_CM_BUFFER);
839
840         if (!skb) {
841                 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
842                 return -1;
843         }
844
845         form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
846         ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
847
848         return ret;
849 }
850
851
852 /**
853  * find_node - find a cm node that matches the reference cm node
854  */
855 static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
856                 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
857 {
858         unsigned long flags;
859         struct list_head *hte;
860         struct nes_cm_node *cm_node;
861
862         /* get a handle on the hte */
863         hte = &cm_core->connected_nodes;
864
865         /* walk list and find cm_node associated with this session ID */
866         spin_lock_irqsave(&cm_core->ht_lock, flags);
867         list_for_each_entry(cm_node, hte, list) {
868                 /* compare quad, return node handle if a match */
869                 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
870                                 cm_node->loc_addr, cm_node->loc_port,
871                                 loc_addr, loc_port,
872                                 cm_node->rem_addr, cm_node->rem_port,
873                                 rem_addr, rem_port);
874                 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
875                                 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
876                         add_ref_cm_node(cm_node);
877                         spin_unlock_irqrestore(&cm_core->ht_lock, flags);
878                         return cm_node;
879                 }
880         }
881         spin_unlock_irqrestore(&cm_core->ht_lock, flags);
882
883         /* no owner node */
884         return NULL;
885 }
886
887
888 /**
889  * find_listener - find a cm node listening on this addr-port pair
890  */
891 static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
892                 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
893 {
894         unsigned long flags;
895         struct nes_cm_listener *listen_node;
896
897         /* walk list and find cm_node associated with this session ID */
898         spin_lock_irqsave(&cm_core->listen_list_lock, flags);
899         list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
900                 /* compare node pair, return node handle if a match */
901                 if (((listen_node->loc_addr == dst_addr) ||
902                                 listen_node->loc_addr == 0x00000000) &&
903                                 (listen_node->loc_port == dst_port) &&
904                                 (listener_state & listen_node->listener_state)) {
905                         atomic_inc(&listen_node->ref_count);
906                         spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
907                         return listen_node;
908                 }
909         }
910         spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
911
912         /* no listener */
913         return NULL;
914 }
915
916
917 /**
918  * add_hte_node - add a cm node to the hash table
919  */
920 static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
921 {
922         unsigned long flags;
923         struct list_head *hte;
924
925         if (!cm_node || !cm_core)
926                 return -EINVAL;
927
928         nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
929                 cm_node);
930
931         spin_lock_irqsave(&cm_core->ht_lock, flags);
932
933         /* get a handle on the hash table element (list head for this slot) */
934         hte = &cm_core->connected_nodes;
935         list_add_tail(&cm_node->list, hte);
936         atomic_inc(&cm_core->ht_node_cnt);
937
938         spin_unlock_irqrestore(&cm_core->ht_lock, flags);
939
940         return 0;
941 }
942
943
944 /**
945  * mini_cm_dec_refcnt_listen
946  */
947 static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
948         struct nes_cm_listener *listener, int free_hanging_nodes)
949 {
950         int ret = -EINVAL;
951         int err = 0;
952         unsigned long flags;
953         struct list_head *list_pos = NULL;
954         struct list_head *list_temp = NULL;
955         struct nes_cm_node *cm_node = NULL;
956         struct list_head reset_list;
957
958         nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
959                 "refcnt=%d\n", listener, free_hanging_nodes,
960                 atomic_read(&listener->ref_count));
961         /* free non-accelerated child nodes for this listener */
962         INIT_LIST_HEAD(&reset_list);
963         if (free_hanging_nodes) {
964                 spin_lock_irqsave(&cm_core->ht_lock, flags);
965                 list_for_each_safe(list_pos, list_temp,
966                                    &g_cm_core->connected_nodes) {
967                         cm_node = container_of(list_pos, struct nes_cm_node,
968                                 list);
969                         if ((cm_node->listener == listener) &&
970                             (!cm_node->accelerated)) {
971                                 add_ref_cm_node(cm_node);
972                                 list_add(&cm_node->reset_entry, &reset_list);
973                         }
974                 }
975                 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
976         }
977
978         list_for_each_safe(list_pos, list_temp, &reset_list) {
979                 cm_node = container_of(list_pos, struct nes_cm_node,
980                                 reset_entry);
981                 {
982                         struct nes_cm_node *loopback = cm_node->loopbackpartner;
983                         enum nes_cm_node_state old_state;
984                         if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
985                                 rem_ref_cm_node(cm_node->cm_core, cm_node);
986                         } else {
987                                 if (!loopback) {
988                                         cleanup_retrans_entry(cm_node);
989                                         err = send_reset(cm_node, NULL);
990                                         if (err) {
991                                                 cm_node->state =
992                                                          NES_CM_STATE_CLOSED;
993                                                 WARN_ON(1);
994                                         } else {
995                                                 old_state = cm_node->state;
996                                                 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
997                                                 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
998                                                         rem_ref_cm_node(
999                                                                 cm_node->cm_core,
1000                                                                 cm_node);
1001                                         }
1002                                 } else {
1003                                         struct nes_cm_event event;
1004
1005                                         event.cm_node = loopback;
1006                                         event.cm_info.rem_addr =
1007                                                         loopback->rem_addr;
1008                                         event.cm_info.loc_addr =
1009                                                         loopback->loc_addr;
1010                                         event.cm_info.rem_port =
1011                                                         loopback->rem_port;
1012                                         event.cm_info.loc_port =
1013                                                          loopback->loc_port;
1014                                         event.cm_info.cm_id = loopback->cm_id;
1015                                         add_ref_cm_node(loopback);
1016                                         loopback->state = NES_CM_STATE_CLOSED;
1017                                         cm_event_connect_error(&event);
1018                                         cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1019
1020                                         rem_ref_cm_node(cm_node->cm_core,
1021                                                          cm_node);
1022
1023                                 }
1024                         }
1025                 }
1026         }
1027
1028         spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1029         if (!atomic_dec_return(&listener->ref_count)) {
1030                 list_del(&listener->list);
1031
1032                 /* decrement our listen node count */
1033                 atomic_dec(&cm_core->listen_node_cnt);
1034
1035                 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1036
1037                 if (listener->nesvnic) {
1038                         nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1039                                         PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1040                 }
1041
1042                 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1043
1044                 kfree(listener);
1045                 listener = NULL;
1046                 ret = 0;
1047                 atomic_inc(&cm_listens_destroyed);
1048         } else {
1049                 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1050         }
1051         if (listener) {
1052                 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1053                         nes_debug(NES_DBG_CM, "destroying listener (%p)"
1054                                         " with non-zero pending accepts=%u\n",
1055                                         listener, atomic_read(&listener->pend_accepts_cnt));
1056         }
1057
1058         return ret;
1059 }
1060
1061
1062 /**
1063  * mini_cm_del_listen
1064  */
1065 static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1066                 struct nes_cm_listener *listener)
1067 {
1068         listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1069         listener->cm_id = NULL; /* going to be destroyed pretty soon */
1070         return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1071 }
1072
1073
1074 /**
1075  * mini_cm_accelerated
1076  */
1077 static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1078                 struct nes_cm_node *cm_node)
1079 {
1080         u32 was_timer_set;
1081         cm_node->accelerated = 1;
1082
1083         if (cm_node->accept_pend) {
1084                 BUG_ON(!cm_node->listener);
1085                 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1086                 cm_node->accept_pend = 0;
1087                 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1088         }
1089
1090         was_timer_set = timer_pending(&cm_core->tcp_timer);
1091         if (!was_timer_set) {
1092                 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1093                 add_timer(&cm_core->tcp_timer);
1094         }
1095
1096         return 0;
1097 }
1098
1099
1100 /**
1101  * nes_addr_resolve_neigh
1102  */
1103 static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
1104 {
1105         struct rtable *rt;
1106         struct flowi fl;
1107         struct neighbour *neigh;
1108         int rc = arpindex;
1109         struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
1110
1111         memset(&fl, 0, sizeof fl);
1112         fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1113         if (ip_route_output_key(&init_net, &rt, &fl)) {
1114                 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
1115                                 __func__, dst_ip);
1116                 return rc;
1117         }
1118
1119         neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1120         if (neigh) {
1121                 if (neigh->nud_state & NUD_VALID) {
1122                         nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
1123                                   " is %pM, Gateway is 0x%08X \n", dst_ip,
1124                                   neigh->ha, ntohl(rt->rt_gateway));
1125
1126                         if (arpindex >= 0) {
1127                                 if (!memcmp(nesadapter->arp_table[arpindex].mac_addr,
1128                                                         neigh->ha, ETH_ALEN)){
1129                                         /* Mac address same as in nes_arp_table */
1130                                         neigh_release(neigh);
1131                                         ip_rt_put(rt);
1132                                         return rc;
1133                                 }
1134
1135                                 nes_manage_arp_cache(nesvnic->netdev,
1136                                                 nesadapter->arp_table[arpindex].mac_addr,
1137                                                 dst_ip, NES_ARP_DELETE);
1138                         }
1139
1140                         nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1141                                              dst_ip, NES_ARP_ADD);
1142                         rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1143                                            NES_ARP_RESOLVE);
1144                 }
1145                 neigh_release(neigh);
1146         }
1147
1148         if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1149                 neigh_event_send(rt->u.dst.neighbour, NULL);
1150
1151         ip_rt_put(rt);
1152         return rc;
1153 }
1154
1155 /**
1156  * make_cm_node - create a new instance of a cm node
1157  */
1158 static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1159                 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1160                 struct nes_cm_listener *listener)
1161 {
1162         struct nes_cm_node *cm_node;
1163         struct timespec ts;
1164         int oldarpindex = 0;
1165         int arpindex = 0;
1166         struct nes_device *nesdev;
1167         struct nes_adapter *nesadapter;
1168
1169         /* create an hte and cm_node for this instance */
1170         cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1171         if (!cm_node)
1172                 return NULL;
1173
1174         /* set our node specific transport info */
1175         cm_node->loc_addr = cm_info->loc_addr;
1176         cm_node->rem_addr = cm_info->rem_addr;
1177         cm_node->loc_port = cm_info->loc_port;
1178         cm_node->rem_port = cm_info->rem_port;
1179         cm_node->send_write0 = send_first;
1180         nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1181                   &cm_node->loc_addr, cm_node->loc_port,
1182                   &cm_node->rem_addr, cm_node->rem_port);
1183         cm_node->listener = listener;
1184         cm_node->netdev = nesvnic->netdev;
1185         cm_node->cm_id = cm_info->cm_id;
1186         memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1187
1188         nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1189                         cm_node->cm_id);
1190
1191         spin_lock_init(&cm_node->retrans_list_lock);
1192
1193         cm_node->loopbackpartner = NULL;
1194         atomic_set(&cm_node->ref_count, 1);
1195         /* associate our parent CM core */
1196         cm_node->cm_core = cm_core;
1197         cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1198         cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1199         cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1200                         NES_CM_DEFAULT_RCV_WND_SCALE;
1201         ts = current_kernel_time();
1202         cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1203         cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
1204                         sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
1205         cm_node->tcp_cntxt.rcv_nxt = 0;
1206         /* get a unique session ID , add thread_id to an upcounter to handle race */
1207         atomic_inc(&cm_core->node_cnt);
1208         cm_node->conn_type = cm_info->conn_type;
1209         cm_node->apbvt_set = 0;
1210         cm_node->accept_pend = 0;
1211
1212         cm_node->nesvnic = nesvnic;
1213         /* get some device handles, for arp lookup */
1214         nesdev = nesvnic->nesdev;
1215         nesadapter = nesdev->nesadapter;
1216
1217         cm_node->loopbackpartner = NULL;
1218
1219         /* get the mac addr for the remote node */
1220         if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1221                 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
1222         else {
1223                 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1224                 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);
1225
1226         }
1227         if (arpindex < 0) {
1228                 kfree(cm_node);
1229                 return NULL;
1230         }
1231
1232         /* copy the mac addr to node context */
1233         memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
1234         nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1235                   cm_node->rem_mac);
1236
1237         add_hte_node(cm_core, cm_node);
1238         atomic_inc(&cm_nodes_created);
1239
1240         return cm_node;
1241 }
1242
1243
1244 /**
1245  * add_ref_cm_node - destroy an instance of a cm node
1246  */
1247 static int add_ref_cm_node(struct nes_cm_node *cm_node)
1248 {
1249         atomic_inc(&cm_node->ref_count);
1250         return 0;
1251 }
1252
1253
1254 /**
1255  * rem_ref_cm_node - destroy an instance of a cm node
1256  */
1257 static int rem_ref_cm_node(struct nes_cm_core *cm_core,
1258         struct nes_cm_node *cm_node)
1259 {
1260         unsigned long flags;
1261         struct nes_qp *nesqp;
1262
1263         if (!cm_node)
1264                 return -EINVAL;
1265
1266         spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1267         if (atomic_dec_return(&cm_node->ref_count)) {
1268                 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1269                 return 0;
1270         }
1271         list_del(&cm_node->list);
1272         atomic_dec(&cm_core->ht_node_cnt);
1273         spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1274
1275         /* if the node is destroyed before connection was accelerated */
1276         if (!cm_node->accelerated && cm_node->accept_pend) {
1277                 BUG_ON(!cm_node->listener);
1278                 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1279                 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1280         }
1281         WARN_ON(cm_node->send_entry);
1282         if (cm_node->recv_entry)
1283                 handle_recv_entry(cm_node, 0);
1284         if (cm_node->listener) {
1285                 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1286         } else {
1287                 if (cm_node->apbvt_set && cm_node->nesvnic) {
1288                         nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
1289                                 PCI_FUNC(
1290                                 cm_node->nesvnic->nesdev->pcidev->devfn),
1291                                 NES_MANAGE_APBVT_DEL);
1292                 }
1293         }
1294
1295         atomic_dec(&cm_core->node_cnt);
1296         atomic_inc(&cm_nodes_destroyed);
1297         nesqp = cm_node->nesqp;
1298         if (nesqp) {
1299                 nesqp->cm_node = NULL;
1300                 nes_rem_ref(&nesqp->ibqp);
1301                 cm_node->nesqp = NULL;
1302         }
1303
1304         kfree(cm_node);
1305         return 0;
1306 }
1307
1308 /**
1309  * process_options
1310  */
1311 static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1312         u32 optionsize, u32 syn_packet)
1313 {
1314         u32 tmp;
1315         u32 offset = 0;
1316         union all_known_options *all_options;
1317         char got_mss_option = 0;
1318
1319         while (offset < optionsize) {
1320                 all_options = (union all_known_options *)(optionsloc + offset);
1321                 switch (all_options->as_base.optionnum) {
1322                 case OPTION_NUMBER_END:
1323                         offset = optionsize;
1324                         break;
1325                 case OPTION_NUMBER_NONE:
1326                         offset += 1;
1327                         continue;
1328                 case OPTION_NUMBER_MSS:
1329                         nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1330                                 "Size: %d\n", __func__,
1331                                 all_options->as_mss.length, offset, optionsize);
1332                         got_mss_option = 1;
1333                         if (all_options->as_mss.length != 4) {
1334                                 return 1;
1335                         } else {
1336                                 tmp = ntohs(all_options->as_mss.mss);
1337                                 if (tmp > 0 && tmp <
1338                                         cm_node->tcp_cntxt.mss)
1339                                         cm_node->tcp_cntxt.mss = tmp;
1340                         }
1341                         break;
1342                 case OPTION_NUMBER_WINDOW_SCALE:
1343                         cm_node->tcp_cntxt.snd_wscale =
1344                                 all_options->as_windowscale.shiftcount;
1345                         break;
1346                 case OPTION_NUMBER_WRITE0:
1347                         cm_node->send_write0 = 1;
1348                         break;
1349                 default:
1350                         nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1351                                 all_options->as_base.optionnum);
1352                         break;
1353                 }
1354                 offset += all_options->as_base.length;
1355         }
1356         if ((!got_mss_option) && (syn_packet))
1357                 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1358         return 0;
1359 }
1360
1361 static void drop_packet(struct sk_buff *skb)
1362 {
1363         atomic_inc(&cm_accel_dropped_pkts);
1364         dev_kfree_skb_any(skb);
1365 }
1366
1367 static void handle_fin_pkt(struct nes_cm_node *cm_node)
1368 {
1369         nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1370                 "refcnt=%d\n", cm_node, cm_node->state,
1371                 atomic_read(&cm_node->ref_count));
1372         switch (cm_node->state) {
1373         case NES_CM_STATE_SYN_RCVD:
1374         case NES_CM_STATE_SYN_SENT:
1375         case NES_CM_STATE_ESTABLISHED:
1376         case NES_CM_STATE_MPAREJ_RCVD:
1377                 cm_node->tcp_cntxt.rcv_nxt++;
1378                 cleanup_retrans_entry(cm_node);
1379                 cm_node->state = NES_CM_STATE_LAST_ACK;
1380                 send_fin(cm_node, NULL);
1381                 break;
1382         case NES_CM_STATE_MPAREQ_SENT:
1383                 create_event(cm_node, NES_CM_EVENT_ABORTED);
1384                 cm_node->tcp_cntxt.rcv_nxt++;
1385                 cleanup_retrans_entry(cm_node);
1386                 cm_node->state = NES_CM_STATE_CLOSED;
1387                 add_ref_cm_node(cm_node);
1388                 send_reset(cm_node, NULL);
1389                 break;
1390         case NES_CM_STATE_FIN_WAIT1:
1391                 cm_node->tcp_cntxt.rcv_nxt++;
1392                 cleanup_retrans_entry(cm_node);
1393                 cm_node->state = NES_CM_STATE_CLOSING;
1394                 send_ack(cm_node, NULL);
1395                 /* Wait for ACK as this is simultanous close..
1396                 * After we receive ACK, do not send anything..
1397                 * Just rm the node.. Done.. */
1398                 break;
1399         case NES_CM_STATE_FIN_WAIT2:
1400                 cm_node->tcp_cntxt.rcv_nxt++;
1401                 cleanup_retrans_entry(cm_node);
1402                 cm_node->state = NES_CM_STATE_TIME_WAIT;
1403                 send_ack(cm_node, NULL);
1404                 schedule_nes_timer(cm_node, NULL,  NES_TIMER_TYPE_CLOSE, 1, 0);
1405                 break;
1406         case NES_CM_STATE_TIME_WAIT:
1407                 cm_node->tcp_cntxt.rcv_nxt++;
1408                 cleanup_retrans_entry(cm_node);
1409                 cm_node->state = NES_CM_STATE_CLOSED;
1410                 rem_ref_cm_node(cm_node->cm_core, cm_node);
1411                 break;
1412         case NES_CM_STATE_TSA:
1413         default:
1414                 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1415                         cm_node, cm_node->state);
1416                 break;
1417         }
1418 }
1419
1420
1421 static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1422         struct tcphdr *tcph)
1423 {
1424
1425         int     reset = 0;      /* whether to send reset in case of err.. */
1426         int     passive_state;
1427         atomic_inc(&cm_resets_recvd);
1428         nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1429                         " refcnt=%d\n", cm_node, cm_node->state,
1430                         atomic_read(&cm_node->ref_count));
1431         cleanup_retrans_entry(cm_node);
1432         switch (cm_node->state) {
1433         case NES_CM_STATE_SYN_SENT:
1434         case NES_CM_STATE_MPAREQ_SENT:
1435                 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1436                         "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1437                         cm_node->listener, cm_node->state);
1438                 active_open_err(cm_node, skb, reset);
1439                 break;
1440         case NES_CM_STATE_MPAREQ_RCVD:
1441                 passive_state = atomic_add_return(1, &cm_node->passive_state);
1442                 if (passive_state ==  NES_SEND_RESET_EVENT)
1443                         create_event(cm_node, NES_CM_EVENT_RESET);
1444                 cm_node->state = NES_CM_STATE_CLOSED;
1445                 dev_kfree_skb_any(skb);
1446                 break;
1447         case NES_CM_STATE_ESTABLISHED:
1448         case NES_CM_STATE_SYN_RCVD:
1449         case NES_CM_STATE_LISTENING:
1450                 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1451                 passive_open_err(cm_node, skb, reset);
1452                 break;
1453         case NES_CM_STATE_TSA:
1454                 active_open_err(cm_node, skb, reset);
1455                 break;
1456         case NES_CM_STATE_CLOSED:
1457                 drop_packet(skb);
1458                 break;
1459         case NES_CM_STATE_FIN_WAIT1:
1460         case NES_CM_STATE_LAST_ACK:
1461                 cm_node->cm_id->rem_ref(cm_node->cm_id);
1462         case NES_CM_STATE_TIME_WAIT:
1463                 cm_node->state = NES_CM_STATE_CLOSED;
1464                 rem_ref_cm_node(cm_node->cm_core, cm_node);
1465                 drop_packet(skb);
1466                 break;
1467         default:
1468                 drop_packet(skb);
1469                 break;
1470         }
1471 }
1472
1473
1474 static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
1475 {
1476
1477         int     ret = 0;
1478         int datasize = skb->len;
1479         u8 *dataloc = skb->data;
1480
1481         enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1482         u32     res_type;
1483         ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1484         if (ret) {
1485                 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
1486                 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
1487                         nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1488                                 "cm_node=%p listener=%p state=%d\n", __func__,
1489                                 __LINE__, cm_node, cm_node->listener,
1490                                 cm_node->state);
1491                         active_open_err(cm_node, skb, 1);
1492                 } else {
1493                         passive_open_err(cm_node, skb, 1);
1494                 }
1495                 return;
1496         }
1497
1498         switch (cm_node->state) {
1499         case NES_CM_STATE_ESTABLISHED:
1500                 if (res_type == NES_MPA_REQUEST_REJECT) {
1501                         /*BIG problem as we are receiving the MPA.. So should
1502                         * not be REJECT.. This is Passive Open.. We can
1503                         * only receive it Reject for Active Open...*/
1504                         WARN_ON(1);
1505                 }
1506                 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1507                 type = NES_CM_EVENT_MPA_REQ;
1508                 atomic_set(&cm_node->passive_state,
1509                                 NES_PASSIVE_STATE_INDICATED);
1510                 break;
1511         case NES_CM_STATE_MPAREQ_SENT:
1512                 cleanup_retrans_entry(cm_node);
1513                 if (res_type == NES_MPA_REQUEST_REJECT) {
1514                         type = NES_CM_EVENT_MPA_REJECT;
1515                         cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1516                 } else {
1517                         type = NES_CM_EVENT_CONNECTED;
1518                         cm_node->state = NES_CM_STATE_TSA;
1519                 }
1520
1521                 break;
1522         default:
1523                 WARN_ON(1);
1524                 break;
1525         }
1526         dev_kfree_skb_any(skb);
1527         create_event(cm_node, type);
1528 }
1529
1530 static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1531 {
1532         switch (cm_node->state) {
1533         case NES_CM_STATE_SYN_SENT:
1534         case NES_CM_STATE_MPAREQ_SENT:
1535                 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1536                         "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1537                         cm_node->listener, cm_node->state);
1538                 active_open_err(cm_node, skb, 1);
1539                 break;
1540         case NES_CM_STATE_ESTABLISHED:
1541         case NES_CM_STATE_SYN_RCVD:
1542                 passive_open_err(cm_node, skb, 1);
1543                 break;
1544         case NES_CM_STATE_TSA:
1545         default:
1546                 drop_packet(skb);
1547         }
1548 }
1549
1550 static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1551         struct sk_buff *skb)
1552 {
1553         int err;
1554
1555         err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1556         if (err)
1557                 active_open_err(cm_node, skb, 1);
1558
1559         return err;
1560 }
1561
1562 static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1563         struct sk_buff *skb)
1564 {
1565         int err = 0;
1566         u32 seq;
1567         u32 ack_seq;
1568         u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1569         u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1570         u32 rcv_wnd;
1571         seq = ntohl(tcph->seq);
1572         ack_seq = ntohl(tcph->ack_seq);
1573         rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1574         if (ack_seq != loc_seq_num)
1575                 err = 1;
1576         else if (!between(seq, rcv_nxt, (rcv_nxt+rcv_wnd)))
1577                 err = 1;
1578         if (err) {
1579                 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1580                         "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1581                         cm_node->listener, cm_node->state);
1582                 indicate_pkt_err(cm_node, skb);
1583                 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1584                         "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1585                         rcv_wnd);
1586         }
1587         return err;
1588 }
1589
1590 /*
1591  * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1592  * is created with a listener or it may comein as rexmitted packet which in
1593  * that case will be just dropped.
1594  */
1595
1596 static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1597         struct tcphdr *tcph)
1598 {
1599         int ret;
1600         u32 inc_sequence;
1601         int optionsize;
1602
1603         optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1604         skb_trim(skb, 0);
1605         inc_sequence = ntohl(tcph->seq);
1606
1607         switch (cm_node->state) {
1608         case NES_CM_STATE_SYN_SENT:
1609         case NES_CM_STATE_MPAREQ_SENT:
1610                 /* Rcvd syn on active open connection*/
1611                 active_open_err(cm_node, skb, 1);
1612                 break;
1613         case NES_CM_STATE_LISTENING:
1614                 /* Passive OPEN */
1615                 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1616                                 cm_node->listener->backlog) {
1617                         nes_debug(NES_DBG_CM, "drop syn due to backlog "
1618                                 "pressure \n");
1619                         cm_backlog_drops++;
1620                         passive_open_err(cm_node, skb, 0);
1621                         break;
1622                 }
1623                 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1624                         1);
1625                 if (ret) {
1626                         passive_open_err(cm_node, skb, 0);
1627                         /* drop pkt */
1628                         break;
1629                 }
1630                 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1631                 BUG_ON(cm_node->send_entry);
1632                 cm_node->accept_pend = 1;
1633                 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1634
1635                 cm_node->state = NES_CM_STATE_SYN_RCVD;
1636                 send_syn(cm_node, 1, skb);
1637                 break;
1638         case NES_CM_STATE_CLOSED:
1639                 cleanup_retrans_entry(cm_node);
1640                 add_ref_cm_node(cm_node);
1641                 send_reset(cm_node, skb);
1642                 break;
1643         case NES_CM_STATE_TSA:
1644         case NES_CM_STATE_ESTABLISHED:
1645         case NES_CM_STATE_FIN_WAIT1:
1646         case NES_CM_STATE_FIN_WAIT2:
1647         case NES_CM_STATE_MPAREQ_RCVD:
1648         case NES_CM_STATE_LAST_ACK:
1649         case NES_CM_STATE_CLOSING:
1650         case NES_CM_STATE_UNKNOWN:
1651         default:
1652                 drop_packet(skb);
1653                 break;
1654         }
1655 }
1656
1657 static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1658         struct tcphdr *tcph)
1659 {
1660
1661         int ret;
1662         u32 inc_sequence;
1663         int optionsize;
1664
1665         optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1666         skb_trim(skb, 0);
1667         inc_sequence = ntohl(tcph->seq);
1668         switch (cm_node->state) {
1669         case NES_CM_STATE_SYN_SENT:
1670                 cleanup_retrans_entry(cm_node);
1671                 /* active open */
1672                 if (check_syn(cm_node, tcph, skb))
1673                         return;
1674                 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1675                 /* setup options */
1676                 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1677                 if (ret) {
1678                         nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1679                                 cm_node);
1680                         break;
1681                 }
1682                 cleanup_retrans_entry(cm_node);
1683                 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1684                 send_mpa_request(cm_node, skb);
1685                 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1686                 break;
1687         case NES_CM_STATE_MPAREQ_RCVD:
1688                 /* passive open, so should not be here */
1689                 passive_open_err(cm_node, skb, 1);
1690                 break;
1691         case NES_CM_STATE_LISTENING:
1692                 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1693                 cleanup_retrans_entry(cm_node);
1694                 cm_node->state = NES_CM_STATE_CLOSED;
1695                 send_reset(cm_node, skb);
1696                 break;
1697         case NES_CM_STATE_CLOSED:
1698                 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1699                 cleanup_retrans_entry(cm_node);
1700                 add_ref_cm_node(cm_node);
1701                 send_reset(cm_node, skb);
1702                 break;
1703         case NES_CM_STATE_ESTABLISHED:
1704         case NES_CM_STATE_FIN_WAIT1:
1705         case NES_CM_STATE_FIN_WAIT2:
1706         case NES_CM_STATE_LAST_ACK:
1707         case NES_CM_STATE_TSA:
1708         case NES_CM_STATE_CLOSING:
1709         case NES_CM_STATE_UNKNOWN:
1710         case NES_CM_STATE_MPAREQ_SENT:
1711         default:
1712                 drop_packet(skb);
1713                 break;
1714         }
1715 }
1716
1717 static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1718         struct tcphdr *tcph)
1719 {
1720         int datasize = 0;
1721         u32 inc_sequence;
1722         u32 rem_seq_ack;
1723         u32 rem_seq;
1724         int ret = 0;
1725         int optionsize;
1726         optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1727
1728         if (check_seq(cm_node, tcph, skb))
1729                 return -EINVAL;
1730
1731         skb_pull(skb, tcph->doff << 2);
1732         inc_sequence = ntohl(tcph->seq);
1733         rem_seq = ntohl(tcph->seq);
1734         rem_seq_ack =  ntohl(tcph->ack_seq);
1735         datasize = skb->len;
1736         switch (cm_node->state) {
1737         case NES_CM_STATE_SYN_RCVD:
1738                 /* Passive OPEN */
1739                 cleanup_retrans_entry(cm_node);
1740                 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1741                 if (ret)
1742                         break;
1743                 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1744                 cm_node->state = NES_CM_STATE_ESTABLISHED;
1745                 if (datasize) {
1746                         cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1747                         handle_rcv_mpa(cm_node, skb);
1748                 } else  /* rcvd ACK only */
1749                         dev_kfree_skb_any(skb);
1750                 break;
1751         case NES_CM_STATE_ESTABLISHED:
1752                 /* Passive OPEN */
1753                 cleanup_retrans_entry(cm_node);
1754                 if (datasize) {
1755                         cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1756                         handle_rcv_mpa(cm_node, skb);
1757                 } else
1758                         drop_packet(skb);
1759                 break;
1760         case NES_CM_STATE_MPAREQ_SENT:
1761                 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1762                 if (datasize) {
1763                         cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1764                         handle_rcv_mpa(cm_node, skb);
1765                 } else  /* Could be just an ack pkt.. */
1766                         dev_kfree_skb_any(skb);
1767                 break;
1768         case NES_CM_STATE_LISTENING:
1769                 cleanup_retrans_entry(cm_node);
1770                 cm_node->state = NES_CM_STATE_CLOSED;
1771                 send_reset(cm_node, skb);
1772                 break;
1773         case NES_CM_STATE_CLOSED:
1774                 cleanup_retrans_entry(cm_node);
1775                 add_ref_cm_node(cm_node);
1776                 send_reset(cm_node, skb);
1777                 break;
1778         case NES_CM_STATE_LAST_ACK:
1779         case NES_CM_STATE_CLOSING:
1780                 cleanup_retrans_entry(cm_node);
1781                 cm_node->state = NES_CM_STATE_CLOSED;
1782                 cm_node->cm_id->rem_ref(cm_node->cm_id);
1783                 rem_ref_cm_node(cm_node->cm_core, cm_node);
1784                 drop_packet(skb);
1785                 break;
1786         case NES_CM_STATE_FIN_WAIT1:
1787                 cleanup_retrans_entry(cm_node);
1788                 drop_packet(skb);
1789                 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1790                 break;
1791         case NES_CM_STATE_SYN_SENT:
1792         case NES_CM_STATE_FIN_WAIT2:
1793         case NES_CM_STATE_TSA:
1794         case NES_CM_STATE_MPAREQ_RCVD:
1795         case NES_CM_STATE_UNKNOWN:
1796         default:
1797                 cleanup_retrans_entry(cm_node);
1798                 drop_packet(skb);
1799                 break;
1800         }
1801         return ret;
1802 }
1803
1804
1805
1806 static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1807         struct sk_buff *skb, int optionsize, int passive)
1808 {
1809         u8 *optionsloc = (u8 *)&tcph[1];
1810         if (optionsize) {
1811                 if (process_options(cm_node, optionsloc, optionsize,
1812                         (u32)tcph->syn)) {
1813                         nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1814                                 __func__, cm_node);
1815                         if (passive)
1816                                 passive_open_err(cm_node, skb, 1);
1817                         else
1818                                 active_open_err(cm_node, skb, 1);
1819                         return 1;
1820                 }
1821         }
1822
1823         cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1824                         cm_node->tcp_cntxt.snd_wscale;
1825
1826         if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
1827                 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
1828         return 0;
1829 }
1830
1831 /*
1832  * active_open_err() will send reset() if flag set..
1833  * It will also send ABORT event.
1834  */
1835
1836 static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1837         int reset)
1838 {
1839         cleanup_retrans_entry(cm_node);
1840         if (reset) {
1841                 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1842                                 "state=%d\n", cm_node, cm_node->state);
1843                 add_ref_cm_node(cm_node);
1844                 send_reset(cm_node, skb);
1845         } else
1846                 dev_kfree_skb_any(skb);
1847
1848         cm_node->state = NES_CM_STATE_CLOSED;
1849         create_event(cm_node, NES_CM_EVENT_ABORTED);
1850 }
1851
1852 /*
1853  * passive_open_err() will either do a reset() or will free up the skb and
1854  * remove the cm_node.
1855  */
1856
1857 static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1858         int reset)
1859 {
1860         cleanup_retrans_entry(cm_node);
1861         cm_node->state = NES_CM_STATE_CLOSED;
1862         if (reset) {
1863                 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1864                         "cm_node=%p state =%d\n", cm_node, cm_node->state);
1865                 send_reset(cm_node, skb);
1866         } else {
1867                 dev_kfree_skb_any(skb);
1868                 rem_ref_cm_node(cm_node->cm_core, cm_node);
1869         }
1870 }
1871
1872 /*
1873  * free_retrans_entry() routines assumes that the retrans_list_lock has
1874  * been acquired before calling.
1875  */
1876 static void free_retrans_entry(struct nes_cm_node *cm_node)
1877 {
1878         struct nes_timer_entry *send_entry;
1879         send_entry = cm_node->send_entry;
1880         if (send_entry) {
1881                 cm_node->send_entry = NULL;
1882                 dev_kfree_skb_any(send_entry->skb);
1883                 kfree(send_entry);
1884                 rem_ref_cm_node(cm_node->cm_core, cm_node);
1885         }
1886 }
1887
1888 static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1889 {
1890         unsigned long flags;
1891
1892         spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1893         free_retrans_entry(cm_node);
1894         spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1895 }
1896
1897 /**
1898  * process_packet
1899  * Returns skb if to be freed, else it will return NULL if already used..
1900  */
1901 static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1902         struct nes_cm_core *cm_core)
1903 {
1904         enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1905         struct tcphdr *tcph = tcp_hdr(skb);
1906         u32     fin_set = 0;
1907         int ret = 0;
1908         skb_pull(skb, ip_hdr(skb)->ihl << 2);
1909
1910         nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1911                 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1912                 tcph->ack, tcph->rst, tcph->fin);
1913
1914         if (tcph->rst)
1915                 pkt_type = NES_PKT_TYPE_RST;
1916         else if (tcph->syn) {
1917                 pkt_type = NES_PKT_TYPE_SYN;
1918                 if (tcph->ack)
1919                         pkt_type = NES_PKT_TYPE_SYNACK;
1920         } else if (tcph->ack)
1921                 pkt_type = NES_PKT_TYPE_ACK;
1922         if (tcph->fin)
1923                 fin_set = 1;
1924
1925         switch (pkt_type) {
1926         case NES_PKT_TYPE_SYN:
1927                 handle_syn_pkt(cm_node, skb, tcph);
1928                 break;
1929         case NES_PKT_TYPE_SYNACK:
1930                 handle_synack_pkt(cm_node, skb, tcph);
1931                 break;
1932         case NES_PKT_TYPE_ACK:
1933                 ret = handle_ack_pkt(cm_node, skb, tcph);
1934                 if (fin_set && !ret)
1935                         handle_fin_pkt(cm_node);
1936                 break;
1937         case NES_PKT_TYPE_RST:
1938                 handle_rst_pkt(cm_node, skb, tcph);
1939                 break;
1940         default:
1941                 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
1942                         handle_fin_pkt(cm_node);
1943                 drop_packet(skb);
1944                 break;
1945         }
1946 }
1947
1948 /**
1949  * mini_cm_listen - create a listen node with params
1950  */
1951 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
1952         struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
1953 {
1954         struct nes_cm_listener *listener;
1955         unsigned long flags;
1956
1957         nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1958                 cm_info->loc_addr, cm_info->loc_port);
1959
1960         /* cannot have multiple matching listeners */
1961         listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1962                         htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1963         if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1964                 /* find automatically incs ref count ??? */
1965                 atomic_dec(&listener->ref_count);
1966                 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1967                 return NULL;
1968         }
1969
1970         if (!listener) {
1971                 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1972                 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1973                 if (!listener) {
1974                         nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1975                         return NULL;
1976                 }
1977
1978                 listener->loc_addr = htonl(cm_info->loc_addr);
1979                 listener->loc_port = htons(cm_info->loc_port);
1980                 listener->reused_node = 0;
1981
1982                 atomic_set(&listener->ref_count, 1);
1983         }
1984         /* pasive case */
1985         /* find already inc'ed the ref count */
1986         else {
1987                 listener->reused_node = 1;
1988         }
1989
1990         listener->cm_id = cm_info->cm_id;
1991         atomic_set(&listener->pend_accepts_cnt, 0);
1992         listener->cm_core = cm_core;
1993         listener->nesvnic = nesvnic;
1994         atomic_inc(&cm_core->node_cnt);
1995
1996         listener->conn_type = cm_info->conn_type;
1997         listener->backlog = cm_info->backlog;
1998         listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1999
2000         if (!listener->reused_node) {
2001                 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2002                 list_add(&listener->list, &cm_core->listen_list.list);
2003                 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2004                 atomic_inc(&cm_core->listen_node_cnt);
2005         }
2006
2007         nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
2008                         " listener = %p, backlog = %d, cm_id = %p.\n",
2009                         cm_info->loc_addr, cm_info->loc_port,
2010                         listener, listener->backlog, listener->cm_id);
2011
2012         return listener;
2013 }
2014
2015
2016 /**
2017  * mini_cm_connect - make a connection node with params
2018  */
2019 static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
2020         struct nes_vnic *nesvnic, u16 private_data_len,
2021         void *private_data, struct nes_cm_info *cm_info)
2022 {
2023         int ret = 0;
2024         struct nes_cm_node *cm_node;
2025         struct nes_cm_listener *loopbackremotelistener;
2026         struct nes_cm_node *loopbackremotenode;
2027         struct nes_cm_info loopback_cm_info;
2028         u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
2029         struct ietf_mpa_frame *mpa_frame = NULL;
2030
2031         /* create a CM connection node */
2032         cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2033         if (!cm_node)
2034                 return NULL;
2035         mpa_frame = &cm_node->mpa_frame;
2036         memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
2037         mpa_frame->flags = IETF_MPA_FLAGS_CRC;
2038         mpa_frame->rev =  IETF_MPA_VERSION;
2039         mpa_frame->priv_data_len = htons(private_data_len);
2040
2041         /* set our node side to client (active) side */
2042         cm_node->tcp_cntxt.client = 1;
2043         cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2044
2045         if (cm_info->loc_addr == cm_info->rem_addr) {
2046                 loopbackremotelistener = find_listener(cm_core,
2047                                 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2048                                 NES_CM_LISTENER_ACTIVE_STATE);
2049                 if (loopbackremotelistener == NULL) {
2050                         create_event(cm_node, NES_CM_EVENT_ABORTED);
2051                 } else {
2052                         loopback_cm_info = *cm_info;
2053                         loopback_cm_info.loc_port = cm_info->rem_port;
2054                         loopback_cm_info.rem_port = cm_info->loc_port;
2055                         loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
2056                         loopbackremotenode = make_cm_node(cm_core, nesvnic,
2057                                 &loopback_cm_info, loopbackremotelistener);
2058                         if (!loopbackremotenode) {
2059                                 rem_ref_cm_node(cm_node->cm_core, cm_node);
2060                                 return NULL;
2061                         }
2062                         atomic_inc(&cm_loopbacks);
2063                         loopbackremotenode->loopbackpartner = cm_node;
2064                         loopbackremotenode->tcp_cntxt.rcv_wscale =
2065                                 NES_CM_DEFAULT_RCV_WND_SCALE;
2066                         cm_node->loopbackpartner = loopbackremotenode;
2067                         memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2068                                 private_data_len);
2069                         loopbackremotenode->mpa_frame_size = private_data_len;
2070
2071                         /* we are done handling this state. */
2072                         /* set node to a TSA state */
2073                         cm_node->state = NES_CM_STATE_TSA;
2074                         cm_node->tcp_cntxt.rcv_nxt =
2075                                 loopbackremotenode->tcp_cntxt.loc_seq_num;
2076                         loopbackremotenode->tcp_cntxt.rcv_nxt =
2077                                 cm_node->tcp_cntxt.loc_seq_num;
2078                         cm_node->tcp_cntxt.max_snd_wnd =
2079                                 loopbackremotenode->tcp_cntxt.rcv_wnd;
2080                         loopbackremotenode->tcp_cntxt.max_snd_wnd =
2081                                 cm_node->tcp_cntxt.rcv_wnd;
2082                         cm_node->tcp_cntxt.snd_wnd =
2083                                 loopbackremotenode->tcp_cntxt.rcv_wnd;
2084                         loopbackremotenode->tcp_cntxt.snd_wnd =
2085                                 cm_node->tcp_cntxt.rcv_wnd;
2086                         cm_node->tcp_cntxt.snd_wscale =
2087                                 loopbackremotenode->tcp_cntxt.rcv_wscale;
2088                         loopbackremotenode->tcp_cntxt.snd_wscale =
2089                                 cm_node->tcp_cntxt.rcv_wscale;
2090                         loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
2091                         create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2092                 }
2093                 return cm_node;
2094         }
2095
2096         /* set our node side to client (active) side */
2097         cm_node->tcp_cntxt.client = 1;
2098         /* init our MPA frame ptr */
2099         memcpy(mpa_frame->priv_data, private_data, private_data_len);
2100
2101         cm_node->mpa_frame_size = mpa_frame_size;
2102
2103         /* send a syn and goto syn sent state */
2104         cm_node->state = NES_CM_STATE_SYN_SENT;
2105         ret = send_syn(cm_node, 0, NULL);
2106
2107         if (ret) {
2108                 /* error in sending the syn free up the cm_node struct */
2109                 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2110                         "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2111                         cm_node->rem_addr, cm_node->rem_port, cm_node,
2112                         cm_node->cm_id);
2113                 rem_ref_cm_node(cm_node->cm_core, cm_node);
2114                 cm_node = NULL;
2115         }
2116
2117         if (cm_node)
2118                 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2119                         "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2120                         cm_node->rem_addr, cm_node->rem_port, cm_node,
2121                         cm_node->cm_id);
2122
2123         return cm_node;
2124 }
2125
2126
2127 /**
2128  * mini_cm_accept - accept a connection
2129  * This function is never called
2130  */
2131 static int mini_cm_accept(struct nes_cm_core *cm_core,
2132         struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
2133 {
2134         return 0;
2135 }
2136
2137
2138 /**
2139  * mini_cm_reject - reject and teardown a connection
2140  */
2141 static int mini_cm_reject(struct nes_cm_core *cm_core,
2142         struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
2143 {
2144         int ret = 0;
2145         int err = 0;
2146         int passive_state;
2147         struct nes_cm_event event;
2148         struct iw_cm_id *cm_id = cm_node->cm_id;
2149         struct nes_cm_node *loopback = cm_node->loopbackpartner;
2150
2151         nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2152                 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
2153
2154         if (cm_node->tcp_cntxt.client)
2155                 return ret;
2156         cleanup_retrans_entry(cm_node);
2157
2158         if (!loopback) {
2159                 passive_state = atomic_add_return(1, &cm_node->passive_state);
2160                 if (passive_state == NES_SEND_RESET_EVENT) {
2161                         cm_node->state = NES_CM_STATE_CLOSED;
2162                         rem_ref_cm_node(cm_core, cm_node);
2163                 } else {
2164                         if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2165                                 rem_ref_cm_node(cm_core, cm_node);
2166                         } else {
2167                                 ret = send_mpa_reject(cm_node);
2168                                 if (ret) {
2169                                         cm_node->state = NES_CM_STATE_CLOSED;
2170                                         err = send_reset(cm_node, NULL);
2171                                         if (err)
2172                                                 WARN_ON(1);
2173                                 } else
2174                                         cm_id->add_ref(cm_id);
2175                         }
2176                 }
2177         } else {
2178                 cm_node->cm_id = NULL;
2179                 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2180                         rem_ref_cm_node(cm_core, cm_node);
2181                         rem_ref_cm_node(cm_core, loopback);
2182                 } else {
2183                         event.cm_node = loopback;
2184                         event.cm_info.rem_addr = loopback->rem_addr;
2185                         event.cm_info.loc_addr = loopback->loc_addr;
2186                         event.cm_info.rem_port = loopback->rem_port;
2187                         event.cm_info.loc_port = loopback->loc_port;
2188                         event.cm_info.cm_id = loopback->cm_id;
2189                         cm_event_mpa_reject(&event);
2190                         rem_ref_cm_node(cm_core, cm_node);
2191                         loopback->state = NES_CM_STATE_CLOSING;
2192
2193                         cm_id = loopback->cm_id;
2194                         rem_ref_cm_node(cm_core, loopback);
2195                         cm_id->rem_ref(cm_id);
2196                 }
2197         }
2198
2199         return ret;
2200 }
2201
2202
2203 /**
2204  * mini_cm_close
2205  */
2206 static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
2207 {
2208         int ret = 0;
2209
2210         if (!cm_core || !cm_node)
2211                 return -EINVAL;
2212
2213         switch (cm_node->state) {
2214         case NES_CM_STATE_SYN_RCVD:
2215         case NES_CM_STATE_SYN_SENT:
2216         case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2217         case NES_CM_STATE_ESTABLISHED:
2218         case NES_CM_STATE_ACCEPTING:
2219         case NES_CM_STATE_MPAREQ_SENT:
2220         case NES_CM_STATE_MPAREQ_RCVD:
2221                 cleanup_retrans_entry(cm_node);
2222                 send_reset(cm_node, NULL);
2223                 break;
2224         case NES_CM_STATE_CLOSE_WAIT:
2225                 cm_node->state = NES_CM_STATE_LAST_ACK;
2226                 send_fin(cm_node, NULL);
2227                 break;
2228         case NES_CM_STATE_FIN_WAIT1:
2229         case NES_CM_STATE_FIN_WAIT2:
2230         case NES_CM_STATE_LAST_ACK:
2231         case NES_CM_STATE_TIME_WAIT:
2232         case NES_CM_STATE_CLOSING:
2233                 ret = -1;
2234                 break;
2235         case NES_CM_STATE_LISTENING:
2236                 cleanup_retrans_entry(cm_node);
2237                 send_reset(cm_node, NULL);
2238                 break;
2239         case NES_CM_STATE_MPAREJ_RCVD:
2240         case NES_CM_STATE_UNKNOWN:
2241         case NES_CM_STATE_INITED:
2242         case NES_CM_STATE_CLOSED:
2243         case NES_CM_STATE_LISTENER_DESTROYED:
2244                 ret = rem_ref_cm_node(cm_core, cm_node);
2245                 break;
2246         case NES_CM_STATE_TSA:
2247                 if (cm_node->send_entry)
2248                         printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2249                                 "send_entry=%p\n", cm_node->send_entry);
2250                 ret = rem_ref_cm_node(cm_core, cm_node);
2251                 break;
2252         }
2253         return ret;
2254 }
2255
2256
2257 /**
2258  * recv_pkt - recv an ETHERNET packet, and process it through CM
2259  * node state machine
2260  */
2261 static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
2262         struct nes_vnic *nesvnic, struct sk_buff *skb)
2263 {
2264         struct nes_cm_node *cm_node = NULL;
2265         struct nes_cm_listener *listener = NULL;
2266         struct iphdr *iph;
2267         struct tcphdr *tcph;
2268         struct nes_cm_info nfo;
2269         int skb_handled = 1;
2270         __be32 tmp_daddr, tmp_saddr;
2271
2272         if (!skb)
2273                 return 0;
2274         if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
2275                 return 0;
2276         }
2277
2278         iph = (struct iphdr *)skb->data;
2279         tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
2280
2281         nfo.loc_addr = ntohl(iph->daddr);
2282         nfo.loc_port = ntohs(tcph->dest);
2283         nfo.rem_addr = ntohl(iph->saddr);
2284         nfo.rem_port = ntohs(tcph->source);
2285
2286         tmp_daddr = cpu_to_be32(iph->daddr);
2287         tmp_saddr = cpu_to_be32(iph->saddr);
2288
2289         nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
2290                   &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
2291
2292         do {
2293                 cm_node = find_node(cm_core,
2294                         nfo.rem_port, nfo.rem_addr,
2295                         nfo.loc_port, nfo.loc_addr);
2296
2297                 if (!cm_node) {
2298                         /* Only type of packet accepted are for */
2299                         /* the PASSIVE open (syn only) */
2300                         if ((!tcph->syn) || (tcph->ack)) {
2301                                 skb_handled = 0;
2302                                 break;
2303                         }
2304                         listener = find_listener(cm_core, nfo.loc_addr,
2305                                 nfo.loc_port,
2306                                 NES_CM_LISTENER_ACTIVE_STATE);
2307                         if (!listener) {
2308                                 nfo.cm_id = NULL;
2309                                 nfo.conn_type = 0;
2310                                 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2311                                 skb_handled = 0;
2312                                 break;
2313                         }
2314                         nfo.cm_id = listener->cm_id;
2315                         nfo.conn_type = listener->conn_type;
2316                         cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2317                                 listener);
2318                         if (!cm_node) {
2319                                 nes_debug(NES_DBG_CM, "Unable to allocate "
2320                                         "node\n");
2321                                 cm_packets_dropped++;
2322                                 atomic_dec(&listener->ref_count);
2323                                 dev_kfree_skb_any(skb);
2324                                 break;
2325                         }
2326                         if (!tcph->rst && !tcph->fin) {
2327                                 cm_node->state = NES_CM_STATE_LISTENING;
2328                         } else {
2329                                 cm_packets_dropped++;
2330                                 rem_ref_cm_node(cm_core, cm_node);
2331                                 dev_kfree_skb_any(skb);
2332                                 break;
2333                         }
2334                         add_ref_cm_node(cm_node);
2335                 } else if (cm_node->state == NES_CM_STATE_TSA) {
2336                         rem_ref_cm_node(cm_core, cm_node);
2337                         atomic_inc(&cm_accel_dropped_pkts);
2338                         dev_kfree_skb_any(skb);
2339                         break;
2340                 }
2341                 skb_reset_network_header(skb);
2342                 skb_set_transport_header(skb, sizeof(*tcph));
2343                 skb->len = ntohs(iph->tot_len);
2344                 process_packet(cm_node, skb, cm_core);
2345                 rem_ref_cm_node(cm_core, cm_node);
2346         } while (0);
2347         return skb_handled;
2348 }
2349
2350
2351 /**
2352  * nes_cm_alloc_core - allocate a top level instance of a cm core
2353  */
2354 static struct nes_cm_core *nes_cm_alloc_core(void)
2355 {
2356         struct nes_cm_core *cm_core;
2357
2358         /* setup the CM core */
2359         /* alloc top level core control structure */
2360         cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2361         if (!cm_core)
2362                 return NULL;
2363
2364         INIT_LIST_HEAD(&cm_core->connected_nodes);
2365         init_timer(&cm_core->tcp_timer);
2366         cm_core->tcp_timer.function = nes_cm_timer_tick;
2367
2368         cm_core->mtu   = NES_CM_DEFAULT_MTU;
2369         cm_core->state = NES_CM_STATE_INITED;
2370         cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2371
2372         atomic_set(&cm_core->events_posted, 0);
2373
2374         cm_core->api = &nes_cm_api;
2375
2376         spin_lock_init(&cm_core->ht_lock);
2377         spin_lock_init(&cm_core->listen_list_lock);
2378
2379         INIT_LIST_HEAD(&cm_core->listen_list.list);
2380
2381         nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2382
2383         nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2384         cm_core->event_wq = create_singlethread_workqueue("nesewq");
2385         cm_core->post_event = nes_cm_post_event;
2386         nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2387         cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2388
2389         print_core(cm_core);
2390         return cm_core;
2391 }
2392
2393
2394 /**
2395  * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2396  */
2397 static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
2398 {
2399         nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2400
2401         if (!cm_core)
2402                 return -EINVAL;
2403
2404         barrier();
2405
2406         if (timer_pending(&cm_core->tcp_timer)) {
2407                 del_timer(&cm_core->tcp_timer);
2408         }
2409
2410         destroy_workqueue(cm_core->event_wq);
2411         destroy_workqueue(cm_core->disconn_wq);
2412         nes_debug(NES_DBG_CM, "\n");
2413         kfree(cm_core);
2414
2415         return 0;
2416 }
2417
2418
2419 /**
2420  * mini_cm_get
2421  */
2422 static int mini_cm_get(struct nes_cm_core *cm_core)
2423 {
2424         return cm_core->state;
2425 }
2426
2427
2428 /**
2429  * mini_cm_set
2430  */
2431 static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
2432 {
2433         int ret = 0;
2434
2435         switch (type) {
2436         case NES_CM_SET_PKT_SIZE:
2437                 cm_core->mtu = value;
2438                 break;
2439         case NES_CM_SET_FREE_PKT_Q_SIZE:
2440                 cm_core->free_tx_pkt_max = value;
2441                 break;
2442         default:
2443                 /* unknown set option */
2444                 ret = -EINVAL;
2445         }
2446
2447         return ret;
2448 }
2449
2450
2451 /**
2452  * nes_cm_init_tsa_conn setup HW; MPA frames must be
2453  * successfully exchanged when this is called
2454  */
2455 static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2456 {
2457         int ret = 0;
2458
2459         if (!nesqp)
2460                 return -EINVAL;
2461
2462         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2463                         NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2464                         NES_QPCONTEXT_MISC_DROS);
2465
2466         if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2467                 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2468
2469         nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2470
2471         nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2472
2473         nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2474                         (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2475
2476         nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2477                         (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2478                         NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2479
2480         nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2481                         (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2482                         NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2483
2484         nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2485         nesqp->nesqp_context->ts_recent = 0;
2486         nesqp->nesqp_context->ts_age = 0;
2487         nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2488         nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2489         nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2490         nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2491                         cm_node->tcp_cntxt.rcv_wscale);
2492         nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2493         nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2494         nesqp->nesqp_context->srtt = 0;
2495         nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2496         nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2497         nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2498         nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2499         nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2500         nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2501
2502         nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2503                         " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2504                         nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2505                         le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2506                         cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2507                         le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2508                         le32_to_cpu(nesqp->nesqp_context->misc));
2509         nes_debug(NES_DBG_CM, "  snd_wnd  = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2510         nes_debug(NES_DBG_CM, "  snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2511         nes_debug(NES_DBG_CM, "  max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2512
2513         nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2514         cm_node->state = NES_CM_STATE_TSA;
2515
2516         return ret;
2517 }
2518
2519
2520 /**
2521  * nes_cm_disconn
2522  */
2523 int nes_cm_disconn(struct nes_qp *nesqp)
2524 {
2525         struct disconn_work *work;
2526
2527         work = kzalloc(sizeof *work, GFP_ATOMIC);
2528         if (!work)
2529                 return -ENOMEM; /* Timer will clean up */
2530
2531         nes_add_ref(&nesqp->ibqp);
2532         work->nesqp = nesqp;
2533         INIT_WORK(&work->work, nes_disconnect_worker);
2534         queue_work(g_cm_core->disconn_wq, &work->work);
2535         return 0;
2536 }
2537
2538
2539 /**
2540  * nes_disconnect_worker
2541  */
2542 static void nes_disconnect_worker(struct work_struct *work)
2543 {
2544         struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2545         struct nes_qp *nesqp = dwork->nesqp;
2546
2547         kfree(dwork);
2548         nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2549                         nesqp->last_aeq, nesqp->hwqp.qp_id);
2550         nes_cm_disconn_true(nesqp);
2551         nes_rem_ref(&nesqp->ibqp);
2552 }
2553
2554
2555 /**
2556  * nes_cm_disconn_true
2557  */
2558 static int nes_cm_disconn_true(struct nes_qp *nesqp)
2559 {
2560         unsigned long flags;
2561         int ret = 0;
2562         struct iw_cm_id *cm_id;
2563         struct iw_cm_event cm_event;
2564         struct nes_vnic *nesvnic;
2565         u16 last_ae;
2566         u8 original_hw_tcp_state;
2567         u8 original_ibqp_state;
2568         enum iw_cm_event_type disconn_status = IW_CM_EVENT_STATUS_OK;
2569         int issue_disconn = 0;
2570         int issue_close = 0;
2571         int issue_flush = 0;
2572         u32 flush_q = NES_CQP_FLUSH_RQ;
2573         struct ib_event ibevent;
2574
2575         if (!nesqp) {
2576                 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2577                 return -1;
2578         }
2579
2580         spin_lock_irqsave(&nesqp->lock, flags);
2581         cm_id = nesqp->cm_id;
2582         /* make sure we havent already closed this connection */
2583         if (!cm_id) {
2584                 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2585                                 nesqp->hwqp.qp_id);
2586                 spin_unlock_irqrestore(&nesqp->lock, flags);
2587                 return -1;
2588         }
2589
2590         nesvnic = to_nesvnic(nesqp->ibqp.device);
2591         nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2592
2593         original_hw_tcp_state = nesqp->hw_tcp_state;
2594         original_ibqp_state   = nesqp->ibqp_state;
2595         last_ae = nesqp->last_aeq;
2596
2597         if (nesqp->term_flags) {
2598                 issue_disconn = 1;
2599                 issue_close = 1;
2600                 nesqp->cm_id = NULL;
2601                 if (nesqp->flush_issued == 0) {
2602                         nesqp->flush_issued = 1;
2603                         issue_flush = 1;
2604                 }
2605         } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2606                         ((original_ibqp_state == IB_QPS_RTS) &&
2607                         (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2608                 issue_disconn = 1;
2609                 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
2610                         disconn_status = IW_CM_EVENT_STATUS_RESET;
2611         }
2612
2613         if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2614                  (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2615                  (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2616                  (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2617                 issue_close = 1;
2618                 nesqp->cm_id = NULL;
2619                 if (nesqp->flush_issued == 0) {
2620                         nesqp->flush_issued = 1;
2621                         issue_flush = 1;
2622                 }
2623         }
2624
2625         spin_unlock_irqrestore(&nesqp->lock, flags);
2626
2627         if ((issue_flush) && (nesqp->destroyed == 0)) {
2628                 /* Flush the queue(s) */
2629                 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2630                         flush_q |= NES_CQP_FLUSH_SQ;
2631                 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2632
2633                 if (nesqp->term_flags) {
2634                         ibevent.device = nesqp->ibqp.device;
2635                         ibevent.event = nesqp->terminate_eventtype;
2636                         ibevent.element.qp = &nesqp->ibqp;
2637                         nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
2638                 }
2639         }
2640
2641         if ((cm_id) && (cm_id->event_handler)) {
2642                 if (issue_disconn) {
2643                         atomic_inc(&cm_disconnects);
2644                         cm_event.event = IW_CM_EVENT_DISCONNECT;
2645                         cm_event.status = disconn_status;
2646                         cm_event.local_addr = cm_id->local_addr;
2647                         cm_event.remote_addr = cm_id->remote_addr;
2648                         cm_event.private_data = NULL;
2649                         cm_event.private_data_len = 0;
2650
2651                         nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2652                                 " for  QP%u, SQ Head = %u, SQ Tail = %u. "
2653                                 "cm_id = %p, refcount = %u.\n",
2654                                 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2655                                 nesqp->hwqp.sq_tail, cm_id,
2656                                 atomic_read(&nesqp->refcount));
2657
2658                         ret = cm_id->event_handler(cm_id, &cm_event);
2659                         if (ret)
2660                                 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2661                                         "returned, ret=%d\n", ret);
2662                 }
2663
2664                 if (issue_close) {
2665                         atomic_inc(&cm_closes);
2666                         nes_disconnect(nesqp, 1);
2667
2668                         cm_id->provider_data = nesqp;
2669                         /* Send up the close complete event */
2670                         cm_event.event = IW_CM_EVENT_CLOSE;
2671                         cm_event.status = IW_CM_EVENT_STATUS_OK;
2672                         cm_event.provider_data = cm_id->provider_data;
2673                         cm_event.local_addr = cm_id->local_addr;
2674                         cm_event.remote_addr = cm_id->remote_addr;
2675                         cm_event.private_data = NULL;
2676                         cm_event.private_data_len = 0;
2677
2678                         ret = cm_id->event_handler(cm_id, &cm_event);
2679                         if (ret) {
2680                                 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2681                         }
2682
2683                         cm_id->rem_ref(cm_id);
2684                 }
2685         }
2686
2687         return 0;
2688 }
2689
2690
2691 /**
2692  * nes_disconnect
2693  */
2694 static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
2695 {
2696         int ret = 0;
2697         struct nes_vnic *nesvnic;
2698         struct nes_device *nesdev;
2699         struct nes_ib_device *nesibdev;
2700
2701         nesvnic = to_nesvnic(nesqp->ibqp.device);
2702         if (!nesvnic)
2703                 return -EINVAL;
2704
2705         nesdev = nesvnic->nesdev;
2706         nesibdev = nesvnic->nesibdev;
2707
2708         nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2709                         atomic_read(&nesvnic->netdev->refcnt));
2710
2711         if (nesqp->active_conn) {
2712
2713                 /* indicate this connection is NOT active */
2714                 nesqp->active_conn = 0;
2715         } else {
2716                 /* Need to free the Last Streaming Mode Message */
2717                 if (nesqp->ietf_frame) {
2718                         if (nesqp->lsmm_mr)
2719                                 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
2720                         pci_free_consistent(nesdev->pcidev,
2721                                         nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2722                                         nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2723                 }
2724         }
2725
2726         /* close the CM node down if it is still active */
2727         if (nesqp->cm_node) {
2728                 nes_debug(NES_DBG_CM, "Call close API\n");
2729
2730                 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
2731         }
2732
2733         return ret;
2734 }
2735
2736
2737 /**
2738  * nes_accept
2739  */
2740 int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2741 {
2742         u64 u64temp;
2743         struct ib_qp *ibqp;
2744         struct nes_qp *nesqp;
2745         struct nes_vnic *nesvnic;
2746         struct nes_device *nesdev;
2747         struct nes_cm_node *cm_node;
2748         struct nes_adapter *adapter;
2749         struct ib_qp_attr attr;
2750         struct iw_cm_event cm_event;
2751         struct nes_hw_qp_wqe *wqe;
2752         struct nes_v4_quad nes_quad;
2753         u32 crc_value;
2754         int ret;
2755         int passive_state;
2756         struct nes_ib_device *nesibdev;
2757         struct ib_mr *ibmr = NULL;
2758         struct ib_phys_buf ibphysbuf;
2759         struct nes_pd *nespd;
2760         u64 tagged_offset;
2761
2762         ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2763         if (!ibqp)
2764                 return -EINVAL;
2765
2766         /* get all our handles */
2767         nesqp = to_nesqp(ibqp);
2768         nesvnic = to_nesvnic(nesqp->ibqp.device);
2769         nesdev = nesvnic->nesdev;
2770         adapter = nesdev->nesadapter;
2771
2772         cm_node = (struct nes_cm_node *)cm_id->provider_data;
2773         nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2774                 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2775                 nesvnic->netdev->name);
2776
2777         if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
2778                 if (cm_node->loopbackpartner)
2779                         rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
2780                 rem_ref_cm_node(cm_node->cm_core, cm_node);
2781                 return -EINVAL;
2782         }
2783
2784         /* associate the node with the QP */
2785         nesqp->cm_node = (void *)cm_node;
2786         cm_node->nesqp = nesqp;
2787
2788         nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2789                 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
2790         atomic_inc(&cm_accepts);
2791
2792         nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2793                         atomic_read(&nesvnic->netdev->refcnt));
2794
2795         /* allocate the ietf frame and space for private data */
2796         nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2797                 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2798                 &nesqp->ietf_frame_pbase);
2799
2800         if (!nesqp->ietf_frame) {
2801                 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2802                         "data\n");
2803                 return -ENOMEM;
2804         }
2805
2806
2807         /* setup the MPA frame */
2808         nesqp->private_data_len = conn_param->private_data_len;
2809         memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
2810
2811         memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2812                         conn_param->private_data_len);
2813
2814         nesqp->ietf_frame->priv_data_len =
2815                 cpu_to_be16(conn_param->private_data_len);
2816         nesqp->ietf_frame->rev = mpa_version;
2817         nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
2818
2819         /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2820         wqe = &nesqp->hwqp.sq_vbase[0];
2821
2822         if (cm_id->remote_addr.sin_addr.s_addr !=
2823                         cm_id->local_addr.sin_addr.s_addr) {
2824                 u64temp = (unsigned long)nesqp;
2825                 nesibdev = nesvnic->nesibdev;
2826                 nespd = nesqp->nespd;
2827                 ibphysbuf.addr = nesqp->ietf_frame_pbase;
2828                 ibphysbuf.size = conn_param->private_data_len +
2829                                         sizeof(struct ietf_mpa_frame);
2830                 tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
2831                 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
2832                                                 &ibphysbuf, 1,
2833                                                 IB_ACCESS_LOCAL_WRITE,
2834                                                 &tagged_offset);
2835                 if (!ibmr) {
2836                         nes_debug(NES_DBG_CM, "Unable to register memory region"
2837                                         "for lSMM for cm_node = %p \n",
2838                                         cm_node);
2839                         pci_free_consistent(nesdev->pcidev,
2840                                 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2841                                 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2842                         return -ENOMEM;
2843                 }
2844
2845                 ibmr->pd = &nespd->ibpd;
2846                 ibmr->device = nespd->ibpd.device;
2847                 nesqp->lsmm_mr = ibmr;
2848
2849                 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2850                 set_wqe_64bit_value(wqe->wqe_words,
2851                         NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2852                         u64temp);
2853                 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2854                         cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2855                         NES_IWARP_SQ_WQE_WRPDU);
2856                 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2857                         cpu_to_le32(conn_param->private_data_len +
2858                         sizeof(struct ietf_mpa_frame));
2859                 set_wqe_64bit_value(wqe->wqe_words,
2860                                         NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
2861                                         (u64)(unsigned long)nesqp->ietf_frame);
2862                 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2863                         cpu_to_le32(conn_param->private_data_len +
2864                         sizeof(struct ietf_mpa_frame));
2865                 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
2866                 if (nesqp->sq_kmapped) {
2867                         nesqp->sq_kmapped = 0;
2868                         kunmap(nesqp->page);
2869                 }
2870
2871                 nesqp->nesqp_context->ird_ord_sizes |=
2872                         cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2873                         NES_QPCONTEXT_ORDIRD_WRPDU);
2874         } else {
2875                 nesqp->nesqp_context->ird_ord_sizes |=
2876                         cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
2877         }
2878         nesqp->skip_lsmm = 1;
2879
2880
2881         /* Cache the cm_id in the qp */
2882         nesqp->cm_id = cm_id;
2883         cm_node->cm_id = cm_id;
2884
2885         /*  nesqp->cm_node = (void *)cm_id->provider_data; */
2886         cm_id->provider_data = nesqp;
2887         nesqp->active_conn   = 0;
2888
2889         if (cm_node->state == NES_CM_STATE_TSA)
2890                 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2891                         cm_node);
2892
2893         nes_cm_init_tsa_conn(nesqp, cm_node);
2894
2895         nesqp->nesqp_context->tcpPorts[0] =
2896                 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2897         nesqp->nesqp_context->tcpPorts[1] =
2898                 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2899
2900         if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2901                 nesqp->nesqp_context->ip0 =
2902                         cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2903         else
2904                 nesqp->nesqp_context->ip0 =
2905                         cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
2906
2907         nesqp->nesqp_context->misc2 |= cpu_to_le32(
2908                         (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2909                         NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
2910
2911         nesqp->nesqp_context->arp_index_vlan |=
2912                 cpu_to_le32(nes_arp_table(nesdev,
2913                         le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
2914                         NES_ARP_RESOLVE) << 16);
2915
2916         nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
2917                 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
2918
2919         nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2920
2921         nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
2922                 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2923         nesqp->nesqp_context->ird_ord_sizes |=
2924                 cpu_to_le32((u32)conn_param->ord);
2925
2926         memset(&nes_quad, 0, sizeof(nes_quad));
2927         nes_quad.DstIpAdrIndex =
2928                 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2929         if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2930                 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2931         else
2932                 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2933         nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2934         nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
2935
2936         /* Produce hash key */
2937         crc_value = get_crc_value(&nes_quad);
2938         nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
2939         nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
2940                 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
2941
2942         nesqp->hte_index &= adapter->hte_index_mask;
2943         nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2944
2945         cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2946
2947         nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2948                         "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2949                         "private data length=%zu.\n", nesqp->hwqp.qp_id,
2950                         ntohl(cm_id->remote_addr.sin_addr.s_addr),
2951                         ntohs(cm_id->remote_addr.sin_port),
2952                         ntohl(cm_id->local_addr.sin_addr.s_addr),
2953                         ntohs(cm_id->local_addr.sin_port),
2954                         le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2955                         le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2956                         conn_param->private_data_len +
2957                         sizeof(struct ietf_mpa_frame));
2958
2959
2960         /* notify OF layer that accept event was successful */
2961         cm_id->add_ref(cm_id);
2962         nes_add_ref(&nesqp->ibqp);
2963
2964         cm_event.event = IW_CM_EVENT_ESTABLISHED;
2965         cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2966         cm_event.provider_data = (void *)nesqp;
2967         cm_event.local_addr = cm_id->local_addr;
2968         cm_event.remote_addr = cm_id->remote_addr;
2969         cm_event.private_data = NULL;
2970         cm_event.private_data_len = 0;
2971         ret = cm_id->event_handler(cm_id, &cm_event);
2972         attr.qp_state = IB_QPS_RTS;
2973         nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
2974         if (cm_node->loopbackpartner) {
2975                 cm_node->loopbackpartner->mpa_frame_size =
2976                         nesqp->private_data_len;
2977                 /* copy entire MPA frame to our cm_node's frame */
2978                 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2979                         nesqp->ietf_frame->priv_data, nesqp->private_data_len);
2980                 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2981         }
2982         if (ret)
2983                 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2984                         "ret=%d\n", __func__, __LINE__, ret);
2985
2986         passive_state = atomic_add_return(1, &cm_node->passive_state);
2987         if (passive_state == NES_SEND_RESET_EVENT)
2988                 create_event(cm_node, NES_CM_EVENT_RESET);
2989         return 0;
2990 }
2991
2992
2993 /**
2994  * nes_reject
2995  */
2996 int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2997 {
2998         struct nes_cm_node *cm_node;
2999         struct nes_cm_node *loopback;
3000
3001         struct nes_cm_core *cm_core;
3002
3003         atomic_inc(&cm_rejects);
3004         cm_node = (struct nes_cm_node *) cm_id->provider_data;
3005         loopback = cm_node->loopbackpartner;
3006         cm_core = cm_node->cm_core;
3007         cm_node->cm_id = cm_id;
3008         cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
3009
3010         if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
3011                 return -EINVAL;
3012
3013         memcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
3014         if (loopback) {
3015                 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3016                 loopback->mpa_frame.priv_data_len = pdata_len;
3017                 loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
3018                                 pdata_len;
3019         } else {
3020                 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
3021                 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
3022         }
3023
3024         cm_node->mpa_frame.rev = mpa_version;
3025         cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
3026
3027         return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
3028 }
3029
3030
3031 /**
3032  * nes_connect
3033  * setup and launch cm connect node
3034  */
3035 int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3036 {
3037         struct ib_qp *ibqp;
3038         struct nes_qp *nesqp;
3039         struct nes_vnic *nesvnic;
3040         struct nes_device *nesdev;
3041         struct nes_cm_node *cm_node;
3042         struct nes_cm_info cm_info;
3043         int apbvt_set = 0;
3044
3045         ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3046         if (!ibqp)
3047                 return -EINVAL;
3048         nesqp = to_nesqp(ibqp);
3049         if (!nesqp)
3050                 return -EINVAL;
3051         nesvnic = to_nesvnic(nesqp->ibqp.device);
3052         if (!nesvnic)
3053                 return -EINVAL;
3054         nesdev  = nesvnic->nesdev;
3055         if (!nesdev)
3056                 return -EINVAL;
3057
3058         if (!(cm_id->local_addr.sin_port) || !(cm_id->remote_addr.sin_port))
3059                 return -EINVAL;
3060
3061         nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
3062                 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3063                 ntohl(nesvnic->local_ipaddr),
3064                 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3065                 ntohs(cm_id->remote_addr.sin_port),
3066                 ntohl(cm_id->local_addr.sin_addr.s_addr),
3067                 ntohs(cm_id->local_addr.sin_port));
3068
3069         atomic_inc(&cm_connects);
3070         nesqp->active_conn = 1;
3071
3072         /* cache the cm_id in the qp */
3073         nesqp->cm_id = cm_id;
3074
3075         cm_id->provider_data = nesqp;
3076
3077         nesqp->private_data_len = conn_param->private_data_len;
3078         nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
3079         nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
3080         nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
3081                 conn_param->private_data_len);
3082
3083         if (cm_id->local_addr.sin_addr.s_addr !=
3084                 cm_id->remote_addr.sin_addr.s_addr) {
3085                 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
3086                         PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
3087                 apbvt_set = 1;
3088         }
3089
3090         /* set up the connection params for the node */
3091         cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3092         cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3093         cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3094         cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
3095         cm_info.cm_id = cm_id;
3096         cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3097
3098         cm_id->add_ref(cm_id);
3099
3100         /* create a connect CM node connection */
3101         cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3102                 conn_param->private_data_len, (void *)conn_param->private_data,
3103                 &cm_info);
3104         if (!cm_node) {
3105                 if (apbvt_set)
3106                         nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
3107                                 PCI_FUNC(nesdev->pcidev->devfn),
3108                                 NES_MANAGE_APBVT_DEL);
3109
3110                 cm_id->rem_ref(cm_id);
3111                 return -ENOMEM;
3112         }
3113
3114         cm_node->apbvt_set = apbvt_set;
3115         nesqp->cm_node = cm_node;
3116         cm_node->nesqp = nesqp;
3117         nes_add_ref(&nesqp->ibqp);
3118
3119         return 0;
3120 }
3121
3122
3123 /**
3124  * nes_create_listen
3125  */
3126 int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3127 {
3128         struct nes_vnic *nesvnic;
3129         struct nes_cm_listener *cm_node;
3130         struct nes_cm_info cm_info;
3131         struct nes_adapter *adapter;
3132         int err;
3133
3134
3135         nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3136                         cm_id, ntohs(cm_id->local_addr.sin_port));
3137
3138         nesvnic = to_nesvnic(cm_id->device);
3139         if (!nesvnic)
3140                 return -EINVAL;
3141         adapter = nesvnic->nesdev->nesadapter;
3142         nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3143                         nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3144
3145         nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3146                         nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3147
3148         /* setup listen params in our api call struct */
3149         cm_info.loc_addr = nesvnic->local_ipaddr;
3150         cm_info.loc_port = cm_id->local_addr.sin_port;
3151         cm_info.backlog = backlog;
3152         cm_info.cm_id = cm_id;
3153
3154         cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3155
3156
3157         cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3158         if (!cm_node) {
3159                 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
3160                                 __func__, __LINE__);
3161                 return -ENOMEM;
3162         }
3163
3164         cm_id->provider_data = cm_node;
3165
3166         if (!cm_node->reused_node) {
3167                 err = nes_manage_apbvt(nesvnic,
3168                         ntohs(cm_id->local_addr.sin_port),
3169                         PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3170                         NES_MANAGE_APBVT_ADD);
3171                 if (err) {
3172                         printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3173                                 err);
3174                         g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3175                         return err;
3176                 }
3177                 atomic_inc(&cm_listens_created);
3178         }
3179
3180         cm_id->add_ref(cm_id);
3181         cm_id->provider_data = (void *)cm_node;
3182
3183
3184         return 0;
3185 }
3186
3187
3188 /**
3189  * nes_destroy_listen
3190  */
3191 int nes_destroy_listen(struct iw_cm_id *cm_id)
3192 {
3193         if (cm_id->provider_data)
3194                 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3195         else
3196                 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3197
3198         cm_id->rem_ref(cm_id);
3199
3200         return 0;
3201 }
3202
3203
3204 /**
3205  * nes_cm_recv
3206  */
3207 int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3208 {
3209         int rc = 0;
3210         cm_packets_received++;
3211         if ((g_cm_core) && (g_cm_core->api)) {
3212                 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
3213         } else {
3214                 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3215                                 " cm is not setup properly.\n");
3216         }
3217
3218         return rc;
3219 }
3220
3221
3222 /**
3223  * nes_cm_start
3224  * Start and init a cm core module
3225  */
3226 int nes_cm_start(void)
3227 {
3228         nes_debug(NES_DBG_CM, "\n");
3229         /* create the primary CM core, pass this handle to subsequent core inits */
3230         g_cm_core = nes_cm_alloc_core();
3231         if (g_cm_core) {
3232                 return 0;
3233         } else {
3234                 return -ENOMEM;
3235         }
3236 }
3237
3238
3239 /**
3240  * nes_cm_stop
3241  * stop and dealloc all cm core instances
3242  */
3243 int nes_cm_stop(void)
3244 {
3245         g_cm_core->api->destroy_cm_core(g_cm_core);
3246         return 0;
3247 }
3248
3249
3250 /**
3251  * cm_event_connected
3252  * handle a connected event, setup QPs and HW
3253  */
3254 static void cm_event_connected(struct nes_cm_event *event)
3255 {
3256         u64 u64temp;
3257         struct nes_qp *nesqp;
3258         struct nes_vnic *nesvnic;
3259         struct nes_device *nesdev;
3260         struct nes_cm_node *cm_node;
3261         struct nes_adapter *nesadapter;
3262         struct ib_qp_attr attr;
3263         struct iw_cm_id *cm_id;
3264         struct iw_cm_event cm_event;
3265         struct nes_hw_qp_wqe *wqe;
3266         struct nes_v4_quad nes_quad;
3267         u32 crc_value;
3268         int ret;
3269
3270         /* get all our handles */
3271         cm_node = event->cm_node;
3272         cm_id = cm_node->cm_id;
3273         nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3274         nesqp = (struct nes_qp *)cm_id->provider_data;
3275         nesvnic = to_nesvnic(nesqp->ibqp.device);
3276         nesdev = nesvnic->nesdev;
3277         nesadapter = nesdev->nesadapter;
3278
3279         if (nesqp->destroyed) {
3280                 return;
3281         }
3282         atomic_inc(&cm_connecteds);
3283         nes_debug(NES_DBG_CM, "QP%u attempting to connect to  0x%08X:0x%04X on"
3284                         " local port 0x%04X. jiffies = %lu.\n",
3285                         nesqp->hwqp.qp_id,
3286                         ntohl(cm_id->remote_addr.sin_addr.s_addr),
3287                         ntohs(cm_id->remote_addr.sin_port),
3288                         ntohs(cm_id->local_addr.sin_port),
3289                         jiffies);
3290
3291         nes_cm_init_tsa_conn(nesqp, cm_node);
3292
3293         /* set the QP tsa context */
3294         nesqp->nesqp_context->tcpPorts[0] =
3295                 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3296         nesqp->nesqp_context->tcpPorts[1] =
3297                 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3298         if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3299                 nesqp->nesqp_context->ip0 =
3300                         cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3301         else
3302                 nesqp->nesqp_context->ip0 =
3303                         cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
3304
3305         nesqp->nesqp_context->misc2 |= cpu_to_le32(
3306                         (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3307                         NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
3308         nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
3309                         nes_arp_table(nesdev,
3310                         le32_to_cpu(nesqp->nesqp_context->ip0),
3311                         NULL, NES_ARP_RESOLVE) << 16);
3312         nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3313                         jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3314         nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3315         nesqp->nesqp_context->ird_ord_sizes |=
3316                         cpu_to_le32((u32)1 <<
3317                         NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
3318
3319         /* Adjust tail for not having a LSMM */
3320         nesqp->hwqp.sq_tail = 1;
3321
3322 #if defined(NES_SEND_FIRST_WRITE)
3323         if (cm_node->send_write0) {
3324                 nes_debug(NES_DBG_CM, "Sending first write.\n");
3325                 wqe = &nesqp->hwqp.sq_vbase[0];
3326                 u64temp = (unsigned long)nesqp;
3327                 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3328                 set_wqe_64bit_value(wqe->wqe_words,
3329                                 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3330                 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3331                         cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3332                 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3333                 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3334                 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3335                 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3336                 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
3337
3338                 if (nesqp->sq_kmapped) {
3339                         nesqp->sq_kmapped = 0;
3340                         kunmap(nesqp->page);
3341                 }
3342
3343                 /* use the reserved spot on the WQ for the extra first WQE */
3344                 nesqp->nesqp_context->ird_ord_sizes &=
3345                         cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3346                                                 NES_QPCONTEXT_ORDIRD_WRPDU |
3347                                                 NES_QPCONTEXT_ORDIRD_ALSMM));
3348                 nesqp->skip_lsmm = 1;
3349                 nesqp->hwqp.sq_tail = 0;
3350                 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3351                                 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3352         }
3353 #endif
3354
3355         memset(&nes_quad, 0, sizeof(nes_quad));
3356
3357         nes_quad.DstIpAdrIndex =
3358                 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3359         if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3360                 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3361         else
3362                 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
3363         nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3364         nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3365
3366         /* Produce hash key */
3367         crc_value = get_crc_value(&nes_quad);
3368         nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
3369         nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3370                         nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3371
3372         nesqp->hte_index &= nesadapter->hte_index_mask;
3373         nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3374
3375         nesqp->ietf_frame = &cm_node->mpa_frame;
3376         nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3377         cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3378
3379         /* notify OF layer we successfully created the requested connection */
3380         cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3381         cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3382         cm_event.provider_data = cm_id->provider_data;
3383         cm_event.local_addr.sin_family = AF_INET;
3384         cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3385         cm_event.remote_addr = cm_id->remote_addr;
3386
3387         cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3388         cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
3389
3390         cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3391         ret = cm_id->event_handler(cm_id, &cm_event);
3392         nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3393
3394         if (ret)
3395                 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3396                         "ret=%d\n", __func__, __LINE__, ret);
3397         attr.qp_state = IB_QPS_RTS;
3398         nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
3399
3400         nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3401                 "%lu\n", nesqp->hwqp.qp_id, jiffies);
3402
3403         return;
3404 }
3405
3406
3407 /**
3408  * cm_event_connect_error
3409  */
3410 static void cm_event_connect_error(struct nes_cm_event *event)
3411 {
3412         struct nes_qp *nesqp;
3413         struct iw_cm_id *cm_id;
3414         struct iw_cm_event cm_event;
3415         /* struct nes_cm_info cm_info; */
3416         int ret;
3417
3418         if (!event->cm_node)
3419                 return;
3420
3421         cm_id = event->cm_node->cm_id;
3422         if (!cm_id) {
3423                 return;
3424         }
3425
3426         nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3427         nesqp = cm_id->provider_data;
3428
3429         if (!nesqp) {
3430                 return;
3431         }
3432
3433         /* notify OF layer about this connection error event */
3434         /* cm_id->rem_ref(cm_id); */
3435         nesqp->cm_id = NULL;
3436         cm_id->provider_data = NULL;
3437         cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3438         cm_event.status = -ECONNRESET;
3439         cm_event.provider_data = cm_id->provider_data;
3440         cm_event.local_addr = cm_id->local_addr;
3441         cm_event.remote_addr = cm_id->remote_addr;
3442         cm_event.private_data = NULL;
3443         cm_event.private_data_len = 0;
3444
3445         nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3446                 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3447                 cm_event.remote_addr.sin_addr.s_addr);
3448
3449         ret = cm_id->event_handler(cm_id, &cm_event);
3450         nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3451         if (ret)
3452                 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3453                         "ret=%d\n", __func__, __LINE__, ret);
3454         cm_id->rem_ref(cm_id);
3455
3456         rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
3457         return;
3458 }
3459
3460
3461 /**
3462  * cm_event_reset
3463  */
3464 static void cm_event_reset(struct nes_cm_event *event)
3465 {
3466         struct nes_qp *nesqp;
3467         struct iw_cm_id *cm_id;
3468         struct iw_cm_event cm_event;
3469         /* struct nes_cm_info cm_info; */
3470         int ret;
3471
3472         if (!event->cm_node)
3473                 return;
3474
3475         if (!event->cm_node->cm_id)
3476                 return;
3477
3478         cm_id = event->cm_node->cm_id;
3479
3480         nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3481         nesqp = cm_id->provider_data;
3482         if (!nesqp)
3483                 return;
3484
3485         nesqp->cm_id = NULL;
3486         /* cm_id->provider_data = NULL; */
3487         cm_event.event = IW_CM_EVENT_DISCONNECT;
3488         cm_event.status = IW_CM_EVENT_STATUS_RESET;
3489         cm_event.provider_data = cm_id->provider_data;
3490         cm_event.local_addr = cm_id->local_addr;
3491         cm_event.remote_addr = cm_id->remote_addr;
3492         cm_event.private_data = NULL;
3493         cm_event.private_data_len = 0;
3494
3495         cm_id->add_ref(cm_id);
3496         ret = cm_id->event_handler(cm_id, &cm_event);
3497         atomic_inc(&cm_closes);
3498         cm_event.event = IW_CM_EVENT_CLOSE;
3499         cm_event.status = IW_CM_EVENT_STATUS_OK;
3500         cm_event.provider_data = cm_id->provider_data;
3501         cm_event.local_addr = cm_id->local_addr;
3502         cm_event.remote_addr = cm_id->remote_addr;
3503         cm_event.private_data = NULL;
3504         cm_event.private_data_len = 0;
3505         nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3506         ret = cm_id->event_handler(cm_id, &cm_event);
3507
3508         nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3509
3510
3511         /* notify OF layer about this connection error event */
3512         cm_id->rem_ref(cm_id);
3513
3514         return;
3515 }
3516
3517
3518 /**
3519  * cm_event_mpa_req
3520  */
3521 static void cm_event_mpa_req(struct nes_cm_event *event)
3522 {
3523         struct iw_cm_id   *cm_id;
3524         struct iw_cm_event cm_event;
3525         int ret;
3526         struct nes_cm_node *cm_node;
3527
3528         cm_node = event->cm_node;
3529         if (!cm_node)
3530                 return;
3531         cm_id = cm_node->cm_id;
3532
3533         atomic_inc(&cm_connect_reqs);
3534         nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3535                         cm_node, cm_id, jiffies);
3536
3537         cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3538         cm_event.status = IW_CM_EVENT_STATUS_OK;
3539         cm_event.provider_data = (void *)cm_node;
3540
3541         cm_event.local_addr.sin_family = AF_INET;
3542         cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3543         cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3544
3545         cm_event.remote_addr.sin_family = AF_INET;
3546         cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3547         cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3548         cm_event.private_data = cm_node->mpa_frame_buf;
3549         cm_event.private_data_len  = (u8) cm_node->mpa_frame_size;
3550
3551         ret = cm_id->event_handler(cm_id, &cm_event);
3552         if (ret)
3553                 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3554                                 __func__, __LINE__, ret);
3555         return;
3556 }
3557
3558
3559 static void cm_event_mpa_reject(struct nes_cm_event *event)
3560 {
3561         struct iw_cm_id   *cm_id;
3562         struct iw_cm_event cm_event;
3563         struct nes_cm_node *cm_node;
3564         int ret;
3565
3566         cm_node = event->cm_node;
3567         if (!cm_node)
3568                 return;
3569         cm_id = cm_node->cm_id;
3570
3571         atomic_inc(&cm_connect_reqs);
3572         nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3573                         cm_node, cm_id, jiffies);
3574
3575         cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3576         cm_event.status = -ECONNREFUSED;
3577         cm_event.provider_data = cm_id->provider_data;
3578
3579         cm_event.local_addr.sin_family = AF_INET;
3580         cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3581         cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3582
3583         cm_event.remote_addr.sin_family = AF_INET;
3584         cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3585         cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3586
3587         cm_event.private_data = cm_node->mpa_frame_buf;
3588         cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3589
3590         nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3591                         "remove_addr=%08x\n",
3592                         cm_event.local_addr.sin_addr.s_addr,
3593                         cm_event.remote_addr.sin_addr.s_addr);
3594
3595         ret = cm_id->event_handler(cm_id, &cm_event);
3596         if (ret)
3597                 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3598                                 __func__, __LINE__, ret);
3599
3600         return;
3601 }
3602
3603
3604 static void nes_cm_event_handler(struct work_struct *);
3605
3606 /**
3607  * nes_cm_post_event
3608  * post an event to the cm event handler
3609  */
3610 static int nes_cm_post_event(struct nes_cm_event *event)
3611 {
3612         atomic_inc(&event->cm_node->cm_core->events_posted);
3613         add_ref_cm_node(event->cm_node);
3614         event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3615         INIT_WORK(&event->event_work, nes_cm_event_handler);
3616         nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3617                 event->cm_node, event);
3618
3619         queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3620
3621         nes_debug(NES_DBG_CM, "Exit\n");
3622         return 0;
3623 }
3624
3625
3626 /**
3627  * nes_cm_event_handler
3628  * worker function to handle cm events
3629  * will free instance of nes_cm_event
3630  */
3631 static void nes_cm_event_handler(struct work_struct *work)
3632 {
3633         struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3634                         event_work);
3635         struct nes_cm_core *cm_core;
3636
3637         if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
3638                 return;
3639
3640         cm_core = event->cm_node->cm_core;
3641         nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
3642                 event, event->type, atomic_read(&cm_core->events_posted));
3643
3644         switch (event->type) {
3645         case NES_CM_EVENT_MPA_REQ:
3646                 cm_event_mpa_req(event);
3647                 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3648                         event->cm_node);
3649                 break;
3650         case NES_CM_EVENT_RESET:
3651                 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3652                         event->cm_node);
3653                 cm_event_reset(event);
3654                 break;
3655         case NES_CM_EVENT_CONNECTED:
3656                 if ((!event->cm_node->cm_id) ||
3657                         (event->cm_node->state != NES_CM_STATE_TSA))
3658                         break;
3659                 cm_event_connected(event);
3660                 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3661                 break;
3662         case NES_CM_EVENT_MPA_REJECT:
3663                 if ((!event->cm_node->cm_id) ||
3664                                 (event->cm_node->state == NES_CM_STATE_TSA))
3665                         break;
3666                 cm_event_mpa_reject(event);
3667                 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3668                 break;
3669
3670         case NES_CM_EVENT_ABORTED:
3671                 if ((!event->cm_node->cm_id) ||
3672                         (event->cm_node->state == NES_CM_STATE_TSA))
3673                         break;
3674                 cm_event_connect_error(event);
3675                 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3676                 break;
3677         case NES_CM_EVENT_DROPPED_PKT:
3678                 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3679                 break;
3680         default:
3681                 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3682                 break;
3683         }
3684
3685         atomic_dec(&cm_core->events_posted);
3686         event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3687         rem_ref_cm_node(cm_core, event->cm_node);
3688         kfree(event);
3689
3690         return;
3691 }