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