blob: cfaacaf6bf5f9c8e11ce0bb5c0dce62c54e15ae4 [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;
Tatyana Nikolovafc4ba722012-09-20 20:55:33 +0000433 if (!(cm_node->netdev->features & NETIF_F_IP_CSUM))
434 skb->ip_summed = CHECKSUM_NONE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800435 skb->protocol = htons(0x800);
436 skb->data_len = 0;
437 skb->mac_len = ETH_HLEN;
438
439 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
440 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
441 ethh->h_proto = htons(0x0800);
442
443 iph->version = IPVERSION;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530444 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800445 iph->tos = 0;
446 iph->tot_len = htons(packetsize);
447 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
448
449 iph->frag_off = htons(0x4000);
450 iph->ttl = 0x40;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530451 iph->protocol = 0x06; /* IPPROTO_TCP */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800452
453 iph->saddr = htonl(cm_node->loc_addr);
454 iph->daddr = htonl(cm_node->rem_addr);
455
456 tcph->source = htons(cm_node->loc_port);
457 tcph->dest = htons(cm_node->rem_port);
458 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
459
460 if (flags & SET_ACK) {
461 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
462 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
463 tcph->ack = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530464 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800465 tcph->ack_seq = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530466 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800467
468 if (flags & SET_SYN) {
469 cm_node->tcp_cntxt.loc_seq_num++;
470 tcph->syn = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530471 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700472 cm_node->tcp_cntxt.loc_seq_num += datasize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530473 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800474
Faisal Latif6492cdf2008-07-24 20:50:45 -0700475 if (flags & SET_FIN) {
476 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800477 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700478 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800479
480 if (flags & SET_RST)
481 tcph->rst = 1;
482
483 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
484 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
485 tcph->urg_ptr = 0;
486 if (optionsize)
487 memcpy(buf, options, optionsize);
488 buf += optionsize;
489 if (datasize)
490 memcpy(buf, data, datasize);
491
492 skb_shinfo(skb)->nr_frags = 0;
493 cm_packets_created++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800494}
495
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800496/**
497 * print_core - dump a cm core
498 */
499static void print_core(struct nes_cm_core *core)
500{
501 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
502 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
503 if (!core)
504 return;
505 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800506
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530507 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800508
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800509 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
510 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
511
512 nes_debug(NES_DBG_CM, "core : %p \n", core);
513
514 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
515}
516
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530517/**
518 * cm_build_mpa_frame - build a MPA V1 frame or MPA V2 frame
519 */
520static int cm_build_mpa_frame(struct nes_cm_node *cm_node, u8 **start_buff,
521 u16 *buff_len, u8 *pci_mem, u8 mpa_key)
522{
523 int ret = 0;
524
525 *start_buff = (pci_mem) ? pci_mem : &cm_node->mpa_frame_buf[0];
526
527 switch (cm_node->mpa_frame_rev) {
528 case IETF_MPA_V1:
529 *start_buff = (u8 *)*start_buff + sizeof(struct ietf_rtr_msg);
530 *buff_len = sizeof(struct ietf_mpa_v1) + cm_node->mpa_frame_size;
531 build_mpa_v1(cm_node, *start_buff, mpa_key);
532 break;
533 case IETF_MPA_V2:
534 *buff_len = sizeof(struct ietf_mpa_v2) + cm_node->mpa_frame_size;
535 build_mpa_v2(cm_node, *start_buff, mpa_key);
536 break;
537 default:
538 ret = -EINVAL;
539 }
540 return ret;
541}
542
543/**
544 * build_mpa_v2 - build a MPA V2 frame
545 */
546static void build_mpa_v2(struct nes_cm_node *cm_node,
547 void *start_addr, u8 mpa_key)
548{
549 struct ietf_mpa_v2 *mpa_frame = (struct ietf_mpa_v2 *)start_addr;
550 struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800551 u16 ctrl_ird;
552 u16 ctrl_ord;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530553
554 /* initialize the upper 5 bytes of the frame */
555 build_mpa_v1(cm_node, start_addr, mpa_key);
556 mpa_frame->flags |= IETF_MPA_V2_FLAG; /* set a bit to indicate MPA V2 */
557 mpa_frame->priv_data_len += htons(IETF_RTR_MSG_SIZE);
558
559 /* initialize RTR msg */
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800560 ctrl_ird = (cm_node->ird_size > IETF_NO_IRD_ORD) ?
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530561 IETF_NO_IRD_ORD : cm_node->ird_size;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800562 ctrl_ord = (cm_node->ord_size > IETF_NO_IRD_ORD) ?
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530563 IETF_NO_IRD_ORD : cm_node->ord_size;
564
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800565 ctrl_ird |= IETF_PEER_TO_PEER;
566 ctrl_ird |= IETF_FLPDU_ZERO_LEN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530567
568 switch (mpa_key) {
569 case MPA_KEY_REQUEST:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800570 ctrl_ord |= IETF_RDMA0_WRITE;
571 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530572 break;
573 case MPA_KEY_REPLY:
574 switch (cm_node->send_rdma0_op) {
575 case SEND_RDMA_WRITE_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800576 ctrl_ord |= IETF_RDMA0_WRITE;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530577 break;
578 case SEND_RDMA_READ_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800579 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530580 break;
581 }
582 }
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800583 rtr_msg->ctrl_ird = htons(ctrl_ird);
584 rtr_msg->ctrl_ord = htons(ctrl_ord);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530585}
586
587/**
588 * build_mpa_v1 - build a MPA V1 frame
589 */
590static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
591{
592 struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
593
594 switch (mpa_key) {
595 case MPA_KEY_REQUEST:
596 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
597 break;
598 case MPA_KEY_REPLY:
599 memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
600 break;
601 }
602 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
603 mpa_frame->rev = cm_node->mpa_frame_rev;
604 mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
605}
606
607static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
608{
609 u64 u64temp;
610 struct nes_qp *nesqp = *nesqp_addr;
611 struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
612
613 u64temp = (unsigned long)nesqp;
614 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
615 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
616
617 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
618 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
619
620 switch (cm_node->send_rdma0_op) {
621 case SEND_RDMA_WRITE_ZERO:
622 nes_debug(NES_DBG_CM, "Sending first write.\n");
623 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
624 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
625 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
626 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
627 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
628 break;
629
630 case SEND_RDMA_READ_ZERO:
631 default:
632 if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO) {
633 printk(KERN_ERR "%s[%u]: Unsupported RDMA0 len operation=%u\n",
634 __func__, __LINE__, cm_node->send_rdma0_op);
635 WARN_ON(1);
636 }
637 nes_debug(NES_DBG_CM, "Sending first rdma operation.\n");
638 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
639 cpu_to_le32(NES_IWARP_SQ_OP_RDMAR);
640 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX] = 1;
641 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_HIGH_IDX] = 0;
642 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] = 0;
643 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_STAG_IDX] = 1;
644 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 1;
645 break;
646 }
647
648 if (nesqp->sq_kmapped) {
649 nesqp->sq_kmapped = 0;
650 kunmap(nesqp->page);
651 }
652
653 /*use the reserved spot on the WQ for the extra first WQE*/
654 nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
655 NES_QPCONTEXT_ORDIRD_WRPDU |
656 NES_QPCONTEXT_ORDIRD_ALSMM));
657 nesqp->skip_lsmm = 1;
658 nesqp->hwqp.sq_tail = 0;
659}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800660
661/**
662 * schedule_nes_timer
663 * note - cm_node needs to be protected before calling this. Encase in:
664 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
665 */
666int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530667 enum nes_timer_type type, int send_retrans,
668 int close_when_complete)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800669{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530670 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700671 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800672 struct nes_timer_entry *new_send;
673 int ret = 0;
674 u32 was_timer_set;
675
676 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
677 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800678 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800679
680 /* new_send->timetosend = currenttime */
681 new_send->retrycount = NES_DEFAULT_RETRYS;
682 new_send->retranscount = NES_DEFAULT_RETRANS;
683 new_send->skb = skb;
684 new_send->timetosend = jiffies;
685 new_send->type = type;
686 new_send->netdev = cm_node->netdev;
687 new_send->send_retrans = send_retrans;
688 new_send->close_when_complete = close_when_complete;
689
690 if (type == NES_TIMER_TYPE_CLOSE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530691 new_send->timetosend += (HZ / 10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800692 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700693 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800694 WARN_ON(1);
695 return -EINVAL;
696 }
697 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800698 }
699
700 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700701 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800702 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700703 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
704 cm_node->send_entry = new_send;
705 add_ref_cm_node(cm_node);
706 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
707 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800708
709 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
710 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700711 nes_debug(NES_DBG_CM, "Error sending packet %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530712 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800713 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700714 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800715 } else {
716 cm_packets_sent++;
717 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700718 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800719 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700720 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800721 return ret;
722 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800723 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800724 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800725
726 was_timer_set = timer_pending(&cm_core->tcp_timer);
727
728 if (!was_timer_set) {
729 cm_core->tcp_timer.expires = new_send->timetosend;
730 add_timer(&cm_core->tcp_timer);
731 }
732
733 return ret;
734}
735
Faisal Latif9d5ab132009-03-06 15:15:01 -0800736static void nes_retrans_expired(struct nes_cm_node *cm_node)
737{
Faisal Latif68237a02009-06-22 22:53:28 -0700738 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latifdae58722010-08-14 21:04:56 +0000739 enum nes_cm_node_state state = cm_node->state;
740 cm_node->state = NES_CM_STATE_CLOSED;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530741
Faisal Latifdae58722010-08-14 21:04:56 +0000742 switch (state) {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800743 case NES_CM_STATE_SYN_RCVD:
744 case NES_CM_STATE_CLOSING:
745 rem_ref_cm_node(cm_node->cm_core, cm_node);
746 break;
747 case NES_CM_STATE_LAST_ACK:
748 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700749 if (cm_node->cm_id)
750 cm_id->rem_ref(cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800751 send_reset(cm_node, NULL);
752 break;
753 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800754 add_ref_cm_node(cm_node);
755 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800756 create_event(cm_node, NES_CM_EVENT_ABORTED);
757 }
758}
759
760static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
761{
762 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
763 struct iw_cm_id *cm_id = cm_node->cm_id;
764 struct nes_qp *nesqp;
765 unsigned long qplockflags;
766
767 if (!recv_entry)
768 return;
769 nesqp = (struct nes_qp *)recv_entry->skb;
770 if (nesqp) {
771 spin_lock_irqsave(&nesqp->lock, qplockflags);
772 if (nesqp->cm_id) {
773 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530774 "refcount = %d: HIT A "
775 "NES_TIMER_TYPE_CLOSE with something "
776 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
777 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800778 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
779 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
780 nesqp->ibqp_state = IB_QPS_ERR;
781 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
782 nes_cm_disconn(nesqp);
783 } else {
784 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
785 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530786 "refcount = %d: HIT A "
787 "NES_TIMER_TYPE_CLOSE with nothing "
788 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
789 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800790 }
791 } else if (rem_node) {
792 /* TIME_WAIT state */
793 rem_ref_cm_node(cm_node->cm_core, cm_node);
794 }
795 if (cm_node->cm_id)
796 cm_id->rem_ref(cm_id);
797 kfree(recv_entry);
798 cm_node->recv_entry = NULL;
799}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800800
801/**
802 * nes_cm_timer_tick
803 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700804static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800805{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800806 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800807 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800808 struct nes_cm_node *cm_node;
809 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800810 struct list_head *list_core_temp;
811 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800812 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800813 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700814 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800815 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800816
Faisal Latif879e5bd2008-11-21 20:50:41 -0600817 struct list_head timer_list;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530818
Faisal Latif879e5bd2008-11-21 20:50:41 -0600819 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800820 spin_lock_irqsave(&cm_core->ht_lock, flags);
821
Faisal Latif6492cdf2008-07-24 20:50:45 -0700822 list_for_each_safe(list_node, list_core_temp,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530823 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800824 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800825 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600826 add_ref_cm_node(cm_node);
827 list_add(&cm_node->timer_entry, &timer_list);
828 }
829 }
830 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
831
832 list_for_each_safe(list_node, list_core_temp, &timer_list) {
833 cm_node = container_of(list_node, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530834 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800835 recv_entry = cm_node->recv_entry;
836
837 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700838 if (time_after(recv_entry->timetosend, jiffies)) {
839 if (nexttimeout > recv_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530840 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800841 nexttimeout = recv_entry->timetosend;
842 settimer = 1;
843 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530844 } else {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800845 handle_recv_entry(cm_node, 1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530846 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800847 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800848
849 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700850 do {
851 send_entry = cm_node->send_entry;
852 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600853 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800854 if (time_after(send_entry->timetosend, jiffies)) {
855 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700856 if ((nexttimeout >
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530857 send_entry->timetosend) ||
858 !settimer) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700859 nexttimeout =
860 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800861 settimer = 1;
862 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800863 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700864 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800865 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800866 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800867 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700868
869 if ((cm_node->state == NES_CM_STATE_TSA) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530870 (cm_node->state == NES_CM_STATE_CLOSED)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700871 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600872 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800873 }
874
Faisal Latif6492cdf2008-07-24 20:50:45 -0700875 if (!send_entry->retranscount ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530876 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800877 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700878 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800879
Faisal Latif6492cdf2008-07-24 20:50:45 -0700880 spin_unlock_irqrestore(
881 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800882 nes_retrans_expired(cm_node);
883 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700884 spin_lock_irqsave(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530885 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600886 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800887 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800888 atomic_inc(&send_entry->skb->users);
889 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700890 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530891 "for node %p, jiffies = %lu, time to send = "
892 "%lu, retranscount = %u, send_entry->seq_num = "
893 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
894 "0x%08X\n", send_entry, cm_node, jiffies,
895 send_entry->timetosend,
896 send_entry->retranscount,
897 send_entry->seq_num,
898 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800899
Faisal Latif6492cdf2008-07-24 20:50:45 -0700900 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530901 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800902 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700903 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800904 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700905 nes_debug(NES_DBG_CM, "rexmit failed for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530906 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800907 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800908 send_entry->retrycount--;
909 nexttimeout = jiffies + NES_SHORT_TIME;
910 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600911 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800912 } else {
913 cm_packets_sent++;
914 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700915 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530916 "%u, retry count = %u.\n",
917 send_entry->retranscount,
918 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800919 if (send_entry->send_retrans) {
920 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700921 timetosend = (NES_RETRY_TIMEOUT <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530922 (NES_DEFAULT_RETRANS - send_entry->retranscount));
Faisal Latif4e9c3902009-04-27 13:39:36 -0700923
Faisal Latif6492cdf2008-07-24 20:50:45 -0700924 send_entry->timetosend = jiffies +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530925 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700926 if (nexttimeout > send_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530927 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800928 nexttimeout = send_entry->timetosend;
929 settimer = 1;
930 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800931 } else {
932 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700933 close_when_complete =
934 send_entry->close_when_complete;
935 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530936 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700937 free_retrans_entry(cm_node);
938 if (close_when_complete)
939 rem_ref_cm_node(cm_node->cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530940 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800941 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700942 } while (0);
943
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800944 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700945 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800946 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800947
948 if (settimer) {
949 if (!timer_pending(&cm_core->tcp_timer)) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530950 cm_core->tcp_timer.expires = nexttimeout;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800951 add_timer(&cm_core->tcp_timer);
952 }
953 }
954}
955
956
957/**
958 * send_syn
959 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700960static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530961 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800962{
963 int ret;
964 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800965 char optionsbuffer[sizeof(struct option_mss) +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530966 sizeof(struct option_windowscale) + sizeof(struct option_base) +
967 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800968
969 int optionssize = 0;
970 /* Sending MSS option */
971 union all_known_options *options;
972
973 if (!cm_node)
974 return -EINVAL;
975
976 options = (union all_known_options *)&optionsbuffer[optionssize];
977 options->as_mss.optionnum = OPTION_NUMBER_MSS;
978 options->as_mss.length = sizeof(struct option_mss);
979 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
980 optionssize += sizeof(struct option_mss);
981
982 options = (union all_known_options *)&optionsbuffer[optionssize];
983 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
984 options->as_windowscale.length = sizeof(struct option_windowscale);
985 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
986 optionssize += sizeof(struct option_windowscale);
987
Faisal Latif6492cdf2008-07-24 20:50:45 -0700988 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800989 options = (union all_known_options *)&optionsbuffer[optionssize];
990 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
991 options->as_base.length = sizeof(struct option_base);
992 optionssize += sizeof(struct option_base);
993 /* we need the size to be a multiple of 4 */
994 options = (union all_known_options *)&optionsbuffer[optionssize];
995 options->as_end = 1;
996 optionssize += 1;
997 options = (union all_known_options *)&optionsbuffer[optionssize];
998 options->as_end = 1;
999 optionssize += 1;
1000 }
1001
1002 options = (union all_known_options *)&optionsbuffer[optionssize];
1003 options->as_end = OPTION_NUMBER_END;
1004 optionssize += 1;
1005
Faisal Latif6492cdf2008-07-24 20:50:45 -07001006 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001007 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001008 if (!skb) {
1009 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1010 return -1;
1011 }
1012
1013 if (sendack)
1014 flags |= SET_ACK;
1015
1016 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
1017 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1018
1019 return ret;
1020}
1021
1022
1023/**
1024 * send_reset
1025 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001026static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001027{
1028 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001029 int flags = SET_RST | SET_ACK;
1030
Faisal Latif6492cdf2008-07-24 20:50:45 -07001031 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001032 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001033 if (!skb) {
1034 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001035 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001036 }
1037
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001038 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
1039 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
1040
1041 return ret;
1042}
1043
1044
1045/**
1046 * send_ack
1047 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001048static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001049{
1050 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001051
1052 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001053 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001054
1055 if (!skb) {
1056 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1057 return -1;
1058 }
1059
1060 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
1061 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
1062
1063 return ret;
1064}
1065
1066
1067/**
1068 * send_fin
1069 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07001070static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001071{
1072 int ret;
1073
1074 /* if we didn't get a frame get one */
1075 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001076 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001077
1078 if (!skb) {
1079 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1080 return -1;
1081 }
1082
1083 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
1084 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1085
1086 return ret;
1087}
1088
1089
1090/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001091 * find_node - find a cm node that matches the reference cm node
1092 */
1093static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301094 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001095{
1096 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001097 struct list_head *hte;
1098 struct nes_cm_node *cm_node;
1099
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001100 /* get a handle on the hte */
1101 hte = &cm_core->connected_nodes;
1102
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001103 /* walk list and find cm_node associated with this session ID */
1104 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001105 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001106 /* compare quad, return node handle if a match */
1107 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301108 cm_node->loc_addr, cm_node->loc_port,
1109 loc_addr, loc_port,
1110 cm_node->rem_addr, cm_node->rem_port,
1111 rem_addr, rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001112 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301113 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001114 add_ref_cm_node(cm_node);
1115 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1116 return cm_node;
1117 }
1118 }
1119 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1120
1121 /* no owner node */
1122 return NULL;
1123}
1124
1125
1126/**
1127 * find_listener - find a cm node listening on this addr-port pair
1128 */
1129static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301130 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001131{
1132 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001133 struct nes_cm_listener *listen_node;
1134
1135 /* walk list and find cm_node associated with this session ID */
1136 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001137 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001138 /* compare node pair, return node handle if a match */
1139 if (((listen_node->loc_addr == dst_addr) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301140 listen_node->loc_addr == 0x00000000) &&
1141 (listen_node->loc_port == dst_port) &&
1142 (listener_state & listen_node->listener_state)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001143 atomic_inc(&listen_node->ref_count);
1144 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1145 return listen_node;
1146 }
1147 }
1148 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1149
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001150 /* no listener */
1151 return NULL;
1152}
1153
1154
1155/**
1156 * add_hte_node - add a cm node to the hash table
1157 */
1158static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1159{
1160 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001161 struct list_head *hte;
1162
1163 if (!cm_node || !cm_core)
1164 return -EINVAL;
1165
Faisal Latif6492cdf2008-07-24 20:50:45 -07001166 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301167 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001168
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001169 spin_lock_irqsave(&cm_core->ht_lock, flags);
1170
1171 /* get a handle on the hash table element (list head for this slot) */
1172 hte = &cm_core->connected_nodes;
1173 list_add_tail(&cm_node->list, hte);
1174 atomic_inc(&cm_core->ht_node_cnt);
1175
1176 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1177
1178 return 0;
1179}
1180
1181
1182/**
1183 * mini_cm_dec_refcnt_listen
1184 */
1185static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301186 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001187{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001188 int ret = -EINVAL;
1189 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001190 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001191 struct list_head *list_pos = NULL;
1192 struct list_head *list_temp = NULL;
1193 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -06001194 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001195
1196 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301197 "refcnt=%d\n", listener, free_hanging_nodes,
1198 atomic_read(&listener->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001199 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -06001200 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001201 if (free_hanging_nodes) {
1202 spin_lock_irqsave(&cm_core->ht_lock, flags);
1203 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -06001204 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001205 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301206 list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001207 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -06001208 (!cm_node->accelerated)) {
1209 add_ref_cm_node(cm_node);
1210 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001211 }
1212 }
1213 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1214 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001215
1216 list_for_each_safe(list_pos, list_temp, &reset_list) {
1217 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301218 reset_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001219 {
1220 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -08001221 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001222 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
1223 rem_ref_cm_node(cm_node->cm_core, cm_node);
1224 } else {
1225 if (!loopback) {
1226 cleanup_retrans_entry(cm_node);
1227 err = send_reset(cm_node, NULL);
1228 if (err) {
1229 cm_node->state =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301230 NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001231 WARN_ON(1);
1232 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08001233 old_state = cm_node->state;
1234 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1235 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1236 rem_ref_cm_node(
1237 cm_node->cm_core,
1238 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001239 }
1240 } else {
1241 struct nes_cm_event event;
1242
1243 event.cm_node = loopback;
1244 event.cm_info.rem_addr =
1245 loopback->rem_addr;
1246 event.cm_info.loc_addr =
1247 loopback->loc_addr;
1248 event.cm_info.rem_port =
1249 loopback->rem_port;
1250 event.cm_info.loc_port =
1251 loopback->loc_port;
1252 event.cm_info.cm_id = loopback->cm_id;
Faisal Latif43093b92010-02-12 19:57:04 +00001253 add_ref_cm_node(loopback);
1254 loopback->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001255 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001256 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001257
Faisal Latif9d5ab132009-03-06 15:15:01 -08001258 rem_ref_cm_node(cm_node->cm_core,
1259 cm_node);
1260
1261 }
1262 }
1263 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001264 }
1265
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001266 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1267 if (!atomic_dec_return(&listener->ref_count)) {
1268 list_del(&listener->list);
1269
1270 /* decrement our listen node count */
1271 atomic_dec(&cm_core->listen_node_cnt);
1272
1273 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1274
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301275 if (listener->nesvnic)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001276 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301277 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001278
1279 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1280
1281 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001282 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001283 ret = 0;
Faisal Latif6e10d2e2010-02-12 19:55:03 +00001284 atomic_inc(&cm_listens_destroyed);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001285 } else {
1286 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1287 }
1288 if (listener) {
1289 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1290 nes_debug(NES_DBG_CM, "destroying listener (%p)"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301291 " with non-zero pending accepts=%u\n",
1292 listener, atomic_read(&listener->pend_accepts_cnt));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001293 }
1294
1295 return ret;
1296}
1297
1298
1299/**
1300 * mini_cm_del_listen
1301 */
1302static int mini_cm_del_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301303 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001304{
1305 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1306 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1307 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1308}
1309
1310
1311/**
1312 * mini_cm_accelerated
1313 */
1314static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301315 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001316{
1317 u32 was_timer_set;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301318
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001319 cm_node->accelerated = 1;
1320
1321 if (cm_node->accept_pend) {
1322 BUG_ON(!cm_node->listener);
1323 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001324 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001325 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1326 }
1327
1328 was_timer_set = timer_pending(&cm_core->tcp_timer);
1329 if (!was_timer_set) {
1330 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1331 add_timer(&cm_core->tcp_timer);
1332 }
1333
1334 return 0;
1335}
1336
1337
1338/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001339 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001340 */
Faisal Latif7a576df2009-12-09 15:54:33 -08001341static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001342{
1343 struct rtable *rt;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001344 struct neighbour *neigh;
Faisal Latif7a576df2009-12-09 15:54:33 -08001345 int rc = arpindex;
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001346 struct net_device *netdev;
Faisal Latif7a576df2009-12-09 15:54:33 -08001347 struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001348
David S. Miller78fbfd82011-03-12 00:00:52 -05001349 rt = ip_route_output(&init_net, htonl(dst_ip), 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001350 if (IS_ERR(rt)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001351 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301352 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001353 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001354 }
1355
Roland Dreiercf55bb22011-03-24 17:13:20 -07001356 if (netif_is_bond_slave(nesvnic->netdev))
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001357 netdev = nesvnic->netdev->master;
1358 else
1359 netdev = nesvnic->netdev;
1360
Tatyana Nikolovaef3d0c42012-09-20 19:23:14 +00001361 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, netdev);
David Millere55684f2012-01-24 13:16:03 +00001362
David Miller40e2bb52011-12-02 16:52:27 +00001363 rcu_read_lock();
Bob Sharp7191a0a2008-10-03 12:21:19 -07001364 if (neigh) {
1365 if (neigh->nud_state & NUD_VALID) {
1366 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001367 " is %pM, Gateway is 0x%08X \n", dst_ip,
1368 neigh->ha, ntohl(rt->rt_gateway));
Faisal Latif7a576df2009-12-09 15:54:33 -08001369
1370 if (arpindex >= 0) {
1371 if (!memcmp(nesadapter->arp_table[arpindex].mac_addr,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301372 neigh->ha, ETH_ALEN)) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001373 /* Mac address same as in nes_arp_table */
David Millere55684f2012-01-24 13:16:03 +00001374 goto out;
Faisal Latif7a576df2009-12-09 15:54:33 -08001375 }
1376
1377 nes_manage_arp_cache(nesvnic->netdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301378 nesadapter->arp_table[arpindex].mac_addr,
1379 dst_ip, NES_ARP_DELETE);
Faisal Latif7a576df2009-12-09 15:54:33 -08001380 }
1381
Bob Sharp7191a0a2008-10-03 12:21:19 -07001382 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1383 dst_ip, NES_ARP_ADD);
1384 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1385 NES_ARP_RESOLVE);
David Miller40e2bb52011-12-02 16:52:27 +00001386 } else {
1387 neigh_event_send(neigh, NULL);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001388 }
Bob Sharp7191a0a2008-10-03 12:21:19 -07001389 }
David Millere55684f2012-01-24 13:16:03 +00001390out:
David Miller40e2bb52011-12-02 16:52:27 +00001391 rcu_read_unlock();
David Millere55684f2012-01-24 13:16:03 +00001392
1393 if (neigh)
1394 neigh_release(neigh);
1395
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001396 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001397 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001398}
1399
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001400/**
1401 * make_cm_node - create a new instance of a cm node
1402 */
1403static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301404 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1405 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001406{
1407 struct nes_cm_node *cm_node;
1408 struct timespec ts;
Faisal Latif7a576df2009-12-09 15:54:33 -08001409 int oldarpindex = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001410 int arpindex = 0;
1411 struct nes_device *nesdev;
1412 struct nes_adapter *nesadapter;
1413
1414 /* create an hte and cm_node for this instance */
1415 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1416 if (!cm_node)
1417 return NULL;
1418
1419 /* set our node specific transport info */
1420 cm_node->loc_addr = cm_info->loc_addr;
1421 cm_node->rem_addr = cm_info->rem_addr;
1422 cm_node->loc_port = cm_info->loc_port;
1423 cm_node->rem_port = cm_info->rem_port;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301424
1425 cm_node->mpa_frame_rev = mpa_version;
1426 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
1427 cm_node->ird_size = IETF_NO_IRD_ORD;
1428 cm_node->ord_size = IETF_NO_IRD_ORD;
1429
Harvey Harrison63779432008-10-31 00:56:00 -07001430 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1431 &cm_node->loc_addr, cm_node->loc_port,
1432 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001433 cm_node->listener = listener;
1434 cm_node->netdev = nesvnic->netdev;
1435 cm_node->cm_id = cm_info->cm_id;
1436 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1437
Faisal Latif6492cdf2008-07-24 20:50:45 -07001438 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301439 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001440
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001441 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001442
1443 cm_node->loopbackpartner = NULL;
1444 atomic_set(&cm_node->ref_count, 1);
1445 /* associate our parent CM core */
1446 cm_node->cm_core = cm_core;
1447 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1448 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1449 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301450 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001451 ts = current_kernel_time();
1452 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1453 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301454 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001455 cm_node->tcp_cntxt.rcv_nxt = 0;
1456 /* get a unique session ID , add thread_id to an upcounter to handle race */
1457 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001458 cm_node->conn_type = cm_info->conn_type;
1459 cm_node->apbvt_set = 0;
1460 cm_node->accept_pend = 0;
1461
1462 cm_node->nesvnic = nesvnic;
1463 /* get some device handles, for arp lookup */
1464 nesdev = nesvnic->nesdev;
1465 nesadapter = nesdev->nesadapter;
1466
1467 cm_node->loopbackpartner = NULL;
Faisal Latif7a576df2009-12-09 15:54:33 -08001468
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001469 /* get the mac addr for the remote node */
Tatyana Nikolovaef3d0c42012-09-20 19:23:14 +00001470 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1471 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001472 if (arpindex < 0) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001473 kfree(cm_node);
1474 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001475 }
1476
1477 /* copy the mac addr to node context */
1478 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001479 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1480 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001481
1482 add_hte_node(cm_core, cm_node);
1483 atomic_inc(&cm_nodes_created);
1484
1485 return cm_node;
1486}
1487
1488
1489/**
1490 * add_ref_cm_node - destroy an instance of a cm node
1491 */
1492static int add_ref_cm_node(struct nes_cm_node *cm_node)
1493{
1494 atomic_inc(&cm_node->ref_count);
1495 return 0;
1496}
1497
1498
1499/**
1500 * rem_ref_cm_node - destroy an instance of a cm node
1501 */
1502static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301503 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001504{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001505 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001506 struct nes_qp *nesqp;
1507
1508 if (!cm_node)
1509 return -EINVAL;
1510
1511 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1512 if (atomic_dec_return(&cm_node->ref_count)) {
1513 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1514 return 0;
1515 }
1516 list_del(&cm_node->list);
1517 atomic_dec(&cm_core->ht_node_cnt);
1518 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1519
1520 /* if the node is destroyed before connection was accelerated */
1521 if (!cm_node->accelerated && cm_node->accept_pend) {
1522 BUG_ON(!cm_node->listener);
1523 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1524 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1525 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001526 WARN_ON(cm_node->send_entry);
1527 if (cm_node->recv_entry)
1528 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001529 if (cm_node->listener) {
1530 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1531 } else {
1532 if (cm_node->apbvt_set && cm_node->nesvnic) {
1533 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301534 PCI_FUNC(
1535 cm_node->nesvnic->nesdev->pcidev->devfn),
1536 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001537 }
1538 }
1539
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001540 atomic_dec(&cm_core->node_cnt);
1541 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001542 nesqp = cm_node->nesqp;
1543 if (nesqp) {
1544 nesqp->cm_node = NULL;
1545 nes_rem_ref(&nesqp->ibqp);
1546 cm_node->nesqp = NULL;
1547 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001548
Faisal Latif6492cdf2008-07-24 20:50:45 -07001549 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001550 return 0;
1551}
1552
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001553/**
1554 * process_options
1555 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001556static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301557 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001558{
1559 u32 tmp;
1560 u32 offset = 0;
1561 union all_known_options *all_options;
1562 char got_mss_option = 0;
1563
1564 while (offset < optionsize) {
1565 all_options = (union all_known_options *)(optionsloc + offset);
1566 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001567 case OPTION_NUMBER_END:
1568 offset = optionsize;
1569 break;
1570 case OPTION_NUMBER_NONE:
1571 offset += 1;
1572 continue;
1573 case OPTION_NUMBER_MSS:
1574 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301575 "Size: %d\n", __func__,
1576 all_options->as_mss.length, offset, optionsize);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001577 got_mss_option = 1;
1578 if (all_options->as_mss.length != 4) {
1579 return 1;
1580 } else {
1581 tmp = ntohs(all_options->as_mss.mss);
1582 if (tmp > 0 && tmp <
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301583 cm_node->tcp_cntxt.mss)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001584 cm_node->tcp_cntxt.mss = tmp;
1585 }
1586 break;
1587 case OPTION_NUMBER_WINDOW_SCALE:
1588 cm_node->tcp_cntxt.snd_wscale =
1589 all_options->as_windowscale.shiftcount;
1590 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001591 default:
1592 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301593 all_options->as_base.optionnum);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001594 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001595 }
1596 offset += all_options->as_base.length;
1597 }
1598 if ((!got_mss_option) && (syn_packet))
1599 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1600 return 0;
1601}
1602
Faisal Latif6492cdf2008-07-24 20:50:45 -07001603static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001604{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001605 atomic_inc(&cm_accel_dropped_pkts);
1606 dev_kfree_skb_any(skb);
1607}
1608
Faisal Latif9d5ab132009-03-06 15:15:01 -08001609static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001610{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001611 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301612 "refcnt=%d\n", cm_node, cm_node->state,
1613 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001614 switch (cm_node->state) {
1615 case NES_CM_STATE_SYN_RCVD:
1616 case NES_CM_STATE_SYN_SENT:
1617 case NES_CM_STATE_ESTABLISHED:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001618 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001619 cm_node->tcp_cntxt.rcv_nxt++;
1620 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001621 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001622 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001623 break;
Faisal Latifb1190d32009-12-09 15:54:32 -08001624 case NES_CM_STATE_MPAREQ_SENT:
1625 create_event(cm_node, NES_CM_EVENT_ABORTED);
1626 cm_node->tcp_cntxt.rcv_nxt++;
1627 cleanup_retrans_entry(cm_node);
1628 cm_node->state = NES_CM_STATE_CLOSED;
1629 add_ref_cm_node(cm_node);
1630 send_reset(cm_node, NULL);
1631 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001632 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001633 cm_node->tcp_cntxt.rcv_nxt++;
1634 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001635 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001636 send_ack(cm_node, NULL);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001637 /* Wait for ACK as this is simultaneous close..
Faisal Latif9d5ab132009-03-06 15:15:01 -08001638 * After we receive ACK, do not send anything..
1639 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001640 break;
1641 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001642 cm_node->tcp_cntxt.rcv_nxt++;
1643 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001644 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001645 send_ack(cm_node, NULL);
1646 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1647 break;
1648 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001649 cm_node->tcp_cntxt.rcv_nxt++;
1650 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001651 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001652 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001653 break;
1654 case NES_CM_STATE_TSA:
1655 default:
1656 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1657 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001658 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001659 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001660}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001661
Faisal Latif6492cdf2008-07-24 20:50:45 -07001662
1663static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1664 struct tcphdr *tcph)
1665{
1666
1667 int reset = 0; /* whether to send reset in case of err.. */
1668 atomic_inc(&cm_resets_recvd);
1669 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1670 " refcnt=%d\n", cm_node, cm_node->state,
1671 atomic_read(&cm_node->ref_count));
1672 cleanup_retrans_entry(cm_node);
1673 switch (cm_node->state) {
1674 case NES_CM_STATE_SYN_SENT:
1675 case NES_CM_STATE_MPAREQ_SENT:
1676 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1677 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1678 cm_node->listener, cm_node->state);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301679 switch (cm_node->mpa_frame_rev) {
1680 case IETF_MPA_V2:
1681 cm_node->mpa_frame_rev = IETF_MPA_V1;
1682 /* send a syn and goto syn sent state */
1683 cm_node->state = NES_CM_STATE_SYN_SENT;
1684 if (send_syn(cm_node, 0, NULL)) {
1685 active_open_err(cm_node, skb, reset);
1686 }
1687 break;
1688 case IETF_MPA_V1:
1689 default:
1690 active_open_err(cm_node, skb, reset);
1691 break;
1692 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001693 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001694 case NES_CM_STATE_MPAREQ_RCVD:
Dan Carpenter4d8d6382010-09-15 18:32:39 +02001695 atomic_inc(&cm_node->passive_state);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001696 dev_kfree_skb_any(skb);
1697 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001698 case NES_CM_STATE_ESTABLISHED:
1699 case NES_CM_STATE_SYN_RCVD:
1700 case NES_CM_STATE_LISTENING:
1701 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1702 passive_open_err(cm_node, skb, reset);
1703 break;
1704 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001705 active_open_err(cm_node, skb, reset);
1706 break;
1707 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001708 drop_packet(skb);
1709 break;
Faisal Latifdae58722010-08-14 21:04:56 +00001710 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif68237a02009-06-22 22:53:28 -07001711 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001712 case NES_CM_STATE_LAST_ACK:
1713 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001714 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001715 cm_node->state = NES_CM_STATE_CLOSED;
1716 rem_ref_cm_node(cm_node->cm_core, cm_node);
1717 drop_packet(skb);
1718 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001719 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001720 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001721 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001722 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001723}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001724
Faisal Latif9d5ab132009-03-06 15:15:01 -08001725
1726static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001727{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301728 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001729 int datasize = skb->len;
1730 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001731
1732 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301733 u32 res_type;
1734
Faisal Latif9d5ab132009-03-06 15:15:01 -08001735 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1736 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001737 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001738 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001739 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301740 "cm_node=%p listener=%p state=%d\n", __func__,
1741 __LINE__, cm_node, cm_node->listener,
1742 cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001743 active_open_err(cm_node, skb, 1);
1744 } else {
1745 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001746 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001747 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001748 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001749
1750 switch (cm_node->state) {
1751 case NES_CM_STATE_ESTABLISHED:
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301752 if (res_type == NES_MPA_REQUEST_REJECT)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001753 /*BIG problem as we are receiving the MPA.. So should
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301754 * not be REJECT.. This is Passive Open.. We can
1755 * only receive it Reject for Active Open...*/
Faisal Latif9d5ab132009-03-06 15:15:01 -08001756 WARN_ON(1);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001757 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1758 type = NES_CM_EVENT_MPA_REQ;
1759 atomic_set(&cm_node->passive_state,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301760 NES_PASSIVE_STATE_INDICATED);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001761 break;
1762 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001763 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001764 if (res_type == NES_MPA_REQUEST_REJECT) {
1765 type = NES_CM_EVENT_MPA_REJECT;
1766 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1767 } else {
1768 type = NES_CM_EVENT_CONNECTED;
1769 cm_node->state = NES_CM_STATE_TSA;
1770 }
1771
1772 break;
1773 default:
1774 WARN_ON(1);
1775 break;
1776 }
1777 dev_kfree_skb_any(skb);
1778 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001779}
1780
1781static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1782{
1783 switch (cm_node->state) {
1784 case NES_CM_STATE_SYN_SENT:
1785 case NES_CM_STATE_MPAREQ_SENT:
1786 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301787 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1788 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001789 active_open_err(cm_node, skb, 1);
1790 break;
1791 case NES_CM_STATE_ESTABLISHED:
1792 case NES_CM_STATE_SYN_RCVD:
1793 passive_open_err(cm_node, skb, 1);
1794 break;
1795 case NES_CM_STATE_TSA:
1796 default:
1797 drop_packet(skb);
1798 }
1799}
1800
1801static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301802 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001803{
1804 int err;
1805
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301806 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001807 if (err)
1808 active_open_err(cm_node, skb, 1);
1809
1810 return err;
1811}
1812
1813static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301814 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001815{
1816 int err = 0;
1817 u32 seq;
1818 u32 ack_seq;
1819 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1820 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1821 u32 rcv_wnd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301822
Faisal Latif6492cdf2008-07-24 20:50:45 -07001823 seq = ntohl(tcph->seq);
1824 ack_seq = ntohl(tcph->ack_seq);
1825 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1826 if (ack_seq != loc_seq_num)
1827 err = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301828 else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001829 err = 1;
1830 if (err) {
1831 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301832 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1833 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001834 indicate_pkt_err(cm_node, skb);
1835 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301836 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1837 rcv_wnd);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001838 }
1839 return err;
1840}
1841
1842/*
1843 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1844 * is created with a listener or it may comein as rexmitted packet which in
1845 * that case will be just dropped.
1846 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001847static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301848 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001849{
1850 int ret;
1851 u32 inc_sequence;
1852 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001853
1854 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001855 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001856 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001857
Faisal Latif6492cdf2008-07-24 20:50:45 -07001858 switch (cm_node->state) {
1859 case NES_CM_STATE_SYN_SENT:
1860 case NES_CM_STATE_MPAREQ_SENT:
1861 /* Rcvd syn on active open connection*/
1862 active_open_err(cm_node, skb, 1);
1863 break;
1864 case NES_CM_STATE_LISTENING:
1865 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001866 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301867 cm_node->listener->backlog) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001868 nes_debug(NES_DBG_CM, "drop syn due to backlog "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301869 "pressure \n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07001870 cm_backlog_drops++;
1871 passive_open_err(cm_node, skb, 0);
1872 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001873 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001874 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301875 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001876 if (ret) {
1877 passive_open_err(cm_node, skb, 0);
1878 /* drop pkt */
1879 break;
1880 }
1881 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1882 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001883 cm_node->accept_pend = 1;
1884 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1885
Faisal Latif6492cdf2008-07-24 20:50:45 -07001886 cm_node->state = NES_CM_STATE_SYN_RCVD;
1887 send_syn(cm_node, 1, skb);
1888 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001889 case NES_CM_STATE_CLOSED:
1890 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001891 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001892 send_reset(cm_node, skb);
1893 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001894 case NES_CM_STATE_TSA:
1895 case NES_CM_STATE_ESTABLISHED:
1896 case NES_CM_STATE_FIN_WAIT1:
1897 case NES_CM_STATE_FIN_WAIT2:
1898 case NES_CM_STATE_MPAREQ_RCVD:
1899 case NES_CM_STATE_LAST_ACK:
1900 case NES_CM_STATE_CLOSING:
1901 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001902 default:
1903 drop_packet(skb);
1904 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001905 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001906}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001907
Faisal Latif6492cdf2008-07-24 20:50:45 -07001908static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301909 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001910{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001911 int ret;
1912 u32 inc_sequence;
1913 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001914
Faisal Latif6492cdf2008-07-24 20:50:45 -07001915 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001916 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001917 inc_sequence = ntohl(tcph->seq);
1918 switch (cm_node->state) {
1919 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001920 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001921 /* active open */
1922 if (check_syn(cm_node, tcph, skb))
1923 return;
1924 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1925 /* setup options */
1926 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1927 if (ret) {
1928 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301929 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001930 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001931 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001932 cleanup_retrans_entry(cm_node);
1933 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1934 send_mpa_request(cm_node, skb);
1935 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1936 break;
1937 case NES_CM_STATE_MPAREQ_RCVD:
1938 /* passive open, so should not be here */
1939 passive_open_err(cm_node, skb, 1);
1940 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001941 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001942 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1943 cleanup_retrans_entry(cm_node);
1944 cm_node->state = NES_CM_STATE_CLOSED;
1945 send_reset(cm_node, skb);
1946 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001947 case NES_CM_STATE_CLOSED:
1948 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1949 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001950 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001951 send_reset(cm_node, skb);
1952 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001953 case NES_CM_STATE_ESTABLISHED:
1954 case NES_CM_STATE_FIN_WAIT1:
1955 case NES_CM_STATE_FIN_WAIT2:
1956 case NES_CM_STATE_LAST_ACK:
1957 case NES_CM_STATE_TSA:
1958 case NES_CM_STATE_CLOSING:
1959 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001960 case NES_CM_STATE_MPAREQ_SENT:
1961 default:
1962 drop_packet(skb);
1963 break;
1964 }
1965}
1966
Faisal Latif109d67e2009-04-27 13:41:06 -07001967static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301968 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001969{
1970 int datasize = 0;
1971 u32 inc_sequence;
Faisal Latif109d67e2009-04-27 13:41:06 -07001972 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06001973 int optionsize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301974
Faisal Latifabb77252008-11-21 20:50:52 -06001975 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001976
Faisal Latif6492cdf2008-07-24 20:50:45 -07001977 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07001978 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001979
1980 skb_pull(skb, tcph->doff << 2);
1981 inc_sequence = ntohl(tcph->seq);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001982 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001983 switch (cm_node->state) {
1984 case NES_CM_STATE_SYN_RCVD:
1985 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07001986 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001987 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1988 if (ret)
1989 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001990 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1991 cm_node->state = NES_CM_STATE_ESTABLISHED;
1992 if (datasize) {
1993 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001994 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301995 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001996 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301997 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001998 break;
1999 case NES_CM_STATE_ESTABLISHED:
2000 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08002001 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002002 if (datasize) {
2003 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002004 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302005 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002006 drop_packet(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302007 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002008 break;
2009 case NES_CM_STATE_MPAREQ_SENT:
2010 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2011 if (datasize) {
2012 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002013 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302014 } else { /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002015 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302016 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002017 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002018 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002019 cleanup_retrans_entry(cm_node);
2020 cm_node->state = NES_CM_STATE_CLOSED;
2021 send_reset(cm_node, skb);
2022 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002023 case NES_CM_STATE_CLOSED:
2024 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08002025 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002026 send_reset(cm_node, skb);
2027 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002028 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002029 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002030 cleanup_retrans_entry(cm_node);
2031 cm_node->state = NES_CM_STATE_CLOSED;
2032 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002033 rem_ref_cm_node(cm_node->cm_core, cm_node);
2034 drop_packet(skb);
2035 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002036 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002037 cleanup_retrans_entry(cm_node);
2038 drop_packet(skb);
2039 cm_node->state = NES_CM_STATE_FIN_WAIT2;
2040 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002041 case NES_CM_STATE_SYN_SENT:
2042 case NES_CM_STATE_FIN_WAIT2:
2043 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002044 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002045 case NES_CM_STATE_UNKNOWN:
2046 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002047 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002048 drop_packet(skb);
2049 break;
2050 }
Faisal Latif109d67e2009-04-27 13:41:06 -07002051 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002052}
2053
2054
2055
2056static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302057 struct sk_buff *skb, int optionsize, int passive)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002058{
2059 u8 *optionsloc = (u8 *)&tcph[1];
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302060
Faisal Latif6492cdf2008-07-24 20:50:45 -07002061 if (optionsize) {
2062 if (process_options(cm_node, optionsloc, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302063 (u32)tcph->syn)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002064 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302065 __func__, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002066 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06002067 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002068 else
Faisal Latifabb77252008-11-21 20:50:52 -06002069 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002070 return 1;
2071 }
2072 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002073
2074 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302075 cm_node->tcp_cntxt.snd_wscale;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002076
Faisal Latif6492cdf2008-07-24 20:50:45 -07002077 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002078 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002079 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002080}
2081
Faisal Latif6492cdf2008-07-24 20:50:45 -07002082/*
2083 * active_open_err() will send reset() if flag set..
2084 * It will also send ABORT event.
2085 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002086static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302087 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002088{
2089 cleanup_retrans_entry(cm_node);
2090 if (reset) {
2091 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302092 "state=%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002093 add_ref_cm_node(cm_node);
2094 send_reset(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302095 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002096 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302097 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002098
2099 cm_node->state = NES_CM_STATE_CLOSED;
2100 create_event(cm_node, NES_CM_EVENT_ABORTED);
2101}
2102
2103/*
2104 * passive_open_err() will either do a reset() or will free up the skb and
2105 * remove the cm_node.
2106 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002107static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302108 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002109{
2110 cleanup_retrans_entry(cm_node);
2111 cm_node->state = NES_CM_STATE_CLOSED;
2112 if (reset) {
2113 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302114 "cm_node=%p state =%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002115 send_reset(cm_node, skb);
2116 } else {
2117 dev_kfree_skb_any(skb);
2118 rem_ref_cm_node(cm_node->cm_core, cm_node);
2119 }
2120}
2121
2122/*
2123 * free_retrans_entry() routines assumes that the retrans_list_lock has
2124 * been acquired before calling.
2125 */
2126static void free_retrans_entry(struct nes_cm_node *cm_node)
2127{
2128 struct nes_timer_entry *send_entry;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302129
Faisal Latif6492cdf2008-07-24 20:50:45 -07002130 send_entry = cm_node->send_entry;
2131 if (send_entry) {
2132 cm_node->send_entry = NULL;
2133 dev_kfree_skb_any(send_entry->skb);
2134 kfree(send_entry);
2135 rem_ref_cm_node(cm_node->cm_core, cm_node);
2136 }
2137}
2138
2139static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
2140{
2141 unsigned long flags;
2142
2143 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
2144 free_retrans_entry(cm_node);
2145 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
2146}
2147
2148/**
2149 * process_packet
2150 * Returns skb if to be freed, else it will return NULL if already used..
2151 */
2152static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302153 struct nes_cm_core *cm_core)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002154{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302155 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002156 struct tcphdr *tcph = tcp_hdr(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302157 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07002158 int ret = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302159
Faisal Latif6492cdf2008-07-24 20:50:45 -07002160 skb_pull(skb, ip_hdr(skb)->ihl << 2);
2161
2162 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302163 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
2164 tcph->ack, tcph->rst, tcph->fin);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002165
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302166 if (tcph->rst) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002167 pkt_type = NES_PKT_TYPE_RST;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302168 } else if (tcph->syn) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002169 pkt_type = NES_PKT_TYPE_SYN;
2170 if (tcph->ack)
2171 pkt_type = NES_PKT_TYPE_SYNACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302172 } else if (tcph->ack) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002173 pkt_type = NES_PKT_TYPE_ACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302174 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002175 if (tcph->fin)
2176 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002177
2178 switch (pkt_type) {
2179 case NES_PKT_TYPE_SYN:
2180 handle_syn_pkt(cm_node, skb, tcph);
2181 break;
2182 case NES_PKT_TYPE_SYNACK:
2183 handle_synack_pkt(cm_node, skb, tcph);
2184 break;
2185 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002186 ret = handle_ack_pkt(cm_node, skb, tcph);
2187 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08002188 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002189 break;
2190 case NES_PKT_TYPE_RST:
2191 handle_rst_pkt(cm_node, skb, tcph);
2192 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002193 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002194 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08002195 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07002196 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002197 break;
2198 }
2199}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002200
2201/**
2202 * mini_cm_listen - create a listen node with params
2203 */
2204static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302205 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002206{
2207 struct nes_cm_listener *listener;
2208 unsigned long flags;
2209
2210 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302211 cm_info->loc_addr, cm_info->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002212
2213 /* cannot have multiple matching listeners */
2214 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302215 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002216 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
2217 /* find automatically incs ref count ??? */
2218 atomic_dec(&listener->ref_count);
2219 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
2220 return NULL;
2221 }
2222
2223 if (!listener) {
2224 /* create a CM listen node (1/2 node to compare incoming traffic to) */
2225 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
2226 if (!listener) {
2227 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
2228 return NULL;
2229 }
2230
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002231 listener->loc_addr = htonl(cm_info->loc_addr);
2232 listener->loc_port = htons(cm_info->loc_port);
2233 listener->reused_node = 0;
2234
2235 atomic_set(&listener->ref_count, 1);
2236 }
2237 /* pasive case */
2238 /* find already inc'ed the ref count */
2239 else {
2240 listener->reused_node = 1;
2241 }
2242
2243 listener->cm_id = cm_info->cm_id;
2244 atomic_set(&listener->pend_accepts_cnt, 0);
2245 listener->cm_core = cm_core;
2246 listener->nesvnic = nesvnic;
2247 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002248
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002249 listener->conn_type = cm_info->conn_type;
2250 listener->backlog = cm_info->backlog;
2251 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
2252
2253 if (!listener->reused_node) {
2254 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2255 list_add(&listener->list, &cm_core->listen_list.list);
2256 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2257 atomic_inc(&cm_core->listen_node_cnt);
2258 }
2259
2260 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302261 " listener = %p, backlog = %d, cm_id = %p.\n",
2262 cm_info->loc_addr, cm_info->loc_port,
2263 listener, listener->backlog, listener->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002264
2265 return listener;
2266}
2267
2268
2269/**
2270 * mini_cm_connect - make a connection node with params
2271 */
Faisal Latif54c86a82008-09-30 14:47:27 -07002272static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302273 struct nes_vnic *nesvnic, u16 private_data_len,
2274 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002275{
2276 int ret = 0;
2277 struct nes_cm_node *cm_node;
2278 struct nes_cm_listener *loopbackremotelistener;
2279 struct nes_cm_node *loopbackremotenode;
2280 struct nes_cm_info loopback_cm_info;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302281 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002282
2283 /* create a CM connection node */
2284 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2285 if (!cm_node)
2286 return NULL;
2287
Glenn Streiff7495ab62008-04-29 13:46:54 -07002288 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002289 cm_node->tcp_cntxt.client = 1;
2290 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2291
2292 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002293 loopbackremotelistener = find_listener(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302294 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2295 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002296 if (loopbackremotelistener == NULL) {
2297 create_event(cm_node, NES_CM_EVENT_ABORTED);
2298 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002299 loopback_cm_info = *cm_info;
2300 loopback_cm_info.loc_port = cm_info->rem_port;
2301 loopback_cm_info.rem_port = cm_info->loc_port;
2302 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002303 loopbackremotenode = make_cm_node(cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302304 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002305 if (!loopbackremotenode) {
2306 rem_ref_cm_node(cm_node->cm_core, cm_node);
2307 return NULL;
2308 }
2309 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002310 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002311 loopbackremotenode->tcp_cntxt.rcv_wscale =
2312 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002313 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002314 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302315 private_data_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002316 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002317
Faisal Latif6492cdf2008-07-24 20:50:45 -07002318 /* we are done handling this state. */
2319 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002320 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002321 cm_node->tcp_cntxt.rcv_nxt =
2322 loopbackremotenode->tcp_cntxt.loc_seq_num;
2323 loopbackremotenode->tcp_cntxt.rcv_nxt =
2324 cm_node->tcp_cntxt.loc_seq_num;
2325 cm_node->tcp_cntxt.max_snd_wnd =
2326 loopbackremotenode->tcp_cntxt.rcv_wnd;
2327 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2328 cm_node->tcp_cntxt.rcv_wnd;
2329 cm_node->tcp_cntxt.snd_wnd =
2330 loopbackremotenode->tcp_cntxt.rcv_wnd;
2331 loopbackremotenode->tcp_cntxt.snd_wnd =
2332 cm_node->tcp_cntxt.rcv_wnd;
2333 cm_node->tcp_cntxt.snd_wscale =
2334 loopbackremotenode->tcp_cntxt.rcv_wscale;
2335 loopbackremotenode->tcp_cntxt.snd_wscale =
2336 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002337 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002338 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2339 }
2340 return cm_node;
2341 }
2342
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302343 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
2344 cm_node->mpa_frame_size = private_data_len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002345
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302346 memcpy(start_buff, private_data, private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002347
2348 /* send a syn and goto syn sent state */
2349 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002350 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002351
Faisal Latif6492cdf2008-07-24 20:50:45 -07002352 if (ret) {
2353 /* error in sending the syn free up the cm_node struct */
2354 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302355 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2356 cm_node->rem_addr, cm_node->rem_port, cm_node,
2357 cm_node->cm_id);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002358 rem_ref_cm_node(cm_node->cm_core, cm_node);
2359 cm_node = NULL;
2360 }
2361
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302362 if (cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002363 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302364 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2365 cm_node->rem_addr, cm_node->rem_port, cm_node,
2366 cm_node->cm_id);
2367 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002368
2369 return cm_node;
2370}
2371
2372
2373/**
2374 * mini_cm_accept - accept a connection
2375 * This function is never called
2376 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302377static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002378{
2379 return 0;
2380}
2381
2382
2383/**
2384 * mini_cm_reject - reject and teardown a connection
2385 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302386static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002387{
2388 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002389 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002390 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002391 struct nes_cm_event event;
2392 struct iw_cm_id *cm_id = cm_node->cm_id;
2393 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002394
Faisal Latif6492cdf2008-07-24 20:50:45 -07002395 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302396 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002397
Faisal Latif6492cdf2008-07-24 20:50:45 -07002398 if (cm_node->tcp_cntxt.client)
2399 return ret;
2400 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002401
Faisal Latif9d5ab132009-03-06 15:15:01 -08002402 if (!loopback) {
2403 passive_state = atomic_add_return(1, &cm_node->passive_state);
2404 if (passive_state == NES_SEND_RESET_EVENT) {
2405 cm_node->state = NES_CM_STATE_CLOSED;
2406 rem_ref_cm_node(cm_core, cm_node);
2407 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002408 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2409 rem_ref_cm_node(cm_core, cm_node);
2410 } else {
2411 ret = send_mpa_reject(cm_node);
2412 if (ret) {
2413 cm_node->state = NES_CM_STATE_CLOSED;
2414 err = send_reset(cm_node, NULL);
2415 if (err)
2416 WARN_ON(1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302417 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002418 cm_id->add_ref(cm_id);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302419 }
Faisal Latifc5a7d482009-12-09 15:54:08 -08002420 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002421 }
2422 } else {
2423 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002424 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2425 rem_ref_cm_node(cm_core, cm_node);
2426 rem_ref_cm_node(cm_core, loopback);
2427 } else {
2428 event.cm_node = loopback;
2429 event.cm_info.rem_addr = loopback->rem_addr;
2430 event.cm_info.loc_addr = loopback->loc_addr;
2431 event.cm_info.rem_port = loopback->rem_port;
2432 event.cm_info.loc_port = loopback->loc_port;
2433 event.cm_info.cm_id = loopback->cm_id;
2434 cm_event_mpa_reject(&event);
2435 rem_ref_cm_node(cm_core, cm_node);
2436 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002437
Faisal Latifc5a7d482009-12-09 15:54:08 -08002438 cm_id = loopback->cm_id;
2439 rem_ref_cm_node(cm_core, loopback);
2440 cm_id->rem_ref(cm_id);
2441 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002442 }
2443
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002444 return ret;
2445}
2446
2447
2448/**
2449 * mini_cm_close
2450 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002451static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002452{
2453 int ret = 0;
2454
2455 if (!cm_core || !cm_node)
2456 return -EINVAL;
2457
2458 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002459 case NES_CM_STATE_SYN_RCVD:
2460 case NES_CM_STATE_SYN_SENT:
2461 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2462 case NES_CM_STATE_ESTABLISHED:
2463 case NES_CM_STATE_ACCEPTING:
2464 case NES_CM_STATE_MPAREQ_SENT:
2465 case NES_CM_STATE_MPAREQ_RCVD:
2466 cleanup_retrans_entry(cm_node);
2467 send_reset(cm_node, NULL);
2468 break;
2469 case NES_CM_STATE_CLOSE_WAIT:
2470 cm_node->state = NES_CM_STATE_LAST_ACK;
2471 send_fin(cm_node, NULL);
2472 break;
2473 case NES_CM_STATE_FIN_WAIT1:
2474 case NES_CM_STATE_FIN_WAIT2:
2475 case NES_CM_STATE_LAST_ACK:
2476 case NES_CM_STATE_TIME_WAIT:
2477 case NES_CM_STATE_CLOSING:
2478 ret = -1;
2479 break;
2480 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002481 cleanup_retrans_entry(cm_node);
2482 send_reset(cm_node, NULL);
2483 break;
2484 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002485 case NES_CM_STATE_UNKNOWN:
2486 case NES_CM_STATE_INITED:
2487 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002488 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002489 ret = rem_ref_cm_node(cm_core, cm_node);
2490 break;
2491 case NES_CM_STATE_TSA:
2492 if (cm_node->send_entry)
2493 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302494 "send_entry=%p\n", cm_node->send_entry);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002495 ret = rem_ref_cm_node(cm_core, cm_node);
2496 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002497 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002498 return ret;
2499}
2500
2501
2502/**
2503 * recv_pkt - recv an ETHERNET packet, and process it through CM
2504 * node state machine
2505 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002506static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302507 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002508{
2509 struct nes_cm_node *cm_node = NULL;
2510 struct nes_cm_listener *listener = NULL;
2511 struct iphdr *iph;
2512 struct tcphdr *tcph;
2513 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002514 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002515 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002516
Faisal Latif6492cdf2008-07-24 20:50:45 -07002517 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002518 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302519 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002520 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002521
2522 iph = (struct iphdr *)skb->data;
2523 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002524
2525 nfo.loc_addr = ntohl(iph->daddr);
2526 nfo.loc_port = ntohs(tcph->dest);
2527 nfo.rem_addr = ntohl(iph->saddr);
2528 nfo.rem_port = ntohs(tcph->source);
2529
Harvey Harrison03080e52009-01-10 21:45:42 -08002530 tmp_daddr = cpu_to_be32(iph->daddr);
2531 tmp_saddr = cpu_to_be32(iph->saddr);
2532
Harvey Harrison63779432008-10-31 00:56:00 -07002533 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002534 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002535
Faisal Latif6492cdf2008-07-24 20:50:45 -07002536 do {
2537 cm_node = find_node(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302538 nfo.rem_port, nfo.rem_addr,
2539 nfo.loc_port, nfo.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002540
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002541 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002542 /* Only type of packet accepted are for */
2543 /* the PASSIVE open (syn only) */
2544 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002545 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002546 break;
2547 }
2548 listener = find_listener(cm_core, nfo.loc_addr,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302549 nfo.loc_port,
2550 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002551 if (!listener) {
2552 nfo.cm_id = NULL;
2553 nfo.conn_type = 0;
2554 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2555 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002556 break;
2557 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002558 nfo.cm_id = listener->cm_id;
2559 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002560 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302561 listener);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002562 if (!cm_node) {
2563 nes_debug(NES_DBG_CM, "Unable to allocate "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302564 "node\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07002565 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002566 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002567 dev_kfree_skb_any(skb);
2568 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002569 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002570 if (!tcph->rst && !tcph->fin) {
2571 cm_node->state = NES_CM_STATE_LISTENING;
2572 } else {
2573 cm_packets_dropped++;
2574 rem_ref_cm_node(cm_core, cm_node);
2575 dev_kfree_skb_any(skb);
2576 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002577 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002578 add_ref_cm_node(cm_node);
2579 } else if (cm_node->state == NES_CM_STATE_TSA) {
Faisal Latif0f0bee82011-09-25 20:34:00 -05002580 if (cm_node->nesqp->pau_mode)
2581 nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
2582 else {
2583 rem_ref_cm_node(cm_core, cm_node);
2584 atomic_inc(&cm_accel_dropped_pkts);
2585 dev_kfree_skb_any(skb);
2586 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002587 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002588 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002589 skb_reset_network_header(skb);
2590 skb_set_transport_header(skb, sizeof(*tcph));
2591 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002592 process_packet(cm_node, skb, cm_core);
2593 rem_ref_cm_node(cm_core, cm_node);
2594 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002595 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002596}
2597
2598
2599/**
2600 * nes_cm_alloc_core - allocate a top level instance of a cm core
2601 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002602static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002603{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002604 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002605
2606 /* setup the CM core */
2607 /* alloc top level core control structure */
2608 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2609 if (!cm_core)
2610 return NULL;
2611
2612 INIT_LIST_HEAD(&cm_core->connected_nodes);
2613 init_timer(&cm_core->tcp_timer);
2614 cm_core->tcp_timer.function = nes_cm_timer_tick;
2615
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302616 cm_core->mtu = NES_CM_DEFAULT_MTU;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002617 cm_core->state = NES_CM_STATE_INITED;
2618 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2619
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002620 atomic_set(&cm_core->events_posted, 0);
2621
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002622 cm_core->api = &nes_cm_api;
2623
2624 spin_lock_init(&cm_core->ht_lock);
2625 spin_lock_init(&cm_core->listen_list_lock);
2626
2627 INIT_LIST_HEAD(&cm_core->listen_list.list);
2628
2629 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2630
2631 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2632 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2633 cm_core->post_event = nes_cm_post_event;
2634 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2635 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2636
2637 print_core(cm_core);
2638 return cm_core;
2639}
2640
2641
2642/**
2643 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2644 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002645static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002646{
2647 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2648
2649 if (!cm_core)
2650 return -EINVAL;
2651
2652 barrier();
2653
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302654 if (timer_pending(&cm_core->tcp_timer))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002655 del_timer(&cm_core->tcp_timer);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002656
2657 destroy_workqueue(cm_core->event_wq);
2658 destroy_workqueue(cm_core->disconn_wq);
2659 nes_debug(NES_DBG_CM, "\n");
2660 kfree(cm_core);
2661
2662 return 0;
2663}
2664
2665
2666/**
2667 * mini_cm_get
2668 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002669static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002670{
2671 return cm_core->state;
2672}
2673
2674
2675/**
2676 * mini_cm_set
2677 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002678static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002679{
2680 int ret = 0;
2681
2682 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002683 case NES_CM_SET_PKT_SIZE:
2684 cm_core->mtu = value;
2685 break;
2686 case NES_CM_SET_FREE_PKT_Q_SIZE:
2687 cm_core->free_tx_pkt_max = value;
2688 break;
2689 default:
2690 /* unknown set option */
2691 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002692 }
2693
2694 return ret;
2695}
2696
2697
2698/**
2699 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2700 * successfully exchanged when this is called
2701 */
2702static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2703{
2704 int ret = 0;
2705
2706 if (!nesqp)
2707 return -EINVAL;
2708
2709 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302710 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2711 NES_QPCONTEXT_MISC_DROS);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002712
2713 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2714 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2715
2716 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2717
2718 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2719
2720 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302721 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002722
2723 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302724 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2725 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002726
2727 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302728 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2729 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002730
2731 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2732 nesqp->nesqp_context->ts_recent = 0;
2733 nesqp->nesqp_context->ts_age = 0;
2734 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2735 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2736 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2737 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302738 cm_node->tcp_cntxt.rcv_wscale);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002739 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2740 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2741 nesqp->nesqp_context->srtt = 0;
2742 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2743 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302744 nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002745 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2746 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2747 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2748
2749 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302750 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2751 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2752 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2753 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2754 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2755 le32_to_cpu(nesqp->nesqp_context->misc));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002756 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2757 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2758 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2759
2760 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2761 cm_node->state = NES_CM_STATE_TSA;
2762
2763 return ret;
2764}
2765
2766
2767/**
2768 * nes_cm_disconn
2769 */
2770int nes_cm_disconn(struct nes_qp *nesqp)
2771{
Don Wood873fcdd2009-09-05 20:36:37 -07002772 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002773
Don Wood873fcdd2009-09-05 20:36:37 -07002774 work = kzalloc(sizeof *work, GFP_ATOMIC);
2775 if (!work)
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302776 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002777
Don Wood873fcdd2009-09-05 20:36:37 -07002778 nes_add_ref(&nesqp->ibqp);
2779 work->nesqp = nesqp;
2780 INIT_WORK(&work->work, nes_disconnect_worker);
2781 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002782 return 0;
2783}
2784
2785
2786/**
2787 * nes_disconnect_worker
2788 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002789static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002790{
Don Wood873fcdd2009-09-05 20:36:37 -07002791 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2792 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002793
Don Wood873fcdd2009-09-05 20:36:37 -07002794 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002795 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302796 nesqp->last_aeq, nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002797 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002798 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002799}
2800
2801
2802/**
2803 * nes_cm_disconn_true
2804 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002805static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002806{
2807 unsigned long flags;
2808 int ret = 0;
2809 struct iw_cm_id *cm_id;
2810 struct iw_cm_event cm_event;
2811 struct nes_vnic *nesvnic;
2812 u16 last_ae;
2813 u8 original_hw_tcp_state;
2814 u8 original_ibqp_state;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002815 int disconn_status = 0;
Don Woodb29a4fc2009-09-05 20:36:39 -07002816 int issue_disconn = 0;
2817 int issue_close = 0;
2818 int issue_flush = 0;
2819 u32 flush_q = NES_CQP_FLUSH_RQ;
2820 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002821
2822 if (!nesqp) {
2823 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2824 return -1;
2825 }
2826
2827 spin_lock_irqsave(&nesqp->lock, flags);
2828 cm_id = nesqp->cm_id;
2829 /* make sure we havent already closed this connection */
2830 if (!cm_id) {
2831 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302832 nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002833 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002834 return -1;
2835 }
2836
2837 nesvnic = to_nesvnic(nesqp->ibqp.device);
2838 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2839
2840 original_hw_tcp_state = nesqp->hw_tcp_state;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302841 original_ibqp_state = nesqp->ibqp_state;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002842 last_ae = nesqp->last_aeq;
2843
Don Woodb29a4fc2009-09-05 20:36:39 -07002844 if (nesqp->term_flags) {
2845 issue_disconn = 1;
2846 issue_close = 1;
2847 nesqp->cm_id = NULL;
Tatyana Nikolova196f40c2011-12-05 11:39:12 -06002848 del_timer(&nesqp->terminate_timer);
Don Woodb29a4fc2009-09-05 20:36:39 -07002849 if (nesqp->flush_issued == 0) {
2850 nesqp->flush_issued = 1;
2851 issue_flush = 1;
2852 }
2853 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2854 ((original_ibqp_state == IB_QPS_RTS) &&
2855 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2856 issue_disconn = 1;
2857 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002858 disconn_status = -ECONNRESET;
Don Woodb29a4fc2009-09-05 20:36:39 -07002859 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002860
Don Woodb29a4fc2009-09-05 20:36:39 -07002861 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2862 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2863 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2864 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2865 issue_close = 1;
2866 nesqp->cm_id = NULL;
2867 if (nesqp->flush_issued == 0) {
2868 nesqp->flush_issued = 1;
2869 issue_flush = 1;
2870 }
2871 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002872
Don Woodb29a4fc2009-09-05 20:36:39 -07002873 spin_unlock_irqrestore(&nesqp->lock, flags);
2874
2875 if ((issue_flush) && (nesqp->destroyed == 0)) {
2876 /* Flush the queue(s) */
2877 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2878 flush_q |= NES_CQP_FLUSH_SQ;
2879 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2880
2881 if (nesqp->term_flags) {
2882 ibevent.device = nesqp->ibqp.device;
2883 ibevent.event = nesqp->terminate_eventtype;
2884 ibevent.element.qp = &nesqp->ibqp;
Tatyana Nikolova784d1352012-05-11 14:29:32 -05002885 if (nesqp->ibqp.event_handler)
2886 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
Don Woodb29a4fc2009-09-05 20:36:39 -07002887 }
2888 }
2889
2890 if ((cm_id) && (cm_id->event_handler)) {
2891 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002892 atomic_inc(&cm_disconnects);
2893 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002894 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002895 cm_event.local_addr = cm_id->local_addr;
2896 cm_event.remote_addr = cm_id->remote_addr;
2897 cm_event.private_data = NULL;
2898 cm_event.private_data_len = 0;
2899
Faisal Latif6492cdf2008-07-24 20:50:45 -07002900 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302901 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2902 "cm_id = %p, refcount = %u.\n",
2903 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2904 nesqp->hwqp.sq_tail, cm_id,
2905 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002906
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002907 ret = cm_id->event_handler(cm_id, &cm_event);
2908 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002909 nes_debug(NES_DBG_CM, "OFA CM event_handler "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302910 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002911 }
2912
Don Woodb29a4fc2009-09-05 20:36:39 -07002913 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002914 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002915 nes_disconnect(nesqp, 1);
2916
2917 cm_id->provider_data = nesqp;
2918 /* Send up the close complete event */
2919 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002920 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002921 cm_event.provider_data = cm_id->provider_data;
2922 cm_event.local_addr = cm_id->local_addr;
2923 cm_event.remote_addr = cm_id->remote_addr;
2924 cm_event.private_data = NULL;
2925 cm_event.private_data_len = 0;
2926
2927 ret = cm_id->event_handler(cm_id, &cm_event);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302928 if (ret)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002929 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002930
2931 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002932 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002933 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002934
2935 return 0;
2936}
2937
2938
2939/**
2940 * nes_disconnect
2941 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002942static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002943{
2944 int ret = 0;
2945 struct nes_vnic *nesvnic;
2946 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002947 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002948
2949 nesvnic = to_nesvnic(nesqp->ibqp.device);
2950 if (!nesvnic)
2951 return -EINVAL;
2952
2953 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002954 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002955
2956 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00002957 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002958
2959 if (nesqp->active_conn) {
2960
2961 /* indicate this connection is NOT active */
2962 nesqp->active_conn = 0;
2963 } else {
2964 /* Need to free the Last Streaming Mode Message */
2965 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002966 if (nesqp->lsmm_mr)
2967 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002968 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302969 nesqp->private_data_len + nesqp->ietf_frame_size,
2970 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002971 }
2972 }
2973
2974 /* close the CM node down if it is still active */
2975 if (nesqp->cm_node) {
2976 nes_debug(NES_DBG_CM, "Call close API\n");
2977
2978 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002979 }
2980
2981 return ret;
2982}
2983
2984
2985/**
2986 * nes_accept
2987 */
2988int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2989{
2990 u64 u64temp;
2991 struct ib_qp *ibqp;
2992 struct nes_qp *nesqp;
2993 struct nes_vnic *nesvnic;
2994 struct nes_device *nesdev;
2995 struct nes_cm_node *cm_node;
2996 struct nes_adapter *adapter;
2997 struct ib_qp_attr attr;
2998 struct iw_cm_event cm_event;
2999 struct nes_hw_qp_wqe *wqe;
3000 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003001 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003002 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003003 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003004 struct nes_ib_device *nesibdev;
3005 struct ib_mr *ibmr = NULL;
3006 struct ib_phys_buf ibphysbuf;
3007 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07003008 u64 tagged_offset;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303009 u8 mpa_frame_offset = 0;
3010 struct ietf_mpa_v2 *mpa_v2_frame;
3011 u8 start_addr = 0;
3012 u8 *start_ptr = &start_addr;
3013 u8 **start_buff = &start_ptr;
3014 u16 buff_len = 0;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003015
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003016 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3017 if (!ibqp)
3018 return -EINVAL;
3019
3020 /* get all our handles */
3021 nesqp = to_nesqp(ibqp);
3022 nesvnic = to_nesvnic(nesqp->ibqp.device);
3023 nesdev = nesvnic->nesdev;
3024 adapter = nesdev->nesadapter;
3025
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003026 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003027 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
3028 "%s\n", cm_node, nesvnic, nesvnic->netdev,
3029 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003030
Faisal Latifc5a7d482009-12-09 15:54:08 -08003031 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
3032 if (cm_node->loopbackpartner)
3033 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
3034 rem_ref_cm_node(cm_node->cm_core, cm_node);
3035 return -EINVAL;
3036 }
3037
Faisal Latifdae58722010-08-14 21:04:56 +00003038 passive_state = atomic_add_return(1, &cm_node->passive_state);
3039 if (passive_state == NES_SEND_RESET_EVENT) {
3040 rem_ref_cm_node(cm_node->cm_core, cm_node);
3041 return -ECONNRESET;
3042 }
3043
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003044 /* associate the node with the QP */
3045 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003046 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003047
Faisal Latif6492cdf2008-07-24 20:50:45 -07003048 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
3049 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003050 atomic_inc(&cm_accepts);
3051
3052 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00003053 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003054
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303055 nesqp->ietf_frame_size = sizeof(struct ietf_mpa_v2);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003056 /* allocate the ietf frame and space for private data */
3057 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303058 nesqp->ietf_frame_size + conn_param->private_data_len,
3059 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003060
Faisal Latif6492cdf2008-07-24 20:50:45 -07003061 if (!nesqp->ietf_frame) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303062 nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07003063 return -ENOMEM;
3064 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303065 mpa_v2_frame = (struct ietf_mpa_v2 *)nesqp->ietf_frame;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003066
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303067 if (cm_node->mpa_frame_rev == IETF_MPA_V1)
3068 mpa_frame_offset = 4;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003069
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303070 memcpy(mpa_v2_frame->priv_data, conn_param->private_data,
3071 conn_param->private_data_len);
3072
3073 cm_build_mpa_frame(cm_node, start_buff, &buff_len, nesqp->ietf_frame, MPA_KEY_REPLY);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003074 nesqp->private_data_len = conn_param->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003075
Faisal Latif6492cdf2008-07-24 20:50:45 -07003076 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
3077 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003078
Faisal Latif6492cdf2008-07-24 20:50:45 -07003079 if (cm_id->remote_addr.sin_addr.s_addr !=
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303080 cm_id->local_addr.sin_addr.s_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003081 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003082 nesibdev = nesvnic->nesibdev;
3083 nespd = nesqp->nespd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303084 ibphysbuf.addr = nesqp->ietf_frame_pbase + mpa_frame_offset;
3085 ibphysbuf.size = buff_len;
3086 tagged_offset = (u64)(unsigned long)*start_buff;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003087 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303088 &ibphysbuf, 1,
3089 IB_ACCESS_LOCAL_WRITE,
3090 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003091 if (!ibmr) {
3092 nes_debug(NES_DBG_CM, "Unable to register memory region"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303093 "for lSMM for cm_node = %p \n",
3094 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07003095 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303096 nesqp->private_data_len + nesqp->ietf_frame_size,
3097 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003098 return -ENOMEM;
3099 }
3100
3101 ibmr->pd = &nespd->ibpd;
3102 ibmr->device = nespd->ibpd.device;
3103 nesqp->lsmm_mr = ibmr;
3104
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303105 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003106 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303107 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
3108 u64temp);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003109 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3110 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303111 NES_IWARP_SQ_WQE_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003112 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303113 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003114 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303115 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3116 (u64)(unsigned long)(*start_buff));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003117 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303118 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003119 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08003120 if (nesqp->sq_kmapped) {
3121 nesqp->sq_kmapped = 0;
3122 kunmap(nesqp->page);
3123 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003124
Faisal Latif6492cdf2008-07-24 20:50:45 -07003125 nesqp->nesqp_context->ird_ord_sizes |=
3126 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303127 NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003128 } else {
3129 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08003130 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003131 }
3132 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003133
3134
3135 /* Cache the cm_id in the qp */
3136 nesqp->cm_id = cm_id;
3137 cm_node->cm_id = cm_id;
3138
3139 /* nesqp->cm_node = (void *)cm_id->provider_data; */
3140 cm_id->provider_data = nesqp;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303141 nesqp->active_conn = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003142
Faisal Latif6492cdf2008-07-24 20:50:45 -07003143 if (cm_node->state == NES_CM_STATE_TSA)
3144 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303145 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003146
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003147 nes_cm_init_tsa_conn(nesqp, cm_node);
3148
Faisal Latif6492cdf2008-07-24 20:50:45 -07003149 nesqp->nesqp_context->tcpPorts[0] =
3150 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3151 nesqp->nesqp_context->tcpPorts[1] =
3152 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3153
Tatyana Nikolovaef3d0c42012-09-20 19:23:14 +00003154 nesqp->nesqp_context->ip0 =
Faisal Latif6492cdf2008-07-24 20:50:45 -07003155 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003156
3157 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303158 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3159 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003160
Faisal Latif6492cdf2008-07-24 20:50:45 -07003161 nesqp->nesqp_context->arp_index_vlan |=
3162 cpu_to_le32(nes_arp_table(nesdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303163 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
3164 NES_ARP_RESOLVE) << 16);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003165
3166 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003167 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003168
3169 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3170
3171 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003172 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
3173 nesqp->nesqp_context->ird_ord_sizes |=
3174 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003175
3176 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003177 nes_quad.DstIpAdrIndex =
3178 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
Tatyana Nikolovaef3d0c42012-09-20 19:23:14 +00003179 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003180 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3181 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003182
3183 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003184 crc_value = get_crc_value(&nes_quad);
3185 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003186 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303187 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003188
3189 nesqp->hte_index &= adapter->hte_index_mask;
3190 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3191
3192 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3193
Faisal Latif6492cdf2008-07-24 20:50:45 -07003194 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303195 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
3196 "private data length=%u.\n", nesqp->hwqp.qp_id,
3197 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3198 ntohs(cm_id->remote_addr.sin_port),
3199 ntohl(cm_id->local_addr.sin_addr.s_addr),
3200 ntohs(cm_id->local_addr.sin_port),
3201 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
3202 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
3203 buff_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003204
Coly Li73ac36e2009-01-07 18:09:16 -08003205 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003206 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07003207 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003208
3209 cm_event.event = IW_CM_EVENT_ESTABLISHED;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003210 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003211 cm_event.provider_data = (void *)nesqp;
3212 cm_event.local_addr = cm_id->local_addr;
3213 cm_event.remote_addr = cm_id->remote_addr;
3214 cm_event.private_data = NULL;
3215 cm_event.private_data_len = 0;
3216 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003217 attr.qp_state = IB_QPS_RTS;
3218 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003219 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003220 cm_node->loopbackpartner->mpa_frame_size =
3221 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003222 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003223 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303224 conn_param->private_data, conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003225 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
3226 }
3227 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003228 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303229 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003230
3231 return 0;
3232}
3233
3234
3235/**
3236 * nes_reject
3237 */
3238int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3239{
3240 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003241 struct nes_cm_node *loopback;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003242 struct nes_cm_core *cm_core;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303243 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003244
3245 atomic_inc(&cm_rejects);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303246 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003247 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003248 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003249 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003250
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303251 if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003252 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003253
Faisal Latif9d5ab132009-03-06 15:15:01 -08003254 if (loopback) {
3255 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3256 loopback->mpa_frame.priv_data_len = pdata_len;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303257 loopback->mpa_frame_size = pdata_len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003258 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303259 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
3260 cm_node->mpa_frame_size = pdata_len;
3261 memcpy(start_buff, pdata, pdata_len);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003262 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303263 return cm_core->api->reject(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003264}
3265
3266
3267/**
3268 * nes_connect
3269 * setup and launch cm connect node
3270 */
3271int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3272{
3273 struct ib_qp *ibqp;
3274 struct nes_qp *nesqp;
3275 struct nes_vnic *nesvnic;
3276 struct nes_device *nesdev;
3277 struct nes_cm_node *cm_node;
3278 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003279 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003280
3281 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3282 if (!ibqp)
3283 return -EINVAL;
3284 nesqp = to_nesqp(ibqp);
3285 if (!nesqp)
3286 return -EINVAL;
3287 nesvnic = to_nesvnic(nesqp->ibqp.device);
3288 if (!nesvnic)
3289 return -EINVAL;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303290 nesdev = nesvnic->nesdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003291 if (!nesdev)
3292 return -EINVAL;
3293
Faisal Latifc5a7d482009-12-09 15:54:08 -08003294 if (!(cm_id->local_addr.sin_port) || !(cm_id->remote_addr.sin_port))
3295 return -EINVAL;
3296
Faisal Latif6492cdf2008-07-24 20:50:45 -07003297 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303298 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3299 ntohl(nesvnic->local_ipaddr),
3300 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3301 ntohs(cm_id->remote_addr.sin_port),
3302 ntohl(cm_id->local_addr.sin_addr.s_addr),
3303 ntohs(cm_id->local_addr.sin_port));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003304
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003305 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003306 nesqp->active_conn = 1;
3307
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003308 /* cache the cm_id in the qp */
3309 nesqp->cm_id = cm_id;
3310
3311 cm_id->provider_data = nesqp;
3312
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003313 nesqp->private_data_len = conn_param->private_data_len;
3314 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
Tatyana Nikolovad3e51322012-05-11 10:46:16 -05003315 /* space for rdma0 read msg */
3316 if (conn_param->ord == 0)
3317 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(1);
3318
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003319 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003320 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303321 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003322
Faisal Latif6492cdf2008-07-24 20:50:45 -07003323 if (cm_id->local_addr.sin_addr.s_addr !=
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303324 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003325 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303326 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003327 apbvt_set = 1;
3328 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003329
3330 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003331 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3332 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3333 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3334 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003335 cm_info.cm_id = cm_id;
3336 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3337
3338 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003339
3340 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003341 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303342 conn_param->private_data_len, (void *)conn_param->private_data,
3343 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003344 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003345 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003346 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303347 PCI_FUNC(nesdev->pcidev->devfn),
3348 NES_MANAGE_APBVT_DEL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003349
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003350 cm_id->rem_ref(cm_id);
3351 return -ENOMEM;
3352 }
3353
Faisal Latif53094c32009-04-27 13:37:34 -07003354 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003355 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003356 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003357 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003358
3359 return 0;
3360}
3361
3362
3363/**
3364 * nes_create_listen
3365 */
3366int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3367{
3368 struct nes_vnic *nesvnic;
3369 struct nes_cm_listener *cm_node;
3370 struct nes_cm_info cm_info;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003371 int err;
3372
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003373 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3374 cm_id, ntohs(cm_id->local_addr.sin_port));
3375
3376 nesvnic = to_nesvnic(cm_id->device);
3377 if (!nesvnic)
3378 return -EINVAL;
Roland Dreier69d51022010-08-04 14:25:40 -07003379
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003380 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3381 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3382
3383 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3384 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3385
3386 /* setup listen params in our api call struct */
3387 cm_info.loc_addr = nesvnic->local_ipaddr;
3388 cm_info.loc_port = cm_id->local_addr.sin_port;
3389 cm_info.backlog = backlog;
3390 cm_info.cm_id = cm_id;
3391
3392 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3393
3394
3395 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3396 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003397 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303398 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003399 return -ENOMEM;
3400 }
3401
3402 cm_id->provider_data = cm_node;
3403
3404 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003405 err = nes_manage_apbvt(nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303406 ntohs(cm_id->local_addr.sin_port),
3407 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3408 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003409 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003410 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303411 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003412 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3413 return err;
3414 }
Faisal Latif6e10d2e2010-02-12 19:55:03 +00003415 atomic_inc(&cm_listens_created);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003416 }
3417
3418 cm_id->add_ref(cm_id);
3419 cm_id->provider_data = (void *)cm_node;
3420
3421
3422 return 0;
3423}
3424
3425
3426/**
3427 * nes_destroy_listen
3428 */
3429int nes_destroy_listen(struct iw_cm_id *cm_id)
3430{
3431 if (cm_id->provider_data)
3432 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3433 else
3434 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3435
3436 cm_id->rem_ref(cm_id);
3437
3438 return 0;
3439}
3440
3441
3442/**
3443 * nes_cm_recv
3444 */
3445int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3446{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003447 int rc = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303448
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003449 cm_packets_received++;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303450 if ((g_cm_core) && (g_cm_core->api))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003451 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303452 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003453 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303454 " cm is not setup properly.\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003455
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003456 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003457}
3458
3459
3460/**
3461 * nes_cm_start
3462 * Start and init a cm core module
3463 */
3464int nes_cm_start(void)
3465{
3466 nes_debug(NES_DBG_CM, "\n");
3467 /* create the primary CM core, pass this handle to subsequent core inits */
3468 g_cm_core = nes_cm_alloc_core();
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303469 if (g_cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003470 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303471 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003472 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003473}
3474
3475
3476/**
3477 * nes_cm_stop
3478 * stop and dealloc all cm core instances
3479 */
3480int nes_cm_stop(void)
3481{
3482 g_cm_core->api->destroy_cm_core(g_cm_core);
3483 return 0;
3484}
3485
3486
3487/**
3488 * cm_event_connected
3489 * handle a connected event, setup QPs and HW
3490 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003491static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003492{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003493 struct nes_qp *nesqp;
3494 struct nes_vnic *nesvnic;
3495 struct nes_device *nesdev;
3496 struct nes_cm_node *cm_node;
3497 struct nes_adapter *nesadapter;
3498 struct ib_qp_attr attr;
3499 struct iw_cm_id *cm_id;
3500 struct iw_cm_event cm_event;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003501 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003502 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003503 int ret;
3504
3505 /* get all our handles */
3506 cm_node = event->cm_node;
3507 cm_id = cm_node->cm_id;
3508 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3509 nesqp = (struct nes_qp *)cm_id->provider_data;
3510 nesvnic = to_nesvnic(nesqp->ibqp.device);
3511 nesdev = nesvnic->nesdev;
3512 nesadapter = nesdev->nesadapter;
3513
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303514 if (nesqp->destroyed)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003515 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003516 atomic_inc(&cm_connecteds);
3517 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303518 " local port 0x%04X. jiffies = %lu.\n",
3519 nesqp->hwqp.qp_id,
3520 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3521 ntohs(cm_id->remote_addr.sin_port),
3522 ntohs(cm_id->local_addr.sin_port),
3523 jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003524
3525 nes_cm_init_tsa_conn(nesqp, cm_node);
3526
3527 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003528 nesqp->nesqp_context->tcpPorts[0] =
3529 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3530 nesqp->nesqp_context->tcpPorts[1] =
3531 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
Tatyana Nikolovaef3d0c42012-09-20 19:23:14 +00003532 nesqp->nesqp_context->ip0 =
Faisal Latif6492cdf2008-07-24 20:50:45 -07003533 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003534
3535 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003536 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3537 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003538 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003539 nes_arp_table(nesdev,
3540 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003541 NULL, NES_ARP_RESOLVE) << 16);
3542 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3543 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3544 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3545 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003546 cpu_to_le32((u32)1 <<
3547 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003548
3549 /* Adjust tail for not having a LSMM */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303550 /*nesqp->hwqp.sq_tail = 1;*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003551
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303552 build_rdma0_msg(cm_node, &nesqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003553
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303554 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3555 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003556
3557 memset(&nes_quad, 0, sizeof(nes_quad));
3558
Faisal Latif6492cdf2008-07-24 20:50:45 -07003559 nes_quad.DstIpAdrIndex =
3560 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
Tatyana Nikolovaef3d0c42012-09-20 19:23:14 +00003561 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003562 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3563 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3564
3565 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003566 crc_value = get_crc_value(&nes_quad);
3567 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003568 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303569 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003570
3571 nesqp->hte_index &= nesadapter->hte_index_mask;
3572 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3573
3574 nesqp->ietf_frame = &cm_node->mpa_frame;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303575 nesqp->private_data_len = (u8)cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003576 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3577
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003578 /* notify OF layer we successfully created the requested connection */
3579 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003580 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003581 cm_event.provider_data = cm_id->provider_data;
3582 cm_event.local_addr.sin_family = AF_INET;
3583 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3584 cm_event.remote_addr = cm_id->remote_addr;
3585
Faisal Latif6492cdf2008-07-24 20:50:45 -07003586 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303587 cm_event.private_data_len = (u8)event->cm_node->mpa_frame_size;
3588 cm_event.ird = cm_node->ird_size;
3589 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003590
3591 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3592 ret = cm_id->event_handler(cm_id, &cm_event);
3593 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3594
3595 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003596 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303597 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003598 attr.qp_state = IB_QPS_RTS;
3599 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003600
Faisal Latif6492cdf2008-07-24 20:50:45 -07003601 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303602 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003603
3604 return;
3605}
3606
3607
3608/**
3609 * cm_event_connect_error
3610 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003611static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003612{
3613 struct nes_qp *nesqp;
3614 struct iw_cm_id *cm_id;
3615 struct iw_cm_event cm_event;
3616 /* struct nes_cm_info cm_info; */
3617 int ret;
3618
3619 if (!event->cm_node)
3620 return;
3621
3622 cm_id = event->cm_node->cm_id;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303623 if (!cm_id)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003624 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003625
3626 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3627 nesqp = cm_id->provider_data;
3628
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303629 if (!nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003630 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003631
3632 /* notify OF layer about this connection error event */
3633 /* cm_id->rem_ref(cm_id); */
3634 nesqp->cm_id = NULL;
3635 cm_id->provider_data = NULL;
3636 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003637 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003638 cm_event.provider_data = cm_id->provider_data;
3639 cm_event.local_addr = cm_id->local_addr;
3640 cm_event.remote_addr = cm_id->remote_addr;
3641 cm_event.private_data = NULL;
3642 cm_event.private_data_len = 0;
3643
Faisal Latif6492cdf2008-07-24 20:50:45 -07003644 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303645 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3646 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003647
3648 ret = cm_id->event_handler(cm_id, &cm_event);
3649 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3650 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003651 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303652 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003653 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003654
Faisal Latif6492cdf2008-07-24 20:50:45 -07003655 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003656 return;
3657}
3658
3659
3660/**
3661 * cm_event_reset
3662 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003663static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003664{
3665 struct nes_qp *nesqp;
3666 struct iw_cm_id *cm_id;
3667 struct iw_cm_event cm_event;
3668 /* struct nes_cm_info cm_info; */
3669 int ret;
3670
3671 if (!event->cm_node)
3672 return;
3673
3674 if (!event->cm_node->cm_id)
3675 return;
3676
3677 cm_id = event->cm_node->cm_id;
3678
3679 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3680 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003681 if (!nesqp)
3682 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003683
3684 nesqp->cm_id = NULL;
3685 /* cm_id->provider_data = NULL; */
3686 cm_event.event = IW_CM_EVENT_DISCONNECT;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003687 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003688 cm_event.provider_data = cm_id->provider_data;
3689 cm_event.local_addr = cm_id->local_addr;
3690 cm_event.remote_addr = cm_id->remote_addr;
3691 cm_event.private_data = NULL;
3692 cm_event.private_data_len = 0;
3693
Faisal Latif183ecfa2008-11-21 20:50:46 -06003694 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003695 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003696 atomic_inc(&cm_closes);
3697 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003698 cm_event.status = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003699 cm_event.provider_data = cm_id->provider_data;
3700 cm_event.local_addr = cm_id->local_addr;
3701 cm_event.remote_addr = cm_id->remote_addr;
3702 cm_event.private_data = NULL;
3703 cm_event.private_data_len = 0;
3704 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3705 ret = cm_id->event_handler(cm_id, &cm_event);
3706
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003707 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3708
3709
3710 /* notify OF layer about this connection error event */
3711 cm_id->rem_ref(cm_id);
3712
3713 return;
3714}
3715
3716
3717/**
3718 * cm_event_mpa_req
3719 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003720static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003721{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303722 struct iw_cm_id *cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003723 struct iw_cm_event cm_event;
3724 int ret;
3725 struct nes_cm_node *cm_node;
3726
3727 cm_node = event->cm_node;
3728 if (!cm_node)
3729 return;
3730 cm_id = cm_node->cm_id;
3731
3732 atomic_inc(&cm_connect_reqs);
3733 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303734 cm_node, cm_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003735
3736 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003737 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003738 cm_event.provider_data = (void *)cm_node;
3739
3740 cm_event.local_addr.sin_family = AF_INET;
3741 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3742 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3743
3744 cm_event.remote_addr.sin_family = AF_INET;
3745 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3746 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003747 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303748 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
3749 cm_event.ird = cm_node->ird_size;
3750 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003751
3752 ret = cm_id->event_handler(cm_id, &cm_event);
3753 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003754 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303755 __func__, __LINE__, ret);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003756 return;
3757}
3758
3759
3760static void cm_event_mpa_reject(struct nes_cm_event *event)
3761{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303762 struct iw_cm_id *cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003763 struct iw_cm_event cm_event;
3764 struct nes_cm_node *cm_node;
3765 int ret;
3766
3767 cm_node = event->cm_node;
3768 if (!cm_node)
3769 return;
3770 cm_id = cm_node->cm_id;
3771
3772 atomic_inc(&cm_connect_reqs);
3773 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303774 cm_node, cm_id, jiffies);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003775
3776 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3777 cm_event.status = -ECONNREFUSED;
3778 cm_event.provider_data = cm_id->provider_data;
3779
3780 cm_event.local_addr.sin_family = AF_INET;
3781 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3782 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3783
3784 cm_event.remote_addr.sin_family = AF_INET;
3785 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3786 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3787
3788 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303789 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003790
3791 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303792 "remove_addr=%08x\n",
3793 cm_event.local_addr.sin_addr.s_addr,
3794 cm_event.remote_addr.sin_addr.s_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003795
3796 ret = cm_id->event_handler(cm_id, &cm_event);
3797 if (ret)
3798 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303799 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003800
3801 return;
3802}
3803
3804
3805static void nes_cm_event_handler(struct work_struct *);
3806
3807/**
3808 * nes_cm_post_event
3809 * post an event to the cm event handler
3810 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003811static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003812{
3813 atomic_inc(&event->cm_node->cm_core->events_posted);
3814 add_ref_cm_node(event->cm_node);
3815 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3816 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003817 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303818 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003819
3820 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3821
3822 nes_debug(NES_DBG_CM, "Exit\n");
3823 return 0;
3824}
3825
3826
3827/**
3828 * nes_cm_event_handler
3829 * worker function to handle cm events
3830 * will free instance of nes_cm_event
3831 */
3832static void nes_cm_event_handler(struct work_struct *work)
3833{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003834 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303835 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003836 struct nes_cm_core *cm_core;
3837
Faisal Latif6492cdf2008-07-24 20:50:45 -07003838 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003839 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003840
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003841 cm_core = event->cm_node->cm_core;
3842 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303843 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003844
3845 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003846 case NES_CM_EVENT_MPA_REQ:
3847 cm_event_mpa_req(event);
3848 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303849 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003850 break;
3851 case NES_CM_EVENT_RESET:
3852 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303853 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003854 cm_event_reset(event);
3855 break;
3856 case NES_CM_EVENT_CONNECTED:
3857 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303858 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003859 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003860 cm_event_connected(event);
3861 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3862 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003863 case NES_CM_EVENT_MPA_REJECT:
3864 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303865 (event->cm_node->state == NES_CM_STATE_TSA))
Faisal Latif9d5ab132009-03-06 15:15:01 -08003866 break;
3867 cm_event_mpa_reject(event);
3868 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3869 break;
3870
Faisal Latif6492cdf2008-07-24 20:50:45 -07003871 case NES_CM_EVENT_ABORTED:
3872 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303873 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003874 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003875 cm_event_connect_error(event);
3876 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3877 break;
3878 case NES_CM_EVENT_DROPPED_PKT:
3879 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3880 break;
3881 default:
3882 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3883 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003884 }
3885
3886 atomic_dec(&cm_core->events_posted);
3887 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3888 rem_ref_cm_node(cm_core, event->cm_node);
3889 kfree(event);
3890
3891 return;
3892}