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