blob: 257d994ec7b5ef57422fe0b07a947bdee4e19291 [file] [log] [blame]
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001/*
2 * Copyright (c) 2006 - 2008 NetEffect, Inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34
35#define TCPOPT_TIMESTAMP 8
36
37#include <asm/atomic.h>
38#include <linux/skbuff.h>
39#include <linux/ip.h>
40#include <linux/tcp.h>
41#include <linux/init.h>
42#include <linux/if_arp.h>
Chien Tungf2b2b592008-03-20 19:55:30 -050043#include <linux/if_vlan.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080044#include <linux/notifier.h>
45#include <linux/net.h>
46#include <linux/types.h>
47#include <linux/timer.h>
48#include <linux/time.h>
49#include <linux/delay.h>
50#include <linux/etherdevice.h>
51#include <linux/netdevice.h>
52#include <linux/random.h>
53#include <linux/list.h>
54#include <linux/threads.h>
Bob Sharp7191a0a2008-10-03 12:21:19 -070055#include <net/arp.h>
Glenn Streiff3c2d7742008-02-04 20:20:45 -080056#include <net/neighbour.h>
57#include <net/route.h>
58#include <net/ip_fib.h>
59
60#include "nes.h"
61
62u32 cm_packets_sent;
63u32 cm_packets_bounced;
64u32 cm_packets_dropped;
65u32 cm_packets_retrans;
66u32 cm_packets_created;
67u32 cm_packets_received;
68u32 cm_listens_created;
69u32 cm_listens_destroyed;
70u32 cm_backlog_drops;
71atomic_t cm_loopbacks;
72atomic_t cm_nodes_created;
73atomic_t cm_nodes_destroyed;
74atomic_t cm_accel_dropped_pkts;
75atomic_t cm_resets_recvd;
76
Faisal Latif6492cdf2008-07-24 20:50:45 -070077static inline int mini_cm_accelerated(struct nes_cm_core *,
78 struct nes_cm_node *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080079static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070080 struct nes_vnic *, struct nes_cm_info *);
Glenn Streiff3c2d7742008-02-04 20:20:45 -080081static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070082static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *,
Faisal Latif6492cdf2008-07-24 20:50:45 -070083 struct nes_vnic *, u16, void *, struct nes_cm_info *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070084static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
Faisal Latif6492cdf2008-07-24 20:50:45 -070085static int mini_cm_accept(struct nes_cm_core *, struct ietf_mpa_frame *,
86 struct nes_cm_node *);
87static int mini_cm_reject(struct nes_cm_core *, struct ietf_mpa_frame *,
88 struct nes_cm_node *);
89static void mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *,
90 struct sk_buff *);
Roland Dreier1a855fb2008-04-16 21:01:09 -070091static int mini_cm_dealloc_core(struct nes_cm_core *);
92static int mini_cm_get(struct nes_cm_core *);
93static int mini_cm_set(struct nes_cm_core *, u32, u32);
Faisal Latif6492cdf2008-07-24 20:50:45 -070094
95static struct sk_buff *form_cm_frame(struct sk_buff *, struct nes_cm_node *,
96 void *, u32, void *, u32, u8);
97static struct sk_buff *get_free_pkt(struct nes_cm_node *cm_node);
98static 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 *);
107static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
108static int send_reset(struct nes_cm_node *, struct sk_buff *);
109static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
Roland Dreier1a855fb2008-04-16 21:01:09 -0700110static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700111static void process_packet(struct nes_cm_node *, struct sk_buff *,
112 struct nes_cm_core *);
113
114static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
115static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
116static void cleanup_retrans_entry(struct nes_cm_node *);
117static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *,
118 enum nes_cm_event_type);
119static 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 *);
128
129static void print_core(struct nes_cm_core *core);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800130
131/* External CM API Interface */
132/* instance of function pointers for client API */
133/* set address of this instance to cm_core->cm_ops at cm_core alloc */
134static struct nes_cm_ops nes_cm_api = {
135 mini_cm_accelerated,
136 mini_cm_listen,
137 mini_cm_del_listen,
138 mini_cm_connect,
139 mini_cm_close,
140 mini_cm_accept,
141 mini_cm_reject,
142 mini_cm_recv_pkt,
143 mini_cm_dealloc_core,
144 mini_cm_get,
145 mini_cm_set
146};
147
Roland Dreier1a855fb2008-04-16 21:01:09 -0700148static struct nes_cm_core *g_cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800149
150atomic_t cm_connects;
151atomic_t cm_accepts;
152atomic_t cm_disconnects;
153atomic_t cm_closes;
154atomic_t cm_connecteds;
155atomic_t cm_connect_reqs;
156atomic_t cm_rejects;
157
158
159/**
160 * create_event
161 */
162static struct nes_cm_event *create_event(struct nes_cm_node *cm_node,
163 enum nes_cm_event_type type)
164{
165 struct nes_cm_event *event;
166
167 if (!cm_node->cm_id)
168 return NULL;
169
170 /* allocate an empty event */
171 event = kzalloc(sizeof(*event), GFP_ATOMIC);
172
173 if (!event)
174 return NULL;
175
176 event->type = type;
177 event->cm_node = cm_node;
178 event->cm_info.rem_addr = cm_node->rem_addr;
179 event->cm_info.loc_addr = cm_node->loc_addr;
180 event->cm_info.rem_port = cm_node->rem_port;
181 event->cm_info.loc_port = cm_node->loc_port;
182 event->cm_info.cm_id = cm_node->cm_id;
183
Faisal Latif6492cdf2008-07-24 20:50:45 -0700184 nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
185 "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
186 cm_node, event, type, event->cm_info.loc_addr,
187 event->cm_info.loc_port, event->cm_info.rem_addr,
188 event->cm_info.rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800189
190 nes_cm_post_event(event);
191 return event;
192}
193
194
195/**
196 * send_mpa_request
197 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700198static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800199{
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800200 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800201 if (!skb) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700202 nes_debug(NES_DBG_CM, "skb set to NULL\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800203 return -1;
204 }
205
206 /* send an MPA Request frame */
207 form_cm_frame(skb, cm_node, NULL, 0, &cm_node->mpa_frame,
208 cm_node->mpa_frame_size, SET_ACK);
209
210 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700211 if (ret < 0)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800212 return ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800213
214 return 0;
215}
216
217
218/**
219 * recv_mpa - process a received TCP pkt, we are expecting an
220 * IETF MPA frame
221 */
222static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 len)
223{
224 struct ietf_mpa_frame *mpa_frame;
225
226 /* assume req frame is in tcp data payload */
227 if (len < sizeof(struct ietf_mpa_frame)) {
228 nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
229 return -1;
230 }
231
232 mpa_frame = (struct ietf_mpa_frame *)buffer;
233 cm_node->mpa_frame_size = ntohs(mpa_frame->priv_data_len);
234
235 if (cm_node->mpa_frame_size + sizeof(struct ietf_mpa_frame) != len) {
236 nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
237 " complete (%x + %x != %x)\n",
238 cm_node->mpa_frame_size, (u32)sizeof(struct ietf_mpa_frame), len);
239 return -1;
240 }
241
242 /* copy entire MPA frame to our cm_node's frame */
243 memcpy(cm_node->mpa_frame_buf, buffer + sizeof(struct ietf_mpa_frame),
244 cm_node->mpa_frame_size);
245
246 return 0;
247}
248
249
250/**
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800251 * form_cm_frame - get a free packet and build empty frame Use
252 * node info to build.
253 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700254static struct sk_buff *form_cm_frame(struct sk_buff *skb,
255 struct nes_cm_node *cm_node, void *options, u32 optionsize,
256 void *data, u32 datasize, u8 flags)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800257{
258 struct tcphdr *tcph;
259 struct iphdr *iph;
260 struct ethhdr *ethh;
261 u8 *buf;
262 u16 packetsize = sizeof(*iph);
263
264 packetsize += sizeof(*tcph);
265 packetsize += optionsize + datasize;
266
267 memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
268
269 skb->len = 0;
270 buf = skb_put(skb, packetsize + ETH_HLEN);
271
272 ethh = (struct ethhdr *) buf;
273 buf += ETH_HLEN;
274
275 iph = (struct iphdr *)buf;
276 buf += sizeof(*iph);
277 tcph = (struct tcphdr *)buf;
278 skb_reset_mac_header(skb);
279 skb_set_network_header(skb, ETH_HLEN);
280 skb_set_transport_header(skb, ETH_HLEN+sizeof(*iph));
281 buf += sizeof(*tcph);
282
283 skb->ip_summed = CHECKSUM_PARTIAL;
284 skb->protocol = htons(0x800);
285 skb->data_len = 0;
286 skb->mac_len = ETH_HLEN;
287
288 memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
289 memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
290 ethh->h_proto = htons(0x0800);
291
292 iph->version = IPVERSION;
293 iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
294 iph->tos = 0;
295 iph->tot_len = htons(packetsize);
296 iph->id = htons(++cm_node->tcp_cntxt.loc_id);
297
298 iph->frag_off = htons(0x4000);
299 iph->ttl = 0x40;
300 iph->protocol = 0x06; /* IPPROTO_TCP */
301
302 iph->saddr = htonl(cm_node->loc_addr);
303 iph->daddr = htonl(cm_node->rem_addr);
304
305 tcph->source = htons(cm_node->loc_port);
306 tcph->dest = htons(cm_node->rem_port);
307 tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
308
309 if (flags & SET_ACK) {
310 cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
311 tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
312 tcph->ack = 1;
313 } else
314 tcph->ack_seq = 0;
315
316 if (flags & SET_SYN) {
317 cm_node->tcp_cntxt.loc_seq_num++;
318 tcph->syn = 1;
319 } else
Faisal Latif6492cdf2008-07-24 20:50:45 -0700320 cm_node->tcp_cntxt.loc_seq_num += datasize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800321
Faisal Latif6492cdf2008-07-24 20:50:45 -0700322 if (flags & SET_FIN) {
323 cm_node->tcp_cntxt.loc_seq_num++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800324 tcph->fin = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700325 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800326
327 if (flags & SET_RST)
328 tcph->rst = 1;
329
330 tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
331 tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
332 tcph->urg_ptr = 0;
333 if (optionsize)
334 memcpy(buf, options, optionsize);
335 buf += optionsize;
336 if (datasize)
337 memcpy(buf, data, datasize);
338
339 skb_shinfo(skb)->nr_frags = 0;
340 cm_packets_created++;
341
342 return skb;
343}
344
345
346/**
347 * print_core - dump a cm core
348 */
349static void print_core(struct nes_cm_core *core)
350{
351 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
352 nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
353 if (!core)
354 return;
355 nes_debug(NES_DBG_CM, "---------------------------------------------\n");
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800356
357 nes_debug(NES_DBG_CM, "State : %u \n", core->state);
358
359 nes_debug(NES_DBG_CM, "Tx Free cnt : %u \n", skb_queue_len(&core->tx_free_list));
360 nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
361 nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
362
363 nes_debug(NES_DBG_CM, "core : %p \n", core);
364
365 nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
366}
367
368
369/**
370 * schedule_nes_timer
371 * note - cm_node needs to be protected before calling this. Encase in:
372 * rem_ref_cm_node(cm_core, cm_node);add_ref_cm_node(cm_node);
373 */
374int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
375 enum nes_timer_type type, int send_retrans,
376 int close_when_complete)
377{
378 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700379 struct nes_cm_core *cm_core = cm_node->cm_core;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800380 struct nes_timer_entry *new_send;
381 int ret = 0;
382 u32 was_timer_set;
383
Adrian Bunked0ba332008-02-21 08:12:06 -0600384 if (!cm_node)
385 return -EINVAL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800386 new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
387 if (!new_send)
388 return -1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800389
390 /* new_send->timetosend = currenttime */
391 new_send->retrycount = NES_DEFAULT_RETRYS;
392 new_send->retranscount = NES_DEFAULT_RETRANS;
393 new_send->skb = skb;
394 new_send->timetosend = jiffies;
395 new_send->type = type;
396 new_send->netdev = cm_node->netdev;
397 new_send->send_retrans = send_retrans;
398 new_send->close_when_complete = close_when_complete;
399
400 if (type == NES_TIMER_TYPE_CLOSE) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700401 new_send->timetosend += (HZ/10);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800402 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
403 list_add_tail(&new_send->list, &cm_node->recv_list);
404 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
405 }
406
407 if (type == NES_TIMER_TYPE_SEND) {
Roland Dreierb30db1c2008-04-16 21:01:07 -0700408 new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800409 atomic_inc(&new_send->skb->users);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700410 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
411 cm_node->send_entry = new_send;
412 add_ref_cm_node(cm_node);
413 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
414 new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800415
416 ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
417 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700418 nes_debug(NES_DBG_CM, "Error sending packet %p "
419 "(jiffies = %lu)\n", new_send, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800420 atomic_dec(&new_send->skb->users);
421 new_send->timetosend = jiffies;
422 } else {
423 cm_packets_sent++;
424 if (!send_retrans) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700425 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800426 if (close_when_complete)
Faisal Latif6492cdf2008-07-24 20:50:45 -0700427 rem_ref_cm_node(cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800428 return ret;
429 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800430 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800431 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800432
433 was_timer_set = timer_pending(&cm_core->tcp_timer);
434
435 if (!was_timer_set) {
436 cm_core->tcp_timer.expires = new_send->timetosend;
437 add_timer(&cm_core->tcp_timer);
438 }
439
440 return ret;
441}
442
443
444/**
445 * nes_cm_timer_tick
446 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700447static void nes_cm_timer_tick(unsigned long pass)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800448{
449 unsigned long flags, qplockflags;
450 unsigned long nexttimeout = jiffies + NES_LONG_TIME;
451 struct iw_cm_id *cm_id;
452 struct nes_cm_node *cm_node;
453 struct nes_timer_entry *send_entry, *recv_entry;
454 struct list_head *list_core, *list_core_temp;
455 struct list_head *list_node, *list_node_temp;
456 struct nes_cm_core *cm_core = g_cm_core;
457 struct nes_qp *nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800458 u32 settimer = 0;
459 int ret = NETDEV_TX_OK;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700460 enum nes_cm_node_state last_state;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800461
Faisal Latif879e5bd2008-11-21 20:50:41 -0600462 struct list_head timer_list;
463 INIT_LIST_HEAD(&timer_list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800464 spin_lock_irqsave(&cm_core->ht_lock, flags);
465
Faisal Latif6492cdf2008-07-24 20:50:45 -0700466 list_for_each_safe(list_node, list_core_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600467 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800468 cm_node = container_of(list_node, struct nes_cm_node, list);
Faisal Latif879e5bd2008-11-21 20:50:41 -0600469 if (!list_empty(&cm_node->recv_list) || (cm_node->send_entry)) {
470 add_ref_cm_node(cm_node);
471 list_add(&cm_node->timer_entry, &timer_list);
472 }
473 }
474 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
475
476 list_for_each_safe(list_node, list_core_temp, &timer_list) {
477 cm_node = container_of(list_node, struct nes_cm_node,
478 timer_entry);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800479 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700480 list_for_each_safe(list_core, list_node_temp,
481 &cm_node->recv_list) {
482 recv_entry = container_of(list_core,
483 struct nes_timer_entry, list);
484 if (!recv_entry)
485 break;
486 if (time_after(recv_entry->timetosend, jiffies)) {
487 if (nexttimeout > recv_entry->timetosend ||
488 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800489 nexttimeout = recv_entry->timetosend;
490 settimer = 1;
491 }
492 continue;
493 }
494 list_del(&recv_entry->list);
495 cm_id = cm_node->cm_id;
496 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700497 nesqp = (struct nes_qp *)recv_entry->skb;
498 spin_lock_irqsave(&nesqp->lock, qplockflags);
499 if (nesqp->cm_id) {
500 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
501 "refcount = %d: HIT A "
502 "NES_TIMER_TYPE_CLOSE with something "
503 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
504 atomic_read(&nesqp->refcount));
505 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
506 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
507 nesqp->ibqp_state = IB_QPS_ERR;
508 spin_unlock_irqrestore(&nesqp->lock,
509 qplockflags);
510 nes_cm_disconn(nesqp);
511 } else {
512 spin_unlock_irqrestore(&nesqp->lock,
513 qplockflags);
514 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
515 "refcount = %d: HIT A "
516 "NES_TIMER_TYPE_CLOSE with nothing "
517 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
518 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800519 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700520 if (cm_id)
521 cm_id->rem_ref(cm_id);
522
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800523 kfree(recv_entry);
524 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
525 }
526 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
527
528 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700529 do {
530 send_entry = cm_node->send_entry;
531 if (!send_entry)
Faisal Latifc5d321e2008-11-21 20:50:38 -0600532 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800533 if (time_after(send_entry->timetosend, jiffies)) {
534 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700535 if ((nexttimeout >
536 send_entry->timetosend) ||
537 !settimer) {
538 nexttimeout =
539 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800540 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600541 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800542 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800543 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700544 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600545 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800546 }
547 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700548
549 if ((cm_node->state == NES_CM_STATE_TSA) ||
550 (cm_node->state == NES_CM_STATE_CLOSED)) {
551 free_retrans_entry(cm_node);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600552 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800553 }
554
Faisal Latif6492cdf2008-07-24 20:50:45 -0700555 if (!send_entry->retranscount ||
556 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800557 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700558 last_state = cm_node->state;
559 cm_node->state = NES_CM_STATE_CLOSED;
560 free_retrans_entry(cm_node);
561 spin_unlock_irqrestore(
562 &cm_node->retrans_list_lock, flags);
563 if (last_state == NES_CM_STATE_SYN_RCVD)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800564 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700565 else
566 create_event(cm_node,
567 NES_CM_EVENT_ABORTED);
568 spin_lock_irqsave(&cm_node->retrans_list_lock,
569 flags);
Faisal Latifc5d321e2008-11-21 20:50:38 -0600570 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800571 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800572 atomic_inc(&send_entry->skb->users);
573 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700574 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
575 "for node %p, jiffies = %lu, time to send = "
576 "%lu, retranscount = %u, send_entry->seq_num = "
577 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
578 "0x%08X\n", send_entry, cm_node, jiffies,
579 send_entry->timetosend,
580 send_entry->retranscount,
581 send_entry->seq_num,
582 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800583
Faisal Latif6492cdf2008-07-24 20:50:45 -0700584 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
585 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800586 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700587 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800588 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700589 nes_debug(NES_DBG_CM, "rexmit failed for "
590 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800591 cm_packets_bounced++;
592 atomic_dec(&send_entry->skb->users);
593 send_entry->retrycount--;
594 nexttimeout = jiffies + NES_SHORT_TIME;
595 settimer = 1;
Faisal Latifc5d321e2008-11-21 20:50:38 -0600596 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800597 } else {
598 cm_packets_sent++;
599 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700600 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
601 "%u, retry count = %u.\n",
602 send_entry->retranscount,
603 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800604 if (send_entry->send_retrans) {
605 send_entry->retranscount--;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700606 send_entry->timetosend = jiffies +
607 NES_RETRY_TIMEOUT;
608 if (nexttimeout > send_entry->timetosend ||
609 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800610 nexttimeout = send_entry->timetosend;
611 settimer = 1;
612 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800613 } else {
614 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700615 close_when_complete =
616 send_entry->close_when_complete;
617 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
618 cm_node, cm_node->state);
619 free_retrans_entry(cm_node);
620 if (close_when_complete)
621 rem_ref_cm_node(cm_node->cm_core,
622 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800623 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700624 } while (0);
625
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800626 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700627 rem_ref_cm_node(cm_node->cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700628 if (ret != NETDEV_TX_OK) {
629 nes_debug(NES_DBG_CM, "rexmit failed for cm_node=%p\n",
630 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800631 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700632 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800633 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800634
635 if (settimer) {
636 if (!timer_pending(&cm_core->tcp_timer)) {
637 cm_core->tcp_timer.expires = nexttimeout;
638 add_timer(&cm_core->tcp_timer);
639 }
640 }
641}
642
643
644/**
645 * send_syn
646 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700647static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
648 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800649{
650 int ret;
651 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800652 char optionsbuffer[sizeof(struct option_mss) +
Faisal Latif6492cdf2008-07-24 20:50:45 -0700653 sizeof(struct option_windowscale) + sizeof(struct option_base) +
654 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800655
656 int optionssize = 0;
657 /* Sending MSS option */
658 union all_known_options *options;
659
660 if (!cm_node)
661 return -EINVAL;
662
663 options = (union all_known_options *)&optionsbuffer[optionssize];
664 options->as_mss.optionnum = OPTION_NUMBER_MSS;
665 options->as_mss.length = sizeof(struct option_mss);
666 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
667 optionssize += sizeof(struct option_mss);
668
669 options = (union all_known_options *)&optionsbuffer[optionssize];
670 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
671 options->as_windowscale.length = sizeof(struct option_windowscale);
672 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
673 optionssize += sizeof(struct option_windowscale);
674
Faisal Latif6492cdf2008-07-24 20:50:45 -0700675 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800676 options = (union all_known_options *)&optionsbuffer[optionssize];
677 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
678 options->as_base.length = sizeof(struct option_base);
679 optionssize += sizeof(struct option_base);
680 /* we need the size to be a multiple of 4 */
681 options = (union all_known_options *)&optionsbuffer[optionssize];
682 options->as_end = 1;
683 optionssize += 1;
684 options = (union all_known_options *)&optionsbuffer[optionssize];
685 options->as_end = 1;
686 optionssize += 1;
687 }
688
689 options = (union all_known_options *)&optionsbuffer[optionssize];
690 options->as_end = OPTION_NUMBER_END;
691 optionssize += 1;
692
Faisal Latif6492cdf2008-07-24 20:50:45 -0700693 if (!skb)
694 skb = get_free_pkt(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800695 if (!skb) {
696 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
697 return -1;
698 }
699
700 if (sendack)
701 flags |= SET_ACK;
702
703 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
704 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
705
706 return ret;
707}
708
709
710/**
711 * send_reset
712 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700713static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800714{
715 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800716 int flags = SET_RST | SET_ACK;
717
Faisal Latif6492cdf2008-07-24 20:50:45 -0700718 if (!skb)
719 skb = get_free_pkt(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800720 if (!skb) {
721 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
722 return -1;
723 }
724
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800725 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
726 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
727
728 return ret;
729}
730
731
732/**
733 * send_ack
734 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700735static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800736{
737 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700738
739 if (!skb)
740 skb = get_free_pkt(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800741
742 if (!skb) {
743 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
744 return -1;
745 }
746
747 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
748 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
749
750 return ret;
751}
752
753
754/**
755 * send_fin
756 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700757static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800758{
759 int ret;
760
761 /* if we didn't get a frame get one */
762 if (!skb)
763 skb = get_free_pkt(cm_node);
764
765 if (!skb) {
766 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
767 return -1;
768 }
769
770 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
771 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
772
773 return ret;
774}
775
776
777/**
778 * get_free_pkt
779 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700780static struct sk_buff *get_free_pkt(struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800781{
782 struct sk_buff *skb, *new_skb;
783
784 /* check to see if we need to repopulate the free tx pkt queue */
785 if (skb_queue_len(&cm_node->cm_core->tx_free_list) < NES_CM_FREE_PKT_LO_WATERMARK) {
786 while (skb_queue_len(&cm_node->cm_core->tx_free_list) <
787 cm_node->cm_core->free_tx_pkt_max) {
788 /* replace the frame we took, we won't get it back */
789 new_skb = dev_alloc_skb(cm_node->cm_core->mtu);
790 BUG_ON(!new_skb);
791 /* add a replacement frame to the free tx list head */
792 skb_queue_head(&cm_node->cm_core->tx_free_list, new_skb);
793 }
794 }
795
796 skb = skb_dequeue(&cm_node->cm_core->tx_free_list);
797
798 return skb;
799}
800
801
802/**
803 * make_hashkey - generate hash key from node tuple
804 */
805static inline int make_hashkey(u16 loc_port, nes_addr_t loc_addr, u16 rem_port,
806 nes_addr_t rem_addr)
807{
808 u32 hashkey = 0;
809
810 hashkey = loc_addr + rem_addr + loc_port + rem_port;
811 hashkey = (hashkey % NES_CM_HASHTABLE_SIZE);
812
813 return hashkey;
814}
815
816
817/**
818 * find_node - find a cm node that matches the reference cm node
819 */
820static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
821 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
822{
823 unsigned long flags;
824 u32 hashkey;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800825 struct list_head *hte;
826 struct nes_cm_node *cm_node;
827
828 /* make a hash index key for this packet */
829 hashkey = make_hashkey(loc_port, loc_addr, rem_port, rem_addr);
830
831 /* get a handle on the hte */
832 hte = &cm_core->connected_nodes;
833
Roland Dreiere4477032008-04-23 11:55:43 -0700834 nes_debug(NES_DBG_CM, "Searching for an owner node: " NIPQUAD_FMT ":%x from core %p->%p\n",
835 HIPQUAD(loc_addr), loc_port, cm_core, hte);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800836
837 /* walk list and find cm_node associated with this session ID */
838 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700839 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800840 /* compare quad, return node handle if a match */
841 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
842 cm_node->loc_addr, cm_node->loc_port,
843 loc_addr, loc_port,
844 cm_node->rem_addr, cm_node->rem_port,
845 rem_addr, rem_port);
846 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
847 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
848 add_ref_cm_node(cm_node);
849 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
850 return cm_node;
851 }
852 }
853 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
854
855 /* no owner node */
856 return NULL;
857}
858
859
860/**
861 * find_listener - find a cm node listening on this addr-port pair
862 */
863static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
864 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
865{
866 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800867 struct nes_cm_listener *listen_node;
868
869 /* walk list and find cm_node associated with this session ID */
870 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700871 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800872 /* compare node pair, return node handle if a match */
873 if (((listen_node->loc_addr == dst_addr) ||
874 listen_node->loc_addr == 0x00000000) &&
875 (listen_node->loc_port == dst_port) &&
876 (listener_state & listen_node->listener_state)) {
877 atomic_inc(&listen_node->ref_count);
878 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
879 return listen_node;
880 }
881 }
882 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
883
Roland Dreiere4477032008-04-23 11:55:43 -0700884 nes_debug(NES_DBG_CM, "Unable to find listener for " NIPQUAD_FMT ":%x\n",
885 HIPQUAD(dst_addr), dst_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800886
887 /* no listener */
888 return NULL;
889}
890
891
892/**
893 * add_hte_node - add a cm node to the hash table
894 */
895static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
896{
897 unsigned long flags;
898 u32 hashkey;
899 struct list_head *hte;
900
901 if (!cm_node || !cm_core)
902 return -EINVAL;
903
Faisal Latif6492cdf2008-07-24 20:50:45 -0700904 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
905 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800906
907 /* first, make an index into our hash table */
908 hashkey = make_hashkey(cm_node->loc_port, cm_node->loc_addr,
909 cm_node->rem_port, cm_node->rem_addr);
910 cm_node->hashkey = hashkey;
911
912 spin_lock_irqsave(&cm_core->ht_lock, flags);
913
914 /* get a handle on the hash table element (list head for this slot) */
915 hte = &cm_core->connected_nodes;
916 list_add_tail(&cm_node->list, hte);
917 atomic_inc(&cm_core->ht_node_cnt);
918
919 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
920
921 return 0;
922}
923
924
925/**
926 * mini_cm_dec_refcnt_listen
927 */
928static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700929 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800930{
931 int ret = 1;
932 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700933 struct list_head *list_pos = NULL;
934 struct list_head *list_temp = NULL;
935 struct nes_cm_node *cm_node = NULL;
Faisal Latif879e5bd2008-11-21 20:50:41 -0600936 struct list_head reset_list;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700937
938 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
939 "refcnt=%d\n", listener, free_hanging_nodes,
940 atomic_read(&listener->ref_count));
941 /* free non-accelerated child nodes for this listener */
Faisal Latif879e5bd2008-11-21 20:50:41 -0600942 INIT_LIST_HEAD(&reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700943 if (free_hanging_nodes) {
944 spin_lock_irqsave(&cm_core->ht_lock, flags);
945 list_for_each_safe(list_pos, list_temp,
Faisal Latif879e5bd2008-11-21 20:50:41 -0600946 &g_cm_core->connected_nodes) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700947 cm_node = container_of(list_pos, struct nes_cm_node,
948 list);
949 if ((cm_node->listener == listener) &&
Faisal Latif879e5bd2008-11-21 20:50:41 -0600950 (!cm_node->accelerated)) {
951 add_ref_cm_node(cm_node);
952 list_add(&cm_node->reset_entry, &reset_list);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700953 }
954 }
955 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
956 }
Faisal Latif879e5bd2008-11-21 20:50:41 -0600957
958 list_for_each_safe(list_pos, list_temp, &reset_list) {
959 cm_node = container_of(list_pos, struct nes_cm_node,
960 reset_entry);
961 cleanup_retrans_entry(cm_node);
962 send_reset(cm_node, NULL);
963 rem_ref_cm_node(cm_node->cm_core, cm_node);
964 }
965
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800966 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
967 if (!atomic_dec_return(&listener->ref_count)) {
968 list_del(&listener->list);
969
970 /* decrement our listen node count */
971 atomic_dec(&cm_core->listen_node_cnt);
972
973 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
974
975 if (listener->nesvnic) {
976 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
977 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
978 }
979
980 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
981
982 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -0600983 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800984 ret = 0;
985 cm_listens_destroyed++;
986 } else {
987 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
988 }
989 if (listener) {
990 if (atomic_read(&listener->pend_accepts_cnt) > 0)
991 nes_debug(NES_DBG_CM, "destroying listener (%p)"
992 " with non-zero pending accepts=%u\n",
993 listener, atomic_read(&listener->pend_accepts_cnt));
994 }
995
996 return ret;
997}
998
999
1000/**
1001 * mini_cm_del_listen
1002 */
1003static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1004 struct nes_cm_listener *listener)
1005{
1006 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
1007 listener->cm_id = NULL; /* going to be destroyed pretty soon */
1008 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1009}
1010
1011
1012/**
1013 * mini_cm_accelerated
1014 */
1015static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1016 struct nes_cm_node *cm_node)
1017{
1018 u32 was_timer_set;
1019 cm_node->accelerated = 1;
1020
1021 if (cm_node->accept_pend) {
1022 BUG_ON(!cm_node->listener);
1023 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1024 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1025 }
1026
1027 was_timer_set = timer_pending(&cm_core->tcp_timer);
1028 if (!was_timer_set) {
1029 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1030 add_timer(&cm_core->tcp_timer);
1031 }
1032
1033 return 0;
1034}
1035
1036
1037/**
Bob Sharp7191a0a2008-10-03 12:21:19 -07001038 * nes_addr_resolve_neigh
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001039 */
Bob Sharp7191a0a2008-10-03 12:21:19 -07001040static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001041{
1042 struct rtable *rt;
1043 struct flowi fl;
Bob Sharp7191a0a2008-10-03 12:21:19 -07001044 struct neighbour *neigh;
1045 int rc = -1;
1046 DECLARE_MAC_BUF(mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001047
1048 memset(&fl, 0, sizeof fl);
1049 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1050 if (ip_route_output_key(&init_net, &rt, &fl)) {
1051 printk("%s: ip_route_output_key failed for 0x%08X\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001052 __func__, dst_ip);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001053 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001054 }
1055
Bob Sharp7191a0a2008-10-03 12:21:19 -07001056 neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, nesvnic->netdev);
1057 if (neigh) {
1058 if (neigh->nud_state & NUD_VALID) {
1059 nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
1060 " is %s, Gateway is 0x%08X \n", dst_ip,
1061 print_mac(mac, neigh->ha), ntohl(rt->rt_gateway));
1062 nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1063 dst_ip, NES_ARP_ADD);
1064 rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1065 NES_ARP_RESOLVE);
1066 }
1067 neigh_release(neigh);
1068 }
1069
1070 if ((neigh == NULL) || (!(neigh->nud_state & NUD_VALID)))
1071 neigh_event_send(rt->u.dst.neighbour, NULL);
1072
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001073 ip_rt_put(rt);
Bob Sharp7191a0a2008-10-03 12:21:19 -07001074 return rc;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001075}
1076
1077
1078/**
1079 * make_cm_node - create a new instance of a cm node
1080 */
1081static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1082 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1083 struct nes_cm_listener *listener)
1084{
1085 struct nes_cm_node *cm_node;
1086 struct timespec ts;
1087 int arpindex = 0;
1088 struct nes_device *nesdev;
1089 struct nes_adapter *nesadapter;
Roland Dreier2bd01c52008-04-23 11:52:18 -07001090 DECLARE_MAC_BUF(mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001091
1092 /* create an hte and cm_node for this instance */
1093 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1094 if (!cm_node)
1095 return NULL;
1096
1097 /* set our node specific transport info */
1098 cm_node->loc_addr = cm_info->loc_addr;
1099 cm_node->rem_addr = cm_info->rem_addr;
1100 cm_node->loc_port = cm_info->loc_port;
1101 cm_node->rem_port = cm_info->rem_port;
1102 cm_node->send_write0 = send_first;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001103 nes_debug(NES_DBG_CM, "Make node addresses : loc = " NIPQUAD_FMT
1104 ":%x, rem = " NIPQUAD_FMT ":%x\n",
1105 HIPQUAD(cm_node->loc_addr), cm_node->loc_port,
1106 HIPQUAD(cm_node->rem_addr), cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001107 cm_node->listener = listener;
1108 cm_node->netdev = nesvnic->netdev;
1109 cm_node->cm_id = cm_info->cm_id;
1110 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1111
Faisal Latif6492cdf2008-07-24 20:50:45 -07001112 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1113 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001114
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001115 spin_lock_init(&cm_node->retrans_list_lock);
1116 INIT_LIST_HEAD(&cm_node->recv_list);
1117 spin_lock_init(&cm_node->recv_list_lock);
1118
1119 cm_node->loopbackpartner = NULL;
1120 atomic_set(&cm_node->ref_count, 1);
1121 /* associate our parent CM core */
1122 cm_node->cm_core = cm_core;
1123 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1124 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1125 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1126 NES_CM_DEFAULT_RCV_WND_SCALE;
1127 ts = current_kernel_time();
1128 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1129 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Chien Tungf2b2b592008-03-20 19:55:30 -05001130 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001131 cm_node->tcp_cntxt.rcv_nxt = 0;
1132 /* get a unique session ID , add thread_id to an upcounter to handle race */
1133 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001134 cm_node->conn_type = cm_info->conn_type;
1135 cm_node->apbvt_set = 0;
1136 cm_node->accept_pend = 0;
1137
1138 cm_node->nesvnic = nesvnic;
1139 /* get some device handles, for arp lookup */
1140 nesdev = nesvnic->nesdev;
1141 nesadapter = nesdev->nesadapter;
1142
1143 cm_node->loopbackpartner = NULL;
1144 /* get the mac addr for the remote node */
1145 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1146 if (arpindex < 0) {
Bob Sharp7191a0a2008-10-03 12:21:19 -07001147 arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr);
1148 if (arpindex < 0) {
1149 kfree(cm_node);
1150 return NULL;
1151 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001152 }
1153
1154 /* copy the mac addr to node context */
1155 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Roland Dreier2bd01c52008-04-23 11:52:18 -07001156 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %s\n",
1157 print_mac(mac, cm_node->rem_mac));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001158
1159 add_hte_node(cm_core, cm_node);
1160 atomic_inc(&cm_nodes_created);
1161
1162 return cm_node;
1163}
1164
1165
1166/**
1167 * add_ref_cm_node - destroy an instance of a cm node
1168 */
1169static int add_ref_cm_node(struct nes_cm_node *cm_node)
1170{
1171 atomic_inc(&cm_node->ref_count);
1172 return 0;
1173}
1174
1175
1176/**
1177 * rem_ref_cm_node - destroy an instance of a cm node
1178 */
1179static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001180 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001181{
1182 unsigned long flags, qplockflags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001183 struct nes_timer_entry *recv_entry;
1184 struct iw_cm_id *cm_id;
1185 struct list_head *list_core, *list_node_temp;
1186 struct nes_qp *nesqp;
1187
1188 if (!cm_node)
1189 return -EINVAL;
1190
1191 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1192 if (atomic_dec_return(&cm_node->ref_count)) {
1193 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1194 return 0;
1195 }
1196 list_del(&cm_node->list);
1197 atomic_dec(&cm_core->ht_node_cnt);
1198 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1199
1200 /* if the node is destroyed before connection was accelerated */
1201 if (!cm_node->accelerated && cm_node->accept_pend) {
1202 BUG_ON(!cm_node->listener);
1203 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1204 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1205 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001206 BUG_ON(cm_node->send_entry);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001207 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
1208 list_for_each_safe(list_core, list_node_temp, &cm_node->recv_list) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001209 recv_entry = container_of(list_core, struct nes_timer_entry,
1210 list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001211 list_del(&recv_entry->list);
1212 cm_id = cm_node->cm_id;
1213 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001214 nesqp = (struct nes_qp *)recv_entry->skb;
1215 spin_lock_irqsave(&nesqp->lock, qplockflags);
1216 if (nesqp->cm_id) {
1217 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p: HIT A "
1218 "NES_TIMER_TYPE_CLOSE with something to do!\n",
1219 nesqp->hwqp.qp_id, cm_id);
1220 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
1221 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
1222 nesqp->ibqp_state = IB_QPS_ERR;
1223 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
1224 nes_cm_disconn(nesqp);
1225 } else {
1226 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
1227 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p: HIT A "
1228 "NES_TIMER_TYPE_CLOSE with nothing to do!\n",
1229 nesqp->hwqp.qp_id, cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001230 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001231 cm_id->rem_ref(cm_id);
1232
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001233 kfree(recv_entry);
1234 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
1235 }
1236 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
1237
1238 if (cm_node->listener) {
1239 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1240 } else {
1241 if (cm_node->apbvt_set && cm_node->nesvnic) {
1242 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001243 PCI_FUNC(
1244 cm_node->nesvnic->nesdev->pcidev->devfn),
1245 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001246 }
1247 }
1248
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001249 atomic_dec(&cm_core->node_cnt);
1250 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001251 nesqp = cm_node->nesqp;
1252 if (nesqp) {
1253 nesqp->cm_node = NULL;
1254 nes_rem_ref(&nesqp->ibqp);
1255 cm_node->nesqp = NULL;
1256 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001257
Faisal Latif6492cdf2008-07-24 20:50:45 -07001258 cm_node->freed = 1;
1259 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001260 return 0;
1261}
1262
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001263/**
1264 * process_options
1265 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001266static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1267 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001268{
1269 u32 tmp;
1270 u32 offset = 0;
1271 union all_known_options *all_options;
1272 char got_mss_option = 0;
1273
1274 while (offset < optionsize) {
1275 all_options = (union all_known_options *)(optionsloc + offset);
1276 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001277 case OPTION_NUMBER_END:
1278 offset = optionsize;
1279 break;
1280 case OPTION_NUMBER_NONE:
1281 offset += 1;
1282 continue;
1283 case OPTION_NUMBER_MSS:
1284 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1285 "Size: %d\n", __func__,
1286 all_options->as_mss.length, offset, optionsize);
1287 got_mss_option = 1;
1288 if (all_options->as_mss.length != 4) {
1289 return 1;
1290 } else {
1291 tmp = ntohs(all_options->as_mss.mss);
1292 if (tmp > 0 && tmp <
1293 cm_node->tcp_cntxt.mss)
1294 cm_node->tcp_cntxt.mss = tmp;
1295 }
1296 break;
1297 case OPTION_NUMBER_WINDOW_SCALE:
1298 cm_node->tcp_cntxt.snd_wscale =
1299 all_options->as_windowscale.shiftcount;
1300 break;
1301 case OPTION_NUMBER_WRITE0:
1302 cm_node->send_write0 = 1;
1303 break;
1304 default:
1305 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1306 all_options->as_base.optionnum);
1307 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001308 }
1309 offset += all_options->as_base.length;
1310 }
1311 if ((!got_mss_option) && (syn_packet))
1312 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1313 return 0;
1314}
1315
Faisal Latif6492cdf2008-07-24 20:50:45 -07001316static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001317{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001318 atomic_inc(&cm_accel_dropped_pkts);
1319 dev_kfree_skb_any(skb);
1320}
1321
1322static void handle_fin_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1323 struct tcphdr *tcph)
1324{
1325 atomic_inc(&cm_resets_recvd);
1326 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:
1336 cm_node->state = NES_CM_STATE_LAST_ACK;
1337 send_fin(cm_node, skb);
1338 break;
1339 case NES_CM_STATE_FIN_WAIT1:
1340 cm_node->state = NES_CM_STATE_CLOSING;
1341 send_ack(cm_node, skb);
1342 break;
1343 case NES_CM_STATE_FIN_WAIT2:
1344 cm_node->state = NES_CM_STATE_TIME_WAIT;
1345 send_ack(cm_node, skb);
1346 cm_node->state = NES_CM_STATE_CLOSED;
1347 break;
1348 case NES_CM_STATE_TSA:
1349 default:
1350 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1351 cm_node, cm_node->state);
1352 drop_packet(skb);
1353 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001354 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001355}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001356
Faisal Latif6492cdf2008-07-24 20:50:45 -07001357
1358static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1359 struct tcphdr *tcph)
1360{
1361
1362 int reset = 0; /* whether to send reset in case of err.. */
1363 atomic_inc(&cm_resets_recvd);
1364 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1365 " refcnt=%d\n", cm_node, cm_node->state,
1366 atomic_read(&cm_node->ref_count));
1367 cleanup_retrans_entry(cm_node);
1368 switch (cm_node->state) {
1369 case NES_CM_STATE_SYN_SENT:
1370 case NES_CM_STATE_MPAREQ_SENT:
1371 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1372 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1373 cm_node->listener, cm_node->state);
1374 active_open_err(cm_node, skb, reset);
1375 break;
1376 /* For PASSIVE open states, remove the cm_node event */
1377 case NES_CM_STATE_ESTABLISHED:
1378 case NES_CM_STATE_SYN_RCVD:
1379 case NES_CM_STATE_LISTENING:
1380 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1381 passive_open_err(cm_node, skb, reset);
1382 break;
1383 case NES_CM_STATE_TSA:
1384 default:
1385 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001386 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001387}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001388
Faisal Latif6492cdf2008-07-24 20:50:45 -07001389static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb,
1390 enum nes_cm_event_type type)
1391{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001392
Faisal Latif6492cdf2008-07-24 20:50:45 -07001393 int ret;
1394 int datasize = skb->len;
1395 u8 *dataloc = skb->data;
1396 ret = parse_mpa(cm_node, dataloc, datasize);
1397 if (ret < 0) {
1398 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
1399 if (type == NES_CM_EVENT_CONNECTED) {
1400 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1401 "cm_node=%p listener=%p state=%d\n", __func__,
1402 __LINE__, cm_node, cm_node->listener,
1403 cm_node->state);
1404 active_open_err(cm_node, skb, 1);
1405 } else {
1406 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001407 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001408 } else {
1409 cleanup_retrans_entry(cm_node);
1410 dev_kfree_skb_any(skb);
1411 if (type == NES_CM_EVENT_CONNECTED)
1412 cm_node->state = NES_CM_STATE_TSA;
1413 create_event(cm_node, type);
1414
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001415 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001416 return ;
1417}
1418
1419static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1420{
1421 switch (cm_node->state) {
1422 case NES_CM_STATE_SYN_SENT:
1423 case NES_CM_STATE_MPAREQ_SENT:
1424 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1425 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1426 cm_node->listener, cm_node->state);
1427 active_open_err(cm_node, skb, 1);
1428 break;
1429 case NES_CM_STATE_ESTABLISHED:
1430 case NES_CM_STATE_SYN_RCVD:
1431 passive_open_err(cm_node, skb, 1);
1432 break;
1433 case NES_CM_STATE_TSA:
1434 default:
1435 drop_packet(skb);
1436 }
1437}
1438
1439static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1440 struct sk_buff *skb)
1441{
1442 int err;
1443
1444 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1445 if (err)
1446 active_open_err(cm_node, skb, 1);
1447
1448 return err;
1449}
1450
1451static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1452 struct sk_buff *skb)
1453{
1454 int err = 0;
1455 u32 seq;
1456 u32 ack_seq;
1457 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1458 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1459 u32 rcv_wnd;
1460 seq = ntohl(tcph->seq);
1461 ack_seq = ntohl(tcph->ack_seq);
1462 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1463 if (ack_seq != loc_seq_num)
1464 err = 1;
1465 else if ((seq + rcv_wnd) < rcv_nxt)
1466 err = 1;
1467 if (err) {
1468 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1469 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1470 cm_node->listener, cm_node->state);
1471 indicate_pkt_err(cm_node, skb);
1472 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1473 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1474 rcv_wnd);
1475 }
1476 return err;
1477}
1478
1479/*
1480 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1481 * is created with a listener or it may comein as rexmitted packet which in
1482 * that case will be just dropped.
1483 */
1484
1485static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1486 struct tcphdr *tcph)
1487{
1488 int ret;
1489 u32 inc_sequence;
1490 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001491
1492 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001493 skb_pull(skb, tcph->doff << 2);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001494 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001495
Faisal Latif6492cdf2008-07-24 20:50:45 -07001496 switch (cm_node->state) {
1497 case NES_CM_STATE_SYN_SENT:
1498 case NES_CM_STATE_MPAREQ_SENT:
1499 /* Rcvd syn on active open connection*/
1500 active_open_err(cm_node, skb, 1);
1501 break;
1502 case NES_CM_STATE_LISTENING:
1503 /* Passive OPEN */
1504 cm_node->accept_pend = 1;
1505 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1506 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1507 cm_node->listener->backlog) {
1508 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1509 "pressure \n");
1510 cm_backlog_drops++;
1511 passive_open_err(cm_node, skb, 0);
1512 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001513 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001514 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1515 1);
1516 if (ret) {
1517 passive_open_err(cm_node, skb, 0);
1518 /* drop pkt */
1519 break;
1520 }
1521 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1522 BUG_ON(cm_node->send_entry);
1523 cm_node->state = NES_CM_STATE_SYN_RCVD;
1524 send_syn(cm_node, 1, skb);
1525 break;
1526 case NES_CM_STATE_TSA:
1527 case NES_CM_STATE_ESTABLISHED:
1528 case NES_CM_STATE_FIN_WAIT1:
1529 case NES_CM_STATE_FIN_WAIT2:
1530 case NES_CM_STATE_MPAREQ_RCVD:
1531 case NES_CM_STATE_LAST_ACK:
1532 case NES_CM_STATE_CLOSING:
1533 case NES_CM_STATE_UNKNOWN:
1534 case NES_CM_STATE_CLOSED:
1535 default:
1536 drop_packet(skb);
1537 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001538 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001539}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001540
Faisal Latif6492cdf2008-07-24 20:50:45 -07001541static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1542 struct tcphdr *tcph)
1543{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001544
Faisal Latif6492cdf2008-07-24 20:50:45 -07001545 int ret;
1546 u32 inc_sequence;
1547 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001548
Faisal Latif6492cdf2008-07-24 20:50:45 -07001549 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1550 skb_pull(skb, tcph->doff << 2);
1551 inc_sequence = ntohl(tcph->seq);
1552 switch (cm_node->state) {
1553 case NES_CM_STATE_SYN_SENT:
1554 /* active open */
1555 if (check_syn(cm_node, tcph, skb))
1556 return;
1557 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1558 /* setup options */
1559 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1560 if (ret) {
1561 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1562 cm_node);
1563 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001564 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001565 cleanup_retrans_entry(cm_node);
1566 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1567 send_mpa_request(cm_node, skb);
1568 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1569 break;
1570 case NES_CM_STATE_MPAREQ_RCVD:
1571 /* passive open, so should not be here */
1572 passive_open_err(cm_node, skb, 1);
1573 break;
1574 case NES_CM_STATE_ESTABLISHED:
1575 case NES_CM_STATE_FIN_WAIT1:
1576 case NES_CM_STATE_FIN_WAIT2:
1577 case NES_CM_STATE_LAST_ACK:
1578 case NES_CM_STATE_TSA:
1579 case NES_CM_STATE_CLOSING:
1580 case NES_CM_STATE_UNKNOWN:
1581 case NES_CM_STATE_CLOSED:
1582 case NES_CM_STATE_MPAREQ_SENT:
1583 default:
1584 drop_packet(skb);
1585 break;
1586 }
1587}
1588
1589static void handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1590 struct tcphdr *tcph)
1591{
1592 int datasize = 0;
1593 u32 inc_sequence;
1594 u32 rem_seq_ack;
1595 u32 rem_seq;
1596 if (check_seq(cm_node, tcph, skb))
1597 return;
1598
1599 skb_pull(skb, tcph->doff << 2);
1600 inc_sequence = ntohl(tcph->seq);
1601 rem_seq = ntohl(tcph->seq);
1602 rem_seq_ack = ntohl(tcph->ack_seq);
1603 datasize = skb->len;
1604
1605 switch (cm_node->state) {
1606 case NES_CM_STATE_SYN_RCVD:
1607 /* Passive OPEN */
1608 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1609 cm_node->state = NES_CM_STATE_ESTABLISHED;
1610 if (datasize) {
1611 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1612 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1613 handle_rcv_mpa(cm_node, skb, NES_CM_EVENT_MPA_REQ);
1614 } else { /* rcvd ACK only */
1615 dev_kfree_skb_any(skb);
1616 cleanup_retrans_entry(cm_node);
1617 }
1618 break;
1619 case NES_CM_STATE_ESTABLISHED:
1620 /* Passive OPEN */
1621 /* We expect mpa frame to be received only */
1622 if (datasize) {
1623 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1624 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1625 handle_rcv_mpa(cm_node, skb,
1626 NES_CM_EVENT_MPA_REQ);
1627 } else
1628 drop_packet(skb);
1629 break;
1630 case NES_CM_STATE_MPAREQ_SENT:
1631 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1632 if (datasize) {
1633 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1634 handle_rcv_mpa(cm_node, skb, NES_CM_EVENT_CONNECTED);
1635 } else { /* Could be just an ack pkt.. */
1636 cleanup_retrans_entry(cm_node);
1637 dev_kfree_skb_any(skb);
1638 }
1639 break;
1640 case NES_CM_STATE_FIN_WAIT1:
1641 case NES_CM_STATE_SYN_SENT:
1642 case NES_CM_STATE_FIN_WAIT2:
1643 case NES_CM_STATE_TSA:
1644 case NES_CM_STATE_CLOSED:
1645 case NES_CM_STATE_MPAREQ_RCVD:
1646 case NES_CM_STATE_LAST_ACK:
1647 case NES_CM_STATE_CLOSING:
1648 case NES_CM_STATE_UNKNOWN:
1649 default:
1650 drop_packet(skb);
1651 break;
1652 }
1653}
1654
1655
1656
1657static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1658 struct sk_buff *skb, int optionsize, int passive)
1659{
1660 u8 *optionsloc = (u8 *)&tcph[1];
1661 if (optionsize) {
1662 if (process_options(cm_node, optionsloc, optionsize,
1663 (u32)tcph->syn)) {
1664 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1665 __func__, cm_node);
1666 if (passive)
1667 passive_open_err(cm_node, skb, 0);
1668 else
1669 active_open_err(cm_node, skb, 0);
1670 return 1;
1671 }
1672 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001673
1674 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1675 cm_node->tcp_cntxt.snd_wscale;
1676
Faisal Latif6492cdf2008-07-24 20:50:45 -07001677 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001678 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001679 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001680}
1681
Faisal Latif6492cdf2008-07-24 20:50:45 -07001682/*
1683 * active_open_err() will send reset() if flag set..
1684 * It will also send ABORT event.
1685 */
1686
1687static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1688 int reset)
1689{
1690 cleanup_retrans_entry(cm_node);
1691 if (reset) {
1692 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1693 "state=%d\n", cm_node, cm_node->state);
1694 add_ref_cm_node(cm_node);
1695 send_reset(cm_node, skb);
1696 } else
1697 dev_kfree_skb_any(skb);
1698
1699 cm_node->state = NES_CM_STATE_CLOSED;
1700 create_event(cm_node, NES_CM_EVENT_ABORTED);
1701}
1702
1703/*
1704 * passive_open_err() will either do a reset() or will free up the skb and
1705 * remove the cm_node.
1706 */
1707
1708static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1709 int reset)
1710{
1711 cleanup_retrans_entry(cm_node);
1712 cm_node->state = NES_CM_STATE_CLOSED;
1713 if (reset) {
1714 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1715 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1716 send_reset(cm_node, skb);
1717 } else {
1718 dev_kfree_skb_any(skb);
1719 rem_ref_cm_node(cm_node->cm_core, cm_node);
1720 }
1721}
1722
1723/*
1724 * free_retrans_entry() routines assumes that the retrans_list_lock has
1725 * been acquired before calling.
1726 */
1727static void free_retrans_entry(struct nes_cm_node *cm_node)
1728{
1729 struct nes_timer_entry *send_entry;
1730 send_entry = cm_node->send_entry;
1731 if (send_entry) {
1732 cm_node->send_entry = NULL;
1733 dev_kfree_skb_any(send_entry->skb);
1734 kfree(send_entry);
1735 rem_ref_cm_node(cm_node->cm_core, cm_node);
1736 }
1737}
1738
1739static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1740{
1741 unsigned long flags;
1742
1743 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1744 free_retrans_entry(cm_node);
1745 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1746}
1747
1748/**
1749 * process_packet
1750 * Returns skb if to be freed, else it will return NULL if already used..
1751 */
1752static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1753 struct nes_cm_core *cm_core)
1754{
1755 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1756 struct tcphdr *tcph = tcp_hdr(skb);
1757 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1758
1759 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1760 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1761 tcph->ack, tcph->rst, tcph->fin);
1762
1763 if (tcph->rst)
1764 pkt_type = NES_PKT_TYPE_RST;
1765 else if (tcph->syn) {
1766 pkt_type = NES_PKT_TYPE_SYN;
1767 if (tcph->ack)
1768 pkt_type = NES_PKT_TYPE_SYNACK;
1769 } else if (tcph->fin)
1770 pkt_type = NES_PKT_TYPE_FIN;
1771 else if (tcph->ack)
1772 pkt_type = NES_PKT_TYPE_ACK;
1773
1774 switch (pkt_type) {
1775 case NES_PKT_TYPE_SYN:
1776 handle_syn_pkt(cm_node, skb, tcph);
1777 break;
1778 case NES_PKT_TYPE_SYNACK:
1779 handle_synack_pkt(cm_node, skb, tcph);
1780 break;
1781 case NES_PKT_TYPE_ACK:
1782 handle_ack_pkt(cm_node, skb, tcph);
1783 break;
1784 case NES_PKT_TYPE_RST:
1785 handle_rst_pkt(cm_node, skb, tcph);
1786 break;
1787 case NES_PKT_TYPE_FIN:
1788 handle_fin_pkt(cm_node, skb, tcph);
1789 break;
1790 default:
1791 drop_packet(skb);
1792 break;
1793 }
1794}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001795
1796/**
1797 * mini_cm_listen - create a listen node with params
1798 */
1799static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001800 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001801{
1802 struct nes_cm_listener *listener;
1803 unsigned long flags;
1804
1805 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1806 cm_info->loc_addr, cm_info->loc_port);
1807
1808 /* cannot have multiple matching listeners */
1809 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1810 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1811 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1812 /* find automatically incs ref count ??? */
1813 atomic_dec(&listener->ref_count);
1814 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1815 return NULL;
1816 }
1817
1818 if (!listener) {
1819 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1820 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1821 if (!listener) {
1822 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1823 return NULL;
1824 }
1825
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001826 listener->loc_addr = htonl(cm_info->loc_addr);
1827 listener->loc_port = htons(cm_info->loc_port);
1828 listener->reused_node = 0;
1829
1830 atomic_set(&listener->ref_count, 1);
1831 }
1832 /* pasive case */
1833 /* find already inc'ed the ref count */
1834 else {
1835 listener->reused_node = 1;
1836 }
1837
1838 listener->cm_id = cm_info->cm_id;
1839 atomic_set(&listener->pend_accepts_cnt, 0);
1840 listener->cm_core = cm_core;
1841 listener->nesvnic = nesvnic;
1842 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001843
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001844 listener->conn_type = cm_info->conn_type;
1845 listener->backlog = cm_info->backlog;
1846 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1847
1848 if (!listener->reused_node) {
1849 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1850 list_add(&listener->list, &cm_core->listen_list.list);
1851 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1852 atomic_inc(&cm_core->listen_node_cnt);
1853 }
1854
1855 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1856 " listener = %p, backlog = %d, cm_id = %p.\n",
1857 cm_info->loc_addr, cm_info->loc_port,
1858 listener, listener->backlog, listener->cm_id);
1859
1860 return listener;
1861}
1862
1863
1864/**
1865 * mini_cm_connect - make a connection node with params
1866 */
Faisal Latif54c86a82008-09-30 14:47:27 -07001867static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001868 struct nes_vnic *nesvnic, u16 private_data_len,
1869 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001870{
1871 int ret = 0;
1872 struct nes_cm_node *cm_node;
1873 struct nes_cm_listener *loopbackremotelistener;
1874 struct nes_cm_node *loopbackremotenode;
1875 struct nes_cm_info loopback_cm_info;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001876 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
1877 struct ietf_mpa_frame *mpa_frame = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001878
1879 /* create a CM connection node */
1880 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
1881 if (!cm_node)
1882 return NULL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001883 mpa_frame = &cm_node->mpa_frame;
1884 strcpy(mpa_frame->key, IEFT_MPA_KEY_REQ);
1885 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
1886 mpa_frame->rev = IETF_MPA_VERSION;
1887 mpa_frame->priv_data_len = htons(private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001888
Glenn Streiff7495ab62008-04-29 13:46:54 -07001889 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001890 cm_node->tcp_cntxt.client = 1;
1891 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1892
1893 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001894 loopbackremotelistener = find_listener(cm_core,
1895 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
1896 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001897 if (loopbackremotelistener == NULL) {
1898 create_event(cm_node, NES_CM_EVENT_ABORTED);
1899 } else {
1900 atomic_inc(&cm_loopbacks);
1901 loopback_cm_info = *cm_info;
1902 loopback_cm_info.loc_port = cm_info->rem_port;
1903 loopback_cm_info.rem_port = cm_info->loc_port;
1904 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001905 loopbackremotenode = make_cm_node(cm_core, nesvnic,
1906 &loopback_cm_info, loopbackremotelistener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001907 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001908 loopbackremotenode->tcp_cntxt.rcv_wscale =
1909 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001910 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001911 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
1912 private_data_len);
1913 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001914
Faisal Latif6492cdf2008-07-24 20:50:45 -07001915 /* we are done handling this state. */
1916 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001917 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001918 cm_node->tcp_cntxt.rcv_nxt =
1919 loopbackremotenode->tcp_cntxt.loc_seq_num;
1920 loopbackremotenode->tcp_cntxt.rcv_nxt =
1921 cm_node->tcp_cntxt.loc_seq_num;
1922 cm_node->tcp_cntxt.max_snd_wnd =
1923 loopbackremotenode->tcp_cntxt.rcv_wnd;
1924 loopbackremotenode->tcp_cntxt.max_snd_wnd =
1925 cm_node->tcp_cntxt.rcv_wnd;
1926 cm_node->tcp_cntxt.snd_wnd =
1927 loopbackremotenode->tcp_cntxt.rcv_wnd;
1928 loopbackremotenode->tcp_cntxt.snd_wnd =
1929 cm_node->tcp_cntxt.rcv_wnd;
1930 cm_node->tcp_cntxt.snd_wscale =
1931 loopbackremotenode->tcp_cntxt.rcv_wscale;
1932 loopbackremotenode->tcp_cntxt.snd_wscale =
1933 cm_node->tcp_cntxt.rcv_wscale;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001934
1935 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
1936 }
1937 return cm_node;
1938 }
1939
1940 /* set our node side to client (active) side */
1941 cm_node->tcp_cntxt.client = 1;
1942 /* init our MPA frame ptr */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001943 memcpy(mpa_frame->priv_data, private_data, private_data_len);
1944
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001945 cm_node->mpa_frame_size = mpa_frame_size;
1946
1947 /* send a syn and goto syn sent state */
1948 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001949 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001950
Faisal Latif6492cdf2008-07-24 20:50:45 -07001951 if (ret) {
1952 /* error in sending the syn free up the cm_node struct */
1953 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
1954 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
1955 cm_node->rem_addr, cm_node->rem_port, cm_node,
1956 cm_node->cm_id);
1957 rem_ref_cm_node(cm_node->cm_core, cm_node);
1958 cm_node = NULL;
1959 }
1960
1961 if (cm_node)
1962 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
1963 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
1964 cm_node->rem_addr, cm_node->rem_port, cm_node,
1965 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001966
1967 return cm_node;
1968}
1969
1970
1971/**
1972 * mini_cm_accept - accept a connection
1973 * This function is never called
1974 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001975static int mini_cm_accept(struct nes_cm_core *cm_core,
1976 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001977{
1978 return 0;
1979}
1980
1981
1982/**
1983 * mini_cm_reject - reject and teardown a connection
1984 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07001985static int mini_cm_reject(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001986 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001987{
1988 int ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001989
Faisal Latif6492cdf2008-07-24 20:50:45 -07001990 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
1991 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001992
Faisal Latif6492cdf2008-07-24 20:50:45 -07001993 if (cm_node->tcp_cntxt.client)
1994 return ret;
1995 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001996 cm_node->state = NES_CM_STATE_CLOSED;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001997
Faisal Latif6492cdf2008-07-24 20:50:45 -07001998 ret = send_reset(cm_node, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001999 return ret;
2000}
2001
2002
2003/**
2004 * mini_cm_close
2005 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002006static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002007{
2008 int ret = 0;
2009
2010 if (!cm_core || !cm_node)
2011 return -EINVAL;
2012
2013 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002014 case NES_CM_STATE_SYN_RCVD:
2015 case NES_CM_STATE_SYN_SENT:
2016 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2017 case NES_CM_STATE_ESTABLISHED:
2018 case NES_CM_STATE_ACCEPTING:
2019 case NES_CM_STATE_MPAREQ_SENT:
2020 case NES_CM_STATE_MPAREQ_RCVD:
2021 cleanup_retrans_entry(cm_node);
2022 send_reset(cm_node, NULL);
2023 break;
2024 case NES_CM_STATE_CLOSE_WAIT:
2025 cm_node->state = NES_CM_STATE_LAST_ACK;
2026 send_fin(cm_node, NULL);
2027 break;
2028 case NES_CM_STATE_FIN_WAIT1:
2029 case NES_CM_STATE_FIN_WAIT2:
2030 case NES_CM_STATE_LAST_ACK:
2031 case NES_CM_STATE_TIME_WAIT:
2032 case NES_CM_STATE_CLOSING:
2033 ret = -1;
2034 break;
2035 case NES_CM_STATE_LISTENING:
2036 case NES_CM_STATE_UNKNOWN:
2037 case NES_CM_STATE_INITED:
2038 case NES_CM_STATE_CLOSED:
2039 ret = rem_ref_cm_node(cm_core, cm_node);
2040 break;
2041 case NES_CM_STATE_TSA:
2042 if (cm_node->send_entry)
2043 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2044 "send_entry=%p\n", cm_node->send_entry);
2045 ret = rem_ref_cm_node(cm_core, cm_node);
2046 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002047 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002048 return ret;
2049}
2050
2051
2052/**
2053 * recv_pkt - recv an ETHERNET packet, and process it through CM
2054 * node state machine
2055 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002056static void mini_cm_recv_pkt(struct nes_cm_core *cm_core,
2057 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002058{
2059 struct nes_cm_node *cm_node = NULL;
2060 struct nes_cm_listener *listener = NULL;
2061 struct iphdr *iph;
2062 struct tcphdr *tcph;
2063 struct nes_cm_info nfo;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002064
Faisal Latif6492cdf2008-07-24 20:50:45 -07002065 if (!skb)
2066 return;
2067 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
2068 dev_kfree_skb_any(skb);
2069 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002070 }
2071
2072 iph = (struct iphdr *)skb->data;
2073 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
2074 skb_reset_network_header(skb);
2075 skb_set_transport_header(skb, sizeof(*tcph));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002076 if (!tcph) {
2077 dev_kfree_skb_any(skb);
2078 return;
2079 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002080 skb->len = ntohs(iph->tot_len);
2081
2082 nfo.loc_addr = ntohl(iph->daddr);
2083 nfo.loc_port = ntohs(tcph->dest);
2084 nfo.rem_addr = ntohl(iph->saddr);
2085 nfo.rem_port = ntohs(tcph->source);
2086
Chien Tungbc5698f32008-04-23 11:55:45 -07002087 nes_debug(NES_DBG_CM, "Received packet: dest=" NIPQUAD_FMT
2088 ":0x%04X src=" NIPQUAD_FMT ":0x%04X\n",
2089 NIPQUAD(iph->daddr), tcph->dest,
2090 NIPQUAD(iph->saddr), tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002091
Faisal Latif6492cdf2008-07-24 20:50:45 -07002092 do {
2093 cm_node = find_node(cm_core,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002094 nfo.rem_port, nfo.rem_addr,
2095 nfo.loc_port, nfo.loc_addr);
2096
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002097 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002098 /* Only type of packet accepted are for */
2099 /* the PASSIVE open (syn only) */
2100 if ((!tcph->syn) || (tcph->ack)) {
2101 cm_packets_dropped++;
2102 break;
2103 }
2104 listener = find_listener(cm_core, nfo.loc_addr,
2105 nfo.loc_port,
2106 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002107 if (listener) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002108 nfo.cm_id = listener->cm_id;
2109 nfo.conn_type = listener->conn_type;
2110 } else {
2111 nes_debug(NES_DBG_CM, "Unable to find listener "
2112 "for the pkt\n");
2113 cm_packets_dropped++;
2114 dev_kfree_skb_any(skb);
2115 break;
2116 }
2117
2118 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2119 listener);
2120 if (!cm_node) {
2121 nes_debug(NES_DBG_CM, "Unable to allocate "
2122 "node\n");
2123 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002124 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002125 dev_kfree_skb_any(skb);
2126 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002127 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002128 if (!tcph->rst && !tcph->fin) {
2129 cm_node->state = NES_CM_STATE_LISTENING;
2130 } else {
2131 cm_packets_dropped++;
2132 rem_ref_cm_node(cm_core, cm_node);
2133 dev_kfree_skb_any(skb);
2134 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002135 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002136 add_ref_cm_node(cm_node);
2137 } else if (cm_node->state == NES_CM_STATE_TSA) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002138 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002139 atomic_inc(&cm_accel_dropped_pkts);
2140 dev_kfree_skb_any(skb);
2141 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002142 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002143 process_packet(cm_node, skb, cm_core);
2144 rem_ref_cm_node(cm_core, cm_node);
2145 } while (0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002146}
2147
2148
2149/**
2150 * nes_cm_alloc_core - allocate a top level instance of a cm core
2151 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002152static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002153{
2154 int i;
2155
2156 struct nes_cm_core *cm_core;
2157 struct sk_buff *skb = NULL;
2158
2159 /* setup the CM core */
2160 /* alloc top level core control structure */
2161 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2162 if (!cm_core)
2163 return NULL;
2164
2165 INIT_LIST_HEAD(&cm_core->connected_nodes);
2166 init_timer(&cm_core->tcp_timer);
2167 cm_core->tcp_timer.function = nes_cm_timer_tick;
2168
2169 cm_core->mtu = NES_CM_DEFAULT_MTU;
2170 cm_core->state = NES_CM_STATE_INITED;
2171 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2172
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002173 atomic_set(&cm_core->events_posted, 0);
2174
2175 /* init the packet lists */
2176 skb_queue_head_init(&cm_core->tx_free_list);
2177
2178 for (i = 0; i < NES_CM_DEFAULT_FRAME_CNT; i++) {
2179 skb = dev_alloc_skb(cm_core->mtu);
2180 if (!skb) {
2181 kfree(cm_core);
2182 return NULL;
2183 }
2184 /* add 'raw' skb to free frame list */
2185 skb_queue_head(&cm_core->tx_free_list, skb);
2186 }
2187
2188 cm_core->api = &nes_cm_api;
2189
2190 spin_lock_init(&cm_core->ht_lock);
2191 spin_lock_init(&cm_core->listen_list_lock);
2192
2193 INIT_LIST_HEAD(&cm_core->listen_list.list);
2194
2195 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2196
2197 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2198 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2199 cm_core->post_event = nes_cm_post_event;
2200 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2201 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2202
2203 print_core(cm_core);
2204 return cm_core;
2205}
2206
2207
2208/**
2209 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2210 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002211static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002212{
2213 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2214
2215 if (!cm_core)
2216 return -EINVAL;
2217
2218 barrier();
2219
2220 if (timer_pending(&cm_core->tcp_timer)) {
2221 del_timer(&cm_core->tcp_timer);
2222 }
2223
2224 destroy_workqueue(cm_core->event_wq);
2225 destroy_workqueue(cm_core->disconn_wq);
2226 nes_debug(NES_DBG_CM, "\n");
2227 kfree(cm_core);
2228
2229 return 0;
2230}
2231
2232
2233/**
2234 * mini_cm_get
2235 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002236static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002237{
2238 return cm_core->state;
2239}
2240
2241
2242/**
2243 * mini_cm_set
2244 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002245static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002246{
2247 int ret = 0;
2248
2249 switch (type) {
2250 case NES_CM_SET_PKT_SIZE:
2251 cm_core->mtu = value;
2252 break;
2253 case NES_CM_SET_FREE_PKT_Q_SIZE:
2254 cm_core->free_tx_pkt_max = value;
2255 break;
2256 default:
2257 /* unknown set option */
2258 ret = -EINVAL;
2259 }
2260
2261 return ret;
2262}
2263
2264
2265/**
2266 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2267 * successfully exchanged when this is called
2268 */
2269static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2270{
2271 int ret = 0;
2272
2273 if (!nesqp)
2274 return -EINVAL;
2275
2276 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2277 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2278 NES_QPCONTEXT_MISC_DROS);
2279
2280 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2281 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2282
2283 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2284
2285 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2286
2287 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2288 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2289
2290 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2291 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2292 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2293
2294 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2295 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2296 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2297
2298 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2299 nesqp->nesqp_context->ts_recent = 0;
2300 nesqp->nesqp_context->ts_age = 0;
2301 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2302 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2303 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2304 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2305 cm_node->tcp_cntxt.rcv_wscale);
2306 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2307 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2308 nesqp->nesqp_context->srtt = 0;
2309 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2310 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2311 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2312 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2313 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2314 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2315
2316 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2317 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2318 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2319 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2320 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2321 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2322 le32_to_cpu(nesqp->nesqp_context->misc));
2323 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2324 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2325 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2326
2327 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2328 cm_node->state = NES_CM_STATE_TSA;
2329
2330 return ret;
2331}
2332
2333
2334/**
2335 * nes_cm_disconn
2336 */
2337int nes_cm_disconn(struct nes_qp *nesqp)
2338{
2339 unsigned long flags;
2340
2341 spin_lock_irqsave(&nesqp->lock, flags);
2342 if (nesqp->disconn_pending == 0) {
2343 nesqp->disconn_pending++;
2344 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002345 /* init our disconnect work element, to */
2346 INIT_WORK(&nesqp->disconn_work, nes_disconnect_worker);
2347
2348 queue_work(g_cm_core->disconn_wq, &nesqp->disconn_work);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002349 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002350 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002351
2352 return 0;
2353}
2354
2355
2356/**
2357 * nes_disconnect_worker
2358 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002359static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002360{
2361 struct nes_qp *nesqp = container_of(work, struct nes_qp, disconn_work);
2362
2363 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2364 nesqp->last_aeq, nesqp->hwqp.qp_id);
2365 nes_cm_disconn_true(nesqp);
2366}
2367
2368
2369/**
2370 * nes_cm_disconn_true
2371 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002372static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002373{
2374 unsigned long flags;
2375 int ret = 0;
2376 struct iw_cm_id *cm_id;
2377 struct iw_cm_event cm_event;
2378 struct nes_vnic *nesvnic;
2379 u16 last_ae;
2380 u8 original_hw_tcp_state;
2381 u8 original_ibqp_state;
2382 u8 issued_disconnect_reset = 0;
2383
2384 if (!nesqp) {
2385 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2386 return -1;
2387 }
2388
2389 spin_lock_irqsave(&nesqp->lock, flags);
2390 cm_id = nesqp->cm_id;
2391 /* make sure we havent already closed this connection */
2392 if (!cm_id) {
2393 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2394 nesqp->hwqp.qp_id);
2395 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002396 return -1;
2397 }
2398
2399 nesvnic = to_nesvnic(nesqp->ibqp.device);
2400 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2401
2402 original_hw_tcp_state = nesqp->hw_tcp_state;
2403 original_ibqp_state = nesqp->ibqp_state;
2404 last_ae = nesqp->last_aeq;
2405
2406
2407 nes_debug(NES_DBG_CM, "set ibqp_state=%u\n", nesqp->ibqp_state);
2408
2409 if ((nesqp->cm_id) && (cm_id->event_handler)) {
2410 if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2411 ((original_ibqp_state == IB_QPS_RTS) &&
2412 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2413 atomic_inc(&cm_disconnects);
2414 cm_event.event = IW_CM_EVENT_DISCONNECT;
2415 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET) {
Faisal Latifd7ffd502008-09-16 11:56:26 -07002416 issued_disconnect_reset = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002417 cm_event.status = IW_CM_EVENT_STATUS_RESET;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002418 nes_debug(NES_DBG_CM, "Generating a CM "
2419 "Disconnect Event (status reset) for "
2420 "QP%u, cm_id = %p. \n",
2421 nesqp->hwqp.qp_id, cm_id);
2422 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002423 cm_event.status = IW_CM_EVENT_STATUS_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002424
2425 cm_event.local_addr = cm_id->local_addr;
2426 cm_event.remote_addr = cm_id->remote_addr;
2427 cm_event.private_data = NULL;
2428 cm_event.private_data_len = 0;
2429
Faisal Latif6492cdf2008-07-24 20:50:45 -07002430 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2431 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2432 "cm_id = %p, refcount = %u.\n",
2433 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2434 nesqp->hwqp.sq_tail, cm_id,
2435 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002436
2437 spin_unlock_irqrestore(&nesqp->lock, flags);
2438 ret = cm_id->event_handler(cm_id, &cm_event);
2439 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002440 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2441 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002442 spin_lock_irqsave(&nesqp->lock, flags);
2443 }
2444
2445 nesqp->disconn_pending = 0;
2446 /* There might have been another AE while the lock was released */
2447 original_hw_tcp_state = nesqp->hw_tcp_state;
2448 original_ibqp_state = nesqp->ibqp_state;
2449 last_ae = nesqp->last_aeq;
2450
2451 if ((issued_disconnect_reset == 0) && (nesqp->cm_id) &&
2452 ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2453 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2454 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2455 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2456 atomic_inc(&cm_closes);
2457 nesqp->cm_id = NULL;
2458 nesqp->in_disconnect = 0;
2459 spin_unlock_irqrestore(&nesqp->lock, flags);
2460 nes_disconnect(nesqp, 1);
2461
2462 cm_id->provider_data = nesqp;
2463 /* Send up the close complete event */
2464 cm_event.event = IW_CM_EVENT_CLOSE;
2465 cm_event.status = IW_CM_EVENT_STATUS_OK;
2466 cm_event.provider_data = cm_id->provider_data;
2467 cm_event.local_addr = cm_id->local_addr;
2468 cm_event.remote_addr = cm_id->remote_addr;
2469 cm_event.private_data = NULL;
2470 cm_event.private_data_len = 0;
2471
2472 ret = cm_id->event_handler(cm_id, &cm_event);
2473 if (ret) {
2474 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2475 }
2476
2477 cm_id->rem_ref(cm_id);
2478
2479 spin_lock_irqsave(&nesqp->lock, flags);
2480 if (nesqp->flush_issued == 0) {
2481 nesqp->flush_issued = 1;
2482 spin_unlock_irqrestore(&nesqp->lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002483 flush_wqes(nesvnic->nesdev, nesqp,
2484 NES_CQP_FLUSH_RQ, 1);
2485 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002486 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002487 } else {
2488 cm_id = nesqp->cm_id;
2489 spin_unlock_irqrestore(&nesqp->lock, flags);
2490 /* check to see if the inbound reset beat the outbound reset */
2491 if ((!cm_id) && (last_ae==NES_AEQE_AEID_RESET_SENT)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002492 nes_debug(NES_DBG_CM, "QP%u: Decing refcount "
2493 "due to inbound reset beating the "
2494 "outbound reset.\n", nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002495 }
2496 }
2497 } else {
2498 nesqp->disconn_pending = 0;
2499 spin_unlock_irqrestore(&nesqp->lock, flags);
2500 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002501
2502 return 0;
2503}
2504
2505
2506/**
2507 * nes_disconnect
2508 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002509static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002510{
2511 int ret = 0;
2512 struct nes_vnic *nesvnic;
2513 struct nes_device *nesdev;
2514
2515 nesvnic = to_nesvnic(nesqp->ibqp.device);
2516 if (!nesvnic)
2517 return -EINVAL;
2518
2519 nesdev = nesvnic->nesdev;
2520
2521 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2522 atomic_read(&nesvnic->netdev->refcnt));
2523
2524 if (nesqp->active_conn) {
2525
2526 /* indicate this connection is NOT active */
2527 nesqp->active_conn = 0;
2528 } else {
2529 /* Need to free the Last Streaming Mode Message */
2530 if (nesqp->ietf_frame) {
2531 pci_free_consistent(nesdev->pcidev,
2532 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2533 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2534 }
2535 }
2536
2537 /* close the CM node down if it is still active */
2538 if (nesqp->cm_node) {
2539 nes_debug(NES_DBG_CM, "Call close API\n");
2540
2541 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002542 }
2543
2544 return ret;
2545}
2546
2547
2548/**
2549 * nes_accept
2550 */
2551int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2552{
2553 u64 u64temp;
2554 struct ib_qp *ibqp;
2555 struct nes_qp *nesqp;
2556 struct nes_vnic *nesvnic;
2557 struct nes_device *nesdev;
2558 struct nes_cm_node *cm_node;
2559 struct nes_adapter *adapter;
2560 struct ib_qp_attr attr;
2561 struct iw_cm_event cm_event;
2562 struct nes_hw_qp_wqe *wqe;
2563 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002564 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002565 int ret;
2566
2567 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2568 if (!ibqp)
2569 return -EINVAL;
2570
2571 /* get all our handles */
2572 nesqp = to_nesqp(ibqp);
2573 nesvnic = to_nesvnic(nesqp->ibqp.device);
2574 nesdev = nesvnic->nesdev;
2575 adapter = nesdev->nesadapter;
2576
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002577 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002578 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2579 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2580 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002581
2582 /* associate the node with the QP */
2583 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002584 cm_node->nesqp = nesqp;
2585 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002586
Faisal Latif6492cdf2008-07-24 20:50:45 -07002587 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2588 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002589 atomic_inc(&cm_accepts);
2590
2591 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2592 atomic_read(&nesvnic->netdev->refcnt));
2593
Faisal Latif6492cdf2008-07-24 20:50:45 -07002594 /* allocate the ietf frame and space for private data */
2595 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2596 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2597 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002598
Faisal Latif6492cdf2008-07-24 20:50:45 -07002599 if (!nesqp->ietf_frame) {
2600 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2601 "data\n");
2602 return -ENOMEM;
2603 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002604
2605
Faisal Latif6492cdf2008-07-24 20:50:45 -07002606 /* setup the MPA frame */
2607 nesqp->private_data_len = conn_param->private_data_len;
2608 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002609
Faisal Latif6492cdf2008-07-24 20:50:45 -07002610 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2611 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002612
Faisal Latif6492cdf2008-07-24 20:50:45 -07002613 nesqp->ietf_frame->priv_data_len =
2614 cpu_to_be16(conn_param->private_data_len);
2615 nesqp->ietf_frame->rev = mpa_version;
2616 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002617
Faisal Latif6492cdf2008-07-24 20:50:45 -07002618 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2619 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002620
Faisal Latif6492cdf2008-07-24 20:50:45 -07002621 if (cm_id->remote_addr.sin_addr.s_addr !=
2622 cm_id->local_addr.sin_addr.s_addr) {
2623 u64temp = (unsigned long)nesqp;
2624 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2625 set_wqe_64bit_value(wqe->wqe_words,
2626 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2627 u64temp);
2628 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2629 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2630 NES_IWARP_SQ_WQE_WRPDU);
2631 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2632 cpu_to_le32(conn_param->private_data_len +
2633 sizeof(struct ietf_mpa_frame));
2634 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] =
2635 cpu_to_le32((u32)nesqp->ietf_frame_pbase);
2636 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] =
2637 cpu_to_le32((u32)((u64)nesqp->ietf_frame_pbase >> 32));
2638 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2639 cpu_to_le32(conn_param->private_data_len +
2640 sizeof(struct ietf_mpa_frame));
2641 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002642
Faisal Latif6492cdf2008-07-24 20:50:45 -07002643 nesqp->nesqp_context->ird_ord_sizes |=
2644 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2645 NES_QPCONTEXT_ORDIRD_WRPDU);
2646 } else {
2647 nesqp->nesqp_context->ird_ord_sizes |=
2648 cpu_to_le32((NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2649 NES_QPCONTEXT_ORDIRD_WRPDU |
2650 NES_QPCONTEXT_ORDIRD_ALSMM));
2651 }
2652 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002653
2654
2655 /* Cache the cm_id in the qp */
2656 nesqp->cm_id = cm_id;
2657 cm_node->cm_id = cm_id;
2658
2659 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2660 cm_id->provider_data = nesqp;
2661 nesqp->active_conn = 0;
2662
Faisal Latif6492cdf2008-07-24 20:50:45 -07002663 if (cm_node->state == NES_CM_STATE_TSA)
2664 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2665 cm_node);
2666
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002667 nes_cm_init_tsa_conn(nesqp, cm_node);
2668
Faisal Latif6492cdf2008-07-24 20:50:45 -07002669 nesqp->nesqp_context->tcpPorts[0] =
2670 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2671 nesqp->nesqp_context->tcpPorts[1] =
2672 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2673
2674 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2675 nesqp->nesqp_context->ip0 =
2676 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2677 else
2678 nesqp->nesqp_context->ip0 =
2679 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002680
2681 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002682 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2683 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002684
Faisal Latif6492cdf2008-07-24 20:50:45 -07002685 nesqp->nesqp_context->arp_index_vlan |=
2686 cpu_to_le32(nes_arp_table(nesdev,
2687 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002688 NES_ARP_RESOLVE) << 16);
2689
2690 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002691 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002692
2693 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2694
2695 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002696 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2697 nesqp->nesqp_context->ird_ord_sizes |=
2698 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002699
2700 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002701 nes_quad.DstIpAdrIndex =
2702 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2703 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2704 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2705 else
2706 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2707 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2708 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002709
2710 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06002711 crc_value = get_crc_value(&nes_quad);
2712 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002713 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07002714 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002715
2716 nesqp->hte_index &= adapter->hte_index_mask;
2717 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2718
2719 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2720
Faisal Latif6492cdf2008-07-24 20:50:45 -07002721 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2722 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2723 "private data length=%zu.\n", nesqp->hwqp.qp_id,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002724 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2725 ntohs(cm_id->remote_addr.sin_port),
2726 ntohl(cm_id->local_addr.sin_addr.s_addr),
2727 ntohs(cm_id->local_addr.sin_port),
2728 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2729 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002730 conn_param->private_data_len +
2731 sizeof(struct ietf_mpa_frame));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002732
2733 attr.qp_state = IB_QPS_RTS;
2734 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
2735
2736 /* notify OF layer that accept event was successfull */
2737 cm_id->add_ref(cm_id);
2738
2739 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2740 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2741 cm_event.provider_data = (void *)nesqp;
2742 cm_event.local_addr = cm_id->local_addr;
2743 cm_event.remote_addr = cm_id->remote_addr;
2744 cm_event.private_data = NULL;
2745 cm_event.private_data_len = 0;
2746 ret = cm_id->event_handler(cm_id, &cm_event);
2747 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002748 cm_node->loopbackpartner->mpa_frame_size =
2749 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002750 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002751 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2752 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002753 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2754 }
2755 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002756 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2757 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002758
2759 return 0;
2760}
2761
2762
2763/**
2764 * nes_reject
2765 */
2766int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2767{
2768 struct nes_cm_node *cm_node;
2769 struct nes_cm_core *cm_core;
2770
2771 atomic_inc(&cm_rejects);
2772 cm_node = (struct nes_cm_node *) cm_id->provider_data;
2773 cm_core = cm_node->cm_core;
2774 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2775
2776 strcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP);
2777 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2778
2779 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2780 cm_node->mpa_frame.rev = mpa_version;
2781 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2782
2783 cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
2784
2785 return 0;
2786}
2787
2788
2789/**
2790 * nes_connect
2791 * setup and launch cm connect node
2792 */
2793int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2794{
2795 struct ib_qp *ibqp;
2796 struct nes_qp *nesqp;
2797 struct nes_vnic *nesvnic;
2798 struct nes_device *nesdev;
2799 struct nes_cm_node *cm_node;
2800 struct nes_cm_info cm_info;
2801
2802 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2803 if (!ibqp)
2804 return -EINVAL;
2805 nesqp = to_nesqp(ibqp);
2806 if (!nesqp)
2807 return -EINVAL;
2808 nesvnic = to_nesvnic(nesqp->ibqp.device);
2809 if (!nesvnic)
2810 return -EINVAL;
2811 nesdev = nesvnic->nesdev;
2812 if (!nesdev)
2813 return -EINVAL;
2814
Faisal Latif6492cdf2008-07-24 20:50:45 -07002815 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
2816 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
2817 ntohl(nesvnic->local_ipaddr),
2818 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2819 ntohs(cm_id->remote_addr.sin_port),
2820 ntohl(cm_id->local_addr.sin_addr.s_addr),
2821 ntohs(cm_id->local_addr.sin_port));
2822
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002823 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002824 nesqp->active_conn = 1;
2825
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002826 /* cache the cm_id in the qp */
2827 nesqp->cm_id = cm_id;
2828
2829 cm_id->provider_data = nesqp;
2830
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002831 nesqp->private_data_len = conn_param->private_data_len;
2832 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
2833 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002834 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
2835 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002836
Faisal Latif6492cdf2008-07-24 20:50:45 -07002837 if (cm_id->local_addr.sin_addr.s_addr !=
2838 cm_id->remote_addr.sin_addr.s_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002839 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002840 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002841
2842 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002843 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
2844 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
2845 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
2846 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002847 cm_info.cm_id = cm_id;
2848 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
2849
2850 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002851
2852 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002853 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
2854 conn_param->private_data_len, (void *)conn_param->private_data,
2855 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002856 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002857 if (cm_id->local_addr.sin_addr.s_addr !=
2858 cm_id->remote_addr.sin_addr.s_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002859 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002860 PCI_FUNC(nesdev->pcidev->devfn),
2861 NES_MANAGE_APBVT_DEL);
2862
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002863 cm_id->rem_ref(cm_id);
2864 return -ENOMEM;
2865 }
2866
2867 cm_node->apbvt_set = 1;
2868 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002869 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07002870 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002871
2872 return 0;
2873}
2874
2875
2876/**
2877 * nes_create_listen
2878 */
2879int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
2880{
2881 struct nes_vnic *nesvnic;
2882 struct nes_cm_listener *cm_node;
2883 struct nes_cm_info cm_info;
2884 struct nes_adapter *adapter;
2885 int err;
2886
2887
2888 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
2889 cm_id, ntohs(cm_id->local_addr.sin_port));
2890
2891 nesvnic = to_nesvnic(cm_id->device);
2892 if (!nesvnic)
2893 return -EINVAL;
2894 adapter = nesvnic->nesdev->nesadapter;
2895 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
2896 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
2897
2898 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
2899 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
2900
2901 /* setup listen params in our api call struct */
2902 cm_info.loc_addr = nesvnic->local_ipaddr;
2903 cm_info.loc_port = cm_id->local_addr.sin_port;
2904 cm_info.backlog = backlog;
2905 cm_info.cm_id = cm_id;
2906
2907 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
2908
2909
2910 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
2911 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002912 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07002913 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002914 return -ENOMEM;
2915 }
2916
2917 cm_id->provider_data = cm_node;
2918
2919 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002920 err = nes_manage_apbvt(nesvnic,
2921 ntohs(cm_id->local_addr.sin_port),
2922 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
2923 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002924 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002925 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
2926 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002927 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
2928 return err;
2929 }
2930 cm_listens_created++;
2931 }
2932
2933 cm_id->add_ref(cm_id);
2934 cm_id->provider_data = (void *)cm_node;
2935
2936
2937 return 0;
2938}
2939
2940
2941/**
2942 * nes_destroy_listen
2943 */
2944int nes_destroy_listen(struct iw_cm_id *cm_id)
2945{
2946 if (cm_id->provider_data)
2947 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
2948 else
2949 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
2950
2951 cm_id->rem_ref(cm_id);
2952
2953 return 0;
2954}
2955
2956
2957/**
2958 * nes_cm_recv
2959 */
2960int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
2961{
2962 cm_packets_received++;
2963 if ((g_cm_core) && (g_cm_core->api)) {
2964 g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
2965 } else {
2966 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
2967 " cm is not setup properly.\n");
2968 }
2969
2970 return 0;
2971}
2972
2973
2974/**
2975 * nes_cm_start
2976 * Start and init a cm core module
2977 */
2978int nes_cm_start(void)
2979{
2980 nes_debug(NES_DBG_CM, "\n");
2981 /* create the primary CM core, pass this handle to subsequent core inits */
2982 g_cm_core = nes_cm_alloc_core();
2983 if (g_cm_core) {
2984 return 0;
2985 } else {
2986 return -ENOMEM;
2987 }
2988}
2989
2990
2991/**
2992 * nes_cm_stop
2993 * stop and dealloc all cm core instances
2994 */
2995int nes_cm_stop(void)
2996{
2997 g_cm_core->api->destroy_cm_core(g_cm_core);
2998 return 0;
2999}
3000
3001
3002/**
3003 * cm_event_connected
3004 * handle a connected event, setup QPs and HW
3005 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003006static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003007{
3008 u64 u64temp;
3009 struct nes_qp *nesqp;
3010 struct nes_vnic *nesvnic;
3011 struct nes_device *nesdev;
3012 struct nes_cm_node *cm_node;
3013 struct nes_adapter *nesadapter;
3014 struct ib_qp_attr attr;
3015 struct iw_cm_id *cm_id;
3016 struct iw_cm_event cm_event;
3017 struct nes_hw_qp_wqe *wqe;
3018 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003019 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003020 int ret;
3021
3022 /* get all our handles */
3023 cm_node = event->cm_node;
3024 cm_id = cm_node->cm_id;
3025 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3026 nesqp = (struct nes_qp *)cm_id->provider_data;
3027 nesvnic = to_nesvnic(nesqp->ibqp.device);
3028 nesdev = nesvnic->nesdev;
3029 nesadapter = nesdev->nesadapter;
3030
3031 if (nesqp->destroyed) {
3032 return;
3033 }
3034 atomic_inc(&cm_connecteds);
3035 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3036 " local port 0x%04X. jiffies = %lu.\n",
3037 nesqp->hwqp.qp_id,
3038 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3039 ntohs(cm_id->remote_addr.sin_port),
3040 ntohs(cm_id->local_addr.sin_port),
3041 jiffies);
3042
3043 nes_cm_init_tsa_conn(nesqp, cm_node);
3044
3045 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003046 nesqp->nesqp_context->tcpPorts[0] =
3047 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3048 nesqp->nesqp_context->tcpPorts[1] =
3049 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3050 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3051 nesqp->nesqp_context->ip0 =
3052 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3053 else
3054 nesqp->nesqp_context->ip0 =
3055 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003056
3057 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003058 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3059 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003060 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003061 nes_arp_table(nesdev,
3062 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003063 NULL, NES_ARP_RESOLVE) << 16);
3064 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3065 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3066 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3067 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003068 cpu_to_le32((u32)1 <<
3069 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003070
3071 /* Adjust tail for not having a LSMM */
3072 nesqp->hwqp.sq_tail = 1;
3073
3074#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003075 if (cm_node->send_write0) {
3076 nes_debug(NES_DBG_CM, "Sending first write.\n");
3077 wqe = &nesqp->hwqp.sq_vbase[0];
3078 u64temp = (unsigned long)nesqp;
3079 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3080 set_wqe_64bit_value(wqe->wqe_words,
3081 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3082 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3083 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3084 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3085 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3086 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3087 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3088 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003089
Faisal Latif6492cdf2008-07-24 20:50:45 -07003090 /* use the reserved spot on the WQ for the extra first WQE */
3091 nesqp->nesqp_context->ird_ord_sizes &=
3092 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3093 NES_QPCONTEXT_ORDIRD_WRPDU |
3094 NES_QPCONTEXT_ORDIRD_ALSMM));
3095 nesqp->skip_lsmm = 1;
3096 nesqp->hwqp.sq_tail = 0;
3097 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3098 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3099 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003100#endif
3101
3102 memset(&nes_quad, 0, sizeof(nes_quad));
3103
Faisal Latif6492cdf2008-07-24 20:50:45 -07003104 nes_quad.DstIpAdrIndex =
3105 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3106 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3107 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3108 else
3109 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003110 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3111 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3112
3113 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003114 crc_value = get_crc_value(&nes_quad);
3115 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003116 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3117 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3118
3119 nesqp->hte_index &= nesadapter->hte_index_mask;
3120 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3121
3122 nesqp->ietf_frame = &cm_node->mpa_frame;
3123 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3124 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3125
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003126 /* notify OF layer we successfully created the requested connection */
3127 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3128 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3129 cm_event.provider_data = cm_id->provider_data;
3130 cm_event.local_addr.sin_family = AF_INET;
3131 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3132 cm_event.remote_addr = cm_id->remote_addr;
3133
Faisal Latif6492cdf2008-07-24 20:50:45 -07003134 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3135 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003136
3137 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3138 ret = cm_id->event_handler(cm_id, &cm_event);
3139 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3140
3141 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003142 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3143 "ret=%d\n", __func__, __LINE__, ret);
3144 attr.qp_state = IB_QPS_RTS;
3145 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003146
Faisal Latif6492cdf2008-07-24 20:50:45 -07003147 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3148 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003149
3150 return;
3151}
3152
3153
3154/**
3155 * cm_event_connect_error
3156 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003157static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003158{
3159 struct nes_qp *nesqp;
3160 struct iw_cm_id *cm_id;
3161 struct iw_cm_event cm_event;
3162 /* struct nes_cm_info cm_info; */
3163 int ret;
3164
3165 if (!event->cm_node)
3166 return;
3167
3168 cm_id = event->cm_node->cm_id;
3169 if (!cm_id) {
3170 return;
3171 }
3172
3173 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3174 nesqp = cm_id->provider_data;
3175
3176 if (!nesqp) {
3177 return;
3178 }
3179
3180 /* notify OF layer about this connection error event */
3181 /* cm_id->rem_ref(cm_id); */
3182 nesqp->cm_id = NULL;
3183 cm_id->provider_data = NULL;
3184 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3185 cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
3186 cm_event.provider_data = cm_id->provider_data;
3187 cm_event.local_addr = cm_id->local_addr;
3188 cm_event.remote_addr = cm_id->remote_addr;
3189 cm_event.private_data = NULL;
3190 cm_event.private_data_len = 0;
3191
Faisal Latif6492cdf2008-07-24 20:50:45 -07003192 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3193 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3194 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003195
3196 ret = cm_id->event_handler(cm_id, &cm_event);
3197 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3198 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003199 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3200 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003201 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003202
Faisal Latif6492cdf2008-07-24 20:50:45 -07003203 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003204 return;
3205}
3206
3207
3208/**
3209 * cm_event_reset
3210 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003211static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003212{
3213 struct nes_qp *nesqp;
3214 struct iw_cm_id *cm_id;
3215 struct iw_cm_event cm_event;
3216 /* struct nes_cm_info cm_info; */
3217 int ret;
3218
3219 if (!event->cm_node)
3220 return;
3221
3222 if (!event->cm_node->cm_id)
3223 return;
3224
3225 cm_id = event->cm_node->cm_id;
3226
3227 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3228 nesqp = cm_id->provider_data;
3229
3230 nesqp->cm_id = NULL;
3231 /* cm_id->provider_data = NULL; */
3232 cm_event.event = IW_CM_EVENT_DISCONNECT;
3233 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3234 cm_event.provider_data = cm_id->provider_data;
3235 cm_event.local_addr = cm_id->local_addr;
3236 cm_event.remote_addr = cm_id->remote_addr;
3237 cm_event.private_data = NULL;
3238 cm_event.private_data_len = 0;
3239
3240 ret = cm_id->event_handler(cm_id, &cm_event);
3241 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3242
3243
3244 /* notify OF layer about this connection error event */
3245 cm_id->rem_ref(cm_id);
3246
3247 return;
3248}
3249
3250
3251/**
3252 * cm_event_mpa_req
3253 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003254static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003255{
3256 struct iw_cm_id *cm_id;
3257 struct iw_cm_event cm_event;
3258 int ret;
3259 struct nes_cm_node *cm_node;
3260
3261 cm_node = event->cm_node;
3262 if (!cm_node)
3263 return;
3264 cm_id = cm_node->cm_id;
3265
3266 atomic_inc(&cm_connect_reqs);
3267 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3268 cm_node, cm_id, jiffies);
3269
3270 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3271 cm_event.status = IW_CM_EVENT_STATUS_OK;
3272 cm_event.provider_data = (void *)cm_node;
3273
3274 cm_event.local_addr.sin_family = AF_INET;
3275 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3276 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3277
3278 cm_event.remote_addr.sin_family = AF_INET;
3279 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3280 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3281
3282 cm_event.private_data = cm_node->mpa_frame_buf;
3283 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3284
3285 ret = cm_id->event_handler(cm_id, &cm_event);
3286 if (ret)
3287 printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003288 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003289
3290 return;
3291}
3292
3293
3294static void nes_cm_event_handler(struct work_struct *);
3295
3296/**
3297 * nes_cm_post_event
3298 * post an event to the cm event handler
3299 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003300static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003301{
3302 atomic_inc(&event->cm_node->cm_core->events_posted);
3303 add_ref_cm_node(event->cm_node);
3304 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3305 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003306 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3307 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003308
3309 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3310
3311 nes_debug(NES_DBG_CM, "Exit\n");
3312 return 0;
3313}
3314
3315
3316/**
3317 * nes_cm_event_handler
3318 * worker function to handle cm events
3319 * will free instance of nes_cm_event
3320 */
3321static void nes_cm_event_handler(struct work_struct *work)
3322{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003323 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3324 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003325 struct nes_cm_core *cm_core;
3326
Faisal Latif6492cdf2008-07-24 20:50:45 -07003327 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003328 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003329
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003330 cm_core = event->cm_node->cm_core;
3331 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003332 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003333
3334 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003335 case NES_CM_EVENT_MPA_REQ:
3336 cm_event_mpa_req(event);
3337 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3338 event->cm_node);
3339 break;
3340 case NES_CM_EVENT_RESET:
3341 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3342 event->cm_node);
3343 cm_event_reset(event);
3344 break;
3345 case NES_CM_EVENT_CONNECTED:
3346 if ((!event->cm_node->cm_id) ||
3347 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003348 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003349 cm_event_connected(event);
3350 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3351 break;
3352 case NES_CM_EVENT_ABORTED:
3353 if ((!event->cm_node->cm_id) ||
3354 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003355 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003356 cm_event_connect_error(event);
3357 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3358 break;
3359 case NES_CM_EVENT_DROPPED_PKT:
3360 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3361 break;
3362 default:
3363 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3364 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003365 }
3366
3367 atomic_dec(&cm_core->events_posted);
3368 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3369 rem_ref_cm_node(cm_core, event->cm_node);
3370 kfree(event);
3371
3372 return;
3373}