blob: 401b7bb828d01b68d3f2baee8786f0625a9cf39d [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
Arun Sharma600634972011-07-26 16:09:06 -070037#include <linux/atomic.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080038#include <linux/skbuff.h>
39#include <linux/ip.h>
40#include <linux/tcp.h>
41#include <linux/init.h>
42#include <linux/if_arp.h>
Chien Tungf2b2b592008-03-20 19:55:30 -050043#include <linux/if_vlan.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080044#include <linux/notifier.h>
45#include <linux/net.h>
46#include <linux/types.h>
47#include <linux/timer.h>
48#include <linux/time.h>
49#include <linux/delay.h>
50#include <linux/etherdevice.h>
51#include <linux/netdevice.h>
52#include <linux/random.h>
53#include <linux/list.h>
54#include <linux/threads.h>
Faisal Latifd2fa9b262009-12-09 15:54:28 -080055#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Bob Sharp7191a0a2008-10-03 12:21:19 -070057#include <net/arp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080058#include <net/neighbour.h>
59#include <net/route.h>
60#include <net/ip_fib.h>
Faisal Latifc11470f2009-04-27 13:38:31 -070061#include <net/tcp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080062
63#include "nes.h"
64
65u32 cm_packets_sent;
66u32 cm_packets_bounced;
67u32 cm_packets_dropped;
68u32 cm_packets_retrans;
69u32 cm_packets_created;
70u32 cm_packets_received;
Faisal Latif6e10d2e2010-02-12 19:55:03 +000071atomic_t cm_listens_created;
72atomic_t cm_listens_destroyed;
Glenn Streiff3c2d7742008-02-04 20:20:45 -080073u32 cm_backlog_drops;
74atomic_t cm_loopbacks;
75atomic_t cm_nodes_created;
76atomic_t cm_nodes_destroyed;
77atomic_t cm_accel_dropped_pkts;
78atomic_t cm_resets_recvd;
79
Faisal Latif6492cdf2008-07-24 20:50:45 -070080static inline int mini_cm_accelerated(struct nes_cm_core *,
81 struct nes_cm_node *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080082static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070083 struct nes_vnic *, struct nes_cm_info *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080084static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070085static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070086 struct nes_vnic *, u16, void *, struct nes_cm_info *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070087static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -070088static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
89 struct nes_cm_node *);
90static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
91 struct nes_cm_node *);
Faisal Latif4a14f6a2008-11-21 20:50:49 -060092static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070093 struct sk_buff *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070094static int mini_cm_dealloc_core(struct nes_cm_core *);
95static int mini_cm_get(struct nes_cm_core *);
96static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070097
Chien Tung6098d102008-11-21 20:51:01 -060098static void form_cm_frame(struct sk_buff *, struct nes_cm_node *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070099 void *, u32, void *, u32, u8);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700100static int add_ref_cm_node(struct nes_cm_node *);
101static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
102
Roland Dreier1a855fb2008-04-16 21:01:09 -0700103static int nes_cm_disconn_true(struct nes_qp *);
104static int nes_cm_post_event(struct nes_cm_event *event);
105static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
106static void nes_disconnect_worker(struct work_struct *work);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700107
108static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800109static int send_mpa_reject(struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700110static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
111static int send_reset(struct nes_cm_node *, struct sk_buff *);
112static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fb2008-04-16 21:01:09 -0700113static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700114static void process_packet(struct nes_cm_node *, struct sk_buff *,
115 struct nes_cm_core *);
116
117static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
118static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
119static void cleanup_retrans_entry(struct nes_cm_node *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800120static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700121static void free_retrans_entry(struct nes_cm_node *cm_node);
122static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
123 struct sk_buff *skb, int optionsize, int passive);
124
125/* CM event handler functions */
126static void cm_event_connected(struct nes_cm_event *);
127static void cm_event_connect_error(struct nes_cm_event *);
128static void cm_event_reset(struct nes_cm_event *);
129static void cm_event_mpa_req(struct nes_cm_event *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800130static void cm_event_mpa_reject(struct nes_cm_event *);
131static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700132
133static void print_core(struct nes_cm_core *core);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800134
135/* External CM API Interface */
136/* instance of function pointers for client API */
137/* set address of this instance to cm_core->cm_ops at cm_core alloc */
138static struct nes_cm_ops nes_cm_api = {
139 mini_cm_accelerated,
140 mini_cm_listen,
141 mini_cm_del_listen,
142 mini_cm_connect,
143 mini_cm_close,
144 mini_cm_accept,
145 mini_cm_reject,
146 mini_cm_recv_pkt,
147 mini_cm_dealloc_core,
148 mini_cm_get,
149 mini_cm_set
150};
151
Roland Dreier1a855fb2008-04-16 21:01:09 -0700152static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800153
154atomic_t cm_connects;
155atomic_t cm_accepts;
156atomic_t cm_disconnects;
157atomic_t cm_closes;
158atomic_t cm_connecteds;
159atomic_t cm_connect_reqs;
160atomic_t cm_rejects;
161
Faisal Latif0f0bee82011-09-25 20:34:00 -0500162int nes_add_ref_cm_node(struct nes_cm_node *cm_node)
163{
164 return add_ref_cm_node(cm_node);
165}
166
167int nes_rem_ref_cm_node(struct nes_cm_node *cm_node)
168{
169 return rem_ref_cm_node(cm_node->cm_core, cm_node);
170}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800171
172/**
173 * create_event
174 */
175static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
176 enum nes_cm_event_type type)
177{
178 struct nes_cm_event *event;
179
180 if (!cm_node->cm_id)
181 return NULL;
182
183 /* allocate an empty event */
184 event = kzalloc(sizeof(*event), GFP_ATOMIC);
185
186 if (!event)
187 return NULL;
188
189 event->type = type;
190 event->cm_node = cm_node;
191 event->cm_info.rem_addr = cm_node->rem_addr;
192 event->cm_info.loc_addr = cm_node->loc_addr;
193 event->cm_info.rem_port = cm_node->rem_port;
194 event->cm_info.loc_port = cm_node->loc_port;
195 event->cm_info.cm_id = cm_node->cm_id;
196
Faisal Latif6492cdf2008-07-24 20:50:45 -0700197 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
198 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
199 cm_node, event, type, event->cm_info.loc_addr,
200 event->cm_info.loc_port, event->cm_info.rem_addr,
201 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800202
203 nes_cm_post_event(event);
204 return event;
205}
206
207
208/**
209 * send_mpa_request
210 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700211static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800212{
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800213 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700214 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800215 return -1;
216 }
217
218 /* send an MPA Request frame */
219 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
220 cm_node->mpa_frame_size, SET_ACK);
221
Faisal Latif9d5ab132009-03-06 15:15:01 -0800222 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
223}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800224
Faisal Latif9d5ab132009-03-06 15:15:01 -0800225
226
227static int send_mpa_reject(struct nes_cm_node *cm_node)
228{
229 struct sk_buff *skb = NULL;
230
231 skb = dev_alloc_skb(MAX_CM_BUFFER);
232 if (!skb) {
233 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
234 return -ENOMEM;
235 }
236
237 /* send an MPA reject frame */
238 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
239 cm_node->mpa_frame_size, SET_ACK | SET_FIN);
240
241 cm_node->state = NES_CM_STATE_FIN_WAIT1;
242 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800243}
244
245
246/**
247 * recv_mpa - process a received TCP pkt, we are expecting an
248 * IETF MPA frame
249 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800250static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
251 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800252{
253 struct ietf_mpa_frame *mpa_frame;
254
Faisal Latif9d5ab132009-03-06 15:15:01 -0800255 *type = NES_MPA_REQUEST_ACCEPT;
256
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800257 /* assume req frame is in tcp data payload */
258 if (len < sizeof(struct ietf_mpa_frame)) {
259 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800260 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800261 }
262
263 mpa_frame = (struct ietf_mpa_frame *)buffer;
264 cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
Faisal Latif1cf078c2009-12-09 15:53:54 -0800265 /* make sure mpa private data len is less than 512 bytes */
266 if (cm_node->mpa_frame_size > IETF_MAX_PRIV_DATA_LEN) {
267 nes_debug(NES_DBG_CM, "The received Length of Private"
268 " Data field exceeds 512 octets\n");
269 return -EINVAL;
270 }
271 /*
272 * make sure MPA receiver interoperate with the
273 * received MPA version and MPA key information
274 *
275 */
276 if (mpa_frame->rev != mpa_version) {
277 nes_debug(NES_DBG_CM, "The received mpa version"
278 " can not be interoperated\n");
279 return -EINVAL;
280 }
281 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
282 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
283 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
284 return -EINVAL;
285 }
286 } else {
287 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
288 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
289 return -EINVAL;
290 }
291 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800292
293 if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
294 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
295 " complete (%x + %x != %x)\n",
Faisal Latif9d5ab132009-03-06 15:15:01 -0800296 cm_node->mpa_frame_size,
297 (u32)sizeof(struct ietf_mpa_frame), len);
298 return -EINVAL;
299 }
300 /* make sure it does not exceed the max size */
301 if (len > MAX_CM_BUFFER) {
302 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
303 " (%x + %x != %x)\n",
304 cm_node->mpa_frame_size,
305 (u32)sizeof(struct ietf_mpa_frame), len);
306 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800307 }
308
309 /* copy entire MPA frame to our cm_node's frame */
310 memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
311 cm_node->mpa_frame_size);
312
Faisal Latif9d5ab132009-03-06 15:15:01 -0800313 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
314 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800315 return 0;
316}
317
318
319/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800320 * form_cm_frame - get a free packet and build empty frame Use
321 * node info to build.
322 */
Chien Tung6098d102008-11-21 20:51:01 -0600323static void form_cm_frame(struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700324 struct nes_cm_node *cm_node, void *options, u32 optionsize,
325 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800326{
327 struct tcphdr *tcph;
328 struct iphdr *iph;
329 struct ethhdr *ethh;
330 u8 *buf;
331 u16 packetsize = sizeof(*iph);
332
333 packetsize += sizeof(*tcph);
334 packetsize += optionsize + datasize;
335
336 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
337
338 skb->len = 0;
339 buf = skb_put(skb, packetsize + ETH_HLEN);
340
341 ethh = (struct ethhdr *) buf;
342 buf += ETH_HLEN;
343
344 iph = (struct iphdr *)buf;
345 buf += sizeof(*iph);
346 tcph = (struct tcphdr *)buf;
347 skb_reset_mac_header(skb);
348 skb_set_network_header(skb, ETH_HLEN);
349 skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
350 buf += sizeof(*tcph);
351
352 skb->ip_summed = CHECKSUM_PARTIAL;
353 skb->protocol = htons(0x800);
354 skb->data_len = 0;
355 skb->mac_len = ETH_HLEN;
356
357 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
358 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
359 ethh->h_proto = htons(0x0800);
360
361 iph->version = IPVERSION;
362 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
363 iph->tos = 0;
364 iph->tot_len = htons(packetsize);
365 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
366
367 iph->frag_off = htons(0x4000);
368 iph->ttl = 0x40;
369 iph->protocol = 0x06; /* IPPROTO_TCP */
370
371 iph->saddr = htonl(cm_node->loc_addr);
372 iph->daddr = htonl(cm_node->rem_addr);
373
374 tcph->source = htons(cm_node->loc_port);
375 tcph->dest = htons(cm_node->rem_port);
376 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
377
378 if (flags & SET_ACK) {
379 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
380 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
381 tcph->ack = 1;
382 } else
383 tcph->ack_seq = 0;
384
385 if (flags & SET_SYN) {
386 cm_node->tcp_cntxt.loc_seq_num++;
387 tcph->syn = 1;
388 } else
Faisal Latif6492cdf2008-07-24 20:50:45 -0700389 cm_node->tcp_cntxt.loc_seq_num += datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800390
Faisal Latif6492cdf2008-07-24 20:50:45 -0700391 if (flags & SET_FIN) {
392 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800393 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700394 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800395
396 if (flags & SET_RST)
397 tcph->rst = 1;
398
399 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
400 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
401 tcph->urg_ptr = 0;
402 if (optionsize)
403 memcpy(buf, options, optionsize);
404 buf += optionsize;
405 if (datasize)
406 memcpy(buf, data, datasize);
407
408 skb_shinfo(skb)->nr_frags = 0;
409 cm_packets_created++;
410
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800411}
412
413
414/**
415 * print_core - dump a cm core
416 */
417static void print_core(struct nes_cm_core *core)
418{
419 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
420 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
421 if (!core)
422 return;
423 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800424
425 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
426
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800427 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
428 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
429
430 nes_debug(NES_DBG_CM, "core : %p \n", core);
431
432 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
433}
434
435
436/**
437 * schedule_nes_timer
438 * note - cm_node needs to be protected before calling this. Encase in:
439 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
440 */
441int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
442 enum nes_timer_type type, int send_retrans,
443 int close_when_complete)
444{
445 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700446 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800447 struct nes_timer_entry *new_send;
448 int ret = 0;
449 u32 was_timer_set;
450
451 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
452 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800453 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800454
455 /* new_send->timetosend = currenttime */
456 new_send->retrycount = NES_DEFAULT_RETRYS;
457 new_send->retranscount = NES_DEFAULT_RETRANS;
458 new_send->skb = skb;
459 new_send->timetosend = jiffies;
460 new_send->type = type;
461 new_send->netdev = cm_node->netdev;
462 new_send->send_retrans = send_retrans;
463 new_send->close_when_complete = close_when_complete;
464
465 if (type == NES_TIMER_TYPE_CLOSE) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700466 new_send->timetosend += (HZ/10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800467 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700468 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800469 WARN_ON(1);
470 return -EINVAL;
471 }
472 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800473 }
474
475 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700476 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800477 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700478 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
479 cm_node->send_entry = new_send;
480 add_ref_cm_node(cm_node);
481 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
482 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800483
484 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
485 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700486 nes_debug(NES_DBG_CM, "Error sending packet %p "
487 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800488 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700489 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800490 } else {
491 cm_packets_sent++;
492 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700493 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800494 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700495 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800496 return ret;
497 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800498 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800499 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800500
501 was_timer_set = timer_pending(&cm_core->tcp_timer);
502
503 if (!was_timer_set) {
504 cm_core->tcp_timer.expires = new_send->timetosend;
505 add_timer(&cm_core->tcp_timer);
506 }
507
508 return ret;
509}
510
Faisal Latif9d5ab132009-03-06 15:15:01 -0800511static void nes_retrans_expired(struct nes_cm_node *cm_node)
512{
Faisal Latif68237a02009-06-22 22:53:28 -0700513 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latifdae58722010-08-14 21:04:56 +0000514 enum nes_cm_node_state state = cm_node->state;
515 cm_node->state = NES_CM_STATE_CLOSED;
516 switch (state) {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800517 case NES_CM_STATE_SYN_RCVD:
518 case NES_CM_STATE_CLOSING:
519 rem_ref_cm_node(cm_node->cm_core, cm_node);
520 break;
521 case NES_CM_STATE_LAST_ACK:
522 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700523 if (cm_node->cm_id)
524 cm_id->rem_ref(cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800525 send_reset(cm_node, NULL);
526 break;
527 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800528 add_ref_cm_node(cm_node);
529 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800530 create_event(cm_node, NES_CM_EVENT_ABORTED);
531 }
532}
533
534static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
535{
536 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
537 struct iw_cm_id *cm_id = cm_node->cm_id;
538 struct nes_qp *nesqp;
539 unsigned long qplockflags;
540
541 if (!recv_entry)
542 return;
543 nesqp = (struct nes_qp *)recv_entry->skb;
544 if (nesqp) {
545 spin_lock_irqsave(&nesqp->lock, qplockflags);
546 if (nesqp->cm_id) {
547 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
548 "refcount = %d: HIT A "
549 "NES_TIMER_TYPE_CLOSE with something "
550 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
551 atomic_read(&nesqp->refcount));
552 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
553 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
554 nesqp->ibqp_state = IB_QPS_ERR;
555 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
556 nes_cm_disconn(nesqp);
557 } else {
558 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
559 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
560 "refcount = %d: HIT A "
561 "NES_TIMER_TYPE_CLOSE with nothing "
562 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
563 atomic_read(&nesqp->refcount));
564 }
565 } else if (rem_node) {
566 /* TIME_WAIT state */
567 rem_ref_cm_node(cm_node->cm_core, cm_node);
568 }
569 if (cm_node->cm_id)
570 cm_id->rem_ref(cm_id);
571 kfree(recv_entry);
572 cm_node->recv_entry = NULL;
573}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800574
575/**
576 * nes_cm_timer_tick
577 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700578static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800579{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800580 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800581 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800582 struct nes_cm_node *cm_node;
583 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800584 struct list_head *list_core_temp;
585 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800586 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800587 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700588 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800589 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800590
Faisal Latif879e5bd2008-11-21 20:50:41 -0600591 struct list_head timer_list;
592 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800593 spin_lock_irqsave(&cm_core->ht_lock, flags);
594
Faisal Latif6492cdf2008-07-24 20:50:45 -0700595 list_for_each_safe(list_node, list_core_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600596 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800597 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800598 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600599 add_ref_cm_node(cm_node);
600 list_add(&cm_node->timer_entry, &timer_list);
601 }
602 }
603 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
604
605 list_for_each_safe(list_node, list_core_temp, &timer_list) {
606 cm_node = container_of(list_node, struct nes_cm_node,
607 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800608 recv_entry = cm_node->recv_entry;
609
610 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700611 if (time_after(recv_entry->timetosend, jiffies)) {
612 if (nexttimeout > recv_entry->timetosend ||
Faisal Latif9d5ab132009-03-06 15:15:01 -0800613 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800614 nexttimeout = recv_entry->timetosend;
615 settimer = 1;
616 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800617 } else
618 handle_recv_entry(cm_node, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800619 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800620
621 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700622 do {
623 send_entry = cm_node->send_entry;
624 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600625 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800626 if (time_after(send_entry->timetosend, jiffies)) {
627 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700628 if ((nexttimeout >
629 send_entry->timetosend) ||
630 !settimer) {
631 nexttimeout =
632 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800633 settimer = 1;
634 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800635 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700636 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800637 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800638 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800639 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700640
641 if ((cm_node->state == NES_CM_STATE_TSA) ||
642 (cm_node->state == NES_CM_STATE_CLOSED)) {
643 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600644 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800645 }
646
Faisal Latif6492cdf2008-07-24 20:50:45 -0700647 if (!send_entry->retranscount ||
648 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800649 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700650 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800651
Faisal Latif6492cdf2008-07-24 20:50:45 -0700652 spin_unlock_irqrestore(
653 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800654 nes_retrans_expired(cm_node);
655 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700656 spin_lock_irqsave(&cm_node->retrans_list_lock,
657 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600658 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800659 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800660 atomic_inc(&send_entry->skb->users);
661 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700662 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
663 "for node %p, jiffies = %lu, time to send = "
664 "%lu, retranscount = %u, send_entry->seq_num = "
665 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
666 "0x%08X\n", send_entry, cm_node, jiffies,
667 send_entry->timetosend,
668 send_entry->retranscount,
669 send_entry->seq_num,
670 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800671
Faisal Latif6492cdf2008-07-24 20:50:45 -0700672 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
673 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800674 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700675 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800676 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700677 nes_debug(NES_DBG_CM, "rexmit failed for "
678 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800679 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800680 send_entry->retrycount--;
681 nexttimeout = jiffies + NES_SHORT_TIME;
682 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600683 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800684 } else {
685 cm_packets_sent++;
686 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700687 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
688 "%u, retry count = %u.\n",
689 send_entry->retranscount,
690 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800691 if (send_entry->send_retrans) {
692 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700693 timetosend = (NES_RETRY_TIMEOUT <<
694 (NES_DEFAULT_RETRANS - send_entry->retranscount));
695
Faisal Latif6492cdf2008-07-24 20:50:45 -0700696 send_entry->timetosend = jiffies +
Faisal Latif4e9c3902009-04-27 13:39:36 -0700697 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700698 if (nexttimeout > send_entry->timetosend ||
699 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800700 nexttimeout = send_entry->timetosend;
701 settimer = 1;
702 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800703 } else {
704 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700705 close_when_complete =
706 send_entry->close_when_complete;
707 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
708 cm_node, cm_node->state);
709 free_retrans_entry(cm_node);
710 if (close_when_complete)
711 rem_ref_cm_node(cm_node->cm_core,
712 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800713 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700714 } while (0);
715
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800716 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700717 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800718 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800719
720 if (settimer) {
721 if (!timer_pending(&cm_core->tcp_timer)) {
722 cm_core->tcp_timer.expires = nexttimeout;
723 add_timer(&cm_core->tcp_timer);
724 }
725 }
726}
727
728
729/**
730 * send_syn
731 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700732static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
733 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800734{
735 int ret;
736 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800737 char optionsbuffer[sizeof(struct option_mss) +
Faisal Latif6492cdf2008-07-24 20:50:45 -0700738 sizeof(struct option_windowscale) + sizeof(struct option_base) +
739 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800740
741 int optionssize = 0;
742 /* Sending MSS option */
743 union all_known_options *options;
744
745 if (!cm_node)
746 return -EINVAL;
747
748 options = (union all_known_options *)&optionsbuffer[optionssize];
749 options->as_mss.optionnum = OPTION_NUMBER_MSS;
750 options->as_mss.length = sizeof(struct option_mss);
751 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
752 optionssize += sizeof(struct option_mss);
753
754 options = (union all_known_options *)&optionsbuffer[optionssize];
755 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
756 options->as_windowscale.length = sizeof(struct option_windowscale);
757 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
758 optionssize += sizeof(struct option_windowscale);
759
Faisal Latif6492cdf2008-07-24 20:50:45 -0700760 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800761 options = (union all_known_options *)&optionsbuffer[optionssize];
762 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
763 options->as_base.length = sizeof(struct option_base);
764 optionssize += sizeof(struct option_base);
765 /* we need the size to be a multiple of 4 */
766 options = (union all_known_options *)&optionsbuffer[optionssize];
767 options->as_end = 1;
768 optionssize += 1;
769 options = (union all_known_options *)&optionsbuffer[optionssize];
770 options->as_end = 1;
771 optionssize += 1;
772 }
773
774 options = (union all_known_options *)&optionsbuffer[optionssize];
775 options->as_end = OPTION_NUMBER_END;
776 optionssize += 1;
777
Faisal Latif6492cdf2008-07-24 20:50:45 -0700778 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800779 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800780 if (!skb) {
781 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
782 return -1;
783 }
784
785 if (sendack)
786 flags |= SET_ACK;
787
788 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
789 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
790
791 return ret;
792}
793
794
795/**
796 * send_reset
797 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700798static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800799{
800 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800801 int flags = SET_RST | SET_ACK;
802
Faisal Latif6492cdf2008-07-24 20:50:45 -0700803 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800804 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800805 if (!skb) {
806 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -0800807 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800808 }
809
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800810 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
811 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
812
813 return ret;
814}
815
816
817/**
818 * send_ack
819 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700820static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800821{
822 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700823
824 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800825 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800826
827 if (!skb) {
828 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
829 return -1;
830 }
831
832 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
833 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
834
835 return ret;
836}
837
838
839/**
840 * send_fin
841 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700842static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800843{
844 int ret;
845
846 /* if we didn't get a frame get one */
847 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800848 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800849
850 if (!skb) {
851 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
852 return -1;
853 }
854
855 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
856 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
857
858 return ret;
859}
860
861
862/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800863 * find_node - find a cm node that matches the reference cm node
864 */
865static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
866 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
867{
868 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800869 struct list_head *hte;
870 struct nes_cm_node *cm_node;
871
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800872 /* get a handle on the hte */
873 hte = &cm_core->connected_nodes;
874
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800875 /* walk list and find cm_node associated with this session ID */
876 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700877 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800878 /* compare quad, return node handle if a match */
879 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
880 cm_node->loc_addr, cm_node->loc_port,
881 loc_addr, loc_port,
882 cm_node->rem_addr, cm_node->rem_port,
883 rem_addr, rem_port);
884 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
885 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
886 add_ref_cm_node(cm_node);
887 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
888 return cm_node;
889 }
890 }
891 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
892
893 /* no owner node */
894 return NULL;
895}
896
897
898/**
899 * find_listener - find a cm node listening on this addr-port pair
900 */
901static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
902 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
903{
904 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800905 struct nes_cm_listener *listen_node;
906
907 /* walk list and find cm_node associated with this session ID */
908 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700909 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800910 /* compare node pair, return node handle if a match */
911 if (((listen_node->loc_addr == dst_addr) ||
912 listen_node->loc_addr == 0x00000000) &&
913 (listen_node->loc_port == dst_port) &&
914 (listener_state & listen_node->listener_state)) {
915 atomic_inc(&listen_node->ref_count);
916 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
917 return listen_node;
918 }
919 }
920 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
921
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800922 /* no listener */
923 return NULL;
924}
925
926
927/**
928 * add_hte_node - add a cm node to the hash table
929 */
930static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
931{
932 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800933 struct list_head *hte;
934
935 if (!cm_node || !cm_core)
936 return -EINVAL;
937
Faisal Latif6492cdf2008-07-24 20:50:45 -0700938 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
939 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800940
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800941 spin_lock_irqsave(&cm_core->ht_lock, flags);
942
943 /* get a handle on the hash table element (list head for this slot) */
944 hte = &cm_core->connected_nodes;
945 list_add_tail(&cm_node->list, hte);
946 atomic_inc(&cm_core->ht_node_cnt);
947
948 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
949
950 return 0;
951}
952
953
954/**
955 * mini_cm_dec_refcnt_listen
956 */
957static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700958 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800959{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800960 int ret = -EINVAL;
961 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800962 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700963 struct list_head *list_pos = NULL;
964 struct list_head *list_temp = NULL;
965 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -0600966 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700967
968 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
969 "refcnt=%d\n", listener, free_hanging_nodes,
970 atomic_read(&listener->ref_count));
971 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -0600972 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700973 if (free_hanging_nodes) {
974 spin_lock_irqsave(&cm_core->ht_lock, flags);
975 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600976 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700977 cm_node = container_of(list_pos, struct nes_cm_node,
978 list);
979 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -0600980 (!cm_node->accelerated)) {
981 add_ref_cm_node(cm_node);
982 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700983 }
984 }
985 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
986 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600987
988 list_for_each_safe(list_pos, list_temp, &reset_list) {
989 cm_node = container_of(list_pos, struct nes_cm_node,
Faisal Latif9d5ab132009-03-06 15:15:01 -0800990 reset_entry);
991 {
992 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -0800993 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800994 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
995 rem_ref_cm_node(cm_node->cm_core, cm_node);
996 } else {
997 if (!loopback) {
998 cleanup_retrans_entry(cm_node);
999 err = send_reset(cm_node, NULL);
1000 if (err) {
1001 cm_node->state =
1002 NES_CM_STATE_CLOSED;
1003 WARN_ON(1);
1004 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08001005 old_state = cm_node->state;
1006 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1007 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1008 rem_ref_cm_node(
1009 cm_node->cm_core,
1010 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001011 }
1012 } else {
1013 struct nes_cm_event event;
1014
1015 event.cm_node = loopback;
1016 event.cm_info.rem_addr =
1017 loopback->rem_addr;
1018 event.cm_info.loc_addr =
1019 loopback->loc_addr;
1020 event.cm_info.rem_port =
1021 loopback->rem_port;
1022 event.cm_info.loc_port =
1023 loopback->loc_port;
1024 event.cm_info.cm_id = loopback->cm_id;
Faisal Latif43093b92010-02-12 19:57:04 +00001025 add_ref_cm_node(loopback);
1026 loopback->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001027 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001028 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001029
Faisal Latif9d5ab132009-03-06 15:15:01 -08001030 rem_ref_cm_node(cm_node->cm_core,
1031 cm_node);
1032
1033 }
1034 }
1035 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001036 }
1037
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001038 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1039 if (!atomic_dec_return(&listener->ref_count)) {
1040 list_del(&listener->list);
1041
1042 /* decrement our listen node count */
1043 atomic_dec(&cm_core->listen_node_cnt);
1044
1045 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1046
1047 if (listener->nesvnic) {
1048 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1049 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1050 }
1051
1052 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1053
1054 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001055 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001056 ret = 0;
Faisal Latif6e10d2e2010-02-12 19:55:03 +00001057 atomic_inc(&cm_listens_destroyed);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001058 } else {
1059 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1060 }
1061 if (listener) {
1062 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1063 nes_debug(NES_DBG_CM, "destroying listener (%p)"
1064 " with non-zero pending accepts=%u\n",
1065 listener, atomic_read(&listener->pend_accepts_cnt));
1066 }
1067
1068 return ret;
1069}
1070
1071
1072/**
1073 * mini_cm_del_listen
1074 */
1075static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1076 struct nes_cm_listener *listener)
1077{
1078 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1079 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1080 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1081}
1082
1083
1084/**
1085 * mini_cm_accelerated
1086 */
1087static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1088 struct nes_cm_node *cm_node)
1089{
1090 u32 was_timer_set;
1091 cm_node->accelerated = 1;
1092
1093 if (cm_node->accept_pend) {
1094 BUG_ON(!cm_node->listener);
1095 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001096 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001097 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1098 }
1099
1100 was_timer_set = timer_pending(&cm_core->tcp_timer);
1101 if (!was_timer_set) {
1102 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1103 add_timer(&cm_core->tcp_timer);
1104 }
1105
1106 return 0;
1107}
1108
1109
1110/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001111 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001112 */
Faisal Latif7a576df2009-12-09 15:54:33 -08001113static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001114{
1115 struct rtable *rt;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001116 struct neighbour *neigh;
Faisal Latif7a576df2009-12-09 15:54:33 -08001117 int rc = arpindex;
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001118 struct net_device *netdev;
Faisal Latif7a576df2009-12-09 15:54:33 -08001119 struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001120
David S. Miller78fbfd82011-03-12 00:00:52 -05001121 rt = ip_route_output(&init_net, htonl(dst_ip), 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001122 if (IS_ERR(rt)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001123 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001124 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001125 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001126 }
1127
Roland Dreiercf55bb22011-03-24 17:13:20 -07001128 if (netif_is_bond_slave(nesvnic->netdev))
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001129 netdev = nesvnic->netdev->master;
1130 else
1131 netdev = nesvnic->netdev;
1132
1133 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, netdev);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001134 if (neigh) {
1135 if (neigh->nud_state & NUD_VALID) {
1136 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001137 " is %pM, Gateway is 0x%08X \n", dst_ip,
1138 neigh->ha, ntohl(rt->rt_gateway));
Faisal Latif7a576df2009-12-09 15:54:33 -08001139
1140 if (arpindex >= 0) {
1141 if (!memcmp(nesadapter->arp_table[arpindex].mac_addr,
1142 neigh->ha, ETH_ALEN)){
1143 /* Mac address same as in nes_arp_table */
1144 neigh_release(neigh);
1145 ip_rt_put(rt);
1146 return rc;
1147 }
1148
1149 nes_manage_arp_cache(nesvnic->netdev,
1150 nesadapter->arp_table[arpindex].mac_addr,
1151 dst_ip, NES_ARP_DELETE);
1152 }
1153
Bob Sharp7191a0a2008-10-03 12:21:19 -07001154 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1155 dst_ip, NES_ARP_ADD);
1156 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1157 NES_ARP_RESOLVE);
1158 }
1159 neigh_release(neigh);
1160 }
1161
1162 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
David S. Miller69cce1d2011-07-17 23:09:49 -07001163 neigh_event_send(dst_get_neighbour(&rt->dst), NULL);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001164
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001165 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001166 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001167}
1168
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001169/**
1170 * make_cm_node - create a new instance of a cm node
1171 */
1172static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1173 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1174 struct nes_cm_listener *listener)
1175{
1176 struct nes_cm_node *cm_node;
1177 struct timespec ts;
Faisal Latif7a576df2009-12-09 15:54:33 -08001178 int oldarpindex = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001179 int arpindex = 0;
1180 struct nes_device *nesdev;
1181 struct nes_adapter *nesadapter;
1182
1183 /* create an hte and cm_node for this instance */
1184 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1185 if (!cm_node)
1186 return NULL;
1187
1188 /* set our node specific transport info */
1189 cm_node->loc_addr = cm_info->loc_addr;
1190 cm_node->rem_addr = cm_info->rem_addr;
1191 cm_node->loc_port = cm_info->loc_port;
1192 cm_node->rem_port = cm_info->rem_port;
1193 cm_node->send_write0 = send_first;
Harvey Harrison63779432008-10-31 00:56:00 -07001194 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1195 &cm_node->loc_addr, cm_node->loc_port,
1196 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001197 cm_node->listener = listener;
1198 cm_node->netdev = nesvnic->netdev;
1199 cm_node->cm_id = cm_info->cm_id;
1200 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1201
Faisal Latif6492cdf2008-07-24 20:50:45 -07001202 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1203 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001204
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001205 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001206
1207 cm_node->loopbackpartner = NULL;
1208 atomic_set(&cm_node->ref_count, 1);
1209 /* associate our parent CM core */
1210 cm_node->cm_core = cm_core;
1211 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1212 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1213 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1214 NES_CM_DEFAULT_RCV_WND_SCALE;
1215 ts = current_kernel_time();
1216 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1217 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Chien Tungf2b2b592008-03-20 19:55:30 -05001218 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001219 cm_node->tcp_cntxt.rcv_nxt = 0;
1220 /* get a unique session ID , add thread_id to an upcounter to handle race */
1221 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001222 cm_node->conn_type = cm_info->conn_type;
1223 cm_node->apbvt_set = 0;
1224 cm_node->accept_pend = 0;
1225
1226 cm_node->nesvnic = nesvnic;
1227 /* get some device handles, for arp lookup */
1228 nesdev = nesvnic->nesdev;
1229 nesadapter = nesdev->nesadapter;
1230
1231 cm_node->loopbackpartner = NULL;
Faisal Latif7a576df2009-12-09 15:54:33 -08001232
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001233 /* get the mac addr for the remote node */
Chien Tung1ee86552008-11-21 20:51:04 -06001234 if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1235 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
Faisal Latif7a576df2009-12-09 15:54:33 -08001236 else {
1237 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1238 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);
1239
1240 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001241 if (arpindex < 0) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001242 kfree(cm_node);
1243 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001244 }
1245
1246 /* copy the mac addr to node context */
1247 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001248 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1249 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001250
1251 add_hte_node(cm_core, cm_node);
1252 atomic_inc(&cm_nodes_created);
1253
1254 return cm_node;
1255}
1256
1257
1258/**
1259 * add_ref_cm_node - destroy an instance of a cm node
1260 */
1261static int add_ref_cm_node(struct nes_cm_node *cm_node)
1262{
1263 atomic_inc(&cm_node->ref_count);
1264 return 0;
1265}
1266
1267
1268/**
1269 * rem_ref_cm_node - destroy an instance of a cm node
1270 */
1271static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001272 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001273{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001274 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001275 struct nes_qp *nesqp;
1276
1277 if (!cm_node)
1278 return -EINVAL;
1279
1280 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1281 if (atomic_dec_return(&cm_node->ref_count)) {
1282 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1283 return 0;
1284 }
1285 list_del(&cm_node->list);
1286 atomic_dec(&cm_core->ht_node_cnt);
1287 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1288
1289 /* if the node is destroyed before connection was accelerated */
1290 if (!cm_node->accelerated && cm_node->accept_pend) {
1291 BUG_ON(!cm_node->listener);
1292 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1293 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1294 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001295 WARN_ON(cm_node->send_entry);
1296 if (cm_node->recv_entry)
1297 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001298 if (cm_node->listener) {
1299 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1300 } else {
1301 if (cm_node->apbvt_set && cm_node->nesvnic) {
1302 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001303 PCI_FUNC(
1304 cm_node->nesvnic->nesdev->pcidev->devfn),
1305 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001306 }
1307 }
1308
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001309 atomic_dec(&cm_core->node_cnt);
1310 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001311 nesqp = cm_node->nesqp;
1312 if (nesqp) {
1313 nesqp->cm_node = NULL;
1314 nes_rem_ref(&nesqp->ibqp);
1315 cm_node->nesqp = NULL;
1316 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001317
Faisal Latif6492cdf2008-07-24 20:50:45 -07001318 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001319 return 0;
1320}
1321
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001322/**
1323 * process_options
1324 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001325static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1326 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001327{
1328 u32 tmp;
1329 u32 offset = 0;
1330 union all_known_options *all_options;
1331 char got_mss_option = 0;
1332
1333 while (offset < optionsize) {
1334 all_options = (union all_known_options *)(optionsloc + offset);
1335 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001336 case OPTION_NUMBER_END:
1337 offset = optionsize;
1338 break;
1339 case OPTION_NUMBER_NONE:
1340 offset += 1;
1341 continue;
1342 case OPTION_NUMBER_MSS:
1343 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1344 "Size: %d\n", __func__,
1345 all_options->as_mss.length, offset, optionsize);
1346 got_mss_option = 1;
1347 if (all_options->as_mss.length != 4) {
1348 return 1;
1349 } else {
1350 tmp = ntohs(all_options->as_mss.mss);
1351 if (tmp > 0 && tmp <
1352 cm_node->tcp_cntxt.mss)
1353 cm_node->tcp_cntxt.mss = tmp;
1354 }
1355 break;
1356 case OPTION_NUMBER_WINDOW_SCALE:
1357 cm_node->tcp_cntxt.snd_wscale =
1358 all_options->as_windowscale.shiftcount;
1359 break;
1360 case OPTION_NUMBER_WRITE0:
1361 cm_node->send_write0 = 1;
1362 break;
1363 default:
1364 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1365 all_options->as_base.optionnum);
1366 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001367 }
1368 offset += all_options->as_base.length;
1369 }
1370 if ((!got_mss_option) && (syn_packet))
1371 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1372 return 0;
1373}
1374
Faisal Latif6492cdf2008-07-24 20:50:45 -07001375static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001376{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001377 atomic_inc(&cm_accel_dropped_pkts);
1378 dev_kfree_skb_any(skb);
1379}
1380
Faisal Latif9d5ab132009-03-06 15:15:01 -08001381static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001382{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001383 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1384 "refcnt=%d\n", cm_node, cm_node->state,
1385 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001386 switch (cm_node->state) {
1387 case NES_CM_STATE_SYN_RCVD:
1388 case NES_CM_STATE_SYN_SENT:
1389 case NES_CM_STATE_ESTABLISHED:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001390 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001391 cm_node->tcp_cntxt.rcv_nxt++;
1392 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001393 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001394 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001395 break;
Faisal Latifb1190d32009-12-09 15:54:32 -08001396 case NES_CM_STATE_MPAREQ_SENT:
1397 create_event(cm_node, NES_CM_EVENT_ABORTED);
1398 cm_node->tcp_cntxt.rcv_nxt++;
1399 cleanup_retrans_entry(cm_node);
1400 cm_node->state = NES_CM_STATE_CLOSED;
1401 add_ref_cm_node(cm_node);
1402 send_reset(cm_node, NULL);
1403 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001404 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001405 cm_node->tcp_cntxt.rcv_nxt++;
1406 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001407 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001408 send_ack(cm_node, NULL);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001409 /* Wait for ACK as this is simultaneous close..
Faisal Latif9d5ab132009-03-06 15:15:01 -08001410 * After we receive ACK, do not send anything..
1411 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001412 break;
1413 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001414 cm_node->tcp_cntxt.rcv_nxt++;
1415 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001416 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001417 send_ack(cm_node, NULL);
1418 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1419 break;
1420 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001421 cm_node->tcp_cntxt.rcv_nxt++;
1422 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001423 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001424 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001425 break;
1426 case NES_CM_STATE_TSA:
1427 default:
1428 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1429 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001430 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001431 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001432}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001433
Faisal Latif6492cdf2008-07-24 20:50:45 -07001434
1435static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1436 struct tcphdr *tcph)
1437{
1438
1439 int reset = 0; /* whether to send reset in case of err.. */
1440 atomic_inc(&cm_resets_recvd);
1441 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1442 " refcnt=%d\n", cm_node, cm_node->state,
1443 atomic_read(&cm_node->ref_count));
1444 cleanup_retrans_entry(cm_node);
1445 switch (cm_node->state) {
1446 case NES_CM_STATE_SYN_SENT:
1447 case NES_CM_STATE_MPAREQ_SENT:
1448 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1449 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1450 cm_node->listener, cm_node->state);
1451 active_open_err(cm_node, skb, reset);
1452 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001453 case NES_CM_STATE_MPAREQ_RCVD:
Dan Carpenter4d8d6382010-09-15 18:32:39 +02001454 atomic_inc(&cm_node->passive_state);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001455 dev_kfree_skb_any(skb);
1456 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001457 case NES_CM_STATE_ESTABLISHED:
1458 case NES_CM_STATE_SYN_RCVD:
1459 case NES_CM_STATE_LISTENING:
1460 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1461 passive_open_err(cm_node, skb, reset);
1462 break;
1463 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001464 active_open_err(cm_node, skb, reset);
1465 break;
1466 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001467 drop_packet(skb);
1468 break;
Faisal Latifdae58722010-08-14 21:04:56 +00001469 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif68237a02009-06-22 22:53:28 -07001470 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001471 case NES_CM_STATE_LAST_ACK:
1472 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001473 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001474 cm_node->state = NES_CM_STATE_CLOSED;
1475 rem_ref_cm_node(cm_node->cm_core, cm_node);
1476 drop_packet(skb);
1477 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001478 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001479 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001480 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001481 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001482}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001483
Faisal Latif9d5ab132009-03-06 15:15:01 -08001484
1485static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001486{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001487
Faisal Latif9d5ab132009-03-06 15:15:01 -08001488 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001489 int datasize = skb->len;
1490 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001491
1492 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1493 u32 res_type;
1494 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1495 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001496 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001497 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001498 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1499 "cm_node=%p listener=%p state=%d\n", __func__,
1500 __LINE__, cm_node, cm_node->listener,
1501 cm_node->state);
1502 active_open_err(cm_node, skb, 1);
1503 } else {
1504 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001505 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001506 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001507 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001508
1509 switch (cm_node->state) {
1510 case NES_CM_STATE_ESTABLISHED:
1511 if (res_type == NES_MPA_REQUEST_REJECT) {
1512 /*BIG problem as we are receiving the MPA.. So should
1513 * not be REJECT.. This is Passive Open.. We can
1514 * only receive it Reject for Active Open...*/
1515 WARN_ON(1);
1516 }
1517 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1518 type = NES_CM_EVENT_MPA_REQ;
1519 atomic_set(&cm_node->passive_state,
1520 NES_PASSIVE_STATE_INDICATED);
1521 break;
1522 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001523 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001524 if (res_type == NES_MPA_REQUEST_REJECT) {
1525 type = NES_CM_EVENT_MPA_REJECT;
1526 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1527 } else {
1528 type = NES_CM_EVENT_CONNECTED;
1529 cm_node->state = NES_CM_STATE_TSA;
1530 }
1531
1532 break;
1533 default:
1534 WARN_ON(1);
1535 break;
1536 }
1537 dev_kfree_skb_any(skb);
1538 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001539}
1540
1541static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1542{
1543 switch (cm_node->state) {
1544 case NES_CM_STATE_SYN_SENT:
1545 case NES_CM_STATE_MPAREQ_SENT:
1546 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1547 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1548 cm_node->listener, cm_node->state);
1549 active_open_err(cm_node, skb, 1);
1550 break;
1551 case NES_CM_STATE_ESTABLISHED:
1552 case NES_CM_STATE_SYN_RCVD:
1553 passive_open_err(cm_node, skb, 1);
1554 break;
1555 case NES_CM_STATE_TSA:
1556 default:
1557 drop_packet(skb);
1558 }
1559}
1560
1561static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1562 struct sk_buff *skb)
1563{
1564 int err;
1565
1566 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1567 if (err)
1568 active_open_err(cm_node, skb, 1);
1569
1570 return err;
1571}
1572
1573static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1574 struct sk_buff *skb)
1575{
1576 int err = 0;
1577 u32 seq;
1578 u32 ack_seq;
1579 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1580 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1581 u32 rcv_wnd;
1582 seq = ntohl(tcph->seq);
1583 ack_seq = ntohl(tcph->ack_seq);
1584 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1585 if (ack_seq != loc_seq_num)
1586 err = 1;
Faisal Latifc11470f2009-04-27 13:38:31 -07001587 else if (!between(seq, rcv_nxt, (rcv_nxt+rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001588 err = 1;
1589 if (err) {
1590 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1591 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1592 cm_node->listener, cm_node->state);
1593 indicate_pkt_err(cm_node, skb);
1594 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1595 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1596 rcv_wnd);
1597 }
1598 return err;
1599}
1600
1601/*
1602 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1603 * is created with a listener or it may comein as rexmitted packet which in
1604 * that case will be just dropped.
1605 */
1606
1607static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1608 struct tcphdr *tcph)
1609{
1610 int ret;
1611 u32 inc_sequence;
1612 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001613
1614 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001615 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001616 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001617
Faisal Latif6492cdf2008-07-24 20:50:45 -07001618 switch (cm_node->state) {
1619 case NES_CM_STATE_SYN_SENT:
1620 case NES_CM_STATE_MPAREQ_SENT:
1621 /* Rcvd syn on active open connection*/
1622 active_open_err(cm_node, skb, 1);
1623 break;
1624 case NES_CM_STATE_LISTENING:
1625 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001626 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1627 cm_node->listener->backlog) {
1628 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1629 "pressure \n");
1630 cm_backlog_drops++;
1631 passive_open_err(cm_node, skb, 0);
1632 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001633 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001634 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1635 1);
1636 if (ret) {
1637 passive_open_err(cm_node, skb, 0);
1638 /* drop pkt */
1639 break;
1640 }
1641 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1642 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001643 cm_node->accept_pend = 1;
1644 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1645
Faisal Latif6492cdf2008-07-24 20:50:45 -07001646 cm_node->state = NES_CM_STATE_SYN_RCVD;
1647 send_syn(cm_node, 1, skb);
1648 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001649 case NES_CM_STATE_CLOSED:
1650 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001651 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001652 send_reset(cm_node, skb);
1653 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001654 case NES_CM_STATE_TSA:
1655 case NES_CM_STATE_ESTABLISHED:
1656 case NES_CM_STATE_FIN_WAIT1:
1657 case NES_CM_STATE_FIN_WAIT2:
1658 case NES_CM_STATE_MPAREQ_RCVD:
1659 case NES_CM_STATE_LAST_ACK:
1660 case NES_CM_STATE_CLOSING:
1661 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001662 default:
1663 drop_packet(skb);
1664 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001665 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001666}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001667
Faisal Latif6492cdf2008-07-24 20:50:45 -07001668static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1669 struct tcphdr *tcph)
1670{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001671
Faisal Latif6492cdf2008-07-24 20:50:45 -07001672 int ret;
1673 u32 inc_sequence;
1674 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001675
Faisal Latif6492cdf2008-07-24 20:50:45 -07001676 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001677 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001678 inc_sequence = ntohl(tcph->seq);
1679 switch (cm_node->state) {
1680 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001681 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001682 /* active open */
1683 if (check_syn(cm_node, tcph, skb))
1684 return;
1685 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1686 /* setup options */
1687 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1688 if (ret) {
1689 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1690 cm_node);
1691 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001692 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001693 cleanup_retrans_entry(cm_node);
1694 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1695 send_mpa_request(cm_node, skb);
1696 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1697 break;
1698 case NES_CM_STATE_MPAREQ_RCVD:
1699 /* passive open, so should not be here */
1700 passive_open_err(cm_node, skb, 1);
1701 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001702 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001703 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1704 cleanup_retrans_entry(cm_node);
1705 cm_node->state = NES_CM_STATE_CLOSED;
1706 send_reset(cm_node, skb);
1707 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001708 case NES_CM_STATE_CLOSED:
1709 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1710 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001711 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001712 send_reset(cm_node, skb);
1713 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001714 case NES_CM_STATE_ESTABLISHED:
1715 case NES_CM_STATE_FIN_WAIT1:
1716 case NES_CM_STATE_FIN_WAIT2:
1717 case NES_CM_STATE_LAST_ACK:
1718 case NES_CM_STATE_TSA:
1719 case NES_CM_STATE_CLOSING:
1720 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001721 case NES_CM_STATE_MPAREQ_SENT:
1722 default:
1723 drop_packet(skb);
1724 break;
1725 }
1726}
1727
Faisal Latif109d67e2009-04-27 13:41:06 -07001728static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001729 struct tcphdr *tcph)
1730{
1731 int datasize = 0;
1732 u32 inc_sequence;
Faisal Latif109d67e2009-04-27 13:41:06 -07001733 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06001734 int optionsize;
Faisal Latifabb77252008-11-21 20:50:52 -06001735 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001736
Faisal Latif6492cdf2008-07-24 20:50:45 -07001737 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07001738 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001739
1740 skb_pull(skb, tcph->doff << 2);
1741 inc_sequence = ntohl(tcph->seq);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001742 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001743 switch (cm_node->state) {
1744 case NES_CM_STATE_SYN_RCVD:
1745 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07001746 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001747 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1748 if (ret)
1749 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001750 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1751 cm_node->state = NES_CM_STATE_ESTABLISHED;
1752 if (datasize) {
1753 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001754 handle_rcv_mpa(cm_node, skb);
Faisal Latif109d67e2009-04-27 13:41:06 -07001755 } else /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001756 dev_kfree_skb_any(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001757 break;
1758 case NES_CM_STATE_ESTABLISHED:
1759 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001760 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001761 if (datasize) {
1762 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001763 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001764 } else
1765 drop_packet(skb);
1766 break;
1767 case NES_CM_STATE_MPAREQ_SENT:
1768 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1769 if (datasize) {
1770 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001771 handle_rcv_mpa(cm_node, skb);
Faisal Latif109d67e2009-04-27 13:41:06 -07001772 } else /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001773 dev_kfree_skb_any(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001774 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001775 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001776 cleanup_retrans_entry(cm_node);
1777 cm_node->state = NES_CM_STATE_CLOSED;
1778 send_reset(cm_node, skb);
1779 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001780 case NES_CM_STATE_CLOSED:
1781 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001782 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001783 send_reset(cm_node, skb);
1784 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001785 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07001786 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001787 cleanup_retrans_entry(cm_node);
1788 cm_node->state = NES_CM_STATE_CLOSED;
1789 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001790 rem_ref_cm_node(cm_node->cm_core, cm_node);
1791 drop_packet(skb);
1792 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001793 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001794 cleanup_retrans_entry(cm_node);
1795 drop_packet(skb);
1796 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1797 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001798 case NES_CM_STATE_SYN_SENT:
1799 case NES_CM_STATE_FIN_WAIT2:
1800 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001801 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001802 case NES_CM_STATE_UNKNOWN:
1803 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07001804 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001805 drop_packet(skb);
1806 break;
1807 }
Faisal Latif109d67e2009-04-27 13:41:06 -07001808 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001809}
1810
1811
1812
1813static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1814 struct sk_buff *skb, int optionsize, int passive)
1815{
1816 u8 *optionsloc = (u8 *)&tcph[1];
1817 if (optionsize) {
1818 if (process_options(cm_node, optionsloc, optionsize,
1819 (u32)tcph->syn)) {
1820 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1821 __func__, cm_node);
1822 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06001823 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001824 else
Faisal Latifabb77252008-11-21 20:50:52 -06001825 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001826 return 1;
1827 }
1828 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001829
1830 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1831 cm_node->tcp_cntxt.snd_wscale;
1832
Faisal Latif6492cdf2008-07-24 20:50:45 -07001833 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001834 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001835 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001836}
1837
Faisal Latif6492cdf2008-07-24 20:50:45 -07001838/*
1839 * active_open_err() will send reset() if flag set..
1840 * It will also send ABORT event.
1841 */
1842
1843static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1844 int reset)
1845{
1846 cleanup_retrans_entry(cm_node);
1847 if (reset) {
1848 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1849 "state=%d\n", cm_node, cm_node->state);
1850 add_ref_cm_node(cm_node);
1851 send_reset(cm_node, skb);
1852 } else
1853 dev_kfree_skb_any(skb);
1854
1855 cm_node->state = NES_CM_STATE_CLOSED;
1856 create_event(cm_node, NES_CM_EVENT_ABORTED);
1857}
1858
1859/*
1860 * passive_open_err() will either do a reset() or will free up the skb and
1861 * remove the cm_node.
1862 */
1863
1864static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1865 int reset)
1866{
1867 cleanup_retrans_entry(cm_node);
1868 cm_node->state = NES_CM_STATE_CLOSED;
1869 if (reset) {
1870 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1871 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1872 send_reset(cm_node, skb);
1873 } else {
1874 dev_kfree_skb_any(skb);
1875 rem_ref_cm_node(cm_node->cm_core, cm_node);
1876 }
1877}
1878
1879/*
1880 * free_retrans_entry() routines assumes that the retrans_list_lock has
1881 * been acquired before calling.
1882 */
1883static void free_retrans_entry(struct nes_cm_node *cm_node)
1884{
1885 struct nes_timer_entry *send_entry;
1886 send_entry = cm_node->send_entry;
1887 if (send_entry) {
1888 cm_node->send_entry = NULL;
1889 dev_kfree_skb_any(send_entry->skb);
1890 kfree(send_entry);
1891 rem_ref_cm_node(cm_node->cm_core, cm_node);
1892 }
1893}
1894
1895static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1896{
1897 unsigned long flags;
1898
1899 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1900 free_retrans_entry(cm_node);
1901 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1902}
1903
1904/**
1905 * process_packet
1906 * Returns skb if to be freed, else it will return NULL if already used..
1907 */
1908static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1909 struct nes_cm_core *cm_core)
1910{
1911 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1912 struct tcphdr *tcph = tcp_hdr(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001913 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07001914 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001915 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1916
1917 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1918 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1919 tcph->ack, tcph->rst, tcph->fin);
1920
1921 if (tcph->rst)
1922 pkt_type = NES_PKT_TYPE_RST;
1923 else if (tcph->syn) {
1924 pkt_type = NES_PKT_TYPE_SYN;
1925 if (tcph->ack)
1926 pkt_type = NES_PKT_TYPE_SYNACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001927 } else if (tcph->ack)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001928 pkt_type = NES_PKT_TYPE_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001929 if (tcph->fin)
1930 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001931
1932 switch (pkt_type) {
1933 case NES_PKT_TYPE_SYN:
1934 handle_syn_pkt(cm_node, skb, tcph);
1935 break;
1936 case NES_PKT_TYPE_SYNACK:
1937 handle_synack_pkt(cm_node, skb, tcph);
1938 break;
1939 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07001940 ret = handle_ack_pkt(cm_node, skb, tcph);
1941 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001942 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001943 break;
1944 case NES_PKT_TYPE_RST:
1945 handle_rst_pkt(cm_node, skb, tcph);
1946 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001947 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07001948 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08001949 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07001950 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001951 break;
1952 }
1953}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001954
1955/**
1956 * mini_cm_listen - create a listen node with params
1957 */
1958static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001959 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001960{
1961 struct nes_cm_listener *listener;
1962 unsigned long flags;
1963
1964 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1965 cm_info->loc_addr, cm_info->loc_port);
1966
1967 /* cannot have multiple matching listeners */
1968 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1969 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1970 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1971 /* find automatically incs ref count ??? */
1972 atomic_dec(&listener->ref_count);
1973 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1974 return NULL;
1975 }
1976
1977 if (!listener) {
1978 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1979 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1980 if (!listener) {
1981 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1982 return NULL;
1983 }
1984
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001985 listener->loc_addr = htonl(cm_info->loc_addr);
1986 listener->loc_port = htons(cm_info->loc_port);
1987 listener->reused_node = 0;
1988
1989 atomic_set(&listener->ref_count, 1);
1990 }
1991 /* pasive case */
1992 /* find already inc'ed the ref count */
1993 else {
1994 listener->reused_node = 1;
1995 }
1996
1997 listener->cm_id = cm_info->cm_id;
1998 atomic_set(&listener->pend_accepts_cnt, 0);
1999 listener->cm_core = cm_core;
2000 listener->nesvnic = nesvnic;
2001 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002002
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002003 listener->conn_type = cm_info->conn_type;
2004 listener->backlog = cm_info->backlog;
2005 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
2006
2007 if (!listener->reused_node) {
2008 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2009 list_add(&listener->list, &cm_core->listen_list.list);
2010 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2011 atomic_inc(&cm_core->listen_node_cnt);
2012 }
2013
2014 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
2015 " listener = %p, backlog = %d, cm_id = %p.\n",
2016 cm_info->loc_addr, cm_info->loc_port,
2017 listener, listener->backlog, listener->cm_id);
2018
2019 return listener;
2020}
2021
2022
2023/**
2024 * mini_cm_connect - make a connection node with params
2025 */
Faisal Latif54c86a82008-09-30 14:47:27 -07002026static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002027 struct nes_vnic *nesvnic, u16 private_data_len,
2028 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002029{
2030 int ret = 0;
2031 struct nes_cm_node *cm_node;
2032 struct nes_cm_listener *loopbackremotelistener;
2033 struct nes_cm_node *loopbackremotenode;
2034 struct nes_cm_info loopback_cm_info;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002035 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
2036 struct ietf_mpa_frame *mpa_frame = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002037
2038 /* create a CM connection node */
2039 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2040 if (!cm_node)
2041 return NULL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002042 mpa_frame = &cm_node->mpa_frame;
Faisal Latif1cf078c2009-12-09 15:53:54 -08002043 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002044 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
2045 mpa_frame->rev = IETF_MPA_VERSION;
2046 mpa_frame->priv_data_len = htons(private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002047
Glenn Streiff7495ab62008-04-29 13:46:54 -07002048 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002049 cm_node->tcp_cntxt.client = 1;
2050 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2051
2052 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002053 loopbackremotelistener = find_listener(cm_core,
2054 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2055 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002056 if (loopbackremotelistener == NULL) {
2057 create_event(cm_node, NES_CM_EVENT_ABORTED);
2058 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002059 loopback_cm_info = *cm_info;
2060 loopback_cm_info.loc_port = cm_info->rem_port;
2061 loopback_cm_info.rem_port = cm_info->loc_port;
2062 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002063 loopbackremotenode = make_cm_node(cm_core, nesvnic,
2064 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002065 if (!loopbackremotenode) {
2066 rem_ref_cm_node(cm_node->cm_core, cm_node);
2067 return NULL;
2068 }
2069 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002070 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002071 loopbackremotenode->tcp_cntxt.rcv_wscale =
2072 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002073 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002074 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2075 private_data_len);
2076 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002077
Faisal Latif6492cdf2008-07-24 20:50:45 -07002078 /* we are done handling this state. */
2079 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002080 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002081 cm_node->tcp_cntxt.rcv_nxt =
2082 loopbackremotenode->tcp_cntxt.loc_seq_num;
2083 loopbackremotenode->tcp_cntxt.rcv_nxt =
2084 cm_node->tcp_cntxt.loc_seq_num;
2085 cm_node->tcp_cntxt.max_snd_wnd =
2086 loopbackremotenode->tcp_cntxt.rcv_wnd;
2087 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2088 cm_node->tcp_cntxt.rcv_wnd;
2089 cm_node->tcp_cntxt.snd_wnd =
2090 loopbackremotenode->tcp_cntxt.rcv_wnd;
2091 loopbackremotenode->tcp_cntxt.snd_wnd =
2092 cm_node->tcp_cntxt.rcv_wnd;
2093 cm_node->tcp_cntxt.snd_wscale =
2094 loopbackremotenode->tcp_cntxt.rcv_wscale;
2095 loopbackremotenode->tcp_cntxt.snd_wscale =
2096 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002097 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002098 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2099 }
2100 return cm_node;
2101 }
2102
2103 /* set our node side to client (active) side */
2104 cm_node->tcp_cntxt.client = 1;
2105 /* init our MPA frame ptr */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002106 memcpy(mpa_frame->priv_data, private_data, private_data_len);
2107
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002108 cm_node->mpa_frame_size = mpa_frame_size;
2109
2110 /* send a syn and goto syn sent state */
2111 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002112 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002113
Faisal Latif6492cdf2008-07-24 20:50:45 -07002114 if (ret) {
2115 /* error in sending the syn free up the cm_node struct */
2116 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2117 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2118 cm_node->rem_addr, cm_node->rem_port, cm_node,
2119 cm_node->cm_id);
2120 rem_ref_cm_node(cm_node->cm_core, cm_node);
2121 cm_node = NULL;
2122 }
2123
2124 if (cm_node)
2125 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2126 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2127 cm_node->rem_addr, cm_node->rem_port, cm_node,
2128 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002129
2130 return cm_node;
2131}
2132
2133
2134/**
2135 * mini_cm_accept - accept a connection
2136 * This function is never called
2137 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002138static int mini_cm_accept(struct nes_cm_core *cm_core,
2139 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002140{
2141 return 0;
2142}
2143
2144
2145/**
2146 * mini_cm_reject - reject and teardown a connection
2147 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002148static int mini_cm_reject(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002149 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002150{
2151 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002152 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002153 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002154 struct nes_cm_event event;
2155 struct iw_cm_id *cm_id = cm_node->cm_id;
2156 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002157
Faisal Latif6492cdf2008-07-24 20:50:45 -07002158 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2159 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002160
Faisal Latif6492cdf2008-07-24 20:50:45 -07002161 if (cm_node->tcp_cntxt.client)
2162 return ret;
2163 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002164
Faisal Latif9d5ab132009-03-06 15:15:01 -08002165 if (!loopback) {
2166 passive_state = atomic_add_return(1, &cm_node->passive_state);
2167 if (passive_state == NES_SEND_RESET_EVENT) {
2168 cm_node->state = NES_CM_STATE_CLOSED;
2169 rem_ref_cm_node(cm_core, cm_node);
2170 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002171 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2172 rem_ref_cm_node(cm_core, cm_node);
2173 } else {
2174 ret = send_mpa_reject(cm_node);
2175 if (ret) {
2176 cm_node->state = NES_CM_STATE_CLOSED;
2177 err = send_reset(cm_node, NULL);
2178 if (err)
2179 WARN_ON(1);
2180 } else
2181 cm_id->add_ref(cm_id);
2182 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002183 }
2184 } else {
2185 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002186 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2187 rem_ref_cm_node(cm_core, cm_node);
2188 rem_ref_cm_node(cm_core, loopback);
2189 } else {
2190 event.cm_node = loopback;
2191 event.cm_info.rem_addr = loopback->rem_addr;
2192 event.cm_info.loc_addr = loopback->loc_addr;
2193 event.cm_info.rem_port = loopback->rem_port;
2194 event.cm_info.loc_port = loopback->loc_port;
2195 event.cm_info.cm_id = loopback->cm_id;
2196 cm_event_mpa_reject(&event);
2197 rem_ref_cm_node(cm_core, cm_node);
2198 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002199
Faisal Latifc5a7d482009-12-09 15:54:08 -08002200 cm_id = loopback->cm_id;
2201 rem_ref_cm_node(cm_core, loopback);
2202 cm_id->rem_ref(cm_id);
2203 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002204 }
2205
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002206 return ret;
2207}
2208
2209
2210/**
2211 * mini_cm_close
2212 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002213static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002214{
2215 int ret = 0;
2216
2217 if (!cm_core || !cm_node)
2218 return -EINVAL;
2219
2220 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002221 case NES_CM_STATE_SYN_RCVD:
2222 case NES_CM_STATE_SYN_SENT:
2223 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2224 case NES_CM_STATE_ESTABLISHED:
2225 case NES_CM_STATE_ACCEPTING:
2226 case NES_CM_STATE_MPAREQ_SENT:
2227 case NES_CM_STATE_MPAREQ_RCVD:
2228 cleanup_retrans_entry(cm_node);
2229 send_reset(cm_node, NULL);
2230 break;
2231 case NES_CM_STATE_CLOSE_WAIT:
2232 cm_node->state = NES_CM_STATE_LAST_ACK;
2233 send_fin(cm_node, NULL);
2234 break;
2235 case NES_CM_STATE_FIN_WAIT1:
2236 case NES_CM_STATE_FIN_WAIT2:
2237 case NES_CM_STATE_LAST_ACK:
2238 case NES_CM_STATE_TIME_WAIT:
2239 case NES_CM_STATE_CLOSING:
2240 ret = -1;
2241 break;
2242 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002243 cleanup_retrans_entry(cm_node);
2244 send_reset(cm_node, NULL);
2245 break;
2246 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002247 case NES_CM_STATE_UNKNOWN:
2248 case NES_CM_STATE_INITED:
2249 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002250 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002251 ret = rem_ref_cm_node(cm_core, cm_node);
2252 break;
2253 case NES_CM_STATE_TSA:
2254 if (cm_node->send_entry)
2255 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2256 "send_entry=%p\n", cm_node->send_entry);
2257 ret = rem_ref_cm_node(cm_core, cm_node);
2258 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002259 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002260 return ret;
2261}
2262
2263
2264/**
2265 * recv_pkt - recv an ETHERNET packet, and process it through CM
2266 * node state machine
2267 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002268static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002269 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002270{
2271 struct nes_cm_node *cm_node = NULL;
2272 struct nes_cm_listener *listener = NULL;
2273 struct iphdr *iph;
2274 struct tcphdr *tcph;
2275 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002276 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002277 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002278
Faisal Latif6492cdf2008-07-24 20:50:45 -07002279 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002280 return 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002281 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002282 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002283 }
2284
2285 iph = (struct iphdr *)skb->data;
2286 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002287
2288 nfo.loc_addr = ntohl(iph->daddr);
2289 nfo.loc_port = ntohs(tcph->dest);
2290 nfo.rem_addr = ntohl(iph->saddr);
2291 nfo.rem_port = ntohs(tcph->source);
2292
Harvey Harrison03080e52009-01-10 21:45:42 -08002293 tmp_daddr = cpu_to_be32(iph->daddr);
2294 tmp_saddr = cpu_to_be32(iph->saddr);
2295
Harvey Harrison63779432008-10-31 00:56:00 -07002296 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002297 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002298
Faisal Latif6492cdf2008-07-24 20:50:45 -07002299 do {
2300 cm_node = find_node(cm_core,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002301 nfo.rem_port, nfo.rem_addr,
2302 nfo.loc_port, nfo.loc_addr);
2303
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002304 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002305 /* Only type of packet accepted are for */
2306 /* the PASSIVE open (syn only) */
2307 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002308 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002309 break;
2310 }
2311 listener = find_listener(cm_core, nfo.loc_addr,
2312 nfo.loc_port,
2313 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002314 if (!listener) {
2315 nfo.cm_id = NULL;
2316 nfo.conn_type = 0;
2317 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2318 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002319 break;
2320 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002321 nfo.cm_id = listener->cm_id;
2322 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002323 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2324 listener);
2325 if (!cm_node) {
2326 nes_debug(NES_DBG_CM, "Unable to allocate "
2327 "node\n");
2328 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002329 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002330 dev_kfree_skb_any(skb);
2331 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002332 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002333 if (!tcph->rst && !tcph->fin) {
2334 cm_node->state = NES_CM_STATE_LISTENING;
2335 } else {
2336 cm_packets_dropped++;
2337 rem_ref_cm_node(cm_core, cm_node);
2338 dev_kfree_skb_any(skb);
2339 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002340 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002341 add_ref_cm_node(cm_node);
2342 } else if (cm_node->state == NES_CM_STATE_TSA) {
Faisal Latif0f0bee82011-09-25 20:34:00 -05002343 if (cm_node->nesqp->pau_mode)
2344 nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
2345 else {
2346 rem_ref_cm_node(cm_core, cm_node);
2347 atomic_inc(&cm_accel_dropped_pkts);
2348 dev_kfree_skb_any(skb);
2349 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002350 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002351 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002352 skb_reset_network_header(skb);
2353 skb_set_transport_header(skb, sizeof(*tcph));
2354 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002355 process_packet(cm_node, skb, cm_core);
2356 rem_ref_cm_node(cm_core, cm_node);
2357 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002358 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002359}
2360
2361
2362/**
2363 * nes_cm_alloc_core - allocate a top level instance of a cm core
2364 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002365static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002366{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002367 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002368
2369 /* setup the CM core */
2370 /* alloc top level core control structure */
2371 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2372 if (!cm_core)
2373 return NULL;
2374
2375 INIT_LIST_HEAD(&cm_core->connected_nodes);
2376 init_timer(&cm_core->tcp_timer);
2377 cm_core->tcp_timer.function = nes_cm_timer_tick;
2378
2379 cm_core->mtu = NES_CM_DEFAULT_MTU;
2380 cm_core->state = NES_CM_STATE_INITED;
2381 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2382
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002383 atomic_set(&cm_core->events_posted, 0);
2384
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002385 cm_core->api = &nes_cm_api;
2386
2387 spin_lock_init(&cm_core->ht_lock);
2388 spin_lock_init(&cm_core->listen_list_lock);
2389
2390 INIT_LIST_HEAD(&cm_core->listen_list.list);
2391
2392 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2393
2394 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2395 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2396 cm_core->post_event = nes_cm_post_event;
2397 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2398 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2399
2400 print_core(cm_core);
2401 return cm_core;
2402}
2403
2404
2405/**
2406 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2407 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002408static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002409{
2410 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2411
2412 if (!cm_core)
2413 return -EINVAL;
2414
2415 barrier();
2416
2417 if (timer_pending(&cm_core->tcp_timer)) {
2418 del_timer(&cm_core->tcp_timer);
2419 }
2420
2421 destroy_workqueue(cm_core->event_wq);
2422 destroy_workqueue(cm_core->disconn_wq);
2423 nes_debug(NES_DBG_CM, "\n");
2424 kfree(cm_core);
2425
2426 return 0;
2427}
2428
2429
2430/**
2431 * mini_cm_get
2432 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002433static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002434{
2435 return cm_core->state;
2436}
2437
2438
2439/**
2440 * mini_cm_set
2441 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002442static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002443{
2444 int ret = 0;
2445
2446 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002447 case NES_CM_SET_PKT_SIZE:
2448 cm_core->mtu = value;
2449 break;
2450 case NES_CM_SET_FREE_PKT_Q_SIZE:
2451 cm_core->free_tx_pkt_max = value;
2452 break;
2453 default:
2454 /* unknown set option */
2455 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002456 }
2457
2458 return ret;
2459}
2460
2461
2462/**
2463 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2464 * successfully exchanged when this is called
2465 */
2466static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2467{
2468 int ret = 0;
2469
2470 if (!nesqp)
2471 return -EINVAL;
2472
2473 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2474 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2475 NES_QPCONTEXT_MISC_DROS);
2476
2477 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2478 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2479
2480 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2481
2482 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2483
2484 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2485 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2486
2487 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2488 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2489 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2490
2491 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2492 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2493 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2494
2495 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2496 nesqp->nesqp_context->ts_recent = 0;
2497 nesqp->nesqp_context->ts_age = 0;
2498 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2499 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2500 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2501 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2502 cm_node->tcp_cntxt.rcv_wscale);
2503 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2504 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2505 nesqp->nesqp_context->srtt = 0;
2506 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2507 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2508 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2509 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2510 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2511 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2512
2513 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2514 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2515 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2516 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2517 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2518 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2519 le32_to_cpu(nesqp->nesqp_context->misc));
2520 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2521 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2522 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2523
2524 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2525 cm_node->state = NES_CM_STATE_TSA;
2526
2527 return ret;
2528}
2529
2530
2531/**
2532 * nes_cm_disconn
2533 */
2534int nes_cm_disconn(struct nes_qp *nesqp)
2535{
Don Wood873fcdd2009-09-05 20:36:37 -07002536 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002537
Don Wood873fcdd2009-09-05 20:36:37 -07002538 work = kzalloc(sizeof *work, GFP_ATOMIC);
2539 if (!work)
2540 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002541
Don Wood873fcdd2009-09-05 20:36:37 -07002542 nes_add_ref(&nesqp->ibqp);
2543 work->nesqp = nesqp;
2544 INIT_WORK(&work->work, nes_disconnect_worker);
2545 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002546 return 0;
2547}
2548
2549
2550/**
2551 * nes_disconnect_worker
2552 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002553static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002554{
Don Wood873fcdd2009-09-05 20:36:37 -07002555 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2556 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002557
Don Wood873fcdd2009-09-05 20:36:37 -07002558 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002559 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2560 nesqp->last_aeq, nesqp->hwqp.qp_id);
2561 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002562 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002563}
2564
2565
2566/**
2567 * nes_cm_disconn_true
2568 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002569static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002570{
2571 unsigned long flags;
2572 int ret = 0;
2573 struct iw_cm_id *cm_id;
2574 struct iw_cm_event cm_event;
2575 struct nes_vnic *nesvnic;
2576 u16 last_ae;
2577 u8 original_hw_tcp_state;
2578 u8 original_ibqp_state;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002579 int disconn_status = 0;
Don Woodb29a4fc2009-09-05 20:36:39 -07002580 int issue_disconn = 0;
2581 int issue_close = 0;
2582 int issue_flush = 0;
2583 u32 flush_q = NES_CQP_FLUSH_RQ;
2584 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002585
2586 if (!nesqp) {
2587 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2588 return -1;
2589 }
2590
2591 spin_lock_irqsave(&nesqp->lock, flags);
2592 cm_id = nesqp->cm_id;
2593 /* make sure we havent already closed this connection */
2594 if (!cm_id) {
2595 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2596 nesqp->hwqp.qp_id);
2597 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002598 return -1;
2599 }
2600
2601 nesvnic = to_nesvnic(nesqp->ibqp.device);
2602 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2603
2604 original_hw_tcp_state = nesqp->hw_tcp_state;
2605 original_ibqp_state = nesqp->ibqp_state;
2606 last_ae = nesqp->last_aeq;
2607
Don Woodb29a4fc2009-09-05 20:36:39 -07002608 if (nesqp->term_flags) {
2609 issue_disconn = 1;
2610 issue_close = 1;
2611 nesqp->cm_id = NULL;
2612 if (nesqp->flush_issued == 0) {
2613 nesqp->flush_issued = 1;
2614 issue_flush = 1;
2615 }
2616 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2617 ((original_ibqp_state == IB_QPS_RTS) &&
2618 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2619 issue_disconn = 1;
2620 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002621 disconn_status = -ECONNRESET;
Don Woodb29a4fc2009-09-05 20:36:39 -07002622 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002623
Don Woodb29a4fc2009-09-05 20:36:39 -07002624 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2625 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2626 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2627 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2628 issue_close = 1;
2629 nesqp->cm_id = NULL;
2630 if (nesqp->flush_issued == 0) {
2631 nesqp->flush_issued = 1;
2632 issue_flush = 1;
2633 }
2634 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002635
Don Woodb29a4fc2009-09-05 20:36:39 -07002636 spin_unlock_irqrestore(&nesqp->lock, flags);
2637
2638 if ((issue_flush) && (nesqp->destroyed == 0)) {
2639 /* Flush the queue(s) */
2640 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2641 flush_q |= NES_CQP_FLUSH_SQ;
2642 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2643
2644 if (nesqp->term_flags) {
2645 ibevent.device = nesqp->ibqp.device;
2646 ibevent.event = nesqp->terminate_eventtype;
2647 ibevent.element.qp = &nesqp->ibqp;
2648 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
2649 }
2650 }
2651
2652 if ((cm_id) && (cm_id->event_handler)) {
2653 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002654 atomic_inc(&cm_disconnects);
2655 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002656 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002657 cm_event.local_addr = cm_id->local_addr;
2658 cm_event.remote_addr = cm_id->remote_addr;
2659 cm_event.private_data = NULL;
2660 cm_event.private_data_len = 0;
2661
Faisal Latif6492cdf2008-07-24 20:50:45 -07002662 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2663 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2664 "cm_id = %p, refcount = %u.\n",
2665 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2666 nesqp->hwqp.sq_tail, cm_id,
2667 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002668
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002669 ret = cm_id->event_handler(cm_id, &cm_event);
2670 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002671 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2672 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002673 }
2674
Don Woodb29a4fc2009-09-05 20:36:39 -07002675 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002676 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002677 nes_disconnect(nesqp, 1);
2678
2679 cm_id->provider_data = nesqp;
2680 /* Send up the close complete event */
2681 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002682 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002683 cm_event.provider_data = cm_id->provider_data;
2684 cm_event.local_addr = cm_id->local_addr;
2685 cm_event.remote_addr = cm_id->remote_addr;
2686 cm_event.private_data = NULL;
2687 cm_event.private_data_len = 0;
2688
2689 ret = cm_id->event_handler(cm_id, &cm_event);
2690 if (ret) {
2691 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2692 }
2693
2694 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002695 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002696 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002697
2698 return 0;
2699}
2700
2701
2702/**
2703 * nes_disconnect
2704 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002705static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002706{
2707 int ret = 0;
2708 struct nes_vnic *nesvnic;
2709 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002710 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002711
2712 nesvnic = to_nesvnic(nesqp->ibqp.device);
2713 if (!nesvnic)
2714 return -EINVAL;
2715
2716 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002717 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002718
2719 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00002720 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002721
2722 if (nesqp->active_conn) {
2723
2724 /* indicate this connection is NOT active */
2725 nesqp->active_conn = 0;
2726 } else {
2727 /* Need to free the Last Streaming Mode Message */
2728 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002729 if (nesqp->lsmm_mr)
2730 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002731 pci_free_consistent(nesdev->pcidev,
2732 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2733 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2734 }
2735 }
2736
2737 /* close the CM node down if it is still active */
2738 if (nesqp->cm_node) {
2739 nes_debug(NES_DBG_CM, "Call close API\n");
2740
2741 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002742 }
2743
2744 return ret;
2745}
2746
2747
2748/**
2749 * nes_accept
2750 */
2751int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2752{
2753 u64 u64temp;
2754 struct ib_qp *ibqp;
2755 struct nes_qp *nesqp;
2756 struct nes_vnic *nesvnic;
2757 struct nes_device *nesdev;
2758 struct nes_cm_node *cm_node;
2759 struct nes_adapter *adapter;
2760 struct ib_qp_attr attr;
2761 struct iw_cm_event cm_event;
2762 struct nes_hw_qp_wqe *wqe;
2763 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002764 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002765 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002766 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002767 struct nes_ib_device *nesibdev;
2768 struct ib_mr *ibmr = NULL;
2769 struct ib_phys_buf ibphysbuf;
2770 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002771 u64 tagged_offset;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002772
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002773 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2774 if (!ibqp)
2775 return -EINVAL;
2776
2777 /* get all our handles */
2778 nesqp = to_nesqp(ibqp);
2779 nesvnic = to_nesvnic(nesqp->ibqp.device);
2780 nesdev = nesvnic->nesdev;
2781 adapter = nesdev->nesadapter;
2782
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002783 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002784 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2785 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2786 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002787
Faisal Latifc5a7d482009-12-09 15:54:08 -08002788 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
2789 if (cm_node->loopbackpartner)
2790 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
2791 rem_ref_cm_node(cm_node->cm_core, cm_node);
2792 return -EINVAL;
2793 }
2794
Faisal Latifdae58722010-08-14 21:04:56 +00002795 passive_state = atomic_add_return(1, &cm_node->passive_state);
2796 if (passive_state == NES_SEND_RESET_EVENT) {
2797 rem_ref_cm_node(cm_node->cm_core, cm_node);
2798 return -ECONNRESET;
2799 }
2800
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002801 /* associate the node with the QP */
2802 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002803 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002804
Faisal Latif6492cdf2008-07-24 20:50:45 -07002805 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2806 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002807 atomic_inc(&cm_accepts);
2808
2809 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00002810 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002811
Faisal Latif6492cdf2008-07-24 20:50:45 -07002812 /* allocate the ietf frame and space for private data */
2813 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2814 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2815 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002816
Faisal Latif6492cdf2008-07-24 20:50:45 -07002817 if (!nesqp->ietf_frame) {
2818 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2819 "data\n");
2820 return -ENOMEM;
2821 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002822
2823
Faisal Latif6492cdf2008-07-24 20:50:45 -07002824 /* setup the MPA frame */
2825 nesqp->private_data_len = conn_param->private_data_len;
2826 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002827
Faisal Latif6492cdf2008-07-24 20:50:45 -07002828 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2829 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002830
Faisal Latif6492cdf2008-07-24 20:50:45 -07002831 nesqp->ietf_frame->priv_data_len =
2832 cpu_to_be16(conn_param->private_data_len);
2833 nesqp->ietf_frame->rev = mpa_version;
2834 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002835
Faisal Latif6492cdf2008-07-24 20:50:45 -07002836 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2837 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002838
Faisal Latif6492cdf2008-07-24 20:50:45 -07002839 if (cm_id->remote_addr.sin_addr.s_addr !=
2840 cm_id->local_addr.sin_addr.s_addr) {
2841 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002842 nesibdev = nesvnic->nesibdev;
2843 nespd = nesqp->nespd;
2844 ibphysbuf.addr = nesqp->ietf_frame_pbase;
2845 ibphysbuf.size = conn_param->private_data_len +
2846 sizeof(struct ietf_mpa_frame);
Don Wood7a5efb62009-04-08 14:21:02 -07002847 tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002848 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
2849 &ibphysbuf, 1,
2850 IB_ACCESS_LOCAL_WRITE,
Don Wood7a5efb62009-04-08 14:21:02 -07002851 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002852 if (!ibmr) {
2853 nes_debug(NES_DBG_CM, "Unable to register memory region"
2854 "for lSMM for cm_node = %p \n",
2855 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07002856 pci_free_consistent(nesdev->pcidev,
2857 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2858 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002859 return -ENOMEM;
2860 }
2861
2862 ibmr->pd = &nespd->ibpd;
2863 ibmr->device = nespd->ibpd.device;
2864 nesqp->lsmm_mr = ibmr;
2865
Faisal Latif6492cdf2008-07-24 20:50:45 -07002866 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2867 set_wqe_64bit_value(wqe->wqe_words,
2868 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2869 u64temp);
2870 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2871 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2872 NES_IWARP_SQ_WQE_WRPDU);
2873 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2874 cpu_to_le32(conn_param->private_data_len +
2875 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002876 set_wqe_64bit_value(wqe->wqe_words,
2877 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
Don Wood7a5efb62009-04-08 14:21:02 -07002878 (u64)(unsigned long)nesqp->ietf_frame);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002879 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2880 cpu_to_le32(conn_param->private_data_len +
2881 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002882 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08002883 if (nesqp->sq_kmapped) {
2884 nesqp->sq_kmapped = 0;
2885 kunmap(nesqp->page);
2886 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002887
Faisal Latif6492cdf2008-07-24 20:50:45 -07002888 nesqp->nesqp_context->ird_ord_sizes |=
2889 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2890 NES_QPCONTEXT_ORDIRD_WRPDU);
2891 } else {
2892 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08002893 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002894 }
2895 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002896
2897
2898 /* Cache the cm_id in the qp */
2899 nesqp->cm_id = cm_id;
2900 cm_node->cm_id = cm_id;
2901
2902 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2903 cm_id->provider_data = nesqp;
2904 nesqp->active_conn = 0;
2905
Faisal Latif6492cdf2008-07-24 20:50:45 -07002906 if (cm_node->state == NES_CM_STATE_TSA)
2907 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2908 cm_node);
2909
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002910 nes_cm_init_tsa_conn(nesqp, cm_node);
2911
Faisal Latif6492cdf2008-07-24 20:50:45 -07002912 nesqp->nesqp_context->tcpPorts[0] =
2913 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2914 nesqp->nesqp_context->tcpPorts[1] =
2915 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2916
2917 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2918 nesqp->nesqp_context->ip0 =
2919 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2920 else
2921 nesqp->nesqp_context->ip0 =
2922 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002923
2924 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002925 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2926 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002927
Faisal Latif6492cdf2008-07-24 20:50:45 -07002928 nesqp->nesqp_context->arp_index_vlan |=
2929 cpu_to_le32(nes_arp_table(nesdev,
2930 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002931 NES_ARP_RESOLVE) << 16);
2932
2933 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002934 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002935
2936 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2937
2938 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002939 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2940 nesqp->nesqp_context->ird_ord_sizes |=
2941 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002942
2943 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002944 nes_quad.DstIpAdrIndex =
2945 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2946 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2947 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2948 else
2949 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2950 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2951 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002952
2953 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06002954 crc_value = get_crc_value(&nes_quad);
2955 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002956 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07002957 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002958
2959 nesqp->hte_index &= adapter->hte_index_mask;
2960 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2961
2962 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2963
Faisal Latif6492cdf2008-07-24 20:50:45 -07002964 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2965 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2966 "private data length=%zu.\n", nesqp->hwqp.qp_id,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002967 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2968 ntohs(cm_id->remote_addr.sin_port),
2969 ntohl(cm_id->local_addr.sin_addr.s_addr),
2970 ntohs(cm_id->local_addr.sin_port),
2971 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2972 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002973 conn_param->private_data_len +
2974 sizeof(struct ietf_mpa_frame));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002975
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002976
Coly Li73ac36e2009-01-07 18:09:16 -08002977 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002978 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07002979 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002980
2981 cm_event.event = IW_CM_EVENT_ESTABLISHED;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002982 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002983 cm_event.provider_data = (void *)nesqp;
2984 cm_event.local_addr = cm_id->local_addr;
2985 cm_event.remote_addr = cm_id->remote_addr;
2986 cm_event.private_data = NULL;
2987 cm_event.private_data_len = 0;
2988 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002989 attr.qp_state = IB_QPS_RTS;
2990 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002991 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002992 cm_node->loopbackpartner->mpa_frame_size =
2993 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002994 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002995 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2996 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002997 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2998 }
2999 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003000 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3001 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003002
3003 return 0;
3004}
3005
3006
3007/**
3008 * nes_reject
3009 */
3010int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3011{
3012 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003013 struct nes_cm_node *loopback;
3014
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003015 struct nes_cm_core *cm_core;
3016
3017 atomic_inc(&cm_rejects);
3018 cm_node = (struct nes_cm_node *) cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003019 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003020 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003021 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003022 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
3023
Faisal Latif9d5ab132009-03-06 15:15:01 -08003024 if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
3025 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003026
Faisal Latif1cf078c2009-12-09 15:53:54 -08003027 memcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003028 if (loopback) {
3029 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3030 loopback->mpa_frame.priv_data_len = pdata_len;
3031 loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
3032 pdata_len;
3033 } else {
3034 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
3035 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
3036 }
3037
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003038 cm_node->mpa_frame.rev = mpa_version;
3039 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
3040
Faisal Latif9d5ab132009-03-06 15:15:01 -08003041 return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003042}
3043
3044
3045/**
3046 * nes_connect
3047 * setup and launch cm connect node
3048 */
3049int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3050{
3051 struct ib_qp *ibqp;
3052 struct nes_qp *nesqp;
3053 struct nes_vnic *nesvnic;
3054 struct nes_device *nesdev;
3055 struct nes_cm_node *cm_node;
3056 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003057 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003058
3059 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3060 if (!ibqp)
3061 return -EINVAL;
3062 nesqp = to_nesqp(ibqp);
3063 if (!nesqp)
3064 return -EINVAL;
3065 nesvnic = to_nesvnic(nesqp->ibqp.device);
3066 if (!nesvnic)
3067 return -EINVAL;
3068 nesdev = nesvnic->nesdev;
3069 if (!nesdev)
3070 return -EINVAL;
3071
Faisal Latifc5a7d482009-12-09 15:54:08 -08003072 if (!(cm_id->local_addr.sin_port) || !(cm_id->remote_addr.sin_port))
3073 return -EINVAL;
3074
Faisal Latif6492cdf2008-07-24 20:50:45 -07003075 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
3076 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3077 ntohl(nesvnic->local_ipaddr),
3078 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3079 ntohs(cm_id->remote_addr.sin_port),
3080 ntohl(cm_id->local_addr.sin_addr.s_addr),
3081 ntohs(cm_id->local_addr.sin_port));
3082
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003083 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003084 nesqp->active_conn = 1;
3085
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003086 /* cache the cm_id in the qp */
3087 nesqp->cm_id = cm_id;
3088
3089 cm_id->provider_data = nesqp;
3090
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003091 nesqp->private_data_len = conn_param->private_data_len;
3092 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
3093 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003094 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
3095 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003096
Faisal Latif6492cdf2008-07-24 20:50:45 -07003097 if (cm_id->local_addr.sin_addr.s_addr !=
Faisal Latif53094c32009-04-27 13:37:34 -07003098 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003099 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003100 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003101 apbvt_set = 1;
3102 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003103
3104 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003105 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3106 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3107 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3108 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003109 cm_info.cm_id = cm_id;
3110 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3111
3112 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003113
3114 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003115 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3116 conn_param->private_data_len, (void *)conn_param->private_data,
3117 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003118 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003119 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003120 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003121 PCI_FUNC(nesdev->pcidev->devfn),
3122 NES_MANAGE_APBVT_DEL);
3123
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003124 cm_id->rem_ref(cm_id);
3125 return -ENOMEM;
3126 }
3127
Faisal Latif53094c32009-04-27 13:37:34 -07003128 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003129 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003130 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003131 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003132
3133 return 0;
3134}
3135
3136
3137/**
3138 * nes_create_listen
3139 */
3140int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3141{
3142 struct nes_vnic *nesvnic;
3143 struct nes_cm_listener *cm_node;
3144 struct nes_cm_info cm_info;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003145 int err;
3146
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003147 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3148 cm_id, ntohs(cm_id->local_addr.sin_port));
3149
3150 nesvnic = to_nesvnic(cm_id->device);
3151 if (!nesvnic)
3152 return -EINVAL;
Roland Dreier69d51022010-08-04 14:25:40 -07003153
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003154 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3155 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3156
3157 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3158 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3159
3160 /* setup listen params in our api call struct */
3161 cm_info.loc_addr = nesvnic->local_ipaddr;
3162 cm_info.loc_port = cm_id->local_addr.sin_port;
3163 cm_info.backlog = backlog;
3164 cm_info.cm_id = cm_id;
3165
3166 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3167
3168
3169 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3170 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003171 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003172 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003173 return -ENOMEM;
3174 }
3175
3176 cm_id->provider_data = cm_node;
3177
3178 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003179 err = nes_manage_apbvt(nesvnic,
3180 ntohs(cm_id->local_addr.sin_port),
3181 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3182 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003183 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003184 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3185 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003186 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3187 return err;
3188 }
Faisal Latif6e10d2e2010-02-12 19:55:03 +00003189 atomic_inc(&cm_listens_created);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003190 }
3191
3192 cm_id->add_ref(cm_id);
3193 cm_id->provider_data = (void *)cm_node;
3194
3195
3196 return 0;
3197}
3198
3199
3200/**
3201 * nes_destroy_listen
3202 */
3203int nes_destroy_listen(struct iw_cm_id *cm_id)
3204{
3205 if (cm_id->provider_data)
3206 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3207 else
3208 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3209
3210 cm_id->rem_ref(cm_id);
3211
3212 return 0;
3213}
3214
3215
3216/**
3217 * nes_cm_recv
3218 */
3219int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3220{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003221 int rc = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003222 cm_packets_received++;
3223 if ((g_cm_core) && (g_cm_core->api)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003224 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003225 } else {
3226 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3227 " cm is not setup properly.\n");
3228 }
3229
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003230 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003231}
3232
3233
3234/**
3235 * nes_cm_start
3236 * Start and init a cm core module
3237 */
3238int nes_cm_start(void)
3239{
3240 nes_debug(NES_DBG_CM, "\n");
3241 /* create the primary CM core, pass this handle to subsequent core inits */
3242 g_cm_core = nes_cm_alloc_core();
3243 if (g_cm_core) {
3244 return 0;
3245 } else {
3246 return -ENOMEM;
3247 }
3248}
3249
3250
3251/**
3252 * nes_cm_stop
3253 * stop and dealloc all cm core instances
3254 */
3255int nes_cm_stop(void)
3256{
3257 g_cm_core->api->destroy_cm_core(g_cm_core);
3258 return 0;
3259}
3260
3261
3262/**
3263 * cm_event_connected
3264 * handle a connected event, setup QPs and HW
3265 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003266static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003267{
3268 u64 u64temp;
3269 struct nes_qp *nesqp;
3270 struct nes_vnic *nesvnic;
3271 struct nes_device *nesdev;
3272 struct nes_cm_node *cm_node;
3273 struct nes_adapter *nesadapter;
3274 struct ib_qp_attr attr;
3275 struct iw_cm_id *cm_id;
3276 struct iw_cm_event cm_event;
3277 struct nes_hw_qp_wqe *wqe;
3278 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003279 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003280 int ret;
3281
3282 /* get all our handles */
3283 cm_node = event->cm_node;
3284 cm_id = cm_node->cm_id;
3285 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3286 nesqp = (struct nes_qp *)cm_id->provider_data;
3287 nesvnic = to_nesvnic(nesqp->ibqp.device);
3288 nesdev = nesvnic->nesdev;
3289 nesadapter = nesdev->nesadapter;
3290
3291 if (nesqp->destroyed) {
3292 return;
3293 }
3294 atomic_inc(&cm_connecteds);
3295 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3296 " local port 0x%04X. jiffies = %lu.\n",
3297 nesqp->hwqp.qp_id,
3298 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3299 ntohs(cm_id->remote_addr.sin_port),
3300 ntohs(cm_id->local_addr.sin_port),
3301 jiffies);
3302
3303 nes_cm_init_tsa_conn(nesqp, cm_node);
3304
3305 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003306 nesqp->nesqp_context->tcpPorts[0] =
3307 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3308 nesqp->nesqp_context->tcpPorts[1] =
3309 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3310 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3311 nesqp->nesqp_context->ip0 =
3312 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3313 else
3314 nesqp->nesqp_context->ip0 =
3315 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003316
3317 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003318 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3319 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003320 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003321 nes_arp_table(nesdev,
3322 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003323 NULL, NES_ARP_RESOLVE) << 16);
3324 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3325 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3326 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3327 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003328 cpu_to_le32((u32)1 <<
3329 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003330
3331 /* Adjust tail for not having a LSMM */
3332 nesqp->hwqp.sq_tail = 1;
3333
3334#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003335 if (cm_node->send_write0) {
3336 nes_debug(NES_DBG_CM, "Sending first write.\n");
3337 wqe = &nesqp->hwqp.sq_vbase[0];
3338 u64temp = (unsigned long)nesqp;
3339 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3340 set_wqe_64bit_value(wqe->wqe_words,
3341 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3342 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3343 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3344 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3345 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3346 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3347 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3348 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003349
Faisal Latifd2fa9b262009-12-09 15:54:28 -08003350 if (nesqp->sq_kmapped) {
3351 nesqp->sq_kmapped = 0;
3352 kunmap(nesqp->page);
3353 }
3354
Faisal Latif6492cdf2008-07-24 20:50:45 -07003355 /* use the reserved spot on the WQ for the extra first WQE */
3356 nesqp->nesqp_context->ird_ord_sizes &=
3357 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3358 NES_QPCONTEXT_ORDIRD_WRPDU |
3359 NES_QPCONTEXT_ORDIRD_ALSMM));
3360 nesqp->skip_lsmm = 1;
3361 nesqp->hwqp.sq_tail = 0;
3362 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3363 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3364 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003365#endif
3366
3367 memset(&nes_quad, 0, sizeof(nes_quad));
3368
Faisal Latif6492cdf2008-07-24 20:50:45 -07003369 nes_quad.DstIpAdrIndex =
3370 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3371 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3372 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3373 else
3374 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003375 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3376 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3377
3378 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003379 crc_value = get_crc_value(&nes_quad);
3380 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003381 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3382 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3383
3384 nesqp->hte_index &= nesadapter->hte_index_mask;
3385 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3386
3387 nesqp->ietf_frame = &cm_node->mpa_frame;
3388 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3389 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3390
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003391 /* notify OF layer we successfully created the requested connection */
3392 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003393 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003394 cm_event.provider_data = cm_id->provider_data;
3395 cm_event.local_addr.sin_family = AF_INET;
3396 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3397 cm_event.remote_addr = cm_id->remote_addr;
3398
Faisal Latif6492cdf2008-07-24 20:50:45 -07003399 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3400 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003401
3402 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3403 ret = cm_id->event_handler(cm_id, &cm_event);
3404 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3405
3406 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003407 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3408 "ret=%d\n", __func__, __LINE__, ret);
3409 attr.qp_state = IB_QPS_RTS;
3410 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003411
Faisal Latif6492cdf2008-07-24 20:50:45 -07003412 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3413 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003414
3415 return;
3416}
3417
3418
3419/**
3420 * cm_event_connect_error
3421 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003422static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003423{
3424 struct nes_qp *nesqp;
3425 struct iw_cm_id *cm_id;
3426 struct iw_cm_event cm_event;
3427 /* struct nes_cm_info cm_info; */
3428 int ret;
3429
3430 if (!event->cm_node)
3431 return;
3432
3433 cm_id = event->cm_node->cm_id;
3434 if (!cm_id) {
3435 return;
3436 }
3437
3438 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3439 nesqp = cm_id->provider_data;
3440
3441 if (!nesqp) {
3442 return;
3443 }
3444
3445 /* notify OF layer about this connection error event */
3446 /* cm_id->rem_ref(cm_id); */
3447 nesqp->cm_id = NULL;
3448 cm_id->provider_data = NULL;
3449 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003450 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003451 cm_event.provider_data = cm_id->provider_data;
3452 cm_event.local_addr = cm_id->local_addr;
3453 cm_event.remote_addr = cm_id->remote_addr;
3454 cm_event.private_data = NULL;
3455 cm_event.private_data_len = 0;
3456
Faisal Latif6492cdf2008-07-24 20:50:45 -07003457 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3458 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3459 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003460
3461 ret = cm_id->event_handler(cm_id, &cm_event);
3462 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3463 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003464 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3465 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003466 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003467
Faisal Latif6492cdf2008-07-24 20:50:45 -07003468 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003469 return;
3470}
3471
3472
3473/**
3474 * cm_event_reset
3475 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003476static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003477{
3478 struct nes_qp *nesqp;
3479 struct iw_cm_id *cm_id;
3480 struct iw_cm_event cm_event;
3481 /* struct nes_cm_info cm_info; */
3482 int ret;
3483
3484 if (!event->cm_node)
3485 return;
3486
3487 if (!event->cm_node->cm_id)
3488 return;
3489
3490 cm_id = event->cm_node->cm_id;
3491
3492 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3493 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003494 if (!nesqp)
3495 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003496
3497 nesqp->cm_id = NULL;
3498 /* cm_id->provider_data = NULL; */
3499 cm_event.event = IW_CM_EVENT_DISCONNECT;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003500 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003501 cm_event.provider_data = cm_id->provider_data;
3502 cm_event.local_addr = cm_id->local_addr;
3503 cm_event.remote_addr = cm_id->remote_addr;
3504 cm_event.private_data = NULL;
3505 cm_event.private_data_len = 0;
3506
Faisal Latif183ecfa2008-11-21 20:50:46 -06003507 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003508 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003509 atomic_inc(&cm_closes);
3510 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003511 cm_event.status = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003512 cm_event.provider_data = cm_id->provider_data;
3513 cm_event.local_addr = cm_id->local_addr;
3514 cm_event.remote_addr = cm_id->remote_addr;
3515 cm_event.private_data = NULL;
3516 cm_event.private_data_len = 0;
3517 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3518 ret = cm_id->event_handler(cm_id, &cm_event);
3519
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003520 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3521
3522
3523 /* notify OF layer about this connection error event */
3524 cm_id->rem_ref(cm_id);
3525
3526 return;
3527}
3528
3529
3530/**
3531 * cm_event_mpa_req
3532 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003533static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003534{
3535 struct iw_cm_id *cm_id;
3536 struct iw_cm_event cm_event;
3537 int ret;
3538 struct nes_cm_node *cm_node;
3539
3540 cm_node = event->cm_node;
3541 if (!cm_node)
3542 return;
3543 cm_id = cm_node->cm_id;
3544
3545 atomic_inc(&cm_connect_reqs);
3546 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3547 cm_node, cm_id, jiffies);
3548
3549 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003550 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003551 cm_event.provider_data = (void *)cm_node;
3552
3553 cm_event.local_addr.sin_family = AF_INET;
3554 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3555 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3556
3557 cm_event.remote_addr.sin_family = AF_INET;
3558 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3559 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003560 cm_event.private_data = cm_node->mpa_frame_buf;
3561 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003562
3563 ret = cm_id->event_handler(cm_id, &cm_event);
3564 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003565 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3566 __func__, __LINE__, ret);
3567 return;
3568}
3569
3570
3571static void cm_event_mpa_reject(struct nes_cm_event *event)
3572{
3573 struct iw_cm_id *cm_id;
3574 struct iw_cm_event cm_event;
3575 struct nes_cm_node *cm_node;
3576 int ret;
3577
3578 cm_node = event->cm_node;
3579 if (!cm_node)
3580 return;
3581 cm_id = cm_node->cm_id;
3582
3583 atomic_inc(&cm_connect_reqs);
3584 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3585 cm_node, cm_id, jiffies);
3586
3587 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3588 cm_event.status = -ECONNREFUSED;
3589 cm_event.provider_data = cm_id->provider_data;
3590
3591 cm_event.local_addr.sin_family = AF_INET;
3592 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3593 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3594
3595 cm_event.remote_addr.sin_family = AF_INET;
3596 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3597 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3598
3599 cm_event.private_data = cm_node->mpa_frame_buf;
3600 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3601
3602 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3603 "remove_addr=%08x\n",
3604 cm_event.local_addr.sin_addr.s_addr,
3605 cm_event.remote_addr.sin_addr.s_addr);
3606
3607 ret = cm_id->event_handler(cm_id, &cm_event);
3608 if (ret)
3609 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003610 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003611
3612 return;
3613}
3614
3615
3616static void nes_cm_event_handler(struct work_struct *);
3617
3618/**
3619 * nes_cm_post_event
3620 * post an event to the cm event handler
3621 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003622static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003623{
3624 atomic_inc(&event->cm_node->cm_core->events_posted);
3625 add_ref_cm_node(event->cm_node);
3626 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3627 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003628 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3629 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003630
3631 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3632
3633 nes_debug(NES_DBG_CM, "Exit\n");
3634 return 0;
3635}
3636
3637
3638/**
3639 * nes_cm_event_handler
3640 * worker function to handle cm events
3641 * will free instance of nes_cm_event
3642 */
3643static void nes_cm_event_handler(struct work_struct *work)
3644{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003645 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3646 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003647 struct nes_cm_core *cm_core;
3648
Faisal Latif6492cdf2008-07-24 20:50:45 -07003649 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003650 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003651
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003652 cm_core = event->cm_node->cm_core;
3653 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003654 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003655
3656 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003657 case NES_CM_EVENT_MPA_REQ:
3658 cm_event_mpa_req(event);
3659 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3660 event->cm_node);
3661 break;
3662 case NES_CM_EVENT_RESET:
3663 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3664 event->cm_node);
3665 cm_event_reset(event);
3666 break;
3667 case NES_CM_EVENT_CONNECTED:
3668 if ((!event->cm_node->cm_id) ||
3669 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003670 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003671 cm_event_connected(event);
3672 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3673 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003674 case NES_CM_EVENT_MPA_REJECT:
3675 if ((!event->cm_node->cm_id) ||
3676 (event->cm_node->state == NES_CM_STATE_TSA))
3677 break;
3678 cm_event_mpa_reject(event);
3679 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3680 break;
3681
Faisal Latif6492cdf2008-07-24 20:50:45 -07003682 case NES_CM_EVENT_ABORTED:
3683 if ((!event->cm_node->cm_id) ||
3684 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003685 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003686 cm_event_connect_error(event);
3687 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3688 break;
3689 case NES_CM_EVENT_DROPPED_PKT:
3690 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3691 break;
3692 default:
3693 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3694 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003695 }
3696
3697 atomic_dec(&cm_core->events_posted);
3698 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3699 rem_ref_cm_node(cm_core, event->cm_node);
3700 kfree(event);
3701
3702 return;
3703}