Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
nes_cm.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006 - 2011 Intel Corporation. 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 <linux/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>
43 #include <linux/if_vlan.h>
44 #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 #include <linux/highmem.h>
56 #include <linux/slab.h>
57 #include <net/arp.h>
58 #include <net/neighbour.h>
59 #include <net/route.h>
60 #include <net/ip_fib.h>
61 #include <net/tcp.h>
62 
63 #include "nes.h"
64 
79 
80 static inline int mini_cm_accelerated(struct nes_cm_core *, struct nes_cm_node *);
81 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *, struct nes_vnic *, struct nes_cm_info *);
82 static int mini_cm_del_listen(struct nes_cm_core *, struct nes_cm_listener *);
83 static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *, struct nes_vnic *, u16, void *, struct nes_cm_info *);
84 static int mini_cm_close(struct nes_cm_core *, struct nes_cm_node *);
85 static int mini_cm_accept(struct nes_cm_core *, struct nes_cm_node *);
86 static int mini_cm_reject(struct nes_cm_core *, struct nes_cm_node *);
87 static int mini_cm_recv_pkt(struct nes_cm_core *, struct nes_vnic *, struct sk_buff *);
88 static int mini_cm_dealloc_core(struct nes_cm_core *);
89 static int mini_cm_get(struct nes_cm_core *);
90 static int mini_cm_set(struct nes_cm_core *, u32, u32);
91 
92 static void form_cm_frame(struct sk_buff *, struct nes_cm_node *, void *, u32, void *, u32, u8);
93 static int add_ref_cm_node(struct nes_cm_node *);
94 static int rem_ref_cm_node(struct nes_cm_core *, struct nes_cm_node *);
95 
96 static int nes_cm_disconn_true(struct nes_qp *);
97 static int nes_cm_post_event(struct nes_cm_event *event);
98 static int nes_disconnect(struct nes_qp *nesqp, int abrupt);
99 static void nes_disconnect_worker(struct work_struct *work);
100 
101 static int send_mpa_request(struct nes_cm_node *, struct sk_buff *);
102 static int send_mpa_reject(struct nes_cm_node *);
103 static int send_syn(struct nes_cm_node *, u32, struct sk_buff *);
104 static int send_reset(struct nes_cm_node *, struct sk_buff *);
105 static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb);
106 static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb);
107 static void process_packet(struct nes_cm_node *, struct sk_buff *, struct nes_cm_core *);
108 
109 static void active_open_err(struct nes_cm_node *, struct sk_buff *, int);
110 static void passive_open_err(struct nes_cm_node *, struct sk_buff *, int);
111 static void cleanup_retrans_entry(struct nes_cm_node *);
112 static void handle_rcv_mpa(struct nes_cm_node *, struct sk_buff *);
113 static void free_retrans_entry(struct nes_cm_node *cm_node);
114 static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph, struct sk_buff *skb, int optionsize, int passive);
115 
116 /* CM event handler functions */
117 static void cm_event_connected(struct nes_cm_event *);
118 static void cm_event_connect_error(struct nes_cm_event *);
119 static void cm_event_reset(struct nes_cm_event *);
120 static void cm_event_mpa_req(struct nes_cm_event *);
121 static void cm_event_mpa_reject(struct nes_cm_event *);
122 static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node);
123 
124 /* MPA build functions */
125 static int cm_build_mpa_frame(struct nes_cm_node *, u8 **, u16 *, u8 *, u8);
126 static void build_mpa_v2(struct nes_cm_node *, void *, u8);
127 static void build_mpa_v1(struct nes_cm_node *, void *, u8);
128 static void build_rdma0_msg(struct nes_cm_node *, struct nes_qp **);
129 
130 static void print_core(struct nes_cm_core *core);
131 
132 /* External CM API Interface */
133 /* instance of function pointers for client API */
134 /* set address of this instance to cm_core->cm_ops at cm_core alloc */
135 static struct nes_cm_ops nes_cm_api = {
136  mini_cm_accelerated,
137  mini_cm_listen,
138  mini_cm_del_listen,
139  mini_cm_connect,
140  mini_cm_close,
141  mini_cm_accept,
142  mini_cm_reject,
143  mini_cm_recv_pkt,
144  mini_cm_dealloc_core,
145  mini_cm_get,
146  mini_cm_set
147 };
148 
149 static struct nes_cm_core *g_cm_core;
150 
158 
159 int nes_add_ref_cm_node(struct nes_cm_node *cm_node)
160 {
161  return add_ref_cm_node(cm_node);
162 }
163 
164 int nes_rem_ref_cm_node(struct nes_cm_node *cm_node)
165 {
166  return rem_ref_cm_node(cm_node->cm_core, cm_node);
167 }
168 
172 static struct nes_cm_event *create_event(struct nes_cm_node * cm_node,
173  enum nes_cm_event_type type)
174 {
175  struct nes_cm_event *event;
176 
177  if (!cm_node->cm_id)
178  return NULL;
179 
180  /* allocate an empty event */
181  event = kzalloc(sizeof(*event), GFP_ATOMIC);
182 
183  if (!event)
184  return NULL;
185 
186  event->type = type;
187  event->cm_node = cm_node;
188  event->cm_info.rem_addr = cm_node->rem_addr;
189  event->cm_info.loc_addr = cm_node->loc_addr;
190  event->cm_info.rem_port = cm_node->rem_port;
191  event->cm_info.loc_port = cm_node->loc_port;
192  event->cm_info.cm_id = cm_node->cm_id;
193 
194  nes_debug(NES_DBG_CM, "cm_node=%p Created event=%p, type=%u, "
195  "dst_addr=%08x[%x], src_addr=%08x[%x]\n",
196  cm_node, event, type, event->cm_info.loc_addr,
197  event->cm_info.loc_port, event->cm_info.rem_addr,
198  event->cm_info.rem_port);
199 
200  nes_cm_post_event(event);
201  return event;
202 }
203 
204 
208 static int send_mpa_request(struct nes_cm_node *cm_node, struct sk_buff *skb)
209 {
210  u8 start_addr = 0;
211  u8 *start_ptr = &start_addr;
212  u8 **start_buff = &start_ptr;
213  u16 buff_len = 0;
214 
215  if (!skb) {
216  nes_debug(NES_DBG_CM, "skb set to NULL\n");
217  return -1;
218  }
219 
220  /* send an MPA Request frame */
221  cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REQUEST);
222  form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK);
223 
224  return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
225 }
226 
227 
228 
229 static int send_mpa_reject(struct nes_cm_node *cm_node)
230 {
231  struct sk_buff *skb = NULL;
232  u8 start_addr = 0;
233  u8 *start_ptr = &start_addr;
234  u8 **start_buff = &start_ptr;
235  u16 buff_len = 0;
236  struct ietf_mpa_v1 *mpa_frame;
237 
238  skb = dev_alloc_skb(MAX_CM_BUFFER);
239  if (!skb) {
240  nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
241  return -ENOMEM;
242  }
243 
244  /* send an MPA reject frame */
245  cm_build_mpa_frame(cm_node, start_buff, &buff_len, NULL, MPA_KEY_REPLY);
246  mpa_frame = (struct ietf_mpa_v1 *)*start_buff;
247  mpa_frame->flags |= IETF_MPA_FLAGS_REJECT;
248  form_cm_frame(skb, cm_node, NULL, 0, *start_buff, buff_len, SET_ACK | SET_FIN);
249 
250  cm_node->state = NES_CM_STATE_FIN_WAIT1;
251  return schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
252 }
253 
254 
259 static int parse_mpa(struct nes_cm_node *cm_node, u8 *buffer, u32 *type,
260  u32 len)
261 {
262  struct ietf_mpa_v1 *mpa_frame;
263  struct ietf_mpa_v2 *mpa_v2_frame;
264  struct ietf_rtr_msg *rtr_msg;
265  int mpa_hdr_len;
266  int priv_data_len;
267 
268  *type = NES_MPA_REQUEST_ACCEPT;
269 
270  /* assume req frame is in tcp data payload */
271  if (len < sizeof(struct ietf_mpa_v1)) {
272  nes_debug(NES_DBG_CM, "The received ietf buffer was too small (%x)\n", len);
273  return -EINVAL;
274  }
275 
276  /* points to the beginning of the frame, which could be MPA V1 or V2 */
277  mpa_frame = (struct ietf_mpa_v1 *)buffer;
278  mpa_hdr_len = sizeof(struct ietf_mpa_v1);
279  priv_data_len = ntohs(mpa_frame->priv_data_len);
280 
281  /* make sure mpa private data len is less than 512 bytes */
282  if (priv_data_len > IETF_MAX_PRIV_DATA_LEN) {
283  nes_debug(NES_DBG_CM, "The received Length of Private"
284  " Data field exceeds 512 octets\n");
285  return -EINVAL;
286  }
287  /*
288  * make sure MPA receiver interoperate with the
289  * received MPA version and MPA key information
290  *
291  */
292  if (mpa_frame->rev != IETF_MPA_V1 && mpa_frame->rev != IETF_MPA_V2) {
293  nes_debug(NES_DBG_CM, "The received mpa version"
294  " is not supported\n");
295  return -EINVAL;
296  }
297  /*
298  * backwards compatibility only
299  */
300  if (mpa_frame->rev > cm_node->mpa_frame_rev) {
301  nes_debug(NES_DBG_CM, "The received mpa version"
302  " can not be interoperated\n");
303  return -EINVAL;
304  } else {
305  cm_node->mpa_frame_rev = mpa_frame->rev;
306  }
307 
308  if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
309  if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REQ, IETF_MPA_KEY_SIZE)) {
310  nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
311  return -EINVAL;
312  }
313  } else {
314  if (memcmp(mpa_frame->key, IEFT_MPA_KEY_REP, IETF_MPA_KEY_SIZE)) {
315  nes_debug(NES_DBG_CM, "Unexpected MPA Key received \n");
316  return -EINVAL;
317  }
318  }
319 
320 
321  if (priv_data_len + mpa_hdr_len != len) {
322  nes_debug(NES_DBG_CM, "The received ietf buffer was not right"
323  " complete (%x + %x != %x)\n",
324  priv_data_len, mpa_hdr_len, len);
325  return -EINVAL;
326  }
327  /* make sure it does not exceed the max size */
328  if (len > MAX_CM_BUFFER) {
329  nes_debug(NES_DBG_CM, "The received ietf buffer was too large"
330  " (%x + %x != %x)\n",
331  priv_data_len, mpa_hdr_len, len);
332  return -EINVAL;
333  }
334 
335  cm_node->mpa_frame_size = priv_data_len;
336 
337  switch (mpa_frame->rev) {
338  case IETF_MPA_V2: {
339  u16 ird_size;
340  u16 ord_size;
341  u16 rtr_ctrl_ird;
342  u16 rtr_ctrl_ord;
343 
344  mpa_v2_frame = (struct ietf_mpa_v2 *)buffer;
345  mpa_hdr_len += IETF_RTR_MSG_SIZE;
346  cm_node->mpa_frame_size -= IETF_RTR_MSG_SIZE;
347  rtr_msg = &mpa_v2_frame->rtr_msg;
348 
349  /* parse rtr message */
350  rtr_ctrl_ird = ntohs(rtr_msg->ctrl_ird);
351  rtr_ctrl_ord = ntohs(rtr_msg->ctrl_ord);
352  ird_size = rtr_ctrl_ird & IETF_NO_IRD_ORD;
353  ord_size = rtr_ctrl_ord & IETF_NO_IRD_ORD;
354 
355  if (!(rtr_ctrl_ird & IETF_PEER_TO_PEER)) {
356  /* send reset */
357  return -EINVAL;
358  }
359 
360  if (cm_node->state != NES_CM_STATE_MPAREQ_SENT) {
361  /* responder */
362  if (cm_node->ord_size > ird_size)
363  cm_node->ord_size = ird_size;
364  } else {
365  /* initiator */
366  if (cm_node->ord_size > ird_size)
367  cm_node->ord_size = ird_size;
368 
369  if (cm_node->ird_size < ord_size) {
370  /* no resources available */
371  /* send terminate message */
372  return -EINVAL;
373  }
374  }
375 
376  if (rtr_ctrl_ord & IETF_RDMA0_READ) {
378  } else if (rtr_ctrl_ord & IETF_RDMA0_WRITE) {
380  } else { /* Not supported RDMA0 operation */
381  return -EINVAL;
382  }
383  break;
384  }
385  case IETF_MPA_V1:
386  default:
387  break;
388  }
389 
390  /* copy entire MPA frame to our cm_node's frame */
391  memcpy(cm_node->mpa_frame_buf, buffer + mpa_hdr_len, cm_node->mpa_frame_size);
392 
393  if (mpa_frame->flags & IETF_MPA_FLAGS_REJECT)
394  *type = NES_MPA_REQUEST_REJECT;
395  return 0;
396 }
397 
398 
403 static void form_cm_frame(struct sk_buff *skb,
404  struct nes_cm_node *cm_node, void *options, u32 optionsize,
405  void *data, u32 datasize, u8 flags)
406 {
407  struct tcphdr *tcph;
408  struct iphdr *iph;
409  struct ethhdr *ethh;
410  u8 *buf;
411  u16 packetsize = sizeof(*iph);
412 
413  packetsize += sizeof(*tcph);
414  packetsize += optionsize + datasize;
415 
416  skb_trim(skb, 0);
417  memset(skb->data, 0x00, ETH_HLEN + sizeof(*iph) + sizeof(*tcph));
418 
419  buf = skb_put(skb, packetsize + ETH_HLEN);
420 
421  ethh = (struct ethhdr *)buf;
422  buf += ETH_HLEN;
423 
424  iph = (struct iphdr *)buf;
425  buf += sizeof(*iph);
426  tcph = (struct tcphdr *)buf;
427  skb_reset_mac_header(skb);
428  skb_set_network_header(skb, ETH_HLEN);
429  skb_set_transport_header(skb, ETH_HLEN + sizeof(*iph));
430  buf += sizeof(*tcph);
431 
433  if (!(cm_node->netdev->features & NETIF_F_IP_CSUM))
434  skb->ip_summed = CHECKSUM_NONE;
435  skb->protocol = htons(0x800);
436  skb->data_len = 0;
437  skb->mac_len = ETH_HLEN;
438 
439  memcpy(ethh->h_dest, cm_node->rem_mac, ETH_ALEN);
440  memcpy(ethh->h_source, cm_node->loc_mac, ETH_ALEN);
441  ethh->h_proto = htons(0x0800);
442 
443  iph->version = IPVERSION;
444  iph->ihl = 5; /* 5 * 4Byte words, IP headr len */
445  iph->tos = 0;
446  iph->tot_len = htons(packetsize);
447  iph->id = htons(++cm_node->tcp_cntxt.loc_id);
448 
449  iph->frag_off = htons(0x4000);
450  iph->ttl = 0x40;
451  iph->protocol = 0x06; /* IPPROTO_TCP */
452 
453  iph->saddr = htonl(cm_node->loc_addr);
454  iph->daddr = htonl(cm_node->rem_addr);
455 
456  tcph->source = htons(cm_node->loc_port);
457  tcph->dest = htons(cm_node->rem_port);
458  tcph->seq = htonl(cm_node->tcp_cntxt.loc_seq_num);
459 
460  if (flags & SET_ACK) {
461  cm_node->tcp_cntxt.loc_ack_num = cm_node->tcp_cntxt.rcv_nxt;
462  tcph->ack_seq = htonl(cm_node->tcp_cntxt.loc_ack_num);
463  tcph->ack = 1;
464  } else {
465  tcph->ack_seq = 0;
466  }
467 
468  if (flags & SET_SYN) {
469  cm_node->tcp_cntxt.loc_seq_num++;
470  tcph->syn = 1;
471  } else {
472  cm_node->tcp_cntxt.loc_seq_num += datasize;
473  }
474 
475  if (flags & SET_FIN) {
476  cm_node->tcp_cntxt.loc_seq_num++;
477  tcph->fin = 1;
478  }
479 
480  if (flags & SET_RST)
481  tcph->rst = 1;
482 
483  tcph->doff = (u16)((sizeof(*tcph) + optionsize + 3) >> 2);
484  tcph->window = htons(cm_node->tcp_cntxt.rcv_wnd);
485  tcph->urg_ptr = 0;
486  if (optionsize)
487  memcpy(buf, options, optionsize);
488  buf += optionsize;
489  if (datasize)
490  memcpy(buf, data, datasize);
491 
492  skb_shinfo(skb)->nr_frags = 0;
494 }
495 
499 static void print_core(struct nes_cm_core *core)
500 {
501  nes_debug(NES_DBG_CM, "---------------------------------------------\n");
502  nes_debug(NES_DBG_CM, "CM Core -- (core = %p )\n", core);
503  if (!core)
504  return;
505  nes_debug(NES_DBG_CM, "---------------------------------------------\n");
506 
507  nes_debug(NES_DBG_CM, "State : %u \n", core->state);
508 
509  nes_debug(NES_DBG_CM, "Listen Nodes : %u \n", atomic_read(&core->listen_node_cnt));
510  nes_debug(NES_DBG_CM, "Active Nodes : %u \n", atomic_read(&core->node_cnt));
511 
512  nes_debug(NES_DBG_CM, "core : %p \n", core);
513 
514  nes_debug(NES_DBG_CM, "-------------- end core ---------------\n");
515 }
516 
520 static int cm_build_mpa_frame(struct nes_cm_node *cm_node, u8 **start_buff,
521  u16 *buff_len, u8 *pci_mem, u8 mpa_key)
522 {
523  int ret = 0;
524 
525  *start_buff = (pci_mem) ? pci_mem : &cm_node->mpa_frame_buf[0];
526 
527  switch (cm_node->mpa_frame_rev) {
528  case IETF_MPA_V1:
529  *start_buff = (u8 *)*start_buff + sizeof(struct ietf_rtr_msg);
530  *buff_len = sizeof(struct ietf_mpa_v1) + cm_node->mpa_frame_size;
531  build_mpa_v1(cm_node, *start_buff, mpa_key);
532  break;
533  case IETF_MPA_V2:
534  *buff_len = sizeof(struct ietf_mpa_v2) + cm_node->mpa_frame_size;
535  build_mpa_v2(cm_node, *start_buff, mpa_key);
536  break;
537  default:
538  ret = -EINVAL;
539  }
540  return ret;
541 }
542 
546 static void build_mpa_v2(struct nes_cm_node *cm_node,
547  void *start_addr, u8 mpa_key)
548 {
549  struct ietf_mpa_v2 *mpa_frame = (struct ietf_mpa_v2 *)start_addr;
550  struct ietf_rtr_msg *rtr_msg = &mpa_frame->rtr_msg;
551  u16 ctrl_ird;
552  u16 ctrl_ord;
553 
554  /* initialize the upper 5 bytes of the frame */
555  build_mpa_v1(cm_node, start_addr, mpa_key);
556  mpa_frame->flags |= IETF_MPA_V2_FLAG; /* set a bit to indicate MPA V2 */
557  mpa_frame->priv_data_len += htons(IETF_RTR_MSG_SIZE);
558 
559  /* initialize RTR msg */
560  ctrl_ird = (cm_node->ird_size > IETF_NO_IRD_ORD) ?
561  IETF_NO_IRD_ORD : cm_node->ird_size;
562  ctrl_ord = (cm_node->ord_size > IETF_NO_IRD_ORD) ?
563  IETF_NO_IRD_ORD : cm_node->ord_size;
564 
565  ctrl_ird |= IETF_PEER_TO_PEER;
566  ctrl_ird |= IETF_FLPDU_ZERO_LEN;
567 
568  switch (mpa_key) {
569  case MPA_KEY_REQUEST:
570  ctrl_ord |= IETF_RDMA0_WRITE;
571  ctrl_ord |= IETF_RDMA0_READ;
572  break;
573  case MPA_KEY_REPLY:
574  switch (cm_node->send_rdma0_op) {
576  ctrl_ord |= IETF_RDMA0_WRITE;
577  break;
578  case SEND_RDMA_READ_ZERO:
579  ctrl_ord |= IETF_RDMA0_READ;
580  break;
581  }
582  }
583  rtr_msg->ctrl_ird = htons(ctrl_ird);
584  rtr_msg->ctrl_ord = htons(ctrl_ord);
585 }
586 
590 static void build_mpa_v1(struct nes_cm_node *cm_node, void *start_addr, u8 mpa_key)
591 {
592  struct ietf_mpa_v1 *mpa_frame = (struct ietf_mpa_v1 *)start_addr;
593 
594  switch (mpa_key) {
595  case MPA_KEY_REQUEST:
597  break;
598  case MPA_KEY_REPLY:
600  break;
601  }
602  mpa_frame->flags = IETF_MPA_FLAGS_CRC;
603  mpa_frame->rev = cm_node->mpa_frame_rev;
604  mpa_frame->priv_data_len = htons(cm_node->mpa_frame_size);
605 }
606 
607 static void build_rdma0_msg(struct nes_cm_node *cm_node, struct nes_qp **nesqp_addr)
608 {
609  u64 u64temp;
610  struct nes_qp *nesqp = *nesqp_addr;
611  struct nes_hw_qp_wqe *wqe = &nesqp->hwqp.sq_vbase[0];
612 
613  u64temp = (unsigned long)nesqp;
614  u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
615  set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_CTX_LOW_IDX, u64temp);
616 
619 
620  switch (cm_node->send_rdma0_op) {
622  nes_debug(NES_DBG_CM, "Sending first write.\n");
628  break;
629 
630  case SEND_RDMA_READ_ZERO:
631  default:
632  if (cm_node->send_rdma0_op != SEND_RDMA_READ_ZERO) {
633  printk(KERN_ERR "%s[%u]: Unsupported RDMA0 len operation=%u\n",
634  __func__, __LINE__, cm_node->send_rdma0_op);
635  WARN_ON(1);
636  }
637  nes_debug(NES_DBG_CM, "Sending first rdma operation.\n");
645  break;
646  }
647 
648  if (nesqp->sq_kmapped) {
649  nesqp->sq_kmapped = 0;
650  kunmap(nesqp->page);
651  }
652 
653  /*use the reserved spot on the WQ for the extra first WQE*/
654  nesqp->nesqp_context->ird_ord_sizes &= cpu_to_le32(~(NES_QPCONTEXT_ORDIRD_LSMM_PRESENT |
657  nesqp->skip_lsmm = 1;
658  nesqp->hwqp.sq_tail = 0;
659 }
660 
666 int schedule_nes_timer(struct nes_cm_node *cm_node, struct sk_buff *skb,
667  enum nes_timer_type type, int send_retrans,
668  int close_when_complete)
669 {
670  unsigned long flags;
671  struct nes_cm_core *cm_core = cm_node->cm_core;
672  struct nes_timer_entry *new_send;
673  int ret = 0;
674  u32 was_timer_set;
675 
676  new_send = kzalloc(sizeof(*new_send), GFP_ATOMIC);
677  if (!new_send)
678  return -ENOMEM;
679 
680  /* new_send->timetosend = currenttime */
681  new_send->retrycount = NES_DEFAULT_RETRYS;
682  new_send->retranscount = NES_DEFAULT_RETRANS;
683  new_send->skb = skb;
684  new_send->timetosend = jiffies;
685  new_send->type = type;
686  new_send->netdev = cm_node->netdev;
687  new_send->send_retrans = send_retrans;
689 
690  if (type == NES_TIMER_TYPE_CLOSE) {
691  new_send->timetosend += (HZ / 10);
692  if (cm_node->recv_entry) {
693  kfree(new_send);
694  WARN_ON(1);
695  return -EINVAL;
696  }
697  cm_node->recv_entry = new_send;
698  }
699 
700  if (type == NES_TIMER_TYPE_SEND) {
701  new_send->seq_num = ntohl(tcp_hdr(skb)->seq);
702  atomic_inc(&new_send->skb->users);
703  spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
704  cm_node->send_entry = new_send;
705  add_ref_cm_node(cm_node);
706  spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
707  new_send->timetosend = jiffies + NES_RETRY_TIMEOUT;
708 
709  ret = nes_nic_cm_xmit(new_send->skb, cm_node->netdev);
710  if (ret != NETDEV_TX_OK) {
711  nes_debug(NES_DBG_CM, "Error sending packet %p "
712  "(jiffies = %lu)\n", new_send, jiffies);
713  new_send->timetosend = jiffies;
714  ret = NETDEV_TX_OK;
715  } else {
716  cm_packets_sent++;
717  if (!send_retrans) {
718  cleanup_retrans_entry(cm_node);
719  if (close_when_complete)
720  rem_ref_cm_node(cm_core, cm_node);
721  return ret;
722  }
723  }
724  }
725 
726  was_timer_set = timer_pending(&cm_core->tcp_timer);
727 
728  if (!was_timer_set) {
729  cm_core->tcp_timer.expires = new_send->timetosend;
730  add_timer(&cm_core->tcp_timer);
731  }
732 
733  return ret;
734 }
735 
736 static void nes_retrans_expired(struct nes_cm_node *cm_node)
737 {
738  struct iw_cm_id *cm_id = cm_node->cm_id;
739  enum nes_cm_node_state state = cm_node->state;
740  cm_node->state = NES_CM_STATE_CLOSED;
741 
742  switch (state) {
745  rem_ref_cm_node(cm_node->cm_core, cm_node);
746  break;
749  if (cm_node->cm_id)
750  cm_id->rem_ref(cm_id);
751  send_reset(cm_node, NULL);
752  break;
753  default:
754  add_ref_cm_node(cm_node);
755  send_reset(cm_node, NULL);
756  create_event(cm_node, NES_CM_EVENT_ABORTED);
757  }
758 }
759 
760 static void handle_recv_entry(struct nes_cm_node *cm_node, u32 rem_node)
761 {
762  struct nes_timer_entry *recv_entry = cm_node->recv_entry;
763  struct iw_cm_id *cm_id = cm_node->cm_id;
764  struct nes_qp *nesqp;
765  unsigned long qplockflags;
766 
767  if (!recv_entry)
768  return;
769  nesqp = (struct nes_qp *)recv_entry->skb;
770  if (nesqp) {
771  spin_lock_irqsave(&nesqp->lock, qplockflags);
772  if (nesqp->cm_id) {
773  nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
774  "refcount = %d: HIT A "
775  "NES_TIMER_TYPE_CLOSE with something "
776  "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
777  atomic_read(&nesqp->refcount));
780  nesqp->ibqp_state = IB_QPS_ERR;
781  spin_unlock_irqrestore(&nesqp->lock, qplockflags);
782  nes_cm_disconn(nesqp);
783  } else {
784  spin_unlock_irqrestore(&nesqp->lock, qplockflags);
785  nes_debug(NES_DBG_CM, "QP%u: cm_id = %p, "
786  "refcount = %d: HIT A "
787  "NES_TIMER_TYPE_CLOSE with nothing "
788  "to do!!!\n", nesqp->hwqp.qp_id, cm_id,
789  atomic_read(&nesqp->refcount));
790  }
791  } else if (rem_node) {
792  /* TIME_WAIT state */
793  rem_ref_cm_node(cm_node->cm_core, cm_node);
794  }
795  if (cm_node->cm_id)
796  cm_id->rem_ref(cm_id);
797  kfree(recv_entry);
798  cm_node->recv_entry = NULL;
799 }
800 
804 static void nes_cm_timer_tick(unsigned long pass)
805 {
806  unsigned long flags;
807  unsigned long nexttimeout = jiffies + NES_LONG_TIME;
808  struct nes_cm_node *cm_node;
809  struct nes_timer_entry *send_entry, *recv_entry;
810  struct list_head *list_core_temp;
811  struct list_head *list_node;
812  struct nes_cm_core *cm_core = g_cm_core;
813  u32 settimer = 0;
814  unsigned long timetosend;
815  int ret = NETDEV_TX_OK;
816 
817  struct list_head timer_list;
818 
819  INIT_LIST_HEAD(&timer_list);
820  spin_lock_irqsave(&cm_core->ht_lock, flags);
821 
822  list_for_each_safe(list_node, list_core_temp,
823  &cm_core->connected_nodes) {
824  cm_node = container_of(list_node, struct nes_cm_node, list);
825  if ((cm_node->recv_entry) || (cm_node->send_entry)) {
826  add_ref_cm_node(cm_node);
827  list_add(&cm_node->timer_entry, &timer_list);
828  }
829  }
830  spin_unlock_irqrestore(&cm_core->ht_lock, flags);
831 
832  list_for_each_safe(list_node, list_core_temp, &timer_list) {
833  cm_node = container_of(list_node, struct nes_cm_node,
834  timer_entry);
835  recv_entry = cm_node->recv_entry;
836 
837  if (recv_entry) {
838  if (time_after(recv_entry->timetosend, jiffies)) {
839  if (nexttimeout > recv_entry->timetosend ||
840  !settimer) {
841  nexttimeout = recv_entry->timetosend;
842  settimer = 1;
843  }
844  } else {
845  handle_recv_entry(cm_node, 1);
846  }
847  }
848 
849  spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
850  do {
851  send_entry = cm_node->send_entry;
852  if (!send_entry)
853  break;
854  if (time_after(send_entry->timetosend, jiffies)) {
855  if (cm_node->state != NES_CM_STATE_TSA) {
856  if ((nexttimeout >
857  send_entry->timetosend) ||
858  !settimer) {
859  nexttimeout =
860  send_entry->timetosend;
861  settimer = 1;
862  }
863  } else {
864  free_retrans_entry(cm_node);
865  }
866  break;
867  }
868 
869  if ((cm_node->state == NES_CM_STATE_TSA) ||
870  (cm_node->state == NES_CM_STATE_CLOSED)) {
871  free_retrans_entry(cm_node);
872  break;
873  }
874 
875  if (!send_entry->retranscount ||
876  !send_entry->retrycount) {
878  free_retrans_entry(cm_node);
879 
880  spin_unlock_irqrestore(
881  &cm_node->retrans_list_lock, flags);
882  nes_retrans_expired(cm_node);
883  cm_node->state = NES_CM_STATE_CLOSED;
885  flags);
886  break;
887  }
888  atomic_inc(&send_entry->skb->users);
890  nes_debug(NES_DBG_CM, "Retransmitting send_entry %p "
891  "for node %p, jiffies = %lu, time to send = "
892  "%lu, retranscount = %u, send_entry->seq_num = "
893  "0x%08X, cm_node->tcp_cntxt.rem_ack_num = "
894  "0x%08X\n", send_entry, cm_node, jiffies,
895  send_entry->timetosend,
896  send_entry->retranscount,
897  send_entry->seq_num,
898  cm_node->tcp_cntxt.rem_ack_num);
899 
900  spin_unlock_irqrestore(&cm_node->retrans_list_lock,
901  flags);
902  ret = nes_nic_cm_xmit(send_entry->skb, cm_node->netdev);
903  spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
904  if (ret != NETDEV_TX_OK) {
905  nes_debug(NES_DBG_CM, "rexmit failed for "
906  "node=%p\n", cm_node);
908  send_entry->retrycount--;
909  nexttimeout = jiffies + NES_SHORT_TIME;
910  settimer = 1;
911  break;
912  } else {
913  cm_packets_sent++;
914  }
915  nes_debug(NES_DBG_CM, "Packet Sent: retrans count = "
916  "%u, retry count = %u.\n",
917  send_entry->retranscount,
918  send_entry->retrycount);
919  if (send_entry->send_retrans) {
920  send_entry->retranscount--;
921  timetosend = (NES_RETRY_TIMEOUT <<
922  (NES_DEFAULT_RETRANS - send_entry->retranscount));
923 
924  send_entry->timetosend = jiffies +
925  min(timetosend, NES_MAX_TIMEOUT);
926  if (nexttimeout > send_entry->timetosend ||
927  !settimer) {
928  nexttimeout = send_entry->timetosend;
929  settimer = 1;
930  }
931  } else {
932  int close_when_complete;
933  close_when_complete =
934  send_entry->close_when_complete;
935  nes_debug(NES_DBG_CM, "cm_node=%p state=%d\n",
936  cm_node, cm_node->state);
937  free_retrans_entry(cm_node);
938  if (close_when_complete)
939  rem_ref_cm_node(cm_node->cm_core,
940  cm_node);
941  }
942  } while (0);
943 
944  spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
945  rem_ref_cm_node(cm_node->cm_core, cm_node);
946  }
947 
948  if (settimer) {
949  if (!timer_pending(&cm_core->tcp_timer)) {
950  cm_core->tcp_timer.expires = nexttimeout;
951  add_timer(&cm_core->tcp_timer);
952  }
953  }
954 }
955 
956 
960 static int send_syn(struct nes_cm_node *cm_node, u32 sendack,
961  struct sk_buff *skb)
962 {
963  int ret;
964  int flags = SET_SYN;
965  char optionsbuffer[sizeof(struct option_mss) +
968 
969  int optionssize = 0;
970  /* Sending MSS option */
971  union all_known_options *options;
972 
973  if (!cm_node)
974  return -EINVAL;
975 
976  options = (union all_known_options *)&optionsbuffer[optionssize];
977  options->as_mss.optionnum = OPTION_NUMBER_MSS;
978  options->as_mss.length = sizeof(struct option_mss);
979  options->as_mss.mss = htons(cm_node->tcp_cntxt.mss);
980  optionssize += sizeof(struct option_mss);
981 
982  options = (union all_known_options *)&optionsbuffer[optionssize];
983  options->as_windowscale.optionnum = OPTION_NUMBER_WINDOW_SCALE;
984  options->as_windowscale.length = sizeof(struct option_windowscale);
985  options->as_windowscale.shiftcount = cm_node->tcp_cntxt.rcv_wscale;
986  optionssize += sizeof(struct option_windowscale);
987 
988  if (sendack && !(NES_DRV_OPT_SUPRESS_OPTION_BC & nes_drv_opt)) {
989  options = (union all_known_options *)&optionsbuffer[optionssize];
990  options->as_base.optionnum = OPTION_NUMBER_WRITE0;
991  options->as_base.length = sizeof(struct option_base);
992  optionssize += sizeof(struct option_base);
993  /* we need the size to be a multiple of 4 */
994  options = (union all_known_options *)&optionsbuffer[optionssize];
995  options->as_end = 1;
996  optionssize += 1;
997  options = (union all_known_options *)&optionsbuffer[optionssize];
998  options->as_end = 1;
999  optionssize += 1;
1000  }
1001 
1002  options = (union all_known_options *)&optionsbuffer[optionssize];
1003  options->as_end = OPTION_NUMBER_END;
1004  optionssize += 1;
1005 
1006  if (!skb)
1007  skb = dev_alloc_skb(MAX_CM_BUFFER);
1008  if (!skb) {
1009  nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1010  return -1;
1011  }
1012 
1013  if (sendack)
1014  flags |= SET_ACK;
1015 
1016  form_cm_frame(skb, cm_node, optionsbuffer, optionssize, NULL, 0, flags);
1017  ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1018 
1019  return ret;
1020 }
1021 
1022 
1026 static int send_reset(struct nes_cm_node *cm_node, struct sk_buff *skb)
1027 {
1028  int ret;
1029  int flags = SET_RST | SET_ACK;
1030 
1031  if (!skb)
1032  skb = dev_alloc_skb(MAX_CM_BUFFER);
1033  if (!skb) {
1034  nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1035  return -ENOMEM;
1036  }
1037 
1038  form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, flags);
1039  ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 1);
1040 
1041  return ret;
1042 }
1043 
1044 
1048 static int send_ack(struct nes_cm_node *cm_node, struct sk_buff *skb)
1049 {
1050  int ret;
1051 
1052  if (!skb)
1053  skb = dev_alloc_skb(MAX_CM_BUFFER);
1054 
1055  if (!skb) {
1056  nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1057  return -1;
1058  }
1059 
1060  form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK);
1061  ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 0, 0);
1062 
1063  return ret;
1064 }
1065 
1066 
1070 static int send_fin(struct nes_cm_node *cm_node, struct sk_buff *skb)
1071 {
1072  int ret;
1073 
1074  /* if we didn't get a frame get one */
1075  if (!skb)
1076  skb = dev_alloc_skb(MAX_CM_BUFFER);
1077 
1078  if (!skb) {
1079  nes_debug(NES_DBG_CM, "Failed to get a Free pkt\n");
1080  return -1;
1081  }
1082 
1083  form_cm_frame(skb, cm_node, NULL, 0, NULL, 0, SET_ACK | SET_FIN);
1084  ret = schedule_nes_timer(cm_node, skb, NES_TIMER_TYPE_SEND, 1, 0);
1085 
1086  return ret;
1087 }
1088 
1089 
1093 static struct nes_cm_node *find_node(struct nes_cm_core *cm_core,
1095 {
1096  unsigned long flags;
1097  struct list_head *hte;
1098  struct nes_cm_node *cm_node;
1099 
1100  /* get a handle on the hte */
1101  hte = &cm_core->connected_nodes;
1102 
1103  /* walk list and find cm_node associated with this session ID */
1104  spin_lock_irqsave(&cm_core->ht_lock, flags);
1105  list_for_each_entry(cm_node, hte, list) {
1106  /* compare quad, return node handle if a match */
1107  nes_debug(NES_DBG_CM, "finding node %x:%x =? %x:%x ^ %x:%x =? %x:%x\n",
1108  cm_node->loc_addr, cm_node->loc_port,
1109  loc_addr, loc_port,
1110  cm_node->rem_addr, cm_node->rem_port,
1111  rem_addr, rem_port);
1112  if ((cm_node->loc_addr == loc_addr) && (cm_node->loc_port == loc_port) &&
1113  (cm_node->rem_addr == rem_addr) && (cm_node->rem_port == rem_port)) {
1114  add_ref_cm_node(cm_node);
1115  spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1116  return cm_node;
1117  }
1118  }
1119  spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1120 
1121  /* no owner node */
1122  return NULL;
1123 }
1124 
1125 
1129 static struct nes_cm_listener *find_listener(struct nes_cm_core *cm_core,
1131 {
1132  unsigned long flags;
1133  struct nes_cm_listener *listen_node;
1134 
1135  /* walk list and find cm_node associated with this session ID */
1136  spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1137  list_for_each_entry(listen_node, &cm_core->listen_list.list, list) {
1138  /* compare node pair, return node handle if a match */
1139  if (((listen_node->loc_addr == dst_addr) ||
1140  listen_node->loc_addr == 0x00000000) &&
1141  (listen_node->loc_port == dst_port) &&
1142  (listener_state & listen_node->listener_state)) {
1143  atomic_inc(&listen_node->ref_count);
1144  spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1145  return listen_node;
1146  }
1147  }
1148  spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1149 
1150  /* no listener */
1151  return NULL;
1152 }
1153 
1154 
1158 static int add_hte_node(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
1159 {
1160  unsigned long flags;
1161  struct list_head *hte;
1162 
1163  if (!cm_node || !cm_core)
1164  return -EINVAL;
1165 
1166  nes_debug(NES_DBG_CM, "Adding Node %p to Active Connection HT\n",
1167  cm_node);
1168 
1169  spin_lock_irqsave(&cm_core->ht_lock, flags);
1170 
1171  /* get a handle on the hash table element (list head for this slot) */
1172  hte = &cm_core->connected_nodes;
1173  list_add_tail(&cm_node->list, hte);
1174  atomic_inc(&cm_core->ht_node_cnt);
1175 
1176  spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1177 
1178  return 0;
1179 }
1180 
1181 
1185 static int mini_cm_dec_refcnt_listen(struct nes_cm_core *cm_core,
1186  struct nes_cm_listener *listener, int free_hanging_nodes)
1187 {
1188  int ret = -EINVAL;
1189  int err = 0;
1190  unsigned long flags;
1191  struct list_head *list_pos = NULL;
1192  struct list_head *list_temp = NULL;
1193  struct nes_cm_node *cm_node = NULL;
1194  struct list_head reset_list;
1195 
1196  nes_debug(NES_DBG_CM, "attempting listener= %p free_nodes= %d, "
1197  "refcnt=%d\n", listener, free_hanging_nodes,
1198  atomic_read(&listener->ref_count));
1199  /* free non-accelerated child nodes for this listener */
1200  INIT_LIST_HEAD(&reset_list);
1201  if (free_hanging_nodes) {
1202  spin_lock_irqsave(&cm_core->ht_lock, flags);
1203  list_for_each_safe(list_pos, list_temp,
1204  &g_cm_core->connected_nodes) {
1205  cm_node = container_of(list_pos, struct nes_cm_node,
1206  list);
1207  if ((cm_node->listener == listener) &&
1208  (!cm_node->accelerated)) {
1209  add_ref_cm_node(cm_node);
1210  list_add(&cm_node->reset_entry, &reset_list);
1211  }
1212  }
1213  spin_unlock_irqrestore(&cm_core->ht_lock, flags);
1214  }
1215 
1216  list_for_each_safe(list_pos, list_temp, &reset_list) {
1217  cm_node = container_of(list_pos, struct nes_cm_node,
1218  reset_entry);
1219  {
1220  struct nes_cm_node *loopback = cm_node->loopbackpartner;
1221  enum nes_cm_node_state old_state;
1222  if (NES_CM_STATE_FIN_WAIT1 <= cm_node->state) {
1223  rem_ref_cm_node(cm_node->cm_core, cm_node);
1224  } else {
1225  if (!loopback) {
1226  cleanup_retrans_entry(cm_node);
1227  err = send_reset(cm_node, NULL);
1228  if (err) {
1229  cm_node->state =
1231  WARN_ON(1);
1232  } else {
1233  old_state = cm_node->state;
1235  if (old_state != NES_CM_STATE_MPAREQ_RCVD)
1236  rem_ref_cm_node(
1237  cm_node->cm_core,
1238  cm_node);
1239  }
1240  } else {
1241  struct nes_cm_event event;
1242 
1243  event.cm_node = loopback;
1244  event.cm_info.rem_addr =
1245  loopback->rem_addr;
1246  event.cm_info.loc_addr =
1247  loopback->loc_addr;
1248  event.cm_info.rem_port =
1249  loopback->rem_port;
1250  event.cm_info.loc_port =
1251  loopback->loc_port;
1252  event.cm_info.cm_id = loopback->cm_id;
1253  add_ref_cm_node(loopback);
1254  loopback->state = NES_CM_STATE_CLOSED;
1255  cm_event_connect_error(&event);
1257 
1258  rem_ref_cm_node(cm_node->cm_core,
1259  cm_node);
1260 
1261  }
1262  }
1263  }
1264  }
1265 
1266  spin_lock_irqsave(&cm_core->listen_list_lock, flags);
1267  if (!atomic_dec_return(&listener->ref_count)) {
1268  list_del(&listener->list);
1269 
1270  /* decrement our listen node count */
1271  atomic_dec(&cm_core->listen_node_cnt);
1272 
1273  spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1274 
1275  if (listener->nesvnic)
1276  nes_manage_apbvt(listener->nesvnic, listener->loc_port,
1277  PCI_FUNC(listener->nesvnic->nesdev->pcidev->devfn), NES_MANAGE_APBVT_DEL);
1278 
1279  nes_debug(NES_DBG_CM, "destroying listener (%p)\n", listener);
1280 
1281  kfree(listener);
1282  listener = NULL;
1283  ret = 0;
1284  atomic_inc(&cm_listens_destroyed);
1285  } else {
1286  spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
1287  }
1288  if (listener) {
1289  if (atomic_read(&listener->pend_accepts_cnt) > 0)
1290  nes_debug(NES_DBG_CM, "destroying listener (%p)"
1291  " with non-zero pending accepts=%u\n",
1292  listener, atomic_read(&listener->pend_accepts_cnt));
1293  }
1294 
1295  return ret;
1296 }
1297 
1298 
1302 static int mini_cm_del_listen(struct nes_cm_core *cm_core,
1303  struct nes_cm_listener *listener)
1304 {
1306  listener->cm_id = NULL; /* going to be destroyed pretty soon */
1307  return mini_cm_dec_refcnt_listen(cm_core, listener, 1);
1308 }
1309 
1310 
1314 static inline int mini_cm_accelerated(struct nes_cm_core *cm_core,
1315  struct nes_cm_node *cm_node)
1316 {
1317  u32 was_timer_set;
1318 
1319  cm_node->accelerated = 1;
1320 
1321  if (cm_node->accept_pend) {
1322  BUG_ON(!cm_node->listener);
1323  atomic_dec(&cm_node->listener->pend_accepts_cnt);
1324  cm_node->accept_pend = 0;
1325  BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1326  }
1327 
1328  was_timer_set = timer_pending(&cm_core->tcp_timer);
1329  if (!was_timer_set) {
1330  cm_core->tcp_timer.expires = jiffies + NES_SHORT_TIME;
1331  add_timer(&cm_core->tcp_timer);
1332  }
1333 
1334  return 0;
1335 }
1336 
1337 
1341 static int nes_addr_resolve_neigh(struct nes_vnic *nesvnic, u32 dst_ip, int arpindex)
1342 {
1343  struct rtable *rt;
1344  struct neighbour *neigh;
1345  int rc = arpindex;
1346  struct net_device *netdev;
1347  struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
1348 
1349  rt = ip_route_output(&init_net, htonl(dst_ip), 0, 0, 0);
1350  if (IS_ERR(rt)) {
1351  printk(KERN_ERR "%s: ip_route_output_key failed for 0x%08X\n",
1352  __func__, dst_ip);
1353  return rc;
1354  }
1355 
1356  if (netif_is_bond_slave(nesvnic->netdev))
1357  netdev = nesvnic->netdev->master;
1358  else
1359  netdev = nesvnic->netdev;
1360 
1361  neigh = neigh_lookup(&arp_tbl, &rt->rt_gateway, netdev);
1362 
1363  rcu_read_lock();
1364  if (neigh) {
1365  if (neigh->nud_state & NUD_VALID) {
1366  nes_debug(NES_DBG_CM, "Neighbor MAC address for 0x%08X"
1367  " is %pM, Gateway is 0x%08X \n", dst_ip,
1368  neigh->ha, ntohl(rt->rt_gateway));
1369 
1370  if (arpindex >= 0) {
1371  if (!memcmp(nesadapter->arp_table[arpindex].mac_addr,
1372  neigh->ha, ETH_ALEN)) {
1373  /* Mac address same as in nes_arp_table */
1374  goto out;
1375  }
1376 
1377  nes_manage_arp_cache(nesvnic->netdev,
1378  nesadapter->arp_table[arpindex].mac_addr,
1379  dst_ip, NES_ARP_DELETE);
1380  }
1381 
1382  nes_manage_arp_cache(nesvnic->netdev, neigh->ha,
1383  dst_ip, NES_ARP_ADD);
1384  rc = nes_arp_table(nesvnic->nesdev, dst_ip, NULL,
1385  NES_ARP_RESOLVE);
1386  } else {
1387  neigh_event_send(neigh, NULL);
1388  }
1389  }
1390 out:
1391  rcu_read_unlock();
1392 
1393  if (neigh)
1394  neigh_release(neigh);
1395 
1396  ip_rt_put(rt);
1397  return rc;
1398 }
1399 
1403 static struct nes_cm_node *make_cm_node(struct nes_cm_core *cm_core,
1404  struct nes_vnic *nesvnic, struct nes_cm_info *cm_info,
1405  struct nes_cm_listener *listener)
1406 {
1407  struct nes_cm_node *cm_node;
1408  struct timespec ts;
1409  int oldarpindex = 0;
1410  int arpindex = 0;
1411  struct nes_device *nesdev;
1412  struct nes_adapter *nesadapter;
1413 
1414  /* create an hte and cm_node for this instance */
1415  cm_node = kzalloc(sizeof(*cm_node), GFP_ATOMIC);
1416  if (!cm_node)
1417  return NULL;
1418 
1419  /* set our node specific transport info */
1420  cm_node->loc_addr = cm_info->loc_addr;
1421  cm_node->rem_addr = cm_info->rem_addr;
1422  cm_node->loc_port = cm_info->loc_port;
1423  cm_node->rem_port = cm_info->rem_port;
1424 
1425  cm_node->mpa_frame_rev = mpa_version;
1427  cm_node->ird_size = IETF_NO_IRD_ORD;
1428  cm_node->ord_size = IETF_NO_IRD_ORD;
1429 
1430  nes_debug(NES_DBG_CM, "Make node addresses : loc = %pI4:%x, rem = %pI4:%x\n",
1431  &cm_node->loc_addr, cm_node->loc_port,
1432  &cm_node->rem_addr, cm_node->rem_port);
1433  cm_node->listener = listener;
1434  cm_node->netdev = nesvnic->netdev;
1435  cm_node->cm_id = cm_info->cm_id;
1436  memcpy(cm_node->loc_mac, nesvnic->netdev->dev_addr, ETH_ALEN);
1437 
1438  nes_debug(NES_DBG_CM, "listener=%p, cm_id=%p\n", cm_node->listener,
1439  cm_node->cm_id);
1440 
1441  spin_lock_init(&cm_node->retrans_list_lock);
1442 
1443  cm_node->loopbackpartner = NULL;
1444  atomic_set(&cm_node->ref_count, 1);
1445  /* associate our parent CM core */
1446  cm_node->cm_core = cm_core;
1447  cm_node->tcp_cntxt.loc_id = NES_CM_DEF_LOCAL_ID;
1448  cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
1449  cm_node->tcp_cntxt.rcv_wnd = NES_CM_DEFAULT_RCV_WND_SCALED >>
1451  ts = current_kernel_time();
1452  cm_node->tcp_cntxt.loc_seq_num = htonl(ts.tv_nsec);
1453  cm_node->tcp_cntxt.mss = nesvnic->max_frame_size - sizeof(struct iphdr) -
1455  cm_node->tcp_cntxt.rcv_nxt = 0;
1456  /* get a unique session ID , add thread_id to an upcounter to handle race */
1457  atomic_inc(&cm_core->node_cnt);
1458  cm_node->conn_type = cm_info->conn_type;
1459  cm_node->apbvt_set = 0;
1460  cm_node->accept_pend = 0;
1461 
1462  cm_node->nesvnic = nesvnic;
1463  /* get some device handles, for arp lookup */
1464  nesdev = nesvnic->nesdev;
1465  nesadapter = nesdev->nesadapter;
1466 
1467  cm_node->loopbackpartner = NULL;
1468 
1469  /* get the mac addr for the remote node */
1470  oldarpindex = nes_arp_table(nesdev, cm_node->rem_addr, NULL, NES_ARP_RESOLVE);
1471  arpindex = nes_addr_resolve_neigh(nesvnic, cm_info->rem_addr, oldarpindex);
1472  if (arpindex < 0) {
1473  kfree(cm_node);
1474  return NULL;
1475  }
1476 
1477  /* copy the mac addr to node context */
1478  memcpy(cm_node->rem_mac, nesadapter->arp_table[arpindex].mac_addr, ETH_ALEN);
1479  nes_debug(NES_DBG_CM, "Remote mac addr from arp table: %pM\n",
1480  cm_node->rem_mac);
1481 
1482  add_hte_node(cm_core, cm_node);
1483  atomic_inc(&cm_nodes_created);
1484 
1485  return cm_node;
1486 }
1487 
1488 
1492 static int add_ref_cm_node(struct nes_cm_node *cm_node)
1493 {
1494  atomic_inc(&cm_node->ref_count);
1495  return 0;
1496 }
1497 
1498 
1502 static int rem_ref_cm_node(struct nes_cm_core *cm_core,
1503  struct nes_cm_node *cm_node)
1504 {
1505  unsigned long flags;
1506  struct nes_qp *nesqp;
1507 
1508  if (!cm_node)
1509  return -EINVAL;
1510 
1511  spin_lock_irqsave(&cm_node->cm_core->ht_lock, flags);
1512  if (atomic_dec_return(&cm_node->ref_count)) {
1513  spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1514  return 0;
1515  }
1516  list_del(&cm_node->list);
1517  atomic_dec(&cm_core->ht_node_cnt);
1518  spin_unlock_irqrestore(&cm_node->cm_core->ht_lock, flags);
1519 
1520  /* if the node is destroyed before connection was accelerated */
1521  if (!cm_node->accelerated && cm_node->accept_pend) {
1522  BUG_ON(!cm_node->listener);
1523  atomic_dec(&cm_node->listener->pend_accepts_cnt);
1524  BUG_ON(atomic_read(&cm_node->listener->pend_accepts_cnt) < 0);
1525  }
1526  WARN_ON(cm_node->send_entry);
1527  if (cm_node->recv_entry)
1528  handle_recv_entry(cm_node, 0);
1529  if (cm_node->listener) {
1530  mini_cm_dec_refcnt_listen(cm_core, cm_node->listener, 0);
1531  } else {
1532  if (cm_node->apbvt_set && cm_node->nesvnic) {
1533  nes_manage_apbvt(cm_node->nesvnic, cm_node->loc_port,
1534  PCI_FUNC(
1535  cm_node->nesvnic->nesdev->pcidev->devfn),
1537  }
1538  }
1539 
1540  atomic_dec(&cm_core->node_cnt);
1541  atomic_inc(&cm_nodes_destroyed);
1542  nesqp = cm_node->nesqp;
1543  if (nesqp) {
1544  nesqp->cm_node = NULL;
1545  nes_rem_ref(&nesqp->ibqp);
1546  cm_node->nesqp = NULL;
1547  }
1548 
1549  kfree(cm_node);
1550  return 0;
1551 }
1552 
1556 static int process_options(struct nes_cm_node *cm_node, u8 *optionsloc,
1557  u32 optionsize, u32 syn_packet)
1558 {
1559  u32 tmp;
1560  u32 offset = 0;
1561  union all_known_options *all_options;
1562  char got_mss_option = 0;
1563 
1564  while (offset < optionsize) {
1565  all_options = (union all_known_options *)(optionsloc + offset);
1566  switch (all_options->as_base.optionnum) {
1567  case OPTION_NUMBER_END:
1568  offset = optionsize;
1569  break;
1570  case OPTION_NUMBER_NONE:
1571  offset += 1;
1572  continue;
1573  case OPTION_NUMBER_MSS:
1574  nes_debug(NES_DBG_CM, "%s: MSS Length: %d Offset: %d "
1575  "Size: %d\n", __func__,
1576  all_options->as_mss.length, offset, optionsize);
1577  got_mss_option = 1;
1578  if (all_options->as_mss.length != 4) {
1579  return 1;
1580  } else {
1581  tmp = ntohs(all_options->as_mss.mss);
1582  if (tmp > 0 && tmp <
1583  cm_node->tcp_cntxt.mss)
1584  cm_node->tcp_cntxt.mss = tmp;
1585  }
1586  break;
1588  cm_node->tcp_cntxt.snd_wscale =
1589  all_options->as_windowscale.shiftcount;
1590  break;
1591  default:
1592  nes_debug(NES_DBG_CM, "TCP Option not understood: %x\n",
1593  all_options->as_base.optionnum);
1594  break;
1595  }
1596  offset += all_options->as_base.length;
1597  }
1598  if ((!got_mss_option) && (syn_packet))
1599  cm_node->tcp_cntxt.mss = NES_CM_DEFAULT_MSS;
1600  return 0;
1601 }
1602 
1603 static void drop_packet(struct sk_buff *skb)
1604 {
1605  atomic_inc(&cm_accel_dropped_pkts);
1606  dev_kfree_skb_any(skb);
1607 }
1608 
1609 static void handle_fin_pkt(struct nes_cm_node *cm_node)
1610 {
1611  nes_debug(NES_DBG_CM, "Received FIN, cm_node = %p, state = %u. "
1612  "refcnt=%d\n", cm_node, cm_node->state,
1613  atomic_read(&cm_node->ref_count));
1614  switch (cm_node->state) {
1615  case NES_CM_STATE_SYN_RCVD:
1616  case NES_CM_STATE_SYN_SENT:
1619  cm_node->tcp_cntxt.rcv_nxt++;
1620  cleanup_retrans_entry(cm_node);
1621  cm_node->state = NES_CM_STATE_LAST_ACK;
1622  send_fin(cm_node, NULL);
1623  break;
1625  create_event(cm_node, NES_CM_EVENT_ABORTED);
1626  cm_node->tcp_cntxt.rcv_nxt++;
1627  cleanup_retrans_entry(cm_node);
1628  cm_node->state = NES_CM_STATE_CLOSED;
1629  add_ref_cm_node(cm_node);
1630  send_reset(cm_node, NULL);
1631  break;
1633  cm_node->tcp_cntxt.rcv_nxt++;
1634  cleanup_retrans_entry(cm_node);
1635  cm_node->state = NES_CM_STATE_CLOSING;
1636  send_ack(cm_node, NULL);
1637  /* Wait for ACK as this is simultaneous close..
1638  * After we receive ACK, do not send anything..
1639  * Just rm the node.. Done.. */
1640  break;
1642  cm_node->tcp_cntxt.rcv_nxt++;
1643  cleanup_retrans_entry(cm_node);
1644  cm_node->state = NES_CM_STATE_TIME_WAIT;
1645  send_ack(cm_node, NULL);
1646  schedule_nes_timer(cm_node, NULL, NES_TIMER_TYPE_CLOSE, 1, 0);
1647  break;
1649  cm_node->tcp_cntxt.rcv_nxt++;
1650  cleanup_retrans_entry(cm_node);
1651  cm_node->state = NES_CM_STATE_CLOSED;
1652  rem_ref_cm_node(cm_node->cm_core, cm_node);
1653  break;
1654  case NES_CM_STATE_TSA:
1655  default:
1656  nes_debug(NES_DBG_CM, "Error Rcvd FIN for node-%p state = %d\n",
1657  cm_node, cm_node->state);
1658  break;
1659  }
1660 }
1661 
1662 
1663 static void handle_rst_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1664  struct tcphdr *tcph)
1665 {
1666 
1667  int reset = 0; /* whether to send reset in case of err.. */
1668  atomic_inc(&cm_resets_recvd);
1669  nes_debug(NES_DBG_CM, "Received Reset, cm_node = %p, state = %u."
1670  " refcnt=%d\n", cm_node, cm_node->state,
1671  atomic_read(&cm_node->ref_count));
1672  cleanup_retrans_entry(cm_node);
1673  switch (cm_node->state) {
1674  case NES_CM_STATE_SYN_SENT:
1676  nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1677  "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1678  cm_node->listener, cm_node->state);
1679  switch (cm_node->mpa_frame_rev) {
1680  case IETF_MPA_V2:
1681  cm_node->mpa_frame_rev = IETF_MPA_V1;
1682  /* send a syn and goto syn sent state */
1683  cm_node->state = NES_CM_STATE_SYN_SENT;
1684  if (send_syn(cm_node, 0, NULL)) {
1685  active_open_err(cm_node, skb, reset);
1686  }
1687  break;
1688  case IETF_MPA_V1:
1689  default:
1690  active_open_err(cm_node, skb, reset);
1691  break;
1692  }
1693  break;
1695  atomic_inc(&cm_node->passive_state);
1696  dev_kfree_skb_any(skb);
1697  break;
1699  case NES_CM_STATE_SYN_RCVD:
1701  nes_debug(NES_DBG_CM, "Bad state %s[%u]\n", __func__, __LINE__);
1702  passive_open_err(cm_node, skb, reset);
1703  break;
1704  case NES_CM_STATE_TSA:
1705  active_open_err(cm_node, skb, reset);
1706  break;
1707  case NES_CM_STATE_CLOSED:
1708  drop_packet(skb);
1709  break;
1712  case NES_CM_STATE_LAST_ACK:
1713  cm_node->cm_id->rem_ref(cm_node->cm_id);
1715  cm_node->state = NES_CM_STATE_CLOSED;
1716  rem_ref_cm_node(cm_node->cm_core, cm_node);
1717  drop_packet(skb);
1718  break;
1719  default:
1720  drop_packet(skb);
1721  break;
1722  }
1723 }
1724 
1725 
1726 static void handle_rcv_mpa(struct nes_cm_node *cm_node, struct sk_buff *skb)
1727 {
1728  int ret = 0;
1729  int datasize = skb->len;
1730  u8 *dataloc = skb->data;
1731 
1733  u32 res_type;
1734 
1735  ret = parse_mpa(cm_node, dataloc, &res_type, datasize);
1736  if (ret) {
1737  nes_debug(NES_DBG_CM, "didn't like MPA Request\n");
1738  if (cm_node->state == NES_CM_STATE_MPAREQ_SENT) {
1739  nes_debug(NES_DBG_CM, "%s[%u] create abort for "
1740  "cm_node=%p listener=%p state=%d\n", __func__,
1741  __LINE__, cm_node, cm_node->listener,
1742  cm_node->state);
1743  active_open_err(cm_node, skb, 1);
1744  } else {
1745  passive_open_err(cm_node, skb, 1);
1746  }
1747  return;
1748  }
1749 
1750  switch (cm_node->state) {
1752  if (res_type == NES_MPA_REQUEST_REJECT)
1753  /*BIG problem as we are receiving the MPA.. So should
1754  * not be REJECT.. This is Passive Open.. We can
1755  * only receive it Reject for Active Open...*/
1756  WARN_ON(1);
1757  cm_node->state = NES_CM_STATE_MPAREQ_RCVD;
1758  type = NES_CM_EVENT_MPA_REQ;
1759  atomic_set(&cm_node->passive_state,
1761  break;
1763  cleanup_retrans_entry(cm_node);
1764  if (res_type == NES_MPA_REQUEST_REJECT) {
1765  type = NES_CM_EVENT_MPA_REJECT;
1766  cm_node->state = NES_CM_STATE_MPAREJ_RCVD;
1767  } else {
1768  type = NES_CM_EVENT_CONNECTED;
1769  cm_node->state = NES_CM_STATE_TSA;
1770  }
1771 
1772  break;
1773  default:
1774  WARN_ON(1);
1775  break;
1776  }
1777  dev_kfree_skb_any(skb);
1778  create_event(cm_node, type);
1779 }
1780 
1781 static void indicate_pkt_err(struct nes_cm_node *cm_node, struct sk_buff *skb)
1782 {
1783  switch (cm_node->state) {
1784  case NES_CM_STATE_SYN_SENT:
1786  nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1787  "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1788  cm_node->listener, cm_node->state);
1789  active_open_err(cm_node, skb, 1);
1790  break;
1792  case NES_CM_STATE_SYN_RCVD:
1793  passive_open_err(cm_node, skb, 1);
1794  break;
1795  case NES_CM_STATE_TSA:
1796  default:
1797  drop_packet(skb);
1798  }
1799 }
1800 
1801 static int check_syn(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1802  struct sk_buff *skb)
1803 {
1804  int err;
1805 
1806  err = ((ntohl(tcph->ack_seq) == cm_node->tcp_cntxt.loc_seq_num)) ? 0 : 1;
1807  if (err)
1808  active_open_err(cm_node, skb, 1);
1809 
1810  return err;
1811 }
1812 
1813 static int check_seq(struct nes_cm_node *cm_node, struct tcphdr *tcph,
1814  struct sk_buff *skb)
1815 {
1816  int err = 0;
1817  u32 seq;
1818  u32 ack_seq;
1819  u32 loc_seq_num = cm_node->tcp_cntxt.loc_seq_num;
1820  u32 rcv_nxt = cm_node->tcp_cntxt.rcv_nxt;
1821  u32 rcv_wnd;
1822 
1823  seq = ntohl(tcph->seq);
1824  ack_seq = ntohl(tcph->ack_seq);
1825  rcv_wnd = cm_node->tcp_cntxt.rcv_wnd;
1826  if (ack_seq != loc_seq_num)
1827  err = 1;
1828  else if (!between(seq, rcv_nxt, (rcv_nxt + rcv_wnd)))
1829  err = 1;
1830  if (err) {
1831  nes_debug(NES_DBG_CM, "%s[%u] create abort for cm_node=%p "
1832  "listener=%p state=%d\n", __func__, __LINE__, cm_node,
1833  cm_node->listener, cm_node->state);
1834  indicate_pkt_err(cm_node, skb);
1835  nes_debug(NES_DBG_CM, "seq ERROR cm_node =%p seq=0x%08X "
1836  "rcv_nxt=0x%08X rcv_wnd=0x%x\n", cm_node, seq, rcv_nxt,
1837  rcv_wnd);
1838  }
1839  return err;
1840 }
1841 
1842 /*
1843  * handle_syn_pkt() is for Passive node. The syn packet is received when a node
1844  * is created with a listener or it may comein as rexmitted packet which in
1845  * that case will be just dropped.
1846  */
1847 static void handle_syn_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1848  struct tcphdr *tcph)
1849 {
1850  int ret;
1851  u32 inc_sequence;
1852  int optionsize;
1853 
1854  optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1855  skb_trim(skb, 0);
1856  inc_sequence = ntohl(tcph->seq);
1857 
1858  switch (cm_node->state) {
1859  case NES_CM_STATE_SYN_SENT:
1861  /* Rcvd syn on active open connection*/
1862  active_open_err(cm_node, skb, 1);
1863  break;
1865  /* Passive OPEN */
1866  if (atomic_read(&cm_node->listener->pend_accepts_cnt) >
1867  cm_node->listener->backlog) {
1868  nes_debug(NES_DBG_CM, "drop syn due to backlog "
1869  "pressure \n");
1870  cm_backlog_drops++;
1871  passive_open_err(cm_node, skb, 0);
1872  break;
1873  }
1874  ret = handle_tcp_options(cm_node, tcph, skb, optionsize,
1875  1);
1876  if (ret) {
1877  passive_open_err(cm_node, skb, 0);
1878  /* drop pkt */
1879  break;
1880  }
1881  cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1882  BUG_ON(cm_node->send_entry);
1883  cm_node->accept_pend = 1;
1884  atomic_inc(&cm_node->listener->pend_accepts_cnt);
1885 
1886  cm_node->state = NES_CM_STATE_SYN_RCVD;
1887  send_syn(cm_node, 1, skb);
1888  break;
1889  case NES_CM_STATE_CLOSED:
1890  cleanup_retrans_entry(cm_node);
1891  add_ref_cm_node(cm_node);
1892  send_reset(cm_node, skb);
1893  break;
1894  case NES_CM_STATE_TSA:
1899  case NES_CM_STATE_LAST_ACK:
1900  case NES_CM_STATE_CLOSING:
1901  case NES_CM_STATE_UNKNOWN:
1902  default:
1903  drop_packet(skb);
1904  break;
1905  }
1906 }
1907 
1908 static void handle_synack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1909  struct tcphdr *tcph)
1910 {
1911  int ret;
1912  u32 inc_sequence;
1913  int optionsize;
1914 
1915  optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1916  skb_trim(skb, 0);
1917  inc_sequence = ntohl(tcph->seq);
1918  switch (cm_node->state) {
1919  case NES_CM_STATE_SYN_SENT:
1920  cleanup_retrans_entry(cm_node);
1921  /* active open */
1922  if (check_syn(cm_node, tcph, skb))
1923  return;
1924  cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1925  /* setup options */
1926  ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 0);
1927  if (ret) {
1928  nes_debug(NES_DBG_CM, "cm_node=%p tcp_options failed\n",
1929  cm_node);
1930  break;
1931  }
1932  cleanup_retrans_entry(cm_node);
1933  cm_node->tcp_cntxt.rcv_nxt = inc_sequence + 1;
1934  send_mpa_request(cm_node, skb);
1935  cm_node->state = NES_CM_STATE_MPAREQ_SENT;
1936  break;
1938  /* passive open, so should not be here */
1939  passive_open_err(cm_node, skb, 1);
1940  break;
1942  cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1943  cleanup_retrans_entry(cm_node);
1944  cm_node->state = NES_CM_STATE_CLOSED;
1945  send_reset(cm_node, skb);
1946  break;
1947  case NES_CM_STATE_CLOSED:
1948  cm_node->tcp_cntxt.loc_seq_num = ntohl(tcph->ack_seq);
1949  cleanup_retrans_entry(cm_node);
1950  add_ref_cm_node(cm_node);
1951  send_reset(cm_node, skb);
1952  break;
1956  case NES_CM_STATE_LAST_ACK:
1957  case NES_CM_STATE_TSA:
1958  case NES_CM_STATE_CLOSING:
1959  case NES_CM_STATE_UNKNOWN:
1961  default:
1962  drop_packet(skb);
1963  break;
1964  }
1965 }
1966 
1967 static int handle_ack_pkt(struct nes_cm_node *cm_node, struct sk_buff *skb,
1968  struct tcphdr *tcph)
1969 {
1970  int datasize = 0;
1971  u32 inc_sequence;
1972  int ret = 0;
1973  int optionsize;
1974 
1975  optionsize = (tcph->doff << 2) - sizeof(struct tcphdr);
1976 
1977  if (check_seq(cm_node, tcph, skb))
1978  return -EINVAL;
1979 
1980  skb_pull(skb, tcph->doff << 2);
1981  inc_sequence = ntohl(tcph->seq);
1982  datasize = skb->len;
1983  switch (cm_node->state) {
1984  case NES_CM_STATE_SYN_RCVD:
1985  /* Passive OPEN */
1986  cleanup_retrans_entry(cm_node);
1987  ret = handle_tcp_options(cm_node, tcph, skb, optionsize, 1);
1988  if (ret)
1989  break;
1990  cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
1991  cm_node->state = NES_CM_STATE_ESTABLISHED;
1992  if (datasize) {
1993  cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
1994  handle_rcv_mpa(cm_node, skb);
1995  } else { /* rcvd ACK only */
1996  dev_kfree_skb_any(skb);
1997  }
1998  break;
2000  /* Passive OPEN */
2001  cleanup_retrans_entry(cm_node);
2002  if (datasize) {
2003  cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
2004  handle_rcv_mpa(cm_node, skb);
2005  } else {
2006  drop_packet(skb);
2007  }
2008  break;
2010  cm_node->tcp_cntxt.rem_ack_num = ntohl(tcph->ack_seq);
2011  if (datasize) {
2012  cm_node->tcp_cntxt.rcv_nxt = inc_sequence + datasize;
2013  handle_rcv_mpa(cm_node, skb);
2014  } else { /* Could be just an ack pkt.. */
2015  dev_kfree_skb_any(skb);
2016  }
2017  break;
2019  cleanup_retrans_entry(cm_node);
2020  cm_node->state = NES_CM_STATE_CLOSED;
2021  send_reset(cm_node, skb);
2022  break;
2023  case NES_CM_STATE_CLOSED:
2024  cleanup_retrans_entry(cm_node);
2025  add_ref_cm_node(cm_node);
2026  send_reset(cm_node, skb);
2027  break;
2028  case NES_CM_STATE_LAST_ACK:
2029  case NES_CM_STATE_CLOSING:
2030  cleanup_retrans_entry(cm_node);
2031  cm_node->state = NES_CM_STATE_CLOSED;
2032  cm_node->cm_id->rem_ref(cm_node->cm_id);
2033  rem_ref_cm_node(cm_node->cm_core, cm_node);
2034  drop_packet(skb);
2035  break;
2037  cleanup_retrans_entry(cm_node);
2038  drop_packet(skb);
2039  cm_node->state = NES_CM_STATE_FIN_WAIT2;
2040  break;
2041  case NES_CM_STATE_SYN_SENT:
2043  case NES_CM_STATE_TSA:
2045  case NES_CM_STATE_UNKNOWN:
2046  default:
2047  cleanup_retrans_entry(cm_node);
2048  drop_packet(skb);
2049  break;
2050  }
2051  return ret;
2052 }
2053 
2054 
2055 
2056 static int handle_tcp_options(struct nes_cm_node *cm_node, struct tcphdr *tcph,
2057  struct sk_buff *skb, int optionsize, int passive)
2058 {
2059  u8 *optionsloc = (u8 *)&tcph[1];
2060 
2061  if (optionsize) {
2062  if (process_options(cm_node, optionsloc, optionsize,
2063  (u32)tcph->syn)) {
2064  nes_debug(NES_DBG_CM, "%s: Node %p, Sending RESET\n",
2065  __func__, cm_node);
2066  if (passive)
2067  passive_open_err(cm_node, skb, 1);
2068  else
2069  active_open_err(cm_node, skb, 1);
2070  return 1;
2071  }
2072  }
2073 
2074  cm_node->tcp_cntxt.snd_wnd = ntohs(tcph->window) <<
2075  cm_node->tcp_cntxt.snd_wscale;
2076 
2077  if (cm_node->tcp_cntxt.snd_wnd > cm_node->tcp_cntxt.max_snd_wnd)
2078  cm_node->tcp_cntxt.max_snd_wnd = cm_node->tcp_cntxt.snd_wnd;
2079  return 0;
2080 }
2081 
2082 /*
2083  * active_open_err() will send reset() if flag set..
2084  * It will also send ABORT event.
2085  */
2086 static void active_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
2087  int reset)
2088 {
2089  cleanup_retrans_entry(cm_node);
2090  if (reset) {
2091  nes_debug(NES_DBG_CM, "ERROR active err called for cm_node=%p, "
2092  "state=%d\n", cm_node, cm_node->state);
2093  add_ref_cm_node(cm_node);
2094  send_reset(cm_node, skb);
2095  } else {
2096  dev_kfree_skb_any(skb);
2097  }
2098 
2099  cm_node->state = NES_CM_STATE_CLOSED;
2100  create_event(cm_node, NES_CM_EVENT_ABORTED);
2101 }
2102 
2103 /*
2104  * passive_open_err() will either do a reset() or will free up the skb and
2105  * remove the cm_node.
2106  */
2107 static void passive_open_err(struct nes_cm_node *cm_node, struct sk_buff *skb,
2108  int reset)
2109 {
2110  cleanup_retrans_entry(cm_node);
2111  cm_node->state = NES_CM_STATE_CLOSED;
2112  if (reset) {
2113  nes_debug(NES_DBG_CM, "passive_open_err sending RST for "
2114  "cm_node=%p state =%d\n", cm_node, cm_node->state);
2115  send_reset(cm_node, skb);
2116  } else {
2117  dev_kfree_skb_any(skb);
2118  rem_ref_cm_node(cm_node->cm_core, cm_node);
2119  }
2120 }
2121 
2122 /*
2123  * free_retrans_entry() routines assumes that the retrans_list_lock has
2124  * been acquired before calling.
2125  */
2126 static void free_retrans_entry(struct nes_cm_node *cm_node)
2127 {
2128  struct nes_timer_entry *send_entry;
2129 
2130  send_entry = cm_node->send_entry;
2131  if (send_entry) {
2132  cm_node->send_entry = NULL;
2133  dev_kfree_skb_any(send_entry->skb);
2134  kfree(send_entry);
2135  rem_ref_cm_node(cm_node->cm_core, cm_node);
2136  }
2137 }
2138 
2139 static void cleanup_retrans_entry(struct nes_cm_node *cm_node)
2140 {
2141  unsigned long flags;
2142 
2143  spin_lock_irqsave(&cm_node->retrans_list_lock, flags);
2144  free_retrans_entry(cm_node);
2145  spin_unlock_irqrestore(&cm_node->retrans_list_lock, flags);
2146 }
2147 
2152 static void process_packet(struct nes_cm_node *cm_node, struct sk_buff *skb,
2153  struct nes_cm_core *cm_core)
2154 {
2155  enum nes_tcpip_pkt_type pkt_type = NES_PKT_TYPE_UNKNOWN;
2156  struct tcphdr *tcph = tcp_hdr(skb);
2157  u32 fin_set = 0;
2158  int ret = 0;
2159 
2160  skb_pull(skb, ip_hdr(skb)->ihl << 2);
2161 
2162  nes_debug(NES_DBG_CM, "process_packet: cm_node=%p state =%d syn=%d "
2163  "ack=%d rst=%d fin=%d\n", cm_node, cm_node->state, tcph->syn,
2164  tcph->ack, tcph->rst, tcph->fin);
2165 
2166  if (tcph->rst) {
2167  pkt_type = NES_PKT_TYPE_RST;
2168  } else if (tcph->syn) {
2169  pkt_type = NES_PKT_TYPE_SYN;
2170  if (tcph->ack)
2171  pkt_type = NES_PKT_TYPE_SYNACK;
2172  } else if (tcph->ack) {
2173  pkt_type = NES_PKT_TYPE_ACK;
2174  }
2175  if (tcph->fin)
2176  fin_set = 1;
2177 
2178  switch (pkt_type) {
2179  case NES_PKT_TYPE_SYN:
2180  handle_syn_pkt(cm_node, skb, tcph);
2181  break;
2182  case NES_PKT_TYPE_SYNACK:
2183  handle_synack_pkt(cm_node, skb, tcph);
2184  break;
2185  case NES_PKT_TYPE_ACK:
2186  ret = handle_ack_pkt(cm_node, skb, tcph);
2187  if (fin_set && !ret)
2188  handle_fin_pkt(cm_node);
2189  break;
2190  case NES_PKT_TYPE_RST:
2191  handle_rst_pkt(cm_node, skb, tcph);
2192  break;
2193  default:
2194  if ((fin_set) && (!check_seq(cm_node, tcph, skb)))
2195  handle_fin_pkt(cm_node);
2196  drop_packet(skb);
2197  break;
2198  }
2199 }
2200 
2204 static struct nes_cm_listener *mini_cm_listen(struct nes_cm_core *cm_core,
2205  struct nes_vnic *nesvnic, struct nes_cm_info *cm_info)
2206 {
2207  struct nes_cm_listener *listener;
2208  unsigned long flags;
2209 
2210  nes_debug(NES_DBG_CM, "Search for 0x%08x : 0x%04x\n",
2211  cm_info->loc_addr, cm_info->loc_port);
2212 
2213  /* cannot have multiple matching listeners */
2214  listener = find_listener(cm_core, htonl(cm_info->loc_addr),
2216  if (listener && listener->listener_state == NES_CM_LISTENER_ACTIVE_STATE) {
2217  /* find automatically incs ref count ??? */
2218  atomic_dec(&listener->ref_count);
2219  nes_debug(NES_DBG_CM, "Not creating listener since it already exists\n");
2220  return NULL;
2221  }
2222 
2223  if (!listener) {
2224  /* create a CM listen node (1/2 node to compare incoming traffic to) */
2225  listener = kzalloc(sizeof(*listener), GFP_ATOMIC);
2226  if (!listener) {
2227  nes_debug(NES_DBG_CM, "Not creating listener memory allocation failed\n");
2228  return NULL;
2229  }
2230 
2231  listener->loc_addr = htonl(cm_info->loc_addr);
2232  listener->loc_port = htons(cm_info->loc_port);
2233  listener->reused_node = 0;
2234 
2235  atomic_set(&listener->ref_count, 1);
2236  }
2237  /* pasive case */
2238  /* find already inc'ed the ref count */
2239  else {
2240  listener->reused_node = 1;
2241  }
2242 
2243  listener->cm_id = cm_info->cm_id;
2244  atomic_set(&listener->pend_accepts_cnt, 0);
2245  listener->cm_core = cm_core;
2246  listener->nesvnic = nesvnic;
2247  atomic_inc(&cm_core->node_cnt);
2248 
2249  listener->conn_type = cm_info->conn_type;
2250  listener->backlog = cm_info->backlog;
2252 
2253  if (!listener->reused_node) {
2254  spin_lock_irqsave(&cm_core->listen_list_lock, flags);
2255  list_add(&listener->list, &cm_core->listen_list.list);
2256  spin_unlock_irqrestore(&cm_core->listen_list_lock, flags);
2257  atomic_inc(&cm_core->listen_node_cnt);
2258  }
2259 
2260  nes_debug(NES_DBG_CM, "Api - listen(): addr=0x%08X, port=0x%04x,"
2261  " listener = %p, backlog = %d, cm_id = %p.\n",
2262  cm_info->loc_addr, cm_info->loc_port,
2263  listener, listener->backlog, listener->cm_id);
2264 
2265  return listener;
2266 }
2267 
2268 
2272 static struct nes_cm_node *mini_cm_connect(struct nes_cm_core *cm_core,
2273  struct nes_vnic *nesvnic, u16 private_data_len,
2274  void *private_data, struct nes_cm_info *cm_info)
2275 {
2276  int ret = 0;
2277  struct nes_cm_node *cm_node;
2278  struct nes_cm_listener *loopbackremotelistener;
2279  struct nes_cm_node *loopbackremotenode;
2280  struct nes_cm_info loopback_cm_info;
2281  u8 *start_buff;
2282 
2283  /* create a CM connection node */
2284  cm_node = make_cm_node(cm_core, nesvnic, cm_info, NULL);
2285  if (!cm_node)
2286  return NULL;
2287 
2288  /* set our node side to client (active) side */
2289  cm_node->tcp_cntxt.client = 1;
2290  cm_node->tcp_cntxt.rcv_wscale = NES_CM_DEFAULT_RCV_WND_SCALE;
2291 
2292  if (cm_info->loc_addr == cm_info->rem_addr) {
2293  loopbackremotelistener = find_listener(cm_core,
2294  ntohl(nesvnic->local_ipaddr), cm_node->rem_port,
2296  if (loopbackremotelistener == NULL) {
2297  create_event(cm_node, NES_CM_EVENT_ABORTED);
2298  } else {
2299  loopback_cm_info = *cm_info;
2300  loopback_cm_info.loc_port = cm_info->rem_port;
2301  loopback_cm_info.rem_port = cm_info->loc_port;
2302  loopback_cm_info.cm_id = loopbackremotelistener->cm_id;
2303  loopbackremotenode = make_cm_node(cm_core, nesvnic,
2304  &loopback_cm_info, loopbackremotelistener);
2305  if (!loopbackremotenode) {
2306  rem_ref_cm_node(cm_node->cm_core, cm_node);
2307  return NULL;
2308  }
2309  atomic_inc(&cm_loopbacks);
2310  loopbackremotenode->loopbackpartner = cm_node;
2311  loopbackremotenode->tcp_cntxt.rcv_wscale =
2313  cm_node->loopbackpartner = loopbackremotenode;
2314  memcpy(loopbackremotenode->mpa_frame_buf, private_data,
2315  private_data_len);
2316  loopbackremotenode->mpa_frame_size = private_data_len;
2317 
2318  /* we are done handling this state. */
2319  /* set node to a TSA state */
2320  cm_node->state = NES_CM_STATE_TSA;
2321  cm_node->tcp_cntxt.rcv_nxt =
2322  loopbackremotenode->tcp_cntxt.loc_seq_num;
2323  loopbackremotenode->tcp_cntxt.rcv_nxt =
2324  cm_node->tcp_cntxt.loc_seq_num;
2325  cm_node->tcp_cntxt.max_snd_wnd =
2326  loopbackremotenode->tcp_cntxt.rcv_wnd;
2327  loopbackremotenode->tcp_cntxt.max_snd_wnd =
2328  cm_node->tcp_cntxt.rcv_wnd;
2329  cm_node->tcp_cntxt.snd_wnd =
2330  loopbackremotenode->tcp_cntxt.rcv_wnd;
2331  loopbackremotenode->tcp_cntxt.snd_wnd =
2332  cm_node->tcp_cntxt.rcv_wnd;
2333  cm_node->tcp_cntxt.snd_wscale =
2334  loopbackremotenode->tcp_cntxt.rcv_wscale;
2335  loopbackremotenode->tcp_cntxt.snd_wscale =
2336  cm_node->tcp_cntxt.rcv_wscale;
2337  loopbackremotenode->state = NES_CM_STATE_MPAREQ_RCVD;
2338  create_event(loopbackremotenode, NES_CM_EVENT_MPA_REQ);
2339  }
2340  return cm_node;
2341  }
2342 
2343  start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
2344  cm_node->mpa_frame_size = private_data_len;
2345 
2346  memcpy(start_buff, private_data, private_data_len);
2347 
2348  /* send a syn and goto syn sent state */
2349  cm_node->state = NES_CM_STATE_SYN_SENT;
2350  ret = send_syn(cm_node, 0, NULL);
2351 
2352  if (ret) {
2353  /* error in sending the syn free up the cm_node struct */
2354  nes_debug(NES_DBG_CM, "Api - connect() FAILED: dest "
2355  "addr=0x%08X, port=0x%04x, cm_node=%p, cm_id = %p.\n",
2356  cm_node->rem_addr, cm_node->rem_port, cm_node,
2357  cm_node->cm_id);
2358  rem_ref_cm_node(cm_node->cm_core, cm_node);
2359  cm_node = NULL;
2360  }
2361 
2362  if (cm_node) {
2363  nes_debug(NES_DBG_CM, "Api - connect(): dest addr=0x%08X,"
2364  "port=0x%04x, cm_node=%p, cm_id = %p.\n",
2365  cm_node->rem_addr, cm_node->rem_port, cm_node,
2366  cm_node->cm_id);
2367  }
2368 
2369  return cm_node;
2370 }
2371 
2372 
2377 static int mini_cm_accept(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
2378 {
2379  return 0;
2380 }
2381 
2382 
2386 static int mini_cm_reject(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
2387 {
2388  int ret = 0;
2389  int err = 0;
2390  int passive_state;
2391  struct nes_cm_event event;
2392  struct iw_cm_id *cm_id = cm_node->cm_id;
2393  struct nes_cm_node *loopback = cm_node->loopbackpartner;
2394 
2395  nes_debug(NES_DBG_CM, "%s cm_node=%p type=%d state=%d\n",
2396  __func__, cm_node, cm_node->tcp_cntxt.client, cm_node->state);
2397 
2398  if (cm_node->tcp_cntxt.client)
2399  return ret;
2400  cleanup_retrans_entry(cm_node);
2401 
2402  if (!loopback) {
2403  passive_state = atomic_add_return(1, &cm_node->passive_state);
2404  if (passive_state == NES_SEND_RESET_EVENT) {
2405  cm_node->state = NES_CM_STATE_CLOSED;
2406  rem_ref_cm_node(cm_core, cm_node);
2407  } else {
2408  if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2409  rem_ref_cm_node(cm_core, cm_node);
2410  } else {
2411  ret = send_mpa_reject(cm_node);
2412  if (ret) {
2413  cm_node->state = NES_CM_STATE_CLOSED;
2414  err = send_reset(cm_node, NULL);
2415  if (err)
2416  WARN_ON(1);
2417  } else {
2418  cm_id->add_ref(cm_id);
2419  }
2420  }
2421  }
2422  } else {
2423  cm_node->cm_id = NULL;
2424  if (cm_node->state == NES_CM_STATE_LISTENER_DESTROYED) {
2425  rem_ref_cm_node(cm_core, cm_node);
2426  rem_ref_cm_node(cm_core, loopback);
2427  } else {
2428  event.cm_node = loopback;
2429  event.cm_info.rem_addr = loopback->rem_addr;
2430  event.cm_info.loc_addr = loopback->loc_addr;
2431  event.cm_info.rem_port = loopback->rem_port;
2432  event.cm_info.loc_port = loopback->loc_port;
2433  event.cm_info.cm_id = loopback->cm_id;
2434  cm_event_mpa_reject(&event);
2435  rem_ref_cm_node(cm_core, cm_node);
2436  loopback->state = NES_CM_STATE_CLOSING;
2437 
2438  cm_id = loopback->cm_id;
2439  rem_ref_cm_node(cm_core, loopback);
2440  cm_id->rem_ref(cm_id);
2441  }
2442  }
2443 
2444  return ret;
2445 }
2446 
2447 
2451 static int mini_cm_close(struct nes_cm_core *cm_core, struct nes_cm_node *cm_node)
2452 {
2453  int ret = 0;
2454 
2455  if (!cm_core || !cm_node)
2456  return -EINVAL;
2457 
2458  switch (cm_node->state) {
2459  case NES_CM_STATE_SYN_RCVD:
2460  case NES_CM_STATE_SYN_SENT:
2466  cleanup_retrans_entry(cm_node);
2467  send_reset(cm_node, NULL);
2468  break;
2470  cm_node->state = NES_CM_STATE_LAST_ACK;
2471  send_fin(cm_node, NULL);
2472  break;
2475  case NES_CM_STATE_LAST_ACK:
2477  case NES_CM_STATE_CLOSING:
2478  ret = -1;
2479  break;
2481  cleanup_retrans_entry(cm_node);
2482  send_reset(cm_node, NULL);
2483  break;
2485  case NES_CM_STATE_UNKNOWN:
2486  case NES_CM_STATE_INITED:
2487  case NES_CM_STATE_CLOSED:
2489  ret = rem_ref_cm_node(cm_core, cm_node);
2490  break;
2491  case NES_CM_STATE_TSA:
2492  if (cm_node->send_entry)
2493  printk(KERN_ERR "ERROR Close got called from STATE_TSA "
2494  "send_entry=%p\n", cm_node->send_entry);
2495  ret = rem_ref_cm_node(cm_core, cm_node);
2496  break;
2497  }
2498  return ret;
2499 }
2500 
2501 
2506 static int mini_cm_recv_pkt(struct nes_cm_core *cm_core,
2507  struct nes_vnic *nesvnic, struct sk_buff *skb)
2508 {
2509  struct nes_cm_node *cm_node = NULL;
2510  struct nes_cm_listener *listener = NULL;
2511  struct iphdr *iph;
2512  struct tcphdr *tcph;
2513  struct nes_cm_info nfo;
2514  int skb_handled = 1;
2515  __be32 tmp_daddr, tmp_saddr;
2516 
2517  if (!skb)
2518  return 0;
2519  if (skb->len < sizeof(struct iphdr) + sizeof(struct tcphdr))
2520  return 0;
2521 
2522  iph = (struct iphdr *)skb->data;
2523  tcph = (struct tcphdr *)(skb->data + sizeof(struct iphdr));
2524 
2525  nfo.loc_addr = ntohl(iph->daddr);
2526  nfo.loc_port = ntohs(tcph->dest);
2527  nfo.rem_addr = ntohl(iph->saddr);
2528  nfo.rem_port = ntohs(tcph->source);
2529 
2530  tmp_daddr = cpu_to_be32(iph->daddr);
2531  tmp_saddr = cpu_to_be32(iph->saddr);
2532 
2533  nes_debug(NES_DBG_CM, "Received packet: dest=%pI4:0x%04X src=%pI4:0x%04X\n",
2534  &tmp_daddr, tcph->dest, &tmp_saddr, tcph->source);
2535 
2536  do {
2537  cm_node = find_node(cm_core,
2538  nfo.rem_port, nfo.rem_addr,
2539  nfo.loc_port, nfo.loc_addr);
2540 
2541  if (!cm_node) {
2542  /* Only type of packet accepted are for */
2543  /* the PASSIVE open (syn only) */
2544  if ((!tcph->syn) || (tcph->ack)) {
2545  skb_handled = 0;
2546  break;
2547  }
2548  listener = find_listener(cm_core, nfo.loc_addr,
2549  nfo.loc_port,
2551  if (!listener) {
2552  nfo.cm_id = NULL;
2553  nfo.conn_type = 0;
2554  nes_debug(NES_DBG_CM, "Unable to find listener for the pkt\n");
2555  skb_handled = 0;
2556  break;
2557  }
2558  nfo.cm_id = listener->cm_id;
2559  nfo.conn_type = listener->conn_type;
2560  cm_node = make_cm_node(cm_core, nesvnic, &nfo,
2561  listener);
2562  if (!cm_node) {
2563  nes_debug(NES_DBG_CM, "Unable to allocate "
2564  "node\n");
2566  atomic_dec(&listener->ref_count);
2567  dev_kfree_skb_any(skb);
2568  break;
2569  }
2570  if (!tcph->rst && !tcph->fin) {
2571  cm_node->state = NES_CM_STATE_LISTENING;
2572  } else {
2574  rem_ref_cm_node(cm_core, cm_node);
2575  dev_kfree_skb_any(skb);
2576  break;
2577  }
2578  add_ref_cm_node(cm_node);
2579  } else if (cm_node->state == NES_CM_STATE_TSA) {
2580  if (cm_node->nesqp->pau_mode)
2581  nes_queue_mgt_skbs(skb, nesvnic, cm_node->nesqp);
2582  else {
2583  rem_ref_cm_node(cm_core, cm_node);
2584  atomic_inc(&cm_accel_dropped_pkts);
2585  dev_kfree_skb_any(skb);
2586  }
2587  break;
2588  }
2589  skb_reset_network_header(skb);
2590  skb_set_transport_header(skb, sizeof(*tcph));
2591  skb->len = ntohs(iph->tot_len);
2592  process_packet(cm_node, skb, cm_core);
2593  rem_ref_cm_node(cm_core, cm_node);
2594  } while (0);
2595  return skb_handled;
2596 }
2597 
2598 
2602 static struct nes_cm_core *nes_cm_alloc_core(void)
2603 {
2604  struct nes_cm_core *cm_core;
2605 
2606  /* setup the CM core */
2607  /* alloc top level core control structure */
2608  cm_core = kzalloc(sizeof(*cm_core), GFP_KERNEL);
2609  if (!cm_core)
2610  return NULL;
2611 
2612  INIT_LIST_HEAD(&cm_core->connected_nodes);
2613  init_timer(&cm_core->tcp_timer);
2614  cm_core->tcp_timer.function = nes_cm_timer_tick;
2615 
2616  cm_core->mtu = NES_CM_DEFAULT_MTU;
2617  cm_core->state = NES_CM_STATE_INITED;
2619 
2620  atomic_set(&cm_core->events_posted, 0);
2621 
2622  cm_core->api = &nes_cm_api;
2623 
2624  spin_lock_init(&cm_core->ht_lock);
2625  spin_lock_init(&cm_core->listen_list_lock);
2626 
2627  INIT_LIST_HEAD(&cm_core->listen_list.list);
2628 
2629  nes_debug(NES_DBG_CM, "Init CM Core completed -- cm_core=%p\n", cm_core);
2630 
2631  nes_debug(NES_DBG_CM, "Enable QUEUE EVENTS\n");
2632  cm_core->event_wq = create_singlethread_workqueue("nesewq");
2633  cm_core->post_event = nes_cm_post_event;
2634  nes_debug(NES_DBG_CM, "Enable QUEUE DISCONNECTS\n");
2635  cm_core->disconn_wq = create_singlethread_workqueue("nesdwq");
2636 
2637  print_core(cm_core);
2638  return cm_core;
2639 }
2640 
2641 
2645 static int mini_cm_dealloc_core(struct nes_cm_core *cm_core)
2646 {
2647  nes_debug(NES_DBG_CM, "De-Alloc CM Core (%p)\n", cm_core);
2648 
2649  if (!cm_core)
2650  return -EINVAL;
2651 
2652  barrier();
2653 
2654  if (timer_pending(&cm_core->tcp_timer))
2655  del_timer(&cm_core->tcp_timer);
2656 
2657  destroy_workqueue(cm_core->event_wq);
2658  destroy_workqueue(cm_core->disconn_wq);
2659  nes_debug(NES_DBG_CM, "\n");
2660  kfree(cm_core);
2661 
2662  return 0;
2663 }
2664 
2665 
2669 static int mini_cm_get(struct nes_cm_core *cm_core)
2670 {
2671  return cm_core->state;
2672 }
2673 
2674 
2678 static int mini_cm_set(struct nes_cm_core *cm_core, u32 type, u32 value)
2679 {
2680  int ret = 0;
2681 
2682  switch (type) {
2683  case NES_CM_SET_PKT_SIZE:
2684  cm_core->mtu = value;
2685  break;
2687  cm_core->free_tx_pkt_max = value;
2688  break;
2689  default:
2690  /* unknown set option */
2691  ret = -EINVAL;
2692  }
2693 
2694  return ret;
2695 }
2696 
2697 
2702 static int nes_cm_init_tsa_conn(struct nes_qp *nesqp, struct nes_cm_node *cm_node)
2703 {
2704  int ret = 0;
2705 
2706  if (!nesqp)
2707  return -EINVAL;
2708 
2712 
2713  if (cm_node->tcp_cntxt.snd_wscale || cm_node->tcp_cntxt.rcv_wscale)
2715 
2717 
2718  nesqp->nesqp_context->mss |= cpu_to_le32(((u32)cm_node->tcp_cntxt.mss) << 16);
2719 
2720  nesqp->nesqp_context->tcp_state_flow_label |= cpu_to_le32(
2722 
2723  nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2724  (cm_node->tcp_cntxt.snd_wscale << NES_QPCONTEXT_PDWSCALE_SND_WSCALE_SHIFT) &
2726 
2727  nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32(
2728  (cm_node->tcp_cntxt.rcv_wscale << NES_QPCONTEXT_PDWSCALE_RCV_WSCALE_SHIFT) &
2730 
2731  nesqp->nesqp_context->keepalive = cpu_to_le32(0x80);
2732  nesqp->nesqp_context->ts_recent = 0;
2733  nesqp->nesqp_context->ts_age = 0;
2734  nesqp->nesqp_context->snd_nxt = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2735  nesqp->nesqp_context->snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.snd_wnd);
2736  nesqp->nesqp_context->rcv_nxt = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2737  nesqp->nesqp_context->rcv_wnd = cpu_to_le32(cm_node->tcp_cntxt.rcv_wnd <<
2738  cm_node->tcp_cntxt.rcv_wscale);
2739  nesqp->nesqp_context->snd_max = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2740  nesqp->nesqp_context->snd_una = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2741  nesqp->nesqp_context->srtt = 0;
2742  nesqp->nesqp_context->rttvar = cpu_to_le32(0x6);
2743  nesqp->nesqp_context->ssthresh = cpu_to_le32(0x3FFFC000);
2744  nesqp->nesqp_context->cwnd = cpu_to_le32(2 * cm_node->tcp_cntxt.mss);
2745  nesqp->nesqp_context->snd_wl1 = cpu_to_le32(cm_node->tcp_cntxt.rcv_nxt);
2746  nesqp->nesqp_context->snd_wl2 = cpu_to_le32(cm_node->tcp_cntxt.loc_seq_num);
2747  nesqp->nesqp_context->max_snd_wnd = cpu_to_le32(cm_node->tcp_cntxt.max_snd_wnd);
2748 
2749  nes_debug(NES_DBG_CM, "QP%u: rcv_nxt = 0x%08X, snd_nxt = 0x%08X,"
2750  " Setting MSS to %u, PDWscale = 0x%08X, rcv_wnd = %u, context misc = 0x%08X.\n",
2751  nesqp->hwqp.qp_id, le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
2752  le32_to_cpu(nesqp->nesqp_context->snd_nxt),
2753  cm_node->tcp_cntxt.mss, le32_to_cpu(nesqp->nesqp_context->pd_index_wscale),
2754  le32_to_cpu(nesqp->nesqp_context->rcv_wnd),
2755  le32_to_cpu(nesqp->nesqp_context->misc));
2756  nes_debug(NES_DBG_CM, " snd_wnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->snd_wnd));
2757  nes_debug(NES_DBG_CM, " snd_cwnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->cwnd));
2758  nes_debug(NES_DBG_CM, " max_swnd = 0x%08X.\n", le32_to_cpu(nesqp->nesqp_context->max_snd_wnd));
2759 
2760  nes_debug(NES_DBG_CM, "Change cm_node state to TSA\n");
2761  cm_node->state = NES_CM_STATE_TSA;
2762 
2763  return ret;
2764 }
2765 
2766 
2770 int nes_cm_disconn(struct nes_qp *nesqp)
2771 {
2772  struct disconn_work *work;
2773 
2774  work = kzalloc(sizeof *work, GFP_ATOMIC);
2775  if (!work)
2776  return -ENOMEM; /* Timer will clean up */
2777 
2778  nes_add_ref(&nesqp->ibqp);
2779  work->nesqp = nesqp;
2780  INIT_WORK(&work->work, nes_disconnect_worker);
2781  queue_work(g_cm_core->disconn_wq, &work->work);
2782  return 0;
2783 }
2784 
2785 
2789 static void nes_disconnect_worker(struct work_struct *work)
2790 {
2791  struct disconn_work *dwork = container_of(work, struct disconn_work, work);
2792  struct nes_qp *nesqp = dwork->nesqp;
2793 
2794  kfree(dwork);
2795  nes_debug(NES_DBG_CM, "processing AEQE id 0x%04X for QP%u.\n",
2796  nesqp->last_aeq, nesqp->hwqp.qp_id);
2797  nes_cm_disconn_true(nesqp);
2798  nes_rem_ref(&nesqp->ibqp);
2799 }
2800 
2801 
2805 static int nes_cm_disconn_true(struct nes_qp *nesqp)
2806 {
2807  unsigned long flags;
2808  int ret = 0;
2809  struct iw_cm_id *cm_id;
2810  struct iw_cm_event cm_event;
2811  struct nes_vnic *nesvnic;
2812  u16 last_ae;
2813  u8 original_hw_tcp_state;
2814  u8 original_ibqp_state;
2815  int disconn_status = 0;
2816  int issue_disconn = 0;
2817  int issue_close = 0;
2818  int issue_flush = 0;
2819  u32 flush_q = NES_CQP_FLUSH_RQ;
2820  struct ib_event ibevent;
2821 
2822  if (!nesqp) {
2823  nes_debug(NES_DBG_CM, "disconnect_worker nesqp is NULL\n");
2824  return -1;
2825  }
2826 
2827  spin_lock_irqsave(&nesqp->lock, flags);
2828  cm_id = nesqp->cm_id;
2829  /* make sure we havent already closed this connection */
2830  if (!cm_id) {
2831  nes_debug(NES_DBG_CM, "QP%u disconnect_worker cmid is NULL\n",
2832  nesqp->hwqp.qp_id);
2833  spin_unlock_irqrestore(&nesqp->lock, flags);
2834  return -1;
2835  }
2836 
2837  nesvnic = to_nesvnic(nesqp->ibqp.device);
2838  nes_debug(NES_DBG_CM, "Disconnecting QP%u\n", nesqp->hwqp.qp_id);
2839 
2840  original_hw_tcp_state = nesqp->hw_tcp_state;
2841  original_ibqp_state = nesqp->ibqp_state;
2842  last_ae = nesqp->last_aeq;
2843 
2844  if (nesqp->term_flags) {
2845  issue_disconn = 1;
2846  issue_close = 1;
2847  nesqp->cm_id = NULL;
2848  del_timer(&nesqp->terminate_timer);
2849  if (nesqp->flush_issued == 0) {
2850  nesqp->flush_issued = 1;
2851  issue_flush = 1;
2852  }
2853  } else if ((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSE_WAIT) ||
2854  ((original_ibqp_state == IB_QPS_RTS) &&
2855  (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2856  issue_disconn = 1;
2857  if (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET)
2858  disconn_status = -ECONNRESET;
2859  }
2860 
2861  if (((original_hw_tcp_state == NES_AEQE_TCP_STATE_CLOSED) ||
2862  (original_hw_tcp_state == NES_AEQE_TCP_STATE_TIME_WAIT) ||
2864  (last_ae == NES_AEQE_AEID_LLP_CONNECTION_RESET))) {
2865  issue_close = 1;
2866  nesqp->cm_id = NULL;
2867  if (nesqp->flush_issued == 0) {
2868  nesqp->flush_issued = 1;
2869  issue_flush = 1;
2870  }
2871  }
2872 
2873  spin_unlock_irqrestore(&nesqp->lock, flags);
2874 
2875  if ((issue_flush) && (nesqp->destroyed == 0)) {
2876  /* Flush the queue(s) */
2878  flush_q |= NES_CQP_FLUSH_SQ;
2879  flush_wqes(nesvnic->nesdev, nesqp, flush_q, 1);
2880 
2881  if (nesqp->term_flags) {
2882  ibevent.device = nesqp->ibqp.device;
2883  ibevent.event = nesqp->terminate_eventtype;
2884  ibevent.element.qp = &nesqp->ibqp;
2885  if (nesqp->ibqp.event_handler)
2886  nesqp->ibqp.event_handler(&ibevent, nesqp->ibqp.qp_context);
2887  }
2888  }
2889 
2890  if ((cm_id) && (cm_id->event_handler)) {
2891  if (issue_disconn) {
2892  atomic_inc(&cm_disconnects);
2893  cm_event.event = IW_CM_EVENT_DISCONNECT;
2894  cm_event.status = disconn_status;
2895  cm_event.local_addr = cm_id->local_addr;
2896  cm_event.remote_addr = cm_id->remote_addr;
2897  cm_event.private_data = NULL;
2898  cm_event.private_data_len = 0;
2899 
2900  nes_debug(NES_DBG_CM, "Generating a CM Disconnect Event"
2901  " for QP%u, SQ Head = %u, SQ Tail = %u. "
2902  "cm_id = %p, refcount = %u.\n",
2903  nesqp->hwqp.qp_id, nesqp->hwqp.sq_head,
2904  nesqp->hwqp.sq_tail, cm_id,
2905  atomic_read(&nesqp->refcount));
2906 
2907  ret = cm_id->event_handler(cm_id, &cm_event);
2908  if (ret)
2909  nes_debug(NES_DBG_CM, "OFA CM event_handler "
2910  "returned, ret=%d\n", ret);
2911  }
2912 
2913  if (issue_close) {
2914  atomic_inc(&cm_closes);
2915  nes_disconnect(nesqp, 1);
2916 
2917  cm_id->provider_data = nesqp;
2918  /* Send up the close complete event */
2919  cm_event.event = IW_CM_EVENT_CLOSE;
2920  cm_event.status = 0;
2921  cm_event.provider_data = cm_id->provider_data;
2922  cm_event.local_addr = cm_id->local_addr;
2923  cm_event.remote_addr = cm_id->remote_addr;
2924  cm_event.private_data = NULL;
2925  cm_event.private_data_len = 0;
2926 
2927  ret = cm_id->event_handler(cm_id, &cm_event);
2928  if (ret)
2929  nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
2930 
2931  cm_id->rem_ref(cm_id);
2932  }
2933  }
2934 
2935  return 0;
2936 }
2937 
2938 
2942 static int nes_disconnect(struct nes_qp *nesqp, int abrupt)
2943 {
2944  int ret = 0;
2945  struct nes_vnic *nesvnic;
2946  struct nes_device *nesdev;
2947  struct nes_ib_device *nesibdev;
2948 
2949  nesvnic = to_nesvnic(nesqp->ibqp.device);
2950  if (!nesvnic)
2951  return -EINVAL;
2952 
2953  nesdev = nesvnic->nesdev;
2954  nesibdev = nesvnic->nesibdev;
2955 
2956  nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
2957  netdev_refcnt_read(nesvnic->netdev));
2958 
2959  if (nesqp->active_conn) {
2960 
2961  /* indicate this connection is NOT active */
2962  nesqp->active_conn = 0;
2963  } else {
2964  /* Need to free the Last Streaming Mode Message */
2965  if (nesqp->ietf_frame) {
2966  if (nesqp->lsmm_mr)
2967  nesibdev->ibdev.dereg_mr(nesqp->lsmm_mr);
2968  pci_free_consistent(nesdev->pcidev,
2969  nesqp->private_data_len + nesqp->ietf_frame_size,
2970  nesqp->ietf_frame, nesqp->ietf_frame_pbase);
2971  }
2972  }
2973 
2974  /* close the CM node down if it is still active */
2975  if (nesqp->cm_node) {
2976  nes_debug(NES_DBG_CM, "Call close API\n");
2977 
2978  g_cm_core->api->close(g_cm_core, nesqp->cm_node);
2979  }
2980 
2981  return ret;
2982 }
2983 
2984 
2988 int nes_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2989 {
2990  u64 u64temp;
2991  struct ib_qp *ibqp;
2992  struct nes_qp *nesqp;
2993  struct nes_vnic *nesvnic;
2994  struct nes_device *nesdev;
2995  struct nes_cm_node *cm_node;
2996  struct nes_adapter *adapter;
2997  struct ib_qp_attr attr;
2998  struct iw_cm_event cm_event;
2999  struct nes_hw_qp_wqe *wqe;
3000  struct nes_v4_quad nes_quad;
3001  u32 crc_value;
3002  int ret;
3003  int passive_state;
3004  struct nes_ib_device *nesibdev;
3005  struct ib_mr *ibmr = NULL;
3006  struct ib_phys_buf ibphysbuf;
3007  struct nes_pd *nespd;
3008  u64 tagged_offset;
3009  u8 mpa_frame_offset = 0;
3010  struct ietf_mpa_v2 *mpa_v2_frame;
3011  u8 start_addr = 0;
3012  u8 *start_ptr = &start_addr;
3013  u8 **start_buff = &start_ptr;
3014  u16 buff_len = 0;
3015 
3016  ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3017  if (!ibqp)
3018  return -EINVAL;
3019 
3020  /* get all our handles */
3021  nesqp = to_nesqp(ibqp);
3022  nesvnic = to_nesvnic(nesqp->ibqp.device);
3023  nesdev = nesvnic->nesdev;
3024  adapter = nesdev->nesadapter;
3025 
3026  cm_node = (struct nes_cm_node *)cm_id->provider_data;
3027  nes_debug(NES_DBG_CM, "nes_accept: cm_node= %p nesvnic=%p, netdev=%p,"
3028  "%s\n", cm_node, nesvnic, nesvnic->netdev,
3029  nesvnic->netdev->name);
3030 
3031  if (NES_CM_STATE_LISTENER_DESTROYED == cm_node->state) {
3032  if (cm_node->loopbackpartner)
3033  rem_ref_cm_node(cm_node->cm_core, cm_node->loopbackpartner);
3034  rem_ref_cm_node(cm_node->cm_core, cm_node);
3035  return -EINVAL;
3036  }
3037 
3038  passive_state = atomic_add_return(1, &cm_node->passive_state);
3039  if (passive_state == NES_SEND_RESET_EVENT) {
3040  rem_ref_cm_node(cm_node->cm_core, cm_node);
3041  return -ECONNRESET;
3042  }
3043 
3044  /* associate the node with the QP */
3045  nesqp->cm_node = (void *)cm_node;
3046  cm_node->nesqp = nesqp;
3047 
3048  nes_debug(NES_DBG_CM, "QP%u, cm_node=%p, jiffies = %lu listener = %p\n",
3049  nesqp->hwqp.qp_id, cm_node, jiffies, cm_node->listener);
3050  atomic_inc(&cm_accepts);
3051 
3052  nes_debug(NES_DBG_CM, "netdev refcnt = %u.\n",
3053  netdev_refcnt_read(nesvnic->netdev));
3054 
3055  nesqp->ietf_frame_size = sizeof(struct ietf_mpa_v2);
3056  /* allocate the ietf frame and space for private data */
3057  nesqp->ietf_frame = pci_alloc_consistent(nesdev->pcidev,
3058  nesqp->ietf_frame_size + conn_param->private_data_len,
3059  &nesqp->ietf_frame_pbase);
3060 
3061  if (!nesqp->ietf_frame) {
3062  nes_debug(NES_DBG_CM, "Unable to allocate memory for private data\n");
3063  return -ENOMEM;
3064  }
3065  mpa_v2_frame = (struct ietf_mpa_v2 *)nesqp->ietf_frame;
3066 
3067  if (cm_node->mpa_frame_rev == IETF_MPA_V1)
3068  mpa_frame_offset = 4;
3069 
3070  memcpy(mpa_v2_frame->priv_data, conn_param->private_data,
3071  conn_param->private_data_len);
3072 
3073  cm_build_mpa_frame(cm_node, start_buff, &buff_len, nesqp->ietf_frame, MPA_KEY_REPLY);
3074  nesqp->private_data_len = conn_param->private_data_len;
3075 
3076  /* setup our first outgoing iWarp send WQE (the IETF frame response) */
3077  wqe = &nesqp->hwqp.sq_vbase[0];
3078 
3079  if (cm_id->remote_addr.sin_addr.s_addr !=
3080  cm_id->local_addr.sin_addr.s_addr) {
3081  u64temp = (unsigned long)nesqp;
3082  nesibdev = nesvnic->nesibdev;
3083  nespd = nesqp->nespd;
3084  ibphysbuf.addr = nesqp->ietf_frame_pbase + mpa_frame_offset;
3085  ibphysbuf.size = buff_len;
3086  tagged_offset = (u64)(unsigned long)*start_buff;
3087  ibmr = nesibdev->ibdev.reg_phys_mr((struct ib_pd *)nespd,
3088  &ibphysbuf, 1,
3090  &tagged_offset);
3091  if (!ibmr) {
3092  nes_debug(NES_DBG_CM, "Unable to register memory region"
3093  "for lSMM for cm_node = %p \n",
3094  cm_node);
3095  pci_free_consistent(nesdev->pcidev,
3096  nesqp->private_data_len + nesqp->ietf_frame_size,
3097  nesqp->ietf_frame, nesqp->ietf_frame_pbase);
3098  return -ENOMEM;
3099  }
3100 
3101  ibmr->pd = &nespd->ibpd;
3102  ibmr->device = nespd->ibpd.device;
3103  nesqp->lsmm_mr = ibmr;
3104 
3105  u64temp |= NES_SW_CONTEXT_ALIGN >> 1;
3106  set_wqe_64bit_value(wqe->wqe_words,
3108  u64temp);
3113  cpu_to_le32(buff_len);
3114  set_wqe_64bit_value(wqe->wqe_words,
3116  (u64)(unsigned long)(*start_buff));
3118  cpu_to_le32(buff_len);
3120  if (nesqp->sq_kmapped) {
3121  nesqp->sq_kmapped = 0;
3122  kunmap(nesqp->page);
3123  }
3124 
3125  nesqp->nesqp_context->ird_ord_sizes |=
3128  } else {
3129  nesqp->nesqp_context->ird_ord_sizes |=
3131  }
3132  nesqp->skip_lsmm = 1;
3133 
3134 
3135  /* Cache the cm_id in the qp */
3136  nesqp->cm_id = cm_id;
3137  cm_node->cm_id = cm_id;
3138 
3139  /* nesqp->cm_node = (void *)cm_id->provider_data; */
3140  cm_id->provider_data = nesqp;
3141  nesqp->active_conn = 0;
3142 
3143  if (cm_node->state == NES_CM_STATE_TSA)
3144  nes_debug(NES_DBG_CM, "Already state = TSA for cm_node=%p\n",
3145  cm_node);
3146 
3147  nes_cm_init_tsa_conn(nesqp, cm_node);
3148 
3149  nesqp->nesqp_context->tcpPorts[0] =
3150  cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3151  nesqp->nesqp_context->tcpPorts[1] =
3152  cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3153 
3154  nesqp->nesqp_context->ip0 =
3155  cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
3156 
3157  nesqp->nesqp_context->misc2 |= cpu_to_le32(
3158  (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3160 
3161  nesqp->nesqp_context->arp_index_vlan |=
3162  cpu_to_le32(nes_arp_table(nesdev,
3163  le32_to_cpu(nesqp->nesqp_context->ip0), NULL,
3164  NES_ARP_RESOLVE) << 16);
3165 
3166  nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3167  jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3168 
3169  nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3170 
3171  nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(
3173  nesqp->nesqp_context->ird_ord_sizes |=
3174  cpu_to_le32((u32)conn_param->ord);
3175 
3176  memset(&nes_quad, 0, sizeof(nes_quad));
3177  nes_quad.DstIpAdrIndex =
3178  cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3179  nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
3180  nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3181  nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3182 
3183  /* Produce hash key */
3184  crc_value = get_crc_value(&nes_quad);
3185  nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
3186  nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, CRC = 0x%08X\n",
3187  nesqp->hte_index, nesqp->hte_index & adapter->hte_index_mask);
3188 
3189  nesqp->hte_index &= adapter->hte_index_mask;
3190  nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3191 
3192  cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3193 
3194  nes_debug(NES_DBG_CM, "QP%u, Destination IP = 0x%08X:0x%04X, local = "
3195  "0x%08X:0x%04X, rcv_nxt=0x%08X, snd_nxt=0x%08X, mpa + "
3196  "private data length=%u.\n", nesqp->hwqp.qp_id,
3197  ntohl(cm_id->remote_addr.sin_addr.s_addr),
3198  ntohs(cm_id->remote_addr.sin_port),
3199  ntohl(cm_id->local_addr.sin_addr.s_addr),
3200  ntohs(cm_id->local_addr.sin_port),
3201  le32_to_cpu(nesqp->nesqp_context->rcv_nxt),
3202  le32_to_cpu(nesqp->nesqp_context->snd_nxt),
3203  buff_len);
3204 
3205  /* notify OF layer that accept event was successful */
3206  cm_id->add_ref(cm_id);
3207  nes_add_ref(&nesqp->ibqp);
3208 
3209  cm_event.event = IW_CM_EVENT_ESTABLISHED;
3210  cm_event.status = 0;
3211  cm_event.provider_data = (void *)nesqp;
3212  cm_event.local_addr = cm_id->local_addr;
3213  cm_event.remote_addr = cm_id->remote_addr;
3214  cm_event.private_data = NULL;
3215  cm_event.private_data_len = 0;
3216  ret = cm_id->event_handler(cm_id, &cm_event);
3217  attr.qp_state = IB_QPS_RTS;
3218  nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
3219  if (cm_node->loopbackpartner) {
3220  cm_node->loopbackpartner->mpa_frame_size =
3221  nesqp->private_data_len;
3222  /* copy entire MPA frame to our cm_node's frame */
3223  memcpy(cm_node->loopbackpartner->mpa_frame_buf,
3224  conn_param->private_data, conn_param->private_data_len);
3225  create_event(cm_node->loopbackpartner, NES_CM_EVENT_CONNECTED);
3226  }
3227  if (ret)
3228  printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3229  "ret=%d\n", __func__, __LINE__, ret);
3230 
3231  return 0;
3232 }
3233 
3234 
3238 int nes_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
3239 {
3240  struct nes_cm_node *cm_node;
3241  struct nes_cm_node *loopback;
3242  struct nes_cm_core *cm_core;
3243  u8 *start_buff;
3244 
3245  atomic_inc(&cm_rejects);
3246  cm_node = (struct nes_cm_node *)cm_id->provider_data;
3247  loopback = cm_node->loopbackpartner;
3248  cm_core = cm_node->cm_core;
3249  cm_node->cm_id = cm_id;
3250 
3251  if (pdata_len + sizeof(struct ietf_mpa_v2) > MAX_CM_BUFFER)
3252  return -EINVAL;
3253 
3254  if (loopback) {
3255  memcpy(&loopback->mpa_frame.priv_data, pdata, pdata_len);
3256  loopback->mpa_frame.priv_data_len = pdata_len;
3257  loopback->mpa_frame_size = pdata_len;
3258  } else {
3259  start_buff = &cm_node->mpa_frame_buf[0] + sizeof(struct ietf_mpa_v2);
3260  cm_node->mpa_frame_size = pdata_len;
3261  memcpy(start_buff, pdata, pdata_len);
3262  }
3263  return cm_core->api->reject(cm_core, cm_node);
3264 }
3265 
3266 
3271 int nes_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
3272 {
3273  struct ib_qp *ibqp;
3274  struct nes_qp *nesqp;
3275  struct nes_vnic *nesvnic;
3276  struct nes_device *nesdev;
3277  struct nes_cm_node *cm_node;
3278  struct nes_cm_info cm_info;
3279  int apbvt_set = 0;
3280 
3281  ibqp = nes_get_qp(cm_id->device, conn_param->qpn);
3282  if (!ibqp)
3283  return -EINVAL;
3284  nesqp = to_nesqp(ibqp);
3285  if (!nesqp)
3286  return -EINVAL;
3287  nesvnic = to_nesvnic(nesqp->ibqp.device);
3288  if (!nesvnic)
3289  return -EINVAL;
3290  nesdev = nesvnic->nesdev;
3291  if (!nesdev)
3292  return -EINVAL;
3293 
3294  if (!(cm_id->local_addr.sin_port) || !(cm_id->remote_addr.sin_port))
3295  return -EINVAL;
3296 
3297  nes_debug(NES_DBG_CM, "QP%u, current IP = 0x%08X, Destination IP = "
3298  "0x%08X:0x%04X, local = 0x%08X:0x%04X.\n", nesqp->hwqp.qp_id,
3299  ntohl(nesvnic->local_ipaddr),
3300  ntohl(cm_id->remote_addr.sin_addr.s_addr),
3301  ntohs(cm_id->remote_addr.sin_port),
3302  ntohl(cm_id->local_addr.sin_addr.s_addr),
3303  ntohs(cm_id->local_addr.sin_port));
3304 
3305  atomic_inc(&cm_connects);
3306  nesqp->active_conn = 1;
3307 
3308  /* cache the cm_id in the qp */
3309  nesqp->cm_id = cm_id;
3310 
3311  cm_id->provider_data = nesqp;
3312 
3313  nesqp->private_data_len = conn_param->private_data_len;
3314  nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32((u32)conn_param->ord);
3315  /* space for rdma0 read msg */
3316  if (conn_param->ord == 0)
3317  nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(1);
3318 
3319  nes_debug(NES_DBG_CM, "requested ord = 0x%08X.\n", (u32)conn_param->ord);
3320  nes_debug(NES_DBG_CM, "mpa private data len =%u\n",
3321  conn_param->private_data_len);
3322 
3323  if (cm_id->local_addr.sin_addr.s_addr !=
3324  cm_id->remote_addr.sin_addr.s_addr) {
3325  nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
3326  PCI_FUNC(nesdev->pcidev->devfn), NES_MANAGE_APBVT_ADD);
3327  apbvt_set = 1;
3328  }
3329 
3330  /* set up the connection params for the node */
3331  cm_info.loc_addr = htonl(cm_id->local_addr.sin_addr.s_addr);
3332  cm_info.loc_port = htons(cm_id->local_addr.sin_port);
3333  cm_info.rem_addr = htonl(cm_id->remote_addr.sin_addr.s_addr);
3334  cm_info.rem_port = htons(cm_id->remote_addr.sin_port);
3335  cm_info.cm_id = cm_id;
3337 
3338  cm_id->add_ref(cm_id);
3339 
3340  /* create a connect CM node connection */
3341  cm_node = g_cm_core->api->connect(g_cm_core, nesvnic,
3342  conn_param->private_data_len, (void *)conn_param->private_data,
3343  &cm_info);
3344  if (!cm_node) {
3345  if (apbvt_set)
3346  nes_manage_apbvt(nesvnic, ntohs(cm_id->local_addr.sin_port),
3347  PCI_FUNC(nesdev->pcidev->devfn),
3349 
3350  cm_id->rem_ref(cm_id);
3351  return -ENOMEM;
3352  }
3353 
3354  cm_node->apbvt_set = apbvt_set;
3355  nesqp->cm_node = cm_node;
3356  cm_node->nesqp = nesqp;
3357  nes_add_ref(&nesqp->ibqp);
3358 
3359  return 0;
3360 }
3361 
3362 
3366 int nes_create_listen(struct iw_cm_id *cm_id, int backlog)
3367 {
3368  struct nes_vnic *nesvnic;
3369  struct nes_cm_listener *cm_node;
3370  struct nes_cm_info cm_info;
3371  int err;
3372 
3373  nes_debug(NES_DBG_CM, "cm_id = %p, local port = 0x%04X.\n",
3374  cm_id, ntohs(cm_id->local_addr.sin_port));
3375 
3376  nesvnic = to_nesvnic(cm_id->device);
3377  if (!nesvnic)
3378  return -EINVAL;
3379 
3380  nes_debug(NES_DBG_CM, "nesvnic=%p, netdev=%p, %s\n",
3381  nesvnic, nesvnic->netdev, nesvnic->netdev->name);
3382 
3383  nes_debug(NES_DBG_CM, "nesvnic->local_ipaddr=0x%08x, sin_addr.s_addr=0x%08x\n",
3384  nesvnic->local_ipaddr, cm_id->local_addr.sin_addr.s_addr);
3385 
3386  /* setup listen params in our api call struct */
3387  cm_info.loc_addr = nesvnic->local_ipaddr;
3388  cm_info.loc_port = cm_id->local_addr.sin_port;
3389  cm_info.backlog = backlog;
3390  cm_info.cm_id = cm_id;
3391 
3393 
3394 
3395  cm_node = g_cm_core->api->listen(g_cm_core, nesvnic, &cm_info);
3396  if (!cm_node) {
3397  printk(KERN_ERR "%s[%u] Error returned from listen API call\n",
3398  __func__, __LINE__);
3399  return -ENOMEM;
3400  }
3401 
3402  cm_id->provider_data = cm_node;
3403 
3404  if (!cm_node->reused_node) {
3405  err = nes_manage_apbvt(nesvnic,
3406  ntohs(cm_id->local_addr.sin_port),
3407  PCI_FUNC(nesvnic->nesdev->pcidev->devfn),
3409  if (err) {
3410  printk(KERN_ERR "nes_manage_apbvt call returned %d.\n",
3411  err);
3412  g_cm_core->api->stop_listener(g_cm_core, (void *)cm_node);
3413  return err;
3414  }
3415  atomic_inc(&cm_listens_created);
3416  }
3417 
3418  cm_id->add_ref(cm_id);
3419  cm_id->provider_data = (void *)cm_node;
3420 
3421 
3422  return 0;
3423 }
3424 
3425 
3429 int nes_destroy_listen(struct iw_cm_id *cm_id)
3430 {
3431  if (cm_id->provider_data)
3432  g_cm_core->api->stop_listener(g_cm_core, cm_id->provider_data);
3433  else
3434  nes_debug(NES_DBG_CM, "cm_id->provider_data was NULL\n");
3435 
3436  cm_id->rem_ref(cm_id);
3437 
3438  return 0;
3439 }
3440 
3441 
3445 int nes_cm_recv(struct sk_buff *skb, struct net_device *netdevice)
3446 {
3447  int rc = 0;
3448 
3450  if ((g_cm_core) && (g_cm_core->api))
3451  rc = g_cm_core->api->recv_pkt(g_cm_core, netdev_priv(netdevice), skb);
3452  else
3453  nes_debug(NES_DBG_CM, "Unable to process packet for CM,"
3454  " cm is not setup properly.\n");
3455 
3456  return rc;
3457 }
3458 
3459 
3464 int nes_cm_start(void)
3465 {
3466  nes_debug(NES_DBG_CM, "\n");
3467  /* create the primary CM core, pass this handle to subsequent core inits */
3468  g_cm_core = nes_cm_alloc_core();
3469  if (g_cm_core)
3470  return 0;
3471  else
3472  return -ENOMEM;
3473 }
3474 
3475 
3480 int nes_cm_stop(void)
3481 {
3482  g_cm_core->api->destroy_cm_core(g_cm_core);
3483  return 0;
3484 }
3485 
3486 
3491 static void cm_event_connected(struct nes_cm_event *event)
3492 {
3493  struct nes_qp *nesqp;
3494  struct nes_vnic *nesvnic;
3495  struct nes_device *nesdev;
3496  struct nes_cm_node *cm_node;
3497  struct nes_adapter *nesadapter;
3498  struct ib_qp_attr attr;
3499  struct iw_cm_id *cm_id;
3500  struct iw_cm_event cm_event;
3501  struct nes_v4_quad nes_quad;
3502  u32 crc_value;
3503  int ret;
3504 
3505  /* get all our handles */
3506  cm_node = event->cm_node;
3507  cm_id = cm_node->cm_id;
3508  nes_debug(NES_DBG_CM, "cm_event_connected - %p - cm_id = %p\n", cm_node, cm_id);
3509  nesqp = (struct nes_qp *)cm_id->provider_data;
3510  nesvnic = to_nesvnic(nesqp->ibqp.device);
3511  nesdev = nesvnic->nesdev;
3512  nesadapter = nesdev->nesadapter;
3513 
3514  if (nesqp->destroyed)
3515  return;
3516  atomic_inc(&cm_connecteds);
3517  nes_debug(NES_DBG_CM, "QP%u attempting to connect to 0x%08X:0x%04X on"
3518  " local port 0x%04X. jiffies = %lu.\n",
3519  nesqp->hwqp.qp_id,
3520  ntohl(cm_id->remote_addr.sin_addr.s_addr),
3521  ntohs(cm_id->remote_addr.sin_port),
3522  ntohs(cm_id->local_addr.sin_port),
3523  jiffies);
3524 
3525  nes_cm_init_tsa_conn(nesqp, cm_node);
3526 
3527  /* set the QP tsa context */
3528  nesqp->nesqp_context->tcpPorts[0] =
3529  cpu_to_le16(ntohs(cm_id->local_addr.sin_port));
3530  nesqp->nesqp_context->tcpPorts[1] =
3531  cpu_to_le16(ntohs(cm_id->remote_addr.sin_port));
3532  nesqp->nesqp_context->ip0 =
3533  cpu_to_le32(ntohl(cm_id->remote_addr.sin_addr.s_addr));
3534 
3535  nesqp->nesqp_context->misc2 |= cpu_to_le32(
3536  (u32)PCI_FUNC(nesdev->pcidev->devfn) <<
3538  nesqp->nesqp_context->arp_index_vlan |= cpu_to_le32(
3539  nes_arp_table(nesdev,
3540  le32_to_cpu(nesqp->nesqp_context->ip0),
3541  NULL, NES_ARP_RESOLVE) << 16);
3542  nesqp->nesqp_context->ts_val_delta = cpu_to_le32(
3543  jiffies - nes_read_indexed(nesdev, NES_IDX_TCP_NOW));
3544  nesqp->nesqp_context->ird_index = cpu_to_le32(nesqp->hwqp.qp_id);
3545  nesqp->nesqp_context->ird_ord_sizes |=
3546  cpu_to_le32((u32)1 <<
3548 
3549  /* Adjust tail for not having a LSMM */
3550  /*nesqp->hwqp.sq_tail = 1;*/
3551 
3552  build_rdma0_msg(cm_node, &nesqp);
3553 
3554  nes_write32(nesdev->regs + NES_WQE_ALLOC,
3555  (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3556 
3557  memset(&nes_quad, 0, sizeof(nes_quad));
3558 
3559  nes_quad.DstIpAdrIndex =
3560  cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
3561  nes_quad.SrcIpadr = cm_id->remote_addr.sin_addr.s_addr;
3562  nes_quad.TcpPorts[0] = cm_id->remote_addr.sin_port;
3563  nes_quad.TcpPorts[1] = cm_id->local_addr.sin_port;
3564 
3565  /* Produce hash key */
3566  crc_value = get_crc_value(&nes_quad);
3567  nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
3568  nes_debug(NES_DBG_CM, "HTE Index = 0x%08X, After CRC = 0x%08X\n",
3569  nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
3570 
3571  nesqp->hte_index &= nesadapter->hte_index_mask;
3572  nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
3573 
3574  nesqp->ietf_frame = &cm_node->mpa_frame;
3575  nesqp->private_data_len = (u8)cm_node->mpa_frame_size;
3576  cm_node->cm_core->api->accelerated(cm_node->cm_core, cm_node);
3577 
3578  /* notify OF layer we successfully created the requested connection */
3579  cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3580  cm_event.status = 0;
3581  cm_event.provider_data = cm_id->provider_data;
3582  cm_event.local_addr.sin_family = AF_INET;
3583  cm_event.local_addr.sin_port = cm_id->local_addr.sin_port;
3584  cm_event.remote_addr = cm_id->remote_addr;
3585 
3586  cm_event.private_data = (void *)event->cm_node->mpa_frame_buf;
3587  cm_event.private_data_len = (u8)event->cm_node->mpa_frame_size;
3588  cm_event.ird = cm_node->ird_size;
3589  cm_event.ord = cm_node->ord_size;
3590 
3591  cm_event.local_addr.sin_addr.s_addr = event->cm_info.rem_addr;
3592  ret = cm_id->event_handler(cm_id, &cm_event);
3593  nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3594 
3595  if (ret)
3596  printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3597  "ret=%d\n", __func__, __LINE__, ret);
3598  attr.qp_state = IB_QPS_RTS;
3599  nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
3600 
3601  nes_debug(NES_DBG_CM, "Exiting connect thread for QP%u. jiffies = "
3602  "%lu\n", nesqp->hwqp.qp_id, jiffies);
3603 
3604  return;
3605 }
3606 
3607 
3611 static void cm_event_connect_error(struct nes_cm_event *event)
3612 {
3613  struct nes_qp *nesqp;
3614  struct iw_cm_id *cm_id;
3615  struct iw_cm_event cm_event;
3616  /* struct nes_cm_info cm_info; */
3617  int ret;
3618 
3619  if (!event->cm_node)
3620  return;
3621 
3622  cm_id = event->cm_node->cm_id;
3623  if (!cm_id)
3624  return;
3625 
3626  nes_debug(NES_DBG_CM, "cm_node=%p, cm_id=%p\n", event->cm_node, cm_id);
3627  nesqp = cm_id->provider_data;
3628 
3629  if (!nesqp)
3630  return;
3631 
3632  /* notify OF layer about this connection error event */
3633  /* cm_id->rem_ref(cm_id); */
3634  nesqp->cm_id = NULL;
3635  cm_id->provider_data = NULL;
3636  cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3637  cm_event.status = -ECONNRESET;
3638  cm_event.provider_data = cm_id->provider_data;
3639  cm_event.local_addr = cm_id->local_addr;
3640  cm_event.remote_addr = cm_id->remote_addr;
3641  cm_event.private_data = NULL;
3642  cm_event.private_data_len = 0;
3643 
3644  nes_debug(NES_DBG_CM, "call CM_EVENT REJECTED, local_addr=%08x, "
3645  "remove_addr=%08x\n", cm_event.local_addr.sin_addr.s_addr,
3646  cm_event.remote_addr.sin_addr.s_addr);
3647 
3648  ret = cm_id->event_handler(cm_id, &cm_event);
3649  nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3650  if (ret)
3651  printk(KERN_ERR "%s[%u] OFA CM event_handler returned, "
3652  "ret=%d\n", __func__, __LINE__, ret);
3653  cm_id->rem_ref(cm_id);
3654 
3655  rem_ref_cm_node(event->cm_node->cm_core, event->cm_node);
3656  return;
3657 }
3658 
3659 
3663 static void cm_event_reset(struct nes_cm_event *event)
3664 {
3665  struct nes_qp *nesqp;
3666  struct iw_cm_id *cm_id;
3667  struct iw_cm_event cm_event;
3668  /* struct nes_cm_info cm_info; */
3669  int ret;
3670 
3671  if (!event->cm_node)
3672  return;
3673 
3674  if (!event->cm_node->cm_id)
3675  return;
3676 
3677  cm_id = event->cm_node->cm_id;
3678 
3679  nes_debug(NES_DBG_CM, "%p - cm_id = %p\n", event->cm_node, cm_id);
3680  nesqp = cm_id->provider_data;
3681  if (!nesqp)
3682  return;
3683 
3684  nesqp->cm_id = NULL;
3685  /* cm_id->provider_data = NULL; */
3686  cm_event.event = IW_CM_EVENT_DISCONNECT;
3687  cm_event.status = -ECONNRESET;
3688  cm_event.provider_data = cm_id->provider_data;
3689  cm_event.local_addr = cm_id->local_addr;
3690  cm_event.remote_addr = cm_id->remote_addr;
3691  cm_event.private_data = NULL;
3692  cm_event.private_data_len = 0;
3693 
3694  cm_id->add_ref(cm_id);
3695  ret = cm_id->event_handler(cm_id, &cm_event);
3696  atomic_inc(&cm_closes);
3697  cm_event.event = IW_CM_EVENT_CLOSE;
3698  cm_event.status = 0;
3699  cm_event.provider_data = cm_id->provider_data;
3700  cm_event.local_addr = cm_id->local_addr;
3701  cm_event.remote_addr = cm_id->remote_addr;
3702  cm_event.private_data = NULL;
3703  cm_event.private_data_len = 0;
3704  nes_debug(NES_DBG_CM, "NODE %p Generating CLOSE\n", event->cm_node);
3705  ret = cm_id->event_handler(cm_id, &cm_event);
3706 
3707  nes_debug(NES_DBG_CM, "OFA CM event_handler returned, ret=%d\n", ret);
3708 
3709 
3710  /* notify OF layer about this connection error event */
3711  cm_id->rem_ref(cm_id);
3712 
3713  return;
3714 }
3715 
3716 
3720 static void cm_event_mpa_req(struct nes_cm_event *event)
3721 {
3722  struct iw_cm_id *cm_id;
3723  struct iw_cm_event cm_event;
3724  int ret;
3725  struct nes_cm_node *cm_node;
3726 
3727  cm_node = event->cm_node;
3728  if (!cm_node)
3729  return;
3730  cm_id = cm_node->cm_id;
3731 
3732  atomic_inc(&cm_connect_reqs);
3733  nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3734  cm_node, cm_id, jiffies);
3735 
3736  cm_event.event = IW_CM_EVENT_CONNECT_REQUEST;
3737  cm_event.status = 0;
3738  cm_event.provider_data = (void *)cm_node;
3739 
3740  cm_event.local_addr.sin_family = AF_INET;
3741  cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3742  cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3743 
3744  cm_event.remote_addr.sin_family = AF_INET;
3745  cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3746  cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3747  cm_event.private_data = cm_node->mpa_frame_buf;
3748  cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
3749  cm_event.ird = cm_node->ird_size;
3750  cm_event.ord = cm_node->ord_size;
3751 
3752  ret = cm_id->event_handler(cm_id, &cm_event);
3753  if (ret)
3754  printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3755  __func__, __LINE__, ret);
3756  return;
3757 }
3758 
3759 
3760 static void cm_event_mpa_reject(struct nes_cm_event *event)
3761 {
3762  struct iw_cm_id *cm_id;
3763  struct iw_cm_event cm_event;
3764  struct nes_cm_node *cm_node;
3765  int ret;
3766 
3767  cm_node = event->cm_node;
3768  if (!cm_node)
3769  return;
3770  cm_id = cm_node->cm_id;
3771 
3772  atomic_inc(&cm_connect_reqs);
3773  nes_debug(NES_DBG_CM, "cm_node = %p - cm_id = %p, jiffies = %lu\n",
3774  cm_node, cm_id, jiffies);
3775 
3776  cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
3777  cm_event.status = -ECONNREFUSED;
3778  cm_event.provider_data = cm_id->provider_data;
3779 
3780  cm_event.local_addr.sin_family = AF_INET;
3781  cm_event.local_addr.sin_port = htons(event->cm_info.loc_port);
3782  cm_event.local_addr.sin_addr.s_addr = htonl(event->cm_info.loc_addr);
3783 
3784  cm_event.remote_addr.sin_family = AF_INET;
3785  cm_event.remote_addr.sin_port = htons(event->cm_info.rem_port);
3786  cm_event.remote_addr.sin_addr.s_addr = htonl(event->cm_info.rem_addr);
3787 
3788  cm_event.private_data = cm_node->mpa_frame_buf;
3789  cm_event.private_data_len = (u8)cm_node->mpa_frame_size;
3790 
3791  nes_debug(NES_DBG_CM, "call CM_EVENT_MPA_REJECTED, local_addr=%08x, "
3792  "remove_addr=%08x\n",
3793  cm_event.local_addr.sin_addr.s_addr,
3794  cm_event.remote_addr.sin_addr.s_addr);
3795 
3796  ret = cm_id->event_handler(cm_id, &cm_event);
3797  if (ret)
3798  printk(KERN_ERR "%s[%u] OFA CM event_handler returned, ret=%d\n",
3799  __func__, __LINE__, ret);
3800 
3801  return;
3802 }
3803 
3804 
3805 static void nes_cm_event_handler(struct work_struct *);
3806 
3811 static int nes_cm_post_event(struct nes_cm_event *event)
3812 {
3813  atomic_inc(&event->cm_node->cm_core->events_posted);
3814  add_ref_cm_node(event->cm_node);
3815  event->cm_info.cm_id->add_ref(event->cm_info.cm_id);
3816  INIT_WORK(&event->event_work, nes_cm_event_handler);
3817  nes_debug(NES_DBG_CM, "cm_node=%p queue_work, event=%p\n",
3818  event->cm_node, event);
3819 
3820  queue_work(event->cm_node->cm_core->event_wq, &event->event_work);
3821 
3822  nes_debug(NES_DBG_CM, "Exit\n");
3823  return 0;
3824 }
3825 
3826 
3832 static void nes_cm_event_handler(struct work_struct *work)
3833 {
3834  struct nes_cm_event *event = container_of(work, struct nes_cm_event,
3835  event_work);
3836  struct nes_cm_core *cm_core;
3837 
3838  if ((!event) || (!event->cm_node) || (!event->cm_node->cm_core))
3839  return;
3840 
3841  cm_core = event->cm_node->cm_core;
3842  nes_debug(NES_DBG_CM, "event=%p, event->type=%u, events posted=%u\n",
3843  event, event->type, atomic_read(&cm_core->events_posted));
3844 
3845  switch (event->type) {
3846  case NES_CM_EVENT_MPA_REQ:
3847  cm_event_mpa_req(event);
3848  nes_debug(NES_DBG_CM, "cm_node=%p CM Event: MPA REQUEST\n",
3849  event->cm_node);
3850  break;
3851  case NES_CM_EVENT_RESET:
3852  nes_debug(NES_DBG_CM, "cm_node = %p CM Event: RESET\n",
3853  event->cm_node);
3854  cm_event_reset(event);
3855  break;
3857  if ((!event->cm_node->cm_id) ||
3858  (event->cm_node->state != NES_CM_STATE_TSA))
3859  break;
3860  cm_event_connected(event);
3861  nes_debug(NES_DBG_CM, "CM Event: CONNECTED\n");
3862  break;
3864  if ((!event->cm_node->cm_id) ||
3865  (event->cm_node->state == NES_CM_STATE_TSA))
3866  break;
3867  cm_event_mpa_reject(event);
3868  nes_debug(NES_DBG_CM, "CM Event: REJECT\n");
3869  break;
3870 
3871  case NES_CM_EVENT_ABORTED:
3872  if ((!event->cm_node->cm_id) ||
3873  (event->cm_node->state == NES_CM_STATE_TSA))
3874  break;
3875  cm_event_connect_error(event);
3876  nes_debug(NES_DBG_CM, "CM Event: ABORTED\n");
3877  break;
3879  nes_debug(NES_DBG_CM, "CM Event: DROPPED PKT\n");
3880  break;
3881  default:
3882  nes_debug(NES_DBG_CM, "CM Event: UNKNOWN EVENT TYPE\n");
3883  break;
3884  }
3885 
3886  atomic_dec(&cm_core->events_posted);
3887  event->cm_info.cm_id->rem_ref(event->cm_info.cm_id);
3888  rem_ref_cm_node(cm_core, event->cm_node);
3889  kfree(event);
3890 
3891  return;
3892 }