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