1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 Chelsio Communications, Inc.
6 #include <linux/module.h>
7 #include <linux/list.h>
8 #include <linux/workqueue.h>
9 #include <linux/skbuff.h>
10 #include <linux/timer.h>
11 #include <linux/notifier.h>
12 #include <linux/inetdevice.h>
14 #include <linux/tcp.h>
15 #include <linux/if_vlan.h>
17 #include <net/neighbour.h>
18 #include <net/netevent.h>
19 #include <net/route.h>
21 #include <net/ip6_route.h>
22 #include <net/addrconf.h>
24 #include <libcxgb_cm.h>
28 static void cxgbit_init_wr_wait(struct cxgbit_wr_wait
*wr_waitp
)
31 reinit_completion(&wr_waitp
->completion
);
35 cxgbit_wake_up(struct cxgbit_wr_wait
*wr_waitp
, const char *func
, u8 ret
)
37 if (ret
== CPL_ERR_NONE
)
43 pr_err("%s: err:%u", func
, ret
);
45 complete(&wr_waitp
->completion
);
49 cxgbit_wait_for_reply(struct cxgbit_device
*cdev
,
50 struct cxgbit_wr_wait
*wr_waitp
, u32 tid
, u32 timeout
,
55 if (!test_bit(CDEV_STATE_UP
, &cdev
->flags
)) {
60 ret
= wait_for_completion_timeout(&wr_waitp
->completion
, timeout
* HZ
);
62 pr_info("%s - Device %s not responding tid %u\n",
63 func
, pci_name(cdev
->lldi
.pdev
), tid
);
64 wr_waitp
->ret
= -ETIMEDOUT
;
68 pr_info("%s: FW reply %d tid %u\n",
69 pci_name(cdev
->lldi
.pdev
), wr_waitp
->ret
, tid
);
73 static int cxgbit_np_hashfn(const struct cxgbit_np
*cnp
)
75 return ((unsigned long)cnp
>> 10) & (NP_INFO_HASH_SIZE
- 1);
78 static struct np_info
*
79 cxgbit_np_hash_add(struct cxgbit_device
*cdev
, struct cxgbit_np
*cnp
,
82 struct np_info
*p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
85 int bucket
= cxgbit_np_hashfn(cnp
);
89 spin_lock(&cdev
->np_lock
);
90 p
->next
= cdev
->np_hash_tab
[bucket
];
91 cdev
->np_hash_tab
[bucket
] = p
;
92 spin_unlock(&cdev
->np_lock
);
99 cxgbit_np_hash_find(struct cxgbit_device
*cdev
, struct cxgbit_np
*cnp
)
101 int stid
= -1, bucket
= cxgbit_np_hashfn(cnp
);
104 spin_lock(&cdev
->np_lock
);
105 for (p
= cdev
->np_hash_tab
[bucket
]; p
; p
= p
->next
) {
111 spin_unlock(&cdev
->np_lock
);
116 static int cxgbit_np_hash_del(struct cxgbit_device
*cdev
, struct cxgbit_np
*cnp
)
118 int stid
= -1, bucket
= cxgbit_np_hashfn(cnp
);
119 struct np_info
*p
, **prev
= &cdev
->np_hash_tab
[bucket
];
121 spin_lock(&cdev
->np_lock
);
122 for (p
= *prev
; p
; prev
= &p
->next
, p
= p
->next
) {
130 spin_unlock(&cdev
->np_lock
);
135 void _cxgbit_free_cnp(struct kref
*kref
)
137 struct cxgbit_np
*cnp
;
139 cnp
= container_of(kref
, struct cxgbit_np
, kref
);
144 cxgbit_create_server6(struct cxgbit_device
*cdev
, unsigned int stid
,
145 struct cxgbit_np
*cnp
)
147 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)
148 &cnp
->com
.local_addr
;
152 pr_debug("%s: dev = %s; stid = %u; sin6_port = %u\n",
153 __func__
, cdev
->lldi
.ports
[0]->name
, stid
, sin6
->sin6_port
);
155 addr_type
= ipv6_addr_type((const struct in6_addr
*)
157 if (addr_type
!= IPV6_ADDR_ANY
) {
158 ret
= cxgb4_clip_get(cdev
->lldi
.ports
[0],
159 (const u32
*)&sin6
->sin6_addr
.s6_addr
, 1);
161 pr_err("Unable to find clip table entry. laddr %pI6. Error:%d.\n",
162 sin6
->sin6_addr
.s6_addr
, ret
);
168 cxgbit_init_wr_wait(&cnp
->com
.wr_wait
);
170 ret
= cxgb4_create_server6(cdev
->lldi
.ports
[0],
171 stid
, &sin6
->sin6_addr
,
173 cdev
->lldi
.rxq_ids
[0]);
175 ret
= cxgbit_wait_for_reply(cdev
, &cnp
->com
.wr_wait
,
178 ret
= net_xmit_errno(ret
);
183 if (ret
!= -ETIMEDOUT
)
184 cxgb4_clip_release(cdev
->lldi
.ports
[0],
185 (const u32
*)&sin6
->sin6_addr
.s6_addr
, 1);
187 pr_err("create server6 err %d stid %d laddr %pI6 lport %d\n",
188 ret
, stid
, sin6
->sin6_addr
.s6_addr
,
189 ntohs(sin6
->sin6_port
));
196 cxgbit_create_server4(struct cxgbit_device
*cdev
, unsigned int stid
,
197 struct cxgbit_np
*cnp
)
199 struct sockaddr_in
*sin
= (struct sockaddr_in
*)
200 &cnp
->com
.local_addr
;
203 pr_debug("%s: dev = %s; stid = %u; sin_port = %u\n",
204 __func__
, cdev
->lldi
.ports
[0]->name
, stid
, sin
->sin_port
);
207 cxgbit_init_wr_wait(&cnp
->com
.wr_wait
);
209 ret
= cxgb4_create_server(cdev
->lldi
.ports
[0],
210 stid
, sin
->sin_addr
.s_addr
,
212 cdev
->lldi
.rxq_ids
[0]);
214 ret
= cxgbit_wait_for_reply(cdev
,
218 ret
= net_xmit_errno(ret
);
223 pr_err("create server failed err %d stid %d laddr %pI4 lport %d\n",
224 ret
, stid
, &sin
->sin_addr
, ntohs(sin
->sin_port
));
228 struct cxgbit_device
*cxgbit_find_device(struct net_device
*ndev
, u8
*port_id
)
230 struct cxgbit_device
*cdev
;
233 list_for_each_entry(cdev
, &cdev_list_head
, list
) {
234 struct cxgb4_lld_info
*lldi
= &cdev
->lldi
;
236 for (i
= 0; i
< lldi
->nports
; i
++) {
237 if (lldi
->ports
[i
] == ndev
) {
248 static struct net_device
*cxgbit_get_real_dev(struct net_device
*ndev
)
250 if (ndev
->priv_flags
& IFF_BONDING
) {
251 pr_err("Bond devices are not supported. Interface:%s\n",
256 if (is_vlan_dev(ndev
))
257 return vlan_dev_real_dev(ndev
);
262 static struct net_device
*cxgbit_ipv4_netdev(__be32 saddr
)
264 struct net_device
*ndev
;
266 ndev
= __ip_dev_find(&init_net
, saddr
, false);
270 return cxgbit_get_real_dev(ndev
);
273 static struct net_device
*cxgbit_ipv6_netdev(struct in6_addr
*addr6
)
275 struct net_device
*ndev
= NULL
;
278 if (IS_ENABLED(CONFIG_IPV6
)) {
279 for_each_netdev_rcu(&init_net
, ndev
)
280 if (ipv6_chk_addr(&init_net
, addr6
, ndev
, 1)) {
287 return cxgbit_get_real_dev(ndev
);
290 static struct cxgbit_device
*cxgbit_find_np_cdev(struct cxgbit_np
*cnp
)
292 struct sockaddr_storage
*sockaddr
= &cnp
->com
.local_addr
;
293 int ss_family
= sockaddr
->ss_family
;
294 struct net_device
*ndev
= NULL
;
295 struct cxgbit_device
*cdev
= NULL
;
298 if (ss_family
== AF_INET
) {
299 struct sockaddr_in
*sin
;
301 sin
= (struct sockaddr_in
*)sockaddr
;
302 ndev
= cxgbit_ipv4_netdev(sin
->sin_addr
.s_addr
);
303 } else if (ss_family
== AF_INET6
) {
304 struct sockaddr_in6
*sin6
;
306 sin6
= (struct sockaddr_in6
*)sockaddr
;
307 ndev
= cxgbit_ipv6_netdev(&sin6
->sin6_addr
);
312 cdev
= cxgbit_find_device(ndev
, NULL
);
318 static bool cxgbit_inaddr_any(struct cxgbit_np
*cnp
)
320 struct sockaddr_storage
*sockaddr
= &cnp
->com
.local_addr
;
321 int ss_family
= sockaddr
->ss_family
;
324 if (ss_family
== AF_INET
) {
325 struct sockaddr_in
*sin
;
327 sin
= (struct sockaddr_in
*)sockaddr
;
328 if (sin
->sin_addr
.s_addr
== htonl(INADDR_ANY
))
330 } else if (ss_family
== AF_INET6
) {
331 struct sockaddr_in6
*sin6
;
333 sin6
= (struct sockaddr_in6
*)sockaddr
;
334 addr_type
= ipv6_addr_type((const struct in6_addr
*)
336 if (addr_type
== IPV6_ADDR_ANY
)
343 __cxgbit_setup_cdev_np(struct cxgbit_device
*cdev
, struct cxgbit_np
*cnp
)
346 int ss_family
= cnp
->com
.local_addr
.ss_family
;
348 if (!test_bit(CDEV_STATE_UP
, &cdev
->flags
))
351 stid
= cxgb4_alloc_stid(cdev
->lldi
.tids
, ss_family
, cnp
);
355 if (!cxgbit_np_hash_add(cdev
, cnp
, stid
)) {
356 cxgb4_free_stid(cdev
->lldi
.tids
, stid
, ss_family
);
360 if (ss_family
== AF_INET
)
361 ret
= cxgbit_create_server4(cdev
, stid
, cnp
);
363 ret
= cxgbit_create_server6(cdev
, stid
, cnp
);
366 if (ret
!= -ETIMEDOUT
)
367 cxgb4_free_stid(cdev
->lldi
.tids
, stid
,
369 cxgbit_np_hash_del(cdev
, cnp
);
375 static int cxgbit_setup_cdev_np(struct cxgbit_np
*cnp
)
377 struct cxgbit_device
*cdev
;
380 mutex_lock(&cdev_list_lock
);
381 cdev
= cxgbit_find_np_cdev(cnp
);
385 if (cxgbit_np_hash_find(cdev
, cnp
) >= 0)
388 if (__cxgbit_setup_cdev_np(cdev
, cnp
))
391 cnp
->com
.cdev
= cdev
;
394 mutex_unlock(&cdev_list_lock
);
398 static int cxgbit_setup_all_np(struct cxgbit_np
*cnp
)
400 struct cxgbit_device
*cdev
;
404 mutex_lock(&cdev_list_lock
);
405 list_for_each_entry(cdev
, &cdev_list_head
, list
) {
406 if (cxgbit_np_hash_find(cdev
, cnp
) >= 0) {
407 mutex_unlock(&cdev_list_lock
);
412 list_for_each_entry(cdev
, &cdev_list_head
, list
) {
413 ret
= __cxgbit_setup_cdev_np(cdev
, cnp
);
414 if (ret
== -ETIMEDOUT
)
420 mutex_unlock(&cdev_list_lock
);
422 return count
? 0 : -1;
425 int cxgbit_setup_np(struct iscsi_np
*np
, struct sockaddr_storage
*ksockaddr
)
427 struct cxgbit_np
*cnp
;
430 if ((ksockaddr
->ss_family
!= AF_INET
) &&
431 (ksockaddr
->ss_family
!= AF_INET6
))
434 cnp
= kzalloc(sizeof(*cnp
), GFP_KERNEL
);
438 init_waitqueue_head(&cnp
->accept_wait
);
439 init_completion(&cnp
->com
.wr_wait
.completion
);
440 init_completion(&cnp
->accept_comp
);
441 INIT_LIST_HEAD(&cnp
->np_accept_list
);
442 spin_lock_init(&cnp
->np_accept_lock
);
443 kref_init(&cnp
->kref
);
444 memcpy(&np
->np_sockaddr
, ksockaddr
,
445 sizeof(struct sockaddr_storage
));
446 memcpy(&cnp
->com
.local_addr
, &np
->np_sockaddr
,
447 sizeof(cnp
->com
.local_addr
));
450 cnp
->com
.cdev
= NULL
;
452 if (cxgbit_inaddr_any(cnp
))
453 ret
= cxgbit_setup_all_np(cnp
);
455 ret
= cxgbit_setup_cdev_np(cnp
);
462 np
->np_context
= cnp
;
463 cnp
->com
.state
= CSK_STATE_LISTEN
;
468 cxgbit_set_conn_info(struct iscsi_np
*np
, struct iscsi_conn
*conn
,
469 struct cxgbit_sock
*csk
)
471 conn
->login_family
= np
->np_sockaddr
.ss_family
;
472 conn
->login_sockaddr
= csk
->com
.remote_addr
;
473 conn
->local_sockaddr
= csk
->com
.local_addr
;
476 int cxgbit_accept_np(struct iscsi_np
*np
, struct iscsi_conn
*conn
)
478 struct cxgbit_np
*cnp
= np
->np_context
;
479 struct cxgbit_sock
*csk
;
483 ret
= wait_for_completion_interruptible(&cnp
->accept_comp
);
487 spin_lock_bh(&np
->np_thread_lock
);
488 if (np
->np_thread_state
>= ISCSI_NP_THREAD_RESET
) {
489 spin_unlock_bh(&np
->np_thread_lock
);
491 * No point in stalling here when np_thread
492 * is in state RESET/SHUTDOWN/EXIT - bail
496 spin_unlock_bh(&np
->np_thread_lock
);
498 spin_lock_bh(&cnp
->np_accept_lock
);
499 if (list_empty(&cnp
->np_accept_list
)) {
500 spin_unlock_bh(&cnp
->np_accept_lock
);
504 csk
= list_first_entry(&cnp
->np_accept_list
,
508 list_del_init(&csk
->accept_node
);
509 spin_unlock_bh(&cnp
->np_accept_lock
);
513 cxgbit_set_conn_info(np
, conn
, csk
);
518 __cxgbit_free_cdev_np(struct cxgbit_device
*cdev
, struct cxgbit_np
*cnp
)
523 stid
= cxgbit_np_hash_del(cdev
, cnp
);
526 if (!test_bit(CDEV_STATE_UP
, &cdev
->flags
))
529 if (cnp
->np
->np_sockaddr
.ss_family
== AF_INET6
)
533 cxgbit_init_wr_wait(&cnp
->com
.wr_wait
);
534 ret
= cxgb4_remove_server(cdev
->lldi
.ports
[0], stid
,
535 cdev
->lldi
.rxq_ids
[0], ipv6
);
538 ret
= net_xmit_errno(ret
);
545 ret
= cxgbit_wait_for_reply(cdev
, &cnp
->com
.wr_wait
,
547 if (ret
== -ETIMEDOUT
)
550 if (ipv6
&& cnp
->com
.cdev
) {
551 struct sockaddr_in6
*sin6
;
553 sin6
= (struct sockaddr_in6
*)&cnp
->com
.local_addr
;
554 cxgb4_clip_release(cdev
->lldi
.ports
[0],
555 (const u32
*)&sin6
->sin6_addr
.s6_addr
,
559 cxgb4_free_stid(cdev
->lldi
.tids
, stid
,
560 cnp
->com
.local_addr
.ss_family
);
564 static void cxgbit_free_all_np(struct cxgbit_np
*cnp
)
566 struct cxgbit_device
*cdev
;
569 mutex_lock(&cdev_list_lock
);
570 list_for_each_entry(cdev
, &cdev_list_head
, list
) {
571 ret
= __cxgbit_free_cdev_np(cdev
, cnp
);
572 if (ret
== -ETIMEDOUT
)
575 mutex_unlock(&cdev_list_lock
);
578 static void cxgbit_free_cdev_np(struct cxgbit_np
*cnp
)
580 struct cxgbit_device
*cdev
;
583 mutex_lock(&cdev_list_lock
);
584 list_for_each_entry(cdev
, &cdev_list_head
, list
) {
585 if (cdev
== cnp
->com
.cdev
) {
593 __cxgbit_free_cdev_np(cdev
, cnp
);
595 mutex_unlock(&cdev_list_lock
);
598 static void __cxgbit_free_conn(struct cxgbit_sock
*csk
);
600 void cxgbit_free_np(struct iscsi_np
*np
)
602 struct cxgbit_np
*cnp
= np
->np_context
;
603 struct cxgbit_sock
*csk
, *tmp
;
605 cnp
->com
.state
= CSK_STATE_DEAD
;
607 cxgbit_free_cdev_np(cnp
);
609 cxgbit_free_all_np(cnp
);
611 spin_lock_bh(&cnp
->np_accept_lock
);
612 list_for_each_entry_safe(csk
, tmp
, &cnp
->np_accept_list
, accept_node
) {
613 list_del_init(&csk
->accept_node
);
614 __cxgbit_free_conn(csk
);
616 spin_unlock_bh(&cnp
->np_accept_lock
);
618 np
->np_context
= NULL
;
622 static void cxgbit_send_halfclose(struct cxgbit_sock
*csk
)
625 u32 len
= roundup(sizeof(struct cpl_close_con_req
), 16);
627 skb
= alloc_skb(len
, GFP_ATOMIC
);
631 cxgb_mk_close_con_req(skb
, len
, csk
->tid
, csk
->txq_idx
,
634 cxgbit_skcb_flags(skb
) |= SKCBF_TX_FLAG_COMPL
;
635 __skb_queue_tail(&csk
->txq
, skb
);
636 cxgbit_push_tx_frames(csk
);
639 static void cxgbit_arp_failure_discard(void *handle
, struct sk_buff
*skb
)
641 struct cxgbit_sock
*csk
= handle
;
643 pr_debug("%s cxgbit_device %p\n", __func__
, handle
);
648 static void cxgbit_abort_arp_failure(void *handle
, struct sk_buff
*skb
)
650 struct cxgbit_device
*cdev
= handle
;
651 struct cpl_abort_req
*req
= cplhdr(skb
);
653 pr_debug("%s cdev %p\n", __func__
, cdev
);
654 req
->cmd
= CPL_ABORT_NO_RST
;
655 cxgbit_ofld_send(cdev
, skb
);
658 static int cxgbit_send_abort_req(struct cxgbit_sock
*csk
)
661 u32 len
= roundup(sizeof(struct cpl_abort_req
), 16);
663 pr_debug("%s: csk %p tid %u; state %d\n",
664 __func__
, csk
, csk
->tid
, csk
->com
.state
);
666 __skb_queue_purge(&csk
->txq
);
668 if (!test_and_set_bit(CSK_TX_DATA_SENT
, &csk
->com
.flags
))
669 cxgbit_send_tx_flowc_wr(csk
);
671 skb
= __skb_dequeue(&csk
->skbq
);
672 cxgb_mk_abort_req(skb
, len
, csk
->tid
, csk
->txq_idx
,
673 csk
->com
.cdev
, cxgbit_abort_arp_failure
);
675 return cxgbit_l2t_send(csk
->com
.cdev
, skb
, csk
->l2t
);
679 __cxgbit_abort_conn(struct cxgbit_sock
*csk
, struct sk_buff
*skb
)
683 if (csk
->com
.state
!= CSK_STATE_ESTABLISHED
)
686 set_bit(CSK_ABORT_RPL_WAIT
, &csk
->com
.flags
);
687 csk
->com
.state
= CSK_STATE_ABORTING
;
689 cxgbit_send_abort_req(csk
);
694 cxgbit_wake_up(&csk
->com
.wr_wait
, __func__
, CPL_ERR_NONE
);
698 void cxgbit_abort_conn(struct cxgbit_sock
*csk
)
700 struct sk_buff
*skb
= alloc_skb(0, GFP_KERNEL
| __GFP_NOFAIL
);
703 cxgbit_init_wr_wait(&csk
->com
.wr_wait
);
705 spin_lock_bh(&csk
->lock
);
706 if (csk
->lock_owner
) {
707 cxgbit_skcb_rx_backlog_fn(skb
) = __cxgbit_abort_conn
;
708 __skb_queue_tail(&csk
->backlogq
, skb
);
710 __cxgbit_abort_conn(csk
, skb
);
712 spin_unlock_bh(&csk
->lock
);
714 cxgbit_wait_for_reply(csk
->com
.cdev
, &csk
->com
.wr_wait
,
715 csk
->tid
, 600, __func__
);
718 static void __cxgbit_free_conn(struct cxgbit_sock
*csk
)
720 struct iscsi_conn
*conn
= csk
->conn
;
721 bool release
= false;
723 pr_debug("%s: state %d\n",
724 __func__
, csk
->com
.state
);
726 spin_lock_bh(&csk
->lock
);
727 switch (csk
->com
.state
) {
728 case CSK_STATE_ESTABLISHED
:
729 if (conn
&& (conn
->conn_state
== TARG_CONN_STATE_IN_LOGOUT
)) {
730 csk
->com
.state
= CSK_STATE_CLOSING
;
731 cxgbit_send_halfclose(csk
);
733 csk
->com
.state
= CSK_STATE_ABORTING
;
734 cxgbit_send_abort_req(csk
);
737 case CSK_STATE_CLOSING
:
738 csk
->com
.state
= CSK_STATE_MORIBUND
;
739 cxgbit_send_halfclose(csk
);
745 pr_err("%s: csk %p; state %d\n",
746 __func__
, csk
, csk
->com
.state
);
748 spin_unlock_bh(&csk
->lock
);
754 void cxgbit_free_conn(struct iscsi_conn
*conn
)
756 __cxgbit_free_conn(conn
->context
);
759 static void cxgbit_set_emss(struct cxgbit_sock
*csk
, u16 opt
)
761 csk
->emss
= csk
->com
.cdev
->lldi
.mtus
[TCPOPT_MSS_G(opt
)] -
762 ((csk
->com
.remote_addr
.ss_family
== AF_INET
) ?
763 sizeof(struct iphdr
) : sizeof(struct ipv6hdr
)) -
764 sizeof(struct tcphdr
);
765 csk
->mss
= csk
->emss
;
766 if (TCPOPT_TSTAMP_G(opt
))
767 csk
->emss
-= round_up(TCPOLEN_TIMESTAMP
, 4);
771 pr_info("Warning: misaligned mtu idx %u mss %u emss=%u\n",
772 TCPOPT_MSS_G(opt
), csk
->mss
, csk
->emss
);
773 pr_debug("%s mss_idx %u mss %u emss=%u\n", __func__
, TCPOPT_MSS_G(opt
),
774 csk
->mss
, csk
->emss
);
777 static void cxgbit_free_skb(struct cxgbit_sock
*csk
)
781 __skb_queue_purge(&csk
->txq
);
782 __skb_queue_purge(&csk
->rxq
);
783 __skb_queue_purge(&csk
->backlogq
);
784 __skb_queue_purge(&csk
->ppodq
);
785 __skb_queue_purge(&csk
->skbq
);
787 while ((skb
= cxgbit_sock_dequeue_wr(csk
)))
790 __kfree_skb(csk
->lro_hskb
);
793 void _cxgbit_free_csk(struct kref
*kref
)
795 struct cxgbit_sock
*csk
;
796 struct cxgbit_device
*cdev
;
798 csk
= container_of(kref
, struct cxgbit_sock
, kref
);
800 pr_debug("%s csk %p state %d\n", __func__
, csk
, csk
->com
.state
);
802 if (csk
->com
.local_addr
.ss_family
== AF_INET6
) {
803 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)
804 &csk
->com
.local_addr
;
805 cxgb4_clip_release(csk
->com
.cdev
->lldi
.ports
[0],
807 &sin6
->sin6_addr
.s6_addr
, 1);
810 cxgb4_remove_tid(csk
->com
.cdev
->lldi
.tids
, 0, csk
->tid
,
811 csk
->com
.local_addr
.ss_family
);
812 dst_release(csk
->dst
);
813 cxgb4_l2t_release(csk
->l2t
);
815 cdev
= csk
->com
.cdev
;
816 spin_lock_bh(&cdev
->cskq
.lock
);
817 list_del(&csk
->list
);
818 spin_unlock_bh(&cdev
->cskq
.lock
);
820 cxgbit_free_skb(csk
);
821 cxgbit_put_cnp(csk
->cnp
);
822 cxgbit_put_cdev(cdev
);
827 static void cxgbit_set_tcp_window(struct cxgbit_sock
*csk
, struct port_info
*pi
)
829 unsigned int linkspeed
;
832 linkspeed
= pi
->link_cfg
.speed
;
833 scale
= linkspeed
/ SPEED_10000
;
835 #define CXGBIT_10G_RCV_WIN (256 * 1024)
836 csk
->rcv_win
= CXGBIT_10G_RCV_WIN
;
838 csk
->rcv_win
*= scale
;
840 #define CXGBIT_10G_SND_WIN (256 * 1024)
841 csk
->snd_win
= CXGBIT_10G_SND_WIN
;
843 csk
->snd_win
*= scale
;
845 pr_debug("%s snd_win %d rcv_win %d\n",
846 __func__
, csk
->snd_win
, csk
->rcv_win
);
849 #ifdef CONFIG_CHELSIO_T4_DCB
850 static u8
cxgbit_get_iscsi_dcb_state(struct net_device
*ndev
)
852 return ndev
->dcbnl_ops
->getstate(ndev
);
855 static int cxgbit_select_priority(int pri_mask
)
860 return (ffs(pri_mask
) - 1);
863 static u8
cxgbit_get_iscsi_dcb_priority(struct net_device
*ndev
, u16 local_port
)
868 struct dcb_app iscsi_dcb_app
= {
869 .protocol
= local_port
872 ret
= (int)ndev
->dcbnl_ops
->getcap(ndev
, DCB_CAP_ATTR_DCBX
, &caps
);
877 if (caps
& DCB_CAP_DCBX_VER_IEEE
) {
878 iscsi_dcb_app
.selector
= IEEE_8021QAZ_APP_SEL_STREAM
;
879 ret
= dcb_ieee_getapp_mask(ndev
, &iscsi_dcb_app
);
881 iscsi_dcb_app
.selector
= IEEE_8021QAZ_APP_SEL_ANY
;
882 ret
= dcb_ieee_getapp_mask(ndev
, &iscsi_dcb_app
);
884 } else if (caps
& DCB_CAP_DCBX_VER_CEE
) {
885 iscsi_dcb_app
.selector
= DCB_APP_IDTYPE_PORTNUM
;
887 ret
= dcb_getapp(ndev
, &iscsi_dcb_app
);
890 pr_info("iSCSI priority is set to %u\n", cxgbit_select_priority(ret
));
892 return cxgbit_select_priority(ret
);
897 cxgbit_offload_init(struct cxgbit_sock
*csk
, int iptype
, __u8
*peer_ip
,
898 u16 local_port
, struct dst_entry
*dst
,
899 struct cxgbit_device
*cdev
)
903 struct net_device
*ndev
;
904 u16 rxq_idx
, port_id
;
905 #ifdef CONFIG_CHELSIO_T4_DCB
909 n
= dst_neigh_lookup(dst
, peer_ip
);
914 if (!(n
->nud_state
& NUD_VALID
))
915 neigh_event_send(n
, NULL
);
918 if (n
->dev
->flags
& IFF_LOOPBACK
) {
920 ndev
= cxgbit_ipv4_netdev(*(__be32
*)peer_ip
);
921 else if (IS_ENABLED(CONFIG_IPV6
))
922 ndev
= cxgbit_ipv6_netdev((struct in6_addr
*)peer_ip
);
931 csk
->l2t
= cxgb4_l2t_get(cdev
->lldi
.l2t
,
935 csk
->mtu
= ndev
->mtu
;
936 csk
->tx_chan
= cxgb4_port_chan(ndev
);
938 ((struct port_info
*)netdev_priv(ndev
))->smt_idx
;
939 step
= cdev
->lldi
.ntxq
/
941 csk
->txq_idx
= cxgb4_port_idx(ndev
) * step
;
942 step
= cdev
->lldi
.nrxq
/
944 csk
->ctrlq_idx
= cxgb4_port_idx(ndev
);
945 csk
->rss_qid
= cdev
->lldi
.rxq_ids
[
946 cxgb4_port_idx(ndev
) * step
];
947 csk
->port_id
= cxgb4_port_idx(ndev
);
948 cxgbit_set_tcp_window(csk
,
949 (struct port_info
*)netdev_priv(ndev
));
951 ndev
= cxgbit_get_real_dev(n
->dev
);
957 #ifdef CONFIG_CHELSIO_T4_DCB
958 if (cxgbit_get_iscsi_dcb_state(ndev
))
959 priority
= cxgbit_get_iscsi_dcb_priority(ndev
,
962 csk
->dcb_priority
= priority
;
964 csk
->l2t
= cxgb4_l2t_get(cdev
->lldi
.l2t
, n
, ndev
, priority
);
966 csk
->l2t
= cxgb4_l2t_get(cdev
->lldi
.l2t
, n
, ndev
, 0);
970 port_id
= cxgb4_port_idx(ndev
);
971 csk
->mtu
= dst_mtu(dst
);
972 csk
->tx_chan
= cxgb4_port_chan(ndev
);
974 ((struct port_info
*)netdev_priv(ndev
))->smt_idx
;
975 step
= cdev
->lldi
.ntxq
/
977 csk
->txq_idx
= (port_id
* step
) +
978 (cdev
->selectq
[port_id
][0]++ % step
);
979 csk
->ctrlq_idx
= cxgb4_port_idx(ndev
);
980 step
= cdev
->lldi
.nrxq
/
982 rxq_idx
= (port_id
* step
) +
983 (cdev
->selectq
[port_id
][1]++ % step
);
984 csk
->rss_qid
= cdev
->lldi
.rxq_ids
[rxq_idx
];
985 csk
->port_id
= port_id
;
986 cxgbit_set_tcp_window(csk
,
987 (struct port_info
*)netdev_priv(ndev
));
996 int cxgbit_ofld_send(struct cxgbit_device
*cdev
, struct sk_buff
*skb
)
1000 if (!test_bit(CDEV_STATE_UP
, &cdev
->flags
)) {
1002 pr_err("%s - device not up - dropping\n", __func__
);
1006 ret
= cxgb4_ofld_send(cdev
->lldi
.ports
[0], skb
);
1009 return ret
< 0 ? ret
: 0;
1012 static void cxgbit_release_tid(struct cxgbit_device
*cdev
, u32 tid
)
1014 u32 len
= roundup(sizeof(struct cpl_tid_release
), 16);
1015 struct sk_buff
*skb
;
1017 skb
= alloc_skb(len
, GFP_ATOMIC
);
1021 cxgb_mk_tid_release(skb
, len
, tid
, 0);
1022 cxgbit_ofld_send(cdev
, skb
);
1026 cxgbit_l2t_send(struct cxgbit_device
*cdev
, struct sk_buff
*skb
,
1027 struct l2t_entry
*l2e
)
1031 if (!test_bit(CDEV_STATE_UP
, &cdev
->flags
)) {
1033 pr_err("%s - device not up - dropping\n", __func__
);
1037 ret
= cxgb4_l2t_send(cdev
->lldi
.ports
[0], skb
, l2e
);
1040 return ret
< 0 ? ret
: 0;
1043 static void cxgbit_send_rx_credits(struct cxgbit_sock
*csk
, struct sk_buff
*skb
)
1045 if (csk
->com
.state
!= CSK_STATE_ESTABLISHED
) {
1050 cxgbit_ofld_send(csk
->com
.cdev
, skb
);
1054 * CPL connection rx data ack: host ->
1055 * Send RX credits through an RX_DATA_ACK CPL message.
1056 * Returns the number of credits sent.
1058 int cxgbit_rx_data_ack(struct cxgbit_sock
*csk
)
1060 struct sk_buff
*skb
;
1061 u32 len
= roundup(sizeof(struct cpl_rx_data_ack
), 16);
1064 skb
= alloc_skb(len
, GFP_KERNEL
);
1068 credit_dack
= RX_DACK_CHANGE_F
| RX_DACK_MODE_V(1) |
1069 RX_CREDITS_V(csk
->rx_credits
);
1071 cxgb_mk_rx_data_ack(skb
, len
, csk
->tid
, csk
->ctrlq_idx
,
1074 csk
->rx_credits
= 0;
1076 spin_lock_bh(&csk
->lock
);
1077 if (csk
->lock_owner
) {
1078 cxgbit_skcb_rx_backlog_fn(skb
) = cxgbit_send_rx_credits
;
1079 __skb_queue_tail(&csk
->backlogq
, skb
);
1080 spin_unlock_bh(&csk
->lock
);
1084 cxgbit_send_rx_credits(csk
, skb
);
1085 spin_unlock_bh(&csk
->lock
);
1090 #define FLOWC_WR_NPARAMS_MIN 9
1091 #define FLOWC_WR_NPARAMS_MAX 11
1092 static int cxgbit_alloc_csk_skb(struct cxgbit_sock
*csk
)
1094 struct sk_buff
*skb
;
1098 flowclen
= offsetof(struct fw_flowc_wr
,
1099 mnemval
[FLOWC_WR_NPARAMS_MAX
]);
1101 len
= max_t(u32
, sizeof(struct cpl_abort_req
),
1102 sizeof(struct cpl_abort_rpl
));
1104 len
= max(len
, flowclen
);
1105 len
= roundup(len
, 16);
1107 for (i
= 0; i
< 3; i
++) {
1108 skb
= alloc_skb(len
, GFP_ATOMIC
);
1111 __skb_queue_tail(&csk
->skbq
, skb
);
1114 skb
= alloc_skb(LRO_SKB_MIN_HEADROOM
, GFP_ATOMIC
);
1118 memset(skb
->data
, 0, LRO_SKB_MIN_HEADROOM
);
1119 csk
->lro_hskb
= skb
;
1123 __skb_queue_purge(&csk
->skbq
);
1128 cxgbit_pass_accept_rpl(struct cxgbit_sock
*csk
, struct cpl_pass_accept_req
*req
)
1130 struct sk_buff
*skb
;
1131 const struct tcphdr
*tcph
;
1132 struct cpl_t5_pass_accept_rpl
*rpl5
;
1133 struct cxgb4_lld_info
*lldi
= &csk
->com
.cdev
->lldi
;
1134 unsigned int len
= roundup(sizeof(*rpl5
), 16);
1135 unsigned int mtu_idx
;
1141 pr_debug("%s csk %p tid %u\n", __func__
, csk
, csk
->tid
);
1143 skb
= alloc_skb(len
, GFP_ATOMIC
);
1145 cxgbit_put_csk(csk
);
1149 rpl5
= __skb_put_zero(skb
, len
);
1151 INIT_TP_WR(rpl5
, csk
->tid
);
1152 OPCODE_TID(rpl5
) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL
,
1154 cxgb_best_mtu(csk
->com
.cdev
->lldi
.mtus
, csk
->mtu
, &mtu_idx
,
1156 (csk
->com
.remote_addr
.ss_family
== AF_INET
) ? 0 : 1);
1157 wscale
= cxgb_compute_wscale(csk
->rcv_win
);
1159 * Specify the largest window that will fit in opt0. The
1160 * remainder will be specified in the rx_data_ack.
1162 win
= csk
->rcv_win
>> 10;
1163 if (win
> RCV_BUFSIZ_M
)
1165 opt0
= TCAM_BYPASS_F
|
1166 WND_SCALE_V(wscale
) |
1167 MSS_IDX_V(mtu_idx
) |
1168 L2T_IDX_V(csk
->l2t
->idx
) |
1169 TX_CHAN_V(csk
->tx_chan
) |
1170 SMAC_SEL_V(csk
->smac_idx
) |
1171 DSCP_V(csk
->tos
>> 2) |
1172 ULP_MODE_V(ULP_MODE_ISCSI
) |
1175 opt2
= RX_CHANNEL_V(0) |
1176 RSS_QUEUE_VALID_F
| RSS_QUEUE_V(csk
->rss_qid
);
1178 if (!is_t5(lldi
->adapter_type
))
1179 opt2
|= RX_FC_DISABLE_F
;
1181 if (req
->tcpopt
.tstamp
)
1182 opt2
|= TSTAMPS_EN_F
;
1183 if (req
->tcpopt
.sack
)
1186 opt2
|= WND_SCALE_EN_F
;
1188 hlen
= ntohl(req
->hdr_len
);
1190 if (is_t5(lldi
->adapter_type
))
1191 tcph
= (struct tcphdr
*)((u8
*)(req
+ 1) +
1192 ETH_HDR_LEN_G(hlen
) + IP_HDR_LEN_G(hlen
));
1194 tcph
= (struct tcphdr
*)((u8
*)(req
+ 1) +
1195 T6_ETH_HDR_LEN_G(hlen
) + T6_IP_HDR_LEN_G(hlen
));
1197 if (tcph
->ece
&& tcph
->cwr
)
1198 opt2
|= CCTRL_ECN_V(1);
1200 opt2
|= RX_COALESCE_V(3);
1201 opt2
|= CONG_CNTRL_V(CONG_ALG_NEWRENO
);
1204 rpl5
->iss
= cpu_to_be32((prandom_u32() & ~7UL) - 1);
1206 opt2
|= T5_OPT_2_VALID_F
;
1208 rpl5
->opt0
= cpu_to_be64(opt0
);
1209 rpl5
->opt2
= cpu_to_be32(opt2
);
1210 set_wr_txq(skb
, CPL_PRIORITY_SETUP
, csk
->ctrlq_idx
);
1211 t4_set_arp_err_handler(skb
, csk
, cxgbit_arp_failure_discard
);
1212 cxgbit_l2t_send(csk
->com
.cdev
, skb
, csk
->l2t
);
1216 cxgbit_pass_accept_req(struct cxgbit_device
*cdev
, struct sk_buff
*skb
)
1218 struct cxgbit_sock
*csk
= NULL
;
1219 struct cxgbit_np
*cnp
;
1220 struct cpl_pass_accept_req
*req
= cplhdr(skb
);
1221 unsigned int stid
= PASS_OPEN_TID_G(ntohl(req
->tos_stid
));
1222 struct tid_info
*t
= cdev
->lldi
.tids
;
1223 unsigned int tid
= GET_TID(req
);
1224 u16 peer_mss
= ntohs(req
->tcpopt
.mss
);
1225 unsigned short hdrs
;
1227 struct dst_entry
*dst
;
1228 __u8 local_ip
[16], peer_ip
[16];
1229 __be16 local_port
, peer_port
;
1233 pr_debug("%s: cdev = %p; stid = %u; tid = %u\n",
1234 __func__
, cdev
, stid
, tid
);
1236 cnp
= lookup_stid(t
, stid
);
1238 pr_err("%s connect request on invalid stid %d\n",
1243 if (cnp
->com
.state
!= CSK_STATE_LISTEN
) {
1244 pr_err("%s - listening parent not in CSK_STATE_LISTEN\n",
1249 csk
= lookup_tid(t
, tid
);
1251 pr_err("%s csk not null tid %u\n",
1256 cxgb_get_4tuple(req
, cdev
->lldi
.adapter_type
, &iptype
, local_ip
,
1257 peer_ip
, &local_port
, &peer_port
);
1259 /* Find output route */
1261 pr_debug("%s parent sock %p tid %u laddr %pI4 raddr %pI4 "
1262 "lport %d rport %d peer_mss %d\n"
1263 , __func__
, cnp
, tid
,
1264 local_ip
, peer_ip
, ntohs(local_port
),
1265 ntohs(peer_port
), peer_mss
);
1266 dst
= cxgb_find_route(&cdev
->lldi
, cxgbit_get_real_dev
,
1267 *(__be32
*)local_ip
,
1269 local_port
, peer_port
,
1270 PASS_OPEN_TOS_G(ntohl(req
->tos_stid
)));
1272 pr_debug("%s parent sock %p tid %u laddr %pI6 raddr %pI6 "
1273 "lport %d rport %d peer_mss %d\n"
1274 , __func__
, cnp
, tid
,
1275 local_ip
, peer_ip
, ntohs(local_port
),
1276 ntohs(peer_port
), peer_mss
);
1277 dst
= cxgb_find_route6(&cdev
->lldi
, cxgbit_get_real_dev
,
1279 local_port
, peer_port
,
1280 PASS_OPEN_TOS_G(ntohl(req
->tos_stid
)),
1281 ((struct sockaddr_in6
*)
1282 &cnp
->com
.local_addr
)->sin6_scope_id
);
1285 pr_err("%s - failed to find dst entry!\n",
1290 csk
= kzalloc(sizeof(*csk
), GFP_ATOMIC
);
1296 ret
= cxgbit_offload_init(csk
, iptype
, peer_ip
, ntohs(local_port
),
1299 pr_err("%s - failed to allocate l2t entry!\n",
1306 kref_init(&csk
->kref
);
1307 init_completion(&csk
->com
.wr_wait
.completion
);
1309 INIT_LIST_HEAD(&csk
->accept_node
);
1311 hdrs
= (iptype
== 4 ? sizeof(struct iphdr
) : sizeof(struct ipv6hdr
)) +
1312 sizeof(struct tcphdr
) + (req
->tcpopt
.tstamp
? 12 : 0);
1313 if (peer_mss
&& csk
->mtu
> (peer_mss
+ hdrs
))
1314 csk
->mtu
= peer_mss
+ hdrs
;
1316 csk
->com
.state
= CSK_STATE_CONNECTING
;
1317 csk
->com
.cdev
= cdev
;
1319 csk
->tos
= PASS_OPEN_TOS_G(ntohl(req
->tos_stid
));
1322 csk
->wr_cred
= cdev
->lldi
.wr_cred
-
1323 DIV_ROUND_UP(sizeof(struct cpl_abort_req
), 16);
1324 csk
->wr_max_cred
= csk
->wr_cred
;
1325 csk
->wr_una_cred
= 0;
1328 struct sockaddr_in
*sin
= (struct sockaddr_in
*)
1329 &csk
->com
.local_addr
;
1330 sin
->sin_family
= AF_INET
;
1331 sin
->sin_port
= local_port
;
1332 sin
->sin_addr
.s_addr
= *(__be32
*)local_ip
;
1334 sin
= (struct sockaddr_in
*)&csk
->com
.remote_addr
;
1335 sin
->sin_family
= AF_INET
;
1336 sin
->sin_port
= peer_port
;
1337 sin
->sin_addr
.s_addr
= *(__be32
*)peer_ip
;
1339 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)
1340 &csk
->com
.local_addr
;
1342 sin6
->sin6_family
= PF_INET6
;
1343 sin6
->sin6_port
= local_port
;
1344 memcpy(sin6
->sin6_addr
.s6_addr
, local_ip
, 16);
1345 cxgb4_clip_get(cdev
->lldi
.ports
[0],
1346 (const u32
*)&sin6
->sin6_addr
.s6_addr
,
1349 sin6
= (struct sockaddr_in6
*)&csk
->com
.remote_addr
;
1350 sin6
->sin6_family
= PF_INET6
;
1351 sin6
->sin6_port
= peer_port
;
1352 memcpy(sin6
->sin6_addr
.s6_addr
, peer_ip
, 16);
1355 skb_queue_head_init(&csk
->rxq
);
1356 skb_queue_head_init(&csk
->txq
);
1357 skb_queue_head_init(&csk
->ppodq
);
1358 skb_queue_head_init(&csk
->backlogq
);
1359 skb_queue_head_init(&csk
->skbq
);
1360 cxgbit_sock_reset_wr_list(csk
);
1361 spin_lock_init(&csk
->lock
);
1362 init_waitqueue_head(&csk
->waitq
);
1363 csk
->lock_owner
= false;
1365 if (cxgbit_alloc_csk_skb(csk
)) {
1371 cxgbit_get_cnp(cnp
);
1372 cxgbit_get_cdev(cdev
);
1374 spin_lock(&cdev
->cskq
.lock
);
1375 list_add_tail(&csk
->list
, &cdev
->cskq
.list
);
1376 spin_unlock(&cdev
->cskq
.lock
);
1377 cxgb4_insert_tid(t
, csk
, tid
, csk
->com
.local_addr
.ss_family
);
1378 cxgbit_pass_accept_rpl(csk
, req
);
1382 cxgbit_release_tid(cdev
, tid
);
1388 cxgbit_tx_flowc_wr_credits(struct cxgbit_sock
*csk
, u32
*nparamsp
,
1391 u32 nparams
, flowclen16
, flowclen
;
1393 nparams
= FLOWC_WR_NPARAMS_MIN
;
1395 if (csk
->snd_wscale
)
1398 #ifdef CONFIG_CHELSIO_T4_DCB
1401 flowclen
= offsetof(struct fw_flowc_wr
, mnemval
[nparams
]);
1402 flowclen16
= DIV_ROUND_UP(flowclen
, 16);
1403 flowclen
= flowclen16
* 16;
1405 * Return the number of 16-byte credits used by the flowc request.
1406 * Pass back the nparams and actual flowc length if requested.
1409 *nparamsp
= nparams
;
1411 *flowclenp
= flowclen
;
1415 u32
cxgbit_send_tx_flowc_wr(struct cxgbit_sock
*csk
)
1417 struct cxgbit_device
*cdev
= csk
->com
.cdev
;
1418 struct fw_flowc_wr
*flowc
;
1419 u32 nparams
, flowclen16
, flowclen
;
1420 struct sk_buff
*skb
;
1423 #ifdef CONFIG_CHELSIO_T4_DCB
1424 u16 vlan
= ((struct l2t_entry
*)csk
->l2t
)->vlan
;
1427 flowclen16
= cxgbit_tx_flowc_wr_credits(csk
, &nparams
, &flowclen
);
1429 skb
= __skb_dequeue(&csk
->skbq
);
1430 flowc
= __skb_put_zero(skb
, flowclen
);
1432 flowc
->op_to_nparams
= cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR
) |
1433 FW_FLOWC_WR_NPARAMS_V(nparams
));
1434 flowc
->flowid_len16
= cpu_to_be32(FW_WR_LEN16_V(flowclen16
) |
1435 FW_WR_FLOWID_V(csk
->tid
));
1436 flowc
->mnemval
[0].mnemonic
= FW_FLOWC_MNEM_PFNVFN
;
1437 flowc
->mnemval
[0].val
= cpu_to_be32(FW_PFVF_CMD_PFN_V
1438 (csk
->com
.cdev
->lldi
.pf
));
1439 flowc
->mnemval
[1].mnemonic
= FW_FLOWC_MNEM_CH
;
1440 flowc
->mnemval
[1].val
= cpu_to_be32(csk
->tx_chan
);
1441 flowc
->mnemval
[2].mnemonic
= FW_FLOWC_MNEM_PORT
;
1442 flowc
->mnemval
[2].val
= cpu_to_be32(csk
->tx_chan
);
1443 flowc
->mnemval
[3].mnemonic
= FW_FLOWC_MNEM_IQID
;
1444 flowc
->mnemval
[3].val
= cpu_to_be32(csk
->rss_qid
);
1445 flowc
->mnemval
[4].mnemonic
= FW_FLOWC_MNEM_SNDNXT
;
1446 flowc
->mnemval
[4].val
= cpu_to_be32(csk
->snd_nxt
);
1447 flowc
->mnemval
[5].mnemonic
= FW_FLOWC_MNEM_RCVNXT
;
1448 flowc
->mnemval
[5].val
= cpu_to_be32(csk
->rcv_nxt
);
1449 flowc
->mnemval
[6].mnemonic
= FW_FLOWC_MNEM_SNDBUF
;
1450 flowc
->mnemval
[6].val
= cpu_to_be32(csk
->snd_win
);
1451 flowc
->mnemval
[7].mnemonic
= FW_FLOWC_MNEM_MSS
;
1452 flowc
->mnemval
[7].val
= cpu_to_be32(csk
->emss
);
1454 flowc
->mnemval
[8].mnemonic
= FW_FLOWC_MNEM_TXDATAPLEN_MAX
;
1455 if (test_bit(CDEV_ISO_ENABLE
, &cdev
->flags
))
1456 flowc
->mnemval
[8].val
= cpu_to_be32(CXGBIT_MAX_ISO_PAYLOAD
);
1458 flowc
->mnemval
[8].val
= cpu_to_be32(16384);
1462 if (csk
->snd_wscale
) {
1463 flowc
->mnemval
[index
].mnemonic
= FW_FLOWC_MNEM_RCV_SCALE
;
1464 flowc
->mnemval
[index
].val
= cpu_to_be32(csk
->snd_wscale
);
1468 #ifdef CONFIG_CHELSIO_T4_DCB
1469 flowc
->mnemval
[index
].mnemonic
= FW_FLOWC_MNEM_DCBPRIO
;
1470 if (vlan
== VLAN_NONE
) {
1471 pr_warn("csk %u without VLAN Tag on DCB Link\n", csk
->tid
);
1472 flowc
->mnemval
[index
].val
= cpu_to_be32(0);
1474 flowc
->mnemval
[index
].val
= cpu_to_be32(
1475 (vlan
& VLAN_PRIO_MASK
) >> VLAN_PRIO_SHIFT
);
1478 pr_debug("%s: csk %p; tx_chan = %u; rss_qid = %u; snd_seq = %u;"
1479 " rcv_seq = %u; snd_win = %u; emss = %u\n",
1480 __func__
, csk
, csk
->tx_chan
, csk
->rss_qid
, csk
->snd_nxt
,
1481 csk
->rcv_nxt
, csk
->snd_win
, csk
->emss
);
1482 set_wr_txq(skb
, CPL_PRIORITY_DATA
, csk
->txq_idx
);
1483 cxgbit_ofld_send(csk
->com
.cdev
, skb
);
1488 cxgbit_send_tcb_skb(struct cxgbit_sock
*csk
, struct sk_buff
*skb
)
1490 spin_lock_bh(&csk
->lock
);
1491 if (unlikely(csk
->com
.state
!= CSK_STATE_ESTABLISHED
)) {
1492 spin_unlock_bh(&csk
->lock
);
1493 pr_err("%s: csk 0x%p, tid %u, state %u\n",
1494 __func__
, csk
, csk
->tid
, csk
->com
.state
);
1499 cxgbit_get_csk(csk
);
1500 cxgbit_init_wr_wait(&csk
->com
.wr_wait
);
1501 cxgbit_ofld_send(csk
->com
.cdev
, skb
);
1502 spin_unlock_bh(&csk
->lock
);
1507 int cxgbit_setup_conn_digest(struct cxgbit_sock
*csk
)
1509 struct sk_buff
*skb
;
1510 struct cpl_set_tcb_field
*req
;
1511 u8 hcrc
= csk
->submode
& CXGBIT_SUBMODE_HCRC
;
1512 u8 dcrc
= csk
->submode
& CXGBIT_SUBMODE_DCRC
;
1513 unsigned int len
= roundup(sizeof(*req
), 16);
1516 skb
= alloc_skb(len
, GFP_KERNEL
);
1520 /* set up ulp submode */
1521 req
= __skb_put_zero(skb
, len
);
1523 INIT_TP_WR(req
, csk
->tid
);
1524 OPCODE_TID(req
) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD
, csk
->tid
));
1525 req
->reply_ctrl
= htons(NO_REPLY_V(0) | QUEUENO_V(csk
->rss_qid
));
1526 req
->word_cookie
= htons(0);
1527 req
->mask
= cpu_to_be64(0x3 << 4);
1528 req
->val
= cpu_to_be64(((hcrc
? ULP_CRC_HEADER
: 0) |
1529 (dcrc
? ULP_CRC_DATA
: 0)) << 4);
1530 set_wr_txq(skb
, CPL_PRIORITY_CONTROL
, csk
->ctrlq_idx
);
1532 if (cxgbit_send_tcb_skb(csk
, skb
))
1535 ret
= cxgbit_wait_for_reply(csk
->com
.cdev
,
1537 csk
->tid
, 5, __func__
);
1544 int cxgbit_setup_conn_pgidx(struct cxgbit_sock
*csk
, u32 pg_idx
)
1546 struct sk_buff
*skb
;
1547 struct cpl_set_tcb_field
*req
;
1548 unsigned int len
= roundup(sizeof(*req
), 16);
1551 skb
= alloc_skb(len
, GFP_KERNEL
);
1555 req
= __skb_put_zero(skb
, len
);
1557 INIT_TP_WR(req
, csk
->tid
);
1558 OPCODE_TID(req
) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD
, csk
->tid
));
1559 req
->reply_ctrl
= htons(NO_REPLY_V(0) | QUEUENO_V(csk
->rss_qid
));
1560 req
->word_cookie
= htons(0);
1561 req
->mask
= cpu_to_be64(0x3 << 8);
1562 req
->val
= cpu_to_be64(pg_idx
<< 8);
1563 set_wr_txq(skb
, CPL_PRIORITY_CONTROL
, csk
->ctrlq_idx
);
1565 if (cxgbit_send_tcb_skb(csk
, skb
))
1568 ret
= cxgbit_wait_for_reply(csk
->com
.cdev
,
1570 csk
->tid
, 5, __func__
);
1578 cxgbit_pass_open_rpl(struct cxgbit_device
*cdev
, struct sk_buff
*skb
)
1580 struct cpl_pass_open_rpl
*rpl
= cplhdr(skb
);
1581 struct tid_info
*t
= cdev
->lldi
.tids
;
1582 unsigned int stid
= GET_TID(rpl
);
1583 struct cxgbit_np
*cnp
= lookup_stid(t
, stid
);
1585 pr_debug("%s: cnp = %p; stid = %u; status = %d\n",
1586 __func__
, cnp
, stid
, rpl
->status
);
1589 pr_info("%s stid %d lookup failure\n", __func__
, stid
);
1593 cxgbit_wake_up(&cnp
->com
.wr_wait
, __func__
, rpl
->status
);
1594 cxgbit_put_cnp(cnp
);
1600 cxgbit_close_listsrv_rpl(struct cxgbit_device
*cdev
, struct sk_buff
*skb
)
1602 struct cpl_close_listsvr_rpl
*rpl
= cplhdr(skb
);
1603 struct tid_info
*t
= cdev
->lldi
.tids
;
1604 unsigned int stid
= GET_TID(rpl
);
1605 struct cxgbit_np
*cnp
= lookup_stid(t
, stid
);
1607 pr_debug("%s: cnp = %p; stid = %u; status = %d\n",
1608 __func__
, cnp
, stid
, rpl
->status
);
1611 pr_info("%s stid %d lookup failure\n", __func__
, stid
);
1615 cxgbit_wake_up(&cnp
->com
.wr_wait
, __func__
, rpl
->status
);
1616 cxgbit_put_cnp(cnp
);
1622 cxgbit_pass_establish(struct cxgbit_device
*cdev
, struct sk_buff
*skb
)
1624 struct cpl_pass_establish
*req
= cplhdr(skb
);
1625 struct tid_info
*t
= cdev
->lldi
.tids
;
1626 unsigned int tid
= GET_TID(req
);
1627 struct cxgbit_sock
*csk
;
1628 struct cxgbit_np
*cnp
;
1629 u16 tcp_opt
= be16_to_cpu(req
->tcp_opt
);
1630 u32 snd_isn
= be32_to_cpu(req
->snd_isn
);
1631 u32 rcv_isn
= be32_to_cpu(req
->rcv_isn
);
1633 csk
= lookup_tid(t
, tid
);
1634 if (unlikely(!csk
)) {
1635 pr_err("can't find connection for tid %u.\n", tid
);
1640 pr_debug("%s: csk %p; tid %u; cnp %p\n",
1641 __func__
, csk
, tid
, cnp
);
1643 csk
->write_seq
= snd_isn
;
1644 csk
->snd_una
= snd_isn
;
1645 csk
->snd_nxt
= snd_isn
;
1647 csk
->rcv_nxt
= rcv_isn
;
1649 if (csk
->rcv_win
> (RCV_BUFSIZ_M
<< 10))
1650 csk
->rx_credits
= (csk
->rcv_win
- (RCV_BUFSIZ_M
<< 10));
1652 csk
->snd_wscale
= TCPOPT_SND_WSCALE_G(tcp_opt
);
1653 cxgbit_set_emss(csk
, tcp_opt
);
1654 dst_confirm(csk
->dst
);
1655 csk
->com
.state
= CSK_STATE_ESTABLISHED
;
1656 spin_lock_bh(&cnp
->np_accept_lock
);
1657 list_add_tail(&csk
->accept_node
, &cnp
->np_accept_list
);
1658 spin_unlock_bh(&cnp
->np_accept_lock
);
1659 complete(&cnp
->accept_comp
);
1664 static void cxgbit_queue_rx_skb(struct cxgbit_sock
*csk
, struct sk_buff
*skb
)
1666 cxgbit_skcb_flags(skb
) = 0;
1667 spin_lock_bh(&csk
->rxq
.lock
);
1668 __skb_queue_tail(&csk
->rxq
, skb
);
1669 spin_unlock_bh(&csk
->rxq
.lock
);
1670 wake_up(&csk
->waitq
);
1673 static void cxgbit_peer_close(struct cxgbit_sock
*csk
, struct sk_buff
*skb
)
1675 pr_debug("%s: csk %p; tid %u; state %d\n",
1676 __func__
, csk
, csk
->tid
, csk
->com
.state
);
1678 switch (csk
->com
.state
) {
1679 case CSK_STATE_ESTABLISHED
:
1680 csk
->com
.state
= CSK_STATE_CLOSING
;
1681 cxgbit_queue_rx_skb(csk
, skb
);
1683 case CSK_STATE_CLOSING
:
1684 /* simultaneous close */
1685 csk
->com
.state
= CSK_STATE_MORIBUND
;
1687 case CSK_STATE_MORIBUND
:
1688 csk
->com
.state
= CSK_STATE_DEAD
;
1689 cxgbit_put_csk(csk
);
1691 case CSK_STATE_ABORTING
:
1694 pr_info("%s: cpl_peer_close in bad state %d\n",
1695 __func__
, csk
->com
.state
);
1701 static void cxgbit_close_con_rpl(struct cxgbit_sock
*csk
, struct sk_buff
*skb
)
1703 pr_debug("%s: csk %p; tid %u; state %d\n",
1704 __func__
, csk
, csk
->tid
, csk
->com
.state
);
1706 switch (csk
->com
.state
) {
1707 case CSK_STATE_CLOSING
:
1708 csk
->com
.state
= CSK_STATE_MORIBUND
;
1710 case CSK_STATE_MORIBUND
:
1711 csk
->com
.state
= CSK_STATE_DEAD
;
1712 cxgbit_put_csk(csk
);
1714 case CSK_STATE_ABORTING
:
1715 case CSK_STATE_DEAD
:
1718 pr_info("%s: cpl_close_con_rpl in bad state %d\n",
1719 __func__
, csk
->com
.state
);
1725 static void cxgbit_abort_req_rss(struct cxgbit_sock
*csk
, struct sk_buff
*skb
)
1727 struct cpl_abort_req_rss
*hdr
= cplhdr(skb
);
1728 unsigned int tid
= GET_TID(hdr
);
1729 struct sk_buff
*rpl_skb
;
1730 bool release
= false;
1731 bool wakeup_thread
= false;
1732 u32 len
= roundup(sizeof(struct cpl_abort_rpl
), 16);
1734 pr_debug("%s: csk %p; tid %u; state %d\n",
1735 __func__
, csk
, tid
, csk
->com
.state
);
1737 if (cxgb_is_neg_adv(hdr
->status
)) {
1738 pr_err("%s: got neg advise %d on tid %u\n",
1739 __func__
, hdr
->status
, tid
);
1743 switch (csk
->com
.state
) {
1744 case CSK_STATE_CONNECTING
:
1745 case CSK_STATE_MORIBUND
:
1746 csk
->com
.state
= CSK_STATE_DEAD
;
1749 case CSK_STATE_ESTABLISHED
:
1750 csk
->com
.state
= CSK_STATE_DEAD
;
1751 wakeup_thread
= true;
1753 case CSK_STATE_CLOSING
:
1754 csk
->com
.state
= CSK_STATE_DEAD
;
1758 case CSK_STATE_ABORTING
:
1761 pr_info("%s: cpl_abort_req_rss in bad state %d\n",
1762 __func__
, csk
->com
.state
);
1763 csk
->com
.state
= CSK_STATE_DEAD
;
1766 __skb_queue_purge(&csk
->txq
);
1768 if (!test_and_set_bit(CSK_TX_DATA_SENT
, &csk
->com
.flags
))
1769 cxgbit_send_tx_flowc_wr(csk
);
1771 rpl_skb
= __skb_dequeue(&csk
->skbq
);
1773 cxgb_mk_abort_rpl(rpl_skb
, len
, csk
->tid
, csk
->txq_idx
);
1774 cxgbit_ofld_send(csk
->com
.cdev
, rpl_skb
);
1776 if (wakeup_thread
) {
1777 cxgbit_queue_rx_skb(csk
, skb
);
1782 cxgbit_put_csk(csk
);
1787 static void cxgbit_abort_rpl_rss(struct cxgbit_sock
*csk
, struct sk_buff
*skb
)
1789 struct cpl_abort_rpl_rss
*rpl
= cplhdr(skb
);
1791 pr_debug("%s: csk %p; tid %u; state %d\n",
1792 __func__
, csk
, csk
->tid
, csk
->com
.state
);
1794 switch (csk
->com
.state
) {
1795 case CSK_STATE_ABORTING
:
1796 csk
->com
.state
= CSK_STATE_DEAD
;
1797 if (test_bit(CSK_ABORT_RPL_WAIT
, &csk
->com
.flags
))
1798 cxgbit_wake_up(&csk
->com
.wr_wait
, __func__
,
1800 cxgbit_put_csk(csk
);
1803 pr_info("%s: cpl_abort_rpl_rss in state %d\n",
1804 __func__
, csk
->com
.state
);
1810 static bool cxgbit_credit_err(const struct cxgbit_sock
*csk
)
1812 const struct sk_buff
*skb
= csk
->wr_pending_head
;
1815 if (unlikely(csk
->wr_cred
> csk
->wr_max_cred
)) {
1816 pr_err("csk 0x%p, tid %u, credit %u > %u\n",
1817 csk
, csk
->tid
, csk
->wr_cred
, csk
->wr_max_cred
);
1822 credit
+= (__force u32
)skb
->csum
;
1823 skb
= cxgbit_skcb_tx_wr_next(skb
);
1826 if (unlikely((csk
->wr_cred
+ credit
) != csk
->wr_max_cred
)) {
1827 pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
1828 csk
, csk
->tid
, csk
->wr_cred
,
1829 credit
, csk
->wr_max_cred
);
1837 static void cxgbit_fw4_ack(struct cxgbit_sock
*csk
, struct sk_buff
*skb
)
1839 struct cpl_fw4_ack
*rpl
= (struct cpl_fw4_ack
*)cplhdr(skb
);
1840 u32 credits
= rpl
->credits
;
1841 u32 snd_una
= ntohl(rpl
->snd_una
);
1843 csk
->wr_cred
+= credits
;
1844 if (csk
->wr_una_cred
> (csk
->wr_max_cred
- csk
->wr_cred
))
1845 csk
->wr_una_cred
= csk
->wr_max_cred
- csk
->wr_cred
;
1848 struct sk_buff
*p
= cxgbit_sock_peek_wr(csk
);
1852 pr_err("csk 0x%p,%u, cr %u,%u+%u, empty.\n",
1853 csk
, csk
->tid
, credits
,
1854 csk
->wr_cred
, csk
->wr_una_cred
);
1858 csum
= (__force u32
)p
->csum
;
1859 if (unlikely(credits
< csum
)) {
1860 pr_warn("csk 0x%p,%u, cr %u,%u+%u, < %u.\n",
1862 credits
, csk
->wr_cred
, csk
->wr_una_cred
,
1864 p
->csum
= (__force __wsum
)(csum
- credits
);
1868 cxgbit_sock_dequeue_wr(csk
);
1873 if (unlikely(cxgbit_credit_err(csk
))) {
1874 cxgbit_queue_rx_skb(csk
, skb
);
1878 if (rpl
->seq_vld
& CPL_FW4_ACK_FLAGS_SEQVAL
) {
1879 if (unlikely(before(snd_una
, csk
->snd_una
))) {
1880 pr_warn("csk 0x%p,%u, snd_una %u/%u.",
1881 csk
, csk
->tid
, snd_una
,
1886 if (csk
->snd_una
!= snd_una
) {
1887 csk
->snd_una
= snd_una
;
1888 dst_confirm(csk
->dst
);
1892 if (skb_queue_len(&csk
->txq
))
1893 cxgbit_push_tx_frames(csk
);
1899 static void cxgbit_set_tcb_rpl(struct cxgbit_device
*cdev
, struct sk_buff
*skb
)
1901 struct cxgbit_sock
*csk
;
1902 struct cpl_set_tcb_rpl
*rpl
= (struct cpl_set_tcb_rpl
*)skb
->data
;
1903 unsigned int tid
= GET_TID(rpl
);
1904 struct cxgb4_lld_info
*lldi
= &cdev
->lldi
;
1905 struct tid_info
*t
= lldi
->tids
;
1907 csk
= lookup_tid(t
, tid
);
1908 if (unlikely(!csk
)) {
1909 pr_err("can't find connection for tid %u.\n", tid
);
1912 cxgbit_wake_up(&csk
->com
.wr_wait
, __func__
, rpl
->status
);
1915 cxgbit_put_csk(csk
);
1920 static void cxgbit_rx_data(struct cxgbit_device
*cdev
, struct sk_buff
*skb
)
1922 struct cxgbit_sock
*csk
;
1923 struct cpl_rx_data
*cpl
= cplhdr(skb
);
1924 unsigned int tid
= GET_TID(cpl
);
1925 struct cxgb4_lld_info
*lldi
= &cdev
->lldi
;
1926 struct tid_info
*t
= lldi
->tids
;
1928 csk
= lookup_tid(t
, tid
);
1929 if (unlikely(!csk
)) {
1930 pr_err("can't find conn. for tid %u.\n", tid
);
1934 cxgbit_queue_rx_skb(csk
, skb
);
1941 __cxgbit_process_rx_cpl(struct cxgbit_sock
*csk
, struct sk_buff
*skb
)
1943 spin_lock(&csk
->lock
);
1944 if (csk
->lock_owner
) {
1945 __skb_queue_tail(&csk
->backlogq
, skb
);
1946 spin_unlock(&csk
->lock
);
1950 cxgbit_skcb_rx_backlog_fn(skb
)(csk
, skb
);
1951 spin_unlock(&csk
->lock
);
1954 static void cxgbit_process_rx_cpl(struct cxgbit_sock
*csk
, struct sk_buff
*skb
)
1956 cxgbit_get_csk(csk
);
1957 __cxgbit_process_rx_cpl(csk
, skb
);
1958 cxgbit_put_csk(csk
);
1961 static void cxgbit_rx_cpl(struct cxgbit_device
*cdev
, struct sk_buff
*skb
)
1963 struct cxgbit_sock
*csk
;
1964 struct cpl_tx_data
*cpl
= cplhdr(skb
);
1965 struct cxgb4_lld_info
*lldi
= &cdev
->lldi
;
1966 struct tid_info
*t
= lldi
->tids
;
1967 unsigned int tid
= GET_TID(cpl
);
1968 u8 opcode
= cxgbit_skcb_rx_opcode(skb
);
1973 cxgbit_skcb_rx_backlog_fn(skb
) = cxgbit_fw4_ack
;
1976 case CPL_PEER_CLOSE
:
1977 cxgbit_skcb_rx_backlog_fn(skb
) = cxgbit_peer_close
;
1979 case CPL_CLOSE_CON_RPL
:
1980 cxgbit_skcb_rx_backlog_fn(skb
) = cxgbit_close_con_rpl
;
1982 case CPL_ABORT_REQ_RSS
:
1983 cxgbit_skcb_rx_backlog_fn(skb
) = cxgbit_abort_req_rss
;
1985 case CPL_ABORT_RPL_RSS
:
1986 cxgbit_skcb_rx_backlog_fn(skb
) = cxgbit_abort_rpl_rss
;
1992 csk
= lookup_tid(t
, tid
);
1993 if (unlikely(!csk
)) {
1994 pr_err("can't find conn. for tid %u.\n", tid
);
1999 cxgbit_process_rx_cpl(csk
, skb
);
2001 __cxgbit_process_rx_cpl(csk
, skb
);
2008 cxgbit_cplhandler_func cxgbit_cplhandlers
[NUM_CPL_CMDS
] = {
2009 [CPL_PASS_OPEN_RPL
] = cxgbit_pass_open_rpl
,
2010 [CPL_CLOSE_LISTSRV_RPL
] = cxgbit_close_listsrv_rpl
,
2011 [CPL_PASS_ACCEPT_REQ
] = cxgbit_pass_accept_req
,
2012 [CPL_PASS_ESTABLISH
] = cxgbit_pass_establish
,
2013 [CPL_SET_TCB_RPL
] = cxgbit_set_tcb_rpl
,
2014 [CPL_RX_DATA
] = cxgbit_rx_data
,
2015 [CPL_FW4_ACK
] = cxgbit_rx_cpl
,
2016 [CPL_PEER_CLOSE
] = cxgbit_rx_cpl
,
2017 [CPL_CLOSE_CON_RPL
] = cxgbit_rx_cpl
,
2018 [CPL_ABORT_REQ_RSS
] = cxgbit_rx_cpl
,
2019 [CPL_ABORT_RPL_RSS
] = cxgbit_rx_cpl
,