blob: dbd9a75474e3385052e9c972fb545fe31035583c [file] [log] [blame]
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001/*
Chien Tungcd6853d2009-03-06 15:12:10 -08002 * Copyright (c) 2006 - 2009 Intel-NE, Inc. All rights reserved.
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34
35#define TCPOPT_TIMESTAMP 8
36
37#include <asm/atomic.h>
38#include <linux/skbuff.h>
39#include <linux/ip.h>
40#include <linux/tcp.h>
41#include <linux/init.h>
42#include <linux/if_arp.h>
Chien Tungf2b2b592008-03-20 19:55:30 -050043#include <linux/if_vlan.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080044#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>
Bob Sharp7191a0a2008-10-03 12:21:19 -070055#include <net/arp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080056#include <net/neighbour.h>
57#include <net/route.h>
58#include <net/ip_fib.h>
59
60#include "nes.h"
61
62u32 cm_packets_sent;
63u32 cm_packets_bounced;
64u32 cm_packets_dropped;
65u32 cm_packets_retrans;
66u32 cm_packets_created;
67u32 cm_packets_received;
68u32 cm_listens_created;
69u32 cm_listens_destroyed;
70u32 cm_backlog_drops;
71atomic_t cm_loopbacks;
72atomic_t cm_nodes_created;
73atomic_t cm_nodes_destroyed;
74atomic_t cm_accel_dropped_pkts;
75atomic_t cm_resets_recvd;
76
Faisal Latif6492cdf2008-07-24 20:50:45 -070077static inline int mini_cm_accelerated(struct nes_cm_core *,
78 struct nes_cm_node *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080079static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070080 struct nes_vnic *, struct nes_cm_info *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080081static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070082static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070083 struct nes_vnic *, u16, void *, struct nes_cm_info *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070084static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -070085static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
86 struct nes_cm_node *);
87static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
88 struct nes_cm_node *);
Faisal Latif4a14f6a2008-11-21 20:50:49 -060089static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070090 struct sk_buff *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070091static int mini_cm_dealloc_core(struct nes_cm_core *);
92static int mini_cm_get(struct nes_cm_core *);
93static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070094
Chien Tung6098d102008-11-21 20:51:01 -060095static void form_cm_frame(struct sk_buff *, struct nes_cm_node *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070096 void *, u32, void *, u32, u8);
Faisal Latif6492cdf2008-07-24 20:50:45 -070097static int add_ref_cm_node(struct nes_cm_node *);
98static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
99
Roland Dreier1a855fb2008-04-16 21:01:09 -0700100static int nes_cm_disconn_true(struct nes_qp *);
101static int nes_cm_post_event(struct nes_cm_event *event);
102static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
103static void nes_disconnect_worker(struct work_struct *work);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700104
105static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800106static int send_mpa_reject(struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700107static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
108static int send_reset(struct nes_cm_node *, struct sk_buff *);
109static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fb2008-04-16 21:01:09 -0700110static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700111static void process_packet(struct nes_cm_node *, struct sk_buff *,
112 struct nes_cm_core *);
113
114static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
115static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
116static void cleanup_retrans_entry(struct nes_cm_node *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800117static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700118static void free_retrans_entry(struct nes_cm_node *cm_node);
119static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
120 struct sk_buff *skb, int optionsize, int passive);
121
122/* CM event handler functions */
123static void cm_event_connected(struct nes_cm_event *);
124static void cm_event_connect_error(struct nes_cm_event *);
125static void cm_event_reset(struct nes_cm_event *);
126static void cm_event_mpa_req(struct nes_cm_event *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800127static void cm_event_mpa_reject(struct nes_cm_event *);
128static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700129
130static void print_core(struct nes_cm_core *core);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800131
132/* External CM API Interface */
133/* instance of function pointers for client API */
134/* set address of this instance to cm_core->cm_ops at cm_core alloc */
135static struct nes_cm_ops nes_cm_api = {
136 mini_cm_accelerated,
137 mini_cm_listen,
138 mini_cm_del_listen,
139 mini_cm_connect,
140 mini_cm_close,
141 mini_cm_accept,
142 mini_cm_reject,
143 mini_cm_recv_pkt,
144 mini_cm_dealloc_core,
145 mini_cm_get,
146 mini_cm_set
147};
148
Roland Dreier1a855fb2008-04-16 21:01:09 -0700149static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800150
151atomic_t cm_connects;
152atomic_t cm_accepts;
153atomic_t cm_disconnects;
154atomic_t cm_closes;
155atomic_t cm_connecteds;
156atomic_t cm_connect_reqs;
157atomic_t cm_rejects;
158
159
160/**
161 * create_event
162 */
163static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
164 enum nes_cm_event_type type)
165{
166 struct nes_cm_event *event;
167
168 if (!cm_node->cm_id)
169 return NULL;
170
171 /* allocate an empty event */
172 event = kzalloc(sizeof(*event), GFP_ATOMIC);
173
174 if (!event)
175 return NULL;
176
177 event->type = type;
178 event->cm_node = cm_node;
179 event->cm_info.rem_addr = cm_node->rem_addr;
180 event->cm_info.loc_addr = cm_node->loc_addr;
181 event->cm_info.rem_port = cm_node->rem_port;
182 event->cm_info.loc_port = cm_node->loc_port;
183 event->cm_info.cm_id = cm_node->cm_id;
184
Faisal Latif6492cdf2008-07-24 20:50:45 -0700185 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
186 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
187 cm_node, event, type, event->cm_info.loc_addr,
188 event->cm_info.loc_port, event->cm_info.rem_addr,
189 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800190
191 nes_cm_post_event(event);
192 return event;
193}
194
195
196/**
197 * send_mpa_request
198 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700199static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800200{
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800201 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700202 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800203 return -1;
204 }
205
206 /* send an MPA Request frame */
207 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
208 cm_node->mpa_frame_size, SET_ACK);
209
Faisal Latif9d5ab132009-03-06 15:15:01 -0800210 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
211}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800212
Faisal Latif9d5ab132009-03-06 15:15:01 -0800213
214
215static int send_mpa_reject(struct nes_cm_node *cm_node)
216{
217 struct sk_buff *skb = NULL;
218
219 skb = dev_alloc_skb(MAX_CM_BUFFER);
220 if (!skb) {
221 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
222 return -ENOMEM;
223 }
224
225 /* send an MPA reject frame */
226 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
227 cm_node->mpa_frame_size, SET_ACK | SET_FIN);
228
229 cm_node->state = NES_CM_STATE_FIN_WAIT1;
230 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800231}
232
233
234/**
235 * recv_mpa - process a received TCP pkt, we are expecting an
236 * IETF MPA frame
237 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800238static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
239 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800240{
241 struct ietf_mpa_frame *mpa_frame;
242
Faisal Latif9d5ab132009-03-06 15:15:01 -0800243 *type = NES_MPA_REQUEST_ACCEPT;
244
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800245 /* assume req frame is in tcp data payload */
246 if (len < sizeof(struct ietf_mpa_frame)) {
247 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800248 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800249 }
250
251 mpa_frame = (struct ietf_mpa_frame *)buffer;
252 cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
253
254 if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
255 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
256 " complete (%x + %x != %x)\n",
Faisal Latif9d5ab132009-03-06 15:15:01 -0800257 cm_node->mpa_frame_size,
258 (u32)sizeof(struct ietf_mpa_frame), len);
259 return -EINVAL;
260 }
261 /* make sure it does not exceed the max size */
262 if (len > MAX_CM_BUFFER) {
263 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
264 " (%x + %x != %x)\n",
265 cm_node->mpa_frame_size,
266 (u32)sizeof(struct ietf_mpa_frame), len);
267 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800268 }
269
270 /* copy entire MPA frame to our cm_node's frame */
271 memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
272 cm_node->mpa_frame_size);
273
Faisal Latif9d5ab132009-03-06 15:15:01 -0800274 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
275 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800276 return 0;
277}
278
279
280/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800281 * form_cm_frame - get a free packet and build empty frame Use
282 * node info to build.
283 */
Chien Tung6098d102008-11-21 20:51:01 -0600284static void form_cm_frame(struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700285 struct nes_cm_node *cm_node, void *options, u32 optionsize,
286 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800287{
288 struct tcphdr *tcph;
289 struct iphdr *iph;
290 struct ethhdr *ethh;
291 u8 *buf;
292 u16 packetsize = sizeof(*iph);
293
294 packetsize += sizeof(*tcph);
295 packetsize += optionsize + datasize;
296
297 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
298
299 skb->len = 0;
300 buf = skb_put(skb, packetsize + ETH_HLEN);
301
302 ethh = (struct ethhdr *) buf;
303 buf += ETH_HLEN;
304
305 iph = (struct iphdr *)buf;
306 buf += sizeof(*iph);
307 tcph = (struct tcphdr *)buf;
308 skb_reset_mac_header(skb);
309 skb_set_network_header(skb, ETH_HLEN);
310 skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
311 buf += sizeof(*tcph);
312
313 skb->ip_summed = CHECKSUM_PARTIAL;
314 skb->protocol = htons(0x800);
315 skb->data_len = 0;
316 skb->mac_len = ETH_HLEN;
317
318 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
319 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
320 ethh->h_proto = htons(0x0800);
321
322 iph->version = IPVERSION;
323 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
324 iph->tos = 0;
325 iph->tot_len = htons(packetsize);
326 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
327
328 iph->frag_off = htons(0x4000);
329 iph->ttl = 0x40;
330 iph->protocol = 0x06; /* IPPROTO_TCP */
331
332 iph->saddr = htonl(cm_node->loc_addr);
333 iph->daddr = htonl(cm_node->rem_addr);
334
335 tcph->source = htons(cm_node->loc_port);
336 tcph->dest = htons(cm_node->rem_port);
337 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
338
339 if (flags & SET_ACK) {
340 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
341 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
342 tcph->ack = 1;
343 } else
344 tcph->ack_seq = 0;
345
346 if (flags & SET_SYN) {
347 cm_node->tcp_cntxt.loc_seq_num++;
348 tcph->syn = 1;
349 } else
Faisal Latif6492cdf2008-07-24 20:50:45 -0700350 cm_node->tcp_cntxt.loc_seq_num += datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800351
Faisal Latif6492cdf2008-07-24 20:50:45 -0700352 if (flags & SET_FIN) {
353 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800354 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700355 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800356
357 if (flags & SET_RST)
358 tcph->rst = 1;
359
360 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
361 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
362 tcph->urg_ptr = 0;
363 if (optionsize)
364 memcpy(buf, options, optionsize);
365 buf += optionsize;
366 if (datasize)
367 memcpy(buf, data, datasize);
368
369 skb_shinfo(skb)->nr_frags = 0;
370 cm_packets_created++;
371
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800372}
373
374
375/**
376 * print_core - dump a cm core
377 */
378static void print_core(struct nes_cm_core *core)
379{
380 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
381 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
382 if (!core)
383 return;
384 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800385
386 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
387
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800388 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
389 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
390
391 nes_debug(NES_DBG_CM, "core : %p \n", core);
392
393 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
394}
395
396
397/**
398 * schedule_nes_timer
399 * note - cm_node needs to be protected before calling this. Encase in:
400 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
401 */
402int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
403 enum nes_timer_type type, int send_retrans,
404 int close_when_complete)
405{
406 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700407 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800408 struct nes_timer_entry *new_send;
409 int ret = 0;
410 u32 was_timer_set;
411
412 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
413 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800414 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800415
416 /* new_send->timetosend = currenttime */
417 new_send->retrycount = NES_DEFAULT_RETRYS;
418 new_send->retranscount = NES_DEFAULT_RETRANS;
419 new_send->skb = skb;
420 new_send->timetosend = jiffies;
421 new_send->type = type;
422 new_send->netdev = cm_node->netdev;
423 new_send->send_retrans = send_retrans;
424 new_send->close_when_complete = close_when_complete;
425
426 if (type == NES_TIMER_TYPE_CLOSE) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700427 new_send->timetosend += (HZ/10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800428 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700429 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800430 WARN_ON(1);
431 return -EINVAL;
432 }
433 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800434 }
435
436 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700437 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800438 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700439 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
440 cm_node->send_entry = new_send;
441 add_ref_cm_node(cm_node);
442 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
443 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800444
445 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
446 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700447 nes_debug(NES_DBG_CM, "Error sending packet %p "
448 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800449 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700450 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800451 } else {
452 cm_packets_sent++;
453 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700454 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800455 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700456 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800457 return ret;
458 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800459 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800460 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800461
462 was_timer_set = timer_pending(&cm_core->tcp_timer);
463
464 if (!was_timer_set) {
465 cm_core->tcp_timer.expires = new_send->timetosend;
466 add_timer(&cm_core->tcp_timer);
467 }
468
469 return ret;
470}
471
Faisal Latif9d5ab132009-03-06 15:15:01 -0800472static void nes_retrans_expired(struct nes_cm_node *cm_node)
473{
474 switch (cm_node->state) {
475 case NES_CM_STATE_SYN_RCVD:
476 case NES_CM_STATE_CLOSING:
477 rem_ref_cm_node(cm_node->cm_core, cm_node);
478 break;
479 case NES_CM_STATE_LAST_ACK:
480 case NES_CM_STATE_FIN_WAIT1:
481 case NES_CM_STATE_MPAREJ_RCVD:
482 send_reset(cm_node, NULL);
483 break;
484 default:
485 create_event(cm_node, NES_CM_EVENT_ABORTED);
486 }
487}
488
489static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
490{
491 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
492 struct iw_cm_id *cm_id = cm_node->cm_id;
493 struct nes_qp *nesqp;
494 unsigned long qplockflags;
495
496 if (!recv_entry)
497 return;
498 nesqp = (struct nes_qp *)recv_entry->skb;
499 if (nesqp) {
500 spin_lock_irqsave(&nesqp->lock, qplockflags);
501 if (nesqp->cm_id) {
502 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
503 "refcount = %d: HIT A "
504 "NES_TIMER_TYPE_CLOSE with something "
505 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
506 atomic_read(&nesqp->refcount));
507 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
508 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
509 nesqp->ibqp_state = IB_QPS_ERR;
510 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
511 nes_cm_disconn(nesqp);
512 } else {
513 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
514 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
515 "refcount = %d: HIT A "
516 "NES_TIMER_TYPE_CLOSE with nothing "
517 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
518 atomic_read(&nesqp->refcount));
519 }
520 } else if (rem_node) {
521 /* TIME_WAIT state */
522 rem_ref_cm_node(cm_node->cm_core, cm_node);
523 }
524 if (cm_node->cm_id)
525 cm_id->rem_ref(cm_id);
526 kfree(recv_entry);
527 cm_node->recv_entry = NULL;
528}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800529
530/**
531 * nes_cm_timer_tick
532 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700533static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800534{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800535 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800536 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800537 struct nes_cm_node *cm_node;
538 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800539 struct list_head *list_core_temp;
540 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800541 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800542 u32 settimer = 0;
543 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800544
Faisal Latif879e5bd2008-11-21 20:50:41 -0600545 struct list_head timer_list;
546 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800547 spin_lock_irqsave(&cm_core->ht_lock, flags);
548
Faisal Latif6492cdf2008-07-24 20:50:45 -0700549 list_for_each_safe(list_node, list_core_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600550 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800551 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800552 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600553 add_ref_cm_node(cm_node);
554 list_add(&cm_node->timer_entry, &timer_list);
555 }
556 }
557 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
558
559 list_for_each_safe(list_node, list_core_temp, &timer_list) {
560 cm_node = container_of(list_node, struct nes_cm_node,
561 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800562 recv_entry = cm_node->recv_entry;
563
564 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700565 if (time_after(recv_entry->timetosend, jiffies)) {
566 if (nexttimeout > recv_entry->timetosend ||
Faisal Latif9d5ab132009-03-06 15:15:01 -0800567 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800568 nexttimeout = recv_entry->timetosend;
569 settimer = 1;
570 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800571 } else
572 handle_recv_entry(cm_node, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800573 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800574
575 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700576 do {
577 send_entry = cm_node->send_entry;
578 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600579 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800580 if (time_after(send_entry->timetosend, jiffies)) {
581 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700582 if ((nexttimeout >
583 send_entry->timetosend) ||
584 !settimer) {
585 nexttimeout =
586 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800587 settimer = 1;
588 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800589 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700590 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800591 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800592 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800593 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700594
595 if ((cm_node->state == NES_CM_STATE_TSA) ||
596 (cm_node->state == NES_CM_STATE_CLOSED)) {
597 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600598 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800599 }
600
Faisal Latif6492cdf2008-07-24 20:50:45 -0700601 if (!send_entry->retranscount ||
602 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800603 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700604 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800605
Faisal Latif6492cdf2008-07-24 20:50:45 -0700606 spin_unlock_irqrestore(
607 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800608 nes_retrans_expired(cm_node);
609 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700610 spin_lock_irqsave(&cm_node->retrans_list_lock,
611 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600612 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800613 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800614 atomic_inc(&send_entry->skb->users);
615 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700616 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
617 "for node %p, jiffies = %lu, time to send = "
618 "%lu, retranscount = %u, send_entry->seq_num = "
619 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
620 "0x%08X\n", send_entry, cm_node, jiffies,
621 send_entry->timetosend,
622 send_entry->retranscount,
623 send_entry->seq_num,
624 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800625
Faisal Latif6492cdf2008-07-24 20:50:45 -0700626 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
627 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800628 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700629 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800630 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700631 nes_debug(NES_DBG_CM, "rexmit failed for "
632 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800633 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800634 send_entry->retrycount--;
635 nexttimeout = jiffies + NES_SHORT_TIME;
636 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600637 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800638 } else {
639 cm_packets_sent++;
640 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700641 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
642 "%u, retry count = %u.\n",
643 send_entry->retranscount,
644 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800645 if (send_entry->send_retrans) {
646 send_entry->retranscount--;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700647 send_entry->timetosend = jiffies +
648 NES_RETRY_TIMEOUT;
649 if (nexttimeout > send_entry->timetosend ||
650 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800651 nexttimeout = send_entry->timetosend;
652 settimer = 1;
653 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800654 } else {
655 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700656 close_when_complete =
657 send_entry->close_when_complete;
658 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
659 cm_node, cm_node->state);
660 free_retrans_entry(cm_node);
661 if (close_when_complete)
662 rem_ref_cm_node(cm_node->cm_core,
663 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800664 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700665 } while (0);
666
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800667 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700668 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800669 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800670
671 if (settimer) {
672 if (!timer_pending(&cm_core->tcp_timer)) {
673 cm_core->tcp_timer.expires = nexttimeout;
674 add_timer(&cm_core->tcp_timer);
675 }
676 }
677}
678
679
680/**
681 * send_syn
682 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700683static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
684 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800685{
686 int ret;
687 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800688 char optionsbuffer[sizeof(struct option_mss) +
Faisal Latif6492cdf2008-07-24 20:50:45 -0700689 sizeof(struct option_windowscale) + sizeof(struct option_base) +
690 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800691
692 int optionssize = 0;
693 /* Sending MSS option */
694 union all_known_options *options;
695
696 if (!cm_node)
697 return -EINVAL;
698
699 options = (union all_known_options *)&optionsbuffer[optionssize];
700 options->as_mss.optionnum = OPTION_NUMBER_MSS;
701 options->as_mss.length = sizeof(struct option_mss);
702 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
703 optionssize += sizeof(struct option_mss);
704
705 options = (union all_known_options *)&optionsbuffer[optionssize];
706 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
707 options->as_windowscale.length = sizeof(struct option_windowscale);
708 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
709 optionssize += sizeof(struct option_windowscale);
710
Faisal Latif6492cdf2008-07-24 20:50:45 -0700711 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800712 options = (union all_known_options *)&optionsbuffer[optionssize];
713 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
714 options->as_base.length = sizeof(struct option_base);
715 optionssize += sizeof(struct option_base);
716 /* we need the size to be a multiple of 4 */
717 options = (union all_known_options *)&optionsbuffer[optionssize];
718 options->as_end = 1;
719 optionssize += 1;
720 options = (union all_known_options *)&optionsbuffer[optionssize];
721 options->as_end = 1;
722 optionssize += 1;
723 }
724
725 options = (union all_known_options *)&optionsbuffer[optionssize];
726 options->as_end = OPTION_NUMBER_END;
727 optionssize += 1;
728
Faisal Latif6492cdf2008-07-24 20:50:45 -0700729 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800730 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800731 if (!skb) {
732 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
733 return -1;
734 }
735
736 if (sendack)
737 flags |= SET_ACK;
738
739 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
740 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
741
742 return ret;
743}
744
745
746/**
747 * send_reset
748 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700749static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800750{
751 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800752 int flags = SET_RST | SET_ACK;
753
Faisal Latif6492cdf2008-07-24 20:50:45 -0700754 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800755 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800756 if (!skb) {
757 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -0800758 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800759 }
760
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800761 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
762 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
763
764 return ret;
765}
766
767
768/**
769 * send_ack
770 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700771static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800772{
773 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700774
775 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800776 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800777
778 if (!skb) {
779 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
780 return -1;
781 }
782
783 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
784 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
785
786 return ret;
787}
788
789
790/**
791 * send_fin
792 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700793static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800794{
795 int ret;
796
797 /* if we didn't get a frame get one */
798 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800799 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800800
801 if (!skb) {
802 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
803 return -1;
804 }
805
806 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
807 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
808
809 return ret;
810}
811
812
813/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800814 * find_node - find a cm node that matches the reference cm node
815 */
816static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
817 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
818{
819 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800820 struct list_head *hte;
821 struct nes_cm_node *cm_node;
822
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800823 /* get a handle on the hte */
824 hte = &cm_core->connected_nodes;
825
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800826 /* walk list and find cm_node associated with this session ID */
827 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700828 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800829 /* compare quad, return node handle if a match */
830 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
831 cm_node->loc_addr, cm_node->loc_port,
832 loc_addr, loc_port,
833 cm_node->rem_addr, cm_node->rem_port,
834 rem_addr, rem_port);
835 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
836 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
837 add_ref_cm_node(cm_node);
838 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
839 return cm_node;
840 }
841 }
842 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
843
844 /* no owner node */
845 return NULL;
846}
847
848
849/**
850 * find_listener - find a cm node listening on this addr-port pair
851 */
852static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
853 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
854{
855 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800856 struct nes_cm_listener *listen_node;
Harvey Harrison03080e52009-01-10 21:45:42 -0800857 __be32 tmp_addr = cpu_to_be32(dst_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800858
859 /* walk list and find cm_node associated with this session ID */
860 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700861 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800862 /* compare node pair, return node handle if a match */
863 if (((listen_node->loc_addr == dst_addr) ||
864 listen_node->loc_addr == 0x00000000) &&
865 (listen_node->loc_port == dst_port) &&
866 (listener_state & listen_node->listener_state)) {
867 atomic_inc(&listen_node->ref_count);
868 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
869 return listen_node;
870 }
871 }
872 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
873
Harvey Harrison63779432008-10-31 00:56:00 -0700874 nes_debug(NES_DBG_CM, "Unable to find listener for %pI4:%x\n",
Harvey Harrison03080e52009-01-10 21:45:42 -0800875 &tmp_addr, dst_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800876
877 /* no listener */
878 return NULL;
879}
880
881
882/**
883 * add_hte_node - add a cm node to the hash table
884 */
885static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
886{
887 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800888 struct list_head *hte;
889
890 if (!cm_node || !cm_core)
891 return -EINVAL;
892
Faisal Latif6492cdf2008-07-24 20:50:45 -0700893 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
894 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800895
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800896 spin_lock_irqsave(&cm_core->ht_lock, flags);
897
898 /* get a handle on the hash table element (list head for this slot) */
899 hte = &cm_core->connected_nodes;
900 list_add_tail(&cm_node->list, hte);
901 atomic_inc(&cm_core->ht_node_cnt);
902
903 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
904
905 return 0;
906}
907
908
909/**
910 * mini_cm_dec_refcnt_listen
911 */
912static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700913 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800914{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800915 int ret = -EINVAL;
916 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800917 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700918 struct list_head *list_pos = NULL;
919 struct list_head *list_temp = NULL;
920 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -0600921 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700922
923 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
924 "refcnt=%d\n", listener, free_hanging_nodes,
925 atomic_read(&listener->ref_count));
926 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -0600927 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700928 if (free_hanging_nodes) {
929 spin_lock_irqsave(&cm_core->ht_lock, flags);
930 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600931 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700932 cm_node = container_of(list_pos, struct nes_cm_node,
933 list);
934 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -0600935 (!cm_node->accelerated)) {
936 add_ref_cm_node(cm_node);
937 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700938 }
939 }
940 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
941 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600942
943 list_for_each_safe(list_pos, list_temp, &reset_list) {
944 cm_node = container_of(list_pos, struct nes_cm_node,
Faisal Latif9d5ab132009-03-06 15:15:01 -0800945 reset_entry);
946 {
947 struct nes_cm_node *loopback = cm_node->loopbackpartner;
948 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
949 rem_ref_cm_node(cm_node->cm_core, cm_node);
950 } else {
951 if (!loopback) {
952 cleanup_retrans_entry(cm_node);
953 err = send_reset(cm_node, NULL);
954 if (err) {
955 cm_node->state =
956 NES_CM_STATE_CLOSED;
957 WARN_ON(1);
958 } else {
959 cm_node->state =
960 NES_CM_STATE_CLOSED;
961 rem_ref_cm_node(
962 cm_node->cm_core,
963 cm_node);
964 }
965 } else {
966 struct nes_cm_event event;
967
968 event.cm_node = loopback;
969 event.cm_info.rem_addr =
970 loopback->rem_addr;
971 event.cm_info.loc_addr =
972 loopback->loc_addr;
973 event.cm_info.rem_port =
974 loopback->rem_port;
975 event.cm_info.loc_port =
976 loopback->loc_port;
977 event.cm_info.cm_id = loopback->cm_id;
978 cm_event_connect_error(&event);
979 loopback->state = NES_CM_STATE_CLOSED;
980
981 event.cm_node = cm_node;
982 event.cm_info.rem_addr =
983 cm_node->rem_addr;
984 event.cm_info.loc_addr =
985 cm_node->loc_addr;
986 event.cm_info.rem_port =
987 cm_node->rem_port;
988 event.cm_info.loc_port =
989 cm_node->loc_port;
990 event.cm_info.cm_id = cm_node->cm_id;
991 cm_event_reset(&event);
992
993 rem_ref_cm_node(cm_node->cm_core,
994 cm_node);
995
996 }
997 }
998 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600999 }
1000
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001001 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1002 if (!atomic_dec_return(&listener->ref_count)) {
1003 list_del(&listener->list);
1004
1005 /* decrement our listen node count */
1006 atomic_dec(&cm_core->listen_node_cnt);
1007
1008 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1009
1010 if (listener->nesvnic) {
1011 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1012 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1013 }
1014
1015 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1016
1017 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001018 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001019 ret = 0;
1020 cm_listens_destroyed++;
1021 } else {
1022 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1023 }
1024 if (listener) {
1025 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1026 nes_debug(NES_DBG_CM, "destroying listener (%p)"
1027 " with non-zero pending accepts=%u\n",
1028 listener, atomic_read(&listener->pend_accepts_cnt));
1029 }
1030
1031 return ret;
1032}
1033
1034
1035/**
1036 * mini_cm_del_listen
1037 */
1038static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1039 struct nes_cm_listener *listener)
1040{
1041 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1042 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1043 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1044}
1045
1046
1047/**
1048 * mini_cm_accelerated
1049 */
1050static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1051 struct nes_cm_node *cm_node)
1052{
1053 u32 was_timer_set;
1054 cm_node->accelerated = 1;
1055
1056 if (cm_node->accept_pend) {
1057 BUG_ON(!cm_node->listener);
1058 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001059 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001060 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1061 }
1062
1063 was_timer_set = timer_pending(&cm_core->tcp_timer);
1064 if (!was_timer_set) {
1065 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1066 add_timer(&cm_core->tcp_timer);
1067 }
1068
1069 return 0;
1070}
1071
1072
1073/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001074 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001075 */
Bob Sharp7191a0a2008-10-03 12:21:19 -07001076static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001077{
1078 struct rtable *rt;
1079 struct flowi fl;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001080 struct neighbour *neigh;
1081 int rc = -1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001082
1083 memset(&fl, 0, sizeof fl);
1084 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1085 if (ip_route_output_key(&init_net, &rt, &fl)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001086 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001087 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001088 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001089 }
1090
Bob Sharp7191a0a2008-10-03 12:21:19 -07001091 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1092 if (neigh) {
1093 if (neigh->nud_state & NUD_VALID) {
1094 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001095 " is %pM, Gateway is 0x%08X \n", dst_ip,
1096 neigh->ha, ntohl(rt->rt_gateway));
Bob Sharp7191a0a2008-10-03 12:21:19 -07001097 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1098 dst_ip, NES_ARP_ADD);
1099 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1100 NES_ARP_RESOLVE);
1101 }
1102 neigh_release(neigh);
1103 }
1104
1105 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1106 neigh_event_send(rt->u.dst.neighbour, NULL);
1107
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001108 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001109 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001110}
1111
1112
1113/**
1114 * make_cm_node - create a new instance of a cm node
1115 */
1116static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1117 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1118 struct nes_cm_listener *listener)
1119{
1120 struct nes_cm_node *cm_node;
1121 struct timespec ts;
1122 int arpindex = 0;
1123 struct nes_device *nesdev;
1124 struct nes_adapter *nesadapter;
1125
1126 /* create an hte and cm_node for this instance */
1127 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1128 if (!cm_node)
1129 return NULL;
1130
1131 /* set our node specific transport info */
1132 cm_node->loc_addr = cm_info->loc_addr;
1133 cm_node->rem_addr = cm_info->rem_addr;
1134 cm_node->loc_port = cm_info->loc_port;
1135 cm_node->rem_port = cm_info->rem_port;
1136 cm_node->send_write0 = send_first;
Harvey Harrison63779432008-10-31 00:56:00 -07001137 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1138 &cm_node->loc_addr, cm_node->loc_port,
1139 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001140 cm_node->listener = listener;
1141 cm_node->netdev = nesvnic->netdev;
1142 cm_node->cm_id = cm_info->cm_id;
1143 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1144
Faisal Latif6492cdf2008-07-24 20:50:45 -07001145 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1146 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001147
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001148 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001149
1150 cm_node->loopbackpartner = NULL;
1151 atomic_set(&cm_node->ref_count, 1);
1152 /* associate our parent CM core */
1153 cm_node->cm_core = cm_core;
1154 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1155 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1156 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1157 NES_CM_DEFAULT_RCV_WND_SCALE;
1158 ts = current_kernel_time();
1159 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1160 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Chien Tungf2b2b592008-03-20 19:55:30 -05001161 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001162 cm_node->tcp_cntxt.rcv_nxt = 0;
1163 /* get a unique session ID , add thread_id to an upcounter to handle race */
1164 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001165 cm_node->conn_type = cm_info->conn_type;
1166 cm_node->apbvt_set = 0;
1167 cm_node->accept_pend = 0;
1168
1169 cm_node->nesvnic = nesvnic;
1170 /* get some device handles, for arp lookup */
1171 nesdev = nesvnic->nesdev;
1172 nesadapter = nesdev->nesadapter;
1173
1174 cm_node->loopbackpartner = NULL;
1175 /* get the mac addr for the remote node */
Chien Tung1ee86552008-11-21 20:51:04 -06001176 if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1177 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
1178 else
1179 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001180 if (arpindex < 0) {
Bob Sharp7191a0a2008-10-03 12:21:19 -07001181 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr);
1182 if (arpindex < 0) {
1183 kfree(cm_node);
1184 return NULL;
1185 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001186 }
1187
1188 /* copy the mac addr to node context */
1189 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001190 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1191 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001192
1193 add_hte_node(cm_core, cm_node);
1194 atomic_inc(&cm_nodes_created);
1195
1196 return cm_node;
1197}
1198
1199
1200/**
1201 * add_ref_cm_node - destroy an instance of a cm node
1202 */
1203static int add_ref_cm_node(struct nes_cm_node *cm_node)
1204{
1205 atomic_inc(&cm_node->ref_count);
1206 return 0;
1207}
1208
1209
1210/**
1211 * rem_ref_cm_node - destroy an instance of a cm node
1212 */
1213static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001214 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001215{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001216 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001217 struct nes_qp *nesqp;
1218
1219 if (!cm_node)
1220 return -EINVAL;
1221
1222 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1223 if (atomic_dec_return(&cm_node->ref_count)) {
1224 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1225 return 0;
1226 }
1227 list_del(&cm_node->list);
1228 atomic_dec(&cm_core->ht_node_cnt);
1229 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1230
1231 /* if the node is destroyed before connection was accelerated */
1232 if (!cm_node->accelerated && cm_node->accept_pend) {
1233 BUG_ON(!cm_node->listener);
1234 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1235 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1236 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001237 WARN_ON(cm_node->send_entry);
1238 if (cm_node->recv_entry)
1239 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001240 if (cm_node->listener) {
1241 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1242 } else {
1243 if (cm_node->apbvt_set && cm_node->nesvnic) {
1244 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001245 PCI_FUNC(
1246 cm_node->nesvnic->nesdev->pcidev->devfn),
1247 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001248 }
1249 }
1250
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001251 atomic_dec(&cm_core->node_cnt);
1252 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001253 nesqp = cm_node->nesqp;
1254 if (nesqp) {
1255 nesqp->cm_node = NULL;
1256 nes_rem_ref(&nesqp->ibqp);
1257 cm_node->nesqp = NULL;
1258 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001259
Faisal Latif6492cdf2008-07-24 20:50:45 -07001260 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001261 return 0;
1262}
1263
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001264/**
1265 * process_options
1266 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001267static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1268 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001269{
1270 u32 tmp;
1271 u32 offset = 0;
1272 union all_known_options *all_options;
1273 char got_mss_option = 0;
1274
1275 while (offset < optionsize) {
1276 all_options = (union all_known_options *)(optionsloc + offset);
1277 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001278 case OPTION_NUMBER_END:
1279 offset = optionsize;
1280 break;
1281 case OPTION_NUMBER_NONE:
1282 offset += 1;
1283 continue;
1284 case OPTION_NUMBER_MSS:
1285 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1286 "Size: %d\n", __func__,
1287 all_options->as_mss.length, offset, optionsize);
1288 got_mss_option = 1;
1289 if (all_options->as_mss.length != 4) {
1290 return 1;
1291 } else {
1292 tmp = ntohs(all_options->as_mss.mss);
1293 if (tmp > 0 && tmp <
1294 cm_node->tcp_cntxt.mss)
1295 cm_node->tcp_cntxt.mss = tmp;
1296 }
1297 break;
1298 case OPTION_NUMBER_WINDOW_SCALE:
1299 cm_node->tcp_cntxt.snd_wscale =
1300 all_options->as_windowscale.shiftcount;
1301 break;
1302 case OPTION_NUMBER_WRITE0:
1303 cm_node->send_write0 = 1;
1304 break;
1305 default:
1306 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1307 all_options->as_base.optionnum);
1308 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001309 }
1310 offset += all_options->as_base.length;
1311 }
1312 if ((!got_mss_option) && (syn_packet))
1313 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1314 return 0;
1315}
1316
Faisal Latif6492cdf2008-07-24 20:50:45 -07001317static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001318{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001319 atomic_inc(&cm_accel_dropped_pkts);
1320 dev_kfree_skb_any(skb);
1321}
1322
Faisal Latif9d5ab132009-03-06 15:15:01 -08001323static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001324{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001325 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1326 "refcnt=%d\n", cm_node, cm_node->state,
1327 atomic_read(&cm_node->ref_count));
1328 cm_node->tcp_cntxt.rcv_nxt++;
1329 cleanup_retrans_entry(cm_node);
1330 switch (cm_node->state) {
1331 case NES_CM_STATE_SYN_RCVD:
1332 case NES_CM_STATE_SYN_SENT:
1333 case NES_CM_STATE_ESTABLISHED:
1334 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001335 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001336 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001337 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001338 break;
1339 case NES_CM_STATE_FIN_WAIT1:
1340 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001341 send_ack(cm_node, NULL);
1342 /* Wait for ACK as this is simultanous close..
1343 * After we receive ACK, do not send anything..
1344 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001345 break;
1346 case NES_CM_STATE_FIN_WAIT2:
1347 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001348 send_ack(cm_node, NULL);
1349 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1350 break;
1351 case NES_CM_STATE_TIME_WAIT:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001352 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001353 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001354 break;
1355 case NES_CM_STATE_TSA:
1356 default:
1357 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1358 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001359 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001360 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001361}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001362
Faisal Latif6492cdf2008-07-24 20:50:45 -07001363
1364static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1365 struct tcphdr *tcph)
1366{
1367
1368 int reset = 0; /* whether to send reset in case of err.. */
Faisal Latif183ecfa2008-11-21 20:50:46 -06001369 int passive_state;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001370 atomic_inc(&cm_resets_recvd);
1371 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1372 " refcnt=%d\n", cm_node, cm_node->state,
1373 atomic_read(&cm_node->ref_count));
1374 cleanup_retrans_entry(cm_node);
1375 switch (cm_node->state) {
1376 case NES_CM_STATE_SYN_SENT:
1377 case NES_CM_STATE_MPAREQ_SENT:
1378 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1379 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1380 cm_node->listener, cm_node->state);
1381 active_open_err(cm_node, skb, reset);
1382 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001383 case NES_CM_STATE_MPAREQ_RCVD:
1384 passive_state = atomic_add_return(1, &cm_node->passive_state);
1385 if (passive_state == NES_SEND_RESET_EVENT)
1386 create_event(cm_node, NES_CM_EVENT_RESET);
1387 cleanup_retrans_entry(cm_node);
1388 cm_node->state = NES_CM_STATE_CLOSED;
1389 dev_kfree_skb_any(skb);
1390 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001391 case NES_CM_STATE_ESTABLISHED:
1392 case NES_CM_STATE_SYN_RCVD:
1393 case NES_CM_STATE_LISTENING:
1394 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1395 passive_open_err(cm_node, skb, reset);
1396 break;
1397 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001398 active_open_err(cm_node, skb, reset);
1399 break;
1400 case NES_CM_STATE_CLOSED:
1401 cleanup_retrans_entry(cm_node);
1402 drop_packet(skb);
1403 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001404 case NES_CM_STATE_TIME_WAIT:
1405 cleanup_retrans_entry(cm_node);
1406 cm_node->state = NES_CM_STATE_CLOSED;
1407 rem_ref_cm_node(cm_node->cm_core, cm_node);
1408 drop_packet(skb);
1409 break;
1410 case NES_CM_STATE_FIN_WAIT1:
1411 cleanup_retrans_entry(cm_node);
1412 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001413 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001414 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001415 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001416 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001417}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001418
Faisal Latif9d5ab132009-03-06 15:15:01 -08001419
1420static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001421{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001422
Faisal Latif9d5ab132009-03-06 15:15:01 -08001423 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001424 int datasize = skb->len;
1425 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001426
1427 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1428 u32 res_type;
1429 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1430 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001431 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001432 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001433 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1434 "cm_node=%p listener=%p state=%d\n", __func__,
1435 __LINE__, cm_node, cm_node->listener,
1436 cm_node->state);
1437 active_open_err(cm_node, skb, 1);
1438 } else {
1439 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001440 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001441 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001442 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001443
1444 switch (cm_node->state) {
1445 case NES_CM_STATE_ESTABLISHED:
1446 if (res_type == NES_MPA_REQUEST_REJECT) {
1447 /*BIG problem as we are receiving the MPA.. So should
1448 * not be REJECT.. This is Passive Open.. We can
1449 * only receive it Reject for Active Open...*/
1450 WARN_ON(1);
1451 }
1452 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1453 type = NES_CM_EVENT_MPA_REQ;
1454 atomic_set(&cm_node->passive_state,
1455 NES_PASSIVE_STATE_INDICATED);
1456 break;
1457 case NES_CM_STATE_MPAREQ_SENT:
1458 if (res_type == NES_MPA_REQUEST_REJECT) {
1459 type = NES_CM_EVENT_MPA_REJECT;
1460 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1461 } else {
1462 type = NES_CM_EVENT_CONNECTED;
1463 cm_node->state = NES_CM_STATE_TSA;
1464 }
1465
1466 break;
1467 default:
1468 WARN_ON(1);
1469 break;
1470 }
1471 dev_kfree_skb_any(skb);
1472 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001473}
1474
1475static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1476{
1477 switch (cm_node->state) {
1478 case NES_CM_STATE_SYN_SENT:
1479 case NES_CM_STATE_MPAREQ_SENT:
1480 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1481 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1482 cm_node->listener, cm_node->state);
1483 active_open_err(cm_node, skb, 1);
1484 break;
1485 case NES_CM_STATE_ESTABLISHED:
1486 case NES_CM_STATE_SYN_RCVD:
1487 passive_open_err(cm_node, skb, 1);
1488 break;
1489 case NES_CM_STATE_TSA:
1490 default:
1491 drop_packet(skb);
1492 }
1493}
1494
1495static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1496 struct sk_buff *skb)
1497{
1498 int err;
1499
1500 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1501 if (err)
1502 active_open_err(cm_node, skb, 1);
1503
1504 return err;
1505}
1506
1507static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1508 struct sk_buff *skb)
1509{
1510 int err = 0;
1511 u32 seq;
1512 u32 ack_seq;
1513 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1514 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1515 u32 rcv_wnd;
1516 seq = ntohl(tcph->seq);
1517 ack_seq = ntohl(tcph->ack_seq);
1518 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1519 if (ack_seq != loc_seq_num)
1520 err = 1;
1521 else if ((seq + rcv_wnd) < rcv_nxt)
1522 err = 1;
1523 if (err) {
1524 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1525 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1526 cm_node->listener, cm_node->state);
1527 indicate_pkt_err(cm_node, skb);
1528 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1529 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1530 rcv_wnd);
1531 }
1532 return err;
1533}
1534
1535/*
1536 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1537 * is created with a listener or it may comein as rexmitted packet which in
1538 * that case will be just dropped.
1539 */
1540
1541static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1542 struct tcphdr *tcph)
1543{
1544 int ret;
1545 u32 inc_sequence;
1546 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001547
1548 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001549 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001550 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001551
Faisal Latif6492cdf2008-07-24 20:50:45 -07001552 switch (cm_node->state) {
1553 case NES_CM_STATE_SYN_SENT:
1554 case NES_CM_STATE_MPAREQ_SENT:
1555 /* Rcvd syn on active open connection*/
1556 active_open_err(cm_node, skb, 1);
1557 break;
1558 case NES_CM_STATE_LISTENING:
1559 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001560 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1561 cm_node->listener->backlog) {
1562 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1563 "pressure \n");
1564 cm_backlog_drops++;
1565 passive_open_err(cm_node, skb, 0);
1566 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001567 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001568 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1569 1);
1570 if (ret) {
1571 passive_open_err(cm_node, skb, 0);
1572 /* drop pkt */
1573 break;
1574 }
1575 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1576 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001577 cm_node->accept_pend = 1;
1578 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1579
Faisal Latif6492cdf2008-07-24 20:50:45 -07001580 cm_node->state = NES_CM_STATE_SYN_RCVD;
1581 send_syn(cm_node, 1, skb);
1582 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001583 case NES_CM_STATE_CLOSED:
1584 cleanup_retrans_entry(cm_node);
1585 send_reset(cm_node, skb);
1586 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001587 case NES_CM_STATE_TSA:
1588 case NES_CM_STATE_ESTABLISHED:
1589 case NES_CM_STATE_FIN_WAIT1:
1590 case NES_CM_STATE_FIN_WAIT2:
1591 case NES_CM_STATE_MPAREQ_RCVD:
1592 case NES_CM_STATE_LAST_ACK:
1593 case NES_CM_STATE_CLOSING:
1594 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001595 default:
1596 drop_packet(skb);
1597 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001598 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001599}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001600
Faisal Latif6492cdf2008-07-24 20:50:45 -07001601static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1602 struct tcphdr *tcph)
1603{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001604
Faisal Latif6492cdf2008-07-24 20:50:45 -07001605 int ret;
1606 u32 inc_sequence;
1607 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001608
Faisal Latif6492cdf2008-07-24 20:50:45 -07001609 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001610 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001611 inc_sequence = ntohl(tcph->seq);
1612 switch (cm_node->state) {
1613 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001614 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001615 /* active open */
1616 if (check_syn(cm_node, tcph, skb))
1617 return;
1618 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1619 /* setup options */
1620 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1621 if (ret) {
1622 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1623 cm_node);
1624 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001625 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001626 cleanup_retrans_entry(cm_node);
1627 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1628 send_mpa_request(cm_node, skb);
1629 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1630 break;
1631 case NES_CM_STATE_MPAREQ_RCVD:
1632 /* passive open, so should not be here */
1633 passive_open_err(cm_node, skb, 1);
1634 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001635 case NES_CM_STATE_LISTENING:
1636 case NES_CM_STATE_CLOSED:
1637 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1638 cleanup_retrans_entry(cm_node);
1639 send_reset(cm_node, skb);
1640 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001641 case NES_CM_STATE_ESTABLISHED:
1642 case NES_CM_STATE_FIN_WAIT1:
1643 case NES_CM_STATE_FIN_WAIT2:
1644 case NES_CM_STATE_LAST_ACK:
1645 case NES_CM_STATE_TSA:
1646 case NES_CM_STATE_CLOSING:
1647 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001648 case NES_CM_STATE_MPAREQ_SENT:
1649 default:
1650 drop_packet(skb);
1651 break;
1652 }
1653}
1654
1655static void handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1656 struct tcphdr *tcph)
1657{
1658 int datasize = 0;
1659 u32 inc_sequence;
1660 u32 rem_seq_ack;
1661 u32 rem_seq;
Faisal Latifabb77252008-11-21 20:50:52 -06001662 int ret;
1663 int optionsize;
Faisal Latifabb77252008-11-21 20:50:52 -06001664 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001665
Faisal Latif6492cdf2008-07-24 20:50:45 -07001666 if (check_seq(cm_node, tcph, skb))
1667 return;
1668
1669 skb_pull(skb, tcph->doff << 2);
1670 inc_sequence = ntohl(tcph->seq);
1671 rem_seq = ntohl(tcph->seq);
1672 rem_seq_ack = ntohl(tcph->ack_seq);
1673 datasize = skb->len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001674 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001675 switch (cm_node->state) {
1676 case NES_CM_STATE_SYN_RCVD:
1677 /* Passive OPEN */
Faisal Latifabb77252008-11-21 20:50:52 -06001678 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1679 if (ret)
1680 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001681 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
Faisal Latifabb77252008-11-21 20:50:52 -06001682 if (cm_node->tcp_cntxt.rem_ack_num !=
1683 cm_node->tcp_cntxt.loc_seq_num) {
1684 nes_debug(NES_DBG_CM, "rem_ack_num != loc_seq_num\n");
1685 cleanup_retrans_entry(cm_node);
1686 send_reset(cm_node, skb);
1687 return;
1688 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001689 cm_node->state = NES_CM_STATE_ESTABLISHED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001690 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001691 if (datasize) {
1692 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001693 handle_rcv_mpa(cm_node, skb);
1694 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001695 dev_kfree_skb_any(skb);
1696 cleanup_retrans_entry(cm_node);
1697 }
1698 break;
1699 case NES_CM_STATE_ESTABLISHED:
1700 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001701 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001702 if (datasize) {
1703 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001704 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001705 } else
1706 drop_packet(skb);
1707 break;
1708 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001709 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001710 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1711 if (datasize) {
1712 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001713 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001714 } else { /* Could be just an ack pkt.. */
1715 cleanup_retrans_entry(cm_node);
1716 dev_kfree_skb_any(skb);
1717 }
1718 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001719 case NES_CM_STATE_LISTENING:
1720 case NES_CM_STATE_CLOSED:
1721 cleanup_retrans_entry(cm_node);
1722 send_reset(cm_node, skb);
1723 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001724 case NES_CM_STATE_LAST_ACK:
1725 cleanup_retrans_entry(cm_node);
1726 cm_node->state = NES_CM_STATE_CLOSED;
1727 cm_node->cm_id->rem_ref(cm_node->cm_id);
1728 case NES_CM_STATE_CLOSING:
1729 cleanup_retrans_entry(cm_node);
1730 rem_ref_cm_node(cm_node->cm_core, cm_node);
1731 drop_packet(skb);
1732 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001733 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001734 cleanup_retrans_entry(cm_node);
1735 drop_packet(skb);
1736 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1737 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001738 case NES_CM_STATE_SYN_SENT:
1739 case NES_CM_STATE_FIN_WAIT2:
1740 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001741 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001742 case NES_CM_STATE_UNKNOWN:
1743 default:
1744 drop_packet(skb);
1745 break;
1746 }
1747}
1748
1749
1750
1751static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1752 struct sk_buff *skb, int optionsize, int passive)
1753{
1754 u8 *optionsloc = (u8 *)&tcph[1];
1755 if (optionsize) {
1756 if (process_options(cm_node, optionsloc, optionsize,
1757 (u32)tcph->syn)) {
1758 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1759 __func__, cm_node);
1760 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06001761 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001762 else
Faisal Latifabb77252008-11-21 20:50:52 -06001763 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001764 return 1;
1765 }
1766 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001767
1768 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1769 cm_node->tcp_cntxt.snd_wscale;
1770
Faisal Latif6492cdf2008-07-24 20:50:45 -07001771 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001772 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001773 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001774}
1775
Faisal Latif6492cdf2008-07-24 20:50:45 -07001776/*
1777 * active_open_err() will send reset() if flag set..
1778 * It will also send ABORT event.
1779 */
1780
1781static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1782 int reset)
1783{
1784 cleanup_retrans_entry(cm_node);
1785 if (reset) {
1786 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1787 "state=%d\n", cm_node, cm_node->state);
1788 add_ref_cm_node(cm_node);
1789 send_reset(cm_node, skb);
1790 } else
1791 dev_kfree_skb_any(skb);
1792
1793 cm_node->state = NES_CM_STATE_CLOSED;
1794 create_event(cm_node, NES_CM_EVENT_ABORTED);
1795}
1796
1797/*
1798 * passive_open_err() will either do a reset() or will free up the skb and
1799 * remove the cm_node.
1800 */
1801
1802static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1803 int reset)
1804{
1805 cleanup_retrans_entry(cm_node);
1806 cm_node->state = NES_CM_STATE_CLOSED;
1807 if (reset) {
1808 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1809 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1810 send_reset(cm_node, skb);
1811 } else {
1812 dev_kfree_skb_any(skb);
1813 rem_ref_cm_node(cm_node->cm_core, cm_node);
1814 }
1815}
1816
1817/*
1818 * free_retrans_entry() routines assumes that the retrans_list_lock has
1819 * been acquired before calling.
1820 */
1821static void free_retrans_entry(struct nes_cm_node *cm_node)
1822{
1823 struct nes_timer_entry *send_entry;
1824 send_entry = cm_node->send_entry;
1825 if (send_entry) {
1826 cm_node->send_entry = NULL;
1827 dev_kfree_skb_any(send_entry->skb);
1828 kfree(send_entry);
1829 rem_ref_cm_node(cm_node->cm_core, cm_node);
1830 }
1831}
1832
1833static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1834{
1835 unsigned long flags;
1836
1837 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1838 free_retrans_entry(cm_node);
1839 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1840}
1841
1842/**
1843 * process_packet
1844 * Returns skb if to be freed, else it will return NULL if already used..
1845 */
1846static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1847 struct nes_cm_core *cm_core)
1848{
1849 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1850 struct tcphdr *tcph = tcp_hdr(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001851 u32 fin_set = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001852 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1853
1854 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1855 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1856 tcph->ack, tcph->rst, tcph->fin);
1857
1858 if (tcph->rst)
1859 pkt_type = NES_PKT_TYPE_RST;
1860 else if (tcph->syn) {
1861 pkt_type = NES_PKT_TYPE_SYN;
1862 if (tcph->ack)
1863 pkt_type = NES_PKT_TYPE_SYNACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001864 } else if (tcph->ack)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001865 pkt_type = NES_PKT_TYPE_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001866 if (tcph->fin)
1867 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001868
1869 switch (pkt_type) {
1870 case NES_PKT_TYPE_SYN:
1871 handle_syn_pkt(cm_node, skb, tcph);
1872 break;
1873 case NES_PKT_TYPE_SYNACK:
1874 handle_synack_pkt(cm_node, skb, tcph);
1875 break;
1876 case NES_PKT_TYPE_ACK:
1877 handle_ack_pkt(cm_node, skb, tcph);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001878 if (fin_set)
1879 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001880 break;
1881 case NES_PKT_TYPE_RST:
1882 handle_rst_pkt(cm_node, skb, tcph);
1883 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001884 default:
1885 drop_packet(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001886 if (fin_set)
1887 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001888 break;
1889 }
1890}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001891
1892/**
1893 * mini_cm_listen - create a listen node with params
1894 */
1895static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001896 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001897{
1898 struct nes_cm_listener *listener;
1899 unsigned long flags;
1900
1901 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1902 cm_info->loc_addr, cm_info->loc_port);
1903
1904 /* cannot have multiple matching listeners */
1905 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1906 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1907 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1908 /* find automatically incs ref count ??? */
1909 atomic_dec(&listener->ref_count);
1910 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1911 return NULL;
1912 }
1913
1914 if (!listener) {
1915 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1916 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1917 if (!listener) {
1918 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1919 return NULL;
1920 }
1921
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001922 listener->loc_addr = htonl(cm_info->loc_addr);
1923 listener->loc_port = htons(cm_info->loc_port);
1924 listener->reused_node = 0;
1925
1926 atomic_set(&listener->ref_count, 1);
1927 }
1928 /* pasive case */
1929 /* find already inc'ed the ref count */
1930 else {
1931 listener->reused_node = 1;
1932 }
1933
1934 listener->cm_id = cm_info->cm_id;
1935 atomic_set(&listener->pend_accepts_cnt, 0);
1936 listener->cm_core = cm_core;
1937 listener->nesvnic = nesvnic;
1938 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001939
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001940 listener->conn_type = cm_info->conn_type;
1941 listener->backlog = cm_info->backlog;
1942 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1943
1944 if (!listener->reused_node) {
1945 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1946 list_add(&listener->list, &cm_core->listen_list.list);
1947 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1948 atomic_inc(&cm_core->listen_node_cnt);
1949 }
1950
1951 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1952 " listener = %p, backlog = %d, cm_id = %p.\n",
1953 cm_info->loc_addr, cm_info->loc_port,
1954 listener, listener->backlog, listener->cm_id);
1955
1956 return listener;
1957}
1958
1959
1960/**
1961 * mini_cm_connect - make a connection node with params
1962 */
Faisal Latif54c86a82008-09-30 14:47:27 -07001963static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001964 struct nes_vnic *nesvnic, u16 private_data_len,
1965 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001966{
1967 int ret = 0;
1968 struct nes_cm_node *cm_node;
1969 struct nes_cm_listener *loopbackremotelistener;
1970 struct nes_cm_node *loopbackremotenode;
1971 struct nes_cm_info loopback_cm_info;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001972 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
1973 struct ietf_mpa_frame *mpa_frame = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001974
1975 /* create a CM connection node */
1976 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
1977 if (!cm_node)
1978 return NULL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001979 mpa_frame = &cm_node->mpa_frame;
1980 strcpy(mpa_frame->key, IEFT_MPA_KEY_REQ);
1981 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
1982 mpa_frame->rev = IETF_MPA_VERSION;
1983 mpa_frame->priv_data_len = htons(private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001984
Glenn Streiff7495ab62008-04-29 13:46:54 -07001985 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001986 cm_node->tcp_cntxt.client = 1;
1987 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1988
1989 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001990 loopbackremotelistener = find_listener(cm_core,
1991 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
1992 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001993 if (loopbackremotelistener == NULL) {
1994 create_event(cm_node, NES_CM_EVENT_ABORTED);
1995 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001996 loopback_cm_info = *cm_info;
1997 loopback_cm_info.loc_port = cm_info->rem_port;
1998 loopback_cm_info.rem_port = cm_info->loc_port;
1999 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002000 loopbackremotenode = make_cm_node(cm_core, nesvnic,
2001 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002002 if (!loopbackremotenode) {
2003 rem_ref_cm_node(cm_node->cm_core, cm_node);
2004 return NULL;
2005 }
2006 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002007 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002008 loopbackremotenode->tcp_cntxt.rcv_wscale =
2009 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002010 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002011 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2012 private_data_len);
2013 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002014
Faisal Latif6492cdf2008-07-24 20:50:45 -07002015 /* we are done handling this state. */
2016 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002017 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002018 cm_node->tcp_cntxt.rcv_nxt =
2019 loopbackremotenode->tcp_cntxt.loc_seq_num;
2020 loopbackremotenode->tcp_cntxt.rcv_nxt =
2021 cm_node->tcp_cntxt.loc_seq_num;
2022 cm_node->tcp_cntxt.max_snd_wnd =
2023 loopbackremotenode->tcp_cntxt.rcv_wnd;
2024 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2025 cm_node->tcp_cntxt.rcv_wnd;
2026 cm_node->tcp_cntxt.snd_wnd =
2027 loopbackremotenode->tcp_cntxt.rcv_wnd;
2028 loopbackremotenode->tcp_cntxt.snd_wnd =
2029 cm_node->tcp_cntxt.rcv_wnd;
2030 cm_node->tcp_cntxt.snd_wscale =
2031 loopbackremotenode->tcp_cntxt.rcv_wscale;
2032 loopbackremotenode->tcp_cntxt.snd_wscale =
2033 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002034 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002035 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2036 }
2037 return cm_node;
2038 }
2039
2040 /* set our node side to client (active) side */
2041 cm_node->tcp_cntxt.client = 1;
2042 /* init our MPA frame ptr */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002043 memcpy(mpa_frame->priv_data, private_data, private_data_len);
2044
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002045 cm_node->mpa_frame_size = mpa_frame_size;
2046
2047 /* send a syn and goto syn sent state */
2048 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002049 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002050
Faisal Latif6492cdf2008-07-24 20:50:45 -07002051 if (ret) {
2052 /* error in sending the syn free up the cm_node struct */
2053 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2054 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2055 cm_node->rem_addr, cm_node->rem_port, cm_node,
2056 cm_node->cm_id);
2057 rem_ref_cm_node(cm_node->cm_core, cm_node);
2058 cm_node = NULL;
2059 }
2060
2061 if (cm_node)
2062 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2063 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2064 cm_node->rem_addr, cm_node->rem_port, cm_node,
2065 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002066
2067 return cm_node;
2068}
2069
2070
2071/**
2072 * mini_cm_accept - accept a connection
2073 * This function is never called
2074 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002075static int mini_cm_accept(struct nes_cm_core *cm_core,
2076 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002077{
2078 return 0;
2079}
2080
2081
2082/**
2083 * mini_cm_reject - reject and teardown a connection
2084 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002085static int mini_cm_reject(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002086 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002087{
2088 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002089 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002090 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002091 struct nes_cm_event event;
2092 struct iw_cm_id *cm_id = cm_node->cm_id;
2093 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002094
Faisal Latif6492cdf2008-07-24 20:50:45 -07002095 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2096 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002097
Faisal Latif6492cdf2008-07-24 20:50:45 -07002098 if (cm_node->tcp_cntxt.client)
2099 return ret;
2100 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002101
Faisal Latif9d5ab132009-03-06 15:15:01 -08002102 if (!loopback) {
2103 passive_state = atomic_add_return(1, &cm_node->passive_state);
2104 if (passive_state == NES_SEND_RESET_EVENT) {
2105 cm_node->state = NES_CM_STATE_CLOSED;
2106 rem_ref_cm_node(cm_core, cm_node);
2107 } else {
2108 ret = send_mpa_reject(cm_node);
2109 if (ret) {
2110 cm_node->state = NES_CM_STATE_CLOSED;
2111 err = send_reset(cm_node, NULL);
2112 if (err)
2113 WARN_ON(1);
2114 } else
2115 cm_id->add_ref(cm_id);
2116 }
2117 } else {
2118 cm_node->cm_id = NULL;
2119 event.cm_node = loopback;
2120 event.cm_info.rem_addr = loopback->rem_addr;
2121 event.cm_info.loc_addr = loopback->loc_addr;
2122 event.cm_info.rem_port = loopback->rem_port;
2123 event.cm_info.loc_port = loopback->loc_port;
2124 event.cm_info.cm_id = loopback->cm_id;
2125 cm_event_mpa_reject(&event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002126 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002127 loopback->state = NES_CM_STATE_CLOSING;
2128
2129 cm_id = loopback->cm_id;
2130 rem_ref_cm_node(cm_core, loopback);
2131 cm_id->rem_ref(cm_id);
2132 }
2133
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002134 return ret;
2135}
2136
2137
2138/**
2139 * mini_cm_close
2140 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002141static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002142{
2143 int ret = 0;
2144
2145 if (!cm_core || !cm_node)
2146 return -EINVAL;
2147
2148 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002149 case NES_CM_STATE_SYN_RCVD:
2150 case NES_CM_STATE_SYN_SENT:
2151 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2152 case NES_CM_STATE_ESTABLISHED:
2153 case NES_CM_STATE_ACCEPTING:
2154 case NES_CM_STATE_MPAREQ_SENT:
2155 case NES_CM_STATE_MPAREQ_RCVD:
2156 cleanup_retrans_entry(cm_node);
2157 send_reset(cm_node, NULL);
2158 break;
2159 case NES_CM_STATE_CLOSE_WAIT:
2160 cm_node->state = NES_CM_STATE_LAST_ACK;
2161 send_fin(cm_node, NULL);
2162 break;
2163 case NES_CM_STATE_FIN_WAIT1:
2164 case NES_CM_STATE_FIN_WAIT2:
2165 case NES_CM_STATE_LAST_ACK:
2166 case NES_CM_STATE_TIME_WAIT:
2167 case NES_CM_STATE_CLOSING:
2168 ret = -1;
2169 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002170 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002171 case NES_CM_STATE_LISTENING:
2172 case NES_CM_STATE_UNKNOWN:
2173 case NES_CM_STATE_INITED:
2174 case NES_CM_STATE_CLOSED:
2175 ret = rem_ref_cm_node(cm_core, cm_node);
2176 break;
2177 case NES_CM_STATE_TSA:
2178 if (cm_node->send_entry)
2179 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2180 "send_entry=%p\n", cm_node->send_entry);
2181 ret = rem_ref_cm_node(cm_core, cm_node);
2182 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002183 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002184 return ret;
2185}
2186
2187
2188/**
2189 * recv_pkt - recv an ETHERNET packet, and process it through CM
2190 * node state machine
2191 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002192static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002193 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002194{
2195 struct nes_cm_node *cm_node = NULL;
2196 struct nes_cm_listener *listener = NULL;
2197 struct iphdr *iph;
2198 struct tcphdr *tcph;
2199 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002200 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002201 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002202
Faisal Latif6492cdf2008-07-24 20:50:45 -07002203 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002204 return 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002205 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002206 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002207 }
2208
2209 iph = (struct iphdr *)skb->data;
2210 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002211
2212 nfo.loc_addr = ntohl(iph->daddr);
2213 nfo.loc_port = ntohs(tcph->dest);
2214 nfo.rem_addr = ntohl(iph->saddr);
2215 nfo.rem_port = ntohs(tcph->source);
2216
Harvey Harrison03080e52009-01-10 21:45:42 -08002217 tmp_daddr = cpu_to_be32(iph->daddr);
2218 tmp_saddr = cpu_to_be32(iph->saddr);
2219
Harvey Harrison63779432008-10-31 00:56:00 -07002220 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002221 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002222
Faisal Latif6492cdf2008-07-24 20:50:45 -07002223 do {
2224 cm_node = find_node(cm_core,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002225 nfo.rem_port, nfo.rem_addr,
2226 nfo.loc_port, nfo.loc_addr);
2227
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002228 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002229 /* Only type of packet accepted are for */
2230 /* the PASSIVE open (syn only) */
2231 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002232 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002233 break;
2234 }
2235 listener = find_listener(cm_core, nfo.loc_addr,
2236 nfo.loc_port,
2237 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002238 if (!listener) {
2239 nfo.cm_id = NULL;
2240 nfo.conn_type = 0;
2241 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2242 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002243 break;
2244 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002245 nfo.cm_id = listener->cm_id;
2246 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002247 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2248 listener);
2249 if (!cm_node) {
2250 nes_debug(NES_DBG_CM, "Unable to allocate "
2251 "node\n");
2252 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002253 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002254 dev_kfree_skb_any(skb);
2255 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002256 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002257 if (!tcph->rst && !tcph->fin) {
2258 cm_node->state = NES_CM_STATE_LISTENING;
2259 } else {
2260 cm_packets_dropped++;
2261 rem_ref_cm_node(cm_core, cm_node);
2262 dev_kfree_skb_any(skb);
2263 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002264 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002265 add_ref_cm_node(cm_node);
2266 } else if (cm_node->state == NES_CM_STATE_TSA) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002267 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002268 atomic_inc(&cm_accel_dropped_pkts);
2269 dev_kfree_skb_any(skb);
2270 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002271 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002272 skb_reset_network_header(skb);
2273 skb_set_transport_header(skb, sizeof(*tcph));
2274 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002275 process_packet(cm_node, skb, cm_core);
2276 rem_ref_cm_node(cm_core, cm_node);
2277 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002278 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002279}
2280
2281
2282/**
2283 * nes_cm_alloc_core - allocate a top level instance of a cm core
2284 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002285static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002286{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002287 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002288
2289 /* setup the CM core */
2290 /* alloc top level core control structure */
2291 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2292 if (!cm_core)
2293 return NULL;
2294
2295 INIT_LIST_HEAD(&cm_core->connected_nodes);
2296 init_timer(&cm_core->tcp_timer);
2297 cm_core->tcp_timer.function = nes_cm_timer_tick;
2298
2299 cm_core->mtu = NES_CM_DEFAULT_MTU;
2300 cm_core->state = NES_CM_STATE_INITED;
2301 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2302
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002303 atomic_set(&cm_core->events_posted, 0);
2304
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002305 cm_core->api = &nes_cm_api;
2306
2307 spin_lock_init(&cm_core->ht_lock);
2308 spin_lock_init(&cm_core->listen_list_lock);
2309
2310 INIT_LIST_HEAD(&cm_core->listen_list.list);
2311
2312 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2313
2314 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2315 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2316 cm_core->post_event = nes_cm_post_event;
2317 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2318 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2319
2320 print_core(cm_core);
2321 return cm_core;
2322}
2323
2324
2325/**
2326 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2327 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002328static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002329{
2330 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2331
2332 if (!cm_core)
2333 return -EINVAL;
2334
2335 barrier();
2336
2337 if (timer_pending(&cm_core->tcp_timer)) {
2338 del_timer(&cm_core->tcp_timer);
2339 }
2340
2341 destroy_workqueue(cm_core->event_wq);
2342 destroy_workqueue(cm_core->disconn_wq);
2343 nes_debug(NES_DBG_CM, "\n");
2344 kfree(cm_core);
2345
2346 return 0;
2347}
2348
2349
2350/**
2351 * mini_cm_get
2352 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002353static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002354{
2355 return cm_core->state;
2356}
2357
2358
2359/**
2360 * mini_cm_set
2361 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002362static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002363{
2364 int ret = 0;
2365
2366 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002367 case NES_CM_SET_PKT_SIZE:
2368 cm_core->mtu = value;
2369 break;
2370 case NES_CM_SET_FREE_PKT_Q_SIZE:
2371 cm_core->free_tx_pkt_max = value;
2372 break;
2373 default:
2374 /* unknown set option */
2375 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002376 }
2377
2378 return ret;
2379}
2380
2381
2382/**
2383 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2384 * successfully exchanged when this is called
2385 */
2386static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2387{
2388 int ret = 0;
2389
2390 if (!nesqp)
2391 return -EINVAL;
2392
2393 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2394 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2395 NES_QPCONTEXT_MISC_DROS);
2396
2397 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2398 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2399
2400 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2401
2402 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2403
2404 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2405 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2406
2407 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2408 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2409 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2410
2411 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2412 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2413 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2414
2415 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2416 nesqp->nesqp_context->ts_recent = 0;
2417 nesqp->nesqp_context->ts_age = 0;
2418 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2419 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2420 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2421 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2422 cm_node->tcp_cntxt.rcv_wscale);
2423 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2424 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2425 nesqp->nesqp_context->srtt = 0;
2426 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2427 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2428 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2429 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2430 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2431 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2432
2433 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2434 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2435 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2436 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2437 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2438 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2439 le32_to_cpu(nesqp->nesqp_context->misc));
2440 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2441 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2442 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2443
2444 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2445 cm_node->state = NES_CM_STATE_TSA;
2446
2447 return ret;
2448}
2449
2450
2451/**
2452 * nes_cm_disconn
2453 */
2454int nes_cm_disconn(struct nes_qp *nesqp)
2455{
2456 unsigned long flags;
2457
2458 spin_lock_irqsave(&nesqp->lock, flags);
2459 if (nesqp->disconn_pending == 0) {
2460 nesqp->disconn_pending++;
2461 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002462 /* init our disconnect work element, to */
2463 INIT_WORK(&nesqp->disconn_work, nes_disconnect_worker);
2464
2465 queue_work(g_cm_core->disconn_wq, &nesqp->disconn_work);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002466 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002467 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002468
2469 return 0;
2470}
2471
2472
2473/**
2474 * nes_disconnect_worker
2475 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002476static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002477{
2478 struct nes_qp *nesqp = container_of(work, struct nes_qp, disconn_work);
2479
2480 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2481 nesqp->last_aeq, nesqp->hwqp.qp_id);
2482 nes_cm_disconn_true(nesqp);
2483}
2484
2485
2486/**
2487 * nes_cm_disconn_true
2488 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002489static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002490{
2491 unsigned long flags;
2492 int ret = 0;
2493 struct iw_cm_id *cm_id;
2494 struct iw_cm_event cm_event;
2495 struct nes_vnic *nesvnic;
2496 u16 last_ae;
2497 u8 original_hw_tcp_state;
2498 u8 original_ibqp_state;
2499 u8 issued_disconnect_reset = 0;
2500
2501 if (!nesqp) {
2502 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2503 return -1;
2504 }
2505
2506 spin_lock_irqsave(&nesqp->lock, flags);
2507 cm_id = nesqp->cm_id;
2508 /* make sure we havent already closed this connection */
2509 if (!cm_id) {
2510 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2511 nesqp->hwqp.qp_id);
2512 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002513 return -1;
2514 }
2515
2516 nesvnic = to_nesvnic(nesqp->ibqp.device);
2517 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2518
2519 original_hw_tcp_state = nesqp->hw_tcp_state;
2520 original_ibqp_state = nesqp->ibqp_state;
2521 last_ae = nesqp->last_aeq;
2522
2523
2524 nes_debug(NES_DBG_CM, "set ibqp_state=%u\n", nesqp->ibqp_state);
2525
2526 if ((nesqp->cm_id) && (cm_id->event_handler)) {
2527 if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2528 ((original_ibqp_state == IB_QPS_RTS) &&
2529 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2530 atomic_inc(&cm_disconnects);
2531 cm_event.event = IW_CM_EVENT_DISCONNECT;
2532 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002533 cm_event.status = IW_CM_EVENT_STATUS_RESET;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002534 nes_debug(NES_DBG_CM, "Generating a CM "
2535 "Disconnect Event (status reset) for "
2536 "QP%u, cm_id = %p. \n",
2537 nesqp->hwqp.qp_id, cm_id);
2538 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002539 cm_event.status = IW_CM_EVENT_STATUS_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002540
2541 cm_event.local_addr = cm_id->local_addr;
2542 cm_event.remote_addr = cm_id->remote_addr;
2543 cm_event.private_data = NULL;
2544 cm_event.private_data_len = 0;
2545
Faisal Latif6492cdf2008-07-24 20:50:45 -07002546 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2547 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2548 "cm_id = %p, refcount = %u.\n",
2549 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2550 nesqp->hwqp.sq_tail, cm_id,
2551 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002552
2553 spin_unlock_irqrestore(&nesqp->lock, flags);
2554 ret = cm_id->event_handler(cm_id, &cm_event);
2555 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002556 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2557 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002558 spin_lock_irqsave(&nesqp->lock, flags);
2559 }
2560
2561 nesqp->disconn_pending = 0;
2562 /* There might have been another AE while the lock was released */
2563 original_hw_tcp_state = nesqp->hw_tcp_state;
2564 original_ibqp_state = nesqp->ibqp_state;
2565 last_ae = nesqp->last_aeq;
2566
2567 if ((issued_disconnect_reset == 0) && (nesqp->cm_id) &&
2568 ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2569 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2570 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2571 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2572 atomic_inc(&cm_closes);
2573 nesqp->cm_id = NULL;
2574 nesqp->in_disconnect = 0;
2575 spin_unlock_irqrestore(&nesqp->lock, flags);
2576 nes_disconnect(nesqp, 1);
2577
2578 cm_id->provider_data = nesqp;
2579 /* Send up the close complete event */
2580 cm_event.event = IW_CM_EVENT_CLOSE;
2581 cm_event.status = IW_CM_EVENT_STATUS_OK;
2582 cm_event.provider_data = cm_id->provider_data;
2583 cm_event.local_addr = cm_id->local_addr;
2584 cm_event.remote_addr = cm_id->remote_addr;
2585 cm_event.private_data = NULL;
2586 cm_event.private_data_len = 0;
2587
2588 ret = cm_id->event_handler(cm_id, &cm_event);
2589 if (ret) {
2590 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2591 }
2592
2593 cm_id->rem_ref(cm_id);
2594
2595 spin_lock_irqsave(&nesqp->lock, flags);
2596 if (nesqp->flush_issued == 0) {
2597 nesqp->flush_issued = 1;
2598 spin_unlock_irqrestore(&nesqp->lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002599 flush_wqes(nesvnic->nesdev, nesqp,
2600 NES_CQP_FLUSH_RQ, 1);
2601 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002602 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002603 } else {
2604 cm_id = nesqp->cm_id;
2605 spin_unlock_irqrestore(&nesqp->lock, flags);
2606 /* check to see if the inbound reset beat the outbound reset */
2607 if ((!cm_id) && (last_ae==NES_AEQE_AEID_RESET_SENT)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002608 nes_debug(NES_DBG_CM, "QP%u: Decing refcount "
2609 "due to inbound reset beating the "
2610 "outbound reset.\n", nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002611 }
2612 }
2613 } else {
2614 nesqp->disconn_pending = 0;
2615 spin_unlock_irqrestore(&nesqp->lock, flags);
2616 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002617
2618 return 0;
2619}
2620
2621
2622/**
2623 * nes_disconnect
2624 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002625static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002626{
2627 int ret = 0;
2628 struct nes_vnic *nesvnic;
2629 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002630 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002631
2632 nesvnic = to_nesvnic(nesqp->ibqp.device);
2633 if (!nesvnic)
2634 return -EINVAL;
2635
2636 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002637 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002638
2639 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2640 atomic_read(&nesvnic->netdev->refcnt));
2641
2642 if (nesqp->active_conn) {
2643
2644 /* indicate this connection is NOT active */
2645 nesqp->active_conn = 0;
2646 } else {
2647 /* Need to free the Last Streaming Mode Message */
2648 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002649 if (nesqp->lsmm_mr)
2650 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002651 pci_free_consistent(nesdev->pcidev,
2652 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2653 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2654 }
2655 }
2656
2657 /* close the CM node down if it is still active */
2658 if (nesqp->cm_node) {
2659 nes_debug(NES_DBG_CM, "Call close API\n");
2660
2661 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002662 }
2663
2664 return ret;
2665}
2666
2667
2668/**
2669 * nes_accept
2670 */
2671int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2672{
2673 u64 u64temp;
2674 struct ib_qp *ibqp;
2675 struct nes_qp *nesqp;
2676 struct nes_vnic *nesvnic;
2677 struct nes_device *nesdev;
2678 struct nes_cm_node *cm_node;
2679 struct nes_adapter *adapter;
2680 struct ib_qp_attr attr;
2681 struct iw_cm_event cm_event;
2682 struct nes_hw_qp_wqe *wqe;
2683 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002684 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002685 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002686 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002687 struct nes_ib_device *nesibdev;
2688 struct ib_mr *ibmr = NULL;
2689 struct ib_phys_buf ibphysbuf;
2690 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002691 u64 tagged_offset;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002692
2693
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002694
2695 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2696 if (!ibqp)
2697 return -EINVAL;
2698
2699 /* get all our handles */
2700 nesqp = to_nesqp(ibqp);
2701 nesvnic = to_nesvnic(nesqp->ibqp.device);
2702 nesdev = nesvnic->nesdev;
2703 adapter = nesdev->nesadapter;
2704
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002705 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002706 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2707 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2708 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002709
2710 /* associate the node with the QP */
2711 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002712 cm_node->nesqp = nesqp;
2713 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002714
Faisal Latif6492cdf2008-07-24 20:50:45 -07002715 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2716 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002717 atomic_inc(&cm_accepts);
2718
2719 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2720 atomic_read(&nesvnic->netdev->refcnt));
2721
Faisal Latif6492cdf2008-07-24 20:50:45 -07002722 /* allocate the ietf frame and space for private data */
2723 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2724 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2725 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002726
Faisal Latif6492cdf2008-07-24 20:50:45 -07002727 if (!nesqp->ietf_frame) {
2728 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2729 "data\n");
2730 return -ENOMEM;
2731 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002732
2733
Faisal Latif6492cdf2008-07-24 20:50:45 -07002734 /* setup the MPA frame */
2735 nesqp->private_data_len = conn_param->private_data_len;
2736 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002737
Faisal Latif6492cdf2008-07-24 20:50:45 -07002738 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2739 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002740
Faisal Latif6492cdf2008-07-24 20:50:45 -07002741 nesqp->ietf_frame->priv_data_len =
2742 cpu_to_be16(conn_param->private_data_len);
2743 nesqp->ietf_frame->rev = mpa_version;
2744 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002745
Faisal Latif6492cdf2008-07-24 20:50:45 -07002746 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2747 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002748
Faisal Latif6492cdf2008-07-24 20:50:45 -07002749 if (cm_id->remote_addr.sin_addr.s_addr !=
2750 cm_id->local_addr.sin_addr.s_addr) {
2751 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002752 nesibdev = nesvnic->nesibdev;
2753 nespd = nesqp->nespd;
2754 ibphysbuf.addr = nesqp->ietf_frame_pbase;
2755 ibphysbuf.size = conn_param->private_data_len +
2756 sizeof(struct ietf_mpa_frame);
Don Wood7a5efb62009-04-08 14:21:02 -07002757 tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002758 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
2759 &ibphysbuf, 1,
2760 IB_ACCESS_LOCAL_WRITE,
Don Wood7a5efb62009-04-08 14:21:02 -07002761 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002762 if (!ibmr) {
2763 nes_debug(NES_DBG_CM, "Unable to register memory region"
2764 "for lSMM for cm_node = %p \n",
2765 cm_node);
2766 return -ENOMEM;
2767 }
2768
2769 ibmr->pd = &nespd->ibpd;
2770 ibmr->device = nespd->ibpd.device;
2771 nesqp->lsmm_mr = ibmr;
2772
Faisal Latif6492cdf2008-07-24 20:50:45 -07002773 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2774 set_wqe_64bit_value(wqe->wqe_words,
2775 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2776 u64temp);
2777 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2778 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2779 NES_IWARP_SQ_WQE_WRPDU);
2780 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2781 cpu_to_le32(conn_param->private_data_len +
2782 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002783 set_wqe_64bit_value(wqe->wqe_words,
2784 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
Don Wood7a5efb62009-04-08 14:21:02 -07002785 (u64)(unsigned long)nesqp->ietf_frame);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002786 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2787 cpu_to_le32(conn_param->private_data_len +
2788 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002789 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002790
Faisal Latif6492cdf2008-07-24 20:50:45 -07002791 nesqp->nesqp_context->ird_ord_sizes |=
2792 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2793 NES_QPCONTEXT_ORDIRD_WRPDU);
2794 } else {
2795 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08002796 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002797 }
2798 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002799
2800
2801 /* Cache the cm_id in the qp */
2802 nesqp->cm_id = cm_id;
2803 cm_node->cm_id = cm_id;
2804
2805 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2806 cm_id->provider_data = nesqp;
2807 nesqp->active_conn = 0;
2808
Faisal Latif6492cdf2008-07-24 20:50:45 -07002809 if (cm_node->state == NES_CM_STATE_TSA)
2810 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2811 cm_node);
2812
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002813 nes_cm_init_tsa_conn(nesqp, cm_node);
2814
Faisal Latif6492cdf2008-07-24 20:50:45 -07002815 nesqp->nesqp_context->tcpPorts[0] =
2816 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2817 nesqp->nesqp_context->tcpPorts[1] =
2818 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2819
2820 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2821 nesqp->nesqp_context->ip0 =
2822 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2823 else
2824 nesqp->nesqp_context->ip0 =
2825 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002826
2827 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002828 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2829 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002830
Faisal Latif6492cdf2008-07-24 20:50:45 -07002831 nesqp->nesqp_context->arp_index_vlan |=
2832 cpu_to_le32(nes_arp_table(nesdev,
2833 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002834 NES_ARP_RESOLVE) << 16);
2835
2836 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002837 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002838
2839 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2840
2841 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002842 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2843 nesqp->nesqp_context->ird_ord_sizes |=
2844 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002845
2846 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002847 nes_quad.DstIpAdrIndex =
2848 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2849 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2850 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2851 else
2852 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2853 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2854 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002855
2856 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06002857 crc_value = get_crc_value(&nes_quad);
2858 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002859 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07002860 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002861
2862 nesqp->hte_index &= adapter->hte_index_mask;
2863 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2864
2865 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2866
Faisal Latif6492cdf2008-07-24 20:50:45 -07002867 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2868 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2869 "private data length=%zu.\n", nesqp->hwqp.qp_id,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002870 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2871 ntohs(cm_id->remote_addr.sin_port),
2872 ntohl(cm_id->local_addr.sin_addr.s_addr),
2873 ntohs(cm_id->local_addr.sin_port),
2874 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2875 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002876 conn_param->private_data_len +
2877 sizeof(struct ietf_mpa_frame));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002878
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002879
Coly Li73ac36e2009-01-07 18:09:16 -08002880 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002881 cm_id->add_ref(cm_id);
2882
2883 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2884 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2885 cm_event.provider_data = (void *)nesqp;
2886 cm_event.local_addr = cm_id->local_addr;
2887 cm_event.remote_addr = cm_id->remote_addr;
2888 cm_event.private_data = NULL;
2889 cm_event.private_data_len = 0;
2890 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002891 attr.qp_state = IB_QPS_RTS;
2892 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002893 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002894 cm_node->loopbackpartner->mpa_frame_size =
2895 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002896 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002897 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2898 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002899 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2900 }
2901 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002902 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2903 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002904
Faisal Latif183ecfa2008-11-21 20:50:46 -06002905 passive_state = atomic_add_return(1, &cm_node->passive_state);
2906 if (passive_state == NES_SEND_RESET_EVENT)
2907 create_event(cm_node, NES_CM_EVENT_RESET);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002908 return 0;
2909}
2910
2911
2912/**
2913 * nes_reject
2914 */
2915int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2916{
2917 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002918 struct nes_cm_node *loopback;
2919
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002920 struct nes_cm_core *cm_core;
2921
2922 atomic_inc(&cm_rejects);
2923 cm_node = (struct nes_cm_node *) cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002924 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002925 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002926 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002927 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2928
Faisal Latif9d5ab132009-03-06 15:15:01 -08002929 if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
2930 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002931
Faisal Latif9d5ab132009-03-06 15:15:01 -08002932 strcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP);
2933 if (loopback) {
2934 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
2935 loopback->mpa_frame.priv_data_len = pdata_len;
2936 loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
2937 pdata_len;
2938 } else {
2939 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2940 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2941 }
2942
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002943 cm_node->mpa_frame.rev = mpa_version;
2944 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2945
Faisal Latif9d5ab132009-03-06 15:15:01 -08002946 return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002947}
2948
2949
2950/**
2951 * nes_connect
2952 * setup and launch cm connect node
2953 */
2954int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2955{
2956 struct ib_qp *ibqp;
2957 struct nes_qp *nesqp;
2958 struct nes_vnic *nesvnic;
2959 struct nes_device *nesdev;
2960 struct nes_cm_node *cm_node;
2961 struct nes_cm_info cm_info;
2962
2963 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2964 if (!ibqp)
2965 return -EINVAL;
2966 nesqp = to_nesqp(ibqp);
2967 if (!nesqp)
2968 return -EINVAL;
2969 nesvnic = to_nesvnic(nesqp->ibqp.device);
2970 if (!nesvnic)
2971 return -EINVAL;
2972 nesdev = nesvnic->nesdev;
2973 if (!nesdev)
2974 return -EINVAL;
2975
Faisal Latif6492cdf2008-07-24 20:50:45 -07002976 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
2977 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
2978 ntohl(nesvnic->local_ipaddr),
2979 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2980 ntohs(cm_id->remote_addr.sin_port),
2981 ntohl(cm_id->local_addr.sin_addr.s_addr),
2982 ntohs(cm_id->local_addr.sin_port));
2983
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002984 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002985 nesqp->active_conn = 1;
2986
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002987 /* cache the cm_id in the qp */
2988 nesqp->cm_id = cm_id;
2989
2990 cm_id->provider_data = nesqp;
2991
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002992 nesqp->private_data_len = conn_param->private_data_len;
2993 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
2994 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002995 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
2996 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002997
Faisal Latif6492cdf2008-07-24 20:50:45 -07002998 if (cm_id->local_addr.sin_addr.s_addr !=
2999 cm_id->remote_addr.sin_addr.s_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003000 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003001 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003002
3003 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003004 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3005 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3006 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3007 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003008 cm_info.cm_id = cm_id;
3009 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3010
3011 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003012
3013 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003014 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3015 conn_param->private_data_len, (void *)conn_param->private_data,
3016 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003017 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003018 if (cm_id->local_addr.sin_addr.s_addr !=
3019 cm_id->remote_addr.sin_addr.s_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003020 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003021 PCI_FUNC(nesdev->pcidev->devfn),
3022 NES_MANAGE_APBVT_DEL);
3023
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003024 cm_id->rem_ref(cm_id);
3025 return -ENOMEM;
3026 }
3027
3028 cm_node->apbvt_set = 1;
3029 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003030 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003031 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003032
3033 return 0;
3034}
3035
3036
3037/**
3038 * nes_create_listen
3039 */
3040int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3041{
3042 struct nes_vnic *nesvnic;
3043 struct nes_cm_listener *cm_node;
3044 struct nes_cm_info cm_info;
3045 struct nes_adapter *adapter;
3046 int err;
3047
3048
3049 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3050 cm_id, ntohs(cm_id->local_addr.sin_port));
3051
3052 nesvnic = to_nesvnic(cm_id->device);
3053 if (!nesvnic)
3054 return -EINVAL;
3055 adapter = nesvnic->nesdev->nesadapter;
3056 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3057 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3058
3059 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3060 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3061
3062 /* setup listen params in our api call struct */
3063 cm_info.loc_addr = nesvnic->local_ipaddr;
3064 cm_info.loc_port = cm_id->local_addr.sin_port;
3065 cm_info.backlog = backlog;
3066 cm_info.cm_id = cm_id;
3067
3068 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3069
3070
3071 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3072 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003073 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003074 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003075 return -ENOMEM;
3076 }
3077
3078 cm_id->provider_data = cm_node;
3079
3080 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003081 err = nes_manage_apbvt(nesvnic,
3082 ntohs(cm_id->local_addr.sin_port),
3083 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3084 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003085 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003086 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3087 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003088 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3089 return err;
3090 }
3091 cm_listens_created++;
3092 }
3093
3094 cm_id->add_ref(cm_id);
3095 cm_id->provider_data = (void *)cm_node;
3096
3097
3098 return 0;
3099}
3100
3101
3102/**
3103 * nes_destroy_listen
3104 */
3105int nes_destroy_listen(struct iw_cm_id *cm_id)
3106{
3107 if (cm_id->provider_data)
3108 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3109 else
3110 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3111
3112 cm_id->rem_ref(cm_id);
3113
3114 return 0;
3115}
3116
3117
3118/**
3119 * nes_cm_recv
3120 */
3121int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3122{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003123 int rc = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003124 cm_packets_received++;
3125 if ((g_cm_core) && (g_cm_core->api)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003126 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003127 } else {
3128 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3129 " cm is not setup properly.\n");
3130 }
3131
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003132 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003133}
3134
3135
3136/**
3137 * nes_cm_start
3138 * Start and init a cm core module
3139 */
3140int nes_cm_start(void)
3141{
3142 nes_debug(NES_DBG_CM, "\n");
3143 /* create the primary CM core, pass this handle to subsequent core inits */
3144 g_cm_core = nes_cm_alloc_core();
3145 if (g_cm_core) {
3146 return 0;
3147 } else {
3148 return -ENOMEM;
3149 }
3150}
3151
3152
3153/**
3154 * nes_cm_stop
3155 * stop and dealloc all cm core instances
3156 */
3157int nes_cm_stop(void)
3158{
3159 g_cm_core->api->destroy_cm_core(g_cm_core);
3160 return 0;
3161}
3162
3163
3164/**
3165 * cm_event_connected
3166 * handle a connected event, setup QPs and HW
3167 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003168static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003169{
3170 u64 u64temp;
3171 struct nes_qp *nesqp;
3172 struct nes_vnic *nesvnic;
3173 struct nes_device *nesdev;
3174 struct nes_cm_node *cm_node;
3175 struct nes_adapter *nesadapter;
3176 struct ib_qp_attr attr;
3177 struct iw_cm_id *cm_id;
3178 struct iw_cm_event cm_event;
3179 struct nes_hw_qp_wqe *wqe;
3180 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003181 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003182 int ret;
3183
3184 /* get all our handles */
3185 cm_node = event->cm_node;
3186 cm_id = cm_node->cm_id;
3187 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3188 nesqp = (struct nes_qp *)cm_id->provider_data;
3189 nesvnic = to_nesvnic(nesqp->ibqp.device);
3190 nesdev = nesvnic->nesdev;
3191 nesadapter = nesdev->nesadapter;
3192
3193 if (nesqp->destroyed) {
3194 return;
3195 }
3196 atomic_inc(&cm_connecteds);
3197 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3198 " local port 0x%04X. jiffies = %lu.\n",
3199 nesqp->hwqp.qp_id,
3200 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3201 ntohs(cm_id->remote_addr.sin_port),
3202 ntohs(cm_id->local_addr.sin_port),
3203 jiffies);
3204
3205 nes_cm_init_tsa_conn(nesqp, cm_node);
3206
3207 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003208 nesqp->nesqp_context->tcpPorts[0] =
3209 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3210 nesqp->nesqp_context->tcpPorts[1] =
3211 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3212 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3213 nesqp->nesqp_context->ip0 =
3214 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3215 else
3216 nesqp->nesqp_context->ip0 =
3217 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003218
3219 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003220 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3221 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003222 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003223 nes_arp_table(nesdev,
3224 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003225 NULL, NES_ARP_RESOLVE) << 16);
3226 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3227 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3228 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3229 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003230 cpu_to_le32((u32)1 <<
3231 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003232
3233 /* Adjust tail for not having a LSMM */
3234 nesqp->hwqp.sq_tail = 1;
3235
3236#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003237 if (cm_node->send_write0) {
3238 nes_debug(NES_DBG_CM, "Sending first write.\n");
3239 wqe = &nesqp->hwqp.sq_vbase[0];
3240 u64temp = (unsigned long)nesqp;
3241 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3242 set_wqe_64bit_value(wqe->wqe_words,
3243 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3244 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3245 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3246 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3247 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3248 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3249 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3250 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003251
Faisal Latif6492cdf2008-07-24 20:50:45 -07003252 /* use the reserved spot on the WQ for the extra first WQE */
3253 nesqp->nesqp_context->ird_ord_sizes &=
3254 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3255 NES_QPCONTEXT_ORDIRD_WRPDU |
3256 NES_QPCONTEXT_ORDIRD_ALSMM));
3257 nesqp->skip_lsmm = 1;
3258 nesqp->hwqp.sq_tail = 0;
3259 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3260 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3261 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003262#endif
3263
3264 memset(&nes_quad, 0, sizeof(nes_quad));
3265
Faisal Latif6492cdf2008-07-24 20:50:45 -07003266 nes_quad.DstIpAdrIndex =
3267 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3268 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3269 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3270 else
3271 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003272 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3273 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3274
3275 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003276 crc_value = get_crc_value(&nes_quad);
3277 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003278 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3279 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3280
3281 nesqp->hte_index &= nesadapter->hte_index_mask;
3282 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3283
3284 nesqp->ietf_frame = &cm_node->mpa_frame;
3285 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3286 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3287
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003288 /* notify OF layer we successfully created the requested connection */
3289 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3290 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3291 cm_event.provider_data = cm_id->provider_data;
3292 cm_event.local_addr.sin_family = AF_INET;
3293 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3294 cm_event.remote_addr = cm_id->remote_addr;
3295
Faisal Latif6492cdf2008-07-24 20:50:45 -07003296 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3297 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003298
3299 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3300 ret = cm_id->event_handler(cm_id, &cm_event);
3301 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3302
3303 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003304 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3305 "ret=%d\n", __func__, __LINE__, ret);
3306 attr.qp_state = IB_QPS_RTS;
3307 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003308
Faisal Latif6492cdf2008-07-24 20:50:45 -07003309 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3310 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003311
3312 return;
3313}
3314
3315
3316/**
3317 * cm_event_connect_error
3318 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003319static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003320{
3321 struct nes_qp *nesqp;
3322 struct iw_cm_id *cm_id;
3323 struct iw_cm_event cm_event;
3324 /* struct nes_cm_info cm_info; */
3325 int ret;
3326
3327 if (!event->cm_node)
3328 return;
3329
3330 cm_id = event->cm_node->cm_id;
3331 if (!cm_id) {
3332 return;
3333 }
3334
3335 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3336 nesqp = cm_id->provider_data;
3337
3338 if (!nesqp) {
3339 return;
3340 }
3341
3342 /* notify OF layer about this connection error event */
3343 /* cm_id->rem_ref(cm_id); */
3344 nesqp->cm_id = NULL;
3345 cm_id->provider_data = NULL;
3346 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3347 cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
3348 cm_event.provider_data = cm_id->provider_data;
3349 cm_event.local_addr = cm_id->local_addr;
3350 cm_event.remote_addr = cm_id->remote_addr;
3351 cm_event.private_data = NULL;
3352 cm_event.private_data_len = 0;
3353
Faisal Latif6492cdf2008-07-24 20:50:45 -07003354 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3355 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3356 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003357
3358 ret = cm_id->event_handler(cm_id, &cm_event);
3359 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3360 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003361 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3362 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003363 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003364
Faisal Latif6492cdf2008-07-24 20:50:45 -07003365 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003366 return;
3367}
3368
3369
3370/**
3371 * cm_event_reset
3372 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003373static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003374{
3375 struct nes_qp *nesqp;
3376 struct iw_cm_id *cm_id;
3377 struct iw_cm_event cm_event;
3378 /* struct nes_cm_info cm_info; */
3379 int ret;
3380
3381 if (!event->cm_node)
3382 return;
3383
3384 if (!event->cm_node->cm_id)
3385 return;
3386
3387 cm_id = event->cm_node->cm_id;
3388
3389 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3390 nesqp = cm_id->provider_data;
3391
3392 nesqp->cm_id = NULL;
3393 /* cm_id->provider_data = NULL; */
3394 cm_event.event = IW_CM_EVENT_DISCONNECT;
3395 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3396 cm_event.provider_data = cm_id->provider_data;
3397 cm_event.local_addr = cm_id->local_addr;
3398 cm_event.remote_addr = cm_id->remote_addr;
3399 cm_event.private_data = NULL;
3400 cm_event.private_data_len = 0;
3401
3402 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003403 cm_id->add_ref(cm_id);
3404 atomic_inc(&cm_closes);
3405 cm_event.event = IW_CM_EVENT_CLOSE;
3406 cm_event.status = IW_CM_EVENT_STATUS_OK;
3407 cm_event.provider_data = cm_id->provider_data;
3408 cm_event.local_addr = cm_id->local_addr;
3409 cm_event.remote_addr = cm_id->remote_addr;
3410 cm_event.private_data = NULL;
3411 cm_event.private_data_len = 0;
3412 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3413 ret = cm_id->event_handler(cm_id, &cm_event);
3414
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003415 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3416
3417
3418 /* notify OF layer about this connection error event */
3419 cm_id->rem_ref(cm_id);
3420
3421 return;
3422}
3423
3424
3425/**
3426 * cm_event_mpa_req
3427 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003428static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003429{
3430 struct iw_cm_id *cm_id;
3431 struct iw_cm_event cm_event;
3432 int ret;
3433 struct nes_cm_node *cm_node;
3434
3435 cm_node = event->cm_node;
3436 if (!cm_node)
3437 return;
3438 cm_id = cm_node->cm_id;
3439
3440 atomic_inc(&cm_connect_reqs);
3441 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3442 cm_node, cm_id, jiffies);
3443
3444 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3445 cm_event.status = IW_CM_EVENT_STATUS_OK;
3446 cm_event.provider_data = (void *)cm_node;
3447
3448 cm_event.local_addr.sin_family = AF_INET;
3449 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3450 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3451
3452 cm_event.remote_addr.sin_family = AF_INET;
3453 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3454 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003455 cm_event.private_data = cm_node->mpa_frame_buf;
3456 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003457
3458 ret = cm_id->event_handler(cm_id, &cm_event);
3459 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003460 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3461 __func__, __LINE__, ret);
3462 return;
3463}
3464
3465
3466static void cm_event_mpa_reject(struct nes_cm_event *event)
3467{
3468 struct iw_cm_id *cm_id;
3469 struct iw_cm_event cm_event;
3470 struct nes_cm_node *cm_node;
3471 int ret;
3472
3473 cm_node = event->cm_node;
3474 if (!cm_node)
3475 return;
3476 cm_id = cm_node->cm_id;
3477
3478 atomic_inc(&cm_connect_reqs);
3479 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3480 cm_node, cm_id, jiffies);
3481
3482 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3483 cm_event.status = -ECONNREFUSED;
3484 cm_event.provider_data = cm_id->provider_data;
3485
3486 cm_event.local_addr.sin_family = AF_INET;
3487 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3488 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3489
3490 cm_event.remote_addr.sin_family = AF_INET;
3491 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3492 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3493
3494 cm_event.private_data = cm_node->mpa_frame_buf;
3495 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3496
3497 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3498 "remove_addr=%08x\n",
3499 cm_event.local_addr.sin_addr.s_addr,
3500 cm_event.remote_addr.sin_addr.s_addr);
3501
3502 ret = cm_id->event_handler(cm_id, &cm_event);
3503 if (ret)
3504 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003505 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003506
3507 return;
3508}
3509
3510
3511static void nes_cm_event_handler(struct work_struct *);
3512
3513/**
3514 * nes_cm_post_event
3515 * post an event to the cm event handler
3516 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003517static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003518{
3519 atomic_inc(&event->cm_node->cm_core->events_posted);
3520 add_ref_cm_node(event->cm_node);
3521 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3522 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003523 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3524 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003525
3526 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3527
3528 nes_debug(NES_DBG_CM, "Exit\n");
3529 return 0;
3530}
3531
3532
3533/**
3534 * nes_cm_event_handler
3535 * worker function to handle cm events
3536 * will free instance of nes_cm_event
3537 */
3538static void nes_cm_event_handler(struct work_struct *work)
3539{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003540 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3541 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003542 struct nes_cm_core *cm_core;
3543
Faisal Latif6492cdf2008-07-24 20:50:45 -07003544 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003545 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003546
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003547 cm_core = event->cm_node->cm_core;
3548 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003549 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003550
3551 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003552 case NES_CM_EVENT_MPA_REQ:
3553 cm_event_mpa_req(event);
3554 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3555 event->cm_node);
3556 break;
3557 case NES_CM_EVENT_RESET:
3558 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3559 event->cm_node);
3560 cm_event_reset(event);
3561 break;
3562 case NES_CM_EVENT_CONNECTED:
3563 if ((!event->cm_node->cm_id) ||
3564 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003565 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003566 cm_event_connected(event);
3567 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3568 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003569 case NES_CM_EVENT_MPA_REJECT:
3570 if ((!event->cm_node->cm_id) ||
3571 (event->cm_node->state == NES_CM_STATE_TSA))
3572 break;
3573 cm_event_mpa_reject(event);
3574 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3575 break;
3576
Faisal Latif6492cdf2008-07-24 20:50:45 -07003577 case NES_CM_EVENT_ABORTED:
3578 if ((!event->cm_node->cm_id) ||
3579 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003580 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003581 cm_event_connect_error(event);
3582 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3583 break;
3584 case NES_CM_EVENT_DROPPED_PKT:
3585 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3586 break;
3587 default:
3588 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3589 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003590 }
3591
3592 atomic_dec(&cm_core->events_posted);
3593 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3594 rem_ref_cm_node(cm_core, event->cm_node);
3595 kfree(event);
3596
3597 return;
3598}