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