x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / drivers / infiniband / hw / nes / nes_cm.c
blob73473db1986361f2a390a30b09c942f74e62ca96
1 /*
2 * Copyright (c) 2006 - 2009 Intel-NE, Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
35 #define TCPOPT_TIMESTAMP 8
37 #include <asm/atomic.h>
38 #include <linux/skbuff.h>
39 #include <linux/ip.h>
40 #include <linux/tcp.h>
41 #include <linux/init.h>
42 #include <linux/if_arp.h>
43 #include <linux/if_vlan.h>
44 #include <linux/notifier.h>
45 #include <linux/net.h>
46 #include <linux/types.h>
47 #include <linux/timer.h>
48 #include <linux/time.h>
49 #include <linux/delay.h>
50 #include <linux/etherdevice.h>
51 #include <linux/netdevice.h>
52 #include <linux/random.h>
53 #include <linux/list.h>
54 #include <linux/threads.h>
55 #include <net/arp.h>
56 #include <net/neighbour.h>
57 #include <net/route.h>
58 #include <net/ip_fib.h>
59 #include <net/tcp.h>
61 #include "nes.h"
63 u32 cm_packets_sent;
64 u32 cm_packets_bounced;
65 u32 cm_packets_dropped;
66 u32 cm_packets_retrans;
67 u32 cm_packets_created;
68 u32 cm_packets_received;
69 u32 cm_listens_created;
70 u32 cm_listens_destroyed;
71 u32 cm_backlog_drops;
72 atomic_t cm_loopbacks;
73 atomic_t cm_nodes_created;
74 atomic_t cm_nodes_destroyed;
75 atomic_t cm_accel_dropped_pkts;
76 atomic_t cm_resets_recvd;
78 static inline int mini_cm_accelerated(struct nes_cm_core *,
79 struct nes_cm_node *);
80 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
81 struct nes_vnic *, struct nes_cm_info *);
82 static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
83 static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
84 struct nes_vnic *, u16, void *, struct nes_cm_info *);
85 static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
86 static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
87 struct nes_cm_node *);
88 static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
89 struct nes_cm_node *);
90 static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
91 struct sk_buff *);
92 static int mini_cm_dealloc_core(struct nes_cm_core *);
93 static int mini_cm_get(struct nes_cm_core *);
94 static int mini_cm_set(struct nes_cm_core *, u32, u32);
96 static void form_cm_frame(struct sk_buff *, struct nes_cm_node *,
97 void *, u32, void *, u32, u8);
98 static int add_ref_cm_node(struct nes_cm_node *);
99 static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
101 static int nes_cm_disconn_true(struct nes_qp *);
102 static int nes_cm_post_event(struct nes_cm_event *event);
103 static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
104 static void nes_disconnect_worker(struct work_struct *work);
106 static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
107 static int send_mpa_reject(struct nes_cm_node *);
108 static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
109 static int send_reset(struct nes_cm_node *, struct sk_buff *);
110 static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
111 static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
112 static void process_packet(struct nes_cm_node *, struct sk_buff *,
113 struct nes_cm_core *);
115 static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
116 static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
117 static void cleanup_retrans_entry(struct nes_cm_node *);
118 static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
119 static void free_retrans_entry(struct nes_cm_node *cm_node);
120 static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
121 struct sk_buff *skb, int optionsize, int passive);
123 /* CM event handler functions */
124 static void cm_event_connected(struct nes_cm_event *);
125 static void cm_event_connect_error(struct nes_cm_event *);
126 static void cm_event_reset(struct nes_cm_event *);
127 static void cm_event_mpa_req(struct nes_cm_event *);
128 static void cm_event_mpa_reject(struct nes_cm_event *);
129 static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
131 static void print_core(struct nes_cm_core *core);
133 /* External CM API Interface */
134 /* instance of function pointers for client API */
135 /* set address of this instance to cm_core->cm_ops at cm_core alloc */
136 static struct nes_cm_ops nes_cm_api = {
137 mini_cm_accelerated,
138 mini_cm_listen,
139 mini_cm_del_listen,
140 mini_cm_connect,
141 mini_cm_close,
142 mini_cm_accept,
143 mini_cm_reject,
144 mini_cm_recv_pkt,
145 mini_cm_dealloc_core,
146 mini_cm_get,
147 mini_cm_set
150 static struct nes_cm_core *g_cm_core;
152 atomic_t cm_connects;
153 atomic_t cm_accepts;
154 atomic_t cm_disconnects;
155 atomic_t cm_closes;
156 atomic_t cm_connecteds;
157 atomic_t cm_connect_reqs;
158 atomic_t cm_rejects;
162 * create_event
164 static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
165 enum nes_cm_event_type type)
167 struct nes_cm_event *event;
169 if (!cm_node->cm_id)
170 return NULL;
172 /* allocate an empty event */
173 event = kzalloc(sizeof(*event), GFP_ATOMIC);
175 if (!event)
176 return NULL;
178 event->type = type;
179 event->cm_node = cm_node;
180 event->cm_info.rem_addr = cm_node->rem_addr;
181 event->cm_info.loc_addr = cm_node->loc_addr;
182 event->cm_info.rem_port = cm_node->rem_port;
183 event->cm_info.loc_port = cm_node->loc_port;
184 event->cm_info.cm_id = cm_node->cm_id;
186 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
187 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
188 cm_node, event, type, event->cm_info.loc_addr,
189 event->cm_info.loc_port, event->cm_info.rem_addr,
190 event->cm_info.rem_port);
192 nes_cm_post_event(event);
193 return event;
198 * send_mpa_request
200 static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
202 if (!skb) {
203 nes_debug(NES_DBG_CM, "skb set to NULL\n");
204 return -1;
207 /* send an MPA Request frame */
208 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
209 cm_node->mpa_frame_size, SET_ACK);
211 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
216 static int send_mpa_reject(struct nes_cm_node *cm_node)
218 struct sk_buff *skb = NULL;
220 skb = dev_alloc_skb(MAX_CM_BUFFER);
221 if (!skb) {
222 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
223 return -ENOMEM;
226 /* send an MPA reject frame */
227 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
228 cm_node->mpa_frame_size, SET_ACK | SET_FIN);
230 cm_node->state = NES_CM_STATE_FIN_WAIT1;
231 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
236 * recv_mpa - process a received TCP pkt, we are expecting an
237 * IETF MPA frame
239 static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
240 u32 len)
242 struct ietf_mpa_frame *mpa_frame;
244 *type = NES_MPA_REQUEST_ACCEPT;
246 /* assume req frame is in tcp data payload */
247 if (len < sizeof(struct ietf_mpa_frame)) {
248 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
249 return -EINVAL;
252 mpa_frame = (struct ietf_mpa_frame *)buffer;
253 cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
255 if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
256 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
257 " complete (%x + %x != %x)\n",
258 cm_node->mpa_frame_size,
259 (u32)sizeof(struct ietf_mpa_frame), len);
260 return -EINVAL;
262 /* make sure it does not exceed the max size */
263 if (len > MAX_CM_BUFFER) {
264 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
265 " (%x + %x != %x)\n",
266 cm_node->mpa_frame_size,
267 (u32)sizeof(struct ietf_mpa_frame), len);
268 return -EINVAL;
271 /* copy entire MPA frame to our cm_node's frame */
272 memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
273 cm_node->mpa_frame_size);
275 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
276 *type = NES_MPA_REQUEST_REJECT;
277 return 0;
282 * form_cm_frame - get a free packet and build empty frame Use
283 * node info to build.
285 static void form_cm_frame(struct sk_buff *skb,
286 struct nes_cm_node *cm_node, void *options, u32 optionsize,
287 void *data, u32 datasize, u8 flags)
289 struct tcphdr *tcph;
290 struct iphdr *iph;
291 struct ethhdr *ethh;
292 u8 *buf;
293 u16 packetsize = sizeof(*iph);
295 packetsize += sizeof(*tcph);
296 packetsize += optionsize + datasize;
298 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
300 skb->len = 0;
301 buf = skb_put(skb, packetsize + ETH_HLEN);
303 ethh = (struct ethhdr *) buf;
304 buf += ETH_HLEN;
306 iph = (struct iphdr *)buf;
307 buf += sizeof(*iph);
308 tcph = (struct tcphdr *)buf;
309 skb_reset_mac_header(skb);
310 skb_set_network_header(skb, ETH_HLEN);
311 skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
312 buf += sizeof(*tcph);
314 skb->ip_summed = CHECKSUM_PARTIAL;
315 skb->protocol = htons(0x800);
316 skb->data_len = 0;
317 skb->mac_len = ETH_HLEN;
319 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
320 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
321 ethh->h_proto = htons(0x0800);
323 iph->version = IPVERSION;
324 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
325 iph->tos = 0;
326 iph->tot_len = htons(packetsize);
327 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
329 iph->frag_off = htons(0x4000);
330 iph->ttl = 0x40;
331 iph->protocol = 0x06; /* IPPROTO_TCP */
333 iph->saddr = htonl(cm_node->loc_addr);
334 iph->daddr = htonl(cm_node->rem_addr);
336 tcph->source = htons(cm_node->loc_port);
337 tcph->dest = htons(cm_node->rem_port);
338 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
340 if (flags & SET_ACK) {
341 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
342 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
343 tcph->ack = 1;
344 } else
345 tcph->ack_seq = 0;
347 if (flags & SET_SYN) {
348 cm_node->tcp_cntxt.loc_seq_num++;
349 tcph->syn = 1;
350 } else
351 cm_node->tcp_cntxt.loc_seq_num += datasize;
353 if (flags & SET_FIN) {
354 cm_node->tcp_cntxt.loc_seq_num++;
355 tcph->fin = 1;
358 if (flags & SET_RST)
359 tcph->rst = 1;
361 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
362 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
363 tcph->urg_ptr = 0;
364 if (optionsize)
365 memcpy(buf, options, optionsize);
366 buf += optionsize;
367 if (datasize)
368 memcpy(buf, data, datasize);
370 skb_shinfo(skb)->nr_frags = 0;
371 cm_packets_created++;
377 * print_core - dump a cm core
379 static void print_core(struct nes_cm_core *core)
381 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
382 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
383 if (!core)
384 return;
385 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
387 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
389 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
390 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
392 nes_debug(NES_DBG_CM, "core : %p \n", core);
394 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
399 * schedule_nes_timer
400 * note - cm_node needs to be protected before calling this. Encase in:
401 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
403 int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
404 enum nes_timer_type type, int send_retrans,
405 int close_when_complete)
407 unsigned long flags;
408 struct nes_cm_core *cm_core = cm_node->cm_core;
409 struct nes_timer_entry *new_send;
410 int ret = 0;
411 u32 was_timer_set;
413 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
414 if (!new_send)
415 return -ENOMEM;
417 /* new_send->timetosend = currenttime */
418 new_send->retrycount = NES_DEFAULT_RETRYS;
419 new_send->retranscount = NES_DEFAULT_RETRANS;
420 new_send->skb = skb;
421 new_send->timetosend = jiffies;
422 new_send->type = type;
423 new_send->netdev = cm_node->netdev;
424 new_send->send_retrans = send_retrans;
425 new_send->close_when_complete = close_when_complete;
427 if (type == NES_TIMER_TYPE_CLOSE) {
428 new_send->timetosend += (HZ/10);
429 if (cm_node->recv_entry) {
430 kfree(new_send);
431 WARN_ON(1);
432 return -EINVAL;
434 cm_node->recv_entry = new_send;
437 if (type == NES_TIMER_TYPE_SEND) {
438 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
439 atomic_inc(&new_send->skb->users);
440 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
441 cm_node->send_entry = new_send;
442 add_ref_cm_node(cm_node);
443 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
444 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
446 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
447 if (ret != NETDEV_TX_OK) {
448 nes_debug(NES_DBG_CM, "Error sending packet %p "
449 "(jiffies = %lu)\n", new_send, jiffies);
450 new_send->timetosend = jiffies;
451 ret = NETDEV_TX_OK;
452 } else {
453 cm_packets_sent++;
454 if (!send_retrans) {
455 cleanup_retrans_entry(cm_node);
456 if (close_when_complete)
457 rem_ref_cm_node(cm_core, cm_node);
458 return ret;
463 was_timer_set = timer_pending(&cm_core->tcp_timer);
465 if (!was_timer_set) {
466 cm_core->tcp_timer.expires = new_send->timetosend;
467 add_timer(&cm_core->tcp_timer);
470 return ret;
473 static void nes_retrans_expired(struct nes_cm_node *cm_node)
475 struct iw_cm_id *cm_id = cm_node->cm_id;
476 switch (cm_node->state) {
477 case NES_CM_STATE_SYN_RCVD:
478 case NES_CM_STATE_CLOSING:
479 rem_ref_cm_node(cm_node->cm_core, cm_node);
480 break;
481 case NES_CM_STATE_LAST_ACK:
482 case NES_CM_STATE_FIN_WAIT1:
483 if (cm_node->cm_id)
484 cm_id->rem_ref(cm_id);
485 cm_node->state = NES_CM_STATE_CLOSED;
486 send_reset(cm_node, NULL);
487 break;
488 default:
489 create_event(cm_node, NES_CM_EVENT_ABORTED);
493 static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
495 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
496 struct iw_cm_id *cm_id = cm_node->cm_id;
497 struct nes_qp *nesqp;
498 unsigned long qplockflags;
500 if (!recv_entry)
501 return;
502 nesqp = (struct nes_qp *)recv_entry->skb;
503 if (nesqp) {
504 spin_lock_irqsave(&nesqp->lock, qplockflags);
505 if (nesqp->cm_id) {
506 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
507 "refcount = %d: HIT A "
508 "NES_TIMER_TYPE_CLOSE with something "
509 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
510 atomic_read(&nesqp->refcount));
511 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
512 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
513 nesqp->ibqp_state = IB_QPS_ERR;
514 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
515 nes_cm_disconn(nesqp);
516 } else {
517 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
518 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
519 "refcount = %d: HIT A "
520 "NES_TIMER_TYPE_CLOSE with nothing "
521 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
522 atomic_read(&nesqp->refcount));
524 } else if (rem_node) {
525 /* TIME_WAIT state */
526 rem_ref_cm_node(cm_node->cm_core, cm_node);
528 if (cm_node->cm_id)
529 cm_id->rem_ref(cm_id);
530 kfree(recv_entry);
531 cm_node->recv_entry = NULL;
535 * nes_cm_timer_tick
537 static void nes_cm_timer_tick(unsigned long pass)
539 unsigned long flags;
540 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
541 struct nes_cm_node *cm_node;
542 struct nes_timer_entry *send_entry, *recv_entry;
543 struct list_head *list_core_temp;
544 struct list_head *list_node;
545 struct nes_cm_core *cm_core = g_cm_core;
546 u32 settimer = 0;
547 unsigned long timetosend;
548 int ret = NETDEV_TX_OK;
550 struct list_head timer_list;
551 INIT_LIST_HEAD(&timer_list);
552 spin_lock_irqsave(&cm_core->ht_lock, flags);
554 list_for_each_safe(list_node, list_core_temp,
555 &cm_core->connected_nodes) {
556 cm_node = container_of(list_node, struct nes_cm_node, list);
557 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
558 add_ref_cm_node(cm_node);
559 list_add(&cm_node->timer_entry, &timer_list);
562 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
564 list_for_each_safe(list_node, list_core_temp, &timer_list) {
565 cm_node = container_of(list_node, struct nes_cm_node,
566 timer_entry);
567 recv_entry = cm_node->recv_entry;
569 if (recv_entry) {
570 if (time_after(recv_entry->timetosend, jiffies)) {
571 if (nexttimeout > recv_entry->timetosend ||
572 !settimer) {
573 nexttimeout = recv_entry->timetosend;
574 settimer = 1;
576 } else
577 handle_recv_entry(cm_node, 1);
580 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
581 do {
582 send_entry = cm_node->send_entry;
583 if (!send_entry)
584 break;
585 if (time_after(send_entry->timetosend, jiffies)) {
586 if (cm_node->state != NES_CM_STATE_TSA) {
587 if ((nexttimeout >
588 send_entry->timetosend) ||
589 !settimer) {
590 nexttimeout =
591 send_entry->timetosend;
592 settimer = 1;
594 } else {
595 free_retrans_entry(cm_node);
597 break;
600 if ((cm_node->state == NES_CM_STATE_TSA) ||
601 (cm_node->state == NES_CM_STATE_CLOSED)) {
602 free_retrans_entry(cm_node);
603 break;
606 if (!send_entry->retranscount ||
607 !send_entry->retrycount) {
608 cm_packets_dropped++;
609 free_retrans_entry(cm_node);
611 spin_unlock_irqrestore(
612 &cm_node->retrans_list_lock, flags);
613 nes_retrans_expired(cm_node);
614 cm_node->state = NES_CM_STATE_CLOSED;
615 spin_lock_irqsave(&cm_node->retrans_list_lock,
616 flags);
617 break;
619 atomic_inc(&send_entry->skb->users);
620 cm_packets_retrans++;
621 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
622 "for node %p, jiffies = %lu, time to send = "
623 "%lu, retranscount = %u, send_entry->seq_num = "
624 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
625 "0x%08X\n", send_entry, cm_node, jiffies,
626 send_entry->timetosend,
627 send_entry->retranscount,
628 send_entry->seq_num,
629 cm_node->tcp_cntxt.rem_ack_num);
631 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
632 flags);
633 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
634 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
635 if (ret != NETDEV_TX_OK) {
636 nes_debug(NES_DBG_CM, "rexmit failed for "
637 "node=%p\n", cm_node);
638 cm_packets_bounced++;
639 send_entry->retrycount--;
640 nexttimeout = jiffies + NES_SHORT_TIME;
641 settimer = 1;
642 break;
643 } else {
644 cm_packets_sent++;
646 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
647 "%u, retry count = %u.\n",
648 send_entry->retranscount,
649 send_entry->retrycount);
650 if (send_entry->send_retrans) {
651 send_entry->retranscount--;
652 timetosend = (NES_RETRY_TIMEOUT <<
653 (NES_DEFAULT_RETRANS - send_entry->retranscount));
655 send_entry->timetosend = jiffies +
656 min(timetosend, NES_MAX_TIMEOUT);
657 if (nexttimeout > send_entry->timetosend ||
658 !settimer) {
659 nexttimeout = send_entry->timetosend;
660 settimer = 1;
662 } else {
663 int close_when_complete;
664 close_when_complete =
665 send_entry->close_when_complete;
666 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
667 cm_node, cm_node->state);
668 free_retrans_entry(cm_node);
669 if (close_when_complete)
670 rem_ref_cm_node(cm_node->cm_core,
671 cm_node);
673 } while (0);
675 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
676 rem_ref_cm_node(cm_node->cm_core, cm_node);
679 if (settimer) {
680 if (!timer_pending(&cm_core->tcp_timer)) {
681 cm_core->tcp_timer.expires = nexttimeout;
682 add_timer(&cm_core->tcp_timer);
689 * send_syn
691 static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
692 struct sk_buff *skb)
694 int ret;
695 int flags = SET_SYN;
696 char optionsbuffer[sizeof(struct option_mss) +
697 sizeof(struct option_windowscale) + sizeof(struct option_base) +
698 TCP_OPTIONS_PADDING];
700 int optionssize = 0;
701 /* Sending MSS option */
702 union all_known_options *options;
704 if (!cm_node)
705 return -EINVAL;
707 options = (union all_known_options *)&optionsbuffer[optionssize];
708 options->as_mss.optionnum = OPTION_NUMBER_MSS;
709 options->as_mss.length = sizeof(struct option_mss);
710 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
711 optionssize += sizeof(struct option_mss);
713 options = (union all_known_options *)&optionsbuffer[optionssize];
714 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
715 options->as_windowscale.length = sizeof(struct option_windowscale);
716 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
717 optionssize += sizeof(struct option_windowscale);
719 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
720 options = (union all_known_options *)&optionsbuffer[optionssize];
721 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
722 options->as_base.length = sizeof(struct option_base);
723 optionssize += sizeof(struct option_base);
724 /* we need the size to be a multiple of 4 */
725 options = (union all_known_options *)&optionsbuffer[optionssize];
726 options->as_end = 1;
727 optionssize += 1;
728 options = (union all_known_options *)&optionsbuffer[optionssize];
729 options->as_end = 1;
730 optionssize += 1;
733 options = (union all_known_options *)&optionsbuffer[optionssize];
734 options->as_end = OPTION_NUMBER_END;
735 optionssize += 1;
737 if (!skb)
738 skb = dev_alloc_skb(MAX_CM_BUFFER);
739 if (!skb) {
740 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
741 return -1;
744 if (sendack)
745 flags |= SET_ACK;
747 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
748 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
750 return ret;
755 * send_reset
757 static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
759 int ret;
760 int flags = SET_RST | SET_ACK;
762 if (!skb)
763 skb = dev_alloc_skb(MAX_CM_BUFFER);
764 if (!skb) {
765 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
766 return -ENOMEM;
769 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
770 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
772 return ret;
777 * send_ack
779 static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
781 int ret;
783 if (!skb)
784 skb = dev_alloc_skb(MAX_CM_BUFFER);
786 if (!skb) {
787 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
788 return -1;
791 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
792 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
794 return ret;
799 * send_fin
801 static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
803 int ret;
805 /* if we didn't get a frame get one */
806 if (!skb)
807 skb = dev_alloc_skb(MAX_CM_BUFFER);
809 if (!skb) {
810 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
811 return -1;
814 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
815 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
817 return ret;
822 * find_node - find a cm node that matches the reference cm node
824 static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
825 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
827 unsigned long flags;
828 struct list_head *hte;
829 struct nes_cm_node *cm_node;
831 /* get a handle on the hte */
832 hte = &cm_core->connected_nodes;
834 /* walk list and find cm_node associated with this session ID */
835 spin_lock_irqsave(&cm_core->ht_lock, flags);
836 list_for_each_entry(cm_node, hte, list) {
837 /* compare quad, return node handle if a match */
838 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
839 cm_node->loc_addr, cm_node->loc_port,
840 loc_addr, loc_port,
841 cm_node->rem_addr, cm_node->rem_port,
842 rem_addr, rem_port);
843 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
844 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
845 add_ref_cm_node(cm_node);
846 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
847 return cm_node;
850 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
852 /* no owner node */
853 return NULL;
858 * find_listener - find a cm node listening on this addr-port pair
860 static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
861 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
863 unsigned long flags;
864 struct nes_cm_listener *listen_node;
866 /* walk list and find cm_node associated with this session ID */
867 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
868 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
869 /* compare node pair, return node handle if a match */
870 if (((listen_node->loc_addr == dst_addr) ||
871 listen_node->loc_addr == 0x00000000) &&
872 (listen_node->loc_port == dst_port) &&
873 (listener_state & listen_node->listener_state)) {
874 atomic_inc(&listen_node->ref_count);
875 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
876 return listen_node;
879 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
881 /* no listener */
882 return NULL;
887 * add_hte_node - add a cm node to the hash table
889 static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
891 unsigned long flags;
892 struct list_head *hte;
894 if (!cm_node || !cm_core)
895 return -EINVAL;
897 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
898 cm_node);
900 spin_lock_irqsave(&cm_core->ht_lock, flags);
902 /* get a handle on the hash table element (list head for this slot) */
903 hte = &cm_core->connected_nodes;
904 list_add_tail(&cm_node->list, hte);
905 atomic_inc(&cm_core->ht_node_cnt);
907 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
909 return 0;
914 * mini_cm_dec_refcnt_listen
916 static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
917 struct nes_cm_listener *listener, int free_hanging_nodes)
919 int ret = -EINVAL;
920 int err = 0;
921 unsigned long flags;
922 struct list_head *list_pos = NULL;
923 struct list_head *list_temp = NULL;
924 struct nes_cm_node *cm_node = NULL;
925 struct list_head reset_list;
927 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
928 "refcnt=%d\n", listener, free_hanging_nodes,
929 atomic_read(&listener->ref_count));
930 /* free non-accelerated child nodes for this listener */
931 INIT_LIST_HEAD(&reset_list);
932 if (free_hanging_nodes) {
933 spin_lock_irqsave(&cm_core->ht_lock, flags);
934 list_for_each_safe(list_pos, list_temp,
935 &g_cm_core->connected_nodes) {
936 cm_node = container_of(list_pos, struct nes_cm_node,
937 list);
938 if ((cm_node->listener == listener) &&
939 (!cm_node->accelerated)) {
940 add_ref_cm_node(cm_node);
941 list_add(&cm_node->reset_entry, &reset_list);
944 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
947 list_for_each_safe(list_pos, list_temp, &reset_list) {
948 cm_node = container_of(list_pos, struct nes_cm_node,
949 reset_entry);
951 struct nes_cm_node *loopback = cm_node->loopbackpartner;
952 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
953 rem_ref_cm_node(cm_node->cm_core, cm_node);
954 } else {
955 if (!loopback) {
956 cleanup_retrans_entry(cm_node);
957 err = send_reset(cm_node, NULL);
958 if (err) {
959 cm_node->state =
960 NES_CM_STATE_CLOSED;
961 WARN_ON(1);
962 } else {
963 cm_node->state =
964 NES_CM_STATE_CLOSED;
965 rem_ref_cm_node(
966 cm_node->cm_core,
967 cm_node);
969 } else {
970 struct nes_cm_event event;
972 event.cm_node = loopback;
973 event.cm_info.rem_addr =
974 loopback->rem_addr;
975 event.cm_info.loc_addr =
976 loopback->loc_addr;
977 event.cm_info.rem_port =
978 loopback->rem_port;
979 event.cm_info.loc_port =
980 loopback->loc_port;
981 event.cm_info.cm_id = loopback->cm_id;
982 cm_event_connect_error(&event);
983 loopback->state = NES_CM_STATE_CLOSED;
985 event.cm_node = cm_node;
986 event.cm_info.rem_addr =
987 cm_node->rem_addr;
988 event.cm_info.loc_addr =
989 cm_node->loc_addr;
990 event.cm_info.rem_port =
991 cm_node->rem_port;
992 event.cm_info.loc_port =
993 cm_node->loc_port;
994 event.cm_info.cm_id = cm_node->cm_id;
995 cm_event_reset(&event);
997 rem_ref_cm_node(cm_node->cm_core,
998 cm_node);
1005 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1006 if (!atomic_dec_return(&listener->ref_count)) {
1007 list_del(&listener->list);
1009 /* decrement our listen node count */
1010 atomic_dec(&cm_core->listen_node_cnt);
1012 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1014 if (listener->nesvnic) {
1015 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1016 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1019 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1021 kfree(listener);
1022 listener = NULL;
1023 ret = 0;
1024 cm_listens_destroyed++;
1025 } else {
1026 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1028 if (listener) {
1029 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1030 nes_debug(NES_DBG_CM, "destroying listener (%p)"
1031 " with non-zero pending accepts=%u\n",
1032 listener, atomic_read(&listener->pend_accepts_cnt));
1035 return ret;
1040 * mini_cm_del_listen
1042 static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1043 struct nes_cm_listener *listener)
1045 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1046 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1047 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1052 * mini_cm_accelerated
1054 static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1055 struct nes_cm_node *cm_node)
1057 u32 was_timer_set;
1058 cm_node->accelerated = 1;
1060 if (cm_node->accept_pend) {
1061 BUG_ON(!cm_node->listener);
1062 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1063 cm_node->accept_pend = 0;
1064 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1067 was_timer_set = timer_pending(&cm_core->tcp_timer);
1068 if (!was_timer_set) {
1069 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1070 add_timer(&cm_core->tcp_timer);
1073 return 0;
1078 * nes_addr_resolve_neigh
1080 static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip)
1082 struct rtable *rt;
1083 struct flowi fl;
1084 struct neighbour *neigh;
1085 int rc = -1;
1087 memset(&fl, 0, sizeof fl);
1088 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1089 if (ip_route_output_key(&init_net, &rt, &fl)) {
1090 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
1091 __func__, dst_ip);
1092 return rc;
1095 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1096 if (neigh) {
1097 if (neigh->nud_state & NUD_VALID) {
1098 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
1099 " is %pM, Gateway is 0x%08X \n", dst_ip,
1100 neigh->ha, ntohl(rt->rt_gateway));
1101 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1102 dst_ip, NES_ARP_ADD);
1103 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1104 NES_ARP_RESOLVE);
1106 neigh_release(neigh);
1109 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1110 neigh_event_send(rt->u.dst.neighbour, NULL);
1112 ip_rt_put(rt);
1113 return rc;
1118 * make_cm_node - create a new instance of a cm node
1120 static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1121 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1122 struct nes_cm_listener *listener)
1124 struct nes_cm_node *cm_node;
1125 struct timespec ts;
1126 int arpindex = 0;
1127 struct nes_device *nesdev;
1128 struct nes_adapter *nesadapter;
1130 /* create an hte and cm_node for this instance */
1131 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1132 if (!cm_node)
1133 return NULL;
1135 /* set our node specific transport info */
1136 cm_node->loc_addr = cm_info->loc_addr;
1137 cm_node->rem_addr = cm_info->rem_addr;
1138 cm_node->loc_port = cm_info->loc_port;
1139 cm_node->rem_port = cm_info->rem_port;
1140 cm_node->send_write0 = send_first;
1141 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1142 &cm_node->loc_addr, cm_node->loc_port,
1143 &cm_node->rem_addr, cm_node->rem_port);
1144 cm_node->listener = listener;
1145 cm_node->netdev = nesvnic->netdev;
1146 cm_node->cm_id = cm_info->cm_id;
1147 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1149 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1150 cm_node->cm_id);
1152 spin_lock_init(&cm_node->retrans_list_lock);
1154 cm_node->loopbackpartner = NULL;
1155 atomic_set(&cm_node->ref_count, 1);
1156 /* associate our parent CM core */
1157 cm_node->cm_core = cm_core;
1158 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1159 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1160 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1161 NES_CM_DEFAULT_RCV_WND_SCALE;
1162 ts = current_kernel_time();
1163 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1164 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
1165 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
1166 cm_node->tcp_cntxt.rcv_nxt = 0;
1167 /* get a unique session ID , add thread_id to an upcounter to handle race */
1168 atomic_inc(&cm_core->node_cnt);
1169 cm_node->conn_type = cm_info->conn_type;
1170 cm_node->apbvt_set = 0;
1171 cm_node->accept_pend = 0;
1173 cm_node->nesvnic = nesvnic;
1174 /* get some device handles, for arp lookup */
1175 nesdev = nesvnic->nesdev;
1176 nesadapter = nesdev->nesadapter;
1178 cm_node->loopbackpartner = NULL;
1179 /* get the mac addr for the remote node */
1180 if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1181 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
1182 else
1183 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1184 if (arpindex < 0) {
1185 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr);
1186 if (arpindex < 0) {
1187 kfree(cm_node);
1188 return NULL;
1192 /* copy the mac addr to node context */
1193 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
1194 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1195 cm_node->rem_mac);
1197 add_hte_node(cm_core, cm_node);
1198 atomic_inc(&cm_nodes_created);
1200 return cm_node;
1205 * add_ref_cm_node - destroy an instance of a cm node
1207 static int add_ref_cm_node(struct nes_cm_node *cm_node)
1209 atomic_inc(&cm_node->ref_count);
1210 return 0;
1215 * rem_ref_cm_node - destroy an instance of a cm node
1217 static int rem_ref_cm_node(struct nes_cm_core *cm_core,
1218 struct nes_cm_node *cm_node)
1220 unsigned long flags;
1221 struct nes_qp *nesqp;
1223 if (!cm_node)
1224 return -EINVAL;
1226 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1227 if (atomic_dec_return(&cm_node->ref_count)) {
1228 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1229 return 0;
1231 list_del(&cm_node->list);
1232 atomic_dec(&cm_core->ht_node_cnt);
1233 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1235 /* if the node is destroyed before connection was accelerated */
1236 if (!cm_node->accelerated && cm_node->accept_pend) {
1237 BUG_ON(!cm_node->listener);
1238 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1239 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1241 WARN_ON(cm_node->send_entry);
1242 if (cm_node->recv_entry)
1243 handle_recv_entry(cm_node, 0);
1244 if (cm_node->listener) {
1245 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1246 } else {
1247 if (cm_node->apbvt_set && cm_node->nesvnic) {
1248 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
1249 PCI_FUNC(
1250 cm_node->nesvnic->nesdev->pcidev->devfn),
1251 NES_MANAGE_APBVT_DEL);
1255 atomic_dec(&cm_core->node_cnt);
1256 atomic_inc(&cm_nodes_destroyed);
1257 nesqp = cm_node->nesqp;
1258 if (nesqp) {
1259 nesqp->cm_node = NULL;
1260 nes_rem_ref(&nesqp->ibqp);
1261 cm_node->nesqp = NULL;
1264 kfree(cm_node);
1265 return 0;
1269 * process_options
1271 static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1272 u32 optionsize, u32 syn_packet)
1274 u32 tmp;
1275 u32 offset = 0;
1276 union all_known_options *all_options;
1277 char got_mss_option = 0;
1279 while (offset < optionsize) {
1280 all_options = (union all_known_options *)(optionsloc + offset);
1281 switch (all_options->as_base.optionnum) {
1282 case OPTION_NUMBER_END:
1283 offset = optionsize;
1284 break;
1285 case OPTION_NUMBER_NONE:
1286 offset += 1;
1287 continue;
1288 case OPTION_NUMBER_MSS:
1289 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1290 "Size: %d\n", __func__,
1291 all_options->as_mss.length, offset, optionsize);
1292 got_mss_option = 1;
1293 if (all_options->as_mss.length != 4) {
1294 return 1;
1295 } else {
1296 tmp = ntohs(all_options->as_mss.mss);
1297 if (tmp > 0 && tmp <
1298 cm_node->tcp_cntxt.mss)
1299 cm_node->tcp_cntxt.mss = tmp;
1301 break;
1302 case OPTION_NUMBER_WINDOW_SCALE:
1303 cm_node->tcp_cntxt.snd_wscale =
1304 all_options->as_windowscale.shiftcount;
1305 break;
1306 case OPTION_NUMBER_WRITE0:
1307 cm_node->send_write0 = 1;
1308 break;
1309 default:
1310 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1311 all_options->as_base.optionnum);
1312 break;
1314 offset += all_options->as_base.length;
1316 if ((!got_mss_option) && (syn_packet))
1317 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1318 return 0;
1321 static void drop_packet(struct sk_buff *skb)
1323 atomic_inc(&cm_accel_dropped_pkts);
1324 dev_kfree_skb_any(skb);
1327 static void handle_fin_pkt(struct nes_cm_node *cm_node)
1329 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1330 "refcnt=%d\n", cm_node, cm_node->state,
1331 atomic_read(&cm_node->ref_count));
1332 switch (cm_node->state) {
1333 case NES_CM_STATE_SYN_RCVD:
1334 case NES_CM_STATE_SYN_SENT:
1335 case NES_CM_STATE_ESTABLISHED:
1336 case NES_CM_STATE_MPAREQ_SENT:
1337 case NES_CM_STATE_MPAREJ_RCVD:
1338 cm_node->tcp_cntxt.rcv_nxt++;
1339 cleanup_retrans_entry(cm_node);
1340 cm_node->state = NES_CM_STATE_LAST_ACK;
1341 send_fin(cm_node, NULL);
1342 break;
1343 case NES_CM_STATE_FIN_WAIT1:
1344 cm_node->tcp_cntxt.rcv_nxt++;
1345 cleanup_retrans_entry(cm_node);
1346 cm_node->state = NES_CM_STATE_CLOSING;
1347 send_ack(cm_node, NULL);
1348 /* Wait for ACK as this is simultanous close..
1349 * After we receive ACK, do not send anything..
1350 * Just rm the node.. Done.. */
1351 break;
1352 case NES_CM_STATE_FIN_WAIT2:
1353 cm_node->tcp_cntxt.rcv_nxt++;
1354 cleanup_retrans_entry(cm_node);
1355 cm_node->state = NES_CM_STATE_TIME_WAIT;
1356 send_ack(cm_node, NULL);
1357 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1358 break;
1359 case NES_CM_STATE_TIME_WAIT:
1360 cm_node->tcp_cntxt.rcv_nxt++;
1361 cleanup_retrans_entry(cm_node);
1362 cm_node->state = NES_CM_STATE_CLOSED;
1363 rem_ref_cm_node(cm_node->cm_core, cm_node);
1364 break;
1365 case NES_CM_STATE_TSA:
1366 default:
1367 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1368 cm_node, cm_node->state);
1369 break;
1374 static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1375 struct tcphdr *tcph)
1378 int reset = 0; /* whether to send reset in case of err.. */
1379 int passive_state;
1380 atomic_inc(&cm_resets_recvd);
1381 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1382 " refcnt=%d\n", cm_node, cm_node->state,
1383 atomic_read(&cm_node->ref_count));
1384 cleanup_retrans_entry(cm_node);
1385 switch (cm_node->state) {
1386 case NES_CM_STATE_SYN_SENT:
1387 case NES_CM_STATE_MPAREQ_SENT:
1388 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1389 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1390 cm_node->listener, cm_node->state);
1391 active_open_err(cm_node, skb, reset);
1392 break;
1393 case NES_CM_STATE_MPAREQ_RCVD:
1394 passive_state = atomic_add_return(1, &cm_node->passive_state);
1395 if (passive_state == NES_SEND_RESET_EVENT)
1396 create_event(cm_node, NES_CM_EVENT_RESET);
1397 cm_node->state = NES_CM_STATE_CLOSED;
1398 dev_kfree_skb_any(skb);
1399 break;
1400 case NES_CM_STATE_ESTABLISHED:
1401 case NES_CM_STATE_SYN_RCVD:
1402 case NES_CM_STATE_LISTENING:
1403 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1404 passive_open_err(cm_node, skb, reset);
1405 break;
1406 case NES_CM_STATE_TSA:
1407 active_open_err(cm_node, skb, reset);
1408 break;
1409 case NES_CM_STATE_CLOSED:
1410 drop_packet(skb);
1411 break;
1412 case NES_CM_STATE_FIN_WAIT1:
1413 case NES_CM_STATE_LAST_ACK:
1414 cm_node->cm_id->rem_ref(cm_node->cm_id);
1415 case NES_CM_STATE_TIME_WAIT:
1416 cm_node->state = NES_CM_STATE_CLOSED;
1417 rem_ref_cm_node(cm_node->cm_core, cm_node);
1418 drop_packet(skb);
1419 break;
1420 default:
1421 drop_packet(skb);
1422 break;
1427 static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
1430 int ret = 0;
1431 int datasize = skb->len;
1432 u8 *dataloc = skb->data;
1434 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1435 u32 res_type;
1436 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1437 if (ret) {
1438 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
1439 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
1440 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1441 "cm_node=%p listener=%p state=%d\n", __func__,
1442 __LINE__, cm_node, cm_node->listener,
1443 cm_node->state);
1444 active_open_err(cm_node, skb, 1);
1445 } else {
1446 passive_open_err(cm_node, skb, 1);
1448 return;
1451 switch (cm_node->state) {
1452 case NES_CM_STATE_ESTABLISHED:
1453 if (res_type == NES_MPA_REQUEST_REJECT) {
1454 /*BIG problem as we are receiving the MPA.. So should
1455 * not be REJECT.. This is Passive Open.. We can
1456 * only receive it Reject for Active Open...*/
1457 WARN_ON(1);
1459 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1460 type = NES_CM_EVENT_MPA_REQ;
1461 atomic_set(&cm_node->passive_state,
1462 NES_PASSIVE_STATE_INDICATED);
1463 break;
1464 case NES_CM_STATE_MPAREQ_SENT:
1465 cleanup_retrans_entry(cm_node);
1466 if (res_type == NES_MPA_REQUEST_REJECT) {
1467 type = NES_CM_EVENT_MPA_REJECT;
1468 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1469 } else {
1470 type = NES_CM_EVENT_CONNECTED;
1471 cm_node->state = NES_CM_STATE_TSA;
1474 break;
1475 default:
1476 WARN_ON(1);
1477 break;
1479 dev_kfree_skb_any(skb);
1480 create_event(cm_node, type);
1483 static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1485 switch (cm_node->state) {
1486 case NES_CM_STATE_SYN_SENT:
1487 case NES_CM_STATE_MPAREQ_SENT:
1488 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1489 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1490 cm_node->listener, cm_node->state);
1491 active_open_err(cm_node, skb, 1);
1492 break;
1493 case NES_CM_STATE_ESTABLISHED:
1494 case NES_CM_STATE_SYN_RCVD:
1495 passive_open_err(cm_node, skb, 1);
1496 break;
1497 case NES_CM_STATE_TSA:
1498 default:
1499 drop_packet(skb);
1503 static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1504 struct sk_buff *skb)
1506 int err;
1508 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1509 if (err)
1510 active_open_err(cm_node, skb, 1);
1512 return err;
1515 static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1516 struct sk_buff *skb)
1518 int err = 0;
1519 u32 seq;
1520 u32 ack_seq;
1521 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1522 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1523 u32 rcv_wnd;
1524 seq = ntohl(tcph->seq);
1525 ack_seq = ntohl(tcph->ack_seq);
1526 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1527 if (ack_seq != loc_seq_num)
1528 err = 1;
1529 else if (!between(seq, rcv_nxt, (rcv_nxt+rcv_wnd)))
1530 err = 1;
1531 if (err) {
1532 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1533 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1534 cm_node->listener, cm_node->state);
1535 indicate_pkt_err(cm_node, skb);
1536 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1537 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1538 rcv_wnd);
1540 return err;
1544 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1545 * is created with a listener or it may comein as rexmitted packet which in
1546 * that case will be just dropped.
1549 static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1550 struct tcphdr *tcph)
1552 int ret;
1553 u32 inc_sequence;
1554 int optionsize;
1556 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1557 skb_trim(skb, 0);
1558 inc_sequence = ntohl(tcph->seq);
1560 switch (cm_node->state) {
1561 case NES_CM_STATE_SYN_SENT:
1562 case NES_CM_STATE_MPAREQ_SENT:
1563 /* Rcvd syn on active open connection*/
1564 active_open_err(cm_node, skb, 1);
1565 break;
1566 case NES_CM_STATE_LISTENING:
1567 /* Passive OPEN */
1568 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1569 cm_node->listener->backlog) {
1570 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1571 "pressure \n");
1572 cm_backlog_drops++;
1573 passive_open_err(cm_node, skb, 0);
1574 break;
1576 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1578 if (ret) {
1579 passive_open_err(cm_node, skb, 0);
1580 /* drop pkt */
1581 break;
1583 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1584 BUG_ON(cm_node->send_entry);
1585 cm_node->accept_pend = 1;
1586 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1588 cm_node->state = NES_CM_STATE_SYN_RCVD;
1589 send_syn(cm_node, 1, skb);
1590 break;
1591 case NES_CM_STATE_CLOSED:
1592 cleanup_retrans_entry(cm_node);
1593 send_reset(cm_node, skb);
1594 break;
1595 case NES_CM_STATE_TSA:
1596 case NES_CM_STATE_ESTABLISHED:
1597 case NES_CM_STATE_FIN_WAIT1:
1598 case NES_CM_STATE_FIN_WAIT2:
1599 case NES_CM_STATE_MPAREQ_RCVD:
1600 case NES_CM_STATE_LAST_ACK:
1601 case NES_CM_STATE_CLOSING:
1602 case NES_CM_STATE_UNKNOWN:
1603 default:
1604 drop_packet(skb);
1605 break;
1609 static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1610 struct tcphdr *tcph)
1613 int ret;
1614 u32 inc_sequence;
1615 int optionsize;
1617 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1618 skb_trim(skb, 0);
1619 inc_sequence = ntohl(tcph->seq);
1620 switch (cm_node->state) {
1621 case NES_CM_STATE_SYN_SENT:
1622 cleanup_retrans_entry(cm_node);
1623 /* active open */
1624 if (check_syn(cm_node, tcph, skb))
1625 return;
1626 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1627 /* setup options */
1628 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1629 if (ret) {
1630 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1631 cm_node);
1632 break;
1634 cleanup_retrans_entry(cm_node);
1635 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1636 send_mpa_request(cm_node, skb);
1637 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1638 break;
1639 case NES_CM_STATE_MPAREQ_RCVD:
1640 /* passive open, so should not be here */
1641 passive_open_err(cm_node, skb, 1);
1642 break;
1643 case NES_CM_STATE_LISTENING:
1644 case NES_CM_STATE_CLOSED:
1645 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1646 cleanup_retrans_entry(cm_node);
1647 send_reset(cm_node, skb);
1648 break;
1649 case NES_CM_STATE_ESTABLISHED:
1650 case NES_CM_STATE_FIN_WAIT1:
1651 case NES_CM_STATE_FIN_WAIT2:
1652 case NES_CM_STATE_LAST_ACK:
1653 case NES_CM_STATE_TSA:
1654 case NES_CM_STATE_CLOSING:
1655 case NES_CM_STATE_UNKNOWN:
1656 case NES_CM_STATE_MPAREQ_SENT:
1657 default:
1658 drop_packet(skb);
1659 break;
1663 static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1664 struct tcphdr *tcph)
1666 int datasize = 0;
1667 u32 inc_sequence;
1668 u32 rem_seq_ack;
1669 u32 rem_seq;
1670 int ret = 0;
1671 int optionsize;
1672 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1674 if (check_seq(cm_node, tcph, skb))
1675 return -EINVAL;
1677 skb_pull(skb, tcph->doff << 2);
1678 inc_sequence = ntohl(tcph->seq);
1679 rem_seq = ntohl(tcph->seq);
1680 rem_seq_ack = ntohl(tcph->ack_seq);
1681 datasize = skb->len;
1682 switch (cm_node->state) {
1683 case NES_CM_STATE_SYN_RCVD:
1684 /* Passive OPEN */
1685 cleanup_retrans_entry(cm_node);
1686 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1687 if (ret)
1688 break;
1689 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1690 cm_node->state = NES_CM_STATE_ESTABLISHED;
1691 if (datasize) {
1692 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1693 handle_rcv_mpa(cm_node, skb);
1694 } else /* rcvd ACK only */
1695 dev_kfree_skb_any(skb);
1696 break;
1697 case NES_CM_STATE_ESTABLISHED:
1698 /* Passive OPEN */
1699 cleanup_retrans_entry(cm_node);
1700 if (datasize) {
1701 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1702 handle_rcv_mpa(cm_node, skb);
1703 } else
1704 drop_packet(skb);
1705 break;
1706 case NES_CM_STATE_MPAREQ_SENT:
1707 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1708 if (datasize) {
1709 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1710 handle_rcv_mpa(cm_node, skb);
1711 } else /* Could be just an ack pkt.. */
1712 dev_kfree_skb_any(skb);
1713 break;
1714 case NES_CM_STATE_LISTENING:
1715 case NES_CM_STATE_CLOSED:
1716 cleanup_retrans_entry(cm_node);
1717 send_reset(cm_node, skb);
1718 break;
1719 case NES_CM_STATE_LAST_ACK:
1720 case NES_CM_STATE_CLOSING:
1721 cleanup_retrans_entry(cm_node);
1722 cm_node->state = NES_CM_STATE_CLOSED;
1723 cm_node->cm_id->rem_ref(cm_node->cm_id);
1724 rem_ref_cm_node(cm_node->cm_core, cm_node);
1725 drop_packet(skb);
1726 break;
1727 case NES_CM_STATE_FIN_WAIT1:
1728 cleanup_retrans_entry(cm_node);
1729 drop_packet(skb);
1730 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1731 break;
1732 case NES_CM_STATE_SYN_SENT:
1733 case NES_CM_STATE_FIN_WAIT2:
1734 case NES_CM_STATE_TSA:
1735 case NES_CM_STATE_MPAREQ_RCVD:
1736 case NES_CM_STATE_UNKNOWN:
1737 default:
1738 cleanup_retrans_entry(cm_node);
1739 drop_packet(skb);
1740 break;
1742 return ret;
1747 static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1748 struct sk_buff *skb, int optionsize, int passive)
1750 u8 *optionsloc = (u8 *)&tcph[1];
1751 if (optionsize) {
1752 if (process_options(cm_node, optionsloc, optionsize,
1753 (u32)tcph->syn)) {
1754 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1755 __func__, cm_node);
1756 if (passive)
1757 passive_open_err(cm_node, skb, 1);
1758 else
1759 active_open_err(cm_node, skb, 1);
1760 return 1;
1764 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1765 cm_node->tcp_cntxt.snd_wscale;
1767 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
1768 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
1769 return 0;
1773 * active_open_err() will send reset() if flag set..
1774 * It will also send ABORT event.
1777 static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1778 int reset)
1780 cleanup_retrans_entry(cm_node);
1781 if (reset) {
1782 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1783 "state=%d\n", cm_node, cm_node->state);
1784 add_ref_cm_node(cm_node);
1785 send_reset(cm_node, skb);
1786 } else
1787 dev_kfree_skb_any(skb);
1789 cm_node->state = NES_CM_STATE_CLOSED;
1790 create_event(cm_node, NES_CM_EVENT_ABORTED);
1794 * passive_open_err() will either do a reset() or will free up the skb and
1795 * remove the cm_node.
1798 static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1799 int reset)
1801 cleanup_retrans_entry(cm_node);
1802 cm_node->state = NES_CM_STATE_CLOSED;
1803 if (reset) {
1804 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1805 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1806 send_reset(cm_node, skb);
1807 } else {
1808 dev_kfree_skb_any(skb);
1809 rem_ref_cm_node(cm_node->cm_core, cm_node);
1814 * free_retrans_entry() routines assumes that the retrans_list_lock has
1815 * been acquired before calling.
1817 static void free_retrans_entry(struct nes_cm_node *cm_node)
1819 struct nes_timer_entry *send_entry;
1820 send_entry = cm_node->send_entry;
1821 if (send_entry) {
1822 cm_node->send_entry = NULL;
1823 dev_kfree_skb_any(send_entry->skb);
1824 kfree(send_entry);
1825 rem_ref_cm_node(cm_node->cm_core, cm_node);
1829 static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1831 unsigned long flags;
1833 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1834 free_retrans_entry(cm_node);
1835 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1839 * process_packet
1840 * Returns skb if to be freed, else it will return NULL if already used..
1842 static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1843 struct nes_cm_core *cm_core)
1845 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1846 struct tcphdr *tcph = tcp_hdr(skb);
1847 u32 fin_set = 0;
1848 int ret = 0;
1849 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1851 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1852 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1853 tcph->ack, tcph->rst, tcph->fin);
1855 if (tcph->rst)
1856 pkt_type = NES_PKT_TYPE_RST;
1857 else if (tcph->syn) {
1858 pkt_type = NES_PKT_TYPE_SYN;
1859 if (tcph->ack)
1860 pkt_type = NES_PKT_TYPE_SYNACK;
1861 } else if (tcph->ack)
1862 pkt_type = NES_PKT_TYPE_ACK;
1863 if (tcph->fin)
1864 fin_set = 1;
1866 switch (pkt_type) {
1867 case NES_PKT_TYPE_SYN:
1868 handle_syn_pkt(cm_node, skb, tcph);
1869 break;
1870 case NES_PKT_TYPE_SYNACK:
1871 handle_synack_pkt(cm_node, skb, tcph);
1872 break;
1873 case NES_PKT_TYPE_ACK:
1874 ret = handle_ack_pkt(cm_node, skb, tcph);
1875 if (fin_set && !ret)
1876 handle_fin_pkt(cm_node);
1877 break;
1878 case NES_PKT_TYPE_RST:
1879 handle_rst_pkt(cm_node, skb, tcph);
1880 break;
1881 default:
1882 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
1883 handle_fin_pkt(cm_node);
1884 drop_packet(skb);
1885 break;
1890 * mini_cm_listen - create a listen node with params
1892 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
1893 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
1895 struct nes_cm_listener *listener;
1896 unsigned long flags;
1898 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1899 cm_info->loc_addr, cm_info->loc_port);
1901 /* cannot have multiple matching listeners */
1902 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1903 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1904 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1905 /* find automatically incs ref count ??? */
1906 atomic_dec(&listener->ref_count);
1907 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1908 return NULL;
1911 if (!listener) {
1912 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1913 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1914 if (!listener) {
1915 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1916 return NULL;
1919 listener->loc_addr = htonl(cm_info->loc_addr);
1920 listener->loc_port = htons(cm_info->loc_port);
1921 listener->reused_node = 0;
1923 atomic_set(&listener->ref_count, 1);
1925 /* pasive case */
1926 /* find already inc'ed the ref count */
1927 else {
1928 listener->reused_node = 1;
1931 listener->cm_id = cm_info->cm_id;
1932 atomic_set(&listener->pend_accepts_cnt, 0);
1933 listener->cm_core = cm_core;
1934 listener->nesvnic = nesvnic;
1935 atomic_inc(&cm_core->node_cnt);
1937 listener->conn_type = cm_info->conn_type;
1938 listener->backlog = cm_info->backlog;
1939 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1941 if (!listener->reused_node) {
1942 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1943 list_add(&listener->list, &cm_core->listen_list.list);
1944 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1945 atomic_inc(&cm_core->listen_node_cnt);
1948 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1949 " listener = %p, backlog = %d, cm_id = %p.\n",
1950 cm_info->loc_addr, cm_info->loc_port,
1951 listener, listener->backlog, listener->cm_id);
1953 return listener;
1958 * mini_cm_connect - make a connection node with params
1960 static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
1961 struct nes_vnic *nesvnic, u16 private_data_len,
1962 void *private_data, struct nes_cm_info *cm_info)
1964 int ret = 0;
1965 struct nes_cm_node *cm_node;
1966 struct nes_cm_listener *loopbackremotelistener;
1967 struct nes_cm_node *loopbackremotenode;
1968 struct nes_cm_info loopback_cm_info;
1969 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
1970 struct ietf_mpa_frame *mpa_frame = NULL;
1972 /* create a CM connection node */
1973 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
1974 if (!cm_node)
1975 return NULL;
1976 mpa_frame = &cm_node->mpa_frame;
1977 strcpy(mpa_frame->key, IEFT_MPA_KEY_REQ);
1978 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
1979 mpa_frame->rev = IETF_MPA_VERSION;
1980 mpa_frame->priv_data_len = htons(private_data_len);
1982 /* set our node side to client (active) side */
1983 cm_node->tcp_cntxt.client = 1;
1984 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1986 if (cm_info->loc_addr == cm_info->rem_addr) {
1987 loopbackremotelistener = find_listener(cm_core,
1988 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
1989 NES_CM_LISTENER_ACTIVE_STATE);
1990 if (loopbackremotelistener == NULL) {
1991 create_event(cm_node, NES_CM_EVENT_ABORTED);
1992 } else {
1993 loopback_cm_info = *cm_info;
1994 loopback_cm_info.loc_port = cm_info->rem_port;
1995 loopback_cm_info.rem_port = cm_info->loc_port;
1996 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
1997 loopbackremotenode = make_cm_node(cm_core, nesvnic,
1998 &loopback_cm_info, loopbackremotelistener);
1999 if (!loopbackremotenode) {
2000 rem_ref_cm_node(cm_node->cm_core, cm_node);
2001 return NULL;
2003 atomic_inc(&cm_loopbacks);
2004 loopbackremotenode->loopbackpartner = cm_node;
2005 loopbackremotenode->tcp_cntxt.rcv_wscale =
2006 NES_CM_DEFAULT_RCV_WND_SCALE;
2007 cm_node->loopbackpartner = loopbackremotenode;
2008 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2009 private_data_len);
2010 loopbackremotenode->mpa_frame_size = private_data_len;
2012 /* we are done handling this state. */
2013 /* set node to a TSA state */
2014 cm_node->state = NES_CM_STATE_TSA;
2015 cm_node->tcp_cntxt.rcv_nxt =
2016 loopbackremotenode->tcp_cntxt.loc_seq_num;
2017 loopbackremotenode->tcp_cntxt.rcv_nxt =
2018 cm_node->tcp_cntxt.loc_seq_num;
2019 cm_node->tcp_cntxt.max_snd_wnd =
2020 loopbackremotenode->tcp_cntxt.rcv_wnd;
2021 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2022 cm_node->tcp_cntxt.rcv_wnd;
2023 cm_node->tcp_cntxt.snd_wnd =
2024 loopbackremotenode->tcp_cntxt.rcv_wnd;
2025 loopbackremotenode->tcp_cntxt.snd_wnd =
2026 cm_node->tcp_cntxt.rcv_wnd;
2027 cm_node->tcp_cntxt.snd_wscale =
2028 loopbackremotenode->tcp_cntxt.rcv_wscale;
2029 loopbackremotenode->tcp_cntxt.snd_wscale =
2030 cm_node->tcp_cntxt.rcv_wscale;
2031 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
2032 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2034 return cm_node;
2037 /* set our node side to client (active) side */
2038 cm_node->tcp_cntxt.client = 1;
2039 /* init our MPA frame ptr */
2040 memcpy(mpa_frame->priv_data, private_data, private_data_len);
2042 cm_node->mpa_frame_size = mpa_frame_size;
2044 /* send a syn and goto syn sent state */
2045 cm_node->state = NES_CM_STATE_SYN_SENT;
2046 ret = send_syn(cm_node, 0, NULL);
2048 if (ret) {
2049 /* error in sending the syn free up the cm_node struct */
2050 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2051 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2052 cm_node->rem_addr, cm_node->rem_port, cm_node,
2053 cm_node->cm_id);
2054 rem_ref_cm_node(cm_node->cm_core, cm_node);
2055 cm_node = NULL;
2058 if (cm_node)
2059 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2060 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2061 cm_node->rem_addr, cm_node->rem_port, cm_node,
2062 cm_node->cm_id);
2064 return cm_node;
2069 * mini_cm_accept - accept a connection
2070 * This function is never called
2072 static int mini_cm_accept(struct nes_cm_core *cm_core,
2073 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
2075 return 0;
2080 * mini_cm_reject - reject and teardown a connection
2082 static int mini_cm_reject(struct nes_cm_core *cm_core,
2083 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
2085 int ret = 0;
2086 int err = 0;
2087 int passive_state;
2088 struct nes_cm_event event;
2089 struct iw_cm_id *cm_id = cm_node->cm_id;
2090 struct nes_cm_node *loopback = cm_node->loopbackpartner;
2092 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2093 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
2095 if (cm_node->tcp_cntxt.client)
2096 return ret;
2097 cleanup_retrans_entry(cm_node);
2099 if (!loopback) {
2100 passive_state = atomic_add_return(1, &cm_node->passive_state);
2101 if (passive_state == NES_SEND_RESET_EVENT) {
2102 cm_node->state = NES_CM_STATE_CLOSED;
2103 rem_ref_cm_node(cm_core, cm_node);
2104 } else {
2105 ret = send_mpa_reject(cm_node);
2106 if (ret) {
2107 cm_node->state = NES_CM_STATE_CLOSED;
2108 err = send_reset(cm_node, NULL);
2109 if (err)
2110 WARN_ON(1);
2111 } else
2112 cm_id->add_ref(cm_id);
2114 } else {
2115 cm_node->cm_id = NULL;
2116 event.cm_node = loopback;
2117 event.cm_info.rem_addr = loopback->rem_addr;
2118 event.cm_info.loc_addr = loopback->loc_addr;
2119 event.cm_info.rem_port = loopback->rem_port;
2120 event.cm_info.loc_port = loopback->loc_port;
2121 event.cm_info.cm_id = loopback->cm_id;
2122 cm_event_mpa_reject(&event);
2123 rem_ref_cm_node(cm_core, cm_node);
2124 loopback->state = NES_CM_STATE_CLOSING;
2126 cm_id = loopback->cm_id;
2127 rem_ref_cm_node(cm_core, loopback);
2128 cm_id->rem_ref(cm_id);
2131 return ret;
2136 * mini_cm_close
2138 static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
2140 int ret = 0;
2142 if (!cm_core || !cm_node)
2143 return -EINVAL;
2145 switch (cm_node->state) {
2146 case NES_CM_STATE_SYN_RCVD:
2147 case NES_CM_STATE_SYN_SENT:
2148 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2149 case NES_CM_STATE_ESTABLISHED:
2150 case NES_CM_STATE_ACCEPTING:
2151 case NES_CM_STATE_MPAREQ_SENT:
2152 case NES_CM_STATE_MPAREQ_RCVD:
2153 cleanup_retrans_entry(cm_node);
2154 send_reset(cm_node, NULL);
2155 break;
2156 case NES_CM_STATE_CLOSE_WAIT:
2157 cm_node->state = NES_CM_STATE_LAST_ACK;
2158 send_fin(cm_node, NULL);
2159 break;
2160 case NES_CM_STATE_FIN_WAIT1:
2161 case NES_CM_STATE_FIN_WAIT2:
2162 case NES_CM_STATE_LAST_ACK:
2163 case NES_CM_STATE_TIME_WAIT:
2164 case NES_CM_STATE_CLOSING:
2165 ret = -1;
2166 break;
2167 case NES_CM_STATE_MPAREJ_RCVD:
2168 case NES_CM_STATE_LISTENING:
2169 case NES_CM_STATE_UNKNOWN:
2170 case NES_CM_STATE_INITED:
2171 case NES_CM_STATE_CLOSED:
2172 ret = rem_ref_cm_node(cm_core, cm_node);
2173 break;
2174 case NES_CM_STATE_TSA:
2175 if (cm_node->send_entry)
2176 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2177 "send_entry=%p\n", cm_node->send_entry);
2178 ret = rem_ref_cm_node(cm_core, cm_node);
2179 break;
2181 return ret;
2186 * recv_pkt - recv an ETHERNET packet, and process it through CM
2187 * node state machine
2189 static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
2190 struct nes_vnic *nesvnic, struct sk_buff *skb)
2192 struct nes_cm_node *cm_node = NULL;
2193 struct nes_cm_listener *listener = NULL;
2194 struct iphdr *iph;
2195 struct tcphdr *tcph;
2196 struct nes_cm_info nfo;
2197 int skb_handled = 1;
2198 __be32 tmp_daddr, tmp_saddr;
2200 if (!skb)
2201 return 0;
2202 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
2203 return 0;
2206 iph = (struct iphdr *)skb->data;
2207 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
2209 nfo.loc_addr = ntohl(iph->daddr);
2210 nfo.loc_port = ntohs(tcph->dest);
2211 nfo.rem_addr = ntohl(iph->saddr);
2212 nfo.rem_port = ntohs(tcph->source);
2214 tmp_daddr = cpu_to_be32(iph->daddr);
2215 tmp_saddr = cpu_to_be32(iph->saddr);
2217 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
2218 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
2220 do {
2221 cm_node = find_node(cm_core,
2222 nfo.rem_port, nfo.rem_addr,
2223 nfo.loc_port, nfo.loc_addr);
2225 if (!cm_node) {
2226 /* Only type of packet accepted are for */
2227 /* the PASSIVE open (syn only) */
2228 if ((!tcph->syn) || (tcph->ack)) {
2229 skb_handled = 0;
2230 break;
2232 listener = find_listener(cm_core, nfo.loc_addr,
2233 nfo.loc_port,
2234 NES_CM_LISTENER_ACTIVE_STATE);
2235 if (!listener) {
2236 nfo.cm_id = NULL;
2237 nfo.conn_type = 0;
2238 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2239 skb_handled = 0;
2240 break;
2242 nfo.cm_id = listener->cm_id;
2243 nfo.conn_type = listener->conn_type;
2244 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2245 listener);
2246 if (!cm_node) {
2247 nes_debug(NES_DBG_CM, "Unable to allocate "
2248 "node\n");
2249 cm_packets_dropped++;
2250 atomic_dec(&listener->ref_count);
2251 dev_kfree_skb_any(skb);
2252 break;
2254 if (!tcph->rst && !tcph->fin) {
2255 cm_node->state = NES_CM_STATE_LISTENING;
2256 } else {
2257 cm_packets_dropped++;
2258 rem_ref_cm_node(cm_core, cm_node);
2259 dev_kfree_skb_any(skb);
2260 break;
2262 add_ref_cm_node(cm_node);
2263 } else if (cm_node->state == NES_CM_STATE_TSA) {
2264 rem_ref_cm_node(cm_core, cm_node);
2265 atomic_inc(&cm_accel_dropped_pkts);
2266 dev_kfree_skb_any(skb);
2267 break;
2269 skb_reset_network_header(skb);
2270 skb_set_transport_header(skb, sizeof(*tcph));
2271 skb->len = ntohs(iph->tot_len);
2272 process_packet(cm_node, skb, cm_core);
2273 rem_ref_cm_node(cm_core, cm_node);
2274 } while (0);
2275 return skb_handled;
2280 * nes_cm_alloc_core - allocate a top level instance of a cm core
2282 static struct nes_cm_core *nes_cm_alloc_core(void)
2284 struct nes_cm_core *cm_core;
2286 /* setup the CM core */
2287 /* alloc top level core control structure */
2288 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2289 if (!cm_core)
2290 return NULL;
2292 INIT_LIST_HEAD(&cm_core->connected_nodes);
2293 init_timer(&cm_core->tcp_timer);
2294 cm_core->tcp_timer.function = nes_cm_timer_tick;
2296 cm_core->mtu = NES_CM_DEFAULT_MTU;
2297 cm_core->state = NES_CM_STATE_INITED;
2298 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2300 atomic_set(&cm_core->events_posted, 0);
2302 cm_core->api = &nes_cm_api;
2304 spin_lock_init(&cm_core->ht_lock);
2305 spin_lock_init(&cm_core->listen_list_lock);
2307 INIT_LIST_HEAD(&cm_core->listen_list.list);
2309 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2311 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2312 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2313 cm_core->post_event = nes_cm_post_event;
2314 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2315 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2317 print_core(cm_core);
2318 return cm_core;
2323 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2325 static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
2327 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2329 if (!cm_core)
2330 return -EINVAL;
2332 barrier();
2334 if (timer_pending(&cm_core->tcp_timer)) {
2335 del_timer(&cm_core->tcp_timer);
2338 destroy_workqueue(cm_core->event_wq);
2339 destroy_workqueue(cm_core->disconn_wq);
2340 nes_debug(NES_DBG_CM, "\n");
2341 kfree(cm_core);
2343 return 0;
2348 * mini_cm_get
2350 static int mini_cm_get(struct nes_cm_core *cm_core)
2352 return cm_core->state;
2357 * mini_cm_set
2359 static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
2361 int ret = 0;
2363 switch (type) {
2364 case NES_CM_SET_PKT_SIZE:
2365 cm_core->mtu = value;
2366 break;
2367 case NES_CM_SET_FREE_PKT_Q_SIZE:
2368 cm_core->free_tx_pkt_max = value;
2369 break;
2370 default:
2371 /* unknown set option */
2372 ret = -EINVAL;
2375 return ret;
2380 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2381 * successfully exchanged when this is called
2383 static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2385 int ret = 0;
2387 if (!nesqp)
2388 return -EINVAL;
2390 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2391 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2392 NES_QPCONTEXT_MISC_DROS);
2394 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2395 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2397 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2399 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2401 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2402 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2404 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2405 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2406 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2408 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2409 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2410 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2412 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2413 nesqp->nesqp_context->ts_recent = 0;
2414 nesqp->nesqp_context->ts_age = 0;
2415 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2416 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2417 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2418 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2419 cm_node->tcp_cntxt.rcv_wscale);
2420 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2421 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2422 nesqp->nesqp_context->srtt = 0;
2423 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2424 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2425 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2426 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2427 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2428 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2430 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2431 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2432 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2433 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2434 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2435 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2436 le32_to_cpu(nesqp->nesqp_context->misc));
2437 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2438 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2439 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2441 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2442 cm_node->state = NES_CM_STATE_TSA;
2444 return ret;
2449 * nes_cm_disconn
2451 int nes_cm_disconn(struct nes_qp *nesqp)
2453 struct disconn_work *work;
2455 work = kzalloc(sizeof *work, GFP_ATOMIC);
2456 if (!work)
2457 return -ENOMEM; /* Timer will clean up */
2459 nes_add_ref(&nesqp->ibqp);
2460 work->nesqp = nesqp;
2461 INIT_WORK(&work->work, nes_disconnect_worker);
2462 queue_work(g_cm_core->disconn_wq, &work->work);
2463 return 0;
2468 * nes_disconnect_worker
2470 static void nes_disconnect_worker(struct work_struct *work)
2472 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2473 struct nes_qp *nesqp = dwork->nesqp;
2475 kfree(dwork);
2476 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2477 nesqp->last_aeq, nesqp->hwqp.qp_id);
2478 nes_cm_disconn_true(nesqp);
2479 nes_rem_ref(&nesqp->ibqp);
2484 * nes_cm_disconn_true
2486 static int nes_cm_disconn_true(struct nes_qp *nesqp)
2488 unsigned long flags;
2489 int ret = 0;
2490 struct iw_cm_id *cm_id;
2491 struct iw_cm_event cm_event;
2492 struct nes_vnic *nesvnic;
2493 u16 last_ae;
2494 u8 original_hw_tcp_state;
2495 u8 original_ibqp_state;
2496 enum iw_cm_event_type disconn_status = IW_CM_EVENT_STATUS_OK;
2497 int issue_disconn = 0;
2498 int issue_close = 0;
2499 int issue_flush = 0;
2500 u32 flush_q = NES_CQP_FLUSH_RQ;
2501 struct ib_event ibevent;
2503 if (!nesqp) {
2504 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2505 return -1;
2508 spin_lock_irqsave(&nesqp->lock, flags);
2509 cm_id = nesqp->cm_id;
2510 /* make sure we havent already closed this connection */
2511 if (!cm_id) {
2512 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2513 nesqp->hwqp.qp_id);
2514 spin_unlock_irqrestore(&nesqp->lock, flags);
2515 return -1;
2518 nesvnic = to_nesvnic(nesqp->ibqp.device);
2519 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2521 original_hw_tcp_state = nesqp->hw_tcp_state;
2522 original_ibqp_state = nesqp->ibqp_state;
2523 last_ae = nesqp->last_aeq;
2525 if (nesqp->term_flags) {
2526 issue_disconn = 1;
2527 issue_close = 1;
2528 nesqp->cm_id = NULL;
2529 if (nesqp->flush_issued == 0) {
2530 nesqp->flush_issued = 1;
2531 issue_flush = 1;
2533 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2534 ((original_ibqp_state == IB_QPS_RTS) &&
2535 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2536 issue_disconn = 1;
2537 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
2538 disconn_status = IW_CM_EVENT_STATUS_RESET;
2541 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2542 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2543 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2544 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2545 issue_close = 1;
2546 nesqp->cm_id = NULL;
2547 if (nesqp->flush_issued == 0) {
2548 nesqp->flush_issued = 1;
2549 issue_flush = 1;
2553 spin_unlock_irqrestore(&nesqp->lock, flags);
2555 if ((issue_flush) && (nesqp->destroyed == 0)) {
2556 /* Flush the queue(s) */
2557 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2558 flush_q |= NES_CQP_FLUSH_SQ;
2559 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2561 if (nesqp->term_flags) {
2562 ibevent.device = nesqp->ibqp.device;
2563 ibevent.event = nesqp->terminate_eventtype;
2564 ibevent.element.qp = &nesqp->ibqp;
2565 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
2569 if ((cm_id) && (cm_id->event_handler)) {
2570 if (issue_disconn) {
2571 atomic_inc(&cm_disconnects);
2572 cm_event.event = IW_CM_EVENT_DISCONNECT;
2573 cm_event.status = disconn_status;
2574 cm_event.local_addr = cm_id->local_addr;
2575 cm_event.remote_addr = cm_id->remote_addr;
2576 cm_event.private_data = NULL;
2577 cm_event.private_data_len = 0;
2579 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2580 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2581 "cm_id = %p, refcount = %u.\n",
2582 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2583 nesqp->hwqp.sq_tail, cm_id,
2584 atomic_read(&nesqp->refcount));
2586 ret = cm_id->event_handler(cm_id, &cm_event);
2587 if (ret)
2588 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2589 "returned, ret=%d\n", ret);
2592 if (issue_close) {
2593 atomic_inc(&cm_closes);
2594 nes_disconnect(nesqp, 1);
2596 cm_id->provider_data = nesqp;
2597 /* Send up the close complete event */
2598 cm_event.event = IW_CM_EVENT_CLOSE;
2599 cm_event.status = IW_CM_EVENT_STATUS_OK;
2600 cm_event.provider_data = cm_id->provider_data;
2601 cm_event.local_addr = cm_id->local_addr;
2602 cm_event.remote_addr = cm_id->remote_addr;
2603 cm_event.private_data = NULL;
2604 cm_event.private_data_len = 0;
2606 ret = cm_id->event_handler(cm_id, &cm_event);
2607 if (ret) {
2608 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2611 cm_id->rem_ref(cm_id);
2615 return 0;
2620 * nes_disconnect
2622 static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
2624 int ret = 0;
2625 struct nes_vnic *nesvnic;
2626 struct nes_device *nesdev;
2627 struct nes_ib_device *nesibdev;
2629 nesvnic = to_nesvnic(nesqp->ibqp.device);
2630 if (!nesvnic)
2631 return -EINVAL;
2633 nesdev = nesvnic->nesdev;
2634 nesibdev = nesvnic->nesibdev;
2636 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2637 atomic_read(&nesvnic->netdev->refcnt));
2639 if (nesqp->active_conn) {
2641 /* indicate this connection is NOT active */
2642 nesqp->active_conn = 0;
2643 } else {
2644 /* Need to free the Last Streaming Mode Message */
2645 if (nesqp->ietf_frame) {
2646 if (nesqp->lsmm_mr)
2647 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
2648 pci_free_consistent(nesdev->pcidev,
2649 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2650 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2654 /* close the CM node down if it is still active */
2655 if (nesqp->cm_node) {
2656 nes_debug(NES_DBG_CM, "Call close API\n");
2658 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
2661 return ret;
2666 * nes_accept
2668 int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2670 u64 u64temp;
2671 struct ib_qp *ibqp;
2672 struct nes_qp *nesqp;
2673 struct nes_vnic *nesvnic;
2674 struct nes_device *nesdev;
2675 struct nes_cm_node *cm_node;
2676 struct nes_adapter *adapter;
2677 struct ib_qp_attr attr;
2678 struct iw_cm_event cm_event;
2679 struct nes_hw_qp_wqe *wqe;
2680 struct nes_v4_quad nes_quad;
2681 u32 crc_value;
2682 int ret;
2683 int passive_state;
2684 struct nes_ib_device *nesibdev;
2685 struct ib_mr *ibmr = NULL;
2686 struct ib_phys_buf ibphysbuf;
2687 struct nes_pd *nespd;
2688 u64 tagged_offset;
2692 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2693 if (!ibqp)
2694 return -EINVAL;
2696 /* get all our handles */
2697 nesqp = to_nesqp(ibqp);
2698 nesvnic = to_nesvnic(nesqp->ibqp.device);
2699 nesdev = nesvnic->nesdev;
2700 adapter = nesdev->nesadapter;
2702 cm_node = (struct nes_cm_node *)cm_id->provider_data;
2703 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2704 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2705 nesvnic->netdev->name);
2707 /* associate the node with the QP */
2708 nesqp->cm_node = (void *)cm_node;
2709 cm_node->nesqp = nesqp;
2711 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2712 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
2713 atomic_inc(&cm_accepts);
2715 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2716 atomic_read(&nesvnic->netdev->refcnt));
2718 /* allocate the ietf frame and space for private data */
2719 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2720 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2721 &nesqp->ietf_frame_pbase);
2723 if (!nesqp->ietf_frame) {
2724 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2725 "data\n");
2726 return -ENOMEM;
2730 /* setup the MPA frame */
2731 nesqp->private_data_len = conn_param->private_data_len;
2732 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
2734 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2735 conn_param->private_data_len);
2737 nesqp->ietf_frame->priv_data_len =
2738 cpu_to_be16(conn_param->private_data_len);
2739 nesqp->ietf_frame->rev = mpa_version;
2740 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
2742 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2743 wqe = &nesqp->hwqp.sq_vbase[0];
2745 if (cm_id->remote_addr.sin_addr.s_addr !=
2746 cm_id->local_addr.sin_addr.s_addr) {
2747 u64temp = (unsigned long)nesqp;
2748 nesibdev = nesvnic->nesibdev;
2749 nespd = nesqp->nespd;
2750 ibphysbuf.addr = nesqp->ietf_frame_pbase;
2751 ibphysbuf.size = conn_param->private_data_len +
2752 sizeof(struct ietf_mpa_frame);
2753 tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
2754 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
2755 &ibphysbuf, 1,
2756 IB_ACCESS_LOCAL_WRITE,
2757 &tagged_offset);
2758 if (!ibmr) {
2759 nes_debug(NES_DBG_CM, "Unable to register memory region"
2760 "for lSMM for cm_node = %p \n",
2761 cm_node);
2762 pci_free_consistent(nesdev->pcidev,
2763 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2764 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2765 return -ENOMEM;
2768 ibmr->pd = &nespd->ibpd;
2769 ibmr->device = nespd->ibpd.device;
2770 nesqp->lsmm_mr = ibmr;
2772 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2773 set_wqe_64bit_value(wqe->wqe_words,
2774 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2775 u64temp);
2776 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2777 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2778 NES_IWARP_SQ_WQE_WRPDU);
2779 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2780 cpu_to_le32(conn_param->private_data_len +
2781 sizeof(struct ietf_mpa_frame));
2782 set_wqe_64bit_value(wqe->wqe_words,
2783 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
2784 (u64)(unsigned long)nesqp->ietf_frame);
2785 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2786 cpu_to_le32(conn_param->private_data_len +
2787 sizeof(struct ietf_mpa_frame));
2788 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
2790 nesqp->nesqp_context->ird_ord_sizes |=
2791 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2792 NES_QPCONTEXT_ORDIRD_WRPDU);
2793 } else {
2794 nesqp->nesqp_context->ird_ord_sizes |=
2795 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
2797 nesqp->skip_lsmm = 1;
2800 /* Cache the cm_id in the qp */
2801 nesqp->cm_id = cm_id;
2802 cm_node->cm_id = cm_id;
2804 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2805 cm_id->provider_data = nesqp;
2806 nesqp->active_conn = 0;
2808 if (cm_node->state == NES_CM_STATE_TSA)
2809 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2810 cm_node);
2812 nes_cm_init_tsa_conn(nesqp, cm_node);
2814 nesqp->nesqp_context->tcpPorts[0] =
2815 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2816 nesqp->nesqp_context->tcpPorts[1] =
2817 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2819 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2820 nesqp->nesqp_context->ip0 =
2821 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2822 else
2823 nesqp->nesqp_context->ip0 =
2824 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
2826 nesqp->nesqp_context->misc2 |= cpu_to_le32(
2827 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2828 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
2830 nesqp->nesqp_context->arp_index_vlan |=
2831 cpu_to_le32(nes_arp_table(nesdev,
2832 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
2833 NES_ARP_RESOLVE) << 16);
2835 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
2836 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
2838 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2840 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
2841 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2842 nesqp->nesqp_context->ird_ord_sizes |=
2843 cpu_to_le32((u32)conn_param->ord);
2845 memset(&nes_quad, 0, sizeof(nes_quad));
2846 nes_quad.DstIpAdrIndex =
2847 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2848 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2849 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2850 else
2851 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2852 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2853 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
2855 /* Produce hash key */
2856 crc_value = get_crc_value(&nes_quad);
2857 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
2858 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
2859 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
2861 nesqp->hte_index &= adapter->hte_index_mask;
2862 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2864 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2866 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2867 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2868 "private data length=%zu.\n", nesqp->hwqp.qp_id,
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),
2873 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2874 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2875 conn_param->private_data_len +
2876 sizeof(struct ietf_mpa_frame));
2879 /* notify OF layer that accept event was successful */
2880 cm_id->add_ref(cm_id);
2881 nes_add_ref(&nesqp->ibqp);
2883 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2884 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2885 cm_event.provider_data = (void *)nesqp;
2886 cm_event.local_addr = cm_id->local_addr;
2887 cm_event.remote_addr = cm_id->remote_addr;
2888 cm_event.private_data = NULL;
2889 cm_event.private_data_len = 0;
2890 ret = cm_id->event_handler(cm_id, &cm_event);
2891 attr.qp_state = IB_QPS_RTS;
2892 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
2893 if (cm_node->loopbackpartner) {
2894 cm_node->loopbackpartner->mpa_frame_size =
2895 nesqp->private_data_len;
2896 /* copy entire MPA frame to our cm_node's frame */
2897 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2898 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
2899 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2901 if (ret)
2902 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2903 "ret=%d\n", __func__, __LINE__, ret);
2905 passive_state = atomic_add_return(1, &cm_node->passive_state);
2906 if (passive_state == NES_SEND_RESET_EVENT)
2907 create_event(cm_node, NES_CM_EVENT_RESET);
2908 return 0;
2913 * nes_reject
2915 int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2917 struct nes_cm_node *cm_node;
2918 struct nes_cm_node *loopback;
2920 struct nes_cm_core *cm_core;
2922 atomic_inc(&cm_rejects);
2923 cm_node = (struct nes_cm_node *) cm_id->provider_data;
2924 loopback = cm_node->loopbackpartner;
2925 cm_core = cm_node->cm_core;
2926 cm_node->cm_id = cm_id;
2927 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2929 if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
2930 return -EINVAL;
2932 strcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP);
2933 if (loopback) {
2934 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
2935 loopback->mpa_frame.priv_data_len = pdata_len;
2936 loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
2937 pdata_len;
2938 } else {
2939 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2940 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2943 cm_node->mpa_frame.rev = mpa_version;
2944 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2946 return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
2951 * nes_connect
2952 * setup and launch cm connect node
2954 int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2956 struct ib_qp *ibqp;
2957 struct nes_qp *nesqp;
2958 struct nes_vnic *nesvnic;
2959 struct nes_device *nesdev;
2960 struct nes_cm_node *cm_node;
2961 struct nes_cm_info cm_info;
2962 int apbvt_set = 0;
2964 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2965 if (!ibqp)
2966 return -EINVAL;
2967 nesqp = to_nesqp(ibqp);
2968 if (!nesqp)
2969 return -EINVAL;
2970 nesvnic = to_nesvnic(nesqp->ibqp.device);
2971 if (!nesvnic)
2972 return -EINVAL;
2973 nesdev = nesvnic->nesdev;
2974 if (!nesdev)
2975 return -EINVAL;
2977 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
2978 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
2979 ntohl(nesvnic->local_ipaddr),
2980 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2981 ntohs(cm_id->remote_addr.sin_port),
2982 ntohl(cm_id->local_addr.sin_addr.s_addr),
2983 ntohs(cm_id->local_addr.sin_port));
2985 atomic_inc(&cm_connects);
2986 nesqp->active_conn = 1;
2988 /* cache the cm_id in the qp */
2989 nesqp->cm_id = cm_id;
2991 cm_id->provider_data = nesqp;
2993 nesqp->private_data_len = conn_param->private_data_len;
2994 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
2995 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
2996 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
2997 conn_param->private_data_len);
2999 if (cm_id->local_addr.sin_addr.s_addr !=
3000 cm_id->remote_addr.sin_addr.s_addr) {
3001 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
3002 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
3003 apbvt_set = 1;
3006 /* set up the connection params for the node */
3007 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3008 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3009 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3010 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
3011 cm_info.cm_id = cm_id;
3012 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3014 cm_id->add_ref(cm_id);
3016 /* create a connect CM node connection */
3017 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3018 conn_param->private_data_len, (void *)conn_param->private_data,
3019 &cm_info);
3020 if (!cm_node) {
3021 if (apbvt_set)
3022 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
3023 PCI_FUNC(nesdev->pcidev->devfn),
3024 NES_MANAGE_APBVT_DEL);
3026 cm_id->rem_ref(cm_id);
3027 return -ENOMEM;
3030 cm_node->apbvt_set = apbvt_set;
3031 nesqp->cm_node = cm_node;
3032 cm_node->nesqp = nesqp;
3033 nes_add_ref(&nesqp->ibqp);
3035 return 0;
3040 * nes_create_listen
3042 int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3044 struct nes_vnic *nesvnic;
3045 struct nes_cm_listener *cm_node;
3046 struct nes_cm_info cm_info;
3047 struct nes_adapter *adapter;
3048 int err;
3051 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3052 cm_id, ntohs(cm_id->local_addr.sin_port));
3054 nesvnic = to_nesvnic(cm_id->device);
3055 if (!nesvnic)
3056 return -EINVAL;
3057 adapter = nesvnic->nesdev->nesadapter;
3058 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3059 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3061 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3062 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3064 /* setup listen params in our api call struct */
3065 cm_info.loc_addr = nesvnic->local_ipaddr;
3066 cm_info.loc_port = cm_id->local_addr.sin_port;
3067 cm_info.backlog = backlog;
3068 cm_info.cm_id = cm_id;
3070 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3073 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3074 if (!cm_node) {
3075 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
3076 __func__, __LINE__);
3077 return -ENOMEM;
3080 cm_id->provider_data = cm_node;
3082 if (!cm_node->reused_node) {
3083 err = nes_manage_apbvt(nesvnic,
3084 ntohs(cm_id->local_addr.sin_port),
3085 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3086 NES_MANAGE_APBVT_ADD);
3087 if (err) {
3088 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3089 err);
3090 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3091 return err;
3093 cm_listens_created++;
3096 cm_id->add_ref(cm_id);
3097 cm_id->provider_data = (void *)cm_node;
3100 return 0;
3105 * nes_destroy_listen
3107 int nes_destroy_listen(struct iw_cm_id *cm_id)
3109 if (cm_id->provider_data)
3110 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3111 else
3112 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3114 cm_id->rem_ref(cm_id);
3116 return 0;
3121 * nes_cm_recv
3123 int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3125 int rc = 0;
3126 cm_packets_received++;
3127 if ((g_cm_core) && (g_cm_core->api)) {
3128 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
3129 } else {
3130 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3131 " cm is not setup properly.\n");
3134 return rc;
3139 * nes_cm_start
3140 * Start and init a cm core module
3142 int nes_cm_start(void)
3144 nes_debug(NES_DBG_CM, "\n");
3145 /* create the primary CM core, pass this handle to subsequent core inits */
3146 g_cm_core = nes_cm_alloc_core();
3147 if (g_cm_core) {
3148 return 0;
3149 } else {
3150 return -ENOMEM;
3156 * nes_cm_stop
3157 * stop and dealloc all cm core instances
3159 int nes_cm_stop(void)
3161 g_cm_core->api->destroy_cm_core(g_cm_core);
3162 return 0;
3167 * cm_event_connected
3168 * handle a connected event, setup QPs and HW
3170 static void cm_event_connected(struct nes_cm_event *event)
3172 u64 u64temp;
3173 struct nes_qp *nesqp;
3174 struct nes_vnic *nesvnic;
3175 struct nes_device *nesdev;
3176 struct nes_cm_node *cm_node;
3177 struct nes_adapter *nesadapter;
3178 struct ib_qp_attr attr;
3179 struct iw_cm_id *cm_id;
3180 struct iw_cm_event cm_event;
3181 struct nes_hw_qp_wqe *wqe;
3182 struct nes_v4_quad nes_quad;
3183 u32 crc_value;
3184 int ret;
3186 /* get all our handles */
3187 cm_node = event->cm_node;
3188 cm_id = cm_node->cm_id;
3189 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3190 nesqp = (struct nes_qp *)cm_id->provider_data;
3191 nesvnic = to_nesvnic(nesqp->ibqp.device);
3192 nesdev = nesvnic->nesdev;
3193 nesadapter = nesdev->nesadapter;
3195 if (nesqp->destroyed) {
3196 return;
3198 atomic_inc(&cm_connecteds);
3199 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3200 " local port 0x%04X. jiffies = %lu.\n",
3201 nesqp->hwqp.qp_id,
3202 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3203 ntohs(cm_id->remote_addr.sin_port),
3204 ntohs(cm_id->local_addr.sin_port),
3205 jiffies);
3207 nes_cm_init_tsa_conn(nesqp, cm_node);
3209 /* set the QP tsa context */
3210 nesqp->nesqp_context->tcpPorts[0] =
3211 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3212 nesqp->nesqp_context->tcpPorts[1] =
3213 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3214 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3215 nesqp->nesqp_context->ip0 =
3216 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3217 else
3218 nesqp->nesqp_context->ip0 =
3219 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
3221 nesqp->nesqp_context->misc2 |= cpu_to_le32(
3222 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3223 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
3224 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
3225 nes_arp_table(nesdev,
3226 le32_to_cpu(nesqp->nesqp_context->ip0),
3227 NULL, NES_ARP_RESOLVE) << 16);
3228 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3229 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3230 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3231 nesqp->nesqp_context->ird_ord_sizes |=
3232 cpu_to_le32((u32)1 <<
3233 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
3235 /* Adjust tail for not having a LSMM */
3236 nesqp->hwqp.sq_tail = 1;
3238 #if defined(NES_SEND_FIRST_WRITE)
3239 if (cm_node->send_write0) {
3240 nes_debug(NES_DBG_CM, "Sending first write.\n");
3241 wqe = &nesqp->hwqp.sq_vbase[0];
3242 u64temp = (unsigned long)nesqp;
3243 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3244 set_wqe_64bit_value(wqe->wqe_words,
3245 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3246 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3247 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3248 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3249 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3250 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3251 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3252 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
3254 /* use the reserved spot on the WQ for the extra first WQE */
3255 nesqp->nesqp_context->ird_ord_sizes &=
3256 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3257 NES_QPCONTEXT_ORDIRD_WRPDU |
3258 NES_QPCONTEXT_ORDIRD_ALSMM));
3259 nesqp->skip_lsmm = 1;
3260 nesqp->hwqp.sq_tail = 0;
3261 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3262 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3264 #endif
3266 memset(&nes_quad, 0, sizeof(nes_quad));
3268 nes_quad.DstIpAdrIndex =
3269 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3270 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3271 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3272 else
3273 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
3274 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3275 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3277 /* Produce hash key */
3278 crc_value = get_crc_value(&nes_quad);
3279 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
3280 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3281 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3283 nesqp->hte_index &= nesadapter->hte_index_mask;
3284 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3286 nesqp->ietf_frame = &cm_node->mpa_frame;
3287 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3288 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3290 /* notify OF layer we successfully created the requested connection */
3291 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3292 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3293 cm_event.provider_data = cm_id->provider_data;
3294 cm_event.local_addr.sin_family = AF_INET;
3295 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3296 cm_event.remote_addr = cm_id->remote_addr;
3298 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3299 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
3301 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3302 ret = cm_id->event_handler(cm_id, &cm_event);
3303 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3305 if (ret)
3306 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3307 "ret=%d\n", __func__, __LINE__, ret);
3308 attr.qp_state = IB_QPS_RTS;
3309 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
3311 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3312 "%lu\n", nesqp->hwqp.qp_id, jiffies);
3314 return;
3319 * cm_event_connect_error
3321 static void cm_event_connect_error(struct nes_cm_event *event)
3323 struct nes_qp *nesqp;
3324 struct iw_cm_id *cm_id;
3325 struct iw_cm_event cm_event;
3326 /* struct nes_cm_info cm_info; */
3327 int ret;
3329 if (!event->cm_node)
3330 return;
3332 cm_id = event->cm_node->cm_id;
3333 if (!cm_id) {
3334 return;
3337 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3338 nesqp = cm_id->provider_data;
3340 if (!nesqp) {
3341 return;
3344 /* notify OF layer about this connection error event */
3345 /* cm_id->rem_ref(cm_id); */
3346 nesqp->cm_id = NULL;
3347 cm_id->provider_data = NULL;
3348 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3349 cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
3350 cm_event.provider_data = cm_id->provider_data;
3351 cm_event.local_addr = cm_id->local_addr;
3352 cm_event.remote_addr = cm_id->remote_addr;
3353 cm_event.private_data = NULL;
3354 cm_event.private_data_len = 0;
3356 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3357 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3358 cm_event.remote_addr.sin_addr.s_addr);
3360 ret = cm_id->event_handler(cm_id, &cm_event);
3361 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3362 if (ret)
3363 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3364 "ret=%d\n", __func__, __LINE__, ret);
3365 cm_id->rem_ref(cm_id);
3367 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
3368 return;
3373 * cm_event_reset
3375 static void cm_event_reset(struct nes_cm_event *event)
3377 struct nes_qp *nesqp;
3378 struct iw_cm_id *cm_id;
3379 struct iw_cm_event cm_event;
3380 /* struct nes_cm_info cm_info; */
3381 int ret;
3383 if (!event->cm_node)
3384 return;
3386 if (!event->cm_node->cm_id)
3387 return;
3389 cm_id = event->cm_node->cm_id;
3391 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3392 nesqp = cm_id->provider_data;
3394 nesqp->cm_id = NULL;
3395 /* cm_id->provider_data = NULL; */
3396 cm_event.event = IW_CM_EVENT_DISCONNECT;
3397 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3398 cm_event.provider_data = cm_id->provider_data;
3399 cm_event.local_addr = cm_id->local_addr;
3400 cm_event.remote_addr = cm_id->remote_addr;
3401 cm_event.private_data = NULL;
3402 cm_event.private_data_len = 0;
3404 ret = cm_id->event_handler(cm_id, &cm_event);
3405 cm_id->add_ref(cm_id);
3406 atomic_inc(&cm_closes);
3407 cm_event.event = IW_CM_EVENT_CLOSE;
3408 cm_event.status = IW_CM_EVENT_STATUS_OK;
3409 cm_event.provider_data = cm_id->provider_data;
3410 cm_event.local_addr = cm_id->local_addr;
3411 cm_event.remote_addr = cm_id->remote_addr;
3412 cm_event.private_data = NULL;
3413 cm_event.private_data_len = 0;
3414 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3415 ret = cm_id->event_handler(cm_id, &cm_event);
3417 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3420 /* notify OF layer about this connection error event */
3421 cm_id->rem_ref(cm_id);
3423 return;
3428 * cm_event_mpa_req
3430 static void cm_event_mpa_req(struct nes_cm_event *event)
3432 struct iw_cm_id *cm_id;
3433 struct iw_cm_event cm_event;
3434 int ret;
3435 struct nes_cm_node *cm_node;
3437 cm_node = event->cm_node;
3438 if (!cm_node)
3439 return;
3440 cm_id = cm_node->cm_id;
3442 atomic_inc(&cm_connect_reqs);
3443 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3444 cm_node, cm_id, jiffies);
3446 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3447 cm_event.status = IW_CM_EVENT_STATUS_OK;
3448 cm_event.provider_data = (void *)cm_node;
3450 cm_event.local_addr.sin_family = AF_INET;
3451 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3452 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3454 cm_event.remote_addr.sin_family = AF_INET;
3455 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3456 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3457 cm_event.private_data = cm_node->mpa_frame_buf;
3458 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3460 ret = cm_id->event_handler(cm_id, &cm_event);
3461 if (ret)
3462 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3463 __func__, __LINE__, ret);
3464 return;
3468 static void cm_event_mpa_reject(struct nes_cm_event *event)
3470 struct iw_cm_id *cm_id;
3471 struct iw_cm_event cm_event;
3472 struct nes_cm_node *cm_node;
3473 int ret;
3475 cm_node = event->cm_node;
3476 if (!cm_node)
3477 return;
3478 cm_id = cm_node->cm_id;
3480 atomic_inc(&cm_connect_reqs);
3481 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3482 cm_node, cm_id, jiffies);
3484 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3485 cm_event.status = -ECONNREFUSED;
3486 cm_event.provider_data = cm_id->provider_data;
3488 cm_event.local_addr.sin_family = AF_INET;
3489 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3490 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3492 cm_event.remote_addr.sin_family = AF_INET;
3493 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3494 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3496 cm_event.private_data = cm_node->mpa_frame_buf;
3497 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3499 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3500 "remove_addr=%08x\n",
3501 cm_event.local_addr.sin_addr.s_addr,
3502 cm_event.remote_addr.sin_addr.s_addr);
3504 ret = cm_id->event_handler(cm_id, &cm_event);
3505 if (ret)
3506 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3507 __func__, __LINE__, ret);
3509 return;
3513 static void nes_cm_event_handler(struct work_struct *);
3516 * nes_cm_post_event
3517 * post an event to the cm event handler
3519 static int nes_cm_post_event(struct nes_cm_event *event)
3521 atomic_inc(&event->cm_node->cm_core->events_posted);
3522 add_ref_cm_node(event->cm_node);
3523 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3524 INIT_WORK(&event->event_work, nes_cm_event_handler);
3525 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3526 event->cm_node, event);
3528 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3530 nes_debug(NES_DBG_CM, "Exit\n");
3531 return 0;
3536 * nes_cm_event_handler
3537 * worker function to handle cm events
3538 * will free instance of nes_cm_event
3540 static void nes_cm_event_handler(struct work_struct *work)
3542 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3543 event_work);
3544 struct nes_cm_core *cm_core;
3546 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
3547 return;
3549 cm_core = event->cm_node->cm_core;
3550 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
3551 event, event->type, atomic_read(&cm_core->events_posted));
3553 switch (event->type) {
3554 case NES_CM_EVENT_MPA_REQ:
3555 cm_event_mpa_req(event);
3556 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3557 event->cm_node);
3558 break;
3559 case NES_CM_EVENT_RESET:
3560 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3561 event->cm_node);
3562 cm_event_reset(event);
3563 break;
3564 case NES_CM_EVENT_CONNECTED:
3565 if ((!event->cm_node->cm_id) ||
3566 (event->cm_node->state != NES_CM_STATE_TSA))
3567 break;
3568 cm_event_connected(event);
3569 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3570 break;
3571 case NES_CM_EVENT_MPA_REJECT:
3572 if ((!event->cm_node->cm_id) ||
3573 (event->cm_node->state == NES_CM_STATE_TSA))
3574 break;
3575 cm_event_mpa_reject(event);
3576 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3577 break;
3579 case NES_CM_EVENT_ABORTED:
3580 if ((!event->cm_node->cm_id) ||
3581 (event->cm_node->state == NES_CM_STATE_TSA))
3582 break;
3583 cm_event_connect_error(event);
3584 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3585 break;
3586 case NES_CM_EVENT_DROPPED_PKT:
3587 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3588 break;
3589 default:
3590 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3591 break;
3594 atomic_dec(&cm_core->events_posted);
3595 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3596 rem_ref_cm_node(cm_core, event->cm_node);
3597 kfree(event);
3599 return;