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