blob: 183f7ab120db55f8b15b991a6ddc0cf326e40a97 [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
Tatyana Nikolova615eb712011-09-25 20:17:46 +053080static inline int mini_cm_accelerated(struct nes_cm_core *, struct nes_cm_node *);
81static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *, struct nes_vnic *, struct nes_cm_info *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080082static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
Tatyana Nikolova615eb712011-09-25 20:17:46 +053083static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *, struct nes_vnic *, u16, void *, struct nes_cm_info *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070084static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Tatyana Nikolova615eb712011-09-25 20:17:46 +053085static int mini_cm_accept(struct nes_cm_core *, struct nes_cm_node *);
86static int mini_cm_reject(struct nes_cm_core *, struct nes_cm_node *);
87static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *, struct sk_buff *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070088static int mini_cm_dealloc_core(struct nes_cm_core *);
89static int mini_cm_get(struct nes_cm_core *);
90static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070091
Tatyana Nikolova615eb712011-09-25 20:17:46 +053092static void form_cm_frame(struct sk_buff *, struct nes_cm_node *, void *, u32, void *, u32, u8);
Faisal Latif6492cdf2008-07-24 20:50:45 -070093static int add_ref_cm_node(struct nes_cm_node *);
94static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
95
Roland Dreier1a855fb2008-04-16 21:01:09 -070096static int nes_cm_disconn_true(struct nes_qp *);
97static int nes_cm_post_event(struct nes_cm_event *event);
98static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
99static void nes_disconnect_worker(struct work_struct *work);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700100
101static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800102static int send_mpa_reject(struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700103static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
104static int send_reset(struct nes_cm_node *, struct sk_buff *);
105static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fb2008-04-16 21:01:09 -0700106static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530107static void process_packet(struct nes_cm_node *, struct sk_buff *, struct nes_cm_core *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700108
109static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
110static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
111static void cleanup_retrans_entry(struct nes_cm_node *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800112static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700113static void free_retrans_entry(struct nes_cm_node *cm_node);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530114static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph, struct sk_buff *skb, int optionsize, int passive);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700115
116/* CM event handler functions */
117static void cm_event_connected(struct nes_cm_event *);
118static void cm_event_connect_error(struct nes_cm_event *);
119static void cm_event_reset(struct nes_cm_event *);
120static void cm_event_mpa_req(struct nes_cm_event *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800121static void cm_event_mpa_reject(struct nes_cm_event *);
122static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700123
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530124/* MPA build functions */
125static int cm_build_mpa_frame(struct nes_cm_node *, u8 **, u16 *, u8 *, u8);
126static void build_mpa_v2(struct nes_cm_node *, void *, u8);
127static void build_mpa_v1(struct nes_cm_node *, void *, u8);
128static void build_rdma0_msg(struct nes_cm_node *, struct nes_qp **);
129
Faisal Latif6492cdf2008-07-24 20:50:45 -0700130static void print_core(struct nes_cm_core *core);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800131
132/* External CM API Interface */
133/* instance of function pointers for client API */
134/* set address of this instance to cm_core->cm_ops at cm_core alloc */
135static struct nes_cm_ops nes_cm_api = {
136 mini_cm_accelerated,
137 mini_cm_listen,
138 mini_cm_del_listen,
139 mini_cm_connect,
140 mini_cm_close,
141 mini_cm_accept,
142 mini_cm_reject,
143 mini_cm_recv_pkt,
144 mini_cm_dealloc_core,
145 mini_cm_get,
146 mini_cm_set
147};
148
Roland Dreier1a855fb2008-04-16 21:01:09 -0700149static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800150
151atomic_t cm_connects;
152atomic_t cm_accepts;
153atomic_t cm_disconnects;
154atomic_t cm_closes;
155atomic_t cm_connecteds;
156atomic_t cm_connect_reqs;
157atomic_t cm_rejects;
158
Faisal Latif0f0bee82011-09-25 20:34:00 -0500159int nes_add_ref_cm_node(struct nes_cm_node *cm_node)
160{
161 return add_ref_cm_node(cm_node);
162}
163
164int nes_rem_ref_cm_node(struct nes_cm_node *cm_node)
165{
166 return rem_ref_cm_node(cm_node->cm_core, cm_node);
167}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800168
169/**
170 * create_event
171 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530172static struct nes_cm_event *create_event(struct nes_cm_node * cm_node,
173 enum nes_cm_event_type type)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800174{
175 struct nes_cm_event *event;
176
177 if (!cm_node->cm_id)
178 return NULL;
179
180 /* allocate an empty event */
181 event = kzalloc(sizeof(*event), GFP_ATOMIC);
182
183 if (!event)
184 return NULL;
185
186 event->type = type;
187 event->cm_node = cm_node;
188 event->cm_info.rem_addr = cm_node->rem_addr;
189 event->cm_info.loc_addr = cm_node->loc_addr;
190 event->cm_info.rem_port = cm_node->rem_port;
191 event->cm_info.loc_port = cm_node->loc_port;
192 event->cm_info.cm_id = cm_node->cm_id;
193
Faisal Latif6492cdf2008-07-24 20:50:45 -0700194 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530195 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
196 cm_node, event, type, event->cm_info.loc_addr,
197 event->cm_info.loc_port, event->cm_info.rem_addr,
198 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800199
200 nes_cm_post_event(event);
201 return event;
202}
203
204
205/**
206 * send_mpa_request
207 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700208static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800209{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530210 u8 start_addr = 0;
211 u8 *start_ptr = &start_addr;
212 u8 **start_buff = &start_ptr;
213 u16 buff_len = 0;
214
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800215 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700216 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800217 return -1;
218 }
219
220 /* send an MPA Request frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530221 cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REQUEST);
222 form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800223
Faisal Latif9d5ab132009-03-06 15:15:01 -0800224 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
225}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800226
Faisal Latif9d5ab132009-03-06 15:15:01 -0800227
228
229static int send_mpa_reject(struct nes_cm_node *cm_node)
230{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530231 struct sk_buff *skb = NULL;
232 u8 start_addr = 0;
233 u8 *start_ptr = &start_addr;
234 u8 **start_buff = &start_ptr;
235 u16 buff_len = 0;
Tatyana Nikolova81f99dc2012-01-17 10:17:30 -0600236 struct ietf_mpa_v1 *mpa_frame;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800237
238 skb = dev_alloc_skb(MAX_CM_BUFFER);
239 if (!skb) {
240 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
241 return -ENOMEM;
242 }
243
244 /* send an MPA reject frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530245 cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REPLY);
Tatyana Nikolova81f99dc2012-01-17 10:17:30 -0600246 mpa_frame = (struct ietf_mpa_v1 *)*start_buff;
247 mpa_frame->flags |= IETF_MPA_FLAGS_REJECT;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530248 form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK | SET_FIN);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800249
250 cm_node->state = NES_CM_STATE_FIN_WAIT1;
251 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800252}
253
254
255/**
256 * recv_mpa - process a received TCP pkt, we are expecting an
257 * IETF MPA frame
258 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800259static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530260 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800261{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530262 struct ietf_mpa_v1 *mpa_frame;
263 struct ietf_mpa_v2 *mpa_v2_frame;
264 struct ietf_rtr_msg *rtr_msg;
265 int mpa_hdr_len;
266 int priv_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800267
Faisal Latif9d5ab132009-03-06 15:15:01 -0800268 *type = NES_MPA_REQUEST_ACCEPT;
269
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800270 /* assume req frame is in tcp data payload */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530271 if (len < sizeof(struct ietf_mpa_v1)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800272 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800273 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800274 }
275
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530276 /* points to the beginning of the frame, which could be MPA V1 or V2 */
277 mpa_frame = (struct ietf_mpa_v1 *)buffer;
278 mpa_hdr_len = sizeof(struct ietf_mpa_v1);
279 priv_data_len = ntohs(mpa_frame->priv_data_len);
280
Faisal Latif1cf078c2009-12-09 15:53:54 -0800281 /* make sure mpa private data len is less than 512 bytes */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530282 if (priv_data_len > IETF_MAX_PRIV_DATA_LEN) {
Faisal Latif1cf078c2009-12-09 15:53:54 -0800283 nes_debug(NES_DBG_CM, "The received Length of Private"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530284 " Data field exceeds 512 octets\n");
Faisal Latif1cf078c2009-12-09 15:53:54 -0800285 return -EINVAL;
286 }
287 /*
288 * make sure MPA receiver interoperate with the
289 * received MPA version and MPA key information
290 *
291 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530292 if (mpa_frame->rev != IETF_MPA_V1 && mpa_frame->rev != IETF_MPA_V2) {
Faisal Latif1cf078c2009-12-09 15:53:54 -0800293 nes_debug(NES_DBG_CM, "The received mpa version"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530294 " is not supported\n");
Faisal Latif1cf078c2009-12-09 15:53:54 -0800295 return -EINVAL;
296 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530297 /*
298 * backwards compatibility only
299 */
300 if (mpa_frame->rev > cm_node->mpa_frame_rev) {
301 nes_debug(NES_DBG_CM, "The received mpa version"
302 " can not be interoperated\n");
303 return -EINVAL;
304 } else {
305 cm_node->mpa_frame_rev = mpa_frame->rev;
306 }
307
Faisal Latif1cf078c2009-12-09 15:53:54 -0800308 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
309 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
310 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
311 return -EINVAL;
312 }
313 } else {
314 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
315 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
316 return -EINVAL;
317 }
318 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800319
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530320
321 if (priv_data_len + mpa_hdr_len != len) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800322 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530323 " complete (%x + %x != %x)\n",
324 priv_data_len, mpa_hdr_len, len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800325 return -EINVAL;
326 }
327 /* make sure it does not exceed the max size */
328 if (len > MAX_CM_BUFFER) {
329 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530330 " (%x + %x != %x)\n",
331 priv_data_len, mpa_hdr_len, len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800332 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800333 }
334
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530335 cm_node->mpa_frame_size = priv_data_len;
336
337 switch (mpa_frame->rev) {
338 case IETF_MPA_V2: {
339 u16 ird_size;
340 u16 ord_size;
341 mpa_v2_frame = (struct ietf_mpa_v2 *)buffer;
342 mpa_hdr_len += IETF_RTR_MSG_SIZE;
343 cm_node->mpa_frame_size -= IETF_RTR_MSG_SIZE;
344 rtr_msg = &mpa_v2_frame->rtr_msg;
345
346 /* parse rtr message */
347 rtr_msg->ctrl_ird = ntohs(rtr_msg->ctrl_ird);
348 rtr_msg->ctrl_ord = ntohs(rtr_msg->ctrl_ord);
349 ird_size = rtr_msg->ctrl_ird & IETF_NO_IRD_ORD;
350 ord_size = rtr_msg->ctrl_ord & IETF_NO_IRD_ORD;
351
352 if (!(rtr_msg->ctrl_ird & IETF_PEER_TO_PEER)) {
353 /* send reset */
354 return -EINVAL;
355 }
356
357 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
358 /* responder */
359 if (cm_node->ord_size > ird_size)
360 cm_node->ord_size = ird_size;
361 } else {
362 /* initiator */
363 if (cm_node->ord_size > ird_size)
364 cm_node->ord_size = ird_size;
365
366 if (cm_node->ird_size < ord_size) {
367 /* no resources available */
368 /* send terminate message */
369 return -EINVAL;
370 }
371 }
372
373 if (rtr_msg->ctrl_ord & IETF_RDMA0_READ) {
374 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
375 } else if (rtr_msg->ctrl_ord & IETF_RDMA0_WRITE) {
376 cm_node->send_rdma0_op = SEND_RDMA_WRITE_ZERO;
377 } else { /* Not supported RDMA0 operation */
378 return -EINVAL;
379 }
380 break;
381 }
382 case IETF_MPA_V1:
383 default:
384 break;
385 }
386
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800387 /* copy entire MPA frame to our cm_node's frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530388 memcpy(cm_node->mpa_frame_buf, buffer + mpa_hdr_len, cm_node->mpa_frame_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800389
Faisal Latif9d5ab132009-03-06 15:15:01 -0800390 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
391 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800392 return 0;
393}
394
395
396/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800397 * form_cm_frame - get a free packet and build empty frame Use
398 * node info to build.
399 */
Chien Tung6098d102008-11-21 20:51:01 -0600400static void form_cm_frame(struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530401 struct nes_cm_node *cm_node, void *options, u32 optionsize,
402 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800403{
404 struct tcphdr *tcph;
405 struct iphdr *iph;
406 struct ethhdr *ethh;
407 u8 *buf;
408 u16 packetsize = sizeof(*iph);
409
410 packetsize += sizeof(*tcph);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530411 packetsize += optionsize + datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800412
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530413 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800414 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
415
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800416 buf = skb_put(skb, packetsize + ETH_HLEN);
417
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530418 ethh = (struct ethhdr *)buf;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800419 buf += ETH_HLEN;
420
421 iph = (struct iphdr *)buf;
422 buf += sizeof(*iph);
423 tcph = (struct tcphdr *)buf;
424 skb_reset_mac_header(skb);
425 skb_set_network_header(skb, ETH_HLEN);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530426 skb_set_transport_header(skb, ETH_HLEN + sizeof(*iph));
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800427 buf += sizeof(*tcph);
428
429 skb->ip_summed = CHECKSUM_PARTIAL;
430 skb->protocol = htons(0x800);
431 skb->data_len = 0;
432 skb->mac_len = ETH_HLEN;
433
434 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
435 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
436 ethh->h_proto = htons(0x0800);
437
438 iph->version = IPVERSION;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530439 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800440 iph->tos = 0;
441 iph->tot_len = htons(packetsize);
442 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
443
444 iph->frag_off = htons(0x4000);
445 iph->ttl = 0x40;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530446 iph->protocol = 0x06; /* IPPROTO_TCP */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800447
448 iph->saddr = htonl(cm_node->loc_addr);
449 iph->daddr = htonl(cm_node->rem_addr);
450
451 tcph->source = htons(cm_node->loc_port);
452 tcph->dest = htons(cm_node->rem_port);
453 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
454
455 if (flags & SET_ACK) {
456 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
457 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
458 tcph->ack = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530459 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800460 tcph->ack_seq = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530461 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800462
463 if (flags & SET_SYN) {
464 cm_node->tcp_cntxt.loc_seq_num++;
465 tcph->syn = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530466 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700467 cm_node->tcp_cntxt.loc_seq_num += datasize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530468 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800469
Faisal Latif6492cdf2008-07-24 20:50:45 -0700470 if (flags & SET_FIN) {
471 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800472 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700473 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800474
475 if (flags & SET_RST)
476 tcph->rst = 1;
477
478 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
479 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
480 tcph->urg_ptr = 0;
481 if (optionsize)
482 memcpy(buf, options, optionsize);
483 buf += optionsize;
484 if (datasize)
485 memcpy(buf, data, datasize);
486
487 skb_shinfo(skb)->nr_frags = 0;
488 cm_packets_created++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800489}
490
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800491/**
492 * print_core - dump a cm core
493 */
494static void print_core(struct nes_cm_core *core)
495{
496 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
497 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
498 if (!core)
499 return;
500 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800501
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530502 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800503
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800504 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
505 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
506
507 nes_debug(NES_DBG_CM, "core : %p \n", core);
508
509 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
510}
511
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530512/**
513 * cm_build_mpa_frame - build a MPA V1 frame or MPA V2 frame
514 */
515static int cm_build_mpa_frame(struct nes_cm_node *cm_node, u8 **start_buff,
516 u16 *buff_len, u8 *pci_mem, u8 mpa_key)
517{
518 int ret = 0;
519
520 *start_buff = (pci_mem) ? pci_mem : &cm_node->mpa_frame_buf[0];
521
522 switch (cm_node->mpa_frame_rev) {
523 case IETF_MPA_V1:
524 *start_buff = (u8 *)*start_buff + sizeof(struct ietf_rtr_msg);
525 *buff_len = sizeof(struct ietf_mpa_v1) + cm_node->mpa_frame_size;
526 build_mpa_v1(cm_node, *start_buff, mpa_key);
527 break;
528 case IETF_MPA_V2:
529 *buff_len = sizeof(struct ietf_mpa_v2) + cm_node->mpa_frame_size;
530 build_mpa_v2(cm_node, *start_buff, mpa_key);
531 break;
532 default:
533 ret = -EINVAL;
534 }
535 return ret;
536}
537
538/**
539 * build_mpa_v2 - build a MPA V2 frame
540 */
541static void build_mpa_v2(struct nes_cm_node *cm_node,
542 void *start_addr, u8 mpa_key)
543{
544 struct ietf_mpa_v2 *mpa_frame = (struct ietf_mpa_v2 *)start_addr;
545 struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
546
547 /* initialize the upper 5 bytes of the frame */
548 build_mpa_v1(cm_node, start_addr, mpa_key);
549 mpa_frame->flags |= IETF_MPA_V2_FLAG; /* set a bit to indicate MPA V2 */
550 mpa_frame->priv_data_len += htons(IETF_RTR_MSG_SIZE);
551
552 /* initialize RTR msg */
553 rtr_msg->ctrl_ird = (cm_node->ird_size > IETF_NO_IRD_ORD) ?
554 IETF_NO_IRD_ORD : cm_node->ird_size;
555 rtr_msg->ctrl_ord = (cm_node->ord_size > IETF_NO_IRD_ORD) ?
556 IETF_NO_IRD_ORD : cm_node->ord_size;
557
558 rtr_msg->ctrl_ird |= IETF_PEER_TO_PEER;
559 rtr_msg->ctrl_ird |= IETF_FLPDU_ZERO_LEN;
560
561 switch (mpa_key) {
562 case MPA_KEY_REQUEST:
563 rtr_msg->ctrl_ord |= IETF_RDMA0_WRITE;
564 rtr_msg->ctrl_ord |= IETF_RDMA0_READ;
565 break;
566 case MPA_KEY_REPLY:
567 switch (cm_node->send_rdma0_op) {
568 case SEND_RDMA_WRITE_ZERO:
569 rtr_msg->ctrl_ord |= IETF_RDMA0_WRITE;
570 break;
571 case SEND_RDMA_READ_ZERO:
572 rtr_msg->ctrl_ord |= IETF_RDMA0_READ;
573 break;
574 }
575 }
576 rtr_msg->ctrl_ird = htons(rtr_msg->ctrl_ird);
577 rtr_msg->ctrl_ord = htons(rtr_msg->ctrl_ord);
578}
579
580/**
581 * build_mpa_v1 - build a MPA V1 frame
582 */
583static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
584{
585 struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
586
587 switch (mpa_key) {
588 case MPA_KEY_REQUEST:
589 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
590 break;
591 case MPA_KEY_REPLY:
592 memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
593 break;
594 }
595 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
596 mpa_frame->rev = cm_node->mpa_frame_rev;
597 mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
598}
599
600static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
601{
602 u64 u64temp;
603 struct nes_qp *nesqp = *nesqp_addr;
604 struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
605
606 u64temp = (unsigned long)nesqp;
607 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
608 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
609
610 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
611 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
612
613 switch (cm_node->send_rdma0_op) {
614 case SEND_RDMA_WRITE_ZERO:
615 nes_debug(NES_DBG_CM, "Sending first write.\n");
616 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
617 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
618 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
619 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
620 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
621 break;
622
623 case SEND_RDMA_READ_ZERO:
624 default:
625 if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO) {
626 printk(KERN_ERR "%s[%u]: Unsupported RDMA0 len operation=%u\n",
627 __func__, __LINE__, cm_node->send_rdma0_op);
628 WARN_ON(1);
629 }
630 nes_debug(NES_DBG_CM, "Sending first rdma operation.\n");
631 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
632 cpu_to_le32(NES_IWARP_SQ_OP_RDMAR);
633 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX] = 1;
634 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_HIGH_IDX] = 0;
635 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] = 0;
636 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_STAG_IDX] = 1;
637 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 1;
638 break;
639 }
640
641 if (nesqp->sq_kmapped) {
642 nesqp->sq_kmapped = 0;
643 kunmap(nesqp->page);
644 }
645
646 /*use the reserved spot on the WQ for the extra first WQE*/
647 nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
648 NES_QPCONTEXT_ORDIRD_WRPDU |
649 NES_QPCONTEXT_ORDIRD_ALSMM));
650 nesqp->skip_lsmm = 1;
651 nesqp->hwqp.sq_tail = 0;
652}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800653
654/**
655 * schedule_nes_timer
656 * note - cm_node needs to be protected before calling this. Encase in:
657 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
658 */
659int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530660 enum nes_timer_type type, int send_retrans,
661 int close_when_complete)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800662{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530663 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700664 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800665 struct nes_timer_entry *new_send;
666 int ret = 0;
667 u32 was_timer_set;
668
669 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
670 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800671 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800672
673 /* new_send->timetosend = currenttime */
674 new_send->retrycount = NES_DEFAULT_RETRYS;
675 new_send->retranscount = NES_DEFAULT_RETRANS;
676 new_send->skb = skb;
677 new_send->timetosend = jiffies;
678 new_send->type = type;
679 new_send->netdev = cm_node->netdev;
680 new_send->send_retrans = send_retrans;
681 new_send->close_when_complete = close_when_complete;
682
683 if (type == NES_TIMER_TYPE_CLOSE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530684 new_send->timetosend += (HZ / 10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800685 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700686 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800687 WARN_ON(1);
688 return -EINVAL;
689 }
690 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800691 }
692
693 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700694 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800695 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700696 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
697 cm_node->send_entry = new_send;
698 add_ref_cm_node(cm_node);
699 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
700 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800701
702 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
703 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700704 nes_debug(NES_DBG_CM, "Error sending packet %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530705 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800706 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700707 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800708 } else {
709 cm_packets_sent++;
710 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700711 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800712 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700713 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800714 return ret;
715 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800716 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800717 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800718
719 was_timer_set = timer_pending(&cm_core->tcp_timer);
720
721 if (!was_timer_set) {
722 cm_core->tcp_timer.expires = new_send->timetosend;
723 add_timer(&cm_core->tcp_timer);
724 }
725
726 return ret;
727}
728
Faisal Latif9d5ab132009-03-06 15:15:01 -0800729static void nes_retrans_expired(struct nes_cm_node *cm_node)
730{
Faisal Latif68237a02009-06-22 22:53:28 -0700731 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latifdae58722010-08-14 21:04:56 +0000732 enum nes_cm_node_state state = cm_node->state;
733 cm_node->state = NES_CM_STATE_CLOSED;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530734
Faisal Latifdae58722010-08-14 21:04:56 +0000735 switch (state) {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800736 case NES_CM_STATE_SYN_RCVD:
737 case NES_CM_STATE_CLOSING:
738 rem_ref_cm_node(cm_node->cm_core, cm_node);
739 break;
740 case NES_CM_STATE_LAST_ACK:
741 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700742 if (cm_node->cm_id)
743 cm_id->rem_ref(cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800744 send_reset(cm_node, NULL);
745 break;
746 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800747 add_ref_cm_node(cm_node);
748 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800749 create_event(cm_node, NES_CM_EVENT_ABORTED);
750 }
751}
752
753static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
754{
755 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
756 struct iw_cm_id *cm_id = cm_node->cm_id;
757 struct nes_qp *nesqp;
758 unsigned long qplockflags;
759
760 if (!recv_entry)
761 return;
762 nesqp = (struct nes_qp *)recv_entry->skb;
763 if (nesqp) {
764 spin_lock_irqsave(&nesqp->lock, qplockflags);
765 if (nesqp->cm_id) {
766 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530767 "refcount = %d: HIT A "
768 "NES_TIMER_TYPE_CLOSE with something "
769 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
770 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800771 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
772 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
773 nesqp->ibqp_state = IB_QPS_ERR;
774 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
775 nes_cm_disconn(nesqp);
776 } else {
777 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
778 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530779 "refcount = %d: HIT A "
780 "NES_TIMER_TYPE_CLOSE with nothing "
781 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
782 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800783 }
784 } else if (rem_node) {
785 /* TIME_WAIT state */
786 rem_ref_cm_node(cm_node->cm_core, cm_node);
787 }
788 if (cm_node->cm_id)
789 cm_id->rem_ref(cm_id);
790 kfree(recv_entry);
791 cm_node->recv_entry = NULL;
792}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800793
794/**
795 * nes_cm_timer_tick
796 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700797static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800798{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800799 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800800 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800801 struct nes_cm_node *cm_node;
802 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800803 struct list_head *list_core_temp;
804 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800805 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800806 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700807 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800808 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800809
Faisal Latif879e5bd2008-11-21 20:50:41 -0600810 struct list_head timer_list;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530811
Faisal Latif879e5bd2008-11-21 20:50:41 -0600812 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800813 spin_lock_irqsave(&cm_core->ht_lock, flags);
814
Faisal Latif6492cdf2008-07-24 20:50:45 -0700815 list_for_each_safe(list_node, list_core_temp,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530816 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800817 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800818 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600819 add_ref_cm_node(cm_node);
820 list_add(&cm_node->timer_entry, &timer_list);
821 }
822 }
823 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
824
825 list_for_each_safe(list_node, list_core_temp, &timer_list) {
826 cm_node = container_of(list_node, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530827 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800828 recv_entry = cm_node->recv_entry;
829
830 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700831 if (time_after(recv_entry->timetosend, jiffies)) {
832 if (nexttimeout > recv_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530833 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800834 nexttimeout = recv_entry->timetosend;
835 settimer = 1;
836 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530837 } else {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800838 handle_recv_entry(cm_node, 1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530839 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800840 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800841
842 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700843 do {
844 send_entry = cm_node->send_entry;
845 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600846 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800847 if (time_after(send_entry->timetosend, jiffies)) {
848 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700849 if ((nexttimeout >
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530850 send_entry->timetosend) ||
851 !settimer) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700852 nexttimeout =
853 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800854 settimer = 1;
855 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800856 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700857 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800858 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800859 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800860 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700861
862 if ((cm_node->state == NES_CM_STATE_TSA) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530863 (cm_node->state == NES_CM_STATE_CLOSED)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700864 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600865 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800866 }
867
Faisal Latif6492cdf2008-07-24 20:50:45 -0700868 if (!send_entry->retranscount ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530869 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800870 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700871 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800872
Faisal Latif6492cdf2008-07-24 20:50:45 -0700873 spin_unlock_irqrestore(
874 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800875 nes_retrans_expired(cm_node);
876 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700877 spin_lock_irqsave(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530878 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600879 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800880 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800881 atomic_inc(&send_entry->skb->users);
882 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700883 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530884 "for node %p, jiffies = %lu, time to send = "
885 "%lu, retranscount = %u, send_entry->seq_num = "
886 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
887 "0x%08X\n", send_entry, cm_node, jiffies,
888 send_entry->timetosend,
889 send_entry->retranscount,
890 send_entry->seq_num,
891 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800892
Faisal Latif6492cdf2008-07-24 20:50:45 -0700893 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530894 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800895 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700896 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800897 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700898 nes_debug(NES_DBG_CM, "rexmit failed for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530899 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800900 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800901 send_entry->retrycount--;
902 nexttimeout = jiffies + NES_SHORT_TIME;
903 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600904 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800905 } else {
906 cm_packets_sent++;
907 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700908 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530909 "%u, retry count = %u.\n",
910 send_entry->retranscount,
911 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800912 if (send_entry->send_retrans) {
913 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700914 timetosend = (NES_RETRY_TIMEOUT <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530915 (NES_DEFAULT_RETRANS - send_entry->retranscount));
Faisal Latif4e9c3902009-04-27 13:39:36 -0700916
Faisal Latif6492cdf2008-07-24 20:50:45 -0700917 send_entry->timetosend = jiffies +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530918 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700919 if (nexttimeout > send_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530920 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800921 nexttimeout = send_entry->timetosend;
922 settimer = 1;
923 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800924 } else {
925 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700926 close_when_complete =
927 send_entry->close_when_complete;
928 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530929 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700930 free_retrans_entry(cm_node);
931 if (close_when_complete)
932 rem_ref_cm_node(cm_node->cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530933 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800934 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700935 } while (0);
936
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800937 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700938 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800939 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800940
941 if (settimer) {
942 if (!timer_pending(&cm_core->tcp_timer)) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530943 cm_core->tcp_timer.expires = nexttimeout;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800944 add_timer(&cm_core->tcp_timer);
945 }
946 }
947}
948
949
950/**
951 * send_syn
952 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700953static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530954 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800955{
956 int ret;
957 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800958 char optionsbuffer[sizeof(struct option_mss) +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530959 sizeof(struct option_windowscale) + sizeof(struct option_base) +
960 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800961
962 int optionssize = 0;
963 /* Sending MSS option */
964 union all_known_options *options;
965
966 if (!cm_node)
967 return -EINVAL;
968
969 options = (union all_known_options *)&optionsbuffer[optionssize];
970 options->as_mss.optionnum = OPTION_NUMBER_MSS;
971 options->as_mss.length = sizeof(struct option_mss);
972 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
973 optionssize += sizeof(struct option_mss);
974
975 options = (union all_known_options *)&optionsbuffer[optionssize];
976 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
977 options->as_windowscale.length = sizeof(struct option_windowscale);
978 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
979 optionssize += sizeof(struct option_windowscale);
980
Faisal Latif6492cdf2008-07-24 20:50:45 -0700981 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800982 options = (union all_known_options *)&optionsbuffer[optionssize];
983 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
984 options->as_base.length = sizeof(struct option_base);
985 optionssize += sizeof(struct option_base);
986 /* we need the size to be a multiple of 4 */
987 options = (union all_known_options *)&optionsbuffer[optionssize];
988 options->as_end = 1;
989 optionssize += 1;
990 options = (union all_known_options *)&optionsbuffer[optionssize];
991 options->as_end = 1;
992 optionssize += 1;
993 }
994
995 options = (union all_known_options *)&optionsbuffer[optionssize];
996 options->as_end = OPTION_NUMBER_END;
997 optionssize += 1;
998
Faisal Latif6492cdf2008-07-24 20:50:45 -0700999 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001000 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001001 if (!skb) {
1002 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1003 return -1;
1004 }
1005
1006 if (sendack)
1007 flags |= SET_ACK;
1008
1009 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
1010 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1011
1012 return ret;
1013}
1014
1015
1016/**
1017 * send_reset
1018 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001019static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001020{
1021 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001022 int flags = SET_RST | SET_ACK;
1023
Faisal Latif6492cdf2008-07-24 20:50:45 -07001024 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001025 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001026 if (!skb) {
1027 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001028 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001029 }
1030
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001031 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
1032 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
1033
1034 return ret;
1035}
1036
1037
1038/**
1039 * send_ack
1040 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001041static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001042{
1043 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001044
1045 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001046 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001047
1048 if (!skb) {
1049 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1050 return -1;
1051 }
1052
1053 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
1054 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
1055
1056 return ret;
1057}
1058
1059
1060/**
1061 * send_fin
1062 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07001063static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001064{
1065 int ret;
1066
1067 /* if we didn't get a frame get one */
1068 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001069 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001070
1071 if (!skb) {
1072 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1073 return -1;
1074 }
1075
1076 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
1077 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1078
1079 return ret;
1080}
1081
1082
1083/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001084 * find_node - find a cm node that matches the reference cm node
1085 */
1086static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301087 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001088{
1089 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001090 struct list_head *hte;
1091 struct nes_cm_node *cm_node;
1092
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001093 /* get a handle on the hte */
1094 hte = &cm_core->connected_nodes;
1095
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001096 /* walk list and find cm_node associated with this session ID */
1097 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001098 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001099 /* compare quad, return node handle if a match */
1100 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301101 cm_node->loc_addr, cm_node->loc_port,
1102 loc_addr, loc_port,
1103 cm_node->rem_addr, cm_node->rem_port,
1104 rem_addr, rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001105 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301106 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001107 add_ref_cm_node(cm_node);
1108 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1109 return cm_node;
1110 }
1111 }
1112 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1113
1114 /* no owner node */
1115 return NULL;
1116}
1117
1118
1119/**
1120 * find_listener - find a cm node listening on this addr-port pair
1121 */
1122static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301123 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001124{
1125 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001126 struct nes_cm_listener *listen_node;
1127
1128 /* walk list and find cm_node associated with this session ID */
1129 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001130 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001131 /* compare node pair, return node handle if a match */
1132 if (((listen_node->loc_addr == dst_addr) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301133 listen_node->loc_addr == 0x00000000) &&
1134 (listen_node->loc_port == dst_port) &&
1135 (listener_state & listen_node->listener_state)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001136 atomic_inc(&listen_node->ref_count);
1137 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1138 return listen_node;
1139 }
1140 }
1141 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1142
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001143 /* no listener */
1144 return NULL;
1145}
1146
1147
1148/**
1149 * add_hte_node - add a cm node to the hash table
1150 */
1151static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1152{
1153 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001154 struct list_head *hte;
1155
1156 if (!cm_node || !cm_core)
1157 return -EINVAL;
1158
Faisal Latif6492cdf2008-07-24 20:50:45 -07001159 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301160 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001161
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001162 spin_lock_irqsave(&cm_core->ht_lock, flags);
1163
1164 /* get a handle on the hash table element (list head for this slot) */
1165 hte = &cm_core->connected_nodes;
1166 list_add_tail(&cm_node->list, hte);
1167 atomic_inc(&cm_core->ht_node_cnt);
1168
1169 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1170
1171 return 0;
1172}
1173
1174
1175/**
1176 * mini_cm_dec_refcnt_listen
1177 */
1178static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301179 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001180{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001181 int ret = -EINVAL;
1182 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001183 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001184 struct list_head *list_pos = NULL;
1185 struct list_head *list_temp = NULL;
1186 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -06001187 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001188
1189 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301190 "refcnt=%d\n", listener, free_hanging_nodes,
1191 atomic_read(&listener->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001192 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -06001193 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001194 if (free_hanging_nodes) {
1195 spin_lock_irqsave(&cm_core->ht_lock, flags);
1196 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -06001197 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001198 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301199 list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001200 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -06001201 (!cm_node->accelerated)) {
1202 add_ref_cm_node(cm_node);
1203 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001204 }
1205 }
1206 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1207 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001208
1209 list_for_each_safe(list_pos, list_temp, &reset_list) {
1210 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301211 reset_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001212 {
1213 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -08001214 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001215 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
1216 rem_ref_cm_node(cm_node->cm_core, cm_node);
1217 } else {
1218 if (!loopback) {
1219 cleanup_retrans_entry(cm_node);
1220 err = send_reset(cm_node, NULL);
1221 if (err) {
1222 cm_node->state =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301223 NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001224 WARN_ON(1);
1225 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08001226 old_state = cm_node->state;
1227 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1228 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1229 rem_ref_cm_node(
1230 cm_node->cm_core,
1231 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001232 }
1233 } else {
1234 struct nes_cm_event event;
1235
1236 event.cm_node = loopback;
1237 event.cm_info.rem_addr =
1238 loopback->rem_addr;
1239 event.cm_info.loc_addr =
1240 loopback->loc_addr;
1241 event.cm_info.rem_port =
1242 loopback->rem_port;
1243 event.cm_info.loc_port =
1244 loopback->loc_port;
1245 event.cm_info.cm_id = loopback->cm_id;
Faisal Latif43093b92010-02-12 19:57:04 +00001246 add_ref_cm_node(loopback);
1247 loopback->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001248 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001249 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001250
Faisal Latif9d5ab132009-03-06 15:15:01 -08001251 rem_ref_cm_node(cm_node->cm_core,
1252 cm_node);
1253
1254 }
1255 }
1256 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001257 }
1258
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001259 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1260 if (!atomic_dec_return(&listener->ref_count)) {
1261 list_del(&listener->list);
1262
1263 /* decrement our listen node count */
1264 atomic_dec(&cm_core->listen_node_cnt);
1265
1266 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1267
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301268 if (listener->nesvnic)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001269 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301270 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001271
1272 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1273
1274 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001275 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001276 ret = 0;
Faisal Latif6e10d2e2010-02-12 19:55:03 +00001277 atomic_inc(&cm_listens_destroyed);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001278 } else {
1279 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1280 }
1281 if (listener) {
1282 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1283 nes_debug(NES_DBG_CM, "destroying listener (%p)"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301284 " with non-zero pending accepts=%u\n",
1285 listener, atomic_read(&listener->pend_accepts_cnt));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001286 }
1287
1288 return ret;
1289}
1290
1291
1292/**
1293 * mini_cm_del_listen
1294 */
1295static int mini_cm_del_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301296 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001297{
1298 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1299 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1300 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1301}
1302
1303
1304/**
1305 * mini_cm_accelerated
1306 */
1307static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301308 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001309{
1310 u32 was_timer_set;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301311
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001312 cm_node->accelerated = 1;
1313
1314 if (cm_node->accept_pend) {
1315 BUG_ON(!cm_node->listener);
1316 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001317 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001318 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1319 }
1320
1321 was_timer_set = timer_pending(&cm_core->tcp_timer);
1322 if (!was_timer_set) {
1323 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1324 add_timer(&cm_core->tcp_timer);
1325 }
1326
1327 return 0;
1328}
1329
1330
1331/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001332 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001333 */
Faisal Latif7a576df2009-12-09 15:54:33 -08001334static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001335{
1336 struct rtable *rt;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001337 struct neighbour *neigh;
Faisal Latif7a576df2009-12-09 15:54:33 -08001338 int rc = arpindex;
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001339 struct net_device *netdev;
Faisal Latif7a576df2009-12-09 15:54:33 -08001340 struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001341
David S. Miller78fbfd82011-03-12 00:00:52 -05001342 rt = ip_route_output(&init_net, htonl(dst_ip), 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001343 if (IS_ERR(rt)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001344 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301345 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001346 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001347 }
1348
Roland Dreiercf55bb22011-03-24 17:13:20 -07001349 if (netif_is_bond_slave(nesvnic->netdev))
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001350 netdev = nesvnic->netdev->master;
1351 else
1352 netdev = nesvnic->netdev;
1353
David Miller40e2bb52011-12-02 16:52:27 +00001354 rcu_read_lock();
1355 neigh = dst_get_neighbour_noref(&rt->dst);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001356 if (neigh) {
1357 if (neigh->nud_state & NUD_VALID) {
1358 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001359 " is %pM, Gateway is 0x%08X \n", dst_ip,
1360 neigh->ha, ntohl(rt->rt_gateway));
Faisal Latif7a576df2009-12-09 15:54:33 -08001361
1362 if (arpindex >= 0) {
1363 if (!memcmp(nesadapter->arp_table[arpindex].mac_addr,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301364 neigh->ha, ETH_ALEN)) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001365 /* Mac address same as in nes_arp_table */
Faisal Latif7a576df2009-12-09 15:54:33 -08001366 ip_rt_put(rt);
1367 return rc;
1368 }
1369
1370 nes_manage_arp_cache(nesvnic->netdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301371 nesadapter->arp_table[arpindex].mac_addr,
1372 dst_ip, NES_ARP_DELETE);
Faisal Latif7a576df2009-12-09 15:54:33 -08001373 }
1374
Bob Sharp7191a0a2008-10-03 12:21:19 -07001375 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1376 dst_ip, NES_ARP_ADD);
1377 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1378 NES_ARP_RESOLVE);
David Miller40e2bb52011-12-02 16:52:27 +00001379 } else {
1380 neigh_event_send(neigh, NULL);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001381 }
Bob Sharp7191a0a2008-10-03 12:21:19 -07001382 }
David Miller40e2bb52011-12-02 16:52:27 +00001383 rcu_read_unlock();
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001384 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001385 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001386}
1387
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001388/**
1389 * make_cm_node - create a new instance of a cm node
1390 */
1391static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301392 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1393 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001394{
1395 struct nes_cm_node *cm_node;
1396 struct timespec ts;
Faisal Latif7a576df2009-12-09 15:54:33 -08001397 int oldarpindex = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001398 int arpindex = 0;
1399 struct nes_device *nesdev;
1400 struct nes_adapter *nesadapter;
1401
1402 /* create an hte and cm_node for this instance */
1403 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1404 if (!cm_node)
1405 return NULL;
1406
1407 /* set our node specific transport info */
1408 cm_node->loc_addr = cm_info->loc_addr;
1409 cm_node->rem_addr = cm_info->rem_addr;
1410 cm_node->loc_port = cm_info->loc_port;
1411 cm_node->rem_port = cm_info->rem_port;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301412
1413 cm_node->mpa_frame_rev = mpa_version;
1414 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
1415 cm_node->ird_size = IETF_NO_IRD_ORD;
1416 cm_node->ord_size = IETF_NO_IRD_ORD;
1417
Harvey Harrison63779432008-10-31 00:56:00 -07001418 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1419 &cm_node->loc_addr, cm_node->loc_port,
1420 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001421 cm_node->listener = listener;
1422 cm_node->netdev = nesvnic->netdev;
1423 cm_node->cm_id = cm_info->cm_id;
1424 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1425
Faisal Latif6492cdf2008-07-24 20:50:45 -07001426 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301427 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001428
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001429 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001430
1431 cm_node->loopbackpartner = NULL;
1432 atomic_set(&cm_node->ref_count, 1);
1433 /* associate our parent CM core */
1434 cm_node->cm_core = cm_core;
1435 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1436 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1437 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301438 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001439 ts = current_kernel_time();
1440 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1441 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301442 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001443 cm_node->tcp_cntxt.rcv_nxt = 0;
1444 /* get a unique session ID , add thread_id to an upcounter to handle race */
1445 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001446 cm_node->conn_type = cm_info->conn_type;
1447 cm_node->apbvt_set = 0;
1448 cm_node->accept_pend = 0;
1449
1450 cm_node->nesvnic = nesvnic;
1451 /* get some device handles, for arp lookup */
1452 nesdev = nesvnic->nesdev;
1453 nesadapter = nesdev->nesadapter;
1454
1455 cm_node->loopbackpartner = NULL;
Faisal Latif7a576df2009-12-09 15:54:33 -08001456
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001457 /* get the mac addr for the remote node */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301458 if (ipv4_is_loopback(htonl(cm_node->rem_addr))) {
Chien Tung1ee86552008-11-21 20:51:04 -06001459 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301460 } else {
Faisal Latif7a576df2009-12-09 15:54:33 -08001461 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1462 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);
Faisal Latif7a576df2009-12-09 15:54:33 -08001463 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001464 if (arpindex < 0) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001465 kfree(cm_node);
1466 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001467 }
1468
1469 /* copy the mac addr to node context */
1470 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001471 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1472 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001473
1474 add_hte_node(cm_core, cm_node);
1475 atomic_inc(&cm_nodes_created);
1476
1477 return cm_node;
1478}
1479
1480
1481/**
1482 * add_ref_cm_node - destroy an instance of a cm node
1483 */
1484static int add_ref_cm_node(struct nes_cm_node *cm_node)
1485{
1486 atomic_inc(&cm_node->ref_count);
1487 return 0;
1488}
1489
1490
1491/**
1492 * rem_ref_cm_node - destroy an instance of a cm node
1493 */
1494static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301495 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001496{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001497 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001498 struct nes_qp *nesqp;
1499
1500 if (!cm_node)
1501 return -EINVAL;
1502
1503 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1504 if (atomic_dec_return(&cm_node->ref_count)) {
1505 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1506 return 0;
1507 }
1508 list_del(&cm_node->list);
1509 atomic_dec(&cm_core->ht_node_cnt);
1510 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1511
1512 /* if the node is destroyed before connection was accelerated */
1513 if (!cm_node->accelerated && cm_node->accept_pend) {
1514 BUG_ON(!cm_node->listener);
1515 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1516 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1517 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001518 WARN_ON(cm_node->send_entry);
1519 if (cm_node->recv_entry)
1520 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001521 if (cm_node->listener) {
1522 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1523 } else {
1524 if (cm_node->apbvt_set && cm_node->nesvnic) {
1525 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301526 PCI_FUNC(
1527 cm_node->nesvnic->nesdev->pcidev->devfn),
1528 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001529 }
1530 }
1531
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001532 atomic_dec(&cm_core->node_cnt);
1533 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001534 nesqp = cm_node->nesqp;
1535 if (nesqp) {
1536 nesqp->cm_node = NULL;
1537 nes_rem_ref(&nesqp->ibqp);
1538 cm_node->nesqp = NULL;
1539 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001540
Faisal Latif6492cdf2008-07-24 20:50:45 -07001541 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001542 return 0;
1543}
1544
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001545/**
1546 * process_options
1547 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001548static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301549 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001550{
1551 u32 tmp;
1552 u32 offset = 0;
1553 union all_known_options *all_options;
1554 char got_mss_option = 0;
1555
1556 while (offset < optionsize) {
1557 all_options = (union all_known_options *)(optionsloc + offset);
1558 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001559 case OPTION_NUMBER_END:
1560 offset = optionsize;
1561 break;
1562 case OPTION_NUMBER_NONE:
1563 offset += 1;
1564 continue;
1565 case OPTION_NUMBER_MSS:
1566 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301567 "Size: %d\n", __func__,
1568 all_options->as_mss.length, offset, optionsize);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001569 got_mss_option = 1;
1570 if (all_options->as_mss.length != 4) {
1571 return 1;
1572 } else {
1573 tmp = ntohs(all_options->as_mss.mss);
1574 if (tmp > 0 && tmp <
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301575 cm_node->tcp_cntxt.mss)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001576 cm_node->tcp_cntxt.mss = tmp;
1577 }
1578 break;
1579 case OPTION_NUMBER_WINDOW_SCALE:
1580 cm_node->tcp_cntxt.snd_wscale =
1581 all_options->as_windowscale.shiftcount;
1582 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001583 default:
1584 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301585 all_options->as_base.optionnum);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001586 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001587 }
1588 offset += all_options->as_base.length;
1589 }
1590 if ((!got_mss_option) && (syn_packet))
1591 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1592 return 0;
1593}
1594
Faisal Latif6492cdf2008-07-24 20:50:45 -07001595static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001596{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001597 atomic_inc(&cm_accel_dropped_pkts);
1598 dev_kfree_skb_any(skb);
1599}
1600
Faisal Latif9d5ab132009-03-06 15:15:01 -08001601static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001602{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001603 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301604 "refcnt=%d\n", cm_node, cm_node->state,
1605 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001606 switch (cm_node->state) {
1607 case NES_CM_STATE_SYN_RCVD:
1608 case NES_CM_STATE_SYN_SENT:
1609 case NES_CM_STATE_ESTABLISHED:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001610 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001611 cm_node->tcp_cntxt.rcv_nxt++;
1612 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001613 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001614 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001615 break;
Faisal Latifb1190d32009-12-09 15:54:32 -08001616 case NES_CM_STATE_MPAREQ_SENT:
1617 create_event(cm_node, NES_CM_EVENT_ABORTED);
1618 cm_node->tcp_cntxt.rcv_nxt++;
1619 cleanup_retrans_entry(cm_node);
1620 cm_node->state = NES_CM_STATE_CLOSED;
1621 add_ref_cm_node(cm_node);
1622 send_reset(cm_node, NULL);
1623 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001624 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001625 cm_node->tcp_cntxt.rcv_nxt++;
1626 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001627 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001628 send_ack(cm_node, NULL);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001629 /* Wait for ACK as this is simultaneous close..
Faisal Latif9d5ab132009-03-06 15:15:01 -08001630 * After we receive ACK, do not send anything..
1631 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001632 break;
1633 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001634 cm_node->tcp_cntxt.rcv_nxt++;
1635 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001636 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001637 send_ack(cm_node, NULL);
1638 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1639 break;
1640 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001641 cm_node->tcp_cntxt.rcv_nxt++;
1642 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001643 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001644 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001645 break;
1646 case NES_CM_STATE_TSA:
1647 default:
1648 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1649 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001650 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001651 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001652}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001653
Faisal Latif6492cdf2008-07-24 20:50:45 -07001654
1655static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1656 struct tcphdr *tcph)
1657{
1658
1659 int reset = 0; /* whether to send reset in case of err.. */
1660 atomic_inc(&cm_resets_recvd);
1661 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1662 " refcnt=%d\n", cm_node, cm_node->state,
1663 atomic_read(&cm_node->ref_count));
1664 cleanup_retrans_entry(cm_node);
1665 switch (cm_node->state) {
1666 case NES_CM_STATE_SYN_SENT:
1667 case NES_CM_STATE_MPAREQ_SENT:
1668 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1669 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1670 cm_node->listener, cm_node->state);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301671 switch (cm_node->mpa_frame_rev) {
1672 case IETF_MPA_V2:
1673 cm_node->mpa_frame_rev = IETF_MPA_V1;
1674 /* send a syn and goto syn sent state */
1675 cm_node->state = NES_CM_STATE_SYN_SENT;
1676 if (send_syn(cm_node, 0, NULL)) {
1677 active_open_err(cm_node, skb, reset);
1678 }
1679 break;
1680 case IETF_MPA_V1:
1681 default:
1682 active_open_err(cm_node, skb, reset);
1683 break;
1684 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001685 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001686 case NES_CM_STATE_MPAREQ_RCVD:
Dan Carpenter4d8d6382010-09-15 18:32:39 +02001687 atomic_inc(&cm_node->passive_state);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001688 dev_kfree_skb_any(skb);
1689 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001690 case NES_CM_STATE_ESTABLISHED:
1691 case NES_CM_STATE_SYN_RCVD:
1692 case NES_CM_STATE_LISTENING:
1693 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1694 passive_open_err(cm_node, skb, reset);
1695 break;
1696 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001697 active_open_err(cm_node, skb, reset);
1698 break;
1699 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001700 drop_packet(skb);
1701 break;
Faisal Latifdae58722010-08-14 21:04:56 +00001702 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif68237a02009-06-22 22:53:28 -07001703 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001704 case NES_CM_STATE_LAST_ACK:
1705 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001706 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001707 cm_node->state = NES_CM_STATE_CLOSED;
1708 rem_ref_cm_node(cm_node->cm_core, cm_node);
1709 drop_packet(skb);
1710 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001711 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001712 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001713 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001714 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001715}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001716
Faisal Latif9d5ab132009-03-06 15:15:01 -08001717
1718static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001719{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301720 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001721 int datasize = skb->len;
1722 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001723
1724 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301725 u32 res_type;
1726
Faisal Latif9d5ab132009-03-06 15:15:01 -08001727 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1728 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001729 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001730 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001731 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301732 "cm_node=%p listener=%p state=%d\n", __func__,
1733 __LINE__, cm_node, cm_node->listener,
1734 cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001735 active_open_err(cm_node, skb, 1);
1736 } else {
1737 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001738 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001739 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001740 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001741
1742 switch (cm_node->state) {
1743 case NES_CM_STATE_ESTABLISHED:
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301744 if (res_type == NES_MPA_REQUEST_REJECT)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001745 /*BIG problem as we are receiving the MPA.. So should
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301746 * not be REJECT.. This is Passive Open.. We can
1747 * only receive it Reject for Active Open...*/
Faisal Latif9d5ab132009-03-06 15:15:01 -08001748 WARN_ON(1);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001749 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1750 type = NES_CM_EVENT_MPA_REQ;
1751 atomic_set(&cm_node->passive_state,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301752 NES_PASSIVE_STATE_INDICATED);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001753 break;
1754 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001755 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001756 if (res_type == NES_MPA_REQUEST_REJECT) {
1757 type = NES_CM_EVENT_MPA_REJECT;
1758 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1759 } else {
1760 type = NES_CM_EVENT_CONNECTED;
1761 cm_node->state = NES_CM_STATE_TSA;
1762 }
1763
1764 break;
1765 default:
1766 WARN_ON(1);
1767 break;
1768 }
1769 dev_kfree_skb_any(skb);
1770 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001771}
1772
1773static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1774{
1775 switch (cm_node->state) {
1776 case NES_CM_STATE_SYN_SENT:
1777 case NES_CM_STATE_MPAREQ_SENT:
1778 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301779 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1780 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001781 active_open_err(cm_node, skb, 1);
1782 break;
1783 case NES_CM_STATE_ESTABLISHED:
1784 case NES_CM_STATE_SYN_RCVD:
1785 passive_open_err(cm_node, skb, 1);
1786 break;
1787 case NES_CM_STATE_TSA:
1788 default:
1789 drop_packet(skb);
1790 }
1791}
1792
1793static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301794 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001795{
1796 int err;
1797
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301798 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001799 if (err)
1800 active_open_err(cm_node, skb, 1);
1801
1802 return err;
1803}
1804
1805static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301806 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001807{
1808 int err = 0;
1809 u32 seq;
1810 u32 ack_seq;
1811 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1812 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1813 u32 rcv_wnd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301814
Faisal Latif6492cdf2008-07-24 20:50:45 -07001815 seq = ntohl(tcph->seq);
1816 ack_seq = ntohl(tcph->ack_seq);
1817 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1818 if (ack_seq != loc_seq_num)
1819 err = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301820 else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001821 err = 1;
1822 if (err) {
1823 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301824 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1825 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001826 indicate_pkt_err(cm_node, skb);
1827 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301828 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1829 rcv_wnd);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001830 }
1831 return err;
1832}
1833
1834/*
1835 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1836 * is created with a listener or it may comein as rexmitted packet which in
1837 * that case will be just dropped.
1838 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001839static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301840 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001841{
1842 int ret;
1843 u32 inc_sequence;
1844 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001845
1846 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001847 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001848 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001849
Faisal Latif6492cdf2008-07-24 20:50:45 -07001850 switch (cm_node->state) {
1851 case NES_CM_STATE_SYN_SENT:
1852 case NES_CM_STATE_MPAREQ_SENT:
1853 /* Rcvd syn on active open connection*/
1854 active_open_err(cm_node, skb, 1);
1855 break;
1856 case NES_CM_STATE_LISTENING:
1857 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001858 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301859 cm_node->listener->backlog) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001860 nes_debug(NES_DBG_CM, "drop syn due to backlog "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301861 "pressure \n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07001862 cm_backlog_drops++;
1863 passive_open_err(cm_node, skb, 0);
1864 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001865 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001866 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301867 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001868 if (ret) {
1869 passive_open_err(cm_node, skb, 0);
1870 /* drop pkt */
1871 break;
1872 }
1873 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1874 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001875 cm_node->accept_pend = 1;
1876 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1877
Faisal Latif6492cdf2008-07-24 20:50:45 -07001878 cm_node->state = NES_CM_STATE_SYN_RCVD;
1879 send_syn(cm_node, 1, skb);
1880 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001881 case NES_CM_STATE_CLOSED:
1882 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001883 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001884 send_reset(cm_node, skb);
1885 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001886 case NES_CM_STATE_TSA:
1887 case NES_CM_STATE_ESTABLISHED:
1888 case NES_CM_STATE_FIN_WAIT1:
1889 case NES_CM_STATE_FIN_WAIT2:
1890 case NES_CM_STATE_MPAREQ_RCVD:
1891 case NES_CM_STATE_LAST_ACK:
1892 case NES_CM_STATE_CLOSING:
1893 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001894 default:
1895 drop_packet(skb);
1896 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001897 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001898}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001899
Faisal Latif6492cdf2008-07-24 20:50:45 -07001900static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301901 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001902{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001903 int ret;
1904 u32 inc_sequence;
1905 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001906
Faisal Latif6492cdf2008-07-24 20:50:45 -07001907 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001908 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001909 inc_sequence = ntohl(tcph->seq);
1910 switch (cm_node->state) {
1911 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001912 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001913 /* active open */
1914 if (check_syn(cm_node, tcph, skb))
1915 return;
1916 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1917 /* setup options */
1918 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1919 if (ret) {
1920 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301921 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001922 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001923 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001924 cleanup_retrans_entry(cm_node);
1925 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1926 send_mpa_request(cm_node, skb);
1927 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1928 break;
1929 case NES_CM_STATE_MPAREQ_RCVD:
1930 /* passive open, so should not be here */
1931 passive_open_err(cm_node, skb, 1);
1932 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001933 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001934 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1935 cleanup_retrans_entry(cm_node);
1936 cm_node->state = NES_CM_STATE_CLOSED;
1937 send_reset(cm_node, skb);
1938 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001939 case NES_CM_STATE_CLOSED:
1940 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1941 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001942 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001943 send_reset(cm_node, skb);
1944 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001945 case NES_CM_STATE_ESTABLISHED:
1946 case NES_CM_STATE_FIN_WAIT1:
1947 case NES_CM_STATE_FIN_WAIT2:
1948 case NES_CM_STATE_LAST_ACK:
1949 case NES_CM_STATE_TSA:
1950 case NES_CM_STATE_CLOSING:
1951 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001952 case NES_CM_STATE_MPAREQ_SENT:
1953 default:
1954 drop_packet(skb);
1955 break;
1956 }
1957}
1958
Faisal Latif109d67e2009-04-27 13:41:06 -07001959static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301960 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001961{
1962 int datasize = 0;
1963 u32 inc_sequence;
Faisal Latif109d67e2009-04-27 13:41:06 -07001964 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06001965 int optionsize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301966
Faisal Latifabb77252008-11-21 20:50:52 -06001967 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001968
Faisal Latif6492cdf2008-07-24 20:50:45 -07001969 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07001970 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001971
1972 skb_pull(skb, tcph->doff << 2);
1973 inc_sequence = ntohl(tcph->seq);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001974 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001975 switch (cm_node->state) {
1976 case NES_CM_STATE_SYN_RCVD:
1977 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07001978 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001979 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1980 if (ret)
1981 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001982 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1983 cm_node->state = NES_CM_STATE_ESTABLISHED;
1984 if (datasize) {
1985 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001986 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301987 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001988 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301989 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001990 break;
1991 case NES_CM_STATE_ESTABLISHED:
1992 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001993 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001994 if (datasize) {
1995 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001996 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301997 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001998 drop_packet(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301999 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002000 break;
2001 case NES_CM_STATE_MPAREQ_SENT:
2002 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2003 if (datasize) {
2004 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002005 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302006 } else { /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002007 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302008 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002009 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002010 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002011 cleanup_retrans_entry(cm_node);
2012 cm_node->state = NES_CM_STATE_CLOSED;
2013 send_reset(cm_node, skb);
2014 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002015 case NES_CM_STATE_CLOSED:
2016 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08002017 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002018 send_reset(cm_node, skb);
2019 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002020 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002021 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002022 cleanup_retrans_entry(cm_node);
2023 cm_node->state = NES_CM_STATE_CLOSED;
2024 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002025 rem_ref_cm_node(cm_node->cm_core, cm_node);
2026 drop_packet(skb);
2027 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002028 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002029 cleanup_retrans_entry(cm_node);
2030 drop_packet(skb);
2031 cm_node->state = NES_CM_STATE_FIN_WAIT2;
2032 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002033 case NES_CM_STATE_SYN_SENT:
2034 case NES_CM_STATE_FIN_WAIT2:
2035 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002036 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002037 case NES_CM_STATE_UNKNOWN:
2038 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002039 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002040 drop_packet(skb);
2041 break;
2042 }
Faisal Latif109d67e2009-04-27 13:41:06 -07002043 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002044}
2045
2046
2047
2048static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302049 struct sk_buff *skb, int optionsize, int passive)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002050{
2051 u8 *optionsloc = (u8 *)&tcph[1];
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302052
Faisal Latif6492cdf2008-07-24 20:50:45 -07002053 if (optionsize) {
2054 if (process_options(cm_node, optionsloc, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302055 (u32)tcph->syn)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002056 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302057 __func__, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002058 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06002059 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002060 else
Faisal Latifabb77252008-11-21 20:50:52 -06002061 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002062 return 1;
2063 }
2064 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002065
2066 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302067 cm_node->tcp_cntxt.snd_wscale;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002068
Faisal Latif6492cdf2008-07-24 20:50:45 -07002069 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002070 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002071 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002072}
2073
Faisal Latif6492cdf2008-07-24 20:50:45 -07002074/*
2075 * active_open_err() will send reset() if flag set..
2076 * It will also send ABORT event.
2077 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002078static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302079 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002080{
2081 cleanup_retrans_entry(cm_node);
2082 if (reset) {
2083 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302084 "state=%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002085 add_ref_cm_node(cm_node);
2086 send_reset(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302087 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002088 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302089 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002090
2091 cm_node->state = NES_CM_STATE_CLOSED;
2092 create_event(cm_node, NES_CM_EVENT_ABORTED);
2093}
2094
2095/*
2096 * passive_open_err() will either do a reset() or will free up the skb and
2097 * remove the cm_node.
2098 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002099static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302100 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002101{
2102 cleanup_retrans_entry(cm_node);
2103 cm_node->state = NES_CM_STATE_CLOSED;
2104 if (reset) {
2105 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302106 "cm_node=%p state =%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002107 send_reset(cm_node, skb);
2108 } else {
2109 dev_kfree_skb_any(skb);
2110 rem_ref_cm_node(cm_node->cm_core, cm_node);
2111 }
2112}
2113
2114/*
2115 * free_retrans_entry() routines assumes that the retrans_list_lock has
2116 * been acquired before calling.
2117 */
2118static void free_retrans_entry(struct nes_cm_node *cm_node)
2119{
2120 struct nes_timer_entry *send_entry;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302121
Faisal Latif6492cdf2008-07-24 20:50:45 -07002122 send_entry = cm_node->send_entry;
2123 if (send_entry) {
2124 cm_node->send_entry = NULL;
2125 dev_kfree_skb_any(send_entry->skb);
2126 kfree(send_entry);
2127 rem_ref_cm_node(cm_node->cm_core, cm_node);
2128 }
2129}
2130
2131static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
2132{
2133 unsigned long flags;
2134
2135 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
2136 free_retrans_entry(cm_node);
2137 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
2138}
2139
2140/**
2141 * process_packet
2142 * Returns skb if to be freed, else it will return NULL if already used..
2143 */
2144static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302145 struct nes_cm_core *cm_core)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002146{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302147 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002148 struct tcphdr *tcph = tcp_hdr(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302149 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07002150 int ret = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302151
Faisal Latif6492cdf2008-07-24 20:50:45 -07002152 skb_pull(skb, ip_hdr(skb)->ihl << 2);
2153
2154 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302155 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
2156 tcph->ack, tcph->rst, tcph->fin);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002157
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302158 if (tcph->rst) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002159 pkt_type = NES_PKT_TYPE_RST;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302160 } else if (tcph->syn) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002161 pkt_type = NES_PKT_TYPE_SYN;
2162 if (tcph->ack)
2163 pkt_type = NES_PKT_TYPE_SYNACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302164 } else if (tcph->ack) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002165 pkt_type = NES_PKT_TYPE_ACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302166 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002167 if (tcph->fin)
2168 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002169
2170 switch (pkt_type) {
2171 case NES_PKT_TYPE_SYN:
2172 handle_syn_pkt(cm_node, skb, tcph);
2173 break;
2174 case NES_PKT_TYPE_SYNACK:
2175 handle_synack_pkt(cm_node, skb, tcph);
2176 break;
2177 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002178 ret = handle_ack_pkt(cm_node, skb, tcph);
2179 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08002180 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002181 break;
2182 case NES_PKT_TYPE_RST:
2183 handle_rst_pkt(cm_node, skb, tcph);
2184 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002185 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002186 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08002187 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07002188 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002189 break;
2190 }
2191}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002192
2193/**
2194 * mini_cm_listen - create a listen node with params
2195 */
2196static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302197 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002198{
2199 struct nes_cm_listener *listener;
2200 unsigned long flags;
2201
2202 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302203 cm_info->loc_addr, cm_info->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002204
2205 /* cannot have multiple matching listeners */
2206 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302207 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002208 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
2209 /* find automatically incs ref count ??? */
2210 atomic_dec(&listener->ref_count);
2211 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
2212 return NULL;
2213 }
2214
2215 if (!listener) {
2216 /* create a CM listen node (1/2 node to compare incoming traffic to) */
2217 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
2218 if (!listener) {
2219 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
2220 return NULL;
2221 }
2222
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002223 listener->loc_addr = htonl(cm_info->loc_addr);
2224 listener->loc_port = htons(cm_info->loc_port);
2225 listener->reused_node = 0;
2226
2227 atomic_set(&listener->ref_count, 1);
2228 }
2229 /* pasive case */
2230 /* find already inc'ed the ref count */
2231 else {
2232 listener->reused_node = 1;
2233 }
2234
2235 listener->cm_id = cm_info->cm_id;
2236 atomic_set(&listener->pend_accepts_cnt, 0);
2237 listener->cm_core = cm_core;
2238 listener->nesvnic = nesvnic;
2239 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002240
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002241 listener->conn_type = cm_info->conn_type;
2242 listener->backlog = cm_info->backlog;
2243 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
2244
2245 if (!listener->reused_node) {
2246 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2247 list_add(&listener->list, &cm_core->listen_list.list);
2248 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2249 atomic_inc(&cm_core->listen_node_cnt);
2250 }
2251
2252 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302253 " listener = %p, backlog = %d, cm_id = %p.\n",
2254 cm_info->loc_addr, cm_info->loc_port,
2255 listener, listener->backlog, listener->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002256
2257 return listener;
2258}
2259
2260
2261/**
2262 * mini_cm_connect - make a connection node with params
2263 */
Faisal Latif54c86a82008-09-30 14:47:27 -07002264static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302265 struct nes_vnic *nesvnic, u16 private_data_len,
2266 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002267{
2268 int ret = 0;
2269 struct nes_cm_node *cm_node;
2270 struct nes_cm_listener *loopbackremotelistener;
2271 struct nes_cm_node *loopbackremotenode;
2272 struct nes_cm_info loopback_cm_info;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302273 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002274
2275 /* create a CM connection node */
2276 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2277 if (!cm_node)
2278 return NULL;
2279
Glenn Streiff7495ab62008-04-29 13:46:54 -07002280 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002281 cm_node->tcp_cntxt.client = 1;
2282 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2283
2284 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002285 loopbackremotelistener = find_listener(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302286 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2287 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002288 if (loopbackremotelistener == NULL) {
2289 create_event(cm_node, NES_CM_EVENT_ABORTED);
2290 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002291 loopback_cm_info = *cm_info;
2292 loopback_cm_info.loc_port = cm_info->rem_port;
2293 loopback_cm_info.rem_port = cm_info->loc_port;
2294 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002295 loopbackremotenode = make_cm_node(cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302296 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002297 if (!loopbackremotenode) {
2298 rem_ref_cm_node(cm_node->cm_core, cm_node);
2299 return NULL;
2300 }
2301 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002302 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002303 loopbackremotenode->tcp_cntxt.rcv_wscale =
2304 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002305 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002306 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302307 private_data_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002308 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002309
Faisal Latif6492cdf2008-07-24 20:50:45 -07002310 /* we are done handling this state. */
2311 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002312 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002313 cm_node->tcp_cntxt.rcv_nxt =
2314 loopbackremotenode->tcp_cntxt.loc_seq_num;
2315 loopbackremotenode->tcp_cntxt.rcv_nxt =
2316 cm_node->tcp_cntxt.loc_seq_num;
2317 cm_node->tcp_cntxt.max_snd_wnd =
2318 loopbackremotenode->tcp_cntxt.rcv_wnd;
2319 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2320 cm_node->tcp_cntxt.rcv_wnd;
2321 cm_node->tcp_cntxt.snd_wnd =
2322 loopbackremotenode->tcp_cntxt.rcv_wnd;
2323 loopbackremotenode->tcp_cntxt.snd_wnd =
2324 cm_node->tcp_cntxt.rcv_wnd;
2325 cm_node->tcp_cntxt.snd_wscale =
2326 loopbackremotenode->tcp_cntxt.rcv_wscale;
2327 loopbackremotenode->tcp_cntxt.snd_wscale =
2328 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002329 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002330 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2331 }
2332 return cm_node;
2333 }
2334
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302335 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
2336 cm_node->mpa_frame_size = private_data_len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002337
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302338 memcpy(start_buff, private_data, private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002339
2340 /* send a syn and goto syn sent state */
2341 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002342 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002343
Faisal Latif6492cdf2008-07-24 20:50:45 -07002344 if (ret) {
2345 /* error in sending the syn free up the cm_node struct */
2346 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302347 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2348 cm_node->rem_addr, cm_node->rem_port, cm_node,
2349 cm_node->cm_id);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002350 rem_ref_cm_node(cm_node->cm_core, cm_node);
2351 cm_node = NULL;
2352 }
2353
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302354 if (cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002355 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302356 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2357 cm_node->rem_addr, cm_node->rem_port, cm_node,
2358 cm_node->cm_id);
2359 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002360
2361 return cm_node;
2362}
2363
2364
2365/**
2366 * mini_cm_accept - accept a connection
2367 * This function is never called
2368 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302369static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002370{
2371 return 0;
2372}
2373
2374
2375/**
2376 * mini_cm_reject - reject and teardown a connection
2377 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302378static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002379{
2380 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002381 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002382 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002383 struct nes_cm_event event;
2384 struct iw_cm_id *cm_id = cm_node->cm_id;
2385 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002386
Faisal Latif6492cdf2008-07-24 20:50:45 -07002387 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302388 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002389
Faisal Latif6492cdf2008-07-24 20:50:45 -07002390 if (cm_node->tcp_cntxt.client)
2391 return ret;
2392 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002393
Faisal Latif9d5ab132009-03-06 15:15:01 -08002394 if (!loopback) {
2395 passive_state = atomic_add_return(1, &cm_node->passive_state);
2396 if (passive_state == NES_SEND_RESET_EVENT) {
2397 cm_node->state = NES_CM_STATE_CLOSED;
2398 rem_ref_cm_node(cm_core, cm_node);
2399 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002400 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2401 rem_ref_cm_node(cm_core, cm_node);
2402 } else {
2403 ret = send_mpa_reject(cm_node);
2404 if (ret) {
2405 cm_node->state = NES_CM_STATE_CLOSED;
2406 err = send_reset(cm_node, NULL);
2407 if (err)
2408 WARN_ON(1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302409 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002410 cm_id->add_ref(cm_id);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302411 }
Faisal Latifc5a7d482009-12-09 15:54:08 -08002412 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002413 }
2414 } else {
2415 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002416 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2417 rem_ref_cm_node(cm_core, cm_node);
2418 rem_ref_cm_node(cm_core, loopback);
2419 } else {
2420 event.cm_node = loopback;
2421 event.cm_info.rem_addr = loopback->rem_addr;
2422 event.cm_info.loc_addr = loopback->loc_addr;
2423 event.cm_info.rem_port = loopback->rem_port;
2424 event.cm_info.loc_port = loopback->loc_port;
2425 event.cm_info.cm_id = loopback->cm_id;
2426 cm_event_mpa_reject(&event);
2427 rem_ref_cm_node(cm_core, cm_node);
2428 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002429
Faisal Latifc5a7d482009-12-09 15:54:08 -08002430 cm_id = loopback->cm_id;
2431 rem_ref_cm_node(cm_core, loopback);
2432 cm_id->rem_ref(cm_id);
2433 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002434 }
2435
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002436 return ret;
2437}
2438
2439
2440/**
2441 * mini_cm_close
2442 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002443static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002444{
2445 int ret = 0;
2446
2447 if (!cm_core || !cm_node)
2448 return -EINVAL;
2449
2450 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002451 case NES_CM_STATE_SYN_RCVD:
2452 case NES_CM_STATE_SYN_SENT:
2453 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2454 case NES_CM_STATE_ESTABLISHED:
2455 case NES_CM_STATE_ACCEPTING:
2456 case NES_CM_STATE_MPAREQ_SENT:
2457 case NES_CM_STATE_MPAREQ_RCVD:
2458 cleanup_retrans_entry(cm_node);
2459 send_reset(cm_node, NULL);
2460 break;
2461 case NES_CM_STATE_CLOSE_WAIT:
2462 cm_node->state = NES_CM_STATE_LAST_ACK;
2463 send_fin(cm_node, NULL);
2464 break;
2465 case NES_CM_STATE_FIN_WAIT1:
2466 case NES_CM_STATE_FIN_WAIT2:
2467 case NES_CM_STATE_LAST_ACK:
2468 case NES_CM_STATE_TIME_WAIT:
2469 case NES_CM_STATE_CLOSING:
2470 ret = -1;
2471 break;
2472 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002473 cleanup_retrans_entry(cm_node);
2474 send_reset(cm_node, NULL);
2475 break;
2476 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002477 case NES_CM_STATE_UNKNOWN:
2478 case NES_CM_STATE_INITED:
2479 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002480 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002481 ret = rem_ref_cm_node(cm_core, cm_node);
2482 break;
2483 case NES_CM_STATE_TSA:
2484 if (cm_node->send_entry)
2485 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302486 "send_entry=%p\n", cm_node->send_entry);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002487 ret = rem_ref_cm_node(cm_core, cm_node);
2488 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002489 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002490 return ret;
2491}
2492
2493
2494/**
2495 * recv_pkt - recv an ETHERNET packet, and process it through CM
2496 * node state machine
2497 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002498static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302499 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002500{
2501 struct nes_cm_node *cm_node = NULL;
2502 struct nes_cm_listener *listener = NULL;
2503 struct iphdr *iph;
2504 struct tcphdr *tcph;
2505 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002506 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002507 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002508
Faisal Latif6492cdf2008-07-24 20:50:45 -07002509 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002510 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302511 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002512 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002513
2514 iph = (struct iphdr *)skb->data;
2515 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002516
2517 nfo.loc_addr = ntohl(iph->daddr);
2518 nfo.loc_port = ntohs(tcph->dest);
2519 nfo.rem_addr = ntohl(iph->saddr);
2520 nfo.rem_port = ntohs(tcph->source);
2521
Harvey Harrison03080e52009-01-10 21:45:42 -08002522 tmp_daddr = cpu_to_be32(iph->daddr);
2523 tmp_saddr = cpu_to_be32(iph->saddr);
2524
Harvey Harrison63779432008-10-31 00:56:00 -07002525 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002526 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002527
Faisal Latif6492cdf2008-07-24 20:50:45 -07002528 do {
2529 cm_node = find_node(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302530 nfo.rem_port, nfo.rem_addr,
2531 nfo.loc_port, nfo.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002532
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002533 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002534 /* Only type of packet accepted are for */
2535 /* the PASSIVE open (syn only) */
2536 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002537 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002538 break;
2539 }
2540 listener = find_listener(cm_core, nfo.loc_addr,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302541 nfo.loc_port,
2542 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002543 if (!listener) {
2544 nfo.cm_id = NULL;
2545 nfo.conn_type = 0;
2546 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2547 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002548 break;
2549 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002550 nfo.cm_id = listener->cm_id;
2551 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002552 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302553 listener);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002554 if (!cm_node) {
2555 nes_debug(NES_DBG_CM, "Unable to allocate "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302556 "node\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07002557 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002558 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002559 dev_kfree_skb_any(skb);
2560 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002561 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002562 if (!tcph->rst && !tcph->fin) {
2563 cm_node->state = NES_CM_STATE_LISTENING;
2564 } else {
2565 cm_packets_dropped++;
2566 rem_ref_cm_node(cm_core, cm_node);
2567 dev_kfree_skb_any(skb);
2568 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002569 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002570 add_ref_cm_node(cm_node);
2571 } else if (cm_node->state == NES_CM_STATE_TSA) {
Faisal Latif0f0bee82011-09-25 20:34:00 -05002572 if (cm_node->nesqp->pau_mode)
2573 nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
2574 else {
2575 rem_ref_cm_node(cm_core, cm_node);
2576 atomic_inc(&cm_accel_dropped_pkts);
2577 dev_kfree_skb_any(skb);
2578 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002579 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002580 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002581 skb_reset_network_header(skb);
2582 skb_set_transport_header(skb, sizeof(*tcph));
2583 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002584 process_packet(cm_node, skb, cm_core);
2585 rem_ref_cm_node(cm_core, cm_node);
2586 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002587 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002588}
2589
2590
2591/**
2592 * nes_cm_alloc_core - allocate a top level instance of a cm core
2593 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002594static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002595{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002596 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002597
2598 /* setup the CM core */
2599 /* alloc top level core control structure */
2600 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2601 if (!cm_core)
2602 return NULL;
2603
2604 INIT_LIST_HEAD(&cm_core->connected_nodes);
2605 init_timer(&cm_core->tcp_timer);
2606 cm_core->tcp_timer.function = nes_cm_timer_tick;
2607
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302608 cm_core->mtu = NES_CM_DEFAULT_MTU;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002609 cm_core->state = NES_CM_STATE_INITED;
2610 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2611
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002612 atomic_set(&cm_core->events_posted, 0);
2613
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002614 cm_core->api = &nes_cm_api;
2615
2616 spin_lock_init(&cm_core->ht_lock);
2617 spin_lock_init(&cm_core->listen_list_lock);
2618
2619 INIT_LIST_HEAD(&cm_core->listen_list.list);
2620
2621 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2622
2623 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2624 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2625 cm_core->post_event = nes_cm_post_event;
2626 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2627 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2628
2629 print_core(cm_core);
2630 return cm_core;
2631}
2632
2633
2634/**
2635 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2636 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002637static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002638{
2639 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2640
2641 if (!cm_core)
2642 return -EINVAL;
2643
2644 barrier();
2645
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302646 if (timer_pending(&cm_core->tcp_timer))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002647 del_timer(&cm_core->tcp_timer);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002648
2649 destroy_workqueue(cm_core->event_wq);
2650 destroy_workqueue(cm_core->disconn_wq);
2651 nes_debug(NES_DBG_CM, "\n");
2652 kfree(cm_core);
2653
2654 return 0;
2655}
2656
2657
2658/**
2659 * mini_cm_get
2660 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002661static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002662{
2663 return cm_core->state;
2664}
2665
2666
2667/**
2668 * mini_cm_set
2669 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002670static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002671{
2672 int ret = 0;
2673
2674 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002675 case NES_CM_SET_PKT_SIZE:
2676 cm_core->mtu = value;
2677 break;
2678 case NES_CM_SET_FREE_PKT_Q_SIZE:
2679 cm_core->free_tx_pkt_max = value;
2680 break;
2681 default:
2682 /* unknown set option */
2683 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002684 }
2685
2686 return ret;
2687}
2688
2689
2690/**
2691 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2692 * successfully exchanged when this is called
2693 */
2694static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2695{
2696 int ret = 0;
2697
2698 if (!nesqp)
2699 return -EINVAL;
2700
2701 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302702 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2703 NES_QPCONTEXT_MISC_DROS);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002704
2705 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2706 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2707
2708 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2709
2710 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2711
2712 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302713 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002714
2715 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302716 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2717 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002718
2719 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302720 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2721 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002722
2723 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2724 nesqp->nesqp_context->ts_recent = 0;
2725 nesqp->nesqp_context->ts_age = 0;
2726 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2727 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2728 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2729 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302730 cm_node->tcp_cntxt.rcv_wscale);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002731 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2732 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2733 nesqp->nesqp_context->srtt = 0;
2734 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2735 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302736 nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002737 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2738 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2739 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2740
2741 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302742 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2743 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2744 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2745 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2746 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2747 le32_to_cpu(nesqp->nesqp_context->misc));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002748 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2749 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2750 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2751
2752 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2753 cm_node->state = NES_CM_STATE_TSA;
2754
2755 return ret;
2756}
2757
2758
2759/**
2760 * nes_cm_disconn
2761 */
2762int nes_cm_disconn(struct nes_qp *nesqp)
2763{
Don Wood873fcdd2009-09-05 20:36:37 -07002764 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002765
Don Wood873fcdd2009-09-05 20:36:37 -07002766 work = kzalloc(sizeof *work, GFP_ATOMIC);
2767 if (!work)
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302768 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002769
Don Wood873fcdd2009-09-05 20:36:37 -07002770 nes_add_ref(&nesqp->ibqp);
2771 work->nesqp = nesqp;
2772 INIT_WORK(&work->work, nes_disconnect_worker);
2773 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002774 return 0;
2775}
2776
2777
2778/**
2779 * nes_disconnect_worker
2780 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002781static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002782{
Don Wood873fcdd2009-09-05 20:36:37 -07002783 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2784 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002785
Don Wood873fcdd2009-09-05 20:36:37 -07002786 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002787 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302788 nesqp->last_aeq, nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002789 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002790 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002791}
2792
2793
2794/**
2795 * nes_cm_disconn_true
2796 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002797static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002798{
2799 unsigned long flags;
2800 int ret = 0;
2801 struct iw_cm_id *cm_id;
2802 struct iw_cm_event cm_event;
2803 struct nes_vnic *nesvnic;
2804 u16 last_ae;
2805 u8 original_hw_tcp_state;
2806 u8 original_ibqp_state;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002807 int disconn_status = 0;
Don Woodb29a4fc2009-09-05 20:36:39 -07002808 int issue_disconn = 0;
2809 int issue_close = 0;
2810 int issue_flush = 0;
2811 u32 flush_q = NES_CQP_FLUSH_RQ;
2812 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002813
2814 if (!nesqp) {
2815 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2816 return -1;
2817 }
2818
2819 spin_lock_irqsave(&nesqp->lock, flags);
2820 cm_id = nesqp->cm_id;
2821 /* make sure we havent already closed this connection */
2822 if (!cm_id) {
2823 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302824 nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002825 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002826 return -1;
2827 }
2828
2829 nesvnic = to_nesvnic(nesqp->ibqp.device);
2830 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2831
2832 original_hw_tcp_state = nesqp->hw_tcp_state;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302833 original_ibqp_state = nesqp->ibqp_state;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002834 last_ae = nesqp->last_aeq;
2835
Don Woodb29a4fc2009-09-05 20:36:39 -07002836 if (nesqp->term_flags) {
2837 issue_disconn = 1;
2838 issue_close = 1;
2839 nesqp->cm_id = NULL;
Tatyana Nikolova196f40c2011-12-05 11:39:12 -06002840 del_timer(&nesqp->terminate_timer);
Don Woodb29a4fc2009-09-05 20:36:39 -07002841 if (nesqp->flush_issued == 0) {
2842 nesqp->flush_issued = 1;
2843 issue_flush = 1;
2844 }
2845 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2846 ((original_ibqp_state == IB_QPS_RTS) &&
2847 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2848 issue_disconn = 1;
2849 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002850 disconn_status = -ECONNRESET;
Don Woodb29a4fc2009-09-05 20:36:39 -07002851 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002852
Don Woodb29a4fc2009-09-05 20:36:39 -07002853 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2854 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2855 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2856 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2857 issue_close = 1;
2858 nesqp->cm_id = NULL;
2859 if (nesqp->flush_issued == 0) {
2860 nesqp->flush_issued = 1;
2861 issue_flush = 1;
2862 }
2863 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002864
Don Woodb29a4fc2009-09-05 20:36:39 -07002865 spin_unlock_irqrestore(&nesqp->lock, flags);
2866
2867 if ((issue_flush) && (nesqp->destroyed == 0)) {
2868 /* Flush the queue(s) */
2869 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2870 flush_q |= NES_CQP_FLUSH_SQ;
2871 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2872
2873 if (nesqp->term_flags) {
2874 ibevent.device = nesqp->ibqp.device;
2875 ibevent.event = nesqp->terminate_eventtype;
2876 ibevent.element.qp = &nesqp->ibqp;
2877 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
2878 }
2879 }
2880
2881 if ((cm_id) && (cm_id->event_handler)) {
2882 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002883 atomic_inc(&cm_disconnects);
2884 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002885 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002886 cm_event.local_addr = cm_id->local_addr;
2887 cm_event.remote_addr = cm_id->remote_addr;
2888 cm_event.private_data = NULL;
2889 cm_event.private_data_len = 0;
2890
Faisal Latif6492cdf2008-07-24 20:50:45 -07002891 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302892 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2893 "cm_id = %p, refcount = %u.\n",
2894 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2895 nesqp->hwqp.sq_tail, cm_id,
2896 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002897
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002898 ret = cm_id->event_handler(cm_id, &cm_event);
2899 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002900 nes_debug(NES_DBG_CM, "OFA CM event_handler "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302901 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002902 }
2903
Don Woodb29a4fc2009-09-05 20:36:39 -07002904 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002905 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002906 nes_disconnect(nesqp, 1);
2907
2908 cm_id->provider_data = nesqp;
2909 /* Send up the close complete event */
2910 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002911 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002912 cm_event.provider_data = cm_id->provider_data;
2913 cm_event.local_addr = cm_id->local_addr;
2914 cm_event.remote_addr = cm_id->remote_addr;
2915 cm_event.private_data = NULL;
2916 cm_event.private_data_len = 0;
2917
2918 ret = cm_id->event_handler(cm_id, &cm_event);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302919 if (ret)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002920 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002921
2922 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002923 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002924 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002925
2926 return 0;
2927}
2928
2929
2930/**
2931 * nes_disconnect
2932 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002933static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002934{
2935 int ret = 0;
2936 struct nes_vnic *nesvnic;
2937 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002938 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002939
2940 nesvnic = to_nesvnic(nesqp->ibqp.device);
2941 if (!nesvnic)
2942 return -EINVAL;
2943
2944 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002945 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002946
2947 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00002948 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002949
2950 if (nesqp->active_conn) {
2951
2952 /* indicate this connection is NOT active */
2953 nesqp->active_conn = 0;
2954 } else {
2955 /* Need to free the Last Streaming Mode Message */
2956 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002957 if (nesqp->lsmm_mr)
2958 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002959 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302960 nesqp->private_data_len + nesqp->ietf_frame_size,
2961 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002962 }
2963 }
2964
2965 /* close the CM node down if it is still active */
2966 if (nesqp->cm_node) {
2967 nes_debug(NES_DBG_CM, "Call close API\n");
2968
2969 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002970 }
2971
2972 return ret;
2973}
2974
2975
2976/**
2977 * nes_accept
2978 */
2979int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2980{
2981 u64 u64temp;
2982 struct ib_qp *ibqp;
2983 struct nes_qp *nesqp;
2984 struct nes_vnic *nesvnic;
2985 struct nes_device *nesdev;
2986 struct nes_cm_node *cm_node;
2987 struct nes_adapter *adapter;
2988 struct ib_qp_attr attr;
2989 struct iw_cm_event cm_event;
2990 struct nes_hw_qp_wqe *wqe;
2991 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002992 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002993 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002994 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002995 struct nes_ib_device *nesibdev;
2996 struct ib_mr *ibmr = NULL;
2997 struct ib_phys_buf ibphysbuf;
2998 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002999 u64 tagged_offset;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303000 u8 mpa_frame_offset = 0;
3001 struct ietf_mpa_v2 *mpa_v2_frame;
3002 u8 start_addr = 0;
3003 u8 *start_ptr = &start_addr;
3004 u8 **start_buff = &start_ptr;
3005 u16 buff_len = 0;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003006
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003007 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3008 if (!ibqp)
3009 return -EINVAL;
3010
3011 /* get all our handles */
3012 nesqp = to_nesqp(ibqp);
3013 nesvnic = to_nesvnic(nesqp->ibqp.device);
3014 nesdev = nesvnic->nesdev;
3015 adapter = nesdev->nesadapter;
3016
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003017 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003018 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
3019 "%s\n", cm_node, nesvnic, nesvnic->netdev,
3020 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003021
Faisal Latifc5a7d482009-12-09 15:54:08 -08003022 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
3023 if (cm_node->loopbackpartner)
3024 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
3025 rem_ref_cm_node(cm_node->cm_core, cm_node);
3026 return -EINVAL;
3027 }
3028
Faisal Latifdae58722010-08-14 21:04:56 +00003029 passive_state = atomic_add_return(1, &cm_node->passive_state);
3030 if (passive_state == NES_SEND_RESET_EVENT) {
3031 rem_ref_cm_node(cm_node->cm_core, cm_node);
3032 return -ECONNRESET;
3033 }
3034
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003035 /* associate the node with the QP */
3036 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003037 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003038
Faisal Latif6492cdf2008-07-24 20:50:45 -07003039 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
3040 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003041 atomic_inc(&cm_accepts);
3042
3043 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00003044 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003045
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303046 nesqp->ietf_frame_size = sizeof(struct ietf_mpa_v2);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003047 /* allocate the ietf frame and space for private data */
3048 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303049 nesqp->ietf_frame_size + conn_param->private_data_len,
3050 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003051
Faisal Latif6492cdf2008-07-24 20:50:45 -07003052 if (!nesqp->ietf_frame) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303053 nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07003054 return -ENOMEM;
3055 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303056 mpa_v2_frame = (struct ietf_mpa_v2 *)nesqp->ietf_frame;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003057
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303058 if (cm_node->mpa_frame_rev == IETF_MPA_V1)
3059 mpa_frame_offset = 4;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003060
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303061 memcpy(mpa_v2_frame->priv_data, conn_param->private_data,
3062 conn_param->private_data_len);
3063
3064 cm_build_mpa_frame(cm_node, start_buff, &buff_len, nesqp->ietf_frame, MPA_KEY_REPLY);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003065 nesqp->private_data_len = conn_param->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003066
Faisal Latif6492cdf2008-07-24 20:50:45 -07003067 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
3068 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003069
Faisal Latif6492cdf2008-07-24 20:50:45 -07003070 if (cm_id->remote_addr.sin_addr.s_addr !=
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303071 cm_id->local_addr.sin_addr.s_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003072 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003073 nesibdev = nesvnic->nesibdev;
3074 nespd = nesqp->nespd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303075 ibphysbuf.addr = nesqp->ietf_frame_pbase + mpa_frame_offset;
3076 ibphysbuf.size = buff_len;
3077 tagged_offset = (u64)(unsigned long)*start_buff;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003078 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303079 &ibphysbuf, 1,
3080 IB_ACCESS_LOCAL_WRITE,
3081 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003082 if (!ibmr) {
3083 nes_debug(NES_DBG_CM, "Unable to register memory region"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303084 "for lSMM for cm_node = %p \n",
3085 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07003086 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303087 nesqp->private_data_len + nesqp->ietf_frame_size,
3088 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003089 return -ENOMEM;
3090 }
3091
3092 ibmr->pd = &nespd->ibpd;
3093 ibmr->device = nespd->ibpd.device;
3094 nesqp->lsmm_mr = ibmr;
3095
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303096 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003097 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303098 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
3099 u64temp);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003100 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3101 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303102 NES_IWARP_SQ_WQE_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003103 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303104 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003105 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303106 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3107 (u64)(unsigned long)(*start_buff));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003108 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303109 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003110 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08003111 if (nesqp->sq_kmapped) {
3112 nesqp->sq_kmapped = 0;
3113 kunmap(nesqp->page);
3114 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003115
Faisal Latif6492cdf2008-07-24 20:50:45 -07003116 nesqp->nesqp_context->ird_ord_sizes |=
3117 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303118 NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003119 } else {
3120 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08003121 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003122 }
3123 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003124
3125
3126 /* Cache the cm_id in the qp */
3127 nesqp->cm_id = cm_id;
3128 cm_node->cm_id = cm_id;
3129
3130 /* nesqp->cm_node = (void *)cm_id->provider_data; */
3131 cm_id->provider_data = nesqp;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303132 nesqp->active_conn = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003133
Faisal Latif6492cdf2008-07-24 20:50:45 -07003134 if (cm_node->state == NES_CM_STATE_TSA)
3135 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303136 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003137
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003138 nes_cm_init_tsa_conn(nesqp, cm_node);
3139
Faisal Latif6492cdf2008-07-24 20:50:45 -07003140 nesqp->nesqp_context->tcpPorts[0] =
3141 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3142 nesqp->nesqp_context->tcpPorts[1] =
3143 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3144
3145 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3146 nesqp->nesqp_context->ip0 =
3147 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3148 else
3149 nesqp->nesqp_context->ip0 =
3150 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003151
3152 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303153 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3154 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003155
Faisal Latif6492cdf2008-07-24 20:50:45 -07003156 nesqp->nesqp_context->arp_index_vlan |=
3157 cpu_to_le32(nes_arp_table(nesdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303158 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
3159 NES_ARP_RESOLVE) << 16);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003160
3161 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003162 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003163
3164 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3165
3166 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003167 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
3168 nesqp->nesqp_context->ird_ord_sizes |=
3169 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003170
3171 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003172 nes_quad.DstIpAdrIndex =
3173 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3174 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3175 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3176 else
3177 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
3178 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3179 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003180
3181 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003182 crc_value = get_crc_value(&nes_quad);
3183 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003184 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303185 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003186
3187 nesqp->hte_index &= adapter->hte_index_mask;
3188 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3189
3190 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3191
Faisal Latif6492cdf2008-07-24 20:50:45 -07003192 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303193 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
3194 "private data length=%u.\n", nesqp->hwqp.qp_id,
3195 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3196 ntohs(cm_id->remote_addr.sin_port),
3197 ntohl(cm_id->local_addr.sin_addr.s_addr),
3198 ntohs(cm_id->local_addr.sin_port),
3199 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
3200 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
3201 buff_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003202
Coly Li73ac36e2009-01-07 18:09:16 -08003203 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003204 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07003205 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003206
3207 cm_event.event = IW_CM_EVENT_ESTABLISHED;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003208 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003209 cm_event.provider_data = (void *)nesqp;
3210 cm_event.local_addr = cm_id->local_addr;
3211 cm_event.remote_addr = cm_id->remote_addr;
3212 cm_event.private_data = NULL;
3213 cm_event.private_data_len = 0;
3214 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003215 attr.qp_state = IB_QPS_RTS;
3216 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003217 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003218 cm_node->loopbackpartner->mpa_frame_size =
3219 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003220 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003221 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303222 conn_param->private_data, conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003223 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
3224 }
3225 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003226 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303227 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003228
3229 return 0;
3230}
3231
3232
3233/**
3234 * nes_reject
3235 */
3236int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3237{
3238 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003239 struct nes_cm_node *loopback;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003240 struct nes_cm_core *cm_core;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303241 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003242
3243 atomic_inc(&cm_rejects);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303244 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003245 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003246 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003247 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003248
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303249 if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003250 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003251
Faisal Latif9d5ab132009-03-06 15:15:01 -08003252 if (loopback) {
3253 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3254 loopback->mpa_frame.priv_data_len = pdata_len;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303255 loopback->mpa_frame_size = pdata_len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003256 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303257 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
3258 cm_node->mpa_frame_size = pdata_len;
3259 memcpy(start_buff, pdata, pdata_len);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003260 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303261 return cm_core->api->reject(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003262}
3263
3264
3265/**
3266 * nes_connect
3267 * setup and launch cm connect node
3268 */
3269int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3270{
3271 struct ib_qp *ibqp;
3272 struct nes_qp *nesqp;
3273 struct nes_vnic *nesvnic;
3274 struct nes_device *nesdev;
3275 struct nes_cm_node *cm_node;
3276 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003277 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003278
3279 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3280 if (!ibqp)
3281 return -EINVAL;
3282 nesqp = to_nesqp(ibqp);
3283 if (!nesqp)
3284 return -EINVAL;
3285 nesvnic = to_nesvnic(nesqp->ibqp.device);
3286 if (!nesvnic)
3287 return -EINVAL;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303288 nesdev = nesvnic->nesdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003289 if (!nesdev)
3290 return -EINVAL;
3291
Faisal Latifc5a7d482009-12-09 15:54:08 -08003292 if (!(cm_id->local_addr.sin_port) || !(cm_id->remote_addr.sin_port))
3293 return -EINVAL;
3294
Faisal Latif6492cdf2008-07-24 20:50:45 -07003295 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303296 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3297 ntohl(nesvnic->local_ipaddr),
3298 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3299 ntohs(cm_id->remote_addr.sin_port),
3300 ntohl(cm_id->local_addr.sin_addr.s_addr),
3301 ntohs(cm_id->local_addr.sin_port));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003302
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003303 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003304 nesqp->active_conn = 1;
3305
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003306 /* cache the cm_id in the qp */
3307 nesqp->cm_id = cm_id;
3308
3309 cm_id->provider_data = nesqp;
3310
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003311 nesqp->private_data_len = conn_param->private_data_len;
3312 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
3313 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003314 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303315 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003316
Faisal Latif6492cdf2008-07-24 20:50:45 -07003317 if (cm_id->local_addr.sin_addr.s_addr !=
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303318 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003319 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303320 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003321 apbvt_set = 1;
3322 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003323
3324 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003325 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3326 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3327 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3328 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003329 cm_info.cm_id = cm_id;
3330 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3331
3332 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003333
3334 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003335 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303336 conn_param->private_data_len, (void *)conn_param->private_data,
3337 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003338 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003339 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003340 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303341 PCI_FUNC(nesdev->pcidev->devfn),
3342 NES_MANAGE_APBVT_DEL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003343
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003344 cm_id->rem_ref(cm_id);
3345 return -ENOMEM;
3346 }
3347
Faisal Latif53094c32009-04-27 13:37:34 -07003348 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003349 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003350 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003351 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003352
3353 return 0;
3354}
3355
3356
3357/**
3358 * nes_create_listen
3359 */
3360int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3361{
3362 struct nes_vnic *nesvnic;
3363 struct nes_cm_listener *cm_node;
3364 struct nes_cm_info cm_info;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003365 int err;
3366
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003367 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3368 cm_id, ntohs(cm_id->local_addr.sin_port));
3369
3370 nesvnic = to_nesvnic(cm_id->device);
3371 if (!nesvnic)
3372 return -EINVAL;
Roland Dreier69d51022010-08-04 14:25:40 -07003373
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003374 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3375 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3376
3377 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3378 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3379
3380 /* setup listen params in our api call struct */
3381 cm_info.loc_addr = nesvnic->local_ipaddr;
3382 cm_info.loc_port = cm_id->local_addr.sin_port;
3383 cm_info.backlog = backlog;
3384 cm_info.cm_id = cm_id;
3385
3386 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3387
3388
3389 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3390 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003391 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303392 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003393 return -ENOMEM;
3394 }
3395
3396 cm_id->provider_data = cm_node;
3397
3398 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003399 err = nes_manage_apbvt(nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303400 ntohs(cm_id->local_addr.sin_port),
3401 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3402 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003403 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003404 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303405 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003406 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3407 return err;
3408 }
Faisal Latif6e10d2e2010-02-12 19:55:03 +00003409 atomic_inc(&cm_listens_created);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003410 }
3411
3412 cm_id->add_ref(cm_id);
3413 cm_id->provider_data = (void *)cm_node;
3414
3415
3416 return 0;
3417}
3418
3419
3420/**
3421 * nes_destroy_listen
3422 */
3423int nes_destroy_listen(struct iw_cm_id *cm_id)
3424{
3425 if (cm_id->provider_data)
3426 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3427 else
3428 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3429
3430 cm_id->rem_ref(cm_id);
3431
3432 return 0;
3433}
3434
3435
3436/**
3437 * nes_cm_recv
3438 */
3439int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3440{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003441 int rc = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303442
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003443 cm_packets_received++;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303444 if ((g_cm_core) && (g_cm_core->api))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003445 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303446 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003447 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303448 " cm is not setup properly.\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003449
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003450 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003451}
3452
3453
3454/**
3455 * nes_cm_start
3456 * Start and init a cm core module
3457 */
3458int nes_cm_start(void)
3459{
3460 nes_debug(NES_DBG_CM, "\n");
3461 /* create the primary CM core, pass this handle to subsequent core inits */
3462 g_cm_core = nes_cm_alloc_core();
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303463 if (g_cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003464 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303465 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003466 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003467}
3468
3469
3470/**
3471 * nes_cm_stop
3472 * stop and dealloc all cm core instances
3473 */
3474int nes_cm_stop(void)
3475{
3476 g_cm_core->api->destroy_cm_core(g_cm_core);
3477 return 0;
3478}
3479
3480
3481/**
3482 * cm_event_connected
3483 * handle a connected event, setup QPs and HW
3484 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003485static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003486{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003487 struct nes_qp *nesqp;
3488 struct nes_vnic *nesvnic;
3489 struct nes_device *nesdev;
3490 struct nes_cm_node *cm_node;
3491 struct nes_adapter *nesadapter;
3492 struct ib_qp_attr attr;
3493 struct iw_cm_id *cm_id;
3494 struct iw_cm_event cm_event;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003495 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003496 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003497 int ret;
3498
3499 /* get all our handles */
3500 cm_node = event->cm_node;
3501 cm_id = cm_node->cm_id;
3502 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3503 nesqp = (struct nes_qp *)cm_id->provider_data;
3504 nesvnic = to_nesvnic(nesqp->ibqp.device);
3505 nesdev = nesvnic->nesdev;
3506 nesadapter = nesdev->nesadapter;
3507
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303508 if (nesqp->destroyed)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003509 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003510 atomic_inc(&cm_connecteds);
3511 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303512 " local port 0x%04X. jiffies = %lu.\n",
3513 nesqp->hwqp.qp_id,
3514 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3515 ntohs(cm_id->remote_addr.sin_port),
3516 ntohs(cm_id->local_addr.sin_port),
3517 jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003518
3519 nes_cm_init_tsa_conn(nesqp, cm_node);
3520
3521 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003522 nesqp->nesqp_context->tcpPorts[0] =
3523 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3524 nesqp->nesqp_context->tcpPorts[1] =
3525 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3526 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3527 nesqp->nesqp_context->ip0 =
3528 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3529 else
3530 nesqp->nesqp_context->ip0 =
3531 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003532
3533 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003534 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3535 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003536 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003537 nes_arp_table(nesdev,
3538 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003539 NULL, NES_ARP_RESOLVE) << 16);
3540 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3541 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3542 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3543 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003544 cpu_to_le32((u32)1 <<
3545 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003546
3547 /* Adjust tail for not having a LSMM */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303548 /*nesqp->hwqp.sq_tail = 1;*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003549
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303550 build_rdma0_msg(cm_node, &nesqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003551
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303552 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3553 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003554
3555 memset(&nes_quad, 0, sizeof(nes_quad));
3556
Faisal Latif6492cdf2008-07-24 20:50:45 -07003557 nes_quad.DstIpAdrIndex =
3558 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3559 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3560 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3561 else
3562 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003563 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3564 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3565
3566 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003567 crc_value = get_crc_value(&nes_quad);
3568 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003569 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303570 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003571
3572 nesqp->hte_index &= nesadapter->hte_index_mask;
3573 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3574
3575 nesqp->ietf_frame = &cm_node->mpa_frame;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303576 nesqp->private_data_len = (u8)cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003577 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3578
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003579 /* notify OF layer we successfully created the requested connection */
3580 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003581 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003582 cm_event.provider_data = cm_id->provider_data;
3583 cm_event.local_addr.sin_family = AF_INET;
3584 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3585 cm_event.remote_addr = cm_id->remote_addr;
3586
Faisal Latif6492cdf2008-07-24 20:50:45 -07003587 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303588 cm_event.private_data_len = (u8)event->cm_node->mpa_frame_size;
3589 cm_event.ird = cm_node->ird_size;
3590 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003591
3592 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3593 ret = cm_id->event_handler(cm_id, &cm_event);
3594 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3595
3596 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003597 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303598 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003599 attr.qp_state = IB_QPS_RTS;
3600 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003601
Faisal Latif6492cdf2008-07-24 20:50:45 -07003602 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303603 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003604
3605 return;
3606}
3607
3608
3609/**
3610 * cm_event_connect_error
3611 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003612static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003613{
3614 struct nes_qp *nesqp;
3615 struct iw_cm_id *cm_id;
3616 struct iw_cm_event cm_event;
3617 /* struct nes_cm_info cm_info; */
3618 int ret;
3619
3620 if (!event->cm_node)
3621 return;
3622
3623 cm_id = event->cm_node->cm_id;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303624 if (!cm_id)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003625 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003626
3627 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3628 nesqp = cm_id->provider_data;
3629
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303630 if (!nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003631 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003632
3633 /* notify OF layer about this connection error event */
3634 /* cm_id->rem_ref(cm_id); */
3635 nesqp->cm_id = NULL;
3636 cm_id->provider_data = NULL;
3637 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003638 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003639 cm_event.provider_data = cm_id->provider_data;
3640 cm_event.local_addr = cm_id->local_addr;
3641 cm_event.remote_addr = cm_id->remote_addr;
3642 cm_event.private_data = NULL;
3643 cm_event.private_data_len = 0;
3644
Faisal Latif6492cdf2008-07-24 20:50:45 -07003645 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303646 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3647 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003648
3649 ret = cm_id->event_handler(cm_id, &cm_event);
3650 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3651 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003652 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303653 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003654 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003655
Faisal Latif6492cdf2008-07-24 20:50:45 -07003656 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003657 return;
3658}
3659
3660
3661/**
3662 * cm_event_reset
3663 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003664static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003665{
3666 struct nes_qp *nesqp;
3667 struct iw_cm_id *cm_id;
3668 struct iw_cm_event cm_event;
3669 /* struct nes_cm_info cm_info; */
3670 int ret;
3671
3672 if (!event->cm_node)
3673 return;
3674
3675 if (!event->cm_node->cm_id)
3676 return;
3677
3678 cm_id = event->cm_node->cm_id;
3679
3680 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3681 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003682 if (!nesqp)
3683 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003684
3685 nesqp->cm_id = NULL;
3686 /* cm_id->provider_data = NULL; */
3687 cm_event.event = IW_CM_EVENT_DISCONNECT;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003688 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003689 cm_event.provider_data = cm_id->provider_data;
3690 cm_event.local_addr = cm_id->local_addr;
3691 cm_event.remote_addr = cm_id->remote_addr;
3692 cm_event.private_data = NULL;
3693 cm_event.private_data_len = 0;
3694
Faisal Latif183ecfa2008-11-21 20:50:46 -06003695 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003696 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003697 atomic_inc(&cm_closes);
3698 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003699 cm_event.status = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003700 cm_event.provider_data = cm_id->provider_data;
3701 cm_event.local_addr = cm_id->local_addr;
3702 cm_event.remote_addr = cm_id->remote_addr;
3703 cm_event.private_data = NULL;
3704 cm_event.private_data_len = 0;
3705 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3706 ret = cm_id->event_handler(cm_id, &cm_event);
3707
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003708 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3709
3710
3711 /* notify OF layer about this connection error event */
3712 cm_id->rem_ref(cm_id);
3713
3714 return;
3715}
3716
3717
3718/**
3719 * cm_event_mpa_req
3720 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003721static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003722{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303723 struct iw_cm_id *cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003724 struct iw_cm_event cm_event;
3725 int ret;
3726 struct nes_cm_node *cm_node;
3727
3728 cm_node = event->cm_node;
3729 if (!cm_node)
3730 return;
3731 cm_id = cm_node->cm_id;
3732
3733 atomic_inc(&cm_connect_reqs);
3734 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303735 cm_node, cm_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003736
3737 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003738 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003739 cm_event.provider_data = (void *)cm_node;
3740
3741 cm_event.local_addr.sin_family = AF_INET;
3742 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3743 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3744
3745 cm_event.remote_addr.sin_family = AF_INET;
3746 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3747 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003748 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303749 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
3750 cm_event.ird = cm_node->ird_size;
3751 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003752
3753 ret = cm_id->event_handler(cm_id, &cm_event);
3754 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003755 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303756 __func__, __LINE__, ret);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003757 return;
3758}
3759
3760
3761static void cm_event_mpa_reject(struct nes_cm_event *event)
3762{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303763 struct iw_cm_id *cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003764 struct iw_cm_event cm_event;
3765 struct nes_cm_node *cm_node;
3766 int ret;
3767
3768 cm_node = event->cm_node;
3769 if (!cm_node)
3770 return;
3771 cm_id = cm_node->cm_id;
3772
3773 atomic_inc(&cm_connect_reqs);
3774 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303775 cm_node, cm_id, jiffies);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003776
3777 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3778 cm_event.status = -ECONNREFUSED;
3779 cm_event.provider_data = cm_id->provider_data;
3780
3781 cm_event.local_addr.sin_family = AF_INET;
3782 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3783 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3784
3785 cm_event.remote_addr.sin_family = AF_INET;
3786 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3787 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3788
3789 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303790 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003791
3792 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303793 "remove_addr=%08x\n",
3794 cm_event.local_addr.sin_addr.s_addr,
3795 cm_event.remote_addr.sin_addr.s_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003796
3797 ret = cm_id->event_handler(cm_id, &cm_event);
3798 if (ret)
3799 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303800 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003801
3802 return;
3803}
3804
3805
3806static void nes_cm_event_handler(struct work_struct *);
3807
3808/**
3809 * nes_cm_post_event
3810 * post an event to the cm event handler
3811 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003812static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003813{
3814 atomic_inc(&event->cm_node->cm_core->events_posted);
3815 add_ref_cm_node(event->cm_node);
3816 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3817 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003818 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303819 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003820
3821 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3822
3823 nes_debug(NES_DBG_CM, "Exit\n");
3824 return 0;
3825}
3826
3827
3828/**
3829 * nes_cm_event_handler
3830 * worker function to handle cm events
3831 * will free instance of nes_cm_event
3832 */
3833static void nes_cm_event_handler(struct work_struct *work)
3834{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003835 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303836 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003837 struct nes_cm_core *cm_core;
3838
Faisal Latif6492cdf2008-07-24 20:50:45 -07003839 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003840 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003841
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003842 cm_core = event->cm_node->cm_core;
3843 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303844 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003845
3846 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003847 case NES_CM_EVENT_MPA_REQ:
3848 cm_event_mpa_req(event);
3849 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303850 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003851 break;
3852 case NES_CM_EVENT_RESET:
3853 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303854 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003855 cm_event_reset(event);
3856 break;
3857 case NES_CM_EVENT_CONNECTED:
3858 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303859 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003860 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003861 cm_event_connected(event);
3862 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3863 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003864 case NES_CM_EVENT_MPA_REJECT:
3865 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303866 (event->cm_node->state == NES_CM_STATE_TSA))
Faisal Latif9d5ab132009-03-06 15:15:01 -08003867 break;
3868 cm_event_mpa_reject(event);
3869 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3870 break;
3871
Faisal Latif6492cdf2008-07-24 20:50:45 -07003872 case NES_CM_EVENT_ABORTED:
3873 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303874 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003875 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003876 cm_event_connect_error(event);
3877 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3878 break;
3879 case NES_CM_EVENT_DROPPED_PKT:
3880 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3881 break;
3882 default:
3883 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3884 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003885 }
3886
3887 atomic_dec(&cm_core->events_posted);
3888 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3889 rem_ref_cm_node(cm_core, event->cm_node);
3890 kfree(event);
3891
3892 return;
3893}