blob: a09caf5b387d7b0b44e4216cc4e4227be2e3b7f8 [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 atomic_dec(&new_send->skb->users);
450 new_send->timetosend = jiffies;
451 } 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++;
634 atomic_dec(&send_entry->skb->users);
635 send_entry->retrycount--;
636 nexttimeout = jiffies + NES_SHORT_TIME;
637 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600638 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800639 } else {
640 cm_packets_sent++;
641 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700642 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
643 "%u, retry count = %u.\n",
644 send_entry->retranscount,
645 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800646 if (send_entry->send_retrans) {
647 send_entry->retranscount--;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700648 send_entry->timetosend = jiffies +
649 NES_RETRY_TIMEOUT;
650 if (nexttimeout > send_entry->timetosend ||
651 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800652 nexttimeout = send_entry->timetosend;
653 settimer = 1;
654 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800655 } else {
656 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700657 close_when_complete =
658 send_entry->close_when_complete;
659 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
660 cm_node, cm_node->state);
661 free_retrans_entry(cm_node);
662 if (close_when_complete)
663 rem_ref_cm_node(cm_node->cm_core,
664 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800665 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700666 } while (0);
667
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800668 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700669 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700670 if (ret != NETDEV_TX_OK) {
671 nes_debug(NES_DBG_CM, "rexmit failed for cm_node=%p\n",
672 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800673 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700674 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800675 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800676
677 if (settimer) {
678 if (!timer_pending(&cm_core->tcp_timer)) {
679 cm_core->tcp_timer.expires = nexttimeout;
680 add_timer(&cm_core->tcp_timer);
681 }
682 }
683}
684
685
686/**
687 * send_syn
688 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700689static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
690 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800691{
692 int ret;
693 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800694 char optionsbuffer[sizeof(struct option_mss) +
Faisal Latif6492cdf2008-07-24 20:50:45 -0700695 sizeof(struct option_windowscale) + sizeof(struct option_base) +
696 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800697
698 int optionssize = 0;
699 /* Sending MSS option */
700 union all_known_options *options;
701
702 if (!cm_node)
703 return -EINVAL;
704
705 options = (union all_known_options *)&optionsbuffer[optionssize];
706 options->as_mss.optionnum = OPTION_NUMBER_MSS;
707 options->as_mss.length = sizeof(struct option_mss);
708 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
709 optionssize += sizeof(struct option_mss);
710
711 options = (union all_known_options *)&optionsbuffer[optionssize];
712 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
713 options->as_windowscale.length = sizeof(struct option_windowscale);
714 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
715 optionssize += sizeof(struct option_windowscale);
716
Faisal Latif6492cdf2008-07-24 20:50:45 -0700717 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800718 options = (union all_known_options *)&optionsbuffer[optionssize];
719 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
720 options->as_base.length = sizeof(struct option_base);
721 optionssize += sizeof(struct option_base);
722 /* we need the size to be a multiple of 4 */
723 options = (union all_known_options *)&optionsbuffer[optionssize];
724 options->as_end = 1;
725 optionssize += 1;
726 options = (union all_known_options *)&optionsbuffer[optionssize];
727 options->as_end = 1;
728 optionssize += 1;
729 }
730
731 options = (union all_known_options *)&optionsbuffer[optionssize];
732 options->as_end = OPTION_NUMBER_END;
733 optionssize += 1;
734
Faisal Latif6492cdf2008-07-24 20:50:45 -0700735 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800736 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800737 if (!skb) {
738 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
739 return -1;
740 }
741
742 if (sendack)
743 flags |= SET_ACK;
744
745 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
746 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
747
748 return ret;
749}
750
751
752/**
753 * send_reset
754 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700755static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800756{
757 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800758 int flags = SET_RST | SET_ACK;
759
Faisal Latif6492cdf2008-07-24 20:50:45 -0700760 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800761 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800762 if (!skb) {
763 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -0800764 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800765 }
766
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800767 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
768 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
769
770 return ret;
771}
772
773
774/**
775 * send_ack
776 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700777static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800778{
779 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700780
781 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800782 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800783
784 if (!skb) {
785 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
786 return -1;
787 }
788
789 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
790 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
791
792 return ret;
793}
794
795
796/**
797 * send_fin
798 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700799static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800800{
801 int ret;
802
803 /* if we didn't get a frame get one */
804 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800805 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800806
807 if (!skb) {
808 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
809 return -1;
810 }
811
812 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
813 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
814
815 return ret;
816}
817
818
819/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800820 * find_node - find a cm node that matches the reference cm node
821 */
822static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
823 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
824{
825 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800826 struct list_head *hte;
827 struct nes_cm_node *cm_node;
828
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800829 /* get a handle on the hte */
830 hte = &cm_core->connected_nodes;
831
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800832 /* walk list and find cm_node associated with this session ID */
833 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700834 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800835 /* compare quad, return node handle if a match */
836 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
837 cm_node->loc_addr, cm_node->loc_port,
838 loc_addr, loc_port,
839 cm_node->rem_addr, cm_node->rem_port,
840 rem_addr, rem_port);
841 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
842 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
843 add_ref_cm_node(cm_node);
844 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
845 return cm_node;
846 }
847 }
848 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
849
850 /* no owner node */
851 return NULL;
852}
853
854
855/**
856 * find_listener - find a cm node listening on this addr-port pair
857 */
858static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
859 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
860{
861 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800862 struct nes_cm_listener *listen_node;
Harvey Harrison03080e52009-01-10 21:45:42 -0800863 __be32 tmp_addr = cpu_to_be32(dst_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800864
865 /* walk list and find cm_node associated with this session ID */
866 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700867 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800868 /* compare node pair, return node handle if a match */
869 if (((listen_node->loc_addr == dst_addr) ||
870 listen_node->loc_addr == 0x00000000) &&
871 (listen_node->loc_port == dst_port) &&
872 (listener_state & listen_node->listener_state)) {
873 atomic_inc(&listen_node->ref_count);
874 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
875 return listen_node;
876 }
877 }
878 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
879
Harvey Harrison63779432008-10-31 00:56:00 -0700880 nes_debug(NES_DBG_CM, "Unable to find listener for %pI4:%x\n",
Harvey Harrison03080e52009-01-10 21:45:42 -0800881 &tmp_addr, dst_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800882
883 /* no listener */
884 return NULL;
885}
886
887
888/**
889 * add_hte_node - add a cm node to the hash table
890 */
891static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
892{
893 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800894 struct list_head *hte;
895
896 if (!cm_node || !cm_core)
897 return -EINVAL;
898
Faisal Latif6492cdf2008-07-24 20:50:45 -0700899 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
900 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800901
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800902 spin_lock_irqsave(&cm_core->ht_lock, flags);
903
904 /* get a handle on the hash table element (list head for this slot) */
905 hte = &cm_core->connected_nodes;
906 list_add_tail(&cm_node->list, hte);
907 atomic_inc(&cm_core->ht_node_cnt);
908
909 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
910
911 return 0;
912}
913
914
915/**
916 * mini_cm_dec_refcnt_listen
917 */
918static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700919 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800920{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800921 int ret = -EINVAL;
922 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800923 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700924 struct list_head *list_pos = NULL;
925 struct list_head *list_temp = NULL;
926 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -0600927 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700928
929 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
930 "refcnt=%d\n", listener, free_hanging_nodes,
931 atomic_read(&listener->ref_count));
932 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -0600933 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700934 if (free_hanging_nodes) {
935 spin_lock_irqsave(&cm_core->ht_lock, flags);
936 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600937 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700938 cm_node = container_of(list_pos, struct nes_cm_node,
939 list);
940 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -0600941 (!cm_node->accelerated)) {
942 add_ref_cm_node(cm_node);
943 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700944 }
945 }
946 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
947 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600948
949 list_for_each_safe(list_pos, list_temp, &reset_list) {
950 cm_node = container_of(list_pos, struct nes_cm_node,
Faisal Latif9d5ab132009-03-06 15:15:01 -0800951 reset_entry);
952 {
953 struct nes_cm_node *loopback = cm_node->loopbackpartner;
954 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
955 rem_ref_cm_node(cm_node->cm_core, cm_node);
956 } else {
957 if (!loopback) {
958 cleanup_retrans_entry(cm_node);
959 err = send_reset(cm_node, NULL);
960 if (err) {
961 cm_node->state =
962 NES_CM_STATE_CLOSED;
963 WARN_ON(1);
964 } else {
965 cm_node->state =
966 NES_CM_STATE_CLOSED;
967 rem_ref_cm_node(
968 cm_node->cm_core,
969 cm_node);
970 }
971 } else {
972 struct nes_cm_event event;
973
974 event.cm_node = loopback;
975 event.cm_info.rem_addr =
976 loopback->rem_addr;
977 event.cm_info.loc_addr =
978 loopback->loc_addr;
979 event.cm_info.rem_port =
980 loopback->rem_port;
981 event.cm_info.loc_port =
982 loopback->loc_port;
983 event.cm_info.cm_id = loopback->cm_id;
984 cm_event_connect_error(&event);
985 loopback->state = NES_CM_STATE_CLOSED;
986
987 event.cm_node = cm_node;
988 event.cm_info.rem_addr =
989 cm_node->rem_addr;
990 event.cm_info.loc_addr =
991 cm_node->loc_addr;
992 event.cm_info.rem_port =
993 cm_node->rem_port;
994 event.cm_info.loc_port =
995 cm_node->loc_port;
996 event.cm_info.cm_id = cm_node->cm_id;
997 cm_event_reset(&event);
998
999 rem_ref_cm_node(cm_node->cm_core,
1000 cm_node);
1001
1002 }
1003 }
1004 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001005 }
1006
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001007 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1008 if (!atomic_dec_return(&listener->ref_count)) {
1009 list_del(&listener->list);
1010
1011 /* decrement our listen node count */
1012 atomic_dec(&cm_core->listen_node_cnt);
1013
1014 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1015
1016 if (listener->nesvnic) {
1017 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1018 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1019 }
1020
1021 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1022
1023 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001024 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001025 ret = 0;
1026 cm_listens_destroyed++;
1027 } else {
1028 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1029 }
1030 if (listener) {
1031 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1032 nes_debug(NES_DBG_CM, "destroying listener (%p)"
1033 " with non-zero pending accepts=%u\n",
1034 listener, atomic_read(&listener->pend_accepts_cnt));
1035 }
1036
1037 return ret;
1038}
1039
1040
1041/**
1042 * mini_cm_del_listen
1043 */
1044static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1045 struct nes_cm_listener *listener)
1046{
1047 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1048 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1049 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1050}
1051
1052
1053/**
1054 * mini_cm_accelerated
1055 */
1056static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1057 struct nes_cm_node *cm_node)
1058{
1059 u32 was_timer_set;
1060 cm_node->accelerated = 1;
1061
1062 if (cm_node->accept_pend) {
1063 BUG_ON(!cm_node->listener);
1064 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001065 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001066 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1067 }
1068
1069 was_timer_set = timer_pending(&cm_core->tcp_timer);
1070 if (!was_timer_set) {
1071 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1072 add_timer(&cm_core->tcp_timer);
1073 }
1074
1075 return 0;
1076}
1077
1078
1079/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001080 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001081 */
Bob Sharp7191a0a2008-10-03 12:21:19 -07001082static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001083{
1084 struct rtable *rt;
1085 struct flowi fl;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001086 struct neighbour *neigh;
1087 int rc = -1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001088
1089 memset(&fl, 0, sizeof fl);
1090 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1091 if (ip_route_output_key(&init_net, &rt, &fl)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001092 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001093 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001094 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001095 }
1096
Bob Sharp7191a0a2008-10-03 12:21:19 -07001097 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1098 if (neigh) {
1099 if (neigh->nud_state & NUD_VALID) {
1100 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001101 " is %pM, Gateway is 0x%08X \n", dst_ip,
1102 neigh->ha, ntohl(rt->rt_gateway));
Bob Sharp7191a0a2008-10-03 12:21:19 -07001103 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1104 dst_ip, NES_ARP_ADD);
1105 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1106 NES_ARP_RESOLVE);
1107 }
1108 neigh_release(neigh);
1109 }
1110
1111 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1112 neigh_event_send(rt->u.dst.neighbour, NULL);
1113
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001114 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001115 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001116}
1117
1118
1119/**
1120 * make_cm_node - create a new instance of a cm node
1121 */
1122static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1123 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1124 struct nes_cm_listener *listener)
1125{
1126 struct nes_cm_node *cm_node;
1127 struct timespec ts;
1128 int arpindex = 0;
1129 struct nes_device *nesdev;
1130 struct nes_adapter *nesadapter;
1131
1132 /* create an hte and cm_node for this instance */
1133 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1134 if (!cm_node)
1135 return NULL;
1136
1137 /* set our node specific transport info */
1138 cm_node->loc_addr = cm_info->loc_addr;
1139 cm_node->rem_addr = cm_info->rem_addr;
1140 cm_node->loc_port = cm_info->loc_port;
1141 cm_node->rem_port = cm_info->rem_port;
1142 cm_node->send_write0 = send_first;
Harvey Harrison63779432008-10-31 00:56:00 -07001143 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1144 &cm_node->loc_addr, cm_node->loc_port,
1145 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001146 cm_node->listener = listener;
1147 cm_node->netdev = nesvnic->netdev;
1148 cm_node->cm_id = cm_info->cm_id;
1149 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1150
Faisal Latif6492cdf2008-07-24 20:50:45 -07001151 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1152 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001153
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001154 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001155
1156 cm_node->loopbackpartner = NULL;
1157 atomic_set(&cm_node->ref_count, 1);
1158 /* associate our parent CM core */
1159 cm_node->cm_core = cm_core;
1160 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1161 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1162 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1163 NES_CM_DEFAULT_RCV_WND_SCALE;
1164 ts = current_kernel_time();
1165 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1166 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Chien Tungf2b2b592008-03-20 19:55:30 -05001167 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001168 cm_node->tcp_cntxt.rcv_nxt = 0;
1169 /* get a unique session ID , add thread_id to an upcounter to handle race */
1170 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001171 cm_node->conn_type = cm_info->conn_type;
1172 cm_node->apbvt_set = 0;
1173 cm_node->accept_pend = 0;
1174
1175 cm_node->nesvnic = nesvnic;
1176 /* get some device handles, for arp lookup */
1177 nesdev = nesvnic->nesdev;
1178 nesadapter = nesdev->nesadapter;
1179
1180 cm_node->loopbackpartner = NULL;
1181 /* get the mac addr for the remote node */
Chien Tung1ee86552008-11-21 20:51:04 -06001182 if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1183 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
1184 else
1185 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001186 if (arpindex < 0) {
Bob Sharp7191a0a2008-10-03 12:21:19 -07001187 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr);
1188 if (arpindex < 0) {
1189 kfree(cm_node);
1190 return NULL;
1191 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001192 }
1193
1194 /* copy the mac addr to node context */
1195 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001196 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1197 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001198
1199 add_hte_node(cm_core, cm_node);
1200 atomic_inc(&cm_nodes_created);
1201
1202 return cm_node;
1203}
1204
1205
1206/**
1207 * add_ref_cm_node - destroy an instance of a cm node
1208 */
1209static int add_ref_cm_node(struct nes_cm_node *cm_node)
1210{
1211 atomic_inc(&cm_node->ref_count);
1212 return 0;
1213}
1214
1215
1216/**
1217 * rem_ref_cm_node - destroy an instance of a cm node
1218 */
1219static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001220 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001221{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001222 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001223 struct nes_qp *nesqp;
1224
1225 if (!cm_node)
1226 return -EINVAL;
1227
1228 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1229 if (atomic_dec_return(&cm_node->ref_count)) {
1230 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1231 return 0;
1232 }
1233 list_del(&cm_node->list);
1234 atomic_dec(&cm_core->ht_node_cnt);
1235 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1236
1237 /* if the node is destroyed before connection was accelerated */
1238 if (!cm_node->accelerated && cm_node->accept_pend) {
1239 BUG_ON(!cm_node->listener);
1240 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1241 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1242 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001243 WARN_ON(cm_node->send_entry);
1244 if (cm_node->recv_entry)
1245 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001246 if (cm_node->listener) {
1247 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1248 } else {
1249 if (cm_node->apbvt_set && cm_node->nesvnic) {
1250 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001251 PCI_FUNC(
1252 cm_node->nesvnic->nesdev->pcidev->devfn),
1253 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001254 }
1255 }
1256
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001257 atomic_dec(&cm_core->node_cnt);
1258 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001259 nesqp = cm_node->nesqp;
1260 if (nesqp) {
1261 nesqp->cm_node = NULL;
1262 nes_rem_ref(&nesqp->ibqp);
1263 cm_node->nesqp = NULL;
1264 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001265
Faisal Latif6492cdf2008-07-24 20:50:45 -07001266 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001267 return 0;
1268}
1269
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001270/**
1271 * process_options
1272 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001273static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1274 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001275{
1276 u32 tmp;
1277 u32 offset = 0;
1278 union all_known_options *all_options;
1279 char got_mss_option = 0;
1280
1281 while (offset < optionsize) {
1282 all_options = (union all_known_options *)(optionsloc + offset);
1283 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001284 case OPTION_NUMBER_END:
1285 offset = optionsize;
1286 break;
1287 case OPTION_NUMBER_NONE:
1288 offset += 1;
1289 continue;
1290 case OPTION_NUMBER_MSS:
1291 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1292 "Size: %d\n", __func__,
1293 all_options->as_mss.length, offset, optionsize);
1294 got_mss_option = 1;
1295 if (all_options->as_mss.length != 4) {
1296 return 1;
1297 } else {
1298 tmp = ntohs(all_options->as_mss.mss);
1299 if (tmp > 0 && tmp <
1300 cm_node->tcp_cntxt.mss)
1301 cm_node->tcp_cntxt.mss = tmp;
1302 }
1303 break;
1304 case OPTION_NUMBER_WINDOW_SCALE:
1305 cm_node->tcp_cntxt.snd_wscale =
1306 all_options->as_windowscale.shiftcount;
1307 break;
1308 case OPTION_NUMBER_WRITE0:
1309 cm_node->send_write0 = 1;
1310 break;
1311 default:
1312 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1313 all_options->as_base.optionnum);
1314 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001315 }
1316 offset += all_options->as_base.length;
1317 }
1318 if ((!got_mss_option) && (syn_packet))
1319 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1320 return 0;
1321}
1322
Faisal Latif6492cdf2008-07-24 20:50:45 -07001323static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001324{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001325 atomic_inc(&cm_accel_dropped_pkts);
1326 dev_kfree_skb_any(skb);
1327}
1328
Faisal Latif9d5ab132009-03-06 15:15:01 -08001329static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001330{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001331 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1332 "refcnt=%d\n", cm_node, cm_node->state,
1333 atomic_read(&cm_node->ref_count));
1334 cm_node->tcp_cntxt.rcv_nxt++;
1335 cleanup_retrans_entry(cm_node);
1336 switch (cm_node->state) {
1337 case NES_CM_STATE_SYN_RCVD:
1338 case NES_CM_STATE_SYN_SENT:
1339 case NES_CM_STATE_ESTABLISHED:
1340 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001341 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001342 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001343 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001344 break;
1345 case NES_CM_STATE_FIN_WAIT1:
1346 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001347 send_ack(cm_node, NULL);
1348 /* Wait for ACK as this is simultanous close..
1349 * After we receive ACK, do not send anything..
1350 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001351 break;
1352 case NES_CM_STATE_FIN_WAIT2:
1353 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001354 send_ack(cm_node, NULL);
1355 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1356 break;
1357 case NES_CM_STATE_TIME_WAIT:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001358 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001359 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001360 break;
1361 case NES_CM_STATE_TSA:
1362 default:
1363 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1364 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001365 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001366 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001367}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001368
Faisal Latif6492cdf2008-07-24 20:50:45 -07001369
1370static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1371 struct tcphdr *tcph)
1372{
1373
1374 int reset = 0; /* whether to send reset in case of err.. */
Faisal Latif183ecfa2008-11-21 20:50:46 -06001375 int passive_state;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001376 atomic_inc(&cm_resets_recvd);
1377 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1378 " refcnt=%d\n", cm_node, cm_node->state,
1379 atomic_read(&cm_node->ref_count));
1380 cleanup_retrans_entry(cm_node);
1381 switch (cm_node->state) {
1382 case NES_CM_STATE_SYN_SENT:
1383 case NES_CM_STATE_MPAREQ_SENT:
1384 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1385 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1386 cm_node->listener, cm_node->state);
1387 active_open_err(cm_node, skb, reset);
1388 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001389 case NES_CM_STATE_MPAREQ_RCVD:
1390 passive_state = atomic_add_return(1, &cm_node->passive_state);
1391 if (passive_state == NES_SEND_RESET_EVENT)
1392 create_event(cm_node, NES_CM_EVENT_RESET);
1393 cleanup_retrans_entry(cm_node);
1394 cm_node->state = NES_CM_STATE_CLOSED;
1395 dev_kfree_skb_any(skb);
1396 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001397 case NES_CM_STATE_ESTABLISHED:
1398 case NES_CM_STATE_SYN_RCVD:
1399 case NES_CM_STATE_LISTENING:
1400 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1401 passive_open_err(cm_node, skb, reset);
1402 break;
1403 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001404 active_open_err(cm_node, skb, reset);
1405 break;
1406 case NES_CM_STATE_CLOSED:
1407 cleanup_retrans_entry(cm_node);
1408 drop_packet(skb);
1409 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001410 case NES_CM_STATE_TIME_WAIT:
1411 cleanup_retrans_entry(cm_node);
1412 cm_node->state = NES_CM_STATE_CLOSED;
1413 rem_ref_cm_node(cm_node->cm_core, cm_node);
1414 drop_packet(skb);
1415 break;
1416 case NES_CM_STATE_FIN_WAIT1:
1417 cleanup_retrans_entry(cm_node);
1418 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001419 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001420 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001421 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001422 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001423}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001424
Faisal Latif9d5ab132009-03-06 15:15:01 -08001425
1426static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001427{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001428
Faisal Latif9d5ab132009-03-06 15:15:01 -08001429 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001430 int datasize = skb->len;
1431 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001432
1433 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1434 u32 res_type;
1435 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1436 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001437 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001438 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001439 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1440 "cm_node=%p listener=%p state=%d\n", __func__,
1441 __LINE__, cm_node, cm_node->listener,
1442 cm_node->state);
1443 active_open_err(cm_node, skb, 1);
1444 } else {
1445 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001446 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001447 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001448 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001449
1450 switch (cm_node->state) {
1451 case NES_CM_STATE_ESTABLISHED:
1452 if (res_type == NES_MPA_REQUEST_REJECT) {
1453 /*BIG problem as we are receiving the MPA.. So should
1454 * not be REJECT.. This is Passive Open.. We can
1455 * only receive it Reject for Active Open...*/
1456 WARN_ON(1);
1457 }
1458 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1459 type = NES_CM_EVENT_MPA_REQ;
1460 atomic_set(&cm_node->passive_state,
1461 NES_PASSIVE_STATE_INDICATED);
1462 break;
1463 case NES_CM_STATE_MPAREQ_SENT:
1464 if (res_type == NES_MPA_REQUEST_REJECT) {
1465 type = NES_CM_EVENT_MPA_REJECT;
1466 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1467 } else {
1468 type = NES_CM_EVENT_CONNECTED;
1469 cm_node->state = NES_CM_STATE_TSA;
1470 }
1471
1472 break;
1473 default:
1474 WARN_ON(1);
1475 break;
1476 }
1477 dev_kfree_skb_any(skb);
1478 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001479}
1480
1481static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1482{
1483 switch (cm_node->state) {
1484 case NES_CM_STATE_SYN_SENT:
1485 case NES_CM_STATE_MPAREQ_SENT:
1486 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1487 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1488 cm_node->listener, cm_node->state);
1489 active_open_err(cm_node, skb, 1);
1490 break;
1491 case NES_CM_STATE_ESTABLISHED:
1492 case NES_CM_STATE_SYN_RCVD:
1493 passive_open_err(cm_node, skb, 1);
1494 break;
1495 case NES_CM_STATE_TSA:
1496 default:
1497 drop_packet(skb);
1498 }
1499}
1500
1501static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1502 struct sk_buff *skb)
1503{
1504 int err;
1505
1506 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1507 if (err)
1508 active_open_err(cm_node, skb, 1);
1509
1510 return err;
1511}
1512
1513static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1514 struct sk_buff *skb)
1515{
1516 int err = 0;
1517 u32 seq;
1518 u32 ack_seq;
1519 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1520 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1521 u32 rcv_wnd;
1522 seq = ntohl(tcph->seq);
1523 ack_seq = ntohl(tcph->ack_seq);
1524 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1525 if (ack_seq != loc_seq_num)
1526 err = 1;
1527 else if ((seq + rcv_wnd) < rcv_nxt)
1528 err = 1;
1529 if (err) {
1530 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1531 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1532 cm_node->listener, cm_node->state);
1533 indicate_pkt_err(cm_node, skb);
1534 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1535 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1536 rcv_wnd);
1537 }
1538 return err;
1539}
1540
1541/*
1542 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1543 * is created with a listener or it may comein as rexmitted packet which in
1544 * that case will be just dropped.
1545 */
1546
1547static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1548 struct tcphdr *tcph)
1549{
1550 int ret;
1551 u32 inc_sequence;
1552 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001553
1554 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001555 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001556 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001557
Faisal Latif6492cdf2008-07-24 20:50:45 -07001558 switch (cm_node->state) {
1559 case NES_CM_STATE_SYN_SENT:
1560 case NES_CM_STATE_MPAREQ_SENT:
1561 /* Rcvd syn on active open connection*/
1562 active_open_err(cm_node, skb, 1);
1563 break;
1564 case NES_CM_STATE_LISTENING:
1565 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001566 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1567 cm_node->listener->backlog) {
1568 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1569 "pressure \n");
1570 cm_backlog_drops++;
1571 passive_open_err(cm_node, skb, 0);
1572 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001573 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001574 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1575 1);
1576 if (ret) {
1577 passive_open_err(cm_node, skb, 0);
1578 /* drop pkt */
1579 break;
1580 }
1581 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1582 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001583 cm_node->accept_pend = 1;
1584 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1585
Faisal Latif6492cdf2008-07-24 20:50:45 -07001586 cm_node->state = NES_CM_STATE_SYN_RCVD;
1587 send_syn(cm_node, 1, skb);
1588 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001589 case NES_CM_STATE_CLOSED:
1590 cleanup_retrans_entry(cm_node);
1591 send_reset(cm_node, skb);
1592 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001593 case NES_CM_STATE_TSA:
1594 case NES_CM_STATE_ESTABLISHED:
1595 case NES_CM_STATE_FIN_WAIT1:
1596 case NES_CM_STATE_FIN_WAIT2:
1597 case NES_CM_STATE_MPAREQ_RCVD:
1598 case NES_CM_STATE_LAST_ACK:
1599 case NES_CM_STATE_CLOSING:
1600 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001601 default:
1602 drop_packet(skb);
1603 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001604 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001605}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001606
Faisal Latif6492cdf2008-07-24 20:50:45 -07001607static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1608 struct tcphdr *tcph)
1609{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001610
Faisal Latif6492cdf2008-07-24 20:50:45 -07001611 int ret;
1612 u32 inc_sequence;
1613 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001614
Faisal Latif6492cdf2008-07-24 20:50:45 -07001615 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001616 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001617 inc_sequence = ntohl(tcph->seq);
1618 switch (cm_node->state) {
1619 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001620 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001621 /* active open */
1622 if (check_syn(cm_node, tcph, skb))
1623 return;
1624 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1625 /* setup options */
1626 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1627 if (ret) {
1628 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1629 cm_node);
1630 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001631 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001632 cleanup_retrans_entry(cm_node);
1633 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1634 send_mpa_request(cm_node, skb);
1635 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1636 break;
1637 case NES_CM_STATE_MPAREQ_RCVD:
1638 /* passive open, so should not be here */
1639 passive_open_err(cm_node, skb, 1);
1640 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001641 case NES_CM_STATE_LISTENING:
1642 case NES_CM_STATE_CLOSED:
1643 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1644 cleanup_retrans_entry(cm_node);
1645 send_reset(cm_node, skb);
1646 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001647 case NES_CM_STATE_ESTABLISHED:
1648 case NES_CM_STATE_FIN_WAIT1:
1649 case NES_CM_STATE_FIN_WAIT2:
1650 case NES_CM_STATE_LAST_ACK:
1651 case NES_CM_STATE_TSA:
1652 case NES_CM_STATE_CLOSING:
1653 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001654 case NES_CM_STATE_MPAREQ_SENT:
1655 default:
1656 drop_packet(skb);
1657 break;
1658 }
1659}
1660
1661static void handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1662 struct tcphdr *tcph)
1663{
1664 int datasize = 0;
1665 u32 inc_sequence;
1666 u32 rem_seq_ack;
1667 u32 rem_seq;
Faisal Latifabb77252008-11-21 20:50:52 -06001668 int ret;
1669 int optionsize;
Faisal Latifabb77252008-11-21 20:50:52 -06001670 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001671
Faisal Latif6492cdf2008-07-24 20:50:45 -07001672 if (check_seq(cm_node, tcph, skb))
1673 return;
1674
1675 skb_pull(skb, tcph->doff << 2);
1676 inc_sequence = ntohl(tcph->seq);
1677 rem_seq = ntohl(tcph->seq);
1678 rem_seq_ack = ntohl(tcph->ack_seq);
1679 datasize = skb->len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001680 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001681 switch (cm_node->state) {
1682 case NES_CM_STATE_SYN_RCVD:
1683 /* Passive OPEN */
Faisal Latifabb77252008-11-21 20:50:52 -06001684 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1685 if (ret)
1686 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001687 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
Faisal Latifabb77252008-11-21 20:50:52 -06001688 if (cm_node->tcp_cntxt.rem_ack_num !=
1689 cm_node->tcp_cntxt.loc_seq_num) {
1690 nes_debug(NES_DBG_CM, "rem_ack_num != loc_seq_num\n");
1691 cleanup_retrans_entry(cm_node);
1692 send_reset(cm_node, skb);
1693 return;
1694 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001695 cm_node->state = NES_CM_STATE_ESTABLISHED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001696 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001697 if (datasize) {
1698 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001699 handle_rcv_mpa(cm_node, skb);
1700 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001701 dev_kfree_skb_any(skb);
1702 cleanup_retrans_entry(cm_node);
1703 }
1704 break;
1705 case NES_CM_STATE_ESTABLISHED:
1706 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001707 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001708 if (datasize) {
1709 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001710 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001711 } else
1712 drop_packet(skb);
1713 break;
1714 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001715 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001716 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1717 if (datasize) {
1718 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001719 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001720 } else { /* Could be just an ack pkt.. */
1721 cleanup_retrans_entry(cm_node);
1722 dev_kfree_skb_any(skb);
1723 }
1724 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001725 case NES_CM_STATE_LISTENING:
1726 case NES_CM_STATE_CLOSED:
1727 cleanup_retrans_entry(cm_node);
1728 send_reset(cm_node, skb);
1729 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001730 case NES_CM_STATE_LAST_ACK:
1731 cleanup_retrans_entry(cm_node);
1732 cm_node->state = NES_CM_STATE_CLOSED;
1733 cm_node->cm_id->rem_ref(cm_node->cm_id);
1734 case NES_CM_STATE_CLOSING:
1735 cleanup_retrans_entry(cm_node);
1736 rem_ref_cm_node(cm_node->cm_core, cm_node);
1737 drop_packet(skb);
1738 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001739 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001740 cleanup_retrans_entry(cm_node);
1741 drop_packet(skb);
1742 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1743 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001744 case NES_CM_STATE_SYN_SENT:
1745 case NES_CM_STATE_FIN_WAIT2:
1746 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001747 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001748 case NES_CM_STATE_UNKNOWN:
1749 default:
1750 drop_packet(skb);
1751 break;
1752 }
1753}
1754
1755
1756
1757static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1758 struct sk_buff *skb, int optionsize, int passive)
1759{
1760 u8 *optionsloc = (u8 *)&tcph[1];
1761 if (optionsize) {
1762 if (process_options(cm_node, optionsloc, optionsize,
1763 (u32)tcph->syn)) {
1764 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1765 __func__, cm_node);
1766 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06001767 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001768 else
Faisal Latifabb77252008-11-21 20:50:52 -06001769 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001770 return 1;
1771 }
1772 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001773
1774 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1775 cm_node->tcp_cntxt.snd_wscale;
1776
Faisal Latif6492cdf2008-07-24 20:50:45 -07001777 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001778 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001779 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001780}
1781
Faisal Latif6492cdf2008-07-24 20:50:45 -07001782/*
1783 * active_open_err() will send reset() if flag set..
1784 * It will also send ABORT event.
1785 */
1786
1787static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1788 int reset)
1789{
1790 cleanup_retrans_entry(cm_node);
1791 if (reset) {
1792 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1793 "state=%d\n", cm_node, cm_node->state);
1794 add_ref_cm_node(cm_node);
1795 send_reset(cm_node, skb);
1796 } else
1797 dev_kfree_skb_any(skb);
1798
1799 cm_node->state = NES_CM_STATE_CLOSED;
1800 create_event(cm_node, NES_CM_EVENT_ABORTED);
1801}
1802
1803/*
1804 * passive_open_err() will either do a reset() or will free up the skb and
1805 * remove the cm_node.
1806 */
1807
1808static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1809 int reset)
1810{
1811 cleanup_retrans_entry(cm_node);
1812 cm_node->state = NES_CM_STATE_CLOSED;
1813 if (reset) {
1814 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1815 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1816 send_reset(cm_node, skb);
1817 } else {
1818 dev_kfree_skb_any(skb);
1819 rem_ref_cm_node(cm_node->cm_core, cm_node);
1820 }
1821}
1822
1823/*
1824 * free_retrans_entry() routines assumes that the retrans_list_lock has
1825 * been acquired before calling.
1826 */
1827static void free_retrans_entry(struct nes_cm_node *cm_node)
1828{
1829 struct nes_timer_entry *send_entry;
1830 send_entry = cm_node->send_entry;
1831 if (send_entry) {
1832 cm_node->send_entry = NULL;
1833 dev_kfree_skb_any(send_entry->skb);
1834 kfree(send_entry);
1835 rem_ref_cm_node(cm_node->cm_core, cm_node);
1836 }
1837}
1838
1839static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1840{
1841 unsigned long flags;
1842
1843 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1844 free_retrans_entry(cm_node);
1845 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1846}
1847
1848/**
1849 * process_packet
1850 * Returns skb if to be freed, else it will return NULL if already used..
1851 */
1852static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1853 struct nes_cm_core *cm_core)
1854{
1855 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1856 struct tcphdr *tcph = tcp_hdr(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001857 u32 fin_set = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001858 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1859
1860 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1861 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1862 tcph->ack, tcph->rst, tcph->fin);
1863
1864 if (tcph->rst)
1865 pkt_type = NES_PKT_TYPE_RST;
1866 else if (tcph->syn) {
1867 pkt_type = NES_PKT_TYPE_SYN;
1868 if (tcph->ack)
1869 pkt_type = NES_PKT_TYPE_SYNACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001870 } else if (tcph->ack)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001871 pkt_type = NES_PKT_TYPE_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001872 if (tcph->fin)
1873 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001874
1875 switch (pkt_type) {
1876 case NES_PKT_TYPE_SYN:
1877 handle_syn_pkt(cm_node, skb, tcph);
1878 break;
1879 case NES_PKT_TYPE_SYNACK:
1880 handle_synack_pkt(cm_node, skb, tcph);
1881 break;
1882 case NES_PKT_TYPE_ACK:
1883 handle_ack_pkt(cm_node, skb, tcph);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001884 if (fin_set)
1885 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001886 break;
1887 case NES_PKT_TYPE_RST:
1888 handle_rst_pkt(cm_node, skb, tcph);
1889 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001890 default:
1891 drop_packet(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001892 if (fin_set)
1893 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001894 break;
1895 }
1896}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001897
1898/**
1899 * mini_cm_listen - create a listen node with params
1900 */
1901static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001902 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001903{
1904 struct nes_cm_listener *listener;
1905 unsigned long flags;
1906
1907 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1908 cm_info->loc_addr, cm_info->loc_port);
1909
1910 /* cannot have multiple matching listeners */
1911 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1912 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1913 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1914 /* find automatically incs ref count ??? */
1915 atomic_dec(&listener->ref_count);
1916 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1917 return NULL;
1918 }
1919
1920 if (!listener) {
1921 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1922 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1923 if (!listener) {
1924 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1925 return NULL;
1926 }
1927
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001928 listener->loc_addr = htonl(cm_info->loc_addr);
1929 listener->loc_port = htons(cm_info->loc_port);
1930 listener->reused_node = 0;
1931
1932 atomic_set(&listener->ref_count, 1);
1933 }
1934 /* pasive case */
1935 /* find already inc'ed the ref count */
1936 else {
1937 listener->reused_node = 1;
1938 }
1939
1940 listener->cm_id = cm_info->cm_id;
1941 atomic_set(&listener->pend_accepts_cnt, 0);
1942 listener->cm_core = cm_core;
1943 listener->nesvnic = nesvnic;
1944 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001945
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001946 listener->conn_type = cm_info->conn_type;
1947 listener->backlog = cm_info->backlog;
1948 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1949
1950 if (!listener->reused_node) {
1951 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1952 list_add(&listener->list, &cm_core->listen_list.list);
1953 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1954 atomic_inc(&cm_core->listen_node_cnt);
1955 }
1956
1957 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1958 " listener = %p, backlog = %d, cm_id = %p.\n",
1959 cm_info->loc_addr, cm_info->loc_port,
1960 listener, listener->backlog, listener->cm_id);
1961
1962 return listener;
1963}
1964
1965
1966/**
1967 * mini_cm_connect - make a connection node with params
1968 */
Faisal Latif54c86a82008-09-30 14:47:27 -07001969static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001970 struct nes_vnic *nesvnic, u16 private_data_len,
1971 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001972{
1973 int ret = 0;
1974 struct nes_cm_node *cm_node;
1975 struct nes_cm_listener *loopbackremotelistener;
1976 struct nes_cm_node *loopbackremotenode;
1977 struct nes_cm_info loopback_cm_info;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001978 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
1979 struct ietf_mpa_frame *mpa_frame = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001980
1981 /* create a CM connection node */
1982 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
1983 if (!cm_node)
1984 return NULL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001985 mpa_frame = &cm_node->mpa_frame;
1986 strcpy(mpa_frame->key, IEFT_MPA_KEY_REQ);
1987 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
1988 mpa_frame->rev = IETF_MPA_VERSION;
1989 mpa_frame->priv_data_len = htons(private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001990
Glenn Streiff7495ab62008-04-29 13:46:54 -07001991 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001992 cm_node->tcp_cntxt.client = 1;
1993 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1994
1995 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001996 loopbackremotelistener = find_listener(cm_core,
1997 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
1998 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001999 if (loopbackremotelistener == NULL) {
2000 create_event(cm_node, NES_CM_EVENT_ABORTED);
2001 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002002 loopback_cm_info = *cm_info;
2003 loopback_cm_info.loc_port = cm_info->rem_port;
2004 loopback_cm_info.rem_port = cm_info->loc_port;
2005 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002006 loopbackremotenode = make_cm_node(cm_core, nesvnic,
2007 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002008 if (!loopbackremotenode) {
2009 rem_ref_cm_node(cm_node->cm_core, cm_node);
2010 return NULL;
2011 }
2012 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002013 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002014 loopbackremotenode->tcp_cntxt.rcv_wscale =
2015 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002016 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002017 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2018 private_data_len);
2019 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002020
Faisal Latif6492cdf2008-07-24 20:50:45 -07002021 /* we are done handling this state. */
2022 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002023 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002024 cm_node->tcp_cntxt.rcv_nxt =
2025 loopbackremotenode->tcp_cntxt.loc_seq_num;
2026 loopbackremotenode->tcp_cntxt.rcv_nxt =
2027 cm_node->tcp_cntxt.loc_seq_num;
2028 cm_node->tcp_cntxt.max_snd_wnd =
2029 loopbackremotenode->tcp_cntxt.rcv_wnd;
2030 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2031 cm_node->tcp_cntxt.rcv_wnd;
2032 cm_node->tcp_cntxt.snd_wnd =
2033 loopbackremotenode->tcp_cntxt.rcv_wnd;
2034 loopbackremotenode->tcp_cntxt.snd_wnd =
2035 cm_node->tcp_cntxt.rcv_wnd;
2036 cm_node->tcp_cntxt.snd_wscale =
2037 loopbackremotenode->tcp_cntxt.rcv_wscale;
2038 loopbackremotenode->tcp_cntxt.snd_wscale =
2039 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002040 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002041 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2042 }
2043 return cm_node;
2044 }
2045
2046 /* set our node side to client (active) side */
2047 cm_node->tcp_cntxt.client = 1;
2048 /* init our MPA frame ptr */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002049 memcpy(mpa_frame->priv_data, private_data, private_data_len);
2050
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002051 cm_node->mpa_frame_size = mpa_frame_size;
2052
2053 /* send a syn and goto syn sent state */
2054 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002055 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002056
Faisal Latif6492cdf2008-07-24 20:50:45 -07002057 if (ret) {
2058 /* error in sending the syn free up the cm_node struct */
2059 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2060 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2061 cm_node->rem_addr, cm_node->rem_port, cm_node,
2062 cm_node->cm_id);
2063 rem_ref_cm_node(cm_node->cm_core, cm_node);
2064 cm_node = NULL;
2065 }
2066
2067 if (cm_node)
2068 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2069 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2070 cm_node->rem_addr, cm_node->rem_port, cm_node,
2071 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002072
2073 return cm_node;
2074}
2075
2076
2077/**
2078 * mini_cm_accept - accept a connection
2079 * This function is never called
2080 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002081static int mini_cm_accept(struct nes_cm_core *cm_core,
2082 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002083{
2084 return 0;
2085}
2086
2087
2088/**
2089 * mini_cm_reject - reject and teardown a connection
2090 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002091static int mini_cm_reject(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002092 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002093{
2094 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002095 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002096 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002097 struct nes_cm_event event;
2098 struct iw_cm_id *cm_id = cm_node->cm_id;
2099 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002100
Faisal Latif6492cdf2008-07-24 20:50:45 -07002101 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2102 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002103
Faisal Latif6492cdf2008-07-24 20:50:45 -07002104 if (cm_node->tcp_cntxt.client)
2105 return ret;
2106 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002107
Faisal Latif9d5ab132009-03-06 15:15:01 -08002108 if (!loopback) {
2109 passive_state = atomic_add_return(1, &cm_node->passive_state);
2110 if (passive_state == NES_SEND_RESET_EVENT) {
2111 cm_node->state = NES_CM_STATE_CLOSED;
2112 rem_ref_cm_node(cm_core, cm_node);
2113 } else {
2114 ret = send_mpa_reject(cm_node);
2115 if (ret) {
2116 cm_node->state = NES_CM_STATE_CLOSED;
2117 err = send_reset(cm_node, NULL);
2118 if (err)
2119 WARN_ON(1);
2120 } else
2121 cm_id->add_ref(cm_id);
2122 }
2123 } else {
2124 cm_node->cm_id = NULL;
2125 event.cm_node = loopback;
2126 event.cm_info.rem_addr = loopback->rem_addr;
2127 event.cm_info.loc_addr = loopback->loc_addr;
2128 event.cm_info.rem_port = loopback->rem_port;
2129 event.cm_info.loc_port = loopback->loc_port;
2130 event.cm_info.cm_id = loopback->cm_id;
2131 cm_event_mpa_reject(&event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002132 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002133 loopback->state = NES_CM_STATE_CLOSING;
2134
2135 cm_id = loopback->cm_id;
2136 rem_ref_cm_node(cm_core, loopback);
2137 cm_id->rem_ref(cm_id);
2138 }
2139
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002140 return ret;
2141}
2142
2143
2144/**
2145 * mini_cm_close
2146 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002147static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002148{
2149 int ret = 0;
2150
2151 if (!cm_core || !cm_node)
2152 return -EINVAL;
2153
2154 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002155 case NES_CM_STATE_SYN_RCVD:
2156 case NES_CM_STATE_SYN_SENT:
2157 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2158 case NES_CM_STATE_ESTABLISHED:
2159 case NES_CM_STATE_ACCEPTING:
2160 case NES_CM_STATE_MPAREQ_SENT:
2161 case NES_CM_STATE_MPAREQ_RCVD:
2162 cleanup_retrans_entry(cm_node);
2163 send_reset(cm_node, NULL);
2164 break;
2165 case NES_CM_STATE_CLOSE_WAIT:
2166 cm_node->state = NES_CM_STATE_LAST_ACK;
2167 send_fin(cm_node, NULL);
2168 break;
2169 case NES_CM_STATE_FIN_WAIT1:
2170 case NES_CM_STATE_FIN_WAIT2:
2171 case NES_CM_STATE_LAST_ACK:
2172 case NES_CM_STATE_TIME_WAIT:
2173 case NES_CM_STATE_CLOSING:
2174 ret = -1;
2175 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002176 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002177 case NES_CM_STATE_LISTENING:
2178 case NES_CM_STATE_UNKNOWN:
2179 case NES_CM_STATE_INITED:
2180 case NES_CM_STATE_CLOSED:
2181 ret = rem_ref_cm_node(cm_core, cm_node);
2182 break;
2183 case NES_CM_STATE_TSA:
2184 if (cm_node->send_entry)
2185 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2186 "send_entry=%p\n", cm_node->send_entry);
2187 ret = rem_ref_cm_node(cm_core, cm_node);
2188 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002189 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002190 return ret;
2191}
2192
2193
2194/**
2195 * recv_pkt - recv an ETHERNET packet, and process it through CM
2196 * node state machine
2197 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002198static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002199 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002200{
2201 struct nes_cm_node *cm_node = NULL;
2202 struct nes_cm_listener *listener = NULL;
2203 struct iphdr *iph;
2204 struct tcphdr *tcph;
2205 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002206 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002207 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002208
Faisal Latif6492cdf2008-07-24 20:50:45 -07002209 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002210 return 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002211 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002212 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002213 }
2214
2215 iph = (struct iphdr *)skb->data;
2216 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002217
2218 nfo.loc_addr = ntohl(iph->daddr);
2219 nfo.loc_port = ntohs(tcph->dest);
2220 nfo.rem_addr = ntohl(iph->saddr);
2221 nfo.rem_port = ntohs(tcph->source);
2222
Harvey Harrison03080e52009-01-10 21:45:42 -08002223 tmp_daddr = cpu_to_be32(iph->daddr);
2224 tmp_saddr = cpu_to_be32(iph->saddr);
2225
Harvey Harrison63779432008-10-31 00:56:00 -07002226 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002227 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002228
Faisal Latif6492cdf2008-07-24 20:50:45 -07002229 do {
2230 cm_node = find_node(cm_core,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002231 nfo.rem_port, nfo.rem_addr,
2232 nfo.loc_port, nfo.loc_addr);
2233
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002234 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002235 /* Only type of packet accepted are for */
2236 /* the PASSIVE open (syn only) */
2237 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002238 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002239 break;
2240 }
2241 listener = find_listener(cm_core, nfo.loc_addr,
2242 nfo.loc_port,
2243 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002244 if (!listener) {
2245 nfo.cm_id = NULL;
2246 nfo.conn_type = 0;
2247 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2248 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002249 break;
2250 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002251 nfo.cm_id = listener->cm_id;
2252 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002253 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2254 listener);
2255 if (!cm_node) {
2256 nes_debug(NES_DBG_CM, "Unable to allocate "
2257 "node\n");
2258 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002259 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002260 dev_kfree_skb_any(skb);
2261 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002262 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002263 if (!tcph->rst && !tcph->fin) {
2264 cm_node->state = NES_CM_STATE_LISTENING;
2265 } else {
2266 cm_packets_dropped++;
2267 rem_ref_cm_node(cm_core, cm_node);
2268 dev_kfree_skb_any(skb);
2269 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002270 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002271 add_ref_cm_node(cm_node);
2272 } else if (cm_node->state == NES_CM_STATE_TSA) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002273 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002274 atomic_inc(&cm_accel_dropped_pkts);
2275 dev_kfree_skb_any(skb);
2276 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002277 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002278 skb_reset_network_header(skb);
2279 skb_set_transport_header(skb, sizeof(*tcph));
2280 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002281 process_packet(cm_node, skb, cm_core);
2282 rem_ref_cm_node(cm_core, cm_node);
2283 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002284 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002285}
2286
2287
2288/**
2289 * nes_cm_alloc_core - allocate a top level instance of a cm core
2290 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002291static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002292{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002293 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002294
2295 /* setup the CM core */
2296 /* alloc top level core control structure */
2297 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2298 if (!cm_core)
2299 return NULL;
2300
2301 INIT_LIST_HEAD(&cm_core->connected_nodes);
2302 init_timer(&cm_core->tcp_timer);
2303 cm_core->tcp_timer.function = nes_cm_timer_tick;
2304
2305 cm_core->mtu = NES_CM_DEFAULT_MTU;
2306 cm_core->state = NES_CM_STATE_INITED;
2307 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2308
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002309 atomic_set(&cm_core->events_posted, 0);
2310
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002311 cm_core->api = &nes_cm_api;
2312
2313 spin_lock_init(&cm_core->ht_lock);
2314 spin_lock_init(&cm_core->listen_list_lock);
2315
2316 INIT_LIST_HEAD(&cm_core->listen_list.list);
2317
2318 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2319
2320 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2321 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2322 cm_core->post_event = nes_cm_post_event;
2323 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2324 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2325
2326 print_core(cm_core);
2327 return cm_core;
2328}
2329
2330
2331/**
2332 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2333 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002334static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002335{
2336 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2337
2338 if (!cm_core)
2339 return -EINVAL;
2340
2341 barrier();
2342
2343 if (timer_pending(&cm_core->tcp_timer)) {
2344 del_timer(&cm_core->tcp_timer);
2345 }
2346
2347 destroy_workqueue(cm_core->event_wq);
2348 destroy_workqueue(cm_core->disconn_wq);
2349 nes_debug(NES_DBG_CM, "\n");
2350 kfree(cm_core);
2351
2352 return 0;
2353}
2354
2355
2356/**
2357 * mini_cm_get
2358 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002359static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002360{
2361 return cm_core->state;
2362}
2363
2364
2365/**
2366 * mini_cm_set
2367 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002368static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002369{
2370 int ret = 0;
2371
2372 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002373 case NES_CM_SET_PKT_SIZE:
2374 cm_core->mtu = value;
2375 break;
2376 case NES_CM_SET_FREE_PKT_Q_SIZE:
2377 cm_core->free_tx_pkt_max = value;
2378 break;
2379 default:
2380 /* unknown set option */
2381 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002382 }
2383
2384 return ret;
2385}
2386
2387
2388/**
2389 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2390 * successfully exchanged when this is called
2391 */
2392static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2393{
2394 int ret = 0;
2395
2396 if (!nesqp)
2397 return -EINVAL;
2398
2399 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2400 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2401 NES_QPCONTEXT_MISC_DROS);
2402
2403 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2404 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2405
2406 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2407
2408 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2409
2410 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2411 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2412
2413 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2414 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2415 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2416
2417 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2418 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2419 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2420
2421 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2422 nesqp->nesqp_context->ts_recent = 0;
2423 nesqp->nesqp_context->ts_age = 0;
2424 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2425 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2426 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2427 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2428 cm_node->tcp_cntxt.rcv_wscale);
2429 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2430 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2431 nesqp->nesqp_context->srtt = 0;
2432 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2433 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2434 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2435 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2436 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2437 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2438
2439 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2440 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2441 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2442 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2443 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2444 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2445 le32_to_cpu(nesqp->nesqp_context->misc));
2446 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2447 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2448 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2449
2450 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2451 cm_node->state = NES_CM_STATE_TSA;
2452
2453 return ret;
2454}
2455
2456
2457/**
2458 * nes_cm_disconn
2459 */
2460int nes_cm_disconn(struct nes_qp *nesqp)
2461{
2462 unsigned long flags;
2463
2464 spin_lock_irqsave(&nesqp->lock, flags);
2465 if (nesqp->disconn_pending == 0) {
2466 nesqp->disconn_pending++;
2467 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002468 /* init our disconnect work element, to */
2469 INIT_WORK(&nesqp->disconn_work, nes_disconnect_worker);
2470
2471 queue_work(g_cm_core->disconn_wq, &nesqp->disconn_work);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002472 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002473 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002474
2475 return 0;
2476}
2477
2478
2479/**
2480 * nes_disconnect_worker
2481 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002482static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002483{
2484 struct nes_qp *nesqp = container_of(work, struct nes_qp, disconn_work);
2485
2486 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2487 nesqp->last_aeq, nesqp->hwqp.qp_id);
2488 nes_cm_disconn_true(nesqp);
2489}
2490
2491
2492/**
2493 * nes_cm_disconn_true
2494 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002495static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002496{
2497 unsigned long flags;
2498 int ret = 0;
2499 struct iw_cm_id *cm_id;
2500 struct iw_cm_event cm_event;
2501 struct nes_vnic *nesvnic;
2502 u16 last_ae;
2503 u8 original_hw_tcp_state;
2504 u8 original_ibqp_state;
2505 u8 issued_disconnect_reset = 0;
2506
2507 if (!nesqp) {
2508 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2509 return -1;
2510 }
2511
2512 spin_lock_irqsave(&nesqp->lock, flags);
2513 cm_id = nesqp->cm_id;
2514 /* make sure we havent already closed this connection */
2515 if (!cm_id) {
2516 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2517 nesqp->hwqp.qp_id);
2518 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002519 return -1;
2520 }
2521
2522 nesvnic = to_nesvnic(nesqp->ibqp.device);
2523 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2524
2525 original_hw_tcp_state = nesqp->hw_tcp_state;
2526 original_ibqp_state = nesqp->ibqp_state;
2527 last_ae = nesqp->last_aeq;
2528
2529
2530 nes_debug(NES_DBG_CM, "set ibqp_state=%u\n", nesqp->ibqp_state);
2531
2532 if ((nesqp->cm_id) && (cm_id->event_handler)) {
2533 if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2534 ((original_ibqp_state == IB_QPS_RTS) &&
2535 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2536 atomic_inc(&cm_disconnects);
2537 cm_event.event = IW_CM_EVENT_DISCONNECT;
2538 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002539 cm_event.status = IW_CM_EVENT_STATUS_RESET;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002540 nes_debug(NES_DBG_CM, "Generating a CM "
2541 "Disconnect Event (status reset) for "
2542 "QP%u, cm_id = %p. \n",
2543 nesqp->hwqp.qp_id, cm_id);
2544 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002545 cm_event.status = IW_CM_EVENT_STATUS_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002546
2547 cm_event.local_addr = cm_id->local_addr;
2548 cm_event.remote_addr = cm_id->remote_addr;
2549 cm_event.private_data = NULL;
2550 cm_event.private_data_len = 0;
2551
Faisal Latif6492cdf2008-07-24 20:50:45 -07002552 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2553 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2554 "cm_id = %p, refcount = %u.\n",
2555 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2556 nesqp->hwqp.sq_tail, cm_id,
2557 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002558
2559 spin_unlock_irqrestore(&nesqp->lock, flags);
2560 ret = cm_id->event_handler(cm_id, &cm_event);
2561 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002562 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2563 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002564 spin_lock_irqsave(&nesqp->lock, flags);
2565 }
2566
2567 nesqp->disconn_pending = 0;
2568 /* There might have been another AE while the lock was released */
2569 original_hw_tcp_state = nesqp->hw_tcp_state;
2570 original_ibqp_state = nesqp->ibqp_state;
2571 last_ae = nesqp->last_aeq;
2572
2573 if ((issued_disconnect_reset == 0) && (nesqp->cm_id) &&
2574 ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2575 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2576 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2577 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2578 atomic_inc(&cm_closes);
2579 nesqp->cm_id = NULL;
2580 nesqp->in_disconnect = 0;
2581 spin_unlock_irqrestore(&nesqp->lock, flags);
2582 nes_disconnect(nesqp, 1);
2583
2584 cm_id->provider_data = nesqp;
2585 /* Send up the close complete event */
2586 cm_event.event = IW_CM_EVENT_CLOSE;
2587 cm_event.status = IW_CM_EVENT_STATUS_OK;
2588 cm_event.provider_data = cm_id->provider_data;
2589 cm_event.local_addr = cm_id->local_addr;
2590 cm_event.remote_addr = cm_id->remote_addr;
2591 cm_event.private_data = NULL;
2592 cm_event.private_data_len = 0;
2593
2594 ret = cm_id->event_handler(cm_id, &cm_event);
2595 if (ret) {
2596 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2597 }
2598
2599 cm_id->rem_ref(cm_id);
2600
2601 spin_lock_irqsave(&nesqp->lock, flags);
2602 if (nesqp->flush_issued == 0) {
2603 nesqp->flush_issued = 1;
2604 spin_unlock_irqrestore(&nesqp->lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002605 flush_wqes(nesvnic->nesdev, nesqp,
2606 NES_CQP_FLUSH_RQ, 1);
2607 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002608 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002609 } else {
2610 cm_id = nesqp->cm_id;
2611 spin_unlock_irqrestore(&nesqp->lock, flags);
2612 /* check to see if the inbound reset beat the outbound reset */
2613 if ((!cm_id) && (last_ae==NES_AEQE_AEID_RESET_SENT)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002614 nes_debug(NES_DBG_CM, "QP%u: Decing refcount "
2615 "due to inbound reset beating the "
2616 "outbound reset.\n", nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002617 }
2618 }
2619 } else {
2620 nesqp->disconn_pending = 0;
2621 spin_unlock_irqrestore(&nesqp->lock, flags);
2622 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002623
2624 return 0;
2625}
2626
2627
2628/**
2629 * nes_disconnect
2630 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002631static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002632{
2633 int ret = 0;
2634 struct nes_vnic *nesvnic;
2635 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002636 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002637
2638 nesvnic = to_nesvnic(nesqp->ibqp.device);
2639 if (!nesvnic)
2640 return -EINVAL;
2641
2642 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002643 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002644
2645 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2646 atomic_read(&nesvnic->netdev->refcnt));
2647
2648 if (nesqp->active_conn) {
2649
2650 /* indicate this connection is NOT active */
2651 nesqp->active_conn = 0;
2652 } else {
2653 /* Need to free the Last Streaming Mode Message */
2654 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002655 if (nesqp->lsmm_mr)
2656 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002657 pci_free_consistent(nesdev->pcidev,
2658 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2659 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2660 }
2661 }
2662
2663 /* close the CM node down if it is still active */
2664 if (nesqp->cm_node) {
2665 nes_debug(NES_DBG_CM, "Call close API\n");
2666
2667 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002668 }
2669
2670 return ret;
2671}
2672
2673
2674/**
2675 * nes_accept
2676 */
2677int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2678{
2679 u64 u64temp;
2680 struct ib_qp *ibqp;
2681 struct nes_qp *nesqp;
2682 struct nes_vnic *nesvnic;
2683 struct nes_device *nesdev;
2684 struct nes_cm_node *cm_node;
2685 struct nes_adapter *adapter;
2686 struct ib_qp_attr attr;
2687 struct iw_cm_event cm_event;
2688 struct nes_hw_qp_wqe *wqe;
2689 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002690 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002691 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002692 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002693 struct nes_ib_device *nesibdev;
2694 struct ib_mr *ibmr = NULL;
2695 struct ib_phys_buf ibphysbuf;
2696 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002697 u64 tagged_offset;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002698
2699
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002700
2701 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2702 if (!ibqp)
2703 return -EINVAL;
2704
2705 /* get all our handles */
2706 nesqp = to_nesqp(ibqp);
2707 nesvnic = to_nesvnic(nesqp->ibqp.device);
2708 nesdev = nesvnic->nesdev;
2709 adapter = nesdev->nesadapter;
2710
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002711 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002712 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2713 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2714 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002715
2716 /* associate the node with the QP */
2717 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002718 cm_node->nesqp = nesqp;
2719 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002720
Faisal Latif6492cdf2008-07-24 20:50:45 -07002721 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2722 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002723 atomic_inc(&cm_accepts);
2724
2725 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2726 atomic_read(&nesvnic->netdev->refcnt));
2727
Faisal Latif6492cdf2008-07-24 20:50:45 -07002728 /* allocate the ietf frame and space for private data */
2729 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2730 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2731 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002732
Faisal Latif6492cdf2008-07-24 20:50:45 -07002733 if (!nesqp->ietf_frame) {
2734 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2735 "data\n");
2736 return -ENOMEM;
2737 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002738
2739
Faisal Latif6492cdf2008-07-24 20:50:45 -07002740 /* setup the MPA frame */
2741 nesqp->private_data_len = conn_param->private_data_len;
2742 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002743
Faisal Latif6492cdf2008-07-24 20:50:45 -07002744 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2745 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002746
Faisal Latif6492cdf2008-07-24 20:50:45 -07002747 nesqp->ietf_frame->priv_data_len =
2748 cpu_to_be16(conn_param->private_data_len);
2749 nesqp->ietf_frame->rev = mpa_version;
2750 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002751
Faisal Latif6492cdf2008-07-24 20:50:45 -07002752 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2753 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002754
Faisal Latif6492cdf2008-07-24 20:50:45 -07002755 if (cm_id->remote_addr.sin_addr.s_addr !=
2756 cm_id->local_addr.sin_addr.s_addr) {
2757 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002758 nesibdev = nesvnic->nesibdev;
2759 nespd = nesqp->nespd;
2760 ibphysbuf.addr = nesqp->ietf_frame_pbase;
2761 ibphysbuf.size = conn_param->private_data_len +
2762 sizeof(struct ietf_mpa_frame);
Don Wood7a5efb62009-04-08 14:21:02 -07002763 tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002764 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
2765 &ibphysbuf, 1,
2766 IB_ACCESS_LOCAL_WRITE,
Don Wood7a5efb62009-04-08 14:21:02 -07002767 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002768 if (!ibmr) {
2769 nes_debug(NES_DBG_CM, "Unable to register memory region"
2770 "for lSMM for cm_node = %p \n",
2771 cm_node);
2772 return -ENOMEM;
2773 }
2774
2775 ibmr->pd = &nespd->ibpd;
2776 ibmr->device = nespd->ibpd.device;
2777 nesqp->lsmm_mr = ibmr;
2778
Faisal Latif6492cdf2008-07-24 20:50:45 -07002779 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2780 set_wqe_64bit_value(wqe->wqe_words,
2781 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2782 u64temp);
2783 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2784 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2785 NES_IWARP_SQ_WQE_WRPDU);
2786 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2787 cpu_to_le32(conn_param->private_data_len +
2788 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002789 set_wqe_64bit_value(wqe->wqe_words,
2790 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
Don Wood7a5efb62009-04-08 14:21:02 -07002791 (u64)(unsigned long)nesqp->ietf_frame);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002792 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2793 cpu_to_le32(conn_param->private_data_len +
2794 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002795 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002796
Faisal Latif6492cdf2008-07-24 20:50:45 -07002797 nesqp->nesqp_context->ird_ord_sizes |=
2798 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2799 NES_QPCONTEXT_ORDIRD_WRPDU);
2800 } else {
2801 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08002802 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002803 }
2804 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002805
2806
2807 /* Cache the cm_id in the qp */
2808 nesqp->cm_id = cm_id;
2809 cm_node->cm_id = cm_id;
2810
2811 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2812 cm_id->provider_data = nesqp;
2813 nesqp->active_conn = 0;
2814
Faisal Latif6492cdf2008-07-24 20:50:45 -07002815 if (cm_node->state == NES_CM_STATE_TSA)
2816 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2817 cm_node);
2818
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002819 nes_cm_init_tsa_conn(nesqp, cm_node);
2820
Faisal Latif6492cdf2008-07-24 20:50:45 -07002821 nesqp->nesqp_context->tcpPorts[0] =
2822 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2823 nesqp->nesqp_context->tcpPorts[1] =
2824 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2825
2826 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2827 nesqp->nesqp_context->ip0 =
2828 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2829 else
2830 nesqp->nesqp_context->ip0 =
2831 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002832
2833 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002834 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2835 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002836
Faisal Latif6492cdf2008-07-24 20:50:45 -07002837 nesqp->nesqp_context->arp_index_vlan |=
2838 cpu_to_le32(nes_arp_table(nesdev,
2839 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002840 NES_ARP_RESOLVE) << 16);
2841
2842 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002843 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002844
2845 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2846
2847 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002848 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2849 nesqp->nesqp_context->ird_ord_sizes |=
2850 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002851
2852 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002853 nes_quad.DstIpAdrIndex =
2854 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2855 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2856 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2857 else
2858 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2859 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2860 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002861
2862 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06002863 crc_value = get_crc_value(&nes_quad);
2864 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002865 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07002866 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002867
2868 nesqp->hte_index &= adapter->hte_index_mask;
2869 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2870
2871 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2872
Faisal Latif6492cdf2008-07-24 20:50:45 -07002873 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2874 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2875 "private data length=%zu.\n", nesqp->hwqp.qp_id,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002876 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2877 ntohs(cm_id->remote_addr.sin_port),
2878 ntohl(cm_id->local_addr.sin_addr.s_addr),
2879 ntohs(cm_id->local_addr.sin_port),
2880 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2881 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002882 conn_param->private_data_len +
2883 sizeof(struct ietf_mpa_frame));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002884
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002885
Coly Li73ac36e2009-01-07 18:09:16 -08002886 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002887 cm_id->add_ref(cm_id);
2888
2889 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2890 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2891 cm_event.provider_data = (void *)nesqp;
2892 cm_event.local_addr = cm_id->local_addr;
2893 cm_event.remote_addr = cm_id->remote_addr;
2894 cm_event.private_data = NULL;
2895 cm_event.private_data_len = 0;
2896 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002897 attr.qp_state = IB_QPS_RTS;
2898 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002899 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002900 cm_node->loopbackpartner->mpa_frame_size =
2901 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002902 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002903 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2904 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002905 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2906 }
2907 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002908 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2909 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002910
Faisal Latif183ecfa2008-11-21 20:50:46 -06002911 passive_state = atomic_add_return(1, &cm_node->passive_state);
2912 if (passive_state == NES_SEND_RESET_EVENT)
2913 create_event(cm_node, NES_CM_EVENT_RESET);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002914 return 0;
2915}
2916
2917
2918/**
2919 * nes_reject
2920 */
2921int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2922{
2923 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002924 struct nes_cm_node *loopback;
2925
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002926 struct nes_cm_core *cm_core;
2927
2928 atomic_inc(&cm_rejects);
2929 cm_node = (struct nes_cm_node *) cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002930 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002931 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002932 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002933 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2934
Faisal Latif9d5ab132009-03-06 15:15:01 -08002935 if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
2936 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002937
Faisal Latif9d5ab132009-03-06 15:15:01 -08002938 strcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP);
2939 if (loopback) {
2940 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
2941 loopback->mpa_frame.priv_data_len = pdata_len;
2942 loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
2943 pdata_len;
2944 } else {
2945 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2946 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2947 }
2948
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002949 cm_node->mpa_frame.rev = mpa_version;
2950 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2951
Faisal Latif9d5ab132009-03-06 15:15:01 -08002952 return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002953}
2954
2955
2956/**
2957 * nes_connect
2958 * setup and launch cm connect node
2959 */
2960int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2961{
2962 struct ib_qp *ibqp;
2963 struct nes_qp *nesqp;
2964 struct nes_vnic *nesvnic;
2965 struct nes_device *nesdev;
2966 struct nes_cm_node *cm_node;
2967 struct nes_cm_info cm_info;
2968
2969 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2970 if (!ibqp)
2971 return -EINVAL;
2972 nesqp = to_nesqp(ibqp);
2973 if (!nesqp)
2974 return -EINVAL;
2975 nesvnic = to_nesvnic(nesqp->ibqp.device);
2976 if (!nesvnic)
2977 return -EINVAL;
2978 nesdev = nesvnic->nesdev;
2979 if (!nesdev)
2980 return -EINVAL;
2981
Faisal Latif6492cdf2008-07-24 20:50:45 -07002982 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
2983 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
2984 ntohl(nesvnic->local_ipaddr),
2985 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2986 ntohs(cm_id->remote_addr.sin_port),
2987 ntohl(cm_id->local_addr.sin_addr.s_addr),
2988 ntohs(cm_id->local_addr.sin_port));
2989
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002990 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002991 nesqp->active_conn = 1;
2992
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002993 /* cache the cm_id in the qp */
2994 nesqp->cm_id = cm_id;
2995
2996 cm_id->provider_data = nesqp;
2997
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002998 nesqp->private_data_len = conn_param->private_data_len;
2999 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
3000 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003001 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
3002 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003003
Faisal Latif6492cdf2008-07-24 20:50:45 -07003004 if (cm_id->local_addr.sin_addr.s_addr !=
3005 cm_id->remote_addr.sin_addr.s_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003006 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003007 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003008
3009 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003010 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3011 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3012 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3013 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003014 cm_info.cm_id = cm_id;
3015 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3016
3017 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003018
3019 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003020 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3021 conn_param->private_data_len, (void *)conn_param->private_data,
3022 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003023 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003024 if (cm_id->local_addr.sin_addr.s_addr !=
3025 cm_id->remote_addr.sin_addr.s_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003026 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003027 PCI_FUNC(nesdev->pcidev->devfn),
3028 NES_MANAGE_APBVT_DEL);
3029
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003030 cm_id->rem_ref(cm_id);
3031 return -ENOMEM;
3032 }
3033
3034 cm_node->apbvt_set = 1;
3035 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003036 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003037 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003038
3039 return 0;
3040}
3041
3042
3043/**
3044 * nes_create_listen
3045 */
3046int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3047{
3048 struct nes_vnic *nesvnic;
3049 struct nes_cm_listener *cm_node;
3050 struct nes_cm_info cm_info;
3051 struct nes_adapter *adapter;
3052 int err;
3053
3054
3055 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3056 cm_id, ntohs(cm_id->local_addr.sin_port));
3057
3058 nesvnic = to_nesvnic(cm_id->device);
3059 if (!nesvnic)
3060 return -EINVAL;
3061 adapter = nesvnic->nesdev->nesadapter;
3062 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3063 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3064
3065 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3066 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3067
3068 /* setup listen params in our api call struct */
3069 cm_info.loc_addr = nesvnic->local_ipaddr;
3070 cm_info.loc_port = cm_id->local_addr.sin_port;
3071 cm_info.backlog = backlog;
3072 cm_info.cm_id = cm_id;
3073
3074 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3075
3076
3077 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3078 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003079 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003080 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003081 return -ENOMEM;
3082 }
3083
3084 cm_id->provider_data = cm_node;
3085
3086 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003087 err = nes_manage_apbvt(nesvnic,
3088 ntohs(cm_id->local_addr.sin_port),
3089 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3090 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003091 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003092 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3093 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003094 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3095 return err;
3096 }
3097 cm_listens_created++;
3098 }
3099
3100 cm_id->add_ref(cm_id);
3101 cm_id->provider_data = (void *)cm_node;
3102
3103
3104 return 0;
3105}
3106
3107
3108/**
3109 * nes_destroy_listen
3110 */
3111int nes_destroy_listen(struct iw_cm_id *cm_id)
3112{
3113 if (cm_id->provider_data)
3114 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3115 else
3116 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3117
3118 cm_id->rem_ref(cm_id);
3119
3120 return 0;
3121}
3122
3123
3124/**
3125 * nes_cm_recv
3126 */
3127int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3128{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003129 int rc = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003130 cm_packets_received++;
3131 if ((g_cm_core) && (g_cm_core->api)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003132 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003133 } else {
3134 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3135 " cm is not setup properly.\n");
3136 }
3137
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003138 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003139}
3140
3141
3142/**
3143 * nes_cm_start
3144 * Start and init a cm core module
3145 */
3146int nes_cm_start(void)
3147{
3148 nes_debug(NES_DBG_CM, "\n");
3149 /* create the primary CM core, pass this handle to subsequent core inits */
3150 g_cm_core = nes_cm_alloc_core();
3151 if (g_cm_core) {
3152 return 0;
3153 } else {
3154 return -ENOMEM;
3155 }
3156}
3157
3158
3159/**
3160 * nes_cm_stop
3161 * stop and dealloc all cm core instances
3162 */
3163int nes_cm_stop(void)
3164{
3165 g_cm_core->api->destroy_cm_core(g_cm_core);
3166 return 0;
3167}
3168
3169
3170/**
3171 * cm_event_connected
3172 * handle a connected event, setup QPs and HW
3173 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003174static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003175{
3176 u64 u64temp;
3177 struct nes_qp *nesqp;
3178 struct nes_vnic *nesvnic;
3179 struct nes_device *nesdev;
3180 struct nes_cm_node *cm_node;
3181 struct nes_adapter *nesadapter;
3182 struct ib_qp_attr attr;
3183 struct iw_cm_id *cm_id;
3184 struct iw_cm_event cm_event;
3185 struct nes_hw_qp_wqe *wqe;
3186 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003187 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003188 int ret;
3189
3190 /* get all our handles */
3191 cm_node = event->cm_node;
3192 cm_id = cm_node->cm_id;
3193 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3194 nesqp = (struct nes_qp *)cm_id->provider_data;
3195 nesvnic = to_nesvnic(nesqp->ibqp.device);
3196 nesdev = nesvnic->nesdev;
3197 nesadapter = nesdev->nesadapter;
3198
3199 if (nesqp->destroyed) {
3200 return;
3201 }
3202 atomic_inc(&cm_connecteds);
3203 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3204 " local port 0x%04X. jiffies = %lu.\n",
3205 nesqp->hwqp.qp_id,
3206 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3207 ntohs(cm_id->remote_addr.sin_port),
3208 ntohs(cm_id->local_addr.sin_port),
3209 jiffies);
3210
3211 nes_cm_init_tsa_conn(nesqp, cm_node);
3212
3213 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003214 nesqp->nesqp_context->tcpPorts[0] =
3215 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3216 nesqp->nesqp_context->tcpPorts[1] =
3217 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3218 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3219 nesqp->nesqp_context->ip0 =
3220 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3221 else
3222 nesqp->nesqp_context->ip0 =
3223 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003224
3225 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003226 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3227 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003228 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003229 nes_arp_table(nesdev,
3230 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003231 NULL, NES_ARP_RESOLVE) << 16);
3232 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3233 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3234 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3235 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003236 cpu_to_le32((u32)1 <<
3237 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003238
3239 /* Adjust tail for not having a LSMM */
3240 nesqp->hwqp.sq_tail = 1;
3241
3242#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003243 if (cm_node->send_write0) {
3244 nes_debug(NES_DBG_CM, "Sending first write.\n");
3245 wqe = &nesqp->hwqp.sq_vbase[0];
3246 u64temp = (unsigned long)nesqp;
3247 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3248 set_wqe_64bit_value(wqe->wqe_words,
3249 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3250 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3251 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3252 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3253 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3254 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3255 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3256 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003257
Faisal Latif6492cdf2008-07-24 20:50:45 -07003258 /* use the reserved spot on the WQ for the extra first WQE */
3259 nesqp->nesqp_context->ird_ord_sizes &=
3260 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3261 NES_QPCONTEXT_ORDIRD_WRPDU |
3262 NES_QPCONTEXT_ORDIRD_ALSMM));
3263 nesqp->skip_lsmm = 1;
3264 nesqp->hwqp.sq_tail = 0;
3265 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3266 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3267 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003268#endif
3269
3270 memset(&nes_quad, 0, sizeof(nes_quad));
3271
Faisal Latif6492cdf2008-07-24 20:50:45 -07003272 nes_quad.DstIpAdrIndex =
3273 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3274 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3275 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3276 else
3277 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003278 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3279 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3280
3281 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003282 crc_value = get_crc_value(&nes_quad);
3283 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003284 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3285 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3286
3287 nesqp->hte_index &= nesadapter->hte_index_mask;
3288 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3289
3290 nesqp->ietf_frame = &cm_node->mpa_frame;
3291 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3292 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3293
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003294 /* notify OF layer we successfully created the requested connection */
3295 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3296 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3297 cm_event.provider_data = cm_id->provider_data;
3298 cm_event.local_addr.sin_family = AF_INET;
3299 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3300 cm_event.remote_addr = cm_id->remote_addr;
3301
Faisal Latif6492cdf2008-07-24 20:50:45 -07003302 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3303 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003304
3305 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3306 ret = cm_id->event_handler(cm_id, &cm_event);
3307 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3308
3309 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003310 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3311 "ret=%d\n", __func__, __LINE__, ret);
3312 attr.qp_state = IB_QPS_RTS;
3313 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003314
Faisal Latif6492cdf2008-07-24 20:50:45 -07003315 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3316 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003317
3318 return;
3319}
3320
3321
3322/**
3323 * cm_event_connect_error
3324 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003325static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003326{
3327 struct nes_qp *nesqp;
3328 struct iw_cm_id *cm_id;
3329 struct iw_cm_event cm_event;
3330 /* struct nes_cm_info cm_info; */
3331 int ret;
3332
3333 if (!event->cm_node)
3334 return;
3335
3336 cm_id = event->cm_node->cm_id;
3337 if (!cm_id) {
3338 return;
3339 }
3340
3341 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3342 nesqp = cm_id->provider_data;
3343
3344 if (!nesqp) {
3345 return;
3346 }
3347
3348 /* notify OF layer about this connection error event */
3349 /* cm_id->rem_ref(cm_id); */
3350 nesqp->cm_id = NULL;
3351 cm_id->provider_data = NULL;
3352 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3353 cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
3354 cm_event.provider_data = cm_id->provider_data;
3355 cm_event.local_addr = cm_id->local_addr;
3356 cm_event.remote_addr = cm_id->remote_addr;
3357 cm_event.private_data = NULL;
3358 cm_event.private_data_len = 0;
3359
Faisal Latif6492cdf2008-07-24 20:50:45 -07003360 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3361 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3362 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003363
3364 ret = cm_id->event_handler(cm_id, &cm_event);
3365 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3366 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003367 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3368 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003369 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003370
Faisal Latif6492cdf2008-07-24 20:50:45 -07003371 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003372 return;
3373}
3374
3375
3376/**
3377 * cm_event_reset
3378 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003379static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003380{
3381 struct nes_qp *nesqp;
3382 struct iw_cm_id *cm_id;
3383 struct iw_cm_event cm_event;
3384 /* struct nes_cm_info cm_info; */
3385 int ret;
3386
3387 if (!event->cm_node)
3388 return;
3389
3390 if (!event->cm_node->cm_id)
3391 return;
3392
3393 cm_id = event->cm_node->cm_id;
3394
3395 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3396 nesqp = cm_id->provider_data;
3397
3398 nesqp->cm_id = NULL;
3399 /* cm_id->provider_data = NULL; */
3400 cm_event.event = IW_CM_EVENT_DISCONNECT;
3401 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3402 cm_event.provider_data = cm_id->provider_data;
3403 cm_event.local_addr = cm_id->local_addr;
3404 cm_event.remote_addr = cm_id->remote_addr;
3405 cm_event.private_data = NULL;
3406 cm_event.private_data_len = 0;
3407
3408 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003409 cm_id->add_ref(cm_id);
3410 atomic_inc(&cm_closes);
3411 cm_event.event = IW_CM_EVENT_CLOSE;
3412 cm_event.status = IW_CM_EVENT_STATUS_OK;
3413 cm_event.provider_data = cm_id->provider_data;
3414 cm_event.local_addr = cm_id->local_addr;
3415 cm_event.remote_addr = cm_id->remote_addr;
3416 cm_event.private_data = NULL;
3417 cm_event.private_data_len = 0;
3418 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3419 ret = cm_id->event_handler(cm_id, &cm_event);
3420
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003421 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3422
3423
3424 /* notify OF layer about this connection error event */
3425 cm_id->rem_ref(cm_id);
3426
3427 return;
3428}
3429
3430
3431/**
3432 * cm_event_mpa_req
3433 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003434static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003435{
3436 struct iw_cm_id *cm_id;
3437 struct iw_cm_event cm_event;
3438 int ret;
3439 struct nes_cm_node *cm_node;
3440
3441 cm_node = event->cm_node;
3442 if (!cm_node)
3443 return;
3444 cm_id = cm_node->cm_id;
3445
3446 atomic_inc(&cm_connect_reqs);
3447 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3448 cm_node, cm_id, jiffies);
3449
3450 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3451 cm_event.status = IW_CM_EVENT_STATUS_OK;
3452 cm_event.provider_data = (void *)cm_node;
3453
3454 cm_event.local_addr.sin_family = AF_INET;
3455 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3456 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3457
3458 cm_event.remote_addr.sin_family = AF_INET;
3459 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3460 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003461 cm_event.private_data = cm_node->mpa_frame_buf;
3462 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003463
3464 ret = cm_id->event_handler(cm_id, &cm_event);
3465 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003466 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3467 __func__, __LINE__, ret);
3468 return;
3469}
3470
3471
3472static void cm_event_mpa_reject(struct nes_cm_event *event)
3473{
3474 struct iw_cm_id *cm_id;
3475 struct iw_cm_event cm_event;
3476 struct nes_cm_node *cm_node;
3477 int ret;
3478
3479 cm_node = event->cm_node;
3480 if (!cm_node)
3481 return;
3482 cm_id = cm_node->cm_id;
3483
3484 atomic_inc(&cm_connect_reqs);
3485 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3486 cm_node, cm_id, jiffies);
3487
3488 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3489 cm_event.status = -ECONNREFUSED;
3490 cm_event.provider_data = cm_id->provider_data;
3491
3492 cm_event.local_addr.sin_family = AF_INET;
3493 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3494 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3495
3496 cm_event.remote_addr.sin_family = AF_INET;
3497 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3498 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3499
3500 cm_event.private_data = cm_node->mpa_frame_buf;
3501 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3502
3503 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3504 "remove_addr=%08x\n",
3505 cm_event.local_addr.sin_addr.s_addr,
3506 cm_event.remote_addr.sin_addr.s_addr);
3507
3508 ret = cm_id->event_handler(cm_id, &cm_event);
3509 if (ret)
3510 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003511 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003512
3513 return;
3514}
3515
3516
3517static void nes_cm_event_handler(struct work_struct *);
3518
3519/**
3520 * nes_cm_post_event
3521 * post an event to the cm event handler
3522 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003523static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003524{
3525 atomic_inc(&event->cm_node->cm_core->events_posted);
3526 add_ref_cm_node(event->cm_node);
3527 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3528 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003529 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3530 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003531
3532 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3533
3534 nes_debug(NES_DBG_CM, "Exit\n");
3535 return 0;
3536}
3537
3538
3539/**
3540 * nes_cm_event_handler
3541 * worker function to handle cm events
3542 * will free instance of nes_cm_event
3543 */
3544static void nes_cm_event_handler(struct work_struct *work)
3545{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003546 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3547 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003548 struct nes_cm_core *cm_core;
3549
Faisal Latif6492cdf2008-07-24 20:50:45 -07003550 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003551 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003552
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003553 cm_core = event->cm_node->cm_core;
3554 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003555 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003556
3557 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003558 case NES_CM_EVENT_MPA_REQ:
3559 cm_event_mpa_req(event);
3560 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3561 event->cm_node);
3562 break;
3563 case NES_CM_EVENT_RESET:
3564 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3565 event->cm_node);
3566 cm_event_reset(event);
3567 break;
3568 case NES_CM_EVENT_CONNECTED:
3569 if ((!event->cm_node->cm_id) ||
3570 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003571 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003572 cm_event_connected(event);
3573 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3574 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003575 case NES_CM_EVENT_MPA_REJECT:
3576 if ((!event->cm_node->cm_id) ||
3577 (event->cm_node->state == NES_CM_STATE_TSA))
3578 break;
3579 cm_event_mpa_reject(event);
3580 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3581 break;
3582
Faisal Latif6492cdf2008-07-24 20:50:45 -07003583 case NES_CM_EVENT_ABORTED:
3584 if ((!event->cm_node->cm_id) ||
3585 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003586 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003587 cm_event_connect_error(event);
3588 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3589 break;
3590 case NES_CM_EVENT_DROPPED_PKT:
3591 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3592 break;
3593 default:
3594 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3595 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003596 }
3597
3598 atomic_dec(&cm_core->events_posted);
3599 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3600 rem_ref_cm_node(cm_core, event->cm_node);
3601 kfree(event);
3602
3603 return;
3604}