blob: d89bdee0cf554ca9388c2e6c2e6ca29536a99829 [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{
Faisal Latif68237a02009-06-22 22:53:28 -0700475 struct iw_cm_id *cm_id = cm_node->cm_id;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800476 switch (cm_node->state) {
477 case NES_CM_STATE_SYN_RCVD:
478 case NES_CM_STATE_CLOSING:
479 rem_ref_cm_node(cm_node->cm_core, cm_node);
480 break;
481 case NES_CM_STATE_LAST_ACK:
482 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif68237a02009-06-22 22:53:28 -0700483 if (cm_node->cm_id)
484 cm_id->rem_ref(cm_id);
485 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800486 send_reset(cm_node, NULL);
487 break;
488 default:
489 create_event(cm_node, NES_CM_EVENT_ABORTED);
490 }
491}
492
493static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
494{
495 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
496 struct iw_cm_id *cm_id = cm_node->cm_id;
497 struct nes_qp *nesqp;
498 unsigned long qplockflags;
499
500 if (!recv_entry)
501 return;
502 nesqp = (struct nes_qp *)recv_entry->skb;
503 if (nesqp) {
504 spin_lock_irqsave(&nesqp->lock, qplockflags);
505 if (nesqp->cm_id) {
506 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
507 "refcount = %d: HIT A "
508 "NES_TIMER_TYPE_CLOSE with something "
509 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
510 atomic_read(&nesqp->refcount));
511 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
512 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
513 nesqp->ibqp_state = IB_QPS_ERR;
514 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
515 nes_cm_disconn(nesqp);
516 } else {
517 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
518 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
519 "refcount = %d: HIT A "
520 "NES_TIMER_TYPE_CLOSE with nothing "
521 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
522 atomic_read(&nesqp->refcount));
523 }
524 } else if (rem_node) {
525 /* TIME_WAIT state */
526 rem_ref_cm_node(cm_node->cm_core, cm_node);
527 }
528 if (cm_node->cm_id)
529 cm_id->rem_ref(cm_id);
530 kfree(recv_entry);
531 cm_node->recv_entry = NULL;
532}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800533
534/**
535 * nes_cm_timer_tick
536 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700537static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800538{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800539 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800540 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800541 struct nes_cm_node *cm_node;
542 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800543 struct list_head *list_core_temp;
544 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800545 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800546 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700547 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800548 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800549
Faisal Latif879e5bd2008-11-21 20:50:41 -0600550 struct list_head timer_list;
551 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800552 spin_lock_irqsave(&cm_core->ht_lock, flags);
553
Faisal Latif6492cdf2008-07-24 20:50:45 -0700554 list_for_each_safe(list_node, list_core_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600555 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800556 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800557 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600558 add_ref_cm_node(cm_node);
559 list_add(&cm_node->timer_entry, &timer_list);
560 }
561 }
562 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
563
564 list_for_each_safe(list_node, list_core_temp, &timer_list) {
565 cm_node = container_of(list_node, struct nes_cm_node,
566 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800567 recv_entry = cm_node->recv_entry;
568
569 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700570 if (time_after(recv_entry->timetosend, jiffies)) {
571 if (nexttimeout > recv_entry->timetosend ||
Faisal Latif9d5ab132009-03-06 15:15:01 -0800572 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800573 nexttimeout = recv_entry->timetosend;
574 settimer = 1;
575 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800576 } else
577 handle_recv_entry(cm_node, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800578 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800579
580 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700581 do {
582 send_entry = cm_node->send_entry;
583 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600584 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800585 if (time_after(send_entry->timetosend, jiffies)) {
586 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700587 if ((nexttimeout >
588 send_entry->timetosend) ||
589 !settimer) {
590 nexttimeout =
591 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800592 settimer = 1;
593 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800594 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700595 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800596 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800597 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800598 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700599
600 if ((cm_node->state == NES_CM_STATE_TSA) ||
601 (cm_node->state == NES_CM_STATE_CLOSED)) {
602 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600603 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800604 }
605
Faisal Latif6492cdf2008-07-24 20:50:45 -0700606 if (!send_entry->retranscount ||
607 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800608 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700609 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800610
Faisal Latif6492cdf2008-07-24 20:50:45 -0700611 spin_unlock_irqrestore(
612 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800613 nes_retrans_expired(cm_node);
614 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700615 spin_lock_irqsave(&cm_node->retrans_list_lock,
616 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600617 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800618 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800619 atomic_inc(&send_entry->skb->users);
620 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700621 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
622 "for node %p, jiffies = %lu, time to send = "
623 "%lu, retranscount = %u, send_entry->seq_num = "
624 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
625 "0x%08X\n", send_entry, cm_node, jiffies,
626 send_entry->timetosend,
627 send_entry->retranscount,
628 send_entry->seq_num,
629 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800630
Faisal Latif6492cdf2008-07-24 20:50:45 -0700631 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
632 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800633 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700634 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800635 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700636 nes_debug(NES_DBG_CM, "rexmit failed for "
637 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800638 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800639 send_entry->retrycount--;
640 nexttimeout = jiffies + NES_SHORT_TIME;
641 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600642 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800643 } else {
644 cm_packets_sent++;
645 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700646 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
647 "%u, retry count = %u.\n",
648 send_entry->retranscount,
649 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800650 if (send_entry->send_retrans) {
651 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700652 timetosend = (NES_RETRY_TIMEOUT <<
653 (NES_DEFAULT_RETRANS - send_entry->retranscount));
654
Faisal Latif6492cdf2008-07-24 20:50:45 -0700655 send_entry->timetosend = jiffies +
Faisal Latif4e9c3902009-04-27 13:39:36 -0700656 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700657 if (nexttimeout > send_entry->timetosend ||
658 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800659 nexttimeout = send_entry->timetosend;
660 settimer = 1;
661 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800662 } else {
663 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700664 close_when_complete =
665 send_entry->close_when_complete;
666 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
667 cm_node, cm_node->state);
668 free_retrans_entry(cm_node);
669 if (close_when_complete)
670 rem_ref_cm_node(cm_node->cm_core,
671 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800672 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700673 } while (0);
674
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800675 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700676 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800677 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800678
679 if (settimer) {
680 if (!timer_pending(&cm_core->tcp_timer)) {
681 cm_core->tcp_timer.expires = nexttimeout;
682 add_timer(&cm_core->tcp_timer);
683 }
684 }
685}
686
687
688/**
689 * send_syn
690 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700691static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
692 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800693{
694 int ret;
695 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800696 char optionsbuffer[sizeof(struct option_mss) +
Faisal Latif6492cdf2008-07-24 20:50:45 -0700697 sizeof(struct option_windowscale) + sizeof(struct option_base) +
698 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800699
700 int optionssize = 0;
701 /* Sending MSS option */
702 union all_known_options *options;
703
704 if (!cm_node)
705 return -EINVAL;
706
707 options = (union all_known_options *)&optionsbuffer[optionssize];
708 options->as_mss.optionnum = OPTION_NUMBER_MSS;
709 options->as_mss.length = sizeof(struct option_mss);
710 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
711 optionssize += sizeof(struct option_mss);
712
713 options = (union all_known_options *)&optionsbuffer[optionssize];
714 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
715 options->as_windowscale.length = sizeof(struct option_windowscale);
716 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
717 optionssize += sizeof(struct option_windowscale);
718
Faisal Latif6492cdf2008-07-24 20:50:45 -0700719 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800720 options = (union all_known_options *)&optionsbuffer[optionssize];
721 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
722 options->as_base.length = sizeof(struct option_base);
723 optionssize += sizeof(struct option_base);
724 /* we need the size to be a multiple of 4 */
725 options = (union all_known_options *)&optionsbuffer[optionssize];
726 options->as_end = 1;
727 optionssize += 1;
728 options = (union all_known_options *)&optionsbuffer[optionssize];
729 options->as_end = 1;
730 optionssize += 1;
731 }
732
733 options = (union all_known_options *)&optionsbuffer[optionssize];
734 options->as_end = OPTION_NUMBER_END;
735 optionssize += 1;
736
Faisal Latif6492cdf2008-07-24 20:50:45 -0700737 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800738 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800739 if (!skb) {
740 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
741 return -1;
742 }
743
744 if (sendack)
745 flags |= SET_ACK;
746
747 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
748 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
749
750 return ret;
751}
752
753
754/**
755 * send_reset
756 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700757static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800758{
759 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800760 int flags = SET_RST | SET_ACK;
761
Faisal Latif6492cdf2008-07-24 20:50:45 -0700762 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800763 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800764 if (!skb) {
765 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -0800766 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800767 }
768
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800769 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
770 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
771
772 return ret;
773}
774
775
776/**
777 * send_ack
778 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700779static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800780{
781 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700782
783 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800784 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800785
786 if (!skb) {
787 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
788 return -1;
789 }
790
791 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
792 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
793
794 return ret;
795}
796
797
798/**
799 * send_fin
800 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700801static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800802{
803 int ret;
804
805 /* if we didn't get a frame get one */
806 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800807 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800808
809 if (!skb) {
810 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
811 return -1;
812 }
813
814 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
815 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
816
817 return ret;
818}
819
820
821/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800822 * find_node - find a cm node that matches the reference cm node
823 */
824static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
825 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
826{
827 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800828 struct list_head *hte;
829 struct nes_cm_node *cm_node;
830
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800831 /* get a handle on the hte */
832 hte = &cm_core->connected_nodes;
833
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800834 /* walk list and find cm_node associated with this session ID */
835 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700836 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800837 /* compare quad, return node handle if a match */
838 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
839 cm_node->loc_addr, cm_node->loc_port,
840 loc_addr, loc_port,
841 cm_node->rem_addr, cm_node->rem_port,
842 rem_addr, rem_port);
843 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
844 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
845 add_ref_cm_node(cm_node);
846 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
847 return cm_node;
848 }
849 }
850 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
851
852 /* no owner node */
853 return NULL;
854}
855
856
857/**
858 * find_listener - find a cm node listening on this addr-port pair
859 */
860static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
861 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
862{
863 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800864 struct nes_cm_listener *listen_node;
865
866 /* walk list and find cm_node associated with this session ID */
867 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700868 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800869 /* compare node pair, return node handle if a match */
870 if (((listen_node->loc_addr == dst_addr) ||
871 listen_node->loc_addr == 0x00000000) &&
872 (listen_node->loc_port == dst_port) &&
873 (listener_state & listen_node->listener_state)) {
874 atomic_inc(&listen_node->ref_count);
875 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
876 return listen_node;
877 }
878 }
879 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
880
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800881 /* no listener */
882 return NULL;
883}
884
885
886/**
887 * add_hte_node - add a cm node to the hash table
888 */
889static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
890{
891 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800892 struct list_head *hte;
893
894 if (!cm_node || !cm_core)
895 return -EINVAL;
896
Faisal Latif6492cdf2008-07-24 20:50:45 -0700897 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
898 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800899
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800900 spin_lock_irqsave(&cm_core->ht_lock, flags);
901
902 /* get a handle on the hash table element (list head for this slot) */
903 hte = &cm_core->connected_nodes;
904 list_add_tail(&cm_node->list, hte);
905 atomic_inc(&cm_core->ht_node_cnt);
906
907 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
908
909 return 0;
910}
911
912
913/**
914 * mini_cm_dec_refcnt_listen
915 */
916static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700917 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800918{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800919 int ret = -EINVAL;
920 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800921 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700922 struct list_head *list_pos = NULL;
923 struct list_head *list_temp = NULL;
924 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -0600925 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700926
927 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
928 "refcnt=%d\n", listener, free_hanging_nodes,
929 atomic_read(&listener->ref_count));
930 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -0600931 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700932 if (free_hanging_nodes) {
933 spin_lock_irqsave(&cm_core->ht_lock, flags);
934 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600935 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700936 cm_node = container_of(list_pos, struct nes_cm_node,
937 list);
938 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -0600939 (!cm_node->accelerated)) {
940 add_ref_cm_node(cm_node);
941 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700942 }
943 }
944 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
945 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600946
947 list_for_each_safe(list_pos, list_temp, &reset_list) {
948 cm_node = container_of(list_pos, struct nes_cm_node,
Faisal Latif9d5ab132009-03-06 15:15:01 -0800949 reset_entry);
950 {
951 struct nes_cm_node *loopback = cm_node->loopbackpartner;
952 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
953 rem_ref_cm_node(cm_node->cm_core, cm_node);
954 } else {
955 if (!loopback) {
956 cleanup_retrans_entry(cm_node);
957 err = send_reset(cm_node, NULL);
958 if (err) {
959 cm_node->state =
960 NES_CM_STATE_CLOSED;
961 WARN_ON(1);
962 } else {
963 cm_node->state =
964 NES_CM_STATE_CLOSED;
965 rem_ref_cm_node(
966 cm_node->cm_core,
967 cm_node);
968 }
969 } else {
970 struct nes_cm_event event;
971
972 event.cm_node = loopback;
973 event.cm_info.rem_addr =
974 loopback->rem_addr;
975 event.cm_info.loc_addr =
976 loopback->loc_addr;
977 event.cm_info.rem_port =
978 loopback->rem_port;
979 event.cm_info.loc_port =
980 loopback->loc_port;
981 event.cm_info.cm_id = loopback->cm_id;
982 cm_event_connect_error(&event);
983 loopback->state = NES_CM_STATE_CLOSED;
984
985 event.cm_node = cm_node;
986 event.cm_info.rem_addr =
987 cm_node->rem_addr;
988 event.cm_info.loc_addr =
989 cm_node->loc_addr;
990 event.cm_info.rem_port =
991 cm_node->rem_port;
992 event.cm_info.loc_port =
993 cm_node->loc_port;
994 event.cm_info.cm_id = cm_node->cm_id;
995 cm_event_reset(&event);
996
997 rem_ref_cm_node(cm_node->cm_core,
998 cm_node);
999
1000 }
1001 }
1002 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001003 }
1004
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001005 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1006 if (!atomic_dec_return(&listener->ref_count)) {
1007 list_del(&listener->list);
1008
1009 /* decrement our listen node count */
1010 atomic_dec(&cm_core->listen_node_cnt);
1011
1012 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1013
1014 if (listener->nesvnic) {
1015 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1016 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1017 }
1018
1019 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1020
1021 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001022 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001023 ret = 0;
1024 cm_listens_destroyed++;
1025 } else {
1026 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1027 }
1028 if (listener) {
1029 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1030 nes_debug(NES_DBG_CM, "destroying listener (%p)"
1031 " with non-zero pending accepts=%u\n",
1032 listener, atomic_read(&listener->pend_accepts_cnt));
1033 }
1034
1035 return ret;
1036}
1037
1038
1039/**
1040 * mini_cm_del_listen
1041 */
1042static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1043 struct nes_cm_listener *listener)
1044{
1045 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1046 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1047 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1048}
1049
1050
1051/**
1052 * mini_cm_accelerated
1053 */
1054static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1055 struct nes_cm_node *cm_node)
1056{
1057 u32 was_timer_set;
1058 cm_node->accelerated = 1;
1059
1060 if (cm_node->accept_pend) {
1061 BUG_ON(!cm_node->listener);
1062 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001063 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001064 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1065 }
1066
1067 was_timer_set = timer_pending(&cm_core->tcp_timer);
1068 if (!was_timer_set) {
1069 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1070 add_timer(&cm_core->tcp_timer);
1071 }
1072
1073 return 0;
1074}
1075
1076
1077/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001078 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001079 */
Bob Sharp7191a0a2008-10-03 12:21:19 -07001080static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001081{
1082 struct rtable *rt;
1083 struct flowi fl;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001084 struct neighbour *neigh;
1085 int rc = -1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001086
1087 memset(&fl, 0, sizeof fl);
1088 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1089 if (ip_route_output_key(&init_net, &rt, &fl)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001090 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001091 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001092 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001093 }
1094
Bob Sharp7191a0a2008-10-03 12:21:19 -07001095 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1096 if (neigh) {
1097 if (neigh->nud_state & NUD_VALID) {
1098 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001099 " is %pM, Gateway is 0x%08X \n", dst_ip,
1100 neigh->ha, ntohl(rt->rt_gateway));
Bob Sharp7191a0a2008-10-03 12:21:19 -07001101 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1102 dst_ip, NES_ARP_ADD);
1103 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1104 NES_ARP_RESOLVE);
1105 }
1106 neigh_release(neigh);
1107 }
1108
1109 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1110 neigh_event_send(rt->u.dst.neighbour, NULL);
1111
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001112 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001113 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001114}
1115
1116
1117/**
1118 * make_cm_node - create a new instance of a cm node
1119 */
1120static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1121 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1122 struct nes_cm_listener *listener)
1123{
1124 struct nes_cm_node *cm_node;
1125 struct timespec ts;
1126 int arpindex = 0;
1127 struct nes_device *nesdev;
1128 struct nes_adapter *nesadapter;
1129
1130 /* create an hte and cm_node for this instance */
1131 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1132 if (!cm_node)
1133 return NULL;
1134
1135 /* set our node specific transport info */
1136 cm_node->loc_addr = cm_info->loc_addr;
1137 cm_node->rem_addr = cm_info->rem_addr;
1138 cm_node->loc_port = cm_info->loc_port;
1139 cm_node->rem_port = cm_info->rem_port;
1140 cm_node->send_write0 = send_first;
Harvey Harrison63779432008-10-31 00:56:00 -07001141 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1142 &cm_node->loc_addr, cm_node->loc_port,
1143 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001144 cm_node->listener = listener;
1145 cm_node->netdev = nesvnic->netdev;
1146 cm_node->cm_id = cm_info->cm_id;
1147 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1148
Faisal Latif6492cdf2008-07-24 20:50:45 -07001149 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1150 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001151
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001152 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001153
1154 cm_node->loopbackpartner = NULL;
1155 atomic_set(&cm_node->ref_count, 1);
1156 /* associate our parent CM core */
1157 cm_node->cm_core = cm_core;
1158 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1159 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1160 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1161 NES_CM_DEFAULT_RCV_WND_SCALE;
1162 ts = current_kernel_time();
1163 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1164 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Chien Tungf2b2b592008-03-20 19:55:30 -05001165 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001166 cm_node->tcp_cntxt.rcv_nxt = 0;
1167 /* get a unique session ID , add thread_id to an upcounter to handle race */
1168 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001169 cm_node->conn_type = cm_info->conn_type;
1170 cm_node->apbvt_set = 0;
1171 cm_node->accept_pend = 0;
1172
1173 cm_node->nesvnic = nesvnic;
1174 /* get some device handles, for arp lookup */
1175 nesdev = nesvnic->nesdev;
1176 nesadapter = nesdev->nesadapter;
1177
1178 cm_node->loopbackpartner = NULL;
1179 /* get the mac addr for the remote node */
Chien Tung1ee86552008-11-21 20:51:04 -06001180 if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1181 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
1182 else
1183 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001184 if (arpindex < 0) {
Bob Sharp7191a0a2008-10-03 12:21:19 -07001185 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr);
1186 if (arpindex < 0) {
1187 kfree(cm_node);
1188 return NULL;
1189 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001190 }
1191
1192 /* copy the mac addr to node context */
1193 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001194 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1195 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001196
1197 add_hte_node(cm_core, cm_node);
1198 atomic_inc(&cm_nodes_created);
1199
1200 return cm_node;
1201}
1202
1203
1204/**
1205 * add_ref_cm_node - destroy an instance of a cm node
1206 */
1207static int add_ref_cm_node(struct nes_cm_node *cm_node)
1208{
1209 atomic_inc(&cm_node->ref_count);
1210 return 0;
1211}
1212
1213
1214/**
1215 * rem_ref_cm_node - destroy an instance of a cm node
1216 */
1217static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001218 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001219{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001220 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001221 struct nes_qp *nesqp;
1222
1223 if (!cm_node)
1224 return -EINVAL;
1225
1226 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1227 if (atomic_dec_return(&cm_node->ref_count)) {
1228 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1229 return 0;
1230 }
1231 list_del(&cm_node->list);
1232 atomic_dec(&cm_core->ht_node_cnt);
1233 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1234
1235 /* if the node is destroyed before connection was accelerated */
1236 if (!cm_node->accelerated && cm_node->accept_pend) {
1237 BUG_ON(!cm_node->listener);
1238 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1239 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1240 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001241 WARN_ON(cm_node->send_entry);
1242 if (cm_node->recv_entry)
1243 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001244 if (cm_node->listener) {
1245 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1246 } else {
1247 if (cm_node->apbvt_set && cm_node->nesvnic) {
1248 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001249 PCI_FUNC(
1250 cm_node->nesvnic->nesdev->pcidev->devfn),
1251 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001252 }
1253 }
1254
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001255 atomic_dec(&cm_core->node_cnt);
1256 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001257 nesqp = cm_node->nesqp;
1258 if (nesqp) {
1259 nesqp->cm_node = NULL;
1260 nes_rem_ref(&nesqp->ibqp);
1261 cm_node->nesqp = NULL;
1262 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001263
Faisal Latif6492cdf2008-07-24 20:50:45 -07001264 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001265 return 0;
1266}
1267
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001268/**
1269 * process_options
1270 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001271static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1272 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001273{
1274 u32 tmp;
1275 u32 offset = 0;
1276 union all_known_options *all_options;
1277 char got_mss_option = 0;
1278
1279 while (offset < optionsize) {
1280 all_options = (union all_known_options *)(optionsloc + offset);
1281 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001282 case OPTION_NUMBER_END:
1283 offset = optionsize;
1284 break;
1285 case OPTION_NUMBER_NONE:
1286 offset += 1;
1287 continue;
1288 case OPTION_NUMBER_MSS:
1289 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1290 "Size: %d\n", __func__,
1291 all_options->as_mss.length, offset, optionsize);
1292 got_mss_option = 1;
1293 if (all_options->as_mss.length != 4) {
1294 return 1;
1295 } else {
1296 tmp = ntohs(all_options->as_mss.mss);
1297 if (tmp > 0 && tmp <
1298 cm_node->tcp_cntxt.mss)
1299 cm_node->tcp_cntxt.mss = tmp;
1300 }
1301 break;
1302 case OPTION_NUMBER_WINDOW_SCALE:
1303 cm_node->tcp_cntxt.snd_wscale =
1304 all_options->as_windowscale.shiftcount;
1305 break;
1306 case OPTION_NUMBER_WRITE0:
1307 cm_node->send_write0 = 1;
1308 break;
1309 default:
1310 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1311 all_options->as_base.optionnum);
1312 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001313 }
1314 offset += all_options->as_base.length;
1315 }
1316 if ((!got_mss_option) && (syn_packet))
1317 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1318 return 0;
1319}
1320
Faisal Latif6492cdf2008-07-24 20:50:45 -07001321static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001322{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001323 atomic_inc(&cm_accel_dropped_pkts);
1324 dev_kfree_skb_any(skb);
1325}
1326
Faisal Latif9d5ab132009-03-06 15:15:01 -08001327static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001328{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001329 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1330 "refcnt=%d\n", cm_node, cm_node->state,
1331 atomic_read(&cm_node->ref_count));
Faisal Latif6492cdf2008-07-24 20:50:45 -07001332 switch (cm_node->state) {
1333 case NES_CM_STATE_SYN_RCVD:
1334 case NES_CM_STATE_SYN_SENT:
1335 case NES_CM_STATE_ESTABLISHED:
1336 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001337 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif109d67e2009-04-27 13:41:06 -07001338 cm_node->tcp_cntxt.rcv_nxt++;
1339 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001340 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001341 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001342 break;
1343 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001344 cm_node->tcp_cntxt.rcv_nxt++;
1345 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001346 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001347 send_ack(cm_node, NULL);
1348 /* Wait for ACK as this is simultanous close..
1349 * After we receive ACK, do not send anything..
1350 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001351 break;
1352 case NES_CM_STATE_FIN_WAIT2:
Faisal Latif109d67e2009-04-27 13:41:06 -07001353 cm_node->tcp_cntxt.rcv_nxt++;
1354 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001355 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001356 send_ack(cm_node, NULL);
1357 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1358 break;
1359 case NES_CM_STATE_TIME_WAIT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001360 cm_node->tcp_cntxt.rcv_nxt++;
1361 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001362 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001363 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001364 break;
1365 case NES_CM_STATE_TSA:
1366 default:
1367 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1368 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001369 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001370 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001371}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001372
Faisal Latif6492cdf2008-07-24 20:50:45 -07001373
1374static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1375 struct tcphdr *tcph)
1376{
1377
1378 int reset = 0; /* whether to send reset in case of err.. */
Faisal Latif183ecfa2008-11-21 20:50:46 -06001379 int passive_state;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001380 atomic_inc(&cm_resets_recvd);
1381 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1382 " refcnt=%d\n", cm_node, cm_node->state,
1383 atomic_read(&cm_node->ref_count));
1384 cleanup_retrans_entry(cm_node);
1385 switch (cm_node->state) {
1386 case NES_CM_STATE_SYN_SENT:
1387 case NES_CM_STATE_MPAREQ_SENT:
1388 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1389 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1390 cm_node->listener, cm_node->state);
1391 active_open_err(cm_node, skb, reset);
1392 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001393 case NES_CM_STATE_MPAREQ_RCVD:
1394 passive_state = atomic_add_return(1, &cm_node->passive_state);
1395 if (passive_state == NES_SEND_RESET_EVENT)
1396 create_event(cm_node, NES_CM_EVENT_RESET);
Faisal Latif183ecfa2008-11-21 20:50:46 -06001397 cm_node->state = NES_CM_STATE_CLOSED;
1398 dev_kfree_skb_any(skb);
1399 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001400 case NES_CM_STATE_ESTABLISHED:
1401 case NES_CM_STATE_SYN_RCVD:
1402 case NES_CM_STATE_LISTENING:
1403 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1404 passive_open_err(cm_node, skb, reset);
1405 break;
1406 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001407 active_open_err(cm_node, skb, reset);
1408 break;
1409 case NES_CM_STATE_CLOSED:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001410 drop_packet(skb);
1411 break;
Faisal Latif68237a02009-06-22 22:53:28 -07001412 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif109d67e2009-04-27 13:41:06 -07001413 case NES_CM_STATE_LAST_ACK:
1414 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001415 case NES_CM_STATE_TIME_WAIT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001416 cm_node->state = NES_CM_STATE_CLOSED;
1417 rem_ref_cm_node(cm_node->cm_core, cm_node);
1418 drop_packet(skb);
1419 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001420 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001421 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001422 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001423 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001424}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001425
Faisal Latif9d5ab132009-03-06 15:15:01 -08001426
1427static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001428{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001429
Faisal Latif9d5ab132009-03-06 15:15:01 -08001430 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001431 int datasize = skb->len;
1432 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001433
1434 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1435 u32 res_type;
1436 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1437 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001438 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001439 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001440 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1441 "cm_node=%p listener=%p state=%d\n", __func__,
1442 __LINE__, cm_node, cm_node->listener,
1443 cm_node->state);
1444 active_open_err(cm_node, skb, 1);
1445 } else {
1446 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001447 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001448 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001449 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001450
1451 switch (cm_node->state) {
1452 case NES_CM_STATE_ESTABLISHED:
1453 if (res_type == NES_MPA_REQUEST_REJECT) {
1454 /*BIG problem as we are receiving the MPA.. So should
1455 * not be REJECT.. This is Passive Open.. We can
1456 * only receive it Reject for Active Open...*/
1457 WARN_ON(1);
1458 }
1459 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1460 type = NES_CM_EVENT_MPA_REQ;
1461 atomic_set(&cm_node->passive_state,
1462 NES_PASSIVE_STATE_INDICATED);
1463 break;
1464 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif109d67e2009-04-27 13:41:06 -07001465 cleanup_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001466 if (res_type == NES_MPA_REQUEST_REJECT) {
1467 type = NES_CM_EVENT_MPA_REJECT;
1468 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1469 } else {
1470 type = NES_CM_EVENT_CONNECTED;
1471 cm_node->state = NES_CM_STATE_TSA;
1472 }
1473
1474 break;
1475 default:
1476 WARN_ON(1);
1477 break;
1478 }
1479 dev_kfree_skb_any(skb);
1480 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001481}
1482
1483static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1484{
1485 switch (cm_node->state) {
1486 case NES_CM_STATE_SYN_SENT:
1487 case NES_CM_STATE_MPAREQ_SENT:
1488 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1489 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1490 cm_node->listener, cm_node->state);
1491 active_open_err(cm_node, skb, 1);
1492 break;
1493 case NES_CM_STATE_ESTABLISHED:
1494 case NES_CM_STATE_SYN_RCVD:
1495 passive_open_err(cm_node, skb, 1);
1496 break;
1497 case NES_CM_STATE_TSA:
1498 default:
1499 drop_packet(skb);
1500 }
1501}
1502
1503static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1504 struct sk_buff *skb)
1505{
1506 int err;
1507
1508 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1509 if (err)
1510 active_open_err(cm_node, skb, 1);
1511
1512 return err;
1513}
1514
1515static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1516 struct sk_buff *skb)
1517{
1518 int err = 0;
1519 u32 seq;
1520 u32 ack_seq;
1521 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1522 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1523 u32 rcv_wnd;
1524 seq = ntohl(tcph->seq);
1525 ack_seq = ntohl(tcph->ack_seq);
1526 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1527 if (ack_seq != loc_seq_num)
1528 err = 1;
Faisal Latifc11470f2009-04-27 13:38:31 -07001529 else if (!between(seq, rcv_nxt, (rcv_nxt+rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001530 err = 1;
1531 if (err) {
1532 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1533 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1534 cm_node->listener, cm_node->state);
1535 indicate_pkt_err(cm_node, skb);
1536 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1537 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1538 rcv_wnd);
1539 }
1540 return err;
1541}
1542
1543/*
1544 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1545 * is created with a listener or it may comein as rexmitted packet which in
1546 * that case will be just dropped.
1547 */
1548
1549static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1550 struct tcphdr *tcph)
1551{
1552 int ret;
1553 u32 inc_sequence;
1554 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001555
1556 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001557 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001558 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001559
Faisal Latif6492cdf2008-07-24 20:50:45 -07001560 switch (cm_node->state) {
1561 case NES_CM_STATE_SYN_SENT:
1562 case NES_CM_STATE_MPAREQ_SENT:
1563 /* Rcvd syn on active open connection*/
1564 active_open_err(cm_node, skb, 1);
1565 break;
1566 case NES_CM_STATE_LISTENING:
1567 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001568 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1569 cm_node->listener->backlog) {
1570 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1571 "pressure \n");
1572 cm_backlog_drops++;
1573 passive_open_err(cm_node, skb, 0);
1574 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001575 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001576 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1577 1);
1578 if (ret) {
1579 passive_open_err(cm_node, skb, 0);
1580 /* drop pkt */
1581 break;
1582 }
1583 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1584 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001585 cm_node->accept_pend = 1;
1586 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1587
Faisal Latif6492cdf2008-07-24 20:50:45 -07001588 cm_node->state = NES_CM_STATE_SYN_RCVD;
1589 send_syn(cm_node, 1, skb);
1590 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001591 case NES_CM_STATE_CLOSED:
1592 cleanup_retrans_entry(cm_node);
1593 send_reset(cm_node, skb);
1594 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001595 case NES_CM_STATE_TSA:
1596 case NES_CM_STATE_ESTABLISHED:
1597 case NES_CM_STATE_FIN_WAIT1:
1598 case NES_CM_STATE_FIN_WAIT2:
1599 case NES_CM_STATE_MPAREQ_RCVD:
1600 case NES_CM_STATE_LAST_ACK:
1601 case NES_CM_STATE_CLOSING:
1602 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001603 default:
1604 drop_packet(skb);
1605 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001606 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001607}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001608
Faisal Latif6492cdf2008-07-24 20:50:45 -07001609static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1610 struct tcphdr *tcph)
1611{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001612
Faisal Latif6492cdf2008-07-24 20:50:45 -07001613 int ret;
1614 u32 inc_sequence;
1615 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001616
Faisal Latif6492cdf2008-07-24 20:50:45 -07001617 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001618 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001619 inc_sequence = ntohl(tcph->seq);
1620 switch (cm_node->state) {
1621 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001622 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001623 /* active open */
1624 if (check_syn(cm_node, tcph, skb))
1625 return;
1626 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1627 /* setup options */
1628 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1629 if (ret) {
1630 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1631 cm_node);
1632 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001633 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001634 cleanup_retrans_entry(cm_node);
1635 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1636 send_mpa_request(cm_node, skb);
1637 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1638 break;
1639 case NES_CM_STATE_MPAREQ_RCVD:
1640 /* passive open, so should not be here */
1641 passive_open_err(cm_node, skb, 1);
1642 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001643 case NES_CM_STATE_LISTENING:
1644 case NES_CM_STATE_CLOSED:
1645 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1646 cleanup_retrans_entry(cm_node);
1647 send_reset(cm_node, skb);
1648 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001649 case NES_CM_STATE_ESTABLISHED:
1650 case NES_CM_STATE_FIN_WAIT1:
1651 case NES_CM_STATE_FIN_WAIT2:
1652 case NES_CM_STATE_LAST_ACK:
1653 case NES_CM_STATE_TSA:
1654 case NES_CM_STATE_CLOSING:
1655 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001656 case NES_CM_STATE_MPAREQ_SENT:
1657 default:
1658 drop_packet(skb);
1659 break;
1660 }
1661}
1662
Faisal Latif109d67e2009-04-27 13:41:06 -07001663static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001664 struct tcphdr *tcph)
1665{
1666 int datasize = 0;
1667 u32 inc_sequence;
1668 u32 rem_seq_ack;
1669 u32 rem_seq;
Faisal Latif109d67e2009-04-27 13:41:06 -07001670 int ret = 0;
Faisal Latifabb77252008-11-21 20:50:52 -06001671 int optionsize;
Faisal Latifabb77252008-11-21 20:50:52 -06001672 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001673
Faisal Latif6492cdf2008-07-24 20:50:45 -07001674 if (check_seq(cm_node, tcph, skb))
Faisal Latif109d67e2009-04-27 13:41:06 -07001675 return -EINVAL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001676
1677 skb_pull(skb, tcph->doff << 2);
1678 inc_sequence = ntohl(tcph->seq);
1679 rem_seq = ntohl(tcph->seq);
1680 rem_seq_ack = ntohl(tcph->ack_seq);
1681 datasize = skb->len;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001682 switch (cm_node->state) {
1683 case NES_CM_STATE_SYN_RCVD:
1684 /* Passive OPEN */
Faisal Latif109d67e2009-04-27 13:41:06 -07001685 cleanup_retrans_entry(cm_node);
Faisal Latifabb77252008-11-21 20:50:52 -06001686 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1687 if (ret)
1688 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001689 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1690 cm_node->state = NES_CM_STATE_ESTABLISHED;
1691 if (datasize) {
1692 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001693 handle_rcv_mpa(cm_node, skb);
Faisal Latif109d67e2009-04-27 13:41:06 -07001694 } else /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001695 dev_kfree_skb_any(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001696 break;
1697 case NES_CM_STATE_ESTABLISHED:
1698 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001699 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001700 if (datasize) {
1701 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001702 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001703 } else
1704 drop_packet(skb);
1705 break;
1706 case NES_CM_STATE_MPAREQ_SENT:
1707 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 Latif109d67e2009-04-27 13:41:06 -07001711 } else /* Could be just an ack pkt.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001712 dev_kfree_skb_any(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001713 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001714 case NES_CM_STATE_LISTENING:
1715 case NES_CM_STATE_CLOSED:
1716 cleanup_retrans_entry(cm_node);
1717 send_reset(cm_node, skb);
1718 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001719 case NES_CM_STATE_LAST_ACK:
Faisal Latif109d67e2009-04-27 13:41:06 -07001720 case NES_CM_STATE_CLOSING:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001721 cleanup_retrans_entry(cm_node);
1722 cm_node->state = NES_CM_STATE_CLOSED;
1723 cm_node->cm_id->rem_ref(cm_node->cm_id);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001724 rem_ref_cm_node(cm_node->cm_core, cm_node);
1725 drop_packet(skb);
1726 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001727 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001728 cleanup_retrans_entry(cm_node);
1729 drop_packet(skb);
1730 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1731 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001732 case NES_CM_STATE_SYN_SENT:
1733 case NES_CM_STATE_FIN_WAIT2:
1734 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001735 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001736 case NES_CM_STATE_UNKNOWN:
1737 default:
Faisal Latif109d67e2009-04-27 13:41:06 -07001738 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001739 drop_packet(skb);
1740 break;
1741 }
Faisal Latif109d67e2009-04-27 13:41:06 -07001742 return ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001743}
1744
1745
1746
1747static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1748 struct sk_buff *skb, int optionsize, int passive)
1749{
1750 u8 *optionsloc = (u8 *)&tcph[1];
1751 if (optionsize) {
1752 if (process_options(cm_node, optionsloc, optionsize,
1753 (u32)tcph->syn)) {
1754 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1755 __func__, cm_node);
1756 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06001757 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001758 else
Faisal Latifabb77252008-11-21 20:50:52 -06001759 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001760 return 1;
1761 }
1762 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001763
1764 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1765 cm_node->tcp_cntxt.snd_wscale;
1766
Faisal Latif6492cdf2008-07-24 20:50:45 -07001767 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001768 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001769 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001770}
1771
Faisal Latif6492cdf2008-07-24 20:50:45 -07001772/*
1773 * active_open_err() will send reset() if flag set..
1774 * It will also send ABORT event.
1775 */
1776
1777static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1778 int reset)
1779{
1780 cleanup_retrans_entry(cm_node);
1781 if (reset) {
1782 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1783 "state=%d\n", cm_node, cm_node->state);
1784 add_ref_cm_node(cm_node);
1785 send_reset(cm_node, skb);
1786 } else
1787 dev_kfree_skb_any(skb);
1788
1789 cm_node->state = NES_CM_STATE_CLOSED;
1790 create_event(cm_node, NES_CM_EVENT_ABORTED);
1791}
1792
1793/*
1794 * passive_open_err() will either do a reset() or will free up the skb and
1795 * remove the cm_node.
1796 */
1797
1798static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1799 int reset)
1800{
1801 cleanup_retrans_entry(cm_node);
1802 cm_node->state = NES_CM_STATE_CLOSED;
1803 if (reset) {
1804 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1805 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1806 send_reset(cm_node, skb);
1807 } else {
1808 dev_kfree_skb_any(skb);
1809 rem_ref_cm_node(cm_node->cm_core, cm_node);
1810 }
1811}
1812
1813/*
1814 * free_retrans_entry() routines assumes that the retrans_list_lock has
1815 * been acquired before calling.
1816 */
1817static void free_retrans_entry(struct nes_cm_node *cm_node)
1818{
1819 struct nes_timer_entry *send_entry;
1820 send_entry = cm_node->send_entry;
1821 if (send_entry) {
1822 cm_node->send_entry = NULL;
1823 dev_kfree_skb_any(send_entry->skb);
1824 kfree(send_entry);
1825 rem_ref_cm_node(cm_node->cm_core, cm_node);
1826 }
1827}
1828
1829static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1830{
1831 unsigned long flags;
1832
1833 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1834 free_retrans_entry(cm_node);
1835 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1836}
1837
1838/**
1839 * process_packet
1840 * Returns skb if to be freed, else it will return NULL if already used..
1841 */
1842static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1843 struct nes_cm_core *cm_core)
1844{
1845 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1846 struct tcphdr *tcph = tcp_hdr(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001847 u32 fin_set = 0;
Faisal Latif109d67e2009-04-27 13:41:06 -07001848 int ret = 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:
Faisal Latif109d67e2009-04-27 13:41:06 -07001874 ret = handle_ack_pkt(cm_node, skb, tcph);
1875 if (fin_set && !ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08001876 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:
Faisal Latif109d67e2009-04-27 13:41:06 -07001882 if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
Faisal Latif9d5ab132009-03-06 15:15:01 -08001883 handle_fin_pkt(cm_node);
Faisal Latif109d67e2009-04-27 13:41:06 -07001884 drop_packet(skb);
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{
Don Wood873fcdd2009-09-05 20:36:37 -07002453 struct disconn_work *work;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002454
Don Wood873fcdd2009-09-05 20:36:37 -07002455 work = kzalloc(sizeof *work, GFP_ATOMIC);
2456 if (!work)
2457 return -ENOMEM; /* Timer will clean up */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002458
Don Wood873fcdd2009-09-05 20:36:37 -07002459 nes_add_ref(&nesqp->ibqp);
2460 work->nesqp = nesqp;
2461 INIT_WORK(&work->work, nes_disconnect_worker);
2462 queue_work(g_cm_core->disconn_wq, &work->work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002463 return 0;
2464}
2465
2466
2467/**
2468 * nes_disconnect_worker
2469 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002470static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002471{
Don Wood873fcdd2009-09-05 20:36:37 -07002472 struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2473 struct nes_qp *nesqp = dwork->nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002474
Don Wood873fcdd2009-09-05 20:36:37 -07002475 kfree(dwork);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002476 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2477 nesqp->last_aeq, nesqp->hwqp.qp_id);
2478 nes_cm_disconn_true(nesqp);
Don Woodc4c3f272009-09-05 20:36:36 -07002479 nes_rem_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002480}
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
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002558 /* There might have been another AE while the lock was released */
2559 original_hw_tcp_state = nesqp->hw_tcp_state;
2560 original_ibqp_state = nesqp->ibqp_state;
2561 last_ae = nesqp->last_aeq;
2562
2563 if ((issued_disconnect_reset == 0) && (nesqp->cm_id) &&
2564 ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2565 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2566 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2567 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2568 atomic_inc(&cm_closes);
2569 nesqp->cm_id = NULL;
2570 nesqp->in_disconnect = 0;
2571 spin_unlock_irqrestore(&nesqp->lock, flags);
2572 nes_disconnect(nesqp, 1);
2573
2574 cm_id->provider_data = nesqp;
2575 /* Send up the close complete event */
2576 cm_event.event = IW_CM_EVENT_CLOSE;
2577 cm_event.status = IW_CM_EVENT_STATUS_OK;
2578 cm_event.provider_data = cm_id->provider_data;
2579 cm_event.local_addr = cm_id->local_addr;
2580 cm_event.remote_addr = cm_id->remote_addr;
2581 cm_event.private_data = NULL;
2582 cm_event.private_data_len = 0;
2583
2584 ret = cm_id->event_handler(cm_id, &cm_event);
2585 if (ret) {
2586 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2587 }
2588
2589 cm_id->rem_ref(cm_id);
2590
2591 spin_lock_irqsave(&nesqp->lock, flags);
2592 if (nesqp->flush_issued == 0) {
2593 nesqp->flush_issued = 1;
2594 spin_unlock_irqrestore(&nesqp->lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002595 flush_wqes(nesvnic->nesdev, nesqp,
2596 NES_CQP_FLUSH_RQ, 1);
2597 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002598 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002599 } else {
2600 cm_id = nesqp->cm_id;
2601 spin_unlock_irqrestore(&nesqp->lock, flags);
2602 /* check to see if the inbound reset beat the outbound reset */
2603 if ((!cm_id) && (last_ae==NES_AEQE_AEID_RESET_SENT)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002604 nes_debug(NES_DBG_CM, "QP%u: Decing refcount "
2605 "due to inbound reset beating the "
2606 "outbound reset.\n", nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002607 }
2608 }
2609 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002610 spin_unlock_irqrestore(&nesqp->lock, flags);
2611 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002612
2613 return 0;
2614}
2615
2616
2617/**
2618 * nes_disconnect
2619 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002620static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002621{
2622 int ret = 0;
2623 struct nes_vnic *nesvnic;
2624 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002625 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002626
2627 nesvnic = to_nesvnic(nesqp->ibqp.device);
2628 if (!nesvnic)
2629 return -EINVAL;
2630
2631 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002632 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002633
2634 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2635 atomic_read(&nesvnic->netdev->refcnt));
2636
2637 if (nesqp->active_conn) {
2638
2639 /* indicate this connection is NOT active */
2640 nesqp->active_conn = 0;
2641 } else {
2642 /* Need to free the Last Streaming Mode Message */
2643 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002644 if (nesqp->lsmm_mr)
2645 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002646 pci_free_consistent(nesdev->pcidev,
2647 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2648 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2649 }
2650 }
2651
2652 /* close the CM node down if it is still active */
2653 if (nesqp->cm_node) {
2654 nes_debug(NES_DBG_CM, "Call close API\n");
2655
2656 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002657 }
2658
2659 return ret;
2660}
2661
2662
2663/**
2664 * nes_accept
2665 */
2666int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2667{
2668 u64 u64temp;
2669 struct ib_qp *ibqp;
2670 struct nes_qp *nesqp;
2671 struct nes_vnic *nesvnic;
2672 struct nes_device *nesdev;
2673 struct nes_cm_node *cm_node;
2674 struct nes_adapter *adapter;
2675 struct ib_qp_attr attr;
2676 struct iw_cm_event cm_event;
2677 struct nes_hw_qp_wqe *wqe;
2678 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002679 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002680 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002681 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002682 struct nes_ib_device *nesibdev;
2683 struct ib_mr *ibmr = NULL;
2684 struct ib_phys_buf ibphysbuf;
2685 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002686 u64 tagged_offset;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002687
2688
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002689
2690 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2691 if (!ibqp)
2692 return -EINVAL;
2693
2694 /* get all our handles */
2695 nesqp = to_nesqp(ibqp);
2696 nesvnic = to_nesvnic(nesqp->ibqp.device);
2697 nesdev = nesvnic->nesdev;
2698 adapter = nesdev->nesadapter;
2699
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002700 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002701 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2702 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2703 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002704
2705 /* associate the node with the QP */
2706 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002707 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002708
Faisal Latif6492cdf2008-07-24 20:50:45 -07002709 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2710 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002711 atomic_inc(&cm_accepts);
2712
2713 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2714 atomic_read(&nesvnic->netdev->refcnt));
2715
Faisal Latif6492cdf2008-07-24 20:50:45 -07002716 /* allocate the ietf frame and space for private data */
2717 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2718 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2719 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002720
Faisal Latif6492cdf2008-07-24 20:50:45 -07002721 if (!nesqp->ietf_frame) {
2722 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2723 "data\n");
2724 return -ENOMEM;
2725 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002726
2727
Faisal Latif6492cdf2008-07-24 20:50:45 -07002728 /* setup the MPA frame */
2729 nesqp->private_data_len = conn_param->private_data_len;
2730 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002731
Faisal Latif6492cdf2008-07-24 20:50:45 -07002732 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2733 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002734
Faisal Latif6492cdf2008-07-24 20:50:45 -07002735 nesqp->ietf_frame->priv_data_len =
2736 cpu_to_be16(conn_param->private_data_len);
2737 nesqp->ietf_frame->rev = mpa_version;
2738 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002739
Faisal Latif6492cdf2008-07-24 20:50:45 -07002740 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2741 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002742
Faisal Latif6492cdf2008-07-24 20:50:45 -07002743 if (cm_id->remote_addr.sin_addr.s_addr !=
2744 cm_id->local_addr.sin_addr.s_addr) {
2745 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002746 nesibdev = nesvnic->nesibdev;
2747 nespd = nesqp->nespd;
2748 ibphysbuf.addr = nesqp->ietf_frame_pbase;
2749 ibphysbuf.size = conn_param->private_data_len +
2750 sizeof(struct ietf_mpa_frame);
Don Wood7a5efb62009-04-08 14:21:02 -07002751 tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002752 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
2753 &ibphysbuf, 1,
2754 IB_ACCESS_LOCAL_WRITE,
Don Wood7a5efb62009-04-08 14:21:02 -07002755 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002756 if (!ibmr) {
2757 nes_debug(NES_DBG_CM, "Unable to register memory region"
2758 "for lSMM for cm_node = %p \n",
2759 cm_node);
Faisal Latif9256b252009-04-27 13:45:19 -07002760 pci_free_consistent(nesdev->pcidev,
2761 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2762 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002763 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);
Faisal Latif9256b252009-04-27 13:45:19 -07002879 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002880
2881 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2882 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2883 cm_event.provider_data = (void *)nesqp;
2884 cm_event.local_addr = cm_id->local_addr;
2885 cm_event.remote_addr = cm_id->remote_addr;
2886 cm_event.private_data = NULL;
2887 cm_event.private_data_len = 0;
2888 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002889 attr.qp_state = IB_QPS_RTS;
2890 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002891 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002892 cm_node->loopbackpartner->mpa_frame_size =
2893 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002894 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002895 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2896 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002897 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2898 }
2899 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002900 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2901 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002902
Faisal Latif183ecfa2008-11-21 20:50:46 -06002903 passive_state = atomic_add_return(1, &cm_node->passive_state);
2904 if (passive_state == NES_SEND_RESET_EVENT)
2905 create_event(cm_node, NES_CM_EVENT_RESET);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002906 return 0;
2907}
2908
2909
2910/**
2911 * nes_reject
2912 */
2913int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2914{
2915 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002916 struct nes_cm_node *loopback;
2917
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002918 struct nes_cm_core *cm_core;
2919
2920 atomic_inc(&cm_rejects);
2921 cm_node = (struct nes_cm_node *) cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002922 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002923 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002924 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002925 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2926
Faisal Latif9d5ab132009-03-06 15:15:01 -08002927 if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
2928 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002929
Faisal Latif9d5ab132009-03-06 15:15:01 -08002930 strcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP);
2931 if (loopback) {
2932 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
2933 loopback->mpa_frame.priv_data_len = pdata_len;
2934 loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
2935 pdata_len;
2936 } else {
2937 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2938 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2939 }
2940
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002941 cm_node->mpa_frame.rev = mpa_version;
2942 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2943
Faisal Latif9d5ab132009-03-06 15:15:01 -08002944 return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002945}
2946
2947
2948/**
2949 * nes_connect
2950 * setup and launch cm connect node
2951 */
2952int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2953{
2954 struct ib_qp *ibqp;
2955 struct nes_qp *nesqp;
2956 struct nes_vnic *nesvnic;
2957 struct nes_device *nesdev;
2958 struct nes_cm_node *cm_node;
2959 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07002960 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002961
2962 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2963 if (!ibqp)
2964 return -EINVAL;
2965 nesqp = to_nesqp(ibqp);
2966 if (!nesqp)
2967 return -EINVAL;
2968 nesvnic = to_nesvnic(nesqp->ibqp.device);
2969 if (!nesvnic)
2970 return -EINVAL;
2971 nesdev = nesvnic->nesdev;
2972 if (!nesdev)
2973 return -EINVAL;
2974
Faisal Latif6492cdf2008-07-24 20:50:45 -07002975 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
2976 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
2977 ntohl(nesvnic->local_ipaddr),
2978 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2979 ntohs(cm_id->remote_addr.sin_port),
2980 ntohl(cm_id->local_addr.sin_addr.s_addr),
2981 ntohs(cm_id->local_addr.sin_port));
2982
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002983 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002984 nesqp->active_conn = 1;
2985
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002986 /* cache the cm_id in the qp */
2987 nesqp->cm_id = cm_id;
2988
2989 cm_id->provider_data = nesqp;
2990
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002991 nesqp->private_data_len = conn_param->private_data_len;
2992 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
2993 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002994 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
2995 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002996
Faisal Latif6492cdf2008-07-24 20:50:45 -07002997 if (cm_id->local_addr.sin_addr.s_addr !=
Faisal Latif53094c32009-04-27 13:37:34 -07002998 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002999 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003000 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003001 apbvt_set = 1;
3002 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003003
3004 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003005 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3006 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3007 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3008 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003009 cm_info.cm_id = cm_id;
3010 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3011
3012 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003013
3014 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003015 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3016 conn_param->private_data_len, (void *)conn_param->private_data,
3017 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003018 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003019 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003020 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003021 PCI_FUNC(nesdev->pcidev->devfn),
3022 NES_MANAGE_APBVT_DEL);
3023
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003024 cm_id->rem_ref(cm_id);
3025 return -ENOMEM;
3026 }
3027
Faisal Latif53094c32009-04-27 13:37:34 -07003028 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003029 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003030 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003031 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003032
3033 return 0;
3034}
3035
3036
3037/**
3038 * nes_create_listen
3039 */
3040int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3041{
3042 struct nes_vnic *nesvnic;
3043 struct nes_cm_listener *cm_node;
3044 struct nes_cm_info cm_info;
3045 struct nes_adapter *adapter;
3046 int err;
3047
3048
3049 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3050 cm_id, ntohs(cm_id->local_addr.sin_port));
3051
3052 nesvnic = to_nesvnic(cm_id->device);
3053 if (!nesvnic)
3054 return -EINVAL;
3055 adapter = nesvnic->nesdev->nesadapter;
3056 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3057 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3058
3059 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3060 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3061
3062 /* setup listen params in our api call struct */
3063 cm_info.loc_addr = nesvnic->local_ipaddr;
3064 cm_info.loc_port = cm_id->local_addr.sin_port;
3065 cm_info.backlog = backlog;
3066 cm_info.cm_id = cm_id;
3067
3068 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3069
3070
3071 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3072 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003073 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003074 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003075 return -ENOMEM;
3076 }
3077
3078 cm_id->provider_data = cm_node;
3079
3080 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003081 err = nes_manage_apbvt(nesvnic,
3082 ntohs(cm_id->local_addr.sin_port),
3083 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3084 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003085 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003086 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3087 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003088 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3089 return err;
3090 }
3091 cm_listens_created++;
3092 }
3093
3094 cm_id->add_ref(cm_id);
3095 cm_id->provider_data = (void *)cm_node;
3096
3097
3098 return 0;
3099}
3100
3101
3102/**
3103 * nes_destroy_listen
3104 */
3105int nes_destroy_listen(struct iw_cm_id *cm_id)
3106{
3107 if (cm_id->provider_data)
3108 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3109 else
3110 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3111
3112 cm_id->rem_ref(cm_id);
3113
3114 return 0;
3115}
3116
3117
3118/**
3119 * nes_cm_recv
3120 */
3121int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3122{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003123 int rc = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003124 cm_packets_received++;
3125 if ((g_cm_core) && (g_cm_core->api)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003126 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003127 } else {
3128 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3129 " cm is not setup properly.\n");
3130 }
3131
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003132 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003133}
3134
3135
3136/**
3137 * nes_cm_start
3138 * Start and init a cm core module
3139 */
3140int nes_cm_start(void)
3141{
3142 nes_debug(NES_DBG_CM, "\n");
3143 /* create the primary CM core, pass this handle to subsequent core inits */
3144 g_cm_core = nes_cm_alloc_core();
3145 if (g_cm_core) {
3146 return 0;
3147 } else {
3148 return -ENOMEM;
3149 }
3150}
3151
3152
3153/**
3154 * nes_cm_stop
3155 * stop and dealloc all cm core instances
3156 */
3157int nes_cm_stop(void)
3158{
3159 g_cm_core->api->destroy_cm_core(g_cm_core);
3160 return 0;
3161}
3162
3163
3164/**
3165 * cm_event_connected
3166 * handle a connected event, setup QPs and HW
3167 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003168static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003169{
3170 u64 u64temp;
3171 struct nes_qp *nesqp;
3172 struct nes_vnic *nesvnic;
3173 struct nes_device *nesdev;
3174 struct nes_cm_node *cm_node;
3175 struct nes_adapter *nesadapter;
3176 struct ib_qp_attr attr;
3177 struct iw_cm_id *cm_id;
3178 struct iw_cm_event cm_event;
3179 struct nes_hw_qp_wqe *wqe;
3180 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003181 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003182 int ret;
3183
3184 /* get all our handles */
3185 cm_node = event->cm_node;
3186 cm_id = cm_node->cm_id;
3187 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3188 nesqp = (struct nes_qp *)cm_id->provider_data;
3189 nesvnic = to_nesvnic(nesqp->ibqp.device);
3190 nesdev = nesvnic->nesdev;
3191 nesadapter = nesdev->nesadapter;
3192
3193 if (nesqp->destroyed) {
3194 return;
3195 }
3196 atomic_inc(&cm_connecteds);
3197 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3198 " local port 0x%04X. jiffies = %lu.\n",
3199 nesqp->hwqp.qp_id,
3200 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3201 ntohs(cm_id->remote_addr.sin_port),
3202 ntohs(cm_id->local_addr.sin_port),
3203 jiffies);
3204
3205 nes_cm_init_tsa_conn(nesqp, cm_node);
3206
3207 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003208 nesqp->nesqp_context->tcpPorts[0] =
3209 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3210 nesqp->nesqp_context->tcpPorts[1] =
3211 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3212 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3213 nesqp->nesqp_context->ip0 =
3214 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3215 else
3216 nesqp->nesqp_context->ip0 =
3217 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003218
3219 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003220 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3221 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003222 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003223 nes_arp_table(nesdev,
3224 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003225 NULL, NES_ARP_RESOLVE) << 16);
3226 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3227 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3228 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3229 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003230 cpu_to_le32((u32)1 <<
3231 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003232
3233 /* Adjust tail for not having a LSMM */
3234 nesqp->hwqp.sq_tail = 1;
3235
3236#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003237 if (cm_node->send_write0) {
3238 nes_debug(NES_DBG_CM, "Sending first write.\n");
3239 wqe = &nesqp->hwqp.sq_vbase[0];
3240 u64temp = (unsigned long)nesqp;
3241 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3242 set_wqe_64bit_value(wqe->wqe_words,
3243 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3244 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3245 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3246 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3247 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3248 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3249 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3250 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003251
Faisal Latif6492cdf2008-07-24 20:50:45 -07003252 /* use the reserved spot on the WQ for the extra first WQE */
3253 nesqp->nesqp_context->ird_ord_sizes &=
3254 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3255 NES_QPCONTEXT_ORDIRD_WRPDU |
3256 NES_QPCONTEXT_ORDIRD_ALSMM));
3257 nesqp->skip_lsmm = 1;
3258 nesqp->hwqp.sq_tail = 0;
3259 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3260 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3261 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003262#endif
3263
3264 memset(&nes_quad, 0, sizeof(nes_quad));
3265
Faisal Latif6492cdf2008-07-24 20:50:45 -07003266 nes_quad.DstIpAdrIndex =
3267 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3268 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3269 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3270 else
3271 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003272 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3273 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3274
3275 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003276 crc_value = get_crc_value(&nes_quad);
3277 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003278 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3279 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3280
3281 nesqp->hte_index &= nesadapter->hte_index_mask;
3282 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3283
3284 nesqp->ietf_frame = &cm_node->mpa_frame;
3285 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3286 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3287
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003288 /* notify OF layer we successfully created the requested connection */
3289 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3290 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3291 cm_event.provider_data = cm_id->provider_data;
3292 cm_event.local_addr.sin_family = AF_INET;
3293 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3294 cm_event.remote_addr = cm_id->remote_addr;
3295
Faisal Latif6492cdf2008-07-24 20:50:45 -07003296 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3297 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003298
3299 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3300 ret = cm_id->event_handler(cm_id, &cm_event);
3301 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3302
3303 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003304 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3305 "ret=%d\n", __func__, __LINE__, ret);
3306 attr.qp_state = IB_QPS_RTS;
3307 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003308
Faisal Latif6492cdf2008-07-24 20:50:45 -07003309 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3310 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003311
3312 return;
3313}
3314
3315
3316/**
3317 * cm_event_connect_error
3318 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003319static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003320{
3321 struct nes_qp *nesqp;
3322 struct iw_cm_id *cm_id;
3323 struct iw_cm_event cm_event;
3324 /* struct nes_cm_info cm_info; */
3325 int ret;
3326
3327 if (!event->cm_node)
3328 return;
3329
3330 cm_id = event->cm_node->cm_id;
3331 if (!cm_id) {
3332 return;
3333 }
3334
3335 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3336 nesqp = cm_id->provider_data;
3337
3338 if (!nesqp) {
3339 return;
3340 }
3341
3342 /* notify OF layer about this connection error event */
3343 /* cm_id->rem_ref(cm_id); */
3344 nesqp->cm_id = NULL;
3345 cm_id->provider_data = NULL;
3346 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3347 cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
3348 cm_event.provider_data = cm_id->provider_data;
3349 cm_event.local_addr = cm_id->local_addr;
3350 cm_event.remote_addr = cm_id->remote_addr;
3351 cm_event.private_data = NULL;
3352 cm_event.private_data_len = 0;
3353
Faisal Latif6492cdf2008-07-24 20:50:45 -07003354 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3355 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3356 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003357
3358 ret = cm_id->event_handler(cm_id, &cm_event);
3359 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3360 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003361 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3362 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003363 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003364
Faisal Latif6492cdf2008-07-24 20:50:45 -07003365 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003366 return;
3367}
3368
3369
3370/**
3371 * cm_event_reset
3372 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003373static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003374{
3375 struct nes_qp *nesqp;
3376 struct iw_cm_id *cm_id;
3377 struct iw_cm_event cm_event;
3378 /* struct nes_cm_info cm_info; */
3379 int ret;
3380
3381 if (!event->cm_node)
3382 return;
3383
3384 if (!event->cm_node->cm_id)
3385 return;
3386
3387 cm_id = event->cm_node->cm_id;
3388
3389 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3390 nesqp = cm_id->provider_data;
3391
3392 nesqp->cm_id = NULL;
3393 /* cm_id->provider_data = NULL; */
3394 cm_event.event = IW_CM_EVENT_DISCONNECT;
3395 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3396 cm_event.provider_data = cm_id->provider_data;
3397 cm_event.local_addr = cm_id->local_addr;
3398 cm_event.remote_addr = cm_id->remote_addr;
3399 cm_event.private_data = NULL;
3400 cm_event.private_data_len = 0;
3401
3402 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003403 cm_id->add_ref(cm_id);
3404 atomic_inc(&cm_closes);
3405 cm_event.event = IW_CM_EVENT_CLOSE;
3406 cm_event.status = IW_CM_EVENT_STATUS_OK;
3407 cm_event.provider_data = cm_id->provider_data;
3408 cm_event.local_addr = cm_id->local_addr;
3409 cm_event.remote_addr = cm_id->remote_addr;
3410 cm_event.private_data = NULL;
3411 cm_event.private_data_len = 0;
3412 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3413 ret = cm_id->event_handler(cm_id, &cm_event);
3414
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003415 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3416
3417
3418 /* notify OF layer about this connection error event */
3419 cm_id->rem_ref(cm_id);
3420
3421 return;
3422}
3423
3424
3425/**
3426 * cm_event_mpa_req
3427 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003428static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003429{
3430 struct iw_cm_id *cm_id;
3431 struct iw_cm_event cm_event;
3432 int ret;
3433 struct nes_cm_node *cm_node;
3434
3435 cm_node = event->cm_node;
3436 if (!cm_node)
3437 return;
3438 cm_id = cm_node->cm_id;
3439
3440 atomic_inc(&cm_connect_reqs);
3441 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3442 cm_node, cm_id, jiffies);
3443
3444 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3445 cm_event.status = IW_CM_EVENT_STATUS_OK;
3446 cm_event.provider_data = (void *)cm_node;
3447
3448 cm_event.local_addr.sin_family = AF_INET;
3449 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3450 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3451
3452 cm_event.remote_addr.sin_family = AF_INET;
3453 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3454 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003455 cm_event.private_data = cm_node->mpa_frame_buf;
3456 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003457
3458 ret = cm_id->event_handler(cm_id, &cm_event);
3459 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003460 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3461 __func__, __LINE__, ret);
3462 return;
3463}
3464
3465
3466static void cm_event_mpa_reject(struct nes_cm_event *event)
3467{
3468 struct iw_cm_id *cm_id;
3469 struct iw_cm_event cm_event;
3470 struct nes_cm_node *cm_node;
3471 int ret;
3472
3473 cm_node = event->cm_node;
3474 if (!cm_node)
3475 return;
3476 cm_id = cm_node->cm_id;
3477
3478 atomic_inc(&cm_connect_reqs);
3479 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3480 cm_node, cm_id, jiffies);
3481
3482 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3483 cm_event.status = -ECONNREFUSED;
3484 cm_event.provider_data = cm_id->provider_data;
3485
3486 cm_event.local_addr.sin_family = AF_INET;
3487 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3488 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3489
3490 cm_event.remote_addr.sin_family = AF_INET;
3491 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3492 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3493
3494 cm_event.private_data = cm_node->mpa_frame_buf;
3495 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3496
3497 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3498 "remove_addr=%08x\n",
3499 cm_event.local_addr.sin_addr.s_addr,
3500 cm_event.remote_addr.sin_addr.s_addr);
3501
3502 ret = cm_id->event_handler(cm_id, &cm_event);
3503 if (ret)
3504 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003505 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003506
3507 return;
3508}
3509
3510
3511static void nes_cm_event_handler(struct work_struct *);
3512
3513/**
3514 * nes_cm_post_event
3515 * post an event to the cm event handler
3516 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003517static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003518{
3519 atomic_inc(&event->cm_node->cm_core->events_posted);
3520 add_ref_cm_node(event->cm_node);
3521 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3522 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003523 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3524 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003525
3526 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3527
3528 nes_debug(NES_DBG_CM, "Exit\n");
3529 return 0;
3530}
3531
3532
3533/**
3534 * nes_cm_event_handler
3535 * worker function to handle cm events
3536 * will free instance of nes_cm_event
3537 */
3538static void nes_cm_event_handler(struct work_struct *work)
3539{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003540 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3541 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003542 struct nes_cm_core *cm_core;
3543
Faisal Latif6492cdf2008-07-24 20:50:45 -07003544 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003545 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003546
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003547 cm_core = event->cm_node->cm_core;
3548 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003549 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003550
3551 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003552 case NES_CM_EVENT_MPA_REQ:
3553 cm_event_mpa_req(event);
3554 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3555 event->cm_node);
3556 break;
3557 case NES_CM_EVENT_RESET:
3558 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3559 event->cm_node);
3560 cm_event_reset(event);
3561 break;
3562 case NES_CM_EVENT_CONNECTED:
3563 if ((!event->cm_node->cm_id) ||
3564 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003565 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003566 cm_event_connected(event);
3567 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3568 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003569 case NES_CM_EVENT_MPA_REJECT:
3570 if ((!event->cm_node->cm_id) ||
3571 (event->cm_node->state == NES_CM_STATE_TSA))
3572 break;
3573 cm_event_mpa_reject(event);
3574 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3575 break;
3576
Faisal Latif6492cdf2008-07-24 20:50:45 -07003577 case NES_CM_EVENT_ABORTED:
3578 if ((!event->cm_node->cm_id) ||
3579 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003580 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003581 cm_event_connect_error(event);
3582 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3583 break;
3584 case NES_CM_EVENT_DROPPED_PKT:
3585 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3586 break;
3587 default:
3588 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3589 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003590 }
3591
3592 atomic_dec(&cm_core->events_posted);
3593 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3594 rem_ref_cm_node(cm_core, event->cm_node);
3595 kfree(event);
3596
3597 return;
3598}