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