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