blob: dfa9df484505e6ecdc5362f758ade1627b98d0c7 [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);
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -0500131static void record_ird_ord(struct nes_cm_node *, u16, u16);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800132
133/* External CM API Interface */
134/* instance of function pointers for client API */
135/* set address of this instance to cm_core->cm_ops at cm_core alloc */
136static struct nes_cm_ops nes_cm_api = {
137 mini_cm_accelerated,
138 mini_cm_listen,
139 mini_cm_del_listen,
140 mini_cm_connect,
141 mini_cm_close,
142 mini_cm_accept,
143 mini_cm_reject,
144 mini_cm_recv_pkt,
145 mini_cm_dealloc_core,
146 mini_cm_get,
147 mini_cm_set
148};
149
Roland Dreier1a855fbf2008-04-16 21:01:09 -0700150static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800151
152atomic_t cm_connects;
153atomic_t cm_accepts;
154atomic_t cm_disconnects;
155atomic_t cm_closes;
156atomic_t cm_connecteds;
157atomic_t cm_connect_reqs;
158atomic_t cm_rejects;
159
Faisal Latif0f0bee82011-09-25 20:34:00 -0500160int nes_add_ref_cm_node(struct nes_cm_node *cm_node)
161{
162 return add_ref_cm_node(cm_node);
163}
164
165int nes_rem_ref_cm_node(struct nes_cm_node *cm_node)
166{
167 return rem_ref_cm_node(cm_node->cm_core, cm_node);
168}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800169
170/**
171 * create_event
172 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530173static struct nes_cm_event *create_event(struct nes_cm_node * cm_node,
174 enum nes_cm_event_type type)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800175{
176 struct nes_cm_event *event;
177
178 if (!cm_node->cm_id)
179 return NULL;
180
181 /* allocate an empty event */
182 event = kzalloc(sizeof(*event), GFP_ATOMIC);
183
184 if (!event)
185 return NULL;
186
187 event->type = type;
188 event->cm_node = cm_node;
189 event->cm_info.rem_addr = cm_node->rem_addr;
190 event->cm_info.loc_addr = cm_node->loc_addr;
191 event->cm_info.rem_port = cm_node->rem_port;
192 event->cm_info.loc_port = cm_node->loc_port;
193 event->cm_info.cm_id = cm_node->cm_id;
194
Faisal Latif6492cdf2008-07-24 20:50:45 -0700195 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530196 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
197 cm_node, event, type, event->cm_info.loc_addr,
198 event->cm_info.loc_port, event->cm_info.rem_addr,
199 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800200
201 nes_cm_post_event(event);
202 return event;
203}
204
205
206/**
207 * send_mpa_request
208 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700209static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800210{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530211 u8 start_addr = 0;
212 u8 *start_ptr = &start_addr;
213 u8 **start_buff = &start_ptr;
214 u16 buff_len = 0;
215
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800216 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700217 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800218 return -1;
219 }
220
221 /* send an MPA Request frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530222 cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REQUEST);
223 form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800224
Faisal Latif9d5ab132009-03-06 15:15:01 -0800225 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
226}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800227
Faisal Latif9d5ab132009-03-06 15:15:01 -0800228
229
230static int send_mpa_reject(struct nes_cm_node *cm_node)
231{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530232 struct sk_buff *skb = NULL;
233 u8 start_addr = 0;
234 u8 *start_ptr = &start_addr;
235 u8 **start_buff = &start_ptr;
236 u16 buff_len = 0;
Tatyana Nikolova81f99dc2012-01-17 10:17:30 -0600237 struct ietf_mpa_v1 *mpa_frame;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800238
239 skb = dev_alloc_skb(MAX_CM_BUFFER);
240 if (!skb) {
241 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
242 return -ENOMEM;
243 }
244
245 /* send an MPA reject frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530246 cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REPLY);
Tatyana Nikolova81f99dc2012-01-17 10:17:30 -0600247 mpa_frame = (struct ietf_mpa_v1 *)*start_buff;
248 mpa_frame->flags |= IETF_MPA_FLAGS_REJECT;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530249 form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK | SET_FIN);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800250
251 cm_node->state = NES_CM_STATE_FIN_WAIT1;
252 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800253}
254
255
256/**
257 * recv_mpa - process a received TCP pkt, we are expecting an
258 * IETF MPA frame
259 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800260static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530261 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800262{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530263 struct ietf_mpa_v1 *mpa_frame;
264 struct ietf_mpa_v2 *mpa_v2_frame;
265 struct ietf_rtr_msg *rtr_msg;
266 int mpa_hdr_len;
267 int priv_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800268
Faisal Latif9d5ab132009-03-06 15:15:01 -0800269 *type = NES_MPA_REQUEST_ACCEPT;
270
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800271 /* assume req frame is in tcp data payload */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530272 if (len < sizeof(struct ietf_mpa_v1)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800273 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800274 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800275 }
276
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530277 /* points to the beginning of the frame, which could be MPA V1 or V2 */
278 mpa_frame = (struct ietf_mpa_v1 *)buffer;
279 mpa_hdr_len = sizeof(struct ietf_mpa_v1);
280 priv_data_len = ntohs(mpa_frame->priv_data_len);
281
Faisal Latif1cf078c2009-12-09 15:53:54 -0800282 /* make sure mpa private data len is less than 512 bytes */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530283 if (priv_data_len > IETF_MAX_PRIV_DATA_LEN) {
Faisal Latif1cf078c2009-12-09 15:53:54 -0800284 nes_debug(NES_DBG_CM, "The received Length of Private"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530285 " Data field exceeds 512 octets\n");
Faisal Latif1cf078c2009-12-09 15:53:54 -0800286 return -EINVAL;
287 }
288 /*
289 * make sure MPA receiver interoperate with the
290 * received MPA version and MPA key information
291 *
292 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530293 if (mpa_frame->rev != IETF_MPA_V1 && mpa_frame->rev != IETF_MPA_V2) {
Faisal Latif1cf078c2009-12-09 15:53:54 -0800294 nes_debug(NES_DBG_CM, "The received mpa version"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530295 " is not supported\n");
Faisal Latif1cf078c2009-12-09 15:53:54 -0800296 return -EINVAL;
297 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530298 /*
299 * backwards compatibility only
300 */
301 if (mpa_frame->rev > cm_node->mpa_frame_rev) {
302 nes_debug(NES_DBG_CM, "The received mpa version"
303 " can not be interoperated\n");
304 return -EINVAL;
305 } else {
306 cm_node->mpa_frame_rev = mpa_frame->rev;
307 }
308
Faisal Latif1cf078c2009-12-09 15:53:54 -0800309 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
310 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
311 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
312 return -EINVAL;
313 }
314 } else {
315 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
316 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
317 return -EINVAL;
318 }
319 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800320
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530321 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 }
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -0500359 if (ird_size == IETF_NO_IRD_ORD || ord_size == IETF_NO_IRD_ORD)
360 cm_node->mpav2_ird_ord = IETF_NO_IRD_ORD;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530361
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -0500362 if (cm_node->mpav2_ird_ord != IETF_NO_IRD_ORD) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530363 /* responder */
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -0500364 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
365 /* we are still negotiating */
366 if (ord_size > NES_MAX_IRD) {
367 cm_node->ird_size = NES_MAX_IRD;
368 } else {
369 cm_node->ird_size = ord_size;
370 if (ord_size == 0 &&
371 (rtr_ctrl_ord & IETF_RDMA0_READ)) {
372 cm_node->ird_size = 1;
373 nes_debug(NES_DBG_CM,
374 "%s: Remote peer doesn't support RDMA0_READ (ord=%u)\n",
375 __func__, ord_size);
376 }
377 }
378 if (ird_size > NES_MAX_ORD)
379 cm_node->ord_size = NES_MAX_ORD;
380 else
381 cm_node->ord_size = ird_size;
382 } else { /* initiator */
383 if (ord_size > NES_MAX_IRD) {
384 nes_debug(NES_DBG_CM,
385 "%s: Unable to support the requested (ord =%u)\n",
386 __func__, ord_size);
387 return -EINVAL;
388 }
389 cm_node->ird_size = ord_size;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530390
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -0500391 if (ird_size > NES_MAX_ORD) {
392 cm_node->ord_size = NES_MAX_ORD;
393 } else {
394 if (ird_size == 0 &&
395 (rtr_ctrl_ord & IETF_RDMA0_READ)) {
396 nes_debug(NES_DBG_CM,
397 "%s: Remote peer doesn't support RDMA0_READ (ird=%u)\n",
398 __func__, ird_size);
399 return -EINVAL;
400 } else {
401 cm_node->ord_size = ird_size;
402 }
403 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530404 }
405 }
406
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800407 if (rtr_ctrl_ord & IETF_RDMA0_READ) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530408 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -0500409
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800410 } else if (rtr_ctrl_ord & IETF_RDMA0_WRITE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530411 cm_node->send_rdma0_op = SEND_RDMA_WRITE_ZERO;
412 } else { /* Not supported RDMA0 operation */
413 return -EINVAL;
414 }
415 break;
416 }
417 case IETF_MPA_V1:
418 default:
419 break;
420 }
421
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800422 /* copy entire MPA frame to our cm_node's frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530423 memcpy(cm_node->mpa_frame_buf, buffer + mpa_hdr_len, cm_node->mpa_frame_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800424
Faisal Latif9d5ab132009-03-06 15:15:01 -0800425 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
426 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800427 return 0;
428}
429
430
431/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800432 * form_cm_frame - get a free packet and build empty frame Use
433 * node info to build.
434 */
Chien Tung6098d102008-11-21 20:51:01 -0600435static void form_cm_frame(struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530436 struct nes_cm_node *cm_node, void *options, u32 optionsize,
437 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800438{
439 struct tcphdr *tcph;
440 struct iphdr *iph;
441 struct ethhdr *ethh;
442 u8 *buf;
443 u16 packetsize = sizeof(*iph);
444
445 packetsize += sizeof(*tcph);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530446 packetsize += optionsize + datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800447
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530448 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800449 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
450
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800451 buf = skb_put(skb, packetsize + ETH_HLEN);
452
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530453 ethh = (struct ethhdr *)buf;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800454 buf += ETH_HLEN;
455
456 iph = (struct iphdr *)buf;
457 buf += sizeof(*iph);
458 tcph = (struct tcphdr *)buf;
459 skb_reset_mac_header(skb);
460 skb_set_network_header(skb, ETH_HLEN);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530461 skb_set_transport_header(skb, ETH_HLEN + sizeof(*iph));
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800462 buf += sizeof(*tcph);
463
464 skb->ip_summed = CHECKSUM_PARTIAL;
Tatyana Nikolovafc4ba722012-09-20 20:55:33 +0000465 if (!(cm_node->netdev->features & NETIF_F_IP_CSUM))
466 skb->ip_summed = CHECKSUM_NONE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800467 skb->protocol = htons(0x800);
468 skb->data_len = 0;
469 skb->mac_len = ETH_HLEN;
470
471 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
472 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
473 ethh->h_proto = htons(0x0800);
474
475 iph->version = IPVERSION;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530476 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800477 iph->tos = 0;
478 iph->tot_len = htons(packetsize);
479 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
480
481 iph->frag_off = htons(0x4000);
482 iph->ttl = 0x40;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530483 iph->protocol = 0x06; /* IPPROTO_TCP */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800484
485 iph->saddr = htonl(cm_node->loc_addr);
486 iph->daddr = htonl(cm_node->rem_addr);
487
488 tcph->source = htons(cm_node->loc_port);
489 tcph->dest = htons(cm_node->rem_port);
490 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
491
492 if (flags & SET_ACK) {
493 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
494 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
495 tcph->ack = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530496 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800497 tcph->ack_seq = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530498 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800499
500 if (flags & SET_SYN) {
501 cm_node->tcp_cntxt.loc_seq_num++;
502 tcph->syn = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530503 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700504 cm_node->tcp_cntxt.loc_seq_num += datasize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530505 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800506
Faisal Latif6492cdf2008-07-24 20:50:45 -0700507 if (flags & SET_FIN) {
508 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800509 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700510 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800511
512 if (flags & SET_RST)
513 tcph->rst = 1;
514
515 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
516 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
517 tcph->urg_ptr = 0;
518 if (optionsize)
519 memcpy(buf, options, optionsize);
520 buf += optionsize;
521 if (datasize)
522 memcpy(buf, data, datasize);
523
524 skb_shinfo(skb)->nr_frags = 0;
525 cm_packets_created++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800526}
527
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800528/**
529 * print_core - dump a cm core
530 */
531static void print_core(struct nes_cm_core *core)
532{
533 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
534 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
535 if (!core)
536 return;
537 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800538
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530539 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800540
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800541 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
542 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
543
544 nes_debug(NES_DBG_CM, "core : %p \n", core);
545
546 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
547}
548
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -0500549static void record_ird_ord(struct nes_cm_node *cm_node,
550 u16 conn_ird, u16 conn_ord)
551{
552 if (conn_ird > NES_MAX_IRD)
553 conn_ird = NES_MAX_IRD;
554
555 if (conn_ord > NES_MAX_ORD)
556 conn_ord = NES_MAX_ORD;
557
558 cm_node->ird_size = conn_ird;
559 cm_node->ord_size = conn_ord;
560}
561
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530562/**
563 * cm_build_mpa_frame - build a MPA V1 frame or MPA V2 frame
564 */
565static int cm_build_mpa_frame(struct nes_cm_node *cm_node, u8 **start_buff,
566 u16 *buff_len, u8 *pci_mem, u8 mpa_key)
567{
568 int ret = 0;
569
570 *start_buff = (pci_mem) ? pci_mem : &cm_node->mpa_frame_buf[0];
571
572 switch (cm_node->mpa_frame_rev) {
573 case IETF_MPA_V1:
574 *start_buff = (u8 *)*start_buff + sizeof(struct ietf_rtr_msg);
575 *buff_len = sizeof(struct ietf_mpa_v1) + cm_node->mpa_frame_size;
576 build_mpa_v1(cm_node, *start_buff, mpa_key);
577 break;
578 case IETF_MPA_V2:
579 *buff_len = sizeof(struct ietf_mpa_v2) + cm_node->mpa_frame_size;
580 build_mpa_v2(cm_node, *start_buff, mpa_key);
581 break;
582 default:
583 ret = -EINVAL;
584 }
585 return ret;
586}
587
588/**
589 * build_mpa_v2 - build a MPA V2 frame
590 */
591static void build_mpa_v2(struct nes_cm_node *cm_node,
592 void *start_addr, u8 mpa_key)
593{
594 struct ietf_mpa_v2 *mpa_frame = (struct ietf_mpa_v2 *)start_addr;
595 struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800596 u16 ctrl_ird;
597 u16 ctrl_ord;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530598
599 /* initialize the upper 5 bytes of the frame */
600 build_mpa_v1(cm_node, start_addr, mpa_key);
601 mpa_frame->flags |= IETF_MPA_V2_FLAG; /* set a bit to indicate MPA V2 */
602 mpa_frame->priv_data_len += htons(IETF_RTR_MSG_SIZE);
603
604 /* initialize RTR msg */
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -0500605 if (cm_node->mpav2_ird_ord == IETF_NO_IRD_ORD) {
606 ctrl_ird = IETF_NO_IRD_ORD;
607 ctrl_ord = IETF_NO_IRD_ORD;
608 } else {
609 ctrl_ird = cm_node->ird_size & IETF_NO_IRD_ORD;
610 ctrl_ord = cm_node->ord_size & IETF_NO_IRD_ORD;
611 }
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800612 ctrl_ird |= IETF_PEER_TO_PEER;
613 ctrl_ird |= IETF_FLPDU_ZERO_LEN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530614
615 switch (mpa_key) {
616 case MPA_KEY_REQUEST:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800617 ctrl_ord |= IETF_RDMA0_WRITE;
618 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530619 break;
620 case MPA_KEY_REPLY:
621 switch (cm_node->send_rdma0_op) {
622 case SEND_RDMA_WRITE_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800623 ctrl_ord |= IETF_RDMA0_WRITE;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530624 break;
625 case SEND_RDMA_READ_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800626 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530627 break;
628 }
629 }
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800630 rtr_msg->ctrl_ird = htons(ctrl_ird);
631 rtr_msg->ctrl_ord = htons(ctrl_ord);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530632}
633
634/**
635 * build_mpa_v1 - build a MPA V1 frame
636 */
637static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
638{
639 struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
640
641 switch (mpa_key) {
642 case MPA_KEY_REQUEST:
643 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
644 break;
645 case MPA_KEY_REPLY:
646 memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
647 break;
648 }
649 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
650 mpa_frame->rev = cm_node->mpa_frame_rev;
651 mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
652}
653
654static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
655{
656 u64 u64temp;
657 struct nes_qp *nesqp = *nesqp_addr;
658 struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
659
Tatyana Nikolova43adff32014-03-11 14:20:17 -0500660 u64temp = (unsigned long)nesqp->nesuqp_addr;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530661 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
662 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
663
664 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
665 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
666
667 switch (cm_node->send_rdma0_op) {
668 case SEND_RDMA_WRITE_ZERO:
669 nes_debug(NES_DBG_CM, "Sending first write.\n");
670 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
671 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
672 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
673 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
674 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
675 break;
676
677 case SEND_RDMA_READ_ZERO:
678 default:
Julia Lawall079abea2012-11-03 10:58:37 +0000679 if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO)
680 WARN(1, "Unsupported RDMA0 len operation=%u\n",
681 cm_node->send_rdma0_op);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530682 nes_debug(NES_DBG_CM, "Sending first rdma operation.\n");
683 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
684 cpu_to_le32(NES_IWARP_SQ_OP_RDMAR);
685 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX] = 1;
686 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_HIGH_IDX] = 0;
687 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] = 0;
688 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_STAG_IDX] = 1;
689 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 1;
690 break;
691 }
692
693 if (nesqp->sq_kmapped) {
694 nesqp->sq_kmapped = 0;
695 kunmap(nesqp->page);
696 }
697
698 /*use the reserved spot on the WQ for the extra first WQE*/
699 nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
700 NES_QPCONTEXT_ORDIRD_WRPDU |
701 NES_QPCONTEXT_ORDIRD_ALSMM));
702 nesqp->skip_lsmm = 1;
703 nesqp->hwqp.sq_tail = 0;
704}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800705
706/**
707 * schedule_nes_timer
708 * note - cm_node needs to be protected before calling this. Encase in:
709 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
710 */
711int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530712 enum nes_timer_type type, int send_retrans,
713 int close_when_complete)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800714{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530715 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700716 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800717 struct nes_timer_entry *new_send;
718 int ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800719
720 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
721 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800722 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800723
724 /* new_send->timetosend = currenttime */
725 new_send->retrycount = NES_DEFAULT_RETRYS;
726 new_send->retranscount = NES_DEFAULT_RETRANS;
727 new_send->skb = skb;
728 new_send->timetosend = jiffies;
729 new_send->type = type;
730 new_send->netdev = cm_node->netdev;
731 new_send->send_retrans = send_retrans;
732 new_send->close_when_complete = close_when_complete;
733
734 if (type == NES_TIMER_TYPE_CLOSE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530735 new_send->timetosend += (HZ / 10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800736 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700737 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800738 WARN_ON(1);
739 return -EINVAL;
740 }
741 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800742 }
743
744 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700745 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800746 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700747 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
748 cm_node->send_entry = new_send;
749 add_ref_cm_node(cm_node);
750 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
751 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800752
753 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
754 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700755 nes_debug(NES_DBG_CM, "Error sending packet %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530756 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800757 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700758 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800759 } else {
760 cm_packets_sent++;
761 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700762 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800763 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700764 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800765 return ret;
766 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800767 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800768 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800769
Tatyana Nikolova00ad2552012-12-06 19:56:31 +0000770 if (!timer_pending(&cm_core->tcp_timer))
771 mod_timer(&cm_core->tcp_timer, new_send->timetosend);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800772
773 return ret;
774}
775
Faisal Latif9d5ab132009-03-06 15:15:01 -0800776static void nes_retrans_expired(struct nes_cm_node *cm_node)
777{
Faisal Latif68237a02009-06-22 22:53:28 -0700778 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latifdae58722010-08-14 21:04:56 +0000779 enum nes_cm_node_state state = cm_node->state;
780 cm_node->state = NES_CM_STATE_CLOSED;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530781
Faisal Latifdae58722010-08-14 21:04:56 +0000782 switch (state) {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800783 case NES_CM_STATE_SYN_RCVD:
784 case NES_CM_STATE_CLOSING:
785 rem_ref_cm_node(cm_node->cm_core, cm_node);
786 break;
787 case NES_CM_STATE_LAST_ACK:
788 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700789 if (cm_node->cm_id)
790 cm_id->rem_ref(cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800791 send_reset(cm_node, NULL);
792 break;
793 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800794 add_ref_cm_node(cm_node);
795 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800796 create_event(cm_node, NES_CM_EVENT_ABORTED);
797 }
798}
799
800static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
801{
802 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
803 struct iw_cm_id *cm_id = cm_node->cm_id;
804 struct nes_qp *nesqp;
805 unsigned long qplockflags;
806
807 if (!recv_entry)
808 return;
809 nesqp = (struct nes_qp *)recv_entry->skb;
810 if (nesqp) {
811 spin_lock_irqsave(&nesqp->lock, qplockflags);
812 if (nesqp->cm_id) {
813 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530814 "refcount = %d: HIT A "
815 "NES_TIMER_TYPE_CLOSE with something "
816 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
817 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800818 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
819 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
820 nesqp->ibqp_state = IB_QPS_ERR;
821 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
822 nes_cm_disconn(nesqp);
823 } else {
824 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
825 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530826 "refcount = %d: HIT A "
827 "NES_TIMER_TYPE_CLOSE with nothing "
828 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
829 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800830 }
831 } else if (rem_node) {
832 /* TIME_WAIT state */
833 rem_ref_cm_node(cm_node->cm_core, cm_node);
834 }
835 if (cm_node->cm_id)
836 cm_id->rem_ref(cm_id);
837 kfree(recv_entry);
838 cm_node->recv_entry = NULL;
839}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800840
841/**
842 * nes_cm_timer_tick
843 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -0700844static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800845{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800846 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800847 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800848 struct nes_cm_node *cm_node;
849 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800850 struct list_head *list_core_temp;
851 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800852 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800853 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700854 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800855 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800856
Faisal Latif879e5bd2008-11-21 20:50:41 -0600857 struct list_head timer_list;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530858
Faisal Latif879e5bd2008-11-21 20:50:41 -0600859 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800860 spin_lock_irqsave(&cm_core->ht_lock, flags);
861
Faisal Latif6492cdf2008-07-24 20:50:45 -0700862 list_for_each_safe(list_node, list_core_temp,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530863 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800864 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800865 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600866 add_ref_cm_node(cm_node);
867 list_add(&cm_node->timer_entry, &timer_list);
868 }
869 }
870 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
871
872 list_for_each_safe(list_node, list_core_temp, &timer_list) {
873 cm_node = container_of(list_node, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530874 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800875 recv_entry = cm_node->recv_entry;
876
877 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700878 if (time_after(recv_entry->timetosend, jiffies)) {
879 if (nexttimeout > recv_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530880 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800881 nexttimeout = recv_entry->timetosend;
882 settimer = 1;
883 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530884 } else {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800885 handle_recv_entry(cm_node, 1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530886 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800887 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800888
889 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700890 do {
891 send_entry = cm_node->send_entry;
892 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600893 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800894 if (time_after(send_entry->timetosend, jiffies)) {
895 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700896 if ((nexttimeout >
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530897 send_entry->timetosend) ||
898 !settimer) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700899 nexttimeout =
900 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800901 settimer = 1;
902 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800903 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700904 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800905 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800906 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800907 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700908
909 if ((cm_node->state == NES_CM_STATE_TSA) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530910 (cm_node->state == NES_CM_STATE_CLOSED)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700911 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600912 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800913 }
914
Faisal Latif6492cdf2008-07-24 20:50:45 -0700915 if (!send_entry->retranscount ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530916 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800917 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700918 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800919
Faisal Latif6492cdf2008-07-24 20:50:45 -0700920 spin_unlock_irqrestore(
921 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800922 nes_retrans_expired(cm_node);
923 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700924 spin_lock_irqsave(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530925 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600926 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800927 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800928 atomic_inc(&send_entry->skb->users);
929 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700930 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530931 "for node %p, jiffies = %lu, time to send = "
932 "%lu, retranscount = %u, send_entry->seq_num = "
933 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
934 "0x%08X\n", send_entry, cm_node, jiffies,
935 send_entry->timetosend,
936 send_entry->retranscount,
937 send_entry->seq_num,
938 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800939
Faisal Latif6492cdf2008-07-24 20:50:45 -0700940 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530941 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800942 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700943 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800944 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700945 nes_debug(NES_DBG_CM, "rexmit failed for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530946 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800947 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800948 send_entry->retrycount--;
949 nexttimeout = jiffies + NES_SHORT_TIME;
950 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600951 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800952 } else {
953 cm_packets_sent++;
954 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700955 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530956 "%u, retry count = %u.\n",
957 send_entry->retranscount,
958 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800959 if (send_entry->send_retrans) {
960 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700961 timetosend = (NES_RETRY_TIMEOUT <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530962 (NES_DEFAULT_RETRANS - send_entry->retranscount));
Faisal Latif4e9c3902009-04-27 13:39:36 -0700963
Faisal Latif6492cdf2008-07-24 20:50:45 -0700964 send_entry->timetosend = jiffies +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530965 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700966 if (nexttimeout > send_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530967 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800968 nexttimeout = send_entry->timetosend;
969 settimer = 1;
970 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800971 } else {
972 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700973 close_when_complete =
974 send_entry->close_when_complete;
975 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530976 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700977 free_retrans_entry(cm_node);
978 if (close_when_complete)
979 rem_ref_cm_node(cm_node->cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530980 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800981 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700982 } while (0);
983
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800984 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700985 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800986 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800987
988 if (settimer) {
Tatyana Nikolova00ad2552012-12-06 19:56:31 +0000989 if (!timer_pending(&cm_core->tcp_timer))
990 mod_timer(&cm_core->tcp_timer, nexttimeout);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800991 }
992}
993
994
995/**
996 * send_syn
997 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700998static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530999 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001000{
1001 int ret;
1002 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001003 char optionsbuffer[sizeof(struct option_mss) +
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301004 sizeof(struct option_windowscale) + sizeof(struct option_base) +
1005 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001006
1007 int optionssize = 0;
1008 /* Sending MSS option */
1009 union all_known_options *options;
1010
1011 if (!cm_node)
1012 return -EINVAL;
1013
1014 options = (union all_known_options *)&optionsbuffer[optionssize];
1015 options->as_mss.optionnum = OPTION_NUMBER_MSS;
1016 options->as_mss.length = sizeof(struct option_mss);
1017 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
1018 optionssize += sizeof(struct option_mss);
1019
1020 options = (union all_known_options *)&optionsbuffer[optionssize];
1021 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
1022 options->as_windowscale.length = sizeof(struct option_windowscale);
1023 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
1024 optionssize += sizeof(struct option_windowscale);
1025
Faisal Latif6492cdf2008-07-24 20:50:45 -07001026 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001027 options = (union all_known_options *)&optionsbuffer[optionssize];
1028 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
1029 options->as_base.length = sizeof(struct option_base);
1030 optionssize += sizeof(struct option_base);
1031 /* we need the size to be a multiple of 4 */
1032 options = (union all_known_options *)&optionsbuffer[optionssize];
1033 options->as_end = 1;
1034 optionssize += 1;
1035 options = (union all_known_options *)&optionsbuffer[optionssize];
1036 options->as_end = 1;
1037 optionssize += 1;
1038 }
1039
1040 options = (union all_known_options *)&optionsbuffer[optionssize];
1041 options->as_end = OPTION_NUMBER_END;
1042 optionssize += 1;
1043
Faisal Latif6492cdf2008-07-24 20:50:45 -07001044 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001045 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001046 if (!skb) {
1047 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1048 return -1;
1049 }
1050
1051 if (sendack)
1052 flags |= SET_ACK;
1053
1054 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
1055 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1056
1057 return ret;
1058}
1059
1060
1061/**
1062 * send_reset
1063 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001064static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001065{
1066 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001067 int flags = SET_RST | SET_ACK;
1068
Faisal Latif6492cdf2008-07-24 20:50:45 -07001069 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001070 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001071 if (!skb) {
1072 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001073 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001074 }
1075
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001076 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
1077 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
1078
1079 return ret;
1080}
1081
1082
1083/**
1084 * send_ack
1085 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001086static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001087{
1088 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001089
1090 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001091 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001092
1093 if (!skb) {
1094 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1095 return -1;
1096 }
1097
1098 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
1099 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
1100
1101 return ret;
1102}
1103
1104
1105/**
1106 * send_fin
1107 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07001108static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001109{
1110 int ret;
1111
1112 /* if we didn't get a frame get one */
1113 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001114 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001115
1116 if (!skb) {
1117 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1118 return -1;
1119 }
1120
1121 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
1122 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1123
1124 return ret;
1125}
1126
1127
1128/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001129 * find_node - find a cm node that matches the reference cm node
1130 */
1131static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301132 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001133{
1134 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001135 struct list_head *hte;
1136 struct nes_cm_node *cm_node;
1137
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001138 /* get a handle on the hte */
1139 hte = &cm_core->connected_nodes;
1140
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001141 /* walk list and find cm_node associated with this session ID */
1142 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001143 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001144 /* compare quad, return node handle if a match */
1145 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301146 cm_node->loc_addr, cm_node->loc_port,
1147 loc_addr, loc_port,
1148 cm_node->rem_addr, cm_node->rem_port,
1149 rem_addr, rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001150 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301151 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001152 add_ref_cm_node(cm_node);
1153 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1154 return cm_node;
1155 }
1156 }
1157 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1158
1159 /* no owner node */
1160 return NULL;
1161}
1162
1163
1164/**
1165 * find_listener - find a cm node listening on this addr-port pair
1166 */
1167static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301168 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001169{
1170 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001171 struct nes_cm_listener *listen_node;
1172
1173 /* walk list and find cm_node associated with this session ID */
1174 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001175 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001176 /* compare node pair, return node handle if a match */
1177 if (((listen_node->loc_addr == dst_addr) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301178 listen_node->loc_addr == 0x00000000) &&
1179 (listen_node->loc_port == dst_port) &&
1180 (listener_state & listen_node->listener_state)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001181 atomic_inc(&listen_node->ref_count);
1182 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1183 return listen_node;
1184 }
1185 }
1186 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1187
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001188 /* no listener */
1189 return NULL;
1190}
1191
1192
1193/**
1194 * add_hte_node - add a cm node to the hash table
1195 */
1196static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1197{
1198 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001199 struct list_head *hte;
1200
1201 if (!cm_node || !cm_core)
1202 return -EINVAL;
1203
Faisal Latif6492cdf2008-07-24 20:50:45 -07001204 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301205 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001206
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001207 spin_lock_irqsave(&cm_core->ht_lock, flags);
1208
1209 /* get a handle on the hash table element (list head for this slot) */
1210 hte = &cm_core->connected_nodes;
1211 list_add_tail(&cm_node->list, hte);
1212 atomic_inc(&cm_core->ht_node_cnt);
1213
1214 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1215
1216 return 0;
1217}
1218
1219
1220/**
1221 * mini_cm_dec_refcnt_listen
1222 */
1223static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301224 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001225{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001226 int ret = -EINVAL;
1227 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001228 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001229 struct list_head *list_pos = NULL;
1230 struct list_head *list_temp = NULL;
1231 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -06001232 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001233
1234 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301235 "refcnt=%d\n", listener, free_hanging_nodes,
1236 atomic_read(&listener->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001237 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -06001238 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001239 if (free_hanging_nodes) {
1240 spin_lock_irqsave(&cm_core->ht_lock, flags);
1241 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -06001242 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001243 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301244 list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001245 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -06001246 (!cm_node->accelerated)) {
1247 add_ref_cm_node(cm_node);
1248 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001249 }
1250 }
1251 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1252 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001253
1254 list_for_each_safe(list_pos, list_temp, &reset_list) {
1255 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301256 reset_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001257 {
1258 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -08001259 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001260 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
1261 rem_ref_cm_node(cm_node->cm_core, cm_node);
1262 } else {
1263 if (!loopback) {
1264 cleanup_retrans_entry(cm_node);
1265 err = send_reset(cm_node, NULL);
1266 if (err) {
1267 cm_node->state =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301268 NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001269 WARN_ON(1);
1270 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08001271 old_state = cm_node->state;
1272 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1273 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1274 rem_ref_cm_node(
1275 cm_node->cm_core,
1276 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001277 }
1278 } else {
1279 struct nes_cm_event event;
1280
1281 event.cm_node = loopback;
1282 event.cm_info.rem_addr =
1283 loopback->rem_addr;
1284 event.cm_info.loc_addr =
1285 loopback->loc_addr;
1286 event.cm_info.rem_port =
1287 loopback->rem_port;
1288 event.cm_info.loc_port =
1289 loopback->loc_port;
1290 event.cm_info.cm_id = loopback->cm_id;
Faisal Latif43093b92010-02-12 19:57:04 +00001291 add_ref_cm_node(loopback);
1292 loopback->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001293 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001294 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001295
Faisal Latif9d5ab132009-03-06 15:15:01 -08001296 rem_ref_cm_node(cm_node->cm_core,
1297 cm_node);
1298
1299 }
1300 }
1301 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001302 }
1303
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001304 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1305 if (!atomic_dec_return(&listener->ref_count)) {
1306 list_del(&listener->list);
1307
1308 /* decrement our listen node count */
1309 atomic_dec(&cm_core->listen_node_cnt);
1310
1311 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1312
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301313 if (listener->nesvnic)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001314 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301315 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001316
1317 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1318
1319 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001320 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001321 ret = 0;
Faisal Latif6e10d2e2010-02-12 19:55:03 +00001322 atomic_inc(&cm_listens_destroyed);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001323 } else {
1324 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1325 }
1326 if (listener) {
1327 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1328 nes_debug(NES_DBG_CM, "destroying listener (%p)"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301329 " with non-zero pending accepts=%u\n",
1330 listener, atomic_read(&listener->pend_accepts_cnt));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001331 }
1332
1333 return ret;
1334}
1335
1336
1337/**
1338 * mini_cm_del_listen
1339 */
1340static int mini_cm_del_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301341 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001342{
1343 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1344 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1345 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1346}
1347
1348
1349/**
1350 * mini_cm_accelerated
1351 */
1352static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301353 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001354{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001355 cm_node->accelerated = 1;
1356
1357 if (cm_node->accept_pend) {
1358 BUG_ON(!cm_node->listener);
1359 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001360 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001361 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1362 }
1363
Tatyana Nikolova00ad2552012-12-06 19:56:31 +00001364 if (!timer_pending(&cm_core->tcp_timer))
1365 mod_timer(&cm_core->tcp_timer, (jiffies + NES_SHORT_TIME));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001366
1367 return 0;
1368}
1369
1370
1371/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001372 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001373 */
Faisal Latif7a576df2009-12-09 15:54:33 -08001374static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001375{
1376 struct rtable *rt;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001377 struct neighbour *neigh;
Faisal Latif7a576df2009-12-09 15:54:33 -08001378 int rc = arpindex;
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001379 struct net_device *netdev;
Faisal Latif7a576df2009-12-09 15:54:33 -08001380 struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001381
David S. Miller78fbfd82011-03-12 00:00:52 -05001382 rt = ip_route_output(&init_net, htonl(dst_ip), 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001383 if (IS_ERR(rt)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001384 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301385 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001386 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001387 }
1388
Roland Dreiercf55bb22011-03-24 17:13:20 -07001389 if (netif_is_bond_slave(nesvnic->netdev))
Jiri Pirko7f6e7102013-01-03 22:49:00 +00001390 netdev = netdev_master_upper_dev_get(nesvnic->netdev);
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001391 else
1392 netdev = nesvnic->netdev;
1393
Tatyana Nikolovaef3d0c42012-09-20 19:23:14 +00001394 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, netdev);
David Millere55684f2012-01-24 13:16:03 +00001395
David Miller40e2bb52011-12-02 16:52:27 +00001396 rcu_read_lock();
Bob Sharp7191a0a2008-10-03 12:21:19 -07001397 if (neigh) {
1398 if (neigh->nud_state & NUD_VALID) {
1399 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001400 " is %pM, Gateway is 0x%08X \n", dst_ip,
1401 neigh->ha, ntohl(rt->rt_gateway));
Faisal Latif7a576df2009-12-09 15:54:33 -08001402
1403 if (arpindex >= 0) {
Ding Tianhong79adc532014-01-08 10:53:39 +08001404 if (ether_addr_equal(nesadapter->arp_table[arpindex].mac_addr, neigh->ha)) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001405 /* Mac address same as in nes_arp_table */
David Millere55684f2012-01-24 13:16:03 +00001406 goto out;
Faisal Latif7a576df2009-12-09 15:54:33 -08001407 }
1408
1409 nes_manage_arp_cache(nesvnic->netdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301410 nesadapter->arp_table[arpindex].mac_addr,
1411 dst_ip, NES_ARP_DELETE);
Faisal Latif7a576df2009-12-09 15:54:33 -08001412 }
1413
Bob Sharp7191a0a2008-10-03 12:21:19 -07001414 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1415 dst_ip, NES_ARP_ADD);
1416 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1417 NES_ARP_RESOLVE);
David Miller40e2bb52011-12-02 16:52:27 +00001418 } else {
1419 neigh_event_send(neigh, NULL);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001420 }
Bob Sharp7191a0a2008-10-03 12:21:19 -07001421 }
David Millere55684f2012-01-24 13:16:03 +00001422out:
David Miller40e2bb52011-12-02 16:52:27 +00001423 rcu_read_unlock();
David Millere55684f2012-01-24 13:16:03 +00001424
1425 if (neigh)
1426 neigh_release(neigh);
1427
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001428 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001429 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001430}
1431
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001432/**
1433 * make_cm_node - create a new instance of a cm node
1434 */
1435static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301436 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1437 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001438{
1439 struct nes_cm_node *cm_node;
1440 struct timespec ts;
Faisal Latif7a576df2009-12-09 15:54:33 -08001441 int oldarpindex = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001442 int arpindex = 0;
1443 struct nes_device *nesdev;
1444 struct nes_adapter *nesadapter;
1445
1446 /* create an hte and cm_node for this instance */
1447 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1448 if (!cm_node)
1449 return NULL;
1450
1451 /* set our node specific transport info */
1452 cm_node->loc_addr = cm_info->loc_addr;
1453 cm_node->rem_addr = cm_info->rem_addr;
1454 cm_node->loc_port = cm_info->loc_port;
1455 cm_node->rem_port = cm_info->rem_port;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301456
1457 cm_node->mpa_frame_rev = mpa_version;
1458 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05001459 cm_node->mpav2_ird_ord = 0;
1460 cm_node->ird_size = 0;
1461 cm_node->ord_size = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301462
Harvey Harrison63779432008-10-31 00:56:00 -07001463 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1464 &cm_node->loc_addr, cm_node->loc_port,
1465 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001466 cm_node->listener = listener;
1467 cm_node->netdev = nesvnic->netdev;
1468 cm_node->cm_id = cm_info->cm_id;
1469 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1470
Faisal Latif6492cdf2008-07-24 20:50:45 -07001471 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301472 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001473
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001474 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001475
1476 cm_node->loopbackpartner = NULL;
1477 atomic_set(&cm_node->ref_count, 1);
1478 /* associate our parent CM core */
1479 cm_node->cm_core = cm_core;
1480 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1481 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1482 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301483 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001484 ts = current_kernel_time();
1485 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1486 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301487 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001488 cm_node->tcp_cntxt.rcv_nxt = 0;
1489 /* get a unique session ID , add thread_id to an upcounter to handle race */
1490 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001491 cm_node->conn_type = cm_info->conn_type;
1492 cm_node->apbvt_set = 0;
1493 cm_node->accept_pend = 0;
1494
1495 cm_node->nesvnic = nesvnic;
1496 /* get some device handles, for arp lookup */
1497 nesdev = nesvnic->nesdev;
1498 nesadapter = nesdev->nesadapter;
1499
1500 cm_node->loopbackpartner = NULL;
Faisal Latif7a576df2009-12-09 15:54:33 -08001501
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001502 /* get the mac addr for the remote node */
Tatyana Nikolovaef3d0c42012-09-20 19:23:14 +00001503 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1504 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001505 if (arpindex < 0) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001506 kfree(cm_node);
1507 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001508 }
1509
1510 /* copy the mac addr to node context */
1511 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001512 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1513 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001514
1515 add_hte_node(cm_core, cm_node);
1516 atomic_inc(&cm_nodes_created);
1517
1518 return cm_node;
1519}
1520
1521
1522/**
1523 * add_ref_cm_node - destroy an instance of a cm node
1524 */
1525static int add_ref_cm_node(struct nes_cm_node *cm_node)
1526{
1527 atomic_inc(&cm_node->ref_count);
1528 return 0;
1529}
1530
1531
1532/**
1533 * rem_ref_cm_node - destroy an instance of a cm node
1534 */
1535static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301536 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001537{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001538 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001539 struct nes_qp *nesqp;
1540
1541 if (!cm_node)
1542 return -EINVAL;
1543
1544 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1545 if (atomic_dec_return(&cm_node->ref_count)) {
1546 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1547 return 0;
1548 }
1549 list_del(&cm_node->list);
1550 atomic_dec(&cm_core->ht_node_cnt);
1551 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1552
1553 /* if the node is destroyed before connection was accelerated */
1554 if (!cm_node->accelerated && cm_node->accept_pend) {
1555 BUG_ON(!cm_node->listener);
1556 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1557 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1558 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001559 WARN_ON(cm_node->send_entry);
1560 if (cm_node->recv_entry)
1561 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001562 if (cm_node->listener) {
1563 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1564 } else {
1565 if (cm_node->apbvt_set && cm_node->nesvnic) {
1566 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301567 PCI_FUNC(
1568 cm_node->nesvnic->nesdev->pcidev->devfn),
1569 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001570 }
1571 }
1572
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001573 atomic_dec(&cm_core->node_cnt);
1574 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001575 nesqp = cm_node->nesqp;
1576 if (nesqp) {
1577 nesqp->cm_node = NULL;
1578 nes_rem_ref(&nesqp->ibqp);
1579 cm_node->nesqp = NULL;
1580 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001581
Faisal Latif6492cdf2008-07-24 20:50:45 -07001582 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001583 return 0;
1584}
1585
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001586/**
1587 * process_options
1588 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001589static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301590 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001591{
1592 u32 tmp;
1593 u32 offset = 0;
1594 union all_known_options *all_options;
1595 char got_mss_option = 0;
1596
1597 while (offset < optionsize) {
1598 all_options = (union all_known_options *)(optionsloc + offset);
1599 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001600 case OPTION_NUMBER_END:
1601 offset = optionsize;
1602 break;
1603 case OPTION_NUMBER_NONE:
1604 offset += 1;
1605 continue;
1606 case OPTION_NUMBER_MSS:
1607 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301608 "Size: %d\n", __func__,
1609 all_options->as_mss.length, offset, optionsize);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001610 got_mss_option = 1;
1611 if (all_options->as_mss.length != 4) {
1612 return 1;
1613 } else {
1614 tmp = ntohs(all_options->as_mss.mss);
1615 if (tmp > 0 && tmp <
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301616 cm_node->tcp_cntxt.mss)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001617 cm_node->tcp_cntxt.mss = tmp;
1618 }
1619 break;
1620 case OPTION_NUMBER_WINDOW_SCALE:
1621 cm_node->tcp_cntxt.snd_wscale =
1622 all_options->as_windowscale.shiftcount;
1623 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001624 default:
1625 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301626 all_options->as_base.optionnum);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001627 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001628 }
1629 offset += all_options->as_base.length;
1630 }
1631 if ((!got_mss_option) && (syn_packet))
1632 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1633 return 0;
1634}
1635
Faisal Latif6492cdf2008-07-24 20:50:45 -07001636static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001637{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001638 atomic_inc(&cm_accel_dropped_pkts);
1639 dev_kfree_skb_any(skb);
1640}
1641
Faisal Latif9d5ab132009-03-06 15:15:01 -08001642static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001643{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001644 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301645 "refcnt=%d\n", cm_node, cm_node->state,
1646 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001647 switch (cm_node->state) {
1648 case NES_CM_STATE_SYN_RCVD:
1649 case NES_CM_STATE_SYN_SENT:
1650 case NES_CM_STATE_ESTABLISHED:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001651 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001652 cm_node->tcp_cntxt.rcv_nxt++;
1653 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001654 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001655 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001656 break;
Faisal Latifb1190d32009-12-09 15:54:32 -08001657 case NES_CM_STATE_MPAREQ_SENT:
1658 create_event(cm_node, NES_CM_EVENT_ABORTED);
1659 cm_node->tcp_cntxt.rcv_nxt++;
1660 cleanup_retrans_entry(cm_node);
1661 cm_node->state = NES_CM_STATE_CLOSED;
1662 add_ref_cm_node(cm_node);
1663 send_reset(cm_node, NULL);
1664 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001665 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001666 cm_node->tcp_cntxt.rcv_nxt++;
1667 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001668 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001669 send_ack(cm_node, NULL);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001670 /* Wait for ACK as this is simultaneous close..
Faisal Latif9d5ab132009-03-06 15:15:01 -08001671 * After we receive ACK, do not send anything..
1672 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001673 break;
1674 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001675 cm_node->tcp_cntxt.rcv_nxt++;
1676 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001677 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001678 send_ack(cm_node, NULL);
1679 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1680 break;
1681 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001682 cm_node->tcp_cntxt.rcv_nxt++;
1683 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001684 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001685 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001686 break;
1687 case NES_CM_STATE_TSA:
1688 default:
1689 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1690 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001691 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001692 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001693}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001694
Faisal Latif6492cdf2008-07-24 20:50:45 -07001695
1696static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1697 struct tcphdr *tcph)
1698{
1699
1700 int reset = 0; /* whether to send reset in case of err.. */
1701 atomic_inc(&cm_resets_recvd);
1702 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1703 " refcnt=%d\n", cm_node, cm_node->state,
1704 atomic_read(&cm_node->ref_count));
1705 cleanup_retrans_entry(cm_node);
1706 switch (cm_node->state) {
1707 case NES_CM_STATE_SYN_SENT:
1708 case NES_CM_STATE_MPAREQ_SENT:
1709 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1710 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1711 cm_node->listener, cm_node->state);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301712 switch (cm_node->mpa_frame_rev) {
1713 case IETF_MPA_V2:
1714 cm_node->mpa_frame_rev = IETF_MPA_V1;
1715 /* send a syn and goto syn sent state */
1716 cm_node->state = NES_CM_STATE_SYN_SENT;
1717 if (send_syn(cm_node, 0, NULL)) {
1718 active_open_err(cm_node, skb, reset);
1719 }
1720 break;
1721 case IETF_MPA_V1:
1722 default:
1723 active_open_err(cm_node, skb, reset);
1724 break;
1725 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001726 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001727 case NES_CM_STATE_MPAREQ_RCVD:
Dan Carpenter4d8d6382010-09-15 18:32:39 +02001728 atomic_inc(&cm_node->passive_state);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001729 dev_kfree_skb_any(skb);
1730 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001731 case NES_CM_STATE_ESTABLISHED:
1732 case NES_CM_STATE_SYN_RCVD:
1733 case NES_CM_STATE_LISTENING:
1734 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1735 passive_open_err(cm_node, skb, reset);
1736 break;
1737 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001738 active_open_err(cm_node, skb, reset);
1739 break;
1740 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001741 drop_packet(skb);
1742 break;
Faisal Latifdae58722010-08-14 21:04:56 +00001743 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif68237a02009-06-22 22:53:28 -07001744 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001745 case NES_CM_STATE_LAST_ACK:
1746 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001747 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001748 cm_node->state = NES_CM_STATE_CLOSED;
1749 rem_ref_cm_node(cm_node->cm_core, cm_node);
1750 drop_packet(skb);
1751 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001752 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001753 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001754 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001755 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001756}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001757
Faisal Latif9d5ab132009-03-06 15:15:01 -08001758
1759static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001760{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301761 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001762 int datasize = skb->len;
1763 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001764
1765 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301766 u32 res_type;
1767
Faisal Latif9d5ab132009-03-06 15:15:01 -08001768 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1769 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001770 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001771 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001772 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301773 "cm_node=%p listener=%p state=%d\n", __func__,
1774 __LINE__, cm_node, cm_node->listener,
1775 cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001776 active_open_err(cm_node, skb, 1);
1777 } else {
1778 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001779 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001780 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001781 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001782
1783 switch (cm_node->state) {
1784 case NES_CM_STATE_ESTABLISHED:
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301785 if (res_type == NES_MPA_REQUEST_REJECT)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001786 /*BIG problem as we are receiving the MPA.. So should
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301787 * not be REJECT.. This is Passive Open.. We can
1788 * only receive it Reject for Active Open...*/
Faisal Latif9d5ab132009-03-06 15:15:01 -08001789 WARN_ON(1);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001790 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1791 type = NES_CM_EVENT_MPA_REQ;
1792 atomic_set(&cm_node->passive_state,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301793 NES_PASSIVE_STATE_INDICATED);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001794 break;
1795 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001796 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001797 if (res_type == NES_MPA_REQUEST_REJECT) {
1798 type = NES_CM_EVENT_MPA_REJECT;
1799 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1800 } else {
1801 type = NES_CM_EVENT_CONNECTED;
1802 cm_node->state = NES_CM_STATE_TSA;
1803 }
1804
1805 break;
1806 default:
1807 WARN_ON(1);
1808 break;
1809 }
1810 dev_kfree_skb_any(skb);
1811 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001812}
1813
1814static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1815{
1816 switch (cm_node->state) {
1817 case NES_CM_STATE_SYN_SENT:
1818 case NES_CM_STATE_MPAREQ_SENT:
1819 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301820 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1821 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001822 active_open_err(cm_node, skb, 1);
1823 break;
1824 case NES_CM_STATE_ESTABLISHED:
1825 case NES_CM_STATE_SYN_RCVD:
1826 passive_open_err(cm_node, skb, 1);
1827 break;
1828 case NES_CM_STATE_TSA:
1829 default:
1830 drop_packet(skb);
1831 }
1832}
1833
1834static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301835 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001836{
1837 int err;
1838
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301839 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001840 if (err)
1841 active_open_err(cm_node, skb, 1);
1842
1843 return err;
1844}
1845
1846static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301847 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001848{
1849 int err = 0;
1850 u32 seq;
1851 u32 ack_seq;
1852 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1853 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1854 u32 rcv_wnd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301855
Faisal Latif6492cdf2008-07-24 20:50:45 -07001856 seq = ntohl(tcph->seq);
1857 ack_seq = ntohl(tcph->ack_seq);
1858 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1859 if (ack_seq != loc_seq_num)
1860 err = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301861 else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001862 err = 1;
1863 if (err) {
1864 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301865 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1866 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001867 indicate_pkt_err(cm_node, skb);
1868 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301869 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1870 rcv_wnd);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001871 }
1872 return err;
1873}
1874
1875/*
1876 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1877 * is created with a listener or it may comein as rexmitted packet which in
1878 * that case will be just dropped.
1879 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001880static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301881 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001882{
1883 int ret;
1884 u32 inc_sequence;
1885 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001886
1887 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001888 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001889 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001890
Faisal Latif6492cdf2008-07-24 20:50:45 -07001891 switch (cm_node->state) {
1892 case NES_CM_STATE_SYN_SENT:
1893 case NES_CM_STATE_MPAREQ_SENT:
1894 /* Rcvd syn on active open connection*/
1895 active_open_err(cm_node, skb, 1);
1896 break;
1897 case NES_CM_STATE_LISTENING:
1898 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001899 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301900 cm_node->listener->backlog) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001901 nes_debug(NES_DBG_CM, "drop syn due to backlog "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301902 "pressure \n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07001903 cm_backlog_drops++;
1904 passive_open_err(cm_node, skb, 0);
1905 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001906 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001907 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301908 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001909 if (ret) {
1910 passive_open_err(cm_node, skb, 0);
1911 /* drop pkt */
1912 break;
1913 }
1914 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1915 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001916 cm_node->accept_pend = 1;
1917 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1918
Faisal Latif6492cdf2008-07-24 20:50:45 -07001919 cm_node->state = NES_CM_STATE_SYN_RCVD;
1920 send_syn(cm_node, 1, skb);
1921 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001922 case NES_CM_STATE_CLOSED:
1923 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001924 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001925 send_reset(cm_node, skb);
1926 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001927 case NES_CM_STATE_TSA:
1928 case NES_CM_STATE_ESTABLISHED:
1929 case NES_CM_STATE_FIN_WAIT1:
1930 case NES_CM_STATE_FIN_WAIT2:
1931 case NES_CM_STATE_MPAREQ_RCVD:
1932 case NES_CM_STATE_LAST_ACK:
1933 case NES_CM_STATE_CLOSING:
1934 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001935 default:
1936 drop_packet(skb);
1937 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001938 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001939}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001940
Faisal Latif6492cdf2008-07-24 20:50:45 -07001941static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301942 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001943{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001944 int ret;
1945 u32 inc_sequence;
1946 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001947
Faisal Latif6492cdf2008-07-24 20:50:45 -07001948 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001949 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001950 inc_sequence = ntohl(tcph->seq);
1951 switch (cm_node->state) {
1952 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001953 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001954 /* active open */
1955 if (check_syn(cm_node, tcph, skb))
1956 return;
1957 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1958 /* setup options */
1959 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1960 if (ret) {
1961 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301962 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001963 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001964 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001965 cleanup_retrans_entry(cm_node);
1966 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1967 send_mpa_request(cm_node, skb);
1968 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1969 break;
1970 case NES_CM_STATE_MPAREQ_RCVD:
1971 /* passive open, so should not be here */
1972 passive_open_err(cm_node, skb, 1);
1973 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001974 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001975 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1976 cleanup_retrans_entry(cm_node);
1977 cm_node->state = NES_CM_STATE_CLOSED;
1978 send_reset(cm_node, skb);
1979 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001980 case NES_CM_STATE_CLOSED:
1981 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1982 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001983 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001984 send_reset(cm_node, skb);
1985 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001986 case NES_CM_STATE_ESTABLISHED:
1987 case NES_CM_STATE_FIN_WAIT1:
1988 case NES_CM_STATE_FIN_WAIT2:
1989 case NES_CM_STATE_LAST_ACK:
1990 case NES_CM_STATE_TSA:
1991 case NES_CM_STATE_CLOSING:
1992 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001993 case NES_CM_STATE_MPAREQ_SENT:
1994 default:
1995 drop_packet(skb);
1996 break;
1997 }
1998}
1999
Faisal Latif109d67e2009-04-27 13:41:06 -07002000static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302001 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002002{
2003 int datasize = 0;
2004 u32 inc_sequence;
Faisal Latif109d67e2009-04-27 13:41:06 -07002005 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06002006 int optionsize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302007
Faisal Latifabb77252008-11-21 20:50:52 -06002008 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06002009
Faisal Latif6492cdf2008-07-24 20:50:45 -07002010 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07002011 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002012
2013 skb_pull(skb, tcph->doff << 2);
2014 inc_sequence = ntohl(tcph->seq);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002015 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002016 switch (cm_node->state) {
2017 case NES_CM_STATE_SYN_RCVD:
2018 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07002019 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002020 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
2021 if (ret)
2022 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002023 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2024 cm_node->state = NES_CM_STATE_ESTABLISHED;
2025 if (datasize) {
2026 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002027 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302028 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002029 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302030 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002031 break;
2032 case NES_CM_STATE_ESTABLISHED:
2033 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08002034 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002035 if (datasize) {
2036 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002037 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302038 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002039 drop_packet(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302040 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002041 break;
2042 case NES_CM_STATE_MPAREQ_SENT:
2043 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2044 if (datasize) {
2045 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002046 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302047 } else { /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002048 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302049 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002050 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002051 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002052 cleanup_retrans_entry(cm_node);
2053 cm_node->state = NES_CM_STATE_CLOSED;
2054 send_reset(cm_node, skb);
2055 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002056 case NES_CM_STATE_CLOSED:
2057 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08002058 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002059 send_reset(cm_node, skb);
2060 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002061 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002062 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002063 cleanup_retrans_entry(cm_node);
2064 cm_node->state = NES_CM_STATE_CLOSED;
2065 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002066 rem_ref_cm_node(cm_node->cm_core, cm_node);
2067 drop_packet(skb);
2068 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002069 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002070 cleanup_retrans_entry(cm_node);
2071 drop_packet(skb);
2072 cm_node->state = NES_CM_STATE_FIN_WAIT2;
2073 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002074 case NES_CM_STATE_SYN_SENT:
2075 case NES_CM_STATE_FIN_WAIT2:
2076 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002077 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002078 case NES_CM_STATE_UNKNOWN:
2079 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002080 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002081 drop_packet(skb);
2082 break;
2083 }
Faisal Latif109d67e2009-04-27 13:41:06 -07002084 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002085}
2086
2087
2088
2089static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302090 struct sk_buff *skb, int optionsize, int passive)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002091{
2092 u8 *optionsloc = (u8 *)&tcph[1];
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302093
Faisal Latif6492cdf2008-07-24 20:50:45 -07002094 if (optionsize) {
2095 if (process_options(cm_node, optionsloc, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302096 (u32)tcph->syn)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002097 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302098 __func__, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002099 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06002100 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002101 else
Faisal Latifabb77252008-11-21 20:50:52 -06002102 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002103 return 1;
2104 }
2105 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002106
2107 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302108 cm_node->tcp_cntxt.snd_wscale;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002109
Faisal Latif6492cdf2008-07-24 20:50:45 -07002110 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002111 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002112 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002113}
2114
Faisal Latif6492cdf2008-07-24 20:50:45 -07002115/*
2116 * active_open_err() will send reset() if flag set..
2117 * It will also send ABORT event.
2118 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002119static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302120 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002121{
2122 cleanup_retrans_entry(cm_node);
2123 if (reset) {
2124 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302125 "state=%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002126 add_ref_cm_node(cm_node);
2127 send_reset(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302128 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002129 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302130 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002131
2132 cm_node->state = NES_CM_STATE_CLOSED;
2133 create_event(cm_node, NES_CM_EVENT_ABORTED);
2134}
2135
2136/*
2137 * passive_open_err() will either do a reset() or will free up the skb and
2138 * remove the cm_node.
2139 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002140static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302141 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002142{
2143 cleanup_retrans_entry(cm_node);
2144 cm_node->state = NES_CM_STATE_CLOSED;
2145 if (reset) {
2146 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302147 "cm_node=%p state =%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002148 send_reset(cm_node, skb);
2149 } else {
2150 dev_kfree_skb_any(skb);
2151 rem_ref_cm_node(cm_node->cm_core, cm_node);
2152 }
2153}
2154
2155/*
2156 * free_retrans_entry() routines assumes that the retrans_list_lock has
2157 * been acquired before calling.
2158 */
2159static void free_retrans_entry(struct nes_cm_node *cm_node)
2160{
2161 struct nes_timer_entry *send_entry;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302162
Faisal Latif6492cdf2008-07-24 20:50:45 -07002163 send_entry = cm_node->send_entry;
2164 if (send_entry) {
2165 cm_node->send_entry = NULL;
2166 dev_kfree_skb_any(send_entry->skb);
2167 kfree(send_entry);
2168 rem_ref_cm_node(cm_node->cm_core, cm_node);
2169 }
2170}
2171
2172static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
2173{
2174 unsigned long flags;
2175
2176 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
2177 free_retrans_entry(cm_node);
2178 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
2179}
2180
2181/**
2182 * process_packet
2183 * Returns skb if to be freed, else it will return NULL if already used..
2184 */
2185static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302186 struct nes_cm_core *cm_core)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002187{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302188 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002189 struct tcphdr *tcph = tcp_hdr(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302190 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07002191 int ret = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302192
Faisal Latif6492cdf2008-07-24 20:50:45 -07002193 skb_pull(skb, ip_hdr(skb)->ihl << 2);
2194
2195 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302196 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
2197 tcph->ack, tcph->rst, tcph->fin);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002198
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302199 if (tcph->rst) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002200 pkt_type = NES_PKT_TYPE_RST;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302201 } else if (tcph->syn) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002202 pkt_type = NES_PKT_TYPE_SYN;
2203 if (tcph->ack)
2204 pkt_type = NES_PKT_TYPE_SYNACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302205 } else if (tcph->ack) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002206 pkt_type = NES_PKT_TYPE_ACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302207 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002208 if (tcph->fin)
2209 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002210
2211 switch (pkt_type) {
2212 case NES_PKT_TYPE_SYN:
2213 handle_syn_pkt(cm_node, skb, tcph);
2214 break;
2215 case NES_PKT_TYPE_SYNACK:
2216 handle_synack_pkt(cm_node, skb, tcph);
2217 break;
2218 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002219 ret = handle_ack_pkt(cm_node, skb, tcph);
2220 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08002221 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002222 break;
2223 case NES_PKT_TYPE_RST:
2224 handle_rst_pkt(cm_node, skb, tcph);
2225 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002226 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002227 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08002228 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07002229 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002230 break;
2231 }
2232}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002233
2234/**
2235 * mini_cm_listen - create a listen node with params
2236 */
2237static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302238 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002239{
2240 struct nes_cm_listener *listener;
2241 unsigned long flags;
2242
2243 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302244 cm_info->loc_addr, cm_info->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002245
2246 /* cannot have multiple matching listeners */
2247 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302248 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002249 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
2250 /* find automatically incs ref count ??? */
2251 atomic_dec(&listener->ref_count);
2252 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
2253 return NULL;
2254 }
2255
2256 if (!listener) {
2257 /* create a CM listen node (1/2 node to compare incoming traffic to) */
2258 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
2259 if (!listener) {
2260 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
2261 return NULL;
2262 }
2263
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002264 listener->loc_addr = htonl(cm_info->loc_addr);
2265 listener->loc_port = htons(cm_info->loc_port);
2266 listener->reused_node = 0;
2267
2268 atomic_set(&listener->ref_count, 1);
2269 }
2270 /* pasive case */
2271 /* find already inc'ed the ref count */
2272 else {
2273 listener->reused_node = 1;
2274 }
2275
2276 listener->cm_id = cm_info->cm_id;
2277 atomic_set(&listener->pend_accepts_cnt, 0);
2278 listener->cm_core = cm_core;
2279 listener->nesvnic = nesvnic;
2280 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002281
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002282 listener->conn_type = cm_info->conn_type;
2283 listener->backlog = cm_info->backlog;
2284 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
2285
2286 if (!listener->reused_node) {
2287 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2288 list_add(&listener->list, &cm_core->listen_list.list);
2289 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2290 atomic_inc(&cm_core->listen_node_cnt);
2291 }
2292
2293 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302294 " listener = %p, backlog = %d, cm_id = %p.\n",
2295 cm_info->loc_addr, cm_info->loc_port,
2296 listener, listener->backlog, listener->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002297
2298 return listener;
2299}
2300
2301
2302/**
2303 * mini_cm_connect - make a connection node with params
2304 */
Faisal Latif54c86a82008-09-30 14:47:27 -07002305static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302306 struct nes_vnic *nesvnic, u16 private_data_len,
2307 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002308{
2309 int ret = 0;
2310 struct nes_cm_node *cm_node;
2311 struct nes_cm_listener *loopbackremotelistener;
2312 struct nes_cm_node *loopbackremotenode;
2313 struct nes_cm_info loopback_cm_info;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302314 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002315
2316 /* create a CM connection node */
2317 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2318 if (!cm_node)
2319 return NULL;
2320
Glenn Streiff7495ab62008-04-29 13:46:54 -07002321 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002322 cm_node->tcp_cntxt.client = 1;
2323 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2324
2325 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002326 loopbackremotelistener = find_listener(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302327 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2328 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002329 if (loopbackremotelistener == NULL) {
2330 create_event(cm_node, NES_CM_EVENT_ABORTED);
2331 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002332 loopback_cm_info = *cm_info;
2333 loopback_cm_info.loc_port = cm_info->rem_port;
2334 loopback_cm_info.rem_port = cm_info->loc_port;
2335 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002336 loopbackremotenode = make_cm_node(cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302337 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002338 if (!loopbackremotenode) {
2339 rem_ref_cm_node(cm_node->cm_core, cm_node);
2340 return NULL;
2341 }
2342 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002343 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002344 loopbackremotenode->tcp_cntxt.rcv_wscale =
2345 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002346 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002347 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302348 private_data_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002349 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002350
Faisal Latif6492cdf2008-07-24 20:50:45 -07002351 /* we are done handling this state. */
2352 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002353 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002354 cm_node->tcp_cntxt.rcv_nxt =
2355 loopbackremotenode->tcp_cntxt.loc_seq_num;
2356 loopbackremotenode->tcp_cntxt.rcv_nxt =
2357 cm_node->tcp_cntxt.loc_seq_num;
2358 cm_node->tcp_cntxt.max_snd_wnd =
2359 loopbackremotenode->tcp_cntxt.rcv_wnd;
2360 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2361 cm_node->tcp_cntxt.rcv_wnd;
2362 cm_node->tcp_cntxt.snd_wnd =
2363 loopbackremotenode->tcp_cntxt.rcv_wnd;
2364 loopbackremotenode->tcp_cntxt.snd_wnd =
2365 cm_node->tcp_cntxt.rcv_wnd;
2366 cm_node->tcp_cntxt.snd_wscale =
2367 loopbackremotenode->tcp_cntxt.rcv_wscale;
2368 loopbackremotenode->tcp_cntxt.snd_wscale =
2369 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002370 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002371 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2372 }
2373 return cm_node;
2374 }
2375
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302376 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
2377 cm_node->mpa_frame_size = private_data_len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002378
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302379 memcpy(start_buff, private_data, private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002380
2381 /* send a syn and goto syn sent state */
2382 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002383 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002384
Faisal Latif6492cdf2008-07-24 20:50:45 -07002385 if (ret) {
2386 /* error in sending the syn free up the cm_node struct */
2387 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302388 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2389 cm_node->rem_addr, cm_node->rem_port, cm_node,
2390 cm_node->cm_id);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002391 rem_ref_cm_node(cm_node->cm_core, cm_node);
2392 cm_node = NULL;
2393 }
2394
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302395 if (cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002396 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302397 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2398 cm_node->rem_addr, cm_node->rem_port, cm_node,
2399 cm_node->cm_id);
2400 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002401
2402 return cm_node;
2403}
2404
2405
2406/**
2407 * mini_cm_accept - accept a connection
2408 * This function is never called
2409 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302410static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002411{
2412 return 0;
2413}
2414
2415
2416/**
2417 * mini_cm_reject - reject and teardown a connection
2418 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302419static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002420{
2421 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002422 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002423 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002424 struct nes_cm_event event;
2425 struct iw_cm_id *cm_id = cm_node->cm_id;
2426 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002427
Faisal Latif6492cdf2008-07-24 20:50:45 -07002428 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302429 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002430
Faisal Latif6492cdf2008-07-24 20:50:45 -07002431 if (cm_node->tcp_cntxt.client)
2432 return ret;
2433 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002434
Faisal Latif9d5ab132009-03-06 15:15:01 -08002435 if (!loopback) {
2436 passive_state = atomic_add_return(1, &cm_node->passive_state);
2437 if (passive_state == NES_SEND_RESET_EVENT) {
2438 cm_node->state = NES_CM_STATE_CLOSED;
2439 rem_ref_cm_node(cm_core, cm_node);
2440 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002441 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2442 rem_ref_cm_node(cm_core, cm_node);
2443 } else {
2444 ret = send_mpa_reject(cm_node);
2445 if (ret) {
2446 cm_node->state = NES_CM_STATE_CLOSED;
2447 err = send_reset(cm_node, NULL);
2448 if (err)
2449 WARN_ON(1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302450 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002451 cm_id->add_ref(cm_id);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302452 }
Faisal Latifc5a7d482009-12-09 15:54:08 -08002453 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002454 }
2455 } else {
2456 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002457 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2458 rem_ref_cm_node(cm_core, cm_node);
2459 rem_ref_cm_node(cm_core, loopback);
2460 } else {
2461 event.cm_node = loopback;
2462 event.cm_info.rem_addr = loopback->rem_addr;
2463 event.cm_info.loc_addr = loopback->loc_addr;
2464 event.cm_info.rem_port = loopback->rem_port;
2465 event.cm_info.loc_port = loopback->loc_port;
2466 event.cm_info.cm_id = loopback->cm_id;
2467 cm_event_mpa_reject(&event);
2468 rem_ref_cm_node(cm_core, cm_node);
2469 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002470
Faisal Latifc5a7d482009-12-09 15:54:08 -08002471 cm_id = loopback->cm_id;
2472 rem_ref_cm_node(cm_core, loopback);
2473 cm_id->rem_ref(cm_id);
2474 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002475 }
2476
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002477 return ret;
2478}
2479
2480
2481/**
2482 * mini_cm_close
2483 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002484static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002485{
2486 int ret = 0;
2487
2488 if (!cm_core || !cm_node)
2489 return -EINVAL;
2490
2491 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002492 case NES_CM_STATE_SYN_RCVD:
2493 case NES_CM_STATE_SYN_SENT:
2494 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2495 case NES_CM_STATE_ESTABLISHED:
2496 case NES_CM_STATE_ACCEPTING:
2497 case NES_CM_STATE_MPAREQ_SENT:
2498 case NES_CM_STATE_MPAREQ_RCVD:
2499 cleanup_retrans_entry(cm_node);
2500 send_reset(cm_node, NULL);
2501 break;
2502 case NES_CM_STATE_CLOSE_WAIT:
2503 cm_node->state = NES_CM_STATE_LAST_ACK;
2504 send_fin(cm_node, NULL);
2505 break;
2506 case NES_CM_STATE_FIN_WAIT1:
2507 case NES_CM_STATE_FIN_WAIT2:
2508 case NES_CM_STATE_LAST_ACK:
2509 case NES_CM_STATE_TIME_WAIT:
2510 case NES_CM_STATE_CLOSING:
2511 ret = -1;
2512 break;
2513 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002514 cleanup_retrans_entry(cm_node);
2515 send_reset(cm_node, NULL);
2516 break;
2517 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002518 case NES_CM_STATE_UNKNOWN:
2519 case NES_CM_STATE_INITED:
2520 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002521 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002522 ret = rem_ref_cm_node(cm_core, cm_node);
2523 break;
2524 case NES_CM_STATE_TSA:
2525 if (cm_node->send_entry)
2526 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302527 "send_entry=%p\n", cm_node->send_entry);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002528 ret = rem_ref_cm_node(cm_core, cm_node);
2529 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002530 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002531 return ret;
2532}
2533
2534
2535/**
2536 * recv_pkt - recv an ETHERNET packet, and process it through CM
2537 * node state machine
2538 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002539static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302540 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002541{
2542 struct nes_cm_node *cm_node = NULL;
2543 struct nes_cm_listener *listener = NULL;
2544 struct iphdr *iph;
2545 struct tcphdr *tcph;
2546 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002547 int skb_handled = 1;
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002548 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002549
Faisal Latif6492cdf2008-07-24 20:50:45 -07002550 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002551 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302552 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002553 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002554
2555 iph = (struct iphdr *)skb->data;
2556 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002557
2558 nfo.loc_addr = ntohl(iph->daddr);
2559 nfo.loc_port = ntohs(tcph->dest);
2560 nfo.rem_addr = ntohl(iph->saddr);
2561 nfo.rem_port = ntohs(tcph->source);
2562
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002563 tmp_daddr = cpu_to_be32(iph->daddr);
2564 tmp_saddr = cpu_to_be32(iph->saddr);
2565
Harvey Harrison63779432008-10-31 00:56:00 -07002566 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002567 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002568
Faisal Latif6492cdf2008-07-24 20:50:45 -07002569 do {
2570 cm_node = find_node(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302571 nfo.rem_port, nfo.rem_addr,
2572 nfo.loc_port, nfo.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002573
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002574 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002575 /* Only type of packet accepted are for */
2576 /* the PASSIVE open (syn only) */
2577 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002578 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002579 break;
2580 }
2581 listener = find_listener(cm_core, nfo.loc_addr,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302582 nfo.loc_port,
2583 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002584 if (!listener) {
2585 nfo.cm_id = NULL;
2586 nfo.conn_type = 0;
2587 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2588 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002589 break;
2590 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002591 nfo.cm_id = listener->cm_id;
2592 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002593 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302594 listener);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002595 if (!cm_node) {
2596 nes_debug(NES_DBG_CM, "Unable to allocate "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302597 "node\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07002598 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002599 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002600 dev_kfree_skb_any(skb);
2601 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002602 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002603 if (!tcph->rst && !tcph->fin) {
2604 cm_node->state = NES_CM_STATE_LISTENING;
2605 } else {
2606 cm_packets_dropped++;
2607 rem_ref_cm_node(cm_core, cm_node);
2608 dev_kfree_skb_any(skb);
2609 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002610 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002611 add_ref_cm_node(cm_node);
2612 } else if (cm_node->state == NES_CM_STATE_TSA) {
Faisal Latif0f0bee82011-09-25 20:34:00 -05002613 if (cm_node->nesqp->pau_mode)
2614 nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
2615 else {
2616 rem_ref_cm_node(cm_core, cm_node);
2617 atomic_inc(&cm_accel_dropped_pkts);
2618 dev_kfree_skb_any(skb);
2619 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002620 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002621 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002622 skb_reset_network_header(skb);
2623 skb_set_transport_header(skb, sizeof(*tcph));
2624 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002625 process_packet(cm_node, skb, cm_core);
2626 rem_ref_cm_node(cm_core, cm_node);
2627 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002628 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002629}
2630
2631
2632/**
2633 * nes_cm_alloc_core - allocate a top level instance of a cm core
2634 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002635static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002636{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002637 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002638
2639 /* setup the CM core */
2640 /* alloc top level core control structure */
2641 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2642 if (!cm_core)
2643 return NULL;
2644
2645 INIT_LIST_HEAD(&cm_core->connected_nodes);
2646 init_timer(&cm_core->tcp_timer);
2647 cm_core->tcp_timer.function = nes_cm_timer_tick;
2648
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302649 cm_core->mtu = NES_CM_DEFAULT_MTU;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002650 cm_core->state = NES_CM_STATE_INITED;
2651 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2652
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002653 atomic_set(&cm_core->events_posted, 0);
2654
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002655 cm_core->api = &nes_cm_api;
2656
2657 spin_lock_init(&cm_core->ht_lock);
2658 spin_lock_init(&cm_core->listen_list_lock);
2659
2660 INIT_LIST_HEAD(&cm_core->listen_list.list);
2661
2662 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2663
2664 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2665 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2666 cm_core->post_event = nes_cm_post_event;
2667 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2668 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2669
2670 print_core(cm_core);
2671 return cm_core;
2672}
2673
2674
2675/**
2676 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2677 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002678static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002679{
2680 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2681
2682 if (!cm_core)
2683 return -EINVAL;
2684
2685 barrier();
2686
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302687 if (timer_pending(&cm_core->tcp_timer))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002688 del_timer(&cm_core->tcp_timer);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002689
2690 destroy_workqueue(cm_core->event_wq);
2691 destroy_workqueue(cm_core->disconn_wq);
2692 nes_debug(NES_DBG_CM, "\n");
2693 kfree(cm_core);
2694
2695 return 0;
2696}
2697
2698
2699/**
2700 * mini_cm_get
2701 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002702static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002703{
2704 return cm_core->state;
2705}
2706
2707
2708/**
2709 * mini_cm_set
2710 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002711static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002712{
2713 int ret = 0;
2714
2715 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002716 case NES_CM_SET_PKT_SIZE:
2717 cm_core->mtu = value;
2718 break;
2719 case NES_CM_SET_FREE_PKT_Q_SIZE:
2720 cm_core->free_tx_pkt_max = value;
2721 break;
2722 default:
2723 /* unknown set option */
2724 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002725 }
2726
2727 return ret;
2728}
2729
2730
2731/**
2732 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2733 * successfully exchanged when this is called
2734 */
2735static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2736{
2737 int ret = 0;
2738
2739 if (!nesqp)
2740 return -EINVAL;
2741
2742 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302743 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2744 NES_QPCONTEXT_MISC_DROS);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002745
2746 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2747 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2748
2749 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2750
2751 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2752
2753 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302754 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002755
2756 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302757 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2758 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002759
2760 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302761 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2762 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002763
2764 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2765 nesqp->nesqp_context->ts_recent = 0;
2766 nesqp->nesqp_context->ts_age = 0;
2767 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2768 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2769 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2770 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302771 cm_node->tcp_cntxt.rcv_wscale);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002772 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2773 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2774 nesqp->nesqp_context->srtt = 0;
2775 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2776 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302777 nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002778 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2779 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2780 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2781
2782 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302783 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2784 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2785 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2786 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2787 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2788 le32_to_cpu(nesqp->nesqp_context->misc));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002789 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2790 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2791 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2792
2793 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2794 cm_node->state = NES_CM_STATE_TSA;
2795
2796 return ret;
2797}
2798
2799
2800/**
2801 * nes_cm_disconn
2802 */
2803int nes_cm_disconn(struct nes_qp *nesqp)
2804{
Don Wood873fcdd2009-09-05 20:36:37 -07002805 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002806
Don Wood873fcdd2009-09-05 20:36:37 -07002807 work = kzalloc(sizeof *work, GFP_ATOMIC);
2808 if (!work)
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302809 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002810
Don Wood873fcdd2009-09-05 20:36:37 -07002811 nes_add_ref(&nesqp->ibqp);
2812 work->nesqp = nesqp;
2813 INIT_WORK(&work->work, nes_disconnect_worker);
2814 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002815 return 0;
2816}
2817
2818
2819/**
2820 * nes_disconnect_worker
2821 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002822static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002823{
Don Wood873fcdd2009-09-05 20:36:37 -07002824 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2825 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002826
Don Wood873fcdd2009-09-05 20:36:37 -07002827 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002828 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302829 nesqp->last_aeq, nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002830 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002831 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002832}
2833
2834
2835/**
2836 * nes_cm_disconn_true
2837 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002838static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002839{
2840 unsigned long flags;
2841 int ret = 0;
2842 struct iw_cm_id *cm_id;
2843 struct iw_cm_event cm_event;
2844 struct nes_vnic *nesvnic;
2845 u16 last_ae;
2846 u8 original_hw_tcp_state;
2847 u8 original_ibqp_state;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002848 int disconn_status = 0;
Don Woodb29a4fc2009-09-05 20:36:39 -07002849 int issue_disconn = 0;
2850 int issue_close = 0;
2851 int issue_flush = 0;
2852 u32 flush_q = NES_CQP_FLUSH_RQ;
2853 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002854
2855 if (!nesqp) {
2856 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2857 return -1;
2858 }
2859
2860 spin_lock_irqsave(&nesqp->lock, flags);
2861 cm_id = nesqp->cm_id;
2862 /* make sure we havent already closed this connection */
2863 if (!cm_id) {
2864 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302865 nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002866 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002867 return -1;
2868 }
2869
2870 nesvnic = to_nesvnic(nesqp->ibqp.device);
2871 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2872
2873 original_hw_tcp_state = nesqp->hw_tcp_state;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302874 original_ibqp_state = nesqp->ibqp_state;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002875 last_ae = nesqp->last_aeq;
2876
Don Woodb29a4fc2009-09-05 20:36:39 -07002877 if (nesqp->term_flags) {
2878 issue_disconn = 1;
2879 issue_close = 1;
2880 nesqp->cm_id = NULL;
Tatyana Nikolova196f40c2011-12-05 11:39:12 -06002881 del_timer(&nesqp->terminate_timer);
Don Woodb29a4fc2009-09-05 20:36:39 -07002882 if (nesqp->flush_issued == 0) {
2883 nesqp->flush_issued = 1;
2884 issue_flush = 1;
2885 }
2886 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2887 ((original_ibqp_state == IB_QPS_RTS) &&
2888 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2889 issue_disconn = 1;
2890 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002891 disconn_status = -ECONNRESET;
Don Woodb29a4fc2009-09-05 20:36:39 -07002892 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002893
Don Woodb29a4fc2009-09-05 20:36:39 -07002894 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2895 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2896 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2897 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2898 issue_close = 1;
2899 nesqp->cm_id = NULL;
2900 if (nesqp->flush_issued == 0) {
2901 nesqp->flush_issued = 1;
2902 issue_flush = 1;
2903 }
2904 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002905
Don Woodb29a4fc2009-09-05 20:36:39 -07002906 spin_unlock_irqrestore(&nesqp->lock, flags);
2907
2908 if ((issue_flush) && (nesqp->destroyed == 0)) {
2909 /* Flush the queue(s) */
2910 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2911 flush_q |= NES_CQP_FLUSH_SQ;
2912 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2913
2914 if (nesqp->term_flags) {
2915 ibevent.device = nesqp->ibqp.device;
2916 ibevent.event = nesqp->terminate_eventtype;
2917 ibevent.element.qp = &nesqp->ibqp;
Tatyana Nikolova784d1352012-05-11 14:29:32 -05002918 if (nesqp->ibqp.event_handler)
2919 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
Don Woodb29a4fc2009-09-05 20:36:39 -07002920 }
2921 }
2922
2923 if ((cm_id) && (cm_id->event_handler)) {
2924 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002925 atomic_inc(&cm_disconnects);
2926 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002927 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002928 cm_event.local_addr = cm_id->local_addr;
2929 cm_event.remote_addr = cm_id->remote_addr;
2930 cm_event.private_data = NULL;
2931 cm_event.private_data_len = 0;
2932
Faisal Latif6492cdf2008-07-24 20:50:45 -07002933 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302934 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2935 "cm_id = %p, refcount = %u.\n",
2936 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2937 nesqp->hwqp.sq_tail, cm_id,
2938 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002939
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002940 ret = cm_id->event_handler(cm_id, &cm_event);
2941 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002942 nes_debug(NES_DBG_CM, "OFA CM event_handler "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302943 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002944 }
2945
Don Woodb29a4fc2009-09-05 20:36:39 -07002946 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002947 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002948 nes_disconnect(nesqp, 1);
2949
2950 cm_id->provider_data = nesqp;
2951 /* Send up the close complete event */
2952 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002953 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002954 cm_event.provider_data = cm_id->provider_data;
2955 cm_event.local_addr = cm_id->local_addr;
2956 cm_event.remote_addr = cm_id->remote_addr;
2957 cm_event.private_data = NULL;
2958 cm_event.private_data_len = 0;
2959
2960 ret = cm_id->event_handler(cm_id, &cm_event);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302961 if (ret)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002962 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002963
2964 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002965 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002966 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002967
2968 return 0;
2969}
2970
2971
2972/**
2973 * nes_disconnect
2974 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002975static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002976{
2977 int ret = 0;
2978 struct nes_vnic *nesvnic;
2979 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002980 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002981
2982 nesvnic = to_nesvnic(nesqp->ibqp.device);
2983 if (!nesvnic)
2984 return -EINVAL;
2985
2986 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002987 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002988
2989 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00002990 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002991
2992 if (nesqp->active_conn) {
2993
2994 /* indicate this connection is NOT active */
2995 nesqp->active_conn = 0;
2996 } else {
2997 /* Need to free the Last Streaming Mode Message */
2998 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002999 if (nesqp->lsmm_mr)
3000 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003001 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303002 nesqp->private_data_len + nesqp->ietf_frame_size,
3003 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003004 }
3005 }
3006
3007 /* close the CM node down if it is still active */
3008 if (nesqp->cm_node) {
3009 nes_debug(NES_DBG_CM, "Call close API\n");
3010
3011 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003012 }
3013
3014 return ret;
3015}
3016
3017
3018/**
3019 * nes_accept
3020 */
3021int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3022{
3023 u64 u64temp;
3024 struct ib_qp *ibqp;
3025 struct nes_qp *nesqp;
3026 struct nes_vnic *nesvnic;
3027 struct nes_device *nesdev;
3028 struct nes_cm_node *cm_node;
3029 struct nes_adapter *adapter;
3030 struct ib_qp_attr attr;
3031 struct iw_cm_event cm_event;
3032 struct nes_hw_qp_wqe *wqe;
3033 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003034 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003035 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003036 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003037 struct nes_ib_device *nesibdev;
3038 struct ib_mr *ibmr = NULL;
3039 struct ib_phys_buf ibphysbuf;
3040 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07003041 u64 tagged_offset;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303042 u8 mpa_frame_offset = 0;
3043 struct ietf_mpa_v2 *mpa_v2_frame;
3044 u8 start_addr = 0;
3045 u8 *start_ptr = &start_addr;
3046 u8 **start_buff = &start_ptr;
3047 u16 buff_len = 0;
Steve Wise24d44a32013-07-04 16:10:44 +05303048 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->local_addr;
3049 struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->remote_addr;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003050
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003051 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3052 if (!ibqp)
3053 return -EINVAL;
3054
3055 /* get all our handles */
3056 nesqp = to_nesqp(ibqp);
3057 nesvnic = to_nesvnic(nesqp->ibqp.device);
3058 nesdev = nesvnic->nesdev;
3059 adapter = nesdev->nesadapter;
3060
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003061 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003062 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
3063 "%s\n", cm_node, nesvnic, nesvnic->netdev,
3064 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003065
Faisal Latifc5a7d482009-12-09 15:54:08 -08003066 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
3067 if (cm_node->loopbackpartner)
3068 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
3069 rem_ref_cm_node(cm_node->cm_core, cm_node);
3070 return -EINVAL;
3071 }
3072
Faisal Latifdae58722010-08-14 21:04:56 +00003073 passive_state = atomic_add_return(1, &cm_node->passive_state);
3074 if (passive_state == NES_SEND_RESET_EVENT) {
3075 rem_ref_cm_node(cm_node->cm_core, cm_node);
3076 return -ECONNRESET;
3077 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003078 /* associate the node with the QP */
3079 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003080 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003081
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003082
Faisal Latif6492cdf2008-07-24 20:50:45 -07003083 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
3084 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003085 atomic_inc(&cm_accepts);
3086
3087 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00003088 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003089
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303090 nesqp->ietf_frame_size = sizeof(struct ietf_mpa_v2);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003091 /* allocate the ietf frame and space for private data */
3092 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303093 nesqp->ietf_frame_size + conn_param->private_data_len,
3094 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003095
Faisal Latif6492cdf2008-07-24 20:50:45 -07003096 if (!nesqp->ietf_frame) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303097 nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07003098 return -ENOMEM;
3099 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303100 mpa_v2_frame = (struct ietf_mpa_v2 *)nesqp->ietf_frame;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003101
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303102 if (cm_node->mpa_frame_rev == IETF_MPA_V1)
3103 mpa_frame_offset = 4;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003104
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003105 if (cm_node->mpa_frame_rev == IETF_MPA_V1 ||
3106 cm_node->mpav2_ird_ord == IETF_NO_IRD_ORD) {
3107 record_ird_ord(cm_node, (u16)conn_param->ird, (u16)conn_param->ord);
3108 }
3109
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303110 memcpy(mpa_v2_frame->priv_data, conn_param->private_data,
3111 conn_param->private_data_len);
3112
3113 cm_build_mpa_frame(cm_node, start_buff, &buff_len, nesqp->ietf_frame, MPA_KEY_REPLY);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003114 nesqp->private_data_len = conn_param->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003115
Faisal Latif6492cdf2008-07-24 20:50:45 -07003116 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
3117 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003118
Steve Wise24d44a32013-07-04 16:10:44 +05303119 if (raddr->sin_addr.s_addr != laddr->sin_addr.s_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003120 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003121 nesibdev = nesvnic->nesibdev;
3122 nespd = nesqp->nespd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303123 ibphysbuf.addr = nesqp->ietf_frame_pbase + mpa_frame_offset;
3124 ibphysbuf.size = buff_len;
3125 tagged_offset = (u64)(unsigned long)*start_buff;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003126 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303127 &ibphysbuf, 1,
3128 IB_ACCESS_LOCAL_WRITE,
3129 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003130 if (!ibmr) {
3131 nes_debug(NES_DBG_CM, "Unable to register memory region"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303132 "for lSMM for cm_node = %p \n",
3133 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07003134 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303135 nesqp->private_data_len + nesqp->ietf_frame_size,
3136 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003137 return -ENOMEM;
3138 }
3139
3140 ibmr->pd = &nespd->ibpd;
3141 ibmr->device = nespd->ibpd.device;
3142 nesqp->lsmm_mr = ibmr;
3143
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303144 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003145 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303146 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
3147 u64temp);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003148 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3149 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303150 NES_IWARP_SQ_WQE_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003151 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303152 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003153 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303154 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3155 (u64)(unsigned long)(*start_buff));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003156 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303157 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003158 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08003159 if (nesqp->sq_kmapped) {
3160 nesqp->sq_kmapped = 0;
3161 kunmap(nesqp->page);
3162 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003163
Faisal Latif6492cdf2008-07-24 20:50:45 -07003164 nesqp->nesqp_context->ird_ord_sizes |=
3165 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303166 NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003167 } else {
3168 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08003169 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003170 }
3171 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003172
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003173 /* Cache the cm_id in the qp */
3174 nesqp->cm_id = cm_id;
3175 cm_node->cm_id = cm_id;
3176
3177 /* nesqp->cm_node = (void *)cm_id->provider_data; */
3178 cm_id->provider_data = nesqp;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303179 nesqp->active_conn = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003180
Faisal Latif6492cdf2008-07-24 20:50:45 -07003181 if (cm_node->state == NES_CM_STATE_TSA)
3182 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303183 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003184
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003185 nes_cm_init_tsa_conn(nesqp, cm_node);
3186
Steve Wise24d44a32013-07-04 16:10:44 +05303187 nesqp->nesqp_context->tcpPorts[0] = cpu_to_le16(ntohs(laddr->sin_port));
3188 nesqp->nesqp_context->tcpPorts[1] = cpu_to_le16(ntohs(raddr->sin_port));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003189
Steve Wise24d44a32013-07-04 16:10:44 +05303190 nesqp->nesqp_context->ip0 = cpu_to_le32(ntohl(raddr->sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003191
3192 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303193 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3194 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003195
Faisal Latif6492cdf2008-07-24 20:50:45 -07003196 nesqp->nesqp_context->arp_index_vlan |=
3197 cpu_to_le32(nes_arp_table(nesdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303198 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
3199 NES_ARP_RESOLVE) << 16);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003200
3201 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003202 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003203
3204 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3205
3206 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003207 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
3208 nesqp->nesqp_context->ird_ord_sizes |=
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003209 cpu_to_le32((u32)cm_node->ord_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003210
3211 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003212 nes_quad.DstIpAdrIndex =
3213 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
Steve Wise24d44a32013-07-04 16:10:44 +05303214 nes_quad.SrcIpadr = raddr->sin_addr.s_addr;
3215 nes_quad.TcpPorts[0] = raddr->sin_port;
3216 nes_quad.TcpPorts[1] = laddr->sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003217
3218 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003219 crc_value = get_crc_value(&nes_quad);
3220 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003221 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303222 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003223
3224 nesqp->hte_index &= adapter->hte_index_mask;
3225 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3226
3227 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3228
Faisal Latif6492cdf2008-07-24 20:50:45 -07003229 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303230 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
3231 "private data length=%u.\n", nesqp->hwqp.qp_id,
Steve Wise24d44a32013-07-04 16:10:44 +05303232 ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port),
3233 ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303234 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
3235 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
3236 buff_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003237
Coly Li73ac36e2009-01-07 18:09:16 -08003238 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003239 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07003240 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003241
3242 cm_event.event = IW_CM_EVENT_ESTABLISHED;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003243 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003244 cm_event.provider_data = (void *)nesqp;
3245 cm_event.local_addr = cm_id->local_addr;
3246 cm_event.remote_addr = cm_id->remote_addr;
3247 cm_event.private_data = NULL;
3248 cm_event.private_data_len = 0;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003249 cm_event.ird = cm_node->ird_size;
3250 cm_event.ord = cm_node->ord_size;
3251
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003252 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003253 attr.qp_state = IB_QPS_RTS;
3254 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003255 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003256 cm_node->loopbackpartner->mpa_frame_size =
3257 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003258 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003259 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303260 conn_param->private_data, conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003261 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
3262 }
3263 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003264 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303265 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003266
3267 return 0;
3268}
3269
3270
3271/**
3272 * nes_reject
3273 */
3274int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3275{
3276 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003277 struct nes_cm_node *loopback;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003278 struct nes_cm_core *cm_core;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303279 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003280
3281 atomic_inc(&cm_rejects);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303282 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003283 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003284 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003285 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003286
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303287 if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003288 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003289
Faisal Latif9d5ab132009-03-06 15:15:01 -08003290 if (loopback) {
3291 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3292 loopback->mpa_frame.priv_data_len = pdata_len;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303293 loopback->mpa_frame_size = pdata_len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003294 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303295 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
3296 cm_node->mpa_frame_size = pdata_len;
3297 memcpy(start_buff, pdata, pdata_len);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003298 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303299 return cm_core->api->reject(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003300}
3301
3302
3303/**
3304 * nes_connect
3305 * setup and launch cm connect node
3306 */
3307int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3308{
3309 struct ib_qp *ibqp;
3310 struct nes_qp *nesqp;
3311 struct nes_vnic *nesvnic;
3312 struct nes_device *nesdev;
3313 struct nes_cm_node *cm_node;
3314 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003315 int apbvt_set = 0;
Steve Wise24d44a32013-07-04 16:10:44 +05303316 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->local_addr;
3317 struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003318
Steve Wise24d44a32013-07-04 16:10:44 +05303319 if (cm_id->remote_addr.ss_family != AF_INET)
3320 return -ENOSYS;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003321 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3322 if (!ibqp)
3323 return -EINVAL;
3324 nesqp = to_nesqp(ibqp);
3325 if (!nesqp)
3326 return -EINVAL;
3327 nesvnic = to_nesvnic(nesqp->ibqp.device);
3328 if (!nesvnic)
3329 return -EINVAL;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303330 nesdev = nesvnic->nesdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003331 if (!nesdev)
3332 return -EINVAL;
3333
Steve Wise24d44a32013-07-04 16:10:44 +05303334 if (!laddr->sin_port || !raddr->sin_port)
Faisal Latifc5a7d482009-12-09 15:54:08 -08003335 return -EINVAL;
3336
Faisal Latif6492cdf2008-07-24 20:50:45 -07003337 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303338 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
Steve Wise24d44a32013-07-04 16:10:44 +05303339 ntohl(nesvnic->local_ipaddr), ntohl(raddr->sin_addr.s_addr),
3340 ntohs(raddr->sin_port), ntohl(laddr->sin_addr.s_addr),
3341 ntohs(laddr->sin_port));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003342
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003343 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003344 nesqp->active_conn = 1;
3345
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003346 /* cache the cm_id in the qp */
3347 nesqp->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003348 cm_id->provider_data = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003349 nesqp->private_data_len = conn_param->private_data_len;
Tatyana Nikolovad3e51322012-05-11 10:46:16 -05003350
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003351 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003352 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303353 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003354
Steve Wise24d44a32013-07-04 16:10:44 +05303355 if (laddr->sin_addr.s_addr != raddr->sin_addr.s_addr) {
3356 nes_manage_apbvt(nesvnic, ntohs(laddr->sin_port),
3357 PCI_FUNC(nesdev->pcidev->devfn),
3358 NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003359 apbvt_set = 1;
3360 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003361
3362 /* set up the connection params for the node */
Steve Wise24d44a32013-07-04 16:10:44 +05303363 cm_info.loc_addr = htonl(laddr->sin_addr.s_addr);
3364 cm_info.loc_port = htons(laddr->sin_port);
3365 cm_info.rem_addr = htonl(raddr->sin_addr.s_addr);
3366 cm_info.rem_port = htons(raddr->sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003367 cm_info.cm_id = cm_id;
3368 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3369
3370 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003371
3372 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003373 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303374 conn_param->private_data_len, (void *)conn_param->private_data,
3375 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003376 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003377 if (apbvt_set)
Steve Wise24d44a32013-07-04 16:10:44 +05303378 nes_manage_apbvt(nesvnic, ntohs(laddr->sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303379 PCI_FUNC(nesdev->pcidev->devfn),
3380 NES_MANAGE_APBVT_DEL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003381
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003382 cm_id->rem_ref(cm_id);
3383 return -ENOMEM;
3384 }
3385
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003386 record_ird_ord(cm_node, (u16)conn_param->ird, (u16)conn_param->ord);
3387 if (cm_node->send_rdma0_op == SEND_RDMA_READ_ZERO &&
3388 cm_node->ord_size == 0)
3389 cm_node->ord_size = 1;
3390
Faisal Latif53094c32009-04-27 13:37:34 -07003391 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003392 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003393 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003394 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003395
3396 return 0;
3397}
3398
3399
3400/**
3401 * nes_create_listen
3402 */
3403int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3404{
3405 struct nes_vnic *nesvnic;
3406 struct nes_cm_listener *cm_node;
3407 struct nes_cm_info cm_info;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003408 int err;
Steve Wise24d44a32013-07-04 16:10:44 +05303409 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->local_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003410
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003411 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303412 cm_id, ntohs(laddr->sin_port));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003413
Steve Wise24d44a32013-07-04 16:10:44 +05303414 if (cm_id->local_addr.ss_family != AF_INET)
3415 return -ENOSYS;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003416 nesvnic = to_nesvnic(cm_id->device);
3417 if (!nesvnic)
3418 return -EINVAL;
Roland Dreier69d51022010-08-04 14:25:40 -07003419
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003420 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3421 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3422
3423 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303424 nesvnic->local_ipaddr, laddr->sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003425
3426 /* setup listen params in our api call struct */
3427 cm_info.loc_addr = nesvnic->local_ipaddr;
Steve Wise24d44a32013-07-04 16:10:44 +05303428 cm_info.loc_port = laddr->sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003429 cm_info.backlog = backlog;
3430 cm_info.cm_id = cm_id;
3431
3432 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3433
3434
3435 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3436 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003437 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303438 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003439 return -ENOMEM;
3440 }
3441
3442 cm_id->provider_data = cm_node;
3443
3444 if (!cm_node->reused_node) {
Steve Wise24d44a32013-07-04 16:10:44 +05303445 err = nes_manage_apbvt(nesvnic, ntohs(laddr->sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303446 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3447 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003448 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003449 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303450 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003451 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3452 return err;
3453 }
Faisal Latif6e10d2e2010-02-12 19:55:03 +00003454 atomic_inc(&cm_listens_created);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003455 }
3456
3457 cm_id->add_ref(cm_id);
3458 cm_id->provider_data = (void *)cm_node;
3459
3460
3461 return 0;
3462}
3463
3464
3465/**
3466 * nes_destroy_listen
3467 */
3468int nes_destroy_listen(struct iw_cm_id *cm_id)
3469{
3470 if (cm_id->provider_data)
3471 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3472 else
3473 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3474
3475 cm_id->rem_ref(cm_id);
3476
3477 return 0;
3478}
3479
3480
3481/**
3482 * nes_cm_recv
3483 */
3484int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3485{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003486 int rc = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303487
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003488 cm_packets_received++;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303489 if ((g_cm_core) && (g_cm_core->api))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003490 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303491 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003492 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303493 " cm is not setup properly.\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003494
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003495 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003496}
3497
3498
3499/**
3500 * nes_cm_start
3501 * Start and init a cm core module
3502 */
3503int nes_cm_start(void)
3504{
3505 nes_debug(NES_DBG_CM, "\n");
3506 /* create the primary CM core, pass this handle to subsequent core inits */
3507 g_cm_core = nes_cm_alloc_core();
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303508 if (g_cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003509 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303510 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003511 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003512}
3513
3514
3515/**
3516 * nes_cm_stop
3517 * stop and dealloc all cm core instances
3518 */
3519int nes_cm_stop(void)
3520{
3521 g_cm_core->api->destroy_cm_core(g_cm_core);
3522 return 0;
3523}
3524
3525
3526/**
3527 * cm_event_connected
3528 * handle a connected event, setup QPs and HW
3529 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003530static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003531{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003532 struct nes_qp *nesqp;
3533 struct nes_vnic *nesvnic;
3534 struct nes_device *nesdev;
3535 struct nes_cm_node *cm_node;
3536 struct nes_adapter *nesadapter;
3537 struct ib_qp_attr attr;
3538 struct iw_cm_id *cm_id;
3539 struct iw_cm_event cm_event;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003540 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003541 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003542 int ret;
Steve Wise24d44a32013-07-04 16:10:44 +05303543 struct sockaddr_in *laddr;
3544 struct sockaddr_in *raddr;
3545 struct sockaddr_in *cm_event_laddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003546
3547 /* get all our handles */
3548 cm_node = event->cm_node;
3549 cm_id = cm_node->cm_id;
3550 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3551 nesqp = (struct nes_qp *)cm_id->provider_data;
3552 nesvnic = to_nesvnic(nesqp->ibqp.device);
3553 nesdev = nesvnic->nesdev;
3554 nesadapter = nesdev->nesadapter;
Steve Wise24d44a32013-07-04 16:10:44 +05303555 laddr = (struct sockaddr_in *)&cm_id->local_addr;
3556 raddr = (struct sockaddr_in *)&cm_id->remote_addr;
3557 cm_event_laddr = (struct sockaddr_in *)&cm_event.local_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003558
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303559 if (nesqp->destroyed)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003560 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003561 atomic_inc(&cm_connecteds);
3562 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303563 " local port 0x%04X. jiffies = %lu.\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303564 nesqp->hwqp.qp_id, ntohl(raddr->sin_addr.s_addr),
3565 ntohs(raddr->sin_port), ntohs(laddr->sin_port), jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003566
3567 nes_cm_init_tsa_conn(nesqp, cm_node);
3568
3569 /* set the QP tsa context */
Steve Wise24d44a32013-07-04 16:10:44 +05303570 nesqp->nesqp_context->tcpPorts[0] = cpu_to_le16(ntohs(laddr->sin_port));
3571 nesqp->nesqp_context->tcpPorts[1] = cpu_to_le16(ntohs(raddr->sin_port));
3572 nesqp->nesqp_context->ip0 = cpu_to_le32(ntohl(raddr->sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003573
3574 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003575 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3576 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003577 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003578 nes_arp_table(nesdev,
3579 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003580 NULL, NES_ARP_RESOLVE) << 16);
3581 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3582 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3583 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3584 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003585 cpu_to_le32((u32)1 <<
3586 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003587 nesqp->nesqp_context->ird_ord_sizes |=
3588 cpu_to_le32((u32)cm_node->ord_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003589
3590 /* Adjust tail for not having a LSMM */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303591 /*nesqp->hwqp.sq_tail = 1;*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003592
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303593 build_rdma0_msg(cm_node, &nesqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003594
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303595 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3596 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003597
3598 memset(&nes_quad, 0, sizeof(nes_quad));
3599
Faisal Latif6492cdf2008-07-24 20:50:45 -07003600 nes_quad.DstIpAdrIndex =
3601 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
Steve Wise24d44a32013-07-04 16:10:44 +05303602 nes_quad.SrcIpadr = raddr->sin_addr.s_addr;
3603 nes_quad.TcpPorts[0] = raddr->sin_port;
3604 nes_quad.TcpPorts[1] = laddr->sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003605
3606 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003607 crc_value = get_crc_value(&nes_quad);
3608 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003609 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303610 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003611
3612 nesqp->hte_index &= nesadapter->hte_index_mask;
3613 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3614
3615 nesqp->ietf_frame = &cm_node->mpa_frame;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303616 nesqp->private_data_len = (u8)cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003617 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3618
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003619 /* notify OF layer we successfully created the requested connection */
3620 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003621 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003622 cm_event.provider_data = cm_id->provider_data;
Steve Wise24d44a32013-07-04 16:10:44 +05303623 cm_event_laddr->sin_family = AF_INET;
3624 cm_event_laddr->sin_port = laddr->sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003625 cm_event.remote_addr = cm_id->remote_addr;
3626
Faisal Latif6492cdf2008-07-24 20:50:45 -07003627 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303628 cm_event.private_data_len = (u8)event->cm_node->mpa_frame_size;
3629 cm_event.ird = cm_node->ird_size;
3630 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003631
Steve Wise24d44a32013-07-04 16:10:44 +05303632 cm_event_laddr->sin_addr.s_addr = event->cm_info.rem_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003633 ret = cm_id->event_handler(cm_id, &cm_event);
3634 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3635
3636 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003637 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303638 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003639 attr.qp_state = IB_QPS_RTS;
3640 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003641
Faisal Latif6492cdf2008-07-24 20:50:45 -07003642 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303643 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003644
3645 return;
3646}
3647
3648
3649/**
3650 * cm_event_connect_error
3651 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003652static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003653{
3654 struct nes_qp *nesqp;
3655 struct iw_cm_id *cm_id;
3656 struct iw_cm_event cm_event;
3657 /* struct nes_cm_info cm_info; */
3658 int ret;
3659
3660 if (!event->cm_node)
3661 return;
3662
3663 cm_id = event->cm_node->cm_id;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303664 if (!cm_id)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003665 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003666
3667 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3668 nesqp = cm_id->provider_data;
3669
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303670 if (!nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003671 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003672
3673 /* notify OF layer about this connection error event */
3674 /* cm_id->rem_ref(cm_id); */
3675 nesqp->cm_id = NULL;
3676 cm_id->provider_data = NULL;
3677 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003678 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003679 cm_event.provider_data = cm_id->provider_data;
3680 cm_event.local_addr = cm_id->local_addr;
3681 cm_event.remote_addr = cm_id->remote_addr;
3682 cm_event.private_data = NULL;
3683 cm_event.private_data_len = 0;
3684
Steve Wise24d44a32013-07-04 16:10:44 +05303685#ifdef CONFIG_INFINIBAND_NES_DEBUG
3686 {
3687 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3688 &cm_event.local_addr;
3689 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3690 &cm_event.remote_addr;
3691 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, remote_addr=%08x\n",
3692 cm_event_laddr->sin_addr.s_addr, cm_event_raddr->sin_addr.s_addr);
3693 }
3694#endif
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003695
3696 ret = cm_id->event_handler(cm_id, &cm_event);
3697 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3698 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003699 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303700 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003701 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003702
Faisal Latif6492cdf2008-07-24 20:50:45 -07003703 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003704 return;
3705}
3706
3707
3708/**
3709 * cm_event_reset
3710 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003711static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003712{
3713 struct nes_qp *nesqp;
3714 struct iw_cm_id *cm_id;
3715 struct iw_cm_event cm_event;
3716 /* struct nes_cm_info cm_info; */
3717 int ret;
3718
3719 if (!event->cm_node)
3720 return;
3721
3722 if (!event->cm_node->cm_id)
3723 return;
3724
3725 cm_id = event->cm_node->cm_id;
3726
3727 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3728 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003729 if (!nesqp)
3730 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003731
3732 nesqp->cm_id = NULL;
3733 /* cm_id->provider_data = NULL; */
3734 cm_event.event = IW_CM_EVENT_DISCONNECT;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003735 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003736 cm_event.provider_data = cm_id->provider_data;
3737 cm_event.local_addr = cm_id->local_addr;
3738 cm_event.remote_addr = cm_id->remote_addr;
3739 cm_event.private_data = NULL;
3740 cm_event.private_data_len = 0;
3741
Faisal Latif183ecfa2008-11-21 20:50:46 -06003742 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003743 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003744 atomic_inc(&cm_closes);
3745 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003746 cm_event.status = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003747 cm_event.provider_data = cm_id->provider_data;
3748 cm_event.local_addr = cm_id->local_addr;
3749 cm_event.remote_addr = cm_id->remote_addr;
3750 cm_event.private_data = NULL;
3751 cm_event.private_data_len = 0;
3752 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3753 ret = cm_id->event_handler(cm_id, &cm_event);
3754
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003755 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3756
3757
3758 /* notify OF layer about this connection error event */
3759 cm_id->rem_ref(cm_id);
3760
3761 return;
3762}
3763
3764
3765/**
3766 * cm_event_mpa_req
3767 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003768static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003769{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303770 struct iw_cm_id *cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003771 struct iw_cm_event cm_event;
3772 int ret;
3773 struct nes_cm_node *cm_node;
Steve Wise24d44a32013-07-04 16:10:44 +05303774 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3775 &cm_event.local_addr;
3776 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3777 &cm_event.remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003778
3779 cm_node = event->cm_node;
3780 if (!cm_node)
3781 return;
3782 cm_id = cm_node->cm_id;
3783
3784 atomic_inc(&cm_connect_reqs);
3785 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303786 cm_node, cm_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003787
3788 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003789 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003790 cm_event.provider_data = (void *)cm_node;
3791
Steve Wise24d44a32013-07-04 16:10:44 +05303792 cm_event_laddr->sin_family = AF_INET;
3793 cm_event_laddr->sin_port = htons(event->cm_info.loc_port);
3794 cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003795
Steve Wise24d44a32013-07-04 16:10:44 +05303796 cm_event_raddr->sin_family = AF_INET;
3797 cm_event_raddr->sin_port = htons(event->cm_info.rem_port);
3798 cm_event_raddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003799 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303800 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003801 if (cm_node->mpa_frame_rev == IETF_MPA_V1) {
3802 cm_event.ird = NES_MAX_IRD;
3803 cm_event.ord = NES_MAX_ORD;
3804 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303805 cm_event.ird = cm_node->ird_size;
3806 cm_event.ord = cm_node->ord_size;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003807 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003808
3809 ret = cm_id->event_handler(cm_id, &cm_event);
3810 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003811 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303812 __func__, __LINE__, ret);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003813 return;
3814}
3815
3816
3817static void cm_event_mpa_reject(struct nes_cm_event *event)
3818{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303819 struct iw_cm_id *cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003820 struct iw_cm_event cm_event;
3821 struct nes_cm_node *cm_node;
3822 int ret;
Steve Wise24d44a32013-07-04 16:10:44 +05303823 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3824 &cm_event.local_addr;
3825 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3826 &cm_event.remote_addr;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003827
3828 cm_node = event->cm_node;
3829 if (!cm_node)
3830 return;
3831 cm_id = cm_node->cm_id;
3832
3833 atomic_inc(&cm_connect_reqs);
3834 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303835 cm_node, cm_id, jiffies);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003836
3837 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3838 cm_event.status = -ECONNREFUSED;
3839 cm_event.provider_data = cm_id->provider_data;
3840
Steve Wise24d44a32013-07-04 16:10:44 +05303841 cm_event_laddr->sin_family = AF_INET;
3842 cm_event_laddr->sin_port = htons(event->cm_info.loc_port);
3843 cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003844
Steve Wise24d44a32013-07-04 16:10:44 +05303845 cm_event_raddr->sin_family = AF_INET;
3846 cm_event_raddr->sin_port = htons(event->cm_info.rem_port);
3847 cm_event_raddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003848
3849 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303850 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003851
3852 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303853 "remove_addr=%08x\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303854 cm_event_laddr->sin_addr.s_addr,
3855 cm_event_raddr->sin_addr.s_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003856
3857 ret = cm_id->event_handler(cm_id, &cm_event);
3858 if (ret)
3859 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303860 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003861
3862 return;
3863}
3864
3865
3866static void nes_cm_event_handler(struct work_struct *);
3867
3868/**
3869 * nes_cm_post_event
3870 * post an event to the cm event handler
3871 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003872static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003873{
3874 atomic_inc(&event->cm_node->cm_core->events_posted);
3875 add_ref_cm_node(event->cm_node);
3876 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3877 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003878 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303879 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003880
3881 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3882
3883 nes_debug(NES_DBG_CM, "Exit\n");
3884 return 0;
3885}
3886
3887
3888/**
3889 * nes_cm_event_handler
3890 * worker function to handle cm events
3891 * will free instance of nes_cm_event
3892 */
3893static void nes_cm_event_handler(struct work_struct *work)
3894{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003895 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303896 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003897 struct nes_cm_core *cm_core;
3898
Faisal Latif6492cdf2008-07-24 20:50:45 -07003899 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003900 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003901
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003902 cm_core = event->cm_node->cm_core;
3903 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303904 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003905
3906 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003907 case NES_CM_EVENT_MPA_REQ:
3908 cm_event_mpa_req(event);
3909 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303910 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003911 break;
3912 case NES_CM_EVENT_RESET:
3913 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303914 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003915 cm_event_reset(event);
3916 break;
3917 case NES_CM_EVENT_CONNECTED:
3918 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303919 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003920 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003921 cm_event_connected(event);
3922 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3923 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003924 case NES_CM_EVENT_MPA_REJECT:
3925 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303926 (event->cm_node->state == NES_CM_STATE_TSA))
Faisal Latif9d5ab132009-03-06 15:15:01 -08003927 break;
3928 cm_event_mpa_reject(event);
3929 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3930 break;
3931
Faisal Latif6492cdf2008-07-24 20:50:45 -07003932 case NES_CM_EVENT_ABORTED:
3933 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303934 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003935 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003936 cm_event_connect_error(event);
3937 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3938 break;
3939 case NES_CM_EVENT_DROPPED_PKT:
3940 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3941 break;
3942 default:
3943 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3944 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003945 }
3946
3947 atomic_dec(&cm_core->events_posted);
3948 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3949 rem_ref_cm_node(cm_core, event->cm_node);
3950 kfree(event);
3951
3952 return;
3953}