2 * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
32 #include <linux/module.h>
33 #include <linux/list.h>
34 #include <linux/workqueue.h>
35 #include <linux/skbuff.h>
36 #include <linux/timer.h>
37 #include <linux/notifier.h>
38 #include <linux/inetdevice.h>
40 #include <linux/tcp.h>
42 #include <net/neighbour.h>
43 #include <net/netevent.h>
44 #include <net/route.h>
48 static char *states[] = {
64 static int dack_mode = 1;
65 module_param(dack_mode, int, 0644);
66 MODULE_PARM_DESC(dack_mode, "Delayed ack mode (default=1)");
68 int c4iw_max_read_depth = 8;
69 module_param(c4iw_max_read_depth, int, 0644);
70 MODULE_PARM_DESC(c4iw_max_read_depth, "Per-connection max ORD/IRD (default=8)");
72 static int enable_tcp_timestamps;
73 module_param(enable_tcp_timestamps, int, 0644);
74 MODULE_PARM_DESC(enable_tcp_timestamps, "Enable tcp timestamps (default=0)");
76 static int enable_tcp_sack;
77 module_param(enable_tcp_sack, int, 0644);
78 MODULE_PARM_DESC(enable_tcp_sack, "Enable tcp SACK (default=0)");
80 static int enable_tcp_window_scaling = 1;
81 module_param(enable_tcp_window_scaling, int, 0644);
82 MODULE_PARM_DESC(enable_tcp_window_scaling,
83 "Enable tcp window scaling (default=1)");
86 module_param(c4iw_debug, int, 0644);
87 MODULE_PARM_DESC(c4iw_debug, "Enable debug logging (default=0)");
90 module_param(peer2peer, int, 0644);
91 MODULE_PARM_DESC(peer2peer, "Support peer2peer ULPs (default=0)");
93 static int p2p_type = FW_RI_INIT_P2PTYPE_READ_REQ;
94 module_param(p2p_type, int, 0644);
95 MODULE_PARM_DESC(p2p_type, "RDMAP opcode to use for the RTR message: "
96 "1=RDMA_READ 0=RDMA_WRITE (default 1)");
98 static int ep_timeout_secs = 60;
99 module_param(ep_timeout_secs, int, 0644);
100 MODULE_PARM_DESC(ep_timeout_secs, "CM Endpoint operation timeout "
101 "in seconds (default=60)");
103 static int mpa_rev = 1;
104 module_param(mpa_rev, int, 0644);
105 MODULE_PARM_DESC(mpa_rev, "MPA Revision, 0 supports amso1100, "
106 "1 is RFC0544 spec compliant, 2 is IETF MPA Peer Connect Draft"
107 " compliant (default=1)");
109 static int markers_enabled;
110 module_param(markers_enabled, int, 0644);
111 MODULE_PARM_DESC(markers_enabled, "Enable MPA MARKERS (default(0)=disabled)");
113 static int crc_enabled = 1;
114 module_param(crc_enabled, int, 0644);
115 MODULE_PARM_DESC(crc_enabled, "Enable MPA CRC (default(1)=enabled)");
117 static int rcv_win = 256 * 1024;
118 module_param(rcv_win, int, 0644);
119 MODULE_PARM_DESC(rcv_win, "TCP receive window in bytes (default=256KB)");
121 static int snd_win = 128 * 1024;
122 module_param(snd_win, int, 0644);
123 MODULE_PARM_DESC(snd_win, "TCP send window in bytes (default=128KB)");
125 static struct workqueue_struct *workq;
127 static struct sk_buff_head rxq;
129 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp);
130 static void ep_timeout(unsigned long arg);
131 static void connect_reply_upcall(struct c4iw_ep *ep, int status);
133 static LIST_HEAD(timeout_list);
134 static spinlock_t timeout_lock;
136 static void start_ep_timer(struct c4iw_ep *ep)
138 PDBG("%s ep %p\n", __func__, ep);
139 if (timer_pending(&ep->timer)) {
140 PDBG("%s stopped / restarted timer ep %p\n", __func__, ep);
141 del_timer_sync(&ep->timer);
143 c4iw_get_ep(&ep->com);
144 ep->timer.expires = jiffies + ep_timeout_secs * HZ;
145 ep->timer.data = (unsigned long)ep;
146 ep->timer.function = ep_timeout;
147 add_timer(&ep->timer);
150 static void stop_ep_timer(struct c4iw_ep *ep)
152 PDBG("%s ep %p\n", __func__, ep);
153 if (!timer_pending(&ep->timer)) {
154 printk(KERN_ERR "%s timer stopped when its not running! "
155 "ep %p state %u\n", __func__, ep, ep->com.state);
159 del_timer_sync(&ep->timer);
160 c4iw_put_ep(&ep->com);
163 static int c4iw_l2t_send(struct c4iw_rdev *rdev, struct sk_buff *skb,
164 struct l2t_entry *l2e)
168 if (c4iw_fatal_error(rdev)) {
170 PDBG("%s - device in error state - dropping\n", __func__);
173 error = cxgb4_l2t_send(rdev->lldi.ports[0], skb, l2e);
176 return error < 0 ? error : 0;
179 int c4iw_ofld_send(struct c4iw_rdev *rdev, struct sk_buff *skb)
183 if (c4iw_fatal_error(rdev)) {
185 PDBG("%s - device in error state - dropping\n", __func__);
188 error = cxgb4_ofld_send(rdev->lldi.ports[0], skb);
191 return error < 0 ? error : 0;
194 static void release_tid(struct c4iw_rdev *rdev, u32 hwtid, struct sk_buff *skb)
196 struct cpl_tid_release *req;
198 skb = get_skb(skb, sizeof *req, GFP_KERNEL);
201 req = (struct cpl_tid_release *) skb_put(skb, sizeof(*req));
202 INIT_TP_WR(req, hwtid);
203 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_TID_RELEASE, hwtid));
204 set_wr_txq(skb, CPL_PRIORITY_SETUP, 0);
205 c4iw_ofld_send(rdev, skb);
209 static void set_emss(struct c4iw_ep *ep, u16 opt)
211 ep->emss = ep->com.dev->rdev.lldi.mtus[GET_TCPOPT_MSS(opt)] - 40;
213 if (GET_TCPOPT_TSTAMP(opt))
217 PDBG("%s mss_idx %u mss %u emss=%u\n", __func__, GET_TCPOPT_MSS(opt),
221 static enum c4iw_ep_state state_read(struct c4iw_ep_common *epc)
223 enum c4iw_ep_state state;
225 mutex_lock(&epc->mutex);
227 mutex_unlock(&epc->mutex);
231 static void __state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
236 static void state_set(struct c4iw_ep_common *epc, enum c4iw_ep_state new)
238 mutex_lock(&epc->mutex);
239 PDBG("%s - %s -> %s\n", __func__, states[epc->state], states[new]);
240 __state_set(epc, new);
241 mutex_unlock(&epc->mutex);
245 static void *alloc_ep(int size, gfp_t gfp)
247 struct c4iw_ep_common *epc;
249 epc = kzalloc(size, gfp);
251 kref_init(&epc->kref);
252 mutex_init(&epc->mutex);
253 c4iw_init_wr_wait(&epc->wr_wait);
255 PDBG("%s alloc ep %p\n", __func__, epc);
259 void _c4iw_free_ep(struct kref *kref)
263 ep = container_of(kref, struct c4iw_ep, com.kref);
264 PDBG("%s ep %p state %s\n", __func__, ep, states[state_read(&ep->com)]);
265 if (test_bit(RELEASE_RESOURCES, &ep->com.flags)) {
266 cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid);
267 dst_release(ep->dst);
268 cxgb4_l2t_release(ep->l2t);
273 static void release_ep_resources(struct c4iw_ep *ep)
275 set_bit(RELEASE_RESOURCES, &ep->com.flags);
276 c4iw_put_ep(&ep->com);
279 static int status2errno(int status)
284 case CPL_ERR_CONN_RESET:
286 case CPL_ERR_ARP_MISS:
287 return -EHOSTUNREACH;
288 case CPL_ERR_CONN_TIMEDOUT:
290 case CPL_ERR_TCAM_FULL:
292 case CPL_ERR_CONN_EXIST:
300 * Try and reuse skbs already allocated...
302 static struct sk_buff *get_skb(struct sk_buff *skb, int len, gfp_t gfp)
304 if (skb && !skb_is_nonlinear(skb) && !skb_cloned(skb)) {
307 skb_reset_transport_header(skb);
309 skb = alloc_skb(len, gfp);
314 static struct rtable *find_route(struct c4iw_dev *dev, __be32 local_ip,
315 __be32 peer_ip, __be16 local_port,
316 __be16 peer_port, u8 tos)
321 rt = ip_route_output_ports(&init_net, &fl4, NULL, peer_ip, local_ip,
322 peer_port, local_port, IPPROTO_TCP,
329 static void arp_failure_discard(void *handle, struct sk_buff *skb)
331 PDBG("%s c4iw_dev %p\n", __func__, handle);
336 * Handle an ARP failure for an active open.
338 static void act_open_req_arp_failure(void *handle, struct sk_buff *skb)
340 printk(KERN_ERR MOD "ARP failure duing connect\n");
345 * Handle an ARP failure for a CPL_ABORT_REQ. Change it into a no RST variant
348 static void abort_arp_failure(void *handle, struct sk_buff *skb)
350 struct c4iw_rdev *rdev = handle;
351 struct cpl_abort_req *req = cplhdr(skb);
353 PDBG("%s rdev %p\n", __func__, rdev);
354 req->cmd = CPL_ABORT_NO_RST;
355 c4iw_ofld_send(rdev, skb);
358 static void send_flowc(struct c4iw_ep *ep, struct sk_buff *skb)
360 unsigned int flowclen = 80;
361 struct fw_flowc_wr *flowc;
364 skb = get_skb(skb, flowclen, GFP_KERNEL);
365 flowc = (struct fw_flowc_wr *)__skb_put(skb, flowclen);
367 flowc->op_to_nparams = cpu_to_be32(FW_WR_OP(FW_FLOWC_WR) |
368 FW_FLOWC_WR_NPARAMS(8));
369 flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16(DIV_ROUND_UP(flowclen,
370 16)) | FW_WR_FLOWID(ep->hwtid));
372 flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
373 flowc->mnemval[0].val = cpu_to_be32(PCI_FUNC(ep->com.dev->rdev.lldi.pdev->devfn) << 8);
374 flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
375 flowc->mnemval[1].val = cpu_to_be32(ep->tx_chan);
376 flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
377 flowc->mnemval[2].val = cpu_to_be32(ep->tx_chan);
378 flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
379 flowc->mnemval[3].val = cpu_to_be32(ep->rss_qid);
380 flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
381 flowc->mnemval[4].val = cpu_to_be32(ep->snd_seq);
382 flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
383 flowc->mnemval[5].val = cpu_to_be32(ep->rcv_seq);
384 flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
385 flowc->mnemval[6].val = cpu_to_be32(snd_win);
386 flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
387 flowc->mnemval[7].val = cpu_to_be32(ep->emss);
388 /* Pad WR to 16 byte boundary */
389 flowc->mnemval[8].mnemonic = 0;
390 flowc->mnemval[8].val = 0;
391 for (i = 0; i < 9; i++) {
392 flowc->mnemval[i].r4[0] = 0;
393 flowc->mnemval[i].r4[1] = 0;
394 flowc->mnemval[i].r4[2] = 0;
397 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
398 c4iw_ofld_send(&ep->com.dev->rdev, skb);
401 static int send_halfclose(struct c4iw_ep *ep, gfp_t gfp)
403 struct cpl_close_con_req *req;
405 int wrlen = roundup(sizeof *req, 16);
407 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
408 skb = get_skb(NULL, wrlen, gfp);
410 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
413 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
414 t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
415 req = (struct cpl_close_con_req *) skb_put(skb, wrlen);
416 memset(req, 0, wrlen);
417 INIT_TP_WR(req, ep->hwtid);
418 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_CLOSE_CON_REQ,
420 return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
423 static int send_abort(struct c4iw_ep *ep, struct sk_buff *skb, gfp_t gfp)
425 struct cpl_abort_req *req;
426 int wrlen = roundup(sizeof *req, 16);
428 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
429 skb = get_skb(skb, wrlen, gfp);
431 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
435 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
436 t4_set_arp_err_handler(skb, &ep->com.dev->rdev, abort_arp_failure);
437 req = (struct cpl_abort_req *) skb_put(skb, wrlen);
438 memset(req, 0, wrlen);
439 INIT_TP_WR(req, ep->hwtid);
440 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_REQ, ep->hwtid));
441 req->cmd = CPL_ABORT_SEND_RST;
442 return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
445 static int send_connect(struct c4iw_ep *ep)
447 struct cpl_act_open_req *req;
451 unsigned int mtu_idx;
453 int wrlen = roundup(sizeof *req, 16);
455 PDBG("%s ep %p atid %u\n", __func__, ep, ep->atid);
457 skb = get_skb(NULL, wrlen, GFP_KERNEL);
459 printk(KERN_ERR MOD "%s - failed to alloc skb.\n",
463 set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
465 cxgb4_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx);
466 wscale = compute_wscale(rcv_win);
467 opt0 = KEEP_ALIVE(1) |
471 L2T_IDX(ep->l2t->idx) |
472 TX_CHAN(ep->tx_chan) |
473 SMAC_SEL(ep->smac_idx) |
475 ULP_MODE(ULP_MODE_TCPDDP) |
476 RCV_BUFSIZ(rcv_win>>10);
477 opt2 = RX_CHANNEL(0) |
478 RSS_QUEUE_VALID | RSS_QUEUE(ep->rss_qid);
479 if (enable_tcp_timestamps)
480 opt2 |= TSTAMPS_EN(1);
483 if (wscale && enable_tcp_window_scaling)
484 opt2 |= WND_SCALE_EN(1);
485 t4_set_arp_err_handler(skb, NULL, act_open_req_arp_failure);
487 req = (struct cpl_act_open_req *) skb_put(skb, wrlen);
489 OPCODE_TID(req) = cpu_to_be32(
490 MK_OPCODE_TID(CPL_ACT_OPEN_REQ, ((ep->rss_qid<<14)|ep->atid)));
491 req->local_port = ep->com.local_addr.sin_port;
492 req->peer_port = ep->com.remote_addr.sin_port;
493 req->local_ip = ep->com.local_addr.sin_addr.s_addr;
494 req->peer_ip = ep->com.remote_addr.sin_addr.s_addr;
495 req->opt0 = cpu_to_be64(opt0);
497 req->opt2 = cpu_to_be32(opt2);
498 return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
501 static void send_mpa_req(struct c4iw_ep *ep, struct sk_buff *skb,
505 struct fw_ofld_tx_data_wr *req;
506 struct mpa_message *mpa;
507 struct mpa_v2_conn_params mpa_v2_params;
509 PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
511 BUG_ON(skb_cloned(skb));
513 mpalen = sizeof(*mpa) + ep->plen;
514 if (mpa_rev_to_use == 2)
515 mpalen += sizeof(struct mpa_v2_conn_params);
516 wrlen = roundup(mpalen + sizeof *req, 16);
517 skb = get_skb(skb, wrlen, GFP_KERNEL);
519 connect_reply_upcall(ep, -ENOMEM);
522 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
524 req = (struct fw_ofld_tx_data_wr *)skb_put(skb, wrlen);
525 memset(req, 0, wrlen);
526 req->op_to_immdlen = cpu_to_be32(
527 FW_WR_OP(FW_OFLD_TX_DATA_WR) |
529 FW_WR_IMMDLEN(mpalen));
530 req->flowid_len16 = cpu_to_be32(
531 FW_WR_FLOWID(ep->hwtid) |
532 FW_WR_LEN16(wrlen >> 4));
533 req->plen = cpu_to_be32(mpalen);
534 req->tunnel_to_proxy = cpu_to_be32(
535 FW_OFLD_TX_DATA_WR_FLUSH(1) |
536 FW_OFLD_TX_DATA_WR_SHOVE(1));
538 mpa = (struct mpa_message *)(req + 1);
539 memcpy(mpa->key, MPA_KEY_REQ, sizeof(mpa->key));
540 mpa->flags = (crc_enabled ? MPA_CRC : 0) |
541 (markers_enabled ? MPA_MARKERS : 0) |
542 (mpa_rev_to_use == 2 ? MPA_ENHANCED_RDMA_CONN : 0);
543 mpa->private_data_size = htons(ep->plen);
544 mpa->revision = mpa_rev_to_use;
545 if (mpa_rev_to_use == 1) {
546 ep->tried_with_mpa_v1 = 1;
547 ep->retry_with_mpa_v1 = 0;
550 if (mpa_rev_to_use == 2) {
551 mpa->private_data_size +=
552 htons(sizeof(struct mpa_v2_conn_params));
553 mpa_v2_params.ird = htons((u16)ep->ird);
554 mpa_v2_params.ord = htons((u16)ep->ord);
557 mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
558 if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE)
560 htons(MPA_V2_RDMA_WRITE_RTR);
561 else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ)
563 htons(MPA_V2_RDMA_READ_RTR);
565 memcpy(mpa->private_data, &mpa_v2_params,
566 sizeof(struct mpa_v2_conn_params));
569 memcpy(mpa->private_data +
570 sizeof(struct mpa_v2_conn_params),
571 ep->mpa_pkt + sizeof(*mpa), ep->plen);
574 memcpy(mpa->private_data,
575 ep->mpa_pkt + sizeof(*mpa), ep->plen);
578 * Reference the mpa skb. This ensures the data area
579 * will remain in memory until the hw acks the tx.
580 * Function fw4_ack() will deref it.
583 t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
586 c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
588 state_set(&ep->com, MPA_REQ_SENT);
589 ep->mpa_attr.initiator = 1;
593 static int send_mpa_reject(struct c4iw_ep *ep, const void *pdata, u8 plen)
596 struct fw_ofld_tx_data_wr *req;
597 struct mpa_message *mpa;
599 struct mpa_v2_conn_params mpa_v2_params;
601 PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
603 mpalen = sizeof(*mpa) + plen;
604 if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
605 mpalen += sizeof(struct mpa_v2_conn_params);
606 wrlen = roundup(mpalen + sizeof *req, 16);
608 skb = get_skb(NULL, wrlen, GFP_KERNEL);
610 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
613 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
615 req = (struct fw_ofld_tx_data_wr *)skb_put(skb, wrlen);
616 memset(req, 0, wrlen);
617 req->op_to_immdlen = cpu_to_be32(
618 FW_WR_OP(FW_OFLD_TX_DATA_WR) |
620 FW_WR_IMMDLEN(mpalen));
621 req->flowid_len16 = cpu_to_be32(
622 FW_WR_FLOWID(ep->hwtid) |
623 FW_WR_LEN16(wrlen >> 4));
624 req->plen = cpu_to_be32(mpalen);
625 req->tunnel_to_proxy = cpu_to_be32(
626 FW_OFLD_TX_DATA_WR_FLUSH(1) |
627 FW_OFLD_TX_DATA_WR_SHOVE(1));
629 mpa = (struct mpa_message *)(req + 1);
630 memset(mpa, 0, sizeof(*mpa));
631 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
632 mpa->flags = MPA_REJECT;
633 mpa->revision = mpa_rev;
634 mpa->private_data_size = htons(plen);
636 if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
637 mpa->flags |= MPA_ENHANCED_RDMA_CONN;
638 mpa->private_data_size +=
639 htons(sizeof(struct mpa_v2_conn_params));
640 mpa_v2_params.ird = htons(((u16)ep->ird) |
641 (peer2peer ? MPA_V2_PEER2PEER_MODEL :
643 mpa_v2_params.ord = htons(((u16)ep->ord) | (peer2peer ?
645 FW_RI_INIT_P2PTYPE_RDMA_WRITE ?
646 MPA_V2_RDMA_WRITE_RTR : p2p_type ==
647 FW_RI_INIT_P2PTYPE_READ_REQ ?
648 MPA_V2_RDMA_READ_RTR : 0) : 0));
649 memcpy(mpa->private_data, &mpa_v2_params,
650 sizeof(struct mpa_v2_conn_params));
653 memcpy(mpa->private_data +
654 sizeof(struct mpa_v2_conn_params), pdata, plen);
657 memcpy(mpa->private_data, pdata, plen);
660 * Reference the mpa skb again. This ensures the data area
661 * will remain in memory until the hw acks the tx.
662 * Function fw4_ack() will deref it.
665 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
666 t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
669 return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
672 static int send_mpa_reply(struct c4iw_ep *ep, const void *pdata, u8 plen)
675 struct fw_ofld_tx_data_wr *req;
676 struct mpa_message *mpa;
678 struct mpa_v2_conn_params mpa_v2_params;
680 PDBG("%s ep %p tid %u pd_len %d\n", __func__, ep, ep->hwtid, ep->plen);
682 mpalen = sizeof(*mpa) + plen;
683 if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn)
684 mpalen += sizeof(struct mpa_v2_conn_params);
685 wrlen = roundup(mpalen + sizeof *req, 16);
687 skb = get_skb(NULL, wrlen, GFP_KERNEL);
689 printk(KERN_ERR MOD "%s - cannot alloc skb!\n", __func__);
692 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
694 req = (struct fw_ofld_tx_data_wr *) skb_put(skb, wrlen);
695 memset(req, 0, wrlen);
696 req->op_to_immdlen = cpu_to_be32(
697 FW_WR_OP(FW_OFLD_TX_DATA_WR) |
699 FW_WR_IMMDLEN(mpalen));
700 req->flowid_len16 = cpu_to_be32(
701 FW_WR_FLOWID(ep->hwtid) |
702 FW_WR_LEN16(wrlen >> 4));
703 req->plen = cpu_to_be32(mpalen);
704 req->tunnel_to_proxy = cpu_to_be32(
705 FW_OFLD_TX_DATA_WR_FLUSH(1) |
706 FW_OFLD_TX_DATA_WR_SHOVE(1));
708 mpa = (struct mpa_message *)(req + 1);
709 memset(mpa, 0, sizeof(*mpa));
710 memcpy(mpa->key, MPA_KEY_REP, sizeof(mpa->key));
711 mpa->flags = (ep->mpa_attr.crc_enabled ? MPA_CRC : 0) |
712 (markers_enabled ? MPA_MARKERS : 0);
713 mpa->revision = ep->mpa_attr.version;
714 mpa->private_data_size = htons(plen);
716 if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
717 mpa->flags |= MPA_ENHANCED_RDMA_CONN;
718 mpa->private_data_size +=
719 htons(sizeof(struct mpa_v2_conn_params));
720 mpa_v2_params.ird = htons((u16)ep->ird);
721 mpa_v2_params.ord = htons((u16)ep->ord);
722 if (peer2peer && (ep->mpa_attr.p2p_type !=
723 FW_RI_INIT_P2PTYPE_DISABLED)) {
724 mpa_v2_params.ird |= htons(MPA_V2_PEER2PEER_MODEL);
726 if (p2p_type == FW_RI_INIT_P2PTYPE_RDMA_WRITE)
728 htons(MPA_V2_RDMA_WRITE_RTR);
729 else if (p2p_type == FW_RI_INIT_P2PTYPE_READ_REQ)
731 htons(MPA_V2_RDMA_READ_RTR);
734 memcpy(mpa->private_data, &mpa_v2_params,
735 sizeof(struct mpa_v2_conn_params));
738 memcpy(mpa->private_data +
739 sizeof(struct mpa_v2_conn_params), pdata, plen);
742 memcpy(mpa->private_data, pdata, plen);
745 * Reference the mpa skb. This ensures the data area
746 * will remain in memory until the hw acks the tx.
747 * Function fw4_ack() will deref it.
750 t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
752 state_set(&ep->com, MPA_REP_SENT);
753 return c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
756 static int act_establish(struct c4iw_dev *dev, struct sk_buff *skb)
759 struct cpl_act_establish *req = cplhdr(skb);
760 unsigned int tid = GET_TID(req);
761 unsigned int atid = GET_TID_TID(ntohl(req->tos_atid));
762 struct tid_info *t = dev->rdev.lldi.tids;
764 ep = lookup_atid(t, atid);
766 PDBG("%s ep %p tid %u snd_isn %u rcv_isn %u\n", __func__, ep, tid,
767 be32_to_cpu(req->snd_isn), be32_to_cpu(req->rcv_isn));
769 dst_confirm(ep->dst);
771 /* setup the hwtid for this connection */
773 cxgb4_insert_tid(t, ep, tid);
775 ep->snd_seq = be32_to_cpu(req->snd_isn);
776 ep->rcv_seq = be32_to_cpu(req->rcv_isn);
778 set_emss(ep, ntohs(req->tcp_opt));
780 /* dealloc the atid */
781 cxgb4_free_atid(t, atid);
783 /* start MPA negotiation */
784 send_flowc(ep, NULL);
785 if (ep->retry_with_mpa_v1)
786 send_mpa_req(ep, skb, 1);
788 send_mpa_req(ep, skb, mpa_rev);
793 static void close_complete_upcall(struct c4iw_ep *ep)
795 struct iw_cm_event event;
797 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
798 memset(&event, 0, sizeof(event));
799 event.event = IW_CM_EVENT_CLOSE;
801 PDBG("close complete delivered ep %p cm_id %p tid %u\n",
802 ep, ep->com.cm_id, ep->hwtid);
803 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
804 ep->com.cm_id->rem_ref(ep->com.cm_id);
805 ep->com.cm_id = NULL;
810 static int abort_connection(struct c4iw_ep *ep, struct sk_buff *skb, gfp_t gfp)
812 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
813 close_complete_upcall(ep);
814 state_set(&ep->com, ABORTING);
815 return send_abort(ep, skb, gfp);
818 static void peer_close_upcall(struct c4iw_ep *ep)
820 struct iw_cm_event event;
822 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
823 memset(&event, 0, sizeof(event));
824 event.event = IW_CM_EVENT_DISCONNECT;
826 PDBG("peer close delivered ep %p cm_id %p tid %u\n",
827 ep, ep->com.cm_id, ep->hwtid);
828 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
832 static void peer_abort_upcall(struct c4iw_ep *ep)
834 struct iw_cm_event event;
836 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
837 memset(&event, 0, sizeof(event));
838 event.event = IW_CM_EVENT_CLOSE;
839 event.status = -ECONNRESET;
841 PDBG("abort delivered ep %p cm_id %p tid %u\n", ep,
842 ep->com.cm_id, ep->hwtid);
843 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
844 ep->com.cm_id->rem_ref(ep->com.cm_id);
845 ep->com.cm_id = NULL;
850 static void connect_reply_upcall(struct c4iw_ep *ep, int status)
852 struct iw_cm_event event;
854 PDBG("%s ep %p tid %u status %d\n", __func__, ep, ep->hwtid, status);
855 memset(&event, 0, sizeof(event));
856 event.event = IW_CM_EVENT_CONNECT_REPLY;
857 event.status = status;
858 event.local_addr = ep->com.local_addr;
859 event.remote_addr = ep->com.remote_addr;
861 if ((status == 0) || (status == -ECONNREFUSED)) {
862 if (!ep->tried_with_mpa_v1) {
863 /* this means MPA_v2 is used */
864 event.private_data_len = ep->plen -
865 sizeof(struct mpa_v2_conn_params);
866 event.private_data = ep->mpa_pkt +
867 sizeof(struct mpa_message) +
868 sizeof(struct mpa_v2_conn_params);
870 /* this means MPA_v1 is used */
871 event.private_data_len = ep->plen;
872 event.private_data = ep->mpa_pkt +
873 sizeof(struct mpa_message);
877 PDBG("%s ep %p tid %u status %d\n", __func__, ep,
879 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
882 ep->com.cm_id->rem_ref(ep->com.cm_id);
883 ep->com.cm_id = NULL;
888 static void connect_request_upcall(struct c4iw_ep *ep)
890 struct iw_cm_event event;
892 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
893 memset(&event, 0, sizeof(event));
894 event.event = IW_CM_EVENT_CONNECT_REQUEST;
895 event.local_addr = ep->com.local_addr;
896 event.remote_addr = ep->com.remote_addr;
897 event.provider_data = ep;
898 if (!ep->tried_with_mpa_v1) {
899 /* this means MPA_v2 is used */
902 event.private_data_len = ep->plen -
903 sizeof(struct mpa_v2_conn_params);
904 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message) +
905 sizeof(struct mpa_v2_conn_params);
907 /* this means MPA_v1 is used. Send max supported */
908 event.ord = c4iw_max_read_depth;
909 event.ird = c4iw_max_read_depth;
910 event.private_data_len = ep->plen;
911 event.private_data = ep->mpa_pkt + sizeof(struct mpa_message);
913 if (state_read(&ep->parent_ep->com) != DEAD) {
914 c4iw_get_ep(&ep->com);
915 ep->parent_ep->com.cm_id->event_handler(
916 ep->parent_ep->com.cm_id,
919 c4iw_put_ep(&ep->parent_ep->com);
920 ep->parent_ep = NULL;
923 static void established_upcall(struct c4iw_ep *ep)
925 struct iw_cm_event event;
927 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
928 memset(&event, 0, sizeof(event));
929 event.event = IW_CM_EVENT_ESTABLISHED;
933 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
934 ep->com.cm_id->event_handler(ep->com.cm_id, &event);
938 static int update_rx_credits(struct c4iw_ep *ep, u32 credits)
940 struct cpl_rx_data_ack *req;
942 int wrlen = roundup(sizeof *req, 16);
944 PDBG("%s ep %p tid %u credits %u\n", __func__, ep, ep->hwtid, credits);
945 skb = get_skb(NULL, wrlen, GFP_KERNEL);
947 printk(KERN_ERR MOD "update_rx_credits - cannot alloc skb!\n");
951 req = (struct cpl_rx_data_ack *) skb_put(skb, wrlen);
952 memset(req, 0, wrlen);
953 INIT_TP_WR(req, ep->hwtid);
954 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_RX_DATA_ACK,
956 req->credit_dack = cpu_to_be32(credits | RX_FORCE_ACK(1) |
958 V_RX_DACK_MODE(dack_mode));
959 set_wr_txq(skb, CPL_PRIORITY_ACK, ep->ctrlq_idx);
960 c4iw_ofld_send(&ep->com.dev->rdev, skb);
964 static void process_mpa_reply(struct c4iw_ep *ep, struct sk_buff *skb)
966 struct mpa_message *mpa;
967 struct mpa_v2_conn_params *mpa_v2_params;
969 u16 resp_ird, resp_ord;
970 u8 rtr_mismatch = 0, insuff_ird = 0;
971 struct c4iw_qp_attributes attrs;
972 enum c4iw_qp_attr_mask mask;
975 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
978 * Stop mpa timer. If it expired, then the state has
979 * changed and we bail since ep_timeout already aborted
983 if (state_read(&ep->com) != MPA_REQ_SENT)
987 * If we get more than the supported amount of private data
988 * then we must fail this connection.
990 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
996 * copy the new data into our accumulation buffer.
998 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
1000 ep->mpa_pkt_len += skb->len;
1003 * if we don't even have the mpa message, then bail.
1005 if (ep->mpa_pkt_len < sizeof(*mpa))
1007 mpa = (struct mpa_message *) ep->mpa_pkt;
1009 /* Validate MPA header. */
1010 if (mpa->revision > mpa_rev) {
1011 printk(KERN_ERR MOD "%s MPA version mismatch. Local = %d,"
1012 " Received = %d\n", __func__, mpa_rev, mpa->revision);
1016 if (memcmp(mpa->key, MPA_KEY_REP, sizeof(mpa->key))) {
1021 plen = ntohs(mpa->private_data_size);
1024 * Fail if there's too much private data.
1026 if (plen > MPA_MAX_PRIVATE_DATA) {
1032 * If plen does not account for pkt size
1034 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1039 ep->plen = (u8) plen;
1042 * If we don't have all the pdata yet, then bail.
1043 * We'll continue process when more data arrives.
1045 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1048 if (mpa->flags & MPA_REJECT) {
1049 err = -ECONNREFUSED;
1054 * If we get here we have accumulated the entire mpa
1055 * start reply message including private data. And
1056 * the MPA header is valid.
1058 state_set(&ep->com, FPDU_MODE);
1059 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1060 ep->mpa_attr.recv_marker_enabled = markers_enabled;
1061 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1062 ep->mpa_attr.version = mpa->revision;
1063 ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1065 if (mpa->revision == 2) {
1066 ep->mpa_attr.enhanced_rdma_conn =
1067 mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
1068 if (ep->mpa_attr.enhanced_rdma_conn) {
1069 mpa_v2_params = (struct mpa_v2_conn_params *)
1070 (ep->mpa_pkt + sizeof(*mpa));
1071 resp_ird = ntohs(mpa_v2_params->ird) &
1072 MPA_V2_IRD_ORD_MASK;
1073 resp_ord = ntohs(mpa_v2_params->ord) &
1074 MPA_V2_IRD_ORD_MASK;
1077 * This is a double-check. Ideally, below checks are
1078 * not required since ird/ord stuff has been taken
1079 * care of in c4iw_accept_cr
1081 if ((ep->ird < resp_ord) || (ep->ord > resp_ird)) {
1088 if (ntohs(mpa_v2_params->ird) &
1089 MPA_V2_PEER2PEER_MODEL) {
1090 if (ntohs(mpa_v2_params->ord) &
1091 MPA_V2_RDMA_WRITE_RTR)
1092 ep->mpa_attr.p2p_type =
1093 FW_RI_INIT_P2PTYPE_RDMA_WRITE;
1094 else if (ntohs(mpa_v2_params->ord) &
1095 MPA_V2_RDMA_READ_RTR)
1096 ep->mpa_attr.p2p_type =
1097 FW_RI_INIT_P2PTYPE_READ_REQ;
1100 } else if (mpa->revision == 1)
1102 ep->mpa_attr.p2p_type = p2p_type;
1104 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
1105 "xmit_marker_enabled=%d, version=%d p2p_type=%d local-p2p_type = "
1106 "%d\n", __func__, ep->mpa_attr.crc_enabled,
1107 ep->mpa_attr.recv_marker_enabled,
1108 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
1109 ep->mpa_attr.p2p_type, p2p_type);
1112 * If responder's RTR does not match with that of initiator, assign
1113 * FW_RI_INIT_P2PTYPE_DISABLED in mpa attributes so that RTR is not
1114 * generated when moving QP to RTS state.
1115 * A TERM message will be sent after QP has moved to RTS state
1117 if ((ep->mpa_attr.version == 2) &&
1118 (ep->mpa_attr.p2p_type != p2p_type)) {
1119 ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1123 attrs.mpa_attr = ep->mpa_attr;
1124 attrs.max_ird = ep->ird;
1125 attrs.max_ord = ep->ord;
1126 attrs.llp_stream_handle = ep;
1127 attrs.next_state = C4IW_QP_STATE_RTS;
1129 mask = C4IW_QP_ATTR_NEXT_STATE |
1130 C4IW_QP_ATTR_LLP_STREAM_HANDLE | C4IW_QP_ATTR_MPA_ATTR |
1131 C4IW_QP_ATTR_MAX_IRD | C4IW_QP_ATTR_MAX_ORD;
1133 /* bind QP and TID with INIT_WR */
1134 err = c4iw_modify_qp(ep->com.qp->rhp,
1135 ep->com.qp, mask, &attrs, 1);
1140 * If responder's RTR requirement did not match with what initiator
1141 * supports, generate TERM message
1144 printk(KERN_ERR "%s: RTR mismatch, sending TERM\n", __func__);
1145 attrs.layer_etype = LAYER_MPA | DDP_LLP;
1146 attrs.ecode = MPA_NOMATCH_RTR;
1147 attrs.next_state = C4IW_QP_STATE_TERMINATE;
1148 err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1149 C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
1155 * Generate TERM if initiator IRD is not sufficient for responder
1156 * provided ORD. Currently, we do the same behaviour even when
1157 * responder provided IRD is also not sufficient as regards to
1161 printk(KERN_ERR "%s: Insufficient IRD, sending TERM\n",
1163 attrs.layer_etype = LAYER_MPA | DDP_LLP;
1164 attrs.ecode = MPA_INSUFF_IRD;
1165 attrs.next_state = C4IW_QP_STATE_TERMINATE;
1166 err = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1167 C4IW_QP_ATTR_NEXT_STATE, &attrs, 0);
1173 state_set(&ep->com, ABORTING);
1174 send_abort(ep, skb, GFP_KERNEL);
1176 connect_reply_upcall(ep, err);
1180 static void process_mpa_request(struct c4iw_ep *ep, struct sk_buff *skb)
1182 struct mpa_message *mpa;
1183 struct mpa_v2_conn_params *mpa_v2_params;
1186 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1188 if (state_read(&ep->com) != MPA_REQ_WAIT)
1192 * If we get more than the supported amount of private data
1193 * then we must fail this connection.
1195 if (ep->mpa_pkt_len + skb->len > sizeof(ep->mpa_pkt)) {
1197 abort_connection(ep, skb, GFP_KERNEL);
1201 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
1204 * Copy the new data into our accumulation buffer.
1206 skb_copy_from_linear_data(skb, &(ep->mpa_pkt[ep->mpa_pkt_len]),
1208 ep->mpa_pkt_len += skb->len;
1211 * If we don't even have the mpa message, then bail.
1212 * We'll continue process when more data arrives.
1214 if (ep->mpa_pkt_len < sizeof(*mpa))
1217 PDBG("%s enter (%s line %u)\n", __func__, __FILE__, __LINE__);
1219 mpa = (struct mpa_message *) ep->mpa_pkt;
1222 * Validate MPA Header.
1224 if (mpa->revision > mpa_rev) {
1225 printk(KERN_ERR MOD "%s MPA version mismatch. Local = %d,"
1226 " Received = %d\n", __func__, mpa_rev, mpa->revision);
1227 abort_connection(ep, skb, GFP_KERNEL);
1231 if (memcmp(mpa->key, MPA_KEY_REQ, sizeof(mpa->key))) {
1232 abort_connection(ep, skb, GFP_KERNEL);
1236 plen = ntohs(mpa->private_data_size);
1239 * Fail if there's too much private data.
1241 if (plen > MPA_MAX_PRIVATE_DATA) {
1242 abort_connection(ep, skb, GFP_KERNEL);
1247 * If plen does not account for pkt size
1249 if (ep->mpa_pkt_len > (sizeof(*mpa) + plen)) {
1250 abort_connection(ep, skb, GFP_KERNEL);
1253 ep->plen = (u8) plen;
1256 * If we don't have all the pdata yet, then bail.
1258 if (ep->mpa_pkt_len < (sizeof(*mpa) + plen))
1262 * If we get here we have accumulated the entire mpa
1263 * start reply message including private data.
1265 ep->mpa_attr.initiator = 0;
1266 ep->mpa_attr.crc_enabled = (mpa->flags & MPA_CRC) | crc_enabled ? 1 : 0;
1267 ep->mpa_attr.recv_marker_enabled = markers_enabled;
1268 ep->mpa_attr.xmit_marker_enabled = mpa->flags & MPA_MARKERS ? 1 : 0;
1269 ep->mpa_attr.version = mpa->revision;
1270 if (mpa->revision == 1)
1271 ep->tried_with_mpa_v1 = 1;
1272 ep->mpa_attr.p2p_type = FW_RI_INIT_P2PTYPE_DISABLED;
1274 if (mpa->revision == 2) {
1275 ep->mpa_attr.enhanced_rdma_conn =
1276 mpa->flags & MPA_ENHANCED_RDMA_CONN ? 1 : 0;
1277 if (ep->mpa_attr.enhanced_rdma_conn) {
1278 mpa_v2_params = (struct mpa_v2_conn_params *)
1279 (ep->mpa_pkt + sizeof(*mpa));
1280 ep->ird = ntohs(mpa_v2_params->ird) &
1281 MPA_V2_IRD_ORD_MASK;
1282 ep->ord = ntohs(mpa_v2_params->ord) &
1283 MPA_V2_IRD_ORD_MASK;
1284 if (ntohs(mpa_v2_params->ird) & MPA_V2_PEER2PEER_MODEL)
1286 if (ntohs(mpa_v2_params->ord) &
1287 MPA_V2_RDMA_WRITE_RTR)
1288 ep->mpa_attr.p2p_type =
1289 FW_RI_INIT_P2PTYPE_RDMA_WRITE;
1290 else if (ntohs(mpa_v2_params->ord) &
1291 MPA_V2_RDMA_READ_RTR)
1292 ep->mpa_attr.p2p_type =
1293 FW_RI_INIT_P2PTYPE_READ_REQ;
1296 } else if (mpa->revision == 1)
1298 ep->mpa_attr.p2p_type = p2p_type;
1300 PDBG("%s - crc_enabled=%d, recv_marker_enabled=%d, "
1301 "xmit_marker_enabled=%d, version=%d p2p_type=%d\n", __func__,
1302 ep->mpa_attr.crc_enabled, ep->mpa_attr.recv_marker_enabled,
1303 ep->mpa_attr.xmit_marker_enabled, ep->mpa_attr.version,
1304 ep->mpa_attr.p2p_type);
1306 state_set(&ep->com, MPA_REQ_RCVD);
1309 connect_request_upcall(ep);
1313 static int rx_data(struct c4iw_dev *dev, struct sk_buff *skb)
1316 struct cpl_rx_data *hdr = cplhdr(skb);
1317 unsigned int dlen = ntohs(hdr->len);
1318 unsigned int tid = GET_TID(hdr);
1319 struct tid_info *t = dev->rdev.lldi.tids;
1321 ep = lookup_tid(t, tid);
1322 PDBG("%s ep %p tid %u dlen %u\n", __func__, ep, ep->hwtid, dlen);
1323 skb_pull(skb, sizeof(*hdr));
1324 skb_trim(skb, dlen);
1326 ep->rcv_seq += dlen;
1327 BUG_ON(ep->rcv_seq != (ntohl(hdr->seq) + dlen));
1329 /* update RX credits */
1330 update_rx_credits(ep, dlen);
1332 switch (state_read(&ep->com)) {
1334 process_mpa_reply(ep, skb);
1337 process_mpa_request(ep, skb);
1342 printk(KERN_ERR MOD "%s Unexpected streaming data."
1343 " ep %p state %d tid %u\n",
1344 __func__, ep, state_read(&ep->com), ep->hwtid);
1347 * The ep will timeout and inform the ULP of the failure.
1355 static int abort_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1358 struct cpl_abort_rpl_rss *rpl = cplhdr(skb);
1360 unsigned int tid = GET_TID(rpl);
1361 struct tid_info *t = dev->rdev.lldi.tids;
1363 ep = lookup_tid(t, tid);
1364 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1366 mutex_lock(&ep->com.mutex);
1367 switch (ep->com.state) {
1369 __state_set(&ep->com, DEAD);
1373 printk(KERN_ERR "%s ep %p state %d\n",
1374 __func__, ep, ep->com.state);
1377 mutex_unlock(&ep->com.mutex);
1380 release_ep_resources(ep);
1385 * Return whether a failed active open has allocated a TID
1387 static inline int act_open_has_tid(int status)
1389 return status != CPL_ERR_TCAM_FULL && status != CPL_ERR_CONN_EXIST &&
1390 status != CPL_ERR_ARP_MISS;
1393 static int act_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1396 struct cpl_act_open_rpl *rpl = cplhdr(skb);
1397 unsigned int atid = GET_TID_TID(GET_AOPEN_ATID(
1398 ntohl(rpl->atid_status)));
1399 struct tid_info *t = dev->rdev.lldi.tids;
1400 int status = GET_AOPEN_STATUS(ntohl(rpl->atid_status));
1402 ep = lookup_atid(t, atid);
1404 PDBG("%s ep %p atid %u status %u errno %d\n", __func__, ep, atid,
1405 status, status2errno(status));
1407 if (status == CPL_ERR_RTX_NEG_ADVICE) {
1408 printk(KERN_WARNING MOD "Connection problems for atid %u\n",
1413 connect_reply_upcall(ep, status2errno(status));
1414 state_set(&ep->com, DEAD);
1416 if (status && act_open_has_tid(status))
1417 cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, GET_TID(rpl));
1419 cxgb4_free_atid(t, atid);
1420 dst_release(ep->dst);
1421 cxgb4_l2t_release(ep->l2t);
1422 c4iw_put_ep(&ep->com);
1427 static int pass_open_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1429 struct cpl_pass_open_rpl *rpl = cplhdr(skb);
1430 struct tid_info *t = dev->rdev.lldi.tids;
1431 unsigned int stid = GET_TID(rpl);
1432 struct c4iw_listen_ep *ep = lookup_stid(t, stid);
1435 printk(KERN_ERR MOD "stid %d lookup failure!\n", stid);
1438 PDBG("%s ep %p status %d error %d\n", __func__, ep,
1439 rpl->status, status2errno(rpl->status));
1440 c4iw_wake_up(&ep->com.wr_wait, status2errno(rpl->status));
1445 static int listen_stop(struct c4iw_listen_ep *ep)
1447 struct sk_buff *skb;
1448 struct cpl_close_listsvr_req *req;
1450 PDBG("%s ep %p\n", __func__, ep);
1451 skb = get_skb(NULL, sizeof(*req), GFP_KERNEL);
1453 printk(KERN_ERR MOD "%s - failed to alloc skb\n", __func__);
1456 req = (struct cpl_close_listsvr_req *) skb_put(skb, sizeof(*req));
1458 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ,
1460 req->reply_ctrl = cpu_to_be16(
1461 QUEUENO(ep->com.dev->rdev.lldi.rxq_ids[0]));
1462 set_wr_txq(skb, CPL_PRIORITY_SETUP, 0);
1463 return c4iw_ofld_send(&ep->com.dev->rdev, skb);
1466 static int close_listsrv_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
1468 struct cpl_close_listsvr_rpl *rpl = cplhdr(skb);
1469 struct tid_info *t = dev->rdev.lldi.tids;
1470 unsigned int stid = GET_TID(rpl);
1471 struct c4iw_listen_ep *ep = lookup_stid(t, stid);
1473 PDBG("%s ep %p\n", __func__, ep);
1474 c4iw_wake_up(&ep->com.wr_wait, status2errno(rpl->status));
1478 static void accept_cr(struct c4iw_ep *ep, __be32 peer_ip, struct sk_buff *skb,
1479 struct cpl_pass_accept_req *req)
1481 struct cpl_pass_accept_rpl *rpl;
1482 unsigned int mtu_idx;
1487 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1488 BUG_ON(skb_cloned(skb));
1489 skb_trim(skb, sizeof(*rpl));
1491 cxgb4_best_mtu(ep->com.dev->rdev.lldi.mtus, ep->mtu, &mtu_idx);
1492 wscale = compute_wscale(rcv_win);
1493 opt0 = KEEP_ALIVE(1) |
1497 L2T_IDX(ep->l2t->idx) |
1498 TX_CHAN(ep->tx_chan) |
1499 SMAC_SEL(ep->smac_idx) |
1501 ULP_MODE(ULP_MODE_TCPDDP) |
1502 RCV_BUFSIZ(rcv_win>>10);
1503 opt2 = RX_CHANNEL(0) |
1504 RSS_QUEUE_VALID | RSS_QUEUE(ep->rss_qid);
1506 if (enable_tcp_timestamps && req->tcpopt.tstamp)
1507 opt2 |= TSTAMPS_EN(1);
1508 if (enable_tcp_sack && req->tcpopt.sack)
1510 if (wscale && enable_tcp_window_scaling)
1511 opt2 |= WND_SCALE_EN(1);
1514 INIT_TP_WR(rpl, ep->hwtid);
1515 OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1517 rpl->opt0 = cpu_to_be64(opt0);
1518 rpl->opt2 = cpu_to_be32(opt2);
1519 set_wr_txq(skb, CPL_PRIORITY_SETUP, ep->ctrlq_idx);
1520 c4iw_l2t_send(&ep->com.dev->rdev, skb, ep->l2t);
1525 static void reject_cr(struct c4iw_dev *dev, u32 hwtid, __be32 peer_ip,
1526 struct sk_buff *skb)
1528 PDBG("%s c4iw_dev %p tid %u peer_ip %x\n", __func__, dev, hwtid,
1530 BUG_ON(skb_cloned(skb));
1531 skb_trim(skb, sizeof(struct cpl_tid_release));
1533 release_tid(&dev->rdev, hwtid, skb);
1537 static void get_4tuple(struct cpl_pass_accept_req *req,
1538 __be32 *local_ip, __be32 *peer_ip,
1539 __be16 *local_port, __be16 *peer_port)
1541 int eth_len = G_ETH_HDR_LEN(be32_to_cpu(req->hdr_len));
1542 int ip_len = G_IP_HDR_LEN(be32_to_cpu(req->hdr_len));
1543 struct iphdr *ip = (struct iphdr *)((u8 *)(req + 1) + eth_len);
1544 struct tcphdr *tcp = (struct tcphdr *)
1545 ((u8 *)(req + 1) + eth_len + ip_len);
1547 PDBG("%s saddr 0x%x daddr 0x%x sport %u dport %u\n", __func__,
1548 ntohl(ip->saddr), ntohl(ip->daddr), ntohs(tcp->source),
1551 *peer_ip = ip->saddr;
1552 *local_ip = ip->daddr;
1553 *peer_port = tcp->source;
1554 *local_port = tcp->dest;
1559 static int pass_accept_req(struct c4iw_dev *dev, struct sk_buff *skb)
1561 struct c4iw_ep *child_ep, *parent_ep;
1562 struct cpl_pass_accept_req *req = cplhdr(skb);
1563 unsigned int stid = GET_POPEN_TID(ntohl(req->tos_stid));
1564 struct tid_info *t = dev->rdev.lldi.tids;
1565 unsigned int hwtid = GET_TID(req);
1566 struct neighbour *neigh;
1567 struct dst_entry *dst;
1568 struct l2t_entry *l2t;
1570 __be32 local_ip, peer_ip;
1571 __be16 local_port, peer_port;
1572 struct net_device *pdev;
1573 u32 tx_chan, smac_idx;
1577 int txq_idx, ctrlq_idx;
1579 parent_ep = lookup_stid(t, stid);
1580 PDBG("%s parent ep %p tid %u\n", __func__, parent_ep, hwtid);
1582 get_4tuple(req, &local_ip, &peer_ip, &local_port, &peer_port);
1584 if (state_read(&parent_ep->com) != LISTEN) {
1585 printk(KERN_ERR "%s - listening ep not in LISTEN\n",
1590 /* Find output route */
1591 rt = find_route(dev, local_ip, peer_ip, local_port, peer_port,
1592 GET_POPEN_TOS(ntohl(req->tos_stid)));
1594 printk(KERN_ERR MOD "%s - failed to find dst entry!\n",
1600 neigh = dst_get_neighbour(dst);
1601 if (neigh->dev->flags & IFF_LOOPBACK) {
1602 pdev = ip_dev_find(&init_net, peer_ip);
1604 l2t = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh, pdev, 0);
1606 tx_chan = cxgb4_port_chan(pdev);
1607 smac_idx = (cxgb4_port_viid(pdev) & 0x7F) << 1;
1608 step = dev->rdev.lldi.ntxq / dev->rdev.lldi.nchan;
1609 txq_idx = cxgb4_port_idx(pdev) * step;
1610 ctrlq_idx = cxgb4_port_idx(pdev);
1611 step = dev->rdev.lldi.nrxq / dev->rdev.lldi.nchan;
1612 rss_qid = dev->rdev.lldi.rxq_ids[cxgb4_port_idx(pdev) * step];
1615 l2t = cxgb4_l2t_get(dev->rdev.lldi.l2t, neigh, neigh->dev, 0);
1617 tx_chan = cxgb4_port_chan(neigh->dev);
1618 smac_idx = (cxgb4_port_viid(neigh->dev) & 0x7F) << 1;
1619 step = dev->rdev.lldi.ntxq / dev->rdev.lldi.nchan;
1620 txq_idx = cxgb4_port_idx(neigh->dev) * step;
1621 ctrlq_idx = cxgb4_port_idx(neigh->dev);
1622 step = dev->rdev.lldi.nrxq / dev->rdev.lldi.nchan;
1623 rss_qid = dev->rdev.lldi.rxq_ids[
1624 cxgb4_port_idx(neigh->dev) * step];
1628 printk(KERN_ERR MOD "%s - failed to allocate l2t entry!\n",
1634 child_ep = alloc_ep(sizeof(*child_ep), GFP_KERNEL);
1636 printk(KERN_ERR MOD "%s - failed to allocate ep entry!\n",
1638 cxgb4_l2t_release(l2t);
1642 state_set(&child_ep->com, CONNECTING);
1643 child_ep->com.dev = dev;
1644 child_ep->com.cm_id = NULL;
1645 child_ep->com.local_addr.sin_family = PF_INET;
1646 child_ep->com.local_addr.sin_port = local_port;
1647 child_ep->com.local_addr.sin_addr.s_addr = local_ip;
1648 child_ep->com.remote_addr.sin_family = PF_INET;
1649 child_ep->com.remote_addr.sin_port = peer_port;
1650 child_ep->com.remote_addr.sin_addr.s_addr = peer_ip;
1651 c4iw_get_ep(&parent_ep->com);
1652 child_ep->parent_ep = parent_ep;
1653 child_ep->tos = GET_POPEN_TOS(ntohl(req->tos_stid));
1654 child_ep->l2t = l2t;
1655 child_ep->dst = dst;
1656 child_ep->hwtid = hwtid;
1657 child_ep->tx_chan = tx_chan;
1658 child_ep->smac_idx = smac_idx;
1659 child_ep->rss_qid = rss_qid;
1660 child_ep->mtu = mtu;
1661 child_ep->txq_idx = txq_idx;
1662 child_ep->ctrlq_idx = ctrlq_idx;
1664 PDBG("%s tx_chan %u smac_idx %u rss_qid %u\n", __func__,
1665 tx_chan, smac_idx, rss_qid);
1667 init_timer(&child_ep->timer);
1668 cxgb4_insert_tid(t, child_ep, hwtid);
1669 accept_cr(child_ep, peer_ip, skb, req);
1672 reject_cr(dev, hwtid, peer_ip, skb);
1677 static int pass_establish(struct c4iw_dev *dev, struct sk_buff *skb)
1680 struct cpl_pass_establish *req = cplhdr(skb);
1681 struct tid_info *t = dev->rdev.lldi.tids;
1682 unsigned int tid = GET_TID(req);
1684 ep = lookup_tid(t, tid);
1685 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1686 ep->snd_seq = be32_to_cpu(req->snd_isn);
1687 ep->rcv_seq = be32_to_cpu(req->rcv_isn);
1689 set_emss(ep, ntohs(req->tcp_opt));
1691 dst_confirm(ep->dst);
1692 state_set(&ep->com, MPA_REQ_WAIT);
1694 send_flowc(ep, skb);
1699 static int peer_close(struct c4iw_dev *dev, struct sk_buff *skb)
1701 struct cpl_peer_close *hdr = cplhdr(skb);
1703 struct c4iw_qp_attributes attrs;
1706 struct tid_info *t = dev->rdev.lldi.tids;
1707 unsigned int tid = GET_TID(hdr);
1710 ep = lookup_tid(t, tid);
1711 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
1712 dst_confirm(ep->dst);
1714 mutex_lock(&ep->com.mutex);
1715 switch (ep->com.state) {
1717 __state_set(&ep->com, CLOSING);
1720 __state_set(&ep->com, CLOSING);
1721 connect_reply_upcall(ep, -ECONNRESET);
1726 * We're gonna mark this puppy DEAD, but keep
1727 * the reference on it until the ULP accepts or
1728 * rejects the CR. Also wake up anyone waiting
1729 * in rdma connection migration (see c4iw_accept_cr()).
1731 __state_set(&ep->com, CLOSING);
1732 PDBG("waking up ep %p tid %u\n", ep, ep->hwtid);
1733 c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
1736 __state_set(&ep->com, CLOSING);
1737 PDBG("waking up ep %p tid %u\n", ep, ep->hwtid);
1738 c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
1742 __state_set(&ep->com, CLOSING);
1743 attrs.next_state = C4IW_QP_STATE_CLOSING;
1744 ret = c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1745 C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1746 if (ret != -ECONNRESET) {
1747 peer_close_upcall(ep);
1755 __state_set(&ep->com, MORIBUND);
1760 if (ep->com.cm_id && ep->com.qp) {
1761 attrs.next_state = C4IW_QP_STATE_IDLE;
1762 c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
1763 C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
1765 close_complete_upcall(ep);
1766 __state_set(&ep->com, DEAD);
1776 mutex_unlock(&ep->com.mutex);
1778 c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
1780 release_ep_resources(ep);
1785 * Returns whether an ABORT_REQ_RSS message is a negative advice.
1787 static int is_neg_adv_abort(unsigned int status)
1789 return status == CPL_ERR_RTX_NEG_ADVICE ||
1790 status == CPL_ERR_PERSIST_NEG_ADVICE;
1793 static int c4iw_reconnect(struct c4iw_ep *ep)
1797 struct net_device *pdev;
1798 struct neighbour *neigh;
1801 PDBG("%s qp %p cm_id %p\n", __func__, ep->com.qp, ep->com.cm_id);
1802 init_timer(&ep->timer);
1805 * Allocate an active TID to initiate a TCP connection.
1807 ep->atid = cxgb4_alloc_atid(ep->com.dev->rdev.lldi.tids, ep);
1808 if (ep->atid == -1) {
1809 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
1815 rt = find_route(ep->com.dev,
1816 ep->com.cm_id->local_addr.sin_addr.s_addr,
1817 ep->com.cm_id->remote_addr.sin_addr.s_addr,
1818 ep->com.cm_id->local_addr.sin_port,
1819 ep->com.cm_id->remote_addr.sin_port, 0);
1821 printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
1822 err = -EHOSTUNREACH;
1828 neigh = dst_get_neighbour(ep->dst);
1830 /* get a l2t entry */
1831 if (neigh->dev->flags & IFF_LOOPBACK) {
1832 PDBG("%s LOOPBACK\n", __func__);
1833 pdev = ip_dev_find(&init_net,
1834 ep->com.cm_id->remote_addr.sin_addr.s_addr);
1835 ep->l2t = cxgb4_l2t_get(ep->com.dev->rdev.lldi.l2t,
1837 ep->mtu = pdev->mtu;
1838 ep->tx_chan = cxgb4_port_chan(pdev);
1839 ep->smac_idx = (cxgb4_port_viid(pdev) & 0x7F) << 1;
1840 step = ep->com.dev->rdev.lldi.ntxq /
1841 ep->com.dev->rdev.lldi.nchan;
1842 ep->txq_idx = cxgb4_port_idx(pdev) * step;
1843 step = ep->com.dev->rdev.lldi.nrxq /
1844 ep->com.dev->rdev.lldi.nchan;
1845 ep->ctrlq_idx = cxgb4_port_idx(pdev);
1846 ep->rss_qid = ep->com.dev->rdev.lldi.rxq_ids[
1847 cxgb4_port_idx(pdev) * step];
1850 ep->l2t = cxgb4_l2t_get(ep->com.dev->rdev.lldi.l2t,
1851 neigh, neigh->dev, 0);
1852 ep->mtu = dst_mtu(ep->dst);
1853 ep->tx_chan = cxgb4_port_chan(neigh->dev);
1854 ep->smac_idx = (cxgb4_port_viid(neigh->dev) & 0x7F) << 1;
1855 step = ep->com.dev->rdev.lldi.ntxq /
1856 ep->com.dev->rdev.lldi.nchan;
1857 ep->txq_idx = cxgb4_port_idx(neigh->dev) * step;
1858 ep->ctrlq_idx = cxgb4_port_idx(neigh->dev);
1859 step = ep->com.dev->rdev.lldi.nrxq /
1860 ep->com.dev->rdev.lldi.nchan;
1861 ep->rss_qid = ep->com.dev->rdev.lldi.rxq_ids[
1862 cxgb4_port_idx(neigh->dev) * step];
1866 printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
1871 PDBG("%s txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
1872 __func__, ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
1875 state_set(&ep->com, CONNECTING);
1878 /* send connect request to rnic */
1879 err = send_connect(ep);
1883 cxgb4_l2t_release(ep->l2t);
1885 dst_release(ep->dst);
1887 cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
1890 * remember to send notification to upper layer.
1891 * We are in here so the upper layer is not aware that this is
1892 * re-connect attempt and so, upper layer is still waiting for
1893 * response of 1st connect request.
1895 connect_reply_upcall(ep, -ECONNRESET);
1896 c4iw_put_ep(&ep->com);
1901 static int peer_abort(struct c4iw_dev *dev, struct sk_buff *skb)
1903 struct cpl_abort_req_rss *req = cplhdr(skb);
1905 struct cpl_abort_rpl *rpl;
1906 struct sk_buff *rpl_skb;
1907 struct c4iw_qp_attributes attrs;
1910 struct tid_info *t = dev->rdev.lldi.tids;
1911 unsigned int tid = GET_TID(req);
1913 ep = lookup_tid(t, tid);
1914 if (is_neg_adv_abort(req->status)) {
1915 PDBG("%s neg_adv_abort ep %p tid %u\n", __func__, ep,
1919 PDBG("%s ep %p tid %u state %u\n", __func__, ep, ep->hwtid,
1923 * Wake up any threads in rdma_init() or rdma_fini().
1924 * However, this is not needed if com state is just
1927 if (ep->com.state != MPA_REQ_SENT)
1928 c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
1930 mutex_lock(&ep->com.mutex);
1931 switch (ep->com.state) {
1939 if (mpa_rev == 2 && ep->tried_with_mpa_v1)
1940 connect_reply_upcall(ep, -ECONNRESET);
1943 * we just don't send notification upwards because we
1944 * want to retry with mpa_v1 without upper layers even
1947 * do some housekeeping so as to re-initiate the
1950 PDBG("%s: mpa_rev=%d. Retrying with mpav1\n", __func__,
1952 ep->retry_with_mpa_v1 = 1;
1964 if (ep->com.cm_id && ep->com.qp) {
1965 attrs.next_state = C4IW_QP_STATE_ERROR;
1966 ret = c4iw_modify_qp(ep->com.qp->rhp,
1967 ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
1971 "%s - qp <- error failed!\n",
1974 peer_abort_upcall(ep);
1979 PDBG("%s PEER_ABORT IN DEAD STATE!!!!\n", __func__);
1980 mutex_unlock(&ep->com.mutex);
1986 dst_confirm(ep->dst);
1987 if (ep->com.state != ABORTING) {
1988 __state_set(&ep->com, DEAD);
1989 /* we don't release if we want to retry with mpa_v1 */
1990 if (!ep->retry_with_mpa_v1)
1993 mutex_unlock(&ep->com.mutex);
1995 rpl_skb = get_skb(skb, sizeof(*rpl), GFP_KERNEL);
1997 printk(KERN_ERR MOD "%s - cannot allocate skb!\n",
2002 set_wr_txq(skb, CPL_PRIORITY_DATA, ep->txq_idx);
2003 rpl = (struct cpl_abort_rpl *) skb_put(rpl_skb, sizeof(*rpl));
2004 INIT_TP_WR(rpl, ep->hwtid);
2005 OPCODE_TID(rpl) = cpu_to_be32(MK_OPCODE_TID(CPL_ABORT_RPL, ep->hwtid));
2006 rpl->cmd = CPL_ABORT_NO_RST;
2007 c4iw_ofld_send(&ep->com.dev->rdev, rpl_skb);
2010 release_ep_resources(ep);
2012 /* retry with mpa-v1 */
2013 if (ep && ep->retry_with_mpa_v1) {
2014 cxgb4_remove_tid(ep->com.dev->rdev.lldi.tids, 0, ep->hwtid);
2015 dst_release(ep->dst);
2016 cxgb4_l2t_release(ep->l2t);
2023 static int close_con_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2026 struct c4iw_qp_attributes attrs;
2027 struct cpl_close_con_rpl *rpl = cplhdr(skb);
2029 struct tid_info *t = dev->rdev.lldi.tids;
2030 unsigned int tid = GET_TID(rpl);
2032 ep = lookup_tid(t, tid);
2034 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2037 /* The cm_id may be null if we failed to connect */
2038 mutex_lock(&ep->com.mutex);
2039 switch (ep->com.state) {
2041 __state_set(&ep->com, MORIBUND);
2045 if ((ep->com.cm_id) && (ep->com.qp)) {
2046 attrs.next_state = C4IW_QP_STATE_IDLE;
2047 c4iw_modify_qp(ep->com.qp->rhp,
2049 C4IW_QP_ATTR_NEXT_STATE,
2052 close_complete_upcall(ep);
2053 __state_set(&ep->com, DEAD);
2063 mutex_unlock(&ep->com.mutex);
2065 release_ep_resources(ep);
2069 static int terminate(struct c4iw_dev *dev, struct sk_buff *skb)
2071 struct cpl_rdma_terminate *rpl = cplhdr(skb);
2072 struct tid_info *t = dev->rdev.lldi.tids;
2073 unsigned int tid = GET_TID(rpl);
2075 struct c4iw_qp_attributes attrs;
2077 ep = lookup_tid(t, tid);
2080 if (ep && ep->com.qp) {
2081 printk(KERN_WARNING MOD "TERM received tid %u qpid %u\n", tid,
2082 ep->com.qp->wq.sq.qid);
2083 attrs.next_state = C4IW_QP_STATE_TERMINATE;
2084 c4iw_modify_qp(ep->com.qp->rhp, ep->com.qp,
2085 C4IW_QP_ATTR_NEXT_STATE, &attrs, 1);
2087 printk(KERN_WARNING MOD "TERM received tid %u no ep/qp\n", tid);
2093 * Upcall from the adapter indicating data has been transmitted.
2094 * For us its just the single MPA request or reply. We can now free
2095 * the skb holding the mpa message.
2097 static int fw4_ack(struct c4iw_dev *dev, struct sk_buff *skb)
2100 struct cpl_fw4_ack *hdr = cplhdr(skb);
2101 u8 credits = hdr->credits;
2102 unsigned int tid = GET_TID(hdr);
2103 struct tid_info *t = dev->rdev.lldi.tids;
2106 ep = lookup_tid(t, tid);
2107 PDBG("%s ep %p tid %u credits %u\n", __func__, ep, ep->hwtid, credits);
2109 PDBG("%s 0 credit ack ep %p tid %u state %u\n",
2110 __func__, ep, ep->hwtid, state_read(&ep->com));
2114 dst_confirm(ep->dst);
2116 PDBG("%s last streaming msg ack ep %p tid %u state %u "
2117 "initiator %u freeing skb\n", __func__, ep, ep->hwtid,
2118 state_read(&ep->com), ep->mpa_attr.initiator ? 1 : 0);
2119 kfree_skb(ep->mpa_skb);
2125 int c4iw_reject_cr(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
2128 struct c4iw_ep *ep = to_ep(cm_id);
2129 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2131 if (state_read(&ep->com) == DEAD) {
2132 c4iw_put_ep(&ep->com);
2135 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
2137 abort_connection(ep, NULL, GFP_KERNEL);
2139 err = send_mpa_reject(ep, pdata, pdata_len);
2140 err = c4iw_ep_disconnect(ep, 0, GFP_KERNEL);
2142 c4iw_put_ep(&ep->com);
2146 int c4iw_accept_cr(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2149 struct c4iw_qp_attributes attrs;
2150 enum c4iw_qp_attr_mask mask;
2151 struct c4iw_ep *ep = to_ep(cm_id);
2152 struct c4iw_dev *h = to_c4iw_dev(cm_id->device);
2153 struct c4iw_qp *qp = get_qhp(h, conn_param->qpn);
2155 PDBG("%s ep %p tid %u\n", __func__, ep, ep->hwtid);
2156 if (state_read(&ep->com) == DEAD) {
2161 BUG_ON(state_read(&ep->com) != MPA_REQ_RCVD);
2164 if ((conn_param->ord > c4iw_max_read_depth) ||
2165 (conn_param->ird > c4iw_max_read_depth)) {
2166 abort_connection(ep, NULL, GFP_KERNEL);
2171 if (ep->mpa_attr.version == 2 && ep->mpa_attr.enhanced_rdma_conn) {
2172 if (conn_param->ord > ep->ird) {
2173 ep->ird = conn_param->ird;
2174 ep->ord = conn_param->ord;
2175 send_mpa_reject(ep, conn_param->private_data,
2176 conn_param->private_data_len);
2177 abort_connection(ep, NULL, GFP_KERNEL);
2181 if (conn_param->ird > ep->ord) {
2183 conn_param->ird = 1;
2185 abort_connection(ep, NULL, GFP_KERNEL);
2192 ep->ird = conn_param->ird;
2193 ep->ord = conn_param->ord;
2195 if (ep->mpa_attr.version != 2)
2196 if (peer2peer && ep->ird == 0)
2199 PDBG("%s %d ird %d ord %d\n", __func__, __LINE__, ep->ird, ep->ord);
2201 cm_id->add_ref(cm_id);
2202 ep->com.cm_id = cm_id;
2205 /* bind QP to EP and move to RTS */
2206 attrs.mpa_attr = ep->mpa_attr;
2207 attrs.max_ird = ep->ird;
2208 attrs.max_ord = ep->ord;
2209 attrs.llp_stream_handle = ep;
2210 attrs.next_state = C4IW_QP_STATE_RTS;
2212 /* bind QP and TID with INIT_WR */
2213 mask = C4IW_QP_ATTR_NEXT_STATE |
2214 C4IW_QP_ATTR_LLP_STREAM_HANDLE |
2215 C4IW_QP_ATTR_MPA_ATTR |
2216 C4IW_QP_ATTR_MAX_IRD |
2217 C4IW_QP_ATTR_MAX_ORD;
2219 err = c4iw_modify_qp(ep->com.qp->rhp,
2220 ep->com.qp, mask, &attrs, 1);
2223 err = send_mpa_reply(ep, conn_param->private_data,
2224 conn_param->private_data_len);
2228 state_set(&ep->com, FPDU_MODE);
2229 established_upcall(ep);
2230 c4iw_put_ep(&ep->com);
2233 ep->com.cm_id = NULL;
2235 cm_id->rem_ref(cm_id);
2237 c4iw_put_ep(&ep->com);
2241 int c4iw_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
2244 struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
2247 struct net_device *pdev;
2248 struct neighbour *neigh;
2251 if ((conn_param->ord > c4iw_max_read_depth) ||
2252 (conn_param->ird > c4iw_max_read_depth)) {
2256 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
2258 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
2262 init_timer(&ep->timer);
2263 ep->plen = conn_param->private_data_len;
2265 memcpy(ep->mpa_pkt + sizeof(struct mpa_message),
2266 conn_param->private_data, ep->plen);
2267 ep->ird = conn_param->ird;
2268 ep->ord = conn_param->ord;
2270 if (peer2peer && ep->ord == 0)
2273 cm_id->add_ref(cm_id);
2275 ep->com.cm_id = cm_id;
2276 ep->com.qp = get_qhp(dev, conn_param->qpn);
2277 BUG_ON(!ep->com.qp);
2278 PDBG("%s qpn 0x%x qp %p cm_id %p\n", __func__, conn_param->qpn,
2282 * Allocate an active TID to initiate a TCP connection.
2284 ep->atid = cxgb4_alloc_atid(dev->rdev.lldi.tids, ep);
2285 if (ep->atid == -1) {
2286 printk(KERN_ERR MOD "%s - cannot alloc atid.\n", __func__);
2291 PDBG("%s saddr 0x%x sport 0x%x raddr 0x%x rport 0x%x\n", __func__,
2292 ntohl(cm_id->local_addr.sin_addr.s_addr),
2293 ntohs(cm_id->local_addr.sin_port),
2294 ntohl(cm_id->remote_addr.sin_addr.s_addr),
2295 ntohs(cm_id->remote_addr.sin_port));
2298 rt = find_route(dev,
2299 cm_id->local_addr.sin_addr.s_addr,
2300 cm_id->remote_addr.sin_addr.s_addr,
2301 cm_id->local_addr.sin_port,
2302 cm_id->remote_addr.sin_port, 0);
2304 printk(KERN_ERR MOD "%s - cannot find route.\n", __func__);
2305 err = -EHOSTUNREACH;
2311 neigh = dst_get_neighbour(ep->dst);
2313 /* get a l2t entry */
2314 if (neigh->dev->flags & IFF_LOOPBACK) {
2315 PDBG("%s LOOPBACK\n", __func__);
2316 pdev = ip_dev_find(&init_net,
2317 cm_id->remote_addr.sin_addr.s_addr);
2318 ep->l2t = cxgb4_l2t_get(ep->com.dev->rdev.lldi.l2t,
2320 ep->mtu = pdev->mtu;
2321 ep->tx_chan = cxgb4_port_chan(pdev);
2322 ep->smac_idx = (cxgb4_port_viid(pdev) & 0x7F) << 1;
2323 step = ep->com.dev->rdev.lldi.ntxq /
2324 ep->com.dev->rdev.lldi.nchan;
2325 ep->txq_idx = cxgb4_port_idx(pdev) * step;
2326 step = ep->com.dev->rdev.lldi.nrxq /
2327 ep->com.dev->rdev.lldi.nchan;
2328 ep->ctrlq_idx = cxgb4_port_idx(pdev);
2329 ep->rss_qid = ep->com.dev->rdev.lldi.rxq_ids[
2330 cxgb4_port_idx(pdev) * step];
2333 ep->l2t = cxgb4_l2t_get(ep->com.dev->rdev.lldi.l2t,
2334 neigh, neigh->dev, 0);
2335 ep->mtu = dst_mtu(ep->dst);
2336 ep->tx_chan = cxgb4_port_chan(neigh->dev);
2337 ep->smac_idx = (cxgb4_port_viid(neigh->dev) & 0x7F) << 1;
2338 step = ep->com.dev->rdev.lldi.ntxq /
2339 ep->com.dev->rdev.lldi.nchan;
2340 ep->txq_idx = cxgb4_port_idx(neigh->dev) * step;
2341 ep->ctrlq_idx = cxgb4_port_idx(neigh->dev);
2342 step = ep->com.dev->rdev.lldi.nrxq /
2343 ep->com.dev->rdev.lldi.nchan;
2344 ep->rss_qid = ep->com.dev->rdev.lldi.rxq_ids[
2345 cxgb4_port_idx(neigh->dev) * step];
2346 ep->retry_with_mpa_v1 = 0;
2347 ep->tried_with_mpa_v1 = 0;
2351 printk(KERN_ERR MOD "%s - cannot alloc l2e.\n", __func__);
2356 PDBG("%s txq_idx %u tx_chan %u smac_idx %u rss_qid %u l2t_idx %u\n",
2357 __func__, ep->txq_idx, ep->tx_chan, ep->smac_idx, ep->rss_qid,
2360 state_set(&ep->com, CONNECTING);
2362 ep->com.local_addr = cm_id->local_addr;
2363 ep->com.remote_addr = cm_id->remote_addr;
2365 /* send connect request to rnic */
2366 err = send_connect(ep);
2370 cxgb4_l2t_release(ep->l2t);
2372 dst_release(ep->dst);
2374 cxgb4_free_atid(ep->com.dev->rdev.lldi.tids, ep->atid);
2376 cm_id->rem_ref(cm_id);
2377 c4iw_put_ep(&ep->com);
2382 int c4iw_create_listen(struct iw_cm_id *cm_id, int backlog)
2385 struct c4iw_dev *dev = to_c4iw_dev(cm_id->device);
2386 struct c4iw_listen_ep *ep;
2391 ep = alloc_ep(sizeof(*ep), GFP_KERNEL);
2393 printk(KERN_ERR MOD "%s - cannot alloc ep.\n", __func__);
2397 PDBG("%s ep %p\n", __func__, ep);
2398 cm_id->add_ref(cm_id);
2399 ep->com.cm_id = cm_id;
2401 ep->backlog = backlog;
2402 ep->com.local_addr = cm_id->local_addr;
2405 * Allocate a server TID.
2407 ep->stid = cxgb4_alloc_stid(dev->rdev.lldi.tids, PF_INET, ep);
2408 if (ep->stid == -1) {
2409 printk(KERN_ERR MOD "%s - cannot alloc stid.\n", __func__);
2414 state_set(&ep->com, LISTEN);
2415 c4iw_init_wr_wait(&ep->com.wr_wait);
2416 err = cxgb4_create_server(ep->com.dev->rdev.lldi.ports[0], ep->stid,
2417 ep->com.local_addr.sin_addr.s_addr,
2418 ep->com.local_addr.sin_port,
2419 ep->com.dev->rdev.lldi.rxq_ids[0]);
2423 /* wait for pass_open_rpl */
2424 err = c4iw_wait_for_reply(&ep->com.dev->rdev, &ep->com.wr_wait, 0, 0,
2427 cm_id->provider_data = ep;
2431 cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid, PF_INET);
2433 cm_id->rem_ref(cm_id);
2434 c4iw_put_ep(&ep->com);
2440 int c4iw_destroy_listen(struct iw_cm_id *cm_id)
2443 struct c4iw_listen_ep *ep = to_listen_ep(cm_id);
2445 PDBG("%s ep %p\n", __func__, ep);
2448 state_set(&ep->com, DEAD);
2449 c4iw_init_wr_wait(&ep->com.wr_wait);
2450 err = listen_stop(ep);
2453 err = c4iw_wait_for_reply(&ep->com.dev->rdev, &ep->com.wr_wait, 0, 0,
2455 cxgb4_free_stid(ep->com.dev->rdev.lldi.tids, ep->stid, PF_INET);
2457 cm_id->rem_ref(cm_id);
2458 c4iw_put_ep(&ep->com);
2462 int c4iw_ep_disconnect(struct c4iw_ep *ep, int abrupt, gfp_t gfp)
2467 struct c4iw_rdev *rdev;
2469 mutex_lock(&ep->com.mutex);
2471 PDBG("%s ep %p state %s, abrupt %d\n", __func__, ep,
2472 states[ep->com.state], abrupt);
2474 rdev = &ep->com.dev->rdev;
2475 if (c4iw_fatal_error(rdev)) {
2477 close_complete_upcall(ep);
2478 ep->com.state = DEAD;
2480 switch (ep->com.state) {
2488 ep->com.state = ABORTING;
2490 ep->com.state = CLOSING;
2493 set_bit(CLOSE_SENT, &ep->com.flags);
2496 if (!test_and_set_bit(CLOSE_SENT, &ep->com.flags)) {
2500 ep->com.state = ABORTING;
2502 ep->com.state = MORIBUND;
2508 PDBG("%s ignoring disconnect ep %p state %u\n",
2509 __func__, ep, ep->com.state);
2518 close_complete_upcall(ep);
2519 ret = send_abort(ep, NULL, gfp);
2521 ret = send_halfclose(ep, gfp);
2525 mutex_unlock(&ep->com.mutex);
2527 release_ep_resources(ep);
2531 static int async_event(struct c4iw_dev *dev, struct sk_buff *skb)
2533 struct cpl_fw6_msg *rpl = cplhdr(skb);
2534 c4iw_ev_dispatch(dev, (struct t4_cqe *)&rpl->data[0]);
2539 * These are the real handlers that are called from a
2542 static c4iw_handler_func work_handlers[NUM_CPL_CMDS] = {
2543 [CPL_ACT_ESTABLISH] = act_establish,
2544 [CPL_ACT_OPEN_RPL] = act_open_rpl,
2545 [CPL_RX_DATA] = rx_data,
2546 [CPL_ABORT_RPL_RSS] = abort_rpl,
2547 [CPL_ABORT_RPL] = abort_rpl,
2548 [CPL_PASS_OPEN_RPL] = pass_open_rpl,
2549 [CPL_CLOSE_LISTSRV_RPL] = close_listsrv_rpl,
2550 [CPL_PASS_ACCEPT_REQ] = pass_accept_req,
2551 [CPL_PASS_ESTABLISH] = pass_establish,
2552 [CPL_PEER_CLOSE] = peer_close,
2553 [CPL_ABORT_REQ_RSS] = peer_abort,
2554 [CPL_CLOSE_CON_RPL] = close_con_rpl,
2555 [CPL_RDMA_TERMINATE] = terminate,
2556 [CPL_FW4_ACK] = fw4_ack,
2557 [CPL_FW6_MSG] = async_event
2560 static void process_timeout(struct c4iw_ep *ep)
2562 struct c4iw_qp_attributes attrs;
2565 mutex_lock(&ep->com.mutex);
2566 PDBG("%s ep %p tid %u state %d\n", __func__, ep, ep->hwtid,
2568 switch (ep->com.state) {
2570 __state_set(&ep->com, ABORTING);
2571 connect_reply_upcall(ep, -ETIMEDOUT);
2574 __state_set(&ep->com, ABORTING);
2578 if (ep->com.cm_id && ep->com.qp) {
2579 attrs.next_state = C4IW_QP_STATE_ERROR;
2580 c4iw_modify_qp(ep->com.qp->rhp,
2581 ep->com.qp, C4IW_QP_ATTR_NEXT_STATE,
2584 __state_set(&ep->com, ABORTING);
2587 printk(KERN_ERR "%s unexpected state ep %p tid %u state %u\n",
2588 __func__, ep, ep->hwtid, ep->com.state);
2592 mutex_unlock(&ep->com.mutex);
2594 abort_connection(ep, NULL, GFP_KERNEL);
2595 c4iw_put_ep(&ep->com);
2598 static void process_timedout_eps(void)
2602 spin_lock_irq(&timeout_lock);
2603 while (!list_empty(&timeout_list)) {
2604 struct list_head *tmp;
2606 tmp = timeout_list.next;
2608 spin_unlock_irq(&timeout_lock);
2609 ep = list_entry(tmp, struct c4iw_ep, entry);
2610 process_timeout(ep);
2611 spin_lock_irq(&timeout_lock);
2613 spin_unlock_irq(&timeout_lock);
2616 static void process_work(struct work_struct *work)
2618 struct sk_buff *skb = NULL;
2619 struct c4iw_dev *dev;
2620 struct cpl_act_establish *rpl;
2621 unsigned int opcode;
2624 while ((skb = skb_dequeue(&rxq))) {
2626 dev = *((struct c4iw_dev **) (skb->cb + sizeof(void *)));
2627 opcode = rpl->ot.opcode;
2629 BUG_ON(!work_handlers[opcode]);
2630 ret = work_handlers[opcode](dev, skb);
2634 process_timedout_eps();
2637 static DECLARE_WORK(skb_work, process_work);
2639 static void ep_timeout(unsigned long arg)
2641 struct c4iw_ep *ep = (struct c4iw_ep *)arg;
2643 spin_lock(&timeout_lock);
2644 list_add_tail(&ep->entry, &timeout_list);
2645 spin_unlock(&timeout_lock);
2646 queue_work(workq, &skb_work);
2650 * All the CM events are handled on a work queue to have a safe context.
2652 static int sched(struct c4iw_dev *dev, struct sk_buff *skb)
2656 * Save dev in the skb->cb area.
2658 *((struct c4iw_dev **) (skb->cb + sizeof(void *))) = dev;
2661 * Queue the skb and schedule the worker thread.
2663 skb_queue_tail(&rxq, skb);
2664 queue_work(workq, &skb_work);
2668 static int set_tcb_rpl(struct c4iw_dev *dev, struct sk_buff *skb)
2670 struct cpl_set_tcb_rpl *rpl = cplhdr(skb);
2672 if (rpl->status != CPL_ERR_NONE) {
2673 printk(KERN_ERR MOD "Unexpected SET_TCB_RPL status %u "
2674 "for tid %u\n", rpl->status, GET_TID(rpl));
2680 static int fw6_msg(struct c4iw_dev *dev, struct sk_buff *skb)
2682 struct cpl_fw6_msg *rpl = cplhdr(skb);
2683 struct c4iw_wr_wait *wr_waitp;
2686 PDBG("%s type %u\n", __func__, rpl->type);
2688 switch (rpl->type) {
2690 ret = (int)((be64_to_cpu(rpl->data[0]) >> 8) & 0xff);
2691 wr_waitp = (struct c4iw_wr_wait *)(__force unsigned long) rpl->data[1];
2692 PDBG("%s wr_waitp %p ret %u\n", __func__, wr_waitp, ret);
2694 c4iw_wake_up(wr_waitp, ret ? -ret : 0);
2701 printk(KERN_ERR MOD "%s unexpected fw6 msg type %u\n", __func__,
2709 static int peer_abort_intr(struct c4iw_dev *dev, struct sk_buff *skb)
2711 struct cpl_abort_req_rss *req = cplhdr(skb);
2713 struct tid_info *t = dev->rdev.lldi.tids;
2714 unsigned int tid = GET_TID(req);
2716 ep = lookup_tid(t, tid);
2717 if (is_neg_adv_abort(req->status)) {
2718 PDBG("%s neg_adv_abort ep %p tid %u\n", __func__, ep,
2723 PDBG("%s ep %p tid %u state %u\n", __func__, ep, ep->hwtid,
2727 * Wake up any threads in rdma_init() or rdma_fini().
2728 * However, this is not needed if com state is just
2731 if (ep->com.state != MPA_REQ_SENT)
2732 c4iw_wake_up(&ep->com.wr_wait, -ECONNRESET);
2738 * Most upcalls from the T4 Core go to sched() to
2739 * schedule the processing on a work queue.
2741 c4iw_handler_func c4iw_handlers[NUM_CPL_CMDS] = {
2742 [CPL_ACT_ESTABLISH] = sched,
2743 [CPL_ACT_OPEN_RPL] = sched,
2744 [CPL_RX_DATA] = sched,
2745 [CPL_ABORT_RPL_RSS] = sched,
2746 [CPL_ABORT_RPL] = sched,
2747 [CPL_PASS_OPEN_RPL] = sched,
2748 [CPL_CLOSE_LISTSRV_RPL] = sched,
2749 [CPL_PASS_ACCEPT_REQ] = sched,
2750 [CPL_PASS_ESTABLISH] = sched,
2751 [CPL_PEER_CLOSE] = sched,
2752 [CPL_CLOSE_CON_RPL] = sched,
2753 [CPL_ABORT_REQ_RSS] = peer_abort_intr,
2754 [CPL_RDMA_TERMINATE] = sched,
2755 [CPL_FW4_ACK] = sched,
2756 [CPL_SET_TCB_RPL] = set_tcb_rpl,
2757 [CPL_FW6_MSG] = fw6_msg
2760 int __init c4iw_cm_init(void)
2762 spin_lock_init(&timeout_lock);
2763 skb_queue_head_init(&rxq);
2765 workq = create_singlethread_workqueue("iw_cxgb4");
2772 void __exit c4iw_cm_term(void)
2774 WARN_ON(!list_empty(&timeout_list));
2775 flush_workqueue(workq);
2776 destroy_workqueue(workq);