target/cxgbit: Use T6 specific macros to get ETH/IP hdr len
[linux/fpc-iii.git] / drivers / target / iscsi / cxgbit / cxgbit_cm.c
blobb26c85a65b80899b9e216c1c3d4d540a5e5cb1e0
1 /*
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.
7 */
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>
16 #include <linux/ip.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>
23 #include <net/tcp.h>
24 #include <net/ip6_route.h>
25 #include <net/addrconf.h>
27 #include <libcxgb_cm.h>
28 #include "cxgbit.h"
29 #include "clip_tbl.h"
31 static void cxgbit_init_wr_wait(struct cxgbit_wr_wait *wr_waitp)
33 wr_waitp->ret = 0;
34 reinit_completion(&wr_waitp->completion);
37 static void
38 cxgbit_wake_up(struct cxgbit_wr_wait *wr_waitp, const char *func, u8 ret)
40 if (ret == CPL_ERR_NONE)
41 wr_waitp->ret = 0;
42 else
43 wr_waitp->ret = -EIO;
45 if (wr_waitp->ret)
46 pr_err("%s: err:%u", func, ret);
48 complete(&wr_waitp->completion);
51 static int
52 cxgbit_wait_for_reply(struct cxgbit_device *cdev,
53 struct cxgbit_wr_wait *wr_waitp, u32 tid, u32 timeout,
54 const char *func)
56 int ret;
58 if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
59 wr_waitp->ret = -EIO;
60 goto out;
63 ret = wait_for_completion_timeout(&wr_waitp->completion, timeout * HZ);
64 if (!ret) {
65 pr_info("%s - Device %s not responding tid %u\n",
66 func, pci_name(cdev->lldi.pdev), tid);
67 wr_waitp->ret = -ETIMEDOUT;
69 out:
70 if (wr_waitp->ret)
71 pr_info("%s: FW reply %d tid %u\n",
72 pci_name(cdev->lldi.pdev), wr_waitp->ret, tid);
73 return wr_waitp->ret;
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,
83 unsigned int stid)
85 struct np_info *p = kzalloc(sizeof(*p), GFP_KERNEL);
87 if (p) {
88 int bucket = cxgbit_np_hashfn(cnp);
90 p->cnp = cnp;
91 p->stid = stid;
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);
98 return p;
101 static int
102 cxgbit_np_hash_find(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
104 int stid = -1, bucket = cxgbit_np_hashfn(cnp);
105 struct np_info *p;
107 spin_lock(&cdev->np_lock);
108 for (p = cdev->np_hash_tab[bucket]; p; p = p->next) {
109 if (p->cnp == cnp) {
110 stid = p->stid;
111 break;
114 spin_unlock(&cdev->np_lock);
116 return stid;
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) {
126 if (p->cnp == cnp) {
127 stid = p->stid;
128 *prev = p->next;
129 kfree(p);
130 break;
133 spin_unlock(&cdev->np_lock);
135 return stid;
138 void _cxgbit_free_cnp(struct kref *kref)
140 struct cxgbit_np *cnp;
142 cnp = container_of(kref, struct cxgbit_np, kref);
143 kfree(cnp);
146 static int
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;
152 int addr_type;
153 int ret;
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 *)
159 &sin6->sin6_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);
163 if (ret) {
164 pr_err("Unable to find clip table entry. laddr %pI6. Error:%d.\n",
165 sin6->sin6_addr.s6_addr, ret);
166 return -ENOMEM;
170 cxgbit_get_cnp(cnp);
171 cxgbit_init_wr_wait(&cnp->com.wr_wait);
173 ret = cxgb4_create_server6(cdev->lldi.ports[0],
174 stid, &sin6->sin6_addr,
175 sin6->sin6_port,
176 cdev->lldi.rxq_ids[0]);
177 if (!ret)
178 ret = cxgbit_wait_for_reply(cdev, &cnp->com.wr_wait,
179 0, 10, __func__);
180 else if (ret > 0)
181 ret = net_xmit_errno(ret);
182 else
183 cxgbit_put_cnp(cnp);
185 if (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));
195 return ret;
198 static int
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;
204 int ret;
206 pr_debug("%s: dev = %s; stid = %u; sin_port = %u\n",
207 __func__, cdev->lldi.ports[0]->name, stid, sin->sin_port);
209 cxgbit_get_cnp(cnp);
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,
214 sin->sin_port, 0,
215 cdev->lldi.rxq_ids[0]);
216 if (!ret)
217 ret = cxgbit_wait_for_reply(cdev,
218 &cnp->com.wr_wait,
219 0, 10, __func__);
220 else if (ret > 0)
221 ret = net_xmit_errno(ret);
222 else
223 cxgbit_put_cnp(cnp);
225 if (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));
228 return ret;
231 struct cxgbit_device *cxgbit_find_device(struct net_device *ndev, u8 *port_id)
233 struct cxgbit_device *cdev;
234 u8 i;
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) {
241 if (port_id)
242 *port_id = i;
243 return cdev;
248 return NULL;
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",
255 ndev->name);
256 return NULL;
259 if (is_vlan_dev(ndev))
260 return vlan_dev_real_dev(ndev);
262 return 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);
270 if (!ndev)
271 return NULL;
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;
279 bool found = false;
281 if (IS_ENABLED(CONFIG_IPV6)) {
282 for_each_netdev_rcu(&init_net, ndev)
283 if (ipv6_chk_addr(&init_net, addr6, ndev, 1)) {
284 found = true;
285 break;
288 if (!found)
289 return NULL;
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;
300 rcu_read_lock();
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);
312 if (!ndev)
313 goto out;
315 cdev = cxgbit_find_device(ndev, NULL);
316 out:
317 rcu_read_unlock();
318 return cdev;
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;
325 int addr_type;
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))
332 return true;
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 *)
338 &sin6->sin6_addr);
339 if (addr_type == IPV6_ADDR_ANY)
340 return true;
342 return false;
345 static int
346 __cxgbit_setup_cdev_np(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
348 int stid, ret;
349 int ss_family = cnp->com.local_addr.ss_family;
351 if (!test_bit(CDEV_STATE_UP, &cdev->flags))
352 return -EINVAL;
354 stid = cxgb4_alloc_stid(cdev->lldi.tids, ss_family, cnp);
355 if (stid < 0)
356 return -EINVAL;
358 if (!cxgbit_np_hash_add(cdev, cnp, stid)) {
359 cxgb4_free_stid(cdev->lldi.tids, stid, ss_family);
360 return -EINVAL;
363 if (ss_family == AF_INET)
364 ret = cxgbit_create_server4(cdev, stid, cnp);
365 else
366 ret = cxgbit_create_server6(cdev, stid, cnp);
368 if (ret) {
369 if (ret != -ETIMEDOUT)
370 cxgb4_free_stid(cdev->lldi.tids, stid,
371 ss_family);
372 cxgbit_np_hash_del(cdev, cnp);
373 return ret;
375 return ret;
378 static int cxgbit_setup_cdev_np(struct cxgbit_np *cnp)
380 struct cxgbit_device *cdev;
381 int ret = -1;
383 mutex_lock(&cdev_list_lock);
384 cdev = cxgbit_find_np_cdev(cnp);
385 if (!cdev)
386 goto out;
388 if (cxgbit_np_hash_find(cdev, cnp) >= 0)
389 goto out;
391 if (__cxgbit_setup_cdev_np(cdev, cnp))
392 goto out;
394 cnp->com.cdev = cdev;
395 ret = 0;
396 out:
397 mutex_unlock(&cdev_list_lock);
398 return ret;
401 static int cxgbit_setup_all_np(struct cxgbit_np *cnp)
403 struct cxgbit_device *cdev;
404 int ret;
405 u32 count = 0;
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);
411 return -1;
415 list_for_each_entry(cdev, &cdev_list_head, list) {
416 ret = __cxgbit_setup_cdev_np(cdev, cnp);
417 if (ret == -ETIMEDOUT)
418 break;
419 if (ret != 0)
420 continue;
421 count++;
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;
431 int ret;
433 if ((ksockaddr->ss_family != AF_INET) &&
434 (ksockaddr->ss_family != AF_INET6))
435 return -EINVAL;
437 cnp = kzalloc(sizeof(*cnp), GFP_KERNEL);
438 if (!cnp)
439 return -ENOMEM;
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));
452 cnp->np = np;
453 cnp->com.cdev = NULL;
455 if (cxgbit_inaddr_any(cnp))
456 ret = cxgbit_setup_all_np(cnp);
457 else
458 ret = cxgbit_setup_cdev_np(cnp);
460 if (ret) {
461 cxgbit_put_cnp(cnp);
462 return -EINVAL;
465 np->np_context = cnp;
466 cnp->com.state = CSK_STATE_LISTEN;
467 return 0;
470 static void
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;
483 int ret = 0;
485 accept_wait:
486 ret = wait_for_completion_interruptible(&cnp->accept_comp);
487 if (ret)
488 return -ENODEV;
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
497 return -ENODEV;
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);
504 goto accept_wait;
507 csk = list_first_entry(&cnp->np_accept_list,
508 struct cxgbit_sock,
509 accept_node);
511 list_del_init(&csk->accept_node);
512 spin_unlock_bh(&cnp->np_accept_lock);
513 conn->context = csk;
514 csk->conn = conn;
516 cxgbit_set_conn_info(np, conn, csk);
517 return 0;
520 static int
521 __cxgbit_free_cdev_np(struct cxgbit_device *cdev, struct cxgbit_np *cnp)
523 int stid, ret;
524 bool ipv6 = false;
526 stid = cxgbit_np_hash_del(cdev, cnp);
527 if (stid < 0)
528 return -EINVAL;
529 if (!test_bit(CDEV_STATE_UP, &cdev->flags))
530 return -EINVAL;
532 if (cnp->np->np_sockaddr.ss_family == AF_INET6)
533 ipv6 = true;
535 cxgbit_get_cnp(cnp);
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);
540 if (ret > 0)
541 ret = net_xmit_errno(ret);
543 if (ret) {
544 cxgbit_put_cnp(cnp);
545 return ret;
548 ret = cxgbit_wait_for_reply(cdev, &cnp->com.wr_wait,
549 0, 10, __func__);
550 if (ret == -ETIMEDOUT)
551 return ret;
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);
564 return 0;
567 static void cxgbit_free_all_np(struct cxgbit_np *cnp)
569 struct cxgbit_device *cdev;
570 int ret;
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)
576 break;
578 mutex_unlock(&cdev_list_lock);
581 static void cxgbit_free_cdev_np(struct cxgbit_np *cnp)
583 struct cxgbit_device *cdev;
584 bool found = false;
586 mutex_lock(&cdev_list_lock);
587 list_for_each_entry(cdev, &cdev_list_head, list) {
588 if (cdev == cnp->com.cdev) {
589 found = true;
590 break;
593 if (!found)
594 goto out;
596 __cxgbit_free_cdev_np(cdev, cnp);
597 out:
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;
606 if (cnp->com.cdev)
607 cxgbit_free_cdev_np(cnp);
608 else
609 cxgbit_free_all_np(cnp);
611 np->np_context = NULL;
612 cxgbit_put_cnp(cnp);
615 static void cxgbit_send_halfclose(struct cxgbit_sock *csk)
617 struct sk_buff *skb;
618 u32 len = roundup(sizeof(struct cpl_close_con_req), 16);
620 skb = alloc_skb(len, GFP_ATOMIC);
621 if (!skb)
622 return;
624 cxgb_mk_close_con_req(skb, len, csk->tid, csk->txq_idx,
625 NULL, NULL);
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);
635 kfree_skb(skb);
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)
650 struct sk_buff *skb;
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);
668 void cxgbit_free_conn(struct iscsi_conn *conn)
670 struct cxgbit_sock *csk = conn->context;
671 bool release = false;
673 pr_debug("%s: state %d\n",
674 __func__, csk->com.state);
676 spin_lock_bh(&csk->lock);
677 switch (csk->com.state) {
678 case CSK_STATE_ESTABLISHED:
679 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT) {
680 csk->com.state = CSK_STATE_CLOSING;
681 cxgbit_send_halfclose(csk);
682 } else {
683 csk->com.state = CSK_STATE_ABORTING;
684 cxgbit_send_abort_req(csk);
686 break;
687 case CSK_STATE_CLOSING:
688 csk->com.state = CSK_STATE_MORIBUND;
689 cxgbit_send_halfclose(csk);
690 break;
691 case CSK_STATE_DEAD:
692 release = true;
693 break;
694 default:
695 pr_err("%s: csk %p; state %d\n",
696 __func__, csk, csk->com.state);
698 spin_unlock_bh(&csk->lock);
700 if (release)
701 cxgbit_put_csk(csk);
704 static void cxgbit_set_emss(struct cxgbit_sock *csk, u16 opt)
706 csk->emss = csk->com.cdev->lldi.mtus[TCPOPT_MSS_G(opt)] -
707 ((csk->com.remote_addr.ss_family == AF_INET) ?
708 sizeof(struct iphdr) : sizeof(struct ipv6hdr)) -
709 sizeof(struct tcphdr);
710 csk->mss = csk->emss;
711 if (TCPOPT_TSTAMP_G(opt))
712 csk->emss -= round_up(TCPOLEN_TIMESTAMP, 4);
713 if (csk->emss < 128)
714 csk->emss = 128;
715 if (csk->emss & 7)
716 pr_info("Warning: misaligned mtu idx %u mss %u emss=%u\n",
717 TCPOPT_MSS_G(opt), csk->mss, csk->emss);
718 pr_debug("%s mss_idx %u mss %u emss=%u\n", __func__, TCPOPT_MSS_G(opt),
719 csk->mss, csk->emss);
722 static void cxgbit_free_skb(struct cxgbit_sock *csk)
724 struct sk_buff *skb;
726 __skb_queue_purge(&csk->txq);
727 __skb_queue_purge(&csk->rxq);
728 __skb_queue_purge(&csk->backlogq);
729 __skb_queue_purge(&csk->ppodq);
730 __skb_queue_purge(&csk->skbq);
732 while ((skb = cxgbit_sock_dequeue_wr(csk)))
733 kfree_skb(skb);
735 __kfree_skb(csk->lro_hskb);
738 void _cxgbit_free_csk(struct kref *kref)
740 struct cxgbit_sock *csk;
741 struct cxgbit_device *cdev;
743 csk = container_of(kref, struct cxgbit_sock, kref);
745 pr_debug("%s csk %p state %d\n", __func__, csk, csk->com.state);
747 if (csk->com.local_addr.ss_family == AF_INET6) {
748 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
749 &csk->com.local_addr;
750 cxgb4_clip_release(csk->com.cdev->lldi.ports[0],
751 (const u32 *)
752 &sin6->sin6_addr.s6_addr, 1);
755 cxgb4_remove_tid(csk->com.cdev->lldi.tids, 0, csk->tid);
756 dst_release(csk->dst);
757 cxgb4_l2t_release(csk->l2t);
759 cdev = csk->com.cdev;
760 spin_lock_bh(&cdev->cskq.lock);
761 list_del(&csk->list);
762 spin_unlock_bh(&cdev->cskq.lock);
764 cxgbit_free_skb(csk);
765 cxgbit_put_cdev(cdev);
767 kfree(csk);
770 static void cxgbit_set_tcp_window(struct cxgbit_sock *csk, struct port_info *pi)
772 unsigned int linkspeed;
773 u8 scale;
775 linkspeed = pi->link_cfg.speed;
776 scale = linkspeed / SPEED_10000;
778 #define CXGBIT_10G_RCV_WIN (256 * 1024)
779 csk->rcv_win = CXGBIT_10G_RCV_WIN;
780 if (scale)
781 csk->rcv_win *= scale;
783 #define CXGBIT_10G_SND_WIN (256 * 1024)
784 csk->snd_win = CXGBIT_10G_SND_WIN;
785 if (scale)
786 csk->snd_win *= scale;
788 pr_debug("%s snd_win %d rcv_win %d\n",
789 __func__, csk->snd_win, csk->rcv_win);
792 #ifdef CONFIG_CHELSIO_T4_DCB
793 static u8 cxgbit_get_iscsi_dcb_state(struct net_device *ndev)
795 return ndev->dcbnl_ops->getstate(ndev);
798 static int cxgbit_select_priority(int pri_mask)
800 if (!pri_mask)
801 return 0;
803 return (ffs(pri_mask) - 1);
806 static u8 cxgbit_get_iscsi_dcb_priority(struct net_device *ndev, u16 local_port)
808 int ret;
809 u8 caps;
811 struct dcb_app iscsi_dcb_app = {
812 .protocol = local_port
815 ret = (int)ndev->dcbnl_ops->getcap(ndev, DCB_CAP_ATTR_DCBX, &caps);
817 if (ret)
818 return 0;
820 if (caps & DCB_CAP_DCBX_VER_IEEE) {
821 iscsi_dcb_app.selector = IEEE_8021QAZ_APP_SEL_ANY;
823 ret = dcb_ieee_getapp_mask(ndev, &iscsi_dcb_app);
825 } else if (caps & DCB_CAP_DCBX_VER_CEE) {
826 iscsi_dcb_app.selector = DCB_APP_IDTYPE_PORTNUM;
828 ret = dcb_getapp(ndev, &iscsi_dcb_app);
831 pr_info("iSCSI priority is set to %u\n", cxgbit_select_priority(ret));
833 return cxgbit_select_priority(ret);
835 #endif
837 static int
838 cxgbit_offload_init(struct cxgbit_sock *csk, int iptype, __u8 *peer_ip,
839 u16 local_port, struct dst_entry *dst,
840 struct cxgbit_device *cdev)
842 struct neighbour *n;
843 int ret, step;
844 struct net_device *ndev;
845 u16 rxq_idx, port_id;
846 #ifdef CONFIG_CHELSIO_T4_DCB
847 u8 priority = 0;
848 #endif
850 n = dst_neigh_lookup(dst, peer_ip);
851 if (!n)
852 return -ENODEV;
854 rcu_read_lock();
855 ret = -ENOMEM;
856 if (n->dev->flags & IFF_LOOPBACK) {
857 if (iptype == 4)
858 ndev = cxgbit_ipv4_netdev(*(__be32 *)peer_ip);
859 else if (IS_ENABLED(CONFIG_IPV6))
860 ndev = cxgbit_ipv6_netdev((struct in6_addr *)peer_ip);
861 else
862 ndev = NULL;
864 if (!ndev) {
865 ret = -ENODEV;
866 goto out;
869 csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t,
870 n, ndev, 0);
871 if (!csk->l2t)
872 goto out;
873 csk->mtu = ndev->mtu;
874 csk->tx_chan = cxgb4_port_chan(ndev);
875 csk->smac_idx = cxgb4_tp_smt_idx(cdev->lldi.adapter_type,
876 cxgb4_port_viid(ndev));
877 step = cdev->lldi.ntxq /
878 cdev->lldi.nchan;
879 csk->txq_idx = cxgb4_port_idx(ndev) * step;
880 step = cdev->lldi.nrxq /
881 cdev->lldi.nchan;
882 csk->ctrlq_idx = cxgb4_port_idx(ndev);
883 csk->rss_qid = cdev->lldi.rxq_ids[
884 cxgb4_port_idx(ndev) * step];
885 csk->port_id = cxgb4_port_idx(ndev);
886 cxgbit_set_tcp_window(csk,
887 (struct port_info *)netdev_priv(ndev));
888 } else {
889 ndev = cxgbit_get_real_dev(n->dev);
890 if (!ndev) {
891 ret = -ENODEV;
892 goto out;
895 #ifdef CONFIG_CHELSIO_T4_DCB
896 if (cxgbit_get_iscsi_dcb_state(ndev))
897 priority = cxgbit_get_iscsi_dcb_priority(ndev,
898 local_port);
900 csk->dcb_priority = priority;
902 csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t, n, ndev, priority);
903 #else
904 csk->l2t = cxgb4_l2t_get(cdev->lldi.l2t, n, ndev, 0);
905 #endif
906 if (!csk->l2t)
907 goto out;
908 port_id = cxgb4_port_idx(ndev);
909 csk->mtu = dst_mtu(dst);
910 csk->tx_chan = cxgb4_port_chan(ndev);
911 csk->smac_idx = cxgb4_tp_smt_idx(cdev->lldi.adapter_type,
912 cxgb4_port_viid(ndev));
913 step = cdev->lldi.ntxq /
914 cdev->lldi.nports;
915 csk->txq_idx = (port_id * step) +
916 (cdev->selectq[port_id][0]++ % step);
917 csk->ctrlq_idx = cxgb4_port_idx(ndev);
918 step = cdev->lldi.nrxq /
919 cdev->lldi.nports;
920 rxq_idx = (port_id * step) +
921 (cdev->selectq[port_id][1]++ % step);
922 csk->rss_qid = cdev->lldi.rxq_ids[rxq_idx];
923 csk->port_id = port_id;
924 cxgbit_set_tcp_window(csk,
925 (struct port_info *)netdev_priv(ndev));
927 ret = 0;
928 out:
929 rcu_read_unlock();
930 neigh_release(n);
931 return ret;
934 int cxgbit_ofld_send(struct cxgbit_device *cdev, struct sk_buff *skb)
936 int ret = 0;
938 if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
939 kfree_skb(skb);
940 pr_err("%s - device not up - dropping\n", __func__);
941 return -EIO;
944 ret = cxgb4_ofld_send(cdev->lldi.ports[0], skb);
945 if (ret < 0)
946 kfree_skb(skb);
947 return ret < 0 ? ret : 0;
950 static void cxgbit_release_tid(struct cxgbit_device *cdev, u32 tid)
952 u32 len = roundup(sizeof(struct cpl_tid_release), 16);
953 struct sk_buff *skb;
955 skb = alloc_skb(len, GFP_ATOMIC);
956 if (!skb)
957 return;
959 cxgb_mk_tid_release(skb, len, tid, 0);
960 cxgbit_ofld_send(cdev, skb);
964 cxgbit_l2t_send(struct cxgbit_device *cdev, struct sk_buff *skb,
965 struct l2t_entry *l2e)
967 int ret = 0;
969 if (!test_bit(CDEV_STATE_UP, &cdev->flags)) {
970 kfree_skb(skb);
971 pr_err("%s - device not up - dropping\n", __func__);
972 return -EIO;
975 ret = cxgb4_l2t_send(cdev->lldi.ports[0], skb, l2e);
976 if (ret < 0)
977 kfree_skb(skb);
978 return ret < 0 ? ret : 0;
981 static void cxgbit_send_rx_credits(struct cxgbit_sock *csk, struct sk_buff *skb)
983 if (csk->com.state != CSK_STATE_ESTABLISHED) {
984 __kfree_skb(skb);
985 return;
988 cxgbit_ofld_send(csk->com.cdev, skb);
992 * CPL connection rx data ack: host ->
993 * Send RX credits through an RX_DATA_ACK CPL message.
994 * Returns the number of credits sent.
996 int cxgbit_rx_data_ack(struct cxgbit_sock *csk)
998 struct sk_buff *skb;
999 u32 len = roundup(sizeof(struct cpl_rx_data_ack), 16);
1000 u32 credit_dack;
1002 skb = alloc_skb(len, GFP_KERNEL);
1003 if (!skb)
1004 return -1;
1006 credit_dack = RX_DACK_CHANGE_F | RX_DACK_MODE_V(1) |
1007 RX_CREDITS_V(csk->rx_credits);
1009 cxgb_mk_rx_data_ack(skb, len, csk->tid, csk->ctrlq_idx,
1010 credit_dack);
1012 csk->rx_credits = 0;
1014 spin_lock_bh(&csk->lock);
1015 if (csk->lock_owner) {
1016 cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_send_rx_credits;
1017 __skb_queue_tail(&csk->backlogq, skb);
1018 spin_unlock_bh(&csk->lock);
1019 return 0;
1022 cxgbit_send_rx_credits(csk, skb);
1023 spin_unlock_bh(&csk->lock);
1025 return 0;
1028 #define FLOWC_WR_NPARAMS_MIN 9
1029 #define FLOWC_WR_NPARAMS_MAX 11
1030 static int cxgbit_alloc_csk_skb(struct cxgbit_sock *csk)
1032 struct sk_buff *skb;
1033 u32 len, flowclen;
1034 u8 i;
1036 flowclen = offsetof(struct fw_flowc_wr,
1037 mnemval[FLOWC_WR_NPARAMS_MAX]);
1039 len = max_t(u32, sizeof(struct cpl_abort_req),
1040 sizeof(struct cpl_abort_rpl));
1042 len = max(len, flowclen);
1043 len = roundup(len, 16);
1045 for (i = 0; i < 3; i++) {
1046 skb = alloc_skb(len, GFP_ATOMIC);
1047 if (!skb)
1048 goto out;
1049 __skb_queue_tail(&csk->skbq, skb);
1052 skb = alloc_skb(LRO_SKB_MIN_HEADROOM, GFP_ATOMIC);
1053 if (!skb)
1054 goto out;
1056 memset(skb->data, 0, LRO_SKB_MIN_HEADROOM);
1057 csk->lro_hskb = skb;
1059 return 0;
1060 out:
1061 __skb_queue_purge(&csk->skbq);
1062 return -ENOMEM;
1065 static void
1066 cxgbit_pass_accept_rpl(struct cxgbit_sock *csk, struct cpl_pass_accept_req *req)
1068 struct sk_buff *skb;
1069 const struct tcphdr *tcph;
1070 struct cpl_t5_pass_accept_rpl *rpl5;
1071 struct cxgb4_lld_info *lldi = &csk->com.cdev->lldi;
1072 unsigned int len = roundup(sizeof(*rpl5), 16);
1073 unsigned int mtu_idx;
1074 u64 opt0;
1075 u32 opt2, hlen;
1076 u32 wscale;
1077 u32 win;
1079 pr_debug("%s csk %p tid %u\n", __func__, csk, csk->tid);
1081 skb = alloc_skb(len, GFP_ATOMIC);
1082 if (!skb) {
1083 cxgbit_put_csk(csk);
1084 return;
1087 rpl5 = (struct cpl_t5_pass_accept_rpl *)__skb_put(skb, len);
1088 memset(rpl5, 0, len);
1090 INIT_TP_WR(rpl5, csk->tid);
1091 OPCODE_TID(rpl5) = cpu_to_be32(MK_OPCODE_TID(CPL_PASS_ACCEPT_RPL,
1092 csk->tid));
1093 cxgb_best_mtu(csk->com.cdev->lldi.mtus, csk->mtu, &mtu_idx,
1094 req->tcpopt.tstamp,
1095 (csk->com.remote_addr.ss_family == AF_INET) ? 0 : 1);
1096 wscale = cxgb_compute_wscale(csk->rcv_win);
1098 * Specify the largest window that will fit in opt0. The
1099 * remainder will be specified in the rx_data_ack.
1101 win = csk->rcv_win >> 10;
1102 if (win > RCV_BUFSIZ_M)
1103 win = RCV_BUFSIZ_M;
1104 opt0 = TCAM_BYPASS_F |
1105 WND_SCALE_V(wscale) |
1106 MSS_IDX_V(mtu_idx) |
1107 L2T_IDX_V(csk->l2t->idx) |
1108 TX_CHAN_V(csk->tx_chan) |
1109 SMAC_SEL_V(csk->smac_idx) |
1110 DSCP_V(csk->tos >> 2) |
1111 ULP_MODE_V(ULP_MODE_ISCSI) |
1112 RCV_BUFSIZ_V(win);
1114 opt2 = RX_CHANNEL_V(0) |
1115 RSS_QUEUE_VALID_F | RSS_QUEUE_V(csk->rss_qid);
1117 if (req->tcpopt.tstamp)
1118 opt2 |= TSTAMPS_EN_F;
1119 if (req->tcpopt.sack)
1120 opt2 |= SACK_EN_F;
1121 if (wscale)
1122 opt2 |= WND_SCALE_EN_F;
1124 hlen = ntohl(req->hdr_len);
1126 if (is_t5(lldi->adapter_type))
1127 tcph = (struct tcphdr *)((u8 *)(req + 1) +
1128 ETH_HDR_LEN_G(hlen) + IP_HDR_LEN_G(hlen));
1129 else
1130 tcph = (struct tcphdr *)((u8 *)(req + 1) +
1131 T6_ETH_HDR_LEN_G(hlen) + T6_IP_HDR_LEN_G(hlen));
1133 if (tcph->ece && tcph->cwr)
1134 opt2 |= CCTRL_ECN_V(1);
1136 opt2 |= RX_COALESCE_V(3);
1137 opt2 |= CONG_CNTRL_V(CONG_ALG_NEWRENO);
1139 opt2 |= T5_ISS_F;
1140 rpl5->iss = cpu_to_be32((prandom_u32() & ~7UL) - 1);
1142 opt2 |= T5_OPT_2_VALID_F;
1144 rpl5->opt0 = cpu_to_be64(opt0);
1145 rpl5->opt2 = cpu_to_be32(opt2);
1146 set_wr_txq(skb, CPL_PRIORITY_SETUP, csk->ctrlq_idx);
1147 t4_set_arp_err_handler(skb, NULL, cxgbit_arp_failure_discard);
1148 cxgbit_l2t_send(csk->com.cdev, skb, csk->l2t);
1151 static void
1152 cxgbit_pass_accept_req(struct cxgbit_device *cdev, struct sk_buff *skb)
1154 struct cxgbit_sock *csk = NULL;
1155 struct cxgbit_np *cnp;
1156 struct cpl_pass_accept_req *req = cplhdr(skb);
1157 unsigned int stid = PASS_OPEN_TID_G(ntohl(req->tos_stid));
1158 struct tid_info *t = cdev->lldi.tids;
1159 unsigned int tid = GET_TID(req);
1160 u16 peer_mss = ntohs(req->tcpopt.mss);
1161 unsigned short hdrs;
1163 struct dst_entry *dst;
1164 __u8 local_ip[16], peer_ip[16];
1165 __be16 local_port, peer_port;
1166 int ret;
1167 int iptype;
1169 pr_debug("%s: cdev = %p; stid = %u; tid = %u\n",
1170 __func__, cdev, stid, tid);
1172 cnp = lookup_stid(t, stid);
1173 if (!cnp) {
1174 pr_err("%s connect request on invalid stid %d\n",
1175 __func__, stid);
1176 goto rel_skb;
1179 if (cnp->com.state != CSK_STATE_LISTEN) {
1180 pr_err("%s - listening parent not in CSK_STATE_LISTEN\n",
1181 __func__);
1182 goto reject;
1185 csk = lookup_tid(t, tid);
1186 if (csk) {
1187 pr_err("%s csk not null tid %u\n",
1188 __func__, tid);
1189 goto rel_skb;
1192 cxgb_get_4tuple(req, cdev->lldi.adapter_type, &iptype, local_ip,
1193 peer_ip, &local_port, &peer_port);
1195 /* Find output route */
1196 if (iptype == 4) {
1197 pr_debug("%s parent sock %p tid %u laddr %pI4 raddr %pI4 "
1198 "lport %d rport %d peer_mss %d\n"
1199 , __func__, cnp, tid,
1200 local_ip, peer_ip, ntohs(local_port),
1201 ntohs(peer_port), peer_mss);
1202 dst = cxgb_find_route(&cdev->lldi, cxgbit_get_real_dev,
1203 *(__be32 *)local_ip,
1204 *(__be32 *)peer_ip,
1205 local_port, peer_port,
1206 PASS_OPEN_TOS_G(ntohl(req->tos_stid)));
1207 } else {
1208 pr_debug("%s parent sock %p tid %u laddr %pI6 raddr %pI6 "
1209 "lport %d rport %d peer_mss %d\n"
1210 , __func__, cnp, tid,
1211 local_ip, peer_ip, ntohs(local_port),
1212 ntohs(peer_port), peer_mss);
1213 dst = cxgb_find_route6(&cdev->lldi, cxgbit_get_real_dev,
1214 local_ip, peer_ip,
1215 local_port, peer_port,
1216 PASS_OPEN_TOS_G(ntohl(req->tos_stid)),
1217 ((struct sockaddr_in6 *)
1218 &cnp->com.local_addr)->sin6_scope_id);
1220 if (!dst) {
1221 pr_err("%s - failed to find dst entry!\n",
1222 __func__);
1223 goto reject;
1226 csk = kzalloc(sizeof(*csk), GFP_ATOMIC);
1227 if (!csk) {
1228 dst_release(dst);
1229 goto rel_skb;
1232 ret = cxgbit_offload_init(csk, iptype, peer_ip, ntohs(local_port),
1233 dst, cdev);
1234 if (ret) {
1235 pr_err("%s - failed to allocate l2t entry!\n",
1236 __func__);
1237 dst_release(dst);
1238 kfree(csk);
1239 goto reject;
1242 kref_init(&csk->kref);
1243 init_completion(&csk->com.wr_wait.completion);
1245 INIT_LIST_HEAD(&csk->accept_node);
1247 hdrs = (iptype == 4 ? sizeof(struct iphdr) : sizeof(struct ipv6hdr)) +
1248 sizeof(struct tcphdr) + (req->tcpopt.tstamp ? 12 : 0);
1249 if (peer_mss && csk->mtu > (peer_mss + hdrs))
1250 csk->mtu = peer_mss + hdrs;
1252 csk->com.state = CSK_STATE_CONNECTING;
1253 csk->com.cdev = cdev;
1254 csk->cnp = cnp;
1255 csk->tos = PASS_OPEN_TOS_G(ntohl(req->tos_stid));
1256 csk->dst = dst;
1257 csk->tid = tid;
1258 csk->wr_cred = cdev->lldi.wr_cred -
1259 DIV_ROUND_UP(sizeof(struct cpl_abort_req), 16);
1260 csk->wr_max_cred = csk->wr_cred;
1261 csk->wr_una_cred = 0;
1263 if (iptype == 4) {
1264 struct sockaddr_in *sin = (struct sockaddr_in *)
1265 &csk->com.local_addr;
1266 sin->sin_family = AF_INET;
1267 sin->sin_port = local_port;
1268 sin->sin_addr.s_addr = *(__be32 *)local_ip;
1270 sin = (struct sockaddr_in *)&csk->com.remote_addr;
1271 sin->sin_family = AF_INET;
1272 sin->sin_port = peer_port;
1273 sin->sin_addr.s_addr = *(__be32 *)peer_ip;
1274 } else {
1275 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
1276 &csk->com.local_addr;
1278 sin6->sin6_family = PF_INET6;
1279 sin6->sin6_port = local_port;
1280 memcpy(sin6->sin6_addr.s6_addr, local_ip, 16);
1281 cxgb4_clip_get(cdev->lldi.ports[0],
1282 (const u32 *)&sin6->sin6_addr.s6_addr,
1285 sin6 = (struct sockaddr_in6 *)&csk->com.remote_addr;
1286 sin6->sin6_family = PF_INET6;
1287 sin6->sin6_port = peer_port;
1288 memcpy(sin6->sin6_addr.s6_addr, peer_ip, 16);
1291 skb_queue_head_init(&csk->rxq);
1292 skb_queue_head_init(&csk->txq);
1293 skb_queue_head_init(&csk->ppodq);
1294 skb_queue_head_init(&csk->backlogq);
1295 skb_queue_head_init(&csk->skbq);
1296 cxgbit_sock_reset_wr_list(csk);
1297 spin_lock_init(&csk->lock);
1298 init_waitqueue_head(&csk->waitq);
1299 init_waitqueue_head(&csk->ack_waitq);
1300 csk->lock_owner = false;
1302 if (cxgbit_alloc_csk_skb(csk)) {
1303 dst_release(dst);
1304 kfree(csk);
1305 goto rel_skb;
1308 cxgbit_get_cdev(cdev);
1310 spin_lock(&cdev->cskq.lock);
1311 list_add_tail(&csk->list, &cdev->cskq.list);
1312 spin_unlock(&cdev->cskq.lock);
1314 cxgb4_insert_tid(t, csk, tid);
1315 cxgbit_pass_accept_rpl(csk, req);
1316 goto rel_skb;
1318 reject:
1319 cxgbit_release_tid(cdev, tid);
1320 rel_skb:
1321 __kfree_skb(skb);
1324 static u32
1325 cxgbit_tx_flowc_wr_credits(struct cxgbit_sock *csk, u32 *nparamsp,
1326 u32 *flowclenp)
1328 u32 nparams, flowclen16, flowclen;
1330 nparams = FLOWC_WR_NPARAMS_MIN;
1332 if (csk->snd_wscale)
1333 nparams++;
1335 #ifdef CONFIG_CHELSIO_T4_DCB
1336 nparams++;
1337 #endif
1338 flowclen = offsetof(struct fw_flowc_wr, mnemval[nparams]);
1339 flowclen16 = DIV_ROUND_UP(flowclen, 16);
1340 flowclen = flowclen16 * 16;
1342 * Return the number of 16-byte credits used by the flowc request.
1343 * Pass back the nparams and actual flowc length if requested.
1345 if (nparamsp)
1346 *nparamsp = nparams;
1347 if (flowclenp)
1348 *flowclenp = flowclen;
1349 return flowclen16;
1352 u32 cxgbit_send_tx_flowc_wr(struct cxgbit_sock *csk)
1354 struct cxgbit_device *cdev = csk->com.cdev;
1355 struct fw_flowc_wr *flowc;
1356 u32 nparams, flowclen16, flowclen;
1357 struct sk_buff *skb;
1358 u8 index;
1360 #ifdef CONFIG_CHELSIO_T4_DCB
1361 u16 vlan = ((struct l2t_entry *)csk->l2t)->vlan;
1362 #endif
1364 flowclen16 = cxgbit_tx_flowc_wr_credits(csk, &nparams, &flowclen);
1366 skb = __skb_dequeue(&csk->skbq);
1367 flowc = (struct fw_flowc_wr *)__skb_put(skb, flowclen);
1368 memset(flowc, 0, flowclen);
1370 flowc->op_to_nparams = cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
1371 FW_FLOWC_WR_NPARAMS_V(nparams));
1372 flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(flowclen16) |
1373 FW_WR_FLOWID_V(csk->tid));
1374 flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
1375 flowc->mnemval[0].val = cpu_to_be32(FW_PFVF_CMD_PFN_V
1376 (csk->com.cdev->lldi.pf));
1377 flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
1378 flowc->mnemval[1].val = cpu_to_be32(csk->tx_chan);
1379 flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
1380 flowc->mnemval[2].val = cpu_to_be32(csk->tx_chan);
1381 flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
1382 flowc->mnemval[3].val = cpu_to_be32(csk->rss_qid);
1383 flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
1384 flowc->mnemval[4].val = cpu_to_be32(csk->snd_nxt);
1385 flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
1386 flowc->mnemval[5].val = cpu_to_be32(csk->rcv_nxt);
1387 flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
1388 flowc->mnemval[6].val = cpu_to_be32(csk->snd_win);
1389 flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
1390 flowc->mnemval[7].val = cpu_to_be32(csk->emss);
1392 flowc->mnemval[8].mnemonic = FW_FLOWC_MNEM_TXDATAPLEN_MAX;
1393 if (test_bit(CDEV_ISO_ENABLE, &cdev->flags))
1394 flowc->mnemval[8].val = cpu_to_be32(CXGBIT_MAX_ISO_PAYLOAD);
1395 else
1396 flowc->mnemval[8].val = cpu_to_be32(16384);
1398 index = 9;
1400 if (csk->snd_wscale) {
1401 flowc->mnemval[index].mnemonic = FW_FLOWC_MNEM_RCV_SCALE;
1402 flowc->mnemval[index].val = cpu_to_be32(csk->snd_wscale);
1403 index++;
1406 #ifdef CONFIG_CHELSIO_T4_DCB
1407 flowc->mnemval[index].mnemonic = FW_FLOWC_MNEM_DCBPRIO;
1408 if (vlan == VLAN_NONE) {
1409 pr_warn("csk %u without VLAN Tag on DCB Link\n", csk->tid);
1410 flowc->mnemval[index].val = cpu_to_be32(0);
1411 } else
1412 flowc->mnemval[index].val = cpu_to_be32(
1413 (vlan & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT);
1414 #endif
1416 pr_debug("%s: csk %p; tx_chan = %u; rss_qid = %u; snd_seq = %u;"
1417 " rcv_seq = %u; snd_win = %u; emss = %u\n",
1418 __func__, csk, csk->tx_chan, csk->rss_qid, csk->snd_nxt,
1419 csk->rcv_nxt, csk->snd_win, csk->emss);
1420 set_wr_txq(skb, CPL_PRIORITY_DATA, csk->txq_idx);
1421 cxgbit_ofld_send(csk->com.cdev, skb);
1422 return flowclen16;
1425 int cxgbit_setup_conn_digest(struct cxgbit_sock *csk)
1427 struct sk_buff *skb;
1428 struct cpl_set_tcb_field *req;
1429 u8 hcrc = csk->submode & CXGBIT_SUBMODE_HCRC;
1430 u8 dcrc = csk->submode & CXGBIT_SUBMODE_DCRC;
1431 unsigned int len = roundup(sizeof(*req), 16);
1432 int ret;
1434 skb = alloc_skb(len, GFP_KERNEL);
1435 if (!skb)
1436 return -ENOMEM;
1438 /* set up ulp submode */
1439 req = (struct cpl_set_tcb_field *)__skb_put(skb, len);
1440 memset(req, 0, len);
1442 INIT_TP_WR(req, csk->tid);
1443 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
1444 req->reply_ctrl = htons(NO_REPLY_V(0) | QUEUENO_V(csk->rss_qid));
1445 req->word_cookie = htons(0);
1446 req->mask = cpu_to_be64(0x3 << 4);
1447 req->val = cpu_to_be64(((hcrc ? ULP_CRC_HEADER : 0) |
1448 (dcrc ? ULP_CRC_DATA : 0)) << 4);
1449 set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->ctrlq_idx);
1451 cxgbit_get_csk(csk);
1452 cxgbit_init_wr_wait(&csk->com.wr_wait);
1454 cxgbit_ofld_send(csk->com.cdev, skb);
1456 ret = cxgbit_wait_for_reply(csk->com.cdev,
1457 &csk->com.wr_wait,
1458 csk->tid, 5, __func__);
1459 if (ret)
1460 return -1;
1462 return 0;
1465 int cxgbit_setup_conn_pgidx(struct cxgbit_sock *csk, u32 pg_idx)
1467 struct sk_buff *skb;
1468 struct cpl_set_tcb_field *req;
1469 unsigned int len = roundup(sizeof(*req), 16);
1470 int ret;
1472 skb = alloc_skb(len, GFP_KERNEL);
1473 if (!skb)
1474 return -ENOMEM;
1476 req = (struct cpl_set_tcb_field *)__skb_put(skb, len);
1477 memset(req, 0, len);
1479 INIT_TP_WR(req, csk->tid);
1480 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, csk->tid));
1481 req->reply_ctrl = htons(NO_REPLY_V(0) | QUEUENO_V(csk->rss_qid));
1482 req->word_cookie = htons(0);
1483 req->mask = cpu_to_be64(0x3 << 8);
1484 req->val = cpu_to_be64(pg_idx << 8);
1485 set_wr_txq(skb, CPL_PRIORITY_CONTROL, csk->ctrlq_idx);
1487 cxgbit_get_csk(csk);
1488 cxgbit_init_wr_wait(&csk->com.wr_wait);
1490 cxgbit_ofld_send(csk->com.cdev, skb);
1492 ret = cxgbit_wait_for_reply(csk->com.cdev,
1493 &csk->com.wr_wait,
1494 csk->tid, 5, __func__);
1495 if (ret)
1496 return -1;
1498 return 0;
1501 static void
1502 cxgbit_pass_open_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1504 struct cpl_pass_open_rpl *rpl = cplhdr(skb);
1505 struct tid_info *t = cdev->lldi.tids;
1506 unsigned int stid = GET_TID(rpl);
1507 struct cxgbit_np *cnp = lookup_stid(t, stid);
1509 pr_debug("%s: cnp = %p; stid = %u; status = %d\n",
1510 __func__, cnp, stid, rpl->status);
1512 if (!cnp) {
1513 pr_info("%s stid %d lookup failure\n", __func__, stid);
1514 return;
1517 cxgbit_wake_up(&cnp->com.wr_wait, __func__, rpl->status);
1518 cxgbit_put_cnp(cnp);
1521 static void
1522 cxgbit_close_listsrv_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1524 struct cpl_close_listsvr_rpl *rpl = cplhdr(skb);
1525 struct tid_info *t = cdev->lldi.tids;
1526 unsigned int stid = GET_TID(rpl);
1527 struct cxgbit_np *cnp = lookup_stid(t, stid);
1529 pr_debug("%s: cnp = %p; stid = %u; status = %d\n",
1530 __func__, cnp, stid, rpl->status);
1532 if (!cnp) {
1533 pr_info("%s stid %d lookup failure\n", __func__, stid);
1534 return;
1537 cxgbit_wake_up(&cnp->com.wr_wait, __func__, rpl->status);
1538 cxgbit_put_cnp(cnp);
1541 static void
1542 cxgbit_pass_establish(struct cxgbit_device *cdev, struct sk_buff *skb)
1544 struct cpl_pass_establish *req = cplhdr(skb);
1545 struct tid_info *t = cdev->lldi.tids;
1546 unsigned int tid = GET_TID(req);
1547 struct cxgbit_sock *csk;
1548 struct cxgbit_np *cnp;
1549 u16 tcp_opt = be16_to_cpu(req->tcp_opt);
1550 u32 snd_isn = be32_to_cpu(req->snd_isn);
1551 u32 rcv_isn = be32_to_cpu(req->rcv_isn);
1553 csk = lookup_tid(t, tid);
1554 if (unlikely(!csk)) {
1555 pr_err("can't find connection for tid %u.\n", tid);
1556 goto rel_skb;
1558 cnp = csk->cnp;
1560 pr_debug("%s: csk %p; tid %u; cnp %p\n",
1561 __func__, csk, tid, cnp);
1563 csk->write_seq = snd_isn;
1564 csk->snd_una = snd_isn;
1565 csk->snd_nxt = snd_isn;
1567 csk->rcv_nxt = rcv_isn;
1569 if (csk->rcv_win > (RCV_BUFSIZ_M << 10))
1570 csk->rx_credits = (csk->rcv_win - (RCV_BUFSIZ_M << 10));
1572 csk->snd_wscale = TCPOPT_SND_WSCALE_G(tcp_opt);
1573 cxgbit_set_emss(csk, tcp_opt);
1574 dst_confirm(csk->dst);
1575 csk->com.state = CSK_STATE_ESTABLISHED;
1576 spin_lock_bh(&cnp->np_accept_lock);
1577 list_add_tail(&csk->accept_node, &cnp->np_accept_list);
1578 spin_unlock_bh(&cnp->np_accept_lock);
1579 complete(&cnp->accept_comp);
1580 rel_skb:
1581 __kfree_skb(skb);
1584 static void cxgbit_queue_rx_skb(struct cxgbit_sock *csk, struct sk_buff *skb)
1586 cxgbit_skcb_flags(skb) = 0;
1587 spin_lock_bh(&csk->rxq.lock);
1588 __skb_queue_tail(&csk->rxq, skb);
1589 spin_unlock_bh(&csk->rxq.lock);
1590 wake_up(&csk->waitq);
1593 static void cxgbit_peer_close(struct cxgbit_sock *csk, struct sk_buff *skb)
1595 pr_debug("%s: csk %p; tid %u; state %d\n",
1596 __func__, csk, csk->tid, csk->com.state);
1598 switch (csk->com.state) {
1599 case CSK_STATE_ESTABLISHED:
1600 csk->com.state = CSK_STATE_CLOSING;
1601 cxgbit_queue_rx_skb(csk, skb);
1602 return;
1603 case CSK_STATE_CLOSING:
1604 /* simultaneous close */
1605 csk->com.state = CSK_STATE_MORIBUND;
1606 break;
1607 case CSK_STATE_MORIBUND:
1608 csk->com.state = CSK_STATE_DEAD;
1609 cxgbit_put_csk(csk);
1610 break;
1611 case CSK_STATE_ABORTING:
1612 break;
1613 default:
1614 pr_info("%s: cpl_peer_close in bad state %d\n",
1615 __func__, csk->com.state);
1618 __kfree_skb(skb);
1621 static void cxgbit_close_con_rpl(struct cxgbit_sock *csk, struct sk_buff *skb)
1623 pr_debug("%s: csk %p; tid %u; state %d\n",
1624 __func__, csk, csk->tid, csk->com.state);
1626 switch (csk->com.state) {
1627 case CSK_STATE_CLOSING:
1628 csk->com.state = CSK_STATE_MORIBUND;
1629 break;
1630 case CSK_STATE_MORIBUND:
1631 csk->com.state = CSK_STATE_DEAD;
1632 cxgbit_put_csk(csk);
1633 break;
1634 case CSK_STATE_ABORTING:
1635 case CSK_STATE_DEAD:
1636 break;
1637 default:
1638 pr_info("%s: cpl_close_con_rpl in bad state %d\n",
1639 __func__, csk->com.state);
1642 __kfree_skb(skb);
1645 static void cxgbit_abort_req_rss(struct cxgbit_sock *csk, struct sk_buff *skb)
1647 struct cpl_abort_req_rss *hdr = cplhdr(skb);
1648 unsigned int tid = GET_TID(hdr);
1649 struct sk_buff *rpl_skb;
1650 bool release = false;
1651 bool wakeup_thread = false;
1652 u32 len = roundup(sizeof(struct cpl_abort_rpl), 16);
1654 pr_debug("%s: csk %p; tid %u; state %d\n",
1655 __func__, csk, tid, csk->com.state);
1657 if (cxgb_is_neg_adv(hdr->status)) {
1658 pr_err("%s: got neg advise %d on tid %u\n",
1659 __func__, hdr->status, tid);
1660 goto rel_skb;
1663 switch (csk->com.state) {
1664 case CSK_STATE_CONNECTING:
1665 case CSK_STATE_MORIBUND:
1666 csk->com.state = CSK_STATE_DEAD;
1667 release = true;
1668 break;
1669 case CSK_STATE_ESTABLISHED:
1670 csk->com.state = CSK_STATE_DEAD;
1671 wakeup_thread = true;
1672 break;
1673 case CSK_STATE_CLOSING:
1674 csk->com.state = CSK_STATE_DEAD;
1675 if (!csk->conn)
1676 release = true;
1677 break;
1678 case CSK_STATE_ABORTING:
1679 break;
1680 default:
1681 pr_info("%s: cpl_abort_req_rss in bad state %d\n",
1682 __func__, csk->com.state);
1683 csk->com.state = CSK_STATE_DEAD;
1686 __skb_queue_purge(&csk->txq);
1688 if (!test_and_set_bit(CSK_TX_DATA_SENT, &csk->com.flags))
1689 cxgbit_send_tx_flowc_wr(csk);
1691 rpl_skb = __skb_dequeue(&csk->skbq);
1693 cxgb_mk_abort_rpl(rpl_skb, len, csk->tid, csk->txq_idx);
1694 cxgbit_ofld_send(csk->com.cdev, rpl_skb);
1696 if (wakeup_thread) {
1697 cxgbit_queue_rx_skb(csk, skb);
1698 return;
1701 if (release)
1702 cxgbit_put_csk(csk);
1703 rel_skb:
1704 __kfree_skb(skb);
1707 static void cxgbit_abort_rpl_rss(struct cxgbit_sock *csk, struct sk_buff *skb)
1709 pr_debug("%s: csk %p; tid %u; state %d\n",
1710 __func__, csk, csk->tid, csk->com.state);
1712 switch (csk->com.state) {
1713 case CSK_STATE_ABORTING:
1714 csk->com.state = CSK_STATE_DEAD;
1715 cxgbit_put_csk(csk);
1716 break;
1717 default:
1718 pr_info("%s: cpl_abort_rpl_rss in state %d\n",
1719 __func__, csk->com.state);
1722 __kfree_skb(skb);
1725 static bool cxgbit_credit_err(const struct cxgbit_sock *csk)
1727 const struct sk_buff *skb = csk->wr_pending_head;
1728 u32 credit = 0;
1730 if (unlikely(csk->wr_cred > csk->wr_max_cred)) {
1731 pr_err("csk 0x%p, tid %u, credit %u > %u\n",
1732 csk, csk->tid, csk->wr_cred, csk->wr_max_cred);
1733 return true;
1736 while (skb) {
1737 credit += (__force u32)skb->csum;
1738 skb = cxgbit_skcb_tx_wr_next(skb);
1741 if (unlikely((csk->wr_cred + credit) != csk->wr_max_cred)) {
1742 pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
1743 csk, csk->tid, csk->wr_cred,
1744 credit, csk->wr_max_cred);
1746 return true;
1749 return false;
1752 static void cxgbit_fw4_ack(struct cxgbit_sock *csk, struct sk_buff *skb)
1754 struct cpl_fw4_ack *rpl = (struct cpl_fw4_ack *)cplhdr(skb);
1755 u32 credits = rpl->credits;
1756 u32 snd_una = ntohl(rpl->snd_una);
1758 csk->wr_cred += credits;
1759 if (csk->wr_una_cred > (csk->wr_max_cred - csk->wr_cred))
1760 csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
1762 while (credits) {
1763 struct sk_buff *p = cxgbit_sock_peek_wr(csk);
1764 const u32 csum = (__force u32)p->csum;
1766 if (unlikely(!p)) {
1767 pr_err("csk 0x%p,%u, cr %u,%u+%u, empty.\n",
1768 csk, csk->tid, credits,
1769 csk->wr_cred, csk->wr_una_cred);
1770 break;
1773 if (unlikely(credits < csum)) {
1774 pr_warn("csk 0x%p,%u, cr %u,%u+%u, < %u.\n",
1775 csk, csk->tid,
1776 credits, csk->wr_cred, csk->wr_una_cred,
1777 csum);
1778 p->csum = (__force __wsum)(csum - credits);
1779 break;
1782 cxgbit_sock_dequeue_wr(csk);
1783 credits -= csum;
1784 kfree_skb(p);
1787 if (unlikely(cxgbit_credit_err(csk))) {
1788 cxgbit_queue_rx_skb(csk, skb);
1789 return;
1792 if (rpl->seq_vld & CPL_FW4_ACK_FLAGS_SEQVAL) {
1793 if (unlikely(before(snd_una, csk->snd_una))) {
1794 pr_warn("csk 0x%p,%u, snd_una %u/%u.",
1795 csk, csk->tid, snd_una,
1796 csk->snd_una);
1797 goto rel_skb;
1800 if (csk->snd_una != snd_una) {
1801 csk->snd_una = snd_una;
1802 dst_confirm(csk->dst);
1803 wake_up(&csk->ack_waitq);
1807 if (skb_queue_len(&csk->txq))
1808 cxgbit_push_tx_frames(csk);
1810 rel_skb:
1811 __kfree_skb(skb);
1814 static void cxgbit_set_tcb_rpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1816 struct cxgbit_sock *csk;
1817 struct cpl_set_tcb_rpl *rpl = (struct cpl_set_tcb_rpl *)skb->data;
1818 unsigned int tid = GET_TID(rpl);
1819 struct cxgb4_lld_info *lldi = &cdev->lldi;
1820 struct tid_info *t = lldi->tids;
1822 csk = lookup_tid(t, tid);
1823 if (unlikely(!csk))
1824 pr_err("can't find connection for tid %u.\n", tid);
1825 else
1826 cxgbit_wake_up(&csk->com.wr_wait, __func__, rpl->status);
1828 cxgbit_put_csk(csk);
1831 static void cxgbit_rx_data(struct cxgbit_device *cdev, struct sk_buff *skb)
1833 struct cxgbit_sock *csk;
1834 struct cpl_rx_data *cpl = cplhdr(skb);
1835 unsigned int tid = GET_TID(cpl);
1836 struct cxgb4_lld_info *lldi = &cdev->lldi;
1837 struct tid_info *t = lldi->tids;
1839 csk = lookup_tid(t, tid);
1840 if (unlikely(!csk)) {
1841 pr_err("can't find conn. for tid %u.\n", tid);
1842 goto rel_skb;
1845 cxgbit_queue_rx_skb(csk, skb);
1846 return;
1847 rel_skb:
1848 __kfree_skb(skb);
1851 static void
1852 __cxgbit_process_rx_cpl(struct cxgbit_sock *csk, struct sk_buff *skb)
1854 spin_lock(&csk->lock);
1855 if (csk->lock_owner) {
1856 __skb_queue_tail(&csk->backlogq, skb);
1857 spin_unlock(&csk->lock);
1858 return;
1861 cxgbit_skcb_rx_backlog_fn(skb)(csk, skb);
1862 spin_unlock(&csk->lock);
1865 static void cxgbit_process_rx_cpl(struct cxgbit_sock *csk, struct sk_buff *skb)
1867 cxgbit_get_csk(csk);
1868 __cxgbit_process_rx_cpl(csk, skb);
1869 cxgbit_put_csk(csk);
1872 static void cxgbit_rx_cpl(struct cxgbit_device *cdev, struct sk_buff *skb)
1874 struct cxgbit_sock *csk;
1875 struct cpl_tx_data *cpl = cplhdr(skb);
1876 struct cxgb4_lld_info *lldi = &cdev->lldi;
1877 struct tid_info *t = lldi->tids;
1878 unsigned int tid = GET_TID(cpl);
1879 u8 opcode = cxgbit_skcb_rx_opcode(skb);
1880 bool ref = true;
1882 switch (opcode) {
1883 case CPL_FW4_ACK:
1884 cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_fw4_ack;
1885 ref = false;
1886 break;
1887 case CPL_PEER_CLOSE:
1888 cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_peer_close;
1889 break;
1890 case CPL_CLOSE_CON_RPL:
1891 cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_close_con_rpl;
1892 break;
1893 case CPL_ABORT_REQ_RSS:
1894 cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_abort_req_rss;
1895 break;
1896 case CPL_ABORT_RPL_RSS:
1897 cxgbit_skcb_rx_backlog_fn(skb) = cxgbit_abort_rpl_rss;
1898 break;
1899 default:
1900 goto rel_skb;
1903 csk = lookup_tid(t, tid);
1904 if (unlikely(!csk)) {
1905 pr_err("can't find conn. for tid %u.\n", tid);
1906 goto rel_skb;
1909 if (ref)
1910 cxgbit_process_rx_cpl(csk, skb);
1911 else
1912 __cxgbit_process_rx_cpl(csk, skb);
1914 return;
1915 rel_skb:
1916 __kfree_skb(skb);
1919 cxgbit_cplhandler_func cxgbit_cplhandlers[NUM_CPL_CMDS] = {
1920 [CPL_PASS_OPEN_RPL] = cxgbit_pass_open_rpl,
1921 [CPL_CLOSE_LISTSRV_RPL] = cxgbit_close_listsrv_rpl,
1922 [CPL_PASS_ACCEPT_REQ] = cxgbit_pass_accept_req,
1923 [CPL_PASS_ESTABLISH] = cxgbit_pass_establish,
1924 [CPL_SET_TCB_RPL] = cxgbit_set_tcb_rpl,
1925 [CPL_RX_DATA] = cxgbit_rx_data,
1926 [CPL_FW4_ACK] = cxgbit_rx_cpl,
1927 [CPL_PEER_CLOSE] = cxgbit_rx_cpl,
1928 [CPL_CLOSE_CON_RPL] = cxgbit_rx_cpl,
1929 [CPL_ABORT_REQ_RSS] = cxgbit_rx_cpl,
1930 [CPL_ABORT_RPL_RSS] = cxgbit_rx_cpl,