blob: 4969c386785e39dfcdce20c361c73323024ab6cb [file] [log] [blame]
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001/*
Chien Tungcd6853d2009-03-06 15:12:10 -08002 * Copyright (c) 2006 - 2009 Intel-NE, Inc. All rights reserved.
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34
35#define TCPOPT_TIMESTAMP 8
36
37#include <asm/atomic.h>
38#include <linux/skbuff.h>
39#include <linux/ip.h>
40#include <linux/tcp.h>
41#include <linux/init.h>
42#include <linux/if_arp.h>
Chien Tungf2b2b592008-03-20 19:55:30 -050043#include <linux/if_vlan.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080044#include <linux/notifier.h>
45#include <linux/net.h>
46#include <linux/types.h>
47#include <linux/timer.h>
48#include <linux/time.h>
49#include <linux/delay.h>
50#include <linux/etherdevice.h>
51#include <linux/netdevice.h>
52#include <linux/random.h>
53#include <linux/list.h>
54#include <linux/threads.h>
Bob Sharp7191a0a2008-10-03 12:21:19 -070055#include <net/arp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080056#include <net/neighbour.h>
57#include <net/route.h>
58#include <net/ip_fib.h>
Faisal Latifc11470f2009-04-27 13:38:31 -070059#include <net/tcp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080060
61#include "nes.h"
62
63u32 cm_packets_sent;
64u32 cm_packets_bounced;
65u32 cm_packets_dropped;
66u32 cm_packets_retrans;
67u32 cm_packets_created;
68u32 cm_packets_received;
69u32 cm_listens_created;
70u32 cm_listens_destroyed;
71u32 cm_backlog_drops;
72atomic_t cm_loopbacks;
73atomic_t cm_nodes_created;
74atomic_t cm_nodes_destroyed;
75atomic_t cm_accel_dropped_pkts;
76atomic_t cm_resets_recvd;
77
Faisal Latif6492cdf2008-07-24 20:50:45 -070078static inline int mini_cm_accelerated(struct nes_cm_core *,
79 struct nes_cm_node *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080080static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070081 struct nes_vnic *, struct nes_cm_info *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080082static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070083static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070084 struct nes_vnic *, u16, void *, struct nes_cm_info *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070085static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -070086static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
87 struct nes_cm_node *);
88static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
89 struct nes_cm_node *);
Faisal Latif4a14f6a2008-11-21 20:50:49 -060090static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070091 struct sk_buff *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070092static int mini_cm_dealloc_core(struct nes_cm_core *);
93static int mini_cm_get(struct nes_cm_core *);
94static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070095
Chien Tung6098d102008-11-21 20:51:01 -060096static void form_cm_frame(struct sk_buff *, struct nes_cm_node *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070097 void *, u32, void *, u32, u8);
Faisal Latif6492cdf2008-07-24 20:50:45 -070098static int add_ref_cm_node(struct nes_cm_node *);
99static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
100
Roland Dreier1a855fb2008-04-16 21:01:09 -0700101static int nes_cm_disconn_true(struct nes_qp *);
102static int nes_cm_post_event(struct nes_cm_event *event);
103static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
104static void nes_disconnect_worker(struct work_struct *work);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700105
106static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800107static int send_mpa_reject(struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700108static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
109static int send_reset(struct nes_cm_node *, struct sk_buff *);
110static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fb2008-04-16 21:01:09 -0700111static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700112static void process_packet(struct nes_cm_node *, struct sk_buff *,
113 struct nes_cm_core *);
114
115static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
116static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
117static void cleanup_retrans_entry(struct nes_cm_node *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800118static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700119static void free_retrans_entry(struct nes_cm_node *cm_node);
120static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
121 struct sk_buff *skb, int optionsize, int passive);
122
123/* CM event handler functions */
124static void cm_event_connected(struct nes_cm_event *);
125static void cm_event_connect_error(struct nes_cm_event *);
126static void cm_event_reset(struct nes_cm_event *);
127static void cm_event_mpa_req(struct nes_cm_event *);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800128static void cm_event_mpa_reject(struct nes_cm_event *);
129static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700130
131static void print_core(struct nes_cm_core *core);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800132
133/* External CM API Interface */
134/* instance of function pointers for client API */
135/* set address of this instance to cm_core->cm_ops at cm_core alloc */
136static struct nes_cm_ops nes_cm_api = {
137 mini_cm_accelerated,
138 mini_cm_listen,
139 mini_cm_del_listen,
140 mini_cm_connect,
141 mini_cm_close,
142 mini_cm_accept,
143 mini_cm_reject,
144 mini_cm_recv_pkt,
145 mini_cm_dealloc_core,
146 mini_cm_get,
147 mini_cm_set
148};
149
Roland Dreier1a855fb2008-04-16 21:01:09 -0700150static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800151
152atomic_t cm_connects;
153atomic_t cm_accepts;
154atomic_t cm_disconnects;
155atomic_t cm_closes;
156atomic_t cm_connecteds;
157atomic_t cm_connect_reqs;
158atomic_t cm_rejects;
159
160
161/**
162 * create_event
163 */
164static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
165 enum nes_cm_event_type type)
166{
167 struct nes_cm_event *event;
168
169 if (!cm_node->cm_id)
170 return NULL;
171
172 /* allocate an empty event */
173 event = kzalloc(sizeof(*event), GFP_ATOMIC);
174
175 if (!event)
176 return NULL;
177
178 event->type = type;
179 event->cm_node = cm_node;
180 event->cm_info.rem_addr = cm_node->rem_addr;
181 event->cm_info.loc_addr = cm_node->loc_addr;
182 event->cm_info.rem_port = cm_node->rem_port;
183 event->cm_info.loc_port = cm_node->loc_port;
184 event->cm_info.cm_id = cm_node->cm_id;
185
Faisal Latif6492cdf2008-07-24 20:50:45 -0700186 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
187 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
188 cm_node, event, type, event->cm_info.loc_addr,
189 event->cm_info.loc_port, event->cm_info.rem_addr,
190 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800191
192 nes_cm_post_event(event);
193 return event;
194}
195
196
197/**
198 * send_mpa_request
199 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700200static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800201{
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800202 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700203 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800204 return -1;
205 }
206
207 /* send an MPA Request frame */
208 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
209 cm_node->mpa_frame_size, SET_ACK);
210
Faisal Latif9d5ab132009-03-06 15:15:01 -0800211 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
212}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800213
Faisal Latif9d5ab132009-03-06 15:15:01 -0800214
215
216static int send_mpa_reject(struct nes_cm_node *cm_node)
217{
218 struct sk_buff *skb = NULL;
219
220 skb = dev_alloc_skb(MAX_CM_BUFFER);
221 if (!skb) {
222 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
223 return -ENOMEM;
224 }
225
226 /* send an MPA reject frame */
227 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
228 cm_node->mpa_frame_size, SET_ACK | SET_FIN);
229
230 cm_node->state = NES_CM_STATE_FIN_WAIT1;
231 return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800232}
233
234
235/**
236 * recv_mpa - process a received TCP pkt, we are expecting an
237 * IETF MPA frame
238 */
Faisal Latif9d5ab132009-03-06 15:15:01 -0800239static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
240 u32 len)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800241{
242 struct ietf_mpa_frame *mpa_frame;
243
Faisal Latif9d5ab132009-03-06 15:15:01 -0800244 *type = NES_MPA_REQUEST_ACCEPT;
245
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800246 /* assume req frame is in tcp data payload */
247 if (len < sizeof(struct ietf_mpa_frame)) {
248 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800249 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800250 }
251
252 mpa_frame = (struct ietf_mpa_frame *)buffer;
253 cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
254
255 if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
256 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
257 " complete (%x + %x != %x)\n",
Faisal Latif9d5ab132009-03-06 15:15:01 -0800258 cm_node->mpa_frame_size,
259 (u32)sizeof(struct ietf_mpa_frame), len);
260 return -EINVAL;
261 }
262 /* make sure it does not exceed the max size */
263 if (len > MAX_CM_BUFFER) {
264 nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
265 " (%x + %x != %x)\n",
266 cm_node->mpa_frame_size,
267 (u32)sizeof(struct ietf_mpa_frame), len);
268 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800269 }
270
271 /* copy entire MPA frame to our cm_node's frame */
272 memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
273 cm_node->mpa_frame_size);
274
Faisal Latif9d5ab132009-03-06 15:15:01 -0800275 if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
276 *type = NES_MPA_REQUEST_REJECT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800277 return 0;
278}
279
280
281/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800282 * form_cm_frame - get a free packet and build empty frame Use
283 * node info to build.
284 */
Chien Tung6098d102008-11-21 20:51:01 -0600285static void form_cm_frame(struct sk_buff *skb,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700286 struct nes_cm_node *cm_node, void *options, u32 optionsize,
287 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800288{
289 struct tcphdr *tcph;
290 struct iphdr *iph;
291 struct ethhdr *ethh;
292 u8 *buf;
293 u16 packetsize = sizeof(*iph);
294
295 packetsize += sizeof(*tcph);
296 packetsize += optionsize + datasize;
297
298 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
299
300 skb->len = 0;
301 buf = skb_put(skb, packetsize + ETH_HLEN);
302
303 ethh = (struct ethhdr *) buf;
304 buf += ETH_HLEN;
305
306 iph = (struct iphdr *)buf;
307 buf += sizeof(*iph);
308 tcph = (struct tcphdr *)buf;
309 skb_reset_mac_header(skb);
310 skb_set_network_header(skb, ETH_HLEN);
311 skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
312 buf += sizeof(*tcph);
313
314 skb->ip_summed = CHECKSUM_PARTIAL;
315 skb->protocol = htons(0x800);
316 skb->data_len = 0;
317 skb->mac_len = ETH_HLEN;
318
319 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
320 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
321 ethh->h_proto = htons(0x0800);
322
323 iph->version = IPVERSION;
324 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
325 iph->tos = 0;
326 iph->tot_len = htons(packetsize);
327 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
328
329 iph->frag_off = htons(0x4000);
330 iph->ttl = 0x40;
331 iph->protocol = 0x06; /* IPPROTO_TCP */
332
333 iph->saddr = htonl(cm_node->loc_addr);
334 iph->daddr = htonl(cm_node->rem_addr);
335
336 tcph->source = htons(cm_node->loc_port);
337 tcph->dest = htons(cm_node->rem_port);
338 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
339
340 if (flags & SET_ACK) {
341 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
342 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
343 tcph->ack = 1;
344 } else
345 tcph->ack_seq = 0;
346
347 if (flags & SET_SYN) {
348 cm_node->tcp_cntxt.loc_seq_num++;
349 tcph->syn = 1;
350 } else
Faisal Latif6492cdf2008-07-24 20:50:45 -0700351 cm_node->tcp_cntxt.loc_seq_num += datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800352
Faisal Latif6492cdf2008-07-24 20:50:45 -0700353 if (flags & SET_FIN) {
354 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800355 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700356 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800357
358 if (flags & SET_RST)
359 tcph->rst = 1;
360
361 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
362 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
363 tcph->urg_ptr = 0;
364 if (optionsize)
365 memcpy(buf, options, optionsize);
366 buf += optionsize;
367 if (datasize)
368 memcpy(buf, data, datasize);
369
370 skb_shinfo(skb)->nr_frags = 0;
371 cm_packets_created++;
372
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800373}
374
375
376/**
377 * print_core - dump a cm core
378 */
379static void print_core(struct nes_cm_core *core)
380{
381 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
382 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
383 if (!core)
384 return;
385 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800386
387 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
388
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800389 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
390 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
391
392 nes_debug(NES_DBG_CM, "core : %p \n", core);
393
394 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
395}
396
397
398/**
399 * schedule_nes_timer
400 * note - cm_node needs to be protected before calling this. Encase in:
401 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
402 */
403int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
404 enum nes_timer_type type, int send_retrans,
405 int close_when_complete)
406{
407 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700408 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800409 struct nes_timer_entry *new_send;
410 int ret = 0;
411 u32 was_timer_set;
412
413 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
414 if (!new_send)
Faisal Latif9d5ab132009-03-06 15:15:01 -0800415 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800416
417 /* new_send->timetosend = currenttime */
418 new_send->retrycount = NES_DEFAULT_RETRYS;
419 new_send->retranscount = NES_DEFAULT_RETRANS;
420 new_send->skb = skb;
421 new_send->timetosend = jiffies;
422 new_send->type = type;
423 new_send->netdev = cm_node->netdev;
424 new_send->send_retrans = send_retrans;
425 new_send->close_when_complete = close_when_complete;
426
427 if (type == NES_TIMER_TYPE_CLOSE) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700428 new_send->timetosend += (HZ/10);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800429 if (cm_node->recv_entry) {
Faisal Latif79fc3d72009-04-08 14:22:20 -0700430 kfree(new_send);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800431 WARN_ON(1);
432 return -EINVAL;
433 }
434 cm_node->recv_entry = new_send;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800435 }
436
437 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700438 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800439 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700440 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
441 cm_node->send_entry = new_send;
442 add_ref_cm_node(cm_node);
443 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
444 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800445
446 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
447 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700448 nes_debug(NES_DBG_CM, "Error sending packet %p "
449 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800450 new_send->timetosend = jiffies;
Faisal Latif5962c2c2009-04-08 14:23:55 -0700451 ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800452 } else {
453 cm_packets_sent++;
454 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700455 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800456 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700457 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800458 return ret;
459 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800460 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800461 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800462
463 was_timer_set = timer_pending(&cm_core->tcp_timer);
464
465 if (!was_timer_set) {
466 cm_core->tcp_timer.expires = new_send->timetosend;
467 add_timer(&cm_core->tcp_timer);
468 }
469
470 return ret;
471}
472
Faisal Latif9d5ab132009-03-06 15:15:01 -0800473static void nes_retrans_expired(struct nes_cm_node *cm_node)
474{
475 switch (cm_node->state) {
476 case NES_CM_STATE_SYN_RCVD:
477 case NES_CM_STATE_CLOSING:
478 rem_ref_cm_node(cm_node->cm_core, cm_node);
479 break;
480 case NES_CM_STATE_LAST_ACK:
481 case NES_CM_STATE_FIN_WAIT1:
482 case NES_CM_STATE_MPAREJ_RCVD:
483 send_reset(cm_node, NULL);
484 break;
485 default:
486 create_event(cm_node, NES_CM_EVENT_ABORTED);
487 }
488}
489
490static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
491{
492 struct nes_timer_entry *recv_entry = cm_node->recv_entry;
493 struct iw_cm_id *cm_id = cm_node->cm_id;
494 struct nes_qp *nesqp;
495 unsigned long qplockflags;
496
497 if (!recv_entry)
498 return;
499 nesqp = (struct nes_qp *)recv_entry->skb;
500 if (nesqp) {
501 spin_lock_irqsave(&nesqp->lock, qplockflags);
502 if (nesqp->cm_id) {
503 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
504 "refcount = %d: HIT A "
505 "NES_TIMER_TYPE_CLOSE with something "
506 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
507 atomic_read(&nesqp->refcount));
508 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
509 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
510 nesqp->ibqp_state = IB_QPS_ERR;
511 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
512 nes_cm_disconn(nesqp);
513 } else {
514 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
515 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
516 "refcount = %d: HIT A "
517 "NES_TIMER_TYPE_CLOSE with nothing "
518 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
519 atomic_read(&nesqp->refcount));
520 }
521 } else if (rem_node) {
522 /* TIME_WAIT state */
523 rem_ref_cm_node(cm_node->cm_core, cm_node);
524 }
525 if (cm_node->cm_id)
526 cm_id->rem_ref(cm_id);
527 kfree(recv_entry);
528 cm_node->recv_entry = NULL;
529}
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800530
531/**
532 * nes_cm_timer_tick
533 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700534static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800535{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800536 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800537 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800538 struct nes_cm_node *cm_node;
539 struct nes_timer_entry *send_entry, *recv_entry;
Faisal Latif9d5ab132009-03-06 15:15:01 -0800540 struct list_head *list_core_temp;
541 struct list_head *list_node;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800542 struct nes_cm_core *cm_core = g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800543 u32 settimer = 0;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700544 unsigned long timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800545 int ret = NETDEV_TX_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800546
Faisal Latif879e5bd2008-11-21 20:50:41 -0600547 struct list_head timer_list;
548 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800549 spin_lock_irqsave(&cm_core->ht_lock, flags);
550
Faisal Latif6492cdf2008-07-24 20:50:45 -0700551 list_for_each_safe(list_node, list_core_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600552 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800553 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800554 if ((cm_node->recv_entry) || (cm_node->send_entry)) {
Faisal Latif879e5bd2008-11-21 20:50:41 -0600555 add_ref_cm_node(cm_node);
556 list_add(&cm_node->timer_entry, &timer_list);
557 }
558 }
559 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
560
561 list_for_each_safe(list_node, list_core_temp, &timer_list) {
562 cm_node = container_of(list_node, struct nes_cm_node,
563 timer_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800564 recv_entry = cm_node->recv_entry;
565
566 if (recv_entry) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700567 if (time_after(recv_entry->timetosend, jiffies)) {
568 if (nexttimeout > recv_entry->timetosend ||
Faisal Latif9d5ab132009-03-06 15:15:01 -0800569 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800570 nexttimeout = recv_entry->timetosend;
571 settimer = 1;
572 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800573 } else
574 handle_recv_entry(cm_node, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800575 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800576
577 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700578 do {
579 send_entry = cm_node->send_entry;
580 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600581 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800582 if (time_after(send_entry->timetosend, jiffies)) {
583 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700584 if ((nexttimeout >
585 send_entry->timetosend) ||
586 !settimer) {
587 nexttimeout =
588 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800589 settimer = 1;
590 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800591 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700592 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800593 }
Faisal Latif9d5ab132009-03-06 15:15:01 -0800594 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800595 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700596
597 if ((cm_node->state == NES_CM_STATE_TSA) ||
598 (cm_node->state == NES_CM_STATE_CLOSED)) {
599 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600600 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800601 }
602
Faisal Latif6492cdf2008-07-24 20:50:45 -0700603 if (!send_entry->retranscount ||
604 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800605 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700606 free_retrans_entry(cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800607
Faisal Latif6492cdf2008-07-24 20:50:45 -0700608 spin_unlock_irqrestore(
609 &cm_node->retrans_list_lock, flags);
Faisal Latif9d5ab132009-03-06 15:15:01 -0800610 nes_retrans_expired(cm_node);
611 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700612 spin_lock_irqsave(&cm_node->retrans_list_lock,
613 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600614 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800615 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800616 atomic_inc(&send_entry->skb->users);
617 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700618 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
619 "for node %p, jiffies = %lu, time to send = "
620 "%lu, retranscount = %u, send_entry->seq_num = "
621 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
622 "0x%08X\n", send_entry, cm_node, jiffies,
623 send_entry->timetosend,
624 send_entry->retranscount,
625 send_entry->seq_num,
626 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800627
Faisal Latif6492cdf2008-07-24 20:50:45 -0700628 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
629 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800630 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700631 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800632 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700633 nes_debug(NES_DBG_CM, "rexmit failed for "
634 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800635 cm_packets_bounced++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800636 send_entry->retrycount--;
637 nexttimeout = jiffies + NES_SHORT_TIME;
638 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600639 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800640 } else {
641 cm_packets_sent++;
642 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700643 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
644 "%u, retry count = %u.\n",
645 send_entry->retranscount,
646 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800647 if (send_entry->send_retrans) {
648 send_entry->retranscount--;
Faisal Latif4e9c3902009-04-27 13:39:36 -0700649 timetosend = (NES_RETRY_TIMEOUT <<
650 (NES_DEFAULT_RETRANS - send_entry->retranscount));
651
Faisal Latif6492cdf2008-07-24 20:50:45 -0700652 send_entry->timetosend = jiffies +
Faisal Latif4e9c3902009-04-27 13:39:36 -0700653 min(timetosend, NES_MAX_TIMEOUT);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700654 if (nexttimeout > send_entry->timetosend ||
655 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800656 nexttimeout = send_entry->timetosend;
657 settimer = 1;
658 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800659 } else {
660 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700661 close_when_complete =
662 send_entry->close_when_complete;
663 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
664 cm_node, cm_node->state);
665 free_retrans_entry(cm_node);
666 if (close_when_complete)
667 rem_ref_cm_node(cm_node->cm_core,
668 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800669 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700670 } while (0);
671
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800672 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700673 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800674 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800675
676 if (settimer) {
677 if (!timer_pending(&cm_core->tcp_timer)) {
678 cm_core->tcp_timer.expires = nexttimeout;
679 add_timer(&cm_core->tcp_timer);
680 }
681 }
682}
683
684
685/**
686 * send_syn
687 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700688static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
689 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800690{
691 int ret;
692 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800693 char optionsbuffer[sizeof(struct option_mss) +
Faisal Latif6492cdf2008-07-24 20:50:45 -0700694 sizeof(struct option_windowscale) + sizeof(struct option_base) +
695 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800696
697 int optionssize = 0;
698 /* Sending MSS option */
699 union all_known_options *options;
700
701 if (!cm_node)
702 return -EINVAL;
703
704 options = (union all_known_options *)&optionsbuffer[optionssize];
705 options->as_mss.optionnum = OPTION_NUMBER_MSS;
706 options->as_mss.length = sizeof(struct option_mss);
707 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
708 optionssize += sizeof(struct option_mss);
709
710 options = (union all_known_options *)&optionsbuffer[optionssize];
711 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
712 options->as_windowscale.length = sizeof(struct option_windowscale);
713 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
714 optionssize += sizeof(struct option_windowscale);
715
Faisal Latif6492cdf2008-07-24 20:50:45 -0700716 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800717 options = (union all_known_options *)&optionsbuffer[optionssize];
718 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
719 options->as_base.length = sizeof(struct option_base);
720 optionssize += sizeof(struct option_base);
721 /* we need the size to be a multiple of 4 */
722 options = (union all_known_options *)&optionsbuffer[optionssize];
723 options->as_end = 1;
724 optionssize += 1;
725 options = (union all_known_options *)&optionsbuffer[optionssize];
726 options->as_end = 1;
727 optionssize += 1;
728 }
729
730 options = (union all_known_options *)&optionsbuffer[optionssize];
731 options->as_end = OPTION_NUMBER_END;
732 optionssize += 1;
733
Faisal Latif6492cdf2008-07-24 20:50:45 -0700734 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800735 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800736 if (!skb) {
737 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
738 return -1;
739 }
740
741 if (sendack)
742 flags |= SET_ACK;
743
744 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
745 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
746
747 return ret;
748}
749
750
751/**
752 * send_reset
753 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700754static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800755{
756 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800757 int flags = SET_RST | SET_ACK;
758
Faisal Latif6492cdf2008-07-24 20:50:45 -0700759 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800760 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800761 if (!skb) {
762 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -0800763 return -ENOMEM;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800764 }
765
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800766 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
767 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
768
769 return ret;
770}
771
772
773/**
774 * send_ack
775 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700776static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800777{
778 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700779
780 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800781 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800782
783 if (!skb) {
784 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
785 return -1;
786 }
787
788 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
789 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
790
791 return ret;
792}
793
794
795/**
796 * send_fin
797 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700798static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800799{
800 int ret;
801
802 /* if we didn't get a frame get one */
803 if (!skb)
Faisal Latife1890622008-12-24 20:30:04 -0800804 skb = dev_alloc_skb(MAX_CM_BUFFER);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800805
806 if (!skb) {
807 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
808 return -1;
809 }
810
811 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
812 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
813
814 return ret;
815}
816
817
818/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800819 * find_node - find a cm node that matches the reference cm node
820 */
821static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
822 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
823{
824 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800825 struct list_head *hte;
826 struct nes_cm_node *cm_node;
827
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800828 /* get a handle on the hte */
829 hte = &cm_core->connected_nodes;
830
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800831 /* walk list and find cm_node associated with this session ID */
832 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700833 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800834 /* compare quad, return node handle if a match */
835 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
836 cm_node->loc_addr, cm_node->loc_port,
837 loc_addr, loc_port,
838 cm_node->rem_addr, cm_node->rem_port,
839 rem_addr, rem_port);
840 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
841 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
842 add_ref_cm_node(cm_node);
843 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
844 return cm_node;
845 }
846 }
847 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
848
849 /* no owner node */
850 return NULL;
851}
852
853
854/**
855 * find_listener - find a cm node listening on this addr-port pair
856 */
857static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
858 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
859{
860 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800861 struct nes_cm_listener *listen_node;
862
863 /* walk list and find cm_node associated with this session ID */
864 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700865 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800866 /* compare node pair, return node handle if a match */
867 if (((listen_node->loc_addr == dst_addr) ||
868 listen_node->loc_addr == 0x00000000) &&
869 (listen_node->loc_port == dst_port) &&
870 (listener_state & listen_node->listener_state)) {
871 atomic_inc(&listen_node->ref_count);
872 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
873 return listen_node;
874 }
875 }
876 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
877
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800878 /* no listener */
879 return NULL;
880}
881
882
883/**
884 * add_hte_node - add a cm node to the hash table
885 */
886static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
887{
888 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800889 struct list_head *hte;
890
891 if (!cm_node || !cm_core)
892 return -EINVAL;
893
Faisal Latif6492cdf2008-07-24 20:50:45 -0700894 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
895 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800896
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800897 spin_lock_irqsave(&cm_core->ht_lock, flags);
898
899 /* get a handle on the hash table element (list head for this slot) */
900 hte = &cm_core->connected_nodes;
901 list_add_tail(&cm_node->list, hte);
902 atomic_inc(&cm_core->ht_node_cnt);
903
904 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
905
906 return 0;
907}
908
909
910/**
911 * mini_cm_dec_refcnt_listen
912 */
913static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700914 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800915{
Faisal Latif9d5ab132009-03-06 15:15:01 -0800916 int ret = -EINVAL;
917 int err = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800918 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700919 struct list_head *list_pos = NULL;
920 struct list_head *list_temp = NULL;
921 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -0600922 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700923
924 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
925 "refcnt=%d\n", listener, free_hanging_nodes,
926 atomic_read(&listener->ref_count));
927 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -0600928 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700929 if (free_hanging_nodes) {
930 spin_lock_irqsave(&cm_core->ht_lock, flags);
931 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600932 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700933 cm_node = container_of(list_pos, struct nes_cm_node,
934 list);
935 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -0600936 (!cm_node->accelerated)) {
937 add_ref_cm_node(cm_node);
938 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700939 }
940 }
941 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
942 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600943
944 list_for_each_safe(list_pos, list_temp, &reset_list) {
945 cm_node = container_of(list_pos, struct nes_cm_node,
Faisal Latif9d5ab132009-03-06 15:15:01 -0800946 reset_entry);
947 {
948 struct nes_cm_node *loopback = cm_node->loopbackpartner;
949 if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
950 rem_ref_cm_node(cm_node->cm_core, cm_node);
951 } else {
952 if (!loopback) {
953 cleanup_retrans_entry(cm_node);
954 err = send_reset(cm_node, NULL);
955 if (err) {
956 cm_node->state =
957 NES_CM_STATE_CLOSED;
958 WARN_ON(1);
959 } else {
960 cm_node->state =
961 NES_CM_STATE_CLOSED;
962 rem_ref_cm_node(
963 cm_node->cm_core,
964 cm_node);
965 }
966 } else {
967 struct nes_cm_event event;
968
969 event.cm_node = loopback;
970 event.cm_info.rem_addr =
971 loopback->rem_addr;
972 event.cm_info.loc_addr =
973 loopback->loc_addr;
974 event.cm_info.rem_port =
975 loopback->rem_port;
976 event.cm_info.loc_port =
977 loopback->loc_port;
978 event.cm_info.cm_id = loopback->cm_id;
979 cm_event_connect_error(&event);
980 loopback->state = NES_CM_STATE_CLOSED;
981
982 event.cm_node = cm_node;
983 event.cm_info.rem_addr =
984 cm_node->rem_addr;
985 event.cm_info.loc_addr =
986 cm_node->loc_addr;
987 event.cm_info.rem_port =
988 cm_node->rem_port;
989 event.cm_info.loc_port =
990 cm_node->loc_port;
991 event.cm_info.cm_id = cm_node->cm_id;
992 cm_event_reset(&event);
993
994 rem_ref_cm_node(cm_node->cm_core,
995 cm_node);
996
997 }
998 }
999 }
Faisal Latif879e5bd2008-11-21 20:50:41 -06001000 }
1001
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001002 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1003 if (!atomic_dec_return(&listener->ref_count)) {
1004 list_del(&listener->list);
1005
1006 /* decrement our listen node count */
1007 atomic_dec(&cm_core->listen_node_cnt);
1008
1009 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1010
1011 if (listener->nesvnic) {
1012 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1013 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1014 }
1015
1016 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1017
1018 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -06001019 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001020 ret = 0;
1021 cm_listens_destroyed++;
1022 } else {
1023 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1024 }
1025 if (listener) {
1026 if (atomic_read(&listener->pend_accepts_cnt) > 0)
1027 nes_debug(NES_DBG_CM, "destroying listener (%p)"
1028 " with non-zero pending accepts=%u\n",
1029 listener, atomic_read(&listener->pend_accepts_cnt));
1030 }
1031
1032 return ret;
1033}
1034
1035
1036/**
1037 * mini_cm_del_listen
1038 */
1039static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1040 struct nes_cm_listener *listener)
1041{
1042 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1043 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1044 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1045}
1046
1047
1048/**
1049 * mini_cm_accelerated
1050 */
1051static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1052 struct nes_cm_node *cm_node)
1053{
1054 u32 was_timer_set;
1055 cm_node->accelerated = 1;
1056
1057 if (cm_node->accept_pend) {
1058 BUG_ON(!cm_node->listener);
1059 atomic_dec(&cm_node->listener->pend_accepts_cnt);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001060 cm_node->accept_pend = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001061 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1062 }
1063
1064 was_timer_set = timer_pending(&cm_core->tcp_timer);
1065 if (!was_timer_set) {
1066 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1067 add_timer(&cm_core->tcp_timer);
1068 }
1069
1070 return 0;
1071}
1072
1073
1074/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001075 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001076 */
Bob Sharp7191a0a2008-10-03 12:21:19 -07001077static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001078{
1079 struct rtable *rt;
1080 struct flowi fl;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001081 struct neighbour *neigh;
1082 int rc = -1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001083
1084 memset(&fl, 0, sizeof fl);
1085 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1086 if (ip_route_output_key(&init_net, &rt, &fl)) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08001087 printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001088 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001089 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001090 }
1091
Bob Sharp7191a0a2008-10-03 12:21:19 -07001092 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1093 if (neigh) {
1094 if (neigh->nud_state & NUD_VALID) {
1095 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
Johannes Berge1749612008-10-27 15:59:26 -07001096 " is %pM, Gateway is 0x%08X \n", dst_ip,
1097 neigh->ha, ntohl(rt->rt_gateway));
Bob Sharp7191a0a2008-10-03 12:21:19 -07001098 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1099 dst_ip, NES_ARP_ADD);
1100 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1101 NES_ARP_RESOLVE);
1102 }
1103 neigh_release(neigh);
1104 }
1105
1106 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1107 neigh_event_send(rt->u.dst.neighbour, NULL);
1108
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001109 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001110 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001111}
1112
1113
1114/**
1115 * make_cm_node - create a new instance of a cm node
1116 */
1117static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1118 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1119 struct nes_cm_listener *listener)
1120{
1121 struct nes_cm_node *cm_node;
1122 struct timespec ts;
1123 int arpindex = 0;
1124 struct nes_device *nesdev;
1125 struct nes_adapter *nesadapter;
1126
1127 /* create an hte and cm_node for this instance */
1128 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1129 if (!cm_node)
1130 return NULL;
1131
1132 /* set our node specific transport info */
1133 cm_node->loc_addr = cm_info->loc_addr;
1134 cm_node->rem_addr = cm_info->rem_addr;
1135 cm_node->loc_port = cm_info->loc_port;
1136 cm_node->rem_port = cm_info->rem_port;
1137 cm_node->send_write0 = send_first;
Harvey Harrison63779432008-10-31 00:56:00 -07001138 nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1139 &cm_node->loc_addr, cm_node->loc_port,
1140 &cm_node->rem_addr, cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001141 cm_node->listener = listener;
1142 cm_node->netdev = nesvnic->netdev;
1143 cm_node->cm_id = cm_info->cm_id;
1144 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1145
Faisal Latif6492cdf2008-07-24 20:50:45 -07001146 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1147 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001148
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001149 spin_lock_init(&cm_node->retrans_list_lock);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001150
1151 cm_node->loopbackpartner = NULL;
1152 atomic_set(&cm_node->ref_count, 1);
1153 /* associate our parent CM core */
1154 cm_node->cm_core = cm_core;
1155 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1156 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1157 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1158 NES_CM_DEFAULT_RCV_WND_SCALE;
1159 ts = current_kernel_time();
1160 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1161 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Chien Tungf2b2b592008-03-20 19:55:30 -05001162 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001163 cm_node->tcp_cntxt.rcv_nxt = 0;
1164 /* get a unique session ID , add thread_id to an upcounter to handle race */
1165 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001166 cm_node->conn_type = cm_info->conn_type;
1167 cm_node->apbvt_set = 0;
1168 cm_node->accept_pend = 0;
1169
1170 cm_node->nesvnic = nesvnic;
1171 /* get some device handles, for arp lookup */
1172 nesdev = nesvnic->nesdev;
1173 nesadapter = nesdev->nesadapter;
1174
1175 cm_node->loopbackpartner = NULL;
1176 /* get the mac addr for the remote node */
Chien Tung1ee86552008-11-21 20:51:04 -06001177 if (ipv4_is_loopback(htonl(cm_node->rem_addr)))
1178 arpindex = nes_arp_table(nesdev, ntohl(nesvnic->local_ipaddr), NULL, NES_ARP_RESOLVE);
1179 else
1180 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001181 if (arpindex < 0) {
Bob Sharp7191a0a2008-10-03 12:21:19 -07001182 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr);
1183 if (arpindex < 0) {
1184 kfree(cm_node);
1185 return NULL;
1186 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001187 }
1188
1189 /* copy the mac addr to node context */
1190 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07001191 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1192 cm_node->rem_mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001193
1194 add_hte_node(cm_core, cm_node);
1195 atomic_inc(&cm_nodes_created);
1196
1197 return cm_node;
1198}
1199
1200
1201/**
1202 * add_ref_cm_node - destroy an instance of a cm node
1203 */
1204static int add_ref_cm_node(struct nes_cm_node *cm_node)
1205{
1206 atomic_inc(&cm_node->ref_count);
1207 return 0;
1208}
1209
1210
1211/**
1212 * rem_ref_cm_node - destroy an instance of a cm node
1213 */
1214static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001215 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001216{
Faisal Latif9d5ab132009-03-06 15:15:01 -08001217 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001218 struct nes_qp *nesqp;
1219
1220 if (!cm_node)
1221 return -EINVAL;
1222
1223 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1224 if (atomic_dec_return(&cm_node->ref_count)) {
1225 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1226 return 0;
1227 }
1228 list_del(&cm_node->list);
1229 atomic_dec(&cm_core->ht_node_cnt);
1230 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1231
1232 /* if the node is destroyed before connection was accelerated */
1233 if (!cm_node->accelerated && cm_node->accept_pend) {
1234 BUG_ON(!cm_node->listener);
1235 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1236 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1237 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001238 WARN_ON(cm_node->send_entry);
1239 if (cm_node->recv_entry)
1240 handle_recv_entry(cm_node, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001241 if (cm_node->listener) {
1242 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1243 } else {
1244 if (cm_node->apbvt_set && cm_node->nesvnic) {
1245 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001246 PCI_FUNC(
1247 cm_node->nesvnic->nesdev->pcidev->devfn),
1248 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001249 }
1250 }
1251
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001252 atomic_dec(&cm_core->node_cnt);
1253 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001254 nesqp = cm_node->nesqp;
1255 if (nesqp) {
1256 nesqp->cm_node = NULL;
1257 nes_rem_ref(&nesqp->ibqp);
1258 cm_node->nesqp = NULL;
1259 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001260
Faisal Latif6492cdf2008-07-24 20:50:45 -07001261 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001262 return 0;
1263}
1264
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001265/**
1266 * process_options
1267 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001268static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1269 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001270{
1271 u32 tmp;
1272 u32 offset = 0;
1273 union all_known_options *all_options;
1274 char got_mss_option = 0;
1275
1276 while (offset < optionsize) {
1277 all_options = (union all_known_options *)(optionsloc + offset);
1278 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001279 case OPTION_NUMBER_END:
1280 offset = optionsize;
1281 break;
1282 case OPTION_NUMBER_NONE:
1283 offset += 1;
1284 continue;
1285 case OPTION_NUMBER_MSS:
1286 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1287 "Size: %d\n", __func__,
1288 all_options->as_mss.length, offset, optionsize);
1289 got_mss_option = 1;
1290 if (all_options->as_mss.length != 4) {
1291 return 1;
1292 } else {
1293 tmp = ntohs(all_options->as_mss.mss);
1294 if (tmp > 0 && tmp <
1295 cm_node->tcp_cntxt.mss)
1296 cm_node->tcp_cntxt.mss = tmp;
1297 }
1298 break;
1299 case OPTION_NUMBER_WINDOW_SCALE:
1300 cm_node->tcp_cntxt.snd_wscale =
1301 all_options->as_windowscale.shiftcount;
1302 break;
1303 case OPTION_NUMBER_WRITE0:
1304 cm_node->send_write0 = 1;
1305 break;
1306 default:
1307 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1308 all_options->as_base.optionnum);
1309 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001310 }
1311 offset += all_options->as_base.length;
1312 }
1313 if ((!got_mss_option) && (syn_packet))
1314 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1315 return 0;
1316}
1317
Faisal Latif6492cdf2008-07-24 20:50:45 -07001318static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001319{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001320 atomic_inc(&cm_accel_dropped_pkts);
1321 dev_kfree_skb_any(skb);
1322}
1323
Faisal Latif9d5ab132009-03-06 15:15:01 -08001324static void handle_fin_pkt(struct nes_cm_node *cm_node)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001325{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001326 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1327 "refcnt=%d\n", cm_node, cm_node->state,
1328 atomic_read(&cm_node->ref_count));
1329 cm_node->tcp_cntxt.rcv_nxt++;
1330 cleanup_retrans_entry(cm_node);
1331 switch (cm_node->state) {
1332 case NES_CM_STATE_SYN_RCVD:
1333 case NES_CM_STATE_SYN_SENT:
1334 case NES_CM_STATE_ESTABLISHED:
1335 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001336 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001337 cm_node->state = NES_CM_STATE_LAST_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001338 send_fin(cm_node, NULL);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001339 break;
1340 case NES_CM_STATE_FIN_WAIT1:
1341 cm_node->state = NES_CM_STATE_CLOSING;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001342 send_ack(cm_node, NULL);
1343 /* Wait for ACK as this is simultanous close..
1344 * After we receive ACK, do not send anything..
1345 * Just rm the node.. Done.. */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001346 break;
1347 case NES_CM_STATE_FIN_WAIT2:
1348 cm_node->state = NES_CM_STATE_TIME_WAIT;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001349 send_ack(cm_node, NULL);
1350 schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1351 break;
1352 case NES_CM_STATE_TIME_WAIT:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001353 cm_node->state = NES_CM_STATE_CLOSED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001354 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001355 break;
1356 case NES_CM_STATE_TSA:
1357 default:
1358 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1359 cm_node, cm_node->state);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001360 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001361 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001362}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001363
Faisal Latif6492cdf2008-07-24 20:50:45 -07001364
1365static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1366 struct tcphdr *tcph)
1367{
1368
1369 int reset = 0; /* whether to send reset in case of err.. */
Faisal Latif183ecfa2008-11-21 20:50:46 -06001370 int passive_state;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001371 atomic_inc(&cm_resets_recvd);
1372 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1373 " refcnt=%d\n", cm_node, cm_node->state,
1374 atomic_read(&cm_node->ref_count));
1375 cleanup_retrans_entry(cm_node);
1376 switch (cm_node->state) {
1377 case NES_CM_STATE_SYN_SENT:
1378 case NES_CM_STATE_MPAREQ_SENT:
1379 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1380 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1381 cm_node->listener, cm_node->state);
1382 active_open_err(cm_node, skb, reset);
1383 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001384 case NES_CM_STATE_MPAREQ_RCVD:
1385 passive_state = atomic_add_return(1, &cm_node->passive_state);
1386 if (passive_state == NES_SEND_RESET_EVENT)
1387 create_event(cm_node, NES_CM_EVENT_RESET);
1388 cleanup_retrans_entry(cm_node);
1389 cm_node->state = NES_CM_STATE_CLOSED;
1390 dev_kfree_skb_any(skb);
1391 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001392 case NES_CM_STATE_ESTABLISHED:
1393 case NES_CM_STATE_SYN_RCVD:
1394 case NES_CM_STATE_LISTENING:
1395 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1396 passive_open_err(cm_node, skb, reset);
1397 break;
1398 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001399 active_open_err(cm_node, skb, reset);
1400 break;
1401 case NES_CM_STATE_CLOSED:
1402 cleanup_retrans_entry(cm_node);
1403 drop_packet(skb);
1404 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001405 case NES_CM_STATE_TIME_WAIT:
1406 cleanup_retrans_entry(cm_node);
1407 cm_node->state = NES_CM_STATE_CLOSED;
1408 rem_ref_cm_node(cm_node->cm_core, cm_node);
1409 drop_packet(skb);
1410 break;
1411 case NES_CM_STATE_FIN_WAIT1:
1412 cleanup_retrans_entry(cm_node);
1413 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001414 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001415 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001416 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001417 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001418}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001419
Faisal Latif9d5ab132009-03-06 15:15:01 -08001420
1421static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001422{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001423
Faisal Latif9d5ab132009-03-06 15:15:01 -08001424 int ret = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001425 int datasize = skb->len;
1426 u8 *dataloc = skb->data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001427
1428 enum nes_cm_event_type type = NES_CM_EVENT_UNKNOWN;
1429 u32 res_type;
1430 ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1431 if (ret) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001432 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
Faisal Latif9d5ab132009-03-06 15:15:01 -08001433 if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001434 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1435 "cm_node=%p listener=%p state=%d\n", __func__,
1436 __LINE__, cm_node, cm_node->listener,
1437 cm_node->state);
1438 active_open_err(cm_node, skb, 1);
1439 } else {
1440 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001441 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001442 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001443 }
Faisal Latif9d5ab132009-03-06 15:15:01 -08001444
1445 switch (cm_node->state) {
1446 case NES_CM_STATE_ESTABLISHED:
1447 if (res_type == NES_MPA_REQUEST_REJECT) {
1448 /*BIG problem as we are receiving the MPA.. So should
1449 * not be REJECT.. This is Passive Open.. We can
1450 * only receive it Reject for Active Open...*/
1451 WARN_ON(1);
1452 }
1453 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1454 type = NES_CM_EVENT_MPA_REQ;
1455 atomic_set(&cm_node->passive_state,
1456 NES_PASSIVE_STATE_INDICATED);
1457 break;
1458 case NES_CM_STATE_MPAREQ_SENT:
1459 if (res_type == NES_MPA_REQUEST_REJECT) {
1460 type = NES_CM_EVENT_MPA_REJECT;
1461 cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1462 } else {
1463 type = NES_CM_EVENT_CONNECTED;
1464 cm_node->state = NES_CM_STATE_TSA;
1465 }
1466
1467 break;
1468 default:
1469 WARN_ON(1);
1470 break;
1471 }
1472 dev_kfree_skb_any(skb);
1473 create_event(cm_node, type);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001474}
1475
1476static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1477{
1478 switch (cm_node->state) {
1479 case NES_CM_STATE_SYN_SENT:
1480 case NES_CM_STATE_MPAREQ_SENT:
1481 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1482 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1483 cm_node->listener, cm_node->state);
1484 active_open_err(cm_node, skb, 1);
1485 break;
1486 case NES_CM_STATE_ESTABLISHED:
1487 case NES_CM_STATE_SYN_RCVD:
1488 passive_open_err(cm_node, skb, 1);
1489 break;
1490 case NES_CM_STATE_TSA:
1491 default:
1492 drop_packet(skb);
1493 }
1494}
1495
1496static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1497 struct sk_buff *skb)
1498{
1499 int err;
1500
1501 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1502 if (err)
1503 active_open_err(cm_node, skb, 1);
1504
1505 return err;
1506}
1507
1508static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1509 struct sk_buff *skb)
1510{
1511 int err = 0;
1512 u32 seq;
1513 u32 ack_seq;
1514 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1515 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1516 u32 rcv_wnd;
1517 seq = ntohl(tcph->seq);
1518 ack_seq = ntohl(tcph->ack_seq);
1519 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1520 if (ack_seq != loc_seq_num)
1521 err = 1;
Faisal Latifc11470f2009-04-27 13:38:31 -07001522 else if (!between(seq, rcv_nxt, (rcv_nxt+rcv_wnd)))
Faisal Latif6492cdf2008-07-24 20:50:45 -07001523 err = 1;
1524 if (err) {
1525 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1526 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1527 cm_node->listener, cm_node->state);
1528 indicate_pkt_err(cm_node, skb);
1529 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1530 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1531 rcv_wnd);
1532 }
1533 return err;
1534}
1535
1536/*
1537 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1538 * is created with a listener or it may comein as rexmitted packet which in
1539 * that case will be just dropped.
1540 */
1541
1542static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1543 struct tcphdr *tcph)
1544{
1545 int ret;
1546 u32 inc_sequence;
1547 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001548
1549 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001550 skb_trim(skb, 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001551 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001552
Faisal Latif6492cdf2008-07-24 20:50:45 -07001553 switch (cm_node->state) {
1554 case NES_CM_STATE_SYN_SENT:
1555 case NES_CM_STATE_MPAREQ_SENT:
1556 /* Rcvd syn on active open connection*/
1557 active_open_err(cm_node, skb, 1);
1558 break;
1559 case NES_CM_STATE_LISTENING:
1560 /* Passive OPEN */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001561 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1562 cm_node->listener->backlog) {
1563 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1564 "pressure \n");
1565 cm_backlog_drops++;
1566 passive_open_err(cm_node, skb, 0);
1567 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001568 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001569 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1570 1);
1571 if (ret) {
1572 passive_open_err(cm_node, skb, 0);
1573 /* drop pkt */
1574 break;
1575 }
1576 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1577 BUG_ON(cm_node->send_entry);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001578 cm_node->accept_pend = 1;
1579 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1580
Faisal Latif6492cdf2008-07-24 20:50:45 -07001581 cm_node->state = NES_CM_STATE_SYN_RCVD;
1582 send_syn(cm_node, 1, skb);
1583 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001584 case NES_CM_STATE_CLOSED:
1585 cleanup_retrans_entry(cm_node);
1586 send_reset(cm_node, skb);
1587 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001588 case NES_CM_STATE_TSA:
1589 case NES_CM_STATE_ESTABLISHED:
1590 case NES_CM_STATE_FIN_WAIT1:
1591 case NES_CM_STATE_FIN_WAIT2:
1592 case NES_CM_STATE_MPAREQ_RCVD:
1593 case NES_CM_STATE_LAST_ACK:
1594 case NES_CM_STATE_CLOSING:
1595 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001596 default:
1597 drop_packet(skb);
1598 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001599 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001600}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001601
Faisal Latif6492cdf2008-07-24 20:50:45 -07001602static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1603 struct tcphdr *tcph)
1604{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001605
Faisal Latif6492cdf2008-07-24 20:50:45 -07001606 int ret;
1607 u32 inc_sequence;
1608 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001609
Faisal Latif6492cdf2008-07-24 20:50:45 -07001610 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001611 skb_trim(skb, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001612 inc_sequence = ntohl(tcph->seq);
1613 switch (cm_node->state) {
1614 case NES_CM_STATE_SYN_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001615 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001616 /* active open */
1617 if (check_syn(cm_node, tcph, skb))
1618 return;
1619 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1620 /* setup options */
1621 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1622 if (ret) {
1623 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1624 cm_node);
1625 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001626 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001627 cleanup_retrans_entry(cm_node);
1628 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1629 send_mpa_request(cm_node, skb);
1630 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1631 break;
1632 case NES_CM_STATE_MPAREQ_RCVD:
1633 /* passive open, so should not be here */
1634 passive_open_err(cm_node, skb, 1);
1635 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001636 case NES_CM_STATE_LISTENING:
1637 case NES_CM_STATE_CLOSED:
1638 cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1639 cleanup_retrans_entry(cm_node);
1640 send_reset(cm_node, skb);
1641 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001642 case NES_CM_STATE_ESTABLISHED:
1643 case NES_CM_STATE_FIN_WAIT1:
1644 case NES_CM_STATE_FIN_WAIT2:
1645 case NES_CM_STATE_LAST_ACK:
1646 case NES_CM_STATE_TSA:
1647 case NES_CM_STATE_CLOSING:
1648 case NES_CM_STATE_UNKNOWN:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001649 case NES_CM_STATE_MPAREQ_SENT:
1650 default:
1651 drop_packet(skb);
1652 break;
1653 }
1654}
1655
1656static void handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1657 struct tcphdr *tcph)
1658{
1659 int datasize = 0;
1660 u32 inc_sequence;
1661 u32 rem_seq_ack;
1662 u32 rem_seq;
Faisal Latifabb77252008-11-21 20:50:52 -06001663 int ret;
1664 int optionsize;
Faisal Latifabb77252008-11-21 20:50:52 -06001665 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Faisal Latifabb77252008-11-21 20:50:52 -06001666
Faisal Latif6492cdf2008-07-24 20:50:45 -07001667 if (check_seq(cm_node, tcph, skb))
1668 return;
1669
1670 skb_pull(skb, tcph->doff << 2);
1671 inc_sequence = ntohl(tcph->seq);
1672 rem_seq = ntohl(tcph->seq);
1673 rem_seq_ack = ntohl(tcph->ack_seq);
1674 datasize = skb->len;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001675 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001676 switch (cm_node->state) {
1677 case NES_CM_STATE_SYN_RCVD:
1678 /* Passive OPEN */
Faisal Latifabb77252008-11-21 20:50:52 -06001679 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1680 if (ret)
1681 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001682 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
Faisal Latifabb77252008-11-21 20:50:52 -06001683 if (cm_node->tcp_cntxt.rem_ack_num !=
1684 cm_node->tcp_cntxt.loc_seq_num) {
1685 nes_debug(NES_DBG_CM, "rem_ack_num != loc_seq_num\n");
1686 cleanup_retrans_entry(cm_node);
1687 send_reset(cm_node, skb);
1688 return;
1689 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001690 cm_node->state = NES_CM_STATE_ESTABLISHED;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001691 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001692 if (datasize) {
1693 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001694 handle_rcv_mpa(cm_node, skb);
1695 } else { /* rcvd ACK only */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001696 dev_kfree_skb_any(skb);
1697 cleanup_retrans_entry(cm_node);
1698 }
1699 break;
1700 case NES_CM_STATE_ESTABLISHED:
1701 /* Passive OPEN */
Faisal Latif9d5ab132009-03-06 15:15:01 -08001702 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001703 if (datasize) {
1704 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001705 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001706 } else
1707 drop_packet(skb);
1708 break;
1709 case NES_CM_STATE_MPAREQ_SENT:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001710 cleanup_retrans_entry(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001711 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1712 if (datasize) {
1713 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001714 handle_rcv_mpa(cm_node, skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001715 } else { /* Could be just an ack pkt.. */
1716 cleanup_retrans_entry(cm_node);
1717 dev_kfree_skb_any(skb);
1718 }
1719 break;
Faisal Latifabb77252008-11-21 20:50:52 -06001720 case NES_CM_STATE_LISTENING:
1721 case NES_CM_STATE_CLOSED:
1722 cleanup_retrans_entry(cm_node);
1723 send_reset(cm_node, skb);
1724 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001725 case NES_CM_STATE_LAST_ACK:
1726 cleanup_retrans_entry(cm_node);
1727 cm_node->state = NES_CM_STATE_CLOSED;
1728 cm_node->cm_id->rem_ref(cm_node->cm_id);
1729 case NES_CM_STATE_CLOSING:
1730 cleanup_retrans_entry(cm_node);
1731 rem_ref_cm_node(cm_node->cm_core, cm_node);
1732 drop_packet(skb);
1733 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001734 case NES_CM_STATE_FIN_WAIT1:
Faisal Latif9d5ab132009-03-06 15:15:01 -08001735 cleanup_retrans_entry(cm_node);
1736 drop_packet(skb);
1737 cm_node->state = NES_CM_STATE_FIN_WAIT2;
1738 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001739 case NES_CM_STATE_SYN_SENT:
1740 case NES_CM_STATE_FIN_WAIT2:
1741 case NES_CM_STATE_TSA:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001742 case NES_CM_STATE_MPAREQ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07001743 case NES_CM_STATE_UNKNOWN:
1744 default:
1745 drop_packet(skb);
1746 break;
1747 }
1748}
1749
1750
1751
1752static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1753 struct sk_buff *skb, int optionsize, int passive)
1754{
1755 u8 *optionsloc = (u8 *)&tcph[1];
1756 if (optionsize) {
1757 if (process_options(cm_node, optionsloc, optionsize,
1758 (u32)tcph->syn)) {
1759 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1760 __func__, cm_node);
1761 if (passive)
Faisal Latifabb77252008-11-21 20:50:52 -06001762 passive_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001763 else
Faisal Latifabb77252008-11-21 20:50:52 -06001764 active_open_err(cm_node, skb, 1);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001765 return 1;
1766 }
1767 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001768
1769 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1770 cm_node->tcp_cntxt.snd_wscale;
1771
Faisal Latif6492cdf2008-07-24 20:50:45 -07001772 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001773 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001774 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001775}
1776
Faisal Latif6492cdf2008-07-24 20:50:45 -07001777/*
1778 * active_open_err() will send reset() if flag set..
1779 * It will also send ABORT event.
1780 */
1781
1782static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1783 int reset)
1784{
1785 cleanup_retrans_entry(cm_node);
1786 if (reset) {
1787 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1788 "state=%d\n", cm_node, cm_node->state);
1789 add_ref_cm_node(cm_node);
1790 send_reset(cm_node, skb);
1791 } else
1792 dev_kfree_skb_any(skb);
1793
1794 cm_node->state = NES_CM_STATE_CLOSED;
1795 create_event(cm_node, NES_CM_EVENT_ABORTED);
1796}
1797
1798/*
1799 * passive_open_err() will either do a reset() or will free up the skb and
1800 * remove the cm_node.
1801 */
1802
1803static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1804 int reset)
1805{
1806 cleanup_retrans_entry(cm_node);
1807 cm_node->state = NES_CM_STATE_CLOSED;
1808 if (reset) {
1809 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1810 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1811 send_reset(cm_node, skb);
1812 } else {
1813 dev_kfree_skb_any(skb);
1814 rem_ref_cm_node(cm_node->cm_core, cm_node);
1815 }
1816}
1817
1818/*
1819 * free_retrans_entry() routines assumes that the retrans_list_lock has
1820 * been acquired before calling.
1821 */
1822static void free_retrans_entry(struct nes_cm_node *cm_node)
1823{
1824 struct nes_timer_entry *send_entry;
1825 send_entry = cm_node->send_entry;
1826 if (send_entry) {
1827 cm_node->send_entry = NULL;
1828 dev_kfree_skb_any(send_entry->skb);
1829 kfree(send_entry);
1830 rem_ref_cm_node(cm_node->cm_core, cm_node);
1831 }
1832}
1833
1834static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1835{
1836 unsigned long flags;
1837
1838 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1839 free_retrans_entry(cm_node);
1840 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1841}
1842
1843/**
1844 * process_packet
1845 * Returns skb if to be freed, else it will return NULL if already used..
1846 */
1847static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1848 struct nes_cm_core *cm_core)
1849{
1850 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1851 struct tcphdr *tcph = tcp_hdr(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001852 u32 fin_set = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001853 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1854
1855 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1856 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1857 tcph->ack, tcph->rst, tcph->fin);
1858
1859 if (tcph->rst)
1860 pkt_type = NES_PKT_TYPE_RST;
1861 else if (tcph->syn) {
1862 pkt_type = NES_PKT_TYPE_SYN;
1863 if (tcph->ack)
1864 pkt_type = NES_PKT_TYPE_SYNACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001865 } else if (tcph->ack)
Faisal Latif6492cdf2008-07-24 20:50:45 -07001866 pkt_type = NES_PKT_TYPE_ACK;
Faisal Latif9d5ab132009-03-06 15:15:01 -08001867 if (tcph->fin)
1868 fin_set = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001869
1870 switch (pkt_type) {
1871 case NES_PKT_TYPE_SYN:
1872 handle_syn_pkt(cm_node, skb, tcph);
1873 break;
1874 case NES_PKT_TYPE_SYNACK:
1875 handle_synack_pkt(cm_node, skb, tcph);
1876 break;
1877 case NES_PKT_TYPE_ACK:
1878 handle_ack_pkt(cm_node, skb, tcph);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001879 if (fin_set)
1880 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001881 break;
1882 case NES_PKT_TYPE_RST:
1883 handle_rst_pkt(cm_node, skb, tcph);
1884 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001885 default:
1886 drop_packet(skb);
Faisal Latif9d5ab132009-03-06 15:15:01 -08001887 if (fin_set)
1888 handle_fin_pkt(cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001889 break;
1890 }
1891}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001892
1893/**
1894 * mini_cm_listen - create a listen node with params
1895 */
1896static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001897 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001898{
1899 struct nes_cm_listener *listener;
1900 unsigned long flags;
1901
1902 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1903 cm_info->loc_addr, cm_info->loc_port);
1904
1905 /* cannot have multiple matching listeners */
1906 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1907 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1908 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1909 /* find automatically incs ref count ??? */
1910 atomic_dec(&listener->ref_count);
1911 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1912 return NULL;
1913 }
1914
1915 if (!listener) {
1916 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1917 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1918 if (!listener) {
1919 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1920 return NULL;
1921 }
1922
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001923 listener->loc_addr = htonl(cm_info->loc_addr);
1924 listener->loc_port = htons(cm_info->loc_port);
1925 listener->reused_node = 0;
1926
1927 atomic_set(&listener->ref_count, 1);
1928 }
1929 /* pasive case */
1930 /* find already inc'ed the ref count */
1931 else {
1932 listener->reused_node = 1;
1933 }
1934
1935 listener->cm_id = cm_info->cm_id;
1936 atomic_set(&listener->pend_accepts_cnt, 0);
1937 listener->cm_core = cm_core;
1938 listener->nesvnic = nesvnic;
1939 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001940
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001941 listener->conn_type = cm_info->conn_type;
1942 listener->backlog = cm_info->backlog;
1943 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1944
1945 if (!listener->reused_node) {
1946 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1947 list_add(&listener->list, &cm_core->listen_list.list);
1948 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1949 atomic_inc(&cm_core->listen_node_cnt);
1950 }
1951
1952 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1953 " listener = %p, backlog = %d, cm_id = %p.\n",
1954 cm_info->loc_addr, cm_info->loc_port,
1955 listener, listener->backlog, listener->cm_id);
1956
1957 return listener;
1958}
1959
1960
1961/**
1962 * mini_cm_connect - make a connection node with params
1963 */
Faisal Latif54c86a82008-09-30 14:47:27 -07001964static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001965 struct nes_vnic *nesvnic, u16 private_data_len,
1966 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001967{
1968 int ret = 0;
1969 struct nes_cm_node *cm_node;
1970 struct nes_cm_listener *loopbackremotelistener;
1971 struct nes_cm_node *loopbackremotenode;
1972 struct nes_cm_info loopback_cm_info;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001973 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
1974 struct ietf_mpa_frame *mpa_frame = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001975
1976 /* create a CM connection node */
1977 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
1978 if (!cm_node)
1979 return NULL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001980 mpa_frame = &cm_node->mpa_frame;
1981 strcpy(mpa_frame->key, IEFT_MPA_KEY_REQ);
1982 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
1983 mpa_frame->rev = IETF_MPA_VERSION;
1984 mpa_frame->priv_data_len = htons(private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001985
Glenn Streiff7495ab62008-04-29 13:46:54 -07001986 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001987 cm_node->tcp_cntxt.client = 1;
1988 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1989
1990 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001991 loopbackremotelistener = find_listener(cm_core,
1992 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
1993 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001994 if (loopbackremotelistener == NULL) {
1995 create_event(cm_node, NES_CM_EVENT_ABORTED);
1996 } else {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001997 loopback_cm_info = *cm_info;
1998 loopback_cm_info.loc_port = cm_info->rem_port;
1999 loopback_cm_info.rem_port = cm_info->loc_port;
2000 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002001 loopbackremotenode = make_cm_node(cm_core, nesvnic,
2002 &loopback_cm_info, loopbackremotelistener);
Faisal Latif79fc3d72009-04-08 14:22:20 -07002003 if (!loopbackremotenode) {
2004 rem_ref_cm_node(cm_node->cm_core, cm_node);
2005 return NULL;
2006 }
2007 atomic_inc(&cm_loopbacks);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002008 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002009 loopbackremotenode->tcp_cntxt.rcv_wscale =
2010 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002011 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002012 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2013 private_data_len);
2014 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002015
Faisal Latif6492cdf2008-07-24 20:50:45 -07002016 /* we are done handling this state. */
2017 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002018 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002019 cm_node->tcp_cntxt.rcv_nxt =
2020 loopbackremotenode->tcp_cntxt.loc_seq_num;
2021 loopbackremotenode->tcp_cntxt.rcv_nxt =
2022 cm_node->tcp_cntxt.loc_seq_num;
2023 cm_node->tcp_cntxt.max_snd_wnd =
2024 loopbackremotenode->tcp_cntxt.rcv_wnd;
2025 loopbackremotenode->tcp_cntxt.max_snd_wnd =
2026 cm_node->tcp_cntxt.rcv_wnd;
2027 cm_node->tcp_cntxt.snd_wnd =
2028 loopbackremotenode->tcp_cntxt.rcv_wnd;
2029 loopbackremotenode->tcp_cntxt.snd_wnd =
2030 cm_node->tcp_cntxt.rcv_wnd;
2031 cm_node->tcp_cntxt.snd_wscale =
2032 loopbackremotenode->tcp_cntxt.rcv_wscale;
2033 loopbackremotenode->tcp_cntxt.snd_wscale =
2034 cm_node->tcp_cntxt.rcv_wscale;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002035 loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002036 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2037 }
2038 return cm_node;
2039 }
2040
2041 /* set our node side to client (active) side */
2042 cm_node->tcp_cntxt.client = 1;
2043 /* init our MPA frame ptr */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002044 memcpy(mpa_frame->priv_data, private_data, private_data_len);
2045
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002046 cm_node->mpa_frame_size = mpa_frame_size;
2047
2048 /* send a syn and goto syn sent state */
2049 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002050 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002051
Faisal Latif6492cdf2008-07-24 20:50:45 -07002052 if (ret) {
2053 /* error in sending the syn free up the cm_node struct */
2054 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2055 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2056 cm_node->rem_addr, cm_node->rem_port, cm_node,
2057 cm_node->cm_id);
2058 rem_ref_cm_node(cm_node->cm_core, cm_node);
2059 cm_node = NULL;
2060 }
2061
2062 if (cm_node)
2063 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2064 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2065 cm_node->rem_addr, cm_node->rem_port, cm_node,
2066 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002067
2068 return cm_node;
2069}
2070
2071
2072/**
2073 * mini_cm_accept - accept a connection
2074 * This function is never called
2075 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002076static int mini_cm_accept(struct nes_cm_core *cm_core,
2077 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002078{
2079 return 0;
2080}
2081
2082
2083/**
2084 * mini_cm_reject - reject and teardown a connection
2085 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002086static int mini_cm_reject(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002087 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002088{
2089 int ret = 0;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002090 int err = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002091 int passive_state;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002092 struct nes_cm_event event;
2093 struct iw_cm_id *cm_id = cm_node->cm_id;
2094 struct nes_cm_node *loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002095
Faisal Latif6492cdf2008-07-24 20:50:45 -07002096 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2097 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002098
Faisal Latif6492cdf2008-07-24 20:50:45 -07002099 if (cm_node->tcp_cntxt.client)
2100 return ret;
2101 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002102
Faisal Latif9d5ab132009-03-06 15:15:01 -08002103 if (!loopback) {
2104 passive_state = atomic_add_return(1, &cm_node->passive_state);
2105 if (passive_state == NES_SEND_RESET_EVENT) {
2106 cm_node->state = NES_CM_STATE_CLOSED;
2107 rem_ref_cm_node(cm_core, cm_node);
2108 } else {
2109 ret = send_mpa_reject(cm_node);
2110 if (ret) {
2111 cm_node->state = NES_CM_STATE_CLOSED;
2112 err = send_reset(cm_node, NULL);
2113 if (err)
2114 WARN_ON(1);
2115 } else
2116 cm_id->add_ref(cm_id);
2117 }
2118 } else {
2119 cm_node->cm_id = NULL;
2120 event.cm_node = loopback;
2121 event.cm_info.rem_addr = loopback->rem_addr;
2122 event.cm_info.loc_addr = loopback->loc_addr;
2123 event.cm_info.rem_port = loopback->rem_port;
2124 event.cm_info.loc_port = loopback->loc_port;
2125 event.cm_info.cm_id = loopback->cm_id;
2126 cm_event_mpa_reject(&event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002127 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif9d5ab132009-03-06 15:15:01 -08002128 loopback->state = NES_CM_STATE_CLOSING;
2129
2130 cm_id = loopback->cm_id;
2131 rem_ref_cm_node(cm_core, loopback);
2132 cm_id->rem_ref(cm_id);
2133 }
2134
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002135 return ret;
2136}
2137
2138
2139/**
2140 * mini_cm_close
2141 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002142static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002143{
2144 int ret = 0;
2145
2146 if (!cm_core || !cm_node)
2147 return -EINVAL;
2148
2149 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002150 case NES_CM_STATE_SYN_RCVD:
2151 case NES_CM_STATE_SYN_SENT:
2152 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2153 case NES_CM_STATE_ESTABLISHED:
2154 case NES_CM_STATE_ACCEPTING:
2155 case NES_CM_STATE_MPAREQ_SENT:
2156 case NES_CM_STATE_MPAREQ_RCVD:
2157 cleanup_retrans_entry(cm_node);
2158 send_reset(cm_node, NULL);
2159 break;
2160 case NES_CM_STATE_CLOSE_WAIT:
2161 cm_node->state = NES_CM_STATE_LAST_ACK;
2162 send_fin(cm_node, NULL);
2163 break;
2164 case NES_CM_STATE_FIN_WAIT1:
2165 case NES_CM_STATE_FIN_WAIT2:
2166 case NES_CM_STATE_LAST_ACK:
2167 case NES_CM_STATE_TIME_WAIT:
2168 case NES_CM_STATE_CLOSING:
2169 ret = -1;
2170 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002171 case NES_CM_STATE_MPAREJ_RCVD:
Faisal Latif6492cdf2008-07-24 20:50:45 -07002172 case NES_CM_STATE_LISTENING:
2173 case NES_CM_STATE_UNKNOWN:
2174 case NES_CM_STATE_INITED:
2175 case NES_CM_STATE_CLOSED:
2176 ret = rem_ref_cm_node(cm_core, cm_node);
2177 break;
2178 case NES_CM_STATE_TSA:
2179 if (cm_node->send_entry)
2180 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2181 "send_entry=%p\n", cm_node->send_entry);
2182 ret = rem_ref_cm_node(cm_core, cm_node);
2183 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002184 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002185 return ret;
2186}
2187
2188
2189/**
2190 * recv_pkt - recv an ETHERNET packet, and process it through CM
2191 * node state machine
2192 */
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002193static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002194 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002195{
2196 struct nes_cm_node *cm_node = NULL;
2197 struct nes_cm_listener *listener = NULL;
2198 struct iphdr *iph;
2199 struct tcphdr *tcph;
2200 struct nes_cm_info nfo;
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002201 int skb_handled = 1;
Harvey Harrison03080e52009-01-10 21:45:42 -08002202 __be32 tmp_daddr, tmp_saddr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002203
Faisal Latif6492cdf2008-07-24 20:50:45 -07002204 if (!skb)
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002205 return 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002206 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002207 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002208 }
2209
2210 iph = (struct iphdr *)skb->data;
2211 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002212
2213 nfo.loc_addr = ntohl(iph->daddr);
2214 nfo.loc_port = ntohs(tcph->dest);
2215 nfo.rem_addr = ntohl(iph->saddr);
2216 nfo.rem_port = ntohs(tcph->source);
2217
Harvey Harrison03080e52009-01-10 21:45:42 -08002218 tmp_daddr = cpu_to_be32(iph->daddr);
2219 tmp_saddr = cpu_to_be32(iph->saddr);
2220
Harvey Harrison63779432008-10-31 00:56:00 -07002221 nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
Harvey Harrison03080e52009-01-10 21:45:42 -08002222 &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002223
Faisal Latif6492cdf2008-07-24 20:50:45 -07002224 do {
2225 cm_node = find_node(cm_core,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002226 nfo.rem_port, nfo.rem_addr,
2227 nfo.loc_port, nfo.loc_addr);
2228
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002229 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002230 /* Only type of packet accepted are for */
2231 /* the PASSIVE open (syn only) */
2232 if ((!tcph->syn) || (tcph->ack)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002233 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002234 break;
2235 }
2236 listener = find_listener(cm_core, nfo.loc_addr,
2237 nfo.loc_port,
2238 NES_CM_LISTENER_ACTIVE_STATE);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002239 if (!listener) {
2240 nfo.cm_id = NULL;
2241 nfo.conn_type = 0;
2242 nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2243 skb_handled = 0;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002244 break;
2245 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002246 nfo.cm_id = listener->cm_id;
2247 nfo.conn_type = listener->conn_type;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002248 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2249 listener);
2250 if (!cm_node) {
2251 nes_debug(NES_DBG_CM, "Unable to allocate "
2252 "node\n");
2253 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002254 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002255 dev_kfree_skb_any(skb);
2256 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002257 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002258 if (!tcph->rst && !tcph->fin) {
2259 cm_node->state = NES_CM_STATE_LISTENING;
2260 } else {
2261 cm_packets_dropped++;
2262 rem_ref_cm_node(cm_core, cm_node);
2263 dev_kfree_skb_any(skb);
2264 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002265 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002266 add_ref_cm_node(cm_node);
2267 } else if (cm_node->state == NES_CM_STATE_TSA) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002268 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002269 atomic_inc(&cm_accel_dropped_pkts);
2270 dev_kfree_skb_any(skb);
2271 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002272 }
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002273 skb_reset_network_header(skb);
2274 skb_set_transport_header(skb, sizeof(*tcph));
2275 skb->len = ntohs(iph->tot_len);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002276 process_packet(cm_node, skb, cm_core);
2277 rem_ref_cm_node(cm_core, cm_node);
2278 } while (0);
Faisal Latif4a14f6a2008-11-21 20:50:49 -06002279 return skb_handled;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002280}
2281
2282
2283/**
2284 * nes_cm_alloc_core - allocate a top level instance of a cm core
2285 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002286static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002287{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002288 struct nes_cm_core *cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002289
2290 /* setup the CM core */
2291 /* alloc top level core control structure */
2292 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2293 if (!cm_core)
2294 return NULL;
2295
2296 INIT_LIST_HEAD(&cm_core->connected_nodes);
2297 init_timer(&cm_core->tcp_timer);
2298 cm_core->tcp_timer.function = nes_cm_timer_tick;
2299
2300 cm_core->mtu = NES_CM_DEFAULT_MTU;
2301 cm_core->state = NES_CM_STATE_INITED;
2302 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2303
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002304 atomic_set(&cm_core->events_posted, 0);
2305
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002306 cm_core->api = &nes_cm_api;
2307
2308 spin_lock_init(&cm_core->ht_lock);
2309 spin_lock_init(&cm_core->listen_list_lock);
2310
2311 INIT_LIST_HEAD(&cm_core->listen_list.list);
2312
2313 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2314
2315 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2316 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2317 cm_core->post_event = nes_cm_post_event;
2318 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2319 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2320
2321 print_core(cm_core);
2322 return cm_core;
2323}
2324
2325
2326/**
2327 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2328 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002329static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002330{
2331 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2332
2333 if (!cm_core)
2334 return -EINVAL;
2335
2336 barrier();
2337
2338 if (timer_pending(&cm_core->tcp_timer)) {
2339 del_timer(&cm_core->tcp_timer);
2340 }
2341
2342 destroy_workqueue(cm_core->event_wq);
2343 destroy_workqueue(cm_core->disconn_wq);
2344 nes_debug(NES_DBG_CM, "\n");
2345 kfree(cm_core);
2346
2347 return 0;
2348}
2349
2350
2351/**
2352 * mini_cm_get
2353 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002354static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002355{
2356 return cm_core->state;
2357}
2358
2359
2360/**
2361 * mini_cm_set
2362 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002363static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002364{
2365 int ret = 0;
2366
2367 switch (type) {
Faisal Latif9d5ab132009-03-06 15:15:01 -08002368 case NES_CM_SET_PKT_SIZE:
2369 cm_core->mtu = value;
2370 break;
2371 case NES_CM_SET_FREE_PKT_Q_SIZE:
2372 cm_core->free_tx_pkt_max = value;
2373 break;
2374 default:
2375 /* unknown set option */
2376 ret = -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002377 }
2378
2379 return ret;
2380}
2381
2382
2383/**
2384 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2385 * successfully exchanged when this is called
2386 */
2387static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2388{
2389 int ret = 0;
2390
2391 if (!nesqp)
2392 return -EINVAL;
2393
2394 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2395 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2396 NES_QPCONTEXT_MISC_DROS);
2397
2398 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2399 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2400
2401 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2402
2403 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2404
2405 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2406 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2407
2408 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2409 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2410 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2411
2412 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2413 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2414 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2415
2416 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2417 nesqp->nesqp_context->ts_recent = 0;
2418 nesqp->nesqp_context->ts_age = 0;
2419 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2420 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2421 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2422 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2423 cm_node->tcp_cntxt.rcv_wscale);
2424 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2425 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2426 nesqp->nesqp_context->srtt = 0;
2427 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2428 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2429 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2430 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2431 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2432 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2433
2434 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2435 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2436 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2437 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2438 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2439 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2440 le32_to_cpu(nesqp->nesqp_context->misc));
2441 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2442 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2443 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2444
2445 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2446 cm_node->state = NES_CM_STATE_TSA;
2447
2448 return ret;
2449}
2450
2451
2452/**
2453 * nes_cm_disconn
2454 */
2455int nes_cm_disconn(struct nes_qp *nesqp)
2456{
2457 unsigned long flags;
2458
2459 spin_lock_irqsave(&nesqp->lock, flags);
2460 if (nesqp->disconn_pending == 0) {
2461 nesqp->disconn_pending++;
2462 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002463 /* init our disconnect work element, to */
2464 INIT_WORK(&nesqp->disconn_work, nes_disconnect_worker);
2465
2466 queue_work(g_cm_core->disconn_wq, &nesqp->disconn_work);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002467 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002468 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002469
2470 return 0;
2471}
2472
2473
2474/**
2475 * nes_disconnect_worker
2476 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002477static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002478{
2479 struct nes_qp *nesqp = container_of(work, struct nes_qp, disconn_work);
2480
2481 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2482 nesqp->last_aeq, nesqp->hwqp.qp_id);
2483 nes_cm_disconn_true(nesqp);
2484}
2485
2486
2487/**
2488 * nes_cm_disconn_true
2489 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002490static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002491{
2492 unsigned long flags;
2493 int ret = 0;
2494 struct iw_cm_id *cm_id;
2495 struct iw_cm_event cm_event;
2496 struct nes_vnic *nesvnic;
2497 u16 last_ae;
2498 u8 original_hw_tcp_state;
2499 u8 original_ibqp_state;
2500 u8 issued_disconnect_reset = 0;
2501
2502 if (!nesqp) {
2503 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2504 return -1;
2505 }
2506
2507 spin_lock_irqsave(&nesqp->lock, flags);
2508 cm_id = nesqp->cm_id;
2509 /* make sure we havent already closed this connection */
2510 if (!cm_id) {
2511 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2512 nesqp->hwqp.qp_id);
2513 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002514 return -1;
2515 }
2516
2517 nesvnic = to_nesvnic(nesqp->ibqp.device);
2518 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2519
2520 original_hw_tcp_state = nesqp->hw_tcp_state;
2521 original_ibqp_state = nesqp->ibqp_state;
2522 last_ae = nesqp->last_aeq;
2523
2524
2525 nes_debug(NES_DBG_CM, "set ibqp_state=%u\n", nesqp->ibqp_state);
2526
2527 if ((nesqp->cm_id) && (cm_id->event_handler)) {
2528 if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2529 ((original_ibqp_state == IB_QPS_RTS) &&
2530 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2531 atomic_inc(&cm_disconnects);
2532 cm_event.event = IW_CM_EVENT_DISCONNECT;
2533 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002534 cm_event.status = IW_CM_EVENT_STATUS_RESET;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002535 nes_debug(NES_DBG_CM, "Generating a CM "
2536 "Disconnect Event (status reset) for "
2537 "QP%u, cm_id = %p. \n",
2538 nesqp->hwqp.qp_id, cm_id);
2539 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002540 cm_event.status = IW_CM_EVENT_STATUS_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002541
2542 cm_event.local_addr = cm_id->local_addr;
2543 cm_event.remote_addr = cm_id->remote_addr;
2544 cm_event.private_data = NULL;
2545 cm_event.private_data_len = 0;
2546
Faisal Latif6492cdf2008-07-24 20:50:45 -07002547 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2548 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2549 "cm_id = %p, refcount = %u.\n",
2550 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2551 nesqp->hwqp.sq_tail, cm_id,
2552 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002553
2554 spin_unlock_irqrestore(&nesqp->lock, flags);
2555 ret = cm_id->event_handler(cm_id, &cm_event);
2556 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002557 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2558 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002559 spin_lock_irqsave(&nesqp->lock, flags);
2560 }
2561
2562 nesqp->disconn_pending = 0;
2563 /* There might have been another AE while the lock was released */
2564 original_hw_tcp_state = nesqp->hw_tcp_state;
2565 original_ibqp_state = nesqp->ibqp_state;
2566 last_ae = nesqp->last_aeq;
2567
2568 if ((issued_disconnect_reset == 0) && (nesqp->cm_id) &&
2569 ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2570 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2571 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2572 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2573 atomic_inc(&cm_closes);
2574 nesqp->cm_id = NULL;
2575 nesqp->in_disconnect = 0;
2576 spin_unlock_irqrestore(&nesqp->lock, flags);
2577 nes_disconnect(nesqp, 1);
2578
2579 cm_id->provider_data = nesqp;
2580 /* Send up the close complete event */
2581 cm_event.event = IW_CM_EVENT_CLOSE;
2582 cm_event.status = IW_CM_EVENT_STATUS_OK;
2583 cm_event.provider_data = cm_id->provider_data;
2584 cm_event.local_addr = cm_id->local_addr;
2585 cm_event.remote_addr = cm_id->remote_addr;
2586 cm_event.private_data = NULL;
2587 cm_event.private_data_len = 0;
2588
2589 ret = cm_id->event_handler(cm_id, &cm_event);
2590 if (ret) {
2591 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2592 }
2593
2594 cm_id->rem_ref(cm_id);
2595
2596 spin_lock_irqsave(&nesqp->lock, flags);
2597 if (nesqp->flush_issued == 0) {
2598 nesqp->flush_issued = 1;
2599 spin_unlock_irqrestore(&nesqp->lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002600 flush_wqes(nesvnic->nesdev, nesqp,
2601 NES_CQP_FLUSH_RQ, 1);
2602 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002603 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002604 } else {
2605 cm_id = nesqp->cm_id;
2606 spin_unlock_irqrestore(&nesqp->lock, flags);
2607 /* check to see if the inbound reset beat the outbound reset */
2608 if ((!cm_id) && (last_ae==NES_AEQE_AEID_RESET_SENT)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002609 nes_debug(NES_DBG_CM, "QP%u: Decing refcount "
2610 "due to inbound reset beating the "
2611 "outbound reset.\n", nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002612 }
2613 }
2614 } else {
2615 nesqp->disconn_pending = 0;
2616 spin_unlock_irqrestore(&nesqp->lock, flags);
2617 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002618
2619 return 0;
2620}
2621
2622
2623/**
2624 * nes_disconnect
2625 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002626static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002627{
2628 int ret = 0;
2629 struct nes_vnic *nesvnic;
2630 struct nes_device *nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002631 struct nes_ib_device *nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002632
2633 nesvnic = to_nesvnic(nesqp->ibqp.device);
2634 if (!nesvnic)
2635 return -EINVAL;
2636
2637 nesdev = nesvnic->nesdev;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002638 nesibdev = nesvnic->nesibdev;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002639
2640 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2641 atomic_read(&nesvnic->netdev->refcnt));
2642
2643 if (nesqp->active_conn) {
2644
2645 /* indicate this connection is NOT active */
2646 nesqp->active_conn = 0;
2647 } else {
2648 /* Need to free the Last Streaming Mode Message */
2649 if (nesqp->ietf_frame) {
Faisal Latifc12e56e2009-03-12 14:34:59 -07002650 if (nesqp->lsmm_mr)
2651 nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002652 pci_free_consistent(nesdev->pcidev,
2653 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2654 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2655 }
2656 }
2657
2658 /* close the CM node down if it is still active */
2659 if (nesqp->cm_node) {
2660 nes_debug(NES_DBG_CM, "Call close API\n");
2661
2662 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002663 }
2664
2665 return ret;
2666}
2667
2668
2669/**
2670 * nes_accept
2671 */
2672int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2673{
2674 u64 u64temp;
2675 struct ib_qp *ibqp;
2676 struct nes_qp *nesqp;
2677 struct nes_vnic *nesvnic;
2678 struct nes_device *nesdev;
2679 struct nes_cm_node *cm_node;
2680 struct nes_adapter *adapter;
2681 struct ib_qp_attr attr;
2682 struct iw_cm_event cm_event;
2683 struct nes_hw_qp_wqe *wqe;
2684 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002685 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002686 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002687 int passive_state;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002688 struct nes_ib_device *nesibdev;
2689 struct ib_mr *ibmr = NULL;
2690 struct ib_phys_buf ibphysbuf;
2691 struct nes_pd *nespd;
Don Wood7a5efb62009-04-08 14:21:02 -07002692 u64 tagged_offset;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002693
2694
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002695
2696 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2697 if (!ibqp)
2698 return -EINVAL;
2699
2700 /* get all our handles */
2701 nesqp = to_nesqp(ibqp);
2702 nesvnic = to_nesvnic(nesqp->ibqp.device);
2703 nesdev = nesvnic->nesdev;
2704 adapter = nesdev->nesadapter;
2705
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002706 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002707 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2708 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2709 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002710
2711 /* associate the node with the QP */
2712 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002713 cm_node->nesqp = nesqp;
2714 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002715
Faisal Latif6492cdf2008-07-24 20:50:45 -07002716 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2717 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002718 atomic_inc(&cm_accepts);
2719
2720 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2721 atomic_read(&nesvnic->netdev->refcnt));
2722
Faisal Latif6492cdf2008-07-24 20:50:45 -07002723 /* allocate the ietf frame and space for private data */
2724 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2725 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2726 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002727
Faisal Latif6492cdf2008-07-24 20:50:45 -07002728 if (!nesqp->ietf_frame) {
2729 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2730 "data\n");
2731 return -ENOMEM;
2732 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002733
2734
Faisal Latif6492cdf2008-07-24 20:50:45 -07002735 /* setup the MPA frame */
2736 nesqp->private_data_len = conn_param->private_data_len;
2737 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002738
Faisal Latif6492cdf2008-07-24 20:50:45 -07002739 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2740 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002741
Faisal Latif6492cdf2008-07-24 20:50:45 -07002742 nesqp->ietf_frame->priv_data_len =
2743 cpu_to_be16(conn_param->private_data_len);
2744 nesqp->ietf_frame->rev = mpa_version;
2745 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002746
Faisal Latif6492cdf2008-07-24 20:50:45 -07002747 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2748 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002749
Faisal Latif6492cdf2008-07-24 20:50:45 -07002750 if (cm_id->remote_addr.sin_addr.s_addr !=
2751 cm_id->local_addr.sin_addr.s_addr) {
2752 u64temp = (unsigned long)nesqp;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002753 nesibdev = nesvnic->nesibdev;
2754 nespd = nesqp->nespd;
2755 ibphysbuf.addr = nesqp->ietf_frame_pbase;
2756 ibphysbuf.size = conn_param->private_data_len +
2757 sizeof(struct ietf_mpa_frame);
Don Wood7a5efb62009-04-08 14:21:02 -07002758 tagged_offset = (u64)(unsigned long)nesqp->ietf_frame;
Faisal Latifc12e56e2009-03-12 14:34:59 -07002759 ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
2760 &ibphysbuf, 1,
2761 IB_ACCESS_LOCAL_WRITE,
Don Wood7a5efb62009-04-08 14:21:02 -07002762 &tagged_offset);
Faisal Latifc12e56e2009-03-12 14:34:59 -07002763 if (!ibmr) {
2764 nes_debug(NES_DBG_CM, "Unable to register memory region"
2765 "for lSMM for cm_node = %p \n",
2766 cm_node);
2767 return -ENOMEM;
2768 }
2769
2770 ibmr->pd = &nespd->ibpd;
2771 ibmr->device = nespd->ibpd.device;
2772 nesqp->lsmm_mr = ibmr;
2773
Faisal Latif6492cdf2008-07-24 20:50:45 -07002774 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2775 set_wqe_64bit_value(wqe->wqe_words,
2776 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2777 u64temp);
2778 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2779 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2780 NES_IWARP_SQ_WQE_WRPDU);
2781 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2782 cpu_to_le32(conn_param->private_data_len +
2783 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002784 set_wqe_64bit_value(wqe->wqe_words,
2785 NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
Don Wood7a5efb62009-04-08 14:21:02 -07002786 (u64)(unsigned long)nesqp->ietf_frame);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002787 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2788 cpu_to_le32(conn_param->private_data_len +
2789 sizeof(struct ietf_mpa_frame));
Faisal Latifc12e56e2009-03-12 14:34:59 -07002790 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = ibmr->lkey;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002791
Faisal Latif6492cdf2008-07-24 20:50:45 -07002792 nesqp->nesqp_context->ird_ord_sizes |=
2793 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2794 NES_QPCONTEXT_ORDIRD_WRPDU);
2795 } else {
2796 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif9d5ab132009-03-06 15:15:01 -08002797 cpu_to_le32(NES_QPCONTEXT_ORDIRD_WRPDU);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002798 }
2799 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002800
2801
2802 /* Cache the cm_id in the qp */
2803 nesqp->cm_id = cm_id;
2804 cm_node->cm_id = cm_id;
2805
2806 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2807 cm_id->provider_data = nesqp;
2808 nesqp->active_conn = 0;
2809
Faisal Latif6492cdf2008-07-24 20:50:45 -07002810 if (cm_node->state == NES_CM_STATE_TSA)
2811 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2812 cm_node);
2813
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002814 nes_cm_init_tsa_conn(nesqp, cm_node);
2815
Faisal Latif6492cdf2008-07-24 20:50:45 -07002816 nesqp->nesqp_context->tcpPorts[0] =
2817 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2818 nesqp->nesqp_context->tcpPorts[1] =
2819 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2820
2821 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2822 nesqp->nesqp_context->ip0 =
2823 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2824 else
2825 nesqp->nesqp_context->ip0 =
2826 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002827
2828 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002829 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2830 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002831
Faisal Latif6492cdf2008-07-24 20:50:45 -07002832 nesqp->nesqp_context->arp_index_vlan |=
2833 cpu_to_le32(nes_arp_table(nesdev,
2834 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002835 NES_ARP_RESOLVE) << 16);
2836
2837 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002838 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002839
2840 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2841
2842 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002843 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2844 nesqp->nesqp_context->ird_ord_sizes |=
2845 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002846
2847 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002848 nes_quad.DstIpAdrIndex =
2849 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2850 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2851 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2852 else
2853 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2854 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2855 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002856
2857 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06002858 crc_value = get_crc_value(&nes_quad);
2859 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002860 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07002861 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002862
2863 nesqp->hte_index &= adapter->hte_index_mask;
2864 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2865
2866 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2867
Faisal Latif6492cdf2008-07-24 20:50:45 -07002868 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2869 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2870 "private data length=%zu.\n", nesqp->hwqp.qp_id,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002871 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2872 ntohs(cm_id->remote_addr.sin_port),
2873 ntohl(cm_id->local_addr.sin_addr.s_addr),
2874 ntohs(cm_id->local_addr.sin_port),
2875 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2876 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002877 conn_param->private_data_len +
2878 sizeof(struct ietf_mpa_frame));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002879
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002880
Coly Li73ac36e2009-01-07 18:09:16 -08002881 /* notify OF layer that accept event was successful */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002882 cm_id->add_ref(cm_id);
2883
2884 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2885 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2886 cm_event.provider_data = (void *)nesqp;
2887 cm_event.local_addr = cm_id->local_addr;
2888 cm_event.remote_addr = cm_id->remote_addr;
2889 cm_event.private_data = NULL;
2890 cm_event.private_data_len = 0;
2891 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002892 attr.qp_state = IB_QPS_RTS;
2893 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002894 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002895 cm_node->loopbackpartner->mpa_frame_size =
2896 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002897 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002898 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2899 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002900 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2901 }
2902 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002903 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2904 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002905
Faisal Latif183ecfa2008-11-21 20:50:46 -06002906 passive_state = atomic_add_return(1, &cm_node->passive_state);
2907 if (passive_state == NES_SEND_RESET_EVENT)
2908 create_event(cm_node, NES_CM_EVENT_RESET);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002909 return 0;
2910}
2911
2912
2913/**
2914 * nes_reject
2915 */
2916int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2917{
2918 struct nes_cm_node *cm_node;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002919 struct nes_cm_node *loopback;
2920
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002921 struct nes_cm_core *cm_core;
2922
2923 atomic_inc(&cm_rejects);
2924 cm_node = (struct nes_cm_node *) cm_id->provider_data;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002925 loopback = cm_node->loopbackpartner;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002926 cm_core = cm_node->cm_core;
Faisal Latif9d5ab132009-03-06 15:15:01 -08002927 cm_node->cm_id = cm_id;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002928 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2929
Faisal Latif9d5ab132009-03-06 15:15:01 -08002930 if (cm_node->mpa_frame_size > MAX_CM_BUFFER)
2931 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002932
Faisal Latif9d5ab132009-03-06 15:15:01 -08002933 strcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP);
2934 if (loopback) {
2935 memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
2936 loopback->mpa_frame.priv_data_len = pdata_len;
2937 loopback->mpa_frame_size = sizeof(struct ietf_mpa_frame) +
2938 pdata_len;
2939 } else {
2940 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2941 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2942 }
2943
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002944 cm_node->mpa_frame.rev = mpa_version;
2945 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2946
Faisal Latif9d5ab132009-03-06 15:15:01 -08002947 return cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002948}
2949
2950
2951/**
2952 * nes_connect
2953 * setup and launch cm connect node
2954 */
2955int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2956{
2957 struct ib_qp *ibqp;
2958 struct nes_qp *nesqp;
2959 struct nes_vnic *nesvnic;
2960 struct nes_device *nesdev;
2961 struct nes_cm_node *cm_node;
2962 struct nes_cm_info cm_info;
Faisal Latif53094c32009-04-27 13:37:34 -07002963 int apbvt_set = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002964
2965 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2966 if (!ibqp)
2967 return -EINVAL;
2968 nesqp = to_nesqp(ibqp);
2969 if (!nesqp)
2970 return -EINVAL;
2971 nesvnic = to_nesvnic(nesqp->ibqp.device);
2972 if (!nesvnic)
2973 return -EINVAL;
2974 nesdev = nesvnic->nesdev;
2975 if (!nesdev)
2976 return -EINVAL;
2977
Faisal Latif6492cdf2008-07-24 20:50:45 -07002978 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
2979 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
2980 ntohl(nesvnic->local_ipaddr),
2981 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2982 ntohs(cm_id->remote_addr.sin_port),
2983 ntohl(cm_id->local_addr.sin_addr.s_addr),
2984 ntohs(cm_id->local_addr.sin_port));
2985
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002986 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002987 nesqp->active_conn = 1;
2988
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002989 /* cache the cm_id in the qp */
2990 nesqp->cm_id = cm_id;
2991
2992 cm_id->provider_data = nesqp;
2993
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002994 nesqp->private_data_len = conn_param->private_data_len;
2995 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
2996 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002997 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
2998 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002999
Faisal Latif6492cdf2008-07-24 20:50:45 -07003000 if (cm_id->local_addr.sin_addr.s_addr !=
Faisal Latif53094c32009-04-27 13:37:34 -07003001 cm_id->remote_addr.sin_addr.s_addr) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003002 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003003 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Faisal Latif53094c32009-04-27 13:37:34 -07003004 apbvt_set = 1;
3005 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003006
3007 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003008 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3009 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3010 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3011 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003012 cm_info.cm_id = cm_id;
3013 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3014
3015 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003016
3017 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003018 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3019 conn_param->private_data_len, (void *)conn_param->private_data,
3020 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003021 if (!cm_node) {
Faisal Latif53094c32009-04-27 13:37:34 -07003022 if (apbvt_set)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003023 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07003024 PCI_FUNC(nesdev->pcidev->devfn),
3025 NES_MANAGE_APBVT_DEL);
3026
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003027 cm_id->rem_ref(cm_id);
3028 return -ENOMEM;
3029 }
3030
Faisal Latif53094c32009-04-27 13:37:34 -07003031 cm_node->apbvt_set = apbvt_set;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003032 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003033 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07003034 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003035
3036 return 0;
3037}
3038
3039
3040/**
3041 * nes_create_listen
3042 */
3043int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3044{
3045 struct nes_vnic *nesvnic;
3046 struct nes_cm_listener *cm_node;
3047 struct nes_cm_info cm_info;
3048 struct nes_adapter *adapter;
3049 int err;
3050
3051
3052 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3053 cm_id, ntohs(cm_id->local_addr.sin_port));
3054
3055 nesvnic = to_nesvnic(cm_id->device);
3056 if (!nesvnic)
3057 return -EINVAL;
3058 adapter = nesvnic->nesdev->nesadapter;
3059 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3060 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3061
3062 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3063 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3064
3065 /* setup listen params in our api call struct */
3066 cm_info.loc_addr = nesvnic->local_ipaddr;
3067 cm_info.loc_port = cm_id->local_addr.sin_port;
3068 cm_info.backlog = backlog;
3069 cm_info.cm_id = cm_id;
3070
3071 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
3072
3073
3074 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3075 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003076 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003077 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003078 return -ENOMEM;
3079 }
3080
3081 cm_id->provider_data = cm_node;
3082
3083 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003084 err = nes_manage_apbvt(nesvnic,
3085 ntohs(cm_id->local_addr.sin_port),
3086 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3087 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003088 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003089 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3090 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003091 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3092 return err;
3093 }
3094 cm_listens_created++;
3095 }
3096
3097 cm_id->add_ref(cm_id);
3098 cm_id->provider_data = (void *)cm_node;
3099
3100
3101 return 0;
3102}
3103
3104
3105/**
3106 * nes_destroy_listen
3107 */
3108int nes_destroy_listen(struct iw_cm_id *cm_id)
3109{
3110 if (cm_id->provider_data)
3111 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3112 else
3113 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3114
3115 cm_id->rem_ref(cm_id);
3116
3117 return 0;
3118}
3119
3120
3121/**
3122 * nes_cm_recv
3123 */
3124int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3125{
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003126 int rc = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003127 cm_packets_received++;
3128 if ((g_cm_core) && (g_cm_core->api)) {
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003129 rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003130 } else {
3131 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3132 " cm is not setup properly.\n");
3133 }
3134
Faisal Latif4a14f6a2008-11-21 20:50:49 -06003135 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003136}
3137
3138
3139/**
3140 * nes_cm_start
3141 * Start and init a cm core module
3142 */
3143int nes_cm_start(void)
3144{
3145 nes_debug(NES_DBG_CM, "\n");
3146 /* create the primary CM core, pass this handle to subsequent core inits */
3147 g_cm_core = nes_cm_alloc_core();
3148 if (g_cm_core) {
3149 return 0;
3150 } else {
3151 return -ENOMEM;
3152 }
3153}
3154
3155
3156/**
3157 * nes_cm_stop
3158 * stop and dealloc all cm core instances
3159 */
3160int nes_cm_stop(void)
3161{
3162 g_cm_core->api->destroy_cm_core(g_cm_core);
3163 return 0;
3164}
3165
3166
3167/**
3168 * cm_event_connected
3169 * handle a connected event, setup QPs and HW
3170 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003171static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003172{
3173 u64 u64temp;
3174 struct nes_qp *nesqp;
3175 struct nes_vnic *nesvnic;
3176 struct nes_device *nesdev;
3177 struct nes_cm_node *cm_node;
3178 struct nes_adapter *nesadapter;
3179 struct ib_qp_attr attr;
3180 struct iw_cm_id *cm_id;
3181 struct iw_cm_event cm_event;
3182 struct nes_hw_qp_wqe *wqe;
3183 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003184 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003185 int ret;
3186
3187 /* get all our handles */
3188 cm_node = event->cm_node;
3189 cm_id = cm_node->cm_id;
3190 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3191 nesqp = (struct nes_qp *)cm_id->provider_data;
3192 nesvnic = to_nesvnic(nesqp->ibqp.device);
3193 nesdev = nesvnic->nesdev;
3194 nesadapter = nesdev->nesadapter;
3195
3196 if (nesqp->destroyed) {
3197 return;
3198 }
3199 atomic_inc(&cm_connecteds);
3200 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3201 " local port 0x%04X. jiffies = %lu.\n",
3202 nesqp->hwqp.qp_id,
3203 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3204 ntohs(cm_id->remote_addr.sin_port),
3205 ntohs(cm_id->local_addr.sin_port),
3206 jiffies);
3207
3208 nes_cm_init_tsa_conn(nesqp, cm_node);
3209
3210 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003211 nesqp->nesqp_context->tcpPorts[0] =
3212 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3213 nesqp->nesqp_context->tcpPorts[1] =
3214 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3215 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3216 nesqp->nesqp_context->ip0 =
3217 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3218 else
3219 nesqp->nesqp_context->ip0 =
3220 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003221
3222 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003223 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3224 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003225 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003226 nes_arp_table(nesdev,
3227 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003228 NULL, NES_ARP_RESOLVE) << 16);
3229 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3230 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3231 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3232 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003233 cpu_to_le32((u32)1 <<
3234 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003235
3236 /* Adjust tail for not having a LSMM */
3237 nesqp->hwqp.sq_tail = 1;
3238
3239#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003240 if (cm_node->send_write0) {
3241 nes_debug(NES_DBG_CM, "Sending first write.\n");
3242 wqe = &nesqp->hwqp.sq_vbase[0];
3243 u64temp = (unsigned long)nesqp;
3244 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3245 set_wqe_64bit_value(wqe->wqe_words,
3246 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3247 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3248 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3249 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3250 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3251 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3252 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3253 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003254
Faisal Latif6492cdf2008-07-24 20:50:45 -07003255 /* use the reserved spot on the WQ for the extra first WQE */
3256 nesqp->nesqp_context->ird_ord_sizes &=
3257 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3258 NES_QPCONTEXT_ORDIRD_WRPDU |
3259 NES_QPCONTEXT_ORDIRD_ALSMM));
3260 nesqp->skip_lsmm = 1;
3261 nesqp->hwqp.sq_tail = 0;
3262 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3263 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3264 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003265#endif
3266
3267 memset(&nes_quad, 0, sizeof(nes_quad));
3268
Faisal Latif6492cdf2008-07-24 20:50:45 -07003269 nes_quad.DstIpAdrIndex =
3270 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3271 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3272 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3273 else
3274 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003275 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3276 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3277
3278 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003279 crc_value = get_crc_value(&nes_quad);
3280 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003281 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3282 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3283
3284 nesqp->hte_index &= nesadapter->hte_index_mask;
3285 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3286
3287 nesqp->ietf_frame = &cm_node->mpa_frame;
3288 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3289 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3290
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003291 /* notify OF layer we successfully created the requested connection */
3292 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3293 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3294 cm_event.provider_data = cm_id->provider_data;
3295 cm_event.local_addr.sin_family = AF_INET;
3296 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3297 cm_event.remote_addr = cm_id->remote_addr;
3298
Faisal Latif6492cdf2008-07-24 20:50:45 -07003299 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3300 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003301
3302 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3303 ret = cm_id->event_handler(cm_id, &cm_event);
3304 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3305
3306 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003307 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3308 "ret=%d\n", __func__, __LINE__, ret);
3309 attr.qp_state = IB_QPS_RTS;
3310 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003311
Faisal Latif6492cdf2008-07-24 20:50:45 -07003312 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3313 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003314
3315 return;
3316}
3317
3318
3319/**
3320 * cm_event_connect_error
3321 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003322static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003323{
3324 struct nes_qp *nesqp;
3325 struct iw_cm_id *cm_id;
3326 struct iw_cm_event cm_event;
3327 /* struct nes_cm_info cm_info; */
3328 int ret;
3329
3330 if (!event->cm_node)
3331 return;
3332
3333 cm_id = event->cm_node->cm_id;
3334 if (!cm_id) {
3335 return;
3336 }
3337
3338 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3339 nesqp = cm_id->provider_data;
3340
3341 if (!nesqp) {
3342 return;
3343 }
3344
3345 /* notify OF layer about this connection error event */
3346 /* cm_id->rem_ref(cm_id); */
3347 nesqp->cm_id = NULL;
3348 cm_id->provider_data = NULL;
3349 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3350 cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
3351 cm_event.provider_data = cm_id->provider_data;
3352 cm_event.local_addr = cm_id->local_addr;
3353 cm_event.remote_addr = cm_id->remote_addr;
3354 cm_event.private_data = NULL;
3355 cm_event.private_data_len = 0;
3356
Faisal Latif6492cdf2008-07-24 20:50:45 -07003357 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3358 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3359 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003360
3361 ret = cm_id->event_handler(cm_id, &cm_event);
3362 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3363 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003364 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3365 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003366 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003367
Faisal Latif6492cdf2008-07-24 20:50:45 -07003368 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003369 return;
3370}
3371
3372
3373/**
3374 * cm_event_reset
3375 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003376static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003377{
3378 struct nes_qp *nesqp;
3379 struct iw_cm_id *cm_id;
3380 struct iw_cm_event cm_event;
3381 /* struct nes_cm_info cm_info; */
3382 int ret;
3383
3384 if (!event->cm_node)
3385 return;
3386
3387 if (!event->cm_node->cm_id)
3388 return;
3389
3390 cm_id = event->cm_node->cm_id;
3391
3392 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3393 nesqp = cm_id->provider_data;
3394
3395 nesqp->cm_id = NULL;
3396 /* cm_id->provider_data = NULL; */
3397 cm_event.event = IW_CM_EVENT_DISCONNECT;
3398 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3399 cm_event.provider_data = cm_id->provider_data;
3400 cm_event.local_addr = cm_id->local_addr;
3401 cm_event.remote_addr = cm_id->remote_addr;
3402 cm_event.private_data = NULL;
3403 cm_event.private_data_len = 0;
3404
3405 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003406 cm_id->add_ref(cm_id);
3407 atomic_inc(&cm_closes);
3408 cm_event.event = IW_CM_EVENT_CLOSE;
3409 cm_event.status = IW_CM_EVENT_STATUS_OK;
3410 cm_event.provider_data = cm_id->provider_data;
3411 cm_event.local_addr = cm_id->local_addr;
3412 cm_event.remote_addr = cm_id->remote_addr;
3413 cm_event.private_data = NULL;
3414 cm_event.private_data_len = 0;
3415 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3416 ret = cm_id->event_handler(cm_id, &cm_event);
3417
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003418 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3419
3420
3421 /* notify OF layer about this connection error event */
3422 cm_id->rem_ref(cm_id);
3423
3424 return;
3425}
3426
3427
3428/**
3429 * cm_event_mpa_req
3430 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003431static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003432{
3433 struct iw_cm_id *cm_id;
3434 struct iw_cm_event cm_event;
3435 int ret;
3436 struct nes_cm_node *cm_node;
3437
3438 cm_node = event->cm_node;
3439 if (!cm_node)
3440 return;
3441 cm_id = cm_node->cm_id;
3442
3443 atomic_inc(&cm_connect_reqs);
3444 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3445 cm_node, cm_id, jiffies);
3446
3447 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3448 cm_event.status = IW_CM_EVENT_STATUS_OK;
3449 cm_event.provider_data = (void *)cm_node;
3450
3451 cm_event.local_addr.sin_family = AF_INET;
3452 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3453 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3454
3455 cm_event.remote_addr.sin_family = AF_INET;
3456 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3457 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
Faisal Latif9d5ab132009-03-06 15:15:01 -08003458 cm_event.private_data = cm_node->mpa_frame_buf;
3459 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003460
3461 ret = cm_id->event_handler(cm_id, &cm_event);
3462 if (ret)
Faisal Latif9d5ab132009-03-06 15:15:01 -08003463 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3464 __func__, __LINE__, ret);
3465 return;
3466}
3467
3468
3469static void cm_event_mpa_reject(struct nes_cm_event *event)
3470{
3471 struct iw_cm_id *cm_id;
3472 struct iw_cm_event cm_event;
3473 struct nes_cm_node *cm_node;
3474 int ret;
3475
3476 cm_node = event->cm_node;
3477 if (!cm_node)
3478 return;
3479 cm_id = cm_node->cm_id;
3480
3481 atomic_inc(&cm_connect_reqs);
3482 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3483 cm_node, cm_id, jiffies);
3484
3485 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3486 cm_event.status = -ECONNREFUSED;
3487 cm_event.provider_data = cm_id->provider_data;
3488
3489 cm_event.local_addr.sin_family = AF_INET;
3490 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3491 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3492
3493 cm_event.remote_addr.sin_family = AF_INET;
3494 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3495 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3496
3497 cm_event.private_data = cm_node->mpa_frame_buf;
3498 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3499
3500 nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3501 "remove_addr=%08x\n",
3502 cm_event.local_addr.sin_addr.s_addr,
3503 cm_event.remote_addr.sin_addr.s_addr);
3504
3505 ret = cm_id->event_handler(cm_id, &cm_event);
3506 if (ret)
3507 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003508 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003509
3510 return;
3511}
3512
3513
3514static void nes_cm_event_handler(struct work_struct *);
3515
3516/**
3517 * nes_cm_post_event
3518 * post an event to the cm event handler
3519 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003520static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003521{
3522 atomic_inc(&event->cm_node->cm_core->events_posted);
3523 add_ref_cm_node(event->cm_node);
3524 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3525 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003526 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3527 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003528
3529 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3530
3531 nes_debug(NES_DBG_CM, "Exit\n");
3532 return 0;
3533}
3534
3535
3536/**
3537 * nes_cm_event_handler
3538 * worker function to handle cm events
3539 * will free instance of nes_cm_event
3540 */
3541static void nes_cm_event_handler(struct work_struct *work)
3542{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003543 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3544 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003545 struct nes_cm_core *cm_core;
3546
Faisal Latif6492cdf2008-07-24 20:50:45 -07003547 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003548 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003549
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003550 cm_core = event->cm_node->cm_core;
3551 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003552 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003553
3554 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003555 case NES_CM_EVENT_MPA_REQ:
3556 cm_event_mpa_req(event);
3557 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3558 event->cm_node);
3559 break;
3560 case NES_CM_EVENT_RESET:
3561 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3562 event->cm_node);
3563 cm_event_reset(event);
3564 break;
3565 case NES_CM_EVENT_CONNECTED:
3566 if ((!event->cm_node->cm_id) ||
3567 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003568 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003569 cm_event_connected(event);
3570 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3571 break;
Faisal Latif9d5ab132009-03-06 15:15:01 -08003572 case NES_CM_EVENT_MPA_REJECT:
3573 if ((!event->cm_node->cm_id) ||
3574 (event->cm_node->state == NES_CM_STATE_TSA))
3575 break;
3576 cm_event_mpa_reject(event);
3577 nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3578 break;
3579
Faisal Latif6492cdf2008-07-24 20:50:45 -07003580 case NES_CM_EVENT_ABORTED:
3581 if ((!event->cm_node->cm_id) ||
3582 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003583 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003584 cm_event_connect_error(event);
3585 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3586 break;
3587 case NES_CM_EVENT_DROPPED_PKT:
3588 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3589 break;
3590 default:
3591 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3592 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003593 }
3594
3595 atomic_dec(&cm_core->events_posted);
3596 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3597 rem_ref_cm_node(cm_core, event->cm_node);
3598 kfree(event);
3599
3600 return;
3601}