blob: 2a49ee40b52003e109b039896c773f5c3209d2bd [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>
Bob Sharp7191a0a2008-10-03 12:21:19 -070056#include <net/arp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080057#include <net/neighbour.h>
58#include <net/route.h>
59#include <net/ip_fib.h>
Faisal Latifc11470f2009-04-27 13:38:31 -070060#include <net/tcp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080061
62#include "nes.h"
63
64u32 cm_packets_sent;
65u32 cm_packets_bounced;
66u32 cm_packets_dropped;
67u32 cm_packets_retrans;
68u32 cm_packets_created;
69u32 cm_packets_received;
Faisal Latif6e10d2e2010-02-12 19:55:03 +000070atomic_t cm_listens_created;
71atomic_t cm_listens_destroyed;
Glenn Streiff3c2d7742008-02-04 20:20:45 -080072u32 cm_backlog_drops;
73atomic_t cm_loopbacks;
74atomic_t cm_nodes_created;
75atomic_t cm_nodes_destroyed;
76atomic_t cm_accel_dropped_pkts;
77atomic_t cm_resets_recvd;
78
Faisal Latif6492cdf2008-07-24 20:50:45 -070079static inline int mini_cm_accelerated(struct nes_cm_core *,
80 struct nes_cm_node *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080081static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070082 struct nes_vnic *, struct nes_cm_info *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080083static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070084static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070085 struct nes_vnic *, u16, void *, struct nes_cm_info *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070086static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -070087static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
88 struct nes_cm_node *);
89static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
90 struct nes_cm_node *);
Faisal Latif4a14f6a2008-11-21 20:50:49 -060091static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070092 struct sk_buff *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070093static int mini_cm_dealloc_core(struct nes_cm_core *);
94static int mini_cm_get(struct nes_cm_core *);
95static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070096
Chien Tung6098d102008-11-21 20:51:01 -060097static void form_cm_frame(struct sk_buff *, struct nes_cm_node *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070098 void *, u32, void *, u32, u8);
Faisal Latif6492cdf2008-07-24 20:50:45 -070099static int add_ref_cm_node(struct nes_cm_node *);
100static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
101
Roland Dreier1a855fb2008-04-16 21:01:09 -0700102static int nes_cm_disconn_true(struct nes_qp *);
103static int nes_cm_post_event(struct nes_cm_event *event);
104static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
105static void nes_disconnect_worker(struct work_struct *work);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700106
107static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800108static int send_mpa_reject(struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700109static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
110static int send_reset(struct nes_cm_node *, struct sk_buff *);
111static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fb2008-04-16 21:01:09 -0700112static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700113static void process_packet(struct nes_cm_node *, struct sk_buff *,
114 struct nes_cm_core *);
115
116static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
117static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
118static void cleanup_retrans_entry(struct nes_cm_node *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800119static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700120static void free_retrans_entry(struct nes_cm_node *cm_node);
121static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
122 struct sk_buff *skb, int optionsize, int passive);
123
124/* CM event handler functions */
125static void cm_event_connected(struct nes_cm_event *);
126static void cm_event_connect_error(struct nes_cm_event *);
127static void cm_event_reset(struct nes_cm_event *);
128static void cm_event_mpa_req(struct nes_cm_event *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800129static void cm_event_mpa_reject(struct nes_cm_event *);
130static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700131
132static void print_core(struct nes_cm_core *core);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800133
134/* External CM API Interface */
135/* instance of function pointers for client API */
136/* set address of this instance to cm_core->cm_ops at cm_core alloc */
137static struct nes_cm_ops nes_cm_api = {
138 mini_cm_accelerated,
139 mini_cm_listen,
140 mini_cm_del_listen,
141 mini_cm_connect,
142 mini_cm_close,
143 mini_cm_accept,
144 mini_cm_reject,
145 mini_cm_recv_pkt,
146 mini_cm_dealloc_core,
147 mini_cm_get,
148 mini_cm_set
149};
150
Roland Dreier1a855fb2008-04-16 21:01:09 -0700151static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800152
153atomic_t cm_connects;
154atomic_t cm_accepts;
155atomic_t cm_disconnects;
156atomic_t cm_closes;
157atomic_t cm_connecteds;
158atomic_t cm_connect_reqs;
159atomic_t cm_rejects;
160
161
162/**
163 * create_event
164 */
165static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
166 enum nes_cm_event_type type)
167{
168 struct nes_cm_event *event;
169
170 if (!cm_node->cm_id)
171 return NULL;
172
173 /* allocate an empty event */
174 event = kzalloc(sizeof(*event), GFP_ATOMIC);
175
176 if (!event)
177 return NULL;
178
179 event->type = type;
180 event->cm_node = cm_node;
181 event->cm_info.rem_addr = cm_node->rem_addr;
182 event->cm_info.loc_addr = cm_node->loc_addr;
183 event->cm_info.rem_port = cm_node->rem_port;
184 event->cm_info.loc_port = cm_node->loc_port;
185 event->cm_info.cm_id = cm_node->cm_id;
186
Faisal Latif6492cdf2008-07-24 20:50:45 -0700187 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
188 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
189 cm_node, event, type, event->cm_info.loc_addr,
190 event->cm_info.loc_port, event->cm_info.rem_addr,
191 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800192
193 nes_cm_post_event(event);
194 return event;
195}
196
197
198/**
199 * send_mpa_request
200 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700201static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800202{
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800203 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700204 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800205 return -1;
206 }
207
208 /* send an MPA Request frame */
209 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
210 cm_node->mpa_frame_size, SET_ACK);
211
Faisal Latif9d5ab132009-03-06 15:15:01 -0800212 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
213}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800214
Faisal Latif9d5ab132009-03-06 15:15:01 -0800215
216
217static int send_mpa_reject(struct nes_cm_node *cm_node)
218{
219 struct sk_buff *skb = NULL;
220
221 skb = dev_alloc_skb(MAX_CM_BUFFER);
222 if (!skb) {
223 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
224 return -ENOMEM;
225 }
226
227 /* send an MPA reject frame */
228 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
229 cm_node->mpa_frame_size, SET_ACK | SET_FIN);
230
231 cm_node->state = NES_CM_STATE_FIN_WAIT1;
232 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800233}
234
235
236/**
237 * recv_mpa - process a received TCP pkt, we are expecting an
238 * IETF MPA frame
239 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800240static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
241 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800242{
243 struct ietf_mpa_frame *mpa_frame;
244
Faisal Latif9d5ab132009-03-06 15:15:01 -0800245 *type = NES_MPA_REQUEST_ACCEPT;
246
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800247 /* assume req frame is in tcp data payload */
248 if (len < sizeof(struct ietf_mpa_frame)) {
249 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800250 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800251 }
252
253 mpa_frame = (struct ietf_mpa_frame *)buffer;
254 cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
Faisal Latif1cf078c2009-12-09 15:53:54 -0800255 /* make sure mpa private data len is less than 512 bytes */
256 if (cm_node->mpa_frame_size > IETF_MAX_PRIV_DATA_LEN) {
257 nes_debug(NES_DBG_CM, "The received Length of Private"
258 " Data field exceeds 512 octets\n");
259 return -EINVAL;
260 }
261 /*
262 * make sure MPA receiver interoperate with the
263 * received MPA version and MPA key information
264 *
265 */
266 if (mpa_frame->rev != mpa_version) {
267 nes_debug(NES_DBG_CM, "The received mpa version"
268 " can not be interoperated\n");
269 return -EINVAL;
270 }
271 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
272 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
273 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
274 return -EINVAL;
275 }
276 } else {
277 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
278 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
279 return -EINVAL;
280 }
281 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800282
283 if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
284 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
285 " complete (%x + %x != %x)\n",
Faisal Latif9d5ab132009-03-06 15:15:01 -0800286 cm_node->mpa_frame_size,
287 (u32)sizeof(struct ietf_mpa_frame), len);
288 return -EINVAL;
289 }
290 /* make sure it does not exceed the max size */
291 if (len > MAX_CM_BUFFER) {
292 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
293 " (%x + %x != %x)\n",
294 cm_node->mpa_frame_size,
295 (u32)sizeof(struct ietf_mpa_frame), len);
296 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800297 }
298
299 /* copy entire MPA frame to our cm_node's frame */
300 memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
301 cm_node->mpa_frame_size);
302
Faisal Latif9d5ab132009-03-06 15:15:01 -0800303 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
304 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800305 return 0;
306}
307
308
309/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800310 * form_cm_frame - get a free packet and build empty frame Use
311 * node info to build.
312 */
Chien Tung6098d102008-11-21 20:51:01 -0600313static void form_cm_frame(struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700314 struct nes_cm_node *cm_node, void *options, u32 optionsize,
315 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800316{
317 struct tcphdr *tcph;
318 struct iphdr *iph;
319 struct ethhdr *ethh;
320 u8 *buf;
321 u16 packetsize = sizeof(*iph);
322
323 packetsize += sizeof(*tcph);
324 packetsize += optionsize + datasize;
325
326 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
327
328 skb->len = 0;
329 buf = skb_put(skb, packetsize + ETH_HLEN);
330
331 ethh = (struct ethhdr *) buf;
332 buf += ETH_HLEN;
333
334 iph = (struct iphdr *)buf;
335 buf += sizeof(*iph);
336 tcph = (struct tcphdr *)buf;
337 skb_reset_mac_header(skb);
338 skb_set_network_header(skb, ETH_HLEN);
339 skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
340 buf += sizeof(*tcph);
341
342 skb->ip_summed = CHECKSUM_PARTIAL;
343 skb->protocol = htons(0x800);
344 skb->data_len = 0;
345 skb->mac_len = ETH_HLEN;
346
347 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
348 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
349 ethh->h_proto = htons(0x0800);
350
351 iph->version = IPVERSION;
352 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
353 iph->tos = 0;
354 iph->tot_len = htons(packetsize);
355 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
356
357 iph->frag_off = htons(0x4000);
358 iph->ttl = 0x40;
359 iph->protocol = 0x06; /* IPPROTO_TCP */
360
361 iph->saddr = htonl(cm_node->loc_addr);
362 iph->daddr = htonl(cm_node->rem_addr);
363
364 tcph->source = htons(cm_node->loc_port);
365 tcph->dest = htons(cm_node->rem_port);
366 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
367
368 if (flags & SET_ACK) {
369 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
370 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
371 tcph->ack = 1;
372 } else
373 tcph->ack_seq = 0;
374
375 if (flags & SET_SYN) {
376 cm_node->tcp_cntxt.loc_seq_num++;
377 tcph->syn = 1;
378 } else
Faisal Latif6492cdf2008-07-24 20:50:45 -0700379 cm_node->tcp_cntxt.loc_seq_num += datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800380
Faisal Latif6492cdf2008-07-24 20:50:45 -0700381 if (flags & SET_FIN) {
382 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800383 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700384 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800385
386 if (flags & SET_RST)
387 tcph->rst = 1;
388
389 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
390 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
391 tcph->urg_ptr = 0;
392 if (optionsize)
393 memcpy(buf, options, optionsize);
394 buf += optionsize;
395 if (datasize)
396 memcpy(buf, data, datasize);
397
398 skb_shinfo(skb)->nr_frags = 0;
399 cm_packets_created++;
400
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800401}
402
403
404/**
405 * print_core - dump a cm core
406 */
407static void print_core(struct nes_cm_core *core)
408{
409 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
410 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
411 if (!core)
412 return;
413 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800414
415 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
416
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800417 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
418 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
419
420 nes_debug(NES_DBG_CM, "core : %p \n", core);
421
422 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
423}
424
425
426/**
427 * schedule_nes_timer
428 * note - cm_node needs to be protected before calling this. Encase in:
429 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
430 */
431int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
432 enum nes_timer_type type, int send_retrans,
433 int close_when_complete)
434{
435 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700436 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800437 struct nes_timer_entry *new_send;
438 int ret = 0;
439 u32 was_timer_set;
440
441 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
442 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800443 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800444
445 /* new_send->timetosend = currenttime */
446 new_send->retrycount = NES_DEFAULT_RETRYS;
447 new_send->retranscount = NES_DEFAULT_RETRANS;
448 new_send->skb = skb;
449 new_send->timetosend = jiffies;
450 new_send->type = type;
451 new_send->netdev = cm_node->netdev;
452 new_send->send_retrans = send_retrans;
453 new_send->close_when_complete = close_when_complete;
454
455 if (type == NES_TIMER_TYPE_CLOSE) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700456 new_send->timetosend += (HZ/10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800457 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700458 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800459 WARN_ON(1);
460 return -EINVAL;
461 }
462 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800463 }
464
465 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700466 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800467 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700468 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
469 cm_node->send_entry = new_send;
470 add_ref_cm_node(cm_node);
471 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
472 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800473
474 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
475 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700476 nes_debug(NES_DBG_CM, "Error sending packet %p "
477 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800478 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700479 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800480 } else {
481 cm_packets_sent++;
482 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700483 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800484 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700485 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800486 return ret;
487 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800488 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800489 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800490
491 was_timer_set = timer_pending(&cm_core->tcp_timer);
492
493 if (!was_timer_set) {
494 cm_core->tcp_timer.expires = new_send->timetosend;
495 add_timer(&cm_core->tcp_timer);
496 }
497
498 return ret;
499}
500
Faisal Latif9d5ab132009-03-06 15:15:01 -0800501static void nes_retrans_expired(struct nes_cm_node *cm_node)
502{
Faisal Latif68237a02009-06-22 22:53:28 -0700503 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800504 switch (cm_node->state) {
505 case NES_CM_STATE_SYN_RCVD:
506 case NES_CM_STATE_CLOSING:
507 rem_ref_cm_node(cm_node->cm_core, cm_node);
508 break;
509 case NES_CM_STATE_LAST_ACK:
510 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700511 if (cm_node->cm_id)
512 cm_id->rem_ref(cm_id);
513 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800514 send_reset(cm_node, NULL);
515 break;
516 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800517 add_ref_cm_node(cm_node);
518 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800519 create_event(cm_node, NES_CM_EVENT_ABORTED);
520 }
521}
522
523static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
524{
525 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
526 struct iw_cm_id *cm_id = cm_node->cm_id;
527 struct nes_qp *nesqp;
528 unsigned long qplockflags;
529
530 if (!recv_entry)
531 return;
532 nesqp = (struct nes_qp *)recv_entry->skb;
533 if (nesqp) {
534 spin_lock_irqsave(&nesqp->lock, qplockflags);
535 if (nesqp->cm_id) {
536 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
537 "refcount = %d: HIT A "
538 "NES_TIMER_TYPE_CLOSE with something "
539 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
540 atomic_read(&nesqp->refcount));
541 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
542 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
543 nesqp->ibqp_state = IB_QPS_ERR;
544 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
545 nes_cm_disconn(nesqp);
546 } else {
547 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
548 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
549 "refcount = %d: HIT A "
550 "NES_TIMER_TYPE_CLOSE with nothing "
551 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
552 atomic_read(&nesqp->refcount));
553 }
554 } else if (rem_node) {
555 /* TIME_WAIT state */
556 rem_ref_cm_node(cm_node->cm_core, cm_node);
557 }
558 if (cm_node->cm_id)
559 cm_id->rem_ref(cm_id);
560 kfree(recv_entry);
561 cm_node->recv_entry = NULL;
562}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800563
564/**
565 * nes_cm_timer_tick
566 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700567static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800568{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800569 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800570 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800571 struct nes_cm_node *cm_node;
572 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800573 struct list_head *list_core_temp;
574 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800575 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800576 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700577 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800578 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800579
Faisal Latif879e5bd2008-11-21 20:50:41 -0600580 struct list_head timer_list;
581 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800582 spin_lock_irqsave(&cm_core->ht_lock, flags);
583
Faisal Latif6492cdf2008-07-24 20:50:45 -0700584 list_for_each_safe(list_node, list_core_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600585 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800586 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800587 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600588 add_ref_cm_node(cm_node);
589 list_add(&cm_node->timer_entry, &timer_list);
590 }
591 }
592 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
593
594 list_for_each_safe(list_node, list_core_temp, &timer_list) {
595 cm_node = container_of(list_node, struct nes_cm_node,
596 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800597 recv_entry = cm_node->recv_entry;
598
599 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700600 if (time_after(recv_entry->timetosend, jiffies)) {
601 if (nexttimeout > recv_entry->timetosend ||
Faisal Latif9d5ab132009-03-06 15:15:01 -0800602 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800603 nexttimeout = recv_entry->timetosend;
604 settimer = 1;
605 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800606 } else
607 handle_recv_entry(cm_node, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800608 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800609
610 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700611 do {
612 send_entry = cm_node->send_entry;
613 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600614 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800615 if (time_after(send_entry->timetosend, jiffies)) {
616 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700617 if ((nexttimeout >
618 send_entry->timetosend) ||
619 !settimer) {
620 nexttimeout =
621 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800622 settimer = 1;
623 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800624 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700625 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800626 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800627 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800628 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700629
630 if ((cm_node->state == NES_CM_STATE_TSA) ||
631 (cm_node->state == NES_CM_STATE_CLOSED)) {
632 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600633 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800634 }
635
Faisal Latif6492cdf2008-07-24 20:50:45 -0700636 if (!send_entry->retranscount ||
637 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800638 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700639 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800640
Faisal Latif6492cdf2008-07-24 20:50:45 -0700641 spin_unlock_irqrestore(
642 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800643 nes_retrans_expired(cm_node);
644 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700645 spin_lock_irqsave(&cm_node->retrans_list_lock,
646 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600647 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800648 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800649 atomic_inc(&send_entry->skb->users);
650 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700651 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
652 "for node %p, jiffies = %lu, time to send = "
653 "%lu, retranscount = %u, send_entry->seq_num = "
654 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
655 "0x%08X\n", send_entry, cm_node, jiffies,
656 send_entry->timetosend,
657 send_entry->retranscount,
658 send_entry->seq_num,
659 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800660
Faisal Latif6492cdf2008-07-24 20:50:45 -0700661 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
662 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800663 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700664 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800665 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700666 nes_debug(NES_DBG_CM, "rexmit failed for "
667 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800668 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800669 send_entry->retrycount--;
670 nexttimeout = jiffies + NES_SHORT_TIME;
671 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600672 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800673 } else {
674 cm_packets_sent++;
675 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700676 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
677 "%u, retry count = %u.\n",
678 send_entry->retranscount,
679 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800680 if (send_entry->send_retrans) {
681 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700682 timetosend = (NES_RETRY_TIMEOUT <<
683 (NES_DEFAULT_RETRANS - send_entry->retranscount));
684
Faisal Latif6492cdf2008-07-24 20:50:45 -0700685 send_entry->timetosend = jiffies +
Faisal Latif4e9c3902009-04-27 13:39:36 -0700686 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700687 if (nexttimeout > send_entry->timetosend ||
688 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800689 nexttimeout = send_entry->timetosend;
690 settimer = 1;
691 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800692 } else {
693 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700694 close_when_complete =
695 send_entry->close_when_complete;
696 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
697 cm_node, cm_node->state);
698 free_retrans_entry(cm_node);
699 if (close_when_complete)
700 rem_ref_cm_node(cm_node->cm_core,
701 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800702 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700703 } while (0);
704
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800705 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700706 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800707 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800708
709 if (settimer) {
710 if (!timer_pending(&cm_core->tcp_timer)) {
711 cm_core->tcp_timer.expires = nexttimeout;
712 add_timer(&cm_core->tcp_timer);
713 }
714 }
715}
716
717
718/**
719 * send_syn
720 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700721static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
722 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800723{
724 int ret;
725 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800726 char optionsbuffer[sizeof(struct option_mss) +
Faisal Latif6492cdf2008-07-24 20:50:45 -0700727 sizeof(struct option_windowscale) + sizeof(struct option_base) +
728 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800729
730 int optionssize = 0;
731 /* Sending MSS option */
732 union all_known_options *options;
733
734 if (!cm_node)
735 return -EINVAL;
736
737 options = (union all_known_options *)&optionsbuffer[optionssize];
738 options->as_mss.optionnum = OPTION_NUMBER_MSS;
739 options->as_mss.length = sizeof(struct option_mss);
740 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
741 optionssize += sizeof(struct option_mss);
742
743 options = (union all_known_options *)&optionsbuffer[optionssize];
744 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
745 options->as_windowscale.length = sizeof(struct option_windowscale);
746 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
747 optionssize += sizeof(struct option_windowscale);
748
Faisal Latif6492cdf2008-07-24 20:50:45 -0700749 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800750 options = (union all_known_options *)&optionsbuffer[optionssize];
751 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
752 options->as_base.length = sizeof(struct option_base);
753 optionssize += sizeof(struct option_base);
754 /* we need the size to be a multiple of 4 */
755 options = (union all_known_options *)&optionsbuffer[optionssize];
756 options->as_end = 1;
757 optionssize += 1;
758 options = (union all_known_options *)&optionsbuffer[optionssize];
759 options->as_end = 1;
760 optionssize += 1;
761 }
762
763 options = (union all_known_options *)&optionsbuffer[optionssize];
764 options->as_end = OPTION_NUMBER_END;
765 optionssize += 1;
766
Faisal Latif6492cdf2008-07-24 20:50:45 -0700767 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800768 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800769 if (!skb) {
770 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
771 return -1;
772 }
773
774 if (sendack)
775 flags |= SET_ACK;
776
777 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
778 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
779
780 return ret;
781}
782
783
784/**
785 * send_reset
786 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700787static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800788{
789 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800790 int flags = SET_RST | SET_ACK;
791
Faisal Latif6492cdf2008-07-24 20:50:45 -0700792 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800793 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800794 if (!skb) {
795 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -0800796 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800797 }
798
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800799 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
800 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
801
802 return ret;
803}
804
805
806/**
807 * send_ack
808 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700809static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800810{
811 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700812
813 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800814 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800815
816 if (!skb) {
817 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
818 return -1;
819 }
820
821 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
822 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
823
824 return ret;
825}
826
827
828/**
829 * send_fin
830 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700831static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800832{
833 int ret;
834
835 /* if we didn't get a frame get one */
836 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800837 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800838
839 if (!skb) {
840 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
841 return -1;
842 }
843
844 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
845 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
846
847 return ret;
848}
849
850
851/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800852 * find_node - find a cm node that matches the reference cm node
853 */
854static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
855 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
856{
857 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800858 struct list_head *hte;
859 struct nes_cm_node *cm_node;
860
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800861 /* get a handle on the hte */
862 hte = &cm_core->connected_nodes;
863
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800864 /* walk list and find cm_node associated with this session ID */
865 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700866 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800867 /* compare quad, return node handle if a match */
868 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
869 cm_node->loc_addr, cm_node->loc_port,
870 loc_addr, loc_port,
871 cm_node->rem_addr, cm_node->rem_port,
872 rem_addr, rem_port);
873 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
874 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
875 add_ref_cm_node(cm_node);
876 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
877 return cm_node;
878 }
879 }
880 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
881
882 /* no owner node */
883 return NULL;
884}
885
886
887/**
888 * find_listener - find a cm node listening on this addr-port pair
889 */
890static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
891 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
892{
893 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800894 struct nes_cm_listener *listen_node;
895
896 /* walk list and find cm_node associated with this session ID */
897 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700898 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800899 /* compare node pair, return node handle if a match */
900 if (((listen_node->loc_addr == dst_addr) ||
901 listen_node->loc_addr == 0x00000000) &&
902 (listen_node->loc_port == dst_port) &&
903 (listener_state & listen_node->listener_state)) {
904 atomic_inc(&listen_node->ref_count);
905 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
906 return listen_node;
907 }
908 }
909 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
910
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800911 /* no listener */
912 return NULL;
913}
914
915
916/**
917 * add_hte_node - add a cm node to the hash table
918 */
919static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
920{
921 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800922 struct list_head *hte;
923
924 if (!cm_node || !cm_core)
925 return -EINVAL;
926
Faisal Latif6492cdf2008-07-24 20:50:45 -0700927 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
928 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800929
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800930 spin_lock_irqsave(&cm_core->ht_lock, flags);
931
932 /* get a handle on the hash table element (list head for this slot) */
933 hte = &cm_core->connected_nodes;
934 list_add_tail(&cm_node->list, hte);
935 atomic_inc(&cm_core->ht_node_cnt);
936
937 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
938
939 return 0;
940}
941
942
943/**
944 * mini_cm_dec_refcnt_listen
945 */
946static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700947 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800948{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800949 int ret = -EINVAL;
950 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800951 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700952 struct list_head *list_pos = NULL;
953 struct list_head *list_temp = NULL;
954 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -0600955 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700956
957 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
958 "refcnt=%d\n", listener, free_hanging_nodes,
959 atomic_read(&listener->ref_count));
960 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -0600961 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700962 if (free_hanging_nodes) {
963 spin_lock_irqsave(&cm_core->ht_lock, flags);
964 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600965 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700966 cm_node = container_of(list_pos, struct nes_cm_node,
967 list);
968 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -0600969 (!cm_node->accelerated)) {
970 add_ref_cm_node(cm_node);
971 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700972 }
973 }
974 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
975 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600976
977 list_for_each_safe(list_pos, list_temp, &reset_list) {
978 cm_node = container_of(list_pos, struct nes_cm_node,
Faisal Latif9d5ab132009-03-06 15:15:01 -0800979 reset_entry);
980 {
981 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -0800982 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800983 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
984 rem_ref_cm_node(cm_node->cm_core, cm_node);
985 } else {
986 if (!loopback) {
987 cleanup_retrans_entry(cm_node);
988 err = send_reset(cm_node, NULL);
989 if (err) {
990 cm_node->state =
991 NES_CM_STATE_CLOSED;
992 WARN_ON(1);
993 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -0800994 old_state = cm_node->state;
995 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
996 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
997 rem_ref_cm_node(
998 cm_node->cm_core,
999 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001000 }
1001 } else {
1002 struct nes_cm_event event;
1003
1004 event.cm_node = loopback;
1005 event.cm_info.rem_addr =
1006 loopback->rem_addr;
1007 event.cm_info.loc_addr =
1008 loopback->loc_addr;
1009 event.cm_info.rem_port =
1010 loopback->rem_port;
1011 event.cm_info.loc_port =
1012 loopback->loc_port;
1013 event.cm_info.cm_id = loopback->cm_id;
Faisal Latif43093b92010-02-12 19:57:04 +00001014 add_ref_cm_node(loopback);
1015 loopback->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001016 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001017 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001018
Faisal Latif9d5ab132009-03-06 15:15:01 -08001019 rem_ref_cm_node(cm_node->cm_core,
1020 cm_node);
1021
1022 }
1023 }
1024 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001025 }
1026
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001027 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1028 if (!atomic_dec_return(&listener->ref_count)) {
1029 list_del(&listener->list);
1030
1031 /* decrement our listen node count */
1032 atomic_dec(&cm_core->listen_node_cnt);
1033
1034 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1035
1036 if (listener->nesvnic) {
1037 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1038 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1039 }
1040
1041 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1042
1043 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001044 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001045 ret = 0;
Faisal Latif6e10d2e2010-02-12 19:55:03 +00001046 atomic_inc(&cm_listens_destroyed);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001047 } else {
1048 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1049 }
1050 if (listener) {
1051 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1052 nes_debug(NES_DBG_CM, "destroying listener (%p)"
1053 " with non-zero pending accepts=%u\n",
1054 listener, atomic_read(&listener->pend_accepts_cnt));
1055 }
1056
1057 return ret;
1058}
1059
1060
1061/**
1062 * mini_cm_del_listen
1063 */
1064static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1065 struct nes_cm_listener *listener)
1066{
1067 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1068 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1069 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1070}
1071
1072
1073/**
1074 * mini_cm_accelerated
1075 */
1076static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1077 struct nes_cm_node *cm_node)
1078{
1079 u32 was_timer_set;
1080 cm_node->accelerated = 1;
1081
1082 if (cm_node->accept_pend) {
1083 BUG_ON(!cm_node->listener);
1084 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001085 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001086 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1087 }
1088
1089 was_timer_set = timer_pending(&cm_core->tcp_timer);
1090 if (!was_timer_set) {
1091 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1092 add_timer(&cm_core->tcp_timer);
1093 }
1094
1095 return 0;
1096}
1097
1098
1099/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001100 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001101 */
Faisal Latif7a576df2009-12-09 15:54:33 -08001102static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001103{
1104 struct rtable *rt;
1105 struct flowi fl;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001106 struct neighbour *neigh;
Faisal Latif7a576df2009-12-09 15:54:33 -08001107 int rc = arpindex;
1108 struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001109
1110 memset(&fl, 0, sizeof fl);
1111 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1112 if (ip_route_output_key(&init_net, &rt, &fl)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001113 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001114 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001115 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001116 }
1117
Bob Sharp7191a0a2008-10-03 12:21:19 -07001118 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1119 if (neigh) {
1120 if (neigh->nud_state & NUD_VALID) {
1121 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001122 " is %pM, Gateway is 0x%08X \n", dst_ip,
1123 neigh->ha, ntohl(rt->rt_gateway));
Faisal Latif7a576df2009-12-09 15:54:33 -08001124
1125 if (arpindex >= 0) {
1126 if (!memcmp(nesadapter->arp_table[arpindex].mac_addr,
1127 neigh->ha, ETH_ALEN)){
1128 /* Mac address same as in nes_arp_table */
1129 neigh_release(neigh);
1130 ip_rt_put(rt);
1131 return rc;
1132 }
1133
1134 nes_manage_arp_cache(nesvnic->netdev,
1135 nesadapter->arp_table[arpindex].mac_addr,
1136 dst_ip, NES_ARP_DELETE);
1137 }
1138
Bob Sharp7191a0a2008-10-03 12:21:19 -07001139 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1140 dst_ip, NES_ARP_ADD);
1141 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1142 NES_ARP_RESOLVE);
1143 }
1144 neigh_release(neigh);
1145 }
1146
1147 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1148 neigh_event_send(rt->u.dst.neighbour, NULL);
1149
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001150 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001151 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001152}
1153
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001154/**
1155 * make_cm_node - create a new instance of a cm node
1156 */
1157static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1158 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1159 struct nes_cm_listener *listener)
1160{
1161 struct nes_cm_node *cm_node;
1162 struct timespec ts;
Faisal Latif7a576df2009-12-09 15:54:33 -08001163 int oldarpindex = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001164 int arpindex = 0;
1165 struct nes_device *nesdev;
1166 struct nes_adapter *nesadapter;
1167
1168 /* create an hte and cm_node for this instance */
1169 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1170 if (!cm_node)
1171 return NULL;
1172
1173 /* set our node specific transport info */
1174 cm_node->loc_addr = cm_info->loc_addr;
1175 cm_node->rem_addr = cm_info->rem_addr;
1176 cm_node->loc_port = cm_info->loc_port;
1177 cm_node->rem_port = cm_info->rem_port;
1178 cm_node->send_write0 = send_first;
Harvey Harrison63779432008-10-31 00:56:00 -07001179 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1180 &cm_node->loc_addr, cm_node->loc_port,
1181 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001182 cm_node->listener = listener;
1183 cm_node->netdev = nesvnic->netdev;
1184 cm_node->cm_id = cm_info->cm_id;
1185 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1186
Faisal Latif6492cdf2008-07-24 20:50:45 -07001187 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1188 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001189
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001190 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001191
1192 cm_node->loopbackpartner = NULL;
1193 atomic_set(&cm_node->ref_count, 1);
1194 /* associate our parent CM core */
1195 cm_node->cm_core = cm_core;
1196 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1197 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1198 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1199 NES_CM_DEFAULT_RCV_WND_SCALE;
1200 ts = current_kernel_time();
1201 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1202 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Chien Tungf2b2b592008-03-20 19:55:30 -05001203 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001204 cm_node->tcp_cntxt.rcv_nxt = 0;
1205 /* get a unique session ID , add thread_id to an upcounter to handle race */
1206 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001207 cm_node->conn_type = cm_info->conn_type;
1208 cm_node->apbvt_set = 0;
1209 cm_node->accept_pend = 0;
1210
1211 cm_node->nesvnic = nesvnic;
1212 /* get some device handles, for arp lookup */
1213 nesdev = nesvnic->nesdev;
1214 nesadapter = nesdev->nesadapter;
1215
1216 cm_node->loopbackpartner = NULL;
Faisal Latif7a576df2009-12-09 15:54:33 -08001217
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001218 /* get the mac addr for the remote node */
Chien Tung1ee86552008-11-21 20:51:04 -06001219 if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1220 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
Faisal Latif7a576df2009-12-09 15:54:33 -08001221 else {
1222 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1223 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);
1224
1225 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001226 if (arpindex < 0) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001227 kfree(cm_node);
1228 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001229 }
1230
1231 /* copy the mac addr to node context */
1232 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001233 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1234 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001235
1236 add_hte_node(cm_core, cm_node);
1237 atomic_inc(&cm_nodes_created);
1238
1239 return cm_node;
1240}
1241
1242
1243/**
1244 * add_ref_cm_node - destroy an instance of a cm node
1245 */
1246static int add_ref_cm_node(struct nes_cm_node *cm_node)
1247{
1248 atomic_inc(&cm_node->ref_count);
1249 return 0;
1250}
1251
1252
1253/**
1254 * rem_ref_cm_node - destroy an instance of a cm node
1255 */
1256static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001257 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001258{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001259 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001260 struct nes_qp *nesqp;
1261
1262 if (!cm_node)
1263 return -EINVAL;
1264
1265 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1266 if (atomic_dec_return(&cm_node->ref_count)) {
1267 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1268 return 0;
1269 }
1270 list_del(&cm_node->list);
1271 atomic_dec(&cm_core->ht_node_cnt);
1272 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1273
1274 /* if the node is destroyed before connection was accelerated */
1275 if (!cm_node->accelerated && cm_node->accept_pend) {
1276 BUG_ON(!cm_node->listener);
1277 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1278 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1279 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001280 WARN_ON(cm_node->send_entry);
1281 if (cm_node->recv_entry)
1282 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001283 if (cm_node->listener) {
1284 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1285 } else {
1286 if (cm_node->apbvt_set && cm_node->nesvnic) {
1287 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001288 PCI_FUNC(
1289 cm_node->nesvnic->nesdev->pcidev->devfn),
1290 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001291 }
1292 }
1293
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001294 atomic_dec(&cm_core->node_cnt);
1295 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001296 nesqp = cm_node->nesqp;
1297 if (nesqp) {
1298 nesqp->cm_node = NULL;
1299 nes_rem_ref(&nesqp->ibqp);
1300 cm_node->nesqp = NULL;
1301 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001302
Faisal Latif6492cdf2008-07-24 20:50:45 -07001303 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001304 return 0;
1305}
1306
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001307/**
1308 * process_options
1309 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001310static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1311 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001312{
1313 u32 tmp;
1314 u32 offset = 0;
1315 union all_known_options *all_options;
1316 char got_mss_option = 0;
1317
1318 while (offset < optionsize) {
1319 all_options = (union all_known_options *)(optionsloc + offset);
1320 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001321 case OPTION_NUMBER_END:
1322 offset = optionsize;
1323 break;
1324 case OPTION_NUMBER_NONE:
1325 offset += 1;
1326 continue;
1327 case OPTION_NUMBER_MSS:
1328 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1329 "Size: %d\n", __func__,
1330 all_options->as_mss.length, offset, optionsize);
1331 got_mss_option = 1;
1332 if (all_options->as_mss.length != 4) {
1333 return 1;
1334 } else {
1335 tmp = ntohs(all_options->as_mss.mss);
1336 if (tmp > 0 && tmp <
1337 cm_node->tcp_cntxt.mss)
1338 cm_node->tcp_cntxt.mss = tmp;
1339 }
1340 break;
1341 case OPTION_NUMBER_WINDOW_SCALE:
1342 cm_node->tcp_cntxt.snd_wscale =
1343 all_options->as_windowscale.shiftcount;
1344 break;
1345 case OPTION_NUMBER_WRITE0:
1346 cm_node->send_write0 = 1;
1347 break;
1348 default:
1349 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1350 all_options->as_base.optionnum);
1351 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001352 }
1353 offset += all_options->as_base.length;
1354 }
1355 if ((!got_mss_option) && (syn_packet))
1356 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1357 return 0;
1358}
1359
Faisal Latif6492cdf2008-07-24 20:50:45 -07001360static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001361{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001362 atomic_inc(&cm_accel_dropped_pkts);
1363 dev_kfree_skb_any(skb);
1364}
1365
Faisal Latif9d5ab132009-03-06 15:15:01 -08001366static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001367{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001368 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1369 "refcnt=%d\n", cm_node, cm_node->state,
1370 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001371 switch (cm_node->state) {
1372 case NES_CM_STATE_SYN_RCVD:
1373 case NES_CM_STATE_SYN_SENT:
1374 case NES_CM_STATE_ESTABLISHED:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001375 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001376 cm_node->tcp_cntxt.rcv_nxt++;
1377 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001378 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001379 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001380 break;
Faisal Latifb1190d32009-12-09 15:54:32 -08001381 case NES_CM_STATE_MPAREQ_SENT:
1382 create_event(cm_node, NES_CM_EVENT_ABORTED);
1383 cm_node->tcp_cntxt.rcv_nxt++;
1384 cleanup_retrans_entry(cm_node);
1385 cm_node->state = NES_CM_STATE_CLOSED;
1386 add_ref_cm_node(cm_node);
1387 send_reset(cm_node, NULL);
1388 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001389 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001390 cm_node->tcp_cntxt.rcv_nxt++;
1391 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001392 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001393 send_ack(cm_node, NULL);
1394 /* Wait for ACK as this is simultanous close..
1395 * After we receive ACK, do not send anything..
1396 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001397 break;
1398 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001399 cm_node->tcp_cntxt.rcv_nxt++;
1400 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001401 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001402 send_ack(cm_node, NULL);
1403 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1404 break;
1405 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001406 cm_node->tcp_cntxt.rcv_nxt++;
1407 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001408 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001409 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001410 break;
1411 case NES_CM_STATE_TSA:
1412 default:
1413 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1414 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001415 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001416 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001417}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001418
Faisal Latif6492cdf2008-07-24 20:50:45 -07001419
1420static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1421 struct tcphdr *tcph)
1422{
1423
1424 int reset = 0; /* whether to send reset in case of err.. */
Faisal Latif183ecfa2008-11-21 20:50:46 -06001425 int passive_state;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001426 atomic_inc(&cm_resets_recvd);
1427 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1428 " refcnt=%d\n", cm_node, cm_node->state,
1429 atomic_read(&cm_node->ref_count));
1430 cleanup_retrans_entry(cm_node);
1431 switch (cm_node->state) {
1432 case NES_CM_STATE_SYN_SENT:
1433 case NES_CM_STATE_MPAREQ_SENT:
1434 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1435 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1436 cm_node->listener, cm_node->state);
1437 active_open_err(cm_node, skb, reset);
1438 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001439 case NES_CM_STATE_MPAREQ_RCVD:
1440 passive_state = atomic_add_return(1, &cm_node->passive_state);
1441 if (passive_state == NES_SEND_RESET_EVENT)
1442 create_event(cm_node, NES_CM_EVENT_RESET);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001443 cm_node->state = NES_CM_STATE_CLOSED;
1444 dev_kfree_skb_any(skb);
1445 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001446 case NES_CM_STATE_ESTABLISHED:
1447 case NES_CM_STATE_SYN_RCVD:
1448 case NES_CM_STATE_LISTENING:
1449 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1450 passive_open_err(cm_node, skb, reset);
1451 break;
1452 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001453 active_open_err(cm_node, skb, reset);
1454 break;
1455 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001456 drop_packet(skb);
1457 break;
Faisal Latif68237a02009-06-22 22:53:28 -07001458 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001459 case NES_CM_STATE_LAST_ACK:
1460 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001461 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001462 cm_node->state = NES_CM_STATE_CLOSED;
1463 rem_ref_cm_node(cm_node->cm_core, cm_node);
1464 drop_packet(skb);
1465 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001466 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001467 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001468 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001469 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001470}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001471
Faisal Latif9d5ab132009-03-06 15:15:01 -08001472
1473static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001474{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001475
Faisal Latif9d5ab132009-03-06 15:15:01 -08001476 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001477 int datasize = skb->len;
1478 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001479
1480 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1481 u32 res_type;
1482 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1483 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001484 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001485 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001486 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1487 "cm_node=%p listener=%p state=%d\n", __func__,
1488 __LINE__, cm_node, cm_node->listener,
1489 cm_node->state);
1490 active_open_err(cm_node, skb, 1);
1491 } else {
1492 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001493 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001494 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001495 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001496
1497 switch (cm_node->state) {
1498 case NES_CM_STATE_ESTABLISHED:
1499 if (res_type == NES_MPA_REQUEST_REJECT) {
1500 /*BIG problem as we are receiving the MPA.. So should
1501 * not be REJECT.. This is Passive Open.. We can
1502 * only receive it Reject for Active Open...*/
1503 WARN_ON(1);
1504 }
1505 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1506 type = NES_CM_EVENT_MPA_REQ;
1507 atomic_set(&cm_node->passive_state,
1508 NES_PASSIVE_STATE_INDICATED);
1509 break;
1510 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001511 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001512 if (res_type == NES_MPA_REQUEST_REJECT) {
1513 type = NES_CM_EVENT_MPA_REJECT;
1514 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1515 } else {
1516 type = NES_CM_EVENT_CONNECTED;
1517 cm_node->state = NES_CM_STATE_TSA;
1518 }
1519
1520 break;
1521 default:
1522 WARN_ON(1);
1523 break;
1524 }
1525 dev_kfree_skb_any(skb);
1526 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001527}
1528
1529static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1530{
1531 switch (cm_node->state) {
1532 case NES_CM_STATE_SYN_SENT:
1533 case NES_CM_STATE_MPAREQ_SENT:
1534 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1535 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1536 cm_node->listener, cm_node->state);
1537 active_open_err(cm_node, skb, 1);
1538 break;
1539 case NES_CM_STATE_ESTABLISHED:
1540 case NES_CM_STATE_SYN_RCVD:
1541 passive_open_err(cm_node, skb, 1);
1542 break;
1543 case NES_CM_STATE_TSA:
1544 default:
1545 drop_packet(skb);
1546 }
1547}
1548
1549static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1550 struct sk_buff *skb)
1551{
1552 int err;
1553
1554 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1555 if (err)
1556 active_open_err(cm_node, skb, 1);
1557
1558 return err;
1559}
1560
1561static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1562 struct sk_buff *skb)
1563{
1564 int err = 0;
1565 u32 seq;
1566 u32 ack_seq;
1567 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1568 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1569 u32 rcv_wnd;
1570 seq = ntohl(tcph->seq);
1571 ack_seq = ntohl(tcph->ack_seq);
1572 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1573 if (ack_seq != loc_seq_num)
1574 err = 1;
Faisal Latifc11470f2009-04-27 13:38:31 -07001575 else if (!between(seq, rcv_nxt, (rcv_nxt+rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001576 err = 1;
1577 if (err) {
1578 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1579 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1580 cm_node->listener, cm_node->state);
1581 indicate_pkt_err(cm_node, skb);
1582 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1583 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1584 rcv_wnd);
1585 }
1586 return err;
1587}
1588
1589/*
1590 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1591 * is created with a listener or it may comein as rexmitted packet which in
1592 * that case will be just dropped.
1593 */
1594
1595static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1596 struct tcphdr *tcph)
1597{
1598 int ret;
1599 u32 inc_sequence;
1600 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001601
1602 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001603 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001604 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001605
Faisal Latif6492cdf2008-07-24 20:50:45 -07001606 switch (cm_node->state) {
1607 case NES_CM_STATE_SYN_SENT:
1608 case NES_CM_STATE_MPAREQ_SENT:
1609 /* Rcvd syn on active open connection*/
1610 active_open_err(cm_node, skb, 1);
1611 break;
1612 case NES_CM_STATE_LISTENING:
1613 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001614 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1615 cm_node->listener->backlog) {
1616 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1617 "pressure \n");
1618 cm_backlog_drops++;
1619 passive_open_err(cm_node, skb, 0);
1620 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001621 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001622 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1623 1);
1624 if (ret) {
1625 passive_open_err(cm_node, skb, 0);
1626 /* drop pkt */
1627 break;
1628 }
1629 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1630 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001631 cm_node->accept_pend = 1;
1632 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1633
Faisal Latif6492cdf2008-07-24 20:50:45 -07001634 cm_node->state = NES_CM_STATE_SYN_RCVD;
1635 send_syn(cm_node, 1, skb);
1636 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001637 case NES_CM_STATE_CLOSED:
1638 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001639 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001640 send_reset(cm_node, skb);
1641 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001642 case NES_CM_STATE_TSA:
1643 case NES_CM_STATE_ESTABLISHED:
1644 case NES_CM_STATE_FIN_WAIT1:
1645 case NES_CM_STATE_FIN_WAIT2:
1646 case NES_CM_STATE_MPAREQ_RCVD:
1647 case NES_CM_STATE_LAST_ACK:
1648 case NES_CM_STATE_CLOSING:
1649 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001650 default:
1651 drop_packet(skb);
1652 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001653 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001654}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001655
Faisal Latif6492cdf2008-07-24 20:50:45 -07001656static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1657 struct tcphdr *tcph)
1658{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001659
Faisal Latif6492cdf2008-07-24 20:50:45 -07001660 int ret;
1661 u32 inc_sequence;
1662 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001663
Faisal Latif6492cdf2008-07-24 20:50:45 -07001664 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001665 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001666 inc_sequence = ntohl(tcph->seq);
1667 switch (cm_node->state) {
1668 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001669 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001670 /* active open */
1671 if (check_syn(cm_node, tcph, skb))
1672 return;
1673 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1674 /* setup options */
1675 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1676 if (ret) {
1677 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1678 cm_node);
1679 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001680 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001681 cleanup_retrans_entry(cm_node);
1682 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1683 send_mpa_request(cm_node, skb);
1684 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1685 break;
1686 case NES_CM_STATE_MPAREQ_RCVD:
1687 /* passive open, so should not be here */
1688 passive_open_err(cm_node, skb, 1);
1689 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001690 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001691 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1692 cleanup_retrans_entry(cm_node);
1693 cm_node->state = NES_CM_STATE_CLOSED;
1694 send_reset(cm_node, skb);
1695 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001696 case NES_CM_STATE_CLOSED:
1697 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1698 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001699 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001700 send_reset(cm_node, skb);
1701 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001702 case NES_CM_STATE_ESTABLISHED:
1703 case NES_CM_STATE_FIN_WAIT1:
1704 case NES_CM_STATE_FIN_WAIT2:
1705 case NES_CM_STATE_LAST_ACK:
1706 case NES_CM_STATE_TSA:
1707 case NES_CM_STATE_CLOSING:
1708 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001709 case NES_CM_STATE_MPAREQ_SENT:
1710 default:
1711 drop_packet(skb);
1712 break;
1713 }
1714}
1715
Faisal Latif109d67e2009-04-27 13:41:06 -07001716static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001717 struct tcphdr *tcph)
1718{
1719 int datasize = 0;
1720 u32 inc_sequence;
1721 u32 rem_seq_ack;
1722 u32 rem_seq;
Faisal Latif109d67e2009-04-27 13:41:06 -07001723 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06001724 int optionsize;
Faisal Latifabb77252008-11-21 20:50:52 -06001725 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001726
Faisal Latif6492cdf2008-07-24 20:50:45 -07001727 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07001728 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001729
1730 skb_pull(skb, tcph->doff << 2);
1731 inc_sequence = ntohl(tcph->seq);
1732 rem_seq = ntohl(tcph->seq);
1733 rem_seq_ack = ntohl(tcph->ack_seq);
1734 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001735 switch (cm_node->state) {
1736 case NES_CM_STATE_SYN_RCVD:
1737 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07001738 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001739 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1740 if (ret)
1741 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001742 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1743 cm_node->state = NES_CM_STATE_ESTABLISHED;
1744 if (datasize) {
1745 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001746 handle_rcv_mpa(cm_node, skb);
Faisal Latif109d67e2009-04-27 13:41:06 -07001747 } else /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001748 dev_kfree_skb_any(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001749 break;
1750 case NES_CM_STATE_ESTABLISHED:
1751 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001752 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001753 if (datasize) {
1754 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001755 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001756 } else
1757 drop_packet(skb);
1758 break;
1759 case NES_CM_STATE_MPAREQ_SENT:
1760 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1761 if (datasize) {
1762 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001763 handle_rcv_mpa(cm_node, skb);
Faisal Latif109d67e2009-04-27 13:41:06 -07001764 } else /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001765 dev_kfree_skb_any(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001766 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001767 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001768 cleanup_retrans_entry(cm_node);
1769 cm_node->state = NES_CM_STATE_CLOSED;
1770 send_reset(cm_node, skb);
1771 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001772 case NES_CM_STATE_CLOSED:
1773 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001774 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001775 send_reset(cm_node, skb);
1776 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001777 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07001778 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001779 cleanup_retrans_entry(cm_node);
1780 cm_node->state = NES_CM_STATE_CLOSED;
1781 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001782 rem_ref_cm_node(cm_node->cm_core, cm_node);
1783 drop_packet(skb);
1784 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001785 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001786 cleanup_retrans_entry(cm_node);
1787 drop_packet(skb);
1788 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1789 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001790 case NES_CM_STATE_SYN_SENT:
1791 case NES_CM_STATE_FIN_WAIT2:
1792 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001793 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001794 case NES_CM_STATE_UNKNOWN:
1795 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07001796 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001797 drop_packet(skb);
1798 break;
1799 }
Faisal Latif109d67e2009-04-27 13:41:06 -07001800 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001801}
1802
1803
1804
1805static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1806 struct sk_buff *skb, int optionsize, int passive)
1807{
1808 u8 *optionsloc = (u8 *)&tcph[1];
1809 if (optionsize) {
1810 if (process_options(cm_node, optionsloc, optionsize,
1811 (u32)tcph->syn)) {
1812 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1813 __func__, cm_node);
1814 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06001815 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001816 else
Faisal Latifabb77252008-11-21 20:50:52 -06001817 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001818 return 1;
1819 }
1820 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001821
1822 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1823 cm_node->tcp_cntxt.snd_wscale;
1824
Faisal Latif6492cdf2008-07-24 20:50:45 -07001825 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001826 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001827 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001828}
1829
Faisal Latif6492cdf2008-07-24 20:50:45 -07001830/*
1831 * active_open_err() will send reset() if flag set..
1832 * It will also send ABORT event.
1833 */
1834
1835static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1836 int reset)
1837{
1838 cleanup_retrans_entry(cm_node);
1839 if (reset) {
1840 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1841 "state=%d\n", cm_node, cm_node->state);
1842 add_ref_cm_node(cm_node);
1843 send_reset(cm_node, skb);
1844 } else
1845 dev_kfree_skb_any(skb);
1846
1847 cm_node->state = NES_CM_STATE_CLOSED;
1848 create_event(cm_node, NES_CM_EVENT_ABORTED);
1849}
1850
1851/*
1852 * passive_open_err() will either do a reset() or will free up the skb and
1853 * remove the cm_node.
1854 */
1855
1856static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1857 int reset)
1858{
1859 cleanup_retrans_entry(cm_node);
1860 cm_node->state = NES_CM_STATE_CLOSED;
1861 if (reset) {
1862 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1863 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1864 send_reset(cm_node, skb);
1865 } else {
1866 dev_kfree_skb_any(skb);
1867 rem_ref_cm_node(cm_node->cm_core, cm_node);
1868 }
1869}
1870
1871/*
1872 * free_retrans_entry() routines assumes that the retrans_list_lock has
1873 * been acquired before calling.
1874 */
1875static void free_retrans_entry(struct nes_cm_node *cm_node)
1876{
1877 struct nes_timer_entry *send_entry;
1878 send_entry = cm_node->send_entry;
1879 if (send_entry) {
1880 cm_node->send_entry = NULL;
1881 dev_kfree_skb_any(send_entry->skb);
1882 kfree(send_entry);
1883 rem_ref_cm_node(cm_node->cm_core, cm_node);
1884 }
1885}
1886
1887static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1888{
1889 unsigned long flags;
1890
1891 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1892 free_retrans_entry(cm_node);
1893 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1894}
1895
1896/**
1897 * process_packet
1898 * Returns skb if to be freed, else it will return NULL if already used..
1899 */
1900static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1901 struct nes_cm_core *cm_core)
1902{
1903 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1904 struct tcphdr *tcph = tcp_hdr(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001905 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07001906 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001907 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1908
1909 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1910 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1911 tcph->ack, tcph->rst, tcph->fin);
1912
1913 if (tcph->rst)
1914 pkt_type = NES_PKT_TYPE_RST;
1915 else if (tcph->syn) {
1916 pkt_type = NES_PKT_TYPE_SYN;
1917 if (tcph->ack)
1918 pkt_type = NES_PKT_TYPE_SYNACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001919 } else if (tcph->ack)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001920 pkt_type = NES_PKT_TYPE_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001921 if (tcph->fin)
1922 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001923
1924 switch (pkt_type) {
1925 case NES_PKT_TYPE_SYN:
1926 handle_syn_pkt(cm_node, skb, tcph);
1927 break;
1928 case NES_PKT_TYPE_SYNACK:
1929 handle_synack_pkt(cm_node, skb, tcph);
1930 break;
1931 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07001932 ret = handle_ack_pkt(cm_node, skb, tcph);
1933 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001934 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001935 break;
1936 case NES_PKT_TYPE_RST:
1937 handle_rst_pkt(cm_node, skb, tcph);
1938 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001939 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07001940 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08001941 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07001942 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001943 break;
1944 }
1945}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001946
1947/**
1948 * mini_cm_listen - create a listen node with params
1949 */
1950static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001951 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001952{
1953 struct nes_cm_listener *listener;
1954 unsigned long flags;
1955
1956 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1957 cm_info->loc_addr, cm_info->loc_port);
1958
1959 /* cannot have multiple matching listeners */
1960 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1961 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1962 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1963 /* find automatically incs ref count ??? */
1964 atomic_dec(&listener->ref_count);
1965 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1966 return NULL;
1967 }
1968
1969 if (!listener) {
1970 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1971 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1972 if (!listener) {
1973 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1974 return NULL;
1975 }
1976
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001977 listener->loc_addr = htonl(cm_info->loc_addr);
1978 listener->loc_port = htons(cm_info->loc_port);
1979 listener->reused_node = 0;
1980
1981 atomic_set(&listener->ref_count, 1);
1982 }
1983 /* pasive case */
1984 /* find already inc'ed the ref count */
1985 else {
1986 listener->reused_node = 1;
1987 }
1988
1989 listener->cm_id = cm_info->cm_id;
1990 atomic_set(&listener->pend_accepts_cnt, 0);
1991 listener->cm_core = cm_core;
1992 listener->nesvnic = nesvnic;
1993 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001994
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001995 listener->conn_type = cm_info->conn_type;
1996 listener->backlog = cm_info->backlog;
1997 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1998
1999 if (!listener->reused_node) {
2000 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2001 list_add(&listener->list, &cm_core->listen_list.list);
2002 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2003 atomic_inc(&cm_core->listen_node_cnt);
2004 }
2005
2006 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
2007 " listener = %p, backlog = %d, cm_id = %p.\n",
2008 cm_info->loc_addr, cm_info->loc_port,
2009 listener, listener->backlog, listener->cm_id);
2010
2011 return listener;
2012}
2013
2014
2015/**
2016 * mini_cm_connect - make a connection node with params
2017 */
Faisal Latif54c86a82008-09-30 14:47:27 -07002018static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002019 struct nes_vnic *nesvnic, u16 private_data_len,
2020 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002021{
2022 int ret = 0;
2023 struct nes_cm_node *cm_node;
2024 struct nes_cm_listener *loopbackremotelistener;
2025 struct nes_cm_node *loopbackremotenode;
2026 struct nes_cm_info loopback_cm_info;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002027 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
2028 struct ietf_mpa_frame *mpa_frame = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002029
2030 /* create a CM connection node */
2031 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2032 if (!cm_node)
2033 return NULL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002034 mpa_frame = &cm_node->mpa_frame;
Faisal Latif1cf078c2009-12-09 15:53:54 -08002035 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002036 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
2037 mpa_frame->rev = IETF_MPA_VERSION;
2038 mpa_frame->priv_data_len = htons(private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002039
Glenn Streiff7495ab62008-04-29 13:46:54 -07002040 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002041 cm_node->tcp_cntxt.client = 1;
2042 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2043
2044 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002045 loopbackremotelistener = find_listener(cm_core,
2046 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2047 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002048 if (loopbackremotelistener == NULL) {
2049 create_event(cm_node, NES_CM_EVENT_ABORTED);
2050 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002051 loopback_cm_info = *cm_info;
2052 loopback_cm_info.loc_port = cm_info->rem_port;
2053 loopback_cm_info.rem_port = cm_info->loc_port;
2054 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002055 loopbackremotenode = make_cm_node(cm_core, nesvnic,
2056 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002057 if (!loopbackremotenode) {
2058 rem_ref_cm_node(cm_node->cm_core, cm_node);
2059 return NULL;
2060 }
2061 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002062 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002063 loopbackremotenode->tcp_cntxt.rcv_wscale =
2064 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002065 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002066 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2067 private_data_len);
2068 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002069
Faisal Latif6492cdf2008-07-24 20:50:45 -07002070 /* we are done handling this state. */
2071 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002072 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002073 cm_node->tcp_cntxt.rcv_nxt =
2074 loopbackremotenode->tcp_cntxt.loc_seq_num;
2075 loopbackremotenode->tcp_cntxt.rcv_nxt =
2076 cm_node->tcp_cntxt.loc_seq_num;
2077 cm_node->tcp_cntxt.max_snd_wnd =
2078 loopbackremotenode->tcp_cntxt.rcv_wnd;
2079 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2080 cm_node->tcp_cntxt.rcv_wnd;
2081 cm_node->tcp_cntxt.snd_wnd =
2082 loopbackremotenode->tcp_cntxt.rcv_wnd;
2083 loopbackremotenode->tcp_cntxt.snd_wnd =
2084 cm_node->tcp_cntxt.rcv_wnd;
2085 cm_node->tcp_cntxt.snd_wscale =
2086 loopbackremotenode->tcp_cntxt.rcv_wscale;
2087 loopbackremotenode->tcp_cntxt.snd_wscale =
2088 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002089 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002090 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2091 }
2092 return cm_node;
2093 }
2094
2095 /* set our node side to client (active) side */
2096 cm_node->tcp_cntxt.client = 1;
2097 /* init our MPA frame ptr */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002098 memcpy(mpa_frame->priv_data, private_data, private_data_len);
2099
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002100 cm_node->mpa_frame_size = mpa_frame_size;
2101
2102 /* send a syn and goto syn sent state */
2103 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002104 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002105
Faisal Latif6492cdf2008-07-24 20:50:45 -07002106 if (ret) {
2107 /* error in sending the syn free up the cm_node struct */
2108 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2109 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2110 cm_node->rem_addr, cm_node->rem_port, cm_node,
2111 cm_node->cm_id);
2112 rem_ref_cm_node(cm_node->cm_core, cm_node);
2113 cm_node = NULL;
2114 }
2115
2116 if (cm_node)
2117 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2118 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2119 cm_node->rem_addr, cm_node->rem_port, cm_node,
2120 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002121
2122 return cm_node;
2123}
2124
2125
2126/**
2127 * mini_cm_accept - accept a connection
2128 * This function is never called
2129 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002130static int mini_cm_accept(struct nes_cm_core *cm_core,
2131 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002132{
2133 return 0;
2134}
2135
2136
2137/**
2138 * mini_cm_reject - reject and teardown a connection
2139 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002140static int mini_cm_reject(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002141 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002142{
2143 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002144 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002145 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002146 struct nes_cm_event event;
2147 struct iw_cm_id *cm_id = cm_node->cm_id;
2148 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002149
Faisal Latif6492cdf2008-07-24 20:50:45 -07002150 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2151 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002152
Faisal Latif6492cdf2008-07-24 20:50:45 -07002153 if (cm_node->tcp_cntxt.client)
2154 return ret;
2155 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002156
Faisal Latif9d5ab132009-03-06 15:15:01 -08002157 if (!loopback) {
2158 passive_state = atomic_add_return(1, &cm_node->passive_state);
2159 if (passive_state == NES_SEND_RESET_EVENT) {
2160 cm_node->state = NES_CM_STATE_CLOSED;
2161 rem_ref_cm_node(cm_core, cm_node);
2162 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002163 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2164 rem_ref_cm_node(cm_core, cm_node);
2165 } else {
2166 ret = send_mpa_reject(cm_node);
2167 if (ret) {
2168 cm_node->state = NES_CM_STATE_CLOSED;
2169 err = send_reset(cm_node, NULL);
2170 if (err)
2171 WARN_ON(1);
2172 } else
2173 cm_id->add_ref(cm_id);
2174 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002175 }
2176 } else {
2177 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002178 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2179 rem_ref_cm_node(cm_core, cm_node);
2180 rem_ref_cm_node(cm_core, loopback);
2181 } else {
2182 event.cm_node = loopback;
2183 event.cm_info.rem_addr = loopback->rem_addr;
2184 event.cm_info.loc_addr = loopback->loc_addr;
2185 event.cm_info.rem_port = loopback->rem_port;
2186 event.cm_info.loc_port = loopback->loc_port;
2187 event.cm_info.cm_id = loopback->cm_id;
2188 cm_event_mpa_reject(&event);
2189 rem_ref_cm_node(cm_core, cm_node);
2190 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002191
Faisal Latifc5a7d482009-12-09 15:54:08 -08002192 cm_id = loopback->cm_id;
2193 rem_ref_cm_node(cm_core, loopback);
2194 cm_id->rem_ref(cm_id);
2195 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002196 }
2197
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002198 return ret;
2199}
2200
2201
2202/**
2203 * mini_cm_close
2204 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002205static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002206{
2207 int ret = 0;
2208
2209 if (!cm_core || !cm_node)
2210 return -EINVAL;
2211
2212 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002213 case NES_CM_STATE_SYN_RCVD:
2214 case NES_CM_STATE_SYN_SENT:
2215 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2216 case NES_CM_STATE_ESTABLISHED:
2217 case NES_CM_STATE_ACCEPTING:
2218 case NES_CM_STATE_MPAREQ_SENT:
2219 case NES_CM_STATE_MPAREQ_RCVD:
2220 cleanup_retrans_entry(cm_node);
2221 send_reset(cm_node, NULL);
2222 break;
2223 case NES_CM_STATE_CLOSE_WAIT:
2224 cm_node->state = NES_CM_STATE_LAST_ACK;
2225 send_fin(cm_node, NULL);
2226 break;
2227 case NES_CM_STATE_FIN_WAIT1:
2228 case NES_CM_STATE_FIN_WAIT2:
2229 case NES_CM_STATE_LAST_ACK:
2230 case NES_CM_STATE_TIME_WAIT:
2231 case NES_CM_STATE_CLOSING:
2232 ret = -1;
2233 break;
2234 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002235 cleanup_retrans_entry(cm_node);
2236 send_reset(cm_node, NULL);
2237 break;
2238 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002239 case NES_CM_STATE_UNKNOWN:
2240 case NES_CM_STATE_INITED:
2241 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002242 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002243 ret = rem_ref_cm_node(cm_core, cm_node);
2244 break;
2245 case NES_CM_STATE_TSA:
2246 if (cm_node->send_entry)
2247 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2248 "send_entry=%p\n", cm_node->send_entry);
2249 ret = rem_ref_cm_node(cm_core, cm_node);
2250 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002251 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002252 return ret;
2253}
2254
2255
2256/**
2257 * recv_pkt - recv an ETHERNET packet, and process it through CM
2258 * node state machine
2259 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002260static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002261 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002262{
2263 struct nes_cm_node *cm_node = NULL;
2264 struct nes_cm_listener *listener = NULL;
2265 struct iphdr *iph;
2266 struct tcphdr *tcph;
2267 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002268 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002269 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002270
Faisal Latif6492cdf2008-07-24 20:50:45 -07002271 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002272 return 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002273 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002274 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002275 }
2276
2277 iph = (struct iphdr *)skb->data;
2278 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002279
2280 nfo.loc_addr = ntohl(iph->daddr);
2281 nfo.loc_port = ntohs(tcph->dest);
2282 nfo.rem_addr = ntohl(iph->saddr);
2283 nfo.rem_port = ntohs(tcph->source);
2284
Harvey Harrison03080e52009-01-10 21:45:42 -08002285 tmp_daddr = cpu_to_be32(iph->daddr);
2286 tmp_saddr = cpu_to_be32(iph->saddr);
2287
Harvey Harrison63779432008-10-31 00:56:00 -07002288 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002289 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002290
Faisal Latif6492cdf2008-07-24 20:50:45 -07002291 do {
2292 cm_node = find_node(cm_core,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002293 nfo.rem_port, nfo.rem_addr,
2294 nfo.loc_port, nfo.loc_addr);
2295
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002296 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002297 /* Only type of packet accepted are for */
2298 /* the PASSIVE open (syn only) */
2299 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002300 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002301 break;
2302 }
2303 listener = find_listener(cm_core, nfo.loc_addr,
2304 nfo.loc_port,
2305 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002306 if (!listener) {
2307 nfo.cm_id = NULL;
2308 nfo.conn_type = 0;
2309 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2310 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002311 break;
2312 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002313 nfo.cm_id = listener->cm_id;
2314 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002315 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2316 listener);
2317 if (!cm_node) {
2318 nes_debug(NES_DBG_CM, "Unable to allocate "
2319 "node\n");
2320 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002321 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002322 dev_kfree_skb_any(skb);
2323 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002324 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002325 if (!tcph->rst && !tcph->fin) {
2326 cm_node->state = NES_CM_STATE_LISTENING;
2327 } else {
2328 cm_packets_dropped++;
2329 rem_ref_cm_node(cm_core, cm_node);
2330 dev_kfree_skb_any(skb);
2331 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002332 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002333 add_ref_cm_node(cm_node);
2334 } else if (cm_node->state == NES_CM_STATE_TSA) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002335 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002336 atomic_inc(&cm_accel_dropped_pkts);
2337 dev_kfree_skb_any(skb);
2338 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002339 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002340 skb_reset_network_header(skb);
2341 skb_set_transport_header(skb, sizeof(*tcph));
2342 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002343 process_packet(cm_node, skb, cm_core);
2344 rem_ref_cm_node(cm_core, cm_node);
2345 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002346 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002347}
2348
2349
2350/**
2351 * nes_cm_alloc_core - allocate a top level instance of a cm core
2352 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002353static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002354{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002355 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002356
2357 /* setup the CM core */
2358 /* alloc top level core control structure */
2359 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2360 if (!cm_core)
2361 return NULL;
2362
2363 INIT_LIST_HEAD(&cm_core->connected_nodes);
2364 init_timer(&cm_core->tcp_timer);
2365 cm_core->tcp_timer.function = nes_cm_timer_tick;
2366
2367 cm_core->mtu = NES_CM_DEFAULT_MTU;
2368 cm_core->state = NES_CM_STATE_INITED;
2369 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2370
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002371 atomic_set(&cm_core->events_posted, 0);
2372
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002373 cm_core->api = &nes_cm_api;
2374
2375 spin_lock_init(&cm_core->ht_lock);
2376 spin_lock_init(&cm_core->listen_list_lock);
2377
2378 INIT_LIST_HEAD(&cm_core->listen_list.list);
2379
2380 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2381
2382 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2383 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2384 cm_core->post_event = nes_cm_post_event;
2385 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2386 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2387
2388 print_core(cm_core);
2389 return cm_core;
2390}
2391
2392
2393/**
2394 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2395 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002396static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002397{
2398 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2399
2400 if (!cm_core)
2401 return -EINVAL;
2402
2403 barrier();
2404
2405 if (timer_pending(&cm_core->tcp_timer)) {
2406 del_timer(&cm_core->tcp_timer);
2407 }
2408
2409 destroy_workqueue(cm_core->event_wq);
2410 destroy_workqueue(cm_core->disconn_wq);
2411 nes_debug(NES_DBG_CM, "\n");
2412 kfree(cm_core);
2413
2414 return 0;
2415}
2416
2417
2418/**
2419 * mini_cm_get
2420 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002421static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002422{
2423 return cm_core->state;
2424}
2425
2426
2427/**
2428 * mini_cm_set
2429 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002430static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002431{
2432 int ret = 0;
2433
2434 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002435 case NES_CM_SET_PKT_SIZE:
2436 cm_core->mtu = value;
2437 break;
2438 case NES_CM_SET_FREE_PKT_Q_SIZE:
2439 cm_core->free_tx_pkt_max = value;
2440 break;
2441 default:
2442 /* unknown set option */
2443 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002444 }
2445
2446 return ret;
2447}
2448
2449
2450/**
2451 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2452 * successfully exchanged when this is called
2453 */
2454static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2455{
2456 int ret = 0;
2457
2458 if (!nesqp)
2459 return -EINVAL;
2460
2461 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2462 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2463 NES_QPCONTEXT_MISC_DROS);
2464
2465 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2466 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2467
2468 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2469
2470 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2471
2472 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2473 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2474
2475 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2476 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2477 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2478
2479 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2480 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2481 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2482
2483 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2484 nesqp->nesqp_context->ts_recent = 0;
2485 nesqp->nesqp_context->ts_age = 0;
2486 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2487 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2488 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2489 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2490 cm_node->tcp_cntxt.rcv_wscale);
2491 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2492 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2493 nesqp->nesqp_context->srtt = 0;
2494 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2495 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2496 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2497 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2498 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2499 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2500
2501 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2502 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2503 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2504 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2505 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2506 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2507 le32_to_cpu(nesqp->nesqp_context->misc));
2508 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2509 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2510 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2511
2512 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2513 cm_node->state = NES_CM_STATE_TSA;
2514
2515 return ret;
2516}
2517
2518
2519/**
2520 * nes_cm_disconn
2521 */
2522int nes_cm_disconn(struct nes_qp *nesqp)
2523{
Don Wood873fcdd2009-09-05 20:36:37 -07002524 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002525
Don Wood873fcdd2009-09-05 20:36:37 -07002526 work = kzalloc(sizeof *work, GFP_ATOMIC);
2527 if (!work)
2528 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002529
Don Wood873fcdd2009-09-05 20:36:37 -07002530 nes_add_ref(&nesqp->ibqp);
2531 work->nesqp = nesqp;
2532 INIT_WORK(&work->work, nes_disconnect_worker);
2533 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002534 return 0;
2535}
2536
2537
2538/**
2539 * nes_disconnect_worker
2540 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002541static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002542{
Don Wood873fcdd2009-09-05 20:36:37 -07002543 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2544 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002545
Don Wood873fcdd2009-09-05 20:36:37 -07002546 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002547 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2548 nesqp->last_aeq, nesqp->hwqp.qp_id);
2549 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002550 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002551}
2552
2553
2554/**
2555 * nes_cm_disconn_true
2556 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002557static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002558{
2559 unsigned long flags;
2560 int ret = 0;
2561 struct iw_cm_id *cm_id;
2562 struct iw_cm_event cm_event;
2563 struct nes_vnic *nesvnic;
2564 u16 last_ae;
2565 u8 original_hw_tcp_state;
2566 u8 original_ibqp_state;
Don Woodb29a4fc2009-09-05 20:36:39 -07002567 enum iw_cm_event_type disconn_status = IW_CM_EVENT_STATUS_OK;
2568 int issue_disconn = 0;
2569 int issue_close = 0;
2570 int issue_flush = 0;
2571 u32 flush_q = NES_CQP_FLUSH_RQ;
2572 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002573
2574 if (!nesqp) {
2575 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2576 return -1;
2577 }
2578
2579 spin_lock_irqsave(&nesqp->lock, flags);
2580 cm_id = nesqp->cm_id;
2581 /* make sure we havent already closed this connection */
2582 if (!cm_id) {
2583 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2584 nesqp->hwqp.qp_id);
2585 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002586 return -1;
2587 }
2588
2589 nesvnic = to_nesvnic(nesqp->ibqp.device);
2590 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2591
2592 original_hw_tcp_state = nesqp->hw_tcp_state;
2593 original_ibqp_state = nesqp->ibqp_state;
2594 last_ae = nesqp->last_aeq;
2595
Don Woodb29a4fc2009-09-05 20:36:39 -07002596 if (nesqp->term_flags) {
2597 issue_disconn = 1;
2598 issue_close = 1;
2599 nesqp->cm_id = NULL;
2600 if (nesqp->flush_issued == 0) {
2601 nesqp->flush_issued = 1;
2602 issue_flush = 1;
2603 }
2604 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2605 ((original_ibqp_state == IB_QPS_RTS) &&
2606 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2607 issue_disconn = 1;
2608 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
2609 disconn_status = IW_CM_EVENT_STATUS_RESET;
2610 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002611
Don Woodb29a4fc2009-09-05 20:36:39 -07002612 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2613 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2614 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2615 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2616 issue_close = 1;
2617 nesqp->cm_id = NULL;
2618 if (nesqp->flush_issued == 0) {
2619 nesqp->flush_issued = 1;
2620 issue_flush = 1;
2621 }
2622 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002623
Don Woodb29a4fc2009-09-05 20:36:39 -07002624 spin_unlock_irqrestore(&nesqp->lock, flags);
2625
2626 if ((issue_flush) && (nesqp->destroyed == 0)) {
2627 /* Flush the queue(s) */
2628 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2629 flush_q |= NES_CQP_FLUSH_SQ;
2630 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2631
2632 if (nesqp->term_flags) {
2633 ibevent.device = nesqp->ibqp.device;
2634 ibevent.event = nesqp->terminate_eventtype;
2635 ibevent.element.qp = &nesqp->ibqp;
2636 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
2637 }
2638 }
2639
2640 if ((cm_id) && (cm_id->event_handler)) {
2641 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002642 atomic_inc(&cm_disconnects);
2643 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002644 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002645 cm_event.local_addr = cm_id->local_addr;
2646 cm_event.remote_addr = cm_id->remote_addr;
2647 cm_event.private_data = NULL;
2648 cm_event.private_data_len = 0;
2649
Faisal Latif6492cdf2008-07-24 20:50:45 -07002650 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2651 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2652 "cm_id = %p, refcount = %u.\n",
2653 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2654 nesqp->hwqp.sq_tail, cm_id,
2655 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002656
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002657 ret = cm_id->event_handler(cm_id, &cm_event);
2658 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002659 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2660 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002661 }
2662
Don Woodb29a4fc2009-09-05 20:36:39 -07002663 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002664 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002665 nes_disconnect(nesqp, 1);
2666
2667 cm_id->provider_data = nesqp;
2668 /* Send up the close complete event */
2669 cm_event.event = IW_CM_EVENT_CLOSE;
2670 cm_event.status = IW_CM_EVENT_STATUS_OK;
2671 cm_event.provider_data = cm_id->provider_data;
2672 cm_event.local_addr = cm_id->local_addr;
2673 cm_event.remote_addr = cm_id->remote_addr;
2674 cm_event.private_data = NULL;
2675 cm_event.private_data_len = 0;
2676
2677 ret = cm_id->event_handler(cm_id, &cm_event);
2678 if (ret) {
2679 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2680 }
2681
2682 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002683 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002684 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002685
2686 return 0;
2687}
2688
2689
2690/**
2691 * nes_disconnect
2692 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002693static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002694{
2695 int ret = 0;
2696 struct nes_vnic *nesvnic;
2697 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002698 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002699
2700 nesvnic = to_nesvnic(nesqp->ibqp.device);
2701 if (!nesvnic)
2702 return -EINVAL;
2703
2704 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002705 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002706
2707 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2708 atomic_read(&nesvnic->netdev->refcnt));
2709
2710 if (nesqp->active_conn) {
2711
2712 /* indicate this connection is NOT active */
2713 nesqp->active_conn = 0;
2714 } else {
2715 /* Need to free the Last Streaming Mode Message */
2716 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002717 if (nesqp->lsmm_mr)
2718 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002719 pci_free_consistent(nesdev->pcidev,
2720 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2721 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2722 }
2723 }
2724
2725 /* close the CM node down if it is still active */
2726 if (nesqp->cm_node) {
2727 nes_debug(NES_DBG_CM, "Call close API\n");
2728
2729 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002730 }
2731
2732 return ret;
2733}
2734
2735
2736/**
2737 * nes_accept
2738 */
2739int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2740{
2741 u64 u64temp;
2742 struct ib_qp *ibqp;
2743 struct nes_qp *nesqp;
2744 struct nes_vnic *nesvnic;
2745 struct nes_device *nesdev;
2746 struct nes_cm_node *cm_node;
2747 struct nes_adapter *adapter;
2748 struct ib_qp_attr attr;
2749 struct iw_cm_event cm_event;
2750 struct nes_hw_qp_wqe *wqe;
2751 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002752 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002753 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002754 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002755 struct nes_ib_device *nesibdev;
2756 struct ib_mr *ibmr = NULL;
2757 struct ib_phys_buf ibphysbuf;
2758 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002759 u64 tagged_offset;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002760
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002761 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2762 if (!ibqp)
2763 return -EINVAL;
2764
2765 /* get all our handles */
2766 nesqp = to_nesqp(ibqp);
2767 nesvnic = to_nesvnic(nesqp->ibqp.device);
2768 nesdev = nesvnic->nesdev;
2769 adapter = nesdev->nesadapter;
2770
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002771 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002772 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2773 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2774 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002775
Faisal Latifc5a7d482009-12-09 15:54:08 -08002776 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
2777 if (cm_node->loopbackpartner)
2778 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
2779 rem_ref_cm_node(cm_node->cm_core, cm_node);
2780 return -EINVAL;
2781 }
2782
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002783 /* associate the node with the QP */
2784 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002785 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002786
Faisal Latif6492cdf2008-07-24 20:50:45 -07002787 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2788 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002789 atomic_inc(&cm_accepts);
2790
2791 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2792 atomic_read(&nesvnic->netdev->refcnt));
2793
Faisal Latif6492cdf2008-07-24 20:50:45 -07002794 /* allocate the ietf frame and space for private data */
2795 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2796 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2797 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002798
Faisal Latif6492cdf2008-07-24 20:50:45 -07002799 if (!nesqp->ietf_frame) {
2800 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2801 "data\n");
2802 return -ENOMEM;
2803 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002804
2805
Faisal Latif6492cdf2008-07-24 20:50:45 -07002806 /* setup the MPA frame */
2807 nesqp->private_data_len = conn_param->private_data_len;
2808 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002809
Faisal Latif6492cdf2008-07-24 20:50:45 -07002810 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2811 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002812
Faisal Latif6492cdf2008-07-24 20:50:45 -07002813 nesqp->ietf_frame->priv_data_len =
2814 cpu_to_be16(conn_param->private_data_len);
2815 nesqp->ietf_frame->rev = mpa_version;
2816 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002817
Faisal Latif6492cdf2008-07-24 20:50:45 -07002818 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2819 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002820
Faisal Latif6492cdf2008-07-24 20:50:45 -07002821 if (cm_id->remote_addr.sin_addr.s_addr !=
2822 cm_id->local_addr.sin_addr.s_addr) {
2823 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002824 nesibdev = nesvnic->nesibdev;
2825 nespd = nesqp->nespd;
2826 ibphysbuf.addr = nesqp->ietf_frame_pbase;
2827 ibphysbuf.size = conn_param->private_data_len +
2828 sizeof(struct ietf_mpa_frame);
Don Wood7a5efb62009-04-08 14:21:02 -07002829 tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002830 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
2831 &ibphysbuf, 1,
2832 IB_ACCESS_LOCAL_WRITE,
Don Wood7a5efb62009-04-08 14:21:02 -07002833 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002834 if (!ibmr) {
2835 nes_debug(NES_DBG_CM, "Unable to register memory region"
2836 "for lSMM for cm_node = %p \n",
2837 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07002838 pci_free_consistent(nesdev->pcidev,
2839 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2840 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002841 return -ENOMEM;
2842 }
2843
2844 ibmr->pd = &nespd->ibpd;
2845 ibmr->device = nespd->ibpd.device;
2846 nesqp->lsmm_mr = ibmr;
2847
Faisal Latif6492cdf2008-07-24 20:50:45 -07002848 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2849 set_wqe_64bit_value(wqe->wqe_words,
2850 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2851 u64temp);
2852 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2853 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2854 NES_IWARP_SQ_WQE_WRPDU);
2855 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2856 cpu_to_le32(conn_param->private_data_len +
2857 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002858 set_wqe_64bit_value(wqe->wqe_words,
2859 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
Don Wood7a5efb62009-04-08 14:21:02 -07002860 (u64)(unsigned long)nesqp->ietf_frame);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002861 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2862 cpu_to_le32(conn_param->private_data_len +
2863 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002864 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08002865 if (nesqp->sq_kmapped) {
2866 nesqp->sq_kmapped = 0;
2867 kunmap(nesqp->page);
2868 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002869
Faisal Latif6492cdf2008-07-24 20:50:45 -07002870 nesqp->nesqp_context->ird_ord_sizes |=
2871 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2872 NES_QPCONTEXT_ORDIRD_WRPDU);
2873 } else {
2874 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08002875 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002876 }
2877 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002878
2879
2880 /* Cache the cm_id in the qp */
2881 nesqp->cm_id = cm_id;
2882 cm_node->cm_id = cm_id;
2883
2884 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2885 cm_id->provider_data = nesqp;
2886 nesqp->active_conn = 0;
2887
Faisal Latif6492cdf2008-07-24 20:50:45 -07002888 if (cm_node->state == NES_CM_STATE_TSA)
2889 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2890 cm_node);
2891
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002892 nes_cm_init_tsa_conn(nesqp, cm_node);
2893
Faisal Latif6492cdf2008-07-24 20:50:45 -07002894 nesqp->nesqp_context->tcpPorts[0] =
2895 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2896 nesqp->nesqp_context->tcpPorts[1] =
2897 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2898
2899 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2900 nesqp->nesqp_context->ip0 =
2901 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2902 else
2903 nesqp->nesqp_context->ip0 =
2904 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002905
2906 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002907 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2908 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002909
Faisal Latif6492cdf2008-07-24 20:50:45 -07002910 nesqp->nesqp_context->arp_index_vlan |=
2911 cpu_to_le32(nes_arp_table(nesdev,
2912 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002913 NES_ARP_RESOLVE) << 16);
2914
2915 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002916 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002917
2918 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2919
2920 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002921 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2922 nesqp->nesqp_context->ird_ord_sizes |=
2923 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002924
2925 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002926 nes_quad.DstIpAdrIndex =
2927 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2928 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2929 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2930 else
2931 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2932 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2933 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002934
2935 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06002936 crc_value = get_crc_value(&nes_quad);
2937 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002938 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07002939 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002940
2941 nesqp->hte_index &= adapter->hte_index_mask;
2942 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2943
2944 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2945
Faisal Latif6492cdf2008-07-24 20:50:45 -07002946 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2947 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2948 "private data length=%zu.\n", nesqp->hwqp.qp_id,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002949 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2950 ntohs(cm_id->remote_addr.sin_port),
2951 ntohl(cm_id->local_addr.sin_addr.s_addr),
2952 ntohs(cm_id->local_addr.sin_port),
2953 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2954 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002955 conn_param->private_data_len +
2956 sizeof(struct ietf_mpa_frame));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002957
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002958
Coly Li73ac36e2009-01-07 18:09:16 -08002959 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002960 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07002961 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002962
2963 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2964 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2965 cm_event.provider_data = (void *)nesqp;
2966 cm_event.local_addr = cm_id->local_addr;
2967 cm_event.remote_addr = cm_id->remote_addr;
2968 cm_event.private_data = NULL;
2969 cm_event.private_data_len = 0;
2970 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002971 attr.qp_state = IB_QPS_RTS;
2972 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002973 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002974 cm_node->loopbackpartner->mpa_frame_size =
2975 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002976 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002977 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2978 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002979 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2980 }
2981 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002982 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2983 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002984
Faisal Latif183ecfa2008-11-21 20:50:46 -06002985 passive_state = atomic_add_return(1, &cm_node->passive_state);
2986 if (passive_state == NES_SEND_RESET_EVENT)
2987 create_event(cm_node, NES_CM_EVENT_RESET);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002988 return 0;
2989}
2990
2991
2992/**
2993 * nes_reject
2994 */
2995int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2996{
2997 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002998 struct nes_cm_node *loopback;
2999
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003000 struct nes_cm_core *cm_core;
3001
3002 atomic_inc(&cm_rejects);
3003 cm_node = (struct nes_cm_node *) cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003004 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003005 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003006 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003007 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
3008
Faisal Latif9d5ab132009-03-06 15:15:01 -08003009 if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
3010 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003011
Faisal Latif1cf078c2009-12-09 15:53:54 -08003012 memcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003013 if (loopback) {
3014 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3015 loopback->mpa_frame.priv_data_len = pdata_len;
3016 loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
3017 pdata_len;
3018 } else {
3019 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
3020 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
3021 }
3022
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003023 cm_node->mpa_frame.rev = mpa_version;
3024 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
3025
Faisal Latif9d5ab132009-03-06 15:15:01 -08003026 return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003027}
3028
3029
3030/**
3031 * nes_connect
3032 * setup and launch cm connect node
3033 */
3034int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3035{
3036 struct ib_qp *ibqp;
3037 struct nes_qp *nesqp;
3038 struct nes_vnic *nesvnic;
3039 struct nes_device *nesdev;
3040 struct nes_cm_node *cm_node;
3041 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003042 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003043
3044 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3045 if (!ibqp)
3046 return -EINVAL;
3047 nesqp = to_nesqp(ibqp);
3048 if (!nesqp)
3049 return -EINVAL;
3050 nesvnic = to_nesvnic(nesqp->ibqp.device);
3051 if (!nesvnic)
3052 return -EINVAL;
3053 nesdev = nesvnic->nesdev;
3054 if (!nesdev)
3055 return -EINVAL;
3056
Faisal Latifc5a7d482009-12-09 15:54:08 -08003057 if (!(cm_id->local_addr.sin_port) || !(cm_id->remote_addr.sin_port))
3058 return -EINVAL;
3059
Faisal Latif6492cdf2008-07-24 20:50:45 -07003060 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
3061 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3062 ntohl(nesvnic->local_ipaddr),
3063 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3064 ntohs(cm_id->remote_addr.sin_port),
3065 ntohl(cm_id->local_addr.sin_addr.s_addr),
3066 ntohs(cm_id->local_addr.sin_port));
3067
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003068 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003069 nesqp->active_conn = 1;
3070
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003071 /* cache the cm_id in the qp */
3072 nesqp->cm_id = cm_id;
3073
3074 cm_id->provider_data = nesqp;
3075
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003076 nesqp->private_data_len = conn_param->private_data_len;
3077 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
3078 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003079 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
3080 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003081
Faisal Latif6492cdf2008-07-24 20:50:45 -07003082 if (cm_id->local_addr.sin_addr.s_addr !=
Faisal Latif53094c32009-04-27 13:37:34 -07003083 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003084 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003085 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003086 apbvt_set = 1;
3087 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003088
3089 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003090 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3091 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3092 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3093 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003094 cm_info.cm_id = cm_id;
3095 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3096
3097 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003098
3099 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003100 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3101 conn_param->private_data_len, (void *)conn_param->private_data,
3102 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003103 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003104 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003105 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003106 PCI_FUNC(nesdev->pcidev->devfn),
3107 NES_MANAGE_APBVT_DEL);
3108
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003109 cm_id->rem_ref(cm_id);
3110 return -ENOMEM;
3111 }
3112
Faisal Latif53094c32009-04-27 13:37:34 -07003113 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003114 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003115 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003116 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003117
3118 return 0;
3119}
3120
3121
3122/**
3123 * nes_create_listen
3124 */
3125int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3126{
3127 struct nes_vnic *nesvnic;
3128 struct nes_cm_listener *cm_node;
3129 struct nes_cm_info cm_info;
3130 struct nes_adapter *adapter;
3131 int err;
3132
3133
3134 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3135 cm_id, ntohs(cm_id->local_addr.sin_port));
3136
3137 nesvnic = to_nesvnic(cm_id->device);
3138 if (!nesvnic)
3139 return -EINVAL;
3140 adapter = nesvnic->nesdev->nesadapter;
3141 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3142 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3143
3144 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3145 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3146
3147 /* setup listen params in our api call struct */
3148 cm_info.loc_addr = nesvnic->local_ipaddr;
3149 cm_info.loc_port = cm_id->local_addr.sin_port;
3150 cm_info.backlog = backlog;
3151 cm_info.cm_id = cm_id;
3152
3153 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3154
3155
3156 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3157 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003158 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003159 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003160 return -ENOMEM;
3161 }
3162
3163 cm_id->provider_data = cm_node;
3164
3165 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003166 err = nes_manage_apbvt(nesvnic,
3167 ntohs(cm_id->local_addr.sin_port),
3168 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3169 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003170 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003171 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3172 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003173 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3174 return err;
3175 }
Faisal Latif6e10d2e2010-02-12 19:55:03 +00003176 atomic_inc(&cm_listens_created);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003177 }
3178
3179 cm_id->add_ref(cm_id);
3180 cm_id->provider_data = (void *)cm_node;
3181
3182
3183 return 0;
3184}
3185
3186
3187/**
3188 * nes_destroy_listen
3189 */
3190int nes_destroy_listen(struct iw_cm_id *cm_id)
3191{
3192 if (cm_id->provider_data)
3193 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3194 else
3195 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3196
3197 cm_id->rem_ref(cm_id);
3198
3199 return 0;
3200}
3201
3202
3203/**
3204 * nes_cm_recv
3205 */
3206int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3207{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003208 int rc = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003209 cm_packets_received++;
3210 if ((g_cm_core) && (g_cm_core->api)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003211 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003212 } else {
3213 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3214 " cm is not setup properly.\n");
3215 }
3216
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003217 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003218}
3219
3220
3221/**
3222 * nes_cm_start
3223 * Start and init a cm core module
3224 */
3225int nes_cm_start(void)
3226{
3227 nes_debug(NES_DBG_CM, "\n");
3228 /* create the primary CM core, pass this handle to subsequent core inits */
3229 g_cm_core = nes_cm_alloc_core();
3230 if (g_cm_core) {
3231 return 0;
3232 } else {
3233 return -ENOMEM;
3234 }
3235}
3236
3237
3238/**
3239 * nes_cm_stop
3240 * stop and dealloc all cm core instances
3241 */
3242int nes_cm_stop(void)
3243{
3244 g_cm_core->api->destroy_cm_core(g_cm_core);
3245 return 0;
3246}
3247
3248
3249/**
3250 * cm_event_connected
3251 * handle a connected event, setup QPs and HW
3252 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003253static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003254{
3255 u64 u64temp;
3256 struct nes_qp *nesqp;
3257 struct nes_vnic *nesvnic;
3258 struct nes_device *nesdev;
3259 struct nes_cm_node *cm_node;
3260 struct nes_adapter *nesadapter;
3261 struct ib_qp_attr attr;
3262 struct iw_cm_id *cm_id;
3263 struct iw_cm_event cm_event;
3264 struct nes_hw_qp_wqe *wqe;
3265 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003266 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003267 int ret;
3268
3269 /* get all our handles */
3270 cm_node = event->cm_node;
3271 cm_id = cm_node->cm_id;
3272 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3273 nesqp = (struct nes_qp *)cm_id->provider_data;
3274 nesvnic = to_nesvnic(nesqp->ibqp.device);
3275 nesdev = nesvnic->nesdev;
3276 nesadapter = nesdev->nesadapter;
3277
3278 if (nesqp->destroyed) {
3279 return;
3280 }
3281 atomic_inc(&cm_connecteds);
3282 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3283 " local port 0x%04X. jiffies = %lu.\n",
3284 nesqp->hwqp.qp_id,
3285 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3286 ntohs(cm_id->remote_addr.sin_port),
3287 ntohs(cm_id->local_addr.sin_port),
3288 jiffies);
3289
3290 nes_cm_init_tsa_conn(nesqp, cm_node);
3291
3292 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003293 nesqp->nesqp_context->tcpPorts[0] =
3294 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3295 nesqp->nesqp_context->tcpPorts[1] =
3296 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3297 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3298 nesqp->nesqp_context->ip0 =
3299 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3300 else
3301 nesqp->nesqp_context->ip0 =
3302 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003303
3304 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003305 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3306 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003307 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003308 nes_arp_table(nesdev,
3309 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003310 NULL, NES_ARP_RESOLVE) << 16);
3311 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3312 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3313 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3314 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003315 cpu_to_le32((u32)1 <<
3316 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003317
3318 /* Adjust tail for not having a LSMM */
3319 nesqp->hwqp.sq_tail = 1;
3320
3321#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003322 if (cm_node->send_write0) {
3323 nes_debug(NES_DBG_CM, "Sending first write.\n");
3324 wqe = &nesqp->hwqp.sq_vbase[0];
3325 u64temp = (unsigned long)nesqp;
3326 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3327 set_wqe_64bit_value(wqe->wqe_words,
3328 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3329 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3330 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3331 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3332 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3333 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3334 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3335 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003336
Faisal Latifd2fa9b262009-12-09 15:54:28 -08003337 if (nesqp->sq_kmapped) {
3338 nesqp->sq_kmapped = 0;
3339 kunmap(nesqp->page);
3340 }
3341
Faisal Latif6492cdf2008-07-24 20:50:45 -07003342 /* use the reserved spot on the WQ for the extra first WQE */
3343 nesqp->nesqp_context->ird_ord_sizes &=
3344 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3345 NES_QPCONTEXT_ORDIRD_WRPDU |
3346 NES_QPCONTEXT_ORDIRD_ALSMM));
3347 nesqp->skip_lsmm = 1;
3348 nesqp->hwqp.sq_tail = 0;
3349 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3350 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3351 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003352#endif
3353
3354 memset(&nes_quad, 0, sizeof(nes_quad));
3355
Faisal Latif6492cdf2008-07-24 20:50:45 -07003356 nes_quad.DstIpAdrIndex =
3357 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3358 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3359 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3360 else
3361 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003362 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3363 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3364
3365 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003366 crc_value = get_crc_value(&nes_quad);
3367 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003368 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3369 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3370
3371 nesqp->hte_index &= nesadapter->hte_index_mask;
3372 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3373
3374 nesqp->ietf_frame = &cm_node->mpa_frame;
3375 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3376 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3377
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003378 /* notify OF layer we successfully created the requested connection */
3379 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3380 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3381 cm_event.provider_data = cm_id->provider_data;
3382 cm_event.local_addr.sin_family = AF_INET;
3383 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3384 cm_event.remote_addr = cm_id->remote_addr;
3385
Faisal Latif6492cdf2008-07-24 20:50:45 -07003386 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3387 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003388
3389 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3390 ret = cm_id->event_handler(cm_id, &cm_event);
3391 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3392
3393 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003394 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3395 "ret=%d\n", __func__, __LINE__, ret);
3396 attr.qp_state = IB_QPS_RTS;
3397 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003398
Faisal Latif6492cdf2008-07-24 20:50:45 -07003399 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3400 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003401
3402 return;
3403}
3404
3405
3406/**
3407 * cm_event_connect_error
3408 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003409static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003410{
3411 struct nes_qp *nesqp;
3412 struct iw_cm_id *cm_id;
3413 struct iw_cm_event cm_event;
3414 /* struct nes_cm_info cm_info; */
3415 int ret;
3416
3417 if (!event->cm_node)
3418 return;
3419
3420 cm_id = event->cm_node->cm_id;
3421 if (!cm_id) {
3422 return;
3423 }
3424
3425 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3426 nesqp = cm_id->provider_data;
3427
3428 if (!nesqp) {
3429 return;
3430 }
3431
3432 /* notify OF layer about this connection error event */
3433 /* cm_id->rem_ref(cm_id); */
3434 nesqp->cm_id = NULL;
3435 cm_id->provider_data = NULL;
3436 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003437 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003438 cm_event.provider_data = cm_id->provider_data;
3439 cm_event.local_addr = cm_id->local_addr;
3440 cm_event.remote_addr = cm_id->remote_addr;
3441 cm_event.private_data = NULL;
3442 cm_event.private_data_len = 0;
3443
Faisal Latif6492cdf2008-07-24 20:50:45 -07003444 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3445 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3446 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003447
3448 ret = cm_id->event_handler(cm_id, &cm_event);
3449 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3450 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003451 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3452 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003453 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003454
Faisal Latif6492cdf2008-07-24 20:50:45 -07003455 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003456 return;
3457}
3458
3459
3460/**
3461 * cm_event_reset
3462 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003463static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003464{
3465 struct nes_qp *nesqp;
3466 struct iw_cm_id *cm_id;
3467 struct iw_cm_event cm_event;
3468 /* struct nes_cm_info cm_info; */
3469 int ret;
3470
3471 if (!event->cm_node)
3472 return;
3473
3474 if (!event->cm_node->cm_id)
3475 return;
3476
3477 cm_id = event->cm_node->cm_id;
3478
3479 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3480 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003481 if (!nesqp)
3482 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003483
3484 nesqp->cm_id = NULL;
3485 /* cm_id->provider_data = NULL; */
3486 cm_event.event = IW_CM_EVENT_DISCONNECT;
3487 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3488 cm_event.provider_data = cm_id->provider_data;
3489 cm_event.local_addr = cm_id->local_addr;
3490 cm_event.remote_addr = cm_id->remote_addr;
3491 cm_event.private_data = NULL;
3492 cm_event.private_data_len = 0;
3493
Faisal Latif183ecfa2008-11-21 20:50:46 -06003494 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003495 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003496 atomic_inc(&cm_closes);
3497 cm_event.event = IW_CM_EVENT_CLOSE;
3498 cm_event.status = IW_CM_EVENT_STATUS_OK;
3499 cm_event.provider_data = cm_id->provider_data;
3500 cm_event.local_addr = cm_id->local_addr;
3501 cm_event.remote_addr = cm_id->remote_addr;
3502 cm_event.private_data = NULL;
3503 cm_event.private_data_len = 0;
3504 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3505 ret = cm_id->event_handler(cm_id, &cm_event);
3506
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003507 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3508
3509
3510 /* notify OF layer about this connection error event */
3511 cm_id->rem_ref(cm_id);
3512
3513 return;
3514}
3515
3516
3517/**
3518 * cm_event_mpa_req
3519 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003520static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003521{
3522 struct iw_cm_id *cm_id;
3523 struct iw_cm_event cm_event;
3524 int ret;
3525 struct nes_cm_node *cm_node;
3526
3527 cm_node = event->cm_node;
3528 if (!cm_node)
3529 return;
3530 cm_id = cm_node->cm_id;
3531
3532 atomic_inc(&cm_connect_reqs);
3533 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3534 cm_node, cm_id, jiffies);
3535
3536 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3537 cm_event.status = IW_CM_EVENT_STATUS_OK;
3538 cm_event.provider_data = (void *)cm_node;
3539
3540 cm_event.local_addr.sin_family = AF_INET;
3541 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3542 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3543
3544 cm_event.remote_addr.sin_family = AF_INET;
3545 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3546 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003547 cm_event.private_data = cm_node->mpa_frame_buf;
3548 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003549
3550 ret = cm_id->event_handler(cm_id, &cm_event);
3551 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003552 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3553 __func__, __LINE__, ret);
3554 return;
3555}
3556
3557
3558static void cm_event_mpa_reject(struct nes_cm_event *event)
3559{
3560 struct iw_cm_id *cm_id;
3561 struct iw_cm_event cm_event;
3562 struct nes_cm_node *cm_node;
3563 int ret;
3564
3565 cm_node = event->cm_node;
3566 if (!cm_node)
3567 return;
3568 cm_id = cm_node->cm_id;
3569
3570 atomic_inc(&cm_connect_reqs);
3571 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3572 cm_node, cm_id, jiffies);
3573
3574 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3575 cm_event.status = -ECONNREFUSED;
3576 cm_event.provider_data = cm_id->provider_data;
3577
3578 cm_event.local_addr.sin_family = AF_INET;
3579 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3580 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3581
3582 cm_event.remote_addr.sin_family = AF_INET;
3583 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3584 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3585
3586 cm_event.private_data = cm_node->mpa_frame_buf;
3587 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3588
3589 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3590 "remove_addr=%08x\n",
3591 cm_event.local_addr.sin_addr.s_addr,
3592 cm_event.remote_addr.sin_addr.s_addr);
3593
3594 ret = cm_id->event_handler(cm_id, &cm_event);
3595 if (ret)
3596 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003597 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003598
3599 return;
3600}
3601
3602
3603static void nes_cm_event_handler(struct work_struct *);
3604
3605/**
3606 * nes_cm_post_event
3607 * post an event to the cm event handler
3608 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003609static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003610{
3611 atomic_inc(&event->cm_node->cm_core->events_posted);
3612 add_ref_cm_node(event->cm_node);
3613 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3614 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003615 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3616 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003617
3618 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3619
3620 nes_debug(NES_DBG_CM, "Exit\n");
3621 return 0;
3622}
3623
3624
3625/**
3626 * nes_cm_event_handler
3627 * worker function to handle cm events
3628 * will free instance of nes_cm_event
3629 */
3630static void nes_cm_event_handler(struct work_struct *work)
3631{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003632 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3633 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003634 struct nes_cm_core *cm_core;
3635
Faisal Latif6492cdf2008-07-24 20:50:45 -07003636 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003637 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003638
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003639 cm_core = event->cm_node->cm_core;
3640 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003641 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003642
3643 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003644 case NES_CM_EVENT_MPA_REQ:
3645 cm_event_mpa_req(event);
3646 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3647 event->cm_node);
3648 break;
3649 case NES_CM_EVENT_RESET:
3650 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3651 event->cm_node);
3652 cm_event_reset(event);
3653 break;
3654 case NES_CM_EVENT_CONNECTED:
3655 if ((!event->cm_node->cm_id) ||
3656 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003657 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003658 cm_event_connected(event);
3659 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3660 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003661 case NES_CM_EVENT_MPA_REJECT:
3662 if ((!event->cm_node->cm_id) ||
3663 (event->cm_node->state == NES_CM_STATE_TSA))
3664 break;
3665 cm_event_mpa_reject(event);
3666 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3667 break;
3668
Faisal Latif6492cdf2008-07-24 20:50:45 -07003669 case NES_CM_EVENT_ABORTED:
3670 if ((!event->cm_node->cm_id) ||
3671 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003672 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003673 cm_event_connect_error(event);
3674 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3675 break;
3676 case NES_CM_EVENT_DROPPED_PKT:
3677 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3678 break;
3679 default:
3680 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3681 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003682 }
3683
3684 atomic_dec(&cm_core->events_posted);
3685 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3686 rem_ref_cm_node(cm_core, event->cm_node);
3687 kfree(event);
3688
3689 return;
3690}