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