spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / scsi / cxgbi / libcxgbi.c
blobd3ff9cd40234f5d5ea478020ecbf0a04bac1503e
1 /*
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>
25 #include <net/dst.h>
26 #include <net/route.h>
27 #include <linux/inetdevice.h> /* ip_dev_find */
28 #include <linux/module.h>
29 #include <net/tcp.h>
31 static unsigned int dbg_level;
33 #include "libcxgbi.h"
35 #define DRV_MODULE_NAME "libcxgbi"
36 #define DRV_MODULE_DESC "Chelsio iSCSI driver library"
37 #define DRV_MODULE_VERSION "0.9.0"
38 #define DRV_MODULE_RELDATE "Jun. 2010"
40 MODULE_AUTHOR("Chelsio Communications, Inc.");
41 MODULE_DESCRIPTION(DRV_MODULE_DESC);
42 MODULE_VERSION(DRV_MODULE_VERSION);
43 MODULE_LICENSE("GPL");
45 module_param(dbg_level, uint, 0644);
46 MODULE_PARM_DESC(dbg_level, "libiscsi debug level (default=0)");
50 * cxgbi device management
51 * maintains a list of the cxgbi devices
53 static LIST_HEAD(cdev_list);
54 static DEFINE_MUTEX(cdev_mutex);
56 int cxgbi_device_portmap_create(struct cxgbi_device *cdev, unsigned int base,
57 unsigned int max_conn)
59 struct cxgbi_ports_map *pmap = &cdev->pmap;
61 pmap->port_csk = cxgbi_alloc_big_mem(max_conn *
62 sizeof(struct cxgbi_sock *),
63 GFP_KERNEL);
64 if (!pmap->port_csk) {
65 pr_warn("cdev 0x%p, portmap OOM %u.\n", cdev, max_conn);
66 return -ENOMEM;
69 pmap->max_connect = max_conn;
70 pmap->sport_base = base;
71 spin_lock_init(&pmap->lock);
72 return 0;
74 EXPORT_SYMBOL_GPL(cxgbi_device_portmap_create);
76 void cxgbi_device_portmap_cleanup(struct cxgbi_device *cdev)
78 struct cxgbi_ports_map *pmap = &cdev->pmap;
79 struct cxgbi_sock *csk;
80 int i;
82 for (i = 0; i < pmap->max_connect; i++) {
83 if (pmap->port_csk[i]) {
84 csk = pmap->port_csk[i];
85 pmap->port_csk[i] = NULL;
86 log_debug(1 << CXGBI_DBG_SOCK,
87 "csk 0x%p, cdev 0x%p, offload down.\n",
88 csk, cdev);
89 spin_lock_bh(&csk->lock);
90 cxgbi_sock_set_flag(csk, CTPF_OFFLOAD_DOWN);
91 cxgbi_sock_closed(csk);
92 spin_unlock_bh(&csk->lock);
93 cxgbi_sock_put(csk);
97 EXPORT_SYMBOL_GPL(cxgbi_device_portmap_cleanup);
99 static inline void cxgbi_device_destroy(struct cxgbi_device *cdev)
101 log_debug(1 << CXGBI_DBG_DEV,
102 "cdev 0x%p, p# %u.\n", cdev, cdev->nports);
103 cxgbi_hbas_remove(cdev);
104 cxgbi_device_portmap_cleanup(cdev);
105 if (cdev->dev_ddp_cleanup)
106 cdev->dev_ddp_cleanup(cdev);
107 else
108 cxgbi_ddp_cleanup(cdev);
109 if (cdev->ddp)
110 cxgbi_ddp_cleanup(cdev);
111 if (cdev->pmap.max_connect)
112 cxgbi_free_big_mem(cdev->pmap.port_csk);
113 kfree(cdev);
116 struct cxgbi_device *cxgbi_device_register(unsigned int extra,
117 unsigned int nports)
119 struct cxgbi_device *cdev;
121 cdev = kzalloc(sizeof(*cdev) + extra + nports *
122 (sizeof(struct cxgbi_hba *) +
123 sizeof(struct net_device *)),
124 GFP_KERNEL);
125 if (!cdev) {
126 pr_warn("nport %d, OOM.\n", nports);
127 return NULL;
129 cdev->ports = (struct net_device **)(cdev + 1);
130 cdev->hbas = (struct cxgbi_hba **)(((char*)cdev->ports) + nports *
131 sizeof(struct net_device *));
132 if (extra)
133 cdev->dd_data = ((char *)cdev->hbas) +
134 nports * sizeof(struct cxgbi_hba *);
135 spin_lock_init(&cdev->pmap.lock);
137 mutex_lock(&cdev_mutex);
138 list_add_tail(&cdev->list_head, &cdev_list);
139 mutex_unlock(&cdev_mutex);
141 log_debug(1 << CXGBI_DBG_DEV,
142 "cdev 0x%p, p# %u.\n", cdev, nports);
143 return cdev;
145 EXPORT_SYMBOL_GPL(cxgbi_device_register);
147 void cxgbi_device_unregister(struct cxgbi_device *cdev)
149 log_debug(1 << CXGBI_DBG_DEV,
150 "cdev 0x%p, p# %u,%s.\n",
151 cdev, cdev->nports, cdev->nports ? cdev->ports[0]->name : "");
152 mutex_lock(&cdev_mutex);
153 list_del(&cdev->list_head);
154 mutex_unlock(&cdev_mutex);
155 cxgbi_device_destroy(cdev);
157 EXPORT_SYMBOL_GPL(cxgbi_device_unregister);
159 void cxgbi_device_unregister_all(unsigned int flag)
161 struct cxgbi_device *cdev, *tmp;
163 mutex_lock(&cdev_mutex);
164 list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
165 if ((cdev->flags & flag) == flag) {
166 log_debug(1 << CXGBI_DBG_DEV,
167 "cdev 0x%p, p# %u,%s.\n",
168 cdev, cdev->nports, cdev->nports ?
169 cdev->ports[0]->name : "");
170 list_del(&cdev->list_head);
171 cxgbi_device_destroy(cdev);
174 mutex_unlock(&cdev_mutex);
176 EXPORT_SYMBOL_GPL(cxgbi_device_unregister_all);
178 struct cxgbi_device *cxgbi_device_find_by_lldev(void *lldev)
180 struct cxgbi_device *cdev, *tmp;
182 mutex_lock(&cdev_mutex);
183 list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
184 if (cdev->lldev == lldev) {
185 mutex_unlock(&cdev_mutex);
186 return cdev;
189 mutex_unlock(&cdev_mutex);
190 log_debug(1 << CXGBI_DBG_DEV,
191 "lldev 0x%p, NO match found.\n", lldev);
192 return NULL;
194 EXPORT_SYMBOL_GPL(cxgbi_device_find_by_lldev);
196 static struct cxgbi_device *cxgbi_device_find_by_netdev(struct net_device *ndev,
197 int *port)
199 struct net_device *vdev = NULL;
200 struct cxgbi_device *cdev, *tmp;
201 int i;
203 if (ndev->priv_flags & IFF_802_1Q_VLAN) {
204 vdev = ndev;
205 ndev = vlan_dev_real_dev(ndev);
206 log_debug(1 << CXGBI_DBG_DEV,
207 "vlan dev %s -> %s.\n", vdev->name, ndev->name);
210 mutex_lock(&cdev_mutex);
211 list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
212 for (i = 0; i < cdev->nports; i++) {
213 if (ndev == cdev->ports[i]) {
214 cdev->hbas[i]->vdev = vdev;
215 mutex_unlock(&cdev_mutex);
216 if (port)
217 *port = i;
218 return cdev;
222 mutex_unlock(&cdev_mutex);
223 log_debug(1 << CXGBI_DBG_DEV,
224 "ndev 0x%p, %s, NO match found.\n", ndev, ndev->name);
225 return NULL;
228 void cxgbi_hbas_remove(struct cxgbi_device *cdev)
230 int i;
231 struct cxgbi_hba *chba;
233 log_debug(1 << CXGBI_DBG_DEV,
234 "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
236 for (i = 0; i < cdev->nports; i++) {
237 chba = cdev->hbas[i];
238 if (chba) {
239 cdev->hbas[i] = NULL;
240 iscsi_host_remove(chba->shost);
241 pci_dev_put(cdev->pdev);
242 iscsi_host_free(chba->shost);
246 EXPORT_SYMBOL_GPL(cxgbi_hbas_remove);
248 int cxgbi_hbas_add(struct cxgbi_device *cdev, unsigned int max_lun,
249 unsigned int max_id, struct scsi_host_template *sht,
250 struct scsi_transport_template *stt)
252 struct cxgbi_hba *chba;
253 struct Scsi_Host *shost;
254 int i, err;
256 log_debug(1 << CXGBI_DBG_DEV, "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
258 for (i = 0; i < cdev->nports; i++) {
259 shost = iscsi_host_alloc(sht, sizeof(*chba), 1);
260 if (!shost) {
261 pr_info("0x%p, p%d, %s, host alloc failed.\n",
262 cdev, i, cdev->ports[i]->name);
263 err = -ENOMEM;
264 goto err_out;
267 shost->transportt = stt;
268 shost->max_lun = max_lun;
269 shost->max_id = max_id;
270 shost->max_channel = 0;
271 shost->max_cmd_len = 16;
273 chba = iscsi_host_priv(shost);
274 chba->cdev = cdev;
275 chba->ndev = cdev->ports[i];
276 chba->shost = shost;
278 log_debug(1 << CXGBI_DBG_DEV,
279 "cdev 0x%p, p#%d %s: chba 0x%p.\n",
280 cdev, i, cdev->ports[i]->name, chba);
282 pci_dev_get(cdev->pdev);
283 err = iscsi_host_add(shost, &cdev->pdev->dev);
284 if (err) {
285 pr_info("cdev 0x%p, p#%d %s, host add failed.\n",
286 cdev, i, cdev->ports[i]->name);
287 pci_dev_put(cdev->pdev);
288 scsi_host_put(shost);
289 goto err_out;
292 cdev->hbas[i] = chba;
295 return 0;
297 err_out:
298 cxgbi_hbas_remove(cdev);
299 return err;
301 EXPORT_SYMBOL_GPL(cxgbi_hbas_add);
304 * iSCSI offload
306 * - source port management
307 * To find a free source port in the port allocation map we use a very simple
308 * rotor scheme to look for the next free port.
310 * If a source port has been specified make sure that it doesn't collide with
311 * our normal source port allocation map. If it's outside the range of our
312 * allocation/deallocation scheme just let them use it.
314 * If the source port is outside our allocation range, the caller is
315 * responsible for keeping track of their port usage.
317 static int sock_get_port(struct cxgbi_sock *csk)
319 struct cxgbi_device *cdev = csk->cdev;
320 struct cxgbi_ports_map *pmap = &cdev->pmap;
321 unsigned int start;
322 int idx;
324 if (!pmap->max_connect) {
325 pr_err("cdev 0x%p, p#%u %s, NO port map.\n",
326 cdev, csk->port_id, cdev->ports[csk->port_id]->name);
327 return -EADDRNOTAVAIL;
330 if (csk->saddr.sin_port) {
331 pr_err("source port NON-ZERO %u.\n",
332 ntohs(csk->saddr.sin_port));
333 return -EADDRINUSE;
336 spin_lock_bh(&pmap->lock);
337 if (pmap->used >= pmap->max_connect) {
338 spin_unlock_bh(&pmap->lock);
339 pr_info("cdev 0x%p, p#%u %s, ALL ports used.\n",
340 cdev, csk->port_id, cdev->ports[csk->port_id]->name);
341 return -EADDRNOTAVAIL;
344 start = idx = pmap->next;
345 do {
346 if (++idx >= pmap->max_connect)
347 idx = 0;
348 if (!pmap->port_csk[idx]) {
349 pmap->used++;
350 csk->saddr.sin_port =
351 htons(pmap->sport_base + idx);
352 pmap->next = idx;
353 pmap->port_csk[idx] = csk;
354 spin_unlock_bh(&pmap->lock);
355 cxgbi_sock_get(csk);
356 log_debug(1 << CXGBI_DBG_SOCK,
357 "cdev 0x%p, p#%u %s, p %u, %u.\n",
358 cdev, csk->port_id,
359 cdev->ports[csk->port_id]->name,
360 pmap->sport_base + idx, pmap->next);
361 return 0;
363 } while (idx != start);
364 spin_unlock_bh(&pmap->lock);
366 /* should not happen */
367 pr_warn("cdev 0x%p, p#%u %s, next %u?\n",
368 cdev, csk->port_id, cdev->ports[csk->port_id]->name,
369 pmap->next);
370 return -EADDRNOTAVAIL;
373 static void sock_put_port(struct cxgbi_sock *csk)
375 struct cxgbi_device *cdev = csk->cdev;
376 struct cxgbi_ports_map *pmap = &cdev->pmap;
378 if (csk->saddr.sin_port) {
379 int idx = ntohs(csk->saddr.sin_port) - pmap->sport_base;
381 csk->saddr.sin_port = 0;
382 if (idx < 0 || idx >= pmap->max_connect) {
383 pr_err("cdev 0x%p, p#%u %s, port %u OOR.\n",
384 cdev, csk->port_id,
385 cdev->ports[csk->port_id]->name,
386 ntohs(csk->saddr.sin_port));
387 return;
390 spin_lock_bh(&pmap->lock);
391 pmap->port_csk[idx] = NULL;
392 pmap->used--;
393 spin_unlock_bh(&pmap->lock);
395 log_debug(1 << CXGBI_DBG_SOCK,
396 "cdev 0x%p, p#%u %s, release %u.\n",
397 cdev, csk->port_id, cdev->ports[csk->port_id]->name,
398 pmap->sport_base + idx);
400 cxgbi_sock_put(csk);
405 * iscsi tcp connection
407 void cxgbi_sock_free_cpl_skbs(struct cxgbi_sock *csk)
409 if (csk->cpl_close) {
410 kfree_skb(csk->cpl_close);
411 csk->cpl_close = NULL;
413 if (csk->cpl_abort_req) {
414 kfree_skb(csk->cpl_abort_req);
415 csk->cpl_abort_req = NULL;
417 if (csk->cpl_abort_rpl) {
418 kfree_skb(csk->cpl_abort_rpl);
419 csk->cpl_abort_rpl = NULL;
422 EXPORT_SYMBOL_GPL(cxgbi_sock_free_cpl_skbs);
424 static struct cxgbi_sock *cxgbi_sock_create(struct cxgbi_device *cdev)
426 struct cxgbi_sock *csk = kzalloc(sizeof(*csk), GFP_NOIO);
428 if (!csk) {
429 pr_info("alloc csk %zu failed.\n", sizeof(*csk));
430 return NULL;
433 if (cdev->csk_alloc_cpls(csk) < 0) {
434 pr_info("csk 0x%p, alloc cpls failed.\n", csk);
435 kfree(csk);
436 return NULL;
439 spin_lock_init(&csk->lock);
440 kref_init(&csk->refcnt);
441 skb_queue_head_init(&csk->receive_queue);
442 skb_queue_head_init(&csk->write_queue);
443 setup_timer(&csk->retry_timer, NULL, (unsigned long)csk);
444 rwlock_init(&csk->callback_lock);
445 csk->cdev = cdev;
446 csk->flags = 0;
447 cxgbi_sock_set_state(csk, CTP_CLOSED);
449 log_debug(1 << CXGBI_DBG_SOCK, "cdev 0x%p, new csk 0x%p.\n", cdev, csk);
451 return csk;
454 static struct rtable *find_route_ipv4(struct flowi4 *fl4,
455 __be32 saddr, __be32 daddr,
456 __be16 sport, __be16 dport, u8 tos)
458 struct rtable *rt;
460 rt = ip_route_output_ports(&init_net, fl4, NULL, daddr, saddr,
461 dport, sport, IPPROTO_TCP, tos, 0);
462 if (IS_ERR(rt))
463 return NULL;
465 return rt;
468 static struct cxgbi_sock *cxgbi_check_route(struct sockaddr *dst_addr)
470 struct sockaddr_in *daddr = (struct sockaddr_in *)dst_addr;
471 struct dst_entry *dst;
472 struct net_device *ndev;
473 struct cxgbi_device *cdev;
474 struct rtable *rt = NULL;
475 struct neighbour *n;
476 struct flowi4 fl4;
477 struct cxgbi_sock *csk = NULL;
478 unsigned int mtu = 0;
479 int port = 0xFFFF;
480 int err = 0;
482 if (daddr->sin_family != AF_INET) {
483 pr_info("address family 0x%x NOT supported.\n",
484 daddr->sin_family);
485 err = -EAFNOSUPPORT;
486 goto err_out;
489 rt = find_route_ipv4(&fl4, 0, daddr->sin_addr.s_addr, 0, daddr->sin_port, 0);
490 if (!rt) {
491 pr_info("no route to ipv4 0x%x, port %u.\n",
492 daddr->sin_addr.s_addr, daddr->sin_port);
493 err = -ENETUNREACH;
494 goto err_out;
496 dst = &rt->dst;
497 n = dst_get_neighbour_noref(dst);
498 if (!n) {
499 err = -ENODEV;
500 goto rel_rt;
502 ndev = n->dev;
504 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
505 pr_info("multi-cast route %pI4, port %u, dev %s.\n",
506 &daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
507 ndev->name);
508 err = -ENETUNREACH;
509 goto rel_rt;
512 if (ndev->flags & IFF_LOOPBACK) {
513 ndev = ip_dev_find(&init_net, daddr->sin_addr.s_addr);
514 mtu = ndev->mtu;
515 pr_info("rt dev %s, loopback -> %s, mtu %u.\n",
516 n->dev->name, ndev->name, mtu);
519 cdev = cxgbi_device_find_by_netdev(ndev, &port);
520 if (!cdev) {
521 pr_info("dst %pI4, %s, NOT cxgbi device.\n",
522 &daddr->sin_addr.s_addr, ndev->name);
523 err = -ENETUNREACH;
524 goto rel_rt;
526 log_debug(1 << CXGBI_DBG_SOCK,
527 "route to %pI4 :%u, ndev p#%d,%s, cdev 0x%p.\n",
528 &daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
529 port, ndev->name, cdev);
531 csk = cxgbi_sock_create(cdev);
532 if (!csk) {
533 err = -ENOMEM;
534 goto rel_rt;
536 csk->cdev = cdev;
537 csk->port_id = port;
538 csk->mtu = mtu;
539 csk->dst = dst;
540 csk->daddr.sin_addr.s_addr = daddr->sin_addr.s_addr;
541 csk->daddr.sin_port = daddr->sin_port;
542 csk->daddr.sin_family = daddr->sin_family;
543 csk->saddr.sin_addr.s_addr = fl4.saddr;
545 return csk;
547 rel_rt:
548 ip_rt_put(rt);
549 if (csk)
550 cxgbi_sock_closed(csk);
551 err_out:
552 return ERR_PTR(err);
555 void cxgbi_sock_established(struct cxgbi_sock *csk, unsigned int snd_isn,
556 unsigned int opt)
558 csk->write_seq = csk->snd_nxt = csk->snd_una = snd_isn;
559 dst_confirm(csk->dst);
560 smp_mb();
561 cxgbi_sock_set_state(csk, CTP_ESTABLISHED);
563 EXPORT_SYMBOL_GPL(cxgbi_sock_established);
565 static void cxgbi_inform_iscsi_conn_closing(struct cxgbi_sock *csk)
567 log_debug(1 << CXGBI_DBG_SOCK,
568 "csk 0x%p, state %u, flags 0x%lx, conn 0x%p.\n",
569 csk, csk->state, csk->flags, csk->user_data);
571 if (csk->state != CTP_ESTABLISHED) {
572 read_lock_bh(&csk->callback_lock);
573 if (csk->user_data)
574 iscsi_conn_failure(csk->user_data,
575 ISCSI_ERR_CONN_FAILED);
576 read_unlock_bh(&csk->callback_lock);
580 void cxgbi_sock_closed(struct cxgbi_sock *csk)
582 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
583 csk, (csk)->state, (csk)->flags, (csk)->tid);
584 cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
585 if (csk->state == CTP_ACTIVE_OPEN || csk->state == CTP_CLOSED)
586 return;
587 if (csk->saddr.sin_port)
588 sock_put_port(csk);
589 if (csk->dst)
590 dst_release(csk->dst);
591 csk->cdev->csk_release_offload_resources(csk);
592 cxgbi_sock_set_state(csk, CTP_CLOSED);
593 cxgbi_inform_iscsi_conn_closing(csk);
594 cxgbi_sock_put(csk);
596 EXPORT_SYMBOL_GPL(cxgbi_sock_closed);
598 static void need_active_close(struct cxgbi_sock *csk)
600 int data_lost;
601 int close_req = 0;
603 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
604 csk, (csk)->state, (csk)->flags, (csk)->tid);
605 spin_lock_bh(&csk->lock);
606 dst_confirm(csk->dst);
607 data_lost = skb_queue_len(&csk->receive_queue);
608 __skb_queue_purge(&csk->receive_queue);
610 if (csk->state == CTP_ACTIVE_OPEN)
611 cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
612 else if (csk->state == CTP_ESTABLISHED) {
613 close_req = 1;
614 cxgbi_sock_set_state(csk, CTP_ACTIVE_CLOSE);
615 } else if (csk->state == CTP_PASSIVE_CLOSE) {
616 close_req = 1;
617 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
620 if (close_req) {
621 if (data_lost)
622 csk->cdev->csk_send_abort_req(csk);
623 else
624 csk->cdev->csk_send_close_req(csk);
627 spin_unlock_bh(&csk->lock);
630 void cxgbi_sock_fail_act_open(struct cxgbi_sock *csk, int errno)
632 pr_info("csk 0x%p,%u,%lx, %pI4:%u-%pI4:%u, err %d.\n",
633 csk, csk->state, csk->flags,
634 &csk->saddr.sin_addr.s_addr, csk->saddr.sin_port,
635 &csk->daddr.sin_addr.s_addr, csk->daddr.sin_port,
636 errno);
638 cxgbi_sock_set_state(csk, CTP_CONNECTING);
639 csk->err = errno;
640 cxgbi_sock_closed(csk);
642 EXPORT_SYMBOL_GPL(cxgbi_sock_fail_act_open);
644 void cxgbi_sock_act_open_req_arp_failure(void *handle, struct sk_buff *skb)
646 struct cxgbi_sock *csk = (struct cxgbi_sock *)skb->sk;
648 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
649 csk, (csk)->state, (csk)->flags, (csk)->tid);
650 cxgbi_sock_get(csk);
651 spin_lock_bh(&csk->lock);
652 if (csk->state == CTP_ACTIVE_OPEN)
653 cxgbi_sock_fail_act_open(csk, -EHOSTUNREACH);
654 spin_unlock_bh(&csk->lock);
655 cxgbi_sock_put(csk);
656 __kfree_skb(skb);
658 EXPORT_SYMBOL_GPL(cxgbi_sock_act_open_req_arp_failure);
660 void cxgbi_sock_rcv_abort_rpl(struct cxgbi_sock *csk)
662 cxgbi_sock_get(csk);
663 spin_lock_bh(&csk->lock);
664 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
665 if (!cxgbi_sock_flag(csk, CTPF_ABORT_RPL_RCVD))
666 cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_RCVD);
667 else {
668 cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_RCVD);
669 cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_PENDING);
670 if (cxgbi_sock_flag(csk, CTPF_ABORT_REQ_RCVD))
671 pr_err("csk 0x%p,%u,0x%lx,%u,ABT_RPL_RSS.\n",
672 csk, csk->state, csk->flags, csk->tid);
673 cxgbi_sock_closed(csk);
676 spin_unlock_bh(&csk->lock);
677 cxgbi_sock_put(csk);
679 EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_abort_rpl);
681 void cxgbi_sock_rcv_peer_close(struct cxgbi_sock *csk)
683 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
684 csk, (csk)->state, (csk)->flags, (csk)->tid);
685 cxgbi_sock_get(csk);
686 spin_lock_bh(&csk->lock);
688 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
689 goto done;
691 switch (csk->state) {
692 case CTP_ESTABLISHED:
693 cxgbi_sock_set_state(csk, CTP_PASSIVE_CLOSE);
694 break;
695 case CTP_ACTIVE_CLOSE:
696 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
697 break;
698 case CTP_CLOSE_WAIT_1:
699 cxgbi_sock_closed(csk);
700 break;
701 case CTP_ABORTING:
702 break;
703 default:
704 pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
705 csk, csk->state, csk->flags, csk->tid);
707 cxgbi_inform_iscsi_conn_closing(csk);
708 done:
709 spin_unlock_bh(&csk->lock);
710 cxgbi_sock_put(csk);
712 EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_peer_close);
714 void cxgbi_sock_rcv_close_conn_rpl(struct cxgbi_sock *csk, u32 snd_nxt)
716 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
717 csk, (csk)->state, (csk)->flags, (csk)->tid);
718 cxgbi_sock_get(csk);
719 spin_lock_bh(&csk->lock);
721 csk->snd_una = snd_nxt - 1;
722 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
723 goto done;
725 switch (csk->state) {
726 case CTP_ACTIVE_CLOSE:
727 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_1);
728 break;
729 case CTP_CLOSE_WAIT_1:
730 case CTP_CLOSE_WAIT_2:
731 cxgbi_sock_closed(csk);
732 break;
733 case CTP_ABORTING:
734 break;
735 default:
736 pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
737 csk, csk->state, csk->flags, csk->tid);
739 done:
740 spin_unlock_bh(&csk->lock);
741 cxgbi_sock_put(csk);
743 EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_close_conn_rpl);
745 void cxgbi_sock_rcv_wr_ack(struct cxgbi_sock *csk, unsigned int credits,
746 unsigned int snd_una, int seq_chk)
748 log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
749 "csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, snd_una %u,%d.\n",
750 csk, csk->state, csk->flags, csk->tid, credits,
751 csk->wr_cred, csk->wr_una_cred, snd_una, seq_chk);
753 spin_lock_bh(&csk->lock);
755 csk->wr_cred += credits;
756 if (csk->wr_una_cred > csk->wr_max_cred - csk->wr_cred)
757 csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
759 while (credits) {
760 struct sk_buff *p = cxgbi_sock_peek_wr(csk);
762 if (unlikely(!p)) {
763 pr_err("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, empty.\n",
764 csk, csk->state, csk->flags, csk->tid, credits,
765 csk->wr_cred, csk->wr_una_cred);
766 break;
769 if (unlikely(credits < p->csum)) {
770 pr_warn("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, < %u.\n",
771 csk, csk->state, csk->flags, csk->tid,
772 credits, csk->wr_cred, csk->wr_una_cred,
773 p->csum);
774 p->csum -= credits;
775 break;
776 } else {
777 cxgbi_sock_dequeue_wr(csk);
778 credits -= p->csum;
779 kfree_skb(p);
783 cxgbi_sock_check_wr_invariants(csk);
785 if (seq_chk) {
786 if (unlikely(before(snd_una, csk->snd_una))) {
787 pr_warn("csk 0x%p,%u,0x%lx,%u, snd_una %u/%u.",
788 csk, csk->state, csk->flags, csk->tid, snd_una,
789 csk->snd_una);
790 goto done;
793 if (csk->snd_una != snd_una) {
794 csk->snd_una = snd_una;
795 dst_confirm(csk->dst);
799 if (skb_queue_len(&csk->write_queue)) {
800 if (csk->cdev->csk_push_tx_frames(csk, 0))
801 cxgbi_conn_tx_open(csk);
802 } else
803 cxgbi_conn_tx_open(csk);
804 done:
805 spin_unlock_bh(&csk->lock);
807 EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_wr_ack);
809 static unsigned int cxgbi_sock_find_best_mtu(struct cxgbi_sock *csk,
810 unsigned short mtu)
812 int i = 0;
814 while (i < csk->cdev->nmtus - 1 && csk->cdev->mtus[i + 1] <= mtu)
815 ++i;
817 return i;
820 unsigned int cxgbi_sock_select_mss(struct cxgbi_sock *csk, unsigned int pmtu)
822 unsigned int idx;
823 struct dst_entry *dst = csk->dst;
825 csk->advmss = dst_metric_advmss(dst);
827 if (csk->advmss > pmtu - 40)
828 csk->advmss = pmtu - 40;
829 if (csk->advmss < csk->cdev->mtus[0] - 40)
830 csk->advmss = csk->cdev->mtus[0] - 40;
831 idx = cxgbi_sock_find_best_mtu(csk, csk->advmss + 40);
833 return idx;
835 EXPORT_SYMBOL_GPL(cxgbi_sock_select_mss);
837 void cxgbi_sock_skb_entail(struct cxgbi_sock *csk, struct sk_buff *skb)
839 cxgbi_skcb_tcp_seq(skb) = csk->write_seq;
840 __skb_queue_tail(&csk->write_queue, skb);
842 EXPORT_SYMBOL_GPL(cxgbi_sock_skb_entail);
844 void cxgbi_sock_purge_wr_queue(struct cxgbi_sock *csk)
846 struct sk_buff *skb;
848 while ((skb = cxgbi_sock_dequeue_wr(csk)) != NULL)
849 kfree_skb(skb);
851 EXPORT_SYMBOL_GPL(cxgbi_sock_purge_wr_queue);
853 void cxgbi_sock_check_wr_invariants(const struct cxgbi_sock *csk)
855 int pending = cxgbi_sock_count_pending_wrs(csk);
857 if (unlikely(csk->wr_cred + pending != csk->wr_max_cred))
858 pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
859 csk, csk->tid, csk->wr_cred, pending, csk->wr_max_cred);
861 EXPORT_SYMBOL_GPL(cxgbi_sock_check_wr_invariants);
863 static int cxgbi_sock_send_pdus(struct cxgbi_sock *csk, struct sk_buff *skb)
865 struct cxgbi_device *cdev = csk->cdev;
866 struct sk_buff *next;
867 int err, copied = 0;
869 spin_lock_bh(&csk->lock);
871 if (csk->state != CTP_ESTABLISHED) {
872 log_debug(1 << CXGBI_DBG_PDU_TX,
873 "csk 0x%p,%u,0x%lx,%u, EAGAIN.\n",
874 csk, csk->state, csk->flags, csk->tid);
875 err = -EAGAIN;
876 goto out_err;
879 if (csk->err) {
880 log_debug(1 << CXGBI_DBG_PDU_TX,
881 "csk 0x%p,%u,0x%lx,%u, EPIPE %d.\n",
882 csk, csk->state, csk->flags, csk->tid, csk->err);
883 err = -EPIPE;
884 goto out_err;
887 if (csk->write_seq - csk->snd_una >= cdev->snd_win) {
888 log_debug(1 << CXGBI_DBG_PDU_TX,
889 "csk 0x%p,%u,0x%lx,%u, FULL %u-%u >= %u.\n",
890 csk, csk->state, csk->flags, csk->tid, csk->write_seq,
891 csk->snd_una, cdev->snd_win);
892 err = -ENOBUFS;
893 goto out_err;
896 while (skb) {
897 int frags = skb_shinfo(skb)->nr_frags +
898 (skb->len != skb->data_len);
900 if (unlikely(skb_headroom(skb) < cdev->skb_tx_rsvd)) {
901 pr_err("csk 0x%p, skb head %u < %u.\n",
902 csk, skb_headroom(skb), cdev->skb_tx_rsvd);
903 err = -EINVAL;
904 goto out_err;
907 if (frags >= SKB_WR_LIST_SIZE) {
908 pr_err("csk 0x%p, frags %d, %u,%u >%u.\n",
909 csk, skb_shinfo(skb)->nr_frags, skb->len,
910 skb->data_len, (uint)(SKB_WR_LIST_SIZE));
911 err = -EINVAL;
912 goto out_err;
915 next = skb->next;
916 skb->next = NULL;
917 cxgbi_skcb_set_flag(skb, SKCBF_TX_NEED_HDR);
918 cxgbi_sock_skb_entail(csk, skb);
919 copied += skb->len;
920 csk->write_seq += skb->len +
921 cxgbi_ulp_extra_len(cxgbi_skcb_ulp_mode(skb));
922 skb = next;
924 done:
925 if (likely(skb_queue_len(&csk->write_queue)))
926 cdev->csk_push_tx_frames(csk, 1);
927 spin_unlock_bh(&csk->lock);
928 return copied;
930 out_err:
931 if (copied == 0 && err == -EPIPE)
932 copied = csk->err ? csk->err : -EPIPE;
933 else
934 copied = err;
935 goto done;
939 * Direct Data Placement -
940 * Directly place the iSCSI Data-In or Data-Out PDU's payload into pre-posted
941 * final destination host-memory buffers based on the Initiator Task Tag (ITT)
942 * in Data-In or Target Task Tag (TTT) in Data-Out PDUs.
943 * The host memory address is programmed into h/w in the format of pagepod
944 * entries.
945 * The location of the pagepod entry is encoded into ddp tag which is used as
946 * the base for ITT/TTT.
949 static unsigned char ddp_page_order[DDP_PGIDX_MAX] = {0, 1, 2, 4};
950 static unsigned char ddp_page_shift[DDP_PGIDX_MAX] = {12, 13, 14, 16};
951 static unsigned char page_idx = DDP_PGIDX_MAX;
953 static unsigned char sw_tag_idx_bits;
954 static unsigned char sw_tag_age_bits;
957 * Direct-Data Placement page size adjustment
959 static int ddp_adjust_page_table(void)
961 int i;
962 unsigned int base_order, order;
964 if (PAGE_SIZE < (1UL << ddp_page_shift[0])) {
965 pr_info("PAGE_SIZE 0x%lx too small, min 0x%lx\n",
966 PAGE_SIZE, 1UL << ddp_page_shift[0]);
967 return -EINVAL;
970 base_order = get_order(1UL << ddp_page_shift[0]);
971 order = get_order(1UL << PAGE_SHIFT);
973 for (i = 0; i < DDP_PGIDX_MAX; i++) {
974 /* first is the kernel page size, then just doubling */
975 ddp_page_order[i] = order - base_order + i;
976 ddp_page_shift[i] = PAGE_SHIFT + i;
978 return 0;
981 static int ddp_find_page_index(unsigned long pgsz)
983 int i;
985 for (i = 0; i < DDP_PGIDX_MAX; i++) {
986 if (pgsz == (1UL << ddp_page_shift[i]))
987 return i;
989 pr_info("ddp page size %lu not supported.\n", pgsz);
990 return DDP_PGIDX_MAX;
993 static void ddp_setup_host_page_size(void)
995 if (page_idx == DDP_PGIDX_MAX) {
996 page_idx = ddp_find_page_index(PAGE_SIZE);
998 if (page_idx == DDP_PGIDX_MAX) {
999 pr_info("system PAGE %lu, update hw.\n", PAGE_SIZE);
1000 if (ddp_adjust_page_table() < 0) {
1001 pr_info("PAGE %lu, disable ddp.\n", PAGE_SIZE);
1002 return;
1004 page_idx = ddp_find_page_index(PAGE_SIZE);
1006 pr_info("system PAGE %lu, ddp idx %u.\n", PAGE_SIZE, page_idx);
1010 void cxgbi_ddp_page_size_factor(int *pgsz_factor)
1012 int i;
1014 for (i = 0; i < DDP_PGIDX_MAX; i++)
1015 pgsz_factor[i] = ddp_page_order[i];
1017 EXPORT_SYMBOL_GPL(cxgbi_ddp_page_size_factor);
1020 * DDP setup & teardown
1023 void cxgbi_ddp_ppod_set(struct cxgbi_pagepod *ppod,
1024 struct cxgbi_pagepod_hdr *hdr,
1025 struct cxgbi_gather_list *gl, unsigned int gidx)
1027 int i;
1029 memcpy(ppod, hdr, sizeof(*hdr));
1030 for (i = 0; i < (PPOD_PAGES_MAX + 1); i++, gidx++) {
1031 ppod->addr[i] = gidx < gl->nelem ?
1032 cpu_to_be64(gl->phys_addr[gidx]) : 0ULL;
1035 EXPORT_SYMBOL_GPL(cxgbi_ddp_ppod_set);
1037 void cxgbi_ddp_ppod_clear(struct cxgbi_pagepod *ppod)
1039 memset(ppod, 0, sizeof(*ppod));
1041 EXPORT_SYMBOL_GPL(cxgbi_ddp_ppod_clear);
1043 static inline int ddp_find_unused_entries(struct cxgbi_ddp_info *ddp,
1044 unsigned int start, unsigned int max,
1045 unsigned int count,
1046 struct cxgbi_gather_list *gl)
1048 unsigned int i, j, k;
1050 /* not enough entries */
1051 if ((max - start) < count) {
1052 log_debug(1 << CXGBI_DBG_DDP,
1053 "NOT enough entries %u+%u < %u.\n", start, count, max);
1054 return -EBUSY;
1057 max -= count;
1058 spin_lock(&ddp->map_lock);
1059 for (i = start; i < max;) {
1060 for (j = 0, k = i; j < count; j++, k++) {
1061 if (ddp->gl_map[k])
1062 break;
1064 if (j == count) {
1065 for (j = 0, k = i; j < count; j++, k++)
1066 ddp->gl_map[k] = gl;
1067 spin_unlock(&ddp->map_lock);
1068 return i;
1070 i += j + 1;
1072 spin_unlock(&ddp->map_lock);
1073 log_debug(1 << CXGBI_DBG_DDP,
1074 "NO suitable entries %u available.\n", count);
1075 return -EBUSY;
1078 static inline void ddp_unmark_entries(struct cxgbi_ddp_info *ddp,
1079 int start, int count)
1081 spin_lock(&ddp->map_lock);
1082 memset(&ddp->gl_map[start], 0,
1083 count * sizeof(struct cxgbi_gather_list *));
1084 spin_unlock(&ddp->map_lock);
1087 static inline void ddp_gl_unmap(struct pci_dev *pdev,
1088 struct cxgbi_gather_list *gl)
1090 int i;
1092 for (i = 0; i < gl->nelem; i++)
1093 dma_unmap_page(&pdev->dev, gl->phys_addr[i], PAGE_SIZE,
1094 PCI_DMA_FROMDEVICE);
1097 static inline int ddp_gl_map(struct pci_dev *pdev,
1098 struct cxgbi_gather_list *gl)
1100 int i;
1102 for (i = 0; i < gl->nelem; i++) {
1103 gl->phys_addr[i] = dma_map_page(&pdev->dev, gl->pages[i], 0,
1104 PAGE_SIZE,
1105 PCI_DMA_FROMDEVICE);
1106 if (unlikely(dma_mapping_error(&pdev->dev, gl->phys_addr[i]))) {
1107 log_debug(1 << CXGBI_DBG_DDP,
1108 "page %d 0x%p, 0x%p dma mapping err.\n",
1109 i, gl->pages[i], pdev);
1110 goto unmap;
1113 return i;
1114 unmap:
1115 if (i) {
1116 unsigned int nelem = gl->nelem;
1118 gl->nelem = i;
1119 ddp_gl_unmap(pdev, gl);
1120 gl->nelem = nelem;
1122 return -EINVAL;
1125 static void ddp_release_gl(struct cxgbi_gather_list *gl,
1126 struct pci_dev *pdev)
1128 ddp_gl_unmap(pdev, gl);
1129 kfree(gl);
1132 static struct cxgbi_gather_list *ddp_make_gl(unsigned int xferlen,
1133 struct scatterlist *sgl,
1134 unsigned int sgcnt,
1135 struct pci_dev *pdev,
1136 gfp_t gfp)
1138 struct cxgbi_gather_list *gl;
1139 struct scatterlist *sg = sgl;
1140 struct page *sgpage = sg_page(sg);
1141 unsigned int sglen = sg->length;
1142 unsigned int sgoffset = sg->offset;
1143 unsigned int npages = (xferlen + sgoffset + PAGE_SIZE - 1) >>
1144 PAGE_SHIFT;
1145 int i = 1, j = 0;
1147 if (xferlen < DDP_THRESHOLD) {
1148 log_debug(1 << CXGBI_DBG_DDP,
1149 "xfer %u < threshold %u, no ddp.\n",
1150 xferlen, DDP_THRESHOLD);
1151 return NULL;
1154 gl = kzalloc(sizeof(struct cxgbi_gather_list) +
1155 npages * (sizeof(dma_addr_t) +
1156 sizeof(struct page *)), gfp);
1157 if (!gl) {
1158 log_debug(1 << CXGBI_DBG_DDP,
1159 "xfer %u, %u pages, OOM.\n", xferlen, npages);
1160 return NULL;
1163 log_debug(1 << CXGBI_DBG_DDP,
1164 "xfer %u, sgl %u, gl max %u.\n", xferlen, sgcnt, npages);
1166 gl->pages = (struct page **)&gl->phys_addr[npages];
1167 gl->nelem = npages;
1168 gl->length = xferlen;
1169 gl->offset = sgoffset;
1170 gl->pages[0] = sgpage;
1172 for (i = 1, sg = sg_next(sgl), j = 0; i < sgcnt;
1173 i++, sg = sg_next(sg)) {
1174 struct page *page = sg_page(sg);
1176 if (sgpage == page && sg->offset == sgoffset + sglen)
1177 sglen += sg->length;
1178 else {
1179 /* make sure the sgl is fit for ddp:
1180 * each has the same page size, and
1181 * all of the middle pages are used completely
1183 if ((j && sgoffset) || ((i != sgcnt - 1) &&
1184 ((sglen + sgoffset) & ~PAGE_MASK))) {
1185 log_debug(1 << CXGBI_DBG_DDP,
1186 "page %d/%u, %u + %u.\n",
1187 i, sgcnt, sgoffset, sglen);
1188 goto error_out;
1191 j++;
1192 if (j == gl->nelem || sg->offset) {
1193 log_debug(1 << CXGBI_DBG_DDP,
1194 "page %d/%u, offset %u.\n",
1195 j, gl->nelem, sg->offset);
1196 goto error_out;
1198 gl->pages[j] = page;
1199 sglen = sg->length;
1200 sgoffset = sg->offset;
1201 sgpage = page;
1204 gl->nelem = ++j;
1206 if (ddp_gl_map(pdev, gl) < 0)
1207 goto error_out;
1209 return gl;
1211 error_out:
1212 kfree(gl);
1213 return NULL;
1216 static void ddp_tag_release(struct cxgbi_hba *chba, u32 tag)
1218 struct cxgbi_device *cdev = chba->cdev;
1219 struct cxgbi_ddp_info *ddp = cdev->ddp;
1220 u32 idx;
1222 idx = (tag >> PPOD_IDX_SHIFT) & ddp->idx_mask;
1223 if (idx < ddp->nppods) {
1224 struct cxgbi_gather_list *gl = ddp->gl_map[idx];
1225 unsigned int npods;
1227 if (!gl || !gl->nelem) {
1228 pr_warn("tag 0x%x, idx %u, gl 0x%p, %u.\n",
1229 tag, idx, gl, gl ? gl->nelem : 0);
1230 return;
1232 npods = (gl->nelem + PPOD_PAGES_MAX - 1) >> PPOD_PAGES_SHIFT;
1233 log_debug(1 << CXGBI_DBG_DDP,
1234 "tag 0x%x, release idx %u, npods %u.\n",
1235 tag, idx, npods);
1236 cdev->csk_ddp_clear(chba, tag, idx, npods);
1237 ddp_unmark_entries(ddp, idx, npods);
1238 ddp_release_gl(gl, ddp->pdev);
1239 } else
1240 pr_warn("tag 0x%x, idx %u > max %u.\n", tag, idx, ddp->nppods);
1243 static int ddp_tag_reserve(struct cxgbi_sock *csk, unsigned int tid,
1244 u32 sw_tag, u32 *tagp, struct cxgbi_gather_list *gl,
1245 gfp_t gfp)
1247 struct cxgbi_device *cdev = csk->cdev;
1248 struct cxgbi_ddp_info *ddp = cdev->ddp;
1249 struct cxgbi_tag_format *tformat = &cdev->tag_format;
1250 struct cxgbi_pagepod_hdr hdr;
1251 unsigned int npods;
1252 int idx = -1;
1253 int err = -ENOMEM;
1254 u32 tag;
1256 npods = (gl->nelem + PPOD_PAGES_MAX - 1) >> PPOD_PAGES_SHIFT;
1257 if (ddp->idx_last == ddp->nppods)
1258 idx = ddp_find_unused_entries(ddp, 0, ddp->nppods,
1259 npods, gl);
1260 else {
1261 idx = ddp_find_unused_entries(ddp, ddp->idx_last + 1,
1262 ddp->nppods, npods,
1263 gl);
1264 if (idx < 0 && ddp->idx_last >= npods) {
1265 idx = ddp_find_unused_entries(ddp, 0,
1266 min(ddp->idx_last + npods, ddp->nppods),
1267 npods, gl);
1270 if (idx < 0) {
1271 log_debug(1 << CXGBI_DBG_DDP,
1272 "xferlen %u, gl %u, npods %u NO DDP.\n",
1273 gl->length, gl->nelem, npods);
1274 return idx;
1277 tag = cxgbi_ddp_tag_base(tformat, sw_tag);
1278 tag |= idx << PPOD_IDX_SHIFT;
1280 hdr.rsvd = 0;
1281 hdr.vld_tid = htonl(PPOD_VALID_FLAG | PPOD_TID(tid));
1282 hdr.pgsz_tag_clr = htonl(tag & ddp->rsvd_tag_mask);
1283 hdr.max_offset = htonl(gl->length);
1284 hdr.page_offset = htonl(gl->offset);
1286 err = cdev->csk_ddp_set(csk, &hdr, idx, npods, gl);
1287 if (err < 0)
1288 goto unmark_entries;
1290 ddp->idx_last = idx;
1291 log_debug(1 << CXGBI_DBG_DDP,
1292 "xfer %u, gl %u,%u, tid 0x%x, tag 0x%x->0x%x(%u,%u).\n",
1293 gl->length, gl->nelem, gl->offset, tid, sw_tag, tag, idx,
1294 npods);
1295 *tagp = tag;
1296 return 0;
1298 unmark_entries:
1299 ddp_unmark_entries(ddp, idx, npods);
1300 return err;
1303 int cxgbi_ddp_reserve(struct cxgbi_sock *csk, unsigned int *tagp,
1304 unsigned int sw_tag, unsigned int xferlen,
1305 struct scatterlist *sgl, unsigned int sgcnt, gfp_t gfp)
1307 struct cxgbi_device *cdev = csk->cdev;
1308 struct cxgbi_tag_format *tformat = &cdev->tag_format;
1309 struct cxgbi_gather_list *gl;
1310 int err;
1312 if (page_idx >= DDP_PGIDX_MAX || !cdev->ddp ||
1313 xferlen < DDP_THRESHOLD) {
1314 log_debug(1 << CXGBI_DBG_DDP,
1315 "pgidx %u, xfer %u, NO ddp.\n", page_idx, xferlen);
1316 return -EINVAL;
1319 if (!cxgbi_sw_tag_usable(tformat, sw_tag)) {
1320 log_debug(1 << CXGBI_DBG_DDP,
1321 "sw_tag 0x%x NOT usable.\n", sw_tag);
1322 return -EINVAL;
1325 gl = ddp_make_gl(xferlen, sgl, sgcnt, cdev->pdev, gfp);
1326 if (!gl)
1327 return -ENOMEM;
1329 err = ddp_tag_reserve(csk, csk->tid, sw_tag, tagp, gl, gfp);
1330 if (err < 0)
1331 ddp_release_gl(gl, cdev->pdev);
1333 return err;
1336 static void ddp_destroy(struct kref *kref)
1338 struct cxgbi_ddp_info *ddp = container_of(kref,
1339 struct cxgbi_ddp_info,
1340 refcnt);
1341 struct cxgbi_device *cdev = ddp->cdev;
1342 int i = 0;
1344 pr_info("kref 0, destroy ddp 0x%p, cdev 0x%p.\n", ddp, cdev);
1346 while (i < ddp->nppods) {
1347 struct cxgbi_gather_list *gl = ddp->gl_map[i];
1349 if (gl) {
1350 int npods = (gl->nelem + PPOD_PAGES_MAX - 1)
1351 >> PPOD_PAGES_SHIFT;
1352 pr_info("cdev 0x%p, ddp %d + %d.\n", cdev, i, npods);
1353 kfree(gl);
1354 i += npods;
1355 } else
1356 i++;
1358 cxgbi_free_big_mem(ddp);
1361 int cxgbi_ddp_cleanup(struct cxgbi_device *cdev)
1363 struct cxgbi_ddp_info *ddp = cdev->ddp;
1365 log_debug(1 << CXGBI_DBG_DDP,
1366 "cdev 0x%p, release ddp 0x%p.\n", cdev, ddp);
1367 cdev->ddp = NULL;
1368 if (ddp)
1369 return kref_put(&ddp->refcnt, ddp_destroy);
1370 return 0;
1372 EXPORT_SYMBOL_GPL(cxgbi_ddp_cleanup);
1374 int cxgbi_ddp_init(struct cxgbi_device *cdev,
1375 unsigned int llimit, unsigned int ulimit,
1376 unsigned int max_txsz, unsigned int max_rxsz)
1378 struct cxgbi_ddp_info *ddp;
1379 unsigned int ppmax, bits;
1381 ppmax = (ulimit - llimit + 1) >> PPOD_SIZE_SHIFT;
1382 bits = __ilog2_u32(ppmax) + 1;
1383 if (bits > PPOD_IDX_MAX_SIZE)
1384 bits = PPOD_IDX_MAX_SIZE;
1385 ppmax = (1 << (bits - 1)) - 1;
1387 ddp = cxgbi_alloc_big_mem(sizeof(struct cxgbi_ddp_info) +
1388 ppmax * (sizeof(struct cxgbi_gather_list *) +
1389 sizeof(struct sk_buff *)),
1390 GFP_KERNEL);
1391 if (!ddp) {
1392 pr_warn("cdev 0x%p, ddp ppmax %u OOM.\n", cdev, ppmax);
1393 return -ENOMEM;
1395 ddp->gl_map = (struct cxgbi_gather_list **)(ddp + 1);
1396 cdev->ddp = ddp;
1398 spin_lock_init(&ddp->map_lock);
1399 kref_init(&ddp->refcnt);
1401 ddp->cdev = cdev;
1402 ddp->pdev = cdev->pdev;
1403 ddp->llimit = llimit;
1404 ddp->ulimit = ulimit;
1405 ddp->max_txsz = min_t(unsigned int, max_txsz, ULP2_MAX_PKT_SIZE);
1406 ddp->max_rxsz = min_t(unsigned int, max_rxsz, ULP2_MAX_PKT_SIZE);
1407 ddp->nppods = ppmax;
1408 ddp->idx_last = ppmax;
1409 ddp->idx_bits = bits;
1410 ddp->idx_mask = (1 << bits) - 1;
1411 ddp->rsvd_tag_mask = (1 << (bits + PPOD_IDX_SHIFT)) - 1;
1413 cdev->tag_format.sw_bits = sw_tag_idx_bits + sw_tag_age_bits;
1414 cdev->tag_format.rsvd_bits = ddp->idx_bits;
1415 cdev->tag_format.rsvd_shift = PPOD_IDX_SHIFT;
1416 cdev->tag_format.rsvd_mask = (1 << cdev->tag_format.rsvd_bits) - 1;
1418 pr_info("%s tag format, sw %u, rsvd %u,%u, mask 0x%x.\n",
1419 cdev->ports[0]->name, cdev->tag_format.sw_bits,
1420 cdev->tag_format.rsvd_bits, cdev->tag_format.rsvd_shift,
1421 cdev->tag_format.rsvd_mask);
1423 cdev->tx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
1424 ddp->max_txsz - ISCSI_PDU_NONPAYLOAD_LEN);
1425 cdev->rx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
1426 ddp->max_rxsz - ISCSI_PDU_NONPAYLOAD_LEN);
1428 log_debug(1 << CXGBI_DBG_DDP,
1429 "%s max payload size: %u/%u, %u/%u.\n",
1430 cdev->ports[0]->name, cdev->tx_max_size, ddp->max_txsz,
1431 cdev->rx_max_size, ddp->max_rxsz);
1432 return 0;
1434 EXPORT_SYMBOL_GPL(cxgbi_ddp_init);
1437 * APIs interacting with open-iscsi libraries
1440 static unsigned char padding[4];
1442 static void task_release_itt(struct iscsi_task *task, itt_t hdr_itt)
1444 struct scsi_cmnd *sc = task->sc;
1445 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
1446 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1447 struct cxgbi_hba *chba = cconn->chba;
1448 struct cxgbi_tag_format *tformat = &chba->cdev->tag_format;
1449 u32 tag = ntohl((__force u32)hdr_itt);
1451 log_debug(1 << CXGBI_DBG_DDP,
1452 "cdev 0x%p, release tag 0x%x.\n", chba->cdev, tag);
1453 if (sc &&
1454 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
1455 cxgbi_is_ddp_tag(tformat, tag))
1456 ddp_tag_release(chba, tag);
1459 static int task_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
1461 struct scsi_cmnd *sc = task->sc;
1462 struct iscsi_conn *conn = task->conn;
1463 struct iscsi_session *sess = conn->session;
1464 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1465 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1466 struct cxgbi_hba *chba = cconn->chba;
1467 struct cxgbi_tag_format *tformat = &chba->cdev->tag_format;
1468 u32 sw_tag = (sess->age << cconn->task_idx_bits) | task->itt;
1469 u32 tag = 0;
1470 int err = -EINVAL;
1472 if (sc &&
1473 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE)) {
1474 err = cxgbi_ddp_reserve(cconn->cep->csk, &tag, sw_tag,
1475 scsi_in(sc)->length,
1476 scsi_in(sc)->table.sgl,
1477 scsi_in(sc)->table.nents,
1478 GFP_ATOMIC);
1479 if (err < 0)
1480 log_debug(1 << CXGBI_DBG_DDP,
1481 "csk 0x%p, R task 0x%p, %u,%u, no ddp.\n",
1482 cconn->cep->csk, task, scsi_in(sc)->length,
1483 scsi_in(sc)->table.nents);
1486 if (err < 0)
1487 tag = cxgbi_set_non_ddp_tag(tformat, sw_tag);
1488 /* the itt need to sent in big-endian order */
1489 *hdr_itt = (__force itt_t)htonl(tag);
1491 log_debug(1 << CXGBI_DBG_DDP,
1492 "cdev 0x%p, task 0x%p, 0x%x(0x%x,0x%x)->0x%x/0x%x.\n",
1493 chba->cdev, task, sw_tag, task->itt, sess->age, tag, *hdr_itt);
1494 return 0;
1497 void cxgbi_parse_pdu_itt(struct iscsi_conn *conn, itt_t itt, int *idx, int *age)
1499 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1500 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1501 struct cxgbi_device *cdev = cconn->chba->cdev;
1502 u32 tag = ntohl((__force u32) itt);
1503 u32 sw_bits;
1505 sw_bits = cxgbi_tag_nonrsvd_bits(&cdev->tag_format, tag);
1506 if (idx)
1507 *idx = sw_bits & ((1 << cconn->task_idx_bits) - 1);
1508 if (age)
1509 *age = (sw_bits >> cconn->task_idx_bits) & ISCSI_AGE_MASK;
1511 log_debug(1 << CXGBI_DBG_DDP,
1512 "cdev 0x%p, tag 0x%x/0x%x, -> 0x%x(0x%x,0x%x).\n",
1513 cdev, tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
1514 age ? *age : 0xFF);
1516 EXPORT_SYMBOL_GPL(cxgbi_parse_pdu_itt);
1518 void cxgbi_conn_tx_open(struct cxgbi_sock *csk)
1520 struct iscsi_conn *conn = csk->user_data;
1522 if (conn) {
1523 log_debug(1 << CXGBI_DBG_SOCK,
1524 "csk 0x%p, cid %d.\n", csk, conn->id);
1525 iscsi_conn_queue_work(conn);
1528 EXPORT_SYMBOL_GPL(cxgbi_conn_tx_open);
1531 * pdu receive, interact with libiscsi_tcp
1533 static inline int read_pdu_skb(struct iscsi_conn *conn,
1534 struct sk_buff *skb,
1535 unsigned int offset,
1536 int offloaded)
1538 int status = 0;
1539 int bytes_read;
1541 bytes_read = iscsi_tcp_recv_skb(conn, skb, offset, offloaded, &status);
1542 switch (status) {
1543 case ISCSI_TCP_CONN_ERR:
1544 pr_info("skb 0x%p, off %u, %d, TCP_ERR.\n",
1545 skb, offset, offloaded);
1546 return -EIO;
1547 case ISCSI_TCP_SUSPENDED:
1548 log_debug(1 << CXGBI_DBG_PDU_RX,
1549 "skb 0x%p, off %u, %d, TCP_SUSPEND, rc %d.\n",
1550 skb, offset, offloaded, bytes_read);
1551 /* no transfer - just have caller flush queue */
1552 return bytes_read;
1553 case ISCSI_TCP_SKB_DONE:
1554 pr_info("skb 0x%p, off %u, %d, TCP_SKB_DONE.\n",
1555 skb, offset, offloaded);
1557 * pdus should always fit in the skb and we should get
1558 * segment done notifcation.
1560 iscsi_conn_printk(KERN_ERR, conn, "Invalid pdu or skb.");
1561 return -EFAULT;
1562 case ISCSI_TCP_SEGMENT_DONE:
1563 log_debug(1 << CXGBI_DBG_PDU_RX,
1564 "skb 0x%p, off %u, %d, TCP_SEG_DONE, rc %d.\n",
1565 skb, offset, offloaded, bytes_read);
1566 return bytes_read;
1567 default:
1568 pr_info("skb 0x%p, off %u, %d, invalid status %d.\n",
1569 skb, offset, offloaded, status);
1570 return -EINVAL;
1574 static int skb_read_pdu_bhs(struct iscsi_conn *conn, struct sk_buff *skb)
1576 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1578 log_debug(1 << CXGBI_DBG_PDU_RX,
1579 "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
1580 conn, skb, skb->len, cxgbi_skcb_flags(skb));
1582 if (!iscsi_tcp_recv_segment_is_hdr(tcp_conn)) {
1583 pr_info("conn 0x%p, skb 0x%p, not hdr.\n", conn, skb);
1584 iscsi_conn_failure(conn, ISCSI_ERR_PROTO);
1585 return -EIO;
1588 if (conn->hdrdgst_en &&
1589 cxgbi_skcb_test_flag(skb, SKCBF_RX_HCRC_ERR)) {
1590 pr_info("conn 0x%p, skb 0x%p, hcrc.\n", conn, skb);
1591 iscsi_conn_failure(conn, ISCSI_ERR_HDR_DGST);
1592 return -EIO;
1595 return read_pdu_skb(conn, skb, 0, 0);
1598 static int skb_read_pdu_data(struct iscsi_conn *conn, struct sk_buff *lskb,
1599 struct sk_buff *skb, unsigned int offset)
1601 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1602 bool offloaded = 0;
1603 int opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
1605 log_debug(1 << CXGBI_DBG_PDU_RX,
1606 "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
1607 conn, skb, skb->len, cxgbi_skcb_flags(skb));
1609 if (conn->datadgst_en &&
1610 cxgbi_skcb_test_flag(lskb, SKCBF_RX_DCRC_ERR)) {
1611 pr_info("conn 0x%p, skb 0x%p, dcrc 0x%lx.\n",
1612 conn, lskb, cxgbi_skcb_flags(lskb));
1613 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
1614 return -EIO;
1617 if (iscsi_tcp_recv_segment_is_hdr(tcp_conn))
1618 return 0;
1620 /* coalesced, add header digest length */
1621 if (lskb == skb && conn->hdrdgst_en)
1622 offset += ISCSI_DIGEST_SIZE;
1624 if (cxgbi_skcb_test_flag(lskb, SKCBF_RX_DATA_DDPD))
1625 offloaded = 1;
1627 if (opcode == ISCSI_OP_SCSI_DATA_IN)
1628 log_debug(1 << CXGBI_DBG_PDU_RX,
1629 "skb 0x%p, op 0x%x, itt 0x%x, %u %s ddp'ed.\n",
1630 skb, opcode, ntohl(tcp_conn->in.hdr->itt),
1631 tcp_conn->in.datalen, offloaded ? "is" : "not");
1633 return read_pdu_skb(conn, skb, offset, offloaded);
1636 static void csk_return_rx_credits(struct cxgbi_sock *csk, int copied)
1638 struct cxgbi_device *cdev = csk->cdev;
1639 int must_send;
1640 u32 credits;
1642 log_debug(1 << CXGBI_DBG_PDU_RX,
1643 "csk 0x%p,%u,0x%lu,%u, seq %u, wup %u, thre %u, %u.\n",
1644 csk, csk->state, csk->flags, csk->tid, csk->copied_seq,
1645 csk->rcv_wup, cdev->rx_credit_thres,
1646 cdev->rcv_win);
1648 if (csk->state != CTP_ESTABLISHED)
1649 return;
1651 credits = csk->copied_seq - csk->rcv_wup;
1652 if (unlikely(!credits))
1653 return;
1654 if (unlikely(cdev->rx_credit_thres == 0))
1655 return;
1657 must_send = credits + 16384 >= cdev->rcv_win;
1658 if (must_send || credits >= cdev->rx_credit_thres)
1659 csk->rcv_wup += cdev->csk_send_rx_credits(csk, credits);
1662 void cxgbi_conn_pdu_ready(struct cxgbi_sock *csk)
1664 struct cxgbi_device *cdev = csk->cdev;
1665 struct iscsi_conn *conn = csk->user_data;
1666 struct sk_buff *skb;
1667 unsigned int read = 0;
1668 int err = 0;
1670 log_debug(1 << CXGBI_DBG_PDU_RX,
1671 "csk 0x%p, conn 0x%p.\n", csk, conn);
1673 if (unlikely(!conn || conn->suspend_rx)) {
1674 log_debug(1 << CXGBI_DBG_PDU_RX,
1675 "csk 0x%p, conn 0x%p, id %d, suspend_rx %lu!\n",
1676 csk, conn, conn ? conn->id : 0xFF,
1677 conn ? conn->suspend_rx : 0xFF);
1678 return;
1681 while (!err) {
1682 skb = skb_peek(&csk->receive_queue);
1683 if (!skb ||
1684 !(cxgbi_skcb_test_flag(skb, SKCBF_RX_STATUS))) {
1685 if (skb)
1686 log_debug(1 << CXGBI_DBG_PDU_RX,
1687 "skb 0x%p, NOT ready 0x%lx.\n",
1688 skb, cxgbi_skcb_flags(skb));
1689 break;
1691 __skb_unlink(skb, &csk->receive_queue);
1693 read += cxgbi_skcb_rx_pdulen(skb);
1694 log_debug(1 << CXGBI_DBG_PDU_RX,
1695 "csk 0x%p, skb 0x%p,%u,f 0x%lx, pdu len %u.\n",
1696 csk, skb, skb->len, cxgbi_skcb_flags(skb),
1697 cxgbi_skcb_rx_pdulen(skb));
1699 if (cxgbi_skcb_test_flag(skb, SKCBF_RX_COALESCED)) {
1700 err = skb_read_pdu_bhs(conn, skb);
1701 if (err < 0) {
1702 pr_err("coalesced bhs, csk 0x%p, skb 0x%p,%u, "
1703 "f 0x%lx, plen %u.\n",
1704 csk, skb, skb->len,
1705 cxgbi_skcb_flags(skb),
1706 cxgbi_skcb_rx_pdulen(skb));
1707 goto skb_done;
1709 err = skb_read_pdu_data(conn, skb, skb,
1710 err + cdev->skb_rx_extra);
1711 if (err < 0)
1712 pr_err("coalesced data, csk 0x%p, skb 0x%p,%u, "
1713 "f 0x%lx, plen %u.\n",
1714 csk, skb, skb->len,
1715 cxgbi_skcb_flags(skb),
1716 cxgbi_skcb_rx_pdulen(skb));
1717 } else {
1718 err = skb_read_pdu_bhs(conn, skb);
1719 if (err < 0) {
1720 pr_err("bhs, csk 0x%p, skb 0x%p,%u, "
1721 "f 0x%lx, plen %u.\n",
1722 csk, skb, skb->len,
1723 cxgbi_skcb_flags(skb),
1724 cxgbi_skcb_rx_pdulen(skb));
1725 goto skb_done;
1728 if (cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA)) {
1729 struct sk_buff *dskb;
1731 dskb = skb_peek(&csk->receive_queue);
1732 if (!dskb) {
1733 pr_err("csk 0x%p, skb 0x%p,%u, f 0x%lx,"
1734 " plen %u, NO data.\n",
1735 csk, skb, skb->len,
1736 cxgbi_skcb_flags(skb),
1737 cxgbi_skcb_rx_pdulen(skb));
1738 err = -EIO;
1739 goto skb_done;
1741 __skb_unlink(dskb, &csk->receive_queue);
1743 err = skb_read_pdu_data(conn, skb, dskb, 0);
1744 if (err < 0)
1745 pr_err("data, csk 0x%p, skb 0x%p,%u, "
1746 "f 0x%lx, plen %u, dskb 0x%p,"
1747 "%u.\n",
1748 csk, skb, skb->len,
1749 cxgbi_skcb_flags(skb),
1750 cxgbi_skcb_rx_pdulen(skb),
1751 dskb, dskb->len);
1752 __kfree_skb(dskb);
1753 } else
1754 err = skb_read_pdu_data(conn, skb, skb, 0);
1756 skb_done:
1757 __kfree_skb(skb);
1759 if (err < 0)
1760 break;
1763 log_debug(1 << CXGBI_DBG_PDU_RX, "csk 0x%p, read %u.\n", csk, read);
1764 if (read) {
1765 csk->copied_seq += read;
1766 csk_return_rx_credits(csk, read);
1767 conn->rxdata_octets += read;
1770 if (err < 0) {
1771 pr_info("csk 0x%p, 0x%p, rx failed %d, read %u.\n",
1772 csk, conn, err, read);
1773 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1776 EXPORT_SYMBOL_GPL(cxgbi_conn_pdu_ready);
1778 static int sgl_seek_offset(struct scatterlist *sgl, unsigned int sgcnt,
1779 unsigned int offset, unsigned int *off,
1780 struct scatterlist **sgp)
1782 int i;
1783 struct scatterlist *sg;
1785 for_each_sg(sgl, sg, sgcnt, i) {
1786 if (offset < sg->length) {
1787 *off = offset;
1788 *sgp = sg;
1789 return 0;
1791 offset -= sg->length;
1793 return -EFAULT;
1796 static int sgl_read_to_frags(struct scatterlist *sg, unsigned int sgoffset,
1797 unsigned int dlen, struct page_frag *frags,
1798 int frag_max)
1800 unsigned int datalen = dlen;
1801 unsigned int sglen = sg->length - sgoffset;
1802 struct page *page = sg_page(sg);
1803 int i;
1805 i = 0;
1806 do {
1807 unsigned int copy;
1809 if (!sglen) {
1810 sg = sg_next(sg);
1811 if (!sg) {
1812 pr_warn("sg %d NULL, len %u/%u.\n",
1813 i, datalen, dlen);
1814 return -EINVAL;
1816 sgoffset = 0;
1817 sglen = sg->length;
1818 page = sg_page(sg);
1821 copy = min(datalen, sglen);
1822 if (i && page == frags[i - 1].page &&
1823 sgoffset + sg->offset ==
1824 frags[i - 1].offset + frags[i - 1].size) {
1825 frags[i - 1].size += copy;
1826 } else {
1827 if (i >= frag_max) {
1828 pr_warn("too many pages %u, dlen %u.\n",
1829 frag_max, dlen);
1830 return -EINVAL;
1833 frags[i].page = page;
1834 frags[i].offset = sg->offset + sgoffset;
1835 frags[i].size = copy;
1836 i++;
1838 datalen -= copy;
1839 sgoffset += copy;
1840 sglen -= copy;
1841 } while (datalen);
1843 return i;
1846 int cxgbi_conn_alloc_pdu(struct iscsi_task *task, u8 opcode)
1848 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
1849 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1850 struct cxgbi_device *cdev = cconn->chba->cdev;
1851 struct iscsi_conn *conn = task->conn;
1852 struct iscsi_tcp_task *tcp_task = task->dd_data;
1853 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1854 struct scsi_cmnd *sc = task->sc;
1855 int headroom = SKB_TX_ISCSI_PDU_HEADER_MAX;
1857 tcp_task->dd_data = tdata;
1858 task->hdr = NULL;
1860 if (SKB_MAX_HEAD(cdev->skb_tx_rsvd) > (512 * MAX_SKB_FRAGS) &&
1861 (opcode == ISCSI_OP_SCSI_DATA_OUT ||
1862 (opcode == ISCSI_OP_SCSI_CMD &&
1863 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_TO_DEVICE))))
1864 /* data could goes into skb head */
1865 headroom += min_t(unsigned int,
1866 SKB_MAX_HEAD(cdev->skb_tx_rsvd),
1867 conn->max_xmit_dlength);
1869 tdata->skb = alloc_skb(cdev->skb_tx_rsvd + headroom, GFP_ATOMIC);
1870 if (!tdata->skb) {
1871 struct cxgbi_sock *csk = cconn->cep->csk;
1872 struct net_device *ndev = cdev->ports[csk->port_id];
1873 ndev->stats.tx_dropped++;
1874 return -ENOMEM;
1877 skb_reserve(tdata->skb, cdev->skb_tx_rsvd);
1878 task->hdr = (struct iscsi_hdr *)tdata->skb->data;
1879 task->hdr_max = SKB_TX_ISCSI_PDU_HEADER_MAX; /* BHS + AHS */
1881 /* data_out uses scsi_cmd's itt */
1882 if (opcode != ISCSI_OP_SCSI_DATA_OUT)
1883 task_reserve_itt(task, &task->hdr->itt);
1885 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1886 "task 0x%p, op 0x%x, skb 0x%p,%u+%u/%u, itt 0x%x.\n",
1887 task, opcode, tdata->skb, cdev->skb_tx_rsvd, headroom,
1888 conn->max_xmit_dlength, ntohl(task->hdr->itt));
1890 return 0;
1892 EXPORT_SYMBOL_GPL(cxgbi_conn_alloc_pdu);
1894 static inline void tx_skb_setmode(struct sk_buff *skb, int hcrc, int dcrc)
1896 if (hcrc || dcrc) {
1897 u8 submode = 0;
1899 if (hcrc)
1900 submode |= 1;
1901 if (dcrc)
1902 submode |= 2;
1903 cxgbi_skcb_ulp_mode(skb) = (ULP2_MODE_ISCSI << 4) | submode;
1904 } else
1905 cxgbi_skcb_ulp_mode(skb) = 0;
1908 int cxgbi_conn_init_pdu(struct iscsi_task *task, unsigned int offset,
1909 unsigned int count)
1911 struct iscsi_conn *conn = task->conn;
1912 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1913 struct sk_buff *skb = tdata->skb;
1914 unsigned int datalen = count;
1915 int i, padlen = iscsi_padding(count);
1916 struct page *pg;
1918 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1919 "task 0x%p,0x%p, skb 0x%p, 0x%x,0x%x,0x%x, %u+%u.\n",
1920 task, task->sc, skb, (*skb->data) & ISCSI_OPCODE_MASK,
1921 ntohl(task->cmdsn), ntohl(task->hdr->itt), offset, count);
1923 skb_put(skb, task->hdr_len);
1924 tx_skb_setmode(skb, conn->hdrdgst_en, datalen ? conn->datadgst_en : 0);
1925 if (!count)
1926 return 0;
1928 if (task->sc) {
1929 struct scsi_data_buffer *sdb = scsi_out(task->sc);
1930 struct scatterlist *sg = NULL;
1931 int err;
1933 tdata->offset = offset;
1934 tdata->count = count;
1935 err = sgl_seek_offset(
1936 sdb->table.sgl, sdb->table.nents,
1937 tdata->offset, &tdata->sgoffset, &sg);
1938 if (err < 0) {
1939 pr_warn("tpdu, sgl %u, bad offset %u/%u.\n",
1940 sdb->table.nents, tdata->offset, sdb->length);
1941 return err;
1943 err = sgl_read_to_frags(sg, tdata->sgoffset, tdata->count,
1944 tdata->frags, MAX_PDU_FRAGS);
1945 if (err < 0) {
1946 pr_warn("tpdu, sgl %u, bad offset %u + %u.\n",
1947 sdb->table.nents, tdata->offset, tdata->count);
1948 return err;
1950 tdata->nr_frags = err;
1952 if (tdata->nr_frags > MAX_SKB_FRAGS ||
1953 (padlen && tdata->nr_frags == MAX_SKB_FRAGS)) {
1954 char *dst = skb->data + task->hdr_len;
1955 struct page_frag *frag = tdata->frags;
1957 /* data fits in the skb's headroom */
1958 for (i = 0; i < tdata->nr_frags; i++, frag++) {
1959 char *src = kmap_atomic(frag->page,
1960 KM_SOFTIRQ0);
1962 memcpy(dst, src+frag->offset, frag->size);
1963 dst += frag->size;
1964 kunmap_atomic(src, KM_SOFTIRQ0);
1966 if (padlen) {
1967 memset(dst, 0, padlen);
1968 padlen = 0;
1970 skb_put(skb, count + padlen);
1971 } else {
1972 /* data fit into frag_list */
1973 for (i = 0; i < tdata->nr_frags; i++) {
1974 __skb_fill_page_desc(skb, i,
1975 tdata->frags[i].page,
1976 tdata->frags[i].offset,
1977 tdata->frags[i].size);
1978 skb_frag_ref(skb, i);
1980 skb_shinfo(skb)->nr_frags = tdata->nr_frags;
1981 skb->len += count;
1982 skb->data_len += count;
1983 skb->truesize += count;
1986 } else {
1987 pg = virt_to_page(task->data);
1989 get_page(pg);
1990 skb_fill_page_desc(skb, 0, pg, offset_in_page(task->data),
1991 count);
1992 skb->len += count;
1993 skb->data_len += count;
1994 skb->truesize += count;
1997 if (padlen) {
1998 i = skb_shinfo(skb)->nr_frags;
1999 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
2000 virt_to_page(padding), offset_in_page(padding),
2001 padlen);
2003 skb->data_len += padlen;
2004 skb->truesize += padlen;
2005 skb->len += padlen;
2008 return 0;
2010 EXPORT_SYMBOL_GPL(cxgbi_conn_init_pdu);
2012 int cxgbi_conn_xmit_pdu(struct iscsi_task *task)
2014 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
2015 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2016 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
2017 struct sk_buff *skb = tdata->skb;
2018 unsigned int datalen;
2019 int err;
2021 if (!skb) {
2022 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2023 "task 0x%p, skb NULL.\n", task);
2024 return 0;
2027 datalen = skb->data_len;
2028 tdata->skb = NULL;
2029 err = cxgbi_sock_send_pdus(cconn->cep->csk, skb);
2030 if (err > 0) {
2031 int pdulen = err;
2033 log_debug(1 << CXGBI_DBG_PDU_TX,
2034 "task 0x%p,0x%p, skb 0x%p, len %u/%u, rv %d.\n",
2035 task, task->sc, skb, skb->len, skb->data_len, err);
2037 if (task->conn->hdrdgst_en)
2038 pdulen += ISCSI_DIGEST_SIZE;
2040 if (datalen && task->conn->datadgst_en)
2041 pdulen += ISCSI_DIGEST_SIZE;
2043 task->conn->txdata_octets += pdulen;
2044 return 0;
2047 if (err == -EAGAIN || err == -ENOBUFS) {
2048 log_debug(1 << CXGBI_DBG_PDU_TX,
2049 "task 0x%p, skb 0x%p, len %u/%u, %d EAGAIN.\n",
2050 task, skb, skb->len, skb->data_len, err);
2051 /* reset skb to send when we are called again */
2052 tdata->skb = skb;
2053 return err;
2056 kfree_skb(skb);
2057 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2058 "itt 0x%x, skb 0x%p, len %u/%u, xmit err %d.\n",
2059 task->itt, skb, skb->len, skb->data_len, err);
2060 iscsi_conn_printk(KERN_ERR, task->conn, "xmit err %d.\n", err);
2061 iscsi_conn_failure(task->conn, ISCSI_ERR_XMIT_FAILED);
2062 return err;
2064 EXPORT_SYMBOL_GPL(cxgbi_conn_xmit_pdu);
2066 void cxgbi_cleanup_task(struct iscsi_task *task)
2068 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
2070 log_debug(1 << CXGBI_DBG_ISCSI,
2071 "task 0x%p, skb 0x%p, itt 0x%x.\n",
2072 task, tdata->skb, task->hdr_itt);
2074 /* never reached the xmit task callout */
2075 if (tdata->skb)
2076 __kfree_skb(tdata->skb);
2077 memset(tdata, 0, sizeof(*tdata));
2079 task_release_itt(task, task->hdr_itt);
2080 iscsi_tcp_cleanup_task(task);
2082 EXPORT_SYMBOL_GPL(cxgbi_cleanup_task);
2084 void cxgbi_get_conn_stats(struct iscsi_cls_conn *cls_conn,
2085 struct iscsi_stats *stats)
2087 struct iscsi_conn *conn = cls_conn->dd_data;
2089 stats->txdata_octets = conn->txdata_octets;
2090 stats->rxdata_octets = conn->rxdata_octets;
2091 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2092 stats->dataout_pdus = conn->dataout_pdus_cnt;
2093 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2094 stats->datain_pdus = conn->datain_pdus_cnt;
2095 stats->r2t_pdus = conn->r2t_pdus_cnt;
2096 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2097 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2098 stats->digest_err = 0;
2099 stats->timeout_err = 0;
2100 stats->custom_length = 1;
2101 strcpy(stats->custom[0].desc, "eh_abort_cnt");
2102 stats->custom[0].value = conn->eh_abort_cnt;
2104 EXPORT_SYMBOL_GPL(cxgbi_get_conn_stats);
2106 static int cxgbi_conn_max_xmit_dlength(struct iscsi_conn *conn)
2108 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2109 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2110 struct cxgbi_device *cdev = cconn->chba->cdev;
2111 unsigned int headroom = SKB_MAX_HEAD(cdev->skb_tx_rsvd);
2112 unsigned int max_def = 512 * MAX_SKB_FRAGS;
2113 unsigned int max = max(max_def, headroom);
2115 max = min(cconn->chba->cdev->tx_max_size, max);
2116 if (conn->max_xmit_dlength)
2117 conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
2118 else
2119 conn->max_xmit_dlength = max;
2120 cxgbi_align_pdu_size(conn->max_xmit_dlength);
2122 return 0;
2125 static int cxgbi_conn_max_recv_dlength(struct iscsi_conn *conn)
2127 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2128 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2129 unsigned int max = cconn->chba->cdev->rx_max_size;
2131 cxgbi_align_pdu_size(max);
2133 if (conn->max_recv_dlength) {
2134 if (conn->max_recv_dlength > max) {
2135 pr_err("MaxRecvDataSegmentLength %u > %u.\n",
2136 conn->max_recv_dlength, max);
2137 return -EINVAL;
2139 conn->max_recv_dlength = min(conn->max_recv_dlength, max);
2140 cxgbi_align_pdu_size(conn->max_recv_dlength);
2141 } else
2142 conn->max_recv_dlength = max;
2144 return 0;
2147 int cxgbi_set_conn_param(struct iscsi_cls_conn *cls_conn,
2148 enum iscsi_param param, char *buf, int buflen)
2150 struct iscsi_conn *conn = cls_conn->dd_data;
2151 struct iscsi_session *session = conn->session;
2152 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2153 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2154 struct cxgbi_sock *csk = cconn->cep->csk;
2155 int value, err = 0;
2157 log_debug(1 << CXGBI_DBG_ISCSI,
2158 "cls_conn 0x%p, param %d, buf(%d) %s.\n",
2159 cls_conn, param, buflen, buf);
2161 switch (param) {
2162 case ISCSI_PARAM_HDRDGST_EN:
2163 err = iscsi_set_param(cls_conn, param, buf, buflen);
2164 if (!err && conn->hdrdgst_en)
2165 err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
2166 conn->hdrdgst_en,
2167 conn->datadgst_en, 0);
2168 break;
2169 case ISCSI_PARAM_DATADGST_EN:
2170 err = iscsi_set_param(cls_conn, param, buf, buflen);
2171 if (!err && conn->datadgst_en)
2172 err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
2173 conn->hdrdgst_en,
2174 conn->datadgst_en, 0);
2175 break;
2176 case ISCSI_PARAM_MAX_R2T:
2177 sscanf(buf, "%d", &value);
2178 if (value <= 0 || !is_power_of_2(value))
2179 return -EINVAL;
2180 if (session->max_r2t == value)
2181 break;
2182 iscsi_tcp_r2tpool_free(session);
2183 err = iscsi_set_param(cls_conn, param, buf, buflen);
2184 if (!err && iscsi_tcp_r2tpool_alloc(session))
2185 return -ENOMEM;
2186 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2187 err = iscsi_set_param(cls_conn, param, buf, buflen);
2188 if (!err)
2189 err = cxgbi_conn_max_recv_dlength(conn);
2190 break;
2191 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2192 err = iscsi_set_param(cls_conn, param, buf, buflen);
2193 if (!err)
2194 err = cxgbi_conn_max_xmit_dlength(conn);
2195 break;
2196 default:
2197 return iscsi_set_param(cls_conn, param, buf, buflen);
2199 return err;
2201 EXPORT_SYMBOL_GPL(cxgbi_set_conn_param);
2203 int cxgbi_get_ep_param(struct iscsi_endpoint *ep, enum iscsi_param param,
2204 char *buf)
2206 struct cxgbi_endpoint *cep = ep->dd_data;
2207 struct cxgbi_sock *csk;
2208 int len;
2210 log_debug(1 << CXGBI_DBG_ISCSI,
2211 "cls_conn 0x%p, param %d.\n", ep, param);
2213 switch (param) {
2214 case ISCSI_PARAM_CONN_PORT:
2215 case ISCSI_PARAM_CONN_ADDRESS:
2216 if (!cep)
2217 return -ENOTCONN;
2219 csk = cep->csk;
2220 if (!csk)
2221 return -ENOTCONN;
2223 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
2224 &csk->daddr, param, buf);
2225 default:
2226 return -ENOSYS;
2228 return len;
2230 EXPORT_SYMBOL_GPL(cxgbi_get_ep_param);
2232 struct iscsi_cls_conn *
2233 cxgbi_create_conn(struct iscsi_cls_session *cls_session, u32 cid)
2235 struct iscsi_cls_conn *cls_conn;
2236 struct iscsi_conn *conn;
2237 struct iscsi_tcp_conn *tcp_conn;
2238 struct cxgbi_conn *cconn;
2240 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
2241 if (!cls_conn)
2242 return NULL;
2244 conn = cls_conn->dd_data;
2245 tcp_conn = conn->dd_data;
2246 cconn = tcp_conn->dd_data;
2247 cconn->iconn = conn;
2249 log_debug(1 << CXGBI_DBG_ISCSI,
2250 "cid %u(0x%x), cls 0x%p,0x%p, conn 0x%p,0x%p,0x%p.\n",
2251 cid, cid, cls_session, cls_conn, conn, tcp_conn, cconn);
2253 return cls_conn;
2255 EXPORT_SYMBOL_GPL(cxgbi_create_conn);
2257 int cxgbi_bind_conn(struct iscsi_cls_session *cls_session,
2258 struct iscsi_cls_conn *cls_conn,
2259 u64 transport_eph, int is_leading)
2261 struct iscsi_conn *conn = cls_conn->dd_data;
2262 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2263 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2264 struct iscsi_endpoint *ep;
2265 struct cxgbi_endpoint *cep;
2266 struct cxgbi_sock *csk;
2267 int err;
2269 ep = iscsi_lookup_endpoint(transport_eph);
2270 if (!ep)
2271 return -EINVAL;
2273 /* setup ddp pagesize */
2274 cep = ep->dd_data;
2275 csk = cep->csk;
2276 err = csk->cdev->csk_ddp_setup_pgidx(csk, csk->tid, page_idx, 0);
2277 if (err < 0)
2278 return err;
2280 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
2281 if (err)
2282 return -EINVAL;
2284 /* calculate the tag idx bits needed for this conn based on cmds_max */
2285 cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
2287 write_lock_bh(&csk->callback_lock);
2288 csk->user_data = conn;
2289 cconn->chba = cep->chba;
2290 cconn->cep = cep;
2291 cep->cconn = cconn;
2292 write_unlock_bh(&csk->callback_lock);
2294 cxgbi_conn_max_xmit_dlength(conn);
2295 cxgbi_conn_max_recv_dlength(conn);
2297 log_debug(1 << CXGBI_DBG_ISCSI,
2298 "cls 0x%p,0x%p, ep 0x%p, cconn 0x%p, csk 0x%p.\n",
2299 cls_session, cls_conn, ep, cconn, csk);
2300 /* init recv engine */
2301 iscsi_tcp_hdr_recv_prep(tcp_conn);
2303 return 0;
2305 EXPORT_SYMBOL_GPL(cxgbi_bind_conn);
2307 struct iscsi_cls_session *cxgbi_create_session(struct iscsi_endpoint *ep,
2308 u16 cmds_max, u16 qdepth,
2309 u32 initial_cmdsn)
2311 struct cxgbi_endpoint *cep;
2312 struct cxgbi_hba *chba;
2313 struct Scsi_Host *shost;
2314 struct iscsi_cls_session *cls_session;
2315 struct iscsi_session *session;
2317 if (!ep) {
2318 pr_err("missing endpoint.\n");
2319 return NULL;
2322 cep = ep->dd_data;
2323 chba = cep->chba;
2324 shost = chba->shost;
2326 BUG_ON(chba != iscsi_host_priv(shost));
2328 cls_session = iscsi_session_setup(chba->cdev->itp, shost,
2329 cmds_max, 0,
2330 sizeof(struct iscsi_tcp_task) +
2331 sizeof(struct cxgbi_task_data),
2332 initial_cmdsn, ISCSI_MAX_TARGET);
2333 if (!cls_session)
2334 return NULL;
2336 session = cls_session->dd_data;
2337 if (iscsi_tcp_r2tpool_alloc(session))
2338 goto remove_session;
2340 log_debug(1 << CXGBI_DBG_ISCSI,
2341 "ep 0x%p, cls sess 0x%p.\n", ep, cls_session);
2342 return cls_session;
2344 remove_session:
2345 iscsi_session_teardown(cls_session);
2346 return NULL;
2348 EXPORT_SYMBOL_GPL(cxgbi_create_session);
2350 void cxgbi_destroy_session(struct iscsi_cls_session *cls_session)
2352 log_debug(1 << CXGBI_DBG_ISCSI,
2353 "cls sess 0x%p.\n", cls_session);
2355 iscsi_tcp_r2tpool_free(cls_session->dd_data);
2356 iscsi_session_teardown(cls_session);
2358 EXPORT_SYMBOL_GPL(cxgbi_destroy_session);
2360 int cxgbi_set_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2361 char *buf, int buflen)
2363 struct cxgbi_hba *chba = iscsi_host_priv(shost);
2365 if (!chba->ndev) {
2366 shost_printk(KERN_ERR, shost, "Could not get host param. "
2367 "netdev for host not set.\n");
2368 return -ENODEV;
2371 log_debug(1 << CXGBI_DBG_ISCSI,
2372 "shost 0x%p, hba 0x%p,%s, param %d, buf(%d) %s.\n",
2373 shost, chba, chba->ndev->name, param, buflen, buf);
2375 switch (param) {
2376 case ISCSI_HOST_PARAM_IPADDRESS:
2378 __be32 addr = in_aton(buf);
2379 log_debug(1 << CXGBI_DBG_ISCSI,
2380 "hba %s, req. ipv4 %pI4.\n", chba->ndev->name, &addr);
2381 cxgbi_set_iscsi_ipv4(chba, addr);
2382 return 0;
2384 case ISCSI_HOST_PARAM_HWADDRESS:
2385 case ISCSI_HOST_PARAM_NETDEV_NAME:
2386 return 0;
2387 default:
2388 return iscsi_host_set_param(shost, param, buf, buflen);
2391 EXPORT_SYMBOL_GPL(cxgbi_set_host_param);
2393 int cxgbi_get_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2394 char *buf)
2396 struct cxgbi_hba *chba = iscsi_host_priv(shost);
2397 int len = 0;
2399 if (!chba->ndev) {
2400 shost_printk(KERN_ERR, shost, "Could not get host param. "
2401 "netdev for host not set.\n");
2402 return -ENODEV;
2405 log_debug(1 << CXGBI_DBG_ISCSI,
2406 "shost 0x%p, hba 0x%p,%s, param %d.\n",
2407 shost, chba, chba->ndev->name, param);
2409 switch (param) {
2410 case ISCSI_HOST_PARAM_HWADDRESS:
2411 len = sysfs_format_mac(buf, chba->ndev->dev_addr, 6);
2412 break;
2413 case ISCSI_HOST_PARAM_NETDEV_NAME:
2414 len = sprintf(buf, "%s\n", chba->ndev->name);
2415 break;
2416 case ISCSI_HOST_PARAM_IPADDRESS:
2418 __be32 addr;
2420 addr = cxgbi_get_iscsi_ipv4(chba);
2421 len = sprintf(buf, "%pI4", &addr);
2422 log_debug(1 << CXGBI_DBG_ISCSI,
2423 "hba %s, ipv4 %pI4.\n", chba->ndev->name, &addr);
2424 break;
2426 default:
2427 return iscsi_host_get_param(shost, param, buf);
2430 return len;
2432 EXPORT_SYMBOL_GPL(cxgbi_get_host_param);
2434 struct iscsi_endpoint *cxgbi_ep_connect(struct Scsi_Host *shost,
2435 struct sockaddr *dst_addr,
2436 int non_blocking)
2438 struct iscsi_endpoint *ep;
2439 struct cxgbi_endpoint *cep;
2440 struct cxgbi_hba *hba = NULL;
2441 struct cxgbi_sock *csk;
2442 int err = -EINVAL;
2444 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2445 "shost 0x%p, non_blocking %d, dst_addr 0x%p.\n",
2446 shost, non_blocking, dst_addr);
2448 if (shost) {
2449 hba = iscsi_host_priv(shost);
2450 if (!hba) {
2451 pr_info("shost 0x%p, priv NULL.\n", shost);
2452 goto err_out;
2456 csk = cxgbi_check_route(dst_addr);
2457 if (IS_ERR(csk))
2458 return (struct iscsi_endpoint *)csk;
2459 cxgbi_sock_get(csk);
2461 if (!hba)
2462 hba = csk->cdev->hbas[csk->port_id];
2463 else if (hba != csk->cdev->hbas[csk->port_id]) {
2464 pr_info("Could not connect through requested host %u"
2465 "hba 0x%p != 0x%p (%u).\n",
2466 shost->host_no, hba,
2467 csk->cdev->hbas[csk->port_id], csk->port_id);
2468 err = -ENOSPC;
2469 goto release_conn;
2472 err = sock_get_port(csk);
2473 if (err)
2474 goto release_conn;
2476 cxgbi_sock_set_state(csk, CTP_CONNECTING);
2477 err = csk->cdev->csk_init_act_open(csk);
2478 if (err)
2479 goto release_conn;
2481 if (cxgbi_sock_is_closing(csk)) {
2482 err = -ENOSPC;
2483 pr_info("csk 0x%p is closing.\n", csk);
2484 goto release_conn;
2487 ep = iscsi_create_endpoint(sizeof(*cep));
2488 if (!ep) {
2489 err = -ENOMEM;
2490 pr_info("iscsi alloc ep, OOM.\n");
2491 goto release_conn;
2494 cep = ep->dd_data;
2495 cep->csk = csk;
2496 cep->chba = hba;
2498 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2499 "ep 0x%p, cep 0x%p, csk 0x%p, hba 0x%p,%s.\n",
2500 ep, cep, csk, hba, hba->ndev->name);
2501 return ep;
2503 release_conn:
2504 cxgbi_sock_put(csk);
2505 cxgbi_sock_closed(csk);
2506 err_out:
2507 return ERR_PTR(err);
2509 EXPORT_SYMBOL_GPL(cxgbi_ep_connect);
2511 int cxgbi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
2513 struct cxgbi_endpoint *cep = ep->dd_data;
2514 struct cxgbi_sock *csk = cep->csk;
2516 if (!cxgbi_sock_is_established(csk))
2517 return 0;
2518 return 1;
2520 EXPORT_SYMBOL_GPL(cxgbi_ep_poll);
2522 void cxgbi_ep_disconnect(struct iscsi_endpoint *ep)
2524 struct cxgbi_endpoint *cep = ep->dd_data;
2525 struct cxgbi_conn *cconn = cep->cconn;
2526 struct cxgbi_sock *csk = cep->csk;
2528 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2529 "ep 0x%p, cep 0x%p, cconn 0x%p, csk 0x%p,%u,0x%lx.\n",
2530 ep, cep, cconn, csk, csk->state, csk->flags);
2532 if (cconn && cconn->iconn) {
2533 iscsi_suspend_tx(cconn->iconn);
2534 write_lock_bh(&csk->callback_lock);
2535 cep->csk->user_data = NULL;
2536 cconn->cep = NULL;
2537 write_unlock_bh(&csk->callback_lock);
2539 iscsi_destroy_endpoint(ep);
2541 if (likely(csk->state >= CTP_ESTABLISHED))
2542 need_active_close(csk);
2543 else
2544 cxgbi_sock_closed(csk);
2546 cxgbi_sock_put(csk);
2548 EXPORT_SYMBOL_GPL(cxgbi_ep_disconnect);
2550 int cxgbi_iscsi_init(struct iscsi_transport *itp,
2551 struct scsi_transport_template **stt)
2553 *stt = iscsi_register_transport(itp);
2554 if (*stt == NULL) {
2555 pr_err("unable to register %s transport 0x%p.\n",
2556 itp->name, itp);
2557 return -ENODEV;
2559 log_debug(1 << CXGBI_DBG_ISCSI,
2560 "%s, registered iscsi transport 0x%p.\n",
2561 itp->name, stt);
2562 return 0;
2564 EXPORT_SYMBOL_GPL(cxgbi_iscsi_init);
2566 void cxgbi_iscsi_cleanup(struct iscsi_transport *itp,
2567 struct scsi_transport_template **stt)
2569 if (*stt) {
2570 log_debug(1 << CXGBI_DBG_ISCSI,
2571 "de-register transport 0x%p, %s, stt 0x%p.\n",
2572 itp, itp->name, *stt);
2573 *stt = NULL;
2574 iscsi_unregister_transport(itp);
2577 EXPORT_SYMBOL_GPL(cxgbi_iscsi_cleanup);
2579 umode_t cxgbi_attr_is_visible(int param_type, int param)
2581 switch (param_type) {
2582 case ISCSI_HOST_PARAM:
2583 switch (param) {
2584 case ISCSI_HOST_PARAM_NETDEV_NAME:
2585 case ISCSI_HOST_PARAM_HWADDRESS:
2586 case ISCSI_HOST_PARAM_IPADDRESS:
2587 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2588 return S_IRUGO;
2589 default:
2590 return 0;
2592 case ISCSI_PARAM:
2593 switch (param) {
2594 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2595 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2596 case ISCSI_PARAM_HDRDGST_EN:
2597 case ISCSI_PARAM_DATADGST_EN:
2598 case ISCSI_PARAM_CONN_ADDRESS:
2599 case ISCSI_PARAM_CONN_PORT:
2600 case ISCSI_PARAM_EXP_STATSN:
2601 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2602 case ISCSI_PARAM_PERSISTENT_PORT:
2603 case ISCSI_PARAM_PING_TMO:
2604 case ISCSI_PARAM_RECV_TMO:
2605 case ISCSI_PARAM_INITIAL_R2T_EN:
2606 case ISCSI_PARAM_MAX_R2T:
2607 case ISCSI_PARAM_IMM_DATA_EN:
2608 case ISCSI_PARAM_FIRST_BURST:
2609 case ISCSI_PARAM_MAX_BURST:
2610 case ISCSI_PARAM_PDU_INORDER_EN:
2611 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2612 case ISCSI_PARAM_ERL:
2613 case ISCSI_PARAM_TARGET_NAME:
2614 case ISCSI_PARAM_TPGT:
2615 case ISCSI_PARAM_USERNAME:
2616 case ISCSI_PARAM_PASSWORD:
2617 case ISCSI_PARAM_USERNAME_IN:
2618 case ISCSI_PARAM_PASSWORD_IN:
2619 case ISCSI_PARAM_FAST_ABORT:
2620 case ISCSI_PARAM_ABORT_TMO:
2621 case ISCSI_PARAM_LU_RESET_TMO:
2622 case ISCSI_PARAM_TGT_RESET_TMO:
2623 case ISCSI_PARAM_IFACE_NAME:
2624 case ISCSI_PARAM_INITIATOR_NAME:
2625 return S_IRUGO;
2626 default:
2627 return 0;
2631 return 0;
2633 EXPORT_SYMBOL_GPL(cxgbi_attr_is_visible);
2635 static int __init libcxgbi_init_module(void)
2637 sw_tag_idx_bits = (__ilog2_u32(ISCSI_ITT_MASK)) + 1;
2638 sw_tag_age_bits = (__ilog2_u32(ISCSI_AGE_MASK)) + 1;
2640 pr_info("tag itt 0x%x, %u bits, age 0x%x, %u bits.\n",
2641 ISCSI_ITT_MASK, sw_tag_idx_bits,
2642 ISCSI_AGE_MASK, sw_tag_age_bits);
2644 ddp_setup_host_page_size();
2645 return 0;
2648 static void __exit libcxgbi_exit_module(void)
2650 cxgbi_device_unregister_all(0xFF);
2651 return;
2654 module_init(libcxgbi_init_module);
2655 module_exit(libcxgbi_exit_module);