blob: 8e703479e7ce65044633953803c8114789ac3bc0 [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 = {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800138 mini_cm_accelerated,
139 mini_cm_listen,
140 mini_cm_del_listen,
141 mini_cm_connect,
142 mini_cm_close,
143 mini_cm_accept,
144 mini_cm_reject,
145 mini_cm_recv_pkt,
146 mini_cm_dealloc_core,
147 mini_cm_get,
148 mini_cm_set
149};
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;
613 ctrl_ird |= IETF_FLPDU_ZERO_LEN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530614
615 switch (mpa_key) {
616 case MPA_KEY_REQUEST:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800617 ctrl_ord |= IETF_RDMA0_WRITE;
618 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530619 break;
620 case MPA_KEY_REPLY:
621 switch (cm_node->send_rdma0_op) {
622 case SEND_RDMA_WRITE_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800623 ctrl_ord |= IETF_RDMA0_WRITE;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530624 break;
625 case SEND_RDMA_READ_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800626 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530627 break;
628 }
629 }
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800630 rtr_msg->ctrl_ird = htons(ctrl_ird);
631 rtr_msg->ctrl_ord = htons(ctrl_ord);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530632}
633
634/**
635 * build_mpa_v1 - build a MPA V1 frame
636 */
637static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
638{
639 struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
640
641 switch (mpa_key) {
642 case MPA_KEY_REQUEST:
643 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
644 break;
645 case MPA_KEY_REPLY:
646 memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
647 break;
648 }
649 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
650 mpa_frame->rev = cm_node->mpa_frame_rev;
651 mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
652}
653
654static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
655{
656 u64 u64temp;
657 struct nes_qp *nesqp = *nesqp_addr;
658 struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
659
Tatyana Nikolova43adff32014-03-11 14:20:17 -0500660 u64temp = (unsigned long)nesqp->nesuqp_addr;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530661 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
662 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
663
664 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
665 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
666
667 switch (cm_node->send_rdma0_op) {
668 case SEND_RDMA_WRITE_ZERO:
669 nes_debug(NES_DBG_CM, "Sending first write.\n");
670 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
671 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
672 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
673 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
674 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
675 break;
676
677 case SEND_RDMA_READ_ZERO:
678 default:
Julia Lawall079abea2012-11-03 10:58:37 +0000679 if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO)
680 WARN(1, "Unsupported RDMA0 len operation=%u\n",
681 cm_node->send_rdma0_op);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530682 nes_debug(NES_DBG_CM, "Sending first rdma operation.\n");
683 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
684 cpu_to_le32(NES_IWARP_SQ_OP_RDMAR);
685 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX] = 1;
686 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_HIGH_IDX] = 0;
687 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] = 0;
688 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_STAG_IDX] = 1;
689 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 1;
690 break;
691 }
692
693 if (nesqp->sq_kmapped) {
694 nesqp->sq_kmapped = 0;
695 kunmap(nesqp->page);
696 }
697
698 /*use the reserved spot on the WQ for the extra first WQE*/
699 nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
700 NES_QPCONTEXT_ORDIRD_WRPDU |
701 NES_QPCONTEXT_ORDIRD_ALSMM));
702 nesqp->skip_lsmm = 1;
703 nesqp->hwqp.sq_tail = 0;
704}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800705
706/**
707 * schedule_nes_timer
708 * note - cm_node needs to be protected before calling this. Encase in:
709 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
710 */
711int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530712 enum nes_timer_type type, int send_retrans,
713 int close_when_complete)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800714{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530715 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700716 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800717 struct nes_timer_entry *new_send;
718 int ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800719
720 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
721 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800722 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800723
724 /* new_send->timetosend = currenttime */
725 new_send->retrycount = NES_DEFAULT_RETRYS;
726 new_send->retranscount = NES_DEFAULT_RETRANS;
727 new_send->skb = skb;
728 new_send->timetosend = jiffies;
729 new_send->type = type;
730 new_send->netdev = cm_node->netdev;
731 new_send->send_retrans = send_retrans;
732 new_send->close_when_complete = close_when_complete;
733
734 if (type == NES_TIMER_TYPE_CLOSE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530735 new_send->timetosend += (HZ / 10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800736 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700737 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800738 WARN_ON(1);
739 return -EINVAL;
740 }
741 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800742 }
743
744 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700745 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800746 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700747 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
748 cm_node->send_entry = new_send;
749 add_ref_cm_node(cm_node);
750 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
751 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800752
753 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
754 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700755 nes_debug(NES_DBG_CM, "Error sending packet %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530756 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800757 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700758 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800759 } else {
760 cm_packets_sent++;
761 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700762 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800763 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700764 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800765 return ret;
766 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800767 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800768 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800769
Tatyana Nikolova00ad2552012-12-06 19:56:31 +0000770 if (!timer_pending(&cm_core->tcp_timer))
771 mod_timer(&cm_core->tcp_timer, new_send->timetosend);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800772
773 return ret;
774}
775
Faisal Latif9d5ab132009-03-06 15:15:01 -0800776static void nes_retrans_expired(struct nes_cm_node *cm_node)
777{
Faisal Latif68237a02009-06-22 22:53:28 -0700778 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latifdae58722010-08-14 21:04:56 +0000779 enum nes_cm_node_state state = cm_node->state;
780 cm_node->state = NES_CM_STATE_CLOSED;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530781
Faisal Latifdae58722010-08-14 21:04:56 +0000782 switch (state) {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800783 case NES_CM_STATE_SYN_RCVD:
784 case NES_CM_STATE_CLOSING:
785 rem_ref_cm_node(cm_node->cm_core, cm_node);
786 break;
787 case NES_CM_STATE_LAST_ACK:
788 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700789 if (cm_node->cm_id)
790 cm_id->rem_ref(cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800791 send_reset(cm_node, NULL);
792 break;
793 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800794 add_ref_cm_node(cm_node);
795 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800796 create_event(cm_node, NES_CM_EVENT_ABORTED);
797 }
798}
799
800static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
801{
802 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
803 struct iw_cm_id *cm_id = cm_node->cm_id;
804 struct nes_qp *nesqp;
805 unsigned long qplockflags;
806
807 if (!recv_entry)
808 return;
809 nesqp = (struct nes_qp *)recv_entry->skb;
810 if (nesqp) {
811 spin_lock_irqsave(&nesqp->lock, qplockflags);
812 if (nesqp->cm_id) {
813 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530814 "refcount = %d: HIT A "
815 "NES_TIMER_TYPE_CLOSE with something "
816 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
817 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800818 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
819 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
820 nesqp->ibqp_state = IB_QPS_ERR;
821 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
822 nes_cm_disconn(nesqp);
823 } else {
824 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
825 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530826 "refcount = %d: HIT A "
827 "NES_TIMER_TYPE_CLOSE with nothing "
828 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
829 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800830 }
831 } else if (rem_node) {
832 /* TIME_WAIT state */
833 rem_ref_cm_node(cm_node->cm_core, cm_node);
834 }
835 if (cm_node->cm_id)
836 cm_id->rem_ref(cm_id);
837 kfree(recv_entry);
838 cm_node->recv_entry = NULL;
839}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800840
841/**
842 * nes_cm_timer_tick
843 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -0700844static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800845{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800846 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800847 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800848 struct nes_cm_node *cm_node;
849 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800850 struct list_head *list_core_temp;
851 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800852 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800853 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700854 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800855 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800856
Faisal Latif879e5bd2008-11-21 20:50:41 -0600857 struct list_head timer_list;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530858
Faisal Latif879e5bd2008-11-21 20:50:41 -0600859 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800860 spin_lock_irqsave(&cm_core->ht_lock, flags);
861
Faisal Latif6492cdf2008-07-24 20:50:45 -0700862 list_for_each_safe(list_node, list_core_temp,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530863 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800864 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800865 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600866 add_ref_cm_node(cm_node);
867 list_add(&cm_node->timer_entry, &timer_list);
868 }
869 }
870 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
871
872 list_for_each_safe(list_node, list_core_temp, &timer_list) {
873 cm_node = container_of(list_node, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530874 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800875 recv_entry = cm_node->recv_entry;
876
877 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700878 if (time_after(recv_entry->timetosend, jiffies)) {
879 if (nexttimeout > recv_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530880 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800881 nexttimeout = recv_entry->timetosend;
882 settimer = 1;
883 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530884 } else {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800885 handle_recv_entry(cm_node, 1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530886 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800887 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800888
889 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700890 do {
891 send_entry = cm_node->send_entry;
892 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600893 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800894 if (time_after(send_entry->timetosend, jiffies)) {
895 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700896 if ((nexttimeout >
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530897 send_entry->timetosend) ||
898 !settimer) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700899 nexttimeout =
900 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800901 settimer = 1;
902 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800903 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700904 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800905 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800906 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800907 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700908
909 if ((cm_node->state == NES_CM_STATE_TSA) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530910 (cm_node->state == NES_CM_STATE_CLOSED)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700911 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600912 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800913 }
914
Faisal Latif6492cdf2008-07-24 20:50:45 -0700915 if (!send_entry->retranscount ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530916 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800917 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700918 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800919
Faisal Latif6492cdf2008-07-24 20:50:45 -0700920 spin_unlock_irqrestore(
921 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800922 nes_retrans_expired(cm_node);
923 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700924 spin_lock_irqsave(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530925 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600926 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800927 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800928 atomic_inc(&send_entry->skb->users);
929 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700930 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530931 "for node %p, jiffies = %lu, time to send = "
932 "%lu, retranscount = %u, send_entry->seq_num = "
933 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
934 "0x%08X\n", send_entry, cm_node, jiffies,
935 send_entry->timetosend,
936 send_entry->retranscount,
937 send_entry->seq_num,
938 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800939
Faisal Latif6492cdf2008-07-24 20:50:45 -0700940 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530941 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800942 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700943 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800944 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700945 nes_debug(NES_DBG_CM, "rexmit failed for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530946 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800947 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800948 send_entry->retrycount--;
949 nexttimeout = jiffies + NES_SHORT_TIME;
950 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600951 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800952 } else {
953 cm_packets_sent++;
954 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700955 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530956 "%u, retry count = %u.\n",
957 send_entry->retranscount,
958 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800959 if (send_entry->send_retrans) {
960 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700961 timetosend = (NES_RETRY_TIMEOUT <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530962 (NES_DEFAULT_RETRANS - send_entry->retranscount));
Faisal Latif4e9c3902009-04-27 13:39:36 -0700963
Faisal Latif6492cdf2008-07-24 20:50:45 -0700964 send_entry->timetosend = jiffies +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530965 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700966 if (nexttimeout > send_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530967 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800968 nexttimeout = send_entry->timetosend;
969 settimer = 1;
970 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800971 } else {
972 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700973 close_when_complete =
974 send_entry->close_when_complete;
975 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530976 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700977 free_retrans_entry(cm_node);
978 if (close_when_complete)
979 rem_ref_cm_node(cm_node->cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530980 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800981 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700982 } while (0);
983
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800984 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700985 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800986 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800987
988 if (settimer) {
Tatyana Nikolova00ad2552012-12-06 19:56:31 +0000989 if (!timer_pending(&cm_core->tcp_timer))
990 mod_timer(&cm_core->tcp_timer, nexttimeout);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800991 }
992}
993
994
995/**
996 * send_syn
997 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700998static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530999 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001000{
1001 int ret;
1002 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001003 char optionsbuffer[sizeof(struct option_mss) +
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301004 sizeof(struct option_windowscale) + sizeof(struct option_base) +
1005 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001006
1007 int optionssize = 0;
1008 /* Sending MSS option */
1009 union all_known_options *options;
1010
1011 if (!cm_node)
1012 return -EINVAL;
1013
1014 options = (union all_known_options *)&optionsbuffer[optionssize];
1015 options->as_mss.optionnum = OPTION_NUMBER_MSS;
1016 options->as_mss.length = sizeof(struct option_mss);
1017 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
1018 optionssize += sizeof(struct option_mss);
1019
1020 options = (union all_known_options *)&optionsbuffer[optionssize];
1021 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
1022 options->as_windowscale.length = sizeof(struct option_windowscale);
1023 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
1024 optionssize += sizeof(struct option_windowscale);
1025
Faisal Latif6492cdf2008-07-24 20:50:45 -07001026 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001027 options = (union all_known_options *)&optionsbuffer[optionssize];
1028 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
1029 options->as_base.length = sizeof(struct option_base);
1030 optionssize += sizeof(struct option_base);
1031 /* we need the size to be a multiple of 4 */
1032 options = (union all_known_options *)&optionsbuffer[optionssize];
1033 options->as_end = 1;
1034 optionssize += 1;
1035 options = (union all_known_options *)&optionsbuffer[optionssize];
1036 options->as_end = 1;
1037 optionssize += 1;
1038 }
1039
1040 options = (union all_known_options *)&optionsbuffer[optionssize];
1041 options->as_end = OPTION_NUMBER_END;
1042 optionssize += 1;
1043
Faisal Latif6492cdf2008-07-24 20:50:45 -07001044 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001045 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001046 if (!skb) {
1047 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1048 return -1;
1049 }
1050
1051 if (sendack)
1052 flags |= SET_ACK;
1053
1054 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
1055 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1056
1057 return ret;
1058}
1059
1060
1061/**
1062 * send_reset
1063 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001064static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001065{
1066 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001067 int flags = SET_RST | SET_ACK;
1068
Faisal Latif6492cdf2008-07-24 20:50:45 -07001069 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001070 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001071 if (!skb) {
1072 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001073 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001074 }
1075
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001076 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
1077 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
1078
1079 return ret;
1080}
1081
1082
1083/**
1084 * send_ack
1085 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001086static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001087{
1088 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001089
1090 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001091 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001092
1093 if (!skb) {
1094 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1095 return -1;
1096 }
1097
1098 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
1099 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
1100
1101 return ret;
1102}
1103
1104
1105/**
1106 * send_fin
1107 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07001108static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001109{
1110 int ret;
1111
1112 /* if we didn't get a frame get one */
1113 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001114 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001115
1116 if (!skb) {
1117 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1118 return -1;
1119 }
1120
1121 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
1122 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1123
1124 return ret;
1125}
1126
1127
1128/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001129 * find_node - find a cm node that matches the reference cm node
1130 */
1131static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301132 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001133{
1134 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001135 struct list_head *hte;
1136 struct nes_cm_node *cm_node;
1137
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001138 /* get a handle on the hte */
1139 hte = &cm_core->connected_nodes;
1140
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001141 /* walk list and find cm_node associated with this session ID */
1142 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001143 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001144 /* compare quad, return node handle if a match */
1145 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301146 cm_node->loc_addr, cm_node->loc_port,
1147 loc_addr, loc_port,
1148 cm_node->rem_addr, cm_node->rem_port,
1149 rem_addr, rem_port);
Faisal Latif6a0dde82016-02-26 09:18:02 -06001150 if ((cm_node->loc_addr == loc_addr) &&
1151 (cm_node->loc_port == loc_port) &&
1152 (cm_node->rem_addr == rem_addr) &&
1153 (cm_node->rem_port == rem_port)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001154 add_ref_cm_node(cm_node);
1155 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1156 return cm_node;
1157 }
1158 }
1159 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1160
1161 /* no owner node */
1162 return NULL;
1163}
1164
1165
1166/**
1167 * find_listener - find a cm node listening on this addr-port pair
1168 */
1169static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
Faisal Latif6a0dde82016-02-26 09:18:02 -06001170 nes_addr_t dst_addr, u16 dst_port,
1171 enum nes_cm_listener_state listener_state)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001172{
1173 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001174 struct nes_cm_listener *listen_node;
Tatyana Nikolova56472632014-03-26 17:07:57 -05001175 nes_addr_t listen_addr;
1176 u16 listen_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001177
1178 /* walk list and find cm_node associated with this session ID */
1179 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001180 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Faisal Latif6a0dde82016-02-26 09:18:02 -06001181 listen_addr = listen_node->loc_addr;
1182 listen_port = listen_node->loc_port;
1183
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001184 /* compare node pair, return node handle if a match */
Tatyana Nikolova56472632014-03-26 17:07:57 -05001185 if (((listen_addr == dst_addr) ||
1186 listen_addr == 0x00000000) &&
1187 (listen_port == dst_port) &&
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301188 (listener_state & listen_node->listener_state)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001189 atomic_inc(&listen_node->ref_count);
1190 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1191 return listen_node;
1192 }
1193 }
1194 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1195
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001196 /* no listener */
1197 return NULL;
1198}
1199
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001200/**
1201 * add_hte_node - add a cm node to the hash table
1202 */
1203static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1204{
1205 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001206 struct list_head *hte;
1207
1208 if (!cm_node || !cm_core)
1209 return -EINVAL;
1210
Faisal Latif6492cdf2008-07-24 20:50:45 -07001211 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301212 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001213
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001214 spin_lock_irqsave(&cm_core->ht_lock, flags);
1215
1216 /* get a handle on the hash table element (list head for this slot) */
1217 hte = &cm_core->connected_nodes;
1218 list_add_tail(&cm_node->list, hte);
1219 atomic_inc(&cm_core->ht_node_cnt);
1220
1221 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1222
1223 return 0;
1224}
1225
1226
1227/**
1228 * mini_cm_dec_refcnt_listen
1229 */
1230static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301231 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001232{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001233 int ret = -EINVAL;
1234 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001235 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001236 struct list_head *list_pos = NULL;
1237 struct list_head *list_temp = NULL;
1238 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -06001239 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001240
1241 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301242 "refcnt=%d\n", listener, free_hanging_nodes,
1243 atomic_read(&listener->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001244 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -06001245 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001246 if (free_hanging_nodes) {
1247 spin_lock_irqsave(&cm_core->ht_lock, flags);
1248 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -06001249 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001250 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301251 list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001252 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -06001253 (!cm_node->accelerated)) {
1254 add_ref_cm_node(cm_node);
1255 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001256 }
1257 }
1258 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1259 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001260
1261 list_for_each_safe(list_pos, list_temp, &reset_list) {
1262 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301263 reset_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001264 {
1265 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -08001266 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001267 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
1268 rem_ref_cm_node(cm_node->cm_core, cm_node);
1269 } else {
1270 if (!loopback) {
1271 cleanup_retrans_entry(cm_node);
1272 err = send_reset(cm_node, NULL);
1273 if (err) {
1274 cm_node->state =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301275 NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001276 WARN_ON(1);
1277 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08001278 old_state = cm_node->state;
1279 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1280 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1281 rem_ref_cm_node(
1282 cm_node->cm_core,
1283 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001284 }
1285 } else {
1286 struct nes_cm_event event;
1287
1288 event.cm_node = loopback;
1289 event.cm_info.rem_addr =
1290 loopback->rem_addr;
1291 event.cm_info.loc_addr =
1292 loopback->loc_addr;
1293 event.cm_info.rem_port =
1294 loopback->rem_port;
1295 event.cm_info.loc_port =
1296 loopback->loc_port;
1297 event.cm_info.cm_id = loopback->cm_id;
Faisal Latif43093b92010-02-12 19:57:04 +00001298 add_ref_cm_node(loopback);
1299 loopback->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001300 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001301 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001302
Faisal Latif9d5ab132009-03-06 15:15:01 -08001303 rem_ref_cm_node(cm_node->cm_core,
1304 cm_node);
1305
1306 }
1307 }
1308 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001309 }
1310
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001311 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1312 if (!atomic_dec_return(&listener->ref_count)) {
1313 list_del(&listener->list);
1314
1315 /* decrement our listen node count */
1316 atomic_dec(&cm_core->listen_node_cnt);
1317
1318 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1319
Tatyana Nikolova56472632014-03-26 17:07:57 -05001320 if (listener->nesvnic) {
1321 nes_manage_apbvt(listener->nesvnic,
Faisal Latif6a0dde82016-02-26 09:18:02 -06001322 listener->loc_port,
Tatyana Nikolova56472632014-03-26 17:07:57 -05001323 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn),
1324 NES_MANAGE_APBVT_DEL);
1325
Tatyana Nikolova56472632014-03-26 17:07:57 -05001326 nes_debug(NES_DBG_NLMSG,
Faisal Latif6a0dde82016-02-26 09:18:02 -06001327 "Delete APBVT loc_port = %04X\n",
1328 listener->loc_port);
Tatyana Nikolova56472632014-03-26 17:07:57 -05001329 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001330
1331 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1332
1333 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001334 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001335 ret = 0;
Faisal Latif6e10d2e2010-02-12 19:55:03 +00001336 atomic_inc(&cm_listens_destroyed);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001337 } else {
1338 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1339 }
1340 if (listener) {
1341 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1342 nes_debug(NES_DBG_CM, "destroying listener (%p)"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301343 " with non-zero pending accepts=%u\n",
1344 listener, atomic_read(&listener->pend_accepts_cnt));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001345 }
1346
1347 return ret;
1348}
1349
1350
1351/**
1352 * mini_cm_del_listen
1353 */
1354static int mini_cm_del_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301355 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001356{
1357 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1358 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1359 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1360}
1361
1362
1363/**
1364 * mini_cm_accelerated
1365 */
1366static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301367 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001368{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001369 cm_node->accelerated = 1;
1370
1371 if (cm_node->accept_pend) {
1372 BUG_ON(!cm_node->listener);
1373 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001374 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001375 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1376 }
1377
Tatyana Nikolova00ad2552012-12-06 19:56:31 +00001378 if (!timer_pending(&cm_core->tcp_timer))
1379 mod_timer(&cm_core->tcp_timer, (jiffies + NES_SHORT_TIME));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001380
1381 return 0;
1382}
1383
1384
1385/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001386 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001387 */
Faisal Latif7a576df2009-12-09 15:54:33 -08001388static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001389{
1390 struct rtable *rt;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001391 struct neighbour *neigh;
Faisal Latif7a576df2009-12-09 15:54:33 -08001392 int rc = arpindex;
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001393 struct net_device *netdev;
Faisal Latif7a576df2009-12-09 15:54:33 -08001394 struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
Tatyana Nikolova165d6822015-07-02 12:49:40 -05001395 __be32 dst_ipaddr = htonl(dst_ip);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001396
Tatyana Nikolova165d6822015-07-02 12:49:40 -05001397 rt = ip_route_output(&init_net, dst_ipaddr, nesvnic->local_ipaddr, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001398 if (IS_ERR(rt)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001399 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301400 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001401 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001402 }
1403
Roland Dreiercf55bb22011-03-24 17:13:20 -07001404 if (netif_is_bond_slave(nesvnic->netdev))
Jiri Pirko7f6e7102013-01-03 22:49:00 +00001405 netdev = netdev_master_upper_dev_get(nesvnic->netdev);
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001406 else
1407 netdev = nesvnic->netdev;
1408
Tatyana Nikolova165d6822015-07-02 12:49:40 -05001409 neigh = dst_neigh_lookup(&rt->dst, &dst_ipaddr);
David Millere55684f2012-01-24 13:16:03 +00001410
David Miller40e2bb52011-12-02 16:52:27 +00001411 rcu_read_lock();
Bob Sharp7191a0a2008-10-03 12:21:19 -07001412 if (neigh) {
1413 if (neigh->nud_state & NUD_VALID) {
1414 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001415 " is %pM, Gateway is 0x%08X \n", dst_ip,
1416 neigh->ha, ntohl(rt->rt_gateway));
Faisal Latif7a576df2009-12-09 15:54:33 -08001417
1418 if (arpindex >= 0) {
Ding Tianhong79adc532014-01-08 10:53:39 +08001419 if (ether_addr_equal(nesadapter->arp_table[arpindex].mac_addr, neigh->ha)) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001420 /* Mac address same as in nes_arp_table */
David Millere55684f2012-01-24 13:16:03 +00001421 goto out;
Faisal Latif7a576df2009-12-09 15:54:33 -08001422 }
1423
1424 nes_manage_arp_cache(nesvnic->netdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301425 nesadapter->arp_table[arpindex].mac_addr,
1426 dst_ip, NES_ARP_DELETE);
Faisal Latif7a576df2009-12-09 15:54:33 -08001427 }
1428
Bob Sharp7191a0a2008-10-03 12:21:19 -07001429 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1430 dst_ip, NES_ARP_ADD);
1431 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1432 NES_ARP_RESOLVE);
David Miller40e2bb52011-12-02 16:52:27 +00001433 } else {
1434 neigh_event_send(neigh, NULL);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001435 }
Bob Sharp7191a0a2008-10-03 12:21:19 -07001436 }
David Millere55684f2012-01-24 13:16:03 +00001437out:
David Miller40e2bb52011-12-02 16:52:27 +00001438 rcu_read_unlock();
David Millere55684f2012-01-24 13:16:03 +00001439
1440 if (neigh)
1441 neigh_release(neigh);
1442
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001443 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001444 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001445}
1446
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001447/**
1448 * make_cm_node - create a new instance of a cm node
1449 */
1450static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301451 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1452 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001453{
1454 struct nes_cm_node *cm_node;
1455 struct timespec ts;
Faisal Latif7a576df2009-12-09 15:54:33 -08001456 int oldarpindex = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001457 int arpindex = 0;
1458 struct nes_device *nesdev;
1459 struct nes_adapter *nesadapter;
1460
1461 /* create an hte and cm_node for this instance */
1462 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1463 if (!cm_node)
1464 return NULL;
1465
1466 /* set our node specific transport info */
Tatyana Nikolova230da362015-04-21 16:28:25 -04001467 if (listener) {
1468 cm_node->loc_addr = listener->loc_addr;
1469 cm_node->loc_port = listener->loc_port;
1470 } else {
1471 cm_node->loc_addr = cm_info->loc_addr;
1472 cm_node->loc_port = cm_info->loc_port;
1473 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001474 cm_node->rem_addr = cm_info->rem_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001475 cm_node->rem_port = cm_info->rem_port;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301476
1477 cm_node->mpa_frame_rev = mpa_version;
1478 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05001479 cm_node->mpav2_ird_ord = 0;
1480 cm_node->ird_size = 0;
1481 cm_node->ord_size = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301482
Harvey Harrison63779432008-10-31 00:56:00 -07001483 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1484 &cm_node->loc_addr, cm_node->loc_port,
1485 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001486 cm_node->listener = listener;
Faisal Latif854ace982015-05-18 15:28:14 -05001487 if (listener)
1488 cm_node->tos = listener->tos;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001489 cm_node->netdev = nesvnic->netdev;
1490 cm_node->cm_id = cm_info->cm_id;
1491 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1492
Faisal Latif6492cdf2008-07-24 20:50:45 -07001493 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301494 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001495
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001496 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001497
1498 cm_node->loopbackpartner = NULL;
1499 atomic_set(&cm_node->ref_count, 1);
1500 /* associate our parent CM core */
1501 cm_node->cm_core = cm_core;
1502 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1503 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1504 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301505 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001506 ts = current_kernel_time();
1507 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1508 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301509 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001510 cm_node->tcp_cntxt.rcv_nxt = 0;
1511 /* get a unique session ID , add thread_id to an upcounter to handle race */
1512 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001513 cm_node->conn_type = cm_info->conn_type;
1514 cm_node->apbvt_set = 0;
1515 cm_node->accept_pend = 0;
1516
1517 cm_node->nesvnic = nesvnic;
1518 /* get some device handles, for arp lookup */
1519 nesdev = nesvnic->nesdev;
1520 nesadapter = nesdev->nesadapter;
1521
1522 cm_node->loopbackpartner = NULL;
Faisal Latif7a576df2009-12-09 15:54:33 -08001523
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001524 /* get the mac addr for the remote node */
Faisal Latif6a0dde82016-02-26 09:18:02 -06001525 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr,
1526 NULL, NES_ARP_RESOLVE);
1527 arpindex = nes_addr_resolve_neigh(nesvnic, cm_node->rem_addr,
1528 oldarpindex);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001529 if (arpindex < 0) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001530 kfree(cm_node);
1531 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001532 }
1533
1534 /* copy the mac addr to node context */
1535 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001536 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1537 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001538
1539 add_hte_node(cm_core, cm_node);
1540 atomic_inc(&cm_nodes_created);
1541
1542 return cm_node;
1543}
1544
1545
1546/**
1547 * add_ref_cm_node - destroy an instance of a cm node
1548 */
1549static int add_ref_cm_node(struct nes_cm_node *cm_node)
1550{
1551 atomic_inc(&cm_node->ref_count);
1552 return 0;
1553}
1554
1555
1556/**
1557 * rem_ref_cm_node - destroy an instance of a cm node
1558 */
1559static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301560 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001561{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001562 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001563 struct nes_qp *nesqp;
1564
1565 if (!cm_node)
1566 return -EINVAL;
1567
1568 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1569 if (atomic_dec_return(&cm_node->ref_count)) {
1570 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1571 return 0;
1572 }
1573 list_del(&cm_node->list);
1574 atomic_dec(&cm_core->ht_node_cnt);
1575 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1576
1577 /* if the node is destroyed before connection was accelerated */
1578 if (!cm_node->accelerated && cm_node->accept_pend) {
1579 BUG_ON(!cm_node->listener);
1580 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1581 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1582 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001583 WARN_ON(cm_node->send_entry);
1584 if (cm_node->recv_entry)
1585 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001586 if (cm_node->listener) {
1587 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1588 } else {
1589 if (cm_node->apbvt_set && cm_node->nesvnic) {
Faisal Latif6a0dde82016-02-26 09:18:02 -06001590 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Tatyana Nikolova56472632014-03-26 17:07:57 -05001591 PCI_FUNC(cm_node->nesvnic->nesdev->pcidev->devfn),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301592 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001593 }
Faisal Latif6a0dde82016-02-26 09:18:02 -06001594 nes_debug(NES_DBG_NLMSG, "Delete APBVT loc_port = %04X\n",
1595 cm_node->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001596 }
1597
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001598 atomic_dec(&cm_core->node_cnt);
1599 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001600 nesqp = cm_node->nesqp;
1601 if (nesqp) {
1602 nesqp->cm_node = NULL;
1603 nes_rem_ref(&nesqp->ibqp);
1604 cm_node->nesqp = NULL;
1605 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001606
Faisal Latif6492cdf2008-07-24 20:50:45 -07001607 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001608 return 0;
1609}
1610
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001611/**
1612 * process_options
1613 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001614static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301615 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001616{
1617 u32 tmp;
1618 u32 offset = 0;
1619 union all_known_options *all_options;
1620 char got_mss_option = 0;
1621
1622 while (offset < optionsize) {
1623 all_options = (union all_known_options *)(optionsloc + offset);
1624 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001625 case OPTION_NUMBER_END:
1626 offset = optionsize;
1627 break;
1628 case OPTION_NUMBER_NONE:
1629 offset += 1;
1630 continue;
1631 case OPTION_NUMBER_MSS:
1632 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301633 "Size: %d\n", __func__,
1634 all_options->as_mss.length, offset, optionsize);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001635 got_mss_option = 1;
1636 if (all_options->as_mss.length != 4) {
1637 return 1;
1638 } else {
1639 tmp = ntohs(all_options->as_mss.mss);
1640 if (tmp > 0 && tmp <
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301641 cm_node->tcp_cntxt.mss)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001642 cm_node->tcp_cntxt.mss = tmp;
1643 }
1644 break;
1645 case OPTION_NUMBER_WINDOW_SCALE:
1646 cm_node->tcp_cntxt.snd_wscale =
1647 all_options->as_windowscale.shiftcount;
1648 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001649 default:
1650 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301651 all_options->as_base.optionnum);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001652 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001653 }
1654 offset += all_options->as_base.length;
1655 }
1656 if ((!got_mss_option) && (syn_packet))
1657 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1658 return 0;
1659}
1660
Faisal Latif6492cdf2008-07-24 20:50:45 -07001661static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001662{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001663 atomic_inc(&cm_accel_dropped_pkts);
1664 dev_kfree_skb_any(skb);
1665}
1666
Faisal Latif9d5ab132009-03-06 15:15:01 -08001667static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001668{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001669 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301670 "refcnt=%d\n", cm_node, cm_node->state,
1671 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001672 switch (cm_node->state) {
1673 case NES_CM_STATE_SYN_RCVD:
1674 case NES_CM_STATE_SYN_SENT:
1675 case NES_CM_STATE_ESTABLISHED:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001676 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001677 cm_node->tcp_cntxt.rcv_nxt++;
1678 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001679 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001680 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001681 break;
Faisal Latifb1190d32009-12-09 15:54:32 -08001682 case NES_CM_STATE_MPAREQ_SENT:
1683 create_event(cm_node, NES_CM_EVENT_ABORTED);
1684 cm_node->tcp_cntxt.rcv_nxt++;
1685 cleanup_retrans_entry(cm_node);
1686 cm_node->state = NES_CM_STATE_CLOSED;
1687 add_ref_cm_node(cm_node);
1688 send_reset(cm_node, NULL);
1689 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001690 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001691 cm_node->tcp_cntxt.rcv_nxt++;
1692 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001693 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001694 send_ack(cm_node, NULL);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001695 /* Wait for ACK as this is simultaneous close..
Faisal Latif9d5ab132009-03-06 15:15:01 -08001696 * After we receive ACK, do not send anything..
1697 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001698 break;
1699 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001700 cm_node->tcp_cntxt.rcv_nxt++;
1701 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001702 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001703 send_ack(cm_node, NULL);
1704 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1705 break;
1706 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001707 cm_node->tcp_cntxt.rcv_nxt++;
1708 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001709 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001710 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001711 break;
1712 case NES_CM_STATE_TSA:
1713 default:
1714 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1715 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001716 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001717 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001718}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001719
Faisal Latif6492cdf2008-07-24 20:50:45 -07001720
1721static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1722 struct tcphdr *tcph)
1723{
1724
1725 int reset = 0; /* whether to send reset in case of err.. */
1726 atomic_inc(&cm_resets_recvd);
1727 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1728 " refcnt=%d\n", cm_node, cm_node->state,
1729 atomic_read(&cm_node->ref_count));
1730 cleanup_retrans_entry(cm_node);
1731 switch (cm_node->state) {
1732 case NES_CM_STATE_SYN_SENT:
1733 case NES_CM_STATE_MPAREQ_SENT:
1734 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1735 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1736 cm_node->listener, cm_node->state);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301737 switch (cm_node->mpa_frame_rev) {
1738 case IETF_MPA_V2:
1739 cm_node->mpa_frame_rev = IETF_MPA_V1;
1740 /* send a syn and goto syn sent state */
1741 cm_node->state = NES_CM_STATE_SYN_SENT;
1742 if (send_syn(cm_node, 0, NULL)) {
1743 active_open_err(cm_node, skb, reset);
1744 }
1745 break;
1746 case IETF_MPA_V1:
1747 default:
1748 active_open_err(cm_node, skb, reset);
1749 break;
1750 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001751 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001752 case NES_CM_STATE_MPAREQ_RCVD:
Dan Carpenter4d8d6382010-09-15 18:32:39 +02001753 atomic_inc(&cm_node->passive_state);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001754 dev_kfree_skb_any(skb);
1755 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001756 case NES_CM_STATE_ESTABLISHED:
1757 case NES_CM_STATE_SYN_RCVD:
1758 case NES_CM_STATE_LISTENING:
1759 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1760 passive_open_err(cm_node, skb, reset);
1761 break;
1762 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001763 active_open_err(cm_node, skb, reset);
1764 break;
1765 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001766 drop_packet(skb);
1767 break;
Faisal Latifdae58722010-08-14 21:04:56 +00001768 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif68237a02009-06-22 22:53:28 -07001769 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001770 case NES_CM_STATE_LAST_ACK:
1771 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001772 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001773 cm_node->state = NES_CM_STATE_CLOSED;
1774 rem_ref_cm_node(cm_node->cm_core, cm_node);
1775 drop_packet(skb);
1776 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001777 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001778 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001779 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001780 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001781}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001782
Faisal Latif9d5ab132009-03-06 15:15:01 -08001783
1784static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001785{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301786 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001787 int datasize = skb->len;
1788 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001789
1790 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301791 u32 res_type;
1792
Faisal Latif9d5ab132009-03-06 15:15:01 -08001793 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1794 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001795 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001796 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001797 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301798 "cm_node=%p listener=%p state=%d\n", __func__,
1799 __LINE__, cm_node, cm_node->listener,
1800 cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001801 active_open_err(cm_node, skb, 1);
1802 } else {
1803 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001804 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001805 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001806 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001807
1808 switch (cm_node->state) {
1809 case NES_CM_STATE_ESTABLISHED:
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301810 if (res_type == NES_MPA_REQUEST_REJECT)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001811 /*BIG problem as we are receiving the MPA.. So should
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301812 * not be REJECT.. This is Passive Open.. We can
1813 * only receive it Reject for Active Open...*/
Faisal Latif9d5ab132009-03-06 15:15:01 -08001814 WARN_ON(1);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001815 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1816 type = NES_CM_EVENT_MPA_REQ;
1817 atomic_set(&cm_node->passive_state,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301818 NES_PASSIVE_STATE_INDICATED);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001819 break;
1820 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001821 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001822 if (res_type == NES_MPA_REQUEST_REJECT) {
1823 type = NES_CM_EVENT_MPA_REJECT;
1824 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1825 } else {
1826 type = NES_CM_EVENT_CONNECTED;
1827 cm_node->state = NES_CM_STATE_TSA;
1828 }
1829
1830 break;
1831 default:
1832 WARN_ON(1);
1833 break;
1834 }
1835 dev_kfree_skb_any(skb);
1836 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001837}
1838
1839static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1840{
1841 switch (cm_node->state) {
1842 case NES_CM_STATE_SYN_SENT:
1843 case NES_CM_STATE_MPAREQ_SENT:
1844 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301845 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1846 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001847 active_open_err(cm_node, skb, 1);
1848 break;
1849 case NES_CM_STATE_ESTABLISHED:
1850 case NES_CM_STATE_SYN_RCVD:
1851 passive_open_err(cm_node, skb, 1);
1852 break;
1853 case NES_CM_STATE_TSA:
1854 default:
1855 drop_packet(skb);
1856 }
1857}
1858
1859static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301860 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001861{
1862 int err;
1863
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301864 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001865 if (err)
1866 active_open_err(cm_node, skb, 1);
1867
1868 return err;
1869}
1870
1871static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301872 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001873{
1874 int err = 0;
1875 u32 seq;
1876 u32 ack_seq;
1877 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1878 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1879 u32 rcv_wnd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301880
Faisal Latif6492cdf2008-07-24 20:50:45 -07001881 seq = ntohl(tcph->seq);
1882 ack_seq = ntohl(tcph->ack_seq);
1883 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1884 if (ack_seq != loc_seq_num)
1885 err = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301886 else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001887 err = 1;
1888 if (err) {
1889 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301890 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1891 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001892 indicate_pkt_err(cm_node, skb);
1893 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301894 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1895 rcv_wnd);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001896 }
1897 return err;
1898}
1899
1900/*
1901 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1902 * is created with a listener or it may comein as rexmitted packet which in
1903 * that case will be just dropped.
1904 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001905static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301906 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001907{
1908 int ret;
1909 u32 inc_sequence;
1910 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001911
1912 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001913 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001914 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001915
Faisal Latif6492cdf2008-07-24 20:50:45 -07001916 switch (cm_node->state) {
1917 case NES_CM_STATE_SYN_SENT:
1918 case NES_CM_STATE_MPAREQ_SENT:
1919 /* Rcvd syn on active open connection*/
1920 active_open_err(cm_node, skb, 1);
1921 break;
1922 case NES_CM_STATE_LISTENING:
1923 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001924 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301925 cm_node->listener->backlog) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001926 nes_debug(NES_DBG_CM, "drop syn due to backlog "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301927 "pressure \n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07001928 cm_backlog_drops++;
1929 passive_open_err(cm_node, skb, 0);
1930 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001931 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001932 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301933 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001934 if (ret) {
1935 passive_open_err(cm_node, skb, 0);
1936 /* drop pkt */
1937 break;
1938 }
1939 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1940 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001941 cm_node->accept_pend = 1;
1942 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1943
Faisal Latif6492cdf2008-07-24 20:50:45 -07001944 cm_node->state = NES_CM_STATE_SYN_RCVD;
1945 send_syn(cm_node, 1, skb);
1946 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001947 case NES_CM_STATE_CLOSED:
1948 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001949 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001950 send_reset(cm_node, skb);
1951 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001952 case NES_CM_STATE_TSA:
1953 case NES_CM_STATE_ESTABLISHED:
1954 case NES_CM_STATE_FIN_WAIT1:
1955 case NES_CM_STATE_FIN_WAIT2:
1956 case NES_CM_STATE_MPAREQ_RCVD:
1957 case NES_CM_STATE_LAST_ACK:
1958 case NES_CM_STATE_CLOSING:
1959 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001960 default:
1961 drop_packet(skb);
1962 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001963 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001964}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001965
Faisal Latif6492cdf2008-07-24 20:50:45 -07001966static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301967 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001968{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001969 int ret;
1970 u32 inc_sequence;
1971 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001972
Faisal Latif6492cdf2008-07-24 20:50:45 -07001973 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001974 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001975 inc_sequence = ntohl(tcph->seq);
1976 switch (cm_node->state) {
1977 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001978 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001979 /* active open */
1980 if (check_syn(cm_node, tcph, skb))
1981 return;
1982 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1983 /* setup options */
1984 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1985 if (ret) {
1986 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301987 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001988 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001989 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001990 cleanup_retrans_entry(cm_node);
1991 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1992 send_mpa_request(cm_node, skb);
1993 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1994 break;
1995 case NES_CM_STATE_MPAREQ_RCVD:
1996 /* passive open, so should not be here */
1997 passive_open_err(cm_node, skb, 1);
1998 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001999 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002000 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
2001 cleanup_retrans_entry(cm_node);
2002 cm_node->state = NES_CM_STATE_CLOSED;
2003 send_reset(cm_node, skb);
2004 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002005 case NES_CM_STATE_CLOSED:
2006 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
2007 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08002008 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002009 send_reset(cm_node, skb);
2010 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002011 case NES_CM_STATE_ESTABLISHED:
2012 case NES_CM_STATE_FIN_WAIT1:
2013 case NES_CM_STATE_FIN_WAIT2:
2014 case NES_CM_STATE_LAST_ACK:
2015 case NES_CM_STATE_TSA:
2016 case NES_CM_STATE_CLOSING:
2017 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002018 case NES_CM_STATE_MPAREQ_SENT:
2019 default:
2020 drop_packet(skb);
2021 break;
2022 }
2023}
2024
Faisal Latif109d67e2009-04-27 13:41:06 -07002025static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302026 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002027{
2028 int datasize = 0;
2029 u32 inc_sequence;
Faisal Latif109d67e2009-04-27 13:41:06 -07002030 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06002031 int optionsize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302032
Faisal Latifabb77252008-11-21 20:50:52 -06002033 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06002034
Faisal Latif6492cdf2008-07-24 20:50:45 -07002035 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07002036 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002037
2038 skb_pull(skb, tcph->doff << 2);
2039 inc_sequence = ntohl(tcph->seq);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002040 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002041 switch (cm_node->state) {
2042 case NES_CM_STATE_SYN_RCVD:
2043 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07002044 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002045 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
2046 if (ret)
2047 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002048 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2049 cm_node->state = NES_CM_STATE_ESTABLISHED;
2050 if (datasize) {
2051 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002052 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302053 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002054 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302055 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002056 break;
2057 case NES_CM_STATE_ESTABLISHED:
2058 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08002059 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002060 if (datasize) {
2061 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002062 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302063 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002064 drop_packet(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302065 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002066 break;
2067 case NES_CM_STATE_MPAREQ_SENT:
2068 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2069 if (datasize) {
2070 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002071 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302072 } else { /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002073 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302074 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002075 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002076 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002077 cleanup_retrans_entry(cm_node);
2078 cm_node->state = NES_CM_STATE_CLOSED;
2079 send_reset(cm_node, skb);
2080 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002081 case NES_CM_STATE_CLOSED:
2082 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08002083 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002084 send_reset(cm_node, skb);
2085 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002086 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002087 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002088 cleanup_retrans_entry(cm_node);
2089 cm_node->state = NES_CM_STATE_CLOSED;
2090 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002091 rem_ref_cm_node(cm_node->cm_core, cm_node);
2092 drop_packet(skb);
2093 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002094 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002095 cleanup_retrans_entry(cm_node);
2096 drop_packet(skb);
2097 cm_node->state = NES_CM_STATE_FIN_WAIT2;
2098 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002099 case NES_CM_STATE_SYN_SENT:
2100 case NES_CM_STATE_FIN_WAIT2:
2101 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002102 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002103 case NES_CM_STATE_UNKNOWN:
2104 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002105 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002106 drop_packet(skb);
2107 break;
2108 }
Faisal Latif109d67e2009-04-27 13:41:06 -07002109 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002110}
2111
2112
2113
2114static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302115 struct sk_buff *skb, int optionsize, int passive)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002116{
2117 u8 *optionsloc = (u8 *)&tcph[1];
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302118
Faisal Latif6492cdf2008-07-24 20:50:45 -07002119 if (optionsize) {
2120 if (process_options(cm_node, optionsloc, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302121 (u32)tcph->syn)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002122 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302123 __func__, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002124 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06002125 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002126 else
Faisal Latifabb77252008-11-21 20:50:52 -06002127 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002128 return 1;
2129 }
2130 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002131
2132 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302133 cm_node->tcp_cntxt.snd_wscale;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002134
Faisal Latif6492cdf2008-07-24 20:50:45 -07002135 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002136 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002137 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002138}
2139
Faisal Latif6492cdf2008-07-24 20:50:45 -07002140/*
2141 * active_open_err() will send reset() if flag set..
2142 * It will also send ABORT event.
2143 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002144static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302145 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002146{
2147 cleanup_retrans_entry(cm_node);
2148 if (reset) {
2149 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302150 "state=%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002151 add_ref_cm_node(cm_node);
2152 send_reset(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302153 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002154 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302155 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002156
2157 cm_node->state = NES_CM_STATE_CLOSED;
2158 create_event(cm_node, NES_CM_EVENT_ABORTED);
2159}
2160
2161/*
2162 * passive_open_err() will either do a reset() or will free up the skb and
2163 * remove the cm_node.
2164 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002165static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302166 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002167{
2168 cleanup_retrans_entry(cm_node);
2169 cm_node->state = NES_CM_STATE_CLOSED;
2170 if (reset) {
2171 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302172 "cm_node=%p state =%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002173 send_reset(cm_node, skb);
2174 } else {
2175 dev_kfree_skb_any(skb);
2176 rem_ref_cm_node(cm_node->cm_core, cm_node);
2177 }
2178}
2179
2180/*
2181 * free_retrans_entry() routines assumes that the retrans_list_lock has
2182 * been acquired before calling.
2183 */
2184static void free_retrans_entry(struct nes_cm_node *cm_node)
2185{
2186 struct nes_timer_entry *send_entry;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302187
Faisal Latif6492cdf2008-07-24 20:50:45 -07002188 send_entry = cm_node->send_entry;
2189 if (send_entry) {
2190 cm_node->send_entry = NULL;
2191 dev_kfree_skb_any(send_entry->skb);
2192 kfree(send_entry);
2193 rem_ref_cm_node(cm_node->cm_core, cm_node);
2194 }
2195}
2196
2197static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
2198{
2199 unsigned long flags;
2200
2201 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
2202 free_retrans_entry(cm_node);
2203 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
2204}
2205
2206/**
2207 * process_packet
2208 * Returns skb if to be freed, else it will return NULL if already used..
2209 */
2210static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302211 struct nes_cm_core *cm_core)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002212{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302213 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002214 struct tcphdr *tcph = tcp_hdr(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302215 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07002216 int ret = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302217
Faisal Latif6492cdf2008-07-24 20:50:45 -07002218 skb_pull(skb, ip_hdr(skb)->ihl << 2);
2219
2220 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302221 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
2222 tcph->ack, tcph->rst, tcph->fin);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002223
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302224 if (tcph->rst) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002225 pkt_type = NES_PKT_TYPE_RST;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302226 } else if (tcph->syn) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002227 pkt_type = NES_PKT_TYPE_SYN;
2228 if (tcph->ack)
2229 pkt_type = NES_PKT_TYPE_SYNACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302230 } else if (tcph->ack) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002231 pkt_type = NES_PKT_TYPE_ACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302232 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002233 if (tcph->fin)
2234 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002235
2236 switch (pkt_type) {
2237 case NES_PKT_TYPE_SYN:
2238 handle_syn_pkt(cm_node, skb, tcph);
2239 break;
2240 case NES_PKT_TYPE_SYNACK:
2241 handle_synack_pkt(cm_node, skb, tcph);
2242 break;
2243 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002244 ret = handle_ack_pkt(cm_node, skb, tcph);
2245 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08002246 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002247 break;
2248 case NES_PKT_TYPE_RST:
2249 handle_rst_pkt(cm_node, skb, tcph);
2250 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002251 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002252 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08002253 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07002254 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002255 break;
2256 }
2257}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002258
2259/**
2260 * mini_cm_listen - create a listen node with params
2261 */
2262static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova56472632014-03-26 17:07:57 -05002263 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002264{
2265 struct nes_cm_listener *listener;
2266 unsigned long flags;
2267
2268 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302269 cm_info->loc_addr, cm_info->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002270
2271 /* cannot have multiple matching listeners */
Tatyana Nikolova56472632014-03-26 17:07:57 -05002272 listener = find_listener(cm_core, cm_info->loc_addr, cm_info->loc_port,
Faisal Latif6a0dde82016-02-26 09:18:02 -06002273 NES_CM_LISTENER_EITHER_STATE);
Tatyana Nikolova56472632014-03-26 17:07:57 -05002274
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002275 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
2276 /* find automatically incs ref count ??? */
2277 atomic_dec(&listener->ref_count);
2278 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
2279 return NULL;
2280 }
2281
2282 if (!listener) {
2283 /* create a CM listen node (1/2 node to compare incoming traffic to) */
2284 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
Leon Romanovsky2e658352016-11-03 16:44:19 +02002285 if (!listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002286 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002287
Tatyana Nikolova56472632014-03-26 17:07:57 -05002288 listener->loc_addr = cm_info->loc_addr;
2289 listener->loc_port = cm_info->loc_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002290 listener->reused_node = 0;
2291
2292 atomic_set(&listener->ref_count, 1);
2293 }
2294 /* pasive case */
2295 /* find already inc'ed the ref count */
2296 else {
2297 listener->reused_node = 1;
2298 }
2299
2300 listener->cm_id = cm_info->cm_id;
2301 atomic_set(&listener->pend_accepts_cnt, 0);
2302 listener->cm_core = cm_core;
2303 listener->nesvnic = nesvnic;
2304 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002305
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002306 listener->conn_type = cm_info->conn_type;
2307 listener->backlog = cm_info->backlog;
2308 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
2309
2310 if (!listener->reused_node) {
2311 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2312 list_add(&listener->list, &cm_core->listen_list.list);
2313 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2314 atomic_inc(&cm_core->listen_node_cnt);
2315 }
2316
2317 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302318 " listener = %p, backlog = %d, cm_id = %p.\n",
2319 cm_info->loc_addr, cm_info->loc_port,
2320 listener, listener->backlog, listener->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002321
2322 return listener;
2323}
2324
2325
2326/**
2327 * mini_cm_connect - make a connection node with params
2328 */
Faisal Latif54c86a82008-09-30 14:47:27 -07002329static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302330 struct nes_vnic *nesvnic, u16 private_data_len,
2331 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002332{
2333 int ret = 0;
2334 struct nes_cm_node *cm_node;
2335 struct nes_cm_listener *loopbackremotelistener;
2336 struct nes_cm_node *loopbackremotenode;
2337 struct nes_cm_info loopback_cm_info;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302338 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002339
2340 /* create a CM connection node */
2341 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2342 if (!cm_node)
2343 return NULL;
2344
Glenn Streiff7495ab62008-04-29 13:46:54 -07002345 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002346 cm_node->tcp_cntxt.client = 1;
2347 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2348
2349 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002350 loopbackremotelistener = find_listener(cm_core,
Faisal Latif6a0dde82016-02-26 09:18:02 -06002351 cm_node->loc_addr, cm_node->rem_port,
2352 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002353 if (loopbackremotelistener == NULL) {
2354 create_event(cm_node, NES_CM_EVENT_ABORTED);
2355 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002356 loopback_cm_info = *cm_info;
2357 loopback_cm_info.loc_port = cm_info->rem_port;
2358 loopback_cm_info.rem_port = cm_info->loc_port;
Faisal Latif6a0dde82016-02-26 09:18:02 -06002359 loopback_cm_info.loc_port =
2360 cm_info->rem_port;
2361 loopback_cm_info.rem_port =
2362 cm_info->loc_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002363 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002364 loopbackremotenode = make_cm_node(cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302365 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002366 if (!loopbackremotenode) {
2367 rem_ref_cm_node(cm_node->cm_core, cm_node);
2368 return NULL;
2369 }
2370 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002371 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002372 loopbackremotenode->tcp_cntxt.rcv_wscale =
2373 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002374 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002375 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302376 private_data_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002377 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002378
Faisal Latif6492cdf2008-07-24 20:50:45 -07002379 /* we are done handling this state. */
2380 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002381 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002382 cm_node->tcp_cntxt.rcv_nxt =
2383 loopbackremotenode->tcp_cntxt.loc_seq_num;
2384 loopbackremotenode->tcp_cntxt.rcv_nxt =
2385 cm_node->tcp_cntxt.loc_seq_num;
2386 cm_node->tcp_cntxt.max_snd_wnd =
2387 loopbackremotenode->tcp_cntxt.rcv_wnd;
2388 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2389 cm_node->tcp_cntxt.rcv_wnd;
2390 cm_node->tcp_cntxt.snd_wnd =
2391 loopbackremotenode->tcp_cntxt.rcv_wnd;
2392 loopbackremotenode->tcp_cntxt.snd_wnd =
2393 cm_node->tcp_cntxt.rcv_wnd;
2394 cm_node->tcp_cntxt.snd_wscale =
2395 loopbackremotenode->tcp_cntxt.rcv_wscale;
2396 loopbackremotenode->tcp_cntxt.snd_wscale =
2397 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002398 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002399 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2400 }
2401 return cm_node;
2402 }
2403
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302404 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
2405 cm_node->mpa_frame_size = private_data_len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002406
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302407 memcpy(start_buff, private_data, private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002408
2409 /* send a syn and goto syn sent state */
2410 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002411 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002412
Faisal Latif6492cdf2008-07-24 20:50:45 -07002413 if (ret) {
2414 /* error in sending the syn free up the cm_node struct */
2415 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302416 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2417 cm_node->rem_addr, cm_node->rem_port, cm_node,
2418 cm_node->cm_id);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002419 rem_ref_cm_node(cm_node->cm_core, cm_node);
2420 cm_node = NULL;
2421 }
2422
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302423 if (cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002424 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302425 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2426 cm_node->rem_addr, cm_node->rem_port, cm_node,
2427 cm_node->cm_id);
2428 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002429
2430 return cm_node;
2431}
2432
2433
2434/**
2435 * mini_cm_accept - accept a connection
2436 * This function is never called
2437 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302438static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002439{
2440 return 0;
2441}
2442
2443
2444/**
2445 * mini_cm_reject - reject and teardown a connection
2446 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302447static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002448{
2449 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002450 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002451 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002452 struct nes_cm_event event;
2453 struct iw_cm_id *cm_id = cm_node->cm_id;
2454 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002455
Faisal Latif6492cdf2008-07-24 20:50:45 -07002456 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302457 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002458
Faisal Latif6492cdf2008-07-24 20:50:45 -07002459 if (cm_node->tcp_cntxt.client)
2460 return ret;
2461 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002462
Faisal Latif9d5ab132009-03-06 15:15:01 -08002463 if (!loopback) {
2464 passive_state = atomic_add_return(1, &cm_node->passive_state);
2465 if (passive_state == NES_SEND_RESET_EVENT) {
2466 cm_node->state = NES_CM_STATE_CLOSED;
2467 rem_ref_cm_node(cm_core, cm_node);
2468 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002469 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2470 rem_ref_cm_node(cm_core, cm_node);
2471 } else {
2472 ret = send_mpa_reject(cm_node);
2473 if (ret) {
2474 cm_node->state = NES_CM_STATE_CLOSED;
2475 err = send_reset(cm_node, NULL);
2476 if (err)
2477 WARN_ON(1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302478 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002479 cm_id->add_ref(cm_id);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302480 }
Faisal Latifc5a7d482009-12-09 15:54:08 -08002481 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002482 }
2483 } else {
2484 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002485 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2486 rem_ref_cm_node(cm_core, cm_node);
2487 rem_ref_cm_node(cm_core, loopback);
2488 } else {
2489 event.cm_node = loopback;
2490 event.cm_info.rem_addr = loopback->rem_addr;
2491 event.cm_info.loc_addr = loopback->loc_addr;
2492 event.cm_info.rem_port = loopback->rem_port;
2493 event.cm_info.loc_port = loopback->loc_port;
2494 event.cm_info.cm_id = loopback->cm_id;
2495 cm_event_mpa_reject(&event);
2496 rem_ref_cm_node(cm_core, cm_node);
2497 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002498
Faisal Latifc5a7d482009-12-09 15:54:08 -08002499 cm_id = loopback->cm_id;
2500 rem_ref_cm_node(cm_core, loopback);
2501 cm_id->rem_ref(cm_id);
2502 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002503 }
2504
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002505 return ret;
2506}
2507
2508
2509/**
2510 * mini_cm_close
2511 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002512static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002513{
2514 int ret = 0;
2515
2516 if (!cm_core || !cm_node)
2517 return -EINVAL;
2518
2519 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002520 case NES_CM_STATE_SYN_RCVD:
2521 case NES_CM_STATE_SYN_SENT:
2522 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2523 case NES_CM_STATE_ESTABLISHED:
2524 case NES_CM_STATE_ACCEPTING:
2525 case NES_CM_STATE_MPAREQ_SENT:
2526 case NES_CM_STATE_MPAREQ_RCVD:
2527 cleanup_retrans_entry(cm_node);
2528 send_reset(cm_node, NULL);
2529 break;
2530 case NES_CM_STATE_CLOSE_WAIT:
2531 cm_node->state = NES_CM_STATE_LAST_ACK;
2532 send_fin(cm_node, NULL);
2533 break;
2534 case NES_CM_STATE_FIN_WAIT1:
2535 case NES_CM_STATE_FIN_WAIT2:
2536 case NES_CM_STATE_LAST_ACK:
2537 case NES_CM_STATE_TIME_WAIT:
2538 case NES_CM_STATE_CLOSING:
2539 ret = -1;
2540 break;
2541 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002542 cleanup_retrans_entry(cm_node);
2543 send_reset(cm_node, NULL);
2544 break;
2545 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002546 case NES_CM_STATE_UNKNOWN:
2547 case NES_CM_STATE_INITED:
2548 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002549 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002550 ret = rem_ref_cm_node(cm_core, cm_node);
2551 break;
2552 case NES_CM_STATE_TSA:
2553 if (cm_node->send_entry)
2554 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302555 "send_entry=%p\n", cm_node->send_entry);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002556 ret = rem_ref_cm_node(cm_core, cm_node);
2557 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002558 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002559 return ret;
2560}
2561
2562
2563/**
2564 * recv_pkt - recv an ETHERNET packet, and process it through CM
2565 * node state machine
2566 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002567static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302568 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002569{
2570 struct nes_cm_node *cm_node = NULL;
2571 struct nes_cm_listener *listener = NULL;
2572 struct iphdr *iph;
2573 struct tcphdr *tcph;
2574 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002575 int skb_handled = 1;
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002576 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002577
Faisal Latif6492cdf2008-07-24 20:50:45 -07002578 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002579 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302580 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002581 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002582
2583 iph = (struct iphdr *)skb->data;
2584 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002585
2586 nfo.loc_addr = ntohl(iph->daddr);
2587 nfo.loc_port = ntohs(tcph->dest);
2588 nfo.rem_addr = ntohl(iph->saddr);
2589 nfo.rem_port = ntohs(tcph->source);
2590
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002591 tmp_daddr = cpu_to_be32(iph->daddr);
2592 tmp_saddr = cpu_to_be32(iph->saddr);
2593
Harvey Harrison63779432008-10-31 00:56:00 -07002594 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002595 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002596
Faisal Latif6492cdf2008-07-24 20:50:45 -07002597 do {
2598 cm_node = find_node(cm_core,
Faisal Latif6a0dde82016-02-26 09:18:02 -06002599 nfo.rem_port, nfo.rem_addr,
2600 nfo.loc_port, nfo.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002601
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002602 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002603 /* Only type of packet accepted are for */
2604 /* the PASSIVE open (syn only) */
2605 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002606 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002607 break;
2608 }
Faisal Latif6a0dde82016-02-26 09:18:02 -06002609 listener = find_listener(cm_core, nfo.loc_addr,
2610 nfo.loc_port,
2611 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002612 if (!listener) {
2613 nfo.cm_id = NULL;
2614 nfo.conn_type = 0;
2615 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2616 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002617 break;
2618 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002619 nfo.cm_id = listener->cm_id;
2620 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002621 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302622 listener);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002623 if (!cm_node) {
2624 nes_debug(NES_DBG_CM, "Unable to allocate "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302625 "node\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07002626 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002627 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002628 dev_kfree_skb_any(skb);
2629 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002630 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002631 if (!tcph->rst && !tcph->fin) {
2632 cm_node->state = NES_CM_STATE_LISTENING;
2633 } else {
2634 cm_packets_dropped++;
2635 rem_ref_cm_node(cm_core, cm_node);
2636 dev_kfree_skb_any(skb);
2637 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002638 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002639 add_ref_cm_node(cm_node);
2640 } else if (cm_node->state == NES_CM_STATE_TSA) {
Faisal Latif0f0bee82011-09-25 20:34:00 -05002641 if (cm_node->nesqp->pau_mode)
2642 nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
2643 else {
2644 rem_ref_cm_node(cm_core, cm_node);
2645 atomic_inc(&cm_accel_dropped_pkts);
2646 dev_kfree_skb_any(skb);
2647 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002648 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002649 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002650 skb_reset_network_header(skb);
2651 skb_set_transport_header(skb, sizeof(*tcph));
2652 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002653 process_packet(cm_node, skb, cm_core);
2654 rem_ref_cm_node(cm_core, cm_node);
2655 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002656 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002657}
2658
2659
2660/**
2661 * nes_cm_alloc_core - allocate a top level instance of a cm core
2662 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002663static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002664{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002665 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002666
2667 /* setup the CM core */
2668 /* alloc top level core control structure */
2669 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2670 if (!cm_core)
2671 return NULL;
2672
2673 INIT_LIST_HEAD(&cm_core->connected_nodes);
2674 init_timer(&cm_core->tcp_timer);
2675 cm_core->tcp_timer.function = nes_cm_timer_tick;
2676
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302677 cm_core->mtu = NES_CM_DEFAULT_MTU;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002678 cm_core->state = NES_CM_STATE_INITED;
2679 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2680
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002681 atomic_set(&cm_core->events_posted, 0);
2682
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002683 cm_core->api = &nes_cm_api;
2684
2685 spin_lock_init(&cm_core->ht_lock);
2686 spin_lock_init(&cm_core->listen_list_lock);
2687
2688 INIT_LIST_HEAD(&cm_core->listen_list.list);
2689
2690 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2691
2692 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
Bhaktipriya Shridhar9e5df312016-08-15 23:44:03 +05302693 cm_core->event_wq = alloc_ordered_workqueue("nesewq", 0);
Insu Yuna82268b2016-02-17 13:06:33 -05002694 if (!cm_core->event_wq)
2695 goto out_free_cmcore;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002696 cm_core->post_event = nes_cm_post_event;
2697 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
Bhaktipriya Shridhar9e5df312016-08-15 23:44:03 +05302698 cm_core->disconn_wq = alloc_ordered_workqueue("nesdwq", 0);
Insu Yuna82268b2016-02-17 13:06:33 -05002699 if (!cm_core->disconn_wq)
2700 goto out_free_wq;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002701
2702 print_core(cm_core);
2703 return cm_core;
Insu Yuna82268b2016-02-17 13:06:33 -05002704
2705out_free_wq:
2706 destroy_workqueue(cm_core->event_wq);
2707out_free_cmcore:
2708 kfree(cm_core);
2709 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002710}
2711
2712
2713/**
2714 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2715 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002716static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002717{
2718 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2719
2720 if (!cm_core)
2721 return -EINVAL;
2722
2723 barrier();
2724
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302725 if (timer_pending(&cm_core->tcp_timer))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002726 del_timer(&cm_core->tcp_timer);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002727
2728 destroy_workqueue(cm_core->event_wq);
2729 destroy_workqueue(cm_core->disconn_wq);
2730 nes_debug(NES_DBG_CM, "\n");
2731 kfree(cm_core);
2732
2733 return 0;
2734}
2735
2736
2737/**
2738 * mini_cm_get
2739 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002740static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002741{
2742 return cm_core->state;
2743}
2744
2745
2746/**
2747 * mini_cm_set
2748 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002749static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002750{
2751 int ret = 0;
2752
2753 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002754 case NES_CM_SET_PKT_SIZE:
2755 cm_core->mtu = value;
2756 break;
2757 case NES_CM_SET_FREE_PKT_Q_SIZE:
2758 cm_core->free_tx_pkt_max = value;
2759 break;
2760 default:
2761 /* unknown set option */
2762 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002763 }
2764
2765 return ret;
2766}
2767
2768
2769/**
2770 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2771 * successfully exchanged when this is called
2772 */
2773static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2774{
2775 int ret = 0;
2776
2777 if (!nesqp)
2778 return -EINVAL;
2779
2780 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302781 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2782 NES_QPCONTEXT_MISC_DROS);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002783
2784 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2785 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2786
2787 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2788
Faisal Latif854ace982015-05-18 15:28:14 -05002789 nesqp->nesqp_context->misc2 |= cpu_to_le32(
2790 cm_node->tos << NES_QPCONTEXT_MISC2_TOS_SHIFT);
2791
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002792 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2793
2794 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302795 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002796
2797 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302798 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2799 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002800
2801 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302802 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2803 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002804
2805 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2806 nesqp->nesqp_context->ts_recent = 0;
2807 nesqp->nesqp_context->ts_age = 0;
2808 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2809 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2810 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2811 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302812 cm_node->tcp_cntxt.rcv_wscale);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002813 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2814 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2815 nesqp->nesqp_context->srtt = 0;
2816 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2817 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302818 nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002819 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2820 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2821 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2822
2823 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302824 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2825 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2826 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2827 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2828 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2829 le32_to_cpu(nesqp->nesqp_context->misc));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002830 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2831 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2832 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2833
2834 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2835 cm_node->state = NES_CM_STATE_TSA;
2836
2837 return ret;
2838}
2839
2840
2841/**
2842 * nes_cm_disconn
2843 */
2844int nes_cm_disconn(struct nes_qp *nesqp)
2845{
Don Wood873fcdd2009-09-05 20:36:37 -07002846 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002847
Don Wood873fcdd2009-09-05 20:36:37 -07002848 work = kzalloc(sizeof *work, GFP_ATOMIC);
2849 if (!work)
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302850 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002851
Don Wood873fcdd2009-09-05 20:36:37 -07002852 nes_add_ref(&nesqp->ibqp);
2853 work->nesqp = nesqp;
2854 INIT_WORK(&work->work, nes_disconnect_worker);
2855 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002856 return 0;
2857}
2858
2859
2860/**
2861 * nes_disconnect_worker
2862 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002863static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002864{
Don Wood873fcdd2009-09-05 20:36:37 -07002865 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2866 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002867
Don Wood873fcdd2009-09-05 20:36:37 -07002868 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002869 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302870 nesqp->last_aeq, nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002871 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002872 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002873}
2874
2875
2876/**
2877 * nes_cm_disconn_true
2878 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002879static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002880{
2881 unsigned long flags;
2882 int ret = 0;
2883 struct iw_cm_id *cm_id;
2884 struct iw_cm_event cm_event;
2885 struct nes_vnic *nesvnic;
2886 u16 last_ae;
2887 u8 original_hw_tcp_state;
2888 u8 original_ibqp_state;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002889 int disconn_status = 0;
Don Woodb29a4fc2009-09-05 20:36:39 -07002890 int issue_disconn = 0;
2891 int issue_close = 0;
2892 int issue_flush = 0;
2893 u32 flush_q = NES_CQP_FLUSH_RQ;
2894 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002895
2896 if (!nesqp) {
2897 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2898 return -1;
2899 }
2900
2901 spin_lock_irqsave(&nesqp->lock, flags);
2902 cm_id = nesqp->cm_id;
2903 /* make sure we havent already closed this connection */
2904 if (!cm_id) {
2905 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302906 nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002907 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002908 return -1;
2909 }
2910
2911 nesvnic = to_nesvnic(nesqp->ibqp.device);
2912 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2913
2914 original_hw_tcp_state = nesqp->hw_tcp_state;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302915 original_ibqp_state = nesqp->ibqp_state;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002916 last_ae = nesqp->last_aeq;
2917
Don Woodb29a4fc2009-09-05 20:36:39 -07002918 if (nesqp->term_flags) {
2919 issue_disconn = 1;
2920 issue_close = 1;
2921 nesqp->cm_id = NULL;
Tatyana Nikolova196f40c2011-12-05 11:39:12 -06002922 del_timer(&nesqp->terminate_timer);
Don Woodb29a4fc2009-09-05 20:36:39 -07002923 if (nesqp->flush_issued == 0) {
2924 nesqp->flush_issued = 1;
2925 issue_flush = 1;
2926 }
2927 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2928 ((original_ibqp_state == IB_QPS_RTS) &&
2929 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2930 issue_disconn = 1;
2931 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002932 disconn_status = -ECONNRESET;
Don Woodb29a4fc2009-09-05 20:36:39 -07002933 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002934
Don Woodb29a4fc2009-09-05 20:36:39 -07002935 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2936 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2937 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2938 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2939 issue_close = 1;
2940 nesqp->cm_id = NULL;
2941 if (nesqp->flush_issued == 0) {
2942 nesqp->flush_issued = 1;
2943 issue_flush = 1;
2944 }
2945 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002946
Don Woodb29a4fc2009-09-05 20:36:39 -07002947 spin_unlock_irqrestore(&nesqp->lock, flags);
2948
2949 if ((issue_flush) && (nesqp->destroyed == 0)) {
2950 /* Flush the queue(s) */
2951 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2952 flush_q |= NES_CQP_FLUSH_SQ;
2953 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2954
2955 if (nesqp->term_flags) {
2956 ibevent.device = nesqp->ibqp.device;
2957 ibevent.event = nesqp->terminate_eventtype;
2958 ibevent.element.qp = &nesqp->ibqp;
Tatyana Nikolova784d1352012-05-11 14:29:32 -05002959 if (nesqp->ibqp.event_handler)
2960 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
Don Woodb29a4fc2009-09-05 20:36:39 -07002961 }
2962 }
2963
2964 if ((cm_id) && (cm_id->event_handler)) {
2965 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002966 atomic_inc(&cm_disconnects);
2967 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002968 cm_event.status = disconn_status;
Faisal Latif6a0dde82016-02-26 09:18:02 -06002969 cm_event.local_addr = cm_id->m_local_addr;
2970 cm_event.remote_addr = cm_id->m_remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002971 cm_event.private_data = NULL;
2972 cm_event.private_data_len = 0;
2973
Faisal Latif6492cdf2008-07-24 20:50:45 -07002974 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302975 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2976 "cm_id = %p, refcount = %u.\n",
2977 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2978 nesqp->hwqp.sq_tail, cm_id,
2979 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002980
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002981 ret = cm_id->event_handler(cm_id, &cm_event);
2982 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002983 nes_debug(NES_DBG_CM, "OFA CM event_handler "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302984 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002985 }
2986
Don Woodb29a4fc2009-09-05 20:36:39 -07002987 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002988 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002989 nes_disconnect(nesqp, 1);
2990
2991 cm_id->provider_data = nesqp;
2992 /* Send up the close complete event */
2993 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002994 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002995 cm_event.provider_data = cm_id->provider_data;
Faisal Latif6a0dde82016-02-26 09:18:02 -06002996 cm_event.local_addr = cm_id->m_local_addr;
2997 cm_event.remote_addr = cm_id->m_remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002998 cm_event.private_data = NULL;
2999 cm_event.private_data_len = 0;
3000
3001 ret = cm_id->event_handler(cm_id, &cm_event);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303002 if (ret)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003003 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003004
3005 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003006 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003007 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003008
3009 return 0;
3010}
3011
3012
3013/**
3014 * nes_disconnect
3015 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003016static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003017{
3018 int ret = 0;
3019 struct nes_vnic *nesvnic;
3020 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003021 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003022
3023 nesvnic = to_nesvnic(nesqp->ibqp.device);
3024 if (!nesvnic)
3025 return -EINVAL;
3026
3027 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003028 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003029
3030 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00003031 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003032
3033 if (nesqp->active_conn) {
3034
3035 /* indicate this connection is NOT active */
3036 nesqp->active_conn = 0;
3037 } else {
3038 /* Need to free the Last Streaming Mode Message */
3039 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07003040 if (nesqp->lsmm_mr)
3041 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003042 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303043 nesqp->private_data_len + nesqp->ietf_frame_size,
3044 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003045 }
3046 }
3047
3048 /* close the CM node down if it is still active */
3049 if (nesqp->cm_node) {
3050 nes_debug(NES_DBG_CM, "Call close API\n");
3051
3052 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003053 }
3054
3055 return ret;
3056}
3057
3058
3059/**
3060 * nes_accept
3061 */
3062int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3063{
3064 u64 u64temp;
3065 struct ib_qp *ibqp;
3066 struct nes_qp *nesqp;
3067 struct nes_vnic *nesvnic;
3068 struct nes_device *nesdev;
3069 struct nes_cm_node *cm_node;
3070 struct nes_adapter *adapter;
3071 struct ib_qp_attr attr;
3072 struct iw_cm_event cm_event;
3073 struct nes_hw_qp_wqe *wqe;
3074 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003075 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003076 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003077 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003078 struct nes_ib_device *nesibdev;
3079 struct ib_mr *ibmr = NULL;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003080 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07003081 u64 tagged_offset;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303082 u8 mpa_frame_offset = 0;
3083 struct ietf_mpa_v2 *mpa_v2_frame;
3084 u8 start_addr = 0;
3085 u8 *start_ptr = &start_addr;
3086 u8 **start_buff = &start_ptr;
3087 u16 buff_len = 0;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003088 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
3089 struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003090
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003091 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3092 if (!ibqp)
3093 return -EINVAL;
3094
3095 /* get all our handles */
3096 nesqp = to_nesqp(ibqp);
3097 nesvnic = to_nesvnic(nesqp->ibqp.device);
3098 nesdev = nesvnic->nesdev;
3099 adapter = nesdev->nesadapter;
3100
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003101 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003102 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
3103 "%s\n", cm_node, nesvnic, nesvnic->netdev,
3104 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003105
Faisal Latifc5a7d482009-12-09 15:54:08 -08003106 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
3107 if (cm_node->loopbackpartner)
3108 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
3109 rem_ref_cm_node(cm_node->cm_core, cm_node);
3110 return -EINVAL;
3111 }
3112
Faisal Latifdae58722010-08-14 21:04:56 +00003113 passive_state = atomic_add_return(1, &cm_node->passive_state);
3114 if (passive_state == NES_SEND_RESET_EVENT) {
3115 rem_ref_cm_node(cm_node->cm_core, cm_node);
3116 return -ECONNRESET;
3117 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003118 /* associate the node with the QP */
3119 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003120 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003121
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003122
Faisal Latif6492cdf2008-07-24 20:50:45 -07003123 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
3124 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003125 atomic_inc(&cm_accepts);
3126
3127 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00003128 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003129
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303130 nesqp->ietf_frame_size = sizeof(struct ietf_mpa_v2);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003131 /* allocate the ietf frame and space for private data */
3132 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303133 nesqp->ietf_frame_size + conn_param->private_data_len,
3134 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003135
Faisal Latif6492cdf2008-07-24 20:50:45 -07003136 if (!nesqp->ietf_frame) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303137 nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07003138 return -ENOMEM;
3139 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303140 mpa_v2_frame = (struct ietf_mpa_v2 *)nesqp->ietf_frame;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003141
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303142 if (cm_node->mpa_frame_rev == IETF_MPA_V1)
3143 mpa_frame_offset = 4;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003144
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003145 if (cm_node->mpa_frame_rev == IETF_MPA_V1 ||
3146 cm_node->mpav2_ird_ord == IETF_NO_IRD_ORD) {
3147 record_ird_ord(cm_node, (u16)conn_param->ird, (u16)conn_param->ord);
3148 }
3149
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303150 memcpy(mpa_v2_frame->priv_data, conn_param->private_data,
3151 conn_param->private_data_len);
3152
3153 cm_build_mpa_frame(cm_node, start_buff, &buff_len, nesqp->ietf_frame, MPA_KEY_REPLY);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003154 nesqp->private_data_len = conn_param->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003155
Faisal Latif6492cdf2008-07-24 20:50:45 -07003156 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
3157 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003158
Steve Wise24d44a32013-07-04 16:10:44 +05303159 if (raddr->sin_addr.s_addr != laddr->sin_addr.s_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003160 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003161 nesibdev = nesvnic->nesibdev;
3162 nespd = nesqp->nespd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303163 tagged_offset = (u64)(unsigned long)*start_buff;
Christoph Hellwigf64054d2015-12-23 19:12:50 +01003164 ibmr = nes_reg_phys_mr(&nespd->ibpd,
3165 nesqp->ietf_frame_pbase + mpa_frame_offset,
3166 buff_len, IB_ACCESS_LOCAL_WRITE,
3167 &tagged_offset);
Dan Carpenterbc1251e2016-01-12 12:27:43 +03003168 if (IS_ERR(ibmr)) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07003169 nes_debug(NES_DBG_CM, "Unable to register memory region"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303170 "for lSMM for cm_node = %p \n",
3171 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07003172 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303173 nesqp->private_data_len + nesqp->ietf_frame_size,
3174 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Dan Carpenterbc1251e2016-01-12 12:27:43 +03003175 return PTR_ERR(ibmr);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003176 }
3177
3178 ibmr->pd = &nespd->ibpd;
3179 ibmr->device = nespd->ibpd.device;
3180 nesqp->lsmm_mr = ibmr;
3181
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303182 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003183 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303184 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
3185 u64temp);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003186 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3187 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303188 NES_IWARP_SQ_WQE_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003189 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303190 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003191 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303192 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3193 (u64)(unsigned long)(*start_buff));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003194 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303195 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003196 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08003197 if (nesqp->sq_kmapped) {
3198 nesqp->sq_kmapped = 0;
3199 kunmap(nesqp->page);
3200 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003201
Faisal Latif6492cdf2008-07-24 20:50:45 -07003202 nesqp->nesqp_context->ird_ord_sizes |=
3203 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303204 NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003205 } else {
3206 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08003207 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003208 }
3209 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003210
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003211 /* Cache the cm_id in the qp */
3212 nesqp->cm_id = cm_id;
3213 cm_node->cm_id = cm_id;
3214
3215 /* nesqp->cm_node = (void *)cm_id->provider_data; */
3216 cm_id->provider_data = nesqp;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303217 nesqp->active_conn = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003218
Faisal Latif6492cdf2008-07-24 20:50:45 -07003219 if (cm_node->state == NES_CM_STATE_TSA)
3220 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303221 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003222
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003223 nes_cm_init_tsa_conn(nesqp, cm_node);
3224
Tatyana Nikolova56472632014-03-26 17:07:57 -05003225 nesqp->nesqp_context->tcpPorts[0] =
Faisal Latif6a0dde82016-02-26 09:18:02 -06003226 cpu_to_le16(cm_node->loc_port);
Tatyana Nikolova56472632014-03-26 17:07:57 -05003227 nesqp->nesqp_context->tcpPorts[1] =
Faisal Latif6a0dde82016-02-26 09:18:02 -06003228 cpu_to_le16(cm_node->rem_port);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003229
Faisal Latif6a0dde82016-02-26 09:18:02 -06003230 nesqp->nesqp_context->ip0 = cpu_to_le32(cm_node->rem_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003231
3232 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303233 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3234 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003235
Faisal Latif6492cdf2008-07-24 20:50:45 -07003236 nesqp->nesqp_context->arp_index_vlan |=
3237 cpu_to_le32(nes_arp_table(nesdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303238 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
3239 NES_ARP_RESOLVE) << 16);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003240
3241 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003242 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003243
3244 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3245
3246 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003247 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
3248 nesqp->nesqp_context->ird_ord_sizes |=
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003249 cpu_to_le32((u32)cm_node->ord_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003250
3251 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003252 nes_quad.DstIpAdrIndex =
3253 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
Faisal Latif6a0dde82016-02-26 09:18:02 -06003254 nes_quad.SrcIpadr = htonl(cm_node->rem_addr);
3255 nes_quad.TcpPorts[0] = htons(cm_node->rem_port);
3256 nes_quad.TcpPorts[1] = htons(cm_node->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003257
3258 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003259 crc_value = get_crc_value(&nes_quad);
3260 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003261 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303262 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003263
3264 nesqp->hte_index &= adapter->hte_index_mask;
3265 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3266
3267 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3268
Faisal Latif6492cdf2008-07-24 20:50:45 -07003269 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303270 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
3271 "private data length=%u.\n", nesqp->hwqp.qp_id,
Steve Wise24d44a32013-07-04 16:10:44 +05303272 ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port),
3273 ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303274 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
3275 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
3276 buff_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003277
Coly Li73ac36e2009-01-07 18:09:16 -08003278 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003279 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07003280 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003281
3282 cm_event.event = IW_CM_EVENT_ESTABLISHED;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003283 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003284 cm_event.provider_data = (void *)nesqp;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003285 cm_event.local_addr = cm_id->m_local_addr;
3286 cm_event.remote_addr = cm_id->m_remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003287 cm_event.private_data = NULL;
3288 cm_event.private_data_len = 0;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003289 cm_event.ird = cm_node->ird_size;
3290 cm_event.ord = cm_node->ord_size;
3291
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003292 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003293 attr.qp_state = IB_QPS_RTS;
3294 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003295 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003296 cm_node->loopbackpartner->mpa_frame_size =
3297 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003298 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003299 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303300 conn_param->private_data, conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003301 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
3302 }
3303 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003304 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303305 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003306
3307 return 0;
3308}
3309
3310
3311/**
3312 * nes_reject
3313 */
3314int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3315{
3316 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003317 struct nes_cm_node *loopback;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003318 struct nes_cm_core *cm_core;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303319 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003320
3321 atomic_inc(&cm_rejects);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303322 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003323 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003324 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003325 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003326
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303327 if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003328 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003329
Faisal Latif9d5ab132009-03-06 15:15:01 -08003330 if (loopback) {
3331 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3332 loopback->mpa_frame.priv_data_len = pdata_len;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303333 loopback->mpa_frame_size = pdata_len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003334 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303335 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
3336 cm_node->mpa_frame_size = pdata_len;
3337 memcpy(start_buff, pdata, pdata_len);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003338 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303339 return cm_core->api->reject(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003340}
3341
3342
3343/**
3344 * nes_connect
3345 * setup and launch cm connect node
3346 */
3347int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3348{
3349 struct ib_qp *ibqp;
3350 struct nes_qp *nesqp;
3351 struct nes_vnic *nesvnic;
3352 struct nes_device *nesdev;
3353 struct nes_cm_node *cm_node;
3354 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003355 int apbvt_set = 0;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003356 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
3357 struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003358
Steve Wise24d44a32013-07-04 16:10:44 +05303359 if (cm_id->remote_addr.ss_family != AF_INET)
3360 return -ENOSYS;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003361 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3362 if (!ibqp)
3363 return -EINVAL;
3364 nesqp = to_nesqp(ibqp);
3365 if (!nesqp)
3366 return -EINVAL;
3367 nesvnic = to_nesvnic(nesqp->ibqp.device);
3368 if (!nesvnic)
3369 return -EINVAL;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303370 nesdev = nesvnic->nesdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003371 if (!nesdev)
3372 return -EINVAL;
3373
Steve Wise24d44a32013-07-04 16:10:44 +05303374 if (!laddr->sin_port || !raddr->sin_port)
Faisal Latifc5a7d482009-12-09 15:54:08 -08003375 return -EINVAL;
3376
Faisal Latif6492cdf2008-07-24 20:50:45 -07003377 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303378 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
Steve Wise24d44a32013-07-04 16:10:44 +05303379 ntohl(nesvnic->local_ipaddr), ntohl(raddr->sin_addr.s_addr),
3380 ntohs(raddr->sin_port), ntohl(laddr->sin_addr.s_addr),
3381 ntohs(laddr->sin_port));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003382
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003383 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003384 nesqp->active_conn = 1;
3385
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003386 /* cache the cm_id in the qp */
3387 nesqp->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003388 cm_id->provider_data = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003389 nesqp->private_data_len = conn_param->private_data_len;
Tatyana Nikolovad3e51322012-05-11 10:46:16 -05003390
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003391 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003392 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303393 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003394
Tatyana Nikolova56472632014-03-26 17:07:57 -05003395 /* set up the connection params for the node */
3396 cm_info.loc_addr = ntohl(laddr->sin_addr.s_addr);
3397 cm_info.loc_port = ntohs(laddr->sin_port);
3398 cm_info.rem_addr = ntohl(raddr->sin_addr.s_addr);
3399 cm_info.rem_port = ntohs(raddr->sin_port);
3400 cm_info.cm_id = cm_id;
3401 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3402
Steve Wise24d44a32013-07-04 16:10:44 +05303403 if (laddr->sin_addr.s_addr != raddr->sin_addr.s_addr) {
Faisal Latif6a0dde82016-02-26 09:18:02 -06003404 nes_manage_apbvt(nesvnic, cm_info.loc_port,
3405 PCI_FUNC(nesdev->pcidev->devfn),
3406 NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003407 apbvt_set = 1;
3408 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003409
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003410 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003411
3412 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003413 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303414 conn_param->private_data_len, (void *)conn_param->private_data,
3415 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003416 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003417 if (apbvt_set)
Faisal Latif6a0dde82016-02-26 09:18:02 -06003418 nes_manage_apbvt(nesvnic, cm_info.loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303419 PCI_FUNC(nesdev->pcidev->devfn),
3420 NES_MANAGE_APBVT_DEL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003421
Faisal Latif6a0dde82016-02-26 09:18:02 -06003422 nes_debug(NES_DBG_NLMSG, "Delete loc_port = %04X\n",
3423 cm_info.loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003424 cm_id->rem_ref(cm_id);
3425 return -ENOMEM;
3426 }
3427
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003428 record_ird_ord(cm_node, (u16)conn_param->ird, (u16)conn_param->ord);
3429 if (cm_node->send_rdma0_op == SEND_RDMA_READ_ZERO &&
3430 cm_node->ord_size == 0)
3431 cm_node->ord_size = 1;
3432
Faisal Latif53094c32009-04-27 13:37:34 -07003433 cm_node->apbvt_set = apbvt_set;
Faisal Latif854ace982015-05-18 15:28:14 -05003434 cm_node->tos = cm_id->tos;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003435 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003436 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003437 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003438
3439 return 0;
3440}
3441
3442
3443/**
3444 * nes_create_listen
3445 */
3446int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3447{
3448 struct nes_vnic *nesvnic;
3449 struct nes_cm_listener *cm_node;
3450 struct nes_cm_info cm_info;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003451 int err;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003452 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003453
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003454 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303455 cm_id, ntohs(laddr->sin_port));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003456
Faisal Latif6a0dde82016-02-26 09:18:02 -06003457 if (cm_id->m_local_addr.ss_family != AF_INET)
Steve Wise24d44a32013-07-04 16:10:44 +05303458 return -ENOSYS;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003459 nesvnic = to_nesvnic(cm_id->device);
3460 if (!nesvnic)
3461 return -EINVAL;
Roland Dreier69d51022010-08-04 14:25:40 -07003462
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003463 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3464 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3465
3466 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303467 nesvnic->local_ipaddr, laddr->sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003468
3469 /* setup listen params in our api call struct */
Tatyana Nikolova56472632014-03-26 17:07:57 -05003470 cm_info.loc_addr = ntohl(nesvnic->local_ipaddr);
3471 cm_info.loc_port = ntohs(laddr->sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003472 cm_info.backlog = backlog;
3473 cm_info.cm_id = cm_id;
3474
3475 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3476
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003477 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3478 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003479 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303480 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003481 return -ENOMEM;
3482 }
3483
3484 cm_id->provider_data = cm_node;
Faisal Latif854ace982015-05-18 15:28:14 -05003485 cm_node->tos = cm_id->tos;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003486
3487 if (!cm_node->reused_node) {
Faisal Latif6a0dde82016-02-26 09:18:02 -06003488 err = nes_manage_apbvt(nesvnic, cm_node->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303489 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3490 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003491 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003492 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303493 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003494 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3495 return err;
3496 }
Faisal Latif6e10d2e2010-02-12 19:55:03 +00003497 atomic_inc(&cm_listens_created);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003498 }
3499
3500 cm_id->add_ref(cm_id);
3501 cm_id->provider_data = (void *)cm_node;
3502
3503
3504 return 0;
3505}
3506
3507
3508/**
3509 * nes_destroy_listen
3510 */
3511int nes_destroy_listen(struct iw_cm_id *cm_id)
3512{
3513 if (cm_id->provider_data)
3514 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3515 else
3516 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3517
3518 cm_id->rem_ref(cm_id);
3519
3520 return 0;
3521}
3522
3523
3524/**
3525 * nes_cm_recv
3526 */
3527int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3528{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003529 int rc = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303530
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003531 cm_packets_received++;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303532 if ((g_cm_core) && (g_cm_core->api))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003533 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303534 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003535 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303536 " cm is not setup properly.\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003537
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003538 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003539}
3540
3541
3542/**
3543 * nes_cm_start
3544 * Start and init a cm core module
3545 */
3546int nes_cm_start(void)
3547{
3548 nes_debug(NES_DBG_CM, "\n");
3549 /* create the primary CM core, pass this handle to subsequent core inits */
3550 g_cm_core = nes_cm_alloc_core();
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303551 if (g_cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003552 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303553 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003554 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003555}
3556
3557
3558/**
3559 * nes_cm_stop
3560 * stop and dealloc all cm core instances
3561 */
3562int nes_cm_stop(void)
3563{
3564 g_cm_core->api->destroy_cm_core(g_cm_core);
3565 return 0;
3566}
3567
3568
3569/**
3570 * cm_event_connected
3571 * handle a connected event, setup QPs and HW
3572 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003573static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003574{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003575 struct nes_qp *nesqp;
3576 struct nes_vnic *nesvnic;
3577 struct nes_device *nesdev;
3578 struct nes_cm_node *cm_node;
3579 struct nes_adapter *nesadapter;
3580 struct ib_qp_attr attr;
3581 struct iw_cm_id *cm_id;
3582 struct iw_cm_event cm_event;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003583 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003584 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003585 int ret;
Steve Wise24d44a32013-07-04 16:10:44 +05303586 struct sockaddr_in *laddr;
3587 struct sockaddr_in *raddr;
3588 struct sockaddr_in *cm_event_laddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003589
3590 /* get all our handles */
3591 cm_node = event->cm_node;
3592 cm_id = cm_node->cm_id;
3593 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3594 nesqp = (struct nes_qp *)cm_id->provider_data;
3595 nesvnic = to_nesvnic(nesqp->ibqp.device);
3596 nesdev = nesvnic->nesdev;
3597 nesadapter = nesdev->nesadapter;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003598 laddr = (struct sockaddr_in *)&cm_id->m_local_addr;
3599 raddr = (struct sockaddr_in *)&cm_id->m_remote_addr;
Steve Wise24d44a32013-07-04 16:10:44 +05303600 cm_event_laddr = (struct sockaddr_in *)&cm_event.local_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003601
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303602 if (nesqp->destroyed)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003603 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003604 atomic_inc(&cm_connecteds);
3605 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303606 " local port 0x%04X. jiffies = %lu.\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303607 nesqp->hwqp.qp_id, ntohl(raddr->sin_addr.s_addr),
3608 ntohs(raddr->sin_port), ntohs(laddr->sin_port), jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003609
3610 nes_cm_init_tsa_conn(nesqp, cm_node);
3611
3612 /* set the QP tsa context */
Tatyana Nikolova56472632014-03-26 17:07:57 -05003613 nesqp->nesqp_context->tcpPorts[0] =
Faisal Latif6a0dde82016-02-26 09:18:02 -06003614 cpu_to_le16(cm_node->loc_port);
Tatyana Nikolova56472632014-03-26 17:07:57 -05003615 nesqp->nesqp_context->tcpPorts[1] =
Faisal Latif6a0dde82016-02-26 09:18:02 -06003616 cpu_to_le16(cm_node->rem_port);
3617 nesqp->nesqp_context->ip0 = cpu_to_le32(cm_node->rem_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003618
3619 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003620 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3621 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003622 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003623 nes_arp_table(nesdev,
3624 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003625 NULL, NES_ARP_RESOLVE) << 16);
3626 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3627 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3628 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3629 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003630 cpu_to_le32((u32)1 <<
3631 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003632 nesqp->nesqp_context->ird_ord_sizes |=
3633 cpu_to_le32((u32)cm_node->ord_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003634
3635 /* Adjust tail for not having a LSMM */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303636 /*nesqp->hwqp.sq_tail = 1;*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003637
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303638 build_rdma0_msg(cm_node, &nesqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003639
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303640 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3641 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003642
3643 memset(&nes_quad, 0, sizeof(nes_quad));
3644
Faisal Latif6492cdf2008-07-24 20:50:45 -07003645 nes_quad.DstIpAdrIndex =
3646 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
Faisal Latif6a0dde82016-02-26 09:18:02 -06003647 nes_quad.SrcIpadr = htonl(cm_node->rem_addr);
3648 nes_quad.TcpPorts[0] = htons(cm_node->rem_port);
3649 nes_quad.TcpPorts[1] = htons(cm_node->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003650
3651 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003652 crc_value = get_crc_value(&nes_quad);
3653 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003654 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303655 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003656
3657 nesqp->hte_index &= nesadapter->hte_index_mask;
3658 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3659
3660 nesqp->ietf_frame = &cm_node->mpa_frame;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303661 nesqp->private_data_len = (u8)cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003662 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3663
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003664 /* notify OF layer we successfully created the requested connection */
3665 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003666 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003667 cm_event.provider_data = cm_id->provider_data;
Steve Wise24d44a32013-07-04 16:10:44 +05303668 cm_event_laddr->sin_family = AF_INET;
3669 cm_event_laddr->sin_port = laddr->sin_port;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003670 cm_event.remote_addr = cm_id->m_remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003671
Faisal Latif6492cdf2008-07-24 20:50:45 -07003672 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303673 cm_event.private_data_len = (u8)event->cm_node->mpa_frame_size;
3674 cm_event.ird = cm_node->ird_size;
3675 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003676
Faisal Latif6a0dde82016-02-26 09:18:02 -06003677 cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003678 ret = cm_id->event_handler(cm_id, &cm_event);
3679 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3680
3681 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003682 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303683 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003684 attr.qp_state = IB_QPS_RTS;
3685 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003686
Faisal Latif6492cdf2008-07-24 20:50:45 -07003687 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303688 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003689
3690 return;
3691}
3692
3693
3694/**
3695 * cm_event_connect_error
3696 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003697static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003698{
3699 struct nes_qp *nesqp;
3700 struct iw_cm_id *cm_id;
3701 struct iw_cm_event cm_event;
3702 /* struct nes_cm_info cm_info; */
3703 int ret;
3704
3705 if (!event->cm_node)
3706 return;
3707
3708 cm_id = event->cm_node->cm_id;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303709 if (!cm_id)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003710 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003711
3712 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3713 nesqp = cm_id->provider_data;
3714
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303715 if (!nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003716 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003717
3718 /* notify OF layer about this connection error event */
3719 /* cm_id->rem_ref(cm_id); */
3720 nesqp->cm_id = NULL;
3721 cm_id->provider_data = NULL;
3722 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003723 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003724 cm_event.provider_data = cm_id->provider_data;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003725 cm_event.local_addr = cm_id->m_local_addr;
3726 cm_event.remote_addr = cm_id->m_remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003727 cm_event.private_data = NULL;
3728 cm_event.private_data_len = 0;
3729
Steve Wise24d44a32013-07-04 16:10:44 +05303730#ifdef CONFIG_INFINIBAND_NES_DEBUG
3731 {
3732 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3733 &cm_event.local_addr;
3734 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3735 &cm_event.remote_addr;
3736 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, remote_addr=%08x\n",
3737 cm_event_laddr->sin_addr.s_addr, cm_event_raddr->sin_addr.s_addr);
3738 }
3739#endif
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003740
3741 ret = cm_id->event_handler(cm_id, &cm_event);
3742 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3743 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003744 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303745 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003746 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003747
Faisal Latif6492cdf2008-07-24 20:50:45 -07003748 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003749 return;
3750}
3751
3752
3753/**
3754 * cm_event_reset
3755 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003756static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003757{
3758 struct nes_qp *nesqp;
3759 struct iw_cm_id *cm_id;
3760 struct iw_cm_event cm_event;
3761 /* struct nes_cm_info cm_info; */
3762 int ret;
3763
3764 if (!event->cm_node)
3765 return;
3766
3767 if (!event->cm_node->cm_id)
3768 return;
3769
3770 cm_id = event->cm_node->cm_id;
3771
3772 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3773 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003774 if (!nesqp)
3775 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003776
3777 nesqp->cm_id = NULL;
3778 /* cm_id->provider_data = NULL; */
3779 cm_event.event = IW_CM_EVENT_DISCONNECT;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003780 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003781 cm_event.provider_data = cm_id->provider_data;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003782 cm_event.local_addr = cm_id->m_local_addr;
3783 cm_event.remote_addr = cm_id->m_remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003784 cm_event.private_data = NULL;
3785 cm_event.private_data_len = 0;
3786
Faisal Latif183ecfa2008-11-21 20:50:46 -06003787 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003788 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003789 atomic_inc(&cm_closes);
3790 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003791 cm_event.status = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003792 cm_event.provider_data = cm_id->provider_data;
Faisal Latif6a0dde82016-02-26 09:18:02 -06003793 cm_event.local_addr = cm_id->m_local_addr;
3794 cm_event.remote_addr = cm_id->m_remote_addr;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003795 cm_event.private_data = NULL;
3796 cm_event.private_data_len = 0;
3797 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3798 ret = cm_id->event_handler(cm_id, &cm_event);
3799
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003800 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3801
3802
3803 /* notify OF layer about this connection error event */
3804 cm_id->rem_ref(cm_id);
3805
3806 return;
3807}
3808
3809
3810/**
3811 * cm_event_mpa_req
3812 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003813static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003814{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303815 struct iw_cm_id *cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003816 struct iw_cm_event cm_event;
3817 int ret;
3818 struct nes_cm_node *cm_node;
Steve Wise24d44a32013-07-04 16:10:44 +05303819 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3820 &cm_event.local_addr;
3821 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3822 &cm_event.remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003823
3824 cm_node = event->cm_node;
3825 if (!cm_node)
3826 return;
3827 cm_id = cm_node->cm_id;
3828
3829 atomic_inc(&cm_connect_reqs);
3830 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303831 cm_node, cm_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003832
3833 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003834 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003835 cm_event.provider_data = (void *)cm_node;
3836
Steve Wise24d44a32013-07-04 16:10:44 +05303837 cm_event_laddr->sin_family = AF_INET;
3838 cm_event_laddr->sin_port = htons(event->cm_info.loc_port);
3839 cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003840
Steve Wise24d44a32013-07-04 16:10:44 +05303841 cm_event_raddr->sin_family = AF_INET;
3842 cm_event_raddr->sin_port = htons(event->cm_info.rem_port);
3843 cm_event_raddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003844 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303845 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003846 if (cm_node->mpa_frame_rev == IETF_MPA_V1) {
3847 cm_event.ird = NES_MAX_IRD;
3848 cm_event.ord = NES_MAX_ORD;
3849 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303850 cm_event.ird = cm_node->ird_size;
3851 cm_event.ord = cm_node->ord_size;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003852 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003853
3854 ret = cm_id->event_handler(cm_id, &cm_event);
3855 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003856 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303857 __func__, __LINE__, ret);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003858 return;
3859}
3860
3861
3862static void cm_event_mpa_reject(struct nes_cm_event *event)
3863{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303864 struct iw_cm_id *cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003865 struct iw_cm_event cm_event;
3866 struct nes_cm_node *cm_node;
3867 int ret;
Steve Wise24d44a32013-07-04 16:10:44 +05303868 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3869 &cm_event.local_addr;
3870 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3871 &cm_event.remote_addr;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003872
3873 cm_node = event->cm_node;
3874 if (!cm_node)
3875 return;
3876 cm_id = cm_node->cm_id;
3877
3878 atomic_inc(&cm_connect_reqs);
3879 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303880 cm_node, cm_id, jiffies);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003881
3882 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3883 cm_event.status = -ECONNREFUSED;
3884 cm_event.provider_data = cm_id->provider_data;
3885
Steve Wise24d44a32013-07-04 16:10:44 +05303886 cm_event_laddr->sin_family = AF_INET;
3887 cm_event_laddr->sin_port = htons(event->cm_info.loc_port);
3888 cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003889
Steve Wise24d44a32013-07-04 16:10:44 +05303890 cm_event_raddr->sin_family = AF_INET;
3891 cm_event_raddr->sin_port = htons(event->cm_info.rem_port);
3892 cm_event_raddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003893
3894 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303895 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003896
3897 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303898 "remove_addr=%08x\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303899 cm_event_laddr->sin_addr.s_addr,
3900 cm_event_raddr->sin_addr.s_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003901
3902 ret = cm_id->event_handler(cm_id, &cm_event);
3903 if (ret)
3904 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303905 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003906
3907 return;
3908}
3909
3910
3911static void nes_cm_event_handler(struct work_struct *);
3912
3913/**
3914 * nes_cm_post_event
3915 * post an event to the cm event handler
3916 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003917static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003918{
3919 atomic_inc(&event->cm_node->cm_core->events_posted);
3920 add_ref_cm_node(event->cm_node);
3921 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3922 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003923 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303924 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003925
3926 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3927
3928 nes_debug(NES_DBG_CM, "Exit\n");
3929 return 0;
3930}
3931
3932
3933/**
3934 * nes_cm_event_handler
3935 * worker function to handle cm events
3936 * will free instance of nes_cm_event
3937 */
3938static void nes_cm_event_handler(struct work_struct *work)
3939{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003940 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303941 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003942 struct nes_cm_core *cm_core;
3943
Faisal Latif6492cdf2008-07-24 20:50:45 -07003944 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003945 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003946
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003947 cm_core = event->cm_node->cm_core;
3948 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303949 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003950
3951 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003952 case NES_CM_EVENT_MPA_REQ:
3953 cm_event_mpa_req(event);
3954 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303955 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003956 break;
3957 case NES_CM_EVENT_RESET:
3958 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303959 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003960 cm_event_reset(event);
3961 break;
3962 case NES_CM_EVENT_CONNECTED:
3963 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303964 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003965 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003966 cm_event_connected(event);
3967 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3968 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003969 case NES_CM_EVENT_MPA_REJECT:
3970 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303971 (event->cm_node->state == NES_CM_STATE_TSA))
Faisal Latif9d5ab132009-03-06 15:15:01 -08003972 break;
3973 cm_event_mpa_reject(event);
3974 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3975 break;
3976
Faisal Latif6492cdf2008-07-24 20:50:45 -07003977 case NES_CM_EVENT_ABORTED:
3978 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303979 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003980 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003981 cm_event_connect_error(event);
3982 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3983 break;
3984 case NES_CM_EVENT_DROPPED_PKT:
3985 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3986 break;
3987 default:
3988 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3989 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003990 }
3991
3992 atomic_dec(&cm_core->events_posted);
3993 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3994 rem_ref_cm_node(cm_core, event->cm_node);
3995 kfree(event);
3996
3997 return;
3998}