blob: 9f0b964b2c9932af7567dcae6efc7d98c6c01728 [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>
55
56#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
462 spin_lock_irqsave(&cm_core->ht_lock, flags);
463
Faisal Latif6492cdf2008-07-24 20:50:45 -0700464 list_for_each_safe(list_node, list_core_temp,
465 &cm_core->connected_nodes) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800466 cm_node = container_of(list_node, struct nes_cm_node, list);
467 add_ref_cm_node(cm_node);
468 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
469 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700470 list_for_each_safe(list_core, list_node_temp,
471 &cm_node->recv_list) {
472 recv_entry = container_of(list_core,
473 struct nes_timer_entry, list);
474 if (!recv_entry)
475 break;
476 if (time_after(recv_entry->timetosend, jiffies)) {
477 if (nexttimeout > recv_entry->timetosend ||
478 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800479 nexttimeout = recv_entry->timetosend;
480 settimer = 1;
481 }
482 continue;
483 }
484 list_del(&recv_entry->list);
485 cm_id = cm_node->cm_id;
486 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700487 nesqp = (struct nes_qp *)recv_entry->skb;
488 spin_lock_irqsave(&nesqp->lock, qplockflags);
489 if (nesqp->cm_id) {
490 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
491 "refcount = %d: HIT A "
492 "NES_TIMER_TYPE_CLOSE with something "
493 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
494 atomic_read(&nesqp->refcount));
495 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
496 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
497 nesqp->ibqp_state = IB_QPS_ERR;
498 spin_unlock_irqrestore(&nesqp->lock,
499 qplockflags);
500 nes_cm_disconn(nesqp);
501 } else {
502 spin_unlock_irqrestore(&nesqp->lock,
503 qplockflags);
504 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
505 "refcount = %d: HIT A "
506 "NES_TIMER_TYPE_CLOSE with nothing "
507 "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
508 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800509 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700510 if (cm_id)
511 cm_id->rem_ref(cm_id);
512
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800513 kfree(recv_entry);
514 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
515 }
516 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
517
518 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700519 do {
520 send_entry = cm_node->send_entry;
521 if (!send_entry)
522 continue;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800523 if (time_after(send_entry->timetosend, jiffies)) {
524 if (cm_node->state != NES_CM_STATE_TSA) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700525 if ((nexttimeout >
526 send_entry->timetosend) ||
527 !settimer) {
528 nexttimeout =
529 send_entry->timetosend;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800530 settimer = 1;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700531 continue;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800532 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800533 } else {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700534 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800535 continue;
536 }
537 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700538
539 if ((cm_node->state == NES_CM_STATE_TSA) ||
540 (cm_node->state == NES_CM_STATE_CLOSED)) {
541 free_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800542 continue;
543 }
544
Faisal Latif6492cdf2008-07-24 20:50:45 -0700545 if (!send_entry->retranscount ||
546 !send_entry->retrycount) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800547 cm_packets_dropped++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700548 last_state = cm_node->state;
549 cm_node->state = NES_CM_STATE_CLOSED;
550 free_retrans_entry(cm_node);
551 spin_unlock_irqrestore(
552 &cm_node->retrans_list_lock, flags);
553 if (last_state == NES_CM_STATE_SYN_RCVD)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800554 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700555 else
556 create_event(cm_node,
557 NES_CM_EVENT_ABORTED);
558 spin_lock_irqsave(&cm_node->retrans_list_lock,
559 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800560 continue;
561 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800562 atomic_inc(&send_entry->skb->users);
563 cm_packets_retrans++;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700564 nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
565 "for node %p, jiffies = %lu, time to send = "
566 "%lu, retranscount = %u, send_entry->seq_num = "
567 "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
568 "0x%08X\n", send_entry, cm_node, jiffies,
569 send_entry->timetosend,
570 send_entry->retranscount,
571 send_entry->seq_num,
572 cm_node->tcp_cntxt.rem_ack_num);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800573
Faisal Latif6492cdf2008-07-24 20:50:45 -0700574 spin_unlock_irqrestore(&cm_node->retrans_list_lock,
575 flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800576 ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700577 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800578 if (ret != NETDEV_TX_OK) {
Faisal Latif6492cdf2008-07-24 20:50:45 -0700579 nes_debug(NES_DBG_CM, "rexmit failed for "
580 "node=%p\n", cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800581 cm_packets_bounced++;
582 atomic_dec(&send_entry->skb->users);
583 send_entry->retrycount--;
584 nexttimeout = jiffies + NES_SHORT_TIME;
585 settimer = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800586 continue;
587 } else {
588 cm_packets_sent++;
589 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700590 nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
591 "%u, retry count = %u.\n",
592 send_entry->retranscount,
593 send_entry->retrycount);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800594 if (send_entry->send_retrans) {
595 send_entry->retranscount--;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700596 send_entry->timetosend = jiffies +
597 NES_RETRY_TIMEOUT;
598 if (nexttimeout > send_entry->timetosend ||
599 !settimer) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800600 nexttimeout = send_entry->timetosend;
601 settimer = 1;
602 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800603 } else {
604 int close_when_complete;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700605 close_when_complete =
606 send_entry->close_when_complete;
607 nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
608 cm_node, cm_node->state);
609 free_retrans_entry(cm_node);
610 if (close_when_complete)
611 rem_ref_cm_node(cm_node->cm_core,
612 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800613 }
Faisal Latif6492cdf2008-07-24 20:50:45 -0700614 } while (0);
615
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800616 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700617 rem_ref_cm_node(cm_node->cm_core, cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800618 spin_lock_irqsave(&cm_core->ht_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -0700619 if (ret != NETDEV_TX_OK) {
620 nes_debug(NES_DBG_CM, "rexmit failed for cm_node=%p\n",
621 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800622 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700623 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800624 }
625 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
626
627 if (settimer) {
628 if (!timer_pending(&cm_core->tcp_timer)) {
629 cm_core->tcp_timer.expires = nexttimeout;
630 add_timer(&cm_core->tcp_timer);
631 }
632 }
633}
634
635
636/**
637 * send_syn
638 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700639static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
640 struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800641{
642 int ret;
643 int flags = SET_SYN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800644 char optionsbuffer[sizeof(struct option_mss) +
Faisal Latif6492cdf2008-07-24 20:50:45 -0700645 sizeof(struct option_windowscale) + sizeof(struct option_base) +
646 TCP_OPTIONS_PADDING];
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800647
648 int optionssize = 0;
649 /* Sending MSS option */
650 union all_known_options *options;
651
652 if (!cm_node)
653 return -EINVAL;
654
655 options = (union all_known_options *)&optionsbuffer[optionssize];
656 options->as_mss.optionnum = OPTION_NUMBER_MSS;
657 options->as_mss.length = sizeof(struct option_mss);
658 options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
659 optionssize += sizeof(struct option_mss);
660
661 options = (union all_known_options *)&optionsbuffer[optionssize];
662 options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
663 options->as_windowscale.length = sizeof(struct option_windowscale);
664 options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
665 optionssize += sizeof(struct option_windowscale);
666
Faisal Latif6492cdf2008-07-24 20:50:45 -0700667 if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800668 options = (union all_known_options *)&optionsbuffer[optionssize];
669 options->as_base.optionnum = OPTION_NUMBER_WRITE0;
670 options->as_base.length = sizeof(struct option_base);
671 optionssize += sizeof(struct option_base);
672 /* we need the size to be a multiple of 4 */
673 options = (union all_known_options *)&optionsbuffer[optionssize];
674 options->as_end = 1;
675 optionssize += 1;
676 options = (union all_known_options *)&optionsbuffer[optionssize];
677 options->as_end = 1;
678 optionssize += 1;
679 }
680
681 options = (union all_known_options *)&optionsbuffer[optionssize];
682 options->as_end = OPTION_NUMBER_END;
683 optionssize += 1;
684
Faisal Latif6492cdf2008-07-24 20:50:45 -0700685 if (!skb)
686 skb = get_free_pkt(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800687 if (!skb) {
688 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
689 return -1;
690 }
691
692 if (sendack)
693 flags |= SET_ACK;
694
695 form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
696 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
697
698 return ret;
699}
700
701
702/**
703 * send_reset
704 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700705static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800706{
707 int ret;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800708 int flags = SET_RST | SET_ACK;
709
Faisal Latif6492cdf2008-07-24 20:50:45 -0700710 if (!skb)
711 skb = get_free_pkt(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800712 if (!skb) {
713 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
714 return -1;
715 }
716
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800717 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
718 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
719
720 return ret;
721}
722
723
724/**
725 * send_ack
726 */
Faisal Latif6492cdf2008-07-24 20:50:45 -0700727static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800728{
729 int ret;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700730
731 if (!skb)
732 skb = get_free_pkt(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800733
734 if (!skb) {
735 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
736 return -1;
737 }
738
739 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
740 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
741
742 return ret;
743}
744
745
746/**
747 * send_fin
748 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700749static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800750{
751 int ret;
752
753 /* if we didn't get a frame get one */
754 if (!skb)
755 skb = get_free_pkt(cm_node);
756
757 if (!skb) {
758 nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
759 return -1;
760 }
761
762 form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
763 ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
764
765 return ret;
766}
767
768
769/**
770 * get_free_pkt
771 */
Roland Dreier1a855fb2008-04-16 21:01:09 -0700772static struct sk_buff *get_free_pkt(struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800773{
774 struct sk_buff *skb, *new_skb;
775
776 /* check to see if we need to repopulate the free tx pkt queue */
777 if (skb_queue_len(&cm_node->cm_core->tx_free_list) < NES_CM_FREE_PKT_LO_WATERMARK) {
778 while (skb_queue_len(&cm_node->cm_core->tx_free_list) <
779 cm_node->cm_core->free_tx_pkt_max) {
780 /* replace the frame we took, we won't get it back */
781 new_skb = dev_alloc_skb(cm_node->cm_core->mtu);
782 BUG_ON(!new_skb);
783 /* add a replacement frame to the free tx list head */
784 skb_queue_head(&cm_node->cm_core->tx_free_list, new_skb);
785 }
786 }
787
788 skb = skb_dequeue(&cm_node->cm_core->tx_free_list);
789
790 return skb;
791}
792
793
794/**
795 * make_hashkey - generate hash key from node tuple
796 */
797static inline int make_hashkey(u16 loc_port, nes_addr_t loc_addr, u16 rem_port,
798 nes_addr_t rem_addr)
799{
800 u32 hashkey = 0;
801
802 hashkey = loc_addr + rem_addr + loc_port + rem_port;
803 hashkey = (hashkey % NES_CM_HASHTABLE_SIZE);
804
805 return hashkey;
806}
807
808
809/**
810 * find_node - find a cm node that matches the reference cm node
811 */
812static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
813 u16 rem_port, nes_addr_t rem_addr, u16 loc_port, nes_addr_t loc_addr)
814{
815 unsigned long flags;
816 u32 hashkey;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800817 struct list_head *hte;
818 struct nes_cm_node *cm_node;
819
820 /* make a hash index key for this packet */
821 hashkey = make_hashkey(loc_port, loc_addr, rem_port, rem_addr);
822
823 /* get a handle on the hte */
824 hte = &cm_core->connected_nodes;
825
Roland Dreiere4477032008-04-23 11:55:43 -0700826 nes_debug(NES_DBG_CM, "Searching for an owner node: " NIPQUAD_FMT ":%x from core %p->%p\n",
827 HIPQUAD(loc_addr), loc_port, cm_core, hte);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800828
829 /* walk list and find cm_node associated with this session ID */
830 spin_lock_irqsave(&cm_core->ht_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700831 list_for_each_entry(cm_node, hte, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800832 /* compare quad, return node handle if a match */
833 nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
834 cm_node->loc_addr, cm_node->loc_port,
835 loc_addr, loc_port,
836 cm_node->rem_addr, cm_node->rem_port,
837 rem_addr, rem_port);
838 if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
839 (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
840 add_ref_cm_node(cm_node);
841 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
842 return cm_node;
843 }
844 }
845 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
846
847 /* no owner node */
848 return NULL;
849}
850
851
852/**
853 * find_listener - find a cm node listening on this addr-port pair
854 */
855static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
856 nes_addr_t dst_addr, u16 dst_port, enum nes_cm_listener_state listener_state)
857{
858 unsigned long flags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800859 struct nes_cm_listener *listen_node;
860
861 /* walk list and find cm_node associated with this session ID */
862 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
Robert P. J. Day4e96a772008-04-16 21:09:26 -0700863 list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800864 /* compare node pair, return node handle if a match */
865 if (((listen_node->loc_addr == dst_addr) ||
866 listen_node->loc_addr == 0x00000000) &&
867 (listen_node->loc_port == dst_port) &&
868 (listener_state & listen_node->listener_state)) {
869 atomic_inc(&listen_node->ref_count);
870 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
871 return listen_node;
872 }
873 }
874 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
875
Roland Dreiere4477032008-04-23 11:55:43 -0700876 nes_debug(NES_DBG_CM, "Unable to find listener for " NIPQUAD_FMT ":%x\n",
877 HIPQUAD(dst_addr), dst_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800878
879 /* no listener */
880 return NULL;
881}
882
883
884/**
885 * add_hte_node - add a cm node to the hash table
886 */
887static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
888{
889 unsigned long flags;
890 u32 hashkey;
891 struct list_head *hte;
892
893 if (!cm_node || !cm_core)
894 return -EINVAL;
895
Faisal Latif6492cdf2008-07-24 20:50:45 -0700896 nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
897 cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800898
899 /* first, make an index into our hash table */
900 hashkey = make_hashkey(cm_node->loc_port, cm_node->loc_addr,
901 cm_node->rem_port, cm_node->rem_addr);
902 cm_node->hashkey = hashkey;
903
904 spin_lock_irqsave(&cm_core->ht_lock, flags);
905
906 /* get a handle on the hash table element (list head for this slot) */
907 hte = &cm_core->connected_nodes;
908 list_add_tail(&cm_node->list, hte);
909 atomic_inc(&cm_core->ht_node_cnt);
910
911 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
912
913 return 0;
914}
915
916
917/**
918 * mini_cm_dec_refcnt_listen
919 */
920static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -0700921 struct nes_cm_listener *listener, int free_hanging_nodes)
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800922{
923 int ret = 1;
924 unsigned long flags;
Faisal Latif6492cdf2008-07-24 20:50:45 -0700925 struct list_head *list_pos = NULL;
926 struct list_head *list_temp = NULL;
927 struct nes_cm_node *cm_node = NULL;
928
929 nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
930 "refcnt=%d\n", listener, free_hanging_nodes,
931 atomic_read(&listener->ref_count));
932 /* free non-accelerated child nodes for this listener */
933 if (free_hanging_nodes) {
934 spin_lock_irqsave(&cm_core->ht_lock, flags);
935 list_for_each_safe(list_pos, list_temp,
936 &g_cm_core->connected_nodes) {
937 cm_node = container_of(list_pos, struct nes_cm_node,
938 list);
939 if ((cm_node->listener == listener) &&
940 (!cm_node->accelerated)) {
941 cleanup_retrans_entry(cm_node);
942 spin_unlock_irqrestore(&cm_core->ht_lock,
943 flags);
944 send_reset(cm_node, NULL);
945 spin_lock_irqsave(&cm_core->ht_lock, flags);
946 }
947 }
948 spin_unlock_irqrestore(&cm_core->ht_lock, flags);
949 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800950 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
951 if (!atomic_dec_return(&listener->ref_count)) {
952 list_del(&listener->list);
953
954 /* decrement our listen node count */
955 atomic_dec(&cm_core->listen_node_cnt);
956
957 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
958
959 if (listener->nesvnic) {
960 nes_manage_apbvt(listener->nesvnic, listener->loc_port,
961 PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
962 }
963
964 nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
965
966 kfree(listener);
Faisal Latifa2e9c382008-02-21 08:27:32 -0600967 listener = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -0800968 ret = 0;
969 cm_listens_destroyed++;
970 } else {
971 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
972 }
973 if (listener) {
974 if (atomic_read(&listener->pend_accepts_cnt) > 0)
975 nes_debug(NES_DBG_CM, "destroying listener (%p)"
976 " with non-zero pending accepts=%u\n",
977 listener, atomic_read(&listener->pend_accepts_cnt));
978 }
979
980 return ret;
981}
982
983
984/**
985 * mini_cm_del_listen
986 */
987static int mini_cm_del_listen(struct nes_cm_core *cm_core,
988 struct nes_cm_listener *listener)
989{
990 listener->listener_state = NES_CM_LISTENER_PASSIVE_STATE;
991 listener->cm_id = NULL; /* going to be destroyed pretty soon */
992 return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
993}
994
995
996/**
997 * mini_cm_accelerated
998 */
999static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1000 struct nes_cm_node *cm_node)
1001{
1002 u32 was_timer_set;
1003 cm_node->accelerated = 1;
1004
1005 if (cm_node->accept_pend) {
1006 BUG_ON(!cm_node->listener);
1007 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1008 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1009 }
1010
1011 was_timer_set = timer_pending(&cm_core->tcp_timer);
1012 if (!was_timer_set) {
1013 cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1014 add_timer(&cm_core->tcp_timer);
1015 }
1016
1017 return 0;
1018}
1019
1020
1021/**
1022 * nes_addr_send_arp
1023 */
1024static void nes_addr_send_arp(u32 dst_ip)
1025{
1026 struct rtable *rt;
1027 struct flowi fl;
1028
1029 memset(&fl, 0, sizeof fl);
1030 fl.nl_u.ip4_u.daddr = htonl(dst_ip);
1031 if (ip_route_output_key(&init_net, &rt, &fl)) {
1032 printk("%s: ip_route_output_key failed for 0x%08X\n",
Harvey Harrison33718362008-04-16 21:01:10 -07001033 __func__, dst_ip);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001034 return;
1035 }
1036
1037 neigh_event_send(rt->u.dst.neighbour, NULL);
1038 ip_rt_put(rt);
1039}
1040
1041
1042/**
1043 * make_cm_node - create a new instance of a cm node
1044 */
1045static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1046 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1047 struct nes_cm_listener *listener)
1048{
1049 struct nes_cm_node *cm_node;
1050 struct timespec ts;
1051 int arpindex = 0;
1052 struct nes_device *nesdev;
1053 struct nes_adapter *nesadapter;
Roland Dreier2bd01c52008-04-23 11:52:18 -07001054 DECLARE_MAC_BUF(mac);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001055
1056 /* create an hte and cm_node for this instance */
1057 cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1058 if (!cm_node)
1059 return NULL;
1060
1061 /* set our node specific transport info */
1062 cm_node->loc_addr = cm_info->loc_addr;
1063 cm_node->rem_addr = cm_info->rem_addr;
1064 cm_node->loc_port = cm_info->loc_port;
1065 cm_node->rem_port = cm_info->rem_port;
1066 cm_node->send_write0 = send_first;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001067 nes_debug(NES_DBG_CM, "Make node addresses : loc = " NIPQUAD_FMT
1068 ":%x, rem = " NIPQUAD_FMT ":%x\n",
1069 HIPQUAD(cm_node->loc_addr), cm_node->loc_port,
1070 HIPQUAD(cm_node->rem_addr), cm_node->rem_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001071 cm_node->listener = listener;
1072 cm_node->netdev = nesvnic->netdev;
1073 cm_node->cm_id = cm_info->cm_id;
1074 memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1075
Faisal Latif6492cdf2008-07-24 20:50:45 -07001076 nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1077 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001078
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001079 spin_lock_init(&cm_node->retrans_list_lock);
1080 INIT_LIST_HEAD(&cm_node->recv_list);
1081 spin_lock_init(&cm_node->recv_list_lock);
1082
1083 cm_node->loopbackpartner = NULL;
1084 atomic_set(&cm_node->ref_count, 1);
1085 /* associate our parent CM core */
1086 cm_node->cm_core = cm_core;
1087 cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1088 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1089 cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1090 NES_CM_DEFAULT_RCV_WND_SCALE;
1091 ts = current_kernel_time();
1092 cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1093 cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
Chien Tungf2b2b592008-03-20 19:55:30 -05001094 sizeof(struct tcphdr) - ETH_HLEN - VLAN_HLEN;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001095 cm_node->tcp_cntxt.rcv_nxt = 0;
1096 /* get a unique session ID , add thread_id to an upcounter to handle race */
1097 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001098 cm_node->conn_type = cm_info->conn_type;
1099 cm_node->apbvt_set = 0;
1100 cm_node->accept_pend = 0;
1101
1102 cm_node->nesvnic = nesvnic;
1103 /* get some device handles, for arp lookup */
1104 nesdev = nesvnic->nesdev;
1105 nesadapter = nesdev->nesadapter;
1106
1107 cm_node->loopbackpartner = NULL;
1108 /* get the mac addr for the remote node */
1109 arpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1110 if (arpindex < 0) {
1111 kfree(cm_node);
1112 nes_addr_send_arp(cm_info->rem_addr);
1113 return NULL;
1114 }
1115
1116 /* copy the mac addr to node context */
1117 memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
Roland Dreier2bd01c52008-04-23 11:52:18 -07001118 nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %s\n",
1119 print_mac(mac, cm_node->rem_mac));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001120
1121 add_hte_node(cm_core, cm_node);
1122 atomic_inc(&cm_nodes_created);
1123
1124 return cm_node;
1125}
1126
1127
1128/**
1129 * add_ref_cm_node - destroy an instance of a cm node
1130 */
1131static int add_ref_cm_node(struct nes_cm_node *cm_node)
1132{
1133 atomic_inc(&cm_node->ref_count);
1134 return 0;
1135}
1136
1137
1138/**
1139 * rem_ref_cm_node - destroy an instance of a cm node
1140 */
1141static int rem_ref_cm_node(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001142 struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001143{
1144 unsigned long flags, qplockflags;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001145 struct nes_timer_entry *recv_entry;
1146 struct iw_cm_id *cm_id;
1147 struct list_head *list_core, *list_node_temp;
1148 struct nes_qp *nesqp;
1149
1150 if (!cm_node)
1151 return -EINVAL;
1152
1153 spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1154 if (atomic_dec_return(&cm_node->ref_count)) {
1155 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1156 return 0;
1157 }
1158 list_del(&cm_node->list);
1159 atomic_dec(&cm_core->ht_node_cnt);
1160 spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1161
1162 /* if the node is destroyed before connection was accelerated */
1163 if (!cm_node->accelerated && cm_node->accept_pend) {
1164 BUG_ON(!cm_node->listener);
1165 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1166 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1167 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001168 BUG_ON(cm_node->send_entry);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001169 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
1170 list_for_each_safe(list_core, list_node_temp, &cm_node->recv_list) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001171 recv_entry = container_of(list_core, struct nes_timer_entry,
1172 list);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001173 list_del(&recv_entry->list);
1174 cm_id = cm_node->cm_id;
1175 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001176 nesqp = (struct nes_qp *)recv_entry->skb;
1177 spin_lock_irqsave(&nesqp->lock, qplockflags);
1178 if (nesqp->cm_id) {
1179 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p: HIT A "
1180 "NES_TIMER_TYPE_CLOSE with something to do!\n",
1181 nesqp->hwqp.qp_id, cm_id);
1182 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
1183 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
1184 nesqp->ibqp_state = IB_QPS_ERR;
1185 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
1186 nes_cm_disconn(nesqp);
1187 } else {
1188 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
1189 nes_debug(NES_DBG_CM, "QP%u: cm_id = %p: HIT A "
1190 "NES_TIMER_TYPE_CLOSE with nothing to do!\n",
1191 nesqp->hwqp.qp_id, cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001192 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001193 cm_id->rem_ref(cm_id);
1194
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001195 kfree(recv_entry);
1196 spin_lock_irqsave(&cm_node->recv_list_lock, flags);
1197 }
1198 spin_unlock_irqrestore(&cm_node->recv_list_lock, flags);
1199
1200 if (cm_node->listener) {
1201 mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1202 } else {
1203 if (cm_node->apbvt_set && cm_node->nesvnic) {
1204 nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001205 PCI_FUNC(
1206 cm_node->nesvnic->nesdev->pcidev->devfn),
1207 NES_MANAGE_APBVT_DEL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001208 }
1209 }
1210
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001211 atomic_dec(&cm_core->node_cnt);
1212 atomic_inc(&cm_nodes_destroyed);
Faisal Latif6492cdf2008-07-24 20:50:45 -07001213 nesqp = cm_node->nesqp;
1214 if (nesqp) {
1215 nesqp->cm_node = NULL;
1216 nes_rem_ref(&nesqp->ibqp);
1217 cm_node->nesqp = NULL;
1218 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001219
Faisal Latif6492cdf2008-07-24 20:50:45 -07001220 cm_node->freed = 1;
1221 kfree(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001222 return 0;
1223}
1224
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001225/**
1226 * process_options
1227 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001228static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1229 u32 optionsize, u32 syn_packet)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001230{
1231 u32 tmp;
1232 u32 offset = 0;
1233 union all_known_options *all_options;
1234 char got_mss_option = 0;
1235
1236 while (offset < optionsize) {
1237 all_options = (union all_known_options *)(optionsloc + offset);
1238 switch (all_options->as_base.optionnum) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001239 case OPTION_NUMBER_END:
1240 offset = optionsize;
1241 break;
1242 case OPTION_NUMBER_NONE:
1243 offset += 1;
1244 continue;
1245 case OPTION_NUMBER_MSS:
1246 nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1247 "Size: %d\n", __func__,
1248 all_options->as_mss.length, offset, optionsize);
1249 got_mss_option = 1;
1250 if (all_options->as_mss.length != 4) {
1251 return 1;
1252 } else {
1253 tmp = ntohs(all_options->as_mss.mss);
1254 if (tmp > 0 && tmp <
1255 cm_node->tcp_cntxt.mss)
1256 cm_node->tcp_cntxt.mss = tmp;
1257 }
1258 break;
1259 case OPTION_NUMBER_WINDOW_SCALE:
1260 cm_node->tcp_cntxt.snd_wscale =
1261 all_options->as_windowscale.shiftcount;
1262 break;
1263 case OPTION_NUMBER_WRITE0:
1264 cm_node->send_write0 = 1;
1265 break;
1266 default:
1267 nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1268 all_options->as_base.optionnum);
1269 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001270 }
1271 offset += all_options->as_base.length;
1272 }
1273 if ((!got_mss_option) && (syn_packet))
1274 cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1275 return 0;
1276}
1277
Faisal Latif6492cdf2008-07-24 20:50:45 -07001278static void drop_packet(struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001279{
Faisal Latif6492cdf2008-07-24 20:50:45 -07001280 atomic_inc(&cm_accel_dropped_pkts);
1281 dev_kfree_skb_any(skb);
1282}
1283
1284static void handle_fin_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1285 struct tcphdr *tcph)
1286{
1287 atomic_inc(&cm_resets_recvd);
1288 nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1289 "refcnt=%d\n", cm_node, cm_node->state,
1290 atomic_read(&cm_node->ref_count));
1291 cm_node->tcp_cntxt.rcv_nxt++;
1292 cleanup_retrans_entry(cm_node);
1293 switch (cm_node->state) {
1294 case NES_CM_STATE_SYN_RCVD:
1295 case NES_CM_STATE_SYN_SENT:
1296 case NES_CM_STATE_ESTABLISHED:
1297 case NES_CM_STATE_MPAREQ_SENT:
1298 cm_node->state = NES_CM_STATE_LAST_ACK;
1299 send_fin(cm_node, skb);
1300 break;
1301 case NES_CM_STATE_FIN_WAIT1:
1302 cm_node->state = NES_CM_STATE_CLOSING;
1303 send_ack(cm_node, skb);
1304 break;
1305 case NES_CM_STATE_FIN_WAIT2:
1306 cm_node->state = NES_CM_STATE_TIME_WAIT;
1307 send_ack(cm_node, skb);
1308 cm_node->state = NES_CM_STATE_CLOSED;
1309 break;
1310 case NES_CM_STATE_TSA:
1311 default:
1312 nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1313 cm_node, cm_node->state);
1314 drop_packet(skb);
1315 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001316 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001317}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001318
Faisal Latif6492cdf2008-07-24 20:50:45 -07001319
1320static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1321 struct tcphdr *tcph)
1322{
1323
1324 int reset = 0; /* whether to send reset in case of err.. */
1325 atomic_inc(&cm_resets_recvd);
1326 nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1327 " refcnt=%d\n", cm_node, cm_node->state,
1328 atomic_read(&cm_node->ref_count));
1329 cleanup_retrans_entry(cm_node);
1330 switch (cm_node->state) {
1331 case NES_CM_STATE_SYN_SENT:
1332 case NES_CM_STATE_MPAREQ_SENT:
1333 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1334 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1335 cm_node->listener, cm_node->state);
1336 active_open_err(cm_node, skb, reset);
1337 break;
1338 /* For PASSIVE open states, remove the cm_node event */
1339 case NES_CM_STATE_ESTABLISHED:
1340 case NES_CM_STATE_SYN_RCVD:
1341 case NES_CM_STATE_LISTENING:
1342 nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1343 passive_open_err(cm_node, skb, reset);
1344 break;
1345 case NES_CM_STATE_TSA:
1346 default:
1347 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001348 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001349}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001350
Faisal Latif6492cdf2008-07-24 20:50:45 -07001351static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb,
1352 enum nes_cm_event_type type)
1353{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001354
Faisal Latif6492cdf2008-07-24 20:50:45 -07001355 int ret;
1356 int datasize = skb->len;
1357 u8 *dataloc = skb->data;
1358 ret = parse_mpa(cm_node, dataloc, datasize);
1359 if (ret < 0) {
1360 nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
1361 if (type == NES_CM_EVENT_CONNECTED) {
1362 nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1363 "cm_node=%p listener=%p state=%d\n", __func__,
1364 __LINE__, cm_node, cm_node->listener,
1365 cm_node->state);
1366 active_open_err(cm_node, skb, 1);
1367 } else {
1368 passive_open_err(cm_node, skb, 1);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001369 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001370 } else {
1371 cleanup_retrans_entry(cm_node);
1372 dev_kfree_skb_any(skb);
1373 if (type == NES_CM_EVENT_CONNECTED)
1374 cm_node->state = NES_CM_STATE_TSA;
1375 create_event(cm_node, type);
1376
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001377 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001378 return ;
1379}
1380
1381static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1382{
1383 switch (cm_node->state) {
1384 case NES_CM_STATE_SYN_SENT:
1385 case NES_CM_STATE_MPAREQ_SENT:
1386 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1387 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1388 cm_node->listener, cm_node->state);
1389 active_open_err(cm_node, skb, 1);
1390 break;
1391 case NES_CM_STATE_ESTABLISHED:
1392 case NES_CM_STATE_SYN_RCVD:
1393 passive_open_err(cm_node, skb, 1);
1394 break;
1395 case NES_CM_STATE_TSA:
1396 default:
1397 drop_packet(skb);
1398 }
1399}
1400
1401static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1402 struct sk_buff *skb)
1403{
1404 int err;
1405
1406 err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num))? 0 : 1;
1407 if (err)
1408 active_open_err(cm_node, skb, 1);
1409
1410 return err;
1411}
1412
1413static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1414 struct sk_buff *skb)
1415{
1416 int err = 0;
1417 u32 seq;
1418 u32 ack_seq;
1419 u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1420 u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1421 u32 rcv_wnd;
1422 seq = ntohl(tcph->seq);
1423 ack_seq = ntohl(tcph->ack_seq);
1424 rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1425 if (ack_seq != loc_seq_num)
1426 err = 1;
1427 else if ((seq + rcv_wnd) < rcv_nxt)
1428 err = 1;
1429 if (err) {
1430 nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1431 "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1432 cm_node->listener, cm_node->state);
1433 indicate_pkt_err(cm_node, skb);
1434 nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1435 "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1436 rcv_wnd);
1437 }
1438 return err;
1439}
1440
1441/*
1442 * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1443 * is created with a listener or it may comein as rexmitted packet which in
1444 * that case will be just dropped.
1445 */
1446
1447static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1448 struct tcphdr *tcph)
1449{
1450 int ret;
1451 u32 inc_sequence;
1452 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001453
1454 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001455 skb_pull(skb, tcph->doff << 2);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001456 inc_sequence = ntohl(tcph->seq);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001457
Faisal Latif6492cdf2008-07-24 20:50:45 -07001458 switch (cm_node->state) {
1459 case NES_CM_STATE_SYN_SENT:
1460 case NES_CM_STATE_MPAREQ_SENT:
1461 /* Rcvd syn on active open connection*/
1462 active_open_err(cm_node, skb, 1);
1463 break;
1464 case NES_CM_STATE_LISTENING:
1465 /* Passive OPEN */
1466 cm_node->accept_pend = 1;
1467 atomic_inc(&cm_node->listener->pend_accepts_cnt);
1468 if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1469 cm_node->listener->backlog) {
1470 nes_debug(NES_DBG_CM, "drop syn due to backlog "
1471 "pressure \n");
1472 cm_backlog_drops++;
1473 passive_open_err(cm_node, skb, 0);
1474 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001475 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001476 ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1477 1);
1478 if (ret) {
1479 passive_open_err(cm_node, skb, 0);
1480 /* drop pkt */
1481 break;
1482 }
1483 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1484 BUG_ON(cm_node->send_entry);
1485 cm_node->state = NES_CM_STATE_SYN_RCVD;
1486 send_syn(cm_node, 1, skb);
1487 break;
1488 case NES_CM_STATE_TSA:
1489 case NES_CM_STATE_ESTABLISHED:
1490 case NES_CM_STATE_FIN_WAIT1:
1491 case NES_CM_STATE_FIN_WAIT2:
1492 case NES_CM_STATE_MPAREQ_RCVD:
1493 case NES_CM_STATE_LAST_ACK:
1494 case NES_CM_STATE_CLOSING:
1495 case NES_CM_STATE_UNKNOWN:
1496 case NES_CM_STATE_CLOSED:
1497 default:
1498 drop_packet(skb);
1499 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001500 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001501}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001502
Faisal Latif6492cdf2008-07-24 20:50:45 -07001503static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1504 struct tcphdr *tcph)
1505{
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001506
Faisal Latif6492cdf2008-07-24 20:50:45 -07001507 int ret;
1508 u32 inc_sequence;
1509 int optionsize;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001510
Faisal Latif6492cdf2008-07-24 20:50:45 -07001511 optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1512 skb_pull(skb, tcph->doff << 2);
1513 inc_sequence = ntohl(tcph->seq);
1514 switch (cm_node->state) {
1515 case NES_CM_STATE_SYN_SENT:
1516 /* active open */
1517 if (check_syn(cm_node, tcph, skb))
1518 return;
1519 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1520 /* setup options */
1521 ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1522 if (ret) {
1523 nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1524 cm_node);
1525 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001526 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07001527 cleanup_retrans_entry(cm_node);
1528 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1529 send_mpa_request(cm_node, skb);
1530 cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1531 break;
1532 case NES_CM_STATE_MPAREQ_RCVD:
1533 /* passive open, so should not be here */
1534 passive_open_err(cm_node, skb, 1);
1535 break;
1536 case NES_CM_STATE_ESTABLISHED:
1537 case NES_CM_STATE_FIN_WAIT1:
1538 case NES_CM_STATE_FIN_WAIT2:
1539 case NES_CM_STATE_LAST_ACK:
1540 case NES_CM_STATE_TSA:
1541 case NES_CM_STATE_CLOSING:
1542 case NES_CM_STATE_UNKNOWN:
1543 case NES_CM_STATE_CLOSED:
1544 case NES_CM_STATE_MPAREQ_SENT:
1545 default:
1546 drop_packet(skb);
1547 break;
1548 }
1549}
1550
1551static void handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1552 struct tcphdr *tcph)
1553{
1554 int datasize = 0;
1555 u32 inc_sequence;
1556 u32 rem_seq_ack;
1557 u32 rem_seq;
1558 if (check_seq(cm_node, tcph, skb))
1559 return;
1560
1561 skb_pull(skb, tcph->doff << 2);
1562 inc_sequence = ntohl(tcph->seq);
1563 rem_seq = ntohl(tcph->seq);
1564 rem_seq_ack = ntohl(tcph->ack_seq);
1565 datasize = skb->len;
1566
1567 switch (cm_node->state) {
1568 case NES_CM_STATE_SYN_RCVD:
1569 /* Passive OPEN */
1570 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1571 cm_node->state = NES_CM_STATE_ESTABLISHED;
1572 if (datasize) {
1573 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1574 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1575 handle_rcv_mpa(cm_node, skb, NES_CM_EVENT_MPA_REQ);
1576 } else { /* rcvd ACK only */
1577 dev_kfree_skb_any(skb);
1578 cleanup_retrans_entry(cm_node);
1579 }
1580 break;
1581 case NES_CM_STATE_ESTABLISHED:
1582 /* Passive OPEN */
1583 /* We expect mpa frame to be received only */
1584 if (datasize) {
1585 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1586 cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1587 handle_rcv_mpa(cm_node, skb,
1588 NES_CM_EVENT_MPA_REQ);
1589 } else
1590 drop_packet(skb);
1591 break;
1592 case NES_CM_STATE_MPAREQ_SENT:
1593 cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1594 if (datasize) {
1595 cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1596 handle_rcv_mpa(cm_node, skb, NES_CM_EVENT_CONNECTED);
1597 } else { /* Could be just an ack pkt.. */
1598 cleanup_retrans_entry(cm_node);
1599 dev_kfree_skb_any(skb);
1600 }
1601 break;
1602 case NES_CM_STATE_FIN_WAIT1:
1603 case NES_CM_STATE_SYN_SENT:
1604 case NES_CM_STATE_FIN_WAIT2:
1605 case NES_CM_STATE_TSA:
1606 case NES_CM_STATE_CLOSED:
1607 case NES_CM_STATE_MPAREQ_RCVD:
1608 case NES_CM_STATE_LAST_ACK:
1609 case NES_CM_STATE_CLOSING:
1610 case NES_CM_STATE_UNKNOWN:
1611 default:
1612 drop_packet(skb);
1613 break;
1614 }
1615}
1616
1617
1618
1619static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1620 struct sk_buff *skb, int optionsize, int passive)
1621{
1622 u8 *optionsloc = (u8 *)&tcph[1];
1623 if (optionsize) {
1624 if (process_options(cm_node, optionsloc, optionsize,
1625 (u32)tcph->syn)) {
1626 nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
1627 __func__, cm_node);
1628 if (passive)
1629 passive_open_err(cm_node, skb, 0);
1630 else
1631 active_open_err(cm_node, skb, 0);
1632 return 1;
1633 }
1634 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001635
1636 cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
1637 cm_node->tcp_cntxt.snd_wscale;
1638
Faisal Latif6492cdf2008-07-24 20:50:45 -07001639 if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001640 cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001641 return 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001642}
1643
Faisal Latif6492cdf2008-07-24 20:50:45 -07001644/*
1645 * active_open_err() will send reset() if flag set..
1646 * It will also send ABORT event.
1647 */
1648
1649static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1650 int reset)
1651{
1652 cleanup_retrans_entry(cm_node);
1653 if (reset) {
1654 nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
1655 "state=%d\n", cm_node, cm_node->state);
1656 add_ref_cm_node(cm_node);
1657 send_reset(cm_node, skb);
1658 } else
1659 dev_kfree_skb_any(skb);
1660
1661 cm_node->state = NES_CM_STATE_CLOSED;
1662 create_event(cm_node, NES_CM_EVENT_ABORTED);
1663}
1664
1665/*
1666 * passive_open_err() will either do a reset() or will free up the skb and
1667 * remove the cm_node.
1668 */
1669
1670static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
1671 int reset)
1672{
1673 cleanup_retrans_entry(cm_node);
1674 cm_node->state = NES_CM_STATE_CLOSED;
1675 if (reset) {
1676 nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
1677 "cm_node=%p state =%d\n", cm_node, cm_node->state);
1678 send_reset(cm_node, skb);
1679 } else {
1680 dev_kfree_skb_any(skb);
1681 rem_ref_cm_node(cm_node->cm_core, cm_node);
1682 }
1683}
1684
1685/*
1686 * free_retrans_entry() routines assumes that the retrans_list_lock has
1687 * been acquired before calling.
1688 */
1689static void free_retrans_entry(struct nes_cm_node *cm_node)
1690{
1691 struct nes_timer_entry *send_entry;
1692 send_entry = cm_node->send_entry;
1693 if (send_entry) {
1694 cm_node->send_entry = NULL;
1695 dev_kfree_skb_any(send_entry->skb);
1696 kfree(send_entry);
1697 rem_ref_cm_node(cm_node->cm_core, cm_node);
1698 }
1699}
1700
1701static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
1702{
1703 unsigned long flags;
1704
1705 spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
1706 free_retrans_entry(cm_node);
1707 spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
1708}
1709
1710/**
1711 * process_packet
1712 * Returns skb if to be freed, else it will return NULL if already used..
1713 */
1714static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
1715 struct nes_cm_core *cm_core)
1716{
1717 enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
1718 struct tcphdr *tcph = tcp_hdr(skb);
1719 skb_pull(skb, ip_hdr(skb)->ihl << 2);
1720
1721 nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
1722 "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
1723 tcph->ack, tcph->rst, tcph->fin);
1724
1725 if (tcph->rst)
1726 pkt_type = NES_PKT_TYPE_RST;
1727 else if (tcph->syn) {
1728 pkt_type = NES_PKT_TYPE_SYN;
1729 if (tcph->ack)
1730 pkt_type = NES_PKT_TYPE_SYNACK;
1731 } else if (tcph->fin)
1732 pkt_type = NES_PKT_TYPE_FIN;
1733 else if (tcph->ack)
1734 pkt_type = NES_PKT_TYPE_ACK;
1735
1736 switch (pkt_type) {
1737 case NES_PKT_TYPE_SYN:
1738 handle_syn_pkt(cm_node, skb, tcph);
1739 break;
1740 case NES_PKT_TYPE_SYNACK:
1741 handle_synack_pkt(cm_node, skb, tcph);
1742 break;
1743 case NES_PKT_TYPE_ACK:
1744 handle_ack_pkt(cm_node, skb, tcph);
1745 break;
1746 case NES_PKT_TYPE_RST:
1747 handle_rst_pkt(cm_node, skb, tcph);
1748 break;
1749 case NES_PKT_TYPE_FIN:
1750 handle_fin_pkt(cm_node, skb, tcph);
1751 break;
1752 default:
1753 drop_packet(skb);
1754 break;
1755 }
1756}
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001757
1758/**
1759 * mini_cm_listen - create a listen node with params
1760 */
1761static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001762 struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001763{
1764 struct nes_cm_listener *listener;
1765 unsigned long flags;
1766
1767 nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
1768 cm_info->loc_addr, cm_info->loc_port);
1769
1770 /* cannot have multiple matching listeners */
1771 listener = find_listener(cm_core, htonl(cm_info->loc_addr),
1772 htons(cm_info->loc_port), NES_CM_LISTENER_EITHER_STATE);
1773 if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
1774 /* find automatically incs ref count ??? */
1775 atomic_dec(&listener->ref_count);
1776 nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
1777 return NULL;
1778 }
1779
1780 if (!listener) {
1781 /* create a CM listen node (1/2 node to compare incoming traffic to) */
1782 listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
1783 if (!listener) {
1784 nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
1785 return NULL;
1786 }
1787
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001788 listener->loc_addr = htonl(cm_info->loc_addr);
1789 listener->loc_port = htons(cm_info->loc_port);
1790 listener->reused_node = 0;
1791
1792 atomic_set(&listener->ref_count, 1);
1793 }
1794 /* pasive case */
1795 /* find already inc'ed the ref count */
1796 else {
1797 listener->reused_node = 1;
1798 }
1799
1800 listener->cm_id = cm_info->cm_id;
1801 atomic_set(&listener->pend_accepts_cnt, 0);
1802 listener->cm_core = cm_core;
1803 listener->nesvnic = nesvnic;
1804 atomic_inc(&cm_core->node_cnt);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001805
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001806 listener->conn_type = cm_info->conn_type;
1807 listener->backlog = cm_info->backlog;
1808 listener->listener_state = NES_CM_LISTENER_ACTIVE_STATE;
1809
1810 if (!listener->reused_node) {
1811 spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1812 list_add(&listener->list, &cm_core->listen_list.list);
1813 spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1814 atomic_inc(&cm_core->listen_node_cnt);
1815 }
1816
1817 nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
1818 " listener = %p, backlog = %d, cm_id = %p.\n",
1819 cm_info->loc_addr, cm_info->loc_port,
1820 listener, listener->backlog, listener->cm_id);
1821
1822 return listener;
1823}
1824
1825
1826/**
1827 * mini_cm_connect - make a connection node with params
1828 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001829struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
1830 struct nes_vnic *nesvnic, u16 private_data_len,
1831 void *private_data, struct nes_cm_info *cm_info)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001832{
1833 int ret = 0;
1834 struct nes_cm_node *cm_node;
1835 struct nes_cm_listener *loopbackremotelistener;
1836 struct nes_cm_node *loopbackremotenode;
1837 struct nes_cm_info loopback_cm_info;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001838 u16 mpa_frame_size = sizeof(struct ietf_mpa_frame) + private_data_len;
1839 struct ietf_mpa_frame *mpa_frame = NULL;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001840
1841 /* create a CM connection node */
1842 cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
1843 if (!cm_node)
1844 return NULL;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001845 mpa_frame = &cm_node->mpa_frame;
1846 strcpy(mpa_frame->key, IEFT_MPA_KEY_REQ);
1847 mpa_frame->flags = IETF_MPA_FLAGS_CRC;
1848 mpa_frame->rev = IETF_MPA_VERSION;
1849 mpa_frame->priv_data_len = htons(private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001850
Glenn Streiff7495ab62008-04-29 13:46:54 -07001851 /* set our node side to client (active) side */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001852 cm_node->tcp_cntxt.client = 1;
1853 cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1854
1855 if (cm_info->loc_addr == cm_info->rem_addr) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001856 loopbackremotelistener = find_listener(cm_core,
1857 ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
1858 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001859 if (loopbackremotelistener == NULL) {
1860 create_event(cm_node, NES_CM_EVENT_ABORTED);
1861 } else {
1862 atomic_inc(&cm_loopbacks);
1863 loopback_cm_info = *cm_info;
1864 loopback_cm_info.loc_port = cm_info->rem_port;
1865 loopback_cm_info.rem_port = cm_info->loc_port;
1866 loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001867 loopbackremotenode = make_cm_node(cm_core, nesvnic,
1868 &loopback_cm_info, loopbackremotelistener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001869 loopbackremotenode->loopbackpartner = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001870 loopbackremotenode->tcp_cntxt.rcv_wscale =
1871 NES_CM_DEFAULT_RCV_WND_SCALE;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001872 cm_node->loopbackpartner = loopbackremotenode;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001873 memcpy(loopbackremotenode->mpa_frame_buf, private_data,
1874 private_data_len);
1875 loopbackremotenode->mpa_frame_size = private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001876
Faisal Latif6492cdf2008-07-24 20:50:45 -07001877 /* we are done handling this state. */
1878 /* set node to a TSA state */
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001879 cm_node->state = NES_CM_STATE_TSA;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001880 cm_node->tcp_cntxt.rcv_nxt =
1881 loopbackremotenode->tcp_cntxt.loc_seq_num;
1882 loopbackremotenode->tcp_cntxt.rcv_nxt =
1883 cm_node->tcp_cntxt.loc_seq_num;
1884 cm_node->tcp_cntxt.max_snd_wnd =
1885 loopbackremotenode->tcp_cntxt.rcv_wnd;
1886 loopbackremotenode->tcp_cntxt.max_snd_wnd =
1887 cm_node->tcp_cntxt.rcv_wnd;
1888 cm_node->tcp_cntxt.snd_wnd =
1889 loopbackremotenode->tcp_cntxt.rcv_wnd;
1890 loopbackremotenode->tcp_cntxt.snd_wnd =
1891 cm_node->tcp_cntxt.rcv_wnd;
1892 cm_node->tcp_cntxt.snd_wscale =
1893 loopbackremotenode->tcp_cntxt.rcv_wscale;
1894 loopbackremotenode->tcp_cntxt.snd_wscale =
1895 cm_node->tcp_cntxt.rcv_wscale;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001896
1897 create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
1898 }
1899 return cm_node;
1900 }
1901
1902 /* set our node side to client (active) side */
1903 cm_node->tcp_cntxt.client = 1;
1904 /* init our MPA frame ptr */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001905 memcpy(mpa_frame->priv_data, private_data, private_data_len);
1906
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001907 cm_node->mpa_frame_size = mpa_frame_size;
1908
1909 /* send a syn and goto syn sent state */
1910 cm_node->state = NES_CM_STATE_SYN_SENT;
Faisal Latif6492cdf2008-07-24 20:50:45 -07001911 ret = send_syn(cm_node, 0, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001912
Faisal Latif6492cdf2008-07-24 20:50:45 -07001913 if (ret) {
1914 /* error in sending the syn free up the cm_node struct */
1915 nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
1916 "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
1917 cm_node->rem_addr, cm_node->rem_port, cm_node,
1918 cm_node->cm_id);
1919 rem_ref_cm_node(cm_node->cm_core, cm_node);
1920 cm_node = NULL;
1921 }
1922
1923 if (cm_node)
1924 nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
1925 "port=0x%04x, cm_node=%p, cm_id = %p.\n",
1926 cm_node->rem_addr, cm_node->rem_port, cm_node,
1927 cm_node->cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001928
1929 return cm_node;
1930}
1931
1932
1933/**
1934 * mini_cm_accept - accept a connection
1935 * This function is never called
1936 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07001937static int mini_cm_accept(struct nes_cm_core *cm_core,
1938 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001939{
1940 return 0;
1941}
1942
1943
1944/**
1945 * mini_cm_reject - reject and teardown a connection
1946 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07001947static int mini_cm_reject(struct nes_cm_core *cm_core,
Faisal Latif6492cdf2008-07-24 20:50:45 -07001948 struct ietf_mpa_frame *mpa_frame, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001949{
1950 int ret = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001951
Faisal Latif6492cdf2008-07-24 20:50:45 -07001952 nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
1953 __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001954
Faisal Latif6492cdf2008-07-24 20:50:45 -07001955 if (cm_node->tcp_cntxt.client)
1956 return ret;
1957 cleanup_retrans_entry(cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001958 cm_node->state = NES_CM_STATE_CLOSED;
1959 ret = send_fin(cm_node, NULL);
1960
Faisal Latif6492cdf2008-07-24 20:50:45 -07001961 if (cm_node->accept_pend) {
1962 BUG_ON(!cm_node->listener);
1963 atomic_dec(&cm_node->listener->pend_accepts_cnt);
1964 BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001965 }
1966
Faisal Latif6492cdf2008-07-24 20:50:45 -07001967 ret = send_reset(cm_node, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001968 return ret;
1969}
1970
1971
1972/**
1973 * mini_cm_close
1974 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07001975static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08001976{
1977 int ret = 0;
1978
1979 if (!cm_core || !cm_node)
1980 return -EINVAL;
1981
1982 switch (cm_node->state) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07001983 case NES_CM_STATE_SYN_RCVD:
1984 case NES_CM_STATE_SYN_SENT:
1985 case NES_CM_STATE_ONE_SIDE_ESTABLISHED:
1986 case NES_CM_STATE_ESTABLISHED:
1987 case NES_CM_STATE_ACCEPTING:
1988 case NES_CM_STATE_MPAREQ_SENT:
1989 case NES_CM_STATE_MPAREQ_RCVD:
1990 cleanup_retrans_entry(cm_node);
1991 send_reset(cm_node, NULL);
1992 break;
1993 case NES_CM_STATE_CLOSE_WAIT:
1994 cm_node->state = NES_CM_STATE_LAST_ACK;
1995 send_fin(cm_node, NULL);
1996 break;
1997 case NES_CM_STATE_FIN_WAIT1:
1998 case NES_CM_STATE_FIN_WAIT2:
1999 case NES_CM_STATE_LAST_ACK:
2000 case NES_CM_STATE_TIME_WAIT:
2001 case NES_CM_STATE_CLOSING:
2002 ret = -1;
2003 break;
2004 case NES_CM_STATE_LISTENING:
2005 case NES_CM_STATE_UNKNOWN:
2006 case NES_CM_STATE_INITED:
2007 case NES_CM_STATE_CLOSED:
2008 ret = rem_ref_cm_node(cm_core, cm_node);
2009 break;
2010 case NES_CM_STATE_TSA:
2011 if (cm_node->send_entry)
2012 printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2013 "send_entry=%p\n", cm_node->send_entry);
2014 ret = rem_ref_cm_node(cm_core, cm_node);
2015 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002016 }
2017 cm_node->cm_id = NULL;
2018 return ret;
2019}
2020
2021
2022/**
2023 * recv_pkt - recv an ETHERNET packet, and process it through CM
2024 * node state machine
2025 */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002026static void mini_cm_recv_pkt(struct nes_cm_core *cm_core,
2027 struct nes_vnic *nesvnic, struct sk_buff *skb)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002028{
2029 struct nes_cm_node *cm_node = NULL;
2030 struct nes_cm_listener *listener = NULL;
2031 struct iphdr *iph;
2032 struct tcphdr *tcph;
2033 struct nes_cm_info nfo;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002034
Faisal Latif6492cdf2008-07-24 20:50:45 -07002035 if (!skb)
2036 return;
2037 if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr)) {
2038 dev_kfree_skb_any(skb);
2039 return;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002040 }
2041
2042 iph = (struct iphdr *)skb->data;
2043 tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
2044 skb_reset_network_header(skb);
2045 skb_set_transport_header(skb, sizeof(*tcph));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002046 if (!tcph) {
2047 dev_kfree_skb_any(skb);
2048 return;
2049 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002050 skb->len = ntohs(iph->tot_len);
2051
2052 nfo.loc_addr = ntohl(iph->daddr);
2053 nfo.loc_port = ntohs(tcph->dest);
2054 nfo.rem_addr = ntohl(iph->saddr);
2055 nfo.rem_port = ntohs(tcph->source);
2056
Chien Tungbc5698f32008-04-23 11:55:45 -07002057 nes_debug(NES_DBG_CM, "Received packet: dest=" NIPQUAD_FMT
2058 ":0x%04X src=" NIPQUAD_FMT ":0x%04X\n",
2059 NIPQUAD(iph->daddr), tcph->dest,
2060 NIPQUAD(iph->saddr), tcph->source);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002061
Faisal Latif6492cdf2008-07-24 20:50:45 -07002062 do {
2063 cm_node = find_node(cm_core,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002064 nfo.rem_port, nfo.rem_addr,
2065 nfo.loc_port, nfo.loc_addr);
2066
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002067 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002068 /* Only type of packet accepted are for */
2069 /* the PASSIVE open (syn only) */
2070 if ((!tcph->syn) || (tcph->ack)) {
2071 cm_packets_dropped++;
2072 break;
2073 }
2074 listener = find_listener(cm_core, nfo.loc_addr,
2075 nfo.loc_port,
2076 NES_CM_LISTENER_ACTIVE_STATE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002077 if (listener) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002078 nfo.cm_id = listener->cm_id;
2079 nfo.conn_type = listener->conn_type;
2080 } else {
2081 nes_debug(NES_DBG_CM, "Unable to find listener "
2082 "for the pkt\n");
2083 cm_packets_dropped++;
2084 dev_kfree_skb_any(skb);
2085 break;
2086 }
2087
2088 cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2089 listener);
2090 if (!cm_node) {
2091 nes_debug(NES_DBG_CM, "Unable to allocate "
2092 "node\n");
2093 cm_packets_dropped++;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002094 atomic_dec(&listener->ref_count);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002095 dev_kfree_skb_any(skb);
2096 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002097 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002098 if (!tcph->rst && !tcph->fin) {
2099 cm_node->state = NES_CM_STATE_LISTENING;
2100 } else {
2101 cm_packets_dropped++;
2102 rem_ref_cm_node(cm_core, cm_node);
2103 dev_kfree_skb_any(skb);
2104 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002105 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002106 add_ref_cm_node(cm_node);
2107 } else if (cm_node->state == NES_CM_STATE_TSA) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002108 rem_ref_cm_node(cm_core, cm_node);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002109 atomic_inc(&cm_accel_dropped_pkts);
2110 dev_kfree_skb_any(skb);
2111 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002112 }
Faisal Latif6492cdf2008-07-24 20:50:45 -07002113 process_packet(cm_node, skb, cm_core);
2114 rem_ref_cm_node(cm_core, cm_node);
2115 } while (0);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002116}
2117
2118
2119/**
2120 * nes_cm_alloc_core - allocate a top level instance of a cm core
2121 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002122static struct nes_cm_core *nes_cm_alloc_core(void)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002123{
2124 int i;
2125
2126 struct nes_cm_core *cm_core;
2127 struct sk_buff *skb = NULL;
2128
2129 /* setup the CM core */
2130 /* alloc top level core control structure */
2131 cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2132 if (!cm_core)
2133 return NULL;
2134
2135 INIT_LIST_HEAD(&cm_core->connected_nodes);
2136 init_timer(&cm_core->tcp_timer);
2137 cm_core->tcp_timer.function = nes_cm_timer_tick;
2138
2139 cm_core->mtu = NES_CM_DEFAULT_MTU;
2140 cm_core->state = NES_CM_STATE_INITED;
2141 cm_core->free_tx_pkt_max = NES_CM_DEFAULT_FREE_PKTS;
2142
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002143 atomic_set(&cm_core->events_posted, 0);
2144
2145 /* init the packet lists */
2146 skb_queue_head_init(&cm_core->tx_free_list);
2147
2148 for (i = 0; i < NES_CM_DEFAULT_FRAME_CNT; i++) {
2149 skb = dev_alloc_skb(cm_core->mtu);
2150 if (!skb) {
2151 kfree(cm_core);
2152 return NULL;
2153 }
2154 /* add 'raw' skb to free frame list */
2155 skb_queue_head(&cm_core->tx_free_list, skb);
2156 }
2157
2158 cm_core->api = &nes_cm_api;
2159
2160 spin_lock_init(&cm_core->ht_lock);
2161 spin_lock_init(&cm_core->listen_list_lock);
2162
2163 INIT_LIST_HEAD(&cm_core->listen_list.list);
2164
2165 nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2166
2167 nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2168 cm_core->event_wq = create_singlethread_workqueue("nesewq");
2169 cm_core->post_event = nes_cm_post_event;
2170 nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2171 cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2172
2173 print_core(cm_core);
2174 return cm_core;
2175}
2176
2177
2178/**
2179 * mini_cm_dealloc_core - deallocate a top level instance of a cm core
2180 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002181static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002182{
2183 nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2184
2185 if (!cm_core)
2186 return -EINVAL;
2187
2188 barrier();
2189
2190 if (timer_pending(&cm_core->tcp_timer)) {
2191 del_timer(&cm_core->tcp_timer);
2192 }
2193
2194 destroy_workqueue(cm_core->event_wq);
2195 destroy_workqueue(cm_core->disconn_wq);
2196 nes_debug(NES_DBG_CM, "\n");
2197 kfree(cm_core);
2198
2199 return 0;
2200}
2201
2202
2203/**
2204 * mini_cm_get
2205 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002206static int mini_cm_get(struct nes_cm_core *cm_core)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002207{
2208 return cm_core->state;
2209}
2210
2211
2212/**
2213 * mini_cm_set
2214 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002215static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002216{
2217 int ret = 0;
2218
2219 switch (type) {
2220 case NES_CM_SET_PKT_SIZE:
2221 cm_core->mtu = value;
2222 break;
2223 case NES_CM_SET_FREE_PKT_Q_SIZE:
2224 cm_core->free_tx_pkt_max = value;
2225 break;
2226 default:
2227 /* unknown set option */
2228 ret = -EINVAL;
2229 }
2230
2231 return ret;
2232}
2233
2234
2235/**
2236 * nes_cm_init_tsa_conn setup HW; MPA frames must be
2237 * successfully exchanged when this is called
2238 */
2239static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2240{
2241 int ret = 0;
2242
2243 if (!nesqp)
2244 return -EINVAL;
2245
2246 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_IPV4 |
2247 NES_QPCONTEXT_MISC_NO_NAGLE | NES_QPCONTEXT_MISC_DO_NOT_FRAG |
2248 NES_QPCONTEXT_MISC_DROS);
2249
2250 if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2251 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WSCALE);
2252
2253 nesqp->nesqp_context->misc2 |= cpu_to_le32(64 << NES_QPCONTEXT_MISC2_TTL_SHIFT);
2254
2255 nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2256
2257 nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2258 (u32)NES_QPCONTEXT_TCPSTATE_EST << NES_QPCONTEXT_TCPFLOW_TCP_STATE_SHIFT);
2259
2260 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2261 (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2262 NES_QPCONTEXT_PDWSCALE_SND_WSCALE_MASK);
2263
2264 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2265 (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2266 NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_MASK);
2267
2268 nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2269 nesqp->nesqp_context->ts_recent = 0;
2270 nesqp->nesqp_context->ts_age = 0;
2271 nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2272 nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2273 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2274 nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2275 cm_node->tcp_cntxt.rcv_wscale);
2276 nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2277 nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2278 nesqp->nesqp_context->srtt = 0;
2279 nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2280 nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2281 nesqp->nesqp_context->cwnd = cpu_to_le32(2*cm_node->tcp_cntxt.mss);
2282 nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2283 nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2284 nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2285
2286 nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2287 " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2288 nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2289 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2290 cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2291 le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2292 le32_to_cpu(nesqp->nesqp_context->misc));
2293 nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2294 nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2295 nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2296
2297 nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2298 cm_node->state = NES_CM_STATE_TSA;
2299
2300 return ret;
2301}
2302
2303
2304/**
2305 * nes_cm_disconn
2306 */
2307int nes_cm_disconn(struct nes_qp *nesqp)
2308{
2309 unsigned long flags;
2310
2311 spin_lock_irqsave(&nesqp->lock, flags);
2312 if (nesqp->disconn_pending == 0) {
2313 nesqp->disconn_pending++;
2314 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002315 /* init our disconnect work element, to */
2316 INIT_WORK(&nesqp->disconn_work, nes_disconnect_worker);
2317
2318 queue_work(g_cm_core->disconn_wq, &nesqp->disconn_work);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002319 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002320 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002321
2322 return 0;
2323}
2324
2325
2326/**
2327 * nes_disconnect_worker
2328 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002329static void nes_disconnect_worker(struct work_struct *work)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002330{
2331 struct nes_qp *nesqp = container_of(work, struct nes_qp, disconn_work);
2332
2333 nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2334 nesqp->last_aeq, nesqp->hwqp.qp_id);
2335 nes_cm_disconn_true(nesqp);
2336}
2337
2338
2339/**
2340 * nes_cm_disconn_true
2341 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002342static int nes_cm_disconn_true(struct nes_qp *nesqp)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002343{
2344 unsigned long flags;
2345 int ret = 0;
2346 struct iw_cm_id *cm_id;
2347 struct iw_cm_event cm_event;
2348 struct nes_vnic *nesvnic;
2349 u16 last_ae;
2350 u8 original_hw_tcp_state;
2351 u8 original_ibqp_state;
2352 u8 issued_disconnect_reset = 0;
2353
2354 if (!nesqp) {
2355 nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2356 return -1;
2357 }
2358
2359 spin_lock_irqsave(&nesqp->lock, flags);
2360 cm_id = nesqp->cm_id;
2361 /* make sure we havent already closed this connection */
2362 if (!cm_id) {
2363 nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2364 nesqp->hwqp.qp_id);
2365 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002366 return -1;
2367 }
2368
2369 nesvnic = to_nesvnic(nesqp->ibqp.device);
2370 nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2371
2372 original_hw_tcp_state = nesqp->hw_tcp_state;
2373 original_ibqp_state = nesqp->ibqp_state;
2374 last_ae = nesqp->last_aeq;
2375
2376
2377 nes_debug(NES_DBG_CM, "set ibqp_state=%u\n", nesqp->ibqp_state);
2378
2379 if ((nesqp->cm_id) && (cm_id->event_handler)) {
2380 if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2381 ((original_ibqp_state == IB_QPS_RTS) &&
2382 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2383 atomic_inc(&cm_disconnects);
2384 cm_event.event = IW_CM_EVENT_DISCONNECT;
2385 if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET) {
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002386 cm_event.status = IW_CM_EVENT_STATUS_RESET;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002387 nes_debug(NES_DBG_CM, "Generating a CM "
2388 "Disconnect Event (status reset) for "
2389 "QP%u, cm_id = %p. \n",
2390 nesqp->hwqp.qp_id, cm_id);
2391 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002392 cm_event.status = IW_CM_EVENT_STATUS_OK;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002393
2394 cm_event.local_addr = cm_id->local_addr;
2395 cm_event.remote_addr = cm_id->remote_addr;
2396 cm_event.private_data = NULL;
2397 cm_event.private_data_len = 0;
2398
Faisal Latif6492cdf2008-07-24 20:50:45 -07002399 nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2400 " for QP%u, SQ Head = %u, SQ Tail = %u. "
2401 "cm_id = %p, refcount = %u.\n",
2402 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2403 nesqp->hwqp.sq_tail, cm_id,
2404 atomic_read(&nesqp->refcount));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002405
2406 spin_unlock_irqrestore(&nesqp->lock, flags);
2407 ret = cm_id->event_handler(cm_id, &cm_event);
2408 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002409 nes_debug(NES_DBG_CM, "OFA CM event_handler "
2410 "returned, ret=%d\n", ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002411 spin_lock_irqsave(&nesqp->lock, flags);
2412 }
2413
2414 nesqp->disconn_pending = 0;
2415 /* There might have been another AE while the lock was released */
2416 original_hw_tcp_state = nesqp->hw_tcp_state;
2417 original_ibqp_state = nesqp->ibqp_state;
2418 last_ae = nesqp->last_aeq;
2419
2420 if ((issued_disconnect_reset == 0) && (nesqp->cm_id) &&
2421 ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2422 (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2423 (last_ae == NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) ||
2424 (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2425 atomic_inc(&cm_closes);
2426 nesqp->cm_id = NULL;
2427 nesqp->in_disconnect = 0;
2428 spin_unlock_irqrestore(&nesqp->lock, flags);
2429 nes_disconnect(nesqp, 1);
2430
2431 cm_id->provider_data = nesqp;
2432 /* Send up the close complete event */
2433 cm_event.event = IW_CM_EVENT_CLOSE;
2434 cm_event.status = IW_CM_EVENT_STATUS_OK;
2435 cm_event.provider_data = cm_id->provider_data;
2436 cm_event.local_addr = cm_id->local_addr;
2437 cm_event.remote_addr = cm_id->remote_addr;
2438 cm_event.private_data = NULL;
2439 cm_event.private_data_len = 0;
2440
2441 ret = cm_id->event_handler(cm_id, &cm_event);
2442 if (ret) {
2443 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2444 }
2445
2446 cm_id->rem_ref(cm_id);
2447
2448 spin_lock_irqsave(&nesqp->lock, flags);
2449 if (nesqp->flush_issued == 0) {
2450 nesqp->flush_issued = 1;
2451 spin_unlock_irqrestore(&nesqp->lock, flags);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002452 flush_wqes(nesvnic->nesdev, nesqp,
2453 NES_CQP_FLUSH_RQ, 1);
2454 } else
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002455 spin_unlock_irqrestore(&nesqp->lock, flags);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002456 } else {
2457 cm_id = nesqp->cm_id;
2458 spin_unlock_irqrestore(&nesqp->lock, flags);
2459 /* check to see if the inbound reset beat the outbound reset */
2460 if ((!cm_id) && (last_ae==NES_AEQE_AEID_RESET_SENT)) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002461 nes_debug(NES_DBG_CM, "QP%u: Decing refcount "
2462 "due to inbound reset beating the "
2463 "outbound reset.\n", nesqp->hwqp.qp_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002464 }
2465 }
2466 } else {
2467 nesqp->disconn_pending = 0;
2468 spin_unlock_irqrestore(&nesqp->lock, flags);
2469 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002470
2471 return 0;
2472}
2473
2474
2475/**
2476 * nes_disconnect
2477 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002478static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002479{
2480 int ret = 0;
2481 struct nes_vnic *nesvnic;
2482 struct nes_device *nesdev;
2483
2484 nesvnic = to_nesvnic(nesqp->ibqp.device);
2485 if (!nesvnic)
2486 return -EINVAL;
2487
2488 nesdev = nesvnic->nesdev;
2489
2490 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2491 atomic_read(&nesvnic->netdev->refcnt));
2492
2493 if (nesqp->active_conn) {
2494
2495 /* indicate this connection is NOT active */
2496 nesqp->active_conn = 0;
2497 } else {
2498 /* Need to free the Last Streaming Mode Message */
2499 if (nesqp->ietf_frame) {
2500 pci_free_consistent(nesdev->pcidev,
2501 nesqp->private_data_len+sizeof(struct ietf_mpa_frame),
2502 nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2503 }
2504 }
2505
2506 /* close the CM node down if it is still active */
2507 if (nesqp->cm_node) {
2508 nes_debug(NES_DBG_CM, "Call close API\n");
2509
2510 g_cm_core->api->close(g_cm_core, nesqp->cm_node);
2511 nesqp->cm_node = NULL;
2512 }
2513
2514 return ret;
2515}
2516
2517
2518/**
2519 * nes_accept
2520 */
2521int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2522{
2523 u64 u64temp;
2524 struct ib_qp *ibqp;
2525 struct nes_qp *nesqp;
2526 struct nes_vnic *nesvnic;
2527 struct nes_device *nesdev;
2528 struct nes_cm_node *cm_node;
2529 struct nes_adapter *adapter;
2530 struct ib_qp_attr attr;
2531 struct iw_cm_event cm_event;
2532 struct nes_hw_qp_wqe *wqe;
2533 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002534 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002535 int ret;
2536
2537 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2538 if (!ibqp)
2539 return -EINVAL;
2540
2541 /* get all our handles */
2542 nesqp = to_nesqp(ibqp);
2543 nesvnic = to_nesvnic(nesqp->ibqp.device);
2544 nesdev = nesvnic->nesdev;
2545 adapter = nesdev->nesadapter;
2546
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002547 cm_node = (struct nes_cm_node *)cm_id->provider_data;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002548 nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
2549 "%s\n", cm_node, nesvnic, nesvnic->netdev,
2550 nesvnic->netdev->name);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002551
2552 /* associate the node with the QP */
2553 nesqp->cm_node = (void *)cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002554 cm_node->nesqp = nesqp;
2555 nes_add_ref(&nesqp->ibqp);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002556
Faisal Latif6492cdf2008-07-24 20:50:45 -07002557 nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
2558 nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002559 atomic_inc(&cm_accepts);
2560
2561 nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2562 atomic_read(&nesvnic->netdev->refcnt));
2563
Faisal Latif6492cdf2008-07-24 20:50:45 -07002564 /* allocate the ietf frame and space for private data */
2565 nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
2566 sizeof(struct ietf_mpa_frame) + conn_param->private_data_len,
2567 &nesqp->ietf_frame_pbase);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002568
Faisal Latif6492cdf2008-07-24 20:50:45 -07002569 if (!nesqp->ietf_frame) {
2570 nes_debug(NES_DBG_CM, "Unable to allocate memory for private "
2571 "data\n");
2572 return -ENOMEM;
2573 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002574
2575
Faisal Latif6492cdf2008-07-24 20:50:45 -07002576 /* setup the MPA frame */
2577 nesqp->private_data_len = conn_param->private_data_len;
2578 memcpy(nesqp->ietf_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002579
Faisal Latif6492cdf2008-07-24 20:50:45 -07002580 memcpy(nesqp->ietf_frame->priv_data, conn_param->private_data,
2581 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002582
Faisal Latif6492cdf2008-07-24 20:50:45 -07002583 nesqp->ietf_frame->priv_data_len =
2584 cpu_to_be16(conn_param->private_data_len);
2585 nesqp->ietf_frame->rev = mpa_version;
2586 nesqp->ietf_frame->flags = IETF_MPA_FLAGS_CRC;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002587
Faisal Latif6492cdf2008-07-24 20:50:45 -07002588 /* setup our first outgoing iWarp send WQE (the IETF frame response) */
2589 wqe = &nesqp->hwqp.sq_vbase[0];
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002590
Faisal Latif6492cdf2008-07-24 20:50:45 -07002591 if (cm_id->remote_addr.sin_addr.s_addr !=
2592 cm_id->local_addr.sin_addr.s_addr) {
2593 u64temp = (unsigned long)nesqp;
2594 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
2595 set_wqe_64bit_value(wqe->wqe_words,
2596 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX,
2597 u64temp);
2598 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
2599 cpu_to_le32(NES_IWARP_SQ_WQE_STREAMING |
2600 NES_IWARP_SQ_WQE_WRPDU);
2601 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] =
2602 cpu_to_le32(conn_param->private_data_len +
2603 sizeof(struct ietf_mpa_frame));
2604 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] =
2605 cpu_to_le32((u32)nesqp->ietf_frame_pbase);
2606 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] =
2607 cpu_to_le32((u32)((u64)nesqp->ietf_frame_pbase >> 32));
2608 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] =
2609 cpu_to_le32(conn_param->private_data_len +
2610 sizeof(struct ietf_mpa_frame));
2611 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002612
Faisal Latif6492cdf2008-07-24 20:50:45 -07002613 nesqp->nesqp_context->ird_ord_sizes |=
2614 cpu_to_le32(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2615 NES_QPCONTEXT_ORDIRD_WRPDU);
2616 } else {
2617 nesqp->nesqp_context->ird_ord_sizes |=
2618 cpu_to_le32((NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
2619 NES_QPCONTEXT_ORDIRD_WRPDU |
2620 NES_QPCONTEXT_ORDIRD_ALSMM));
2621 }
2622 nesqp->skip_lsmm = 1;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002623
2624
2625 /* Cache the cm_id in the qp */
2626 nesqp->cm_id = cm_id;
2627 cm_node->cm_id = cm_id;
2628
2629 /* nesqp->cm_node = (void *)cm_id->provider_data; */
2630 cm_id->provider_data = nesqp;
2631 nesqp->active_conn = 0;
2632
Faisal Latif6492cdf2008-07-24 20:50:45 -07002633 if (cm_node->state == NES_CM_STATE_TSA)
2634 nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
2635 cm_node);
2636
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002637 nes_cm_init_tsa_conn(nesqp, cm_node);
2638
Faisal Latif6492cdf2008-07-24 20:50:45 -07002639 nesqp->nesqp_context->tcpPorts[0] =
2640 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
2641 nesqp->nesqp_context->tcpPorts[1] =
2642 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
2643
2644 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2645 nesqp->nesqp_context->ip0 =
2646 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
2647 else
2648 nesqp->nesqp_context->ip0 =
2649 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002650
2651 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002652 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
2653 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002654
Faisal Latif6492cdf2008-07-24 20:50:45 -07002655 nesqp->nesqp_context->arp_index_vlan |=
2656 cpu_to_le32(nes_arp_table(nesdev,
2657 le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002658 NES_ARP_RESOLVE) << 16);
2659
2660 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002661 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002662
2663 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
2664
2665 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07002666 ((u32)1 << NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT));
2667 nesqp->nesqp_context->ird_ord_sizes |=
2668 cpu_to_le32((u32)conn_param->ord);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002669
2670 memset(&nes_quad, 0, sizeof(nes_quad));
Faisal Latif6492cdf2008-07-24 20:50:45 -07002671 nes_quad.DstIpAdrIndex =
2672 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
2673 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
2674 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
2675 else
2676 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
2677 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
2678 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002679
2680 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06002681 crc_value = get_crc_value(&nes_quad);
2682 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002683 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07002684 nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002685
2686 nesqp->hte_index &= adapter->hte_index_mask;
2687 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
2688
2689 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
2690
Faisal Latif6492cdf2008-07-24 20:50:45 -07002691 nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
2692 "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
2693 "private data length=%zu.\n", nesqp->hwqp.qp_id,
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002694 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2695 ntohs(cm_id->remote_addr.sin_port),
2696 ntohl(cm_id->local_addr.sin_addr.s_addr),
2697 ntohs(cm_id->local_addr.sin_port),
2698 le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2699 le32_to_cpu(nesqp->nesqp_context->snd_nxt),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002700 conn_param->private_data_len +
2701 sizeof(struct ietf_mpa_frame));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002702
2703 attr.qp_state = IB_QPS_RTS;
2704 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
2705
2706 /* notify OF layer that accept event was successfull */
2707 cm_id->add_ref(cm_id);
2708
2709 cm_event.event = IW_CM_EVENT_ESTABLISHED;
2710 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
2711 cm_event.provider_data = (void *)nesqp;
2712 cm_event.local_addr = cm_id->local_addr;
2713 cm_event.remote_addr = cm_id->remote_addr;
2714 cm_event.private_data = NULL;
2715 cm_event.private_data_len = 0;
2716 ret = cm_id->event_handler(cm_id, &cm_event);
2717 if (cm_node->loopbackpartner) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002718 cm_node->loopbackpartner->mpa_frame_size =
2719 nesqp->private_data_len;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002720 /* copy entire MPA frame to our cm_node's frame */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002721 memcpy(cm_node->loopbackpartner->mpa_frame_buf,
2722 nesqp->ietf_frame->priv_data, nesqp->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002723 create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
2724 }
2725 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07002726 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
2727 "ret=%d\n", __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002728
2729 return 0;
2730}
2731
2732
2733/**
2734 * nes_reject
2735 */
2736int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2737{
2738 struct nes_cm_node *cm_node;
2739 struct nes_cm_core *cm_core;
2740
2741 atomic_inc(&cm_rejects);
2742 cm_node = (struct nes_cm_node *) cm_id->provider_data;
2743 cm_core = cm_node->cm_core;
2744 cm_node->mpa_frame_size = sizeof(struct ietf_mpa_frame) + pdata_len;
2745
2746 strcpy(&cm_node->mpa_frame.key[0], IEFT_MPA_KEY_REP);
2747 memcpy(&cm_node->mpa_frame.priv_data, pdata, pdata_len);
2748
2749 cm_node->mpa_frame.priv_data_len = cpu_to_be16(pdata_len);
2750 cm_node->mpa_frame.rev = mpa_version;
2751 cm_node->mpa_frame.flags = IETF_MPA_FLAGS_CRC | IETF_MPA_FLAGS_REJECT;
2752
2753 cm_core->api->reject(cm_core, &cm_node->mpa_frame, cm_node);
2754
2755 return 0;
2756}
2757
2758
2759/**
2760 * nes_connect
2761 * setup and launch cm connect node
2762 */
2763int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2764{
2765 struct ib_qp *ibqp;
2766 struct nes_qp *nesqp;
2767 struct nes_vnic *nesvnic;
2768 struct nes_device *nesdev;
2769 struct nes_cm_node *cm_node;
2770 struct nes_cm_info cm_info;
2771
2772 ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
2773 if (!ibqp)
2774 return -EINVAL;
2775 nesqp = to_nesqp(ibqp);
2776 if (!nesqp)
2777 return -EINVAL;
2778 nesvnic = to_nesvnic(nesqp->ibqp.device);
2779 if (!nesvnic)
2780 return -EINVAL;
2781 nesdev = nesvnic->nesdev;
2782 if (!nesdev)
2783 return -EINVAL;
2784
Faisal Latif6492cdf2008-07-24 20:50:45 -07002785 nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
2786 "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
2787 ntohl(nesvnic->local_ipaddr),
2788 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2789 ntohs(cm_id->remote_addr.sin_port),
2790 ntohl(cm_id->local_addr.sin_addr.s_addr),
2791 ntohs(cm_id->local_addr.sin_port));
2792
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002793 atomic_inc(&cm_connects);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002794 nesqp->active_conn = 1;
2795
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002796 /* cache the cm_id in the qp */
2797 nesqp->cm_id = cm_id;
2798
2799 cm_id->provider_data = nesqp;
2800
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002801 nesqp->private_data_len = conn_param->private_data_len;
2802 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
2803 nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
Faisal Latif6492cdf2008-07-24 20:50:45 -07002804 nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
2805 conn_param->private_data_len);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002806
Faisal Latif6492cdf2008-07-24 20:50:45 -07002807 if (cm_id->local_addr.sin_addr.s_addr !=
2808 cm_id->remote_addr.sin_addr.s_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002809 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002810 PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002811
2812 /* set up the connection params for the node */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002813 cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
2814 cm_info.loc_port = htons(cm_id->local_addr.sin_port);
2815 cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
2816 cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002817 cm_info.cm_id = cm_id;
2818 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
2819
2820 cm_id->add_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002821
2822 /* create a connect CM node connection */
Faisal Latif6492cdf2008-07-24 20:50:45 -07002823 cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
2824 conn_param->private_data_len, (void *)conn_param->private_data,
2825 &cm_info);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002826 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002827 if (cm_id->local_addr.sin_addr.s_addr !=
2828 cm_id->remote_addr.sin_addr.s_addr)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002829 nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
Faisal Latif6492cdf2008-07-24 20:50:45 -07002830 PCI_FUNC(nesdev->pcidev->devfn),
2831 NES_MANAGE_APBVT_DEL);
2832
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002833 cm_id->rem_ref(cm_id);
2834 return -ENOMEM;
2835 }
2836
2837 cm_node->apbvt_set = 1;
2838 nesqp->cm_node = cm_node;
Faisal Latif6492cdf2008-07-24 20:50:45 -07002839 cm_node->nesqp = nesqp;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002840
2841 return 0;
2842}
2843
2844
2845/**
2846 * nes_create_listen
2847 */
2848int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
2849{
2850 struct nes_vnic *nesvnic;
2851 struct nes_cm_listener *cm_node;
2852 struct nes_cm_info cm_info;
2853 struct nes_adapter *adapter;
2854 int err;
2855
2856
2857 nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
2858 cm_id, ntohs(cm_id->local_addr.sin_port));
2859
2860 nesvnic = to_nesvnic(cm_id->device);
2861 if (!nesvnic)
2862 return -EINVAL;
2863 adapter = nesvnic->nesdev->nesadapter;
2864 nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
2865 nesvnic, nesvnic->netdev, nesvnic->netdev->name);
2866
2867 nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
2868 nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
2869
2870 /* setup listen params in our api call struct */
2871 cm_info.loc_addr = nesvnic->local_ipaddr;
2872 cm_info.loc_port = cm_id->local_addr.sin_port;
2873 cm_info.backlog = backlog;
2874 cm_info.cm_id = cm_id;
2875
2876 cm_info.conn_type = NES_CM_IWARP_CONN_TYPE;
2877
2878
2879 cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
2880 if (!cm_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002881 printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
Harvey Harrison33718362008-04-16 21:01:10 -07002882 __func__, __LINE__);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002883 return -ENOMEM;
2884 }
2885
2886 cm_id->provider_data = cm_node;
2887
2888 if (!cm_node->reused_node) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002889 err = nes_manage_apbvt(nesvnic,
2890 ntohs(cm_id->local_addr.sin_port),
2891 PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
2892 NES_MANAGE_APBVT_ADD);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002893 if (err) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07002894 printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
2895 err);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002896 g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
2897 return err;
2898 }
2899 cm_listens_created++;
2900 }
2901
2902 cm_id->add_ref(cm_id);
2903 cm_id->provider_data = (void *)cm_node;
2904
2905
2906 return 0;
2907}
2908
2909
2910/**
2911 * nes_destroy_listen
2912 */
2913int nes_destroy_listen(struct iw_cm_id *cm_id)
2914{
2915 if (cm_id->provider_data)
2916 g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
2917 else
2918 nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
2919
2920 cm_id->rem_ref(cm_id);
2921
2922 return 0;
2923}
2924
2925
2926/**
2927 * nes_cm_recv
2928 */
2929int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
2930{
2931 cm_packets_received++;
2932 if ((g_cm_core) && (g_cm_core->api)) {
2933 g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
2934 } else {
2935 nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
2936 " cm is not setup properly.\n");
2937 }
2938
2939 return 0;
2940}
2941
2942
2943/**
2944 * nes_cm_start
2945 * Start and init a cm core module
2946 */
2947int nes_cm_start(void)
2948{
2949 nes_debug(NES_DBG_CM, "\n");
2950 /* create the primary CM core, pass this handle to subsequent core inits */
2951 g_cm_core = nes_cm_alloc_core();
2952 if (g_cm_core) {
2953 return 0;
2954 } else {
2955 return -ENOMEM;
2956 }
2957}
2958
2959
2960/**
2961 * nes_cm_stop
2962 * stop and dealloc all cm core instances
2963 */
2964int nes_cm_stop(void)
2965{
2966 g_cm_core->api->destroy_cm_core(g_cm_core);
2967 return 0;
2968}
2969
2970
2971/**
2972 * cm_event_connected
2973 * handle a connected event, setup QPs and HW
2974 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07002975static void cm_event_connected(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002976{
2977 u64 u64temp;
2978 struct nes_qp *nesqp;
2979 struct nes_vnic *nesvnic;
2980 struct nes_device *nesdev;
2981 struct nes_cm_node *cm_node;
2982 struct nes_adapter *nesadapter;
2983 struct ib_qp_attr attr;
2984 struct iw_cm_id *cm_id;
2985 struct iw_cm_event cm_event;
2986 struct nes_hw_qp_wqe *wqe;
2987 struct nes_v4_quad nes_quad;
Faisal Latif30da7cf2008-02-21 08:31:22 -06002988 u32 crc_value;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08002989 int ret;
2990
2991 /* get all our handles */
2992 cm_node = event->cm_node;
2993 cm_id = cm_node->cm_id;
2994 nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
2995 nesqp = (struct nes_qp *)cm_id->provider_data;
2996 nesvnic = to_nesvnic(nesqp->ibqp.device);
2997 nesdev = nesvnic->nesdev;
2998 nesadapter = nesdev->nesadapter;
2999
3000 if (nesqp->destroyed) {
3001 return;
3002 }
3003 atomic_inc(&cm_connecteds);
3004 nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3005 " local port 0x%04X. jiffies = %lu.\n",
3006 nesqp->hwqp.qp_id,
3007 ntohl(cm_id->remote_addr.sin_addr.s_addr),
3008 ntohs(cm_id->remote_addr.sin_port),
3009 ntohs(cm_id->local_addr.sin_port),
3010 jiffies);
3011
3012 nes_cm_init_tsa_conn(nesqp, cm_node);
3013
3014 /* set the QP tsa context */
Faisal Latif6492cdf2008-07-24 20:50:45 -07003015 nesqp->nesqp_context->tcpPorts[0] =
3016 cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3017 nesqp->nesqp_context->tcpPorts[1] =
3018 cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3019 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3020 nesqp->nesqp_context->ip0 =
3021 cpu_to_le32(ntohl(nesvnic->local_ipaddr));
3022 else
3023 nesqp->nesqp_context->ip0 =
3024 cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003025
3026 nesqp->nesqp_context->misc2 |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003027 (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3028 NES_QPCONTEXT_MISC2_SRC_IP_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003029 nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
Faisal Latif6492cdf2008-07-24 20:50:45 -07003030 nes_arp_table(nesdev,
3031 le32_to_cpu(nesqp->nesqp_context->ip0),
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003032 NULL, NES_ARP_RESOLVE) << 16);
3033 nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3034 jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3035 nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3036 nesqp->nesqp_context->ird_ord_sizes |=
Faisal Latif6492cdf2008-07-24 20:50:45 -07003037 cpu_to_le32((u32)1 <<
3038 NES_QPCONTEXT_ORDIRD_IWARP_MODE_SHIFT);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003039
3040 /* Adjust tail for not having a LSMM */
3041 nesqp->hwqp.sq_tail = 1;
3042
3043#if defined(NES_SEND_FIRST_WRITE)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003044 if (cm_node->send_write0) {
3045 nes_debug(NES_DBG_CM, "Sending first write.\n");
3046 wqe = &nesqp->hwqp.sq_vbase[0];
3047 u64temp = (unsigned long)nesqp;
3048 u64temp |= NES_SW_CONTEXT_ALIGN>>1;
3049 set_wqe_64bit_value(wqe->wqe_words,
3050 NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
3051 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] =
3052 cpu_to_le32(NES_IWARP_SQ_OP_RDMAW);
3053 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX] = 0;
3054 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_LOW_IDX] = 0;
3055 wqe->wqe_words[NES_IWARP_SQ_WQE_FRAG0_HIGH_IDX] = 0;
3056 wqe->wqe_words[NES_IWARP_SQ_WQE_LENGTH0_IDX] = 0;
3057 wqe->wqe_words[NES_IWARP_SQ_WQE_STAG0_IDX] = 0;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003058
Faisal Latif6492cdf2008-07-24 20:50:45 -07003059 /* use the reserved spot on the WQ for the extra first WQE */
3060 nesqp->nesqp_context->ird_ord_sizes &=
3061 cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
3062 NES_QPCONTEXT_ORDIRD_WRPDU |
3063 NES_QPCONTEXT_ORDIRD_ALSMM));
3064 nesqp->skip_lsmm = 1;
3065 nesqp->hwqp.sq_tail = 0;
3066 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3067 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3068 }
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003069#endif
3070
3071 memset(&nes_quad, 0, sizeof(nes_quad));
3072
Faisal Latif6492cdf2008-07-24 20:50:45 -07003073 nes_quad.DstIpAdrIndex =
3074 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3075 if (ipv4_is_loopback(cm_id->remote_addr.sin_addr.s_addr))
3076 nes_quad.SrcIpadr = nesvnic->local_ipaddr;
3077 else
3078 nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003079 nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3080 nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3081
3082 /* Produce hash key */
Faisal Latif30da7cf2008-02-21 08:31:22 -06003083 crc_value = get_crc_value(&nes_quad);
3084 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003085 nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3086 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3087
3088 nesqp->hte_index &= nesadapter->hte_index_mask;
3089 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3090
3091 nesqp->ietf_frame = &cm_node->mpa_frame;
3092 nesqp->private_data_len = (u8) cm_node->mpa_frame_size;
3093 cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3094
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003095 /* notify OF layer we successfully created the requested connection */
3096 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3097 cm_event.status = IW_CM_EVENT_STATUS_ACCEPTED;
3098 cm_event.provider_data = cm_id->provider_data;
3099 cm_event.local_addr.sin_family = AF_INET;
3100 cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3101 cm_event.remote_addr = cm_id->remote_addr;
3102
Faisal Latif6492cdf2008-07-24 20:50:45 -07003103 cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3104 cm_event.private_data_len = (u8) event->cm_node->mpa_frame_size;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003105
3106 cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3107 ret = cm_id->event_handler(cm_id, &cm_event);
3108 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3109
3110 if (ret)
Faisal Latif6492cdf2008-07-24 20:50:45 -07003111 printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3112 "ret=%d\n", __func__, __LINE__, ret);
3113 attr.qp_state = IB_QPS_RTS;
3114 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003115
Faisal Latif6492cdf2008-07-24 20:50:45 -07003116 nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3117 "%lu\n", nesqp->hwqp.qp_id, jiffies);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003118
3119 return;
3120}
3121
3122
3123/**
3124 * cm_event_connect_error
3125 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003126static void cm_event_connect_error(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003127{
3128 struct nes_qp *nesqp;
3129 struct iw_cm_id *cm_id;
3130 struct iw_cm_event cm_event;
3131 /* struct nes_cm_info cm_info; */
3132 int ret;
3133
3134 if (!event->cm_node)
3135 return;
3136
3137 cm_id = event->cm_node->cm_id;
3138 if (!cm_id) {
3139 return;
3140 }
3141
3142 nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3143 nesqp = cm_id->provider_data;
3144
3145 if (!nesqp) {
3146 return;
3147 }
3148
3149 /* notify OF layer about this connection error event */
3150 /* cm_id->rem_ref(cm_id); */
3151 nesqp->cm_id = NULL;
3152 cm_id->provider_data = NULL;
3153 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3154 cm_event.status = IW_CM_EVENT_STATUS_REJECTED;
3155 cm_event.provider_data = cm_id->provider_data;
3156 cm_event.local_addr = cm_id->local_addr;
3157 cm_event.remote_addr = cm_id->remote_addr;
3158 cm_event.private_data = NULL;
3159 cm_event.private_data_len = 0;
3160
Faisal Latif6492cdf2008-07-24 20:50:45 -07003161 nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3162 "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3163 cm_event.remote_addr.sin_addr.s_addr);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003164
3165 ret = cm_id->event_handler(cm_id, &cm_event);
3166 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
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);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003170 nes_rem_ref(&nesqp->ibqp);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003171 cm_id->rem_ref(cm_id);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003172
Faisal Latif6492cdf2008-07-24 20:50:45 -07003173 rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003174 return;
3175}
3176
3177
3178/**
3179 * cm_event_reset
3180 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003181static void cm_event_reset(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003182{
3183 struct nes_qp *nesqp;
3184 struct iw_cm_id *cm_id;
3185 struct iw_cm_event cm_event;
3186 /* struct nes_cm_info cm_info; */
3187 int ret;
3188
3189 if (!event->cm_node)
3190 return;
3191
3192 if (!event->cm_node->cm_id)
3193 return;
3194
3195 cm_id = event->cm_node->cm_id;
3196
3197 nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3198 nesqp = cm_id->provider_data;
3199
3200 nesqp->cm_id = NULL;
3201 /* cm_id->provider_data = NULL; */
3202 cm_event.event = IW_CM_EVENT_DISCONNECT;
3203 cm_event.status = IW_CM_EVENT_STATUS_RESET;
3204 cm_event.provider_data = cm_id->provider_data;
3205 cm_event.local_addr = cm_id->local_addr;
3206 cm_event.remote_addr = cm_id->remote_addr;
3207 cm_event.private_data = NULL;
3208 cm_event.private_data_len = 0;
3209
3210 ret = cm_id->event_handler(cm_id, &cm_event);
3211 nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3212
3213
3214 /* notify OF layer about this connection error event */
3215 cm_id->rem_ref(cm_id);
3216
3217 return;
3218}
3219
3220
3221/**
3222 * cm_event_mpa_req
3223 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003224static void cm_event_mpa_req(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003225{
3226 struct iw_cm_id *cm_id;
3227 struct iw_cm_event cm_event;
3228 int ret;
3229 struct nes_cm_node *cm_node;
3230
3231 cm_node = event->cm_node;
3232 if (!cm_node)
3233 return;
3234 cm_id = cm_node->cm_id;
3235
3236 atomic_inc(&cm_connect_reqs);
3237 nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3238 cm_node, cm_id, jiffies);
3239
3240 cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3241 cm_event.status = IW_CM_EVENT_STATUS_OK;
3242 cm_event.provider_data = (void *)cm_node;
3243
3244 cm_event.local_addr.sin_family = AF_INET;
3245 cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3246 cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3247
3248 cm_event.remote_addr.sin_family = AF_INET;
3249 cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3250 cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3251
3252 cm_event.private_data = cm_node->mpa_frame_buf;
3253 cm_event.private_data_len = (u8) cm_node->mpa_frame_size;
3254
3255 ret = cm_id->event_handler(cm_id, &cm_event);
3256 if (ret)
3257 printk("%s[%u] OFA CM event_handler returned, ret=%d\n",
Harvey Harrison33718362008-04-16 21:01:10 -07003258 __func__, __LINE__, ret);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003259
3260 return;
3261}
3262
3263
3264static void nes_cm_event_handler(struct work_struct *);
3265
3266/**
3267 * nes_cm_post_event
3268 * post an event to the cm event handler
3269 */
Roland Dreier1a855fb2008-04-16 21:01:09 -07003270static int nes_cm_post_event(struct nes_cm_event *event)
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003271{
3272 atomic_inc(&event->cm_node->cm_core->events_posted);
3273 add_ref_cm_node(event->cm_node);
3274 event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3275 INIT_WORK(&event->event_work, nes_cm_event_handler);
Faisal Latif6492cdf2008-07-24 20:50:45 -07003276 nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3277 event->cm_node, event);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003278
3279 queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3280
3281 nes_debug(NES_DBG_CM, "Exit\n");
3282 return 0;
3283}
3284
3285
3286/**
3287 * nes_cm_event_handler
3288 * worker function to handle cm events
3289 * will free instance of nes_cm_event
3290 */
3291static void nes_cm_event_handler(struct work_struct *work)
3292{
Faisal Latif6492cdf2008-07-24 20:50:45 -07003293 struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3294 event_work);
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003295 struct nes_cm_core *cm_core;
3296
Faisal Latif6492cdf2008-07-24 20:50:45 -07003297 if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003298 return;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003299
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003300 cm_core = event->cm_node->cm_core;
3301 nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
Faisal Latif6492cdf2008-07-24 20:50:45 -07003302 event, event->type, atomic_read(&cm_core->events_posted));
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003303
3304 switch (event->type) {
Faisal Latif6492cdf2008-07-24 20:50:45 -07003305 case NES_CM_EVENT_MPA_REQ:
3306 cm_event_mpa_req(event);
3307 nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3308 event->cm_node);
3309 break;
3310 case NES_CM_EVENT_RESET:
3311 nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3312 event->cm_node);
3313 cm_event_reset(event);
3314 break;
3315 case NES_CM_EVENT_CONNECTED:
3316 if ((!event->cm_node->cm_id) ||
3317 (event->cm_node->state != NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003318 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003319 cm_event_connected(event);
3320 nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3321 break;
3322 case NES_CM_EVENT_ABORTED:
3323 if ((!event->cm_node->cm_id) ||
3324 (event->cm_node->state == NES_CM_STATE_TSA))
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003325 break;
Faisal Latif6492cdf2008-07-24 20:50:45 -07003326 cm_event_connect_error(event);
3327 nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3328 break;
3329 case NES_CM_EVENT_DROPPED_PKT:
3330 nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3331 break;
3332 default:
3333 nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3334 break;
Glenn Streiff3c2d7742008-02-04 20:20:45 -08003335 }
3336
3337 atomic_dec(&cm_core->events_posted);
3338 event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3339 rem_ref_cm_node(cm_core, event->cm_node);
3340 kfree(event);
3341
3342 return;
3343}