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