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/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>
56 #include <net/neighbour.h>
57 #include <net/route.h>
58 #include <net/ip_fib.h>
63 u32 cm_packets_bounced;
64 u32 cm_packets_dropped;
65 u32 cm_packets_retrans;
66 u32 cm_packets_created;
67 u32 cm_packets_received;
68 u32 cm_listens_created;
69 u32 cm_listens_destroyed;
71 atomic_t cm_loopbacks;
72 atomic_t cm_nodes_created;
73 atomic_t cm_nodes_destroyed;
74 atomic_t cm_accel_dropped_pkts;
75 atomic_t cm_resets_recvd;
77 static inline int mini_cm_accelerated(struct nes_cm_core *, struct nes_cm_node *);
78 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
79 struct nes_vnic *, struct nes_cm_info *);
80 static int add_ref_cm_node(struct nes_cm_node *);
81 static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
82 static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
83 static struct sk_buff *form_cm_frame(struct sk_buff *, struct nes_cm_node *,
84 void *, u32, void *, u32, u8);
85 static struct sk_buff *get_free_pkt(struct nes_cm_node *cm_node);
87 static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
89 struct ietf_mpa_frame *,
90 struct nes_cm_info *);
91 static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
92 struct nes_cm_node *);
93 static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
94 struct nes_cm_node *);
95 static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
96 static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
98 static int mini_cm_dealloc_core(struct nes_cm_core *);
99 static int mini_cm_get(struct nes_cm_core *);
100 static int mini_cm_set(struct nes_cm_core *, u32, u32);
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);
105 static int send_ack(struct nes_cm_node *cm_node);
106 static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
108 /* External CM API Interface */
109 /* instance of function pointers for client API */
110 /* set address of this instance to cm_core->cm_ops at cm_core alloc */
111 static struct nes_cm_ops nes_cm_api = {
120 mini_cm_dealloc_core,
125 static struct nes_cm_core *g_cm_core;
127 atomic_t cm_connects;
129 atomic_t cm_disconnects;
131 atomic_t cm_connecteds;
132 atomic_t cm_connect_reqs;
139 static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
140 enum nes_cm_event_type type)
142 struct nes_cm_event *event;
147 /* allocate an empty event */
148 event = kzalloc(sizeof(*event), GFP_ATOMIC);
154 event->cm_node = cm_node;
155 event->cm_info.rem_addr = cm_node->rem_addr;
156 event->cm_info.loc_addr = cm_node->loc_addr;
157 event->cm_info.rem_port = cm_node->rem_port;
158 event->cm_info.loc_port = cm_node->loc_port;
159 event->cm_info.cm_id = cm_node->cm_id;
161 nes_debug(NES_DBG_CM, "Created event=%p, type=%u, dst_addr=%08x[%x],"
162 " src_addr=%08x[%x]\n",
164 event->cm_info.loc_addr, event->cm_info.loc_port,
165 event->cm_info.rem_addr, event->cm_info.rem_port);
167 nes_cm_post_event(event);
175 static int send_mpa_request(struct nes_cm_node *cm_node)
180 skb = get_free_pkt(cm_node);
182 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
186 /* send an MPA Request frame */
187 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
188 cm_node->mpa_frame_size, SET_ACK);
190 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
200 * recv_mpa - process a received TCP pkt, we are expecting an
203 static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 len)
205 struct ietf_mpa_frame *mpa_frame;
207 /* assume req frame is in tcp data payload */
208 if (len < sizeof(struct ietf_mpa_frame)) {
209 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
213 mpa_frame = (struct ietf_mpa_frame *)buffer;
214 cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
216 if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
217 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
218 " complete (%x + %x != %x)\n",
219 cm_node->mpa_frame_size, (u32)sizeof(struct ietf_mpa_frame), len);
223 /* copy entire MPA frame to our cm_node's frame */
224 memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
225 cm_node->mpa_frame_size);
232 * handle_exception_pkt - process an exception packet.
233 * We have been in a TSA state, and we have now received SW
234 * TCP/IP traffic should be a FIN request or IP pkt with options
236 static int handle_exception_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb)
239 struct tcphdr *tcph = tcp_hdr(skb);
241 /* first check to see if this a FIN pkt */
243 /* we need to ACK the FIN request */
246 /* check which side we are (client/server) and set next state accordingly */
247 if (cm_node->tcp_cntxt.client)
248 cm_node->state = NES_CM_STATE_CLOSING;
250 /* we are the server side */
251 cm_node->state = NES_CM_STATE_CLOSE_WAIT;
252 /* since this is a self contained CM we don't wait for */
253 /* an APP to close us, just send final FIN immediately */
254 ret = send_fin(cm_node, NULL);
255 cm_node->state = NES_CM_STATE_LAST_ACK;
266 * form_cm_frame - get a free packet and build empty frame Use
267 * node info to build.
269 static struct sk_buff *form_cm_frame(struct sk_buff *skb, struct nes_cm_node *cm_node,
270 void *options, u32 optionsize, void *data,
271 u32 datasize, u8 flags)
277 u16 packetsize = sizeof(*iph);
279 packetsize += sizeof(*tcph);
280 packetsize += optionsize + datasize;
282 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
285 buf = skb_put(skb, packetsize + ETH_HLEN);
287 ethh = (struct ethhdr *) buf;
290 iph = (struct iphdr *)buf;
292 tcph = (struct tcphdr *)buf;
293 skb_reset_mac_header(skb);
294 skb_set_network_header(skb, ETH_HLEN);
295 skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
296 buf += sizeof(*tcph);
298 skb->ip_summed = CHECKSUM_PARTIAL;
299 skb->protocol = htons(0x800);
301 skb->mac_len = ETH_HLEN;
303 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
304 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
305 ethh->h_proto = htons(0x0800);
307 iph->version = IPVERSION;
308 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
310 iph->tot_len = htons(packetsize);
311 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
313 iph->frag_off = htons(0x4000);
315 iph->protocol = 0x06; /* IPPROTO_TCP */
317 iph->saddr = htonl(cm_node->loc_addr);
318 iph->daddr = htonl(cm_node->rem_addr);
320 tcph->source = htons(cm_node->loc_port);
321 tcph->dest = htons(cm_node->rem_port);
322 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
324 if (flags & SET_ACK) {
325 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
326 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
331 if (flags & SET_SYN) {
332 cm_node->tcp_cntxt.loc_seq_num++;
335 cm_node->tcp_cntxt.loc_seq_num += datasize; /* data (no headers) */
343 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
344 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
347 memcpy(buf, options, optionsize);
350 memcpy(buf, data, datasize);
352 skb_shinfo(skb)->nr_frags = 0;
353 cm_packets_created++;
360 * print_core - dump a cm core
362 static void print_core(struct nes_cm_core *core)
364 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
365 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
368 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
370 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
372 nes_debug(NES_DBG_CM, "Tx Free cnt : %u \n", skb_queue_len(&core->tx_free_list));
373 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
374 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
376 nes_debug(NES_DBG_CM, "core : %p \n", core);
378 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
384 * note - cm_node needs to be protected before calling this. Encase in:
385 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
387 int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
388 enum nes_timer_type type, int send_retrans,
389 int close_when_complete)
392 struct nes_cm_core *cm_core;
393 struct nes_timer_entry *new_send;
399 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
403 /* new_send->timetosend = currenttime */
404 new_send->retrycount = NES_DEFAULT_RETRYS;
405 new_send->retranscount = NES_DEFAULT_RETRANS;
407 new_send->timetosend = jiffies;
408 new_send->type = type;
409 new_send->netdev = cm_node->netdev;
410 new_send->send_retrans = send_retrans;
411 new_send->close_when_complete = close_when_complete;
413 if (type == NES_TIMER_TYPE_CLOSE) {
414 new_send->timetosend += (HZ/2); /* TODO: decide on the correct value here */
415 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
416 list_add_tail(&new_send->list, &cm_node->recv_list);
417 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
420 if (type == NES_TIMER_TYPE_SEND) {
421 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
422 atomic_inc(&new_send->skb->users);
424 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
425 if (ret != NETDEV_TX_OK) {
426 nes_debug(NES_DBG_CM, "Error sending packet %p (jiffies = %lu)\n",
428 atomic_dec(&new_send->skb->users);
429 new_send->timetosend = jiffies;
433 if (close_when_complete)
434 rem_ref_cm_node(cm_node->cm_core, cm_node);
435 dev_kfree_skb_any(new_send->skb);
439 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
441 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
442 list_add_tail(&new_send->list, &cm_node->retrans_list);
443 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
445 if (type == NES_TIMER_TYPE_RECV) {
446 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
447 new_send->timetosend = jiffies;
448 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
449 list_add_tail(&new_send->list, &cm_node->recv_list);
450 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
452 cm_core = cm_node->cm_core;
454 was_timer_set = timer_pending(&cm_core->tcp_timer);
456 if (!was_timer_set) {
457 cm_core->tcp_timer.expires = new_send->timetosend;
458 add_timer(&cm_core->tcp_timer);
468 static void nes_cm_timer_tick(unsigned long pass)
470 unsigned long flags, qplockflags;
471 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
472 struct iw_cm_id *cm_id;
473 struct nes_cm_node *cm_node;
474 struct nes_timer_entry *send_entry, *recv_entry;
475 struct list_head *list_core, *list_core_temp;
476 struct list_head *list_node, *list_node_temp;
477 struct nes_cm_core *cm_core = g_cm_core;
478 struct nes_qp *nesqp;
481 int ret = NETDEV_TX_OK;
484 spin_lock_irqsave(&cm_core->ht_lock, flags);
486 list_for_each_safe(list_node, list_core_temp, &cm_core->connected_nodes) {
487 cm_node = container_of(list_node, struct nes_cm_node, list);
488 add_ref_cm_node(cm_node);
489 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
490 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
491 list_for_each_safe(list_core, list_node_temp, &cm_node->recv_list) {
492 recv_entry = container_of(list_core, struct nes_timer_entry, list);
493 if ((time_after(recv_entry->timetosend, jiffies)) &&
494 (recv_entry->type == NES_TIMER_TYPE_CLOSE)) {
495 if (nexttimeout > recv_entry->timetosend || !settimer) {
496 nexttimeout = recv_entry->timetosend;
501 list_del(&recv_entry->list);
502 cm_id = cm_node->cm_id;
503 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
504 if (recv_entry->type == NES_TIMER_TYPE_CLOSE) {
505 nesqp = (struct nes_qp *)recv_entry->skb;
506 spin_lock_irqsave(&nesqp->lock, qplockflags);
508 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, refcount = %d: "
509 "****** HIT A NES_TIMER_TYPE_CLOSE"
510 " with something to do!!! ******\n",
511 nesqp->hwqp.qp_id, cm_id,
512 atomic_read(&nesqp->refcount));
513 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
514 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
515 nesqp->ibqp_state = IB_QPS_ERR;
516 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
517 nes_cm_disconn(nesqp);
519 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
520 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, refcount = %d:"
521 " ****** HIT A NES_TIMER_TYPE_CLOSE"
522 " with nothing to do!!! ******\n",
523 nesqp->hwqp.qp_id, cm_id,
524 atomic_read(&nesqp->refcount));
525 nes_rem_ref(&nesqp->ibqp);
528 cm_id->rem_ref(cm_id);
531 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
533 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
535 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
537 list_for_each_safe(list_core, list_node_temp, &cm_node->retrans_list) {
541 send_entry = container_of(list_core, struct nes_timer_entry, list);
542 if (time_after(send_entry->timetosend, jiffies)) {
543 if (cm_node->state != NES_CM_STATE_TSA) {
544 if ((nexttimeout > send_entry->timetosend) || !settimer) {
545 nexttimeout = send_entry->timetosend;
551 list_del(&send_entry->list);
552 skb = send_entry->skb;
553 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
554 dev_kfree_skb_any(skb);
556 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
560 if (send_entry->type == NES_TIMER_NODE_CLEANUP) {
561 list_del(&send_entry->list);
562 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
564 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
567 if ((send_entry->seq_num < cm_node->tcp_cntxt.rem_ack_num) ||
568 (cm_node->state == NES_CM_STATE_TSA) ||
569 (cm_node->state == NES_CM_STATE_CLOSED)) {
570 skb = send_entry->skb;
571 list_del(&send_entry->list);
572 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
574 dev_kfree_skb_any(skb);
575 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
579 if (!send_entry->retranscount || !send_entry->retrycount) {
580 cm_packets_dropped++;
581 skb = send_entry->skb;
582 list_del(&send_entry->list);
583 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
584 dev_kfree_skb_any(skb);
586 if (cm_node->state == NES_CM_STATE_SYN_RCVD) {
587 /* this node never even generated an indication up to the cm */
588 rem_ref_cm_node(cm_core, cm_node);
590 cm_node->state = NES_CM_STATE_CLOSED;
591 create_event(cm_node, NES_CM_EVENT_ABORTED);
593 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
596 /* this seems like the correct place, but leave send entry unprotected */
597 // spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
598 atomic_inc(&send_entry->skb->users);
599 cm_packets_retrans++;
600 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p for node %p,"
601 " jiffies = %lu, time to send = %lu, retranscount = %u, "
602 "send_entry->seq_num = 0x%08X, cm_node->tcp_cntxt.rem_ack_num = 0x%08X\n",
603 send_entry, cm_node, jiffies, send_entry->timetosend, send_entry->retranscount,
604 send_entry->seq_num, cm_node->tcp_cntxt.rem_ack_num);
606 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
607 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
608 if (ret != NETDEV_TX_OK) {
609 cm_packets_bounced++;
610 atomic_dec(&send_entry->skb->users);
611 send_entry->retrycount--;
612 nexttimeout = jiffies + NES_SHORT_TIME;
615 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
620 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
621 list_del(&send_entry->list);
622 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = %u, retry count = %u.\n",
623 send_entry->retranscount, send_entry->retrycount);
624 if (send_entry->send_retrans) {
625 send_entry->retranscount--;
626 send_entry->timetosend = jiffies + NES_RETRY_TIMEOUT;
627 if (nexttimeout > send_entry->timetosend || !settimer) {
628 nexttimeout = send_entry->timetosend;
631 list_add(&send_entry->list, &cm_node->retrans_list);
634 int close_when_complete;
635 skb = send_entry->skb;
636 close_when_complete = send_entry->close_when_complete;
637 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
638 if (close_when_complete) {
639 BUG_ON(atomic_read(&cm_node->ref_count) == 1);
640 rem_ref_cm_node(cm_core, cm_node);
642 dev_kfree_skb_any(skb);
644 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
648 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
650 rem_ref_cm_node(cm_core, cm_node);
652 spin_lock_irqsave(&cm_core->ht_lock, flags);
653 if (ret != NETDEV_TX_OK)
656 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
659 if (!timer_pending(&cm_core->tcp_timer)) {
660 cm_core->tcp_timer.expires = nexttimeout;
661 add_timer(&cm_core->tcp_timer);
670 static int send_syn(struct nes_cm_node *cm_node, u32 sendack)
675 char optionsbuffer[sizeof(struct option_mss) +
676 sizeof(struct option_windowscale) +
677 sizeof(struct option_base) + 1];
680 /* Sending MSS option */
681 union all_known_options *options;
686 options = (union all_known_options *)&optionsbuffer[optionssize];
687 options->as_mss.optionnum = OPTION_NUMBER_MSS;
688 options->as_mss.length = sizeof(struct option_mss);
689 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
690 optionssize += sizeof(struct option_mss);
692 options = (union all_known_options *)&optionsbuffer[optionssize];
693 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
694 options->as_windowscale.length = sizeof(struct option_windowscale);
695 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
696 optionssize += sizeof(struct option_windowscale);
698 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)
700 options = (union all_known_options *)&optionsbuffer[optionssize];
701 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
702 options->as_base.length = sizeof(struct option_base);
703 optionssize += sizeof(struct option_base);
704 /* we need the size to be a multiple of 4 */
705 options = (union all_known_options *)&optionsbuffer[optionssize];
708 options = (union all_known_options *)&optionsbuffer[optionssize];
713 options = (union all_known_options *)&optionsbuffer[optionssize];
714 options->as_end = OPTION_NUMBER_END;
717 skb = get_free_pkt(cm_node);
719 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
726 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
727 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
736 static int send_reset(struct nes_cm_node *cm_node)
739 struct sk_buff *skb = get_free_pkt(cm_node);
740 int flags = SET_RST | SET_ACK;
743 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
747 add_ref_cm_node(cm_node);
748 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
749 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
758 static int send_ack(struct nes_cm_node *cm_node)
761 struct sk_buff *skb = get_free_pkt(cm_node);
764 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
768 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
769 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
778 static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
782 /* if we didn't get a frame get one */
784 skb = get_free_pkt(cm_node);
787 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
791 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
792 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
801 static struct sk_buff *get_free_pkt(struct nes_cm_node *cm_node)
803 struct sk_buff *skb, *new_skb;
805 /* check to see if we need to repopulate the free tx pkt queue */
806 if (skb_queue_len(&cm_node->cm_core->tx_free_list) < NES_CM_FREE_PKT_LO_WATERMARK) {
807 while (skb_queue_len(&cm_node->cm_core->tx_free_list) <
808 cm_node->cm_core->free_tx_pkt_max) {
809 /* replace the frame we took, we won't get it back */
810 new_skb = dev_alloc_skb(cm_node->cm_core->mtu);
812 /* add a replacement frame to the free tx list head */
813 skb_queue_head(&cm_node->cm_core->tx_free_list, new_skb);
817 skb = skb_dequeue(&cm_node->cm_core->tx_free_list);
824 * make_hashkey - generate hash key from node tuple
826 static inline int make_hashkey(u16 loc_port, nes_addr_t loc_addr, u16 rem_port,
831 hashkey = loc_addr + rem_addr + loc_port + rem_port;
832 hashkey = (hashkey % NES_CM_HASHTABLE_SIZE);
839 * find_node - find a cm node that matches the reference cm node
841 static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
842 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
846 struct list_head *list_pos;
847 struct list_head *hte;
848 struct nes_cm_node *cm_node;
850 /* make a hash index key for this packet */
851 hashkey = make_hashkey(loc_port, loc_addr, rem_port, rem_addr);
853 /* get a handle on the hte */
854 hte = &cm_core->connected_nodes;
856 nes_debug(NES_DBG_CM, "Searching for an owner node:%x:%x from core %p->%p\n",
857 loc_addr, loc_port, cm_core, hte);
859 /* walk list and find cm_node associated with this session ID */
860 spin_lock_irqsave(&cm_core->ht_lock, flags);
861 list_for_each(list_pos, hte) {
862 cm_node = container_of(list_pos, struct nes_cm_node, list);
863 /* compare quad, return node handle if a match */
864 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
865 cm_node->loc_addr, cm_node->loc_port,
867 cm_node->rem_addr, cm_node->rem_port,
869 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
870 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
871 add_ref_cm_node(cm_node);
872 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
876 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
884 * find_listener - find a cm node listening on this addr-port pair
886 static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
887 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
890 struct list_head *listen_list;
891 struct nes_cm_listener *listen_node;
893 /* walk list and find cm_node associated with this session ID */
894 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
895 list_for_each(listen_list, &cm_core->listen_list.list) {
896 listen_node = container_of(listen_list, struct nes_cm_listener, list);
897 /* compare node pair, return node handle if a match */
898 if (((listen_node->loc_addr == dst_addr) ||
899 listen_node->loc_addr == 0x00000000) &&
900 (listen_node->loc_port == dst_port) &&
901 (listener_state & listen_node->listener_state)) {
902 atomic_inc(&listen_node->ref_count);
903 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
907 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
909 nes_debug(NES_DBG_CM, "Unable to find listener- %x:%x\n",
918 * add_hte_node - add a cm node to the hash table
920 static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
924 struct list_head *hte;
926 if (!cm_node || !cm_core)
929 nes_debug(NES_DBG_CM, "Adding Node to Active Connection HT\n");
931 /* first, make an index into our hash table */
932 hashkey = make_hashkey(cm_node->loc_port, cm_node->loc_addr,
933 cm_node->rem_port, cm_node->rem_addr);
934 cm_node->hashkey = hashkey;
936 spin_lock_irqsave(&cm_core->ht_lock, flags);
938 /* get a handle on the hash table element (list head for this slot) */
939 hte = &cm_core->connected_nodes;
940 list_add_tail(&cm_node->list, hte);
941 atomic_inc(&cm_core->ht_node_cnt);
943 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
950 * mini_cm_dec_refcnt_listen
952 static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
953 struct nes_cm_listener *listener, int free_hanging_nodes)
957 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
958 if (!atomic_dec_return(&listener->ref_count)) {
959 list_del(&listener->list);
961 /* decrement our listen node count */
962 atomic_dec(&cm_core->listen_node_cnt);
964 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
966 if (listener->nesvnic) {
967 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
968 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
971 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
976 cm_listens_destroyed++;
978 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
981 if (atomic_read(&listener->pend_accepts_cnt) > 0)
982 nes_debug(NES_DBG_CM, "destroying listener (%p)"
983 " with non-zero pending accepts=%u\n",
984 listener, atomic_read(&listener->pend_accepts_cnt));
994 static int mini_cm_del_listen(struct nes_cm_core *cm_core,
995 struct nes_cm_listener *listener)
997 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
998 listener->cm_id = NULL; /* going to be destroyed pretty soon */
999 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1004 * mini_cm_accelerated
1006 static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1007 struct nes_cm_node *cm_node)
1010 cm_node->accelerated = 1;
1012 if (cm_node->accept_pend) {
1013 BUG_ON(!cm_node->listener);
1014 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1015 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1018 was_timer_set = timer_pending(&cm_core->tcp_timer);
1019 if (!was_timer_set) {
1020 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1021 add_timer(&cm_core->tcp_timer);
1031 static void nes_addr_send_arp(u32 dst_ip)
1036 memset(&fl, 0, sizeof fl);
1037 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1038 if (ip_route_output_key(&init_net, &rt, &fl)) {
1039 printk("%s: ip_route_output_key failed for 0x%08X\n",
1044 neigh_event_send(rt->u.dst.neighbour, NULL);
1050 * make_cm_node - create a new instance of a cm node
1052 static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1053 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1054 struct nes_cm_listener *listener)
1056 struct nes_cm_node *cm_node;
1059 struct nes_device *nesdev;
1060 struct nes_adapter *nesadapter;
1062 /* create an hte and cm_node for this instance */
1063 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1067 /* set our node specific transport info */
1068 cm_node->loc_addr = cm_info->loc_addr;
1069 cm_node->rem_addr = cm_info->rem_addr;
1070 cm_node->loc_port = cm_info->loc_port;
1071 cm_node->rem_port = cm_info->rem_port;
1072 cm_node->send_write0 = send_first;
1073 nes_debug(NES_DBG_CM, "Make node addresses : loc = %x:%x, rem = %x:%x\n",
1074 cm_node->loc_addr, cm_node->loc_port, cm_node->rem_addr, cm_node->rem_port);
1075 cm_node->listener = listener;
1076 cm_node->netdev = nesvnic->netdev;
1077 cm_node->cm_id = cm_info->cm_id;
1078 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1080 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n",
1081 cm_node->listener, cm_node->cm_id);
1083 INIT_LIST_HEAD(&cm_node->retrans_list);
1084 spin_lock_init(&cm_node->retrans_list_lock);
1085 INIT_LIST_HEAD(&cm_node->recv_list);
1086 spin_lock_init(&cm_node->recv_list_lock);
1088 cm_node->loopbackpartner = NULL;
1089 atomic_set(&cm_node->ref_count, 1);
1090 /* associate our parent CM core */
1091 cm_node->cm_core = cm_core;
1092 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1093 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1094 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1095 NES_CM_DEFAULT_RCV_WND_SCALE;
1096 ts = current_kernel_time();
1097 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1098 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
1099 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
1100 cm_node->tcp_cntxt.rcv_nxt = 0;
1101 /* get a unique session ID , add thread_id to an upcounter to handle race */
1102 atomic_inc(&cm_core->node_cnt);
1103 cm_node->conn_type = cm_info->conn_type;
1104 cm_node->apbvt_set = 0;
1105 cm_node->accept_pend = 0;
1107 cm_node->nesvnic = nesvnic;
1108 /* get some device handles, for arp lookup */
1109 nesdev = nesvnic->nesdev;
1110 nesadapter = nesdev->nesadapter;
1112 cm_node->loopbackpartner = NULL;
1113 /* get the mac addr for the remote node */
1114 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1117 nes_addr_send_arp(cm_info->rem_addr);
1121 /* copy the mac addr to node context */
1122 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
1123 nes_debug(NES_DBG_CM, "Remote mac addr from arp table:%02x,"
1124 " %02x, %02x, %02x, %02x, %02x\n",
1125 cm_node->rem_mac[0], cm_node->rem_mac[1],
1126 cm_node->rem_mac[2], cm_node->rem_mac[3],
1127 cm_node->rem_mac[4], cm_node->rem_mac[5]);
1129 add_hte_node(cm_core, cm_node);
1130 atomic_inc(&cm_nodes_created);
1137 * add_ref_cm_node - destroy an instance of a cm node
1139 static int add_ref_cm_node(struct nes_cm_node *cm_node)
1141 atomic_inc(&cm_node->ref_count);
1147 * rem_ref_cm_node - destroy an instance of a cm node
1149 static int rem_ref_cm_node(struct nes_cm_core *cm_core,
1150 struct nes_cm_node *cm_node)
1152 unsigned long flags, qplockflags;
1153 struct nes_timer_entry *send_entry;
1154 struct nes_timer_entry *recv_entry;
1155 struct iw_cm_id *cm_id;
1156 struct list_head *list_core, *list_node_temp;
1157 struct nes_qp *nesqp;
1162 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1163 if (atomic_dec_return(&cm_node->ref_count)) {
1164 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1167 list_del(&cm_node->list);
1168 atomic_dec(&cm_core->ht_node_cnt);
1169 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1171 /* if the node is destroyed before connection was accelerated */
1172 if (!cm_node->accelerated && cm_node->accept_pend) {
1173 BUG_ON(!cm_node->listener);
1174 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1175 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1178 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1179 list_for_each_safe(list_core, list_node_temp, &cm_node->retrans_list) {
1180 send_entry = container_of(list_core, struct nes_timer_entry, list);
1181 list_del(&send_entry->list);
1182 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1183 dev_kfree_skb_any(send_entry->skb);
1185 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1188 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1190 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
1191 list_for_each_safe(list_core, list_node_temp, &cm_node->recv_list) {
1192 recv_entry = container_of(list_core, struct nes_timer_entry, list);
1193 list_del(&recv_entry->list);
1194 cm_id = cm_node->cm_id;
1195 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
1196 if (recv_entry->type == NES_TIMER_TYPE_CLOSE) {
1197 nesqp = (struct nes_qp *)recv_entry->skb;
1198 spin_lock_irqsave(&nesqp->lock, qplockflags);
1200 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p: ****** HIT A NES_TIMER_TYPE_CLOSE"
1201 " with something to do!!! ******\n",
1202 nesqp->hwqp.qp_id, cm_id);
1203 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
1204 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
1205 nesqp->ibqp_state = IB_QPS_ERR;
1206 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
1207 nes_cm_disconn(nesqp);
1209 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
1210 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p: ****** HIT A NES_TIMER_TYPE_CLOSE"
1211 " with nothing to do!!! ******\n",
1212 nesqp->hwqp.qp_id, cm_id);
1213 nes_rem_ref(&nesqp->ibqp);
1215 cm_id->rem_ref(cm_id);
1216 } else if (recv_entry->type == NES_TIMER_TYPE_RECV) {
1217 dev_kfree_skb_any(recv_entry->skb);
1220 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
1222 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
1224 if (cm_node->listener) {
1225 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1227 if (cm_node->apbvt_set && cm_node->nesvnic) {
1228 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
1229 PCI_FUNC(cm_node->nesvnic->nesdev->pcidev->devfn),
1230 NES_MANAGE_APBVT_DEL);
1235 atomic_dec(&cm_core->node_cnt);
1236 atomic_inc(&cm_nodes_destroyed);
1245 static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc, u32 optionsize, u32 syn_packet)
1249 union all_known_options *all_options;
1250 char got_mss_option = 0;
1252 while (offset < optionsize) {
1253 all_options = (union all_known_options *)(optionsloc + offset);
1254 switch (all_options->as_base.optionnum) {
1255 case OPTION_NUMBER_END:
1256 offset = optionsize;
1258 case OPTION_NUMBER_NONE:
1261 case OPTION_NUMBER_MSS:
1262 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d Size: %d\n",
1264 all_options->as_mss.length, offset, optionsize);
1266 if (all_options->as_mss.length != 4) {
1269 tmp = ntohs(all_options->as_mss.mss);
1270 if (tmp > 0 && tmp < cm_node->tcp_cntxt.mss)
1271 cm_node->tcp_cntxt.mss = tmp;
1274 case OPTION_NUMBER_WINDOW_SCALE:
1275 cm_node->tcp_cntxt.snd_wscale = all_options->as_windowscale.shiftcount;
1277 case OPTION_NUMBER_WRITE0:
1278 cm_node->send_write0 = 1;
1281 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1282 all_options->as_base.optionnum);
1285 offset += all_options->as_base.length;
1287 if ((!got_mss_option) && (syn_packet))
1288 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1296 static int process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1297 struct nes_cm_core *cm_core)
1302 struct tcphdr *tcph = tcp_hdr(skb);
1304 if (cm_node->state == NES_CM_STATE_SYN_SENT && tcph->syn) {
1305 inc_sequence = ntohl(tcph->seq);
1306 cm_node->tcp_cntxt.rcv_nxt = inc_sequence;
1309 if ((!tcph) || (cm_node->state == NES_CM_STATE_TSA)) {
1311 atomic_inc(&cm_accel_dropped_pkts);
1316 atomic_inc(&cm_resets_recvd);
1317 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u. refcnt=%d\n",
1318 cm_node, cm_node->state, atomic_read(&cm_node->ref_count));
1319 switch (cm_node->state) {
1320 case NES_CM_STATE_LISTENING:
1321 rem_ref_cm_node(cm_core, cm_node);
1323 case NES_CM_STATE_TSA:
1324 case NES_CM_STATE_CLOSED:
1326 case NES_CM_STATE_SYN_RCVD:
1327 nes_debug(NES_DBG_CM, "Received a reset for local 0x%08X:%04X,"
1328 " remote 0x%08X:%04X, node state = %u\n",
1329 cm_node->loc_addr, cm_node->loc_port,
1330 cm_node->rem_addr, cm_node->rem_port,
1332 rem_ref_cm_node(cm_core, cm_node);
1334 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
1335 case NES_CM_STATE_ESTABLISHED:
1336 case NES_CM_STATE_MPAREQ_SENT:
1338 nes_debug(NES_DBG_CM, "Received a reset for local 0x%08X:%04X,"
1339 " remote 0x%08X:%04X, node state = %u refcnt=%d\n",
1340 cm_node->loc_addr, cm_node->loc_port,
1341 cm_node->rem_addr, cm_node->rem_port,
1342 cm_node->state, atomic_read(&cm_node->ref_count));
1344 cm_node->state = NES_CM_STATE_CLOSED;
1346 create_event(cm_node, NES_CM_EVENT_ABORTED);
1353 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1355 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1356 skb_pull(skb, tcph->doff << 2);
1358 datasize = skb->len;
1359 inc_sequence = ntohl(tcph->seq);
1360 nes_debug(NES_DBG_CM, "datasize = %u, sequence = 0x%08X, ack_seq = 0x%08X,"
1361 " rcv_nxt = 0x%08X Flags: %s %s.\n",
1362 datasize, inc_sequence, ntohl(tcph->ack_seq),
1363 cm_node->tcp_cntxt.rcv_nxt, (tcph->syn ? "SYN":""),
1364 (tcph->ack ? "ACK":""));
1366 if (!tcph->syn && (inc_sequence != cm_node->tcp_cntxt.rcv_nxt)
1368 nes_debug(NES_DBG_CM, "dropping packet, datasize = %u, sequence = 0x%08X,"
1369 " ack_seq = 0x%08X, rcv_nxt = 0x%08X Flags: %s.\n",
1370 datasize, inc_sequence, ntohl(tcph->ack_seq),
1371 cm_node->tcp_cntxt.rcv_nxt, (tcph->ack ? "ACK":""));
1372 if (cm_node->state == NES_CM_STATE_LISTENING) {
1373 rem_ref_cm_node(cm_core, cm_node);
1378 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1382 u8 *optionsloc = (u8 *)&tcph[1];
1383 if (process_options(cm_node, optionsloc, optionsize, (u32)tcph->syn)) {
1384 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n", __func__, cm_node);
1385 send_reset(cm_node);
1386 if (cm_node->state != NES_CM_STATE_SYN_SENT)
1387 rem_ref_cm_node(cm_core, cm_node);
1390 } else if (tcph->syn)
1391 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1393 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1394 cm_node->tcp_cntxt.snd_wscale;
1396 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd) {
1397 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
1401 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1402 switch (cm_node->state) {
1403 case NES_CM_STATE_SYN_RCVD:
1404 case NES_CM_STATE_SYN_SENT:
1405 /* read and stash current sequence number */
1406 if (cm_node->tcp_cntxt.rem_ack_num != cm_node->tcp_cntxt.loc_seq_num) {
1407 nes_debug(NES_DBG_CM, "ERROR - cm_node->tcp_cntxt.rem_ack_num !="
1408 " cm_node->tcp_cntxt.loc_seq_num\n");
1409 send_reset(cm_node);
1412 if (cm_node->state == NES_CM_STATE_SYN_SENT)
1413 cm_node->state = NES_CM_STATE_ONE_SIDE_ESTABLISHED;
1415 cm_node->state = NES_CM_STATE_ESTABLISHED;
1418 case NES_CM_STATE_LAST_ACK:
1419 cm_node->state = NES_CM_STATE_CLOSED;
1421 case NES_CM_STATE_FIN_WAIT1:
1422 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1424 case NES_CM_STATE_CLOSING:
1425 cm_node->state = NES_CM_STATE_TIME_WAIT;
1426 /* need to schedule this to happen in 2MSL timeouts */
1427 cm_node->state = NES_CM_STATE_CLOSED;
1429 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
1430 case NES_CM_STATE_ESTABLISHED:
1431 case NES_CM_STATE_MPAREQ_SENT:
1432 case NES_CM_STATE_CLOSE_WAIT:
1433 case NES_CM_STATE_TIME_WAIT:
1434 case NES_CM_STATE_CLOSED:
1436 case NES_CM_STATE_LISTENING:
1437 nes_debug(NES_DBG_CM, "Received an ACK on a listening port (SYN %d)\n", tcph->syn);
1438 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1439 send_reset(cm_node);
1440 /* send_reset bumps refcount, this should have been a new node */
1441 rem_ref_cm_node(cm_core, cm_node);
1444 case NES_CM_STATE_TSA:
1445 nes_debug(NES_DBG_CM, "Received a packet with the ack bit set while in TSA state\n");
1447 case NES_CM_STATE_UNKNOWN:
1448 case NES_CM_STATE_INITED:
1449 case NES_CM_STATE_ACCEPTING:
1450 case NES_CM_STATE_FIN_WAIT2:
1452 nes_debug(NES_DBG_CM, "Received ack from unknown state: %x\n",
1454 send_reset(cm_node);
1460 if (cm_node->state == NES_CM_STATE_LISTENING) {
1461 /* do not exceed backlog */
1462 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1463 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1464 cm_node->listener->backlog) {
1465 nes_debug(NES_DBG_CM, "drop syn due to backlog pressure \n");
1467 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1468 rem_ref_cm_node(cm_core, cm_node);
1471 cm_node->accept_pend = 1;
1475 cm_node->tcp_cntxt.rcv_nxt ++;
1477 if (cm_node->state == NES_CM_STATE_LISTENING) {
1478 cm_node->state = NES_CM_STATE_SYN_RCVD;
1479 send_syn(cm_node, 1);
1481 if (cm_node->state == NES_CM_STATE_ONE_SIDE_ESTABLISHED) {
1482 cm_node->state = NES_CM_STATE_ESTABLISHED;
1483 /* send final handshake ACK */
1484 ret = send_ack(cm_node);
1488 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1489 ret = send_mpa_request(cm_node);
1496 cm_node->tcp_cntxt.rcv_nxt++;
1497 switch (cm_node->state) {
1498 case NES_CM_STATE_SYN_RCVD:
1499 case NES_CM_STATE_SYN_SENT:
1500 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
1501 case NES_CM_STATE_ESTABLISHED:
1502 case NES_CM_STATE_ACCEPTING:
1503 case NES_CM_STATE_MPAREQ_SENT:
1504 cm_node->state = NES_CM_STATE_CLOSE_WAIT;
1505 cm_node->state = NES_CM_STATE_LAST_ACK;
1506 ret = send_fin(cm_node, NULL);
1508 case NES_CM_STATE_FIN_WAIT1:
1509 cm_node->state = NES_CM_STATE_CLOSING;
1510 ret = send_ack(cm_node);
1512 case NES_CM_STATE_FIN_WAIT2:
1513 cm_node->state = NES_CM_STATE_TIME_WAIT;
1514 cm_node->tcp_cntxt.loc_seq_num ++;
1515 ret = send_ack(cm_node);
1516 /* need to schedule this to happen in 2MSL timeouts */
1517 cm_node->state = NES_CM_STATE_CLOSED;
1519 case NES_CM_STATE_CLOSE_WAIT:
1520 case NES_CM_STATE_LAST_ACK:
1521 case NES_CM_STATE_CLOSING:
1522 case NES_CM_STATE_TSA:
1524 nes_debug(NES_DBG_CM, "Received a fin while in %x state\n",
1532 u8 *dataloc = skb->data;
1533 /* figure out what state we are in and handle transition to next state */
1534 switch (cm_node->state) {
1535 case NES_CM_STATE_LISTENING:
1536 case NES_CM_STATE_SYN_RCVD:
1537 case NES_CM_STATE_SYN_SENT:
1538 case NES_CM_STATE_FIN_WAIT1:
1539 case NES_CM_STATE_FIN_WAIT2:
1540 case NES_CM_STATE_CLOSE_WAIT:
1541 case NES_CM_STATE_LAST_ACK:
1542 case NES_CM_STATE_CLOSING:
1544 case NES_CM_STATE_MPAREQ_SENT:
1545 /* recv the mpa res frame, ret=frame len (incl priv data) */
1546 ret = parse_mpa(cm_node, dataloc, datasize);
1549 /* set the req frame payload len in skb */
1550 /* we are done handling this state, set node to a TSA state */
1551 cm_node->state = NES_CM_STATE_TSA;
1553 create_event(cm_node, NES_CM_EVENT_CONNECTED);
1556 case NES_CM_STATE_ESTABLISHED:
1557 /* we are expecting an MPA req frame */
1558 ret = parse_mpa(cm_node, dataloc, datasize);
1562 cm_node->state = NES_CM_STATE_TSA;
1564 /* we got a valid MPA request, create an event */
1565 create_event(cm_node, NES_CM_EVENT_MPA_REQ);
1567 case NES_CM_STATE_TSA:
1568 handle_exception_pkt(cm_node, skb);
1570 case NES_CM_STATE_UNKNOWN:
1571 case NES_CM_STATE_INITED:
1582 * mini_cm_listen - create a listen node with params
1584 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
1585 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
1587 struct nes_cm_listener *listener;
1588 unsigned long flags;
1590 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1591 cm_info->loc_addr, cm_info->loc_port);
1593 /* cannot have multiple matching listeners */
1594 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1595 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1596 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1597 /* find automatically incs ref count ??? */
1598 atomic_dec(&listener->ref_count);
1599 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1604 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1605 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1607 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1611 memset(listener, 0, sizeof(struct nes_cm_listener));
1612 listener->loc_addr = htonl(cm_info->loc_addr);
1613 listener->loc_port = htons(cm_info->loc_port);
1614 listener->reused_node = 0;
1616 atomic_set(&listener->ref_count, 1);
1619 /* find already inc'ed the ref count */
1621 listener->reused_node = 1;
1624 listener->cm_id = cm_info->cm_id;
1625 atomic_set(&listener->pend_accepts_cnt, 0);
1626 listener->cm_core = cm_core;
1627 listener->nesvnic = nesvnic;
1628 atomic_inc(&cm_core->node_cnt);
1630 listener->conn_type = cm_info->conn_type;
1631 listener->backlog = cm_info->backlog;
1632 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1634 if (!listener->reused_node) {
1635 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1636 list_add(&listener->list, &cm_core->listen_list.list);
1637 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1638 atomic_inc(&cm_core->listen_node_cnt);
1641 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1642 " listener = %p, backlog = %d, cm_id = %p.\n",
1643 cm_info->loc_addr, cm_info->loc_port,
1644 listener, listener->backlog, listener->cm_id);
1651 * mini_cm_connect - make a connection node with params
1653 static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
1654 struct nes_vnic *nesvnic,
1655 struct ietf_mpa_frame *mpa_frame,
1656 struct nes_cm_info *cm_info)
1659 struct nes_cm_node *cm_node;
1660 struct nes_cm_listener *loopbackremotelistener;
1661 struct nes_cm_node *loopbackremotenode;
1662 struct nes_cm_info loopback_cm_info;
1664 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) +
1665 ntohs(mpa_frame->priv_data_len);
1667 cm_info->loc_addr = htonl(cm_info->loc_addr);
1668 cm_info->rem_addr = htonl(cm_info->rem_addr);
1669 cm_info->loc_port = htons(cm_info->loc_port);
1670 cm_info->rem_port = htons(cm_info->rem_port);
1672 /* create a CM connection node */
1673 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
1677 // set our node side to client (active) side
1678 cm_node->tcp_cntxt.client = 1;
1679 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1681 if (cm_info->loc_addr == cm_info->rem_addr) {
1682 loopbackremotelistener = find_listener(cm_core, cm_node->rem_addr,
1683 cm_node->rem_port, NES_CM_LISTENER_ACTIVE_STATE);
1684 if (loopbackremotelistener == NULL) {
1685 create_event(cm_node, NES_CM_EVENT_ABORTED);
1687 atomic_inc(&cm_loopbacks);
1688 loopback_cm_info = *cm_info;
1689 loopback_cm_info.loc_port = cm_info->rem_port;
1690 loopback_cm_info.rem_port = cm_info->loc_port;
1691 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
1692 loopbackremotenode = make_cm_node(cm_core, nesvnic, &loopback_cm_info,
1693 loopbackremotelistener);
1694 loopbackremotenode->loopbackpartner = cm_node;
1695 loopbackremotenode->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1696 cm_node->loopbackpartner = loopbackremotenode;
1697 memcpy(loopbackremotenode->mpa_frame_buf, &mpa_frame->priv_data,
1699 loopbackremotenode->mpa_frame_size = mpa_frame_size -
1700 sizeof(struct ietf_mpa_frame);
1702 // we are done handling this state, set node to a TSA state
1703 cm_node->state = NES_CM_STATE_TSA;
1704 cm_node->tcp_cntxt.rcv_nxt = loopbackremotenode->tcp_cntxt.loc_seq_num;
1705 loopbackremotenode->tcp_cntxt.rcv_nxt = cm_node->tcp_cntxt.loc_seq_num;
1706 cm_node->tcp_cntxt.max_snd_wnd = loopbackremotenode->tcp_cntxt.rcv_wnd;
1707 loopbackremotenode->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.rcv_wnd;
1708 cm_node->tcp_cntxt.snd_wnd = loopbackremotenode->tcp_cntxt.rcv_wnd;
1709 loopbackremotenode->tcp_cntxt.snd_wnd = cm_node->tcp_cntxt.rcv_wnd;
1710 cm_node->tcp_cntxt.snd_wscale = loopbackremotenode->tcp_cntxt.rcv_wscale;
1711 loopbackremotenode->tcp_cntxt.snd_wscale = cm_node->tcp_cntxt.rcv_wscale;
1713 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
1718 /* set our node side to client (active) side */
1719 cm_node->tcp_cntxt.client = 1;
1720 /* init our MPA frame ptr */
1721 memcpy(&cm_node->mpa_frame, mpa_frame, mpa_frame_size);
1722 cm_node->mpa_frame_size = mpa_frame_size;
1724 /* send a syn and goto syn sent state */
1725 cm_node->state = NES_CM_STATE_SYN_SENT;
1726 ret = send_syn(cm_node, 0);
1728 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X, port=0x%04x,"
1729 " cm_node=%p, cm_id = %p.\n",
1730 cm_node->rem_addr, cm_node->rem_port, cm_node, cm_node->cm_id);
1737 * mini_cm_accept - accept a connection
1738 * This function is never called
1740 static int mini_cm_accept(struct nes_cm_core *cm_core, struct ietf_mpa_frame *mpa_frame,
1741 struct nes_cm_node *cm_node)
1748 * mini_cm_reject - reject and teardown a connection
1750 static int mini_cm_reject(struct nes_cm_core *cm_core,
1751 struct ietf_mpa_frame *mpa_frame,
1752 struct nes_cm_node *cm_node)
1755 struct sk_buff *skb;
1756 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) +
1757 ntohs(mpa_frame->priv_data_len);
1759 skb = get_free_pkt(cm_node);
1761 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1765 /* send an MPA Request frame */
1766 form_cm_frame(skb, cm_node, NULL, 0, mpa_frame, mpa_frame_size, SET_ACK | SET_FIN);
1767 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1769 cm_node->state = NES_CM_STATE_CLOSED;
1770 ret = send_fin(cm_node, NULL);
1773 printk(KERN_INFO PFX "failed to send MPA Reply (reject)\n");
1784 static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1788 if (!cm_core || !cm_node)
1791 switch (cm_node->state) {
1792 /* if passed in node is null, create a reference key node for node search */
1793 /* check if we found an owner node for this pkt */
1794 case NES_CM_STATE_SYN_RCVD:
1795 case NES_CM_STATE_SYN_SENT:
1796 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
1797 case NES_CM_STATE_ESTABLISHED:
1798 case NES_CM_STATE_ACCEPTING:
1799 case NES_CM_STATE_MPAREQ_SENT:
1800 cm_node->state = NES_CM_STATE_FIN_WAIT1;
1801 send_fin(cm_node, NULL);
1803 case NES_CM_STATE_CLOSE_WAIT:
1804 cm_node->state = NES_CM_STATE_LAST_ACK;
1805 send_fin(cm_node, NULL);
1807 case NES_CM_STATE_FIN_WAIT1:
1808 case NES_CM_STATE_FIN_WAIT2:
1809 case NES_CM_STATE_LAST_ACK:
1810 case NES_CM_STATE_TIME_WAIT:
1811 case NES_CM_STATE_CLOSING:
1814 case NES_CM_STATE_LISTENING:
1815 case NES_CM_STATE_UNKNOWN:
1816 case NES_CM_STATE_INITED:
1817 case NES_CM_STATE_CLOSED:
1818 case NES_CM_STATE_TSA:
1819 ret = rem_ref_cm_node(cm_core, cm_node);
1822 cm_node->cm_id = NULL;
1828 * recv_pkt - recv an ETHERNET packet, and process it through CM
1829 * node state machine
1831 static int mini_cm_recv_pkt(struct nes_cm_core *cm_core, struct nes_vnic *nesvnic,
1832 struct sk_buff *skb)
1834 struct nes_cm_node *cm_node = NULL;
1835 struct nes_cm_listener *listener = NULL;
1837 struct tcphdr *tcph;
1838 struct nes_cm_info nfo;
1841 if (!skb || skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
1846 iph = (struct iphdr *)skb->data;
1847 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
1848 skb_reset_network_header(skb);
1849 skb_set_transport_header(skb, sizeof(*tcph));
1850 skb->len = ntohs(iph->tot_len);
1852 nfo.loc_addr = ntohl(iph->daddr);
1853 nfo.loc_port = ntohs(tcph->dest);
1854 nfo.rem_addr = ntohl(iph->saddr);
1855 nfo.rem_port = ntohs(tcph->source);
1857 nes_debug(NES_DBG_CM, "Received packet: dest=0x%08X:0x%04X src=0x%08X:0x%04X\n",
1858 iph->daddr, tcph->dest, iph->saddr, tcph->source);
1860 /* note: this call is going to increment cm_node ref count */
1861 cm_node = find_node(cm_core,
1862 nfo.rem_port, nfo.rem_addr,
1863 nfo.loc_port, nfo.loc_addr);
1866 listener = find_listener(cm_core, nfo.loc_addr, nfo.loc_port,
1867 NES_CM_LISTENER_ACTIVE_STATE);
1869 nfo.cm_id = listener->cm_id;
1870 nfo.conn_type = listener->conn_type;
1876 cm_node = make_cm_node(cm_core, nesvnic, &nfo, listener);
1878 nes_debug(NES_DBG_CM, "Unable to allocate node\n");
1880 nes_debug(NES_DBG_CM, "unable to allocate node and decrementing listener refcount\n");
1881 atomic_dec(&listener->ref_count);
1887 nes_debug(NES_DBG_CM, "Packet found for unknown port %x refcnt=%d\n",
1888 nfo.loc_port, atomic_read(&cm_node->ref_count));
1890 nes_debug(NES_DBG_CM, "Packet found for unknown port=%d"
1891 " rem_port=%d refcnt=%d\n",
1892 nfo.loc_port, nfo.rem_port, atomic_read(&cm_node->ref_count));
1894 cm_node->tcp_cntxt.rcv_nxt = ntohl(tcph->seq);
1895 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1896 send_reset(cm_node);
1898 rem_ref_cm_node(cm_core, cm_node);
1902 add_ref_cm_node(cm_node);
1903 cm_node->state = NES_CM_STATE_LISTENING;
1906 nes_debug(NES_DBG_CM, "Processing Packet for node %p, data = (%p):\n",
1907 cm_node, skb->data);
1908 process_packet(cm_node, skb, cm_core);
1910 rem_ref_cm_node(cm_core, cm_node);
1913 dev_kfree_skb_any(skb);
1919 * nes_cm_alloc_core - allocate a top level instance of a cm core
1921 static struct nes_cm_core *nes_cm_alloc_core(void)
1925 struct nes_cm_core *cm_core;
1926 struct sk_buff *skb = NULL;
1928 /* setup the CM core */
1929 /* alloc top level core control structure */
1930 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
1934 INIT_LIST_HEAD(&cm_core->connected_nodes);
1935 init_timer(&cm_core->tcp_timer);
1936 cm_core->tcp_timer.function = nes_cm_timer_tick;
1938 cm_core->mtu = NES_CM_DEFAULT_MTU;
1939 cm_core->state = NES_CM_STATE_INITED;
1940 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
1942 atomic_set(&cm_core->events_posted, 0);
1944 /* init the packet lists */
1945 skb_queue_head_init(&cm_core->tx_free_list);
1947 for (i = 0; i < NES_CM_DEFAULT_FRAME_CNT; i++) {
1948 skb = dev_alloc_skb(cm_core->mtu);
1953 /* add 'raw' skb to free frame list */
1954 skb_queue_head(&cm_core->tx_free_list, skb);
1957 cm_core->api = &nes_cm_api;
1959 spin_lock_init(&cm_core->ht_lock);
1960 spin_lock_init(&cm_core->listen_list_lock);
1962 INIT_LIST_HEAD(&cm_core->listen_list.list);
1964 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
1966 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
1967 cm_core->event_wq = create_singlethread_workqueue("nesewq");
1968 cm_core->post_event = nes_cm_post_event;
1969 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
1970 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
1972 print_core(cm_core);
1978 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
1980 static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
1982 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
1989 if (timer_pending(&cm_core->tcp_timer)) {
1990 del_timer(&cm_core->tcp_timer);
1993 destroy_workqueue(cm_core->event_wq);
1994 destroy_workqueue(cm_core->disconn_wq);
1995 nes_debug(NES_DBG_CM, "\n");
2005 static int mini_cm_get(struct nes_cm_core *cm_core)
2007 return cm_core->state;
2014 static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
2019 case NES_CM_SET_PKT_SIZE:
2020 cm_core->mtu = value;
2022 case NES_CM_SET_FREE_PKT_Q_SIZE:
2023 cm_core->free_tx_pkt_max = value;
2026 /* unknown set option */
2035 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2036 * successfully exchanged when this is called
2038 static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2045 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2046 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2047 NES_QPCONTEXT_MISC_DROS);
2049 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2050 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2052 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2054 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2056 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2057 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2059 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2060 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2061 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2063 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2064 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2065 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2067 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2068 nesqp->nesqp_context->ts_recent = 0;
2069 nesqp->nesqp_context->ts_age = 0;
2070 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2071 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2072 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2073 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2074 cm_node->tcp_cntxt.rcv_wscale);
2075 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2076 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2077 nesqp->nesqp_context->srtt = 0;
2078 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2079 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2080 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2081 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2082 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2083 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2085 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2086 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2087 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2088 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2089 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2090 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2091 le32_to_cpu(nesqp->nesqp_context->misc));
2092 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2093 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2094 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2096 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2097 cm_node->state = NES_CM_STATE_TSA;
2106 int nes_cm_disconn(struct nes_qp *nesqp)
2108 unsigned long flags;
2110 spin_lock_irqsave(&nesqp->lock, flags);
2111 if (nesqp->disconn_pending == 0) {
2112 nesqp->disconn_pending++;
2113 spin_unlock_irqrestore(&nesqp->lock, flags);
2114 /* nes_add_ref(&nesqp->ibqp); */
2115 /* init our disconnect work element, to */
2116 INIT_WORK(&nesqp->disconn_work, nes_disconnect_worker);
2118 queue_work(g_cm_core->disconn_wq, &nesqp->disconn_work);
2120 spin_unlock_irqrestore(&nesqp->lock, flags);
2121 nes_rem_ref(&nesqp->ibqp);
2129 * nes_disconnect_worker
2131 static void nes_disconnect_worker(struct work_struct *work)
2133 struct nes_qp *nesqp = container_of(work, struct nes_qp, disconn_work);
2135 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2136 nesqp->last_aeq, nesqp->hwqp.qp_id);
2137 nes_cm_disconn_true(nesqp);
2142 * nes_cm_disconn_true
2144 static int nes_cm_disconn_true(struct nes_qp *nesqp)
2146 unsigned long flags;
2148 struct iw_cm_id *cm_id;
2149 struct iw_cm_event cm_event;
2150 struct nes_vnic *nesvnic;
2152 u8 original_hw_tcp_state;
2153 u8 original_ibqp_state;
2154 u8 issued_disconnect_reset = 0;
2157 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2161 spin_lock_irqsave(&nesqp->lock, flags);
2162 cm_id = nesqp->cm_id;
2163 /* make sure we havent already closed this connection */
2165 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2167 spin_unlock_irqrestore(&nesqp->lock, flags);
2168 nes_rem_ref(&nesqp->ibqp);
2172 nesvnic = to_nesvnic(nesqp->ibqp.device);
2173 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2175 original_hw_tcp_state = nesqp->hw_tcp_state;
2176 original_ibqp_state = nesqp->ibqp_state;
2177 last_ae = nesqp->last_aeq;
2180 nes_debug(NES_DBG_CM, "set ibqp_state=%u\n", nesqp->ibqp_state);
2182 if ((nesqp->cm_id) && (cm_id->event_handler)) {
2183 if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2184 ((original_ibqp_state == IB_QPS_RTS) &&
2185 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2186 atomic_inc(&cm_disconnects);
2187 cm_event.event = IW_CM_EVENT_DISCONNECT;
2188 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET) {
2189 issued_disconnect_reset = 1;
2190 cm_event.status = IW_CM_EVENT_STATUS_RESET;
2191 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event (status reset) for "
2192 " QP%u, cm_id = %p. \n",
2193 nesqp->hwqp.qp_id, cm_id);
2195 cm_event.status = IW_CM_EVENT_STATUS_OK;
2198 cm_event.local_addr = cm_id->local_addr;
2199 cm_event.remote_addr = cm_id->remote_addr;
2200 cm_event.private_data = NULL;
2201 cm_event.private_data_len = 0;
2203 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event for "
2204 " QP%u, SQ Head = %u, SQ Tail = %u. cm_id = %p, refcount = %u.\n",
2206 nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail, cm_id,
2207 atomic_read(&nesqp->refcount));
2209 spin_unlock_irqrestore(&nesqp->lock, flags);
2210 ret = cm_id->event_handler(cm_id, &cm_event);
2212 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2213 spin_lock_irqsave(&nesqp->lock, flags);
2216 nesqp->disconn_pending = 0;
2217 /* There might have been another AE while the lock was released */
2218 original_hw_tcp_state = nesqp->hw_tcp_state;
2219 original_ibqp_state = nesqp->ibqp_state;
2220 last_ae = nesqp->last_aeq;
2222 if ((issued_disconnect_reset == 0) && (nesqp->cm_id) &&
2223 ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2224 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2225 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2226 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2227 atomic_inc(&cm_closes);
2228 nesqp->cm_id = NULL;
2229 nesqp->in_disconnect = 0;
2230 spin_unlock_irqrestore(&nesqp->lock, flags);
2231 nes_disconnect(nesqp, 1);
2233 cm_id->provider_data = nesqp;
2234 /* Send up the close complete event */
2235 cm_event.event = IW_CM_EVENT_CLOSE;
2236 cm_event.status = IW_CM_EVENT_STATUS_OK;
2237 cm_event.provider_data = cm_id->provider_data;
2238 cm_event.local_addr = cm_id->local_addr;
2239 cm_event.remote_addr = cm_id->remote_addr;
2240 cm_event.private_data = NULL;
2241 cm_event.private_data_len = 0;
2243 ret = cm_id->event_handler(cm_id, &cm_event);
2245 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2248 cm_id->rem_ref(cm_id);
2250 spin_lock_irqsave(&nesqp->lock, flags);
2251 if (nesqp->flush_issued == 0) {
2252 nesqp->flush_issued = 1;
2253 spin_unlock_irqrestore(&nesqp->lock, flags);
2254 flush_wqes(nesvnic->nesdev, nesqp, NES_CQP_FLUSH_RQ, 1);
2256 spin_unlock_irqrestore(&nesqp->lock, flags);
2259 /* This reference is from either ModifyQP or the AE processing,
2260 there is still a race here with modifyqp */
2261 nes_rem_ref(&nesqp->ibqp);
2264 cm_id = nesqp->cm_id;
2265 spin_unlock_irqrestore(&nesqp->lock, flags);
2266 /* check to see if the inbound reset beat the outbound reset */
2267 if ((!cm_id) && (last_ae==NES_AEQE_AEID_RESET_SENT)) {
2268 nes_debug(NES_DBG_CM, "QP%u: Decing refcount due to inbound reset"
2269 " beating the outbound reset.\n",
2271 nes_rem_ref(&nesqp->ibqp);
2275 nesqp->disconn_pending = 0;
2276 spin_unlock_irqrestore(&nesqp->lock, flags);
2278 nes_rem_ref(&nesqp->ibqp);
2287 static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
2290 struct nes_vnic *nesvnic;
2291 struct nes_device *nesdev;
2293 nesvnic = to_nesvnic(nesqp->ibqp.device);
2297 nesdev = nesvnic->nesdev;
2299 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2300 atomic_read(&nesvnic->netdev->refcnt));
2302 if (nesqp->active_conn) {
2304 /* indicate this connection is NOT active */
2305 nesqp->active_conn = 0;
2307 /* Need to free the Last Streaming Mode Message */
2308 if (nesqp->ietf_frame) {
2309 pci_free_consistent(nesdev->pcidev,
2310 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2311 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2315 /* close the CM node down if it is still active */
2316 if (nesqp->cm_node) {
2317 nes_debug(NES_DBG_CM, "Call close API\n");
2319 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
2320 nesqp->cm_node = NULL;
2330 int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2334 struct nes_qp *nesqp;
2335 struct nes_vnic *nesvnic;
2336 struct nes_device *nesdev;
2337 struct nes_cm_node *cm_node;
2338 struct nes_adapter *adapter;
2339 struct ib_qp_attr attr;
2340 struct iw_cm_event cm_event;
2341 struct nes_hw_qp_wqe *wqe;
2342 struct nes_v4_quad nes_quad;
2346 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2350 /* get all our handles */
2351 nesqp = to_nesqp(ibqp);
2352 nesvnic = to_nesvnic(nesqp->ibqp.device);
2353 nesdev = nesvnic->nesdev;
2354 adapter = nesdev->nesadapter;
2356 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
2357 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
2359 /* since this is from a listen, we were able to put node handle into cm_id */
2360 cm_node = (struct nes_cm_node *)cm_id->provider_data;
2362 /* associate the node with the QP */
2363 nesqp->cm_node = (void *)cm_node;
2365 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu\n",
2366 nesqp->hwqp.qp_id, cm_node, jiffies);
2367 atomic_inc(&cm_accepts);
2369 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2370 atomic_read(&nesvnic->netdev->refcnt));
2372 /* allocate the ietf frame and space for private data */
2373 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2374 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2375 &nesqp->ietf_frame_pbase);
2377 if (!nesqp->ietf_frame) {
2378 nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
2383 /* setup the MPA frame */
2384 nesqp->private_data_len = conn_param->private_data_len;
2385 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
2387 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2388 conn_param->private_data_len);
2390 nesqp->ietf_frame->priv_data_len = cpu_to_be16(conn_param->private_data_len);
2391 nesqp->ietf_frame->rev = mpa_version;
2392 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
2394 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2395 wqe = &nesqp->hwqp.sq_vbase[0];
2397 if (cm_id->remote_addr.sin_addr.s_addr != cm_id->local_addr.sin_addr.s_addr) {
2398 u64temp = (unsigned long)nesqp;
2399 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2400 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2402 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2403 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING | NES_IWARP_SQ_WQE_WRPDU);
2404 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2405 cpu_to_le32(conn_param->private_data_len + sizeof(struct ietf_mpa_frame));
2406 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] =
2407 cpu_to_le32((u32)nesqp->ietf_frame_pbase);
2408 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] =
2409 cpu_to_le32((u32)((u64)nesqp->ietf_frame_pbase >> 32));
2410 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2411 cpu_to_le32(conn_param->private_data_len + sizeof(struct ietf_mpa_frame));
2412 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
2414 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
2415 NES_QPCONTEXT_ORDIRD_LSMM_PRESENT | NES_QPCONTEXT_ORDIRD_WRPDU);
2417 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2418 NES_QPCONTEXT_ORDIRD_WRPDU | NES_QPCONTEXT_ORDIRD_ALSMM));
2420 nesqp->skip_lsmm = 1;
2423 /* Cache the cm_id in the qp */
2424 nesqp->cm_id = cm_id;
2425 cm_node->cm_id = cm_id;
2427 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2428 cm_id->provider_data = nesqp;
2429 nesqp->active_conn = 0;
2431 nes_cm_init_tsa_conn(nesqp, cm_node);
2433 nesqp->nesqp_context->tcpPorts[0] = cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2434 nesqp->nesqp_context->tcpPorts[1] = cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2435 nesqp->nesqp_context->ip0 = cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
2437 nesqp->nesqp_context->misc2 |= cpu_to_le32(
2438 (u32)PCI_FUNC(nesdev->pcidev->devfn) << NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
2440 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
2441 nes_arp_table(nesdev, le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
2442 NES_ARP_RESOLVE) << 16);
2444 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
2445 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
2447 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2449 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
2450 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2451 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
2453 memset(&nes_quad, 0, sizeof(nes_quad));
2454 nes_quad.DstIpAdrIndex = cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2455 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2456 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2457 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
2459 /* Produce hash key */
2460 crc_value = get_crc_value(&nes_quad);
2461 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
2462 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
2463 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
2465 nesqp->hte_index &= adapter->hte_index_mask;
2466 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2468 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2470 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = 0x%08X:0x%04X,"
2471 " rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + private data length=%zu.\n",
2473 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2474 ntohs(cm_id->remote_addr.sin_port),
2475 ntohl(cm_id->local_addr.sin_addr.s_addr),
2476 ntohs(cm_id->local_addr.sin_port),
2477 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2478 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2479 conn_param->private_data_len+sizeof(struct ietf_mpa_frame));
2481 attr.qp_state = IB_QPS_RTS;
2482 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
2484 /* notify OF layer that accept event was successfull */
2485 cm_id->add_ref(cm_id);
2487 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2488 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2489 cm_event.provider_data = (void *)nesqp;
2490 cm_event.local_addr = cm_id->local_addr;
2491 cm_event.remote_addr = cm_id->remote_addr;
2492 cm_event.private_data = NULL;
2493 cm_event.private_data_len = 0;
2494 ret = cm_id->event_handler(cm_id, &cm_event);
2495 if (cm_node->loopbackpartner) {
2496 cm_node->loopbackpartner->mpa_frame_size = nesqp->private_data_len;
2497 /* copy entire MPA frame to our cm_node's frame */
2498 memcpy(cm_node->loopbackpartner->mpa_frame_buf, nesqp->ietf_frame->priv_data,
2499 nesqp->private_data_len);
2500 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2503 printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
2504 __func__, __LINE__, ret);
2513 int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2515 struct nes_cm_node *cm_node;
2516 struct nes_cm_core *cm_core;
2518 atomic_inc(&cm_rejects);
2519 cm_node = (struct nes_cm_node *) cm_id->provider_data;
2520 cm_core = cm_node->cm_core;
2521 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2523 strcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP);
2524 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2526 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2527 cm_node->mpa_frame.rev = mpa_version;
2528 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2530 cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
2538 * setup and launch cm connect node
2540 int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2543 struct nes_qp *nesqp;
2544 struct nes_vnic *nesvnic;
2545 struct nes_device *nesdev;
2546 struct nes_cm_node *cm_node;
2547 struct nes_cm_info cm_info;
2549 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2552 nesqp = to_nesqp(ibqp);
2555 nesvnic = to_nesvnic(nesqp->ibqp.device);
2558 nesdev = nesvnic->nesdev;
2562 atomic_inc(&cm_connects);
2564 nesqp->ietf_frame = kzalloc(sizeof(struct ietf_mpa_frame) +
2565 conn_param->private_data_len, GFP_KERNEL);
2566 if (!nesqp->ietf_frame)
2569 /* set qp as having an active connection */
2570 nesqp->active_conn = 1;
2572 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = 0x%08X:0x%04X.\n",
2574 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2575 ntohs(cm_id->remote_addr.sin_port),
2576 ntohl(cm_id->local_addr.sin_addr.s_addr),
2577 ntohs(cm_id->local_addr.sin_port));
2579 /* cache the cm_id in the qp */
2580 nesqp->cm_id = cm_id;
2582 cm_id->provider_data = nesqp;
2584 /* copy the private data */
2585 if (conn_param->private_data_len) {
2586 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2587 conn_param->private_data_len);
2590 nesqp->private_data_len = conn_param->private_data_len;
2591 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
2592 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
2593 nes_debug(NES_DBG_CM, "mpa private data len =%u\n", conn_param->private_data_len);
2595 strcpy(&nesqp->ietf_frame->key[0], IEFT_MPA_KEY_REQ);
2596 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
2597 nesqp->ietf_frame->rev = IETF_MPA_VERSION;
2598 nesqp->ietf_frame->priv_data_len = htons(conn_param->private_data_len);
2600 if (cm_id->local_addr.sin_addr.s_addr != cm_id->remote_addr.sin_addr.s_addr)
2601 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
2602 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
2604 /* set up the connection params for the node */
2605 cm_info.loc_addr = (cm_id->local_addr.sin_addr.s_addr);
2606 cm_info.loc_port = (cm_id->local_addr.sin_port);
2607 cm_info.rem_addr = (cm_id->remote_addr.sin_addr.s_addr);
2608 cm_info.rem_port = (cm_id->remote_addr.sin_port);
2609 cm_info.cm_id = cm_id;
2610 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
2612 cm_id->add_ref(cm_id);
2613 nes_add_ref(&nesqp->ibqp);
2615 /* create a connect CM node connection */
2616 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic, nesqp->ietf_frame, &cm_info);
2618 if (cm_id->local_addr.sin_addr.s_addr != cm_id->remote_addr.sin_addr.s_addr)
2619 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
2620 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
2621 nes_rem_ref(&nesqp->ibqp);
2622 kfree(nesqp->ietf_frame);
2623 nesqp->ietf_frame = NULL;
2624 cm_id->rem_ref(cm_id);
2628 cm_node->apbvt_set = 1;
2629 nesqp->cm_node = cm_node;
2638 int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
2640 struct nes_vnic *nesvnic;
2641 struct nes_cm_listener *cm_node;
2642 struct nes_cm_info cm_info;
2643 struct nes_adapter *adapter;
2647 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
2648 cm_id, ntohs(cm_id->local_addr.sin_port));
2650 nesvnic = to_nesvnic(cm_id->device);
2653 adapter = nesvnic->nesdev->nesadapter;
2654 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
2655 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
2657 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
2658 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
2660 /* setup listen params in our api call struct */
2661 cm_info.loc_addr = nesvnic->local_ipaddr;
2662 cm_info.loc_port = cm_id->local_addr.sin_port;
2663 cm_info.backlog = backlog;
2664 cm_info.cm_id = cm_id;
2666 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
2669 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
2671 printk("%s[%u] Error returned from listen API call\n",
2672 __func__, __LINE__);
2676 cm_id->provider_data = cm_node;
2678 if (!cm_node->reused_node) {
2679 err = nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
2680 PCI_FUNC(nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
2682 printk("nes_manage_apbvt call returned %d.\n", err);
2683 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
2686 cm_listens_created++;
2689 cm_id->add_ref(cm_id);
2690 cm_id->provider_data = (void *)cm_node;
2698 * nes_destroy_listen
2700 int nes_destroy_listen(struct iw_cm_id *cm_id)
2702 if (cm_id->provider_data)
2703 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
2705 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
2707 cm_id->rem_ref(cm_id);
2716 int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
2718 cm_packets_received++;
2719 if ((g_cm_core) && (g_cm_core->api)) {
2720 g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
2722 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
2723 " cm is not setup properly.\n");
2732 * Start and init a cm core module
2734 int nes_cm_start(void)
2736 nes_debug(NES_DBG_CM, "\n");
2737 /* create the primary CM core, pass this handle to subsequent core inits */
2738 g_cm_core = nes_cm_alloc_core();
2749 * stop and dealloc all cm core instances
2751 int nes_cm_stop(void)
2753 g_cm_core->api->destroy_cm_core(g_cm_core);
2759 * cm_event_connected
2760 * handle a connected event, setup QPs and HW
2762 static void cm_event_connected(struct nes_cm_event *event)
2765 struct nes_qp *nesqp;
2766 struct nes_vnic *nesvnic;
2767 struct nes_device *nesdev;
2768 struct nes_cm_node *cm_node;
2769 struct nes_adapter *nesadapter;
2770 struct ib_qp_attr attr;
2771 struct iw_cm_id *cm_id;
2772 struct iw_cm_event cm_event;
2773 struct nes_hw_qp_wqe *wqe;
2774 struct nes_v4_quad nes_quad;
2778 /* get all our handles */
2779 cm_node = event->cm_node;
2780 cm_id = cm_node->cm_id;
2781 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
2782 nesqp = (struct nes_qp *)cm_id->provider_data;
2783 nesvnic = to_nesvnic(nesqp->ibqp.device);
2784 nesdev = nesvnic->nesdev;
2785 nesadapter = nesdev->nesadapter;
2787 if (nesqp->destroyed) {
2790 atomic_inc(&cm_connecteds);
2791 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
2792 " local port 0x%04X. jiffies = %lu.\n",
2794 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2795 ntohs(cm_id->remote_addr.sin_port),
2796 ntohs(cm_id->local_addr.sin_port),
2799 nes_cm_init_tsa_conn(nesqp, cm_node);
2801 /* set the QP tsa context */
2802 nesqp->nesqp_context->tcpPorts[0] = cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2803 nesqp->nesqp_context->tcpPorts[1] = cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2804 nesqp->nesqp_context->ip0 = cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
2806 nesqp->nesqp_context->misc2 |= cpu_to_le32(
2807 (u32)PCI_FUNC(nesdev->pcidev->devfn) << NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
2808 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
2809 nes_arp_table(nesdev, le32_to_cpu(nesqp->nesqp_context->ip0),
2810 NULL, NES_ARP_RESOLVE) << 16);
2811 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
2812 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
2813 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2814 nesqp->nesqp_context->ird_ord_sizes |=
2815 cpu_to_le32((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
2817 /* Adjust tail for not having a LSMM */
2818 nesqp->hwqp.sq_tail = 1;
2820 #if defined(NES_SEND_FIRST_WRITE)
2821 if (cm_node->send_write0) {
2822 nes_debug(NES_DBG_CM, "Sending first write.\n");
2823 wqe = &nesqp->hwqp.sq_vbase[0];
2824 u64temp = (unsigned long)nesqp;
2825 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2826 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2828 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
2829 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
2830 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
2831 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
2832 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
2833 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
2835 /* use the reserved spot on the WQ for the extra first WQE */
2836 nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2837 NES_QPCONTEXT_ORDIRD_WRPDU | NES_QPCONTEXT_ORDIRD_ALSMM));
2838 nesqp->skip_lsmm = 1;
2839 nesqp->hwqp.sq_tail = 0;
2840 nes_write32(nesdev->regs + NES_WQE_ALLOC,
2841 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
2845 memset(&nes_quad, 0, sizeof(nes_quad));
2847 nes_quad.DstIpAdrIndex = cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2848 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2849 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2850 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
2852 /* Produce hash key */
2853 crc_value = get_crc_value(&nes_quad);
2854 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
2855 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
2856 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
2858 nesqp->hte_index &= nesadapter->hte_index_mask;
2859 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2861 nesqp->ietf_frame = &cm_node->mpa_frame;
2862 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
2863 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2865 /* modify QP state to rts */
2866 attr.qp_state = IB_QPS_RTS;
2867 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
2869 /* notify OF layer we successfully created the requested connection */
2870 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
2871 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2872 cm_event.provider_data = cm_id->provider_data;
2873 cm_event.local_addr.sin_family = AF_INET;
2874 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
2875 cm_event.remote_addr = cm_id->remote_addr;
2877 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
2878 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
2880 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
2881 ret = cm_id->event_handler(cm_id, &cm_event);
2882 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2885 printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
2886 __func__, __LINE__, ret);
2887 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = %lu\n",
2888 nesqp->hwqp.qp_id, jiffies );
2890 nes_rem_ref(&nesqp->ibqp);
2897 * cm_event_connect_error
2899 static void cm_event_connect_error(struct nes_cm_event *event)
2901 struct nes_qp *nesqp;
2902 struct iw_cm_id *cm_id;
2903 struct iw_cm_event cm_event;
2904 /* struct nes_cm_info cm_info; */
2907 if (!event->cm_node)
2910 cm_id = event->cm_node->cm_id;
2915 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
2916 nesqp = cm_id->provider_data;
2922 /* notify OF layer about this connection error event */
2923 /* cm_id->rem_ref(cm_id); */
2924 nesqp->cm_id = NULL;
2925 cm_id->provider_data = NULL;
2926 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
2927 cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
2928 cm_event.provider_data = cm_id->provider_data;
2929 cm_event.local_addr = cm_id->local_addr;
2930 cm_event.remote_addr = cm_id->remote_addr;
2931 cm_event.private_data = NULL;
2932 cm_event.private_data_len = 0;
2934 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, remove_addr=%08x\n",
2935 cm_event.local_addr.sin_addr.s_addr, cm_event.remote_addr.sin_addr.s_addr);
2937 ret = cm_id->event_handler(cm_id, &cm_event);
2938 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2940 printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
2941 __func__, __LINE__, ret);
2942 nes_rem_ref(&nesqp->ibqp);
2943 cm_id->rem_ref(cm_id);
2952 static void cm_event_reset(struct nes_cm_event *event)
2954 struct nes_qp *nesqp;
2955 struct iw_cm_id *cm_id;
2956 struct iw_cm_event cm_event;
2957 /* struct nes_cm_info cm_info; */
2960 if (!event->cm_node)
2963 if (!event->cm_node->cm_id)
2966 cm_id = event->cm_node->cm_id;
2968 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
2969 nesqp = cm_id->provider_data;
2971 nesqp->cm_id = NULL;
2972 /* cm_id->provider_data = NULL; */
2973 cm_event.event = IW_CM_EVENT_DISCONNECT;
2974 cm_event.status = IW_CM_EVENT_STATUS_RESET;
2975 cm_event.provider_data = cm_id->provider_data;
2976 cm_event.local_addr = cm_id->local_addr;
2977 cm_event.remote_addr = cm_id->remote_addr;
2978 cm_event.private_data = NULL;
2979 cm_event.private_data_len = 0;
2981 ret = cm_id->event_handler(cm_id, &cm_event);
2982 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2985 /* notify OF layer about this connection error event */
2986 cm_id->rem_ref(cm_id);
2995 static void cm_event_mpa_req(struct nes_cm_event *event)
2997 struct iw_cm_id *cm_id;
2998 struct iw_cm_event cm_event;
3000 struct nes_cm_node *cm_node;
3002 cm_node = event->cm_node;
3005 cm_id = cm_node->cm_id;
3007 atomic_inc(&cm_connect_reqs);
3008 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3009 cm_node, cm_id, jiffies);
3011 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3012 cm_event.status = IW_CM_EVENT_STATUS_OK;
3013 cm_event.provider_data = (void *)cm_node;
3015 cm_event.local_addr.sin_family = AF_INET;
3016 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3017 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3019 cm_event.remote_addr.sin_family = AF_INET;
3020 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3021 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3023 cm_event.private_data = cm_node->mpa_frame_buf;
3024 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3026 ret = cm_id->event_handler(cm_id, &cm_event);
3028 printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
3029 __func__, __LINE__, ret);
3035 static void nes_cm_event_handler(struct work_struct *);
3039 * post an event to the cm event handler
3041 static int nes_cm_post_event(struct nes_cm_event *event)
3043 atomic_inc(&event->cm_node->cm_core->events_posted);
3044 add_ref_cm_node(event->cm_node);
3045 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3046 INIT_WORK(&event->event_work, nes_cm_event_handler);
3047 nes_debug(NES_DBG_CM, "queue_work, event=%p\n", event);
3049 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3051 nes_debug(NES_DBG_CM, "Exit\n");
3057 * nes_cm_event_handler
3058 * worker function to handle cm events
3059 * will free instance of nes_cm_event
3061 static void nes_cm_event_handler(struct work_struct *work)
3063 struct nes_cm_event *event = container_of(work, struct nes_cm_event, event_work);
3064 struct nes_cm_core *cm_core;
3066 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core)) {
3069 cm_core = event->cm_node->cm_core;
3070 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
3071 event, event->type, atomic_read(&cm_core->events_posted));
3073 switch (event->type) {
3074 case NES_CM_EVENT_MPA_REQ:
3075 cm_event_mpa_req(event);
3076 nes_debug(NES_DBG_CM, "CM Event: MPA REQUEST\n");
3078 case NES_CM_EVENT_RESET:
3079 nes_debug(NES_DBG_CM, "CM Event: RESET\n");
3080 cm_event_reset(event);
3082 case NES_CM_EVENT_CONNECTED:
3083 if ((!event->cm_node->cm_id) ||
3084 (event->cm_node->state != NES_CM_STATE_TSA)) {
3087 cm_event_connected(event);
3088 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3090 case NES_CM_EVENT_ABORTED:
3091 if ((!event->cm_node->cm_id) || (event->cm_node->state == NES_CM_STATE_TSA)) {
3094 cm_event_connect_error(event);
3095 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3097 case NES_CM_EVENT_DROPPED_PKT:
3098 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3101 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3105 atomic_dec(&cm_core->events_posted);
3106 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3107 rem_ref_cm_node(cm_core, event->cm_node);