blob: fc5192ee928b68395c8795d0b3c582f26acaf994 [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;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800236
237 skb = dev_alloc_skb(MAX_CM_BUFFER);
238 if (!skb) {
239 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
240 return -ENOMEM;
241 }
242
243 /* send an MPA reject frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530244 cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REPLY);
245 form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK | SET_FIN);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800246
247 cm_node->state = NES_CM_STATE_FIN_WAIT1;
248 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800249}
250
251
252/**
253 * recv_mpa - process a received TCP pkt, we are expecting an
254 * IETF MPA frame
255 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800256static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530257 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800258{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530259 struct ietf_mpa_v1 *mpa_frame;
260 struct ietf_mpa_v2 *mpa_v2_frame;
261 struct ietf_rtr_msg *rtr_msg;
262 int mpa_hdr_len;
263 int priv_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800264
Faisal Latif9d5ab132009-03-06 15:15:01 -0800265 *type = NES_MPA_REQUEST_ACCEPT;
266
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800267 /* assume req frame is in tcp data payload */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530268 if (len < sizeof(struct ietf_mpa_v1)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800269 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800270 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800271 }
272
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530273 /* points to the beginning of the frame, which could be MPA V1 or V2 */
274 mpa_frame = (struct ietf_mpa_v1 *)buffer;
275 mpa_hdr_len = sizeof(struct ietf_mpa_v1);
276 priv_data_len = ntohs(mpa_frame->priv_data_len);
277
Faisal Latif1cf078c2009-12-09 15:53:54 -0800278 /* make sure mpa private data len is less than 512 bytes */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530279 if (priv_data_len > IETF_MAX_PRIV_DATA_LEN) {
Faisal Latif1cf078c2009-12-09 15:53:54 -0800280 nes_debug(NES_DBG_CM, "The received Length of Private"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530281 " Data field exceeds 512 octets\n");
Faisal Latif1cf078c2009-12-09 15:53:54 -0800282 return -EINVAL;
283 }
284 /*
285 * make sure MPA receiver interoperate with the
286 * received MPA version and MPA key information
287 *
288 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530289 if (mpa_frame->rev != IETF_MPA_V1 && mpa_frame->rev != IETF_MPA_V2) {
Faisal Latif1cf078c2009-12-09 15:53:54 -0800290 nes_debug(NES_DBG_CM, "The received mpa version"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530291 " is not supported\n");
Faisal Latif1cf078c2009-12-09 15:53:54 -0800292 return -EINVAL;
293 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530294 /*
295 * backwards compatibility only
296 */
297 if (mpa_frame->rev > cm_node->mpa_frame_rev) {
298 nes_debug(NES_DBG_CM, "The received mpa version"
299 " can not be interoperated\n");
300 return -EINVAL;
301 } else {
302 cm_node->mpa_frame_rev = mpa_frame->rev;
303 }
304
Faisal Latif1cf078c2009-12-09 15:53:54 -0800305 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
306 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
307 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
308 return -EINVAL;
309 }
310 } else {
311 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
312 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
313 return -EINVAL;
314 }
315 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800316
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530317
318 if (priv_data_len + mpa_hdr_len != len) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800319 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530320 " complete (%x + %x != %x)\n",
321 priv_data_len, mpa_hdr_len, len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800322 return -EINVAL;
323 }
324 /* make sure it does not exceed the max size */
325 if (len > MAX_CM_BUFFER) {
326 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530327 " (%x + %x != %x)\n",
328 priv_data_len, mpa_hdr_len, len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800329 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800330 }
331
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530332 cm_node->mpa_frame_size = priv_data_len;
333
334 switch (mpa_frame->rev) {
335 case IETF_MPA_V2: {
336 u16 ird_size;
337 u16 ord_size;
338 mpa_v2_frame = (struct ietf_mpa_v2 *)buffer;
339 mpa_hdr_len += IETF_RTR_MSG_SIZE;
340 cm_node->mpa_frame_size -= IETF_RTR_MSG_SIZE;
341 rtr_msg = &mpa_v2_frame->rtr_msg;
342
343 /* parse rtr message */
344 rtr_msg->ctrl_ird = ntohs(rtr_msg->ctrl_ird);
345 rtr_msg->ctrl_ord = ntohs(rtr_msg->ctrl_ord);
346 ird_size = rtr_msg->ctrl_ird & IETF_NO_IRD_ORD;
347 ord_size = rtr_msg->ctrl_ord & IETF_NO_IRD_ORD;
348
349 if (!(rtr_msg->ctrl_ird & IETF_PEER_TO_PEER)) {
350 /* send reset */
351 return -EINVAL;
352 }
353
354 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
355 /* responder */
356 if (cm_node->ord_size > ird_size)
357 cm_node->ord_size = ird_size;
358 } else {
359 /* initiator */
360 if (cm_node->ord_size > ird_size)
361 cm_node->ord_size = ird_size;
362
363 if (cm_node->ird_size < ord_size) {
364 /* no resources available */
365 /* send terminate message */
366 return -EINVAL;
367 }
368 }
369
370 if (rtr_msg->ctrl_ord & IETF_RDMA0_READ) {
371 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
372 } else if (rtr_msg->ctrl_ord & IETF_RDMA0_WRITE) {
373 cm_node->send_rdma0_op = SEND_RDMA_WRITE_ZERO;
374 } else { /* Not supported RDMA0 operation */
375 return -EINVAL;
376 }
377 break;
378 }
379 case IETF_MPA_V1:
380 default:
381 break;
382 }
383
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800384 /* copy entire MPA frame to our cm_node's frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530385 memcpy(cm_node->mpa_frame_buf, buffer + mpa_hdr_len, cm_node->mpa_frame_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800386
Faisal Latif9d5ab132009-03-06 15:15:01 -0800387 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
388 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800389 return 0;
390}
391
392
393/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800394 * form_cm_frame - get a free packet and build empty frame Use
395 * node info to build.
396 */
Chien Tung6098d102008-11-21 20:51:01 -0600397static void form_cm_frame(struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530398 struct nes_cm_node *cm_node, void *options, u32 optionsize,
399 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800400{
401 struct tcphdr *tcph;
402 struct iphdr *iph;
403 struct ethhdr *ethh;
404 u8 *buf;
405 u16 packetsize = sizeof(*iph);
406
407 packetsize += sizeof(*tcph);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530408 packetsize += optionsize + datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800409
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530410 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800411 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
412
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800413 buf = skb_put(skb, packetsize + ETH_HLEN);
414
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530415 ethh = (struct ethhdr *)buf;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800416 buf += ETH_HLEN;
417
418 iph = (struct iphdr *)buf;
419 buf += sizeof(*iph);
420 tcph = (struct tcphdr *)buf;
421 skb_reset_mac_header(skb);
422 skb_set_network_header(skb, ETH_HLEN);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530423 skb_set_transport_header(skb, ETH_HLEN + sizeof(*iph));
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800424 buf += sizeof(*tcph);
425
426 skb->ip_summed = CHECKSUM_PARTIAL;
427 skb->protocol = htons(0x800);
428 skb->data_len = 0;
429 skb->mac_len = ETH_HLEN;
430
431 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
432 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
433 ethh->h_proto = htons(0x0800);
434
435 iph->version = IPVERSION;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530436 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800437 iph->tos = 0;
438 iph->tot_len = htons(packetsize);
439 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
440
441 iph->frag_off = htons(0x4000);
442 iph->ttl = 0x40;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530443 iph->protocol = 0x06; /* IPPROTO_TCP */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800444
445 iph->saddr = htonl(cm_node->loc_addr);
446 iph->daddr = htonl(cm_node->rem_addr);
447
448 tcph->source = htons(cm_node->loc_port);
449 tcph->dest = htons(cm_node->rem_port);
450 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
451
452 if (flags & SET_ACK) {
453 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
454 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
455 tcph->ack = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530456 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800457 tcph->ack_seq = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530458 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800459
460 if (flags & SET_SYN) {
461 cm_node->tcp_cntxt.loc_seq_num++;
462 tcph->syn = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530463 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700464 cm_node->tcp_cntxt.loc_seq_num += datasize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530465 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800466
Faisal Latif6492cdf2008-07-24 20:50:45 -0700467 if (flags & SET_FIN) {
468 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800469 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700470 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800471
472 if (flags & SET_RST)
473 tcph->rst = 1;
474
475 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
476 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
477 tcph->urg_ptr = 0;
478 if (optionsize)
479 memcpy(buf, options, optionsize);
480 buf += optionsize;
481 if (datasize)
482 memcpy(buf, data, datasize);
483
484 skb_shinfo(skb)->nr_frags = 0;
485 cm_packets_created++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800486}
487
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800488/**
489 * print_core - dump a cm core
490 */
491static void print_core(struct nes_cm_core *core)
492{
493 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
494 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
495 if (!core)
496 return;
497 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800498
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530499 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800500
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800501 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
502 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
503
504 nes_debug(NES_DBG_CM, "core : %p \n", core);
505
506 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
507}
508
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530509/**
510 * cm_build_mpa_frame - build a MPA V1 frame or MPA V2 frame
511 */
512static int cm_build_mpa_frame(struct nes_cm_node *cm_node, u8 **start_buff,
513 u16 *buff_len, u8 *pci_mem, u8 mpa_key)
514{
515 int ret = 0;
516
517 *start_buff = (pci_mem) ? pci_mem : &cm_node->mpa_frame_buf[0];
518
519 switch (cm_node->mpa_frame_rev) {
520 case IETF_MPA_V1:
521 *start_buff = (u8 *)*start_buff + sizeof(struct ietf_rtr_msg);
522 *buff_len = sizeof(struct ietf_mpa_v1) + cm_node->mpa_frame_size;
523 build_mpa_v1(cm_node, *start_buff, mpa_key);
524 break;
525 case IETF_MPA_V2:
526 *buff_len = sizeof(struct ietf_mpa_v2) + cm_node->mpa_frame_size;
527 build_mpa_v2(cm_node, *start_buff, mpa_key);
528 break;
529 default:
530 ret = -EINVAL;
531 }
532 return ret;
533}
534
535/**
536 * build_mpa_v2 - build a MPA V2 frame
537 */
538static void build_mpa_v2(struct nes_cm_node *cm_node,
539 void *start_addr, u8 mpa_key)
540{
541 struct ietf_mpa_v2 *mpa_frame = (struct ietf_mpa_v2 *)start_addr;
542 struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
543
544 /* initialize the upper 5 bytes of the frame */
545 build_mpa_v1(cm_node, start_addr, mpa_key);
546 mpa_frame->flags |= IETF_MPA_V2_FLAG; /* set a bit to indicate MPA V2 */
547 mpa_frame->priv_data_len += htons(IETF_RTR_MSG_SIZE);
548
549 /* initialize RTR msg */
550 rtr_msg->ctrl_ird = (cm_node->ird_size > IETF_NO_IRD_ORD) ?
551 IETF_NO_IRD_ORD : cm_node->ird_size;
552 rtr_msg->ctrl_ord = (cm_node->ord_size > IETF_NO_IRD_ORD) ?
553 IETF_NO_IRD_ORD : cm_node->ord_size;
554
555 rtr_msg->ctrl_ird |= IETF_PEER_TO_PEER;
556 rtr_msg->ctrl_ird |= IETF_FLPDU_ZERO_LEN;
557
558 switch (mpa_key) {
559 case MPA_KEY_REQUEST:
560 rtr_msg->ctrl_ord |= IETF_RDMA0_WRITE;
561 rtr_msg->ctrl_ord |= IETF_RDMA0_READ;
562 break;
563 case MPA_KEY_REPLY:
564 switch (cm_node->send_rdma0_op) {
565 case SEND_RDMA_WRITE_ZERO:
566 rtr_msg->ctrl_ord |= IETF_RDMA0_WRITE;
567 break;
568 case SEND_RDMA_READ_ZERO:
569 rtr_msg->ctrl_ord |= IETF_RDMA0_READ;
570 break;
571 }
572 }
573 rtr_msg->ctrl_ird = htons(rtr_msg->ctrl_ird);
574 rtr_msg->ctrl_ord = htons(rtr_msg->ctrl_ord);
575}
576
577/**
578 * build_mpa_v1 - build a MPA V1 frame
579 */
580static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
581{
582 struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
583
584 switch (mpa_key) {
585 case MPA_KEY_REQUEST:
586 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
587 break;
588 case MPA_KEY_REPLY:
589 memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
590 break;
591 }
592 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
593 mpa_frame->rev = cm_node->mpa_frame_rev;
594 mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
595}
596
597static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
598{
599 u64 u64temp;
600 struct nes_qp *nesqp = *nesqp_addr;
601 struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
602
603 u64temp = (unsigned long)nesqp;
604 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
605 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
606
607 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
608 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
609
610 switch (cm_node->send_rdma0_op) {
611 case SEND_RDMA_WRITE_ZERO:
612 nes_debug(NES_DBG_CM, "Sending first write.\n");
613 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
614 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
615 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
616 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
617 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
618 break;
619
620 case SEND_RDMA_READ_ZERO:
621 default:
622 if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO) {
623 printk(KERN_ERR "%s[%u]: Unsupported RDMA0 len operation=%u\n",
624 __func__, __LINE__, cm_node->send_rdma0_op);
625 WARN_ON(1);
626 }
627 nes_debug(NES_DBG_CM, "Sending first rdma operation.\n");
628 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
629 cpu_to_le32(NES_IWARP_SQ_OP_RDMAR);
630 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX] = 1;
631 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_HIGH_IDX] = 0;
632 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] = 0;
633 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_STAG_IDX] = 1;
634 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 1;
635 break;
636 }
637
638 if (nesqp->sq_kmapped) {
639 nesqp->sq_kmapped = 0;
640 kunmap(nesqp->page);
641 }
642
643 /*use the reserved spot on the WQ for the extra first WQE*/
644 nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
645 NES_QPCONTEXT_ORDIRD_WRPDU |
646 NES_QPCONTEXT_ORDIRD_ALSMM));
647 nesqp->skip_lsmm = 1;
648 nesqp->hwqp.sq_tail = 0;
649}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800650
651/**
652 * schedule_nes_timer
653 * note - cm_node needs to be protected before calling this. Encase in:
654 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
655 */
656int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530657 enum nes_timer_type type, int send_retrans,
658 int close_when_complete)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800659{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530660 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700661 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800662 struct nes_timer_entry *new_send;
663 int ret = 0;
664 u32 was_timer_set;
665
666 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
667 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800668 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800669
670 /* new_send->timetosend = currenttime */
671 new_send->retrycount = NES_DEFAULT_RETRYS;
672 new_send->retranscount = NES_DEFAULT_RETRANS;
673 new_send->skb = skb;
674 new_send->timetosend = jiffies;
675 new_send->type = type;
676 new_send->netdev = cm_node->netdev;
677 new_send->send_retrans = send_retrans;
678 new_send->close_when_complete = close_when_complete;
679
680 if (type == NES_TIMER_TYPE_CLOSE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530681 new_send->timetosend += (HZ / 10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800682 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700683 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800684 WARN_ON(1);
685 return -EINVAL;
686 }
687 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800688 }
689
690 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700691 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800692 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700693 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
694 cm_node->send_entry = new_send;
695 add_ref_cm_node(cm_node);
696 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
697 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800698
699 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
700 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700701 nes_debug(NES_DBG_CM, "Error sending packet %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530702 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800703 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700704 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800705 } else {
706 cm_packets_sent++;
707 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700708 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800709 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700710 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800711 return ret;
712 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800713 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800714 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800715
716 was_timer_set = timer_pending(&cm_core->tcp_timer);
717
718 if (!was_timer_set) {
719 cm_core->tcp_timer.expires = new_send->timetosend;
720 add_timer(&cm_core->tcp_timer);
721 }
722
723 return ret;
724}
725
Faisal Latif9d5ab132009-03-06 15:15:01 -0800726static void nes_retrans_expired(struct nes_cm_node *cm_node)
727{
Faisal Latif68237a02009-06-22 22:53:28 -0700728 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latifdae58722010-08-14 21:04:56 +0000729 enum nes_cm_node_state state = cm_node->state;
730 cm_node->state = NES_CM_STATE_CLOSED;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530731
Faisal Latifdae58722010-08-14 21:04:56 +0000732 switch (state) {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800733 case NES_CM_STATE_SYN_RCVD:
734 case NES_CM_STATE_CLOSING:
735 rem_ref_cm_node(cm_node->cm_core, cm_node);
736 break;
737 case NES_CM_STATE_LAST_ACK:
738 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700739 if (cm_node->cm_id)
740 cm_id->rem_ref(cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800741 send_reset(cm_node, NULL);
742 break;
743 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800744 add_ref_cm_node(cm_node);
745 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800746 create_event(cm_node, NES_CM_EVENT_ABORTED);
747 }
748}
749
750static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
751{
752 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
753 struct iw_cm_id *cm_id = cm_node->cm_id;
754 struct nes_qp *nesqp;
755 unsigned long qplockflags;
756
757 if (!recv_entry)
758 return;
759 nesqp = (struct nes_qp *)recv_entry->skb;
760 if (nesqp) {
761 spin_lock_irqsave(&nesqp->lock, qplockflags);
762 if (nesqp->cm_id) {
763 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530764 "refcount = %d: HIT A "
765 "NES_TIMER_TYPE_CLOSE with something "
766 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
767 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800768 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
769 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
770 nesqp->ibqp_state = IB_QPS_ERR;
771 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
772 nes_cm_disconn(nesqp);
773 } else {
774 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
775 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530776 "refcount = %d: HIT A "
777 "NES_TIMER_TYPE_CLOSE with nothing "
778 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
779 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800780 }
781 } else if (rem_node) {
782 /* TIME_WAIT state */
783 rem_ref_cm_node(cm_node->cm_core, cm_node);
784 }
785 if (cm_node->cm_id)
786 cm_id->rem_ref(cm_id);
787 kfree(recv_entry);
788 cm_node->recv_entry = NULL;
789}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800790
791/**
792 * nes_cm_timer_tick
793 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700794static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800795{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800796 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800797 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800798 struct nes_cm_node *cm_node;
799 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800800 struct list_head *list_core_temp;
801 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800802 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800803 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700804 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800805 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800806
Faisal Latif879e5bd2008-11-21 20:50:41 -0600807 struct list_head timer_list;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530808
Faisal Latif879e5bd2008-11-21 20:50:41 -0600809 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800810 spin_lock_irqsave(&cm_core->ht_lock, flags);
811
Faisal Latif6492cdf2008-07-24 20:50:45 -0700812 list_for_each_safe(list_node, list_core_temp,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530813 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800814 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800815 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600816 add_ref_cm_node(cm_node);
817 list_add(&cm_node->timer_entry, &timer_list);
818 }
819 }
820 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
821
822 list_for_each_safe(list_node, list_core_temp, &timer_list) {
823 cm_node = container_of(list_node, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530824 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800825 recv_entry = cm_node->recv_entry;
826
827 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700828 if (time_after(recv_entry->timetosend, jiffies)) {
829 if (nexttimeout > recv_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530830 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800831 nexttimeout = recv_entry->timetosend;
832 settimer = 1;
833 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530834 } else {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800835 handle_recv_entry(cm_node, 1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530836 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800837 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800838
839 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700840 do {
841 send_entry = cm_node->send_entry;
842 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600843 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800844 if (time_after(send_entry->timetosend, jiffies)) {
845 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700846 if ((nexttimeout >
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530847 send_entry->timetosend) ||
848 !settimer) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700849 nexttimeout =
850 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800851 settimer = 1;
852 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800853 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700854 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800855 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800856 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800857 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700858
859 if ((cm_node->state == NES_CM_STATE_TSA) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530860 (cm_node->state == NES_CM_STATE_CLOSED)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700861 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600862 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800863 }
864
Faisal Latif6492cdf2008-07-24 20:50:45 -0700865 if (!send_entry->retranscount ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530866 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800867 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700868 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800869
Faisal Latif6492cdf2008-07-24 20:50:45 -0700870 spin_unlock_irqrestore(
871 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800872 nes_retrans_expired(cm_node);
873 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700874 spin_lock_irqsave(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530875 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600876 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800877 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800878 atomic_inc(&send_entry->skb->users);
879 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700880 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530881 "for node %p, jiffies = %lu, time to send = "
882 "%lu, retranscount = %u, send_entry->seq_num = "
883 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
884 "0x%08X\n", send_entry, cm_node, jiffies,
885 send_entry->timetosend,
886 send_entry->retranscount,
887 send_entry->seq_num,
888 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800889
Faisal Latif6492cdf2008-07-24 20:50:45 -0700890 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530891 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800892 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700893 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800894 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700895 nes_debug(NES_DBG_CM, "rexmit failed for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530896 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800897 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800898 send_entry->retrycount--;
899 nexttimeout = jiffies + NES_SHORT_TIME;
900 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600901 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800902 } else {
903 cm_packets_sent++;
904 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700905 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530906 "%u, retry count = %u.\n",
907 send_entry->retranscount,
908 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800909 if (send_entry->send_retrans) {
910 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700911 timetosend = (NES_RETRY_TIMEOUT <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530912 (NES_DEFAULT_RETRANS - send_entry->retranscount));
Faisal Latif4e9c3902009-04-27 13:39:36 -0700913
Faisal Latif6492cdf2008-07-24 20:50:45 -0700914 send_entry->timetosend = jiffies +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530915 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700916 if (nexttimeout > send_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530917 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800918 nexttimeout = send_entry->timetosend;
919 settimer = 1;
920 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800921 } else {
922 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700923 close_when_complete =
924 send_entry->close_when_complete;
925 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530926 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700927 free_retrans_entry(cm_node);
928 if (close_when_complete)
929 rem_ref_cm_node(cm_node->cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530930 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800931 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700932 } while (0);
933
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800934 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700935 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800936 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800937
938 if (settimer) {
939 if (!timer_pending(&cm_core->tcp_timer)) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530940 cm_core->tcp_timer.expires = nexttimeout;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800941 add_timer(&cm_core->tcp_timer);
942 }
943 }
944}
945
946
947/**
948 * send_syn
949 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700950static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530951 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800952{
953 int ret;
954 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800955 char optionsbuffer[sizeof(struct option_mss) +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530956 sizeof(struct option_windowscale) + sizeof(struct option_base) +
957 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800958
959 int optionssize = 0;
960 /* Sending MSS option */
961 union all_known_options *options;
962
963 if (!cm_node)
964 return -EINVAL;
965
966 options = (union all_known_options *)&optionsbuffer[optionssize];
967 options->as_mss.optionnum = OPTION_NUMBER_MSS;
968 options->as_mss.length = sizeof(struct option_mss);
969 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
970 optionssize += sizeof(struct option_mss);
971
972 options = (union all_known_options *)&optionsbuffer[optionssize];
973 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
974 options->as_windowscale.length = sizeof(struct option_windowscale);
975 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
976 optionssize += sizeof(struct option_windowscale);
977
Faisal Latif6492cdf2008-07-24 20:50:45 -0700978 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800979 options = (union all_known_options *)&optionsbuffer[optionssize];
980 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
981 options->as_base.length = sizeof(struct option_base);
982 optionssize += sizeof(struct option_base);
983 /* we need the size to be a multiple of 4 */
984 options = (union all_known_options *)&optionsbuffer[optionssize];
985 options->as_end = 1;
986 optionssize += 1;
987 options = (union all_known_options *)&optionsbuffer[optionssize];
988 options->as_end = 1;
989 optionssize += 1;
990 }
991
992 options = (union all_known_options *)&optionsbuffer[optionssize];
993 options->as_end = OPTION_NUMBER_END;
994 optionssize += 1;
995
Faisal Latif6492cdf2008-07-24 20:50:45 -0700996 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800997 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800998 if (!skb) {
999 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1000 return -1;
1001 }
1002
1003 if (sendack)
1004 flags |= SET_ACK;
1005
1006 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
1007 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1008
1009 return ret;
1010}
1011
1012
1013/**
1014 * send_reset
1015 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001016static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001017{
1018 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001019 int flags = SET_RST | SET_ACK;
1020
Faisal Latif6492cdf2008-07-24 20:50:45 -07001021 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001022 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001023 if (!skb) {
1024 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001025 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001026 }
1027
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001028 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
1029 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
1030
1031 return ret;
1032}
1033
1034
1035/**
1036 * send_ack
1037 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001038static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001039{
1040 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001041
1042 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001043 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001044
1045 if (!skb) {
1046 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1047 return -1;
1048 }
1049
1050 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
1051 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
1052
1053 return ret;
1054}
1055
1056
1057/**
1058 * send_fin
1059 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07001060static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001061{
1062 int ret;
1063
1064 /* if we didn't get a frame get one */
1065 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001066 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001067
1068 if (!skb) {
1069 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1070 return -1;
1071 }
1072
1073 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
1074 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1075
1076 return ret;
1077}
1078
1079
1080/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001081 * find_node - find a cm node that matches the reference cm node
1082 */
1083static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301084 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001085{
1086 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001087 struct list_head *hte;
1088 struct nes_cm_node *cm_node;
1089
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001090 /* get a handle on the hte */
1091 hte = &cm_core->connected_nodes;
1092
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001093 /* walk list and find cm_node associated with this session ID */
1094 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001095 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001096 /* compare quad, return node handle if a match */
1097 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301098 cm_node->loc_addr, cm_node->loc_port,
1099 loc_addr, loc_port,
1100 cm_node->rem_addr, cm_node->rem_port,
1101 rem_addr, rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001102 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301103 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001104 add_ref_cm_node(cm_node);
1105 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1106 return cm_node;
1107 }
1108 }
1109 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1110
1111 /* no owner node */
1112 return NULL;
1113}
1114
1115
1116/**
1117 * find_listener - find a cm node listening on this addr-port pair
1118 */
1119static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301120 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001121{
1122 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001123 struct nes_cm_listener *listen_node;
1124
1125 /* walk list and find cm_node associated with this session ID */
1126 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001127 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001128 /* compare node pair, return node handle if a match */
1129 if (((listen_node->loc_addr == dst_addr) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301130 listen_node->loc_addr == 0x00000000) &&
1131 (listen_node->loc_port == dst_port) &&
1132 (listener_state & listen_node->listener_state)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001133 atomic_inc(&listen_node->ref_count);
1134 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1135 return listen_node;
1136 }
1137 }
1138 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1139
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001140 /* no listener */
1141 return NULL;
1142}
1143
1144
1145/**
1146 * add_hte_node - add a cm node to the hash table
1147 */
1148static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1149{
1150 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001151 struct list_head *hte;
1152
1153 if (!cm_node || !cm_core)
1154 return -EINVAL;
1155
Faisal Latif6492cdf2008-07-24 20:50:45 -07001156 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301157 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001158
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001159 spin_lock_irqsave(&cm_core->ht_lock, flags);
1160
1161 /* get a handle on the hash table element (list head for this slot) */
1162 hte = &cm_core->connected_nodes;
1163 list_add_tail(&cm_node->list, hte);
1164 atomic_inc(&cm_core->ht_node_cnt);
1165
1166 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1167
1168 return 0;
1169}
1170
1171
1172/**
1173 * mini_cm_dec_refcnt_listen
1174 */
1175static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301176 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001177{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001178 int ret = -EINVAL;
1179 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001180 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001181 struct list_head *list_pos = NULL;
1182 struct list_head *list_temp = NULL;
1183 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -06001184 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001185
1186 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301187 "refcnt=%d\n", listener, free_hanging_nodes,
1188 atomic_read(&listener->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001189 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -06001190 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001191 if (free_hanging_nodes) {
1192 spin_lock_irqsave(&cm_core->ht_lock, flags);
1193 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -06001194 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001195 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301196 list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001197 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -06001198 (!cm_node->accelerated)) {
1199 add_ref_cm_node(cm_node);
1200 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001201 }
1202 }
1203 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1204 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001205
1206 list_for_each_safe(list_pos, list_temp, &reset_list) {
1207 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301208 reset_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001209 {
1210 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -08001211 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001212 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
1213 rem_ref_cm_node(cm_node->cm_core, cm_node);
1214 } else {
1215 if (!loopback) {
1216 cleanup_retrans_entry(cm_node);
1217 err = send_reset(cm_node, NULL);
1218 if (err) {
1219 cm_node->state =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301220 NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001221 WARN_ON(1);
1222 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08001223 old_state = cm_node->state;
1224 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1225 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1226 rem_ref_cm_node(
1227 cm_node->cm_core,
1228 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001229 }
1230 } else {
1231 struct nes_cm_event event;
1232
1233 event.cm_node = loopback;
1234 event.cm_info.rem_addr =
1235 loopback->rem_addr;
1236 event.cm_info.loc_addr =
1237 loopback->loc_addr;
1238 event.cm_info.rem_port =
1239 loopback->rem_port;
1240 event.cm_info.loc_port =
1241 loopback->loc_port;
1242 event.cm_info.cm_id = loopback->cm_id;
Faisal Latif43093b92010-02-12 19:57:04 +00001243 add_ref_cm_node(loopback);
1244 loopback->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001245 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001246 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001247
Faisal Latif9d5ab132009-03-06 15:15:01 -08001248 rem_ref_cm_node(cm_node->cm_core,
1249 cm_node);
1250
1251 }
1252 }
1253 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001254 }
1255
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001256 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1257 if (!atomic_dec_return(&listener->ref_count)) {
1258 list_del(&listener->list);
1259
1260 /* decrement our listen node count */
1261 atomic_dec(&cm_core->listen_node_cnt);
1262
1263 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1264
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301265 if (listener->nesvnic)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001266 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301267 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001268
1269 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1270
1271 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001272 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001273 ret = 0;
Faisal Latif6e10d2e2010-02-12 19:55:03 +00001274 atomic_inc(&cm_listens_destroyed);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001275 } else {
1276 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1277 }
1278 if (listener) {
1279 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1280 nes_debug(NES_DBG_CM, "destroying listener (%p)"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301281 " with non-zero pending accepts=%u\n",
1282 listener, atomic_read(&listener->pend_accepts_cnt));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001283 }
1284
1285 return ret;
1286}
1287
1288
1289/**
1290 * mini_cm_del_listen
1291 */
1292static int mini_cm_del_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301293 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001294{
1295 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1296 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1297 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1298}
1299
1300
1301/**
1302 * mini_cm_accelerated
1303 */
1304static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301305 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001306{
1307 u32 was_timer_set;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301308
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001309 cm_node->accelerated = 1;
1310
1311 if (cm_node->accept_pend) {
1312 BUG_ON(!cm_node->listener);
1313 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001314 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001315 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1316 }
1317
1318 was_timer_set = timer_pending(&cm_core->tcp_timer);
1319 if (!was_timer_set) {
1320 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1321 add_timer(&cm_core->tcp_timer);
1322 }
1323
1324 return 0;
1325}
1326
1327
1328/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001329 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001330 */
Faisal Latif7a576df2009-12-09 15:54:33 -08001331static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001332{
1333 struct rtable *rt;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001334 struct neighbour *neigh;
Faisal Latif7a576df2009-12-09 15:54:33 -08001335 int rc = arpindex;
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001336 struct net_device *netdev;
Faisal Latif7a576df2009-12-09 15:54:33 -08001337 struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001338
David S. Miller78fbfd82011-03-12 00:00:52 -05001339 rt = ip_route_output(&init_net, htonl(dst_ip), 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001340 if (IS_ERR(rt)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001341 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301342 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001343 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001344 }
1345
Roland Dreiercf55bb22011-03-24 17:13:20 -07001346 if (netif_is_bond_slave(nesvnic->netdev))
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001347 netdev = nesvnic->netdev->master;
1348 else
1349 netdev = nesvnic->netdev;
1350
David Millere55684f2012-01-24 13:16:03 +00001351 neigh = dst_neigh_lookup(&rt->dst, &dst_ip);
1352
David Miller40e2bb52011-12-02 16:52:27 +00001353 rcu_read_lock();
Bob Sharp7191a0a2008-10-03 12:21:19 -07001354 if (neigh) {
1355 if (neigh->nud_state & NUD_VALID) {
1356 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001357 " is %pM, Gateway is 0x%08X \n", dst_ip,
1358 neigh->ha, ntohl(rt->rt_gateway));
Faisal Latif7a576df2009-12-09 15:54:33 -08001359
1360 if (arpindex >= 0) {
1361 if (!memcmp(nesadapter->arp_table[arpindex].mac_addr,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301362 neigh->ha, ETH_ALEN)) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001363 /* Mac address same as in nes_arp_table */
David Millere55684f2012-01-24 13:16:03 +00001364 goto out;
Faisal Latif7a576df2009-12-09 15:54:33 -08001365 }
1366
1367 nes_manage_arp_cache(nesvnic->netdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301368 nesadapter->arp_table[arpindex].mac_addr,
1369 dst_ip, NES_ARP_DELETE);
Faisal Latif7a576df2009-12-09 15:54:33 -08001370 }
1371
Bob Sharp7191a0a2008-10-03 12:21:19 -07001372 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1373 dst_ip, NES_ARP_ADD);
1374 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1375 NES_ARP_RESOLVE);
David Miller40e2bb52011-12-02 16:52:27 +00001376 } else {
1377 neigh_event_send(neigh, NULL);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001378 }
Bob Sharp7191a0a2008-10-03 12:21:19 -07001379 }
David Millere55684f2012-01-24 13:16:03 +00001380out:
David Miller40e2bb52011-12-02 16:52:27 +00001381 rcu_read_unlock();
David Millere55684f2012-01-24 13:16:03 +00001382
1383 if (neigh)
1384 neigh_release(neigh);
1385
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001386 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001387 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001388}
1389
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001390/**
1391 * make_cm_node - create a new instance of a cm node
1392 */
1393static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301394 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1395 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001396{
1397 struct nes_cm_node *cm_node;
1398 struct timespec ts;
Faisal Latif7a576df2009-12-09 15:54:33 -08001399 int oldarpindex = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001400 int arpindex = 0;
1401 struct nes_device *nesdev;
1402 struct nes_adapter *nesadapter;
1403
1404 /* create an hte and cm_node for this instance */
1405 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1406 if (!cm_node)
1407 return NULL;
1408
1409 /* set our node specific transport info */
1410 cm_node->loc_addr = cm_info->loc_addr;
1411 cm_node->rem_addr = cm_info->rem_addr;
1412 cm_node->loc_port = cm_info->loc_port;
1413 cm_node->rem_port = cm_info->rem_port;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301414
1415 cm_node->mpa_frame_rev = mpa_version;
1416 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
1417 cm_node->ird_size = IETF_NO_IRD_ORD;
1418 cm_node->ord_size = IETF_NO_IRD_ORD;
1419
Harvey Harrison63779432008-10-31 00:56:00 -07001420 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1421 &cm_node->loc_addr, cm_node->loc_port,
1422 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001423 cm_node->listener = listener;
1424 cm_node->netdev = nesvnic->netdev;
1425 cm_node->cm_id = cm_info->cm_id;
1426 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1427
Faisal Latif6492cdf2008-07-24 20:50:45 -07001428 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301429 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001430
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001431 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001432
1433 cm_node->loopbackpartner = NULL;
1434 atomic_set(&cm_node->ref_count, 1);
1435 /* associate our parent CM core */
1436 cm_node->cm_core = cm_core;
1437 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1438 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1439 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301440 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001441 ts = current_kernel_time();
1442 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1443 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301444 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001445 cm_node->tcp_cntxt.rcv_nxt = 0;
1446 /* get a unique session ID , add thread_id to an upcounter to handle race */
1447 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001448 cm_node->conn_type = cm_info->conn_type;
1449 cm_node->apbvt_set = 0;
1450 cm_node->accept_pend = 0;
1451
1452 cm_node->nesvnic = nesvnic;
1453 /* get some device handles, for arp lookup */
1454 nesdev = nesvnic->nesdev;
1455 nesadapter = nesdev->nesadapter;
1456
1457 cm_node->loopbackpartner = NULL;
Faisal Latif7a576df2009-12-09 15:54:33 -08001458
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001459 /* get the mac addr for the remote node */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301460 if (ipv4_is_loopback(htonl(cm_node->rem_addr))) {
Chien Tung1ee86552008-11-21 20:51:04 -06001461 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301462 } else {
Faisal Latif7a576df2009-12-09 15:54:33 -08001463 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1464 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);
Faisal Latif7a576df2009-12-09 15:54:33 -08001465 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001466 if (arpindex < 0) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001467 kfree(cm_node);
1468 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001469 }
1470
1471 /* copy the mac addr to node context */
1472 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001473 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1474 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001475
1476 add_hte_node(cm_core, cm_node);
1477 atomic_inc(&cm_nodes_created);
1478
1479 return cm_node;
1480}
1481
1482
1483/**
1484 * add_ref_cm_node - destroy an instance of a cm node
1485 */
1486static int add_ref_cm_node(struct nes_cm_node *cm_node)
1487{
1488 atomic_inc(&cm_node->ref_count);
1489 return 0;
1490}
1491
1492
1493/**
1494 * rem_ref_cm_node - destroy an instance of a cm node
1495 */
1496static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301497 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001498{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001499 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001500 struct nes_qp *nesqp;
1501
1502 if (!cm_node)
1503 return -EINVAL;
1504
1505 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1506 if (atomic_dec_return(&cm_node->ref_count)) {
1507 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1508 return 0;
1509 }
1510 list_del(&cm_node->list);
1511 atomic_dec(&cm_core->ht_node_cnt);
1512 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1513
1514 /* if the node is destroyed before connection was accelerated */
1515 if (!cm_node->accelerated && cm_node->accept_pend) {
1516 BUG_ON(!cm_node->listener);
1517 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1518 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1519 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001520 WARN_ON(cm_node->send_entry);
1521 if (cm_node->recv_entry)
1522 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001523 if (cm_node->listener) {
1524 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1525 } else {
1526 if (cm_node->apbvt_set && cm_node->nesvnic) {
1527 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301528 PCI_FUNC(
1529 cm_node->nesvnic->nesdev->pcidev->devfn),
1530 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001531 }
1532 }
1533
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001534 atomic_dec(&cm_core->node_cnt);
1535 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001536 nesqp = cm_node->nesqp;
1537 if (nesqp) {
1538 nesqp->cm_node = NULL;
1539 nes_rem_ref(&nesqp->ibqp);
1540 cm_node->nesqp = NULL;
1541 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001542
Faisal Latif6492cdf2008-07-24 20:50:45 -07001543 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001544 return 0;
1545}
1546
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001547/**
1548 * process_options
1549 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001550static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301551 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001552{
1553 u32 tmp;
1554 u32 offset = 0;
1555 union all_known_options *all_options;
1556 char got_mss_option = 0;
1557
1558 while (offset < optionsize) {
1559 all_options = (union all_known_options *)(optionsloc + offset);
1560 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001561 case OPTION_NUMBER_END:
1562 offset = optionsize;
1563 break;
1564 case OPTION_NUMBER_NONE:
1565 offset += 1;
1566 continue;
1567 case OPTION_NUMBER_MSS:
1568 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301569 "Size: %d\n", __func__,
1570 all_options->as_mss.length, offset, optionsize);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001571 got_mss_option = 1;
1572 if (all_options->as_mss.length != 4) {
1573 return 1;
1574 } else {
1575 tmp = ntohs(all_options->as_mss.mss);
1576 if (tmp > 0 && tmp <
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301577 cm_node->tcp_cntxt.mss)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001578 cm_node->tcp_cntxt.mss = tmp;
1579 }
1580 break;
1581 case OPTION_NUMBER_WINDOW_SCALE:
1582 cm_node->tcp_cntxt.snd_wscale =
1583 all_options->as_windowscale.shiftcount;
1584 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001585 default:
1586 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301587 all_options->as_base.optionnum);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001588 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001589 }
1590 offset += all_options->as_base.length;
1591 }
1592 if ((!got_mss_option) && (syn_packet))
1593 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1594 return 0;
1595}
1596
Faisal Latif6492cdf2008-07-24 20:50:45 -07001597static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001598{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001599 atomic_inc(&cm_accel_dropped_pkts);
1600 dev_kfree_skb_any(skb);
1601}
1602
Faisal Latif9d5ab132009-03-06 15:15:01 -08001603static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001604{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001605 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301606 "refcnt=%d\n", cm_node, cm_node->state,
1607 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001608 switch (cm_node->state) {
1609 case NES_CM_STATE_SYN_RCVD:
1610 case NES_CM_STATE_SYN_SENT:
1611 case NES_CM_STATE_ESTABLISHED:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001612 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001613 cm_node->tcp_cntxt.rcv_nxt++;
1614 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001615 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001616 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001617 break;
Faisal Latifb1190d32009-12-09 15:54:32 -08001618 case NES_CM_STATE_MPAREQ_SENT:
1619 create_event(cm_node, NES_CM_EVENT_ABORTED);
1620 cm_node->tcp_cntxt.rcv_nxt++;
1621 cleanup_retrans_entry(cm_node);
1622 cm_node->state = NES_CM_STATE_CLOSED;
1623 add_ref_cm_node(cm_node);
1624 send_reset(cm_node, NULL);
1625 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001626 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001627 cm_node->tcp_cntxt.rcv_nxt++;
1628 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001629 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001630 send_ack(cm_node, NULL);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001631 /* Wait for ACK as this is simultaneous close..
Faisal Latif9d5ab132009-03-06 15:15:01 -08001632 * After we receive ACK, do not send anything..
1633 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001634 break;
1635 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001636 cm_node->tcp_cntxt.rcv_nxt++;
1637 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001638 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001639 send_ack(cm_node, NULL);
1640 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1641 break;
1642 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001643 cm_node->tcp_cntxt.rcv_nxt++;
1644 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001645 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001646 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001647 break;
1648 case NES_CM_STATE_TSA:
1649 default:
1650 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1651 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001652 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001653 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001654}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001655
Faisal Latif6492cdf2008-07-24 20:50:45 -07001656
1657static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1658 struct tcphdr *tcph)
1659{
1660
1661 int reset = 0; /* whether to send reset in case of err.. */
1662 atomic_inc(&cm_resets_recvd);
1663 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1664 " refcnt=%d\n", cm_node, cm_node->state,
1665 atomic_read(&cm_node->ref_count));
1666 cleanup_retrans_entry(cm_node);
1667 switch (cm_node->state) {
1668 case NES_CM_STATE_SYN_SENT:
1669 case NES_CM_STATE_MPAREQ_SENT:
1670 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1671 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1672 cm_node->listener, cm_node->state);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301673 switch (cm_node->mpa_frame_rev) {
1674 case IETF_MPA_V2:
1675 cm_node->mpa_frame_rev = IETF_MPA_V1;
1676 /* send a syn and goto syn sent state */
1677 cm_node->state = NES_CM_STATE_SYN_SENT;
1678 if (send_syn(cm_node, 0, NULL)) {
1679 active_open_err(cm_node, skb, reset);
1680 }
1681 break;
1682 case IETF_MPA_V1:
1683 default:
1684 active_open_err(cm_node, skb, reset);
1685 break;
1686 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001687 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001688 case NES_CM_STATE_MPAREQ_RCVD:
Dan Carpenter4d8d6382010-09-15 18:32:39 +02001689 atomic_inc(&cm_node->passive_state);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001690 dev_kfree_skb_any(skb);
1691 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001692 case NES_CM_STATE_ESTABLISHED:
1693 case NES_CM_STATE_SYN_RCVD:
1694 case NES_CM_STATE_LISTENING:
1695 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1696 passive_open_err(cm_node, skb, reset);
1697 break;
1698 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001699 active_open_err(cm_node, skb, reset);
1700 break;
1701 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001702 drop_packet(skb);
1703 break;
Faisal Latifdae58722010-08-14 21:04:56 +00001704 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif68237a02009-06-22 22:53:28 -07001705 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001706 case NES_CM_STATE_LAST_ACK:
1707 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001708 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001709 cm_node->state = NES_CM_STATE_CLOSED;
1710 rem_ref_cm_node(cm_node->cm_core, cm_node);
1711 drop_packet(skb);
1712 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001713 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001714 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001715 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001716 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001717}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001718
Faisal Latif9d5ab132009-03-06 15:15:01 -08001719
1720static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001721{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301722 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001723 int datasize = skb->len;
1724 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001725
1726 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301727 u32 res_type;
1728
Faisal Latif9d5ab132009-03-06 15:15:01 -08001729 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1730 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001731 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001732 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001733 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301734 "cm_node=%p listener=%p state=%d\n", __func__,
1735 __LINE__, cm_node, cm_node->listener,
1736 cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001737 active_open_err(cm_node, skb, 1);
1738 } else {
1739 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001740 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001741 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001742 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001743
1744 switch (cm_node->state) {
1745 case NES_CM_STATE_ESTABLISHED:
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301746 if (res_type == NES_MPA_REQUEST_REJECT)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001747 /*BIG problem as we are receiving the MPA.. So should
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301748 * not be REJECT.. This is Passive Open.. We can
1749 * only receive it Reject for Active Open...*/
Faisal Latif9d5ab132009-03-06 15:15:01 -08001750 WARN_ON(1);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001751 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1752 type = NES_CM_EVENT_MPA_REQ;
1753 atomic_set(&cm_node->passive_state,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301754 NES_PASSIVE_STATE_INDICATED);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001755 break;
1756 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001757 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001758 if (res_type == NES_MPA_REQUEST_REJECT) {
1759 type = NES_CM_EVENT_MPA_REJECT;
1760 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1761 } else {
1762 type = NES_CM_EVENT_CONNECTED;
1763 cm_node->state = NES_CM_STATE_TSA;
1764 }
1765
1766 break;
1767 default:
1768 WARN_ON(1);
1769 break;
1770 }
1771 dev_kfree_skb_any(skb);
1772 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001773}
1774
1775static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1776{
1777 switch (cm_node->state) {
1778 case NES_CM_STATE_SYN_SENT:
1779 case NES_CM_STATE_MPAREQ_SENT:
1780 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301781 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1782 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001783 active_open_err(cm_node, skb, 1);
1784 break;
1785 case NES_CM_STATE_ESTABLISHED:
1786 case NES_CM_STATE_SYN_RCVD:
1787 passive_open_err(cm_node, skb, 1);
1788 break;
1789 case NES_CM_STATE_TSA:
1790 default:
1791 drop_packet(skb);
1792 }
1793}
1794
1795static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301796 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001797{
1798 int err;
1799
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301800 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001801 if (err)
1802 active_open_err(cm_node, skb, 1);
1803
1804 return err;
1805}
1806
1807static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301808 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001809{
1810 int err = 0;
1811 u32 seq;
1812 u32 ack_seq;
1813 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1814 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1815 u32 rcv_wnd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301816
Faisal Latif6492cdf2008-07-24 20:50:45 -07001817 seq = ntohl(tcph->seq);
1818 ack_seq = ntohl(tcph->ack_seq);
1819 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1820 if (ack_seq != loc_seq_num)
1821 err = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301822 else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001823 err = 1;
1824 if (err) {
1825 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301826 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1827 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001828 indicate_pkt_err(cm_node, skb);
1829 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301830 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1831 rcv_wnd);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001832 }
1833 return err;
1834}
1835
1836/*
1837 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1838 * is created with a listener or it may comein as rexmitted packet which in
1839 * that case will be just dropped.
1840 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001841static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301842 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001843{
1844 int ret;
1845 u32 inc_sequence;
1846 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001847
1848 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001849 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001850 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001851
Faisal Latif6492cdf2008-07-24 20:50:45 -07001852 switch (cm_node->state) {
1853 case NES_CM_STATE_SYN_SENT:
1854 case NES_CM_STATE_MPAREQ_SENT:
1855 /* Rcvd syn on active open connection*/
1856 active_open_err(cm_node, skb, 1);
1857 break;
1858 case NES_CM_STATE_LISTENING:
1859 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001860 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301861 cm_node->listener->backlog) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001862 nes_debug(NES_DBG_CM, "drop syn due to backlog "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301863 "pressure \n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07001864 cm_backlog_drops++;
1865 passive_open_err(cm_node, skb, 0);
1866 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001867 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001868 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301869 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001870 if (ret) {
1871 passive_open_err(cm_node, skb, 0);
1872 /* drop pkt */
1873 break;
1874 }
1875 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1876 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001877 cm_node->accept_pend = 1;
1878 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1879
Faisal Latif6492cdf2008-07-24 20:50:45 -07001880 cm_node->state = NES_CM_STATE_SYN_RCVD;
1881 send_syn(cm_node, 1, skb);
1882 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001883 case NES_CM_STATE_CLOSED:
1884 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001885 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001886 send_reset(cm_node, skb);
1887 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001888 case NES_CM_STATE_TSA:
1889 case NES_CM_STATE_ESTABLISHED:
1890 case NES_CM_STATE_FIN_WAIT1:
1891 case NES_CM_STATE_FIN_WAIT2:
1892 case NES_CM_STATE_MPAREQ_RCVD:
1893 case NES_CM_STATE_LAST_ACK:
1894 case NES_CM_STATE_CLOSING:
1895 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001896 default:
1897 drop_packet(skb);
1898 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001899 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001900}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001901
Faisal Latif6492cdf2008-07-24 20:50:45 -07001902static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301903 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001904{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001905 int ret;
1906 u32 inc_sequence;
1907 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001908
Faisal Latif6492cdf2008-07-24 20:50:45 -07001909 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001910 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001911 inc_sequence = ntohl(tcph->seq);
1912 switch (cm_node->state) {
1913 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001914 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001915 /* active open */
1916 if (check_syn(cm_node, tcph, skb))
1917 return;
1918 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1919 /* setup options */
1920 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1921 if (ret) {
1922 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301923 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001924 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001925 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001926 cleanup_retrans_entry(cm_node);
1927 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1928 send_mpa_request(cm_node, skb);
1929 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1930 break;
1931 case NES_CM_STATE_MPAREQ_RCVD:
1932 /* passive open, so should not be here */
1933 passive_open_err(cm_node, skb, 1);
1934 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001935 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001936 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1937 cleanup_retrans_entry(cm_node);
1938 cm_node->state = NES_CM_STATE_CLOSED;
1939 send_reset(cm_node, skb);
1940 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001941 case NES_CM_STATE_CLOSED:
1942 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1943 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001944 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001945 send_reset(cm_node, skb);
1946 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001947 case NES_CM_STATE_ESTABLISHED:
1948 case NES_CM_STATE_FIN_WAIT1:
1949 case NES_CM_STATE_FIN_WAIT2:
1950 case NES_CM_STATE_LAST_ACK:
1951 case NES_CM_STATE_TSA:
1952 case NES_CM_STATE_CLOSING:
1953 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001954 case NES_CM_STATE_MPAREQ_SENT:
1955 default:
1956 drop_packet(skb);
1957 break;
1958 }
1959}
1960
Faisal Latif109d67e2009-04-27 13:41:06 -07001961static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301962 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001963{
1964 int datasize = 0;
1965 u32 inc_sequence;
Faisal Latif109d67e2009-04-27 13:41:06 -07001966 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06001967 int optionsize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301968
Faisal Latifabb77252008-11-21 20:50:52 -06001969 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001970
Faisal Latif6492cdf2008-07-24 20:50:45 -07001971 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07001972 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001973
1974 skb_pull(skb, tcph->doff << 2);
1975 inc_sequence = ntohl(tcph->seq);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001976 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001977 switch (cm_node->state) {
1978 case NES_CM_STATE_SYN_RCVD:
1979 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07001980 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001981 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1982 if (ret)
1983 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001984 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1985 cm_node->state = NES_CM_STATE_ESTABLISHED;
1986 if (datasize) {
1987 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001988 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301989 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001990 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301991 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001992 break;
1993 case NES_CM_STATE_ESTABLISHED:
1994 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001995 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001996 if (datasize) {
1997 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001998 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301999 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002000 drop_packet(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302001 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002002 break;
2003 case NES_CM_STATE_MPAREQ_SENT:
2004 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2005 if (datasize) {
2006 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002007 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302008 } else { /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002009 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302010 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002011 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002012 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002013 cleanup_retrans_entry(cm_node);
2014 cm_node->state = NES_CM_STATE_CLOSED;
2015 send_reset(cm_node, skb);
2016 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002017 case NES_CM_STATE_CLOSED:
2018 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08002019 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002020 send_reset(cm_node, skb);
2021 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002022 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002023 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002024 cleanup_retrans_entry(cm_node);
2025 cm_node->state = NES_CM_STATE_CLOSED;
2026 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002027 rem_ref_cm_node(cm_node->cm_core, cm_node);
2028 drop_packet(skb);
2029 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002030 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002031 cleanup_retrans_entry(cm_node);
2032 drop_packet(skb);
2033 cm_node->state = NES_CM_STATE_FIN_WAIT2;
2034 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002035 case NES_CM_STATE_SYN_SENT:
2036 case NES_CM_STATE_FIN_WAIT2:
2037 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002038 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002039 case NES_CM_STATE_UNKNOWN:
2040 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002041 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002042 drop_packet(skb);
2043 break;
2044 }
Faisal Latif109d67e2009-04-27 13:41:06 -07002045 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002046}
2047
2048
2049
2050static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302051 struct sk_buff *skb, int optionsize, int passive)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002052{
2053 u8 *optionsloc = (u8 *)&tcph[1];
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302054
Faisal Latif6492cdf2008-07-24 20:50:45 -07002055 if (optionsize) {
2056 if (process_options(cm_node, optionsloc, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302057 (u32)tcph->syn)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002058 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302059 __func__, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002060 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06002061 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002062 else
Faisal Latifabb77252008-11-21 20:50:52 -06002063 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002064 return 1;
2065 }
2066 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002067
2068 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302069 cm_node->tcp_cntxt.snd_wscale;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002070
Faisal Latif6492cdf2008-07-24 20:50:45 -07002071 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002072 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002073 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002074}
2075
Faisal Latif6492cdf2008-07-24 20:50:45 -07002076/*
2077 * active_open_err() will send reset() if flag set..
2078 * It will also send ABORT event.
2079 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002080static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302081 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002082{
2083 cleanup_retrans_entry(cm_node);
2084 if (reset) {
2085 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302086 "state=%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002087 add_ref_cm_node(cm_node);
2088 send_reset(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302089 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002090 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302091 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002092
2093 cm_node->state = NES_CM_STATE_CLOSED;
2094 create_event(cm_node, NES_CM_EVENT_ABORTED);
2095}
2096
2097/*
2098 * passive_open_err() will either do a reset() or will free up the skb and
2099 * remove the cm_node.
2100 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002101static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302102 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002103{
2104 cleanup_retrans_entry(cm_node);
2105 cm_node->state = NES_CM_STATE_CLOSED;
2106 if (reset) {
2107 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302108 "cm_node=%p state =%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002109 send_reset(cm_node, skb);
2110 } else {
2111 dev_kfree_skb_any(skb);
2112 rem_ref_cm_node(cm_node->cm_core, cm_node);
2113 }
2114}
2115
2116/*
2117 * free_retrans_entry() routines assumes that the retrans_list_lock has
2118 * been acquired before calling.
2119 */
2120static void free_retrans_entry(struct nes_cm_node *cm_node)
2121{
2122 struct nes_timer_entry *send_entry;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302123
Faisal Latif6492cdf2008-07-24 20:50:45 -07002124 send_entry = cm_node->send_entry;
2125 if (send_entry) {
2126 cm_node->send_entry = NULL;
2127 dev_kfree_skb_any(send_entry->skb);
2128 kfree(send_entry);
2129 rem_ref_cm_node(cm_node->cm_core, cm_node);
2130 }
2131}
2132
2133static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
2134{
2135 unsigned long flags;
2136
2137 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
2138 free_retrans_entry(cm_node);
2139 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
2140}
2141
2142/**
2143 * process_packet
2144 * Returns skb if to be freed, else it will return NULL if already used..
2145 */
2146static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302147 struct nes_cm_core *cm_core)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002148{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302149 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002150 struct tcphdr *tcph = tcp_hdr(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302151 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07002152 int ret = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302153
Faisal Latif6492cdf2008-07-24 20:50:45 -07002154 skb_pull(skb, ip_hdr(skb)->ihl << 2);
2155
2156 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302157 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
2158 tcph->ack, tcph->rst, tcph->fin);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002159
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302160 if (tcph->rst) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002161 pkt_type = NES_PKT_TYPE_RST;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302162 } else if (tcph->syn) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002163 pkt_type = NES_PKT_TYPE_SYN;
2164 if (tcph->ack)
2165 pkt_type = NES_PKT_TYPE_SYNACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302166 } else if (tcph->ack) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002167 pkt_type = NES_PKT_TYPE_ACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302168 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002169 if (tcph->fin)
2170 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002171
2172 switch (pkt_type) {
2173 case NES_PKT_TYPE_SYN:
2174 handle_syn_pkt(cm_node, skb, tcph);
2175 break;
2176 case NES_PKT_TYPE_SYNACK:
2177 handle_synack_pkt(cm_node, skb, tcph);
2178 break;
2179 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002180 ret = handle_ack_pkt(cm_node, skb, tcph);
2181 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08002182 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002183 break;
2184 case NES_PKT_TYPE_RST:
2185 handle_rst_pkt(cm_node, skb, tcph);
2186 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002187 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002188 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08002189 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07002190 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002191 break;
2192 }
2193}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002194
2195/**
2196 * mini_cm_listen - create a listen node with params
2197 */
2198static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302199 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002200{
2201 struct nes_cm_listener *listener;
2202 unsigned long flags;
2203
2204 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302205 cm_info->loc_addr, cm_info->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002206
2207 /* cannot have multiple matching listeners */
2208 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302209 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002210 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
2211 /* find automatically incs ref count ??? */
2212 atomic_dec(&listener->ref_count);
2213 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
2214 return NULL;
2215 }
2216
2217 if (!listener) {
2218 /* create a CM listen node (1/2 node to compare incoming traffic to) */
2219 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
2220 if (!listener) {
2221 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
2222 return NULL;
2223 }
2224
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002225 listener->loc_addr = htonl(cm_info->loc_addr);
2226 listener->loc_port = htons(cm_info->loc_port);
2227 listener->reused_node = 0;
2228
2229 atomic_set(&listener->ref_count, 1);
2230 }
2231 /* pasive case */
2232 /* find already inc'ed the ref count */
2233 else {
2234 listener->reused_node = 1;
2235 }
2236
2237 listener->cm_id = cm_info->cm_id;
2238 atomic_set(&listener->pend_accepts_cnt, 0);
2239 listener->cm_core = cm_core;
2240 listener->nesvnic = nesvnic;
2241 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002242
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002243 listener->conn_type = cm_info->conn_type;
2244 listener->backlog = cm_info->backlog;
2245 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
2246
2247 if (!listener->reused_node) {
2248 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2249 list_add(&listener->list, &cm_core->listen_list.list);
2250 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2251 atomic_inc(&cm_core->listen_node_cnt);
2252 }
2253
2254 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302255 " listener = %p, backlog = %d, cm_id = %p.\n",
2256 cm_info->loc_addr, cm_info->loc_port,
2257 listener, listener->backlog, listener->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002258
2259 return listener;
2260}
2261
2262
2263/**
2264 * mini_cm_connect - make a connection node with params
2265 */
Faisal Latif54c86a82008-09-30 14:47:27 -07002266static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302267 struct nes_vnic *nesvnic, u16 private_data_len,
2268 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002269{
2270 int ret = 0;
2271 struct nes_cm_node *cm_node;
2272 struct nes_cm_listener *loopbackremotelistener;
2273 struct nes_cm_node *loopbackremotenode;
2274 struct nes_cm_info loopback_cm_info;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302275 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002276
2277 /* create a CM connection node */
2278 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2279 if (!cm_node)
2280 return NULL;
2281
Glenn Streiff7495ab62008-04-29 13:46:54 -07002282 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002283 cm_node->tcp_cntxt.client = 1;
2284 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2285
2286 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002287 loopbackremotelistener = find_listener(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302288 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2289 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002290 if (loopbackremotelistener == NULL) {
2291 create_event(cm_node, NES_CM_EVENT_ABORTED);
2292 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002293 loopback_cm_info = *cm_info;
2294 loopback_cm_info.loc_port = cm_info->rem_port;
2295 loopback_cm_info.rem_port = cm_info->loc_port;
2296 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002297 loopbackremotenode = make_cm_node(cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302298 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002299 if (!loopbackremotenode) {
2300 rem_ref_cm_node(cm_node->cm_core, cm_node);
2301 return NULL;
2302 }
2303 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002304 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002305 loopbackremotenode->tcp_cntxt.rcv_wscale =
2306 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002307 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002308 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302309 private_data_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002310 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002311
Faisal Latif6492cdf2008-07-24 20:50:45 -07002312 /* we are done handling this state. */
2313 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002314 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002315 cm_node->tcp_cntxt.rcv_nxt =
2316 loopbackremotenode->tcp_cntxt.loc_seq_num;
2317 loopbackremotenode->tcp_cntxt.rcv_nxt =
2318 cm_node->tcp_cntxt.loc_seq_num;
2319 cm_node->tcp_cntxt.max_snd_wnd =
2320 loopbackremotenode->tcp_cntxt.rcv_wnd;
2321 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2322 cm_node->tcp_cntxt.rcv_wnd;
2323 cm_node->tcp_cntxt.snd_wnd =
2324 loopbackremotenode->tcp_cntxt.rcv_wnd;
2325 loopbackremotenode->tcp_cntxt.snd_wnd =
2326 cm_node->tcp_cntxt.rcv_wnd;
2327 cm_node->tcp_cntxt.snd_wscale =
2328 loopbackremotenode->tcp_cntxt.rcv_wscale;
2329 loopbackremotenode->tcp_cntxt.snd_wscale =
2330 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002331 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002332 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2333 }
2334 return cm_node;
2335 }
2336
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302337 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
2338 cm_node->mpa_frame_size = private_data_len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002339
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302340 memcpy(start_buff, private_data, private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002341
2342 /* send a syn and goto syn sent state */
2343 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002344 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002345
Faisal Latif6492cdf2008-07-24 20:50:45 -07002346 if (ret) {
2347 /* error in sending the syn free up the cm_node struct */
2348 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302349 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2350 cm_node->rem_addr, cm_node->rem_port, cm_node,
2351 cm_node->cm_id);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002352 rem_ref_cm_node(cm_node->cm_core, cm_node);
2353 cm_node = NULL;
2354 }
2355
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302356 if (cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002357 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302358 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2359 cm_node->rem_addr, cm_node->rem_port, cm_node,
2360 cm_node->cm_id);
2361 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002362
2363 return cm_node;
2364}
2365
2366
2367/**
2368 * mini_cm_accept - accept a connection
2369 * This function is never called
2370 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302371static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002372{
2373 return 0;
2374}
2375
2376
2377/**
2378 * mini_cm_reject - reject and teardown a connection
2379 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302380static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002381{
2382 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002383 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002384 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002385 struct nes_cm_event event;
2386 struct iw_cm_id *cm_id = cm_node->cm_id;
2387 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002388
Faisal Latif6492cdf2008-07-24 20:50:45 -07002389 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302390 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002391
Faisal Latif6492cdf2008-07-24 20:50:45 -07002392 if (cm_node->tcp_cntxt.client)
2393 return ret;
2394 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002395
Faisal Latif9d5ab132009-03-06 15:15:01 -08002396 if (!loopback) {
2397 passive_state = atomic_add_return(1, &cm_node->passive_state);
2398 if (passive_state == NES_SEND_RESET_EVENT) {
2399 cm_node->state = NES_CM_STATE_CLOSED;
2400 rem_ref_cm_node(cm_core, cm_node);
2401 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002402 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2403 rem_ref_cm_node(cm_core, cm_node);
2404 } else {
2405 ret = send_mpa_reject(cm_node);
2406 if (ret) {
2407 cm_node->state = NES_CM_STATE_CLOSED;
2408 err = send_reset(cm_node, NULL);
2409 if (err)
2410 WARN_ON(1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302411 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002412 cm_id->add_ref(cm_id);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302413 }
Faisal Latifc5a7d482009-12-09 15:54:08 -08002414 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002415 }
2416 } else {
2417 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002418 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2419 rem_ref_cm_node(cm_core, cm_node);
2420 rem_ref_cm_node(cm_core, loopback);
2421 } else {
2422 event.cm_node = loopback;
2423 event.cm_info.rem_addr = loopback->rem_addr;
2424 event.cm_info.loc_addr = loopback->loc_addr;
2425 event.cm_info.rem_port = loopback->rem_port;
2426 event.cm_info.loc_port = loopback->loc_port;
2427 event.cm_info.cm_id = loopback->cm_id;
2428 cm_event_mpa_reject(&event);
2429 rem_ref_cm_node(cm_core, cm_node);
2430 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002431
Faisal Latifc5a7d482009-12-09 15:54:08 -08002432 cm_id = loopback->cm_id;
2433 rem_ref_cm_node(cm_core, loopback);
2434 cm_id->rem_ref(cm_id);
2435 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002436 }
2437
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002438 return ret;
2439}
2440
2441
2442/**
2443 * mini_cm_close
2444 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002445static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002446{
2447 int ret = 0;
2448
2449 if (!cm_core || !cm_node)
2450 return -EINVAL;
2451
2452 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002453 case NES_CM_STATE_SYN_RCVD:
2454 case NES_CM_STATE_SYN_SENT:
2455 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2456 case NES_CM_STATE_ESTABLISHED:
2457 case NES_CM_STATE_ACCEPTING:
2458 case NES_CM_STATE_MPAREQ_SENT:
2459 case NES_CM_STATE_MPAREQ_RCVD:
2460 cleanup_retrans_entry(cm_node);
2461 send_reset(cm_node, NULL);
2462 break;
2463 case NES_CM_STATE_CLOSE_WAIT:
2464 cm_node->state = NES_CM_STATE_LAST_ACK;
2465 send_fin(cm_node, NULL);
2466 break;
2467 case NES_CM_STATE_FIN_WAIT1:
2468 case NES_CM_STATE_FIN_WAIT2:
2469 case NES_CM_STATE_LAST_ACK:
2470 case NES_CM_STATE_TIME_WAIT:
2471 case NES_CM_STATE_CLOSING:
2472 ret = -1;
2473 break;
2474 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002475 cleanup_retrans_entry(cm_node);
2476 send_reset(cm_node, NULL);
2477 break;
2478 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002479 case NES_CM_STATE_UNKNOWN:
2480 case NES_CM_STATE_INITED:
2481 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002482 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002483 ret = rem_ref_cm_node(cm_core, cm_node);
2484 break;
2485 case NES_CM_STATE_TSA:
2486 if (cm_node->send_entry)
2487 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302488 "send_entry=%p\n", cm_node->send_entry);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002489 ret = rem_ref_cm_node(cm_core, cm_node);
2490 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002491 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002492 return ret;
2493}
2494
2495
2496/**
2497 * recv_pkt - recv an ETHERNET packet, and process it through CM
2498 * node state machine
2499 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002500static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302501 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002502{
2503 struct nes_cm_node *cm_node = NULL;
2504 struct nes_cm_listener *listener = NULL;
2505 struct iphdr *iph;
2506 struct tcphdr *tcph;
2507 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002508 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002509 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002510
Faisal Latif6492cdf2008-07-24 20:50:45 -07002511 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002512 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302513 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002514 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002515
2516 iph = (struct iphdr *)skb->data;
2517 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002518
2519 nfo.loc_addr = ntohl(iph->daddr);
2520 nfo.loc_port = ntohs(tcph->dest);
2521 nfo.rem_addr = ntohl(iph->saddr);
2522 nfo.rem_port = ntohs(tcph->source);
2523
Harvey Harrison03080e52009-01-10 21:45:42 -08002524 tmp_daddr = cpu_to_be32(iph->daddr);
2525 tmp_saddr = cpu_to_be32(iph->saddr);
2526
Harvey Harrison63779432008-10-31 00:56:00 -07002527 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002528 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002529
Faisal Latif6492cdf2008-07-24 20:50:45 -07002530 do {
2531 cm_node = find_node(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302532 nfo.rem_port, nfo.rem_addr,
2533 nfo.loc_port, nfo.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002534
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002535 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002536 /* Only type of packet accepted are for */
2537 /* the PASSIVE open (syn only) */
2538 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002539 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002540 break;
2541 }
2542 listener = find_listener(cm_core, nfo.loc_addr,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302543 nfo.loc_port,
2544 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002545 if (!listener) {
2546 nfo.cm_id = NULL;
2547 nfo.conn_type = 0;
2548 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2549 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002550 break;
2551 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002552 nfo.cm_id = listener->cm_id;
2553 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002554 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302555 listener);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002556 if (!cm_node) {
2557 nes_debug(NES_DBG_CM, "Unable to allocate "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302558 "node\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07002559 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002560 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002561 dev_kfree_skb_any(skb);
2562 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002563 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002564 if (!tcph->rst && !tcph->fin) {
2565 cm_node->state = NES_CM_STATE_LISTENING;
2566 } else {
2567 cm_packets_dropped++;
2568 rem_ref_cm_node(cm_core, cm_node);
2569 dev_kfree_skb_any(skb);
2570 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002571 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002572 add_ref_cm_node(cm_node);
2573 } else if (cm_node->state == NES_CM_STATE_TSA) {
Faisal Latif0f0bee82011-09-25 20:34:00 -05002574 if (cm_node->nesqp->pau_mode)
2575 nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
2576 else {
2577 rem_ref_cm_node(cm_core, cm_node);
2578 atomic_inc(&cm_accel_dropped_pkts);
2579 dev_kfree_skb_any(skb);
2580 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002581 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002582 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002583 skb_reset_network_header(skb);
2584 skb_set_transport_header(skb, sizeof(*tcph));
2585 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002586 process_packet(cm_node, skb, cm_core);
2587 rem_ref_cm_node(cm_core, cm_node);
2588 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002589 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002590}
2591
2592
2593/**
2594 * nes_cm_alloc_core - allocate a top level instance of a cm core
2595 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002596static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002597{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002598 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002599
2600 /* setup the CM core */
2601 /* alloc top level core control structure */
2602 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2603 if (!cm_core)
2604 return NULL;
2605
2606 INIT_LIST_HEAD(&cm_core->connected_nodes);
2607 init_timer(&cm_core->tcp_timer);
2608 cm_core->tcp_timer.function = nes_cm_timer_tick;
2609
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302610 cm_core->mtu = NES_CM_DEFAULT_MTU;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002611 cm_core->state = NES_CM_STATE_INITED;
2612 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2613
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002614 atomic_set(&cm_core->events_posted, 0);
2615
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002616 cm_core->api = &nes_cm_api;
2617
2618 spin_lock_init(&cm_core->ht_lock);
2619 spin_lock_init(&cm_core->listen_list_lock);
2620
2621 INIT_LIST_HEAD(&cm_core->listen_list.list);
2622
2623 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2624
2625 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2626 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2627 cm_core->post_event = nes_cm_post_event;
2628 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2629 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2630
2631 print_core(cm_core);
2632 return cm_core;
2633}
2634
2635
2636/**
2637 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2638 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002639static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002640{
2641 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2642
2643 if (!cm_core)
2644 return -EINVAL;
2645
2646 barrier();
2647
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302648 if (timer_pending(&cm_core->tcp_timer))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002649 del_timer(&cm_core->tcp_timer);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002650
2651 destroy_workqueue(cm_core->event_wq);
2652 destroy_workqueue(cm_core->disconn_wq);
2653 nes_debug(NES_DBG_CM, "\n");
2654 kfree(cm_core);
2655
2656 return 0;
2657}
2658
2659
2660/**
2661 * mini_cm_get
2662 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002663static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002664{
2665 return cm_core->state;
2666}
2667
2668
2669/**
2670 * mini_cm_set
2671 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002672static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002673{
2674 int ret = 0;
2675
2676 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002677 case NES_CM_SET_PKT_SIZE:
2678 cm_core->mtu = value;
2679 break;
2680 case NES_CM_SET_FREE_PKT_Q_SIZE:
2681 cm_core->free_tx_pkt_max = value;
2682 break;
2683 default:
2684 /* unknown set option */
2685 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002686 }
2687
2688 return ret;
2689}
2690
2691
2692/**
2693 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2694 * successfully exchanged when this is called
2695 */
2696static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2697{
2698 int ret = 0;
2699
2700 if (!nesqp)
2701 return -EINVAL;
2702
2703 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302704 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2705 NES_QPCONTEXT_MISC_DROS);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002706
2707 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2708 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2709
2710 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2711
2712 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2713
2714 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302715 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002716
2717 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302718 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2719 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002720
2721 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302722 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2723 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002724
2725 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2726 nesqp->nesqp_context->ts_recent = 0;
2727 nesqp->nesqp_context->ts_age = 0;
2728 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2729 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2730 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2731 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302732 cm_node->tcp_cntxt.rcv_wscale);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002733 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2734 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2735 nesqp->nesqp_context->srtt = 0;
2736 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2737 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302738 nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002739 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2740 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2741 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2742
2743 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302744 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2745 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2746 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2747 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2748 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2749 le32_to_cpu(nesqp->nesqp_context->misc));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002750 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2751 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2752 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2753
2754 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2755 cm_node->state = NES_CM_STATE_TSA;
2756
2757 return ret;
2758}
2759
2760
2761/**
2762 * nes_cm_disconn
2763 */
2764int nes_cm_disconn(struct nes_qp *nesqp)
2765{
Don Wood873fcdd2009-09-05 20:36:37 -07002766 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002767
Don Wood873fcdd2009-09-05 20:36:37 -07002768 work = kzalloc(sizeof *work, GFP_ATOMIC);
2769 if (!work)
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302770 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002771
Don Wood873fcdd2009-09-05 20:36:37 -07002772 nes_add_ref(&nesqp->ibqp);
2773 work->nesqp = nesqp;
2774 INIT_WORK(&work->work, nes_disconnect_worker);
2775 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002776 return 0;
2777}
2778
2779
2780/**
2781 * nes_disconnect_worker
2782 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002783static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002784{
Don Wood873fcdd2009-09-05 20:36:37 -07002785 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2786 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002787
Don Wood873fcdd2009-09-05 20:36:37 -07002788 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002789 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302790 nesqp->last_aeq, nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002791 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002792 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002793}
2794
2795
2796/**
2797 * nes_cm_disconn_true
2798 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002799static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002800{
2801 unsigned long flags;
2802 int ret = 0;
2803 struct iw_cm_id *cm_id;
2804 struct iw_cm_event cm_event;
2805 struct nes_vnic *nesvnic;
2806 u16 last_ae;
2807 u8 original_hw_tcp_state;
2808 u8 original_ibqp_state;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002809 int disconn_status = 0;
Don Woodb29a4fc2009-09-05 20:36:39 -07002810 int issue_disconn = 0;
2811 int issue_close = 0;
2812 int issue_flush = 0;
2813 u32 flush_q = NES_CQP_FLUSH_RQ;
2814 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002815
2816 if (!nesqp) {
2817 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2818 return -1;
2819 }
2820
2821 spin_lock_irqsave(&nesqp->lock, flags);
2822 cm_id = nesqp->cm_id;
2823 /* make sure we havent already closed this connection */
2824 if (!cm_id) {
2825 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302826 nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002827 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002828 return -1;
2829 }
2830
2831 nesvnic = to_nesvnic(nesqp->ibqp.device);
2832 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2833
2834 original_hw_tcp_state = nesqp->hw_tcp_state;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302835 original_ibqp_state = nesqp->ibqp_state;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002836 last_ae = nesqp->last_aeq;
2837
Don Woodb29a4fc2009-09-05 20:36:39 -07002838 if (nesqp->term_flags) {
2839 issue_disconn = 1;
2840 issue_close = 1;
2841 nesqp->cm_id = NULL;
Tatyana Nikolova196f40c2011-12-05 11:39:12 -06002842 del_timer(&nesqp->terminate_timer);
Don Woodb29a4fc2009-09-05 20:36:39 -07002843 if (nesqp->flush_issued == 0) {
2844 nesqp->flush_issued = 1;
2845 issue_flush = 1;
2846 }
2847 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2848 ((original_ibqp_state == IB_QPS_RTS) &&
2849 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2850 issue_disconn = 1;
2851 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002852 disconn_status = -ECONNRESET;
Don Woodb29a4fc2009-09-05 20:36:39 -07002853 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002854
Don Woodb29a4fc2009-09-05 20:36:39 -07002855 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2856 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2857 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2858 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2859 issue_close = 1;
2860 nesqp->cm_id = NULL;
2861 if (nesqp->flush_issued == 0) {
2862 nesqp->flush_issued = 1;
2863 issue_flush = 1;
2864 }
2865 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002866
Don Woodb29a4fc2009-09-05 20:36:39 -07002867 spin_unlock_irqrestore(&nesqp->lock, flags);
2868
2869 if ((issue_flush) && (nesqp->destroyed == 0)) {
2870 /* Flush the queue(s) */
2871 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2872 flush_q |= NES_CQP_FLUSH_SQ;
2873 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2874
2875 if (nesqp->term_flags) {
2876 ibevent.device = nesqp->ibqp.device;
2877 ibevent.event = nesqp->terminate_eventtype;
2878 ibevent.element.qp = &nesqp->ibqp;
2879 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
2880 }
2881 }
2882
2883 if ((cm_id) && (cm_id->event_handler)) {
2884 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002885 atomic_inc(&cm_disconnects);
2886 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002887 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002888 cm_event.local_addr = cm_id->local_addr;
2889 cm_event.remote_addr = cm_id->remote_addr;
2890 cm_event.private_data = NULL;
2891 cm_event.private_data_len = 0;
2892
Faisal Latif6492cdf2008-07-24 20:50:45 -07002893 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302894 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2895 "cm_id = %p, refcount = %u.\n",
2896 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2897 nesqp->hwqp.sq_tail, cm_id,
2898 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002899
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002900 ret = cm_id->event_handler(cm_id, &cm_event);
2901 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002902 nes_debug(NES_DBG_CM, "OFA CM event_handler "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302903 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002904 }
2905
Don Woodb29a4fc2009-09-05 20:36:39 -07002906 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002907 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002908 nes_disconnect(nesqp, 1);
2909
2910 cm_id->provider_data = nesqp;
2911 /* Send up the close complete event */
2912 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002913 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002914 cm_event.provider_data = cm_id->provider_data;
2915 cm_event.local_addr = cm_id->local_addr;
2916 cm_event.remote_addr = cm_id->remote_addr;
2917 cm_event.private_data = NULL;
2918 cm_event.private_data_len = 0;
2919
2920 ret = cm_id->event_handler(cm_id, &cm_event);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302921 if (ret)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002922 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002923
2924 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002925 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002926 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002927
2928 return 0;
2929}
2930
2931
2932/**
2933 * nes_disconnect
2934 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002935static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002936{
2937 int ret = 0;
2938 struct nes_vnic *nesvnic;
2939 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002940 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002941
2942 nesvnic = to_nesvnic(nesqp->ibqp.device);
2943 if (!nesvnic)
2944 return -EINVAL;
2945
2946 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002947 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002948
2949 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00002950 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002951
2952 if (nesqp->active_conn) {
2953
2954 /* indicate this connection is NOT active */
2955 nesqp->active_conn = 0;
2956 } else {
2957 /* Need to free the Last Streaming Mode Message */
2958 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002959 if (nesqp->lsmm_mr)
2960 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002961 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302962 nesqp->private_data_len + nesqp->ietf_frame_size,
2963 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002964 }
2965 }
2966
2967 /* close the CM node down if it is still active */
2968 if (nesqp->cm_node) {
2969 nes_debug(NES_DBG_CM, "Call close API\n");
2970
2971 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002972 }
2973
2974 return ret;
2975}
2976
2977
2978/**
2979 * nes_accept
2980 */
2981int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2982{
2983 u64 u64temp;
2984 struct ib_qp *ibqp;
2985 struct nes_qp *nesqp;
2986 struct nes_vnic *nesvnic;
2987 struct nes_device *nesdev;
2988 struct nes_cm_node *cm_node;
2989 struct nes_adapter *adapter;
2990 struct ib_qp_attr attr;
2991 struct iw_cm_event cm_event;
2992 struct nes_hw_qp_wqe *wqe;
2993 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002994 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002995 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002996 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002997 struct nes_ib_device *nesibdev;
2998 struct ib_mr *ibmr = NULL;
2999 struct ib_phys_buf ibphysbuf;
3000 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07003001 u64 tagged_offset;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303002 u8 mpa_frame_offset = 0;
3003 struct ietf_mpa_v2 *mpa_v2_frame;
3004 u8 start_addr = 0;
3005 u8 *start_ptr = &start_addr;
3006 u8 **start_buff = &start_ptr;
3007 u16 buff_len = 0;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003008
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003009 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3010 if (!ibqp)
3011 return -EINVAL;
3012
3013 /* get all our handles */
3014 nesqp = to_nesqp(ibqp);
3015 nesvnic = to_nesvnic(nesqp->ibqp.device);
3016 nesdev = nesvnic->nesdev;
3017 adapter = nesdev->nesadapter;
3018
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003019 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003020 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
3021 "%s\n", cm_node, nesvnic, nesvnic->netdev,
3022 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003023
Faisal Latifc5a7d482009-12-09 15:54:08 -08003024 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
3025 if (cm_node->loopbackpartner)
3026 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
3027 rem_ref_cm_node(cm_node->cm_core, cm_node);
3028 return -EINVAL;
3029 }
3030
Faisal Latifdae58722010-08-14 21:04:56 +00003031 passive_state = atomic_add_return(1, &cm_node->passive_state);
3032 if (passive_state == NES_SEND_RESET_EVENT) {
3033 rem_ref_cm_node(cm_node->cm_core, cm_node);
3034 return -ECONNRESET;
3035 }
3036
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003037 /* associate the node with the QP */
3038 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003039 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003040
Faisal Latif6492cdf2008-07-24 20:50:45 -07003041 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
3042 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003043 atomic_inc(&cm_accepts);
3044
3045 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00003046 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003047
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303048 nesqp->ietf_frame_size = sizeof(struct ietf_mpa_v2);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003049 /* allocate the ietf frame and space for private data */
3050 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303051 nesqp->ietf_frame_size + conn_param->private_data_len,
3052 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003053
Faisal Latif6492cdf2008-07-24 20:50:45 -07003054 if (!nesqp->ietf_frame) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303055 nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07003056 return -ENOMEM;
3057 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303058 mpa_v2_frame = (struct ietf_mpa_v2 *)nesqp->ietf_frame;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003059
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303060 if (cm_node->mpa_frame_rev == IETF_MPA_V1)
3061 mpa_frame_offset = 4;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003062
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303063 memcpy(mpa_v2_frame->priv_data, conn_param->private_data,
3064 conn_param->private_data_len);
3065
3066 cm_build_mpa_frame(cm_node, start_buff, &buff_len, nesqp->ietf_frame, MPA_KEY_REPLY);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003067 nesqp->private_data_len = conn_param->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003068
Faisal Latif6492cdf2008-07-24 20:50:45 -07003069 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
3070 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003071
Faisal Latif6492cdf2008-07-24 20:50:45 -07003072 if (cm_id->remote_addr.sin_addr.s_addr !=
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303073 cm_id->local_addr.sin_addr.s_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003074 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003075 nesibdev = nesvnic->nesibdev;
3076 nespd = nesqp->nespd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303077 ibphysbuf.addr = nesqp->ietf_frame_pbase + mpa_frame_offset;
3078 ibphysbuf.size = buff_len;
3079 tagged_offset = (u64)(unsigned long)*start_buff;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003080 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303081 &ibphysbuf, 1,
3082 IB_ACCESS_LOCAL_WRITE,
3083 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003084 if (!ibmr) {
3085 nes_debug(NES_DBG_CM, "Unable to register memory region"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303086 "for lSMM for cm_node = %p \n",
3087 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07003088 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303089 nesqp->private_data_len + nesqp->ietf_frame_size,
3090 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003091 return -ENOMEM;
3092 }
3093
3094 ibmr->pd = &nespd->ibpd;
3095 ibmr->device = nespd->ibpd.device;
3096 nesqp->lsmm_mr = ibmr;
3097
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303098 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003099 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303100 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
3101 u64temp);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003102 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3103 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303104 NES_IWARP_SQ_WQE_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003105 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303106 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003107 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303108 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3109 (u64)(unsigned long)(*start_buff));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003110 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303111 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003112 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08003113 if (nesqp->sq_kmapped) {
3114 nesqp->sq_kmapped = 0;
3115 kunmap(nesqp->page);
3116 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003117
Faisal Latif6492cdf2008-07-24 20:50:45 -07003118 nesqp->nesqp_context->ird_ord_sizes |=
3119 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303120 NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003121 } else {
3122 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08003123 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003124 }
3125 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003126
3127
3128 /* Cache the cm_id in the qp */
3129 nesqp->cm_id = cm_id;
3130 cm_node->cm_id = cm_id;
3131
3132 /* nesqp->cm_node = (void *)cm_id->provider_data; */
3133 cm_id->provider_data = nesqp;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303134 nesqp->active_conn = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003135
Faisal Latif6492cdf2008-07-24 20:50:45 -07003136 if (cm_node->state == NES_CM_STATE_TSA)
3137 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303138 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003139
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003140 nes_cm_init_tsa_conn(nesqp, cm_node);
3141
Faisal Latif6492cdf2008-07-24 20:50:45 -07003142 nesqp->nesqp_context->tcpPorts[0] =
3143 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3144 nesqp->nesqp_context->tcpPorts[1] =
3145 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3146
3147 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3148 nesqp->nesqp_context->ip0 =
3149 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3150 else
3151 nesqp->nesqp_context->ip0 =
3152 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003153
3154 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303155 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3156 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003157
Faisal Latif6492cdf2008-07-24 20:50:45 -07003158 nesqp->nesqp_context->arp_index_vlan |=
3159 cpu_to_le32(nes_arp_table(nesdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303160 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
3161 NES_ARP_RESOLVE) << 16);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003162
3163 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003164 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003165
3166 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3167
3168 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003169 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
3170 nesqp->nesqp_context->ird_ord_sizes |=
3171 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003172
3173 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003174 nes_quad.DstIpAdrIndex =
3175 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3176 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3177 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3178 else
3179 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
3180 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3181 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003182
3183 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003184 crc_value = get_crc_value(&nes_quad);
3185 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003186 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303187 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003188
3189 nesqp->hte_index &= adapter->hte_index_mask;
3190 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3191
3192 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3193
Faisal Latif6492cdf2008-07-24 20:50:45 -07003194 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303195 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
3196 "private data length=%u.\n", nesqp->hwqp.qp_id,
3197 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3198 ntohs(cm_id->remote_addr.sin_port),
3199 ntohl(cm_id->local_addr.sin_addr.s_addr),
3200 ntohs(cm_id->local_addr.sin_port),
3201 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
3202 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
3203 buff_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003204
Coly Li73ac36e2009-01-07 18:09:16 -08003205 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003206 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07003207 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003208
3209 cm_event.event = IW_CM_EVENT_ESTABLISHED;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003210 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003211 cm_event.provider_data = (void *)nesqp;
3212 cm_event.local_addr = cm_id->local_addr;
3213 cm_event.remote_addr = cm_id->remote_addr;
3214 cm_event.private_data = NULL;
3215 cm_event.private_data_len = 0;
3216 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003217 attr.qp_state = IB_QPS_RTS;
3218 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003219 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003220 cm_node->loopbackpartner->mpa_frame_size =
3221 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003222 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003223 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303224 conn_param->private_data, conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003225 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
3226 }
3227 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003228 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303229 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003230
3231 return 0;
3232}
3233
3234
3235/**
3236 * nes_reject
3237 */
3238int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3239{
3240 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003241 struct nes_cm_node *loopback;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003242 struct nes_cm_core *cm_core;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303243 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003244
3245 atomic_inc(&cm_rejects);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303246 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003247 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003248 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003249 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003250
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303251 if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003252 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003253
Faisal Latif9d5ab132009-03-06 15:15:01 -08003254 if (loopback) {
3255 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3256 loopback->mpa_frame.priv_data_len = pdata_len;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303257 loopback->mpa_frame_size = pdata_len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003258 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303259 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
3260 cm_node->mpa_frame_size = pdata_len;
3261 memcpy(start_buff, pdata, pdata_len);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003262 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303263 return cm_core->api->reject(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003264}
3265
3266
3267/**
3268 * nes_connect
3269 * setup and launch cm connect node
3270 */
3271int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3272{
3273 struct ib_qp *ibqp;
3274 struct nes_qp *nesqp;
3275 struct nes_vnic *nesvnic;
3276 struct nes_device *nesdev;
3277 struct nes_cm_node *cm_node;
3278 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003279 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003280
3281 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3282 if (!ibqp)
3283 return -EINVAL;
3284 nesqp = to_nesqp(ibqp);
3285 if (!nesqp)
3286 return -EINVAL;
3287 nesvnic = to_nesvnic(nesqp->ibqp.device);
3288 if (!nesvnic)
3289 return -EINVAL;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303290 nesdev = nesvnic->nesdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003291 if (!nesdev)
3292 return -EINVAL;
3293
Faisal Latifc5a7d482009-12-09 15:54:08 -08003294 if (!(cm_id->local_addr.sin_port) || !(cm_id->remote_addr.sin_port))
3295 return -EINVAL;
3296
Faisal Latif6492cdf2008-07-24 20:50:45 -07003297 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303298 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3299 ntohl(nesvnic->local_ipaddr),
3300 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3301 ntohs(cm_id->remote_addr.sin_port),
3302 ntohl(cm_id->local_addr.sin_addr.s_addr),
3303 ntohs(cm_id->local_addr.sin_port));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003304
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003305 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003306 nesqp->active_conn = 1;
3307
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003308 /* cache the cm_id in the qp */
3309 nesqp->cm_id = cm_id;
3310
3311 cm_id->provider_data = nesqp;
3312
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003313 nesqp->private_data_len = conn_param->private_data_len;
3314 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
3315 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003316 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303317 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003318
Faisal Latif6492cdf2008-07-24 20:50:45 -07003319 if (cm_id->local_addr.sin_addr.s_addr !=
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303320 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003321 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303322 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003323 apbvt_set = 1;
3324 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003325
3326 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003327 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3328 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3329 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3330 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003331 cm_info.cm_id = cm_id;
3332 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3333
3334 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003335
3336 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003337 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303338 conn_param->private_data_len, (void *)conn_param->private_data,
3339 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003340 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003341 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003342 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303343 PCI_FUNC(nesdev->pcidev->devfn),
3344 NES_MANAGE_APBVT_DEL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003345
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003346 cm_id->rem_ref(cm_id);
3347 return -ENOMEM;
3348 }
3349
Faisal Latif53094c32009-04-27 13:37:34 -07003350 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003351 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003352 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003353 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003354
3355 return 0;
3356}
3357
3358
3359/**
3360 * nes_create_listen
3361 */
3362int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3363{
3364 struct nes_vnic *nesvnic;
3365 struct nes_cm_listener *cm_node;
3366 struct nes_cm_info cm_info;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003367 int err;
3368
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003369 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3370 cm_id, ntohs(cm_id->local_addr.sin_port));
3371
3372 nesvnic = to_nesvnic(cm_id->device);
3373 if (!nesvnic)
3374 return -EINVAL;
Roland Dreier69d51022010-08-04 14:25:40 -07003375
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003376 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3377 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3378
3379 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3380 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3381
3382 /* setup listen params in our api call struct */
3383 cm_info.loc_addr = nesvnic->local_ipaddr;
3384 cm_info.loc_port = cm_id->local_addr.sin_port;
3385 cm_info.backlog = backlog;
3386 cm_info.cm_id = cm_id;
3387
3388 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3389
3390
3391 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3392 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003393 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303394 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003395 return -ENOMEM;
3396 }
3397
3398 cm_id->provider_data = cm_node;
3399
3400 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003401 err = nes_manage_apbvt(nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303402 ntohs(cm_id->local_addr.sin_port),
3403 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3404 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003405 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003406 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303407 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003408 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3409 return err;
3410 }
Faisal Latif6e10d2e2010-02-12 19:55:03 +00003411 atomic_inc(&cm_listens_created);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003412 }
3413
3414 cm_id->add_ref(cm_id);
3415 cm_id->provider_data = (void *)cm_node;
3416
3417
3418 return 0;
3419}
3420
3421
3422/**
3423 * nes_destroy_listen
3424 */
3425int nes_destroy_listen(struct iw_cm_id *cm_id)
3426{
3427 if (cm_id->provider_data)
3428 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3429 else
3430 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3431
3432 cm_id->rem_ref(cm_id);
3433
3434 return 0;
3435}
3436
3437
3438/**
3439 * nes_cm_recv
3440 */
3441int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3442{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003443 int rc = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303444
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003445 cm_packets_received++;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303446 if ((g_cm_core) && (g_cm_core->api))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003447 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303448 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003449 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303450 " cm is not setup properly.\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003451
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003452 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003453}
3454
3455
3456/**
3457 * nes_cm_start
3458 * Start and init a cm core module
3459 */
3460int nes_cm_start(void)
3461{
3462 nes_debug(NES_DBG_CM, "\n");
3463 /* create the primary CM core, pass this handle to subsequent core inits */
3464 g_cm_core = nes_cm_alloc_core();
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303465 if (g_cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003466 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303467 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003468 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003469}
3470
3471
3472/**
3473 * nes_cm_stop
3474 * stop and dealloc all cm core instances
3475 */
3476int nes_cm_stop(void)
3477{
3478 g_cm_core->api->destroy_cm_core(g_cm_core);
3479 return 0;
3480}
3481
3482
3483/**
3484 * cm_event_connected
3485 * handle a connected event, setup QPs and HW
3486 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003487static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003488{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003489 struct nes_qp *nesqp;
3490 struct nes_vnic *nesvnic;
3491 struct nes_device *nesdev;
3492 struct nes_cm_node *cm_node;
3493 struct nes_adapter *nesadapter;
3494 struct ib_qp_attr attr;
3495 struct iw_cm_id *cm_id;
3496 struct iw_cm_event cm_event;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003497 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003498 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003499 int ret;
3500
3501 /* get all our handles */
3502 cm_node = event->cm_node;
3503 cm_id = cm_node->cm_id;
3504 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3505 nesqp = (struct nes_qp *)cm_id->provider_data;
3506 nesvnic = to_nesvnic(nesqp->ibqp.device);
3507 nesdev = nesvnic->nesdev;
3508 nesadapter = nesdev->nesadapter;
3509
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303510 if (nesqp->destroyed)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003511 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003512 atomic_inc(&cm_connecteds);
3513 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303514 " local port 0x%04X. jiffies = %lu.\n",
3515 nesqp->hwqp.qp_id,
3516 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3517 ntohs(cm_id->remote_addr.sin_port),
3518 ntohs(cm_id->local_addr.sin_port),
3519 jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003520
3521 nes_cm_init_tsa_conn(nesqp, cm_node);
3522
3523 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003524 nesqp->nesqp_context->tcpPorts[0] =
3525 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3526 nesqp->nesqp_context->tcpPorts[1] =
3527 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3528 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3529 nesqp->nesqp_context->ip0 =
3530 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3531 else
3532 nesqp->nesqp_context->ip0 =
3533 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003534
3535 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003536 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3537 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003538 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003539 nes_arp_table(nesdev,
3540 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003541 NULL, NES_ARP_RESOLVE) << 16);
3542 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3543 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3544 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3545 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003546 cpu_to_le32((u32)1 <<
3547 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003548
3549 /* Adjust tail for not having a LSMM */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303550 /*nesqp->hwqp.sq_tail = 1;*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003551
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303552 build_rdma0_msg(cm_node, &nesqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003553
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303554 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3555 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003556
3557 memset(&nes_quad, 0, sizeof(nes_quad));
3558
Faisal Latif6492cdf2008-07-24 20:50:45 -07003559 nes_quad.DstIpAdrIndex =
3560 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3561 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3562 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3563 else
3564 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003565 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3566 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3567
3568 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003569 crc_value = get_crc_value(&nes_quad);
3570 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003571 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303572 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003573
3574 nesqp->hte_index &= nesadapter->hte_index_mask;
3575 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3576
3577 nesqp->ietf_frame = &cm_node->mpa_frame;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303578 nesqp->private_data_len = (u8)cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003579 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3580
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003581 /* notify OF layer we successfully created the requested connection */
3582 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003583 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003584 cm_event.provider_data = cm_id->provider_data;
3585 cm_event.local_addr.sin_family = AF_INET;
3586 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3587 cm_event.remote_addr = cm_id->remote_addr;
3588
Faisal Latif6492cdf2008-07-24 20:50:45 -07003589 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303590 cm_event.private_data_len = (u8)event->cm_node->mpa_frame_size;
3591 cm_event.ird = cm_node->ird_size;
3592 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003593
3594 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3595 ret = cm_id->event_handler(cm_id, &cm_event);
3596 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3597
3598 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003599 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303600 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003601 attr.qp_state = IB_QPS_RTS;
3602 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003603
Faisal Latif6492cdf2008-07-24 20:50:45 -07003604 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303605 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003606
3607 return;
3608}
3609
3610
3611/**
3612 * cm_event_connect_error
3613 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003614static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003615{
3616 struct nes_qp *nesqp;
3617 struct iw_cm_id *cm_id;
3618 struct iw_cm_event cm_event;
3619 /* struct nes_cm_info cm_info; */
3620 int ret;
3621
3622 if (!event->cm_node)
3623 return;
3624
3625 cm_id = event->cm_node->cm_id;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303626 if (!cm_id)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003627 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003628
3629 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3630 nesqp = cm_id->provider_data;
3631
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303632 if (!nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003633 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003634
3635 /* notify OF layer about this connection error event */
3636 /* cm_id->rem_ref(cm_id); */
3637 nesqp->cm_id = NULL;
3638 cm_id->provider_data = NULL;
3639 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003640 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003641 cm_event.provider_data = cm_id->provider_data;
3642 cm_event.local_addr = cm_id->local_addr;
3643 cm_event.remote_addr = cm_id->remote_addr;
3644 cm_event.private_data = NULL;
3645 cm_event.private_data_len = 0;
3646
Faisal Latif6492cdf2008-07-24 20:50:45 -07003647 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303648 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3649 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003650
3651 ret = cm_id->event_handler(cm_id, &cm_event);
3652 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3653 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003654 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303655 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003656 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003657
Faisal Latif6492cdf2008-07-24 20:50:45 -07003658 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003659 return;
3660}
3661
3662
3663/**
3664 * cm_event_reset
3665 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003666static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003667{
3668 struct nes_qp *nesqp;
3669 struct iw_cm_id *cm_id;
3670 struct iw_cm_event cm_event;
3671 /* struct nes_cm_info cm_info; */
3672 int ret;
3673
3674 if (!event->cm_node)
3675 return;
3676
3677 if (!event->cm_node->cm_id)
3678 return;
3679
3680 cm_id = event->cm_node->cm_id;
3681
3682 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3683 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003684 if (!nesqp)
3685 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003686
3687 nesqp->cm_id = NULL;
3688 /* cm_id->provider_data = NULL; */
3689 cm_event.event = IW_CM_EVENT_DISCONNECT;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003690 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003691 cm_event.provider_data = cm_id->provider_data;
3692 cm_event.local_addr = cm_id->local_addr;
3693 cm_event.remote_addr = cm_id->remote_addr;
3694 cm_event.private_data = NULL;
3695 cm_event.private_data_len = 0;
3696
Faisal Latif183ecfa2008-11-21 20:50:46 -06003697 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003698 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003699 atomic_inc(&cm_closes);
3700 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003701 cm_event.status = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003702 cm_event.provider_data = cm_id->provider_data;
3703 cm_event.local_addr = cm_id->local_addr;
3704 cm_event.remote_addr = cm_id->remote_addr;
3705 cm_event.private_data = NULL;
3706 cm_event.private_data_len = 0;
3707 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3708 ret = cm_id->event_handler(cm_id, &cm_event);
3709
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003710 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3711
3712
3713 /* notify OF layer about this connection error event */
3714 cm_id->rem_ref(cm_id);
3715
3716 return;
3717}
3718
3719
3720/**
3721 * cm_event_mpa_req
3722 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003723static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003724{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303725 struct iw_cm_id *cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003726 struct iw_cm_event cm_event;
3727 int ret;
3728 struct nes_cm_node *cm_node;
3729
3730 cm_node = event->cm_node;
3731 if (!cm_node)
3732 return;
3733 cm_id = cm_node->cm_id;
3734
3735 atomic_inc(&cm_connect_reqs);
3736 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303737 cm_node, cm_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003738
3739 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003740 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003741 cm_event.provider_data = (void *)cm_node;
3742
3743 cm_event.local_addr.sin_family = AF_INET;
3744 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3745 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3746
3747 cm_event.remote_addr.sin_family = AF_INET;
3748 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3749 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003750 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303751 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
3752 cm_event.ird = cm_node->ird_size;
3753 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003754
3755 ret = cm_id->event_handler(cm_id, &cm_event);
3756 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003757 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303758 __func__, __LINE__, ret);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003759 return;
3760}
3761
3762
3763static void cm_event_mpa_reject(struct nes_cm_event *event)
3764{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303765 struct iw_cm_id *cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003766 struct iw_cm_event cm_event;
3767 struct nes_cm_node *cm_node;
3768 int ret;
3769
3770 cm_node = event->cm_node;
3771 if (!cm_node)
3772 return;
3773 cm_id = cm_node->cm_id;
3774
3775 atomic_inc(&cm_connect_reqs);
3776 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303777 cm_node, cm_id, jiffies);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003778
3779 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3780 cm_event.status = -ECONNREFUSED;
3781 cm_event.provider_data = cm_id->provider_data;
3782
3783 cm_event.local_addr.sin_family = AF_INET;
3784 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3785 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3786
3787 cm_event.remote_addr.sin_family = AF_INET;
3788 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3789 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3790
3791 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303792 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003793
3794 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303795 "remove_addr=%08x\n",
3796 cm_event.local_addr.sin_addr.s_addr,
3797 cm_event.remote_addr.sin_addr.s_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003798
3799 ret = cm_id->event_handler(cm_id, &cm_event);
3800 if (ret)
3801 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303802 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003803
3804 return;
3805}
3806
3807
3808static void nes_cm_event_handler(struct work_struct *);
3809
3810/**
3811 * nes_cm_post_event
3812 * post an event to the cm event handler
3813 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003814static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003815{
3816 atomic_inc(&event->cm_node->cm_core->events_posted);
3817 add_ref_cm_node(event->cm_node);
3818 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3819 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003820 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303821 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003822
3823 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3824
3825 nes_debug(NES_DBG_CM, "Exit\n");
3826 return 0;
3827}
3828
3829
3830/**
3831 * nes_cm_event_handler
3832 * worker function to handle cm events
3833 * will free instance of nes_cm_event
3834 */
3835static void nes_cm_event_handler(struct work_struct *work)
3836{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003837 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303838 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003839 struct nes_cm_core *cm_core;
3840
Faisal Latif6492cdf2008-07-24 20:50:45 -07003841 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003842 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003843
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003844 cm_core = event->cm_node->cm_core;
3845 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303846 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003847
3848 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003849 case NES_CM_EVENT_MPA_REQ:
3850 cm_event_mpa_req(event);
3851 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303852 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003853 break;
3854 case NES_CM_EVENT_RESET:
3855 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303856 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003857 cm_event_reset(event);
3858 break;
3859 case NES_CM_EVENT_CONNECTED:
3860 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303861 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003862 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003863 cm_event_connected(event);
3864 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3865 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003866 case NES_CM_EVENT_MPA_REJECT:
3867 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303868 (event->cm_node->state == NES_CM_STATE_TSA))
Faisal Latif9d5ab132009-03-06 15:15:01 -08003869 break;
3870 cm_event_mpa_reject(event);
3871 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3872 break;
3873
Faisal Latif6492cdf2008-07-24 20:50:45 -07003874 case NES_CM_EVENT_ABORTED:
3875 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303876 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003877 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003878 cm_event_connect_error(event);
3879 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3880 break;
3881 case NES_CM_EVENT_DROPPED_PKT:
3882 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3883 break;
3884 default:
3885 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3886 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003887 }
3888
3889 atomic_dec(&cm_core->events_posted);
3890 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3891 rem_ref_cm_node(cm_core, event->cm_node);
3892 kfree(event);
3893
3894 return;
3895}