2 * Copyright (c) 2016 Chelsio Communications, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/module.h>
10 #include <linux/list.h>
11 #include <linux/workqueue.h>
12 #include <linux/skbuff.h>
13 #include <linux/timer.h>
14 #include <linux/notifier.h>
15 #include <linux/inetdevice.h>
17 #include <linux/tcp.h>
18 #include <linux/if_vlan.h>
20 #include <net/neighbour.h>
21 #include <net/netevent.h>
22 #include <net/route.h>
24 #include <net/ip6_route.h>
25 #include <net/addrconf.h>
27 #include <libcxgb_cm.h>
31 static void cxgbit_init_wr_wait(struct cxgbit_wr_wait *wr_waitp)
34 reinit_completion(&wr_waitp->completion);
38 cxgbit_wake_up(struct cxgbit_wr_wait *wr_waitp, const char *func, u8 ret)
40 if (ret == CPL_ERR_NONE)
46 pr_err("%s: err:%u", func, ret);
48 complete(&wr_waitp->completion);
52 cxgbit_wait_for_reply(struct cxgbit_device *cdev,
53 struct cxgbit_wr_wait *wr_waitp, u32 tid, u32 timeout,
58 if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
63 ret = wait_for_completion_timeout(&wr_waitp->completion, timeout * HZ);
65 pr_info("%s - Device %s not responding tid %u\n",
66 func, pci_name(cdev->lldi.pdev), tid);
67 wr_waitp->ret = -ETIMEDOUT;
71 pr_info("%s: FW reply %d tid %u\n",
72 pci_name(cdev->lldi.pdev), wr_waitp->ret, tid);
76 static int cxgbit_np_hashfn(const struct cxgbit_np *cnp)
78 return ((unsigned long)cnp >> 10) & (NP_INFO_HASH_SIZE - 1);
81 static struct np_info *
82 cxgbit_np_hash_add(struct cxgbit_device *cdev, struct cxgbit_np *cnp,
85 struct np_info *p = kzalloc(sizeof(*p), GFP_KERNEL);
88 int bucket = cxgbit_np_hashfn(cnp);
92 spin_lock(&cdev->np_lock);
93 p->next = cdev->np_hash_tab[bucket];
94 cdev->np_hash_tab[bucket] = p;
95 spin_unlock(&cdev->np_lock);
102 cxgbit_np_hash_find(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
104 int stid = -1, bucket = cxgbit_np_hashfn(cnp);
107 spin_lock(&cdev->np_lock);
108 for (p = cdev->np_hash_tab[bucket]; p; p = p->next) {
114 spin_unlock(&cdev->np_lock);
119 static int cxgbit_np_hash_del(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
121 int stid = -1, bucket = cxgbit_np_hashfn(cnp);
122 struct np_info *p, **prev = &cdev->np_hash_tab[bucket];
124 spin_lock(&cdev->np_lock);
125 for (p = *prev; p; prev = &p->next, p = p->next) {
133 spin_unlock(&cdev->np_lock);
138 void _cxgbit_free_cnp(struct kref *kref)
140 struct cxgbit_np *cnp;
142 cnp = container_of(kref, struct cxgbit_np, kref);
147 cxgbit_create_server6(struct cxgbit_device *cdev, unsigned int stid,
148 struct cxgbit_np *cnp)
150 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
151 &cnp->com.local_addr;
155 pr_debug("%s: dev = %s; stid = %u; sin6_port = %u\n",
156 __func__, cdev->lldi.ports[0]->name, stid, sin6->sin6_port);
158 addr_type = ipv6_addr_type((const struct in6_addr *)
160 if (addr_type != IPV6_ADDR_ANY) {
161 ret = cxgb4_clip_get(cdev->lldi.ports[0],
162 (const u32 *)&sin6->sin6_addr.s6_addr, 1);
164 pr_err("Unable to find clip table entry. laddr %pI6. Error:%d.\n",
165 sin6->sin6_addr.s6_addr, ret);
171 cxgbit_init_wr_wait(&cnp->com.wr_wait);
173 ret = cxgb4_create_server6(cdev->lldi.ports[0],
174 stid, &sin6->sin6_addr,
176 cdev->lldi.rxq_ids[0]);
178 ret = cxgbit_wait_for_reply(cdev, &cnp->com.wr_wait,
181 ret = net_xmit_errno(ret);
186 if (ret != -ETIMEDOUT)
187 cxgb4_clip_release(cdev->lldi.ports[0],
188 (const u32 *)&sin6->sin6_addr.s6_addr, 1);
190 pr_err("create server6 err %d stid %d laddr %pI6 lport %d\n",
191 ret, stid, sin6->sin6_addr.s6_addr,
192 ntohs(sin6->sin6_port));
199 cxgbit_create_server4(struct cxgbit_device *cdev, unsigned int stid,
200 struct cxgbit_np *cnp)
202 struct sockaddr_in *sin = (struct sockaddr_in *)
203 &cnp->com.local_addr;
206 pr_debug("%s: dev = %s; stid = %u; sin_port = %u\n",
207 __func__, cdev->lldi.ports[0]->name, stid, sin->sin_port);
210 cxgbit_init_wr_wait(&cnp->com.wr_wait);
212 ret = cxgb4_create_server(cdev->lldi.ports[0],
213 stid, sin->sin_addr.s_addr,
215 cdev->lldi.rxq_ids[0]);
217 ret = cxgbit_wait_for_reply(cdev,
221 ret = net_xmit_errno(ret);
226 pr_err("create server failed err %d stid %d laddr %pI4 lport %d\n",
227 ret, stid, &sin->sin_addr, ntohs(sin->sin_port));
231 struct cxgbit_device *cxgbit_find_device(struct net_device *ndev, u8 *port_id)
233 struct cxgbit_device *cdev;
236 list_for_each_entry(cdev, &cdev_list_head, list) {
237 struct cxgb4_lld_info *lldi = &cdev->lldi;
239 for (i = 0; i < lldi->nports; i++) {
240 if (lldi->ports[i] == ndev) {
251 static struct net_device *cxgbit_get_real_dev(struct net_device *ndev)
253 if (ndev->priv_flags & IFF_BONDING) {
254 pr_err("Bond devices are not supported. Interface:%s\n",
259 if (is_vlan_dev(ndev))
260 return vlan_dev_real_dev(ndev);
265 static struct net_device *cxgbit_ipv4_netdev(__be32 saddr)
267 struct net_device *ndev;
269 ndev = __ip_dev_find(&init_net, saddr, false);
273 return cxgbit_get_real_dev(ndev);
276 static struct net_device *cxgbit_ipv6_netdev(struct in6_addr *addr6)
278 struct net_device *ndev = NULL;
281 if (IS_ENABLED(CONFIG_IPV6)) {
282 for_each_netdev_rcu(&init_net, ndev)
283 if (ipv6_chk_addr(&init_net, addr6, ndev, 1)) {
290 return cxgbit_get_real_dev(ndev);
293 static struct cxgbit_device *cxgbit_find_np_cdev(struct cxgbit_np *cnp)
295 struct sockaddr_storage *sockaddr = &cnp->com.local_addr;
296 int ss_family = sockaddr->ss_family;
297 struct net_device *ndev = NULL;
298 struct cxgbit_device *cdev = NULL;
301 if (ss_family == AF_INET) {
302 struct sockaddr_in *sin;
304 sin = (struct sockaddr_in *)sockaddr;
305 ndev = cxgbit_ipv4_netdev(sin->sin_addr.s_addr);
306 } else if (ss_family == AF_INET6) {
307 struct sockaddr_in6 *sin6;
309 sin6 = (struct sockaddr_in6 *)sockaddr;
310 ndev = cxgbit_ipv6_netdev(&sin6->sin6_addr);
315 cdev = cxgbit_find_device(ndev, NULL);
321 static bool cxgbit_inaddr_any(struct cxgbit_np *cnp)
323 struct sockaddr_storage *sockaddr = &cnp->com.local_addr;
324 int ss_family = sockaddr->ss_family;
327 if (ss_family == AF_INET) {
328 struct sockaddr_in *sin;
330 sin = (struct sockaddr_in *)sockaddr;
331 if (sin->sin_addr.s_addr == htonl(INADDR_ANY))
333 } else if (ss_family == AF_INET6) {
334 struct sockaddr_in6 *sin6;
336 sin6 = (struct sockaddr_in6 *)sockaddr;
337 addr_type = ipv6_addr_type((const struct in6_addr *)
339 if (addr_type == IPV6_ADDR_ANY)
346 __cxgbit_setup_cdev_np(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
349 int ss_family = cnp->com.local_addr.ss_family;
351 if (!test_bit(CDEV_STATE_UP, &cdev->flags))
354 stid = cxgb4_alloc_stid(cdev->lldi.tids, ss_family, cnp);
358 if (!cxgbit_np_hash_add(cdev, cnp, stid)) {
359 cxgb4_free_stid(cdev->lldi.tids, stid, ss_family);
363 if (ss_family == AF_INET)
364 ret = cxgbit_create_server4(cdev, stid, cnp);
366 ret = cxgbit_create_server6(cdev, stid, cnp);
369 if (ret != -ETIMEDOUT)
370 cxgb4_free_stid(cdev->lldi.tids, stid,
372 cxgbit_np_hash_del(cdev, cnp);
378 static int cxgbit_setup_cdev_np(struct cxgbit_np *cnp)
380 struct cxgbit_device *cdev;
383 mutex_lock(&cdev_list_lock);
384 cdev = cxgbit_find_np_cdev(cnp);
388 if (cxgbit_np_hash_find(cdev, cnp) >= 0)
391 if (__cxgbit_setup_cdev_np(cdev, cnp))
394 cnp->com.cdev = cdev;
397 mutex_unlock(&cdev_list_lock);
401 static int cxgbit_setup_all_np(struct cxgbit_np *cnp)
403 struct cxgbit_device *cdev;
407 mutex_lock(&cdev_list_lock);
408 list_for_each_entry(cdev, &cdev_list_head, list) {
409 if (cxgbit_np_hash_find(cdev, cnp) >= 0) {
410 mutex_unlock(&cdev_list_lock);
415 list_for_each_entry(cdev, &cdev_list_head, list) {
416 ret = __cxgbit_setup_cdev_np(cdev, cnp);
417 if (ret == -ETIMEDOUT)
423 mutex_unlock(&cdev_list_lock);
425 return count ? 0 : -1;
428 int cxgbit_setup_np(struct iscsi_np *np, struct sockaddr_storage *ksockaddr)
430 struct cxgbit_np *cnp;
433 if ((ksockaddr->ss_family != AF_INET) &&
434 (ksockaddr->ss_family != AF_INET6))
437 cnp = kzalloc(sizeof(*cnp), GFP_KERNEL);
441 init_waitqueue_head(&cnp->accept_wait);
442 init_completion(&cnp->com.wr_wait.completion);
443 init_completion(&cnp->accept_comp);
444 INIT_LIST_HEAD(&cnp->np_accept_list);
445 spin_lock_init(&cnp->np_accept_lock);
446 kref_init(&cnp->kref);
447 memcpy(&np->np_sockaddr, ksockaddr,
448 sizeof(struct sockaddr_storage));
449 memcpy(&cnp->com.local_addr, &np->np_sockaddr,
450 sizeof(cnp->com.local_addr));
453 cnp->com.cdev = NULL;
455 if (cxgbit_inaddr_any(cnp))
456 ret = cxgbit_setup_all_np(cnp);
458 ret = cxgbit_setup_cdev_np(cnp);
465 np->np_context = cnp;
466 cnp->com.state = CSK_STATE_LISTEN;
471 cxgbit_set_conn_info(struct iscsi_np *np, struct iscsi_conn *conn,
472 struct cxgbit_sock *csk)
474 conn->login_family = np->np_sockaddr.ss_family;
475 conn->login_sockaddr = csk->com.remote_addr;
476 conn->local_sockaddr = csk->com.local_addr;
479 int cxgbit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
481 struct cxgbit_np *cnp = np->np_context;
482 struct cxgbit_sock *csk;
486 ret = wait_for_completion_interruptible(&cnp->accept_comp);
490 spin_lock_bh(&np->np_thread_lock);
491 if (np->np_thread_state >= ISCSI_NP_THREAD_RESET) {
492 spin_unlock_bh(&np->np_thread_lock);
494 * No point in stalling here when np_thread
495 * is in state RESET/SHUTDOWN/EXIT - bail
499 spin_unlock_bh(&np->np_thread_lock);
501 spin_lock_bh(&cnp->np_accept_lock);
502 if (list_empty(&cnp->np_accept_list)) {
503 spin_unlock_bh(&cnp->np_accept_lock);
507 csk = list_first_entry(&cnp->np_accept_list,
511 list_del_init(&csk->accept_node);
512 spin_unlock_bh(&cnp->np_accept_lock);
516 cxgbit_set_conn_info(np, conn, csk);
521 __cxgbit_free_cdev_np(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
526 stid = cxgbit_np_hash_del(cdev, cnp);
529 if (!test_bit(CDEV_STATE_UP, &cdev->flags))
532 if (cnp->np->np_sockaddr.ss_family == AF_INET6)
536 cxgbit_init_wr_wait(&cnp->com.wr_wait);
537 ret = cxgb4_remove_server(cdev->lldi.ports[0], stid,
538 cdev->lldi.rxq_ids[0], ipv6);
541 ret = net_xmit_errno(ret);
548 ret = cxgbit_wait_for_reply(cdev, &cnp->com.wr_wait,
550 if (ret == -ETIMEDOUT)
553 if (ipv6 && cnp->com.cdev) {
554 struct sockaddr_in6 *sin6;
556 sin6 = (struct sockaddr_in6 *)&cnp->com.local_addr;
557 cxgb4_clip_release(cdev->lldi.ports[0],
558 (const u32 *)&sin6->sin6_addr.s6_addr,
562 cxgb4_free_stid(cdev->lldi.tids, stid,
563 cnp->com.local_addr.ss_family);
567 static void cxgbit_free_all_np(struct cxgbit_np *cnp)
569 struct cxgbit_device *cdev;
572 mutex_lock(&cdev_list_lock);
573 list_for_each_entry(cdev, &cdev_list_head, list) {
574 ret = __cxgbit_free_cdev_np(cdev, cnp);
575 if (ret == -ETIMEDOUT)
578 mutex_unlock(&cdev_list_lock);
581 static void cxgbit_free_cdev_np(struct cxgbit_np *cnp)
583 struct cxgbit_device *cdev;
586 mutex_lock(&cdev_list_lock);
587 list_for_each_entry(cdev, &cdev_list_head, list) {
588 if (cdev == cnp->com.cdev) {
596 __cxgbit_free_cdev_np(cdev, cnp);
598 mutex_unlock(&cdev_list_lock);
601 void cxgbit_free_np(struct iscsi_np *np)
603 struct cxgbit_np *cnp = np->np_context;
605 cnp->com.state = CSK_STATE_DEAD;
607 cxgbit_free_cdev_np(cnp);
609 cxgbit_free_all_np(cnp);
611 np->np_context = NULL;
615 static void cxgbit_send_halfclose(struct cxgbit_sock *csk)
618 u32 len = roundup(sizeof(struct cpl_close_con_req), 16);
620 skb = alloc_skb(len, GFP_ATOMIC);
624 cxgb_mk_close_con_req(skb, len, csk->tid, csk->txq_idx,
627 cxgbit_skcb_flags(skb) |= SKCBF_TX_FLAG_COMPL;
628 __skb_queue_tail(&csk->txq, skb);
629 cxgbit_push_tx_frames(csk);
632 static void cxgbit_arp_failure_discard(void *handle, struct sk_buff *skb)
634 pr_debug("%s cxgbit_device %p\n", __func__, handle);
638 static void cxgbit_abort_arp_failure(void *handle, struct sk_buff *skb)
640 struct cxgbit_device *cdev = handle;
641 struct cpl_abort_req *req = cplhdr(skb);
643 pr_debug("%s cdev %p\n", __func__, cdev);
644 req->cmd = CPL_ABORT_NO_RST;
645 cxgbit_ofld_send(cdev, skb);
648 static int cxgbit_send_abort_req(struct cxgbit_sock *csk)
651 u32 len = roundup(sizeof(struct cpl_abort_req), 16);
653 pr_debug("%s: csk %p tid %u; state %d\n",
654 __func__, csk, csk->tid, csk->com.state);
656 __skb_queue_purge(&csk->txq);
658 if (!test_and_set_bit(CSK_TX_DATA_SENT, &csk->com.flags))
659 cxgbit_send_tx_flowc_wr(csk);
661 skb = __skb_dequeue(&csk->skbq);
662 cxgb_mk_abort_req(skb, len, csk->tid, csk->txq_idx,
663 csk->com.cdev, cxgbit_abort_arp_failure);
665 return cxgbit_l2t_send(csk->com.cdev, skb, csk->l2t);
669 __cxgbit_abort_conn(struct cxgbit_sock *csk, struct sk_buff *skb)
673 if (csk->com.state != CSK_STATE_ESTABLISHED)
676 set_bit(CSK_ABORT_RPL_WAIT, &csk->com.flags);
677 csk->com.state = CSK_STATE_ABORTING;
679 cxgbit_send_abort_req(csk);
684 cxgbit_wake_up(&csk->com.wr_wait, __func__, CPL_ERR_NONE);
688 void cxgbit_abort_conn(struct cxgbit_sock *csk)
690 struct sk_buff *skb = alloc_skb(0, GFP_KERNEL | __GFP_NOFAIL);
693 cxgbit_init_wr_wait(&csk->com.wr_wait);
695 spin_lock_bh(&csk->lock);
696 if (csk->lock_owner) {
697 cxgbit_skcb_rx_backlog_fn(skb) = __cxgbit_abort_conn;
698 __skb_queue_tail(&csk->backlogq, skb);
700 __cxgbit_abort_conn(csk, skb);
702 spin_unlock_bh(&csk->lock);
704 cxgbit_wait_for_reply(csk->com.cdev, &csk->com.wr_wait,
705 csk->tid, 600, __func__);
708 void cxgbit_free_conn(struct iscsi_conn *conn)
710 struct cxgbit_sock *csk = conn->context;
711 bool release = false;
713 pr_debug("%s: state %d\n",
714 __func__, csk->com.state);
716 spin_lock_bh(&csk->lock);
717 switch (csk->com.state) {
718 case CSK_STATE_ESTABLISHED:
719 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT) {
720 csk->com.state = CSK_STATE_CLOSING;
721 cxgbit_send_halfclose(csk);
723 csk->com.state = CSK_STATE_ABORTING;
724 cxgbit_send_abort_req(csk);
727 case CSK_STATE_CLOSING:
728 csk->com.state = CSK_STATE_MORIBUND;
729 cxgbit_send_halfclose(csk);
735 pr_err("%s: csk %p; state %d\n",
736 __func__, csk, csk->com.state);
738 spin_unlock_bh(&csk->lock);
744 static void cxgbit_set_emss(struct cxgbit_sock *csk, u16 opt)
746 csk->emss = csk->com.cdev->lldi.mtus[TCPOPT_MSS_G(opt)] -
747 ((csk->com.remote_addr.ss_family == AF_INET) ?
748 sizeof(struct iphdr) : sizeof(struct ipv6hdr)) -
749 sizeof(struct tcphdr);
750 csk->mss = csk->emss;
751 if (TCPOPT_TSTAMP_G(opt))
752 csk->emss -= round_up(TCPOLEN_TIMESTAMP, 4);
756 pr_info("Warning: misaligned mtu idx %u mss %u emss=%u\n",
757 TCPOPT_MSS_G(opt), csk->mss, csk->emss);
758 pr_debug("%s mss_idx %u mss %u emss=%u\n", __func__, TCPOPT_MSS_G(opt),
759 csk->mss, csk->emss);
762 static void cxgbit_free_skb(struct cxgbit_sock *csk)
766 __skb_queue_purge(&csk->txq);
767 __skb_queue_purge(&csk->rxq);
768 __skb_queue_purge(&csk->backlogq);
769 __skb_queue_purge(&csk->ppodq);
770 __skb_queue_purge(&csk->skbq);
772 while ((skb = cxgbit_sock_dequeue_wr(csk)))
775 __kfree_skb(csk->lro_hskb);
778 void _cxgbit_free_csk(struct kref *kref)
780 struct cxgbit_sock *csk;
781 struct cxgbit_device *cdev;
783 csk = container_of(kref, struct cxgbit_sock, kref);
785 pr_debug("%s csk %p state %d\n", __func__, csk, csk->com.state);
787 if (csk->com.local_addr.ss_family == AF_INET6) {
788 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
789 &csk->com.local_addr;
790 cxgb4_clip_release(csk->com.cdev->lldi.ports[0],
792 &sin6->sin6_addr.s6_addr, 1);
795 cxgb4_remove_tid(csk->com.cdev->lldi.tids, 0, csk->tid,
796 csk->com.local_addr.ss_family);
797 dst_release(csk->dst);
798 cxgb4_l2t_release(csk->l2t);
800 cdev = csk->com.cdev;
801 spin_lock_bh(&cdev->cskq.lock);
802 list_del(&csk->list);
803 spin_unlock_bh(&cdev->cskq.lock);
805 cxgbit_free_skb(csk);
806 cxgbit_put_cdev(cdev);
811 static void cxgbit_set_tcp_window(struct cxgbit_sock *csk, struct port_info *pi)
813 unsigned int linkspeed;
816 linkspeed = pi->link_cfg.speed;
817 scale = linkspeed / SPEED_10000;
819 #define CXGBIT_10G_RCV_WIN (256 * 1024)
820 csk->rcv_win = CXGBIT_10G_RCV_WIN;
822 csk->rcv_win *= scale;
824 #define CXGBIT_10G_SND_WIN (256 * 1024)
825 csk->snd_win = CXGBIT_10G_SND_WIN;
827 csk->snd_win *= scale;
829 pr_debug("%s snd_win %d rcv_win %d\n",
830 __func__, csk->snd_win, csk->rcv_win);
833 #ifdef CONFIG_CHELSIO_T4_DCB
834 static u8 cxgbit_get_iscsi_dcb_state(struct net_device *ndev)
836 return ndev->dcbnl_ops->getstate(ndev);
839 static int cxgbit_select_priority(int pri_mask)
844 return (ffs(pri_mask) - 1);
847 static u8 cxgbit_get_iscsi_dcb_priority(struct net_device *ndev, u16 local_port)
852 struct dcb_app iscsi_dcb_app = {
853 .protocol = local_port
856 ret = (int)ndev->dcbnl_ops->getcap(ndev, DCB_CAP_ATTR_DCBX, &caps);
861 if (caps & DCB_CAP_DCBX_VER_IEEE) {
862 iscsi_dcb_app.selector = IEEE_8021QAZ_APP_SEL_ANY;
864 ret = dcb_ieee_getapp_mask(ndev, &iscsi_dcb_app);
866 } else if (caps & DCB_CAP_DCBX_VER_CEE) {
867 iscsi_dcb_app.selector = DCB_APP_IDTYPE_PORTNUM;
869 ret = dcb_getapp(ndev, &iscsi_dcb_app);
872 pr_info("iSCSI priority is set to %u\n", cxgbit_select_priority(ret));
874 return cxgbit_select_priority(ret);
879 cxgbit_offload_init(struct cxgbit_sock *csk, int iptype, __u8 *peer_ip,
880 u16 local_port, struct dst_entry *dst,
881 struct cxgbit_device *cdev)
885 struct net_device *ndev;
886 u16 rxq_idx, port_id;
887 #ifdef CONFIG_CHELSIO_T4_DCB
891 n = dst_neigh_lookup(dst, peer_ip);
897 if (n->dev->flags & IFF_LOOPBACK) {
899 ndev = cxgbit_ipv4_netdev(*(__be32 *)peer_ip);
900 else if (IS_ENABLED(CONFIG_IPV6))
901 ndev = cxgbit_ipv6_netdev((struct in6_addr *)peer_ip);
910 csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t,
914 csk->mtu = ndev->mtu;
915 csk->tx_chan = cxgb4_port_chan(ndev);
916 csk->smac_idx = cxgb4_tp_smt_idx(cdev->lldi.adapter_type,
917 cxgb4_port_viid(ndev));
918 step = cdev->lldi.ntxq /
920 csk->txq_idx = cxgb4_port_idx(ndev) * step;
921 step = cdev->lldi.nrxq /
923 csk->ctrlq_idx = cxgb4_port_idx(ndev);
924 csk->rss_qid = cdev->lldi.rxq_ids[
925 cxgb4_port_idx(ndev) * step];
926 csk->port_id = cxgb4_port_idx(ndev);
927 cxgbit_set_tcp_window(csk,
928 (struct port_info *)netdev_priv(ndev));
930 ndev = cxgbit_get_real_dev(n->dev);
936 #ifdef CONFIG_CHELSIO_T4_DCB
937 if (cxgbit_get_iscsi_dcb_state(ndev))
938 priority = cxgbit_get_iscsi_dcb_priority(ndev,
941 csk->dcb_priority = priority;
943 csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t, n, ndev, priority);
945 csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t, n, ndev, 0);
949 port_id = cxgb4_port_idx(ndev);
950 csk->mtu = dst_mtu(dst);
951 csk->tx_chan = cxgb4_port_chan(ndev);
952 csk->smac_idx = cxgb4_tp_smt_idx(cdev->lldi.adapter_type,
953 cxgb4_port_viid(ndev));
954 step = cdev->lldi.ntxq /
956 csk->txq_idx = (port_id * step) +
957 (cdev->selectq[port_id][0]++ % step);
958 csk->ctrlq_idx = cxgb4_port_idx(ndev);
959 step = cdev->lldi.nrxq /
961 rxq_idx = (port_id * step) +
962 (cdev->selectq[port_id][1]++ % step);
963 csk->rss_qid = cdev->lldi.rxq_ids[rxq_idx];
964 csk->port_id = port_id;
965 cxgbit_set_tcp_window(csk,
966 (struct port_info *)netdev_priv(ndev));
975 int cxgbit_ofld_send(struct cxgbit_device *cdev, struct sk_buff *skb)
979 if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
981 pr_err("%s - device not up - dropping\n", __func__);
985 ret = cxgb4_ofld_send(cdev->lldi.ports[0], skb);
988 return ret < 0 ? ret : 0;
991 static void cxgbit_release_tid(struct cxgbit_device *cdev, u32 tid)
993 u32 len = roundup(sizeof(struct cpl_tid_release), 16);
996 skb = alloc_skb(len, GFP_ATOMIC);
1000 cxgb_mk_tid_release(skb, len, tid, 0);
1001 cxgbit_ofld_send(cdev, skb);
1005 cxgbit_l2t_send(struct cxgbit_device *cdev, struct sk_buff *skb,
1006 struct l2t_entry *l2e)
1010 if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
1012 pr_err("%s - device not up - dropping\n", __func__);
1016 ret = cxgb4_l2t_send(cdev->lldi.ports[0], skb, l2e);
1019 return ret < 0 ? ret : 0;
1022 static void cxgbit_send_rx_credits(struct cxgbit_sock *csk, struct sk_buff *skb)
1024 if (csk->com.state != CSK_STATE_ESTABLISHED) {
1029 cxgbit_ofld_send(csk->com.cdev, skb);
1033 * CPL connection rx data ack: host ->
1034 * Send RX credits through an RX_DATA_ACK CPL message.
1035 * Returns the number of credits sent.
1037 int cxgbit_rx_data_ack(struct cxgbit_sock *csk)
1039 struct sk_buff *skb;
1040 u32 len = roundup(sizeof(struct cpl_rx_data_ack), 16);
1043 skb = alloc_skb(len, GFP_KERNEL);
1047 credit_dack = RX_DACK_CHANGE_F | RX_DACK_MODE_V(1) |
1048 RX_CREDITS_V(csk->rx_credits);
1050 cxgb_mk_rx_data_ack(skb, len, csk->tid, csk->ctrlq_idx,
1053 csk->rx_credits = 0;
1055 spin_lock_bh(&csk->lock);
1056 if (csk->lock_owner) {
1057 cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_send_rx_credits;
1058 __skb_queue_tail(&csk->backlogq, skb);
1059 spin_unlock_bh(&csk->lock);
1063 cxgbit_send_rx_credits(csk, skb);
1064 spin_unlock_bh(&csk->lock);
1069 #define FLOWC_WR_NPARAMS_MIN 9
1070 #define FLOWC_WR_NPARAMS_MAX 11
1071 static int cxgbit_alloc_csk_skb(struct cxgbit_sock *csk)
1073 struct sk_buff *skb;
1077 flowclen = offsetof(struct fw_flowc_wr,
1078 mnemval[FLOWC_WR_NPARAMS_MAX]);
1080 len = max_t(u32, sizeof(struct cpl_abort_req),
1081 sizeof(struct cpl_abort_rpl));
1083 len = max(len, flowclen);
1084 len = roundup(len, 16);
1086 for (i = 0; i < 3; i++) {
1087 skb = alloc_skb(len, GFP_ATOMIC);
1090 __skb_queue_tail(&csk->skbq, skb);
1093 skb = alloc_skb(LRO_SKB_MIN_HEADROOM, GFP_ATOMIC);
1097 memset(skb->data, 0, LRO_SKB_MIN_HEADROOM);
1098 csk->lro_hskb = skb;
1102 __skb_queue_purge(&csk->skbq);
1107 cxgbit_pass_accept_rpl(struct cxgbit_sock *csk, struct cpl_pass_accept_req *req)
1109 struct sk_buff *skb;
1110 const struct tcphdr *tcph;
1111 struct cpl_t5_pass_accept_rpl *rpl5;
1112 struct cxgb4_lld_info *lldi = &csk->com.cdev->lldi;
1113 unsigned int len = roundup(sizeof(*rpl5), 16);
1114 unsigned int mtu_idx;
1120 pr_debug("%s csk %p tid %u\n", __func__, csk, csk->tid);
1122 skb = alloc_skb(len, GFP_ATOMIC);
1124 cxgbit_put_csk(csk);
1128 rpl5 = __skb_put_zero(skb, len);
1130 INIT_TP_WR(rpl5, csk->tid);
1131 OPCODE_TID(rpl5) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1133 cxgb_best_mtu(csk->com.cdev->lldi.mtus, csk->mtu, &mtu_idx,
1135 (csk->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
1136 wscale = cxgb_compute_wscale(csk->rcv_win);
1138 * Specify the largest window that will fit in opt0. The
1139 * remainder will be specified in the rx_data_ack.
1141 win = csk->rcv_win >> 10;
1142 if (win > RCV_BUFSIZ_M)
1144 opt0 = TCAM_BYPASS_F |
1145 WND_SCALE_V(wscale) |
1146 MSS_IDX_V(mtu_idx) |
1147 L2T_IDX_V(csk->l2t->idx) |
1148 TX_CHAN_V(csk->tx_chan) |
1149 SMAC_SEL_V(csk->smac_idx) |
1150 DSCP_V(csk->tos >> 2) |
1151 ULP_MODE_V(ULP_MODE_ISCSI) |
1154 opt2 = RX_CHANNEL_V(0) |
1155 RSS_QUEUE_VALID_F | RSS_QUEUE_V(csk->rss_qid);
1157 if (!is_t5(lldi->adapter_type))
1158 opt2 |= RX_FC_DISABLE_F;
1160 if (req->tcpopt.tstamp)
1161 opt2 |= TSTAMPS_EN_F;
1162 if (req->tcpopt.sack)
1165 opt2 |= WND_SCALE_EN_F;
1167 hlen = ntohl(req->hdr_len);
1169 if (is_t5(lldi->adapter_type))
1170 tcph = (struct tcphdr *)((u8 *)(req + 1) +
1171 ETH_HDR_LEN_G(hlen) + IP_HDR_LEN_G(hlen));
1173 tcph = (struct tcphdr *)((u8 *)(req + 1) +
1174 T6_ETH_HDR_LEN_G(hlen) + T6_IP_HDR_LEN_G(hlen));
1176 if (tcph->ece && tcph->cwr)
1177 opt2 |= CCTRL_ECN_V(1);
1179 opt2 |= RX_COALESCE_V(3);
1180 opt2 |= CONG_CNTRL_V(CONG_ALG_NEWRENO);
1183 rpl5->iss = cpu_to_be32((prandom_u32() & ~7UL) - 1);
1185 opt2 |= T5_OPT_2_VALID_F;
1187 rpl5->opt0 = cpu_to_be64(opt0);
1188 rpl5->opt2 = cpu_to_be32(opt2);
1189 set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->ctrlq_idx);
1190 t4_set_arp_err_handler(skb, NULL, cxgbit_arp_failure_discard);
1191 cxgbit_l2t_send(csk->com.cdev, skb, csk->l2t);
1195 cxgbit_pass_accept_req(struct cxgbit_device *cdev, struct sk_buff *skb)
1197 struct cxgbit_sock *csk = NULL;
1198 struct cxgbit_np *cnp;
1199 struct cpl_pass_accept_req *req = cplhdr(skb);
1200 unsigned int stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
1201 struct tid_info *t = cdev->lldi.tids;
1202 unsigned int tid = GET_TID(req);
1203 u16 peer_mss = ntohs(req->tcpopt.mss);
1204 unsigned short hdrs;
1206 struct dst_entry *dst;
1207 __u8 local_ip[16], peer_ip[16];
1208 __be16 local_port, peer_port;
1212 pr_debug("%s: cdev = %p; stid = %u; tid = %u\n",
1213 __func__, cdev, stid, tid);
1215 cnp = lookup_stid(t, stid);
1217 pr_err("%s connect request on invalid stid %d\n",
1222 if (cnp->com.state != CSK_STATE_LISTEN) {
1223 pr_err("%s - listening parent not in CSK_STATE_LISTEN\n",
1228 csk = lookup_tid(t, tid);
1230 pr_err("%s csk not null tid %u\n",
1235 cxgb_get_4tuple(req, cdev->lldi.adapter_type, &iptype, local_ip,
1236 peer_ip, &local_port, &peer_port);
1238 /* Find output route */
1240 pr_debug("%s parent sock %p tid %u laddr %pI4 raddr %pI4 "
1241 "lport %d rport %d peer_mss %d\n"
1242 , __func__, cnp, tid,
1243 local_ip, peer_ip, ntohs(local_port),
1244 ntohs(peer_port), peer_mss);
1245 dst = cxgb_find_route(&cdev->lldi, cxgbit_get_real_dev,
1246 *(__be32 *)local_ip,
1248 local_port, peer_port,
1249 PASS_OPEN_TOS_G(ntohl(req->tos_stid)));
1251 pr_debug("%s parent sock %p tid %u laddr %pI6 raddr %pI6 "
1252 "lport %d rport %d peer_mss %d\n"
1253 , __func__, cnp, tid,
1254 local_ip, peer_ip, ntohs(local_port),
1255 ntohs(peer_port), peer_mss);
1256 dst = cxgb_find_route6(&cdev->lldi, cxgbit_get_real_dev,
1258 local_port, peer_port,
1259 PASS_OPEN_TOS_G(ntohl(req->tos_stid)),
1260 ((struct sockaddr_in6 *)
1261 &cnp->com.local_addr)->sin6_scope_id);
1264 pr_err("%s - failed to find dst entry!\n",
1269 csk = kzalloc(sizeof(*csk), GFP_ATOMIC);
1275 ret = cxgbit_offload_init(csk, iptype, peer_ip, ntohs(local_port),
1278 pr_err("%s - failed to allocate l2t entry!\n",
1285 kref_init(&csk->kref);
1286 init_completion(&csk->com.wr_wait.completion);
1288 INIT_LIST_HEAD(&csk->accept_node);
1290 hdrs = (iptype == 4 ? sizeof(struct iphdr) : sizeof(struct ipv6hdr)) +
1291 sizeof(struct tcphdr) + (req->tcpopt.tstamp ? 12 : 0);
1292 if (peer_mss && csk->mtu > (peer_mss + hdrs))
1293 csk->mtu = peer_mss + hdrs;
1295 csk->com.state = CSK_STATE_CONNECTING;
1296 csk->com.cdev = cdev;
1298 csk->tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));
1301 csk->wr_cred = cdev->lldi.wr_cred -
1302 DIV_ROUND_UP(sizeof(struct cpl_abort_req), 16);
1303 csk->wr_max_cred = csk->wr_cred;
1304 csk->wr_una_cred = 0;
1307 struct sockaddr_in *sin = (struct sockaddr_in *)
1308 &csk->com.local_addr;
1309 sin->sin_family = AF_INET;
1310 sin->sin_port = local_port;
1311 sin->sin_addr.s_addr = *(__be32 *)local_ip;
1313 sin = (struct sockaddr_in *)&csk->com.remote_addr;
1314 sin->sin_family = AF_INET;
1315 sin->sin_port = peer_port;
1316 sin->sin_addr.s_addr = *(__be32 *)peer_ip;
1318 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
1319 &csk->com.local_addr;
1321 sin6->sin6_family = PF_INET6;
1322 sin6->sin6_port = local_port;
1323 memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
1324 cxgb4_clip_get(cdev->lldi.ports[0],
1325 (const u32 *)&sin6->sin6_addr.s6_addr,
1328 sin6 = (struct sockaddr_in6 *)&csk->com.remote_addr;
1329 sin6->sin6_family = PF_INET6;
1330 sin6->sin6_port = peer_port;
1331 memcpy(sin6->sin6_addr.s6_addr, peer_ip, 16);
1334 skb_queue_head_init(&csk->rxq);
1335 skb_queue_head_init(&csk->txq);
1336 skb_queue_head_init(&csk->ppodq);
1337 skb_queue_head_init(&csk->backlogq);
1338 skb_queue_head_init(&csk->skbq);
1339 cxgbit_sock_reset_wr_list(csk);
1340 spin_lock_init(&csk->lock);
1341 init_waitqueue_head(&csk->waitq);
1342 init_waitqueue_head(&csk->ack_waitq);
1343 csk->lock_owner = false;
1345 if (cxgbit_alloc_csk_skb(csk)) {
1351 cxgbit_get_cdev(cdev);
1353 spin_lock(&cdev->cskq.lock);
1354 list_add_tail(&csk->list, &cdev->cskq.list);
1355 spin_unlock(&cdev->cskq.lock);
1356 cxgb4_insert_tid(t, csk, tid, csk->com.local_addr.ss_family);
1357 cxgbit_pass_accept_rpl(csk, req);
1361 cxgbit_release_tid(cdev, tid);
1367 cxgbit_tx_flowc_wr_credits(struct cxgbit_sock *csk, u32 *nparamsp,
1370 u32 nparams, flowclen16, flowclen;
1372 nparams = FLOWC_WR_NPARAMS_MIN;
1374 if (csk->snd_wscale)
1377 #ifdef CONFIG_CHELSIO_T4_DCB
1380 flowclen = offsetof(struct fw_flowc_wr, mnemval[nparams]);
1381 flowclen16 = DIV_ROUND_UP(flowclen, 16);
1382 flowclen = flowclen16 * 16;
1384 * Return the number of 16-byte credits used by the flowc request.
1385 * Pass back the nparams and actual flowc length if requested.
1388 *nparamsp = nparams;
1390 *flowclenp = flowclen;
1394 u32 cxgbit_send_tx_flowc_wr(struct cxgbit_sock *csk)
1396 struct cxgbit_device *cdev = csk->com.cdev;
1397 struct fw_flowc_wr *flowc;
1398 u32 nparams, flowclen16, flowclen;
1399 struct sk_buff *skb;
1402 #ifdef CONFIG_CHELSIO_T4_DCB
1403 u16 vlan = ((struct l2t_entry *)csk->l2t)->vlan;
1406 flowclen16 = cxgbit_tx_flowc_wr_credits(csk, &nparams, &flowclen);
1408 skb = __skb_dequeue(&csk->skbq);
1409 flowc = __skb_put_zero(skb, flowclen);
1411 flowc->op_to_nparams = cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
1412 FW_FLOWC_WR_NPARAMS_V(nparams));
1413 flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(flowclen16) |
1414 FW_WR_FLOWID_V(csk->tid));
1415 flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
1416 flowc->mnemval[0].val = cpu_to_be32(FW_PFVF_CMD_PFN_V
1417 (csk->com.cdev->lldi.pf));
1418 flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
1419 flowc->mnemval[1].val = cpu_to_be32(csk->tx_chan);
1420 flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
1421 flowc->mnemval[2].val = cpu_to_be32(csk->tx_chan);
1422 flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
1423 flowc->mnemval[3].val = cpu_to_be32(csk->rss_qid);
1424 flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
1425 flowc->mnemval[4].val = cpu_to_be32(csk->snd_nxt);
1426 flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
1427 flowc->mnemval[5].val = cpu_to_be32(csk->rcv_nxt);
1428 flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
1429 flowc->mnemval[6].val = cpu_to_be32(csk->snd_win);
1430 flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
1431 flowc->mnemval[7].val = cpu_to_be32(csk->emss);
1433 flowc->mnemval[8].mnemonic = FW_FLOWC_MNEM_TXDATAPLEN_MAX;
1434 if (test_bit(CDEV_ISO_ENABLE, &cdev->flags))
1435 flowc->mnemval[8].val = cpu_to_be32(CXGBIT_MAX_ISO_PAYLOAD);
1437 flowc->mnemval[8].val = cpu_to_be32(16384);
1441 if (csk->snd_wscale) {
1442 flowc->mnemval[index].mnemonic = FW_FLOWC_MNEM_RCV_SCALE;
1443 flowc->mnemval[index].val = cpu_to_be32(csk->snd_wscale);
1447 #ifdef CONFIG_CHELSIO_T4_DCB
1448 flowc->mnemval[index].mnemonic = FW_FLOWC_MNEM_DCBPRIO;
1449 if (vlan == VLAN_NONE) {
1450 pr_warn("csk %u without VLAN Tag on DCB Link\n", csk->tid);
1451 flowc->mnemval[index].val = cpu_to_be32(0);
1453 flowc->mnemval[index].val = cpu_to_be32(
1454 (vlan & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT);
1457 pr_debug("%s: csk %p; tx_chan = %u; rss_qid = %u; snd_seq = %u;"
1458 " rcv_seq = %u; snd_win = %u; emss = %u\n",
1459 __func__, csk, csk->tx_chan, csk->rss_qid, csk->snd_nxt,
1460 csk->rcv_nxt, csk->snd_win, csk->emss);
1461 set_wr_txq(skb, CPL_PRIORITY_DATA, csk->txq_idx);
1462 cxgbit_ofld_send(csk->com.cdev, skb);
1466 int cxgbit_setup_conn_digest(struct cxgbit_sock *csk)
1468 struct sk_buff *skb;
1469 struct cpl_set_tcb_field *req;
1470 u8 hcrc = csk->submode & CXGBIT_SUBMODE_HCRC;
1471 u8 dcrc = csk->submode & CXGBIT_SUBMODE_DCRC;
1472 unsigned int len = roundup(sizeof(*req), 16);
1475 skb = alloc_skb(len, GFP_KERNEL);
1479 /* set up ulp submode */
1480 req = __skb_put_zero(skb, len);
1482 INIT_TP_WR(req, csk->tid);
1483 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
1484 req->reply_ctrl = htons(NO_REPLY_V(0) | QUEUENO_V(csk->rss_qid));
1485 req->word_cookie = htons(0);
1486 req->mask = cpu_to_be64(0x3 << 4);
1487 req->val = cpu_to_be64(((hcrc ? ULP_CRC_HEADER : 0) |
1488 (dcrc ? ULP_CRC_DATA : 0)) << 4);
1489 set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->ctrlq_idx);
1491 cxgbit_get_csk(csk);
1492 cxgbit_init_wr_wait(&csk->com.wr_wait);
1494 cxgbit_ofld_send(csk->com.cdev, skb);
1496 ret = cxgbit_wait_for_reply(csk->com.cdev,
1498 csk->tid, 5, __func__);
1505 int cxgbit_setup_conn_pgidx(struct cxgbit_sock *csk, u32 pg_idx)
1507 struct sk_buff *skb;
1508 struct cpl_set_tcb_field *req;
1509 unsigned int len = roundup(sizeof(*req), 16);
1512 skb = alloc_skb(len, GFP_KERNEL);
1516 req = __skb_put_zero(skb, len);
1518 INIT_TP_WR(req, csk->tid);
1519 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
1520 req->reply_ctrl = htons(NO_REPLY_V(0) | QUEUENO_V(csk->rss_qid));
1521 req->word_cookie = htons(0);
1522 req->mask = cpu_to_be64(0x3 << 8);
1523 req->val = cpu_to_be64(pg_idx << 8);
1524 set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->ctrlq_idx);
1526 cxgbit_get_csk(csk);
1527 cxgbit_init_wr_wait(&csk->com.wr_wait);
1529 cxgbit_ofld_send(csk->com.cdev, skb);
1531 ret = cxgbit_wait_for_reply(csk->com.cdev,
1533 csk->tid, 5, __func__);
1541 cxgbit_pass_open_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1543 struct cpl_pass_open_rpl *rpl = cplhdr(skb);
1544 struct tid_info *t = cdev->lldi.tids;
1545 unsigned int stid = GET_TID(rpl);
1546 struct cxgbit_np *cnp = lookup_stid(t, stid);
1548 pr_debug("%s: cnp = %p; stid = %u; status = %d\n",
1549 __func__, cnp, stid, rpl->status);
1552 pr_info("%s stid %d lookup failure\n", __func__, stid);
1556 cxgbit_wake_up(&cnp->com.wr_wait, __func__, rpl->status);
1557 cxgbit_put_cnp(cnp);
1563 cxgbit_close_listsrv_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1565 struct cpl_close_listsvr_rpl *rpl = cplhdr(skb);
1566 struct tid_info *t = cdev->lldi.tids;
1567 unsigned int stid = GET_TID(rpl);
1568 struct cxgbit_np *cnp = lookup_stid(t, stid);
1570 pr_debug("%s: cnp = %p; stid = %u; status = %d\n",
1571 __func__, cnp, stid, rpl->status);
1574 pr_info("%s stid %d lookup failure\n", __func__, stid);
1578 cxgbit_wake_up(&cnp->com.wr_wait, __func__, rpl->status);
1579 cxgbit_put_cnp(cnp);
1585 cxgbit_pass_establish(struct cxgbit_device *cdev, struct sk_buff *skb)
1587 struct cpl_pass_establish *req = cplhdr(skb);
1588 struct tid_info *t = cdev->lldi.tids;
1589 unsigned int tid = GET_TID(req);
1590 struct cxgbit_sock *csk;
1591 struct cxgbit_np *cnp;
1592 u16 tcp_opt = be16_to_cpu(req->tcp_opt);
1593 u32 snd_isn = be32_to_cpu(req->snd_isn);
1594 u32 rcv_isn = be32_to_cpu(req->rcv_isn);
1596 csk = lookup_tid(t, tid);
1597 if (unlikely(!csk)) {
1598 pr_err("can't find connection for tid %u.\n", tid);
1603 pr_debug("%s: csk %p; tid %u; cnp %p\n",
1604 __func__, csk, tid, cnp);
1606 csk->write_seq = snd_isn;
1607 csk->snd_una = snd_isn;
1608 csk->snd_nxt = snd_isn;
1610 csk->rcv_nxt = rcv_isn;
1612 if (csk->rcv_win > (RCV_BUFSIZ_M << 10))
1613 csk->rx_credits = (csk->rcv_win - (RCV_BUFSIZ_M << 10));
1615 csk->snd_wscale = TCPOPT_SND_WSCALE_G(tcp_opt);
1616 cxgbit_set_emss(csk, tcp_opt);
1617 dst_confirm(csk->dst);
1618 csk->com.state = CSK_STATE_ESTABLISHED;
1619 spin_lock_bh(&cnp->np_accept_lock);
1620 list_add_tail(&csk->accept_node, &cnp->np_accept_list);
1621 spin_unlock_bh(&cnp->np_accept_lock);
1622 complete(&cnp->accept_comp);
1627 static void cxgbit_queue_rx_skb(struct cxgbit_sock *csk, struct sk_buff *skb)
1629 cxgbit_skcb_flags(skb) = 0;
1630 spin_lock_bh(&csk->rxq.lock);
1631 __skb_queue_tail(&csk->rxq, skb);
1632 spin_unlock_bh(&csk->rxq.lock);
1633 wake_up(&csk->waitq);
1636 static void cxgbit_peer_close(struct cxgbit_sock *csk, struct sk_buff *skb)
1638 pr_debug("%s: csk %p; tid %u; state %d\n",
1639 __func__, csk, csk->tid, csk->com.state);
1641 switch (csk->com.state) {
1642 case CSK_STATE_ESTABLISHED:
1643 csk->com.state = CSK_STATE_CLOSING;
1644 cxgbit_queue_rx_skb(csk, skb);
1646 case CSK_STATE_CLOSING:
1647 /* simultaneous close */
1648 csk->com.state = CSK_STATE_MORIBUND;
1650 case CSK_STATE_MORIBUND:
1651 csk->com.state = CSK_STATE_DEAD;
1652 cxgbit_put_csk(csk);
1654 case CSK_STATE_ABORTING:
1657 pr_info("%s: cpl_peer_close in bad state %d\n",
1658 __func__, csk->com.state);
1664 static void cxgbit_close_con_rpl(struct cxgbit_sock *csk, struct sk_buff *skb)
1666 pr_debug("%s: csk %p; tid %u; state %d\n",
1667 __func__, csk, csk->tid, csk->com.state);
1669 switch (csk->com.state) {
1670 case CSK_STATE_CLOSING:
1671 csk->com.state = CSK_STATE_MORIBUND;
1673 case CSK_STATE_MORIBUND:
1674 csk->com.state = CSK_STATE_DEAD;
1675 cxgbit_put_csk(csk);
1677 case CSK_STATE_ABORTING:
1678 case CSK_STATE_DEAD:
1681 pr_info("%s: cpl_close_con_rpl in bad state %d\n",
1682 __func__, csk->com.state);
1688 static void cxgbit_abort_req_rss(struct cxgbit_sock *csk, struct sk_buff *skb)
1690 struct cpl_abort_req_rss *hdr = cplhdr(skb);
1691 unsigned int tid = GET_TID(hdr);
1692 struct sk_buff *rpl_skb;
1693 bool release = false;
1694 bool wakeup_thread = false;
1695 u32 len = roundup(sizeof(struct cpl_abort_rpl), 16);
1697 pr_debug("%s: csk %p; tid %u; state %d\n",
1698 __func__, csk, tid, csk->com.state);
1700 if (cxgb_is_neg_adv(hdr->status)) {
1701 pr_err("%s: got neg advise %d on tid %u\n",
1702 __func__, hdr->status, tid);
1706 switch (csk->com.state) {
1707 case CSK_STATE_CONNECTING:
1708 case CSK_STATE_MORIBUND:
1709 csk->com.state = CSK_STATE_DEAD;
1712 case CSK_STATE_ESTABLISHED:
1713 csk->com.state = CSK_STATE_DEAD;
1714 wakeup_thread = true;
1716 case CSK_STATE_CLOSING:
1717 csk->com.state = CSK_STATE_DEAD;
1721 case CSK_STATE_ABORTING:
1724 pr_info("%s: cpl_abort_req_rss in bad state %d\n",
1725 __func__, csk->com.state);
1726 csk->com.state = CSK_STATE_DEAD;
1729 __skb_queue_purge(&csk->txq);
1731 if (!test_and_set_bit(CSK_TX_DATA_SENT, &csk->com.flags))
1732 cxgbit_send_tx_flowc_wr(csk);
1734 rpl_skb = __skb_dequeue(&csk->skbq);
1736 cxgb_mk_abort_rpl(rpl_skb, len, csk->tid, csk->txq_idx);
1737 cxgbit_ofld_send(csk->com.cdev, rpl_skb);
1739 if (wakeup_thread) {
1740 cxgbit_queue_rx_skb(csk, skb);
1745 cxgbit_put_csk(csk);
1750 static void cxgbit_abort_rpl_rss(struct cxgbit_sock *csk, struct sk_buff *skb)
1752 struct cpl_abort_rpl_rss *rpl = cplhdr(skb);
1754 pr_debug("%s: csk %p; tid %u; state %d\n",
1755 __func__, csk, csk->tid, csk->com.state);
1757 switch (csk->com.state) {
1758 case CSK_STATE_ABORTING:
1759 csk->com.state = CSK_STATE_DEAD;
1760 if (test_bit(CSK_ABORT_RPL_WAIT, &csk->com.flags))
1761 cxgbit_wake_up(&csk->com.wr_wait, __func__,
1763 cxgbit_put_csk(csk);
1766 pr_info("%s: cpl_abort_rpl_rss in state %d\n",
1767 __func__, csk->com.state);
1773 static bool cxgbit_credit_err(const struct cxgbit_sock *csk)
1775 const struct sk_buff *skb = csk->wr_pending_head;
1778 if (unlikely(csk->wr_cred > csk->wr_max_cred)) {
1779 pr_err("csk 0x%p, tid %u, credit %u > %u\n",
1780 csk, csk->tid, csk->wr_cred, csk->wr_max_cred);
1785 credit += (__force u32)skb->csum;
1786 skb = cxgbit_skcb_tx_wr_next(skb);
1789 if (unlikely((csk->wr_cred + credit) != csk->wr_max_cred)) {
1790 pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
1791 csk, csk->tid, csk->wr_cred,
1792 credit, csk->wr_max_cred);
1800 static void cxgbit_fw4_ack(struct cxgbit_sock *csk, struct sk_buff *skb)
1802 struct cpl_fw4_ack *rpl = (struct cpl_fw4_ack *)cplhdr(skb);
1803 u32 credits = rpl->credits;
1804 u32 snd_una = ntohl(rpl->snd_una);
1806 csk->wr_cred += credits;
1807 if (csk->wr_una_cred > (csk->wr_max_cred - csk->wr_cred))
1808 csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
1811 struct sk_buff *p = cxgbit_sock_peek_wr(csk);
1812 const u32 csum = (__force u32)p->csum;
1815 pr_err("csk 0x%p,%u, cr %u,%u+%u, empty.\n",
1816 csk, csk->tid, credits,
1817 csk->wr_cred, csk->wr_una_cred);
1821 if (unlikely(credits < csum)) {
1822 pr_warn("csk 0x%p,%u, cr %u,%u+%u, < %u.\n",
1824 credits, csk->wr_cred, csk->wr_una_cred,
1826 p->csum = (__force __wsum)(csum - credits);
1830 cxgbit_sock_dequeue_wr(csk);
1835 if (unlikely(cxgbit_credit_err(csk))) {
1836 cxgbit_queue_rx_skb(csk, skb);
1840 if (rpl->seq_vld & CPL_FW4_ACK_FLAGS_SEQVAL) {
1841 if (unlikely(before(snd_una, csk->snd_una))) {
1842 pr_warn("csk 0x%p,%u, snd_una %u/%u.",
1843 csk, csk->tid, snd_una,
1848 if (csk->snd_una != snd_una) {
1849 csk->snd_una = snd_una;
1850 dst_confirm(csk->dst);
1851 wake_up(&csk->ack_waitq);
1855 if (skb_queue_len(&csk->txq))
1856 cxgbit_push_tx_frames(csk);
1862 static void cxgbit_set_tcb_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1864 struct cxgbit_sock *csk;
1865 struct cpl_set_tcb_rpl *rpl = (struct cpl_set_tcb_rpl *)skb->data;
1866 unsigned int tid = GET_TID(rpl);
1867 struct cxgb4_lld_info *lldi = &cdev->lldi;
1868 struct tid_info *t = lldi->tids;
1870 csk = lookup_tid(t, tid);
1871 if (unlikely(!csk)) {
1872 pr_err("can't find connection for tid %u.\n", tid);
1875 cxgbit_wake_up(&csk->com.wr_wait, __func__, rpl->status);
1878 cxgbit_put_csk(csk);
1883 static void cxgbit_rx_data(struct cxgbit_device *cdev, struct sk_buff *skb)
1885 struct cxgbit_sock *csk;
1886 struct cpl_rx_data *cpl = cplhdr(skb);
1887 unsigned int tid = GET_TID(cpl);
1888 struct cxgb4_lld_info *lldi = &cdev->lldi;
1889 struct tid_info *t = lldi->tids;
1891 csk = lookup_tid(t, tid);
1892 if (unlikely(!csk)) {
1893 pr_err("can't find conn. for tid %u.\n", tid);
1897 cxgbit_queue_rx_skb(csk, skb);
1904 __cxgbit_process_rx_cpl(struct cxgbit_sock *csk, struct sk_buff *skb)
1906 spin_lock(&csk->lock);
1907 if (csk->lock_owner) {
1908 __skb_queue_tail(&csk->backlogq, skb);
1909 spin_unlock(&csk->lock);
1913 cxgbit_skcb_rx_backlog_fn(skb)(csk, skb);
1914 spin_unlock(&csk->lock);
1917 static void cxgbit_process_rx_cpl(struct cxgbit_sock *csk, struct sk_buff *skb)
1919 cxgbit_get_csk(csk);
1920 __cxgbit_process_rx_cpl(csk, skb);
1921 cxgbit_put_csk(csk);
1924 static void cxgbit_rx_cpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1926 struct cxgbit_sock *csk;
1927 struct cpl_tx_data *cpl = cplhdr(skb);
1928 struct cxgb4_lld_info *lldi = &cdev->lldi;
1929 struct tid_info *t = lldi->tids;
1930 unsigned int tid = GET_TID(cpl);
1931 u8 opcode = cxgbit_skcb_rx_opcode(skb);
1936 cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_fw4_ack;
1939 case CPL_PEER_CLOSE:
1940 cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_peer_close;
1942 case CPL_CLOSE_CON_RPL:
1943 cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_close_con_rpl;
1945 case CPL_ABORT_REQ_RSS:
1946 cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_abort_req_rss;
1948 case CPL_ABORT_RPL_RSS:
1949 cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_abort_rpl_rss;
1955 csk = lookup_tid(t, tid);
1956 if (unlikely(!csk)) {
1957 pr_err("can't find conn. for tid %u.\n", tid);
1962 cxgbit_process_rx_cpl(csk, skb);
1964 __cxgbit_process_rx_cpl(csk, skb);
1971 cxgbit_cplhandler_func cxgbit_cplhandlers[NUM_CPL_CMDS] = {
1972 [CPL_PASS_OPEN_RPL] = cxgbit_pass_open_rpl,
1973 [CPL_CLOSE_LISTSRV_RPL] = cxgbit_close_listsrv_rpl,
1974 [CPL_PASS_ACCEPT_REQ] = cxgbit_pass_accept_req,
1975 [CPL_PASS_ESTABLISH] = cxgbit_pass_establish,
1976 [CPL_SET_TCB_RPL] = cxgbit_set_tcb_rpl,
1977 [CPL_RX_DATA] = cxgbit_rx_data,
1978 [CPL_FW4_ACK] = cxgbit_rx_cpl,
1979 [CPL_PEER_CLOSE] = cxgbit_rx_cpl,
1980 [CPL_CLOSE_CON_RPL] = cxgbit_rx_cpl,
1981 [CPL_ABORT_REQ_RSS] = cxgbit_rx_cpl,
1982 [CPL_ABORT_RPL_RSS] = cxgbit_rx_cpl,