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