blob: da2c67db5ebb725468bc431801df3880b31e395b [file] [log] [blame]
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001/*
Tatyana Nikolovac5488c52011-11-28 14:22:29 -06002 * Copyright (c) 2006 - 2011 Intel Corporation. All rights reserved.
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34
35#define TCPOPT_TIMESTAMP 8
36
Arun Sharma600634972011-07-26 16:09:06 -070037#include <linux/atomic.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080038#include <linux/skbuff.h>
39#include <linux/ip.h>
40#include <linux/tcp.h>
41#include <linux/init.h>
42#include <linux/if_arp.h>
Chien Tungf2b2b592008-03-20 19:55:30 -050043#include <linux/if_vlan.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080044#include <linux/notifier.h>
45#include <linux/net.h>
46#include <linux/types.h>
47#include <linux/timer.h>
48#include <linux/time.h>
49#include <linux/delay.h>
50#include <linux/etherdevice.h>
51#include <linux/netdevice.h>
52#include <linux/random.h>
53#include <linux/list.h>
54#include <linux/threads.h>
Faisal Latifd2fa9b262009-12-09 15:54:28 -080055#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Bob Sharp7191a0a2008-10-03 12:21:19 -070057#include <net/arp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080058#include <net/neighbour.h>
59#include <net/route.h>
60#include <net/ip_fib.h>
Faisal Latifc11470f2009-04-27 13:38:31 -070061#include <net/tcp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080062
63#include "nes.h"
64
65u32 cm_packets_sent;
66u32 cm_packets_bounced;
67u32 cm_packets_dropped;
68u32 cm_packets_retrans;
69u32 cm_packets_created;
70u32 cm_packets_received;
Faisal Latif6e10d2e2010-02-12 19:55:03 +000071atomic_t cm_listens_created;
72atomic_t cm_listens_destroyed;
Glenn Streiff3c2d7742008-02-04 20:20:45 -080073u32 cm_backlog_drops;
74atomic_t cm_loopbacks;
75atomic_t cm_nodes_created;
76atomic_t cm_nodes_destroyed;
77atomic_t cm_accel_dropped_pkts;
78atomic_t cm_resets_recvd;
79
Tatyana Nikolova615eb712011-09-25 20:17:46 +053080static inline int mini_cm_accelerated(struct nes_cm_core *, struct nes_cm_node *);
81static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *, struct nes_vnic *, struct nes_cm_info *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080082static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
Tatyana Nikolova615eb712011-09-25 20:17:46 +053083static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *, struct nes_vnic *, u16, void *, struct nes_cm_info *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070084static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Tatyana Nikolova615eb712011-09-25 20:17:46 +053085static int mini_cm_accept(struct nes_cm_core *, struct nes_cm_node *);
86static int mini_cm_reject(struct nes_cm_core *, struct nes_cm_node *);
87static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *, struct sk_buff *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070088static int mini_cm_dealloc_core(struct nes_cm_core *);
89static int mini_cm_get(struct nes_cm_core *);
90static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070091
Tatyana Nikolova615eb712011-09-25 20:17:46 +053092static void form_cm_frame(struct sk_buff *, struct nes_cm_node *, void *, u32, void *, u32, u8);
Faisal Latif6492cdf2008-07-24 20:50:45 -070093static int add_ref_cm_node(struct nes_cm_node *);
94static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
95
Roland Dreier1a855fb2008-04-16 21:01:09 -070096static int nes_cm_disconn_true(struct nes_qp *);
97static int nes_cm_post_event(struct nes_cm_event *event);
98static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
99static void nes_disconnect_worker(struct work_struct *work);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700100
101static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800102static int send_mpa_reject(struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700103static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
104static int send_reset(struct nes_cm_node *, struct sk_buff *);
105static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fb2008-04-16 21:01:09 -0700106static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530107static void process_packet(struct nes_cm_node *, struct sk_buff *, struct nes_cm_core *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700108
109static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
110static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
111static void cleanup_retrans_entry(struct nes_cm_node *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800112static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700113static void free_retrans_entry(struct nes_cm_node *cm_node);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530114static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph, struct sk_buff *skb, int optionsize, int passive);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700115
116/* CM event handler functions */
117static void cm_event_connected(struct nes_cm_event *);
118static void cm_event_connect_error(struct nes_cm_event *);
119static void cm_event_reset(struct nes_cm_event *);
120static void cm_event_mpa_req(struct nes_cm_event *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800121static void cm_event_mpa_reject(struct nes_cm_event *);
122static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700123
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530124/* MPA build functions */
125static int cm_build_mpa_frame(struct nes_cm_node *, u8 **, u16 *, u8 *, u8);
126static void build_mpa_v2(struct nes_cm_node *, void *, u8);
127static void build_mpa_v1(struct nes_cm_node *, void *, u8);
128static void build_rdma0_msg(struct nes_cm_node *, struct nes_qp **);
129
Faisal Latif6492cdf2008-07-24 20:50:45 -0700130static void print_core(struct nes_cm_core *core);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800131
132/* External CM API Interface */
133/* instance of function pointers for client API */
134/* set address of this instance to cm_core->cm_ops at cm_core alloc */
135static struct nes_cm_ops nes_cm_api = {
136 mini_cm_accelerated,
137 mini_cm_listen,
138 mini_cm_del_listen,
139 mini_cm_connect,
140 mini_cm_close,
141 mini_cm_accept,
142 mini_cm_reject,
143 mini_cm_recv_pkt,
144 mini_cm_dealloc_core,
145 mini_cm_get,
146 mini_cm_set
147};
148
Roland Dreier1a855fb2008-04-16 21:01:09 -0700149static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800150
151atomic_t cm_connects;
152atomic_t cm_accepts;
153atomic_t cm_disconnects;
154atomic_t cm_closes;
155atomic_t cm_connecteds;
156atomic_t cm_connect_reqs;
157atomic_t cm_rejects;
158
Faisal Latif0f0bee82011-09-25 20:34:00 -0500159int nes_add_ref_cm_node(struct nes_cm_node *cm_node)
160{
161 return add_ref_cm_node(cm_node);
162}
163
164int nes_rem_ref_cm_node(struct nes_cm_node *cm_node)
165{
166 return rem_ref_cm_node(cm_node->cm_core, cm_node);
167}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800168
169/**
170 * create_event
171 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530172static struct nes_cm_event *create_event(struct nes_cm_node * cm_node,
173 enum nes_cm_event_type type)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800174{
175 struct nes_cm_event *event;
176
177 if (!cm_node->cm_id)
178 return NULL;
179
180 /* allocate an empty event */
181 event = kzalloc(sizeof(*event), GFP_ATOMIC);
182
183 if (!event)
184 return NULL;
185
186 event->type = type;
187 event->cm_node = cm_node;
188 event->cm_info.rem_addr = cm_node->rem_addr;
189 event->cm_info.loc_addr = cm_node->loc_addr;
190 event->cm_info.rem_port = cm_node->rem_port;
191 event->cm_info.loc_port = cm_node->loc_port;
192 event->cm_info.cm_id = cm_node->cm_id;
193
Faisal Latif6492cdf2008-07-24 20:50:45 -0700194 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530195 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
196 cm_node, event, type, event->cm_info.loc_addr,
197 event->cm_info.loc_port, event->cm_info.rem_addr,
198 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800199
200 nes_cm_post_event(event);
201 return event;
202}
203
204
205/**
206 * send_mpa_request
207 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700208static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800209{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530210 u8 start_addr = 0;
211 u8 *start_ptr = &start_addr;
212 u8 **start_buff = &start_ptr;
213 u16 buff_len = 0;
214
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800215 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700216 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800217 return -1;
218 }
219
220 /* send an MPA Request frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530221 cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REQUEST);
222 form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800223
Faisal Latif9d5ab132009-03-06 15:15:01 -0800224 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
225}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800226
Faisal Latif9d5ab132009-03-06 15:15:01 -0800227
228
229static int send_mpa_reject(struct nes_cm_node *cm_node)
230{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530231 struct sk_buff *skb = NULL;
232 u8 start_addr = 0;
233 u8 *start_ptr = &start_addr;
234 u8 **start_buff = &start_ptr;
235 u16 buff_len = 0;
Tatyana Nikolova81f99dc2012-01-17 10:17:30 -0600236 struct ietf_mpa_v1 *mpa_frame;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800237
238 skb = dev_alloc_skb(MAX_CM_BUFFER);
239 if (!skb) {
240 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
241 return -ENOMEM;
242 }
243
244 /* send an MPA reject frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530245 cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REPLY);
Tatyana Nikolova81f99dc2012-01-17 10:17:30 -0600246 mpa_frame = (struct ietf_mpa_v1 *)*start_buff;
247 mpa_frame->flags |= IETF_MPA_FLAGS_REJECT;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530248 form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK | SET_FIN);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800249
250 cm_node->state = NES_CM_STATE_FIN_WAIT1;
251 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800252}
253
254
255/**
256 * recv_mpa - process a received TCP pkt, we are expecting an
257 * IETF MPA frame
258 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800259static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530260 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800261{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530262 struct ietf_mpa_v1 *mpa_frame;
263 struct ietf_mpa_v2 *mpa_v2_frame;
264 struct ietf_rtr_msg *rtr_msg;
265 int mpa_hdr_len;
266 int priv_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800267
Faisal Latif9d5ab132009-03-06 15:15:01 -0800268 *type = NES_MPA_REQUEST_ACCEPT;
269
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800270 /* assume req frame is in tcp data payload */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530271 if (len < sizeof(struct ietf_mpa_v1)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800272 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800273 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800274 }
275
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530276 /* points to the beginning of the frame, which could be MPA V1 or V2 */
277 mpa_frame = (struct ietf_mpa_v1 *)buffer;
278 mpa_hdr_len = sizeof(struct ietf_mpa_v1);
279 priv_data_len = ntohs(mpa_frame->priv_data_len);
280
Faisal Latif1cf078c2009-12-09 15:53:54 -0800281 /* make sure mpa private data len is less than 512 bytes */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530282 if (priv_data_len > IETF_MAX_PRIV_DATA_LEN) {
Faisal Latif1cf078c2009-12-09 15:53:54 -0800283 nes_debug(NES_DBG_CM, "The received Length of Private"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530284 " Data field exceeds 512 octets\n");
Faisal Latif1cf078c2009-12-09 15:53:54 -0800285 return -EINVAL;
286 }
287 /*
288 * make sure MPA receiver interoperate with the
289 * received MPA version and MPA key information
290 *
291 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530292 if (mpa_frame->rev != IETF_MPA_V1 && mpa_frame->rev != IETF_MPA_V2) {
Faisal Latif1cf078c2009-12-09 15:53:54 -0800293 nes_debug(NES_DBG_CM, "The received mpa version"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530294 " is not supported\n");
Faisal Latif1cf078c2009-12-09 15:53:54 -0800295 return -EINVAL;
296 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530297 /*
298 * backwards compatibility only
299 */
300 if (mpa_frame->rev > cm_node->mpa_frame_rev) {
301 nes_debug(NES_DBG_CM, "The received mpa version"
302 " can not be interoperated\n");
303 return -EINVAL;
304 } else {
305 cm_node->mpa_frame_rev = mpa_frame->rev;
306 }
307
Faisal Latif1cf078c2009-12-09 15:53:54 -0800308 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
309 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
310 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
311 return -EINVAL;
312 }
313 } else {
314 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
315 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
316 return -EINVAL;
317 }
318 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800319
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530320
321 if (priv_data_len + mpa_hdr_len != len) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800322 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530323 " complete (%x + %x != %x)\n",
324 priv_data_len, mpa_hdr_len, len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800325 return -EINVAL;
326 }
327 /* make sure it does not exceed the max size */
328 if (len > MAX_CM_BUFFER) {
329 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530330 " (%x + %x != %x)\n",
331 priv_data_len, mpa_hdr_len, len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800332 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800333 }
334
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530335 cm_node->mpa_frame_size = priv_data_len;
336
337 switch (mpa_frame->rev) {
338 case IETF_MPA_V2: {
339 u16 ird_size;
340 u16 ord_size;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800341 u16 rtr_ctrl_ird;
342 u16 rtr_ctrl_ord;
343
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530344 mpa_v2_frame = (struct ietf_mpa_v2 *)buffer;
345 mpa_hdr_len += IETF_RTR_MSG_SIZE;
346 cm_node->mpa_frame_size -= IETF_RTR_MSG_SIZE;
347 rtr_msg = &mpa_v2_frame->rtr_msg;
348
349 /* parse rtr message */
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800350 rtr_ctrl_ird = ntohs(rtr_msg->ctrl_ird);
351 rtr_ctrl_ord = ntohs(rtr_msg->ctrl_ord);
352 ird_size = rtr_ctrl_ird & IETF_NO_IRD_ORD;
353 ord_size = rtr_ctrl_ord & IETF_NO_IRD_ORD;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530354
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800355 if (!(rtr_ctrl_ird & IETF_PEER_TO_PEER)) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530356 /* send reset */
357 return -EINVAL;
358 }
359
360 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
361 /* responder */
362 if (cm_node->ord_size > ird_size)
363 cm_node->ord_size = ird_size;
364 } else {
365 /* initiator */
366 if (cm_node->ord_size > ird_size)
367 cm_node->ord_size = ird_size;
368
369 if (cm_node->ird_size < ord_size) {
370 /* no resources available */
371 /* send terminate message */
372 return -EINVAL;
373 }
374 }
375
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800376 if (rtr_ctrl_ord & IETF_RDMA0_READ) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530377 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800378 } else if (rtr_ctrl_ord & IETF_RDMA0_WRITE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530379 cm_node->send_rdma0_op = SEND_RDMA_WRITE_ZERO;
380 } else { /* Not supported RDMA0 operation */
381 return -EINVAL;
382 }
383 break;
384 }
385 case IETF_MPA_V1:
386 default:
387 break;
388 }
389
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800390 /* copy entire MPA frame to our cm_node's frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530391 memcpy(cm_node->mpa_frame_buf, buffer + mpa_hdr_len, cm_node->mpa_frame_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800392
Faisal Latif9d5ab132009-03-06 15:15:01 -0800393 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
394 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800395 return 0;
396}
397
398
399/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800400 * form_cm_frame - get a free packet and build empty frame Use
401 * node info to build.
402 */
Chien Tung6098d102008-11-21 20:51:01 -0600403static void form_cm_frame(struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530404 struct nes_cm_node *cm_node, void *options, u32 optionsize,
405 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800406{
407 struct tcphdr *tcph;
408 struct iphdr *iph;
409 struct ethhdr *ethh;
410 u8 *buf;
411 u16 packetsize = sizeof(*iph);
412
413 packetsize += sizeof(*tcph);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530414 packetsize += optionsize + datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800415
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530416 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800417 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
418
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800419 buf = skb_put(skb, packetsize + ETH_HLEN);
420
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530421 ethh = (struct ethhdr *)buf;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800422 buf += ETH_HLEN;
423
424 iph = (struct iphdr *)buf;
425 buf += sizeof(*iph);
426 tcph = (struct tcphdr *)buf;
427 skb_reset_mac_header(skb);
428 skb_set_network_header(skb, ETH_HLEN);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530429 skb_set_transport_header(skb, ETH_HLEN + sizeof(*iph));
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800430 buf += sizeof(*tcph);
431
432 skb->ip_summed = CHECKSUM_PARTIAL;
433 skb->protocol = htons(0x800);
434 skb->data_len = 0;
435 skb->mac_len = ETH_HLEN;
436
437 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
438 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
439 ethh->h_proto = htons(0x0800);
440
441 iph->version = IPVERSION;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530442 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800443 iph->tos = 0;
444 iph->tot_len = htons(packetsize);
445 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
446
447 iph->frag_off = htons(0x4000);
448 iph->ttl = 0x40;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530449 iph->protocol = 0x06; /* IPPROTO_TCP */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800450
451 iph->saddr = htonl(cm_node->loc_addr);
452 iph->daddr = htonl(cm_node->rem_addr);
453
454 tcph->source = htons(cm_node->loc_port);
455 tcph->dest = htons(cm_node->rem_port);
456 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
457
458 if (flags & SET_ACK) {
459 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
460 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
461 tcph->ack = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530462 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800463 tcph->ack_seq = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530464 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800465
466 if (flags & SET_SYN) {
467 cm_node->tcp_cntxt.loc_seq_num++;
468 tcph->syn = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530469 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700470 cm_node->tcp_cntxt.loc_seq_num += datasize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530471 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800472
Faisal Latif6492cdf2008-07-24 20:50:45 -0700473 if (flags & SET_FIN) {
474 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800475 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700476 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800477
478 if (flags & SET_RST)
479 tcph->rst = 1;
480
481 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
482 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
483 tcph->urg_ptr = 0;
484 if (optionsize)
485 memcpy(buf, options, optionsize);
486 buf += optionsize;
487 if (datasize)
488 memcpy(buf, data, datasize);
489
490 skb_shinfo(skb)->nr_frags = 0;
491 cm_packets_created++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800492}
493
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800494/**
495 * print_core - dump a cm core
496 */
497static void print_core(struct nes_cm_core *core)
498{
499 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
500 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
501 if (!core)
502 return;
503 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800504
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530505 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800506
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800507 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
508 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
509
510 nes_debug(NES_DBG_CM, "core : %p \n", core);
511
512 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
513}
514
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530515/**
516 * cm_build_mpa_frame - build a MPA V1 frame or MPA V2 frame
517 */
518static int cm_build_mpa_frame(struct nes_cm_node *cm_node, u8 **start_buff,
519 u16 *buff_len, u8 *pci_mem, u8 mpa_key)
520{
521 int ret = 0;
522
523 *start_buff = (pci_mem) ? pci_mem : &cm_node->mpa_frame_buf[0];
524
525 switch (cm_node->mpa_frame_rev) {
526 case IETF_MPA_V1:
527 *start_buff = (u8 *)*start_buff + sizeof(struct ietf_rtr_msg);
528 *buff_len = sizeof(struct ietf_mpa_v1) + cm_node->mpa_frame_size;
529 build_mpa_v1(cm_node, *start_buff, mpa_key);
530 break;
531 case IETF_MPA_V2:
532 *buff_len = sizeof(struct ietf_mpa_v2) + cm_node->mpa_frame_size;
533 build_mpa_v2(cm_node, *start_buff, mpa_key);
534 break;
535 default:
536 ret = -EINVAL;
537 }
538 return ret;
539}
540
541/**
542 * build_mpa_v2 - build a MPA V2 frame
543 */
544static void build_mpa_v2(struct nes_cm_node *cm_node,
545 void *start_addr, u8 mpa_key)
546{
547 struct ietf_mpa_v2 *mpa_frame = (struct ietf_mpa_v2 *)start_addr;
548 struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800549 u16 ctrl_ird;
550 u16 ctrl_ord;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530551
552 /* initialize the upper 5 bytes of the frame */
553 build_mpa_v1(cm_node, start_addr, mpa_key);
554 mpa_frame->flags |= IETF_MPA_V2_FLAG; /* set a bit to indicate MPA V2 */
555 mpa_frame->priv_data_len += htons(IETF_RTR_MSG_SIZE);
556
557 /* initialize RTR msg */
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800558 ctrl_ird = (cm_node->ird_size > IETF_NO_IRD_ORD) ?
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530559 IETF_NO_IRD_ORD : cm_node->ird_size;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800560 ctrl_ord = (cm_node->ord_size > IETF_NO_IRD_ORD) ?
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530561 IETF_NO_IRD_ORD : cm_node->ord_size;
562
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800563 ctrl_ird |= IETF_PEER_TO_PEER;
564 ctrl_ird |= IETF_FLPDU_ZERO_LEN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530565
566 switch (mpa_key) {
567 case MPA_KEY_REQUEST:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800568 ctrl_ord |= IETF_RDMA0_WRITE;
569 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530570 break;
571 case MPA_KEY_REPLY:
572 switch (cm_node->send_rdma0_op) {
573 case SEND_RDMA_WRITE_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800574 ctrl_ord |= IETF_RDMA0_WRITE;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530575 break;
576 case SEND_RDMA_READ_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800577 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530578 break;
579 }
580 }
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800581 rtr_msg->ctrl_ird = htons(ctrl_ird);
582 rtr_msg->ctrl_ord = htons(ctrl_ord);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530583}
584
585/**
586 * build_mpa_v1 - build a MPA V1 frame
587 */
588static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
589{
590 struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
591
592 switch (mpa_key) {
593 case MPA_KEY_REQUEST:
594 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
595 break;
596 case MPA_KEY_REPLY:
597 memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
598 break;
599 }
600 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
601 mpa_frame->rev = cm_node->mpa_frame_rev;
602 mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
603}
604
605static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
606{
607 u64 u64temp;
608 struct nes_qp *nesqp = *nesqp_addr;
609 struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
610
611 u64temp = (unsigned long)nesqp;
612 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
613 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
614
615 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
616 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
617
618 switch (cm_node->send_rdma0_op) {
619 case SEND_RDMA_WRITE_ZERO:
620 nes_debug(NES_DBG_CM, "Sending first write.\n");
621 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
622 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
623 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
624 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
625 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
626 break;
627
628 case SEND_RDMA_READ_ZERO:
629 default:
630 if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO) {
631 printk(KERN_ERR "%s[%u]: Unsupported RDMA0 len operation=%u\n",
632 __func__, __LINE__, cm_node->send_rdma0_op);
633 WARN_ON(1);
634 }
635 nes_debug(NES_DBG_CM, "Sending first rdma operation.\n");
636 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
637 cpu_to_le32(NES_IWARP_SQ_OP_RDMAR);
638 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX] = 1;
639 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_HIGH_IDX] = 0;
640 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] = 0;
641 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_STAG_IDX] = 1;
642 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 1;
643 break;
644 }
645
646 if (nesqp->sq_kmapped) {
647 nesqp->sq_kmapped = 0;
648 kunmap(nesqp->page);
649 }
650
651 /*use the reserved spot on the WQ for the extra first WQE*/
652 nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
653 NES_QPCONTEXT_ORDIRD_WRPDU |
654 NES_QPCONTEXT_ORDIRD_ALSMM));
655 nesqp->skip_lsmm = 1;
656 nesqp->hwqp.sq_tail = 0;
657}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800658
659/**
660 * schedule_nes_timer
661 * note - cm_node needs to be protected before calling this. Encase in:
662 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
663 */
664int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530665 enum nes_timer_type type, int send_retrans,
666 int close_when_complete)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800667{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530668 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700669 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800670 struct nes_timer_entry *new_send;
671 int ret = 0;
672 u32 was_timer_set;
673
674 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
675 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800676 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800677
678 /* new_send->timetosend = currenttime */
679 new_send->retrycount = NES_DEFAULT_RETRYS;
680 new_send->retranscount = NES_DEFAULT_RETRANS;
681 new_send->skb = skb;
682 new_send->timetosend = jiffies;
683 new_send->type = type;
684 new_send->netdev = cm_node->netdev;
685 new_send->send_retrans = send_retrans;
686 new_send->close_when_complete = close_when_complete;
687
688 if (type == NES_TIMER_TYPE_CLOSE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530689 new_send->timetosend += (HZ / 10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800690 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700691 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800692 WARN_ON(1);
693 return -EINVAL;
694 }
695 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800696 }
697
698 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700699 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800700 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700701 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
702 cm_node->send_entry = new_send;
703 add_ref_cm_node(cm_node);
704 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
705 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800706
707 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
708 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700709 nes_debug(NES_DBG_CM, "Error sending packet %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530710 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800711 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700712 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800713 } else {
714 cm_packets_sent++;
715 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700716 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800717 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700718 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800719 return ret;
720 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800721 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800722 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800723
724 was_timer_set = timer_pending(&cm_core->tcp_timer);
725
726 if (!was_timer_set) {
727 cm_core->tcp_timer.expires = new_send->timetosend;
728 add_timer(&cm_core->tcp_timer);
729 }
730
731 return ret;
732}
733
Faisal Latif9d5ab132009-03-06 15:15:01 -0800734static void nes_retrans_expired(struct nes_cm_node *cm_node)
735{
Faisal Latif68237a02009-06-22 22:53:28 -0700736 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latifdae58722010-08-14 21:04:56 +0000737 enum nes_cm_node_state state = cm_node->state;
738 cm_node->state = NES_CM_STATE_CLOSED;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530739
Faisal Latifdae58722010-08-14 21:04:56 +0000740 switch (state) {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800741 case NES_CM_STATE_SYN_RCVD:
742 case NES_CM_STATE_CLOSING:
743 rem_ref_cm_node(cm_node->cm_core, cm_node);
744 break;
745 case NES_CM_STATE_LAST_ACK:
746 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700747 if (cm_node->cm_id)
748 cm_id->rem_ref(cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800749 send_reset(cm_node, NULL);
750 break;
751 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800752 add_ref_cm_node(cm_node);
753 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800754 create_event(cm_node, NES_CM_EVENT_ABORTED);
755 }
756}
757
758static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
759{
760 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
761 struct iw_cm_id *cm_id = cm_node->cm_id;
762 struct nes_qp *nesqp;
763 unsigned long qplockflags;
764
765 if (!recv_entry)
766 return;
767 nesqp = (struct nes_qp *)recv_entry->skb;
768 if (nesqp) {
769 spin_lock_irqsave(&nesqp->lock, qplockflags);
770 if (nesqp->cm_id) {
771 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530772 "refcount = %d: HIT A "
773 "NES_TIMER_TYPE_CLOSE with something "
774 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
775 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800776 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
777 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
778 nesqp->ibqp_state = IB_QPS_ERR;
779 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
780 nes_cm_disconn(nesqp);
781 } else {
782 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
783 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530784 "refcount = %d: HIT A "
785 "NES_TIMER_TYPE_CLOSE with nothing "
786 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
787 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800788 }
789 } else if (rem_node) {
790 /* TIME_WAIT state */
791 rem_ref_cm_node(cm_node->cm_core, cm_node);
792 }
793 if (cm_node->cm_id)
794 cm_id->rem_ref(cm_id);
795 kfree(recv_entry);
796 cm_node->recv_entry = NULL;
797}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800798
799/**
800 * nes_cm_timer_tick
801 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700802static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800803{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800804 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800805 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800806 struct nes_cm_node *cm_node;
807 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800808 struct list_head *list_core_temp;
809 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800810 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800811 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700812 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800813 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800814
Faisal Latif879e5bd2008-11-21 20:50:41 -0600815 struct list_head timer_list;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530816
Faisal Latif879e5bd2008-11-21 20:50:41 -0600817 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800818 spin_lock_irqsave(&cm_core->ht_lock, flags);
819
Faisal Latif6492cdf2008-07-24 20:50:45 -0700820 list_for_each_safe(list_node, list_core_temp,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530821 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800822 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800823 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600824 add_ref_cm_node(cm_node);
825 list_add(&cm_node->timer_entry, &timer_list);
826 }
827 }
828 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
829
830 list_for_each_safe(list_node, list_core_temp, &timer_list) {
831 cm_node = container_of(list_node, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530832 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800833 recv_entry = cm_node->recv_entry;
834
835 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700836 if (time_after(recv_entry->timetosend, jiffies)) {
837 if (nexttimeout > recv_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530838 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800839 nexttimeout = recv_entry->timetosend;
840 settimer = 1;
841 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530842 } else {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800843 handle_recv_entry(cm_node, 1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530844 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800845 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800846
847 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700848 do {
849 send_entry = cm_node->send_entry;
850 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600851 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800852 if (time_after(send_entry->timetosend, jiffies)) {
853 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700854 if ((nexttimeout >
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530855 send_entry->timetosend) ||
856 !settimer) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700857 nexttimeout =
858 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800859 settimer = 1;
860 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800861 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700862 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800863 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800864 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800865 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700866
867 if ((cm_node->state == NES_CM_STATE_TSA) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530868 (cm_node->state == NES_CM_STATE_CLOSED)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700869 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600870 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800871 }
872
Faisal Latif6492cdf2008-07-24 20:50:45 -0700873 if (!send_entry->retranscount ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530874 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800875 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700876 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800877
Faisal Latif6492cdf2008-07-24 20:50:45 -0700878 spin_unlock_irqrestore(
879 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800880 nes_retrans_expired(cm_node);
881 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700882 spin_lock_irqsave(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530883 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600884 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800885 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800886 atomic_inc(&send_entry->skb->users);
887 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700888 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530889 "for node %p, jiffies = %lu, time to send = "
890 "%lu, retranscount = %u, send_entry->seq_num = "
891 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
892 "0x%08X\n", send_entry, cm_node, jiffies,
893 send_entry->timetosend,
894 send_entry->retranscount,
895 send_entry->seq_num,
896 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800897
Faisal Latif6492cdf2008-07-24 20:50:45 -0700898 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530899 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800900 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700901 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800902 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700903 nes_debug(NES_DBG_CM, "rexmit failed for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530904 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800905 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800906 send_entry->retrycount--;
907 nexttimeout = jiffies + NES_SHORT_TIME;
908 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600909 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800910 } else {
911 cm_packets_sent++;
912 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700913 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530914 "%u, retry count = %u.\n",
915 send_entry->retranscount,
916 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800917 if (send_entry->send_retrans) {
918 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700919 timetosend = (NES_RETRY_TIMEOUT <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530920 (NES_DEFAULT_RETRANS - send_entry->retranscount));
Faisal Latif4e9c3902009-04-27 13:39:36 -0700921
Faisal Latif6492cdf2008-07-24 20:50:45 -0700922 send_entry->timetosend = jiffies +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530923 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700924 if (nexttimeout > send_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530925 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800926 nexttimeout = send_entry->timetosend;
927 settimer = 1;
928 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800929 } else {
930 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700931 close_when_complete =
932 send_entry->close_when_complete;
933 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530934 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700935 free_retrans_entry(cm_node);
936 if (close_when_complete)
937 rem_ref_cm_node(cm_node->cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530938 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800939 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700940 } while (0);
941
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800942 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700943 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800944 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800945
946 if (settimer) {
947 if (!timer_pending(&cm_core->tcp_timer)) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530948 cm_core->tcp_timer.expires = nexttimeout;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800949 add_timer(&cm_core->tcp_timer);
950 }
951 }
952}
953
954
955/**
956 * send_syn
957 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700958static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530959 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800960{
961 int ret;
962 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800963 char optionsbuffer[sizeof(struct option_mss) +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530964 sizeof(struct option_windowscale) + sizeof(struct option_base) +
965 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800966
967 int optionssize = 0;
968 /* Sending MSS option */
969 union all_known_options *options;
970
971 if (!cm_node)
972 return -EINVAL;
973
974 options = (union all_known_options *)&optionsbuffer[optionssize];
975 options->as_mss.optionnum = OPTION_NUMBER_MSS;
976 options->as_mss.length = sizeof(struct option_mss);
977 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
978 optionssize += sizeof(struct option_mss);
979
980 options = (union all_known_options *)&optionsbuffer[optionssize];
981 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
982 options->as_windowscale.length = sizeof(struct option_windowscale);
983 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
984 optionssize += sizeof(struct option_windowscale);
985
Faisal Latif6492cdf2008-07-24 20:50:45 -0700986 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800987 options = (union all_known_options *)&optionsbuffer[optionssize];
988 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
989 options->as_base.length = sizeof(struct option_base);
990 optionssize += sizeof(struct option_base);
991 /* we need the size to be a multiple of 4 */
992 options = (union all_known_options *)&optionsbuffer[optionssize];
993 options->as_end = 1;
994 optionssize += 1;
995 options = (union all_known_options *)&optionsbuffer[optionssize];
996 options->as_end = 1;
997 optionssize += 1;
998 }
999
1000 options = (union all_known_options *)&optionsbuffer[optionssize];
1001 options->as_end = OPTION_NUMBER_END;
1002 optionssize += 1;
1003
Faisal Latif6492cdf2008-07-24 20:50:45 -07001004 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001005 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001006 if (!skb) {
1007 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1008 return -1;
1009 }
1010
1011 if (sendack)
1012 flags |= SET_ACK;
1013
1014 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
1015 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1016
1017 return ret;
1018}
1019
1020
1021/**
1022 * send_reset
1023 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001024static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001025{
1026 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001027 int flags = SET_RST | SET_ACK;
1028
Faisal Latif6492cdf2008-07-24 20:50:45 -07001029 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001030 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001031 if (!skb) {
1032 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001033 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001034 }
1035
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001036 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
1037 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
1038
1039 return ret;
1040}
1041
1042
1043/**
1044 * send_ack
1045 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001046static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001047{
1048 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001049
1050 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001051 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001052
1053 if (!skb) {
1054 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1055 return -1;
1056 }
1057
1058 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
1059 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
1060
1061 return ret;
1062}
1063
1064
1065/**
1066 * send_fin
1067 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07001068static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001069{
1070 int ret;
1071
1072 /* if we didn't get a frame get one */
1073 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001074 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001075
1076 if (!skb) {
1077 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1078 return -1;
1079 }
1080
1081 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
1082 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1083
1084 return ret;
1085}
1086
1087
1088/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001089 * find_node - find a cm node that matches the reference cm node
1090 */
1091static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301092 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001093{
1094 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001095 struct list_head *hte;
1096 struct nes_cm_node *cm_node;
1097
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001098 /* get a handle on the hte */
1099 hte = &cm_core->connected_nodes;
1100
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001101 /* walk list and find cm_node associated with this session ID */
1102 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001103 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001104 /* compare quad, return node handle if a match */
1105 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301106 cm_node->loc_addr, cm_node->loc_port,
1107 loc_addr, loc_port,
1108 cm_node->rem_addr, cm_node->rem_port,
1109 rem_addr, rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001110 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301111 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001112 add_ref_cm_node(cm_node);
1113 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1114 return cm_node;
1115 }
1116 }
1117 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1118
1119 /* no owner node */
1120 return NULL;
1121}
1122
1123
1124/**
1125 * find_listener - find a cm node listening on this addr-port pair
1126 */
1127static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301128 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001129{
1130 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001131 struct nes_cm_listener *listen_node;
1132
1133 /* walk list and find cm_node associated with this session ID */
1134 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001135 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001136 /* compare node pair, return node handle if a match */
1137 if (((listen_node->loc_addr == dst_addr) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301138 listen_node->loc_addr == 0x00000000) &&
1139 (listen_node->loc_port == dst_port) &&
1140 (listener_state & listen_node->listener_state)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001141 atomic_inc(&listen_node->ref_count);
1142 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1143 return listen_node;
1144 }
1145 }
1146 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1147
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001148 /* no listener */
1149 return NULL;
1150}
1151
1152
1153/**
1154 * add_hte_node - add a cm node to the hash table
1155 */
1156static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1157{
1158 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001159 struct list_head *hte;
1160
1161 if (!cm_node || !cm_core)
1162 return -EINVAL;
1163
Faisal Latif6492cdf2008-07-24 20:50:45 -07001164 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301165 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001166
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001167 spin_lock_irqsave(&cm_core->ht_lock, flags);
1168
1169 /* get a handle on the hash table element (list head for this slot) */
1170 hte = &cm_core->connected_nodes;
1171 list_add_tail(&cm_node->list, hte);
1172 atomic_inc(&cm_core->ht_node_cnt);
1173
1174 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1175
1176 return 0;
1177}
1178
1179
1180/**
1181 * mini_cm_dec_refcnt_listen
1182 */
1183static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301184 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001185{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001186 int ret = -EINVAL;
1187 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001188 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001189 struct list_head *list_pos = NULL;
1190 struct list_head *list_temp = NULL;
1191 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -06001192 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001193
1194 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301195 "refcnt=%d\n", listener, free_hanging_nodes,
1196 atomic_read(&listener->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001197 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -06001198 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001199 if (free_hanging_nodes) {
1200 spin_lock_irqsave(&cm_core->ht_lock, flags);
1201 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -06001202 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001203 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301204 list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001205 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -06001206 (!cm_node->accelerated)) {
1207 add_ref_cm_node(cm_node);
1208 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001209 }
1210 }
1211 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1212 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001213
1214 list_for_each_safe(list_pos, list_temp, &reset_list) {
1215 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301216 reset_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001217 {
1218 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -08001219 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001220 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
1221 rem_ref_cm_node(cm_node->cm_core, cm_node);
1222 } else {
1223 if (!loopback) {
1224 cleanup_retrans_entry(cm_node);
1225 err = send_reset(cm_node, NULL);
1226 if (err) {
1227 cm_node->state =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301228 NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001229 WARN_ON(1);
1230 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08001231 old_state = cm_node->state;
1232 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1233 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1234 rem_ref_cm_node(
1235 cm_node->cm_core,
1236 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001237 }
1238 } else {
1239 struct nes_cm_event event;
1240
1241 event.cm_node = loopback;
1242 event.cm_info.rem_addr =
1243 loopback->rem_addr;
1244 event.cm_info.loc_addr =
1245 loopback->loc_addr;
1246 event.cm_info.rem_port =
1247 loopback->rem_port;
1248 event.cm_info.loc_port =
1249 loopback->loc_port;
1250 event.cm_info.cm_id = loopback->cm_id;
Faisal Latif43093b92010-02-12 19:57:04 +00001251 add_ref_cm_node(loopback);
1252 loopback->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001253 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001254 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001255
Faisal Latif9d5ab132009-03-06 15:15:01 -08001256 rem_ref_cm_node(cm_node->cm_core,
1257 cm_node);
1258
1259 }
1260 }
1261 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001262 }
1263
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001264 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1265 if (!atomic_dec_return(&listener->ref_count)) {
1266 list_del(&listener->list);
1267
1268 /* decrement our listen node count */
1269 atomic_dec(&cm_core->listen_node_cnt);
1270
1271 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1272
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301273 if (listener->nesvnic)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001274 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301275 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001276
1277 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1278
1279 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001280 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001281 ret = 0;
Faisal Latif6e10d2e2010-02-12 19:55:03 +00001282 atomic_inc(&cm_listens_destroyed);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001283 } else {
1284 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1285 }
1286 if (listener) {
1287 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1288 nes_debug(NES_DBG_CM, "destroying listener (%p)"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301289 " with non-zero pending accepts=%u\n",
1290 listener, atomic_read(&listener->pend_accepts_cnt));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001291 }
1292
1293 return ret;
1294}
1295
1296
1297/**
1298 * mini_cm_del_listen
1299 */
1300static int mini_cm_del_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301301 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001302{
1303 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1304 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1305 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1306}
1307
1308
1309/**
1310 * mini_cm_accelerated
1311 */
1312static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301313 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001314{
1315 u32 was_timer_set;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301316
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001317 cm_node->accelerated = 1;
1318
1319 if (cm_node->accept_pend) {
1320 BUG_ON(!cm_node->listener);
1321 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001322 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001323 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1324 }
1325
1326 was_timer_set = timer_pending(&cm_core->tcp_timer);
1327 if (!was_timer_set) {
1328 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1329 add_timer(&cm_core->tcp_timer);
1330 }
1331
1332 return 0;
1333}
1334
1335
1336/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001337 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001338 */
Faisal Latif7a576df2009-12-09 15:54:33 -08001339static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001340{
1341 struct rtable *rt;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001342 struct neighbour *neigh;
Faisal Latif7a576df2009-12-09 15:54:33 -08001343 int rc = arpindex;
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001344 struct net_device *netdev;
Faisal Latif7a576df2009-12-09 15:54:33 -08001345 struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001346
David S. Miller78fbfd82011-03-12 00:00:52 -05001347 rt = ip_route_output(&init_net, htonl(dst_ip), 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001348 if (IS_ERR(rt)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001349 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301350 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001351 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001352 }
1353
Roland Dreiercf55bb22011-03-24 17:13:20 -07001354 if (netif_is_bond_slave(nesvnic->netdev))
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001355 netdev = nesvnic->netdev->master;
1356 else
1357 netdev = nesvnic->netdev;
1358
David Miller40e2bb52011-12-02 16:52:27 +00001359 rcu_read_lock();
1360 neigh = dst_get_neighbour_noref(&rt->dst);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001361 if (neigh) {
1362 if (neigh->nud_state & NUD_VALID) {
1363 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001364 " is %pM, Gateway is 0x%08X \n", dst_ip,
1365 neigh->ha, ntohl(rt->rt_gateway));
Faisal Latif7a576df2009-12-09 15:54:33 -08001366
1367 if (arpindex >= 0) {
1368 if (!memcmp(nesadapter->arp_table[arpindex].mac_addr,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301369 neigh->ha, ETH_ALEN)) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001370 /* Mac address same as in nes_arp_table */
Roland Dreier7525c852012-01-27 09:54:30 -08001371 goto out;
Faisal Latif7a576df2009-12-09 15:54:33 -08001372 }
1373
1374 nes_manage_arp_cache(nesvnic->netdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301375 nesadapter->arp_table[arpindex].mac_addr,
1376 dst_ip, NES_ARP_DELETE);
Faisal Latif7a576df2009-12-09 15:54:33 -08001377 }
1378
Bob Sharp7191a0a2008-10-03 12:21:19 -07001379 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1380 dst_ip, NES_ARP_ADD);
1381 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1382 NES_ARP_RESOLVE);
David Miller40e2bb52011-12-02 16:52:27 +00001383 } else {
1384 neigh_event_send(neigh, NULL);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001385 }
Bob Sharp7191a0a2008-10-03 12:21:19 -07001386 }
Roland Dreier7525c852012-01-27 09:54:30 -08001387
1388out:
David Miller40e2bb52011-12-02 16:52:27 +00001389 rcu_read_unlock();
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001390 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001391 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001392}
1393
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001394/**
1395 * make_cm_node - create a new instance of a cm node
1396 */
1397static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301398 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1399 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001400{
1401 struct nes_cm_node *cm_node;
1402 struct timespec ts;
Faisal Latif7a576df2009-12-09 15:54:33 -08001403 int oldarpindex = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001404 int arpindex = 0;
1405 struct nes_device *nesdev;
1406 struct nes_adapter *nesadapter;
1407
1408 /* create an hte and cm_node for this instance */
1409 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1410 if (!cm_node)
1411 return NULL;
1412
1413 /* set our node specific transport info */
1414 cm_node->loc_addr = cm_info->loc_addr;
1415 cm_node->rem_addr = cm_info->rem_addr;
1416 cm_node->loc_port = cm_info->loc_port;
1417 cm_node->rem_port = cm_info->rem_port;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301418
1419 cm_node->mpa_frame_rev = mpa_version;
1420 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
1421 cm_node->ird_size = IETF_NO_IRD_ORD;
1422 cm_node->ord_size = IETF_NO_IRD_ORD;
1423
Harvey Harrison63779432008-10-31 00:56:00 -07001424 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1425 &cm_node->loc_addr, cm_node->loc_port,
1426 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001427 cm_node->listener = listener;
1428 cm_node->netdev = nesvnic->netdev;
1429 cm_node->cm_id = cm_info->cm_id;
1430 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1431
Faisal Latif6492cdf2008-07-24 20:50:45 -07001432 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301433 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001434
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001435 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001436
1437 cm_node->loopbackpartner = NULL;
1438 atomic_set(&cm_node->ref_count, 1);
1439 /* associate our parent CM core */
1440 cm_node->cm_core = cm_core;
1441 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1442 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1443 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301444 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001445 ts = current_kernel_time();
1446 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1447 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301448 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001449 cm_node->tcp_cntxt.rcv_nxt = 0;
1450 /* get a unique session ID , add thread_id to an upcounter to handle race */
1451 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001452 cm_node->conn_type = cm_info->conn_type;
1453 cm_node->apbvt_set = 0;
1454 cm_node->accept_pend = 0;
1455
1456 cm_node->nesvnic = nesvnic;
1457 /* get some device handles, for arp lookup */
1458 nesdev = nesvnic->nesdev;
1459 nesadapter = nesdev->nesadapter;
1460
1461 cm_node->loopbackpartner = NULL;
Faisal Latif7a576df2009-12-09 15:54:33 -08001462
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001463 /* get the mac addr for the remote node */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301464 if (ipv4_is_loopback(htonl(cm_node->rem_addr))) {
Chien Tung1ee86552008-11-21 20:51:04 -06001465 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301466 } else {
Faisal Latif7a576df2009-12-09 15:54:33 -08001467 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1468 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);
Faisal Latif7a576df2009-12-09 15:54:33 -08001469 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001470 if (arpindex < 0) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001471 kfree(cm_node);
1472 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001473 }
1474
1475 /* copy the mac addr to node context */
1476 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001477 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1478 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001479
1480 add_hte_node(cm_core, cm_node);
1481 atomic_inc(&cm_nodes_created);
1482
1483 return cm_node;
1484}
1485
1486
1487/**
1488 * add_ref_cm_node - destroy an instance of a cm node
1489 */
1490static int add_ref_cm_node(struct nes_cm_node *cm_node)
1491{
1492 atomic_inc(&cm_node->ref_count);
1493 return 0;
1494}
1495
1496
1497/**
1498 * rem_ref_cm_node - destroy an instance of a cm node
1499 */
1500static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301501 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001502{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001503 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001504 struct nes_qp *nesqp;
1505
1506 if (!cm_node)
1507 return -EINVAL;
1508
1509 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1510 if (atomic_dec_return(&cm_node->ref_count)) {
1511 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1512 return 0;
1513 }
1514 list_del(&cm_node->list);
1515 atomic_dec(&cm_core->ht_node_cnt);
1516 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1517
1518 /* if the node is destroyed before connection was accelerated */
1519 if (!cm_node->accelerated && cm_node->accept_pend) {
1520 BUG_ON(!cm_node->listener);
1521 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1522 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1523 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001524 WARN_ON(cm_node->send_entry);
1525 if (cm_node->recv_entry)
1526 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001527 if (cm_node->listener) {
1528 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1529 } else {
1530 if (cm_node->apbvt_set && cm_node->nesvnic) {
1531 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301532 PCI_FUNC(
1533 cm_node->nesvnic->nesdev->pcidev->devfn),
1534 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001535 }
1536 }
1537
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001538 atomic_dec(&cm_core->node_cnt);
1539 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001540 nesqp = cm_node->nesqp;
1541 if (nesqp) {
1542 nesqp->cm_node = NULL;
1543 nes_rem_ref(&nesqp->ibqp);
1544 cm_node->nesqp = NULL;
1545 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001546
Faisal Latif6492cdf2008-07-24 20:50:45 -07001547 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001548 return 0;
1549}
1550
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001551/**
1552 * process_options
1553 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001554static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301555 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001556{
1557 u32 tmp;
1558 u32 offset = 0;
1559 union all_known_options *all_options;
1560 char got_mss_option = 0;
1561
1562 while (offset < optionsize) {
1563 all_options = (union all_known_options *)(optionsloc + offset);
1564 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001565 case OPTION_NUMBER_END:
1566 offset = optionsize;
1567 break;
1568 case OPTION_NUMBER_NONE:
1569 offset += 1;
1570 continue;
1571 case OPTION_NUMBER_MSS:
1572 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301573 "Size: %d\n", __func__,
1574 all_options->as_mss.length, offset, optionsize);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001575 got_mss_option = 1;
1576 if (all_options->as_mss.length != 4) {
1577 return 1;
1578 } else {
1579 tmp = ntohs(all_options->as_mss.mss);
1580 if (tmp > 0 && tmp <
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301581 cm_node->tcp_cntxt.mss)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001582 cm_node->tcp_cntxt.mss = tmp;
1583 }
1584 break;
1585 case OPTION_NUMBER_WINDOW_SCALE:
1586 cm_node->tcp_cntxt.snd_wscale =
1587 all_options->as_windowscale.shiftcount;
1588 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001589 default:
1590 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301591 all_options->as_base.optionnum);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001592 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001593 }
1594 offset += all_options->as_base.length;
1595 }
1596 if ((!got_mss_option) && (syn_packet))
1597 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1598 return 0;
1599}
1600
Faisal Latif6492cdf2008-07-24 20:50:45 -07001601static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001602{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001603 atomic_inc(&cm_accel_dropped_pkts);
1604 dev_kfree_skb_any(skb);
1605}
1606
Faisal Latif9d5ab132009-03-06 15:15:01 -08001607static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001608{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001609 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301610 "refcnt=%d\n", cm_node, cm_node->state,
1611 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001612 switch (cm_node->state) {
1613 case NES_CM_STATE_SYN_RCVD:
1614 case NES_CM_STATE_SYN_SENT:
1615 case NES_CM_STATE_ESTABLISHED:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001616 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001617 cm_node->tcp_cntxt.rcv_nxt++;
1618 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001619 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001620 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001621 break;
Faisal Latifb1190d32009-12-09 15:54:32 -08001622 case NES_CM_STATE_MPAREQ_SENT:
1623 create_event(cm_node, NES_CM_EVENT_ABORTED);
1624 cm_node->tcp_cntxt.rcv_nxt++;
1625 cleanup_retrans_entry(cm_node);
1626 cm_node->state = NES_CM_STATE_CLOSED;
1627 add_ref_cm_node(cm_node);
1628 send_reset(cm_node, NULL);
1629 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001630 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001631 cm_node->tcp_cntxt.rcv_nxt++;
1632 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001633 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001634 send_ack(cm_node, NULL);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001635 /* Wait for ACK as this is simultaneous close..
Faisal Latif9d5ab132009-03-06 15:15:01 -08001636 * After we receive ACK, do not send anything..
1637 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001638 break;
1639 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001640 cm_node->tcp_cntxt.rcv_nxt++;
1641 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001642 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001643 send_ack(cm_node, NULL);
1644 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1645 break;
1646 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001647 cm_node->tcp_cntxt.rcv_nxt++;
1648 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001649 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001650 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001651 break;
1652 case NES_CM_STATE_TSA:
1653 default:
1654 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1655 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001656 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001657 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001658}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001659
Faisal Latif6492cdf2008-07-24 20:50:45 -07001660
1661static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1662 struct tcphdr *tcph)
1663{
1664
1665 int reset = 0; /* whether to send reset in case of err.. */
1666 atomic_inc(&cm_resets_recvd);
1667 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1668 " refcnt=%d\n", cm_node, cm_node->state,
1669 atomic_read(&cm_node->ref_count));
1670 cleanup_retrans_entry(cm_node);
1671 switch (cm_node->state) {
1672 case NES_CM_STATE_SYN_SENT:
1673 case NES_CM_STATE_MPAREQ_SENT:
1674 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1675 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1676 cm_node->listener, cm_node->state);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301677 switch (cm_node->mpa_frame_rev) {
1678 case IETF_MPA_V2:
1679 cm_node->mpa_frame_rev = IETF_MPA_V1;
1680 /* send a syn and goto syn sent state */
1681 cm_node->state = NES_CM_STATE_SYN_SENT;
1682 if (send_syn(cm_node, 0, NULL)) {
1683 active_open_err(cm_node, skb, reset);
1684 }
1685 break;
1686 case IETF_MPA_V1:
1687 default:
1688 active_open_err(cm_node, skb, reset);
1689 break;
1690 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001691 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001692 case NES_CM_STATE_MPAREQ_RCVD:
Dan Carpenter4d8d6382010-09-15 18:32:39 +02001693 atomic_inc(&cm_node->passive_state);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001694 dev_kfree_skb_any(skb);
1695 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001696 case NES_CM_STATE_ESTABLISHED:
1697 case NES_CM_STATE_SYN_RCVD:
1698 case NES_CM_STATE_LISTENING:
1699 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1700 passive_open_err(cm_node, skb, reset);
1701 break;
1702 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001703 active_open_err(cm_node, skb, reset);
1704 break;
1705 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001706 drop_packet(skb);
1707 break;
Faisal Latifdae58722010-08-14 21:04:56 +00001708 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif68237a02009-06-22 22:53:28 -07001709 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001710 case NES_CM_STATE_LAST_ACK:
1711 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001712 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001713 cm_node->state = NES_CM_STATE_CLOSED;
1714 rem_ref_cm_node(cm_node->cm_core, cm_node);
1715 drop_packet(skb);
1716 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001717 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001718 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001719 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001720 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001721}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001722
Faisal Latif9d5ab132009-03-06 15:15:01 -08001723
1724static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001725{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301726 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001727 int datasize = skb->len;
1728 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001729
1730 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301731 u32 res_type;
1732
Faisal Latif9d5ab132009-03-06 15:15:01 -08001733 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1734 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001735 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001736 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001737 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301738 "cm_node=%p listener=%p state=%d\n", __func__,
1739 __LINE__, cm_node, cm_node->listener,
1740 cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001741 active_open_err(cm_node, skb, 1);
1742 } else {
1743 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001744 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001745 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001746 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001747
1748 switch (cm_node->state) {
1749 case NES_CM_STATE_ESTABLISHED:
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301750 if (res_type == NES_MPA_REQUEST_REJECT)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001751 /*BIG problem as we are receiving the MPA.. So should
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301752 * not be REJECT.. This is Passive Open.. We can
1753 * only receive it Reject for Active Open...*/
Faisal Latif9d5ab132009-03-06 15:15:01 -08001754 WARN_ON(1);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001755 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1756 type = NES_CM_EVENT_MPA_REQ;
1757 atomic_set(&cm_node->passive_state,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301758 NES_PASSIVE_STATE_INDICATED);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001759 break;
1760 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001761 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001762 if (res_type == NES_MPA_REQUEST_REJECT) {
1763 type = NES_CM_EVENT_MPA_REJECT;
1764 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1765 } else {
1766 type = NES_CM_EVENT_CONNECTED;
1767 cm_node->state = NES_CM_STATE_TSA;
1768 }
1769
1770 break;
1771 default:
1772 WARN_ON(1);
1773 break;
1774 }
1775 dev_kfree_skb_any(skb);
1776 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001777}
1778
1779static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1780{
1781 switch (cm_node->state) {
1782 case NES_CM_STATE_SYN_SENT:
1783 case NES_CM_STATE_MPAREQ_SENT:
1784 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301785 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1786 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001787 active_open_err(cm_node, skb, 1);
1788 break;
1789 case NES_CM_STATE_ESTABLISHED:
1790 case NES_CM_STATE_SYN_RCVD:
1791 passive_open_err(cm_node, skb, 1);
1792 break;
1793 case NES_CM_STATE_TSA:
1794 default:
1795 drop_packet(skb);
1796 }
1797}
1798
1799static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301800 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001801{
1802 int err;
1803
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301804 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001805 if (err)
1806 active_open_err(cm_node, skb, 1);
1807
1808 return err;
1809}
1810
1811static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301812 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001813{
1814 int err = 0;
1815 u32 seq;
1816 u32 ack_seq;
1817 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1818 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1819 u32 rcv_wnd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301820
Faisal Latif6492cdf2008-07-24 20:50:45 -07001821 seq = ntohl(tcph->seq);
1822 ack_seq = ntohl(tcph->ack_seq);
1823 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1824 if (ack_seq != loc_seq_num)
1825 err = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301826 else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001827 err = 1;
1828 if (err) {
1829 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301830 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1831 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001832 indicate_pkt_err(cm_node, skb);
1833 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301834 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1835 rcv_wnd);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001836 }
1837 return err;
1838}
1839
1840/*
1841 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1842 * is created with a listener or it may comein as rexmitted packet which in
1843 * that case will be just dropped.
1844 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001845static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301846 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001847{
1848 int ret;
1849 u32 inc_sequence;
1850 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001851
1852 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001853 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001854 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001855
Faisal Latif6492cdf2008-07-24 20:50:45 -07001856 switch (cm_node->state) {
1857 case NES_CM_STATE_SYN_SENT:
1858 case NES_CM_STATE_MPAREQ_SENT:
1859 /* Rcvd syn on active open connection*/
1860 active_open_err(cm_node, skb, 1);
1861 break;
1862 case NES_CM_STATE_LISTENING:
1863 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001864 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301865 cm_node->listener->backlog) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001866 nes_debug(NES_DBG_CM, "drop syn due to backlog "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301867 "pressure \n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07001868 cm_backlog_drops++;
1869 passive_open_err(cm_node, skb, 0);
1870 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001871 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001872 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301873 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001874 if (ret) {
1875 passive_open_err(cm_node, skb, 0);
1876 /* drop pkt */
1877 break;
1878 }
1879 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1880 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001881 cm_node->accept_pend = 1;
1882 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1883
Faisal Latif6492cdf2008-07-24 20:50:45 -07001884 cm_node->state = NES_CM_STATE_SYN_RCVD;
1885 send_syn(cm_node, 1, skb);
1886 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001887 case NES_CM_STATE_CLOSED:
1888 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001889 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001890 send_reset(cm_node, skb);
1891 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001892 case NES_CM_STATE_TSA:
1893 case NES_CM_STATE_ESTABLISHED:
1894 case NES_CM_STATE_FIN_WAIT1:
1895 case NES_CM_STATE_FIN_WAIT2:
1896 case NES_CM_STATE_MPAREQ_RCVD:
1897 case NES_CM_STATE_LAST_ACK:
1898 case NES_CM_STATE_CLOSING:
1899 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001900 default:
1901 drop_packet(skb);
1902 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001903 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001904}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001905
Faisal Latif6492cdf2008-07-24 20:50:45 -07001906static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301907 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001908{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001909 int ret;
1910 u32 inc_sequence;
1911 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001912
Faisal Latif6492cdf2008-07-24 20:50:45 -07001913 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001914 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001915 inc_sequence = ntohl(tcph->seq);
1916 switch (cm_node->state) {
1917 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001918 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001919 /* active open */
1920 if (check_syn(cm_node, tcph, skb))
1921 return;
1922 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1923 /* setup options */
1924 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1925 if (ret) {
1926 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301927 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001928 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001929 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001930 cleanup_retrans_entry(cm_node);
1931 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1932 send_mpa_request(cm_node, skb);
1933 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1934 break;
1935 case NES_CM_STATE_MPAREQ_RCVD:
1936 /* passive open, so should not be here */
1937 passive_open_err(cm_node, skb, 1);
1938 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001939 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001940 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1941 cleanup_retrans_entry(cm_node);
1942 cm_node->state = NES_CM_STATE_CLOSED;
1943 send_reset(cm_node, skb);
1944 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001945 case NES_CM_STATE_CLOSED:
1946 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1947 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001948 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001949 send_reset(cm_node, skb);
1950 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001951 case NES_CM_STATE_ESTABLISHED:
1952 case NES_CM_STATE_FIN_WAIT1:
1953 case NES_CM_STATE_FIN_WAIT2:
1954 case NES_CM_STATE_LAST_ACK:
1955 case NES_CM_STATE_TSA:
1956 case NES_CM_STATE_CLOSING:
1957 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001958 case NES_CM_STATE_MPAREQ_SENT:
1959 default:
1960 drop_packet(skb);
1961 break;
1962 }
1963}
1964
Faisal Latif109d67e2009-04-27 13:41:06 -07001965static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301966 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001967{
1968 int datasize = 0;
1969 u32 inc_sequence;
Faisal Latif109d67e2009-04-27 13:41:06 -07001970 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06001971 int optionsize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301972
Faisal Latifabb77252008-11-21 20:50:52 -06001973 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001974
Faisal Latif6492cdf2008-07-24 20:50:45 -07001975 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07001976 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001977
1978 skb_pull(skb, tcph->doff << 2);
1979 inc_sequence = ntohl(tcph->seq);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001980 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001981 switch (cm_node->state) {
1982 case NES_CM_STATE_SYN_RCVD:
1983 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07001984 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001985 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1986 if (ret)
1987 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001988 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1989 cm_node->state = NES_CM_STATE_ESTABLISHED;
1990 if (datasize) {
1991 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001992 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301993 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001994 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301995 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001996 break;
1997 case NES_CM_STATE_ESTABLISHED:
1998 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001999 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002000 if (datasize) {
2001 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002002 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302003 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002004 drop_packet(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302005 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002006 break;
2007 case NES_CM_STATE_MPAREQ_SENT:
2008 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2009 if (datasize) {
2010 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002011 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302012 } else { /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002013 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302014 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002015 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002016 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002017 cleanup_retrans_entry(cm_node);
2018 cm_node->state = NES_CM_STATE_CLOSED;
2019 send_reset(cm_node, skb);
2020 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002021 case NES_CM_STATE_CLOSED:
2022 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08002023 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002024 send_reset(cm_node, skb);
2025 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002026 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002027 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002028 cleanup_retrans_entry(cm_node);
2029 cm_node->state = NES_CM_STATE_CLOSED;
2030 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002031 rem_ref_cm_node(cm_node->cm_core, cm_node);
2032 drop_packet(skb);
2033 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002034 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002035 cleanup_retrans_entry(cm_node);
2036 drop_packet(skb);
2037 cm_node->state = NES_CM_STATE_FIN_WAIT2;
2038 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002039 case NES_CM_STATE_SYN_SENT:
2040 case NES_CM_STATE_FIN_WAIT2:
2041 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002042 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002043 case NES_CM_STATE_UNKNOWN:
2044 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002045 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002046 drop_packet(skb);
2047 break;
2048 }
Faisal Latif109d67e2009-04-27 13:41:06 -07002049 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002050}
2051
2052
2053
2054static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302055 struct sk_buff *skb, int optionsize, int passive)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002056{
2057 u8 *optionsloc = (u8 *)&tcph[1];
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302058
Faisal Latif6492cdf2008-07-24 20:50:45 -07002059 if (optionsize) {
2060 if (process_options(cm_node, optionsloc, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302061 (u32)tcph->syn)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002062 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302063 __func__, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002064 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06002065 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002066 else
Faisal Latifabb77252008-11-21 20:50:52 -06002067 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002068 return 1;
2069 }
2070 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002071
2072 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302073 cm_node->tcp_cntxt.snd_wscale;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002074
Faisal Latif6492cdf2008-07-24 20:50:45 -07002075 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002076 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002077 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002078}
2079
Faisal Latif6492cdf2008-07-24 20:50:45 -07002080/*
2081 * active_open_err() will send reset() if flag set..
2082 * It will also send ABORT event.
2083 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002084static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302085 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002086{
2087 cleanup_retrans_entry(cm_node);
2088 if (reset) {
2089 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302090 "state=%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002091 add_ref_cm_node(cm_node);
2092 send_reset(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302093 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002094 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302095 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002096
2097 cm_node->state = NES_CM_STATE_CLOSED;
2098 create_event(cm_node, NES_CM_EVENT_ABORTED);
2099}
2100
2101/*
2102 * passive_open_err() will either do a reset() or will free up the skb and
2103 * remove the cm_node.
2104 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002105static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302106 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002107{
2108 cleanup_retrans_entry(cm_node);
2109 cm_node->state = NES_CM_STATE_CLOSED;
2110 if (reset) {
2111 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302112 "cm_node=%p state =%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002113 send_reset(cm_node, skb);
2114 } else {
2115 dev_kfree_skb_any(skb);
2116 rem_ref_cm_node(cm_node->cm_core, cm_node);
2117 }
2118}
2119
2120/*
2121 * free_retrans_entry() routines assumes that the retrans_list_lock has
2122 * been acquired before calling.
2123 */
2124static void free_retrans_entry(struct nes_cm_node *cm_node)
2125{
2126 struct nes_timer_entry *send_entry;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302127
Faisal Latif6492cdf2008-07-24 20:50:45 -07002128 send_entry = cm_node->send_entry;
2129 if (send_entry) {
2130 cm_node->send_entry = NULL;
2131 dev_kfree_skb_any(send_entry->skb);
2132 kfree(send_entry);
2133 rem_ref_cm_node(cm_node->cm_core, cm_node);
2134 }
2135}
2136
2137static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
2138{
2139 unsigned long flags;
2140
2141 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
2142 free_retrans_entry(cm_node);
2143 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
2144}
2145
2146/**
2147 * process_packet
2148 * Returns skb if to be freed, else it will return NULL if already used..
2149 */
2150static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302151 struct nes_cm_core *cm_core)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002152{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302153 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002154 struct tcphdr *tcph = tcp_hdr(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302155 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07002156 int ret = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302157
Faisal Latif6492cdf2008-07-24 20:50:45 -07002158 skb_pull(skb, ip_hdr(skb)->ihl << 2);
2159
2160 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302161 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
2162 tcph->ack, tcph->rst, tcph->fin);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002163
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302164 if (tcph->rst) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002165 pkt_type = NES_PKT_TYPE_RST;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302166 } else if (tcph->syn) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002167 pkt_type = NES_PKT_TYPE_SYN;
2168 if (tcph->ack)
2169 pkt_type = NES_PKT_TYPE_SYNACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302170 } else if (tcph->ack) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002171 pkt_type = NES_PKT_TYPE_ACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302172 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002173 if (tcph->fin)
2174 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002175
2176 switch (pkt_type) {
2177 case NES_PKT_TYPE_SYN:
2178 handle_syn_pkt(cm_node, skb, tcph);
2179 break;
2180 case NES_PKT_TYPE_SYNACK:
2181 handle_synack_pkt(cm_node, skb, tcph);
2182 break;
2183 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002184 ret = handle_ack_pkt(cm_node, skb, tcph);
2185 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08002186 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002187 break;
2188 case NES_PKT_TYPE_RST:
2189 handle_rst_pkt(cm_node, skb, tcph);
2190 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002191 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002192 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08002193 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07002194 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002195 break;
2196 }
2197}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002198
2199/**
2200 * mini_cm_listen - create a listen node with params
2201 */
2202static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302203 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002204{
2205 struct nes_cm_listener *listener;
2206 unsigned long flags;
2207
2208 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302209 cm_info->loc_addr, cm_info->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002210
2211 /* cannot have multiple matching listeners */
2212 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302213 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002214 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
2215 /* find automatically incs ref count ??? */
2216 atomic_dec(&listener->ref_count);
2217 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
2218 return NULL;
2219 }
2220
2221 if (!listener) {
2222 /* create a CM listen node (1/2 node to compare incoming traffic to) */
2223 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
2224 if (!listener) {
2225 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
2226 return NULL;
2227 }
2228
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002229 listener->loc_addr = htonl(cm_info->loc_addr);
2230 listener->loc_port = htons(cm_info->loc_port);
2231 listener->reused_node = 0;
2232
2233 atomic_set(&listener->ref_count, 1);
2234 }
2235 /* pasive case */
2236 /* find already inc'ed the ref count */
2237 else {
2238 listener->reused_node = 1;
2239 }
2240
2241 listener->cm_id = cm_info->cm_id;
2242 atomic_set(&listener->pend_accepts_cnt, 0);
2243 listener->cm_core = cm_core;
2244 listener->nesvnic = nesvnic;
2245 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002246
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002247 listener->conn_type = cm_info->conn_type;
2248 listener->backlog = cm_info->backlog;
2249 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
2250
2251 if (!listener->reused_node) {
2252 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2253 list_add(&listener->list, &cm_core->listen_list.list);
2254 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2255 atomic_inc(&cm_core->listen_node_cnt);
2256 }
2257
2258 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302259 " listener = %p, backlog = %d, cm_id = %p.\n",
2260 cm_info->loc_addr, cm_info->loc_port,
2261 listener, listener->backlog, listener->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002262
2263 return listener;
2264}
2265
2266
2267/**
2268 * mini_cm_connect - make a connection node with params
2269 */
Faisal Latif54c86a82008-09-30 14:47:27 -07002270static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302271 struct nes_vnic *nesvnic, u16 private_data_len,
2272 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002273{
2274 int ret = 0;
2275 struct nes_cm_node *cm_node;
2276 struct nes_cm_listener *loopbackremotelistener;
2277 struct nes_cm_node *loopbackremotenode;
2278 struct nes_cm_info loopback_cm_info;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302279 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002280
2281 /* create a CM connection node */
2282 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2283 if (!cm_node)
2284 return NULL;
2285
Glenn Streiff7495ab62008-04-29 13:46:54 -07002286 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002287 cm_node->tcp_cntxt.client = 1;
2288 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2289
2290 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002291 loopbackremotelistener = find_listener(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302292 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2293 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002294 if (loopbackremotelistener == NULL) {
2295 create_event(cm_node, NES_CM_EVENT_ABORTED);
2296 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002297 loopback_cm_info = *cm_info;
2298 loopback_cm_info.loc_port = cm_info->rem_port;
2299 loopback_cm_info.rem_port = cm_info->loc_port;
2300 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002301 loopbackremotenode = make_cm_node(cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302302 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002303 if (!loopbackremotenode) {
2304 rem_ref_cm_node(cm_node->cm_core, cm_node);
2305 return NULL;
2306 }
2307 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002308 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002309 loopbackremotenode->tcp_cntxt.rcv_wscale =
2310 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002311 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002312 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302313 private_data_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002314 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002315
Faisal Latif6492cdf2008-07-24 20:50:45 -07002316 /* we are done handling this state. */
2317 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002318 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002319 cm_node->tcp_cntxt.rcv_nxt =
2320 loopbackremotenode->tcp_cntxt.loc_seq_num;
2321 loopbackremotenode->tcp_cntxt.rcv_nxt =
2322 cm_node->tcp_cntxt.loc_seq_num;
2323 cm_node->tcp_cntxt.max_snd_wnd =
2324 loopbackremotenode->tcp_cntxt.rcv_wnd;
2325 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2326 cm_node->tcp_cntxt.rcv_wnd;
2327 cm_node->tcp_cntxt.snd_wnd =
2328 loopbackremotenode->tcp_cntxt.rcv_wnd;
2329 loopbackremotenode->tcp_cntxt.snd_wnd =
2330 cm_node->tcp_cntxt.rcv_wnd;
2331 cm_node->tcp_cntxt.snd_wscale =
2332 loopbackremotenode->tcp_cntxt.rcv_wscale;
2333 loopbackremotenode->tcp_cntxt.snd_wscale =
2334 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002335 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002336 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2337 }
2338 return cm_node;
2339 }
2340
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302341 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
2342 cm_node->mpa_frame_size = private_data_len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002343
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302344 memcpy(start_buff, private_data, private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002345
2346 /* send a syn and goto syn sent state */
2347 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002348 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002349
Faisal Latif6492cdf2008-07-24 20:50:45 -07002350 if (ret) {
2351 /* error in sending the syn free up the cm_node struct */
2352 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302353 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2354 cm_node->rem_addr, cm_node->rem_port, cm_node,
2355 cm_node->cm_id);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002356 rem_ref_cm_node(cm_node->cm_core, cm_node);
2357 cm_node = NULL;
2358 }
2359
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302360 if (cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002361 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302362 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2363 cm_node->rem_addr, cm_node->rem_port, cm_node,
2364 cm_node->cm_id);
2365 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002366
2367 return cm_node;
2368}
2369
2370
2371/**
2372 * mini_cm_accept - accept a connection
2373 * This function is never called
2374 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302375static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002376{
2377 return 0;
2378}
2379
2380
2381/**
2382 * mini_cm_reject - reject and teardown a connection
2383 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302384static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002385{
2386 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002387 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002388 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002389 struct nes_cm_event event;
2390 struct iw_cm_id *cm_id = cm_node->cm_id;
2391 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002392
Faisal Latif6492cdf2008-07-24 20:50:45 -07002393 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302394 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002395
Faisal Latif6492cdf2008-07-24 20:50:45 -07002396 if (cm_node->tcp_cntxt.client)
2397 return ret;
2398 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002399
Faisal Latif9d5ab132009-03-06 15:15:01 -08002400 if (!loopback) {
2401 passive_state = atomic_add_return(1, &cm_node->passive_state);
2402 if (passive_state == NES_SEND_RESET_EVENT) {
2403 cm_node->state = NES_CM_STATE_CLOSED;
2404 rem_ref_cm_node(cm_core, cm_node);
2405 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002406 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2407 rem_ref_cm_node(cm_core, cm_node);
2408 } else {
2409 ret = send_mpa_reject(cm_node);
2410 if (ret) {
2411 cm_node->state = NES_CM_STATE_CLOSED;
2412 err = send_reset(cm_node, NULL);
2413 if (err)
2414 WARN_ON(1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302415 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002416 cm_id->add_ref(cm_id);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302417 }
Faisal Latifc5a7d482009-12-09 15:54:08 -08002418 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002419 }
2420 } else {
2421 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002422 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2423 rem_ref_cm_node(cm_core, cm_node);
2424 rem_ref_cm_node(cm_core, loopback);
2425 } else {
2426 event.cm_node = loopback;
2427 event.cm_info.rem_addr = loopback->rem_addr;
2428 event.cm_info.loc_addr = loopback->loc_addr;
2429 event.cm_info.rem_port = loopback->rem_port;
2430 event.cm_info.loc_port = loopback->loc_port;
2431 event.cm_info.cm_id = loopback->cm_id;
2432 cm_event_mpa_reject(&event);
2433 rem_ref_cm_node(cm_core, cm_node);
2434 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002435
Faisal Latifc5a7d482009-12-09 15:54:08 -08002436 cm_id = loopback->cm_id;
2437 rem_ref_cm_node(cm_core, loopback);
2438 cm_id->rem_ref(cm_id);
2439 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002440 }
2441
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002442 return ret;
2443}
2444
2445
2446/**
2447 * mini_cm_close
2448 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002449static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002450{
2451 int ret = 0;
2452
2453 if (!cm_core || !cm_node)
2454 return -EINVAL;
2455
2456 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002457 case NES_CM_STATE_SYN_RCVD:
2458 case NES_CM_STATE_SYN_SENT:
2459 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2460 case NES_CM_STATE_ESTABLISHED:
2461 case NES_CM_STATE_ACCEPTING:
2462 case NES_CM_STATE_MPAREQ_SENT:
2463 case NES_CM_STATE_MPAREQ_RCVD:
2464 cleanup_retrans_entry(cm_node);
2465 send_reset(cm_node, NULL);
2466 break;
2467 case NES_CM_STATE_CLOSE_WAIT:
2468 cm_node->state = NES_CM_STATE_LAST_ACK;
2469 send_fin(cm_node, NULL);
2470 break;
2471 case NES_CM_STATE_FIN_WAIT1:
2472 case NES_CM_STATE_FIN_WAIT2:
2473 case NES_CM_STATE_LAST_ACK:
2474 case NES_CM_STATE_TIME_WAIT:
2475 case NES_CM_STATE_CLOSING:
2476 ret = -1;
2477 break;
2478 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002479 cleanup_retrans_entry(cm_node);
2480 send_reset(cm_node, NULL);
2481 break;
2482 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002483 case NES_CM_STATE_UNKNOWN:
2484 case NES_CM_STATE_INITED:
2485 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002486 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002487 ret = rem_ref_cm_node(cm_core, cm_node);
2488 break;
2489 case NES_CM_STATE_TSA:
2490 if (cm_node->send_entry)
2491 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302492 "send_entry=%p\n", cm_node->send_entry);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002493 ret = rem_ref_cm_node(cm_core, cm_node);
2494 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002495 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002496 return ret;
2497}
2498
2499
2500/**
2501 * recv_pkt - recv an ETHERNET packet, and process it through CM
2502 * node state machine
2503 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002504static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302505 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002506{
2507 struct nes_cm_node *cm_node = NULL;
2508 struct nes_cm_listener *listener = NULL;
2509 struct iphdr *iph;
2510 struct tcphdr *tcph;
2511 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002512 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002513 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002514
Faisal Latif6492cdf2008-07-24 20:50:45 -07002515 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002516 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302517 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002518 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002519
2520 iph = (struct iphdr *)skb->data;
2521 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002522
2523 nfo.loc_addr = ntohl(iph->daddr);
2524 nfo.loc_port = ntohs(tcph->dest);
2525 nfo.rem_addr = ntohl(iph->saddr);
2526 nfo.rem_port = ntohs(tcph->source);
2527
Harvey Harrison03080e52009-01-10 21:45:42 -08002528 tmp_daddr = cpu_to_be32(iph->daddr);
2529 tmp_saddr = cpu_to_be32(iph->saddr);
2530
Harvey Harrison63779432008-10-31 00:56:00 -07002531 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002532 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002533
Faisal Latif6492cdf2008-07-24 20:50:45 -07002534 do {
2535 cm_node = find_node(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302536 nfo.rem_port, nfo.rem_addr,
2537 nfo.loc_port, nfo.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002538
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002539 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002540 /* Only type of packet accepted are for */
2541 /* the PASSIVE open (syn only) */
2542 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002543 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002544 break;
2545 }
2546 listener = find_listener(cm_core, nfo.loc_addr,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302547 nfo.loc_port,
2548 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002549 if (!listener) {
2550 nfo.cm_id = NULL;
2551 nfo.conn_type = 0;
2552 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2553 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002554 break;
2555 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002556 nfo.cm_id = listener->cm_id;
2557 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002558 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302559 listener);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002560 if (!cm_node) {
2561 nes_debug(NES_DBG_CM, "Unable to allocate "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302562 "node\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07002563 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002564 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002565 dev_kfree_skb_any(skb);
2566 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002567 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002568 if (!tcph->rst && !tcph->fin) {
2569 cm_node->state = NES_CM_STATE_LISTENING;
2570 } else {
2571 cm_packets_dropped++;
2572 rem_ref_cm_node(cm_core, cm_node);
2573 dev_kfree_skb_any(skb);
2574 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002575 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002576 add_ref_cm_node(cm_node);
2577 } else if (cm_node->state == NES_CM_STATE_TSA) {
Faisal Latif0f0bee82011-09-25 20:34:00 -05002578 if (cm_node->nesqp->pau_mode)
2579 nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
2580 else {
2581 rem_ref_cm_node(cm_core, cm_node);
2582 atomic_inc(&cm_accel_dropped_pkts);
2583 dev_kfree_skb_any(skb);
2584 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002585 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002586 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002587 skb_reset_network_header(skb);
2588 skb_set_transport_header(skb, sizeof(*tcph));
2589 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002590 process_packet(cm_node, skb, cm_core);
2591 rem_ref_cm_node(cm_core, cm_node);
2592 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002593 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002594}
2595
2596
2597/**
2598 * nes_cm_alloc_core - allocate a top level instance of a cm core
2599 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002600static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002601{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002602 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002603
2604 /* setup the CM core */
2605 /* alloc top level core control structure */
2606 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2607 if (!cm_core)
2608 return NULL;
2609
2610 INIT_LIST_HEAD(&cm_core->connected_nodes);
2611 init_timer(&cm_core->tcp_timer);
2612 cm_core->tcp_timer.function = nes_cm_timer_tick;
2613
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302614 cm_core->mtu = NES_CM_DEFAULT_MTU;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002615 cm_core->state = NES_CM_STATE_INITED;
2616 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2617
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002618 atomic_set(&cm_core->events_posted, 0);
2619
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002620 cm_core->api = &nes_cm_api;
2621
2622 spin_lock_init(&cm_core->ht_lock);
2623 spin_lock_init(&cm_core->listen_list_lock);
2624
2625 INIT_LIST_HEAD(&cm_core->listen_list.list);
2626
2627 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2628
2629 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2630 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2631 cm_core->post_event = nes_cm_post_event;
2632 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2633 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2634
2635 print_core(cm_core);
2636 return cm_core;
2637}
2638
2639
2640/**
2641 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2642 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002643static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002644{
2645 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2646
2647 if (!cm_core)
2648 return -EINVAL;
2649
2650 barrier();
2651
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302652 if (timer_pending(&cm_core->tcp_timer))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002653 del_timer(&cm_core->tcp_timer);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002654
2655 destroy_workqueue(cm_core->event_wq);
2656 destroy_workqueue(cm_core->disconn_wq);
2657 nes_debug(NES_DBG_CM, "\n");
2658 kfree(cm_core);
2659
2660 return 0;
2661}
2662
2663
2664/**
2665 * mini_cm_get
2666 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002667static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002668{
2669 return cm_core->state;
2670}
2671
2672
2673/**
2674 * mini_cm_set
2675 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002676static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002677{
2678 int ret = 0;
2679
2680 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002681 case NES_CM_SET_PKT_SIZE:
2682 cm_core->mtu = value;
2683 break;
2684 case NES_CM_SET_FREE_PKT_Q_SIZE:
2685 cm_core->free_tx_pkt_max = value;
2686 break;
2687 default:
2688 /* unknown set option */
2689 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002690 }
2691
2692 return ret;
2693}
2694
2695
2696/**
2697 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2698 * successfully exchanged when this is called
2699 */
2700static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2701{
2702 int ret = 0;
2703
2704 if (!nesqp)
2705 return -EINVAL;
2706
2707 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302708 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2709 NES_QPCONTEXT_MISC_DROS);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002710
2711 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2712 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2713
2714 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2715
2716 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2717
2718 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302719 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002720
2721 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302722 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2723 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002724
2725 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302726 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2727 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002728
2729 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2730 nesqp->nesqp_context->ts_recent = 0;
2731 nesqp->nesqp_context->ts_age = 0;
2732 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2733 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2734 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2735 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302736 cm_node->tcp_cntxt.rcv_wscale);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002737 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2738 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2739 nesqp->nesqp_context->srtt = 0;
2740 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2741 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302742 nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002743 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2744 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2745 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2746
2747 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302748 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2749 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2750 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2751 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2752 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2753 le32_to_cpu(nesqp->nesqp_context->misc));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002754 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2755 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2756 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2757
2758 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2759 cm_node->state = NES_CM_STATE_TSA;
2760
2761 return ret;
2762}
2763
2764
2765/**
2766 * nes_cm_disconn
2767 */
2768int nes_cm_disconn(struct nes_qp *nesqp)
2769{
Don Wood873fcdd2009-09-05 20:36:37 -07002770 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002771
Don Wood873fcdd2009-09-05 20:36:37 -07002772 work = kzalloc(sizeof *work, GFP_ATOMIC);
2773 if (!work)
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302774 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002775
Don Wood873fcdd2009-09-05 20:36:37 -07002776 nes_add_ref(&nesqp->ibqp);
2777 work->nesqp = nesqp;
2778 INIT_WORK(&work->work, nes_disconnect_worker);
2779 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002780 return 0;
2781}
2782
2783
2784/**
2785 * nes_disconnect_worker
2786 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002787static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002788{
Don Wood873fcdd2009-09-05 20:36:37 -07002789 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2790 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002791
Don Wood873fcdd2009-09-05 20:36:37 -07002792 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002793 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302794 nesqp->last_aeq, nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002795 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002796 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002797}
2798
2799
2800/**
2801 * nes_cm_disconn_true
2802 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002803static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002804{
2805 unsigned long flags;
2806 int ret = 0;
2807 struct iw_cm_id *cm_id;
2808 struct iw_cm_event cm_event;
2809 struct nes_vnic *nesvnic;
2810 u16 last_ae;
2811 u8 original_hw_tcp_state;
2812 u8 original_ibqp_state;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002813 int disconn_status = 0;
Don Woodb29a4fc2009-09-05 20:36:39 -07002814 int issue_disconn = 0;
2815 int issue_close = 0;
2816 int issue_flush = 0;
2817 u32 flush_q = NES_CQP_FLUSH_RQ;
2818 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002819
2820 if (!nesqp) {
2821 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2822 return -1;
2823 }
2824
2825 spin_lock_irqsave(&nesqp->lock, flags);
2826 cm_id = nesqp->cm_id;
2827 /* make sure we havent already closed this connection */
2828 if (!cm_id) {
2829 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302830 nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002831 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002832 return -1;
2833 }
2834
2835 nesvnic = to_nesvnic(nesqp->ibqp.device);
2836 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2837
2838 original_hw_tcp_state = nesqp->hw_tcp_state;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302839 original_ibqp_state = nesqp->ibqp_state;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002840 last_ae = nesqp->last_aeq;
2841
Don Woodb29a4fc2009-09-05 20:36:39 -07002842 if (nesqp->term_flags) {
2843 issue_disconn = 1;
2844 issue_close = 1;
2845 nesqp->cm_id = NULL;
Tatyana Nikolova196f40c2011-12-05 11:39:12 -06002846 del_timer(&nesqp->terminate_timer);
Don Woodb29a4fc2009-09-05 20:36:39 -07002847 if (nesqp->flush_issued == 0) {
2848 nesqp->flush_issued = 1;
2849 issue_flush = 1;
2850 }
2851 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2852 ((original_ibqp_state == IB_QPS_RTS) &&
2853 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2854 issue_disconn = 1;
2855 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002856 disconn_status = -ECONNRESET;
Don Woodb29a4fc2009-09-05 20:36:39 -07002857 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002858
Don Woodb29a4fc2009-09-05 20:36:39 -07002859 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2860 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2861 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2862 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2863 issue_close = 1;
2864 nesqp->cm_id = NULL;
2865 if (nesqp->flush_issued == 0) {
2866 nesqp->flush_issued = 1;
2867 issue_flush = 1;
2868 }
2869 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002870
Don Woodb29a4fc2009-09-05 20:36:39 -07002871 spin_unlock_irqrestore(&nesqp->lock, flags);
2872
2873 if ((issue_flush) && (nesqp->destroyed == 0)) {
2874 /* Flush the queue(s) */
2875 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2876 flush_q |= NES_CQP_FLUSH_SQ;
2877 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2878
2879 if (nesqp->term_flags) {
2880 ibevent.device = nesqp->ibqp.device;
2881 ibevent.event = nesqp->terminate_eventtype;
2882 ibevent.element.qp = &nesqp->ibqp;
2883 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
2884 }
2885 }
2886
2887 if ((cm_id) && (cm_id->event_handler)) {
2888 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002889 atomic_inc(&cm_disconnects);
2890 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002891 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002892 cm_event.local_addr = cm_id->local_addr;
2893 cm_event.remote_addr = cm_id->remote_addr;
2894 cm_event.private_data = NULL;
2895 cm_event.private_data_len = 0;
2896
Faisal Latif6492cdf2008-07-24 20:50:45 -07002897 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302898 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2899 "cm_id = %p, refcount = %u.\n",
2900 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2901 nesqp->hwqp.sq_tail, cm_id,
2902 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002903
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002904 ret = cm_id->event_handler(cm_id, &cm_event);
2905 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002906 nes_debug(NES_DBG_CM, "OFA CM event_handler "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302907 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002908 }
2909
Don Woodb29a4fc2009-09-05 20:36:39 -07002910 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002911 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002912 nes_disconnect(nesqp, 1);
2913
2914 cm_id->provider_data = nesqp;
2915 /* Send up the close complete event */
2916 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002917 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002918 cm_event.provider_data = cm_id->provider_data;
2919 cm_event.local_addr = cm_id->local_addr;
2920 cm_event.remote_addr = cm_id->remote_addr;
2921 cm_event.private_data = NULL;
2922 cm_event.private_data_len = 0;
2923
2924 ret = cm_id->event_handler(cm_id, &cm_event);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302925 if (ret)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002926 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002927
2928 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002929 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002930 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002931
2932 return 0;
2933}
2934
2935
2936/**
2937 * nes_disconnect
2938 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002939static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002940{
2941 int ret = 0;
2942 struct nes_vnic *nesvnic;
2943 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002944 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002945
2946 nesvnic = to_nesvnic(nesqp->ibqp.device);
2947 if (!nesvnic)
2948 return -EINVAL;
2949
2950 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002951 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002952
2953 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00002954 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002955
2956 if (nesqp->active_conn) {
2957
2958 /* indicate this connection is NOT active */
2959 nesqp->active_conn = 0;
2960 } else {
2961 /* Need to free the Last Streaming Mode Message */
2962 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002963 if (nesqp->lsmm_mr)
2964 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002965 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302966 nesqp->private_data_len + nesqp->ietf_frame_size,
2967 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002968 }
2969 }
2970
2971 /* close the CM node down if it is still active */
2972 if (nesqp->cm_node) {
2973 nes_debug(NES_DBG_CM, "Call close API\n");
2974
2975 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002976 }
2977
2978 return ret;
2979}
2980
2981
2982/**
2983 * nes_accept
2984 */
2985int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2986{
2987 u64 u64temp;
2988 struct ib_qp *ibqp;
2989 struct nes_qp *nesqp;
2990 struct nes_vnic *nesvnic;
2991 struct nes_device *nesdev;
2992 struct nes_cm_node *cm_node;
2993 struct nes_adapter *adapter;
2994 struct ib_qp_attr attr;
2995 struct iw_cm_event cm_event;
2996 struct nes_hw_qp_wqe *wqe;
2997 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002998 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002999 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003000 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003001 struct nes_ib_device *nesibdev;
3002 struct ib_mr *ibmr = NULL;
3003 struct ib_phys_buf ibphysbuf;
3004 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07003005 u64 tagged_offset;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303006 u8 mpa_frame_offset = 0;
3007 struct ietf_mpa_v2 *mpa_v2_frame;
3008 u8 start_addr = 0;
3009 u8 *start_ptr = &start_addr;
3010 u8 **start_buff = &start_ptr;
3011 u16 buff_len = 0;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003012
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003013 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3014 if (!ibqp)
3015 return -EINVAL;
3016
3017 /* get all our handles */
3018 nesqp = to_nesqp(ibqp);
3019 nesvnic = to_nesvnic(nesqp->ibqp.device);
3020 nesdev = nesvnic->nesdev;
3021 adapter = nesdev->nesadapter;
3022
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003023 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003024 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
3025 "%s\n", cm_node, nesvnic, nesvnic->netdev,
3026 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003027
Faisal Latifc5a7d482009-12-09 15:54:08 -08003028 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
3029 if (cm_node->loopbackpartner)
3030 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
3031 rem_ref_cm_node(cm_node->cm_core, cm_node);
3032 return -EINVAL;
3033 }
3034
Faisal Latifdae58722010-08-14 21:04:56 +00003035 passive_state = atomic_add_return(1, &cm_node->passive_state);
3036 if (passive_state == NES_SEND_RESET_EVENT) {
3037 rem_ref_cm_node(cm_node->cm_core, cm_node);
3038 return -ECONNRESET;
3039 }
3040
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003041 /* associate the node with the QP */
3042 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003043 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003044
Faisal Latif6492cdf2008-07-24 20:50:45 -07003045 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
3046 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003047 atomic_inc(&cm_accepts);
3048
3049 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00003050 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003051
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303052 nesqp->ietf_frame_size = sizeof(struct ietf_mpa_v2);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003053 /* allocate the ietf frame and space for private data */
3054 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303055 nesqp->ietf_frame_size + conn_param->private_data_len,
3056 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003057
Faisal Latif6492cdf2008-07-24 20:50:45 -07003058 if (!nesqp->ietf_frame) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303059 nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07003060 return -ENOMEM;
3061 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303062 mpa_v2_frame = (struct ietf_mpa_v2 *)nesqp->ietf_frame;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003063
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303064 if (cm_node->mpa_frame_rev == IETF_MPA_V1)
3065 mpa_frame_offset = 4;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003066
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303067 memcpy(mpa_v2_frame->priv_data, conn_param->private_data,
3068 conn_param->private_data_len);
3069
3070 cm_build_mpa_frame(cm_node, start_buff, &buff_len, nesqp->ietf_frame, MPA_KEY_REPLY);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003071 nesqp->private_data_len = conn_param->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003072
Faisal Latif6492cdf2008-07-24 20:50:45 -07003073 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
3074 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003075
Faisal Latif6492cdf2008-07-24 20:50:45 -07003076 if (cm_id->remote_addr.sin_addr.s_addr !=
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303077 cm_id->local_addr.sin_addr.s_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003078 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003079 nesibdev = nesvnic->nesibdev;
3080 nespd = nesqp->nespd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303081 ibphysbuf.addr = nesqp->ietf_frame_pbase + mpa_frame_offset;
3082 ibphysbuf.size = buff_len;
3083 tagged_offset = (u64)(unsigned long)*start_buff;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003084 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303085 &ibphysbuf, 1,
3086 IB_ACCESS_LOCAL_WRITE,
3087 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003088 if (!ibmr) {
3089 nes_debug(NES_DBG_CM, "Unable to register memory region"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303090 "for lSMM for cm_node = %p \n",
3091 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07003092 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303093 nesqp->private_data_len + nesqp->ietf_frame_size,
3094 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003095 return -ENOMEM;
3096 }
3097
3098 ibmr->pd = &nespd->ibpd;
3099 ibmr->device = nespd->ibpd.device;
3100 nesqp->lsmm_mr = ibmr;
3101
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303102 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003103 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303104 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
3105 u64temp);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003106 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3107 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303108 NES_IWARP_SQ_WQE_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003109 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303110 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003111 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303112 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3113 (u64)(unsigned long)(*start_buff));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003114 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303115 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003116 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08003117 if (nesqp->sq_kmapped) {
3118 nesqp->sq_kmapped = 0;
3119 kunmap(nesqp->page);
3120 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003121
Faisal Latif6492cdf2008-07-24 20:50:45 -07003122 nesqp->nesqp_context->ird_ord_sizes |=
3123 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303124 NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003125 } else {
3126 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08003127 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003128 }
3129 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003130
3131
3132 /* Cache the cm_id in the qp */
3133 nesqp->cm_id = cm_id;
3134 cm_node->cm_id = cm_id;
3135
3136 /* nesqp->cm_node = (void *)cm_id->provider_data; */
3137 cm_id->provider_data = nesqp;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303138 nesqp->active_conn = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003139
Faisal Latif6492cdf2008-07-24 20:50:45 -07003140 if (cm_node->state == NES_CM_STATE_TSA)
3141 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303142 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003143
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003144 nes_cm_init_tsa_conn(nesqp, cm_node);
3145
Faisal Latif6492cdf2008-07-24 20:50:45 -07003146 nesqp->nesqp_context->tcpPorts[0] =
3147 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3148 nesqp->nesqp_context->tcpPorts[1] =
3149 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3150
3151 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3152 nesqp->nesqp_context->ip0 =
3153 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3154 else
3155 nesqp->nesqp_context->ip0 =
3156 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003157
3158 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303159 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3160 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003161
Faisal Latif6492cdf2008-07-24 20:50:45 -07003162 nesqp->nesqp_context->arp_index_vlan |=
3163 cpu_to_le32(nes_arp_table(nesdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303164 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
3165 NES_ARP_RESOLVE) << 16);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003166
3167 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003168 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003169
3170 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3171
3172 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003173 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
3174 nesqp->nesqp_context->ird_ord_sizes |=
3175 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003176
3177 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003178 nes_quad.DstIpAdrIndex =
3179 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3180 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3181 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3182 else
3183 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
3184 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3185 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003186
3187 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003188 crc_value = get_crc_value(&nes_quad);
3189 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003190 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303191 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003192
3193 nesqp->hte_index &= adapter->hte_index_mask;
3194 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3195
3196 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3197
Faisal Latif6492cdf2008-07-24 20:50:45 -07003198 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303199 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
3200 "private data length=%u.\n", nesqp->hwqp.qp_id,
3201 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3202 ntohs(cm_id->remote_addr.sin_port),
3203 ntohl(cm_id->local_addr.sin_addr.s_addr),
3204 ntohs(cm_id->local_addr.sin_port),
3205 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
3206 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
3207 buff_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003208
Coly Li73ac36e2009-01-07 18:09:16 -08003209 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003210 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07003211 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003212
3213 cm_event.event = IW_CM_EVENT_ESTABLISHED;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003214 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003215 cm_event.provider_data = (void *)nesqp;
3216 cm_event.local_addr = cm_id->local_addr;
3217 cm_event.remote_addr = cm_id->remote_addr;
3218 cm_event.private_data = NULL;
3219 cm_event.private_data_len = 0;
3220 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003221 attr.qp_state = IB_QPS_RTS;
3222 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003223 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003224 cm_node->loopbackpartner->mpa_frame_size =
3225 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003226 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003227 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303228 conn_param->private_data, conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003229 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
3230 }
3231 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003232 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303233 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003234
3235 return 0;
3236}
3237
3238
3239/**
3240 * nes_reject
3241 */
3242int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3243{
3244 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003245 struct nes_cm_node *loopback;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003246 struct nes_cm_core *cm_core;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303247 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003248
3249 atomic_inc(&cm_rejects);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303250 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003251 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003252 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003253 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003254
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303255 if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003256 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003257
Faisal Latif9d5ab132009-03-06 15:15:01 -08003258 if (loopback) {
3259 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3260 loopback->mpa_frame.priv_data_len = pdata_len;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303261 loopback->mpa_frame_size = pdata_len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003262 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303263 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
3264 cm_node->mpa_frame_size = pdata_len;
3265 memcpy(start_buff, pdata, pdata_len);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003266 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303267 return cm_core->api->reject(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003268}
3269
3270
3271/**
3272 * nes_connect
3273 * setup and launch cm connect node
3274 */
3275int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3276{
3277 struct ib_qp *ibqp;
3278 struct nes_qp *nesqp;
3279 struct nes_vnic *nesvnic;
3280 struct nes_device *nesdev;
3281 struct nes_cm_node *cm_node;
3282 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003283 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003284
3285 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3286 if (!ibqp)
3287 return -EINVAL;
3288 nesqp = to_nesqp(ibqp);
3289 if (!nesqp)
3290 return -EINVAL;
3291 nesvnic = to_nesvnic(nesqp->ibqp.device);
3292 if (!nesvnic)
3293 return -EINVAL;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303294 nesdev = nesvnic->nesdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003295 if (!nesdev)
3296 return -EINVAL;
3297
Faisal Latifc5a7d482009-12-09 15:54:08 -08003298 if (!(cm_id->local_addr.sin_port) || !(cm_id->remote_addr.sin_port))
3299 return -EINVAL;
3300
Faisal Latif6492cdf2008-07-24 20:50:45 -07003301 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303302 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3303 ntohl(nesvnic->local_ipaddr),
3304 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3305 ntohs(cm_id->remote_addr.sin_port),
3306 ntohl(cm_id->local_addr.sin_addr.s_addr),
3307 ntohs(cm_id->local_addr.sin_port));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003308
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003309 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003310 nesqp->active_conn = 1;
3311
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003312 /* cache the cm_id in the qp */
3313 nesqp->cm_id = cm_id;
3314
3315 cm_id->provider_data = nesqp;
3316
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003317 nesqp->private_data_len = conn_param->private_data_len;
3318 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
3319 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));
3532 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3533 nesqp->nesqp_context->ip0 =
3534 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3535 else
3536 nesqp->nesqp_context->ip0 =
3537 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003538
3539 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003540 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3541 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003542 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003543 nes_arp_table(nesdev,
3544 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003545 NULL, NES_ARP_RESOLVE) << 16);
3546 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3547 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3548 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3549 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003550 cpu_to_le32((u32)1 <<
3551 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003552
3553 /* Adjust tail for not having a LSMM */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303554 /*nesqp->hwqp.sq_tail = 1;*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003555
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303556 build_rdma0_msg(cm_node, &nesqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003557
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303558 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3559 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003560
3561 memset(&nes_quad, 0, sizeof(nes_quad));
3562
Faisal Latif6492cdf2008-07-24 20:50:45 -07003563 nes_quad.DstIpAdrIndex =
3564 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3565 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3566 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3567 else
3568 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003569 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3570 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3571
3572 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003573 crc_value = get_crc_value(&nes_quad);
3574 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003575 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303576 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003577
3578 nesqp->hte_index &= nesadapter->hte_index_mask;
3579 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3580
3581 nesqp->ietf_frame = &cm_node->mpa_frame;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303582 nesqp->private_data_len = (u8)cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003583 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3584
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003585 /* notify OF layer we successfully created the requested connection */
3586 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003587 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003588 cm_event.provider_data = cm_id->provider_data;
3589 cm_event.local_addr.sin_family = AF_INET;
3590 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3591 cm_event.remote_addr = cm_id->remote_addr;
3592
Faisal Latif6492cdf2008-07-24 20:50:45 -07003593 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303594 cm_event.private_data_len = (u8)event->cm_node->mpa_frame_size;
3595 cm_event.ird = cm_node->ird_size;
3596 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003597
3598 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3599 ret = cm_id->event_handler(cm_id, &cm_event);
3600 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3601
3602 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003603 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303604 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003605 attr.qp_state = IB_QPS_RTS;
3606 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003607
Faisal Latif6492cdf2008-07-24 20:50:45 -07003608 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303609 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003610
3611 return;
3612}
3613
3614
3615/**
3616 * cm_event_connect_error
3617 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003618static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003619{
3620 struct nes_qp *nesqp;
3621 struct iw_cm_id *cm_id;
3622 struct iw_cm_event cm_event;
3623 /* struct nes_cm_info cm_info; */
3624 int ret;
3625
3626 if (!event->cm_node)
3627 return;
3628
3629 cm_id = event->cm_node->cm_id;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303630 if (!cm_id)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003631 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003632
3633 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3634 nesqp = cm_id->provider_data;
3635
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303636 if (!nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003637 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003638
3639 /* notify OF layer about this connection error event */
3640 /* cm_id->rem_ref(cm_id); */
3641 nesqp->cm_id = NULL;
3642 cm_id->provider_data = NULL;
3643 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003644 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003645 cm_event.provider_data = cm_id->provider_data;
3646 cm_event.local_addr = cm_id->local_addr;
3647 cm_event.remote_addr = cm_id->remote_addr;
3648 cm_event.private_data = NULL;
3649 cm_event.private_data_len = 0;
3650
Faisal Latif6492cdf2008-07-24 20:50:45 -07003651 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303652 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3653 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003654
3655 ret = cm_id->event_handler(cm_id, &cm_event);
3656 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3657 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003658 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303659 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003660 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003661
Faisal Latif6492cdf2008-07-24 20:50:45 -07003662 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003663 return;
3664}
3665
3666
3667/**
3668 * cm_event_reset
3669 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003670static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003671{
3672 struct nes_qp *nesqp;
3673 struct iw_cm_id *cm_id;
3674 struct iw_cm_event cm_event;
3675 /* struct nes_cm_info cm_info; */
3676 int ret;
3677
3678 if (!event->cm_node)
3679 return;
3680
3681 if (!event->cm_node->cm_id)
3682 return;
3683
3684 cm_id = event->cm_node->cm_id;
3685
3686 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3687 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003688 if (!nesqp)
3689 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003690
3691 nesqp->cm_id = NULL;
3692 /* cm_id->provider_data = NULL; */
3693 cm_event.event = IW_CM_EVENT_DISCONNECT;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003694 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003695 cm_event.provider_data = cm_id->provider_data;
3696 cm_event.local_addr = cm_id->local_addr;
3697 cm_event.remote_addr = cm_id->remote_addr;
3698 cm_event.private_data = NULL;
3699 cm_event.private_data_len = 0;
3700
Faisal Latif183ecfa2008-11-21 20:50:46 -06003701 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003702 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003703 atomic_inc(&cm_closes);
3704 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003705 cm_event.status = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003706 cm_event.provider_data = cm_id->provider_data;
3707 cm_event.local_addr = cm_id->local_addr;
3708 cm_event.remote_addr = cm_id->remote_addr;
3709 cm_event.private_data = NULL;
3710 cm_event.private_data_len = 0;
3711 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3712 ret = cm_id->event_handler(cm_id, &cm_event);
3713
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003714 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3715
3716
3717 /* notify OF layer about this connection error event */
3718 cm_id->rem_ref(cm_id);
3719
3720 return;
3721}
3722
3723
3724/**
3725 * cm_event_mpa_req
3726 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003727static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003728{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303729 struct iw_cm_id *cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003730 struct iw_cm_event cm_event;
3731 int ret;
3732 struct nes_cm_node *cm_node;
3733
3734 cm_node = event->cm_node;
3735 if (!cm_node)
3736 return;
3737 cm_id = cm_node->cm_id;
3738
3739 atomic_inc(&cm_connect_reqs);
3740 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303741 cm_node, cm_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003742
3743 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003744 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003745 cm_event.provider_data = (void *)cm_node;
3746
3747 cm_event.local_addr.sin_family = AF_INET;
3748 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3749 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3750
3751 cm_event.remote_addr.sin_family = AF_INET;
3752 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3753 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003754 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303755 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
3756 cm_event.ird = cm_node->ird_size;
3757 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003758
3759 ret = cm_id->event_handler(cm_id, &cm_event);
3760 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003761 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303762 __func__, __LINE__, ret);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003763 return;
3764}
3765
3766
3767static void cm_event_mpa_reject(struct nes_cm_event *event)
3768{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303769 struct iw_cm_id *cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003770 struct iw_cm_event cm_event;
3771 struct nes_cm_node *cm_node;
3772 int ret;
3773
3774 cm_node = event->cm_node;
3775 if (!cm_node)
3776 return;
3777 cm_id = cm_node->cm_id;
3778
3779 atomic_inc(&cm_connect_reqs);
3780 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303781 cm_node, cm_id, jiffies);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003782
3783 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3784 cm_event.status = -ECONNREFUSED;
3785 cm_event.provider_data = cm_id->provider_data;
3786
3787 cm_event.local_addr.sin_family = AF_INET;
3788 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3789 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3790
3791 cm_event.remote_addr.sin_family = AF_INET;
3792 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3793 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3794
3795 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303796 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003797
3798 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303799 "remove_addr=%08x\n",
3800 cm_event.local_addr.sin_addr.s_addr,
3801 cm_event.remote_addr.sin_addr.s_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003802
3803 ret = cm_id->event_handler(cm_id, &cm_event);
3804 if (ret)
3805 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303806 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003807
3808 return;
3809}
3810
3811
3812static void nes_cm_event_handler(struct work_struct *);
3813
3814/**
3815 * nes_cm_post_event
3816 * post an event to the cm event handler
3817 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003818static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003819{
3820 atomic_inc(&event->cm_node->cm_core->events_posted);
3821 add_ref_cm_node(event->cm_node);
3822 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3823 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003824 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303825 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003826
3827 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3828
3829 nes_debug(NES_DBG_CM, "Exit\n");
3830 return 0;
3831}
3832
3833
3834/**
3835 * nes_cm_event_handler
3836 * worker function to handle cm events
3837 * will free instance of nes_cm_event
3838 */
3839static void nes_cm_event_handler(struct work_struct *work)
3840{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003841 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303842 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003843 struct nes_cm_core *cm_core;
3844
Faisal Latif6492cdf2008-07-24 20:50:45 -07003845 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003846 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003847
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003848 cm_core = event->cm_node->cm_core;
3849 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303850 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003851
3852 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003853 case NES_CM_EVENT_MPA_REQ:
3854 cm_event_mpa_req(event);
3855 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303856 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003857 break;
3858 case NES_CM_EVENT_RESET:
3859 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303860 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003861 cm_event_reset(event);
3862 break;
3863 case NES_CM_EVENT_CONNECTED:
3864 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303865 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003866 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003867 cm_event_connected(event);
3868 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3869 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003870 case NES_CM_EVENT_MPA_REJECT:
3871 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303872 (event->cm_node->state == NES_CM_STATE_TSA))
Faisal Latif9d5ab132009-03-06 15:15:01 -08003873 break;
3874 cm_event_mpa_reject(event);
3875 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3876 break;
3877
Faisal Latif6492cdf2008-07-24 20:50:45 -07003878 case NES_CM_EVENT_ABORTED:
3879 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303880 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003881 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003882 cm_event_connect_error(event);
3883 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3884 break;
3885 case NES_CM_EVENT_DROPPED_PKT:
3886 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3887 break;
3888 default:
3889 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3890 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003891 }
3892
3893 atomic_dec(&cm_core->events_posted);
3894 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3895 rem_ref_cm_node(cm_core, event->cm_node);
3896 kfree(event);
3897
3898 return;
3899}