blob: 1f7a659e6e9a3095232e1e2eedc370a4b28e9f95 [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>
Faisal Latifc11470f2009-04-27 13:38:31 -070059#include <net/tcp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080060
61#include "nes.h"
62
63u32 cm_packets_sent;
64u32 cm_packets_bounced;
65u32 cm_packets_dropped;
66u32 cm_packets_retrans;
67u32 cm_packets_created;
68u32 cm_packets_received;
69u32 cm_listens_created;
70u32 cm_listens_destroyed;
71u32 cm_backlog_drops;
72atomic_t cm_loopbacks;
73atomic_t cm_nodes_created;
74atomic_t cm_nodes_destroyed;
75atomic_t cm_accel_dropped_pkts;
76atomic_t cm_resets_recvd;
77
Faisal Latif6492cdf2008-07-24 20:50:45 -070078static inline int mini_cm_accelerated(struct nes_cm_core *,
79 struct nes_cm_node *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080080static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070081 struct nes_vnic *, struct nes_cm_info *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080082static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070083static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070084 struct nes_vnic *, u16, void *, struct nes_cm_info *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070085static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -070086static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
87 struct nes_cm_node *);
88static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
89 struct nes_cm_node *);
Faisal Latif4a14f6a2008-11-21 20:50:49 -060090static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070091 struct sk_buff *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070092static int mini_cm_dealloc_core(struct nes_cm_core *);
93static int mini_cm_get(struct nes_cm_core *);
94static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070095
Chien Tung6098d102008-11-21 20:51:01 -060096static void form_cm_frame(struct sk_buff *, struct nes_cm_node *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070097 void *, u32, void *, u32, u8);
Faisal Latif6492cdf2008-07-24 20:50:45 -070098static int add_ref_cm_node(struct nes_cm_node *);
99static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
100
Roland Dreier1a855fb2008-04-16 21:01:09 -0700101static int nes_cm_disconn_true(struct nes_qp *);
102static int nes_cm_post_event(struct nes_cm_event *event);
103static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
104static void nes_disconnect_worker(struct work_struct *work);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700105
106static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800107static int send_mpa_reject(struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700108static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
109static int send_reset(struct nes_cm_node *, struct sk_buff *);
110static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fb2008-04-16 21:01:09 -0700111static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700112static void process_packet(struct nes_cm_node *, struct sk_buff *,
113 struct nes_cm_core *);
114
115static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
116static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
117static void cleanup_retrans_entry(struct nes_cm_node *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800118static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700119static void free_retrans_entry(struct nes_cm_node *cm_node);
120static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
121 struct sk_buff *skb, int optionsize, int passive);
122
123/* CM event handler functions */
124static void cm_event_connected(struct nes_cm_event *);
125static void cm_event_connect_error(struct nes_cm_event *);
126static void cm_event_reset(struct nes_cm_event *);
127static void cm_event_mpa_req(struct nes_cm_event *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800128static void cm_event_mpa_reject(struct nes_cm_event *);
129static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700130
131static void print_core(struct nes_cm_core *core);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800132
133/* External CM API Interface */
134/* instance of function pointers for client API */
135/* set address of this instance to cm_core->cm_ops at cm_core alloc */
136static struct nes_cm_ops nes_cm_api = {
137 mini_cm_accelerated,
138 mini_cm_listen,
139 mini_cm_del_listen,
140 mini_cm_connect,
141 mini_cm_close,
142 mini_cm_accept,
143 mini_cm_reject,
144 mini_cm_recv_pkt,
145 mini_cm_dealloc_core,
146 mini_cm_get,
147 mini_cm_set
148};
149
Roland Dreier1a855fb2008-04-16 21:01:09 -0700150static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800151
152atomic_t cm_connects;
153atomic_t cm_accepts;
154atomic_t cm_disconnects;
155atomic_t cm_closes;
156atomic_t cm_connecteds;
157atomic_t cm_connect_reqs;
158atomic_t cm_rejects;
159
160
161/**
162 * create_event
163 */
164static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
165 enum nes_cm_event_type type)
166{
167 struct nes_cm_event *event;
168
169 if (!cm_node->cm_id)
170 return NULL;
171
172 /* allocate an empty event */
173 event = kzalloc(sizeof(*event), GFP_ATOMIC);
174
175 if (!event)
176 return NULL;
177
178 event->type = type;
179 event->cm_node = cm_node;
180 event->cm_info.rem_addr = cm_node->rem_addr;
181 event->cm_info.loc_addr = cm_node->loc_addr;
182 event->cm_info.rem_port = cm_node->rem_port;
183 event->cm_info.loc_port = cm_node->loc_port;
184 event->cm_info.cm_id = cm_node->cm_id;
185
Faisal Latif6492cdf2008-07-24 20:50:45 -0700186 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
187 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
188 cm_node, event, type, event->cm_info.loc_addr,
189 event->cm_info.loc_port, event->cm_info.rem_addr,
190 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800191
192 nes_cm_post_event(event);
193 return event;
194}
195
196
197/**
198 * send_mpa_request
199 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700200static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800201{
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800202 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700203 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800204 return -1;
205 }
206
207 /* send an MPA Request frame */
208 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
209 cm_node->mpa_frame_size, SET_ACK);
210
Faisal Latif9d5ab132009-03-06 15:15:01 -0800211 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
212}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800213
Faisal Latif9d5ab132009-03-06 15:15:01 -0800214
215
216static int send_mpa_reject(struct nes_cm_node *cm_node)
217{
218 struct sk_buff *skb = NULL;
219
220 skb = dev_alloc_skb(MAX_CM_BUFFER);
221 if (!skb) {
222 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
223 return -ENOMEM;
224 }
225
226 /* send an MPA reject frame */
227 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
228 cm_node->mpa_frame_size, SET_ACK | SET_FIN);
229
230 cm_node->state = NES_CM_STATE_FIN_WAIT1;
231 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800232}
233
234
235/**
236 * recv_mpa - process a received TCP pkt, we are expecting an
237 * IETF MPA frame
238 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800239static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
240 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800241{
242 struct ietf_mpa_frame *mpa_frame;
243
Faisal Latif9d5ab132009-03-06 15:15:01 -0800244 *type = NES_MPA_REQUEST_ACCEPT;
245
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800246 /* assume req frame is in tcp data payload */
247 if (len < sizeof(struct ietf_mpa_frame)) {
248 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800249 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800250 }
251
252 mpa_frame = (struct ietf_mpa_frame *)buffer;
253 cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
254
255 if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
256 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
257 " complete (%x + %x != %x)\n",
Faisal Latif9d5ab132009-03-06 15:15:01 -0800258 cm_node->mpa_frame_size,
259 (u32)sizeof(struct ietf_mpa_frame), len);
260 return -EINVAL;
261 }
262 /* make sure it does not exceed the max size */
263 if (len > MAX_CM_BUFFER) {
264 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
265 " (%x + %x != %x)\n",
266 cm_node->mpa_frame_size,
267 (u32)sizeof(struct ietf_mpa_frame), len);
268 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800269 }
270
271 /* copy entire MPA frame to our cm_node's frame */
272 memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
273 cm_node->mpa_frame_size);
274
Faisal Latif9d5ab132009-03-06 15:15:01 -0800275 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
276 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800277 return 0;
278}
279
280
281/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800282 * form_cm_frame - get a free packet and build empty frame Use
283 * node info to build.
284 */
Chien Tung6098d102008-11-21 20:51:01 -0600285static void form_cm_frame(struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700286 struct nes_cm_node *cm_node, void *options, u32 optionsize,
287 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800288{
289 struct tcphdr *tcph;
290 struct iphdr *iph;
291 struct ethhdr *ethh;
292 u8 *buf;
293 u16 packetsize = sizeof(*iph);
294
295 packetsize += sizeof(*tcph);
296 packetsize += optionsize + datasize;
297
298 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
299
300 skb->len = 0;
301 buf = skb_put(skb, packetsize + ETH_HLEN);
302
303 ethh = (struct ethhdr *) buf;
304 buf += ETH_HLEN;
305
306 iph = (struct iphdr *)buf;
307 buf += sizeof(*iph);
308 tcph = (struct tcphdr *)buf;
309 skb_reset_mac_header(skb);
310 skb_set_network_header(skb, ETH_HLEN);
311 skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
312 buf += sizeof(*tcph);
313
314 skb->ip_summed = CHECKSUM_PARTIAL;
315 skb->protocol = htons(0x800);
316 skb->data_len = 0;
317 skb->mac_len = ETH_HLEN;
318
319 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
320 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
321 ethh->h_proto = htons(0x0800);
322
323 iph->version = IPVERSION;
324 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
325 iph->tos = 0;
326 iph->tot_len = htons(packetsize);
327 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
328
329 iph->frag_off = htons(0x4000);
330 iph->ttl = 0x40;
331 iph->protocol = 0x06; /* IPPROTO_TCP */
332
333 iph->saddr = htonl(cm_node->loc_addr);
334 iph->daddr = htonl(cm_node->rem_addr);
335
336 tcph->source = htons(cm_node->loc_port);
337 tcph->dest = htons(cm_node->rem_port);
338 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
339
340 if (flags & SET_ACK) {
341 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
342 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
343 tcph->ack = 1;
344 } else
345 tcph->ack_seq = 0;
346
347 if (flags & SET_SYN) {
348 cm_node->tcp_cntxt.loc_seq_num++;
349 tcph->syn = 1;
350 } else
Faisal Latif6492cdf2008-07-24 20:50:45 -0700351 cm_node->tcp_cntxt.loc_seq_num += datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800352
Faisal Latif6492cdf2008-07-24 20:50:45 -0700353 if (flags & SET_FIN) {
354 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800355 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700356 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800357
358 if (flags & SET_RST)
359 tcph->rst = 1;
360
361 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
362 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
363 tcph->urg_ptr = 0;
364 if (optionsize)
365 memcpy(buf, options, optionsize);
366 buf += optionsize;
367 if (datasize)
368 memcpy(buf, data, datasize);
369
370 skb_shinfo(skb)->nr_frags = 0;
371 cm_packets_created++;
372
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800373}
374
375
376/**
377 * print_core - dump a cm core
378 */
379static void print_core(struct nes_cm_core *core)
380{
381 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
382 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
383 if (!core)
384 return;
385 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800386
387 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
388
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800389 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
390 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
391
392 nes_debug(NES_DBG_CM, "core : %p \n", core);
393
394 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
395}
396
397
398/**
399 * schedule_nes_timer
400 * note - cm_node needs to be protected before calling this. Encase in:
401 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
402 */
403int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
404 enum nes_timer_type type, int send_retrans,
405 int close_when_complete)
406{
407 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700408 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800409 struct nes_timer_entry *new_send;
410 int ret = 0;
411 u32 was_timer_set;
412
413 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
414 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800415 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800416
417 /* new_send->timetosend = currenttime */
418 new_send->retrycount = NES_DEFAULT_RETRYS;
419 new_send->retranscount = NES_DEFAULT_RETRANS;
420 new_send->skb = skb;
421 new_send->timetosend = jiffies;
422 new_send->type = type;
423 new_send->netdev = cm_node->netdev;
424 new_send->send_retrans = send_retrans;
425 new_send->close_when_complete = close_when_complete;
426
427 if (type == NES_TIMER_TYPE_CLOSE) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700428 new_send->timetosend += (HZ/10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800429 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700430 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800431 WARN_ON(1);
432 return -EINVAL;
433 }
434 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800435 }
436
437 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700438 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800439 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700440 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
441 cm_node->send_entry = new_send;
442 add_ref_cm_node(cm_node);
443 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
444 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800445
446 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
447 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700448 nes_debug(NES_DBG_CM, "Error sending packet %p "
449 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800450 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700451 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800452 } else {
453 cm_packets_sent++;
454 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700455 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800456 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700457 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800458 return ret;
459 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800460 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800461 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800462
463 was_timer_set = timer_pending(&cm_core->tcp_timer);
464
465 if (!was_timer_set) {
466 cm_core->tcp_timer.expires = new_send->timetosend;
467 add_timer(&cm_core->tcp_timer);
468 }
469
470 return ret;
471}
472
Faisal Latif9d5ab132009-03-06 15:15:01 -0800473static void nes_retrans_expired(struct nes_cm_node *cm_node)
474{
475 switch (cm_node->state) {
476 case NES_CM_STATE_SYN_RCVD:
477 case NES_CM_STATE_CLOSING:
478 rem_ref_cm_node(cm_node->cm_core, cm_node);
479 break;
480 case NES_CM_STATE_LAST_ACK:
481 case NES_CM_STATE_FIN_WAIT1:
482 case NES_CM_STATE_MPAREJ_RCVD:
483 send_reset(cm_node, NULL);
484 break;
485 default:
486 create_event(cm_node, NES_CM_EVENT_ABORTED);
487 }
488}
489
490static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
491{
492 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
493 struct iw_cm_id *cm_id = cm_node->cm_id;
494 struct nes_qp *nesqp;
495 unsigned long qplockflags;
496
497 if (!recv_entry)
498 return;
499 nesqp = (struct nes_qp *)recv_entry->skb;
500 if (nesqp) {
501 spin_lock_irqsave(&nesqp->lock, qplockflags);
502 if (nesqp->cm_id) {
503 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
504 "refcount = %d: HIT A "
505 "NES_TIMER_TYPE_CLOSE with something "
506 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
507 atomic_read(&nesqp->refcount));
508 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
509 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
510 nesqp->ibqp_state = IB_QPS_ERR;
511 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
512 nes_cm_disconn(nesqp);
513 } else {
514 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
515 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
516 "refcount = %d: HIT A "
517 "NES_TIMER_TYPE_CLOSE with nothing "
518 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
519 atomic_read(&nesqp->refcount));
520 }
521 } else if (rem_node) {
522 /* TIME_WAIT state */
523 rem_ref_cm_node(cm_node->cm_core, cm_node);
524 }
525 if (cm_node->cm_id)
526 cm_id->rem_ref(cm_id);
527 kfree(recv_entry);
528 cm_node->recv_entry = NULL;
529}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800530
531/**
532 * nes_cm_timer_tick
533 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700534static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800535{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800536 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800537 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800538 struct nes_cm_node *cm_node;
539 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800540 struct list_head *list_core_temp;
541 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800542 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800543 u32 settimer = 0;
544 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800545
Faisal Latif879e5bd2008-11-21 20:50:41 -0600546 struct list_head timer_list;
547 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800548 spin_lock_irqsave(&cm_core->ht_lock, flags);
549
Faisal Latif6492cdf2008-07-24 20:50:45 -0700550 list_for_each_safe(list_node, list_core_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600551 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800552 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800553 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600554 add_ref_cm_node(cm_node);
555 list_add(&cm_node->timer_entry, &timer_list);
556 }
557 }
558 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
559
560 list_for_each_safe(list_node, list_core_temp, &timer_list) {
561 cm_node = container_of(list_node, struct nes_cm_node,
562 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800563 recv_entry = cm_node->recv_entry;
564
565 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700566 if (time_after(recv_entry->timetosend, jiffies)) {
567 if (nexttimeout > recv_entry->timetosend ||
Faisal Latif9d5ab132009-03-06 15:15:01 -0800568 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800569 nexttimeout = recv_entry->timetosend;
570 settimer = 1;
571 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800572 } else
573 handle_recv_entry(cm_node, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800574 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800575
576 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700577 do {
578 send_entry = cm_node->send_entry;
579 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600580 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800581 if (time_after(send_entry->timetosend, jiffies)) {
582 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700583 if ((nexttimeout >
584 send_entry->timetosend) ||
585 !settimer) {
586 nexttimeout =
587 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800588 settimer = 1;
589 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800590 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700591 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800592 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800593 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800594 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700595
596 if ((cm_node->state == NES_CM_STATE_TSA) ||
597 (cm_node->state == NES_CM_STATE_CLOSED)) {
598 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600599 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800600 }
601
Faisal Latif6492cdf2008-07-24 20:50:45 -0700602 if (!send_entry->retranscount ||
603 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800604 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700605 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800606
Faisal Latif6492cdf2008-07-24 20:50:45 -0700607 spin_unlock_irqrestore(
608 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800609 nes_retrans_expired(cm_node);
610 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700611 spin_lock_irqsave(&cm_node->retrans_list_lock,
612 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600613 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800614 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800615 atomic_inc(&send_entry->skb->users);
616 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700617 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
618 "for node %p, jiffies = %lu, time to send = "
619 "%lu, retranscount = %u, send_entry->seq_num = "
620 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
621 "0x%08X\n", send_entry, cm_node, jiffies,
622 send_entry->timetosend,
623 send_entry->retranscount,
624 send_entry->seq_num,
625 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800626
Faisal Latif6492cdf2008-07-24 20:50:45 -0700627 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
628 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800629 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700630 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800631 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700632 nes_debug(NES_DBG_CM, "rexmit failed for "
633 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800634 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800635 send_entry->retrycount--;
636 nexttimeout = jiffies + NES_SHORT_TIME;
637 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600638 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800639 } else {
640 cm_packets_sent++;
641 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700642 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
643 "%u, retry count = %u.\n",
644 send_entry->retranscount,
645 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800646 if (send_entry->send_retrans) {
647 send_entry->retranscount--;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700648 send_entry->timetosend = jiffies +
649 NES_RETRY_TIMEOUT;
650 if (nexttimeout > send_entry->timetosend ||
651 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800652 nexttimeout = send_entry->timetosend;
653 settimer = 1;
654 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800655 } else {
656 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700657 close_when_complete =
658 send_entry->close_when_complete;
659 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
660 cm_node, cm_node->state);
661 free_retrans_entry(cm_node);
662 if (close_when_complete)
663 rem_ref_cm_node(cm_node->cm_core,
664 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800665 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700666 } while (0);
667
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800668 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700669 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800670 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800671
672 if (settimer) {
673 if (!timer_pending(&cm_core->tcp_timer)) {
674 cm_core->tcp_timer.expires = nexttimeout;
675 add_timer(&cm_core->tcp_timer);
676 }
677 }
678}
679
680
681/**
682 * send_syn
683 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700684static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
685 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800686{
687 int ret;
688 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800689 char optionsbuffer[sizeof(struct option_mss) +
Faisal Latif6492cdf2008-07-24 20:50:45 -0700690 sizeof(struct option_windowscale) + sizeof(struct option_base) +
691 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800692
693 int optionssize = 0;
694 /* Sending MSS option */
695 union all_known_options *options;
696
697 if (!cm_node)
698 return -EINVAL;
699
700 options = (union all_known_options *)&optionsbuffer[optionssize];
701 options->as_mss.optionnum = OPTION_NUMBER_MSS;
702 options->as_mss.length = sizeof(struct option_mss);
703 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
704 optionssize += sizeof(struct option_mss);
705
706 options = (union all_known_options *)&optionsbuffer[optionssize];
707 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
708 options->as_windowscale.length = sizeof(struct option_windowscale);
709 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
710 optionssize += sizeof(struct option_windowscale);
711
Faisal Latif6492cdf2008-07-24 20:50:45 -0700712 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800713 options = (union all_known_options *)&optionsbuffer[optionssize];
714 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
715 options->as_base.length = sizeof(struct option_base);
716 optionssize += sizeof(struct option_base);
717 /* we need the size to be a multiple of 4 */
718 options = (union all_known_options *)&optionsbuffer[optionssize];
719 options->as_end = 1;
720 optionssize += 1;
721 options = (union all_known_options *)&optionsbuffer[optionssize];
722 options->as_end = 1;
723 optionssize += 1;
724 }
725
726 options = (union all_known_options *)&optionsbuffer[optionssize];
727 options->as_end = OPTION_NUMBER_END;
728 optionssize += 1;
729
Faisal Latif6492cdf2008-07-24 20:50:45 -0700730 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800731 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800732 if (!skb) {
733 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
734 return -1;
735 }
736
737 if (sendack)
738 flags |= SET_ACK;
739
740 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
741 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
742
743 return ret;
744}
745
746
747/**
748 * send_reset
749 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700750static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800751{
752 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800753 int flags = SET_RST | SET_ACK;
754
Faisal Latif6492cdf2008-07-24 20:50:45 -0700755 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800756 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800757 if (!skb) {
758 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -0800759 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800760 }
761
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800762 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
763 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
764
765 return ret;
766}
767
768
769/**
770 * send_ack
771 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700772static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800773{
774 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700775
776 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800777 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800778
779 if (!skb) {
780 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
781 return -1;
782 }
783
784 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
785 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
786
787 return ret;
788}
789
790
791/**
792 * send_fin
793 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700794static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800795{
796 int ret;
797
798 /* if we didn't get a frame get one */
799 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800800 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800801
802 if (!skb) {
803 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
804 return -1;
805 }
806
807 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
808 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
809
810 return ret;
811}
812
813
814/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800815 * find_node - find a cm node that matches the reference cm node
816 */
817static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
818 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
819{
820 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800821 struct list_head *hte;
822 struct nes_cm_node *cm_node;
823
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800824 /* get a handle on the hte */
825 hte = &cm_core->connected_nodes;
826
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800827 /* walk list and find cm_node associated with this session ID */
828 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700829 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800830 /* compare quad, return node handle if a match */
831 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
832 cm_node->loc_addr, cm_node->loc_port,
833 loc_addr, loc_port,
834 cm_node->rem_addr, cm_node->rem_port,
835 rem_addr, rem_port);
836 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
837 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
838 add_ref_cm_node(cm_node);
839 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
840 return cm_node;
841 }
842 }
843 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
844
845 /* no owner node */
846 return NULL;
847}
848
849
850/**
851 * find_listener - find a cm node listening on this addr-port pair
852 */
853static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
854 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
855{
856 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800857 struct nes_cm_listener *listen_node;
858
859 /* walk list and find cm_node associated with this session ID */
860 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700861 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800862 /* compare node pair, return node handle if a match */
863 if (((listen_node->loc_addr == dst_addr) ||
864 listen_node->loc_addr == 0x00000000) &&
865 (listen_node->loc_port == dst_port) &&
866 (listener_state & listen_node->listener_state)) {
867 atomic_inc(&listen_node->ref_count);
868 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
869 return listen_node;
870 }
871 }
872 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
873
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800874 /* no listener */
875 return NULL;
876}
877
878
879/**
880 * add_hte_node - add a cm node to the hash table
881 */
882static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
883{
884 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800885 struct list_head *hte;
886
887 if (!cm_node || !cm_core)
888 return -EINVAL;
889
Faisal Latif6492cdf2008-07-24 20:50:45 -0700890 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
891 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800892
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800893 spin_lock_irqsave(&cm_core->ht_lock, flags);
894
895 /* get a handle on the hash table element (list head for this slot) */
896 hte = &cm_core->connected_nodes;
897 list_add_tail(&cm_node->list, hte);
898 atomic_inc(&cm_core->ht_node_cnt);
899
900 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
901
902 return 0;
903}
904
905
906/**
907 * mini_cm_dec_refcnt_listen
908 */
909static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700910 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800911{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800912 int ret = -EINVAL;
913 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800914 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700915 struct list_head *list_pos = NULL;
916 struct list_head *list_temp = NULL;
917 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -0600918 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700919
920 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
921 "refcnt=%d\n", listener, free_hanging_nodes,
922 atomic_read(&listener->ref_count));
923 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -0600924 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700925 if (free_hanging_nodes) {
926 spin_lock_irqsave(&cm_core->ht_lock, flags);
927 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600928 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700929 cm_node = container_of(list_pos, struct nes_cm_node,
930 list);
931 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -0600932 (!cm_node->accelerated)) {
933 add_ref_cm_node(cm_node);
934 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700935 }
936 }
937 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
938 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600939
940 list_for_each_safe(list_pos, list_temp, &reset_list) {
941 cm_node = container_of(list_pos, struct nes_cm_node,
Faisal Latif9d5ab132009-03-06 15:15:01 -0800942 reset_entry);
943 {
944 struct nes_cm_node *loopback = cm_node->loopbackpartner;
945 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
946 rem_ref_cm_node(cm_node->cm_core, cm_node);
947 } else {
948 if (!loopback) {
949 cleanup_retrans_entry(cm_node);
950 err = send_reset(cm_node, NULL);
951 if (err) {
952 cm_node->state =
953 NES_CM_STATE_CLOSED;
954 WARN_ON(1);
955 } else {
956 cm_node->state =
957 NES_CM_STATE_CLOSED;
958 rem_ref_cm_node(
959 cm_node->cm_core,
960 cm_node);
961 }
962 } else {
963 struct nes_cm_event event;
964
965 event.cm_node = loopback;
966 event.cm_info.rem_addr =
967 loopback->rem_addr;
968 event.cm_info.loc_addr =
969 loopback->loc_addr;
970 event.cm_info.rem_port =
971 loopback->rem_port;
972 event.cm_info.loc_port =
973 loopback->loc_port;
974 event.cm_info.cm_id = loopback->cm_id;
975 cm_event_connect_error(&event);
976 loopback->state = NES_CM_STATE_CLOSED;
977
978 event.cm_node = cm_node;
979 event.cm_info.rem_addr =
980 cm_node->rem_addr;
981 event.cm_info.loc_addr =
982 cm_node->loc_addr;
983 event.cm_info.rem_port =
984 cm_node->rem_port;
985 event.cm_info.loc_port =
986 cm_node->loc_port;
987 event.cm_info.cm_id = cm_node->cm_id;
988 cm_event_reset(&event);
989
990 rem_ref_cm_node(cm_node->cm_core,
991 cm_node);
992
993 }
994 }
995 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600996 }
997
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800998 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
999 if (!atomic_dec_return(&listener->ref_count)) {
1000 list_del(&listener->list);
1001
1002 /* decrement our listen node count */
1003 atomic_dec(&cm_core->listen_node_cnt);
1004
1005 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1006
1007 if (listener->nesvnic) {
1008 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1009 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1010 }
1011
1012 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1013
1014 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001015 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001016 ret = 0;
1017 cm_listens_destroyed++;
1018 } else {
1019 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1020 }
1021 if (listener) {
1022 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1023 nes_debug(NES_DBG_CM, "destroying listener (%p)"
1024 " with non-zero pending accepts=%u\n",
1025 listener, atomic_read(&listener->pend_accepts_cnt));
1026 }
1027
1028 return ret;
1029}
1030
1031
1032/**
1033 * mini_cm_del_listen
1034 */
1035static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1036 struct nes_cm_listener *listener)
1037{
1038 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1039 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1040 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1041}
1042
1043
1044/**
1045 * mini_cm_accelerated
1046 */
1047static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1048 struct nes_cm_node *cm_node)
1049{
1050 u32 was_timer_set;
1051 cm_node->accelerated = 1;
1052
1053 if (cm_node->accept_pend) {
1054 BUG_ON(!cm_node->listener);
1055 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001056 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001057 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1058 }
1059
1060 was_timer_set = timer_pending(&cm_core->tcp_timer);
1061 if (!was_timer_set) {
1062 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1063 add_timer(&cm_core->tcp_timer);
1064 }
1065
1066 return 0;
1067}
1068
1069
1070/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001071 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001072 */
Bob Sharp7191a0a2008-10-03 12:21:19 -07001073static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001074{
1075 struct rtable *rt;
1076 struct flowi fl;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001077 struct neighbour *neigh;
1078 int rc = -1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001079
1080 memset(&fl, 0, sizeof fl);
1081 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1082 if (ip_route_output_key(&init_net, &rt, &fl)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001083 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001084 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001085 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001086 }
1087
Bob Sharp7191a0a2008-10-03 12:21:19 -07001088 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1089 if (neigh) {
1090 if (neigh->nud_state & NUD_VALID) {
1091 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001092 " is %pM, Gateway is 0x%08X \n", dst_ip,
1093 neigh->ha, ntohl(rt->rt_gateway));
Bob Sharp7191a0a2008-10-03 12:21:19 -07001094 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1095 dst_ip, NES_ARP_ADD);
1096 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1097 NES_ARP_RESOLVE);
1098 }
1099 neigh_release(neigh);
1100 }
1101
1102 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1103 neigh_event_send(rt->u.dst.neighbour, NULL);
1104
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001105 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001106 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001107}
1108
1109
1110/**
1111 * make_cm_node - create a new instance of a cm node
1112 */
1113static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1114 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1115 struct nes_cm_listener *listener)
1116{
1117 struct nes_cm_node *cm_node;
1118 struct timespec ts;
1119 int arpindex = 0;
1120 struct nes_device *nesdev;
1121 struct nes_adapter *nesadapter;
1122
1123 /* create an hte and cm_node for this instance */
1124 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1125 if (!cm_node)
1126 return NULL;
1127
1128 /* set our node specific transport info */
1129 cm_node->loc_addr = cm_info->loc_addr;
1130 cm_node->rem_addr = cm_info->rem_addr;
1131 cm_node->loc_port = cm_info->loc_port;
1132 cm_node->rem_port = cm_info->rem_port;
1133 cm_node->send_write0 = send_first;
Harvey Harrison63779432008-10-31 00:56:00 -07001134 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1135 &cm_node->loc_addr, cm_node->loc_port,
1136 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001137 cm_node->listener = listener;
1138 cm_node->netdev = nesvnic->netdev;
1139 cm_node->cm_id = cm_info->cm_id;
1140 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1141
Faisal Latif6492cdf2008-07-24 20:50:45 -07001142 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1143 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001144
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001145 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001146
1147 cm_node->loopbackpartner = NULL;
1148 atomic_set(&cm_node->ref_count, 1);
1149 /* associate our parent CM core */
1150 cm_node->cm_core = cm_core;
1151 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1152 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1153 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1154 NES_CM_DEFAULT_RCV_WND_SCALE;
1155 ts = current_kernel_time();
1156 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1157 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Chien Tungf2b2b592008-03-20 19:55:30 -05001158 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001159 cm_node->tcp_cntxt.rcv_nxt = 0;
1160 /* get a unique session ID , add thread_id to an upcounter to handle race */
1161 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001162 cm_node->conn_type = cm_info->conn_type;
1163 cm_node->apbvt_set = 0;
1164 cm_node->accept_pend = 0;
1165
1166 cm_node->nesvnic = nesvnic;
1167 /* get some device handles, for arp lookup */
1168 nesdev = nesvnic->nesdev;
1169 nesadapter = nesdev->nesadapter;
1170
1171 cm_node->loopbackpartner = NULL;
1172 /* get the mac addr for the remote node */
Chien Tung1ee86552008-11-21 20:51:04 -06001173 if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1174 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
1175 else
1176 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001177 if (arpindex < 0) {
Bob Sharp7191a0a2008-10-03 12:21:19 -07001178 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr);
1179 if (arpindex < 0) {
1180 kfree(cm_node);
1181 return NULL;
1182 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001183 }
1184
1185 /* copy the mac addr to node context */
1186 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001187 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1188 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001189
1190 add_hte_node(cm_core, cm_node);
1191 atomic_inc(&cm_nodes_created);
1192
1193 return cm_node;
1194}
1195
1196
1197/**
1198 * add_ref_cm_node - destroy an instance of a cm node
1199 */
1200static int add_ref_cm_node(struct nes_cm_node *cm_node)
1201{
1202 atomic_inc(&cm_node->ref_count);
1203 return 0;
1204}
1205
1206
1207/**
1208 * rem_ref_cm_node - destroy an instance of a cm node
1209 */
1210static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001211 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001212{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001213 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001214 struct nes_qp *nesqp;
1215
1216 if (!cm_node)
1217 return -EINVAL;
1218
1219 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1220 if (atomic_dec_return(&cm_node->ref_count)) {
1221 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1222 return 0;
1223 }
1224 list_del(&cm_node->list);
1225 atomic_dec(&cm_core->ht_node_cnt);
1226 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1227
1228 /* if the node is destroyed before connection was accelerated */
1229 if (!cm_node->accelerated && cm_node->accept_pend) {
1230 BUG_ON(!cm_node->listener);
1231 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1232 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1233 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001234 WARN_ON(cm_node->send_entry);
1235 if (cm_node->recv_entry)
1236 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001237 if (cm_node->listener) {
1238 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1239 } else {
1240 if (cm_node->apbvt_set && cm_node->nesvnic) {
1241 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001242 PCI_FUNC(
1243 cm_node->nesvnic->nesdev->pcidev->devfn),
1244 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001245 }
1246 }
1247
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001248 atomic_dec(&cm_core->node_cnt);
1249 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001250 nesqp = cm_node->nesqp;
1251 if (nesqp) {
1252 nesqp->cm_node = NULL;
1253 nes_rem_ref(&nesqp->ibqp);
1254 cm_node->nesqp = NULL;
1255 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001256
Faisal Latif6492cdf2008-07-24 20:50:45 -07001257 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001258 return 0;
1259}
1260
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001261/**
1262 * process_options
1263 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001264static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1265 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001266{
1267 u32 tmp;
1268 u32 offset = 0;
1269 union all_known_options *all_options;
1270 char got_mss_option = 0;
1271
1272 while (offset < optionsize) {
1273 all_options = (union all_known_options *)(optionsloc + offset);
1274 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001275 case OPTION_NUMBER_END:
1276 offset = optionsize;
1277 break;
1278 case OPTION_NUMBER_NONE:
1279 offset += 1;
1280 continue;
1281 case OPTION_NUMBER_MSS:
1282 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1283 "Size: %d\n", __func__,
1284 all_options->as_mss.length, offset, optionsize);
1285 got_mss_option = 1;
1286 if (all_options->as_mss.length != 4) {
1287 return 1;
1288 } else {
1289 tmp = ntohs(all_options->as_mss.mss);
1290 if (tmp > 0 && tmp <
1291 cm_node->tcp_cntxt.mss)
1292 cm_node->tcp_cntxt.mss = tmp;
1293 }
1294 break;
1295 case OPTION_NUMBER_WINDOW_SCALE:
1296 cm_node->tcp_cntxt.snd_wscale =
1297 all_options->as_windowscale.shiftcount;
1298 break;
1299 case OPTION_NUMBER_WRITE0:
1300 cm_node->send_write0 = 1;
1301 break;
1302 default:
1303 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1304 all_options->as_base.optionnum);
1305 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001306 }
1307 offset += all_options->as_base.length;
1308 }
1309 if ((!got_mss_option) && (syn_packet))
1310 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1311 return 0;
1312}
1313
Faisal Latif6492cdf2008-07-24 20:50:45 -07001314static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001315{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001316 atomic_inc(&cm_accel_dropped_pkts);
1317 dev_kfree_skb_any(skb);
1318}
1319
Faisal Latif9d5ab132009-03-06 15:15:01 -08001320static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001321{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001322 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1323 "refcnt=%d\n", cm_node, cm_node->state,
1324 atomic_read(&cm_node->ref_count));
1325 cm_node->tcp_cntxt.rcv_nxt++;
1326 cleanup_retrans_entry(cm_node);
1327 switch (cm_node->state) {
1328 case NES_CM_STATE_SYN_RCVD:
1329 case NES_CM_STATE_SYN_SENT:
1330 case NES_CM_STATE_ESTABLISHED:
1331 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001332 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001333 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001334 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001335 break;
1336 case NES_CM_STATE_FIN_WAIT1:
1337 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001338 send_ack(cm_node, NULL);
1339 /* Wait for ACK as this is simultanous close..
1340 * After we receive ACK, do not send anything..
1341 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001342 break;
1343 case NES_CM_STATE_FIN_WAIT2:
1344 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001345 send_ack(cm_node, NULL);
1346 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1347 break;
1348 case NES_CM_STATE_TIME_WAIT:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001349 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001350 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001351 break;
1352 case NES_CM_STATE_TSA:
1353 default:
1354 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1355 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001356 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001357 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001358}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001359
Faisal Latif6492cdf2008-07-24 20:50:45 -07001360
1361static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1362 struct tcphdr *tcph)
1363{
1364
1365 int reset = 0; /* whether to send reset in case of err.. */
Faisal Latif183ecfa2008-11-21 20:50:46 -06001366 int passive_state;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001367 atomic_inc(&cm_resets_recvd);
1368 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1369 " refcnt=%d\n", cm_node, cm_node->state,
1370 atomic_read(&cm_node->ref_count));
1371 cleanup_retrans_entry(cm_node);
1372 switch (cm_node->state) {
1373 case NES_CM_STATE_SYN_SENT:
1374 case NES_CM_STATE_MPAREQ_SENT:
1375 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1376 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1377 cm_node->listener, cm_node->state);
1378 active_open_err(cm_node, skb, reset);
1379 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001380 case NES_CM_STATE_MPAREQ_RCVD:
1381 passive_state = atomic_add_return(1, &cm_node->passive_state);
1382 if (passive_state == NES_SEND_RESET_EVENT)
1383 create_event(cm_node, NES_CM_EVENT_RESET);
1384 cleanup_retrans_entry(cm_node);
1385 cm_node->state = NES_CM_STATE_CLOSED;
1386 dev_kfree_skb_any(skb);
1387 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001388 case NES_CM_STATE_ESTABLISHED:
1389 case NES_CM_STATE_SYN_RCVD:
1390 case NES_CM_STATE_LISTENING:
1391 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1392 passive_open_err(cm_node, skb, reset);
1393 break;
1394 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001395 active_open_err(cm_node, skb, reset);
1396 break;
1397 case NES_CM_STATE_CLOSED:
1398 cleanup_retrans_entry(cm_node);
1399 drop_packet(skb);
1400 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001401 case NES_CM_STATE_TIME_WAIT:
1402 cleanup_retrans_entry(cm_node);
1403 cm_node->state = NES_CM_STATE_CLOSED;
1404 rem_ref_cm_node(cm_node->cm_core, cm_node);
1405 drop_packet(skb);
1406 break;
1407 case NES_CM_STATE_FIN_WAIT1:
1408 cleanup_retrans_entry(cm_node);
1409 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001410 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001411 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001412 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001413 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001414}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001415
Faisal Latif9d5ab132009-03-06 15:15:01 -08001416
1417static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001418{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001419
Faisal Latif9d5ab132009-03-06 15:15:01 -08001420 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001421 int datasize = skb->len;
1422 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001423
1424 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1425 u32 res_type;
1426 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1427 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001428 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001429 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001430 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1431 "cm_node=%p listener=%p state=%d\n", __func__,
1432 __LINE__, cm_node, cm_node->listener,
1433 cm_node->state);
1434 active_open_err(cm_node, skb, 1);
1435 } else {
1436 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001437 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001438 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001439 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001440
1441 switch (cm_node->state) {
1442 case NES_CM_STATE_ESTABLISHED:
1443 if (res_type == NES_MPA_REQUEST_REJECT) {
1444 /*BIG problem as we are receiving the MPA.. So should
1445 * not be REJECT.. This is Passive Open.. We can
1446 * only receive it Reject for Active Open...*/
1447 WARN_ON(1);
1448 }
1449 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1450 type = NES_CM_EVENT_MPA_REQ;
1451 atomic_set(&cm_node->passive_state,
1452 NES_PASSIVE_STATE_INDICATED);
1453 break;
1454 case NES_CM_STATE_MPAREQ_SENT:
1455 if (res_type == NES_MPA_REQUEST_REJECT) {
1456 type = NES_CM_EVENT_MPA_REJECT;
1457 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1458 } else {
1459 type = NES_CM_EVENT_CONNECTED;
1460 cm_node->state = NES_CM_STATE_TSA;
1461 }
1462
1463 break;
1464 default:
1465 WARN_ON(1);
1466 break;
1467 }
1468 dev_kfree_skb_any(skb);
1469 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001470}
1471
1472static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1473{
1474 switch (cm_node->state) {
1475 case NES_CM_STATE_SYN_SENT:
1476 case NES_CM_STATE_MPAREQ_SENT:
1477 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1478 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1479 cm_node->listener, cm_node->state);
1480 active_open_err(cm_node, skb, 1);
1481 break;
1482 case NES_CM_STATE_ESTABLISHED:
1483 case NES_CM_STATE_SYN_RCVD:
1484 passive_open_err(cm_node, skb, 1);
1485 break;
1486 case NES_CM_STATE_TSA:
1487 default:
1488 drop_packet(skb);
1489 }
1490}
1491
1492static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1493 struct sk_buff *skb)
1494{
1495 int err;
1496
1497 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1498 if (err)
1499 active_open_err(cm_node, skb, 1);
1500
1501 return err;
1502}
1503
1504static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1505 struct sk_buff *skb)
1506{
1507 int err = 0;
1508 u32 seq;
1509 u32 ack_seq;
1510 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1511 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1512 u32 rcv_wnd;
1513 seq = ntohl(tcph->seq);
1514 ack_seq = ntohl(tcph->ack_seq);
1515 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1516 if (ack_seq != loc_seq_num)
1517 err = 1;
Faisal Latifc11470f2009-04-27 13:38:31 -07001518 else if (!between(seq, rcv_nxt, (rcv_nxt+rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001519 err = 1;
1520 if (err) {
1521 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1522 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1523 cm_node->listener, cm_node->state);
1524 indicate_pkt_err(cm_node, skb);
1525 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1526 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1527 rcv_wnd);
1528 }
1529 return err;
1530}
1531
1532/*
1533 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1534 * is created with a listener or it may comein as rexmitted packet which in
1535 * that case will be just dropped.
1536 */
1537
1538static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1539 struct tcphdr *tcph)
1540{
1541 int ret;
1542 u32 inc_sequence;
1543 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001544
1545 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001546 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001547 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001548
Faisal Latif6492cdf2008-07-24 20:50:45 -07001549 switch (cm_node->state) {
1550 case NES_CM_STATE_SYN_SENT:
1551 case NES_CM_STATE_MPAREQ_SENT:
1552 /* Rcvd syn on active open connection*/
1553 active_open_err(cm_node, skb, 1);
1554 break;
1555 case NES_CM_STATE_LISTENING:
1556 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001557 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1558 cm_node->listener->backlog) {
1559 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1560 "pressure \n");
1561 cm_backlog_drops++;
1562 passive_open_err(cm_node, skb, 0);
1563 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001564 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001565 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1566 1);
1567 if (ret) {
1568 passive_open_err(cm_node, skb, 0);
1569 /* drop pkt */
1570 break;
1571 }
1572 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1573 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001574 cm_node->accept_pend = 1;
1575 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1576
Faisal Latif6492cdf2008-07-24 20:50:45 -07001577 cm_node->state = NES_CM_STATE_SYN_RCVD;
1578 send_syn(cm_node, 1, skb);
1579 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001580 case NES_CM_STATE_CLOSED:
1581 cleanup_retrans_entry(cm_node);
1582 send_reset(cm_node, skb);
1583 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001584 case NES_CM_STATE_TSA:
1585 case NES_CM_STATE_ESTABLISHED:
1586 case NES_CM_STATE_FIN_WAIT1:
1587 case NES_CM_STATE_FIN_WAIT2:
1588 case NES_CM_STATE_MPAREQ_RCVD:
1589 case NES_CM_STATE_LAST_ACK:
1590 case NES_CM_STATE_CLOSING:
1591 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001592 default:
1593 drop_packet(skb);
1594 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001595 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001596}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001597
Faisal Latif6492cdf2008-07-24 20:50:45 -07001598static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1599 struct tcphdr *tcph)
1600{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001601
Faisal Latif6492cdf2008-07-24 20:50:45 -07001602 int ret;
1603 u32 inc_sequence;
1604 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001605
Faisal Latif6492cdf2008-07-24 20:50:45 -07001606 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001607 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001608 inc_sequence = ntohl(tcph->seq);
1609 switch (cm_node->state) {
1610 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001611 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001612 /* active open */
1613 if (check_syn(cm_node, tcph, skb))
1614 return;
1615 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1616 /* setup options */
1617 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1618 if (ret) {
1619 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1620 cm_node);
1621 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001622 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001623 cleanup_retrans_entry(cm_node);
1624 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1625 send_mpa_request(cm_node, skb);
1626 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1627 break;
1628 case NES_CM_STATE_MPAREQ_RCVD:
1629 /* passive open, so should not be here */
1630 passive_open_err(cm_node, skb, 1);
1631 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001632 case NES_CM_STATE_LISTENING:
1633 case NES_CM_STATE_CLOSED:
1634 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1635 cleanup_retrans_entry(cm_node);
1636 send_reset(cm_node, skb);
1637 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001638 case NES_CM_STATE_ESTABLISHED:
1639 case NES_CM_STATE_FIN_WAIT1:
1640 case NES_CM_STATE_FIN_WAIT2:
1641 case NES_CM_STATE_LAST_ACK:
1642 case NES_CM_STATE_TSA:
1643 case NES_CM_STATE_CLOSING:
1644 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001645 case NES_CM_STATE_MPAREQ_SENT:
1646 default:
1647 drop_packet(skb);
1648 break;
1649 }
1650}
1651
1652static void handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1653 struct tcphdr *tcph)
1654{
1655 int datasize = 0;
1656 u32 inc_sequence;
1657 u32 rem_seq_ack;
1658 u32 rem_seq;
Faisal Latifabb77252008-11-21 20:50:52 -06001659 int ret;
1660 int optionsize;
Faisal Latifabb77252008-11-21 20:50:52 -06001661 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001662
Faisal Latif6492cdf2008-07-24 20:50:45 -07001663 if (check_seq(cm_node, tcph, skb))
1664 return;
1665
1666 skb_pull(skb, tcph->doff << 2);
1667 inc_sequence = ntohl(tcph->seq);
1668 rem_seq = ntohl(tcph->seq);
1669 rem_seq_ack = ntohl(tcph->ack_seq);
1670 datasize = skb->len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001671 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001672 switch (cm_node->state) {
1673 case NES_CM_STATE_SYN_RCVD:
1674 /* Passive OPEN */
Faisal Latifabb77252008-11-21 20:50:52 -06001675 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1676 if (ret)
1677 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001678 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
Faisal Latifabb77252008-11-21 20:50:52 -06001679 if (cm_node->tcp_cntxt.rem_ack_num !=
1680 cm_node->tcp_cntxt.loc_seq_num) {
1681 nes_debug(NES_DBG_CM, "rem_ack_num != loc_seq_num\n");
1682 cleanup_retrans_entry(cm_node);
1683 send_reset(cm_node, skb);
1684 return;
1685 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001686 cm_node->state = NES_CM_STATE_ESTABLISHED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001687 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001688 if (datasize) {
1689 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001690 handle_rcv_mpa(cm_node, skb);
1691 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001692 dev_kfree_skb_any(skb);
1693 cleanup_retrans_entry(cm_node);
1694 }
1695 break;
1696 case NES_CM_STATE_ESTABLISHED:
1697 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001698 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001699 if (datasize) {
1700 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001701 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001702 } else
1703 drop_packet(skb);
1704 break;
1705 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001706 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001707 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1708 if (datasize) {
1709 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001710 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001711 } else { /* Could be just an ack pkt.. */
1712 cleanup_retrans_entry(cm_node);
1713 dev_kfree_skb_any(skb);
1714 }
1715 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001716 case NES_CM_STATE_LISTENING:
1717 case NES_CM_STATE_CLOSED:
1718 cleanup_retrans_entry(cm_node);
1719 send_reset(cm_node, skb);
1720 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001721 case NES_CM_STATE_LAST_ACK:
1722 cleanup_retrans_entry(cm_node);
1723 cm_node->state = NES_CM_STATE_CLOSED;
1724 cm_node->cm_id->rem_ref(cm_node->cm_id);
1725 case NES_CM_STATE_CLOSING:
1726 cleanup_retrans_entry(cm_node);
1727 rem_ref_cm_node(cm_node->cm_core, cm_node);
1728 drop_packet(skb);
1729 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001730 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001731 cleanup_retrans_entry(cm_node);
1732 drop_packet(skb);
1733 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1734 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001735 case NES_CM_STATE_SYN_SENT:
1736 case NES_CM_STATE_FIN_WAIT2:
1737 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001738 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001739 case NES_CM_STATE_UNKNOWN:
1740 default:
1741 drop_packet(skb);
1742 break;
1743 }
1744}
1745
1746
1747
1748static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1749 struct sk_buff *skb, int optionsize, int passive)
1750{
1751 u8 *optionsloc = (u8 *)&tcph[1];
1752 if (optionsize) {
1753 if (process_options(cm_node, optionsloc, optionsize,
1754 (u32)tcph->syn)) {
1755 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1756 __func__, cm_node);
1757 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06001758 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001759 else
Faisal Latifabb77252008-11-21 20:50:52 -06001760 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001761 return 1;
1762 }
1763 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001764
1765 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1766 cm_node->tcp_cntxt.snd_wscale;
1767
Faisal Latif6492cdf2008-07-24 20:50:45 -07001768 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001769 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001770 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001771}
1772
Faisal Latif6492cdf2008-07-24 20:50:45 -07001773/*
1774 * active_open_err() will send reset() if flag set..
1775 * It will also send ABORT event.
1776 */
1777
1778static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1779 int reset)
1780{
1781 cleanup_retrans_entry(cm_node);
1782 if (reset) {
1783 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1784 "state=%d\n", cm_node, cm_node->state);
1785 add_ref_cm_node(cm_node);
1786 send_reset(cm_node, skb);
1787 } else
1788 dev_kfree_skb_any(skb);
1789
1790 cm_node->state = NES_CM_STATE_CLOSED;
1791 create_event(cm_node, NES_CM_EVENT_ABORTED);
1792}
1793
1794/*
1795 * passive_open_err() will either do a reset() or will free up the skb and
1796 * remove the cm_node.
1797 */
1798
1799static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1800 int reset)
1801{
1802 cleanup_retrans_entry(cm_node);
1803 cm_node->state = NES_CM_STATE_CLOSED;
1804 if (reset) {
1805 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1806 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1807 send_reset(cm_node, skb);
1808 } else {
1809 dev_kfree_skb_any(skb);
1810 rem_ref_cm_node(cm_node->cm_core, cm_node);
1811 }
1812}
1813
1814/*
1815 * free_retrans_entry() routines assumes that the retrans_list_lock has
1816 * been acquired before calling.
1817 */
1818static void free_retrans_entry(struct nes_cm_node *cm_node)
1819{
1820 struct nes_timer_entry *send_entry;
1821 send_entry = cm_node->send_entry;
1822 if (send_entry) {
1823 cm_node->send_entry = NULL;
1824 dev_kfree_skb_any(send_entry->skb);
1825 kfree(send_entry);
1826 rem_ref_cm_node(cm_node->cm_core, cm_node);
1827 }
1828}
1829
1830static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1831{
1832 unsigned long flags;
1833
1834 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1835 free_retrans_entry(cm_node);
1836 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1837}
1838
1839/**
1840 * process_packet
1841 * Returns skb if to be freed, else it will return NULL if already used..
1842 */
1843static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1844 struct nes_cm_core *cm_core)
1845{
1846 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1847 struct tcphdr *tcph = tcp_hdr(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001848 u32 fin_set = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001849 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1850
1851 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1852 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1853 tcph->ack, tcph->rst, tcph->fin);
1854
1855 if (tcph->rst)
1856 pkt_type = NES_PKT_TYPE_RST;
1857 else if (tcph->syn) {
1858 pkt_type = NES_PKT_TYPE_SYN;
1859 if (tcph->ack)
1860 pkt_type = NES_PKT_TYPE_SYNACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001861 } else if (tcph->ack)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001862 pkt_type = NES_PKT_TYPE_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001863 if (tcph->fin)
1864 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001865
1866 switch (pkt_type) {
1867 case NES_PKT_TYPE_SYN:
1868 handle_syn_pkt(cm_node, skb, tcph);
1869 break;
1870 case NES_PKT_TYPE_SYNACK:
1871 handle_synack_pkt(cm_node, skb, tcph);
1872 break;
1873 case NES_PKT_TYPE_ACK:
1874 handle_ack_pkt(cm_node, skb, tcph);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001875 if (fin_set)
1876 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001877 break;
1878 case NES_PKT_TYPE_RST:
1879 handle_rst_pkt(cm_node, skb, tcph);
1880 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001881 default:
1882 drop_packet(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001883 if (fin_set)
1884 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001885 break;
1886 }
1887}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001888
1889/**
1890 * mini_cm_listen - create a listen node with params
1891 */
1892static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001893 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001894{
1895 struct nes_cm_listener *listener;
1896 unsigned long flags;
1897
1898 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1899 cm_info->loc_addr, cm_info->loc_port);
1900
1901 /* cannot have multiple matching listeners */
1902 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1903 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1904 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1905 /* find automatically incs ref count ??? */
1906 atomic_dec(&listener->ref_count);
1907 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1908 return NULL;
1909 }
1910
1911 if (!listener) {
1912 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1913 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1914 if (!listener) {
1915 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1916 return NULL;
1917 }
1918
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001919 listener->loc_addr = htonl(cm_info->loc_addr);
1920 listener->loc_port = htons(cm_info->loc_port);
1921 listener->reused_node = 0;
1922
1923 atomic_set(&listener->ref_count, 1);
1924 }
1925 /* pasive case */
1926 /* find already inc'ed the ref count */
1927 else {
1928 listener->reused_node = 1;
1929 }
1930
1931 listener->cm_id = cm_info->cm_id;
1932 atomic_set(&listener->pend_accepts_cnt, 0);
1933 listener->cm_core = cm_core;
1934 listener->nesvnic = nesvnic;
1935 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001936
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001937 listener->conn_type = cm_info->conn_type;
1938 listener->backlog = cm_info->backlog;
1939 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1940
1941 if (!listener->reused_node) {
1942 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1943 list_add(&listener->list, &cm_core->listen_list.list);
1944 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1945 atomic_inc(&cm_core->listen_node_cnt);
1946 }
1947
1948 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1949 " listener = %p, backlog = %d, cm_id = %p.\n",
1950 cm_info->loc_addr, cm_info->loc_port,
1951 listener, listener->backlog, listener->cm_id);
1952
1953 return listener;
1954}
1955
1956
1957/**
1958 * mini_cm_connect - make a connection node with params
1959 */
Faisal Latif54c86a82008-09-30 14:47:27 -07001960static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001961 struct nes_vnic *nesvnic, u16 private_data_len,
1962 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001963{
1964 int ret = 0;
1965 struct nes_cm_node *cm_node;
1966 struct nes_cm_listener *loopbackremotelistener;
1967 struct nes_cm_node *loopbackremotenode;
1968 struct nes_cm_info loopback_cm_info;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001969 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
1970 struct ietf_mpa_frame *mpa_frame = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001971
1972 /* create a CM connection node */
1973 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
1974 if (!cm_node)
1975 return NULL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001976 mpa_frame = &cm_node->mpa_frame;
1977 strcpy(mpa_frame->key, IEFT_MPA_KEY_REQ);
1978 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
1979 mpa_frame->rev = IETF_MPA_VERSION;
1980 mpa_frame->priv_data_len = htons(private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001981
Glenn Streiff7495ab62008-04-29 13:46:54 -07001982 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001983 cm_node->tcp_cntxt.client = 1;
1984 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1985
1986 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001987 loopbackremotelistener = find_listener(cm_core,
1988 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
1989 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001990 if (loopbackremotelistener == NULL) {
1991 create_event(cm_node, NES_CM_EVENT_ABORTED);
1992 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001993 loopback_cm_info = *cm_info;
1994 loopback_cm_info.loc_port = cm_info->rem_port;
1995 loopback_cm_info.rem_port = cm_info->loc_port;
1996 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001997 loopbackremotenode = make_cm_node(cm_core, nesvnic,
1998 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07001999 if (!loopbackremotenode) {
2000 rem_ref_cm_node(cm_node->cm_core, cm_node);
2001 return NULL;
2002 }
2003 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002004 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002005 loopbackremotenode->tcp_cntxt.rcv_wscale =
2006 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002007 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002008 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2009 private_data_len);
2010 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002011
Faisal Latif6492cdf2008-07-24 20:50:45 -07002012 /* we are done handling this state. */
2013 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002014 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002015 cm_node->tcp_cntxt.rcv_nxt =
2016 loopbackremotenode->tcp_cntxt.loc_seq_num;
2017 loopbackremotenode->tcp_cntxt.rcv_nxt =
2018 cm_node->tcp_cntxt.loc_seq_num;
2019 cm_node->tcp_cntxt.max_snd_wnd =
2020 loopbackremotenode->tcp_cntxt.rcv_wnd;
2021 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2022 cm_node->tcp_cntxt.rcv_wnd;
2023 cm_node->tcp_cntxt.snd_wnd =
2024 loopbackremotenode->tcp_cntxt.rcv_wnd;
2025 loopbackremotenode->tcp_cntxt.snd_wnd =
2026 cm_node->tcp_cntxt.rcv_wnd;
2027 cm_node->tcp_cntxt.snd_wscale =
2028 loopbackremotenode->tcp_cntxt.rcv_wscale;
2029 loopbackremotenode->tcp_cntxt.snd_wscale =
2030 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002031 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002032 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2033 }
2034 return cm_node;
2035 }
2036
2037 /* set our node side to client (active) side */
2038 cm_node->tcp_cntxt.client = 1;
2039 /* init our MPA frame ptr */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002040 memcpy(mpa_frame->priv_data, private_data, private_data_len);
2041
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002042 cm_node->mpa_frame_size = mpa_frame_size;
2043
2044 /* send a syn and goto syn sent state */
2045 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002046 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002047
Faisal Latif6492cdf2008-07-24 20:50:45 -07002048 if (ret) {
2049 /* error in sending the syn free up the cm_node struct */
2050 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2051 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2052 cm_node->rem_addr, cm_node->rem_port, cm_node,
2053 cm_node->cm_id);
2054 rem_ref_cm_node(cm_node->cm_core, cm_node);
2055 cm_node = NULL;
2056 }
2057
2058 if (cm_node)
2059 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2060 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2061 cm_node->rem_addr, cm_node->rem_port, cm_node,
2062 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002063
2064 return cm_node;
2065}
2066
2067
2068/**
2069 * mini_cm_accept - accept a connection
2070 * This function is never called
2071 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002072static int mini_cm_accept(struct nes_cm_core *cm_core,
2073 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002074{
2075 return 0;
2076}
2077
2078
2079/**
2080 * mini_cm_reject - reject and teardown a connection
2081 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002082static int mini_cm_reject(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002083 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002084{
2085 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002086 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002087 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002088 struct nes_cm_event event;
2089 struct iw_cm_id *cm_id = cm_node->cm_id;
2090 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002091
Faisal Latif6492cdf2008-07-24 20:50:45 -07002092 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2093 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002094
Faisal Latif6492cdf2008-07-24 20:50:45 -07002095 if (cm_node->tcp_cntxt.client)
2096 return ret;
2097 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002098
Faisal Latif9d5ab132009-03-06 15:15:01 -08002099 if (!loopback) {
2100 passive_state = atomic_add_return(1, &cm_node->passive_state);
2101 if (passive_state == NES_SEND_RESET_EVENT) {
2102 cm_node->state = NES_CM_STATE_CLOSED;
2103 rem_ref_cm_node(cm_core, cm_node);
2104 } else {
2105 ret = send_mpa_reject(cm_node);
2106 if (ret) {
2107 cm_node->state = NES_CM_STATE_CLOSED;
2108 err = send_reset(cm_node, NULL);
2109 if (err)
2110 WARN_ON(1);
2111 } else
2112 cm_id->add_ref(cm_id);
2113 }
2114 } else {
2115 cm_node->cm_id = NULL;
2116 event.cm_node = loopback;
2117 event.cm_info.rem_addr = loopback->rem_addr;
2118 event.cm_info.loc_addr = loopback->loc_addr;
2119 event.cm_info.rem_port = loopback->rem_port;
2120 event.cm_info.loc_port = loopback->loc_port;
2121 event.cm_info.cm_id = loopback->cm_id;
2122 cm_event_mpa_reject(&event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002123 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002124 loopback->state = NES_CM_STATE_CLOSING;
2125
2126 cm_id = loopback->cm_id;
2127 rem_ref_cm_node(cm_core, loopback);
2128 cm_id->rem_ref(cm_id);
2129 }
2130
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002131 return ret;
2132}
2133
2134
2135/**
2136 * mini_cm_close
2137 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002138static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002139{
2140 int ret = 0;
2141
2142 if (!cm_core || !cm_node)
2143 return -EINVAL;
2144
2145 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002146 case NES_CM_STATE_SYN_RCVD:
2147 case NES_CM_STATE_SYN_SENT:
2148 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2149 case NES_CM_STATE_ESTABLISHED:
2150 case NES_CM_STATE_ACCEPTING:
2151 case NES_CM_STATE_MPAREQ_SENT:
2152 case NES_CM_STATE_MPAREQ_RCVD:
2153 cleanup_retrans_entry(cm_node);
2154 send_reset(cm_node, NULL);
2155 break;
2156 case NES_CM_STATE_CLOSE_WAIT:
2157 cm_node->state = NES_CM_STATE_LAST_ACK;
2158 send_fin(cm_node, NULL);
2159 break;
2160 case NES_CM_STATE_FIN_WAIT1:
2161 case NES_CM_STATE_FIN_WAIT2:
2162 case NES_CM_STATE_LAST_ACK:
2163 case NES_CM_STATE_TIME_WAIT:
2164 case NES_CM_STATE_CLOSING:
2165 ret = -1;
2166 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002167 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002168 case NES_CM_STATE_LISTENING:
2169 case NES_CM_STATE_UNKNOWN:
2170 case NES_CM_STATE_INITED:
2171 case NES_CM_STATE_CLOSED:
2172 ret = rem_ref_cm_node(cm_core, cm_node);
2173 break;
2174 case NES_CM_STATE_TSA:
2175 if (cm_node->send_entry)
2176 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2177 "send_entry=%p\n", cm_node->send_entry);
2178 ret = rem_ref_cm_node(cm_core, cm_node);
2179 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002180 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002181 return ret;
2182}
2183
2184
2185/**
2186 * recv_pkt - recv an ETHERNET packet, and process it through CM
2187 * node state machine
2188 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002189static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002190 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002191{
2192 struct nes_cm_node *cm_node = NULL;
2193 struct nes_cm_listener *listener = NULL;
2194 struct iphdr *iph;
2195 struct tcphdr *tcph;
2196 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002197 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002198 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002199
Faisal Latif6492cdf2008-07-24 20:50:45 -07002200 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002201 return 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002202 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002203 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002204 }
2205
2206 iph = (struct iphdr *)skb->data;
2207 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002208
2209 nfo.loc_addr = ntohl(iph->daddr);
2210 nfo.loc_port = ntohs(tcph->dest);
2211 nfo.rem_addr = ntohl(iph->saddr);
2212 nfo.rem_port = ntohs(tcph->source);
2213
Harvey Harrison03080e52009-01-10 21:45:42 -08002214 tmp_daddr = cpu_to_be32(iph->daddr);
2215 tmp_saddr = cpu_to_be32(iph->saddr);
2216
Harvey Harrison63779432008-10-31 00:56:00 -07002217 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002218 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002219
Faisal Latif6492cdf2008-07-24 20:50:45 -07002220 do {
2221 cm_node = find_node(cm_core,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002222 nfo.rem_port, nfo.rem_addr,
2223 nfo.loc_port, nfo.loc_addr);
2224
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002225 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002226 /* Only type of packet accepted are for */
2227 /* the PASSIVE open (syn only) */
2228 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002229 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002230 break;
2231 }
2232 listener = find_listener(cm_core, nfo.loc_addr,
2233 nfo.loc_port,
2234 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002235 if (!listener) {
2236 nfo.cm_id = NULL;
2237 nfo.conn_type = 0;
2238 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2239 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002240 break;
2241 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002242 nfo.cm_id = listener->cm_id;
2243 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002244 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2245 listener);
2246 if (!cm_node) {
2247 nes_debug(NES_DBG_CM, "Unable to allocate "
2248 "node\n");
2249 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002250 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002251 dev_kfree_skb_any(skb);
2252 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002253 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002254 if (!tcph->rst && !tcph->fin) {
2255 cm_node->state = NES_CM_STATE_LISTENING;
2256 } else {
2257 cm_packets_dropped++;
2258 rem_ref_cm_node(cm_core, cm_node);
2259 dev_kfree_skb_any(skb);
2260 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002261 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002262 add_ref_cm_node(cm_node);
2263 } else if (cm_node->state == NES_CM_STATE_TSA) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002264 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002265 atomic_inc(&cm_accel_dropped_pkts);
2266 dev_kfree_skb_any(skb);
2267 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002268 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002269 skb_reset_network_header(skb);
2270 skb_set_transport_header(skb, sizeof(*tcph));
2271 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002272 process_packet(cm_node, skb, cm_core);
2273 rem_ref_cm_node(cm_core, cm_node);
2274 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002275 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002276}
2277
2278
2279/**
2280 * nes_cm_alloc_core - allocate a top level instance of a cm core
2281 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002282static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002283{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002284 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002285
2286 /* setup the CM core */
2287 /* alloc top level core control structure */
2288 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2289 if (!cm_core)
2290 return NULL;
2291
2292 INIT_LIST_HEAD(&cm_core->connected_nodes);
2293 init_timer(&cm_core->tcp_timer);
2294 cm_core->tcp_timer.function = nes_cm_timer_tick;
2295
2296 cm_core->mtu = NES_CM_DEFAULT_MTU;
2297 cm_core->state = NES_CM_STATE_INITED;
2298 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2299
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002300 atomic_set(&cm_core->events_posted, 0);
2301
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002302 cm_core->api = &nes_cm_api;
2303
2304 spin_lock_init(&cm_core->ht_lock);
2305 spin_lock_init(&cm_core->listen_list_lock);
2306
2307 INIT_LIST_HEAD(&cm_core->listen_list.list);
2308
2309 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2310
2311 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2312 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2313 cm_core->post_event = nes_cm_post_event;
2314 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2315 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2316
2317 print_core(cm_core);
2318 return cm_core;
2319}
2320
2321
2322/**
2323 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2324 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002325static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002326{
2327 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2328
2329 if (!cm_core)
2330 return -EINVAL;
2331
2332 barrier();
2333
2334 if (timer_pending(&cm_core->tcp_timer)) {
2335 del_timer(&cm_core->tcp_timer);
2336 }
2337
2338 destroy_workqueue(cm_core->event_wq);
2339 destroy_workqueue(cm_core->disconn_wq);
2340 nes_debug(NES_DBG_CM, "\n");
2341 kfree(cm_core);
2342
2343 return 0;
2344}
2345
2346
2347/**
2348 * mini_cm_get
2349 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002350static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002351{
2352 return cm_core->state;
2353}
2354
2355
2356/**
2357 * mini_cm_set
2358 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002359static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002360{
2361 int ret = 0;
2362
2363 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002364 case NES_CM_SET_PKT_SIZE:
2365 cm_core->mtu = value;
2366 break;
2367 case NES_CM_SET_FREE_PKT_Q_SIZE:
2368 cm_core->free_tx_pkt_max = value;
2369 break;
2370 default:
2371 /* unknown set option */
2372 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002373 }
2374
2375 return ret;
2376}
2377
2378
2379/**
2380 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2381 * successfully exchanged when this is called
2382 */
2383static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2384{
2385 int ret = 0;
2386
2387 if (!nesqp)
2388 return -EINVAL;
2389
2390 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2391 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2392 NES_QPCONTEXT_MISC_DROS);
2393
2394 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2395 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2396
2397 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2398
2399 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2400
2401 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2402 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2403
2404 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2405 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2406 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2407
2408 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2409 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2410 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2411
2412 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2413 nesqp->nesqp_context->ts_recent = 0;
2414 nesqp->nesqp_context->ts_age = 0;
2415 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2416 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2417 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2418 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2419 cm_node->tcp_cntxt.rcv_wscale);
2420 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2421 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2422 nesqp->nesqp_context->srtt = 0;
2423 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2424 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2425 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2426 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2427 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2428 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2429
2430 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2431 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2432 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2433 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2434 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2435 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2436 le32_to_cpu(nesqp->nesqp_context->misc));
2437 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2438 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2439 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2440
2441 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2442 cm_node->state = NES_CM_STATE_TSA;
2443
2444 return ret;
2445}
2446
2447
2448/**
2449 * nes_cm_disconn
2450 */
2451int nes_cm_disconn(struct nes_qp *nesqp)
2452{
2453 unsigned long flags;
2454
2455 spin_lock_irqsave(&nesqp->lock, flags);
2456 if (nesqp->disconn_pending == 0) {
2457 nesqp->disconn_pending++;
2458 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002459 /* init our disconnect work element, to */
2460 INIT_WORK(&nesqp->disconn_work, nes_disconnect_worker);
2461
2462 queue_work(g_cm_core->disconn_wq, &nesqp->disconn_work);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002463 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002464 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002465
2466 return 0;
2467}
2468
2469
2470/**
2471 * nes_disconnect_worker
2472 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002473static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002474{
2475 struct nes_qp *nesqp = container_of(work, struct nes_qp, disconn_work);
2476
2477 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2478 nesqp->last_aeq, nesqp->hwqp.qp_id);
2479 nes_cm_disconn_true(nesqp);
2480}
2481
2482
2483/**
2484 * nes_cm_disconn_true
2485 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002486static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002487{
2488 unsigned long flags;
2489 int ret = 0;
2490 struct iw_cm_id *cm_id;
2491 struct iw_cm_event cm_event;
2492 struct nes_vnic *nesvnic;
2493 u16 last_ae;
2494 u8 original_hw_tcp_state;
2495 u8 original_ibqp_state;
2496 u8 issued_disconnect_reset = 0;
2497
2498 if (!nesqp) {
2499 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2500 return -1;
2501 }
2502
2503 spin_lock_irqsave(&nesqp->lock, flags);
2504 cm_id = nesqp->cm_id;
2505 /* make sure we havent already closed this connection */
2506 if (!cm_id) {
2507 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2508 nesqp->hwqp.qp_id);
2509 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002510 return -1;
2511 }
2512
2513 nesvnic = to_nesvnic(nesqp->ibqp.device);
2514 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2515
2516 original_hw_tcp_state = nesqp->hw_tcp_state;
2517 original_ibqp_state = nesqp->ibqp_state;
2518 last_ae = nesqp->last_aeq;
2519
2520
2521 nes_debug(NES_DBG_CM, "set ibqp_state=%u\n", nesqp->ibqp_state);
2522
2523 if ((nesqp->cm_id) && (cm_id->event_handler)) {
2524 if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2525 ((original_ibqp_state == IB_QPS_RTS) &&
2526 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2527 atomic_inc(&cm_disconnects);
2528 cm_event.event = IW_CM_EVENT_DISCONNECT;
2529 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002530 cm_event.status = IW_CM_EVENT_STATUS_RESET;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002531 nes_debug(NES_DBG_CM, "Generating a CM "
2532 "Disconnect Event (status reset) for "
2533 "QP%u, cm_id = %p. \n",
2534 nesqp->hwqp.qp_id, cm_id);
2535 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002536 cm_event.status = IW_CM_EVENT_STATUS_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002537
2538 cm_event.local_addr = cm_id->local_addr;
2539 cm_event.remote_addr = cm_id->remote_addr;
2540 cm_event.private_data = NULL;
2541 cm_event.private_data_len = 0;
2542
Faisal Latif6492cdf2008-07-24 20:50:45 -07002543 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2544 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2545 "cm_id = %p, refcount = %u.\n",
2546 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2547 nesqp->hwqp.sq_tail, cm_id,
2548 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002549
2550 spin_unlock_irqrestore(&nesqp->lock, flags);
2551 ret = cm_id->event_handler(cm_id, &cm_event);
2552 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002553 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2554 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002555 spin_lock_irqsave(&nesqp->lock, flags);
2556 }
2557
2558 nesqp->disconn_pending = 0;
2559 /* There might have been another AE while the lock was released */
2560 original_hw_tcp_state = nesqp->hw_tcp_state;
2561 original_ibqp_state = nesqp->ibqp_state;
2562 last_ae = nesqp->last_aeq;
2563
2564 if ((issued_disconnect_reset == 0) && (nesqp->cm_id) &&
2565 ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2566 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2567 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2568 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2569 atomic_inc(&cm_closes);
2570 nesqp->cm_id = NULL;
2571 nesqp->in_disconnect = 0;
2572 spin_unlock_irqrestore(&nesqp->lock, flags);
2573 nes_disconnect(nesqp, 1);
2574
2575 cm_id->provider_data = nesqp;
2576 /* Send up the close complete event */
2577 cm_event.event = IW_CM_EVENT_CLOSE;
2578 cm_event.status = IW_CM_EVENT_STATUS_OK;
2579 cm_event.provider_data = cm_id->provider_data;
2580 cm_event.local_addr = cm_id->local_addr;
2581 cm_event.remote_addr = cm_id->remote_addr;
2582 cm_event.private_data = NULL;
2583 cm_event.private_data_len = 0;
2584
2585 ret = cm_id->event_handler(cm_id, &cm_event);
2586 if (ret) {
2587 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2588 }
2589
2590 cm_id->rem_ref(cm_id);
2591
2592 spin_lock_irqsave(&nesqp->lock, flags);
2593 if (nesqp->flush_issued == 0) {
2594 nesqp->flush_issued = 1;
2595 spin_unlock_irqrestore(&nesqp->lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002596 flush_wqes(nesvnic->nesdev, nesqp,
2597 NES_CQP_FLUSH_RQ, 1);
2598 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002599 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002600 } else {
2601 cm_id = nesqp->cm_id;
2602 spin_unlock_irqrestore(&nesqp->lock, flags);
2603 /* check to see if the inbound reset beat the outbound reset */
2604 if ((!cm_id) && (last_ae==NES_AEQE_AEID_RESET_SENT)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002605 nes_debug(NES_DBG_CM, "QP%u: Decing refcount "
2606 "due to inbound reset beating the "
2607 "outbound reset.\n", nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002608 }
2609 }
2610 } else {
2611 nesqp->disconn_pending = 0;
2612 spin_unlock_irqrestore(&nesqp->lock, flags);
2613 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002614
2615 return 0;
2616}
2617
2618
2619/**
2620 * nes_disconnect
2621 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002622static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002623{
2624 int ret = 0;
2625 struct nes_vnic *nesvnic;
2626 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002627 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002628
2629 nesvnic = to_nesvnic(nesqp->ibqp.device);
2630 if (!nesvnic)
2631 return -EINVAL;
2632
2633 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002634 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002635
2636 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2637 atomic_read(&nesvnic->netdev->refcnt));
2638
2639 if (nesqp->active_conn) {
2640
2641 /* indicate this connection is NOT active */
2642 nesqp->active_conn = 0;
2643 } else {
2644 /* Need to free the Last Streaming Mode Message */
2645 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002646 if (nesqp->lsmm_mr)
2647 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002648 pci_free_consistent(nesdev->pcidev,
2649 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2650 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2651 }
2652 }
2653
2654 /* close the CM node down if it is still active */
2655 if (nesqp->cm_node) {
2656 nes_debug(NES_DBG_CM, "Call close API\n");
2657
2658 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002659 }
2660
2661 return ret;
2662}
2663
2664
2665/**
2666 * nes_accept
2667 */
2668int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2669{
2670 u64 u64temp;
2671 struct ib_qp *ibqp;
2672 struct nes_qp *nesqp;
2673 struct nes_vnic *nesvnic;
2674 struct nes_device *nesdev;
2675 struct nes_cm_node *cm_node;
2676 struct nes_adapter *adapter;
2677 struct ib_qp_attr attr;
2678 struct iw_cm_event cm_event;
2679 struct nes_hw_qp_wqe *wqe;
2680 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002681 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002682 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002683 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002684 struct nes_ib_device *nesibdev;
2685 struct ib_mr *ibmr = NULL;
2686 struct ib_phys_buf ibphysbuf;
2687 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002688 u64 tagged_offset;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002689
2690
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002691
2692 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2693 if (!ibqp)
2694 return -EINVAL;
2695
2696 /* get all our handles */
2697 nesqp = to_nesqp(ibqp);
2698 nesvnic = to_nesvnic(nesqp->ibqp.device);
2699 nesdev = nesvnic->nesdev;
2700 adapter = nesdev->nesadapter;
2701
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002702 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002703 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2704 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2705 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002706
2707 /* associate the node with the QP */
2708 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002709 cm_node->nesqp = nesqp;
2710 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002711
Faisal Latif6492cdf2008-07-24 20:50:45 -07002712 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2713 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002714 atomic_inc(&cm_accepts);
2715
2716 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2717 atomic_read(&nesvnic->netdev->refcnt));
2718
Faisal Latif6492cdf2008-07-24 20:50:45 -07002719 /* allocate the ietf frame and space for private data */
2720 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2721 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2722 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002723
Faisal Latif6492cdf2008-07-24 20:50:45 -07002724 if (!nesqp->ietf_frame) {
2725 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2726 "data\n");
2727 return -ENOMEM;
2728 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002729
2730
Faisal Latif6492cdf2008-07-24 20:50:45 -07002731 /* setup the MPA frame */
2732 nesqp->private_data_len = conn_param->private_data_len;
2733 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002734
Faisal Latif6492cdf2008-07-24 20:50:45 -07002735 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2736 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002737
Faisal Latif6492cdf2008-07-24 20:50:45 -07002738 nesqp->ietf_frame->priv_data_len =
2739 cpu_to_be16(conn_param->private_data_len);
2740 nesqp->ietf_frame->rev = mpa_version;
2741 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002742
Faisal Latif6492cdf2008-07-24 20:50:45 -07002743 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2744 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002745
Faisal Latif6492cdf2008-07-24 20:50:45 -07002746 if (cm_id->remote_addr.sin_addr.s_addr !=
2747 cm_id->local_addr.sin_addr.s_addr) {
2748 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002749 nesibdev = nesvnic->nesibdev;
2750 nespd = nesqp->nespd;
2751 ibphysbuf.addr = nesqp->ietf_frame_pbase;
2752 ibphysbuf.size = conn_param->private_data_len +
2753 sizeof(struct ietf_mpa_frame);
Don Wood7a5efb62009-04-08 14:21:02 -07002754 tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002755 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
2756 &ibphysbuf, 1,
2757 IB_ACCESS_LOCAL_WRITE,
Don Wood7a5efb62009-04-08 14:21:02 -07002758 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002759 if (!ibmr) {
2760 nes_debug(NES_DBG_CM, "Unable to register memory region"
2761 "for lSMM for cm_node = %p \n",
2762 cm_node);
2763 return -ENOMEM;
2764 }
2765
2766 ibmr->pd = &nespd->ibpd;
2767 ibmr->device = nespd->ibpd.device;
2768 nesqp->lsmm_mr = ibmr;
2769
Faisal Latif6492cdf2008-07-24 20:50:45 -07002770 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2771 set_wqe_64bit_value(wqe->wqe_words,
2772 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2773 u64temp);
2774 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2775 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2776 NES_IWARP_SQ_WQE_WRPDU);
2777 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2778 cpu_to_le32(conn_param->private_data_len +
2779 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002780 set_wqe_64bit_value(wqe->wqe_words,
2781 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
Don Wood7a5efb62009-04-08 14:21:02 -07002782 (u64)(unsigned long)nesqp->ietf_frame);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002783 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2784 cpu_to_le32(conn_param->private_data_len +
2785 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002786 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002787
Faisal Latif6492cdf2008-07-24 20:50:45 -07002788 nesqp->nesqp_context->ird_ord_sizes |=
2789 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2790 NES_QPCONTEXT_ORDIRD_WRPDU);
2791 } else {
2792 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08002793 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002794 }
2795 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002796
2797
2798 /* Cache the cm_id in the qp */
2799 nesqp->cm_id = cm_id;
2800 cm_node->cm_id = cm_id;
2801
2802 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2803 cm_id->provider_data = nesqp;
2804 nesqp->active_conn = 0;
2805
Faisal Latif6492cdf2008-07-24 20:50:45 -07002806 if (cm_node->state == NES_CM_STATE_TSA)
2807 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2808 cm_node);
2809
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002810 nes_cm_init_tsa_conn(nesqp, cm_node);
2811
Faisal Latif6492cdf2008-07-24 20:50:45 -07002812 nesqp->nesqp_context->tcpPorts[0] =
2813 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2814 nesqp->nesqp_context->tcpPorts[1] =
2815 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2816
2817 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2818 nesqp->nesqp_context->ip0 =
2819 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2820 else
2821 nesqp->nesqp_context->ip0 =
2822 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002823
2824 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002825 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2826 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002827
Faisal Latif6492cdf2008-07-24 20:50:45 -07002828 nesqp->nesqp_context->arp_index_vlan |=
2829 cpu_to_le32(nes_arp_table(nesdev,
2830 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002831 NES_ARP_RESOLVE) << 16);
2832
2833 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002834 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002835
2836 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2837
2838 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002839 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2840 nesqp->nesqp_context->ird_ord_sizes |=
2841 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002842
2843 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002844 nes_quad.DstIpAdrIndex =
2845 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2846 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2847 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2848 else
2849 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2850 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2851 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002852
2853 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06002854 crc_value = get_crc_value(&nes_quad);
2855 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002856 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07002857 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002858
2859 nesqp->hte_index &= adapter->hte_index_mask;
2860 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2861
2862 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2863
Faisal Latif6492cdf2008-07-24 20:50:45 -07002864 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2865 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2866 "private data length=%zu.\n", nesqp->hwqp.qp_id,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002867 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2868 ntohs(cm_id->remote_addr.sin_port),
2869 ntohl(cm_id->local_addr.sin_addr.s_addr),
2870 ntohs(cm_id->local_addr.sin_port),
2871 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2872 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002873 conn_param->private_data_len +
2874 sizeof(struct ietf_mpa_frame));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002875
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002876
Coly Li73ac36e2009-01-07 18:09:16 -08002877 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002878 cm_id->add_ref(cm_id);
2879
2880 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2881 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2882 cm_event.provider_data = (void *)nesqp;
2883 cm_event.local_addr = cm_id->local_addr;
2884 cm_event.remote_addr = cm_id->remote_addr;
2885 cm_event.private_data = NULL;
2886 cm_event.private_data_len = 0;
2887 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002888 attr.qp_state = IB_QPS_RTS;
2889 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002890 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002891 cm_node->loopbackpartner->mpa_frame_size =
2892 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002893 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002894 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2895 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002896 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2897 }
2898 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002899 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2900 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002901
Faisal Latif183ecfa2008-11-21 20:50:46 -06002902 passive_state = atomic_add_return(1, &cm_node->passive_state);
2903 if (passive_state == NES_SEND_RESET_EVENT)
2904 create_event(cm_node, NES_CM_EVENT_RESET);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002905 return 0;
2906}
2907
2908
2909/**
2910 * nes_reject
2911 */
2912int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2913{
2914 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002915 struct nes_cm_node *loopback;
2916
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002917 struct nes_cm_core *cm_core;
2918
2919 atomic_inc(&cm_rejects);
2920 cm_node = (struct nes_cm_node *) cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002921 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002922 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002923 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002924 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2925
Faisal Latif9d5ab132009-03-06 15:15:01 -08002926 if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
2927 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002928
Faisal Latif9d5ab132009-03-06 15:15:01 -08002929 strcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP);
2930 if (loopback) {
2931 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
2932 loopback->mpa_frame.priv_data_len = pdata_len;
2933 loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
2934 pdata_len;
2935 } else {
2936 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2937 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2938 }
2939
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002940 cm_node->mpa_frame.rev = mpa_version;
2941 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2942
Faisal Latif9d5ab132009-03-06 15:15:01 -08002943 return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002944}
2945
2946
2947/**
2948 * nes_connect
2949 * setup and launch cm connect node
2950 */
2951int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2952{
2953 struct ib_qp *ibqp;
2954 struct nes_qp *nesqp;
2955 struct nes_vnic *nesvnic;
2956 struct nes_device *nesdev;
2957 struct nes_cm_node *cm_node;
2958 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07002959 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002960
2961 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2962 if (!ibqp)
2963 return -EINVAL;
2964 nesqp = to_nesqp(ibqp);
2965 if (!nesqp)
2966 return -EINVAL;
2967 nesvnic = to_nesvnic(nesqp->ibqp.device);
2968 if (!nesvnic)
2969 return -EINVAL;
2970 nesdev = nesvnic->nesdev;
2971 if (!nesdev)
2972 return -EINVAL;
2973
Faisal Latif6492cdf2008-07-24 20:50:45 -07002974 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
2975 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
2976 ntohl(nesvnic->local_ipaddr),
2977 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2978 ntohs(cm_id->remote_addr.sin_port),
2979 ntohl(cm_id->local_addr.sin_addr.s_addr),
2980 ntohs(cm_id->local_addr.sin_port));
2981
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002982 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002983 nesqp->active_conn = 1;
2984
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002985 /* cache the cm_id in the qp */
2986 nesqp->cm_id = cm_id;
2987
2988 cm_id->provider_data = nesqp;
2989
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002990 nesqp->private_data_len = conn_param->private_data_len;
2991 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
2992 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002993 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
2994 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002995
Faisal Latif6492cdf2008-07-24 20:50:45 -07002996 if (cm_id->local_addr.sin_addr.s_addr !=
Faisal Latif53094c32009-04-27 13:37:34 -07002997 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002998 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002999 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003000 apbvt_set = 1;
3001 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003002
3003 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003004 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3005 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3006 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3007 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003008 cm_info.cm_id = cm_id;
3009 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3010
3011 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003012
3013 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003014 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3015 conn_param->private_data_len, (void *)conn_param->private_data,
3016 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003017 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003018 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003019 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003020 PCI_FUNC(nesdev->pcidev->devfn),
3021 NES_MANAGE_APBVT_DEL);
3022
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003023 cm_id->rem_ref(cm_id);
3024 return -ENOMEM;
3025 }
3026
Faisal Latif53094c32009-04-27 13:37:34 -07003027 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003028 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003029 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003030 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003031
3032 return 0;
3033}
3034
3035
3036/**
3037 * nes_create_listen
3038 */
3039int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3040{
3041 struct nes_vnic *nesvnic;
3042 struct nes_cm_listener *cm_node;
3043 struct nes_cm_info cm_info;
3044 struct nes_adapter *adapter;
3045 int err;
3046
3047
3048 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3049 cm_id, ntohs(cm_id->local_addr.sin_port));
3050
3051 nesvnic = to_nesvnic(cm_id->device);
3052 if (!nesvnic)
3053 return -EINVAL;
3054 adapter = nesvnic->nesdev->nesadapter;
3055 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3056 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3057
3058 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3059 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3060
3061 /* setup listen params in our api call struct */
3062 cm_info.loc_addr = nesvnic->local_ipaddr;
3063 cm_info.loc_port = cm_id->local_addr.sin_port;
3064 cm_info.backlog = backlog;
3065 cm_info.cm_id = cm_id;
3066
3067 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3068
3069
3070 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3071 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003072 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003073 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003074 return -ENOMEM;
3075 }
3076
3077 cm_id->provider_data = cm_node;
3078
3079 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003080 err = nes_manage_apbvt(nesvnic,
3081 ntohs(cm_id->local_addr.sin_port),
3082 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3083 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003084 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003085 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3086 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003087 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3088 return err;
3089 }
3090 cm_listens_created++;
3091 }
3092
3093 cm_id->add_ref(cm_id);
3094 cm_id->provider_data = (void *)cm_node;
3095
3096
3097 return 0;
3098}
3099
3100
3101/**
3102 * nes_destroy_listen
3103 */
3104int nes_destroy_listen(struct iw_cm_id *cm_id)
3105{
3106 if (cm_id->provider_data)
3107 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3108 else
3109 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3110
3111 cm_id->rem_ref(cm_id);
3112
3113 return 0;
3114}
3115
3116
3117/**
3118 * nes_cm_recv
3119 */
3120int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3121{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003122 int rc = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003123 cm_packets_received++;
3124 if ((g_cm_core) && (g_cm_core->api)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003125 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003126 } else {
3127 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3128 " cm is not setup properly.\n");
3129 }
3130
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003131 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003132}
3133
3134
3135/**
3136 * nes_cm_start
3137 * Start and init a cm core module
3138 */
3139int nes_cm_start(void)
3140{
3141 nes_debug(NES_DBG_CM, "\n");
3142 /* create the primary CM core, pass this handle to subsequent core inits */
3143 g_cm_core = nes_cm_alloc_core();
3144 if (g_cm_core) {
3145 return 0;
3146 } else {
3147 return -ENOMEM;
3148 }
3149}
3150
3151
3152/**
3153 * nes_cm_stop
3154 * stop and dealloc all cm core instances
3155 */
3156int nes_cm_stop(void)
3157{
3158 g_cm_core->api->destroy_cm_core(g_cm_core);
3159 return 0;
3160}
3161
3162
3163/**
3164 * cm_event_connected
3165 * handle a connected event, setup QPs and HW
3166 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003167static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003168{
3169 u64 u64temp;
3170 struct nes_qp *nesqp;
3171 struct nes_vnic *nesvnic;
3172 struct nes_device *nesdev;
3173 struct nes_cm_node *cm_node;
3174 struct nes_adapter *nesadapter;
3175 struct ib_qp_attr attr;
3176 struct iw_cm_id *cm_id;
3177 struct iw_cm_event cm_event;
3178 struct nes_hw_qp_wqe *wqe;
3179 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003180 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003181 int ret;
3182
3183 /* get all our handles */
3184 cm_node = event->cm_node;
3185 cm_id = cm_node->cm_id;
3186 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3187 nesqp = (struct nes_qp *)cm_id->provider_data;
3188 nesvnic = to_nesvnic(nesqp->ibqp.device);
3189 nesdev = nesvnic->nesdev;
3190 nesadapter = nesdev->nesadapter;
3191
3192 if (nesqp->destroyed) {
3193 return;
3194 }
3195 atomic_inc(&cm_connecteds);
3196 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3197 " local port 0x%04X. jiffies = %lu.\n",
3198 nesqp->hwqp.qp_id,
3199 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3200 ntohs(cm_id->remote_addr.sin_port),
3201 ntohs(cm_id->local_addr.sin_port),
3202 jiffies);
3203
3204 nes_cm_init_tsa_conn(nesqp, cm_node);
3205
3206 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003207 nesqp->nesqp_context->tcpPorts[0] =
3208 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3209 nesqp->nesqp_context->tcpPorts[1] =
3210 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3211 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3212 nesqp->nesqp_context->ip0 =
3213 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3214 else
3215 nesqp->nesqp_context->ip0 =
3216 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003217
3218 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003219 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3220 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003221 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003222 nes_arp_table(nesdev,
3223 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003224 NULL, NES_ARP_RESOLVE) << 16);
3225 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3226 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3227 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3228 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003229 cpu_to_le32((u32)1 <<
3230 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003231
3232 /* Adjust tail for not having a LSMM */
3233 nesqp->hwqp.sq_tail = 1;
3234
3235#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003236 if (cm_node->send_write0) {
3237 nes_debug(NES_DBG_CM, "Sending first write.\n");
3238 wqe = &nesqp->hwqp.sq_vbase[0];
3239 u64temp = (unsigned long)nesqp;
3240 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3241 set_wqe_64bit_value(wqe->wqe_words,
3242 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3243 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3244 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3245 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3246 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3247 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3248 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3249 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003250
Faisal Latif6492cdf2008-07-24 20:50:45 -07003251 /* use the reserved spot on the WQ for the extra first WQE */
3252 nesqp->nesqp_context->ird_ord_sizes &=
3253 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3254 NES_QPCONTEXT_ORDIRD_WRPDU |
3255 NES_QPCONTEXT_ORDIRD_ALSMM));
3256 nesqp->skip_lsmm = 1;
3257 nesqp->hwqp.sq_tail = 0;
3258 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3259 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3260 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003261#endif
3262
3263 memset(&nes_quad, 0, sizeof(nes_quad));
3264
Faisal Latif6492cdf2008-07-24 20:50:45 -07003265 nes_quad.DstIpAdrIndex =
3266 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3267 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3268 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3269 else
3270 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003271 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3272 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3273
3274 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003275 crc_value = get_crc_value(&nes_quad);
3276 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003277 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3278 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3279
3280 nesqp->hte_index &= nesadapter->hte_index_mask;
3281 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3282
3283 nesqp->ietf_frame = &cm_node->mpa_frame;
3284 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3285 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3286
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003287 /* notify OF layer we successfully created the requested connection */
3288 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3289 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3290 cm_event.provider_data = cm_id->provider_data;
3291 cm_event.local_addr.sin_family = AF_INET;
3292 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3293 cm_event.remote_addr = cm_id->remote_addr;
3294
Faisal Latif6492cdf2008-07-24 20:50:45 -07003295 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3296 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003297
3298 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3299 ret = cm_id->event_handler(cm_id, &cm_event);
3300 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3301
3302 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003303 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3304 "ret=%d\n", __func__, __LINE__, ret);
3305 attr.qp_state = IB_QPS_RTS;
3306 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003307
Faisal Latif6492cdf2008-07-24 20:50:45 -07003308 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3309 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003310
3311 return;
3312}
3313
3314
3315/**
3316 * cm_event_connect_error
3317 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003318static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003319{
3320 struct nes_qp *nesqp;
3321 struct iw_cm_id *cm_id;
3322 struct iw_cm_event cm_event;
3323 /* struct nes_cm_info cm_info; */
3324 int ret;
3325
3326 if (!event->cm_node)
3327 return;
3328
3329 cm_id = event->cm_node->cm_id;
3330 if (!cm_id) {
3331 return;
3332 }
3333
3334 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3335 nesqp = cm_id->provider_data;
3336
3337 if (!nesqp) {
3338 return;
3339 }
3340
3341 /* notify OF layer about this connection error event */
3342 /* cm_id->rem_ref(cm_id); */
3343 nesqp->cm_id = NULL;
3344 cm_id->provider_data = NULL;
3345 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3346 cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
3347 cm_event.provider_data = cm_id->provider_data;
3348 cm_event.local_addr = cm_id->local_addr;
3349 cm_event.remote_addr = cm_id->remote_addr;
3350 cm_event.private_data = NULL;
3351 cm_event.private_data_len = 0;
3352
Faisal Latif6492cdf2008-07-24 20:50:45 -07003353 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3354 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3355 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003356
3357 ret = cm_id->event_handler(cm_id, &cm_event);
3358 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3359 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003360 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3361 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003362 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003363
Faisal Latif6492cdf2008-07-24 20:50:45 -07003364 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003365 return;
3366}
3367
3368
3369/**
3370 * cm_event_reset
3371 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003372static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003373{
3374 struct nes_qp *nesqp;
3375 struct iw_cm_id *cm_id;
3376 struct iw_cm_event cm_event;
3377 /* struct nes_cm_info cm_info; */
3378 int ret;
3379
3380 if (!event->cm_node)
3381 return;
3382
3383 if (!event->cm_node->cm_id)
3384 return;
3385
3386 cm_id = event->cm_node->cm_id;
3387
3388 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3389 nesqp = cm_id->provider_data;
3390
3391 nesqp->cm_id = NULL;
3392 /* cm_id->provider_data = NULL; */
3393 cm_event.event = IW_CM_EVENT_DISCONNECT;
3394 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3395 cm_event.provider_data = cm_id->provider_data;
3396 cm_event.local_addr = cm_id->local_addr;
3397 cm_event.remote_addr = cm_id->remote_addr;
3398 cm_event.private_data = NULL;
3399 cm_event.private_data_len = 0;
3400
3401 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003402 cm_id->add_ref(cm_id);
3403 atomic_inc(&cm_closes);
3404 cm_event.event = IW_CM_EVENT_CLOSE;
3405 cm_event.status = IW_CM_EVENT_STATUS_OK;
3406 cm_event.provider_data = cm_id->provider_data;
3407 cm_event.local_addr = cm_id->local_addr;
3408 cm_event.remote_addr = cm_id->remote_addr;
3409 cm_event.private_data = NULL;
3410 cm_event.private_data_len = 0;
3411 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3412 ret = cm_id->event_handler(cm_id, &cm_event);
3413
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003414 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3415
3416
3417 /* notify OF layer about this connection error event */
3418 cm_id->rem_ref(cm_id);
3419
3420 return;
3421}
3422
3423
3424/**
3425 * cm_event_mpa_req
3426 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003427static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003428{
3429 struct iw_cm_id *cm_id;
3430 struct iw_cm_event cm_event;
3431 int ret;
3432 struct nes_cm_node *cm_node;
3433
3434 cm_node = event->cm_node;
3435 if (!cm_node)
3436 return;
3437 cm_id = cm_node->cm_id;
3438
3439 atomic_inc(&cm_connect_reqs);
3440 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3441 cm_node, cm_id, jiffies);
3442
3443 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3444 cm_event.status = IW_CM_EVENT_STATUS_OK;
3445 cm_event.provider_data = (void *)cm_node;
3446
3447 cm_event.local_addr.sin_family = AF_INET;
3448 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3449 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3450
3451 cm_event.remote_addr.sin_family = AF_INET;
3452 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3453 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003454 cm_event.private_data = cm_node->mpa_frame_buf;
3455 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003456
3457 ret = cm_id->event_handler(cm_id, &cm_event);
3458 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003459 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3460 __func__, __LINE__, ret);
3461 return;
3462}
3463
3464
3465static void cm_event_mpa_reject(struct nes_cm_event *event)
3466{
3467 struct iw_cm_id *cm_id;
3468 struct iw_cm_event cm_event;
3469 struct nes_cm_node *cm_node;
3470 int ret;
3471
3472 cm_node = event->cm_node;
3473 if (!cm_node)
3474 return;
3475 cm_id = cm_node->cm_id;
3476
3477 atomic_inc(&cm_connect_reqs);
3478 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3479 cm_node, cm_id, jiffies);
3480
3481 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3482 cm_event.status = -ECONNREFUSED;
3483 cm_event.provider_data = cm_id->provider_data;
3484
3485 cm_event.local_addr.sin_family = AF_INET;
3486 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3487 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3488
3489 cm_event.remote_addr.sin_family = AF_INET;
3490 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3491 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3492
3493 cm_event.private_data = cm_node->mpa_frame_buf;
3494 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3495
3496 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3497 "remove_addr=%08x\n",
3498 cm_event.local_addr.sin_addr.s_addr,
3499 cm_event.remote_addr.sin_addr.s_addr);
3500
3501 ret = cm_id->event_handler(cm_id, &cm_event);
3502 if (ret)
3503 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003504 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003505
3506 return;
3507}
3508
3509
3510static void nes_cm_event_handler(struct work_struct *);
3511
3512/**
3513 * nes_cm_post_event
3514 * post an event to the cm event handler
3515 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003516static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003517{
3518 atomic_inc(&event->cm_node->cm_core->events_posted);
3519 add_ref_cm_node(event->cm_node);
3520 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3521 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003522 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3523 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003524
3525 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3526
3527 nes_debug(NES_DBG_CM, "Exit\n");
3528 return 0;
3529}
3530
3531
3532/**
3533 * nes_cm_event_handler
3534 * worker function to handle cm events
3535 * will free instance of nes_cm_event
3536 */
3537static void nes_cm_event_handler(struct work_struct *work)
3538{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003539 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3540 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003541 struct nes_cm_core *cm_core;
3542
Faisal Latif6492cdf2008-07-24 20:50:45 -07003543 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003544 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003545
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003546 cm_core = event->cm_node->cm_core;
3547 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003548 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003549
3550 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003551 case NES_CM_EVENT_MPA_REQ:
3552 cm_event_mpa_req(event);
3553 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3554 event->cm_node);
3555 break;
3556 case NES_CM_EVENT_RESET:
3557 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3558 event->cm_node);
3559 cm_event_reset(event);
3560 break;
3561 case NES_CM_EVENT_CONNECTED:
3562 if ((!event->cm_node->cm_id) ||
3563 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003564 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003565 cm_event_connected(event);
3566 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3567 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003568 case NES_CM_EVENT_MPA_REJECT:
3569 if ((!event->cm_node->cm_id) ||
3570 (event->cm_node->state == NES_CM_STATE_TSA))
3571 break;
3572 cm_event_mpa_reject(event);
3573 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3574 break;
3575
Faisal Latif6492cdf2008-07-24 20:50:45 -07003576 case NES_CM_EVENT_ABORTED:
3577 if ((!event->cm_node->cm_id) ||
3578 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003579 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003580 cm_event_connect_error(event);
3581 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3582 break;
3583 case NES_CM_EVENT_DROPPED_PKT:
3584 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3585 break;
3586 default:
3587 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3588 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003589 }
3590
3591 atomic_dec(&cm_core->events_posted);
3592 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3593 rem_ref_cm_node(cm_core, event->cm_node);
3594 kfree(event);
3595
3596 return;
3597}