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