blob: ec04786b6069c7eb1f4b68b17a6a8c500aa4b3be [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
37#include <asm/atomic.h>
38#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>
Bob Sharp7191a0a2008-10-03 12:21:19 -070055#include <net/arp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080056#include <net/neighbour.h>
57#include <net/route.h>
58#include <net/ip_fib.h>
Faisal Latifc11470f2009-04-27 13:38:31 -070059#include <net/tcp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080060
61#include "nes.h"
62
63u32 cm_packets_sent;
64u32 cm_packets_bounced;
65u32 cm_packets_dropped;
66u32 cm_packets_retrans;
67u32 cm_packets_created;
68u32 cm_packets_received;
69u32 cm_listens_created;
70u32 cm_listens_destroyed;
71u32 cm_backlog_drops;
72atomic_t cm_loopbacks;
73atomic_t cm_nodes_created;
74atomic_t cm_nodes_destroyed;
75atomic_t cm_accel_dropped_pkts;
76atomic_t cm_resets_recvd;
77
Faisal Latif6492cdf2008-07-24 20:50:45 -070078static inline int mini_cm_accelerated(struct nes_cm_core *,
79 struct nes_cm_node *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080080static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070081 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 *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070083static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070084 struct nes_vnic *, u16, void *, struct nes_cm_info *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070085static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -070086static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
87 struct nes_cm_node *);
88static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
89 struct nes_cm_node *);
Faisal Latif4a14f6a2008-11-21 20:50:49 -060090static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070091 struct sk_buff *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070092static int mini_cm_dealloc_core(struct nes_cm_core *);
93static int mini_cm_get(struct nes_cm_core *);
94static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070095
Chien Tung6098d102008-11-21 20:51:01 -060096static void form_cm_frame(struct sk_buff *, struct nes_cm_node *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070097 void *, u32, void *, u32, u8);
Faisal Latif6492cdf2008-07-24 20:50:45 -070098static int add_ref_cm_node(struct nes_cm_node *);
99static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
100
Roland Dreier1a855fb2008-04-16 21:01:09 -0700101static int nes_cm_disconn_true(struct nes_qp *);
102static int nes_cm_post_event(struct nes_cm_event *event);
103static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
104static void nes_disconnect_worker(struct work_struct *work);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700105
106static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800107static int send_mpa_reject(struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700108static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
109static int send_reset(struct nes_cm_node *, struct sk_buff *);
110static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fb2008-04-16 21:01:09 -0700111static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700112static void process_packet(struct nes_cm_node *, struct sk_buff *,
113 struct nes_cm_core *);
114
115static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
116static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
117static void cleanup_retrans_entry(struct nes_cm_node *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800118static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700119static void free_retrans_entry(struct nes_cm_node *cm_node);
120static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
121 struct sk_buff *skb, int optionsize, int passive);
122
123/* CM event handler functions */
124static void cm_event_connected(struct nes_cm_event *);
125static void cm_event_connect_error(struct nes_cm_event *);
126static void cm_event_reset(struct nes_cm_event *);
127static void cm_event_mpa_req(struct nes_cm_event *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800128static void cm_event_mpa_reject(struct nes_cm_event *);
129static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700130
131static void print_core(struct nes_cm_core *core);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800132
133/* External CM API Interface */
134/* instance of function pointers for client API */
135/* set address of this instance to cm_core->cm_ops at cm_core alloc */
136static struct nes_cm_ops nes_cm_api = {
137 mini_cm_accelerated,
138 mini_cm_listen,
139 mini_cm_del_listen,
140 mini_cm_connect,
141 mini_cm_close,
142 mini_cm_accept,
143 mini_cm_reject,
144 mini_cm_recv_pkt,
145 mini_cm_dealloc_core,
146 mini_cm_get,
147 mini_cm_set
148};
149
Roland Dreier1a855fb2008-04-16 21:01:09 -0700150static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800151
152atomic_t cm_connects;
153atomic_t cm_accepts;
154atomic_t cm_disconnects;
155atomic_t cm_closes;
156atomic_t cm_connecteds;
157atomic_t cm_connect_reqs;
158atomic_t cm_rejects;
159
160
161/**
162 * create_event
163 */
164static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
165 enum nes_cm_event_type type)
166{
167 struct nes_cm_event *event;
168
169 if (!cm_node->cm_id)
170 return NULL;
171
172 /* allocate an empty event */
173 event = kzalloc(sizeof(*event), GFP_ATOMIC);
174
175 if (!event)
176 return NULL;
177
178 event->type = type;
179 event->cm_node = cm_node;
180 event->cm_info.rem_addr = cm_node->rem_addr;
181 event->cm_info.loc_addr = cm_node->loc_addr;
182 event->cm_info.rem_port = cm_node->rem_port;
183 event->cm_info.loc_port = cm_node->loc_port;
184 event->cm_info.cm_id = cm_node->cm_id;
185
Faisal Latif6492cdf2008-07-24 20:50:45 -0700186 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
187 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
188 cm_node, event, type, event->cm_info.loc_addr,
189 event->cm_info.loc_port, event->cm_info.rem_addr,
190 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800191
192 nes_cm_post_event(event);
193 return event;
194}
195
196
197/**
198 * send_mpa_request
199 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700200static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800201{
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800202 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700203 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800204 return -1;
205 }
206
207 /* send an MPA Request frame */
208 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
209 cm_node->mpa_frame_size, SET_ACK);
210
Faisal Latif9d5ab132009-03-06 15:15:01 -0800211 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
212}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800213
Faisal Latif9d5ab132009-03-06 15:15:01 -0800214
215
216static int send_mpa_reject(struct nes_cm_node *cm_node)
217{
218 struct sk_buff *skb = NULL;
219
220 skb = dev_alloc_skb(MAX_CM_BUFFER);
221 if (!skb) {
222 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
223 return -ENOMEM;
224 }
225
226 /* send an MPA reject frame */
227 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
228 cm_node->mpa_frame_size, SET_ACK | SET_FIN);
229
230 cm_node->state = NES_CM_STATE_FIN_WAIT1;
231 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800232}
233
234
235/**
236 * recv_mpa - process a received TCP pkt, we are expecting an
237 * IETF MPA frame
238 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800239static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
240 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800241{
242 struct ietf_mpa_frame *mpa_frame;
243
Faisal Latif9d5ab132009-03-06 15:15:01 -0800244 *type = NES_MPA_REQUEST_ACCEPT;
245
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800246 /* assume req frame is in tcp data payload */
247 if (len < sizeof(struct ietf_mpa_frame)) {
248 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800249 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800250 }
251
252 mpa_frame = (struct ietf_mpa_frame *)buffer;
253 cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
Faisal Latif1cf078c2009-12-09 15:53:54 -0800254 /* make sure mpa private data len is less than 512 bytes */
255 if (cm_node->mpa_frame_size > IETF_MAX_PRIV_DATA_LEN) {
256 nes_debug(NES_DBG_CM, "The received Length of Private"
257 " Data field exceeds 512 octets\n");
258 return -EINVAL;
259 }
260 /*
261 * make sure MPA receiver interoperate with the
262 * received MPA version and MPA key information
263 *
264 */
265 if (mpa_frame->rev != mpa_version) {
266 nes_debug(NES_DBG_CM, "The received mpa version"
267 " can not be interoperated\n");
268 return -EINVAL;
269 }
270 if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
271 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
272 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
273 return -EINVAL;
274 }
275 } else {
276 if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
277 nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
278 return -EINVAL;
279 }
280 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800281
282 if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
283 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
284 " complete (%x + %x != %x)\n",
Faisal Latif9d5ab132009-03-06 15:15:01 -0800285 cm_node->mpa_frame_size,
286 (u32)sizeof(struct ietf_mpa_frame), len);
287 return -EINVAL;
288 }
289 /* make sure it does not exceed the max size */
290 if (len > MAX_CM_BUFFER) {
291 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
292 " (%x + %x != %x)\n",
293 cm_node->mpa_frame_size,
294 (u32)sizeof(struct ietf_mpa_frame), len);
295 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800296 }
297
298 /* copy entire MPA frame to our cm_node's frame */
299 memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
300 cm_node->mpa_frame_size);
301
Faisal Latif9d5ab132009-03-06 15:15:01 -0800302 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
303 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800304 return 0;
305}
306
307
308/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800309 * form_cm_frame - get a free packet and build empty frame Use
310 * node info to build.
311 */
Chien Tung6098d102008-11-21 20:51:01 -0600312static void form_cm_frame(struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700313 struct nes_cm_node *cm_node, void *options, u32 optionsize,
314 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800315{
316 struct tcphdr *tcph;
317 struct iphdr *iph;
318 struct ethhdr *ethh;
319 u8 *buf;
320 u16 packetsize = sizeof(*iph);
321
322 packetsize += sizeof(*tcph);
323 packetsize += optionsize + datasize;
324
325 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
326
327 skb->len = 0;
328 buf = skb_put(skb, packetsize + ETH_HLEN);
329
330 ethh = (struct ethhdr *) buf;
331 buf += ETH_HLEN;
332
333 iph = (struct iphdr *)buf;
334 buf += sizeof(*iph);
335 tcph = (struct tcphdr *)buf;
336 skb_reset_mac_header(skb);
337 skb_set_network_header(skb, ETH_HLEN);
338 skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
339 buf += sizeof(*tcph);
340
341 skb->ip_summed = CHECKSUM_PARTIAL;
342 skb->protocol = htons(0x800);
343 skb->data_len = 0;
344 skb->mac_len = ETH_HLEN;
345
346 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
347 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
348 ethh->h_proto = htons(0x0800);
349
350 iph->version = IPVERSION;
351 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
352 iph->tos = 0;
353 iph->tot_len = htons(packetsize);
354 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
355
356 iph->frag_off = htons(0x4000);
357 iph->ttl = 0x40;
358 iph->protocol = 0x06; /* IPPROTO_TCP */
359
360 iph->saddr = htonl(cm_node->loc_addr);
361 iph->daddr = htonl(cm_node->rem_addr);
362
363 tcph->source = htons(cm_node->loc_port);
364 tcph->dest = htons(cm_node->rem_port);
365 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
366
367 if (flags & SET_ACK) {
368 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
369 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
370 tcph->ack = 1;
371 } else
372 tcph->ack_seq = 0;
373
374 if (flags & SET_SYN) {
375 cm_node->tcp_cntxt.loc_seq_num++;
376 tcph->syn = 1;
377 } else
Faisal Latif6492cdf2008-07-24 20:50:45 -0700378 cm_node->tcp_cntxt.loc_seq_num += datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800379
Faisal Latif6492cdf2008-07-24 20:50:45 -0700380 if (flags & SET_FIN) {
381 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800382 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700383 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800384
385 if (flags & SET_RST)
386 tcph->rst = 1;
387
388 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
389 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
390 tcph->urg_ptr = 0;
391 if (optionsize)
392 memcpy(buf, options, optionsize);
393 buf += optionsize;
394 if (datasize)
395 memcpy(buf, data, datasize);
396
397 skb_shinfo(skb)->nr_frags = 0;
398 cm_packets_created++;
399
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800400}
401
402
403/**
404 * print_core - dump a cm core
405 */
406static void print_core(struct nes_cm_core *core)
407{
408 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
409 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
410 if (!core)
411 return;
412 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800413
414 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
415
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800416 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
417 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
418
419 nes_debug(NES_DBG_CM, "core : %p \n", core);
420
421 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
422}
423
424
425/**
426 * schedule_nes_timer
427 * note - cm_node needs to be protected before calling this. Encase in:
428 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
429 */
430int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
431 enum nes_timer_type type, int send_retrans,
432 int close_when_complete)
433{
434 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700435 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800436 struct nes_timer_entry *new_send;
437 int ret = 0;
438 u32 was_timer_set;
439
440 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
441 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800442 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800443
444 /* new_send->timetosend = currenttime */
445 new_send->retrycount = NES_DEFAULT_RETRYS;
446 new_send->retranscount = NES_DEFAULT_RETRANS;
447 new_send->skb = skb;
448 new_send->timetosend = jiffies;
449 new_send->type = type;
450 new_send->netdev = cm_node->netdev;
451 new_send->send_retrans = send_retrans;
452 new_send->close_when_complete = close_when_complete;
453
454 if (type == NES_TIMER_TYPE_CLOSE) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700455 new_send->timetosend += (HZ/10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800456 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700457 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800458 WARN_ON(1);
459 return -EINVAL;
460 }
461 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800462 }
463
464 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700465 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800466 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700467 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
468 cm_node->send_entry = new_send;
469 add_ref_cm_node(cm_node);
470 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
471 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800472
473 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
474 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700475 nes_debug(NES_DBG_CM, "Error sending packet %p "
476 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800477 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700478 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800479 } else {
480 cm_packets_sent++;
481 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700482 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800483 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700484 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800485 return ret;
486 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800487 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800488 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800489
490 was_timer_set = timer_pending(&cm_core->tcp_timer);
491
492 if (!was_timer_set) {
493 cm_core->tcp_timer.expires = new_send->timetosend;
494 add_timer(&cm_core->tcp_timer);
495 }
496
497 return ret;
498}
499
Faisal Latif9d5ab132009-03-06 15:15:01 -0800500static void nes_retrans_expired(struct nes_cm_node *cm_node)
501{
Faisal Latif68237a02009-06-22 22:53:28 -0700502 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800503 switch (cm_node->state) {
504 case NES_CM_STATE_SYN_RCVD:
505 case NES_CM_STATE_CLOSING:
506 rem_ref_cm_node(cm_node->cm_core, cm_node);
507 break;
508 case NES_CM_STATE_LAST_ACK:
509 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700510 if (cm_node->cm_id)
511 cm_id->rem_ref(cm_id);
512 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800513 send_reset(cm_node, NULL);
514 break;
515 default:
Faisal Latif69524e12009-12-09 15:54:03 -0800516 add_ref_cm_node(cm_node);
517 send_reset(cm_node, NULL);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800518 create_event(cm_node, NES_CM_EVENT_ABORTED);
519 }
520}
521
522static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
523{
524 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
525 struct iw_cm_id *cm_id = cm_node->cm_id;
526 struct nes_qp *nesqp;
527 unsigned long qplockflags;
528
529 if (!recv_entry)
530 return;
531 nesqp = (struct nes_qp *)recv_entry->skb;
532 if (nesqp) {
533 spin_lock_irqsave(&nesqp->lock, qplockflags);
534 if (nesqp->cm_id) {
535 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
536 "refcount = %d: HIT A "
537 "NES_TIMER_TYPE_CLOSE with something "
538 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
539 atomic_read(&nesqp->refcount));
540 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
541 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
542 nesqp->ibqp_state = IB_QPS_ERR;
543 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
544 nes_cm_disconn(nesqp);
545 } else {
546 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
547 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
548 "refcount = %d: HIT A "
549 "NES_TIMER_TYPE_CLOSE with nothing "
550 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
551 atomic_read(&nesqp->refcount));
552 }
553 } else if (rem_node) {
554 /* TIME_WAIT state */
555 rem_ref_cm_node(cm_node->cm_core, cm_node);
556 }
557 if (cm_node->cm_id)
558 cm_id->rem_ref(cm_id);
559 kfree(recv_entry);
560 cm_node->recv_entry = NULL;
561}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800562
563/**
564 * nes_cm_timer_tick
565 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700566static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800567{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800568 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800569 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800570 struct nes_cm_node *cm_node;
571 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800572 struct list_head *list_core_temp;
573 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800574 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800575 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700576 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800577 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800578
Faisal Latif879e5bd2008-11-21 20:50:41 -0600579 struct list_head timer_list;
580 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800581 spin_lock_irqsave(&cm_core->ht_lock, flags);
582
Faisal Latif6492cdf2008-07-24 20:50:45 -0700583 list_for_each_safe(list_node, list_core_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600584 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800585 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800586 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600587 add_ref_cm_node(cm_node);
588 list_add(&cm_node->timer_entry, &timer_list);
589 }
590 }
591 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
592
593 list_for_each_safe(list_node, list_core_temp, &timer_list) {
594 cm_node = container_of(list_node, struct nes_cm_node,
595 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800596 recv_entry = cm_node->recv_entry;
597
598 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700599 if (time_after(recv_entry->timetosend, jiffies)) {
600 if (nexttimeout > recv_entry->timetosend ||
Faisal Latif9d5ab132009-03-06 15:15:01 -0800601 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800602 nexttimeout = recv_entry->timetosend;
603 settimer = 1;
604 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800605 } else
606 handle_recv_entry(cm_node, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800607 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800608
609 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700610 do {
611 send_entry = cm_node->send_entry;
612 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600613 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800614 if (time_after(send_entry->timetosend, jiffies)) {
615 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700616 if ((nexttimeout >
617 send_entry->timetosend) ||
618 !settimer) {
619 nexttimeout =
620 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800621 settimer = 1;
622 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800623 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700624 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800625 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800626 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800627 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700628
629 if ((cm_node->state == NES_CM_STATE_TSA) ||
630 (cm_node->state == NES_CM_STATE_CLOSED)) {
631 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600632 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800633 }
634
Faisal Latif6492cdf2008-07-24 20:50:45 -0700635 if (!send_entry->retranscount ||
636 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800637 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700638 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800639
Faisal Latif6492cdf2008-07-24 20:50:45 -0700640 spin_unlock_irqrestore(
641 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800642 nes_retrans_expired(cm_node);
643 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700644 spin_lock_irqsave(&cm_node->retrans_list_lock,
645 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600646 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800647 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800648 atomic_inc(&send_entry->skb->users);
649 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700650 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
651 "for node %p, jiffies = %lu, time to send = "
652 "%lu, retranscount = %u, send_entry->seq_num = "
653 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
654 "0x%08X\n", send_entry, cm_node, jiffies,
655 send_entry->timetosend,
656 send_entry->retranscount,
657 send_entry->seq_num,
658 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800659
Faisal Latif6492cdf2008-07-24 20:50:45 -0700660 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
661 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800662 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700663 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800664 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700665 nes_debug(NES_DBG_CM, "rexmit failed for "
666 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800667 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800668 send_entry->retrycount--;
669 nexttimeout = jiffies + NES_SHORT_TIME;
670 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600671 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800672 } else {
673 cm_packets_sent++;
674 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700675 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
676 "%u, retry count = %u.\n",
677 send_entry->retranscount,
678 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800679 if (send_entry->send_retrans) {
680 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700681 timetosend = (NES_RETRY_TIMEOUT <<
682 (NES_DEFAULT_RETRANS - send_entry->retranscount));
683
Faisal Latif6492cdf2008-07-24 20:50:45 -0700684 send_entry->timetosend = jiffies +
Faisal Latif4e9c3902009-04-27 13:39:36 -0700685 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700686 if (nexttimeout > send_entry->timetosend ||
687 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800688 nexttimeout = send_entry->timetosend;
689 settimer = 1;
690 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800691 } else {
692 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700693 close_when_complete =
694 send_entry->close_when_complete;
695 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
696 cm_node, cm_node->state);
697 free_retrans_entry(cm_node);
698 if (close_when_complete)
699 rem_ref_cm_node(cm_node->cm_core,
700 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800701 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700702 } while (0);
703
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800704 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700705 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800706 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800707
708 if (settimer) {
709 if (!timer_pending(&cm_core->tcp_timer)) {
710 cm_core->tcp_timer.expires = nexttimeout;
711 add_timer(&cm_core->tcp_timer);
712 }
713 }
714}
715
716
717/**
718 * send_syn
719 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700720static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
721 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800722{
723 int ret;
724 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800725 char optionsbuffer[sizeof(struct option_mss) +
Faisal Latif6492cdf2008-07-24 20:50:45 -0700726 sizeof(struct option_windowscale) + sizeof(struct option_base) +
727 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800728
729 int optionssize = 0;
730 /* Sending MSS option */
731 union all_known_options *options;
732
733 if (!cm_node)
734 return -EINVAL;
735
736 options = (union all_known_options *)&optionsbuffer[optionssize];
737 options->as_mss.optionnum = OPTION_NUMBER_MSS;
738 options->as_mss.length = sizeof(struct option_mss);
739 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
740 optionssize += sizeof(struct option_mss);
741
742 options = (union all_known_options *)&optionsbuffer[optionssize];
743 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
744 options->as_windowscale.length = sizeof(struct option_windowscale);
745 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
746 optionssize += sizeof(struct option_windowscale);
747
Faisal Latif6492cdf2008-07-24 20:50:45 -0700748 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800749 options = (union all_known_options *)&optionsbuffer[optionssize];
750 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
751 options->as_base.length = sizeof(struct option_base);
752 optionssize += sizeof(struct option_base);
753 /* we need the size to be a multiple of 4 */
754 options = (union all_known_options *)&optionsbuffer[optionssize];
755 options->as_end = 1;
756 optionssize += 1;
757 options = (union all_known_options *)&optionsbuffer[optionssize];
758 options->as_end = 1;
759 optionssize += 1;
760 }
761
762 options = (union all_known_options *)&optionsbuffer[optionssize];
763 options->as_end = OPTION_NUMBER_END;
764 optionssize += 1;
765
Faisal Latif6492cdf2008-07-24 20:50:45 -0700766 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800767 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800768 if (!skb) {
769 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
770 return -1;
771 }
772
773 if (sendack)
774 flags |= SET_ACK;
775
776 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
777 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
778
779 return ret;
780}
781
782
783/**
784 * send_reset
785 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700786static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800787{
788 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800789 int flags = SET_RST | SET_ACK;
790
Faisal Latif6492cdf2008-07-24 20:50:45 -0700791 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800792 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800793 if (!skb) {
794 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -0800795 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800796 }
797
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800798 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
799 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
800
801 return ret;
802}
803
804
805/**
806 * send_ack
807 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700808static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800809{
810 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700811
812 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800813 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800814
815 if (!skb) {
816 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
817 return -1;
818 }
819
820 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
821 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
822
823 return ret;
824}
825
826
827/**
828 * send_fin
829 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700830static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800831{
832 int ret;
833
834 /* if we didn't get a frame get one */
835 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800836 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800837
838 if (!skb) {
839 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
840 return -1;
841 }
842
843 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
844 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
845
846 return ret;
847}
848
849
850/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800851 * find_node - find a cm node that matches the reference cm node
852 */
853static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
854 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
855{
856 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800857 struct list_head *hte;
858 struct nes_cm_node *cm_node;
859
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800860 /* get a handle on the hte */
861 hte = &cm_core->connected_nodes;
862
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800863 /* walk list and find cm_node associated with this session ID */
864 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700865 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800866 /* compare quad, return node handle if a match */
867 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
868 cm_node->loc_addr, cm_node->loc_port,
869 loc_addr, loc_port,
870 cm_node->rem_addr, cm_node->rem_port,
871 rem_addr, rem_port);
872 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
873 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
874 add_ref_cm_node(cm_node);
875 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
876 return cm_node;
877 }
878 }
879 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
880
881 /* no owner node */
882 return NULL;
883}
884
885
886/**
887 * find_listener - find a cm node listening on this addr-port pair
888 */
889static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
890 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
891{
892 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800893 struct nes_cm_listener *listen_node;
894
895 /* walk list and find cm_node associated with this session ID */
896 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700897 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800898 /* compare node pair, return node handle if a match */
899 if (((listen_node->loc_addr == dst_addr) ||
900 listen_node->loc_addr == 0x00000000) &&
901 (listen_node->loc_port == dst_port) &&
902 (listener_state & listen_node->listener_state)) {
903 atomic_inc(&listen_node->ref_count);
904 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
905 return listen_node;
906 }
907 }
908 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
909
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800910 /* no listener */
911 return NULL;
912}
913
914
915/**
916 * add_hte_node - add a cm node to the hash table
917 */
918static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
919{
920 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800921 struct list_head *hte;
922
923 if (!cm_node || !cm_core)
924 return -EINVAL;
925
Faisal Latif6492cdf2008-07-24 20:50:45 -0700926 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
927 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800928
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800929 spin_lock_irqsave(&cm_core->ht_lock, flags);
930
931 /* get a handle on the hash table element (list head for this slot) */
932 hte = &cm_core->connected_nodes;
933 list_add_tail(&cm_node->list, hte);
934 atomic_inc(&cm_core->ht_node_cnt);
935
936 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
937
938 return 0;
939}
940
941
942/**
943 * mini_cm_dec_refcnt_listen
944 */
945static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700946 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800947{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800948 int ret = -EINVAL;
949 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800950 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700951 struct list_head *list_pos = NULL;
952 struct list_head *list_temp = NULL;
953 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -0600954 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700955
956 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
957 "refcnt=%d\n", listener, free_hanging_nodes,
958 atomic_read(&listener->ref_count));
959 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -0600960 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700961 if (free_hanging_nodes) {
962 spin_lock_irqsave(&cm_core->ht_lock, flags);
963 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600964 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700965 cm_node = container_of(list_pos, struct nes_cm_node,
966 list);
967 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -0600968 (!cm_node->accelerated)) {
969 add_ref_cm_node(cm_node);
970 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700971 }
972 }
973 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
974 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600975
976 list_for_each_safe(list_pos, list_temp, &reset_list) {
977 cm_node = container_of(list_pos, struct nes_cm_node,
Faisal Latif9d5ab132009-03-06 15:15:01 -0800978 reset_entry);
979 {
980 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Faisal Latifc5a7d482009-12-09 15:54:08 -0800981 enum nes_cm_node_state old_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800982 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
983 rem_ref_cm_node(cm_node->cm_core, cm_node);
984 } else {
985 if (!loopback) {
986 cleanup_retrans_entry(cm_node);
987 err = send_reset(cm_node, NULL);
988 if (err) {
989 cm_node->state =
990 NES_CM_STATE_CLOSED;
991 WARN_ON(1);
992 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -0800993 old_state = cm_node->state;
994 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
995 if (old_state != NES_CM_STATE_MPAREQ_RCVD)
996 rem_ref_cm_node(
997 cm_node->cm_core,
998 cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800999 }
1000 } else {
1001 struct nes_cm_event event;
1002
1003 event.cm_node = loopback;
1004 event.cm_info.rem_addr =
1005 loopback->rem_addr;
1006 event.cm_info.loc_addr =
1007 loopback->loc_addr;
1008 event.cm_info.rem_port =
1009 loopback->rem_port;
1010 event.cm_info.loc_port =
1011 loopback->loc_port;
1012 event.cm_info.cm_id = loopback->cm_id;
1013 cm_event_connect_error(&event);
Faisal Latifc5a7d482009-12-09 15:54:08 -08001014 cm_node->state = NES_CM_STATE_LISTENER_DESTROYED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001015 loopback->state = NES_CM_STATE_CLOSED;
1016
1017 event.cm_node = cm_node;
1018 event.cm_info.rem_addr =
1019 cm_node->rem_addr;
1020 event.cm_info.loc_addr =
1021 cm_node->loc_addr;
1022 event.cm_info.rem_port =
1023 cm_node->rem_port;
1024 event.cm_info.loc_port =
1025 cm_node->loc_port;
1026 event.cm_info.cm_id = cm_node->cm_id;
1027 cm_event_reset(&event);
1028
1029 rem_ref_cm_node(cm_node->cm_core,
1030 cm_node);
1031
1032 }
1033 }
1034 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001035 }
1036
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001037 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1038 if (!atomic_dec_return(&listener->ref_count)) {
1039 list_del(&listener->list);
1040
1041 /* decrement our listen node count */
1042 atomic_dec(&cm_core->listen_node_cnt);
1043
1044 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1045
1046 if (listener->nesvnic) {
1047 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1048 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1049 }
1050
1051 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1052
1053 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001054 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001055 ret = 0;
1056 cm_listens_destroyed++;
1057 } else {
1058 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1059 }
1060 if (listener) {
1061 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1062 nes_debug(NES_DBG_CM, "destroying listener (%p)"
1063 " with non-zero pending accepts=%u\n",
1064 listener, atomic_read(&listener->pend_accepts_cnt));
1065 }
1066
1067 return ret;
1068}
1069
1070
1071/**
1072 * mini_cm_del_listen
1073 */
1074static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1075 struct nes_cm_listener *listener)
1076{
1077 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1078 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1079 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1080}
1081
1082
1083/**
1084 * mini_cm_accelerated
1085 */
1086static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1087 struct nes_cm_node *cm_node)
1088{
1089 u32 was_timer_set;
1090 cm_node->accelerated = 1;
1091
1092 if (cm_node->accept_pend) {
1093 BUG_ON(!cm_node->listener);
1094 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001095 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001096 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1097 }
1098
1099 was_timer_set = timer_pending(&cm_core->tcp_timer);
1100 if (!was_timer_set) {
1101 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1102 add_timer(&cm_core->tcp_timer);
1103 }
1104
1105 return 0;
1106}
1107
1108
1109/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001110 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001111 */
Bob Sharp7191a0a2008-10-03 12:21:19 -07001112static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001113{
1114 struct rtable *rt;
1115 struct flowi fl;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001116 struct neighbour *neigh;
1117 int rc = -1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001118
1119 memset(&fl, 0, sizeof fl);
1120 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1121 if (ip_route_output_key(&init_net, &rt, &fl)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001122 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001123 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001124 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001125 }
1126
Bob Sharp7191a0a2008-10-03 12:21:19 -07001127 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1128 if (neigh) {
1129 if (neigh->nud_state & NUD_VALID) {
1130 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001131 " is %pM, Gateway is 0x%08X \n", dst_ip,
1132 neigh->ha, ntohl(rt->rt_gateway));
Bob Sharp7191a0a2008-10-03 12:21:19 -07001133 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1134 dst_ip, NES_ARP_ADD);
1135 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1136 NES_ARP_RESOLVE);
1137 }
1138 neigh_release(neigh);
1139 }
1140
1141 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1142 neigh_event_send(rt->u.dst.neighbour, NULL);
1143
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001144 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001145 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001146}
1147
1148
1149/**
1150 * make_cm_node - create a new instance of a cm node
1151 */
1152static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1153 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1154 struct nes_cm_listener *listener)
1155{
1156 struct nes_cm_node *cm_node;
1157 struct timespec ts;
1158 int arpindex = 0;
1159 struct nes_device *nesdev;
1160 struct nes_adapter *nesadapter;
1161
1162 /* create an hte and cm_node for this instance */
1163 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1164 if (!cm_node)
1165 return NULL;
1166
1167 /* set our node specific transport info */
1168 cm_node->loc_addr = cm_info->loc_addr;
1169 cm_node->rem_addr = cm_info->rem_addr;
1170 cm_node->loc_port = cm_info->loc_port;
1171 cm_node->rem_port = cm_info->rem_port;
1172 cm_node->send_write0 = send_first;
Harvey Harrison63779432008-10-31 00:56:00 -07001173 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1174 &cm_node->loc_addr, cm_node->loc_port,
1175 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001176 cm_node->listener = listener;
1177 cm_node->netdev = nesvnic->netdev;
1178 cm_node->cm_id = cm_info->cm_id;
1179 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1180
Faisal Latif6492cdf2008-07-24 20:50:45 -07001181 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1182 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001183
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001184 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001185
1186 cm_node->loopbackpartner = NULL;
1187 atomic_set(&cm_node->ref_count, 1);
1188 /* associate our parent CM core */
1189 cm_node->cm_core = cm_core;
1190 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1191 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1192 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1193 NES_CM_DEFAULT_RCV_WND_SCALE;
1194 ts = current_kernel_time();
1195 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1196 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Chien Tungf2b2b592008-03-20 19:55:30 -05001197 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001198 cm_node->tcp_cntxt.rcv_nxt = 0;
1199 /* get a unique session ID , add thread_id to an upcounter to handle race */
1200 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001201 cm_node->conn_type = cm_info->conn_type;
1202 cm_node->apbvt_set = 0;
1203 cm_node->accept_pend = 0;
1204
1205 cm_node->nesvnic = nesvnic;
1206 /* get some device handles, for arp lookup */
1207 nesdev = nesvnic->nesdev;
1208 nesadapter = nesdev->nesadapter;
1209
1210 cm_node->loopbackpartner = NULL;
1211 /* get the mac addr for the remote node */
Chien Tung1ee86552008-11-21 20:51:04 -06001212 if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1213 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
1214 else
1215 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001216 if (arpindex < 0) {
Bob Sharp7191a0a2008-10-03 12:21:19 -07001217 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr);
1218 if (arpindex < 0) {
1219 kfree(cm_node);
1220 return NULL;
1221 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001222 }
1223
1224 /* copy the mac addr to node context */
1225 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001226 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1227 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001228
1229 add_hte_node(cm_core, cm_node);
1230 atomic_inc(&cm_nodes_created);
1231
1232 return cm_node;
1233}
1234
1235
1236/**
1237 * add_ref_cm_node - destroy an instance of a cm node
1238 */
1239static int add_ref_cm_node(struct nes_cm_node *cm_node)
1240{
1241 atomic_inc(&cm_node->ref_count);
1242 return 0;
1243}
1244
1245
1246/**
1247 * rem_ref_cm_node - destroy an instance of a cm node
1248 */
1249static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001250 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001251{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001252 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001253 struct nes_qp *nesqp;
1254
1255 if (!cm_node)
1256 return -EINVAL;
1257
1258 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1259 if (atomic_dec_return(&cm_node->ref_count)) {
1260 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1261 return 0;
1262 }
1263 list_del(&cm_node->list);
1264 atomic_dec(&cm_core->ht_node_cnt);
1265 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1266
1267 /* if the node is destroyed before connection was accelerated */
1268 if (!cm_node->accelerated && cm_node->accept_pend) {
1269 BUG_ON(!cm_node->listener);
1270 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1271 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1272 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001273 WARN_ON(cm_node->send_entry);
1274 if (cm_node->recv_entry)
1275 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001276 if (cm_node->listener) {
1277 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1278 } else {
1279 if (cm_node->apbvt_set && cm_node->nesvnic) {
1280 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001281 PCI_FUNC(
1282 cm_node->nesvnic->nesdev->pcidev->devfn),
1283 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001284 }
1285 }
1286
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001287 atomic_dec(&cm_core->node_cnt);
1288 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001289 nesqp = cm_node->nesqp;
1290 if (nesqp) {
1291 nesqp->cm_node = NULL;
1292 nes_rem_ref(&nesqp->ibqp);
1293 cm_node->nesqp = NULL;
1294 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001295
Faisal Latif6492cdf2008-07-24 20:50:45 -07001296 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001297 return 0;
1298}
1299
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001300/**
1301 * process_options
1302 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001303static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1304 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001305{
1306 u32 tmp;
1307 u32 offset = 0;
1308 union all_known_options *all_options;
1309 char got_mss_option = 0;
1310
1311 while (offset < optionsize) {
1312 all_options = (union all_known_options *)(optionsloc + offset);
1313 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001314 case OPTION_NUMBER_END:
1315 offset = optionsize;
1316 break;
1317 case OPTION_NUMBER_NONE:
1318 offset += 1;
1319 continue;
1320 case OPTION_NUMBER_MSS:
1321 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1322 "Size: %d\n", __func__,
1323 all_options->as_mss.length, offset, optionsize);
1324 got_mss_option = 1;
1325 if (all_options->as_mss.length != 4) {
1326 return 1;
1327 } else {
1328 tmp = ntohs(all_options->as_mss.mss);
1329 if (tmp > 0 && tmp <
1330 cm_node->tcp_cntxt.mss)
1331 cm_node->tcp_cntxt.mss = tmp;
1332 }
1333 break;
1334 case OPTION_NUMBER_WINDOW_SCALE:
1335 cm_node->tcp_cntxt.snd_wscale =
1336 all_options->as_windowscale.shiftcount;
1337 break;
1338 case OPTION_NUMBER_WRITE0:
1339 cm_node->send_write0 = 1;
1340 break;
1341 default:
1342 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1343 all_options->as_base.optionnum);
1344 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001345 }
1346 offset += all_options->as_base.length;
1347 }
1348 if ((!got_mss_option) && (syn_packet))
1349 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1350 return 0;
1351}
1352
Faisal Latif6492cdf2008-07-24 20:50:45 -07001353static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001354{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001355 atomic_inc(&cm_accel_dropped_pkts);
1356 dev_kfree_skb_any(skb);
1357}
1358
Faisal Latif9d5ab132009-03-06 15:15:01 -08001359static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001360{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001361 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1362 "refcnt=%d\n", cm_node, cm_node->state,
1363 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001364 switch (cm_node->state) {
1365 case NES_CM_STATE_SYN_RCVD:
1366 case NES_CM_STATE_SYN_SENT:
1367 case NES_CM_STATE_ESTABLISHED:
1368 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001369 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001370 cm_node->tcp_cntxt.rcv_nxt++;
1371 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001372 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001373 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001374 break;
1375 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001376 cm_node->tcp_cntxt.rcv_nxt++;
1377 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001378 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001379 send_ack(cm_node, NULL);
1380 /* Wait for ACK as this is simultanous close..
1381 * After we receive ACK, do not send anything..
1382 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001383 break;
1384 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001385 cm_node->tcp_cntxt.rcv_nxt++;
1386 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001387 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001388 send_ack(cm_node, NULL);
1389 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1390 break;
1391 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001392 cm_node->tcp_cntxt.rcv_nxt++;
1393 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001394 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001395 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001396 break;
1397 case NES_CM_STATE_TSA:
1398 default:
1399 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1400 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001401 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001402 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001403}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001404
Faisal Latif6492cdf2008-07-24 20:50:45 -07001405
1406static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1407 struct tcphdr *tcph)
1408{
1409
1410 int reset = 0; /* whether to send reset in case of err.. */
Faisal Latif183ecfa2008-11-21 20:50:46 -06001411 int passive_state;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001412 atomic_inc(&cm_resets_recvd);
1413 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1414 " refcnt=%d\n", cm_node, cm_node->state,
1415 atomic_read(&cm_node->ref_count));
1416 cleanup_retrans_entry(cm_node);
1417 switch (cm_node->state) {
1418 case NES_CM_STATE_SYN_SENT:
1419 case NES_CM_STATE_MPAREQ_SENT:
1420 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1421 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1422 cm_node->listener, cm_node->state);
1423 active_open_err(cm_node, skb, reset);
1424 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001425 case NES_CM_STATE_MPAREQ_RCVD:
1426 passive_state = atomic_add_return(1, &cm_node->passive_state);
1427 if (passive_state == NES_SEND_RESET_EVENT)
1428 create_event(cm_node, NES_CM_EVENT_RESET);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001429 cm_node->state = NES_CM_STATE_CLOSED;
1430 dev_kfree_skb_any(skb);
1431 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001432 case NES_CM_STATE_ESTABLISHED:
1433 case NES_CM_STATE_SYN_RCVD:
1434 case NES_CM_STATE_LISTENING:
1435 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1436 passive_open_err(cm_node, skb, reset);
1437 break;
1438 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001439 active_open_err(cm_node, skb, reset);
1440 break;
1441 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001442 drop_packet(skb);
1443 break;
Faisal Latif68237a02009-06-22 22:53:28 -07001444 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001445 case NES_CM_STATE_LAST_ACK:
1446 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001447 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001448 cm_node->state = NES_CM_STATE_CLOSED;
1449 rem_ref_cm_node(cm_node->cm_core, cm_node);
1450 drop_packet(skb);
1451 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001452 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001453 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001454 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001455 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001456}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001457
Faisal Latif9d5ab132009-03-06 15:15:01 -08001458
1459static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001460{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001461
Faisal Latif9d5ab132009-03-06 15:15:01 -08001462 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001463 int datasize = skb->len;
1464 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001465
1466 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1467 u32 res_type;
1468 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1469 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001470 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001471 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001472 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1473 "cm_node=%p listener=%p state=%d\n", __func__,
1474 __LINE__, cm_node, cm_node->listener,
1475 cm_node->state);
1476 active_open_err(cm_node, skb, 1);
1477 } else {
1478 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001479 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001480 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001481 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001482
1483 switch (cm_node->state) {
1484 case NES_CM_STATE_ESTABLISHED:
1485 if (res_type == NES_MPA_REQUEST_REJECT) {
1486 /*BIG problem as we are receiving the MPA.. So should
1487 * not be REJECT.. This is Passive Open.. We can
1488 * only receive it Reject for Active Open...*/
1489 WARN_ON(1);
1490 }
1491 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1492 type = NES_CM_EVENT_MPA_REQ;
1493 atomic_set(&cm_node->passive_state,
1494 NES_PASSIVE_STATE_INDICATED);
1495 break;
1496 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001497 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001498 if (res_type == NES_MPA_REQUEST_REJECT) {
1499 type = NES_CM_EVENT_MPA_REJECT;
1500 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1501 } else {
1502 type = NES_CM_EVENT_CONNECTED;
1503 cm_node->state = NES_CM_STATE_TSA;
1504 }
1505
1506 break;
1507 default:
1508 WARN_ON(1);
1509 break;
1510 }
1511 dev_kfree_skb_any(skb);
1512 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001513}
1514
1515static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1516{
1517 switch (cm_node->state) {
1518 case NES_CM_STATE_SYN_SENT:
1519 case NES_CM_STATE_MPAREQ_SENT:
1520 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1521 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1522 cm_node->listener, cm_node->state);
1523 active_open_err(cm_node, skb, 1);
1524 break;
1525 case NES_CM_STATE_ESTABLISHED:
1526 case NES_CM_STATE_SYN_RCVD:
1527 passive_open_err(cm_node, skb, 1);
1528 break;
1529 case NES_CM_STATE_TSA:
1530 default:
1531 drop_packet(skb);
1532 }
1533}
1534
1535static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1536 struct sk_buff *skb)
1537{
1538 int err;
1539
1540 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1541 if (err)
1542 active_open_err(cm_node, skb, 1);
1543
1544 return err;
1545}
1546
1547static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1548 struct sk_buff *skb)
1549{
1550 int err = 0;
1551 u32 seq;
1552 u32 ack_seq;
1553 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1554 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1555 u32 rcv_wnd;
1556 seq = ntohl(tcph->seq);
1557 ack_seq = ntohl(tcph->ack_seq);
1558 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1559 if (ack_seq != loc_seq_num)
1560 err = 1;
Faisal Latifc11470f2009-04-27 13:38:31 -07001561 else if (!between(seq, rcv_nxt, (rcv_nxt+rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001562 err = 1;
1563 if (err) {
1564 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1565 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1566 cm_node->listener, cm_node->state);
1567 indicate_pkt_err(cm_node, skb);
1568 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1569 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1570 rcv_wnd);
1571 }
1572 return err;
1573}
1574
1575/*
1576 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1577 * is created with a listener or it may comein as rexmitted packet which in
1578 * that case will be just dropped.
1579 */
1580
1581static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1582 struct tcphdr *tcph)
1583{
1584 int ret;
1585 u32 inc_sequence;
1586 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001587
1588 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001589 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001590 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001591
Faisal Latif6492cdf2008-07-24 20:50:45 -07001592 switch (cm_node->state) {
1593 case NES_CM_STATE_SYN_SENT:
1594 case NES_CM_STATE_MPAREQ_SENT:
1595 /* Rcvd syn on active open connection*/
1596 active_open_err(cm_node, skb, 1);
1597 break;
1598 case NES_CM_STATE_LISTENING:
1599 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001600 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1601 cm_node->listener->backlog) {
1602 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1603 "pressure \n");
1604 cm_backlog_drops++;
1605 passive_open_err(cm_node, skb, 0);
1606 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001607 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001608 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1609 1);
1610 if (ret) {
1611 passive_open_err(cm_node, skb, 0);
1612 /* drop pkt */
1613 break;
1614 }
1615 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1616 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001617 cm_node->accept_pend = 1;
1618 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1619
Faisal Latif6492cdf2008-07-24 20:50:45 -07001620 cm_node->state = NES_CM_STATE_SYN_RCVD;
1621 send_syn(cm_node, 1, skb);
1622 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001623 case NES_CM_STATE_CLOSED:
1624 cleanup_retrans_entry(cm_node);
1625 send_reset(cm_node, skb);
1626 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001627 case NES_CM_STATE_TSA:
1628 case NES_CM_STATE_ESTABLISHED:
1629 case NES_CM_STATE_FIN_WAIT1:
1630 case NES_CM_STATE_FIN_WAIT2:
1631 case NES_CM_STATE_MPAREQ_RCVD:
1632 case NES_CM_STATE_LAST_ACK:
1633 case NES_CM_STATE_CLOSING:
1634 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001635 default:
1636 drop_packet(skb);
1637 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001638 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001639}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001640
Faisal Latif6492cdf2008-07-24 20:50:45 -07001641static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1642 struct tcphdr *tcph)
1643{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001644
Faisal Latif6492cdf2008-07-24 20:50:45 -07001645 int ret;
1646 u32 inc_sequence;
1647 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001648
Faisal Latif6492cdf2008-07-24 20:50:45 -07001649 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001650 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001651 inc_sequence = ntohl(tcph->seq);
1652 switch (cm_node->state) {
1653 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001654 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001655 /* active open */
1656 if (check_syn(cm_node, tcph, skb))
1657 return;
1658 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1659 /* setup options */
1660 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1661 if (ret) {
1662 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1663 cm_node);
1664 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001665 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001666 cleanup_retrans_entry(cm_node);
1667 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1668 send_mpa_request(cm_node, skb);
1669 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1670 break;
1671 case NES_CM_STATE_MPAREQ_RCVD:
1672 /* passive open, so should not be here */
1673 passive_open_err(cm_node, skb, 1);
1674 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001675 case NES_CM_STATE_LISTENING:
1676 case NES_CM_STATE_CLOSED:
1677 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1678 cleanup_retrans_entry(cm_node);
1679 send_reset(cm_node, skb);
1680 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001681 case NES_CM_STATE_ESTABLISHED:
1682 case NES_CM_STATE_FIN_WAIT1:
1683 case NES_CM_STATE_FIN_WAIT2:
1684 case NES_CM_STATE_LAST_ACK:
1685 case NES_CM_STATE_TSA:
1686 case NES_CM_STATE_CLOSING:
1687 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001688 case NES_CM_STATE_MPAREQ_SENT:
1689 default:
1690 drop_packet(skb);
1691 break;
1692 }
1693}
1694
Faisal Latif109d67e2009-04-27 13:41:06 -07001695static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001696 struct tcphdr *tcph)
1697{
1698 int datasize = 0;
1699 u32 inc_sequence;
1700 u32 rem_seq_ack;
1701 u32 rem_seq;
Faisal Latif109d67e2009-04-27 13:41:06 -07001702 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06001703 int optionsize;
Faisal Latifabb77252008-11-21 20:50:52 -06001704 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001705
Faisal Latif6492cdf2008-07-24 20:50:45 -07001706 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07001707 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001708
1709 skb_pull(skb, tcph->doff << 2);
1710 inc_sequence = ntohl(tcph->seq);
1711 rem_seq = ntohl(tcph->seq);
1712 rem_seq_ack = ntohl(tcph->ack_seq);
1713 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001714 switch (cm_node->state) {
1715 case NES_CM_STATE_SYN_RCVD:
1716 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07001717 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001718 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1719 if (ret)
1720 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001721 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1722 cm_node->state = NES_CM_STATE_ESTABLISHED;
1723 if (datasize) {
1724 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001725 handle_rcv_mpa(cm_node, skb);
Faisal Latif109d67e2009-04-27 13:41:06 -07001726 } else /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001727 dev_kfree_skb_any(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001728 break;
1729 case NES_CM_STATE_ESTABLISHED:
1730 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001731 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001732 if (datasize) {
1733 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001734 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001735 } else
1736 drop_packet(skb);
1737 break;
1738 case NES_CM_STATE_MPAREQ_SENT:
1739 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1740 if (datasize) {
1741 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001742 handle_rcv_mpa(cm_node, skb);
Faisal Latif109d67e2009-04-27 13:41:06 -07001743 } else /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001744 dev_kfree_skb_any(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001745 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001746 case NES_CM_STATE_LISTENING:
1747 case NES_CM_STATE_CLOSED:
1748 cleanup_retrans_entry(cm_node);
1749 send_reset(cm_node, skb);
1750 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001751 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07001752 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001753 cleanup_retrans_entry(cm_node);
1754 cm_node->state = NES_CM_STATE_CLOSED;
1755 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001756 rem_ref_cm_node(cm_node->cm_core, cm_node);
1757 drop_packet(skb);
1758 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001759 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001760 cleanup_retrans_entry(cm_node);
1761 drop_packet(skb);
1762 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1763 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001764 case NES_CM_STATE_SYN_SENT:
1765 case NES_CM_STATE_FIN_WAIT2:
1766 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001767 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001768 case NES_CM_STATE_UNKNOWN:
1769 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07001770 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001771 drop_packet(skb);
1772 break;
1773 }
Faisal Latif109d67e2009-04-27 13:41:06 -07001774 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001775}
1776
1777
1778
1779static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1780 struct sk_buff *skb, int optionsize, int passive)
1781{
1782 u8 *optionsloc = (u8 *)&tcph[1];
1783 if (optionsize) {
1784 if (process_options(cm_node, optionsloc, optionsize,
1785 (u32)tcph->syn)) {
1786 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1787 __func__, cm_node);
1788 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06001789 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001790 else
Faisal Latifabb77252008-11-21 20:50:52 -06001791 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001792 return 1;
1793 }
1794 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001795
1796 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1797 cm_node->tcp_cntxt.snd_wscale;
1798
Faisal Latif6492cdf2008-07-24 20:50:45 -07001799 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001800 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001801 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001802}
1803
Faisal Latif6492cdf2008-07-24 20:50:45 -07001804/*
1805 * active_open_err() will send reset() if flag set..
1806 * It will also send ABORT event.
1807 */
1808
1809static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1810 int reset)
1811{
1812 cleanup_retrans_entry(cm_node);
1813 if (reset) {
1814 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1815 "state=%d\n", cm_node, cm_node->state);
1816 add_ref_cm_node(cm_node);
1817 send_reset(cm_node, skb);
1818 } else
1819 dev_kfree_skb_any(skb);
1820
1821 cm_node->state = NES_CM_STATE_CLOSED;
1822 create_event(cm_node, NES_CM_EVENT_ABORTED);
1823}
1824
1825/*
1826 * passive_open_err() will either do a reset() or will free up the skb and
1827 * remove the cm_node.
1828 */
1829
1830static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1831 int reset)
1832{
1833 cleanup_retrans_entry(cm_node);
1834 cm_node->state = NES_CM_STATE_CLOSED;
1835 if (reset) {
1836 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1837 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1838 send_reset(cm_node, skb);
1839 } else {
1840 dev_kfree_skb_any(skb);
1841 rem_ref_cm_node(cm_node->cm_core, cm_node);
1842 }
1843}
1844
1845/*
1846 * free_retrans_entry() routines assumes that the retrans_list_lock has
1847 * been acquired before calling.
1848 */
1849static void free_retrans_entry(struct nes_cm_node *cm_node)
1850{
1851 struct nes_timer_entry *send_entry;
1852 send_entry = cm_node->send_entry;
1853 if (send_entry) {
1854 cm_node->send_entry = NULL;
1855 dev_kfree_skb_any(send_entry->skb);
1856 kfree(send_entry);
1857 rem_ref_cm_node(cm_node->cm_core, cm_node);
1858 }
1859}
1860
1861static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1862{
1863 unsigned long flags;
1864
1865 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1866 free_retrans_entry(cm_node);
1867 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1868}
1869
1870/**
1871 * process_packet
1872 * Returns skb if to be freed, else it will return NULL if already used..
1873 */
1874static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1875 struct nes_cm_core *cm_core)
1876{
1877 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1878 struct tcphdr *tcph = tcp_hdr(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001879 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07001880 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001881 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1882
1883 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1884 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1885 tcph->ack, tcph->rst, tcph->fin);
1886
1887 if (tcph->rst)
1888 pkt_type = NES_PKT_TYPE_RST;
1889 else if (tcph->syn) {
1890 pkt_type = NES_PKT_TYPE_SYN;
1891 if (tcph->ack)
1892 pkt_type = NES_PKT_TYPE_SYNACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001893 } else if (tcph->ack)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001894 pkt_type = NES_PKT_TYPE_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001895 if (tcph->fin)
1896 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001897
1898 switch (pkt_type) {
1899 case NES_PKT_TYPE_SYN:
1900 handle_syn_pkt(cm_node, skb, tcph);
1901 break;
1902 case NES_PKT_TYPE_SYNACK:
1903 handle_synack_pkt(cm_node, skb, tcph);
1904 break;
1905 case NES_PKT_TYPE_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07001906 ret = handle_ack_pkt(cm_node, skb, tcph);
1907 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001908 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001909 break;
1910 case NES_PKT_TYPE_RST:
1911 handle_rst_pkt(cm_node, skb, tcph);
1912 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001913 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07001914 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08001915 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07001916 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001917 break;
1918 }
1919}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001920
1921/**
1922 * mini_cm_listen - create a listen node with params
1923 */
1924static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001925 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001926{
1927 struct nes_cm_listener *listener;
1928 unsigned long flags;
1929
1930 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1931 cm_info->loc_addr, cm_info->loc_port);
1932
1933 /* cannot have multiple matching listeners */
1934 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1935 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1936 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1937 /* find automatically incs ref count ??? */
1938 atomic_dec(&listener->ref_count);
1939 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1940 return NULL;
1941 }
1942
1943 if (!listener) {
1944 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1945 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1946 if (!listener) {
1947 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1948 return NULL;
1949 }
1950
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001951 listener->loc_addr = htonl(cm_info->loc_addr);
1952 listener->loc_port = htons(cm_info->loc_port);
1953 listener->reused_node = 0;
1954
1955 atomic_set(&listener->ref_count, 1);
1956 }
1957 /* pasive case */
1958 /* find already inc'ed the ref count */
1959 else {
1960 listener->reused_node = 1;
1961 }
1962
1963 listener->cm_id = cm_info->cm_id;
1964 atomic_set(&listener->pend_accepts_cnt, 0);
1965 listener->cm_core = cm_core;
1966 listener->nesvnic = nesvnic;
1967 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001968
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001969 listener->conn_type = cm_info->conn_type;
1970 listener->backlog = cm_info->backlog;
1971 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1972
1973 if (!listener->reused_node) {
1974 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1975 list_add(&listener->list, &cm_core->listen_list.list);
1976 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1977 atomic_inc(&cm_core->listen_node_cnt);
1978 }
1979
1980 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1981 " listener = %p, backlog = %d, cm_id = %p.\n",
1982 cm_info->loc_addr, cm_info->loc_port,
1983 listener, listener->backlog, listener->cm_id);
1984
1985 return listener;
1986}
1987
1988
1989/**
1990 * mini_cm_connect - make a connection node with params
1991 */
Faisal Latif54c86a82008-09-30 14:47:27 -07001992static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001993 struct nes_vnic *nesvnic, u16 private_data_len,
1994 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001995{
1996 int ret = 0;
1997 struct nes_cm_node *cm_node;
1998 struct nes_cm_listener *loopbackremotelistener;
1999 struct nes_cm_node *loopbackremotenode;
2000 struct nes_cm_info loopback_cm_info;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002001 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
2002 struct ietf_mpa_frame *mpa_frame = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002003
2004 /* create a CM connection node */
2005 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2006 if (!cm_node)
2007 return NULL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002008 mpa_frame = &cm_node->mpa_frame;
Faisal Latif1cf078c2009-12-09 15:53:54 -08002009 memcpy(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002010 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
2011 mpa_frame->rev = IETF_MPA_VERSION;
2012 mpa_frame->priv_data_len = htons(private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002013
Glenn Streiff7495ab62008-04-29 13:46:54 -07002014 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002015 cm_node->tcp_cntxt.client = 1;
2016 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2017
2018 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002019 loopbackremotelistener = find_listener(cm_core,
2020 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2021 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002022 if (loopbackremotelistener == NULL) {
2023 create_event(cm_node, NES_CM_EVENT_ABORTED);
2024 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002025 loopback_cm_info = *cm_info;
2026 loopback_cm_info.loc_port = cm_info->rem_port;
2027 loopback_cm_info.rem_port = cm_info->loc_port;
2028 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002029 loopbackremotenode = make_cm_node(cm_core, nesvnic,
2030 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002031 if (!loopbackremotenode) {
2032 rem_ref_cm_node(cm_node->cm_core, cm_node);
2033 return NULL;
2034 }
2035 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002036 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002037 loopbackremotenode->tcp_cntxt.rcv_wscale =
2038 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002039 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002040 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2041 private_data_len);
2042 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002043
Faisal Latif6492cdf2008-07-24 20:50:45 -07002044 /* we are done handling this state. */
2045 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002046 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002047 cm_node->tcp_cntxt.rcv_nxt =
2048 loopbackremotenode->tcp_cntxt.loc_seq_num;
2049 loopbackremotenode->tcp_cntxt.rcv_nxt =
2050 cm_node->tcp_cntxt.loc_seq_num;
2051 cm_node->tcp_cntxt.max_snd_wnd =
2052 loopbackremotenode->tcp_cntxt.rcv_wnd;
2053 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2054 cm_node->tcp_cntxt.rcv_wnd;
2055 cm_node->tcp_cntxt.snd_wnd =
2056 loopbackremotenode->tcp_cntxt.rcv_wnd;
2057 loopbackremotenode->tcp_cntxt.snd_wnd =
2058 cm_node->tcp_cntxt.rcv_wnd;
2059 cm_node->tcp_cntxt.snd_wscale =
2060 loopbackremotenode->tcp_cntxt.rcv_wscale;
2061 loopbackremotenode->tcp_cntxt.snd_wscale =
2062 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002063 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002064 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2065 }
2066 return cm_node;
2067 }
2068
2069 /* set our node side to client (active) side */
2070 cm_node->tcp_cntxt.client = 1;
2071 /* init our MPA frame ptr */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002072 memcpy(mpa_frame->priv_data, private_data, private_data_len);
2073
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002074 cm_node->mpa_frame_size = mpa_frame_size;
2075
2076 /* send a syn and goto syn sent state */
2077 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002078 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002079
Faisal Latif6492cdf2008-07-24 20:50:45 -07002080 if (ret) {
2081 /* error in sending the syn free up the cm_node struct */
2082 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2083 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2084 cm_node->rem_addr, cm_node->rem_port, cm_node,
2085 cm_node->cm_id);
2086 rem_ref_cm_node(cm_node->cm_core, cm_node);
2087 cm_node = NULL;
2088 }
2089
2090 if (cm_node)
2091 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2092 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2093 cm_node->rem_addr, cm_node->rem_port, cm_node,
2094 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002095
2096 return cm_node;
2097}
2098
2099
2100/**
2101 * mini_cm_accept - accept a connection
2102 * This function is never called
2103 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002104static int mini_cm_accept(struct nes_cm_core *cm_core,
2105 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002106{
2107 return 0;
2108}
2109
2110
2111/**
2112 * mini_cm_reject - reject and teardown a connection
2113 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002114static int mini_cm_reject(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002115 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002116{
2117 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002118 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002119 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002120 struct nes_cm_event event;
2121 struct iw_cm_id *cm_id = cm_node->cm_id;
2122 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002123
Faisal Latif6492cdf2008-07-24 20:50:45 -07002124 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2125 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002126
Faisal Latif6492cdf2008-07-24 20:50:45 -07002127 if (cm_node->tcp_cntxt.client)
2128 return ret;
2129 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002130
Faisal Latif9d5ab132009-03-06 15:15:01 -08002131 if (!loopback) {
2132 passive_state = atomic_add_return(1, &cm_node->passive_state);
2133 if (passive_state == NES_SEND_RESET_EVENT) {
2134 cm_node->state = NES_CM_STATE_CLOSED;
2135 rem_ref_cm_node(cm_core, cm_node);
2136 } else {
Faisal Latifc5a7d482009-12-09 15:54:08 -08002137 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2138 rem_ref_cm_node(cm_core, cm_node);
2139 } else {
2140 ret = send_mpa_reject(cm_node);
2141 if (ret) {
2142 cm_node->state = NES_CM_STATE_CLOSED;
2143 err = send_reset(cm_node, NULL);
2144 if (err)
2145 WARN_ON(1);
2146 } else
2147 cm_id->add_ref(cm_id);
2148 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002149 }
2150 } else {
2151 cm_node->cm_id = NULL;
Faisal Latifc5a7d482009-12-09 15:54:08 -08002152 if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2153 rem_ref_cm_node(cm_core, cm_node);
2154 rem_ref_cm_node(cm_core, loopback);
2155 } else {
2156 event.cm_node = loopback;
2157 event.cm_info.rem_addr = loopback->rem_addr;
2158 event.cm_info.loc_addr = loopback->loc_addr;
2159 event.cm_info.rem_port = loopback->rem_port;
2160 event.cm_info.loc_port = loopback->loc_port;
2161 event.cm_info.cm_id = loopback->cm_id;
2162 cm_event_mpa_reject(&event);
2163 rem_ref_cm_node(cm_core, cm_node);
2164 loopback->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002165
Faisal Latifc5a7d482009-12-09 15:54:08 -08002166 cm_id = loopback->cm_id;
2167 rem_ref_cm_node(cm_core, loopback);
2168 cm_id->rem_ref(cm_id);
2169 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08002170 }
2171
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002172 return ret;
2173}
2174
2175
2176/**
2177 * mini_cm_close
2178 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002179static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002180{
2181 int ret = 0;
2182
2183 if (!cm_core || !cm_node)
2184 return -EINVAL;
2185
2186 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002187 case NES_CM_STATE_SYN_RCVD:
2188 case NES_CM_STATE_SYN_SENT:
2189 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2190 case NES_CM_STATE_ESTABLISHED:
2191 case NES_CM_STATE_ACCEPTING:
2192 case NES_CM_STATE_MPAREQ_SENT:
2193 case NES_CM_STATE_MPAREQ_RCVD:
2194 cleanup_retrans_entry(cm_node);
2195 send_reset(cm_node, NULL);
2196 break;
2197 case NES_CM_STATE_CLOSE_WAIT:
2198 cm_node->state = NES_CM_STATE_LAST_ACK;
2199 send_fin(cm_node, NULL);
2200 break;
2201 case NES_CM_STATE_FIN_WAIT1:
2202 case NES_CM_STATE_FIN_WAIT2:
2203 case NES_CM_STATE_LAST_ACK:
2204 case NES_CM_STATE_TIME_WAIT:
2205 case NES_CM_STATE_CLOSING:
2206 ret = -1;
2207 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002208 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002209 case NES_CM_STATE_LISTENING:
2210 case NES_CM_STATE_UNKNOWN:
2211 case NES_CM_STATE_INITED:
2212 case NES_CM_STATE_CLOSED:
Faisal Latifc5a7d482009-12-09 15:54:08 -08002213 case NES_CM_STATE_LISTENER_DESTROYED:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002214 ret = rem_ref_cm_node(cm_core, cm_node);
2215 break;
2216 case NES_CM_STATE_TSA:
2217 if (cm_node->send_entry)
2218 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2219 "send_entry=%p\n", cm_node->send_entry);
2220 ret = rem_ref_cm_node(cm_core, cm_node);
2221 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002222 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002223 return ret;
2224}
2225
2226
2227/**
2228 * recv_pkt - recv an ETHERNET packet, and process it through CM
2229 * node state machine
2230 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002231static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002232 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002233{
2234 struct nes_cm_node *cm_node = NULL;
2235 struct nes_cm_listener *listener = NULL;
2236 struct iphdr *iph;
2237 struct tcphdr *tcph;
2238 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002239 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002240 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002241
Faisal Latif6492cdf2008-07-24 20:50:45 -07002242 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002243 return 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002244 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002245 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002246 }
2247
2248 iph = (struct iphdr *)skb->data;
2249 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002250
2251 nfo.loc_addr = ntohl(iph->daddr);
2252 nfo.loc_port = ntohs(tcph->dest);
2253 nfo.rem_addr = ntohl(iph->saddr);
2254 nfo.rem_port = ntohs(tcph->source);
2255
Harvey Harrison03080e52009-01-10 21:45:42 -08002256 tmp_daddr = cpu_to_be32(iph->daddr);
2257 tmp_saddr = cpu_to_be32(iph->saddr);
2258
Harvey Harrison63779432008-10-31 00:56:00 -07002259 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002260 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002261
Faisal Latif6492cdf2008-07-24 20:50:45 -07002262 do {
2263 cm_node = find_node(cm_core,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002264 nfo.rem_port, nfo.rem_addr,
2265 nfo.loc_port, nfo.loc_addr);
2266
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002267 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002268 /* Only type of packet accepted are for */
2269 /* the PASSIVE open (syn only) */
2270 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002271 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002272 break;
2273 }
2274 listener = find_listener(cm_core, nfo.loc_addr,
2275 nfo.loc_port,
2276 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002277 if (!listener) {
2278 nfo.cm_id = NULL;
2279 nfo.conn_type = 0;
2280 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2281 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002282 break;
2283 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002284 nfo.cm_id = listener->cm_id;
2285 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002286 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2287 listener);
2288 if (!cm_node) {
2289 nes_debug(NES_DBG_CM, "Unable to allocate "
2290 "node\n");
2291 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002292 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002293 dev_kfree_skb_any(skb);
2294 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002295 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002296 if (!tcph->rst && !tcph->fin) {
2297 cm_node->state = NES_CM_STATE_LISTENING;
2298 } else {
2299 cm_packets_dropped++;
2300 rem_ref_cm_node(cm_core, cm_node);
2301 dev_kfree_skb_any(skb);
2302 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002303 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002304 add_ref_cm_node(cm_node);
2305 } else if (cm_node->state == NES_CM_STATE_TSA) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002306 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002307 atomic_inc(&cm_accel_dropped_pkts);
2308 dev_kfree_skb_any(skb);
2309 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002310 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002311 skb_reset_network_header(skb);
2312 skb_set_transport_header(skb, sizeof(*tcph));
2313 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002314 process_packet(cm_node, skb, cm_core);
2315 rem_ref_cm_node(cm_core, cm_node);
2316 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002317 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002318}
2319
2320
2321/**
2322 * nes_cm_alloc_core - allocate a top level instance of a cm core
2323 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002324static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002325{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002326 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002327
2328 /* setup the CM core */
2329 /* alloc top level core control structure */
2330 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2331 if (!cm_core)
2332 return NULL;
2333
2334 INIT_LIST_HEAD(&cm_core->connected_nodes);
2335 init_timer(&cm_core->tcp_timer);
2336 cm_core->tcp_timer.function = nes_cm_timer_tick;
2337
2338 cm_core->mtu = NES_CM_DEFAULT_MTU;
2339 cm_core->state = NES_CM_STATE_INITED;
2340 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2341
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002342 atomic_set(&cm_core->events_posted, 0);
2343
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002344 cm_core->api = &nes_cm_api;
2345
2346 spin_lock_init(&cm_core->ht_lock);
2347 spin_lock_init(&cm_core->listen_list_lock);
2348
2349 INIT_LIST_HEAD(&cm_core->listen_list.list);
2350
2351 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2352
2353 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2354 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2355 cm_core->post_event = nes_cm_post_event;
2356 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2357 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2358
2359 print_core(cm_core);
2360 return cm_core;
2361}
2362
2363
2364/**
2365 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2366 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002367static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002368{
2369 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2370
2371 if (!cm_core)
2372 return -EINVAL;
2373
2374 barrier();
2375
2376 if (timer_pending(&cm_core->tcp_timer)) {
2377 del_timer(&cm_core->tcp_timer);
2378 }
2379
2380 destroy_workqueue(cm_core->event_wq);
2381 destroy_workqueue(cm_core->disconn_wq);
2382 nes_debug(NES_DBG_CM, "\n");
2383 kfree(cm_core);
2384
2385 return 0;
2386}
2387
2388
2389/**
2390 * mini_cm_get
2391 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002392static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002393{
2394 return cm_core->state;
2395}
2396
2397
2398/**
2399 * mini_cm_set
2400 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002401static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002402{
2403 int ret = 0;
2404
2405 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002406 case NES_CM_SET_PKT_SIZE:
2407 cm_core->mtu = value;
2408 break;
2409 case NES_CM_SET_FREE_PKT_Q_SIZE:
2410 cm_core->free_tx_pkt_max = value;
2411 break;
2412 default:
2413 /* unknown set option */
2414 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002415 }
2416
2417 return ret;
2418}
2419
2420
2421/**
2422 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2423 * successfully exchanged when this is called
2424 */
2425static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2426{
2427 int ret = 0;
2428
2429 if (!nesqp)
2430 return -EINVAL;
2431
2432 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2433 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2434 NES_QPCONTEXT_MISC_DROS);
2435
2436 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2437 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2438
2439 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2440
2441 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2442
2443 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2444 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2445
2446 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2447 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2448 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2449
2450 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2451 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2452 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2453
2454 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2455 nesqp->nesqp_context->ts_recent = 0;
2456 nesqp->nesqp_context->ts_age = 0;
2457 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2458 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2459 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2460 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2461 cm_node->tcp_cntxt.rcv_wscale);
2462 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2463 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2464 nesqp->nesqp_context->srtt = 0;
2465 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2466 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2467 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2468 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2469 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2470 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2471
2472 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2473 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2474 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2475 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2476 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2477 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2478 le32_to_cpu(nesqp->nesqp_context->misc));
2479 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2480 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2481 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2482
2483 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2484 cm_node->state = NES_CM_STATE_TSA;
2485
2486 return ret;
2487}
2488
2489
2490/**
2491 * nes_cm_disconn
2492 */
2493int nes_cm_disconn(struct nes_qp *nesqp)
2494{
Don Wood873fcdd2009-09-05 20:36:37 -07002495 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002496
Don Wood873fcdd2009-09-05 20:36:37 -07002497 work = kzalloc(sizeof *work, GFP_ATOMIC);
2498 if (!work)
2499 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002500
Don Wood873fcdd2009-09-05 20:36:37 -07002501 nes_add_ref(&nesqp->ibqp);
2502 work->nesqp = nesqp;
2503 INIT_WORK(&work->work, nes_disconnect_worker);
2504 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002505 return 0;
2506}
2507
2508
2509/**
2510 * nes_disconnect_worker
2511 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002512static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002513{
Don Wood873fcdd2009-09-05 20:36:37 -07002514 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2515 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002516
Don Wood873fcdd2009-09-05 20:36:37 -07002517 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002518 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2519 nesqp->last_aeq, nesqp->hwqp.qp_id);
2520 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002521 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002522}
2523
2524
2525/**
2526 * nes_cm_disconn_true
2527 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002528static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002529{
2530 unsigned long flags;
2531 int ret = 0;
2532 struct iw_cm_id *cm_id;
2533 struct iw_cm_event cm_event;
2534 struct nes_vnic *nesvnic;
2535 u16 last_ae;
2536 u8 original_hw_tcp_state;
2537 u8 original_ibqp_state;
Don Woodb29a4fc2009-09-05 20:36:39 -07002538 enum iw_cm_event_type disconn_status = IW_CM_EVENT_STATUS_OK;
2539 int issue_disconn = 0;
2540 int issue_close = 0;
2541 int issue_flush = 0;
2542 u32 flush_q = NES_CQP_FLUSH_RQ;
2543 struct ib_event ibevent;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002544
2545 if (!nesqp) {
2546 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2547 return -1;
2548 }
2549
2550 spin_lock_irqsave(&nesqp->lock, flags);
2551 cm_id = nesqp->cm_id;
2552 /* make sure we havent already closed this connection */
2553 if (!cm_id) {
2554 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2555 nesqp->hwqp.qp_id);
2556 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002557 return -1;
2558 }
2559
2560 nesvnic = to_nesvnic(nesqp->ibqp.device);
2561 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2562
2563 original_hw_tcp_state = nesqp->hw_tcp_state;
2564 original_ibqp_state = nesqp->ibqp_state;
2565 last_ae = nesqp->last_aeq;
2566
Don Woodb29a4fc2009-09-05 20:36:39 -07002567 if (nesqp->term_flags) {
2568 issue_disconn = 1;
2569 issue_close = 1;
2570 nesqp->cm_id = NULL;
2571 if (nesqp->flush_issued == 0) {
2572 nesqp->flush_issued = 1;
2573 issue_flush = 1;
2574 }
2575 } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2576 ((original_ibqp_state == IB_QPS_RTS) &&
2577 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2578 issue_disconn = 1;
2579 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
2580 disconn_status = IW_CM_EVENT_STATUS_RESET;
2581 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002582
Don Woodb29a4fc2009-09-05 20:36:39 -07002583 if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2584 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2585 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2586 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2587 issue_close = 1;
2588 nesqp->cm_id = NULL;
2589 if (nesqp->flush_issued == 0) {
2590 nesqp->flush_issued = 1;
2591 issue_flush = 1;
2592 }
2593 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002594
Don Woodb29a4fc2009-09-05 20:36:39 -07002595 spin_unlock_irqrestore(&nesqp->lock, flags);
2596
2597 if ((issue_flush) && (nesqp->destroyed == 0)) {
2598 /* Flush the queue(s) */
2599 if (nesqp->hw_iwarp_state >= NES_AEQE_IWARP_STATE_TERMINATE)
2600 flush_q |= NES_CQP_FLUSH_SQ;
2601 flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2602
2603 if (nesqp->term_flags) {
2604 ibevent.device = nesqp->ibqp.device;
2605 ibevent.event = nesqp->terminate_eventtype;
2606 ibevent.element.qp = &nesqp->ibqp;
2607 nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
2608 }
2609 }
2610
2611 if ((cm_id) && (cm_id->event_handler)) {
2612 if (issue_disconn) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002613 atomic_inc(&cm_disconnects);
2614 cm_event.event = IW_CM_EVENT_DISCONNECT;
Don Woodb29a4fc2009-09-05 20:36:39 -07002615 cm_event.status = disconn_status;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002616 cm_event.local_addr = cm_id->local_addr;
2617 cm_event.remote_addr = cm_id->remote_addr;
2618 cm_event.private_data = NULL;
2619 cm_event.private_data_len = 0;
2620
Faisal Latif6492cdf2008-07-24 20:50:45 -07002621 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2622 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2623 "cm_id = %p, refcount = %u.\n",
2624 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2625 nesqp->hwqp.sq_tail, cm_id,
2626 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002627
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002628 ret = cm_id->event_handler(cm_id, &cm_event);
2629 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002630 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2631 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002632 }
2633
Don Woodb29a4fc2009-09-05 20:36:39 -07002634 if (issue_close) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002635 atomic_inc(&cm_closes);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002636 nes_disconnect(nesqp, 1);
2637
2638 cm_id->provider_data = nesqp;
2639 /* Send up the close complete event */
2640 cm_event.event = IW_CM_EVENT_CLOSE;
2641 cm_event.status = IW_CM_EVENT_STATUS_OK;
2642 cm_event.provider_data = cm_id->provider_data;
2643 cm_event.local_addr = cm_id->local_addr;
2644 cm_event.remote_addr = cm_id->remote_addr;
2645 cm_event.private_data = NULL;
2646 cm_event.private_data_len = 0;
2647
2648 ret = cm_id->event_handler(cm_id, &cm_event);
2649 if (ret) {
2650 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2651 }
2652
2653 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002654 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002655 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002656
2657 return 0;
2658}
2659
2660
2661/**
2662 * nes_disconnect
2663 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002664static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002665{
2666 int ret = 0;
2667 struct nes_vnic *nesvnic;
2668 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002669 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002670
2671 nesvnic = to_nesvnic(nesqp->ibqp.device);
2672 if (!nesvnic)
2673 return -EINVAL;
2674
2675 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002676 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002677
2678 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2679 atomic_read(&nesvnic->netdev->refcnt));
2680
2681 if (nesqp->active_conn) {
2682
2683 /* indicate this connection is NOT active */
2684 nesqp->active_conn = 0;
2685 } else {
2686 /* Need to free the Last Streaming Mode Message */
2687 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002688 if (nesqp->lsmm_mr)
2689 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002690 pci_free_consistent(nesdev->pcidev,
2691 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2692 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2693 }
2694 }
2695
2696 /* close the CM node down if it is still active */
2697 if (nesqp->cm_node) {
2698 nes_debug(NES_DBG_CM, "Call close API\n");
2699
2700 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002701 }
2702
2703 return ret;
2704}
2705
2706
2707/**
2708 * nes_accept
2709 */
2710int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2711{
2712 u64 u64temp;
2713 struct ib_qp *ibqp;
2714 struct nes_qp *nesqp;
2715 struct nes_vnic *nesvnic;
2716 struct nes_device *nesdev;
2717 struct nes_cm_node *cm_node;
2718 struct nes_adapter *adapter;
2719 struct ib_qp_attr attr;
2720 struct iw_cm_event cm_event;
2721 struct nes_hw_qp_wqe *wqe;
2722 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002723 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002724 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002725 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002726 struct nes_ib_device *nesibdev;
2727 struct ib_mr *ibmr = NULL;
2728 struct ib_phys_buf ibphysbuf;
2729 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002730 u64 tagged_offset;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002731
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002732 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2733 if (!ibqp)
2734 return -EINVAL;
2735
2736 /* get all our handles */
2737 nesqp = to_nesqp(ibqp);
2738 nesvnic = to_nesvnic(nesqp->ibqp.device);
2739 nesdev = nesvnic->nesdev;
2740 adapter = nesdev->nesadapter;
2741
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002742 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002743 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2744 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2745 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002746
Faisal Latifc5a7d482009-12-09 15:54:08 -08002747 if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
2748 if (cm_node->loopbackpartner)
2749 rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
2750 rem_ref_cm_node(cm_node->cm_core, cm_node);
2751 return -EINVAL;
2752 }
2753
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002754 /* associate the node with the QP */
2755 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002756 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002757
Faisal Latif6492cdf2008-07-24 20:50:45 -07002758 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2759 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002760 atomic_inc(&cm_accepts);
2761
2762 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2763 atomic_read(&nesvnic->netdev->refcnt));
2764
Faisal Latif6492cdf2008-07-24 20:50:45 -07002765 /* allocate the ietf frame and space for private data */
2766 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2767 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2768 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002769
Faisal Latif6492cdf2008-07-24 20:50:45 -07002770 if (!nesqp->ietf_frame) {
2771 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2772 "data\n");
2773 return -ENOMEM;
2774 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002775
2776
Faisal Latif6492cdf2008-07-24 20:50:45 -07002777 /* setup the MPA frame */
2778 nesqp->private_data_len = conn_param->private_data_len;
2779 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002780
Faisal Latif6492cdf2008-07-24 20:50:45 -07002781 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2782 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002783
Faisal Latif6492cdf2008-07-24 20:50:45 -07002784 nesqp->ietf_frame->priv_data_len =
2785 cpu_to_be16(conn_param->private_data_len);
2786 nesqp->ietf_frame->rev = mpa_version;
2787 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002788
Faisal Latif6492cdf2008-07-24 20:50:45 -07002789 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2790 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002791
Faisal Latif6492cdf2008-07-24 20:50:45 -07002792 if (cm_id->remote_addr.sin_addr.s_addr !=
2793 cm_id->local_addr.sin_addr.s_addr) {
2794 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002795 nesibdev = nesvnic->nesibdev;
2796 nespd = nesqp->nespd;
2797 ibphysbuf.addr = nesqp->ietf_frame_pbase;
2798 ibphysbuf.size = conn_param->private_data_len +
2799 sizeof(struct ietf_mpa_frame);
Don Wood7a5efb62009-04-08 14:21:02 -07002800 tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002801 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
2802 &ibphysbuf, 1,
2803 IB_ACCESS_LOCAL_WRITE,
Don Wood7a5efb62009-04-08 14:21:02 -07002804 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002805 if (!ibmr) {
2806 nes_debug(NES_DBG_CM, "Unable to register memory region"
2807 "for lSMM for cm_node = %p \n",
2808 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07002809 pci_free_consistent(nesdev->pcidev,
2810 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2811 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002812 return -ENOMEM;
2813 }
2814
2815 ibmr->pd = &nespd->ibpd;
2816 ibmr->device = nespd->ibpd.device;
2817 nesqp->lsmm_mr = ibmr;
2818
Faisal Latif6492cdf2008-07-24 20:50:45 -07002819 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2820 set_wqe_64bit_value(wqe->wqe_words,
2821 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2822 u64temp);
2823 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2824 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2825 NES_IWARP_SQ_WQE_WRPDU);
2826 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2827 cpu_to_le32(conn_param->private_data_len +
2828 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002829 set_wqe_64bit_value(wqe->wqe_words,
2830 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
Don Wood7a5efb62009-04-08 14:21:02 -07002831 (u64)(unsigned long)nesqp->ietf_frame);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002832 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2833 cpu_to_le32(conn_param->private_data_len +
2834 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002835 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002836
Faisal Latif6492cdf2008-07-24 20:50:45 -07002837 nesqp->nesqp_context->ird_ord_sizes |=
2838 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2839 NES_QPCONTEXT_ORDIRD_WRPDU);
2840 } else {
2841 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08002842 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002843 }
2844 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002845
2846
2847 /* Cache the cm_id in the qp */
2848 nesqp->cm_id = cm_id;
2849 cm_node->cm_id = cm_id;
2850
2851 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2852 cm_id->provider_data = nesqp;
2853 nesqp->active_conn = 0;
2854
Faisal Latif6492cdf2008-07-24 20:50:45 -07002855 if (cm_node->state == NES_CM_STATE_TSA)
2856 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2857 cm_node);
2858
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002859 nes_cm_init_tsa_conn(nesqp, cm_node);
2860
Faisal Latif6492cdf2008-07-24 20:50:45 -07002861 nesqp->nesqp_context->tcpPorts[0] =
2862 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2863 nesqp->nesqp_context->tcpPorts[1] =
2864 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2865
2866 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2867 nesqp->nesqp_context->ip0 =
2868 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2869 else
2870 nesqp->nesqp_context->ip0 =
2871 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002872
2873 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002874 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2875 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002876
Faisal Latif6492cdf2008-07-24 20:50:45 -07002877 nesqp->nesqp_context->arp_index_vlan |=
2878 cpu_to_le32(nes_arp_table(nesdev,
2879 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002880 NES_ARP_RESOLVE) << 16);
2881
2882 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002883 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002884
2885 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2886
2887 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002888 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2889 nesqp->nesqp_context->ird_ord_sizes |=
2890 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002891
2892 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002893 nes_quad.DstIpAdrIndex =
2894 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2895 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2896 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2897 else
2898 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2899 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2900 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002901
2902 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06002903 crc_value = get_crc_value(&nes_quad);
2904 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002905 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07002906 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002907
2908 nesqp->hte_index &= adapter->hte_index_mask;
2909 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2910
2911 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2912
Faisal Latif6492cdf2008-07-24 20:50:45 -07002913 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2914 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2915 "private data length=%zu.\n", nesqp->hwqp.qp_id,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002916 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2917 ntohs(cm_id->remote_addr.sin_port),
2918 ntohl(cm_id->local_addr.sin_addr.s_addr),
2919 ntohs(cm_id->local_addr.sin_port),
2920 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2921 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002922 conn_param->private_data_len +
2923 sizeof(struct ietf_mpa_frame));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002924
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002925
Coly Li73ac36e2009-01-07 18:09:16 -08002926 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002927 cm_id->add_ref(cm_id);
Faisal Latif9256b252009-04-27 13:45:19 -07002928 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002929
2930 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2931 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2932 cm_event.provider_data = (void *)nesqp;
2933 cm_event.local_addr = cm_id->local_addr;
2934 cm_event.remote_addr = cm_id->remote_addr;
2935 cm_event.private_data = NULL;
2936 cm_event.private_data_len = 0;
2937 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002938 attr.qp_state = IB_QPS_RTS;
2939 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002940 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002941 cm_node->loopbackpartner->mpa_frame_size =
2942 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002943 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002944 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2945 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002946 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2947 }
2948 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002949 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2950 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002951
Faisal Latif183ecfa2008-11-21 20:50:46 -06002952 passive_state = atomic_add_return(1, &cm_node->passive_state);
2953 if (passive_state == NES_SEND_RESET_EVENT)
2954 create_event(cm_node, NES_CM_EVENT_RESET);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002955 return 0;
2956}
2957
2958
2959/**
2960 * nes_reject
2961 */
2962int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2963{
2964 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002965 struct nes_cm_node *loopback;
2966
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002967 struct nes_cm_core *cm_core;
2968
2969 atomic_inc(&cm_rejects);
2970 cm_node = (struct nes_cm_node *) cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002971 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002972 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002973 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002974 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2975
Faisal Latif9d5ab132009-03-06 15:15:01 -08002976 if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
2977 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002978
Faisal Latif1cf078c2009-12-09 15:53:54 -08002979 memcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002980 if (loopback) {
2981 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
2982 loopback->mpa_frame.priv_data_len = pdata_len;
2983 loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
2984 pdata_len;
2985 } else {
2986 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2987 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2988 }
2989
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002990 cm_node->mpa_frame.rev = mpa_version;
2991 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2992
Faisal Latif9d5ab132009-03-06 15:15:01 -08002993 return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002994}
2995
2996
2997/**
2998 * nes_connect
2999 * setup and launch cm connect node
3000 */
3001int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3002{
3003 struct ib_qp *ibqp;
3004 struct nes_qp *nesqp;
3005 struct nes_vnic *nesvnic;
3006 struct nes_device *nesdev;
3007 struct nes_cm_node *cm_node;
3008 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07003009 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003010
3011 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3012 if (!ibqp)
3013 return -EINVAL;
3014 nesqp = to_nesqp(ibqp);
3015 if (!nesqp)
3016 return -EINVAL;
3017 nesvnic = to_nesvnic(nesqp->ibqp.device);
3018 if (!nesvnic)
3019 return -EINVAL;
3020 nesdev = nesvnic->nesdev;
3021 if (!nesdev)
3022 return -EINVAL;
3023
Faisal Latifc5a7d482009-12-09 15:54:08 -08003024 if (!(cm_id->local_addr.sin_port) || !(cm_id->remote_addr.sin_port))
3025 return -EINVAL;
3026
Faisal Latif6492cdf2008-07-24 20:50:45 -07003027 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
3028 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3029 ntohl(nesvnic->local_ipaddr),
3030 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3031 ntohs(cm_id->remote_addr.sin_port),
3032 ntohl(cm_id->local_addr.sin_addr.s_addr),
3033 ntohs(cm_id->local_addr.sin_port));
3034
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003035 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003036 nesqp->active_conn = 1;
3037
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003038 /* cache the cm_id in the qp */
3039 nesqp->cm_id = cm_id;
3040
3041 cm_id->provider_data = nesqp;
3042
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003043 nesqp->private_data_len = conn_param->private_data_len;
3044 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
3045 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003046 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
3047 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003048
Faisal Latif6492cdf2008-07-24 20:50:45 -07003049 if (cm_id->local_addr.sin_addr.s_addr !=
Faisal Latif53094c32009-04-27 13:37:34 -07003050 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003051 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003052 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003053 apbvt_set = 1;
3054 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003055
3056 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003057 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3058 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3059 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3060 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003061 cm_info.cm_id = cm_id;
3062 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3063
3064 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003065
3066 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003067 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3068 conn_param->private_data_len, (void *)conn_param->private_data,
3069 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003070 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003071 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003072 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003073 PCI_FUNC(nesdev->pcidev->devfn),
3074 NES_MANAGE_APBVT_DEL);
3075
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003076 cm_id->rem_ref(cm_id);
3077 return -ENOMEM;
3078 }
3079
Faisal Latif53094c32009-04-27 13:37:34 -07003080 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003081 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003082 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003083 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003084
3085 return 0;
3086}
3087
3088
3089/**
3090 * nes_create_listen
3091 */
3092int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3093{
3094 struct nes_vnic *nesvnic;
3095 struct nes_cm_listener *cm_node;
3096 struct nes_cm_info cm_info;
3097 struct nes_adapter *adapter;
3098 int err;
3099
3100
3101 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3102 cm_id, ntohs(cm_id->local_addr.sin_port));
3103
3104 nesvnic = to_nesvnic(cm_id->device);
3105 if (!nesvnic)
3106 return -EINVAL;
3107 adapter = nesvnic->nesdev->nesadapter;
3108 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3109 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3110
3111 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3112 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3113
3114 /* setup listen params in our api call struct */
3115 cm_info.loc_addr = nesvnic->local_ipaddr;
3116 cm_info.loc_port = cm_id->local_addr.sin_port;
3117 cm_info.backlog = backlog;
3118 cm_info.cm_id = cm_id;
3119
3120 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3121
3122
3123 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3124 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003125 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003126 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003127 return -ENOMEM;
3128 }
3129
3130 cm_id->provider_data = cm_node;
3131
3132 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003133 err = nes_manage_apbvt(nesvnic,
3134 ntohs(cm_id->local_addr.sin_port),
3135 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3136 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003137 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003138 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3139 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003140 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3141 return err;
3142 }
3143 cm_listens_created++;
3144 }
3145
3146 cm_id->add_ref(cm_id);
3147 cm_id->provider_data = (void *)cm_node;
3148
3149
3150 return 0;
3151}
3152
3153
3154/**
3155 * nes_destroy_listen
3156 */
3157int nes_destroy_listen(struct iw_cm_id *cm_id)
3158{
3159 if (cm_id->provider_data)
3160 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3161 else
3162 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3163
3164 cm_id->rem_ref(cm_id);
3165
3166 return 0;
3167}
3168
3169
3170/**
3171 * nes_cm_recv
3172 */
3173int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3174{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003175 int rc = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003176 cm_packets_received++;
3177 if ((g_cm_core) && (g_cm_core->api)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003178 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003179 } else {
3180 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3181 " cm is not setup properly.\n");
3182 }
3183
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003184 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003185}
3186
3187
3188/**
3189 * nes_cm_start
3190 * Start and init a cm core module
3191 */
3192int nes_cm_start(void)
3193{
3194 nes_debug(NES_DBG_CM, "\n");
3195 /* create the primary CM core, pass this handle to subsequent core inits */
3196 g_cm_core = nes_cm_alloc_core();
3197 if (g_cm_core) {
3198 return 0;
3199 } else {
3200 return -ENOMEM;
3201 }
3202}
3203
3204
3205/**
3206 * nes_cm_stop
3207 * stop and dealloc all cm core instances
3208 */
3209int nes_cm_stop(void)
3210{
3211 g_cm_core->api->destroy_cm_core(g_cm_core);
3212 return 0;
3213}
3214
3215
3216/**
3217 * cm_event_connected
3218 * handle a connected event, setup QPs and HW
3219 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003220static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003221{
3222 u64 u64temp;
3223 struct nes_qp *nesqp;
3224 struct nes_vnic *nesvnic;
3225 struct nes_device *nesdev;
3226 struct nes_cm_node *cm_node;
3227 struct nes_adapter *nesadapter;
3228 struct ib_qp_attr attr;
3229 struct iw_cm_id *cm_id;
3230 struct iw_cm_event cm_event;
3231 struct nes_hw_qp_wqe *wqe;
3232 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003233 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003234 int ret;
3235
3236 /* get all our handles */
3237 cm_node = event->cm_node;
3238 cm_id = cm_node->cm_id;
3239 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3240 nesqp = (struct nes_qp *)cm_id->provider_data;
3241 nesvnic = to_nesvnic(nesqp->ibqp.device);
3242 nesdev = nesvnic->nesdev;
3243 nesadapter = nesdev->nesadapter;
3244
3245 if (nesqp->destroyed) {
3246 return;
3247 }
3248 atomic_inc(&cm_connecteds);
3249 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3250 " local port 0x%04X. jiffies = %lu.\n",
3251 nesqp->hwqp.qp_id,
3252 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3253 ntohs(cm_id->remote_addr.sin_port),
3254 ntohs(cm_id->local_addr.sin_port),
3255 jiffies);
3256
3257 nes_cm_init_tsa_conn(nesqp, cm_node);
3258
3259 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003260 nesqp->nesqp_context->tcpPorts[0] =
3261 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3262 nesqp->nesqp_context->tcpPorts[1] =
3263 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3264 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3265 nesqp->nesqp_context->ip0 =
3266 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3267 else
3268 nesqp->nesqp_context->ip0 =
3269 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003270
3271 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003272 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3273 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003274 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003275 nes_arp_table(nesdev,
3276 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003277 NULL, NES_ARP_RESOLVE) << 16);
3278 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3279 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3280 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3281 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003282 cpu_to_le32((u32)1 <<
3283 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003284
3285 /* Adjust tail for not having a LSMM */
3286 nesqp->hwqp.sq_tail = 1;
3287
3288#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003289 if (cm_node->send_write0) {
3290 nes_debug(NES_DBG_CM, "Sending first write.\n");
3291 wqe = &nesqp->hwqp.sq_vbase[0];
3292 u64temp = (unsigned long)nesqp;
3293 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3294 set_wqe_64bit_value(wqe->wqe_words,
3295 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3296 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3297 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3298 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3299 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3300 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3301 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3302 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003303
Faisal Latif6492cdf2008-07-24 20:50:45 -07003304 /* use the reserved spot on the WQ for the extra first WQE */
3305 nesqp->nesqp_context->ird_ord_sizes &=
3306 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3307 NES_QPCONTEXT_ORDIRD_WRPDU |
3308 NES_QPCONTEXT_ORDIRD_ALSMM));
3309 nesqp->skip_lsmm = 1;
3310 nesqp->hwqp.sq_tail = 0;
3311 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3312 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3313 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003314#endif
3315
3316 memset(&nes_quad, 0, sizeof(nes_quad));
3317
Faisal Latif6492cdf2008-07-24 20:50:45 -07003318 nes_quad.DstIpAdrIndex =
3319 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3320 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3321 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3322 else
3323 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003324 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3325 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3326
3327 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003328 crc_value = get_crc_value(&nes_quad);
3329 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003330 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3331 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3332
3333 nesqp->hte_index &= nesadapter->hte_index_mask;
3334 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3335
3336 nesqp->ietf_frame = &cm_node->mpa_frame;
3337 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3338 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3339
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003340 /* notify OF layer we successfully created the requested connection */
3341 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3342 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3343 cm_event.provider_data = cm_id->provider_data;
3344 cm_event.local_addr.sin_family = AF_INET;
3345 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3346 cm_event.remote_addr = cm_id->remote_addr;
3347
Faisal Latif6492cdf2008-07-24 20:50:45 -07003348 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3349 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003350
3351 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3352 ret = cm_id->event_handler(cm_id, &cm_event);
3353 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3354
3355 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003356 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3357 "ret=%d\n", __func__, __LINE__, ret);
3358 attr.qp_state = IB_QPS_RTS;
3359 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003360
Faisal Latif6492cdf2008-07-24 20:50:45 -07003361 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3362 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003363
3364 return;
3365}
3366
3367
3368/**
3369 * cm_event_connect_error
3370 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003371static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003372{
3373 struct nes_qp *nesqp;
3374 struct iw_cm_id *cm_id;
3375 struct iw_cm_event cm_event;
3376 /* struct nes_cm_info cm_info; */
3377 int ret;
3378
3379 if (!event->cm_node)
3380 return;
3381
3382 cm_id = event->cm_node->cm_id;
3383 if (!cm_id) {
3384 return;
3385 }
3386
3387 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3388 nesqp = cm_id->provider_data;
3389
3390 if (!nesqp) {
3391 return;
3392 }
3393
3394 /* notify OF layer about this connection error event */
3395 /* cm_id->rem_ref(cm_id); */
3396 nesqp->cm_id = NULL;
3397 cm_id->provider_data = NULL;
3398 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
Faisal Latifc5a7d482009-12-09 15:54:08 -08003399 cm_event.status = -ECONNRESET;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003400 cm_event.provider_data = cm_id->provider_data;
3401 cm_event.local_addr = cm_id->local_addr;
3402 cm_event.remote_addr = cm_id->remote_addr;
3403 cm_event.private_data = NULL;
3404 cm_event.private_data_len = 0;
3405
Faisal Latif6492cdf2008-07-24 20:50:45 -07003406 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3407 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3408 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003409
3410 ret = cm_id->event_handler(cm_id, &cm_event);
3411 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3412 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003413 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3414 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003415 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003416
Faisal Latif6492cdf2008-07-24 20:50:45 -07003417 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003418 return;
3419}
3420
3421
3422/**
3423 * cm_event_reset
3424 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003425static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003426{
3427 struct nes_qp *nesqp;
3428 struct iw_cm_id *cm_id;
3429 struct iw_cm_event cm_event;
3430 /* struct nes_cm_info cm_info; */
3431 int ret;
3432
3433 if (!event->cm_node)
3434 return;
3435
3436 if (!event->cm_node->cm_id)
3437 return;
3438
3439 cm_id = event->cm_node->cm_id;
3440
3441 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3442 nesqp = cm_id->provider_data;
3443
3444 nesqp->cm_id = NULL;
3445 /* cm_id->provider_data = NULL; */
3446 cm_event.event = IW_CM_EVENT_DISCONNECT;
3447 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3448 cm_event.provider_data = cm_id->provider_data;
3449 cm_event.local_addr = cm_id->local_addr;
3450 cm_event.remote_addr = cm_id->remote_addr;
3451 cm_event.private_data = NULL;
3452 cm_event.private_data_len = 0;
3453
3454 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003455 cm_id->add_ref(cm_id);
3456 atomic_inc(&cm_closes);
3457 cm_event.event = IW_CM_EVENT_CLOSE;
3458 cm_event.status = IW_CM_EVENT_STATUS_OK;
3459 cm_event.provider_data = cm_id->provider_data;
3460 cm_event.local_addr = cm_id->local_addr;
3461 cm_event.remote_addr = cm_id->remote_addr;
3462 cm_event.private_data = NULL;
3463 cm_event.private_data_len = 0;
3464 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3465 ret = cm_id->event_handler(cm_id, &cm_event);
3466
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003467 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3468
3469
3470 /* notify OF layer about this connection error event */
3471 cm_id->rem_ref(cm_id);
3472
3473 return;
3474}
3475
3476
3477/**
3478 * cm_event_mpa_req
3479 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003480static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003481{
3482 struct iw_cm_id *cm_id;
3483 struct iw_cm_event cm_event;
3484 int ret;
3485 struct nes_cm_node *cm_node;
3486
3487 cm_node = event->cm_node;
3488 if (!cm_node)
3489 return;
3490 cm_id = cm_node->cm_id;
3491
3492 atomic_inc(&cm_connect_reqs);
3493 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3494 cm_node, cm_id, jiffies);
3495
3496 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3497 cm_event.status = IW_CM_EVENT_STATUS_OK;
3498 cm_event.provider_data = (void *)cm_node;
3499
3500 cm_event.local_addr.sin_family = AF_INET;
3501 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3502 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3503
3504 cm_event.remote_addr.sin_family = AF_INET;
3505 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3506 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003507 cm_event.private_data = cm_node->mpa_frame_buf;
3508 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003509
3510 ret = cm_id->event_handler(cm_id, &cm_event);
3511 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003512 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3513 __func__, __LINE__, ret);
3514 return;
3515}
3516
3517
3518static void cm_event_mpa_reject(struct nes_cm_event *event)
3519{
3520 struct iw_cm_id *cm_id;
3521 struct iw_cm_event cm_event;
3522 struct nes_cm_node *cm_node;
3523 int ret;
3524
3525 cm_node = event->cm_node;
3526 if (!cm_node)
3527 return;
3528 cm_id = cm_node->cm_id;
3529
3530 atomic_inc(&cm_connect_reqs);
3531 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3532 cm_node, cm_id, jiffies);
3533
3534 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3535 cm_event.status = -ECONNREFUSED;
3536 cm_event.provider_data = cm_id->provider_data;
3537
3538 cm_event.local_addr.sin_family = AF_INET;
3539 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3540 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3541
3542 cm_event.remote_addr.sin_family = AF_INET;
3543 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3544 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3545
3546 cm_event.private_data = cm_node->mpa_frame_buf;
3547 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3548
3549 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3550 "remove_addr=%08x\n",
3551 cm_event.local_addr.sin_addr.s_addr,
3552 cm_event.remote_addr.sin_addr.s_addr);
3553
3554 ret = cm_id->event_handler(cm_id, &cm_event);
3555 if (ret)
3556 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003557 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003558
3559 return;
3560}
3561
3562
3563static void nes_cm_event_handler(struct work_struct *);
3564
3565/**
3566 * nes_cm_post_event
3567 * post an event to the cm event handler
3568 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003569static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003570{
3571 atomic_inc(&event->cm_node->cm_core->events_posted);
3572 add_ref_cm_node(event->cm_node);
3573 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3574 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003575 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3576 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003577
3578 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3579
3580 nes_debug(NES_DBG_CM, "Exit\n");
3581 return 0;
3582}
3583
3584
3585/**
3586 * nes_cm_event_handler
3587 * worker function to handle cm events
3588 * will free instance of nes_cm_event
3589 */
3590static void nes_cm_event_handler(struct work_struct *work)
3591{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003592 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3593 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003594 struct nes_cm_core *cm_core;
3595
Faisal Latif6492cdf2008-07-24 20:50:45 -07003596 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003597 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003598
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003599 cm_core = event->cm_node->cm_core;
3600 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003601 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003602
3603 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003604 case NES_CM_EVENT_MPA_REQ:
3605 cm_event_mpa_req(event);
3606 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3607 event->cm_node);
3608 break;
3609 case NES_CM_EVENT_RESET:
3610 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3611 event->cm_node);
3612 cm_event_reset(event);
3613 break;
3614 case NES_CM_EVENT_CONNECTED:
3615 if ((!event->cm_node->cm_id) ||
3616 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003617 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003618 cm_event_connected(event);
3619 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3620 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003621 case NES_CM_EVENT_MPA_REJECT:
3622 if ((!event->cm_node->cm_id) ||
3623 (event->cm_node->state == NES_CM_STATE_TSA))
3624 break;
3625 cm_event_mpa_reject(event);
3626 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3627 break;
3628
Faisal Latif6492cdf2008-07-24 20:50:45 -07003629 case NES_CM_EVENT_ABORTED:
3630 if ((!event->cm_node->cm_id) ||
3631 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003632 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003633 cm_event_connect_error(event);
3634 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3635 break;
3636 case NES_CM_EVENT_DROPPED_PKT:
3637 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3638 break;
3639 default:
3640 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3641 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003642 }
3643
3644 atomic_dec(&cm_core->events_posted);
3645 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3646 rem_ref_cm_node(cm_core, event->cm_node);
3647 kfree(event);
3648
3649 return;
3650}