blob: 7dc43ea7d4e77583320edbbe1181190f57f0a6c8 [file] [log] [blame]
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001/*
Chien Tungfa6c87d2009-12-09 15:21:56 -08002 * Copyright (c) 2006 - 2009 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 Sharma60063492011-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 Latifd2fa9b22009-12-09 15:54:28 -080055#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Bob Sharp7191a0a2008-10-03 12:21:19 -070057#include <net/arp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080058#include <net/neighbour.h>
59#include <net/route.h>
60#include <net/ip_fib.h>
Faisal Latifc11470f2009-04-27 13:38:31 -070061#include <net/tcp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080062
63#include "nes.h"
64
65u32 cm_packets_sent;
66u32 cm_packets_bounced;
67u32 cm_packets_dropped;
68u32 cm_packets_retrans;
69u32 cm_packets_created;
70u32 cm_packets_received;
Faisal Latif6e10d2e2010-02-12 19:55:03 +000071atomic_t cm_listens_created;
72atomic_t cm_listens_destroyed;
Glenn Streiff3c2d7742008-02-04 20:20:45 -080073u32 cm_backlog_drops;
74atomic_t cm_loopbacks;
75atomic_t cm_nodes_created;
76atomic_t cm_nodes_destroyed;
77atomic_t cm_accel_dropped_pkts;
78atomic_t cm_resets_recvd;
79
Tatyana Nikolova615eb712011-09-25 20:17:46 +053080static inline int mini_cm_accelerated(struct nes_cm_core *, struct nes_cm_node *);
81static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *, struct nes_vnic *, struct nes_cm_info *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080082static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
Tatyana Nikolova615eb712011-09-25 20:17:46 +053083static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *, struct nes_vnic *, u16, void *, struct nes_cm_info *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070084static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Tatyana Nikolova615eb712011-09-25 20:17:46 +053085static int mini_cm_accept(struct nes_cm_core *, struct nes_cm_node *);
86static int mini_cm_reject(struct nes_cm_core *, struct nes_cm_node *);
87static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *, struct sk_buff *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070088static int mini_cm_dealloc_core(struct nes_cm_core *);
89static int mini_cm_get(struct nes_cm_core *);
90static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070091
Tatyana Nikolova615eb712011-09-25 20:17:46 +053092static void form_cm_frame(struct sk_buff *, struct nes_cm_node *, void *, u32, void *, u32, u8);
Faisal Latif6492cdf2008-07-24 20:50:45 -070093static int add_ref_cm_node(struct nes_cm_node *);
94static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
95
Roland Dreier1a855fb2008-04-16 21:01:09 -070096static int nes_cm_disconn_true(struct nes_qp *);
97static int nes_cm_post_event(struct nes_cm_event *event);
98static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
99static void nes_disconnect_worker(struct work_struct *work);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700100
101static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800102static int send_mpa_reject(struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700103static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
104static int send_reset(struct nes_cm_node *, struct sk_buff *);
105static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fb2008-04-16 21:01:09 -0700106static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530107static void process_packet(struct nes_cm_node *, struct sk_buff *, struct nes_cm_core *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700108
109static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
110static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
111static void cleanup_retrans_entry(struct nes_cm_node *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800112static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700113static void free_retrans_entry(struct nes_cm_node *cm_node);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530114static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph, struct sk_buff *skb, int optionsize, int passive);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700115
116/* CM event handler functions */
117static void cm_event_connected(struct nes_cm_event *);
118static void cm_event_connect_error(struct nes_cm_event *);
119static void cm_event_reset(struct nes_cm_event *);
120static void cm_event_mpa_req(struct nes_cm_event *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800121static void cm_event_mpa_reject(struct nes_cm_event *);
122static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700123
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530124/* MPA build functions */
125static int cm_build_mpa_frame(struct nes_cm_node *, u8 **, u16 *, u8 *, u8);
126static void build_mpa_v2(struct nes_cm_node *, void *, u8);
127static void build_mpa_v1(struct nes_cm_node *, void *, u8);
128static void build_rdma0_msg(struct nes_cm_node *, struct nes_qp **);
129
Faisal Latif6492cdf2008-07-24 20:50:45 -0700130static void print_core(struct nes_cm_core *core);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800131
132/* External CM API Interface */
133/* instance of function pointers for client API */
134/* set address of this instance to cm_core->cm_ops at cm_core alloc */
135static struct nes_cm_ops nes_cm_api = {
136 mini_cm_accelerated,
137 mini_cm_listen,
138 mini_cm_del_listen,
139 mini_cm_connect,
140 mini_cm_close,
141 mini_cm_accept,
142 mini_cm_reject,
143 mini_cm_recv_pkt,
144 mini_cm_dealloc_core,
145 mini_cm_get,
146 mini_cm_set
147};
148
Roland Dreier1a855fb2008-04-16 21:01:09 -0700149static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800150
151atomic_t cm_connects;
152atomic_t cm_accepts;
153atomic_t cm_disconnects;
154atomic_t cm_closes;
155atomic_t cm_connecteds;
156atomic_t cm_connect_reqs;
157atomic_t cm_rejects;
158
159
160/**
161 * create_event
162 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530163static struct nes_cm_event *create_event(struct nes_cm_node * cm_node,
164 enum nes_cm_event_type type)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800165{
166 struct nes_cm_event *event;
167
168 if (!cm_node->cm_id)
169 return NULL;
170
171 /* allocate an empty event */
172 event = kzalloc(sizeof(*event), GFP_ATOMIC);
173
174 if (!event)
175 return NULL;
176
177 event->type = type;
178 event->cm_node = cm_node;
179 event->cm_info.rem_addr = cm_node->rem_addr;
180 event->cm_info.loc_addr = cm_node->loc_addr;
181 event->cm_info.rem_port = cm_node->rem_port;
182 event->cm_info.loc_port = cm_node->loc_port;
183 event->cm_info.cm_id = cm_node->cm_id;
184
Faisal Latif6492cdf2008-07-24 20:50:45 -0700185 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530186 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
187 cm_node, event, type, event->cm_info.loc_addr,
188 event->cm_info.loc_port, event->cm_info.rem_addr,
189 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800190
191 nes_cm_post_event(event);
192 return event;
193}
194
195
196/**
197 * send_mpa_request
198 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700199static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800200{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530201 u8 start_addr = 0;
202 u8 *start_ptr = &start_addr;
203 u8 **start_buff = &start_ptr;
204 u16 buff_len = 0;
205
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800206 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700207 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800208 return -1;
209 }
210
211 /* send an MPA Request frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530212 cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REQUEST);
213 form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800214
Faisal Latif9d5ab132009-03-06 15:15:01 -0800215 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
216}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800217
Faisal Latif9d5ab132009-03-06 15:15:01 -0800218
219
220static int send_mpa_reject(struct nes_cm_node *cm_node)
221{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530222 struct sk_buff *skb = NULL;
223 u8 start_addr = 0;
224 u8 *start_ptr = &start_addr;
225 u8 **start_buff = &start_ptr;
226 u16 buff_len = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800227
228 skb = dev_alloc_skb(MAX_CM_BUFFER);
229 if (!skb) {
230 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
231 return -ENOMEM;
232 }
233
234 /* send an MPA reject frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530235 cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REPLY);
236 form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK | SET_FIN);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800237
238 cm_node->state = NES_CM_STATE_FIN_WAIT1;
239 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800240}
241
242
243/**
244 * recv_mpa - process a received TCP pkt, we are expecting an
245 * IETF MPA frame
246 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800247static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530248 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800249{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530250 struct ietf_mpa_v1 *mpa_frame;
251 struct ietf_mpa_v2 *mpa_v2_frame;
252 struct ietf_rtr_msg *rtr_msg;
253 int mpa_hdr_len;
254 int priv_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800255
Faisal Latif9d5ab132009-03-06 15:15:01 -0800256 *type = NES_MPA_REQUEST_ACCEPT;
257
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800258 /* assume req frame is in tcp data payload */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530259 if (len < sizeof(struct ietf_mpa_v1)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800260 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800261 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800262 }
263
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530264 /* points to the beginning of the frame, which could be MPA V1 or V2 */
265 mpa_frame = (struct ietf_mpa_v1 *)buffer;
266 mpa_hdr_len = sizeof(struct ietf_mpa_v1);
267 priv_data_len = ntohs(mpa_frame->priv_data_len);
268
Faisal Latif1cf078c2009-12-09 15:53:54 -0800269 /* make sure mpa private data len is less than 512 bytes */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530270 if (priv_data_len > IETF_MAX_PRIV_DATA_LEN) {
Faisal Latif1cf078c2009-12-09 15:53:54 -0800271 nes_debug(NES_DBG_CM, "The received Length of Private"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530272 " Data field exceeds 512 octets\n");
Faisal Latif1cf078c2009-12-09 15:53:54 -0800273 return -EINVAL;
274 }
275 /*
276 * make sure MPA receiver interoperate with the
277 * received MPA version and MPA key information
278 *
279 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530280 if (mpa_frame->rev != IETF_MPA_V1 && mpa_frame->rev != IETF_MPA_V2) {
Faisal Latif1cf078c2009-12-09 15:53:54 -0800281 nes_debug(NES_DBG_CM, "The received mpa version"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530282 " is not supported\n");
Faisal Latif1cf078c2009-12-09 15:53:54 -0800283 return -EINVAL;
284 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530285 /*
286 * backwards compatibility only
287 */
288 if (mpa_frame->rev > cm_node->mpa_frame_rev) {
289 nes_debug(NES_DBG_CM, "The received mpa version"
290 " can not be interoperated\n");
291 return -EINVAL;
292 } else {
293 cm_node->mpa_frame_rev = mpa_frame->rev;
294 }
295
Faisal Latif1cf078c2009-12-09 15:53:54 -0800296 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
297 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
298 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
299 return -EINVAL;
300 }
301 } else {
302 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
303 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
304 return -EINVAL;
305 }
306 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800307
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530308
309 if (priv_data_len + mpa_hdr_len != len) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800310 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530311 " complete (%x + %x != %x)\n",
312 priv_data_len, mpa_hdr_len, len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800313 return -EINVAL;
314 }
315 /* make sure it does not exceed the max size */
316 if (len > MAX_CM_BUFFER) {
317 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530318 " (%x + %x != %x)\n",
319 priv_data_len, mpa_hdr_len, len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800320 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800321 }
322
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530323 cm_node->mpa_frame_size = priv_data_len;
324
325 switch (mpa_frame->rev) {
326 case IETF_MPA_V2: {
327 u16 ird_size;
328 u16 ord_size;
329 mpa_v2_frame = (struct ietf_mpa_v2 *)buffer;
330 mpa_hdr_len += IETF_RTR_MSG_SIZE;
331 cm_node->mpa_frame_size -= IETF_RTR_MSG_SIZE;
332 rtr_msg = &mpa_v2_frame->rtr_msg;
333
334 /* parse rtr message */
335 rtr_msg->ctrl_ird = ntohs(rtr_msg->ctrl_ird);
336 rtr_msg->ctrl_ord = ntohs(rtr_msg->ctrl_ord);
337 ird_size = rtr_msg->ctrl_ird & IETF_NO_IRD_ORD;
338 ord_size = rtr_msg->ctrl_ord & IETF_NO_IRD_ORD;
339
340 if (!(rtr_msg->ctrl_ird & IETF_PEER_TO_PEER)) {
341 /* send reset */
342 return -EINVAL;
343 }
344
345 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
346 /* responder */
347 if (cm_node->ord_size > ird_size)
348 cm_node->ord_size = ird_size;
349 } else {
350 /* initiator */
351 if (cm_node->ord_size > ird_size)
352 cm_node->ord_size = ird_size;
353
354 if (cm_node->ird_size < ord_size) {
355 /* no resources available */
356 /* send terminate message */
357 return -EINVAL;
358 }
359 }
360
361 if (rtr_msg->ctrl_ord & IETF_RDMA0_READ) {
362 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
363 } else if (rtr_msg->ctrl_ord & IETF_RDMA0_WRITE) {
364 cm_node->send_rdma0_op = SEND_RDMA_WRITE_ZERO;
365 } else { /* Not supported RDMA0 operation */
366 return -EINVAL;
367 }
368 break;
369 }
370 case IETF_MPA_V1:
371 default:
372 break;
373 }
374
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800375 /* copy entire MPA frame to our cm_node's frame */
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530376 memcpy(cm_node->mpa_frame_buf, buffer + mpa_hdr_len, cm_node->mpa_frame_size);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800377
Faisal Latif9d5ab132009-03-06 15:15:01 -0800378 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
379 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800380 return 0;
381}
382
383
384/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800385 * form_cm_frame - get a free packet and build empty frame Use
386 * node info to build.
387 */
Chien Tung6098d102008-11-21 20:51:01 -0600388static void form_cm_frame(struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530389 struct nes_cm_node *cm_node, void *options, u32 optionsize,
390 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800391{
392 struct tcphdr *tcph;
393 struct iphdr *iph;
394 struct ethhdr *ethh;
395 u8 *buf;
396 u16 packetsize = sizeof(*iph);
397
398 packetsize += sizeof(*tcph);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530399 packetsize += optionsize + datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800400
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530401 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800402 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
403
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800404 buf = skb_put(skb, packetsize + ETH_HLEN);
405
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530406 ethh = (struct ethhdr *)buf;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800407 buf += ETH_HLEN;
408
409 iph = (struct iphdr *)buf;
410 buf += sizeof(*iph);
411 tcph = (struct tcphdr *)buf;
412 skb_reset_mac_header(skb);
413 skb_set_network_header(skb, ETH_HLEN);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530414 skb_set_transport_header(skb, ETH_HLEN + sizeof(*iph));
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800415 buf += sizeof(*tcph);
416
417 skb->ip_summed = CHECKSUM_PARTIAL;
418 skb->protocol = htons(0x800);
419 skb->data_len = 0;
420 skb->mac_len = ETH_HLEN;
421
422 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
423 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
424 ethh->h_proto = htons(0x0800);
425
426 iph->version = IPVERSION;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530427 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800428 iph->tos = 0;
429 iph->tot_len = htons(packetsize);
430 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
431
432 iph->frag_off = htons(0x4000);
433 iph->ttl = 0x40;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530434 iph->protocol = 0x06; /* IPPROTO_TCP */
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800435
436 iph->saddr = htonl(cm_node->loc_addr);
437 iph->daddr = htonl(cm_node->rem_addr);
438
439 tcph->source = htons(cm_node->loc_port);
440 tcph->dest = htons(cm_node->rem_port);
441 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
442
443 if (flags & SET_ACK) {
444 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
445 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
446 tcph->ack = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530447 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800448 tcph->ack_seq = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530449 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800450
451 if (flags & SET_SYN) {
452 cm_node->tcp_cntxt.loc_seq_num++;
453 tcph->syn = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530454 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700455 cm_node->tcp_cntxt.loc_seq_num += datasize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530456 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800457
Faisal Latif6492cdf2008-07-24 20:50:45 -0700458 if (flags & SET_FIN) {
459 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800460 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700461 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800462
463 if (flags & SET_RST)
464 tcph->rst = 1;
465
466 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
467 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
468 tcph->urg_ptr = 0;
469 if (optionsize)
470 memcpy(buf, options, optionsize);
471 buf += optionsize;
472 if (datasize)
473 memcpy(buf, data, datasize);
474
475 skb_shinfo(skb)->nr_frags = 0;
476 cm_packets_created++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800477}
478
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800479/**
480 * print_core - dump a cm core
481 */
482static void print_core(struct nes_cm_core *core)
483{
484 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
485 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
486 if (!core)
487 return;
488 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800489
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530490 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800491
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800492 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
493 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
494
495 nes_debug(NES_DBG_CM, "core : %p \n", core);
496
497 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
498}
499
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530500/**
501 * cm_build_mpa_frame - build a MPA V1 frame or MPA V2 frame
502 */
503static int cm_build_mpa_frame(struct nes_cm_node *cm_node, u8 **start_buff,
504 u16 *buff_len, u8 *pci_mem, u8 mpa_key)
505{
506 int ret = 0;
507
508 *start_buff = (pci_mem) ? pci_mem : &cm_node->mpa_frame_buf[0];
509
510 switch (cm_node->mpa_frame_rev) {
511 case IETF_MPA_V1:
512 *start_buff = (u8 *)*start_buff + sizeof(struct ietf_rtr_msg);
513 *buff_len = sizeof(struct ietf_mpa_v1) + cm_node->mpa_frame_size;
514 build_mpa_v1(cm_node, *start_buff, mpa_key);
515 break;
516 case IETF_MPA_V2:
517 *buff_len = sizeof(struct ietf_mpa_v2) + cm_node->mpa_frame_size;
518 build_mpa_v2(cm_node, *start_buff, mpa_key);
519 break;
520 default:
521 ret = -EINVAL;
522 }
523 return ret;
524}
525
526/**
527 * build_mpa_v2 - build a MPA V2 frame
528 */
529static void build_mpa_v2(struct nes_cm_node *cm_node,
530 void *start_addr, u8 mpa_key)
531{
532 struct ietf_mpa_v2 *mpa_frame = (struct ietf_mpa_v2 *)start_addr;
533 struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
534
535 /* initialize the upper 5 bytes of the frame */
536 build_mpa_v1(cm_node, start_addr, mpa_key);
537 mpa_frame->flags |= IETF_MPA_V2_FLAG; /* set a bit to indicate MPA V2 */
538 mpa_frame->priv_data_len += htons(IETF_RTR_MSG_SIZE);
539
540 /* initialize RTR msg */
541 rtr_msg->ctrl_ird = (cm_node->ird_size > IETF_NO_IRD_ORD) ?
542 IETF_NO_IRD_ORD : cm_node->ird_size;
543 rtr_msg->ctrl_ord = (cm_node->ord_size > IETF_NO_IRD_ORD) ?
544 IETF_NO_IRD_ORD : cm_node->ord_size;
545
546 rtr_msg->ctrl_ird |= IETF_PEER_TO_PEER;
547 rtr_msg->ctrl_ird |= IETF_FLPDU_ZERO_LEN;
548
549 switch (mpa_key) {
550 case MPA_KEY_REQUEST:
551 rtr_msg->ctrl_ord |= IETF_RDMA0_WRITE;
552 rtr_msg->ctrl_ord |= IETF_RDMA0_READ;
553 break;
554 case MPA_KEY_REPLY:
555 switch (cm_node->send_rdma0_op) {
556 case SEND_RDMA_WRITE_ZERO:
557 rtr_msg->ctrl_ord |= IETF_RDMA0_WRITE;
558 break;
559 case SEND_RDMA_READ_ZERO:
560 rtr_msg->ctrl_ord |= IETF_RDMA0_READ;
561 break;
562 }
563 }
564 rtr_msg->ctrl_ird = htons(rtr_msg->ctrl_ird);
565 rtr_msg->ctrl_ord = htons(rtr_msg->ctrl_ord);
566}
567
568/**
569 * build_mpa_v1 - build a MPA V1 frame
570 */
571static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
572{
573 struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
574
575 switch (mpa_key) {
576 case MPA_KEY_REQUEST:
577 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
578 break;
579 case MPA_KEY_REPLY:
580 memcpy(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
581 break;
582 }
583 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
584 mpa_frame->rev = cm_node->mpa_frame_rev;
585 mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
586}
587
588static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
589{
590 u64 u64temp;
591 struct nes_qp *nesqp = *nesqp_addr;
592 struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
593
594 u64temp = (unsigned long)nesqp;
595 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
596 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
597
598 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
599 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
600
601 switch (cm_node->send_rdma0_op) {
602 case SEND_RDMA_WRITE_ZERO:
603 nes_debug(NES_DBG_CM, "Sending first write.\n");
604 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
605 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
606 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
607 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
608 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
609 break;
610
611 case SEND_RDMA_READ_ZERO:
612 default:
613 if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO) {
614 printk(KERN_ERR "%s[%u]: Unsupported RDMA0 len operation=%u\n",
615 __func__, __LINE__, cm_node->send_rdma0_op);
616 WARN_ON(1);
617 }
618 nes_debug(NES_DBG_CM, "Sending first rdma operation.\n");
619 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
620 cpu_to_le32(NES_IWARP_SQ_OP_RDMAR);
621 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX] = 1;
622 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_TO_HIGH_IDX] = 0;
623 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] = 0;
624 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_STAG_IDX] = 1;
625 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 1;
626 break;
627 }
628
629 if (nesqp->sq_kmapped) {
630 nesqp->sq_kmapped = 0;
631 kunmap(nesqp->page);
632 }
633
634 /*use the reserved spot on the WQ for the extra first WQE*/
635 nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
636 NES_QPCONTEXT_ORDIRD_WRPDU |
637 NES_QPCONTEXT_ORDIRD_ALSMM));
638 nesqp->skip_lsmm = 1;
639 nesqp->hwqp.sq_tail = 0;
640}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800641
642/**
643 * schedule_nes_timer
644 * note - cm_node needs to be protected before calling this. Encase in:
645 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
646 */
647int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530648 enum nes_timer_type type, int send_retrans,
649 int close_when_complete)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800650{
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530651 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700652 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800653 struct nes_timer_entry *new_send;
654 int ret = 0;
655 u32 was_timer_set;
656
657 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
658 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800659 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800660
661 /* new_send->timetosend = currenttime */
662 new_send->retrycount = NES_DEFAULT_RETRYS;
663 new_send->retranscount = NES_DEFAULT_RETRANS;
664 new_send->skb = skb;
665 new_send->timetosend = jiffies;
666 new_send->type = type;
667 new_send->netdev = cm_node->netdev;
668 new_send->send_retrans = send_retrans;
669 new_send->close_when_complete = close_when_complete;
670
671 if (type == NES_TIMER_TYPE_CLOSE) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530672 new_send->timetosend += (HZ / 10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800673 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700674 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800675 WARN_ON(1);
676 return -EINVAL;
677 }
678 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800679 }
680
681 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700682 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800683 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700684 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
685 cm_node->send_entry = new_send;
686 add_ref_cm_node(cm_node);
687 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
688 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800689
690 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
691 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700692 nes_debug(NES_DBG_CM, "Error sending packet %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530693 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800694 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700695 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800696 } else {
697 cm_packets_sent++;
698 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700699 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800700 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700701 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800702 return ret;
703 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800704 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800705 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800706
707 was_timer_set = timer_pending(&cm_core->tcp_timer);
708
709 if (!was_timer_set) {
710 cm_core->tcp_timer.expires = new_send->timetosend;
711 add_timer(&cm_core->tcp_timer);
712 }
713
714 return ret;
715}
716
Faisal Latif9d5ab132009-03-06 15:15:01 -0800717static void nes_retrans_expired(struct nes_cm_node *cm_node)
718{
Faisal Latif68237a02009-06-22 22:53:28 -0700719 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latifdae58722010-08-14 21:04:56 +0000720 enum nes_cm_node_state state = cm_node->state;
721 cm_node->state = NES_CM_STATE_CLOSED;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530722
Faisal Latifdae58722010-08-14 21:04:56 +0000723 switch (state) {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800724 case NES_CM_STATE_SYN_RCVD:
725 case NES_CM_STATE_CLOSING:
726 rem_ref_cm_node(cm_node->cm_core, cm_node);
727 break;
728 case NES_CM_STATE_LAST_ACK:
729 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700730 if (cm_node->cm_id)
731 cm_id->rem_ref(cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800732 send_reset(cm_node, NULL);
733 break;
734 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800735 add_ref_cm_node(cm_node);
736 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800737 create_event(cm_node, NES_CM_EVENT_ABORTED);
738 }
739}
740
741static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
742{
743 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
744 struct iw_cm_id *cm_id = cm_node->cm_id;
745 struct nes_qp *nesqp;
746 unsigned long qplockflags;
747
748 if (!recv_entry)
749 return;
750 nesqp = (struct nes_qp *)recv_entry->skb;
751 if (nesqp) {
752 spin_lock_irqsave(&nesqp->lock, qplockflags);
753 if (nesqp->cm_id) {
754 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530755 "refcount = %d: HIT A "
756 "NES_TIMER_TYPE_CLOSE with something "
757 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
758 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800759 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
760 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
761 nesqp->ibqp_state = IB_QPS_ERR;
762 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
763 nes_cm_disconn(nesqp);
764 } else {
765 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
766 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530767 "refcount = %d: HIT A "
768 "NES_TIMER_TYPE_CLOSE with nothing "
769 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
770 atomic_read(&nesqp->refcount));
Faisal Latif9d5ab132009-03-06 15:15:01 -0800771 }
772 } else if (rem_node) {
773 /* TIME_WAIT state */
774 rem_ref_cm_node(cm_node->cm_core, cm_node);
775 }
776 if (cm_node->cm_id)
777 cm_id->rem_ref(cm_id);
778 kfree(recv_entry);
779 cm_node->recv_entry = NULL;
780}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800781
782/**
783 * nes_cm_timer_tick
784 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700785static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800786{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800787 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800788 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800789 struct nes_cm_node *cm_node;
790 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800791 struct list_head *list_core_temp;
792 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800793 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800794 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700795 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800796 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800797
Faisal Latif879e5bd2008-11-21 20:50:41 -0600798 struct list_head timer_list;
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530799
Faisal Latif879e5bd2008-11-21 20:50:41 -0600800 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800801 spin_lock_irqsave(&cm_core->ht_lock, flags);
802
Faisal Latif6492cdf2008-07-24 20:50:45 -0700803 list_for_each_safe(list_node, list_core_temp,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530804 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800805 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800806 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600807 add_ref_cm_node(cm_node);
808 list_add(&cm_node->timer_entry, &timer_list);
809 }
810 }
811 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
812
813 list_for_each_safe(list_node, list_core_temp, &timer_list) {
814 cm_node = container_of(list_node, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530815 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800816 recv_entry = cm_node->recv_entry;
817
818 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700819 if (time_after(recv_entry->timetosend, jiffies)) {
820 if (nexttimeout > recv_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530821 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800822 nexttimeout = recv_entry->timetosend;
823 settimer = 1;
824 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530825 } else {
Faisal Latif9d5ab132009-03-06 15:15:01 -0800826 handle_recv_entry(cm_node, 1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530827 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800828 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800829
830 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700831 do {
832 send_entry = cm_node->send_entry;
833 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600834 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800835 if (time_after(send_entry->timetosend, jiffies)) {
836 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700837 if ((nexttimeout >
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530838 send_entry->timetosend) ||
839 !settimer) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700840 nexttimeout =
841 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800842 settimer = 1;
843 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800844 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700845 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800846 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800847 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800848 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700849
850 if ((cm_node->state == NES_CM_STATE_TSA) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530851 (cm_node->state == NES_CM_STATE_CLOSED)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700852 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600853 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800854 }
855
Faisal Latif6492cdf2008-07-24 20:50:45 -0700856 if (!send_entry->retranscount ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530857 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800858 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700859 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800860
Faisal Latif6492cdf2008-07-24 20:50:45 -0700861 spin_unlock_irqrestore(
862 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800863 nes_retrans_expired(cm_node);
864 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700865 spin_lock_irqsave(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530866 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600867 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800868 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800869 atomic_inc(&send_entry->skb->users);
870 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700871 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530872 "for node %p, jiffies = %lu, time to send = "
873 "%lu, retranscount = %u, send_entry->seq_num = "
874 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
875 "0x%08X\n", send_entry, cm_node, jiffies,
876 send_entry->timetosend,
877 send_entry->retranscount,
878 send_entry->seq_num,
879 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800880
Faisal Latif6492cdf2008-07-24 20:50:45 -0700881 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530882 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800883 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700884 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800885 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700886 nes_debug(NES_DBG_CM, "rexmit failed for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530887 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800888 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800889 send_entry->retrycount--;
890 nexttimeout = jiffies + NES_SHORT_TIME;
891 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600892 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800893 } else {
894 cm_packets_sent++;
895 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700896 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530897 "%u, retry count = %u.\n",
898 send_entry->retranscount,
899 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800900 if (send_entry->send_retrans) {
901 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700902 timetosend = (NES_RETRY_TIMEOUT <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530903 (NES_DEFAULT_RETRANS - send_entry->retranscount));
Faisal Latif4e9c3902009-04-27 13:39:36 -0700904
Faisal Latif6492cdf2008-07-24 20:50:45 -0700905 send_entry->timetosend = jiffies +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530906 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700907 if (nexttimeout > send_entry->timetosend ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530908 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800909 nexttimeout = send_entry->timetosend;
910 settimer = 1;
911 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800912 } else {
913 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700914 close_when_complete =
915 send_entry->close_when_complete;
916 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530917 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700918 free_retrans_entry(cm_node);
919 if (close_when_complete)
920 rem_ref_cm_node(cm_node->cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530921 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800922 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700923 } while (0);
924
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800925 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700926 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800927 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800928
929 if (settimer) {
930 if (!timer_pending(&cm_core->tcp_timer)) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530931 cm_core->tcp_timer.expires = nexttimeout;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800932 add_timer(&cm_core->tcp_timer);
933 }
934 }
935}
936
937
938/**
939 * send_syn
940 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700941static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530942 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800943{
944 int ret;
945 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800946 char optionsbuffer[sizeof(struct option_mss) +
Tatyana Nikolova615eb712011-09-25 20:17:46 +0530947 sizeof(struct option_windowscale) + sizeof(struct option_base) +
948 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800949
950 int optionssize = 0;
951 /* Sending MSS option */
952 union all_known_options *options;
953
954 if (!cm_node)
955 return -EINVAL;
956
957 options = (union all_known_options *)&optionsbuffer[optionssize];
958 options->as_mss.optionnum = OPTION_NUMBER_MSS;
959 options->as_mss.length = sizeof(struct option_mss);
960 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
961 optionssize += sizeof(struct option_mss);
962
963 options = (union all_known_options *)&optionsbuffer[optionssize];
964 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
965 options->as_windowscale.length = sizeof(struct option_windowscale);
966 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
967 optionssize += sizeof(struct option_windowscale);
968
Faisal Latif6492cdf2008-07-24 20:50:45 -0700969 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800970 options = (union all_known_options *)&optionsbuffer[optionssize];
971 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
972 options->as_base.length = sizeof(struct option_base);
973 optionssize += sizeof(struct option_base);
974 /* we need the size to be a multiple of 4 */
975 options = (union all_known_options *)&optionsbuffer[optionssize];
976 options->as_end = 1;
977 optionssize += 1;
978 options = (union all_known_options *)&optionsbuffer[optionssize];
979 options->as_end = 1;
980 optionssize += 1;
981 }
982
983 options = (union all_known_options *)&optionsbuffer[optionssize];
984 options->as_end = OPTION_NUMBER_END;
985 optionssize += 1;
986
Faisal Latif6492cdf2008-07-24 20:50:45 -0700987 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800988 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800989 if (!skb) {
990 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
991 return -1;
992 }
993
994 if (sendack)
995 flags |= SET_ACK;
996
997 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
998 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
999
1000 return ret;
1001}
1002
1003
1004/**
1005 * send_reset
1006 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001007static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001008{
1009 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001010 int flags = SET_RST | SET_ACK;
1011
Faisal Latif6492cdf2008-07-24 20:50:45 -07001012 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001013 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001014 if (!skb) {
1015 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001016 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001017 }
1018
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001019 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
1020 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
1021
1022 return ret;
1023}
1024
1025
1026/**
1027 * send_ack
1028 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001029static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001030{
1031 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001032
1033 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001034 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001035
1036 if (!skb) {
1037 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1038 return -1;
1039 }
1040
1041 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
1042 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
1043
1044 return ret;
1045}
1046
1047
1048/**
1049 * send_fin
1050 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07001051static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001052{
1053 int ret;
1054
1055 /* if we didn't get a frame get one */
1056 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -08001057 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001058
1059 if (!skb) {
1060 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1061 return -1;
1062 }
1063
1064 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
1065 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1066
1067 return ret;
1068}
1069
1070
1071/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001072 * find_node - find a cm node that matches the reference cm node
1073 */
1074static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301075 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001076{
1077 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001078 struct list_head *hte;
1079 struct nes_cm_node *cm_node;
1080
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001081 /* get a handle on the hte */
1082 hte = &cm_core->connected_nodes;
1083
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001084 /* walk list and find cm_node associated with this session ID */
1085 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001086 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001087 /* compare quad, return node handle if a match */
1088 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301089 cm_node->loc_addr, cm_node->loc_port,
1090 loc_addr, loc_port,
1091 cm_node->rem_addr, cm_node->rem_port,
1092 rem_addr, rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001093 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301094 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001095 add_ref_cm_node(cm_node);
1096 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1097 return cm_node;
1098 }
1099 }
1100 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1101
1102 /* no owner node */
1103 return NULL;
1104}
1105
1106
1107/**
1108 * find_listener - find a cm node listening on this addr-port pair
1109 */
1110static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301111 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001112{
1113 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001114 struct nes_cm_listener *listen_node;
1115
1116 /* walk list and find cm_node associated with this session ID */
1117 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -07001118 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001119 /* compare node pair, return node handle if a match */
1120 if (((listen_node->loc_addr == dst_addr) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301121 listen_node->loc_addr == 0x00000000) &&
1122 (listen_node->loc_port == dst_port) &&
1123 (listener_state & listen_node->listener_state)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001124 atomic_inc(&listen_node->ref_count);
1125 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1126 return listen_node;
1127 }
1128 }
1129 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1130
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001131 /* no listener */
1132 return NULL;
1133}
1134
1135
1136/**
1137 * add_hte_node - add a cm node to the hash table
1138 */
1139static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1140{
1141 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001142 struct list_head *hte;
1143
1144 if (!cm_node || !cm_core)
1145 return -EINVAL;
1146
Faisal Latif6492cdf2008-07-24 20:50:45 -07001147 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301148 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001149
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001150 spin_lock_irqsave(&cm_core->ht_lock, flags);
1151
1152 /* get a handle on the hash table element (list head for this slot) */
1153 hte = &cm_core->connected_nodes;
1154 list_add_tail(&cm_node->list, hte);
1155 atomic_inc(&cm_core->ht_node_cnt);
1156
1157 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1158
1159 return 0;
1160}
1161
1162
1163/**
1164 * mini_cm_dec_refcnt_listen
1165 */
1166static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301167 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001168{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001169 int ret = -EINVAL;
1170 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001171 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001172 struct list_head *list_pos = NULL;
1173 struct list_head *list_temp = NULL;
1174 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -06001175 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001176
1177 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301178 "refcnt=%d\n", listener, free_hanging_nodes,
1179 atomic_read(&listener->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001180 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -06001181 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001182 if (free_hanging_nodes) {
1183 spin_lock_irqsave(&cm_core->ht_lock, flags);
1184 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -06001185 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001186 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301187 list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001188 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -06001189 (!cm_node->accelerated)) {
1190 add_ref_cm_node(cm_node);
1191 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001192 }
1193 }
1194 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1195 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001196
1197 list_for_each_safe(list_pos, list_temp, &reset_list) {
1198 cm_node = container_of(list_pos, struct nes_cm_node,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301199 reset_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001200 {
1201 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -08001202 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001203 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
1204 rem_ref_cm_node(cm_node->cm_core, cm_node);
1205 } else {
1206 if (!loopback) {
1207 cleanup_retrans_entry(cm_node);
1208 err = send_reset(cm_node, NULL);
1209 if (err) {
1210 cm_node->state =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301211 NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001212 WARN_ON(1);
1213 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08001214 old_state = cm_node->state;
1215 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
1216 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1217 rem_ref_cm_node(
1218 cm_node->cm_core,
1219 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001220 }
1221 } else {
1222 struct nes_cm_event event;
1223
1224 event.cm_node = loopback;
1225 event.cm_info.rem_addr =
1226 loopback->rem_addr;
1227 event.cm_info.loc_addr =
1228 loopback->loc_addr;
1229 event.cm_info.rem_port =
1230 loopback->rem_port;
1231 event.cm_info.loc_port =
1232 loopback->loc_port;
1233 event.cm_info.cm_id = loopback->cm_id;
Faisal Latif43093b92010-02-12 19:57:04 +00001234 add_ref_cm_node(loopback);
1235 loopback->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001236 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001237 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001238
Faisal Latif9d5ab132009-03-06 15:15:01 -08001239 rem_ref_cm_node(cm_node->cm_core,
1240 cm_node);
1241
1242 }
1243 }
1244 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001245 }
1246
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001247 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1248 if (!atomic_dec_return(&listener->ref_count)) {
1249 list_del(&listener->list);
1250
1251 /* decrement our listen node count */
1252 atomic_dec(&cm_core->listen_node_cnt);
1253
1254 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1255
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301256 if (listener->nesvnic)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001257 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301258 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001259
1260 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1261
1262 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001263 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001264 ret = 0;
Faisal Latif6e10d2e2010-02-12 19:55:03 +00001265 atomic_inc(&cm_listens_destroyed);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001266 } else {
1267 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1268 }
1269 if (listener) {
1270 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1271 nes_debug(NES_DBG_CM, "destroying listener (%p)"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301272 " with non-zero pending accepts=%u\n",
1273 listener, atomic_read(&listener->pend_accepts_cnt));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001274 }
1275
1276 return ret;
1277}
1278
1279
1280/**
1281 * mini_cm_del_listen
1282 */
1283static int mini_cm_del_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301284 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001285{
1286 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1287 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1288 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1289}
1290
1291
1292/**
1293 * mini_cm_accelerated
1294 */
1295static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301296 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001297{
1298 u32 was_timer_set;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301299
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001300 cm_node->accelerated = 1;
1301
1302 if (cm_node->accept_pend) {
1303 BUG_ON(!cm_node->listener);
1304 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001305 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001306 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1307 }
1308
1309 was_timer_set = timer_pending(&cm_core->tcp_timer);
1310 if (!was_timer_set) {
1311 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1312 add_timer(&cm_core->tcp_timer);
1313 }
1314
1315 return 0;
1316}
1317
1318
1319/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001320 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001321 */
Faisal Latif7a576df2009-12-09 15:54:33 -08001322static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001323{
1324 struct rtable *rt;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001325 struct neighbour *neigh;
Faisal Latif7a576df2009-12-09 15:54:33 -08001326 int rc = arpindex;
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001327 struct net_device *netdev;
Faisal Latif7a576df2009-12-09 15:54:33 -08001328 struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001329
David S. Miller78fbfd82011-03-12 00:00:52 -05001330 rt = ip_route_output(&init_net, htonl(dst_ip), 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001331 if (IS_ERR(rt)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001332 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301333 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001334 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001335 }
1336
Roland Dreiercf55bb22011-03-24 17:13:20 -07001337 if (netif_is_bond_slave(nesvnic->netdev))
Maciej Sosnowski2a4c97e2010-11-24 17:29:30 +00001338 netdev = nesvnic->netdev->master;
1339 else
1340 netdev = nesvnic->netdev;
1341
1342 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, netdev);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001343 if (neigh) {
1344 if (neigh->nud_state & NUD_VALID) {
1345 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001346 " is %pM, Gateway is 0x%08X \n", dst_ip,
1347 neigh->ha, ntohl(rt->rt_gateway));
Faisal Latif7a576df2009-12-09 15:54:33 -08001348
1349 if (arpindex >= 0) {
1350 if (!memcmp(nesadapter->arp_table[arpindex].mac_addr,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301351 neigh->ha, ETH_ALEN)) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001352 /* Mac address same as in nes_arp_table */
1353 neigh_release(neigh);
1354 ip_rt_put(rt);
1355 return rc;
1356 }
1357
1358 nes_manage_arp_cache(nesvnic->netdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301359 nesadapter->arp_table[arpindex].mac_addr,
1360 dst_ip, NES_ARP_DELETE);
Faisal Latif7a576df2009-12-09 15:54:33 -08001361 }
1362
Bob Sharp7191a0a2008-10-03 12:21:19 -07001363 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1364 dst_ip, NES_ARP_ADD);
1365 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1366 NES_ARP_RESOLVE);
1367 }
1368 neigh_release(neigh);
1369 }
1370
1371 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
David S. Miller69cce1d2011-07-17 23:09:49 -07001372 neigh_event_send(dst_get_neighbour(&rt->dst), NULL);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001373
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001374 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001375 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001376}
1377
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001378/**
1379 * make_cm_node - create a new instance of a cm node
1380 */
1381static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301382 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1383 struct nes_cm_listener *listener)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001384{
1385 struct nes_cm_node *cm_node;
1386 struct timespec ts;
Faisal Latif7a576df2009-12-09 15:54:33 -08001387 int oldarpindex = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001388 int arpindex = 0;
1389 struct nes_device *nesdev;
1390 struct nes_adapter *nesadapter;
1391
1392 /* create an hte and cm_node for this instance */
1393 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1394 if (!cm_node)
1395 return NULL;
1396
1397 /* set our node specific transport info */
1398 cm_node->loc_addr = cm_info->loc_addr;
1399 cm_node->rem_addr = cm_info->rem_addr;
1400 cm_node->loc_port = cm_info->loc_port;
1401 cm_node->rem_port = cm_info->rem_port;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301402
1403 cm_node->mpa_frame_rev = mpa_version;
1404 cm_node->send_rdma0_op = SEND_RDMA_READ_ZERO;
1405 cm_node->ird_size = IETF_NO_IRD_ORD;
1406 cm_node->ord_size = IETF_NO_IRD_ORD;
1407
Harvey Harrison63779432008-10-31 00:56:00 -07001408 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1409 &cm_node->loc_addr, cm_node->loc_port,
1410 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001411 cm_node->listener = listener;
1412 cm_node->netdev = nesvnic->netdev;
1413 cm_node->cm_id = cm_info->cm_id;
1414 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1415
Faisal Latif6492cdf2008-07-24 20:50:45 -07001416 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301417 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001418
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001419 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001420
1421 cm_node->loopbackpartner = NULL;
1422 atomic_set(&cm_node->ref_count, 1);
1423 /* associate our parent CM core */
1424 cm_node->cm_core = cm_core;
1425 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1426 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1427 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301428 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001429 ts = current_kernel_time();
1430 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1431 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301432 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001433 cm_node->tcp_cntxt.rcv_nxt = 0;
1434 /* get a unique session ID , add thread_id to an upcounter to handle race */
1435 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001436 cm_node->conn_type = cm_info->conn_type;
1437 cm_node->apbvt_set = 0;
1438 cm_node->accept_pend = 0;
1439
1440 cm_node->nesvnic = nesvnic;
1441 /* get some device handles, for arp lookup */
1442 nesdev = nesvnic->nesdev;
1443 nesadapter = nesdev->nesadapter;
1444
1445 cm_node->loopbackpartner = NULL;
Faisal Latif7a576df2009-12-09 15:54:33 -08001446
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001447 /* get the mac addr for the remote node */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301448 if (ipv4_is_loopback(htonl(cm_node->rem_addr))) {
Chien Tung1ee86552008-11-21 20:51:04 -06001449 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301450 } else {
Faisal Latif7a576df2009-12-09 15:54:33 -08001451 oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1452 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);
Faisal Latif7a576df2009-12-09 15:54:33 -08001453 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001454 if (arpindex < 0) {
Faisal Latif7a576df2009-12-09 15:54:33 -08001455 kfree(cm_node);
1456 return NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001457 }
1458
1459 /* copy the mac addr to node context */
1460 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001461 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1462 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001463
1464 add_hte_node(cm_core, cm_node);
1465 atomic_inc(&cm_nodes_created);
1466
1467 return cm_node;
1468}
1469
1470
1471/**
1472 * add_ref_cm_node - destroy an instance of a cm node
1473 */
1474static int add_ref_cm_node(struct nes_cm_node *cm_node)
1475{
1476 atomic_inc(&cm_node->ref_count);
1477 return 0;
1478}
1479
1480
1481/**
1482 * rem_ref_cm_node - destroy an instance of a cm node
1483 */
1484static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301485 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001486{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001487 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001488 struct nes_qp *nesqp;
1489
1490 if (!cm_node)
1491 return -EINVAL;
1492
1493 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1494 if (atomic_dec_return(&cm_node->ref_count)) {
1495 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1496 return 0;
1497 }
1498 list_del(&cm_node->list);
1499 atomic_dec(&cm_core->ht_node_cnt);
1500 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1501
1502 /* if the node is destroyed before connection was accelerated */
1503 if (!cm_node->accelerated && cm_node->accept_pend) {
1504 BUG_ON(!cm_node->listener);
1505 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1506 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1507 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001508 WARN_ON(cm_node->send_entry);
1509 if (cm_node->recv_entry)
1510 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001511 if (cm_node->listener) {
1512 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1513 } else {
1514 if (cm_node->apbvt_set && cm_node->nesvnic) {
1515 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301516 PCI_FUNC(
1517 cm_node->nesvnic->nesdev->pcidev->devfn),
1518 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001519 }
1520 }
1521
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001522 atomic_dec(&cm_core->node_cnt);
1523 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001524 nesqp = cm_node->nesqp;
1525 if (nesqp) {
1526 nesqp->cm_node = NULL;
1527 nes_rem_ref(&nesqp->ibqp);
1528 cm_node->nesqp = NULL;
1529 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001530
Faisal Latif6492cdf2008-07-24 20:50:45 -07001531 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001532 return 0;
1533}
1534
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001535/**
1536 * process_options
1537 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001538static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301539 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001540{
1541 u32 tmp;
1542 u32 offset = 0;
1543 union all_known_options *all_options;
1544 char got_mss_option = 0;
1545
1546 while (offset < optionsize) {
1547 all_options = (union all_known_options *)(optionsloc + offset);
1548 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001549 case OPTION_NUMBER_END:
1550 offset = optionsize;
1551 break;
1552 case OPTION_NUMBER_NONE:
1553 offset += 1;
1554 continue;
1555 case OPTION_NUMBER_MSS:
1556 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301557 "Size: %d\n", __func__,
1558 all_options->as_mss.length, offset, optionsize);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001559 got_mss_option = 1;
1560 if (all_options->as_mss.length != 4) {
1561 return 1;
1562 } else {
1563 tmp = ntohs(all_options->as_mss.mss);
1564 if (tmp > 0 && tmp <
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301565 cm_node->tcp_cntxt.mss)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001566 cm_node->tcp_cntxt.mss = tmp;
1567 }
1568 break;
1569 case OPTION_NUMBER_WINDOW_SCALE:
1570 cm_node->tcp_cntxt.snd_wscale =
1571 all_options->as_windowscale.shiftcount;
1572 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001573 default:
1574 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301575 all_options->as_base.optionnum);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001576 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001577 }
1578 offset += all_options->as_base.length;
1579 }
1580 if ((!got_mss_option) && (syn_packet))
1581 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1582 return 0;
1583}
1584
Faisal Latif6492cdf2008-07-24 20:50:45 -07001585static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001586{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001587 atomic_inc(&cm_accel_dropped_pkts);
1588 dev_kfree_skb_any(skb);
1589}
1590
Faisal Latif9d5ab132009-03-06 15:15:01 -08001591static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001592{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001593 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301594 "refcnt=%d\n", cm_node, cm_node->state,
1595 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001596 switch (cm_node->state) {
1597 case NES_CM_STATE_SYN_RCVD:
1598 case NES_CM_STATE_SYN_SENT:
1599 case NES_CM_STATE_ESTABLISHED:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001600 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001601 cm_node->tcp_cntxt.rcv_nxt++;
1602 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001603 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001604 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001605 break;
Faisal Latifb1190d32009-12-09 15:54:32 -08001606 case NES_CM_STATE_MPAREQ_SENT:
1607 create_event(cm_node, NES_CM_EVENT_ABORTED);
1608 cm_node->tcp_cntxt.rcv_nxt++;
1609 cleanup_retrans_entry(cm_node);
1610 cm_node->state = NES_CM_STATE_CLOSED;
1611 add_ref_cm_node(cm_node);
1612 send_reset(cm_node, NULL);
1613 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001614 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001615 cm_node->tcp_cntxt.rcv_nxt++;
1616 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001617 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001618 send_ack(cm_node, NULL);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001619 /* Wait for ACK as this is simultaneous close..
Faisal Latif9d5ab132009-03-06 15:15:01 -08001620 * After we receive ACK, do not send anything..
1621 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001622 break;
1623 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001624 cm_node->tcp_cntxt.rcv_nxt++;
1625 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001626 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001627 send_ack(cm_node, NULL);
1628 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1629 break;
1630 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001631 cm_node->tcp_cntxt.rcv_nxt++;
1632 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001633 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001634 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001635 break;
1636 case NES_CM_STATE_TSA:
1637 default:
1638 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1639 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001640 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001641 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001642}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001643
Faisal Latif6492cdf2008-07-24 20:50:45 -07001644
1645static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1646 struct tcphdr *tcph)
1647{
1648
1649 int reset = 0; /* whether to send reset in case of err.. */
1650 atomic_inc(&cm_resets_recvd);
1651 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1652 " refcnt=%d\n", cm_node, cm_node->state,
1653 atomic_read(&cm_node->ref_count));
1654 cleanup_retrans_entry(cm_node);
1655 switch (cm_node->state) {
1656 case NES_CM_STATE_SYN_SENT:
1657 case NES_CM_STATE_MPAREQ_SENT:
1658 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1659 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1660 cm_node->listener, cm_node->state);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301661 switch (cm_node->mpa_frame_rev) {
1662 case IETF_MPA_V2:
1663 cm_node->mpa_frame_rev = IETF_MPA_V1;
1664 /* send a syn and goto syn sent state */
1665 cm_node->state = NES_CM_STATE_SYN_SENT;
1666 if (send_syn(cm_node, 0, NULL)) {
1667 active_open_err(cm_node, skb, reset);
1668 }
1669 break;
1670 case IETF_MPA_V1:
1671 default:
1672 active_open_err(cm_node, skb, reset);
1673 break;
1674 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001675 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001676 case NES_CM_STATE_MPAREQ_RCVD:
Dan Carpenter4d8d6382010-09-15 18:32:39 +02001677 atomic_inc(&cm_node->passive_state);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001678 dev_kfree_skb_any(skb);
1679 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001680 case NES_CM_STATE_ESTABLISHED:
1681 case NES_CM_STATE_SYN_RCVD:
1682 case NES_CM_STATE_LISTENING:
1683 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1684 passive_open_err(cm_node, skb, reset);
1685 break;
1686 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001687 active_open_err(cm_node, skb, reset);
1688 break;
1689 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001690 drop_packet(skb);
1691 break;
Faisal Latifdae58722010-08-14 21:04:56 +00001692 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif68237a02009-06-22 22:53:28 -07001693 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001694 case NES_CM_STATE_LAST_ACK:
1695 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001696 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001697 cm_node->state = NES_CM_STATE_CLOSED;
1698 rem_ref_cm_node(cm_node->cm_core, cm_node);
1699 drop_packet(skb);
1700 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001701 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001702 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001703 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001704 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001705}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001706
Faisal Latif9d5ab132009-03-06 15:15:01 -08001707
1708static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001709{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301710 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001711 int datasize = skb->len;
1712 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001713
1714 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301715 u32 res_type;
1716
Faisal Latif9d5ab132009-03-06 15:15:01 -08001717 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1718 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001719 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001720 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001721 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301722 "cm_node=%p listener=%p state=%d\n", __func__,
1723 __LINE__, cm_node, cm_node->listener,
1724 cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001725 active_open_err(cm_node, skb, 1);
1726 } else {
1727 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001728 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001729 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001730 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001731
1732 switch (cm_node->state) {
1733 case NES_CM_STATE_ESTABLISHED:
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301734 if (res_type == NES_MPA_REQUEST_REJECT)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001735 /*BIG problem as we are receiving the MPA.. So should
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301736 * not be REJECT.. This is Passive Open.. We can
1737 * only receive it Reject for Active Open...*/
Faisal Latif9d5ab132009-03-06 15:15:01 -08001738 WARN_ON(1);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001739 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1740 type = NES_CM_EVENT_MPA_REQ;
1741 atomic_set(&cm_node->passive_state,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301742 NES_PASSIVE_STATE_INDICATED);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001743 break;
1744 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001745 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001746 if (res_type == NES_MPA_REQUEST_REJECT) {
1747 type = NES_CM_EVENT_MPA_REJECT;
1748 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1749 } else {
1750 type = NES_CM_EVENT_CONNECTED;
1751 cm_node->state = NES_CM_STATE_TSA;
1752 }
1753
1754 break;
1755 default:
1756 WARN_ON(1);
1757 break;
1758 }
1759 dev_kfree_skb_any(skb);
1760 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001761}
1762
1763static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1764{
1765 switch (cm_node->state) {
1766 case NES_CM_STATE_SYN_SENT:
1767 case NES_CM_STATE_MPAREQ_SENT:
1768 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301769 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1770 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001771 active_open_err(cm_node, skb, 1);
1772 break;
1773 case NES_CM_STATE_ESTABLISHED:
1774 case NES_CM_STATE_SYN_RCVD:
1775 passive_open_err(cm_node, skb, 1);
1776 break;
1777 case NES_CM_STATE_TSA:
1778 default:
1779 drop_packet(skb);
1780 }
1781}
1782
1783static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301784 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001785{
1786 int err;
1787
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301788 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001789 if (err)
1790 active_open_err(cm_node, skb, 1);
1791
1792 return err;
1793}
1794
1795static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301796 struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001797{
1798 int err = 0;
1799 u32 seq;
1800 u32 ack_seq;
1801 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1802 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1803 u32 rcv_wnd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301804
Faisal Latif6492cdf2008-07-24 20:50:45 -07001805 seq = ntohl(tcph->seq);
1806 ack_seq = ntohl(tcph->ack_seq);
1807 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1808 if (ack_seq != loc_seq_num)
1809 err = 1;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301810 else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001811 err = 1;
1812 if (err) {
1813 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301814 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1815 cm_node->listener, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001816 indicate_pkt_err(cm_node, skb);
1817 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301818 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1819 rcv_wnd);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001820 }
1821 return err;
1822}
1823
1824/*
1825 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1826 * is created with a listener or it may comein as rexmitted packet which in
1827 * that case will be just dropped.
1828 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001829static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301830 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001831{
1832 int ret;
1833 u32 inc_sequence;
1834 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001835
1836 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001837 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001838 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001839
Faisal Latif6492cdf2008-07-24 20:50:45 -07001840 switch (cm_node->state) {
1841 case NES_CM_STATE_SYN_SENT:
1842 case NES_CM_STATE_MPAREQ_SENT:
1843 /* Rcvd syn on active open connection*/
1844 active_open_err(cm_node, skb, 1);
1845 break;
1846 case NES_CM_STATE_LISTENING:
1847 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001848 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301849 cm_node->listener->backlog) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001850 nes_debug(NES_DBG_CM, "drop syn due to backlog "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301851 "pressure \n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07001852 cm_backlog_drops++;
1853 passive_open_err(cm_node, skb, 0);
1854 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001855 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001856 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301857 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001858 if (ret) {
1859 passive_open_err(cm_node, skb, 0);
1860 /* drop pkt */
1861 break;
1862 }
1863 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1864 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001865 cm_node->accept_pend = 1;
1866 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1867
Faisal Latif6492cdf2008-07-24 20:50:45 -07001868 cm_node->state = NES_CM_STATE_SYN_RCVD;
1869 send_syn(cm_node, 1, skb);
1870 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001871 case NES_CM_STATE_CLOSED:
1872 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001873 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001874 send_reset(cm_node, skb);
1875 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001876 case NES_CM_STATE_TSA:
1877 case NES_CM_STATE_ESTABLISHED:
1878 case NES_CM_STATE_FIN_WAIT1:
1879 case NES_CM_STATE_FIN_WAIT2:
1880 case NES_CM_STATE_MPAREQ_RCVD:
1881 case NES_CM_STATE_LAST_ACK:
1882 case NES_CM_STATE_CLOSING:
1883 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001884 default:
1885 drop_packet(skb);
1886 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001887 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001888}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001889
Faisal Latif6492cdf2008-07-24 20:50:45 -07001890static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301891 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001892{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001893 int ret;
1894 u32 inc_sequence;
1895 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001896
Faisal Latif6492cdf2008-07-24 20:50:45 -07001897 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001898 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001899 inc_sequence = ntohl(tcph->seq);
1900 switch (cm_node->state) {
1901 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001902 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001903 /* active open */
1904 if (check_syn(cm_node, tcph, skb))
1905 return;
1906 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1907 /* setup options */
1908 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1909 if (ret) {
1910 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301911 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001912 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001913 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001914 cleanup_retrans_entry(cm_node);
1915 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1916 send_mpa_request(cm_node, skb);
1917 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1918 break;
1919 case NES_CM_STATE_MPAREQ_RCVD:
1920 /* passive open, so should not be here */
1921 passive_open_err(cm_node, skb, 1);
1922 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001923 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08001924 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1925 cleanup_retrans_entry(cm_node);
1926 cm_node->state = NES_CM_STATE_CLOSED;
1927 send_reset(cm_node, skb);
1928 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001929 case NES_CM_STATE_CLOSED:
1930 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1931 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08001932 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001933 send_reset(cm_node, skb);
1934 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001935 case NES_CM_STATE_ESTABLISHED:
1936 case NES_CM_STATE_FIN_WAIT1:
1937 case NES_CM_STATE_FIN_WAIT2:
1938 case NES_CM_STATE_LAST_ACK:
1939 case NES_CM_STATE_TSA:
1940 case NES_CM_STATE_CLOSING:
1941 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001942 case NES_CM_STATE_MPAREQ_SENT:
1943 default:
1944 drop_packet(skb);
1945 break;
1946 }
1947}
1948
Faisal Latif109d67e2009-04-27 13:41:06 -07001949static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301950 struct tcphdr *tcph)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001951{
1952 int datasize = 0;
1953 u32 inc_sequence;
Faisal Latif109d67e2009-04-27 13:41:06 -07001954 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06001955 int optionsize;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301956
Faisal Latifabb77252008-11-21 20:50:52 -06001957 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001958
Faisal Latif6492cdf2008-07-24 20:50:45 -07001959 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07001960 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001961
1962 skb_pull(skb, tcph->doff << 2);
1963 inc_sequence = ntohl(tcph->seq);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001964 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001965 switch (cm_node->state) {
1966 case NES_CM_STATE_SYN_RCVD:
1967 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07001968 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001969 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1970 if (ret)
1971 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001972 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1973 cm_node->state = NES_CM_STATE_ESTABLISHED;
1974 if (datasize) {
1975 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001976 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301977 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001978 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301979 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001980 break;
1981 case NES_CM_STATE_ESTABLISHED:
1982 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001983 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001984 if (datasize) {
1985 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001986 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301987 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001988 drop_packet(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301989 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001990 break;
1991 case NES_CM_STATE_MPAREQ_SENT:
1992 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1993 if (datasize) {
1994 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001995 handle_rcv_mpa(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301996 } else { /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001997 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05301998 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001999 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002000 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002001 cleanup_retrans_entry(cm_node);
2002 cm_node->state = NES_CM_STATE_CLOSED;
2003 send_reset(cm_node, skb);
2004 break;
Faisal Latifabb77252008-11-21 20:50:52 -06002005 case NES_CM_STATE_CLOSED:
2006 cleanup_retrans_entry(cm_node);
Faisal Latif886f98a2009-12-09 15:54:18 -08002007 add_ref_cm_node(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06002008 send_reset(cm_node, skb);
2009 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002010 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002011 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002012 cleanup_retrans_entry(cm_node);
2013 cm_node->state = NES_CM_STATE_CLOSED;
2014 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002015 rem_ref_cm_node(cm_node->cm_core, cm_node);
2016 drop_packet(skb);
2017 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002018 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08002019 cleanup_retrans_entry(cm_node);
2020 drop_packet(skb);
2021 cm_node->state = NES_CM_STATE_FIN_WAIT2;
2022 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002023 case NES_CM_STATE_SYN_SENT:
2024 case NES_CM_STATE_FIN_WAIT2:
2025 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002026 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002027 case NES_CM_STATE_UNKNOWN:
2028 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002029 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002030 drop_packet(skb);
2031 break;
2032 }
Faisal Latif109d67e2009-04-27 13:41:06 -07002033 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002034}
2035
2036
2037
2038static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302039 struct sk_buff *skb, int optionsize, int passive)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002040{
2041 u8 *optionsloc = (u8 *)&tcph[1];
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302042
Faisal Latif6492cdf2008-07-24 20:50:45 -07002043 if (optionsize) {
2044 if (process_options(cm_node, optionsloc, optionsize,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302045 (u32)tcph->syn)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002046 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302047 __func__, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002048 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06002049 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002050 else
Faisal Latifabb77252008-11-21 20:50:52 -06002051 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002052 return 1;
2053 }
2054 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002055
2056 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302057 cm_node->tcp_cntxt.snd_wscale;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002058
Faisal Latif6492cdf2008-07-24 20:50:45 -07002059 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002060 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002061 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002062}
2063
Faisal Latif6492cdf2008-07-24 20:50:45 -07002064/*
2065 * active_open_err() will send reset() if flag set..
2066 * It will also send ABORT event.
2067 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002068static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302069 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002070{
2071 cleanup_retrans_entry(cm_node);
2072 if (reset) {
2073 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302074 "state=%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002075 add_ref_cm_node(cm_node);
2076 send_reset(cm_node, skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302077 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002078 dev_kfree_skb_any(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302079 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002080
2081 cm_node->state = NES_CM_STATE_CLOSED;
2082 create_event(cm_node, NES_CM_EVENT_ABORTED);
2083}
2084
2085/*
2086 * passive_open_err() will either do a reset() or will free up the skb and
2087 * remove the cm_node.
2088 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002089static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302090 int reset)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002091{
2092 cleanup_retrans_entry(cm_node);
2093 cm_node->state = NES_CM_STATE_CLOSED;
2094 if (reset) {
2095 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302096 "cm_node=%p state =%d\n", cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002097 send_reset(cm_node, skb);
2098 } else {
2099 dev_kfree_skb_any(skb);
2100 rem_ref_cm_node(cm_node->cm_core, cm_node);
2101 }
2102}
2103
2104/*
2105 * free_retrans_entry() routines assumes that the retrans_list_lock has
2106 * been acquired before calling.
2107 */
2108static void free_retrans_entry(struct nes_cm_node *cm_node)
2109{
2110 struct nes_timer_entry *send_entry;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302111
Faisal Latif6492cdf2008-07-24 20:50:45 -07002112 send_entry = cm_node->send_entry;
2113 if (send_entry) {
2114 cm_node->send_entry = NULL;
2115 dev_kfree_skb_any(send_entry->skb);
2116 kfree(send_entry);
2117 rem_ref_cm_node(cm_node->cm_core, cm_node);
2118 }
2119}
2120
2121static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
2122{
2123 unsigned long flags;
2124
2125 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
2126 free_retrans_entry(cm_node);
2127 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
2128}
2129
2130/**
2131 * process_packet
2132 * Returns skb if to be freed, else it will return NULL if already used..
2133 */
2134static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302135 struct nes_cm_core *cm_core)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002136{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302137 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002138 struct tcphdr *tcph = tcp_hdr(skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302139 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07002140 int ret = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302141
Faisal Latif6492cdf2008-07-24 20:50:45 -07002142 skb_pull(skb, ip_hdr(skb)->ihl << 2);
2143
2144 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302145 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
2146 tcph->ack, tcph->rst, tcph->fin);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002147
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302148 if (tcph->rst) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002149 pkt_type = NES_PKT_TYPE_RST;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302150 } else if (tcph->syn) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002151 pkt_type = NES_PKT_TYPE_SYN;
2152 if (tcph->ack)
2153 pkt_type = NES_PKT_TYPE_SYNACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302154 } else if (tcph->ack) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002155 pkt_type = NES_PKT_TYPE_ACK;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302156 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002157 if (tcph->fin)
2158 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002159
2160 switch (pkt_type) {
2161 case NES_PKT_TYPE_SYN:
2162 handle_syn_pkt(cm_node, skb, tcph);
2163 break;
2164 case NES_PKT_TYPE_SYNACK:
2165 handle_synack_pkt(cm_node, skb, tcph);
2166 break;
2167 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07002168 ret = handle_ack_pkt(cm_node, skb, tcph);
2169 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08002170 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002171 break;
2172 case NES_PKT_TYPE_RST:
2173 handle_rst_pkt(cm_node, skb, tcph);
2174 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002175 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07002176 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08002177 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07002178 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002179 break;
2180 }
2181}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002182
2183/**
2184 * mini_cm_listen - create a listen node with params
2185 */
2186static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302187 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002188{
2189 struct nes_cm_listener *listener;
2190 unsigned long flags;
2191
2192 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302193 cm_info->loc_addr, cm_info->loc_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002194
2195 /* cannot have multiple matching listeners */
2196 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302197 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002198 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
2199 /* find automatically incs ref count ??? */
2200 atomic_dec(&listener->ref_count);
2201 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
2202 return NULL;
2203 }
2204
2205 if (!listener) {
2206 /* create a CM listen node (1/2 node to compare incoming traffic to) */
2207 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
2208 if (!listener) {
2209 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
2210 return NULL;
2211 }
2212
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002213 listener->loc_addr = htonl(cm_info->loc_addr);
2214 listener->loc_port = htons(cm_info->loc_port);
2215 listener->reused_node = 0;
2216
2217 atomic_set(&listener->ref_count, 1);
2218 }
2219 /* pasive case */
2220 /* find already inc'ed the ref count */
2221 else {
2222 listener->reused_node = 1;
2223 }
2224
2225 listener->cm_id = cm_info->cm_id;
2226 atomic_set(&listener->pend_accepts_cnt, 0);
2227 listener->cm_core = cm_core;
2228 listener->nesvnic = nesvnic;
2229 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002230
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002231 listener->conn_type = cm_info->conn_type;
2232 listener->backlog = cm_info->backlog;
2233 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
2234
2235 if (!listener->reused_node) {
2236 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2237 list_add(&listener->list, &cm_core->listen_list.list);
2238 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2239 atomic_inc(&cm_core->listen_node_cnt);
2240 }
2241
2242 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302243 " listener = %p, backlog = %d, cm_id = %p.\n",
2244 cm_info->loc_addr, cm_info->loc_port,
2245 listener, listener->backlog, listener->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002246
2247 return listener;
2248}
2249
2250
2251/**
2252 * mini_cm_connect - make a connection node with params
2253 */
Faisal Latif54c86a82008-09-30 14:47:27 -07002254static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302255 struct nes_vnic *nesvnic, u16 private_data_len,
2256 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002257{
2258 int ret = 0;
2259 struct nes_cm_node *cm_node;
2260 struct nes_cm_listener *loopbackremotelistener;
2261 struct nes_cm_node *loopbackremotenode;
2262 struct nes_cm_info loopback_cm_info;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302263 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002264
2265 /* create a CM connection node */
2266 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2267 if (!cm_node)
2268 return NULL;
2269
Glenn Streiff7495ab62008-04-29 13:46:54 -07002270 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002271 cm_node->tcp_cntxt.client = 1;
2272 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2273
2274 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002275 loopbackremotelistener = find_listener(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302276 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2277 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002278 if (loopbackremotelistener == NULL) {
2279 create_event(cm_node, NES_CM_EVENT_ABORTED);
2280 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002281 loopback_cm_info = *cm_info;
2282 loopback_cm_info.loc_port = cm_info->rem_port;
2283 loopback_cm_info.rem_port = cm_info->loc_port;
2284 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002285 loopbackremotenode = make_cm_node(cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302286 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002287 if (!loopbackremotenode) {
2288 rem_ref_cm_node(cm_node->cm_core, cm_node);
2289 return NULL;
2290 }
2291 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002292 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002293 loopbackremotenode->tcp_cntxt.rcv_wscale =
2294 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002295 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002296 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302297 private_data_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002298 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002299
Faisal Latif6492cdf2008-07-24 20:50:45 -07002300 /* we are done handling this state. */
2301 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002302 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002303 cm_node->tcp_cntxt.rcv_nxt =
2304 loopbackremotenode->tcp_cntxt.loc_seq_num;
2305 loopbackremotenode->tcp_cntxt.rcv_nxt =
2306 cm_node->tcp_cntxt.loc_seq_num;
2307 cm_node->tcp_cntxt.max_snd_wnd =
2308 loopbackremotenode->tcp_cntxt.rcv_wnd;
2309 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2310 cm_node->tcp_cntxt.rcv_wnd;
2311 cm_node->tcp_cntxt.snd_wnd =
2312 loopbackremotenode->tcp_cntxt.rcv_wnd;
2313 loopbackremotenode->tcp_cntxt.snd_wnd =
2314 cm_node->tcp_cntxt.rcv_wnd;
2315 cm_node->tcp_cntxt.snd_wscale =
2316 loopbackremotenode->tcp_cntxt.rcv_wscale;
2317 loopbackremotenode->tcp_cntxt.snd_wscale =
2318 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002319 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002320 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2321 }
2322 return cm_node;
2323 }
2324
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302325 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
2326 cm_node->mpa_frame_size = private_data_len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002327
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302328 memcpy(start_buff, private_data, private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002329
2330 /* send a syn and goto syn sent state */
2331 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002332 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002333
Faisal Latif6492cdf2008-07-24 20:50:45 -07002334 if (ret) {
2335 /* error in sending the syn free up the cm_node struct */
2336 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302337 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2338 cm_node->rem_addr, cm_node->rem_port, cm_node,
2339 cm_node->cm_id);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002340 rem_ref_cm_node(cm_node->cm_core, cm_node);
2341 cm_node = NULL;
2342 }
2343
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302344 if (cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002345 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302346 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2347 cm_node->rem_addr, cm_node->rem_port, cm_node,
2348 cm_node->cm_id);
2349 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002350
2351 return cm_node;
2352}
2353
2354
2355/**
2356 * mini_cm_accept - accept a connection
2357 * This function is never called
2358 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302359static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002360{
2361 return 0;
2362}
2363
2364
2365/**
2366 * mini_cm_reject - reject and teardown a connection
2367 */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302368static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002369{
2370 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002371 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002372 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002373 struct nes_cm_event event;
2374 struct iw_cm_id *cm_id = cm_node->cm_id;
2375 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002376
Faisal Latif6492cdf2008-07-24 20:50:45 -07002377 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302378 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002379
Faisal Latif6492cdf2008-07-24 20:50:45 -07002380 if (cm_node->tcp_cntxt.client)
2381 return ret;
2382 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002383
Faisal Latif9d5ab132009-03-06 15:15:01 -08002384 if (!loopback) {
2385 passive_state = atomic_add_return(1, &cm_node->passive_state);
2386 if (passive_state == NES_SEND_RESET_EVENT) {
2387 cm_node->state = NES_CM_STATE_CLOSED;
2388 rem_ref_cm_node(cm_core, cm_node);
2389 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002390 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2391 rem_ref_cm_node(cm_core, cm_node);
2392 } else {
2393 ret = send_mpa_reject(cm_node);
2394 if (ret) {
2395 cm_node->state = NES_CM_STATE_CLOSED;
2396 err = send_reset(cm_node, NULL);
2397 if (err)
2398 WARN_ON(1);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302399 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002400 cm_id->add_ref(cm_id);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302401 }
Faisal Latifc5a7d482009-12-09 15:54:08 -08002402 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002403 }
2404 } else {
2405 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002406 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2407 rem_ref_cm_node(cm_core, cm_node);
2408 rem_ref_cm_node(cm_core, loopback);
2409 } else {
2410 event.cm_node = loopback;
2411 event.cm_info.rem_addr = loopback->rem_addr;
2412 event.cm_info.loc_addr = loopback->loc_addr;
2413 event.cm_info.rem_port = loopback->rem_port;
2414 event.cm_info.loc_port = loopback->loc_port;
2415 event.cm_info.cm_id = loopback->cm_id;
2416 cm_event_mpa_reject(&event);
2417 rem_ref_cm_node(cm_core, cm_node);
2418 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002419
Faisal Latifc5a7d482009-12-09 15:54:08 -08002420 cm_id = loopback->cm_id;
2421 rem_ref_cm_node(cm_core, loopback);
2422 cm_id->rem_ref(cm_id);
2423 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002424 }
2425
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002426 return ret;
2427}
2428
2429
2430/**
2431 * mini_cm_close
2432 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002433static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002434{
2435 int ret = 0;
2436
2437 if (!cm_core || !cm_node)
2438 return -EINVAL;
2439
2440 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002441 case NES_CM_STATE_SYN_RCVD:
2442 case NES_CM_STATE_SYN_SENT:
2443 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2444 case NES_CM_STATE_ESTABLISHED:
2445 case NES_CM_STATE_ACCEPTING:
2446 case NES_CM_STATE_MPAREQ_SENT:
2447 case NES_CM_STATE_MPAREQ_RCVD:
2448 cleanup_retrans_entry(cm_node);
2449 send_reset(cm_node, NULL);
2450 break;
2451 case NES_CM_STATE_CLOSE_WAIT:
2452 cm_node->state = NES_CM_STATE_LAST_ACK;
2453 send_fin(cm_node, NULL);
2454 break;
2455 case NES_CM_STATE_FIN_WAIT1:
2456 case NES_CM_STATE_FIN_WAIT2:
2457 case NES_CM_STATE_LAST_ACK:
2458 case NES_CM_STATE_TIME_WAIT:
2459 case NES_CM_STATE_CLOSING:
2460 ret = -1;
2461 break;
2462 case NES_CM_STATE_LISTENING:
Faisal Latif886f98a2009-12-09 15:54:18 -08002463 cleanup_retrans_entry(cm_node);
2464 send_reset(cm_node, NULL);
2465 break;
2466 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002467 case NES_CM_STATE_UNKNOWN:
2468 case NES_CM_STATE_INITED:
2469 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002470 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002471 ret = rem_ref_cm_node(cm_core, cm_node);
2472 break;
2473 case NES_CM_STATE_TSA:
2474 if (cm_node->send_entry)
2475 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302476 "send_entry=%p\n", cm_node->send_entry);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002477 ret = rem_ref_cm_node(cm_core, cm_node);
2478 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002479 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002480 return ret;
2481}
2482
2483
2484/**
2485 * recv_pkt - recv an ETHERNET packet, and process it through CM
2486 * node state machine
2487 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002488static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302489 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002490{
2491 struct nes_cm_node *cm_node = NULL;
2492 struct nes_cm_listener *listener = NULL;
2493 struct iphdr *iph;
2494 struct tcphdr *tcph;
2495 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002496 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002497 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002498
Faisal Latif6492cdf2008-07-24 20:50:45 -07002499 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002500 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302501 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002502 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002503
2504 iph = (struct iphdr *)skb->data;
2505 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002506
2507 nfo.loc_addr = ntohl(iph->daddr);
2508 nfo.loc_port = ntohs(tcph->dest);
2509 nfo.rem_addr = ntohl(iph->saddr);
2510 nfo.rem_port = ntohs(tcph->source);
2511
Harvey Harrison03080e52009-01-10 21:45:42 -08002512 tmp_daddr = cpu_to_be32(iph->daddr);
2513 tmp_saddr = cpu_to_be32(iph->saddr);
2514
Harvey Harrison63779432008-10-31 00:56:00 -07002515 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002516 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002517
Faisal Latif6492cdf2008-07-24 20:50:45 -07002518 do {
2519 cm_node = find_node(cm_core,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302520 nfo.rem_port, nfo.rem_addr,
2521 nfo.loc_port, nfo.loc_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002522
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002523 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002524 /* Only type of packet accepted are for */
2525 /* the PASSIVE open (syn only) */
2526 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002527 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002528 break;
2529 }
2530 listener = find_listener(cm_core, nfo.loc_addr,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302531 nfo.loc_port,
2532 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002533 if (!listener) {
2534 nfo.cm_id = NULL;
2535 nfo.conn_type = 0;
2536 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2537 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002538 break;
2539 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002540 nfo.cm_id = listener->cm_id;
2541 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002542 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302543 listener);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002544 if (!cm_node) {
2545 nes_debug(NES_DBG_CM, "Unable to allocate "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302546 "node\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07002547 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002548 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002549 dev_kfree_skb_any(skb);
2550 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002551 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002552 if (!tcph->rst && !tcph->fin) {
2553 cm_node->state = NES_CM_STATE_LISTENING;
2554 } else {
2555 cm_packets_dropped++;
2556 rem_ref_cm_node(cm_core, cm_node);
2557 dev_kfree_skb_any(skb);
2558 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002559 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002560 add_ref_cm_node(cm_node);
2561 } else if (cm_node->state == NES_CM_STATE_TSA) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002562 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002563 atomic_inc(&cm_accel_dropped_pkts);
2564 dev_kfree_skb_any(skb);
2565 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002566 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002567 skb_reset_network_header(skb);
2568 skb_set_transport_header(skb, sizeof(*tcph));
2569 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002570 process_packet(cm_node, skb, cm_core);
2571 rem_ref_cm_node(cm_core, cm_node);
2572 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002573 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002574}
2575
2576
2577/**
2578 * nes_cm_alloc_core - allocate a top level instance of a cm core
2579 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002580static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002581{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002582 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002583
2584 /* setup the CM core */
2585 /* alloc top level core control structure */
2586 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2587 if (!cm_core)
2588 return NULL;
2589
2590 INIT_LIST_HEAD(&cm_core->connected_nodes);
2591 init_timer(&cm_core->tcp_timer);
2592 cm_core->tcp_timer.function = nes_cm_timer_tick;
2593
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302594 cm_core->mtu = NES_CM_DEFAULT_MTU;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002595 cm_core->state = NES_CM_STATE_INITED;
2596 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2597
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002598 atomic_set(&cm_core->events_posted, 0);
2599
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002600 cm_core->api = &nes_cm_api;
2601
2602 spin_lock_init(&cm_core->ht_lock);
2603 spin_lock_init(&cm_core->listen_list_lock);
2604
2605 INIT_LIST_HEAD(&cm_core->listen_list.list);
2606
2607 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2608
2609 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2610 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2611 cm_core->post_event = nes_cm_post_event;
2612 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2613 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2614
2615 print_core(cm_core);
2616 return cm_core;
2617}
2618
2619
2620/**
2621 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2622 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002623static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002624{
2625 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2626
2627 if (!cm_core)
2628 return -EINVAL;
2629
2630 barrier();
2631
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302632 if (timer_pending(&cm_core->tcp_timer))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002633 del_timer(&cm_core->tcp_timer);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002634
2635 destroy_workqueue(cm_core->event_wq);
2636 destroy_workqueue(cm_core->disconn_wq);
2637 nes_debug(NES_DBG_CM, "\n");
2638 kfree(cm_core);
2639
2640 return 0;
2641}
2642
2643
2644/**
2645 * mini_cm_get
2646 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002647static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002648{
2649 return cm_core->state;
2650}
2651
2652
2653/**
2654 * mini_cm_set
2655 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002656static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002657{
2658 int ret = 0;
2659
2660 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002661 case NES_CM_SET_PKT_SIZE:
2662 cm_core->mtu = value;
2663 break;
2664 case NES_CM_SET_FREE_PKT_Q_SIZE:
2665 cm_core->free_tx_pkt_max = value;
2666 break;
2667 default:
2668 /* unknown set option */
2669 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002670 }
2671
2672 return ret;
2673}
2674
2675
2676/**
2677 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2678 * successfully exchanged when this is called
2679 */
2680static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2681{
2682 int ret = 0;
2683
2684 if (!nesqp)
2685 return -EINVAL;
2686
2687 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302688 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2689 NES_QPCONTEXT_MISC_DROS);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002690
2691 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2692 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2693
2694 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2695
2696 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2697
2698 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302699 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002700
2701 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302702 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2703 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002704
2705 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302706 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2707 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002708
2709 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2710 nesqp->nesqp_context->ts_recent = 0;
2711 nesqp->nesqp_context->ts_age = 0;
2712 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2713 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2714 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2715 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302716 cm_node->tcp_cntxt.rcv_wscale);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002717 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2718 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2719 nesqp->nesqp_context->srtt = 0;
2720 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2721 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302722 nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002723 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2724 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2725 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2726
2727 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302728 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2729 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2730 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2731 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2732 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2733 le32_to_cpu(nesqp->nesqp_context->misc));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002734 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2735 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2736 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2737
2738 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2739 cm_node->state = NES_CM_STATE_TSA;
2740
2741 return ret;
2742}
2743
2744
2745/**
2746 * nes_cm_disconn
2747 */
2748int nes_cm_disconn(struct nes_qp *nesqp)
2749{
Don Wood873fcdd2009-09-05 20:36:37 -07002750 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002751
Don Wood873fcdd2009-09-05 20:36:37 -07002752 work = kzalloc(sizeof *work, GFP_ATOMIC);
2753 if (!work)
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302754 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002755
Don Wood873fcdd2009-09-05 20:36:37 -07002756 nes_add_ref(&nesqp->ibqp);
2757 work->nesqp = nesqp;
2758 INIT_WORK(&work->work, nes_disconnect_worker);
2759 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002760 return 0;
2761}
2762
2763
2764/**
2765 * nes_disconnect_worker
2766 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002767static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002768{
Don Wood873fcdd2009-09-05 20:36:37 -07002769 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2770 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002771
Don Wood873fcdd2009-09-05 20:36:37 -07002772 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002773 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302774 nesqp->last_aeq, nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002775 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002776 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002777}
2778
2779
2780/**
2781 * nes_cm_disconn_true
2782 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002783static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002784{
2785 unsigned long flags;
2786 int ret = 0;
2787 struct iw_cm_id *cm_id;
2788 struct iw_cm_event cm_event;
2789 struct nes_vnic *nesvnic;
2790 u16 last_ae;
2791 u8 original_hw_tcp_state;
2792 u8 original_ibqp_state;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002793 int disconn_status = 0;
Don Woodb29a4fc2009-09-05 20:36:39 -07002794 int issue_disconn = 0;
2795 int issue_close = 0;
2796 int issue_flush = 0;
2797 u32 flush_q = NES_CQP_FLUSH_RQ;
2798 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002799
2800 if (!nesqp) {
2801 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2802 return -1;
2803 }
2804
2805 spin_lock_irqsave(&nesqp->lock, flags);
2806 cm_id = nesqp->cm_id;
2807 /* make sure we havent already closed this connection */
2808 if (!cm_id) {
2809 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302810 nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002811 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002812 return -1;
2813 }
2814
2815 nesvnic = to_nesvnic(nesqp->ibqp.device);
2816 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2817
2818 original_hw_tcp_state = nesqp->hw_tcp_state;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302819 original_ibqp_state = nesqp->ibqp_state;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002820 last_ae = nesqp->last_aeq;
2821
Don Woodb29a4fc2009-09-05 20:36:39 -07002822 if (nesqp->term_flags) {
2823 issue_disconn = 1;
2824 issue_close = 1;
2825 nesqp->cm_id = NULL;
2826 if (nesqp->flush_issued == 0) {
2827 nesqp->flush_issued = 1;
2828 issue_flush = 1;
2829 }
2830 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2831 ((original_ibqp_state == IB_QPS_RTS) &&
2832 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2833 issue_disconn = 1;
2834 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002835 disconn_status = -ECONNRESET;
Don Woodb29a4fc2009-09-05 20:36:39 -07002836 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002837
Don Woodb29a4fc2009-09-05 20:36:39 -07002838 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2839 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2840 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2841 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2842 issue_close = 1;
2843 nesqp->cm_id = NULL;
2844 if (nesqp->flush_issued == 0) {
2845 nesqp->flush_issued = 1;
2846 issue_flush = 1;
2847 }
2848 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002849
Don Woodb29a4fc2009-09-05 20:36:39 -07002850 spin_unlock_irqrestore(&nesqp->lock, flags);
2851
2852 if ((issue_flush) && (nesqp->destroyed == 0)) {
2853 /* Flush the queue(s) */
2854 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2855 flush_q |= NES_CQP_FLUSH_SQ;
2856 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2857
2858 if (nesqp->term_flags) {
2859 ibevent.device = nesqp->ibqp.device;
2860 ibevent.event = nesqp->terminate_eventtype;
2861 ibevent.element.qp = &nesqp->ibqp;
2862 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
2863 }
2864 }
2865
2866 if ((cm_id) && (cm_id->event_handler)) {
2867 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002868 atomic_inc(&cm_disconnects);
2869 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002870 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002871 cm_event.local_addr = cm_id->local_addr;
2872 cm_event.remote_addr = cm_id->remote_addr;
2873 cm_event.private_data = NULL;
2874 cm_event.private_data_len = 0;
2875
Faisal Latif6492cdf2008-07-24 20:50:45 -07002876 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302877 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2878 "cm_id = %p, refcount = %u.\n",
2879 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2880 nesqp->hwqp.sq_tail, cm_id,
2881 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002882
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002883 ret = cm_id->event_handler(cm_id, &cm_event);
2884 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002885 nes_debug(NES_DBG_CM, "OFA CM event_handler "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302886 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002887 }
2888
Don Woodb29a4fc2009-09-05 20:36:39 -07002889 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002890 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002891 nes_disconnect(nesqp, 1);
2892
2893 cm_id->provider_data = nesqp;
2894 /* Send up the close complete event */
2895 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07002896 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002897 cm_event.provider_data = cm_id->provider_data;
2898 cm_event.local_addr = cm_id->local_addr;
2899 cm_event.remote_addr = cm_id->remote_addr;
2900 cm_event.private_data = NULL;
2901 cm_event.private_data_len = 0;
2902
2903 ret = cm_id->event_handler(cm_id, &cm_event);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302904 if (ret)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002905 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002906
2907 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002908 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002909 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002910
2911 return 0;
2912}
2913
2914
2915/**
2916 * nes_disconnect
2917 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002918static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002919{
2920 int ret = 0;
2921 struct nes_vnic *nesvnic;
2922 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002923 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002924
2925 nesvnic = to_nesvnic(nesqp->ibqp.device);
2926 if (!nesvnic)
2927 return -EINVAL;
2928
2929 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002930 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002931
2932 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00002933 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002934
2935 if (nesqp->active_conn) {
2936
2937 /* indicate this connection is NOT active */
2938 nesqp->active_conn = 0;
2939 } else {
2940 /* Need to free the Last Streaming Mode Message */
2941 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002942 if (nesqp->lsmm_mr)
2943 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002944 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302945 nesqp->private_data_len + nesqp->ietf_frame_size,
2946 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002947 }
2948 }
2949
2950 /* close the CM node down if it is still active */
2951 if (nesqp->cm_node) {
2952 nes_debug(NES_DBG_CM, "Call close API\n");
2953
2954 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002955 }
2956
2957 return ret;
2958}
2959
2960
2961/**
2962 * nes_accept
2963 */
2964int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2965{
2966 u64 u64temp;
2967 struct ib_qp *ibqp;
2968 struct nes_qp *nesqp;
2969 struct nes_vnic *nesvnic;
2970 struct nes_device *nesdev;
2971 struct nes_cm_node *cm_node;
2972 struct nes_adapter *adapter;
2973 struct ib_qp_attr attr;
2974 struct iw_cm_event cm_event;
2975 struct nes_hw_qp_wqe *wqe;
2976 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002977 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002978 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002979 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002980 struct nes_ib_device *nesibdev;
2981 struct ib_mr *ibmr = NULL;
2982 struct ib_phys_buf ibphysbuf;
2983 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002984 u64 tagged_offset;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05302985 u8 mpa_frame_offset = 0;
2986 struct ietf_mpa_v2 *mpa_v2_frame;
2987 u8 start_addr = 0;
2988 u8 *start_ptr = &start_addr;
2989 u8 **start_buff = &start_ptr;
2990 u16 buff_len = 0;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002991
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002992 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2993 if (!ibqp)
2994 return -EINVAL;
2995
2996 /* get all our handles */
2997 nesqp = to_nesqp(ibqp);
2998 nesvnic = to_nesvnic(nesqp->ibqp.device);
2999 nesdev = nesvnic->nesdev;
3000 adapter = nesdev->nesadapter;
3001
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003002 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003003 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
3004 "%s\n", cm_node, nesvnic, nesvnic->netdev,
3005 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003006
Faisal Latifc5a7d482009-12-09 15:54:08 -08003007 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
3008 if (cm_node->loopbackpartner)
3009 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
3010 rem_ref_cm_node(cm_node->cm_core, cm_node);
3011 return -EINVAL;
3012 }
3013
Faisal Latifdae58722010-08-14 21:04:56 +00003014 passive_state = atomic_add_return(1, &cm_node->passive_state);
3015 if (passive_state == NES_SEND_RESET_EVENT) {
3016 rem_ref_cm_node(cm_node->cm_core, cm_node);
3017 return -ECONNRESET;
3018 }
3019
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003020 /* associate the node with the QP */
3021 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003022 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003023
Faisal Latif6492cdf2008-07-24 20:50:45 -07003024 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
3025 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003026 atomic_inc(&cm_accepts);
3027
3028 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
Eric Dumazet29b44332010-10-11 10:22:12 +00003029 netdev_refcnt_read(nesvnic->netdev));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003030
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303031 nesqp->ietf_frame_size = sizeof(struct ietf_mpa_v2);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003032 /* allocate the ietf frame and space for private data */
3033 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303034 nesqp->ietf_frame_size + conn_param->private_data_len,
3035 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003036
Faisal Latif6492cdf2008-07-24 20:50:45 -07003037 if (!nesqp->ietf_frame) {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303038 nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
Faisal Latif6492cdf2008-07-24 20:50:45 -07003039 return -ENOMEM;
3040 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303041 mpa_v2_frame = (struct ietf_mpa_v2 *)nesqp->ietf_frame;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003042
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303043 if (cm_node->mpa_frame_rev == IETF_MPA_V1)
3044 mpa_frame_offset = 4;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003045
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303046 memcpy(mpa_v2_frame->priv_data, conn_param->private_data,
3047 conn_param->private_data_len);
3048
3049 cm_build_mpa_frame(cm_node, start_buff, &buff_len, nesqp->ietf_frame, MPA_KEY_REPLY);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003050 nesqp->private_data_len = conn_param->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003051
Faisal Latif6492cdf2008-07-24 20:50:45 -07003052 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
3053 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003054
Faisal Latif6492cdf2008-07-24 20:50:45 -07003055 if (cm_id->remote_addr.sin_addr.s_addr !=
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303056 cm_id->local_addr.sin_addr.s_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003057 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003058 nesibdev = nesvnic->nesibdev;
3059 nespd = nesqp->nespd;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303060 ibphysbuf.addr = nesqp->ietf_frame_pbase + mpa_frame_offset;
3061 ibphysbuf.size = buff_len;
3062 tagged_offset = (u64)(unsigned long)*start_buff;
Faisal Latifc12e56e2009-03-12 14:34:59 -07003063 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303064 &ibphysbuf, 1,
3065 IB_ACCESS_LOCAL_WRITE,
3066 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003067 if (!ibmr) {
3068 nes_debug(NES_DBG_CM, "Unable to register memory region"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303069 "for lSMM for cm_node = %p \n",
3070 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07003071 pci_free_consistent(nesdev->pcidev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303072 nesqp->private_data_len + nesqp->ietf_frame_size,
3073 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003074 return -ENOMEM;
3075 }
3076
3077 ibmr->pd = &nespd->ibpd;
3078 ibmr->device = nespd->ibpd.device;
3079 nesqp->lsmm_mr = ibmr;
3080
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303081 u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003082 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303083 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
3084 u64temp);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003085 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3086 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303087 NES_IWARP_SQ_WQE_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003088 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303089 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003090 set_wqe_64bit_value(wqe->wqe_words,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303091 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3092 (u64)(unsigned long)(*start_buff));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003093 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303094 cpu_to_le32(buff_len);
Faisal Latifc12e56e2009-03-12 14:34:59 -07003095 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Faisal Latifd2fa9b22009-12-09 15:54:28 -08003096 if (nesqp->sq_kmapped) {
3097 nesqp->sq_kmapped = 0;
3098 kunmap(nesqp->page);
3099 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003100
Faisal Latif6492cdf2008-07-24 20:50:45 -07003101 nesqp->nesqp_context->ird_ord_sizes |=
3102 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303103 NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003104 } else {
3105 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08003106 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003107 }
3108 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003109
3110
3111 /* Cache the cm_id in the qp */
3112 nesqp->cm_id = cm_id;
3113 cm_node->cm_id = cm_id;
3114
3115 /* nesqp->cm_node = (void *)cm_id->provider_data; */
3116 cm_id->provider_data = nesqp;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303117 nesqp->active_conn = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003118
Faisal Latif6492cdf2008-07-24 20:50:45 -07003119 if (cm_node->state == NES_CM_STATE_TSA)
3120 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303121 cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003122
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003123 nes_cm_init_tsa_conn(nesqp, cm_node);
3124
Faisal Latif6492cdf2008-07-24 20:50:45 -07003125 nesqp->nesqp_context->tcpPorts[0] =
3126 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3127 nesqp->nesqp_context->tcpPorts[1] =
3128 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3129
3130 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3131 nesqp->nesqp_context->ip0 =
3132 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3133 else
3134 nesqp->nesqp_context->ip0 =
3135 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003136
3137 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303138 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3139 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003140
Faisal Latif6492cdf2008-07-24 20:50:45 -07003141 nesqp->nesqp_context->arp_index_vlan |=
3142 cpu_to_le32(nes_arp_table(nesdev,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303143 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
3144 NES_ARP_RESOLVE) << 16);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003145
3146 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003147 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003148
3149 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3150
3151 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003152 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
3153 nesqp->nesqp_context->ird_ord_sizes |=
3154 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003155
3156 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003157 nes_quad.DstIpAdrIndex =
3158 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3159 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3160 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3161 else
3162 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
3163 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3164 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003165
3166 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003167 crc_value = get_crc_value(&nes_quad);
3168 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003169 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303170 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003171
3172 nesqp->hte_index &= adapter->hte_index_mask;
3173 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3174
3175 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3176
Faisal Latif6492cdf2008-07-24 20:50:45 -07003177 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303178 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
3179 "private data length=%u.\n", nesqp->hwqp.qp_id,
3180 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3181 ntohs(cm_id->remote_addr.sin_port),
3182 ntohl(cm_id->local_addr.sin_addr.s_addr),
3183 ntohs(cm_id->local_addr.sin_port),
3184 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
3185 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
3186 buff_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003187
Coly Li73ac36e2009-01-07 18:09:16 -08003188 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003189 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07003190 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003191
3192 cm_event.event = IW_CM_EVENT_ESTABLISHED;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003193 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003194 cm_event.provider_data = (void *)nesqp;
3195 cm_event.local_addr = cm_id->local_addr;
3196 cm_event.remote_addr = cm_id->remote_addr;
3197 cm_event.private_data = NULL;
3198 cm_event.private_data_len = 0;
3199 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003200 attr.qp_state = IB_QPS_RTS;
3201 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003202 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003203 cm_node->loopbackpartner->mpa_frame_size =
3204 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003205 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003206 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303207 conn_param->private_data, conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003208 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
3209 }
3210 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003211 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303212 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003213
3214 return 0;
3215}
3216
3217
3218/**
3219 * nes_reject
3220 */
3221int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3222{
3223 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003224 struct nes_cm_node *loopback;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003225 struct nes_cm_core *cm_core;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303226 u8 *start_buff;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003227
3228 atomic_inc(&cm_rejects);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303229 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003230 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003231 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003232 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003233
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303234 if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003235 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003236
Faisal Latif9d5ab132009-03-06 15:15:01 -08003237 if (loopback) {
3238 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3239 loopback->mpa_frame.priv_data_len = pdata_len;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303240 loopback->mpa_frame_size = pdata_len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003241 } else {
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303242 start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
3243 cm_node->mpa_frame_size = pdata_len;
3244 memcpy(start_buff, pdata, pdata_len);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003245 }
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303246 return cm_core->api->reject(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003247}
3248
3249
3250/**
3251 * nes_connect
3252 * setup and launch cm connect node
3253 */
3254int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3255{
3256 struct ib_qp *ibqp;
3257 struct nes_qp *nesqp;
3258 struct nes_vnic *nesvnic;
3259 struct nes_device *nesdev;
3260 struct nes_cm_node *cm_node;
3261 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003262 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003263
3264 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3265 if (!ibqp)
3266 return -EINVAL;
3267 nesqp = to_nesqp(ibqp);
3268 if (!nesqp)
3269 return -EINVAL;
3270 nesvnic = to_nesvnic(nesqp->ibqp.device);
3271 if (!nesvnic)
3272 return -EINVAL;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303273 nesdev = nesvnic->nesdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003274 if (!nesdev)
3275 return -EINVAL;
3276
Faisal Latifc5a7d482009-12-09 15:54:08 -08003277 if (!(cm_id->local_addr.sin_port) || !(cm_id->remote_addr.sin_port))
3278 return -EINVAL;
3279
Faisal Latif6492cdf2008-07-24 20:50:45 -07003280 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303281 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3282 ntohl(nesvnic->local_ipaddr),
3283 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3284 ntohs(cm_id->remote_addr.sin_port),
3285 ntohl(cm_id->local_addr.sin_addr.s_addr),
3286 ntohs(cm_id->local_addr.sin_port));
Faisal Latif6492cdf2008-07-24 20:50:45 -07003287
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003288 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003289 nesqp->active_conn = 1;
3290
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003291 /* cache the cm_id in the qp */
3292 nesqp->cm_id = cm_id;
3293
3294 cm_id->provider_data = nesqp;
3295
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003296 nesqp->private_data_len = conn_param->private_data_len;
3297 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
3298 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003299 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303300 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003301
Faisal Latif6492cdf2008-07-24 20:50:45 -07003302 if (cm_id->local_addr.sin_addr.s_addr !=
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303303 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003304 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303305 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003306 apbvt_set = 1;
3307 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003308
3309 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003310 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3311 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3312 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3313 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003314 cm_info.cm_id = cm_id;
3315 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3316
3317 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003318
3319 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003320 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303321 conn_param->private_data_len, (void *)conn_param->private_data,
3322 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003323 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003324 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003325 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303326 PCI_FUNC(nesdev->pcidev->devfn),
3327 NES_MANAGE_APBVT_DEL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003328
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003329 cm_id->rem_ref(cm_id);
3330 return -ENOMEM;
3331 }
3332
Faisal Latif53094c32009-04-27 13:37:34 -07003333 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003334 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003335 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003336 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003337
3338 return 0;
3339}
3340
3341
3342/**
3343 * nes_create_listen
3344 */
3345int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3346{
3347 struct nes_vnic *nesvnic;
3348 struct nes_cm_listener *cm_node;
3349 struct nes_cm_info cm_info;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003350 int err;
3351
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003352 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3353 cm_id, ntohs(cm_id->local_addr.sin_port));
3354
3355 nesvnic = to_nesvnic(cm_id->device);
3356 if (!nesvnic)
3357 return -EINVAL;
Roland Dreier69d51022010-08-04 14:25:40 -07003358
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003359 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3360 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3361
3362 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3363 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3364
3365 /* setup listen params in our api call struct */
3366 cm_info.loc_addr = nesvnic->local_ipaddr;
3367 cm_info.loc_port = cm_id->local_addr.sin_port;
3368 cm_info.backlog = backlog;
3369 cm_info.cm_id = cm_id;
3370
3371 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3372
3373
3374 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3375 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003376 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303377 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003378 return -ENOMEM;
3379 }
3380
3381 cm_id->provider_data = cm_node;
3382
3383 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003384 err = nes_manage_apbvt(nesvnic,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303385 ntohs(cm_id->local_addr.sin_port),
3386 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3387 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003388 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003389 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303390 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003391 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3392 return err;
3393 }
Faisal Latif6e10d2e2010-02-12 19:55:03 +00003394 atomic_inc(&cm_listens_created);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003395 }
3396
3397 cm_id->add_ref(cm_id);
3398 cm_id->provider_data = (void *)cm_node;
3399
3400
3401 return 0;
3402}
3403
3404
3405/**
3406 * nes_destroy_listen
3407 */
3408int nes_destroy_listen(struct iw_cm_id *cm_id)
3409{
3410 if (cm_id->provider_data)
3411 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3412 else
3413 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3414
3415 cm_id->rem_ref(cm_id);
3416
3417 return 0;
3418}
3419
3420
3421/**
3422 * nes_cm_recv
3423 */
3424int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3425{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003426 int rc = 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303427
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003428 cm_packets_received++;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303429 if ((g_cm_core) && (g_cm_core->api))
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003430 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303431 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003432 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303433 " cm is not setup properly.\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003434
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003435 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003436}
3437
3438
3439/**
3440 * nes_cm_start
3441 * Start and init a cm core module
3442 */
3443int nes_cm_start(void)
3444{
3445 nes_debug(NES_DBG_CM, "\n");
3446 /* create the primary CM core, pass this handle to subsequent core inits */
3447 g_cm_core = nes_cm_alloc_core();
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303448 if (g_cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003449 return 0;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303450 else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003451 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003452}
3453
3454
3455/**
3456 * nes_cm_stop
3457 * stop and dealloc all cm core instances
3458 */
3459int nes_cm_stop(void)
3460{
3461 g_cm_core->api->destroy_cm_core(g_cm_core);
3462 return 0;
3463}
3464
3465
3466/**
3467 * cm_event_connected
3468 * handle a connected event, setup QPs and HW
3469 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003470static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003471{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003472 struct nes_qp *nesqp;
3473 struct nes_vnic *nesvnic;
3474 struct nes_device *nesdev;
3475 struct nes_cm_node *cm_node;
3476 struct nes_adapter *nesadapter;
3477 struct ib_qp_attr attr;
3478 struct iw_cm_id *cm_id;
3479 struct iw_cm_event cm_event;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003480 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003481 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003482 int ret;
3483
3484 /* get all our handles */
3485 cm_node = event->cm_node;
3486 cm_id = cm_node->cm_id;
3487 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3488 nesqp = (struct nes_qp *)cm_id->provider_data;
3489 nesvnic = to_nesvnic(nesqp->ibqp.device);
3490 nesdev = nesvnic->nesdev;
3491 nesadapter = nesdev->nesadapter;
3492
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303493 if (nesqp->destroyed)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003494 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003495 atomic_inc(&cm_connecteds);
3496 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303497 " local port 0x%04X. jiffies = %lu.\n",
3498 nesqp->hwqp.qp_id,
3499 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3500 ntohs(cm_id->remote_addr.sin_port),
3501 ntohs(cm_id->local_addr.sin_port),
3502 jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003503
3504 nes_cm_init_tsa_conn(nesqp, cm_node);
3505
3506 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003507 nesqp->nesqp_context->tcpPorts[0] =
3508 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3509 nesqp->nesqp_context->tcpPorts[1] =
3510 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3511 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3512 nesqp->nesqp_context->ip0 =
3513 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3514 else
3515 nesqp->nesqp_context->ip0 =
3516 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003517
3518 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003519 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3520 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003521 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003522 nes_arp_table(nesdev,
3523 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003524 NULL, NES_ARP_RESOLVE) << 16);
3525 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3526 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3527 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3528 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003529 cpu_to_le32((u32)1 <<
3530 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003531
3532 /* Adjust tail for not having a LSMM */
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303533 /*nesqp->hwqp.sq_tail = 1;*/
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003534
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303535 build_rdma0_msg(cm_node, &nesqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003536
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303537 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3538 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003539
3540 memset(&nes_quad, 0, sizeof(nes_quad));
3541
Faisal Latif6492cdf2008-07-24 20:50:45 -07003542 nes_quad.DstIpAdrIndex =
3543 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3544 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3545 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3546 else
3547 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003548 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3549 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3550
3551 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003552 crc_value = get_crc_value(&nes_quad);
3553 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003554 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303555 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003556
3557 nesqp->hte_index &= nesadapter->hte_index_mask;
3558 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3559
3560 nesqp->ietf_frame = &cm_node->mpa_frame;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303561 nesqp->private_data_len = (u8)cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003562 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3563
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003564 /* notify OF layer we successfully created the requested connection */
3565 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003566 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003567 cm_event.provider_data = cm_id->provider_data;
3568 cm_event.local_addr.sin_family = AF_INET;
3569 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3570 cm_event.remote_addr = cm_id->remote_addr;
3571
Faisal Latif6492cdf2008-07-24 20:50:45 -07003572 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303573 cm_event.private_data_len = (u8)event->cm_node->mpa_frame_size;
3574 cm_event.ird = cm_node->ird_size;
3575 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003576
3577 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3578 ret = cm_id->event_handler(cm_id, &cm_event);
3579 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3580
3581 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003582 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303583 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003584 attr.qp_state = IB_QPS_RTS;
3585 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003586
Faisal Latif6492cdf2008-07-24 20:50:45 -07003587 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303588 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003589
3590 return;
3591}
3592
3593
3594/**
3595 * cm_event_connect_error
3596 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003597static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003598{
3599 struct nes_qp *nesqp;
3600 struct iw_cm_id *cm_id;
3601 struct iw_cm_event cm_event;
3602 /* struct nes_cm_info cm_info; */
3603 int ret;
3604
3605 if (!event->cm_node)
3606 return;
3607
3608 cm_id = event->cm_node->cm_id;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303609 if (!cm_id)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003610 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003611
3612 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3613 nesqp = cm_id->provider_data;
3614
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303615 if (!nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003616 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003617
3618 /* notify OF layer about this connection error event */
3619 /* cm_id->rem_ref(cm_id); */
3620 nesqp->cm_id = NULL;
3621 cm_id->provider_data = NULL;
3622 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003623 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003624 cm_event.provider_data = cm_id->provider_data;
3625 cm_event.local_addr = cm_id->local_addr;
3626 cm_event.remote_addr = cm_id->remote_addr;
3627 cm_event.private_data = NULL;
3628 cm_event.private_data_len = 0;
3629
Faisal Latif6492cdf2008-07-24 20:50:45 -07003630 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303631 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3632 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003633
3634 ret = cm_id->event_handler(cm_id, &cm_event);
3635 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3636 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003637 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303638 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003639 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003640
Faisal Latif6492cdf2008-07-24 20:50:45 -07003641 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003642 return;
3643}
3644
3645
3646/**
3647 * cm_event_reset
3648 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003649static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003650{
3651 struct nes_qp *nesqp;
3652 struct iw_cm_id *cm_id;
3653 struct iw_cm_event cm_event;
3654 /* struct nes_cm_info cm_info; */
3655 int ret;
3656
3657 if (!event->cm_node)
3658 return;
3659
3660 if (!event->cm_node->cm_id)
3661 return;
3662
3663 cm_id = event->cm_node->cm_id;
3664
3665 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3666 nesqp = cm_id->provider_data;
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003667 if (!nesqp)
3668 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003669
3670 nesqp->cm_id = NULL;
3671 /* cm_id->provider_data = NULL; */
3672 cm_event.event = IW_CM_EVENT_DISCONNECT;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003673 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003674 cm_event.provider_data = cm_id->provider_data;
3675 cm_event.local_addr = cm_id->local_addr;
3676 cm_event.remote_addr = cm_id->remote_addr;
3677 cm_event.private_data = NULL;
3678 cm_event.private_data_len = 0;
3679
Faisal Latif183ecfa2008-11-21 20:50:46 -06003680 cm_id->add_ref(cm_id);
Faisal Latiff9f3f1e2009-12-09 15:54:14 -08003681 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003682 atomic_inc(&cm_closes);
3683 cm_event.event = IW_CM_EVENT_CLOSE;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003684 cm_event.status = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06003685 cm_event.provider_data = cm_id->provider_data;
3686 cm_event.local_addr = cm_id->local_addr;
3687 cm_event.remote_addr = cm_id->remote_addr;
3688 cm_event.private_data = NULL;
3689 cm_event.private_data_len = 0;
3690 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3691 ret = cm_id->event_handler(cm_id, &cm_event);
3692
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003693 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3694
3695
3696 /* notify OF layer about this connection error event */
3697 cm_id->rem_ref(cm_id);
3698
3699 return;
3700}
3701
3702
3703/**
3704 * cm_event_mpa_req
3705 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003706static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003707{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303708 struct iw_cm_id *cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003709 struct iw_cm_event cm_event;
3710 int ret;
3711 struct nes_cm_node *cm_node;
3712
3713 cm_node = event->cm_node;
3714 if (!cm_node)
3715 return;
3716 cm_id = cm_node->cm_id;
3717
3718 atomic_inc(&cm_connect_reqs);
3719 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303720 cm_node, cm_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003721
3722 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
Roland Dreierd0c49bf2011-05-09 22:23:57 -07003723 cm_event.status = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003724 cm_event.provider_data = (void *)cm_node;
3725
3726 cm_event.local_addr.sin_family = AF_INET;
3727 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3728 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3729
3730 cm_event.remote_addr.sin_family = AF_INET;
3731 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3732 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003733 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303734 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
3735 cm_event.ird = cm_node->ird_size;
3736 cm_event.ord = cm_node->ord_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003737
3738 ret = cm_id->event_handler(cm_id, &cm_event);
3739 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003740 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303741 __func__, __LINE__, ret);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003742 return;
3743}
3744
3745
3746static void cm_event_mpa_reject(struct nes_cm_event *event)
3747{
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303748 struct iw_cm_id *cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003749 struct iw_cm_event cm_event;
3750 struct nes_cm_node *cm_node;
3751 int ret;
3752
3753 cm_node = event->cm_node;
3754 if (!cm_node)
3755 return;
3756 cm_id = cm_node->cm_id;
3757
3758 atomic_inc(&cm_connect_reqs);
3759 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303760 cm_node, cm_id, jiffies);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003761
3762 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3763 cm_event.status = -ECONNREFUSED;
3764 cm_event.provider_data = cm_id->provider_data;
3765
3766 cm_event.local_addr.sin_family = AF_INET;
3767 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3768 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3769
3770 cm_event.remote_addr.sin_family = AF_INET;
3771 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3772 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3773
3774 cm_event.private_data = cm_node->mpa_frame_buf;
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303775 cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003776
3777 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303778 "remove_addr=%08x\n",
3779 cm_event.local_addr.sin_addr.s_addr,
3780 cm_event.remote_addr.sin_addr.s_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003781
3782 ret = cm_id->event_handler(cm_id, &cm_event);
3783 if (ret)
3784 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303785 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003786
3787 return;
3788}
3789
3790
3791static void nes_cm_event_handler(struct work_struct *);
3792
3793/**
3794 * nes_cm_post_event
3795 * post an event to the cm event handler
3796 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003797static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003798{
3799 atomic_inc(&event->cm_node->cm_core->events_posted);
3800 add_ref_cm_node(event->cm_node);
3801 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3802 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003803 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303804 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003805
3806 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3807
3808 nes_debug(NES_DBG_CM, "Exit\n");
3809 return 0;
3810}
3811
3812
3813/**
3814 * nes_cm_event_handler
3815 * worker function to handle cm events
3816 * will free instance of nes_cm_event
3817 */
3818static void nes_cm_event_handler(struct work_struct *work)
3819{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003820 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303821 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003822 struct nes_cm_core *cm_core;
3823
Faisal Latif6492cdf2008-07-24 20:50:45 -07003824 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003825 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003826
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003827 cm_core = event->cm_node->cm_core;
3828 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303829 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003830
3831 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003832 case NES_CM_EVENT_MPA_REQ:
3833 cm_event_mpa_req(event);
3834 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303835 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003836 break;
3837 case NES_CM_EVENT_RESET:
3838 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303839 event->cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003840 cm_event_reset(event);
3841 break;
3842 case NES_CM_EVENT_CONNECTED:
3843 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303844 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003845 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003846 cm_event_connected(event);
3847 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3848 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003849 case NES_CM_EVENT_MPA_REJECT:
3850 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303851 (event->cm_node->state == NES_CM_STATE_TSA))
Faisal Latif9d5ab132009-03-06 15:15:01 -08003852 break;
3853 cm_event_mpa_reject(event);
3854 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3855 break;
3856
Faisal Latif6492cdf2008-07-24 20:50:45 -07003857 case NES_CM_EVENT_ABORTED:
3858 if ((!event->cm_node->cm_id) ||
Tatyana Nikolova615eb712011-09-25 20:17:46 +05303859 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003860 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003861 cm_event_connect_error(event);
3862 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3863 break;
3864 case NES_CM_EVENT_DROPPED_PKT:
3865 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3866 break;
3867 default:
3868 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3869 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003870 }
3871
3872 atomic_dec(&cm_core->events_posted);
3873 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3874 rem_ref_cm_node(cm_core, event->cm_node);
3875 kfree(event);
3876
3877 return;
3878}