blob: 9047af4299065f543252a29297fbd40e603531b3 [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 */
137static struct nes_cm_ops nes_cm_api = {
138 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
Tatyana Nikolova56472632014-03-26 17:07:57 -0500485 iph->saddr = htonl(cm_node->mapped_loc_addr);
486 iph->daddr = htonl(cm_node->mapped_rem_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800487
Tatyana Nikolova56472632014-03-26 17:07:57 -0500488 tcph->source = htons(cm_node->mapped_loc_port);
489 tcph->dest = htons(cm_node->mapped_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
Tatyana Nikolova56472632014-03-26 17:07:57 -0500528/*
529 * nes_create_sockaddr - Record ip addr and tcp port in a sockaddr struct
530 */
531static void nes_create_sockaddr(__be32 ip_addr, __be16 port,
532 struct sockaddr_storage *addr)
533{
534 struct sockaddr_in *nes_sockaddr = (struct sockaddr_in *)addr;
535 nes_sockaddr->sin_family = AF_INET;
536 memcpy(&nes_sockaddr->sin_addr.s_addr, &ip_addr, sizeof(__be32));
537 nes_sockaddr->sin_port = port;
538}
539
540/*
541 * nes_create_mapinfo - Create a mapinfo object in the port mapper data base
542 */
543static int nes_create_mapinfo(struct nes_cm_info *cm_info)
544{
545 struct sockaddr_storage local_sockaddr;
546 struct sockaddr_storage mapped_sockaddr;
547
548 nes_create_sockaddr(htonl(cm_info->loc_addr), htons(cm_info->loc_port),
549 &local_sockaddr);
550 nes_create_sockaddr(htonl(cm_info->mapped_loc_addr),
551 htons(cm_info->mapped_loc_port), &mapped_sockaddr);
552
553 return iwpm_create_mapinfo(&local_sockaddr,
554 &mapped_sockaddr, RDMA_NL_NES);
555}
556
557/*
558 * nes_remove_mapinfo - Remove a mapinfo object from the port mapper data base
559 * and send a remove mapping op message to
560 * the userspace port mapper
561 */
562static int nes_remove_mapinfo(u32 loc_addr, u16 loc_port,
563 u32 mapped_loc_addr, u16 mapped_loc_port)
564{
565 struct sockaddr_storage local_sockaddr;
566 struct sockaddr_storage mapped_sockaddr;
567
568 nes_create_sockaddr(htonl(loc_addr), htons(loc_port), &local_sockaddr);
569 nes_create_sockaddr(htonl(mapped_loc_addr), htons(mapped_loc_port),
570 &mapped_sockaddr);
571
572 iwpm_remove_mapinfo(&local_sockaddr, &mapped_sockaddr);
573 return iwpm_remove_mapping(&local_sockaddr, RDMA_NL_NES);
574}
575
576/*
577 * nes_form_pm_msg - Form a port mapper message with mapping info
578 */
579static void nes_form_pm_msg(struct nes_cm_info *cm_info,
580 struct iwpm_sa_data *pm_msg)
581{
582 nes_create_sockaddr(htonl(cm_info->loc_addr), htons(cm_info->loc_port),
583 &pm_msg->loc_addr);
584 nes_create_sockaddr(htonl(cm_info->rem_addr), htons(cm_info->rem_port),
585 &pm_msg->rem_addr);
586}
587
588/*
589 * nes_form_reg_msg - Form a port mapper message with dev info
590 */
591static void nes_form_reg_msg(struct nes_vnic *nesvnic,
592 struct iwpm_dev_data *pm_msg)
593{
594 memcpy(pm_msg->dev_name, nesvnic->nesibdev->ibdev.name,
595 IWPM_DEVNAME_SIZE);
596 memcpy(pm_msg->if_name, nesvnic->netdev->name, IWPM_IFNAME_SIZE);
597}
598
Tatyana Nikolova230da362015-04-21 16:28:25 -0400599static void record_sockaddr_info(struct sockaddr_storage *addr_info,
600 nes_addr_t *ip_addr, u16 *port_num)
601{
602 struct sockaddr_in *in_addr = (struct sockaddr_in *)addr_info;
603
604 if (in_addr->sin_family == AF_INET) {
605 *ip_addr = ntohl(in_addr->sin_addr.s_addr);
606 *port_num = ntohs(in_addr->sin_port);
607 }
608}
609
Tatyana Nikolova56472632014-03-26 17:07:57 -0500610/*
611 * nes_record_pm_msg - Save the received mapping info
612 */
613static void nes_record_pm_msg(struct nes_cm_info *cm_info,
614 struct iwpm_sa_data *pm_msg)
615{
Tatyana Nikolova230da362015-04-21 16:28:25 -0400616 record_sockaddr_info(&pm_msg->mapped_loc_addr,
617 &cm_info->mapped_loc_addr, &cm_info->mapped_loc_port);
Tatyana Nikolova56472632014-03-26 17:07:57 -0500618
Tatyana Nikolova230da362015-04-21 16:28:25 -0400619 record_sockaddr_info(&pm_msg->mapped_rem_addr,
620 &cm_info->mapped_rem_addr, &cm_info->mapped_rem_port);
621}
622
623/*
624 * nes_get_reminfo - Get the address info of the remote connecting peer
625 */
626static int nes_get_remote_addr(struct nes_cm_node *cm_node)
627{
628 struct sockaddr_storage mapped_loc_addr, mapped_rem_addr;
629 struct sockaddr_storage remote_addr;
630 int ret;
631
632 nes_create_sockaddr(htonl(cm_node->mapped_loc_addr),
633 htons(cm_node->mapped_loc_port), &mapped_loc_addr);
634 nes_create_sockaddr(htonl(cm_node->mapped_rem_addr),
635 htons(cm_node->mapped_rem_port), &mapped_rem_addr);
636
637 ret = iwpm_get_remote_info(&mapped_loc_addr, &mapped_rem_addr,
638 &remote_addr, RDMA_NL_NES);
639 if (ret)
640 nes_debug(NES_DBG_CM, "Unable to find remote peer address info\n");
641 else
642 record_sockaddr_info(&remote_addr, &cm_node->rem_addr,
643 &cm_node->rem_port);
644 return ret;
Tatyana Nikolova56472632014-03-26 17:07:57 -0500645}
646
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800647/**
648 * print_core - dump a cm core
649 */
650static void print_core(struct nes_cm_core *core)
651{
652 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
653 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
654 if (!core)
655 return;
656 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800657
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530658 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800659
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800660 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
661 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
662
663 nes_debug(NES_DBG_CM, "core : %p \n", core);
664
665 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
666}
667
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -0500668static void record_ird_ord(struct nes_cm_node *cm_node,
669 u16 conn_ird, u16 conn_ord)
670{
671 if (conn_ird > NES_MAX_IRD)
672 conn_ird = NES_MAX_IRD;
673
674 if (conn_ord > NES_MAX_ORD)
675 conn_ord = NES_MAX_ORD;
676
677 cm_node->ird_size = conn_ird;
678 cm_node->ord_size = conn_ord;
679}
680
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530681/**
682 * cm_build_mpa_frame - build a MPA V1 frame or MPA V2 frame
683 */
684static int cm_build_mpa_frame(struct nes_cm_node *cm_node, u8 **start_buff,
685 u16 *buff_len, u8 *pci_mem, u8 mpa_key)
686{
687 int ret = 0;
688
689 *start_buff = (pci_mem) ? pci_mem : &cm_node->mpa_frame_buf[0];
690
691 switch (cm_node->mpa_frame_rev) {
692 case IETF_MPA_V1:
693 *start_buff = (u8 *)*start_buff + sizeof(struct ietf_rtr_msg);
694 *buff_len = sizeof(struct ietf_mpa_v1) + cm_node->mpa_frame_size;
695 build_mpa_v1(cm_node, *start_buff, mpa_key);
696 break;
697 case IETF_MPA_V2:
698 *buff_len = sizeof(struct ietf_mpa_v2) + cm_node->mpa_frame_size;
699 build_mpa_v2(cm_node, *start_buff, mpa_key);
700 break;
701 default:
702 ret = -EINVAL;
703 }
704 return ret;
705}
706
707/**
708 * build_mpa_v2 - build a MPA V2 frame
709 */
710static void build_mpa_v2(struct nes_cm_node *cm_node,
711 void *start_addr, u8 mpa_key)
712{
713 struct ietf_mpa_v2 *mpa_frame = (struct ietf_mpa_v2 *)start_addr;
714 struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800715 u16 ctrl_ird;
716 u16 ctrl_ord;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530717
718 /* initialize the upper 5 bytes of the frame */
719 build_mpa_v1(cm_node, start_addr, mpa_key);
720 mpa_frame->flags |= IETF_MPA_V2_FLAG; /* set a bit to indicate MPA V2 */
721 mpa_frame->priv_data_len += htons(IETF_RTR_MSG_SIZE);
722
723 /* initialize RTR msg */
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -0500724 if (cm_node->mpav2_ird_ord == IETF_NO_IRD_ORD) {
725 ctrl_ird = IETF_NO_IRD_ORD;
726 ctrl_ord = IETF_NO_IRD_ORD;
727 } else {
728 ctrl_ird = cm_node->ird_size & IETF_NO_IRD_ORD;
729 ctrl_ord = cm_node->ord_size & IETF_NO_IRD_ORD;
730 }
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800731 ctrl_ird |= IETF_PEER_TO_PEER;
732 ctrl_ird |= IETF_FLPDU_ZERO_LEN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530733
734 switch (mpa_key) {
735 case MPA_KEY_REQUEST:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800736 ctrl_ord |= IETF_RDMA0_WRITE;
737 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530738 break;
739 case MPA_KEY_REPLY:
740 switch (cm_node->send_rdma0_op) {
741 case SEND_RDMA_WRITE_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800742 ctrl_ord |= IETF_RDMA0_WRITE;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530743 break;
744 case SEND_RDMA_READ_ZERO:
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800745 ctrl_ord |= IETF_RDMA0_READ;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530746 break;
747 }
748 }
Tatyana Nikolova8dd87fb2012-02-25 17:45:37 -0800749 rtr_msg->ctrl_ird = htons(ctrl_ird);
750 rtr_msg->ctrl_ord = htons(ctrl_ord);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530751}
752
753/**
754 * build_mpa_v1 - build a MPA V1 frame
755 */
756static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
757{
758 struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
759
760 switch (mpa_key) {
761 case MPA_KEY_REQUEST:
762 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
763 break;
764 case MPA_KEY_REPLY:
765 memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
766 break;
767 }
768 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
769 mpa_frame->rev = cm_node->mpa_frame_rev;
770 mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
771}
772
773static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
774{
775 u64 u64temp;
776 struct nes_qp *nesqp = *nesqp_addr;
777 struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
778
Tatyana Nikolova43adff32014-03-11 14:20:17 -0500779 u64temp = (unsigned long)nesqp->nesuqp_addr;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530780 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
781 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
782
783 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
784 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
785
786 switch (cm_node->send_rdma0_op) {
787 case SEND_RDMA_WRITE_ZERO:
788 nes_debug(NES_DBG_CM, "Sending first write.\n");
789 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
790 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
791 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
792 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
793 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
794 break;
795
796 case SEND_RDMA_READ_ZERO:
797 default:
Julia Lawall079abea2012-11-03 10:58:37 +0000798 if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO)
799 WARN(1, "Unsupported RDMA0 len operation=%u\n",
800 cm_node->send_rdma0_op);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530801 nes_debug(NES_DBG_CM, "Sending first rdma operation.\n");
802 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
803 cpu_to_le32(NES_IWARP_SQ_OP_RDMAR);
804 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX] = 1;
805 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_HIGH_IDX] = 0;
806 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] = 0;
807 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_STAG_IDX] = 1;
808 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 1;
809 break;
810 }
811
812 if (nesqp->sq_kmapped) {
813 nesqp->sq_kmapped = 0;
814 kunmap(nesqp->page);
815 }
816
817 /*use the reserved spot on the WQ for the extra first WQE*/
818 nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
819 NES_QPCONTEXT_ORDIRD_WRPDU |
820 NES_QPCONTEXT_ORDIRD_ALSMM));
821 nesqp->skip_lsmm = 1;
822 nesqp->hwqp.sq_tail = 0;
823}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800824
825/**
826 * schedule_nes_timer
827 * note - cm_node needs to be protected before calling this. Encase in:
828 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
829 */
830int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530831 enum nes_timer_type type, int send_retrans,
832 int close_when_complete)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800833{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530834 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700835 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800836 struct nes_timer_entry *new_send;
837 int ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800838
839 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
840 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800841 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800842
843 /* new_send->timetosend = currenttime */
844 new_send->retrycount = NES_DEFAULT_RETRYS;
845 new_send->retranscount = NES_DEFAULT_RETRANS;
846 new_send->skb = skb;
847 new_send->timetosend = jiffies;
848 new_send->type = type;
849 new_send->netdev = cm_node->netdev;
850 new_send->send_retrans = send_retrans;
851 new_send->close_when_complete = close_when_complete;
852
853 if (type == NES_TIMER_TYPE_CLOSE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530854 new_send->timetosend += (HZ / 10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800855 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700856 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800857 WARN_ON(1);
858 return -EINVAL;
859 }
860 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800861 }
862
863 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700864 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800865 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700866 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
867 cm_node->send_entry = new_send;
868 add_ref_cm_node(cm_node);
869 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
870 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800871
872 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
873 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700874 nes_debug(NES_DBG_CM, "Error sending packet %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530875 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800876 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700877 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800878 } else {
879 cm_packets_sent++;
880 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700881 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800882 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700883 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800884 return ret;
885 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800886 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800887 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800888
Tatyana Nikolova00ad2552012-12-06 19:56:31 +0000889 if (!timer_pending(&cm_core->tcp_timer))
890 mod_timer(&cm_core->tcp_timer, new_send->timetosend);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800891
892 return ret;
893}
894
Faisal Latif9d5ab132009-03-06 15:15:01 -0800895static void nes_retrans_expired(struct nes_cm_node *cm_node)
896{
Faisal Latif68237a02009-06-22 22:53:28 -0700897 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latifdae58722010-08-14 21:04:56 +0000898 enum nes_cm_node_state state = cm_node->state;
899 cm_node->state = NES_CM_STATE_CLOSED;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530900
Faisal Latifdae58722010-08-14 21:04:56 +0000901 switch (state) {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800902 case NES_CM_STATE_SYN_RCVD:
903 case NES_CM_STATE_CLOSING:
904 rem_ref_cm_node(cm_node->cm_core, cm_node);
905 break;
906 case NES_CM_STATE_LAST_ACK:
907 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700908 if (cm_node->cm_id)
909 cm_id->rem_ref(cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800910 send_reset(cm_node, NULL);
911 break;
912 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800913 add_ref_cm_node(cm_node);
914 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800915 create_event(cm_node, NES_CM_EVENT_ABORTED);
916 }
917}
918
919static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
920{
921 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
922 struct iw_cm_id *cm_id = cm_node->cm_id;
923 struct nes_qp *nesqp;
924 unsigned long qplockflags;
925
926 if (!recv_entry)
927 return;
928 nesqp = (struct nes_qp *)recv_entry->skb;
929 if (nesqp) {
930 spin_lock_irqsave(&nesqp->lock, qplockflags);
931 if (nesqp->cm_id) {
932 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530933 "refcount = %d: HIT A "
934 "NES_TIMER_TYPE_CLOSE with something "
935 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
936 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800937 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
938 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
939 nesqp->ibqp_state = IB_QPS_ERR;
940 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
941 nes_cm_disconn(nesqp);
942 } else {
943 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
944 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530945 "refcount = %d: HIT A "
946 "NES_TIMER_TYPE_CLOSE with nothing "
947 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
948 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800949 }
950 } else if (rem_node) {
951 /* TIME_WAIT state */
952 rem_ref_cm_node(cm_node->cm_core, cm_node);
953 }
954 if (cm_node->cm_id)
955 cm_id->rem_ref(cm_id);
956 kfree(recv_entry);
957 cm_node->recv_entry = NULL;
958}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800959
960/**
961 * nes_cm_timer_tick
962 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -0700963static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800964{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800965 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800966 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800967 struct nes_cm_node *cm_node;
968 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800969 struct list_head *list_core_temp;
970 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800971 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800972 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700973 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800974 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800975
Faisal Latif879e5bd2008-11-21 20:50:41 -0600976 struct list_head timer_list;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530977
Faisal Latif879e5bd2008-11-21 20:50:41 -0600978 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800979 spin_lock_irqsave(&cm_core->ht_lock, flags);
980
Faisal Latif6492cdf2008-07-24 20:50:45 -0700981 list_for_each_safe(list_node, list_core_temp,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530982 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800983 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800984 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600985 add_ref_cm_node(cm_node);
986 list_add(&cm_node->timer_entry, &timer_list);
987 }
988 }
989 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
990
991 list_for_each_safe(list_node, list_core_temp, &timer_list) {
992 cm_node = container_of(list_node, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530993 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800994 recv_entry = cm_node->recv_entry;
995
996 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700997 if (time_after(recv_entry->timetosend, jiffies)) {
998 if (nexttimeout > recv_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530999 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001000 nexttimeout = recv_entry->timetosend;
1001 settimer = 1;
1002 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301003 } else {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001004 handle_recv_entry(cm_node, 1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301005 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001006 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001007
1008 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001009 do {
1010 send_entry = cm_node->send_entry;
1011 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -06001012 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001013 if (time_after(send_entry->timetosend, jiffies)) {
1014 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001015 if ((nexttimeout >
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301016 send_entry->timetosend) ||
1017 !settimer) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001018 nexttimeout =
1019 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001020 settimer = 1;
1021 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001022 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001023 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001024 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001025 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001026 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001027
1028 if ((cm_node->state == NES_CM_STATE_TSA) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301029 (cm_node->state == NES_CM_STATE_CLOSED)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001030 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -06001031 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001032 }
1033
Faisal Latif6492cdf2008-07-24 20:50:45 -07001034 if (!send_entry->retranscount ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301035 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001036 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001037 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001038
Faisal Latif6492cdf2008-07-24 20:50:45 -07001039 spin_unlock_irqrestore(
1040 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001041 nes_retrans_expired(cm_node);
1042 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001043 spin_lock_irqsave(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301044 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -06001045 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001046 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001047 atomic_inc(&send_entry->skb->users);
1048 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001049 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301050 "for node %p, jiffies = %lu, time to send = "
1051 "%lu, retranscount = %u, send_entry->seq_num = "
1052 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
1053 "0x%08X\n", send_entry, cm_node, jiffies,
1054 send_entry->timetosend,
1055 send_entry->retranscount,
1056 send_entry->seq_num,
1057 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001058
Faisal Latif6492cdf2008-07-24 20:50:45 -07001059 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301060 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001061 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001062 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001063 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001064 nes_debug(NES_DBG_CM, "rexmit failed for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301065 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001066 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001067 send_entry->retrycount--;
1068 nexttimeout = jiffies + NES_SHORT_TIME;
1069 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -06001070 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001071 } else {
1072 cm_packets_sent++;
1073 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001074 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301075 "%u, retry count = %u.\n",
1076 send_entry->retranscount,
1077 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001078 if (send_entry->send_retrans) {
1079 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -07001080 timetosend = (NES_RETRY_TIMEOUT <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301081 (NES_DEFAULT_RETRANS - send_entry->retranscount));
Faisal Latif4e9c3902009-04-27 13:39:36 -07001082
Faisal Latif6492cdf2008-07-24 20:50:45 -07001083 send_entry->timetosend = jiffies +
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301084 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001085 if (nexttimeout > send_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301086 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001087 nexttimeout = send_entry->timetosend;
1088 settimer = 1;
1089 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001090 } else {
1091 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001092 close_when_complete =
1093 send_entry->close_when_complete;
1094 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301095 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001096 free_retrans_entry(cm_node);
1097 if (close_when_complete)
1098 rem_ref_cm_node(cm_node->cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301099 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001100 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001101 } while (0);
1102
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001103 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001104 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001105 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001106
1107 if (settimer) {
Tatyana Nikolova00ad2552012-12-06 19:56:31 +00001108 if (!timer_pending(&cm_core->tcp_timer))
1109 mod_timer(&cm_core->tcp_timer, nexttimeout);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001110 }
1111}
1112
1113
1114/**
1115 * send_syn
1116 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001117static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301118 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001119{
1120 int ret;
1121 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001122 char optionsbuffer[sizeof(struct option_mss) +
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301123 sizeof(struct option_windowscale) + sizeof(struct option_base) +
1124 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001125
1126 int optionssize = 0;
1127 /* Sending MSS option */
1128 union all_known_options *options;
1129
1130 if (!cm_node)
1131 return -EINVAL;
1132
1133 options = (union all_known_options *)&optionsbuffer[optionssize];
1134 options->as_mss.optionnum = OPTION_NUMBER_MSS;
1135 options->as_mss.length = sizeof(struct option_mss);
1136 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
1137 optionssize += sizeof(struct option_mss);
1138
1139 options = (union all_known_options *)&optionsbuffer[optionssize];
1140 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
1141 options->as_windowscale.length = sizeof(struct option_windowscale);
1142 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
1143 optionssize += sizeof(struct option_windowscale);
1144
Faisal Latif6492cdf2008-07-24 20:50:45 -07001145 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001146 options = (union all_known_options *)&optionsbuffer[optionssize];
1147 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
1148 options->as_base.length = sizeof(struct option_base);
1149 optionssize += sizeof(struct option_base);
1150 /* we need the size to be a multiple of 4 */
1151 options = (union all_known_options *)&optionsbuffer[optionssize];
1152 options->as_end = 1;
1153 optionssize += 1;
1154 options = (union all_known_options *)&optionsbuffer[optionssize];
1155 options->as_end = 1;
1156 optionssize += 1;
1157 }
1158
1159 options = (union all_known_options *)&optionsbuffer[optionssize];
1160 options->as_end = OPTION_NUMBER_END;
1161 optionssize += 1;
1162
Faisal Latif6492cdf2008-07-24 20:50:45 -07001163 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001164 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001165 if (!skb) {
1166 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1167 return -1;
1168 }
1169
1170 if (sendack)
1171 flags |= SET_ACK;
1172
1173 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
1174 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1175
1176 return ret;
1177}
1178
1179
1180/**
1181 * send_reset
1182 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001183static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001184{
1185 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001186 int flags = SET_RST | SET_ACK;
1187
Faisal Latif6492cdf2008-07-24 20:50:45 -07001188 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001189 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001190 if (!skb) {
1191 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001192 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001193 }
1194
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001195 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
1196 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
1197
1198 return ret;
1199}
1200
1201
1202/**
1203 * send_ack
1204 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001205static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001206{
1207 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001208
1209 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001210 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001211
1212 if (!skb) {
1213 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1214 return -1;
1215 }
1216
1217 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
1218 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
1219
1220 return ret;
1221}
1222
1223
1224/**
1225 * send_fin
1226 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07001227static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001228{
1229 int ret;
1230
1231 /* if we didn't get a frame get one */
1232 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001233 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001234
1235 if (!skb) {
1236 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1237 return -1;
1238 }
1239
1240 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
1241 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1242
1243 return ret;
1244}
1245
1246
1247/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001248 * find_node - find a cm node that matches the reference cm node
1249 */
1250static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301251 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001252{
1253 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001254 struct list_head *hte;
1255 struct nes_cm_node *cm_node;
1256
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001257 /* get a handle on the hte */
1258 hte = &cm_core->connected_nodes;
1259
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001260 /* walk list and find cm_node associated with this session ID */
1261 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001262 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001263 /* compare quad, return node handle if a match */
1264 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301265 cm_node->loc_addr, cm_node->loc_port,
1266 loc_addr, loc_port,
1267 cm_node->rem_addr, cm_node->rem_port,
1268 rem_addr, rem_port);
Tatyana Nikolova56472632014-03-26 17:07:57 -05001269 if ((cm_node->mapped_loc_addr == loc_addr) &&
1270 (cm_node->mapped_loc_port == loc_port) &&
1271 (cm_node->mapped_rem_addr == rem_addr) &&
1272 (cm_node->mapped_rem_port == rem_port)) {
1273
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001274 add_ref_cm_node(cm_node);
1275 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1276 return cm_node;
1277 }
1278 }
1279 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1280
1281 /* no owner node */
1282 return NULL;
1283}
1284
1285
1286/**
1287 * find_listener - find a cm node listening on this addr-port pair
1288 */
1289static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
Tatyana Nikolova56472632014-03-26 17:07:57 -05001290 nes_addr_t dst_addr, u16 dst_port,
1291 enum nes_cm_listener_state listener_state, int local)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001292{
1293 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001294 struct nes_cm_listener *listen_node;
Tatyana Nikolova56472632014-03-26 17:07:57 -05001295 nes_addr_t listen_addr;
1296 u16 listen_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001297
1298 /* walk list and find cm_node associated with this session ID */
1299 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001300 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Tatyana Nikolova56472632014-03-26 17:07:57 -05001301 if (local) {
1302 listen_addr = listen_node->loc_addr;
1303 listen_port = listen_node->loc_port;
1304 } else {
1305 listen_addr = listen_node->mapped_loc_addr;
1306 listen_port = listen_node->mapped_loc_port;
1307 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001308 /* compare node pair, return node handle if a match */
Tatyana Nikolova56472632014-03-26 17:07:57 -05001309 if (((listen_addr == dst_addr) ||
1310 listen_addr == 0x00000000) &&
1311 (listen_port == dst_port) &&
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301312 (listener_state & listen_node->listener_state)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001313 atomic_inc(&listen_node->ref_count);
1314 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1315 return listen_node;
1316 }
1317 }
1318 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1319
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001320 /* no listener */
1321 return NULL;
1322}
1323
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001324/**
1325 * add_hte_node - add a cm node to the hash table
1326 */
1327static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1328{
1329 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001330 struct list_head *hte;
1331
1332 if (!cm_node || !cm_core)
1333 return -EINVAL;
1334
Faisal Latif6492cdf2008-07-24 20:50:45 -07001335 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301336 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001337
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001338 spin_lock_irqsave(&cm_core->ht_lock, flags);
1339
1340 /* get a handle on the hash table element (list head for this slot) */
1341 hte = &cm_core->connected_nodes;
1342 list_add_tail(&cm_node->list, hte);
1343 atomic_inc(&cm_core->ht_node_cnt);
1344
1345 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1346
1347 return 0;
1348}
1349
1350
1351/**
1352 * mini_cm_dec_refcnt_listen
1353 */
1354static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301355 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001356{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001357 int ret = -EINVAL;
1358 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001359 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001360 struct list_head *list_pos = NULL;
1361 struct list_head *list_temp = NULL;
1362 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -06001363 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001364
1365 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301366 "refcnt=%d\n", listener, free_hanging_nodes,
1367 atomic_read(&listener->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001368 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -06001369 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001370 if (free_hanging_nodes) {
1371 spin_lock_irqsave(&cm_core->ht_lock, flags);
1372 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -06001373 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001374 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301375 list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001376 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -06001377 (!cm_node->accelerated)) {
1378 add_ref_cm_node(cm_node);
1379 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001380 }
1381 }
1382 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1383 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001384
1385 list_for_each_safe(list_pos, list_temp, &reset_list) {
1386 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301387 reset_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001388 {
1389 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -08001390 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001391 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
1392 rem_ref_cm_node(cm_node->cm_core, cm_node);
1393 } else {
1394 if (!loopback) {
1395 cleanup_retrans_entry(cm_node);
1396 err = send_reset(cm_node, NULL);
1397 if (err) {
1398 cm_node->state =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301399 NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001400 WARN_ON(1);
1401 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08001402 old_state = cm_node->state;
1403 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1404 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1405 rem_ref_cm_node(
1406 cm_node->cm_core,
1407 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001408 }
1409 } else {
1410 struct nes_cm_event event;
1411
1412 event.cm_node = loopback;
1413 event.cm_info.rem_addr =
1414 loopback->rem_addr;
1415 event.cm_info.loc_addr =
1416 loopback->loc_addr;
1417 event.cm_info.rem_port =
1418 loopback->rem_port;
1419 event.cm_info.loc_port =
1420 loopback->loc_port;
1421 event.cm_info.cm_id = loopback->cm_id;
Faisal Latif43093b92010-02-12 19:57:04 +00001422 add_ref_cm_node(loopback);
1423 loopback->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001424 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001425 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001426
Faisal Latif9d5ab132009-03-06 15:15:01 -08001427 rem_ref_cm_node(cm_node->cm_core,
1428 cm_node);
1429
1430 }
1431 }
1432 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001433 }
1434
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001435 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1436 if (!atomic_dec_return(&listener->ref_count)) {
1437 list_del(&listener->list);
1438
1439 /* decrement our listen node count */
1440 atomic_dec(&cm_core->listen_node_cnt);
1441
1442 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1443
Tatyana Nikolova56472632014-03-26 17:07:57 -05001444 if (listener->nesvnic) {
1445 nes_manage_apbvt(listener->nesvnic,
1446 listener->mapped_loc_port,
1447 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn),
1448 NES_MANAGE_APBVT_DEL);
1449
1450 nes_remove_mapinfo(listener->loc_addr,
1451 listener->loc_port,
1452 listener->mapped_loc_addr,
1453 listener->mapped_loc_port);
1454 nes_debug(NES_DBG_NLMSG,
1455 "Delete APBVT mapped_loc_port = %04X\n",
1456 listener->mapped_loc_port);
1457 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001458
1459 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1460
1461 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001462 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001463 ret = 0;
Faisal Latif6e10d2e2010-02-12 19:55:03 +00001464 atomic_inc(&cm_listens_destroyed);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001465 } else {
1466 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1467 }
1468 if (listener) {
1469 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1470 nes_debug(NES_DBG_CM, "destroying listener (%p)"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301471 " with non-zero pending accepts=%u\n",
1472 listener, atomic_read(&listener->pend_accepts_cnt));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001473 }
1474
1475 return ret;
1476}
1477
1478
1479/**
1480 * mini_cm_del_listen
1481 */
1482static int mini_cm_del_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301483 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001484{
1485 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1486 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1487 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1488}
1489
1490
1491/**
1492 * mini_cm_accelerated
1493 */
1494static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301495 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001496{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001497 cm_node->accelerated = 1;
1498
1499 if (cm_node->accept_pend) {
1500 BUG_ON(!cm_node->listener);
1501 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001502 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001503 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1504 }
1505
Tatyana Nikolova00ad2552012-12-06 19:56:31 +00001506 if (!timer_pending(&cm_core->tcp_timer))
1507 mod_timer(&cm_core->tcp_timer, (jiffies + NES_SHORT_TIME));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001508
1509 return 0;
1510}
1511
1512
1513/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001514 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001515 */
Faisal Latif7a576df2009-12-09 15:54:33 -08001516static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001517{
1518 struct rtable *rt;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001519 struct neighbour *neigh;
Faisal Latif7a576df2009-12-09 15:54:33 -08001520 int rc = arpindex;
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001521 struct net_device *netdev;
Faisal Latif7a576df2009-12-09 15:54:33 -08001522 struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001523
David S. Miller78fbfd82011-03-12 00:00:52 -05001524 rt = ip_route_output(&init_net, htonl(dst_ip), 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001525 if (IS_ERR(rt)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001526 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301527 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001528 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001529 }
1530
Roland Dreiercf55bb22011-03-24 17:13:20 -07001531 if (netif_is_bond_slave(nesvnic->netdev))
Jiri Pirko7f6e7102013-01-03 22:49:00 +00001532 netdev = netdev_master_upper_dev_get(nesvnic->netdev);
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001533 else
1534 netdev = nesvnic->netdev;
1535
Tatyana Nikolovaef3d0c42012-09-20 19:23:14 +00001536 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, netdev);
David Millere55684f2012-01-24 13:16:03 +00001537
David Miller40e2bb52011-12-02 16:52:27 +00001538 rcu_read_lock();
Bob Sharp7191a0a2008-10-03 12:21:19 -07001539 if (neigh) {
1540 if (neigh->nud_state & NUD_VALID) {
1541 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001542 " is %pM, Gateway is 0x%08X \n", dst_ip,
1543 neigh->ha, ntohl(rt->rt_gateway));
Faisal Latif7a576df2009-12-09 15:54:33 -08001544
1545 if (arpindex >= 0) {
Ding Tianhong79adc532014-01-08 10:53:39 +08001546 if (ether_addr_equal(nesadapter->arp_table[arpindex].mac_addr, neigh->ha)) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001547 /* Mac address same as in nes_arp_table */
David Millere55684f2012-01-24 13:16:03 +00001548 goto out;
Faisal Latif7a576df2009-12-09 15:54:33 -08001549 }
1550
1551 nes_manage_arp_cache(nesvnic->netdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301552 nesadapter->arp_table[arpindex].mac_addr,
1553 dst_ip, NES_ARP_DELETE);
Faisal Latif7a576df2009-12-09 15:54:33 -08001554 }
1555
Bob Sharp7191a0a2008-10-03 12:21:19 -07001556 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1557 dst_ip, NES_ARP_ADD);
1558 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1559 NES_ARP_RESOLVE);
David Miller40e2bb52011-12-02 16:52:27 +00001560 } else {
1561 neigh_event_send(neigh, NULL);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001562 }
Bob Sharp7191a0a2008-10-03 12:21:19 -07001563 }
David Millere55684f2012-01-24 13:16:03 +00001564out:
David Miller40e2bb52011-12-02 16:52:27 +00001565 rcu_read_unlock();
David Millere55684f2012-01-24 13:16:03 +00001566
1567 if (neigh)
1568 neigh_release(neigh);
1569
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001570 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001571 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001572}
1573
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001574/**
1575 * make_cm_node - create a new instance of a cm node
1576 */
1577static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301578 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1579 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001580{
1581 struct nes_cm_node *cm_node;
1582 struct timespec ts;
Faisal Latif7a576df2009-12-09 15:54:33 -08001583 int oldarpindex = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001584 int arpindex = 0;
1585 struct nes_device *nesdev;
1586 struct nes_adapter *nesadapter;
1587
1588 /* create an hte and cm_node for this instance */
1589 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1590 if (!cm_node)
1591 return NULL;
1592
1593 /* set our node specific transport info */
Tatyana Nikolova230da362015-04-21 16:28:25 -04001594 if (listener) {
1595 cm_node->loc_addr = listener->loc_addr;
1596 cm_node->loc_port = listener->loc_port;
1597 } else {
1598 cm_node->loc_addr = cm_info->loc_addr;
1599 cm_node->loc_port = cm_info->loc_port;
1600 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001601 cm_node->rem_addr = cm_info->rem_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001602 cm_node->rem_port = cm_info->rem_port;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301603
Tatyana Nikolova56472632014-03-26 17:07:57 -05001604 cm_node->mapped_loc_addr = cm_info->mapped_loc_addr;
1605 cm_node->mapped_rem_addr = cm_info->mapped_rem_addr;
1606 cm_node->mapped_loc_port = cm_info->mapped_loc_port;
1607 cm_node->mapped_rem_port = cm_info->mapped_rem_port;
1608
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301609 cm_node->mpa_frame_rev = mpa_version;
1610 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05001611 cm_node->mpav2_ird_ord = 0;
1612 cm_node->ird_size = 0;
1613 cm_node->ord_size = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301614
Harvey Harrison63779432008-10-31 00:56:00 -07001615 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1616 &cm_node->loc_addr, cm_node->loc_port,
1617 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001618 cm_node->listener = listener;
Faisal Latif854ace982015-05-18 15:28:14 -05001619 if (listener)
1620 cm_node->tos = listener->tos;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001621 cm_node->netdev = nesvnic->netdev;
1622 cm_node->cm_id = cm_info->cm_id;
1623 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1624
Faisal Latif6492cdf2008-07-24 20:50:45 -07001625 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301626 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001627
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001628 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001629
1630 cm_node->loopbackpartner = NULL;
1631 atomic_set(&cm_node->ref_count, 1);
1632 /* associate our parent CM core */
1633 cm_node->cm_core = cm_core;
1634 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1635 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1636 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301637 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001638 ts = current_kernel_time();
1639 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1640 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301641 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001642 cm_node->tcp_cntxt.rcv_nxt = 0;
1643 /* get a unique session ID , add thread_id to an upcounter to handle race */
1644 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001645 cm_node->conn_type = cm_info->conn_type;
1646 cm_node->apbvt_set = 0;
1647 cm_node->accept_pend = 0;
1648
1649 cm_node->nesvnic = nesvnic;
1650 /* get some device handles, for arp lookup */
1651 nesdev = nesvnic->nesdev;
1652 nesadapter = nesdev->nesadapter;
1653
1654 cm_node->loopbackpartner = NULL;
Faisal Latif7a576df2009-12-09 15:54:33 -08001655
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001656 /* get the mac addr for the remote node */
Tatyana Nikolova56472632014-03-26 17:07:57 -05001657 oldarpindex = nes_arp_table(nesdev, cm_node->mapped_rem_addr,
1658 NULL, NES_ARP_RESOLVE);
1659 arpindex = nes_addr_resolve_neigh(nesvnic,
1660 cm_node->mapped_rem_addr, oldarpindex);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001661 if (arpindex < 0) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001662 kfree(cm_node);
1663 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001664 }
1665
1666 /* copy the mac addr to node context */
1667 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001668 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1669 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001670
1671 add_hte_node(cm_core, cm_node);
1672 atomic_inc(&cm_nodes_created);
1673
1674 return cm_node;
1675}
1676
1677
1678/**
1679 * add_ref_cm_node - destroy an instance of a cm node
1680 */
1681static int add_ref_cm_node(struct nes_cm_node *cm_node)
1682{
1683 atomic_inc(&cm_node->ref_count);
1684 return 0;
1685}
1686
1687
1688/**
1689 * rem_ref_cm_node - destroy an instance of a cm node
1690 */
1691static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301692 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001693{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001694 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001695 struct nes_qp *nesqp;
1696
1697 if (!cm_node)
1698 return -EINVAL;
1699
1700 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1701 if (atomic_dec_return(&cm_node->ref_count)) {
1702 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1703 return 0;
1704 }
1705 list_del(&cm_node->list);
1706 atomic_dec(&cm_core->ht_node_cnt);
1707 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1708
1709 /* if the node is destroyed before connection was accelerated */
1710 if (!cm_node->accelerated && cm_node->accept_pend) {
1711 BUG_ON(!cm_node->listener);
1712 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1713 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1714 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001715 WARN_ON(cm_node->send_entry);
1716 if (cm_node->recv_entry)
1717 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001718 if (cm_node->listener) {
1719 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1720 } else {
1721 if (cm_node->apbvt_set && cm_node->nesvnic) {
Tatyana Nikolova56472632014-03-26 17:07:57 -05001722 nes_manage_apbvt(cm_node->nesvnic, cm_node->mapped_loc_port,
1723 PCI_FUNC(cm_node->nesvnic->nesdev->pcidev->devfn),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301724 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001725 }
Tatyana Nikolova56472632014-03-26 17:07:57 -05001726 nes_debug(NES_DBG_NLMSG, "Delete APBVT mapped_loc_port = %04X\n",
1727 cm_node->mapped_loc_port);
1728 nes_remove_mapinfo(cm_node->loc_addr, cm_node->loc_port,
1729 cm_node->mapped_loc_addr, cm_node->mapped_loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001730 }
1731
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001732 atomic_dec(&cm_core->node_cnt);
1733 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001734 nesqp = cm_node->nesqp;
1735 if (nesqp) {
1736 nesqp->cm_node = NULL;
1737 nes_rem_ref(&nesqp->ibqp);
1738 cm_node->nesqp = NULL;
1739 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001740
Faisal Latif6492cdf2008-07-24 20:50:45 -07001741 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001742 return 0;
1743}
1744
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001745/**
1746 * process_options
1747 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001748static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301749 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001750{
1751 u32 tmp;
1752 u32 offset = 0;
1753 union all_known_options *all_options;
1754 char got_mss_option = 0;
1755
1756 while (offset < optionsize) {
1757 all_options = (union all_known_options *)(optionsloc + offset);
1758 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001759 case OPTION_NUMBER_END:
1760 offset = optionsize;
1761 break;
1762 case OPTION_NUMBER_NONE:
1763 offset += 1;
1764 continue;
1765 case OPTION_NUMBER_MSS:
1766 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301767 "Size: %d\n", __func__,
1768 all_options->as_mss.length, offset, optionsize);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001769 got_mss_option = 1;
1770 if (all_options->as_mss.length != 4) {
1771 return 1;
1772 } else {
1773 tmp = ntohs(all_options->as_mss.mss);
1774 if (tmp > 0 && tmp <
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301775 cm_node->tcp_cntxt.mss)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001776 cm_node->tcp_cntxt.mss = tmp;
1777 }
1778 break;
1779 case OPTION_NUMBER_WINDOW_SCALE:
1780 cm_node->tcp_cntxt.snd_wscale =
1781 all_options->as_windowscale.shiftcount;
1782 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001783 default:
1784 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301785 all_options->as_base.optionnum);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001786 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001787 }
1788 offset += all_options->as_base.length;
1789 }
1790 if ((!got_mss_option) && (syn_packet))
1791 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1792 return 0;
1793}
1794
Faisal Latif6492cdf2008-07-24 20:50:45 -07001795static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001796{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001797 atomic_inc(&cm_accel_dropped_pkts);
1798 dev_kfree_skb_any(skb);
1799}
1800
Faisal Latif9d5ab132009-03-06 15:15:01 -08001801static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001802{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001803 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301804 "refcnt=%d\n", cm_node, cm_node->state,
1805 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001806 switch (cm_node->state) {
1807 case NES_CM_STATE_SYN_RCVD:
1808 case NES_CM_STATE_SYN_SENT:
1809 case NES_CM_STATE_ESTABLISHED:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001810 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001811 cm_node->tcp_cntxt.rcv_nxt++;
1812 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001813 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001814 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001815 break;
Faisal Latifb1190d32009-12-09 15:54:32 -08001816 case NES_CM_STATE_MPAREQ_SENT:
1817 create_event(cm_node, NES_CM_EVENT_ABORTED);
1818 cm_node->tcp_cntxt.rcv_nxt++;
1819 cleanup_retrans_entry(cm_node);
1820 cm_node->state = NES_CM_STATE_CLOSED;
1821 add_ref_cm_node(cm_node);
1822 send_reset(cm_node, NULL);
1823 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001824 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001825 cm_node->tcp_cntxt.rcv_nxt++;
1826 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001827 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001828 send_ack(cm_node, NULL);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001829 /* Wait for ACK as this is simultaneous close..
Faisal Latif9d5ab132009-03-06 15:15:01 -08001830 * After we receive ACK, do not send anything..
1831 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001832 break;
1833 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001834 cm_node->tcp_cntxt.rcv_nxt++;
1835 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001836 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001837 send_ack(cm_node, NULL);
1838 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1839 break;
1840 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001841 cm_node->tcp_cntxt.rcv_nxt++;
1842 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001843 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001844 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001845 break;
1846 case NES_CM_STATE_TSA:
1847 default:
1848 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1849 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001850 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001851 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001852}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001853
Faisal Latif6492cdf2008-07-24 20:50:45 -07001854
1855static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1856 struct tcphdr *tcph)
1857{
1858
1859 int reset = 0; /* whether to send reset in case of err.. */
1860 atomic_inc(&cm_resets_recvd);
1861 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1862 " refcnt=%d\n", cm_node, cm_node->state,
1863 atomic_read(&cm_node->ref_count));
1864 cleanup_retrans_entry(cm_node);
1865 switch (cm_node->state) {
1866 case NES_CM_STATE_SYN_SENT:
1867 case NES_CM_STATE_MPAREQ_SENT:
1868 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1869 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1870 cm_node->listener, cm_node->state);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301871 switch (cm_node->mpa_frame_rev) {
1872 case IETF_MPA_V2:
1873 cm_node->mpa_frame_rev = IETF_MPA_V1;
1874 /* send a syn and goto syn sent state */
1875 cm_node->state = NES_CM_STATE_SYN_SENT;
1876 if (send_syn(cm_node, 0, NULL)) {
1877 active_open_err(cm_node, skb, reset);
1878 }
1879 break;
1880 case IETF_MPA_V1:
1881 default:
1882 active_open_err(cm_node, skb, reset);
1883 break;
1884 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001885 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001886 case NES_CM_STATE_MPAREQ_RCVD:
Dan Carpenter4d8d6382010-09-15 18:32:39 +02001887 atomic_inc(&cm_node->passive_state);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001888 dev_kfree_skb_any(skb);
1889 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001890 case NES_CM_STATE_ESTABLISHED:
1891 case NES_CM_STATE_SYN_RCVD:
1892 case NES_CM_STATE_LISTENING:
1893 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1894 passive_open_err(cm_node, skb, reset);
1895 break;
1896 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001897 active_open_err(cm_node, skb, reset);
1898 break;
1899 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001900 drop_packet(skb);
1901 break;
Faisal Latifdae58722010-08-14 21:04:56 +00001902 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif68237a02009-06-22 22:53:28 -07001903 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001904 case NES_CM_STATE_LAST_ACK:
1905 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001906 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001907 cm_node->state = NES_CM_STATE_CLOSED;
1908 rem_ref_cm_node(cm_node->cm_core, cm_node);
1909 drop_packet(skb);
1910 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001911 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001912 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001913 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001914 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001915}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001916
Faisal Latif9d5ab132009-03-06 15:15:01 -08001917
1918static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001919{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301920 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001921 int datasize = skb->len;
1922 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001923
1924 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301925 u32 res_type;
1926
Faisal Latif9d5ab132009-03-06 15:15:01 -08001927 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1928 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001929 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001930 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001931 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301932 "cm_node=%p listener=%p state=%d\n", __func__,
1933 __LINE__, cm_node, cm_node->listener,
1934 cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001935 active_open_err(cm_node, skb, 1);
1936 } else {
1937 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001938 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001939 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001940 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001941
1942 switch (cm_node->state) {
1943 case NES_CM_STATE_ESTABLISHED:
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301944 if (res_type == NES_MPA_REQUEST_REJECT)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001945 /*BIG problem as we are receiving the MPA.. So should
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301946 * not be REJECT.. This is Passive Open.. We can
1947 * only receive it Reject for Active Open...*/
Faisal Latif9d5ab132009-03-06 15:15:01 -08001948 WARN_ON(1);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001949 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1950 type = NES_CM_EVENT_MPA_REQ;
1951 atomic_set(&cm_node->passive_state,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301952 NES_PASSIVE_STATE_INDICATED);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001953 break;
1954 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001955 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001956 if (res_type == NES_MPA_REQUEST_REJECT) {
1957 type = NES_CM_EVENT_MPA_REJECT;
1958 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1959 } else {
1960 type = NES_CM_EVENT_CONNECTED;
1961 cm_node->state = NES_CM_STATE_TSA;
1962 }
1963
1964 break;
1965 default:
1966 WARN_ON(1);
1967 break;
1968 }
1969 dev_kfree_skb_any(skb);
1970 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001971}
1972
1973static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1974{
1975 switch (cm_node->state) {
1976 case NES_CM_STATE_SYN_SENT:
1977 case NES_CM_STATE_MPAREQ_SENT:
1978 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301979 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1980 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001981 active_open_err(cm_node, skb, 1);
1982 break;
1983 case NES_CM_STATE_ESTABLISHED:
1984 case NES_CM_STATE_SYN_RCVD:
1985 passive_open_err(cm_node, skb, 1);
1986 break;
1987 case NES_CM_STATE_TSA:
1988 default:
1989 drop_packet(skb);
1990 }
1991}
1992
1993static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301994 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001995{
1996 int err;
1997
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301998 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001999 if (err)
2000 active_open_err(cm_node, skb, 1);
2001
2002 return err;
2003}
2004
2005static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302006 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002007{
2008 int err = 0;
2009 u32 seq;
2010 u32 ack_seq;
2011 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
2012 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
2013 u32 rcv_wnd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302014
Faisal Latif6492cdf2008-07-24 20:50:45 -07002015 seq = ntohl(tcph->seq);
2016 ack_seq = ntohl(tcph->ack_seq);
2017 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
2018 if (ack_seq != loc_seq_num)
2019 err = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302020 else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07002021 err = 1;
2022 if (err) {
2023 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302024 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
2025 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002026 indicate_pkt_err(cm_node, skb);
2027 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302028 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
2029 rcv_wnd);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002030 }
2031 return err;
2032}
2033
2034/*
2035 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
2036 * is created with a listener or it may comein as rexmitted packet which in
2037 * that case will be just dropped.
2038 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002039static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302040 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002041{
2042 int ret;
2043 u32 inc_sequence;
2044 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002045
2046 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06002047 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002048 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002049
Faisal Latif6492cdf2008-07-24 20:50:45 -07002050 switch (cm_node->state) {
2051 case NES_CM_STATE_SYN_SENT:
2052 case NES_CM_STATE_MPAREQ_SENT:
2053 /* Rcvd syn on active open connection*/
2054 active_open_err(cm_node, skb, 1);
2055 break;
2056 case NES_CM_STATE_LISTENING:
2057 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002058 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302059 cm_node->listener->backlog) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002060 nes_debug(NES_DBG_CM, "drop syn due to backlog "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302061 "pressure \n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07002062 cm_backlog_drops++;
2063 passive_open_err(cm_node, skb, 0);
2064 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002065 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002066 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302067 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002068 if (ret) {
2069 passive_open_err(cm_node, skb, 0);
2070 /* drop pkt */
2071 break;
2072 }
2073 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
2074 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002075 cm_node->accept_pend = 1;
2076 atomic_inc(&cm_node->listener->pend_accepts_cnt);
2077
Faisal Latif6492cdf2008-07-24 20:50:45 -07002078 cm_node->state = NES_CM_STATE_SYN_RCVD;
2079 send_syn(cm_node, 1, 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 Latif6492cdf2008-07-24 20:50:45 -07002086 case NES_CM_STATE_TSA:
2087 case NES_CM_STATE_ESTABLISHED:
2088 case NES_CM_STATE_FIN_WAIT1:
2089 case NES_CM_STATE_FIN_WAIT2:
2090 case NES_CM_STATE_MPAREQ_RCVD:
2091 case NES_CM_STATE_LAST_ACK:
2092 case NES_CM_STATE_CLOSING:
2093 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002094 default:
2095 drop_packet(skb);
2096 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002097 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002098}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002099
Faisal Latif6492cdf2008-07-24 20:50:45 -07002100static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302101 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002102{
Faisal Latif6492cdf2008-07-24 20:50:45 -07002103 int ret;
2104 u32 inc_sequence;
2105 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002106
Faisal Latif6492cdf2008-07-24 20:50:45 -07002107 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06002108 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002109 inc_sequence = ntohl(tcph->seq);
2110 switch (cm_node->state) {
2111 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002112 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002113 /* active open */
2114 if (check_syn(cm_node, tcph, skb))
2115 return;
2116 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2117 /* setup options */
2118 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
2119 if (ret) {
2120 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302121 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002122 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002123 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002124 cleanup_retrans_entry(cm_node);
2125 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
2126 send_mpa_request(cm_node, skb);
2127 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
2128 break;
2129 case NES_CM_STATE_MPAREQ_RCVD:
2130 /* passive open, so should not be here */
2131 passive_open_err(cm_node, skb, 1);
2132 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002133 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002134 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
2135 cleanup_retrans_entry(cm_node);
2136 cm_node->state = NES_CM_STATE_CLOSED;
2137 send_reset(cm_node, skb);
2138 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002139 case NES_CM_STATE_CLOSED:
2140 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
2141 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08002142 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002143 send_reset(cm_node, skb);
2144 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002145 case NES_CM_STATE_ESTABLISHED:
2146 case NES_CM_STATE_FIN_WAIT1:
2147 case NES_CM_STATE_FIN_WAIT2:
2148 case NES_CM_STATE_LAST_ACK:
2149 case NES_CM_STATE_TSA:
2150 case NES_CM_STATE_CLOSING:
2151 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002152 case NES_CM_STATE_MPAREQ_SENT:
2153 default:
2154 drop_packet(skb);
2155 break;
2156 }
2157}
2158
Faisal Latif109d67e2009-04-27 13:41:06 -07002159static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302160 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002161{
2162 int datasize = 0;
2163 u32 inc_sequence;
Faisal Latif109d67e2009-04-27 13:41:06 -07002164 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06002165 int optionsize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302166
Faisal Latifabb77252008-11-21 20:50:52 -06002167 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06002168
Faisal Latif6492cdf2008-07-24 20:50:45 -07002169 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07002170 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002171
2172 skb_pull(skb, tcph->doff << 2);
2173 inc_sequence = ntohl(tcph->seq);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002174 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002175 switch (cm_node->state) {
2176 case NES_CM_STATE_SYN_RCVD:
2177 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07002178 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002179 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
2180 if (ret)
2181 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002182 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2183 cm_node->state = NES_CM_STATE_ESTABLISHED;
2184 if (datasize) {
2185 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Tatyana Nikolova230da362015-04-21 16:28:25 -04002186 nes_get_remote_addr(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002187 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302188 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002189 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302190 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002191 break;
2192 case NES_CM_STATE_ESTABLISHED:
2193 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08002194 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002195 if (datasize) {
2196 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002197 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302198 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002199 drop_packet(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302200 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002201 break;
2202 case NES_CM_STATE_MPAREQ_SENT:
2203 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2204 if (datasize) {
2205 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002206 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302207 } else { /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002208 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302209 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002210 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002211 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002212 cleanup_retrans_entry(cm_node);
2213 cm_node->state = NES_CM_STATE_CLOSED;
2214 send_reset(cm_node, skb);
2215 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002216 case NES_CM_STATE_CLOSED:
2217 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08002218 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002219 send_reset(cm_node, skb);
2220 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002221 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002222 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002223 cleanup_retrans_entry(cm_node);
2224 cm_node->state = NES_CM_STATE_CLOSED;
2225 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002226 rem_ref_cm_node(cm_node->cm_core, cm_node);
2227 drop_packet(skb);
2228 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002229 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002230 cleanup_retrans_entry(cm_node);
2231 drop_packet(skb);
2232 cm_node->state = NES_CM_STATE_FIN_WAIT2;
2233 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002234 case NES_CM_STATE_SYN_SENT:
2235 case NES_CM_STATE_FIN_WAIT2:
2236 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002237 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002238 case NES_CM_STATE_UNKNOWN:
2239 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002240 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002241 drop_packet(skb);
2242 break;
2243 }
Faisal Latif109d67e2009-04-27 13:41:06 -07002244 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002245}
2246
2247
2248
2249static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302250 struct sk_buff *skb, int optionsize, int passive)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002251{
2252 u8 *optionsloc = (u8 *)&tcph[1];
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302253
Faisal Latif6492cdf2008-07-24 20:50:45 -07002254 if (optionsize) {
2255 if (process_options(cm_node, optionsloc, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302256 (u32)tcph->syn)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002257 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302258 __func__, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002259 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06002260 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002261 else
Faisal Latifabb77252008-11-21 20:50:52 -06002262 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002263 return 1;
2264 }
2265 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002266
2267 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302268 cm_node->tcp_cntxt.snd_wscale;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002269
Faisal Latif6492cdf2008-07-24 20:50:45 -07002270 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002271 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002272 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002273}
2274
Faisal Latif6492cdf2008-07-24 20:50:45 -07002275/*
2276 * active_open_err() will send reset() if flag set..
2277 * It will also send ABORT event.
2278 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002279static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302280 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002281{
2282 cleanup_retrans_entry(cm_node);
2283 if (reset) {
2284 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302285 "state=%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002286 add_ref_cm_node(cm_node);
2287 send_reset(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302288 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002289 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302290 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002291
2292 cm_node->state = NES_CM_STATE_CLOSED;
2293 create_event(cm_node, NES_CM_EVENT_ABORTED);
2294}
2295
2296/*
2297 * passive_open_err() will either do a reset() or will free up the skb and
2298 * remove the cm_node.
2299 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002300static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302301 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002302{
2303 cleanup_retrans_entry(cm_node);
2304 cm_node->state = NES_CM_STATE_CLOSED;
2305 if (reset) {
2306 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302307 "cm_node=%p state =%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002308 send_reset(cm_node, skb);
2309 } else {
2310 dev_kfree_skb_any(skb);
2311 rem_ref_cm_node(cm_node->cm_core, cm_node);
2312 }
2313}
2314
2315/*
2316 * free_retrans_entry() routines assumes that the retrans_list_lock has
2317 * been acquired before calling.
2318 */
2319static void free_retrans_entry(struct nes_cm_node *cm_node)
2320{
2321 struct nes_timer_entry *send_entry;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302322
Faisal Latif6492cdf2008-07-24 20:50:45 -07002323 send_entry = cm_node->send_entry;
2324 if (send_entry) {
2325 cm_node->send_entry = NULL;
2326 dev_kfree_skb_any(send_entry->skb);
2327 kfree(send_entry);
2328 rem_ref_cm_node(cm_node->cm_core, cm_node);
2329 }
2330}
2331
2332static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
2333{
2334 unsigned long flags;
2335
2336 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
2337 free_retrans_entry(cm_node);
2338 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
2339}
2340
2341/**
2342 * process_packet
2343 * Returns skb if to be freed, else it will return NULL if already used..
2344 */
2345static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302346 struct nes_cm_core *cm_core)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002347{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302348 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002349 struct tcphdr *tcph = tcp_hdr(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302350 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07002351 int ret = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302352
Faisal Latif6492cdf2008-07-24 20:50:45 -07002353 skb_pull(skb, ip_hdr(skb)->ihl << 2);
2354
2355 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302356 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
2357 tcph->ack, tcph->rst, tcph->fin);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002358
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302359 if (tcph->rst) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002360 pkt_type = NES_PKT_TYPE_RST;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302361 } else if (tcph->syn) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002362 pkt_type = NES_PKT_TYPE_SYN;
2363 if (tcph->ack)
2364 pkt_type = NES_PKT_TYPE_SYNACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302365 } else if (tcph->ack) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002366 pkt_type = NES_PKT_TYPE_ACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302367 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002368 if (tcph->fin)
2369 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002370
2371 switch (pkt_type) {
2372 case NES_PKT_TYPE_SYN:
2373 handle_syn_pkt(cm_node, skb, tcph);
2374 break;
2375 case NES_PKT_TYPE_SYNACK:
2376 handle_synack_pkt(cm_node, skb, tcph);
2377 break;
2378 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002379 ret = handle_ack_pkt(cm_node, skb, tcph);
2380 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08002381 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002382 break;
2383 case NES_PKT_TYPE_RST:
2384 handle_rst_pkt(cm_node, skb, tcph);
2385 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002386 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002387 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08002388 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07002389 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002390 break;
2391 }
2392}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002393
2394/**
2395 * mini_cm_listen - create a listen node with params
2396 */
2397static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova56472632014-03-26 17:07:57 -05002398 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002399{
2400 struct nes_cm_listener *listener;
Tatyana Nikolova56472632014-03-26 17:07:57 -05002401 struct iwpm_dev_data pm_reg_msg;
2402 struct iwpm_sa_data pm_msg;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002403 unsigned long flags;
Tatyana Nikolova56472632014-03-26 17:07:57 -05002404 int iwpm_err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002405
2406 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302407 cm_info->loc_addr, cm_info->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002408
2409 /* cannot have multiple matching listeners */
Tatyana Nikolova56472632014-03-26 17:07:57 -05002410 listener = find_listener(cm_core, cm_info->loc_addr, cm_info->loc_port,
2411 NES_CM_LISTENER_EITHER_STATE, 1);
2412
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002413 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
2414 /* find automatically incs ref count ??? */
2415 atomic_dec(&listener->ref_count);
2416 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
2417 return NULL;
2418 }
2419
2420 if (!listener) {
Tatyana Nikolova56472632014-03-26 17:07:57 -05002421 nes_form_reg_msg(nesvnic, &pm_reg_msg);
2422 iwpm_err = iwpm_register_pid(&pm_reg_msg, RDMA_NL_NES);
2423 if (iwpm_err) {
2424 nes_debug(NES_DBG_NLMSG,
2425 "Port Mapper reg pid fail (err = %d).\n", iwpm_err);
2426 }
2427 if (iwpm_valid_pid() && !iwpm_err) {
2428 nes_form_pm_msg(cm_info, &pm_msg);
2429 iwpm_err = iwpm_add_mapping(&pm_msg, RDMA_NL_NES);
2430 if (iwpm_err)
2431 nes_debug(NES_DBG_NLMSG,
2432 "Port Mapper query fail (err = %d).\n", iwpm_err);
2433 else
2434 nes_record_pm_msg(cm_info, &pm_msg);
2435 }
2436
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002437 /* create a CM listen node (1/2 node to compare incoming traffic to) */
2438 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
2439 if (!listener) {
2440 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
2441 return NULL;
2442 }
2443
Tatyana Nikolova56472632014-03-26 17:07:57 -05002444 listener->loc_addr = cm_info->loc_addr;
2445 listener->loc_port = cm_info->loc_port;
2446 listener->mapped_loc_addr = cm_info->mapped_loc_addr;
2447 listener->mapped_loc_port = cm_info->mapped_loc_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002448 listener->reused_node = 0;
2449
2450 atomic_set(&listener->ref_count, 1);
2451 }
2452 /* pasive case */
2453 /* find already inc'ed the ref count */
2454 else {
2455 listener->reused_node = 1;
2456 }
2457
2458 listener->cm_id = cm_info->cm_id;
2459 atomic_set(&listener->pend_accepts_cnt, 0);
2460 listener->cm_core = cm_core;
2461 listener->nesvnic = nesvnic;
2462 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002463
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002464 listener->conn_type = cm_info->conn_type;
2465 listener->backlog = cm_info->backlog;
2466 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
2467
2468 if (!listener->reused_node) {
2469 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2470 list_add(&listener->list, &cm_core->listen_list.list);
2471 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2472 atomic_inc(&cm_core->listen_node_cnt);
2473 }
2474
2475 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302476 " listener = %p, backlog = %d, cm_id = %p.\n",
2477 cm_info->loc_addr, cm_info->loc_port,
2478 listener, listener->backlog, listener->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002479
2480 return listener;
2481}
2482
2483
2484/**
2485 * mini_cm_connect - make a connection node with params
2486 */
Faisal Latif54c86a82008-09-30 14:47:27 -07002487static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302488 struct nes_vnic *nesvnic, u16 private_data_len,
2489 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002490{
2491 int ret = 0;
2492 struct nes_cm_node *cm_node;
2493 struct nes_cm_listener *loopbackremotelistener;
2494 struct nes_cm_node *loopbackremotenode;
2495 struct nes_cm_info loopback_cm_info;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302496 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002497
2498 /* create a CM connection node */
2499 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2500 if (!cm_node)
2501 return NULL;
2502
Glenn Streiff7495ab62008-04-29 13:46:54 -07002503 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002504 cm_node->tcp_cntxt.client = 1;
2505 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2506
2507 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002508 loopbackremotelistener = find_listener(cm_core,
Tatyana Nikolova56472632014-03-26 17:07:57 -05002509 cm_node->mapped_loc_addr, cm_node->mapped_rem_port,
2510 NES_CM_LISTENER_ACTIVE_STATE, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002511 if (loopbackremotelistener == NULL) {
2512 create_event(cm_node, NES_CM_EVENT_ABORTED);
2513 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002514 loopback_cm_info = *cm_info;
2515 loopback_cm_info.loc_port = cm_info->rem_port;
2516 loopback_cm_info.rem_port = cm_info->loc_port;
Tatyana Nikolova56472632014-03-26 17:07:57 -05002517 loopback_cm_info.mapped_loc_port =
2518 cm_info->mapped_rem_port;
2519 loopback_cm_info.mapped_rem_port =
2520 cm_info->mapped_loc_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002521 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002522 loopbackremotenode = make_cm_node(cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302523 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002524 if (!loopbackremotenode) {
2525 rem_ref_cm_node(cm_node->cm_core, cm_node);
2526 return NULL;
2527 }
2528 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002529 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002530 loopbackremotenode->tcp_cntxt.rcv_wscale =
2531 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002532 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002533 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302534 private_data_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002535 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002536
Faisal Latif6492cdf2008-07-24 20:50:45 -07002537 /* we are done handling this state. */
2538 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002539 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002540 cm_node->tcp_cntxt.rcv_nxt =
2541 loopbackremotenode->tcp_cntxt.loc_seq_num;
2542 loopbackremotenode->tcp_cntxt.rcv_nxt =
2543 cm_node->tcp_cntxt.loc_seq_num;
2544 cm_node->tcp_cntxt.max_snd_wnd =
2545 loopbackremotenode->tcp_cntxt.rcv_wnd;
2546 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2547 cm_node->tcp_cntxt.rcv_wnd;
2548 cm_node->tcp_cntxt.snd_wnd =
2549 loopbackremotenode->tcp_cntxt.rcv_wnd;
2550 loopbackremotenode->tcp_cntxt.snd_wnd =
2551 cm_node->tcp_cntxt.rcv_wnd;
2552 cm_node->tcp_cntxt.snd_wscale =
2553 loopbackremotenode->tcp_cntxt.rcv_wscale;
2554 loopbackremotenode->tcp_cntxt.snd_wscale =
2555 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002556 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002557 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2558 }
2559 return cm_node;
2560 }
2561
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302562 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
2563 cm_node->mpa_frame_size = private_data_len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002564
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302565 memcpy(start_buff, private_data, private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002566
2567 /* send a syn and goto syn sent state */
2568 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002569 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002570
Faisal Latif6492cdf2008-07-24 20:50:45 -07002571 if (ret) {
2572 /* error in sending the syn free up the cm_node struct */
2573 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302574 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2575 cm_node->rem_addr, cm_node->rem_port, cm_node,
2576 cm_node->cm_id);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002577 rem_ref_cm_node(cm_node->cm_core, cm_node);
2578 cm_node = NULL;
2579 }
2580
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302581 if (cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002582 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302583 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2584 cm_node->rem_addr, cm_node->rem_port, cm_node,
2585 cm_node->cm_id);
2586 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002587
2588 return cm_node;
2589}
2590
2591
2592/**
2593 * mini_cm_accept - accept a connection
2594 * This function is never called
2595 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302596static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002597{
2598 return 0;
2599}
2600
2601
2602/**
2603 * mini_cm_reject - reject and teardown a connection
2604 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302605static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002606{
2607 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002608 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002609 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002610 struct nes_cm_event event;
2611 struct iw_cm_id *cm_id = cm_node->cm_id;
2612 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002613
Faisal Latif6492cdf2008-07-24 20:50:45 -07002614 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302615 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002616
Faisal Latif6492cdf2008-07-24 20:50:45 -07002617 if (cm_node->tcp_cntxt.client)
2618 return ret;
2619 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002620
Faisal Latif9d5ab132009-03-06 15:15:01 -08002621 if (!loopback) {
2622 passive_state = atomic_add_return(1, &cm_node->passive_state);
2623 if (passive_state == NES_SEND_RESET_EVENT) {
2624 cm_node->state = NES_CM_STATE_CLOSED;
2625 rem_ref_cm_node(cm_core, cm_node);
2626 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002627 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2628 rem_ref_cm_node(cm_core, cm_node);
2629 } else {
2630 ret = send_mpa_reject(cm_node);
2631 if (ret) {
2632 cm_node->state = NES_CM_STATE_CLOSED;
2633 err = send_reset(cm_node, NULL);
2634 if (err)
2635 WARN_ON(1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302636 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002637 cm_id->add_ref(cm_id);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302638 }
Faisal Latifc5a7d482009-12-09 15:54:08 -08002639 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002640 }
2641 } else {
2642 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002643 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2644 rem_ref_cm_node(cm_core, cm_node);
2645 rem_ref_cm_node(cm_core, loopback);
2646 } else {
2647 event.cm_node = loopback;
2648 event.cm_info.rem_addr = loopback->rem_addr;
2649 event.cm_info.loc_addr = loopback->loc_addr;
2650 event.cm_info.rem_port = loopback->rem_port;
2651 event.cm_info.loc_port = loopback->loc_port;
2652 event.cm_info.cm_id = loopback->cm_id;
2653 cm_event_mpa_reject(&event);
2654 rem_ref_cm_node(cm_core, cm_node);
2655 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002656
Faisal Latifc5a7d482009-12-09 15:54:08 -08002657 cm_id = loopback->cm_id;
2658 rem_ref_cm_node(cm_core, loopback);
2659 cm_id->rem_ref(cm_id);
2660 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002661 }
2662
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002663 return ret;
2664}
2665
2666
2667/**
2668 * mini_cm_close
2669 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002670static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002671{
2672 int ret = 0;
2673
2674 if (!cm_core || !cm_node)
2675 return -EINVAL;
2676
2677 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002678 case NES_CM_STATE_SYN_RCVD:
2679 case NES_CM_STATE_SYN_SENT:
2680 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2681 case NES_CM_STATE_ESTABLISHED:
2682 case NES_CM_STATE_ACCEPTING:
2683 case NES_CM_STATE_MPAREQ_SENT:
2684 case NES_CM_STATE_MPAREQ_RCVD:
2685 cleanup_retrans_entry(cm_node);
2686 send_reset(cm_node, NULL);
2687 break;
2688 case NES_CM_STATE_CLOSE_WAIT:
2689 cm_node->state = NES_CM_STATE_LAST_ACK;
2690 send_fin(cm_node, NULL);
2691 break;
2692 case NES_CM_STATE_FIN_WAIT1:
2693 case NES_CM_STATE_FIN_WAIT2:
2694 case NES_CM_STATE_LAST_ACK:
2695 case NES_CM_STATE_TIME_WAIT:
2696 case NES_CM_STATE_CLOSING:
2697 ret = -1;
2698 break;
2699 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002700 cleanup_retrans_entry(cm_node);
2701 send_reset(cm_node, NULL);
2702 break;
2703 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002704 case NES_CM_STATE_UNKNOWN:
2705 case NES_CM_STATE_INITED:
2706 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002707 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002708 ret = rem_ref_cm_node(cm_core, cm_node);
2709 break;
2710 case NES_CM_STATE_TSA:
2711 if (cm_node->send_entry)
2712 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302713 "send_entry=%p\n", cm_node->send_entry);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002714 ret = rem_ref_cm_node(cm_core, cm_node);
2715 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002716 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002717 return ret;
2718}
2719
2720
2721/**
2722 * recv_pkt - recv an ETHERNET packet, and process it through CM
2723 * node state machine
2724 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002725static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302726 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002727{
2728 struct nes_cm_node *cm_node = NULL;
2729 struct nes_cm_listener *listener = NULL;
2730 struct iphdr *iph;
2731 struct tcphdr *tcph;
2732 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002733 int skb_handled = 1;
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002734 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002735
Faisal Latif6492cdf2008-07-24 20:50:45 -07002736 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002737 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302738 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002739 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002740
2741 iph = (struct iphdr *)skb->data;
2742 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002743
2744 nfo.loc_addr = ntohl(iph->daddr);
2745 nfo.loc_port = ntohs(tcph->dest);
2746 nfo.rem_addr = ntohl(iph->saddr);
2747 nfo.rem_port = ntohs(tcph->source);
2748
Tatyana Nikolova56472632014-03-26 17:07:57 -05002749 /* If port mapper is available these should be mapped address info */
2750 nfo.mapped_loc_addr = ntohl(iph->daddr);
2751 nfo.mapped_loc_port = ntohs(tcph->dest);
2752 nfo.mapped_rem_addr = ntohl(iph->saddr);
2753 nfo.mapped_rem_port = ntohs(tcph->source);
2754
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002755 tmp_daddr = cpu_to_be32(iph->daddr);
2756 tmp_saddr = cpu_to_be32(iph->saddr);
2757
Harvey Harrison63779432008-10-31 00:56:00 -07002758 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e5c2009-01-10 21:45:42 -08002759 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002760
Faisal Latif6492cdf2008-07-24 20:50:45 -07002761 do {
2762 cm_node = find_node(cm_core,
Tatyana Nikolova56472632014-03-26 17:07:57 -05002763 nfo.mapped_rem_port, nfo.mapped_rem_addr,
2764 nfo.mapped_loc_port, nfo.mapped_loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002765
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002766 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002767 /* Only type of packet accepted are for */
2768 /* the PASSIVE open (syn only) */
2769 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002770 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002771 break;
2772 }
Tatyana Nikolova56472632014-03-26 17:07:57 -05002773 listener = find_listener(cm_core, nfo.mapped_loc_addr,
2774 nfo.mapped_loc_port,
2775 NES_CM_LISTENER_ACTIVE_STATE, 0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002776 if (!listener) {
2777 nfo.cm_id = NULL;
2778 nfo.conn_type = 0;
2779 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2780 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002781 break;
2782 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002783 nfo.cm_id = listener->cm_id;
2784 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002785 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302786 listener);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002787 if (!cm_node) {
2788 nes_debug(NES_DBG_CM, "Unable to allocate "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302789 "node\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07002790 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002791 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002792 dev_kfree_skb_any(skb);
2793 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002794 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002795 if (!tcph->rst && !tcph->fin) {
2796 cm_node->state = NES_CM_STATE_LISTENING;
2797 } else {
2798 cm_packets_dropped++;
2799 rem_ref_cm_node(cm_core, cm_node);
2800 dev_kfree_skb_any(skb);
2801 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002802 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002803 add_ref_cm_node(cm_node);
2804 } else if (cm_node->state == NES_CM_STATE_TSA) {
Faisal Latif0f0bee82011-09-25 20:34:00 -05002805 if (cm_node->nesqp->pau_mode)
2806 nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
2807 else {
2808 rem_ref_cm_node(cm_core, cm_node);
2809 atomic_inc(&cm_accel_dropped_pkts);
2810 dev_kfree_skb_any(skb);
2811 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002812 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002813 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002814 skb_reset_network_header(skb);
2815 skb_set_transport_header(skb, sizeof(*tcph));
2816 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002817 process_packet(cm_node, skb, cm_core);
2818 rem_ref_cm_node(cm_core, cm_node);
2819 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002820 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002821}
2822
2823
2824/**
2825 * nes_cm_alloc_core - allocate a top level instance of a cm core
2826 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002827static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002828{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002829 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002830
2831 /* setup the CM core */
2832 /* alloc top level core control structure */
2833 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2834 if (!cm_core)
2835 return NULL;
2836
2837 INIT_LIST_HEAD(&cm_core->connected_nodes);
2838 init_timer(&cm_core->tcp_timer);
2839 cm_core->tcp_timer.function = nes_cm_timer_tick;
2840
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302841 cm_core->mtu = NES_CM_DEFAULT_MTU;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002842 cm_core->state = NES_CM_STATE_INITED;
2843 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2844
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002845 atomic_set(&cm_core->events_posted, 0);
2846
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002847 cm_core->api = &nes_cm_api;
2848
2849 spin_lock_init(&cm_core->ht_lock);
2850 spin_lock_init(&cm_core->listen_list_lock);
2851
2852 INIT_LIST_HEAD(&cm_core->listen_list.list);
2853
2854 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2855
2856 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2857 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2858 cm_core->post_event = nes_cm_post_event;
2859 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2860 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2861
2862 print_core(cm_core);
2863 return cm_core;
2864}
2865
2866
2867/**
2868 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2869 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002870static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002871{
2872 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2873
2874 if (!cm_core)
2875 return -EINVAL;
2876
2877 barrier();
2878
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302879 if (timer_pending(&cm_core->tcp_timer))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002880 del_timer(&cm_core->tcp_timer);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002881
2882 destroy_workqueue(cm_core->event_wq);
2883 destroy_workqueue(cm_core->disconn_wq);
2884 nes_debug(NES_DBG_CM, "\n");
2885 kfree(cm_core);
2886
2887 return 0;
2888}
2889
2890
2891/**
2892 * mini_cm_get
2893 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002894static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002895{
2896 return cm_core->state;
2897}
2898
2899
2900/**
2901 * mini_cm_set
2902 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07002903static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002904{
2905 int ret = 0;
2906
2907 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002908 case NES_CM_SET_PKT_SIZE:
2909 cm_core->mtu = value;
2910 break;
2911 case NES_CM_SET_FREE_PKT_Q_SIZE:
2912 cm_core->free_tx_pkt_max = value;
2913 break;
2914 default:
2915 /* unknown set option */
2916 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002917 }
2918
2919 return ret;
2920}
2921
2922
2923/**
2924 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2925 * successfully exchanged when this is called
2926 */
2927static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2928{
2929 int ret = 0;
2930
2931 if (!nesqp)
2932 return -EINVAL;
2933
2934 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302935 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2936 NES_QPCONTEXT_MISC_DROS);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002937
2938 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2939 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2940
2941 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2942
Faisal Latif854ace982015-05-18 15:28:14 -05002943 nesqp->nesqp_context->misc2 |= cpu_to_le32(
2944 cm_node->tos << NES_QPCONTEXT_MISC2_TOS_SHIFT);
2945
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002946 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2947
2948 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302949 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002950
2951 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302952 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2953 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002954
2955 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302956 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2957 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002958
2959 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2960 nesqp->nesqp_context->ts_recent = 0;
2961 nesqp->nesqp_context->ts_age = 0;
2962 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2963 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2964 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2965 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302966 cm_node->tcp_cntxt.rcv_wscale);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002967 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2968 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2969 nesqp->nesqp_context->srtt = 0;
2970 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2971 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302972 nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002973 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2974 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2975 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2976
2977 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302978 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2979 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2980 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2981 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2982 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2983 le32_to_cpu(nesqp->nesqp_context->misc));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002984 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2985 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2986 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2987
2988 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2989 cm_node->state = NES_CM_STATE_TSA;
2990
2991 return ret;
2992}
2993
2994
2995/**
2996 * nes_cm_disconn
2997 */
2998int nes_cm_disconn(struct nes_qp *nesqp)
2999{
Don Wood873fcdd2009-09-05 20:36:37 -07003000 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003001
Don Wood873fcdd2009-09-05 20:36:37 -07003002 work = kzalloc(sizeof *work, GFP_ATOMIC);
3003 if (!work)
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303004 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003005
Don Wood873fcdd2009-09-05 20:36:37 -07003006 nes_add_ref(&nesqp->ibqp);
3007 work->nesqp = nesqp;
3008 INIT_WORK(&work->work, nes_disconnect_worker);
3009 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003010 return 0;
3011}
3012
3013
3014/**
3015 * nes_disconnect_worker
3016 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003017static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003018{
Don Wood873fcdd2009-09-05 20:36:37 -07003019 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
3020 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003021
Don Wood873fcdd2009-09-05 20:36:37 -07003022 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003023 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303024 nesqp->last_aeq, nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003025 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07003026 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003027}
3028
3029
3030/**
3031 * nes_cm_disconn_true
3032 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003033static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003034{
3035 unsigned long flags;
3036 int ret = 0;
3037 struct iw_cm_id *cm_id;
3038 struct iw_cm_event cm_event;
3039 struct nes_vnic *nesvnic;
3040 u16 last_ae;
3041 u8 original_hw_tcp_state;
3042 u8 original_ibqp_state;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003043 int disconn_status = 0;
Don Woodb29a4fc2009-09-05 20:36:39 -07003044 int issue_disconn = 0;
3045 int issue_close = 0;
3046 int issue_flush = 0;
3047 u32 flush_q = NES_CQP_FLUSH_RQ;
3048 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003049
3050 if (!nesqp) {
3051 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
3052 return -1;
3053 }
3054
3055 spin_lock_irqsave(&nesqp->lock, flags);
3056 cm_id = nesqp->cm_id;
3057 /* make sure we havent already closed this connection */
3058 if (!cm_id) {
3059 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303060 nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003061 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003062 return -1;
3063 }
3064
3065 nesvnic = to_nesvnic(nesqp->ibqp.device);
3066 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
3067
3068 original_hw_tcp_state = nesqp->hw_tcp_state;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303069 original_ibqp_state = nesqp->ibqp_state;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003070 last_ae = nesqp->last_aeq;
3071
Don Woodb29a4fc2009-09-05 20:36:39 -07003072 if (nesqp->term_flags) {
3073 issue_disconn = 1;
3074 issue_close = 1;
3075 nesqp->cm_id = NULL;
Tatyana Nikolova196f40c2011-12-05 11:39:12 -06003076 del_timer(&nesqp->terminate_timer);
Don Woodb29a4fc2009-09-05 20:36:39 -07003077 if (nesqp->flush_issued == 0) {
3078 nesqp->flush_issued = 1;
3079 issue_flush = 1;
3080 }
3081 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
3082 ((original_ibqp_state == IB_QPS_RTS) &&
3083 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
3084 issue_disconn = 1;
3085 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003086 disconn_status = -ECONNRESET;
Don Woodb29a4fc2009-09-05 20:36:39 -07003087 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003088
Don Woodb29a4fc2009-09-05 20:36:39 -07003089 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
3090 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
3091 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
3092 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
3093 issue_close = 1;
3094 nesqp->cm_id = NULL;
3095 if (nesqp->flush_issued == 0) {
3096 nesqp->flush_issued = 1;
3097 issue_flush = 1;
3098 }
3099 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003100
Don Woodb29a4fc2009-09-05 20:36:39 -07003101 spin_unlock_irqrestore(&nesqp->lock, flags);
3102
3103 if ((issue_flush) && (nesqp->destroyed == 0)) {
3104 /* Flush the queue(s) */
3105 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
3106 flush_q |= NES_CQP_FLUSH_SQ;
3107 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
3108
3109 if (nesqp->term_flags) {
3110 ibevent.device = nesqp->ibqp.device;
3111 ibevent.event = nesqp->terminate_eventtype;
3112 ibevent.element.qp = &nesqp->ibqp;
Tatyana Nikolova784d1352012-05-11 14:29:32 -05003113 if (nesqp->ibqp.event_handler)
3114 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
Don Woodb29a4fc2009-09-05 20:36:39 -07003115 }
3116 }
3117
3118 if ((cm_id) && (cm_id->event_handler)) {
3119 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003120 atomic_inc(&cm_disconnects);
3121 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07003122 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003123 cm_event.local_addr = cm_id->local_addr;
3124 cm_event.remote_addr = cm_id->remote_addr;
3125 cm_event.private_data = NULL;
3126 cm_event.private_data_len = 0;
3127
Faisal Latif6492cdf2008-07-24 20:50:45 -07003128 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303129 " for QP%u, SQ Head = %u, SQ Tail = %u. "
3130 "cm_id = %p, refcount = %u.\n",
3131 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
3132 nesqp->hwqp.sq_tail, cm_id,
3133 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003134
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003135 ret = cm_id->event_handler(cm_id, &cm_event);
3136 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003137 nes_debug(NES_DBG_CM, "OFA CM event_handler "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303138 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003139 }
3140
Don Woodb29a4fc2009-09-05 20:36:39 -07003141 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003142 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003143 nes_disconnect(nesqp, 1);
3144
3145 cm_id->provider_data = nesqp;
3146 /* Send up the close complete event */
3147 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003148 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003149 cm_event.provider_data = cm_id->provider_data;
3150 cm_event.local_addr = cm_id->local_addr;
3151 cm_event.remote_addr = cm_id->remote_addr;
3152 cm_event.private_data = NULL;
3153 cm_event.private_data_len = 0;
3154
3155 ret = cm_id->event_handler(cm_id, &cm_event);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303156 if (ret)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003157 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003158
3159 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003160 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003161 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003162
3163 return 0;
3164}
3165
3166
3167/**
3168 * nes_disconnect
3169 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003170static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003171{
3172 int ret = 0;
3173 struct nes_vnic *nesvnic;
3174 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003175 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003176
3177 nesvnic = to_nesvnic(nesqp->ibqp.device);
3178 if (!nesvnic)
3179 return -EINVAL;
3180
3181 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003182 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003183
3184 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00003185 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003186
3187 if (nesqp->active_conn) {
3188
3189 /* indicate this connection is NOT active */
3190 nesqp->active_conn = 0;
3191 } else {
3192 /* Need to free the Last Streaming Mode Message */
3193 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07003194 if (nesqp->lsmm_mr)
3195 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003196 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303197 nesqp->private_data_len + nesqp->ietf_frame_size,
3198 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003199 }
3200 }
3201
3202 /* close the CM node down if it is still active */
3203 if (nesqp->cm_node) {
3204 nes_debug(NES_DBG_CM, "Call close API\n");
3205
3206 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003207 }
3208
3209 return ret;
3210}
3211
3212
3213/**
3214 * nes_accept
3215 */
3216int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3217{
3218 u64 u64temp;
3219 struct ib_qp *ibqp;
3220 struct nes_qp *nesqp;
3221 struct nes_vnic *nesvnic;
3222 struct nes_device *nesdev;
3223 struct nes_cm_node *cm_node;
3224 struct nes_adapter *adapter;
3225 struct ib_qp_attr attr;
3226 struct iw_cm_event cm_event;
3227 struct nes_hw_qp_wqe *wqe;
3228 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003229 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003230 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003231 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003232 struct nes_ib_device *nesibdev;
3233 struct ib_mr *ibmr = NULL;
3234 struct ib_phys_buf ibphysbuf;
3235 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07003236 u64 tagged_offset;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303237 u8 mpa_frame_offset = 0;
3238 struct ietf_mpa_v2 *mpa_v2_frame;
3239 u8 start_addr = 0;
3240 u8 *start_ptr = &start_addr;
3241 u8 **start_buff = &start_ptr;
3242 u16 buff_len = 0;
Steve Wise24d44a32013-07-04 16:10:44 +05303243 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->local_addr;
3244 struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->remote_addr;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003245
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003246 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3247 if (!ibqp)
3248 return -EINVAL;
3249
3250 /* get all our handles */
3251 nesqp = to_nesqp(ibqp);
3252 nesvnic = to_nesvnic(nesqp->ibqp.device);
3253 nesdev = nesvnic->nesdev;
3254 adapter = nesdev->nesadapter;
3255
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003256 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003257 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
3258 "%s\n", cm_node, nesvnic, nesvnic->netdev,
3259 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003260
Faisal Latifc5a7d482009-12-09 15:54:08 -08003261 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
3262 if (cm_node->loopbackpartner)
3263 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
3264 rem_ref_cm_node(cm_node->cm_core, cm_node);
3265 return -EINVAL;
3266 }
3267
Faisal Latifdae58722010-08-14 21:04:56 +00003268 passive_state = atomic_add_return(1, &cm_node->passive_state);
3269 if (passive_state == NES_SEND_RESET_EVENT) {
3270 rem_ref_cm_node(cm_node->cm_core, cm_node);
3271 return -ECONNRESET;
3272 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003273 /* associate the node with the QP */
3274 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003275 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003276
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003277
Faisal Latif6492cdf2008-07-24 20:50:45 -07003278 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
3279 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003280 atomic_inc(&cm_accepts);
3281
3282 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00003283 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003284
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303285 nesqp->ietf_frame_size = sizeof(struct ietf_mpa_v2);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003286 /* allocate the ietf frame and space for private data */
3287 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303288 nesqp->ietf_frame_size + conn_param->private_data_len,
3289 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003290
Faisal Latif6492cdf2008-07-24 20:50:45 -07003291 if (!nesqp->ietf_frame) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303292 nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07003293 return -ENOMEM;
3294 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303295 mpa_v2_frame = (struct ietf_mpa_v2 *)nesqp->ietf_frame;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003296
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303297 if (cm_node->mpa_frame_rev == IETF_MPA_V1)
3298 mpa_frame_offset = 4;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003299
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003300 if (cm_node->mpa_frame_rev == IETF_MPA_V1 ||
3301 cm_node->mpav2_ird_ord == IETF_NO_IRD_ORD) {
3302 record_ird_ord(cm_node, (u16)conn_param->ird, (u16)conn_param->ord);
3303 }
3304
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303305 memcpy(mpa_v2_frame->priv_data, conn_param->private_data,
3306 conn_param->private_data_len);
3307
3308 cm_build_mpa_frame(cm_node, start_buff, &buff_len, nesqp->ietf_frame, MPA_KEY_REPLY);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003309 nesqp->private_data_len = conn_param->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003310
Faisal Latif6492cdf2008-07-24 20:50:45 -07003311 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
3312 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003313
Steve Wise24d44a32013-07-04 16:10:44 +05303314 if (raddr->sin_addr.s_addr != laddr->sin_addr.s_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003315 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003316 nesibdev = nesvnic->nesibdev;
3317 nespd = nesqp->nespd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303318 ibphysbuf.addr = nesqp->ietf_frame_pbase + mpa_frame_offset;
3319 ibphysbuf.size = buff_len;
3320 tagged_offset = (u64)(unsigned long)*start_buff;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003321 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303322 &ibphysbuf, 1,
3323 IB_ACCESS_LOCAL_WRITE,
3324 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003325 if (!ibmr) {
3326 nes_debug(NES_DBG_CM, "Unable to register memory region"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303327 "for lSMM for cm_node = %p \n",
3328 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07003329 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303330 nesqp->private_data_len + nesqp->ietf_frame_size,
3331 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003332 return -ENOMEM;
3333 }
3334
3335 ibmr->pd = &nespd->ibpd;
3336 ibmr->device = nespd->ibpd.device;
3337 nesqp->lsmm_mr = ibmr;
3338
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303339 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003340 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303341 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
3342 u64temp);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003343 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3344 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303345 NES_IWARP_SQ_WQE_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003346 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303347 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003348 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303349 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3350 (u64)(unsigned long)(*start_buff));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003351 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303352 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003353 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Faisal Latifd2fa9b262009-12-09 15:54:28 -08003354 if (nesqp->sq_kmapped) {
3355 nesqp->sq_kmapped = 0;
3356 kunmap(nesqp->page);
3357 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003358
Faisal Latif6492cdf2008-07-24 20:50:45 -07003359 nesqp->nesqp_context->ird_ord_sizes |=
3360 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303361 NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003362 } else {
3363 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08003364 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003365 }
3366 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003367
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003368 /* Cache the cm_id in the qp */
3369 nesqp->cm_id = cm_id;
3370 cm_node->cm_id = cm_id;
3371
3372 /* nesqp->cm_node = (void *)cm_id->provider_data; */
3373 cm_id->provider_data = nesqp;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303374 nesqp->active_conn = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003375
Faisal Latif6492cdf2008-07-24 20:50:45 -07003376 if (cm_node->state == NES_CM_STATE_TSA)
3377 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303378 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003379
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003380 nes_cm_init_tsa_conn(nesqp, cm_node);
3381
Tatyana Nikolova56472632014-03-26 17:07:57 -05003382 nesqp->nesqp_context->tcpPorts[0] =
3383 cpu_to_le16(cm_node->mapped_loc_port);
3384 nesqp->nesqp_context->tcpPorts[1] =
3385 cpu_to_le16(cm_node->mapped_rem_port);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003386
Tatyana Nikolova56472632014-03-26 17:07:57 -05003387 nesqp->nesqp_context->ip0 = cpu_to_le32(cm_node->mapped_rem_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003388
3389 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303390 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3391 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003392
Faisal Latif6492cdf2008-07-24 20:50:45 -07003393 nesqp->nesqp_context->arp_index_vlan |=
3394 cpu_to_le32(nes_arp_table(nesdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303395 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
3396 NES_ARP_RESOLVE) << 16);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003397
3398 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003399 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003400
3401 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3402
3403 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003404 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
3405 nesqp->nesqp_context->ird_ord_sizes |=
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003406 cpu_to_le32((u32)cm_node->ord_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003407
3408 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003409 nes_quad.DstIpAdrIndex =
3410 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
Tatyana Nikolova56472632014-03-26 17:07:57 -05003411 nes_quad.SrcIpadr = htonl(cm_node->mapped_rem_addr);
3412 nes_quad.TcpPorts[0] = htons(cm_node->mapped_rem_port);
3413 nes_quad.TcpPorts[1] = htons(cm_node->mapped_loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003414
3415 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003416 crc_value = get_crc_value(&nes_quad);
3417 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003418 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303419 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003420
3421 nesqp->hte_index &= adapter->hte_index_mask;
3422 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3423
3424 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3425
Faisal Latif6492cdf2008-07-24 20:50:45 -07003426 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303427 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
3428 "private data length=%u.\n", nesqp->hwqp.qp_id,
Steve Wise24d44a32013-07-04 16:10:44 +05303429 ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port),
3430 ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303431 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
3432 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
3433 buff_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003434
Coly Li73ac36e2009-01-07 18:09:16 -08003435 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003436 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07003437 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003438
3439 cm_event.event = IW_CM_EVENT_ESTABLISHED;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003440 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003441 cm_event.provider_data = (void *)nesqp;
3442 cm_event.local_addr = cm_id->local_addr;
3443 cm_event.remote_addr = cm_id->remote_addr;
3444 cm_event.private_data = NULL;
3445 cm_event.private_data_len = 0;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003446 cm_event.ird = cm_node->ird_size;
3447 cm_event.ord = cm_node->ord_size;
3448
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003449 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003450 attr.qp_state = IB_QPS_RTS;
3451 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003452 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003453 cm_node->loopbackpartner->mpa_frame_size =
3454 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003455 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003456 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303457 conn_param->private_data, conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003458 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
3459 }
3460 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003461 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303462 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003463
3464 return 0;
3465}
3466
3467
3468/**
3469 * nes_reject
3470 */
3471int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3472{
3473 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003474 struct nes_cm_node *loopback;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003475 struct nes_cm_core *cm_core;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303476 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003477
3478 atomic_inc(&cm_rejects);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303479 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003480 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003481 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003482 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003483
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303484 if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003485 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003486
Faisal Latif9d5ab132009-03-06 15:15:01 -08003487 if (loopback) {
3488 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3489 loopback->mpa_frame.priv_data_len = pdata_len;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303490 loopback->mpa_frame_size = pdata_len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003491 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303492 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
3493 cm_node->mpa_frame_size = pdata_len;
3494 memcpy(start_buff, pdata, pdata_len);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003495 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303496 return cm_core->api->reject(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003497}
3498
3499
3500/**
3501 * nes_connect
3502 * setup and launch cm connect node
3503 */
3504int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3505{
3506 struct ib_qp *ibqp;
3507 struct nes_qp *nesqp;
3508 struct nes_vnic *nesvnic;
3509 struct nes_device *nesdev;
3510 struct nes_cm_node *cm_node;
3511 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003512 int apbvt_set = 0;
Steve Wise24d44a32013-07-04 16:10:44 +05303513 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->local_addr;
3514 struct sockaddr_in *raddr = (struct sockaddr_in *)&cm_id->remote_addr;
Tatyana Nikolova56472632014-03-26 17:07:57 -05003515 struct iwpm_dev_data pm_reg_msg;
3516 struct iwpm_sa_data pm_msg;
3517 int iwpm_err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003518
Steve Wise24d44a32013-07-04 16:10:44 +05303519 if (cm_id->remote_addr.ss_family != AF_INET)
3520 return -ENOSYS;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003521 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3522 if (!ibqp)
3523 return -EINVAL;
3524 nesqp = to_nesqp(ibqp);
3525 if (!nesqp)
3526 return -EINVAL;
3527 nesvnic = to_nesvnic(nesqp->ibqp.device);
3528 if (!nesvnic)
3529 return -EINVAL;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303530 nesdev = nesvnic->nesdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003531 if (!nesdev)
3532 return -EINVAL;
3533
Steve Wise24d44a32013-07-04 16:10:44 +05303534 if (!laddr->sin_port || !raddr->sin_port)
Faisal Latifc5a7d482009-12-09 15:54:08 -08003535 return -EINVAL;
3536
Faisal Latif6492cdf2008-07-24 20:50:45 -07003537 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303538 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
Steve Wise24d44a32013-07-04 16:10:44 +05303539 ntohl(nesvnic->local_ipaddr), ntohl(raddr->sin_addr.s_addr),
3540 ntohs(raddr->sin_port), ntohl(laddr->sin_addr.s_addr),
3541 ntohs(laddr->sin_port));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003542
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003543 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003544 nesqp->active_conn = 1;
3545
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003546 /* cache the cm_id in the qp */
3547 nesqp->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003548 cm_id->provider_data = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003549 nesqp->private_data_len = conn_param->private_data_len;
Tatyana Nikolovad3e51322012-05-11 10:46:16 -05003550
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003551 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003552 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303553 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003554
Tatyana Nikolova56472632014-03-26 17:07:57 -05003555 /* set up the connection params for the node */
3556 cm_info.loc_addr = ntohl(laddr->sin_addr.s_addr);
3557 cm_info.loc_port = ntohs(laddr->sin_port);
3558 cm_info.rem_addr = ntohl(raddr->sin_addr.s_addr);
3559 cm_info.rem_port = ntohs(raddr->sin_port);
3560 cm_info.cm_id = cm_id;
3561 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3562
3563 /* No port mapper available, go with the specified peer information */
3564 cm_info.mapped_loc_addr = cm_info.loc_addr;
3565 cm_info.mapped_loc_port = cm_info.loc_port;
3566 cm_info.mapped_rem_addr = cm_info.rem_addr;
3567 cm_info.mapped_rem_port = cm_info.rem_port;
3568
3569 nes_form_reg_msg(nesvnic, &pm_reg_msg);
3570 iwpm_err = iwpm_register_pid(&pm_reg_msg, RDMA_NL_NES);
3571 if (iwpm_err) {
3572 nes_debug(NES_DBG_NLMSG,
3573 "Port Mapper reg pid fail (err = %d).\n", iwpm_err);
3574 }
3575 if (iwpm_valid_pid() && !iwpm_err) {
3576 nes_form_pm_msg(&cm_info, &pm_msg);
3577 iwpm_err = iwpm_add_and_query_mapping(&pm_msg, RDMA_NL_NES);
3578 if (iwpm_err)
3579 nes_debug(NES_DBG_NLMSG,
3580 "Port Mapper query fail (err = %d).\n", iwpm_err);
3581 else
3582 nes_record_pm_msg(&cm_info, &pm_msg);
3583 }
3584
Steve Wise24d44a32013-07-04 16:10:44 +05303585 if (laddr->sin_addr.s_addr != raddr->sin_addr.s_addr) {
Tatyana Nikolova56472632014-03-26 17:07:57 -05003586 nes_manage_apbvt(nesvnic, cm_info.mapped_loc_port,
3587 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003588 apbvt_set = 1;
3589 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003590
Tatyana Nikolova56472632014-03-26 17:07:57 -05003591 if (nes_create_mapinfo(&cm_info))
3592 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003593
3594 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003595
3596 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003597 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303598 conn_param->private_data_len, (void *)conn_param->private_data,
3599 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003600 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003601 if (apbvt_set)
Tatyana Nikolova56472632014-03-26 17:07:57 -05003602 nes_manage_apbvt(nesvnic, cm_info.mapped_loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303603 PCI_FUNC(nesdev->pcidev->devfn),
3604 NES_MANAGE_APBVT_DEL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003605
Tatyana Nikolova56472632014-03-26 17:07:57 -05003606 nes_debug(NES_DBG_NLMSG, "Delete mapped_loc_port = %04X\n",
3607 cm_info.mapped_loc_port);
3608 nes_remove_mapinfo(cm_info.loc_addr, cm_info.loc_port,
3609 cm_info.mapped_loc_addr, cm_info.mapped_loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003610 cm_id->rem_ref(cm_id);
3611 return -ENOMEM;
3612 }
3613
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003614 record_ird_ord(cm_node, (u16)conn_param->ird, (u16)conn_param->ord);
3615 if (cm_node->send_rdma0_op == SEND_RDMA_READ_ZERO &&
3616 cm_node->ord_size == 0)
3617 cm_node->ord_size = 1;
3618
Faisal Latif53094c32009-04-27 13:37:34 -07003619 cm_node->apbvt_set = apbvt_set;
Faisal Latif854ace982015-05-18 15:28:14 -05003620 cm_node->tos = cm_id->tos;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003621 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003622 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003623 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003624
3625 return 0;
3626}
3627
3628
3629/**
3630 * nes_create_listen
3631 */
3632int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3633{
3634 struct nes_vnic *nesvnic;
3635 struct nes_cm_listener *cm_node;
3636 struct nes_cm_info cm_info;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003637 int err;
Steve Wise24d44a32013-07-04 16:10:44 +05303638 struct sockaddr_in *laddr = (struct sockaddr_in *)&cm_id->local_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003639
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003640 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303641 cm_id, ntohs(laddr->sin_port));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003642
Steve Wise24d44a32013-07-04 16:10:44 +05303643 if (cm_id->local_addr.ss_family != AF_INET)
3644 return -ENOSYS;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003645 nesvnic = to_nesvnic(cm_id->device);
3646 if (!nesvnic)
3647 return -EINVAL;
Roland Dreier69d51022010-08-04 14:25:40 -07003648
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003649 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3650 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3651
3652 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303653 nesvnic->local_ipaddr, laddr->sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003654
3655 /* setup listen params in our api call struct */
Tatyana Nikolova56472632014-03-26 17:07:57 -05003656 cm_info.loc_addr = ntohl(nesvnic->local_ipaddr);
3657 cm_info.loc_port = ntohs(laddr->sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003658 cm_info.backlog = backlog;
3659 cm_info.cm_id = cm_id;
3660
3661 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3662
Tatyana Nikolova56472632014-03-26 17:07:57 -05003663 /* No port mapper available, go with the specified info */
3664 cm_info.mapped_loc_addr = cm_info.loc_addr;
3665 cm_info.mapped_loc_port = cm_info.loc_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003666
3667 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3668 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003669 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303670 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003671 return -ENOMEM;
3672 }
3673
3674 cm_id->provider_data = cm_node;
Faisal Latif854ace982015-05-18 15:28:14 -05003675 cm_node->tos = cm_id->tos;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003676
3677 if (!cm_node->reused_node) {
Tatyana Nikolova56472632014-03-26 17:07:57 -05003678 if (nes_create_mapinfo(&cm_info))
3679 return -ENOMEM;
3680
3681 err = nes_manage_apbvt(nesvnic, cm_node->mapped_loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303682 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3683 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003684 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003685 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303686 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003687 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3688 return err;
3689 }
Faisal Latif6e10d2e2010-02-12 19:55:03 +00003690 atomic_inc(&cm_listens_created);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003691 }
3692
3693 cm_id->add_ref(cm_id);
3694 cm_id->provider_data = (void *)cm_node;
3695
3696
3697 return 0;
3698}
3699
3700
3701/**
3702 * nes_destroy_listen
3703 */
3704int nes_destroy_listen(struct iw_cm_id *cm_id)
3705{
3706 if (cm_id->provider_data)
3707 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3708 else
3709 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3710
3711 cm_id->rem_ref(cm_id);
3712
3713 return 0;
3714}
3715
3716
3717/**
3718 * nes_cm_recv
3719 */
3720int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3721{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003722 int rc = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303723
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003724 cm_packets_received++;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303725 if ((g_cm_core) && (g_cm_core->api))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003726 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303727 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003728 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303729 " cm is not setup properly.\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003730
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003731 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003732}
3733
3734
3735/**
3736 * nes_cm_start
3737 * Start and init a cm core module
3738 */
3739int nes_cm_start(void)
3740{
3741 nes_debug(NES_DBG_CM, "\n");
3742 /* create the primary CM core, pass this handle to subsequent core inits */
3743 g_cm_core = nes_cm_alloc_core();
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303744 if (g_cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003745 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303746 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003747 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003748}
3749
3750
3751/**
3752 * nes_cm_stop
3753 * stop and dealloc all cm core instances
3754 */
3755int nes_cm_stop(void)
3756{
3757 g_cm_core->api->destroy_cm_core(g_cm_core);
3758 return 0;
3759}
3760
3761
3762/**
3763 * cm_event_connected
3764 * handle a connected event, setup QPs and HW
3765 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003766static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003767{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003768 struct nes_qp *nesqp;
3769 struct nes_vnic *nesvnic;
3770 struct nes_device *nesdev;
3771 struct nes_cm_node *cm_node;
3772 struct nes_adapter *nesadapter;
3773 struct ib_qp_attr attr;
3774 struct iw_cm_id *cm_id;
3775 struct iw_cm_event cm_event;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003776 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003777 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003778 int ret;
Steve Wise24d44a32013-07-04 16:10:44 +05303779 struct sockaddr_in *laddr;
3780 struct sockaddr_in *raddr;
3781 struct sockaddr_in *cm_event_laddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003782
3783 /* get all our handles */
3784 cm_node = event->cm_node;
3785 cm_id = cm_node->cm_id;
3786 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3787 nesqp = (struct nes_qp *)cm_id->provider_data;
3788 nesvnic = to_nesvnic(nesqp->ibqp.device);
3789 nesdev = nesvnic->nesdev;
3790 nesadapter = nesdev->nesadapter;
Steve Wise24d44a32013-07-04 16:10:44 +05303791 laddr = (struct sockaddr_in *)&cm_id->local_addr;
3792 raddr = (struct sockaddr_in *)&cm_id->remote_addr;
3793 cm_event_laddr = (struct sockaddr_in *)&cm_event.local_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003794
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303795 if (nesqp->destroyed)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003796 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003797 atomic_inc(&cm_connecteds);
3798 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303799 " local port 0x%04X. jiffies = %lu.\n",
Steve Wise24d44a32013-07-04 16:10:44 +05303800 nesqp->hwqp.qp_id, ntohl(raddr->sin_addr.s_addr),
3801 ntohs(raddr->sin_port), ntohs(laddr->sin_port), jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003802
3803 nes_cm_init_tsa_conn(nesqp, cm_node);
3804
3805 /* set the QP tsa context */
Tatyana Nikolova56472632014-03-26 17:07:57 -05003806 nesqp->nesqp_context->tcpPorts[0] =
3807 cpu_to_le16(cm_node->mapped_loc_port);
3808 nesqp->nesqp_context->tcpPorts[1] =
3809 cpu_to_le16(cm_node->mapped_rem_port);
3810 nesqp->nesqp_context->ip0 = cpu_to_le32(cm_node->mapped_rem_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003811
3812 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003813 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3814 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003815 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003816 nes_arp_table(nesdev,
3817 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003818 NULL, NES_ARP_RESOLVE) << 16);
3819 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3820 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3821 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3822 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003823 cpu_to_le32((u32)1 <<
3824 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05003825 nesqp->nesqp_context->ird_ord_sizes |=
3826 cpu_to_le32((u32)cm_node->ord_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003827
3828 /* Adjust tail for not having a LSMM */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303829 /*nesqp->hwqp.sq_tail = 1;*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003830
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303831 build_rdma0_msg(cm_node, &nesqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003832
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303833 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3834 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003835
3836 memset(&nes_quad, 0, sizeof(nes_quad));
3837
Faisal Latif6492cdf2008-07-24 20:50:45 -07003838 nes_quad.DstIpAdrIndex =
3839 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
Tatyana Nikolova56472632014-03-26 17:07:57 -05003840 nes_quad.SrcIpadr = htonl(cm_node->mapped_rem_addr);
3841 nes_quad.TcpPorts[0] = htons(cm_node->mapped_rem_port);
3842 nes_quad.TcpPorts[1] = htons(cm_node->mapped_loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003843
3844 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003845 crc_value = get_crc_value(&nes_quad);
3846 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003847 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303848 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003849
3850 nesqp->hte_index &= nesadapter->hte_index_mask;
3851 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3852
3853 nesqp->ietf_frame = &cm_node->mpa_frame;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303854 nesqp->private_data_len = (u8)cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003855 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3856
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003857 /* notify OF layer we successfully created the requested connection */
3858 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003859 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003860 cm_event.provider_data = cm_id->provider_data;
Steve Wise24d44a32013-07-04 16:10:44 +05303861 cm_event_laddr->sin_family = AF_INET;
3862 cm_event_laddr->sin_port = laddr->sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003863 cm_event.remote_addr = cm_id->remote_addr;
3864
Faisal Latif6492cdf2008-07-24 20:50:45 -07003865 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303866 cm_event.private_data_len = (u8)event->cm_node->mpa_frame_size;
3867 cm_event.ird = cm_node->ird_size;
3868 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003869
Tatyana Nikolova56472632014-03-26 17:07:57 -05003870 cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003871 ret = cm_id->event_handler(cm_id, &cm_event);
3872 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3873
3874 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003875 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303876 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003877 attr.qp_state = IB_QPS_RTS;
3878 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003879
Faisal Latif6492cdf2008-07-24 20:50:45 -07003880 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303881 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003882
3883 return;
3884}
3885
3886
3887/**
3888 * cm_event_connect_error
3889 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003890static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003891{
3892 struct nes_qp *nesqp;
3893 struct iw_cm_id *cm_id;
3894 struct iw_cm_event cm_event;
3895 /* struct nes_cm_info cm_info; */
3896 int ret;
3897
3898 if (!event->cm_node)
3899 return;
3900
3901 cm_id = event->cm_node->cm_id;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303902 if (!cm_id)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003903 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003904
3905 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3906 nesqp = cm_id->provider_data;
3907
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303908 if (!nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003909 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003910
3911 /* notify OF layer about this connection error event */
3912 /* cm_id->rem_ref(cm_id); */
3913 nesqp->cm_id = NULL;
3914 cm_id->provider_data = NULL;
3915 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003916 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003917 cm_event.provider_data = cm_id->provider_data;
3918 cm_event.local_addr = cm_id->local_addr;
3919 cm_event.remote_addr = cm_id->remote_addr;
3920 cm_event.private_data = NULL;
3921 cm_event.private_data_len = 0;
3922
Steve Wise24d44a32013-07-04 16:10:44 +05303923#ifdef CONFIG_INFINIBAND_NES_DEBUG
3924 {
3925 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
3926 &cm_event.local_addr;
3927 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
3928 &cm_event.remote_addr;
3929 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, remote_addr=%08x\n",
3930 cm_event_laddr->sin_addr.s_addr, cm_event_raddr->sin_addr.s_addr);
3931 }
3932#endif
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003933
3934 ret = cm_id->event_handler(cm_id, &cm_event);
3935 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3936 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003937 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303938 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003939 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003940
Faisal Latif6492cdf2008-07-24 20:50:45 -07003941 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003942 return;
3943}
3944
3945
3946/**
3947 * cm_event_reset
3948 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07003949static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003950{
3951 struct nes_qp *nesqp;
3952 struct iw_cm_id *cm_id;
3953 struct iw_cm_event cm_event;
3954 /* struct nes_cm_info cm_info; */
3955 int ret;
3956
3957 if (!event->cm_node)
3958 return;
3959
3960 if (!event->cm_node->cm_id)
3961 return;
3962
3963 cm_id = event->cm_node->cm_id;
3964
3965 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3966 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003967 if (!nesqp)
3968 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003969
3970 nesqp->cm_id = NULL;
3971 /* cm_id->provider_data = NULL; */
3972 cm_event.event = IW_CM_EVENT_DISCONNECT;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003973 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003974 cm_event.provider_data = cm_id->provider_data;
3975 cm_event.local_addr = cm_id->local_addr;
3976 cm_event.remote_addr = cm_id->remote_addr;
3977 cm_event.private_data = NULL;
3978 cm_event.private_data_len = 0;
3979
Faisal Latif183ecfa2008-11-21 20:50:46 -06003980 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003981 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003982 atomic_inc(&cm_closes);
3983 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003984 cm_event.status = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003985 cm_event.provider_data = cm_id->provider_data;
3986 cm_event.local_addr = cm_id->local_addr;
3987 cm_event.remote_addr = cm_id->remote_addr;
3988 cm_event.private_data = NULL;
3989 cm_event.private_data_len = 0;
3990 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3991 ret = cm_id->event_handler(cm_id, &cm_event);
3992
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003993 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3994
3995
3996 /* notify OF layer about this connection error event */
3997 cm_id->rem_ref(cm_id);
3998
3999 return;
4000}
4001
4002
4003/**
4004 * cm_event_mpa_req
4005 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07004006static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004007{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304008 struct iw_cm_id *cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004009 struct iw_cm_event cm_event;
4010 int ret;
4011 struct nes_cm_node *cm_node;
Steve Wise24d44a32013-07-04 16:10:44 +05304012 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
4013 &cm_event.local_addr;
4014 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
4015 &cm_event.remote_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004016
4017 cm_node = event->cm_node;
4018 if (!cm_node)
4019 return;
4020 cm_id = cm_node->cm_id;
4021
4022 atomic_inc(&cm_connect_reqs);
4023 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304024 cm_node, cm_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004025
4026 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07004027 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004028 cm_event.provider_data = (void *)cm_node;
4029
Steve Wise24d44a32013-07-04 16:10:44 +05304030 cm_event_laddr->sin_family = AF_INET;
4031 cm_event_laddr->sin_port = htons(event->cm_info.loc_port);
4032 cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004033
Steve Wise24d44a32013-07-04 16:10:44 +05304034 cm_event_raddr->sin_family = AF_INET;
4035 cm_event_raddr->sin_port = htons(event->cm_info.rem_port);
4036 cm_event_raddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08004037 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304038 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05004039 if (cm_node->mpa_frame_rev == IETF_MPA_V1) {
4040 cm_event.ird = NES_MAX_IRD;
4041 cm_event.ord = NES_MAX_ORD;
4042 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304043 cm_event.ird = cm_node->ird_size;
4044 cm_event.ord = cm_node->ord_size;
Tatyana Nikolova4ac79a72014-03-11 14:17:25 -05004045 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004046
4047 ret = cm_id->event_handler(cm_id, &cm_event);
4048 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08004049 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304050 __func__, __LINE__, ret);
Faisal Latif9d5ab132009-03-06 15:15:01 -08004051 return;
4052}
4053
4054
4055static void cm_event_mpa_reject(struct nes_cm_event *event)
4056{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304057 struct iw_cm_id *cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -08004058 struct iw_cm_event cm_event;
4059 struct nes_cm_node *cm_node;
4060 int ret;
Steve Wise24d44a32013-07-04 16:10:44 +05304061 struct sockaddr_in *cm_event_laddr = (struct sockaddr_in *)
4062 &cm_event.local_addr;
4063 struct sockaddr_in *cm_event_raddr = (struct sockaddr_in *)
4064 &cm_event.remote_addr;
Faisal Latif9d5ab132009-03-06 15:15:01 -08004065
4066 cm_node = event->cm_node;
4067 if (!cm_node)
4068 return;
4069 cm_id = cm_node->cm_id;
4070
4071 atomic_inc(&cm_connect_reqs);
4072 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304073 cm_node, cm_id, jiffies);
Faisal Latif9d5ab132009-03-06 15:15:01 -08004074
4075 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
4076 cm_event.status = -ECONNREFUSED;
4077 cm_event.provider_data = cm_id->provider_data;
4078
Steve Wise24d44a32013-07-04 16:10:44 +05304079 cm_event_laddr->sin_family = AF_INET;
4080 cm_event_laddr->sin_port = htons(event->cm_info.loc_port);
4081 cm_event_laddr->sin_addr.s_addr = htonl(event->cm_info.loc_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08004082
Steve Wise24d44a32013-07-04 16:10:44 +05304083 cm_event_raddr->sin_family = AF_INET;
4084 cm_event_raddr->sin_port = htons(event->cm_info.rem_port);
4085 cm_event_raddr->sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08004086
4087 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304088 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Faisal Latif9d5ab132009-03-06 15:15:01 -08004089
4090 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304091 "remove_addr=%08x\n",
Steve Wise24d44a32013-07-04 16:10:44 +05304092 cm_event_laddr->sin_addr.s_addr,
4093 cm_event_raddr->sin_addr.s_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08004094
4095 ret = cm_id->event_handler(cm_id, &cm_event);
4096 if (ret)
4097 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304098 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004099
4100 return;
4101}
4102
4103
4104static void nes_cm_event_handler(struct work_struct *);
4105
4106/**
4107 * nes_cm_post_event
4108 * post an event to the cm event handler
4109 */
Roland Dreier1a855fbf2008-04-16 21:01:09 -07004110static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004111{
4112 atomic_inc(&event->cm_node->cm_core->events_posted);
4113 add_ref_cm_node(event->cm_node);
4114 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
4115 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07004116 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304117 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004118
4119 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
4120
4121 nes_debug(NES_DBG_CM, "Exit\n");
4122 return 0;
4123}
4124
4125
4126/**
4127 * nes_cm_event_handler
4128 * worker function to handle cm events
4129 * will free instance of nes_cm_event
4130 */
4131static void nes_cm_event_handler(struct work_struct *work)
4132{
Faisal Latif6492cdf2008-07-24 20:50:45 -07004133 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304134 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004135 struct nes_cm_core *cm_core;
4136
Faisal Latif6492cdf2008-07-24 20:50:45 -07004137 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004138 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07004139
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004140 cm_core = event->cm_node->cm_core;
4141 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304142 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004143
4144 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07004145 case NES_CM_EVENT_MPA_REQ:
4146 cm_event_mpa_req(event);
4147 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304148 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07004149 break;
4150 case NES_CM_EVENT_RESET:
4151 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304152 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07004153 cm_event_reset(event);
4154 break;
4155 case NES_CM_EVENT_CONNECTED:
4156 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304157 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004158 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07004159 cm_event_connected(event);
4160 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
4161 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08004162 case NES_CM_EVENT_MPA_REJECT:
4163 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304164 (event->cm_node->state == NES_CM_STATE_TSA))
Faisal Latif9d5ab132009-03-06 15:15:01 -08004165 break;
4166 cm_event_mpa_reject(event);
4167 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
4168 break;
4169
Faisal Latif6492cdf2008-07-24 20:50:45 -07004170 case NES_CM_EVENT_ABORTED:
4171 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05304172 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004173 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07004174 cm_event_connect_error(event);
4175 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
4176 break;
4177 case NES_CM_EVENT_DROPPED_PKT:
4178 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
4179 break;
4180 default:
4181 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
4182 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08004183 }
4184
4185 atomic_dec(&cm_core->events_posted);
4186 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
4187 rem_ref_cm_node(cm_core, event->cm_node);
4188 kfree(event);
4189
4190 return;
4191}