blob: de4025deaa4ad384db5b86f15165aa991dcb45b4 [file] [log] [blame]
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001/*
Tatyana Nikolova56472632014-03-26 17:07:57 -05002 * Copyright (c) 2006 - 2014 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>
Tatyana Nikolova56472632014-03-26 17:07:57 -050062#include <linux/fcntl.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080063
64#include "nes.h"
65
66u32 cm_packets_sent;
67u32 cm_packets_bounced;
68u32 cm_packets_dropped;
69u32 cm_packets_retrans;
70u32 cm_packets_created;
71u32 cm_packets_received;
Faisal Latif6e10d2e2010-02-12 19:55:03 +000072atomic_t cm_listens_created;
73atomic_t cm_listens_destroyed;
Glenn Streiff3c2d7742008-02-04 20:20:45 -080074u32 cm_backlog_drops;
75atomic_t cm_loopbacks;
76atomic_t cm_nodes_created;
77atomic_t cm_nodes_destroyed;
78atomic_t cm_accel_dropped_pkts;
79atomic_t cm_resets_recvd;
80
Tatyana Nikolova615eb712011-09-25 20:17:46 +053081static inline int mini_cm_accelerated(struct nes_cm_core *, struct nes_cm_node *);
82static 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 -080083static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
Tatyana Nikolova615eb712011-09-25 20:17:46 +053084static 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 -070085static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Tatyana Nikolova615eb712011-09-25 20:17:46 +053086static int mini_cm_accept(struct nes_cm_core *, struct nes_cm_node *);
87static int mini_cm_reject(struct nes_cm_core *, struct nes_cm_node *);
88static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *, struct sk_buff *);
Roland Dreier1a855fbf2008-04-16 21:01:09 -070089static int mini_cm_dealloc_core(struct nes_cm_core *);
90static int mini_cm_get(struct nes_cm_core *);
91static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070092
Tatyana Nikolova615eb712011-09-25 20:17:46 +053093static void form_cm_frame(struct sk_buff *, struct nes_cm_node *, void *, u32, void *, u32, u8);
Faisal Latif6492cdf2008-07-24 20:50:45 -070094static int add_ref_cm_node(struct nes_cm_node *);
95static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
96
Roland Dreier1a855fbf2008-04-16 21:01:09 -070097static int nes_cm_disconn_true(struct nes_qp *);
98static int nes_cm_post_event(struct nes_cm_event *event);
99static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
100static void nes_disconnect_worker(struct work_struct *work);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700101
102static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800103static int send_mpa_reject(struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700104static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
105static int send_reset(struct nes_cm_node *, struct sk_buff *);
106static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fbf2008-04-16 21:01:09 -0700107static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530108static void process_packet(struct nes_cm_node *, struct sk_buff *, struct nes_cm_core *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700109
110static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
111static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
112static void cleanup_retrans_entry(struct nes_cm_node *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800113static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700114static void free_retrans_entry(struct nes_cm_node *cm_node);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530115static 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 -0700116
117/* CM event handler functions */
118static void cm_event_connected(struct nes_cm_event *);
119static void cm_event_connect_error(struct nes_cm_event *);
120static void cm_event_reset(struct nes_cm_event *);
121static void cm_event_mpa_req(struct nes_cm_event *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800122static void cm_event_mpa_reject(struct nes_cm_event *);
123static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700124
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530125/* MPA build functions */
126static int cm_build_mpa_frame(struct nes_cm_node *, u8 **, u16 *, u8 *, u8);
127static void build_mpa_v2(struct nes_cm_node *, void *, u8);
128static void build_mpa_v1(struct nes_cm_node *, void *, u8);
129static void build_rdma0_msg(struct nes_cm_node *, struct nes_qp **);
130
Faisal Latif6492cdf2008-07-24 20:50:45 -0700131static void print_core(struct nes_cm_core *core);
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -0500132static void record_ird_ord(struct nes_cm_node *, u16, u16);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800133
134/* External CM API Interface */
135/* instance of function pointers for client API */
136/* set address of this instance to cm_core->cm_ops at cm_core alloc */
Julia Lawallc4198742015-11-28 15:00:37 +0100137static const struct nes_cm_ops nes_cm_api = {
Kees Cook6554c9f2016-12-16 17:03:16 -0800138 .accelerated = mini_cm_accelerated,
139 .listen = mini_cm_listen,
140 .stop_listener = mini_cm_del_listen,
141 .connect = mini_cm_connect,
142 .close = mini_cm_close,
143 .accept = mini_cm_accept,
144 .reject = mini_cm_reject,
145 .recv_pkt = mini_cm_recv_pkt,
146 .destroy_cm_core = mini_cm_dealloc_core,
147 .get = mini_cm_get,
148 .set = mini_cm_set
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800149};
150
Roland Dreier1a855fbf2008-04-16 21:01:09 -0700151static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800152
153atomic_t cm_connects;
154atomic_t cm_accepts;
155atomic_t cm_disconnects;
156atomic_t cm_closes;
157atomic_t cm_connecteds;
158atomic_t cm_connect_reqs;
159atomic_t cm_rejects;
160
Faisal Latif0f0bee82011-09-25 20:34:00 -0500161int nes_add_ref_cm_node(struct nes_cm_node *cm_node)
162{
163 return add_ref_cm_node(cm_node);
164}
165
166int nes_rem_ref_cm_node(struct nes_cm_node *cm_node)
167{
168 return rem_ref_cm_node(cm_node->cm_core, cm_node);
169}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800170/**
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
Faisal Latif6a0dde82016-02-26 09:18:02 -0600485 iph->saddr = htonl(cm_node->loc_addr);
486 iph->daddr = htonl(cm_node->rem_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800487
Faisal Latif6a0dde82016-02-26 09:18:02 -0600488 tcph->source = htons(cm_node->loc_port);
489 tcph->dest = htons(cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800490 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;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530613
614 switch (mpa_key) {
615 case MPA_KEY_REQUEST:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800616 ctrl_ord |= IETF_RDMA0_WRITE;
617 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530618 break;
619 case MPA_KEY_REPLY:
620 switch (cm_node->send_rdma0_op) {
621 case SEND_RDMA_WRITE_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800622 ctrl_ord |= IETF_RDMA0_WRITE;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530623 break;
624 case SEND_RDMA_READ_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800625 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530626 break;
627 }
628 }
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800629 rtr_msg->ctrl_ird = htons(ctrl_ird);
630 rtr_msg->ctrl_ord = htons(ctrl_ord);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530631}
632
633/**
634 * build_mpa_v1 - build a MPA V1 frame
635 */
636static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
637{
638 struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
639
640 switch (mpa_key) {
641 case MPA_KEY_REQUEST:
642 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
643 break;
644 case MPA_KEY_REPLY:
645 memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
646 break;
647 }
648 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
649 mpa_frame->rev = cm_node->mpa_frame_rev;
650 mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
651}
652
653static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
654{
655 u64 u64temp;
656 struct nes_qp *nesqp = *nesqp_addr;
657 struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
658
Tatyana Nikolova43adff32014-03-11 14:20:17 -0500659 u64temp = (unsigned long)nesqp->nesuqp_addr;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530660 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
661 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
662
663 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
664 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
665
666 switch (cm_node->send_rdma0_op) {
667 case SEND_RDMA_WRITE_ZERO:
668 nes_debug(NES_DBG_CM, "Sending first write.\n");
669 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
670 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
671 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
672 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
673 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
674 break;
675
676 case SEND_RDMA_READ_ZERO:
677 default:
Julia Lawall079abea2012-11-03 10:58:37 +0000678 if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO)
679 WARN(1, "Unsupported RDMA0 len operation=%u\n",
680 cm_node->send_rdma0_op);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530681 nes_debug(NES_DBG_CM, "Sending first rdma operation.\n");
682 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
683 cpu_to_le32(NES_IWARP_SQ_OP_RDMAR);
684 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX] = 1;
685 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_HIGH_IDX] = 0;
686 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] = 0;
687 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_STAG_IDX] = 1;
688 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 1;
689 break;
690 }
691
692 if (nesqp->sq_kmapped) {
693 nesqp->sq_kmapped = 0;
694 kunmap(nesqp->page);
695 }
696
697 /*use the reserved spot on the WQ for the extra first WQE*/
698 nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
699 NES_QPCONTEXT_ORDIRD_WRPDU |
700 NES_QPCONTEXT_ORDIRD_ALSMM));
701 nesqp->skip_lsmm = 1;
702 nesqp->hwqp.sq_tail = 0;
703}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800704
705/**
706 * schedule_nes_timer
707 * note - cm_node needs to be protected before calling this. Encase in:
708 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
709 */
710int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530711 enum nes_timer_type type, int send_retrans,
712 int close_when_complete)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800713{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530714 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700715 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800716 struct nes_timer_entry *new_send;
717 int ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800718
719 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
720 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800721 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800722
723 /* new_send->timetosend = currenttime */
724 new_send->retrycount = NES_DEFAULT_RETRYS;
725 new_send->retranscount = NES_DEFAULT_RETRANS;
726 new_send->skb = skb;
727 new_send->timetosend = jiffies;
728 new_send->type = type;
729 new_send->netdev = cm_node->netdev;
730 new_send->send_retrans = send_retrans;
731 new_send->close_when_complete = close_when_complete;
732
733 if (type == NES_TIMER_TYPE_CLOSE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530734 new_send->timetosend += (HZ / 10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800735 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700736 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800737 WARN_ON(1);
738 return -EINVAL;
739 }
740 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800741 }
742
743 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700744 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Reshetova, Elena63354792017-06-30 13:07:58 +0300745 refcount_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700746 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
747 cm_node->send_entry = new_send;
748 add_ref_cm_node(cm_node);
749 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
750 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800751
752 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
753 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700754 nes_debug(NES_DBG_CM, "Error sending packet %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530755 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800756 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700757 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800758 } else {
759 cm_packets_sent++;
760 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700761 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800762 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700763 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800764 return ret;
765 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800766 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800767 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800768
Tatyana Nikolova00ad2552012-12-06 19:56:31 +0000769 if (!timer_pending(&cm_core->tcp_timer))
770 mod_timer(&cm_core->tcp_timer, new_send->timetosend);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800771
772 return ret;
773}
774
Faisal Latif9d5ab132009-03-06 15:15:01 -0800775static void nes_retrans_expired(struct nes_cm_node *cm_node)
776{
Faisal Latif68237a02009-06-22 22:53:28 -0700777 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latifdae58722010-08-14 21:04:56 +0000778 enum nes_cm_node_state state = cm_node->state;
779 cm_node->state = NES_CM_STATE_CLOSED;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530780
Faisal Latifdae58722010-08-14 21:04:56 +0000781 switch (state) {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800782 case NES_CM_STATE_SYN_RCVD:
783 case NES_CM_STATE_CLOSING:
784 rem_ref_cm_node(cm_node->cm_core, cm_node);
785 break;
786 case NES_CM_STATE_LAST_ACK:
787 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700788 if (cm_node->cm_id)
789 cm_id->rem_ref(cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800790 send_reset(cm_node, NULL);
791 break;
792 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800793 add_ref_cm_node(cm_node);
794 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800795 create_event(cm_node, NES_CM_EVENT_ABORTED);
796 }
797}
798
799static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
800{
801 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
802 struct iw_cm_id *cm_id = cm_node->cm_id;
803 struct nes_qp *nesqp;
804 unsigned long qplockflags;
805
806 if (!recv_entry)
807 return;
808 nesqp = (struct nes_qp *)recv_entry->skb;
809 if (nesqp) {
810 spin_lock_irqsave(&nesqp->lock, qplockflags);
811 if (nesqp->cm_id) {
812 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530813 "refcount = %d: HIT A "
814 "NES_TIMER_TYPE_CLOSE with something "
815 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
816 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800817 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
818 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
819 nesqp->ibqp_state = IB_QPS_ERR;
820 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
821 nes_cm_disconn(nesqp);
822 } else {
823 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
824 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530825 "refcount = %d: HIT A "
826 "NES_TIMER_TYPE_CLOSE with nothing "
827 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
828 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800829 }
830 } else if (rem_node) {
831 /* TIME_WAIT state */
832 rem_ref_cm_node(cm_node->cm_core, cm_node);
833 }
834 if (cm_node->cm_id)
835 cm_id->rem_ref(cm_id);
836 kfree(recv_entry);
837 cm_node->recv_entry = NULL;
838}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800839
840/**
841 * nes_cm_timer_tick
842 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -0700843static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800844{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800845 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800846 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800847 struct nes_cm_node *cm_node;
848 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800849 struct list_head *list_core_temp;
850 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800851 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800852 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700853 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800854 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800855
Faisal Latif879e5bd2008-11-21 20:50:41 -0600856 struct list_head timer_list;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530857
Faisal Latif879e5bd2008-11-21 20:50:41 -0600858 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800859 spin_lock_irqsave(&cm_core->ht_lock, flags);
860
Faisal Latif6492cdf2008-07-24 20:50:45 -0700861 list_for_each_safe(list_node, list_core_temp,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530862 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800863 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800864 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600865 add_ref_cm_node(cm_node);
866 list_add(&cm_node->timer_entry, &timer_list);
867 }
868 }
869 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
870
871 list_for_each_safe(list_node, list_core_temp, &timer_list) {
872 cm_node = container_of(list_node, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530873 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800874 recv_entry = cm_node->recv_entry;
875
876 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700877 if (time_after(recv_entry->timetosend, jiffies)) {
878 if (nexttimeout > recv_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530879 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800880 nexttimeout = recv_entry->timetosend;
881 settimer = 1;
882 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530883 } else {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800884 handle_recv_entry(cm_node, 1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530885 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800886 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800887
888 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700889 do {
890 send_entry = cm_node->send_entry;
891 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600892 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800893 if (time_after(send_entry->timetosend, jiffies)) {
894 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700895 if ((nexttimeout >
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530896 send_entry->timetosend) ||
897 !settimer) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700898 nexttimeout =
899 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800900 settimer = 1;
901 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800902 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700903 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800904 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800905 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800906 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700907
908 if ((cm_node->state == NES_CM_STATE_TSA) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530909 (cm_node->state == NES_CM_STATE_CLOSED)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700910 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600911 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800912 }
913
Faisal Latif6492cdf2008-07-24 20:50:45 -0700914 if (!send_entry->retranscount ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530915 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800916 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700917 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800918
Faisal Latif6492cdf2008-07-24 20:50:45 -0700919 spin_unlock_irqrestore(
920 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800921 nes_retrans_expired(cm_node);
922 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700923 spin_lock_irqsave(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530924 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600925 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800926 }
Reshetova, Elena63354792017-06-30 13:07:58 +0300927 refcount_inc(&send_entry->skb->users);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800928 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700929 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530930 "for node %p, jiffies = %lu, time to send = "
931 "%lu, retranscount = %u, send_entry->seq_num = "
932 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
933 "0x%08X\n", send_entry, cm_node, jiffies,
934 send_entry->timetosend,
935 send_entry->retranscount,
936 send_entry->seq_num,
937 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800938
Faisal Latif6492cdf2008-07-24 20:50:45 -0700939 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530940 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800941 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700942 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800943 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700944 nes_debug(NES_DBG_CM, "rexmit failed for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530945 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800946 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800947 send_entry->retrycount--;
948 nexttimeout = jiffies + NES_SHORT_TIME;
949 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600950 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800951 } else {
952 cm_packets_sent++;
953 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700954 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530955 "%u, retry count = %u.\n",
956 send_entry->retranscount,
957 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800958 if (send_entry->send_retrans) {
959 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700960 timetosend = (NES_RETRY_TIMEOUT <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530961 (NES_DEFAULT_RETRANS - send_entry->retranscount));
Faisal Latif4e9c3902009-04-27 13:39:36 -0700962
Faisal Latif6492cdf2008-07-24 20:50:45 -0700963 send_entry->timetosend = jiffies +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530964 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700965 if (nexttimeout > send_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530966 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800967 nexttimeout = send_entry->timetosend;
968 settimer = 1;
969 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800970 } else {
971 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700972 close_when_complete =
973 send_entry->close_when_complete;
974 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530975 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700976 free_retrans_entry(cm_node);
977 if (close_when_complete)
978 rem_ref_cm_node(cm_node->cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530979 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800980 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700981 } while (0);
982
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800983 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700984 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800985 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800986
987 if (settimer) {
Tatyana Nikolova00ad2552012-12-06 19:56:31 +0000988 if (!timer_pending(&cm_core->tcp_timer))
989 mod_timer(&cm_core->tcp_timer, nexttimeout);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800990 }
991}
992
993
994/**
995 * send_syn
996 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700997static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530998 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800999{
1000 int ret;
1001 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001002 char optionsbuffer[sizeof(struct option_mss) +
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301003 sizeof(struct option_windowscale) + sizeof(struct option_base) +
1004 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001005
1006 int optionssize = 0;
1007 /* Sending MSS option */
1008 union all_known_options *options;
1009
1010 if (!cm_node)
1011 return -EINVAL;
1012
1013 options = (union all_known_options *)&optionsbuffer[optionssize];
1014 options->as_mss.optionnum = OPTION_NUMBER_MSS;
1015 options->as_mss.length = sizeof(struct option_mss);
1016 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
1017 optionssize += sizeof(struct option_mss);
1018
1019 options = (union all_known_options *)&optionsbuffer[optionssize];
1020 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
1021 options->as_windowscale.length = sizeof(struct option_windowscale);
1022 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
1023 optionssize += sizeof(struct option_windowscale);
1024
Faisal Latif6492cdf2008-07-24 20:50:45 -07001025 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001026 options = (union all_known_options *)&optionsbuffer[optionssize];
1027 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
1028 options->as_base.length = sizeof(struct option_base);
1029 optionssize += sizeof(struct option_base);
1030 /* we need the size to be a multiple of 4 */
1031 options = (union all_known_options *)&optionsbuffer[optionssize];
1032 options->as_end = 1;
1033 optionssize += 1;
1034 options = (union all_known_options *)&optionsbuffer[optionssize];
1035 options->as_end = 1;
1036 optionssize += 1;
1037 }
1038
1039 options = (union all_known_options *)&optionsbuffer[optionssize];
1040 options->as_end = OPTION_NUMBER_END;
1041 optionssize += 1;
1042
Faisal Latif6492cdf2008-07-24 20:50:45 -07001043 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001044 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001045 if (!skb) {
1046 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1047 return -1;
1048 }
1049
1050 if (sendack)
1051 flags |= SET_ACK;
1052
1053 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
1054 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1055
1056 return ret;
1057}
1058
1059
1060/**
1061 * send_reset
1062 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001063static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001064{
1065 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001066 int flags = SET_RST | SET_ACK;
1067
Faisal Latif6492cdf2008-07-24 20:50:45 -07001068 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001069 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001070 if (!skb) {
1071 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001072 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001073 }
1074
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001075 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
1076 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
1077
1078 return ret;
1079}
1080
1081
1082/**
1083 * send_ack
1084 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001085static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001086{
1087 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001088
1089 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001090 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001091
1092 if (!skb) {
1093 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1094 return -1;
1095 }
1096
1097 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
1098 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
1099
1100 return ret;
1101}
1102
1103
1104/**
1105 * send_fin
1106 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07001107static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001108{
1109 int ret;
1110
1111 /* if we didn't get a frame get one */
1112 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001113 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001114
1115 if (!skb) {
1116 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1117 return -1;
1118 }
1119
1120 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
1121 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1122
1123 return ret;
1124}
1125
1126
1127/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001128 * find_node - find a cm node that matches the reference cm node
1129 */
1130static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301131 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001132{
1133 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001134 struct list_head *hte;
1135 struct nes_cm_node *cm_node;
1136
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001137 /* get a handle on the hte */
1138 hte = &cm_core->connected_nodes;
1139
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001140 /* walk list and find cm_node associated with this session ID */
1141 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001142 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001143 /* compare quad, return node handle if a match */
1144 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301145 cm_node->loc_addr, cm_node->loc_port,
1146 loc_addr, loc_port,
1147 cm_node->rem_addr, cm_node->rem_port,
1148 rem_addr, rem_port);
Faisal Latif6a0dde82016-02-26 09:18:02 -06001149 if ((cm_node->loc_addr == loc_addr) &&
1150 (cm_node->loc_port == loc_port) &&
1151 (cm_node->rem_addr == rem_addr) &&
1152 (cm_node->rem_port == rem_port)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001153 add_ref_cm_node(cm_node);
1154 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1155 return cm_node;
1156 }
1157 }
1158 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1159
1160 /* no owner node */
1161 return NULL;
1162}
1163
1164
1165/**
1166 * find_listener - find a cm node listening on this addr-port pair
1167 */
1168static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
Faisal Latif6a0dde82016-02-26 09:18:02 -06001169 nes_addr_t dst_addr, u16 dst_port,
1170 enum nes_cm_listener_state listener_state)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001171{
1172 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001173 struct nes_cm_listener *listen_node;
Tatyana Nikolova56472632014-03-26 17:07:57 -05001174 nes_addr_t listen_addr;
1175 u16 listen_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001176
1177 /* walk list and find cm_node associated with this session ID */
1178 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001179 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Faisal Latif6a0dde82016-02-26 09:18:02 -06001180 listen_addr = listen_node->loc_addr;
1181 listen_port = listen_node->loc_port;
1182
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001183 /* compare node pair, return node handle if a match */
Tatyana Nikolova56472632014-03-26 17:07:57 -05001184 if (((listen_addr == dst_addr) ||
1185 listen_addr == 0x00000000) &&
1186 (listen_port == dst_port) &&
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301187 (listener_state & listen_node->listener_state)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001188 atomic_inc(&listen_node->ref_count);
1189 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1190 return listen_node;
1191 }
1192 }
1193 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1194
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001195 /* no listener */
1196 return NULL;
1197}
1198
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001199/**
1200 * add_hte_node - add a cm node to the hash table
1201 */
1202static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1203{
1204 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001205 struct list_head *hte;
1206
1207 if (!cm_node || !cm_core)
1208 return -EINVAL;
1209
Faisal Latif6492cdf2008-07-24 20:50:45 -07001210 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301211 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001212
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001213 spin_lock_irqsave(&cm_core->ht_lock, flags);
1214
1215 /* get a handle on the hash table element (list head for this slot) */
1216 hte = &cm_core->connected_nodes;
1217 list_add_tail(&cm_node->list, hte);
1218 atomic_inc(&cm_core->ht_node_cnt);
1219
1220 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1221
1222 return 0;
1223}
1224
1225
1226/**
1227 * mini_cm_dec_refcnt_listen
1228 */
1229static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301230 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001231{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001232 int ret = -EINVAL;
1233 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001234 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001235 struct list_head *list_pos = NULL;
1236 struct list_head *list_temp = NULL;
1237 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -06001238 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001239
1240 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301241 "refcnt=%d\n", listener, free_hanging_nodes,
1242 atomic_read(&listener->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001243 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -06001244 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001245 if (free_hanging_nodes) {
1246 spin_lock_irqsave(&cm_core->ht_lock, flags);
1247 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -06001248 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001249 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301250 list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001251 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -06001252 (!cm_node->accelerated)) {
1253 add_ref_cm_node(cm_node);
1254 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001255 }
1256 }
1257 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1258 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001259
1260 list_for_each_safe(list_pos, list_temp, &reset_list) {
1261 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301262 reset_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001263 {
1264 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -08001265 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001266 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
1267 rem_ref_cm_node(cm_node->cm_core, cm_node);
1268 } else {
1269 if (!loopback) {
1270 cleanup_retrans_entry(cm_node);
1271 err = send_reset(cm_node, NULL);
1272 if (err) {
1273 cm_node->state =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301274 NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001275 WARN_ON(1);
1276 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08001277 old_state = cm_node->state;
1278 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1279 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1280 rem_ref_cm_node(
1281 cm_node->cm_core,
1282 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001283 }
1284 } else {
1285 struct nes_cm_event event;
1286
1287 event.cm_node = loopback;
1288 event.cm_info.rem_addr =
1289 loopback->rem_addr;
1290 event.cm_info.loc_addr =
1291 loopback->loc_addr;
1292 event.cm_info.rem_port =
1293 loopback->rem_port;
1294 event.cm_info.loc_port =
1295 loopback->loc_port;
1296 event.cm_info.cm_id = loopback->cm_id;
Faisal Latif43093b92010-02-12 19:57:04 +00001297 add_ref_cm_node(loopback);
1298 loopback->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001299 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001300 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001301
Faisal Latif9d5ab132009-03-06 15:15:01 -08001302 rem_ref_cm_node(cm_node->cm_core,
1303 cm_node);
1304
1305 }
1306 }
1307 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001308 }
1309
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001310 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1311 if (!atomic_dec_return(&listener->ref_count)) {
1312 list_del(&listener->list);
1313
1314 /* decrement our listen node count */
1315 atomic_dec(&cm_core->listen_node_cnt);
1316
1317 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1318
Tatyana Nikolova56472632014-03-26 17:07:57 -05001319 if (listener->nesvnic) {
1320 nes_manage_apbvt(listener->nesvnic,
Faisal Latif6a0dde82016-02-26 09:18:02 -06001321 listener->loc_port,
Tatyana Nikolova56472632014-03-26 17:07:57 -05001322 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn),
1323 NES_MANAGE_APBVT_DEL);
1324
Tatyana Nikolova56472632014-03-26 17:07:57 -05001325 nes_debug(NES_DBG_NLMSG,
Faisal Latif6a0dde82016-02-26 09:18:02 -06001326 "Delete APBVT loc_port = %04X\n",
1327 listener->loc_port);
Tatyana Nikolova56472632014-03-26 17:07:57 -05001328 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001329
1330 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1331
1332 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001333 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001334 ret = 0;
Faisal Latif6e10d2e2010-02-12 19:55:03 +00001335 atomic_inc(&cm_listens_destroyed);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001336 } else {
1337 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1338 }
1339 if (listener) {
1340 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1341 nes_debug(NES_DBG_CM, "destroying listener (%p)"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301342 " with non-zero pending accepts=%u\n",
1343 listener, atomic_read(&listener->pend_accepts_cnt));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001344 }
1345
1346 return ret;
1347}
1348
1349
1350/**
1351 * mini_cm_del_listen
1352 */
1353static int mini_cm_del_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301354 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001355{
1356 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1357 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1358 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1359}
1360
1361
1362/**
1363 * mini_cm_accelerated
1364 */
1365static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301366 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001367{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001368 cm_node->accelerated = 1;
1369
1370 if (cm_node->accept_pend) {
1371 BUG_ON(!cm_node->listener);
1372 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001373 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001374 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1375 }
1376
Tatyana Nikolova00ad2552012-12-06 19:56:31 +00001377 if (!timer_pending(&cm_core->tcp_timer))
1378 mod_timer(&cm_core->tcp_timer, (jiffies + NES_SHORT_TIME));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001379
1380 return 0;
1381}
1382
1383
1384/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001385 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001386 */
Faisal Latif7a576df2009-12-09 15:54:33 -08001387static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001388{
1389 struct rtable *rt;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001390 struct neighbour *neigh;
Faisal Latif7a576df2009-12-09 15:54:33 -08001391 int rc = arpindex;
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001392 struct net_device *netdev;
Faisal Latif7a576df2009-12-09 15:54:33 -08001393 struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
Tatyana Nikolova165d6822015-07-02 12:49:40 -05001394 __be32 dst_ipaddr = htonl(dst_ip);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001395
Tatyana Nikolova165d6822015-07-02 12:49:40 -05001396 rt = ip_route_output(&init_net, dst_ipaddr, nesvnic->local_ipaddr, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001397 if (IS_ERR(rt)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001398 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301399 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001400 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001401 }
1402
Roland Dreiercf55bb22011-03-24 17:13:20 -07001403 if (netif_is_bond_slave(nesvnic->netdev))
Jiri Pirko7f6e7102013-01-03 22:49:00 +00001404 netdev = netdev_master_upper_dev_get(nesvnic->netdev);
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001405 else
1406 netdev = nesvnic->netdev;
1407
Tatyana Nikolova165d6822015-07-02 12:49:40 -05001408 neigh = dst_neigh_lookup(&rt->dst, &dst_ipaddr);
David Millere55684f2012-01-24 13:16:03 +00001409
David Miller40e2bb52011-12-02 16:52:27 +00001410 rcu_read_lock();
Bob Sharp7191a0a2008-10-03 12:21:19 -07001411 if (neigh) {
1412 if (neigh->nud_state & NUD_VALID) {
1413 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001414 " is %pM, Gateway is 0x%08X \n", dst_ip,
1415 neigh->ha, ntohl(rt->rt_gateway));
Faisal Latif7a576df2009-12-09 15:54:33 -08001416
1417 if (arpindex >= 0) {
Ding Tianhong79adc532014-01-08 10:53:39 +08001418 if (ether_addr_equal(nesadapter->arp_table[arpindex].mac_addr, neigh->ha)) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001419 /* Mac address same as in nes_arp_table */
David Millere55684f2012-01-24 13:16:03 +00001420 goto out;
Faisal Latif7a576df2009-12-09 15:54:33 -08001421 }
1422
1423 nes_manage_arp_cache(nesvnic->netdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301424 nesadapter->arp_table[arpindex].mac_addr,
1425 dst_ip, NES_ARP_DELETE);
Faisal Latif7a576df2009-12-09 15:54:33 -08001426 }
1427
Bob Sharp7191a0a2008-10-03 12:21:19 -07001428 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1429 dst_ip, NES_ARP_ADD);
1430 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1431 NES_ARP_RESOLVE);
David Miller40e2bb52011-12-02 16:52:27 +00001432 } else {
1433 neigh_event_send(neigh, NULL);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001434 }
Bob Sharp7191a0a2008-10-03 12:21:19 -07001435 }
David Millere55684f2012-01-24 13:16:03 +00001436out:
David Miller40e2bb52011-12-02 16:52:27 +00001437 rcu_read_unlock();
David Millere55684f2012-01-24 13:16:03 +00001438
1439 if (neigh)
1440 neigh_release(neigh);
1441
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001442 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001443 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001444}
1445
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001446/**
1447 * make_cm_node - create a new instance of a cm node
1448 */
1449static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301450 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1451 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001452{
1453 struct nes_cm_node *cm_node;
1454 struct timespec ts;
Faisal Latif7a576df2009-12-09 15:54:33 -08001455 int oldarpindex = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001456 int arpindex = 0;
1457 struct nes_device *nesdev;
1458 struct nes_adapter *nesadapter;
1459
1460 /* create an hte and cm_node for this instance */
1461 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1462 if (!cm_node)
1463 return NULL;
1464
1465 /* set our node specific transport info */
Tatyana Nikolova230da362015-04-21 16:28:25 -04001466 if (listener) {
1467 cm_node->loc_addr = listener->loc_addr;
1468 cm_node->loc_port = listener->loc_port;
1469 } else {
1470 cm_node->loc_addr = cm_info->loc_addr;
1471 cm_node->loc_port = cm_info->loc_port;
1472 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001473 cm_node->rem_addr = cm_info->rem_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001474 cm_node->rem_port = cm_info->rem_port;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301475
1476 cm_node->mpa_frame_rev = mpa_version;
1477 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05001478 cm_node->mpav2_ird_ord = 0;
1479 cm_node->ird_size = 0;
1480 cm_node->ord_size = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301481
Harvey Harrison63779432008-10-31 00:56:00 -07001482 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1483 &cm_node->loc_addr, cm_node->loc_port,
1484 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001485 cm_node->listener = listener;
Faisal Latif854ace982015-05-18 15:28:14 -05001486 if (listener)
1487 cm_node->tos = listener->tos;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001488 cm_node->netdev = nesvnic->netdev;
1489 cm_node->cm_id = cm_info->cm_id;
1490 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1491
Faisal Latif6492cdf2008-07-24 20:50:45 -07001492 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301493 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001494
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001495 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001496
1497 cm_node->loopbackpartner = NULL;
1498 atomic_set(&cm_node->ref_count, 1);
1499 /* associate our parent CM core */
1500 cm_node->cm_core = cm_core;
1501 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1502 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1503 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301504 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001505 ts = current_kernel_time();
1506 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1507 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301508 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001509 cm_node->tcp_cntxt.rcv_nxt = 0;
1510 /* get a unique session ID , add thread_id to an upcounter to handle race */
1511 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001512 cm_node->conn_type = cm_info->conn_type;
1513 cm_node->apbvt_set = 0;
1514 cm_node->accept_pend = 0;
1515
1516 cm_node->nesvnic = nesvnic;
1517 /* get some device handles, for arp lookup */
1518 nesdev = nesvnic->nesdev;
1519 nesadapter = nesdev->nesadapter;
1520
1521 cm_node->loopbackpartner = NULL;
Faisal Latif7a576df2009-12-09 15:54:33 -08001522
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001523 /* get the mac addr for the remote node */
Faisal Latif6a0dde82016-02-26 09:18:02 -06001524 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr,
1525 NULL, NES_ARP_RESOLVE);
1526 arpindex = nes_addr_resolve_neigh(nesvnic, cm_node->rem_addr,
1527 oldarpindex);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001528 if (arpindex < 0) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001529 kfree(cm_node);
1530 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001531 }
1532
1533 /* copy the mac addr to node context */
1534 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001535 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1536 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001537
1538 add_hte_node(cm_core, cm_node);
1539 atomic_inc(&cm_nodes_created);
1540
1541 return cm_node;
1542}
1543
1544
1545/**
1546 * add_ref_cm_node - destroy an instance of a cm node
1547 */
1548static int add_ref_cm_node(struct nes_cm_node *cm_node)
1549{
1550 atomic_inc(&cm_node->ref_count);
1551 return 0;
1552}
1553
1554
1555/**
1556 * rem_ref_cm_node - destroy an instance of a cm node
1557 */
1558static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301559 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001560{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001561 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001562 struct nes_qp *nesqp;
1563
1564 if (!cm_node)
1565 return -EINVAL;
1566
1567 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1568 if (atomic_dec_return(&cm_node->ref_count)) {
1569 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1570 return 0;
1571 }
1572 list_del(&cm_node->list);
1573 atomic_dec(&cm_core->ht_node_cnt);
1574 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1575
1576 /* if the node is destroyed before connection was accelerated */
1577 if (!cm_node->accelerated && cm_node->accept_pend) {
1578 BUG_ON(!cm_node->listener);
1579 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1580 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1581 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001582 WARN_ON(cm_node->send_entry);
1583 if (cm_node->recv_entry)
1584 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001585 if (cm_node->listener) {
1586 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1587 } else {
1588 if (cm_node->apbvt_set && cm_node->nesvnic) {
Faisal Latif6a0dde82016-02-26 09:18:02 -06001589 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Tatyana Nikolova56472632014-03-26 17:07:57 -05001590 PCI_FUNC(cm_node->nesvnic->nesdev->pcidev->devfn),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301591 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001592 }
Faisal Latif6a0dde82016-02-26 09:18:02 -06001593 nes_debug(NES_DBG_NLMSG, "Delete APBVT loc_port = %04X\n",
1594 cm_node->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001595 }
1596
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001597 atomic_dec(&cm_core->node_cnt);
1598 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001599 nesqp = cm_node->nesqp;
1600 if (nesqp) {
1601 nesqp->cm_node = NULL;
1602 nes_rem_ref(&nesqp->ibqp);
1603 cm_node->nesqp = NULL;
1604 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001605
Faisal Latif6492cdf2008-07-24 20:50:45 -07001606 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001607 return 0;
1608}
1609
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001610/**
1611 * process_options
1612 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001613static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301614 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001615{
1616 u32 tmp;
1617 u32 offset = 0;
1618 union all_known_options *all_options;
1619 char got_mss_option = 0;
1620
1621 while (offset < optionsize) {
1622 all_options = (union all_known_options *)(optionsloc + offset);
1623 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001624 case OPTION_NUMBER_END:
1625 offset = optionsize;
1626 break;
1627 case OPTION_NUMBER_NONE:
1628 offset += 1;
1629 continue;
1630 case OPTION_NUMBER_MSS:
1631 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301632 "Size: %d\n", __func__,
1633 all_options->as_mss.length, offset, optionsize);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001634 got_mss_option = 1;
1635 if (all_options->as_mss.length != 4) {
1636 return 1;
1637 } else {
1638 tmp = ntohs(all_options->as_mss.mss);
1639 if (tmp > 0 && tmp <
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301640 cm_node->tcp_cntxt.mss)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001641 cm_node->tcp_cntxt.mss = tmp;
1642 }
1643 break;
1644 case OPTION_NUMBER_WINDOW_SCALE:
1645 cm_node->tcp_cntxt.snd_wscale =
1646 all_options->as_windowscale.shiftcount;
1647 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001648 default:
1649 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301650 all_options->as_base.optionnum);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001651 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001652 }
1653 offset += all_options->as_base.length;
1654 }
1655 if ((!got_mss_option) && (syn_packet))
1656 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1657 return 0;
1658}
1659
Faisal Latif6492cdf2008-07-24 20:50:45 -07001660static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001661{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001662 atomic_inc(&cm_accel_dropped_pkts);
1663 dev_kfree_skb_any(skb);
1664}
1665
Faisal Latif9d5ab132009-03-06 15:15:01 -08001666static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001667{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001668 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301669 "refcnt=%d\n", cm_node, cm_node->state,
1670 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001671 switch (cm_node->state) {
1672 case NES_CM_STATE_SYN_RCVD:
1673 case NES_CM_STATE_SYN_SENT:
1674 case NES_CM_STATE_ESTABLISHED:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001675 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001676 cm_node->tcp_cntxt.rcv_nxt++;
1677 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001678 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001679 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001680 break;
Faisal Latifb1190d32009-12-09 15:54:32 -08001681 case NES_CM_STATE_MPAREQ_SENT:
1682 create_event(cm_node, NES_CM_EVENT_ABORTED);
1683 cm_node->tcp_cntxt.rcv_nxt++;
1684 cleanup_retrans_entry(cm_node);
1685 cm_node->state = NES_CM_STATE_CLOSED;
1686 add_ref_cm_node(cm_node);
1687 send_reset(cm_node, NULL);
1688 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001689 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001690 cm_node->tcp_cntxt.rcv_nxt++;
1691 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001692 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001693 send_ack(cm_node, NULL);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001694 /* Wait for ACK as this is simultaneous close..
Faisal Latif9d5ab132009-03-06 15:15:01 -08001695 * After we receive ACK, do not send anything..
1696 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001697 break;
1698 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001699 cm_node->tcp_cntxt.rcv_nxt++;
1700 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001701 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001702 send_ack(cm_node, NULL);
1703 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1704 break;
1705 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001706 cm_node->tcp_cntxt.rcv_nxt++;
1707 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001708 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001709 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001710 break;
1711 case NES_CM_STATE_TSA:
1712 default:
1713 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1714 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001715 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001716 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001717}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001718
Faisal Latif6492cdf2008-07-24 20:50:45 -07001719
1720static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1721 struct tcphdr *tcph)
1722{
1723
1724 int reset = 0; /* whether to send reset in case of err.. */
1725 atomic_inc(&cm_resets_recvd);
1726 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1727 " refcnt=%d\n", cm_node, cm_node->state,
1728 atomic_read(&cm_node->ref_count));
1729 cleanup_retrans_entry(cm_node);
1730 switch (cm_node->state) {
1731 case NES_CM_STATE_SYN_SENT:
1732 case NES_CM_STATE_MPAREQ_SENT:
1733 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1734 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1735 cm_node->listener, cm_node->state);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301736 switch (cm_node->mpa_frame_rev) {
1737 case IETF_MPA_V2:
1738 cm_node->mpa_frame_rev = IETF_MPA_V1;
1739 /* send a syn and goto syn sent state */
1740 cm_node->state = NES_CM_STATE_SYN_SENT;
1741 if (send_syn(cm_node, 0, NULL)) {
1742 active_open_err(cm_node, skb, reset);
1743 }
1744 break;
1745 case IETF_MPA_V1:
1746 default:
1747 active_open_err(cm_node, skb, reset);
1748 break;
1749 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001750 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001751 case NES_CM_STATE_MPAREQ_RCVD:
Dan Carpenter4d8d6382010-09-15 18:32:39 +02001752 atomic_inc(&cm_node->passive_state);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001753 dev_kfree_skb_any(skb);
1754 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001755 case NES_CM_STATE_ESTABLISHED:
1756 case NES_CM_STATE_SYN_RCVD:
1757 case NES_CM_STATE_LISTENING:
1758 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1759 passive_open_err(cm_node, skb, reset);
1760 break;
1761 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001762 active_open_err(cm_node, skb, reset);
1763 break;
1764 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001765 drop_packet(skb);
1766 break;
Faisal Latifdae58722010-08-14 21:04:56 +00001767 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif68237a02009-06-22 22:53:28 -07001768 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001769 case NES_CM_STATE_LAST_ACK:
1770 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001771 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001772 cm_node->state = NES_CM_STATE_CLOSED;
1773 rem_ref_cm_node(cm_node->cm_core, cm_node);
1774 drop_packet(skb);
1775 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001776 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001777 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001778 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001779 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001780}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001781
Faisal Latif9d5ab132009-03-06 15:15:01 -08001782
1783static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001784{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301785 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001786 int datasize = skb->len;
1787 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001788
1789 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301790 u32 res_type;
1791
Faisal Latif9d5ab132009-03-06 15:15:01 -08001792 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1793 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001794 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001795 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001796 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301797 "cm_node=%p listener=%p state=%d\n", __func__,
1798 __LINE__, cm_node, cm_node->listener,
1799 cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001800 active_open_err(cm_node, skb, 1);
1801 } else {
1802 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001803 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001804 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001805 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001806
1807 switch (cm_node->state) {
1808 case NES_CM_STATE_ESTABLISHED:
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301809 if (res_type == NES_MPA_REQUEST_REJECT)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001810 /*BIG problem as we are receiving the MPA.. So should
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301811 * not be REJECT.. This is Passive Open.. We can
1812 * only receive it Reject for Active Open...*/
Faisal Latif9d5ab132009-03-06 15:15:01 -08001813 WARN_ON(1);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001814 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1815 type = NES_CM_EVENT_MPA_REQ;
1816 atomic_set(&cm_node->passive_state,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301817 NES_PASSIVE_STATE_INDICATED);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001818 break;
1819 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001820 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001821 if (res_type == NES_MPA_REQUEST_REJECT) {
1822 type = NES_CM_EVENT_MPA_REJECT;
1823 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1824 } else {
1825 type = NES_CM_EVENT_CONNECTED;
1826 cm_node->state = NES_CM_STATE_TSA;
1827 }
Tatyana Nikolovaf863de72017-05-10 16:46:40 -05001828 send_ack(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001829 break;
1830 default:
1831 WARN_ON(1);
1832 break;
1833 }
1834 dev_kfree_skb_any(skb);
1835 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001836}
1837
1838static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1839{
1840 switch (cm_node->state) {
1841 case NES_CM_STATE_SYN_SENT:
1842 case NES_CM_STATE_MPAREQ_SENT:
1843 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301844 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1845 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001846 active_open_err(cm_node, skb, 1);
1847 break;
1848 case NES_CM_STATE_ESTABLISHED:
1849 case NES_CM_STATE_SYN_RCVD:
1850 passive_open_err(cm_node, skb, 1);
1851 break;
1852 case NES_CM_STATE_TSA:
1853 default:
1854 drop_packet(skb);
1855 }
1856}
1857
1858static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301859 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001860{
1861 int err;
1862
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301863 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001864 if (err)
1865 active_open_err(cm_node, skb, 1);
1866
1867 return err;
1868}
1869
1870static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301871 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001872{
1873 int err = 0;
1874 u32 seq;
1875 u32 ack_seq;
1876 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1877 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1878 u32 rcv_wnd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301879
Faisal Latif6492cdf2008-07-24 20:50:45 -07001880 seq = ntohl(tcph->seq);
1881 ack_seq = ntohl(tcph->ack_seq);
1882 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1883 if (ack_seq != loc_seq_num)
1884 err = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301885 else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001886 err = 1;
1887 if (err) {
1888 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301889 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1890 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001891 indicate_pkt_err(cm_node, skb);
1892 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301893 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1894 rcv_wnd);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001895 }
1896 return err;
1897}
1898
1899/*
1900 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1901 * is created with a listener or it may comein as rexmitted packet which in
1902 * that case will be just dropped.
1903 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001904static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301905 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001906{
1907 int ret;
1908 u32 inc_sequence;
1909 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001910
1911 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001912 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001913 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001914
Faisal Latif6492cdf2008-07-24 20:50:45 -07001915 switch (cm_node->state) {
1916 case NES_CM_STATE_SYN_SENT:
1917 case NES_CM_STATE_MPAREQ_SENT:
1918 /* Rcvd syn on active open connection*/
1919 active_open_err(cm_node, skb, 1);
1920 break;
1921 case NES_CM_STATE_LISTENING:
1922 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001923 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301924 cm_node->listener->backlog) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001925 nes_debug(NES_DBG_CM, "drop syn due to backlog "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301926 "pressure \n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07001927 cm_backlog_drops++;
1928 passive_open_err(cm_node, skb, 0);
1929 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001930 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001931 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301932 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001933 if (ret) {
1934 passive_open_err(cm_node, skb, 0);
1935 /* drop pkt */
1936 break;
1937 }
1938 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1939 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001940 cm_node->accept_pend = 1;
1941 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1942
Faisal Latif6492cdf2008-07-24 20:50:45 -07001943 cm_node->state = NES_CM_STATE_SYN_RCVD;
1944 send_syn(cm_node, 1, skb);
1945 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001946 case NES_CM_STATE_CLOSED:
1947 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001948 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001949 send_reset(cm_node, skb);
1950 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001951 case NES_CM_STATE_TSA:
1952 case NES_CM_STATE_ESTABLISHED:
1953 case NES_CM_STATE_FIN_WAIT1:
1954 case NES_CM_STATE_FIN_WAIT2:
1955 case NES_CM_STATE_MPAREQ_RCVD:
1956 case NES_CM_STATE_LAST_ACK:
1957 case NES_CM_STATE_CLOSING:
1958 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001959 default:
1960 drop_packet(skb);
1961 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001962 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001963}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001964
Faisal Latif6492cdf2008-07-24 20:50:45 -07001965static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301966 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001967{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001968 int ret;
1969 u32 inc_sequence;
1970 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001971
Faisal Latif6492cdf2008-07-24 20:50:45 -07001972 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001973 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001974 inc_sequence = ntohl(tcph->seq);
1975 switch (cm_node->state) {
1976 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001977 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001978 /* active open */
1979 if (check_syn(cm_node, tcph, skb))
1980 return;
1981 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1982 /* setup options */
1983 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1984 if (ret) {
1985 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301986 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001987 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001988 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001989 cleanup_retrans_entry(cm_node);
1990 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1991 send_mpa_request(cm_node, skb);
1992 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1993 break;
1994 case NES_CM_STATE_MPAREQ_RCVD:
1995 /* passive open, so should not be here */
1996 passive_open_err(cm_node, skb, 1);
1997 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001998 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001999 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
2000 cleanup_retrans_entry(cm_node);
2001 cm_node->state = NES_CM_STATE_CLOSED;
2002 send_reset(cm_node, skb);
2003 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002004 case NES_CM_STATE_CLOSED:
2005 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
2006 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08002007 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002008 send_reset(cm_node, skb);
2009 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002010 case NES_CM_STATE_ESTABLISHED:
2011 case NES_CM_STATE_FIN_WAIT1:
2012 case NES_CM_STATE_FIN_WAIT2:
2013 case NES_CM_STATE_LAST_ACK:
2014 case NES_CM_STATE_TSA:
2015 case NES_CM_STATE_CLOSING:
2016 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002017 case NES_CM_STATE_MPAREQ_SENT:
2018 default:
2019 drop_packet(skb);
2020 break;
2021 }
2022}
2023
Faisal Latif109d67e2009-04-27 13:41:06 -07002024static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302025 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002026{
2027 int datasize = 0;
2028 u32 inc_sequence;
Faisal Latif109d67e2009-04-27 13:41:06 -07002029 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06002030 int optionsize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302031
Faisal Latifabb77252008-11-21 20:50:52 -06002032 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06002033
Faisal Latif6492cdf2008-07-24 20:50:45 -07002034 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07002035 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002036
2037 skb_pull(skb, tcph->doff << 2);
2038 inc_sequence = ntohl(tcph->seq);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002039 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002040 switch (cm_node->state) {
2041 case NES_CM_STATE_SYN_RCVD:
2042 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07002043 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002044 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
2045 if (ret)
2046 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002047 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2048 cm_node->state = NES_CM_STATE_ESTABLISHED;
2049 if (datasize) {
2050 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002051 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302052 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002053 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302054 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002055 break;
2056 case NES_CM_STATE_ESTABLISHED:
2057 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08002058 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002059 if (datasize) {
2060 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002061 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302062 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002063 drop_packet(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302064 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002065 break;
2066 case NES_CM_STATE_MPAREQ_SENT:
2067 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2068 if (datasize) {
2069 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002070 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302071 } else { /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002072 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302073 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002074 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002075 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002076 cleanup_retrans_entry(cm_node);
2077 cm_node->state = NES_CM_STATE_CLOSED;
2078 send_reset(cm_node, skb);
2079 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002080 case NES_CM_STATE_CLOSED:
2081 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08002082 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002083 send_reset(cm_node, skb);
2084 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002085 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002086 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002087 cleanup_retrans_entry(cm_node);
2088 cm_node->state = NES_CM_STATE_CLOSED;
2089 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002090 rem_ref_cm_node(cm_node->cm_core, cm_node);
2091 drop_packet(skb);
2092 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002093 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002094 cleanup_retrans_entry(cm_node);
2095 drop_packet(skb);
2096 cm_node->state = NES_CM_STATE_FIN_WAIT2;
2097 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002098 case NES_CM_STATE_SYN_SENT:
2099 case NES_CM_STATE_FIN_WAIT2:
2100 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002101 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002102 case NES_CM_STATE_UNKNOWN:
2103 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002104 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002105 drop_packet(skb);
2106 break;
2107 }
Faisal Latif109d67e2009-04-27 13:41:06 -07002108 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002109}
2110
2111
2112
2113static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302114 struct sk_buff *skb, int optionsize, int passive)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002115{
2116 u8 *optionsloc = (u8 *)&tcph[1];
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302117
Faisal Latif6492cdf2008-07-24 20:50:45 -07002118 if (optionsize) {
2119 if (process_options(cm_node, optionsloc, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302120 (u32)tcph->syn)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002121 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302122 __func__, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002123 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06002124 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002125 else
Faisal Latifabb77252008-11-21 20:50:52 -06002126 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002127 return 1;
2128 }
2129 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002130
2131 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302132 cm_node->tcp_cntxt.snd_wscale;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002133
Faisal Latif6492cdf2008-07-24 20:50:45 -07002134 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002135 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002136 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002137}
2138
Faisal Latif6492cdf2008-07-24 20:50:45 -07002139/*
2140 * active_open_err() will send reset() if flag set..
2141 * It will also send ABORT event.
2142 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002143static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302144 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002145{
2146 cleanup_retrans_entry(cm_node);
2147 if (reset) {
2148 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302149 "state=%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002150 add_ref_cm_node(cm_node);
2151 send_reset(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302152 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002153 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302154 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002155
2156 cm_node->state = NES_CM_STATE_CLOSED;
2157 create_event(cm_node, NES_CM_EVENT_ABORTED);
2158}
2159
2160/*
2161 * passive_open_err() will either do a reset() or will free up the skb and
2162 * remove the cm_node.
2163 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002164static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302165 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002166{
2167 cleanup_retrans_entry(cm_node);
2168 cm_node->state = NES_CM_STATE_CLOSED;
2169 if (reset) {
2170 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302171 "cm_node=%p state =%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002172 send_reset(cm_node, skb);
2173 } else {
2174 dev_kfree_skb_any(skb);
2175 rem_ref_cm_node(cm_node->cm_core, cm_node);
2176 }
2177}
2178
2179/*
2180 * free_retrans_entry() routines assumes that the retrans_list_lock has
2181 * been acquired before calling.
2182 */
2183static void free_retrans_entry(struct nes_cm_node *cm_node)
2184{
2185 struct nes_timer_entry *send_entry;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302186
Faisal Latif6492cdf2008-07-24 20:50:45 -07002187 send_entry = cm_node->send_entry;
2188 if (send_entry) {
2189 cm_node->send_entry = NULL;
2190 dev_kfree_skb_any(send_entry->skb);
2191 kfree(send_entry);
2192 rem_ref_cm_node(cm_node->cm_core, cm_node);
2193 }
2194}
2195
2196static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
2197{
2198 unsigned long flags;
2199
2200 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
2201 free_retrans_entry(cm_node);
2202 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
2203}
2204
2205/**
2206 * process_packet
2207 * Returns skb if to be freed, else it will return NULL if already used..
2208 */
2209static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302210 struct nes_cm_core *cm_core)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002211{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302212 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002213 struct tcphdr *tcph = tcp_hdr(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302214 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07002215 int ret = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302216
Faisal Latif6492cdf2008-07-24 20:50:45 -07002217 skb_pull(skb, ip_hdr(skb)->ihl << 2);
2218
2219 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302220 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
2221 tcph->ack, tcph->rst, tcph->fin);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002222
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302223 if (tcph->rst) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002224 pkt_type = NES_PKT_TYPE_RST;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302225 } else if (tcph->syn) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002226 pkt_type = NES_PKT_TYPE_SYN;
2227 if (tcph->ack)
2228 pkt_type = NES_PKT_TYPE_SYNACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302229 } else if (tcph->ack) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002230 pkt_type = NES_PKT_TYPE_ACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302231 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002232 if (tcph->fin)
2233 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002234
2235 switch (pkt_type) {
2236 case NES_PKT_TYPE_SYN:
2237 handle_syn_pkt(cm_node, skb, tcph);
2238 break;
2239 case NES_PKT_TYPE_SYNACK:
2240 handle_synack_pkt(cm_node, skb, tcph);
2241 break;
2242 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002243 ret = handle_ack_pkt(cm_node, skb, tcph);
2244 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08002245 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002246 break;
2247 case NES_PKT_TYPE_RST:
2248 handle_rst_pkt(cm_node, skb, tcph);
2249 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002250 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002251 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08002252 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07002253 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002254 break;
2255 }
2256}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002257
2258/**
2259 * mini_cm_listen - create a listen node with params
2260 */
2261static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova56472632014-03-26 17:07:57 -05002262 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002263{
2264 struct nes_cm_listener *listener;
2265 unsigned long flags;
2266
2267 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302268 cm_info->loc_addr, cm_info->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002269
2270 /* cannot have multiple matching listeners */
Tatyana Nikolova56472632014-03-26 17:07:57 -05002271 listener = find_listener(cm_core, cm_info->loc_addr, cm_info->loc_port,
Faisal Latif6a0dde82016-02-26 09:18:02 -06002272 NES_CM_LISTENER_EITHER_STATE);
Tatyana Nikolova56472632014-03-26 17:07:57 -05002273
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002274 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
2275 /* find automatically incs ref count ??? */
2276 atomic_dec(&listener->ref_count);
2277 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
2278 return NULL;
2279 }
2280
2281 if (!listener) {
2282 /* create a CM listen node (1/2 node to compare incoming traffic to) */
2283 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
Leon Romanovsky2e658352016-11-03 16:44:19 +02002284 if (!listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002285 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002286
Tatyana Nikolova56472632014-03-26 17:07:57 -05002287 listener->loc_addr = cm_info->loc_addr;
2288 listener->loc_port = cm_info->loc_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002289 listener->reused_node = 0;
2290
2291 atomic_set(&listener->ref_count, 1);
2292 }
2293 /* pasive case */
2294 /* find already inc'ed the ref count */
2295 else {
2296 listener->reused_node = 1;
2297 }
2298
2299 listener->cm_id = cm_info->cm_id;
2300 atomic_set(&listener->pend_accepts_cnt, 0);
2301 listener->cm_core = cm_core;
2302 listener->nesvnic = nesvnic;
2303 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002304
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002305 listener->conn_type = cm_info->conn_type;
2306 listener->backlog = cm_info->backlog;
2307 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
2308
2309 if (!listener->reused_node) {
2310 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2311 list_add(&listener->list, &cm_core->listen_list.list);
2312 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2313 atomic_inc(&cm_core->listen_node_cnt);
2314 }
2315
2316 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302317 " listener = %p, backlog = %d, cm_id = %p.\n",
2318 cm_info->loc_addr, cm_info->loc_port,
2319 listener, listener->backlog, listener->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002320
2321 return listener;
2322}
2323
2324
2325/**
2326 * mini_cm_connect - make a connection node with params
2327 */
Faisal Latif54c86a82008-09-30 14:47:27 -07002328static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302329 struct nes_vnic *nesvnic, u16 private_data_len,
2330 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002331{
2332 int ret = 0;
2333 struct nes_cm_node *cm_node;
2334 struct nes_cm_listener *loopbackremotelistener;
2335 struct nes_cm_node *loopbackremotenode;
2336 struct nes_cm_info loopback_cm_info;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302337 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002338
2339 /* create a CM connection node */
2340 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2341 if (!cm_node)
2342 return NULL;
2343
Glenn Streiff7495ab62008-04-29 13:46:54 -07002344 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002345 cm_node->tcp_cntxt.client = 1;
2346 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2347
2348 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002349 loopbackremotelistener = find_listener(cm_core,
Faisal Latif6a0dde82016-02-26 09:18:02 -06002350 cm_node->loc_addr, cm_node->rem_port,
2351 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002352 if (loopbackremotelistener == NULL) {
2353 create_event(cm_node, NES_CM_EVENT_ABORTED);
2354 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002355 loopback_cm_info = *cm_info;
2356 loopback_cm_info.loc_port = cm_info->rem_port;
2357 loopback_cm_info.rem_port = cm_info->loc_port;
Faisal Latif6a0dde82016-02-26 09:18:02 -06002358 loopback_cm_info.loc_port =
2359 cm_info->rem_port;
2360 loopback_cm_info.rem_port =
2361 cm_info->loc_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002362 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002363 loopbackremotenode = make_cm_node(cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302364 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002365 if (!loopbackremotenode) {
2366 rem_ref_cm_node(cm_node->cm_core, cm_node);
2367 return NULL;
2368 }
2369 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002370 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002371 loopbackremotenode->tcp_cntxt.rcv_wscale =
2372 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002373 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002374 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302375 private_data_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002376 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002377
Faisal Latif6492cdf2008-07-24 20:50:45 -07002378 /* we are done handling this state. */
2379 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002380 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002381 cm_node->tcp_cntxt.rcv_nxt =
2382 loopbackremotenode->tcp_cntxt.loc_seq_num;
2383 loopbackremotenode->tcp_cntxt.rcv_nxt =
2384 cm_node->tcp_cntxt.loc_seq_num;
2385 cm_node->tcp_cntxt.max_snd_wnd =
2386 loopbackremotenode->tcp_cntxt.rcv_wnd;
2387 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2388 cm_node->tcp_cntxt.rcv_wnd;
2389 cm_node->tcp_cntxt.snd_wnd =
2390 loopbackremotenode->tcp_cntxt.rcv_wnd;
2391 loopbackremotenode->tcp_cntxt.snd_wnd =
2392 cm_node->tcp_cntxt.rcv_wnd;
2393 cm_node->tcp_cntxt.snd_wscale =
2394 loopbackremotenode->tcp_cntxt.rcv_wscale;
2395 loopbackremotenode->tcp_cntxt.snd_wscale =
2396 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002397 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002398 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2399 }
2400 return cm_node;
2401 }
2402
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302403 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
2404 cm_node->mpa_frame_size = private_data_len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002405
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302406 memcpy(start_buff, private_data, private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002407
2408 /* send a syn and goto syn sent state */
2409 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002410 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002411
Faisal Latif6492cdf2008-07-24 20:50:45 -07002412 if (ret) {
2413 /* error in sending the syn free up the cm_node struct */
2414 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302415 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2416 cm_node->rem_addr, cm_node->rem_port, cm_node,
2417 cm_node->cm_id);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002418 rem_ref_cm_node(cm_node->cm_core, cm_node);
2419 cm_node = NULL;
2420 }
2421
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302422 if (cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002423 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302424 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2425 cm_node->rem_addr, cm_node->rem_port, cm_node,
2426 cm_node->cm_id);
2427 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002428
2429 return cm_node;
2430}
2431
2432
2433/**
2434 * mini_cm_accept - accept a connection
2435 * This function is never called
2436 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302437static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002438{
2439 return 0;
2440}
2441
2442
2443/**
2444 * mini_cm_reject - reject and teardown a connection
2445 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302446static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002447{
2448 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002449 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002450 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002451 struct nes_cm_event event;
2452 struct iw_cm_id *cm_id = cm_node->cm_id;
2453 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002454
Faisal Latif6492cdf2008-07-24 20:50:45 -07002455 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302456 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002457
Faisal Latif6492cdf2008-07-24 20:50:45 -07002458 if (cm_node->tcp_cntxt.client)
2459 return ret;
2460 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002461
Faisal Latif9d5ab132009-03-06 15:15:01 -08002462 if (!loopback) {
2463 passive_state = atomic_add_return(1, &cm_node->passive_state);
2464 if (passive_state == NES_SEND_RESET_EVENT) {
2465 cm_node->state = NES_CM_STATE_CLOSED;
2466 rem_ref_cm_node(cm_core, cm_node);
2467 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002468 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2469 rem_ref_cm_node(cm_core, cm_node);
2470 } else {
2471 ret = send_mpa_reject(cm_node);
2472 if (ret) {
2473 cm_node->state = NES_CM_STATE_CLOSED;
2474 err = send_reset(cm_node, NULL);
2475 if (err)
2476 WARN_ON(1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302477 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002478 cm_id->add_ref(cm_id);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302479 }
Faisal Latifc5a7d482009-12-09 15:54:08 -08002480 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002481 }
2482 } else {
2483 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002484 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2485 rem_ref_cm_node(cm_core, cm_node);
2486 rem_ref_cm_node(cm_core, loopback);
2487 } else {
2488 event.cm_node = loopback;
2489 event.cm_info.rem_addr = loopback->rem_addr;
2490 event.cm_info.loc_addr = loopback->loc_addr;
2491 event.cm_info.rem_port = loopback->rem_port;
2492 event.cm_info.loc_port = loopback->loc_port;
2493 event.cm_info.cm_id = loopback->cm_id;
2494 cm_event_mpa_reject(&event);
2495 rem_ref_cm_node(cm_core, cm_node);
2496 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002497
Faisal Latifc5a7d482009-12-09 15:54:08 -08002498 cm_id = loopback->cm_id;
2499 rem_ref_cm_node(cm_core, loopback);
2500 cm_id->rem_ref(cm_id);
2501 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002502 }
2503
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002504 return ret;
2505}
2506
2507
2508/**
2509 * mini_cm_close
2510 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002511static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002512{
2513 int ret = 0;
2514
2515 if (!cm_core || !cm_node)
2516 return -EINVAL;
2517
2518 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002519 case NES_CM_STATE_SYN_RCVD:
2520 case NES_CM_STATE_SYN_SENT:
2521 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2522 case NES_CM_STATE_ESTABLISHED:
2523 case NES_CM_STATE_ACCEPTING:
2524 case NES_CM_STATE_MPAREQ_SENT:
2525 case NES_CM_STATE_MPAREQ_RCVD:
2526 cleanup_retrans_entry(cm_node);
2527 send_reset(cm_node, NULL);
2528 break;
2529 case NES_CM_STATE_CLOSE_WAIT:
2530 cm_node->state = NES_CM_STATE_LAST_ACK;
2531 send_fin(cm_node, NULL);
2532 break;
2533 case NES_CM_STATE_FIN_WAIT1:
2534 case NES_CM_STATE_FIN_WAIT2:
2535 case NES_CM_STATE_LAST_ACK:
2536 case NES_CM_STATE_TIME_WAIT:
2537 case NES_CM_STATE_CLOSING:
2538 ret = -1;
2539 break;
2540 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002541 cleanup_retrans_entry(cm_node);
2542 send_reset(cm_node, NULL);
2543 break;
2544 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002545 case NES_CM_STATE_UNKNOWN:
2546 case NES_CM_STATE_INITED:
2547 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002548 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002549 ret = rem_ref_cm_node(cm_core, cm_node);
2550 break;
2551 case NES_CM_STATE_TSA:
2552 if (cm_node->send_entry)
2553 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302554 "send_entry=%p\n", cm_node->send_entry);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002555 ret = rem_ref_cm_node(cm_core, cm_node);
2556 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002557 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002558 return ret;
2559}
2560
2561
2562/**
2563 * recv_pkt - recv an ETHERNET packet, and process it through CM
2564 * node state machine
2565 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002566static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302567 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002568{
2569 struct nes_cm_node *cm_node = NULL;
2570 struct nes_cm_listener *listener = NULL;
2571 struct iphdr *iph;
2572 struct tcphdr *tcph;
2573 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002574 int skb_handled = 1;
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002575 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002576
Faisal Latif6492cdf2008-07-24 20:50:45 -07002577 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002578 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302579 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002580 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002581
2582 iph = (struct iphdr *)skb->data;
2583 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002584
2585 nfo.loc_addr = ntohl(iph->daddr);
2586 nfo.loc_port = ntohs(tcph->dest);
2587 nfo.rem_addr = ntohl(iph->saddr);
2588 nfo.rem_port = ntohs(tcph->source);
2589
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002590 tmp_daddr = cpu_to_be32(iph->daddr);
2591 tmp_saddr = cpu_to_be32(iph->saddr);
2592
Harvey Harrison63779432008-10-31 00:56:00 -07002593 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002594 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002595
Faisal Latif6492cdf2008-07-24 20:50:45 -07002596 do {
2597 cm_node = find_node(cm_core,
Faisal Latif6a0dde82016-02-26 09:18:02 -06002598 nfo.rem_port, nfo.rem_addr,
2599 nfo.loc_port, nfo.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002600
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002601 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002602 /* Only type of packet accepted are for */
2603 /* the PASSIVE open (syn only) */
2604 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002605 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002606 break;
2607 }
Faisal Latif6a0dde82016-02-26 09:18:02 -06002608 listener = find_listener(cm_core, nfo.loc_addr,
2609 nfo.loc_port,
2610 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002611 if (!listener) {
2612 nfo.cm_id = NULL;
2613 nfo.conn_type = 0;
2614 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2615 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002616 break;
2617 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002618 nfo.cm_id = listener->cm_id;
2619 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002620 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302621 listener);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002622 if (!cm_node) {
2623 nes_debug(NES_DBG_CM, "Unable to allocate "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302624 "node\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07002625 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002626 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002627 dev_kfree_skb_any(skb);
2628 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002629 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002630 if (!tcph->rst && !tcph->fin) {
2631 cm_node->state = NES_CM_STATE_LISTENING;
2632 } else {
2633 cm_packets_dropped++;
2634 rem_ref_cm_node(cm_core, cm_node);
2635 dev_kfree_skb_any(skb);
2636 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002637 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002638 add_ref_cm_node(cm_node);
2639 } else if (cm_node->state == NES_CM_STATE_TSA) {
Faisal Latif0f0bee82011-09-25 20:34:00 -05002640 if (cm_node->nesqp->pau_mode)
2641 nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
2642 else {
2643 rem_ref_cm_node(cm_core, cm_node);
2644 atomic_inc(&cm_accel_dropped_pkts);
2645 dev_kfree_skb_any(skb);
2646 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002647 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002648 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002649 skb_reset_network_header(skb);
2650 skb_set_transport_header(skb, sizeof(*tcph));
2651 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002652 process_packet(cm_node, skb, cm_core);
2653 rem_ref_cm_node(cm_core, cm_node);
2654 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002655 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002656}
2657
2658
2659/**
2660 * nes_cm_alloc_core - allocate a top level instance of a cm core
2661 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002662static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002663{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002664 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002665
2666 /* setup the CM core */
2667 /* alloc top level core control structure */
2668 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2669 if (!cm_core)
2670 return NULL;
2671
2672 INIT_LIST_HEAD(&cm_core->connected_nodes);
2673 init_timer(&cm_core->tcp_timer);
2674 cm_core->tcp_timer.function = nes_cm_timer_tick;
2675
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302676 cm_core->mtu = NES_CM_DEFAULT_MTU;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002677 cm_core->state = NES_CM_STATE_INITED;
2678 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2679
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002680 atomic_set(&cm_core->events_posted, 0);
2681
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002682 cm_core->api = &nes_cm_api;
2683
2684 spin_lock_init(&cm_core->ht_lock);
2685 spin_lock_init(&cm_core->listen_list_lock);
2686
2687 INIT_LIST_HEAD(&cm_core->listen_list.list);
2688
2689 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2690
2691 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
Bhaktipriya Shridhar9e5df312016-08-15 23:44:03 +05302692 cm_core->event_wq = alloc_ordered_workqueue("nesewq", 0);
Insu Yuna82268b2016-02-17 13:06:33 -05002693 if (!cm_core->event_wq)
2694 goto out_free_cmcore;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002695 cm_core->post_event = nes_cm_post_event;
2696 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
Bhaktipriya Shridhar9e5df312016-08-15 23:44:03 +05302697 cm_core->disconn_wq = alloc_ordered_workqueue("nesdwq", 0);
Insu Yuna82268b2016-02-17 13:06:33 -05002698 if (!cm_core->disconn_wq)
2699 goto out_free_wq;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002700
2701 print_core(cm_core);
2702 return cm_core;
Insu Yuna82268b2016-02-17 13:06:33 -05002703
2704out_free_wq:
2705 destroy_workqueue(cm_core->event_wq);
2706out_free_cmcore:
2707 kfree(cm_core);
2708 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002709}
2710
2711
2712/**
2713 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2714 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002715static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002716{
2717 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2718
2719 if (!cm_core)
2720 return -EINVAL;
2721
2722 barrier();
2723
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302724 if (timer_pending(&cm_core->tcp_timer))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002725 del_timer(&cm_core->tcp_timer);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002726
2727 destroy_workqueue(cm_core->event_wq);
2728 destroy_workqueue(cm_core->disconn_wq);
2729 nes_debug(NES_DBG_CM, "\n");
2730 kfree(cm_core);
2731
2732 return 0;
2733}
2734
2735
2736/**
2737 * mini_cm_get
2738 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002739static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002740{
2741 return cm_core->state;
2742}
2743
2744
2745/**
2746 * mini_cm_set
2747 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002748static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002749{
2750 int ret = 0;
2751
2752 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002753 case NES_CM_SET_PKT_SIZE:
2754 cm_core->mtu = value;
2755 break;
2756 case NES_CM_SET_FREE_PKT_Q_SIZE:
2757 cm_core->free_tx_pkt_max = value;
2758 break;
2759 default:
2760 /* unknown set option */
2761 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002762 }
2763
2764 return ret;
2765}
2766
2767
2768/**
2769 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2770 * successfully exchanged when this is called
2771 */
2772static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2773{
2774 int ret = 0;
2775
2776 if (!nesqp)
2777 return -EINVAL;
2778
2779 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302780 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2781 NES_QPCONTEXT_MISC_DROS);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002782
2783 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2784 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2785
2786 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2787
Faisal Latif854ace982015-05-18 15:28:14 -05002788 nesqp->nesqp_context->misc2 |= cpu_to_le32(
2789 cm_node->tos << NES_QPCONTEXT_MISC2_TOS_SHIFT);
2790
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002791 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2792
2793 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302794 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002795
2796 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302797 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2798 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002799
2800 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302801 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2802 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002803
2804 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2805 nesqp->nesqp_context->ts_recent = 0;
2806 nesqp->nesqp_context->ts_age = 0;
2807 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2808 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2809 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2810 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302811 cm_node->tcp_cntxt.rcv_wscale);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002812 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2813 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2814 nesqp->nesqp_context->srtt = 0;
2815 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2816 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302817 nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002818 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2819 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2820 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2821
2822 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302823 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2824 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2825 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2826 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2827 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2828 le32_to_cpu(nesqp->nesqp_context->misc));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002829 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2830 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2831 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2832
2833 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2834 cm_node->state = NES_CM_STATE_TSA;
2835
2836 return ret;
2837}
2838
2839
2840/**
2841 * nes_cm_disconn
2842 */
2843int nes_cm_disconn(struct nes_qp *nesqp)
2844{
Don Wood873fcdd2009-09-05 20:36:37 -07002845 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002846
Don Wood873fcdd2009-09-05 20:36:37 -07002847 work = kzalloc(sizeof *work, GFP_ATOMIC);
2848 if (!work)
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302849 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002850
Don Wood873fcdd2009-09-05 20:36:37 -07002851 nes_add_ref(&nesqp->ibqp);
2852 work->nesqp = nesqp;
2853 INIT_WORK(&work->work, nes_disconnect_worker);
2854 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002855 return 0;
2856}
2857
2858
2859/**
2860 * nes_disconnect_worker
2861 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002862static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002863{
Don Wood873fcdd2009-09-05 20:36:37 -07002864 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2865 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002866
Don Wood873fcdd2009-09-05 20:36:37 -07002867 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002868 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302869 nesqp->last_aeq, nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002870 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002871 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002872}
2873
2874
2875/**
2876 * nes_cm_disconn_true
2877 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002878static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002879{
2880 unsigned long flags;
2881 int ret = 0;
2882 struct iw_cm_id *cm_id;
2883 struct iw_cm_event cm_event;
2884 struct nes_vnic *nesvnic;
2885 u16 last_ae;
2886 u8 original_hw_tcp_state;
2887 u8 original_ibqp_state;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002888 int disconn_status = 0;
Don Woodb29a4fc2009-09-05 20:36:39 -07002889 int issue_disconn = 0;
2890 int issue_close = 0;
2891 int issue_flush = 0;
2892 u32 flush_q = NES_CQP_FLUSH_RQ;
2893 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002894
2895 if (!nesqp) {
2896 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2897 return -1;
2898 }
2899
2900 spin_lock_irqsave(&nesqp->lock, flags);
2901 cm_id = nesqp->cm_id;
2902 /* make sure we havent already closed this connection */
2903 if (!cm_id) {
2904 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302905 nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002906 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002907 return -1;
2908 }
2909
2910 nesvnic = to_nesvnic(nesqp->ibqp.device);
2911 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2912
2913 original_hw_tcp_state = nesqp->hw_tcp_state;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302914 original_ibqp_state = nesqp->ibqp_state;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002915 last_ae = nesqp->last_aeq;
2916
Don Woodb29a4fc2009-09-05 20:36:39 -07002917 if (nesqp->term_flags) {
2918 issue_disconn = 1;
2919 issue_close = 1;
2920 nesqp->cm_id = NULL;
Tatyana Nikolova196f40c2011-12-05 11:39:12 -06002921 del_timer(&nesqp->terminate_timer);
Don Woodb29a4fc2009-09-05 20:36:39 -07002922 if (nesqp->flush_issued == 0) {
2923 nesqp->flush_issued = 1;
2924 issue_flush = 1;
2925 }
2926 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2927 ((original_ibqp_state == IB_QPS_RTS) &&
2928 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2929 issue_disconn = 1;
2930 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002931 disconn_status = -ECONNRESET;
Don Woodb29a4fc2009-09-05 20:36:39 -07002932 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002933
Don Woodb29a4fc2009-09-05 20:36:39 -07002934 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2935 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2936 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2937 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2938 issue_close = 1;
2939 nesqp->cm_id = NULL;
2940 if (nesqp->flush_issued == 0) {
2941 nesqp->flush_issued = 1;
2942 issue_flush = 1;
2943 }
2944 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002945
Don Woodb29a4fc2009-09-05 20:36:39 -07002946 spin_unlock_irqrestore(&nesqp->lock, flags);
2947
2948 if ((issue_flush) && (nesqp->destroyed == 0)) {
2949 /* Flush the queue(s) */
2950 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2951 flush_q |= NES_CQP_FLUSH_SQ;
2952 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2953
2954 if (nesqp->term_flags) {
2955 ibevent.device = nesqp->ibqp.device;
2956 ibevent.event = nesqp->terminate_eventtype;
2957 ibevent.element.qp = &nesqp->ibqp;
Tatyana Nikolova784d1352012-05-11 14:29:32 -05002958 if (nesqp->ibqp.event_handler)
2959 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
Don Woodb29a4fc2009-09-05 20:36:39 -07002960 }
2961 }
2962
2963 if ((cm_id) && (cm_id->event_handler)) {
2964 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002965 atomic_inc(&cm_disconnects);
2966 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002967 cm_event.status = disconn_status;
Faisal Latif6a0dde82016-02-26 09:18:02 -06002968 cm_event.local_addr = cm_id->m_local_addr;
2969 cm_event.remote_addr = cm_id->m_remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002970 cm_event.private_data = NULL;
2971 cm_event.private_data_len = 0;
2972
Faisal Latif6492cdf2008-07-24 20:50:45 -07002973 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302974 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2975 "cm_id = %p, refcount = %u.\n",
2976 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2977 nesqp->hwqp.sq_tail, cm_id,
2978 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002979
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002980 ret = cm_id->event_handler(cm_id, &cm_event);
2981 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002982 nes_debug(NES_DBG_CM, "OFA CM event_handler "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302983 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002984 }
2985
Don Woodb29a4fc2009-09-05 20:36:39 -07002986 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002987 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002988 nes_disconnect(nesqp, 1);
2989
2990 cm_id->provider_data = nesqp;
2991 /* Send up the close complete event */
2992 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002993 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002994 cm_event.provider_data = cm_id->provider_data;
Faisal Latif6a0dde82016-02-26 09:18:02 -06002995 cm_event.local_addr = cm_id->m_local_addr;
2996 cm_event.remote_addr = cm_id->m_remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002997 cm_event.private_data = NULL;
2998 cm_event.private_data_len = 0;
2999
3000 ret = cm_id->event_handler(cm_id, &cm_event);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303001 if (ret)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003002 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003003
3004 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003005 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003006 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003007
3008 return 0;
3009}
3010
3011
3012/**
3013 * nes_disconnect
3014 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003015static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003016{
3017 int ret = 0;
3018 struct nes_vnic *nesvnic;
3019 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003020 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003021
3022 nesvnic = to_nesvnic(nesqp->ibqp.device);
3023 if (!nesvnic)
3024 return -EINVAL;
3025
3026 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003027 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003028
3029 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00003030 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003031
3032 if (nesqp->active_conn) {
3033
3034 /* indicate this connection is NOT active */
3035 nesqp->active_conn = 0;
3036 } else {
3037 /* Need to free the Last Streaming Mode Message */
3038 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07003039 if (nesqp->lsmm_mr)
3040 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003041 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303042 nesqp->private_data_len + nesqp->ietf_frame_size,
3043 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003044 }
3045 }
3046
3047 /* close the CM node down if it is still active */
3048 if (nesqp->cm_node) {
3049 nes_debug(NES_DBG_CM, "Call close API\n");
3050
3051 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003052 }
3053
3054 return ret;
3055}
3056
3057
3058/**
3059 * nes_accept
3060 */
3061int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3062{
3063 u64 u64temp;
3064 struct ib_qp *ibqp;
3065 struct nes_qp *nesqp;
3066 struct nes_vnic *nesvnic;
3067 struct nes_device *nesdev;
3068 struct nes_cm_node *cm_node;
3069 struct nes_adapter *adapter;
3070 struct ib_qp_attr attr;
3071 struct iw_cm_event cm_event;
3072 struct nes_hw_qp_wqe *wqe;
3073 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003074 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003075 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003076 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003077 struct nes_ib_device *nesibdev;
3078 struct ib_mr *ibmr = NULL;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003079 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07003080 u64 tagged_offset;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303081 u8 mpa_frame_offset = 0;
3082 struct ietf_mpa_v2 *mpa_v2_frame;
3083 u8 start_addr = 0;
3084 u8 *start_ptr = &start_addr;
3085 u8 **start_buff = &start_ptr;
3086 u16 buff_len = 0;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003087 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
3088 struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003089
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003090 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3091 if (!ibqp)
3092 return -EINVAL;
3093
3094 /* get all our handles */
3095 nesqp = to_nesqp(ibqp);
3096 nesvnic = to_nesvnic(nesqp->ibqp.device);
3097 nesdev = nesvnic->nesdev;
3098 adapter = nesdev->nesadapter;
3099
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003100 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003101 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
3102 "%s\n", cm_node, nesvnic, nesvnic->netdev,
3103 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003104
Faisal Latifc5a7d482009-12-09 15:54:08 -08003105 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
3106 if (cm_node->loopbackpartner)
3107 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
3108 rem_ref_cm_node(cm_node->cm_core, cm_node);
3109 return -EINVAL;
3110 }
3111
Faisal Latifdae58722010-08-14 21:04:56 +00003112 passive_state = atomic_add_return(1, &cm_node->passive_state);
3113 if (passive_state == NES_SEND_RESET_EVENT) {
3114 rem_ref_cm_node(cm_node->cm_core, cm_node);
3115 return -ECONNRESET;
3116 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003117 /* associate the node with the QP */
3118 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003119 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003120
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003121
Faisal Latif6492cdf2008-07-24 20:50:45 -07003122 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
3123 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003124 atomic_inc(&cm_accepts);
3125
3126 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00003127 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003128
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303129 nesqp->ietf_frame_size = sizeof(struct ietf_mpa_v2);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003130 /* allocate the ietf frame and space for private data */
3131 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303132 nesqp->ietf_frame_size + conn_param->private_data_len,
3133 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003134
Faisal Latif6492cdf2008-07-24 20:50:45 -07003135 if (!nesqp->ietf_frame) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303136 nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07003137 return -ENOMEM;
3138 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303139 mpa_v2_frame = (struct ietf_mpa_v2 *)nesqp->ietf_frame;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003140
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303141 if (cm_node->mpa_frame_rev == IETF_MPA_V1)
3142 mpa_frame_offset = 4;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003143
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003144 if (cm_node->mpa_frame_rev == IETF_MPA_V1 ||
3145 cm_node->mpav2_ird_ord == IETF_NO_IRD_ORD) {
3146 record_ird_ord(cm_node, (u16)conn_param->ird, (u16)conn_param->ord);
3147 }
3148
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303149 memcpy(mpa_v2_frame->priv_data, conn_param->private_data,
3150 conn_param->private_data_len);
3151
3152 cm_build_mpa_frame(cm_node, start_buff, &buff_len, nesqp->ietf_frame, MPA_KEY_REPLY);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003153 nesqp->private_data_len = conn_param->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003154
Faisal Latif6492cdf2008-07-24 20:50:45 -07003155 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
3156 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003157
Steve Wise24d44a32013-07-04 16:10:44 +05303158 if (raddr->sin_addr.s_addr != laddr->sin_addr.s_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003159 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003160 nesibdev = nesvnic->nesibdev;
3161 nespd = nesqp->nespd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303162 tagged_offset = (u64)(unsigned long)*start_buff;
Christoph Hellwigf64054d2015-12-23 19:12:50 +01003163 ibmr = nes_reg_phys_mr(&nespd->ibpd,
3164 nesqp->ietf_frame_pbase + mpa_frame_offset,
3165 buff_len, IB_ACCESS_LOCAL_WRITE,
3166 &tagged_offset);
Dan Carpenterbc1251e2016-01-12 12:27:43 +03003167 if (IS_ERR(ibmr)) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07003168 nes_debug(NES_DBG_CM, "Unable to register memory region"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303169 "for lSMM for cm_node = %p \n",
3170 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07003171 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303172 nesqp->private_data_len + nesqp->ietf_frame_size,
3173 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Dan Carpenterbc1251e2016-01-12 12:27:43 +03003174 return PTR_ERR(ibmr);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003175 }
3176
3177 ibmr->pd = &nespd->ibpd;
3178 ibmr->device = nespd->ibpd.device;
3179 nesqp->lsmm_mr = ibmr;
3180
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303181 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003182 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303183 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
3184 u64temp);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003185 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3186 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303187 NES_IWARP_SQ_WQE_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003188 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303189 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003190 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303191 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3192 (u64)(unsigned long)(*start_buff));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003193 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303194 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003195 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08003196 if (nesqp->sq_kmapped) {
3197 nesqp->sq_kmapped = 0;
3198 kunmap(nesqp->page);
3199 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003200
Faisal Latif6492cdf2008-07-24 20:50:45 -07003201 nesqp->nesqp_context->ird_ord_sizes |=
3202 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303203 NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003204 } else {
3205 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08003206 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003207 }
3208 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003209
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003210 /* Cache the cm_id in the qp */
3211 nesqp->cm_id = cm_id;
3212 cm_node->cm_id = cm_id;
3213
3214 /* nesqp->cm_node = (void *)cm_id->provider_data; */
3215 cm_id->provider_data = nesqp;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303216 nesqp->active_conn = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003217
Faisal Latif6492cdf2008-07-24 20:50:45 -07003218 if (cm_node->state == NES_CM_STATE_TSA)
3219 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303220 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003221
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003222 nes_cm_init_tsa_conn(nesqp, cm_node);
3223
Tatyana Nikolova56472632014-03-26 17:07:57 -05003224 nesqp->nesqp_context->tcpPorts[0] =
Faisal Latif6a0dde82016-02-26 09:18:02 -06003225 cpu_to_le16(cm_node->loc_port);
Tatyana Nikolova56472632014-03-26 17:07:57 -05003226 nesqp->nesqp_context->tcpPorts[1] =
Faisal Latif6a0dde82016-02-26 09:18:02 -06003227 cpu_to_le16(cm_node->rem_port);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003228
Faisal Latif6a0dde82016-02-26 09:18:02 -06003229 nesqp->nesqp_context->ip0 = cpu_to_le32(cm_node->rem_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003230
3231 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303232 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3233 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003234
Faisal Latif6492cdf2008-07-24 20:50:45 -07003235 nesqp->nesqp_context->arp_index_vlan |=
3236 cpu_to_le32(nes_arp_table(nesdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303237 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
3238 NES_ARP_RESOLVE) << 16);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003239
3240 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003241 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003242
3243 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3244
3245 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003246 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
3247 nesqp->nesqp_context->ird_ord_sizes |=
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003248 cpu_to_le32((u32)cm_node->ord_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003249
3250 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003251 nes_quad.DstIpAdrIndex =
3252 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
Faisal Latif6a0dde82016-02-26 09:18:02 -06003253 nes_quad.SrcIpadr = htonl(cm_node->rem_addr);
3254 nes_quad.TcpPorts[0] = htons(cm_node->rem_port);
3255 nes_quad.TcpPorts[1] = htons(cm_node->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003256
3257 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003258 crc_value = get_crc_value(&nes_quad);
3259 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003260 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303261 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003262
3263 nesqp->hte_index &= adapter->hte_index_mask;
3264 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3265
3266 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3267
Faisal Latif6492cdf2008-07-24 20:50:45 -07003268 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303269 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
3270 "private data length=%u.\n", nesqp->hwqp.qp_id,
Steve Wise24d44a32013-07-04 16:10:44 +05303271 ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port),
3272 ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303273 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
3274 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
3275 buff_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003276
Coly Li73ac36e2009-01-07 18:09:16 -08003277 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003278 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07003279 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003280
3281 cm_event.event = IW_CM_EVENT_ESTABLISHED;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003282 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003283 cm_event.provider_data = (void *)nesqp;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003284 cm_event.local_addr = cm_id->m_local_addr;
3285 cm_event.remote_addr = cm_id->m_remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003286 cm_event.private_data = NULL;
3287 cm_event.private_data_len = 0;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003288 cm_event.ird = cm_node->ird_size;
3289 cm_event.ord = cm_node->ord_size;
3290
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003291 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003292 attr.qp_state = IB_QPS_RTS;
3293 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003294 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003295 cm_node->loopbackpartner->mpa_frame_size =
3296 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003297 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003298 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303299 conn_param->private_data, conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003300 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
3301 }
3302 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003303 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303304 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003305
3306 return 0;
3307}
3308
3309
3310/**
3311 * nes_reject
3312 */
3313int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3314{
3315 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003316 struct nes_cm_node *loopback;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003317 struct nes_cm_core *cm_core;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303318 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003319
3320 atomic_inc(&cm_rejects);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303321 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003322 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003323 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003324 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003325
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303326 if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003327 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003328
Faisal Latif9d5ab132009-03-06 15:15:01 -08003329 if (loopback) {
3330 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3331 loopback->mpa_frame.priv_data_len = pdata_len;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303332 loopback->mpa_frame_size = pdata_len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003333 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303334 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
3335 cm_node->mpa_frame_size = pdata_len;
3336 memcpy(start_buff, pdata, pdata_len);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003337 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303338 return cm_core->api->reject(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003339}
3340
3341
3342/**
3343 * nes_connect
3344 * setup and launch cm connect node
3345 */
3346int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3347{
3348 struct ib_qp *ibqp;
3349 struct nes_qp *nesqp;
3350 struct nes_vnic *nesvnic;
3351 struct nes_device *nesdev;
3352 struct nes_cm_node *cm_node;
3353 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003354 int apbvt_set = 0;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003355 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
3356 struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003357
Steve Wise24d44a32013-07-04 16:10:44 +05303358 if (cm_id->remote_addr.ss_family != AF_INET)
3359 return -ENOSYS;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003360 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3361 if (!ibqp)
3362 return -EINVAL;
3363 nesqp = to_nesqp(ibqp);
3364 if (!nesqp)
3365 return -EINVAL;
3366 nesvnic = to_nesvnic(nesqp->ibqp.device);
3367 if (!nesvnic)
3368 return -EINVAL;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303369 nesdev = nesvnic->nesdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003370 if (!nesdev)
3371 return -EINVAL;
3372
Steve Wise24d44a32013-07-04 16:10:44 +05303373 if (!laddr->sin_port || !raddr->sin_port)
Faisal Latifc5a7d482009-12-09 15:54:08 -08003374 return -EINVAL;
3375
Faisal Latif6492cdf2008-07-24 20:50:45 -07003376 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303377 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
Steve Wise24d44a32013-07-04 16:10:44 +05303378 ntohl(nesvnic->local_ipaddr), ntohl(raddr->sin_addr.s_addr),
3379 ntohs(raddr->sin_port), ntohl(laddr->sin_addr.s_addr),
3380 ntohs(laddr->sin_port));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003381
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003382 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003383 nesqp->active_conn = 1;
3384
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003385 /* cache the cm_id in the qp */
3386 nesqp->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003387 cm_id->provider_data = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003388 nesqp->private_data_len = conn_param->private_data_len;
Tatyana Nikolovad3e51322012-05-11 10:46:16 -05003389
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003390 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003391 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303392 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003393
Tatyana Nikolova56472632014-03-26 17:07:57 -05003394 /* set up the connection params for the node */
3395 cm_info.loc_addr = ntohl(laddr->sin_addr.s_addr);
3396 cm_info.loc_port = ntohs(laddr->sin_port);
3397 cm_info.rem_addr = ntohl(raddr->sin_addr.s_addr);
3398 cm_info.rem_port = ntohs(raddr->sin_port);
3399 cm_info.cm_id = cm_id;
3400 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3401
Steve Wise24d44a32013-07-04 16:10:44 +05303402 if (laddr->sin_addr.s_addr != raddr->sin_addr.s_addr) {
Faisal Latif6a0dde82016-02-26 09:18:02 -06003403 nes_manage_apbvt(nesvnic, cm_info.loc_port,
3404 PCI_FUNC(nesdev->pcidev->devfn),
3405 NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003406 apbvt_set = 1;
3407 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003408
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003409 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003410
3411 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003412 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303413 conn_param->private_data_len, (void *)conn_param->private_data,
3414 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003415 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003416 if (apbvt_set)
Faisal Latif6a0dde82016-02-26 09:18:02 -06003417 nes_manage_apbvt(nesvnic, cm_info.loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303418 PCI_FUNC(nesdev->pcidev->devfn),
3419 NES_MANAGE_APBVT_DEL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003420
Faisal Latif6a0dde82016-02-26 09:18:02 -06003421 nes_debug(NES_DBG_NLMSG, "Delete loc_port = %04X\n",
3422 cm_info.loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003423 cm_id->rem_ref(cm_id);
3424 return -ENOMEM;
3425 }
3426
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003427 record_ird_ord(cm_node, (u16)conn_param->ird, (u16)conn_param->ord);
3428 if (cm_node->send_rdma0_op == SEND_RDMA_READ_ZERO &&
3429 cm_node->ord_size == 0)
3430 cm_node->ord_size = 1;
3431
Faisal Latif53094c32009-04-27 13:37:34 -07003432 cm_node->apbvt_set = apbvt_set;
Faisal Latif854ace982015-05-18 15:28:14 -05003433 cm_node->tos = cm_id->tos;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003434 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003435 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003436 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003437
3438 return 0;
3439}
3440
3441
3442/**
3443 * nes_create_listen
3444 */
3445int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3446{
3447 struct nes_vnic *nesvnic;
3448 struct nes_cm_listener *cm_node;
3449 struct nes_cm_info cm_info;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003450 int err;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003451 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003452
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003453 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303454 cm_id, ntohs(laddr->sin_port));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003455
Faisal Latif6a0dde82016-02-26 09:18:02 -06003456 if (cm_id->m_local_addr.ss_family != AF_INET)
Steve Wise24d44a32013-07-04 16:10:44 +05303457 return -ENOSYS;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003458 nesvnic = to_nesvnic(cm_id->device);
3459 if (!nesvnic)
3460 return -EINVAL;
Roland Dreier69d51022010-08-04 14:25:40 -07003461
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003462 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3463 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3464
3465 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303466 nesvnic->local_ipaddr, laddr->sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003467
3468 /* setup listen params in our api call struct */
Tatyana Nikolova56472632014-03-26 17:07:57 -05003469 cm_info.loc_addr = ntohl(nesvnic->local_ipaddr);
3470 cm_info.loc_port = ntohs(laddr->sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003471 cm_info.backlog = backlog;
3472 cm_info.cm_id = cm_id;
3473
3474 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3475
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003476 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3477 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003478 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303479 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003480 return -ENOMEM;
3481 }
3482
3483 cm_id->provider_data = cm_node;
Faisal Latif854ace982015-05-18 15:28:14 -05003484 cm_node->tos = cm_id->tos;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003485
3486 if (!cm_node->reused_node) {
Faisal Latif6a0dde82016-02-26 09:18:02 -06003487 err = nes_manage_apbvt(nesvnic, cm_node->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303488 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3489 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003490 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003491 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303492 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003493 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3494 return err;
3495 }
Faisal Latif6e10d2e2010-02-12 19:55:03 +00003496 atomic_inc(&cm_listens_created);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003497 }
3498
3499 cm_id->add_ref(cm_id);
3500 cm_id->provider_data = (void *)cm_node;
3501
3502
3503 return 0;
3504}
3505
3506
3507/**
3508 * nes_destroy_listen
3509 */
3510int nes_destroy_listen(struct iw_cm_id *cm_id)
3511{
3512 if (cm_id->provider_data)
3513 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3514 else
3515 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3516
3517 cm_id->rem_ref(cm_id);
3518
3519 return 0;
3520}
3521
3522
3523/**
3524 * nes_cm_recv
3525 */
3526int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3527{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003528 int rc = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303529
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003530 cm_packets_received++;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303531 if ((g_cm_core) && (g_cm_core->api))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003532 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303533 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003534 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303535 " cm is not setup properly.\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003536
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003537 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003538}
3539
3540
3541/**
3542 * nes_cm_start
3543 * Start and init a cm core module
3544 */
3545int nes_cm_start(void)
3546{
3547 nes_debug(NES_DBG_CM, "\n");
3548 /* create the primary CM core, pass this handle to subsequent core inits */
3549 g_cm_core = nes_cm_alloc_core();
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303550 if (g_cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003551 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303552 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003553 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003554}
3555
3556
3557/**
3558 * nes_cm_stop
3559 * stop and dealloc all cm core instances
3560 */
3561int nes_cm_stop(void)
3562{
3563 g_cm_core->api->destroy_cm_core(g_cm_core);
3564 return 0;
3565}
3566
3567
3568/**
3569 * cm_event_connected
3570 * handle a connected event, setup QPs and HW
3571 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003572static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003573{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003574 struct nes_qp *nesqp;
3575 struct nes_vnic *nesvnic;
3576 struct nes_device *nesdev;
3577 struct nes_cm_node *cm_node;
3578 struct nes_adapter *nesadapter;
3579 struct ib_qp_attr attr;
3580 struct iw_cm_id *cm_id;
3581 struct iw_cm_event cm_event;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003582 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003583 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003584 int ret;
Steve Wise24d44a32013-07-04 16:10:44 +05303585 struct sockaddr_in *laddr;
3586 struct sockaddr_in *raddr;
3587 struct sockaddr_in *cm_event_laddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003588
3589 /* get all our handles */
3590 cm_node = event->cm_node;
3591 cm_id = cm_node->cm_id;
3592 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3593 nesqp = (struct nes_qp *)cm_id->provider_data;
3594 nesvnic = to_nesvnic(nesqp->ibqp.device);
3595 nesdev = nesvnic->nesdev;
3596 nesadapter = nesdev->nesadapter;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003597 laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
3598 raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
Steve Wise24d44a32013-07-04 16:10:44 +05303599 cm_event_laddr = (struct sockaddr_in *)&cm_event.local_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003600
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303601 if (nesqp->destroyed)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003602 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003603 atomic_inc(&cm_connecteds);
3604 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303605 " local port 0x%04X. jiffies = %lu.\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303606 nesqp->hwqp.qp_id, ntohl(raddr->sin_addr.s_addr),
3607 ntohs(raddr->sin_port), ntohs(laddr->sin_port), jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003608
3609 nes_cm_init_tsa_conn(nesqp, cm_node);
3610
3611 /* set the QP tsa context */
Tatyana Nikolova56472632014-03-26 17:07:57 -05003612 nesqp->nesqp_context->tcpPorts[0] =
Faisal Latif6a0dde82016-02-26 09:18:02 -06003613 cpu_to_le16(cm_node->loc_port);
Tatyana Nikolova56472632014-03-26 17:07:57 -05003614 nesqp->nesqp_context->tcpPorts[1] =
Faisal Latif6a0dde82016-02-26 09:18:02 -06003615 cpu_to_le16(cm_node->rem_port);
3616 nesqp->nesqp_context->ip0 = cpu_to_le32(cm_node->rem_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003617
3618 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003619 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3620 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003621 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003622 nes_arp_table(nesdev,
3623 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003624 NULL, NES_ARP_RESOLVE) << 16);
3625 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3626 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3627 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3628 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003629 cpu_to_le32((u32)1 <<
3630 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003631 nesqp->nesqp_context->ird_ord_sizes |=
3632 cpu_to_le32((u32)cm_node->ord_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003633
3634 /* Adjust tail for not having a LSMM */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303635 /*nesqp->hwqp.sq_tail = 1;*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003636
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303637 build_rdma0_msg(cm_node, &nesqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003638
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303639 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3640 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003641
3642 memset(&nes_quad, 0, sizeof(nes_quad));
3643
Faisal Latif6492cdf2008-07-24 20:50:45 -07003644 nes_quad.DstIpAdrIndex =
3645 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
Faisal Latif6a0dde82016-02-26 09:18:02 -06003646 nes_quad.SrcIpadr = htonl(cm_node->rem_addr);
3647 nes_quad.TcpPorts[0] = htons(cm_node->rem_port);
3648 nes_quad.TcpPorts[1] = htons(cm_node->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003649
3650 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003651 crc_value = get_crc_value(&nes_quad);
3652 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003653 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303654 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003655
3656 nesqp->hte_index &= nesadapter->hte_index_mask;
3657 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3658
3659 nesqp->ietf_frame = &cm_node->mpa_frame;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303660 nesqp->private_data_len = (u8)cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003661 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3662
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003663 /* notify OF layer we successfully created the requested connection */
3664 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003665 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003666 cm_event.provider_data = cm_id->provider_data;
Steve Wise24d44a32013-07-04 16:10:44 +05303667 cm_event_laddr->sin_family = AF_INET;
3668 cm_event_laddr->sin_port = laddr->sin_port;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003669 cm_event.remote_addr = cm_id->m_remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003670
Faisal Latif6492cdf2008-07-24 20:50:45 -07003671 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303672 cm_event.private_data_len = (u8)event->cm_node->mpa_frame_size;
3673 cm_event.ird = cm_node->ird_size;
3674 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003675
Faisal Latif6a0dde82016-02-26 09:18:02 -06003676 cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003677 ret = cm_id->event_handler(cm_id, &cm_event);
3678 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3679
3680 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003681 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303682 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003683 attr.qp_state = IB_QPS_RTS;
3684 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003685
Faisal Latif6492cdf2008-07-24 20:50:45 -07003686 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303687 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003688
3689 return;
3690}
3691
3692
3693/**
3694 * cm_event_connect_error
3695 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003696static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003697{
3698 struct nes_qp *nesqp;
3699 struct iw_cm_id *cm_id;
3700 struct iw_cm_event cm_event;
3701 /* struct nes_cm_info cm_info; */
3702 int ret;
3703
3704 if (!event->cm_node)
3705 return;
3706
3707 cm_id = event->cm_node->cm_id;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303708 if (!cm_id)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003709 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003710
3711 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3712 nesqp = cm_id->provider_data;
3713
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303714 if (!nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003715 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003716
3717 /* notify OF layer about this connection error event */
3718 /* cm_id->rem_ref(cm_id); */
3719 nesqp->cm_id = NULL;
3720 cm_id->provider_data = NULL;
3721 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003722 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003723 cm_event.provider_data = cm_id->provider_data;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003724 cm_event.local_addr = cm_id->m_local_addr;
3725 cm_event.remote_addr = cm_id->m_remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003726 cm_event.private_data = NULL;
3727 cm_event.private_data_len = 0;
3728
Steve Wise24d44a32013-07-04 16:10:44 +05303729#ifdef CONFIG_INFINIBAND_NES_DEBUG
3730 {
3731 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3732 &cm_event.local_addr;
3733 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3734 &cm_event.remote_addr;
3735 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, remote_addr=%08x\n",
3736 cm_event_laddr->sin_addr.s_addr, cm_event_raddr->sin_addr.s_addr);
3737 }
3738#endif
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003739
3740 ret = cm_id->event_handler(cm_id, &cm_event);
3741 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3742 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003743 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303744 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003745 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003746
Faisal Latif6492cdf2008-07-24 20:50:45 -07003747 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003748 return;
3749}
3750
3751
3752/**
3753 * cm_event_reset
3754 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003755static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003756{
3757 struct nes_qp *nesqp;
3758 struct iw_cm_id *cm_id;
3759 struct iw_cm_event cm_event;
3760 /* struct nes_cm_info cm_info; */
3761 int ret;
3762
3763 if (!event->cm_node)
3764 return;
3765
3766 if (!event->cm_node->cm_id)
3767 return;
3768
3769 cm_id = event->cm_node->cm_id;
3770
3771 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3772 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003773 if (!nesqp)
3774 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003775
3776 nesqp->cm_id = NULL;
3777 /* cm_id->provider_data = NULL; */
3778 cm_event.event = IW_CM_EVENT_DISCONNECT;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003779 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003780 cm_event.provider_data = cm_id->provider_data;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003781 cm_event.local_addr = cm_id->m_local_addr;
3782 cm_event.remote_addr = cm_id->m_remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003783 cm_event.private_data = NULL;
3784 cm_event.private_data_len = 0;
3785
Faisal Latif183ecfa2008-11-21 20:50:46 -06003786 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003787 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003788 atomic_inc(&cm_closes);
3789 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003790 cm_event.status = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003791 cm_event.provider_data = cm_id->provider_data;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003792 cm_event.local_addr = cm_id->m_local_addr;
3793 cm_event.remote_addr = cm_id->m_remote_addr;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003794 cm_event.private_data = NULL;
3795 cm_event.private_data_len = 0;
3796 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3797 ret = cm_id->event_handler(cm_id, &cm_event);
3798
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003799 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3800
3801
3802 /* notify OF layer about this connection error event */
3803 cm_id->rem_ref(cm_id);
3804
3805 return;
3806}
3807
3808
3809/**
3810 * cm_event_mpa_req
3811 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003812static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003813{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303814 struct iw_cm_id *cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003815 struct iw_cm_event cm_event;
3816 int ret;
3817 struct nes_cm_node *cm_node;
Steve Wise24d44a32013-07-04 16:10:44 +05303818 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3819 &cm_event.local_addr;
3820 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3821 &cm_event.remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003822
3823 cm_node = event->cm_node;
3824 if (!cm_node)
3825 return;
3826 cm_id = cm_node->cm_id;
3827
3828 atomic_inc(&cm_connect_reqs);
3829 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303830 cm_node, cm_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003831
3832 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003833 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003834 cm_event.provider_data = (void *)cm_node;
3835
Steve Wise24d44a32013-07-04 16:10:44 +05303836 cm_event_laddr->sin_family = AF_INET;
3837 cm_event_laddr->sin_port = htons(event->cm_info.loc_port);
3838 cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003839
Steve Wise24d44a32013-07-04 16:10:44 +05303840 cm_event_raddr->sin_family = AF_INET;
3841 cm_event_raddr->sin_port = htons(event->cm_info.rem_port);
3842 cm_event_raddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003843 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303844 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003845 if (cm_node->mpa_frame_rev == IETF_MPA_V1) {
3846 cm_event.ird = NES_MAX_IRD;
3847 cm_event.ord = NES_MAX_ORD;
3848 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303849 cm_event.ird = cm_node->ird_size;
3850 cm_event.ord = cm_node->ord_size;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003851 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003852
3853 ret = cm_id->event_handler(cm_id, &cm_event);
3854 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003855 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303856 __func__, __LINE__, ret);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003857 return;
3858}
3859
3860
3861static void cm_event_mpa_reject(struct nes_cm_event *event)
3862{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303863 struct iw_cm_id *cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003864 struct iw_cm_event cm_event;
3865 struct nes_cm_node *cm_node;
3866 int ret;
Steve Wise24d44a32013-07-04 16:10:44 +05303867 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3868 &cm_event.local_addr;
3869 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3870 &cm_event.remote_addr;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003871
3872 cm_node = event->cm_node;
3873 if (!cm_node)
3874 return;
3875 cm_id = cm_node->cm_id;
3876
3877 atomic_inc(&cm_connect_reqs);
3878 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303879 cm_node, cm_id, jiffies);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003880
3881 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3882 cm_event.status = -ECONNREFUSED;
3883 cm_event.provider_data = cm_id->provider_data;
3884
Steve Wise24d44a32013-07-04 16:10:44 +05303885 cm_event_laddr->sin_family = AF_INET;
3886 cm_event_laddr->sin_port = htons(event->cm_info.loc_port);
3887 cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003888
Steve Wise24d44a32013-07-04 16:10:44 +05303889 cm_event_raddr->sin_family = AF_INET;
3890 cm_event_raddr->sin_port = htons(event->cm_info.rem_port);
3891 cm_event_raddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003892
3893 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303894 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003895
3896 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303897 "remove_addr=%08x\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303898 cm_event_laddr->sin_addr.s_addr,
3899 cm_event_raddr->sin_addr.s_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003900
3901 ret = cm_id->event_handler(cm_id, &cm_event);
3902 if (ret)
3903 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303904 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003905
3906 return;
3907}
3908
3909
3910static void nes_cm_event_handler(struct work_struct *);
3911
3912/**
3913 * nes_cm_post_event
3914 * post an event to the cm event handler
3915 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003916static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003917{
3918 atomic_inc(&event->cm_node->cm_core->events_posted);
3919 add_ref_cm_node(event->cm_node);
3920 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3921 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003922 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303923 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003924
3925 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3926
3927 nes_debug(NES_DBG_CM, "Exit\n");
3928 return 0;
3929}
3930
3931
3932/**
3933 * nes_cm_event_handler
3934 * worker function to handle cm events
3935 * will free instance of nes_cm_event
3936 */
3937static void nes_cm_event_handler(struct work_struct *work)
3938{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003939 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303940 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003941 struct nes_cm_core *cm_core;
3942
Faisal Latif6492cdf2008-07-24 20:50:45 -07003943 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003944 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003945
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003946 cm_core = event->cm_node->cm_core;
3947 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303948 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003949
3950 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003951 case NES_CM_EVENT_MPA_REQ:
3952 cm_event_mpa_req(event);
3953 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303954 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003955 break;
3956 case NES_CM_EVENT_RESET:
3957 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303958 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003959 cm_event_reset(event);
3960 break;
3961 case NES_CM_EVENT_CONNECTED:
3962 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303963 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003964 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003965 cm_event_connected(event);
3966 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3967 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003968 case NES_CM_EVENT_MPA_REJECT:
3969 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303970 (event->cm_node->state == NES_CM_STATE_TSA))
Faisal Latif9d5ab132009-03-06 15:15:01 -08003971 break;
3972 cm_event_mpa_reject(event);
3973 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3974 break;
3975
Faisal Latif6492cdf2008-07-24 20:50:45 -07003976 case NES_CM_EVENT_ABORTED:
3977 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303978 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003979 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003980 cm_event_connect_error(event);
3981 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3982 break;
3983 case NES_CM_EVENT_DROPPED_PKT:
3984 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3985 break;
3986 default:
3987 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3988 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003989 }
3990
3991 atomic_dec(&cm_core->events_posted);
3992 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3993 rem_ref_cm_node(cm_core, event->cm_node);
3994 kfree(event);
3995
3996 return;
3997}