blob: 8bc11e183803c58432b98fd042c85b212f9545d5 [file] [log] [blame]
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001/*
Chien Tungfa6c87d2009-12-09 15:21:56 -08002 * Copyright (c) 2006 - 2009 Intel Corporation. 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>
Faisal Latifd2fa9b262009-12-09 15:54:28 -080055#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Bob Sharp7191a0a2008-10-03 12:21:19 -070057#include <net/arp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080058#include <net/neighbour.h>
59#include <net/route.h>
60#include <net/ip_fib.h>
Faisal Latifc11470f2009-04-27 13:38:31 -070061#include <net/tcp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080062
63#include "nes.h"
64
65u32 cm_packets_sent;
66u32 cm_packets_bounced;
67u32 cm_packets_dropped;
68u32 cm_packets_retrans;
69u32 cm_packets_created;
70u32 cm_packets_received;
Faisal Latif6e10d2e2010-02-12 19:55:03 +000071atomic_t cm_listens_created;
72atomic_t cm_listens_destroyed;
Glenn Streiff3c2d7742008-02-04 20:20:45 -080073u32 cm_backlog_drops;
74atomic_t cm_loopbacks;
75atomic_t cm_nodes_created;
76atomic_t cm_nodes_destroyed;
77atomic_t cm_accel_dropped_pkts;
78atomic_t cm_resets_recvd;
79
Faisal Latif6492cdf2008-07-24 20:50:45 -070080static inline int mini_cm_accelerated(struct nes_cm_core *,
81 struct nes_cm_node *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080082static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070083 struct nes_vnic *, struct nes_cm_info *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080084static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070085static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070086 struct nes_vnic *, u16, void *, struct nes_cm_info *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070087static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -070088static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
89 struct nes_cm_node *);
90static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
91 struct nes_cm_node *);
Faisal Latif4a14f6a2008-11-21 20:50:49 -060092static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070093 struct sk_buff *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070094static int mini_cm_dealloc_core(struct nes_cm_core *);
95static int mini_cm_get(struct nes_cm_core *);
96static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070097
Chien Tung6098d102008-11-21 20:51:01 -060098static void form_cm_frame(struct sk_buff *, struct nes_cm_node *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070099 void *, u32, void *, u32, u8);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700100static int add_ref_cm_node(struct nes_cm_node *);
101static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
102
Roland Dreier1a855fb2008-04-16 21:01:09 -0700103static int nes_cm_disconn_true(struct nes_qp *);
104static int nes_cm_post_event(struct nes_cm_event *event);
105static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
106static void nes_disconnect_worker(struct work_struct *work);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700107
108static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800109static int send_mpa_reject(struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700110static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
111static int send_reset(struct nes_cm_node *, struct sk_buff *);
112static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fb2008-04-16 21:01:09 -0700113static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700114static void process_packet(struct nes_cm_node *, struct sk_buff *,
115 struct nes_cm_core *);
116
117static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
118static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
119static void cleanup_retrans_entry(struct nes_cm_node *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800120static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700121static void free_retrans_entry(struct nes_cm_node *cm_node);
122static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
123 struct sk_buff *skb, int optionsize, int passive);
124
125/* CM event handler functions */
126static void cm_event_connected(struct nes_cm_event *);
127static void cm_event_connect_error(struct nes_cm_event *);
128static void cm_event_reset(struct nes_cm_event *);
129static void cm_event_mpa_req(struct nes_cm_event *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800130static void cm_event_mpa_reject(struct nes_cm_event *);
131static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700132
133static void print_core(struct nes_cm_core *core);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800134
135/* External CM API Interface */
136/* instance of function pointers for client API */
137/* set address of this instance to cm_core->cm_ops at cm_core alloc */
138static struct nes_cm_ops nes_cm_api = {
139 mini_cm_accelerated,
140 mini_cm_listen,
141 mini_cm_del_listen,
142 mini_cm_connect,
143 mini_cm_close,
144 mini_cm_accept,
145 mini_cm_reject,
146 mini_cm_recv_pkt,
147 mini_cm_dealloc_core,
148 mini_cm_get,
149 mini_cm_set
150};
151
Roland Dreier1a855fb2008-04-16 21:01:09 -0700152static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800153
154atomic_t cm_connects;
155atomic_t cm_accepts;
156atomic_t cm_disconnects;
157atomic_t cm_closes;
158atomic_t cm_connecteds;
159atomic_t cm_connect_reqs;
160atomic_t cm_rejects;
161
162
163/**
164 * create_event
165 */
166static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
167 enum nes_cm_event_type type)
168{
169 struct nes_cm_event *event;
170
171 if (!cm_node->cm_id)
172 return NULL;
173
174 /* allocate an empty event */
175 event = kzalloc(sizeof(*event), GFP_ATOMIC);
176
177 if (!event)
178 return NULL;
179
180 event->type = type;
181 event->cm_node = cm_node;
182 event->cm_info.rem_addr = cm_node->rem_addr;
183 event->cm_info.loc_addr = cm_node->loc_addr;
184 event->cm_info.rem_port = cm_node->rem_port;
185 event->cm_info.loc_port = cm_node->loc_port;
186 event->cm_info.cm_id = cm_node->cm_id;
187
Faisal Latif6492cdf2008-07-24 20:50:45 -0700188 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
189 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
190 cm_node, event, type, event->cm_info.loc_addr,
191 event->cm_info.loc_port, event->cm_info.rem_addr,
192 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800193
194 nes_cm_post_event(event);
195 return event;
196}
197
198
199/**
200 * send_mpa_request
201 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700202static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800203{
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800204 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700205 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800206 return -1;
207 }
208
209 /* send an MPA Request frame */
210 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
211 cm_node->mpa_frame_size, SET_ACK);
212
Faisal Latif9d5ab132009-03-06 15:15:01 -0800213 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
214}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800215
Faisal Latif9d5ab132009-03-06 15:15:01 -0800216
217
218static int send_mpa_reject(struct nes_cm_node *cm_node)
219{
220 struct sk_buff *skb = NULL;
221
222 skb = dev_alloc_skb(MAX_CM_BUFFER);
223 if (!skb) {
224 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
225 return -ENOMEM;
226 }
227
228 /* send an MPA reject frame */
229 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
230 cm_node->mpa_frame_size, SET_ACK | SET_FIN);
231
232 cm_node->state = NES_CM_STATE_FIN_WAIT1;
233 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800234}
235
236
237/**
238 * recv_mpa - process a received TCP pkt, we are expecting an
239 * IETF MPA frame
240 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800241static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
242 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800243{
244 struct ietf_mpa_frame *mpa_frame;
245
Faisal Latif9d5ab132009-03-06 15:15:01 -0800246 *type = NES_MPA_REQUEST_ACCEPT;
247
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800248 /* assume req frame is in tcp data payload */
249 if (len < sizeof(struct ietf_mpa_frame)) {
250 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800251 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800252 }
253
254 mpa_frame = (struct ietf_mpa_frame *)buffer;
255 cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
Faisal Latif1cf078c2009-12-09 15:53:54 -0800256 /* make sure mpa private data len is less than 512 bytes */
257 if (cm_node->mpa_frame_size > IETF_MAX_PRIV_DATA_LEN) {
258 nes_debug(NES_DBG_CM, "The received Length of Private"
259 " Data field exceeds 512 octets\n");
260 return -EINVAL;
261 }
262 /*
263 * make sure MPA receiver interoperate with the
264 * received MPA version and MPA key information
265 *
266 */
267 if (mpa_frame->rev != mpa_version) {
268 nes_debug(NES_DBG_CM, "The received mpa version"
269 " can not be interoperated\n");
270 return -EINVAL;
271 }
272 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
273 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
274 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
275 return -EINVAL;
276 }
277 } else {
278 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
279 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
280 return -EINVAL;
281 }
282 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800283
284 if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
285 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
286 " complete (%x + %x != %x)\n",
Faisal Latif9d5ab132009-03-06 15:15:01 -0800287 cm_node->mpa_frame_size,
288 (u32)sizeof(struct ietf_mpa_frame), len);
289 return -EINVAL;
290 }
291 /* make sure it does not exceed the max size */
292 if (len > MAX_CM_BUFFER) {
293 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
294 " (%x + %x != %x)\n",
295 cm_node->mpa_frame_size,
296 (u32)sizeof(struct ietf_mpa_frame), len);
297 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800298 }
299
300 /* copy entire MPA frame to our cm_node's frame */
301 memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
302 cm_node->mpa_frame_size);
303
Faisal Latif9d5ab132009-03-06 15:15:01 -0800304 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
305 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800306 return 0;
307}
308
309
310/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800311 * form_cm_frame - get a free packet and build empty frame Use
312 * node info to build.
313 */
Chien Tung6098d102008-11-21 20:51:01 -0600314static void form_cm_frame(struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700315 struct nes_cm_node *cm_node, void *options, u32 optionsize,
316 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800317{
318 struct tcphdr *tcph;
319 struct iphdr *iph;
320 struct ethhdr *ethh;
321 u8 *buf;
322 u16 packetsize = sizeof(*iph);
323
324 packetsize += sizeof(*tcph);
325 packetsize += optionsize + datasize;
326
327 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
328
329 skb->len = 0;
330 buf = skb_put(skb, packetsize + ETH_HLEN);
331
332 ethh = (struct ethhdr *) buf;
333 buf += ETH_HLEN;
334
335 iph = (struct iphdr *)buf;
336 buf += sizeof(*iph);
337 tcph = (struct tcphdr *)buf;
338 skb_reset_mac_header(skb);
339 skb_set_network_header(skb, ETH_HLEN);
340 skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
341 buf += sizeof(*tcph);
342
343 skb->ip_summed = CHECKSUM_PARTIAL;
344 skb->protocol = htons(0x800);
345 skb->data_len = 0;
346 skb->mac_len = ETH_HLEN;
347
348 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
349 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
350 ethh->h_proto = htons(0x0800);
351
352 iph->version = IPVERSION;
353 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
354 iph->tos = 0;
355 iph->tot_len = htons(packetsize);
356 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
357
358 iph->frag_off = htons(0x4000);
359 iph->ttl = 0x40;
360 iph->protocol = 0x06; /* IPPROTO_TCP */
361
362 iph->saddr = htonl(cm_node->loc_addr);
363 iph->daddr = htonl(cm_node->rem_addr);
364
365 tcph->source = htons(cm_node->loc_port);
366 tcph->dest = htons(cm_node->rem_port);
367 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
368
369 if (flags & SET_ACK) {
370 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
371 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
372 tcph->ack = 1;
373 } else
374 tcph->ack_seq = 0;
375
376 if (flags & SET_SYN) {
377 cm_node->tcp_cntxt.loc_seq_num++;
378 tcph->syn = 1;
379 } else
Faisal Latif6492cdf2008-07-24 20:50:45 -0700380 cm_node->tcp_cntxt.loc_seq_num += datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800381
Faisal Latif6492cdf2008-07-24 20:50:45 -0700382 if (flags & SET_FIN) {
383 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800384 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700385 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800386
387 if (flags & SET_RST)
388 tcph->rst = 1;
389
390 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
391 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
392 tcph->urg_ptr = 0;
393 if (optionsize)
394 memcpy(buf, options, optionsize);
395 buf += optionsize;
396 if (datasize)
397 memcpy(buf, data, datasize);
398
399 skb_shinfo(skb)->nr_frags = 0;
400 cm_packets_created++;
401
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800402}
403
404
405/**
406 * print_core - dump a cm core
407 */
408static void print_core(struct nes_cm_core *core)
409{
410 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
411 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
412 if (!core)
413 return;
414 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800415
416 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
417
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800418 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
419 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
420
421 nes_debug(NES_DBG_CM, "core : %p \n", core);
422
423 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
424}
425
426
427/**
428 * schedule_nes_timer
429 * note - cm_node needs to be protected before calling this. Encase in:
430 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
431 */
432int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
433 enum nes_timer_type type, int send_retrans,
434 int close_when_complete)
435{
436 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700437 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800438 struct nes_timer_entry *new_send;
439 int ret = 0;
440 u32 was_timer_set;
441
442 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
443 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800444 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800445
446 /* new_send->timetosend = currenttime */
447 new_send->retrycount = NES_DEFAULT_RETRYS;
448 new_send->retranscount = NES_DEFAULT_RETRANS;
449 new_send->skb = skb;
450 new_send->timetosend = jiffies;
451 new_send->type = type;
452 new_send->netdev = cm_node->netdev;
453 new_send->send_retrans = send_retrans;
454 new_send->close_when_complete = close_when_complete;
455
456 if (type == NES_TIMER_TYPE_CLOSE) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700457 new_send->timetosend += (HZ/10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800458 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700459 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800460 WARN_ON(1);
461 return -EINVAL;
462 }
463 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800464 }
465
466 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700467 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800468 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700469 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
470 cm_node->send_entry = new_send;
471 add_ref_cm_node(cm_node);
472 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
473 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800474
475 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
476 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700477 nes_debug(NES_DBG_CM, "Error sending packet %p "
478 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800479 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700480 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800481 } else {
482 cm_packets_sent++;
483 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700484 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800485 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700486 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800487 return ret;
488 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800489 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800490 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800491
492 was_timer_set = timer_pending(&cm_core->tcp_timer);
493
494 if (!was_timer_set) {
495 cm_core->tcp_timer.expires = new_send->timetosend;
496 add_timer(&cm_core->tcp_timer);
497 }
498
499 return ret;
500}
501
Faisal Latif9d5ab132009-03-06 15:15:01 -0800502static void nes_retrans_expired(struct nes_cm_node *cm_node)
503{
Faisal Latif68237a02009-06-22 22:53:28 -0700504 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800505 switch (cm_node->state) {
506 case NES_CM_STATE_SYN_RCVD:
507 case NES_CM_STATE_CLOSING:
508 rem_ref_cm_node(cm_node->cm_core, cm_node);
509 break;
510 case NES_CM_STATE_LAST_ACK:
511 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700512 if (cm_node->cm_id)
513 cm_id->rem_ref(cm_id);
514 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800515 send_reset(cm_node, NULL);
516 break;
517 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800518 add_ref_cm_node(cm_node);
519 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800520 create_event(cm_node, NES_CM_EVENT_ABORTED);
521 }
522}
523
524static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
525{
526 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
527 struct iw_cm_id *cm_id = cm_node->cm_id;
528 struct nes_qp *nesqp;
529 unsigned long qplockflags;
530
531 if (!recv_entry)
532 return;
533 nesqp = (struct nes_qp *)recv_entry->skb;
534 if (nesqp) {
535 spin_lock_irqsave(&nesqp->lock, qplockflags);
536 if (nesqp->cm_id) {
537 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
538 "refcount = %d: HIT A "
539 "NES_TIMER_TYPE_CLOSE with something "
540 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
541 atomic_read(&nesqp->refcount));
542 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
543 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
544 nesqp->ibqp_state = IB_QPS_ERR;
545 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
546 nes_cm_disconn(nesqp);
547 } else {
548 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
549 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
550 "refcount = %d: HIT A "
551 "NES_TIMER_TYPE_CLOSE with nothing "
552 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
553 atomic_read(&nesqp->refcount));
554 }
555 } else if (rem_node) {
556 /* TIME_WAIT state */
557 rem_ref_cm_node(cm_node->cm_core, cm_node);
558 }
559 if (cm_node->cm_id)
560 cm_id->rem_ref(cm_id);
561 kfree(recv_entry);
562 cm_node->recv_entry = NULL;
563}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800564
565/**
566 * nes_cm_timer_tick
567 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700568static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800569{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800570 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800571 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800572 struct nes_cm_node *cm_node;
573 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800574 struct list_head *list_core_temp;
575 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800576 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800577 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700578 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800579 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800580
Faisal Latif879e5bd2008-11-21 20:50:41 -0600581 struct list_head timer_list;
582 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800583 spin_lock_irqsave(&cm_core->ht_lock, flags);
584
Faisal Latif6492cdf2008-07-24 20:50:45 -0700585 list_for_each_safe(list_node, list_core_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600586 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800587 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800588 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600589 add_ref_cm_node(cm_node);
590 list_add(&cm_node->timer_entry, &timer_list);
591 }
592 }
593 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
594
595 list_for_each_safe(list_node, list_core_temp, &timer_list) {
596 cm_node = container_of(list_node, struct nes_cm_node,
597 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800598 recv_entry = cm_node->recv_entry;
599
600 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700601 if (time_after(recv_entry->timetosend, jiffies)) {
602 if (nexttimeout > recv_entry->timetosend ||
Faisal Latif9d5ab132009-03-06 15:15:01 -0800603 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800604 nexttimeout = recv_entry->timetosend;
605 settimer = 1;
606 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800607 } else
608 handle_recv_entry(cm_node, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800609 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800610
611 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700612 do {
613 send_entry = cm_node->send_entry;
614 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600615 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800616 if (time_after(send_entry->timetosend, jiffies)) {
617 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700618 if ((nexttimeout >
619 send_entry->timetosend) ||
620 !settimer) {
621 nexttimeout =
622 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800623 settimer = 1;
624 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800625 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700626 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800627 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800628 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800629 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700630
631 if ((cm_node->state == NES_CM_STATE_TSA) ||
632 (cm_node->state == NES_CM_STATE_CLOSED)) {
633 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600634 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800635 }
636
Faisal Latif6492cdf2008-07-24 20:50:45 -0700637 if (!send_entry->retranscount ||
638 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800639 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700640 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800641
Faisal Latif6492cdf2008-07-24 20:50:45 -0700642 spin_unlock_irqrestore(
643 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800644 nes_retrans_expired(cm_node);
645 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700646 spin_lock_irqsave(&cm_node->retrans_list_lock,
647 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600648 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800649 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800650 atomic_inc(&send_entry->skb->users);
651 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700652 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
653 "for node %p, jiffies = %lu, time to send = "
654 "%lu, retranscount = %u, send_entry->seq_num = "
655 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
656 "0x%08X\n", send_entry, cm_node, jiffies,
657 send_entry->timetosend,
658 send_entry->retranscount,
659 send_entry->seq_num,
660 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800661
Faisal Latif6492cdf2008-07-24 20:50:45 -0700662 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
663 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800664 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700665 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800666 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700667 nes_debug(NES_DBG_CM, "rexmit failed for "
668 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800669 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800670 send_entry->retrycount--;
671 nexttimeout = jiffies + NES_SHORT_TIME;
672 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600673 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800674 } else {
675 cm_packets_sent++;
676 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700677 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
678 "%u, retry count = %u.\n",
679 send_entry->retranscount,
680 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800681 if (send_entry->send_retrans) {
682 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700683 timetosend = (NES_RETRY_TIMEOUT <<
684 (NES_DEFAULT_RETRANS - send_entry->retranscount));
685
Faisal Latif6492cdf2008-07-24 20:50:45 -0700686 send_entry->timetosend = jiffies +
Faisal Latif4e9c3902009-04-27 13:39:36 -0700687 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700688 if (nexttimeout > send_entry->timetosend ||
689 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800690 nexttimeout = send_entry->timetosend;
691 settimer = 1;
692 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800693 } else {
694 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700695 close_when_complete =
696 send_entry->close_when_complete;
697 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
698 cm_node, cm_node->state);
699 free_retrans_entry(cm_node);
700 if (close_when_complete)
701 rem_ref_cm_node(cm_node->cm_core,
702 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800703 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700704 } while (0);
705
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800706 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700707 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800708 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800709
710 if (settimer) {
711 if (!timer_pending(&cm_core->tcp_timer)) {
712 cm_core->tcp_timer.expires = nexttimeout;
713 add_timer(&cm_core->tcp_timer);
714 }
715 }
716}
717
718
719/**
720 * send_syn
721 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700722static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
723 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800724{
725 int ret;
726 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800727 char optionsbuffer[sizeof(struct option_mss) +
Faisal Latif6492cdf2008-07-24 20:50:45 -0700728 sizeof(struct option_windowscale) + sizeof(struct option_base) +
729 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800730
731 int optionssize = 0;
732 /* Sending MSS option */
733 union all_known_options *options;
734
735 if (!cm_node)
736 return -EINVAL;
737
738 options = (union all_known_options *)&optionsbuffer[optionssize];
739 options->as_mss.optionnum = OPTION_NUMBER_MSS;
740 options->as_mss.length = sizeof(struct option_mss);
741 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
742 optionssize += sizeof(struct option_mss);
743
744 options = (union all_known_options *)&optionsbuffer[optionssize];
745 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
746 options->as_windowscale.length = sizeof(struct option_windowscale);
747 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
748 optionssize += sizeof(struct option_windowscale);
749
Faisal Latif6492cdf2008-07-24 20:50:45 -0700750 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800751 options = (union all_known_options *)&optionsbuffer[optionssize];
752 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
753 options->as_base.length = sizeof(struct option_base);
754 optionssize += sizeof(struct option_base);
755 /* we need the size to be a multiple of 4 */
756 options = (union all_known_options *)&optionsbuffer[optionssize];
757 options->as_end = 1;
758 optionssize += 1;
759 options = (union all_known_options *)&optionsbuffer[optionssize];
760 options->as_end = 1;
761 optionssize += 1;
762 }
763
764 options = (union all_known_options *)&optionsbuffer[optionssize];
765 options->as_end = OPTION_NUMBER_END;
766 optionssize += 1;
767
Faisal Latif6492cdf2008-07-24 20:50:45 -0700768 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800769 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800770 if (!skb) {
771 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
772 return -1;
773 }
774
775 if (sendack)
776 flags |= SET_ACK;
777
778 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
779 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
780
781 return ret;
782}
783
784
785/**
786 * send_reset
787 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700788static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800789{
790 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800791 int flags = SET_RST | SET_ACK;
792
Faisal Latif6492cdf2008-07-24 20:50:45 -0700793 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800794 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800795 if (!skb) {
796 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -0800797 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800798 }
799
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800800 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
801 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
802
803 return ret;
804}
805
806
807/**
808 * send_ack
809 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700810static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800811{
812 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700813
814 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800815 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800816
817 if (!skb) {
818 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
819 return -1;
820 }
821
822 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
823 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
824
825 return ret;
826}
827
828
829/**
830 * send_fin
831 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700832static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800833{
834 int ret;
835
836 /* if we didn't get a frame get one */
837 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800838 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800839
840 if (!skb) {
841 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
842 return -1;
843 }
844
845 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
846 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
847
848 return ret;
849}
850
851
852/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800853 * find_node - find a cm node that matches the reference cm node
854 */
855static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
856 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
857{
858 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800859 struct list_head *hte;
860 struct nes_cm_node *cm_node;
861
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800862 /* get a handle on the hte */
863 hte = &cm_core->connected_nodes;
864
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800865 /* walk list and find cm_node associated with this session ID */
866 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700867 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800868 /* compare quad, return node handle if a match */
869 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
870 cm_node->loc_addr, cm_node->loc_port,
871 loc_addr, loc_port,
872 cm_node->rem_addr, cm_node->rem_port,
873 rem_addr, rem_port);
874 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
875 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
876 add_ref_cm_node(cm_node);
877 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
878 return cm_node;
879 }
880 }
881 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
882
883 /* no owner node */
884 return NULL;
885}
886
887
888/**
889 * find_listener - find a cm node listening on this addr-port pair
890 */
891static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
892 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
893{
894 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800895 struct nes_cm_listener *listen_node;
896
897 /* walk list and find cm_node associated with this session ID */
898 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700899 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800900 /* compare node pair, return node handle if a match */
901 if (((listen_node->loc_addr == dst_addr) ||
902 listen_node->loc_addr == 0x00000000) &&
903 (listen_node->loc_port == dst_port) &&
904 (listener_state & listen_node->listener_state)) {
905 atomic_inc(&listen_node->ref_count);
906 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
907 return listen_node;
908 }
909 }
910 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
911
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800912 /* no listener */
913 return NULL;
914}
915
916
917/**
918 * add_hte_node - add a cm node to the hash table
919 */
920static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
921{
922 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800923 struct list_head *hte;
924
925 if (!cm_node || !cm_core)
926 return -EINVAL;
927
Faisal Latif6492cdf2008-07-24 20:50:45 -0700928 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
929 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800930
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800931 spin_lock_irqsave(&cm_core->ht_lock, flags);
932
933 /* get a handle on the hash table element (list head for this slot) */
934 hte = &cm_core->connected_nodes;
935 list_add_tail(&cm_node->list, hte);
936 atomic_inc(&cm_core->ht_node_cnt);
937
938 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
939
940 return 0;
941}
942
943
944/**
945 * mini_cm_dec_refcnt_listen
946 */
947static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700948 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800949{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800950 int ret = -EINVAL;
951 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800952 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700953 struct list_head *list_pos = NULL;
954 struct list_head *list_temp = NULL;
955 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -0600956 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700957
958 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
959 "refcnt=%d\n", listener, free_hanging_nodes,
960 atomic_read(&listener->ref_count));
961 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -0600962 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700963 if (free_hanging_nodes) {
964 spin_lock_irqsave(&cm_core->ht_lock, flags);
965 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600966 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700967 cm_node = container_of(list_pos, struct nes_cm_node,
968 list);
969 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -0600970 (!cm_node->accelerated)) {
971 add_ref_cm_node(cm_node);
972 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700973 }
974 }
975 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
976 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600977
978 list_for_each_safe(list_pos, list_temp, &reset_list) {
979 cm_node = container_of(list_pos, struct nes_cm_node,
Faisal Latif9d5ab132009-03-06 15:15:01 -0800980 reset_entry);
981 {
982 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -0800983 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800984 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
985 rem_ref_cm_node(cm_node->cm_core, cm_node);
986 } else {
987 if (!loopback) {
988 cleanup_retrans_entry(cm_node);
989 err = send_reset(cm_node, NULL);
990 if (err) {
991 cm_node->state =
992 NES_CM_STATE_CLOSED;
993 WARN_ON(1);
994 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -0800995 old_state = cm_node->state;
996 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
997 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
998 rem_ref_cm_node(
999 cm_node->cm_core,
1000 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001001 }
1002 } else {
1003 struct nes_cm_event event;
1004
1005 event.cm_node = loopback;
1006 event.cm_info.rem_addr =
1007 loopback->rem_addr;
1008 event.cm_info.loc_addr =
1009 loopback->loc_addr;
1010 event.cm_info.rem_port =
1011 loopback->rem_port;
1012 event.cm_info.loc_port =
1013 loopback->loc_port;
1014 event.cm_info.cm_id = loopback->cm_id;
Faisal Latif43093b92010-02-12 19:57:04 +00001015 add_ref_cm_node(loopback);
1016 loopback->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001017 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001018 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001019
Faisal Latif9d5ab132009-03-06 15:15:01 -08001020 rem_ref_cm_node(cm_node->cm_core,
1021 cm_node);
1022
1023 }
1024 }
1025 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001026 }
1027
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001028 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1029 if (!atomic_dec_return(&listener->ref_count)) {
1030 list_del(&listener->list);
1031
1032 /* decrement our listen node count */
1033 atomic_dec(&cm_core->listen_node_cnt);
1034
1035 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1036
1037 if (listener->nesvnic) {
1038 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1039 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1040 }
1041
1042 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1043
1044 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001045 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001046 ret = 0;
Faisal Latif6e10d2e2010-02-12 19:55:03 +00001047 atomic_inc(&cm_listens_destroyed);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001048 } else {
1049 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1050 }
1051 if (listener) {
1052 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1053 nes_debug(NES_DBG_CM, "destroying listener (%p)"
1054 " with non-zero pending accepts=%u\n",
1055 listener, atomic_read(&listener->pend_accepts_cnt));
1056 }
1057
1058 return ret;
1059}
1060
1061
1062/**
1063 * mini_cm_del_listen
1064 */
1065static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1066 struct nes_cm_listener *listener)
1067{
1068 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1069 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1070 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1071}
1072
1073
1074/**
1075 * mini_cm_accelerated
1076 */
1077static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1078 struct nes_cm_node *cm_node)
1079{
1080 u32 was_timer_set;
1081 cm_node->accelerated = 1;
1082
1083 if (cm_node->accept_pend) {
1084 BUG_ON(!cm_node->listener);
1085 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001086 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001087 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1088 }
1089
1090 was_timer_set = timer_pending(&cm_core->tcp_timer);
1091 if (!was_timer_set) {
1092 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1093 add_timer(&cm_core->tcp_timer);
1094 }
1095
1096 return 0;
1097}
1098
1099
1100/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001101 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001102 */
Faisal Latif7a576df2009-12-09 15:54:33 -08001103static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001104{
1105 struct rtable *rt;
1106 struct flowi fl;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001107 struct neighbour *neigh;
Faisal Latif7a576df2009-12-09 15:54:33 -08001108 int rc = arpindex;
1109 struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001110
1111 memset(&fl, 0, sizeof fl);
1112 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1113 if (ip_route_output_key(&init_net, &rt, &fl)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001114 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001115 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001116 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001117 }
1118
Bob Sharp7191a0a2008-10-03 12:21:19 -07001119 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1120 if (neigh) {
1121 if (neigh->nud_state & NUD_VALID) {
1122 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001123 " is %pM, Gateway is 0x%08X \n", dst_ip,
1124 neigh->ha, ntohl(rt->rt_gateway));
Faisal Latif7a576df2009-12-09 15:54:33 -08001125
1126 if (arpindex >= 0) {
1127 if (!memcmp(nesadapter->arp_table[arpindex].mac_addr,
1128 neigh->ha, ETH_ALEN)){
1129 /* Mac address same as in nes_arp_table */
1130 neigh_release(neigh);
1131 ip_rt_put(rt);
1132 return rc;
1133 }
1134
1135 nes_manage_arp_cache(nesvnic->netdev,
1136 nesadapter->arp_table[arpindex].mac_addr,
1137 dst_ip, NES_ARP_DELETE);
1138 }
1139
Bob Sharp7191a0a2008-10-03 12:21:19 -07001140 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1141 dst_ip, NES_ARP_ADD);
1142 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1143 NES_ARP_RESOLVE);
1144 }
1145 neigh_release(neigh);
1146 }
1147
1148 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1149 neigh_event_send(rt->u.dst.neighbour, NULL);
1150
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001151 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001152 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001153}
1154
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001155/**
1156 * make_cm_node - create a new instance of a cm node
1157 */
1158static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1159 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1160 struct nes_cm_listener *listener)
1161{
1162 struct nes_cm_node *cm_node;
1163 struct timespec ts;
Faisal Latif7a576df2009-12-09 15:54:33 -08001164 int oldarpindex = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001165 int arpindex = 0;
1166 struct nes_device *nesdev;
1167 struct nes_adapter *nesadapter;
1168
1169 /* create an hte and cm_node for this instance */
1170 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1171 if (!cm_node)
1172 return NULL;
1173
1174 /* set our node specific transport info */
1175 cm_node->loc_addr = cm_info->loc_addr;
1176 cm_node->rem_addr = cm_info->rem_addr;
1177 cm_node->loc_port = cm_info->loc_port;
1178 cm_node->rem_port = cm_info->rem_port;
1179 cm_node->send_write0 = send_first;
Harvey Harrison63779432008-10-31 00:56:00 -07001180 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1181 &cm_node->loc_addr, cm_node->loc_port,
1182 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001183 cm_node->listener = listener;
1184 cm_node->netdev = nesvnic->netdev;
1185 cm_node->cm_id = cm_info->cm_id;
1186 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1187
Faisal Latif6492cdf2008-07-24 20:50:45 -07001188 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1189 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001190
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001191 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001192
1193 cm_node->loopbackpartner = NULL;
1194 atomic_set(&cm_node->ref_count, 1);
1195 /* associate our parent CM core */
1196 cm_node->cm_core = cm_core;
1197 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1198 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1199 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1200 NES_CM_DEFAULT_RCV_WND_SCALE;
1201 ts = current_kernel_time();
1202 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1203 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Chien Tungf2b2b592008-03-20 19:55:30 -05001204 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001205 cm_node->tcp_cntxt.rcv_nxt = 0;
1206 /* get a unique session ID , add thread_id to an upcounter to handle race */
1207 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001208 cm_node->conn_type = cm_info->conn_type;
1209 cm_node->apbvt_set = 0;
1210 cm_node->accept_pend = 0;
1211
1212 cm_node->nesvnic = nesvnic;
1213 /* get some device handles, for arp lookup */
1214 nesdev = nesvnic->nesdev;
1215 nesadapter = nesdev->nesadapter;
1216
1217 cm_node->loopbackpartner = NULL;
Faisal Latif7a576df2009-12-09 15:54:33 -08001218
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001219 /* get the mac addr for the remote node */
Chien Tung1ee86552008-11-21 20:51:04 -06001220 if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1221 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
Faisal Latif7a576df2009-12-09 15:54:33 -08001222 else {
1223 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1224 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);
1225
1226 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001227 if (arpindex < 0) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001228 kfree(cm_node);
1229 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001230 }
1231
1232 /* copy the mac addr to node context */
1233 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001234 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1235 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001236
1237 add_hte_node(cm_core, cm_node);
1238 atomic_inc(&cm_nodes_created);
1239
1240 return cm_node;
1241}
1242
1243
1244/**
1245 * add_ref_cm_node - destroy an instance of a cm node
1246 */
1247static int add_ref_cm_node(struct nes_cm_node *cm_node)
1248{
1249 atomic_inc(&cm_node->ref_count);
1250 return 0;
1251}
1252
1253
1254/**
1255 * rem_ref_cm_node - destroy an instance of a cm node
1256 */
1257static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001258 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001259{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001260 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001261 struct nes_qp *nesqp;
1262
1263 if (!cm_node)
1264 return -EINVAL;
1265
1266 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1267 if (atomic_dec_return(&cm_node->ref_count)) {
1268 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1269 return 0;
1270 }
1271 list_del(&cm_node->list);
1272 atomic_dec(&cm_core->ht_node_cnt);
1273 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1274
1275 /* if the node is destroyed before connection was accelerated */
1276 if (!cm_node->accelerated && cm_node->accept_pend) {
1277 BUG_ON(!cm_node->listener);
1278 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1279 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1280 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001281 WARN_ON(cm_node->send_entry);
1282 if (cm_node->recv_entry)
1283 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001284 if (cm_node->listener) {
1285 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1286 } else {
1287 if (cm_node->apbvt_set && cm_node->nesvnic) {
1288 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001289 PCI_FUNC(
1290 cm_node->nesvnic->nesdev->pcidev->devfn),
1291 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001292 }
1293 }
1294
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001295 atomic_dec(&cm_core->node_cnt);
1296 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001297 nesqp = cm_node->nesqp;
1298 if (nesqp) {
1299 nesqp->cm_node = NULL;
1300 nes_rem_ref(&nesqp->ibqp);
1301 cm_node->nesqp = NULL;
1302 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001303
Faisal Latif6492cdf2008-07-24 20:50:45 -07001304 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001305 return 0;
1306}
1307
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001308/**
1309 * process_options
1310 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001311static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1312 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001313{
1314 u32 tmp;
1315 u32 offset = 0;
1316 union all_known_options *all_options;
1317 char got_mss_option = 0;
1318
1319 while (offset < optionsize) {
1320 all_options = (union all_known_options *)(optionsloc + offset);
1321 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001322 case OPTION_NUMBER_END:
1323 offset = optionsize;
1324 break;
1325 case OPTION_NUMBER_NONE:
1326 offset += 1;
1327 continue;
1328 case OPTION_NUMBER_MSS:
1329 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1330 "Size: %d\n", __func__,
1331 all_options->as_mss.length, offset, optionsize);
1332 got_mss_option = 1;
1333 if (all_options->as_mss.length != 4) {
1334 return 1;
1335 } else {
1336 tmp = ntohs(all_options->as_mss.mss);
1337 if (tmp > 0 && tmp <
1338 cm_node->tcp_cntxt.mss)
1339 cm_node->tcp_cntxt.mss = tmp;
1340 }
1341 break;
1342 case OPTION_NUMBER_WINDOW_SCALE:
1343 cm_node->tcp_cntxt.snd_wscale =
1344 all_options->as_windowscale.shiftcount;
1345 break;
1346 case OPTION_NUMBER_WRITE0:
1347 cm_node->send_write0 = 1;
1348 break;
1349 default:
1350 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1351 all_options->as_base.optionnum);
1352 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001353 }
1354 offset += all_options->as_base.length;
1355 }
1356 if ((!got_mss_option) && (syn_packet))
1357 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1358 return 0;
1359}
1360
Faisal Latif6492cdf2008-07-24 20:50:45 -07001361static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001362{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001363 atomic_inc(&cm_accel_dropped_pkts);
1364 dev_kfree_skb_any(skb);
1365}
1366
Faisal Latif9d5ab132009-03-06 15:15:01 -08001367static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001368{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001369 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1370 "refcnt=%d\n", cm_node, cm_node->state,
1371 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001372 switch (cm_node->state) {
1373 case NES_CM_STATE_SYN_RCVD:
1374 case NES_CM_STATE_SYN_SENT:
1375 case NES_CM_STATE_ESTABLISHED:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001376 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001377 cm_node->tcp_cntxt.rcv_nxt++;
1378 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001379 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001380 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001381 break;
Faisal Latifb1190d32009-12-09 15:54:32 -08001382 case NES_CM_STATE_MPAREQ_SENT:
1383 create_event(cm_node, NES_CM_EVENT_ABORTED);
1384 cm_node->tcp_cntxt.rcv_nxt++;
1385 cleanup_retrans_entry(cm_node);
1386 cm_node->state = NES_CM_STATE_CLOSED;
1387 add_ref_cm_node(cm_node);
1388 send_reset(cm_node, NULL);
1389 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001390 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001391 cm_node->tcp_cntxt.rcv_nxt++;
1392 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001393 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001394 send_ack(cm_node, NULL);
1395 /* Wait for ACK as this is simultanous close..
1396 * After we receive ACK, do not send anything..
1397 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001398 break;
1399 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001400 cm_node->tcp_cntxt.rcv_nxt++;
1401 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001402 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001403 send_ack(cm_node, NULL);
1404 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1405 break;
1406 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001407 cm_node->tcp_cntxt.rcv_nxt++;
1408 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001409 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001410 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001411 break;
1412 case NES_CM_STATE_TSA:
1413 default:
1414 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1415 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001416 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001417 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001418}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001419
Faisal Latif6492cdf2008-07-24 20:50:45 -07001420
1421static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1422 struct tcphdr *tcph)
1423{
1424
1425 int reset = 0; /* whether to send reset in case of err.. */
Faisal Latif183ecfa2008-11-21 20:50:46 -06001426 int passive_state;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001427 atomic_inc(&cm_resets_recvd);
1428 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1429 " refcnt=%d\n", cm_node, cm_node->state,
1430 atomic_read(&cm_node->ref_count));
1431 cleanup_retrans_entry(cm_node);
1432 switch (cm_node->state) {
1433 case NES_CM_STATE_SYN_SENT:
1434 case NES_CM_STATE_MPAREQ_SENT:
1435 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1436 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1437 cm_node->listener, cm_node->state);
1438 active_open_err(cm_node, skb, reset);
1439 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001440 case NES_CM_STATE_MPAREQ_RCVD:
1441 passive_state = atomic_add_return(1, &cm_node->passive_state);
1442 if (passive_state == NES_SEND_RESET_EVENT)
1443 create_event(cm_node, NES_CM_EVENT_RESET);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001444 cm_node->state = NES_CM_STATE_CLOSED;
1445 dev_kfree_skb_any(skb);
1446 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001447 case NES_CM_STATE_ESTABLISHED:
1448 case NES_CM_STATE_SYN_RCVD:
1449 case NES_CM_STATE_LISTENING:
1450 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1451 passive_open_err(cm_node, skb, reset);
1452 break;
1453 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001454 active_open_err(cm_node, skb, reset);
1455 break;
1456 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001457 drop_packet(skb);
1458 break;
Faisal Latif68237a02009-06-22 22:53:28 -07001459 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001460 case NES_CM_STATE_LAST_ACK:
1461 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001462 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001463 cm_node->state = NES_CM_STATE_CLOSED;
1464 rem_ref_cm_node(cm_node->cm_core, cm_node);
1465 drop_packet(skb);
1466 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001467 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001468 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001469 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001470 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001471}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001472
Faisal Latif9d5ab132009-03-06 15:15:01 -08001473
1474static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001475{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001476
Faisal Latif9d5ab132009-03-06 15:15:01 -08001477 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001478 int datasize = skb->len;
1479 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001480
1481 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1482 u32 res_type;
1483 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1484 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001485 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001486 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001487 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1488 "cm_node=%p listener=%p state=%d\n", __func__,
1489 __LINE__, cm_node, cm_node->listener,
1490 cm_node->state);
1491 active_open_err(cm_node, skb, 1);
1492 } else {
1493 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001494 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001495 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001496 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001497
1498 switch (cm_node->state) {
1499 case NES_CM_STATE_ESTABLISHED:
1500 if (res_type == NES_MPA_REQUEST_REJECT) {
1501 /*BIG problem as we are receiving the MPA.. So should
1502 * not be REJECT.. This is Passive Open.. We can
1503 * only receive it Reject for Active Open...*/
1504 WARN_ON(1);
1505 }
1506 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1507 type = NES_CM_EVENT_MPA_REQ;
1508 atomic_set(&cm_node->passive_state,
1509 NES_PASSIVE_STATE_INDICATED);
1510 break;
1511 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001512 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001513 if (res_type == NES_MPA_REQUEST_REJECT) {
1514 type = NES_CM_EVENT_MPA_REJECT;
1515 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1516 } else {
1517 type = NES_CM_EVENT_CONNECTED;
1518 cm_node->state = NES_CM_STATE_TSA;
1519 }
1520
1521 break;
1522 default:
1523 WARN_ON(1);
1524 break;
1525 }
1526 dev_kfree_skb_any(skb);
1527 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001528}
1529
1530static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1531{
1532 switch (cm_node->state) {
1533 case NES_CM_STATE_SYN_SENT:
1534 case NES_CM_STATE_MPAREQ_SENT:
1535 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1536 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1537 cm_node->listener, cm_node->state);
1538 active_open_err(cm_node, skb, 1);
1539 break;
1540 case NES_CM_STATE_ESTABLISHED:
1541 case NES_CM_STATE_SYN_RCVD:
1542 passive_open_err(cm_node, skb, 1);
1543 break;
1544 case NES_CM_STATE_TSA:
1545 default:
1546 drop_packet(skb);
1547 }
1548}
1549
1550static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1551 struct sk_buff *skb)
1552{
1553 int err;
1554
1555 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1556 if (err)
1557 active_open_err(cm_node, skb, 1);
1558
1559 return err;
1560}
1561
1562static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1563 struct sk_buff *skb)
1564{
1565 int err = 0;
1566 u32 seq;
1567 u32 ack_seq;
1568 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1569 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1570 u32 rcv_wnd;
1571 seq = ntohl(tcph->seq);
1572 ack_seq = ntohl(tcph->ack_seq);
1573 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1574 if (ack_seq != loc_seq_num)
1575 err = 1;
Faisal Latifc11470f2009-04-27 13:38:31 -07001576 else if (!between(seq, rcv_nxt, (rcv_nxt+rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001577 err = 1;
1578 if (err) {
1579 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1580 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1581 cm_node->listener, cm_node->state);
1582 indicate_pkt_err(cm_node, skb);
1583 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1584 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1585 rcv_wnd);
1586 }
1587 return err;
1588}
1589
1590/*
1591 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1592 * is created with a listener or it may comein as rexmitted packet which in
1593 * that case will be just dropped.
1594 */
1595
1596static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1597 struct tcphdr *tcph)
1598{
1599 int ret;
1600 u32 inc_sequence;
1601 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001602
1603 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001604 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001605 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001606
Faisal Latif6492cdf2008-07-24 20:50:45 -07001607 switch (cm_node->state) {
1608 case NES_CM_STATE_SYN_SENT:
1609 case NES_CM_STATE_MPAREQ_SENT:
1610 /* Rcvd syn on active open connection*/
1611 active_open_err(cm_node, skb, 1);
1612 break;
1613 case NES_CM_STATE_LISTENING:
1614 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001615 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1616 cm_node->listener->backlog) {
1617 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1618 "pressure \n");
1619 cm_backlog_drops++;
1620 passive_open_err(cm_node, skb, 0);
1621 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001622 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001623 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1624 1);
1625 if (ret) {
1626 passive_open_err(cm_node, skb, 0);
1627 /* drop pkt */
1628 break;
1629 }
1630 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1631 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001632 cm_node->accept_pend = 1;
1633 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1634
Faisal Latif6492cdf2008-07-24 20:50:45 -07001635 cm_node->state = NES_CM_STATE_SYN_RCVD;
1636 send_syn(cm_node, 1, skb);
1637 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001638 case NES_CM_STATE_CLOSED:
1639 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001640 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001641 send_reset(cm_node, skb);
1642 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001643 case NES_CM_STATE_TSA:
1644 case NES_CM_STATE_ESTABLISHED:
1645 case NES_CM_STATE_FIN_WAIT1:
1646 case NES_CM_STATE_FIN_WAIT2:
1647 case NES_CM_STATE_MPAREQ_RCVD:
1648 case NES_CM_STATE_LAST_ACK:
1649 case NES_CM_STATE_CLOSING:
1650 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001651 default:
1652 drop_packet(skb);
1653 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001654 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001655}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001656
Faisal Latif6492cdf2008-07-24 20:50:45 -07001657static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1658 struct tcphdr *tcph)
1659{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001660
Faisal Latif6492cdf2008-07-24 20:50:45 -07001661 int ret;
1662 u32 inc_sequence;
1663 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001664
Faisal Latif6492cdf2008-07-24 20:50:45 -07001665 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001666 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001667 inc_sequence = ntohl(tcph->seq);
1668 switch (cm_node->state) {
1669 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001670 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001671 /* active open */
1672 if (check_syn(cm_node, tcph, skb))
1673 return;
1674 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1675 /* setup options */
1676 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1677 if (ret) {
1678 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1679 cm_node);
1680 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001681 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001682 cleanup_retrans_entry(cm_node);
1683 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1684 send_mpa_request(cm_node, skb);
1685 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1686 break;
1687 case NES_CM_STATE_MPAREQ_RCVD:
1688 /* passive open, so should not be here */
1689 passive_open_err(cm_node, skb, 1);
1690 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001691 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001692 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1693 cleanup_retrans_entry(cm_node);
1694 cm_node->state = NES_CM_STATE_CLOSED;
1695 send_reset(cm_node, skb);
1696 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001697 case NES_CM_STATE_CLOSED:
1698 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1699 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001700 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001701 send_reset(cm_node, skb);
1702 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001703 case NES_CM_STATE_ESTABLISHED:
1704 case NES_CM_STATE_FIN_WAIT1:
1705 case NES_CM_STATE_FIN_WAIT2:
1706 case NES_CM_STATE_LAST_ACK:
1707 case NES_CM_STATE_TSA:
1708 case NES_CM_STATE_CLOSING:
1709 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001710 case NES_CM_STATE_MPAREQ_SENT:
1711 default:
1712 drop_packet(skb);
1713 break;
1714 }
1715}
1716
Faisal Latif109d67e2009-04-27 13:41:06 -07001717static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001718 struct tcphdr *tcph)
1719{
1720 int datasize = 0;
1721 u32 inc_sequence;
Faisal Latif109d67e2009-04-27 13:41:06 -07001722 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06001723 int optionsize;
Faisal Latifabb77252008-11-21 20:50:52 -06001724 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001725
Faisal Latif6492cdf2008-07-24 20:50:45 -07001726 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07001727 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001728
1729 skb_pull(skb, tcph->doff << 2);
1730 inc_sequence = ntohl(tcph->seq);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001731 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001732 switch (cm_node->state) {
1733 case NES_CM_STATE_SYN_RCVD:
1734 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07001735 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001736 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1737 if (ret)
1738 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001739 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1740 cm_node->state = NES_CM_STATE_ESTABLISHED;
1741 if (datasize) {
1742 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001743 handle_rcv_mpa(cm_node, skb);
Faisal Latif109d67e2009-04-27 13:41:06 -07001744 } else /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001745 dev_kfree_skb_any(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001746 break;
1747 case NES_CM_STATE_ESTABLISHED:
1748 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001749 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001750 if (datasize) {
1751 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001752 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001753 } else
1754 drop_packet(skb);
1755 break;
1756 case NES_CM_STATE_MPAREQ_SENT:
1757 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1758 if (datasize) {
1759 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001760 handle_rcv_mpa(cm_node, skb);
Faisal Latif109d67e2009-04-27 13:41:06 -07001761 } else /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001762 dev_kfree_skb_any(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001763 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001764 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001765 cleanup_retrans_entry(cm_node);
1766 cm_node->state = NES_CM_STATE_CLOSED;
1767 send_reset(cm_node, skb);
1768 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001769 case NES_CM_STATE_CLOSED:
1770 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001771 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001772 send_reset(cm_node, skb);
1773 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001774 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07001775 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001776 cleanup_retrans_entry(cm_node);
1777 cm_node->state = NES_CM_STATE_CLOSED;
1778 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001779 rem_ref_cm_node(cm_node->cm_core, cm_node);
1780 drop_packet(skb);
1781 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001782 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001783 cleanup_retrans_entry(cm_node);
1784 drop_packet(skb);
1785 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1786 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001787 case NES_CM_STATE_SYN_SENT:
1788 case NES_CM_STATE_FIN_WAIT2:
1789 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001790 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001791 case NES_CM_STATE_UNKNOWN:
1792 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07001793 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001794 drop_packet(skb);
1795 break;
1796 }
Faisal Latif109d67e2009-04-27 13:41:06 -07001797 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001798}
1799
1800
1801
1802static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1803 struct sk_buff *skb, int optionsize, int passive)
1804{
1805 u8 *optionsloc = (u8 *)&tcph[1];
1806 if (optionsize) {
1807 if (process_options(cm_node, optionsloc, optionsize,
1808 (u32)tcph->syn)) {
1809 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1810 __func__, cm_node);
1811 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06001812 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001813 else
Faisal Latifabb77252008-11-21 20:50:52 -06001814 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001815 return 1;
1816 }
1817 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001818
1819 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1820 cm_node->tcp_cntxt.snd_wscale;
1821
Faisal Latif6492cdf2008-07-24 20:50:45 -07001822 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001823 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001824 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001825}
1826
Faisal Latif6492cdf2008-07-24 20:50:45 -07001827/*
1828 * active_open_err() will send reset() if flag set..
1829 * It will also send ABORT event.
1830 */
1831
1832static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1833 int reset)
1834{
1835 cleanup_retrans_entry(cm_node);
1836 if (reset) {
1837 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1838 "state=%d\n", cm_node, cm_node->state);
1839 add_ref_cm_node(cm_node);
1840 send_reset(cm_node, skb);
1841 } else
1842 dev_kfree_skb_any(skb);
1843
1844 cm_node->state = NES_CM_STATE_CLOSED;
1845 create_event(cm_node, NES_CM_EVENT_ABORTED);
1846}
1847
1848/*
1849 * passive_open_err() will either do a reset() or will free up the skb and
1850 * remove the cm_node.
1851 */
1852
1853static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1854 int reset)
1855{
1856 cleanup_retrans_entry(cm_node);
1857 cm_node->state = NES_CM_STATE_CLOSED;
1858 if (reset) {
1859 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1860 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1861 send_reset(cm_node, skb);
1862 } else {
1863 dev_kfree_skb_any(skb);
1864 rem_ref_cm_node(cm_node->cm_core, cm_node);
1865 }
1866}
1867
1868/*
1869 * free_retrans_entry() routines assumes that the retrans_list_lock has
1870 * been acquired before calling.
1871 */
1872static void free_retrans_entry(struct nes_cm_node *cm_node)
1873{
1874 struct nes_timer_entry *send_entry;
1875 send_entry = cm_node->send_entry;
1876 if (send_entry) {
1877 cm_node->send_entry = NULL;
1878 dev_kfree_skb_any(send_entry->skb);
1879 kfree(send_entry);
1880 rem_ref_cm_node(cm_node->cm_core, cm_node);
1881 }
1882}
1883
1884static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1885{
1886 unsigned long flags;
1887
1888 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1889 free_retrans_entry(cm_node);
1890 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1891}
1892
1893/**
1894 * process_packet
1895 * Returns skb if to be freed, else it will return NULL if already used..
1896 */
1897static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1898 struct nes_cm_core *cm_core)
1899{
1900 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1901 struct tcphdr *tcph = tcp_hdr(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001902 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07001903 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001904 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1905
1906 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1907 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1908 tcph->ack, tcph->rst, tcph->fin);
1909
1910 if (tcph->rst)
1911 pkt_type = NES_PKT_TYPE_RST;
1912 else if (tcph->syn) {
1913 pkt_type = NES_PKT_TYPE_SYN;
1914 if (tcph->ack)
1915 pkt_type = NES_PKT_TYPE_SYNACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001916 } else if (tcph->ack)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001917 pkt_type = NES_PKT_TYPE_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001918 if (tcph->fin)
1919 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001920
1921 switch (pkt_type) {
1922 case NES_PKT_TYPE_SYN:
1923 handle_syn_pkt(cm_node, skb, tcph);
1924 break;
1925 case NES_PKT_TYPE_SYNACK:
1926 handle_synack_pkt(cm_node, skb, tcph);
1927 break;
1928 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07001929 ret = handle_ack_pkt(cm_node, skb, tcph);
1930 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001931 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001932 break;
1933 case NES_PKT_TYPE_RST:
1934 handle_rst_pkt(cm_node, skb, tcph);
1935 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001936 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07001937 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08001938 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07001939 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001940 break;
1941 }
1942}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001943
1944/**
1945 * mini_cm_listen - create a listen node with params
1946 */
1947static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001948 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001949{
1950 struct nes_cm_listener *listener;
1951 unsigned long flags;
1952
1953 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1954 cm_info->loc_addr, cm_info->loc_port);
1955
1956 /* cannot have multiple matching listeners */
1957 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1958 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1959 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1960 /* find automatically incs ref count ??? */
1961 atomic_dec(&listener->ref_count);
1962 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1963 return NULL;
1964 }
1965
1966 if (!listener) {
1967 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1968 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1969 if (!listener) {
1970 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1971 return NULL;
1972 }
1973
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001974 listener->loc_addr = htonl(cm_info->loc_addr);
1975 listener->loc_port = htons(cm_info->loc_port);
1976 listener->reused_node = 0;
1977
1978 atomic_set(&listener->ref_count, 1);
1979 }
1980 /* pasive case */
1981 /* find already inc'ed the ref count */
1982 else {
1983 listener->reused_node = 1;
1984 }
1985
1986 listener->cm_id = cm_info->cm_id;
1987 atomic_set(&listener->pend_accepts_cnt, 0);
1988 listener->cm_core = cm_core;
1989 listener->nesvnic = nesvnic;
1990 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001991
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001992 listener->conn_type = cm_info->conn_type;
1993 listener->backlog = cm_info->backlog;
1994 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1995
1996 if (!listener->reused_node) {
1997 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1998 list_add(&listener->list, &cm_core->listen_list.list);
1999 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2000 atomic_inc(&cm_core->listen_node_cnt);
2001 }
2002
2003 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
2004 " listener = %p, backlog = %d, cm_id = %p.\n",
2005 cm_info->loc_addr, cm_info->loc_port,
2006 listener, listener->backlog, listener->cm_id);
2007
2008 return listener;
2009}
2010
2011
2012/**
2013 * mini_cm_connect - make a connection node with params
2014 */
Faisal Latif54c86a82008-09-30 14:47:27 -07002015static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002016 struct nes_vnic *nesvnic, u16 private_data_len,
2017 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002018{
2019 int ret = 0;
2020 struct nes_cm_node *cm_node;
2021 struct nes_cm_listener *loopbackremotelistener;
2022 struct nes_cm_node *loopbackremotenode;
2023 struct nes_cm_info loopback_cm_info;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002024 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
2025 struct ietf_mpa_frame *mpa_frame = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002026
2027 /* create a CM connection node */
2028 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2029 if (!cm_node)
2030 return NULL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002031 mpa_frame = &cm_node->mpa_frame;
Faisal Latif1cf078c2009-12-09 15:53:54 -08002032 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002033 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
2034 mpa_frame->rev = IETF_MPA_VERSION;
2035 mpa_frame->priv_data_len = htons(private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002036
Glenn Streiff7495ab62008-04-29 13:46:54 -07002037 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002038 cm_node->tcp_cntxt.client = 1;
2039 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2040
2041 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002042 loopbackremotelistener = find_listener(cm_core,
2043 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2044 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002045 if (loopbackremotelistener == NULL) {
2046 create_event(cm_node, NES_CM_EVENT_ABORTED);
2047 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002048 loopback_cm_info = *cm_info;
2049 loopback_cm_info.loc_port = cm_info->rem_port;
2050 loopback_cm_info.rem_port = cm_info->loc_port;
2051 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002052 loopbackremotenode = make_cm_node(cm_core, nesvnic,
2053 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002054 if (!loopbackremotenode) {
2055 rem_ref_cm_node(cm_node->cm_core, cm_node);
2056 return NULL;
2057 }
2058 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002059 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002060 loopbackremotenode->tcp_cntxt.rcv_wscale =
2061 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002062 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002063 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2064 private_data_len);
2065 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002066
Faisal Latif6492cdf2008-07-24 20:50:45 -07002067 /* we are done handling this state. */
2068 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002069 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002070 cm_node->tcp_cntxt.rcv_nxt =
2071 loopbackremotenode->tcp_cntxt.loc_seq_num;
2072 loopbackremotenode->tcp_cntxt.rcv_nxt =
2073 cm_node->tcp_cntxt.loc_seq_num;
2074 cm_node->tcp_cntxt.max_snd_wnd =
2075 loopbackremotenode->tcp_cntxt.rcv_wnd;
2076 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2077 cm_node->tcp_cntxt.rcv_wnd;
2078 cm_node->tcp_cntxt.snd_wnd =
2079 loopbackremotenode->tcp_cntxt.rcv_wnd;
2080 loopbackremotenode->tcp_cntxt.snd_wnd =
2081 cm_node->tcp_cntxt.rcv_wnd;
2082 cm_node->tcp_cntxt.snd_wscale =
2083 loopbackremotenode->tcp_cntxt.rcv_wscale;
2084 loopbackremotenode->tcp_cntxt.snd_wscale =
2085 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002086 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002087 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2088 }
2089 return cm_node;
2090 }
2091
2092 /* set our node side to client (active) side */
2093 cm_node->tcp_cntxt.client = 1;
2094 /* init our MPA frame ptr */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002095 memcpy(mpa_frame->priv_data, private_data, private_data_len);
2096
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002097 cm_node->mpa_frame_size = mpa_frame_size;
2098
2099 /* send a syn and goto syn sent state */
2100 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002101 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002102
Faisal Latif6492cdf2008-07-24 20:50:45 -07002103 if (ret) {
2104 /* error in sending the syn free up the cm_node struct */
2105 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2106 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2107 cm_node->rem_addr, cm_node->rem_port, cm_node,
2108 cm_node->cm_id);
2109 rem_ref_cm_node(cm_node->cm_core, cm_node);
2110 cm_node = NULL;
2111 }
2112
2113 if (cm_node)
2114 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2115 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2116 cm_node->rem_addr, cm_node->rem_port, cm_node,
2117 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002118
2119 return cm_node;
2120}
2121
2122
2123/**
2124 * mini_cm_accept - accept a connection
2125 * This function is never called
2126 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002127static int mini_cm_accept(struct nes_cm_core *cm_core,
2128 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002129{
2130 return 0;
2131}
2132
2133
2134/**
2135 * mini_cm_reject - reject and teardown a connection
2136 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002137static int mini_cm_reject(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002138 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002139{
2140 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002141 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002142 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002143 struct nes_cm_event event;
2144 struct iw_cm_id *cm_id = cm_node->cm_id;
2145 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002146
Faisal Latif6492cdf2008-07-24 20:50:45 -07002147 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2148 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002149
Faisal Latif6492cdf2008-07-24 20:50:45 -07002150 if (cm_node->tcp_cntxt.client)
2151 return ret;
2152 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002153
Faisal Latif9d5ab132009-03-06 15:15:01 -08002154 if (!loopback) {
2155 passive_state = atomic_add_return(1, &cm_node->passive_state);
2156 if (passive_state == NES_SEND_RESET_EVENT) {
2157 cm_node->state = NES_CM_STATE_CLOSED;
2158 rem_ref_cm_node(cm_core, cm_node);
2159 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002160 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2161 rem_ref_cm_node(cm_core, cm_node);
2162 } else {
2163 ret = send_mpa_reject(cm_node);
2164 if (ret) {
2165 cm_node->state = NES_CM_STATE_CLOSED;
2166 err = send_reset(cm_node, NULL);
2167 if (err)
2168 WARN_ON(1);
2169 } else
2170 cm_id->add_ref(cm_id);
2171 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002172 }
2173 } else {
2174 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002175 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2176 rem_ref_cm_node(cm_core, cm_node);
2177 rem_ref_cm_node(cm_core, loopback);
2178 } else {
2179 event.cm_node = loopback;
2180 event.cm_info.rem_addr = loopback->rem_addr;
2181 event.cm_info.loc_addr = loopback->loc_addr;
2182 event.cm_info.rem_port = loopback->rem_port;
2183 event.cm_info.loc_port = loopback->loc_port;
2184 event.cm_info.cm_id = loopback->cm_id;
2185 cm_event_mpa_reject(&event);
2186 rem_ref_cm_node(cm_core, cm_node);
2187 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002188
Faisal Latifc5a7d482009-12-09 15:54:08 -08002189 cm_id = loopback->cm_id;
2190 rem_ref_cm_node(cm_core, loopback);
2191 cm_id->rem_ref(cm_id);
2192 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002193 }
2194
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002195 return ret;
2196}
2197
2198
2199/**
2200 * mini_cm_close
2201 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002202static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002203{
2204 int ret = 0;
2205
2206 if (!cm_core || !cm_node)
2207 return -EINVAL;
2208
2209 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002210 case NES_CM_STATE_SYN_RCVD:
2211 case NES_CM_STATE_SYN_SENT:
2212 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2213 case NES_CM_STATE_ESTABLISHED:
2214 case NES_CM_STATE_ACCEPTING:
2215 case NES_CM_STATE_MPAREQ_SENT:
2216 case NES_CM_STATE_MPAREQ_RCVD:
2217 cleanup_retrans_entry(cm_node);
2218 send_reset(cm_node, NULL);
2219 break;
2220 case NES_CM_STATE_CLOSE_WAIT:
2221 cm_node->state = NES_CM_STATE_LAST_ACK;
2222 send_fin(cm_node, NULL);
2223 break;
2224 case NES_CM_STATE_FIN_WAIT1:
2225 case NES_CM_STATE_FIN_WAIT2:
2226 case NES_CM_STATE_LAST_ACK:
2227 case NES_CM_STATE_TIME_WAIT:
2228 case NES_CM_STATE_CLOSING:
2229 ret = -1;
2230 break;
2231 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002232 cleanup_retrans_entry(cm_node);
2233 send_reset(cm_node, NULL);
2234 break;
2235 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002236 case NES_CM_STATE_UNKNOWN:
2237 case NES_CM_STATE_INITED:
2238 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002239 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002240 ret = rem_ref_cm_node(cm_core, cm_node);
2241 break;
2242 case NES_CM_STATE_TSA:
2243 if (cm_node->send_entry)
2244 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2245 "send_entry=%p\n", cm_node->send_entry);
2246 ret = rem_ref_cm_node(cm_core, cm_node);
2247 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002248 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002249 return ret;
2250}
2251
2252
2253/**
2254 * recv_pkt - recv an ETHERNET packet, and process it through CM
2255 * node state machine
2256 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002257static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002258 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002259{
2260 struct nes_cm_node *cm_node = NULL;
2261 struct nes_cm_listener *listener = NULL;
2262 struct iphdr *iph;
2263 struct tcphdr *tcph;
2264 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002265 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002266 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002267
Faisal Latif6492cdf2008-07-24 20:50:45 -07002268 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002269 return 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002270 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002271 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002272 }
2273
2274 iph = (struct iphdr *)skb->data;
2275 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002276
2277 nfo.loc_addr = ntohl(iph->daddr);
2278 nfo.loc_port = ntohs(tcph->dest);
2279 nfo.rem_addr = ntohl(iph->saddr);
2280 nfo.rem_port = ntohs(tcph->source);
2281
Harvey Harrison03080e52009-01-10 21:45:42 -08002282 tmp_daddr = cpu_to_be32(iph->daddr);
2283 tmp_saddr = cpu_to_be32(iph->saddr);
2284
Harvey Harrison63779432008-10-31 00:56:00 -07002285 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002286 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002287
Faisal Latif6492cdf2008-07-24 20:50:45 -07002288 do {
2289 cm_node = find_node(cm_core,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002290 nfo.rem_port, nfo.rem_addr,
2291 nfo.loc_port, nfo.loc_addr);
2292
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002293 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002294 /* Only type of packet accepted are for */
2295 /* the PASSIVE open (syn only) */
2296 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002297 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002298 break;
2299 }
2300 listener = find_listener(cm_core, nfo.loc_addr,
2301 nfo.loc_port,
2302 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002303 if (!listener) {
2304 nfo.cm_id = NULL;
2305 nfo.conn_type = 0;
2306 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2307 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002308 break;
2309 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002310 nfo.cm_id = listener->cm_id;
2311 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002312 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2313 listener);
2314 if (!cm_node) {
2315 nes_debug(NES_DBG_CM, "Unable to allocate "
2316 "node\n");
2317 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002318 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002319 dev_kfree_skb_any(skb);
2320 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002321 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002322 if (!tcph->rst && !tcph->fin) {
2323 cm_node->state = NES_CM_STATE_LISTENING;
2324 } else {
2325 cm_packets_dropped++;
2326 rem_ref_cm_node(cm_core, cm_node);
2327 dev_kfree_skb_any(skb);
2328 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002329 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002330 add_ref_cm_node(cm_node);
2331 } else if (cm_node->state == NES_CM_STATE_TSA) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002332 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002333 atomic_inc(&cm_accel_dropped_pkts);
2334 dev_kfree_skb_any(skb);
2335 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002336 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002337 skb_reset_network_header(skb);
2338 skb_set_transport_header(skb, sizeof(*tcph));
2339 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002340 process_packet(cm_node, skb, cm_core);
2341 rem_ref_cm_node(cm_core, cm_node);
2342 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002343 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002344}
2345
2346
2347/**
2348 * nes_cm_alloc_core - allocate a top level instance of a cm core
2349 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002350static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002351{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002352 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002353
2354 /* setup the CM core */
2355 /* alloc top level core control structure */
2356 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2357 if (!cm_core)
2358 return NULL;
2359
2360 INIT_LIST_HEAD(&cm_core->connected_nodes);
2361 init_timer(&cm_core->tcp_timer);
2362 cm_core->tcp_timer.function = nes_cm_timer_tick;
2363
2364 cm_core->mtu = NES_CM_DEFAULT_MTU;
2365 cm_core->state = NES_CM_STATE_INITED;
2366 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2367
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002368 atomic_set(&cm_core->events_posted, 0);
2369
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002370 cm_core->api = &nes_cm_api;
2371
2372 spin_lock_init(&cm_core->ht_lock);
2373 spin_lock_init(&cm_core->listen_list_lock);
2374
2375 INIT_LIST_HEAD(&cm_core->listen_list.list);
2376
2377 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2378
2379 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2380 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2381 cm_core->post_event = nes_cm_post_event;
2382 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2383 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2384
2385 print_core(cm_core);
2386 return cm_core;
2387}
2388
2389
2390/**
2391 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2392 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002393static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002394{
2395 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2396
2397 if (!cm_core)
2398 return -EINVAL;
2399
2400 barrier();
2401
2402 if (timer_pending(&cm_core->tcp_timer)) {
2403 del_timer(&cm_core->tcp_timer);
2404 }
2405
2406 destroy_workqueue(cm_core->event_wq);
2407 destroy_workqueue(cm_core->disconn_wq);
2408 nes_debug(NES_DBG_CM, "\n");
2409 kfree(cm_core);
2410
2411 return 0;
2412}
2413
2414
2415/**
2416 * mini_cm_get
2417 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002418static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002419{
2420 return cm_core->state;
2421}
2422
2423
2424/**
2425 * mini_cm_set
2426 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002427static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002428{
2429 int ret = 0;
2430
2431 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002432 case NES_CM_SET_PKT_SIZE:
2433 cm_core->mtu = value;
2434 break;
2435 case NES_CM_SET_FREE_PKT_Q_SIZE:
2436 cm_core->free_tx_pkt_max = value;
2437 break;
2438 default:
2439 /* unknown set option */
2440 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002441 }
2442
2443 return ret;
2444}
2445
2446
2447/**
2448 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2449 * successfully exchanged when this is called
2450 */
2451static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2452{
2453 int ret = 0;
2454
2455 if (!nesqp)
2456 return -EINVAL;
2457
2458 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2459 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2460 NES_QPCONTEXT_MISC_DROS);
2461
2462 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2463 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2464
2465 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2466
2467 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2468
2469 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2470 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2471
2472 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2473 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2474 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2475
2476 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2477 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2478 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2479
2480 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2481 nesqp->nesqp_context->ts_recent = 0;
2482 nesqp->nesqp_context->ts_age = 0;
2483 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2484 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2485 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2486 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2487 cm_node->tcp_cntxt.rcv_wscale);
2488 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2489 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2490 nesqp->nesqp_context->srtt = 0;
2491 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2492 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2493 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2494 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2495 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2496 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2497
2498 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2499 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2500 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2501 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2502 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2503 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2504 le32_to_cpu(nesqp->nesqp_context->misc));
2505 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2506 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2507 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2508
2509 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2510 cm_node->state = NES_CM_STATE_TSA;
2511
2512 return ret;
2513}
2514
2515
2516/**
2517 * nes_cm_disconn
2518 */
2519int nes_cm_disconn(struct nes_qp *nesqp)
2520{
Don Wood873fcdd2009-09-05 20:36:37 -07002521 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002522
Don Wood873fcdd2009-09-05 20:36:37 -07002523 work = kzalloc(sizeof *work, GFP_ATOMIC);
2524 if (!work)
2525 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002526
Don Wood873fcdd2009-09-05 20:36:37 -07002527 nes_add_ref(&nesqp->ibqp);
2528 work->nesqp = nesqp;
2529 INIT_WORK(&work->work, nes_disconnect_worker);
2530 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002531 return 0;
2532}
2533
2534
2535/**
2536 * nes_disconnect_worker
2537 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002538static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002539{
Don Wood873fcdd2009-09-05 20:36:37 -07002540 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2541 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002542
Don Wood873fcdd2009-09-05 20:36:37 -07002543 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002544 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2545 nesqp->last_aeq, nesqp->hwqp.qp_id);
2546 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002547 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002548}
2549
2550
2551/**
2552 * nes_cm_disconn_true
2553 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002554static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002555{
2556 unsigned long flags;
2557 int ret = 0;
2558 struct iw_cm_id *cm_id;
2559 struct iw_cm_event cm_event;
2560 struct nes_vnic *nesvnic;
2561 u16 last_ae;
2562 u8 original_hw_tcp_state;
2563 u8 original_ibqp_state;
Or Gerlitz812d8672010-07-20 12:00:20 +00002564 enum iw_cm_event_status disconn_status = IW_CM_EVENT_STATUS_OK;
Don Woodb29a4fc2009-09-05 20:36:39 -07002565 int issue_disconn = 0;
2566 int issue_close = 0;
2567 int issue_flush = 0;
2568 u32 flush_q = NES_CQP_FLUSH_RQ;
2569 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002570
2571 if (!nesqp) {
2572 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2573 return -1;
2574 }
2575
2576 spin_lock_irqsave(&nesqp->lock, flags);
2577 cm_id = nesqp->cm_id;
2578 /* make sure we havent already closed this connection */
2579 if (!cm_id) {
2580 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2581 nesqp->hwqp.qp_id);
2582 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002583 return -1;
2584 }
2585
2586 nesvnic = to_nesvnic(nesqp->ibqp.device);
2587 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2588
2589 original_hw_tcp_state = nesqp->hw_tcp_state;
2590 original_ibqp_state = nesqp->ibqp_state;
2591 last_ae = nesqp->last_aeq;
2592
Don Woodb29a4fc2009-09-05 20:36:39 -07002593 if (nesqp->term_flags) {
2594 issue_disconn = 1;
2595 issue_close = 1;
2596 nesqp->cm_id = NULL;
2597 if (nesqp->flush_issued == 0) {
2598 nesqp->flush_issued = 1;
2599 issue_flush = 1;
2600 }
2601 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2602 ((original_ibqp_state == IB_QPS_RTS) &&
2603 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2604 issue_disconn = 1;
2605 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
2606 disconn_status = IW_CM_EVENT_STATUS_RESET;
2607 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002608
Don Woodb29a4fc2009-09-05 20:36:39 -07002609 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2610 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2611 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2612 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2613 issue_close = 1;
2614 nesqp->cm_id = NULL;
2615 if (nesqp->flush_issued == 0) {
2616 nesqp->flush_issued = 1;
2617 issue_flush = 1;
2618 }
2619 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002620
Don Woodb29a4fc2009-09-05 20:36:39 -07002621 spin_unlock_irqrestore(&nesqp->lock, flags);
2622
2623 if ((issue_flush) && (nesqp->destroyed == 0)) {
2624 /* Flush the queue(s) */
2625 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2626 flush_q |= NES_CQP_FLUSH_SQ;
2627 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2628
2629 if (nesqp->term_flags) {
2630 ibevent.device = nesqp->ibqp.device;
2631 ibevent.event = nesqp->terminate_eventtype;
2632 ibevent.element.qp = &nesqp->ibqp;
2633 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
2634 }
2635 }
2636
2637 if ((cm_id) && (cm_id->event_handler)) {
2638 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002639 atomic_inc(&cm_disconnects);
2640 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002641 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002642 cm_event.local_addr = cm_id->local_addr;
2643 cm_event.remote_addr = cm_id->remote_addr;
2644 cm_event.private_data = NULL;
2645 cm_event.private_data_len = 0;
2646
Faisal Latif6492cdf2008-07-24 20:50:45 -07002647 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2648 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2649 "cm_id = %p, refcount = %u.\n",
2650 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2651 nesqp->hwqp.sq_tail, cm_id,
2652 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002653
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002654 ret = cm_id->event_handler(cm_id, &cm_event);
2655 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002656 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2657 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002658 }
2659
Don Woodb29a4fc2009-09-05 20:36:39 -07002660 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002661 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002662 nes_disconnect(nesqp, 1);
2663
2664 cm_id->provider_data = nesqp;
2665 /* Send up the close complete event */
2666 cm_event.event = IW_CM_EVENT_CLOSE;
2667 cm_event.status = IW_CM_EVENT_STATUS_OK;
2668 cm_event.provider_data = cm_id->provider_data;
2669 cm_event.local_addr = cm_id->local_addr;
2670 cm_event.remote_addr = cm_id->remote_addr;
2671 cm_event.private_data = NULL;
2672 cm_event.private_data_len = 0;
2673
2674 ret = cm_id->event_handler(cm_id, &cm_event);
2675 if (ret) {
2676 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2677 }
2678
2679 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002680 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002681 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002682
2683 return 0;
2684}
2685
2686
2687/**
2688 * nes_disconnect
2689 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002690static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002691{
2692 int ret = 0;
2693 struct nes_vnic *nesvnic;
2694 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002695 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002696
2697 nesvnic = to_nesvnic(nesqp->ibqp.device);
2698 if (!nesvnic)
2699 return -EINVAL;
2700
2701 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002702 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002703
2704 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2705 atomic_read(&nesvnic->netdev->refcnt));
2706
2707 if (nesqp->active_conn) {
2708
2709 /* indicate this connection is NOT active */
2710 nesqp->active_conn = 0;
2711 } else {
2712 /* Need to free the Last Streaming Mode Message */
2713 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002714 if (nesqp->lsmm_mr)
2715 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002716 pci_free_consistent(nesdev->pcidev,
2717 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2718 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2719 }
2720 }
2721
2722 /* close the CM node down if it is still active */
2723 if (nesqp->cm_node) {
2724 nes_debug(NES_DBG_CM, "Call close API\n");
2725
2726 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002727 }
2728
2729 return ret;
2730}
2731
2732
2733/**
2734 * nes_accept
2735 */
2736int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2737{
2738 u64 u64temp;
2739 struct ib_qp *ibqp;
2740 struct nes_qp *nesqp;
2741 struct nes_vnic *nesvnic;
2742 struct nes_device *nesdev;
2743 struct nes_cm_node *cm_node;
2744 struct nes_adapter *adapter;
2745 struct ib_qp_attr attr;
2746 struct iw_cm_event cm_event;
2747 struct nes_hw_qp_wqe *wqe;
2748 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002749 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002750 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002751 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002752 struct nes_ib_device *nesibdev;
2753 struct ib_mr *ibmr = NULL;
2754 struct ib_phys_buf ibphysbuf;
2755 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002756 u64 tagged_offset;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002757
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002758 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2759 if (!ibqp)
2760 return -EINVAL;
2761
2762 /* get all our handles */
2763 nesqp = to_nesqp(ibqp);
2764 nesvnic = to_nesvnic(nesqp->ibqp.device);
2765 nesdev = nesvnic->nesdev;
2766 adapter = nesdev->nesadapter;
2767
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002768 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002769 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2770 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2771 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002772
Faisal Latifc5a7d482009-12-09 15:54:08 -08002773 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
2774 if (cm_node->loopbackpartner)
2775 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
2776 rem_ref_cm_node(cm_node->cm_core, cm_node);
2777 return -EINVAL;
2778 }
2779
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002780 /* associate the node with the QP */
2781 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002782 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002783
Faisal Latif6492cdf2008-07-24 20:50:45 -07002784 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2785 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002786 atomic_inc(&cm_accepts);
2787
2788 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2789 atomic_read(&nesvnic->netdev->refcnt));
2790
Faisal Latif6492cdf2008-07-24 20:50:45 -07002791 /* allocate the ietf frame and space for private data */
2792 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2793 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2794 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002795
Faisal Latif6492cdf2008-07-24 20:50:45 -07002796 if (!nesqp->ietf_frame) {
2797 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2798 "data\n");
2799 return -ENOMEM;
2800 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002801
2802
Faisal Latif6492cdf2008-07-24 20:50:45 -07002803 /* setup the MPA frame */
2804 nesqp->private_data_len = conn_param->private_data_len;
2805 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002806
Faisal Latif6492cdf2008-07-24 20:50:45 -07002807 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2808 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002809
Faisal Latif6492cdf2008-07-24 20:50:45 -07002810 nesqp->ietf_frame->priv_data_len =
2811 cpu_to_be16(conn_param->private_data_len);
2812 nesqp->ietf_frame->rev = mpa_version;
2813 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002814
Faisal Latif6492cdf2008-07-24 20:50:45 -07002815 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2816 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002817
Faisal Latif6492cdf2008-07-24 20:50:45 -07002818 if (cm_id->remote_addr.sin_addr.s_addr !=
2819 cm_id->local_addr.sin_addr.s_addr) {
2820 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002821 nesibdev = nesvnic->nesibdev;
2822 nespd = nesqp->nespd;
2823 ibphysbuf.addr = nesqp->ietf_frame_pbase;
2824 ibphysbuf.size = conn_param->private_data_len +
2825 sizeof(struct ietf_mpa_frame);
Don Wood7a5efb62009-04-08 14:21:02 -07002826 tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002827 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
2828 &ibphysbuf, 1,
2829 IB_ACCESS_LOCAL_WRITE,
Don Wood7a5efb62009-04-08 14:21:02 -07002830 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002831 if (!ibmr) {
2832 nes_debug(NES_DBG_CM, "Unable to register memory region"
2833 "for lSMM for cm_node = %p \n",
2834 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07002835 pci_free_consistent(nesdev->pcidev,
2836 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2837 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002838 return -ENOMEM;
2839 }
2840
2841 ibmr->pd = &nespd->ibpd;
2842 ibmr->device = nespd->ibpd.device;
2843 nesqp->lsmm_mr = ibmr;
2844
Faisal Latif6492cdf2008-07-24 20:50:45 -07002845 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2846 set_wqe_64bit_value(wqe->wqe_words,
2847 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2848 u64temp);
2849 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2850 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2851 NES_IWARP_SQ_WQE_WRPDU);
2852 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2853 cpu_to_le32(conn_param->private_data_len +
2854 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002855 set_wqe_64bit_value(wqe->wqe_words,
2856 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
Don Wood7a5efb62009-04-08 14:21:02 -07002857 (u64)(unsigned long)nesqp->ietf_frame);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002858 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2859 cpu_to_le32(conn_param->private_data_len +
2860 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002861 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08002862 if (nesqp->sq_kmapped) {
2863 nesqp->sq_kmapped = 0;
2864 kunmap(nesqp->page);
2865 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002866
Faisal Latif6492cdf2008-07-24 20:50:45 -07002867 nesqp->nesqp_context->ird_ord_sizes |=
2868 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2869 NES_QPCONTEXT_ORDIRD_WRPDU);
2870 } else {
2871 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08002872 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002873 }
2874 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002875
2876
2877 /* Cache the cm_id in the qp */
2878 nesqp->cm_id = cm_id;
2879 cm_node->cm_id = cm_id;
2880
2881 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2882 cm_id->provider_data = nesqp;
2883 nesqp->active_conn = 0;
2884
Faisal Latif6492cdf2008-07-24 20:50:45 -07002885 if (cm_node->state == NES_CM_STATE_TSA)
2886 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2887 cm_node);
2888
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002889 nes_cm_init_tsa_conn(nesqp, cm_node);
2890
Faisal Latif6492cdf2008-07-24 20:50:45 -07002891 nesqp->nesqp_context->tcpPorts[0] =
2892 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2893 nesqp->nesqp_context->tcpPorts[1] =
2894 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2895
2896 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2897 nesqp->nesqp_context->ip0 =
2898 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2899 else
2900 nesqp->nesqp_context->ip0 =
2901 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002902
2903 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002904 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2905 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002906
Faisal Latif6492cdf2008-07-24 20:50:45 -07002907 nesqp->nesqp_context->arp_index_vlan |=
2908 cpu_to_le32(nes_arp_table(nesdev,
2909 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002910 NES_ARP_RESOLVE) << 16);
2911
2912 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002913 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002914
2915 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2916
2917 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002918 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2919 nesqp->nesqp_context->ird_ord_sizes |=
2920 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002921
2922 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002923 nes_quad.DstIpAdrIndex =
2924 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2925 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2926 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2927 else
2928 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2929 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2930 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002931
2932 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06002933 crc_value = get_crc_value(&nes_quad);
2934 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002935 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07002936 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002937
2938 nesqp->hte_index &= adapter->hte_index_mask;
2939 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2940
2941 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2942
Faisal Latif6492cdf2008-07-24 20:50:45 -07002943 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2944 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2945 "private data length=%zu.\n", nesqp->hwqp.qp_id,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002946 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2947 ntohs(cm_id->remote_addr.sin_port),
2948 ntohl(cm_id->local_addr.sin_addr.s_addr),
2949 ntohs(cm_id->local_addr.sin_port),
2950 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2951 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002952 conn_param->private_data_len +
2953 sizeof(struct ietf_mpa_frame));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002954
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002955
Coly Li73ac36e2009-01-07 18:09:16 -08002956 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002957 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07002958 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002959
2960 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2961 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2962 cm_event.provider_data = (void *)nesqp;
2963 cm_event.local_addr = cm_id->local_addr;
2964 cm_event.remote_addr = cm_id->remote_addr;
2965 cm_event.private_data = NULL;
2966 cm_event.private_data_len = 0;
2967 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002968 attr.qp_state = IB_QPS_RTS;
2969 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002970 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002971 cm_node->loopbackpartner->mpa_frame_size =
2972 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002973 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002974 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2975 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002976 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2977 }
2978 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002979 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2980 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002981
Faisal Latif183ecfa2008-11-21 20:50:46 -06002982 passive_state = atomic_add_return(1, &cm_node->passive_state);
2983 if (passive_state == NES_SEND_RESET_EVENT)
2984 create_event(cm_node, NES_CM_EVENT_RESET);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002985 return 0;
2986}
2987
2988
2989/**
2990 * nes_reject
2991 */
2992int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2993{
2994 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002995 struct nes_cm_node *loopback;
2996
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002997 struct nes_cm_core *cm_core;
2998
2999 atomic_inc(&cm_rejects);
3000 cm_node = (struct nes_cm_node *) cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003001 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003002 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003003 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003004 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
3005
Faisal Latif9d5ab132009-03-06 15:15:01 -08003006 if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
3007 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003008
Faisal Latif1cf078c2009-12-09 15:53:54 -08003009 memcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003010 if (loopback) {
3011 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3012 loopback->mpa_frame.priv_data_len = pdata_len;
3013 loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
3014 pdata_len;
3015 } else {
3016 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
3017 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
3018 }
3019
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003020 cm_node->mpa_frame.rev = mpa_version;
3021 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
3022
Faisal Latif9d5ab132009-03-06 15:15:01 -08003023 return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003024}
3025
3026
3027/**
3028 * nes_connect
3029 * setup and launch cm connect node
3030 */
3031int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3032{
3033 struct ib_qp *ibqp;
3034 struct nes_qp *nesqp;
3035 struct nes_vnic *nesvnic;
3036 struct nes_device *nesdev;
3037 struct nes_cm_node *cm_node;
3038 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003039 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003040
3041 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3042 if (!ibqp)
3043 return -EINVAL;
3044 nesqp = to_nesqp(ibqp);
3045 if (!nesqp)
3046 return -EINVAL;
3047 nesvnic = to_nesvnic(nesqp->ibqp.device);
3048 if (!nesvnic)
3049 return -EINVAL;
3050 nesdev = nesvnic->nesdev;
3051 if (!nesdev)
3052 return -EINVAL;
3053
Faisal Latifc5a7d482009-12-09 15:54:08 -08003054 if (!(cm_id->local_addr.sin_port) || !(cm_id->remote_addr.sin_port))
3055 return -EINVAL;
3056
Faisal Latif6492cdf2008-07-24 20:50:45 -07003057 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
3058 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3059 ntohl(nesvnic->local_ipaddr),
3060 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3061 ntohs(cm_id->remote_addr.sin_port),
3062 ntohl(cm_id->local_addr.sin_addr.s_addr),
3063 ntohs(cm_id->local_addr.sin_port));
3064
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003065 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003066 nesqp->active_conn = 1;
3067
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003068 /* cache the cm_id in the qp */
3069 nesqp->cm_id = cm_id;
3070
3071 cm_id->provider_data = nesqp;
3072
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003073 nesqp->private_data_len = conn_param->private_data_len;
3074 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
3075 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003076 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
3077 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003078
Faisal Latif6492cdf2008-07-24 20:50:45 -07003079 if (cm_id->local_addr.sin_addr.s_addr !=
Faisal Latif53094c32009-04-27 13:37:34 -07003080 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003081 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003082 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003083 apbvt_set = 1;
3084 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003085
3086 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003087 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3088 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3089 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3090 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003091 cm_info.cm_id = cm_id;
3092 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3093
3094 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003095
3096 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003097 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3098 conn_param->private_data_len, (void *)conn_param->private_data,
3099 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003100 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003101 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003102 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003103 PCI_FUNC(nesdev->pcidev->devfn),
3104 NES_MANAGE_APBVT_DEL);
3105
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003106 cm_id->rem_ref(cm_id);
3107 return -ENOMEM;
3108 }
3109
Faisal Latif53094c32009-04-27 13:37:34 -07003110 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003111 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003112 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003113 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003114
3115 return 0;
3116}
3117
3118
3119/**
3120 * nes_create_listen
3121 */
3122int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3123{
3124 struct nes_vnic *nesvnic;
3125 struct nes_cm_listener *cm_node;
3126 struct nes_cm_info cm_info;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003127 int err;
3128
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003129 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3130 cm_id, ntohs(cm_id->local_addr.sin_port));
3131
3132 nesvnic = to_nesvnic(cm_id->device);
3133 if (!nesvnic)
3134 return -EINVAL;
Roland Dreier69d51022010-08-04 14:25:40 -07003135
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003136 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3137 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3138
3139 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3140 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3141
3142 /* setup listen params in our api call struct */
3143 cm_info.loc_addr = nesvnic->local_ipaddr;
3144 cm_info.loc_port = cm_id->local_addr.sin_port;
3145 cm_info.backlog = backlog;
3146 cm_info.cm_id = cm_id;
3147
3148 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3149
3150
3151 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3152 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003153 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003154 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003155 return -ENOMEM;
3156 }
3157
3158 cm_id->provider_data = cm_node;
3159
3160 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003161 err = nes_manage_apbvt(nesvnic,
3162 ntohs(cm_id->local_addr.sin_port),
3163 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3164 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003165 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003166 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3167 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003168 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3169 return err;
3170 }
Faisal Latif6e10d2e2010-02-12 19:55:03 +00003171 atomic_inc(&cm_listens_created);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003172 }
3173
3174 cm_id->add_ref(cm_id);
3175 cm_id->provider_data = (void *)cm_node;
3176
3177
3178 return 0;
3179}
3180
3181
3182/**
3183 * nes_destroy_listen
3184 */
3185int nes_destroy_listen(struct iw_cm_id *cm_id)
3186{
3187 if (cm_id->provider_data)
3188 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3189 else
3190 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3191
3192 cm_id->rem_ref(cm_id);
3193
3194 return 0;
3195}
3196
3197
3198/**
3199 * nes_cm_recv
3200 */
3201int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3202{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003203 int rc = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003204 cm_packets_received++;
3205 if ((g_cm_core) && (g_cm_core->api)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003206 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003207 } else {
3208 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3209 " cm is not setup properly.\n");
3210 }
3211
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003212 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003213}
3214
3215
3216/**
3217 * nes_cm_start
3218 * Start and init a cm core module
3219 */
3220int nes_cm_start(void)
3221{
3222 nes_debug(NES_DBG_CM, "\n");
3223 /* create the primary CM core, pass this handle to subsequent core inits */
3224 g_cm_core = nes_cm_alloc_core();
3225 if (g_cm_core) {
3226 return 0;
3227 } else {
3228 return -ENOMEM;
3229 }
3230}
3231
3232
3233/**
3234 * nes_cm_stop
3235 * stop and dealloc all cm core instances
3236 */
3237int nes_cm_stop(void)
3238{
3239 g_cm_core->api->destroy_cm_core(g_cm_core);
3240 return 0;
3241}
3242
3243
3244/**
3245 * cm_event_connected
3246 * handle a connected event, setup QPs and HW
3247 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003248static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003249{
3250 u64 u64temp;
3251 struct nes_qp *nesqp;
3252 struct nes_vnic *nesvnic;
3253 struct nes_device *nesdev;
3254 struct nes_cm_node *cm_node;
3255 struct nes_adapter *nesadapter;
3256 struct ib_qp_attr attr;
3257 struct iw_cm_id *cm_id;
3258 struct iw_cm_event cm_event;
3259 struct nes_hw_qp_wqe *wqe;
3260 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003261 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003262 int ret;
3263
3264 /* get all our handles */
3265 cm_node = event->cm_node;
3266 cm_id = cm_node->cm_id;
3267 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3268 nesqp = (struct nes_qp *)cm_id->provider_data;
3269 nesvnic = to_nesvnic(nesqp->ibqp.device);
3270 nesdev = nesvnic->nesdev;
3271 nesadapter = nesdev->nesadapter;
3272
3273 if (nesqp->destroyed) {
3274 return;
3275 }
3276 atomic_inc(&cm_connecteds);
3277 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3278 " local port 0x%04X. jiffies = %lu.\n",
3279 nesqp->hwqp.qp_id,
3280 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3281 ntohs(cm_id->remote_addr.sin_port),
3282 ntohs(cm_id->local_addr.sin_port),
3283 jiffies);
3284
3285 nes_cm_init_tsa_conn(nesqp, cm_node);
3286
3287 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003288 nesqp->nesqp_context->tcpPorts[0] =
3289 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3290 nesqp->nesqp_context->tcpPorts[1] =
3291 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3292 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3293 nesqp->nesqp_context->ip0 =
3294 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3295 else
3296 nesqp->nesqp_context->ip0 =
3297 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003298
3299 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003300 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3301 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003302 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003303 nes_arp_table(nesdev,
3304 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003305 NULL, NES_ARP_RESOLVE) << 16);
3306 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3307 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3308 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3309 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003310 cpu_to_le32((u32)1 <<
3311 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003312
3313 /* Adjust tail for not having a LSMM */
3314 nesqp->hwqp.sq_tail = 1;
3315
3316#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003317 if (cm_node->send_write0) {
3318 nes_debug(NES_DBG_CM, "Sending first write.\n");
3319 wqe = &nesqp->hwqp.sq_vbase[0];
3320 u64temp = (unsigned long)nesqp;
3321 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3322 set_wqe_64bit_value(wqe->wqe_words,
3323 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3324 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3325 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3326 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3327 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3328 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3329 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3330 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003331
Faisal Latifd2fa9b262009-12-09 15:54:28 -08003332 if (nesqp->sq_kmapped) {
3333 nesqp->sq_kmapped = 0;
3334 kunmap(nesqp->page);
3335 }
3336
Faisal Latif6492cdf2008-07-24 20:50:45 -07003337 /* use the reserved spot on the WQ for the extra first WQE */
3338 nesqp->nesqp_context->ird_ord_sizes &=
3339 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3340 NES_QPCONTEXT_ORDIRD_WRPDU |
3341 NES_QPCONTEXT_ORDIRD_ALSMM));
3342 nesqp->skip_lsmm = 1;
3343 nesqp->hwqp.sq_tail = 0;
3344 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3345 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3346 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003347#endif
3348
3349 memset(&nes_quad, 0, sizeof(nes_quad));
3350
Faisal Latif6492cdf2008-07-24 20:50:45 -07003351 nes_quad.DstIpAdrIndex =
3352 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3353 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3354 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3355 else
3356 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003357 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3358 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3359
3360 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003361 crc_value = get_crc_value(&nes_quad);
3362 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003363 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3364 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3365
3366 nesqp->hte_index &= nesadapter->hte_index_mask;
3367 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3368
3369 nesqp->ietf_frame = &cm_node->mpa_frame;
3370 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3371 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3372
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003373 /* notify OF layer we successfully created the requested connection */
3374 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3375 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3376 cm_event.provider_data = cm_id->provider_data;
3377 cm_event.local_addr.sin_family = AF_INET;
3378 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3379 cm_event.remote_addr = cm_id->remote_addr;
3380
Faisal Latif6492cdf2008-07-24 20:50:45 -07003381 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3382 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003383
3384 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3385 ret = cm_id->event_handler(cm_id, &cm_event);
3386 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3387
3388 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003389 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3390 "ret=%d\n", __func__, __LINE__, ret);
3391 attr.qp_state = IB_QPS_RTS;
3392 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003393
Faisal Latif6492cdf2008-07-24 20:50:45 -07003394 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3395 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003396
3397 return;
3398}
3399
3400
3401/**
3402 * cm_event_connect_error
3403 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003404static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003405{
3406 struct nes_qp *nesqp;
3407 struct iw_cm_id *cm_id;
3408 struct iw_cm_event cm_event;
3409 /* struct nes_cm_info cm_info; */
3410 int ret;
3411
3412 if (!event->cm_node)
3413 return;
3414
3415 cm_id = event->cm_node->cm_id;
3416 if (!cm_id) {
3417 return;
3418 }
3419
3420 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3421 nesqp = cm_id->provider_data;
3422
3423 if (!nesqp) {
3424 return;
3425 }
3426
3427 /* notify OF layer about this connection error event */
3428 /* cm_id->rem_ref(cm_id); */
3429 nesqp->cm_id = NULL;
3430 cm_id->provider_data = NULL;
3431 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003432 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003433 cm_event.provider_data = cm_id->provider_data;
3434 cm_event.local_addr = cm_id->local_addr;
3435 cm_event.remote_addr = cm_id->remote_addr;
3436 cm_event.private_data = NULL;
3437 cm_event.private_data_len = 0;
3438
Faisal Latif6492cdf2008-07-24 20:50:45 -07003439 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3440 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3441 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003442
3443 ret = cm_id->event_handler(cm_id, &cm_event);
3444 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3445 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003446 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3447 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003448 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003449
Faisal Latif6492cdf2008-07-24 20:50:45 -07003450 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003451 return;
3452}
3453
3454
3455/**
3456 * cm_event_reset
3457 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003458static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003459{
3460 struct nes_qp *nesqp;
3461 struct iw_cm_id *cm_id;
3462 struct iw_cm_event cm_event;
3463 /* struct nes_cm_info cm_info; */
3464 int ret;
3465
3466 if (!event->cm_node)
3467 return;
3468
3469 if (!event->cm_node->cm_id)
3470 return;
3471
3472 cm_id = event->cm_node->cm_id;
3473
3474 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3475 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003476 if (!nesqp)
3477 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003478
3479 nesqp->cm_id = NULL;
3480 /* cm_id->provider_data = NULL; */
3481 cm_event.event = IW_CM_EVENT_DISCONNECT;
3482 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3483 cm_event.provider_data = cm_id->provider_data;
3484 cm_event.local_addr = cm_id->local_addr;
3485 cm_event.remote_addr = cm_id->remote_addr;
3486 cm_event.private_data = NULL;
3487 cm_event.private_data_len = 0;
3488
Faisal Latif183ecfa2008-11-21 20:50:46 -06003489 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003490 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003491 atomic_inc(&cm_closes);
3492 cm_event.event = IW_CM_EVENT_CLOSE;
3493 cm_event.status = IW_CM_EVENT_STATUS_OK;
3494 cm_event.provider_data = cm_id->provider_data;
3495 cm_event.local_addr = cm_id->local_addr;
3496 cm_event.remote_addr = cm_id->remote_addr;
3497 cm_event.private_data = NULL;
3498 cm_event.private_data_len = 0;
3499 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3500 ret = cm_id->event_handler(cm_id, &cm_event);
3501
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003502 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3503
3504
3505 /* notify OF layer about this connection error event */
3506 cm_id->rem_ref(cm_id);
3507
3508 return;
3509}
3510
3511
3512/**
3513 * cm_event_mpa_req
3514 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003515static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003516{
3517 struct iw_cm_id *cm_id;
3518 struct iw_cm_event cm_event;
3519 int ret;
3520 struct nes_cm_node *cm_node;
3521
3522 cm_node = event->cm_node;
3523 if (!cm_node)
3524 return;
3525 cm_id = cm_node->cm_id;
3526
3527 atomic_inc(&cm_connect_reqs);
3528 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3529 cm_node, cm_id, jiffies);
3530
3531 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3532 cm_event.status = IW_CM_EVENT_STATUS_OK;
3533 cm_event.provider_data = (void *)cm_node;
3534
3535 cm_event.local_addr.sin_family = AF_INET;
3536 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3537 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3538
3539 cm_event.remote_addr.sin_family = AF_INET;
3540 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3541 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003542 cm_event.private_data = cm_node->mpa_frame_buf;
3543 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003544
3545 ret = cm_id->event_handler(cm_id, &cm_event);
3546 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003547 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3548 __func__, __LINE__, ret);
3549 return;
3550}
3551
3552
3553static void cm_event_mpa_reject(struct nes_cm_event *event)
3554{
3555 struct iw_cm_id *cm_id;
3556 struct iw_cm_event cm_event;
3557 struct nes_cm_node *cm_node;
3558 int ret;
3559
3560 cm_node = event->cm_node;
3561 if (!cm_node)
3562 return;
3563 cm_id = cm_node->cm_id;
3564
3565 atomic_inc(&cm_connect_reqs);
3566 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3567 cm_node, cm_id, jiffies);
3568
3569 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3570 cm_event.status = -ECONNREFUSED;
3571 cm_event.provider_data = cm_id->provider_data;
3572
3573 cm_event.local_addr.sin_family = AF_INET;
3574 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3575 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3576
3577 cm_event.remote_addr.sin_family = AF_INET;
3578 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3579 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3580
3581 cm_event.private_data = cm_node->mpa_frame_buf;
3582 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3583
3584 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3585 "remove_addr=%08x\n",
3586 cm_event.local_addr.sin_addr.s_addr,
3587 cm_event.remote_addr.sin_addr.s_addr);
3588
3589 ret = cm_id->event_handler(cm_id, &cm_event);
3590 if (ret)
3591 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003592 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003593
3594 return;
3595}
3596
3597
3598static void nes_cm_event_handler(struct work_struct *);
3599
3600/**
3601 * nes_cm_post_event
3602 * post an event to the cm event handler
3603 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003604static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003605{
3606 atomic_inc(&event->cm_node->cm_core->events_posted);
3607 add_ref_cm_node(event->cm_node);
3608 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3609 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003610 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3611 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003612
3613 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3614
3615 nes_debug(NES_DBG_CM, "Exit\n");
3616 return 0;
3617}
3618
3619
3620/**
3621 * nes_cm_event_handler
3622 * worker function to handle cm events
3623 * will free instance of nes_cm_event
3624 */
3625static void nes_cm_event_handler(struct work_struct *work)
3626{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003627 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3628 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003629 struct nes_cm_core *cm_core;
3630
Faisal Latif6492cdf2008-07-24 20:50:45 -07003631 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003632 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003633
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003634 cm_core = event->cm_node->cm_core;
3635 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003636 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003637
3638 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003639 case NES_CM_EVENT_MPA_REQ:
3640 cm_event_mpa_req(event);
3641 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3642 event->cm_node);
3643 break;
3644 case NES_CM_EVENT_RESET:
3645 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3646 event->cm_node);
3647 cm_event_reset(event);
3648 break;
3649 case NES_CM_EVENT_CONNECTED:
3650 if ((!event->cm_node->cm_id) ||
3651 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003652 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003653 cm_event_connected(event);
3654 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3655 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003656 case NES_CM_EVENT_MPA_REJECT:
3657 if ((!event->cm_node->cm_id) ||
3658 (event->cm_node->state == NES_CM_STATE_TSA))
3659 break;
3660 cm_event_mpa_reject(event);
3661 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3662 break;
3663
Faisal Latif6492cdf2008-07-24 20:50:45 -07003664 case NES_CM_EVENT_ABORTED:
3665 if ((!event->cm_node->cm_id) ||
3666 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003667 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003668 cm_event_connect_error(event);
3669 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3670 break;
3671 case NES_CM_EVENT_DROPPED_PKT:
3672 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3673 break;
3674 default:
3675 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3676 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003677 }
3678
3679 atomic_dec(&cm_core->events_posted);
3680 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3681 rem_ref_cm_node(cm_core, event->cm_node);
3682 kfree(event);
3683
3684 return;
3685}