blob: 20e21f1a18b96358591d52c46450bd5c8f87718f [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>
Bob Sharp7191a0a2008-10-03 12:21:19 -070055#include <net/arp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080056#include <net/neighbour.h>
57#include <net/route.h>
58#include <net/ip_fib.h>
Faisal Latifc11470f2009-04-27 13:38:31 -070059#include <net/tcp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080060
61#include "nes.h"
62
63u32 cm_packets_sent;
64u32 cm_packets_bounced;
65u32 cm_packets_dropped;
66u32 cm_packets_retrans;
67u32 cm_packets_created;
68u32 cm_packets_received;
69u32 cm_listens_created;
70u32 cm_listens_destroyed;
71u32 cm_backlog_drops;
72atomic_t cm_loopbacks;
73atomic_t cm_nodes_created;
74atomic_t cm_nodes_destroyed;
75atomic_t cm_accel_dropped_pkts;
76atomic_t cm_resets_recvd;
77
Faisal Latif6492cdf2008-07-24 20:50:45 -070078static inline int mini_cm_accelerated(struct nes_cm_core *,
79 struct nes_cm_node *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080080static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070081 struct nes_vnic *, struct nes_cm_info *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080082static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070083static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070084 struct nes_vnic *, u16, void *, struct nes_cm_info *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070085static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -070086static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
87 struct nes_cm_node *);
88static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
89 struct nes_cm_node *);
Faisal Latif4a14f6a2008-11-21 20:50:49 -060090static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070091 struct sk_buff *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070092static int mini_cm_dealloc_core(struct nes_cm_core *);
93static int mini_cm_get(struct nes_cm_core *);
94static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070095
Chien Tung6098d102008-11-21 20:51:01 -060096static void form_cm_frame(struct sk_buff *, struct nes_cm_node *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070097 void *, u32, void *, u32, u8);
Faisal Latif6492cdf2008-07-24 20:50:45 -070098static int add_ref_cm_node(struct nes_cm_node *);
99static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
100
Roland Dreier1a855fb2008-04-16 21:01:09 -0700101static int nes_cm_disconn_true(struct nes_qp *);
102static int nes_cm_post_event(struct nes_cm_event *event);
103static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
104static void nes_disconnect_worker(struct work_struct *work);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700105
106static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800107static int send_mpa_reject(struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700108static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
109static int send_reset(struct nes_cm_node *, struct sk_buff *);
110static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fb2008-04-16 21:01:09 -0700111static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700112static void process_packet(struct nes_cm_node *, struct sk_buff *,
113 struct nes_cm_core *);
114
115static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
116static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
117static void cleanup_retrans_entry(struct nes_cm_node *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800118static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700119static void free_retrans_entry(struct nes_cm_node *cm_node);
120static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
121 struct sk_buff *skb, int optionsize, int passive);
122
123/* CM event handler functions */
124static void cm_event_connected(struct nes_cm_event *);
125static void cm_event_connect_error(struct nes_cm_event *);
126static void cm_event_reset(struct nes_cm_event *);
127static void cm_event_mpa_req(struct nes_cm_event *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800128static void cm_event_mpa_reject(struct nes_cm_event *);
129static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700130
131static void print_core(struct nes_cm_core *core);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800132
133/* External CM API Interface */
134/* instance of function pointers for client API */
135/* set address of this instance to cm_core->cm_ops at cm_core alloc */
136static struct nes_cm_ops nes_cm_api = {
137 mini_cm_accelerated,
138 mini_cm_listen,
139 mini_cm_del_listen,
140 mini_cm_connect,
141 mini_cm_close,
142 mini_cm_accept,
143 mini_cm_reject,
144 mini_cm_recv_pkt,
145 mini_cm_dealloc_core,
146 mini_cm_get,
147 mini_cm_set
148};
149
Roland Dreier1a855fb2008-04-16 21:01:09 -0700150static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800151
152atomic_t cm_connects;
153atomic_t cm_accepts;
154atomic_t cm_disconnects;
155atomic_t cm_closes;
156atomic_t cm_connecteds;
157atomic_t cm_connect_reqs;
158atomic_t cm_rejects;
159
160
161/**
162 * create_event
163 */
164static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
165 enum nes_cm_event_type type)
166{
167 struct nes_cm_event *event;
168
169 if (!cm_node->cm_id)
170 return NULL;
171
172 /* allocate an empty event */
173 event = kzalloc(sizeof(*event), GFP_ATOMIC);
174
175 if (!event)
176 return NULL;
177
178 event->type = type;
179 event->cm_node = cm_node;
180 event->cm_info.rem_addr = cm_node->rem_addr;
181 event->cm_info.loc_addr = cm_node->loc_addr;
182 event->cm_info.rem_port = cm_node->rem_port;
183 event->cm_info.loc_port = cm_node->loc_port;
184 event->cm_info.cm_id = cm_node->cm_id;
185
Faisal Latif6492cdf2008-07-24 20:50:45 -0700186 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
187 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
188 cm_node, event, type, event->cm_info.loc_addr,
189 event->cm_info.loc_port, event->cm_info.rem_addr,
190 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800191
192 nes_cm_post_event(event);
193 return event;
194}
195
196
197/**
198 * send_mpa_request
199 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700200static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800201{
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800202 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700203 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800204 return -1;
205 }
206
207 /* send an MPA Request frame */
208 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
209 cm_node->mpa_frame_size, SET_ACK);
210
Faisal Latif9d5ab132009-03-06 15:15:01 -0800211 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
212}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800213
Faisal Latif9d5ab132009-03-06 15:15:01 -0800214
215
216static int send_mpa_reject(struct nes_cm_node *cm_node)
217{
218 struct sk_buff *skb = NULL;
219
220 skb = dev_alloc_skb(MAX_CM_BUFFER);
221 if (!skb) {
222 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
223 return -ENOMEM;
224 }
225
226 /* send an MPA reject frame */
227 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
228 cm_node->mpa_frame_size, SET_ACK | SET_FIN);
229
230 cm_node->state = NES_CM_STATE_FIN_WAIT1;
231 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800232}
233
234
235/**
236 * recv_mpa - process a received TCP pkt, we are expecting an
237 * IETF MPA frame
238 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800239static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
240 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800241{
242 struct ietf_mpa_frame *mpa_frame;
243
Faisal Latif9d5ab132009-03-06 15:15:01 -0800244 *type = NES_MPA_REQUEST_ACCEPT;
245
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800246 /* assume req frame is in tcp data payload */
247 if (len < sizeof(struct ietf_mpa_frame)) {
248 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800249 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800250 }
251
252 mpa_frame = (struct ietf_mpa_frame *)buffer;
253 cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
Faisal Latif1cf078c2009-12-09 15:53:54 -0800254 /* make sure mpa private data len is less than 512 bytes */
255 if (cm_node->mpa_frame_size > IETF_MAX_PRIV_DATA_LEN) {
256 nes_debug(NES_DBG_CM, "The received Length of Private"
257 " Data field exceeds 512 octets\n");
258 return -EINVAL;
259 }
260 /*
261 * make sure MPA receiver interoperate with the
262 * received MPA version and MPA key information
263 *
264 */
265 if (mpa_frame->rev != mpa_version) {
266 nes_debug(NES_DBG_CM, "The received mpa version"
267 " can not be interoperated\n");
268 return -EINVAL;
269 }
270 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
271 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
272 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
273 return -EINVAL;
274 }
275 } else {
276 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
277 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
278 return -EINVAL;
279 }
280 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800281
282 if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
283 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
284 " complete (%x + %x != %x)\n",
Faisal Latif9d5ab132009-03-06 15:15:01 -0800285 cm_node->mpa_frame_size,
286 (u32)sizeof(struct ietf_mpa_frame), len);
287 return -EINVAL;
288 }
289 /* make sure it does not exceed the max size */
290 if (len > MAX_CM_BUFFER) {
291 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
292 " (%x + %x != %x)\n",
293 cm_node->mpa_frame_size,
294 (u32)sizeof(struct ietf_mpa_frame), len);
295 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800296 }
297
298 /* copy entire MPA frame to our cm_node's frame */
299 memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
300 cm_node->mpa_frame_size);
301
Faisal Latif9d5ab132009-03-06 15:15:01 -0800302 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
303 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800304 return 0;
305}
306
307
308/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800309 * form_cm_frame - get a free packet and build empty frame Use
310 * node info to build.
311 */
Chien Tung6098d102008-11-21 20:51:01 -0600312static void form_cm_frame(struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700313 struct nes_cm_node *cm_node, void *options, u32 optionsize,
314 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800315{
316 struct tcphdr *tcph;
317 struct iphdr *iph;
318 struct ethhdr *ethh;
319 u8 *buf;
320 u16 packetsize = sizeof(*iph);
321
322 packetsize += sizeof(*tcph);
323 packetsize += optionsize + datasize;
324
325 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
326
327 skb->len = 0;
328 buf = skb_put(skb, packetsize + ETH_HLEN);
329
330 ethh = (struct ethhdr *) buf;
331 buf += ETH_HLEN;
332
333 iph = (struct iphdr *)buf;
334 buf += sizeof(*iph);
335 tcph = (struct tcphdr *)buf;
336 skb_reset_mac_header(skb);
337 skb_set_network_header(skb, ETH_HLEN);
338 skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
339 buf += sizeof(*tcph);
340
341 skb->ip_summed = CHECKSUM_PARTIAL;
342 skb->protocol = htons(0x800);
343 skb->data_len = 0;
344 skb->mac_len = ETH_HLEN;
345
346 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
347 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
348 ethh->h_proto = htons(0x0800);
349
350 iph->version = IPVERSION;
351 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
352 iph->tos = 0;
353 iph->tot_len = htons(packetsize);
354 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
355
356 iph->frag_off = htons(0x4000);
357 iph->ttl = 0x40;
358 iph->protocol = 0x06; /* IPPROTO_TCP */
359
360 iph->saddr = htonl(cm_node->loc_addr);
361 iph->daddr = htonl(cm_node->rem_addr);
362
363 tcph->source = htons(cm_node->loc_port);
364 tcph->dest = htons(cm_node->rem_port);
365 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
366
367 if (flags & SET_ACK) {
368 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
369 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
370 tcph->ack = 1;
371 } else
372 tcph->ack_seq = 0;
373
374 if (flags & SET_SYN) {
375 cm_node->tcp_cntxt.loc_seq_num++;
376 tcph->syn = 1;
377 } else
Faisal Latif6492cdf2008-07-24 20:50:45 -0700378 cm_node->tcp_cntxt.loc_seq_num += datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800379
Faisal Latif6492cdf2008-07-24 20:50:45 -0700380 if (flags & SET_FIN) {
381 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800382 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700383 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800384
385 if (flags & SET_RST)
386 tcph->rst = 1;
387
388 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
389 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
390 tcph->urg_ptr = 0;
391 if (optionsize)
392 memcpy(buf, options, optionsize);
393 buf += optionsize;
394 if (datasize)
395 memcpy(buf, data, datasize);
396
397 skb_shinfo(skb)->nr_frags = 0;
398 cm_packets_created++;
399
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800400}
401
402
403/**
404 * print_core - dump a cm core
405 */
406static void print_core(struct nes_cm_core *core)
407{
408 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
409 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
410 if (!core)
411 return;
412 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800413
414 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
415
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800416 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
417 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
418
419 nes_debug(NES_DBG_CM, "core : %p \n", core);
420
421 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
422}
423
424
425/**
426 * schedule_nes_timer
427 * note - cm_node needs to be protected before calling this. Encase in:
428 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
429 */
430int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
431 enum nes_timer_type type, int send_retrans,
432 int close_when_complete)
433{
434 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700435 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800436 struct nes_timer_entry *new_send;
437 int ret = 0;
438 u32 was_timer_set;
439
440 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
441 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800442 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800443
444 /* new_send->timetosend = currenttime */
445 new_send->retrycount = NES_DEFAULT_RETRYS;
446 new_send->retranscount = NES_DEFAULT_RETRANS;
447 new_send->skb = skb;
448 new_send->timetosend = jiffies;
449 new_send->type = type;
450 new_send->netdev = cm_node->netdev;
451 new_send->send_retrans = send_retrans;
452 new_send->close_when_complete = close_when_complete;
453
454 if (type == NES_TIMER_TYPE_CLOSE) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700455 new_send->timetosend += (HZ/10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800456 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700457 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800458 WARN_ON(1);
459 return -EINVAL;
460 }
461 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800462 }
463
464 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700465 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800466 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700467 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
468 cm_node->send_entry = new_send;
469 add_ref_cm_node(cm_node);
470 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
471 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800472
473 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
474 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700475 nes_debug(NES_DBG_CM, "Error sending packet %p "
476 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800477 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700478 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800479 } else {
480 cm_packets_sent++;
481 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700482 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800483 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700484 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800485 return ret;
486 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800487 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800488 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800489
490 was_timer_set = timer_pending(&cm_core->tcp_timer);
491
492 if (!was_timer_set) {
493 cm_core->tcp_timer.expires = new_send->timetosend;
494 add_timer(&cm_core->tcp_timer);
495 }
496
497 return ret;
498}
499
Faisal Latif9d5ab132009-03-06 15:15:01 -0800500static void nes_retrans_expired(struct nes_cm_node *cm_node)
501{
Faisal Latif68237a02009-06-22 22:53:28 -0700502 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800503 switch (cm_node->state) {
504 case NES_CM_STATE_SYN_RCVD:
505 case NES_CM_STATE_CLOSING:
506 rem_ref_cm_node(cm_node->cm_core, cm_node);
507 break;
508 case NES_CM_STATE_LAST_ACK:
509 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700510 if (cm_node->cm_id)
511 cm_id->rem_ref(cm_id);
512 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800513 send_reset(cm_node, NULL);
514 break;
515 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800516 add_ref_cm_node(cm_node);
517 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800518 create_event(cm_node, NES_CM_EVENT_ABORTED);
519 }
520}
521
522static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
523{
524 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
525 struct iw_cm_id *cm_id = cm_node->cm_id;
526 struct nes_qp *nesqp;
527 unsigned long qplockflags;
528
529 if (!recv_entry)
530 return;
531 nesqp = (struct nes_qp *)recv_entry->skb;
532 if (nesqp) {
533 spin_lock_irqsave(&nesqp->lock, qplockflags);
534 if (nesqp->cm_id) {
535 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
536 "refcount = %d: HIT A "
537 "NES_TIMER_TYPE_CLOSE with something "
538 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
539 atomic_read(&nesqp->refcount));
540 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
541 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
542 nesqp->ibqp_state = IB_QPS_ERR;
543 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
544 nes_cm_disconn(nesqp);
545 } else {
546 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
547 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
548 "refcount = %d: HIT A "
549 "NES_TIMER_TYPE_CLOSE with nothing "
550 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
551 atomic_read(&nesqp->refcount));
552 }
553 } else if (rem_node) {
554 /* TIME_WAIT state */
555 rem_ref_cm_node(cm_node->cm_core, cm_node);
556 }
557 if (cm_node->cm_id)
558 cm_id->rem_ref(cm_id);
559 kfree(recv_entry);
560 cm_node->recv_entry = NULL;
561}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800562
563/**
564 * nes_cm_timer_tick
565 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700566static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800567{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800568 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800569 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800570 struct nes_cm_node *cm_node;
571 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800572 struct list_head *list_core_temp;
573 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800574 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800575 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700576 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800577 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800578
Faisal Latif879e5bd2008-11-21 20:50:41 -0600579 struct list_head timer_list;
580 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800581 spin_lock_irqsave(&cm_core->ht_lock, flags);
582
Faisal Latif6492cdf2008-07-24 20:50:45 -0700583 list_for_each_safe(list_node, list_core_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600584 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800585 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800586 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600587 add_ref_cm_node(cm_node);
588 list_add(&cm_node->timer_entry, &timer_list);
589 }
590 }
591 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
592
593 list_for_each_safe(list_node, list_core_temp, &timer_list) {
594 cm_node = container_of(list_node, struct nes_cm_node,
595 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800596 recv_entry = cm_node->recv_entry;
597
598 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700599 if (time_after(recv_entry->timetosend, jiffies)) {
600 if (nexttimeout > recv_entry->timetosend ||
Faisal Latif9d5ab132009-03-06 15:15:01 -0800601 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800602 nexttimeout = recv_entry->timetosend;
603 settimer = 1;
604 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800605 } else
606 handle_recv_entry(cm_node, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800607 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800608
609 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700610 do {
611 send_entry = cm_node->send_entry;
612 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600613 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800614 if (time_after(send_entry->timetosend, jiffies)) {
615 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700616 if ((nexttimeout >
617 send_entry->timetosend) ||
618 !settimer) {
619 nexttimeout =
620 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800621 settimer = 1;
622 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800623 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700624 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800625 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800626 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800627 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700628
629 if ((cm_node->state == NES_CM_STATE_TSA) ||
630 (cm_node->state == NES_CM_STATE_CLOSED)) {
631 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600632 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800633 }
634
Faisal Latif6492cdf2008-07-24 20:50:45 -0700635 if (!send_entry->retranscount ||
636 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800637 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700638 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800639
Faisal Latif6492cdf2008-07-24 20:50:45 -0700640 spin_unlock_irqrestore(
641 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800642 nes_retrans_expired(cm_node);
643 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700644 spin_lock_irqsave(&cm_node->retrans_list_lock,
645 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600646 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800647 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800648 atomic_inc(&send_entry->skb->users);
649 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700650 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
651 "for node %p, jiffies = %lu, time to send = "
652 "%lu, retranscount = %u, send_entry->seq_num = "
653 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
654 "0x%08X\n", send_entry, cm_node, jiffies,
655 send_entry->timetosend,
656 send_entry->retranscount,
657 send_entry->seq_num,
658 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800659
Faisal Latif6492cdf2008-07-24 20:50:45 -0700660 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
661 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800662 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700663 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800664 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700665 nes_debug(NES_DBG_CM, "rexmit failed for "
666 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800667 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800668 send_entry->retrycount--;
669 nexttimeout = jiffies + NES_SHORT_TIME;
670 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600671 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800672 } else {
673 cm_packets_sent++;
674 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700675 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
676 "%u, retry count = %u.\n",
677 send_entry->retranscount,
678 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800679 if (send_entry->send_retrans) {
680 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700681 timetosend = (NES_RETRY_TIMEOUT <<
682 (NES_DEFAULT_RETRANS - send_entry->retranscount));
683
Faisal Latif6492cdf2008-07-24 20:50:45 -0700684 send_entry->timetosend = jiffies +
Faisal Latif4e9c3902009-04-27 13:39:36 -0700685 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700686 if (nexttimeout > send_entry->timetosend ||
687 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800688 nexttimeout = send_entry->timetosend;
689 settimer = 1;
690 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800691 } else {
692 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700693 close_when_complete =
694 send_entry->close_when_complete;
695 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
696 cm_node, cm_node->state);
697 free_retrans_entry(cm_node);
698 if (close_when_complete)
699 rem_ref_cm_node(cm_node->cm_core,
700 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800701 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700702 } while (0);
703
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800704 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700705 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800706 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800707
708 if (settimer) {
709 if (!timer_pending(&cm_core->tcp_timer)) {
710 cm_core->tcp_timer.expires = nexttimeout;
711 add_timer(&cm_core->tcp_timer);
712 }
713 }
714}
715
716
717/**
718 * send_syn
719 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700720static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
721 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800722{
723 int ret;
724 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800725 char optionsbuffer[sizeof(struct option_mss) +
Faisal Latif6492cdf2008-07-24 20:50:45 -0700726 sizeof(struct option_windowscale) + sizeof(struct option_base) +
727 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800728
729 int optionssize = 0;
730 /* Sending MSS option */
731 union all_known_options *options;
732
733 if (!cm_node)
734 return -EINVAL;
735
736 options = (union all_known_options *)&optionsbuffer[optionssize];
737 options->as_mss.optionnum = OPTION_NUMBER_MSS;
738 options->as_mss.length = sizeof(struct option_mss);
739 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
740 optionssize += sizeof(struct option_mss);
741
742 options = (union all_known_options *)&optionsbuffer[optionssize];
743 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
744 options->as_windowscale.length = sizeof(struct option_windowscale);
745 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
746 optionssize += sizeof(struct option_windowscale);
747
Faisal Latif6492cdf2008-07-24 20:50:45 -0700748 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800749 options = (union all_known_options *)&optionsbuffer[optionssize];
750 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
751 options->as_base.length = sizeof(struct option_base);
752 optionssize += sizeof(struct option_base);
753 /* we need the size to be a multiple of 4 */
754 options = (union all_known_options *)&optionsbuffer[optionssize];
755 options->as_end = 1;
756 optionssize += 1;
757 options = (union all_known_options *)&optionsbuffer[optionssize];
758 options->as_end = 1;
759 optionssize += 1;
760 }
761
762 options = (union all_known_options *)&optionsbuffer[optionssize];
763 options->as_end = OPTION_NUMBER_END;
764 optionssize += 1;
765
Faisal Latif6492cdf2008-07-24 20:50:45 -0700766 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800767 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800768 if (!skb) {
769 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
770 return -1;
771 }
772
773 if (sendack)
774 flags |= SET_ACK;
775
776 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
777 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
778
779 return ret;
780}
781
782
783/**
784 * send_reset
785 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700786static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800787{
788 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800789 int flags = SET_RST | SET_ACK;
790
Faisal Latif6492cdf2008-07-24 20:50:45 -0700791 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800792 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800793 if (!skb) {
794 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -0800795 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800796 }
797
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800798 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
799 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
800
801 return ret;
802}
803
804
805/**
806 * send_ack
807 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700808static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800809{
810 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700811
812 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800813 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800814
815 if (!skb) {
816 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
817 return -1;
818 }
819
820 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
821 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
822
823 return ret;
824}
825
826
827/**
828 * send_fin
829 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700830static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800831{
832 int ret;
833
834 /* if we didn't get a frame get one */
835 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800836 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800837
838 if (!skb) {
839 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
840 return -1;
841 }
842
843 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
844 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
845
846 return ret;
847}
848
849
850/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800851 * find_node - find a cm node that matches the reference cm node
852 */
853static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
854 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
855{
856 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800857 struct list_head *hte;
858 struct nes_cm_node *cm_node;
859
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800860 /* get a handle on the hte */
861 hte = &cm_core->connected_nodes;
862
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800863 /* walk list and find cm_node associated with this session ID */
864 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700865 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800866 /* compare quad, return node handle if a match */
867 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
868 cm_node->loc_addr, cm_node->loc_port,
869 loc_addr, loc_port,
870 cm_node->rem_addr, cm_node->rem_port,
871 rem_addr, rem_port);
872 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
873 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
874 add_ref_cm_node(cm_node);
875 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
876 return cm_node;
877 }
878 }
879 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
880
881 /* no owner node */
882 return NULL;
883}
884
885
886/**
887 * find_listener - find a cm node listening on this addr-port pair
888 */
889static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
890 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
891{
892 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800893 struct nes_cm_listener *listen_node;
894
895 /* walk list and find cm_node associated with this session ID */
896 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700897 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800898 /* compare node pair, return node handle if a match */
899 if (((listen_node->loc_addr == dst_addr) ||
900 listen_node->loc_addr == 0x00000000) &&
901 (listen_node->loc_port == dst_port) &&
902 (listener_state & listen_node->listener_state)) {
903 atomic_inc(&listen_node->ref_count);
904 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
905 return listen_node;
906 }
907 }
908 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
909
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800910 /* no listener */
911 return NULL;
912}
913
914
915/**
916 * add_hte_node - add a cm node to the hash table
917 */
918static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
919{
920 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800921 struct list_head *hte;
922
923 if (!cm_node || !cm_core)
924 return -EINVAL;
925
Faisal Latif6492cdf2008-07-24 20:50:45 -0700926 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
927 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800928
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800929 spin_lock_irqsave(&cm_core->ht_lock, flags);
930
931 /* get a handle on the hash table element (list head for this slot) */
932 hte = &cm_core->connected_nodes;
933 list_add_tail(&cm_node->list, hte);
934 atomic_inc(&cm_core->ht_node_cnt);
935
936 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
937
938 return 0;
939}
940
941
942/**
943 * mini_cm_dec_refcnt_listen
944 */
945static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700946 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800947{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800948 int ret = -EINVAL;
949 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800950 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700951 struct list_head *list_pos = NULL;
952 struct list_head *list_temp = NULL;
953 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -0600954 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700955
956 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
957 "refcnt=%d\n", listener, free_hanging_nodes,
958 atomic_read(&listener->ref_count));
959 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -0600960 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700961 if (free_hanging_nodes) {
962 spin_lock_irqsave(&cm_core->ht_lock, flags);
963 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600964 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700965 cm_node = container_of(list_pos, struct nes_cm_node,
966 list);
967 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -0600968 (!cm_node->accelerated)) {
969 add_ref_cm_node(cm_node);
970 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700971 }
972 }
973 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
974 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600975
976 list_for_each_safe(list_pos, list_temp, &reset_list) {
977 cm_node = container_of(list_pos, struct nes_cm_node,
Faisal Latif9d5ab132009-03-06 15:15:01 -0800978 reset_entry);
979 {
980 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -0800981 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800982 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
983 rem_ref_cm_node(cm_node->cm_core, cm_node);
984 } else {
985 if (!loopback) {
986 cleanup_retrans_entry(cm_node);
987 err = send_reset(cm_node, NULL);
988 if (err) {
989 cm_node->state =
990 NES_CM_STATE_CLOSED;
991 WARN_ON(1);
992 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -0800993 old_state = cm_node->state;
994 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
995 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
996 rem_ref_cm_node(
997 cm_node->cm_core,
998 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800999 }
1000 } else {
1001 struct nes_cm_event event;
1002
1003 event.cm_node = loopback;
1004 event.cm_info.rem_addr =
1005 loopback->rem_addr;
1006 event.cm_info.loc_addr =
1007 loopback->loc_addr;
1008 event.cm_info.rem_port =
1009 loopback->rem_port;
1010 event.cm_info.loc_port =
1011 loopback->loc_port;
1012 event.cm_info.cm_id = loopback->cm_id;
1013 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001014 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001015 loopback->state = NES_CM_STATE_CLOSED;
1016
Faisal Latif9d5ab132009-03-06 15:15:01 -08001017 rem_ref_cm_node(cm_node->cm_core,
1018 cm_node);
1019
1020 }
1021 }
1022 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001023 }
1024
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001025 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1026 if (!atomic_dec_return(&listener->ref_count)) {
1027 list_del(&listener->list);
1028
1029 /* decrement our listen node count */
1030 atomic_dec(&cm_core->listen_node_cnt);
1031
1032 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1033
1034 if (listener->nesvnic) {
1035 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1036 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1037 }
1038
1039 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1040
1041 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001042 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001043 ret = 0;
1044 cm_listens_destroyed++;
1045 } else {
1046 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1047 }
1048 if (listener) {
1049 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1050 nes_debug(NES_DBG_CM, "destroying listener (%p)"
1051 " with non-zero pending accepts=%u\n",
1052 listener, atomic_read(&listener->pend_accepts_cnt));
1053 }
1054
1055 return ret;
1056}
1057
1058
1059/**
1060 * mini_cm_del_listen
1061 */
1062static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1063 struct nes_cm_listener *listener)
1064{
1065 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1066 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1067 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1068}
1069
1070
1071/**
1072 * mini_cm_accelerated
1073 */
1074static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1075 struct nes_cm_node *cm_node)
1076{
1077 u32 was_timer_set;
1078 cm_node->accelerated = 1;
1079
1080 if (cm_node->accept_pend) {
1081 BUG_ON(!cm_node->listener);
1082 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001083 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001084 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1085 }
1086
1087 was_timer_set = timer_pending(&cm_core->tcp_timer);
1088 if (!was_timer_set) {
1089 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1090 add_timer(&cm_core->tcp_timer);
1091 }
1092
1093 return 0;
1094}
1095
1096
1097/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001098 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001099 */
Bob Sharp7191a0a2008-10-03 12:21:19 -07001100static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001101{
1102 struct rtable *rt;
1103 struct flowi fl;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001104 struct neighbour *neigh;
1105 int rc = -1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001106
1107 memset(&fl, 0, sizeof fl);
1108 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1109 if (ip_route_output_key(&init_net, &rt, &fl)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001110 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001111 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001112 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001113 }
1114
Bob Sharp7191a0a2008-10-03 12:21:19 -07001115 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1116 if (neigh) {
1117 if (neigh->nud_state & NUD_VALID) {
1118 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001119 " is %pM, Gateway is 0x%08X \n", dst_ip,
1120 neigh->ha, ntohl(rt->rt_gateway));
Bob Sharp7191a0a2008-10-03 12:21:19 -07001121 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1122 dst_ip, NES_ARP_ADD);
1123 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1124 NES_ARP_RESOLVE);
1125 }
1126 neigh_release(neigh);
1127 }
1128
1129 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1130 neigh_event_send(rt->u.dst.neighbour, NULL);
1131
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001132 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001133 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001134}
1135
1136
1137/**
1138 * make_cm_node - create a new instance of a cm node
1139 */
1140static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1141 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1142 struct nes_cm_listener *listener)
1143{
1144 struct nes_cm_node *cm_node;
1145 struct timespec ts;
1146 int arpindex = 0;
1147 struct nes_device *nesdev;
1148 struct nes_adapter *nesadapter;
1149
1150 /* create an hte and cm_node for this instance */
1151 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1152 if (!cm_node)
1153 return NULL;
1154
1155 /* set our node specific transport info */
1156 cm_node->loc_addr = cm_info->loc_addr;
1157 cm_node->rem_addr = cm_info->rem_addr;
1158 cm_node->loc_port = cm_info->loc_port;
1159 cm_node->rem_port = cm_info->rem_port;
1160 cm_node->send_write0 = send_first;
Harvey Harrison63779432008-10-31 00:56:00 -07001161 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1162 &cm_node->loc_addr, cm_node->loc_port,
1163 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001164 cm_node->listener = listener;
1165 cm_node->netdev = nesvnic->netdev;
1166 cm_node->cm_id = cm_info->cm_id;
1167 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1168
Faisal Latif6492cdf2008-07-24 20:50:45 -07001169 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1170 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001171
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001172 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001173
1174 cm_node->loopbackpartner = NULL;
1175 atomic_set(&cm_node->ref_count, 1);
1176 /* associate our parent CM core */
1177 cm_node->cm_core = cm_core;
1178 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1179 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1180 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1181 NES_CM_DEFAULT_RCV_WND_SCALE;
1182 ts = current_kernel_time();
1183 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1184 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Chien Tungf2b2b592008-03-20 19:55:30 -05001185 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001186 cm_node->tcp_cntxt.rcv_nxt = 0;
1187 /* get a unique session ID , add thread_id to an upcounter to handle race */
1188 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001189 cm_node->conn_type = cm_info->conn_type;
1190 cm_node->apbvt_set = 0;
1191 cm_node->accept_pend = 0;
1192
1193 cm_node->nesvnic = nesvnic;
1194 /* get some device handles, for arp lookup */
1195 nesdev = nesvnic->nesdev;
1196 nesadapter = nesdev->nesadapter;
1197
1198 cm_node->loopbackpartner = NULL;
1199 /* get the mac addr for the remote node */
Chien Tung1ee86552008-11-21 20:51:04 -06001200 if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1201 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
1202 else
1203 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001204 if (arpindex < 0) {
Bob Sharp7191a0a2008-10-03 12:21:19 -07001205 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr);
1206 if (arpindex < 0) {
1207 kfree(cm_node);
1208 return NULL;
1209 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001210 }
1211
1212 /* copy the mac addr to node context */
1213 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001214 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1215 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001216
1217 add_hte_node(cm_core, cm_node);
1218 atomic_inc(&cm_nodes_created);
1219
1220 return cm_node;
1221}
1222
1223
1224/**
1225 * add_ref_cm_node - destroy an instance of a cm node
1226 */
1227static int add_ref_cm_node(struct nes_cm_node *cm_node)
1228{
1229 atomic_inc(&cm_node->ref_count);
1230 return 0;
1231}
1232
1233
1234/**
1235 * rem_ref_cm_node - destroy an instance of a cm node
1236 */
1237static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001238 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001239{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001240 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001241 struct nes_qp *nesqp;
1242
1243 if (!cm_node)
1244 return -EINVAL;
1245
1246 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1247 if (atomic_dec_return(&cm_node->ref_count)) {
1248 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1249 return 0;
1250 }
1251 list_del(&cm_node->list);
1252 atomic_dec(&cm_core->ht_node_cnt);
1253 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1254
1255 /* if the node is destroyed before connection was accelerated */
1256 if (!cm_node->accelerated && cm_node->accept_pend) {
1257 BUG_ON(!cm_node->listener);
1258 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1259 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1260 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001261 WARN_ON(cm_node->send_entry);
1262 if (cm_node->recv_entry)
1263 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001264 if (cm_node->listener) {
1265 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1266 } else {
1267 if (cm_node->apbvt_set && cm_node->nesvnic) {
1268 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001269 PCI_FUNC(
1270 cm_node->nesvnic->nesdev->pcidev->devfn),
1271 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001272 }
1273 }
1274
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001275 atomic_dec(&cm_core->node_cnt);
1276 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001277 nesqp = cm_node->nesqp;
1278 if (nesqp) {
1279 nesqp->cm_node = NULL;
1280 nes_rem_ref(&nesqp->ibqp);
1281 cm_node->nesqp = NULL;
1282 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001283
Faisal Latif6492cdf2008-07-24 20:50:45 -07001284 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001285 return 0;
1286}
1287
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001288/**
1289 * process_options
1290 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001291static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1292 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001293{
1294 u32 tmp;
1295 u32 offset = 0;
1296 union all_known_options *all_options;
1297 char got_mss_option = 0;
1298
1299 while (offset < optionsize) {
1300 all_options = (union all_known_options *)(optionsloc + offset);
1301 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001302 case OPTION_NUMBER_END:
1303 offset = optionsize;
1304 break;
1305 case OPTION_NUMBER_NONE:
1306 offset += 1;
1307 continue;
1308 case OPTION_NUMBER_MSS:
1309 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1310 "Size: %d\n", __func__,
1311 all_options->as_mss.length, offset, optionsize);
1312 got_mss_option = 1;
1313 if (all_options->as_mss.length != 4) {
1314 return 1;
1315 } else {
1316 tmp = ntohs(all_options->as_mss.mss);
1317 if (tmp > 0 && tmp <
1318 cm_node->tcp_cntxt.mss)
1319 cm_node->tcp_cntxt.mss = tmp;
1320 }
1321 break;
1322 case OPTION_NUMBER_WINDOW_SCALE:
1323 cm_node->tcp_cntxt.snd_wscale =
1324 all_options->as_windowscale.shiftcount;
1325 break;
1326 case OPTION_NUMBER_WRITE0:
1327 cm_node->send_write0 = 1;
1328 break;
1329 default:
1330 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1331 all_options->as_base.optionnum);
1332 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001333 }
1334 offset += all_options->as_base.length;
1335 }
1336 if ((!got_mss_option) && (syn_packet))
1337 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1338 return 0;
1339}
1340
Faisal Latif6492cdf2008-07-24 20:50:45 -07001341static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001342{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001343 atomic_inc(&cm_accel_dropped_pkts);
1344 dev_kfree_skb_any(skb);
1345}
1346
Faisal Latif9d5ab132009-03-06 15:15:01 -08001347static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001348{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001349 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1350 "refcnt=%d\n", cm_node, cm_node->state,
1351 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001352 switch (cm_node->state) {
1353 case NES_CM_STATE_SYN_RCVD:
1354 case NES_CM_STATE_SYN_SENT:
1355 case NES_CM_STATE_ESTABLISHED:
1356 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001357 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001358 cm_node->tcp_cntxt.rcv_nxt++;
1359 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001360 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001361 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001362 break;
1363 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001364 cm_node->tcp_cntxt.rcv_nxt++;
1365 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001366 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001367 send_ack(cm_node, NULL);
1368 /* Wait for ACK as this is simultanous close..
1369 * After we receive ACK, do not send anything..
1370 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001371 break;
1372 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001373 cm_node->tcp_cntxt.rcv_nxt++;
1374 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001375 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001376 send_ack(cm_node, NULL);
1377 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1378 break;
1379 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001380 cm_node->tcp_cntxt.rcv_nxt++;
1381 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001382 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001383 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001384 break;
1385 case NES_CM_STATE_TSA:
1386 default:
1387 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1388 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001389 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001390 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001391}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001392
Faisal Latif6492cdf2008-07-24 20:50:45 -07001393
1394static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1395 struct tcphdr *tcph)
1396{
1397
1398 int reset = 0; /* whether to send reset in case of err.. */
Faisal Latif183ecfa2008-11-21 20:50:46 -06001399 int passive_state;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001400 atomic_inc(&cm_resets_recvd);
1401 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1402 " refcnt=%d\n", cm_node, cm_node->state,
1403 atomic_read(&cm_node->ref_count));
1404 cleanup_retrans_entry(cm_node);
1405 switch (cm_node->state) {
1406 case NES_CM_STATE_SYN_SENT:
1407 case NES_CM_STATE_MPAREQ_SENT:
1408 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1409 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1410 cm_node->listener, cm_node->state);
1411 active_open_err(cm_node, skb, reset);
1412 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001413 case NES_CM_STATE_MPAREQ_RCVD:
1414 passive_state = atomic_add_return(1, &cm_node->passive_state);
1415 if (passive_state == NES_SEND_RESET_EVENT)
1416 create_event(cm_node, NES_CM_EVENT_RESET);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001417 cm_node->state = NES_CM_STATE_CLOSED;
1418 dev_kfree_skb_any(skb);
1419 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001420 case NES_CM_STATE_ESTABLISHED:
1421 case NES_CM_STATE_SYN_RCVD:
1422 case NES_CM_STATE_LISTENING:
1423 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1424 passive_open_err(cm_node, skb, reset);
1425 break;
1426 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001427 active_open_err(cm_node, skb, reset);
1428 break;
1429 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001430 drop_packet(skb);
1431 break;
Faisal Latif68237a02009-06-22 22:53:28 -07001432 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001433 case NES_CM_STATE_LAST_ACK:
1434 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001435 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001436 cm_node->state = NES_CM_STATE_CLOSED;
1437 rem_ref_cm_node(cm_node->cm_core, cm_node);
1438 drop_packet(skb);
1439 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001440 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001441 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001442 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001443 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001444}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001445
Faisal Latif9d5ab132009-03-06 15:15:01 -08001446
1447static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001448{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001449
Faisal Latif9d5ab132009-03-06 15:15:01 -08001450 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001451 int datasize = skb->len;
1452 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001453
1454 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1455 u32 res_type;
1456 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1457 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001458 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001459 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001460 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1461 "cm_node=%p listener=%p state=%d\n", __func__,
1462 __LINE__, cm_node, cm_node->listener,
1463 cm_node->state);
1464 active_open_err(cm_node, skb, 1);
1465 } else {
1466 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001467 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001468 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001469 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001470
1471 switch (cm_node->state) {
1472 case NES_CM_STATE_ESTABLISHED:
1473 if (res_type == NES_MPA_REQUEST_REJECT) {
1474 /*BIG problem as we are receiving the MPA.. So should
1475 * not be REJECT.. This is Passive Open.. We can
1476 * only receive it Reject for Active Open...*/
1477 WARN_ON(1);
1478 }
1479 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1480 type = NES_CM_EVENT_MPA_REQ;
1481 atomic_set(&cm_node->passive_state,
1482 NES_PASSIVE_STATE_INDICATED);
1483 break;
1484 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001485 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001486 if (res_type == NES_MPA_REQUEST_REJECT) {
1487 type = NES_CM_EVENT_MPA_REJECT;
1488 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1489 } else {
1490 type = NES_CM_EVENT_CONNECTED;
1491 cm_node->state = NES_CM_STATE_TSA;
1492 }
1493
1494 break;
1495 default:
1496 WARN_ON(1);
1497 break;
1498 }
1499 dev_kfree_skb_any(skb);
1500 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001501}
1502
1503static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1504{
1505 switch (cm_node->state) {
1506 case NES_CM_STATE_SYN_SENT:
1507 case NES_CM_STATE_MPAREQ_SENT:
1508 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1509 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1510 cm_node->listener, cm_node->state);
1511 active_open_err(cm_node, skb, 1);
1512 break;
1513 case NES_CM_STATE_ESTABLISHED:
1514 case NES_CM_STATE_SYN_RCVD:
1515 passive_open_err(cm_node, skb, 1);
1516 break;
1517 case NES_CM_STATE_TSA:
1518 default:
1519 drop_packet(skb);
1520 }
1521}
1522
1523static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1524 struct sk_buff *skb)
1525{
1526 int err;
1527
1528 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1529 if (err)
1530 active_open_err(cm_node, skb, 1);
1531
1532 return err;
1533}
1534
1535static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1536 struct sk_buff *skb)
1537{
1538 int err = 0;
1539 u32 seq;
1540 u32 ack_seq;
1541 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1542 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1543 u32 rcv_wnd;
1544 seq = ntohl(tcph->seq);
1545 ack_seq = ntohl(tcph->ack_seq);
1546 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1547 if (ack_seq != loc_seq_num)
1548 err = 1;
Faisal Latifc11470f2009-04-27 13:38:31 -07001549 else if (!between(seq, rcv_nxt, (rcv_nxt+rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001550 err = 1;
1551 if (err) {
1552 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1553 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1554 cm_node->listener, cm_node->state);
1555 indicate_pkt_err(cm_node, skb);
1556 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1557 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1558 rcv_wnd);
1559 }
1560 return err;
1561}
1562
1563/*
1564 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1565 * is created with a listener or it may comein as rexmitted packet which in
1566 * that case will be just dropped.
1567 */
1568
1569static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1570 struct tcphdr *tcph)
1571{
1572 int ret;
1573 u32 inc_sequence;
1574 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001575
1576 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001577 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001578 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001579
Faisal Latif6492cdf2008-07-24 20:50:45 -07001580 switch (cm_node->state) {
1581 case NES_CM_STATE_SYN_SENT:
1582 case NES_CM_STATE_MPAREQ_SENT:
1583 /* Rcvd syn on active open connection*/
1584 active_open_err(cm_node, skb, 1);
1585 break;
1586 case NES_CM_STATE_LISTENING:
1587 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001588 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1589 cm_node->listener->backlog) {
1590 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1591 "pressure \n");
1592 cm_backlog_drops++;
1593 passive_open_err(cm_node, skb, 0);
1594 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001595 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001596 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1597 1);
1598 if (ret) {
1599 passive_open_err(cm_node, skb, 0);
1600 /* drop pkt */
1601 break;
1602 }
1603 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1604 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001605 cm_node->accept_pend = 1;
1606 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1607
Faisal Latif6492cdf2008-07-24 20:50:45 -07001608 cm_node->state = NES_CM_STATE_SYN_RCVD;
1609 send_syn(cm_node, 1, skb);
1610 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001611 case NES_CM_STATE_CLOSED:
1612 cleanup_retrans_entry(cm_node);
1613 send_reset(cm_node, skb);
1614 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001615 case NES_CM_STATE_TSA:
1616 case NES_CM_STATE_ESTABLISHED:
1617 case NES_CM_STATE_FIN_WAIT1:
1618 case NES_CM_STATE_FIN_WAIT2:
1619 case NES_CM_STATE_MPAREQ_RCVD:
1620 case NES_CM_STATE_LAST_ACK:
1621 case NES_CM_STATE_CLOSING:
1622 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001623 default:
1624 drop_packet(skb);
1625 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001626 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001627}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001628
Faisal Latif6492cdf2008-07-24 20:50:45 -07001629static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1630 struct tcphdr *tcph)
1631{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001632
Faisal Latif6492cdf2008-07-24 20:50:45 -07001633 int ret;
1634 u32 inc_sequence;
1635 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001636
Faisal Latif6492cdf2008-07-24 20:50:45 -07001637 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001638 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001639 inc_sequence = ntohl(tcph->seq);
1640 switch (cm_node->state) {
1641 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001642 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001643 /* active open */
1644 if (check_syn(cm_node, tcph, skb))
1645 return;
1646 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1647 /* setup options */
1648 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1649 if (ret) {
1650 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1651 cm_node);
1652 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001653 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001654 cleanup_retrans_entry(cm_node);
1655 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1656 send_mpa_request(cm_node, skb);
1657 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1658 break;
1659 case NES_CM_STATE_MPAREQ_RCVD:
1660 /* passive open, so should not be here */
1661 passive_open_err(cm_node, skb, 1);
1662 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001663 case NES_CM_STATE_LISTENING:
1664 case NES_CM_STATE_CLOSED:
1665 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1666 cleanup_retrans_entry(cm_node);
1667 send_reset(cm_node, skb);
1668 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001669 case NES_CM_STATE_ESTABLISHED:
1670 case NES_CM_STATE_FIN_WAIT1:
1671 case NES_CM_STATE_FIN_WAIT2:
1672 case NES_CM_STATE_LAST_ACK:
1673 case NES_CM_STATE_TSA:
1674 case NES_CM_STATE_CLOSING:
1675 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001676 case NES_CM_STATE_MPAREQ_SENT:
1677 default:
1678 drop_packet(skb);
1679 break;
1680 }
1681}
1682
Faisal Latif109d67e2009-04-27 13:41:06 -07001683static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001684 struct tcphdr *tcph)
1685{
1686 int datasize = 0;
1687 u32 inc_sequence;
1688 u32 rem_seq_ack;
1689 u32 rem_seq;
Faisal Latif109d67e2009-04-27 13:41:06 -07001690 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06001691 int optionsize;
Faisal Latifabb77252008-11-21 20:50:52 -06001692 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001693
Faisal Latif6492cdf2008-07-24 20:50:45 -07001694 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07001695 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001696
1697 skb_pull(skb, tcph->doff << 2);
1698 inc_sequence = ntohl(tcph->seq);
1699 rem_seq = ntohl(tcph->seq);
1700 rem_seq_ack = ntohl(tcph->ack_seq);
1701 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001702 switch (cm_node->state) {
1703 case NES_CM_STATE_SYN_RCVD:
1704 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07001705 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001706 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1707 if (ret)
1708 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001709 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1710 cm_node->state = NES_CM_STATE_ESTABLISHED;
1711 if (datasize) {
1712 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001713 handle_rcv_mpa(cm_node, skb);
Faisal Latif109d67e2009-04-27 13:41:06 -07001714 } else /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001715 dev_kfree_skb_any(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001716 break;
1717 case NES_CM_STATE_ESTABLISHED:
1718 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001719 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001720 if (datasize) {
1721 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001722 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001723 } else
1724 drop_packet(skb);
1725 break;
1726 case NES_CM_STATE_MPAREQ_SENT:
1727 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1728 if (datasize) {
1729 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001730 handle_rcv_mpa(cm_node, skb);
Faisal Latif109d67e2009-04-27 13:41:06 -07001731 } else /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001732 dev_kfree_skb_any(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001733 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001734 case NES_CM_STATE_LISTENING:
1735 case NES_CM_STATE_CLOSED:
1736 cleanup_retrans_entry(cm_node);
1737 send_reset(cm_node, skb);
1738 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001739 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07001740 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001741 cleanup_retrans_entry(cm_node);
1742 cm_node->state = NES_CM_STATE_CLOSED;
1743 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001744 rem_ref_cm_node(cm_node->cm_core, cm_node);
1745 drop_packet(skb);
1746 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001747 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001748 cleanup_retrans_entry(cm_node);
1749 drop_packet(skb);
1750 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1751 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001752 case NES_CM_STATE_SYN_SENT:
1753 case NES_CM_STATE_FIN_WAIT2:
1754 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001755 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001756 case NES_CM_STATE_UNKNOWN:
1757 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07001758 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001759 drop_packet(skb);
1760 break;
1761 }
Faisal Latif109d67e2009-04-27 13:41:06 -07001762 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001763}
1764
1765
1766
1767static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1768 struct sk_buff *skb, int optionsize, int passive)
1769{
1770 u8 *optionsloc = (u8 *)&tcph[1];
1771 if (optionsize) {
1772 if (process_options(cm_node, optionsloc, optionsize,
1773 (u32)tcph->syn)) {
1774 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1775 __func__, cm_node);
1776 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06001777 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001778 else
Faisal Latifabb77252008-11-21 20:50:52 -06001779 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001780 return 1;
1781 }
1782 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001783
1784 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1785 cm_node->tcp_cntxt.snd_wscale;
1786
Faisal Latif6492cdf2008-07-24 20:50:45 -07001787 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001788 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001789 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001790}
1791
Faisal Latif6492cdf2008-07-24 20:50:45 -07001792/*
1793 * active_open_err() will send reset() if flag set..
1794 * It will also send ABORT event.
1795 */
1796
1797static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1798 int reset)
1799{
1800 cleanup_retrans_entry(cm_node);
1801 if (reset) {
1802 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1803 "state=%d\n", cm_node, cm_node->state);
1804 add_ref_cm_node(cm_node);
1805 send_reset(cm_node, skb);
1806 } else
1807 dev_kfree_skb_any(skb);
1808
1809 cm_node->state = NES_CM_STATE_CLOSED;
1810 create_event(cm_node, NES_CM_EVENT_ABORTED);
1811}
1812
1813/*
1814 * passive_open_err() will either do a reset() or will free up the skb and
1815 * remove the cm_node.
1816 */
1817
1818static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1819 int reset)
1820{
1821 cleanup_retrans_entry(cm_node);
1822 cm_node->state = NES_CM_STATE_CLOSED;
1823 if (reset) {
1824 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1825 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1826 send_reset(cm_node, skb);
1827 } else {
1828 dev_kfree_skb_any(skb);
1829 rem_ref_cm_node(cm_node->cm_core, cm_node);
1830 }
1831}
1832
1833/*
1834 * free_retrans_entry() routines assumes that the retrans_list_lock has
1835 * been acquired before calling.
1836 */
1837static void free_retrans_entry(struct nes_cm_node *cm_node)
1838{
1839 struct nes_timer_entry *send_entry;
1840 send_entry = cm_node->send_entry;
1841 if (send_entry) {
1842 cm_node->send_entry = NULL;
1843 dev_kfree_skb_any(send_entry->skb);
1844 kfree(send_entry);
1845 rem_ref_cm_node(cm_node->cm_core, cm_node);
1846 }
1847}
1848
1849static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1850{
1851 unsigned long flags;
1852
1853 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1854 free_retrans_entry(cm_node);
1855 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1856}
1857
1858/**
1859 * process_packet
1860 * Returns skb if to be freed, else it will return NULL if already used..
1861 */
1862static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1863 struct nes_cm_core *cm_core)
1864{
1865 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1866 struct tcphdr *tcph = tcp_hdr(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001867 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07001868 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001869 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1870
1871 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1872 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1873 tcph->ack, tcph->rst, tcph->fin);
1874
1875 if (tcph->rst)
1876 pkt_type = NES_PKT_TYPE_RST;
1877 else if (tcph->syn) {
1878 pkt_type = NES_PKT_TYPE_SYN;
1879 if (tcph->ack)
1880 pkt_type = NES_PKT_TYPE_SYNACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001881 } else if (tcph->ack)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001882 pkt_type = NES_PKT_TYPE_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001883 if (tcph->fin)
1884 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001885
1886 switch (pkt_type) {
1887 case NES_PKT_TYPE_SYN:
1888 handle_syn_pkt(cm_node, skb, tcph);
1889 break;
1890 case NES_PKT_TYPE_SYNACK:
1891 handle_synack_pkt(cm_node, skb, tcph);
1892 break;
1893 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07001894 ret = handle_ack_pkt(cm_node, skb, tcph);
1895 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001896 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001897 break;
1898 case NES_PKT_TYPE_RST:
1899 handle_rst_pkt(cm_node, skb, tcph);
1900 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001901 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07001902 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08001903 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07001904 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001905 break;
1906 }
1907}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001908
1909/**
1910 * mini_cm_listen - create a listen node with params
1911 */
1912static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001913 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001914{
1915 struct nes_cm_listener *listener;
1916 unsigned long flags;
1917
1918 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1919 cm_info->loc_addr, cm_info->loc_port);
1920
1921 /* cannot have multiple matching listeners */
1922 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1923 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1924 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1925 /* find automatically incs ref count ??? */
1926 atomic_dec(&listener->ref_count);
1927 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1928 return NULL;
1929 }
1930
1931 if (!listener) {
1932 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1933 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1934 if (!listener) {
1935 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1936 return NULL;
1937 }
1938
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001939 listener->loc_addr = htonl(cm_info->loc_addr);
1940 listener->loc_port = htons(cm_info->loc_port);
1941 listener->reused_node = 0;
1942
1943 atomic_set(&listener->ref_count, 1);
1944 }
1945 /* pasive case */
1946 /* find already inc'ed the ref count */
1947 else {
1948 listener->reused_node = 1;
1949 }
1950
1951 listener->cm_id = cm_info->cm_id;
1952 atomic_set(&listener->pend_accepts_cnt, 0);
1953 listener->cm_core = cm_core;
1954 listener->nesvnic = nesvnic;
1955 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001956
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001957 listener->conn_type = cm_info->conn_type;
1958 listener->backlog = cm_info->backlog;
1959 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1960
1961 if (!listener->reused_node) {
1962 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1963 list_add(&listener->list, &cm_core->listen_list.list);
1964 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1965 atomic_inc(&cm_core->listen_node_cnt);
1966 }
1967
1968 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1969 " listener = %p, backlog = %d, cm_id = %p.\n",
1970 cm_info->loc_addr, cm_info->loc_port,
1971 listener, listener->backlog, listener->cm_id);
1972
1973 return listener;
1974}
1975
1976
1977/**
1978 * mini_cm_connect - make a connection node with params
1979 */
Faisal Latif54c86a82008-09-30 14:47:27 -07001980static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001981 struct nes_vnic *nesvnic, u16 private_data_len,
1982 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001983{
1984 int ret = 0;
1985 struct nes_cm_node *cm_node;
1986 struct nes_cm_listener *loopbackremotelistener;
1987 struct nes_cm_node *loopbackremotenode;
1988 struct nes_cm_info loopback_cm_info;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001989 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
1990 struct ietf_mpa_frame *mpa_frame = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001991
1992 /* create a CM connection node */
1993 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
1994 if (!cm_node)
1995 return NULL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001996 mpa_frame = &cm_node->mpa_frame;
Faisal Latif1cf078c2009-12-09 15:53:54 -08001997 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001998 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
1999 mpa_frame->rev = IETF_MPA_VERSION;
2000 mpa_frame->priv_data_len = htons(private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002001
Glenn Streiff7495ab62008-04-29 13:46:54 -07002002 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002003 cm_node->tcp_cntxt.client = 1;
2004 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2005
2006 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002007 loopbackremotelistener = find_listener(cm_core,
2008 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2009 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002010 if (loopbackremotelistener == NULL) {
2011 create_event(cm_node, NES_CM_EVENT_ABORTED);
2012 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002013 loopback_cm_info = *cm_info;
2014 loopback_cm_info.loc_port = cm_info->rem_port;
2015 loopback_cm_info.rem_port = cm_info->loc_port;
2016 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002017 loopbackremotenode = make_cm_node(cm_core, nesvnic,
2018 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002019 if (!loopbackremotenode) {
2020 rem_ref_cm_node(cm_node->cm_core, cm_node);
2021 return NULL;
2022 }
2023 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002024 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002025 loopbackremotenode->tcp_cntxt.rcv_wscale =
2026 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002027 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002028 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2029 private_data_len);
2030 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002031
Faisal Latif6492cdf2008-07-24 20:50:45 -07002032 /* we are done handling this state. */
2033 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002034 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002035 cm_node->tcp_cntxt.rcv_nxt =
2036 loopbackremotenode->tcp_cntxt.loc_seq_num;
2037 loopbackremotenode->tcp_cntxt.rcv_nxt =
2038 cm_node->tcp_cntxt.loc_seq_num;
2039 cm_node->tcp_cntxt.max_snd_wnd =
2040 loopbackremotenode->tcp_cntxt.rcv_wnd;
2041 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2042 cm_node->tcp_cntxt.rcv_wnd;
2043 cm_node->tcp_cntxt.snd_wnd =
2044 loopbackremotenode->tcp_cntxt.rcv_wnd;
2045 loopbackremotenode->tcp_cntxt.snd_wnd =
2046 cm_node->tcp_cntxt.rcv_wnd;
2047 cm_node->tcp_cntxt.snd_wscale =
2048 loopbackremotenode->tcp_cntxt.rcv_wscale;
2049 loopbackremotenode->tcp_cntxt.snd_wscale =
2050 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002051 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002052 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2053 }
2054 return cm_node;
2055 }
2056
2057 /* set our node side to client (active) side */
2058 cm_node->tcp_cntxt.client = 1;
2059 /* init our MPA frame ptr */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002060 memcpy(mpa_frame->priv_data, private_data, private_data_len);
2061
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002062 cm_node->mpa_frame_size = mpa_frame_size;
2063
2064 /* send a syn and goto syn sent state */
2065 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002066 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002067
Faisal Latif6492cdf2008-07-24 20:50:45 -07002068 if (ret) {
2069 /* error in sending the syn free up the cm_node struct */
2070 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2071 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2072 cm_node->rem_addr, cm_node->rem_port, cm_node,
2073 cm_node->cm_id);
2074 rem_ref_cm_node(cm_node->cm_core, cm_node);
2075 cm_node = NULL;
2076 }
2077
2078 if (cm_node)
2079 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2080 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2081 cm_node->rem_addr, cm_node->rem_port, cm_node,
2082 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002083
2084 return cm_node;
2085}
2086
2087
2088/**
2089 * mini_cm_accept - accept a connection
2090 * This function is never called
2091 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002092static int mini_cm_accept(struct nes_cm_core *cm_core,
2093 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002094{
2095 return 0;
2096}
2097
2098
2099/**
2100 * mini_cm_reject - reject and teardown a connection
2101 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002102static int mini_cm_reject(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002103 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002104{
2105 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002106 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002107 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002108 struct nes_cm_event event;
2109 struct iw_cm_id *cm_id = cm_node->cm_id;
2110 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002111
Faisal Latif6492cdf2008-07-24 20:50:45 -07002112 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2113 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002114
Faisal Latif6492cdf2008-07-24 20:50:45 -07002115 if (cm_node->tcp_cntxt.client)
2116 return ret;
2117 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002118
Faisal Latif9d5ab132009-03-06 15:15:01 -08002119 if (!loopback) {
2120 passive_state = atomic_add_return(1, &cm_node->passive_state);
2121 if (passive_state == NES_SEND_RESET_EVENT) {
2122 cm_node->state = NES_CM_STATE_CLOSED;
2123 rem_ref_cm_node(cm_core, cm_node);
2124 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002125 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2126 rem_ref_cm_node(cm_core, cm_node);
2127 } else {
2128 ret = send_mpa_reject(cm_node);
2129 if (ret) {
2130 cm_node->state = NES_CM_STATE_CLOSED;
2131 err = send_reset(cm_node, NULL);
2132 if (err)
2133 WARN_ON(1);
2134 } else
2135 cm_id->add_ref(cm_id);
2136 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002137 }
2138 } else {
2139 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002140 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2141 rem_ref_cm_node(cm_core, cm_node);
2142 rem_ref_cm_node(cm_core, loopback);
2143 } else {
2144 event.cm_node = loopback;
2145 event.cm_info.rem_addr = loopback->rem_addr;
2146 event.cm_info.loc_addr = loopback->loc_addr;
2147 event.cm_info.rem_port = loopback->rem_port;
2148 event.cm_info.loc_port = loopback->loc_port;
2149 event.cm_info.cm_id = loopback->cm_id;
2150 cm_event_mpa_reject(&event);
2151 rem_ref_cm_node(cm_core, cm_node);
2152 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002153
Faisal Latifc5a7d482009-12-09 15:54:08 -08002154 cm_id = loopback->cm_id;
2155 rem_ref_cm_node(cm_core, loopback);
2156 cm_id->rem_ref(cm_id);
2157 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002158 }
2159
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002160 return ret;
2161}
2162
2163
2164/**
2165 * mini_cm_close
2166 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002167static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002168{
2169 int ret = 0;
2170
2171 if (!cm_core || !cm_node)
2172 return -EINVAL;
2173
2174 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002175 case NES_CM_STATE_SYN_RCVD:
2176 case NES_CM_STATE_SYN_SENT:
2177 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2178 case NES_CM_STATE_ESTABLISHED:
2179 case NES_CM_STATE_ACCEPTING:
2180 case NES_CM_STATE_MPAREQ_SENT:
2181 case NES_CM_STATE_MPAREQ_RCVD:
2182 cleanup_retrans_entry(cm_node);
2183 send_reset(cm_node, NULL);
2184 break;
2185 case NES_CM_STATE_CLOSE_WAIT:
2186 cm_node->state = NES_CM_STATE_LAST_ACK;
2187 send_fin(cm_node, NULL);
2188 break;
2189 case NES_CM_STATE_FIN_WAIT1:
2190 case NES_CM_STATE_FIN_WAIT2:
2191 case NES_CM_STATE_LAST_ACK:
2192 case NES_CM_STATE_TIME_WAIT:
2193 case NES_CM_STATE_CLOSING:
2194 ret = -1;
2195 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002196 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002197 case NES_CM_STATE_LISTENING:
2198 case NES_CM_STATE_UNKNOWN:
2199 case NES_CM_STATE_INITED:
2200 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002201 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002202 ret = rem_ref_cm_node(cm_core, cm_node);
2203 break;
2204 case NES_CM_STATE_TSA:
2205 if (cm_node->send_entry)
2206 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2207 "send_entry=%p\n", cm_node->send_entry);
2208 ret = rem_ref_cm_node(cm_core, cm_node);
2209 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002210 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002211 return ret;
2212}
2213
2214
2215/**
2216 * recv_pkt - recv an ETHERNET packet, and process it through CM
2217 * node state machine
2218 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002219static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002220 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002221{
2222 struct nes_cm_node *cm_node = NULL;
2223 struct nes_cm_listener *listener = NULL;
2224 struct iphdr *iph;
2225 struct tcphdr *tcph;
2226 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002227 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002228 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002229
Faisal Latif6492cdf2008-07-24 20:50:45 -07002230 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002231 return 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002232 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002233 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002234 }
2235
2236 iph = (struct iphdr *)skb->data;
2237 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002238
2239 nfo.loc_addr = ntohl(iph->daddr);
2240 nfo.loc_port = ntohs(tcph->dest);
2241 nfo.rem_addr = ntohl(iph->saddr);
2242 nfo.rem_port = ntohs(tcph->source);
2243
Harvey Harrison03080e52009-01-10 21:45:42 -08002244 tmp_daddr = cpu_to_be32(iph->daddr);
2245 tmp_saddr = cpu_to_be32(iph->saddr);
2246
Harvey Harrison63779432008-10-31 00:56:00 -07002247 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002248 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002249
Faisal Latif6492cdf2008-07-24 20:50:45 -07002250 do {
2251 cm_node = find_node(cm_core,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002252 nfo.rem_port, nfo.rem_addr,
2253 nfo.loc_port, nfo.loc_addr);
2254
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002255 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002256 /* Only type of packet accepted are for */
2257 /* the PASSIVE open (syn only) */
2258 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002259 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002260 break;
2261 }
2262 listener = find_listener(cm_core, nfo.loc_addr,
2263 nfo.loc_port,
2264 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002265 if (!listener) {
2266 nfo.cm_id = NULL;
2267 nfo.conn_type = 0;
2268 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2269 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002270 break;
2271 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002272 nfo.cm_id = listener->cm_id;
2273 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002274 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2275 listener);
2276 if (!cm_node) {
2277 nes_debug(NES_DBG_CM, "Unable to allocate "
2278 "node\n");
2279 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002280 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002281 dev_kfree_skb_any(skb);
2282 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002283 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002284 if (!tcph->rst && !tcph->fin) {
2285 cm_node->state = NES_CM_STATE_LISTENING;
2286 } else {
2287 cm_packets_dropped++;
2288 rem_ref_cm_node(cm_core, cm_node);
2289 dev_kfree_skb_any(skb);
2290 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002291 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002292 add_ref_cm_node(cm_node);
2293 } else if (cm_node->state == NES_CM_STATE_TSA) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002294 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002295 atomic_inc(&cm_accel_dropped_pkts);
2296 dev_kfree_skb_any(skb);
2297 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002298 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002299 skb_reset_network_header(skb);
2300 skb_set_transport_header(skb, sizeof(*tcph));
2301 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002302 process_packet(cm_node, skb, cm_core);
2303 rem_ref_cm_node(cm_core, cm_node);
2304 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002305 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002306}
2307
2308
2309/**
2310 * nes_cm_alloc_core - allocate a top level instance of a cm core
2311 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002312static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002313{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002314 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002315
2316 /* setup the CM core */
2317 /* alloc top level core control structure */
2318 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2319 if (!cm_core)
2320 return NULL;
2321
2322 INIT_LIST_HEAD(&cm_core->connected_nodes);
2323 init_timer(&cm_core->tcp_timer);
2324 cm_core->tcp_timer.function = nes_cm_timer_tick;
2325
2326 cm_core->mtu = NES_CM_DEFAULT_MTU;
2327 cm_core->state = NES_CM_STATE_INITED;
2328 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2329
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002330 atomic_set(&cm_core->events_posted, 0);
2331
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002332 cm_core->api = &nes_cm_api;
2333
2334 spin_lock_init(&cm_core->ht_lock);
2335 spin_lock_init(&cm_core->listen_list_lock);
2336
2337 INIT_LIST_HEAD(&cm_core->listen_list.list);
2338
2339 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2340
2341 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2342 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2343 cm_core->post_event = nes_cm_post_event;
2344 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2345 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2346
2347 print_core(cm_core);
2348 return cm_core;
2349}
2350
2351
2352/**
2353 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2354 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002355static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002356{
2357 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2358
2359 if (!cm_core)
2360 return -EINVAL;
2361
2362 barrier();
2363
2364 if (timer_pending(&cm_core->tcp_timer)) {
2365 del_timer(&cm_core->tcp_timer);
2366 }
2367
2368 destroy_workqueue(cm_core->event_wq);
2369 destroy_workqueue(cm_core->disconn_wq);
2370 nes_debug(NES_DBG_CM, "\n");
2371 kfree(cm_core);
2372
2373 return 0;
2374}
2375
2376
2377/**
2378 * mini_cm_get
2379 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002380static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002381{
2382 return cm_core->state;
2383}
2384
2385
2386/**
2387 * mini_cm_set
2388 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002389static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002390{
2391 int ret = 0;
2392
2393 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002394 case NES_CM_SET_PKT_SIZE:
2395 cm_core->mtu = value;
2396 break;
2397 case NES_CM_SET_FREE_PKT_Q_SIZE:
2398 cm_core->free_tx_pkt_max = value;
2399 break;
2400 default:
2401 /* unknown set option */
2402 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002403 }
2404
2405 return ret;
2406}
2407
2408
2409/**
2410 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2411 * successfully exchanged when this is called
2412 */
2413static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2414{
2415 int ret = 0;
2416
2417 if (!nesqp)
2418 return -EINVAL;
2419
2420 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2421 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2422 NES_QPCONTEXT_MISC_DROS);
2423
2424 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2425 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2426
2427 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2428
2429 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2430
2431 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2432 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2433
2434 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2435 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2436 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2437
2438 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2439 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2440 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2441
2442 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2443 nesqp->nesqp_context->ts_recent = 0;
2444 nesqp->nesqp_context->ts_age = 0;
2445 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2446 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2447 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2448 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2449 cm_node->tcp_cntxt.rcv_wscale);
2450 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2451 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2452 nesqp->nesqp_context->srtt = 0;
2453 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2454 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2455 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2456 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2457 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2458 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2459
2460 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2461 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2462 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2463 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2464 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2465 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2466 le32_to_cpu(nesqp->nesqp_context->misc));
2467 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2468 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2469 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2470
2471 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2472 cm_node->state = NES_CM_STATE_TSA;
2473
2474 return ret;
2475}
2476
2477
2478/**
2479 * nes_cm_disconn
2480 */
2481int nes_cm_disconn(struct nes_qp *nesqp)
2482{
Don Wood873fcdd2009-09-05 20:36:37 -07002483 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002484
Don Wood873fcdd2009-09-05 20:36:37 -07002485 work = kzalloc(sizeof *work, GFP_ATOMIC);
2486 if (!work)
2487 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002488
Don Wood873fcdd2009-09-05 20:36:37 -07002489 nes_add_ref(&nesqp->ibqp);
2490 work->nesqp = nesqp;
2491 INIT_WORK(&work->work, nes_disconnect_worker);
2492 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002493 return 0;
2494}
2495
2496
2497/**
2498 * nes_disconnect_worker
2499 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002500static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002501{
Don Wood873fcdd2009-09-05 20:36:37 -07002502 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2503 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002504
Don Wood873fcdd2009-09-05 20:36:37 -07002505 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002506 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2507 nesqp->last_aeq, nesqp->hwqp.qp_id);
2508 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002509 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002510}
2511
2512
2513/**
2514 * nes_cm_disconn_true
2515 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002516static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002517{
2518 unsigned long flags;
2519 int ret = 0;
2520 struct iw_cm_id *cm_id;
2521 struct iw_cm_event cm_event;
2522 struct nes_vnic *nesvnic;
2523 u16 last_ae;
2524 u8 original_hw_tcp_state;
2525 u8 original_ibqp_state;
Don Woodb29a4fc2009-09-05 20:36:39 -07002526 enum iw_cm_event_type disconn_status = IW_CM_EVENT_STATUS_OK;
2527 int issue_disconn = 0;
2528 int issue_close = 0;
2529 int issue_flush = 0;
2530 u32 flush_q = NES_CQP_FLUSH_RQ;
2531 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002532
2533 if (!nesqp) {
2534 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2535 return -1;
2536 }
2537
2538 spin_lock_irqsave(&nesqp->lock, flags);
2539 cm_id = nesqp->cm_id;
2540 /* make sure we havent already closed this connection */
2541 if (!cm_id) {
2542 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2543 nesqp->hwqp.qp_id);
2544 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002545 return -1;
2546 }
2547
2548 nesvnic = to_nesvnic(nesqp->ibqp.device);
2549 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2550
2551 original_hw_tcp_state = nesqp->hw_tcp_state;
2552 original_ibqp_state = nesqp->ibqp_state;
2553 last_ae = nesqp->last_aeq;
2554
Don Woodb29a4fc2009-09-05 20:36:39 -07002555 if (nesqp->term_flags) {
2556 issue_disconn = 1;
2557 issue_close = 1;
2558 nesqp->cm_id = NULL;
2559 if (nesqp->flush_issued == 0) {
2560 nesqp->flush_issued = 1;
2561 issue_flush = 1;
2562 }
2563 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2564 ((original_ibqp_state == IB_QPS_RTS) &&
2565 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2566 issue_disconn = 1;
2567 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
2568 disconn_status = IW_CM_EVENT_STATUS_RESET;
2569 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002570
Don Woodb29a4fc2009-09-05 20:36:39 -07002571 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2572 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2573 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2574 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2575 issue_close = 1;
2576 nesqp->cm_id = NULL;
2577 if (nesqp->flush_issued == 0) {
2578 nesqp->flush_issued = 1;
2579 issue_flush = 1;
2580 }
2581 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002582
Don Woodb29a4fc2009-09-05 20:36:39 -07002583 spin_unlock_irqrestore(&nesqp->lock, flags);
2584
2585 if ((issue_flush) && (nesqp->destroyed == 0)) {
2586 /* Flush the queue(s) */
2587 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2588 flush_q |= NES_CQP_FLUSH_SQ;
2589 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2590
2591 if (nesqp->term_flags) {
2592 ibevent.device = nesqp->ibqp.device;
2593 ibevent.event = nesqp->terminate_eventtype;
2594 ibevent.element.qp = &nesqp->ibqp;
2595 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
2596 }
2597 }
2598
2599 if ((cm_id) && (cm_id->event_handler)) {
2600 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002601 atomic_inc(&cm_disconnects);
2602 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002603 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002604 cm_event.local_addr = cm_id->local_addr;
2605 cm_event.remote_addr = cm_id->remote_addr;
2606 cm_event.private_data = NULL;
2607 cm_event.private_data_len = 0;
2608
Faisal Latif6492cdf2008-07-24 20:50:45 -07002609 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2610 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2611 "cm_id = %p, refcount = %u.\n",
2612 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2613 nesqp->hwqp.sq_tail, cm_id,
2614 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002615
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002616 ret = cm_id->event_handler(cm_id, &cm_event);
2617 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002618 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2619 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002620 }
2621
Don Woodb29a4fc2009-09-05 20:36:39 -07002622 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002623 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002624 nes_disconnect(nesqp, 1);
2625
2626 cm_id->provider_data = nesqp;
2627 /* Send up the close complete event */
2628 cm_event.event = IW_CM_EVENT_CLOSE;
2629 cm_event.status = IW_CM_EVENT_STATUS_OK;
2630 cm_event.provider_data = cm_id->provider_data;
2631 cm_event.local_addr = cm_id->local_addr;
2632 cm_event.remote_addr = cm_id->remote_addr;
2633 cm_event.private_data = NULL;
2634 cm_event.private_data_len = 0;
2635
2636 ret = cm_id->event_handler(cm_id, &cm_event);
2637 if (ret) {
2638 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2639 }
2640
2641 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002642 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002643 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002644
2645 return 0;
2646}
2647
2648
2649/**
2650 * nes_disconnect
2651 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002652static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002653{
2654 int ret = 0;
2655 struct nes_vnic *nesvnic;
2656 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002657 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002658
2659 nesvnic = to_nesvnic(nesqp->ibqp.device);
2660 if (!nesvnic)
2661 return -EINVAL;
2662
2663 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002664 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002665
2666 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2667 atomic_read(&nesvnic->netdev->refcnt));
2668
2669 if (nesqp->active_conn) {
2670
2671 /* indicate this connection is NOT active */
2672 nesqp->active_conn = 0;
2673 } else {
2674 /* Need to free the Last Streaming Mode Message */
2675 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002676 if (nesqp->lsmm_mr)
2677 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002678 pci_free_consistent(nesdev->pcidev,
2679 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2680 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2681 }
2682 }
2683
2684 /* close the CM node down if it is still active */
2685 if (nesqp->cm_node) {
2686 nes_debug(NES_DBG_CM, "Call close API\n");
2687
2688 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002689 }
2690
2691 return ret;
2692}
2693
2694
2695/**
2696 * nes_accept
2697 */
2698int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2699{
2700 u64 u64temp;
2701 struct ib_qp *ibqp;
2702 struct nes_qp *nesqp;
2703 struct nes_vnic *nesvnic;
2704 struct nes_device *nesdev;
2705 struct nes_cm_node *cm_node;
2706 struct nes_adapter *adapter;
2707 struct ib_qp_attr attr;
2708 struct iw_cm_event cm_event;
2709 struct nes_hw_qp_wqe *wqe;
2710 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002711 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002712 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002713 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002714 struct nes_ib_device *nesibdev;
2715 struct ib_mr *ibmr = NULL;
2716 struct ib_phys_buf ibphysbuf;
2717 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002718 u64 tagged_offset;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002719
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002720 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2721 if (!ibqp)
2722 return -EINVAL;
2723
2724 /* get all our handles */
2725 nesqp = to_nesqp(ibqp);
2726 nesvnic = to_nesvnic(nesqp->ibqp.device);
2727 nesdev = nesvnic->nesdev;
2728 adapter = nesdev->nesadapter;
2729
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002730 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002731 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2732 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2733 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002734
Faisal Latifc5a7d482009-12-09 15:54:08 -08002735 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
2736 if (cm_node->loopbackpartner)
2737 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
2738 rem_ref_cm_node(cm_node->cm_core, cm_node);
2739 return -EINVAL;
2740 }
2741
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002742 /* associate the node with the QP */
2743 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002744 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002745
Faisal Latif6492cdf2008-07-24 20:50:45 -07002746 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2747 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002748 atomic_inc(&cm_accepts);
2749
2750 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2751 atomic_read(&nesvnic->netdev->refcnt));
2752
Faisal Latif6492cdf2008-07-24 20:50:45 -07002753 /* allocate the ietf frame and space for private data */
2754 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2755 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2756 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002757
Faisal Latif6492cdf2008-07-24 20:50:45 -07002758 if (!nesqp->ietf_frame) {
2759 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2760 "data\n");
2761 return -ENOMEM;
2762 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002763
2764
Faisal Latif6492cdf2008-07-24 20:50:45 -07002765 /* setup the MPA frame */
2766 nesqp->private_data_len = conn_param->private_data_len;
2767 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002768
Faisal Latif6492cdf2008-07-24 20:50:45 -07002769 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2770 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002771
Faisal Latif6492cdf2008-07-24 20:50:45 -07002772 nesqp->ietf_frame->priv_data_len =
2773 cpu_to_be16(conn_param->private_data_len);
2774 nesqp->ietf_frame->rev = mpa_version;
2775 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002776
Faisal Latif6492cdf2008-07-24 20:50:45 -07002777 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2778 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002779
Faisal Latif6492cdf2008-07-24 20:50:45 -07002780 if (cm_id->remote_addr.sin_addr.s_addr !=
2781 cm_id->local_addr.sin_addr.s_addr) {
2782 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002783 nesibdev = nesvnic->nesibdev;
2784 nespd = nesqp->nespd;
2785 ibphysbuf.addr = nesqp->ietf_frame_pbase;
2786 ibphysbuf.size = conn_param->private_data_len +
2787 sizeof(struct ietf_mpa_frame);
Don Wood7a5efb62009-04-08 14:21:02 -07002788 tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002789 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
2790 &ibphysbuf, 1,
2791 IB_ACCESS_LOCAL_WRITE,
Don Wood7a5efb62009-04-08 14:21:02 -07002792 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002793 if (!ibmr) {
2794 nes_debug(NES_DBG_CM, "Unable to register memory region"
2795 "for lSMM for cm_node = %p \n",
2796 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07002797 pci_free_consistent(nesdev->pcidev,
2798 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2799 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002800 return -ENOMEM;
2801 }
2802
2803 ibmr->pd = &nespd->ibpd;
2804 ibmr->device = nespd->ibpd.device;
2805 nesqp->lsmm_mr = ibmr;
2806
Faisal Latif6492cdf2008-07-24 20:50:45 -07002807 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2808 set_wqe_64bit_value(wqe->wqe_words,
2809 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2810 u64temp);
2811 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2812 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2813 NES_IWARP_SQ_WQE_WRPDU);
2814 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2815 cpu_to_le32(conn_param->private_data_len +
2816 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002817 set_wqe_64bit_value(wqe->wqe_words,
2818 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
Don Wood7a5efb62009-04-08 14:21:02 -07002819 (u64)(unsigned long)nesqp->ietf_frame);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002820 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2821 cpu_to_le32(conn_param->private_data_len +
2822 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002823 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002824
Faisal Latif6492cdf2008-07-24 20:50:45 -07002825 nesqp->nesqp_context->ird_ord_sizes |=
2826 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2827 NES_QPCONTEXT_ORDIRD_WRPDU);
2828 } else {
2829 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08002830 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002831 }
2832 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002833
2834
2835 /* Cache the cm_id in the qp */
2836 nesqp->cm_id = cm_id;
2837 cm_node->cm_id = cm_id;
2838
2839 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2840 cm_id->provider_data = nesqp;
2841 nesqp->active_conn = 0;
2842
Faisal Latif6492cdf2008-07-24 20:50:45 -07002843 if (cm_node->state == NES_CM_STATE_TSA)
2844 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2845 cm_node);
2846
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002847 nes_cm_init_tsa_conn(nesqp, cm_node);
2848
Faisal Latif6492cdf2008-07-24 20:50:45 -07002849 nesqp->nesqp_context->tcpPorts[0] =
2850 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2851 nesqp->nesqp_context->tcpPorts[1] =
2852 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2853
2854 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2855 nesqp->nesqp_context->ip0 =
2856 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2857 else
2858 nesqp->nesqp_context->ip0 =
2859 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002860
2861 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002862 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2863 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002864
Faisal Latif6492cdf2008-07-24 20:50:45 -07002865 nesqp->nesqp_context->arp_index_vlan |=
2866 cpu_to_le32(nes_arp_table(nesdev,
2867 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002868 NES_ARP_RESOLVE) << 16);
2869
2870 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002871 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002872
2873 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2874
2875 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002876 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2877 nesqp->nesqp_context->ird_ord_sizes |=
2878 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002879
2880 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002881 nes_quad.DstIpAdrIndex =
2882 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2883 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2884 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2885 else
2886 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2887 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2888 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002889
2890 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06002891 crc_value = get_crc_value(&nes_quad);
2892 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002893 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07002894 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002895
2896 nesqp->hte_index &= adapter->hte_index_mask;
2897 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2898
2899 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2900
Faisal Latif6492cdf2008-07-24 20:50:45 -07002901 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2902 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2903 "private data length=%zu.\n", nesqp->hwqp.qp_id,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002904 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2905 ntohs(cm_id->remote_addr.sin_port),
2906 ntohl(cm_id->local_addr.sin_addr.s_addr),
2907 ntohs(cm_id->local_addr.sin_port),
2908 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2909 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002910 conn_param->private_data_len +
2911 sizeof(struct ietf_mpa_frame));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002912
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002913
Coly Li73ac36e2009-01-07 18:09:16 -08002914 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002915 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07002916 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002917
2918 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2919 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2920 cm_event.provider_data = (void *)nesqp;
2921 cm_event.local_addr = cm_id->local_addr;
2922 cm_event.remote_addr = cm_id->remote_addr;
2923 cm_event.private_data = NULL;
2924 cm_event.private_data_len = 0;
2925 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002926 attr.qp_state = IB_QPS_RTS;
2927 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002928 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002929 cm_node->loopbackpartner->mpa_frame_size =
2930 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002931 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002932 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2933 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002934 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2935 }
2936 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002937 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2938 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002939
Faisal Latif183ecfa2008-11-21 20:50:46 -06002940 passive_state = atomic_add_return(1, &cm_node->passive_state);
2941 if (passive_state == NES_SEND_RESET_EVENT)
2942 create_event(cm_node, NES_CM_EVENT_RESET);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002943 return 0;
2944}
2945
2946
2947/**
2948 * nes_reject
2949 */
2950int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2951{
2952 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002953 struct nes_cm_node *loopback;
2954
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002955 struct nes_cm_core *cm_core;
2956
2957 atomic_inc(&cm_rejects);
2958 cm_node = (struct nes_cm_node *) cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002959 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002960 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002961 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002962 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2963
Faisal Latif9d5ab132009-03-06 15:15:01 -08002964 if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
2965 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002966
Faisal Latif1cf078c2009-12-09 15:53:54 -08002967 memcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002968 if (loopback) {
2969 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
2970 loopback->mpa_frame.priv_data_len = pdata_len;
2971 loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
2972 pdata_len;
2973 } else {
2974 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2975 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2976 }
2977
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002978 cm_node->mpa_frame.rev = mpa_version;
2979 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2980
Faisal Latif9d5ab132009-03-06 15:15:01 -08002981 return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002982}
2983
2984
2985/**
2986 * nes_connect
2987 * setup and launch cm connect node
2988 */
2989int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2990{
2991 struct ib_qp *ibqp;
2992 struct nes_qp *nesqp;
2993 struct nes_vnic *nesvnic;
2994 struct nes_device *nesdev;
2995 struct nes_cm_node *cm_node;
2996 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07002997 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002998
2999 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3000 if (!ibqp)
3001 return -EINVAL;
3002 nesqp = to_nesqp(ibqp);
3003 if (!nesqp)
3004 return -EINVAL;
3005 nesvnic = to_nesvnic(nesqp->ibqp.device);
3006 if (!nesvnic)
3007 return -EINVAL;
3008 nesdev = nesvnic->nesdev;
3009 if (!nesdev)
3010 return -EINVAL;
3011
Faisal Latifc5a7d482009-12-09 15:54:08 -08003012 if (!(cm_id->local_addr.sin_port) || !(cm_id->remote_addr.sin_port))
3013 return -EINVAL;
3014
Faisal Latif6492cdf2008-07-24 20:50:45 -07003015 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
3016 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3017 ntohl(nesvnic->local_ipaddr),
3018 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3019 ntohs(cm_id->remote_addr.sin_port),
3020 ntohl(cm_id->local_addr.sin_addr.s_addr),
3021 ntohs(cm_id->local_addr.sin_port));
3022
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003023 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003024 nesqp->active_conn = 1;
3025
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003026 /* cache the cm_id in the qp */
3027 nesqp->cm_id = cm_id;
3028
3029 cm_id->provider_data = nesqp;
3030
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003031 nesqp->private_data_len = conn_param->private_data_len;
3032 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
3033 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003034 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
3035 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003036
Faisal Latif6492cdf2008-07-24 20:50:45 -07003037 if (cm_id->local_addr.sin_addr.s_addr !=
Faisal Latif53094c32009-04-27 13:37:34 -07003038 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003039 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003040 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003041 apbvt_set = 1;
3042 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003043
3044 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003045 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3046 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3047 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3048 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003049 cm_info.cm_id = cm_id;
3050 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3051
3052 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003053
3054 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003055 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3056 conn_param->private_data_len, (void *)conn_param->private_data,
3057 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003058 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003059 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003060 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003061 PCI_FUNC(nesdev->pcidev->devfn),
3062 NES_MANAGE_APBVT_DEL);
3063
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003064 cm_id->rem_ref(cm_id);
3065 return -ENOMEM;
3066 }
3067
Faisal Latif53094c32009-04-27 13:37:34 -07003068 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003069 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003070 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003071 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003072
3073 return 0;
3074}
3075
3076
3077/**
3078 * nes_create_listen
3079 */
3080int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3081{
3082 struct nes_vnic *nesvnic;
3083 struct nes_cm_listener *cm_node;
3084 struct nes_cm_info cm_info;
3085 struct nes_adapter *adapter;
3086 int err;
3087
3088
3089 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3090 cm_id, ntohs(cm_id->local_addr.sin_port));
3091
3092 nesvnic = to_nesvnic(cm_id->device);
3093 if (!nesvnic)
3094 return -EINVAL;
3095 adapter = nesvnic->nesdev->nesadapter;
3096 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3097 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3098
3099 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3100 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3101
3102 /* setup listen params in our api call struct */
3103 cm_info.loc_addr = nesvnic->local_ipaddr;
3104 cm_info.loc_port = cm_id->local_addr.sin_port;
3105 cm_info.backlog = backlog;
3106 cm_info.cm_id = cm_id;
3107
3108 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3109
3110
3111 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3112 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003113 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003114 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003115 return -ENOMEM;
3116 }
3117
3118 cm_id->provider_data = cm_node;
3119
3120 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003121 err = nes_manage_apbvt(nesvnic,
3122 ntohs(cm_id->local_addr.sin_port),
3123 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3124 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003125 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003126 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3127 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003128 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3129 return err;
3130 }
3131 cm_listens_created++;
3132 }
3133
3134 cm_id->add_ref(cm_id);
3135 cm_id->provider_data = (void *)cm_node;
3136
3137
3138 return 0;
3139}
3140
3141
3142/**
3143 * nes_destroy_listen
3144 */
3145int nes_destroy_listen(struct iw_cm_id *cm_id)
3146{
3147 if (cm_id->provider_data)
3148 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3149 else
3150 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3151
3152 cm_id->rem_ref(cm_id);
3153
3154 return 0;
3155}
3156
3157
3158/**
3159 * nes_cm_recv
3160 */
3161int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3162{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003163 int rc = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003164 cm_packets_received++;
3165 if ((g_cm_core) && (g_cm_core->api)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003166 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003167 } else {
3168 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3169 " cm is not setup properly.\n");
3170 }
3171
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003172 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003173}
3174
3175
3176/**
3177 * nes_cm_start
3178 * Start and init a cm core module
3179 */
3180int nes_cm_start(void)
3181{
3182 nes_debug(NES_DBG_CM, "\n");
3183 /* create the primary CM core, pass this handle to subsequent core inits */
3184 g_cm_core = nes_cm_alloc_core();
3185 if (g_cm_core) {
3186 return 0;
3187 } else {
3188 return -ENOMEM;
3189 }
3190}
3191
3192
3193/**
3194 * nes_cm_stop
3195 * stop and dealloc all cm core instances
3196 */
3197int nes_cm_stop(void)
3198{
3199 g_cm_core->api->destroy_cm_core(g_cm_core);
3200 return 0;
3201}
3202
3203
3204/**
3205 * cm_event_connected
3206 * handle a connected event, setup QPs and HW
3207 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003208static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003209{
3210 u64 u64temp;
3211 struct nes_qp *nesqp;
3212 struct nes_vnic *nesvnic;
3213 struct nes_device *nesdev;
3214 struct nes_cm_node *cm_node;
3215 struct nes_adapter *nesadapter;
3216 struct ib_qp_attr attr;
3217 struct iw_cm_id *cm_id;
3218 struct iw_cm_event cm_event;
3219 struct nes_hw_qp_wqe *wqe;
3220 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003221 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003222 int ret;
3223
3224 /* get all our handles */
3225 cm_node = event->cm_node;
3226 cm_id = cm_node->cm_id;
3227 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3228 nesqp = (struct nes_qp *)cm_id->provider_data;
3229 nesvnic = to_nesvnic(nesqp->ibqp.device);
3230 nesdev = nesvnic->nesdev;
3231 nesadapter = nesdev->nesadapter;
3232
3233 if (nesqp->destroyed) {
3234 return;
3235 }
3236 atomic_inc(&cm_connecteds);
3237 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3238 " local port 0x%04X. jiffies = %lu.\n",
3239 nesqp->hwqp.qp_id,
3240 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3241 ntohs(cm_id->remote_addr.sin_port),
3242 ntohs(cm_id->local_addr.sin_port),
3243 jiffies);
3244
3245 nes_cm_init_tsa_conn(nesqp, cm_node);
3246
3247 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003248 nesqp->nesqp_context->tcpPorts[0] =
3249 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3250 nesqp->nesqp_context->tcpPorts[1] =
3251 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3252 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3253 nesqp->nesqp_context->ip0 =
3254 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3255 else
3256 nesqp->nesqp_context->ip0 =
3257 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003258
3259 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003260 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3261 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003262 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003263 nes_arp_table(nesdev,
3264 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003265 NULL, NES_ARP_RESOLVE) << 16);
3266 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3267 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3268 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3269 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003270 cpu_to_le32((u32)1 <<
3271 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003272
3273 /* Adjust tail for not having a LSMM */
3274 nesqp->hwqp.sq_tail = 1;
3275
3276#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003277 if (cm_node->send_write0) {
3278 nes_debug(NES_DBG_CM, "Sending first write.\n");
3279 wqe = &nesqp->hwqp.sq_vbase[0];
3280 u64temp = (unsigned long)nesqp;
3281 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3282 set_wqe_64bit_value(wqe->wqe_words,
3283 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3284 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3285 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3286 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3287 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3288 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3289 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3290 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003291
Faisal Latif6492cdf2008-07-24 20:50:45 -07003292 /* use the reserved spot on the WQ for the extra first WQE */
3293 nesqp->nesqp_context->ird_ord_sizes &=
3294 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3295 NES_QPCONTEXT_ORDIRD_WRPDU |
3296 NES_QPCONTEXT_ORDIRD_ALSMM));
3297 nesqp->skip_lsmm = 1;
3298 nesqp->hwqp.sq_tail = 0;
3299 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3300 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3301 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003302#endif
3303
3304 memset(&nes_quad, 0, sizeof(nes_quad));
3305
Faisal Latif6492cdf2008-07-24 20:50:45 -07003306 nes_quad.DstIpAdrIndex =
3307 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3308 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3309 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3310 else
3311 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003312 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3313 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3314
3315 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003316 crc_value = get_crc_value(&nes_quad);
3317 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003318 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3319 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3320
3321 nesqp->hte_index &= nesadapter->hte_index_mask;
3322 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3323
3324 nesqp->ietf_frame = &cm_node->mpa_frame;
3325 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3326 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3327
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003328 /* notify OF layer we successfully created the requested connection */
3329 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3330 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3331 cm_event.provider_data = cm_id->provider_data;
3332 cm_event.local_addr.sin_family = AF_INET;
3333 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3334 cm_event.remote_addr = cm_id->remote_addr;
3335
Faisal Latif6492cdf2008-07-24 20:50:45 -07003336 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3337 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003338
3339 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3340 ret = cm_id->event_handler(cm_id, &cm_event);
3341 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3342
3343 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003344 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3345 "ret=%d\n", __func__, __LINE__, ret);
3346 attr.qp_state = IB_QPS_RTS;
3347 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003348
Faisal Latif6492cdf2008-07-24 20:50:45 -07003349 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3350 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003351
3352 return;
3353}
3354
3355
3356/**
3357 * cm_event_connect_error
3358 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003359static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003360{
3361 struct nes_qp *nesqp;
3362 struct iw_cm_id *cm_id;
3363 struct iw_cm_event cm_event;
3364 /* struct nes_cm_info cm_info; */
3365 int ret;
3366
3367 if (!event->cm_node)
3368 return;
3369
3370 cm_id = event->cm_node->cm_id;
3371 if (!cm_id) {
3372 return;
3373 }
3374
3375 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3376 nesqp = cm_id->provider_data;
3377
3378 if (!nesqp) {
3379 return;
3380 }
3381
3382 /* notify OF layer about this connection error event */
3383 /* cm_id->rem_ref(cm_id); */
3384 nesqp->cm_id = NULL;
3385 cm_id->provider_data = NULL;
3386 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003387 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003388 cm_event.provider_data = cm_id->provider_data;
3389 cm_event.local_addr = cm_id->local_addr;
3390 cm_event.remote_addr = cm_id->remote_addr;
3391 cm_event.private_data = NULL;
3392 cm_event.private_data_len = 0;
3393
Faisal Latif6492cdf2008-07-24 20:50:45 -07003394 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3395 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3396 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003397
3398 ret = cm_id->event_handler(cm_id, &cm_event);
3399 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3400 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003401 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3402 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003403 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003404
Faisal Latif6492cdf2008-07-24 20:50:45 -07003405 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003406 return;
3407}
3408
3409
3410/**
3411 * cm_event_reset
3412 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003413static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003414{
3415 struct nes_qp *nesqp;
3416 struct iw_cm_id *cm_id;
3417 struct iw_cm_event cm_event;
3418 /* struct nes_cm_info cm_info; */
3419 int ret;
3420
3421 if (!event->cm_node)
3422 return;
3423
3424 if (!event->cm_node->cm_id)
3425 return;
3426
3427 cm_id = event->cm_node->cm_id;
3428
3429 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3430 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003431 if (!nesqp)
3432 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003433
3434 nesqp->cm_id = NULL;
3435 /* cm_id->provider_data = NULL; */
3436 cm_event.event = IW_CM_EVENT_DISCONNECT;
3437 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3438 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 Latif183ecfa2008-11-21 20:50:46 -06003444 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003445 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003446 atomic_inc(&cm_closes);
3447 cm_event.event = IW_CM_EVENT_CLOSE;
3448 cm_event.status = IW_CM_EVENT_STATUS_OK;
3449 cm_event.provider_data = cm_id->provider_data;
3450 cm_event.local_addr = cm_id->local_addr;
3451 cm_event.remote_addr = cm_id->remote_addr;
3452 cm_event.private_data = NULL;
3453 cm_event.private_data_len = 0;
3454 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3455 ret = cm_id->event_handler(cm_id, &cm_event);
3456
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003457 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3458
3459
3460 /* notify OF layer about this connection error event */
3461 cm_id->rem_ref(cm_id);
3462
3463 return;
3464}
3465
3466
3467/**
3468 * cm_event_mpa_req
3469 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003470static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003471{
3472 struct iw_cm_id *cm_id;
3473 struct iw_cm_event cm_event;
3474 int ret;
3475 struct nes_cm_node *cm_node;
3476
3477 cm_node = event->cm_node;
3478 if (!cm_node)
3479 return;
3480 cm_id = cm_node->cm_id;
3481
3482 atomic_inc(&cm_connect_reqs);
3483 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3484 cm_node, cm_id, jiffies);
3485
3486 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3487 cm_event.status = IW_CM_EVENT_STATUS_OK;
3488 cm_event.provider_data = (void *)cm_node;
3489
3490 cm_event.local_addr.sin_family = AF_INET;
3491 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3492 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3493
3494 cm_event.remote_addr.sin_family = AF_INET;
3495 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3496 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003497 cm_event.private_data = cm_node->mpa_frame_buf;
3498 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003499
3500 ret = cm_id->event_handler(cm_id, &cm_event);
3501 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003502 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3503 __func__, __LINE__, ret);
3504 return;
3505}
3506
3507
3508static void cm_event_mpa_reject(struct nes_cm_event *event)
3509{
3510 struct iw_cm_id *cm_id;
3511 struct iw_cm_event cm_event;
3512 struct nes_cm_node *cm_node;
3513 int ret;
3514
3515 cm_node = event->cm_node;
3516 if (!cm_node)
3517 return;
3518 cm_id = cm_node->cm_id;
3519
3520 atomic_inc(&cm_connect_reqs);
3521 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3522 cm_node, cm_id, jiffies);
3523
3524 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3525 cm_event.status = -ECONNREFUSED;
3526 cm_event.provider_data = cm_id->provider_data;
3527
3528 cm_event.local_addr.sin_family = AF_INET;
3529 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3530 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3531
3532 cm_event.remote_addr.sin_family = AF_INET;
3533 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3534 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3535
3536 cm_event.private_data = cm_node->mpa_frame_buf;
3537 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3538
3539 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3540 "remove_addr=%08x\n",
3541 cm_event.local_addr.sin_addr.s_addr,
3542 cm_event.remote_addr.sin_addr.s_addr);
3543
3544 ret = cm_id->event_handler(cm_id, &cm_event);
3545 if (ret)
3546 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003547 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003548
3549 return;
3550}
3551
3552
3553static void nes_cm_event_handler(struct work_struct *);
3554
3555/**
3556 * nes_cm_post_event
3557 * post an event to the cm event handler
3558 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003559static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003560{
3561 atomic_inc(&event->cm_node->cm_core->events_posted);
3562 add_ref_cm_node(event->cm_node);
3563 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3564 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003565 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3566 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003567
3568 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3569
3570 nes_debug(NES_DBG_CM, "Exit\n");
3571 return 0;
3572}
3573
3574
3575/**
3576 * nes_cm_event_handler
3577 * worker function to handle cm events
3578 * will free instance of nes_cm_event
3579 */
3580static void nes_cm_event_handler(struct work_struct *work)
3581{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003582 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3583 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003584 struct nes_cm_core *cm_core;
3585
Faisal Latif6492cdf2008-07-24 20:50:45 -07003586 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003587 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003588
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003589 cm_core = event->cm_node->cm_core;
3590 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003591 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003592
3593 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003594 case NES_CM_EVENT_MPA_REQ:
3595 cm_event_mpa_req(event);
3596 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3597 event->cm_node);
3598 break;
3599 case NES_CM_EVENT_RESET:
3600 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3601 event->cm_node);
3602 cm_event_reset(event);
3603 break;
3604 case NES_CM_EVENT_CONNECTED:
3605 if ((!event->cm_node->cm_id) ||
3606 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003607 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003608 cm_event_connected(event);
3609 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3610 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003611 case NES_CM_EVENT_MPA_REJECT:
3612 if ((!event->cm_node->cm_id) ||
3613 (event->cm_node->state == NES_CM_STATE_TSA))
3614 break;
3615 cm_event_mpa_reject(event);
3616 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3617 break;
3618
Faisal Latif6492cdf2008-07-24 20:50:45 -07003619 case NES_CM_EVENT_ABORTED:
3620 if ((!event->cm_node->cm_id) ||
3621 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003622 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003623 cm_event_connect_error(event);
3624 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3625 break;
3626 case NES_CM_EVENT_DROPPED_PKT:
3627 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3628 break;
3629 default:
3630 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3631 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003632 }
3633
3634 atomic_dec(&cm_core->events_posted);
3635 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3636 rem_ref_cm_node(cm_core, event->cm_node);
3637 kfree(event);
3638
3639 return;
3640}