blob: c259ddc8dd88480cdbbf7c3dc614b089fba14b84 [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.. */
Faisal Latif183ecfa2008-11-21 20:50:46 -06001363 int passive_state;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001364 atomic_inc(&cm_resets_recvd);
1365 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1366 " refcnt=%d\n", cm_node, cm_node->state,
1367 atomic_read(&cm_node->ref_count));
1368 cleanup_retrans_entry(cm_node);
1369 switch (cm_node->state) {
1370 case NES_CM_STATE_SYN_SENT:
1371 case NES_CM_STATE_MPAREQ_SENT:
1372 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1373 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1374 cm_node->listener, cm_node->state);
1375 active_open_err(cm_node, skb, reset);
1376 break;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001377 case NES_CM_STATE_MPAREQ_RCVD:
1378 passive_state = atomic_add_return(1, &cm_node->passive_state);
1379 if (passive_state == NES_SEND_RESET_EVENT)
1380 create_event(cm_node, NES_CM_EVENT_RESET);
1381 cleanup_retrans_entry(cm_node);
1382 cm_node->state = NES_CM_STATE_CLOSED;
1383 dev_kfree_skb_any(skb);
1384 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001385 case NES_CM_STATE_ESTABLISHED:
1386 case NES_CM_STATE_SYN_RCVD:
1387 case NES_CM_STATE_LISTENING:
1388 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1389 passive_open_err(cm_node, skb, reset);
1390 break;
1391 case NES_CM_STATE_TSA:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001392 active_open_err(cm_node, skb, reset);
1393 break;
1394 case NES_CM_STATE_CLOSED:
1395 cleanup_retrans_entry(cm_node);
1396 drop_packet(skb);
1397 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001398 default:
Faisal Latif183ecfa2008-11-21 20:50:46 -06001399 drop_packet(skb);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001400 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001401 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001402}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001403
Faisal Latif6492cdf2008-07-24 20:50:45 -07001404static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb,
1405 enum nes_cm_event_type type)
1406{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001407
Faisal Latif6492cdf2008-07-24 20:50:45 -07001408 int ret;
1409 int datasize = skb->len;
1410 u8 *dataloc = skb->data;
1411 ret = parse_mpa(cm_node, dataloc, datasize);
1412 if (ret < 0) {
1413 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
1414 if (type == NES_CM_EVENT_CONNECTED) {
1415 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1416 "cm_node=%p listener=%p state=%d\n", __func__,
1417 __LINE__, cm_node, cm_node->listener,
1418 cm_node->state);
1419 active_open_err(cm_node, skb, 1);
1420 } else {
1421 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001422 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001423 } else {
1424 cleanup_retrans_entry(cm_node);
1425 dev_kfree_skb_any(skb);
1426 if (type == NES_CM_EVENT_CONNECTED)
1427 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif183ecfa2008-11-21 20:50:46 -06001428 else
1429 atomic_set(&cm_node->passive_state,
1430 NES_PASSIVE_STATE_INDICATED);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001431 create_event(cm_node, type);
1432
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001433 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001434 return ;
1435}
1436
1437static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1438{
1439 switch (cm_node->state) {
1440 case NES_CM_STATE_SYN_SENT:
1441 case NES_CM_STATE_MPAREQ_SENT:
1442 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1443 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1444 cm_node->listener, cm_node->state);
1445 active_open_err(cm_node, skb, 1);
1446 break;
1447 case NES_CM_STATE_ESTABLISHED:
1448 case NES_CM_STATE_SYN_RCVD:
1449 passive_open_err(cm_node, skb, 1);
1450 break;
1451 case NES_CM_STATE_TSA:
1452 default:
1453 drop_packet(skb);
1454 }
1455}
1456
1457static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1458 struct sk_buff *skb)
1459{
1460 int err;
1461
1462 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1463 if (err)
1464 active_open_err(cm_node, skb, 1);
1465
1466 return err;
1467}
1468
1469static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1470 struct sk_buff *skb)
1471{
1472 int err = 0;
1473 u32 seq;
1474 u32 ack_seq;
1475 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1476 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1477 u32 rcv_wnd;
1478 seq = ntohl(tcph->seq);
1479 ack_seq = ntohl(tcph->ack_seq);
1480 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1481 if (ack_seq != loc_seq_num)
1482 err = 1;
1483 else if ((seq + rcv_wnd) < rcv_nxt)
1484 err = 1;
1485 if (err) {
1486 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1487 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1488 cm_node->listener, cm_node->state);
1489 indicate_pkt_err(cm_node, skb);
1490 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1491 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1492 rcv_wnd);
1493 }
1494 return err;
1495}
1496
1497/*
1498 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1499 * is created with a listener or it may comein as rexmitted packet which in
1500 * that case will be just dropped.
1501 */
1502
1503static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1504 struct tcphdr *tcph)
1505{
1506 int ret;
1507 u32 inc_sequence;
1508 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001509
1510 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001511 skb_pull(skb, tcph->doff << 2);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001512 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001513
Faisal Latif6492cdf2008-07-24 20:50:45 -07001514 switch (cm_node->state) {
1515 case NES_CM_STATE_SYN_SENT:
1516 case NES_CM_STATE_MPAREQ_SENT:
1517 /* Rcvd syn on active open connection*/
1518 active_open_err(cm_node, skb, 1);
1519 break;
1520 case NES_CM_STATE_LISTENING:
1521 /* Passive OPEN */
1522 cm_node->accept_pend = 1;
1523 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1524 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1525 cm_node->listener->backlog) {
1526 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1527 "pressure \n");
1528 cm_backlog_drops++;
1529 passive_open_err(cm_node, skb, 0);
1530 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001531 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001532 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1533 1);
1534 if (ret) {
1535 passive_open_err(cm_node, skb, 0);
1536 /* drop pkt */
1537 break;
1538 }
1539 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1540 BUG_ON(cm_node->send_entry);
1541 cm_node->state = NES_CM_STATE_SYN_RCVD;
1542 send_syn(cm_node, 1, skb);
1543 break;
1544 case NES_CM_STATE_TSA:
1545 case NES_CM_STATE_ESTABLISHED:
1546 case NES_CM_STATE_FIN_WAIT1:
1547 case NES_CM_STATE_FIN_WAIT2:
1548 case NES_CM_STATE_MPAREQ_RCVD:
1549 case NES_CM_STATE_LAST_ACK:
1550 case NES_CM_STATE_CLOSING:
1551 case NES_CM_STATE_UNKNOWN:
1552 case NES_CM_STATE_CLOSED:
1553 default:
1554 drop_packet(skb);
1555 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001556 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001557}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001558
Faisal Latif6492cdf2008-07-24 20:50:45 -07001559static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1560 struct tcphdr *tcph)
1561{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001562
Faisal Latif6492cdf2008-07-24 20:50:45 -07001563 int ret;
1564 u32 inc_sequence;
1565 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001566
Faisal Latif6492cdf2008-07-24 20:50:45 -07001567 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1568 skb_pull(skb, tcph->doff << 2);
1569 inc_sequence = ntohl(tcph->seq);
1570 switch (cm_node->state) {
1571 case NES_CM_STATE_SYN_SENT:
1572 /* active open */
1573 if (check_syn(cm_node, tcph, skb))
1574 return;
1575 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1576 /* setup options */
1577 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1578 if (ret) {
1579 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1580 cm_node);
1581 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001582 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001583 cleanup_retrans_entry(cm_node);
1584 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1585 send_mpa_request(cm_node, skb);
1586 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1587 break;
1588 case NES_CM_STATE_MPAREQ_RCVD:
1589 /* passive open, so should not be here */
1590 passive_open_err(cm_node, skb, 1);
1591 break;
1592 case NES_CM_STATE_ESTABLISHED:
1593 case NES_CM_STATE_FIN_WAIT1:
1594 case NES_CM_STATE_FIN_WAIT2:
1595 case NES_CM_STATE_LAST_ACK:
1596 case NES_CM_STATE_TSA:
1597 case NES_CM_STATE_CLOSING:
1598 case NES_CM_STATE_UNKNOWN:
1599 case NES_CM_STATE_CLOSED:
1600 case NES_CM_STATE_MPAREQ_SENT:
1601 default:
1602 drop_packet(skb);
1603 break;
1604 }
1605}
1606
1607static void handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1608 struct tcphdr *tcph)
1609{
1610 int datasize = 0;
1611 u32 inc_sequence;
1612 u32 rem_seq_ack;
1613 u32 rem_seq;
1614 if (check_seq(cm_node, tcph, skb))
1615 return;
1616
1617 skb_pull(skb, tcph->doff << 2);
1618 inc_sequence = ntohl(tcph->seq);
1619 rem_seq = ntohl(tcph->seq);
1620 rem_seq_ack = ntohl(tcph->ack_seq);
1621 datasize = skb->len;
1622
1623 switch (cm_node->state) {
1624 case NES_CM_STATE_SYN_RCVD:
1625 /* Passive OPEN */
1626 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1627 cm_node->state = NES_CM_STATE_ESTABLISHED;
1628 if (datasize) {
1629 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1630 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1631 handle_rcv_mpa(cm_node, skb, NES_CM_EVENT_MPA_REQ);
1632 } else { /* rcvd ACK only */
1633 dev_kfree_skb_any(skb);
1634 cleanup_retrans_entry(cm_node);
1635 }
1636 break;
1637 case NES_CM_STATE_ESTABLISHED:
1638 /* Passive OPEN */
1639 /* We expect mpa frame to be received only */
1640 if (datasize) {
1641 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1642 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1643 handle_rcv_mpa(cm_node, skb,
1644 NES_CM_EVENT_MPA_REQ);
1645 } else
1646 drop_packet(skb);
1647 break;
1648 case NES_CM_STATE_MPAREQ_SENT:
1649 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1650 if (datasize) {
1651 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1652 handle_rcv_mpa(cm_node, skb, NES_CM_EVENT_CONNECTED);
1653 } else { /* Could be just an ack pkt.. */
1654 cleanup_retrans_entry(cm_node);
1655 dev_kfree_skb_any(skb);
1656 }
1657 break;
1658 case NES_CM_STATE_FIN_WAIT1:
1659 case NES_CM_STATE_SYN_SENT:
1660 case NES_CM_STATE_FIN_WAIT2:
1661 case NES_CM_STATE_TSA:
1662 case NES_CM_STATE_CLOSED:
1663 case NES_CM_STATE_MPAREQ_RCVD:
1664 case NES_CM_STATE_LAST_ACK:
1665 case NES_CM_STATE_CLOSING:
1666 case NES_CM_STATE_UNKNOWN:
1667 default:
1668 drop_packet(skb);
1669 break;
1670 }
1671}
1672
1673
1674
1675static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1676 struct sk_buff *skb, int optionsize, int passive)
1677{
1678 u8 *optionsloc = (u8 *)&tcph[1];
1679 if (optionsize) {
1680 if (process_options(cm_node, optionsloc, optionsize,
1681 (u32)tcph->syn)) {
1682 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1683 __func__, cm_node);
1684 if (passive)
1685 passive_open_err(cm_node, skb, 0);
1686 else
1687 active_open_err(cm_node, skb, 0);
1688 return 1;
1689 }
1690 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001691
1692 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1693 cm_node->tcp_cntxt.snd_wscale;
1694
Faisal Latif6492cdf2008-07-24 20:50:45 -07001695 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001696 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001697 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001698}
1699
Faisal Latif6492cdf2008-07-24 20:50:45 -07001700/*
1701 * active_open_err() will send reset() if flag set..
1702 * It will also send ABORT event.
1703 */
1704
1705static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1706 int reset)
1707{
1708 cleanup_retrans_entry(cm_node);
1709 if (reset) {
1710 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1711 "state=%d\n", cm_node, cm_node->state);
1712 add_ref_cm_node(cm_node);
1713 send_reset(cm_node, skb);
1714 } else
1715 dev_kfree_skb_any(skb);
1716
1717 cm_node->state = NES_CM_STATE_CLOSED;
1718 create_event(cm_node, NES_CM_EVENT_ABORTED);
1719}
1720
1721/*
1722 * passive_open_err() will either do a reset() or will free up the skb and
1723 * remove the cm_node.
1724 */
1725
1726static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1727 int reset)
1728{
1729 cleanup_retrans_entry(cm_node);
1730 cm_node->state = NES_CM_STATE_CLOSED;
1731 if (reset) {
1732 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1733 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1734 send_reset(cm_node, skb);
1735 } else {
1736 dev_kfree_skb_any(skb);
1737 rem_ref_cm_node(cm_node->cm_core, cm_node);
1738 }
1739}
1740
1741/*
1742 * free_retrans_entry() routines assumes that the retrans_list_lock has
1743 * been acquired before calling.
1744 */
1745static void free_retrans_entry(struct nes_cm_node *cm_node)
1746{
1747 struct nes_timer_entry *send_entry;
1748 send_entry = cm_node->send_entry;
1749 if (send_entry) {
1750 cm_node->send_entry = NULL;
1751 dev_kfree_skb_any(send_entry->skb);
1752 kfree(send_entry);
1753 rem_ref_cm_node(cm_node->cm_core, cm_node);
1754 }
1755}
1756
1757static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1758{
1759 unsigned long flags;
1760
1761 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1762 free_retrans_entry(cm_node);
1763 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1764}
1765
1766/**
1767 * process_packet
1768 * Returns skb if to be freed, else it will return NULL if already used..
1769 */
1770static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1771 struct nes_cm_core *cm_core)
1772{
1773 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1774 struct tcphdr *tcph = tcp_hdr(skb);
1775 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1776
1777 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1778 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1779 tcph->ack, tcph->rst, tcph->fin);
1780
1781 if (tcph->rst)
1782 pkt_type = NES_PKT_TYPE_RST;
1783 else if (tcph->syn) {
1784 pkt_type = NES_PKT_TYPE_SYN;
1785 if (tcph->ack)
1786 pkt_type = NES_PKT_TYPE_SYNACK;
1787 } else if (tcph->fin)
1788 pkt_type = NES_PKT_TYPE_FIN;
1789 else if (tcph->ack)
1790 pkt_type = NES_PKT_TYPE_ACK;
1791
1792 switch (pkt_type) {
1793 case NES_PKT_TYPE_SYN:
1794 handle_syn_pkt(cm_node, skb, tcph);
1795 break;
1796 case NES_PKT_TYPE_SYNACK:
1797 handle_synack_pkt(cm_node, skb, tcph);
1798 break;
1799 case NES_PKT_TYPE_ACK:
1800 handle_ack_pkt(cm_node, skb, tcph);
1801 break;
1802 case NES_PKT_TYPE_RST:
1803 handle_rst_pkt(cm_node, skb, tcph);
1804 break;
1805 case NES_PKT_TYPE_FIN:
1806 handle_fin_pkt(cm_node, skb, tcph);
1807 break;
1808 default:
1809 drop_packet(skb);
1810 break;
1811 }
1812}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001813
1814/**
1815 * mini_cm_listen - create a listen node with params
1816 */
1817static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001818 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001819{
1820 struct nes_cm_listener *listener;
1821 unsigned long flags;
1822
1823 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1824 cm_info->loc_addr, cm_info->loc_port);
1825
1826 /* cannot have multiple matching listeners */
1827 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1828 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1829 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1830 /* find automatically incs ref count ??? */
1831 atomic_dec(&listener->ref_count);
1832 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1833 return NULL;
1834 }
1835
1836 if (!listener) {
1837 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1838 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1839 if (!listener) {
1840 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1841 return NULL;
1842 }
1843
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001844 listener->loc_addr = htonl(cm_info->loc_addr);
1845 listener->loc_port = htons(cm_info->loc_port);
1846 listener->reused_node = 0;
1847
1848 atomic_set(&listener->ref_count, 1);
1849 }
1850 /* pasive case */
1851 /* find already inc'ed the ref count */
1852 else {
1853 listener->reused_node = 1;
1854 }
1855
1856 listener->cm_id = cm_info->cm_id;
1857 atomic_set(&listener->pend_accepts_cnt, 0);
1858 listener->cm_core = cm_core;
1859 listener->nesvnic = nesvnic;
1860 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001861
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001862 listener->conn_type = cm_info->conn_type;
1863 listener->backlog = cm_info->backlog;
1864 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1865
1866 if (!listener->reused_node) {
1867 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1868 list_add(&listener->list, &cm_core->listen_list.list);
1869 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1870 atomic_inc(&cm_core->listen_node_cnt);
1871 }
1872
1873 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1874 " listener = %p, backlog = %d, cm_id = %p.\n",
1875 cm_info->loc_addr, cm_info->loc_port,
1876 listener, listener->backlog, listener->cm_id);
1877
1878 return listener;
1879}
1880
1881
1882/**
1883 * mini_cm_connect - make a connection node with params
1884 */
Faisal Latif54c86a82008-09-30 14:47:27 -07001885static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001886 struct nes_vnic *nesvnic, u16 private_data_len,
1887 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001888{
1889 int ret = 0;
1890 struct nes_cm_node *cm_node;
1891 struct nes_cm_listener *loopbackremotelistener;
1892 struct nes_cm_node *loopbackremotenode;
1893 struct nes_cm_info loopback_cm_info;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001894 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
1895 struct ietf_mpa_frame *mpa_frame = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001896
1897 /* create a CM connection node */
1898 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
1899 if (!cm_node)
1900 return NULL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001901 mpa_frame = &cm_node->mpa_frame;
1902 strcpy(mpa_frame->key, IEFT_MPA_KEY_REQ);
1903 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
1904 mpa_frame->rev = IETF_MPA_VERSION;
1905 mpa_frame->priv_data_len = htons(private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001906
Glenn Streiff7495ab62008-04-29 13:46:54 -07001907 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001908 cm_node->tcp_cntxt.client = 1;
1909 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1910
1911 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001912 loopbackremotelistener = find_listener(cm_core,
1913 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
1914 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001915 if (loopbackremotelistener == NULL) {
1916 create_event(cm_node, NES_CM_EVENT_ABORTED);
1917 } else {
1918 atomic_inc(&cm_loopbacks);
1919 loopback_cm_info = *cm_info;
1920 loopback_cm_info.loc_port = cm_info->rem_port;
1921 loopback_cm_info.rem_port = cm_info->loc_port;
1922 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001923 loopbackremotenode = make_cm_node(cm_core, nesvnic,
1924 &loopback_cm_info, loopbackremotelistener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001925 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001926 loopbackremotenode->tcp_cntxt.rcv_wscale =
1927 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001928 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001929 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
1930 private_data_len);
1931 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001932
Faisal Latif6492cdf2008-07-24 20:50:45 -07001933 /* we are done handling this state. */
1934 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001935 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001936 cm_node->tcp_cntxt.rcv_nxt =
1937 loopbackremotenode->tcp_cntxt.loc_seq_num;
1938 loopbackremotenode->tcp_cntxt.rcv_nxt =
1939 cm_node->tcp_cntxt.loc_seq_num;
1940 cm_node->tcp_cntxt.max_snd_wnd =
1941 loopbackremotenode->tcp_cntxt.rcv_wnd;
1942 loopbackremotenode->tcp_cntxt.max_snd_wnd =
1943 cm_node->tcp_cntxt.rcv_wnd;
1944 cm_node->tcp_cntxt.snd_wnd =
1945 loopbackremotenode->tcp_cntxt.rcv_wnd;
1946 loopbackremotenode->tcp_cntxt.snd_wnd =
1947 cm_node->tcp_cntxt.rcv_wnd;
1948 cm_node->tcp_cntxt.snd_wscale =
1949 loopbackremotenode->tcp_cntxt.rcv_wscale;
1950 loopbackremotenode->tcp_cntxt.snd_wscale =
1951 cm_node->tcp_cntxt.rcv_wscale;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001952
1953 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
1954 }
1955 return cm_node;
1956 }
1957
1958 /* set our node side to client (active) side */
1959 cm_node->tcp_cntxt.client = 1;
1960 /* init our MPA frame ptr */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001961 memcpy(mpa_frame->priv_data, private_data, private_data_len);
1962
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001963 cm_node->mpa_frame_size = mpa_frame_size;
1964
1965 /* send a syn and goto syn sent state */
1966 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001967 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001968
Faisal Latif6492cdf2008-07-24 20:50:45 -07001969 if (ret) {
1970 /* error in sending the syn free up the cm_node struct */
1971 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
1972 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
1973 cm_node->rem_addr, cm_node->rem_port, cm_node,
1974 cm_node->cm_id);
1975 rem_ref_cm_node(cm_node->cm_core, cm_node);
1976 cm_node = NULL;
1977 }
1978
1979 if (cm_node)
1980 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
1981 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
1982 cm_node->rem_addr, cm_node->rem_port, cm_node,
1983 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001984
1985 return cm_node;
1986}
1987
1988
1989/**
1990 * mini_cm_accept - accept a connection
1991 * This function is never called
1992 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001993static int mini_cm_accept(struct nes_cm_core *cm_core,
1994 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001995{
1996 return 0;
1997}
1998
1999
2000/**
2001 * mini_cm_reject - reject and teardown a connection
2002 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002003static int mini_cm_reject(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07002004 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002005{
2006 int ret = 0;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002007 int passive_state;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002008
Faisal Latif6492cdf2008-07-24 20:50:45 -07002009 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2010 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002011
Faisal Latif6492cdf2008-07-24 20:50:45 -07002012 if (cm_node->tcp_cntxt.client)
2013 return ret;
2014 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002015
Faisal Latif183ecfa2008-11-21 20:50:46 -06002016 passive_state = atomic_add_return(1, &cm_node->passive_state);
2017 cm_node->state = NES_CM_STATE_CLOSED;
2018 if (passive_state == NES_SEND_RESET_EVENT)
2019 rem_ref_cm_node(cm_core, cm_node);
2020 else
2021 ret = send_reset(cm_node, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002022 return ret;
2023}
2024
2025
2026/**
2027 * mini_cm_close
2028 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002029static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002030{
2031 int ret = 0;
2032
2033 if (!cm_core || !cm_node)
2034 return -EINVAL;
2035
2036 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002037 case NES_CM_STATE_SYN_RCVD:
2038 case NES_CM_STATE_SYN_SENT:
2039 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
2040 case NES_CM_STATE_ESTABLISHED:
2041 case NES_CM_STATE_ACCEPTING:
2042 case NES_CM_STATE_MPAREQ_SENT:
2043 case NES_CM_STATE_MPAREQ_RCVD:
2044 cleanup_retrans_entry(cm_node);
2045 send_reset(cm_node, NULL);
2046 break;
2047 case NES_CM_STATE_CLOSE_WAIT:
2048 cm_node->state = NES_CM_STATE_LAST_ACK;
2049 send_fin(cm_node, NULL);
2050 break;
2051 case NES_CM_STATE_FIN_WAIT1:
2052 case NES_CM_STATE_FIN_WAIT2:
2053 case NES_CM_STATE_LAST_ACK:
2054 case NES_CM_STATE_TIME_WAIT:
2055 case NES_CM_STATE_CLOSING:
2056 ret = -1;
2057 break;
2058 case NES_CM_STATE_LISTENING:
2059 case NES_CM_STATE_UNKNOWN:
2060 case NES_CM_STATE_INITED:
2061 case NES_CM_STATE_CLOSED:
2062 ret = rem_ref_cm_node(cm_core, cm_node);
2063 break;
2064 case NES_CM_STATE_TSA:
2065 if (cm_node->send_entry)
2066 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2067 "send_entry=%p\n", cm_node->send_entry);
2068 ret = rem_ref_cm_node(cm_core, cm_node);
2069 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002070 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002071 return ret;
2072}
2073
2074
2075/**
2076 * recv_pkt - recv an ETHERNET packet, and process it through CM
2077 * node state machine
2078 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002079static void mini_cm_recv_pkt(struct nes_cm_core *cm_core,
2080 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002081{
2082 struct nes_cm_node *cm_node = NULL;
2083 struct nes_cm_listener *listener = NULL;
2084 struct iphdr *iph;
2085 struct tcphdr *tcph;
2086 struct nes_cm_info nfo;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002087
Faisal Latif6492cdf2008-07-24 20:50:45 -07002088 if (!skb)
2089 return;
2090 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
2091 dev_kfree_skb_any(skb);
2092 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002093 }
2094
2095 iph = (struct iphdr *)skb->data;
2096 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
2097 skb_reset_network_header(skb);
2098 skb_set_transport_header(skb, sizeof(*tcph));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002099 if (!tcph) {
2100 dev_kfree_skb_any(skb);
2101 return;
2102 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002103 skb->len = ntohs(iph->tot_len);
2104
2105 nfo.loc_addr = ntohl(iph->daddr);
2106 nfo.loc_port = ntohs(tcph->dest);
2107 nfo.rem_addr = ntohl(iph->saddr);
2108 nfo.rem_port = ntohs(tcph->source);
2109
Chien Tungbc5698f32008-04-23 11:55:45 -07002110 nes_debug(NES_DBG_CM, "Received packet: dest=" NIPQUAD_FMT
2111 ":0x%04X src=" NIPQUAD_FMT ":0x%04X\n",
2112 NIPQUAD(iph->daddr), tcph->dest,
2113 NIPQUAD(iph->saddr), tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002114
Faisal Latif6492cdf2008-07-24 20:50:45 -07002115 do {
2116 cm_node = find_node(cm_core,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002117 nfo.rem_port, nfo.rem_addr,
2118 nfo.loc_port, nfo.loc_addr);
2119
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002120 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002121 /* Only type of packet accepted are for */
2122 /* the PASSIVE open (syn only) */
2123 if ((!tcph->syn) || (tcph->ack)) {
2124 cm_packets_dropped++;
2125 break;
2126 }
2127 listener = find_listener(cm_core, nfo.loc_addr,
2128 nfo.loc_port,
2129 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002130 if (listener) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002131 nfo.cm_id = listener->cm_id;
2132 nfo.conn_type = listener->conn_type;
2133 } else {
2134 nes_debug(NES_DBG_CM, "Unable to find listener "
2135 "for the pkt\n");
2136 cm_packets_dropped++;
2137 dev_kfree_skb_any(skb);
2138 break;
2139 }
2140
2141 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2142 listener);
2143 if (!cm_node) {
2144 nes_debug(NES_DBG_CM, "Unable to allocate "
2145 "node\n");
2146 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002147 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002148 dev_kfree_skb_any(skb);
2149 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002150 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002151 if (!tcph->rst && !tcph->fin) {
2152 cm_node->state = NES_CM_STATE_LISTENING;
2153 } else {
2154 cm_packets_dropped++;
2155 rem_ref_cm_node(cm_core, cm_node);
2156 dev_kfree_skb_any(skb);
2157 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002158 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002159 add_ref_cm_node(cm_node);
2160 } else if (cm_node->state == NES_CM_STATE_TSA) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002161 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002162 atomic_inc(&cm_accel_dropped_pkts);
2163 dev_kfree_skb_any(skb);
2164 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002165 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002166 process_packet(cm_node, skb, cm_core);
2167 rem_ref_cm_node(cm_core, cm_node);
2168 } while (0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002169}
2170
2171
2172/**
2173 * nes_cm_alloc_core - allocate a top level instance of a cm core
2174 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002175static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002176{
2177 int i;
2178
2179 struct nes_cm_core *cm_core;
2180 struct sk_buff *skb = NULL;
2181
2182 /* setup the CM core */
2183 /* alloc top level core control structure */
2184 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2185 if (!cm_core)
2186 return NULL;
2187
2188 INIT_LIST_HEAD(&cm_core->connected_nodes);
2189 init_timer(&cm_core->tcp_timer);
2190 cm_core->tcp_timer.function = nes_cm_timer_tick;
2191
2192 cm_core->mtu = NES_CM_DEFAULT_MTU;
2193 cm_core->state = NES_CM_STATE_INITED;
2194 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2195
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002196 atomic_set(&cm_core->events_posted, 0);
2197
2198 /* init the packet lists */
2199 skb_queue_head_init(&cm_core->tx_free_list);
2200
2201 for (i = 0; i < NES_CM_DEFAULT_FRAME_CNT; i++) {
2202 skb = dev_alloc_skb(cm_core->mtu);
2203 if (!skb) {
2204 kfree(cm_core);
2205 return NULL;
2206 }
2207 /* add 'raw' skb to free frame list */
2208 skb_queue_head(&cm_core->tx_free_list, skb);
2209 }
2210
2211 cm_core->api = &nes_cm_api;
2212
2213 spin_lock_init(&cm_core->ht_lock);
2214 spin_lock_init(&cm_core->listen_list_lock);
2215
2216 INIT_LIST_HEAD(&cm_core->listen_list.list);
2217
2218 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2219
2220 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2221 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2222 cm_core->post_event = nes_cm_post_event;
2223 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2224 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2225
2226 print_core(cm_core);
2227 return cm_core;
2228}
2229
2230
2231/**
2232 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2233 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002234static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002235{
2236 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2237
2238 if (!cm_core)
2239 return -EINVAL;
2240
2241 barrier();
2242
2243 if (timer_pending(&cm_core->tcp_timer)) {
2244 del_timer(&cm_core->tcp_timer);
2245 }
2246
2247 destroy_workqueue(cm_core->event_wq);
2248 destroy_workqueue(cm_core->disconn_wq);
2249 nes_debug(NES_DBG_CM, "\n");
2250 kfree(cm_core);
2251
2252 return 0;
2253}
2254
2255
2256/**
2257 * mini_cm_get
2258 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002259static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002260{
2261 return cm_core->state;
2262}
2263
2264
2265/**
2266 * mini_cm_set
2267 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002268static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002269{
2270 int ret = 0;
2271
2272 switch (type) {
2273 case NES_CM_SET_PKT_SIZE:
2274 cm_core->mtu = value;
2275 break;
2276 case NES_CM_SET_FREE_PKT_Q_SIZE:
2277 cm_core->free_tx_pkt_max = value;
2278 break;
2279 default:
2280 /* unknown set option */
2281 ret = -EINVAL;
2282 }
2283
2284 return ret;
2285}
2286
2287
2288/**
2289 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2290 * successfully exchanged when this is called
2291 */
2292static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2293{
2294 int ret = 0;
2295
2296 if (!nesqp)
2297 return -EINVAL;
2298
2299 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2300 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2301 NES_QPCONTEXT_MISC_DROS);
2302
2303 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2304 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2305
2306 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2307
2308 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2309
2310 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2311 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2312
2313 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2314 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2315 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2316
2317 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2318 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2319 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2320
2321 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2322 nesqp->nesqp_context->ts_recent = 0;
2323 nesqp->nesqp_context->ts_age = 0;
2324 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2325 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2326 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2327 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2328 cm_node->tcp_cntxt.rcv_wscale);
2329 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2330 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2331 nesqp->nesqp_context->srtt = 0;
2332 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2333 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2334 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2335 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2336 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2337 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2338
2339 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2340 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2341 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2342 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2343 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2344 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2345 le32_to_cpu(nesqp->nesqp_context->misc));
2346 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2347 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2348 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2349
2350 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2351 cm_node->state = NES_CM_STATE_TSA;
2352
2353 return ret;
2354}
2355
2356
2357/**
2358 * nes_cm_disconn
2359 */
2360int nes_cm_disconn(struct nes_qp *nesqp)
2361{
2362 unsigned long flags;
2363
2364 spin_lock_irqsave(&nesqp->lock, flags);
2365 if (nesqp->disconn_pending == 0) {
2366 nesqp->disconn_pending++;
2367 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002368 /* init our disconnect work element, to */
2369 INIT_WORK(&nesqp->disconn_work, nes_disconnect_worker);
2370
2371 queue_work(g_cm_core->disconn_wq, &nesqp->disconn_work);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002372 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002373 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002374
2375 return 0;
2376}
2377
2378
2379/**
2380 * nes_disconnect_worker
2381 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002382static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002383{
2384 struct nes_qp *nesqp = container_of(work, struct nes_qp, disconn_work);
2385
2386 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2387 nesqp->last_aeq, nesqp->hwqp.qp_id);
2388 nes_cm_disconn_true(nesqp);
2389}
2390
2391
2392/**
2393 * nes_cm_disconn_true
2394 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002395static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002396{
2397 unsigned long flags;
2398 int ret = 0;
2399 struct iw_cm_id *cm_id;
2400 struct iw_cm_event cm_event;
2401 struct nes_vnic *nesvnic;
2402 u16 last_ae;
2403 u8 original_hw_tcp_state;
2404 u8 original_ibqp_state;
2405 u8 issued_disconnect_reset = 0;
2406
2407 if (!nesqp) {
2408 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2409 return -1;
2410 }
2411
2412 spin_lock_irqsave(&nesqp->lock, flags);
2413 cm_id = nesqp->cm_id;
2414 /* make sure we havent already closed this connection */
2415 if (!cm_id) {
2416 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2417 nesqp->hwqp.qp_id);
2418 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002419 return -1;
2420 }
2421
2422 nesvnic = to_nesvnic(nesqp->ibqp.device);
2423 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2424
2425 original_hw_tcp_state = nesqp->hw_tcp_state;
2426 original_ibqp_state = nesqp->ibqp_state;
2427 last_ae = nesqp->last_aeq;
2428
2429
2430 nes_debug(NES_DBG_CM, "set ibqp_state=%u\n", nesqp->ibqp_state);
2431
2432 if ((nesqp->cm_id) && (cm_id->event_handler)) {
2433 if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2434 ((original_ibqp_state == IB_QPS_RTS) &&
2435 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2436 atomic_inc(&cm_disconnects);
2437 cm_event.event = IW_CM_EVENT_DISCONNECT;
2438 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002439 cm_event.status = IW_CM_EVENT_STATUS_RESET;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002440 nes_debug(NES_DBG_CM, "Generating a CM "
2441 "Disconnect Event (status reset) for "
2442 "QP%u, cm_id = %p. \n",
2443 nesqp->hwqp.qp_id, cm_id);
2444 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002445 cm_event.status = IW_CM_EVENT_STATUS_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002446
2447 cm_event.local_addr = cm_id->local_addr;
2448 cm_event.remote_addr = cm_id->remote_addr;
2449 cm_event.private_data = NULL;
2450 cm_event.private_data_len = 0;
2451
Faisal Latif6492cdf2008-07-24 20:50:45 -07002452 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2453 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2454 "cm_id = %p, refcount = %u.\n",
2455 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2456 nesqp->hwqp.sq_tail, cm_id,
2457 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002458
2459 spin_unlock_irqrestore(&nesqp->lock, flags);
2460 ret = cm_id->event_handler(cm_id, &cm_event);
2461 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002462 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2463 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002464 spin_lock_irqsave(&nesqp->lock, flags);
2465 }
2466
2467 nesqp->disconn_pending = 0;
2468 /* There might have been another AE while the lock was released */
2469 original_hw_tcp_state = nesqp->hw_tcp_state;
2470 original_ibqp_state = nesqp->ibqp_state;
2471 last_ae = nesqp->last_aeq;
2472
2473 if ((issued_disconnect_reset == 0) && (nesqp->cm_id) &&
2474 ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2475 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2476 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2477 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2478 atomic_inc(&cm_closes);
2479 nesqp->cm_id = NULL;
2480 nesqp->in_disconnect = 0;
2481 spin_unlock_irqrestore(&nesqp->lock, flags);
2482 nes_disconnect(nesqp, 1);
2483
2484 cm_id->provider_data = nesqp;
2485 /* Send up the close complete event */
2486 cm_event.event = IW_CM_EVENT_CLOSE;
2487 cm_event.status = IW_CM_EVENT_STATUS_OK;
2488 cm_event.provider_data = cm_id->provider_data;
2489 cm_event.local_addr = cm_id->local_addr;
2490 cm_event.remote_addr = cm_id->remote_addr;
2491 cm_event.private_data = NULL;
2492 cm_event.private_data_len = 0;
2493
2494 ret = cm_id->event_handler(cm_id, &cm_event);
2495 if (ret) {
2496 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2497 }
2498
2499 cm_id->rem_ref(cm_id);
2500
2501 spin_lock_irqsave(&nesqp->lock, flags);
2502 if (nesqp->flush_issued == 0) {
2503 nesqp->flush_issued = 1;
2504 spin_unlock_irqrestore(&nesqp->lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002505 flush_wqes(nesvnic->nesdev, nesqp,
2506 NES_CQP_FLUSH_RQ, 1);
2507 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002508 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002509 } else {
2510 cm_id = nesqp->cm_id;
2511 spin_unlock_irqrestore(&nesqp->lock, flags);
2512 /* check to see if the inbound reset beat the outbound reset */
2513 if ((!cm_id) && (last_ae==NES_AEQE_AEID_RESET_SENT)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002514 nes_debug(NES_DBG_CM, "QP%u: Decing refcount "
2515 "due to inbound reset beating the "
2516 "outbound reset.\n", nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002517 }
2518 }
2519 } else {
2520 nesqp->disconn_pending = 0;
2521 spin_unlock_irqrestore(&nesqp->lock, flags);
2522 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002523
2524 return 0;
2525}
2526
2527
2528/**
2529 * nes_disconnect
2530 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002531static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002532{
2533 int ret = 0;
2534 struct nes_vnic *nesvnic;
2535 struct nes_device *nesdev;
2536
2537 nesvnic = to_nesvnic(nesqp->ibqp.device);
2538 if (!nesvnic)
2539 return -EINVAL;
2540
2541 nesdev = nesvnic->nesdev;
2542
2543 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2544 atomic_read(&nesvnic->netdev->refcnt));
2545
2546 if (nesqp->active_conn) {
2547
2548 /* indicate this connection is NOT active */
2549 nesqp->active_conn = 0;
2550 } else {
2551 /* Need to free the Last Streaming Mode Message */
2552 if (nesqp->ietf_frame) {
2553 pci_free_consistent(nesdev->pcidev,
2554 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2555 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2556 }
2557 }
2558
2559 /* close the CM node down if it is still active */
2560 if (nesqp->cm_node) {
2561 nes_debug(NES_DBG_CM, "Call close API\n");
2562
2563 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002564 }
2565
2566 return ret;
2567}
2568
2569
2570/**
2571 * nes_accept
2572 */
2573int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2574{
2575 u64 u64temp;
2576 struct ib_qp *ibqp;
2577 struct nes_qp *nesqp;
2578 struct nes_vnic *nesvnic;
2579 struct nes_device *nesdev;
2580 struct nes_cm_node *cm_node;
2581 struct nes_adapter *adapter;
2582 struct ib_qp_attr attr;
2583 struct iw_cm_event cm_event;
2584 struct nes_hw_qp_wqe *wqe;
2585 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002586 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002587 int ret;
Faisal Latif183ecfa2008-11-21 20:50:46 -06002588 int passive_state;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002589
2590 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2591 if (!ibqp)
2592 return -EINVAL;
2593
2594 /* get all our handles */
2595 nesqp = to_nesqp(ibqp);
2596 nesvnic = to_nesvnic(nesqp->ibqp.device);
2597 nesdev = nesvnic->nesdev;
2598 adapter = nesdev->nesadapter;
2599
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002600 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002601 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2602 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2603 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002604
2605 /* associate the node with the QP */
2606 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002607 cm_node->nesqp = nesqp;
2608 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002609
Faisal Latif6492cdf2008-07-24 20:50:45 -07002610 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2611 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002612 atomic_inc(&cm_accepts);
2613
2614 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2615 atomic_read(&nesvnic->netdev->refcnt));
2616
Faisal Latif6492cdf2008-07-24 20:50:45 -07002617 /* allocate the ietf frame and space for private data */
2618 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2619 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2620 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002621
Faisal Latif6492cdf2008-07-24 20:50:45 -07002622 if (!nesqp->ietf_frame) {
2623 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2624 "data\n");
2625 return -ENOMEM;
2626 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002627
2628
Faisal Latif6492cdf2008-07-24 20:50:45 -07002629 /* setup the MPA frame */
2630 nesqp->private_data_len = conn_param->private_data_len;
2631 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002632
Faisal Latif6492cdf2008-07-24 20:50:45 -07002633 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2634 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002635
Faisal Latif6492cdf2008-07-24 20:50:45 -07002636 nesqp->ietf_frame->priv_data_len =
2637 cpu_to_be16(conn_param->private_data_len);
2638 nesqp->ietf_frame->rev = mpa_version;
2639 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002640
Faisal Latif6492cdf2008-07-24 20:50:45 -07002641 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2642 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002643
Faisal Latif6492cdf2008-07-24 20:50:45 -07002644 if (cm_id->remote_addr.sin_addr.s_addr !=
2645 cm_id->local_addr.sin_addr.s_addr) {
2646 u64temp = (unsigned long)nesqp;
2647 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2648 set_wqe_64bit_value(wqe->wqe_words,
2649 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2650 u64temp);
2651 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2652 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2653 NES_IWARP_SQ_WQE_WRPDU);
2654 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2655 cpu_to_le32(conn_param->private_data_len +
2656 sizeof(struct ietf_mpa_frame));
2657 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] =
2658 cpu_to_le32((u32)nesqp->ietf_frame_pbase);
2659 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] =
2660 cpu_to_le32((u32)((u64)nesqp->ietf_frame_pbase >> 32));
2661 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2662 cpu_to_le32(conn_param->private_data_len +
2663 sizeof(struct ietf_mpa_frame));
2664 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002665
Faisal Latif6492cdf2008-07-24 20:50:45 -07002666 nesqp->nesqp_context->ird_ord_sizes |=
2667 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2668 NES_QPCONTEXT_ORDIRD_WRPDU);
2669 } else {
2670 nesqp->nesqp_context->ird_ord_sizes |=
2671 cpu_to_le32((NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2672 NES_QPCONTEXT_ORDIRD_WRPDU |
2673 NES_QPCONTEXT_ORDIRD_ALSMM));
2674 }
2675 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002676
2677
2678 /* Cache the cm_id in the qp */
2679 nesqp->cm_id = cm_id;
2680 cm_node->cm_id = cm_id;
2681
2682 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2683 cm_id->provider_data = nesqp;
2684 nesqp->active_conn = 0;
2685
Faisal Latif6492cdf2008-07-24 20:50:45 -07002686 if (cm_node->state == NES_CM_STATE_TSA)
2687 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2688 cm_node);
2689
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002690 nes_cm_init_tsa_conn(nesqp, cm_node);
2691
Faisal Latif6492cdf2008-07-24 20:50:45 -07002692 nesqp->nesqp_context->tcpPorts[0] =
2693 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2694 nesqp->nesqp_context->tcpPorts[1] =
2695 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2696
2697 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2698 nesqp->nesqp_context->ip0 =
2699 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2700 else
2701 nesqp->nesqp_context->ip0 =
2702 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002703
2704 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002705 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2706 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002707
Faisal Latif6492cdf2008-07-24 20:50:45 -07002708 nesqp->nesqp_context->arp_index_vlan |=
2709 cpu_to_le32(nes_arp_table(nesdev,
2710 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002711 NES_ARP_RESOLVE) << 16);
2712
2713 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002714 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002715
2716 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2717
2718 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002719 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2720 nesqp->nesqp_context->ird_ord_sizes |=
2721 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002722
2723 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002724 nes_quad.DstIpAdrIndex =
2725 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2726 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2727 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2728 else
2729 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2730 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2731 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002732
2733 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06002734 crc_value = get_crc_value(&nes_quad);
2735 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002736 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07002737 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002738
2739 nesqp->hte_index &= adapter->hte_index_mask;
2740 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2741
2742 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2743
Faisal Latif6492cdf2008-07-24 20:50:45 -07002744 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2745 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2746 "private data length=%zu.\n", nesqp->hwqp.qp_id,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002747 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2748 ntohs(cm_id->remote_addr.sin_port),
2749 ntohl(cm_id->local_addr.sin_addr.s_addr),
2750 ntohs(cm_id->local_addr.sin_port),
2751 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2752 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002753 conn_param->private_data_len +
2754 sizeof(struct ietf_mpa_frame));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002755
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002756
2757 /* notify OF layer that accept event was successfull */
2758 cm_id->add_ref(cm_id);
2759
2760 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2761 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2762 cm_event.provider_data = (void *)nesqp;
2763 cm_event.local_addr = cm_id->local_addr;
2764 cm_event.remote_addr = cm_id->remote_addr;
2765 cm_event.private_data = NULL;
2766 cm_event.private_data_len = 0;
2767 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06002768 attr.qp_state = IB_QPS_RTS;
2769 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002770 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002771 cm_node->loopbackpartner->mpa_frame_size =
2772 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002773 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002774 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2775 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002776 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2777 }
2778 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002779 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2780 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002781
Faisal Latif183ecfa2008-11-21 20:50:46 -06002782 passive_state = atomic_add_return(1, &cm_node->passive_state);
2783 if (passive_state == NES_SEND_RESET_EVENT)
2784 create_event(cm_node, NES_CM_EVENT_RESET);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002785 return 0;
2786}
2787
2788
2789/**
2790 * nes_reject
2791 */
2792int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2793{
2794 struct nes_cm_node *cm_node;
2795 struct nes_cm_core *cm_core;
2796
2797 atomic_inc(&cm_rejects);
2798 cm_node = (struct nes_cm_node *) cm_id->provider_data;
2799 cm_core = cm_node->cm_core;
2800 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2801
2802 strcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP);
2803 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2804
2805 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2806 cm_node->mpa_frame.rev = mpa_version;
2807 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2808
2809 cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
2810
2811 return 0;
2812}
2813
2814
2815/**
2816 * nes_connect
2817 * setup and launch cm connect node
2818 */
2819int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2820{
2821 struct ib_qp *ibqp;
2822 struct nes_qp *nesqp;
2823 struct nes_vnic *nesvnic;
2824 struct nes_device *nesdev;
2825 struct nes_cm_node *cm_node;
2826 struct nes_cm_info cm_info;
2827
2828 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2829 if (!ibqp)
2830 return -EINVAL;
2831 nesqp = to_nesqp(ibqp);
2832 if (!nesqp)
2833 return -EINVAL;
2834 nesvnic = to_nesvnic(nesqp->ibqp.device);
2835 if (!nesvnic)
2836 return -EINVAL;
2837 nesdev = nesvnic->nesdev;
2838 if (!nesdev)
2839 return -EINVAL;
2840
Faisal Latif6492cdf2008-07-24 20:50:45 -07002841 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
2842 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
2843 ntohl(nesvnic->local_ipaddr),
2844 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2845 ntohs(cm_id->remote_addr.sin_port),
2846 ntohl(cm_id->local_addr.sin_addr.s_addr),
2847 ntohs(cm_id->local_addr.sin_port));
2848
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002849 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002850 nesqp->active_conn = 1;
2851
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002852 /* cache the cm_id in the qp */
2853 nesqp->cm_id = cm_id;
2854
2855 cm_id->provider_data = nesqp;
2856
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002857 nesqp->private_data_len = conn_param->private_data_len;
2858 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
2859 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002860 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
2861 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002862
Faisal Latif6492cdf2008-07-24 20:50:45 -07002863 if (cm_id->local_addr.sin_addr.s_addr !=
2864 cm_id->remote_addr.sin_addr.s_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002865 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002866 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002867
2868 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002869 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
2870 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
2871 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
2872 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002873 cm_info.cm_id = cm_id;
2874 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
2875
2876 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002877
2878 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002879 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
2880 conn_param->private_data_len, (void *)conn_param->private_data,
2881 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002882 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002883 if (cm_id->local_addr.sin_addr.s_addr !=
2884 cm_id->remote_addr.sin_addr.s_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002885 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002886 PCI_FUNC(nesdev->pcidev->devfn),
2887 NES_MANAGE_APBVT_DEL);
2888
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002889 cm_id->rem_ref(cm_id);
2890 return -ENOMEM;
2891 }
2892
2893 cm_node->apbvt_set = 1;
2894 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002895 cm_node->nesqp = nesqp;
Faisal Latifd7ffd502008-09-16 11:56:26 -07002896 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002897
2898 return 0;
2899}
2900
2901
2902/**
2903 * nes_create_listen
2904 */
2905int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
2906{
2907 struct nes_vnic *nesvnic;
2908 struct nes_cm_listener *cm_node;
2909 struct nes_cm_info cm_info;
2910 struct nes_adapter *adapter;
2911 int err;
2912
2913
2914 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
2915 cm_id, ntohs(cm_id->local_addr.sin_port));
2916
2917 nesvnic = to_nesvnic(cm_id->device);
2918 if (!nesvnic)
2919 return -EINVAL;
2920 adapter = nesvnic->nesdev->nesadapter;
2921 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
2922 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
2923
2924 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
2925 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
2926
2927 /* setup listen params in our api call struct */
2928 cm_info.loc_addr = nesvnic->local_ipaddr;
2929 cm_info.loc_port = cm_id->local_addr.sin_port;
2930 cm_info.backlog = backlog;
2931 cm_info.cm_id = cm_id;
2932
2933 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
2934
2935
2936 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
2937 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002938 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07002939 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002940 return -ENOMEM;
2941 }
2942
2943 cm_id->provider_data = cm_node;
2944
2945 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002946 err = nes_manage_apbvt(nesvnic,
2947 ntohs(cm_id->local_addr.sin_port),
2948 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
2949 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002950 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002951 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
2952 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002953 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
2954 return err;
2955 }
2956 cm_listens_created++;
2957 }
2958
2959 cm_id->add_ref(cm_id);
2960 cm_id->provider_data = (void *)cm_node;
2961
2962
2963 return 0;
2964}
2965
2966
2967/**
2968 * nes_destroy_listen
2969 */
2970int nes_destroy_listen(struct iw_cm_id *cm_id)
2971{
2972 if (cm_id->provider_data)
2973 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
2974 else
2975 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
2976
2977 cm_id->rem_ref(cm_id);
2978
2979 return 0;
2980}
2981
2982
2983/**
2984 * nes_cm_recv
2985 */
2986int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
2987{
2988 cm_packets_received++;
2989 if ((g_cm_core) && (g_cm_core->api)) {
2990 g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
2991 } else {
2992 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
2993 " cm is not setup properly.\n");
2994 }
2995
2996 return 0;
2997}
2998
2999
3000/**
3001 * nes_cm_start
3002 * Start and init a cm core module
3003 */
3004int nes_cm_start(void)
3005{
3006 nes_debug(NES_DBG_CM, "\n");
3007 /* create the primary CM core, pass this handle to subsequent core inits */
3008 g_cm_core = nes_cm_alloc_core();
3009 if (g_cm_core) {
3010 return 0;
3011 } else {
3012 return -ENOMEM;
3013 }
3014}
3015
3016
3017/**
3018 * nes_cm_stop
3019 * stop and dealloc all cm core instances
3020 */
3021int nes_cm_stop(void)
3022{
3023 g_cm_core->api->destroy_cm_core(g_cm_core);
3024 return 0;
3025}
3026
3027
3028/**
3029 * cm_event_connected
3030 * handle a connected event, setup QPs and HW
3031 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003032static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003033{
3034 u64 u64temp;
3035 struct nes_qp *nesqp;
3036 struct nes_vnic *nesvnic;
3037 struct nes_device *nesdev;
3038 struct nes_cm_node *cm_node;
3039 struct nes_adapter *nesadapter;
3040 struct ib_qp_attr attr;
3041 struct iw_cm_id *cm_id;
3042 struct iw_cm_event cm_event;
3043 struct nes_hw_qp_wqe *wqe;
3044 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06003045 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003046 int ret;
3047
3048 /* get all our handles */
3049 cm_node = event->cm_node;
3050 cm_id = cm_node->cm_id;
3051 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3052 nesqp = (struct nes_qp *)cm_id->provider_data;
3053 nesvnic = to_nesvnic(nesqp->ibqp.device);
3054 nesdev = nesvnic->nesdev;
3055 nesadapter = nesdev->nesadapter;
3056
3057 if (nesqp->destroyed) {
3058 return;
3059 }
3060 atomic_inc(&cm_connecteds);
3061 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3062 " local port 0x%04X. jiffies = %lu.\n",
3063 nesqp->hwqp.qp_id,
3064 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3065 ntohs(cm_id->remote_addr.sin_port),
3066 ntohs(cm_id->local_addr.sin_port),
3067 jiffies);
3068
3069 nes_cm_init_tsa_conn(nesqp, cm_node);
3070
3071 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003072 nesqp->nesqp_context->tcpPorts[0] =
3073 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3074 nesqp->nesqp_context->tcpPorts[1] =
3075 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3076 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3077 nesqp->nesqp_context->ip0 =
3078 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3079 else
3080 nesqp->nesqp_context->ip0 =
3081 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003082
3083 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003084 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3085 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003086 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003087 nes_arp_table(nesdev,
3088 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003089 NULL, NES_ARP_RESOLVE) << 16);
3090 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3091 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3092 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3093 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003094 cpu_to_le32((u32)1 <<
3095 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003096
3097 /* Adjust tail for not having a LSMM */
3098 nesqp->hwqp.sq_tail = 1;
3099
3100#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003101 if (cm_node->send_write0) {
3102 nes_debug(NES_DBG_CM, "Sending first write.\n");
3103 wqe = &nesqp->hwqp.sq_vbase[0];
3104 u64temp = (unsigned long)nesqp;
3105 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3106 set_wqe_64bit_value(wqe->wqe_words,
3107 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3108 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3109 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3110 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3111 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3112 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3113 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3114 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003115
Faisal Latif6492cdf2008-07-24 20:50:45 -07003116 /* use the reserved spot on the WQ for the extra first WQE */
3117 nesqp->nesqp_context->ird_ord_sizes &=
3118 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3119 NES_QPCONTEXT_ORDIRD_WRPDU |
3120 NES_QPCONTEXT_ORDIRD_ALSMM));
3121 nesqp->skip_lsmm = 1;
3122 nesqp->hwqp.sq_tail = 0;
3123 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3124 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3125 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003126#endif
3127
3128 memset(&nes_quad, 0, sizeof(nes_quad));
3129
Faisal Latif6492cdf2008-07-24 20:50:45 -07003130 nes_quad.DstIpAdrIndex =
3131 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3132 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3133 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3134 else
3135 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003136 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3137 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3138
3139 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003140 crc_value = get_crc_value(&nes_quad);
3141 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003142 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3143 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3144
3145 nesqp->hte_index &= nesadapter->hte_index_mask;
3146 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3147
3148 nesqp->ietf_frame = &cm_node->mpa_frame;
3149 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3150 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3151
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003152 /* notify OF layer we successfully created the requested connection */
3153 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3154 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3155 cm_event.provider_data = cm_id->provider_data;
3156 cm_event.local_addr.sin_family = AF_INET;
3157 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3158 cm_event.remote_addr = cm_id->remote_addr;
3159
Faisal Latif6492cdf2008-07-24 20:50:45 -07003160 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3161 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003162
3163 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3164 ret = cm_id->event_handler(cm_id, &cm_event);
3165 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3166
3167 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003168 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3169 "ret=%d\n", __func__, __LINE__, ret);
3170 attr.qp_state = IB_QPS_RTS;
3171 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003172
Faisal Latif6492cdf2008-07-24 20:50:45 -07003173 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3174 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003175
3176 return;
3177}
3178
3179
3180/**
3181 * cm_event_connect_error
3182 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003183static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003184{
3185 struct nes_qp *nesqp;
3186 struct iw_cm_id *cm_id;
3187 struct iw_cm_event cm_event;
3188 /* struct nes_cm_info cm_info; */
3189 int ret;
3190
3191 if (!event->cm_node)
3192 return;
3193
3194 cm_id = event->cm_node->cm_id;
3195 if (!cm_id) {
3196 return;
3197 }
3198
3199 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3200 nesqp = cm_id->provider_data;
3201
3202 if (!nesqp) {
3203 return;
3204 }
3205
3206 /* notify OF layer about this connection error event */
3207 /* cm_id->rem_ref(cm_id); */
3208 nesqp->cm_id = NULL;
3209 cm_id->provider_data = NULL;
3210 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3211 cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
3212 cm_event.provider_data = cm_id->provider_data;
3213 cm_event.local_addr = cm_id->local_addr;
3214 cm_event.remote_addr = cm_id->remote_addr;
3215 cm_event.private_data = NULL;
3216 cm_event.private_data_len = 0;
3217
Faisal Latif6492cdf2008-07-24 20:50:45 -07003218 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3219 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3220 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003221
3222 ret = cm_id->event_handler(cm_id, &cm_event);
3223 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3224 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003225 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3226 "ret=%d\n", __func__, __LINE__, ret);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003227 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003228
Faisal Latif6492cdf2008-07-24 20:50:45 -07003229 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003230 return;
3231}
3232
3233
3234/**
3235 * cm_event_reset
3236 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003237static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003238{
3239 struct nes_qp *nesqp;
3240 struct iw_cm_id *cm_id;
3241 struct iw_cm_event cm_event;
3242 /* struct nes_cm_info cm_info; */
3243 int ret;
3244
3245 if (!event->cm_node)
3246 return;
3247
3248 if (!event->cm_node->cm_id)
3249 return;
3250
3251 cm_id = event->cm_node->cm_id;
3252
3253 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3254 nesqp = cm_id->provider_data;
3255
3256 nesqp->cm_id = NULL;
3257 /* cm_id->provider_data = NULL; */
3258 cm_event.event = IW_CM_EVENT_DISCONNECT;
3259 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3260 cm_event.provider_data = cm_id->provider_data;
3261 cm_event.local_addr = cm_id->local_addr;
3262 cm_event.remote_addr = cm_id->remote_addr;
3263 cm_event.private_data = NULL;
3264 cm_event.private_data_len = 0;
3265
3266 ret = cm_id->event_handler(cm_id, &cm_event);
Faisal Latif183ecfa2008-11-21 20:50:46 -06003267 cm_id->add_ref(cm_id);
3268 atomic_inc(&cm_closes);
3269 cm_event.event = IW_CM_EVENT_CLOSE;
3270 cm_event.status = IW_CM_EVENT_STATUS_OK;
3271 cm_event.provider_data = cm_id->provider_data;
3272 cm_event.local_addr = cm_id->local_addr;
3273 cm_event.remote_addr = cm_id->remote_addr;
3274 cm_event.private_data = NULL;
3275 cm_event.private_data_len = 0;
3276 nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3277 ret = cm_id->event_handler(cm_id, &cm_event);
3278
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003279 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3280
3281
3282 /* notify OF layer about this connection error event */
3283 cm_id->rem_ref(cm_id);
3284
3285 return;
3286}
3287
3288
3289/**
3290 * cm_event_mpa_req
3291 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003292static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003293{
3294 struct iw_cm_id *cm_id;
3295 struct iw_cm_event cm_event;
3296 int ret;
3297 struct nes_cm_node *cm_node;
3298
3299 cm_node = event->cm_node;
3300 if (!cm_node)
3301 return;
3302 cm_id = cm_node->cm_id;
3303
3304 atomic_inc(&cm_connect_reqs);
3305 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3306 cm_node, cm_id, jiffies);
3307
3308 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3309 cm_event.status = IW_CM_EVENT_STATUS_OK;
3310 cm_event.provider_data = (void *)cm_node;
3311
3312 cm_event.local_addr.sin_family = AF_INET;
3313 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3314 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3315
3316 cm_event.remote_addr.sin_family = AF_INET;
3317 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3318 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3319
3320 cm_event.private_data = cm_node->mpa_frame_buf;
3321 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3322
3323 ret = cm_id->event_handler(cm_id, &cm_event);
3324 if (ret)
3325 printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003326 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003327
3328 return;
3329}
3330
3331
3332static void nes_cm_event_handler(struct work_struct *);
3333
3334/**
3335 * nes_cm_post_event
3336 * post an event to the cm event handler
3337 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003338static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003339{
3340 atomic_inc(&event->cm_node->cm_core->events_posted);
3341 add_ref_cm_node(event->cm_node);
3342 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3343 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003344 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3345 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003346
3347 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3348
3349 nes_debug(NES_DBG_CM, "Exit\n");
3350 return 0;
3351}
3352
3353
3354/**
3355 * nes_cm_event_handler
3356 * worker function to handle cm events
3357 * will free instance of nes_cm_event
3358 */
3359static void nes_cm_event_handler(struct work_struct *work)
3360{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003361 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3362 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003363 struct nes_cm_core *cm_core;
3364
Faisal Latif6492cdf2008-07-24 20:50:45 -07003365 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003366 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003367
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003368 cm_core = event->cm_node->cm_core;
3369 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003370 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003371
3372 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003373 case NES_CM_EVENT_MPA_REQ:
3374 cm_event_mpa_req(event);
3375 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3376 event->cm_node);
3377 break;
3378 case NES_CM_EVENT_RESET:
3379 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3380 event->cm_node);
3381 cm_event_reset(event);
3382 break;
3383 case NES_CM_EVENT_CONNECTED:
3384 if ((!event->cm_node->cm_id) ||
3385 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003386 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003387 cm_event_connected(event);
3388 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3389 break;
3390 case NES_CM_EVENT_ABORTED:
3391 if ((!event->cm_node->cm_id) ||
3392 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003393 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003394 cm_event_connect_error(event);
3395 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3396 break;
3397 case NES_CM_EVENT_DROPPED_PKT:
3398 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3399 break;
3400 default:
3401 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3402 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003403 }
3404
3405 atomic_dec(&cm_core->events_posted);
3406 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3407 rem_ref_cm_node(cm_core, event->cm_node);
3408 kfree(event);
3409
3410 return;
3411}