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