blob: ae0946342a9ce5523204ca9536a647445684ef25 [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:
516 create_event(cm_node, NES_CM_EVENT_ABORTED);
517 }
518}
519
520static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
521{
522 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
523 struct iw_cm_id *cm_id = cm_node->cm_id;
524 struct nes_qp *nesqp;
525 unsigned long qplockflags;
526
527 if (!recv_entry)
528 return;
529 nesqp = (struct nes_qp *)recv_entry->skb;
530 if (nesqp) {
531 spin_lock_irqsave(&nesqp->lock, qplockflags);
532 if (nesqp->cm_id) {
533 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
534 "refcount = %d: HIT A "
535 "NES_TIMER_TYPE_CLOSE with something "
536 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
537 atomic_read(&nesqp->refcount));
538 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
539 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
540 nesqp->ibqp_state = IB_QPS_ERR;
541 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
542 nes_cm_disconn(nesqp);
543 } else {
544 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
545 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
546 "refcount = %d: HIT A "
547 "NES_TIMER_TYPE_CLOSE with nothing "
548 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
549 atomic_read(&nesqp->refcount));
550 }
551 } else if (rem_node) {
552 /* TIME_WAIT state */
553 rem_ref_cm_node(cm_node->cm_core, cm_node);
554 }
555 if (cm_node->cm_id)
556 cm_id->rem_ref(cm_id);
557 kfree(recv_entry);
558 cm_node->recv_entry = NULL;
559}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800560
561/**
562 * nes_cm_timer_tick
563 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700564static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800565{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800566 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800567 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800568 struct nes_cm_node *cm_node;
569 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800570 struct list_head *list_core_temp;
571 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800572 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800573 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700574 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800575 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800576
Faisal Latif879e5bd2008-11-21 20:50:41 -0600577 struct list_head timer_list;
578 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800579 spin_lock_irqsave(&cm_core->ht_lock, flags);
580
Faisal Latif6492cdf2008-07-24 20:50:45 -0700581 list_for_each_safe(list_node, list_core_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600582 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800583 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800584 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600585 add_ref_cm_node(cm_node);
586 list_add(&cm_node->timer_entry, &timer_list);
587 }
588 }
589 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
590
591 list_for_each_safe(list_node, list_core_temp, &timer_list) {
592 cm_node = container_of(list_node, struct nes_cm_node,
593 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800594 recv_entry = cm_node->recv_entry;
595
596 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700597 if (time_after(recv_entry->timetosend, jiffies)) {
598 if (nexttimeout > recv_entry->timetosend ||
Faisal Latif9d5ab132009-03-06 15:15:01 -0800599 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800600 nexttimeout = recv_entry->timetosend;
601 settimer = 1;
602 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800603 } else
604 handle_recv_entry(cm_node, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800605 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800606
607 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700608 do {
609 send_entry = cm_node->send_entry;
610 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600611 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800612 if (time_after(send_entry->timetosend, jiffies)) {
613 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700614 if ((nexttimeout >
615 send_entry->timetosend) ||
616 !settimer) {
617 nexttimeout =
618 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800619 settimer = 1;
620 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800621 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700622 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800623 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800624 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800625 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700626
627 if ((cm_node->state == NES_CM_STATE_TSA) ||
628 (cm_node->state == NES_CM_STATE_CLOSED)) {
629 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600630 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800631 }
632
Faisal Latif6492cdf2008-07-24 20:50:45 -0700633 if (!send_entry->retranscount ||
634 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800635 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700636 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800637
Faisal Latif6492cdf2008-07-24 20:50:45 -0700638 spin_unlock_irqrestore(
639 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800640 nes_retrans_expired(cm_node);
641 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700642 spin_lock_irqsave(&cm_node->retrans_list_lock,
643 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600644 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800645 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800646 atomic_inc(&send_entry->skb->users);
647 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700648 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
649 "for node %p, jiffies = %lu, time to send = "
650 "%lu, retranscount = %u, send_entry->seq_num = "
651 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
652 "0x%08X\n", send_entry, cm_node, jiffies,
653 send_entry->timetosend,
654 send_entry->retranscount,
655 send_entry->seq_num,
656 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800657
Faisal Latif6492cdf2008-07-24 20:50:45 -0700658 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
659 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800660 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700661 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800662 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700663 nes_debug(NES_DBG_CM, "rexmit failed for "
664 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800665 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800666 send_entry->retrycount--;
667 nexttimeout = jiffies + NES_SHORT_TIME;
668 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600669 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800670 } else {
671 cm_packets_sent++;
672 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700673 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
674 "%u, retry count = %u.\n",
675 send_entry->retranscount,
676 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800677 if (send_entry->send_retrans) {
678 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700679 timetosend = (NES_RETRY_TIMEOUT <<
680 (NES_DEFAULT_RETRANS - send_entry->retranscount));
681
Faisal Latif6492cdf2008-07-24 20:50:45 -0700682 send_entry->timetosend = jiffies +
Faisal Latif4e9c3902009-04-27 13:39:36 -0700683 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700684 if (nexttimeout > send_entry->timetosend ||
685 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800686 nexttimeout = send_entry->timetosend;
687 settimer = 1;
688 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800689 } else {
690 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700691 close_when_complete =
692 send_entry->close_when_complete;
693 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
694 cm_node, cm_node->state);
695 free_retrans_entry(cm_node);
696 if (close_when_complete)
697 rem_ref_cm_node(cm_node->cm_core,
698 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800699 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700700 } while (0);
701
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800702 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700703 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800704 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800705
706 if (settimer) {
707 if (!timer_pending(&cm_core->tcp_timer)) {
708 cm_core->tcp_timer.expires = nexttimeout;
709 add_timer(&cm_core->tcp_timer);
710 }
711 }
712}
713
714
715/**
716 * send_syn
717 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700718static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
719 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800720{
721 int ret;
722 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800723 char optionsbuffer[sizeof(struct option_mss) +
Faisal Latif6492cdf2008-07-24 20:50:45 -0700724 sizeof(struct option_windowscale) + sizeof(struct option_base) +
725 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800726
727 int optionssize = 0;
728 /* Sending MSS option */
729 union all_known_options *options;
730
731 if (!cm_node)
732 return -EINVAL;
733
734 options = (union all_known_options *)&optionsbuffer[optionssize];
735 options->as_mss.optionnum = OPTION_NUMBER_MSS;
736 options->as_mss.length = sizeof(struct option_mss);
737 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
738 optionssize += sizeof(struct option_mss);
739
740 options = (union all_known_options *)&optionsbuffer[optionssize];
741 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
742 options->as_windowscale.length = sizeof(struct option_windowscale);
743 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
744 optionssize += sizeof(struct option_windowscale);
745
Faisal Latif6492cdf2008-07-24 20:50:45 -0700746 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800747 options = (union all_known_options *)&optionsbuffer[optionssize];
748 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
749 options->as_base.length = sizeof(struct option_base);
750 optionssize += sizeof(struct option_base);
751 /* we need the size to be a multiple of 4 */
752 options = (union all_known_options *)&optionsbuffer[optionssize];
753 options->as_end = 1;
754 optionssize += 1;
755 options = (union all_known_options *)&optionsbuffer[optionssize];
756 options->as_end = 1;
757 optionssize += 1;
758 }
759
760 options = (union all_known_options *)&optionsbuffer[optionssize];
761 options->as_end = OPTION_NUMBER_END;
762 optionssize += 1;
763
Faisal Latif6492cdf2008-07-24 20:50:45 -0700764 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800765 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800766 if (!skb) {
767 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
768 return -1;
769 }
770
771 if (sendack)
772 flags |= SET_ACK;
773
774 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
775 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
776
777 return ret;
778}
779
780
781/**
782 * send_reset
783 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700784static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800785{
786 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800787 int flags = SET_RST | SET_ACK;
788
Faisal Latif6492cdf2008-07-24 20:50:45 -0700789 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800790 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800791 if (!skb) {
792 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -0800793 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800794 }
795
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800796 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
797 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
798
799 return ret;
800}
801
802
803/**
804 * send_ack
805 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700806static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800807{
808 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700809
810 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800811 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800812
813 if (!skb) {
814 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
815 return -1;
816 }
817
818 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
819 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
820
821 return ret;
822}
823
824
825/**
826 * send_fin
827 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700828static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800829{
830 int ret;
831
832 /* if we didn't get a frame get one */
833 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800834 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800835
836 if (!skb) {
837 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
838 return -1;
839 }
840
841 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
842 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
843
844 return ret;
845}
846
847
848/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800849 * find_node - find a cm node that matches the reference cm node
850 */
851static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
852 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
853{
854 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800855 struct list_head *hte;
856 struct nes_cm_node *cm_node;
857
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800858 /* get a handle on the hte */
859 hte = &cm_core->connected_nodes;
860
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800861 /* walk list and find cm_node associated with this session ID */
862 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700863 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800864 /* compare quad, return node handle if a match */
865 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
866 cm_node->loc_addr, cm_node->loc_port,
867 loc_addr, loc_port,
868 cm_node->rem_addr, cm_node->rem_port,
869 rem_addr, rem_port);
870 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
871 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
872 add_ref_cm_node(cm_node);
873 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
874 return cm_node;
875 }
876 }
877 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
878
879 /* no owner node */
880 return NULL;
881}
882
883
884/**
885 * find_listener - find a cm node listening on this addr-port pair
886 */
887static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
888 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
889{
890 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800891 struct nes_cm_listener *listen_node;
892
893 /* walk list and find cm_node associated with this session ID */
894 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700895 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800896 /* compare node pair, return node handle if a match */
897 if (((listen_node->loc_addr == dst_addr) ||
898 listen_node->loc_addr == 0x00000000) &&
899 (listen_node->loc_port == dst_port) &&
900 (listener_state & listen_node->listener_state)) {
901 atomic_inc(&listen_node->ref_count);
902 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
903 return listen_node;
904 }
905 }
906 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
907
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800908 /* no listener */
909 return NULL;
910}
911
912
913/**
914 * add_hte_node - add a cm node to the hash table
915 */
916static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
917{
918 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800919 struct list_head *hte;
920
921 if (!cm_node || !cm_core)
922 return -EINVAL;
923
Faisal Latif6492cdf2008-07-24 20:50:45 -0700924 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
925 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800926
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800927 spin_lock_irqsave(&cm_core->ht_lock, flags);
928
929 /* get a handle on the hash table element (list head for this slot) */
930 hte = &cm_core->connected_nodes;
931 list_add_tail(&cm_node->list, hte);
932 atomic_inc(&cm_core->ht_node_cnt);
933
934 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
935
936 return 0;
937}
938
939
940/**
941 * mini_cm_dec_refcnt_listen
942 */
943static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700944 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800945{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800946 int ret = -EINVAL;
947 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800948 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700949 struct list_head *list_pos = NULL;
950 struct list_head *list_temp = NULL;
951 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -0600952 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700953
954 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
955 "refcnt=%d\n", listener, free_hanging_nodes,
956 atomic_read(&listener->ref_count));
957 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -0600958 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700959 if (free_hanging_nodes) {
960 spin_lock_irqsave(&cm_core->ht_lock, flags);
961 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600962 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700963 cm_node = container_of(list_pos, struct nes_cm_node,
964 list);
965 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -0600966 (!cm_node->accelerated)) {
967 add_ref_cm_node(cm_node);
968 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700969 }
970 }
971 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
972 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600973
974 list_for_each_safe(list_pos, list_temp, &reset_list) {
975 cm_node = container_of(list_pos, struct nes_cm_node,
Faisal Latif9d5ab132009-03-06 15:15:01 -0800976 reset_entry);
977 {
978 struct nes_cm_node *loopback = cm_node->loopbackpartner;
979 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
980 rem_ref_cm_node(cm_node->cm_core, cm_node);
981 } else {
982 if (!loopback) {
983 cleanup_retrans_entry(cm_node);
984 err = send_reset(cm_node, NULL);
985 if (err) {
986 cm_node->state =
987 NES_CM_STATE_CLOSED;
988 WARN_ON(1);
989 } else {
990 cm_node->state =
991 NES_CM_STATE_CLOSED;
992 rem_ref_cm_node(
993 cm_node->cm_core,
994 cm_node);
995 }
996 } else {
997 struct nes_cm_event event;
998
999 event.cm_node = loopback;
1000 event.cm_info.rem_addr =
1001 loopback->rem_addr;
1002 event.cm_info.loc_addr =
1003 loopback->loc_addr;
1004 event.cm_info.rem_port =
1005 loopback->rem_port;
1006 event.cm_info.loc_port =
1007 loopback->loc_port;
1008 event.cm_info.cm_id = loopback->cm_id;
1009 cm_event_connect_error(&event);
1010 loopback->state = NES_CM_STATE_CLOSED;
1011
1012 event.cm_node = cm_node;
1013 event.cm_info.rem_addr =
1014 cm_node->rem_addr;
1015 event.cm_info.loc_addr =
1016 cm_node->loc_addr;
1017 event.cm_info.rem_port =
1018 cm_node->rem_port;
1019 event.cm_info.loc_port =
1020 cm_node->loc_port;
1021 event.cm_info.cm_id = cm_node->cm_id;
1022 cm_event_reset(&event);
1023
1024 rem_ref_cm_node(cm_node->cm_core,
1025 cm_node);
1026
1027 }
1028 }
1029 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001030 }
1031
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001032 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1033 if (!atomic_dec_return(&listener->ref_count)) {
1034 list_del(&listener->list);
1035
1036 /* decrement our listen node count */
1037 atomic_dec(&cm_core->listen_node_cnt);
1038
1039 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1040
1041 if (listener->nesvnic) {
1042 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1043 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1044 }
1045
1046 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1047
1048 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001049 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001050 ret = 0;
1051 cm_listens_destroyed++;
1052 } else {
1053 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1054 }
1055 if (listener) {
1056 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1057 nes_debug(NES_DBG_CM, "destroying listener (%p)"
1058 " with non-zero pending accepts=%u\n",
1059 listener, atomic_read(&listener->pend_accepts_cnt));
1060 }
1061
1062 return ret;
1063}
1064
1065
1066/**
1067 * mini_cm_del_listen
1068 */
1069static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1070 struct nes_cm_listener *listener)
1071{
1072 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1073 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1074 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1075}
1076
1077
1078/**
1079 * mini_cm_accelerated
1080 */
1081static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1082 struct nes_cm_node *cm_node)
1083{
1084 u32 was_timer_set;
1085 cm_node->accelerated = 1;
1086
1087 if (cm_node->accept_pend) {
1088 BUG_ON(!cm_node->listener);
1089 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001090 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001091 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1092 }
1093
1094 was_timer_set = timer_pending(&cm_core->tcp_timer);
1095 if (!was_timer_set) {
1096 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1097 add_timer(&cm_core->tcp_timer);
1098 }
1099
1100 return 0;
1101}
1102
1103
1104/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001105 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001106 */
Bob Sharp7191a0a2008-10-03 12:21:19 -07001107static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001108{
1109 struct rtable *rt;
1110 struct flowi fl;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001111 struct neighbour *neigh;
1112 int rc = -1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001113
1114 memset(&fl, 0, sizeof fl);
1115 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1116 if (ip_route_output_key(&init_net, &rt, &fl)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001117 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001118 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001119 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001120 }
1121
Bob Sharp7191a0a2008-10-03 12:21:19 -07001122 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1123 if (neigh) {
1124 if (neigh->nud_state & NUD_VALID) {
1125 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001126 " is %pM, Gateway is 0x%08X \n", dst_ip,
1127 neigh->ha, ntohl(rt->rt_gateway));
Bob Sharp7191a0a2008-10-03 12:21:19 -07001128 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1129 dst_ip, NES_ARP_ADD);
1130 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1131 NES_ARP_RESOLVE);
1132 }
1133 neigh_release(neigh);
1134 }
1135
1136 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1137 neigh_event_send(rt->u.dst.neighbour, NULL);
1138
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001139 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001140 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001141}
1142
1143
1144/**
1145 * make_cm_node - create a new instance of a cm node
1146 */
1147static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1148 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1149 struct nes_cm_listener *listener)
1150{
1151 struct nes_cm_node *cm_node;
1152 struct timespec ts;
1153 int arpindex = 0;
1154 struct nes_device *nesdev;
1155 struct nes_adapter *nesadapter;
1156
1157 /* create an hte and cm_node for this instance */
1158 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1159 if (!cm_node)
1160 return NULL;
1161
1162 /* set our node specific transport info */
1163 cm_node->loc_addr = cm_info->loc_addr;
1164 cm_node->rem_addr = cm_info->rem_addr;
1165 cm_node->loc_port = cm_info->loc_port;
1166 cm_node->rem_port = cm_info->rem_port;
1167 cm_node->send_write0 = send_first;
Harvey Harrison63779432008-10-31 00:56:00 -07001168 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1169 &cm_node->loc_addr, cm_node->loc_port,
1170 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001171 cm_node->listener = listener;
1172 cm_node->netdev = nesvnic->netdev;
1173 cm_node->cm_id = cm_info->cm_id;
1174 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1175
Faisal Latif6492cdf2008-07-24 20:50:45 -07001176 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1177 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001178
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001179 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001180
1181 cm_node->loopbackpartner = NULL;
1182 atomic_set(&cm_node->ref_count, 1);
1183 /* associate our parent CM core */
1184 cm_node->cm_core = cm_core;
1185 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1186 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1187 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1188 NES_CM_DEFAULT_RCV_WND_SCALE;
1189 ts = current_kernel_time();
1190 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1191 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Chien Tungf2b2b592008-03-20 19:55:30 -05001192 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001193 cm_node->tcp_cntxt.rcv_nxt = 0;
1194 /* get a unique session ID , add thread_id to an upcounter to handle race */
1195 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001196 cm_node->conn_type = cm_info->conn_type;
1197 cm_node->apbvt_set = 0;
1198 cm_node->accept_pend = 0;
1199
1200 cm_node->nesvnic = nesvnic;
1201 /* get some device handles, for arp lookup */
1202 nesdev = nesvnic->nesdev;
1203 nesadapter = nesdev->nesadapter;
1204
1205 cm_node->loopbackpartner = NULL;
1206 /* get the mac addr for the remote node */
Chien Tung1ee86552008-11-21 20:51:04 -06001207 if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1208 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
1209 else
1210 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001211 if (arpindex < 0) {
Bob Sharp7191a0a2008-10-03 12:21:19 -07001212 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr);
1213 if (arpindex < 0) {
1214 kfree(cm_node);
1215 return NULL;
1216 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001217 }
1218
1219 /* copy the mac addr to node context */
1220 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001221 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1222 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001223
1224 add_hte_node(cm_core, cm_node);
1225 atomic_inc(&cm_nodes_created);
1226
1227 return cm_node;
1228}
1229
1230
1231/**
1232 * add_ref_cm_node - destroy an instance of a cm node
1233 */
1234static int add_ref_cm_node(struct nes_cm_node *cm_node)
1235{
1236 atomic_inc(&cm_node->ref_count);
1237 return 0;
1238}
1239
1240
1241/**
1242 * rem_ref_cm_node - destroy an instance of a cm node
1243 */
1244static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001245 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001246{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001247 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001248 struct nes_qp *nesqp;
1249
1250 if (!cm_node)
1251 return -EINVAL;
1252
1253 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1254 if (atomic_dec_return(&cm_node->ref_count)) {
1255 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1256 return 0;
1257 }
1258 list_del(&cm_node->list);
1259 atomic_dec(&cm_core->ht_node_cnt);
1260 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1261
1262 /* if the node is destroyed before connection was accelerated */
1263 if (!cm_node->accelerated && cm_node->accept_pend) {
1264 BUG_ON(!cm_node->listener);
1265 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1266 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1267 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001268 WARN_ON(cm_node->send_entry);
1269 if (cm_node->recv_entry)
1270 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001271 if (cm_node->listener) {
1272 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1273 } else {
1274 if (cm_node->apbvt_set && cm_node->nesvnic) {
1275 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001276 PCI_FUNC(
1277 cm_node->nesvnic->nesdev->pcidev->devfn),
1278 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001279 }
1280 }
1281
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001282 atomic_dec(&cm_core->node_cnt);
1283 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001284 nesqp = cm_node->nesqp;
1285 if (nesqp) {
1286 nesqp->cm_node = NULL;
1287 nes_rem_ref(&nesqp->ibqp);
1288 cm_node->nesqp = NULL;
1289 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001290
Faisal Latif6492cdf2008-07-24 20:50:45 -07001291 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001292 return 0;
1293}
1294
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001295/**
1296 * process_options
1297 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001298static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1299 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001300{
1301 u32 tmp;
1302 u32 offset = 0;
1303 union all_known_options *all_options;
1304 char got_mss_option = 0;
1305
1306 while (offset < optionsize) {
1307 all_options = (union all_known_options *)(optionsloc + offset);
1308 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001309 case OPTION_NUMBER_END:
1310 offset = optionsize;
1311 break;
1312 case OPTION_NUMBER_NONE:
1313 offset += 1;
1314 continue;
1315 case OPTION_NUMBER_MSS:
1316 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1317 "Size: %d\n", __func__,
1318 all_options->as_mss.length, offset, optionsize);
1319 got_mss_option = 1;
1320 if (all_options->as_mss.length != 4) {
1321 return 1;
1322 } else {
1323 tmp = ntohs(all_options->as_mss.mss);
1324 if (tmp > 0 && tmp <
1325 cm_node->tcp_cntxt.mss)
1326 cm_node->tcp_cntxt.mss = tmp;
1327 }
1328 break;
1329 case OPTION_NUMBER_WINDOW_SCALE:
1330 cm_node->tcp_cntxt.snd_wscale =
1331 all_options->as_windowscale.shiftcount;
1332 break;
1333 case OPTION_NUMBER_WRITE0:
1334 cm_node->send_write0 = 1;
1335 break;
1336 default:
1337 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1338 all_options->as_base.optionnum);
1339 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001340 }
1341 offset += all_options->as_base.length;
1342 }
1343 if ((!got_mss_option) && (syn_packet))
1344 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1345 return 0;
1346}
1347
Faisal Latif6492cdf2008-07-24 20:50:45 -07001348static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001349{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001350 atomic_inc(&cm_accel_dropped_pkts);
1351 dev_kfree_skb_any(skb);
1352}
1353
Faisal Latif9d5ab132009-03-06 15:15:01 -08001354static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001355{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001356 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1357 "refcnt=%d\n", cm_node, cm_node->state,
1358 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001359 switch (cm_node->state) {
1360 case NES_CM_STATE_SYN_RCVD:
1361 case NES_CM_STATE_SYN_SENT:
1362 case NES_CM_STATE_ESTABLISHED:
1363 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001364 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001365 cm_node->tcp_cntxt.rcv_nxt++;
1366 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001367 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001368 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001369 break;
1370 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001371 cm_node->tcp_cntxt.rcv_nxt++;
1372 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001373 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001374 send_ack(cm_node, NULL);
1375 /* Wait for ACK as this is simultanous close..
1376 * After we receive ACK, do not send anything..
1377 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001378 break;
1379 case NES_CM_STATE_FIN_WAIT2:
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_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001383 send_ack(cm_node, NULL);
1384 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1385 break;
1386 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001387 cm_node->tcp_cntxt.rcv_nxt++;
1388 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001389 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001390 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001391 break;
1392 case NES_CM_STATE_TSA:
1393 default:
1394 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1395 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001396 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001397 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001398}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001399
Faisal Latif6492cdf2008-07-24 20:50:45 -07001400
1401static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1402 struct tcphdr *tcph)
1403{
1404
1405 int reset = 0; /* whether to send reset in case of err.. */
Faisal Latif183ecfa2008-11-21 20:50:46 -06001406 int passive_state;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001407 atomic_inc(&cm_resets_recvd);
1408 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1409 " refcnt=%d\n", cm_node, cm_node->state,
1410 atomic_read(&cm_node->ref_count));
1411 cleanup_retrans_entry(cm_node);
1412 switch (cm_node->state) {
1413 case NES_CM_STATE_SYN_SENT:
1414 case NES_CM_STATE_MPAREQ_SENT:
1415 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1416 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1417 cm_node->listener, cm_node->state);
1418 active_open_err(cm_node, skb, reset);
1419 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001420 case NES_CM_STATE_MPAREQ_RCVD:
1421 passive_state = atomic_add_return(1, &cm_node->passive_state);
1422 if (passive_state == NES_SEND_RESET_EVENT)
1423 create_event(cm_node, NES_CM_EVENT_RESET);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001424 cm_node->state = NES_CM_STATE_CLOSED;
1425 dev_kfree_skb_any(skb);
1426 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001427 case NES_CM_STATE_ESTABLISHED:
1428 case NES_CM_STATE_SYN_RCVD:
1429 case NES_CM_STATE_LISTENING:
1430 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1431 passive_open_err(cm_node, skb, reset);
1432 break;
1433 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001434 active_open_err(cm_node, skb, reset);
1435 break;
1436 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001437 drop_packet(skb);
1438 break;
Faisal Latif68237a02009-06-22 22:53:28 -07001439 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001440 case NES_CM_STATE_LAST_ACK:
1441 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001442 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001443 cm_node->state = NES_CM_STATE_CLOSED;
1444 rem_ref_cm_node(cm_node->cm_core, cm_node);
1445 drop_packet(skb);
1446 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001447 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001448 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001449 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001450 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001451}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001452
Faisal Latif9d5ab132009-03-06 15:15:01 -08001453
1454static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001455{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001456
Faisal Latif9d5ab132009-03-06 15:15:01 -08001457 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001458 int datasize = skb->len;
1459 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001460
1461 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1462 u32 res_type;
1463 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1464 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001465 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001466 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001467 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1468 "cm_node=%p listener=%p state=%d\n", __func__,
1469 __LINE__, cm_node, cm_node->listener,
1470 cm_node->state);
1471 active_open_err(cm_node, skb, 1);
1472 } else {
1473 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001474 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001475 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001476 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001477
1478 switch (cm_node->state) {
1479 case NES_CM_STATE_ESTABLISHED:
1480 if (res_type == NES_MPA_REQUEST_REJECT) {
1481 /*BIG problem as we are receiving the MPA.. So should
1482 * not be REJECT.. This is Passive Open.. We can
1483 * only receive it Reject for Active Open...*/
1484 WARN_ON(1);
1485 }
1486 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1487 type = NES_CM_EVENT_MPA_REQ;
1488 atomic_set(&cm_node->passive_state,
1489 NES_PASSIVE_STATE_INDICATED);
1490 break;
1491 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001492 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001493 if (res_type == NES_MPA_REQUEST_REJECT) {
1494 type = NES_CM_EVENT_MPA_REJECT;
1495 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1496 } else {
1497 type = NES_CM_EVENT_CONNECTED;
1498 cm_node->state = NES_CM_STATE_TSA;
1499 }
1500
1501 break;
1502 default:
1503 WARN_ON(1);
1504 break;
1505 }
1506 dev_kfree_skb_any(skb);
1507 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001508}
1509
1510static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1511{
1512 switch (cm_node->state) {
1513 case NES_CM_STATE_SYN_SENT:
1514 case NES_CM_STATE_MPAREQ_SENT:
1515 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1516 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1517 cm_node->listener, cm_node->state);
1518 active_open_err(cm_node, skb, 1);
1519 break;
1520 case NES_CM_STATE_ESTABLISHED:
1521 case NES_CM_STATE_SYN_RCVD:
1522 passive_open_err(cm_node, skb, 1);
1523 break;
1524 case NES_CM_STATE_TSA:
1525 default:
1526 drop_packet(skb);
1527 }
1528}
1529
1530static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1531 struct sk_buff *skb)
1532{
1533 int err;
1534
1535 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1536 if (err)
1537 active_open_err(cm_node, skb, 1);
1538
1539 return err;
1540}
1541
1542static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1543 struct sk_buff *skb)
1544{
1545 int err = 0;
1546 u32 seq;
1547 u32 ack_seq;
1548 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1549 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1550 u32 rcv_wnd;
1551 seq = ntohl(tcph->seq);
1552 ack_seq = ntohl(tcph->ack_seq);
1553 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1554 if (ack_seq != loc_seq_num)
1555 err = 1;
Faisal Latifc11470f2009-04-27 13:38:31 -07001556 else if (!between(seq, rcv_nxt, (rcv_nxt+rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001557 err = 1;
1558 if (err) {
1559 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1560 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1561 cm_node->listener, cm_node->state);
1562 indicate_pkt_err(cm_node, skb);
1563 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1564 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1565 rcv_wnd);
1566 }
1567 return err;
1568}
1569
1570/*
1571 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1572 * is created with a listener or it may comein as rexmitted packet which in
1573 * that case will be just dropped.
1574 */
1575
1576static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1577 struct tcphdr *tcph)
1578{
1579 int ret;
1580 u32 inc_sequence;
1581 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001582
1583 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001584 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001585 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001586
Faisal Latif6492cdf2008-07-24 20:50:45 -07001587 switch (cm_node->state) {
1588 case NES_CM_STATE_SYN_SENT:
1589 case NES_CM_STATE_MPAREQ_SENT:
1590 /* Rcvd syn on active open connection*/
1591 active_open_err(cm_node, skb, 1);
1592 break;
1593 case NES_CM_STATE_LISTENING:
1594 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001595 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1596 cm_node->listener->backlog) {
1597 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1598 "pressure \n");
1599 cm_backlog_drops++;
1600 passive_open_err(cm_node, skb, 0);
1601 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001602 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001603 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1604 1);
1605 if (ret) {
1606 passive_open_err(cm_node, skb, 0);
1607 /* drop pkt */
1608 break;
1609 }
1610 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1611 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001612 cm_node->accept_pend = 1;
1613 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1614
Faisal Latif6492cdf2008-07-24 20:50:45 -07001615 cm_node->state = NES_CM_STATE_SYN_RCVD;
1616 send_syn(cm_node, 1, skb);
1617 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001618 case NES_CM_STATE_CLOSED:
1619 cleanup_retrans_entry(cm_node);
1620 send_reset(cm_node, skb);
1621 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001622 case NES_CM_STATE_TSA:
1623 case NES_CM_STATE_ESTABLISHED:
1624 case NES_CM_STATE_FIN_WAIT1:
1625 case NES_CM_STATE_FIN_WAIT2:
1626 case NES_CM_STATE_MPAREQ_RCVD:
1627 case NES_CM_STATE_LAST_ACK:
1628 case NES_CM_STATE_CLOSING:
1629 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001630 default:
1631 drop_packet(skb);
1632 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001633 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001634}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001635
Faisal Latif6492cdf2008-07-24 20:50:45 -07001636static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1637 struct tcphdr *tcph)
1638{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001639
Faisal Latif6492cdf2008-07-24 20:50:45 -07001640 int ret;
1641 u32 inc_sequence;
1642 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001643
Faisal Latif6492cdf2008-07-24 20:50:45 -07001644 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001645 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001646 inc_sequence = ntohl(tcph->seq);
1647 switch (cm_node->state) {
1648 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001649 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001650 /* active open */
1651 if (check_syn(cm_node, tcph, skb))
1652 return;
1653 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1654 /* setup options */
1655 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1656 if (ret) {
1657 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1658 cm_node);
1659 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001660 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001661 cleanup_retrans_entry(cm_node);
1662 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1663 send_mpa_request(cm_node, skb);
1664 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1665 break;
1666 case NES_CM_STATE_MPAREQ_RCVD:
1667 /* passive open, so should not be here */
1668 passive_open_err(cm_node, skb, 1);
1669 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001670 case NES_CM_STATE_LISTENING:
1671 case NES_CM_STATE_CLOSED:
1672 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1673 cleanup_retrans_entry(cm_node);
1674 send_reset(cm_node, skb);
1675 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001676 case NES_CM_STATE_ESTABLISHED:
1677 case NES_CM_STATE_FIN_WAIT1:
1678 case NES_CM_STATE_FIN_WAIT2:
1679 case NES_CM_STATE_LAST_ACK:
1680 case NES_CM_STATE_TSA:
1681 case NES_CM_STATE_CLOSING:
1682 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001683 case NES_CM_STATE_MPAREQ_SENT:
1684 default:
1685 drop_packet(skb);
1686 break;
1687 }
1688}
1689
Faisal Latif109d67e2009-04-27 13:41:06 -07001690static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001691 struct tcphdr *tcph)
1692{
1693 int datasize = 0;
1694 u32 inc_sequence;
1695 u32 rem_seq_ack;
1696 u32 rem_seq;
Faisal Latif109d67e2009-04-27 13:41:06 -07001697 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06001698 int optionsize;
Faisal Latifabb77252008-11-21 20:50:52 -06001699 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001700
Faisal Latif6492cdf2008-07-24 20:50:45 -07001701 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07001702 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001703
1704 skb_pull(skb, tcph->doff << 2);
1705 inc_sequence = ntohl(tcph->seq);
1706 rem_seq = ntohl(tcph->seq);
1707 rem_seq_ack = ntohl(tcph->ack_seq);
1708 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001709 switch (cm_node->state) {
1710 case NES_CM_STATE_SYN_RCVD:
1711 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07001712 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001713 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1714 if (ret)
1715 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001716 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1717 cm_node->state = NES_CM_STATE_ESTABLISHED;
1718 if (datasize) {
1719 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001720 handle_rcv_mpa(cm_node, skb);
Faisal Latif109d67e2009-04-27 13:41:06 -07001721 } else /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001722 dev_kfree_skb_any(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001723 break;
1724 case NES_CM_STATE_ESTABLISHED:
1725 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001726 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001727 if (datasize) {
1728 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001729 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001730 } else
1731 drop_packet(skb);
1732 break;
1733 case NES_CM_STATE_MPAREQ_SENT:
1734 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1735 if (datasize) {
1736 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001737 handle_rcv_mpa(cm_node, skb);
Faisal Latif109d67e2009-04-27 13:41:06 -07001738 } else /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001739 dev_kfree_skb_any(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001740 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001741 case NES_CM_STATE_LISTENING:
1742 case NES_CM_STATE_CLOSED:
1743 cleanup_retrans_entry(cm_node);
1744 send_reset(cm_node, skb);
1745 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001746 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07001747 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001748 cleanup_retrans_entry(cm_node);
1749 cm_node->state = NES_CM_STATE_CLOSED;
1750 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001751 rem_ref_cm_node(cm_node->cm_core, cm_node);
1752 drop_packet(skb);
1753 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001754 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001755 cleanup_retrans_entry(cm_node);
1756 drop_packet(skb);
1757 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1758 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001759 case NES_CM_STATE_SYN_SENT:
1760 case NES_CM_STATE_FIN_WAIT2:
1761 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001762 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001763 case NES_CM_STATE_UNKNOWN:
1764 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07001765 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001766 drop_packet(skb);
1767 break;
1768 }
Faisal Latif109d67e2009-04-27 13:41:06 -07001769 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001770}
1771
1772
1773
1774static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1775 struct sk_buff *skb, int optionsize, int passive)
1776{
1777 u8 *optionsloc = (u8 *)&tcph[1];
1778 if (optionsize) {
1779 if (process_options(cm_node, optionsloc, optionsize,
1780 (u32)tcph->syn)) {
1781 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1782 __func__, cm_node);
1783 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06001784 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001785 else
Faisal Latifabb77252008-11-21 20:50:52 -06001786 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001787 return 1;
1788 }
1789 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001790
1791 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1792 cm_node->tcp_cntxt.snd_wscale;
1793
Faisal Latif6492cdf2008-07-24 20:50:45 -07001794 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001795 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001796 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001797}
1798
Faisal Latif6492cdf2008-07-24 20:50:45 -07001799/*
1800 * active_open_err() will send reset() if flag set..
1801 * It will also send ABORT event.
1802 */
1803
1804static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1805 int reset)
1806{
1807 cleanup_retrans_entry(cm_node);
1808 if (reset) {
1809 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1810 "state=%d\n", cm_node, cm_node->state);
1811 add_ref_cm_node(cm_node);
1812 send_reset(cm_node, skb);
1813 } else
1814 dev_kfree_skb_any(skb);
1815
1816 cm_node->state = NES_CM_STATE_CLOSED;
1817 create_event(cm_node, NES_CM_EVENT_ABORTED);
1818}
1819
1820/*
1821 * passive_open_err() will either do a reset() or will free up the skb and
1822 * remove the cm_node.
1823 */
1824
1825static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1826 int reset)
1827{
1828 cleanup_retrans_entry(cm_node);
1829 cm_node->state = NES_CM_STATE_CLOSED;
1830 if (reset) {
1831 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1832 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1833 send_reset(cm_node, skb);
1834 } else {
1835 dev_kfree_skb_any(skb);
1836 rem_ref_cm_node(cm_node->cm_core, cm_node);
1837 }
1838}
1839
1840/*
1841 * free_retrans_entry() routines assumes that the retrans_list_lock has
1842 * been acquired before calling.
1843 */
1844static void free_retrans_entry(struct nes_cm_node *cm_node)
1845{
1846 struct nes_timer_entry *send_entry;
1847 send_entry = cm_node->send_entry;
1848 if (send_entry) {
1849 cm_node->send_entry = NULL;
1850 dev_kfree_skb_any(send_entry->skb);
1851 kfree(send_entry);
1852 rem_ref_cm_node(cm_node->cm_core, cm_node);
1853 }
1854}
1855
1856static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1857{
1858 unsigned long flags;
1859
1860 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1861 free_retrans_entry(cm_node);
1862 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1863}
1864
1865/**
1866 * process_packet
1867 * Returns skb if to be freed, else it will return NULL if already used..
1868 */
1869static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1870 struct nes_cm_core *cm_core)
1871{
1872 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1873 struct tcphdr *tcph = tcp_hdr(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001874 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07001875 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001876 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1877
1878 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1879 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1880 tcph->ack, tcph->rst, tcph->fin);
1881
1882 if (tcph->rst)
1883 pkt_type = NES_PKT_TYPE_RST;
1884 else if (tcph->syn) {
1885 pkt_type = NES_PKT_TYPE_SYN;
1886 if (tcph->ack)
1887 pkt_type = NES_PKT_TYPE_SYNACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001888 } else if (tcph->ack)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001889 pkt_type = NES_PKT_TYPE_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001890 if (tcph->fin)
1891 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001892
1893 switch (pkt_type) {
1894 case NES_PKT_TYPE_SYN:
1895 handle_syn_pkt(cm_node, skb, tcph);
1896 break;
1897 case NES_PKT_TYPE_SYNACK:
1898 handle_synack_pkt(cm_node, skb, tcph);
1899 break;
1900 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07001901 ret = handle_ack_pkt(cm_node, skb, tcph);
1902 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001903 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001904 break;
1905 case NES_PKT_TYPE_RST:
1906 handle_rst_pkt(cm_node, skb, tcph);
1907 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001908 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07001909 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08001910 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07001911 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001912 break;
1913 }
1914}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001915
1916/**
1917 * mini_cm_listen - create a listen node with params
1918 */
1919static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001920 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001921{
1922 struct nes_cm_listener *listener;
1923 unsigned long flags;
1924
1925 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1926 cm_info->loc_addr, cm_info->loc_port);
1927
1928 /* cannot have multiple matching listeners */
1929 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1930 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1931 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1932 /* find automatically incs ref count ??? */
1933 atomic_dec(&listener->ref_count);
1934 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1935 return NULL;
1936 }
1937
1938 if (!listener) {
1939 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1940 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1941 if (!listener) {
1942 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1943 return NULL;
1944 }
1945
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001946 listener->loc_addr = htonl(cm_info->loc_addr);
1947 listener->loc_port = htons(cm_info->loc_port);
1948 listener->reused_node = 0;
1949
1950 atomic_set(&listener->ref_count, 1);
1951 }
1952 /* pasive case */
1953 /* find already inc'ed the ref count */
1954 else {
1955 listener->reused_node = 1;
1956 }
1957
1958 listener->cm_id = cm_info->cm_id;
1959 atomic_set(&listener->pend_accepts_cnt, 0);
1960 listener->cm_core = cm_core;
1961 listener->nesvnic = nesvnic;
1962 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001963
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001964 listener->conn_type = cm_info->conn_type;
1965 listener->backlog = cm_info->backlog;
1966 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1967
1968 if (!listener->reused_node) {
1969 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1970 list_add(&listener->list, &cm_core->listen_list.list);
1971 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1972 atomic_inc(&cm_core->listen_node_cnt);
1973 }
1974
1975 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1976 " listener = %p, backlog = %d, cm_id = %p.\n",
1977 cm_info->loc_addr, cm_info->loc_port,
1978 listener, listener->backlog, listener->cm_id);
1979
1980 return listener;
1981}
1982
1983
1984/**
1985 * mini_cm_connect - make a connection node with params
1986 */
Faisal Latif54c86a82008-09-30 14:47:27 -07001987static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001988 struct nes_vnic *nesvnic, u16 private_data_len,
1989 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001990{
1991 int ret = 0;
1992 struct nes_cm_node *cm_node;
1993 struct nes_cm_listener *loopbackremotelistener;
1994 struct nes_cm_node *loopbackremotenode;
1995 struct nes_cm_info loopback_cm_info;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001996 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
1997 struct ietf_mpa_frame *mpa_frame = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001998
1999 /* create a CM connection node */
2000 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2001 if (!cm_node)
2002 return NULL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002003 mpa_frame = &cm_node->mpa_frame;
Faisal Latif1cf078c2009-12-09 15:53:54 -08002004 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002005 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
2006 mpa_frame->rev = IETF_MPA_VERSION;
2007 mpa_frame->priv_data_len = htons(private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002008
Glenn Streiff7495ab62008-04-29 13:46:54 -07002009 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002010 cm_node->tcp_cntxt.client = 1;
2011 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2012
2013 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002014 loopbackremotelistener = find_listener(cm_core,
2015 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2016 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002017 if (loopbackremotelistener == NULL) {
2018 create_event(cm_node, NES_CM_EVENT_ABORTED);
2019 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002020 loopback_cm_info = *cm_info;
2021 loopback_cm_info.loc_port = cm_info->rem_port;
2022 loopback_cm_info.rem_port = cm_info->loc_port;
2023 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002024 loopbackremotenode = make_cm_node(cm_core, nesvnic,
2025 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002026 if (!loopbackremotenode) {
2027 rem_ref_cm_node(cm_node->cm_core, cm_node);
2028 return NULL;
2029 }
2030 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002031 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002032 loopbackremotenode->tcp_cntxt.rcv_wscale =
2033 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002034 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002035 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2036 private_data_len);
2037 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002038
Faisal Latif6492cdf2008-07-24 20:50:45 -07002039 /* we are done handling this state. */
2040 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002041 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002042 cm_node->tcp_cntxt.rcv_nxt =
2043 loopbackremotenode->tcp_cntxt.loc_seq_num;
2044 loopbackremotenode->tcp_cntxt.rcv_nxt =
2045 cm_node->tcp_cntxt.loc_seq_num;
2046 cm_node->tcp_cntxt.max_snd_wnd =
2047 loopbackremotenode->tcp_cntxt.rcv_wnd;
2048 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2049 cm_node->tcp_cntxt.rcv_wnd;
2050 cm_node->tcp_cntxt.snd_wnd =
2051 loopbackremotenode->tcp_cntxt.rcv_wnd;
2052 loopbackremotenode->tcp_cntxt.snd_wnd =
2053 cm_node->tcp_cntxt.rcv_wnd;
2054 cm_node->tcp_cntxt.snd_wscale =
2055 loopbackremotenode->tcp_cntxt.rcv_wscale;
2056 loopbackremotenode->tcp_cntxt.snd_wscale =
2057 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002058 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002059 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2060 }
2061 return cm_node;
2062 }
2063
2064 /* set our node side to client (active) side */
2065 cm_node->tcp_cntxt.client = 1;
2066 /* init our MPA frame ptr */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002067 memcpy(mpa_frame->priv_data, private_data, private_data_len);
2068
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002069 cm_node->mpa_frame_size = mpa_frame_size;
2070
2071 /* send a syn and goto syn sent state */
2072 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002073 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002074
Faisal Latif6492cdf2008-07-24 20:50:45 -07002075 if (ret) {
2076 /* error in sending the syn free up the cm_node struct */
2077 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2078 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2079 cm_node->rem_addr, cm_node->rem_port, cm_node,
2080 cm_node->cm_id);
2081 rem_ref_cm_node(cm_node->cm_core, cm_node);
2082 cm_node = NULL;
2083 }
2084
2085 if (cm_node)
2086 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2087 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2088 cm_node->rem_addr, cm_node->rem_port, cm_node,
2089 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002090
2091 return cm_node;
2092}
2093
2094
2095/**
2096 * mini_cm_accept - accept a connection
2097 * This function is never called
2098 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002099static int mini_cm_accept(struct nes_cm_core *cm_core,
2100 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002101{
2102 return 0;
2103}
2104
2105
2106/**
2107 * mini_cm_reject - reject and teardown a connection
2108 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002109static int mini_cm_reject(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002110 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002111{
2112 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002113 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002114 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002115 struct nes_cm_event event;
2116 struct iw_cm_id *cm_id = cm_node->cm_id;
2117 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002118
Faisal Latif6492cdf2008-07-24 20:50:45 -07002119 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2120 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002121
Faisal Latif6492cdf2008-07-24 20:50:45 -07002122 if (cm_node->tcp_cntxt.client)
2123 return ret;
2124 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002125
Faisal Latif9d5ab132009-03-06 15:15:01 -08002126 if (!loopback) {
2127 passive_state = atomic_add_return(1, &cm_node->passive_state);
2128 if (passive_state == NES_SEND_RESET_EVENT) {
2129 cm_node->state = NES_CM_STATE_CLOSED;
2130 rem_ref_cm_node(cm_core, cm_node);
2131 } else {
2132 ret = send_mpa_reject(cm_node);
2133 if (ret) {
2134 cm_node->state = NES_CM_STATE_CLOSED;
2135 err = send_reset(cm_node, NULL);
2136 if (err)
2137 WARN_ON(1);
2138 } else
2139 cm_id->add_ref(cm_id);
2140 }
2141 } else {
2142 cm_node->cm_id = NULL;
2143 event.cm_node = loopback;
2144 event.cm_info.rem_addr = loopback->rem_addr;
2145 event.cm_info.loc_addr = loopback->loc_addr;
2146 event.cm_info.rem_port = loopback->rem_port;
2147 event.cm_info.loc_port = loopback->loc_port;
2148 event.cm_info.cm_id = loopback->cm_id;
2149 cm_event_mpa_reject(&event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002150 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002151 loopback->state = NES_CM_STATE_CLOSING;
2152
2153 cm_id = loopback->cm_id;
2154 rem_ref_cm_node(cm_core, loopback);
2155 cm_id->rem_ref(cm_id);
2156 }
2157
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002158 return ret;
2159}
2160
2161
2162/**
2163 * mini_cm_close
2164 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002165static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002166{
2167 int ret = 0;
2168
2169 if (!cm_core || !cm_node)
2170 return -EINVAL;
2171
2172 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002173 case NES_CM_STATE_SYN_RCVD:
2174 case NES_CM_STATE_SYN_SENT:
2175 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2176 case NES_CM_STATE_ESTABLISHED:
2177 case NES_CM_STATE_ACCEPTING:
2178 case NES_CM_STATE_MPAREQ_SENT:
2179 case NES_CM_STATE_MPAREQ_RCVD:
2180 cleanup_retrans_entry(cm_node);
2181 send_reset(cm_node, NULL);
2182 break;
2183 case NES_CM_STATE_CLOSE_WAIT:
2184 cm_node->state = NES_CM_STATE_LAST_ACK;
2185 send_fin(cm_node, NULL);
2186 break;
2187 case NES_CM_STATE_FIN_WAIT1:
2188 case NES_CM_STATE_FIN_WAIT2:
2189 case NES_CM_STATE_LAST_ACK:
2190 case NES_CM_STATE_TIME_WAIT:
2191 case NES_CM_STATE_CLOSING:
2192 ret = -1;
2193 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002194 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002195 case NES_CM_STATE_LISTENING:
2196 case NES_CM_STATE_UNKNOWN:
2197 case NES_CM_STATE_INITED:
2198 case NES_CM_STATE_CLOSED:
2199 ret = rem_ref_cm_node(cm_core, cm_node);
2200 break;
2201 case NES_CM_STATE_TSA:
2202 if (cm_node->send_entry)
2203 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2204 "send_entry=%p\n", cm_node->send_entry);
2205 ret = rem_ref_cm_node(cm_core, cm_node);
2206 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002207 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002208 return ret;
2209}
2210
2211
2212/**
2213 * recv_pkt - recv an ETHERNET packet, and process it through CM
2214 * node state machine
2215 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002216static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002217 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002218{
2219 struct nes_cm_node *cm_node = NULL;
2220 struct nes_cm_listener *listener = NULL;
2221 struct iphdr *iph;
2222 struct tcphdr *tcph;
2223 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002224 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002225 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002226
Faisal Latif6492cdf2008-07-24 20:50:45 -07002227 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002228 return 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002229 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002230 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002231 }
2232
2233 iph = (struct iphdr *)skb->data;
2234 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002235
2236 nfo.loc_addr = ntohl(iph->daddr);
2237 nfo.loc_port = ntohs(tcph->dest);
2238 nfo.rem_addr = ntohl(iph->saddr);
2239 nfo.rem_port = ntohs(tcph->source);
2240
Harvey Harrison03080e52009-01-10 21:45:42 -08002241 tmp_daddr = cpu_to_be32(iph->daddr);
2242 tmp_saddr = cpu_to_be32(iph->saddr);
2243
Harvey Harrison63779432008-10-31 00:56:00 -07002244 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002245 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002246
Faisal Latif6492cdf2008-07-24 20:50:45 -07002247 do {
2248 cm_node = find_node(cm_core,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002249 nfo.rem_port, nfo.rem_addr,
2250 nfo.loc_port, nfo.loc_addr);
2251
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002252 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002253 /* Only type of packet accepted are for */
2254 /* the PASSIVE open (syn only) */
2255 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002256 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002257 break;
2258 }
2259 listener = find_listener(cm_core, nfo.loc_addr,
2260 nfo.loc_port,
2261 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002262 if (!listener) {
2263 nfo.cm_id = NULL;
2264 nfo.conn_type = 0;
2265 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2266 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002267 break;
2268 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002269 nfo.cm_id = listener->cm_id;
2270 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002271 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2272 listener);
2273 if (!cm_node) {
2274 nes_debug(NES_DBG_CM, "Unable to allocate "
2275 "node\n");
2276 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002277 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002278 dev_kfree_skb_any(skb);
2279 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002280 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002281 if (!tcph->rst && !tcph->fin) {
2282 cm_node->state = NES_CM_STATE_LISTENING;
2283 } else {
2284 cm_packets_dropped++;
2285 rem_ref_cm_node(cm_core, cm_node);
2286 dev_kfree_skb_any(skb);
2287 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002288 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002289 add_ref_cm_node(cm_node);
2290 } else if (cm_node->state == NES_CM_STATE_TSA) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002291 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002292 atomic_inc(&cm_accel_dropped_pkts);
2293 dev_kfree_skb_any(skb);
2294 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002295 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002296 skb_reset_network_header(skb);
2297 skb_set_transport_header(skb, sizeof(*tcph));
2298 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002299 process_packet(cm_node, skb, cm_core);
2300 rem_ref_cm_node(cm_core, cm_node);
2301 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002302 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002303}
2304
2305
2306/**
2307 * nes_cm_alloc_core - allocate a top level instance of a cm core
2308 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002309static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002310{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002311 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002312
2313 /* setup the CM core */
2314 /* alloc top level core control structure */
2315 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2316 if (!cm_core)
2317 return NULL;
2318
2319 INIT_LIST_HEAD(&cm_core->connected_nodes);
2320 init_timer(&cm_core->tcp_timer);
2321 cm_core->tcp_timer.function = nes_cm_timer_tick;
2322
2323 cm_core->mtu = NES_CM_DEFAULT_MTU;
2324 cm_core->state = NES_CM_STATE_INITED;
2325 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2326
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002327 atomic_set(&cm_core->events_posted, 0);
2328
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002329 cm_core->api = &nes_cm_api;
2330
2331 spin_lock_init(&cm_core->ht_lock);
2332 spin_lock_init(&cm_core->listen_list_lock);
2333
2334 INIT_LIST_HEAD(&cm_core->listen_list.list);
2335
2336 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2337
2338 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2339 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2340 cm_core->post_event = nes_cm_post_event;
2341 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2342 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2343
2344 print_core(cm_core);
2345 return cm_core;
2346}
2347
2348
2349/**
2350 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2351 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002352static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002353{
2354 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2355
2356 if (!cm_core)
2357 return -EINVAL;
2358
2359 barrier();
2360
2361 if (timer_pending(&cm_core->tcp_timer)) {
2362 del_timer(&cm_core->tcp_timer);
2363 }
2364
2365 destroy_workqueue(cm_core->event_wq);
2366 destroy_workqueue(cm_core->disconn_wq);
2367 nes_debug(NES_DBG_CM, "\n");
2368 kfree(cm_core);
2369
2370 return 0;
2371}
2372
2373
2374/**
2375 * mini_cm_get
2376 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002377static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002378{
2379 return cm_core->state;
2380}
2381
2382
2383/**
2384 * mini_cm_set
2385 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002386static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002387{
2388 int ret = 0;
2389
2390 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002391 case NES_CM_SET_PKT_SIZE:
2392 cm_core->mtu = value;
2393 break;
2394 case NES_CM_SET_FREE_PKT_Q_SIZE:
2395 cm_core->free_tx_pkt_max = value;
2396 break;
2397 default:
2398 /* unknown set option */
2399 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002400 }
2401
2402 return ret;
2403}
2404
2405
2406/**
2407 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2408 * successfully exchanged when this is called
2409 */
2410static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2411{
2412 int ret = 0;
2413
2414 if (!nesqp)
2415 return -EINVAL;
2416
2417 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2418 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2419 NES_QPCONTEXT_MISC_DROS);
2420
2421 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2422 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2423
2424 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2425
2426 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2427
2428 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2429 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2430
2431 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2432 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2433 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2434
2435 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2436 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2437 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2438
2439 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2440 nesqp->nesqp_context->ts_recent = 0;
2441 nesqp->nesqp_context->ts_age = 0;
2442 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2443 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2444 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2445 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2446 cm_node->tcp_cntxt.rcv_wscale);
2447 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2448 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2449 nesqp->nesqp_context->srtt = 0;
2450 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2451 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2452 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2453 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2454 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2455 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2456
2457 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2458 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2459 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2460 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2461 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2462 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2463 le32_to_cpu(nesqp->nesqp_context->misc));
2464 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2465 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2466 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2467
2468 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2469 cm_node->state = NES_CM_STATE_TSA;
2470
2471 return ret;
2472}
2473
2474
2475/**
2476 * nes_cm_disconn
2477 */
2478int nes_cm_disconn(struct nes_qp *nesqp)
2479{
Don Wood873fcdd2009-09-05 20:36:37 -07002480 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002481
Don Wood873fcdd2009-09-05 20:36:37 -07002482 work = kzalloc(sizeof *work, GFP_ATOMIC);
2483 if (!work)
2484 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002485
Don Wood873fcdd2009-09-05 20:36:37 -07002486 nes_add_ref(&nesqp->ibqp);
2487 work->nesqp = nesqp;
2488 INIT_WORK(&work->work, nes_disconnect_worker);
2489 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002490 return 0;
2491}
2492
2493
2494/**
2495 * nes_disconnect_worker
2496 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002497static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002498{
Don Wood873fcdd2009-09-05 20:36:37 -07002499 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2500 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002501
Don Wood873fcdd2009-09-05 20:36:37 -07002502 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002503 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2504 nesqp->last_aeq, nesqp->hwqp.qp_id);
2505 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002506 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002507}
2508
2509
2510/**
2511 * nes_cm_disconn_true
2512 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002513static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002514{
2515 unsigned long flags;
2516 int ret = 0;
2517 struct iw_cm_id *cm_id;
2518 struct iw_cm_event cm_event;
2519 struct nes_vnic *nesvnic;
2520 u16 last_ae;
2521 u8 original_hw_tcp_state;
2522 u8 original_ibqp_state;
Don Woodb29a4fc2009-09-05 20:36:39 -07002523 enum iw_cm_event_type disconn_status = IW_CM_EVENT_STATUS_OK;
2524 int issue_disconn = 0;
2525 int issue_close = 0;
2526 int issue_flush = 0;
2527 u32 flush_q = NES_CQP_FLUSH_RQ;
2528 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002529
2530 if (!nesqp) {
2531 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2532 return -1;
2533 }
2534
2535 spin_lock_irqsave(&nesqp->lock, flags);
2536 cm_id = nesqp->cm_id;
2537 /* make sure we havent already closed this connection */
2538 if (!cm_id) {
2539 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2540 nesqp->hwqp.qp_id);
2541 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002542 return -1;
2543 }
2544
2545 nesvnic = to_nesvnic(nesqp->ibqp.device);
2546 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2547
2548 original_hw_tcp_state = nesqp->hw_tcp_state;
2549 original_ibqp_state = nesqp->ibqp_state;
2550 last_ae = nesqp->last_aeq;
2551
Don Woodb29a4fc2009-09-05 20:36:39 -07002552 if (nesqp->term_flags) {
2553 issue_disconn = 1;
2554 issue_close = 1;
2555 nesqp->cm_id = NULL;
2556 if (nesqp->flush_issued == 0) {
2557 nesqp->flush_issued = 1;
2558 issue_flush = 1;
2559 }
2560 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2561 ((original_ibqp_state == IB_QPS_RTS) &&
2562 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2563 issue_disconn = 1;
2564 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
2565 disconn_status = IW_CM_EVENT_STATUS_RESET;
2566 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002567
Don Woodb29a4fc2009-09-05 20:36:39 -07002568 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2569 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2570 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2571 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2572 issue_close = 1;
2573 nesqp->cm_id = NULL;
2574 if (nesqp->flush_issued == 0) {
2575 nesqp->flush_issued = 1;
2576 issue_flush = 1;
2577 }
2578 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002579
Don Woodb29a4fc2009-09-05 20:36:39 -07002580 spin_unlock_irqrestore(&nesqp->lock, flags);
2581
2582 if ((issue_flush) && (nesqp->destroyed == 0)) {
2583 /* Flush the queue(s) */
2584 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2585 flush_q |= NES_CQP_FLUSH_SQ;
2586 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2587
2588 if (nesqp->term_flags) {
2589 ibevent.device = nesqp->ibqp.device;
2590 ibevent.event = nesqp->terminate_eventtype;
2591 ibevent.element.qp = &nesqp->ibqp;
2592 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
2593 }
2594 }
2595
2596 if ((cm_id) && (cm_id->event_handler)) {
2597 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002598 atomic_inc(&cm_disconnects);
2599 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002600 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002601 cm_event.local_addr = cm_id->local_addr;
2602 cm_event.remote_addr = cm_id->remote_addr;
2603 cm_event.private_data = NULL;
2604 cm_event.private_data_len = 0;
2605
Faisal Latif6492cdf2008-07-24 20:50:45 -07002606 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2607 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2608 "cm_id = %p, refcount = %u.\n",
2609 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2610 nesqp->hwqp.sq_tail, cm_id,
2611 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002612
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002613 ret = cm_id->event_handler(cm_id, &cm_event);
2614 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002615 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2616 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002617 }
2618
Don Woodb29a4fc2009-09-05 20:36:39 -07002619 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002620 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002621 nes_disconnect(nesqp, 1);
2622
2623 cm_id->provider_data = nesqp;
2624 /* Send up the close complete event */
2625 cm_event.event = IW_CM_EVENT_CLOSE;
2626 cm_event.status = IW_CM_EVENT_STATUS_OK;
2627 cm_event.provider_data = cm_id->provider_data;
2628 cm_event.local_addr = cm_id->local_addr;
2629 cm_event.remote_addr = cm_id->remote_addr;
2630 cm_event.private_data = NULL;
2631 cm_event.private_data_len = 0;
2632
2633 ret = cm_id->event_handler(cm_id, &cm_event);
2634 if (ret) {
2635 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2636 }
2637
2638 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002639 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002640 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002641
2642 return 0;
2643}
2644
2645
2646/**
2647 * nes_disconnect
2648 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002649static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002650{
2651 int ret = 0;
2652 struct nes_vnic *nesvnic;
2653 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002654 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002655
2656 nesvnic = to_nesvnic(nesqp->ibqp.device);
2657 if (!nesvnic)
2658 return -EINVAL;
2659
2660 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002661 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002662
2663 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2664 atomic_read(&nesvnic->netdev->refcnt));
2665
2666 if (nesqp->active_conn) {
2667
2668 /* indicate this connection is NOT active */
2669 nesqp->active_conn = 0;
2670 } else {
2671 /* Need to free the Last Streaming Mode Message */
2672 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002673 if (nesqp->lsmm_mr)
2674 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002675 pci_free_consistent(nesdev->pcidev,
2676 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2677 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2678 }
2679 }
2680
2681 /* close the CM node down if it is still active */
2682 if (nesqp->cm_node) {
2683 nes_debug(NES_DBG_CM, "Call close API\n");
2684
2685 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002686 }
2687
2688 return ret;
2689}
2690
2691
2692/**
2693 * nes_accept
2694 */
2695int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2696{
2697 u64 u64temp;
2698 struct ib_qp *ibqp;
2699 struct nes_qp *nesqp;
2700 struct nes_vnic *nesvnic;
2701 struct nes_device *nesdev;
2702 struct nes_cm_node *cm_node;
2703 struct nes_adapter *adapter;
2704 struct ib_qp_attr attr;
2705 struct iw_cm_event cm_event;
2706 struct nes_hw_qp_wqe *wqe;
2707 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002708 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002709 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002710 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002711 struct nes_ib_device *nesibdev;
2712 struct ib_mr *ibmr = NULL;
2713 struct ib_phys_buf ibphysbuf;
2714 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002715 u64 tagged_offset;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002716
2717
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002718
2719 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2720 if (!ibqp)
2721 return -EINVAL;
2722
2723 /* get all our handles */
2724 nesqp = to_nesqp(ibqp);
2725 nesvnic = to_nesvnic(nesqp->ibqp.device);
2726 nesdev = nesvnic->nesdev;
2727 adapter = nesdev->nesadapter;
2728
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002729 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002730 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2731 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2732 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002733
2734 /* associate the node with the QP */
2735 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002736 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002737
Faisal Latif6492cdf2008-07-24 20:50:45 -07002738 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2739 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002740 atomic_inc(&cm_accepts);
2741
2742 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2743 atomic_read(&nesvnic->netdev->refcnt));
2744
Faisal Latif6492cdf2008-07-24 20:50:45 -07002745 /* allocate the ietf frame and space for private data */
2746 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2747 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2748 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002749
Faisal Latif6492cdf2008-07-24 20:50:45 -07002750 if (!nesqp->ietf_frame) {
2751 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2752 "data\n");
2753 return -ENOMEM;
2754 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002755
2756
Faisal Latif6492cdf2008-07-24 20:50:45 -07002757 /* setup the MPA frame */
2758 nesqp->private_data_len = conn_param->private_data_len;
2759 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002760
Faisal Latif6492cdf2008-07-24 20:50:45 -07002761 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2762 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002763
Faisal Latif6492cdf2008-07-24 20:50:45 -07002764 nesqp->ietf_frame->priv_data_len =
2765 cpu_to_be16(conn_param->private_data_len);
2766 nesqp->ietf_frame->rev = mpa_version;
2767 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002768
Faisal Latif6492cdf2008-07-24 20:50:45 -07002769 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2770 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002771
Faisal Latif6492cdf2008-07-24 20:50:45 -07002772 if (cm_id->remote_addr.sin_addr.s_addr !=
2773 cm_id->local_addr.sin_addr.s_addr) {
2774 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002775 nesibdev = nesvnic->nesibdev;
2776 nespd = nesqp->nespd;
2777 ibphysbuf.addr = nesqp->ietf_frame_pbase;
2778 ibphysbuf.size = conn_param->private_data_len +
2779 sizeof(struct ietf_mpa_frame);
Don Wood7a5efb62009-04-08 14:21:02 -07002780 tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002781 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
2782 &ibphysbuf, 1,
2783 IB_ACCESS_LOCAL_WRITE,
Don Wood7a5efb62009-04-08 14:21:02 -07002784 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002785 if (!ibmr) {
2786 nes_debug(NES_DBG_CM, "Unable to register memory region"
2787 "for lSMM for cm_node = %p \n",
2788 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07002789 pci_free_consistent(nesdev->pcidev,
2790 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2791 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002792 return -ENOMEM;
2793 }
2794
2795 ibmr->pd = &nespd->ibpd;
2796 ibmr->device = nespd->ibpd.device;
2797 nesqp->lsmm_mr = ibmr;
2798
Faisal Latif6492cdf2008-07-24 20:50:45 -07002799 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2800 set_wqe_64bit_value(wqe->wqe_words,
2801 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2802 u64temp);
2803 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2804 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2805 NES_IWARP_SQ_WQE_WRPDU);
2806 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2807 cpu_to_le32(conn_param->private_data_len +
2808 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002809 set_wqe_64bit_value(wqe->wqe_words,
2810 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
Don Wood7a5efb62009-04-08 14:21:02 -07002811 (u64)(unsigned long)nesqp->ietf_frame);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002812 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2813 cpu_to_le32(conn_param->private_data_len +
2814 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002815 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002816
Faisal Latif6492cdf2008-07-24 20:50:45 -07002817 nesqp->nesqp_context->ird_ord_sizes |=
2818 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2819 NES_QPCONTEXT_ORDIRD_WRPDU);
2820 } else {
2821 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08002822 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002823 }
2824 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002825
2826
2827 /* Cache the cm_id in the qp */
2828 nesqp->cm_id = cm_id;
2829 cm_node->cm_id = cm_id;
2830
2831 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2832 cm_id->provider_data = nesqp;
2833 nesqp->active_conn = 0;
2834
Faisal Latif6492cdf2008-07-24 20:50:45 -07002835 if (cm_node->state == NES_CM_STATE_TSA)
2836 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2837 cm_node);
2838
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002839 nes_cm_init_tsa_conn(nesqp, cm_node);
2840
Faisal Latif6492cdf2008-07-24 20:50:45 -07002841 nesqp->nesqp_context->tcpPorts[0] =
2842 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2843 nesqp->nesqp_context->tcpPorts[1] =
2844 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2845
2846 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2847 nesqp->nesqp_context->ip0 =
2848 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2849 else
2850 nesqp->nesqp_context->ip0 =
2851 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002852
2853 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002854 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2855 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002856
Faisal Latif6492cdf2008-07-24 20:50:45 -07002857 nesqp->nesqp_context->arp_index_vlan |=
2858 cpu_to_le32(nes_arp_table(nesdev,
2859 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002860 NES_ARP_RESOLVE) << 16);
2861
2862 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002863 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002864
2865 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2866
2867 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002868 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2869 nesqp->nesqp_context->ird_ord_sizes |=
2870 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002871
2872 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002873 nes_quad.DstIpAdrIndex =
2874 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2875 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2876 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2877 else
2878 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2879 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2880 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002881
2882 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06002883 crc_value = get_crc_value(&nes_quad);
2884 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002885 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07002886 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002887
2888 nesqp->hte_index &= adapter->hte_index_mask;
2889 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2890
2891 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2892
Faisal Latif6492cdf2008-07-24 20:50:45 -07002893 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2894 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2895 "private data length=%zu.\n", nesqp->hwqp.qp_id,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002896 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2897 ntohs(cm_id->remote_addr.sin_port),
2898 ntohl(cm_id->local_addr.sin_addr.s_addr),
2899 ntohs(cm_id->local_addr.sin_port),
2900 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2901 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002902 conn_param->private_data_len +
2903 sizeof(struct ietf_mpa_frame));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002904
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002905
Coly Li73ac36e2009-01-07 18:09:16 -08002906 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002907 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07002908 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002909
2910 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2911 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2912 cm_event.provider_data = (void *)nesqp;
2913 cm_event.local_addr = cm_id->local_addr;
2914 cm_event.remote_addr = cm_id->remote_addr;
2915 cm_event.private_data = NULL;
2916 cm_event.private_data_len = 0;
2917 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002918 attr.qp_state = IB_QPS_RTS;
2919 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002920 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002921 cm_node->loopbackpartner->mpa_frame_size =
2922 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002923 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002924 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2925 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002926 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2927 }
2928 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002929 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2930 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002931
Faisal Latif183ecfa2008-11-21 20:50:46 -06002932 passive_state = atomic_add_return(1, &cm_node->passive_state);
2933 if (passive_state == NES_SEND_RESET_EVENT)
2934 create_event(cm_node, NES_CM_EVENT_RESET);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002935 return 0;
2936}
2937
2938
2939/**
2940 * nes_reject
2941 */
2942int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2943{
2944 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002945 struct nes_cm_node *loopback;
2946
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002947 struct nes_cm_core *cm_core;
2948
2949 atomic_inc(&cm_rejects);
2950 cm_node = (struct nes_cm_node *) cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002951 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002952 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002953 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002954 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2955
Faisal Latif9d5ab132009-03-06 15:15:01 -08002956 if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
2957 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002958
Faisal Latif1cf078c2009-12-09 15:53:54 -08002959 memcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002960 if (loopback) {
2961 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
2962 loopback->mpa_frame.priv_data_len = pdata_len;
2963 loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
2964 pdata_len;
2965 } else {
2966 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2967 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2968 }
2969
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002970 cm_node->mpa_frame.rev = mpa_version;
2971 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2972
Faisal Latif9d5ab132009-03-06 15:15:01 -08002973 return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002974}
2975
2976
2977/**
2978 * nes_connect
2979 * setup and launch cm connect node
2980 */
2981int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2982{
2983 struct ib_qp *ibqp;
2984 struct nes_qp *nesqp;
2985 struct nes_vnic *nesvnic;
2986 struct nes_device *nesdev;
2987 struct nes_cm_node *cm_node;
2988 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07002989 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002990
2991 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2992 if (!ibqp)
2993 return -EINVAL;
2994 nesqp = to_nesqp(ibqp);
2995 if (!nesqp)
2996 return -EINVAL;
2997 nesvnic = to_nesvnic(nesqp->ibqp.device);
2998 if (!nesvnic)
2999 return -EINVAL;
3000 nesdev = nesvnic->nesdev;
3001 if (!nesdev)
3002 return -EINVAL;
3003
Faisal Latif6492cdf2008-07-24 20:50:45 -07003004 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
3005 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3006 ntohl(nesvnic->local_ipaddr),
3007 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3008 ntohs(cm_id->remote_addr.sin_port),
3009 ntohl(cm_id->local_addr.sin_addr.s_addr),
3010 ntohs(cm_id->local_addr.sin_port));
3011
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003012 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003013 nesqp->active_conn = 1;
3014
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003015 /* cache the cm_id in the qp */
3016 nesqp->cm_id = cm_id;
3017
3018 cm_id->provider_data = nesqp;
3019
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003020 nesqp->private_data_len = conn_param->private_data_len;
3021 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
3022 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003023 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
3024 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003025
Faisal Latif6492cdf2008-07-24 20:50:45 -07003026 if (cm_id->local_addr.sin_addr.s_addr !=
Faisal Latif53094c32009-04-27 13:37:34 -07003027 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003028 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003029 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003030 apbvt_set = 1;
3031 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003032
3033 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003034 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3035 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3036 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3037 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003038 cm_info.cm_id = cm_id;
3039 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3040
3041 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003042
3043 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003044 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3045 conn_param->private_data_len, (void *)conn_param->private_data,
3046 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003047 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003048 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003049 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003050 PCI_FUNC(nesdev->pcidev->devfn),
3051 NES_MANAGE_APBVT_DEL);
3052
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003053 cm_id->rem_ref(cm_id);
3054 return -ENOMEM;
3055 }
3056
Faisal Latif53094c32009-04-27 13:37:34 -07003057 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003058 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003059 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003060 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003061
3062 return 0;
3063}
3064
3065
3066/**
3067 * nes_create_listen
3068 */
3069int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3070{
3071 struct nes_vnic *nesvnic;
3072 struct nes_cm_listener *cm_node;
3073 struct nes_cm_info cm_info;
3074 struct nes_adapter *adapter;
3075 int err;
3076
3077
3078 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3079 cm_id, ntohs(cm_id->local_addr.sin_port));
3080
3081 nesvnic = to_nesvnic(cm_id->device);
3082 if (!nesvnic)
3083 return -EINVAL;
3084 adapter = nesvnic->nesdev->nesadapter;
3085 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3086 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3087
3088 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3089 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3090
3091 /* setup listen params in our api call struct */
3092 cm_info.loc_addr = nesvnic->local_ipaddr;
3093 cm_info.loc_port = cm_id->local_addr.sin_port;
3094 cm_info.backlog = backlog;
3095 cm_info.cm_id = cm_id;
3096
3097 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3098
3099
3100 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3101 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003102 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003103 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003104 return -ENOMEM;
3105 }
3106
3107 cm_id->provider_data = cm_node;
3108
3109 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003110 err = nes_manage_apbvt(nesvnic,
3111 ntohs(cm_id->local_addr.sin_port),
3112 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3113 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003114 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003115 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3116 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003117 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3118 return err;
3119 }
3120 cm_listens_created++;
3121 }
3122
3123 cm_id->add_ref(cm_id);
3124 cm_id->provider_data = (void *)cm_node;
3125
3126
3127 return 0;
3128}
3129
3130
3131/**
3132 * nes_destroy_listen
3133 */
3134int nes_destroy_listen(struct iw_cm_id *cm_id)
3135{
3136 if (cm_id->provider_data)
3137 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3138 else
3139 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3140
3141 cm_id->rem_ref(cm_id);
3142
3143 return 0;
3144}
3145
3146
3147/**
3148 * nes_cm_recv
3149 */
3150int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3151{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003152 int rc = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003153 cm_packets_received++;
3154 if ((g_cm_core) && (g_cm_core->api)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003155 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003156 } else {
3157 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3158 " cm is not setup properly.\n");
3159 }
3160
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003161 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003162}
3163
3164
3165/**
3166 * nes_cm_start
3167 * Start and init a cm core module
3168 */
3169int nes_cm_start(void)
3170{
3171 nes_debug(NES_DBG_CM, "\n");
3172 /* create the primary CM core, pass this handle to subsequent core inits */
3173 g_cm_core = nes_cm_alloc_core();
3174 if (g_cm_core) {
3175 return 0;
3176 } else {
3177 return -ENOMEM;
3178 }
3179}
3180
3181
3182/**
3183 * nes_cm_stop
3184 * stop and dealloc all cm core instances
3185 */
3186int nes_cm_stop(void)
3187{
3188 g_cm_core->api->destroy_cm_core(g_cm_core);
3189 return 0;
3190}
3191
3192
3193/**
3194 * cm_event_connected
3195 * handle a connected event, setup QPs and HW
3196 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003197static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003198{
3199 u64 u64temp;
3200 struct nes_qp *nesqp;
3201 struct nes_vnic *nesvnic;
3202 struct nes_device *nesdev;
3203 struct nes_cm_node *cm_node;
3204 struct nes_adapter *nesadapter;
3205 struct ib_qp_attr attr;
3206 struct iw_cm_id *cm_id;
3207 struct iw_cm_event cm_event;
3208 struct nes_hw_qp_wqe *wqe;
3209 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003210 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003211 int ret;
3212
3213 /* get all our handles */
3214 cm_node = event->cm_node;
3215 cm_id = cm_node->cm_id;
3216 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3217 nesqp = (struct nes_qp *)cm_id->provider_data;
3218 nesvnic = to_nesvnic(nesqp->ibqp.device);
3219 nesdev = nesvnic->nesdev;
3220 nesadapter = nesdev->nesadapter;
3221
3222 if (nesqp->destroyed) {
3223 return;
3224 }
3225 atomic_inc(&cm_connecteds);
3226 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3227 " local port 0x%04X. jiffies = %lu.\n",
3228 nesqp->hwqp.qp_id,
3229 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3230 ntohs(cm_id->remote_addr.sin_port),
3231 ntohs(cm_id->local_addr.sin_port),
3232 jiffies);
3233
3234 nes_cm_init_tsa_conn(nesqp, cm_node);
3235
3236 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003237 nesqp->nesqp_context->tcpPorts[0] =
3238 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3239 nesqp->nesqp_context->tcpPorts[1] =
3240 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3241 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3242 nesqp->nesqp_context->ip0 =
3243 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3244 else
3245 nesqp->nesqp_context->ip0 =
3246 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003247
3248 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003249 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3250 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003251 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003252 nes_arp_table(nesdev,
3253 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003254 NULL, NES_ARP_RESOLVE) << 16);
3255 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3256 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3257 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3258 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003259 cpu_to_le32((u32)1 <<
3260 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003261
3262 /* Adjust tail for not having a LSMM */
3263 nesqp->hwqp.sq_tail = 1;
3264
3265#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003266 if (cm_node->send_write0) {
3267 nes_debug(NES_DBG_CM, "Sending first write.\n");
3268 wqe = &nesqp->hwqp.sq_vbase[0];
3269 u64temp = (unsigned long)nesqp;
3270 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3271 set_wqe_64bit_value(wqe->wqe_words,
3272 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3273 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3274 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3275 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3276 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3277 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3278 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3279 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003280
Faisal Latif6492cdf2008-07-24 20:50:45 -07003281 /* use the reserved spot on the WQ for the extra first WQE */
3282 nesqp->nesqp_context->ird_ord_sizes &=
3283 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3284 NES_QPCONTEXT_ORDIRD_WRPDU |
3285 NES_QPCONTEXT_ORDIRD_ALSMM));
3286 nesqp->skip_lsmm = 1;
3287 nesqp->hwqp.sq_tail = 0;
3288 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3289 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3290 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003291#endif
3292
3293 memset(&nes_quad, 0, sizeof(nes_quad));
3294
Faisal Latif6492cdf2008-07-24 20:50:45 -07003295 nes_quad.DstIpAdrIndex =
3296 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3297 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3298 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3299 else
3300 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003301 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3302 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3303
3304 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003305 crc_value = get_crc_value(&nes_quad);
3306 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003307 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3308 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3309
3310 nesqp->hte_index &= nesadapter->hte_index_mask;
3311 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3312
3313 nesqp->ietf_frame = &cm_node->mpa_frame;
3314 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3315 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3316
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003317 /* notify OF layer we successfully created the requested connection */
3318 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3319 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3320 cm_event.provider_data = cm_id->provider_data;
3321 cm_event.local_addr.sin_family = AF_INET;
3322 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3323 cm_event.remote_addr = cm_id->remote_addr;
3324
Faisal Latif6492cdf2008-07-24 20:50:45 -07003325 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3326 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003327
3328 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3329 ret = cm_id->event_handler(cm_id, &cm_event);
3330 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3331
3332 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003333 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3334 "ret=%d\n", __func__, __LINE__, ret);
3335 attr.qp_state = IB_QPS_RTS;
3336 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003337
Faisal Latif6492cdf2008-07-24 20:50:45 -07003338 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3339 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003340
3341 return;
3342}
3343
3344
3345/**
3346 * cm_event_connect_error
3347 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003348static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003349{
3350 struct nes_qp *nesqp;
3351 struct iw_cm_id *cm_id;
3352 struct iw_cm_event cm_event;
3353 /* struct nes_cm_info cm_info; */
3354 int ret;
3355
3356 if (!event->cm_node)
3357 return;
3358
3359 cm_id = event->cm_node->cm_id;
3360 if (!cm_id) {
3361 return;
3362 }
3363
3364 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3365 nesqp = cm_id->provider_data;
3366
3367 if (!nesqp) {
3368 return;
3369 }
3370
3371 /* notify OF layer about this connection error event */
3372 /* cm_id->rem_ref(cm_id); */
3373 nesqp->cm_id = NULL;
3374 cm_id->provider_data = NULL;
3375 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3376 cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
3377 cm_event.provider_data = cm_id->provider_data;
3378 cm_event.local_addr = cm_id->local_addr;
3379 cm_event.remote_addr = cm_id->remote_addr;
3380 cm_event.private_data = NULL;
3381 cm_event.private_data_len = 0;
3382
Faisal Latif6492cdf2008-07-24 20:50:45 -07003383 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3384 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3385 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003386
3387 ret = cm_id->event_handler(cm_id, &cm_event);
3388 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3389 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003390 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3391 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003392 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003393
Faisal Latif6492cdf2008-07-24 20:50:45 -07003394 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003395 return;
3396}
3397
3398
3399/**
3400 * cm_event_reset
3401 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003402static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003403{
3404 struct nes_qp *nesqp;
3405 struct iw_cm_id *cm_id;
3406 struct iw_cm_event cm_event;
3407 /* struct nes_cm_info cm_info; */
3408 int ret;
3409
3410 if (!event->cm_node)
3411 return;
3412
3413 if (!event->cm_node->cm_id)
3414 return;
3415
3416 cm_id = event->cm_node->cm_id;
3417
3418 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3419 nesqp = cm_id->provider_data;
3420
3421 nesqp->cm_id = NULL;
3422 /* cm_id->provider_data = NULL; */
3423 cm_event.event = IW_CM_EVENT_DISCONNECT;
3424 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3425 cm_event.provider_data = cm_id->provider_data;
3426 cm_event.local_addr = cm_id->local_addr;
3427 cm_event.remote_addr = cm_id->remote_addr;
3428 cm_event.private_data = NULL;
3429 cm_event.private_data_len = 0;
3430
3431 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003432 cm_id->add_ref(cm_id);
3433 atomic_inc(&cm_closes);
3434 cm_event.event = IW_CM_EVENT_CLOSE;
3435 cm_event.status = IW_CM_EVENT_STATUS_OK;
3436 cm_event.provider_data = cm_id->provider_data;
3437 cm_event.local_addr = cm_id->local_addr;
3438 cm_event.remote_addr = cm_id->remote_addr;
3439 cm_event.private_data = NULL;
3440 cm_event.private_data_len = 0;
3441 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3442 ret = cm_id->event_handler(cm_id, &cm_event);
3443
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003444 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3445
3446
3447 /* notify OF layer about this connection error event */
3448 cm_id->rem_ref(cm_id);
3449
3450 return;
3451}
3452
3453
3454/**
3455 * cm_event_mpa_req
3456 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003457static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003458{
3459 struct iw_cm_id *cm_id;
3460 struct iw_cm_event cm_event;
3461 int ret;
3462 struct nes_cm_node *cm_node;
3463
3464 cm_node = event->cm_node;
3465 if (!cm_node)
3466 return;
3467 cm_id = cm_node->cm_id;
3468
3469 atomic_inc(&cm_connect_reqs);
3470 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3471 cm_node, cm_id, jiffies);
3472
3473 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3474 cm_event.status = IW_CM_EVENT_STATUS_OK;
3475 cm_event.provider_data = (void *)cm_node;
3476
3477 cm_event.local_addr.sin_family = AF_INET;
3478 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3479 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3480
3481 cm_event.remote_addr.sin_family = AF_INET;
3482 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3483 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003484 cm_event.private_data = cm_node->mpa_frame_buf;
3485 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003486
3487 ret = cm_id->event_handler(cm_id, &cm_event);
3488 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003489 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3490 __func__, __LINE__, ret);
3491 return;
3492}
3493
3494
3495static void cm_event_mpa_reject(struct nes_cm_event *event)
3496{
3497 struct iw_cm_id *cm_id;
3498 struct iw_cm_event cm_event;
3499 struct nes_cm_node *cm_node;
3500 int ret;
3501
3502 cm_node = event->cm_node;
3503 if (!cm_node)
3504 return;
3505 cm_id = cm_node->cm_id;
3506
3507 atomic_inc(&cm_connect_reqs);
3508 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3509 cm_node, cm_id, jiffies);
3510
3511 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3512 cm_event.status = -ECONNREFUSED;
3513 cm_event.provider_data = cm_id->provider_data;
3514
3515 cm_event.local_addr.sin_family = AF_INET;
3516 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3517 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3518
3519 cm_event.remote_addr.sin_family = AF_INET;
3520 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3521 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3522
3523 cm_event.private_data = cm_node->mpa_frame_buf;
3524 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3525
3526 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3527 "remove_addr=%08x\n",
3528 cm_event.local_addr.sin_addr.s_addr,
3529 cm_event.remote_addr.sin_addr.s_addr);
3530
3531 ret = cm_id->event_handler(cm_id, &cm_event);
3532 if (ret)
3533 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003534 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003535
3536 return;
3537}
3538
3539
3540static void nes_cm_event_handler(struct work_struct *);
3541
3542/**
3543 * nes_cm_post_event
3544 * post an event to the cm event handler
3545 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003546static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003547{
3548 atomic_inc(&event->cm_node->cm_core->events_posted);
3549 add_ref_cm_node(event->cm_node);
3550 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3551 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003552 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3553 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003554
3555 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3556
3557 nes_debug(NES_DBG_CM, "Exit\n");
3558 return 0;
3559}
3560
3561
3562/**
3563 * nes_cm_event_handler
3564 * worker function to handle cm events
3565 * will free instance of nes_cm_event
3566 */
3567static void nes_cm_event_handler(struct work_struct *work)
3568{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003569 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3570 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003571 struct nes_cm_core *cm_core;
3572
Faisal Latif6492cdf2008-07-24 20:50:45 -07003573 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003574 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003575
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003576 cm_core = event->cm_node->cm_core;
3577 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003578 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003579
3580 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003581 case NES_CM_EVENT_MPA_REQ:
3582 cm_event_mpa_req(event);
3583 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3584 event->cm_node);
3585 break;
3586 case NES_CM_EVENT_RESET:
3587 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3588 event->cm_node);
3589 cm_event_reset(event);
3590 break;
3591 case NES_CM_EVENT_CONNECTED:
3592 if ((!event->cm_node->cm_id) ||
3593 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003594 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003595 cm_event_connected(event);
3596 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3597 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003598 case NES_CM_EVENT_MPA_REJECT:
3599 if ((!event->cm_node->cm_id) ||
3600 (event->cm_node->state == NES_CM_STATE_TSA))
3601 break;
3602 cm_event_mpa_reject(event);
3603 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3604 break;
3605
Faisal Latif6492cdf2008-07-24 20:50:45 -07003606 case NES_CM_EVENT_ABORTED:
3607 if ((!event->cm_node->cm_id) ||
3608 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003609 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003610 cm_event_connect_error(event);
3611 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3612 break;
3613 case NES_CM_EVENT_DROPPED_PKT:
3614 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3615 break;
3616 default:
3617 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3618 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003619 }
3620
3621 atomic_dec(&cm_core->events_posted);
3622 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3623 rem_ref_cm_node(cm_core, event->cm_node);
3624 kfree(event);
3625
3626 return;
3627}