2 * Copyright (c) 2006 - 2008 NetEffect, 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
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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
35 #define TCPOPT_TIMESTAMP 8
37 #include <asm/atomic.h>
38 #include <linux/skbuff.h>
40 #include <linux/tcp.h>
41 #include <linux/init.h>
42 #include <linux/if_arp.h>
43 #include <linux/notifier.h>
44 #include <linux/net.h>
45 #include <linux/types.h>
46 #include <linux/timer.h>
47 #include <linux/time.h>
48 #include <linux/delay.h>
49 #include <linux/etherdevice.h>
50 #include <linux/netdevice.h>
51 #include <linux/random.h>
52 #include <linux/list.h>
53 #include <linux/threads.h>
55 #include <net/neighbour.h>
56 #include <net/route.h>
57 #include <net/ip_fib.h>
62 u32 cm_packets_bounced;
63 u32 cm_packets_dropped;
64 u32 cm_packets_retrans;
65 u32 cm_packets_created;
66 u32 cm_packets_received;
67 u32 cm_listens_created;
68 u32 cm_listens_destroyed;
70 atomic_t cm_loopbacks;
71 atomic_t cm_nodes_created;
72 atomic_t cm_nodes_destroyed;
73 atomic_t cm_accel_dropped_pkts;
74 atomic_t cm_resets_recvd;
76 static inline int mini_cm_accelerated(struct nes_cm_core *, struct nes_cm_node *);
77 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
78 struct nes_vnic *, struct nes_cm_info *);
79 static int add_ref_cm_node(struct nes_cm_node *);
80 static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
81 static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
84 /* External CM API Interface */
85 /* instance of function pointers for client API */
86 /* set address of this instance to cm_core->cm_ops at cm_core alloc */
87 static struct nes_cm_ops nes_cm_api = {
101 struct nes_cm_core *g_cm_core;
103 atomic_t cm_connects;
105 atomic_t cm_disconnects;
107 atomic_t cm_connecteds;
108 atomic_t cm_connect_reqs;
115 static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
116 enum nes_cm_event_type type)
118 struct nes_cm_event *event;
123 /* allocate an empty event */
124 event = kzalloc(sizeof(*event), GFP_ATOMIC);
130 event->cm_node = cm_node;
131 event->cm_info.rem_addr = cm_node->rem_addr;
132 event->cm_info.loc_addr = cm_node->loc_addr;
133 event->cm_info.rem_port = cm_node->rem_port;
134 event->cm_info.loc_port = cm_node->loc_port;
135 event->cm_info.cm_id = cm_node->cm_id;
137 nes_debug(NES_DBG_CM, "Created event=%p, type=%u, dst_addr=%08x[%x],"
138 " src_addr=%08x[%x]\n",
140 event->cm_info.loc_addr, event->cm_info.loc_port,
141 event->cm_info.rem_addr, event->cm_info.rem_port);
143 nes_cm_post_event(event);
151 int send_mpa_request(struct nes_cm_node *cm_node)
156 skb = get_free_pkt(cm_node);
158 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
162 /* send an MPA Request frame */
163 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
164 cm_node->mpa_frame_size, SET_ACK);
166 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
176 * recv_mpa - process a received TCP pkt, we are expecting an
179 static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 len)
181 struct ietf_mpa_frame *mpa_frame;
183 /* assume req frame is in tcp data payload */
184 if (len < sizeof(struct ietf_mpa_frame)) {
185 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
189 mpa_frame = (struct ietf_mpa_frame *)buffer;
190 cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
192 if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
193 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
194 " complete (%x + %x != %x)\n",
195 cm_node->mpa_frame_size, (u32)sizeof(struct ietf_mpa_frame), len);
199 /* copy entire MPA frame to our cm_node's frame */
200 memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
201 cm_node->mpa_frame_size);
208 * handle_exception_pkt - process an exception packet.
209 * We have been in a TSA state, and we have now received SW
210 * TCP/IP traffic should be a FIN request or IP pkt with options
212 static int handle_exception_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb)
215 struct tcphdr *tcph = tcp_hdr(skb);
217 /* first check to see if this a FIN pkt */
219 /* we need to ACK the FIN request */
222 /* check which side we are (client/server) and set next state accordingly */
223 if (cm_node->tcp_cntxt.client)
224 cm_node->state = NES_CM_STATE_CLOSING;
226 /* we are the server side */
227 cm_node->state = NES_CM_STATE_CLOSE_WAIT;
228 /* since this is a self contained CM we don't wait for */
229 /* an APP to close us, just send final FIN immediately */
230 ret = send_fin(cm_node, NULL);
231 cm_node->state = NES_CM_STATE_LAST_ACK;
242 * form_cm_frame - get a free packet and build empty frame Use
243 * node info to build.
245 struct sk_buff *form_cm_frame(struct sk_buff *skb, struct nes_cm_node *cm_node,
246 void *options, u32 optionsize, void *data, u32 datasize, u8 flags)
252 u16 packetsize = sizeof(*iph);
254 packetsize += sizeof(*tcph);
255 packetsize += optionsize + datasize;
257 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
260 buf = skb_put(skb, packetsize + ETH_HLEN);
262 ethh = (struct ethhdr *) buf;
265 iph = (struct iphdr *)buf;
267 tcph = (struct tcphdr *)buf;
268 skb_reset_mac_header(skb);
269 skb_set_network_header(skb, ETH_HLEN);
270 skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
271 buf += sizeof(*tcph);
273 skb->ip_summed = CHECKSUM_PARTIAL;
274 skb->protocol = htons(0x800);
276 skb->mac_len = ETH_HLEN;
278 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
279 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
280 ethh->h_proto = htons(0x0800);
282 iph->version = IPVERSION;
283 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
285 iph->tot_len = htons(packetsize);
286 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
288 iph->frag_off = htons(0x4000);
290 iph->protocol = 0x06; /* IPPROTO_TCP */
292 iph->saddr = htonl(cm_node->loc_addr);
293 iph->daddr = htonl(cm_node->rem_addr);
295 tcph->source = htons(cm_node->loc_port);
296 tcph->dest = htons(cm_node->rem_port);
297 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
299 if (flags & SET_ACK) {
300 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
301 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
306 if (flags & SET_SYN) {
307 cm_node->tcp_cntxt.loc_seq_num++;
310 cm_node->tcp_cntxt.loc_seq_num += datasize; /* data (no headers) */
318 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
319 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
322 memcpy(buf, options, optionsize);
325 memcpy(buf, data, datasize);
327 skb_shinfo(skb)->nr_frags = 0;
328 cm_packets_created++;
335 * print_core - dump a cm core
337 static void print_core(struct nes_cm_core *core)
339 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
340 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
343 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
344 nes_debug(NES_DBG_CM, "Session ID : %u \n", atomic_read(&core->session_id));
346 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
348 nes_debug(NES_DBG_CM, "Tx Free cnt : %u \n", skb_queue_len(&core->tx_free_list));
349 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
350 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
352 nes_debug(NES_DBG_CM, "core : %p \n", core);
354 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
360 * note - cm_node needs to be protected before calling this. Encase in:
361 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
363 int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
364 enum nes_timer_type type, int send_retrans,
365 int close_when_complete)
368 struct nes_cm_core *cm_core;
369 struct nes_timer_entry *new_send;
375 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
379 /* new_send->timetosend = currenttime */
380 new_send->retrycount = NES_DEFAULT_RETRYS;
381 new_send->retranscount = NES_DEFAULT_RETRANS;
383 new_send->timetosend = jiffies;
384 new_send->type = type;
385 new_send->netdev = cm_node->netdev;
386 new_send->send_retrans = send_retrans;
387 new_send->close_when_complete = close_when_complete;
389 if (type == NES_TIMER_TYPE_CLOSE) {
390 new_send->timetosend += (HZ/2); /* TODO: decide on the correct value here */
391 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
392 list_add_tail(&new_send->list, &cm_node->recv_list);
393 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
396 if (type == NES_TIMER_TYPE_SEND) {
397 new_send->seq_num = htonl(tcp_hdr(skb)->seq);
398 atomic_inc(&new_send->skb->users);
400 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
401 if (ret != NETDEV_TX_OK) {
402 nes_debug(NES_DBG_CM, "Error sending packet %p (jiffies = %lu)\n",
404 atomic_dec(&new_send->skb->users);
405 new_send->timetosend = jiffies;
409 if (close_when_complete)
410 rem_ref_cm_node(cm_node->cm_core, cm_node);
411 dev_kfree_skb_any(new_send->skb);
415 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
417 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
418 list_add_tail(&new_send->list, &cm_node->retrans_list);
419 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
421 if (type == NES_TIMER_TYPE_RECV) {
422 new_send->seq_num = htonl(tcp_hdr(skb)->seq);
423 new_send->timetosend = jiffies;
424 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
425 list_add_tail(&new_send->list, &cm_node->recv_list);
426 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
428 cm_core = cm_node->cm_core;
430 was_timer_set = timer_pending(&cm_core->tcp_timer);
432 if (!was_timer_set) {
433 cm_core->tcp_timer.expires = new_send->timetosend;
434 add_timer(&cm_core->tcp_timer);
444 void nes_cm_timer_tick(unsigned long pass)
446 unsigned long flags, qplockflags;
447 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
448 struct iw_cm_id *cm_id;
449 struct nes_cm_node *cm_node;
450 struct nes_timer_entry *send_entry, *recv_entry;
451 struct list_head *list_core, *list_core_temp;
452 struct list_head *list_node, *list_node_temp;
453 struct nes_cm_core *cm_core = g_cm_core;
454 struct nes_qp *nesqp;
457 int ret = NETDEV_TX_OK;
460 spin_lock_irqsave(&cm_core->ht_lock, flags);
462 list_for_each_safe(list_node, list_core_temp, &cm_core->connected_nodes) {
463 cm_node = container_of(list_node, struct nes_cm_node, list);
464 add_ref_cm_node(cm_node);
465 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
466 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
467 list_for_each_safe(list_core, list_node_temp, &cm_node->recv_list) {
468 recv_entry = container_of(list_core, struct nes_timer_entry, list);
469 if ((time_after(recv_entry->timetosend, jiffies)) &&
470 (recv_entry->type == NES_TIMER_TYPE_CLOSE)) {
471 if (nexttimeout > recv_entry->timetosend || !settimer) {
472 nexttimeout = recv_entry->timetosend;
477 list_del(&recv_entry->list);
478 cm_id = cm_node->cm_id;
479 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
480 if (recv_entry->type == NES_TIMER_TYPE_CLOSE) {
481 nesqp = (struct nes_qp *)recv_entry->skb;
482 spin_lock_irqsave(&nesqp->lock, qplockflags);
484 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, refcount = %d: "
485 "****** HIT A NES_TIMER_TYPE_CLOSE"
486 " with something to do!!! ******\n",
487 nesqp->hwqp.qp_id, cm_id,
488 atomic_read(&nesqp->refcount));
489 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
490 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
491 nesqp->ibqp_state = IB_QPS_ERR;
492 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
493 nes_cm_disconn(nesqp);
495 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
496 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, refcount = %d:"
497 " ****** HIT A NES_TIMER_TYPE_CLOSE"
498 " with nothing to do!!! ******\n",
499 nesqp->hwqp.qp_id, cm_id,
500 atomic_read(&nesqp->refcount));
501 nes_rem_ref(&nesqp->ibqp);
504 cm_id->rem_ref(cm_id);
507 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
509 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
511 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
513 list_for_each_safe(list_core, list_node_temp, &cm_node->retrans_list) {
517 send_entry = container_of(list_core, struct nes_timer_entry, list);
518 if (time_after(send_entry->timetosend, jiffies)) {
519 if (cm_node->state != NES_CM_STATE_TSA) {
520 if ((nexttimeout > send_entry->timetosend) || !settimer) {
521 nexttimeout = send_entry->timetosend;
527 list_del(&send_entry->list);
528 skb = send_entry->skb;
529 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
530 dev_kfree_skb_any(skb);
532 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
536 if (send_entry->type == NES_TIMER_NODE_CLEANUP) {
537 list_del(&send_entry->list);
538 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
540 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
543 if ((send_entry->seq_num < cm_node->tcp_cntxt.rem_ack_num) ||
544 (cm_node->state == NES_CM_STATE_TSA) ||
545 (cm_node->state == NES_CM_STATE_CLOSED)) {
546 skb = send_entry->skb;
547 list_del(&send_entry->list);
548 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
550 dev_kfree_skb_any(skb);
551 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
555 if (!send_entry->retranscount || !send_entry->retrycount) {
556 cm_packets_dropped++;
557 skb = send_entry->skb;
558 list_del(&send_entry->list);
559 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
560 dev_kfree_skb_any(skb);
562 if (cm_node->state == NES_CM_STATE_SYN_RCVD) {
563 /* this node never even generated an indication up to the cm */
564 rem_ref_cm_node(cm_core, cm_node);
566 cm_node->state = NES_CM_STATE_CLOSED;
567 create_event(cm_node, NES_CM_EVENT_ABORTED);
569 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
572 /* this seems like the correct place, but leave send entry unprotected */
573 // spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
574 atomic_inc(&send_entry->skb->users);
575 cm_packets_retrans++;
576 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p for node %p,"
577 " jiffies = %lu, time to send = %lu, retranscount = %u, "
578 "send_entry->seq_num = 0x%08X, cm_node->tcp_cntxt.rem_ack_num = 0x%08X\n",
579 send_entry, cm_node, jiffies, send_entry->timetosend, send_entry->retranscount,
580 send_entry->seq_num, cm_node->tcp_cntxt.rem_ack_num);
582 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
583 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
584 if (ret != NETDEV_TX_OK) {
585 cm_packets_bounced++;
586 atomic_dec(&send_entry->skb->users);
587 send_entry->retrycount--;
588 nexttimeout = jiffies + NES_SHORT_TIME;
591 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
596 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
597 list_del(&send_entry->list);
598 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = %u, retry count = %u.\n",
599 send_entry->retranscount, send_entry->retrycount);
600 if (send_entry->send_retrans) {
601 send_entry->retranscount--;
602 send_entry->timetosend = jiffies + NES_RETRY_TIMEOUT;
603 if (nexttimeout > send_entry->timetosend || !settimer) {
604 nexttimeout = send_entry->timetosend;
607 list_add(&send_entry->list, &cm_node->retrans_list);
610 int close_when_complete;
611 skb = send_entry->skb;
612 close_when_complete = send_entry->close_when_complete;
613 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
614 if (close_when_complete) {
615 BUG_ON(atomic_read(&cm_node->ref_count) == 1);
616 rem_ref_cm_node(cm_core, cm_node);
618 dev_kfree_skb_any(skb);
620 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
624 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
626 rem_ref_cm_node(cm_core, cm_node);
628 spin_lock_irqsave(&cm_core->ht_lock, flags);
629 if (ret != NETDEV_TX_OK)
632 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
635 if (!timer_pending(&cm_core->tcp_timer)) {
636 cm_core->tcp_timer.expires = nexttimeout;
637 add_timer(&cm_core->tcp_timer);
646 int send_syn(struct nes_cm_node *cm_node, u32 sendack)
651 char optionsbuffer[sizeof(struct option_mss) +
652 sizeof(struct option_windowscale) +
653 sizeof(struct option_base) + 1];
656 /* Sending MSS option */
657 union all_known_options *options;
662 options = (union all_known_options *)&optionsbuffer[optionssize];
663 options->as_mss.optionnum = OPTION_NUMBER_MSS;
664 options->as_mss.length = sizeof(struct option_mss);
665 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
666 optionssize += sizeof(struct option_mss);
668 options = (union all_known_options *)&optionsbuffer[optionssize];
669 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
670 options->as_windowscale.length = sizeof(struct option_windowscale);
671 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
672 optionssize += sizeof(struct option_windowscale);
674 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)
676 options = (union all_known_options *)&optionsbuffer[optionssize];
677 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
678 options->as_base.length = sizeof(struct option_base);
679 optionssize += sizeof(struct option_base);
680 /* we need the size to be a multiple of 4 */
681 options = (union all_known_options *)&optionsbuffer[optionssize];
684 options = (union all_known_options *)&optionsbuffer[optionssize];
689 options = (union all_known_options *)&optionsbuffer[optionssize];
690 options->as_end = OPTION_NUMBER_END;
693 skb = get_free_pkt(cm_node);
695 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
702 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
703 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
712 int send_reset(struct nes_cm_node *cm_node)
715 struct sk_buff *skb = get_free_pkt(cm_node);
716 int flags = SET_RST | SET_ACK;
719 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
723 add_ref_cm_node(cm_node);
724 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
725 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
734 int send_ack(struct nes_cm_node *cm_node)
737 struct sk_buff *skb = get_free_pkt(cm_node);
740 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
744 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
745 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
754 int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
758 /* if we didn't get a frame get one */
760 skb = get_free_pkt(cm_node);
763 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
767 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
768 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
777 struct sk_buff *get_free_pkt(struct nes_cm_node *cm_node)
779 struct sk_buff *skb, *new_skb;
781 /* check to see if we need to repopulate the free tx pkt queue */
782 if (skb_queue_len(&cm_node->cm_core->tx_free_list) < NES_CM_FREE_PKT_LO_WATERMARK) {
783 while (skb_queue_len(&cm_node->cm_core->tx_free_list) <
784 cm_node->cm_core->free_tx_pkt_max) {
785 /* replace the frame we took, we won't get it back */
786 new_skb = dev_alloc_skb(cm_node->cm_core->mtu);
788 /* add a replacement frame to the free tx list head */
789 skb_queue_head(&cm_node->cm_core->tx_free_list, new_skb);
793 skb = skb_dequeue(&cm_node->cm_core->tx_free_list);
800 * make_hashkey - generate hash key from node tuple
802 static inline int make_hashkey(u16 loc_port, nes_addr_t loc_addr, u16 rem_port,
807 hashkey = loc_addr + rem_addr + loc_port + rem_port;
808 hashkey = (hashkey % NES_CM_HASHTABLE_SIZE);
815 * find_node - find a cm node that matches the reference cm node
817 static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
818 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
822 struct list_head *list_pos;
823 struct list_head *hte;
824 struct nes_cm_node *cm_node;
826 /* make a hash index key for this packet */
827 hashkey = make_hashkey(loc_port, loc_addr, rem_port, rem_addr);
829 /* get a handle on the hte */
830 hte = &cm_core->connected_nodes;
832 nes_debug(NES_DBG_CM, "Searching for an owner node:%x:%x from core %p->%p\n",
833 loc_addr, loc_port, cm_core, hte);
835 /* walk list and find cm_node associated with this session ID */
836 spin_lock_irqsave(&cm_core->ht_lock, flags);
837 list_for_each(list_pos, hte) {
838 cm_node = container_of(list_pos, struct nes_cm_node, list);
839 /* compare quad, return node handle if a match */
840 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
841 cm_node->loc_addr, cm_node->loc_port,
843 cm_node->rem_addr, cm_node->rem_port,
845 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
846 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
847 add_ref_cm_node(cm_node);
848 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
852 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
860 * find_listener - find a cm node listening on this addr-port pair
862 static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
863 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
866 struct list_head *listen_list;
867 struct nes_cm_listener *listen_node;
869 /* walk list and find cm_node associated with this session ID */
870 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
871 list_for_each(listen_list, &cm_core->listen_list.list) {
872 listen_node = container_of(listen_list, struct nes_cm_listener, list);
873 /* compare node pair, return node handle if a match */
874 if (((listen_node->loc_addr == dst_addr) ||
875 listen_node->loc_addr == 0x00000000) &&
876 (listen_node->loc_port == dst_port) &&
877 (listener_state & listen_node->listener_state)) {
878 atomic_inc(&listen_node->ref_count);
879 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
883 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
885 nes_debug(NES_DBG_CM, "Unable to find listener- %x:%x\n",
894 * add_hte_node - add a cm node to the hash table
896 static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
900 struct list_head *hte;
902 if (!cm_node || !cm_core)
905 nes_debug(NES_DBG_CM, "Adding Node to Active Connection HT\n");
907 /* first, make an index into our hash table */
908 hashkey = make_hashkey(cm_node->loc_port, cm_node->loc_addr,
909 cm_node->rem_port, cm_node->rem_addr);
910 cm_node->hashkey = hashkey;
912 spin_lock_irqsave(&cm_core->ht_lock, flags);
914 /* get a handle on the hash table element (list head for this slot) */
915 hte = &cm_core->connected_nodes;
916 list_add_tail(&cm_node->list, hte);
917 atomic_inc(&cm_core->ht_node_cnt);
919 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
926 * mini_cm_dec_refcnt_listen
928 static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
929 struct nes_cm_listener *listener, int free_hanging_nodes)
933 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
934 if (!atomic_dec_return(&listener->ref_count)) {
935 list_del(&listener->list);
937 /* decrement our listen node count */
938 atomic_dec(&cm_core->listen_node_cnt);
940 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
942 if (listener->nesvnic) {
943 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
944 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
947 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
952 cm_listens_destroyed++;
954 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
957 if (atomic_read(&listener->pend_accepts_cnt) > 0)
958 nes_debug(NES_DBG_CM, "destroying listener (%p)"
959 " with non-zero pending accepts=%u\n",
960 listener, atomic_read(&listener->pend_accepts_cnt));
970 static int mini_cm_del_listen(struct nes_cm_core *cm_core,
971 struct nes_cm_listener *listener)
973 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
974 listener->cm_id = NULL; /* going to be destroyed pretty soon */
975 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
980 * mini_cm_accelerated
982 static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
983 struct nes_cm_node *cm_node)
986 cm_node->accelerated = 1;
988 if (cm_node->accept_pend) {
989 BUG_ON(!cm_node->listener);
990 atomic_dec(&cm_node->listener->pend_accepts_cnt);
991 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
994 was_timer_set = timer_pending(&cm_core->tcp_timer);
995 if (!was_timer_set) {
996 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
997 add_timer(&cm_core->tcp_timer);
1007 static void nes_addr_send_arp(u32 dst_ip)
1012 memset(&fl, 0, sizeof fl);
1013 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1014 if (ip_route_output_key(&init_net, &rt, &fl)) {
1015 printk("%s: ip_route_output_key failed for 0x%08X\n",
1016 __FUNCTION__, dst_ip);
1020 neigh_event_send(rt->u.dst.neighbour, NULL);
1026 * make_cm_node - create a new instance of a cm node
1028 static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1029 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1030 struct nes_cm_listener *listener)
1032 struct nes_cm_node *cm_node;
1035 struct nes_device *nesdev;
1036 struct nes_adapter *nesadapter;
1038 /* create an hte and cm_node for this instance */
1039 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1043 /* set our node specific transport info */
1044 cm_node->loc_addr = cm_info->loc_addr;
1045 cm_node->rem_addr = cm_info->rem_addr;
1046 cm_node->loc_port = cm_info->loc_port;
1047 cm_node->rem_port = cm_info->rem_port;
1048 cm_node->send_write0 = send_first;
1049 nes_debug(NES_DBG_CM, "Make node addresses : loc = %x:%x, rem = %x:%x\n",
1050 cm_node->loc_addr, cm_node->loc_port, cm_node->rem_addr, cm_node->rem_port);
1051 cm_node->listener = listener;
1052 cm_node->netdev = nesvnic->netdev;
1053 cm_node->cm_id = cm_info->cm_id;
1054 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1056 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n",
1057 cm_node->listener, cm_node->cm_id);
1059 INIT_LIST_HEAD(&cm_node->retrans_list);
1060 spin_lock_init(&cm_node->retrans_list_lock);
1061 INIT_LIST_HEAD(&cm_node->recv_list);
1062 spin_lock_init(&cm_node->recv_list_lock);
1064 cm_node->loopbackpartner = NULL;
1065 atomic_set(&cm_node->ref_count, 1);
1066 /* associate our parent CM core */
1067 cm_node->cm_core = cm_core;
1068 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1069 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1070 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1071 NES_CM_DEFAULT_RCV_WND_SCALE;
1072 ts = current_kernel_time();
1073 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1074 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
1075 sizeof(struct tcphdr) - ETH_HLEN;
1076 cm_node->tcp_cntxt.rcv_nxt = 0;
1077 /* get a unique session ID , add thread_id to an upcounter to handle race */
1078 atomic_inc(&cm_core->node_cnt);
1079 atomic_inc(&cm_core->session_id);
1080 cm_node->session_id = (u32)(atomic_read(&cm_core->session_id) + current->tgid);
1081 cm_node->conn_type = cm_info->conn_type;
1082 cm_node->apbvt_set = 0;
1083 cm_node->accept_pend = 0;
1085 cm_node->nesvnic = nesvnic;
1086 /* get some device handles, for arp lookup */
1087 nesdev = nesvnic->nesdev;
1088 nesadapter = nesdev->nesadapter;
1090 cm_node->loopbackpartner = NULL;
1091 /* get the mac addr for the remote node */
1092 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1095 nes_addr_send_arp(cm_info->rem_addr);
1099 /* copy the mac addr to node context */
1100 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
1101 nes_debug(NES_DBG_CM, "Remote mac addr from arp table:%02x,"
1102 " %02x, %02x, %02x, %02x, %02x\n",
1103 cm_node->rem_mac[0], cm_node->rem_mac[1],
1104 cm_node->rem_mac[2], cm_node->rem_mac[3],
1105 cm_node->rem_mac[4], cm_node->rem_mac[5]);
1107 add_hte_node(cm_core, cm_node);
1108 atomic_inc(&cm_nodes_created);
1115 * add_ref_cm_node - destroy an instance of a cm node
1117 static int add_ref_cm_node(struct nes_cm_node *cm_node)
1119 atomic_inc(&cm_node->ref_count);
1125 * rem_ref_cm_node - destroy an instance of a cm node
1127 static int rem_ref_cm_node(struct nes_cm_core *cm_core,
1128 struct nes_cm_node *cm_node)
1130 unsigned long flags, qplockflags;
1131 struct nes_timer_entry *send_entry;
1132 struct nes_timer_entry *recv_entry;
1133 struct iw_cm_id *cm_id;
1134 struct list_head *list_core, *list_node_temp;
1135 struct nes_qp *nesqp;
1140 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1141 if (atomic_dec_return(&cm_node->ref_count)) {
1142 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1145 list_del(&cm_node->list);
1146 atomic_dec(&cm_core->ht_node_cnt);
1147 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1149 /* if the node is destroyed before connection was accelerated */
1150 if (!cm_node->accelerated && cm_node->accept_pend) {
1151 BUG_ON(!cm_node->listener);
1152 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1153 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1156 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1157 list_for_each_safe(list_core, list_node_temp, &cm_node->retrans_list) {
1158 send_entry = container_of(list_core, struct nes_timer_entry, list);
1159 list_del(&send_entry->list);
1160 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1161 dev_kfree_skb_any(send_entry->skb);
1163 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1166 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1168 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
1169 list_for_each_safe(list_core, list_node_temp, &cm_node->recv_list) {
1170 recv_entry = container_of(list_core, struct nes_timer_entry, list);
1171 list_del(&recv_entry->list);
1172 cm_id = cm_node->cm_id;
1173 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
1174 if (recv_entry->type == NES_TIMER_TYPE_CLOSE) {
1175 nesqp = (struct nes_qp *)recv_entry->skb;
1176 spin_lock_irqsave(&nesqp->lock, qplockflags);
1178 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p: ****** HIT A NES_TIMER_TYPE_CLOSE"
1179 " with something to do!!! ******\n",
1180 nesqp->hwqp.qp_id, cm_id);
1181 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
1182 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
1183 nesqp->ibqp_state = IB_QPS_ERR;
1184 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
1185 nes_cm_disconn(nesqp);
1187 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
1188 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p: ****** HIT A NES_TIMER_TYPE_CLOSE"
1189 " with nothing to do!!! ******\n",
1190 nesqp->hwqp.qp_id, cm_id);
1191 nes_rem_ref(&nesqp->ibqp);
1193 cm_id->rem_ref(cm_id);
1194 } else if (recv_entry->type == NES_TIMER_TYPE_RECV) {
1195 dev_kfree_skb_any(recv_entry->skb);
1198 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
1200 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
1202 if (cm_node->listener) {
1203 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1205 if (cm_node->apbvt_set && cm_node->nesvnic) {
1206 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
1207 PCI_FUNC(cm_node->nesvnic->nesdev->pcidev->devfn),
1208 NES_MANAGE_APBVT_DEL);
1213 atomic_dec(&cm_core->node_cnt);
1214 atomic_inc(&cm_nodes_destroyed);
1223 static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc, u32 optionsize, u32 syn_packet)
1227 union all_known_options *all_options;
1228 char got_mss_option = 0;
1230 while (offset < optionsize) {
1231 all_options = (union all_known_options *)(optionsloc + offset);
1232 switch (all_options->as_base.optionnum) {
1233 case OPTION_NUMBER_END:
1234 offset = optionsize;
1236 case OPTION_NUMBER_NONE:
1239 case OPTION_NUMBER_MSS:
1240 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d Size: %d\n",
1242 all_options->as_mss.length, offset, optionsize);
1244 if (all_options->as_mss.length != 4) {
1247 tmp = ntohs(all_options->as_mss.mss);
1248 if (tmp > 0 && tmp < cm_node->tcp_cntxt.mss)
1249 cm_node->tcp_cntxt.mss = tmp;
1252 case OPTION_NUMBER_WINDOW_SCALE:
1253 cm_node->tcp_cntxt.snd_wscale = all_options->as_windowscale.shiftcount;
1255 case OPTION_NUMBER_WRITE0:
1256 cm_node->send_write0 = 1;
1259 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1260 all_options->as_base.optionnum);
1263 offset += all_options->as_base.length;
1265 if ((!got_mss_option) && (syn_packet))
1266 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1274 int process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1275 struct nes_cm_core *cm_core)
1280 struct tcphdr *tcph = tcp_hdr(skb);
1282 if (cm_node->state == NES_CM_STATE_SYN_SENT && tcph->syn) {
1283 inc_sequence = ntohl(tcph->seq);
1284 cm_node->tcp_cntxt.rcv_nxt = inc_sequence;
1287 if ((!tcph) || (cm_node->state == NES_CM_STATE_TSA)) {
1289 atomic_inc(&cm_accel_dropped_pkts);
1294 atomic_inc(&cm_resets_recvd);
1295 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u. refcnt=%d\n",
1296 cm_node, cm_node->state, atomic_read(&cm_node->ref_count));
1297 switch (cm_node->state) {
1298 case NES_CM_STATE_LISTENING:
1299 rem_ref_cm_node(cm_core, cm_node);
1301 case NES_CM_STATE_TSA:
1302 case NES_CM_STATE_CLOSED:
1304 case NES_CM_STATE_SYN_RCVD:
1305 nes_debug(NES_DBG_CM, "Received a reset for local 0x%08X:%04X,"
1306 " remote 0x%08X:%04X, node state = %u\n",
1307 cm_node->loc_addr, cm_node->loc_port,
1308 cm_node->rem_addr, cm_node->rem_port,
1310 rem_ref_cm_node(cm_core, cm_node);
1312 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
1313 case NES_CM_STATE_ESTABLISHED:
1314 case NES_CM_STATE_MPAREQ_SENT:
1316 nes_debug(NES_DBG_CM, "Received a reset for local 0x%08X:%04X,"
1317 " remote 0x%08X:%04X, node state = %u refcnt=%d\n",
1318 cm_node->loc_addr, cm_node->loc_port,
1319 cm_node->rem_addr, cm_node->rem_port,
1320 cm_node->state, atomic_read(&cm_node->ref_count));
1322 cm_node->state = NES_CM_STATE_CLOSED;
1324 create_event(cm_node, NES_CM_EVENT_ABORTED);
1331 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1333 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1334 skb_pull(skb, tcph->doff << 2);
1336 datasize = skb->len;
1337 inc_sequence = ntohl(tcph->seq);
1338 nes_debug(NES_DBG_CM, "datasize = %u, sequence = 0x%08X, ack_seq = 0x%08X,"
1339 " rcv_nxt = 0x%08X Flags: %s %s.\n",
1340 datasize, inc_sequence, ntohl(tcph->ack_seq),
1341 cm_node->tcp_cntxt.rcv_nxt, (tcph->syn ? "SYN":""),
1342 (tcph->ack ? "ACK":""));
1344 if (!tcph->syn && (inc_sequence != cm_node->tcp_cntxt.rcv_nxt)
1346 nes_debug(NES_DBG_CM, "dropping packet, datasize = %u, sequence = 0x%08X,"
1347 " ack_seq = 0x%08X, rcv_nxt = 0x%08X Flags: %s.\n",
1348 datasize, inc_sequence, ntohl(tcph->ack_seq),
1349 cm_node->tcp_cntxt.rcv_nxt, (tcph->ack ? "ACK":""));
1350 if (cm_node->state == NES_CM_STATE_LISTENING) {
1351 rem_ref_cm_node(cm_core, cm_node);
1356 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1360 u8 *optionsloc = (u8 *)&tcph[1];
1361 if (process_options(cm_node, optionsloc, optionsize, (u32)tcph->syn)) {
1362 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n", __FUNCTION__, cm_node);
1363 send_reset(cm_node);
1364 if (cm_node->state != NES_CM_STATE_SYN_SENT)
1365 rem_ref_cm_node(cm_core, cm_node);
1368 } else if (tcph->syn)
1369 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1371 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1372 cm_node->tcp_cntxt.snd_wscale;
1374 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd) {
1375 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
1379 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1380 switch (cm_node->state) {
1381 case NES_CM_STATE_SYN_RCVD:
1382 case NES_CM_STATE_SYN_SENT:
1383 /* read and stash current sequence number */
1384 if (cm_node->tcp_cntxt.rem_ack_num != cm_node->tcp_cntxt.loc_seq_num) {
1385 nes_debug(NES_DBG_CM, "ERROR - cm_node->tcp_cntxt.rem_ack_num !="
1386 " cm_node->tcp_cntxt.loc_seq_num\n");
1387 send_reset(cm_node);
1390 if (cm_node->state == NES_CM_STATE_SYN_SENT)
1391 cm_node->state = NES_CM_STATE_ONE_SIDE_ESTABLISHED;
1393 cm_node->state = NES_CM_STATE_ESTABLISHED;
1396 case NES_CM_STATE_LAST_ACK:
1397 cm_node->state = NES_CM_STATE_CLOSED;
1399 case NES_CM_STATE_FIN_WAIT1:
1400 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1402 case NES_CM_STATE_CLOSING:
1403 cm_node->state = NES_CM_STATE_TIME_WAIT;
1404 /* need to schedule this to happen in 2MSL timeouts */
1405 cm_node->state = NES_CM_STATE_CLOSED;
1407 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
1408 case NES_CM_STATE_ESTABLISHED:
1409 case NES_CM_STATE_MPAREQ_SENT:
1410 case NES_CM_STATE_CLOSE_WAIT:
1411 case NES_CM_STATE_TIME_WAIT:
1412 case NES_CM_STATE_CLOSED:
1414 case NES_CM_STATE_LISTENING:
1415 nes_debug(NES_DBG_CM, "Received an ACK on a listening port (SYN %d)\n", tcph->syn);
1416 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1417 send_reset(cm_node);
1418 /* send_reset bumps refcount, this should have been a new node */
1419 rem_ref_cm_node(cm_core, cm_node);
1422 case NES_CM_STATE_TSA:
1423 nes_debug(NES_DBG_CM, "Received a packet with the ack bit set while in TSA state\n");
1425 case NES_CM_STATE_UNKNOWN:
1426 case NES_CM_STATE_INITED:
1427 case NES_CM_STATE_ACCEPTING:
1428 case NES_CM_STATE_FIN_WAIT2:
1430 nes_debug(NES_DBG_CM, "Received ack from unknown state: %x\n",
1432 send_reset(cm_node);
1438 if (cm_node->state == NES_CM_STATE_LISTENING) {
1439 /* do not exceed backlog */
1440 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1441 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1442 cm_node->listener->backlog) {
1443 nes_debug(NES_DBG_CM, "drop syn due to backlog pressure \n");
1445 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1446 rem_ref_cm_node(cm_core, cm_node);
1449 cm_node->accept_pend = 1;
1453 cm_node->tcp_cntxt.rcv_nxt ++;
1455 if (cm_node->state == NES_CM_STATE_LISTENING) {
1456 cm_node->state = NES_CM_STATE_SYN_RCVD;
1457 send_syn(cm_node, 1);
1459 if (cm_node->state == NES_CM_STATE_ONE_SIDE_ESTABLISHED) {
1460 cm_node->state = NES_CM_STATE_ESTABLISHED;
1461 /* send final handshake ACK */
1462 ret = send_ack(cm_node);
1466 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1467 ret = send_mpa_request(cm_node);
1474 cm_node->tcp_cntxt.rcv_nxt++;
1475 switch (cm_node->state) {
1476 case NES_CM_STATE_SYN_RCVD:
1477 case NES_CM_STATE_SYN_SENT:
1478 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
1479 case NES_CM_STATE_ESTABLISHED:
1480 case NES_CM_STATE_ACCEPTING:
1481 case NES_CM_STATE_MPAREQ_SENT:
1482 cm_node->state = NES_CM_STATE_CLOSE_WAIT;
1483 cm_node->state = NES_CM_STATE_LAST_ACK;
1484 ret = send_fin(cm_node, NULL);
1486 case NES_CM_STATE_FIN_WAIT1:
1487 cm_node->state = NES_CM_STATE_CLOSING;
1488 ret = send_ack(cm_node);
1490 case NES_CM_STATE_FIN_WAIT2:
1491 cm_node->state = NES_CM_STATE_TIME_WAIT;
1492 cm_node->tcp_cntxt.loc_seq_num ++;
1493 ret = send_ack(cm_node);
1494 /* need to schedule this to happen in 2MSL timeouts */
1495 cm_node->state = NES_CM_STATE_CLOSED;
1497 case NES_CM_STATE_CLOSE_WAIT:
1498 case NES_CM_STATE_LAST_ACK:
1499 case NES_CM_STATE_CLOSING:
1500 case NES_CM_STATE_TSA:
1502 nes_debug(NES_DBG_CM, "Received a fin while in %x state\n",
1510 u8 *dataloc = skb->data;
1511 /* figure out what state we are in and handle transition to next state */
1512 switch (cm_node->state) {
1513 case NES_CM_STATE_LISTENING:
1514 case NES_CM_STATE_SYN_RCVD:
1515 case NES_CM_STATE_SYN_SENT:
1516 case NES_CM_STATE_FIN_WAIT1:
1517 case NES_CM_STATE_FIN_WAIT2:
1518 case NES_CM_STATE_CLOSE_WAIT:
1519 case NES_CM_STATE_LAST_ACK:
1520 case NES_CM_STATE_CLOSING:
1522 case NES_CM_STATE_MPAREQ_SENT:
1523 /* recv the mpa res frame, ret=frame len (incl priv data) */
1524 ret = parse_mpa(cm_node, dataloc, datasize);
1527 /* set the req frame payload len in skb */
1528 /* we are done handling this state, set node to a TSA state */
1529 cm_node->state = NES_CM_STATE_TSA;
1531 create_event(cm_node, NES_CM_EVENT_CONNECTED);
1534 case NES_CM_STATE_ESTABLISHED:
1535 /* we are expecting an MPA req frame */
1536 ret = parse_mpa(cm_node, dataloc, datasize);
1540 cm_node->state = NES_CM_STATE_TSA;
1542 /* we got a valid MPA request, create an event */
1543 create_event(cm_node, NES_CM_EVENT_MPA_REQ);
1545 case NES_CM_STATE_TSA:
1546 handle_exception_pkt(cm_node, skb);
1548 case NES_CM_STATE_UNKNOWN:
1549 case NES_CM_STATE_INITED:
1560 * mini_cm_listen - create a listen node with params
1562 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
1563 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
1565 struct nes_cm_listener *listener;
1566 unsigned long flags;
1568 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1569 cm_info->loc_addr, cm_info->loc_port);
1571 /* cannot have multiple matching listeners */
1572 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1573 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1574 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1575 /* find automatically incs ref count ??? */
1576 atomic_dec(&listener->ref_count);
1577 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1582 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1583 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1585 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1589 memset(listener, 0, sizeof(struct nes_cm_listener));
1590 listener->loc_addr = htonl(cm_info->loc_addr);
1591 listener->loc_port = htons(cm_info->loc_port);
1592 listener->reused_node = 0;
1594 atomic_set(&listener->ref_count, 1);
1597 /* find already inc'ed the ref count */
1599 listener->reused_node = 1;
1602 listener->cm_id = cm_info->cm_id;
1603 atomic_set(&listener->pend_accepts_cnt, 0);
1604 listener->cm_core = cm_core;
1605 listener->nesvnic = nesvnic;
1606 atomic_inc(&cm_core->node_cnt);
1607 atomic_inc(&cm_core->session_id);
1609 listener->session_id = (u32)(atomic_read(&cm_core->session_id) + current->tgid);
1610 listener->conn_type = cm_info->conn_type;
1611 listener->backlog = cm_info->backlog;
1612 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1614 if (!listener->reused_node) {
1615 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1616 list_add(&listener->list, &cm_core->listen_list.list);
1617 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1618 atomic_inc(&cm_core->listen_node_cnt);
1621 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1622 " listener = %p, backlog = %d, cm_id = %p.\n",
1623 cm_info->loc_addr, cm_info->loc_port,
1624 listener, listener->backlog, listener->cm_id);
1631 * mini_cm_connect - make a connection node with params
1633 struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
1634 struct nes_vnic *nesvnic, struct ietf_mpa_frame *mpa_frame,
1635 struct nes_cm_info *cm_info)
1638 struct nes_cm_node *cm_node;
1639 struct nes_cm_listener *loopbackremotelistener;
1640 struct nes_cm_node *loopbackremotenode;
1641 struct nes_cm_info loopback_cm_info;
1643 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) +
1644 ntohs(mpa_frame->priv_data_len);
1646 cm_info->loc_addr = htonl(cm_info->loc_addr);
1647 cm_info->rem_addr = htonl(cm_info->rem_addr);
1648 cm_info->loc_port = htons(cm_info->loc_port);
1649 cm_info->rem_port = htons(cm_info->rem_port);
1651 /* create a CM connection node */
1652 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
1656 // set our node side to client (active) side
1657 cm_node->tcp_cntxt.client = 1;
1658 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1660 if (cm_info->loc_addr == cm_info->rem_addr) {
1661 loopbackremotelistener = find_listener(cm_core, cm_node->rem_addr,
1662 cm_node->rem_port, NES_CM_LISTENER_ACTIVE_STATE);
1663 if (loopbackremotelistener == NULL) {
1664 create_event(cm_node, NES_CM_EVENT_ABORTED);
1666 atomic_inc(&cm_loopbacks);
1667 loopback_cm_info = *cm_info;
1668 loopback_cm_info.loc_port = cm_info->rem_port;
1669 loopback_cm_info.rem_port = cm_info->loc_port;
1670 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
1671 loopbackremotenode = make_cm_node(cm_core, nesvnic, &loopback_cm_info,
1672 loopbackremotelistener);
1673 loopbackremotenode->loopbackpartner = cm_node;
1674 loopbackremotenode->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1675 cm_node->loopbackpartner = loopbackremotenode;
1676 memcpy(loopbackremotenode->mpa_frame_buf, &mpa_frame->priv_data,
1678 loopbackremotenode->mpa_frame_size = mpa_frame_size -
1679 sizeof(struct ietf_mpa_frame);
1681 // we are done handling this state, set node to a TSA state
1682 cm_node->state = NES_CM_STATE_TSA;
1683 cm_node->tcp_cntxt.rcv_nxt = loopbackremotenode->tcp_cntxt.loc_seq_num;
1684 loopbackremotenode->tcp_cntxt.rcv_nxt = cm_node->tcp_cntxt.loc_seq_num;
1685 cm_node->tcp_cntxt.max_snd_wnd = loopbackremotenode->tcp_cntxt.rcv_wnd;
1686 loopbackremotenode->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.rcv_wnd;
1687 cm_node->tcp_cntxt.snd_wnd = loopbackremotenode->tcp_cntxt.rcv_wnd;
1688 loopbackremotenode->tcp_cntxt.snd_wnd = cm_node->tcp_cntxt.rcv_wnd;
1689 cm_node->tcp_cntxt.snd_wscale = loopbackremotenode->tcp_cntxt.rcv_wscale;
1690 loopbackremotenode->tcp_cntxt.snd_wscale = cm_node->tcp_cntxt.rcv_wscale;
1692 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
1697 /* set our node side to client (active) side */
1698 cm_node->tcp_cntxt.client = 1;
1699 /* init our MPA frame ptr */
1700 memcpy(&cm_node->mpa_frame, mpa_frame, mpa_frame_size);
1701 cm_node->mpa_frame_size = mpa_frame_size;
1703 /* send a syn and goto syn sent state */
1704 cm_node->state = NES_CM_STATE_SYN_SENT;
1705 ret = send_syn(cm_node, 0);
1707 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X, port=0x%04x,"
1708 " cm_node=%p, cm_id = %p.\n",
1709 cm_node->rem_addr, cm_node->rem_port, cm_node, cm_node->cm_id);
1716 * mini_cm_accept - accept a connection
1717 * This function is never called
1719 int mini_cm_accept(struct nes_cm_core *cm_core, struct ietf_mpa_frame *mpa_frame,
1720 struct nes_cm_node *cm_node)
1727 * mini_cm_reject - reject and teardown a connection
1729 int mini_cm_reject(struct nes_cm_core *cm_core,
1730 struct ietf_mpa_frame *mpa_frame,
1731 struct nes_cm_node *cm_node)
1734 struct sk_buff *skb;
1735 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) +
1736 ntohs(mpa_frame->priv_data_len);
1738 skb = get_free_pkt(cm_node);
1740 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1744 /* send an MPA Request frame */
1745 form_cm_frame(skb, cm_node, NULL, 0, mpa_frame, mpa_frame_size, SET_ACK | SET_FIN);
1746 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1748 cm_node->state = NES_CM_STATE_CLOSED;
1749 ret = send_fin(cm_node, NULL);
1752 printk(KERN_INFO PFX "failed to send MPA Reply (reject)\n");
1763 int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1767 if (!cm_core || !cm_node)
1770 switch (cm_node->state) {
1771 /* if passed in node is null, create a reference key node for node search */
1772 /* check if we found an owner node for this pkt */
1773 case NES_CM_STATE_SYN_RCVD:
1774 case NES_CM_STATE_SYN_SENT:
1775 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
1776 case NES_CM_STATE_ESTABLISHED:
1777 case NES_CM_STATE_ACCEPTING:
1778 case NES_CM_STATE_MPAREQ_SENT:
1779 cm_node->state = NES_CM_STATE_FIN_WAIT1;
1780 send_fin(cm_node, NULL);
1782 case NES_CM_STATE_CLOSE_WAIT:
1783 cm_node->state = NES_CM_STATE_LAST_ACK;
1784 send_fin(cm_node, NULL);
1786 case NES_CM_STATE_FIN_WAIT1:
1787 case NES_CM_STATE_FIN_WAIT2:
1788 case NES_CM_STATE_LAST_ACK:
1789 case NES_CM_STATE_TIME_WAIT:
1790 case NES_CM_STATE_CLOSING:
1793 case NES_CM_STATE_LISTENING:
1794 case NES_CM_STATE_UNKNOWN:
1795 case NES_CM_STATE_INITED:
1796 case NES_CM_STATE_CLOSED:
1797 case NES_CM_STATE_TSA:
1798 ret = rem_ref_cm_node(cm_core, cm_node);
1801 cm_node->cm_id = NULL;
1807 * recv_pkt - recv an ETHERNET packet, and process it through CM
1808 * node state machine
1810 int mini_cm_recv_pkt(struct nes_cm_core *cm_core, struct nes_vnic *nesvnic,
1811 struct sk_buff *skb)
1813 struct nes_cm_node *cm_node = NULL;
1814 struct nes_cm_listener *listener = NULL;
1816 struct tcphdr *tcph;
1817 struct nes_cm_info nfo;
1820 if (!skb || skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
1825 iph = (struct iphdr *)skb->data;
1826 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
1827 skb_reset_network_header(skb);
1828 skb_set_transport_header(skb, sizeof(*tcph));
1829 skb->len = ntohs(iph->tot_len);
1831 nfo.loc_addr = ntohl(iph->daddr);
1832 nfo.loc_port = ntohs(tcph->dest);
1833 nfo.rem_addr = ntohl(iph->saddr);
1834 nfo.rem_port = ntohs(tcph->source);
1836 nes_debug(NES_DBG_CM, "Received packet: dest=0x%08X:0x%04X src=0x%08X:0x%04X\n",
1837 iph->daddr, tcph->dest, iph->saddr, tcph->source);
1839 /* note: this call is going to increment cm_node ref count */
1840 cm_node = find_node(cm_core,
1841 nfo.rem_port, nfo.rem_addr,
1842 nfo.loc_port, nfo.loc_addr);
1845 listener = find_listener(cm_core, nfo.loc_addr, nfo.loc_port,
1846 NES_CM_LISTENER_ACTIVE_STATE);
1848 nfo.cm_id = listener->cm_id;
1849 nfo.conn_type = listener->conn_type;
1855 cm_node = make_cm_node(cm_core, nesvnic, &nfo, listener);
1857 nes_debug(NES_DBG_CM, "Unable to allocate node\n");
1859 nes_debug(NES_DBG_CM, "unable to allocate node and decrementing listener refcount\n");
1860 atomic_dec(&listener->ref_count);
1866 nes_debug(NES_DBG_CM, "Packet found for unknown port %x refcnt=%d\n",
1867 nfo.loc_port, atomic_read(&cm_node->ref_count));
1869 nes_debug(NES_DBG_CM, "Packet found for unknown port=%d"
1870 " rem_port=%d refcnt=%d\n",
1871 nfo.loc_port, nfo.rem_port, atomic_read(&cm_node->ref_count));
1873 cm_node->tcp_cntxt.rcv_nxt = ntohl(tcph->seq);
1874 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1875 send_reset(cm_node);
1877 rem_ref_cm_node(cm_core, cm_node);
1881 add_ref_cm_node(cm_node);
1882 cm_node->state = NES_CM_STATE_LISTENING;
1885 nes_debug(NES_DBG_CM, "Processing Packet for node %p, data = (%p):\n",
1886 cm_node, skb->data);
1887 process_packet(cm_node, skb, cm_core);
1889 rem_ref_cm_node(cm_core, cm_node);
1892 dev_kfree_skb_any(skb);
1898 * nes_cm_alloc_core - allocate a top level instance of a cm core
1900 struct nes_cm_core *nes_cm_alloc_core(void)
1904 struct nes_cm_core *cm_core;
1905 struct sk_buff *skb = NULL;
1907 /* setup the CM core */
1908 /* alloc top level core control structure */
1909 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
1913 INIT_LIST_HEAD(&cm_core->connected_nodes);
1914 init_timer(&cm_core->tcp_timer);
1915 cm_core->tcp_timer.function = nes_cm_timer_tick;
1917 cm_core->mtu = NES_CM_DEFAULT_MTU;
1918 cm_core->state = NES_CM_STATE_INITED;
1919 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
1921 atomic_set(&cm_core->session_id, 0);
1922 atomic_set(&cm_core->events_posted, 0);
1924 /* init the packet lists */
1925 skb_queue_head_init(&cm_core->tx_free_list);
1927 for (i = 0; i < NES_CM_DEFAULT_FRAME_CNT; i++) {
1928 skb = dev_alloc_skb(cm_core->mtu);
1933 /* add 'raw' skb to free frame list */
1934 skb_queue_head(&cm_core->tx_free_list, skb);
1937 cm_core->api = &nes_cm_api;
1939 spin_lock_init(&cm_core->ht_lock);
1940 spin_lock_init(&cm_core->listen_list_lock);
1942 INIT_LIST_HEAD(&cm_core->listen_list.list);
1944 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
1946 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
1947 cm_core->event_wq = create_singlethread_workqueue("nesewq");
1948 cm_core->post_event = nes_cm_post_event;
1949 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
1950 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
1952 print_core(cm_core);
1958 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
1960 int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
1962 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
1969 if (timer_pending(&cm_core->tcp_timer)) {
1970 del_timer(&cm_core->tcp_timer);
1973 destroy_workqueue(cm_core->event_wq);
1974 destroy_workqueue(cm_core->disconn_wq);
1975 nes_debug(NES_DBG_CM, "\n");
1985 int mini_cm_get(struct nes_cm_core *cm_core)
1987 return cm_core->state;
1994 int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
1999 case NES_CM_SET_PKT_SIZE:
2000 cm_core->mtu = value;
2002 case NES_CM_SET_FREE_PKT_Q_SIZE:
2003 cm_core->free_tx_pkt_max = value;
2006 /* unknown set option */
2015 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2016 * successfully exchanged when this is called
2018 static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2025 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2026 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2027 NES_QPCONTEXT_MISC_DROS);
2029 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2030 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2032 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2034 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2036 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2037 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2039 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2040 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2041 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2043 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2044 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2045 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2047 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2048 nesqp->nesqp_context->ts_recent = 0;
2049 nesqp->nesqp_context->ts_age = 0;
2050 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2051 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2052 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2053 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2054 cm_node->tcp_cntxt.rcv_wscale);
2055 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2056 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2057 nesqp->nesqp_context->srtt = 0;
2058 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2059 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2060 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2061 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2062 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2063 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2065 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2066 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2067 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2068 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2069 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2070 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2071 le32_to_cpu(nesqp->nesqp_context->misc));
2072 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2073 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2074 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2076 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2077 cm_node->state = NES_CM_STATE_TSA;
2086 int nes_cm_disconn(struct nes_qp *nesqp)
2088 unsigned long flags;
2090 spin_lock_irqsave(&nesqp->lock, flags);
2091 if (nesqp->disconn_pending == 0) {
2092 nesqp->disconn_pending++;
2093 spin_unlock_irqrestore(&nesqp->lock, flags);
2094 /* nes_add_ref(&nesqp->ibqp); */
2095 /* init our disconnect work element, to */
2096 INIT_WORK(&nesqp->disconn_work, nes_disconnect_worker);
2098 queue_work(g_cm_core->disconn_wq, &nesqp->disconn_work);
2100 spin_unlock_irqrestore(&nesqp->lock, flags);
2101 nes_rem_ref(&nesqp->ibqp);
2109 * nes_disconnect_worker
2111 void nes_disconnect_worker(struct work_struct *work)
2113 struct nes_qp *nesqp = container_of(work, struct nes_qp, disconn_work);
2115 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2116 nesqp->last_aeq, nesqp->hwqp.qp_id);
2117 nes_cm_disconn_true(nesqp);
2122 * nes_cm_disconn_true
2124 int nes_cm_disconn_true(struct nes_qp *nesqp)
2126 unsigned long flags;
2128 struct iw_cm_id *cm_id;
2129 struct iw_cm_event cm_event;
2130 struct nes_vnic *nesvnic;
2132 u8 original_hw_tcp_state;
2133 u8 original_ibqp_state;
2134 u8 issued_disconnect_reset = 0;
2137 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2141 spin_lock_irqsave(&nesqp->lock, flags);
2142 cm_id = nesqp->cm_id;
2143 /* make sure we havent already closed this connection */
2145 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2147 spin_unlock_irqrestore(&nesqp->lock, flags);
2148 nes_rem_ref(&nesqp->ibqp);
2152 nesvnic = to_nesvnic(nesqp->ibqp.device);
2153 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2155 original_hw_tcp_state = nesqp->hw_tcp_state;
2156 original_ibqp_state = nesqp->ibqp_state;
2157 last_ae = nesqp->last_aeq;
2160 nes_debug(NES_DBG_CM, "set ibqp_state=%u\n", nesqp->ibqp_state);
2162 if ((nesqp->cm_id) && (cm_id->event_handler)) {
2163 if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2164 ((original_ibqp_state == IB_QPS_RTS) &&
2165 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2166 atomic_inc(&cm_disconnects);
2167 cm_event.event = IW_CM_EVENT_DISCONNECT;
2168 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET) {
2169 issued_disconnect_reset = 1;
2170 cm_event.status = IW_CM_EVENT_STATUS_RESET;
2171 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event (status reset) for "
2172 " QP%u, cm_id = %p. \n",
2173 nesqp->hwqp.qp_id, cm_id);
2175 cm_event.status = IW_CM_EVENT_STATUS_OK;
2178 cm_event.local_addr = cm_id->local_addr;
2179 cm_event.remote_addr = cm_id->remote_addr;
2180 cm_event.private_data = NULL;
2181 cm_event.private_data_len = 0;
2183 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event for "
2184 " QP%u, SQ Head = %u, SQ Tail = %u. cm_id = %p, refcount = %u.\n",
2186 nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail, cm_id,
2187 atomic_read(&nesqp->refcount));
2189 spin_unlock_irqrestore(&nesqp->lock, flags);
2190 ret = cm_id->event_handler(cm_id, &cm_event);
2192 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2193 spin_lock_irqsave(&nesqp->lock, flags);
2196 nesqp->disconn_pending = 0;
2197 /* There might have been another AE while the lock was released */
2198 original_hw_tcp_state = nesqp->hw_tcp_state;
2199 original_ibqp_state = nesqp->ibqp_state;
2200 last_ae = nesqp->last_aeq;
2202 if ((issued_disconnect_reset == 0) && (nesqp->cm_id) &&
2203 ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2204 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2205 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2206 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2207 atomic_inc(&cm_closes);
2208 nesqp->cm_id = NULL;
2209 nesqp->in_disconnect = 0;
2210 spin_unlock_irqrestore(&nesqp->lock, flags);
2211 nes_disconnect(nesqp, 1);
2213 cm_id->provider_data = nesqp;
2214 /* Send up the close complete event */
2215 cm_event.event = IW_CM_EVENT_CLOSE;
2216 cm_event.status = IW_CM_EVENT_STATUS_OK;
2217 cm_event.provider_data = cm_id->provider_data;
2218 cm_event.local_addr = cm_id->local_addr;
2219 cm_event.remote_addr = cm_id->remote_addr;
2220 cm_event.private_data = NULL;
2221 cm_event.private_data_len = 0;
2223 ret = cm_id->event_handler(cm_id, &cm_event);
2225 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2228 cm_id->rem_ref(cm_id);
2230 spin_lock_irqsave(&nesqp->lock, flags);
2231 if (nesqp->flush_issued == 0) {
2232 nesqp->flush_issued = 1;
2233 spin_unlock_irqrestore(&nesqp->lock, flags);
2234 flush_wqes(nesvnic->nesdev, nesqp, NES_CQP_FLUSH_RQ, 1);
2236 spin_unlock_irqrestore(&nesqp->lock, flags);
2239 /* This reference is from either ModifyQP or the AE processing,
2240 there is still a race here with modifyqp */
2241 nes_rem_ref(&nesqp->ibqp);
2244 cm_id = nesqp->cm_id;
2245 spin_unlock_irqrestore(&nesqp->lock, flags);
2246 /* check to see if the inbound reset beat the outbound reset */
2247 if ((!cm_id) && (last_ae==NES_AEQE_AEID_RESET_SENT)) {
2248 nes_debug(NES_DBG_CM, "QP%u: Decing refcount due to inbound reset"
2249 " beating the outbound reset.\n",
2251 nes_rem_ref(&nesqp->ibqp);
2255 nesqp->disconn_pending = 0;
2256 spin_unlock_irqrestore(&nesqp->lock, flags);
2258 nes_rem_ref(&nesqp->ibqp);
2267 int nes_disconnect(struct nes_qp *nesqp, int abrupt)
2270 struct nes_vnic *nesvnic;
2271 struct nes_device *nesdev;
2273 nesvnic = to_nesvnic(nesqp->ibqp.device);
2277 nesdev = nesvnic->nesdev;
2279 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2280 atomic_read(&nesvnic->netdev->refcnt));
2282 if (nesqp->active_conn) {
2284 /* indicate this connection is NOT active */
2285 nesqp->active_conn = 0;
2287 /* Need to free the Last Streaming Mode Message */
2288 if (nesqp->ietf_frame) {
2289 pci_free_consistent(nesdev->pcidev,
2290 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2291 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2295 /* close the CM node down if it is still active */
2296 if (nesqp->cm_node) {
2297 nes_debug(NES_DBG_CM, "Call close API\n");
2299 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
2300 nesqp->cm_node = NULL;
2310 int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2314 struct nes_qp *nesqp;
2315 struct nes_vnic *nesvnic;
2316 struct nes_device *nesdev;
2317 struct nes_cm_node *cm_node;
2318 struct nes_adapter *adapter;
2319 struct ib_qp_attr attr;
2320 struct iw_cm_event cm_event;
2321 struct nes_hw_qp_wqe *wqe;
2322 struct nes_v4_quad nes_quad;
2325 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2329 /* get all our handles */
2330 nesqp = to_nesqp(ibqp);
2331 nesvnic = to_nesvnic(nesqp->ibqp.device);
2332 nesdev = nesvnic->nesdev;
2333 adapter = nesdev->nesadapter;
2335 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
2336 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
2338 /* since this is from a listen, we were able to put node handle into cm_id */
2339 cm_node = (struct nes_cm_node *)cm_id->provider_data;
2341 /* associate the node with the QP */
2342 nesqp->cm_node = (void *)cm_node;
2344 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu\n",
2345 nesqp->hwqp.qp_id, cm_node, jiffies);
2346 atomic_inc(&cm_accepts);
2348 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2349 atomic_read(&nesvnic->netdev->refcnt));
2351 /* allocate the ietf frame and space for private data */
2352 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2353 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2354 &nesqp->ietf_frame_pbase);
2356 if (!nesqp->ietf_frame) {
2357 nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
2362 /* setup the MPA frame */
2363 nesqp->private_data_len = conn_param->private_data_len;
2364 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
2366 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2367 conn_param->private_data_len);
2369 nesqp->ietf_frame->priv_data_len = cpu_to_be16(conn_param->private_data_len);
2370 nesqp->ietf_frame->rev = mpa_version;
2371 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
2373 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2374 wqe = &nesqp->hwqp.sq_vbase[0];
2376 if (cm_id->remote_addr.sin_addr.s_addr != cm_id->local_addr.sin_addr.s_addr) {
2377 u64temp = (unsigned long)nesqp;
2378 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2379 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2381 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2382 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING | NES_IWARP_SQ_WQE_WRPDU);
2383 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2384 cpu_to_le32(conn_param->private_data_len + sizeof(struct ietf_mpa_frame));
2385 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] =
2386 cpu_to_le32((u32)nesqp->ietf_frame_pbase);
2387 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] =
2388 cpu_to_le32((u32)((u64)nesqp->ietf_frame_pbase >> 32));
2389 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2390 cpu_to_le32(conn_param->private_data_len + sizeof(struct ietf_mpa_frame));
2391 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
2393 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
2394 NES_QPCONTEXT_ORDIRD_LSMM_PRESENT | NES_QPCONTEXT_ORDIRD_WRPDU);
2396 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2397 NES_QPCONTEXT_ORDIRD_WRPDU | NES_QPCONTEXT_ORDIRD_ALSMM));
2399 nesqp->skip_lsmm = 1;
2402 /* Cache the cm_id in the qp */
2403 nesqp->cm_id = cm_id;
2404 cm_node->cm_id = cm_id;
2406 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2407 cm_id->provider_data = nesqp;
2408 nesqp->active_conn = 0;
2410 nes_cm_init_tsa_conn(nesqp, cm_node);
2412 nesqp->nesqp_context->tcpPorts[0] = cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2413 nesqp->nesqp_context->tcpPorts[1] = cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2414 nesqp->nesqp_context->ip0 = cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
2416 nesqp->nesqp_context->misc2 |= cpu_to_le32(
2417 (u32)PCI_FUNC(nesdev->pcidev->devfn) << NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
2419 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
2420 nes_arp_table(nesdev, le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
2421 NES_ARP_RESOLVE) << 16);
2423 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
2424 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
2426 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2428 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
2429 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2430 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
2432 memset(&nes_quad, 0, sizeof(nes_quad));
2433 nes_quad.DstIpAdrIndex = cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2434 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2435 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2436 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
2438 /* Produce hash key */
2439 nesqp->hte_index = cpu_to_be32(
2440 crc32c(~0, (void *)&nes_quad, sizeof(nes_quad)) ^ 0xffffffff);
2441 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
2442 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
2444 nesqp->hte_index &= adapter->hte_index_mask;
2445 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2447 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2449 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = 0x%08X:0x%04X,"
2450 " rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + private data length=%zu.\n",
2452 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2453 ntohs(cm_id->remote_addr.sin_port),
2454 ntohl(cm_id->local_addr.sin_addr.s_addr),
2455 ntohs(cm_id->local_addr.sin_port),
2456 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2457 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2458 conn_param->private_data_len+sizeof(struct ietf_mpa_frame));
2460 attr.qp_state = IB_QPS_RTS;
2461 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
2463 /* notify OF layer that accept event was successfull */
2464 cm_id->add_ref(cm_id);
2466 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2467 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2468 cm_event.provider_data = (void *)nesqp;
2469 cm_event.local_addr = cm_id->local_addr;
2470 cm_event.remote_addr = cm_id->remote_addr;
2471 cm_event.private_data = NULL;
2472 cm_event.private_data_len = 0;
2473 ret = cm_id->event_handler(cm_id, &cm_event);
2474 if (cm_node->loopbackpartner) {
2475 cm_node->loopbackpartner->mpa_frame_size = nesqp->private_data_len;
2476 /* copy entire MPA frame to our cm_node's frame */
2477 memcpy(cm_node->loopbackpartner->mpa_frame_buf, nesqp->ietf_frame->priv_data,
2478 nesqp->private_data_len);
2479 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2482 printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
2483 __FUNCTION__, __LINE__, ret);
2492 int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2494 struct nes_cm_node *cm_node;
2495 struct nes_cm_core *cm_core;
2497 atomic_inc(&cm_rejects);
2498 cm_node = (struct nes_cm_node *) cm_id->provider_data;
2499 cm_core = cm_node->cm_core;
2500 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2502 strcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP);
2503 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2505 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2506 cm_node->mpa_frame.rev = mpa_version;
2507 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2509 cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
2517 * setup and launch cm connect node
2519 int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2522 struct nes_qp *nesqp;
2523 struct nes_vnic *nesvnic;
2524 struct nes_device *nesdev;
2525 struct nes_cm_node *cm_node;
2526 struct nes_cm_info cm_info;
2528 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2531 nesqp = to_nesqp(ibqp);
2534 nesvnic = to_nesvnic(nesqp->ibqp.device);
2537 nesdev = nesvnic->nesdev;
2541 atomic_inc(&cm_connects);
2543 nesqp->ietf_frame = kzalloc(sizeof(struct ietf_mpa_frame) +
2544 conn_param->private_data_len, GFP_KERNEL);
2545 if (!nesqp->ietf_frame)
2548 /* set qp as having an active connection */
2549 nesqp->active_conn = 1;
2551 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = 0x%08X:0x%04X.\n",
2553 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2554 ntohs(cm_id->remote_addr.sin_port),
2555 ntohl(cm_id->local_addr.sin_addr.s_addr),
2556 ntohs(cm_id->local_addr.sin_port));
2558 /* cache the cm_id in the qp */
2559 nesqp->cm_id = cm_id;
2561 cm_id->provider_data = nesqp;
2563 /* copy the private data */
2564 if (conn_param->private_data_len) {
2565 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2566 conn_param->private_data_len);
2569 nesqp->private_data_len = conn_param->private_data_len;
2570 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
2571 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
2572 nes_debug(NES_DBG_CM, "mpa private data len =%u\n", conn_param->private_data_len);
2574 strcpy(&nesqp->ietf_frame->key[0], IEFT_MPA_KEY_REQ);
2575 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
2576 nesqp->ietf_frame->rev = IETF_MPA_VERSION;
2577 nesqp->ietf_frame->priv_data_len = htons(conn_param->private_data_len);
2579 if (cm_id->local_addr.sin_addr.s_addr != cm_id->remote_addr.sin_addr.s_addr)
2580 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
2581 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
2583 /* set up the connection params for the node */
2584 cm_info.loc_addr = (cm_id->local_addr.sin_addr.s_addr);
2585 cm_info.loc_port = (cm_id->local_addr.sin_port);
2586 cm_info.rem_addr = (cm_id->remote_addr.sin_addr.s_addr);
2587 cm_info.rem_port = (cm_id->remote_addr.sin_port);
2588 cm_info.cm_id = cm_id;
2589 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
2591 cm_id->add_ref(cm_id);
2592 nes_add_ref(&nesqp->ibqp);
2594 /* create a connect CM node connection */
2595 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic, nesqp->ietf_frame, &cm_info);
2597 if (cm_id->local_addr.sin_addr.s_addr != cm_id->remote_addr.sin_addr.s_addr)
2598 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
2599 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
2600 nes_rem_ref(&nesqp->ibqp);
2601 kfree(nesqp->ietf_frame);
2602 nesqp->ietf_frame = NULL;
2603 cm_id->rem_ref(cm_id);
2607 cm_node->apbvt_set = 1;
2608 nesqp->cm_node = cm_node;
2617 int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
2619 struct nes_vnic *nesvnic;
2620 struct nes_cm_listener *cm_node;
2621 struct nes_cm_info cm_info;
2622 struct nes_adapter *adapter;
2626 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
2627 cm_id, ntohs(cm_id->local_addr.sin_port));
2629 nesvnic = to_nesvnic(cm_id->device);
2632 adapter = nesvnic->nesdev->nesadapter;
2633 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
2634 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
2636 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
2637 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
2639 /* setup listen params in our api call struct */
2640 cm_info.loc_addr = nesvnic->local_ipaddr;
2641 cm_info.loc_port = cm_id->local_addr.sin_port;
2642 cm_info.backlog = backlog;
2643 cm_info.cm_id = cm_id;
2645 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
2648 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
2650 printk("%s[%u] Error returned from listen API call\n",
2651 __FUNCTION__, __LINE__);
2655 cm_id->provider_data = cm_node;
2657 if (!cm_node->reused_node) {
2658 err = nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
2659 PCI_FUNC(nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
2661 printk("nes_manage_apbvt call returned %d.\n", err);
2662 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
2665 cm_listens_created++;
2668 cm_id->add_ref(cm_id);
2669 cm_id->provider_data = (void *)cm_node;
2677 * nes_destroy_listen
2679 int nes_destroy_listen(struct iw_cm_id *cm_id)
2681 if (cm_id->provider_data)
2682 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
2684 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
2686 cm_id->rem_ref(cm_id);
2695 int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
2697 cm_packets_received++;
2698 if ((g_cm_core) && (g_cm_core->api)) {
2699 g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
2701 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
2702 " cm is not setup properly.\n");
2711 * Start and init a cm core module
2713 int nes_cm_start(void)
2715 nes_debug(NES_DBG_CM, "\n");
2716 /* create the primary CM core, pass this handle to subsequent core inits */
2717 g_cm_core = nes_cm_alloc_core();
2728 * stop and dealloc all cm core instances
2730 int nes_cm_stop(void)
2732 g_cm_core->api->destroy_cm_core(g_cm_core);
2738 * cm_event_connected
2739 * handle a connected event, setup QPs and HW
2741 void cm_event_connected(struct nes_cm_event *event)
2744 struct nes_qp *nesqp;
2745 struct nes_vnic *nesvnic;
2746 struct nes_device *nesdev;
2747 struct nes_cm_node *cm_node;
2748 struct nes_adapter *nesadapter;
2749 struct ib_qp_attr attr;
2750 struct iw_cm_id *cm_id;
2751 struct iw_cm_event cm_event;
2752 struct nes_hw_qp_wqe *wqe;
2753 struct nes_v4_quad nes_quad;
2756 /* get all our handles */
2757 cm_node = event->cm_node;
2758 cm_id = cm_node->cm_id;
2759 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
2760 nesqp = (struct nes_qp *)cm_id->provider_data;
2761 nesvnic = to_nesvnic(nesqp->ibqp.device);
2762 nesdev = nesvnic->nesdev;
2763 nesadapter = nesdev->nesadapter;
2765 if (nesqp->destroyed) {
2768 atomic_inc(&cm_connecteds);
2769 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
2770 " local port 0x%04X. jiffies = %lu.\n",
2772 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2773 ntohs(cm_id->remote_addr.sin_port),
2774 ntohs(cm_id->local_addr.sin_port),
2777 nes_cm_init_tsa_conn(nesqp, cm_node);
2779 /* set the QP tsa context */
2780 nesqp->nesqp_context->tcpPorts[0] = cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2781 nesqp->nesqp_context->tcpPorts[1] = cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2782 nesqp->nesqp_context->ip0 = cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
2784 nesqp->nesqp_context->misc2 |= cpu_to_le32(
2785 (u32)PCI_FUNC(nesdev->pcidev->devfn) << NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
2786 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
2787 nes_arp_table(nesdev, le32_to_cpu(nesqp->nesqp_context->ip0),
2788 NULL, NES_ARP_RESOLVE) << 16);
2789 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
2790 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
2791 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2792 nesqp->nesqp_context->ird_ord_sizes |=
2793 cpu_to_le32((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
2795 /* Adjust tail for not having a LSMM */
2796 nesqp->hwqp.sq_tail = 1;
2798 #if defined(NES_SEND_FIRST_WRITE)
2799 if (cm_node->send_write0) {
2800 nes_debug(NES_DBG_CM, "Sending first write.\n");
2801 wqe = &nesqp->hwqp.sq_vbase[0];
2802 u64temp = (unsigned long)nesqp;
2803 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2804 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2806 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
2807 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
2808 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
2809 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
2810 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
2811 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
2813 /* use the reserved spot on the WQ for the extra first WQE */
2814 nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2815 NES_QPCONTEXT_ORDIRD_WRPDU | NES_QPCONTEXT_ORDIRD_ALSMM));
2816 nesqp->skip_lsmm = 1;
2817 nesqp->hwqp.sq_tail = 0;
2818 nes_write32(nesdev->regs + NES_WQE_ALLOC,
2819 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
2823 memset(&nes_quad, 0, sizeof(nes_quad));
2825 nes_quad.DstIpAdrIndex = cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2826 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2827 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2828 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
2830 /* Produce hash key */
2831 nesqp->hte_index = cpu_to_be32(
2832 crc32c(~0, (void *)&nes_quad, sizeof(nes_quad)) ^ 0xffffffff);
2833 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
2834 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
2836 nesqp->hte_index &= nesadapter->hte_index_mask;
2837 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2839 nesqp->ietf_frame = &cm_node->mpa_frame;
2840 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
2841 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2843 /* modify QP state to rts */
2844 attr.qp_state = IB_QPS_RTS;
2845 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
2847 /* notify OF layer we successfully created the requested connection */
2848 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
2849 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2850 cm_event.provider_data = cm_id->provider_data;
2851 cm_event.local_addr.sin_family = AF_INET;
2852 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
2853 cm_event.remote_addr = cm_id->remote_addr;
2855 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
2856 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
2858 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
2859 ret = cm_id->event_handler(cm_id, &cm_event);
2860 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2863 printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
2864 __FUNCTION__, __LINE__, ret);
2865 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = %lu\n",
2866 nesqp->hwqp.qp_id, jiffies );
2868 nes_rem_ref(&nesqp->ibqp);
2875 * cm_event_connect_error
2877 void cm_event_connect_error(struct nes_cm_event *event)
2879 struct nes_qp *nesqp;
2880 struct iw_cm_id *cm_id;
2881 struct iw_cm_event cm_event;
2882 /* struct nes_cm_info cm_info; */
2885 if (!event->cm_node)
2888 cm_id = event->cm_node->cm_id;
2893 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
2894 nesqp = cm_id->provider_data;
2900 /* notify OF layer about this connection error event */
2901 /* cm_id->rem_ref(cm_id); */
2902 nesqp->cm_id = NULL;
2903 cm_id->provider_data = NULL;
2904 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
2905 cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
2906 cm_event.provider_data = cm_id->provider_data;
2907 cm_event.local_addr = cm_id->local_addr;
2908 cm_event.remote_addr = cm_id->remote_addr;
2909 cm_event.private_data = NULL;
2910 cm_event.private_data_len = 0;
2912 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, remove_addr=%08x\n",
2913 cm_event.local_addr.sin_addr.s_addr, cm_event.remote_addr.sin_addr.s_addr);
2915 ret = cm_id->event_handler(cm_id, &cm_event);
2916 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2918 printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
2919 __FUNCTION__, __LINE__, ret);
2920 nes_rem_ref(&nesqp->ibqp);
2921 cm_id->rem_ref(cm_id);
2930 void cm_event_reset(struct nes_cm_event *event)
2932 struct nes_qp *nesqp;
2933 struct iw_cm_id *cm_id;
2934 struct iw_cm_event cm_event;
2935 /* struct nes_cm_info cm_info; */
2938 if (!event->cm_node)
2941 if (!event->cm_node->cm_id)
2944 cm_id = event->cm_node->cm_id;
2946 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
2947 nesqp = cm_id->provider_data;
2949 nesqp->cm_id = NULL;
2950 /* cm_id->provider_data = NULL; */
2951 cm_event.event = IW_CM_EVENT_DISCONNECT;
2952 cm_event.status = IW_CM_EVENT_STATUS_RESET;
2953 cm_event.provider_data = cm_id->provider_data;
2954 cm_event.local_addr = cm_id->local_addr;
2955 cm_event.remote_addr = cm_id->remote_addr;
2956 cm_event.private_data = NULL;
2957 cm_event.private_data_len = 0;
2959 ret = cm_id->event_handler(cm_id, &cm_event);
2960 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2963 /* notify OF layer about this connection error event */
2964 cm_id->rem_ref(cm_id);
2973 void cm_event_mpa_req(struct nes_cm_event *event)
2975 struct iw_cm_id *cm_id;
2976 struct iw_cm_event cm_event;
2978 struct nes_cm_node *cm_node;
2980 cm_node = event->cm_node;
2983 cm_id = cm_node->cm_id;
2985 atomic_inc(&cm_connect_reqs);
2986 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
2987 cm_node, cm_id, jiffies);
2989 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
2990 cm_event.status = IW_CM_EVENT_STATUS_OK;
2991 cm_event.provider_data = (void *)cm_node;
2993 cm_event.local_addr.sin_family = AF_INET;
2994 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
2995 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
2997 cm_event.remote_addr.sin_family = AF_INET;
2998 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
2999 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3001 cm_event.private_data = cm_node->mpa_frame_buf;
3002 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3004 ret = cm_id->event_handler(cm_id, &cm_event);
3006 printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
3007 __FUNCTION__, __LINE__, ret);
3013 static void nes_cm_event_handler(struct work_struct *);
3017 * post an event to the cm event handler
3019 int nes_cm_post_event(struct nes_cm_event *event)
3021 atomic_inc(&event->cm_node->cm_core->events_posted);
3022 add_ref_cm_node(event->cm_node);
3023 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3024 INIT_WORK(&event->event_work, nes_cm_event_handler);
3025 nes_debug(NES_DBG_CM, "queue_work, event=%p\n", event);
3027 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3029 nes_debug(NES_DBG_CM, "Exit\n");
3035 * nes_cm_event_handler
3036 * worker function to handle cm events
3037 * will free instance of nes_cm_event
3039 static void nes_cm_event_handler(struct work_struct *work)
3041 struct nes_cm_event *event = container_of(work, struct nes_cm_event, event_work);
3042 struct nes_cm_core *cm_core;
3044 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core)) {
3047 cm_core = event->cm_node->cm_core;
3048 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
3049 event, event->type, atomic_read(&cm_core->events_posted));
3051 switch (event->type) {
3052 case NES_CM_EVENT_MPA_REQ:
3053 cm_event_mpa_req(event);
3054 nes_debug(NES_DBG_CM, "CM Event: MPA REQUEST\n");
3056 case NES_CM_EVENT_RESET:
3057 nes_debug(NES_DBG_CM, "CM Event: RESET\n");
3058 cm_event_reset(event);
3060 case NES_CM_EVENT_CONNECTED:
3061 if ((!event->cm_node->cm_id) ||
3062 (event->cm_node->state != NES_CM_STATE_TSA)) {
3065 cm_event_connected(event);
3066 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3068 case NES_CM_EVENT_ABORTED:
3069 if ((!event->cm_node->cm_id) || (event->cm_node->state == NES_CM_STATE_TSA)) {
3072 cm_event_connect_error(event);
3073 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3075 case NES_CM_EVENT_DROPPED_PKT:
3076 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3079 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3083 atomic_dec(&cm_core->events_posted);
3084 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3085 rem_ref_cm_node(cm_core, event->cm_node);