blob: 020e95c4c4b9b08da6119b13bbe6b8b6a2710b88 [file] [log] [blame]
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001/*
Tatyana Nikolovac5488c52011-11-28 14:22:29 -06002 * Copyright (c) 2006 - 2011 Intel Corporation. All rights reserved.
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34
35#define TCPOPT_TIMESTAMP 8
36
Arun Sharma600634972011-07-26 16:09:06 -070037#include <linux/atomic.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080038#include <linux/skbuff.h>
39#include <linux/ip.h>
40#include <linux/tcp.h>
41#include <linux/init.h>
42#include <linux/if_arp.h>
Chien Tungf2b2b592008-03-20 19:55:30 -050043#include <linux/if_vlan.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080044#include <linux/notifier.h>
45#include <linux/net.h>
46#include <linux/types.h>
47#include <linux/timer.h>
48#include <linux/time.h>
49#include <linux/delay.h>
50#include <linux/etherdevice.h>
51#include <linux/netdevice.h>
52#include <linux/random.h>
53#include <linux/list.h>
54#include <linux/threads.h>
Faisal Latifd2fa9b262009-12-09 15:54:28 -080055#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Bob Sharp7191a0a2008-10-03 12:21:19 -070057#include <net/arp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080058#include <net/neighbour.h>
59#include <net/route.h>
60#include <net/ip_fib.h>
Faisal Latifc11470f2009-04-27 13:38:31 -070061#include <net/tcp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080062
63#include "nes.h"
64
65u32 cm_packets_sent;
66u32 cm_packets_bounced;
67u32 cm_packets_dropped;
68u32 cm_packets_retrans;
69u32 cm_packets_created;
70u32 cm_packets_received;
Faisal Latif6e10d2e2010-02-12 19:55:03 +000071atomic_t cm_listens_created;
72atomic_t cm_listens_destroyed;
Glenn Streiff3c2d7742008-02-04 20:20:45 -080073u32 cm_backlog_drops;
74atomic_t cm_loopbacks;
75atomic_t cm_nodes_created;
76atomic_t cm_nodes_destroyed;
77atomic_t cm_accel_dropped_pkts;
78atomic_t cm_resets_recvd;
79
Tatyana Nikolova615eb712011-09-25 20:17:46 +053080static inline int mini_cm_accelerated(struct nes_cm_core *, struct nes_cm_node *);
81static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *, struct nes_vnic *, struct nes_cm_info *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080082static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
Tatyana Nikolova615eb712011-09-25 20:17:46 +053083static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *, struct nes_vnic *, u16, void *, struct nes_cm_info *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070084static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Tatyana Nikolova615eb712011-09-25 20:17:46 +053085static int mini_cm_accept(struct nes_cm_core *, struct nes_cm_node *);
86static int mini_cm_reject(struct nes_cm_core *, struct nes_cm_node *);
87static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *, struct sk_buff *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070088static int mini_cm_dealloc_core(struct nes_cm_core *);
89static int mini_cm_get(struct nes_cm_core *);
90static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070091
Tatyana Nikolova615eb712011-09-25 20:17:46 +053092static void form_cm_frame(struct sk_buff *, struct nes_cm_node *, void *, u32, void *, u32, u8);
Faisal Latif6492cdf2008-07-24 20:50:45 -070093static int add_ref_cm_node(struct nes_cm_node *);
94static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
95
Roland Dreier1a855fb2008-04-16 21:01:09 -070096static int nes_cm_disconn_true(struct nes_qp *);
97static int nes_cm_post_event(struct nes_cm_event *event);
98static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
99static void nes_disconnect_worker(struct work_struct *work);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700100
101static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800102static int send_mpa_reject(struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700103static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
104static int send_reset(struct nes_cm_node *, struct sk_buff *);
105static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fb2008-04-16 21:01:09 -0700106static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530107static void process_packet(struct nes_cm_node *, struct sk_buff *, struct nes_cm_core *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700108
109static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
110static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
111static void cleanup_retrans_entry(struct nes_cm_node *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800112static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700113static void free_retrans_entry(struct nes_cm_node *cm_node);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530114static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph, struct sk_buff *skb, int optionsize, int passive);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700115
116/* CM event handler functions */
117static void cm_event_connected(struct nes_cm_event *);
118static void cm_event_connect_error(struct nes_cm_event *);
119static void cm_event_reset(struct nes_cm_event *);
120static void cm_event_mpa_req(struct nes_cm_event *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800121static void cm_event_mpa_reject(struct nes_cm_event *);
122static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700123
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530124/* MPA build functions */
125static int cm_build_mpa_frame(struct nes_cm_node *, u8 **, u16 *, u8 *, u8);
126static void build_mpa_v2(struct nes_cm_node *, void *, u8);
127static void build_mpa_v1(struct nes_cm_node *, void *, u8);
128static void build_rdma0_msg(struct nes_cm_node *, struct nes_qp **);
129
Faisal Latif6492cdf2008-07-24 20:50:45 -0700130static void print_core(struct nes_cm_core *core);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800131
132/* External CM API Interface */
133/* instance of function pointers for client API */
134/* set address of this instance to cm_core->cm_ops at cm_core alloc */
135static struct nes_cm_ops nes_cm_api = {
136 mini_cm_accelerated,
137 mini_cm_listen,
138 mini_cm_del_listen,
139 mini_cm_connect,
140 mini_cm_close,
141 mini_cm_accept,
142 mini_cm_reject,
143 mini_cm_recv_pkt,
144 mini_cm_dealloc_core,
145 mini_cm_get,
146 mini_cm_set
147};
148
Roland Dreier1a855fb2008-04-16 21:01:09 -0700149static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800150
151atomic_t cm_connects;
152atomic_t cm_accepts;
153atomic_t cm_disconnects;
154atomic_t cm_closes;
155atomic_t cm_connecteds;
156atomic_t cm_connect_reqs;
157atomic_t cm_rejects;
158
Faisal Latif0f0bee82011-09-25 20:34:00 -0500159int nes_add_ref_cm_node(struct nes_cm_node *cm_node)
160{
161 return add_ref_cm_node(cm_node);
162}
163
164int nes_rem_ref_cm_node(struct nes_cm_node *cm_node)
165{
166 return rem_ref_cm_node(cm_node->cm_core, cm_node);
167}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800168
169/**
170 * create_event
171 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530172static struct nes_cm_event *create_event(struct nes_cm_node * cm_node,
173 enum nes_cm_event_type type)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800174{
175 struct nes_cm_event *event;
176
177 if (!cm_node->cm_id)
178 return NULL;
179
180 /* allocate an empty event */
181 event = kzalloc(sizeof(*event), GFP_ATOMIC);
182
183 if (!event)
184 return NULL;
185
186 event->type = type;
187 event->cm_node = cm_node;
188 event->cm_info.rem_addr = cm_node->rem_addr;
189 event->cm_info.loc_addr = cm_node->loc_addr;
190 event->cm_info.rem_port = cm_node->rem_port;
191 event->cm_info.loc_port = cm_node->loc_port;
192 event->cm_info.cm_id = cm_node->cm_id;
193
Faisal Latif6492cdf2008-07-24 20:50:45 -0700194 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530195 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
196 cm_node, event, type, event->cm_info.loc_addr,
197 event->cm_info.loc_port, event->cm_info.rem_addr,
198 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800199
200 nes_cm_post_event(event);
201 return event;
202}
203
204
205/**
206 * send_mpa_request
207 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700208static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800209{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530210 u8 start_addr = 0;
211 u8 *start_ptr = &start_addr;
212 u8 **start_buff = &start_ptr;
213 u16 buff_len = 0;
214
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800215 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700216 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800217 return -1;
218 }
219
220 /* send an MPA Request frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530221 cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REQUEST);
222 form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800223
Faisal Latif9d5ab132009-03-06 15:15:01 -0800224 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
225}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800226
Faisal Latif9d5ab132009-03-06 15:15:01 -0800227
228
229static int send_mpa_reject(struct nes_cm_node *cm_node)
230{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530231 struct sk_buff *skb = NULL;
232 u8 start_addr = 0;
233 u8 *start_ptr = &start_addr;
234 u8 **start_buff = &start_ptr;
235 u16 buff_len = 0;
Tatyana Nikolova81f99dc2012-01-17 10:17:30 -0600236 struct ietf_mpa_v1 *mpa_frame;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800237
238 skb = dev_alloc_skb(MAX_CM_BUFFER);
239 if (!skb) {
240 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
241 return -ENOMEM;
242 }
243
244 /* send an MPA reject frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530245 cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REPLY);
Tatyana Nikolova81f99dc2012-01-17 10:17:30 -0600246 mpa_frame = (struct ietf_mpa_v1 *)*start_buff;
247 mpa_frame->flags |= IETF_MPA_FLAGS_REJECT;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530248 form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK | SET_FIN);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800249
250 cm_node->state = NES_CM_STATE_FIN_WAIT1;
251 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800252}
253
254
255/**
256 * recv_mpa - process a received TCP pkt, we are expecting an
257 * IETF MPA frame
258 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800259static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530260 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800261{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530262 struct ietf_mpa_v1 *mpa_frame;
263 struct ietf_mpa_v2 *mpa_v2_frame;
264 struct ietf_rtr_msg *rtr_msg;
265 int mpa_hdr_len;
266 int priv_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800267
Faisal Latif9d5ab132009-03-06 15:15:01 -0800268 *type = NES_MPA_REQUEST_ACCEPT;
269
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800270 /* assume req frame is in tcp data payload */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530271 if (len < sizeof(struct ietf_mpa_v1)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800272 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800273 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800274 }
275
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530276 /* points to the beginning of the frame, which could be MPA V1 or V2 */
277 mpa_frame = (struct ietf_mpa_v1 *)buffer;
278 mpa_hdr_len = sizeof(struct ietf_mpa_v1);
279 priv_data_len = ntohs(mpa_frame->priv_data_len);
280
Faisal Latif1cf078c2009-12-09 15:53:54 -0800281 /* make sure mpa private data len is less than 512 bytes */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530282 if (priv_data_len > IETF_MAX_PRIV_DATA_LEN) {
Faisal Latif1cf078c2009-12-09 15:53:54 -0800283 nes_debug(NES_DBG_CM, "The received Length of Private"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530284 " Data field exceeds 512 octets\n");
Faisal Latif1cf078c2009-12-09 15:53:54 -0800285 return -EINVAL;
286 }
287 /*
288 * make sure MPA receiver interoperate with the
289 * received MPA version and MPA key information
290 *
291 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530292 if (mpa_frame->rev != IETF_MPA_V1 && mpa_frame->rev != IETF_MPA_V2) {
Faisal Latif1cf078c2009-12-09 15:53:54 -0800293 nes_debug(NES_DBG_CM, "The received mpa version"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530294 " is not supported\n");
Faisal Latif1cf078c2009-12-09 15:53:54 -0800295 return -EINVAL;
296 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530297 /*
298 * backwards compatibility only
299 */
300 if (mpa_frame->rev > cm_node->mpa_frame_rev) {
301 nes_debug(NES_DBG_CM, "The received mpa version"
302 " can not be interoperated\n");
303 return -EINVAL;
304 } else {
305 cm_node->mpa_frame_rev = mpa_frame->rev;
306 }
307
Faisal Latif1cf078c2009-12-09 15:53:54 -0800308 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
309 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
310 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
311 return -EINVAL;
312 }
313 } else {
314 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
315 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
316 return -EINVAL;
317 }
318 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800319
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530320
321 if (priv_data_len + mpa_hdr_len != len) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800322 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530323 " complete (%x + %x != %x)\n",
324 priv_data_len, mpa_hdr_len, len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800325 return -EINVAL;
326 }
327 /* make sure it does not exceed the max size */
328 if (len > MAX_CM_BUFFER) {
329 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530330 " (%x + %x != %x)\n",
331 priv_data_len, mpa_hdr_len, len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800332 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800333 }
334
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530335 cm_node->mpa_frame_size = priv_data_len;
336
337 switch (mpa_frame->rev) {
338 case IETF_MPA_V2: {
339 u16 ird_size;
340 u16 ord_size;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800341 u16 rtr_ctrl_ird;
342 u16 rtr_ctrl_ord;
343
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530344 mpa_v2_frame = (struct ietf_mpa_v2 *)buffer;
345 mpa_hdr_len += IETF_RTR_MSG_SIZE;
346 cm_node->mpa_frame_size -= IETF_RTR_MSG_SIZE;
347 rtr_msg = &mpa_v2_frame->rtr_msg;
348
349 /* parse rtr message */
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800350 rtr_ctrl_ird = ntohs(rtr_msg->ctrl_ird);
351 rtr_ctrl_ord = ntohs(rtr_msg->ctrl_ord);
352 ird_size = rtr_ctrl_ird & IETF_NO_IRD_ORD;
353 ord_size = rtr_ctrl_ord & IETF_NO_IRD_ORD;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530354
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800355 if (!(rtr_ctrl_ird & IETF_PEER_TO_PEER)) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530356 /* send reset */
357 return -EINVAL;
358 }
359
360 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
361 /* responder */
362 if (cm_node->ord_size > ird_size)
363 cm_node->ord_size = ird_size;
364 } else {
365 /* initiator */
366 if (cm_node->ord_size > ird_size)
367 cm_node->ord_size = ird_size;
368
369 if (cm_node->ird_size < ord_size) {
370 /* no resources available */
371 /* send terminate message */
372 return -EINVAL;
373 }
374 }
375
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800376 if (rtr_ctrl_ord & IETF_RDMA0_READ) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530377 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800378 } else if (rtr_ctrl_ord & IETF_RDMA0_WRITE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530379 cm_node->send_rdma0_op = SEND_RDMA_WRITE_ZERO;
380 } else { /* Not supported RDMA0 operation */
381 return -EINVAL;
382 }
383 break;
384 }
385 case IETF_MPA_V1:
386 default:
387 break;
388 }
389
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800390 /* copy entire MPA frame to our cm_node's frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530391 memcpy(cm_node->mpa_frame_buf, buffer + mpa_hdr_len, cm_node->mpa_frame_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800392
Faisal Latif9d5ab132009-03-06 15:15:01 -0800393 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
394 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800395 return 0;
396}
397
398
399/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800400 * form_cm_frame - get a free packet and build empty frame Use
401 * node info to build.
402 */
Chien Tung6098d102008-11-21 20:51:01 -0600403static void form_cm_frame(struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530404 struct nes_cm_node *cm_node, void *options, u32 optionsize,
405 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800406{
407 struct tcphdr *tcph;
408 struct iphdr *iph;
409 struct ethhdr *ethh;
410 u8 *buf;
411 u16 packetsize = sizeof(*iph);
412
413 packetsize += sizeof(*tcph);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530414 packetsize += optionsize + datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800415
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530416 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800417 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
418
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800419 buf = skb_put(skb, packetsize + ETH_HLEN);
420
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530421 ethh = (struct ethhdr *)buf;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800422 buf += ETH_HLEN;
423
424 iph = (struct iphdr *)buf;
425 buf += sizeof(*iph);
426 tcph = (struct tcphdr *)buf;
427 skb_reset_mac_header(skb);
428 skb_set_network_header(skb, ETH_HLEN);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530429 skb_set_transport_header(skb, ETH_HLEN + sizeof(*iph));
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800430 buf += sizeof(*tcph);
431
432 skb->ip_summed = CHECKSUM_PARTIAL;
433 skb->protocol = htons(0x800);
434 skb->data_len = 0;
435 skb->mac_len = ETH_HLEN;
436
437 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
438 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
439 ethh->h_proto = htons(0x0800);
440
441 iph->version = IPVERSION;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530442 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800443 iph->tos = 0;
444 iph->tot_len = htons(packetsize);
445 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
446
447 iph->frag_off = htons(0x4000);
448 iph->ttl = 0x40;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530449 iph->protocol = 0x06; /* IPPROTO_TCP */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800450
451 iph->saddr = htonl(cm_node->loc_addr);
452 iph->daddr = htonl(cm_node->rem_addr);
453
454 tcph->source = htons(cm_node->loc_port);
455 tcph->dest = htons(cm_node->rem_port);
456 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
457
458 if (flags & SET_ACK) {
459 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
460 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
461 tcph->ack = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530462 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800463 tcph->ack_seq = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530464 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800465
466 if (flags & SET_SYN) {
467 cm_node->tcp_cntxt.loc_seq_num++;
468 tcph->syn = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530469 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700470 cm_node->tcp_cntxt.loc_seq_num += datasize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530471 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800472
Faisal Latif6492cdf2008-07-24 20:50:45 -0700473 if (flags & SET_FIN) {
474 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800475 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700476 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800477
478 if (flags & SET_RST)
479 tcph->rst = 1;
480
481 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
482 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
483 tcph->urg_ptr = 0;
484 if (optionsize)
485 memcpy(buf, options, optionsize);
486 buf += optionsize;
487 if (datasize)
488 memcpy(buf, data, datasize);
489
490 skb_shinfo(skb)->nr_frags = 0;
491 cm_packets_created++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800492}
493
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800494/**
495 * print_core - dump a cm core
496 */
497static void print_core(struct nes_cm_core *core)
498{
499 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
500 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
501 if (!core)
502 return;
503 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800504
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530505 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800506
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800507 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
508 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
509
510 nes_debug(NES_DBG_CM, "core : %p \n", core);
511
512 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
513}
514
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530515/**
516 * cm_build_mpa_frame - build a MPA V1 frame or MPA V2 frame
517 */
518static int cm_build_mpa_frame(struct nes_cm_node *cm_node, u8 **start_buff,
519 u16 *buff_len, u8 *pci_mem, u8 mpa_key)
520{
521 int ret = 0;
522
523 *start_buff = (pci_mem) ? pci_mem : &cm_node->mpa_frame_buf[0];
524
525 switch (cm_node->mpa_frame_rev) {
526 case IETF_MPA_V1:
527 *start_buff = (u8 *)*start_buff + sizeof(struct ietf_rtr_msg);
528 *buff_len = sizeof(struct ietf_mpa_v1) + cm_node->mpa_frame_size;
529 build_mpa_v1(cm_node, *start_buff, mpa_key);
530 break;
531 case IETF_MPA_V2:
532 *buff_len = sizeof(struct ietf_mpa_v2) + cm_node->mpa_frame_size;
533 build_mpa_v2(cm_node, *start_buff, mpa_key);
534 break;
535 default:
536 ret = -EINVAL;
537 }
538 return ret;
539}
540
541/**
542 * build_mpa_v2 - build a MPA V2 frame
543 */
544static void build_mpa_v2(struct nes_cm_node *cm_node,
545 void *start_addr, u8 mpa_key)
546{
547 struct ietf_mpa_v2 *mpa_frame = (struct ietf_mpa_v2 *)start_addr;
548 struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800549 u16 ctrl_ird;
550 u16 ctrl_ord;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530551
552 /* initialize the upper 5 bytes of the frame */
553 build_mpa_v1(cm_node, start_addr, mpa_key);
554 mpa_frame->flags |= IETF_MPA_V2_FLAG; /* set a bit to indicate MPA V2 */
555 mpa_frame->priv_data_len += htons(IETF_RTR_MSG_SIZE);
556
557 /* initialize RTR msg */
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800558 ctrl_ird = (cm_node->ird_size > IETF_NO_IRD_ORD) ?
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530559 IETF_NO_IRD_ORD : cm_node->ird_size;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800560 ctrl_ord = (cm_node->ord_size > IETF_NO_IRD_ORD) ?
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530561 IETF_NO_IRD_ORD : cm_node->ord_size;
562
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800563 ctrl_ird |= IETF_PEER_TO_PEER;
564 ctrl_ird |= IETF_FLPDU_ZERO_LEN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530565
566 switch (mpa_key) {
567 case MPA_KEY_REQUEST:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800568 ctrl_ord |= IETF_RDMA0_WRITE;
569 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530570 break;
571 case MPA_KEY_REPLY:
572 switch (cm_node->send_rdma0_op) {
573 case SEND_RDMA_WRITE_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800574 ctrl_ord |= IETF_RDMA0_WRITE;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530575 break;
576 case SEND_RDMA_READ_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800577 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530578 break;
579 }
580 }
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800581 rtr_msg->ctrl_ird = htons(ctrl_ird);
582 rtr_msg->ctrl_ord = htons(ctrl_ord);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530583}
584
585/**
586 * build_mpa_v1 - build a MPA V1 frame
587 */
588static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
589{
590 struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
591
592 switch (mpa_key) {
593 case MPA_KEY_REQUEST:
594 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
595 break;
596 case MPA_KEY_REPLY:
597 memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
598 break;
599 }
600 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
601 mpa_frame->rev = cm_node->mpa_frame_rev;
602 mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
603}
604
605static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
606{
607 u64 u64temp;
608 struct nes_qp *nesqp = *nesqp_addr;
609 struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
610
611 u64temp = (unsigned long)nesqp;
612 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
613 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
614
615 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
616 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
617
618 switch (cm_node->send_rdma0_op) {
619 case SEND_RDMA_WRITE_ZERO:
620 nes_debug(NES_DBG_CM, "Sending first write.\n");
621 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
622 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
623 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
624 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
625 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
626 break;
627
628 case SEND_RDMA_READ_ZERO:
629 default:
630 if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO) {
631 printk(KERN_ERR "%s[%u]: Unsupported RDMA0 len operation=%u\n",
632 __func__, __LINE__, cm_node->send_rdma0_op);
633 WARN_ON(1);
634 }
635 nes_debug(NES_DBG_CM, "Sending first rdma operation.\n");
636 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
637 cpu_to_le32(NES_IWARP_SQ_OP_RDMAR);
638 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX] = 1;
639 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_HIGH_IDX] = 0;
640 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] = 0;
641 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_STAG_IDX] = 1;
642 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 1;
643 break;
644 }
645
646 if (nesqp->sq_kmapped) {
647 nesqp->sq_kmapped = 0;
648 kunmap(nesqp->page);
649 }
650
651 /*use the reserved spot on the WQ for the extra first WQE*/
652 nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
653 NES_QPCONTEXT_ORDIRD_WRPDU |
654 NES_QPCONTEXT_ORDIRD_ALSMM));
655 nesqp->skip_lsmm = 1;
656 nesqp->hwqp.sq_tail = 0;
657}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800658
659/**
660 * schedule_nes_timer
661 * note - cm_node needs to be protected before calling this. Encase in:
662 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
663 */
664int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530665 enum nes_timer_type type, int send_retrans,
666 int close_when_complete)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800667{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530668 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700669 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800670 struct nes_timer_entry *new_send;
671 int ret = 0;
672 u32 was_timer_set;
673
674 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
675 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800676 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800677
678 /* new_send->timetosend = currenttime */
679 new_send->retrycount = NES_DEFAULT_RETRYS;
680 new_send->retranscount = NES_DEFAULT_RETRANS;
681 new_send->skb = skb;
682 new_send->timetosend = jiffies;
683 new_send->type = type;
684 new_send->netdev = cm_node->netdev;
685 new_send->send_retrans = send_retrans;
686 new_send->close_when_complete = close_when_complete;
687
688 if (type == NES_TIMER_TYPE_CLOSE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530689 new_send->timetosend += (HZ / 10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800690 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700691 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800692 WARN_ON(1);
693 return -EINVAL;
694 }
695 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800696 }
697
698 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700699 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800700 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700701 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
702 cm_node->send_entry = new_send;
703 add_ref_cm_node(cm_node);
704 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
705 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800706
707 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
708 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700709 nes_debug(NES_DBG_CM, "Error sending packet %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530710 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800711 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700712 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800713 } else {
714 cm_packets_sent++;
715 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700716 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800717 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700718 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800719 return ret;
720 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800721 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800722 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800723
724 was_timer_set = timer_pending(&cm_core->tcp_timer);
725
726 if (!was_timer_set) {
727 cm_core->tcp_timer.expires = new_send->timetosend;
728 add_timer(&cm_core->tcp_timer);
729 }
730
731 return ret;
732}
733
Faisal Latif9d5ab132009-03-06 15:15:01 -0800734static void nes_retrans_expired(struct nes_cm_node *cm_node)
735{
Faisal Latif68237a02009-06-22 22:53:28 -0700736 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latifdae58722010-08-14 21:04:56 +0000737 enum nes_cm_node_state state = cm_node->state;
738 cm_node->state = NES_CM_STATE_CLOSED;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530739
Faisal Latifdae58722010-08-14 21:04:56 +0000740 switch (state) {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800741 case NES_CM_STATE_SYN_RCVD:
742 case NES_CM_STATE_CLOSING:
743 rem_ref_cm_node(cm_node->cm_core, cm_node);
744 break;
745 case NES_CM_STATE_LAST_ACK:
746 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700747 if (cm_node->cm_id)
748 cm_id->rem_ref(cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800749 send_reset(cm_node, NULL);
750 break;
751 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800752 add_ref_cm_node(cm_node);
753 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800754 create_event(cm_node, NES_CM_EVENT_ABORTED);
755 }
756}
757
758static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
759{
760 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
761 struct iw_cm_id *cm_id = cm_node->cm_id;
762 struct nes_qp *nesqp;
763 unsigned long qplockflags;
764
765 if (!recv_entry)
766 return;
767 nesqp = (struct nes_qp *)recv_entry->skb;
768 if (nesqp) {
769 spin_lock_irqsave(&nesqp->lock, qplockflags);
770 if (nesqp->cm_id) {
771 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530772 "refcount = %d: HIT A "
773 "NES_TIMER_TYPE_CLOSE with something "
774 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
775 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800776 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
777 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
778 nesqp->ibqp_state = IB_QPS_ERR;
779 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
780 nes_cm_disconn(nesqp);
781 } else {
782 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
783 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530784 "refcount = %d: HIT A "
785 "NES_TIMER_TYPE_CLOSE with nothing "
786 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
787 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800788 }
789 } else if (rem_node) {
790 /* TIME_WAIT state */
791 rem_ref_cm_node(cm_node->cm_core, cm_node);
792 }
793 if (cm_node->cm_id)
794 cm_id->rem_ref(cm_id);
795 kfree(recv_entry);
796 cm_node->recv_entry = NULL;
797}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800798
799/**
800 * nes_cm_timer_tick
801 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700802static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800803{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800804 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800805 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800806 struct nes_cm_node *cm_node;
807 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800808 struct list_head *list_core_temp;
809 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800810 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800811 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700812 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800813 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800814
Faisal Latif879e5bd2008-11-21 20:50:41 -0600815 struct list_head timer_list;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530816
Faisal Latif879e5bd2008-11-21 20:50:41 -0600817 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800818 spin_lock_irqsave(&cm_core->ht_lock, flags);
819
Faisal Latif6492cdf2008-07-24 20:50:45 -0700820 list_for_each_safe(list_node, list_core_temp,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530821 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800822 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800823 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600824 add_ref_cm_node(cm_node);
825 list_add(&cm_node->timer_entry, &timer_list);
826 }
827 }
828 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
829
830 list_for_each_safe(list_node, list_core_temp, &timer_list) {
831 cm_node = container_of(list_node, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530832 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800833 recv_entry = cm_node->recv_entry;
834
835 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700836 if (time_after(recv_entry->timetosend, jiffies)) {
837 if (nexttimeout > recv_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530838 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800839 nexttimeout = recv_entry->timetosend;
840 settimer = 1;
841 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530842 } else {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800843 handle_recv_entry(cm_node, 1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530844 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800845 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800846
847 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700848 do {
849 send_entry = cm_node->send_entry;
850 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600851 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800852 if (time_after(send_entry->timetosend, jiffies)) {
853 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700854 if ((nexttimeout >
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530855 send_entry->timetosend) ||
856 !settimer) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700857 nexttimeout =
858 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800859 settimer = 1;
860 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800861 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700862 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800863 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800864 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800865 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700866
867 if ((cm_node->state == NES_CM_STATE_TSA) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530868 (cm_node->state == NES_CM_STATE_CLOSED)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700869 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600870 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800871 }
872
Faisal Latif6492cdf2008-07-24 20:50:45 -0700873 if (!send_entry->retranscount ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530874 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800875 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700876 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800877
Faisal Latif6492cdf2008-07-24 20:50:45 -0700878 spin_unlock_irqrestore(
879 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800880 nes_retrans_expired(cm_node);
881 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700882 spin_lock_irqsave(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530883 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600884 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800885 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800886 atomic_inc(&send_entry->skb->users);
887 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700888 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530889 "for node %p, jiffies = %lu, time to send = "
890 "%lu, retranscount = %u, send_entry->seq_num = "
891 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
892 "0x%08X\n", send_entry, cm_node, jiffies,
893 send_entry->timetosend,
894 send_entry->retranscount,
895 send_entry->seq_num,
896 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800897
Faisal Latif6492cdf2008-07-24 20:50:45 -0700898 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530899 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800900 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700901 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800902 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700903 nes_debug(NES_DBG_CM, "rexmit failed for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530904 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800905 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800906 send_entry->retrycount--;
907 nexttimeout = jiffies + NES_SHORT_TIME;
908 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600909 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800910 } else {
911 cm_packets_sent++;
912 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700913 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530914 "%u, retry count = %u.\n",
915 send_entry->retranscount,
916 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800917 if (send_entry->send_retrans) {
918 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700919 timetosend = (NES_RETRY_TIMEOUT <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530920 (NES_DEFAULT_RETRANS - send_entry->retranscount));
Faisal Latif4e9c3902009-04-27 13:39:36 -0700921
Faisal Latif6492cdf2008-07-24 20:50:45 -0700922 send_entry->timetosend = jiffies +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530923 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700924 if (nexttimeout > send_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530925 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800926 nexttimeout = send_entry->timetosend;
927 settimer = 1;
928 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800929 } else {
930 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700931 close_when_complete =
932 send_entry->close_when_complete;
933 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530934 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700935 free_retrans_entry(cm_node);
936 if (close_when_complete)
937 rem_ref_cm_node(cm_node->cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530938 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800939 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700940 } while (0);
941
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800942 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700943 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800944 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800945
946 if (settimer) {
947 if (!timer_pending(&cm_core->tcp_timer)) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530948 cm_core->tcp_timer.expires = nexttimeout;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800949 add_timer(&cm_core->tcp_timer);
950 }
951 }
952}
953
954
955/**
956 * send_syn
957 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700958static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530959 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800960{
961 int ret;
962 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800963 char optionsbuffer[sizeof(struct option_mss) +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530964 sizeof(struct option_windowscale) + sizeof(struct option_base) +
965 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800966
967 int optionssize = 0;
968 /* Sending MSS option */
969 union all_known_options *options;
970
971 if (!cm_node)
972 return -EINVAL;
973
974 options = (union all_known_options *)&optionsbuffer[optionssize];
975 options->as_mss.optionnum = OPTION_NUMBER_MSS;
976 options->as_mss.length = sizeof(struct option_mss);
977 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
978 optionssize += sizeof(struct option_mss);
979
980 options = (union all_known_options *)&optionsbuffer[optionssize];
981 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
982 options->as_windowscale.length = sizeof(struct option_windowscale);
983 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
984 optionssize += sizeof(struct option_windowscale);
985
Faisal Latif6492cdf2008-07-24 20:50:45 -0700986 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800987 options = (union all_known_options *)&optionsbuffer[optionssize];
988 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
989 options->as_base.length = sizeof(struct option_base);
990 optionssize += sizeof(struct option_base);
991 /* we need the size to be a multiple of 4 */
992 options = (union all_known_options *)&optionsbuffer[optionssize];
993 options->as_end = 1;
994 optionssize += 1;
995 options = (union all_known_options *)&optionsbuffer[optionssize];
996 options->as_end = 1;
997 optionssize += 1;
998 }
999
1000 options = (union all_known_options *)&optionsbuffer[optionssize];
1001 options->as_end = OPTION_NUMBER_END;
1002 optionssize += 1;
1003
Faisal Latif6492cdf2008-07-24 20:50:45 -07001004 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001005 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001006 if (!skb) {
1007 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1008 return -1;
1009 }
1010
1011 if (sendack)
1012 flags |= SET_ACK;
1013
1014 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
1015 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1016
1017 return ret;
1018}
1019
1020
1021/**
1022 * send_reset
1023 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001024static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001025{
1026 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001027 int flags = SET_RST | SET_ACK;
1028
Faisal Latif6492cdf2008-07-24 20:50:45 -07001029 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001030 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001031 if (!skb) {
1032 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001033 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001034 }
1035
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001036 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
1037 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
1038
1039 return ret;
1040}
1041
1042
1043/**
1044 * send_ack
1045 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001046static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001047{
1048 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001049
1050 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001051 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001052
1053 if (!skb) {
1054 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1055 return -1;
1056 }
1057
1058 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
1059 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
1060
1061 return ret;
1062}
1063
1064
1065/**
1066 * send_fin
1067 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07001068static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001069{
1070 int ret;
1071
1072 /* if we didn't get a frame get one */
1073 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001074 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001075
1076 if (!skb) {
1077 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1078 return -1;
1079 }
1080
1081 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
1082 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1083
1084 return ret;
1085}
1086
1087
1088/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001089 * find_node - find a cm node that matches the reference cm node
1090 */
1091static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301092 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001093{
1094 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001095 struct list_head *hte;
1096 struct nes_cm_node *cm_node;
1097
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001098 /* get a handle on the hte */
1099 hte = &cm_core->connected_nodes;
1100
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001101 /* walk list and find cm_node associated with this session ID */
1102 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001103 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001104 /* compare quad, return node handle if a match */
1105 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301106 cm_node->loc_addr, cm_node->loc_port,
1107 loc_addr, loc_port,
1108 cm_node->rem_addr, cm_node->rem_port,
1109 rem_addr, rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001110 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301111 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001112 add_ref_cm_node(cm_node);
1113 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1114 return cm_node;
1115 }
1116 }
1117 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1118
1119 /* no owner node */
1120 return NULL;
1121}
1122
1123
1124/**
1125 * find_listener - find a cm node listening on this addr-port pair
1126 */
1127static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301128 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001129{
1130 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001131 struct nes_cm_listener *listen_node;
1132
1133 /* walk list and find cm_node associated with this session ID */
1134 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001135 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001136 /* compare node pair, return node handle if a match */
1137 if (((listen_node->loc_addr == dst_addr) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301138 listen_node->loc_addr == 0x00000000) &&
1139 (listen_node->loc_port == dst_port) &&
1140 (listener_state & listen_node->listener_state)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001141 atomic_inc(&listen_node->ref_count);
1142 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1143 return listen_node;
1144 }
1145 }
1146 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1147
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001148 /* no listener */
1149 return NULL;
1150}
1151
1152
1153/**
1154 * add_hte_node - add a cm node to the hash table
1155 */
1156static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1157{
1158 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001159 struct list_head *hte;
1160
1161 if (!cm_node || !cm_core)
1162 return -EINVAL;
1163
Faisal Latif6492cdf2008-07-24 20:50:45 -07001164 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301165 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001166
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001167 spin_lock_irqsave(&cm_core->ht_lock, flags);
1168
1169 /* get a handle on the hash table element (list head for this slot) */
1170 hte = &cm_core->connected_nodes;
1171 list_add_tail(&cm_node->list, hte);
1172 atomic_inc(&cm_core->ht_node_cnt);
1173
1174 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1175
1176 return 0;
1177}
1178
1179
1180/**
1181 * mini_cm_dec_refcnt_listen
1182 */
1183static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301184 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001185{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001186 int ret = -EINVAL;
1187 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001188 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001189 struct list_head *list_pos = NULL;
1190 struct list_head *list_temp = NULL;
1191 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -06001192 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001193
1194 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301195 "refcnt=%d\n", listener, free_hanging_nodes,
1196 atomic_read(&listener->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001197 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -06001198 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001199 if (free_hanging_nodes) {
1200 spin_lock_irqsave(&cm_core->ht_lock, flags);
1201 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -06001202 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001203 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301204 list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001205 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -06001206 (!cm_node->accelerated)) {
1207 add_ref_cm_node(cm_node);
1208 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001209 }
1210 }
1211 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1212 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001213
1214 list_for_each_safe(list_pos, list_temp, &reset_list) {
1215 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301216 reset_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001217 {
1218 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -08001219 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001220 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
1221 rem_ref_cm_node(cm_node->cm_core, cm_node);
1222 } else {
1223 if (!loopback) {
1224 cleanup_retrans_entry(cm_node);
1225 err = send_reset(cm_node, NULL);
1226 if (err) {
1227 cm_node->state =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301228 NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001229 WARN_ON(1);
1230 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08001231 old_state = cm_node->state;
1232 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1233 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1234 rem_ref_cm_node(
1235 cm_node->cm_core,
1236 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001237 }
1238 } else {
1239 struct nes_cm_event event;
1240
1241 event.cm_node = loopback;
1242 event.cm_info.rem_addr =
1243 loopback->rem_addr;
1244 event.cm_info.loc_addr =
1245 loopback->loc_addr;
1246 event.cm_info.rem_port =
1247 loopback->rem_port;
1248 event.cm_info.loc_port =
1249 loopback->loc_port;
1250 event.cm_info.cm_id = loopback->cm_id;
Faisal Latif43093b92010-02-12 19:57:04 +00001251 add_ref_cm_node(loopback);
1252 loopback->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001253 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001254 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001255
Faisal Latif9d5ab132009-03-06 15:15:01 -08001256 rem_ref_cm_node(cm_node->cm_core,
1257 cm_node);
1258
1259 }
1260 }
1261 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001262 }
1263
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001264 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1265 if (!atomic_dec_return(&listener->ref_count)) {
1266 list_del(&listener->list);
1267
1268 /* decrement our listen node count */
1269 atomic_dec(&cm_core->listen_node_cnt);
1270
1271 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1272
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301273 if (listener->nesvnic)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001274 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301275 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001276
1277 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1278
1279 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001280 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001281 ret = 0;
Faisal Latif6e10d2e2010-02-12 19:55:03 +00001282 atomic_inc(&cm_listens_destroyed);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001283 } else {
1284 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1285 }
1286 if (listener) {
1287 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1288 nes_debug(NES_DBG_CM, "destroying listener (%p)"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301289 " with non-zero pending accepts=%u\n",
1290 listener, atomic_read(&listener->pend_accepts_cnt));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001291 }
1292
1293 return ret;
1294}
1295
1296
1297/**
1298 * mini_cm_del_listen
1299 */
1300static int mini_cm_del_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301301 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001302{
1303 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1304 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1305 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1306}
1307
1308
1309/**
1310 * mini_cm_accelerated
1311 */
1312static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301313 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001314{
1315 u32 was_timer_set;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301316
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001317 cm_node->accelerated = 1;
1318
1319 if (cm_node->accept_pend) {
1320 BUG_ON(!cm_node->listener);
1321 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001322 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001323 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1324 }
1325
1326 was_timer_set = timer_pending(&cm_core->tcp_timer);
1327 if (!was_timer_set) {
1328 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1329 add_timer(&cm_core->tcp_timer);
1330 }
1331
1332 return 0;
1333}
1334
1335
1336/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001337 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001338 */
Faisal Latif7a576df2009-12-09 15:54:33 -08001339static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001340{
1341 struct rtable *rt;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001342 struct neighbour *neigh;
Faisal Latif7a576df2009-12-09 15:54:33 -08001343 int rc = arpindex;
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001344 struct net_device *netdev;
Faisal Latif7a576df2009-12-09 15:54:33 -08001345 struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001346
David S. Miller78fbfd82011-03-12 00:00:52 -05001347 rt = ip_route_output(&init_net, htonl(dst_ip), 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001348 if (IS_ERR(rt)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001349 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301350 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001351 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001352 }
1353
Roland Dreiercf55bb22011-03-24 17:13:20 -07001354 if (netif_is_bond_slave(nesvnic->netdev))
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001355 netdev = nesvnic->netdev->master;
1356 else
1357 netdev = nesvnic->netdev;
1358
David Millere55684f2012-01-24 13:16:03 +00001359 neigh = dst_neigh_lookup(&rt->dst, &dst_ip);
1360
David Miller40e2bb52011-12-02 16:52:27 +00001361 rcu_read_lock();
Bob Sharp7191a0a2008-10-03 12:21:19 -07001362 if (neigh) {
1363 if (neigh->nud_state & NUD_VALID) {
1364 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001365 " is %pM, Gateway is 0x%08X \n", dst_ip,
1366 neigh->ha, ntohl(rt->rt_gateway));
Faisal Latif7a576df2009-12-09 15:54:33 -08001367
1368 if (arpindex >= 0) {
1369 if (!memcmp(nesadapter->arp_table[arpindex].mac_addr,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301370 neigh->ha, ETH_ALEN)) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001371 /* Mac address same as in nes_arp_table */
David Millere55684f2012-01-24 13:16:03 +00001372 goto out;
Faisal Latif7a576df2009-12-09 15:54:33 -08001373 }
1374
1375 nes_manage_arp_cache(nesvnic->netdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301376 nesadapter->arp_table[arpindex].mac_addr,
1377 dst_ip, NES_ARP_DELETE);
Faisal Latif7a576df2009-12-09 15:54:33 -08001378 }
1379
Bob Sharp7191a0a2008-10-03 12:21:19 -07001380 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1381 dst_ip, NES_ARP_ADD);
1382 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1383 NES_ARP_RESOLVE);
David Miller40e2bb52011-12-02 16:52:27 +00001384 } else {
1385 neigh_event_send(neigh, NULL);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001386 }
Bob Sharp7191a0a2008-10-03 12:21:19 -07001387 }
David Millere55684f2012-01-24 13:16:03 +00001388out:
David Miller40e2bb52011-12-02 16:52:27 +00001389 rcu_read_unlock();
David Millere55684f2012-01-24 13:16:03 +00001390
1391 if (neigh)
1392 neigh_release(neigh);
1393
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001394 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001395 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001396}
1397
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001398/**
1399 * make_cm_node - create a new instance of a cm node
1400 */
1401static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301402 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1403 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001404{
1405 struct nes_cm_node *cm_node;
1406 struct timespec ts;
Faisal Latif7a576df2009-12-09 15:54:33 -08001407 int oldarpindex = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001408 int arpindex = 0;
1409 struct nes_device *nesdev;
1410 struct nes_adapter *nesadapter;
1411
1412 /* create an hte and cm_node for this instance */
1413 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1414 if (!cm_node)
1415 return NULL;
1416
1417 /* set our node specific transport info */
1418 cm_node->loc_addr = cm_info->loc_addr;
1419 cm_node->rem_addr = cm_info->rem_addr;
1420 cm_node->loc_port = cm_info->loc_port;
1421 cm_node->rem_port = cm_info->rem_port;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301422
1423 cm_node->mpa_frame_rev = mpa_version;
1424 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
1425 cm_node->ird_size = IETF_NO_IRD_ORD;
1426 cm_node->ord_size = IETF_NO_IRD_ORD;
1427
Harvey Harrison63779432008-10-31 00:56:00 -07001428 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1429 &cm_node->loc_addr, cm_node->loc_port,
1430 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001431 cm_node->listener = listener;
1432 cm_node->netdev = nesvnic->netdev;
1433 cm_node->cm_id = cm_info->cm_id;
1434 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1435
Faisal Latif6492cdf2008-07-24 20:50:45 -07001436 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301437 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001438
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001439 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001440
1441 cm_node->loopbackpartner = NULL;
1442 atomic_set(&cm_node->ref_count, 1);
1443 /* associate our parent CM core */
1444 cm_node->cm_core = cm_core;
1445 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1446 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1447 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301448 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001449 ts = current_kernel_time();
1450 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1451 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301452 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001453 cm_node->tcp_cntxt.rcv_nxt = 0;
1454 /* get a unique session ID , add thread_id to an upcounter to handle race */
1455 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001456 cm_node->conn_type = cm_info->conn_type;
1457 cm_node->apbvt_set = 0;
1458 cm_node->accept_pend = 0;
1459
1460 cm_node->nesvnic = nesvnic;
1461 /* get some device handles, for arp lookup */
1462 nesdev = nesvnic->nesdev;
1463 nesadapter = nesdev->nesadapter;
1464
1465 cm_node->loopbackpartner = NULL;
Faisal Latif7a576df2009-12-09 15:54:33 -08001466
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001467 /* get the mac addr for the remote node */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301468 if (ipv4_is_loopback(htonl(cm_node->rem_addr))) {
Chien Tung1ee86552008-11-21 20:51:04 -06001469 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301470 } else {
Faisal Latif7a576df2009-12-09 15:54:33 -08001471 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1472 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);
Faisal Latif7a576df2009-12-09 15:54:33 -08001473 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001474 if (arpindex < 0) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001475 kfree(cm_node);
1476 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001477 }
1478
1479 /* copy the mac addr to node context */
1480 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001481 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1482 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001483
1484 add_hte_node(cm_core, cm_node);
1485 atomic_inc(&cm_nodes_created);
1486
1487 return cm_node;
1488}
1489
1490
1491/**
1492 * add_ref_cm_node - destroy an instance of a cm node
1493 */
1494static int add_ref_cm_node(struct nes_cm_node *cm_node)
1495{
1496 atomic_inc(&cm_node->ref_count);
1497 return 0;
1498}
1499
1500
1501/**
1502 * rem_ref_cm_node - destroy an instance of a cm node
1503 */
1504static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301505 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001506{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001507 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001508 struct nes_qp *nesqp;
1509
1510 if (!cm_node)
1511 return -EINVAL;
1512
1513 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1514 if (atomic_dec_return(&cm_node->ref_count)) {
1515 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1516 return 0;
1517 }
1518 list_del(&cm_node->list);
1519 atomic_dec(&cm_core->ht_node_cnt);
1520 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1521
1522 /* if the node is destroyed before connection was accelerated */
1523 if (!cm_node->accelerated && cm_node->accept_pend) {
1524 BUG_ON(!cm_node->listener);
1525 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1526 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1527 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001528 WARN_ON(cm_node->send_entry);
1529 if (cm_node->recv_entry)
1530 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001531 if (cm_node->listener) {
1532 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1533 } else {
1534 if (cm_node->apbvt_set && cm_node->nesvnic) {
1535 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301536 PCI_FUNC(
1537 cm_node->nesvnic->nesdev->pcidev->devfn),
1538 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001539 }
1540 }
1541
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001542 atomic_dec(&cm_core->node_cnt);
1543 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001544 nesqp = cm_node->nesqp;
1545 if (nesqp) {
1546 nesqp->cm_node = NULL;
1547 nes_rem_ref(&nesqp->ibqp);
1548 cm_node->nesqp = NULL;
1549 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001550
Faisal Latif6492cdf2008-07-24 20:50:45 -07001551 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001552 return 0;
1553}
1554
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001555/**
1556 * process_options
1557 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001558static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301559 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001560{
1561 u32 tmp;
1562 u32 offset = 0;
1563 union all_known_options *all_options;
1564 char got_mss_option = 0;
1565
1566 while (offset < optionsize) {
1567 all_options = (union all_known_options *)(optionsloc + offset);
1568 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001569 case OPTION_NUMBER_END:
1570 offset = optionsize;
1571 break;
1572 case OPTION_NUMBER_NONE:
1573 offset += 1;
1574 continue;
1575 case OPTION_NUMBER_MSS:
1576 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301577 "Size: %d\n", __func__,
1578 all_options->as_mss.length, offset, optionsize);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001579 got_mss_option = 1;
1580 if (all_options->as_mss.length != 4) {
1581 return 1;
1582 } else {
1583 tmp = ntohs(all_options->as_mss.mss);
1584 if (tmp > 0 && tmp <
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301585 cm_node->tcp_cntxt.mss)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001586 cm_node->tcp_cntxt.mss = tmp;
1587 }
1588 break;
1589 case OPTION_NUMBER_WINDOW_SCALE:
1590 cm_node->tcp_cntxt.snd_wscale =
1591 all_options->as_windowscale.shiftcount;
1592 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001593 default:
1594 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301595 all_options->as_base.optionnum);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001596 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001597 }
1598 offset += all_options->as_base.length;
1599 }
1600 if ((!got_mss_option) && (syn_packet))
1601 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1602 return 0;
1603}
1604
Faisal Latif6492cdf2008-07-24 20:50:45 -07001605static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001606{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001607 atomic_inc(&cm_accel_dropped_pkts);
1608 dev_kfree_skb_any(skb);
1609}
1610
Faisal Latif9d5ab132009-03-06 15:15:01 -08001611static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001612{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001613 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301614 "refcnt=%d\n", cm_node, cm_node->state,
1615 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001616 switch (cm_node->state) {
1617 case NES_CM_STATE_SYN_RCVD:
1618 case NES_CM_STATE_SYN_SENT:
1619 case NES_CM_STATE_ESTABLISHED:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001620 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001621 cm_node->tcp_cntxt.rcv_nxt++;
1622 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001623 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001624 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001625 break;
Faisal Latifb1190d32009-12-09 15:54:32 -08001626 case NES_CM_STATE_MPAREQ_SENT:
1627 create_event(cm_node, NES_CM_EVENT_ABORTED);
1628 cm_node->tcp_cntxt.rcv_nxt++;
1629 cleanup_retrans_entry(cm_node);
1630 cm_node->state = NES_CM_STATE_CLOSED;
1631 add_ref_cm_node(cm_node);
1632 send_reset(cm_node, NULL);
1633 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001634 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001635 cm_node->tcp_cntxt.rcv_nxt++;
1636 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001637 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001638 send_ack(cm_node, NULL);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001639 /* Wait for ACK as this is simultaneous close..
Faisal Latif9d5ab132009-03-06 15:15:01 -08001640 * After we receive ACK, do not send anything..
1641 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001642 break;
1643 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001644 cm_node->tcp_cntxt.rcv_nxt++;
1645 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001646 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001647 send_ack(cm_node, NULL);
1648 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1649 break;
1650 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001651 cm_node->tcp_cntxt.rcv_nxt++;
1652 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001653 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001654 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001655 break;
1656 case NES_CM_STATE_TSA:
1657 default:
1658 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1659 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001660 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001661 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001662}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001663
Faisal Latif6492cdf2008-07-24 20:50:45 -07001664
1665static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1666 struct tcphdr *tcph)
1667{
1668
1669 int reset = 0; /* whether to send reset in case of err.. */
1670 atomic_inc(&cm_resets_recvd);
1671 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1672 " refcnt=%d\n", cm_node, cm_node->state,
1673 atomic_read(&cm_node->ref_count));
1674 cleanup_retrans_entry(cm_node);
1675 switch (cm_node->state) {
1676 case NES_CM_STATE_SYN_SENT:
1677 case NES_CM_STATE_MPAREQ_SENT:
1678 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1679 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1680 cm_node->listener, cm_node->state);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301681 switch (cm_node->mpa_frame_rev) {
1682 case IETF_MPA_V2:
1683 cm_node->mpa_frame_rev = IETF_MPA_V1;
1684 /* send a syn and goto syn sent state */
1685 cm_node->state = NES_CM_STATE_SYN_SENT;
1686 if (send_syn(cm_node, 0, NULL)) {
1687 active_open_err(cm_node, skb, reset);
1688 }
1689 break;
1690 case IETF_MPA_V1:
1691 default:
1692 active_open_err(cm_node, skb, reset);
1693 break;
1694 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001695 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001696 case NES_CM_STATE_MPAREQ_RCVD:
Dan Carpenter4d8d6382010-09-15 18:32:39 +02001697 atomic_inc(&cm_node->passive_state);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001698 dev_kfree_skb_any(skb);
1699 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001700 case NES_CM_STATE_ESTABLISHED:
1701 case NES_CM_STATE_SYN_RCVD:
1702 case NES_CM_STATE_LISTENING:
1703 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1704 passive_open_err(cm_node, skb, reset);
1705 break;
1706 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001707 active_open_err(cm_node, skb, reset);
1708 break;
1709 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001710 drop_packet(skb);
1711 break;
Faisal Latifdae58722010-08-14 21:04:56 +00001712 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif68237a02009-06-22 22:53:28 -07001713 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001714 case NES_CM_STATE_LAST_ACK:
1715 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001716 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001717 cm_node->state = NES_CM_STATE_CLOSED;
1718 rem_ref_cm_node(cm_node->cm_core, cm_node);
1719 drop_packet(skb);
1720 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001721 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001722 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001723 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001724 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001725}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001726
Faisal Latif9d5ab132009-03-06 15:15:01 -08001727
1728static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001729{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301730 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001731 int datasize = skb->len;
1732 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001733
1734 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301735 u32 res_type;
1736
Faisal Latif9d5ab132009-03-06 15:15:01 -08001737 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1738 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001739 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001740 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001741 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301742 "cm_node=%p listener=%p state=%d\n", __func__,
1743 __LINE__, cm_node, cm_node->listener,
1744 cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001745 active_open_err(cm_node, skb, 1);
1746 } else {
1747 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001748 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001749 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001750 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001751
1752 switch (cm_node->state) {
1753 case NES_CM_STATE_ESTABLISHED:
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301754 if (res_type == NES_MPA_REQUEST_REJECT)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001755 /*BIG problem as we are receiving the MPA.. So should
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301756 * not be REJECT.. This is Passive Open.. We can
1757 * only receive it Reject for Active Open...*/
Faisal Latif9d5ab132009-03-06 15:15:01 -08001758 WARN_ON(1);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001759 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1760 type = NES_CM_EVENT_MPA_REQ;
1761 atomic_set(&cm_node->passive_state,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301762 NES_PASSIVE_STATE_INDICATED);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001763 break;
1764 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001765 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001766 if (res_type == NES_MPA_REQUEST_REJECT) {
1767 type = NES_CM_EVENT_MPA_REJECT;
1768 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1769 } else {
1770 type = NES_CM_EVENT_CONNECTED;
1771 cm_node->state = NES_CM_STATE_TSA;
1772 }
1773
1774 break;
1775 default:
1776 WARN_ON(1);
1777 break;
1778 }
1779 dev_kfree_skb_any(skb);
1780 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001781}
1782
1783static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1784{
1785 switch (cm_node->state) {
1786 case NES_CM_STATE_SYN_SENT:
1787 case NES_CM_STATE_MPAREQ_SENT:
1788 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301789 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1790 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001791 active_open_err(cm_node, skb, 1);
1792 break;
1793 case NES_CM_STATE_ESTABLISHED:
1794 case NES_CM_STATE_SYN_RCVD:
1795 passive_open_err(cm_node, skb, 1);
1796 break;
1797 case NES_CM_STATE_TSA:
1798 default:
1799 drop_packet(skb);
1800 }
1801}
1802
1803static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301804 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001805{
1806 int err;
1807
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301808 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001809 if (err)
1810 active_open_err(cm_node, skb, 1);
1811
1812 return err;
1813}
1814
1815static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301816 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001817{
1818 int err = 0;
1819 u32 seq;
1820 u32 ack_seq;
1821 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1822 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1823 u32 rcv_wnd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301824
Faisal Latif6492cdf2008-07-24 20:50:45 -07001825 seq = ntohl(tcph->seq);
1826 ack_seq = ntohl(tcph->ack_seq);
1827 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1828 if (ack_seq != loc_seq_num)
1829 err = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301830 else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001831 err = 1;
1832 if (err) {
1833 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301834 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1835 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001836 indicate_pkt_err(cm_node, skb);
1837 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301838 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1839 rcv_wnd);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001840 }
1841 return err;
1842}
1843
1844/*
1845 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1846 * is created with a listener or it may comein as rexmitted packet which in
1847 * that case will be just dropped.
1848 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001849static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301850 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001851{
1852 int ret;
1853 u32 inc_sequence;
1854 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001855
1856 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001857 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001858 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001859
Faisal Latif6492cdf2008-07-24 20:50:45 -07001860 switch (cm_node->state) {
1861 case NES_CM_STATE_SYN_SENT:
1862 case NES_CM_STATE_MPAREQ_SENT:
1863 /* Rcvd syn on active open connection*/
1864 active_open_err(cm_node, skb, 1);
1865 break;
1866 case NES_CM_STATE_LISTENING:
1867 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001868 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301869 cm_node->listener->backlog) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001870 nes_debug(NES_DBG_CM, "drop syn due to backlog "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301871 "pressure \n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07001872 cm_backlog_drops++;
1873 passive_open_err(cm_node, skb, 0);
1874 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001875 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001876 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301877 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001878 if (ret) {
1879 passive_open_err(cm_node, skb, 0);
1880 /* drop pkt */
1881 break;
1882 }
1883 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1884 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001885 cm_node->accept_pend = 1;
1886 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1887
Faisal Latif6492cdf2008-07-24 20:50:45 -07001888 cm_node->state = NES_CM_STATE_SYN_RCVD;
1889 send_syn(cm_node, 1, skb);
1890 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001891 case NES_CM_STATE_CLOSED:
1892 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001893 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001894 send_reset(cm_node, skb);
1895 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001896 case NES_CM_STATE_TSA:
1897 case NES_CM_STATE_ESTABLISHED:
1898 case NES_CM_STATE_FIN_WAIT1:
1899 case NES_CM_STATE_FIN_WAIT2:
1900 case NES_CM_STATE_MPAREQ_RCVD:
1901 case NES_CM_STATE_LAST_ACK:
1902 case NES_CM_STATE_CLOSING:
1903 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001904 default:
1905 drop_packet(skb);
1906 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001907 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001908}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001909
Faisal Latif6492cdf2008-07-24 20:50:45 -07001910static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301911 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001912{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001913 int ret;
1914 u32 inc_sequence;
1915 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001916
Faisal Latif6492cdf2008-07-24 20:50:45 -07001917 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001918 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001919 inc_sequence = ntohl(tcph->seq);
1920 switch (cm_node->state) {
1921 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001922 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001923 /* active open */
1924 if (check_syn(cm_node, tcph, skb))
1925 return;
1926 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1927 /* setup options */
1928 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1929 if (ret) {
1930 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301931 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001932 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001933 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001934 cleanup_retrans_entry(cm_node);
1935 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1936 send_mpa_request(cm_node, skb);
1937 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1938 break;
1939 case NES_CM_STATE_MPAREQ_RCVD:
1940 /* passive open, so should not be here */
1941 passive_open_err(cm_node, skb, 1);
1942 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001943 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001944 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1945 cleanup_retrans_entry(cm_node);
1946 cm_node->state = NES_CM_STATE_CLOSED;
1947 send_reset(cm_node, skb);
1948 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001949 case NES_CM_STATE_CLOSED:
1950 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1951 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001952 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001953 send_reset(cm_node, skb);
1954 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001955 case NES_CM_STATE_ESTABLISHED:
1956 case NES_CM_STATE_FIN_WAIT1:
1957 case NES_CM_STATE_FIN_WAIT2:
1958 case NES_CM_STATE_LAST_ACK:
1959 case NES_CM_STATE_TSA:
1960 case NES_CM_STATE_CLOSING:
1961 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001962 case NES_CM_STATE_MPAREQ_SENT:
1963 default:
1964 drop_packet(skb);
1965 break;
1966 }
1967}
1968
Faisal Latif109d67e2009-04-27 13:41:06 -07001969static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301970 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001971{
1972 int datasize = 0;
1973 u32 inc_sequence;
Faisal Latif109d67e2009-04-27 13:41:06 -07001974 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06001975 int optionsize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301976
Faisal Latifabb77252008-11-21 20:50:52 -06001977 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001978
Faisal Latif6492cdf2008-07-24 20:50:45 -07001979 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07001980 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001981
1982 skb_pull(skb, tcph->doff << 2);
1983 inc_sequence = ntohl(tcph->seq);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001984 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001985 switch (cm_node->state) {
1986 case NES_CM_STATE_SYN_RCVD:
1987 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07001988 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001989 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1990 if (ret)
1991 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001992 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1993 cm_node->state = NES_CM_STATE_ESTABLISHED;
1994 if (datasize) {
1995 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001996 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301997 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001998 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301999 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002000 break;
2001 case NES_CM_STATE_ESTABLISHED:
2002 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08002003 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002004 if (datasize) {
2005 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002006 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302007 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002008 drop_packet(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302009 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002010 break;
2011 case NES_CM_STATE_MPAREQ_SENT:
2012 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2013 if (datasize) {
2014 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002015 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302016 } else { /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002017 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302018 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002019 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002020 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002021 cleanup_retrans_entry(cm_node);
2022 cm_node->state = NES_CM_STATE_CLOSED;
2023 send_reset(cm_node, skb);
2024 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002025 case NES_CM_STATE_CLOSED:
2026 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08002027 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002028 send_reset(cm_node, skb);
2029 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002030 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002031 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002032 cleanup_retrans_entry(cm_node);
2033 cm_node->state = NES_CM_STATE_CLOSED;
2034 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002035 rem_ref_cm_node(cm_node->cm_core, cm_node);
2036 drop_packet(skb);
2037 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002038 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002039 cleanup_retrans_entry(cm_node);
2040 drop_packet(skb);
2041 cm_node->state = NES_CM_STATE_FIN_WAIT2;
2042 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002043 case NES_CM_STATE_SYN_SENT:
2044 case NES_CM_STATE_FIN_WAIT2:
2045 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002046 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002047 case NES_CM_STATE_UNKNOWN:
2048 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002049 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002050 drop_packet(skb);
2051 break;
2052 }
Faisal Latif109d67e2009-04-27 13:41:06 -07002053 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002054}
2055
2056
2057
2058static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302059 struct sk_buff *skb, int optionsize, int passive)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002060{
2061 u8 *optionsloc = (u8 *)&tcph[1];
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302062
Faisal Latif6492cdf2008-07-24 20:50:45 -07002063 if (optionsize) {
2064 if (process_options(cm_node, optionsloc, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302065 (u32)tcph->syn)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002066 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302067 __func__, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002068 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06002069 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002070 else
Faisal Latifabb77252008-11-21 20:50:52 -06002071 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002072 return 1;
2073 }
2074 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002075
2076 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302077 cm_node->tcp_cntxt.snd_wscale;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002078
Faisal Latif6492cdf2008-07-24 20:50:45 -07002079 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002080 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002081 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002082}
2083
Faisal Latif6492cdf2008-07-24 20:50:45 -07002084/*
2085 * active_open_err() will send reset() if flag set..
2086 * It will also send ABORT event.
2087 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002088static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302089 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002090{
2091 cleanup_retrans_entry(cm_node);
2092 if (reset) {
2093 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302094 "state=%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002095 add_ref_cm_node(cm_node);
2096 send_reset(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302097 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002098 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302099 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002100
2101 cm_node->state = NES_CM_STATE_CLOSED;
2102 create_event(cm_node, NES_CM_EVENT_ABORTED);
2103}
2104
2105/*
2106 * passive_open_err() will either do a reset() or will free up the skb and
2107 * remove the cm_node.
2108 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002109static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302110 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002111{
2112 cleanup_retrans_entry(cm_node);
2113 cm_node->state = NES_CM_STATE_CLOSED;
2114 if (reset) {
2115 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302116 "cm_node=%p state =%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002117 send_reset(cm_node, skb);
2118 } else {
2119 dev_kfree_skb_any(skb);
2120 rem_ref_cm_node(cm_node->cm_core, cm_node);
2121 }
2122}
2123
2124/*
2125 * free_retrans_entry() routines assumes that the retrans_list_lock has
2126 * been acquired before calling.
2127 */
2128static void free_retrans_entry(struct nes_cm_node *cm_node)
2129{
2130 struct nes_timer_entry *send_entry;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302131
Faisal Latif6492cdf2008-07-24 20:50:45 -07002132 send_entry = cm_node->send_entry;
2133 if (send_entry) {
2134 cm_node->send_entry = NULL;
2135 dev_kfree_skb_any(send_entry->skb);
2136 kfree(send_entry);
2137 rem_ref_cm_node(cm_node->cm_core, cm_node);
2138 }
2139}
2140
2141static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
2142{
2143 unsigned long flags;
2144
2145 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
2146 free_retrans_entry(cm_node);
2147 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
2148}
2149
2150/**
2151 * process_packet
2152 * Returns skb if to be freed, else it will return NULL if already used..
2153 */
2154static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302155 struct nes_cm_core *cm_core)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002156{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302157 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002158 struct tcphdr *tcph = tcp_hdr(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302159 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07002160 int ret = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302161
Faisal Latif6492cdf2008-07-24 20:50:45 -07002162 skb_pull(skb, ip_hdr(skb)->ihl << 2);
2163
2164 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302165 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
2166 tcph->ack, tcph->rst, tcph->fin);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002167
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302168 if (tcph->rst) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002169 pkt_type = NES_PKT_TYPE_RST;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302170 } else if (tcph->syn) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002171 pkt_type = NES_PKT_TYPE_SYN;
2172 if (tcph->ack)
2173 pkt_type = NES_PKT_TYPE_SYNACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302174 } else if (tcph->ack) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002175 pkt_type = NES_PKT_TYPE_ACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302176 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002177 if (tcph->fin)
2178 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002179
2180 switch (pkt_type) {
2181 case NES_PKT_TYPE_SYN:
2182 handle_syn_pkt(cm_node, skb, tcph);
2183 break;
2184 case NES_PKT_TYPE_SYNACK:
2185 handle_synack_pkt(cm_node, skb, tcph);
2186 break;
2187 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002188 ret = handle_ack_pkt(cm_node, skb, tcph);
2189 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08002190 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002191 break;
2192 case NES_PKT_TYPE_RST:
2193 handle_rst_pkt(cm_node, skb, tcph);
2194 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002195 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002196 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08002197 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07002198 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002199 break;
2200 }
2201}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002202
2203/**
2204 * mini_cm_listen - create a listen node with params
2205 */
2206static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302207 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002208{
2209 struct nes_cm_listener *listener;
2210 unsigned long flags;
2211
2212 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302213 cm_info->loc_addr, cm_info->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002214
2215 /* cannot have multiple matching listeners */
2216 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302217 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002218 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
2219 /* find automatically incs ref count ??? */
2220 atomic_dec(&listener->ref_count);
2221 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
2222 return NULL;
2223 }
2224
2225 if (!listener) {
2226 /* create a CM listen node (1/2 node to compare incoming traffic to) */
2227 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
2228 if (!listener) {
2229 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
2230 return NULL;
2231 }
2232
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002233 listener->loc_addr = htonl(cm_info->loc_addr);
2234 listener->loc_port = htons(cm_info->loc_port);
2235 listener->reused_node = 0;
2236
2237 atomic_set(&listener->ref_count, 1);
2238 }
2239 /* pasive case */
2240 /* find already inc'ed the ref count */
2241 else {
2242 listener->reused_node = 1;
2243 }
2244
2245 listener->cm_id = cm_info->cm_id;
2246 atomic_set(&listener->pend_accepts_cnt, 0);
2247 listener->cm_core = cm_core;
2248 listener->nesvnic = nesvnic;
2249 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002250
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002251 listener->conn_type = cm_info->conn_type;
2252 listener->backlog = cm_info->backlog;
2253 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
2254
2255 if (!listener->reused_node) {
2256 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2257 list_add(&listener->list, &cm_core->listen_list.list);
2258 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2259 atomic_inc(&cm_core->listen_node_cnt);
2260 }
2261
2262 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302263 " listener = %p, backlog = %d, cm_id = %p.\n",
2264 cm_info->loc_addr, cm_info->loc_port,
2265 listener, listener->backlog, listener->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002266
2267 return listener;
2268}
2269
2270
2271/**
2272 * mini_cm_connect - make a connection node with params
2273 */
Faisal Latif54c86a82008-09-30 14:47:27 -07002274static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302275 struct nes_vnic *nesvnic, u16 private_data_len,
2276 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002277{
2278 int ret = 0;
2279 struct nes_cm_node *cm_node;
2280 struct nes_cm_listener *loopbackremotelistener;
2281 struct nes_cm_node *loopbackremotenode;
2282 struct nes_cm_info loopback_cm_info;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302283 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002284
2285 /* create a CM connection node */
2286 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2287 if (!cm_node)
2288 return NULL;
2289
Glenn Streiff7495ab62008-04-29 13:46:54 -07002290 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002291 cm_node->tcp_cntxt.client = 1;
2292 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2293
2294 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002295 loopbackremotelistener = find_listener(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302296 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2297 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002298 if (loopbackremotelistener == NULL) {
2299 create_event(cm_node, NES_CM_EVENT_ABORTED);
2300 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002301 loopback_cm_info = *cm_info;
2302 loopback_cm_info.loc_port = cm_info->rem_port;
2303 loopback_cm_info.rem_port = cm_info->loc_port;
2304 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002305 loopbackremotenode = make_cm_node(cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302306 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002307 if (!loopbackremotenode) {
2308 rem_ref_cm_node(cm_node->cm_core, cm_node);
2309 return NULL;
2310 }
2311 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002312 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002313 loopbackremotenode->tcp_cntxt.rcv_wscale =
2314 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002315 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002316 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302317 private_data_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002318 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002319
Faisal Latif6492cdf2008-07-24 20:50:45 -07002320 /* we are done handling this state. */
2321 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002322 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002323 cm_node->tcp_cntxt.rcv_nxt =
2324 loopbackremotenode->tcp_cntxt.loc_seq_num;
2325 loopbackremotenode->tcp_cntxt.rcv_nxt =
2326 cm_node->tcp_cntxt.loc_seq_num;
2327 cm_node->tcp_cntxt.max_snd_wnd =
2328 loopbackremotenode->tcp_cntxt.rcv_wnd;
2329 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2330 cm_node->tcp_cntxt.rcv_wnd;
2331 cm_node->tcp_cntxt.snd_wnd =
2332 loopbackremotenode->tcp_cntxt.rcv_wnd;
2333 loopbackremotenode->tcp_cntxt.snd_wnd =
2334 cm_node->tcp_cntxt.rcv_wnd;
2335 cm_node->tcp_cntxt.snd_wscale =
2336 loopbackremotenode->tcp_cntxt.rcv_wscale;
2337 loopbackremotenode->tcp_cntxt.snd_wscale =
2338 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002339 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002340 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2341 }
2342 return cm_node;
2343 }
2344
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302345 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
2346 cm_node->mpa_frame_size = private_data_len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002347
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302348 memcpy(start_buff, private_data, private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002349
2350 /* send a syn and goto syn sent state */
2351 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002352 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002353
Faisal Latif6492cdf2008-07-24 20:50:45 -07002354 if (ret) {
2355 /* error in sending the syn free up the cm_node struct */
2356 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302357 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2358 cm_node->rem_addr, cm_node->rem_port, cm_node,
2359 cm_node->cm_id);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002360 rem_ref_cm_node(cm_node->cm_core, cm_node);
2361 cm_node = NULL;
2362 }
2363
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302364 if (cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002365 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302366 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2367 cm_node->rem_addr, cm_node->rem_port, cm_node,
2368 cm_node->cm_id);
2369 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002370
2371 return cm_node;
2372}
2373
2374
2375/**
2376 * mini_cm_accept - accept a connection
2377 * This function is never called
2378 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302379static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002380{
2381 return 0;
2382}
2383
2384
2385/**
2386 * mini_cm_reject - reject and teardown a connection
2387 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302388static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002389{
2390 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002391 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002392 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002393 struct nes_cm_event event;
2394 struct iw_cm_id *cm_id = cm_node->cm_id;
2395 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002396
Faisal Latif6492cdf2008-07-24 20:50:45 -07002397 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302398 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002399
Faisal Latif6492cdf2008-07-24 20:50:45 -07002400 if (cm_node->tcp_cntxt.client)
2401 return ret;
2402 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002403
Faisal Latif9d5ab132009-03-06 15:15:01 -08002404 if (!loopback) {
2405 passive_state = atomic_add_return(1, &cm_node->passive_state);
2406 if (passive_state == NES_SEND_RESET_EVENT) {
2407 cm_node->state = NES_CM_STATE_CLOSED;
2408 rem_ref_cm_node(cm_core, cm_node);
2409 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002410 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2411 rem_ref_cm_node(cm_core, cm_node);
2412 } else {
2413 ret = send_mpa_reject(cm_node);
2414 if (ret) {
2415 cm_node->state = NES_CM_STATE_CLOSED;
2416 err = send_reset(cm_node, NULL);
2417 if (err)
2418 WARN_ON(1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302419 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002420 cm_id->add_ref(cm_id);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302421 }
Faisal Latifc5a7d482009-12-09 15:54:08 -08002422 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002423 }
2424 } else {
2425 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002426 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2427 rem_ref_cm_node(cm_core, cm_node);
2428 rem_ref_cm_node(cm_core, loopback);
2429 } else {
2430 event.cm_node = loopback;
2431 event.cm_info.rem_addr = loopback->rem_addr;
2432 event.cm_info.loc_addr = loopback->loc_addr;
2433 event.cm_info.rem_port = loopback->rem_port;
2434 event.cm_info.loc_port = loopback->loc_port;
2435 event.cm_info.cm_id = loopback->cm_id;
2436 cm_event_mpa_reject(&event);
2437 rem_ref_cm_node(cm_core, cm_node);
2438 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002439
Faisal Latifc5a7d482009-12-09 15:54:08 -08002440 cm_id = loopback->cm_id;
2441 rem_ref_cm_node(cm_core, loopback);
2442 cm_id->rem_ref(cm_id);
2443 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002444 }
2445
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002446 return ret;
2447}
2448
2449
2450/**
2451 * mini_cm_close
2452 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002453static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002454{
2455 int ret = 0;
2456
2457 if (!cm_core || !cm_node)
2458 return -EINVAL;
2459
2460 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002461 case NES_CM_STATE_SYN_RCVD:
2462 case NES_CM_STATE_SYN_SENT:
2463 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2464 case NES_CM_STATE_ESTABLISHED:
2465 case NES_CM_STATE_ACCEPTING:
2466 case NES_CM_STATE_MPAREQ_SENT:
2467 case NES_CM_STATE_MPAREQ_RCVD:
2468 cleanup_retrans_entry(cm_node);
2469 send_reset(cm_node, NULL);
2470 break;
2471 case NES_CM_STATE_CLOSE_WAIT:
2472 cm_node->state = NES_CM_STATE_LAST_ACK;
2473 send_fin(cm_node, NULL);
2474 break;
2475 case NES_CM_STATE_FIN_WAIT1:
2476 case NES_CM_STATE_FIN_WAIT2:
2477 case NES_CM_STATE_LAST_ACK:
2478 case NES_CM_STATE_TIME_WAIT:
2479 case NES_CM_STATE_CLOSING:
2480 ret = -1;
2481 break;
2482 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002483 cleanup_retrans_entry(cm_node);
2484 send_reset(cm_node, NULL);
2485 break;
2486 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002487 case NES_CM_STATE_UNKNOWN:
2488 case NES_CM_STATE_INITED:
2489 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002490 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002491 ret = rem_ref_cm_node(cm_core, cm_node);
2492 break;
2493 case NES_CM_STATE_TSA:
2494 if (cm_node->send_entry)
2495 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302496 "send_entry=%p\n", cm_node->send_entry);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002497 ret = rem_ref_cm_node(cm_core, cm_node);
2498 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002499 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002500 return ret;
2501}
2502
2503
2504/**
2505 * recv_pkt - recv an ETHERNET packet, and process it through CM
2506 * node state machine
2507 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002508static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302509 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002510{
2511 struct nes_cm_node *cm_node = NULL;
2512 struct nes_cm_listener *listener = NULL;
2513 struct iphdr *iph;
2514 struct tcphdr *tcph;
2515 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002516 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002517 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002518
Faisal Latif6492cdf2008-07-24 20:50:45 -07002519 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002520 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302521 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002522 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002523
2524 iph = (struct iphdr *)skb->data;
2525 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002526
2527 nfo.loc_addr = ntohl(iph->daddr);
2528 nfo.loc_port = ntohs(tcph->dest);
2529 nfo.rem_addr = ntohl(iph->saddr);
2530 nfo.rem_port = ntohs(tcph->source);
2531
Harvey Harrison03080e52009-01-10 21:45:42 -08002532 tmp_daddr = cpu_to_be32(iph->daddr);
2533 tmp_saddr = cpu_to_be32(iph->saddr);
2534
Harvey Harrison63779432008-10-31 00:56:00 -07002535 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002536 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002537
Faisal Latif6492cdf2008-07-24 20:50:45 -07002538 do {
2539 cm_node = find_node(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302540 nfo.rem_port, nfo.rem_addr,
2541 nfo.loc_port, nfo.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002542
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002543 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002544 /* Only type of packet accepted are for */
2545 /* the PASSIVE open (syn only) */
2546 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002547 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002548 break;
2549 }
2550 listener = find_listener(cm_core, nfo.loc_addr,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302551 nfo.loc_port,
2552 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002553 if (!listener) {
2554 nfo.cm_id = NULL;
2555 nfo.conn_type = 0;
2556 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2557 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002558 break;
2559 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002560 nfo.cm_id = listener->cm_id;
2561 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002562 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302563 listener);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002564 if (!cm_node) {
2565 nes_debug(NES_DBG_CM, "Unable to allocate "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302566 "node\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07002567 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002568 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002569 dev_kfree_skb_any(skb);
2570 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002571 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002572 if (!tcph->rst && !tcph->fin) {
2573 cm_node->state = NES_CM_STATE_LISTENING;
2574 } else {
2575 cm_packets_dropped++;
2576 rem_ref_cm_node(cm_core, cm_node);
2577 dev_kfree_skb_any(skb);
2578 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002579 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002580 add_ref_cm_node(cm_node);
2581 } else if (cm_node->state == NES_CM_STATE_TSA) {
Faisal Latif0f0bee82011-09-25 20:34:00 -05002582 if (cm_node->nesqp->pau_mode)
2583 nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
2584 else {
2585 rem_ref_cm_node(cm_core, cm_node);
2586 atomic_inc(&cm_accel_dropped_pkts);
2587 dev_kfree_skb_any(skb);
2588 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002589 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002590 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002591 skb_reset_network_header(skb);
2592 skb_set_transport_header(skb, sizeof(*tcph));
2593 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002594 process_packet(cm_node, skb, cm_core);
2595 rem_ref_cm_node(cm_core, cm_node);
2596 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002597 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002598}
2599
2600
2601/**
2602 * nes_cm_alloc_core - allocate a top level instance of a cm core
2603 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002604static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002605{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002606 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002607
2608 /* setup the CM core */
2609 /* alloc top level core control structure */
2610 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2611 if (!cm_core)
2612 return NULL;
2613
2614 INIT_LIST_HEAD(&cm_core->connected_nodes);
2615 init_timer(&cm_core->tcp_timer);
2616 cm_core->tcp_timer.function = nes_cm_timer_tick;
2617
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302618 cm_core->mtu = NES_CM_DEFAULT_MTU;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002619 cm_core->state = NES_CM_STATE_INITED;
2620 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2621
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002622 atomic_set(&cm_core->events_posted, 0);
2623
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002624 cm_core->api = &nes_cm_api;
2625
2626 spin_lock_init(&cm_core->ht_lock);
2627 spin_lock_init(&cm_core->listen_list_lock);
2628
2629 INIT_LIST_HEAD(&cm_core->listen_list.list);
2630
2631 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2632
2633 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2634 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2635 cm_core->post_event = nes_cm_post_event;
2636 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2637 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2638
2639 print_core(cm_core);
2640 return cm_core;
2641}
2642
2643
2644/**
2645 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2646 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002647static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002648{
2649 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2650
2651 if (!cm_core)
2652 return -EINVAL;
2653
2654 barrier();
2655
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302656 if (timer_pending(&cm_core->tcp_timer))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002657 del_timer(&cm_core->tcp_timer);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002658
2659 destroy_workqueue(cm_core->event_wq);
2660 destroy_workqueue(cm_core->disconn_wq);
2661 nes_debug(NES_DBG_CM, "\n");
2662 kfree(cm_core);
2663
2664 return 0;
2665}
2666
2667
2668/**
2669 * mini_cm_get
2670 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002671static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002672{
2673 return cm_core->state;
2674}
2675
2676
2677/**
2678 * mini_cm_set
2679 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002680static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002681{
2682 int ret = 0;
2683
2684 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002685 case NES_CM_SET_PKT_SIZE:
2686 cm_core->mtu = value;
2687 break;
2688 case NES_CM_SET_FREE_PKT_Q_SIZE:
2689 cm_core->free_tx_pkt_max = value;
2690 break;
2691 default:
2692 /* unknown set option */
2693 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002694 }
2695
2696 return ret;
2697}
2698
2699
2700/**
2701 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2702 * successfully exchanged when this is called
2703 */
2704static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2705{
2706 int ret = 0;
2707
2708 if (!nesqp)
2709 return -EINVAL;
2710
2711 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302712 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2713 NES_QPCONTEXT_MISC_DROS);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002714
2715 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2716 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2717
2718 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2719
2720 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2721
2722 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302723 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002724
2725 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302726 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2727 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002728
2729 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302730 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2731 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002732
2733 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2734 nesqp->nesqp_context->ts_recent = 0;
2735 nesqp->nesqp_context->ts_age = 0;
2736 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2737 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2738 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2739 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302740 cm_node->tcp_cntxt.rcv_wscale);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002741 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2742 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2743 nesqp->nesqp_context->srtt = 0;
2744 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2745 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302746 nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002747 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2748 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2749 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2750
2751 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302752 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2753 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2754 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2755 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2756 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2757 le32_to_cpu(nesqp->nesqp_context->misc));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002758 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2759 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2760 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2761
2762 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2763 cm_node->state = NES_CM_STATE_TSA;
2764
2765 return ret;
2766}
2767
2768
2769/**
2770 * nes_cm_disconn
2771 */
2772int nes_cm_disconn(struct nes_qp *nesqp)
2773{
Don Wood873fcdd2009-09-05 20:36:37 -07002774 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002775
Don Wood873fcdd2009-09-05 20:36:37 -07002776 work = kzalloc(sizeof *work, GFP_ATOMIC);
2777 if (!work)
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302778 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002779
Don Wood873fcdd2009-09-05 20:36:37 -07002780 nes_add_ref(&nesqp->ibqp);
2781 work->nesqp = nesqp;
2782 INIT_WORK(&work->work, nes_disconnect_worker);
2783 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002784 return 0;
2785}
2786
2787
2788/**
2789 * nes_disconnect_worker
2790 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002791static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002792{
Don Wood873fcdd2009-09-05 20:36:37 -07002793 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2794 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002795
Don Wood873fcdd2009-09-05 20:36:37 -07002796 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002797 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302798 nesqp->last_aeq, nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002799 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002800 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002801}
2802
2803
2804/**
2805 * nes_cm_disconn_true
2806 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002807static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002808{
2809 unsigned long flags;
2810 int ret = 0;
2811 struct iw_cm_id *cm_id;
2812 struct iw_cm_event cm_event;
2813 struct nes_vnic *nesvnic;
2814 u16 last_ae;
2815 u8 original_hw_tcp_state;
2816 u8 original_ibqp_state;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002817 int disconn_status = 0;
Don Woodb29a4fc2009-09-05 20:36:39 -07002818 int issue_disconn = 0;
2819 int issue_close = 0;
2820 int issue_flush = 0;
2821 u32 flush_q = NES_CQP_FLUSH_RQ;
2822 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002823
2824 if (!nesqp) {
2825 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2826 return -1;
2827 }
2828
2829 spin_lock_irqsave(&nesqp->lock, flags);
2830 cm_id = nesqp->cm_id;
2831 /* make sure we havent already closed this connection */
2832 if (!cm_id) {
2833 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302834 nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002835 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002836 return -1;
2837 }
2838
2839 nesvnic = to_nesvnic(nesqp->ibqp.device);
2840 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2841
2842 original_hw_tcp_state = nesqp->hw_tcp_state;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302843 original_ibqp_state = nesqp->ibqp_state;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002844 last_ae = nesqp->last_aeq;
2845
Don Woodb29a4fc2009-09-05 20:36:39 -07002846 if (nesqp->term_flags) {
2847 issue_disconn = 1;
2848 issue_close = 1;
2849 nesqp->cm_id = NULL;
Tatyana Nikolova196f40c2011-12-05 11:39:12 -06002850 del_timer(&nesqp->terminate_timer);
Don Woodb29a4fc2009-09-05 20:36:39 -07002851 if (nesqp->flush_issued == 0) {
2852 nesqp->flush_issued = 1;
2853 issue_flush = 1;
2854 }
2855 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2856 ((original_ibqp_state == IB_QPS_RTS) &&
2857 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2858 issue_disconn = 1;
2859 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002860 disconn_status = -ECONNRESET;
Don Woodb29a4fc2009-09-05 20:36:39 -07002861 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002862
Don Woodb29a4fc2009-09-05 20:36:39 -07002863 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2864 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2865 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2866 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2867 issue_close = 1;
2868 nesqp->cm_id = NULL;
2869 if (nesqp->flush_issued == 0) {
2870 nesqp->flush_issued = 1;
2871 issue_flush = 1;
2872 }
2873 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002874
Don Woodb29a4fc2009-09-05 20:36:39 -07002875 spin_unlock_irqrestore(&nesqp->lock, flags);
2876
2877 if ((issue_flush) && (nesqp->destroyed == 0)) {
2878 /* Flush the queue(s) */
2879 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2880 flush_q |= NES_CQP_FLUSH_SQ;
2881 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2882
2883 if (nesqp->term_flags) {
2884 ibevent.device = nesqp->ibqp.device;
2885 ibevent.event = nesqp->terminate_eventtype;
2886 ibevent.element.qp = &nesqp->ibqp;
Tatyana Nikolova784d1352012-05-11 14:29:32 -05002887 if (nesqp->ibqp.event_handler)
2888 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
Don Woodb29a4fc2009-09-05 20:36:39 -07002889 }
2890 }
2891
2892 if ((cm_id) && (cm_id->event_handler)) {
2893 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002894 atomic_inc(&cm_disconnects);
2895 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002896 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002897 cm_event.local_addr = cm_id->local_addr;
2898 cm_event.remote_addr = cm_id->remote_addr;
2899 cm_event.private_data = NULL;
2900 cm_event.private_data_len = 0;
2901
Faisal Latif6492cdf2008-07-24 20:50:45 -07002902 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302903 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2904 "cm_id = %p, refcount = %u.\n",
2905 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2906 nesqp->hwqp.sq_tail, cm_id,
2907 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002908
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002909 ret = cm_id->event_handler(cm_id, &cm_event);
2910 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002911 nes_debug(NES_DBG_CM, "OFA CM event_handler "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302912 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002913 }
2914
Don Woodb29a4fc2009-09-05 20:36:39 -07002915 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002916 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002917 nes_disconnect(nesqp, 1);
2918
2919 cm_id->provider_data = nesqp;
2920 /* Send up the close complete event */
2921 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002922 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002923 cm_event.provider_data = cm_id->provider_data;
2924 cm_event.local_addr = cm_id->local_addr;
2925 cm_event.remote_addr = cm_id->remote_addr;
2926 cm_event.private_data = NULL;
2927 cm_event.private_data_len = 0;
2928
2929 ret = cm_id->event_handler(cm_id, &cm_event);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302930 if (ret)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002931 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002932
2933 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002934 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002935 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002936
2937 return 0;
2938}
2939
2940
2941/**
2942 * nes_disconnect
2943 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002944static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002945{
2946 int ret = 0;
2947 struct nes_vnic *nesvnic;
2948 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002949 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002950
2951 nesvnic = to_nesvnic(nesqp->ibqp.device);
2952 if (!nesvnic)
2953 return -EINVAL;
2954
2955 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002956 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002957
2958 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00002959 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002960
2961 if (nesqp->active_conn) {
2962
2963 /* indicate this connection is NOT active */
2964 nesqp->active_conn = 0;
2965 } else {
2966 /* Need to free the Last Streaming Mode Message */
2967 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002968 if (nesqp->lsmm_mr)
2969 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002970 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302971 nesqp->private_data_len + nesqp->ietf_frame_size,
2972 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002973 }
2974 }
2975
2976 /* close the CM node down if it is still active */
2977 if (nesqp->cm_node) {
2978 nes_debug(NES_DBG_CM, "Call close API\n");
2979
2980 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002981 }
2982
2983 return ret;
2984}
2985
2986
2987/**
2988 * nes_accept
2989 */
2990int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2991{
2992 u64 u64temp;
2993 struct ib_qp *ibqp;
2994 struct nes_qp *nesqp;
2995 struct nes_vnic *nesvnic;
2996 struct nes_device *nesdev;
2997 struct nes_cm_node *cm_node;
2998 struct nes_adapter *adapter;
2999 struct ib_qp_attr attr;
3000 struct iw_cm_event cm_event;
3001 struct nes_hw_qp_wqe *wqe;
3002 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003003 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003004 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003005 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003006 struct nes_ib_device *nesibdev;
3007 struct ib_mr *ibmr = NULL;
3008 struct ib_phys_buf ibphysbuf;
3009 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07003010 u64 tagged_offset;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303011 u8 mpa_frame_offset = 0;
3012 struct ietf_mpa_v2 *mpa_v2_frame;
3013 u8 start_addr = 0;
3014 u8 *start_ptr = &start_addr;
3015 u8 **start_buff = &start_ptr;
3016 u16 buff_len = 0;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003017
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003018 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3019 if (!ibqp)
3020 return -EINVAL;
3021
3022 /* get all our handles */
3023 nesqp = to_nesqp(ibqp);
3024 nesvnic = to_nesvnic(nesqp->ibqp.device);
3025 nesdev = nesvnic->nesdev;
3026 adapter = nesdev->nesadapter;
3027
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003028 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003029 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
3030 "%s\n", cm_node, nesvnic, nesvnic->netdev,
3031 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003032
Faisal Latifc5a7d482009-12-09 15:54:08 -08003033 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
3034 if (cm_node->loopbackpartner)
3035 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
3036 rem_ref_cm_node(cm_node->cm_core, cm_node);
3037 return -EINVAL;
3038 }
3039
Faisal Latifdae58722010-08-14 21:04:56 +00003040 passive_state = atomic_add_return(1, &cm_node->passive_state);
3041 if (passive_state == NES_SEND_RESET_EVENT) {
3042 rem_ref_cm_node(cm_node->cm_core, cm_node);
3043 return -ECONNRESET;
3044 }
3045
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003046 /* associate the node with the QP */
3047 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003048 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003049
Faisal Latif6492cdf2008-07-24 20:50:45 -07003050 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
3051 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003052 atomic_inc(&cm_accepts);
3053
3054 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00003055 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003056
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303057 nesqp->ietf_frame_size = sizeof(struct ietf_mpa_v2);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003058 /* allocate the ietf frame and space for private data */
3059 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303060 nesqp->ietf_frame_size + conn_param->private_data_len,
3061 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003062
Faisal Latif6492cdf2008-07-24 20:50:45 -07003063 if (!nesqp->ietf_frame) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303064 nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07003065 return -ENOMEM;
3066 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303067 mpa_v2_frame = (struct ietf_mpa_v2 *)nesqp->ietf_frame;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003068
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303069 if (cm_node->mpa_frame_rev == IETF_MPA_V1)
3070 mpa_frame_offset = 4;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003071
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303072 memcpy(mpa_v2_frame->priv_data, conn_param->private_data,
3073 conn_param->private_data_len);
3074
3075 cm_build_mpa_frame(cm_node, start_buff, &buff_len, nesqp->ietf_frame, MPA_KEY_REPLY);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003076 nesqp->private_data_len = conn_param->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003077
Faisal Latif6492cdf2008-07-24 20:50:45 -07003078 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
3079 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003080
Faisal Latif6492cdf2008-07-24 20:50:45 -07003081 if (cm_id->remote_addr.sin_addr.s_addr !=
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303082 cm_id->local_addr.sin_addr.s_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003083 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003084 nesibdev = nesvnic->nesibdev;
3085 nespd = nesqp->nespd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303086 ibphysbuf.addr = nesqp->ietf_frame_pbase + mpa_frame_offset;
3087 ibphysbuf.size = buff_len;
3088 tagged_offset = (u64)(unsigned long)*start_buff;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003089 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303090 &ibphysbuf, 1,
3091 IB_ACCESS_LOCAL_WRITE,
3092 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003093 if (!ibmr) {
3094 nes_debug(NES_DBG_CM, "Unable to register memory region"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303095 "for lSMM for cm_node = %p \n",
3096 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07003097 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303098 nesqp->private_data_len + nesqp->ietf_frame_size,
3099 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003100 return -ENOMEM;
3101 }
3102
3103 ibmr->pd = &nespd->ibpd;
3104 ibmr->device = nespd->ibpd.device;
3105 nesqp->lsmm_mr = ibmr;
3106
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303107 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003108 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303109 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
3110 u64temp);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003111 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3112 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303113 NES_IWARP_SQ_WQE_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003114 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303115 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003116 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303117 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3118 (u64)(unsigned long)(*start_buff));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003119 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303120 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003121 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08003122 if (nesqp->sq_kmapped) {
3123 nesqp->sq_kmapped = 0;
3124 kunmap(nesqp->page);
3125 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003126
Faisal Latif6492cdf2008-07-24 20:50:45 -07003127 nesqp->nesqp_context->ird_ord_sizes |=
3128 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303129 NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003130 } else {
3131 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08003132 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003133 }
3134 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003135
3136
3137 /* Cache the cm_id in the qp */
3138 nesqp->cm_id = cm_id;
3139 cm_node->cm_id = cm_id;
3140
3141 /* nesqp->cm_node = (void *)cm_id->provider_data; */
3142 cm_id->provider_data = nesqp;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303143 nesqp->active_conn = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003144
Faisal Latif6492cdf2008-07-24 20:50:45 -07003145 if (cm_node->state == NES_CM_STATE_TSA)
3146 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303147 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003148
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003149 nes_cm_init_tsa_conn(nesqp, cm_node);
3150
Faisal Latif6492cdf2008-07-24 20:50:45 -07003151 nesqp->nesqp_context->tcpPorts[0] =
3152 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3153 nesqp->nesqp_context->tcpPorts[1] =
3154 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3155
3156 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3157 nesqp->nesqp_context->ip0 =
3158 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3159 else
3160 nesqp->nesqp_context->ip0 =
3161 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003162
3163 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303164 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3165 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003166
Faisal Latif6492cdf2008-07-24 20:50:45 -07003167 nesqp->nesqp_context->arp_index_vlan |=
3168 cpu_to_le32(nes_arp_table(nesdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303169 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
3170 NES_ARP_RESOLVE) << 16);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003171
3172 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003173 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003174
3175 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3176
3177 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003178 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
3179 nesqp->nesqp_context->ird_ord_sizes |=
3180 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003181
3182 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003183 nes_quad.DstIpAdrIndex =
3184 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3185 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3186 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3187 else
3188 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
3189 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3190 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003191
3192 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003193 crc_value = get_crc_value(&nes_quad);
3194 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003195 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303196 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003197
3198 nesqp->hte_index &= adapter->hte_index_mask;
3199 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3200
3201 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3202
Faisal Latif6492cdf2008-07-24 20:50:45 -07003203 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303204 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
3205 "private data length=%u.\n", nesqp->hwqp.qp_id,
3206 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3207 ntohs(cm_id->remote_addr.sin_port),
3208 ntohl(cm_id->local_addr.sin_addr.s_addr),
3209 ntohs(cm_id->local_addr.sin_port),
3210 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
3211 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
3212 buff_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003213
Coly Li73ac36e2009-01-07 18:09:16 -08003214 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003215 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07003216 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003217
3218 cm_event.event = IW_CM_EVENT_ESTABLISHED;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003219 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003220 cm_event.provider_data = (void *)nesqp;
3221 cm_event.local_addr = cm_id->local_addr;
3222 cm_event.remote_addr = cm_id->remote_addr;
3223 cm_event.private_data = NULL;
3224 cm_event.private_data_len = 0;
3225 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003226 attr.qp_state = IB_QPS_RTS;
3227 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003228 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003229 cm_node->loopbackpartner->mpa_frame_size =
3230 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003231 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003232 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303233 conn_param->private_data, conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003234 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
3235 }
3236 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003237 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303238 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003239
3240 return 0;
3241}
3242
3243
3244/**
3245 * nes_reject
3246 */
3247int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3248{
3249 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003250 struct nes_cm_node *loopback;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003251 struct nes_cm_core *cm_core;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303252 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003253
3254 atomic_inc(&cm_rejects);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303255 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003256 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003257 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003258 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003259
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303260 if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003261 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003262
Faisal Latif9d5ab132009-03-06 15:15:01 -08003263 if (loopback) {
3264 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3265 loopback->mpa_frame.priv_data_len = pdata_len;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303266 loopback->mpa_frame_size = pdata_len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003267 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303268 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
3269 cm_node->mpa_frame_size = pdata_len;
3270 memcpy(start_buff, pdata, pdata_len);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003271 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303272 return cm_core->api->reject(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003273}
3274
3275
3276/**
3277 * nes_connect
3278 * setup and launch cm connect node
3279 */
3280int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3281{
3282 struct ib_qp *ibqp;
3283 struct nes_qp *nesqp;
3284 struct nes_vnic *nesvnic;
3285 struct nes_device *nesdev;
3286 struct nes_cm_node *cm_node;
3287 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003288 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003289
3290 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3291 if (!ibqp)
3292 return -EINVAL;
3293 nesqp = to_nesqp(ibqp);
3294 if (!nesqp)
3295 return -EINVAL;
3296 nesvnic = to_nesvnic(nesqp->ibqp.device);
3297 if (!nesvnic)
3298 return -EINVAL;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303299 nesdev = nesvnic->nesdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003300 if (!nesdev)
3301 return -EINVAL;
3302
Faisal Latifc5a7d482009-12-09 15:54:08 -08003303 if (!(cm_id->local_addr.sin_port) || !(cm_id->remote_addr.sin_port))
3304 return -EINVAL;
3305
Faisal Latif6492cdf2008-07-24 20:50:45 -07003306 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303307 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3308 ntohl(nesvnic->local_ipaddr),
3309 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3310 ntohs(cm_id->remote_addr.sin_port),
3311 ntohl(cm_id->local_addr.sin_addr.s_addr),
3312 ntohs(cm_id->local_addr.sin_port));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003313
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003314 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003315 nesqp->active_conn = 1;
3316
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003317 /* cache the cm_id in the qp */
3318 nesqp->cm_id = cm_id;
3319
3320 cm_id->provider_data = nesqp;
3321
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003322 nesqp->private_data_len = conn_param->private_data_len;
3323 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
Tatyana Nikolovad3e51322012-05-11 10:46:16 -05003324 /* space for rdma0 read msg */
3325 if (conn_param->ord == 0)
3326 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(1);
3327
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003328 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003329 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303330 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003331
Faisal Latif6492cdf2008-07-24 20:50:45 -07003332 if (cm_id->local_addr.sin_addr.s_addr !=
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303333 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003334 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303335 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003336 apbvt_set = 1;
3337 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003338
3339 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003340 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3341 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3342 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3343 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003344 cm_info.cm_id = cm_id;
3345 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3346
3347 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003348
3349 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003350 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303351 conn_param->private_data_len, (void *)conn_param->private_data,
3352 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003353 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003354 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003355 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303356 PCI_FUNC(nesdev->pcidev->devfn),
3357 NES_MANAGE_APBVT_DEL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003358
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003359 cm_id->rem_ref(cm_id);
3360 return -ENOMEM;
3361 }
3362
Faisal Latif53094c32009-04-27 13:37:34 -07003363 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003364 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003365 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003366 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003367
3368 return 0;
3369}
3370
3371
3372/**
3373 * nes_create_listen
3374 */
3375int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3376{
3377 struct nes_vnic *nesvnic;
3378 struct nes_cm_listener *cm_node;
3379 struct nes_cm_info cm_info;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003380 int err;
3381
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003382 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3383 cm_id, ntohs(cm_id->local_addr.sin_port));
3384
3385 nesvnic = to_nesvnic(cm_id->device);
3386 if (!nesvnic)
3387 return -EINVAL;
Roland Dreier69d51022010-08-04 14:25:40 -07003388
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003389 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3390 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3391
3392 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3393 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3394
3395 /* setup listen params in our api call struct */
3396 cm_info.loc_addr = nesvnic->local_ipaddr;
3397 cm_info.loc_port = cm_id->local_addr.sin_port;
3398 cm_info.backlog = backlog;
3399 cm_info.cm_id = cm_id;
3400
3401 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3402
3403
3404 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3405 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003406 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303407 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003408 return -ENOMEM;
3409 }
3410
3411 cm_id->provider_data = cm_node;
3412
3413 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003414 err = nes_manage_apbvt(nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303415 ntohs(cm_id->local_addr.sin_port),
3416 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3417 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003418 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003419 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303420 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003421 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3422 return err;
3423 }
Faisal Latif6e10d2e2010-02-12 19:55:03 +00003424 atomic_inc(&cm_listens_created);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003425 }
3426
3427 cm_id->add_ref(cm_id);
3428 cm_id->provider_data = (void *)cm_node;
3429
3430
3431 return 0;
3432}
3433
3434
3435/**
3436 * nes_destroy_listen
3437 */
3438int nes_destroy_listen(struct iw_cm_id *cm_id)
3439{
3440 if (cm_id->provider_data)
3441 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3442 else
3443 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3444
3445 cm_id->rem_ref(cm_id);
3446
3447 return 0;
3448}
3449
3450
3451/**
3452 * nes_cm_recv
3453 */
3454int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3455{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003456 int rc = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303457
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003458 cm_packets_received++;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303459 if ((g_cm_core) && (g_cm_core->api))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003460 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303461 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003462 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303463 " cm is not setup properly.\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003464
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003465 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003466}
3467
3468
3469/**
3470 * nes_cm_start
3471 * Start and init a cm core module
3472 */
3473int nes_cm_start(void)
3474{
3475 nes_debug(NES_DBG_CM, "\n");
3476 /* create the primary CM core, pass this handle to subsequent core inits */
3477 g_cm_core = nes_cm_alloc_core();
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303478 if (g_cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003479 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303480 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003481 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003482}
3483
3484
3485/**
3486 * nes_cm_stop
3487 * stop and dealloc all cm core instances
3488 */
3489int nes_cm_stop(void)
3490{
3491 g_cm_core->api->destroy_cm_core(g_cm_core);
3492 return 0;
3493}
3494
3495
3496/**
3497 * cm_event_connected
3498 * handle a connected event, setup QPs and HW
3499 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003500static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003501{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003502 struct nes_qp *nesqp;
3503 struct nes_vnic *nesvnic;
3504 struct nes_device *nesdev;
3505 struct nes_cm_node *cm_node;
3506 struct nes_adapter *nesadapter;
3507 struct ib_qp_attr attr;
3508 struct iw_cm_id *cm_id;
3509 struct iw_cm_event cm_event;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003510 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003511 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003512 int ret;
3513
3514 /* get all our handles */
3515 cm_node = event->cm_node;
3516 cm_id = cm_node->cm_id;
3517 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3518 nesqp = (struct nes_qp *)cm_id->provider_data;
3519 nesvnic = to_nesvnic(nesqp->ibqp.device);
3520 nesdev = nesvnic->nesdev;
3521 nesadapter = nesdev->nesadapter;
3522
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303523 if (nesqp->destroyed)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003524 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003525 atomic_inc(&cm_connecteds);
3526 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303527 " local port 0x%04X. jiffies = %lu.\n",
3528 nesqp->hwqp.qp_id,
3529 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3530 ntohs(cm_id->remote_addr.sin_port),
3531 ntohs(cm_id->local_addr.sin_port),
3532 jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003533
3534 nes_cm_init_tsa_conn(nesqp, cm_node);
3535
3536 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003537 nesqp->nesqp_context->tcpPorts[0] =
3538 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3539 nesqp->nesqp_context->tcpPorts[1] =
3540 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3541 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3542 nesqp->nesqp_context->ip0 =
3543 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3544 else
3545 nesqp->nesqp_context->ip0 =
3546 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003547
3548 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003549 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3550 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003551 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003552 nes_arp_table(nesdev,
3553 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003554 NULL, NES_ARP_RESOLVE) << 16);
3555 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3556 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3557 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3558 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003559 cpu_to_le32((u32)1 <<
3560 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003561
3562 /* Adjust tail for not having a LSMM */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303563 /*nesqp->hwqp.sq_tail = 1;*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003564
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303565 build_rdma0_msg(cm_node, &nesqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003566
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303567 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3568 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003569
3570 memset(&nes_quad, 0, sizeof(nes_quad));
3571
Faisal Latif6492cdf2008-07-24 20:50:45 -07003572 nes_quad.DstIpAdrIndex =
3573 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3574 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3575 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3576 else
3577 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003578 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3579 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3580
3581 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003582 crc_value = get_crc_value(&nes_quad);
3583 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003584 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303585 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003586
3587 nesqp->hte_index &= nesadapter->hte_index_mask;
3588 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3589
3590 nesqp->ietf_frame = &cm_node->mpa_frame;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303591 nesqp->private_data_len = (u8)cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003592 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3593
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003594 /* notify OF layer we successfully created the requested connection */
3595 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003596 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003597 cm_event.provider_data = cm_id->provider_data;
3598 cm_event.local_addr.sin_family = AF_INET;
3599 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3600 cm_event.remote_addr = cm_id->remote_addr;
3601
Faisal Latif6492cdf2008-07-24 20:50:45 -07003602 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303603 cm_event.private_data_len = (u8)event->cm_node->mpa_frame_size;
3604 cm_event.ird = cm_node->ird_size;
3605 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003606
3607 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3608 ret = cm_id->event_handler(cm_id, &cm_event);
3609 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3610
3611 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003612 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303613 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003614 attr.qp_state = IB_QPS_RTS;
3615 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003616
Faisal Latif6492cdf2008-07-24 20:50:45 -07003617 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303618 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003619
3620 return;
3621}
3622
3623
3624/**
3625 * cm_event_connect_error
3626 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003627static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003628{
3629 struct nes_qp *nesqp;
3630 struct iw_cm_id *cm_id;
3631 struct iw_cm_event cm_event;
3632 /* struct nes_cm_info cm_info; */
3633 int ret;
3634
3635 if (!event->cm_node)
3636 return;
3637
3638 cm_id = event->cm_node->cm_id;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303639 if (!cm_id)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003640 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003641
3642 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3643 nesqp = cm_id->provider_data;
3644
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303645 if (!nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003646 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003647
3648 /* notify OF layer about this connection error event */
3649 /* cm_id->rem_ref(cm_id); */
3650 nesqp->cm_id = NULL;
3651 cm_id->provider_data = NULL;
3652 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003653 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003654 cm_event.provider_data = cm_id->provider_data;
3655 cm_event.local_addr = cm_id->local_addr;
3656 cm_event.remote_addr = cm_id->remote_addr;
3657 cm_event.private_data = NULL;
3658 cm_event.private_data_len = 0;
3659
Faisal Latif6492cdf2008-07-24 20:50:45 -07003660 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303661 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3662 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003663
3664 ret = cm_id->event_handler(cm_id, &cm_event);
3665 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3666 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003667 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303668 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003669 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003670
Faisal Latif6492cdf2008-07-24 20:50:45 -07003671 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003672 return;
3673}
3674
3675
3676/**
3677 * cm_event_reset
3678 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003679static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003680{
3681 struct nes_qp *nesqp;
3682 struct iw_cm_id *cm_id;
3683 struct iw_cm_event cm_event;
3684 /* struct nes_cm_info cm_info; */
3685 int ret;
3686
3687 if (!event->cm_node)
3688 return;
3689
3690 if (!event->cm_node->cm_id)
3691 return;
3692
3693 cm_id = event->cm_node->cm_id;
3694
3695 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3696 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003697 if (!nesqp)
3698 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003699
3700 nesqp->cm_id = NULL;
3701 /* cm_id->provider_data = NULL; */
3702 cm_event.event = IW_CM_EVENT_DISCONNECT;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003703 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003704 cm_event.provider_data = cm_id->provider_data;
3705 cm_event.local_addr = cm_id->local_addr;
3706 cm_event.remote_addr = cm_id->remote_addr;
3707 cm_event.private_data = NULL;
3708 cm_event.private_data_len = 0;
3709
Faisal Latif183ecfa2008-11-21 20:50:46 -06003710 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003711 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003712 atomic_inc(&cm_closes);
3713 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003714 cm_event.status = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003715 cm_event.provider_data = cm_id->provider_data;
3716 cm_event.local_addr = cm_id->local_addr;
3717 cm_event.remote_addr = cm_id->remote_addr;
3718 cm_event.private_data = NULL;
3719 cm_event.private_data_len = 0;
3720 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3721 ret = cm_id->event_handler(cm_id, &cm_event);
3722
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003723 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3724
3725
3726 /* notify OF layer about this connection error event */
3727 cm_id->rem_ref(cm_id);
3728
3729 return;
3730}
3731
3732
3733/**
3734 * cm_event_mpa_req
3735 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003736static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003737{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303738 struct iw_cm_id *cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003739 struct iw_cm_event cm_event;
3740 int ret;
3741 struct nes_cm_node *cm_node;
3742
3743 cm_node = event->cm_node;
3744 if (!cm_node)
3745 return;
3746 cm_id = cm_node->cm_id;
3747
3748 atomic_inc(&cm_connect_reqs);
3749 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303750 cm_node, cm_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003751
3752 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003753 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003754 cm_event.provider_data = (void *)cm_node;
3755
3756 cm_event.local_addr.sin_family = AF_INET;
3757 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3758 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3759
3760 cm_event.remote_addr.sin_family = AF_INET;
3761 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3762 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003763 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303764 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
3765 cm_event.ird = cm_node->ird_size;
3766 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003767
3768 ret = cm_id->event_handler(cm_id, &cm_event);
3769 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003770 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303771 __func__, __LINE__, ret);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003772 return;
3773}
3774
3775
3776static void cm_event_mpa_reject(struct nes_cm_event *event)
3777{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303778 struct iw_cm_id *cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003779 struct iw_cm_event cm_event;
3780 struct nes_cm_node *cm_node;
3781 int ret;
3782
3783 cm_node = event->cm_node;
3784 if (!cm_node)
3785 return;
3786 cm_id = cm_node->cm_id;
3787
3788 atomic_inc(&cm_connect_reqs);
3789 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303790 cm_node, cm_id, jiffies);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003791
3792 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3793 cm_event.status = -ECONNREFUSED;
3794 cm_event.provider_data = cm_id->provider_data;
3795
3796 cm_event.local_addr.sin_family = AF_INET;
3797 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3798 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3799
3800 cm_event.remote_addr.sin_family = AF_INET;
3801 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3802 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3803
3804 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303805 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003806
3807 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303808 "remove_addr=%08x\n",
3809 cm_event.local_addr.sin_addr.s_addr,
3810 cm_event.remote_addr.sin_addr.s_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003811
3812 ret = cm_id->event_handler(cm_id, &cm_event);
3813 if (ret)
3814 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303815 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003816
3817 return;
3818}
3819
3820
3821static void nes_cm_event_handler(struct work_struct *);
3822
3823/**
3824 * nes_cm_post_event
3825 * post an event to the cm event handler
3826 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003827static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003828{
3829 atomic_inc(&event->cm_node->cm_core->events_posted);
3830 add_ref_cm_node(event->cm_node);
3831 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3832 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003833 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303834 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003835
3836 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3837
3838 nes_debug(NES_DBG_CM, "Exit\n");
3839 return 0;
3840}
3841
3842
3843/**
3844 * nes_cm_event_handler
3845 * worker function to handle cm events
3846 * will free instance of nes_cm_event
3847 */
3848static void nes_cm_event_handler(struct work_struct *work)
3849{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003850 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303851 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003852 struct nes_cm_core *cm_core;
3853
Faisal Latif6492cdf2008-07-24 20:50:45 -07003854 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003855 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003856
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003857 cm_core = event->cm_node->cm_core;
3858 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303859 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003860
3861 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003862 case NES_CM_EVENT_MPA_REQ:
3863 cm_event_mpa_req(event);
3864 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303865 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003866 break;
3867 case NES_CM_EVENT_RESET:
3868 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303869 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003870 cm_event_reset(event);
3871 break;
3872 case NES_CM_EVENT_CONNECTED:
3873 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303874 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003875 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003876 cm_event_connected(event);
3877 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3878 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003879 case NES_CM_EVENT_MPA_REJECT:
3880 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303881 (event->cm_node->state == NES_CM_STATE_TSA))
Faisal Latif9d5ab132009-03-06 15:15:01 -08003882 break;
3883 cm_event_mpa_reject(event);
3884 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3885 break;
3886
Faisal Latif6492cdf2008-07-24 20:50:45 -07003887 case NES_CM_EVENT_ABORTED:
3888 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303889 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003890 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003891 cm_event_connect_error(event);
3892 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3893 break;
3894 case NES_CM_EVENT_DROPPED_PKT:
3895 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3896 break;
3897 default:
3898 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3899 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003900 }
3901
3902 atomic_dec(&cm_core->events_posted);
3903 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3904 rem_ref_cm_node(cm_core, event->cm_node);
3905 kfree(event);
3906
3907 return;
3908}