blob: 1efe0beca0637a8210bb17c7bd60320331b6bfa9 [file] [log] [blame]
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001/*
Chien Tungcd6853d2009-03-06 15:12:10 -08002 * Copyright (c) 2006 - 2009 Intel-NE, Inc. 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>
59
60#include "nes.h"
61
62u32 cm_packets_sent;
63u32 cm_packets_bounced;
64u32 cm_packets_dropped;
65u32 cm_packets_retrans;
66u32 cm_packets_created;
67u32 cm_packets_received;
68u32 cm_listens_created;
69u32 cm_listens_destroyed;
70u32 cm_backlog_drops;
71atomic_t cm_loopbacks;
72atomic_t cm_nodes_created;
73atomic_t cm_nodes_destroyed;
74atomic_t cm_accel_dropped_pkts;
75atomic_t cm_resets_recvd;
76
Faisal Latif6492cdf2008-07-24 20:50:45 -070077static inline int mini_cm_accelerated(struct nes_cm_core *,
78 struct nes_cm_node *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080079static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070080 struct nes_vnic *, struct nes_cm_info *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080081static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070082static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070083 struct nes_vnic *, u16, void *, struct nes_cm_info *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070084static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -070085static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
86 struct nes_cm_node *);
87static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
88 struct nes_cm_node *);
Faisal Latif4a14f6a2008-11-21 20:50:49 -060089static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070090 struct sk_buff *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070091static int mini_cm_dealloc_core(struct nes_cm_core *);
92static int mini_cm_get(struct nes_cm_core *);
93static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070094
Chien Tung6098d102008-11-21 20:51:01 -060095static void form_cm_frame(struct sk_buff *, struct nes_cm_node *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070096 void *, u32, void *, u32, u8);
Faisal Latif6492cdf2008-07-24 20:50:45 -070097static int add_ref_cm_node(struct nes_cm_node *);
98static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
99
Roland Dreier1a855fb2008-04-16 21:01:09 -0700100static int nes_cm_disconn_true(struct nes_qp *);
101static int nes_cm_post_event(struct nes_cm_event *event);
102static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
103static void nes_disconnect_worker(struct work_struct *work);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700104
105static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800106static int send_mpa_reject(struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700107static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
108static int send_reset(struct nes_cm_node *, struct sk_buff *);
109static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fb2008-04-16 21:01:09 -0700110static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700111static void process_packet(struct nes_cm_node *, struct sk_buff *,
112 struct nes_cm_core *);
113
114static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
115static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
116static void cleanup_retrans_entry(struct nes_cm_node *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800117static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700118static void free_retrans_entry(struct nes_cm_node *cm_node);
119static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
120 struct sk_buff *skb, int optionsize, int passive);
121
122/* CM event handler functions */
123static void cm_event_connected(struct nes_cm_event *);
124static void cm_event_connect_error(struct nes_cm_event *);
125static void cm_event_reset(struct nes_cm_event *);
126static void cm_event_mpa_req(struct nes_cm_event *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800127static void cm_event_mpa_reject(struct nes_cm_event *);
128static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700129
130static void print_core(struct nes_cm_core *core);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800131
132/* External CM API Interface */
133/* instance of function pointers for client API */
134/* set address of this instance to cm_core->cm_ops at cm_core alloc */
135static struct nes_cm_ops nes_cm_api = {
136 mini_cm_accelerated,
137 mini_cm_listen,
138 mini_cm_del_listen,
139 mini_cm_connect,
140 mini_cm_close,
141 mini_cm_accept,
142 mini_cm_reject,
143 mini_cm_recv_pkt,
144 mini_cm_dealloc_core,
145 mini_cm_get,
146 mini_cm_set
147};
148
Roland Dreier1a855fb2008-04-16 21:01:09 -0700149static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800150
151atomic_t cm_connects;
152atomic_t cm_accepts;
153atomic_t cm_disconnects;
154atomic_t cm_closes;
155atomic_t cm_connecteds;
156atomic_t cm_connect_reqs;
157atomic_t cm_rejects;
158
159
160/**
161 * create_event
162 */
163static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
164 enum nes_cm_event_type type)
165{
166 struct nes_cm_event *event;
167
168 if (!cm_node->cm_id)
169 return NULL;
170
171 /* allocate an empty event */
172 event = kzalloc(sizeof(*event), GFP_ATOMIC);
173
174 if (!event)
175 return NULL;
176
177 event->type = type;
178 event->cm_node = cm_node;
179 event->cm_info.rem_addr = cm_node->rem_addr;
180 event->cm_info.loc_addr = cm_node->loc_addr;
181 event->cm_info.rem_port = cm_node->rem_port;
182 event->cm_info.loc_port = cm_node->loc_port;
183 event->cm_info.cm_id = cm_node->cm_id;
184
Faisal Latif6492cdf2008-07-24 20:50:45 -0700185 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
186 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
187 cm_node, event, type, event->cm_info.loc_addr,
188 event->cm_info.loc_port, event->cm_info.rem_addr,
189 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800190
191 nes_cm_post_event(event);
192 return event;
193}
194
195
196/**
197 * send_mpa_request
198 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700199static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800200{
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800201 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700202 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800203 return -1;
204 }
205
206 /* send an MPA Request frame */
207 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
208 cm_node->mpa_frame_size, SET_ACK);
209
Faisal Latif9d5ab132009-03-06 15:15:01 -0800210 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
211}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800212
Faisal Latif9d5ab132009-03-06 15:15:01 -0800213
214
215static int send_mpa_reject(struct nes_cm_node *cm_node)
216{
217 struct sk_buff *skb = NULL;
218
219 skb = dev_alloc_skb(MAX_CM_BUFFER);
220 if (!skb) {
221 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
222 return -ENOMEM;
223 }
224
225 /* send an MPA reject frame */
226 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
227 cm_node->mpa_frame_size, SET_ACK | SET_FIN);
228
229 cm_node->state = NES_CM_STATE_FIN_WAIT1;
230 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800231}
232
233
234/**
235 * recv_mpa - process a received TCP pkt, we are expecting an
236 * IETF MPA frame
237 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800238static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
239 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800240{
241 struct ietf_mpa_frame *mpa_frame;
242
Faisal Latif9d5ab132009-03-06 15:15:01 -0800243 *type = NES_MPA_REQUEST_ACCEPT;
244
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800245 /* assume req frame is in tcp data payload */
246 if (len < sizeof(struct ietf_mpa_frame)) {
247 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800248 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800249 }
250
251 mpa_frame = (struct ietf_mpa_frame *)buffer;
252 cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
253
254 if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
255 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
256 " complete (%x + %x != %x)\n",
Faisal Latif9d5ab132009-03-06 15:15:01 -0800257 cm_node->mpa_frame_size,
258 (u32)sizeof(struct ietf_mpa_frame), len);
259 return -EINVAL;
260 }
261 /* make sure it does not exceed the max size */
262 if (len > MAX_CM_BUFFER) {
263 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
264 " (%x + %x != %x)\n",
265 cm_node->mpa_frame_size,
266 (u32)sizeof(struct ietf_mpa_frame), len);
267 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800268 }
269
270 /* copy entire MPA frame to our cm_node's frame */
271 memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
272 cm_node->mpa_frame_size);
273
Faisal Latif9d5ab132009-03-06 15:15:01 -0800274 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
275 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800276 return 0;
277}
278
279
280/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800281 * form_cm_frame - get a free packet and build empty frame Use
282 * node info to build.
283 */
Chien Tung6098d102008-11-21 20:51:01 -0600284static void form_cm_frame(struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700285 struct nes_cm_node *cm_node, void *options, u32 optionsize,
286 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800287{
288 struct tcphdr *tcph;
289 struct iphdr *iph;
290 struct ethhdr *ethh;
291 u8 *buf;
292 u16 packetsize = sizeof(*iph);
293
294 packetsize += sizeof(*tcph);
295 packetsize += optionsize + datasize;
296
297 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
298
299 skb->len = 0;
300 buf = skb_put(skb, packetsize + ETH_HLEN);
301
302 ethh = (struct ethhdr *) buf;
303 buf += ETH_HLEN;
304
305 iph = (struct iphdr *)buf;
306 buf += sizeof(*iph);
307 tcph = (struct tcphdr *)buf;
308 skb_reset_mac_header(skb);
309 skb_set_network_header(skb, ETH_HLEN);
310 skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
311 buf += sizeof(*tcph);
312
313 skb->ip_summed = CHECKSUM_PARTIAL;
314 skb->protocol = htons(0x800);
315 skb->data_len = 0;
316 skb->mac_len = ETH_HLEN;
317
318 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
319 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
320 ethh->h_proto = htons(0x0800);
321
322 iph->version = IPVERSION;
323 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
324 iph->tos = 0;
325 iph->tot_len = htons(packetsize);
326 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
327
328 iph->frag_off = htons(0x4000);
329 iph->ttl = 0x40;
330 iph->protocol = 0x06; /* IPPROTO_TCP */
331
332 iph->saddr = htonl(cm_node->loc_addr);
333 iph->daddr = htonl(cm_node->rem_addr);
334
335 tcph->source = htons(cm_node->loc_port);
336 tcph->dest = htons(cm_node->rem_port);
337 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
338
339 if (flags & SET_ACK) {
340 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
341 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
342 tcph->ack = 1;
343 } else
344 tcph->ack_seq = 0;
345
346 if (flags & SET_SYN) {
347 cm_node->tcp_cntxt.loc_seq_num++;
348 tcph->syn = 1;
349 } else
Faisal Latif6492cdf2008-07-24 20:50:45 -0700350 cm_node->tcp_cntxt.loc_seq_num += datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800351
Faisal Latif6492cdf2008-07-24 20:50:45 -0700352 if (flags & SET_FIN) {
353 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800354 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700355 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800356
357 if (flags & SET_RST)
358 tcph->rst = 1;
359
360 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
361 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
362 tcph->urg_ptr = 0;
363 if (optionsize)
364 memcpy(buf, options, optionsize);
365 buf += optionsize;
366 if (datasize)
367 memcpy(buf, data, datasize);
368
369 skb_shinfo(skb)->nr_frags = 0;
370 cm_packets_created++;
371
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800372}
373
374
375/**
376 * print_core - dump a cm core
377 */
378static void print_core(struct nes_cm_core *core)
379{
380 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
381 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
382 if (!core)
383 return;
384 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800385
386 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
387
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800388 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
389 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
390
391 nes_debug(NES_DBG_CM, "core : %p \n", core);
392
393 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
394}
395
396
397/**
398 * schedule_nes_timer
399 * note - cm_node needs to be protected before calling this. Encase in:
400 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
401 */
402int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
403 enum nes_timer_type type, int send_retrans,
404 int close_when_complete)
405{
406 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700407 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800408 struct nes_timer_entry *new_send;
409 int ret = 0;
410 u32 was_timer_set;
411
412 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
413 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800414 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800415
416 /* new_send->timetosend = currenttime */
417 new_send->retrycount = NES_DEFAULT_RETRYS;
418 new_send->retranscount = NES_DEFAULT_RETRANS;
419 new_send->skb = skb;
420 new_send->timetosend = jiffies;
421 new_send->type = type;
422 new_send->netdev = cm_node->netdev;
423 new_send->send_retrans = send_retrans;
424 new_send->close_when_complete = close_when_complete;
425
426 if (type == NES_TIMER_TYPE_CLOSE) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700427 new_send->timetosend += (HZ/10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800428 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700429 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800430 WARN_ON(1);
431 return -EINVAL;
432 }
433 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800434 }
435
436 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700437 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800438 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700439 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
440 cm_node->send_entry = new_send;
441 add_ref_cm_node(cm_node);
442 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
443 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800444
445 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
446 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700447 nes_debug(NES_DBG_CM, "Error sending packet %p "
448 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800449 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700450 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800451 } else {
452 cm_packets_sent++;
453 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700454 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800455 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700456 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800457 return ret;
458 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800459 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800460 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800461
462 was_timer_set = timer_pending(&cm_core->tcp_timer);
463
464 if (!was_timer_set) {
465 cm_core->tcp_timer.expires = new_send->timetosend;
466 add_timer(&cm_core->tcp_timer);
467 }
468
469 return ret;
470}
471
Faisal Latif9d5ab132009-03-06 15:15:01 -0800472static void nes_retrans_expired(struct nes_cm_node *cm_node)
473{
474 switch (cm_node->state) {
475 case NES_CM_STATE_SYN_RCVD:
476 case NES_CM_STATE_CLOSING:
477 rem_ref_cm_node(cm_node->cm_core, cm_node);
478 break;
479 case NES_CM_STATE_LAST_ACK:
480 case NES_CM_STATE_FIN_WAIT1:
481 case NES_CM_STATE_MPAREJ_RCVD:
482 send_reset(cm_node, NULL);
483 break;
484 default:
485 create_event(cm_node, NES_CM_EVENT_ABORTED);
486 }
487}
488
489static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
490{
491 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
492 struct iw_cm_id *cm_id = cm_node->cm_id;
493 struct nes_qp *nesqp;
494 unsigned long qplockflags;
495
496 if (!recv_entry)
497 return;
498 nesqp = (struct nes_qp *)recv_entry->skb;
499 if (nesqp) {
500 spin_lock_irqsave(&nesqp->lock, qplockflags);
501 if (nesqp->cm_id) {
502 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
503 "refcount = %d: HIT A "
504 "NES_TIMER_TYPE_CLOSE with something "
505 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
506 atomic_read(&nesqp->refcount));
507 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
508 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
509 nesqp->ibqp_state = IB_QPS_ERR;
510 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
511 nes_cm_disconn(nesqp);
512 } else {
513 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
514 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
515 "refcount = %d: HIT A "
516 "NES_TIMER_TYPE_CLOSE with nothing "
517 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
518 atomic_read(&nesqp->refcount));
519 }
520 } else if (rem_node) {
521 /* TIME_WAIT state */
522 rem_ref_cm_node(cm_node->cm_core, cm_node);
523 }
524 if (cm_node->cm_id)
525 cm_id->rem_ref(cm_id);
526 kfree(recv_entry);
527 cm_node->recv_entry = NULL;
528}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800529
530/**
531 * nes_cm_timer_tick
532 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700533static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800534{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800535 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800536 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800537 struct nes_cm_node *cm_node;
538 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800539 struct list_head *list_core_temp;
540 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800541 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800542 u32 settimer = 0;
543 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800544
Faisal Latif879e5bd2008-11-21 20:50:41 -0600545 struct list_head timer_list;
546 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800547 spin_lock_irqsave(&cm_core->ht_lock, flags);
548
Faisal Latif6492cdf2008-07-24 20:50:45 -0700549 list_for_each_safe(list_node, list_core_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600550 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800551 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800552 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600553 add_ref_cm_node(cm_node);
554 list_add(&cm_node->timer_entry, &timer_list);
555 }
556 }
557 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
558
559 list_for_each_safe(list_node, list_core_temp, &timer_list) {
560 cm_node = container_of(list_node, struct nes_cm_node,
561 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800562 recv_entry = cm_node->recv_entry;
563
564 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700565 if (time_after(recv_entry->timetosend, jiffies)) {
566 if (nexttimeout > recv_entry->timetosend ||
Faisal Latif9d5ab132009-03-06 15:15:01 -0800567 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800568 nexttimeout = recv_entry->timetosend;
569 settimer = 1;
570 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800571 } else
572 handle_recv_entry(cm_node, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800573 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800574
575 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700576 do {
577 send_entry = cm_node->send_entry;
578 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600579 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800580 if (time_after(send_entry->timetosend, jiffies)) {
581 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700582 if ((nexttimeout >
583 send_entry->timetosend) ||
584 !settimer) {
585 nexttimeout =
586 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800587 settimer = 1;
588 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800589 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700590 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800591 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800592 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800593 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700594
595 if ((cm_node->state == NES_CM_STATE_TSA) ||
596 (cm_node->state == NES_CM_STATE_CLOSED)) {
597 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600598 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800599 }
600
Faisal Latif6492cdf2008-07-24 20:50:45 -0700601 if (!send_entry->retranscount ||
602 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800603 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700604 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800605
Faisal Latif6492cdf2008-07-24 20:50:45 -0700606 spin_unlock_irqrestore(
607 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800608 nes_retrans_expired(cm_node);
609 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700610 spin_lock_irqsave(&cm_node->retrans_list_lock,
611 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600612 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800613 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800614 atomic_inc(&send_entry->skb->users);
615 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700616 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
617 "for node %p, jiffies = %lu, time to send = "
618 "%lu, retranscount = %u, send_entry->seq_num = "
619 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
620 "0x%08X\n", send_entry, cm_node, jiffies,
621 send_entry->timetosend,
622 send_entry->retranscount,
623 send_entry->seq_num,
624 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800625
Faisal Latif6492cdf2008-07-24 20:50:45 -0700626 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
627 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800628 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700629 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800630 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700631 nes_debug(NES_DBG_CM, "rexmit failed for "
632 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800633 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800634 send_entry->retrycount--;
635 nexttimeout = jiffies + NES_SHORT_TIME;
636 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600637 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800638 } else {
639 cm_packets_sent++;
640 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700641 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
642 "%u, retry count = %u.\n",
643 send_entry->retranscount,
644 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800645 if (send_entry->send_retrans) {
646 send_entry->retranscount--;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700647 send_entry->timetosend = jiffies +
648 NES_RETRY_TIMEOUT;
649 if (nexttimeout > send_entry->timetosend ||
650 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800651 nexttimeout = send_entry->timetosend;
652 settimer = 1;
653 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800654 } else {
655 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700656 close_when_complete =
657 send_entry->close_when_complete;
658 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
659 cm_node, cm_node->state);
660 free_retrans_entry(cm_node);
661 if (close_when_complete)
662 rem_ref_cm_node(cm_node->cm_core,
663 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800664 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700665 } while (0);
666
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800667 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700668 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800669 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800670
671 if (settimer) {
672 if (!timer_pending(&cm_core->tcp_timer)) {
673 cm_core->tcp_timer.expires = nexttimeout;
674 add_timer(&cm_core->tcp_timer);
675 }
676 }
677}
678
679
680/**
681 * send_syn
682 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700683static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
684 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800685{
686 int ret;
687 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800688 char optionsbuffer[sizeof(struct option_mss) +
Faisal Latif6492cdf2008-07-24 20:50:45 -0700689 sizeof(struct option_windowscale) + sizeof(struct option_base) +
690 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800691
692 int optionssize = 0;
693 /* Sending MSS option */
694 union all_known_options *options;
695
696 if (!cm_node)
697 return -EINVAL;
698
699 options = (union all_known_options *)&optionsbuffer[optionssize];
700 options->as_mss.optionnum = OPTION_NUMBER_MSS;
701 options->as_mss.length = sizeof(struct option_mss);
702 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
703 optionssize += sizeof(struct option_mss);
704
705 options = (union all_known_options *)&optionsbuffer[optionssize];
706 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
707 options->as_windowscale.length = sizeof(struct option_windowscale);
708 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
709 optionssize += sizeof(struct option_windowscale);
710
Faisal Latif6492cdf2008-07-24 20:50:45 -0700711 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800712 options = (union all_known_options *)&optionsbuffer[optionssize];
713 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
714 options->as_base.length = sizeof(struct option_base);
715 optionssize += sizeof(struct option_base);
716 /* we need the size to be a multiple of 4 */
717 options = (union all_known_options *)&optionsbuffer[optionssize];
718 options->as_end = 1;
719 optionssize += 1;
720 options = (union all_known_options *)&optionsbuffer[optionssize];
721 options->as_end = 1;
722 optionssize += 1;
723 }
724
725 options = (union all_known_options *)&optionsbuffer[optionssize];
726 options->as_end = OPTION_NUMBER_END;
727 optionssize += 1;
728
Faisal Latif6492cdf2008-07-24 20:50:45 -0700729 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800730 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800731 if (!skb) {
732 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
733 return -1;
734 }
735
736 if (sendack)
737 flags |= SET_ACK;
738
739 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
740 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
741
742 return ret;
743}
744
745
746/**
747 * send_reset
748 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700749static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800750{
751 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800752 int flags = SET_RST | SET_ACK;
753
Faisal Latif6492cdf2008-07-24 20:50:45 -0700754 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800755 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800756 if (!skb) {
757 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -0800758 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800759 }
760
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800761 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
762 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
763
764 return ret;
765}
766
767
768/**
769 * send_ack
770 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700771static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800772{
773 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700774
775 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800776 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800777
778 if (!skb) {
779 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
780 return -1;
781 }
782
783 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
784 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
785
786 return ret;
787}
788
789
790/**
791 * send_fin
792 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700793static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800794{
795 int ret;
796
797 /* if we didn't get a frame get one */
798 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800799 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800800
801 if (!skb) {
802 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
803 return -1;
804 }
805
806 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
807 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
808
809 return ret;
810}
811
812
813/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800814 * find_node - find a cm node that matches the reference cm node
815 */
816static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
817 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
818{
819 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800820 struct list_head *hte;
821 struct nes_cm_node *cm_node;
822
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800823 /* get a handle on the hte */
824 hte = &cm_core->connected_nodes;
825
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800826 /* walk list and find cm_node associated with this session ID */
827 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700828 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800829 /* compare quad, return node handle if a match */
830 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
831 cm_node->loc_addr, cm_node->loc_port,
832 loc_addr, loc_port,
833 cm_node->rem_addr, cm_node->rem_port,
834 rem_addr, rem_port);
835 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
836 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
837 add_ref_cm_node(cm_node);
838 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
839 return cm_node;
840 }
841 }
842 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
843
844 /* no owner node */
845 return NULL;
846}
847
848
849/**
850 * find_listener - find a cm node listening on this addr-port pair
851 */
852static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
853 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
854{
855 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800856 struct nes_cm_listener *listen_node;
857
858 /* walk list and find cm_node associated with this session ID */
859 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700860 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800861 /* compare node pair, return node handle if a match */
862 if (((listen_node->loc_addr == dst_addr) ||
863 listen_node->loc_addr == 0x00000000) &&
864 (listen_node->loc_port == dst_port) &&
865 (listener_state & listen_node->listener_state)) {
866 atomic_inc(&listen_node->ref_count);
867 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
868 return listen_node;
869 }
870 }
871 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
872
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800873 /* no listener */
874 return NULL;
875}
876
877
878/**
879 * add_hte_node - add a cm node to the hash table
880 */
881static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
882{
883 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800884 struct list_head *hte;
885
886 if (!cm_node || !cm_core)
887 return -EINVAL;
888
Faisal Latif6492cdf2008-07-24 20:50:45 -0700889 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
890 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800891
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800892 spin_lock_irqsave(&cm_core->ht_lock, flags);
893
894 /* get a handle on the hash table element (list head for this slot) */
895 hte = &cm_core->connected_nodes;
896 list_add_tail(&cm_node->list, hte);
897 atomic_inc(&cm_core->ht_node_cnt);
898
899 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
900
901 return 0;
902}
903
904
905/**
906 * mini_cm_dec_refcnt_listen
907 */
908static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700909 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800910{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800911 int ret = -EINVAL;
912 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800913 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700914 struct list_head *list_pos = NULL;
915 struct list_head *list_temp = NULL;
916 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -0600917 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700918
919 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
920 "refcnt=%d\n", listener, free_hanging_nodes,
921 atomic_read(&listener->ref_count));
922 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -0600923 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700924 if (free_hanging_nodes) {
925 spin_lock_irqsave(&cm_core->ht_lock, flags);
926 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600927 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700928 cm_node = container_of(list_pos, struct nes_cm_node,
929 list);
930 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -0600931 (!cm_node->accelerated)) {
932 add_ref_cm_node(cm_node);
933 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700934 }
935 }
936 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
937 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600938
939 list_for_each_safe(list_pos, list_temp, &reset_list) {
940 cm_node = container_of(list_pos, struct nes_cm_node,
Faisal Latif9d5ab132009-03-06 15:15:01 -0800941 reset_entry);
942 {
943 struct nes_cm_node *loopback = cm_node->loopbackpartner;
944 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
945 rem_ref_cm_node(cm_node->cm_core, cm_node);
946 } else {
947 if (!loopback) {
948 cleanup_retrans_entry(cm_node);
949 err = send_reset(cm_node, NULL);
950 if (err) {
951 cm_node->state =
952 NES_CM_STATE_CLOSED;
953 WARN_ON(1);
954 } else {
955 cm_node->state =
956 NES_CM_STATE_CLOSED;
957 rem_ref_cm_node(
958 cm_node->cm_core,
959 cm_node);
960 }
961 } else {
962 struct nes_cm_event event;
963
964 event.cm_node = loopback;
965 event.cm_info.rem_addr =
966 loopback->rem_addr;
967 event.cm_info.loc_addr =
968 loopback->loc_addr;
969 event.cm_info.rem_port =
970 loopback->rem_port;
971 event.cm_info.loc_port =
972 loopback->loc_port;
973 event.cm_info.cm_id = loopback->cm_id;
974 cm_event_connect_error(&event);
975 loopback->state = NES_CM_STATE_CLOSED;
976
977 event.cm_node = cm_node;
978 event.cm_info.rem_addr =
979 cm_node->rem_addr;
980 event.cm_info.loc_addr =
981 cm_node->loc_addr;
982 event.cm_info.rem_port =
983 cm_node->rem_port;
984 event.cm_info.loc_port =
985 cm_node->loc_port;
986 event.cm_info.cm_id = cm_node->cm_id;
987 cm_event_reset(&event);
988
989 rem_ref_cm_node(cm_node->cm_core,
990 cm_node);
991
992 }
993 }
994 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600995 }
996
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800997 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
998 if (!atomic_dec_return(&listener->ref_count)) {
999 list_del(&listener->list);
1000
1001 /* decrement our listen node count */
1002 atomic_dec(&cm_core->listen_node_cnt);
1003
1004 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1005
1006 if (listener->nesvnic) {
1007 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1008 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1009 }
1010
1011 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1012
1013 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001014 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001015 ret = 0;
1016 cm_listens_destroyed++;
1017 } else {
1018 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1019 }
1020 if (listener) {
1021 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1022 nes_debug(NES_DBG_CM, "destroying listener (%p)"
1023 " with non-zero pending accepts=%u\n",
1024 listener, atomic_read(&listener->pend_accepts_cnt));
1025 }
1026
1027 return ret;
1028}
1029
1030
1031/**
1032 * mini_cm_del_listen
1033 */
1034static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1035 struct nes_cm_listener *listener)
1036{
1037 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1038 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1039 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1040}
1041
1042
1043/**
1044 * mini_cm_accelerated
1045 */
1046static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1047 struct nes_cm_node *cm_node)
1048{
1049 u32 was_timer_set;
1050 cm_node->accelerated = 1;
1051
1052 if (cm_node->accept_pend) {
1053 BUG_ON(!cm_node->listener);
1054 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001055 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001056 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1057 }
1058
1059 was_timer_set = timer_pending(&cm_core->tcp_timer);
1060 if (!was_timer_set) {
1061 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1062 add_timer(&cm_core->tcp_timer);
1063 }
1064
1065 return 0;
1066}
1067
1068
1069/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001070 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001071 */
Bob Sharp7191a0a2008-10-03 12:21:19 -07001072static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001073{
1074 struct rtable *rt;
1075 struct flowi fl;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001076 struct neighbour *neigh;
1077 int rc = -1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001078
1079 memset(&fl, 0, sizeof fl);
1080 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1081 if (ip_route_output_key(&init_net, &rt, &fl)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001082 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001083 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001084 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001085 }
1086
Bob Sharp7191a0a2008-10-03 12:21:19 -07001087 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1088 if (neigh) {
1089 if (neigh->nud_state & NUD_VALID) {
1090 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001091 " is %pM, Gateway is 0x%08X \n", dst_ip,
1092 neigh->ha, ntohl(rt->rt_gateway));
Bob Sharp7191a0a2008-10-03 12:21:19 -07001093 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1094 dst_ip, NES_ARP_ADD);
1095 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1096 NES_ARP_RESOLVE);
1097 }
1098 neigh_release(neigh);
1099 }
1100
1101 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1102 neigh_event_send(rt->u.dst.neighbour, NULL);
1103
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001104 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001105 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001106}
1107
1108
1109/**
1110 * make_cm_node - create a new instance of a cm node
1111 */
1112static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1113 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1114 struct nes_cm_listener *listener)
1115{
1116 struct nes_cm_node *cm_node;
1117 struct timespec ts;
1118 int arpindex = 0;
1119 struct nes_device *nesdev;
1120 struct nes_adapter *nesadapter;
1121
1122 /* create an hte and cm_node for this instance */
1123 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1124 if (!cm_node)
1125 return NULL;
1126
1127 /* set our node specific transport info */
1128 cm_node->loc_addr = cm_info->loc_addr;
1129 cm_node->rem_addr = cm_info->rem_addr;
1130 cm_node->loc_port = cm_info->loc_port;
1131 cm_node->rem_port = cm_info->rem_port;
1132 cm_node->send_write0 = send_first;
Harvey Harrison63779432008-10-31 00:56:00 -07001133 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1134 &cm_node->loc_addr, cm_node->loc_port,
1135 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001136 cm_node->listener = listener;
1137 cm_node->netdev = nesvnic->netdev;
1138 cm_node->cm_id = cm_info->cm_id;
1139 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1140
Faisal Latif6492cdf2008-07-24 20:50:45 -07001141 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1142 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001143
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001144 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001145
1146 cm_node->loopbackpartner = NULL;
1147 atomic_set(&cm_node->ref_count, 1);
1148 /* associate our parent CM core */
1149 cm_node->cm_core = cm_core;
1150 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1151 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1152 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1153 NES_CM_DEFAULT_RCV_WND_SCALE;
1154 ts = current_kernel_time();
1155 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1156 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Chien Tungf2b2b592008-03-20 19:55:30 -05001157 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001158 cm_node->tcp_cntxt.rcv_nxt = 0;
1159 /* get a unique session ID , add thread_id to an upcounter to handle race */
1160 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001161 cm_node->conn_type = cm_info->conn_type;
1162 cm_node->apbvt_set = 0;
1163 cm_node->accept_pend = 0;
1164
1165 cm_node->nesvnic = nesvnic;
1166 /* get some device handles, for arp lookup */
1167 nesdev = nesvnic->nesdev;
1168 nesadapter = nesdev->nesadapter;
1169
1170 cm_node->loopbackpartner = NULL;
1171 /* get the mac addr for the remote node */
Chien Tung1ee86552008-11-21 20:51:04 -06001172 if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1173 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
1174 else
1175 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001176 if (arpindex < 0) {
Bob Sharp7191a0a2008-10-03 12:21:19 -07001177 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr);
1178 if (arpindex < 0) {
1179 kfree(cm_node);
1180 return NULL;
1181 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001182 }
1183
1184 /* copy the mac addr to node context */
1185 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001186 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1187 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001188
1189 add_hte_node(cm_core, cm_node);
1190 atomic_inc(&cm_nodes_created);
1191
1192 return cm_node;
1193}
1194
1195
1196/**
1197 * add_ref_cm_node - destroy an instance of a cm node
1198 */
1199static int add_ref_cm_node(struct nes_cm_node *cm_node)
1200{
1201 atomic_inc(&cm_node->ref_count);
1202 return 0;
1203}
1204
1205
1206/**
1207 * rem_ref_cm_node - destroy an instance of a cm node
1208 */
1209static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001210 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001211{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001212 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001213 struct nes_qp *nesqp;
1214
1215 if (!cm_node)
1216 return -EINVAL;
1217
1218 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1219 if (atomic_dec_return(&cm_node->ref_count)) {
1220 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1221 return 0;
1222 }
1223 list_del(&cm_node->list);
1224 atomic_dec(&cm_core->ht_node_cnt);
1225 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1226
1227 /* if the node is destroyed before connection was accelerated */
1228 if (!cm_node->accelerated && cm_node->accept_pend) {
1229 BUG_ON(!cm_node->listener);
1230 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1231 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1232 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001233 WARN_ON(cm_node->send_entry);
1234 if (cm_node->recv_entry)
1235 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001236 if (cm_node->listener) {
1237 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1238 } else {
1239 if (cm_node->apbvt_set && cm_node->nesvnic) {
1240 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001241 PCI_FUNC(
1242 cm_node->nesvnic->nesdev->pcidev->devfn),
1243 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001244 }
1245 }
1246
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001247 atomic_dec(&cm_core->node_cnt);
1248 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001249 nesqp = cm_node->nesqp;
1250 if (nesqp) {
1251 nesqp->cm_node = NULL;
1252 nes_rem_ref(&nesqp->ibqp);
1253 cm_node->nesqp = NULL;
1254 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001255
Faisal Latif6492cdf2008-07-24 20:50:45 -07001256 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001257 return 0;
1258}
1259
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001260/**
1261 * process_options
1262 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001263static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1264 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001265{
1266 u32 tmp;
1267 u32 offset = 0;
1268 union all_known_options *all_options;
1269 char got_mss_option = 0;
1270
1271 while (offset < optionsize) {
1272 all_options = (union all_known_options *)(optionsloc + offset);
1273 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001274 case OPTION_NUMBER_END:
1275 offset = optionsize;
1276 break;
1277 case OPTION_NUMBER_NONE:
1278 offset += 1;
1279 continue;
1280 case OPTION_NUMBER_MSS:
1281 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1282 "Size: %d\n", __func__,
1283 all_options->as_mss.length, offset, optionsize);
1284 got_mss_option = 1;
1285 if (all_options->as_mss.length != 4) {
1286 return 1;
1287 } else {
1288 tmp = ntohs(all_options->as_mss.mss);
1289 if (tmp > 0 && tmp <
1290 cm_node->tcp_cntxt.mss)
1291 cm_node->tcp_cntxt.mss = tmp;
1292 }
1293 break;
1294 case OPTION_NUMBER_WINDOW_SCALE:
1295 cm_node->tcp_cntxt.snd_wscale =
1296 all_options->as_windowscale.shiftcount;
1297 break;
1298 case OPTION_NUMBER_WRITE0:
1299 cm_node->send_write0 = 1;
1300 break;
1301 default:
1302 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1303 all_options->as_base.optionnum);
1304 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001305 }
1306 offset += all_options->as_base.length;
1307 }
1308 if ((!got_mss_option) && (syn_packet))
1309 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1310 return 0;
1311}
1312
Faisal Latif6492cdf2008-07-24 20:50:45 -07001313static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001314{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001315 atomic_inc(&cm_accel_dropped_pkts);
1316 dev_kfree_skb_any(skb);
1317}
1318
Faisal Latif9d5ab132009-03-06 15:15:01 -08001319static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001320{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001321 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1322 "refcnt=%d\n", cm_node, cm_node->state,
1323 atomic_read(&cm_node->ref_count));
1324 cm_node->tcp_cntxt.rcv_nxt++;
1325 cleanup_retrans_entry(cm_node);
1326 switch (cm_node->state) {
1327 case NES_CM_STATE_SYN_RCVD:
1328 case NES_CM_STATE_SYN_SENT:
1329 case NES_CM_STATE_ESTABLISHED:
1330 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001331 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001332 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001333 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001334 break;
1335 case NES_CM_STATE_FIN_WAIT1:
1336 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001337 send_ack(cm_node, NULL);
1338 /* Wait for ACK as this is simultanous close..
1339 * After we receive ACK, do not send anything..
1340 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001341 break;
1342 case NES_CM_STATE_FIN_WAIT2:
1343 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001344 send_ack(cm_node, NULL);
1345 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1346 break;
1347 case NES_CM_STATE_TIME_WAIT:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001348 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001349 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001350 break;
1351 case NES_CM_STATE_TSA:
1352 default:
1353 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1354 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001355 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001356 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001357}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001358
Faisal Latif6492cdf2008-07-24 20:50:45 -07001359
1360static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1361 struct tcphdr *tcph)
1362{
1363
1364 int reset = 0; /* whether to send reset in case of err.. */
Faisal Latif183ecfa2008-11-21 20:50:46 -06001365 int passive_state;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001366 atomic_inc(&cm_resets_recvd);
1367 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1368 " refcnt=%d\n", cm_node, cm_node->state,
1369 atomic_read(&cm_node->ref_count));
1370 cleanup_retrans_entry(cm_node);
1371 switch (cm_node->state) {
1372 case NES_CM_STATE_SYN_SENT:
1373 case NES_CM_STATE_MPAREQ_SENT:
1374 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1375 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1376 cm_node->listener, cm_node->state);
1377 active_open_err(cm_node, skb, reset);
1378 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001379 case NES_CM_STATE_MPAREQ_RCVD:
1380 passive_state = atomic_add_return(1, &cm_node->passive_state);
1381 if (passive_state == NES_SEND_RESET_EVENT)
1382 create_event(cm_node, NES_CM_EVENT_RESET);
1383 cleanup_retrans_entry(cm_node);
1384 cm_node->state = NES_CM_STATE_CLOSED;
1385 dev_kfree_skb_any(skb);
1386 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001387 case NES_CM_STATE_ESTABLISHED:
1388 case NES_CM_STATE_SYN_RCVD:
1389 case NES_CM_STATE_LISTENING:
1390 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1391 passive_open_err(cm_node, skb, reset);
1392 break;
1393 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001394 active_open_err(cm_node, skb, reset);
1395 break;
1396 case NES_CM_STATE_CLOSED:
1397 cleanup_retrans_entry(cm_node);
1398 drop_packet(skb);
1399 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001400 case NES_CM_STATE_TIME_WAIT:
1401 cleanup_retrans_entry(cm_node);
1402 cm_node->state = NES_CM_STATE_CLOSED;
1403 rem_ref_cm_node(cm_node->cm_core, cm_node);
1404 drop_packet(skb);
1405 break;
1406 case NES_CM_STATE_FIN_WAIT1:
1407 cleanup_retrans_entry(cm_node);
1408 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001409 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001410 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001411 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001412 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001413}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001414
Faisal Latif9d5ab132009-03-06 15:15:01 -08001415
1416static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001417{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001418
Faisal Latif9d5ab132009-03-06 15:15:01 -08001419 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001420 int datasize = skb->len;
1421 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001422
1423 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1424 u32 res_type;
1425 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1426 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001427 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001428 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001429 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1430 "cm_node=%p listener=%p state=%d\n", __func__,
1431 __LINE__, cm_node, cm_node->listener,
1432 cm_node->state);
1433 active_open_err(cm_node, skb, 1);
1434 } else {
1435 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001436 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001437 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001438 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001439
1440 switch (cm_node->state) {
1441 case NES_CM_STATE_ESTABLISHED:
1442 if (res_type == NES_MPA_REQUEST_REJECT) {
1443 /*BIG problem as we are receiving the MPA.. So should
1444 * not be REJECT.. This is Passive Open.. We can
1445 * only receive it Reject for Active Open...*/
1446 WARN_ON(1);
1447 }
1448 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1449 type = NES_CM_EVENT_MPA_REQ;
1450 atomic_set(&cm_node->passive_state,
1451 NES_PASSIVE_STATE_INDICATED);
1452 break;
1453 case NES_CM_STATE_MPAREQ_SENT:
1454 if (res_type == NES_MPA_REQUEST_REJECT) {
1455 type = NES_CM_EVENT_MPA_REJECT;
1456 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1457 } else {
1458 type = NES_CM_EVENT_CONNECTED;
1459 cm_node->state = NES_CM_STATE_TSA;
1460 }
1461
1462 break;
1463 default:
1464 WARN_ON(1);
1465 break;
1466 }
1467 dev_kfree_skb_any(skb);
1468 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001469}
1470
1471static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1472{
1473 switch (cm_node->state) {
1474 case NES_CM_STATE_SYN_SENT:
1475 case NES_CM_STATE_MPAREQ_SENT:
1476 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1477 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1478 cm_node->listener, cm_node->state);
1479 active_open_err(cm_node, skb, 1);
1480 break;
1481 case NES_CM_STATE_ESTABLISHED:
1482 case NES_CM_STATE_SYN_RCVD:
1483 passive_open_err(cm_node, skb, 1);
1484 break;
1485 case NES_CM_STATE_TSA:
1486 default:
1487 drop_packet(skb);
1488 }
1489}
1490
1491static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1492 struct sk_buff *skb)
1493{
1494 int err;
1495
1496 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1497 if (err)
1498 active_open_err(cm_node, skb, 1);
1499
1500 return err;
1501}
1502
1503static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1504 struct sk_buff *skb)
1505{
1506 int err = 0;
1507 u32 seq;
1508 u32 ack_seq;
1509 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1510 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1511 u32 rcv_wnd;
1512 seq = ntohl(tcph->seq);
1513 ack_seq = ntohl(tcph->ack_seq);
1514 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1515 if (ack_seq != loc_seq_num)
1516 err = 1;
1517 else if ((seq + rcv_wnd) < rcv_nxt)
1518 err = 1;
1519 if (err) {
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 indicate_pkt_err(cm_node, skb);
1524 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1525 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1526 rcv_wnd);
1527 }
1528 return err;
1529}
1530
1531/*
1532 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1533 * is created with a listener or it may comein as rexmitted packet which in
1534 * that case will be just dropped.
1535 */
1536
1537static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1538 struct tcphdr *tcph)
1539{
1540 int ret;
1541 u32 inc_sequence;
1542 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001543
1544 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001545 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001546 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001547
Faisal Latif6492cdf2008-07-24 20:50:45 -07001548 switch (cm_node->state) {
1549 case NES_CM_STATE_SYN_SENT:
1550 case NES_CM_STATE_MPAREQ_SENT:
1551 /* Rcvd syn on active open connection*/
1552 active_open_err(cm_node, skb, 1);
1553 break;
1554 case NES_CM_STATE_LISTENING:
1555 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001556 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1557 cm_node->listener->backlog) {
1558 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1559 "pressure \n");
1560 cm_backlog_drops++;
1561 passive_open_err(cm_node, skb, 0);
1562 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001563 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001564 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1565 1);
1566 if (ret) {
1567 passive_open_err(cm_node, skb, 0);
1568 /* drop pkt */
1569 break;
1570 }
1571 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1572 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001573 cm_node->accept_pend = 1;
1574 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1575
Faisal Latif6492cdf2008-07-24 20:50:45 -07001576 cm_node->state = NES_CM_STATE_SYN_RCVD;
1577 send_syn(cm_node, 1, skb);
1578 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001579 case NES_CM_STATE_CLOSED:
1580 cleanup_retrans_entry(cm_node);
1581 send_reset(cm_node, skb);
1582 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001583 case NES_CM_STATE_TSA:
1584 case NES_CM_STATE_ESTABLISHED:
1585 case NES_CM_STATE_FIN_WAIT1:
1586 case NES_CM_STATE_FIN_WAIT2:
1587 case NES_CM_STATE_MPAREQ_RCVD:
1588 case NES_CM_STATE_LAST_ACK:
1589 case NES_CM_STATE_CLOSING:
1590 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001591 default:
1592 drop_packet(skb);
1593 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001594 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001595}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001596
Faisal Latif6492cdf2008-07-24 20:50:45 -07001597static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1598 struct tcphdr *tcph)
1599{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001600
Faisal Latif6492cdf2008-07-24 20:50:45 -07001601 int ret;
1602 u32 inc_sequence;
1603 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001604
Faisal Latif6492cdf2008-07-24 20:50:45 -07001605 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001606 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001607 inc_sequence = ntohl(tcph->seq);
1608 switch (cm_node->state) {
1609 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001610 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001611 /* active open */
1612 if (check_syn(cm_node, tcph, skb))
1613 return;
1614 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1615 /* setup options */
1616 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1617 if (ret) {
1618 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1619 cm_node);
1620 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001621 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001622 cleanup_retrans_entry(cm_node);
1623 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1624 send_mpa_request(cm_node, skb);
1625 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1626 break;
1627 case NES_CM_STATE_MPAREQ_RCVD:
1628 /* passive open, so should not be here */
1629 passive_open_err(cm_node, skb, 1);
1630 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001631 case NES_CM_STATE_LISTENING:
1632 case NES_CM_STATE_CLOSED:
1633 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1634 cleanup_retrans_entry(cm_node);
1635 send_reset(cm_node, skb);
1636 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001637 case NES_CM_STATE_ESTABLISHED:
1638 case NES_CM_STATE_FIN_WAIT1:
1639 case NES_CM_STATE_FIN_WAIT2:
1640 case NES_CM_STATE_LAST_ACK:
1641 case NES_CM_STATE_TSA:
1642 case NES_CM_STATE_CLOSING:
1643 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001644 case NES_CM_STATE_MPAREQ_SENT:
1645 default:
1646 drop_packet(skb);
1647 break;
1648 }
1649}
1650
1651static void handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1652 struct tcphdr *tcph)
1653{
1654 int datasize = 0;
1655 u32 inc_sequence;
1656 u32 rem_seq_ack;
1657 u32 rem_seq;
Faisal Latifabb77252008-11-21 20:50:52 -06001658 int ret;
1659 int optionsize;
Faisal Latifabb77252008-11-21 20:50:52 -06001660 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001661
Faisal Latif6492cdf2008-07-24 20:50:45 -07001662 if (check_seq(cm_node, tcph, skb))
1663 return;
1664
1665 skb_pull(skb, tcph->doff << 2);
1666 inc_sequence = ntohl(tcph->seq);
1667 rem_seq = ntohl(tcph->seq);
1668 rem_seq_ack = ntohl(tcph->ack_seq);
1669 datasize = skb->len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001670 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001671 switch (cm_node->state) {
1672 case NES_CM_STATE_SYN_RCVD:
1673 /* Passive OPEN */
Faisal Latifabb77252008-11-21 20:50:52 -06001674 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1675 if (ret)
1676 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001677 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
Faisal Latifabb77252008-11-21 20:50:52 -06001678 if (cm_node->tcp_cntxt.rem_ack_num !=
1679 cm_node->tcp_cntxt.loc_seq_num) {
1680 nes_debug(NES_DBG_CM, "rem_ack_num != loc_seq_num\n");
1681 cleanup_retrans_entry(cm_node);
1682 send_reset(cm_node, skb);
1683 return;
1684 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001685 cm_node->state = NES_CM_STATE_ESTABLISHED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001686 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001687 if (datasize) {
1688 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001689 handle_rcv_mpa(cm_node, skb);
1690 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001691 dev_kfree_skb_any(skb);
1692 cleanup_retrans_entry(cm_node);
1693 }
1694 break;
1695 case NES_CM_STATE_ESTABLISHED:
1696 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001697 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001698 if (datasize) {
1699 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001700 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001701 } else
1702 drop_packet(skb);
1703 break;
1704 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001705 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001706 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1707 if (datasize) {
1708 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001709 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001710 } else { /* Could be just an ack pkt.. */
1711 cleanup_retrans_entry(cm_node);
1712 dev_kfree_skb_any(skb);
1713 }
1714 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001715 case NES_CM_STATE_LISTENING:
1716 case NES_CM_STATE_CLOSED:
1717 cleanup_retrans_entry(cm_node);
1718 send_reset(cm_node, skb);
1719 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001720 case NES_CM_STATE_LAST_ACK:
1721 cleanup_retrans_entry(cm_node);
1722 cm_node->state = NES_CM_STATE_CLOSED;
1723 cm_node->cm_id->rem_ref(cm_node->cm_id);
1724 case NES_CM_STATE_CLOSING:
1725 cleanup_retrans_entry(cm_node);
1726 rem_ref_cm_node(cm_node->cm_core, cm_node);
1727 drop_packet(skb);
1728 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001729 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001730 cleanup_retrans_entry(cm_node);
1731 drop_packet(skb);
1732 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1733 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001734 case NES_CM_STATE_SYN_SENT:
1735 case NES_CM_STATE_FIN_WAIT2:
1736 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001737 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001738 case NES_CM_STATE_UNKNOWN:
1739 default:
1740 drop_packet(skb);
1741 break;
1742 }
1743}
1744
1745
1746
1747static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1748 struct sk_buff *skb, int optionsize, int passive)
1749{
1750 u8 *optionsloc = (u8 *)&tcph[1];
1751 if (optionsize) {
1752 if (process_options(cm_node, optionsloc, optionsize,
1753 (u32)tcph->syn)) {
1754 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1755 __func__, cm_node);
1756 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06001757 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001758 else
Faisal Latifabb77252008-11-21 20:50:52 -06001759 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001760 return 1;
1761 }
1762 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001763
1764 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1765 cm_node->tcp_cntxt.snd_wscale;
1766
Faisal Latif6492cdf2008-07-24 20:50:45 -07001767 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001768 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001769 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001770}
1771
Faisal Latif6492cdf2008-07-24 20:50:45 -07001772/*
1773 * active_open_err() will send reset() if flag set..
1774 * It will also send ABORT event.
1775 */
1776
1777static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1778 int reset)
1779{
1780 cleanup_retrans_entry(cm_node);
1781 if (reset) {
1782 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1783 "state=%d\n", cm_node, cm_node->state);
1784 add_ref_cm_node(cm_node);
1785 send_reset(cm_node, skb);
1786 } else
1787 dev_kfree_skb_any(skb);
1788
1789 cm_node->state = NES_CM_STATE_CLOSED;
1790 create_event(cm_node, NES_CM_EVENT_ABORTED);
1791}
1792
1793/*
1794 * passive_open_err() will either do a reset() or will free up the skb and
1795 * remove the cm_node.
1796 */
1797
1798static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1799 int reset)
1800{
1801 cleanup_retrans_entry(cm_node);
1802 cm_node->state = NES_CM_STATE_CLOSED;
1803 if (reset) {
1804 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1805 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1806 send_reset(cm_node, skb);
1807 } else {
1808 dev_kfree_skb_any(skb);
1809 rem_ref_cm_node(cm_node->cm_core, cm_node);
1810 }
1811}
1812
1813/*
1814 * free_retrans_entry() routines assumes that the retrans_list_lock has
1815 * been acquired before calling.
1816 */
1817static void free_retrans_entry(struct nes_cm_node *cm_node)
1818{
1819 struct nes_timer_entry *send_entry;
1820 send_entry = cm_node->send_entry;
1821 if (send_entry) {
1822 cm_node->send_entry = NULL;
1823 dev_kfree_skb_any(send_entry->skb);
1824 kfree(send_entry);
1825 rem_ref_cm_node(cm_node->cm_core, cm_node);
1826 }
1827}
1828
1829static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1830{
1831 unsigned long flags;
1832
1833 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1834 free_retrans_entry(cm_node);
1835 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1836}
1837
1838/**
1839 * process_packet
1840 * Returns skb if to be freed, else it will return NULL if already used..
1841 */
1842static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1843 struct nes_cm_core *cm_core)
1844{
1845 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1846 struct tcphdr *tcph = tcp_hdr(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001847 u32 fin_set = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001848 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1849
1850 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1851 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1852 tcph->ack, tcph->rst, tcph->fin);
1853
1854 if (tcph->rst)
1855 pkt_type = NES_PKT_TYPE_RST;
1856 else if (tcph->syn) {
1857 pkt_type = NES_PKT_TYPE_SYN;
1858 if (tcph->ack)
1859 pkt_type = NES_PKT_TYPE_SYNACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001860 } else if (tcph->ack)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001861 pkt_type = NES_PKT_TYPE_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001862 if (tcph->fin)
1863 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001864
1865 switch (pkt_type) {
1866 case NES_PKT_TYPE_SYN:
1867 handle_syn_pkt(cm_node, skb, tcph);
1868 break;
1869 case NES_PKT_TYPE_SYNACK:
1870 handle_synack_pkt(cm_node, skb, tcph);
1871 break;
1872 case NES_PKT_TYPE_ACK:
1873 handle_ack_pkt(cm_node, skb, tcph);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001874 if (fin_set)
1875 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001876 break;
1877 case NES_PKT_TYPE_RST:
1878 handle_rst_pkt(cm_node, skb, tcph);
1879 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001880 default:
1881 drop_packet(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001882 if (fin_set)
1883 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001884 break;
1885 }
1886}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001887
1888/**
1889 * mini_cm_listen - create a listen node with params
1890 */
1891static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001892 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001893{
1894 struct nes_cm_listener *listener;
1895 unsigned long flags;
1896
1897 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1898 cm_info->loc_addr, cm_info->loc_port);
1899
1900 /* cannot have multiple matching listeners */
1901 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1902 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1903 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1904 /* find automatically incs ref count ??? */
1905 atomic_dec(&listener->ref_count);
1906 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1907 return NULL;
1908 }
1909
1910 if (!listener) {
1911 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1912 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1913 if (!listener) {
1914 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1915 return NULL;
1916 }
1917
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001918 listener->loc_addr = htonl(cm_info->loc_addr);
1919 listener->loc_port = htons(cm_info->loc_port);
1920 listener->reused_node = 0;
1921
1922 atomic_set(&listener->ref_count, 1);
1923 }
1924 /* pasive case */
1925 /* find already inc'ed the ref count */
1926 else {
1927 listener->reused_node = 1;
1928 }
1929
1930 listener->cm_id = cm_info->cm_id;
1931 atomic_set(&listener->pend_accepts_cnt, 0);
1932 listener->cm_core = cm_core;
1933 listener->nesvnic = nesvnic;
1934 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001935
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001936 listener->conn_type = cm_info->conn_type;
1937 listener->backlog = cm_info->backlog;
1938 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1939
1940 if (!listener->reused_node) {
1941 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1942 list_add(&listener->list, &cm_core->listen_list.list);
1943 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1944 atomic_inc(&cm_core->listen_node_cnt);
1945 }
1946
1947 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1948 " listener = %p, backlog = %d, cm_id = %p.\n",
1949 cm_info->loc_addr, cm_info->loc_port,
1950 listener, listener->backlog, listener->cm_id);
1951
1952 return listener;
1953}
1954
1955
1956/**
1957 * mini_cm_connect - make a connection node with params
1958 */
Faisal Latif54c86a82008-09-30 14:47:27 -07001959static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001960 struct nes_vnic *nesvnic, u16 private_data_len,
1961 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001962{
1963 int ret = 0;
1964 struct nes_cm_node *cm_node;
1965 struct nes_cm_listener *loopbackremotelistener;
1966 struct nes_cm_node *loopbackremotenode;
1967 struct nes_cm_info loopback_cm_info;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001968 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
1969 struct ietf_mpa_frame *mpa_frame = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001970
1971 /* create a CM connection node */
1972 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
1973 if (!cm_node)
1974 return NULL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001975 mpa_frame = &cm_node->mpa_frame;
1976 strcpy(mpa_frame->key, IEFT_MPA_KEY_REQ);
1977 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
1978 mpa_frame->rev = IETF_MPA_VERSION;
1979 mpa_frame->priv_data_len = htons(private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001980
Glenn Streiff7495ab62008-04-29 13:46:54 -07001981 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001982 cm_node->tcp_cntxt.client = 1;
1983 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1984
1985 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001986 loopbackremotelistener = find_listener(cm_core,
1987 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
1988 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001989 if (loopbackremotelistener == NULL) {
1990 create_event(cm_node, NES_CM_EVENT_ABORTED);
1991 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001992 loopback_cm_info = *cm_info;
1993 loopback_cm_info.loc_port = cm_info->rem_port;
1994 loopback_cm_info.rem_port = cm_info->loc_port;
1995 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001996 loopbackremotenode = make_cm_node(cm_core, nesvnic,
1997 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07001998 if (!loopbackremotenode) {
1999 rem_ref_cm_node(cm_node->cm_core, cm_node);
2000 return NULL;
2001 }
2002 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002003 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002004 loopbackremotenode->tcp_cntxt.rcv_wscale =
2005 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002006 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002007 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2008 private_data_len);
2009 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002010
Faisal Latif6492cdf2008-07-24 20:50:45 -07002011 /* we are done handling this state. */
2012 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002013 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002014 cm_node->tcp_cntxt.rcv_nxt =
2015 loopbackremotenode->tcp_cntxt.loc_seq_num;
2016 loopbackremotenode->tcp_cntxt.rcv_nxt =
2017 cm_node->tcp_cntxt.loc_seq_num;
2018 cm_node->tcp_cntxt.max_snd_wnd =
2019 loopbackremotenode->tcp_cntxt.rcv_wnd;
2020 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2021 cm_node->tcp_cntxt.rcv_wnd;
2022 cm_node->tcp_cntxt.snd_wnd =
2023 loopbackremotenode->tcp_cntxt.rcv_wnd;
2024 loopbackremotenode->tcp_cntxt.snd_wnd =
2025 cm_node->tcp_cntxt.rcv_wnd;
2026 cm_node->tcp_cntxt.snd_wscale =
2027 loopbackremotenode->tcp_cntxt.rcv_wscale;
2028 loopbackremotenode->tcp_cntxt.snd_wscale =
2029 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002030 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002031 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2032 }
2033 return cm_node;
2034 }
2035
2036 /* set our node side to client (active) side */
2037 cm_node->tcp_cntxt.client = 1;
2038 /* init our MPA frame ptr */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002039 memcpy(mpa_frame->priv_data, private_data, private_data_len);
2040
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002041 cm_node->mpa_frame_size = mpa_frame_size;
2042
2043 /* send a syn and goto syn sent state */
2044 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002045 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002046
Faisal Latif6492cdf2008-07-24 20:50:45 -07002047 if (ret) {
2048 /* error in sending the syn free up the cm_node struct */
2049 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2050 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2051 cm_node->rem_addr, cm_node->rem_port, cm_node,
2052 cm_node->cm_id);
2053 rem_ref_cm_node(cm_node->cm_core, cm_node);
2054 cm_node = NULL;
2055 }
2056
2057 if (cm_node)
2058 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2059 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2060 cm_node->rem_addr, cm_node->rem_port, cm_node,
2061 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002062
2063 return cm_node;
2064}
2065
2066
2067/**
2068 * mini_cm_accept - accept a connection
2069 * This function is never called
2070 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002071static int mini_cm_accept(struct nes_cm_core *cm_core,
2072 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002073{
2074 return 0;
2075}
2076
2077
2078/**
2079 * mini_cm_reject - reject and teardown a connection
2080 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002081static int mini_cm_reject(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002082 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002083{
2084 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002085 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002086 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002087 struct nes_cm_event event;
2088 struct iw_cm_id *cm_id = cm_node->cm_id;
2089 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002090
Faisal Latif6492cdf2008-07-24 20:50:45 -07002091 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2092 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002093
Faisal Latif6492cdf2008-07-24 20:50:45 -07002094 if (cm_node->tcp_cntxt.client)
2095 return ret;
2096 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002097
Faisal Latif9d5ab132009-03-06 15:15:01 -08002098 if (!loopback) {
2099 passive_state = atomic_add_return(1, &cm_node->passive_state);
2100 if (passive_state == NES_SEND_RESET_EVENT) {
2101 cm_node->state = NES_CM_STATE_CLOSED;
2102 rem_ref_cm_node(cm_core, cm_node);
2103 } else {
2104 ret = send_mpa_reject(cm_node);
2105 if (ret) {
2106 cm_node->state = NES_CM_STATE_CLOSED;
2107 err = send_reset(cm_node, NULL);
2108 if (err)
2109 WARN_ON(1);
2110 } else
2111 cm_id->add_ref(cm_id);
2112 }
2113 } else {
2114 cm_node->cm_id = NULL;
2115 event.cm_node = loopback;
2116 event.cm_info.rem_addr = loopback->rem_addr;
2117 event.cm_info.loc_addr = loopback->loc_addr;
2118 event.cm_info.rem_port = loopback->rem_port;
2119 event.cm_info.loc_port = loopback->loc_port;
2120 event.cm_info.cm_id = loopback->cm_id;
2121 cm_event_mpa_reject(&event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002122 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002123 loopback->state = NES_CM_STATE_CLOSING;
2124
2125 cm_id = loopback->cm_id;
2126 rem_ref_cm_node(cm_core, loopback);
2127 cm_id->rem_ref(cm_id);
2128 }
2129
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002130 return ret;
2131}
2132
2133
2134/**
2135 * mini_cm_close
2136 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002137static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002138{
2139 int ret = 0;
2140
2141 if (!cm_core || !cm_node)
2142 return -EINVAL;
2143
2144 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002145 case NES_CM_STATE_SYN_RCVD:
2146 case NES_CM_STATE_SYN_SENT:
2147 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2148 case NES_CM_STATE_ESTABLISHED:
2149 case NES_CM_STATE_ACCEPTING:
2150 case NES_CM_STATE_MPAREQ_SENT:
2151 case NES_CM_STATE_MPAREQ_RCVD:
2152 cleanup_retrans_entry(cm_node);
2153 send_reset(cm_node, NULL);
2154 break;
2155 case NES_CM_STATE_CLOSE_WAIT:
2156 cm_node->state = NES_CM_STATE_LAST_ACK;
2157 send_fin(cm_node, NULL);
2158 break;
2159 case NES_CM_STATE_FIN_WAIT1:
2160 case NES_CM_STATE_FIN_WAIT2:
2161 case NES_CM_STATE_LAST_ACK:
2162 case NES_CM_STATE_TIME_WAIT:
2163 case NES_CM_STATE_CLOSING:
2164 ret = -1;
2165 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002166 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002167 case NES_CM_STATE_LISTENING:
2168 case NES_CM_STATE_UNKNOWN:
2169 case NES_CM_STATE_INITED:
2170 case NES_CM_STATE_CLOSED:
2171 ret = rem_ref_cm_node(cm_core, cm_node);
2172 break;
2173 case NES_CM_STATE_TSA:
2174 if (cm_node->send_entry)
2175 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2176 "send_entry=%p\n", cm_node->send_entry);
2177 ret = rem_ref_cm_node(cm_core, cm_node);
2178 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002179 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002180 return ret;
2181}
2182
2183
2184/**
2185 * recv_pkt - recv an ETHERNET packet, and process it through CM
2186 * node state machine
2187 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002188static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002189 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002190{
2191 struct nes_cm_node *cm_node = NULL;
2192 struct nes_cm_listener *listener = NULL;
2193 struct iphdr *iph;
2194 struct tcphdr *tcph;
2195 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002196 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002197 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002198
Faisal Latif6492cdf2008-07-24 20:50:45 -07002199 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002200 return 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002201 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002202 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002203 }
2204
2205 iph = (struct iphdr *)skb->data;
2206 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002207
2208 nfo.loc_addr = ntohl(iph->daddr);
2209 nfo.loc_port = ntohs(tcph->dest);
2210 nfo.rem_addr = ntohl(iph->saddr);
2211 nfo.rem_port = ntohs(tcph->source);
2212
Harvey Harrison03080e52009-01-10 21:45:42 -08002213 tmp_daddr = cpu_to_be32(iph->daddr);
2214 tmp_saddr = cpu_to_be32(iph->saddr);
2215
Harvey Harrison63779432008-10-31 00:56:00 -07002216 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002217 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002218
Faisal Latif6492cdf2008-07-24 20:50:45 -07002219 do {
2220 cm_node = find_node(cm_core,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002221 nfo.rem_port, nfo.rem_addr,
2222 nfo.loc_port, nfo.loc_addr);
2223
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002224 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002225 /* Only type of packet accepted are for */
2226 /* the PASSIVE open (syn only) */
2227 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002228 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002229 break;
2230 }
2231 listener = find_listener(cm_core, nfo.loc_addr,
2232 nfo.loc_port,
2233 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002234 if (!listener) {
2235 nfo.cm_id = NULL;
2236 nfo.conn_type = 0;
2237 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2238 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002239 break;
2240 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002241 nfo.cm_id = listener->cm_id;
2242 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002243 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2244 listener);
2245 if (!cm_node) {
2246 nes_debug(NES_DBG_CM, "Unable to allocate "
2247 "node\n");
2248 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002249 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002250 dev_kfree_skb_any(skb);
2251 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002252 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002253 if (!tcph->rst && !tcph->fin) {
2254 cm_node->state = NES_CM_STATE_LISTENING;
2255 } else {
2256 cm_packets_dropped++;
2257 rem_ref_cm_node(cm_core, cm_node);
2258 dev_kfree_skb_any(skb);
2259 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002260 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002261 add_ref_cm_node(cm_node);
2262 } else if (cm_node->state == NES_CM_STATE_TSA) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002263 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002264 atomic_inc(&cm_accel_dropped_pkts);
2265 dev_kfree_skb_any(skb);
2266 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002267 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002268 skb_reset_network_header(skb);
2269 skb_set_transport_header(skb, sizeof(*tcph));
2270 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002271 process_packet(cm_node, skb, cm_core);
2272 rem_ref_cm_node(cm_core, cm_node);
2273 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002274 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002275}
2276
2277
2278/**
2279 * nes_cm_alloc_core - allocate a top level instance of a cm core
2280 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002281static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002282{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002283 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002284
2285 /* setup the CM core */
2286 /* alloc top level core control structure */
2287 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2288 if (!cm_core)
2289 return NULL;
2290
2291 INIT_LIST_HEAD(&cm_core->connected_nodes);
2292 init_timer(&cm_core->tcp_timer);
2293 cm_core->tcp_timer.function = nes_cm_timer_tick;
2294
2295 cm_core->mtu = NES_CM_DEFAULT_MTU;
2296 cm_core->state = NES_CM_STATE_INITED;
2297 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2298
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002299 atomic_set(&cm_core->events_posted, 0);
2300
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002301 cm_core->api = &nes_cm_api;
2302
2303 spin_lock_init(&cm_core->ht_lock);
2304 spin_lock_init(&cm_core->listen_list_lock);
2305
2306 INIT_LIST_HEAD(&cm_core->listen_list.list);
2307
2308 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2309
2310 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2311 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2312 cm_core->post_event = nes_cm_post_event;
2313 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2314 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2315
2316 print_core(cm_core);
2317 return cm_core;
2318}
2319
2320
2321/**
2322 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2323 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002324static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002325{
2326 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2327
2328 if (!cm_core)
2329 return -EINVAL;
2330
2331 barrier();
2332
2333 if (timer_pending(&cm_core->tcp_timer)) {
2334 del_timer(&cm_core->tcp_timer);
2335 }
2336
2337 destroy_workqueue(cm_core->event_wq);
2338 destroy_workqueue(cm_core->disconn_wq);
2339 nes_debug(NES_DBG_CM, "\n");
2340 kfree(cm_core);
2341
2342 return 0;
2343}
2344
2345
2346/**
2347 * mini_cm_get
2348 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002349static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002350{
2351 return cm_core->state;
2352}
2353
2354
2355/**
2356 * mini_cm_set
2357 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002358static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002359{
2360 int ret = 0;
2361
2362 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002363 case NES_CM_SET_PKT_SIZE:
2364 cm_core->mtu = value;
2365 break;
2366 case NES_CM_SET_FREE_PKT_Q_SIZE:
2367 cm_core->free_tx_pkt_max = value;
2368 break;
2369 default:
2370 /* unknown set option */
2371 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002372 }
2373
2374 return ret;
2375}
2376
2377
2378/**
2379 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2380 * successfully exchanged when this is called
2381 */
2382static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2383{
2384 int ret = 0;
2385
2386 if (!nesqp)
2387 return -EINVAL;
2388
2389 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2390 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2391 NES_QPCONTEXT_MISC_DROS);
2392
2393 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2394 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2395
2396 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2397
2398 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2399
2400 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2401 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2402
2403 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2404 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2405 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2406
2407 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2408 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2409 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2410
2411 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2412 nesqp->nesqp_context->ts_recent = 0;
2413 nesqp->nesqp_context->ts_age = 0;
2414 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2415 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2416 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2417 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2418 cm_node->tcp_cntxt.rcv_wscale);
2419 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2420 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2421 nesqp->nesqp_context->srtt = 0;
2422 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2423 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2424 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2425 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2426 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2427 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2428
2429 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2430 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2431 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2432 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2433 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2434 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2435 le32_to_cpu(nesqp->nesqp_context->misc));
2436 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2437 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2438 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2439
2440 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2441 cm_node->state = NES_CM_STATE_TSA;
2442
2443 return ret;
2444}
2445
2446
2447/**
2448 * nes_cm_disconn
2449 */
2450int nes_cm_disconn(struct nes_qp *nesqp)
2451{
2452 unsigned long flags;
2453
2454 spin_lock_irqsave(&nesqp->lock, flags);
2455 if (nesqp->disconn_pending == 0) {
2456 nesqp->disconn_pending++;
2457 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002458 /* init our disconnect work element, to */
2459 INIT_WORK(&nesqp->disconn_work, nes_disconnect_worker);
2460
2461 queue_work(g_cm_core->disconn_wq, &nesqp->disconn_work);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002462 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002463 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002464
2465 return 0;
2466}
2467
2468
2469/**
2470 * nes_disconnect_worker
2471 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002472static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002473{
2474 struct nes_qp *nesqp = container_of(work, struct nes_qp, disconn_work);
2475
2476 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2477 nesqp->last_aeq, nesqp->hwqp.qp_id);
2478 nes_cm_disconn_true(nesqp);
2479}
2480
2481
2482/**
2483 * nes_cm_disconn_true
2484 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002485static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002486{
2487 unsigned long flags;
2488 int ret = 0;
2489 struct iw_cm_id *cm_id;
2490 struct iw_cm_event cm_event;
2491 struct nes_vnic *nesvnic;
2492 u16 last_ae;
2493 u8 original_hw_tcp_state;
2494 u8 original_ibqp_state;
2495 u8 issued_disconnect_reset = 0;
2496
2497 if (!nesqp) {
2498 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2499 return -1;
2500 }
2501
2502 spin_lock_irqsave(&nesqp->lock, flags);
2503 cm_id = nesqp->cm_id;
2504 /* make sure we havent already closed this connection */
2505 if (!cm_id) {
2506 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2507 nesqp->hwqp.qp_id);
2508 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002509 return -1;
2510 }
2511
2512 nesvnic = to_nesvnic(nesqp->ibqp.device);
2513 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2514
2515 original_hw_tcp_state = nesqp->hw_tcp_state;
2516 original_ibqp_state = nesqp->ibqp_state;
2517 last_ae = nesqp->last_aeq;
2518
2519
2520 nes_debug(NES_DBG_CM, "set ibqp_state=%u\n", nesqp->ibqp_state);
2521
2522 if ((nesqp->cm_id) && (cm_id->event_handler)) {
2523 if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2524 ((original_ibqp_state == IB_QPS_RTS) &&
2525 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2526 atomic_inc(&cm_disconnects);
2527 cm_event.event = IW_CM_EVENT_DISCONNECT;
2528 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002529 cm_event.status = IW_CM_EVENT_STATUS_RESET;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002530 nes_debug(NES_DBG_CM, "Generating a CM "
2531 "Disconnect Event (status reset) for "
2532 "QP%u, cm_id = %p. \n",
2533 nesqp->hwqp.qp_id, cm_id);
2534 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002535 cm_event.status = IW_CM_EVENT_STATUS_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002536
2537 cm_event.local_addr = cm_id->local_addr;
2538 cm_event.remote_addr = cm_id->remote_addr;
2539 cm_event.private_data = NULL;
2540 cm_event.private_data_len = 0;
2541
Faisal Latif6492cdf2008-07-24 20:50:45 -07002542 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2543 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2544 "cm_id = %p, refcount = %u.\n",
2545 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2546 nesqp->hwqp.sq_tail, cm_id,
2547 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002548
2549 spin_unlock_irqrestore(&nesqp->lock, flags);
2550 ret = cm_id->event_handler(cm_id, &cm_event);
2551 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002552 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2553 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002554 spin_lock_irqsave(&nesqp->lock, flags);
2555 }
2556
2557 nesqp->disconn_pending = 0;
2558 /* There might have been another AE while the lock was released */
2559 original_hw_tcp_state = nesqp->hw_tcp_state;
2560 original_ibqp_state = nesqp->ibqp_state;
2561 last_ae = nesqp->last_aeq;
2562
2563 if ((issued_disconnect_reset == 0) && (nesqp->cm_id) &&
2564 ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2565 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2566 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2567 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2568 atomic_inc(&cm_closes);
2569 nesqp->cm_id = NULL;
2570 nesqp->in_disconnect = 0;
2571 spin_unlock_irqrestore(&nesqp->lock, flags);
2572 nes_disconnect(nesqp, 1);
2573
2574 cm_id->provider_data = nesqp;
2575 /* Send up the close complete event */
2576 cm_event.event = IW_CM_EVENT_CLOSE;
2577 cm_event.status = IW_CM_EVENT_STATUS_OK;
2578 cm_event.provider_data = cm_id->provider_data;
2579 cm_event.local_addr = cm_id->local_addr;
2580 cm_event.remote_addr = cm_id->remote_addr;
2581 cm_event.private_data = NULL;
2582 cm_event.private_data_len = 0;
2583
2584 ret = cm_id->event_handler(cm_id, &cm_event);
2585 if (ret) {
2586 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2587 }
2588
2589 cm_id->rem_ref(cm_id);
2590
2591 spin_lock_irqsave(&nesqp->lock, flags);
2592 if (nesqp->flush_issued == 0) {
2593 nesqp->flush_issued = 1;
2594 spin_unlock_irqrestore(&nesqp->lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002595 flush_wqes(nesvnic->nesdev, nesqp,
2596 NES_CQP_FLUSH_RQ, 1);
2597 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002598 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002599 } else {
2600 cm_id = nesqp->cm_id;
2601 spin_unlock_irqrestore(&nesqp->lock, flags);
2602 /* check to see if the inbound reset beat the outbound reset */
2603 if ((!cm_id) && (last_ae==NES_AEQE_AEID_RESET_SENT)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002604 nes_debug(NES_DBG_CM, "QP%u: Decing refcount "
2605 "due to inbound reset beating the "
2606 "outbound reset.\n", nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002607 }
2608 }
2609 } else {
2610 nesqp->disconn_pending = 0;
2611 spin_unlock_irqrestore(&nesqp->lock, flags);
2612 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002613
2614 return 0;
2615}
2616
2617
2618/**
2619 * nes_disconnect
2620 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002621static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002622{
2623 int ret = 0;
2624 struct nes_vnic *nesvnic;
2625 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002626 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002627
2628 nesvnic = to_nesvnic(nesqp->ibqp.device);
2629 if (!nesvnic)
2630 return -EINVAL;
2631
2632 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002633 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002634
2635 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2636 atomic_read(&nesvnic->netdev->refcnt));
2637
2638 if (nesqp->active_conn) {
2639
2640 /* indicate this connection is NOT active */
2641 nesqp->active_conn = 0;
2642 } else {
2643 /* Need to free the Last Streaming Mode Message */
2644 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002645 if (nesqp->lsmm_mr)
2646 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002647 pci_free_consistent(nesdev->pcidev,
2648 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2649 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2650 }
2651 }
2652
2653 /* close the CM node down if it is still active */
2654 if (nesqp->cm_node) {
2655 nes_debug(NES_DBG_CM, "Call close API\n");
2656
2657 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002658 }
2659
2660 return ret;
2661}
2662
2663
2664/**
2665 * nes_accept
2666 */
2667int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2668{
2669 u64 u64temp;
2670 struct ib_qp *ibqp;
2671 struct nes_qp *nesqp;
2672 struct nes_vnic *nesvnic;
2673 struct nes_device *nesdev;
2674 struct nes_cm_node *cm_node;
2675 struct nes_adapter *adapter;
2676 struct ib_qp_attr attr;
2677 struct iw_cm_event cm_event;
2678 struct nes_hw_qp_wqe *wqe;
2679 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002680 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002681 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002682 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002683 struct nes_ib_device *nesibdev;
2684 struct ib_mr *ibmr = NULL;
2685 struct ib_phys_buf ibphysbuf;
2686 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002687 u64 tagged_offset;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002688
2689
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002690
2691 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2692 if (!ibqp)
2693 return -EINVAL;
2694
2695 /* get all our handles */
2696 nesqp = to_nesqp(ibqp);
2697 nesvnic = to_nesvnic(nesqp->ibqp.device);
2698 nesdev = nesvnic->nesdev;
2699 adapter = nesdev->nesadapter;
2700
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002701 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002702 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2703 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2704 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002705
2706 /* associate the node with the QP */
2707 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002708 cm_node->nesqp = nesqp;
2709 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002710
Faisal Latif6492cdf2008-07-24 20:50:45 -07002711 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2712 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002713 atomic_inc(&cm_accepts);
2714
2715 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2716 atomic_read(&nesvnic->netdev->refcnt));
2717
Faisal Latif6492cdf2008-07-24 20:50:45 -07002718 /* allocate the ietf frame and space for private data */
2719 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2720 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2721 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002722
Faisal Latif6492cdf2008-07-24 20:50:45 -07002723 if (!nesqp->ietf_frame) {
2724 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2725 "data\n");
2726 return -ENOMEM;
2727 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002728
2729
Faisal Latif6492cdf2008-07-24 20:50:45 -07002730 /* setup the MPA frame */
2731 nesqp->private_data_len = conn_param->private_data_len;
2732 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002733
Faisal Latif6492cdf2008-07-24 20:50:45 -07002734 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2735 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002736
Faisal Latif6492cdf2008-07-24 20:50:45 -07002737 nesqp->ietf_frame->priv_data_len =
2738 cpu_to_be16(conn_param->private_data_len);
2739 nesqp->ietf_frame->rev = mpa_version;
2740 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002741
Faisal Latif6492cdf2008-07-24 20:50:45 -07002742 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2743 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002744
Faisal Latif6492cdf2008-07-24 20:50:45 -07002745 if (cm_id->remote_addr.sin_addr.s_addr !=
2746 cm_id->local_addr.sin_addr.s_addr) {
2747 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002748 nesibdev = nesvnic->nesibdev;
2749 nespd = nesqp->nespd;
2750 ibphysbuf.addr = nesqp->ietf_frame_pbase;
2751 ibphysbuf.size = conn_param->private_data_len +
2752 sizeof(struct ietf_mpa_frame);
Don Wood7a5efb62009-04-08 14:21:02 -07002753 tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002754 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
2755 &ibphysbuf, 1,
2756 IB_ACCESS_LOCAL_WRITE,
Don Wood7a5efb62009-04-08 14:21:02 -07002757 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002758 if (!ibmr) {
2759 nes_debug(NES_DBG_CM, "Unable to register memory region"
2760 "for lSMM for cm_node = %p \n",
2761 cm_node);
2762 return -ENOMEM;
2763 }
2764
2765 ibmr->pd = &nespd->ibpd;
2766 ibmr->device = nespd->ibpd.device;
2767 nesqp->lsmm_mr = ibmr;
2768
Faisal Latif6492cdf2008-07-24 20:50:45 -07002769 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2770 set_wqe_64bit_value(wqe->wqe_words,
2771 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2772 u64temp);
2773 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2774 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2775 NES_IWARP_SQ_WQE_WRPDU);
2776 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2777 cpu_to_le32(conn_param->private_data_len +
2778 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002779 set_wqe_64bit_value(wqe->wqe_words,
2780 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
Don Wood7a5efb62009-04-08 14:21:02 -07002781 (u64)(unsigned long)nesqp->ietf_frame);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002782 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2783 cpu_to_le32(conn_param->private_data_len +
2784 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002785 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002786
Faisal Latif6492cdf2008-07-24 20:50:45 -07002787 nesqp->nesqp_context->ird_ord_sizes |=
2788 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2789 NES_QPCONTEXT_ORDIRD_WRPDU);
2790 } else {
2791 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08002792 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002793 }
2794 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002795
2796
2797 /* Cache the cm_id in the qp */
2798 nesqp->cm_id = cm_id;
2799 cm_node->cm_id = cm_id;
2800
2801 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2802 cm_id->provider_data = nesqp;
2803 nesqp->active_conn = 0;
2804
Faisal Latif6492cdf2008-07-24 20:50:45 -07002805 if (cm_node->state == NES_CM_STATE_TSA)
2806 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2807 cm_node);
2808
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002809 nes_cm_init_tsa_conn(nesqp, cm_node);
2810
Faisal Latif6492cdf2008-07-24 20:50:45 -07002811 nesqp->nesqp_context->tcpPorts[0] =
2812 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2813 nesqp->nesqp_context->tcpPorts[1] =
2814 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2815
2816 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2817 nesqp->nesqp_context->ip0 =
2818 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2819 else
2820 nesqp->nesqp_context->ip0 =
2821 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002822
2823 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002824 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2825 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002826
Faisal Latif6492cdf2008-07-24 20:50:45 -07002827 nesqp->nesqp_context->arp_index_vlan |=
2828 cpu_to_le32(nes_arp_table(nesdev,
2829 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002830 NES_ARP_RESOLVE) << 16);
2831
2832 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002833 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002834
2835 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2836
2837 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002838 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2839 nesqp->nesqp_context->ird_ord_sizes |=
2840 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002841
2842 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002843 nes_quad.DstIpAdrIndex =
2844 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2845 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2846 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2847 else
2848 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2849 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2850 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002851
2852 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06002853 crc_value = get_crc_value(&nes_quad);
2854 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002855 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07002856 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002857
2858 nesqp->hte_index &= adapter->hte_index_mask;
2859 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2860
2861 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2862
Faisal Latif6492cdf2008-07-24 20:50:45 -07002863 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2864 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2865 "private data length=%zu.\n", nesqp->hwqp.qp_id,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002866 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2867 ntohs(cm_id->remote_addr.sin_port),
2868 ntohl(cm_id->local_addr.sin_addr.s_addr),
2869 ntohs(cm_id->local_addr.sin_port),
2870 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2871 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002872 conn_param->private_data_len +
2873 sizeof(struct ietf_mpa_frame));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002874
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002875
Coly Li73ac36e2009-01-07 18:09:16 -08002876 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002877 cm_id->add_ref(cm_id);
2878
2879 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2880 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2881 cm_event.provider_data = (void *)nesqp;
2882 cm_event.local_addr = cm_id->local_addr;
2883 cm_event.remote_addr = cm_id->remote_addr;
2884 cm_event.private_data = NULL;
2885 cm_event.private_data_len = 0;
2886 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002887 attr.qp_state = IB_QPS_RTS;
2888 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002889 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002890 cm_node->loopbackpartner->mpa_frame_size =
2891 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002892 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002893 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2894 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002895 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2896 }
2897 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002898 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2899 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002900
Faisal Latif183ecfa2008-11-21 20:50:46 -06002901 passive_state = atomic_add_return(1, &cm_node->passive_state);
2902 if (passive_state == NES_SEND_RESET_EVENT)
2903 create_event(cm_node, NES_CM_EVENT_RESET);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002904 return 0;
2905}
2906
2907
2908/**
2909 * nes_reject
2910 */
2911int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2912{
2913 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002914 struct nes_cm_node *loopback;
2915
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002916 struct nes_cm_core *cm_core;
2917
2918 atomic_inc(&cm_rejects);
2919 cm_node = (struct nes_cm_node *) cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002920 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002921 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002922 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002923 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2924
Faisal Latif9d5ab132009-03-06 15:15:01 -08002925 if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
2926 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002927
Faisal Latif9d5ab132009-03-06 15:15:01 -08002928 strcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP);
2929 if (loopback) {
2930 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
2931 loopback->mpa_frame.priv_data_len = pdata_len;
2932 loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
2933 pdata_len;
2934 } else {
2935 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2936 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2937 }
2938
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002939 cm_node->mpa_frame.rev = mpa_version;
2940 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2941
Faisal Latif9d5ab132009-03-06 15:15:01 -08002942 return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002943}
2944
2945
2946/**
2947 * nes_connect
2948 * setup and launch cm connect node
2949 */
2950int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2951{
2952 struct ib_qp *ibqp;
2953 struct nes_qp *nesqp;
2954 struct nes_vnic *nesvnic;
2955 struct nes_device *nesdev;
2956 struct nes_cm_node *cm_node;
2957 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07002958 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002959
2960 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2961 if (!ibqp)
2962 return -EINVAL;
2963 nesqp = to_nesqp(ibqp);
2964 if (!nesqp)
2965 return -EINVAL;
2966 nesvnic = to_nesvnic(nesqp->ibqp.device);
2967 if (!nesvnic)
2968 return -EINVAL;
2969 nesdev = nesvnic->nesdev;
2970 if (!nesdev)
2971 return -EINVAL;
2972
Faisal Latif6492cdf2008-07-24 20:50:45 -07002973 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
2974 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
2975 ntohl(nesvnic->local_ipaddr),
2976 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2977 ntohs(cm_id->remote_addr.sin_port),
2978 ntohl(cm_id->local_addr.sin_addr.s_addr),
2979 ntohs(cm_id->local_addr.sin_port));
2980
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002981 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002982 nesqp->active_conn = 1;
2983
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002984 /* cache the cm_id in the qp */
2985 nesqp->cm_id = cm_id;
2986
2987 cm_id->provider_data = nesqp;
2988
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002989 nesqp->private_data_len = conn_param->private_data_len;
2990 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
2991 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002992 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
2993 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002994
Faisal Latif6492cdf2008-07-24 20:50:45 -07002995 if (cm_id->local_addr.sin_addr.s_addr !=
Faisal Latif53094c32009-04-27 13:37:34 -07002996 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002997 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002998 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07002999 apbvt_set = 1;
3000 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003001
3002 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003003 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3004 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3005 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3006 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003007 cm_info.cm_id = cm_id;
3008 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3009
3010 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003011
3012 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003013 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3014 conn_param->private_data_len, (void *)conn_param->private_data,
3015 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003016 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003017 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003018 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003019 PCI_FUNC(nesdev->pcidev->devfn),
3020 NES_MANAGE_APBVT_DEL);
3021
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003022 cm_id->rem_ref(cm_id);
3023 return -ENOMEM;
3024 }
3025
Faisal Latif53094c32009-04-27 13:37:34 -07003026 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003027 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003028 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003029 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003030
3031 return 0;
3032}
3033
3034
3035/**
3036 * nes_create_listen
3037 */
3038int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3039{
3040 struct nes_vnic *nesvnic;
3041 struct nes_cm_listener *cm_node;
3042 struct nes_cm_info cm_info;
3043 struct nes_adapter *adapter;
3044 int err;
3045
3046
3047 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3048 cm_id, ntohs(cm_id->local_addr.sin_port));
3049
3050 nesvnic = to_nesvnic(cm_id->device);
3051 if (!nesvnic)
3052 return -EINVAL;
3053 adapter = nesvnic->nesdev->nesadapter;
3054 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3055 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3056
3057 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3058 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3059
3060 /* setup listen params in our api call struct */
3061 cm_info.loc_addr = nesvnic->local_ipaddr;
3062 cm_info.loc_port = cm_id->local_addr.sin_port;
3063 cm_info.backlog = backlog;
3064 cm_info.cm_id = cm_id;
3065
3066 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3067
3068
3069 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3070 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003071 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003072 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003073 return -ENOMEM;
3074 }
3075
3076 cm_id->provider_data = cm_node;
3077
3078 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003079 err = nes_manage_apbvt(nesvnic,
3080 ntohs(cm_id->local_addr.sin_port),
3081 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3082 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003083 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003084 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3085 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003086 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3087 return err;
3088 }
3089 cm_listens_created++;
3090 }
3091
3092 cm_id->add_ref(cm_id);
3093 cm_id->provider_data = (void *)cm_node;
3094
3095
3096 return 0;
3097}
3098
3099
3100/**
3101 * nes_destroy_listen
3102 */
3103int nes_destroy_listen(struct iw_cm_id *cm_id)
3104{
3105 if (cm_id->provider_data)
3106 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3107 else
3108 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3109
3110 cm_id->rem_ref(cm_id);
3111
3112 return 0;
3113}
3114
3115
3116/**
3117 * nes_cm_recv
3118 */
3119int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3120{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003121 int rc = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003122 cm_packets_received++;
3123 if ((g_cm_core) && (g_cm_core->api)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003124 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003125 } else {
3126 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3127 " cm is not setup properly.\n");
3128 }
3129
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003130 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003131}
3132
3133
3134/**
3135 * nes_cm_start
3136 * Start and init a cm core module
3137 */
3138int nes_cm_start(void)
3139{
3140 nes_debug(NES_DBG_CM, "\n");
3141 /* create the primary CM core, pass this handle to subsequent core inits */
3142 g_cm_core = nes_cm_alloc_core();
3143 if (g_cm_core) {
3144 return 0;
3145 } else {
3146 return -ENOMEM;
3147 }
3148}
3149
3150
3151/**
3152 * nes_cm_stop
3153 * stop and dealloc all cm core instances
3154 */
3155int nes_cm_stop(void)
3156{
3157 g_cm_core->api->destroy_cm_core(g_cm_core);
3158 return 0;
3159}
3160
3161
3162/**
3163 * cm_event_connected
3164 * handle a connected event, setup QPs and HW
3165 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003166static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003167{
3168 u64 u64temp;
3169 struct nes_qp *nesqp;
3170 struct nes_vnic *nesvnic;
3171 struct nes_device *nesdev;
3172 struct nes_cm_node *cm_node;
3173 struct nes_adapter *nesadapter;
3174 struct ib_qp_attr attr;
3175 struct iw_cm_id *cm_id;
3176 struct iw_cm_event cm_event;
3177 struct nes_hw_qp_wqe *wqe;
3178 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003179 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003180 int ret;
3181
3182 /* get all our handles */
3183 cm_node = event->cm_node;
3184 cm_id = cm_node->cm_id;
3185 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3186 nesqp = (struct nes_qp *)cm_id->provider_data;
3187 nesvnic = to_nesvnic(nesqp->ibqp.device);
3188 nesdev = nesvnic->nesdev;
3189 nesadapter = nesdev->nesadapter;
3190
3191 if (nesqp->destroyed) {
3192 return;
3193 }
3194 atomic_inc(&cm_connecteds);
3195 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3196 " local port 0x%04X. jiffies = %lu.\n",
3197 nesqp->hwqp.qp_id,
3198 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3199 ntohs(cm_id->remote_addr.sin_port),
3200 ntohs(cm_id->local_addr.sin_port),
3201 jiffies);
3202
3203 nes_cm_init_tsa_conn(nesqp, cm_node);
3204
3205 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003206 nesqp->nesqp_context->tcpPorts[0] =
3207 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3208 nesqp->nesqp_context->tcpPorts[1] =
3209 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3210 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3211 nesqp->nesqp_context->ip0 =
3212 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3213 else
3214 nesqp->nesqp_context->ip0 =
3215 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003216
3217 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003218 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3219 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003220 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003221 nes_arp_table(nesdev,
3222 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003223 NULL, NES_ARP_RESOLVE) << 16);
3224 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3225 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3226 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3227 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003228 cpu_to_le32((u32)1 <<
3229 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003230
3231 /* Adjust tail for not having a LSMM */
3232 nesqp->hwqp.sq_tail = 1;
3233
3234#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003235 if (cm_node->send_write0) {
3236 nes_debug(NES_DBG_CM, "Sending first write.\n");
3237 wqe = &nesqp->hwqp.sq_vbase[0];
3238 u64temp = (unsigned long)nesqp;
3239 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3240 set_wqe_64bit_value(wqe->wqe_words,
3241 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3242 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3243 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3244 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3245 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3246 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3247 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3248 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003249
Faisal Latif6492cdf2008-07-24 20:50:45 -07003250 /* use the reserved spot on the WQ for the extra first WQE */
3251 nesqp->nesqp_context->ird_ord_sizes &=
3252 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3253 NES_QPCONTEXT_ORDIRD_WRPDU |
3254 NES_QPCONTEXT_ORDIRD_ALSMM));
3255 nesqp->skip_lsmm = 1;
3256 nesqp->hwqp.sq_tail = 0;
3257 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3258 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3259 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003260#endif
3261
3262 memset(&nes_quad, 0, sizeof(nes_quad));
3263
Faisal Latif6492cdf2008-07-24 20:50:45 -07003264 nes_quad.DstIpAdrIndex =
3265 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3266 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3267 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3268 else
3269 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003270 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3271 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3272
3273 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003274 crc_value = get_crc_value(&nes_quad);
3275 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003276 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3277 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3278
3279 nesqp->hte_index &= nesadapter->hte_index_mask;
3280 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3281
3282 nesqp->ietf_frame = &cm_node->mpa_frame;
3283 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3284 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3285
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003286 /* notify OF layer we successfully created the requested connection */
3287 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3288 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3289 cm_event.provider_data = cm_id->provider_data;
3290 cm_event.local_addr.sin_family = AF_INET;
3291 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3292 cm_event.remote_addr = cm_id->remote_addr;
3293
Faisal Latif6492cdf2008-07-24 20:50:45 -07003294 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3295 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003296
3297 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3298 ret = cm_id->event_handler(cm_id, &cm_event);
3299 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3300
3301 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003302 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3303 "ret=%d\n", __func__, __LINE__, ret);
3304 attr.qp_state = IB_QPS_RTS;
3305 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003306
Faisal Latif6492cdf2008-07-24 20:50:45 -07003307 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3308 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003309
3310 return;
3311}
3312
3313
3314/**
3315 * cm_event_connect_error
3316 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003317static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003318{
3319 struct nes_qp *nesqp;
3320 struct iw_cm_id *cm_id;
3321 struct iw_cm_event cm_event;
3322 /* struct nes_cm_info cm_info; */
3323 int ret;
3324
3325 if (!event->cm_node)
3326 return;
3327
3328 cm_id = event->cm_node->cm_id;
3329 if (!cm_id) {
3330 return;
3331 }
3332
3333 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3334 nesqp = cm_id->provider_data;
3335
3336 if (!nesqp) {
3337 return;
3338 }
3339
3340 /* notify OF layer about this connection error event */
3341 /* cm_id->rem_ref(cm_id); */
3342 nesqp->cm_id = NULL;
3343 cm_id->provider_data = NULL;
3344 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3345 cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
3346 cm_event.provider_data = cm_id->provider_data;
3347 cm_event.local_addr = cm_id->local_addr;
3348 cm_event.remote_addr = cm_id->remote_addr;
3349 cm_event.private_data = NULL;
3350 cm_event.private_data_len = 0;
3351
Faisal Latif6492cdf2008-07-24 20:50:45 -07003352 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3353 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3354 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003355
3356 ret = cm_id->event_handler(cm_id, &cm_event);
3357 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3358 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003359 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3360 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003361 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003362
Faisal Latif6492cdf2008-07-24 20:50:45 -07003363 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003364 return;
3365}
3366
3367
3368/**
3369 * cm_event_reset
3370 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003371static void cm_event_reset(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 if (!event->cm_node->cm_id)
3383 return;
3384
3385 cm_id = event->cm_node->cm_id;
3386
3387 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3388 nesqp = cm_id->provider_data;
3389
3390 nesqp->cm_id = NULL;
3391 /* cm_id->provider_data = NULL; */
3392 cm_event.event = IW_CM_EVENT_DISCONNECT;
3393 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3394 cm_event.provider_data = cm_id->provider_data;
3395 cm_event.local_addr = cm_id->local_addr;
3396 cm_event.remote_addr = cm_id->remote_addr;
3397 cm_event.private_data = NULL;
3398 cm_event.private_data_len = 0;
3399
3400 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003401 cm_id->add_ref(cm_id);
3402 atomic_inc(&cm_closes);
3403 cm_event.event = IW_CM_EVENT_CLOSE;
3404 cm_event.status = IW_CM_EVENT_STATUS_OK;
3405 cm_event.provider_data = cm_id->provider_data;
3406 cm_event.local_addr = cm_id->local_addr;
3407 cm_event.remote_addr = cm_id->remote_addr;
3408 cm_event.private_data = NULL;
3409 cm_event.private_data_len = 0;
3410 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3411 ret = cm_id->event_handler(cm_id, &cm_event);
3412
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003413 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3414
3415
3416 /* notify OF layer about this connection error event */
3417 cm_id->rem_ref(cm_id);
3418
3419 return;
3420}
3421
3422
3423/**
3424 * cm_event_mpa_req
3425 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003426static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003427{
3428 struct iw_cm_id *cm_id;
3429 struct iw_cm_event cm_event;
3430 int ret;
3431 struct nes_cm_node *cm_node;
3432
3433 cm_node = event->cm_node;
3434 if (!cm_node)
3435 return;
3436 cm_id = cm_node->cm_id;
3437
3438 atomic_inc(&cm_connect_reqs);
3439 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3440 cm_node, cm_id, jiffies);
3441
3442 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3443 cm_event.status = IW_CM_EVENT_STATUS_OK;
3444 cm_event.provider_data = (void *)cm_node;
3445
3446 cm_event.local_addr.sin_family = AF_INET;
3447 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3448 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3449
3450 cm_event.remote_addr.sin_family = AF_INET;
3451 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3452 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003453 cm_event.private_data = cm_node->mpa_frame_buf;
3454 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003455
3456 ret = cm_id->event_handler(cm_id, &cm_event);
3457 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003458 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3459 __func__, __LINE__, ret);
3460 return;
3461}
3462
3463
3464static void cm_event_mpa_reject(struct nes_cm_event *event)
3465{
3466 struct iw_cm_id *cm_id;
3467 struct iw_cm_event cm_event;
3468 struct nes_cm_node *cm_node;
3469 int ret;
3470
3471 cm_node = event->cm_node;
3472 if (!cm_node)
3473 return;
3474 cm_id = cm_node->cm_id;
3475
3476 atomic_inc(&cm_connect_reqs);
3477 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3478 cm_node, cm_id, jiffies);
3479
3480 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3481 cm_event.status = -ECONNREFUSED;
3482 cm_event.provider_data = cm_id->provider_data;
3483
3484 cm_event.local_addr.sin_family = AF_INET;
3485 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3486 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3487
3488 cm_event.remote_addr.sin_family = AF_INET;
3489 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3490 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3491
3492 cm_event.private_data = cm_node->mpa_frame_buf;
3493 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3494
3495 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3496 "remove_addr=%08x\n",
3497 cm_event.local_addr.sin_addr.s_addr,
3498 cm_event.remote_addr.sin_addr.s_addr);
3499
3500 ret = cm_id->event_handler(cm_id, &cm_event);
3501 if (ret)
3502 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003503 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003504
3505 return;
3506}
3507
3508
3509static void nes_cm_event_handler(struct work_struct *);
3510
3511/**
3512 * nes_cm_post_event
3513 * post an event to the cm event handler
3514 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003515static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003516{
3517 atomic_inc(&event->cm_node->cm_core->events_posted);
3518 add_ref_cm_node(event->cm_node);
3519 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3520 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003521 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3522 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003523
3524 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3525
3526 nes_debug(NES_DBG_CM, "Exit\n");
3527 return 0;
3528}
3529
3530
3531/**
3532 * nes_cm_event_handler
3533 * worker function to handle cm events
3534 * will free instance of nes_cm_event
3535 */
3536static void nes_cm_event_handler(struct work_struct *work)
3537{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003538 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3539 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003540 struct nes_cm_core *cm_core;
3541
Faisal Latif6492cdf2008-07-24 20:50:45 -07003542 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003543 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003544
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003545 cm_core = event->cm_node->cm_core;
3546 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003547 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003548
3549 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003550 case NES_CM_EVENT_MPA_REQ:
3551 cm_event_mpa_req(event);
3552 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3553 event->cm_node);
3554 break;
3555 case NES_CM_EVENT_RESET:
3556 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3557 event->cm_node);
3558 cm_event_reset(event);
3559 break;
3560 case NES_CM_EVENT_CONNECTED:
3561 if ((!event->cm_node->cm_id) ||
3562 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003563 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003564 cm_event_connected(event);
3565 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3566 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003567 case NES_CM_EVENT_MPA_REJECT:
3568 if ((!event->cm_node->cm_id) ||
3569 (event->cm_node->state == NES_CM_STATE_TSA))
3570 break;
3571 cm_event_mpa_reject(event);
3572 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3573 break;
3574
Faisal Latif6492cdf2008-07-24 20:50:45 -07003575 case NES_CM_EVENT_ABORTED:
3576 if ((!event->cm_node->cm_id) ||
3577 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003578 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003579 cm_event_connect_error(event);
3580 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3581 break;
3582 case NES_CM_EVENT_DROPPED_PKT:
3583 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3584 break;
3585 default:
3586 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3587 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003588 }
3589
3590 atomic_dec(&cm_core->events_posted);
3591 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3592 rem_ref_cm_node(cm_core, event->cm_node);
3593 kfree(event);
3594
3595 return;
3596}