blob: 9c9f2f57e960e1803b831aaf1a94b87011822844 [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 Dreier1a855fbf2008-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 Dreier1a855fbf2008-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 Dreier1a855fbf2008-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 Dreier1a855fbf2008-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 Dreier1a855fbf2008-04-16 21:01:09 -0700149static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800150
151atomic_t cm_connects;
152atomic_t cm_accepts;
153atomic_t cm_disconnects;
154atomic_t cm_closes;
155atomic_t cm_connecteds;
156atomic_t cm_connect_reqs;
157atomic_t cm_rejects;
158
Faisal Latif0f0bee82011-09-25 20:34:00 -0500159int nes_add_ref_cm_node(struct nes_cm_node *cm_node)
160{
161 return add_ref_cm_node(cm_node);
162}
163
164int nes_rem_ref_cm_node(struct nes_cm_node *cm_node)
165{
166 return rem_ref_cm_node(cm_node->cm_core, cm_node);
167}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800168
169/**
170 * create_event
171 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530172static struct nes_cm_event *create_event(struct nes_cm_node * cm_node,
173 enum nes_cm_event_type type)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800174{
175 struct nes_cm_event *event;
176
177 if (!cm_node->cm_id)
178 return NULL;
179
180 /* allocate an empty event */
181 event = kzalloc(sizeof(*event), GFP_ATOMIC);
182
183 if (!event)
184 return NULL;
185
186 event->type = type;
187 event->cm_node = cm_node;
188 event->cm_info.rem_addr = cm_node->rem_addr;
189 event->cm_info.loc_addr = cm_node->loc_addr;
190 event->cm_info.rem_port = cm_node->rem_port;
191 event->cm_info.loc_port = cm_node->loc_port;
192 event->cm_info.cm_id = cm_node->cm_id;
193
Faisal Latif6492cdf2008-07-24 20:50:45 -0700194 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530195 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
196 cm_node, event, type, event->cm_info.loc_addr,
197 event->cm_info.loc_port, event->cm_info.rem_addr,
198 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800199
200 nes_cm_post_event(event);
201 return event;
202}
203
204
205/**
206 * send_mpa_request
207 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700208static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800209{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530210 u8 start_addr = 0;
211 u8 *start_ptr = &start_addr;
212 u8 **start_buff = &start_ptr;
213 u16 buff_len = 0;
214
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800215 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700216 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800217 return -1;
218 }
219
220 /* send an MPA Request frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530221 cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REQUEST);
222 form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800223
Faisal Latif9d5ab132009-03-06 15:15:01 -0800224 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
225}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800226
Faisal Latif9d5ab132009-03-06 15:15:01 -0800227
228
229static int send_mpa_reject(struct nes_cm_node *cm_node)
230{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530231 struct sk_buff *skb = NULL;
232 u8 start_addr = 0;
233 u8 *start_ptr = &start_addr;
234 u8 **start_buff = &start_ptr;
235 u16 buff_len = 0;
Tatyana Nikolova81f99dc2012-01-17 10:17:30 -0600236 struct ietf_mpa_v1 *mpa_frame;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800237
238 skb = dev_alloc_skb(MAX_CM_BUFFER);
239 if (!skb) {
240 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
241 return -ENOMEM;
242 }
243
244 /* send an MPA reject frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530245 cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REPLY);
Tatyana Nikolova81f99dc2012-01-17 10:17:30 -0600246 mpa_frame = (struct ietf_mpa_v1 *)*start_buff;
247 mpa_frame->flags |= IETF_MPA_FLAGS_REJECT;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530248 form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK | SET_FIN);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800249
250 cm_node->state = NES_CM_STATE_FIN_WAIT1;
251 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800252}
253
254
255/**
256 * recv_mpa - process a received TCP pkt, we are expecting an
257 * IETF MPA frame
258 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800259static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530260 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800261{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530262 struct ietf_mpa_v1 *mpa_frame;
263 struct ietf_mpa_v2 *mpa_v2_frame;
264 struct ietf_rtr_msg *rtr_msg;
265 int mpa_hdr_len;
266 int priv_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800267
Faisal Latif9d5ab132009-03-06 15:15:01 -0800268 *type = NES_MPA_REQUEST_ACCEPT;
269
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800270 /* assume req frame is in tcp data payload */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530271 if (len < sizeof(struct ietf_mpa_v1)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800272 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800273 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800274 }
275
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530276 /* points to the beginning of the frame, which could be MPA V1 or V2 */
277 mpa_frame = (struct ietf_mpa_v1 *)buffer;
278 mpa_hdr_len = sizeof(struct ietf_mpa_v1);
279 priv_data_len = ntohs(mpa_frame->priv_data_len);
280
Faisal Latif1cf078c2009-12-09 15:53:54 -0800281 /* make sure mpa private data len is less than 512 bytes */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530282 if (priv_data_len > IETF_MAX_PRIV_DATA_LEN) {
Faisal Latif1cf078c2009-12-09 15:53:54 -0800283 nes_debug(NES_DBG_CM, "The received Length of Private"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530284 " Data field exceeds 512 octets\n");
Faisal Latif1cf078c2009-12-09 15:53:54 -0800285 return -EINVAL;
286 }
287 /*
288 * make sure MPA receiver interoperate with the
289 * received MPA version and MPA key information
290 *
291 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530292 if (mpa_frame->rev != IETF_MPA_V1 && mpa_frame->rev != IETF_MPA_V2) {
Faisal Latif1cf078c2009-12-09 15:53:54 -0800293 nes_debug(NES_DBG_CM, "The received mpa version"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530294 " is not supported\n");
Faisal Latif1cf078c2009-12-09 15:53:54 -0800295 return -EINVAL;
296 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530297 /*
298 * backwards compatibility only
299 */
300 if (mpa_frame->rev > cm_node->mpa_frame_rev) {
301 nes_debug(NES_DBG_CM, "The received mpa version"
302 " can not be interoperated\n");
303 return -EINVAL;
304 } else {
305 cm_node->mpa_frame_rev = mpa_frame->rev;
306 }
307
Faisal Latif1cf078c2009-12-09 15:53:54 -0800308 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
309 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
310 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
311 return -EINVAL;
312 }
313 } else {
314 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
315 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
316 return -EINVAL;
317 }
318 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800319
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530320
321 if (priv_data_len + mpa_hdr_len != len) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800322 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530323 " complete (%x + %x != %x)\n",
324 priv_data_len, mpa_hdr_len, len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800325 return -EINVAL;
326 }
327 /* make sure it does not exceed the max size */
328 if (len > MAX_CM_BUFFER) {
329 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530330 " (%x + %x != %x)\n",
331 priv_data_len, mpa_hdr_len, len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800332 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800333 }
334
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530335 cm_node->mpa_frame_size = priv_data_len;
336
337 switch (mpa_frame->rev) {
338 case IETF_MPA_V2: {
339 u16 ird_size;
340 u16 ord_size;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800341 u16 rtr_ctrl_ird;
342 u16 rtr_ctrl_ord;
343
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530344 mpa_v2_frame = (struct ietf_mpa_v2 *)buffer;
345 mpa_hdr_len += IETF_RTR_MSG_SIZE;
346 cm_node->mpa_frame_size -= IETF_RTR_MSG_SIZE;
347 rtr_msg = &mpa_v2_frame->rtr_msg;
348
349 /* parse rtr message */
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800350 rtr_ctrl_ird = ntohs(rtr_msg->ctrl_ird);
351 rtr_ctrl_ord = ntohs(rtr_msg->ctrl_ord);
352 ird_size = rtr_ctrl_ird & IETF_NO_IRD_ORD;
353 ord_size = rtr_ctrl_ord & IETF_NO_IRD_ORD;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530354
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800355 if (!(rtr_ctrl_ird & IETF_PEER_TO_PEER)) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530356 /* send reset */
357 return -EINVAL;
358 }
359
360 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
361 /* responder */
362 if (cm_node->ord_size > ird_size)
363 cm_node->ord_size = ird_size;
364 } else {
365 /* initiator */
366 if (cm_node->ord_size > ird_size)
367 cm_node->ord_size = ird_size;
368
369 if (cm_node->ird_size < ord_size) {
370 /* no resources available */
371 /* send terminate message */
372 return -EINVAL;
373 }
374 }
375
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800376 if (rtr_ctrl_ord & IETF_RDMA0_READ) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530377 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800378 } else if (rtr_ctrl_ord & IETF_RDMA0_WRITE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530379 cm_node->send_rdma0_op = SEND_RDMA_WRITE_ZERO;
380 } else { /* Not supported RDMA0 operation */
381 return -EINVAL;
382 }
383 break;
384 }
385 case IETF_MPA_V1:
386 default:
387 break;
388 }
389
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800390 /* copy entire MPA frame to our cm_node's frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530391 memcpy(cm_node->mpa_frame_buf, buffer + mpa_hdr_len, cm_node->mpa_frame_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800392
Faisal Latif9d5ab132009-03-06 15:15:01 -0800393 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
394 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800395 return 0;
396}
397
398
399/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800400 * form_cm_frame - get a free packet and build empty frame Use
401 * node info to build.
402 */
Chien Tung6098d102008-11-21 20:51:01 -0600403static void form_cm_frame(struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530404 struct nes_cm_node *cm_node, void *options, u32 optionsize,
405 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800406{
407 struct tcphdr *tcph;
408 struct iphdr *iph;
409 struct ethhdr *ethh;
410 u8 *buf;
411 u16 packetsize = sizeof(*iph);
412
413 packetsize += sizeof(*tcph);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530414 packetsize += optionsize + datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800415
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530416 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800417 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
418
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800419 buf = skb_put(skb, packetsize + ETH_HLEN);
420
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530421 ethh = (struct ethhdr *)buf;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800422 buf += ETH_HLEN;
423
424 iph = (struct iphdr *)buf;
425 buf += sizeof(*iph);
426 tcph = (struct tcphdr *)buf;
427 skb_reset_mac_header(skb);
428 skb_set_network_header(skb, ETH_HLEN);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530429 skb_set_transport_header(skb, ETH_HLEN + sizeof(*iph));
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800430 buf += sizeof(*tcph);
431
432 skb->ip_summed = CHECKSUM_PARTIAL;
Tatyana Nikolovafc4ba722012-09-20 20:55:33 +0000433 if (!(cm_node->netdev->features & NETIF_F_IP_CSUM))
434 skb->ip_summed = CHECKSUM_NONE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800435 skb->protocol = htons(0x800);
436 skb->data_len = 0;
437 skb->mac_len = ETH_HLEN;
438
439 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
440 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
441 ethh->h_proto = htons(0x0800);
442
443 iph->version = IPVERSION;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530444 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800445 iph->tos = 0;
446 iph->tot_len = htons(packetsize);
447 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
448
449 iph->frag_off = htons(0x4000);
450 iph->ttl = 0x40;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530451 iph->protocol = 0x06; /* IPPROTO_TCP */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800452
453 iph->saddr = htonl(cm_node->loc_addr);
454 iph->daddr = htonl(cm_node->rem_addr);
455
456 tcph->source = htons(cm_node->loc_port);
457 tcph->dest = htons(cm_node->rem_port);
458 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
459
460 if (flags & SET_ACK) {
461 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
462 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
463 tcph->ack = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530464 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800465 tcph->ack_seq = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530466 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800467
468 if (flags & SET_SYN) {
469 cm_node->tcp_cntxt.loc_seq_num++;
470 tcph->syn = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530471 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700472 cm_node->tcp_cntxt.loc_seq_num += datasize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530473 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800474
Faisal Latif6492cdf2008-07-24 20:50:45 -0700475 if (flags & SET_FIN) {
476 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800477 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700478 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800479
480 if (flags & SET_RST)
481 tcph->rst = 1;
482
483 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
484 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
485 tcph->urg_ptr = 0;
486 if (optionsize)
487 memcpy(buf, options, optionsize);
488 buf += optionsize;
489 if (datasize)
490 memcpy(buf, data, datasize);
491
492 skb_shinfo(skb)->nr_frags = 0;
493 cm_packets_created++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800494}
495
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800496/**
497 * print_core - dump a cm core
498 */
499static void print_core(struct nes_cm_core *core)
500{
501 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
502 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
503 if (!core)
504 return;
505 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800506
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530507 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800508
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800509 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
510 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
511
512 nes_debug(NES_DBG_CM, "core : %p \n", core);
513
514 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
515}
516
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530517/**
518 * cm_build_mpa_frame - build a MPA V1 frame or MPA V2 frame
519 */
520static int cm_build_mpa_frame(struct nes_cm_node *cm_node, u8 **start_buff,
521 u16 *buff_len, u8 *pci_mem, u8 mpa_key)
522{
523 int ret = 0;
524
525 *start_buff = (pci_mem) ? pci_mem : &cm_node->mpa_frame_buf[0];
526
527 switch (cm_node->mpa_frame_rev) {
528 case IETF_MPA_V1:
529 *start_buff = (u8 *)*start_buff + sizeof(struct ietf_rtr_msg);
530 *buff_len = sizeof(struct ietf_mpa_v1) + cm_node->mpa_frame_size;
531 build_mpa_v1(cm_node, *start_buff, mpa_key);
532 break;
533 case IETF_MPA_V2:
534 *buff_len = sizeof(struct ietf_mpa_v2) + cm_node->mpa_frame_size;
535 build_mpa_v2(cm_node, *start_buff, mpa_key);
536 break;
537 default:
538 ret = -EINVAL;
539 }
540 return ret;
541}
542
543/**
544 * build_mpa_v2 - build a MPA V2 frame
545 */
546static void build_mpa_v2(struct nes_cm_node *cm_node,
547 void *start_addr, u8 mpa_key)
548{
549 struct ietf_mpa_v2 *mpa_frame = (struct ietf_mpa_v2 *)start_addr;
550 struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800551 u16 ctrl_ird;
552 u16 ctrl_ord;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530553
554 /* initialize the upper 5 bytes of the frame */
555 build_mpa_v1(cm_node, start_addr, mpa_key);
556 mpa_frame->flags |= IETF_MPA_V2_FLAG; /* set a bit to indicate MPA V2 */
557 mpa_frame->priv_data_len += htons(IETF_RTR_MSG_SIZE);
558
559 /* initialize RTR msg */
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800560 ctrl_ird = (cm_node->ird_size > IETF_NO_IRD_ORD) ?
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530561 IETF_NO_IRD_ORD : cm_node->ird_size;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800562 ctrl_ord = (cm_node->ord_size > IETF_NO_IRD_ORD) ?
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530563 IETF_NO_IRD_ORD : cm_node->ord_size;
564
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800565 ctrl_ird |= IETF_PEER_TO_PEER;
566 ctrl_ird |= IETF_FLPDU_ZERO_LEN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530567
568 switch (mpa_key) {
569 case MPA_KEY_REQUEST:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800570 ctrl_ord |= IETF_RDMA0_WRITE;
571 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530572 break;
573 case MPA_KEY_REPLY:
574 switch (cm_node->send_rdma0_op) {
575 case SEND_RDMA_WRITE_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800576 ctrl_ord |= IETF_RDMA0_WRITE;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530577 break;
578 case SEND_RDMA_READ_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800579 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530580 break;
581 }
582 }
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800583 rtr_msg->ctrl_ird = htons(ctrl_ird);
584 rtr_msg->ctrl_ord = htons(ctrl_ord);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530585}
586
587/**
588 * build_mpa_v1 - build a MPA V1 frame
589 */
590static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
591{
592 struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
593
594 switch (mpa_key) {
595 case MPA_KEY_REQUEST:
596 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
597 break;
598 case MPA_KEY_REPLY:
599 memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
600 break;
601 }
602 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
603 mpa_frame->rev = cm_node->mpa_frame_rev;
604 mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
605}
606
607static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
608{
609 u64 u64temp;
610 struct nes_qp *nesqp = *nesqp_addr;
611 struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
612
613 u64temp = (unsigned long)nesqp;
614 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
615 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
616
617 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
618 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
619
620 switch (cm_node->send_rdma0_op) {
621 case SEND_RDMA_WRITE_ZERO:
622 nes_debug(NES_DBG_CM, "Sending first write.\n");
623 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
624 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
625 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
626 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
627 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
628 break;
629
630 case SEND_RDMA_READ_ZERO:
631 default:
Julia Lawall079abea2012-11-03 10:58:37 +0000632 if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO)
633 WARN(1, "Unsupported RDMA0 len operation=%u\n",
634 cm_node->send_rdma0_op);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530635 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;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800672
673 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
674 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800675 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800676
677 /* new_send->timetosend = currenttime */
678 new_send->retrycount = NES_DEFAULT_RETRYS;
679 new_send->retranscount = NES_DEFAULT_RETRANS;
680 new_send->skb = skb;
681 new_send->timetosend = jiffies;
682 new_send->type = type;
683 new_send->netdev = cm_node->netdev;
684 new_send->send_retrans = send_retrans;
685 new_send->close_when_complete = close_when_complete;
686
687 if (type == NES_TIMER_TYPE_CLOSE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530688 new_send->timetosend += (HZ / 10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800689 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700690 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800691 WARN_ON(1);
692 return -EINVAL;
693 }
694 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800695 }
696
697 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700698 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800699 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700700 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
701 cm_node->send_entry = new_send;
702 add_ref_cm_node(cm_node);
703 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
704 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800705
706 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
707 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700708 nes_debug(NES_DBG_CM, "Error sending packet %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530709 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800710 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700711 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800712 } else {
713 cm_packets_sent++;
714 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700715 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800716 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700717 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800718 return ret;
719 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800720 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800721 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800722
Tatyana Nikolova00ad2552012-12-06 19:56:31 +0000723 if (!timer_pending(&cm_core->tcp_timer))
724 mod_timer(&cm_core->tcp_timer, new_send->timetosend);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800725
726 return ret;
727}
728
Faisal Latif9d5ab132009-03-06 15:15:01 -0800729static void nes_retrans_expired(struct nes_cm_node *cm_node)
730{
Faisal Latif68237a02009-06-22 22:53:28 -0700731 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latifdae58722010-08-14 21:04:56 +0000732 enum nes_cm_node_state state = cm_node->state;
733 cm_node->state = NES_CM_STATE_CLOSED;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530734
Faisal Latifdae58722010-08-14 21:04:56 +0000735 switch (state) {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800736 case NES_CM_STATE_SYN_RCVD:
737 case NES_CM_STATE_CLOSING:
738 rem_ref_cm_node(cm_node->cm_core, cm_node);
739 break;
740 case NES_CM_STATE_LAST_ACK:
741 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700742 if (cm_node->cm_id)
743 cm_id->rem_ref(cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800744 send_reset(cm_node, NULL);
745 break;
746 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800747 add_ref_cm_node(cm_node);
748 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800749 create_event(cm_node, NES_CM_EVENT_ABORTED);
750 }
751}
752
753static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
754{
755 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
756 struct iw_cm_id *cm_id = cm_node->cm_id;
757 struct nes_qp *nesqp;
758 unsigned long qplockflags;
759
760 if (!recv_entry)
761 return;
762 nesqp = (struct nes_qp *)recv_entry->skb;
763 if (nesqp) {
764 spin_lock_irqsave(&nesqp->lock, qplockflags);
765 if (nesqp->cm_id) {
766 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530767 "refcount = %d: HIT A "
768 "NES_TIMER_TYPE_CLOSE with something "
769 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
770 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800771 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
772 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
773 nesqp->ibqp_state = IB_QPS_ERR;
774 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
775 nes_cm_disconn(nesqp);
776 } else {
777 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
778 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530779 "refcount = %d: HIT A "
780 "NES_TIMER_TYPE_CLOSE with nothing "
781 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
782 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800783 }
784 } else if (rem_node) {
785 /* TIME_WAIT state */
786 rem_ref_cm_node(cm_node->cm_core, cm_node);
787 }
788 if (cm_node->cm_id)
789 cm_id->rem_ref(cm_id);
790 kfree(recv_entry);
791 cm_node->recv_entry = NULL;
792}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800793
794/**
795 * nes_cm_timer_tick
796 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -0700797static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800798{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800799 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800800 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800801 struct nes_cm_node *cm_node;
802 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800803 struct list_head *list_core_temp;
804 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800805 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800806 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700807 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800808 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800809
Faisal Latif879e5bd2008-11-21 20:50:41 -0600810 struct list_head timer_list;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530811
Faisal Latif879e5bd2008-11-21 20:50:41 -0600812 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800813 spin_lock_irqsave(&cm_core->ht_lock, flags);
814
Faisal Latif6492cdf2008-07-24 20:50:45 -0700815 list_for_each_safe(list_node, list_core_temp,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530816 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800817 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800818 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600819 add_ref_cm_node(cm_node);
820 list_add(&cm_node->timer_entry, &timer_list);
821 }
822 }
823 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
824
825 list_for_each_safe(list_node, list_core_temp, &timer_list) {
826 cm_node = container_of(list_node, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530827 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800828 recv_entry = cm_node->recv_entry;
829
830 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700831 if (time_after(recv_entry->timetosend, jiffies)) {
832 if (nexttimeout > recv_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530833 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800834 nexttimeout = recv_entry->timetosend;
835 settimer = 1;
836 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530837 } else {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800838 handle_recv_entry(cm_node, 1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530839 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800840 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800841
842 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700843 do {
844 send_entry = cm_node->send_entry;
845 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600846 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800847 if (time_after(send_entry->timetosend, jiffies)) {
848 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700849 if ((nexttimeout >
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530850 send_entry->timetosend) ||
851 !settimer) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700852 nexttimeout =
853 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800854 settimer = 1;
855 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800856 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700857 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800858 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800859 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800860 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700861
862 if ((cm_node->state == NES_CM_STATE_TSA) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530863 (cm_node->state == NES_CM_STATE_CLOSED)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700864 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600865 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800866 }
867
Faisal Latif6492cdf2008-07-24 20:50:45 -0700868 if (!send_entry->retranscount ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530869 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800870 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700871 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800872
Faisal Latif6492cdf2008-07-24 20:50:45 -0700873 spin_unlock_irqrestore(
874 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800875 nes_retrans_expired(cm_node);
876 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700877 spin_lock_irqsave(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530878 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600879 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800880 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800881 atomic_inc(&send_entry->skb->users);
882 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700883 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530884 "for node %p, jiffies = %lu, time to send = "
885 "%lu, retranscount = %u, send_entry->seq_num = "
886 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
887 "0x%08X\n", send_entry, cm_node, jiffies,
888 send_entry->timetosend,
889 send_entry->retranscount,
890 send_entry->seq_num,
891 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800892
Faisal Latif6492cdf2008-07-24 20:50:45 -0700893 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530894 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800895 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700896 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800897 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700898 nes_debug(NES_DBG_CM, "rexmit failed for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530899 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800900 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800901 send_entry->retrycount--;
902 nexttimeout = jiffies + NES_SHORT_TIME;
903 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600904 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800905 } else {
906 cm_packets_sent++;
907 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700908 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530909 "%u, retry count = %u.\n",
910 send_entry->retranscount,
911 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800912 if (send_entry->send_retrans) {
913 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700914 timetosend = (NES_RETRY_TIMEOUT <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530915 (NES_DEFAULT_RETRANS - send_entry->retranscount));
Faisal Latif4e9c3902009-04-27 13:39:36 -0700916
Faisal Latif6492cdf2008-07-24 20:50:45 -0700917 send_entry->timetosend = jiffies +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530918 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700919 if (nexttimeout > send_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530920 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800921 nexttimeout = send_entry->timetosend;
922 settimer = 1;
923 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800924 } else {
925 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700926 close_when_complete =
927 send_entry->close_when_complete;
928 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530929 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700930 free_retrans_entry(cm_node);
931 if (close_when_complete)
932 rem_ref_cm_node(cm_node->cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530933 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800934 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700935 } while (0);
936
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800937 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700938 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800939 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800940
941 if (settimer) {
Tatyana Nikolova00ad2552012-12-06 19:56:31 +0000942 if (!timer_pending(&cm_core->tcp_timer))
943 mod_timer(&cm_core->tcp_timer, nexttimeout);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800944 }
945}
946
947
948/**
949 * send_syn
950 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700951static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530952 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800953{
954 int ret;
955 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800956 char optionsbuffer[sizeof(struct option_mss) +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530957 sizeof(struct option_windowscale) + sizeof(struct option_base) +
958 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800959
960 int optionssize = 0;
961 /* Sending MSS option */
962 union all_known_options *options;
963
964 if (!cm_node)
965 return -EINVAL;
966
967 options = (union all_known_options *)&optionsbuffer[optionssize];
968 options->as_mss.optionnum = OPTION_NUMBER_MSS;
969 options->as_mss.length = sizeof(struct option_mss);
970 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
971 optionssize += sizeof(struct option_mss);
972
973 options = (union all_known_options *)&optionsbuffer[optionssize];
974 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
975 options->as_windowscale.length = sizeof(struct option_windowscale);
976 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
977 optionssize += sizeof(struct option_windowscale);
978
Faisal Latif6492cdf2008-07-24 20:50:45 -0700979 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800980 options = (union all_known_options *)&optionsbuffer[optionssize];
981 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
982 options->as_base.length = sizeof(struct option_base);
983 optionssize += sizeof(struct option_base);
984 /* we need the size to be a multiple of 4 */
985 options = (union all_known_options *)&optionsbuffer[optionssize];
986 options->as_end = 1;
987 optionssize += 1;
988 options = (union all_known_options *)&optionsbuffer[optionssize];
989 options->as_end = 1;
990 optionssize += 1;
991 }
992
993 options = (union all_known_options *)&optionsbuffer[optionssize];
994 options->as_end = OPTION_NUMBER_END;
995 optionssize += 1;
996
Faisal Latif6492cdf2008-07-24 20:50:45 -0700997 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800998 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800999 if (!skb) {
1000 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1001 return -1;
1002 }
1003
1004 if (sendack)
1005 flags |= SET_ACK;
1006
1007 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
1008 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1009
1010 return ret;
1011}
1012
1013
1014/**
1015 * send_reset
1016 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001017static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001018{
1019 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001020 int flags = SET_RST | SET_ACK;
1021
Faisal Latif6492cdf2008-07-24 20:50:45 -07001022 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001023 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001024 if (!skb) {
1025 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001026 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001027 }
1028
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001029 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
1030 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
1031
1032 return ret;
1033}
1034
1035
1036/**
1037 * send_ack
1038 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001039static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001040{
1041 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001042
1043 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001044 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001045
1046 if (!skb) {
1047 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1048 return -1;
1049 }
1050
1051 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
1052 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
1053
1054 return ret;
1055}
1056
1057
1058/**
1059 * send_fin
1060 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07001061static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001062{
1063 int ret;
1064
1065 /* if we didn't get a frame get one */
1066 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001067 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001068
1069 if (!skb) {
1070 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1071 return -1;
1072 }
1073
1074 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
1075 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1076
1077 return ret;
1078}
1079
1080
1081/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001082 * find_node - find a cm node that matches the reference cm node
1083 */
1084static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301085 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001086{
1087 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001088 struct list_head *hte;
1089 struct nes_cm_node *cm_node;
1090
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001091 /* get a handle on the hte */
1092 hte = &cm_core->connected_nodes;
1093
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001094 /* walk list and find cm_node associated with this session ID */
1095 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001096 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001097 /* compare quad, return node handle if a match */
1098 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301099 cm_node->loc_addr, cm_node->loc_port,
1100 loc_addr, loc_port,
1101 cm_node->rem_addr, cm_node->rem_port,
1102 rem_addr, rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001103 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301104 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001105 add_ref_cm_node(cm_node);
1106 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1107 return cm_node;
1108 }
1109 }
1110 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1111
1112 /* no owner node */
1113 return NULL;
1114}
1115
1116
1117/**
1118 * find_listener - find a cm node listening on this addr-port pair
1119 */
1120static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301121 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001122{
1123 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001124 struct nes_cm_listener *listen_node;
1125
1126 /* walk list and find cm_node associated with this session ID */
1127 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001128 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001129 /* compare node pair, return node handle if a match */
1130 if (((listen_node->loc_addr == dst_addr) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301131 listen_node->loc_addr == 0x00000000) &&
1132 (listen_node->loc_port == dst_port) &&
1133 (listener_state & listen_node->listener_state)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001134 atomic_inc(&listen_node->ref_count);
1135 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1136 return listen_node;
1137 }
1138 }
1139 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1140
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001141 /* no listener */
1142 return NULL;
1143}
1144
1145
1146/**
1147 * add_hte_node - add a cm node to the hash table
1148 */
1149static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1150{
1151 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001152 struct list_head *hte;
1153
1154 if (!cm_node || !cm_core)
1155 return -EINVAL;
1156
Faisal Latif6492cdf2008-07-24 20:50:45 -07001157 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301158 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001159
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001160 spin_lock_irqsave(&cm_core->ht_lock, flags);
1161
1162 /* get a handle on the hash table element (list head for this slot) */
1163 hte = &cm_core->connected_nodes;
1164 list_add_tail(&cm_node->list, hte);
1165 atomic_inc(&cm_core->ht_node_cnt);
1166
1167 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1168
1169 return 0;
1170}
1171
1172
1173/**
1174 * mini_cm_dec_refcnt_listen
1175 */
1176static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301177 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001178{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001179 int ret = -EINVAL;
1180 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001181 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001182 struct list_head *list_pos = NULL;
1183 struct list_head *list_temp = NULL;
1184 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -06001185 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001186
1187 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301188 "refcnt=%d\n", listener, free_hanging_nodes,
1189 atomic_read(&listener->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001190 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -06001191 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001192 if (free_hanging_nodes) {
1193 spin_lock_irqsave(&cm_core->ht_lock, flags);
1194 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -06001195 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001196 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301197 list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001198 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -06001199 (!cm_node->accelerated)) {
1200 add_ref_cm_node(cm_node);
1201 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001202 }
1203 }
1204 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1205 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001206
1207 list_for_each_safe(list_pos, list_temp, &reset_list) {
1208 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301209 reset_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001210 {
1211 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -08001212 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001213 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
1214 rem_ref_cm_node(cm_node->cm_core, cm_node);
1215 } else {
1216 if (!loopback) {
1217 cleanup_retrans_entry(cm_node);
1218 err = send_reset(cm_node, NULL);
1219 if (err) {
1220 cm_node->state =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301221 NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001222 WARN_ON(1);
1223 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08001224 old_state = cm_node->state;
1225 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1226 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1227 rem_ref_cm_node(
1228 cm_node->cm_core,
1229 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001230 }
1231 } else {
1232 struct nes_cm_event event;
1233
1234 event.cm_node = loopback;
1235 event.cm_info.rem_addr =
1236 loopback->rem_addr;
1237 event.cm_info.loc_addr =
1238 loopback->loc_addr;
1239 event.cm_info.rem_port =
1240 loopback->rem_port;
1241 event.cm_info.loc_port =
1242 loopback->loc_port;
1243 event.cm_info.cm_id = loopback->cm_id;
Faisal Latif43093b92010-02-12 19:57:04 +00001244 add_ref_cm_node(loopback);
1245 loopback->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001246 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001247 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001248
Faisal Latif9d5ab132009-03-06 15:15:01 -08001249 rem_ref_cm_node(cm_node->cm_core,
1250 cm_node);
1251
1252 }
1253 }
1254 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001255 }
1256
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001257 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1258 if (!atomic_dec_return(&listener->ref_count)) {
1259 list_del(&listener->list);
1260
1261 /* decrement our listen node count */
1262 atomic_dec(&cm_core->listen_node_cnt);
1263
1264 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1265
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301266 if (listener->nesvnic)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001267 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301268 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001269
1270 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1271
1272 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001273 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001274 ret = 0;
Faisal Latif6e10d2e2010-02-12 19:55:03 +00001275 atomic_inc(&cm_listens_destroyed);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001276 } else {
1277 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1278 }
1279 if (listener) {
1280 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1281 nes_debug(NES_DBG_CM, "destroying listener (%p)"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301282 " with non-zero pending accepts=%u\n",
1283 listener, atomic_read(&listener->pend_accepts_cnt));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001284 }
1285
1286 return ret;
1287}
1288
1289
1290/**
1291 * mini_cm_del_listen
1292 */
1293static int mini_cm_del_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301294 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001295{
1296 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1297 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1298 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1299}
1300
1301
1302/**
1303 * mini_cm_accelerated
1304 */
1305static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301306 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001307{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001308 cm_node->accelerated = 1;
1309
1310 if (cm_node->accept_pend) {
1311 BUG_ON(!cm_node->listener);
1312 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001313 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001314 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1315 }
1316
Tatyana Nikolova00ad2552012-12-06 19:56:31 +00001317 if (!timer_pending(&cm_core->tcp_timer))
1318 mod_timer(&cm_core->tcp_timer, (jiffies + NES_SHORT_TIME));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001319
1320 return 0;
1321}
1322
1323
1324/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001325 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001326 */
Faisal Latif7a576df2009-12-09 15:54:33 -08001327static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001328{
1329 struct rtable *rt;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001330 struct neighbour *neigh;
Faisal Latif7a576df2009-12-09 15:54:33 -08001331 int rc = arpindex;
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001332 struct net_device *netdev;
Faisal Latif7a576df2009-12-09 15:54:33 -08001333 struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001334
David S. Miller78fbfd82011-03-12 00:00:52 -05001335 rt = ip_route_output(&init_net, htonl(dst_ip), 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001336 if (IS_ERR(rt)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001337 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301338 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001339 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001340 }
1341
Roland Dreiercf55bb22011-03-24 17:13:20 -07001342 if (netif_is_bond_slave(nesvnic->netdev))
Jiri Pirko7f6e7102013-01-03 22:49:00 +00001343 netdev = netdev_master_upper_dev_get(nesvnic->netdev);
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001344 else
1345 netdev = nesvnic->netdev;
1346
Tatyana Nikolovaef3d0c42012-09-20 19:23:14 +00001347 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, netdev);
David Millere55684f2012-01-24 13:16:03 +00001348
David Miller40e2bb52011-12-02 16:52:27 +00001349 rcu_read_lock();
Bob Sharp7191a0a2008-10-03 12:21:19 -07001350 if (neigh) {
1351 if (neigh->nud_state & NUD_VALID) {
1352 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001353 " is %pM, Gateway is 0x%08X \n", dst_ip,
1354 neigh->ha, ntohl(rt->rt_gateway));
Faisal Latif7a576df2009-12-09 15:54:33 -08001355
1356 if (arpindex >= 0) {
Ding Tianhong79adc532014-01-08 10:53:39 +08001357 if (ether_addr_equal(nesadapter->arp_table[arpindex].mac_addr, neigh->ha)) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001358 /* Mac address same as in nes_arp_table */
David Millere55684f2012-01-24 13:16:03 +00001359 goto out;
Faisal Latif7a576df2009-12-09 15:54:33 -08001360 }
1361
1362 nes_manage_arp_cache(nesvnic->netdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301363 nesadapter->arp_table[arpindex].mac_addr,
1364 dst_ip, NES_ARP_DELETE);
Faisal Latif7a576df2009-12-09 15:54:33 -08001365 }
1366
Bob Sharp7191a0a2008-10-03 12:21:19 -07001367 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1368 dst_ip, NES_ARP_ADD);
1369 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1370 NES_ARP_RESOLVE);
David Miller40e2bb52011-12-02 16:52:27 +00001371 } else {
1372 neigh_event_send(neigh, NULL);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001373 }
Bob Sharp7191a0a2008-10-03 12:21:19 -07001374 }
David Millere55684f2012-01-24 13:16:03 +00001375out:
David Miller40e2bb52011-12-02 16:52:27 +00001376 rcu_read_unlock();
David Millere55684f2012-01-24 13:16:03 +00001377
1378 if (neigh)
1379 neigh_release(neigh);
1380
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001381 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001382 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001383}
1384
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001385/**
1386 * make_cm_node - create a new instance of a cm node
1387 */
1388static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301389 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1390 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001391{
1392 struct nes_cm_node *cm_node;
1393 struct timespec ts;
Faisal Latif7a576df2009-12-09 15:54:33 -08001394 int oldarpindex = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001395 int arpindex = 0;
1396 struct nes_device *nesdev;
1397 struct nes_adapter *nesadapter;
1398
1399 /* create an hte and cm_node for this instance */
1400 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1401 if (!cm_node)
1402 return NULL;
1403
1404 /* set our node specific transport info */
1405 cm_node->loc_addr = cm_info->loc_addr;
1406 cm_node->rem_addr = cm_info->rem_addr;
1407 cm_node->loc_port = cm_info->loc_port;
1408 cm_node->rem_port = cm_info->rem_port;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301409
1410 cm_node->mpa_frame_rev = mpa_version;
1411 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
1412 cm_node->ird_size = IETF_NO_IRD_ORD;
1413 cm_node->ord_size = IETF_NO_IRD_ORD;
1414
Harvey Harrison63779432008-10-31 00:56:00 -07001415 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1416 &cm_node->loc_addr, cm_node->loc_port,
1417 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001418 cm_node->listener = listener;
1419 cm_node->netdev = nesvnic->netdev;
1420 cm_node->cm_id = cm_info->cm_id;
1421 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1422
Faisal Latif6492cdf2008-07-24 20:50:45 -07001423 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301424 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001425
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001426 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001427
1428 cm_node->loopbackpartner = NULL;
1429 atomic_set(&cm_node->ref_count, 1);
1430 /* associate our parent CM core */
1431 cm_node->cm_core = cm_core;
1432 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1433 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1434 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301435 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001436 ts = current_kernel_time();
1437 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1438 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301439 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001440 cm_node->tcp_cntxt.rcv_nxt = 0;
1441 /* get a unique session ID , add thread_id to an upcounter to handle race */
1442 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001443 cm_node->conn_type = cm_info->conn_type;
1444 cm_node->apbvt_set = 0;
1445 cm_node->accept_pend = 0;
1446
1447 cm_node->nesvnic = nesvnic;
1448 /* get some device handles, for arp lookup */
1449 nesdev = nesvnic->nesdev;
1450 nesadapter = nesdev->nesadapter;
1451
1452 cm_node->loopbackpartner = NULL;
Faisal Latif7a576df2009-12-09 15:54:33 -08001453
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001454 /* get the mac addr for the remote node */
Tatyana Nikolovaef3d0c42012-09-20 19:23:14 +00001455 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1456 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001457 if (arpindex < 0) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001458 kfree(cm_node);
1459 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001460 }
1461
1462 /* copy the mac addr to node context */
1463 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001464 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1465 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001466
1467 add_hte_node(cm_core, cm_node);
1468 atomic_inc(&cm_nodes_created);
1469
1470 return cm_node;
1471}
1472
1473
1474/**
1475 * add_ref_cm_node - destroy an instance of a cm node
1476 */
1477static int add_ref_cm_node(struct nes_cm_node *cm_node)
1478{
1479 atomic_inc(&cm_node->ref_count);
1480 return 0;
1481}
1482
1483
1484/**
1485 * rem_ref_cm_node - destroy an instance of a cm node
1486 */
1487static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301488 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001489{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001490 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001491 struct nes_qp *nesqp;
1492
1493 if (!cm_node)
1494 return -EINVAL;
1495
1496 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1497 if (atomic_dec_return(&cm_node->ref_count)) {
1498 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1499 return 0;
1500 }
1501 list_del(&cm_node->list);
1502 atomic_dec(&cm_core->ht_node_cnt);
1503 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1504
1505 /* if the node is destroyed before connection was accelerated */
1506 if (!cm_node->accelerated && cm_node->accept_pend) {
1507 BUG_ON(!cm_node->listener);
1508 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1509 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1510 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001511 WARN_ON(cm_node->send_entry);
1512 if (cm_node->recv_entry)
1513 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001514 if (cm_node->listener) {
1515 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1516 } else {
1517 if (cm_node->apbvt_set && cm_node->nesvnic) {
1518 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301519 PCI_FUNC(
1520 cm_node->nesvnic->nesdev->pcidev->devfn),
1521 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001522 }
1523 }
1524
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001525 atomic_dec(&cm_core->node_cnt);
1526 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001527 nesqp = cm_node->nesqp;
1528 if (nesqp) {
1529 nesqp->cm_node = NULL;
1530 nes_rem_ref(&nesqp->ibqp);
1531 cm_node->nesqp = NULL;
1532 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001533
Faisal Latif6492cdf2008-07-24 20:50:45 -07001534 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001535 return 0;
1536}
1537
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001538/**
1539 * process_options
1540 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001541static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301542 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001543{
1544 u32 tmp;
1545 u32 offset = 0;
1546 union all_known_options *all_options;
1547 char got_mss_option = 0;
1548
1549 while (offset < optionsize) {
1550 all_options = (union all_known_options *)(optionsloc + offset);
1551 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001552 case OPTION_NUMBER_END:
1553 offset = optionsize;
1554 break;
1555 case OPTION_NUMBER_NONE:
1556 offset += 1;
1557 continue;
1558 case OPTION_NUMBER_MSS:
1559 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301560 "Size: %d\n", __func__,
1561 all_options->as_mss.length, offset, optionsize);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001562 got_mss_option = 1;
1563 if (all_options->as_mss.length != 4) {
1564 return 1;
1565 } else {
1566 tmp = ntohs(all_options->as_mss.mss);
1567 if (tmp > 0 && tmp <
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301568 cm_node->tcp_cntxt.mss)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001569 cm_node->tcp_cntxt.mss = tmp;
1570 }
1571 break;
1572 case OPTION_NUMBER_WINDOW_SCALE:
1573 cm_node->tcp_cntxt.snd_wscale =
1574 all_options->as_windowscale.shiftcount;
1575 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001576 default:
1577 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301578 all_options->as_base.optionnum);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001579 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001580 }
1581 offset += all_options->as_base.length;
1582 }
1583 if ((!got_mss_option) && (syn_packet))
1584 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1585 return 0;
1586}
1587
Faisal Latif6492cdf2008-07-24 20:50:45 -07001588static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001589{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001590 atomic_inc(&cm_accel_dropped_pkts);
1591 dev_kfree_skb_any(skb);
1592}
1593
Faisal Latif9d5ab132009-03-06 15:15:01 -08001594static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001595{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001596 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301597 "refcnt=%d\n", cm_node, cm_node->state,
1598 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001599 switch (cm_node->state) {
1600 case NES_CM_STATE_SYN_RCVD:
1601 case NES_CM_STATE_SYN_SENT:
1602 case NES_CM_STATE_ESTABLISHED:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001603 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001604 cm_node->tcp_cntxt.rcv_nxt++;
1605 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001606 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001607 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001608 break;
Faisal Latifb1190d32009-12-09 15:54:32 -08001609 case NES_CM_STATE_MPAREQ_SENT:
1610 create_event(cm_node, NES_CM_EVENT_ABORTED);
1611 cm_node->tcp_cntxt.rcv_nxt++;
1612 cleanup_retrans_entry(cm_node);
1613 cm_node->state = NES_CM_STATE_CLOSED;
1614 add_ref_cm_node(cm_node);
1615 send_reset(cm_node, NULL);
1616 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001617 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001618 cm_node->tcp_cntxt.rcv_nxt++;
1619 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001620 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001621 send_ack(cm_node, NULL);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001622 /* Wait for ACK as this is simultaneous close..
Faisal Latif9d5ab132009-03-06 15:15:01 -08001623 * After we receive ACK, do not send anything..
1624 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001625 break;
1626 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001627 cm_node->tcp_cntxt.rcv_nxt++;
1628 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001629 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001630 send_ack(cm_node, NULL);
1631 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1632 break;
1633 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001634 cm_node->tcp_cntxt.rcv_nxt++;
1635 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001636 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001637 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001638 break;
1639 case NES_CM_STATE_TSA:
1640 default:
1641 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1642 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001643 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001644 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001645}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001646
Faisal Latif6492cdf2008-07-24 20:50:45 -07001647
1648static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1649 struct tcphdr *tcph)
1650{
1651
1652 int reset = 0; /* whether to send reset in case of err.. */
1653 atomic_inc(&cm_resets_recvd);
1654 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1655 " refcnt=%d\n", cm_node, cm_node->state,
1656 atomic_read(&cm_node->ref_count));
1657 cleanup_retrans_entry(cm_node);
1658 switch (cm_node->state) {
1659 case NES_CM_STATE_SYN_SENT:
1660 case NES_CM_STATE_MPAREQ_SENT:
1661 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1662 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1663 cm_node->listener, cm_node->state);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301664 switch (cm_node->mpa_frame_rev) {
1665 case IETF_MPA_V2:
1666 cm_node->mpa_frame_rev = IETF_MPA_V1;
1667 /* send a syn and goto syn sent state */
1668 cm_node->state = NES_CM_STATE_SYN_SENT;
1669 if (send_syn(cm_node, 0, NULL)) {
1670 active_open_err(cm_node, skb, reset);
1671 }
1672 break;
1673 case IETF_MPA_V1:
1674 default:
1675 active_open_err(cm_node, skb, reset);
1676 break;
1677 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001678 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001679 case NES_CM_STATE_MPAREQ_RCVD:
Dan Carpenter4d8d6382010-09-15 18:32:39 +02001680 atomic_inc(&cm_node->passive_state);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001681 dev_kfree_skb_any(skb);
1682 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001683 case NES_CM_STATE_ESTABLISHED:
1684 case NES_CM_STATE_SYN_RCVD:
1685 case NES_CM_STATE_LISTENING:
1686 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1687 passive_open_err(cm_node, skb, reset);
1688 break;
1689 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001690 active_open_err(cm_node, skb, reset);
1691 break;
1692 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001693 drop_packet(skb);
1694 break;
Faisal Latifdae58722010-08-14 21:04:56 +00001695 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif68237a02009-06-22 22:53:28 -07001696 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001697 case NES_CM_STATE_LAST_ACK:
1698 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001699 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001700 cm_node->state = NES_CM_STATE_CLOSED;
1701 rem_ref_cm_node(cm_node->cm_core, cm_node);
1702 drop_packet(skb);
1703 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001704 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001705 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001706 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001707 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001708}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001709
Faisal Latif9d5ab132009-03-06 15:15:01 -08001710
1711static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001712{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301713 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001714 int datasize = skb->len;
1715 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001716
1717 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301718 u32 res_type;
1719
Faisal Latif9d5ab132009-03-06 15:15:01 -08001720 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1721 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001722 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001723 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001724 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301725 "cm_node=%p listener=%p state=%d\n", __func__,
1726 __LINE__, cm_node, cm_node->listener,
1727 cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001728 active_open_err(cm_node, skb, 1);
1729 } else {
1730 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001731 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001732 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001733 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001734
1735 switch (cm_node->state) {
1736 case NES_CM_STATE_ESTABLISHED:
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301737 if (res_type == NES_MPA_REQUEST_REJECT)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001738 /*BIG problem as we are receiving the MPA.. So should
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301739 * not be REJECT.. This is Passive Open.. We can
1740 * only receive it Reject for Active Open...*/
Faisal Latif9d5ab132009-03-06 15:15:01 -08001741 WARN_ON(1);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001742 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1743 type = NES_CM_EVENT_MPA_REQ;
1744 atomic_set(&cm_node->passive_state,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301745 NES_PASSIVE_STATE_INDICATED);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001746 break;
1747 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001748 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001749 if (res_type == NES_MPA_REQUEST_REJECT) {
1750 type = NES_CM_EVENT_MPA_REJECT;
1751 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1752 } else {
1753 type = NES_CM_EVENT_CONNECTED;
1754 cm_node->state = NES_CM_STATE_TSA;
1755 }
1756
1757 break;
1758 default:
1759 WARN_ON(1);
1760 break;
1761 }
1762 dev_kfree_skb_any(skb);
1763 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001764}
1765
1766static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1767{
1768 switch (cm_node->state) {
1769 case NES_CM_STATE_SYN_SENT:
1770 case NES_CM_STATE_MPAREQ_SENT:
1771 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301772 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1773 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001774 active_open_err(cm_node, skb, 1);
1775 break;
1776 case NES_CM_STATE_ESTABLISHED:
1777 case NES_CM_STATE_SYN_RCVD:
1778 passive_open_err(cm_node, skb, 1);
1779 break;
1780 case NES_CM_STATE_TSA:
1781 default:
1782 drop_packet(skb);
1783 }
1784}
1785
1786static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301787 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001788{
1789 int err;
1790
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301791 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001792 if (err)
1793 active_open_err(cm_node, skb, 1);
1794
1795 return err;
1796}
1797
1798static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301799 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001800{
1801 int err = 0;
1802 u32 seq;
1803 u32 ack_seq;
1804 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1805 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1806 u32 rcv_wnd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301807
Faisal Latif6492cdf2008-07-24 20:50:45 -07001808 seq = ntohl(tcph->seq);
1809 ack_seq = ntohl(tcph->ack_seq);
1810 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1811 if (ack_seq != loc_seq_num)
1812 err = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301813 else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001814 err = 1;
1815 if (err) {
1816 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301817 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1818 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001819 indicate_pkt_err(cm_node, skb);
1820 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301821 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1822 rcv_wnd);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001823 }
1824 return err;
1825}
1826
1827/*
1828 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1829 * is created with a listener or it may comein as rexmitted packet which in
1830 * that case will be just dropped.
1831 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001832static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301833 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001834{
1835 int ret;
1836 u32 inc_sequence;
1837 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001838
1839 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001840 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001841 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001842
Faisal Latif6492cdf2008-07-24 20:50:45 -07001843 switch (cm_node->state) {
1844 case NES_CM_STATE_SYN_SENT:
1845 case NES_CM_STATE_MPAREQ_SENT:
1846 /* Rcvd syn on active open connection*/
1847 active_open_err(cm_node, skb, 1);
1848 break;
1849 case NES_CM_STATE_LISTENING:
1850 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001851 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301852 cm_node->listener->backlog) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001853 nes_debug(NES_DBG_CM, "drop syn due to backlog "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301854 "pressure \n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07001855 cm_backlog_drops++;
1856 passive_open_err(cm_node, skb, 0);
1857 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001858 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001859 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301860 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001861 if (ret) {
1862 passive_open_err(cm_node, skb, 0);
1863 /* drop pkt */
1864 break;
1865 }
1866 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1867 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001868 cm_node->accept_pend = 1;
1869 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1870
Faisal Latif6492cdf2008-07-24 20:50:45 -07001871 cm_node->state = NES_CM_STATE_SYN_RCVD;
1872 send_syn(cm_node, 1, skb);
1873 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001874 case NES_CM_STATE_CLOSED:
1875 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001876 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001877 send_reset(cm_node, skb);
1878 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001879 case NES_CM_STATE_TSA:
1880 case NES_CM_STATE_ESTABLISHED:
1881 case NES_CM_STATE_FIN_WAIT1:
1882 case NES_CM_STATE_FIN_WAIT2:
1883 case NES_CM_STATE_MPAREQ_RCVD:
1884 case NES_CM_STATE_LAST_ACK:
1885 case NES_CM_STATE_CLOSING:
1886 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001887 default:
1888 drop_packet(skb);
1889 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001890 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001891}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001892
Faisal Latif6492cdf2008-07-24 20:50:45 -07001893static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301894 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001895{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001896 int ret;
1897 u32 inc_sequence;
1898 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001899
Faisal Latif6492cdf2008-07-24 20:50:45 -07001900 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001901 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001902 inc_sequence = ntohl(tcph->seq);
1903 switch (cm_node->state) {
1904 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001905 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001906 /* active open */
1907 if (check_syn(cm_node, tcph, skb))
1908 return;
1909 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1910 /* setup options */
1911 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1912 if (ret) {
1913 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301914 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001915 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001916 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001917 cleanup_retrans_entry(cm_node);
1918 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1919 send_mpa_request(cm_node, skb);
1920 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1921 break;
1922 case NES_CM_STATE_MPAREQ_RCVD:
1923 /* passive open, so should not be here */
1924 passive_open_err(cm_node, skb, 1);
1925 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001926 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001927 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1928 cleanup_retrans_entry(cm_node);
1929 cm_node->state = NES_CM_STATE_CLOSED;
1930 send_reset(cm_node, skb);
1931 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001932 case NES_CM_STATE_CLOSED:
1933 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1934 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001935 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001936 send_reset(cm_node, skb);
1937 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001938 case NES_CM_STATE_ESTABLISHED:
1939 case NES_CM_STATE_FIN_WAIT1:
1940 case NES_CM_STATE_FIN_WAIT2:
1941 case NES_CM_STATE_LAST_ACK:
1942 case NES_CM_STATE_TSA:
1943 case NES_CM_STATE_CLOSING:
1944 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001945 case NES_CM_STATE_MPAREQ_SENT:
1946 default:
1947 drop_packet(skb);
1948 break;
1949 }
1950}
1951
Faisal Latif109d67e2009-04-27 13:41:06 -07001952static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301953 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001954{
1955 int datasize = 0;
1956 u32 inc_sequence;
Faisal Latif109d67e2009-04-27 13:41:06 -07001957 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06001958 int optionsize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301959
Faisal Latifabb77252008-11-21 20:50:52 -06001960 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001961
Faisal Latif6492cdf2008-07-24 20:50:45 -07001962 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07001963 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001964
1965 skb_pull(skb, tcph->doff << 2);
1966 inc_sequence = ntohl(tcph->seq);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001967 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001968 switch (cm_node->state) {
1969 case NES_CM_STATE_SYN_RCVD:
1970 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07001971 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001972 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1973 if (ret)
1974 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001975 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1976 cm_node->state = NES_CM_STATE_ESTABLISHED;
1977 if (datasize) {
1978 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001979 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301980 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001981 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301982 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001983 break;
1984 case NES_CM_STATE_ESTABLISHED:
1985 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001986 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001987 if (datasize) {
1988 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001989 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301990 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001991 drop_packet(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301992 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001993 break;
1994 case NES_CM_STATE_MPAREQ_SENT:
1995 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1996 if (datasize) {
1997 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001998 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301999 } else { /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002000 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302001 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002002 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002003 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002004 cleanup_retrans_entry(cm_node);
2005 cm_node->state = NES_CM_STATE_CLOSED;
2006 send_reset(cm_node, skb);
2007 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002008 case NES_CM_STATE_CLOSED:
2009 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08002010 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002011 send_reset(cm_node, skb);
2012 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002013 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002014 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002015 cleanup_retrans_entry(cm_node);
2016 cm_node->state = NES_CM_STATE_CLOSED;
2017 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002018 rem_ref_cm_node(cm_node->cm_core, cm_node);
2019 drop_packet(skb);
2020 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002021 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002022 cleanup_retrans_entry(cm_node);
2023 drop_packet(skb);
2024 cm_node->state = NES_CM_STATE_FIN_WAIT2;
2025 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002026 case NES_CM_STATE_SYN_SENT:
2027 case NES_CM_STATE_FIN_WAIT2:
2028 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002029 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002030 case NES_CM_STATE_UNKNOWN:
2031 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002032 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002033 drop_packet(skb);
2034 break;
2035 }
Faisal Latif109d67e2009-04-27 13:41:06 -07002036 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002037}
2038
2039
2040
2041static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302042 struct sk_buff *skb, int optionsize, int passive)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002043{
2044 u8 *optionsloc = (u8 *)&tcph[1];
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302045
Faisal Latif6492cdf2008-07-24 20:50:45 -07002046 if (optionsize) {
2047 if (process_options(cm_node, optionsloc, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302048 (u32)tcph->syn)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002049 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302050 __func__, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002051 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06002052 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002053 else
Faisal Latifabb77252008-11-21 20:50:52 -06002054 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002055 return 1;
2056 }
2057 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002058
2059 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302060 cm_node->tcp_cntxt.snd_wscale;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002061
Faisal Latif6492cdf2008-07-24 20:50:45 -07002062 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002063 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002064 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002065}
2066
Faisal Latif6492cdf2008-07-24 20:50:45 -07002067/*
2068 * active_open_err() will send reset() if flag set..
2069 * It will also send ABORT event.
2070 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002071static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302072 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002073{
2074 cleanup_retrans_entry(cm_node);
2075 if (reset) {
2076 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302077 "state=%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002078 add_ref_cm_node(cm_node);
2079 send_reset(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302080 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002081 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302082 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002083
2084 cm_node->state = NES_CM_STATE_CLOSED;
2085 create_event(cm_node, NES_CM_EVENT_ABORTED);
2086}
2087
2088/*
2089 * passive_open_err() will either do a reset() or will free up the skb and
2090 * remove the cm_node.
2091 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002092static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302093 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002094{
2095 cleanup_retrans_entry(cm_node);
2096 cm_node->state = NES_CM_STATE_CLOSED;
2097 if (reset) {
2098 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302099 "cm_node=%p state =%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002100 send_reset(cm_node, skb);
2101 } else {
2102 dev_kfree_skb_any(skb);
2103 rem_ref_cm_node(cm_node->cm_core, cm_node);
2104 }
2105}
2106
2107/*
2108 * free_retrans_entry() routines assumes that the retrans_list_lock has
2109 * been acquired before calling.
2110 */
2111static void free_retrans_entry(struct nes_cm_node *cm_node)
2112{
2113 struct nes_timer_entry *send_entry;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302114
Faisal Latif6492cdf2008-07-24 20:50:45 -07002115 send_entry = cm_node->send_entry;
2116 if (send_entry) {
2117 cm_node->send_entry = NULL;
2118 dev_kfree_skb_any(send_entry->skb);
2119 kfree(send_entry);
2120 rem_ref_cm_node(cm_node->cm_core, cm_node);
2121 }
2122}
2123
2124static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
2125{
2126 unsigned long flags;
2127
2128 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
2129 free_retrans_entry(cm_node);
2130 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
2131}
2132
2133/**
2134 * process_packet
2135 * Returns skb if to be freed, else it will return NULL if already used..
2136 */
2137static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302138 struct nes_cm_core *cm_core)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002139{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302140 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002141 struct tcphdr *tcph = tcp_hdr(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302142 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07002143 int ret = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302144
Faisal Latif6492cdf2008-07-24 20:50:45 -07002145 skb_pull(skb, ip_hdr(skb)->ihl << 2);
2146
2147 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302148 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
2149 tcph->ack, tcph->rst, tcph->fin);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002150
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302151 if (tcph->rst) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002152 pkt_type = NES_PKT_TYPE_RST;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302153 } else if (tcph->syn) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002154 pkt_type = NES_PKT_TYPE_SYN;
2155 if (tcph->ack)
2156 pkt_type = NES_PKT_TYPE_SYNACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302157 } else if (tcph->ack) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002158 pkt_type = NES_PKT_TYPE_ACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302159 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002160 if (tcph->fin)
2161 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002162
2163 switch (pkt_type) {
2164 case NES_PKT_TYPE_SYN:
2165 handle_syn_pkt(cm_node, skb, tcph);
2166 break;
2167 case NES_PKT_TYPE_SYNACK:
2168 handle_synack_pkt(cm_node, skb, tcph);
2169 break;
2170 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002171 ret = handle_ack_pkt(cm_node, skb, tcph);
2172 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08002173 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002174 break;
2175 case NES_PKT_TYPE_RST:
2176 handle_rst_pkt(cm_node, skb, tcph);
2177 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002178 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002179 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08002180 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07002181 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002182 break;
2183 }
2184}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002185
2186/**
2187 * mini_cm_listen - create a listen node with params
2188 */
2189static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302190 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002191{
2192 struct nes_cm_listener *listener;
2193 unsigned long flags;
2194
2195 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302196 cm_info->loc_addr, cm_info->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002197
2198 /* cannot have multiple matching listeners */
2199 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302200 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002201 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
2202 /* find automatically incs ref count ??? */
2203 atomic_dec(&listener->ref_count);
2204 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
2205 return NULL;
2206 }
2207
2208 if (!listener) {
2209 /* create a CM listen node (1/2 node to compare incoming traffic to) */
2210 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
2211 if (!listener) {
2212 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
2213 return NULL;
2214 }
2215
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002216 listener->loc_addr = htonl(cm_info->loc_addr);
2217 listener->loc_port = htons(cm_info->loc_port);
2218 listener->reused_node = 0;
2219
2220 atomic_set(&listener->ref_count, 1);
2221 }
2222 /* pasive case */
2223 /* find already inc'ed the ref count */
2224 else {
2225 listener->reused_node = 1;
2226 }
2227
2228 listener->cm_id = cm_info->cm_id;
2229 atomic_set(&listener->pend_accepts_cnt, 0);
2230 listener->cm_core = cm_core;
2231 listener->nesvnic = nesvnic;
2232 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002233
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002234 listener->conn_type = cm_info->conn_type;
2235 listener->backlog = cm_info->backlog;
2236 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
2237
2238 if (!listener->reused_node) {
2239 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2240 list_add(&listener->list, &cm_core->listen_list.list);
2241 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2242 atomic_inc(&cm_core->listen_node_cnt);
2243 }
2244
2245 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302246 " listener = %p, backlog = %d, cm_id = %p.\n",
2247 cm_info->loc_addr, cm_info->loc_port,
2248 listener, listener->backlog, listener->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002249
2250 return listener;
2251}
2252
2253
2254/**
2255 * mini_cm_connect - make a connection node with params
2256 */
Faisal Latif54c86a82008-09-30 14:47:27 -07002257static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302258 struct nes_vnic *nesvnic, u16 private_data_len,
2259 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002260{
2261 int ret = 0;
2262 struct nes_cm_node *cm_node;
2263 struct nes_cm_listener *loopbackremotelistener;
2264 struct nes_cm_node *loopbackremotenode;
2265 struct nes_cm_info loopback_cm_info;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302266 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002267
2268 /* create a CM connection node */
2269 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2270 if (!cm_node)
2271 return NULL;
2272
Glenn Streiff7495ab62008-04-29 13:46:54 -07002273 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002274 cm_node->tcp_cntxt.client = 1;
2275 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2276
2277 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002278 loopbackremotelistener = find_listener(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302279 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2280 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002281 if (loopbackremotelistener == NULL) {
2282 create_event(cm_node, NES_CM_EVENT_ABORTED);
2283 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002284 loopback_cm_info = *cm_info;
2285 loopback_cm_info.loc_port = cm_info->rem_port;
2286 loopback_cm_info.rem_port = cm_info->loc_port;
2287 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002288 loopbackremotenode = make_cm_node(cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302289 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002290 if (!loopbackremotenode) {
2291 rem_ref_cm_node(cm_node->cm_core, cm_node);
2292 return NULL;
2293 }
2294 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002295 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002296 loopbackremotenode->tcp_cntxt.rcv_wscale =
2297 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002298 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002299 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302300 private_data_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002301 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002302
Faisal Latif6492cdf2008-07-24 20:50:45 -07002303 /* we are done handling this state. */
2304 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002305 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002306 cm_node->tcp_cntxt.rcv_nxt =
2307 loopbackremotenode->tcp_cntxt.loc_seq_num;
2308 loopbackremotenode->tcp_cntxt.rcv_nxt =
2309 cm_node->tcp_cntxt.loc_seq_num;
2310 cm_node->tcp_cntxt.max_snd_wnd =
2311 loopbackremotenode->tcp_cntxt.rcv_wnd;
2312 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2313 cm_node->tcp_cntxt.rcv_wnd;
2314 cm_node->tcp_cntxt.snd_wnd =
2315 loopbackremotenode->tcp_cntxt.rcv_wnd;
2316 loopbackremotenode->tcp_cntxt.snd_wnd =
2317 cm_node->tcp_cntxt.rcv_wnd;
2318 cm_node->tcp_cntxt.snd_wscale =
2319 loopbackremotenode->tcp_cntxt.rcv_wscale;
2320 loopbackremotenode->tcp_cntxt.snd_wscale =
2321 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002322 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002323 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2324 }
2325 return cm_node;
2326 }
2327
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302328 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
2329 cm_node->mpa_frame_size = private_data_len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002330
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302331 memcpy(start_buff, private_data, private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002332
2333 /* send a syn and goto syn sent state */
2334 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002335 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002336
Faisal Latif6492cdf2008-07-24 20:50:45 -07002337 if (ret) {
2338 /* error in sending the syn free up the cm_node struct */
2339 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302340 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2341 cm_node->rem_addr, cm_node->rem_port, cm_node,
2342 cm_node->cm_id);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002343 rem_ref_cm_node(cm_node->cm_core, cm_node);
2344 cm_node = NULL;
2345 }
2346
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302347 if (cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002348 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302349 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2350 cm_node->rem_addr, cm_node->rem_port, cm_node,
2351 cm_node->cm_id);
2352 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002353
2354 return cm_node;
2355}
2356
2357
2358/**
2359 * mini_cm_accept - accept a connection
2360 * This function is never called
2361 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302362static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002363{
2364 return 0;
2365}
2366
2367
2368/**
2369 * mini_cm_reject - reject and teardown a connection
2370 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302371static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002372{
2373 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002374 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002375 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002376 struct nes_cm_event event;
2377 struct iw_cm_id *cm_id = cm_node->cm_id;
2378 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002379
Faisal Latif6492cdf2008-07-24 20:50:45 -07002380 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302381 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002382
Faisal Latif6492cdf2008-07-24 20:50:45 -07002383 if (cm_node->tcp_cntxt.client)
2384 return ret;
2385 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002386
Faisal Latif9d5ab132009-03-06 15:15:01 -08002387 if (!loopback) {
2388 passive_state = atomic_add_return(1, &cm_node->passive_state);
2389 if (passive_state == NES_SEND_RESET_EVENT) {
2390 cm_node->state = NES_CM_STATE_CLOSED;
2391 rem_ref_cm_node(cm_core, cm_node);
2392 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002393 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2394 rem_ref_cm_node(cm_core, cm_node);
2395 } else {
2396 ret = send_mpa_reject(cm_node);
2397 if (ret) {
2398 cm_node->state = NES_CM_STATE_CLOSED;
2399 err = send_reset(cm_node, NULL);
2400 if (err)
2401 WARN_ON(1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302402 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002403 cm_id->add_ref(cm_id);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302404 }
Faisal Latifc5a7d482009-12-09 15:54:08 -08002405 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002406 }
2407 } else {
2408 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002409 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2410 rem_ref_cm_node(cm_core, cm_node);
2411 rem_ref_cm_node(cm_core, loopback);
2412 } else {
2413 event.cm_node = loopback;
2414 event.cm_info.rem_addr = loopback->rem_addr;
2415 event.cm_info.loc_addr = loopback->loc_addr;
2416 event.cm_info.rem_port = loopback->rem_port;
2417 event.cm_info.loc_port = loopback->loc_port;
2418 event.cm_info.cm_id = loopback->cm_id;
2419 cm_event_mpa_reject(&event);
2420 rem_ref_cm_node(cm_core, cm_node);
2421 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002422
Faisal Latifc5a7d482009-12-09 15:54:08 -08002423 cm_id = loopback->cm_id;
2424 rem_ref_cm_node(cm_core, loopback);
2425 cm_id->rem_ref(cm_id);
2426 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002427 }
2428
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002429 return ret;
2430}
2431
2432
2433/**
2434 * mini_cm_close
2435 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002436static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002437{
2438 int ret = 0;
2439
2440 if (!cm_core || !cm_node)
2441 return -EINVAL;
2442
2443 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002444 case NES_CM_STATE_SYN_RCVD:
2445 case NES_CM_STATE_SYN_SENT:
2446 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2447 case NES_CM_STATE_ESTABLISHED:
2448 case NES_CM_STATE_ACCEPTING:
2449 case NES_CM_STATE_MPAREQ_SENT:
2450 case NES_CM_STATE_MPAREQ_RCVD:
2451 cleanup_retrans_entry(cm_node);
2452 send_reset(cm_node, NULL);
2453 break;
2454 case NES_CM_STATE_CLOSE_WAIT:
2455 cm_node->state = NES_CM_STATE_LAST_ACK;
2456 send_fin(cm_node, NULL);
2457 break;
2458 case NES_CM_STATE_FIN_WAIT1:
2459 case NES_CM_STATE_FIN_WAIT2:
2460 case NES_CM_STATE_LAST_ACK:
2461 case NES_CM_STATE_TIME_WAIT:
2462 case NES_CM_STATE_CLOSING:
2463 ret = -1;
2464 break;
2465 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002466 cleanup_retrans_entry(cm_node);
2467 send_reset(cm_node, NULL);
2468 break;
2469 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002470 case NES_CM_STATE_UNKNOWN:
2471 case NES_CM_STATE_INITED:
2472 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002473 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002474 ret = rem_ref_cm_node(cm_core, cm_node);
2475 break;
2476 case NES_CM_STATE_TSA:
2477 if (cm_node->send_entry)
2478 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302479 "send_entry=%p\n", cm_node->send_entry);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002480 ret = rem_ref_cm_node(cm_core, cm_node);
2481 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002482 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002483 return ret;
2484}
2485
2486
2487/**
2488 * recv_pkt - recv an ETHERNET packet, and process it through CM
2489 * node state machine
2490 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002491static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302492 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002493{
2494 struct nes_cm_node *cm_node = NULL;
2495 struct nes_cm_listener *listener = NULL;
2496 struct iphdr *iph;
2497 struct tcphdr *tcph;
2498 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002499 int skb_handled = 1;
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002500 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002501
Faisal Latif6492cdf2008-07-24 20:50:45 -07002502 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002503 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302504 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002505 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002506
2507 iph = (struct iphdr *)skb->data;
2508 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002509
2510 nfo.loc_addr = ntohl(iph->daddr);
2511 nfo.loc_port = ntohs(tcph->dest);
2512 nfo.rem_addr = ntohl(iph->saddr);
2513 nfo.rem_port = ntohs(tcph->source);
2514
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002515 tmp_daddr = cpu_to_be32(iph->daddr);
2516 tmp_saddr = cpu_to_be32(iph->saddr);
2517
Harvey Harrison63779432008-10-31 00:56:00 -07002518 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002519 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002520
Faisal Latif6492cdf2008-07-24 20:50:45 -07002521 do {
2522 cm_node = find_node(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302523 nfo.rem_port, nfo.rem_addr,
2524 nfo.loc_port, nfo.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002525
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002526 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002527 /* Only type of packet accepted are for */
2528 /* the PASSIVE open (syn only) */
2529 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002530 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002531 break;
2532 }
2533 listener = find_listener(cm_core, nfo.loc_addr,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302534 nfo.loc_port,
2535 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002536 if (!listener) {
2537 nfo.cm_id = NULL;
2538 nfo.conn_type = 0;
2539 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2540 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002541 break;
2542 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002543 nfo.cm_id = listener->cm_id;
2544 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002545 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302546 listener);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002547 if (!cm_node) {
2548 nes_debug(NES_DBG_CM, "Unable to allocate "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302549 "node\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07002550 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002551 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002552 dev_kfree_skb_any(skb);
2553 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002554 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002555 if (!tcph->rst && !tcph->fin) {
2556 cm_node->state = NES_CM_STATE_LISTENING;
2557 } else {
2558 cm_packets_dropped++;
2559 rem_ref_cm_node(cm_core, cm_node);
2560 dev_kfree_skb_any(skb);
2561 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002562 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002563 add_ref_cm_node(cm_node);
2564 } else if (cm_node->state == NES_CM_STATE_TSA) {
Faisal Latif0f0bee82011-09-25 20:34:00 -05002565 if (cm_node->nesqp->pau_mode)
2566 nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
2567 else {
2568 rem_ref_cm_node(cm_core, cm_node);
2569 atomic_inc(&cm_accel_dropped_pkts);
2570 dev_kfree_skb_any(skb);
2571 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002572 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002573 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002574 skb_reset_network_header(skb);
2575 skb_set_transport_header(skb, sizeof(*tcph));
2576 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002577 process_packet(cm_node, skb, cm_core);
2578 rem_ref_cm_node(cm_core, cm_node);
2579 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002580 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002581}
2582
2583
2584/**
2585 * nes_cm_alloc_core - allocate a top level instance of a cm core
2586 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002587static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002588{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002589 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002590
2591 /* setup the CM core */
2592 /* alloc top level core control structure */
2593 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2594 if (!cm_core)
2595 return NULL;
2596
2597 INIT_LIST_HEAD(&cm_core->connected_nodes);
2598 init_timer(&cm_core->tcp_timer);
2599 cm_core->tcp_timer.function = nes_cm_timer_tick;
2600
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302601 cm_core->mtu = NES_CM_DEFAULT_MTU;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002602 cm_core->state = NES_CM_STATE_INITED;
2603 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2604
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002605 atomic_set(&cm_core->events_posted, 0);
2606
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002607 cm_core->api = &nes_cm_api;
2608
2609 spin_lock_init(&cm_core->ht_lock);
2610 spin_lock_init(&cm_core->listen_list_lock);
2611
2612 INIT_LIST_HEAD(&cm_core->listen_list.list);
2613
2614 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2615
2616 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2617 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2618 cm_core->post_event = nes_cm_post_event;
2619 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2620 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2621
2622 print_core(cm_core);
2623 return cm_core;
2624}
2625
2626
2627/**
2628 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2629 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002630static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002631{
2632 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2633
2634 if (!cm_core)
2635 return -EINVAL;
2636
2637 barrier();
2638
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302639 if (timer_pending(&cm_core->tcp_timer))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002640 del_timer(&cm_core->tcp_timer);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002641
2642 destroy_workqueue(cm_core->event_wq);
2643 destroy_workqueue(cm_core->disconn_wq);
2644 nes_debug(NES_DBG_CM, "\n");
2645 kfree(cm_core);
2646
2647 return 0;
2648}
2649
2650
2651/**
2652 * mini_cm_get
2653 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002654static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002655{
2656 return cm_core->state;
2657}
2658
2659
2660/**
2661 * mini_cm_set
2662 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002663static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002664{
2665 int ret = 0;
2666
2667 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002668 case NES_CM_SET_PKT_SIZE:
2669 cm_core->mtu = value;
2670 break;
2671 case NES_CM_SET_FREE_PKT_Q_SIZE:
2672 cm_core->free_tx_pkt_max = value;
2673 break;
2674 default:
2675 /* unknown set option */
2676 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002677 }
2678
2679 return ret;
2680}
2681
2682
2683/**
2684 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2685 * successfully exchanged when this is called
2686 */
2687static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2688{
2689 int ret = 0;
2690
2691 if (!nesqp)
2692 return -EINVAL;
2693
2694 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302695 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2696 NES_QPCONTEXT_MISC_DROS);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002697
2698 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2699 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2700
2701 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2702
2703 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2704
2705 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302706 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002707
2708 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302709 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2710 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002711
2712 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302713 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2714 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002715
2716 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2717 nesqp->nesqp_context->ts_recent = 0;
2718 nesqp->nesqp_context->ts_age = 0;
2719 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2720 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2721 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2722 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302723 cm_node->tcp_cntxt.rcv_wscale);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002724 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2725 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2726 nesqp->nesqp_context->srtt = 0;
2727 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2728 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302729 nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002730 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2731 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2732 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2733
2734 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302735 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2736 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2737 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2738 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2739 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2740 le32_to_cpu(nesqp->nesqp_context->misc));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002741 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2742 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2743 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2744
2745 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2746 cm_node->state = NES_CM_STATE_TSA;
2747
2748 return ret;
2749}
2750
2751
2752/**
2753 * nes_cm_disconn
2754 */
2755int nes_cm_disconn(struct nes_qp *nesqp)
2756{
Don Wood873fcdd2009-09-05 20:36:37 -07002757 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002758
Don Wood873fcdd2009-09-05 20:36:37 -07002759 work = kzalloc(sizeof *work, GFP_ATOMIC);
2760 if (!work)
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302761 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002762
Don Wood873fcdd2009-09-05 20:36:37 -07002763 nes_add_ref(&nesqp->ibqp);
2764 work->nesqp = nesqp;
2765 INIT_WORK(&work->work, nes_disconnect_worker);
2766 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002767 return 0;
2768}
2769
2770
2771/**
2772 * nes_disconnect_worker
2773 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002774static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002775{
Don Wood873fcdd2009-09-05 20:36:37 -07002776 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2777 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002778
Don Wood873fcdd2009-09-05 20:36:37 -07002779 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002780 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302781 nesqp->last_aeq, nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002782 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002783 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002784}
2785
2786
2787/**
2788 * nes_cm_disconn_true
2789 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002790static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002791{
2792 unsigned long flags;
2793 int ret = 0;
2794 struct iw_cm_id *cm_id;
2795 struct iw_cm_event cm_event;
2796 struct nes_vnic *nesvnic;
2797 u16 last_ae;
2798 u8 original_hw_tcp_state;
2799 u8 original_ibqp_state;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002800 int disconn_status = 0;
Don Woodb29a4fc2009-09-05 20:36:39 -07002801 int issue_disconn = 0;
2802 int issue_close = 0;
2803 int issue_flush = 0;
2804 u32 flush_q = NES_CQP_FLUSH_RQ;
2805 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002806
2807 if (!nesqp) {
2808 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2809 return -1;
2810 }
2811
2812 spin_lock_irqsave(&nesqp->lock, flags);
2813 cm_id = nesqp->cm_id;
2814 /* make sure we havent already closed this connection */
2815 if (!cm_id) {
2816 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302817 nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002818 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002819 return -1;
2820 }
2821
2822 nesvnic = to_nesvnic(nesqp->ibqp.device);
2823 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2824
2825 original_hw_tcp_state = nesqp->hw_tcp_state;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302826 original_ibqp_state = nesqp->ibqp_state;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002827 last_ae = nesqp->last_aeq;
2828
Don Woodb29a4fc2009-09-05 20:36:39 -07002829 if (nesqp->term_flags) {
2830 issue_disconn = 1;
2831 issue_close = 1;
2832 nesqp->cm_id = NULL;
Tatyana Nikolova196f40c2011-12-05 11:39:12 -06002833 del_timer(&nesqp->terminate_timer);
Don Woodb29a4fc2009-09-05 20:36:39 -07002834 if (nesqp->flush_issued == 0) {
2835 nesqp->flush_issued = 1;
2836 issue_flush = 1;
2837 }
2838 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2839 ((original_ibqp_state == IB_QPS_RTS) &&
2840 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2841 issue_disconn = 1;
2842 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002843 disconn_status = -ECONNRESET;
Don Woodb29a4fc2009-09-05 20:36:39 -07002844 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002845
Don Woodb29a4fc2009-09-05 20:36:39 -07002846 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2847 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2848 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2849 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2850 issue_close = 1;
2851 nesqp->cm_id = NULL;
2852 if (nesqp->flush_issued == 0) {
2853 nesqp->flush_issued = 1;
2854 issue_flush = 1;
2855 }
2856 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002857
Don Woodb29a4fc2009-09-05 20:36:39 -07002858 spin_unlock_irqrestore(&nesqp->lock, flags);
2859
2860 if ((issue_flush) && (nesqp->destroyed == 0)) {
2861 /* Flush the queue(s) */
2862 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2863 flush_q |= NES_CQP_FLUSH_SQ;
2864 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2865
2866 if (nesqp->term_flags) {
2867 ibevent.device = nesqp->ibqp.device;
2868 ibevent.event = nesqp->terminate_eventtype;
2869 ibevent.element.qp = &nesqp->ibqp;
Tatyana Nikolova784d1352012-05-11 14:29:32 -05002870 if (nesqp->ibqp.event_handler)
2871 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
Don Woodb29a4fc2009-09-05 20:36:39 -07002872 }
2873 }
2874
2875 if ((cm_id) && (cm_id->event_handler)) {
2876 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002877 atomic_inc(&cm_disconnects);
2878 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002879 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002880 cm_event.local_addr = cm_id->local_addr;
2881 cm_event.remote_addr = cm_id->remote_addr;
2882 cm_event.private_data = NULL;
2883 cm_event.private_data_len = 0;
2884
Faisal Latif6492cdf2008-07-24 20:50:45 -07002885 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302886 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2887 "cm_id = %p, refcount = %u.\n",
2888 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2889 nesqp->hwqp.sq_tail, cm_id,
2890 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002891
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002892 ret = cm_id->event_handler(cm_id, &cm_event);
2893 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002894 nes_debug(NES_DBG_CM, "OFA CM event_handler "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302895 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002896 }
2897
Don Woodb29a4fc2009-09-05 20:36:39 -07002898 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002899 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002900 nes_disconnect(nesqp, 1);
2901
2902 cm_id->provider_data = nesqp;
2903 /* Send up the close complete event */
2904 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002905 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002906 cm_event.provider_data = cm_id->provider_data;
2907 cm_event.local_addr = cm_id->local_addr;
2908 cm_event.remote_addr = cm_id->remote_addr;
2909 cm_event.private_data = NULL;
2910 cm_event.private_data_len = 0;
2911
2912 ret = cm_id->event_handler(cm_id, &cm_event);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302913 if (ret)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002914 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002915
2916 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002917 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002918 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002919
2920 return 0;
2921}
2922
2923
2924/**
2925 * nes_disconnect
2926 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002927static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002928{
2929 int ret = 0;
2930 struct nes_vnic *nesvnic;
2931 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002932 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002933
2934 nesvnic = to_nesvnic(nesqp->ibqp.device);
2935 if (!nesvnic)
2936 return -EINVAL;
2937
2938 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002939 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002940
2941 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00002942 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002943
2944 if (nesqp->active_conn) {
2945
2946 /* indicate this connection is NOT active */
2947 nesqp->active_conn = 0;
2948 } else {
2949 /* Need to free the Last Streaming Mode Message */
2950 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002951 if (nesqp->lsmm_mr)
2952 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002953 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302954 nesqp->private_data_len + nesqp->ietf_frame_size,
2955 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002956 }
2957 }
2958
2959 /* close the CM node down if it is still active */
2960 if (nesqp->cm_node) {
2961 nes_debug(NES_DBG_CM, "Call close API\n");
2962
2963 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002964 }
2965
2966 return ret;
2967}
2968
2969
2970/**
2971 * nes_accept
2972 */
2973int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2974{
2975 u64 u64temp;
2976 struct ib_qp *ibqp;
2977 struct nes_qp *nesqp;
2978 struct nes_vnic *nesvnic;
2979 struct nes_device *nesdev;
2980 struct nes_cm_node *cm_node;
2981 struct nes_adapter *adapter;
2982 struct ib_qp_attr attr;
2983 struct iw_cm_event cm_event;
2984 struct nes_hw_qp_wqe *wqe;
2985 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002986 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002987 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002988 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002989 struct nes_ib_device *nesibdev;
2990 struct ib_mr *ibmr = NULL;
2991 struct ib_phys_buf ibphysbuf;
2992 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002993 u64 tagged_offset;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302994 u8 mpa_frame_offset = 0;
2995 struct ietf_mpa_v2 *mpa_v2_frame;
2996 u8 start_addr = 0;
2997 u8 *start_ptr = &start_addr;
2998 u8 **start_buff = &start_ptr;
2999 u16 buff_len = 0;
Steve Wise24d44a32013-07-04 16:10:44 +05303000 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->local_addr;
3001 struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->remote_addr;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003002
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003003 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3004 if (!ibqp)
3005 return -EINVAL;
3006
3007 /* get all our handles */
3008 nesqp = to_nesqp(ibqp);
3009 nesvnic = to_nesvnic(nesqp->ibqp.device);
3010 nesdev = nesvnic->nesdev;
3011 adapter = nesdev->nesadapter;
3012
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003013 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003014 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
3015 "%s\n", cm_node, nesvnic, nesvnic->netdev,
3016 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003017
Faisal Latifc5a7d482009-12-09 15:54:08 -08003018 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
3019 if (cm_node->loopbackpartner)
3020 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
3021 rem_ref_cm_node(cm_node->cm_core, cm_node);
3022 return -EINVAL;
3023 }
3024
Faisal Latifdae58722010-08-14 21:04:56 +00003025 passive_state = atomic_add_return(1, &cm_node->passive_state);
3026 if (passive_state == NES_SEND_RESET_EVENT) {
3027 rem_ref_cm_node(cm_node->cm_core, cm_node);
3028 return -ECONNRESET;
3029 }
3030
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003031 /* associate the node with the QP */
3032 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003033 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003034
Faisal Latif6492cdf2008-07-24 20:50:45 -07003035 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
3036 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003037 atomic_inc(&cm_accepts);
3038
3039 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00003040 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003041
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303042 nesqp->ietf_frame_size = sizeof(struct ietf_mpa_v2);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003043 /* allocate the ietf frame and space for private data */
3044 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303045 nesqp->ietf_frame_size + conn_param->private_data_len,
3046 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003047
Faisal Latif6492cdf2008-07-24 20:50:45 -07003048 if (!nesqp->ietf_frame) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303049 nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07003050 return -ENOMEM;
3051 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303052 mpa_v2_frame = (struct ietf_mpa_v2 *)nesqp->ietf_frame;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003053
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303054 if (cm_node->mpa_frame_rev == IETF_MPA_V1)
3055 mpa_frame_offset = 4;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003056
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303057 memcpy(mpa_v2_frame->priv_data, conn_param->private_data,
3058 conn_param->private_data_len);
3059
3060 cm_build_mpa_frame(cm_node, start_buff, &buff_len, nesqp->ietf_frame, MPA_KEY_REPLY);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003061 nesqp->private_data_len = conn_param->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003062
Faisal Latif6492cdf2008-07-24 20:50:45 -07003063 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
3064 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003065
Steve Wise24d44a32013-07-04 16:10:44 +05303066 if (raddr->sin_addr.s_addr != laddr->sin_addr.s_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003067 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003068 nesibdev = nesvnic->nesibdev;
3069 nespd = nesqp->nespd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303070 ibphysbuf.addr = nesqp->ietf_frame_pbase + mpa_frame_offset;
3071 ibphysbuf.size = buff_len;
3072 tagged_offset = (u64)(unsigned long)*start_buff;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003073 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303074 &ibphysbuf, 1,
3075 IB_ACCESS_LOCAL_WRITE,
3076 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003077 if (!ibmr) {
3078 nes_debug(NES_DBG_CM, "Unable to register memory region"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303079 "for lSMM for cm_node = %p \n",
3080 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07003081 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303082 nesqp->private_data_len + nesqp->ietf_frame_size,
3083 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003084 return -ENOMEM;
3085 }
3086
3087 ibmr->pd = &nespd->ibpd;
3088 ibmr->device = nespd->ibpd.device;
3089 nesqp->lsmm_mr = ibmr;
3090
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303091 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003092 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303093 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
3094 u64temp);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003095 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3096 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303097 NES_IWARP_SQ_WQE_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003098 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303099 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003100 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303101 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3102 (u64)(unsigned long)(*start_buff));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003103 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303104 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003105 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08003106 if (nesqp->sq_kmapped) {
3107 nesqp->sq_kmapped = 0;
3108 kunmap(nesqp->page);
3109 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003110
Faisal Latif6492cdf2008-07-24 20:50:45 -07003111 nesqp->nesqp_context->ird_ord_sizes |=
3112 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303113 NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003114 } else {
3115 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08003116 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003117 }
3118 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003119
3120
3121 /* Cache the cm_id in the qp */
3122 nesqp->cm_id = cm_id;
3123 cm_node->cm_id = cm_id;
3124
3125 /* nesqp->cm_node = (void *)cm_id->provider_data; */
3126 cm_id->provider_data = nesqp;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303127 nesqp->active_conn = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003128
Faisal Latif6492cdf2008-07-24 20:50:45 -07003129 if (cm_node->state == NES_CM_STATE_TSA)
3130 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303131 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003132
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003133 nes_cm_init_tsa_conn(nesqp, cm_node);
3134
Steve Wise24d44a32013-07-04 16:10:44 +05303135 nesqp->nesqp_context->tcpPorts[0] = cpu_to_le16(ntohs(laddr->sin_port));
3136 nesqp->nesqp_context->tcpPorts[1] = cpu_to_le16(ntohs(raddr->sin_port));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003137
Steve Wise24d44a32013-07-04 16:10:44 +05303138 nesqp->nesqp_context->ip0 = cpu_to_le32(ntohl(raddr->sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003139
3140 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303141 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3142 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003143
Faisal Latif6492cdf2008-07-24 20:50:45 -07003144 nesqp->nesqp_context->arp_index_vlan |=
3145 cpu_to_le32(nes_arp_table(nesdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303146 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
3147 NES_ARP_RESOLVE) << 16);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003148
3149 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003150 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003151
3152 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3153
3154 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003155 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
3156 nesqp->nesqp_context->ird_ord_sizes |=
3157 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003158
3159 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003160 nes_quad.DstIpAdrIndex =
3161 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
Steve Wise24d44a32013-07-04 16:10:44 +05303162 nes_quad.SrcIpadr = raddr->sin_addr.s_addr;
3163 nes_quad.TcpPorts[0] = raddr->sin_port;
3164 nes_quad.TcpPorts[1] = laddr->sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003165
3166 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003167 crc_value = get_crc_value(&nes_quad);
3168 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003169 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303170 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003171
3172 nesqp->hte_index &= adapter->hte_index_mask;
3173 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3174
3175 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3176
Faisal Latif6492cdf2008-07-24 20:50:45 -07003177 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303178 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
3179 "private data length=%u.\n", nesqp->hwqp.qp_id,
Steve Wise24d44a32013-07-04 16:10:44 +05303180 ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port),
3181 ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303182 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
3183 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
3184 buff_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003185
Coly Li73ac36e2009-01-07 18:09:16 -08003186 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003187 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07003188 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003189
3190 cm_event.event = IW_CM_EVENT_ESTABLISHED;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003191 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003192 cm_event.provider_data = (void *)nesqp;
3193 cm_event.local_addr = cm_id->local_addr;
3194 cm_event.remote_addr = cm_id->remote_addr;
3195 cm_event.private_data = NULL;
3196 cm_event.private_data_len = 0;
3197 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003198 attr.qp_state = IB_QPS_RTS;
3199 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003200 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003201 cm_node->loopbackpartner->mpa_frame_size =
3202 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003203 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003204 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303205 conn_param->private_data, conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003206 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
3207 }
3208 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003209 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303210 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003211
3212 return 0;
3213}
3214
3215
3216/**
3217 * nes_reject
3218 */
3219int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3220{
3221 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003222 struct nes_cm_node *loopback;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003223 struct nes_cm_core *cm_core;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303224 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003225
3226 atomic_inc(&cm_rejects);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303227 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003228 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003229 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003230 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003231
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303232 if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003233 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003234
Faisal Latif9d5ab132009-03-06 15:15:01 -08003235 if (loopback) {
3236 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3237 loopback->mpa_frame.priv_data_len = pdata_len;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303238 loopback->mpa_frame_size = pdata_len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003239 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303240 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
3241 cm_node->mpa_frame_size = pdata_len;
3242 memcpy(start_buff, pdata, pdata_len);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003243 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303244 return cm_core->api->reject(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003245}
3246
3247
3248/**
3249 * nes_connect
3250 * setup and launch cm connect node
3251 */
3252int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3253{
3254 struct ib_qp *ibqp;
3255 struct nes_qp *nesqp;
3256 struct nes_vnic *nesvnic;
3257 struct nes_device *nesdev;
3258 struct nes_cm_node *cm_node;
3259 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003260 int apbvt_set = 0;
Steve Wise24d44a32013-07-04 16:10:44 +05303261 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->local_addr;
3262 struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003263
Steve Wise24d44a32013-07-04 16:10:44 +05303264 if (cm_id->remote_addr.ss_family != AF_INET)
3265 return -ENOSYS;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003266 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3267 if (!ibqp)
3268 return -EINVAL;
3269 nesqp = to_nesqp(ibqp);
3270 if (!nesqp)
3271 return -EINVAL;
3272 nesvnic = to_nesvnic(nesqp->ibqp.device);
3273 if (!nesvnic)
3274 return -EINVAL;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303275 nesdev = nesvnic->nesdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003276 if (!nesdev)
3277 return -EINVAL;
3278
Steve Wise24d44a32013-07-04 16:10:44 +05303279 if (!laddr->sin_port || !raddr->sin_port)
Faisal Latifc5a7d482009-12-09 15:54:08 -08003280 return -EINVAL;
3281
Faisal Latif6492cdf2008-07-24 20:50:45 -07003282 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303283 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
Steve Wise24d44a32013-07-04 16:10:44 +05303284 ntohl(nesvnic->local_ipaddr), ntohl(raddr->sin_addr.s_addr),
3285 ntohs(raddr->sin_port), ntohl(laddr->sin_addr.s_addr),
3286 ntohs(laddr->sin_port));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003287
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003288 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003289 nesqp->active_conn = 1;
3290
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003291 /* cache the cm_id in the qp */
3292 nesqp->cm_id = cm_id;
3293
3294 cm_id->provider_data = nesqp;
3295
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003296 nesqp->private_data_len = conn_param->private_data_len;
3297 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
Tatyana Nikolovad3e51322012-05-11 10:46:16 -05003298 /* space for rdma0 read msg */
3299 if (conn_param->ord == 0)
3300 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(1);
3301
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003302 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003303 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303304 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003305
Steve Wise24d44a32013-07-04 16:10:44 +05303306 if (laddr->sin_addr.s_addr != raddr->sin_addr.s_addr) {
3307 nes_manage_apbvt(nesvnic, ntohs(laddr->sin_port),
3308 PCI_FUNC(nesdev->pcidev->devfn),
3309 NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003310 apbvt_set = 1;
3311 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003312
3313 /* set up the connection params for the node */
Steve Wise24d44a32013-07-04 16:10:44 +05303314 cm_info.loc_addr = htonl(laddr->sin_addr.s_addr);
3315 cm_info.loc_port = htons(laddr->sin_port);
3316 cm_info.rem_addr = htonl(raddr->sin_addr.s_addr);
3317 cm_info.rem_port = htons(raddr->sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003318 cm_info.cm_id = cm_id;
3319 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3320
3321 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003322
3323 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003324 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303325 conn_param->private_data_len, (void *)conn_param->private_data,
3326 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003327 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003328 if (apbvt_set)
Steve Wise24d44a32013-07-04 16:10:44 +05303329 nes_manage_apbvt(nesvnic, ntohs(laddr->sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303330 PCI_FUNC(nesdev->pcidev->devfn),
3331 NES_MANAGE_APBVT_DEL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003332
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003333 cm_id->rem_ref(cm_id);
3334 return -ENOMEM;
3335 }
3336
Faisal Latif53094c32009-04-27 13:37:34 -07003337 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003338 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003339 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003340 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003341
3342 return 0;
3343}
3344
3345
3346/**
3347 * nes_create_listen
3348 */
3349int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3350{
3351 struct nes_vnic *nesvnic;
3352 struct nes_cm_listener *cm_node;
3353 struct nes_cm_info cm_info;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003354 int err;
Steve Wise24d44a32013-07-04 16:10:44 +05303355 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->local_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003356
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003357 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303358 cm_id, ntohs(laddr->sin_port));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003359
Steve Wise24d44a32013-07-04 16:10:44 +05303360 if (cm_id->local_addr.ss_family != AF_INET)
3361 return -ENOSYS;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003362 nesvnic = to_nesvnic(cm_id->device);
3363 if (!nesvnic)
3364 return -EINVAL;
Roland Dreier69d51022010-08-04 14:25:40 -07003365
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003366 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3367 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3368
3369 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303370 nesvnic->local_ipaddr, laddr->sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003371
3372 /* setup listen params in our api call struct */
3373 cm_info.loc_addr = nesvnic->local_ipaddr;
Steve Wise24d44a32013-07-04 16:10:44 +05303374 cm_info.loc_port = laddr->sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003375 cm_info.backlog = backlog;
3376 cm_info.cm_id = cm_id;
3377
3378 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3379
3380
3381 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3382 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003383 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303384 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003385 return -ENOMEM;
3386 }
3387
3388 cm_id->provider_data = cm_node;
3389
3390 if (!cm_node->reused_node) {
Steve Wise24d44a32013-07-04 16:10:44 +05303391 err = nes_manage_apbvt(nesvnic, ntohs(laddr->sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303392 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3393 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003394 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003395 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303396 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003397 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3398 return err;
3399 }
Faisal Latif6e10d2e2010-02-12 19:55:03 +00003400 atomic_inc(&cm_listens_created);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003401 }
3402
3403 cm_id->add_ref(cm_id);
3404 cm_id->provider_data = (void *)cm_node;
3405
3406
3407 return 0;
3408}
3409
3410
3411/**
3412 * nes_destroy_listen
3413 */
3414int nes_destroy_listen(struct iw_cm_id *cm_id)
3415{
3416 if (cm_id->provider_data)
3417 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3418 else
3419 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3420
3421 cm_id->rem_ref(cm_id);
3422
3423 return 0;
3424}
3425
3426
3427/**
3428 * nes_cm_recv
3429 */
3430int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3431{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003432 int rc = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303433
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003434 cm_packets_received++;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303435 if ((g_cm_core) && (g_cm_core->api))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003436 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303437 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003438 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303439 " cm is not setup properly.\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003440
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003441 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003442}
3443
3444
3445/**
3446 * nes_cm_start
3447 * Start and init a cm core module
3448 */
3449int nes_cm_start(void)
3450{
3451 nes_debug(NES_DBG_CM, "\n");
3452 /* create the primary CM core, pass this handle to subsequent core inits */
3453 g_cm_core = nes_cm_alloc_core();
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303454 if (g_cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003455 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303456 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003457 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003458}
3459
3460
3461/**
3462 * nes_cm_stop
3463 * stop and dealloc all cm core instances
3464 */
3465int nes_cm_stop(void)
3466{
3467 g_cm_core->api->destroy_cm_core(g_cm_core);
3468 return 0;
3469}
3470
3471
3472/**
3473 * cm_event_connected
3474 * handle a connected event, setup QPs and HW
3475 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003476static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003477{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003478 struct nes_qp *nesqp;
3479 struct nes_vnic *nesvnic;
3480 struct nes_device *nesdev;
3481 struct nes_cm_node *cm_node;
3482 struct nes_adapter *nesadapter;
3483 struct ib_qp_attr attr;
3484 struct iw_cm_id *cm_id;
3485 struct iw_cm_event cm_event;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003486 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003487 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003488 int ret;
Steve Wise24d44a32013-07-04 16:10:44 +05303489 struct sockaddr_in *laddr;
3490 struct sockaddr_in *raddr;
3491 struct sockaddr_in *cm_event_laddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003492
3493 /* get all our handles */
3494 cm_node = event->cm_node;
3495 cm_id = cm_node->cm_id;
3496 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3497 nesqp = (struct nes_qp *)cm_id->provider_data;
3498 nesvnic = to_nesvnic(nesqp->ibqp.device);
3499 nesdev = nesvnic->nesdev;
3500 nesadapter = nesdev->nesadapter;
Steve Wise24d44a32013-07-04 16:10:44 +05303501 laddr = (struct sockaddr_in *)&cm_id->local_addr;
3502 raddr = (struct sockaddr_in *)&cm_id->remote_addr;
3503 cm_event_laddr = (struct sockaddr_in *)&cm_event.local_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003504
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303505 if (nesqp->destroyed)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003506 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003507 atomic_inc(&cm_connecteds);
3508 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303509 " local port 0x%04X. jiffies = %lu.\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303510 nesqp->hwqp.qp_id, ntohl(raddr->sin_addr.s_addr),
3511 ntohs(raddr->sin_port), ntohs(laddr->sin_port), jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003512
3513 nes_cm_init_tsa_conn(nesqp, cm_node);
3514
3515 /* set the QP tsa context */
Steve Wise24d44a32013-07-04 16:10:44 +05303516 nesqp->nesqp_context->tcpPorts[0] = cpu_to_le16(ntohs(laddr->sin_port));
3517 nesqp->nesqp_context->tcpPorts[1] = cpu_to_le16(ntohs(raddr->sin_port));
3518 nesqp->nesqp_context->ip0 = cpu_to_le32(ntohl(raddr->sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003519
3520 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003521 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3522 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003523 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003524 nes_arp_table(nesdev,
3525 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003526 NULL, NES_ARP_RESOLVE) << 16);
3527 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3528 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3529 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3530 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003531 cpu_to_le32((u32)1 <<
3532 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003533
3534 /* Adjust tail for not having a LSMM */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303535 /*nesqp->hwqp.sq_tail = 1;*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003536
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303537 build_rdma0_msg(cm_node, &nesqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003538
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303539 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3540 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003541
3542 memset(&nes_quad, 0, sizeof(nes_quad));
3543
Faisal Latif6492cdf2008-07-24 20:50:45 -07003544 nes_quad.DstIpAdrIndex =
3545 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
Steve Wise24d44a32013-07-04 16:10:44 +05303546 nes_quad.SrcIpadr = raddr->sin_addr.s_addr;
3547 nes_quad.TcpPorts[0] = raddr->sin_port;
3548 nes_quad.TcpPorts[1] = laddr->sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003549
3550 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003551 crc_value = get_crc_value(&nes_quad);
3552 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003553 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303554 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003555
3556 nesqp->hte_index &= nesadapter->hte_index_mask;
3557 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3558
3559 nesqp->ietf_frame = &cm_node->mpa_frame;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303560 nesqp->private_data_len = (u8)cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003561 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3562
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003563 /* notify OF layer we successfully created the requested connection */
3564 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003565 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003566 cm_event.provider_data = cm_id->provider_data;
Steve Wise24d44a32013-07-04 16:10:44 +05303567 cm_event_laddr->sin_family = AF_INET;
3568 cm_event_laddr->sin_port = laddr->sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003569 cm_event.remote_addr = cm_id->remote_addr;
3570
Faisal Latif6492cdf2008-07-24 20:50:45 -07003571 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303572 cm_event.private_data_len = (u8)event->cm_node->mpa_frame_size;
3573 cm_event.ird = cm_node->ird_size;
3574 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003575
Steve Wise24d44a32013-07-04 16:10:44 +05303576 cm_event_laddr->sin_addr.s_addr = event->cm_info.rem_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003577 ret = cm_id->event_handler(cm_id, &cm_event);
3578 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3579
3580 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003581 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303582 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003583 attr.qp_state = IB_QPS_RTS;
3584 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003585
Faisal Latif6492cdf2008-07-24 20:50:45 -07003586 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303587 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003588
3589 return;
3590}
3591
3592
3593/**
3594 * cm_event_connect_error
3595 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003596static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003597{
3598 struct nes_qp *nesqp;
3599 struct iw_cm_id *cm_id;
3600 struct iw_cm_event cm_event;
3601 /* struct nes_cm_info cm_info; */
3602 int ret;
3603
3604 if (!event->cm_node)
3605 return;
3606
3607 cm_id = event->cm_node->cm_id;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303608 if (!cm_id)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003609 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003610
3611 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3612 nesqp = cm_id->provider_data;
3613
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303614 if (!nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003615 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003616
3617 /* notify OF layer about this connection error event */
3618 /* cm_id->rem_ref(cm_id); */
3619 nesqp->cm_id = NULL;
3620 cm_id->provider_data = NULL;
3621 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003622 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003623 cm_event.provider_data = cm_id->provider_data;
3624 cm_event.local_addr = cm_id->local_addr;
3625 cm_event.remote_addr = cm_id->remote_addr;
3626 cm_event.private_data = NULL;
3627 cm_event.private_data_len = 0;
3628
Steve Wise24d44a32013-07-04 16:10:44 +05303629#ifdef CONFIG_INFINIBAND_NES_DEBUG
3630 {
3631 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3632 &cm_event.local_addr;
3633 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3634 &cm_event.remote_addr;
3635 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, remote_addr=%08x\n",
3636 cm_event_laddr->sin_addr.s_addr, cm_event_raddr->sin_addr.s_addr);
3637 }
3638#endif
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003639
3640 ret = cm_id->event_handler(cm_id, &cm_event);
3641 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3642 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003643 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303644 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003645 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003646
Faisal Latif6492cdf2008-07-24 20:50:45 -07003647 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003648 return;
3649}
3650
3651
3652/**
3653 * cm_event_reset
3654 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003655static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003656{
3657 struct nes_qp *nesqp;
3658 struct iw_cm_id *cm_id;
3659 struct iw_cm_event cm_event;
3660 /* struct nes_cm_info cm_info; */
3661 int ret;
3662
3663 if (!event->cm_node)
3664 return;
3665
3666 if (!event->cm_node->cm_id)
3667 return;
3668
3669 cm_id = event->cm_node->cm_id;
3670
3671 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3672 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003673 if (!nesqp)
3674 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003675
3676 nesqp->cm_id = NULL;
3677 /* cm_id->provider_data = NULL; */
3678 cm_event.event = IW_CM_EVENT_DISCONNECT;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003679 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003680 cm_event.provider_data = cm_id->provider_data;
3681 cm_event.local_addr = cm_id->local_addr;
3682 cm_event.remote_addr = cm_id->remote_addr;
3683 cm_event.private_data = NULL;
3684 cm_event.private_data_len = 0;
3685
Faisal Latif183ecfa2008-11-21 20:50:46 -06003686 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003687 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003688 atomic_inc(&cm_closes);
3689 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003690 cm_event.status = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003691 cm_event.provider_data = cm_id->provider_data;
3692 cm_event.local_addr = cm_id->local_addr;
3693 cm_event.remote_addr = cm_id->remote_addr;
3694 cm_event.private_data = NULL;
3695 cm_event.private_data_len = 0;
3696 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3697 ret = cm_id->event_handler(cm_id, &cm_event);
3698
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003699 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3700
3701
3702 /* notify OF layer about this connection error event */
3703 cm_id->rem_ref(cm_id);
3704
3705 return;
3706}
3707
3708
3709/**
3710 * cm_event_mpa_req
3711 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003712static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003713{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303714 struct iw_cm_id *cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003715 struct iw_cm_event cm_event;
3716 int ret;
3717 struct nes_cm_node *cm_node;
Steve Wise24d44a32013-07-04 16:10:44 +05303718 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3719 &cm_event.local_addr;
3720 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3721 &cm_event.remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003722
3723 cm_node = event->cm_node;
3724 if (!cm_node)
3725 return;
3726 cm_id = cm_node->cm_id;
3727
3728 atomic_inc(&cm_connect_reqs);
3729 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303730 cm_node, cm_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003731
3732 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003733 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003734 cm_event.provider_data = (void *)cm_node;
3735
Steve Wise24d44a32013-07-04 16:10:44 +05303736 cm_event_laddr->sin_family = AF_INET;
3737 cm_event_laddr->sin_port = htons(event->cm_info.loc_port);
3738 cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003739
Steve Wise24d44a32013-07-04 16:10:44 +05303740 cm_event_raddr->sin_family = AF_INET;
3741 cm_event_raddr->sin_port = htons(event->cm_info.rem_port);
3742 cm_event_raddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003743 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303744 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
3745 cm_event.ird = cm_node->ird_size;
3746 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003747
3748 ret = cm_id->event_handler(cm_id, &cm_event);
3749 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003750 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303751 __func__, __LINE__, ret);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003752 return;
3753}
3754
3755
3756static void cm_event_mpa_reject(struct nes_cm_event *event)
3757{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303758 struct iw_cm_id *cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003759 struct iw_cm_event cm_event;
3760 struct nes_cm_node *cm_node;
3761 int ret;
Steve Wise24d44a32013-07-04 16:10:44 +05303762 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3763 &cm_event.local_addr;
3764 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3765 &cm_event.remote_addr;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003766
3767 cm_node = event->cm_node;
3768 if (!cm_node)
3769 return;
3770 cm_id = cm_node->cm_id;
3771
3772 atomic_inc(&cm_connect_reqs);
3773 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303774 cm_node, cm_id, jiffies);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003775
3776 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3777 cm_event.status = -ECONNREFUSED;
3778 cm_event.provider_data = cm_id->provider_data;
3779
Steve Wise24d44a32013-07-04 16:10:44 +05303780 cm_event_laddr->sin_family = AF_INET;
3781 cm_event_laddr->sin_port = htons(event->cm_info.loc_port);
3782 cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003783
Steve Wise24d44a32013-07-04 16:10:44 +05303784 cm_event_raddr->sin_family = AF_INET;
3785 cm_event_raddr->sin_port = htons(event->cm_info.rem_port);
3786 cm_event_raddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003787
3788 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303789 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003790
3791 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303792 "remove_addr=%08x\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303793 cm_event_laddr->sin_addr.s_addr,
3794 cm_event_raddr->sin_addr.s_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003795
3796 ret = cm_id->event_handler(cm_id, &cm_event);
3797 if (ret)
3798 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303799 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003800
3801 return;
3802}
3803
3804
3805static void nes_cm_event_handler(struct work_struct *);
3806
3807/**
3808 * nes_cm_post_event
3809 * post an event to the cm event handler
3810 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003811static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003812{
3813 atomic_inc(&event->cm_node->cm_core->events_posted);
3814 add_ref_cm_node(event->cm_node);
3815 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3816 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003817 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303818 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003819
3820 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3821
3822 nes_debug(NES_DBG_CM, "Exit\n");
3823 return 0;
3824}
3825
3826
3827/**
3828 * nes_cm_event_handler
3829 * worker function to handle cm events
3830 * will free instance of nes_cm_event
3831 */
3832static void nes_cm_event_handler(struct work_struct *work)
3833{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003834 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303835 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003836 struct nes_cm_core *cm_core;
3837
Faisal Latif6492cdf2008-07-24 20:50:45 -07003838 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003839 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003840
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003841 cm_core = event->cm_node->cm_core;
3842 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303843 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003844
3845 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003846 case NES_CM_EVENT_MPA_REQ:
3847 cm_event_mpa_req(event);
3848 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303849 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003850 break;
3851 case NES_CM_EVENT_RESET:
3852 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303853 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003854 cm_event_reset(event);
3855 break;
3856 case NES_CM_EVENT_CONNECTED:
3857 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303858 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003859 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003860 cm_event_connected(event);
3861 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3862 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003863 case NES_CM_EVENT_MPA_REJECT:
3864 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303865 (event->cm_node->state == NES_CM_STATE_TSA))
Faisal Latif9d5ab132009-03-06 15:15:01 -08003866 break;
3867 cm_event_mpa_reject(event);
3868 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3869 break;
3870
Faisal Latif6492cdf2008-07-24 20:50:45 -07003871 case NES_CM_EVENT_ABORTED:
3872 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303873 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003874 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003875 cm_event_connect_error(event);
3876 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3877 break;
3878 case NES_CM_EVENT_DROPPED_PKT:
3879 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3880 break;
3881 default:
3882 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3883 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003884 }
3885
3886 atomic_dec(&cm_core->events_posted);
3887 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3888 rem_ref_cm_node(cm_core, event->cm_node);
3889 kfree(event);
3890
3891 return;
3892}