2 * libcxgbi.c: Chelsio common library for T3/T4 iSCSI driver.
4 * Copyright (c) 2010 Chelsio Communications, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
10 * Written by: Karen Xie (kxie@chelsio.com)
11 * Written by: Rakesh Ranjan (rranjan@chelsio.com)
14 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
16 #include <linux/skbuff.h>
17 #include <linux/crypto.h>
18 #include <linux/scatterlist.h>
19 #include <linux/pci.h>
20 #include <scsi/scsi.h>
21 #include <scsi/scsi_cmnd.h>
22 #include <scsi/scsi_host.h>
23 #include <linux/if_vlan.h>
24 #include <linux/inet.h>
26 #include <net/route.h>
27 #include <linux/inetdevice.h> /* ip_dev_find */
30 static unsigned int dbg_level;
34 #define DRV_MODULE_NAME "libcxgbi"
35 #define DRV_MODULE_DESC "Chelsio iSCSI driver library"
36 #define DRV_MODULE_VERSION "0.9.0"
37 #define DRV_MODULE_RELDATE "Jun. 2010"
39 MODULE_AUTHOR("Chelsio Communications, Inc.");
40 MODULE_DESCRIPTION(DRV_MODULE_DESC);
41 MODULE_VERSION(DRV_MODULE_VERSION);
42 MODULE_LICENSE("GPL");
44 module_param(dbg_level, uint, 0644);
45 MODULE_PARM_DESC(dbg_level, "libiscsi debug level (default=0)");
49 * cxgbi device management
50 * maintains a list of the cxgbi devices
52 static LIST_HEAD(cdev_list);
53 static DEFINE_MUTEX(cdev_mutex);
55 int cxgbi_device_portmap_create(struct cxgbi_device *cdev, unsigned int base,
56 unsigned int max_conn)
58 struct cxgbi_ports_map *pmap = &cdev->pmap;
60 pmap->port_csk = cxgbi_alloc_big_mem(max_conn *
61 sizeof(struct cxgbi_sock *),
63 if (!pmap->port_csk) {
64 pr_warn("cdev 0x%p, portmap OOM %u.\n", cdev, max_conn);
68 pmap->max_connect = max_conn;
69 pmap->sport_base = base;
70 spin_lock_init(&pmap->lock);
73 EXPORT_SYMBOL_GPL(cxgbi_device_portmap_create);
75 void cxgbi_device_portmap_cleanup(struct cxgbi_device *cdev)
77 struct cxgbi_ports_map *pmap = &cdev->pmap;
78 struct cxgbi_sock *csk;
81 for (i = 0; i < pmap->max_connect; i++) {
82 if (pmap->port_csk[i]) {
83 csk = pmap->port_csk[i];
84 pmap->port_csk[i] = NULL;
85 log_debug(1 << CXGBI_DBG_SOCK,
86 "csk 0x%p, cdev 0x%p, offload down.\n",
88 spin_lock_bh(&csk->lock);
89 cxgbi_sock_set_flag(csk, CTPF_OFFLOAD_DOWN);
90 cxgbi_sock_closed(csk);
91 spin_unlock_bh(&csk->lock);
96 EXPORT_SYMBOL_GPL(cxgbi_device_portmap_cleanup);
98 static inline void cxgbi_device_destroy(struct cxgbi_device *cdev)
100 log_debug(1 << CXGBI_DBG_DEV,
101 "cdev 0x%p, p# %u.\n", cdev, cdev->nports);
102 cxgbi_hbas_remove(cdev);
103 cxgbi_device_portmap_cleanup(cdev);
104 if (cdev->dev_ddp_cleanup)
105 cdev->dev_ddp_cleanup(cdev);
107 cxgbi_ddp_cleanup(cdev);
109 cxgbi_ddp_cleanup(cdev);
110 if (cdev->pmap.max_connect)
111 cxgbi_free_big_mem(cdev->pmap.port_csk);
115 struct cxgbi_device *cxgbi_device_register(unsigned int extra,
118 struct cxgbi_device *cdev;
120 cdev = kzalloc(sizeof(*cdev) + extra + nports *
121 (sizeof(struct cxgbi_hba *) +
122 sizeof(struct net_device *)),
125 pr_warn("nport %d, OOM.\n", nports);
128 cdev->ports = (struct net_device **)(cdev + 1);
129 cdev->hbas = (struct cxgbi_hba **)(((char*)cdev->ports) + nports *
130 sizeof(struct net_device *));
132 cdev->dd_data = ((char *)cdev->hbas) +
133 nports * sizeof(struct cxgbi_hba *);
134 spin_lock_init(&cdev->pmap.lock);
136 mutex_lock(&cdev_mutex);
137 list_add_tail(&cdev->list_head, &cdev_list);
138 mutex_unlock(&cdev_mutex);
140 log_debug(1 << CXGBI_DBG_DEV,
141 "cdev 0x%p, p# %u.\n", cdev, nports);
144 EXPORT_SYMBOL_GPL(cxgbi_device_register);
146 void cxgbi_device_unregister(struct cxgbi_device *cdev)
148 log_debug(1 << CXGBI_DBG_DEV,
149 "cdev 0x%p, p# %u,%s.\n",
150 cdev, cdev->nports, cdev->nports ? cdev->ports[0]->name : "");
151 mutex_lock(&cdev_mutex);
152 list_del(&cdev->list_head);
153 mutex_unlock(&cdev_mutex);
154 cxgbi_device_destroy(cdev);
156 EXPORT_SYMBOL_GPL(cxgbi_device_unregister);
158 void cxgbi_device_unregister_all(unsigned int flag)
160 struct cxgbi_device *cdev, *tmp;
162 mutex_lock(&cdev_mutex);
163 list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
164 if ((cdev->flags & flag) == flag) {
165 log_debug(1 << CXGBI_DBG_DEV,
166 "cdev 0x%p, p# %u,%s.\n",
167 cdev, cdev->nports, cdev->nports ?
168 cdev->ports[0]->name : "");
169 list_del(&cdev->list_head);
170 cxgbi_device_destroy(cdev);
173 mutex_unlock(&cdev_mutex);
175 EXPORT_SYMBOL_GPL(cxgbi_device_unregister_all);
177 struct cxgbi_device *cxgbi_device_find_by_lldev(void *lldev)
179 struct cxgbi_device *cdev, *tmp;
181 mutex_lock(&cdev_mutex);
182 list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
183 if (cdev->lldev == lldev) {
184 mutex_unlock(&cdev_mutex);
188 mutex_unlock(&cdev_mutex);
189 log_debug(1 << CXGBI_DBG_DEV,
190 "lldev 0x%p, NO match found.\n", lldev);
193 EXPORT_SYMBOL_GPL(cxgbi_device_find_by_lldev);
195 static struct cxgbi_device *cxgbi_device_find_by_netdev(struct net_device *ndev,
198 struct net_device *vdev = NULL;
199 struct cxgbi_device *cdev, *tmp;
202 if (ndev->priv_flags & IFF_802_1Q_VLAN) {
204 ndev = vlan_dev_real_dev(ndev);
205 log_debug(1 << CXGBI_DBG_DEV,
206 "vlan dev %s -> %s.\n", vdev->name, ndev->name);
209 mutex_lock(&cdev_mutex);
210 list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
211 for (i = 0; i < cdev->nports; i++) {
212 if (ndev == cdev->ports[i]) {
213 cdev->hbas[i]->vdev = vdev;
214 mutex_unlock(&cdev_mutex);
221 mutex_unlock(&cdev_mutex);
222 log_debug(1 << CXGBI_DBG_DEV,
223 "ndev 0x%p, %s, NO match found.\n", ndev, ndev->name);
227 void cxgbi_hbas_remove(struct cxgbi_device *cdev)
230 struct cxgbi_hba *chba;
232 log_debug(1 << CXGBI_DBG_DEV,
233 "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
235 for (i = 0; i < cdev->nports; i++) {
236 chba = cdev->hbas[i];
238 cdev->hbas[i] = NULL;
239 iscsi_host_remove(chba->shost);
240 pci_dev_put(cdev->pdev);
241 iscsi_host_free(chba->shost);
245 EXPORT_SYMBOL_GPL(cxgbi_hbas_remove);
247 int cxgbi_hbas_add(struct cxgbi_device *cdev, unsigned int max_lun,
248 unsigned int max_id, struct scsi_host_template *sht,
249 struct scsi_transport_template *stt)
251 struct cxgbi_hba *chba;
252 struct Scsi_Host *shost;
255 log_debug(1 << CXGBI_DBG_DEV, "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
257 for (i = 0; i < cdev->nports; i++) {
258 shost = iscsi_host_alloc(sht, sizeof(*chba), 1);
260 pr_info("0x%p, p%d, %s, host alloc failed.\n",
261 cdev, i, cdev->ports[i]->name);
266 shost->transportt = stt;
267 shost->max_lun = max_lun;
268 shost->max_id = max_id;
269 shost->max_channel = 0;
270 shost->max_cmd_len = 16;
272 chba = iscsi_host_priv(shost);
274 chba->ndev = cdev->ports[i];
277 log_debug(1 << CXGBI_DBG_DEV,
278 "cdev 0x%p, p#%d %s: chba 0x%p.\n",
279 cdev, i, cdev->ports[i]->name, chba);
281 pci_dev_get(cdev->pdev);
282 err = iscsi_host_add(shost, &cdev->pdev->dev);
284 pr_info("cdev 0x%p, p#%d %s, host add failed.\n",
285 cdev, i, cdev->ports[i]->name);
286 pci_dev_put(cdev->pdev);
287 scsi_host_put(shost);
291 cdev->hbas[i] = chba;
297 cxgbi_hbas_remove(cdev);
300 EXPORT_SYMBOL_GPL(cxgbi_hbas_add);
305 * - source port management
306 * To find a free source port in the port allocation map we use a very simple
307 * rotor scheme to look for the next free port.
309 * If a source port has been specified make sure that it doesn't collide with
310 * our normal source port allocation map. If it's outside the range of our
311 * allocation/deallocation scheme just let them use it.
313 * If the source port is outside our allocation range, the caller is
314 * responsible for keeping track of their port usage.
316 static int sock_get_port(struct cxgbi_sock *csk)
318 struct cxgbi_device *cdev = csk->cdev;
319 struct cxgbi_ports_map *pmap = &cdev->pmap;
323 if (!pmap->max_connect) {
324 pr_err("cdev 0x%p, p#%u %s, NO port map.\n",
325 cdev, csk->port_id, cdev->ports[csk->port_id]->name);
326 return -EADDRNOTAVAIL;
329 if (csk->saddr.sin_port) {
330 pr_err("source port NON-ZERO %u.\n",
331 ntohs(csk->saddr.sin_port));
335 spin_lock_bh(&pmap->lock);
336 if (pmap->used >= pmap->max_connect) {
337 spin_unlock_bh(&pmap->lock);
338 pr_info("cdev 0x%p, p#%u %s, ALL ports used.\n",
339 cdev, csk->port_id, cdev->ports[csk->port_id]->name);
340 return -EADDRNOTAVAIL;
343 start = idx = pmap->next;
345 if (++idx >= pmap->max_connect)
347 if (!pmap->port_csk[idx]) {
349 csk->saddr.sin_port =
350 htons(pmap->sport_base + idx);
352 pmap->port_csk[idx] = csk;
353 spin_unlock_bh(&pmap->lock);
355 log_debug(1 << CXGBI_DBG_SOCK,
356 "cdev 0x%p, p#%u %s, p %u, %u.\n",
358 cdev->ports[csk->port_id]->name,
359 pmap->sport_base + idx, pmap->next);
362 } while (idx != start);
363 spin_unlock_bh(&pmap->lock);
365 /* should not happen */
366 pr_warn("cdev 0x%p, p#%u %s, next %u?\n",
367 cdev, csk->port_id, cdev->ports[csk->port_id]->name,
369 return -EADDRNOTAVAIL;
372 static void sock_put_port(struct cxgbi_sock *csk)
374 struct cxgbi_device *cdev = csk->cdev;
375 struct cxgbi_ports_map *pmap = &cdev->pmap;
377 if (csk->saddr.sin_port) {
378 int idx = ntohs(csk->saddr.sin_port) - pmap->sport_base;
380 csk->saddr.sin_port = 0;
381 if (idx < 0 || idx >= pmap->max_connect) {
382 pr_err("cdev 0x%p, p#%u %s, port %u OOR.\n",
384 cdev->ports[csk->port_id]->name,
385 ntohs(csk->saddr.sin_port));
389 spin_lock_bh(&pmap->lock);
390 pmap->port_csk[idx] = NULL;
392 spin_unlock_bh(&pmap->lock);
394 log_debug(1 << CXGBI_DBG_SOCK,
395 "cdev 0x%p, p#%u %s, release %u.\n",
396 cdev, csk->port_id, cdev->ports[csk->port_id]->name,
397 pmap->sport_base + idx);
404 * iscsi tcp connection
406 void cxgbi_sock_free_cpl_skbs(struct cxgbi_sock *csk)
408 if (csk->cpl_close) {
409 kfree_skb(csk->cpl_close);
410 csk->cpl_close = NULL;
412 if (csk->cpl_abort_req) {
413 kfree_skb(csk->cpl_abort_req);
414 csk->cpl_abort_req = NULL;
416 if (csk->cpl_abort_rpl) {
417 kfree_skb(csk->cpl_abort_rpl);
418 csk->cpl_abort_rpl = NULL;
421 EXPORT_SYMBOL_GPL(cxgbi_sock_free_cpl_skbs);
423 static struct cxgbi_sock *cxgbi_sock_create(struct cxgbi_device *cdev)
425 struct cxgbi_sock *csk = kzalloc(sizeof(*csk), GFP_NOIO);
428 pr_info("alloc csk %zu failed.\n", sizeof(*csk));
432 if (cdev->csk_alloc_cpls(csk) < 0) {
433 pr_info("csk 0x%p, alloc cpls failed.\n", csk);
438 spin_lock_init(&csk->lock);
439 kref_init(&csk->refcnt);
440 skb_queue_head_init(&csk->receive_queue);
441 skb_queue_head_init(&csk->write_queue);
442 setup_timer(&csk->retry_timer, NULL, (unsigned long)csk);
443 rwlock_init(&csk->callback_lock);
446 cxgbi_sock_set_state(csk, CTP_CLOSED);
448 log_debug(1 << CXGBI_DBG_SOCK, "cdev 0x%p, new csk 0x%p.\n", cdev, csk);
453 static struct rtable *find_route_ipv4(__be32 saddr, __be32 daddr,
454 __be16 sport, __be16 dport, u8 tos)
458 rt = ip_route_output_ports(&init_net, NULL, daddr, saddr,
459 dport, sport, IPPROTO_TCP, tos, 0);
466 static struct cxgbi_sock *cxgbi_check_route(struct sockaddr *dst_addr)
468 struct sockaddr_in *daddr = (struct sockaddr_in *)dst_addr;
469 struct dst_entry *dst;
470 struct net_device *ndev;
471 struct cxgbi_device *cdev;
472 struct rtable *rt = NULL;
473 struct cxgbi_sock *csk = NULL;
474 unsigned int mtu = 0;
478 if (daddr->sin_family != AF_INET) {
479 pr_info("address family 0x%x NOT supported.\n",
485 rt = find_route_ipv4(0, daddr->sin_addr.s_addr, 0, daddr->sin_port, 0);
487 pr_info("no route to ipv4 0x%x, port %u.\n",
488 daddr->sin_addr.s_addr, daddr->sin_port);
493 ndev = dst->neighbour->dev;
495 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
496 pr_info("multi-cast route %pI4, port %u, dev %s.\n",
497 &daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
503 if (ndev->flags & IFF_LOOPBACK) {
504 ndev = ip_dev_find(&init_net, daddr->sin_addr.s_addr);
506 pr_info("rt dev %s, loopback -> %s, mtu %u.\n",
507 dst->neighbour->dev->name, ndev->name, mtu);
510 cdev = cxgbi_device_find_by_netdev(ndev, &port);
512 pr_info("dst %pI4, %s, NOT cxgbi device.\n",
513 &daddr->sin_addr.s_addr, ndev->name);
517 log_debug(1 << CXGBI_DBG_SOCK,
518 "route to %pI4 :%u, ndev p#%d,%s, cdev 0x%p.\n",
519 &daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
520 port, ndev->name, cdev);
522 csk = cxgbi_sock_create(cdev);
531 csk->daddr.sin_addr.s_addr = daddr->sin_addr.s_addr;
532 csk->daddr.sin_port = daddr->sin_port;
533 csk->daddr.sin_family = daddr->sin_family;
534 csk->saddr.sin_addr.s_addr = rt->rt_src;
541 cxgbi_sock_closed(csk);
546 void cxgbi_sock_established(struct cxgbi_sock *csk, unsigned int snd_isn,
549 csk->write_seq = csk->snd_nxt = csk->snd_una = snd_isn;
550 dst_confirm(csk->dst);
552 cxgbi_sock_set_state(csk, CTP_ESTABLISHED);
554 EXPORT_SYMBOL_GPL(cxgbi_sock_established);
556 static void cxgbi_inform_iscsi_conn_closing(struct cxgbi_sock *csk)
558 log_debug(1 << CXGBI_DBG_SOCK,
559 "csk 0x%p, state %u, flags 0x%lx, conn 0x%p.\n",
560 csk, csk->state, csk->flags, csk->user_data);
562 if (csk->state != CTP_ESTABLISHED) {
563 read_lock_bh(&csk->callback_lock);
565 iscsi_conn_failure(csk->user_data,
566 ISCSI_ERR_CONN_FAILED);
567 read_unlock_bh(&csk->callback_lock);
571 void cxgbi_sock_closed(struct cxgbi_sock *csk)
573 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
574 csk, (csk)->state, (csk)->flags, (csk)->tid);
575 cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
576 if (csk->state == CTP_ACTIVE_OPEN || csk->state == CTP_CLOSED)
578 if (csk->saddr.sin_port)
581 dst_release(csk->dst);
582 csk->cdev->csk_release_offload_resources(csk);
583 cxgbi_sock_set_state(csk, CTP_CLOSED);
584 cxgbi_inform_iscsi_conn_closing(csk);
587 EXPORT_SYMBOL_GPL(cxgbi_sock_closed);
589 static void need_active_close(struct cxgbi_sock *csk)
594 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
595 csk, (csk)->state, (csk)->flags, (csk)->tid);
596 spin_lock_bh(&csk->lock);
597 dst_confirm(csk->dst);
598 data_lost = skb_queue_len(&csk->receive_queue);
599 __skb_queue_purge(&csk->receive_queue);
601 if (csk->state == CTP_ACTIVE_OPEN)
602 cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
603 else if (csk->state == CTP_ESTABLISHED) {
605 cxgbi_sock_set_state(csk, CTP_ACTIVE_CLOSE);
606 } else if (csk->state == CTP_PASSIVE_CLOSE) {
608 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
613 csk->cdev->csk_send_abort_req(csk);
615 csk->cdev->csk_send_close_req(csk);
618 spin_unlock_bh(&csk->lock);
621 void cxgbi_sock_fail_act_open(struct cxgbi_sock *csk, int errno)
623 pr_info("csk 0x%p,%u,%lx, %pI4:%u-%pI4:%u, err %d.\n",
624 csk, csk->state, csk->flags,
625 &csk->saddr.sin_addr.s_addr, csk->saddr.sin_port,
626 &csk->daddr.sin_addr.s_addr, csk->daddr.sin_port,
629 cxgbi_sock_set_state(csk, CTP_CONNECTING);
631 cxgbi_sock_closed(csk);
633 EXPORT_SYMBOL_GPL(cxgbi_sock_fail_act_open);
635 void cxgbi_sock_act_open_req_arp_failure(void *handle, struct sk_buff *skb)
637 struct cxgbi_sock *csk = (struct cxgbi_sock *)skb->sk;
639 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
640 csk, (csk)->state, (csk)->flags, (csk)->tid);
642 spin_lock_bh(&csk->lock);
643 if (csk->state == CTP_ACTIVE_OPEN)
644 cxgbi_sock_fail_act_open(csk, -EHOSTUNREACH);
645 spin_unlock_bh(&csk->lock);
649 EXPORT_SYMBOL_GPL(cxgbi_sock_act_open_req_arp_failure);
651 void cxgbi_sock_rcv_abort_rpl(struct cxgbi_sock *csk)
654 spin_lock_bh(&csk->lock);
655 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
656 if (!cxgbi_sock_flag(csk, CTPF_ABORT_RPL_RCVD))
657 cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_RCVD);
659 cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_RCVD);
660 cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_PENDING);
661 if (cxgbi_sock_flag(csk, CTPF_ABORT_REQ_RCVD))
662 pr_err("csk 0x%p,%u,0x%lx,%u,ABT_RPL_RSS.\n",
663 csk, csk->state, csk->flags, csk->tid);
664 cxgbi_sock_closed(csk);
667 spin_unlock_bh(&csk->lock);
670 EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_abort_rpl);
672 void cxgbi_sock_rcv_peer_close(struct cxgbi_sock *csk)
674 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
675 csk, (csk)->state, (csk)->flags, (csk)->tid);
677 spin_lock_bh(&csk->lock);
679 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
682 switch (csk->state) {
683 case CTP_ESTABLISHED:
684 cxgbi_sock_set_state(csk, CTP_PASSIVE_CLOSE);
686 case CTP_ACTIVE_CLOSE:
687 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
689 case CTP_CLOSE_WAIT_1:
690 cxgbi_sock_closed(csk);
695 pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
696 csk, csk->state, csk->flags, csk->tid);
698 cxgbi_inform_iscsi_conn_closing(csk);
700 spin_unlock_bh(&csk->lock);
703 EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_peer_close);
705 void cxgbi_sock_rcv_close_conn_rpl(struct cxgbi_sock *csk, u32 snd_nxt)
707 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
708 csk, (csk)->state, (csk)->flags, (csk)->tid);
710 spin_lock_bh(&csk->lock);
712 csk->snd_una = snd_nxt - 1;
713 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
716 switch (csk->state) {
717 case CTP_ACTIVE_CLOSE:
718 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_1);
720 case CTP_CLOSE_WAIT_1:
721 case CTP_CLOSE_WAIT_2:
722 cxgbi_sock_closed(csk);
727 pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
728 csk, csk->state, csk->flags, csk->tid);
731 spin_unlock_bh(&csk->lock);
734 EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_close_conn_rpl);
736 void cxgbi_sock_rcv_wr_ack(struct cxgbi_sock *csk, unsigned int credits,
737 unsigned int snd_una, int seq_chk)
739 log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
740 "csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, snd_una %u,%d.\n",
741 csk, csk->state, csk->flags, csk->tid, credits,
742 csk->wr_cred, csk->wr_una_cred, snd_una, seq_chk);
744 spin_lock_bh(&csk->lock);
746 csk->wr_cred += credits;
747 if (csk->wr_una_cred > csk->wr_max_cred - csk->wr_cred)
748 csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
751 struct sk_buff *p = cxgbi_sock_peek_wr(csk);
754 pr_err("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, empty.\n",
755 csk, csk->state, csk->flags, csk->tid, credits,
756 csk->wr_cred, csk->wr_una_cred);
760 if (unlikely(credits < p->csum)) {
761 pr_warn("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, < %u.\n",
762 csk, csk->state, csk->flags, csk->tid,
763 credits, csk->wr_cred, csk->wr_una_cred,
768 cxgbi_sock_dequeue_wr(csk);
774 cxgbi_sock_check_wr_invariants(csk);
777 if (unlikely(before(snd_una, csk->snd_una))) {
778 pr_warn("csk 0x%p,%u,0x%lx,%u, snd_una %u/%u.",
779 csk, csk->state, csk->flags, csk->tid, snd_una,
784 if (csk->snd_una != snd_una) {
785 csk->snd_una = snd_una;
786 dst_confirm(csk->dst);
790 if (skb_queue_len(&csk->write_queue)) {
791 if (csk->cdev->csk_push_tx_frames(csk, 0))
792 cxgbi_conn_tx_open(csk);
794 cxgbi_conn_tx_open(csk);
796 spin_unlock_bh(&csk->lock);
798 EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_wr_ack);
800 static unsigned int cxgbi_sock_find_best_mtu(struct cxgbi_sock *csk,
805 while (i < csk->cdev->nmtus - 1 && csk->cdev->mtus[i + 1] <= mtu)
811 unsigned int cxgbi_sock_select_mss(struct cxgbi_sock *csk, unsigned int pmtu)
814 struct dst_entry *dst = csk->dst;
816 csk->advmss = dst_metric_advmss(dst);
818 if (csk->advmss > pmtu - 40)
819 csk->advmss = pmtu - 40;
820 if (csk->advmss < csk->cdev->mtus[0] - 40)
821 csk->advmss = csk->cdev->mtus[0] - 40;
822 idx = cxgbi_sock_find_best_mtu(csk, csk->advmss + 40);
826 EXPORT_SYMBOL_GPL(cxgbi_sock_select_mss);
828 void cxgbi_sock_skb_entail(struct cxgbi_sock *csk, struct sk_buff *skb)
830 cxgbi_skcb_tcp_seq(skb) = csk->write_seq;
831 __skb_queue_tail(&csk->write_queue, skb);
833 EXPORT_SYMBOL_GPL(cxgbi_sock_skb_entail);
835 void cxgbi_sock_purge_wr_queue(struct cxgbi_sock *csk)
839 while ((skb = cxgbi_sock_dequeue_wr(csk)) != NULL)
842 EXPORT_SYMBOL_GPL(cxgbi_sock_purge_wr_queue);
844 void cxgbi_sock_check_wr_invariants(const struct cxgbi_sock *csk)
846 int pending = cxgbi_sock_count_pending_wrs(csk);
848 if (unlikely(csk->wr_cred + pending != csk->wr_max_cred))
849 pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
850 csk, csk->tid, csk->wr_cred, pending, csk->wr_max_cred);
852 EXPORT_SYMBOL_GPL(cxgbi_sock_check_wr_invariants);
854 static int cxgbi_sock_send_pdus(struct cxgbi_sock *csk, struct sk_buff *skb)
856 struct cxgbi_device *cdev = csk->cdev;
857 struct sk_buff *next;
860 spin_lock_bh(&csk->lock);
862 if (csk->state != CTP_ESTABLISHED) {
863 log_debug(1 << CXGBI_DBG_PDU_TX,
864 "csk 0x%p,%u,0x%lx,%u, EAGAIN.\n",
865 csk, csk->state, csk->flags, csk->tid);
871 log_debug(1 << CXGBI_DBG_PDU_TX,
872 "csk 0x%p,%u,0x%lx,%u, EPIPE %d.\n",
873 csk, csk->state, csk->flags, csk->tid, csk->err);
878 if (csk->write_seq - csk->snd_una >= cdev->snd_win) {
879 log_debug(1 << CXGBI_DBG_PDU_TX,
880 "csk 0x%p,%u,0x%lx,%u, FULL %u-%u >= %u.\n",
881 csk, csk->state, csk->flags, csk->tid, csk->write_seq,
882 csk->snd_una, cdev->snd_win);
888 int frags = skb_shinfo(skb)->nr_frags +
889 (skb->len != skb->data_len);
891 if (unlikely(skb_headroom(skb) < cdev->skb_tx_rsvd)) {
892 pr_err("csk 0x%p, skb head %u < %u.\n",
893 csk, skb_headroom(skb), cdev->skb_tx_rsvd);
898 if (frags >= SKB_WR_LIST_SIZE) {
899 pr_err("csk 0x%p, frags %d, %u,%u >%u.\n",
900 csk, skb_shinfo(skb)->nr_frags, skb->len,
901 skb->data_len, (uint)(SKB_WR_LIST_SIZE));
908 cxgbi_skcb_set_flag(skb, SKCBF_TX_NEED_HDR);
909 cxgbi_sock_skb_entail(csk, skb);
911 csk->write_seq += skb->len +
912 cxgbi_ulp_extra_len(cxgbi_skcb_ulp_mode(skb));
916 if (likely(skb_queue_len(&csk->write_queue)))
917 cdev->csk_push_tx_frames(csk, 1);
918 spin_unlock_bh(&csk->lock);
922 if (copied == 0 && err == -EPIPE)
923 copied = csk->err ? csk->err : -EPIPE;
930 * Direct Data Placement -
931 * Directly place the iSCSI Data-In or Data-Out PDU's payload into pre-posted
932 * final destination host-memory buffers based on the Initiator Task Tag (ITT)
933 * in Data-In or Target Task Tag (TTT) in Data-Out PDUs.
934 * The host memory address is programmed into h/w in the format of pagepod
936 * The location of the pagepod entry is encoded into ddp tag which is used as
937 * the base for ITT/TTT.
940 static unsigned char ddp_page_order[DDP_PGIDX_MAX] = {0, 1, 2, 4};
941 static unsigned char ddp_page_shift[DDP_PGIDX_MAX] = {12, 13, 14, 16};
942 static unsigned char page_idx = DDP_PGIDX_MAX;
944 static unsigned char sw_tag_idx_bits;
945 static unsigned char sw_tag_age_bits;
948 * Direct-Data Placement page size adjustment
950 static int ddp_adjust_page_table(void)
953 unsigned int base_order, order;
955 if (PAGE_SIZE < (1UL << ddp_page_shift[0])) {
956 pr_info("PAGE_SIZE 0x%lx too small, min 0x%lx\n",
957 PAGE_SIZE, 1UL << ddp_page_shift[0]);
961 base_order = get_order(1UL << ddp_page_shift[0]);
962 order = get_order(1UL << PAGE_SHIFT);
964 for (i = 0; i < DDP_PGIDX_MAX; i++) {
965 /* first is the kernel page size, then just doubling */
966 ddp_page_order[i] = order - base_order + i;
967 ddp_page_shift[i] = PAGE_SHIFT + i;
972 static int ddp_find_page_index(unsigned long pgsz)
976 for (i = 0; i < DDP_PGIDX_MAX; i++) {
977 if (pgsz == (1UL << ddp_page_shift[i]))
980 pr_info("ddp page size %lu not supported.\n", pgsz);
981 return DDP_PGIDX_MAX;
984 static void ddp_setup_host_page_size(void)
986 if (page_idx == DDP_PGIDX_MAX) {
987 page_idx = ddp_find_page_index(PAGE_SIZE);
989 if (page_idx == DDP_PGIDX_MAX) {
990 pr_info("system PAGE %lu, update hw.\n", PAGE_SIZE);
991 if (ddp_adjust_page_table() < 0) {
992 pr_info("PAGE %lu, disable ddp.\n", PAGE_SIZE);
995 page_idx = ddp_find_page_index(PAGE_SIZE);
997 pr_info("system PAGE %lu, ddp idx %u.\n", PAGE_SIZE, page_idx);
1001 void cxgbi_ddp_page_size_factor(int *pgsz_factor)
1005 for (i = 0; i < DDP_PGIDX_MAX; i++)
1006 pgsz_factor[i] = ddp_page_order[i];
1008 EXPORT_SYMBOL_GPL(cxgbi_ddp_page_size_factor);
1011 * DDP setup & teardown
1014 void cxgbi_ddp_ppod_set(struct cxgbi_pagepod *ppod,
1015 struct cxgbi_pagepod_hdr *hdr,
1016 struct cxgbi_gather_list *gl, unsigned int gidx)
1020 memcpy(ppod, hdr, sizeof(*hdr));
1021 for (i = 0; i < (PPOD_PAGES_MAX + 1); i++, gidx++) {
1022 ppod->addr[i] = gidx < gl->nelem ?
1023 cpu_to_be64(gl->phys_addr[gidx]) : 0ULL;
1026 EXPORT_SYMBOL_GPL(cxgbi_ddp_ppod_set);
1028 void cxgbi_ddp_ppod_clear(struct cxgbi_pagepod *ppod)
1030 memset(ppod, 0, sizeof(*ppod));
1032 EXPORT_SYMBOL_GPL(cxgbi_ddp_ppod_clear);
1034 static inline int ddp_find_unused_entries(struct cxgbi_ddp_info *ddp,
1035 unsigned int start, unsigned int max,
1037 struct cxgbi_gather_list *gl)
1039 unsigned int i, j, k;
1041 /* not enough entries */
1042 if ((max - start) < count) {
1043 log_debug(1 << CXGBI_DBG_DDP,
1044 "NOT enough entries %u+%u < %u.\n", start, count, max);
1049 spin_lock(&ddp->map_lock);
1050 for (i = start; i < max;) {
1051 for (j = 0, k = i; j < count; j++, k++) {
1056 for (j = 0, k = i; j < count; j++, k++)
1057 ddp->gl_map[k] = gl;
1058 spin_unlock(&ddp->map_lock);
1063 spin_unlock(&ddp->map_lock);
1064 log_debug(1 << CXGBI_DBG_DDP,
1065 "NO suitable entries %u available.\n", count);
1069 static inline void ddp_unmark_entries(struct cxgbi_ddp_info *ddp,
1070 int start, int count)
1072 spin_lock(&ddp->map_lock);
1073 memset(&ddp->gl_map[start], 0,
1074 count * sizeof(struct cxgbi_gather_list *));
1075 spin_unlock(&ddp->map_lock);
1078 static inline void ddp_gl_unmap(struct pci_dev *pdev,
1079 struct cxgbi_gather_list *gl)
1083 for (i = 0; i < gl->nelem; i++)
1084 dma_unmap_page(&pdev->dev, gl->phys_addr[i], PAGE_SIZE,
1085 PCI_DMA_FROMDEVICE);
1088 static inline int ddp_gl_map(struct pci_dev *pdev,
1089 struct cxgbi_gather_list *gl)
1093 for (i = 0; i < gl->nelem; i++) {
1094 gl->phys_addr[i] = dma_map_page(&pdev->dev, gl->pages[i], 0,
1096 PCI_DMA_FROMDEVICE);
1097 if (unlikely(dma_mapping_error(&pdev->dev, gl->phys_addr[i]))) {
1098 log_debug(1 << CXGBI_DBG_DDP,
1099 "page %d 0x%p, 0x%p dma mapping err.\n",
1100 i, gl->pages[i], pdev);
1107 unsigned int nelem = gl->nelem;
1110 ddp_gl_unmap(pdev, gl);
1116 static void ddp_release_gl(struct cxgbi_gather_list *gl,
1117 struct pci_dev *pdev)
1119 ddp_gl_unmap(pdev, gl);
1123 static struct cxgbi_gather_list *ddp_make_gl(unsigned int xferlen,
1124 struct scatterlist *sgl,
1126 struct pci_dev *pdev,
1129 struct cxgbi_gather_list *gl;
1130 struct scatterlist *sg = sgl;
1131 struct page *sgpage = sg_page(sg);
1132 unsigned int sglen = sg->length;
1133 unsigned int sgoffset = sg->offset;
1134 unsigned int npages = (xferlen + sgoffset + PAGE_SIZE - 1) >>
1138 if (xferlen < DDP_THRESHOLD) {
1139 log_debug(1 << CXGBI_DBG_DDP,
1140 "xfer %u < threshold %u, no ddp.\n",
1141 xferlen, DDP_THRESHOLD);
1145 gl = kzalloc(sizeof(struct cxgbi_gather_list) +
1146 npages * (sizeof(dma_addr_t) +
1147 sizeof(struct page *)), gfp);
1149 log_debug(1 << CXGBI_DBG_DDP,
1150 "xfer %u, %u pages, OOM.\n", xferlen, npages);
1154 log_debug(1 << CXGBI_DBG_DDP,
1155 "xfer %u, sgl %u, gl max %u.\n", xferlen, sgcnt, npages);
1157 gl->pages = (struct page **)&gl->phys_addr[npages];
1159 gl->length = xferlen;
1160 gl->offset = sgoffset;
1161 gl->pages[0] = sgpage;
1163 for (i = 1, sg = sg_next(sgl), j = 0; i < sgcnt;
1164 i++, sg = sg_next(sg)) {
1165 struct page *page = sg_page(sg);
1167 if (sgpage == page && sg->offset == sgoffset + sglen)
1168 sglen += sg->length;
1170 /* make sure the sgl is fit for ddp:
1171 * each has the same page size, and
1172 * all of the middle pages are used completely
1174 if ((j && sgoffset) || ((i != sgcnt - 1) &&
1175 ((sglen + sgoffset) & ~PAGE_MASK))) {
1176 log_debug(1 << CXGBI_DBG_DDP,
1177 "page %d/%u, %u + %u.\n",
1178 i, sgcnt, sgoffset, sglen);
1183 if (j == gl->nelem || sg->offset) {
1184 log_debug(1 << CXGBI_DBG_DDP,
1185 "page %d/%u, offset %u.\n",
1186 j, gl->nelem, sg->offset);
1189 gl->pages[j] = page;
1191 sgoffset = sg->offset;
1197 if (ddp_gl_map(pdev, gl) < 0)
1207 static void ddp_tag_release(struct cxgbi_hba *chba, u32 tag)
1209 struct cxgbi_device *cdev = chba->cdev;
1210 struct cxgbi_ddp_info *ddp = cdev->ddp;
1213 idx = (tag >> PPOD_IDX_SHIFT) & ddp->idx_mask;
1214 if (idx < ddp->nppods) {
1215 struct cxgbi_gather_list *gl = ddp->gl_map[idx];
1218 if (!gl || !gl->nelem) {
1219 pr_warn("tag 0x%x, idx %u, gl 0x%p, %u.\n",
1220 tag, idx, gl, gl ? gl->nelem : 0);
1223 npods = (gl->nelem + PPOD_PAGES_MAX - 1) >> PPOD_PAGES_SHIFT;
1224 log_debug(1 << CXGBI_DBG_DDP,
1225 "tag 0x%x, release idx %u, npods %u.\n",
1227 cdev->csk_ddp_clear(chba, tag, idx, npods);
1228 ddp_unmark_entries(ddp, idx, npods);
1229 ddp_release_gl(gl, ddp->pdev);
1231 pr_warn("tag 0x%x, idx %u > max %u.\n", tag, idx, ddp->nppods);
1234 static int ddp_tag_reserve(struct cxgbi_sock *csk, unsigned int tid,
1235 u32 sw_tag, u32 *tagp, struct cxgbi_gather_list *gl,
1238 struct cxgbi_device *cdev = csk->cdev;
1239 struct cxgbi_ddp_info *ddp = cdev->ddp;
1240 struct cxgbi_tag_format *tformat = &cdev->tag_format;
1241 struct cxgbi_pagepod_hdr hdr;
1247 npods = (gl->nelem + PPOD_PAGES_MAX - 1) >> PPOD_PAGES_SHIFT;
1248 if (ddp->idx_last == ddp->nppods)
1249 idx = ddp_find_unused_entries(ddp, 0, ddp->nppods,
1252 idx = ddp_find_unused_entries(ddp, ddp->idx_last + 1,
1255 if (idx < 0 && ddp->idx_last >= npods) {
1256 idx = ddp_find_unused_entries(ddp, 0,
1257 min(ddp->idx_last + npods, ddp->nppods),
1262 log_debug(1 << CXGBI_DBG_DDP,
1263 "xferlen %u, gl %u, npods %u NO DDP.\n",
1264 gl->length, gl->nelem, npods);
1268 tag = cxgbi_ddp_tag_base(tformat, sw_tag);
1269 tag |= idx << PPOD_IDX_SHIFT;
1272 hdr.vld_tid = htonl(PPOD_VALID_FLAG | PPOD_TID(tid));
1273 hdr.pgsz_tag_clr = htonl(tag & ddp->rsvd_tag_mask);
1274 hdr.max_offset = htonl(gl->length);
1275 hdr.page_offset = htonl(gl->offset);
1277 err = cdev->csk_ddp_set(csk, &hdr, idx, npods, gl);
1279 goto unmark_entries;
1281 ddp->idx_last = idx;
1282 log_debug(1 << CXGBI_DBG_DDP,
1283 "xfer %u, gl %u,%u, tid 0x%x, tag 0x%x->0x%x(%u,%u).\n",
1284 gl->length, gl->nelem, gl->offset, tid, sw_tag, tag, idx,
1290 ddp_unmark_entries(ddp, idx, npods);
1294 int cxgbi_ddp_reserve(struct cxgbi_sock *csk, unsigned int *tagp,
1295 unsigned int sw_tag, unsigned int xferlen,
1296 struct scatterlist *sgl, unsigned int sgcnt, gfp_t gfp)
1298 struct cxgbi_device *cdev = csk->cdev;
1299 struct cxgbi_tag_format *tformat = &cdev->tag_format;
1300 struct cxgbi_gather_list *gl;
1303 if (page_idx >= DDP_PGIDX_MAX || !cdev->ddp ||
1304 xferlen < DDP_THRESHOLD) {
1305 log_debug(1 << CXGBI_DBG_DDP,
1306 "pgidx %u, xfer %u, NO ddp.\n", page_idx, xferlen);
1310 if (!cxgbi_sw_tag_usable(tformat, sw_tag)) {
1311 log_debug(1 << CXGBI_DBG_DDP,
1312 "sw_tag 0x%x NOT usable.\n", sw_tag);
1316 gl = ddp_make_gl(xferlen, sgl, sgcnt, cdev->pdev, gfp);
1320 err = ddp_tag_reserve(csk, csk->tid, sw_tag, tagp, gl, gfp);
1322 ddp_release_gl(gl, cdev->pdev);
1327 static void ddp_destroy(struct kref *kref)
1329 struct cxgbi_ddp_info *ddp = container_of(kref,
1330 struct cxgbi_ddp_info,
1332 struct cxgbi_device *cdev = ddp->cdev;
1335 pr_info("kref 0, destroy ddp 0x%p, cdev 0x%p.\n", ddp, cdev);
1337 while (i < ddp->nppods) {
1338 struct cxgbi_gather_list *gl = ddp->gl_map[i];
1341 int npods = (gl->nelem + PPOD_PAGES_MAX - 1)
1342 >> PPOD_PAGES_SHIFT;
1343 pr_info("cdev 0x%p, ddp %d + %d.\n", cdev, i, npods);
1349 cxgbi_free_big_mem(ddp);
1352 int cxgbi_ddp_cleanup(struct cxgbi_device *cdev)
1354 struct cxgbi_ddp_info *ddp = cdev->ddp;
1356 log_debug(1 << CXGBI_DBG_DDP,
1357 "cdev 0x%p, release ddp 0x%p.\n", cdev, ddp);
1360 return kref_put(&ddp->refcnt, ddp_destroy);
1363 EXPORT_SYMBOL_GPL(cxgbi_ddp_cleanup);
1365 int cxgbi_ddp_init(struct cxgbi_device *cdev,
1366 unsigned int llimit, unsigned int ulimit,
1367 unsigned int max_txsz, unsigned int max_rxsz)
1369 struct cxgbi_ddp_info *ddp;
1370 unsigned int ppmax, bits;
1372 ppmax = (ulimit - llimit + 1) >> PPOD_SIZE_SHIFT;
1373 bits = __ilog2_u32(ppmax) + 1;
1374 if (bits > PPOD_IDX_MAX_SIZE)
1375 bits = PPOD_IDX_MAX_SIZE;
1376 ppmax = (1 << (bits - 1)) - 1;
1378 ddp = cxgbi_alloc_big_mem(sizeof(struct cxgbi_ddp_info) +
1379 ppmax * (sizeof(struct cxgbi_gather_list *) +
1380 sizeof(struct sk_buff *)),
1383 pr_warn("cdev 0x%p, ddp ppmax %u OOM.\n", cdev, ppmax);
1386 ddp->gl_map = (struct cxgbi_gather_list **)(ddp + 1);
1389 spin_lock_init(&ddp->map_lock);
1390 kref_init(&ddp->refcnt);
1393 ddp->pdev = cdev->pdev;
1394 ddp->llimit = llimit;
1395 ddp->ulimit = ulimit;
1396 ddp->max_txsz = min_t(unsigned int, max_txsz, ULP2_MAX_PKT_SIZE);
1397 ddp->max_rxsz = min_t(unsigned int, max_rxsz, ULP2_MAX_PKT_SIZE);
1398 ddp->nppods = ppmax;
1399 ddp->idx_last = ppmax;
1400 ddp->idx_bits = bits;
1401 ddp->idx_mask = (1 << bits) - 1;
1402 ddp->rsvd_tag_mask = (1 << (bits + PPOD_IDX_SHIFT)) - 1;
1404 cdev->tag_format.sw_bits = sw_tag_idx_bits + sw_tag_age_bits;
1405 cdev->tag_format.rsvd_bits = ddp->idx_bits;
1406 cdev->tag_format.rsvd_shift = PPOD_IDX_SHIFT;
1407 cdev->tag_format.rsvd_mask = (1 << cdev->tag_format.rsvd_bits) - 1;
1409 pr_info("%s tag format, sw %u, rsvd %u,%u, mask 0x%x.\n",
1410 cdev->ports[0]->name, cdev->tag_format.sw_bits,
1411 cdev->tag_format.rsvd_bits, cdev->tag_format.rsvd_shift,
1412 cdev->tag_format.rsvd_mask);
1414 cdev->tx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
1415 ddp->max_txsz - ISCSI_PDU_NONPAYLOAD_LEN);
1416 cdev->rx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
1417 ddp->max_rxsz - ISCSI_PDU_NONPAYLOAD_LEN);
1419 log_debug(1 << CXGBI_DBG_DDP,
1420 "%s max payload size: %u/%u, %u/%u.\n",
1421 cdev->ports[0]->name, cdev->tx_max_size, ddp->max_txsz,
1422 cdev->rx_max_size, ddp->max_rxsz);
1425 EXPORT_SYMBOL_GPL(cxgbi_ddp_init);
1428 * APIs interacting with open-iscsi libraries
1431 static unsigned char padding[4];
1433 static void task_release_itt(struct iscsi_task *task, itt_t hdr_itt)
1435 struct scsi_cmnd *sc = task->sc;
1436 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
1437 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1438 struct cxgbi_hba *chba = cconn->chba;
1439 struct cxgbi_tag_format *tformat = &chba->cdev->tag_format;
1440 u32 tag = ntohl((__force u32)hdr_itt);
1442 log_debug(1 << CXGBI_DBG_DDP,
1443 "cdev 0x%p, release tag 0x%x.\n", chba->cdev, tag);
1445 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
1446 cxgbi_is_ddp_tag(tformat, tag))
1447 ddp_tag_release(chba, tag);
1450 static int task_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
1452 struct scsi_cmnd *sc = task->sc;
1453 struct iscsi_conn *conn = task->conn;
1454 struct iscsi_session *sess = conn->session;
1455 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1456 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1457 struct cxgbi_hba *chba = cconn->chba;
1458 struct cxgbi_tag_format *tformat = &chba->cdev->tag_format;
1459 u32 sw_tag = (sess->age << cconn->task_idx_bits) | task->itt;
1464 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE)) {
1465 err = cxgbi_ddp_reserve(cconn->cep->csk, &tag, sw_tag,
1466 scsi_in(sc)->length,
1467 scsi_in(sc)->table.sgl,
1468 scsi_in(sc)->table.nents,
1471 log_debug(1 << CXGBI_DBG_DDP,
1472 "csk 0x%p, R task 0x%p, %u,%u, no ddp.\n",
1473 cconn->cep->csk, task, scsi_in(sc)->length,
1474 scsi_in(sc)->table.nents);
1478 tag = cxgbi_set_non_ddp_tag(tformat, sw_tag);
1479 /* the itt need to sent in big-endian order */
1480 *hdr_itt = (__force itt_t)htonl(tag);
1482 log_debug(1 << CXGBI_DBG_DDP,
1483 "cdev 0x%p, task 0x%p, 0x%x(0x%x,0x%x)->0x%x/0x%x.\n",
1484 chba->cdev, task, sw_tag, task->itt, sess->age, tag, *hdr_itt);
1488 void cxgbi_parse_pdu_itt(struct iscsi_conn *conn, itt_t itt, int *idx, int *age)
1490 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1491 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1492 struct cxgbi_device *cdev = cconn->chba->cdev;
1493 u32 tag = ntohl((__force u32) itt);
1496 sw_bits = cxgbi_tag_nonrsvd_bits(&cdev->tag_format, tag);
1498 *idx = sw_bits & ((1 << cconn->task_idx_bits) - 1);
1500 *age = (sw_bits >> cconn->task_idx_bits) & ISCSI_AGE_MASK;
1502 log_debug(1 << CXGBI_DBG_DDP,
1503 "cdev 0x%p, tag 0x%x/0x%x, -> 0x%x(0x%x,0x%x).\n",
1504 cdev, tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
1507 EXPORT_SYMBOL_GPL(cxgbi_parse_pdu_itt);
1509 void cxgbi_conn_tx_open(struct cxgbi_sock *csk)
1511 struct iscsi_conn *conn = csk->user_data;
1514 log_debug(1 << CXGBI_DBG_SOCK,
1515 "csk 0x%p, cid %d.\n", csk, conn->id);
1516 iscsi_conn_queue_work(conn);
1519 EXPORT_SYMBOL_GPL(cxgbi_conn_tx_open);
1522 * pdu receive, interact with libiscsi_tcp
1524 static inline int read_pdu_skb(struct iscsi_conn *conn,
1525 struct sk_buff *skb,
1526 unsigned int offset,
1532 bytes_read = iscsi_tcp_recv_skb(conn, skb, offset, offloaded, &status);
1534 case ISCSI_TCP_CONN_ERR:
1535 pr_info("skb 0x%p, off %u, %d, TCP_ERR.\n",
1536 skb, offset, offloaded);
1538 case ISCSI_TCP_SUSPENDED:
1539 log_debug(1 << CXGBI_DBG_PDU_RX,
1540 "skb 0x%p, off %u, %d, TCP_SUSPEND, rc %d.\n",
1541 skb, offset, offloaded, bytes_read);
1542 /* no transfer - just have caller flush queue */
1544 case ISCSI_TCP_SKB_DONE:
1545 pr_info("skb 0x%p, off %u, %d, TCP_SKB_DONE.\n",
1546 skb, offset, offloaded);
1548 * pdus should always fit in the skb and we should get
1549 * segment done notifcation.
1551 iscsi_conn_printk(KERN_ERR, conn, "Invalid pdu or skb.");
1553 case ISCSI_TCP_SEGMENT_DONE:
1554 log_debug(1 << CXGBI_DBG_PDU_RX,
1555 "skb 0x%p, off %u, %d, TCP_SEG_DONE, rc %d.\n",
1556 skb, offset, offloaded, bytes_read);
1559 pr_info("skb 0x%p, off %u, %d, invalid status %d.\n",
1560 skb, offset, offloaded, status);
1565 static int skb_read_pdu_bhs(struct iscsi_conn *conn, struct sk_buff *skb)
1567 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1569 log_debug(1 << CXGBI_DBG_PDU_RX,
1570 "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
1571 conn, skb, skb->len, cxgbi_skcb_flags(skb));
1573 if (!iscsi_tcp_recv_segment_is_hdr(tcp_conn)) {
1574 pr_info("conn 0x%p, skb 0x%p, not hdr.\n", conn, skb);
1575 iscsi_conn_failure(conn, ISCSI_ERR_PROTO);
1579 if (conn->hdrdgst_en &&
1580 cxgbi_skcb_test_flag(skb, SKCBF_RX_HCRC_ERR)) {
1581 pr_info("conn 0x%p, skb 0x%p, hcrc.\n", conn, skb);
1582 iscsi_conn_failure(conn, ISCSI_ERR_HDR_DGST);
1586 return read_pdu_skb(conn, skb, 0, 0);
1589 static int skb_read_pdu_data(struct iscsi_conn *conn, struct sk_buff *lskb,
1590 struct sk_buff *skb, unsigned int offset)
1592 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1594 int opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
1596 log_debug(1 << CXGBI_DBG_PDU_RX,
1597 "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
1598 conn, skb, skb->len, cxgbi_skcb_flags(skb));
1600 if (conn->datadgst_en &&
1601 cxgbi_skcb_test_flag(lskb, SKCBF_RX_DCRC_ERR)) {
1602 pr_info("conn 0x%p, skb 0x%p, dcrc 0x%lx.\n",
1603 conn, lskb, cxgbi_skcb_flags(lskb));
1604 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
1608 if (iscsi_tcp_recv_segment_is_hdr(tcp_conn))
1611 /* coalesced, add header digest length */
1612 if (lskb == skb && conn->hdrdgst_en)
1613 offset += ISCSI_DIGEST_SIZE;
1615 if (cxgbi_skcb_test_flag(lskb, SKCBF_RX_DATA_DDPD))
1618 if (opcode == ISCSI_OP_SCSI_DATA_IN)
1619 log_debug(1 << CXGBI_DBG_PDU_RX,
1620 "skb 0x%p, op 0x%x, itt 0x%x, %u %s ddp'ed.\n",
1621 skb, opcode, ntohl(tcp_conn->in.hdr->itt),
1622 tcp_conn->in.datalen, offloaded ? "is" : "not");
1624 return read_pdu_skb(conn, skb, offset, offloaded);
1627 static void csk_return_rx_credits(struct cxgbi_sock *csk, int copied)
1629 struct cxgbi_device *cdev = csk->cdev;
1633 log_debug(1 << CXGBI_DBG_PDU_RX,
1634 "csk 0x%p,%u,0x%lu,%u, seq %u, wup %u, thre %u, %u.\n",
1635 csk, csk->state, csk->flags, csk->tid, csk->copied_seq,
1636 csk->rcv_wup, cdev->rx_credit_thres,
1639 if (csk->state != CTP_ESTABLISHED)
1642 credits = csk->copied_seq - csk->rcv_wup;
1643 if (unlikely(!credits))
1645 if (unlikely(cdev->rx_credit_thres == 0))
1648 must_send = credits + 16384 >= cdev->rcv_win;
1649 if (must_send || credits >= cdev->rx_credit_thres)
1650 csk->rcv_wup += cdev->csk_send_rx_credits(csk, credits);
1653 void cxgbi_conn_pdu_ready(struct cxgbi_sock *csk)
1655 struct cxgbi_device *cdev = csk->cdev;
1656 struct iscsi_conn *conn = csk->user_data;
1657 struct sk_buff *skb;
1658 unsigned int read = 0;
1661 log_debug(1 << CXGBI_DBG_PDU_RX,
1662 "csk 0x%p, conn 0x%p.\n", csk, conn);
1664 if (unlikely(!conn || conn->suspend_rx)) {
1665 log_debug(1 << CXGBI_DBG_PDU_RX,
1666 "csk 0x%p, conn 0x%p, id %d, suspend_rx %lu!\n",
1667 csk, conn, conn ? conn->id : 0xFF,
1668 conn ? conn->suspend_rx : 0xFF);
1673 skb = skb_peek(&csk->receive_queue);
1675 !(cxgbi_skcb_test_flag(skb, SKCBF_RX_STATUS))) {
1677 log_debug(1 << CXGBI_DBG_PDU_RX,
1678 "skb 0x%p, NOT ready 0x%lx.\n",
1679 skb, cxgbi_skcb_flags(skb));
1682 __skb_unlink(skb, &csk->receive_queue);
1684 read += cxgbi_skcb_rx_pdulen(skb);
1685 log_debug(1 << CXGBI_DBG_PDU_RX,
1686 "csk 0x%p, skb 0x%p,%u,f 0x%lx, pdu len %u.\n",
1687 csk, skb, skb->len, cxgbi_skcb_flags(skb),
1688 cxgbi_skcb_rx_pdulen(skb));
1690 if (cxgbi_skcb_test_flag(skb, SKCBF_RX_COALESCED)) {
1691 err = skb_read_pdu_bhs(conn, skb);
1693 pr_err("coalesced bhs, csk 0x%p, skb 0x%p,%u, "
1694 "f 0x%lx, plen %u.\n",
1696 cxgbi_skcb_flags(skb),
1697 cxgbi_skcb_rx_pdulen(skb));
1700 err = skb_read_pdu_data(conn, skb, skb,
1701 err + cdev->skb_rx_extra);
1703 pr_err("coalesced data, csk 0x%p, skb 0x%p,%u, "
1704 "f 0x%lx, plen %u.\n",
1706 cxgbi_skcb_flags(skb),
1707 cxgbi_skcb_rx_pdulen(skb));
1709 err = skb_read_pdu_bhs(conn, skb);
1711 pr_err("bhs, csk 0x%p, skb 0x%p,%u, "
1712 "f 0x%lx, plen %u.\n",
1714 cxgbi_skcb_flags(skb),
1715 cxgbi_skcb_rx_pdulen(skb));
1719 if (cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA)) {
1720 struct sk_buff *dskb;
1722 dskb = skb_peek(&csk->receive_queue);
1724 pr_err("csk 0x%p, skb 0x%p,%u, f 0x%lx,"
1725 " plen %u, NO data.\n",
1727 cxgbi_skcb_flags(skb),
1728 cxgbi_skcb_rx_pdulen(skb));
1732 __skb_unlink(dskb, &csk->receive_queue);
1734 err = skb_read_pdu_data(conn, skb, dskb, 0);
1736 pr_err("data, csk 0x%p, skb 0x%p,%u, "
1737 "f 0x%lx, plen %u, dskb 0x%p,"
1740 cxgbi_skcb_flags(skb),
1741 cxgbi_skcb_rx_pdulen(skb),
1745 err = skb_read_pdu_data(conn, skb, skb, 0);
1754 log_debug(1 << CXGBI_DBG_PDU_RX, "csk 0x%p, read %u.\n", csk, read);
1756 csk->copied_seq += read;
1757 csk_return_rx_credits(csk, read);
1758 conn->rxdata_octets += read;
1762 pr_info("csk 0x%p, 0x%p, rx failed %d, read %u.\n",
1763 csk, conn, err, read);
1764 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1767 EXPORT_SYMBOL_GPL(cxgbi_conn_pdu_ready);
1769 static int sgl_seek_offset(struct scatterlist *sgl, unsigned int sgcnt,
1770 unsigned int offset, unsigned int *off,
1771 struct scatterlist **sgp)
1774 struct scatterlist *sg;
1776 for_each_sg(sgl, sg, sgcnt, i) {
1777 if (offset < sg->length) {
1782 offset -= sg->length;
1787 static int sgl_read_to_frags(struct scatterlist *sg, unsigned int sgoffset,
1788 unsigned int dlen, skb_frag_t *frags,
1791 unsigned int datalen = dlen;
1792 unsigned int sglen = sg->length - sgoffset;
1793 struct page *page = sg_page(sg);
1803 pr_warn("sg %d NULL, len %u/%u.\n",
1812 copy = min(datalen, sglen);
1813 if (i && page == frags[i - 1].page &&
1814 sgoffset + sg->offset ==
1815 frags[i - 1].page_offset + frags[i - 1].size) {
1816 frags[i - 1].size += copy;
1818 if (i >= frag_max) {
1819 pr_warn("too many pages %u, dlen %u.\n",
1824 frags[i].page = page;
1825 frags[i].page_offset = sg->offset + sgoffset;
1826 frags[i].size = copy;
1837 int cxgbi_conn_alloc_pdu(struct iscsi_task *task, u8 opcode)
1839 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
1840 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1841 struct cxgbi_device *cdev = cconn->chba->cdev;
1842 struct iscsi_conn *conn = task->conn;
1843 struct iscsi_tcp_task *tcp_task = task->dd_data;
1844 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1845 struct scsi_cmnd *sc = task->sc;
1846 int headroom = SKB_TX_ISCSI_PDU_HEADER_MAX;
1848 tcp_task->dd_data = tdata;
1851 if (SKB_MAX_HEAD(cdev->skb_tx_rsvd) > (512 * MAX_SKB_FRAGS) &&
1852 (opcode == ISCSI_OP_SCSI_DATA_OUT ||
1853 (opcode == ISCSI_OP_SCSI_CMD &&
1854 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_TO_DEVICE))))
1855 /* data could goes into skb head */
1856 headroom += min_t(unsigned int,
1857 SKB_MAX_HEAD(cdev->skb_tx_rsvd),
1858 conn->max_xmit_dlength);
1860 tdata->skb = alloc_skb(cdev->skb_tx_rsvd + headroom, GFP_ATOMIC);
1862 pr_warn("alloc skb %u+%u, opcode 0x%x failed.\n",
1863 cdev->skb_tx_rsvd, headroom, opcode);
1867 skb_reserve(tdata->skb, cdev->skb_tx_rsvd);
1868 task->hdr = (struct iscsi_hdr *)tdata->skb->data;
1869 task->hdr_max = SKB_TX_ISCSI_PDU_HEADER_MAX; /* BHS + AHS */
1871 /* data_out uses scsi_cmd's itt */
1872 if (opcode != ISCSI_OP_SCSI_DATA_OUT)
1873 task_reserve_itt(task, &task->hdr->itt);
1875 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1876 "task 0x%p, op 0x%x, skb 0x%p,%u+%u/%u, itt 0x%x.\n",
1877 task, opcode, tdata->skb, cdev->skb_tx_rsvd, headroom,
1878 conn->max_xmit_dlength, ntohl(task->hdr->itt));
1882 EXPORT_SYMBOL_GPL(cxgbi_conn_alloc_pdu);
1884 static inline void tx_skb_setmode(struct sk_buff *skb, int hcrc, int dcrc)
1893 cxgbi_skcb_ulp_mode(skb) = (ULP2_MODE_ISCSI << 4) | submode;
1895 cxgbi_skcb_ulp_mode(skb) = 0;
1898 int cxgbi_conn_init_pdu(struct iscsi_task *task, unsigned int offset,
1901 struct iscsi_conn *conn = task->conn;
1902 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1903 struct sk_buff *skb = tdata->skb;
1904 unsigned int datalen = count;
1905 int i, padlen = iscsi_padding(count);
1908 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1909 "task 0x%p,0x%p, skb 0x%p, 0x%x,0x%x,0x%x, %u+%u.\n",
1910 task, task->sc, skb, (*skb->data) & ISCSI_OPCODE_MASK,
1911 ntohl(task->cmdsn), ntohl(task->hdr->itt), offset, count);
1913 skb_put(skb, task->hdr_len);
1914 tx_skb_setmode(skb, conn->hdrdgst_en, datalen ? conn->datadgst_en : 0);
1919 struct scsi_data_buffer *sdb = scsi_out(task->sc);
1920 struct scatterlist *sg = NULL;
1923 tdata->offset = offset;
1924 tdata->count = count;
1925 err = sgl_seek_offset(
1926 sdb->table.sgl, sdb->table.nents,
1927 tdata->offset, &tdata->sgoffset, &sg);
1929 pr_warn("tpdu, sgl %u, bad offset %u/%u.\n",
1930 sdb->table.nents, tdata->offset, sdb->length);
1933 err = sgl_read_to_frags(sg, tdata->sgoffset, tdata->count,
1934 tdata->frags, MAX_PDU_FRAGS);
1936 pr_warn("tpdu, sgl %u, bad offset %u + %u.\n",
1937 sdb->table.nents, tdata->offset, tdata->count);
1940 tdata->nr_frags = err;
1942 if (tdata->nr_frags > MAX_SKB_FRAGS ||
1943 (padlen && tdata->nr_frags == MAX_SKB_FRAGS)) {
1944 char *dst = skb->data + task->hdr_len;
1945 skb_frag_t *frag = tdata->frags;
1947 /* data fits in the skb's headroom */
1948 for (i = 0; i < tdata->nr_frags; i++, frag++) {
1949 char *src = kmap_atomic(frag->page,
1952 memcpy(dst, src+frag->page_offset, frag->size);
1954 kunmap_atomic(src, KM_SOFTIRQ0);
1957 memset(dst, 0, padlen);
1960 skb_put(skb, count + padlen);
1962 /* data fit into frag_list */
1963 for (i = 0; i < tdata->nr_frags; i++)
1964 get_page(tdata->frags[i].page);
1966 memcpy(skb_shinfo(skb)->frags, tdata->frags,
1967 sizeof(skb_frag_t) * tdata->nr_frags);
1968 skb_shinfo(skb)->nr_frags = tdata->nr_frags;
1970 skb->data_len += count;
1971 skb->truesize += count;
1975 pg = virt_to_page(task->data);
1978 skb_fill_page_desc(skb, 0, pg, offset_in_page(task->data),
1981 skb->data_len += count;
1982 skb->truesize += count;
1986 i = skb_shinfo(skb)->nr_frags;
1987 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
1988 virt_to_page(padding), offset_in_page(padding),
1991 skb->data_len += padlen;
1992 skb->truesize += padlen;
1998 EXPORT_SYMBOL_GPL(cxgbi_conn_init_pdu);
2000 int cxgbi_conn_xmit_pdu(struct iscsi_task *task)
2002 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
2003 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2004 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
2005 struct sk_buff *skb = tdata->skb;
2006 unsigned int datalen;
2010 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2011 "task 0x%p, skb NULL.\n", task);
2015 datalen = skb->data_len;
2017 err = cxgbi_sock_send_pdus(cconn->cep->csk, skb);
2021 log_debug(1 << CXGBI_DBG_PDU_TX,
2022 "task 0x%p,0x%p, skb 0x%p, len %u/%u, rv %d.\n",
2023 task, task->sc, skb, skb->len, skb->data_len, err);
2025 if (task->conn->hdrdgst_en)
2026 pdulen += ISCSI_DIGEST_SIZE;
2028 if (datalen && task->conn->datadgst_en)
2029 pdulen += ISCSI_DIGEST_SIZE;
2031 task->conn->txdata_octets += pdulen;
2035 if (err == -EAGAIN || err == -ENOBUFS) {
2036 log_debug(1 << CXGBI_DBG_PDU_TX,
2037 "task 0x%p, skb 0x%p, len %u/%u, %d EAGAIN.\n",
2038 task, skb, skb->len, skb->data_len, err);
2039 /* reset skb to send when we are called again */
2045 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2046 "itt 0x%x, skb 0x%p, len %u/%u, xmit err %d.\n",
2047 task->itt, skb, skb->len, skb->data_len, err);
2048 iscsi_conn_printk(KERN_ERR, task->conn, "xmit err %d.\n", err);
2049 iscsi_conn_failure(task->conn, ISCSI_ERR_XMIT_FAILED);
2052 EXPORT_SYMBOL_GPL(cxgbi_conn_xmit_pdu);
2054 void cxgbi_cleanup_task(struct iscsi_task *task)
2056 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
2058 log_debug(1 << CXGBI_DBG_ISCSI,
2059 "task 0x%p, skb 0x%p, itt 0x%x.\n",
2060 task, tdata->skb, task->hdr_itt);
2062 /* never reached the xmit task callout */
2064 __kfree_skb(tdata->skb);
2065 memset(tdata, 0, sizeof(*tdata));
2067 task_release_itt(task, task->hdr_itt);
2068 iscsi_tcp_cleanup_task(task);
2070 EXPORT_SYMBOL_GPL(cxgbi_cleanup_task);
2072 void cxgbi_get_conn_stats(struct iscsi_cls_conn *cls_conn,
2073 struct iscsi_stats *stats)
2075 struct iscsi_conn *conn = cls_conn->dd_data;
2077 stats->txdata_octets = conn->txdata_octets;
2078 stats->rxdata_octets = conn->rxdata_octets;
2079 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2080 stats->dataout_pdus = conn->dataout_pdus_cnt;
2081 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2082 stats->datain_pdus = conn->datain_pdus_cnt;
2083 stats->r2t_pdus = conn->r2t_pdus_cnt;
2084 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2085 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2086 stats->digest_err = 0;
2087 stats->timeout_err = 0;
2088 stats->custom_length = 1;
2089 strcpy(stats->custom[0].desc, "eh_abort_cnt");
2090 stats->custom[0].value = conn->eh_abort_cnt;
2092 EXPORT_SYMBOL_GPL(cxgbi_get_conn_stats);
2094 static int cxgbi_conn_max_xmit_dlength(struct iscsi_conn *conn)
2096 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2097 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2098 struct cxgbi_device *cdev = cconn->chba->cdev;
2099 unsigned int headroom = SKB_MAX_HEAD(cdev->skb_tx_rsvd);
2100 unsigned int max_def = 512 * MAX_SKB_FRAGS;
2101 unsigned int max = max(max_def, headroom);
2103 max = min(cconn->chba->cdev->tx_max_size, max);
2104 if (conn->max_xmit_dlength)
2105 conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
2107 conn->max_xmit_dlength = max;
2108 cxgbi_align_pdu_size(conn->max_xmit_dlength);
2113 static int cxgbi_conn_max_recv_dlength(struct iscsi_conn *conn)
2115 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2116 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2117 unsigned int max = cconn->chba->cdev->rx_max_size;
2119 cxgbi_align_pdu_size(max);
2121 if (conn->max_recv_dlength) {
2122 if (conn->max_recv_dlength > max) {
2123 pr_err("MaxRecvDataSegmentLength %u > %u.\n",
2124 conn->max_recv_dlength, max);
2127 conn->max_recv_dlength = min(conn->max_recv_dlength, max);
2128 cxgbi_align_pdu_size(conn->max_recv_dlength);
2130 conn->max_recv_dlength = max;
2135 int cxgbi_set_conn_param(struct iscsi_cls_conn *cls_conn,
2136 enum iscsi_param param, char *buf, int buflen)
2138 struct iscsi_conn *conn = cls_conn->dd_data;
2139 struct iscsi_session *session = conn->session;
2140 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2141 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2142 struct cxgbi_sock *csk = cconn->cep->csk;
2145 log_debug(1 << CXGBI_DBG_ISCSI,
2146 "cls_conn 0x%p, param %d, buf(%d) %s.\n",
2147 cls_conn, param, buflen, buf);
2150 case ISCSI_PARAM_HDRDGST_EN:
2151 err = iscsi_set_param(cls_conn, param, buf, buflen);
2152 if (!err && conn->hdrdgst_en)
2153 err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
2155 conn->datadgst_en, 0);
2157 case ISCSI_PARAM_DATADGST_EN:
2158 err = iscsi_set_param(cls_conn, param, buf, buflen);
2159 if (!err && conn->datadgst_en)
2160 err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
2162 conn->datadgst_en, 0);
2164 case ISCSI_PARAM_MAX_R2T:
2165 sscanf(buf, "%d", &value);
2166 if (value <= 0 || !is_power_of_2(value))
2168 if (session->max_r2t == value)
2170 iscsi_tcp_r2tpool_free(session);
2171 err = iscsi_set_param(cls_conn, param, buf, buflen);
2172 if (!err && iscsi_tcp_r2tpool_alloc(session))
2174 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2175 err = iscsi_set_param(cls_conn, param, buf, buflen);
2177 err = cxgbi_conn_max_recv_dlength(conn);
2179 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2180 err = iscsi_set_param(cls_conn, param, buf, buflen);
2182 err = cxgbi_conn_max_xmit_dlength(conn);
2185 return iscsi_set_param(cls_conn, param, buf, buflen);
2189 EXPORT_SYMBOL_GPL(cxgbi_set_conn_param);
2191 int cxgbi_get_ep_param(struct iscsi_endpoint *ep, enum iscsi_param param,
2194 struct cxgbi_endpoint *cep = ep->dd_data;
2195 struct cxgbi_sock *csk;
2198 log_debug(1 << CXGBI_DBG_ISCSI,
2199 "cls_conn 0x%p, param %d.\n", ep, param);
2202 case ISCSI_PARAM_CONN_PORT:
2203 case ISCSI_PARAM_CONN_ADDRESS:
2211 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
2212 &csk->daddr, param, buf);
2218 EXPORT_SYMBOL_GPL(cxgbi_get_ep_param);
2220 struct iscsi_cls_conn *
2221 cxgbi_create_conn(struct iscsi_cls_session *cls_session, u32 cid)
2223 struct iscsi_cls_conn *cls_conn;
2224 struct iscsi_conn *conn;
2225 struct iscsi_tcp_conn *tcp_conn;
2226 struct cxgbi_conn *cconn;
2228 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
2232 conn = cls_conn->dd_data;
2233 tcp_conn = conn->dd_data;
2234 cconn = tcp_conn->dd_data;
2235 cconn->iconn = conn;
2237 log_debug(1 << CXGBI_DBG_ISCSI,
2238 "cid %u(0x%x), cls 0x%p,0x%p, conn 0x%p,0x%p,0x%p.\n",
2239 cid, cid, cls_session, cls_conn, conn, tcp_conn, cconn);
2243 EXPORT_SYMBOL_GPL(cxgbi_create_conn);
2245 int cxgbi_bind_conn(struct iscsi_cls_session *cls_session,
2246 struct iscsi_cls_conn *cls_conn,
2247 u64 transport_eph, int is_leading)
2249 struct iscsi_conn *conn = cls_conn->dd_data;
2250 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2251 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2252 struct iscsi_endpoint *ep;
2253 struct cxgbi_endpoint *cep;
2254 struct cxgbi_sock *csk;
2257 ep = iscsi_lookup_endpoint(transport_eph);
2261 /* setup ddp pagesize */
2264 err = csk->cdev->csk_ddp_setup_pgidx(csk, csk->tid, page_idx, 0);
2268 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
2272 /* calculate the tag idx bits needed for this conn based on cmds_max */
2273 cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
2275 write_lock_bh(&csk->callback_lock);
2276 csk->user_data = conn;
2277 cconn->chba = cep->chba;
2280 write_unlock_bh(&csk->callback_lock);
2282 cxgbi_conn_max_xmit_dlength(conn);
2283 cxgbi_conn_max_recv_dlength(conn);
2285 log_debug(1 << CXGBI_DBG_ISCSI,
2286 "cls 0x%p,0x%p, ep 0x%p, cconn 0x%p, csk 0x%p.\n",
2287 cls_session, cls_conn, ep, cconn, csk);
2288 /* init recv engine */
2289 iscsi_tcp_hdr_recv_prep(tcp_conn);
2293 EXPORT_SYMBOL_GPL(cxgbi_bind_conn);
2295 struct iscsi_cls_session *cxgbi_create_session(struct iscsi_endpoint *ep,
2296 u16 cmds_max, u16 qdepth,
2299 struct cxgbi_endpoint *cep;
2300 struct cxgbi_hba *chba;
2301 struct Scsi_Host *shost;
2302 struct iscsi_cls_session *cls_session;
2303 struct iscsi_session *session;
2306 pr_err("missing endpoint.\n");
2312 shost = chba->shost;
2314 BUG_ON(chba != iscsi_host_priv(shost));
2316 cls_session = iscsi_session_setup(chba->cdev->itp, shost,
2318 sizeof(struct iscsi_tcp_task) +
2319 sizeof(struct cxgbi_task_data),
2320 initial_cmdsn, ISCSI_MAX_TARGET);
2324 session = cls_session->dd_data;
2325 if (iscsi_tcp_r2tpool_alloc(session))
2326 goto remove_session;
2328 log_debug(1 << CXGBI_DBG_ISCSI,
2329 "ep 0x%p, cls sess 0x%p.\n", ep, cls_session);
2333 iscsi_session_teardown(cls_session);
2336 EXPORT_SYMBOL_GPL(cxgbi_create_session);
2338 void cxgbi_destroy_session(struct iscsi_cls_session *cls_session)
2340 log_debug(1 << CXGBI_DBG_ISCSI,
2341 "cls sess 0x%p.\n", cls_session);
2343 iscsi_tcp_r2tpool_free(cls_session->dd_data);
2344 iscsi_session_teardown(cls_session);
2346 EXPORT_SYMBOL_GPL(cxgbi_destroy_session);
2348 int cxgbi_set_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2349 char *buf, int buflen)
2351 struct cxgbi_hba *chba = iscsi_host_priv(shost);
2354 shost_printk(KERN_ERR, shost, "Could not get host param. "
2355 "netdev for host not set.\n");
2359 log_debug(1 << CXGBI_DBG_ISCSI,
2360 "shost 0x%p, hba 0x%p,%s, param %d, buf(%d) %s.\n",
2361 shost, chba, chba->ndev->name, param, buflen, buf);
2364 case ISCSI_HOST_PARAM_IPADDRESS:
2366 __be32 addr = in_aton(buf);
2367 log_debug(1 << CXGBI_DBG_ISCSI,
2368 "hba %s, req. ipv4 %pI4.\n", chba->ndev->name, &addr);
2369 cxgbi_set_iscsi_ipv4(chba, addr);
2372 case ISCSI_HOST_PARAM_HWADDRESS:
2373 case ISCSI_HOST_PARAM_NETDEV_NAME:
2376 return iscsi_host_set_param(shost, param, buf, buflen);
2379 EXPORT_SYMBOL_GPL(cxgbi_set_host_param);
2381 int cxgbi_get_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2384 struct cxgbi_hba *chba = iscsi_host_priv(shost);
2388 shost_printk(KERN_ERR, shost, "Could not get host param. "
2389 "netdev for host not set.\n");
2393 log_debug(1 << CXGBI_DBG_ISCSI,
2394 "shost 0x%p, hba 0x%p,%s, param %d.\n",
2395 shost, chba, chba->ndev->name, param);
2398 case ISCSI_HOST_PARAM_HWADDRESS:
2399 len = sysfs_format_mac(buf, chba->ndev->dev_addr, 6);
2401 case ISCSI_HOST_PARAM_NETDEV_NAME:
2402 len = sprintf(buf, "%s\n", chba->ndev->name);
2404 case ISCSI_HOST_PARAM_IPADDRESS:
2408 addr = cxgbi_get_iscsi_ipv4(chba);
2409 len = sprintf(buf, "%pI4", &addr);
2410 log_debug(1 << CXGBI_DBG_ISCSI,
2411 "hba %s, ipv4 %pI4.\n", chba->ndev->name, &addr);
2415 return iscsi_host_get_param(shost, param, buf);
2420 EXPORT_SYMBOL_GPL(cxgbi_get_host_param);
2422 struct iscsi_endpoint *cxgbi_ep_connect(struct Scsi_Host *shost,
2423 struct sockaddr *dst_addr,
2426 struct iscsi_endpoint *ep;
2427 struct cxgbi_endpoint *cep;
2428 struct cxgbi_hba *hba = NULL;
2429 struct cxgbi_sock *csk;
2432 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2433 "shost 0x%p, non_blocking %d, dst_addr 0x%p.\n",
2434 shost, non_blocking, dst_addr);
2437 hba = iscsi_host_priv(shost);
2439 pr_info("shost 0x%p, priv NULL.\n", shost);
2444 csk = cxgbi_check_route(dst_addr);
2446 return (struct iscsi_endpoint *)csk;
2447 cxgbi_sock_get(csk);
2450 hba = csk->cdev->hbas[csk->port_id];
2451 else if (hba != csk->cdev->hbas[csk->port_id]) {
2452 pr_info("Could not connect through requested host %u"
2453 "hba 0x%p != 0x%p (%u).\n",
2454 shost->host_no, hba,
2455 csk->cdev->hbas[csk->port_id], csk->port_id);
2460 err = sock_get_port(csk);
2464 cxgbi_sock_set_state(csk, CTP_CONNECTING);
2465 err = csk->cdev->csk_init_act_open(csk);
2469 if (cxgbi_sock_is_closing(csk)) {
2471 pr_info("csk 0x%p is closing.\n", csk);
2475 ep = iscsi_create_endpoint(sizeof(*cep));
2478 pr_info("iscsi alloc ep, OOM.\n");
2486 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2487 "ep 0x%p, cep 0x%p, csk 0x%p, hba 0x%p,%s.\n",
2488 ep, cep, csk, hba, hba->ndev->name);
2492 cxgbi_sock_put(csk);
2493 cxgbi_sock_closed(csk);
2495 return ERR_PTR(err);
2497 EXPORT_SYMBOL_GPL(cxgbi_ep_connect);
2499 int cxgbi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
2501 struct cxgbi_endpoint *cep = ep->dd_data;
2502 struct cxgbi_sock *csk = cep->csk;
2504 if (!cxgbi_sock_is_established(csk))
2508 EXPORT_SYMBOL_GPL(cxgbi_ep_poll);
2510 void cxgbi_ep_disconnect(struct iscsi_endpoint *ep)
2512 struct cxgbi_endpoint *cep = ep->dd_data;
2513 struct cxgbi_conn *cconn = cep->cconn;
2514 struct cxgbi_sock *csk = cep->csk;
2516 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2517 "ep 0x%p, cep 0x%p, cconn 0x%p, csk 0x%p,%u,0x%lx.\n",
2518 ep, cep, cconn, csk, csk->state, csk->flags);
2520 if (cconn && cconn->iconn) {
2521 iscsi_suspend_tx(cconn->iconn);
2522 write_lock_bh(&csk->callback_lock);
2523 cep->csk->user_data = NULL;
2525 write_unlock_bh(&csk->callback_lock);
2527 iscsi_destroy_endpoint(ep);
2529 if (likely(csk->state >= CTP_ESTABLISHED))
2530 need_active_close(csk);
2532 cxgbi_sock_closed(csk);
2534 cxgbi_sock_put(csk);
2536 EXPORT_SYMBOL_GPL(cxgbi_ep_disconnect);
2538 int cxgbi_iscsi_init(struct iscsi_transport *itp,
2539 struct scsi_transport_template **stt)
2541 *stt = iscsi_register_transport(itp);
2543 pr_err("unable to register %s transport 0x%p.\n",
2547 log_debug(1 << CXGBI_DBG_ISCSI,
2548 "%s, registered iscsi transport 0x%p.\n",
2552 EXPORT_SYMBOL_GPL(cxgbi_iscsi_init);
2554 void cxgbi_iscsi_cleanup(struct iscsi_transport *itp,
2555 struct scsi_transport_template **stt)
2558 log_debug(1 << CXGBI_DBG_ISCSI,
2559 "de-register transport 0x%p, %s, stt 0x%p.\n",
2560 itp, itp->name, *stt);
2562 iscsi_unregister_transport(itp);
2565 EXPORT_SYMBOL_GPL(cxgbi_iscsi_cleanup);
2567 static int __init libcxgbi_init_module(void)
2569 sw_tag_idx_bits = (__ilog2_u32(ISCSI_ITT_MASK)) + 1;
2570 sw_tag_age_bits = (__ilog2_u32(ISCSI_AGE_MASK)) + 1;
2572 pr_info("tag itt 0x%x, %u bits, age 0x%x, %u bits.\n",
2573 ISCSI_ITT_MASK, sw_tag_idx_bits,
2574 ISCSI_AGE_MASK, sw_tag_age_bits);
2576 ddp_setup_host_page_size();
2580 static void __exit libcxgbi_exit_module(void)
2582 cxgbi_device_unregister_all(0xFF);
2586 module_init(libcxgbi_init_module);
2587 module_exit(libcxgbi_exit_module);