Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[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         __be32 tmp_addr = cpu_to_be32(loc_addr);
782
783         /* get a handle on the hte */
784         hte = &cm_core->connected_nodes;
785
786         nes_debug(NES_DBG_CM, "Searching for an owner node: %pI4:%x from core %p->%p\n",
787                   &tmp_addr, loc_port, cm_core, hte);
788
789         /* walk list and find cm_node associated with this session ID */
790         spin_lock_irqsave(&cm_core->ht_lock, flags);
791         list_for_each_entry(cm_node, hte, list) {
792                 /* compare quad, return node handle if a match */
793                 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
794                                 cm_node->loc_addr, cm_node->loc_port,
795                                 loc_addr, loc_port,
796                                 cm_node->rem_addr, cm_node->rem_port,
797                                 rem_addr, rem_port);
798                 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
799                                 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
800                         add_ref_cm_node(cm_node);
801                         spin_unlock_irqrestore(&cm_core->ht_lock, flags);
802                         return cm_node;
803                 }
804         }
805         spin_unlock_irqrestore(&cm_core->ht_lock, flags);
806
807         /* no owner node */
808         return NULL;
809 }
810
811
812 /**
813  * find_listener - find a cm node listening on this addr-port pair
814  */
815 static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
816                 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
817 {
818         unsigned long flags;
819         struct nes_cm_listener *listen_node;
820         __be32 tmp_addr = cpu_to_be32(dst_addr);
821
822         /* walk list and find cm_node associated with this session ID */
823         spin_lock_irqsave(&cm_core->listen_list_lock, flags);
824         list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
825                 /* compare node pair, return node handle if a match */
826                 if (((listen_node->loc_addr == dst_addr) ||
827                                 listen_node->loc_addr == 0x00000000) &&
828                                 (listen_node->loc_port == dst_port) &&
829                                 (listener_state & listen_node->listener_state)) {
830                         atomic_inc(&listen_node->ref_count);
831                         spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
832                         return listen_node;
833                 }
834         }
835         spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
836
837         nes_debug(NES_DBG_CM, "Unable to find listener for %pI4:%x\n",
838                   &tmp_addr, dst_port);
839
840         /* no listener */
841         return NULL;
842 }
843
844
845 /**
846  * add_hte_node - add a cm node to the hash table
847  */
848 static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
849 {
850         unsigned long flags;
851         struct list_head *hte;
852
853         if (!cm_node || !cm_core)
854                 return -EINVAL;
855
856         nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
857                 cm_node);
858
859         spin_lock_irqsave(&cm_core->ht_lock, flags);
860
861         /* get a handle on the hash table element (list head for this slot) */
862         hte = &cm_core->connected_nodes;
863         list_add_tail(&cm_node->list, hte);
864         atomic_inc(&cm_core->ht_node_cnt);
865
866         spin_unlock_irqrestore(&cm_core->ht_lock, flags);
867
868         return 0;
869 }
870
871
872 /**
873  * mini_cm_dec_refcnt_listen
874  */
875 static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
876         struct nes_cm_listener *listener, int free_hanging_nodes)
877 {
878         int ret = 1;
879         unsigned long flags;
880         struct list_head *list_pos = NULL;
881         struct list_head *list_temp = NULL;
882         struct nes_cm_node *cm_node = NULL;
883         struct list_head reset_list;
884
885         nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
886                 "refcnt=%d\n", listener, free_hanging_nodes,
887                 atomic_read(&listener->ref_count));
888         /* free non-accelerated child nodes for this listener */
889         INIT_LIST_HEAD(&reset_list);
890         if (free_hanging_nodes) {
891                 spin_lock_irqsave(&cm_core->ht_lock, flags);
892                 list_for_each_safe(list_pos, list_temp,
893                                    &g_cm_core->connected_nodes) {
894                         cm_node = container_of(list_pos, struct nes_cm_node,
895                                 list);
896                         if ((cm_node->listener == listener) &&
897                             (!cm_node->accelerated)) {
898                                 add_ref_cm_node(cm_node);
899                                 list_add(&cm_node->reset_entry, &reset_list);
900                         }
901                 }
902                 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
903         }
904
905         list_for_each_safe(list_pos, list_temp, &reset_list) {
906                 cm_node = container_of(list_pos, struct nes_cm_node,
907                                         reset_entry);
908                 cleanup_retrans_entry(cm_node);
909                 send_reset(cm_node, NULL);
910                 rem_ref_cm_node(cm_node->cm_core, cm_node);
911         }
912
913         spin_lock_irqsave(&cm_core->listen_list_lock, flags);
914         if (!atomic_dec_return(&listener->ref_count)) {
915                 list_del(&listener->list);
916
917                 /* decrement our listen node count */
918                 atomic_dec(&cm_core->listen_node_cnt);
919
920                 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
921
922                 if (listener->nesvnic) {
923                         nes_manage_apbvt(listener->nesvnic, listener->loc_port,
924                                         PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
925                 }
926
927                 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
928
929                 kfree(listener);
930                 listener = NULL;
931                 ret = 0;
932                 cm_listens_destroyed++;
933         } else {
934                 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
935         }
936         if (listener) {
937                 if (atomic_read(&listener->pend_accepts_cnt) > 0)
938                         nes_debug(NES_DBG_CM, "destroying listener (%p)"
939                                         " with non-zero pending accepts=%u\n",
940                                         listener, atomic_read(&listener->pend_accepts_cnt));
941         }
942
943         return ret;
944 }
945
946
947 /**
948  * mini_cm_del_listen
949  */
950 static int mini_cm_del_listen(struct nes_cm_core *cm_core,
951                 struct nes_cm_listener *listener)
952 {
953         listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
954         listener->cm_id = NULL; /* going to be destroyed pretty soon */
955         return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
956 }
957
958
959 /**
960  * mini_cm_accelerated
961  */
962 static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
963                 struct nes_cm_node *cm_node)
964 {
965         u32 was_timer_set;
966         cm_node->accelerated = 1;
967
968         if (cm_node->accept_pend) {
969                 BUG_ON(!cm_node->listener);
970                 atomic_dec(&cm_node->listener->pend_accepts_cnt);
971                 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
972         }
973
974         was_timer_set = timer_pending(&cm_core->tcp_timer);
975         if (!was_timer_set) {
976                 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
977                 add_timer(&cm_core->tcp_timer);
978         }
979
980         return 0;
981 }
982
983
984 /**
985  * nes_addr_resolve_neigh
986  */
987 static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip)
988 {
989         struct rtable *rt;
990         struct flowi fl;
991         struct neighbour *neigh;
992         int rc = -1;
993
994         memset(&fl, 0, sizeof fl);
995         fl.nl_u.ip4_u.daddr = htonl(dst_ip);
996         if (ip_route_output_key(&init_net, &rt, &fl)) {
997                 printk("%s: ip_route_output_key failed for 0x%08X\n",
998                                 __func__, dst_ip);
999                 return rc;
1000         }
1001
1002         neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1003         if (neigh) {
1004                 if (neigh->nud_state & NUD_VALID) {
1005                         nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
1006                                   " is %pM, Gateway is 0x%08X \n", dst_ip,
1007                                   neigh->ha, ntohl(rt->rt_gateway));
1008                         nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1009                                              dst_ip, NES_ARP_ADD);
1010                         rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1011                                            NES_ARP_RESOLVE);
1012                 }
1013                 neigh_release(neigh);
1014         }
1015
1016         if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1017                 neigh_event_send(rt->u.dst.neighbour, NULL);
1018
1019         ip_rt_put(rt);
1020         return rc;
1021 }
1022
1023
1024 /**
1025  * make_cm_node - create a new instance of a cm node
1026  */
1027 static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1028                 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1029                 struct nes_cm_listener *listener)
1030 {
1031         struct nes_cm_node *cm_node;
1032         struct timespec ts;
1033         int arpindex = 0;
1034         struct nes_device *nesdev;
1035         struct nes_adapter *nesadapter;
1036
1037         /* create an hte and cm_node for this instance */
1038         cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1039         if (!cm_node)
1040                 return NULL;
1041
1042         /* set our node specific transport info */
1043         cm_node->loc_addr = cm_info->loc_addr;
1044         cm_node->rem_addr = cm_info->rem_addr;
1045         cm_node->loc_port = cm_info->loc_port;
1046         cm_node->rem_port = cm_info->rem_port;
1047         cm_node->send_write0 = send_first;
1048         nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1049                   &cm_node->loc_addr, cm_node->loc_port,
1050                   &cm_node->rem_addr, cm_node->rem_port);
1051         cm_node->listener = listener;
1052         cm_node->netdev = nesvnic->netdev;
1053         cm_node->cm_id = cm_info->cm_id;
1054         memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1055
1056         nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1057                         cm_node->cm_id);
1058
1059         spin_lock_init(&cm_node->retrans_list_lock);
1060         INIT_LIST_HEAD(&cm_node->recv_list);
1061         spin_lock_init(&cm_node->recv_list_lock);
1062
1063         cm_node->loopbackpartner = NULL;
1064         atomic_set(&cm_node->ref_count, 1);
1065         /* associate our parent CM core */
1066         cm_node->cm_core = cm_core;
1067         cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1068         cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1069         cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1070                         NES_CM_DEFAULT_RCV_WND_SCALE;
1071         ts = current_kernel_time();
1072         cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1073         cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
1074                         sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
1075         cm_node->tcp_cntxt.rcv_nxt = 0;
1076         /* get a unique session ID , add thread_id to an upcounter to handle race */
1077         atomic_inc(&cm_core->node_cnt);
1078         cm_node->conn_type = cm_info->conn_type;
1079         cm_node->apbvt_set = 0;
1080         cm_node->accept_pend = 0;
1081
1082         cm_node->nesvnic = nesvnic;
1083         /* get some device handles, for arp lookup */
1084         nesdev = nesvnic->nesdev;
1085         nesadapter = nesdev->nesadapter;
1086
1087         cm_node->loopbackpartner = NULL;
1088         /* get the mac addr for the remote node */
1089         if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1090                 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
1091         else
1092                 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1093         if (arpindex < 0) {
1094                 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr);
1095                 if (arpindex < 0) {
1096                         kfree(cm_node);
1097                         return NULL;
1098                 }
1099         }
1100
1101         /* copy the mac addr to node context */
1102         memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
1103         nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1104                   cm_node->rem_mac);
1105
1106         add_hte_node(cm_core, cm_node);
1107         atomic_inc(&cm_nodes_created);
1108
1109         return cm_node;
1110 }
1111
1112
1113 /**
1114  * add_ref_cm_node - destroy an instance of a cm node
1115  */
1116 static int add_ref_cm_node(struct nes_cm_node *cm_node)
1117 {
1118         atomic_inc(&cm_node->ref_count);
1119         return 0;
1120 }
1121
1122
1123 /**
1124  * rem_ref_cm_node - destroy an instance of a cm node
1125  */
1126 static int rem_ref_cm_node(struct nes_cm_core *cm_core,
1127         struct nes_cm_node *cm_node)
1128 {
1129         unsigned long flags, qplockflags;
1130         struct nes_timer_entry *recv_entry;
1131         struct iw_cm_id *cm_id;
1132         struct list_head *list_core, *list_node_temp;
1133         struct nes_qp *nesqp;
1134
1135         if (!cm_node)
1136                 return -EINVAL;
1137
1138         spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1139         if (atomic_dec_return(&cm_node->ref_count)) {
1140                 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1141                 return 0;
1142         }
1143         list_del(&cm_node->list);
1144         atomic_dec(&cm_core->ht_node_cnt);
1145         spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1146
1147         /* if the node is destroyed before connection was accelerated */
1148         if (!cm_node->accelerated && cm_node->accept_pend) {
1149                 BUG_ON(!cm_node->listener);
1150                 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1151                 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1152         }
1153         BUG_ON(cm_node->send_entry);
1154         spin_lock_irqsave(&cm_node->recv_list_lock, flags);
1155         list_for_each_safe(list_core, list_node_temp, &cm_node->recv_list) {
1156                 recv_entry = container_of(list_core, struct nes_timer_entry,
1157                                 list);
1158                 list_del(&recv_entry->list);
1159                 cm_id = cm_node->cm_id;
1160                 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
1161                 nesqp = (struct nes_qp *)recv_entry->skb;
1162                 spin_lock_irqsave(&nesqp->lock, qplockflags);
1163                 if (nesqp->cm_id) {
1164                         nes_debug(NES_DBG_CM, "QP%u: cm_id = %p: HIT A "
1165                                 "NES_TIMER_TYPE_CLOSE with something to do!\n",
1166                                 nesqp->hwqp.qp_id, cm_id);
1167                         nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
1168                         nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
1169                         nesqp->ibqp_state = IB_QPS_ERR;
1170                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
1171                         nes_cm_disconn(nesqp);
1172                 } else {
1173                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
1174                         nes_debug(NES_DBG_CM, "QP%u: cm_id = %p: HIT A "
1175                                 "NES_TIMER_TYPE_CLOSE with nothing to do!\n",
1176                                 nesqp->hwqp.qp_id, cm_id);
1177                 }
1178                 cm_id->rem_ref(cm_id);
1179
1180                 kfree(recv_entry);
1181                 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
1182         }
1183         spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
1184
1185         if (cm_node->listener) {
1186                 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1187         } else {
1188                 if (cm_node->apbvt_set && cm_node->nesvnic) {
1189                         nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
1190                                 PCI_FUNC(
1191                                 cm_node->nesvnic->nesdev->pcidev->devfn),
1192                                 NES_MANAGE_APBVT_DEL);
1193                 }
1194         }
1195
1196         atomic_dec(&cm_core->node_cnt);
1197         atomic_inc(&cm_nodes_destroyed);
1198         nesqp = cm_node->nesqp;
1199         if (nesqp) {
1200                 nesqp->cm_node = NULL;
1201                 nes_rem_ref(&nesqp->ibqp);
1202                 cm_node->nesqp = NULL;
1203         }
1204
1205         cm_node->freed = 1;
1206         kfree(cm_node);
1207         return 0;
1208 }
1209
1210 /**
1211  * process_options
1212  */
1213 static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1214         u32 optionsize, u32 syn_packet)
1215 {
1216         u32 tmp;
1217         u32 offset = 0;
1218         union all_known_options *all_options;
1219         char got_mss_option = 0;
1220
1221         while (offset < optionsize) {
1222                 all_options = (union all_known_options *)(optionsloc + offset);
1223                 switch (all_options->as_base.optionnum) {
1224                 case OPTION_NUMBER_END:
1225                         offset = optionsize;
1226                         break;
1227                 case OPTION_NUMBER_NONE:
1228                         offset += 1;
1229                         continue;
1230                 case OPTION_NUMBER_MSS:
1231                         nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1232                                 "Size: %d\n", __func__,
1233                                 all_options->as_mss.length, offset, optionsize);
1234                         got_mss_option = 1;
1235                         if (all_options->as_mss.length != 4) {
1236                                 return 1;
1237                         } else {
1238                                 tmp = ntohs(all_options->as_mss.mss);
1239                                 if (tmp > 0 && tmp <
1240                                         cm_node->tcp_cntxt.mss)
1241                                         cm_node->tcp_cntxt.mss = tmp;
1242                         }
1243                         break;
1244                 case OPTION_NUMBER_WINDOW_SCALE:
1245                         cm_node->tcp_cntxt.snd_wscale =
1246                                 all_options->as_windowscale.shiftcount;
1247                         break;
1248                 case OPTION_NUMBER_WRITE0:
1249                         cm_node->send_write0 = 1;
1250                         break;
1251                 default:
1252                         nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1253                                 all_options->as_base.optionnum);
1254                         break;
1255                 }
1256                 offset += all_options->as_base.length;
1257         }
1258         if ((!got_mss_option) && (syn_packet))
1259                 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1260         return 0;
1261 }
1262
1263 static void drop_packet(struct sk_buff *skb)
1264 {
1265         atomic_inc(&cm_accel_dropped_pkts);
1266         dev_kfree_skb_any(skb);
1267 }
1268
1269 static void handle_fin_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1270         struct tcphdr *tcph)
1271 {
1272         nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1273                 "refcnt=%d\n", cm_node, cm_node->state,
1274                 atomic_read(&cm_node->ref_count));
1275         cm_node->tcp_cntxt.rcv_nxt++;
1276         cleanup_retrans_entry(cm_node);
1277         switch (cm_node->state) {
1278         case NES_CM_STATE_SYN_RCVD:
1279         case NES_CM_STATE_SYN_SENT:
1280         case NES_CM_STATE_ESTABLISHED:
1281         case NES_CM_STATE_MPAREQ_SENT:
1282                 cm_node->state = NES_CM_STATE_LAST_ACK;
1283                 send_fin(cm_node, skb);
1284                 break;
1285         case NES_CM_STATE_FIN_WAIT1:
1286                 cm_node->state = NES_CM_STATE_CLOSING;
1287                 send_ack(cm_node, skb);
1288                 break;
1289         case NES_CM_STATE_FIN_WAIT2:
1290                 cm_node->state = NES_CM_STATE_TIME_WAIT;
1291                 send_ack(cm_node, skb);
1292                 cm_node->state = NES_CM_STATE_CLOSED;
1293                 break;
1294         case NES_CM_STATE_TSA:
1295         default:
1296                 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1297                         cm_node, cm_node->state);
1298                 drop_packet(skb);
1299                 break;
1300         }
1301 }
1302
1303
1304 static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1305         struct tcphdr *tcph)
1306 {
1307
1308         int     reset = 0;      /* whether to send reset in case of err.. */
1309         int     passive_state;
1310         atomic_inc(&cm_resets_recvd);
1311         nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1312                         " refcnt=%d\n", cm_node, cm_node->state,
1313                         atomic_read(&cm_node->ref_count));
1314         cleanup_retrans_entry(cm_node);
1315         switch (cm_node->state) {
1316         case NES_CM_STATE_SYN_SENT:
1317         case NES_CM_STATE_MPAREQ_SENT:
1318                 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1319                         "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1320                         cm_node->listener, cm_node->state);
1321                 active_open_err(cm_node, skb, reset);
1322                 break;
1323         case NES_CM_STATE_MPAREQ_RCVD:
1324                 passive_state = atomic_add_return(1, &cm_node->passive_state);
1325                 if (passive_state ==  NES_SEND_RESET_EVENT)
1326                         create_event(cm_node, NES_CM_EVENT_RESET);
1327                 cleanup_retrans_entry(cm_node);
1328                 cm_node->state = NES_CM_STATE_CLOSED;
1329                 dev_kfree_skb_any(skb);
1330                 break;
1331         case NES_CM_STATE_ESTABLISHED:
1332         case NES_CM_STATE_SYN_RCVD:
1333         case NES_CM_STATE_LISTENING:
1334                 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1335                 passive_open_err(cm_node, skb, reset);
1336                 break;
1337         case NES_CM_STATE_TSA:
1338                 active_open_err(cm_node, skb, reset);
1339                 break;
1340         case NES_CM_STATE_CLOSED:
1341                 cleanup_retrans_entry(cm_node);
1342                 drop_packet(skb);
1343                 break;
1344         default:
1345                 drop_packet(skb);
1346                 break;
1347         }
1348 }
1349
1350 static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb,
1351         enum nes_cm_event_type type)
1352 {
1353
1354         int     ret;
1355         int datasize = skb->len;
1356         u8 *dataloc = skb->data;
1357         ret = parse_mpa(cm_node, dataloc, datasize);
1358         if (ret < 0) {
1359                 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
1360                 if (type == NES_CM_EVENT_CONNECTED) {
1361                         nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1362                                 "cm_node=%p listener=%p state=%d\n", __func__,
1363                                 __LINE__, cm_node, cm_node->listener,
1364                                 cm_node->state);
1365                         active_open_err(cm_node, skb, 1);
1366                 } else {
1367                         passive_open_err(cm_node, skb, 1);
1368                 }
1369         } else {
1370                 cleanup_retrans_entry(cm_node);
1371                 dev_kfree_skb_any(skb);
1372                 if (type == NES_CM_EVENT_CONNECTED)
1373                         cm_node->state = NES_CM_STATE_TSA;
1374                 else
1375                         atomic_set(&cm_node->passive_state,
1376                                         NES_PASSIVE_STATE_INDICATED);
1377                 create_event(cm_node, type);
1378
1379         }
1380         return ;
1381 }
1382
1383 static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1384 {
1385         switch (cm_node->state) {
1386         case NES_CM_STATE_SYN_SENT:
1387         case NES_CM_STATE_MPAREQ_SENT:
1388                 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1389                         "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1390                         cm_node->listener, cm_node->state);
1391                 active_open_err(cm_node, skb, 1);
1392                 break;
1393         case NES_CM_STATE_ESTABLISHED:
1394         case NES_CM_STATE_SYN_RCVD:
1395                 passive_open_err(cm_node, skb, 1);
1396                 break;
1397         case NES_CM_STATE_TSA:
1398         default:
1399                 drop_packet(skb);
1400         }
1401 }
1402
1403 static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1404         struct sk_buff *skb)
1405 {
1406         int err;
1407
1408         err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1409         if (err)
1410                 active_open_err(cm_node, skb, 1);
1411
1412         return err;
1413 }
1414
1415 static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1416         struct sk_buff *skb)
1417 {
1418         int err = 0;
1419         u32 seq;
1420         u32 ack_seq;
1421         u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1422         u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1423         u32 rcv_wnd;
1424         seq = ntohl(tcph->seq);
1425         ack_seq = ntohl(tcph->ack_seq);
1426         rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1427         if (ack_seq != loc_seq_num)
1428                 err = 1;
1429         else if ((seq + rcv_wnd) < rcv_nxt)
1430                 err = 1;
1431         if (err) {
1432                 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1433                         "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1434                         cm_node->listener, cm_node->state);
1435                 indicate_pkt_err(cm_node, skb);
1436                 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1437                         "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1438                         rcv_wnd);
1439         }
1440         return err;
1441 }
1442
1443 /*
1444  * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1445  * is created with a listener or it may comein as rexmitted packet which in
1446  * that case will be just dropped.
1447  */
1448
1449 static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1450         struct tcphdr *tcph)
1451 {
1452         int ret;
1453         u32 inc_sequence;
1454         int optionsize;
1455
1456         optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1457         skb_trim(skb, 0);
1458         inc_sequence = ntohl(tcph->seq);
1459
1460         switch (cm_node->state) {
1461         case NES_CM_STATE_SYN_SENT:
1462         case NES_CM_STATE_MPAREQ_SENT:
1463                 /* Rcvd syn on active open connection*/
1464                 active_open_err(cm_node, skb, 1);
1465                 break;
1466         case NES_CM_STATE_LISTENING:
1467                 /* Passive OPEN */
1468                 cm_node->accept_pend = 1;
1469                 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1470                 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1471                                 cm_node->listener->backlog) {
1472                         nes_debug(NES_DBG_CM, "drop syn due to backlog "
1473                                 "pressure \n");
1474                         cm_backlog_drops++;
1475                         passive_open_err(cm_node, skb, 0);
1476                         break;
1477                 }
1478                 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1479                         1);
1480                 if (ret) {
1481                         passive_open_err(cm_node, skb, 0);
1482                         /* drop pkt */
1483                         break;
1484                 }
1485                 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1486                 BUG_ON(cm_node->send_entry);
1487                 cm_node->state = NES_CM_STATE_SYN_RCVD;
1488                 send_syn(cm_node, 1, skb);
1489                 break;
1490         case NES_CM_STATE_CLOSED:
1491                 cleanup_retrans_entry(cm_node);
1492                 send_reset(cm_node, skb);
1493                 break;
1494         case NES_CM_STATE_TSA:
1495         case NES_CM_STATE_ESTABLISHED:
1496         case NES_CM_STATE_FIN_WAIT1:
1497         case NES_CM_STATE_FIN_WAIT2:
1498         case NES_CM_STATE_MPAREQ_RCVD:
1499         case NES_CM_STATE_LAST_ACK:
1500         case NES_CM_STATE_CLOSING:
1501         case NES_CM_STATE_UNKNOWN:
1502         default:
1503                 drop_packet(skb);
1504                 break;
1505         }
1506 }
1507
1508 static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1509         struct tcphdr *tcph)
1510 {
1511
1512         int ret;
1513         u32 inc_sequence;
1514         int optionsize;
1515
1516         optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1517         skb_trim(skb, 0);
1518         inc_sequence = ntohl(tcph->seq);
1519         switch (cm_node->state) {
1520         case NES_CM_STATE_SYN_SENT:
1521                 /* active open */
1522                 if (check_syn(cm_node, tcph, skb))
1523                         return;
1524                 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1525                 /* setup options */
1526                 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1527                 if (ret) {
1528                         nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1529                                 cm_node);
1530                         break;
1531                 }
1532                 cleanup_retrans_entry(cm_node);
1533                 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1534                 send_mpa_request(cm_node, skb);
1535                 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1536                 break;
1537         case NES_CM_STATE_MPAREQ_RCVD:
1538                 /* passive open, so should not be here */
1539                 passive_open_err(cm_node, skb, 1);
1540                 break;
1541         case NES_CM_STATE_LISTENING:
1542         case NES_CM_STATE_CLOSED:
1543                 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1544                 cleanup_retrans_entry(cm_node);
1545                 send_reset(cm_node, skb);
1546                 break;
1547         case NES_CM_STATE_ESTABLISHED:
1548         case NES_CM_STATE_FIN_WAIT1:
1549         case NES_CM_STATE_FIN_WAIT2:
1550         case NES_CM_STATE_LAST_ACK:
1551         case NES_CM_STATE_TSA:
1552         case NES_CM_STATE_CLOSING:
1553         case NES_CM_STATE_UNKNOWN:
1554         case NES_CM_STATE_MPAREQ_SENT:
1555         default:
1556                 drop_packet(skb);
1557                 break;
1558         }
1559 }
1560
1561 static void handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1562         struct tcphdr *tcph)
1563 {
1564         int datasize = 0;
1565         u32 inc_sequence;
1566         u32 rem_seq_ack;
1567         u32 rem_seq;
1568         int ret;
1569         int optionsize;
1570         u32 temp_seq = cm_node->tcp_cntxt.loc_seq_num;
1571
1572         optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1573         cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1574
1575         if (check_seq(cm_node, tcph, skb))
1576                 return;
1577
1578         skb_pull(skb, tcph->doff << 2);
1579         inc_sequence = ntohl(tcph->seq);
1580         rem_seq = ntohl(tcph->seq);
1581         rem_seq_ack =  ntohl(tcph->ack_seq);
1582         datasize = skb->len;
1583
1584         switch (cm_node->state) {
1585         case NES_CM_STATE_SYN_RCVD:
1586                 /* Passive OPEN */
1587                 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1588                 if (ret)
1589                         break;
1590                 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1591                 cm_node->tcp_cntxt.loc_seq_num = temp_seq;
1592                 if (cm_node->tcp_cntxt.rem_ack_num !=
1593                     cm_node->tcp_cntxt.loc_seq_num) {
1594                         nes_debug(NES_DBG_CM, "rem_ack_num != loc_seq_num\n");
1595                         cleanup_retrans_entry(cm_node);
1596                         send_reset(cm_node, skb);
1597                         return;
1598                 }
1599                 cm_node->state = NES_CM_STATE_ESTABLISHED;
1600                 if (datasize) {
1601                         cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1602                         cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1603                         handle_rcv_mpa(cm_node, skb, NES_CM_EVENT_MPA_REQ);
1604                  } else { /* rcvd ACK only */
1605                         dev_kfree_skb_any(skb);
1606                         cleanup_retrans_entry(cm_node);
1607                  }
1608                 break;
1609         case NES_CM_STATE_ESTABLISHED:
1610                 /* Passive OPEN */
1611                 /* We expect mpa frame to be received only */
1612                 if (datasize) {
1613                         cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1614                         cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1615                         handle_rcv_mpa(cm_node, skb,
1616                                 NES_CM_EVENT_MPA_REQ);
1617                 } else
1618                         drop_packet(skb);
1619                 break;
1620         case NES_CM_STATE_MPAREQ_SENT:
1621                 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1622                 if (datasize) {
1623                         cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1624                         handle_rcv_mpa(cm_node, skb, NES_CM_EVENT_CONNECTED);
1625                 } else { /* Could be just an ack pkt.. */
1626                         cleanup_retrans_entry(cm_node);
1627                         dev_kfree_skb_any(skb);
1628                 }
1629                 break;
1630         case NES_CM_STATE_LISTENING:
1631         case NES_CM_STATE_CLOSED:
1632                 cleanup_retrans_entry(cm_node);
1633                 send_reset(cm_node, skb);
1634                 break;
1635         case NES_CM_STATE_FIN_WAIT1:
1636         case NES_CM_STATE_SYN_SENT:
1637         case NES_CM_STATE_FIN_WAIT2:
1638         case NES_CM_STATE_TSA:
1639         case NES_CM_STATE_MPAREQ_RCVD:
1640         case NES_CM_STATE_LAST_ACK:
1641         case NES_CM_STATE_CLOSING:
1642         case NES_CM_STATE_UNKNOWN:
1643         default:
1644                 drop_packet(skb);
1645                 break;
1646         }
1647 }
1648
1649
1650
1651 static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1652         struct sk_buff *skb, int optionsize, int passive)
1653 {
1654         u8 *optionsloc = (u8 *)&tcph[1];
1655         if (optionsize) {
1656                 if (process_options(cm_node, optionsloc, optionsize,
1657                         (u32)tcph->syn)) {
1658                         nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1659                                 __func__, cm_node);
1660                         if (passive)
1661                                 passive_open_err(cm_node, skb, 1);
1662                         else
1663                                 active_open_err(cm_node, skb, 1);
1664                         return 1;
1665                 }
1666         }
1667
1668         cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1669                         cm_node->tcp_cntxt.snd_wscale;
1670
1671         if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
1672                 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
1673         return 0;
1674 }
1675
1676 /*
1677  * active_open_err() will send reset() if flag set..
1678  * It will also send ABORT event.
1679  */
1680
1681 static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1682         int reset)
1683 {
1684         cleanup_retrans_entry(cm_node);
1685         if (reset) {
1686                 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1687                                 "state=%d\n", cm_node, cm_node->state);
1688                 add_ref_cm_node(cm_node);
1689                 send_reset(cm_node, skb);
1690         } else
1691                 dev_kfree_skb_any(skb);
1692
1693         cm_node->state = NES_CM_STATE_CLOSED;
1694         create_event(cm_node, NES_CM_EVENT_ABORTED);
1695 }
1696
1697 /*
1698  * passive_open_err() will either do a reset() or will free up the skb and
1699  * remove the cm_node.
1700  */
1701
1702 static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1703         int reset)
1704 {
1705         cleanup_retrans_entry(cm_node);
1706         cm_node->state = NES_CM_STATE_CLOSED;
1707         if (reset) {
1708                 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1709                         "cm_node=%p state =%d\n", cm_node, cm_node->state);
1710                 send_reset(cm_node, skb);
1711         } else {
1712                 dev_kfree_skb_any(skb);
1713                 rem_ref_cm_node(cm_node->cm_core, cm_node);
1714         }
1715 }
1716
1717 /*
1718  * free_retrans_entry() routines assumes that the retrans_list_lock has
1719  * been acquired before calling.
1720  */
1721 static void free_retrans_entry(struct nes_cm_node *cm_node)
1722 {
1723         struct nes_timer_entry *send_entry;
1724         send_entry = cm_node->send_entry;
1725         if (send_entry) {
1726                 cm_node->send_entry = NULL;
1727                 dev_kfree_skb_any(send_entry->skb);
1728                 kfree(send_entry);
1729                 rem_ref_cm_node(cm_node->cm_core, cm_node);
1730         }
1731 }
1732
1733 static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1734 {
1735         unsigned long flags;
1736
1737         spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1738         free_retrans_entry(cm_node);
1739         spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1740 }
1741
1742 /**
1743  * process_packet
1744  * Returns skb if to be freed, else it will return NULL if already used..
1745  */
1746 static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1747         struct nes_cm_core *cm_core)
1748 {
1749         enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1750         struct tcphdr *tcph = tcp_hdr(skb);
1751         skb_pull(skb, ip_hdr(skb)->ihl << 2);
1752
1753         nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1754                 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1755                 tcph->ack, tcph->rst, tcph->fin);
1756
1757         if (tcph->rst)
1758                 pkt_type = NES_PKT_TYPE_RST;
1759         else if (tcph->syn) {
1760                 pkt_type = NES_PKT_TYPE_SYN;
1761                 if (tcph->ack)
1762                         pkt_type = NES_PKT_TYPE_SYNACK;
1763         } else if (tcph->fin)
1764                 pkt_type = NES_PKT_TYPE_FIN;
1765         else if (tcph->ack)
1766                 pkt_type = NES_PKT_TYPE_ACK;
1767
1768         switch (pkt_type) {
1769         case NES_PKT_TYPE_SYN:
1770                 handle_syn_pkt(cm_node, skb, tcph);
1771                 break;
1772         case NES_PKT_TYPE_SYNACK:
1773                 handle_synack_pkt(cm_node, skb, tcph);
1774                 break;
1775         case NES_PKT_TYPE_ACK:
1776                 handle_ack_pkt(cm_node, skb, tcph);
1777                 break;
1778         case NES_PKT_TYPE_RST:
1779                 handle_rst_pkt(cm_node, skb, tcph);
1780                 break;
1781         case NES_PKT_TYPE_FIN:
1782                 handle_fin_pkt(cm_node, skb, tcph);
1783                 break;
1784         default:
1785                 drop_packet(skb);
1786                 break;
1787         }
1788 }
1789
1790 /**
1791  * mini_cm_listen - create a listen node with params
1792  */
1793 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
1794         struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
1795 {
1796         struct nes_cm_listener *listener;
1797         unsigned long flags;
1798
1799         nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1800                 cm_info->loc_addr, cm_info->loc_port);
1801
1802         /* cannot have multiple matching listeners */
1803         listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1804                         htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1805         if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1806                 /* find automatically incs ref count ??? */
1807                 atomic_dec(&listener->ref_count);
1808                 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1809                 return NULL;
1810         }
1811
1812         if (!listener) {
1813                 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1814                 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1815                 if (!listener) {
1816                         nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1817                         return NULL;
1818                 }
1819
1820                 listener->loc_addr = htonl(cm_info->loc_addr);
1821                 listener->loc_port = htons(cm_info->loc_port);
1822                 listener->reused_node = 0;
1823
1824                 atomic_set(&listener->ref_count, 1);
1825         }
1826         /* pasive case */
1827         /* find already inc'ed the ref count */
1828         else {
1829                 listener->reused_node = 1;
1830         }
1831
1832         listener->cm_id = cm_info->cm_id;
1833         atomic_set(&listener->pend_accepts_cnt, 0);
1834         listener->cm_core = cm_core;
1835         listener->nesvnic = nesvnic;
1836         atomic_inc(&cm_core->node_cnt);
1837
1838         listener->conn_type = cm_info->conn_type;
1839         listener->backlog = cm_info->backlog;
1840         listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1841
1842         if (!listener->reused_node) {
1843                 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1844                 list_add(&listener->list, &cm_core->listen_list.list);
1845                 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1846                 atomic_inc(&cm_core->listen_node_cnt);
1847         }
1848
1849         nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1850                         " listener = %p, backlog = %d, cm_id = %p.\n",
1851                         cm_info->loc_addr, cm_info->loc_port,
1852                         listener, listener->backlog, listener->cm_id);
1853
1854         return listener;
1855 }
1856
1857
1858 /**
1859  * mini_cm_connect - make a connection node with params
1860  */
1861 static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
1862         struct nes_vnic *nesvnic, u16 private_data_len,
1863         void *private_data, struct nes_cm_info *cm_info)
1864 {
1865         int ret = 0;
1866         struct nes_cm_node *cm_node;
1867         struct nes_cm_listener *loopbackremotelistener;
1868         struct nes_cm_node *loopbackremotenode;
1869         struct nes_cm_info loopback_cm_info;
1870         u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
1871         struct ietf_mpa_frame *mpa_frame = NULL;
1872
1873         /* create a CM connection node */
1874         cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
1875         if (!cm_node)
1876                 return NULL;
1877         mpa_frame = &cm_node->mpa_frame;
1878         strcpy(mpa_frame->key, IEFT_MPA_KEY_REQ);
1879         mpa_frame->flags = IETF_MPA_FLAGS_CRC;
1880         mpa_frame->rev =  IETF_MPA_VERSION;
1881         mpa_frame->priv_data_len = htons(private_data_len);
1882
1883         /* set our node side to client (active) side */
1884         cm_node->tcp_cntxt.client = 1;
1885         cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1886
1887         if (cm_info->loc_addr == cm_info->rem_addr) {
1888                 loopbackremotelistener = find_listener(cm_core,
1889                                 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
1890                                 NES_CM_LISTENER_ACTIVE_STATE);
1891                 if (loopbackremotelistener == NULL) {
1892                         create_event(cm_node, NES_CM_EVENT_ABORTED);
1893                 } else {
1894                         atomic_inc(&cm_loopbacks);
1895                         loopback_cm_info = *cm_info;
1896                         loopback_cm_info.loc_port = cm_info->rem_port;
1897                         loopback_cm_info.rem_port = cm_info->loc_port;
1898                         loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
1899                         loopbackremotenode = make_cm_node(cm_core, nesvnic,
1900                                 &loopback_cm_info, loopbackremotelistener);
1901                         loopbackremotenode->loopbackpartner = cm_node;
1902                         loopbackremotenode->tcp_cntxt.rcv_wscale =
1903                                 NES_CM_DEFAULT_RCV_WND_SCALE;
1904                         cm_node->loopbackpartner = loopbackremotenode;
1905                         memcpy(loopbackremotenode->mpa_frame_buf, private_data,
1906                                 private_data_len);
1907                         loopbackremotenode->mpa_frame_size = private_data_len;
1908
1909                         /* we are done handling this state. */
1910                         /* set node to a TSA state */
1911                         cm_node->state = NES_CM_STATE_TSA;
1912                         cm_node->tcp_cntxt.rcv_nxt =
1913                                 loopbackremotenode->tcp_cntxt.loc_seq_num;
1914                         loopbackremotenode->tcp_cntxt.rcv_nxt =
1915                                 cm_node->tcp_cntxt.loc_seq_num;
1916                         cm_node->tcp_cntxt.max_snd_wnd =
1917                                 loopbackremotenode->tcp_cntxt.rcv_wnd;
1918                         loopbackremotenode->tcp_cntxt.max_snd_wnd =
1919                                 cm_node->tcp_cntxt.rcv_wnd;
1920                         cm_node->tcp_cntxt.snd_wnd =
1921                                 loopbackremotenode->tcp_cntxt.rcv_wnd;
1922                         loopbackremotenode->tcp_cntxt.snd_wnd =
1923                                 cm_node->tcp_cntxt.rcv_wnd;
1924                         cm_node->tcp_cntxt.snd_wscale =
1925                                 loopbackremotenode->tcp_cntxt.rcv_wscale;
1926                         loopbackremotenode->tcp_cntxt.snd_wscale =
1927                                 cm_node->tcp_cntxt.rcv_wscale;
1928
1929                         create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
1930                 }
1931                 return cm_node;
1932         }
1933
1934         /* set our node side to client (active) side */
1935         cm_node->tcp_cntxt.client = 1;
1936         /* init our MPA frame ptr */
1937         memcpy(mpa_frame->priv_data, private_data, private_data_len);
1938
1939         cm_node->mpa_frame_size = mpa_frame_size;
1940
1941         /* send a syn and goto syn sent state */
1942         cm_node->state = NES_CM_STATE_SYN_SENT;
1943         ret = send_syn(cm_node, 0, NULL);
1944
1945         if (ret) {
1946                 /* error in sending the syn free up the cm_node struct */
1947                 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
1948                         "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
1949                         cm_node->rem_addr, cm_node->rem_port, cm_node,
1950                         cm_node->cm_id);
1951                 rem_ref_cm_node(cm_node->cm_core, cm_node);
1952                 cm_node = NULL;
1953         }
1954
1955         if (cm_node)
1956                 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
1957                         "port=0x%04x, cm_node=%p, cm_id = %p.\n",
1958                         cm_node->rem_addr, cm_node->rem_port, cm_node,
1959                         cm_node->cm_id);
1960
1961         return cm_node;
1962 }
1963
1964
1965 /**
1966  * mini_cm_accept - accept a connection
1967  * This function is never called
1968  */
1969 static int mini_cm_accept(struct nes_cm_core *cm_core,
1970         struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
1971 {
1972         return 0;
1973 }
1974
1975
1976 /**
1977  * mini_cm_reject - reject and teardown a connection
1978  */
1979 static int mini_cm_reject(struct nes_cm_core *cm_core,
1980         struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
1981 {
1982         int ret = 0;
1983         int passive_state;
1984
1985         nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
1986                 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
1987
1988         if (cm_node->tcp_cntxt.client)
1989                 return ret;
1990         cleanup_retrans_entry(cm_node);
1991
1992         passive_state = atomic_add_return(1, &cm_node->passive_state);
1993         cm_node->state = NES_CM_STATE_CLOSED;
1994         if (passive_state == NES_SEND_RESET_EVENT)
1995                 rem_ref_cm_node(cm_core, cm_node);
1996         else
1997                 ret = send_reset(cm_node, NULL);
1998         return ret;
1999 }
2000
2001
2002 /**
2003  * mini_cm_close
2004  */
2005 static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
2006 {
2007         int ret = 0;
2008
2009         if (!cm_core || !cm_node)
2010                 return -EINVAL;
2011
2012         switch (cm_node->state) {
2013         case NES_CM_STATE_SYN_RCVD:
2014         case NES_CM_STATE_SYN_SENT:
2015         case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2016         case NES_CM_STATE_ESTABLISHED:
2017         case NES_CM_STATE_ACCEPTING:
2018         case NES_CM_STATE_MPAREQ_SENT:
2019         case NES_CM_STATE_MPAREQ_RCVD:
2020                 cleanup_retrans_entry(cm_node);
2021                 send_reset(cm_node, NULL);
2022                 break;
2023         case NES_CM_STATE_CLOSE_WAIT:
2024                 cm_node->state = NES_CM_STATE_LAST_ACK;
2025                 send_fin(cm_node, NULL);
2026                 break;
2027         case NES_CM_STATE_FIN_WAIT1:
2028         case NES_CM_STATE_FIN_WAIT2:
2029         case NES_CM_STATE_LAST_ACK:
2030         case NES_CM_STATE_TIME_WAIT:
2031         case NES_CM_STATE_CLOSING:
2032                 ret = -1;
2033                 break;
2034         case NES_CM_STATE_LISTENING:
2035         case NES_CM_STATE_UNKNOWN:
2036         case NES_CM_STATE_INITED:
2037         case NES_CM_STATE_CLOSED:
2038                 ret = rem_ref_cm_node(cm_core, cm_node);
2039                 break;
2040         case NES_CM_STATE_TSA:
2041                 if (cm_node->send_entry)
2042                         printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2043                                 "send_entry=%p\n", cm_node->send_entry);
2044                 ret = rem_ref_cm_node(cm_core, cm_node);
2045                 break;
2046         }
2047         return ret;
2048 }
2049
2050
2051 /**
2052  * recv_pkt - recv an ETHERNET packet, and process it through CM
2053  * node state machine
2054  */
2055 static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
2056         struct nes_vnic *nesvnic, struct sk_buff *skb)
2057 {
2058         struct nes_cm_node *cm_node = NULL;
2059         struct nes_cm_listener *listener = NULL;
2060         struct iphdr *iph;
2061         struct tcphdr *tcph;
2062         struct nes_cm_info nfo;
2063         int skb_handled = 1;
2064         __be32 tmp_daddr, tmp_saddr;
2065
2066         if (!skb)
2067                 return 0;
2068         if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
2069                 return 0;
2070         }
2071
2072         iph = (struct iphdr *)skb->data;
2073         tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
2074
2075         nfo.loc_addr = ntohl(iph->daddr);
2076         nfo.loc_port = ntohs(tcph->dest);
2077         nfo.rem_addr = ntohl(iph->saddr);
2078         nfo.rem_port = ntohs(tcph->source);
2079
2080         tmp_daddr = cpu_to_be32(iph->daddr);
2081         tmp_saddr = cpu_to_be32(iph->saddr);
2082
2083         nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
2084                   &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
2085
2086         do {
2087                 cm_node = find_node(cm_core,
2088                         nfo.rem_port, nfo.rem_addr,
2089                         nfo.loc_port, nfo.loc_addr);
2090
2091                 if (!cm_node) {
2092                         /* Only type of packet accepted are for */
2093                         /* the PASSIVE open (syn only) */
2094                         if ((!tcph->syn) || (tcph->ack)) {
2095                                 skb_handled = 0;
2096                                 break;
2097                         }
2098                         listener = find_listener(cm_core, nfo.loc_addr,
2099                                 nfo.loc_port,
2100                                 NES_CM_LISTENER_ACTIVE_STATE);
2101                         if (!listener) {
2102                                 nfo.cm_id = NULL;
2103                                 nfo.conn_type = 0;
2104                                 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2105                                 skb_handled = 0;
2106                                 break;
2107                         }
2108                         nfo.cm_id = listener->cm_id;
2109                         nfo.conn_type = listener->conn_type;
2110                         cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2111                                 listener);
2112                         if (!cm_node) {
2113                                 nes_debug(NES_DBG_CM, "Unable to allocate "
2114                                         "node\n");
2115                                 cm_packets_dropped++;
2116                                 atomic_dec(&listener->ref_count);
2117                                 dev_kfree_skb_any(skb);
2118                                 break;
2119                         }
2120                         if (!tcph->rst && !tcph->fin) {
2121                                 cm_node->state = NES_CM_STATE_LISTENING;
2122                         } else {
2123                                 cm_packets_dropped++;
2124                                 rem_ref_cm_node(cm_core, cm_node);
2125                                 dev_kfree_skb_any(skb);
2126                                 break;
2127                         }
2128                         add_ref_cm_node(cm_node);
2129                 } else if (cm_node->state == NES_CM_STATE_TSA) {
2130                         rem_ref_cm_node(cm_core, cm_node);
2131                         atomic_inc(&cm_accel_dropped_pkts);
2132                         dev_kfree_skb_any(skb);
2133                         break;
2134                 }
2135                 skb_reset_network_header(skb);
2136                 skb_set_transport_header(skb, sizeof(*tcph));
2137                 skb->len = ntohs(iph->tot_len);
2138                 process_packet(cm_node, skb, cm_core);
2139                 rem_ref_cm_node(cm_core, cm_node);
2140         } while (0);
2141         return skb_handled;
2142 }
2143
2144
2145 /**
2146  * nes_cm_alloc_core - allocate a top level instance of a cm core
2147  */
2148 static struct nes_cm_core *nes_cm_alloc_core(void)
2149 {
2150         struct nes_cm_core *cm_core;
2151
2152         /* setup the CM core */
2153         /* alloc top level core control structure */
2154         cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2155         if (!cm_core)
2156                 return NULL;
2157
2158         INIT_LIST_HEAD(&cm_core->connected_nodes);
2159         init_timer(&cm_core->tcp_timer);
2160         cm_core->tcp_timer.function = nes_cm_timer_tick;
2161
2162         cm_core->mtu   = NES_CM_DEFAULT_MTU;
2163         cm_core->state = NES_CM_STATE_INITED;
2164         cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2165
2166         atomic_set(&cm_core->events_posted, 0);
2167
2168         cm_core->api = &nes_cm_api;
2169
2170         spin_lock_init(&cm_core->ht_lock);
2171         spin_lock_init(&cm_core->listen_list_lock);
2172
2173         INIT_LIST_HEAD(&cm_core->listen_list.list);
2174
2175         nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2176
2177         nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2178         cm_core->event_wq = create_singlethread_workqueue("nesewq");
2179         cm_core->post_event = nes_cm_post_event;
2180         nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2181         cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2182
2183         print_core(cm_core);
2184         return cm_core;
2185 }
2186
2187
2188 /**
2189  * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2190  */
2191 static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
2192 {
2193         nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2194
2195         if (!cm_core)
2196                 return -EINVAL;
2197
2198         barrier();
2199
2200         if (timer_pending(&cm_core->tcp_timer)) {
2201                 del_timer(&cm_core->tcp_timer);
2202         }
2203
2204         destroy_workqueue(cm_core->event_wq);
2205         destroy_workqueue(cm_core->disconn_wq);
2206         nes_debug(NES_DBG_CM, "\n");
2207         kfree(cm_core);
2208
2209         return 0;
2210 }
2211
2212
2213 /**
2214  * mini_cm_get
2215  */
2216 static int mini_cm_get(struct nes_cm_core *cm_core)
2217 {
2218         return cm_core->state;
2219 }
2220
2221
2222 /**
2223  * mini_cm_set
2224  */
2225 static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
2226 {
2227         int ret = 0;
2228
2229         switch (type) {
2230                 case NES_CM_SET_PKT_SIZE:
2231                         cm_core->mtu = value;
2232                         break;
2233                 case NES_CM_SET_FREE_PKT_Q_SIZE:
2234                         cm_core->free_tx_pkt_max = value;
2235                         break;
2236                 default:
2237                         /* unknown set option */
2238                         ret = -EINVAL;
2239         }
2240
2241         return ret;
2242 }
2243
2244
2245 /**
2246  * nes_cm_init_tsa_conn setup HW; MPA frames must be
2247  * successfully exchanged when this is called
2248  */
2249 static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2250 {
2251         int ret = 0;
2252
2253         if (!nesqp)
2254                 return -EINVAL;
2255
2256         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2257                         NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2258                         NES_QPCONTEXT_MISC_DROS);
2259
2260         if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2261                 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2262
2263         nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2264
2265         nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2266
2267         nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2268                         (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2269
2270         nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2271                         (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2272                         NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2273
2274         nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2275                         (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2276                         NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2277
2278         nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2279         nesqp->nesqp_context->ts_recent = 0;
2280         nesqp->nesqp_context->ts_age = 0;
2281         nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2282         nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2283         nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2284         nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2285                         cm_node->tcp_cntxt.rcv_wscale);
2286         nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2287         nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2288         nesqp->nesqp_context->srtt = 0;
2289         nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2290         nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2291         nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2292         nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2293         nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2294         nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2295
2296         nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2297                         " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2298                         nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2299                         le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2300                         cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2301                         le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2302                         le32_to_cpu(nesqp->nesqp_context->misc));
2303         nes_debug(NES_DBG_CM, "  snd_wnd  = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2304         nes_debug(NES_DBG_CM, "  snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2305         nes_debug(NES_DBG_CM, "  max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2306
2307         nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2308         cm_node->state = NES_CM_STATE_TSA;
2309
2310         return ret;
2311 }
2312
2313
2314 /**
2315  * nes_cm_disconn
2316  */
2317 int nes_cm_disconn(struct nes_qp *nesqp)
2318 {
2319         unsigned long flags;
2320
2321         spin_lock_irqsave(&nesqp->lock, flags);
2322         if (nesqp->disconn_pending == 0) {
2323                 nesqp->disconn_pending++;
2324                 spin_unlock_irqrestore(&nesqp->lock, flags);
2325                 /* init our disconnect work element, to */
2326                 INIT_WORK(&nesqp->disconn_work, nes_disconnect_worker);
2327
2328                 queue_work(g_cm_core->disconn_wq, &nesqp->disconn_work);
2329         } else
2330                 spin_unlock_irqrestore(&nesqp->lock, flags);
2331
2332         return 0;
2333 }
2334
2335
2336 /**
2337  * nes_disconnect_worker
2338  */
2339 static void nes_disconnect_worker(struct work_struct *work)
2340 {
2341         struct nes_qp *nesqp = container_of(work, struct nes_qp, disconn_work);
2342
2343         nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2344                         nesqp->last_aeq, nesqp->hwqp.qp_id);
2345         nes_cm_disconn_true(nesqp);
2346 }
2347
2348
2349 /**
2350  * nes_cm_disconn_true
2351  */
2352 static int nes_cm_disconn_true(struct nes_qp *nesqp)
2353 {
2354         unsigned long flags;
2355         int ret = 0;
2356         struct iw_cm_id *cm_id;
2357         struct iw_cm_event cm_event;
2358         struct nes_vnic *nesvnic;
2359         u16 last_ae;
2360         u8 original_hw_tcp_state;
2361         u8 original_ibqp_state;
2362         u8 issued_disconnect_reset = 0;
2363
2364         if (!nesqp) {
2365                 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2366                 return -1;
2367         }
2368
2369         spin_lock_irqsave(&nesqp->lock, flags);
2370         cm_id = nesqp->cm_id;
2371         /* make sure we havent already closed this connection */
2372         if (!cm_id) {
2373                 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2374                                 nesqp->hwqp.qp_id);
2375                 spin_unlock_irqrestore(&nesqp->lock, flags);
2376                 return -1;
2377         }
2378
2379         nesvnic = to_nesvnic(nesqp->ibqp.device);
2380         nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2381
2382         original_hw_tcp_state = nesqp->hw_tcp_state;
2383         original_ibqp_state   = nesqp->ibqp_state;
2384         last_ae = nesqp->last_aeq;
2385
2386
2387         nes_debug(NES_DBG_CM, "set ibqp_state=%u\n", nesqp->ibqp_state);
2388
2389         if ((nesqp->cm_id) && (cm_id->event_handler)) {
2390                 if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2391                                 ((original_ibqp_state == IB_QPS_RTS) &&
2392                                 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2393                         atomic_inc(&cm_disconnects);
2394                         cm_event.event = IW_CM_EVENT_DISCONNECT;
2395                         if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET) {
2396                                 cm_event.status = IW_CM_EVENT_STATUS_RESET;
2397                                 nes_debug(NES_DBG_CM, "Generating a CM "
2398                                         "Disconnect Event (status reset) for "
2399                                         "QP%u, cm_id = %p. \n",
2400                                         nesqp->hwqp.qp_id, cm_id);
2401                         } else
2402                                 cm_event.status = IW_CM_EVENT_STATUS_OK;
2403
2404                         cm_event.local_addr = cm_id->local_addr;
2405                         cm_event.remote_addr = cm_id->remote_addr;
2406                         cm_event.private_data = NULL;
2407                         cm_event.private_data_len = 0;
2408
2409                         nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2410                                 " for  QP%u, SQ Head = %u, SQ Tail = %u. "
2411                                 "cm_id = %p, refcount = %u.\n",
2412                                 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2413                                 nesqp->hwqp.sq_tail, cm_id,
2414                                 atomic_read(&nesqp->refcount));
2415
2416                         spin_unlock_irqrestore(&nesqp->lock, flags);
2417                         ret = cm_id->event_handler(cm_id, &cm_event);
2418                         if (ret)
2419                                 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2420                                         "returned, ret=%d\n", ret);
2421                         spin_lock_irqsave(&nesqp->lock, flags);
2422                 }
2423
2424                 nesqp->disconn_pending = 0;
2425                 /* There might have been another AE while the lock was released */
2426                 original_hw_tcp_state = nesqp->hw_tcp_state;
2427                 original_ibqp_state   = nesqp->ibqp_state;
2428                 last_ae = nesqp->last_aeq;
2429
2430                 if ((issued_disconnect_reset == 0) && (nesqp->cm_id) &&
2431                                 ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2432                                  (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2433                                  (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2434                                  (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2435                         atomic_inc(&cm_closes);
2436                         nesqp->cm_id = NULL;
2437                         nesqp->in_disconnect = 0;
2438                         spin_unlock_irqrestore(&nesqp->lock, flags);
2439                         nes_disconnect(nesqp, 1);
2440
2441                         cm_id->provider_data = nesqp;
2442                         /* Send up the close complete event */
2443                         cm_event.event = IW_CM_EVENT_CLOSE;
2444                         cm_event.status = IW_CM_EVENT_STATUS_OK;
2445                         cm_event.provider_data = cm_id->provider_data;
2446                         cm_event.local_addr = cm_id->local_addr;
2447                         cm_event.remote_addr = cm_id->remote_addr;
2448                         cm_event.private_data = NULL;
2449                         cm_event.private_data_len = 0;
2450
2451                         ret = cm_id->event_handler(cm_id, &cm_event);
2452                         if (ret) {
2453                                 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2454                         }
2455
2456                         cm_id->rem_ref(cm_id);
2457
2458                         spin_lock_irqsave(&nesqp->lock, flags);
2459                         if (nesqp->flush_issued == 0) {
2460                                 nesqp->flush_issued = 1;
2461                                 spin_unlock_irqrestore(&nesqp->lock, flags);
2462                                 flush_wqes(nesvnic->nesdev, nesqp,
2463                                         NES_CQP_FLUSH_RQ, 1);
2464                         } else
2465                                 spin_unlock_irqrestore(&nesqp->lock, flags);
2466                 } else {
2467                         cm_id = nesqp->cm_id;
2468                         spin_unlock_irqrestore(&nesqp->lock, flags);
2469                         /* check to see if the inbound reset beat the outbound reset */
2470                         if ((!cm_id) && (last_ae==NES_AEQE_AEID_RESET_SENT)) {
2471                                 nes_debug(NES_DBG_CM, "QP%u: Decing refcount "
2472                                         "due to inbound reset beating the "
2473                                         "outbound reset.\n", nesqp->hwqp.qp_id);
2474                         }
2475                 }
2476         } else {
2477                 nesqp->disconn_pending = 0;
2478                 spin_unlock_irqrestore(&nesqp->lock, flags);
2479         }
2480
2481         return 0;
2482 }
2483
2484
2485 /**
2486  * nes_disconnect
2487  */
2488 static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
2489 {
2490         int ret = 0;
2491         struct nes_vnic *nesvnic;
2492         struct nes_device *nesdev;
2493
2494         nesvnic = to_nesvnic(nesqp->ibqp.device);
2495         if (!nesvnic)
2496                 return -EINVAL;
2497
2498         nesdev = nesvnic->nesdev;
2499
2500         nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2501                         atomic_read(&nesvnic->netdev->refcnt));
2502
2503         if (nesqp->active_conn) {
2504
2505                 /* indicate this connection is NOT active */
2506                 nesqp->active_conn = 0;
2507         } else {
2508                 /* Need to free the Last Streaming Mode Message */
2509                 if (nesqp->ietf_frame) {
2510                         pci_free_consistent(nesdev->pcidev,
2511                                         nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2512                                         nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2513                 }
2514         }
2515
2516         /* close the CM node down if it is still active */
2517         if (nesqp->cm_node) {
2518                 nes_debug(NES_DBG_CM, "Call close API\n");
2519
2520                 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
2521         }
2522
2523         return ret;
2524 }
2525
2526
2527 /**
2528  * nes_accept
2529  */
2530 int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2531 {
2532         u64 u64temp;
2533         struct ib_qp *ibqp;
2534         struct nes_qp *nesqp;
2535         struct nes_vnic *nesvnic;
2536         struct nes_device *nesdev;
2537         struct nes_cm_node *cm_node;
2538         struct nes_adapter *adapter;
2539         struct ib_qp_attr attr;
2540         struct iw_cm_event cm_event;
2541         struct nes_hw_qp_wqe *wqe;
2542         struct nes_v4_quad nes_quad;
2543         u32 crc_value;
2544         int ret;
2545         int passive_state;
2546
2547         ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2548         if (!ibqp)
2549                 return -EINVAL;
2550
2551         /* get all our handles */
2552         nesqp = to_nesqp(ibqp);
2553         nesvnic = to_nesvnic(nesqp->ibqp.device);
2554         nesdev = nesvnic->nesdev;
2555         adapter = nesdev->nesadapter;
2556
2557         cm_node = (struct nes_cm_node *)cm_id->provider_data;
2558         nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2559                 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2560                 nesvnic->netdev->name);
2561
2562         /* associate the node with the QP */
2563         nesqp->cm_node = (void *)cm_node;
2564         cm_node->nesqp = nesqp;
2565         nes_add_ref(&nesqp->ibqp);
2566
2567         nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2568                 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
2569         atomic_inc(&cm_accepts);
2570
2571         nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2572                         atomic_read(&nesvnic->netdev->refcnt));
2573
2574         /* allocate the ietf frame and space for private data */
2575         nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2576                 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2577                 &nesqp->ietf_frame_pbase);
2578
2579         if (!nesqp->ietf_frame) {
2580                 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2581                         "data\n");
2582                 return -ENOMEM;
2583         }
2584
2585
2586         /* setup the MPA frame */
2587         nesqp->private_data_len = conn_param->private_data_len;
2588         memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
2589
2590         memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2591                         conn_param->private_data_len);
2592
2593         nesqp->ietf_frame->priv_data_len =
2594                 cpu_to_be16(conn_param->private_data_len);
2595         nesqp->ietf_frame->rev = mpa_version;
2596         nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
2597
2598         /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2599         wqe = &nesqp->hwqp.sq_vbase[0];
2600
2601         if (cm_id->remote_addr.sin_addr.s_addr !=
2602                         cm_id->local_addr.sin_addr.s_addr) {
2603                 u64temp = (unsigned long)nesqp;
2604                 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2605                 set_wqe_64bit_value(wqe->wqe_words,
2606                         NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2607                         u64temp);
2608                 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2609                         cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2610                         NES_IWARP_SQ_WQE_WRPDU);
2611                 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2612                         cpu_to_le32(conn_param->private_data_len +
2613                         sizeof(struct ietf_mpa_frame));
2614                 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] =
2615                         cpu_to_le32((u32)nesqp->ietf_frame_pbase);
2616                 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] =
2617                         cpu_to_le32((u32)((u64)nesqp->ietf_frame_pbase >> 32));
2618                 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2619                         cpu_to_le32(conn_param->private_data_len +
2620                         sizeof(struct ietf_mpa_frame));
2621                 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
2622
2623                 nesqp->nesqp_context->ird_ord_sizes |=
2624                         cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2625                         NES_QPCONTEXT_ORDIRD_WRPDU);
2626         } else {
2627                 nesqp->nesqp_context->ird_ord_sizes |=
2628                         cpu_to_le32((NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2629                         NES_QPCONTEXT_ORDIRD_WRPDU |
2630                         NES_QPCONTEXT_ORDIRD_ALSMM));
2631         }
2632         nesqp->skip_lsmm = 1;
2633
2634
2635         /* Cache the cm_id in the qp */
2636         nesqp->cm_id = cm_id;
2637         cm_node->cm_id = cm_id;
2638
2639         /*  nesqp->cm_node = (void *)cm_id->provider_data; */
2640         cm_id->provider_data = nesqp;
2641         nesqp->active_conn   = 0;
2642
2643         if (cm_node->state == NES_CM_STATE_TSA)
2644                 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2645                         cm_node);
2646
2647         nes_cm_init_tsa_conn(nesqp, cm_node);
2648
2649         nesqp->nesqp_context->tcpPorts[0] =
2650                 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2651         nesqp->nesqp_context->tcpPorts[1] =
2652                 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2653
2654         if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2655                 nesqp->nesqp_context->ip0 =
2656                         cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2657         else
2658                 nesqp->nesqp_context->ip0 =
2659                         cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
2660
2661         nesqp->nesqp_context->misc2 |= cpu_to_le32(
2662                         (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2663                         NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
2664
2665         nesqp->nesqp_context->arp_index_vlan |=
2666                 cpu_to_le32(nes_arp_table(nesdev,
2667                         le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
2668                         NES_ARP_RESOLVE) << 16);
2669
2670         nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
2671                 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
2672
2673         nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2674
2675         nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
2676                 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2677         nesqp->nesqp_context->ird_ord_sizes |=
2678                 cpu_to_le32((u32)conn_param->ord);
2679
2680         memset(&nes_quad, 0, sizeof(nes_quad));
2681         nes_quad.DstIpAdrIndex =
2682                 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2683         if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2684                 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2685         else
2686                 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2687         nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2688         nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
2689
2690         /* Produce hash key */
2691         crc_value = get_crc_value(&nes_quad);
2692         nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
2693         nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
2694                 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
2695
2696         nesqp->hte_index &= adapter->hte_index_mask;
2697         nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2698
2699         cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2700
2701         nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2702                         "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2703                         "private data length=%zu.\n", nesqp->hwqp.qp_id,
2704                         ntohl(cm_id->remote_addr.sin_addr.s_addr),
2705                         ntohs(cm_id->remote_addr.sin_port),
2706                         ntohl(cm_id->local_addr.sin_addr.s_addr),
2707                         ntohs(cm_id->local_addr.sin_port),
2708                         le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2709                         le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2710                         conn_param->private_data_len +
2711                         sizeof(struct ietf_mpa_frame));
2712
2713
2714         /* notify OF layer that accept event was successful */
2715         cm_id->add_ref(cm_id);
2716
2717         cm_event.event = IW_CM_EVENT_ESTABLISHED;
2718         cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2719         cm_event.provider_data = (void *)nesqp;
2720         cm_event.local_addr = cm_id->local_addr;
2721         cm_event.remote_addr = cm_id->remote_addr;
2722         cm_event.private_data = NULL;
2723         cm_event.private_data_len = 0;
2724         ret = cm_id->event_handler(cm_id, &cm_event);
2725         attr.qp_state = IB_QPS_RTS;
2726         nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
2727         if (cm_node->loopbackpartner) {
2728                 cm_node->loopbackpartner->mpa_frame_size =
2729                         nesqp->private_data_len;
2730                 /* copy entire MPA frame to our cm_node's frame */
2731                 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2732                         nesqp->ietf_frame->priv_data, nesqp->private_data_len);
2733                 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2734         }
2735         if (ret)
2736                 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2737                         "ret=%d\n", __func__, __LINE__, ret);
2738
2739         passive_state = atomic_add_return(1, &cm_node->passive_state);
2740         if (passive_state == NES_SEND_RESET_EVENT)
2741                 create_event(cm_node, NES_CM_EVENT_RESET);
2742         return 0;
2743 }
2744
2745
2746 /**
2747  * nes_reject
2748  */
2749 int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2750 {
2751         struct nes_cm_node *cm_node;
2752         struct nes_cm_core *cm_core;
2753
2754         atomic_inc(&cm_rejects);
2755         cm_node = (struct nes_cm_node *) cm_id->provider_data;
2756         cm_core = cm_node->cm_core;
2757         cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2758
2759         strcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP);
2760         memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2761
2762         cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2763         cm_node->mpa_frame.rev = mpa_version;
2764         cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2765
2766         cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
2767
2768         return 0;
2769 }
2770
2771
2772 /**
2773  * nes_connect
2774  * setup and launch cm connect node
2775  */
2776 int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2777 {
2778         struct ib_qp *ibqp;
2779         struct nes_qp *nesqp;
2780         struct nes_vnic *nesvnic;
2781         struct nes_device *nesdev;
2782         struct nes_cm_node *cm_node;
2783         struct nes_cm_info cm_info;
2784
2785         ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2786         if (!ibqp)
2787                 return -EINVAL;
2788         nesqp = to_nesqp(ibqp);
2789         if (!nesqp)
2790                 return -EINVAL;
2791         nesvnic = to_nesvnic(nesqp->ibqp.device);
2792         if (!nesvnic)
2793                 return -EINVAL;
2794         nesdev  = nesvnic->nesdev;
2795         if (!nesdev)
2796                 return -EINVAL;
2797
2798         nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
2799                 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
2800                 ntohl(nesvnic->local_ipaddr),
2801                 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2802                 ntohs(cm_id->remote_addr.sin_port),
2803                 ntohl(cm_id->local_addr.sin_addr.s_addr),
2804                 ntohs(cm_id->local_addr.sin_port));
2805
2806         atomic_inc(&cm_connects);
2807         nesqp->active_conn = 1;
2808
2809         /* cache the cm_id in the qp */
2810         nesqp->cm_id = cm_id;
2811
2812         cm_id->provider_data = nesqp;
2813
2814         nesqp->private_data_len = conn_param->private_data_len;
2815         nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
2816         nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
2817         nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
2818                 conn_param->private_data_len);
2819
2820         if (cm_id->local_addr.sin_addr.s_addr !=
2821                 cm_id->remote_addr.sin_addr.s_addr)
2822                 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
2823                         PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
2824
2825         /* set up the connection params for the node */
2826         cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
2827         cm_info.loc_port = htons(cm_id->local_addr.sin_port);
2828         cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
2829         cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
2830         cm_info.cm_id = cm_id;
2831         cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
2832
2833         cm_id->add_ref(cm_id);
2834
2835         /* create a connect CM node connection */
2836         cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
2837                 conn_param->private_data_len, (void *)conn_param->private_data,
2838                 &cm_info);
2839         if (!cm_node) {
2840                 if (cm_id->local_addr.sin_addr.s_addr !=
2841                                 cm_id->remote_addr.sin_addr.s_addr)
2842                         nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
2843                                 PCI_FUNC(nesdev->pcidev->devfn),
2844                                 NES_MANAGE_APBVT_DEL);
2845
2846                 cm_id->rem_ref(cm_id);
2847                 return -ENOMEM;
2848         }
2849
2850         cm_node->apbvt_set = 1;
2851         nesqp->cm_node = cm_node;
2852         cm_node->nesqp = nesqp;
2853         nes_add_ref(&nesqp->ibqp);
2854
2855         return 0;
2856 }
2857
2858
2859 /**
2860  * nes_create_listen
2861  */
2862 int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
2863 {
2864         struct nes_vnic *nesvnic;
2865         struct nes_cm_listener *cm_node;
2866         struct nes_cm_info cm_info;
2867         struct nes_adapter *adapter;
2868         int err;
2869
2870
2871         nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
2872                         cm_id, ntohs(cm_id->local_addr.sin_port));
2873
2874         nesvnic = to_nesvnic(cm_id->device);
2875         if (!nesvnic)
2876                 return -EINVAL;
2877         adapter = nesvnic->nesdev->nesadapter;
2878         nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
2879                         nesvnic, nesvnic->netdev, nesvnic->netdev->name);
2880
2881         nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
2882                         nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
2883
2884         /* setup listen params in our api call struct */
2885         cm_info.loc_addr = nesvnic->local_ipaddr;
2886         cm_info.loc_port = cm_id->local_addr.sin_port;
2887         cm_info.backlog = backlog;
2888         cm_info.cm_id = cm_id;
2889
2890         cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
2891
2892
2893         cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
2894         if (!cm_node) {
2895                 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
2896                                 __func__, __LINE__);
2897                 return -ENOMEM;
2898         }
2899
2900         cm_id->provider_data = cm_node;
2901
2902         if (!cm_node->reused_node) {
2903                 err = nes_manage_apbvt(nesvnic,
2904                         ntohs(cm_id->local_addr.sin_port),
2905                         PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
2906                         NES_MANAGE_APBVT_ADD);
2907                 if (err) {
2908                         printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
2909                                 err);
2910                         g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
2911                         return err;
2912                 }
2913                 cm_listens_created++;
2914         }
2915
2916         cm_id->add_ref(cm_id);
2917         cm_id->provider_data = (void *)cm_node;
2918
2919
2920         return 0;
2921 }
2922
2923
2924 /**
2925  * nes_destroy_listen
2926  */
2927 int nes_destroy_listen(struct iw_cm_id *cm_id)
2928 {
2929         if (cm_id->provider_data)
2930                 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
2931         else
2932                 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
2933
2934         cm_id->rem_ref(cm_id);
2935
2936         return 0;
2937 }
2938
2939
2940 /**
2941  * nes_cm_recv
2942  */
2943 int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
2944 {
2945         int rc = 0;
2946         cm_packets_received++;
2947         if ((g_cm_core) && (g_cm_core->api)) {
2948                 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
2949         } else {
2950                 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
2951                                 " cm is not setup properly.\n");
2952         }
2953
2954         return rc;
2955 }
2956
2957
2958 /**
2959  * nes_cm_start
2960  * Start and init a cm core module
2961  */
2962 int nes_cm_start(void)
2963 {
2964         nes_debug(NES_DBG_CM, "\n");
2965         /* create the primary CM core, pass this handle to subsequent core inits */
2966         g_cm_core = nes_cm_alloc_core();
2967         if (g_cm_core) {
2968                 return 0;
2969         } else {
2970                 return -ENOMEM;
2971         }
2972 }
2973
2974
2975 /**
2976  * nes_cm_stop
2977  * stop and dealloc all cm core instances
2978  */
2979 int nes_cm_stop(void)
2980 {
2981         g_cm_core->api->destroy_cm_core(g_cm_core);
2982         return 0;
2983 }
2984
2985
2986 /**
2987  * cm_event_connected
2988  * handle a connected event, setup QPs and HW
2989  */
2990 static void cm_event_connected(struct nes_cm_event *event)
2991 {
2992         u64 u64temp;
2993         struct nes_qp *nesqp;
2994         struct nes_vnic *nesvnic;
2995         struct nes_device *nesdev;
2996         struct nes_cm_node *cm_node;
2997         struct nes_adapter *nesadapter;
2998         struct ib_qp_attr attr;
2999         struct iw_cm_id *cm_id;
3000         struct iw_cm_event cm_event;
3001         struct nes_hw_qp_wqe *wqe;
3002         struct nes_v4_quad nes_quad;
3003         u32 crc_value;
3004         int ret;
3005
3006         /* get all our handles */
3007         cm_node = event->cm_node;
3008         cm_id = cm_node->cm_id;
3009         nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3010         nesqp = (struct nes_qp *)cm_id->provider_data;
3011         nesvnic = to_nesvnic(nesqp->ibqp.device);
3012         nesdev = nesvnic->nesdev;
3013         nesadapter = nesdev->nesadapter;
3014
3015         if (nesqp->destroyed) {
3016                 return;
3017         }
3018         atomic_inc(&cm_connecteds);
3019         nes_debug(NES_DBG_CM, "QP%u attempting to connect to  0x%08X:0x%04X on"
3020                         " local port 0x%04X. jiffies = %lu.\n",
3021                         nesqp->hwqp.qp_id,
3022                         ntohl(cm_id->remote_addr.sin_addr.s_addr),
3023                         ntohs(cm_id->remote_addr.sin_port),
3024                         ntohs(cm_id->local_addr.sin_port),
3025                         jiffies);
3026
3027         nes_cm_init_tsa_conn(nesqp, cm_node);
3028
3029         /* set the QP tsa context */
3030         nesqp->nesqp_context->tcpPorts[0] =
3031                 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3032         nesqp->nesqp_context->tcpPorts[1] =
3033                 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3034         if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3035                 nesqp->nesqp_context->ip0 =
3036                         cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3037         else
3038                 nesqp->nesqp_context->ip0 =
3039                         cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
3040
3041         nesqp->nesqp_context->misc2 |= cpu_to_le32(
3042                         (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3043                         NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
3044         nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
3045                         nes_arp_table(nesdev,
3046                         le32_to_cpu(nesqp->nesqp_context->ip0),
3047                         NULL, NES_ARP_RESOLVE) << 16);
3048         nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3049                         jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3050         nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3051         nesqp->nesqp_context->ird_ord_sizes |=
3052                         cpu_to_le32((u32)1 <<
3053                         NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
3054
3055         /* Adjust tail for not having a LSMM */
3056         nesqp->hwqp.sq_tail = 1;
3057
3058 #if defined(NES_SEND_FIRST_WRITE)
3059         if (cm_node->send_write0) {
3060                 nes_debug(NES_DBG_CM, "Sending first write.\n");
3061                 wqe = &nesqp->hwqp.sq_vbase[0];
3062                 u64temp = (unsigned long)nesqp;
3063                 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3064                 set_wqe_64bit_value(wqe->wqe_words,
3065                                 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3066                 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3067                         cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3068                 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3069                 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3070                 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3071                 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3072                 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
3073
3074                 /* use the reserved spot on the WQ for the extra first WQE */
3075                 nesqp->nesqp_context->ird_ord_sizes &=
3076                         cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3077                                                 NES_QPCONTEXT_ORDIRD_WRPDU |
3078                                                 NES_QPCONTEXT_ORDIRD_ALSMM));
3079                 nesqp->skip_lsmm = 1;
3080                 nesqp->hwqp.sq_tail = 0;
3081                 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3082                                 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3083         }
3084 #endif
3085
3086         memset(&nes_quad, 0, sizeof(nes_quad));
3087
3088         nes_quad.DstIpAdrIndex =
3089                 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3090         if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3091                 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3092         else
3093                 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
3094         nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3095         nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3096
3097         /* Produce hash key */
3098         crc_value = get_crc_value(&nes_quad);
3099         nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
3100         nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3101                         nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3102
3103         nesqp->hte_index &= nesadapter->hte_index_mask;
3104         nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3105
3106         nesqp->ietf_frame = &cm_node->mpa_frame;
3107         nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3108         cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3109
3110         /* notify OF layer we successfully created the requested connection */
3111         cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3112         cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3113         cm_event.provider_data = cm_id->provider_data;
3114         cm_event.local_addr.sin_family = AF_INET;
3115         cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3116         cm_event.remote_addr = cm_id->remote_addr;
3117
3118         cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3119         cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
3120
3121         cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3122         ret = cm_id->event_handler(cm_id, &cm_event);
3123         nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3124
3125         if (ret)
3126                 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3127                         "ret=%d\n", __func__, __LINE__, ret);
3128         attr.qp_state = IB_QPS_RTS;
3129         nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
3130
3131         nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3132                 "%lu\n", nesqp->hwqp.qp_id, jiffies);
3133
3134         return;
3135 }
3136
3137
3138 /**
3139  * cm_event_connect_error
3140  */
3141 static void cm_event_connect_error(struct nes_cm_event *event)
3142 {
3143         struct nes_qp *nesqp;
3144         struct iw_cm_id *cm_id;
3145         struct iw_cm_event cm_event;
3146         /* struct nes_cm_info cm_info; */
3147         int ret;
3148
3149         if (!event->cm_node)
3150                 return;
3151
3152         cm_id = event->cm_node->cm_id;
3153         if (!cm_id) {
3154                 return;
3155         }
3156
3157         nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3158         nesqp = cm_id->provider_data;
3159
3160         if (!nesqp) {
3161                 return;
3162         }
3163
3164         /* notify OF layer about this connection error event */
3165         /* cm_id->rem_ref(cm_id); */
3166         nesqp->cm_id = NULL;
3167         cm_id->provider_data = NULL;
3168         cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3169         cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
3170         cm_event.provider_data = cm_id->provider_data;
3171         cm_event.local_addr = cm_id->local_addr;
3172         cm_event.remote_addr = cm_id->remote_addr;
3173         cm_event.private_data = NULL;
3174         cm_event.private_data_len = 0;
3175
3176         nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3177                 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3178                 cm_event.remote_addr.sin_addr.s_addr);
3179
3180         ret = cm_id->event_handler(cm_id, &cm_event);
3181         nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3182         if (ret)
3183                 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3184                         "ret=%d\n", __func__, __LINE__, ret);
3185         cm_id->rem_ref(cm_id);
3186
3187         rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
3188         return;
3189 }
3190
3191
3192 /**
3193  * cm_event_reset
3194  */
3195 static void cm_event_reset(struct nes_cm_event *event)
3196 {
3197         struct nes_qp *nesqp;
3198         struct iw_cm_id *cm_id;
3199         struct iw_cm_event cm_event;
3200         /* struct nes_cm_info cm_info; */
3201         int ret;
3202
3203         if (!event->cm_node)
3204                 return;
3205
3206         if (!event->cm_node->cm_id)
3207                 return;
3208
3209         cm_id = event->cm_node->cm_id;
3210
3211         nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3212         nesqp = cm_id->provider_data;
3213
3214         nesqp->cm_id = NULL;
3215         /* cm_id->provider_data = NULL; */
3216         cm_event.event = IW_CM_EVENT_DISCONNECT;
3217         cm_event.status = IW_CM_EVENT_STATUS_RESET;
3218         cm_event.provider_data = cm_id->provider_data;
3219         cm_event.local_addr = cm_id->local_addr;
3220         cm_event.remote_addr = cm_id->remote_addr;
3221         cm_event.private_data = NULL;
3222         cm_event.private_data_len = 0;
3223
3224         ret = cm_id->event_handler(cm_id, &cm_event);
3225         cm_id->add_ref(cm_id);
3226         atomic_inc(&cm_closes);
3227         cm_event.event = IW_CM_EVENT_CLOSE;
3228         cm_event.status = IW_CM_EVENT_STATUS_OK;
3229         cm_event.provider_data = cm_id->provider_data;
3230         cm_event.local_addr = cm_id->local_addr;
3231         cm_event.remote_addr = cm_id->remote_addr;
3232         cm_event.private_data = NULL;
3233         cm_event.private_data_len = 0;
3234         nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3235         ret = cm_id->event_handler(cm_id, &cm_event);
3236
3237         nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3238
3239
3240         /* notify OF layer about this connection error event */
3241         cm_id->rem_ref(cm_id);
3242
3243         return;
3244 }
3245
3246
3247 /**
3248  * cm_event_mpa_req
3249  */
3250 static void cm_event_mpa_req(struct nes_cm_event *event)
3251 {
3252         struct iw_cm_id   *cm_id;
3253         struct iw_cm_event cm_event;
3254         int ret;
3255         struct nes_cm_node *cm_node;
3256
3257         cm_node = event->cm_node;
3258         if (!cm_node)
3259                 return;
3260         cm_id = cm_node->cm_id;
3261
3262         atomic_inc(&cm_connect_reqs);
3263         nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3264                         cm_node, cm_id, jiffies);
3265
3266         cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3267         cm_event.status = IW_CM_EVENT_STATUS_OK;
3268         cm_event.provider_data = (void *)cm_node;
3269
3270         cm_event.local_addr.sin_family = AF_INET;
3271         cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3272         cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3273
3274         cm_event.remote_addr.sin_family = AF_INET;
3275         cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3276         cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3277
3278                 cm_event.private_data                = cm_node->mpa_frame_buf;
3279                 cm_event.private_data_len            = (u8) cm_node->mpa_frame_size;
3280
3281         ret = cm_id->event_handler(cm_id, &cm_event);
3282         if (ret)
3283                 printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
3284                                 __func__, __LINE__, ret);
3285
3286         return;
3287 }
3288
3289
3290 static void nes_cm_event_handler(struct work_struct *);
3291
3292 /**
3293  * nes_cm_post_event
3294  * post an event to the cm event handler
3295  */
3296 static int nes_cm_post_event(struct nes_cm_event *event)
3297 {
3298         atomic_inc(&event->cm_node->cm_core->events_posted);
3299         add_ref_cm_node(event->cm_node);
3300         event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3301         INIT_WORK(&event->event_work, nes_cm_event_handler);
3302         nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3303                 event->cm_node, event);
3304
3305         queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3306
3307         nes_debug(NES_DBG_CM, "Exit\n");
3308         return 0;
3309 }
3310
3311
3312 /**
3313  * nes_cm_event_handler
3314  * worker function to handle cm events
3315  * will free instance of nes_cm_event
3316  */
3317 static void nes_cm_event_handler(struct work_struct *work)
3318 {
3319         struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3320                         event_work);
3321         struct nes_cm_core *cm_core;
3322
3323         if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
3324                 return;
3325
3326         cm_core = event->cm_node->cm_core;
3327         nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
3328                 event, event->type, atomic_read(&cm_core->events_posted));
3329
3330         switch (event->type) {
3331         case NES_CM_EVENT_MPA_REQ:
3332                 cm_event_mpa_req(event);
3333                 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3334                         event->cm_node);
3335                 break;
3336         case NES_CM_EVENT_RESET:
3337                 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3338                         event->cm_node);
3339                 cm_event_reset(event);
3340                 break;
3341         case NES_CM_EVENT_CONNECTED:
3342                 if ((!event->cm_node->cm_id) ||
3343                         (event->cm_node->state != NES_CM_STATE_TSA))
3344                         break;
3345                 cm_event_connected(event);
3346                 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3347                 break;
3348         case NES_CM_EVENT_ABORTED:
3349                 if ((!event->cm_node->cm_id) ||
3350                         (event->cm_node->state == NES_CM_STATE_TSA))
3351                         break;
3352                 cm_event_connect_error(event);
3353                 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3354                 break;
3355         case NES_CM_EVENT_DROPPED_PKT:
3356                 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3357                 break;
3358         default:
3359                 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3360                 break;
3361         }
3362
3363         atomic_dec(&cm_core->events_posted);
3364         event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3365         rem_ref_cm_node(cm_core, event->cm_node);
3366         kfree(event);
3367
3368         return;
3369 }