blob: 889199aa1f5b232d83439d67dfc75fee74aeb52f [file] [log] [blame]
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001/*
2 * libcxgbi.c: Chelsio common library for T3/T4 iSCSI driver.
3 *
4 * Copyright (c) 2010 Chelsio Communications, Inc.
5 *
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.
9 *
10 * Written by: Karen Xie (kxie@chelsio.com)
11 * Written by: Rakesh Ranjan (rranjan@chelsio.com)
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
15
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 <net/tcp.h>
29
30static unsigned int dbg_level;
31
32#include "libcxgbi.h"
33
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"
38
39MODULE_AUTHOR("Chelsio Communications, Inc.");
40MODULE_DESCRIPTION(DRV_MODULE_DESC);
41MODULE_VERSION(DRV_MODULE_VERSION);
42MODULE_LICENSE("GPL");
43
44module_param(dbg_level, uint, 0644);
45MODULE_PARM_DESC(dbg_level, "libiscsi debug level (default=0)");
46
47
48/*
49 * cxgbi device management
50 * maintains a list of the cxgbi devices
51 */
52static LIST_HEAD(cdev_list);
53static DEFINE_MUTEX(cdev_mutex);
54
55int cxgbi_device_portmap_create(struct cxgbi_device *cdev, unsigned int base,
56 unsigned int max_conn)
57{
58 struct cxgbi_ports_map *pmap = &cdev->pmap;
59
60 pmap->port_csk = cxgbi_alloc_big_mem(max_conn *
61 sizeof(struct cxgbi_sock *),
62 GFP_KERNEL);
63 if (!pmap->port_csk) {
64 pr_warn("cdev 0x%p, portmap OOM %u.\n", cdev, max_conn);
65 return -ENOMEM;
66 }
67
68 pmap->max_connect = max_conn;
69 pmap->sport_base = base;
70 spin_lock_init(&pmap->lock);
71 return 0;
72}
73EXPORT_SYMBOL_GPL(cxgbi_device_portmap_create);
74
75void cxgbi_device_portmap_cleanup(struct cxgbi_device *cdev)
76{
77 struct cxgbi_ports_map *pmap = &cdev->pmap;
78 struct cxgbi_sock *csk;
79 int i;
80
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",
87 csk, cdev);
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);
92 cxgbi_sock_put(csk);
93 }
94 }
95}
96EXPORT_SYMBOL_GPL(cxgbi_device_portmap_cleanup);
97
98static inline void cxgbi_device_destroy(struct cxgbi_device *cdev)
99{
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);
106 else
107 cxgbi_ddp_cleanup(cdev);
108 if (cdev->ddp)
109 cxgbi_ddp_cleanup(cdev);
110 if (cdev->pmap.max_connect)
111 cxgbi_free_big_mem(cdev->pmap.port_csk);
112 kfree(cdev);
113}
114
115struct cxgbi_device *cxgbi_device_register(unsigned int extra,
116 unsigned int nports)
117{
118 struct cxgbi_device *cdev;
119
120 cdev = kzalloc(sizeof(*cdev) + extra + nports *
121 (sizeof(struct cxgbi_hba *) +
122 sizeof(struct net_device *)),
123 GFP_KERNEL);
124 if (!cdev) {
125 pr_warn("nport %d, OOM.\n", nports);
126 return NULL;
127 }
128 cdev->ports = (struct net_device **)(cdev + 1);
129 cdev->hbas = (struct cxgbi_hba **)(((char*)cdev->ports) + nports *
130 sizeof(struct net_device *));
131 if (extra)
132 cdev->dd_data = ((char *)cdev->hbas) +
133 nports * sizeof(struct cxgbi_hba *);
134 spin_lock_init(&cdev->pmap.lock);
135
136 mutex_lock(&cdev_mutex);
137 list_add_tail(&cdev->list_head, &cdev_list);
138 mutex_unlock(&cdev_mutex);
139
140 log_debug(1 << CXGBI_DBG_DEV,
141 "cdev 0x%p, p# %u.\n", cdev, nports);
142 return cdev;
143}
144EXPORT_SYMBOL_GPL(cxgbi_device_register);
145
146void cxgbi_device_unregister(struct cxgbi_device *cdev)
147{
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);
155}
156EXPORT_SYMBOL_GPL(cxgbi_device_unregister);
157
158void cxgbi_device_unregister_all(unsigned int flag)
159{
160 struct cxgbi_device *cdev, *tmp;
161
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);
171 }
172 }
173 mutex_unlock(&cdev_mutex);
174}
175EXPORT_SYMBOL_GPL(cxgbi_device_unregister_all);
176
177struct cxgbi_device *cxgbi_device_find_by_lldev(void *lldev)
178{
179 struct cxgbi_device *cdev, *tmp;
180
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);
185 return cdev;
186 }
187 }
188 mutex_unlock(&cdev_mutex);
189 log_debug(1 << CXGBI_DBG_DEV,
190 "lldev 0x%p, NO match found.\n", lldev);
191 return NULL;
192}
193EXPORT_SYMBOL_GPL(cxgbi_device_find_by_lldev);
194
195static struct cxgbi_device *cxgbi_device_find_by_netdev(struct net_device *ndev,
196 int *port)
197{
kxie@chelsio.com0b3d8942010-09-23 16:43:23 -0700198 struct net_device *vdev = NULL;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700199 struct cxgbi_device *cdev, *tmp;
200 int i;
201
kxie@chelsio.com0b3d8942010-09-23 16:43:23 -0700202 if (ndev->priv_flags & IFF_802_1Q_VLAN) {
203 vdev = ndev;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700204 ndev = vlan_dev_real_dev(ndev);
kxie@chelsio.com0b3d8942010-09-23 16:43:23 -0700205 log_debug(1 << CXGBI_DBG_DEV,
206 "vlan dev %s -> %s.\n", vdev->name, ndev->name);
207 }
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700208
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]) {
kxie@chelsio.com0b3d8942010-09-23 16:43:23 -0700213 cdev->hbas[i]->vdev = vdev;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700214 mutex_unlock(&cdev_mutex);
215 if (port)
216 *port = i;
217 return cdev;
218 }
219 }
220 }
221 mutex_unlock(&cdev_mutex);
222 log_debug(1 << CXGBI_DBG_DEV,
223 "ndev 0x%p, %s, NO match found.\n", ndev, ndev->name);
224 return NULL;
225}
226
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700227void cxgbi_hbas_remove(struct cxgbi_device *cdev)
228{
229 int i;
230 struct cxgbi_hba *chba;
231
232 log_debug(1 << CXGBI_DBG_DEV,
233 "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
234
235 for (i = 0; i < cdev->nports; i++) {
236 chba = cdev->hbas[i];
237 if (chba) {
238 cdev->hbas[i] = NULL;
239 iscsi_host_remove(chba->shost);
240 pci_dev_put(cdev->pdev);
241 iscsi_host_free(chba->shost);
242 }
243 }
244}
245EXPORT_SYMBOL_GPL(cxgbi_hbas_remove);
246
247int 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)
250{
251 struct cxgbi_hba *chba;
252 struct Scsi_Host *shost;
253 int i, err;
254
255 log_debug(1 << CXGBI_DBG_DEV, "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
256
257 for (i = 0; i < cdev->nports; i++) {
258 shost = iscsi_host_alloc(sht, sizeof(*chba), 1);
259 if (!shost) {
260 pr_info("0x%p, p%d, %s, host alloc failed.\n",
261 cdev, i, cdev->ports[i]->name);
262 err = -ENOMEM;
263 goto err_out;
264 }
265
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;
271
272 chba = iscsi_host_priv(shost);
273 chba->cdev = cdev;
274 chba->ndev = cdev->ports[i];
275 chba->shost = shost;
276
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);
280
281 pci_dev_get(cdev->pdev);
282 err = iscsi_host_add(shost, &cdev->pdev->dev);
283 if (err) {
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);
288 goto err_out;
289 }
290
291 cdev->hbas[i] = chba;
292 }
293
294 return 0;
295
296err_out:
297 cxgbi_hbas_remove(cdev);
298 return err;
299}
300EXPORT_SYMBOL_GPL(cxgbi_hbas_add);
301
302/*
303 * iSCSI offload
304 *
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.
308 *
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.
312 *
313 * If the source port is outside our allocation range, the caller is
314 * responsible for keeping track of their port usage.
315 */
316static int sock_get_port(struct cxgbi_sock *csk)
317{
318 struct cxgbi_device *cdev = csk->cdev;
319 struct cxgbi_ports_map *pmap = &cdev->pmap;
320 unsigned int start;
321 int idx;
322
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;
327 }
328
329 if (csk->saddr.sin_port) {
330 pr_err("source port NON-ZERO %u.\n",
331 ntohs(csk->saddr.sin_port));
332 return -EADDRINUSE;
333 }
334
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;
341 }
342
343 start = idx = pmap->next;
344 do {
345 if (++idx >= pmap->max_connect)
346 idx = 0;
347 if (!pmap->port_csk[idx]) {
348 pmap->used++;
349 csk->saddr.sin_port =
350 htons(pmap->sport_base + idx);
351 pmap->next = idx;
352 pmap->port_csk[idx] = csk;
353 spin_unlock_bh(&pmap->lock);
354 cxgbi_sock_get(csk);
355 log_debug(1 << CXGBI_DBG_SOCK,
356 "cdev 0x%p, p#%u %s, p %u, %u.\n",
357 cdev, csk->port_id,
358 cdev->ports[csk->port_id]->name,
359 pmap->sport_base + idx, pmap->next);
360 return 0;
361 }
362 } while (idx != start);
363 spin_unlock_bh(&pmap->lock);
364
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,
368 pmap->next);
369 return -EADDRNOTAVAIL;
370}
371
372static void sock_put_port(struct cxgbi_sock *csk)
373{
374 struct cxgbi_device *cdev = csk->cdev;
375 struct cxgbi_ports_map *pmap = &cdev->pmap;
376
377 if (csk->saddr.sin_port) {
378 int idx = ntohs(csk->saddr.sin_port) - pmap->sport_base;
379
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",
383 cdev, csk->port_id,
384 cdev->ports[csk->port_id]->name,
385 ntohs(csk->saddr.sin_port));
386 return;
387 }
388
389 spin_lock_bh(&pmap->lock);
390 pmap->port_csk[idx] = NULL;
391 pmap->used--;
392 spin_unlock_bh(&pmap->lock);
393
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);
398
399 cxgbi_sock_put(csk);
400 }
401}
402
403/*
404 * iscsi tcp connection
405 */
406void cxgbi_sock_free_cpl_skbs(struct cxgbi_sock *csk)
407{
408 if (csk->cpl_close) {
409 kfree_skb(csk->cpl_close);
410 csk->cpl_close = NULL;
411 }
412 if (csk->cpl_abort_req) {
413 kfree_skb(csk->cpl_abort_req);
414 csk->cpl_abort_req = NULL;
415 }
416 if (csk->cpl_abort_rpl) {
417 kfree_skb(csk->cpl_abort_rpl);
418 csk->cpl_abort_rpl = NULL;
419 }
420}
421EXPORT_SYMBOL_GPL(cxgbi_sock_free_cpl_skbs);
422
423static struct cxgbi_sock *cxgbi_sock_create(struct cxgbi_device *cdev)
424{
425 struct cxgbi_sock *csk = kzalloc(sizeof(*csk), GFP_NOIO);
426
427 if (!csk) {
428 pr_info("alloc csk %zu failed.\n", sizeof(*csk));
429 return NULL;
430 }
431
432 if (cdev->csk_alloc_cpls(csk) < 0) {
433 pr_info("csk 0x%p, alloc cpls failed.\n", csk);
434 kfree(csk);
435 return NULL;
436 }
437
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);
444 csk->cdev = cdev;
445 csk->flags = 0;
446 cxgbi_sock_set_state(csk, CTP_CLOSED);
447
448 log_debug(1 << CXGBI_DBG_SOCK, "cdev 0x%p, new csk 0x%p.\n", cdev, csk);
449
450 return csk;
451}
452
453static struct rtable *find_route_ipv4(__be32 saddr, __be32 daddr,
454 __be16 sport, __be16 dport, u8 tos)
455{
456 struct rtable *rt;
457 struct flowi fl = {
458 .oif = 0,
459 .nl_u = {
460 .ip4_u = {
461 .daddr = daddr,
462 .saddr = saddr,
463 .tos = tos }
464 },
465 .proto = IPPROTO_TCP,
466 .uli_u = {
467 .ports = {
468 .sport = sport,
469 .dport = dport }
470 }
471 };
472
David S. Millerb23dd4f2011-03-02 14:31:35 -0800473 rt = ip_route_output_flow(&init_net, &fl, NULL);
474 if (IS_ERR(rt))
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700475 return NULL;
476
477 return rt;
478}
479
480static struct cxgbi_sock *cxgbi_check_route(struct sockaddr *dst_addr)
481{
482 struct sockaddr_in *daddr = (struct sockaddr_in *)dst_addr;
483 struct dst_entry *dst;
484 struct net_device *ndev;
485 struct cxgbi_device *cdev;
486 struct rtable *rt = NULL;
487 struct cxgbi_sock *csk = NULL;
488 unsigned int mtu = 0;
489 int port = 0xFFFF;
490 int err = 0;
491
492 if (daddr->sin_family != AF_INET) {
493 pr_info("address family 0x%x NOT supported.\n",
494 daddr->sin_family);
495 err = -EAFNOSUPPORT;
496 goto err_out;
497 }
498
499 rt = find_route_ipv4(0, daddr->sin_addr.s_addr, 0, daddr->sin_port, 0);
500 if (!rt) {
501 pr_info("no route to ipv4 0x%x, port %u.\n",
502 daddr->sin_addr.s_addr, daddr->sin_port);
503 err = -ENETUNREACH;
504 goto err_out;
505 }
506 dst = &rt->dst;
507 ndev = dst->neighbour->dev;
508
509 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
510 pr_info("multi-cast route %pI4, port %u, dev %s.\n",
511 &daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
512 ndev->name);
513 err = -ENETUNREACH;
514 goto rel_rt;
515 }
516
517 if (ndev->flags & IFF_LOOPBACK) {
518 ndev = ip_dev_find(&init_net, daddr->sin_addr.s_addr);
519 mtu = ndev->mtu;
520 pr_info("rt dev %s, loopback -> %s, mtu %u.\n",
521 dst->neighbour->dev->name, ndev->name, mtu);
522 }
523
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700524 cdev = cxgbi_device_find_by_netdev(ndev, &port);
525 if (!cdev) {
526 pr_info("dst %pI4, %s, NOT cxgbi device.\n",
527 &daddr->sin_addr.s_addr, ndev->name);
528 err = -ENETUNREACH;
529 goto rel_rt;
530 }
531 log_debug(1 << CXGBI_DBG_SOCK,
532 "route to %pI4 :%u, ndev p#%d,%s, cdev 0x%p.\n",
533 &daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
534 port, ndev->name, cdev);
535
536 csk = cxgbi_sock_create(cdev);
537 if (!csk) {
538 err = -ENOMEM;
539 goto rel_rt;
540 }
541 csk->cdev = cdev;
542 csk->port_id = port;
543 csk->mtu = mtu;
544 csk->dst = dst;
545 csk->daddr.sin_addr.s_addr = daddr->sin_addr.s_addr;
546 csk->daddr.sin_port = daddr->sin_port;
kxie@chelsio.com0b3d8942010-09-23 16:43:23 -0700547 csk->saddr.sin_addr.s_addr = rt->rt_src;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700548
549 return csk;
550
551rel_rt:
552 ip_rt_put(rt);
553 if (csk)
554 cxgbi_sock_closed(csk);
555err_out:
556 return ERR_PTR(err);
557}
558
559void cxgbi_sock_established(struct cxgbi_sock *csk, unsigned int snd_isn,
560 unsigned int opt)
561{
562 csk->write_seq = csk->snd_nxt = csk->snd_una = snd_isn;
563 dst_confirm(csk->dst);
564 smp_mb();
565 cxgbi_sock_set_state(csk, CTP_ESTABLISHED);
566}
567EXPORT_SYMBOL_GPL(cxgbi_sock_established);
568
569static void cxgbi_inform_iscsi_conn_closing(struct cxgbi_sock *csk)
570{
571 log_debug(1 << CXGBI_DBG_SOCK,
572 "csk 0x%p, state %u, flags 0x%lx, conn 0x%p.\n",
573 csk, csk->state, csk->flags, csk->user_data);
574
575 if (csk->state != CTP_ESTABLISHED) {
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -0700576 read_lock_bh(&csk->callback_lock);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700577 if (csk->user_data)
578 iscsi_conn_failure(csk->user_data,
579 ISCSI_ERR_CONN_FAILED);
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -0700580 read_unlock_bh(&csk->callback_lock);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700581 }
582}
583
584void cxgbi_sock_closed(struct cxgbi_sock *csk)
585{
586 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
587 csk, (csk)->state, (csk)->flags, (csk)->tid);
588 cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
589 if (csk->state == CTP_ACTIVE_OPEN || csk->state == CTP_CLOSED)
590 return;
591 if (csk->saddr.sin_port)
592 sock_put_port(csk);
593 if (csk->dst)
594 dst_release(csk->dst);
595 csk->cdev->csk_release_offload_resources(csk);
596 cxgbi_sock_set_state(csk, CTP_CLOSED);
597 cxgbi_inform_iscsi_conn_closing(csk);
598 cxgbi_sock_put(csk);
599}
600EXPORT_SYMBOL_GPL(cxgbi_sock_closed);
601
602static void need_active_close(struct cxgbi_sock *csk)
603{
604 int data_lost;
605 int close_req = 0;
606
607 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
608 csk, (csk)->state, (csk)->flags, (csk)->tid);
609 spin_lock_bh(&csk->lock);
610 dst_confirm(csk->dst);
611 data_lost = skb_queue_len(&csk->receive_queue);
612 __skb_queue_purge(&csk->receive_queue);
613
614 if (csk->state == CTP_ACTIVE_OPEN)
615 cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
616 else if (csk->state == CTP_ESTABLISHED) {
617 close_req = 1;
618 cxgbi_sock_set_state(csk, CTP_ACTIVE_CLOSE);
619 } else if (csk->state == CTP_PASSIVE_CLOSE) {
620 close_req = 1;
621 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
622 }
623
624 if (close_req) {
625 if (data_lost)
626 csk->cdev->csk_send_abort_req(csk);
627 else
628 csk->cdev->csk_send_close_req(csk);
629 }
630
631 spin_unlock_bh(&csk->lock);
632}
633
634void cxgbi_sock_fail_act_open(struct cxgbi_sock *csk, int errno)
635{
636 pr_info("csk 0x%p,%u,%lx, %pI4:%u-%pI4:%u, err %d.\n",
637 csk, csk->state, csk->flags,
638 &csk->saddr.sin_addr.s_addr, csk->saddr.sin_port,
639 &csk->daddr.sin_addr.s_addr, csk->daddr.sin_port,
640 errno);
641
642 cxgbi_sock_set_state(csk, CTP_CONNECTING);
643 csk->err = errno;
644 cxgbi_sock_closed(csk);
645}
646EXPORT_SYMBOL_GPL(cxgbi_sock_fail_act_open);
647
648void cxgbi_sock_act_open_req_arp_failure(void *handle, struct sk_buff *skb)
649{
650 struct cxgbi_sock *csk = (struct cxgbi_sock *)skb->sk;
651
652 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
653 csk, (csk)->state, (csk)->flags, (csk)->tid);
654 cxgbi_sock_get(csk);
655 spin_lock_bh(&csk->lock);
656 if (csk->state == CTP_ACTIVE_OPEN)
657 cxgbi_sock_fail_act_open(csk, -EHOSTUNREACH);
658 spin_unlock_bh(&csk->lock);
659 cxgbi_sock_put(csk);
660 __kfree_skb(skb);
661}
662EXPORT_SYMBOL_GPL(cxgbi_sock_act_open_req_arp_failure);
663
664void cxgbi_sock_rcv_abort_rpl(struct cxgbi_sock *csk)
665{
666 cxgbi_sock_get(csk);
667 spin_lock_bh(&csk->lock);
668 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
669 if (!cxgbi_sock_flag(csk, CTPF_ABORT_RPL_RCVD))
670 cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_RCVD);
671 else {
672 cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_RCVD);
673 cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_PENDING);
674 if (cxgbi_sock_flag(csk, CTPF_ABORT_REQ_RCVD))
675 pr_err("csk 0x%p,%u,0x%lx,%u,ABT_RPL_RSS.\n",
676 csk, csk->state, csk->flags, csk->tid);
677 cxgbi_sock_closed(csk);
678 }
679 }
680 spin_unlock_bh(&csk->lock);
681 cxgbi_sock_put(csk);
682}
683EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_abort_rpl);
684
685void cxgbi_sock_rcv_peer_close(struct cxgbi_sock *csk)
686{
687 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
688 csk, (csk)->state, (csk)->flags, (csk)->tid);
689 cxgbi_sock_get(csk);
690 spin_lock_bh(&csk->lock);
691
692 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
693 goto done;
694
695 switch (csk->state) {
696 case CTP_ESTABLISHED:
697 cxgbi_sock_set_state(csk, CTP_PASSIVE_CLOSE);
698 break;
699 case CTP_ACTIVE_CLOSE:
700 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
701 break;
702 case CTP_CLOSE_WAIT_1:
703 cxgbi_sock_closed(csk);
704 break;
705 case CTP_ABORTING:
706 break;
707 default:
708 pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
709 csk, csk->state, csk->flags, csk->tid);
710 }
711 cxgbi_inform_iscsi_conn_closing(csk);
712done:
713 spin_unlock_bh(&csk->lock);
714 cxgbi_sock_put(csk);
715}
716EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_peer_close);
717
718void cxgbi_sock_rcv_close_conn_rpl(struct cxgbi_sock *csk, u32 snd_nxt)
719{
720 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
721 csk, (csk)->state, (csk)->flags, (csk)->tid);
722 cxgbi_sock_get(csk);
723 spin_lock_bh(&csk->lock);
724
725 csk->snd_una = snd_nxt - 1;
726 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
727 goto done;
728
729 switch (csk->state) {
730 case CTP_ACTIVE_CLOSE:
731 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_1);
732 break;
733 case CTP_CLOSE_WAIT_1:
734 case CTP_CLOSE_WAIT_2:
735 cxgbi_sock_closed(csk);
736 break;
737 case CTP_ABORTING:
738 break;
739 default:
740 pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
741 csk, csk->state, csk->flags, csk->tid);
742 }
743done:
744 spin_unlock_bh(&csk->lock);
745 cxgbi_sock_put(csk);
746}
747EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_close_conn_rpl);
748
749void cxgbi_sock_rcv_wr_ack(struct cxgbi_sock *csk, unsigned int credits,
750 unsigned int snd_una, int seq_chk)
751{
752 log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
753 "csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, snd_una %u,%d.\n",
754 csk, csk->state, csk->flags, csk->tid, credits,
755 csk->wr_cred, csk->wr_una_cred, snd_una, seq_chk);
756
757 spin_lock_bh(&csk->lock);
758
759 csk->wr_cred += credits;
760 if (csk->wr_una_cred > csk->wr_max_cred - csk->wr_cred)
761 csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
762
763 while (credits) {
764 struct sk_buff *p = cxgbi_sock_peek_wr(csk);
765
766 if (unlikely(!p)) {
767 pr_err("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, empty.\n",
768 csk, csk->state, csk->flags, csk->tid, credits,
769 csk->wr_cred, csk->wr_una_cred);
770 break;
771 }
772
773 if (unlikely(credits < p->csum)) {
774 pr_warn("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, < %u.\n",
775 csk, csk->state, csk->flags, csk->tid,
776 credits, csk->wr_cred, csk->wr_una_cred,
777 p->csum);
778 p->csum -= credits;
779 break;
780 } else {
781 cxgbi_sock_dequeue_wr(csk);
782 credits -= p->csum;
783 kfree_skb(p);
784 }
785 }
786
787 cxgbi_sock_check_wr_invariants(csk);
788
789 if (seq_chk) {
790 if (unlikely(before(snd_una, csk->snd_una))) {
791 pr_warn("csk 0x%p,%u,0x%lx,%u, snd_una %u/%u.",
792 csk, csk->state, csk->flags, csk->tid, snd_una,
793 csk->snd_una);
794 goto done;
795 }
796
797 if (csk->snd_una != snd_una) {
798 csk->snd_una = snd_una;
799 dst_confirm(csk->dst);
800 }
801 }
802
803 if (skb_queue_len(&csk->write_queue)) {
804 if (csk->cdev->csk_push_tx_frames(csk, 0))
805 cxgbi_conn_tx_open(csk);
806 } else
807 cxgbi_conn_tx_open(csk);
808done:
809 spin_unlock_bh(&csk->lock);
810}
811EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_wr_ack);
812
813static unsigned int cxgbi_sock_find_best_mtu(struct cxgbi_sock *csk,
814 unsigned short mtu)
815{
816 int i = 0;
817
818 while (i < csk->cdev->nmtus - 1 && csk->cdev->mtus[i + 1] <= mtu)
819 ++i;
820
821 return i;
822}
823
824unsigned int cxgbi_sock_select_mss(struct cxgbi_sock *csk, unsigned int pmtu)
825{
826 unsigned int idx;
827 struct dst_entry *dst = csk->dst;
828
David S. Miller0dbaee32010-12-13 12:52:14 -0800829 csk->advmss = dst_metric_advmss(dst);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700830
831 if (csk->advmss > pmtu - 40)
832 csk->advmss = pmtu - 40;
833 if (csk->advmss < csk->cdev->mtus[0] - 40)
834 csk->advmss = csk->cdev->mtus[0] - 40;
835 idx = cxgbi_sock_find_best_mtu(csk, csk->advmss + 40);
836
837 return idx;
838}
839EXPORT_SYMBOL_GPL(cxgbi_sock_select_mss);
840
841void cxgbi_sock_skb_entail(struct cxgbi_sock *csk, struct sk_buff *skb)
842{
843 cxgbi_skcb_tcp_seq(skb) = csk->write_seq;
844 __skb_queue_tail(&csk->write_queue, skb);
845}
846EXPORT_SYMBOL_GPL(cxgbi_sock_skb_entail);
847
848void cxgbi_sock_purge_wr_queue(struct cxgbi_sock *csk)
849{
850 struct sk_buff *skb;
851
852 while ((skb = cxgbi_sock_dequeue_wr(csk)) != NULL)
853 kfree_skb(skb);
854}
855EXPORT_SYMBOL_GPL(cxgbi_sock_purge_wr_queue);
856
857void cxgbi_sock_check_wr_invariants(const struct cxgbi_sock *csk)
858{
859 int pending = cxgbi_sock_count_pending_wrs(csk);
860
861 if (unlikely(csk->wr_cred + pending != csk->wr_max_cred))
862 pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
863 csk, csk->tid, csk->wr_cred, pending, csk->wr_max_cred);
864}
865EXPORT_SYMBOL_GPL(cxgbi_sock_check_wr_invariants);
866
867static int cxgbi_sock_send_pdus(struct cxgbi_sock *csk, struct sk_buff *skb)
868{
869 struct cxgbi_device *cdev = csk->cdev;
870 struct sk_buff *next;
871 int err, copied = 0;
872
873 spin_lock_bh(&csk->lock);
874
875 if (csk->state != CTP_ESTABLISHED) {
876 log_debug(1 << CXGBI_DBG_PDU_TX,
877 "csk 0x%p,%u,0x%lx,%u, EAGAIN.\n",
878 csk, csk->state, csk->flags, csk->tid);
879 err = -EAGAIN;
880 goto out_err;
881 }
882
883 if (csk->err) {
884 log_debug(1 << CXGBI_DBG_PDU_TX,
885 "csk 0x%p,%u,0x%lx,%u, EPIPE %d.\n",
886 csk, csk->state, csk->flags, csk->tid, csk->err);
887 err = -EPIPE;
888 goto out_err;
889 }
890
891 if (csk->write_seq - csk->snd_una >= cdev->snd_win) {
892 log_debug(1 << CXGBI_DBG_PDU_TX,
893 "csk 0x%p,%u,0x%lx,%u, FULL %u-%u >= %u.\n",
894 csk, csk->state, csk->flags, csk->tid, csk->write_seq,
895 csk->snd_una, cdev->snd_win);
896 err = -ENOBUFS;
897 goto out_err;
898 }
899
900 while (skb) {
901 int frags = skb_shinfo(skb)->nr_frags +
902 (skb->len != skb->data_len);
903
904 if (unlikely(skb_headroom(skb) < cdev->skb_tx_rsvd)) {
905 pr_err("csk 0x%p, skb head %u < %u.\n",
906 csk, skb_headroom(skb), cdev->skb_tx_rsvd);
907 err = -EINVAL;
908 goto out_err;
909 }
910
911 if (frags >= SKB_WR_LIST_SIZE) {
912 pr_err("csk 0x%p, frags %d, %u,%u >%u.\n",
913 csk, skb_shinfo(skb)->nr_frags, skb->len,
914 skb->data_len, (uint)(SKB_WR_LIST_SIZE));
915 err = -EINVAL;
916 goto out_err;
917 }
918
919 next = skb->next;
920 skb->next = NULL;
921 cxgbi_skcb_set_flag(skb, SKCBF_TX_NEED_HDR);
922 cxgbi_sock_skb_entail(csk, skb);
923 copied += skb->len;
924 csk->write_seq += skb->len +
925 cxgbi_ulp_extra_len(cxgbi_skcb_ulp_mode(skb));
926 skb = next;
927 }
928done:
929 if (likely(skb_queue_len(&csk->write_queue)))
930 cdev->csk_push_tx_frames(csk, 1);
931 spin_unlock_bh(&csk->lock);
932 return copied;
933
934out_err:
935 if (copied == 0 && err == -EPIPE)
936 copied = csk->err ? csk->err : -EPIPE;
937 else
938 copied = err;
939 goto done;
940}
941
942/*
943 * Direct Data Placement -
944 * Directly place the iSCSI Data-In or Data-Out PDU's payload into pre-posted
945 * final destination host-memory buffers based on the Initiator Task Tag (ITT)
946 * in Data-In or Target Task Tag (TTT) in Data-Out PDUs.
947 * The host memory address is programmed into h/w in the format of pagepod
948 * entries.
949 * The location of the pagepod entry is encoded into ddp tag which is used as
950 * the base for ITT/TTT.
951 */
952
953static unsigned char ddp_page_order[DDP_PGIDX_MAX] = {0, 1, 2, 4};
954static unsigned char ddp_page_shift[DDP_PGIDX_MAX] = {12, 13, 14, 16};
955static unsigned char page_idx = DDP_PGIDX_MAX;
956
957static unsigned char sw_tag_idx_bits;
958static unsigned char sw_tag_age_bits;
959
960/*
961 * Direct-Data Placement page size adjustment
962 */
963static int ddp_adjust_page_table(void)
964{
965 int i;
966 unsigned int base_order, order;
967
968 if (PAGE_SIZE < (1UL << ddp_page_shift[0])) {
969 pr_info("PAGE_SIZE 0x%lx too small, min 0x%lx\n",
970 PAGE_SIZE, 1UL << ddp_page_shift[0]);
971 return -EINVAL;
972 }
973
974 base_order = get_order(1UL << ddp_page_shift[0]);
975 order = get_order(1UL << PAGE_SHIFT);
976
977 for (i = 0; i < DDP_PGIDX_MAX; i++) {
978 /* first is the kernel page size, then just doubling */
979 ddp_page_order[i] = order - base_order + i;
980 ddp_page_shift[i] = PAGE_SHIFT + i;
981 }
982 return 0;
983}
984
985static int ddp_find_page_index(unsigned long pgsz)
986{
987 int i;
988
989 for (i = 0; i < DDP_PGIDX_MAX; i++) {
990 if (pgsz == (1UL << ddp_page_shift[i]))
991 return i;
992 }
993 pr_info("ddp page size %lu not supported.\n", pgsz);
994 return DDP_PGIDX_MAX;
995}
996
997static void ddp_setup_host_page_size(void)
998{
999 if (page_idx == DDP_PGIDX_MAX) {
1000 page_idx = ddp_find_page_index(PAGE_SIZE);
1001
1002 if (page_idx == DDP_PGIDX_MAX) {
1003 pr_info("system PAGE %lu, update hw.\n", PAGE_SIZE);
1004 if (ddp_adjust_page_table() < 0) {
1005 pr_info("PAGE %lu, disable ddp.\n", PAGE_SIZE);
1006 return;
1007 }
1008 page_idx = ddp_find_page_index(PAGE_SIZE);
1009 }
1010 pr_info("system PAGE %lu, ddp idx %u.\n", PAGE_SIZE, page_idx);
1011 }
1012}
1013
1014void cxgbi_ddp_page_size_factor(int *pgsz_factor)
1015{
1016 int i;
1017
1018 for (i = 0; i < DDP_PGIDX_MAX; i++)
1019 pgsz_factor[i] = ddp_page_order[i];
1020}
1021EXPORT_SYMBOL_GPL(cxgbi_ddp_page_size_factor);
1022
1023/*
1024 * DDP setup & teardown
1025 */
1026
1027void cxgbi_ddp_ppod_set(struct cxgbi_pagepod *ppod,
1028 struct cxgbi_pagepod_hdr *hdr,
1029 struct cxgbi_gather_list *gl, unsigned int gidx)
1030{
1031 int i;
1032
1033 memcpy(ppod, hdr, sizeof(*hdr));
1034 for (i = 0; i < (PPOD_PAGES_MAX + 1); i++, gidx++) {
1035 ppod->addr[i] = gidx < gl->nelem ?
1036 cpu_to_be64(gl->phys_addr[gidx]) : 0ULL;
1037 }
1038}
1039EXPORT_SYMBOL_GPL(cxgbi_ddp_ppod_set);
1040
1041void cxgbi_ddp_ppod_clear(struct cxgbi_pagepod *ppod)
1042{
1043 memset(ppod, 0, sizeof(*ppod));
1044}
1045EXPORT_SYMBOL_GPL(cxgbi_ddp_ppod_clear);
1046
1047static inline int ddp_find_unused_entries(struct cxgbi_ddp_info *ddp,
1048 unsigned int start, unsigned int max,
1049 unsigned int count,
1050 struct cxgbi_gather_list *gl)
1051{
1052 unsigned int i, j, k;
1053
1054 /* not enough entries */
1055 if ((max - start) < count) {
1056 log_debug(1 << CXGBI_DBG_DDP,
1057 "NOT enough entries %u+%u < %u.\n", start, count, max);
1058 return -EBUSY;
1059 }
1060
1061 max -= count;
1062 spin_lock(&ddp->map_lock);
1063 for (i = start; i < max;) {
1064 for (j = 0, k = i; j < count; j++, k++) {
1065 if (ddp->gl_map[k])
1066 break;
1067 }
1068 if (j == count) {
1069 for (j = 0, k = i; j < count; j++, k++)
1070 ddp->gl_map[k] = gl;
1071 spin_unlock(&ddp->map_lock);
1072 return i;
1073 }
1074 i += j + 1;
1075 }
1076 spin_unlock(&ddp->map_lock);
1077 log_debug(1 << CXGBI_DBG_DDP,
1078 "NO suitable entries %u available.\n", count);
1079 return -EBUSY;
1080}
1081
1082static inline void ddp_unmark_entries(struct cxgbi_ddp_info *ddp,
1083 int start, int count)
1084{
1085 spin_lock(&ddp->map_lock);
1086 memset(&ddp->gl_map[start], 0,
1087 count * sizeof(struct cxgbi_gather_list *));
1088 spin_unlock(&ddp->map_lock);
1089}
1090
1091static inline void ddp_gl_unmap(struct pci_dev *pdev,
1092 struct cxgbi_gather_list *gl)
1093{
1094 int i;
1095
1096 for (i = 0; i < gl->nelem; i++)
1097 dma_unmap_page(&pdev->dev, gl->phys_addr[i], PAGE_SIZE,
1098 PCI_DMA_FROMDEVICE);
1099}
1100
1101static inline int ddp_gl_map(struct pci_dev *pdev,
1102 struct cxgbi_gather_list *gl)
1103{
1104 int i;
1105
1106 for (i = 0; i < gl->nelem; i++) {
1107 gl->phys_addr[i] = dma_map_page(&pdev->dev, gl->pages[i], 0,
1108 PAGE_SIZE,
1109 PCI_DMA_FROMDEVICE);
1110 if (unlikely(dma_mapping_error(&pdev->dev, gl->phys_addr[i]))) {
1111 log_debug(1 << CXGBI_DBG_DDP,
1112 "page %d 0x%p, 0x%p dma mapping err.\n",
1113 i, gl->pages[i], pdev);
1114 goto unmap;
1115 }
1116 }
1117 return i;
1118unmap:
1119 if (i) {
1120 unsigned int nelem = gl->nelem;
1121
1122 gl->nelem = i;
1123 ddp_gl_unmap(pdev, gl);
1124 gl->nelem = nelem;
1125 }
1126 return -EINVAL;
1127}
1128
1129static void ddp_release_gl(struct cxgbi_gather_list *gl,
1130 struct pci_dev *pdev)
1131{
1132 ddp_gl_unmap(pdev, gl);
1133 kfree(gl);
1134}
1135
1136static struct cxgbi_gather_list *ddp_make_gl(unsigned int xferlen,
1137 struct scatterlist *sgl,
1138 unsigned int sgcnt,
1139 struct pci_dev *pdev,
1140 gfp_t gfp)
1141{
1142 struct cxgbi_gather_list *gl;
1143 struct scatterlist *sg = sgl;
1144 struct page *sgpage = sg_page(sg);
1145 unsigned int sglen = sg->length;
1146 unsigned int sgoffset = sg->offset;
1147 unsigned int npages = (xferlen + sgoffset + PAGE_SIZE - 1) >>
1148 PAGE_SHIFT;
1149 int i = 1, j = 0;
1150
1151 if (xferlen < DDP_THRESHOLD) {
1152 log_debug(1 << CXGBI_DBG_DDP,
1153 "xfer %u < threshold %u, no ddp.\n",
1154 xferlen, DDP_THRESHOLD);
1155 return NULL;
1156 }
1157
1158 gl = kzalloc(sizeof(struct cxgbi_gather_list) +
1159 npages * (sizeof(dma_addr_t) +
1160 sizeof(struct page *)), gfp);
1161 if (!gl) {
1162 log_debug(1 << CXGBI_DBG_DDP,
1163 "xfer %u, %u pages, OOM.\n", xferlen, npages);
1164 return NULL;
1165 }
1166
1167 log_debug(1 << CXGBI_DBG_DDP,
1168 "xfer %u, sgl %u, gl max %u.\n", xferlen, sgcnt, npages);
1169
1170 gl->pages = (struct page **)&gl->phys_addr[npages];
1171 gl->nelem = npages;
1172 gl->length = xferlen;
1173 gl->offset = sgoffset;
1174 gl->pages[0] = sgpage;
1175
1176 for (i = 1, sg = sg_next(sgl), j = 0; i < sgcnt;
1177 i++, sg = sg_next(sg)) {
1178 struct page *page = sg_page(sg);
1179
1180 if (sgpage == page && sg->offset == sgoffset + sglen)
1181 sglen += sg->length;
1182 else {
1183 /* make sure the sgl is fit for ddp:
1184 * each has the same page size, and
1185 * all of the middle pages are used completely
1186 */
1187 if ((j && sgoffset) || ((i != sgcnt - 1) &&
1188 ((sglen + sgoffset) & ~PAGE_MASK))) {
1189 log_debug(1 << CXGBI_DBG_DDP,
1190 "page %d/%u, %u + %u.\n",
1191 i, sgcnt, sgoffset, sglen);
1192 goto error_out;
1193 }
1194
1195 j++;
1196 if (j == gl->nelem || sg->offset) {
1197 log_debug(1 << CXGBI_DBG_DDP,
1198 "page %d/%u, offset %u.\n",
1199 j, gl->nelem, sg->offset);
1200 goto error_out;
1201 }
1202 gl->pages[j] = page;
1203 sglen = sg->length;
1204 sgoffset = sg->offset;
1205 sgpage = page;
1206 }
1207 }
1208 gl->nelem = ++j;
1209
1210 if (ddp_gl_map(pdev, gl) < 0)
1211 goto error_out;
1212
1213 return gl;
1214
1215error_out:
1216 kfree(gl);
1217 return NULL;
1218}
1219
1220static void ddp_tag_release(struct cxgbi_hba *chba, u32 tag)
1221{
1222 struct cxgbi_device *cdev = chba->cdev;
1223 struct cxgbi_ddp_info *ddp = cdev->ddp;
1224 u32 idx;
1225
1226 idx = (tag >> PPOD_IDX_SHIFT) & ddp->idx_mask;
1227 if (idx < ddp->nppods) {
1228 struct cxgbi_gather_list *gl = ddp->gl_map[idx];
1229 unsigned int npods;
1230
1231 if (!gl || !gl->nelem) {
1232 pr_warn("tag 0x%x, idx %u, gl 0x%p, %u.\n",
1233 tag, idx, gl, gl ? gl->nelem : 0);
1234 return;
1235 }
1236 npods = (gl->nelem + PPOD_PAGES_MAX - 1) >> PPOD_PAGES_SHIFT;
1237 log_debug(1 << CXGBI_DBG_DDP,
1238 "tag 0x%x, release idx %u, npods %u.\n",
1239 tag, idx, npods);
1240 cdev->csk_ddp_clear(chba, tag, idx, npods);
1241 ddp_unmark_entries(ddp, idx, npods);
1242 ddp_release_gl(gl, ddp->pdev);
1243 } else
1244 pr_warn("tag 0x%x, idx %u > max %u.\n", tag, idx, ddp->nppods);
1245}
1246
1247static int ddp_tag_reserve(struct cxgbi_sock *csk, unsigned int tid,
1248 u32 sw_tag, u32 *tagp, struct cxgbi_gather_list *gl,
1249 gfp_t gfp)
1250{
1251 struct cxgbi_device *cdev = csk->cdev;
1252 struct cxgbi_ddp_info *ddp = cdev->ddp;
1253 struct cxgbi_tag_format *tformat = &cdev->tag_format;
1254 struct cxgbi_pagepod_hdr hdr;
1255 unsigned int npods;
1256 int idx = -1;
1257 int err = -ENOMEM;
1258 u32 tag;
1259
1260 npods = (gl->nelem + PPOD_PAGES_MAX - 1) >> PPOD_PAGES_SHIFT;
1261 if (ddp->idx_last == ddp->nppods)
1262 idx = ddp_find_unused_entries(ddp, 0, ddp->nppods,
1263 npods, gl);
1264 else {
1265 idx = ddp_find_unused_entries(ddp, ddp->idx_last + 1,
1266 ddp->nppods, npods,
1267 gl);
1268 if (idx < 0 && ddp->idx_last >= npods) {
1269 idx = ddp_find_unused_entries(ddp, 0,
1270 min(ddp->idx_last + npods, ddp->nppods),
1271 npods, gl);
1272 }
1273 }
1274 if (idx < 0) {
1275 log_debug(1 << CXGBI_DBG_DDP,
1276 "xferlen %u, gl %u, npods %u NO DDP.\n",
1277 gl->length, gl->nelem, npods);
1278 return idx;
1279 }
1280
1281 if (cdev->csk_ddp_alloc_gl_skb) {
1282 err = cdev->csk_ddp_alloc_gl_skb(ddp, idx, npods, gfp);
1283 if (err < 0)
1284 goto unmark_entries;
1285 }
1286
1287 tag = cxgbi_ddp_tag_base(tformat, sw_tag);
1288 tag |= idx << PPOD_IDX_SHIFT;
1289
1290 hdr.rsvd = 0;
1291 hdr.vld_tid = htonl(PPOD_VALID_FLAG | PPOD_TID(tid));
1292 hdr.pgsz_tag_clr = htonl(tag & ddp->rsvd_tag_mask);
1293 hdr.max_offset = htonl(gl->length);
1294 hdr.page_offset = htonl(gl->offset);
1295
1296 err = cdev->csk_ddp_set(csk, &hdr, idx, npods, gl);
1297 if (err < 0) {
1298 if (cdev->csk_ddp_free_gl_skb)
1299 cdev->csk_ddp_free_gl_skb(ddp, idx, npods);
1300 goto unmark_entries;
1301 }
1302
1303 ddp->idx_last = idx;
1304 log_debug(1 << CXGBI_DBG_DDP,
1305 "xfer %u, gl %u,%u, tid 0x%x, tag 0x%x->0x%x(%u,%u).\n",
1306 gl->length, gl->nelem, gl->offset, tid, sw_tag, tag, idx,
1307 npods);
1308 *tagp = tag;
1309 return 0;
1310
1311unmark_entries:
1312 ddp_unmark_entries(ddp, idx, npods);
1313 return err;
1314}
1315
1316int cxgbi_ddp_reserve(struct cxgbi_sock *csk, unsigned int *tagp,
1317 unsigned int sw_tag, unsigned int xferlen,
1318 struct scatterlist *sgl, unsigned int sgcnt, gfp_t gfp)
1319{
1320 struct cxgbi_device *cdev = csk->cdev;
1321 struct cxgbi_tag_format *tformat = &cdev->tag_format;
1322 struct cxgbi_gather_list *gl;
1323 int err;
1324
1325 if (page_idx >= DDP_PGIDX_MAX || !cdev->ddp ||
1326 xferlen < DDP_THRESHOLD) {
1327 log_debug(1 << CXGBI_DBG_DDP,
1328 "pgidx %u, xfer %u, NO ddp.\n", page_idx, xferlen);
1329 return -EINVAL;
1330 }
1331
1332 if (!cxgbi_sw_tag_usable(tformat, sw_tag)) {
1333 log_debug(1 << CXGBI_DBG_DDP,
1334 "sw_tag 0x%x NOT usable.\n", sw_tag);
1335 return -EINVAL;
1336 }
1337
1338 gl = ddp_make_gl(xferlen, sgl, sgcnt, cdev->pdev, gfp);
1339 if (!gl)
1340 return -ENOMEM;
1341
1342 err = ddp_tag_reserve(csk, csk->tid, sw_tag, tagp, gl, gfp);
1343 if (err < 0)
1344 ddp_release_gl(gl, cdev->pdev);
1345
1346 return err;
1347}
1348
1349static void ddp_destroy(struct kref *kref)
1350{
1351 struct cxgbi_ddp_info *ddp = container_of(kref,
1352 struct cxgbi_ddp_info,
1353 refcnt);
1354 struct cxgbi_device *cdev = ddp->cdev;
1355 int i = 0;
1356
1357 pr_info("kref 0, destroy ddp 0x%p, cdev 0x%p.\n", ddp, cdev);
1358
1359 while (i < ddp->nppods) {
1360 struct cxgbi_gather_list *gl = ddp->gl_map[i];
1361
1362 if (gl) {
1363 int npods = (gl->nelem + PPOD_PAGES_MAX - 1)
1364 >> PPOD_PAGES_SHIFT;
1365 pr_info("cdev 0x%p, ddp %d + %d.\n", cdev, i, npods);
1366 kfree(gl);
1367 if (cdev->csk_ddp_free_gl_skb)
1368 cdev->csk_ddp_free_gl_skb(ddp, i, npods);
1369 i += npods;
1370 } else
1371 i++;
1372 }
1373 cxgbi_free_big_mem(ddp);
1374}
1375
1376int cxgbi_ddp_cleanup(struct cxgbi_device *cdev)
1377{
1378 struct cxgbi_ddp_info *ddp = cdev->ddp;
1379
1380 log_debug(1 << CXGBI_DBG_DDP,
1381 "cdev 0x%p, release ddp 0x%p.\n", cdev, ddp);
1382 cdev->ddp = NULL;
1383 if (ddp)
1384 return kref_put(&ddp->refcnt, ddp_destroy);
1385 return 0;
1386}
1387EXPORT_SYMBOL_GPL(cxgbi_ddp_cleanup);
1388
1389int cxgbi_ddp_init(struct cxgbi_device *cdev,
1390 unsigned int llimit, unsigned int ulimit,
1391 unsigned int max_txsz, unsigned int max_rxsz)
1392{
1393 struct cxgbi_ddp_info *ddp;
1394 unsigned int ppmax, bits;
1395
1396 ppmax = (ulimit - llimit + 1) >> PPOD_SIZE_SHIFT;
1397 bits = __ilog2_u32(ppmax) + 1;
1398 if (bits > PPOD_IDX_MAX_SIZE)
1399 bits = PPOD_IDX_MAX_SIZE;
1400 ppmax = (1 << (bits - 1)) - 1;
1401
1402 ddp = cxgbi_alloc_big_mem(sizeof(struct cxgbi_ddp_info) +
1403 ppmax * (sizeof(struct cxgbi_gather_list *) +
1404 sizeof(struct sk_buff *)),
1405 GFP_KERNEL);
1406 if (!ddp) {
1407 pr_warn("cdev 0x%p, ddp ppmax %u OOM.\n", cdev, ppmax);
1408 return -ENOMEM;
1409 }
1410 ddp->gl_map = (struct cxgbi_gather_list **)(ddp + 1);
1411 ddp->gl_skb = (struct sk_buff **)(((char *)ddp->gl_map) +
1412 ppmax * sizeof(struct cxgbi_gather_list *));
1413 cdev->ddp = ddp;
1414
1415 spin_lock_init(&ddp->map_lock);
1416 kref_init(&ddp->refcnt);
1417
1418 ddp->cdev = cdev;
1419 ddp->pdev = cdev->pdev;
1420 ddp->llimit = llimit;
1421 ddp->ulimit = ulimit;
1422 ddp->max_txsz = min_t(unsigned int, max_txsz, ULP2_MAX_PKT_SIZE);
1423 ddp->max_rxsz = min_t(unsigned int, max_rxsz, ULP2_MAX_PKT_SIZE);
1424 ddp->nppods = ppmax;
1425 ddp->idx_last = ppmax;
1426 ddp->idx_bits = bits;
1427 ddp->idx_mask = (1 << bits) - 1;
1428 ddp->rsvd_tag_mask = (1 << (bits + PPOD_IDX_SHIFT)) - 1;
1429
1430 cdev->tag_format.sw_bits = sw_tag_idx_bits + sw_tag_age_bits;
1431 cdev->tag_format.rsvd_bits = ddp->idx_bits;
1432 cdev->tag_format.rsvd_shift = PPOD_IDX_SHIFT;
1433 cdev->tag_format.rsvd_mask = (1 << cdev->tag_format.rsvd_bits) - 1;
1434
1435 pr_info("%s tag format, sw %u, rsvd %u,%u, mask 0x%x.\n",
1436 cdev->ports[0]->name, cdev->tag_format.sw_bits,
1437 cdev->tag_format.rsvd_bits, cdev->tag_format.rsvd_shift,
1438 cdev->tag_format.rsvd_mask);
1439
1440 cdev->tx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
1441 ddp->max_txsz - ISCSI_PDU_NONPAYLOAD_LEN);
1442 cdev->rx_max_size = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
1443 ddp->max_rxsz - ISCSI_PDU_NONPAYLOAD_LEN);
1444
1445 log_debug(1 << CXGBI_DBG_DDP,
1446 "%s max payload size: %u/%u, %u/%u.\n",
1447 cdev->ports[0]->name, cdev->tx_max_size, ddp->max_txsz,
1448 cdev->rx_max_size, ddp->max_rxsz);
1449 return 0;
1450}
1451EXPORT_SYMBOL_GPL(cxgbi_ddp_init);
1452
1453/*
1454 * APIs interacting with open-iscsi libraries
1455 */
1456
1457static unsigned char padding[4];
1458
1459static void task_release_itt(struct iscsi_task *task, itt_t hdr_itt)
1460{
1461 struct scsi_cmnd *sc = task->sc;
1462 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
1463 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1464 struct cxgbi_hba *chba = cconn->chba;
1465 struct cxgbi_tag_format *tformat = &chba->cdev->tag_format;
1466 u32 tag = ntohl((__force u32)hdr_itt);
1467
1468 log_debug(1 << CXGBI_DBG_DDP,
1469 "cdev 0x%p, release tag 0x%x.\n", chba->cdev, tag);
1470 if (sc &&
1471 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
1472 cxgbi_is_ddp_tag(tformat, tag))
1473 ddp_tag_release(chba, tag);
1474}
1475
1476static int task_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
1477{
1478 struct scsi_cmnd *sc = task->sc;
1479 struct iscsi_conn *conn = task->conn;
1480 struct iscsi_session *sess = conn->session;
1481 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1482 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1483 struct cxgbi_hba *chba = cconn->chba;
1484 struct cxgbi_tag_format *tformat = &chba->cdev->tag_format;
1485 u32 sw_tag = (sess->age << cconn->task_idx_bits) | task->itt;
1486 u32 tag = 0;
1487 int err = -EINVAL;
1488
1489 if (sc &&
1490 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE)) {
1491 err = cxgbi_ddp_reserve(cconn->cep->csk, &tag, sw_tag,
1492 scsi_in(sc)->length,
1493 scsi_in(sc)->table.sgl,
1494 scsi_in(sc)->table.nents,
1495 GFP_ATOMIC);
1496 if (err < 0)
1497 log_debug(1 << CXGBI_DBG_DDP,
1498 "csk 0x%p, R task 0x%p, %u,%u, no ddp.\n",
1499 cconn->cep->csk, task, scsi_in(sc)->length,
1500 scsi_in(sc)->table.nents);
1501 }
1502
1503 if (err < 0)
1504 tag = cxgbi_set_non_ddp_tag(tformat, sw_tag);
1505 /* the itt need to sent in big-endian order */
1506 *hdr_itt = (__force itt_t)htonl(tag);
1507
1508 log_debug(1 << CXGBI_DBG_DDP,
1509 "cdev 0x%p, task 0x%p, 0x%x(0x%x,0x%x)->0x%x/0x%x.\n",
1510 chba->cdev, task, sw_tag, task->itt, sess->age, tag, *hdr_itt);
1511 return 0;
1512}
1513
1514void cxgbi_parse_pdu_itt(struct iscsi_conn *conn, itt_t itt, int *idx, int *age)
1515{
1516 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1517 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1518 struct cxgbi_device *cdev = cconn->chba->cdev;
1519 u32 tag = ntohl((__force u32) itt);
1520 u32 sw_bits;
1521
1522 sw_bits = cxgbi_tag_nonrsvd_bits(&cdev->tag_format, tag);
1523 if (idx)
1524 *idx = sw_bits & ((1 << cconn->task_idx_bits) - 1);
1525 if (age)
1526 *age = (sw_bits >> cconn->task_idx_bits) & ISCSI_AGE_MASK;
1527
1528 log_debug(1 << CXGBI_DBG_DDP,
1529 "cdev 0x%p, tag 0x%x/0x%x, -> 0x%x(0x%x,0x%x).\n",
1530 cdev, tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
1531 age ? *age : 0xFF);
1532}
1533EXPORT_SYMBOL_GPL(cxgbi_parse_pdu_itt);
1534
1535void cxgbi_conn_tx_open(struct cxgbi_sock *csk)
1536{
1537 struct iscsi_conn *conn = csk->user_data;
1538
1539 if (conn) {
1540 log_debug(1 << CXGBI_DBG_SOCK,
1541 "csk 0x%p, cid %d.\n", csk, conn->id);
1542 iscsi_conn_queue_work(conn);
1543 }
1544}
1545EXPORT_SYMBOL_GPL(cxgbi_conn_tx_open);
1546
1547/*
1548 * pdu receive, interact with libiscsi_tcp
1549 */
1550static inline int read_pdu_skb(struct iscsi_conn *conn,
1551 struct sk_buff *skb,
1552 unsigned int offset,
1553 int offloaded)
1554{
1555 int status = 0;
1556 int bytes_read;
1557
1558 bytes_read = iscsi_tcp_recv_skb(conn, skb, offset, offloaded, &status);
1559 switch (status) {
1560 case ISCSI_TCP_CONN_ERR:
1561 pr_info("skb 0x%p, off %u, %d, TCP_ERR.\n",
1562 skb, offset, offloaded);
1563 return -EIO;
1564 case ISCSI_TCP_SUSPENDED:
1565 log_debug(1 << CXGBI_DBG_PDU_RX,
1566 "skb 0x%p, off %u, %d, TCP_SUSPEND, rc %d.\n",
1567 skb, offset, offloaded, bytes_read);
1568 /* no transfer - just have caller flush queue */
1569 return bytes_read;
1570 case ISCSI_TCP_SKB_DONE:
1571 pr_info("skb 0x%p, off %u, %d, TCP_SKB_DONE.\n",
1572 skb, offset, offloaded);
1573 /*
1574 * pdus should always fit in the skb and we should get
1575 * segment done notifcation.
1576 */
1577 iscsi_conn_printk(KERN_ERR, conn, "Invalid pdu or skb.");
1578 return -EFAULT;
1579 case ISCSI_TCP_SEGMENT_DONE:
1580 log_debug(1 << CXGBI_DBG_PDU_RX,
1581 "skb 0x%p, off %u, %d, TCP_SEG_DONE, rc %d.\n",
1582 skb, offset, offloaded, bytes_read);
1583 return bytes_read;
1584 default:
1585 pr_info("skb 0x%p, off %u, %d, invalid status %d.\n",
1586 skb, offset, offloaded, status);
1587 return -EINVAL;
1588 }
1589}
1590
1591static int skb_read_pdu_bhs(struct iscsi_conn *conn, struct sk_buff *skb)
1592{
1593 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1594
1595 log_debug(1 << CXGBI_DBG_PDU_RX,
1596 "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
1597 conn, skb, skb->len, cxgbi_skcb_flags(skb));
1598
1599 if (!iscsi_tcp_recv_segment_is_hdr(tcp_conn)) {
1600 pr_info("conn 0x%p, skb 0x%p, not hdr.\n", conn, skb);
1601 iscsi_conn_failure(conn, ISCSI_ERR_PROTO);
1602 return -EIO;
1603 }
1604
1605 if (conn->hdrdgst_en &&
1606 cxgbi_skcb_test_flag(skb, SKCBF_RX_HCRC_ERR)) {
1607 pr_info("conn 0x%p, skb 0x%p, hcrc.\n", conn, skb);
1608 iscsi_conn_failure(conn, ISCSI_ERR_HDR_DGST);
1609 return -EIO;
1610 }
1611
1612 return read_pdu_skb(conn, skb, 0, 0);
1613}
1614
1615static int skb_read_pdu_data(struct iscsi_conn *conn, struct sk_buff *lskb,
1616 struct sk_buff *skb, unsigned int offset)
1617{
1618 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1619 bool offloaded = 0;
1620 int opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
1621
1622 log_debug(1 << CXGBI_DBG_PDU_RX,
1623 "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
1624 conn, skb, skb->len, cxgbi_skcb_flags(skb));
1625
1626 if (conn->datadgst_en &&
1627 cxgbi_skcb_test_flag(lskb, SKCBF_RX_DCRC_ERR)) {
1628 pr_info("conn 0x%p, skb 0x%p, dcrc 0x%lx.\n",
1629 conn, lskb, cxgbi_skcb_flags(lskb));
1630 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
1631 return -EIO;
1632 }
1633
1634 if (iscsi_tcp_recv_segment_is_hdr(tcp_conn))
1635 return 0;
1636
1637 /* coalesced, add header digest length */
1638 if (lskb == skb && conn->hdrdgst_en)
1639 offset += ISCSI_DIGEST_SIZE;
1640
1641 if (cxgbi_skcb_test_flag(lskb, SKCBF_RX_DATA_DDPD))
1642 offloaded = 1;
1643
1644 if (opcode == ISCSI_OP_SCSI_DATA_IN)
1645 log_debug(1 << CXGBI_DBG_PDU_RX,
1646 "skb 0x%p, op 0x%x, itt 0x%x, %u %s ddp'ed.\n",
1647 skb, opcode, ntohl(tcp_conn->in.hdr->itt),
1648 tcp_conn->in.datalen, offloaded ? "is" : "not");
1649
1650 return read_pdu_skb(conn, skb, offset, offloaded);
1651}
1652
1653static void csk_return_rx_credits(struct cxgbi_sock *csk, int copied)
1654{
1655 struct cxgbi_device *cdev = csk->cdev;
1656 int must_send;
1657 u32 credits;
1658
1659 log_debug(1 << CXGBI_DBG_PDU_RX,
1660 "csk 0x%p,%u,0x%lu,%u, seq %u, wup %u, thre %u, %u.\n",
1661 csk, csk->state, csk->flags, csk->tid, csk->copied_seq,
1662 csk->rcv_wup, cdev->rx_credit_thres,
1663 cdev->rcv_win);
1664
1665 if (csk->state != CTP_ESTABLISHED)
1666 return;
1667
1668 credits = csk->copied_seq - csk->rcv_wup;
1669 if (unlikely(!credits))
1670 return;
1671 if (unlikely(cdev->rx_credit_thres == 0))
1672 return;
1673
1674 must_send = credits + 16384 >= cdev->rcv_win;
1675 if (must_send || credits >= cdev->rx_credit_thres)
1676 csk->rcv_wup += cdev->csk_send_rx_credits(csk, credits);
1677}
1678
1679void cxgbi_conn_pdu_ready(struct cxgbi_sock *csk)
1680{
1681 struct cxgbi_device *cdev = csk->cdev;
1682 struct iscsi_conn *conn = csk->user_data;
1683 struct sk_buff *skb;
1684 unsigned int read = 0;
1685 int err = 0;
1686
1687 log_debug(1 << CXGBI_DBG_PDU_RX,
1688 "csk 0x%p, conn 0x%p.\n", csk, conn);
1689
1690 if (unlikely(!conn || conn->suspend_rx)) {
1691 log_debug(1 << CXGBI_DBG_PDU_RX,
1692 "csk 0x%p, conn 0x%p, id %d, suspend_rx %lu!\n",
1693 csk, conn, conn ? conn->id : 0xFF,
1694 conn ? conn->suspend_rx : 0xFF);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001695 return;
1696 }
1697
1698 while (!err) {
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001699 skb = skb_peek(&csk->receive_queue);
1700 if (!skb ||
1701 !(cxgbi_skcb_test_flag(skb, SKCBF_RX_STATUS))) {
1702 if (skb)
1703 log_debug(1 << CXGBI_DBG_PDU_RX,
1704 "skb 0x%p, NOT ready 0x%lx.\n",
1705 skb, cxgbi_skcb_flags(skb));
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001706 break;
1707 }
1708 __skb_unlink(skb, &csk->receive_queue);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001709
1710 read += cxgbi_skcb_rx_pdulen(skb);
1711 log_debug(1 << CXGBI_DBG_PDU_RX,
1712 "csk 0x%p, skb 0x%p,%u,f 0x%lx, pdu len %u.\n",
1713 csk, skb, skb->len, cxgbi_skcb_flags(skb),
1714 cxgbi_skcb_rx_pdulen(skb));
1715
1716 if (cxgbi_skcb_test_flag(skb, SKCBF_RX_COALESCED)) {
1717 err = skb_read_pdu_bhs(conn, skb);
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001718 if (err < 0) {
1719 pr_err("coalesced bhs, csk 0x%p, skb 0x%p,%u, "
1720 "f 0x%lx, plen %u.\n",
1721 csk, skb, skb->len,
1722 cxgbi_skcb_flags(skb),
1723 cxgbi_skcb_rx_pdulen(skb));
1724 goto skb_done;
1725 }
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001726 err = skb_read_pdu_data(conn, skb, skb,
1727 err + cdev->skb_rx_extra);
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001728 if (err < 0)
1729 pr_err("coalesced data, csk 0x%p, skb 0x%p,%u, "
1730 "f 0x%lx, plen %u.\n",
1731 csk, skb, skb->len,
1732 cxgbi_skcb_flags(skb),
1733 cxgbi_skcb_rx_pdulen(skb));
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001734 } else {
1735 err = skb_read_pdu_bhs(conn, skb);
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001736 if (err < 0) {
1737 pr_err("bhs, csk 0x%p, skb 0x%p,%u, "
1738 "f 0x%lx, plen %u.\n",
1739 csk, skb, skb->len,
1740 cxgbi_skcb_flags(skb),
1741 cxgbi_skcb_rx_pdulen(skb));
1742 goto skb_done;
1743 }
1744
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001745 if (cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA)) {
1746 struct sk_buff *dskb;
1747
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001748 dskb = skb_peek(&csk->receive_queue);
1749 if (!dskb) {
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001750 pr_err("csk 0x%p, skb 0x%p,%u, f 0x%lx,"
1751 " plen %u, NO data.\n",
1752 csk, skb, skb->len,
1753 cxgbi_skcb_flags(skb),
1754 cxgbi_skcb_rx_pdulen(skb));
1755 err = -EIO;
1756 goto skb_done;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001757 }
1758 __skb_unlink(dskb, &csk->receive_queue);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001759
1760 err = skb_read_pdu_data(conn, skb, dskb, 0);
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001761 if (err < 0)
1762 pr_err("data, csk 0x%p, skb 0x%p,%u, "
1763 "f 0x%lx, plen %u, dskb 0x%p,"
1764 "%u.\n",
1765 csk, skb, skb->len,
1766 cxgbi_skcb_flags(skb),
1767 cxgbi_skcb_rx_pdulen(skb),
1768 dskb, dskb->len);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001769 __kfree_skb(dskb);
1770 } else
1771 err = skb_read_pdu_data(conn, skb, skb, 0);
1772 }
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001773skb_done:
1774 __kfree_skb(skb);
1775
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001776 if (err < 0)
1777 break;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001778 }
1779
1780 log_debug(1 << CXGBI_DBG_PDU_RX, "csk 0x%p, read %u.\n", csk, read);
1781 if (read) {
1782 csk->copied_seq += read;
1783 csk_return_rx_credits(csk, read);
1784 conn->rxdata_octets += read;
1785 }
1786
1787 if (err < 0) {
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001788 pr_info("csk 0x%p, 0x%p, rx failed %d, read %u.\n",
1789 csk, conn, err, read);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001790 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1791 }
1792}
1793EXPORT_SYMBOL_GPL(cxgbi_conn_pdu_ready);
1794
1795static int sgl_seek_offset(struct scatterlist *sgl, unsigned int sgcnt,
1796 unsigned int offset, unsigned int *off,
1797 struct scatterlist **sgp)
1798{
1799 int i;
1800 struct scatterlist *sg;
1801
1802 for_each_sg(sgl, sg, sgcnt, i) {
1803 if (offset < sg->length) {
1804 *off = offset;
1805 *sgp = sg;
1806 return 0;
1807 }
1808 offset -= sg->length;
1809 }
1810 return -EFAULT;
1811}
1812
1813static int sgl_read_to_frags(struct scatterlist *sg, unsigned int sgoffset,
1814 unsigned int dlen, skb_frag_t *frags,
1815 int frag_max)
1816{
1817 unsigned int datalen = dlen;
1818 unsigned int sglen = sg->length - sgoffset;
1819 struct page *page = sg_page(sg);
1820 int i;
1821
1822 i = 0;
1823 do {
1824 unsigned int copy;
1825
1826 if (!sglen) {
1827 sg = sg_next(sg);
1828 if (!sg) {
1829 pr_warn("sg %d NULL, len %u/%u.\n",
1830 i, datalen, dlen);
1831 return -EINVAL;
1832 }
1833 sgoffset = 0;
1834 sglen = sg->length;
1835 page = sg_page(sg);
1836
1837 }
1838 copy = min(datalen, sglen);
1839 if (i && page == frags[i - 1].page &&
1840 sgoffset + sg->offset ==
1841 frags[i - 1].page_offset + frags[i - 1].size) {
1842 frags[i - 1].size += copy;
1843 } else {
1844 if (i >= frag_max) {
1845 pr_warn("too many pages %u, dlen %u.\n",
1846 frag_max, dlen);
1847 return -EINVAL;
1848 }
1849
1850 frags[i].page = page;
1851 frags[i].page_offset = sg->offset + sgoffset;
1852 frags[i].size = copy;
1853 i++;
1854 }
1855 datalen -= copy;
1856 sgoffset += copy;
1857 sglen -= copy;
1858 } while (datalen);
1859
1860 return i;
1861}
1862
1863int cxgbi_conn_alloc_pdu(struct iscsi_task *task, u8 opcode)
1864{
1865 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
1866 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1867 struct cxgbi_device *cdev = cconn->chba->cdev;
1868 struct iscsi_conn *conn = task->conn;
1869 struct iscsi_tcp_task *tcp_task = task->dd_data;
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001870 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001871 struct scsi_cmnd *sc = task->sc;
1872 int headroom = SKB_TX_ISCSI_PDU_HEADER_MAX;
1873
1874 tcp_task->dd_data = tdata;
1875 task->hdr = NULL;
1876
1877 if (SKB_MAX_HEAD(cdev->skb_tx_rsvd) > (512 * MAX_SKB_FRAGS) &&
1878 (opcode == ISCSI_OP_SCSI_DATA_OUT ||
1879 (opcode == ISCSI_OP_SCSI_CMD &&
1880 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_TO_DEVICE))))
1881 /* data could goes into skb head */
1882 headroom += min_t(unsigned int,
1883 SKB_MAX_HEAD(cdev->skb_tx_rsvd),
1884 conn->max_xmit_dlength);
1885
1886 tdata->skb = alloc_skb(cdev->skb_tx_rsvd + headroom, GFP_ATOMIC);
1887 if (!tdata->skb) {
1888 pr_warn("alloc skb %u+%u, opcode 0x%x failed.\n",
1889 cdev->skb_tx_rsvd, headroom, opcode);
1890 return -ENOMEM;
1891 }
1892
1893 skb_reserve(tdata->skb, cdev->skb_tx_rsvd);
1894 task->hdr = (struct iscsi_hdr *)tdata->skb->data;
1895 task->hdr_max = SKB_TX_ISCSI_PDU_HEADER_MAX; /* BHS + AHS */
1896
1897 /* data_out uses scsi_cmd's itt */
1898 if (opcode != ISCSI_OP_SCSI_DATA_OUT)
1899 task_reserve_itt(task, &task->hdr->itt);
1900
1901 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1902 "task 0x%p, op 0x%x, skb 0x%p,%u+%u/%u, itt 0x%x.\n",
1903 task, opcode, tdata->skb, cdev->skb_tx_rsvd, headroom,
1904 conn->max_xmit_dlength, ntohl(task->hdr->itt));
1905
1906 return 0;
1907}
1908EXPORT_SYMBOL_GPL(cxgbi_conn_alloc_pdu);
1909
1910static inline void tx_skb_setmode(struct sk_buff *skb, int hcrc, int dcrc)
1911{
1912 u8 submode = 0;
1913
1914 if (hcrc)
1915 submode |= 1;
1916 if (dcrc)
1917 submode |= 2;
1918 cxgbi_skcb_ulp_mode(skb) = (ULP2_MODE_ISCSI << 4) | submode;
1919}
1920
1921int cxgbi_conn_init_pdu(struct iscsi_task *task, unsigned int offset,
1922 unsigned int count)
1923{
1924 struct iscsi_conn *conn = task->conn;
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001925 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001926 struct sk_buff *skb = tdata->skb;
1927 unsigned int datalen = count;
1928 int i, padlen = iscsi_padding(count);
1929 struct page *pg;
1930
1931 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1932 "task 0x%p,0x%p, skb 0x%p, 0x%x,0x%x,0x%x, %u+%u.\n",
1933 task, task->sc, skb, (*skb->data) & ISCSI_OPCODE_MASK,
1934 ntohl(task->cmdsn), ntohl(task->hdr->itt), offset, count);
1935
1936 skb_put(skb, task->hdr_len);
1937 tx_skb_setmode(skb, conn->hdrdgst_en, datalen ? conn->datadgst_en : 0);
1938 if (!count)
1939 return 0;
1940
1941 if (task->sc) {
1942 struct scsi_data_buffer *sdb = scsi_out(task->sc);
1943 struct scatterlist *sg = NULL;
1944 int err;
1945
1946 tdata->offset = offset;
1947 tdata->count = count;
1948 err = sgl_seek_offset(
1949 sdb->table.sgl, sdb->table.nents,
1950 tdata->offset, &tdata->sgoffset, &sg);
1951 if (err < 0) {
1952 pr_warn("tpdu, sgl %u, bad offset %u/%u.\n",
1953 sdb->table.nents, tdata->offset, sdb->length);
1954 return err;
1955 }
1956 err = sgl_read_to_frags(sg, tdata->sgoffset, tdata->count,
1957 tdata->frags, MAX_PDU_FRAGS);
1958 if (err < 0) {
1959 pr_warn("tpdu, sgl %u, bad offset %u + %u.\n",
1960 sdb->table.nents, tdata->offset, tdata->count);
1961 return err;
1962 }
1963 tdata->nr_frags = err;
1964
1965 if (tdata->nr_frags > MAX_SKB_FRAGS ||
1966 (padlen && tdata->nr_frags == MAX_SKB_FRAGS)) {
1967 char *dst = skb->data + task->hdr_len;
1968 skb_frag_t *frag = tdata->frags;
1969
1970 /* data fits in the skb's headroom */
1971 for (i = 0; i < tdata->nr_frags; i++, frag++) {
1972 char *src = kmap_atomic(frag->page,
1973 KM_SOFTIRQ0);
1974
1975 memcpy(dst, src+frag->page_offset, frag->size);
1976 dst += frag->size;
1977 kunmap_atomic(src, KM_SOFTIRQ0);
1978 }
1979 if (padlen) {
1980 memset(dst, 0, padlen);
1981 padlen = 0;
1982 }
1983 skb_put(skb, count + padlen);
1984 } else {
1985 /* data fit into frag_list */
1986 for (i = 0; i < tdata->nr_frags; i++)
1987 get_page(tdata->frags[i].page);
1988
1989 memcpy(skb_shinfo(skb)->frags, tdata->frags,
1990 sizeof(skb_frag_t) * tdata->nr_frags);
1991 skb_shinfo(skb)->nr_frags = tdata->nr_frags;
1992 skb->len += count;
1993 skb->data_len += count;
1994 skb->truesize += count;
1995 }
1996
1997 } else {
1998 pg = virt_to_page(task->data);
1999
2000 get_page(pg);
2001 skb_fill_page_desc(skb, 0, pg, offset_in_page(task->data),
2002 count);
2003 skb->len += count;
2004 skb->data_len += count;
2005 skb->truesize += count;
2006 }
2007
2008 if (padlen) {
2009 i = skb_shinfo(skb)->nr_frags;
2010 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
2011 virt_to_page(padding), offset_in_page(padding),
2012 padlen);
2013
2014 skb->data_len += padlen;
2015 skb->truesize += padlen;
2016 skb->len += padlen;
2017 }
2018
2019 return 0;
2020}
2021EXPORT_SYMBOL_GPL(cxgbi_conn_init_pdu);
2022
2023int cxgbi_conn_xmit_pdu(struct iscsi_task *task)
2024{
2025 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
2026 struct cxgbi_conn *cconn = tcp_conn->dd_data;
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07002027 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002028 struct sk_buff *skb = tdata->skb;
2029 unsigned int datalen;
2030 int err;
2031
2032 if (!skb) {
2033 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2034 "task 0x%p, skb NULL.\n", task);
2035 return 0;
2036 }
2037
2038 datalen = skb->data_len;
2039 tdata->skb = NULL;
2040 err = cxgbi_sock_send_pdus(cconn->cep->csk, skb);
2041 if (err > 0) {
2042 int pdulen = err;
2043
2044 log_debug(1 << CXGBI_DBG_PDU_TX,
2045 "task 0x%p,0x%p, skb 0x%p, len %u/%u, rv %d.\n",
2046 task, task->sc, skb, skb->len, skb->data_len, err);
2047
2048 if (task->conn->hdrdgst_en)
2049 pdulen += ISCSI_DIGEST_SIZE;
2050
2051 if (datalen && task->conn->datadgst_en)
2052 pdulen += ISCSI_DIGEST_SIZE;
2053
2054 task->conn->txdata_octets += pdulen;
2055 return 0;
2056 }
2057
2058 if (err == -EAGAIN || err == -ENOBUFS) {
2059 log_debug(1 << CXGBI_DBG_PDU_TX,
2060 "task 0x%p, skb 0x%p, len %u/%u, %d EAGAIN.\n",
2061 task, skb, skb->len, skb->data_len, err);
2062 /* reset skb to send when we are called again */
2063 tdata->skb = skb;
2064 return err;
2065 }
2066
2067 kfree_skb(skb);
2068 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2069 "itt 0x%x, skb 0x%p, len %u/%u, xmit err %d.\n",
2070 task->itt, skb, skb->len, skb->data_len, err);
2071 iscsi_conn_printk(KERN_ERR, task->conn, "xmit err %d.\n", err);
2072 iscsi_conn_failure(task->conn, ISCSI_ERR_XMIT_FAILED);
2073 return err;
2074}
2075EXPORT_SYMBOL_GPL(cxgbi_conn_xmit_pdu);
2076
2077void cxgbi_cleanup_task(struct iscsi_task *task)
2078{
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07002079 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002080
2081 log_debug(1 << CXGBI_DBG_ISCSI,
2082 "task 0x%p, skb 0x%p, itt 0x%x.\n",
2083 task, tdata->skb, task->hdr_itt);
2084
2085 /* never reached the xmit task callout */
2086 if (tdata->skb)
2087 __kfree_skb(tdata->skb);
2088 memset(tdata, 0, sizeof(*tdata));
2089
2090 task_release_itt(task, task->hdr_itt);
2091 iscsi_tcp_cleanup_task(task);
2092}
2093EXPORT_SYMBOL_GPL(cxgbi_cleanup_task);
2094
2095void cxgbi_get_conn_stats(struct iscsi_cls_conn *cls_conn,
2096 struct iscsi_stats *stats)
2097{
2098 struct iscsi_conn *conn = cls_conn->dd_data;
2099
2100 stats->txdata_octets = conn->txdata_octets;
2101 stats->rxdata_octets = conn->rxdata_octets;
2102 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2103 stats->dataout_pdus = conn->dataout_pdus_cnt;
2104 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2105 stats->datain_pdus = conn->datain_pdus_cnt;
2106 stats->r2t_pdus = conn->r2t_pdus_cnt;
2107 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2108 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2109 stats->digest_err = 0;
2110 stats->timeout_err = 0;
2111 stats->custom_length = 1;
2112 strcpy(stats->custom[0].desc, "eh_abort_cnt");
2113 stats->custom[0].value = conn->eh_abort_cnt;
2114}
2115EXPORT_SYMBOL_GPL(cxgbi_get_conn_stats);
2116
2117static int cxgbi_conn_max_xmit_dlength(struct iscsi_conn *conn)
2118{
2119 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2120 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2121 struct cxgbi_device *cdev = cconn->chba->cdev;
2122 unsigned int headroom = SKB_MAX_HEAD(cdev->skb_tx_rsvd);
2123 unsigned int max_def = 512 * MAX_SKB_FRAGS;
2124 unsigned int max = max(max_def, headroom);
2125
2126 max = min(cconn->chba->cdev->tx_max_size, max);
2127 if (conn->max_xmit_dlength)
2128 conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
2129 else
2130 conn->max_xmit_dlength = max;
2131 cxgbi_align_pdu_size(conn->max_xmit_dlength);
2132
2133 return 0;
2134}
2135
2136static int cxgbi_conn_max_recv_dlength(struct iscsi_conn *conn)
2137{
2138 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2139 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2140 unsigned int max = cconn->chba->cdev->rx_max_size;
2141
2142 cxgbi_align_pdu_size(max);
2143
2144 if (conn->max_recv_dlength) {
2145 if (conn->max_recv_dlength > max) {
2146 pr_err("MaxRecvDataSegmentLength %u > %u.\n",
2147 conn->max_recv_dlength, max);
2148 return -EINVAL;
2149 }
2150 conn->max_recv_dlength = min(conn->max_recv_dlength, max);
2151 cxgbi_align_pdu_size(conn->max_recv_dlength);
2152 } else
2153 conn->max_recv_dlength = max;
2154
2155 return 0;
2156}
2157
2158int cxgbi_set_conn_param(struct iscsi_cls_conn *cls_conn,
2159 enum iscsi_param param, char *buf, int buflen)
2160{
2161 struct iscsi_conn *conn = cls_conn->dd_data;
2162 struct iscsi_session *session = conn->session;
2163 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2164 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2165 struct cxgbi_sock *csk = cconn->cep->csk;
2166 int value, err = 0;
2167
2168 log_debug(1 << CXGBI_DBG_ISCSI,
2169 "cls_conn 0x%p, param %d, buf(%d) %s.\n",
2170 cls_conn, param, buflen, buf);
2171
2172 switch (param) {
2173 case ISCSI_PARAM_HDRDGST_EN:
2174 err = iscsi_set_param(cls_conn, param, buf, buflen);
2175 if (!err && conn->hdrdgst_en)
2176 err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
2177 conn->hdrdgst_en,
2178 conn->datadgst_en, 0);
2179 break;
2180 case ISCSI_PARAM_DATADGST_EN:
2181 err = iscsi_set_param(cls_conn, param, buf, buflen);
2182 if (!err && conn->datadgst_en)
2183 err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
2184 conn->hdrdgst_en,
2185 conn->datadgst_en, 0);
2186 break;
2187 case ISCSI_PARAM_MAX_R2T:
2188 sscanf(buf, "%d", &value);
2189 if (value <= 0 || !is_power_of_2(value))
2190 return -EINVAL;
2191 if (session->max_r2t == value)
2192 break;
2193 iscsi_tcp_r2tpool_free(session);
2194 err = iscsi_set_param(cls_conn, param, buf, buflen);
2195 if (!err && iscsi_tcp_r2tpool_alloc(session))
2196 return -ENOMEM;
2197 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2198 err = iscsi_set_param(cls_conn, param, buf, buflen);
2199 if (!err)
2200 err = cxgbi_conn_max_recv_dlength(conn);
2201 break;
2202 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2203 err = iscsi_set_param(cls_conn, param, buf, buflen);
2204 if (!err)
2205 err = cxgbi_conn_max_xmit_dlength(conn);
2206 break;
2207 default:
2208 return iscsi_set_param(cls_conn, param, buf, buflen);
2209 }
2210 return err;
2211}
2212EXPORT_SYMBOL_GPL(cxgbi_set_conn_param);
2213
2214int cxgbi_get_conn_param(struct iscsi_cls_conn *cls_conn,
2215 enum iscsi_param param, char *buf)
2216{
2217 struct iscsi_conn *iconn = cls_conn->dd_data;
2218 int len;
2219
2220 log_debug(1 << CXGBI_DBG_ISCSI,
2221 "cls_conn 0x%p, param %d.\n", cls_conn, param);
2222
2223 switch (param) {
2224 case ISCSI_PARAM_CONN_PORT:
2225 spin_lock_bh(&iconn->session->lock);
2226 len = sprintf(buf, "%hu\n", iconn->portal_port);
2227 spin_unlock_bh(&iconn->session->lock);
2228 break;
2229 case ISCSI_PARAM_CONN_ADDRESS:
2230 spin_lock_bh(&iconn->session->lock);
2231 len = sprintf(buf, "%s\n", iconn->portal_address);
2232 spin_unlock_bh(&iconn->session->lock);
2233 break;
2234 default:
2235 return iscsi_conn_get_param(cls_conn, param, buf);
2236 }
2237 return len;
2238}
2239EXPORT_SYMBOL_GPL(cxgbi_get_conn_param);
2240
2241struct iscsi_cls_conn *
2242cxgbi_create_conn(struct iscsi_cls_session *cls_session, u32 cid)
2243{
2244 struct iscsi_cls_conn *cls_conn;
2245 struct iscsi_conn *conn;
2246 struct iscsi_tcp_conn *tcp_conn;
2247 struct cxgbi_conn *cconn;
2248
2249 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
2250 if (!cls_conn)
2251 return NULL;
2252
2253 conn = cls_conn->dd_data;
2254 tcp_conn = conn->dd_data;
2255 cconn = tcp_conn->dd_data;
2256 cconn->iconn = conn;
2257
2258 log_debug(1 << CXGBI_DBG_ISCSI,
2259 "cid %u(0x%x), cls 0x%p,0x%p, conn 0x%p,0x%p,0x%p.\n",
2260 cid, cid, cls_session, cls_conn, conn, tcp_conn, cconn);
2261
2262 return cls_conn;
2263}
2264EXPORT_SYMBOL_GPL(cxgbi_create_conn);
2265
2266int cxgbi_bind_conn(struct iscsi_cls_session *cls_session,
2267 struct iscsi_cls_conn *cls_conn,
2268 u64 transport_eph, int is_leading)
2269{
2270 struct iscsi_conn *conn = cls_conn->dd_data;
2271 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2272 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2273 struct iscsi_endpoint *ep;
2274 struct cxgbi_endpoint *cep;
2275 struct cxgbi_sock *csk;
2276 int err;
2277
2278 ep = iscsi_lookup_endpoint(transport_eph);
2279 if (!ep)
2280 return -EINVAL;
2281
2282 /* setup ddp pagesize */
2283 cep = ep->dd_data;
2284 csk = cep->csk;
2285 err = csk->cdev->csk_ddp_setup_pgidx(csk, csk->tid, page_idx, 0);
2286 if (err < 0)
2287 return err;
2288
2289 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
2290 if (err)
2291 return -EINVAL;
2292
2293 /* calculate the tag idx bits needed for this conn based on cmds_max */
2294 cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
2295
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07002296 write_lock_bh(&csk->callback_lock);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002297 csk->user_data = conn;
2298 cconn->chba = cep->chba;
2299 cconn->cep = cep;
2300 cep->cconn = cconn;
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07002301 write_unlock_bh(&csk->callback_lock);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002302
2303 cxgbi_conn_max_xmit_dlength(conn);
2304 cxgbi_conn_max_recv_dlength(conn);
2305
2306 spin_lock_bh(&conn->session->lock);
2307 sprintf(conn->portal_address, "%pI4", &csk->daddr.sin_addr.s_addr);
2308 conn->portal_port = ntohs(csk->daddr.sin_port);
2309 spin_unlock_bh(&conn->session->lock);
2310
2311 log_debug(1 << CXGBI_DBG_ISCSI,
2312 "cls 0x%p,0x%p, ep 0x%p, cconn 0x%p, csk 0x%p.\n",
2313 cls_session, cls_conn, ep, cconn, csk);
2314 /* init recv engine */
2315 iscsi_tcp_hdr_recv_prep(tcp_conn);
2316
2317 return 0;
2318}
2319EXPORT_SYMBOL_GPL(cxgbi_bind_conn);
2320
2321struct iscsi_cls_session *cxgbi_create_session(struct iscsi_endpoint *ep,
2322 u16 cmds_max, u16 qdepth,
2323 u32 initial_cmdsn)
2324{
2325 struct cxgbi_endpoint *cep;
2326 struct cxgbi_hba *chba;
2327 struct Scsi_Host *shost;
2328 struct iscsi_cls_session *cls_session;
2329 struct iscsi_session *session;
2330
2331 if (!ep) {
2332 pr_err("missing endpoint.\n");
2333 return NULL;
2334 }
2335
2336 cep = ep->dd_data;
2337 chba = cep->chba;
2338 shost = chba->shost;
2339
2340 BUG_ON(chba != iscsi_host_priv(shost));
2341
2342 cls_session = iscsi_session_setup(chba->cdev->itp, shost,
2343 cmds_max, 0,
2344 sizeof(struct iscsi_tcp_task) +
2345 sizeof(struct cxgbi_task_data),
2346 initial_cmdsn, ISCSI_MAX_TARGET);
2347 if (!cls_session)
2348 return NULL;
2349
2350 session = cls_session->dd_data;
2351 if (iscsi_tcp_r2tpool_alloc(session))
2352 goto remove_session;
2353
2354 log_debug(1 << CXGBI_DBG_ISCSI,
2355 "ep 0x%p, cls sess 0x%p.\n", ep, cls_session);
2356 return cls_session;
2357
2358remove_session:
2359 iscsi_session_teardown(cls_session);
2360 return NULL;
2361}
2362EXPORT_SYMBOL_GPL(cxgbi_create_session);
2363
2364void cxgbi_destroy_session(struct iscsi_cls_session *cls_session)
2365{
2366 log_debug(1 << CXGBI_DBG_ISCSI,
2367 "cls sess 0x%p.\n", cls_session);
2368
2369 iscsi_tcp_r2tpool_free(cls_session->dd_data);
2370 iscsi_session_teardown(cls_session);
2371}
2372EXPORT_SYMBOL_GPL(cxgbi_destroy_session);
2373
2374int cxgbi_set_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2375 char *buf, int buflen)
2376{
2377 struct cxgbi_hba *chba = iscsi_host_priv(shost);
2378
2379 if (!chba->ndev) {
2380 shost_printk(KERN_ERR, shost, "Could not get host param. "
2381 "netdev for host not set.\n");
2382 return -ENODEV;
2383 }
2384
2385 log_debug(1 << CXGBI_DBG_ISCSI,
2386 "shost 0x%p, hba 0x%p,%s, param %d, buf(%d) %s.\n",
2387 shost, chba, chba->ndev->name, param, buflen, buf);
2388
2389 switch (param) {
2390 case ISCSI_HOST_PARAM_IPADDRESS:
2391 {
2392 __be32 addr = in_aton(buf);
2393 log_debug(1 << CXGBI_DBG_ISCSI,
2394 "hba %s, req. ipv4 %pI4.\n", chba->ndev->name, &addr);
2395 cxgbi_set_iscsi_ipv4(chba, addr);
2396 return 0;
2397 }
2398 case ISCSI_HOST_PARAM_HWADDRESS:
2399 case ISCSI_HOST_PARAM_NETDEV_NAME:
2400 return 0;
2401 default:
2402 return iscsi_host_set_param(shost, param, buf, buflen);
2403 }
2404}
2405EXPORT_SYMBOL_GPL(cxgbi_set_host_param);
2406
2407int cxgbi_get_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2408 char *buf)
2409{
2410 struct cxgbi_hba *chba = iscsi_host_priv(shost);
2411 int len = 0;
2412
2413 if (!chba->ndev) {
2414 shost_printk(KERN_ERR, shost, "Could not get host param. "
2415 "netdev for host not set.\n");
2416 return -ENODEV;
2417 }
2418
2419 log_debug(1 << CXGBI_DBG_ISCSI,
2420 "shost 0x%p, hba 0x%p,%s, param %d.\n",
2421 shost, chba, chba->ndev->name, param);
2422
2423 switch (param) {
2424 case ISCSI_HOST_PARAM_HWADDRESS:
2425 len = sysfs_format_mac(buf, chba->ndev->dev_addr, 6);
2426 break;
2427 case ISCSI_HOST_PARAM_NETDEV_NAME:
2428 len = sprintf(buf, "%s\n", chba->ndev->name);
2429 break;
2430 case ISCSI_HOST_PARAM_IPADDRESS:
2431 {
2432 __be32 addr;
2433
2434 addr = cxgbi_get_iscsi_ipv4(chba);
2435 len = sprintf(buf, "%pI4", &addr);
2436 log_debug(1 << CXGBI_DBG_ISCSI,
2437 "hba %s, ipv4 %pI4.\n", chba->ndev->name, &addr);
2438 break;
2439 }
2440 default:
2441 return iscsi_host_get_param(shost, param, buf);
2442 }
2443
2444 return len;
2445}
2446EXPORT_SYMBOL_GPL(cxgbi_get_host_param);
2447
2448struct iscsi_endpoint *cxgbi_ep_connect(struct Scsi_Host *shost,
2449 struct sockaddr *dst_addr,
2450 int non_blocking)
2451{
2452 struct iscsi_endpoint *ep;
2453 struct cxgbi_endpoint *cep;
2454 struct cxgbi_hba *hba = NULL;
2455 struct cxgbi_sock *csk;
2456 int err = -EINVAL;
2457
2458 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2459 "shost 0x%p, non_blocking %d, dst_addr 0x%p.\n",
2460 shost, non_blocking, dst_addr);
2461
2462 if (shost) {
2463 hba = iscsi_host_priv(shost);
2464 if (!hba) {
2465 pr_info("shost 0x%p, priv NULL.\n", shost);
2466 goto err_out;
2467 }
2468 }
2469
2470 csk = cxgbi_check_route(dst_addr);
2471 if (IS_ERR(csk))
2472 return (struct iscsi_endpoint *)csk;
2473 cxgbi_sock_get(csk);
2474
2475 if (!hba)
2476 hba = csk->cdev->hbas[csk->port_id];
2477 else if (hba != csk->cdev->hbas[csk->port_id]) {
2478 pr_info("Could not connect through requested host %u"
2479 "hba 0x%p != 0x%p (%u).\n",
2480 shost->host_no, hba,
2481 csk->cdev->hbas[csk->port_id], csk->port_id);
2482 err = -ENOSPC;
2483 goto release_conn;
2484 }
2485
2486 err = sock_get_port(csk);
2487 if (err)
2488 goto release_conn;
2489
2490 cxgbi_sock_set_state(csk, CTP_CONNECTING);
2491 err = csk->cdev->csk_init_act_open(csk);
2492 if (err)
2493 goto release_conn;
2494
2495 if (cxgbi_sock_is_closing(csk)) {
2496 err = -ENOSPC;
2497 pr_info("csk 0x%p is closing.\n", csk);
2498 goto release_conn;
2499 }
2500
2501 ep = iscsi_create_endpoint(sizeof(*cep));
2502 if (!ep) {
2503 err = -ENOMEM;
2504 pr_info("iscsi alloc ep, OOM.\n");
2505 goto release_conn;
2506 }
2507
2508 cep = ep->dd_data;
2509 cep->csk = csk;
2510 cep->chba = hba;
2511
2512 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2513 "ep 0x%p, cep 0x%p, csk 0x%p, hba 0x%p,%s.\n",
2514 ep, cep, csk, hba, hba->ndev->name);
2515 return ep;
2516
2517release_conn:
2518 cxgbi_sock_put(csk);
2519 cxgbi_sock_closed(csk);
2520err_out:
2521 return ERR_PTR(err);
2522}
2523EXPORT_SYMBOL_GPL(cxgbi_ep_connect);
2524
2525int cxgbi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
2526{
2527 struct cxgbi_endpoint *cep = ep->dd_data;
2528 struct cxgbi_sock *csk = cep->csk;
2529
2530 if (!cxgbi_sock_is_established(csk))
2531 return 0;
2532 return 1;
2533}
2534EXPORT_SYMBOL_GPL(cxgbi_ep_poll);
2535
2536void cxgbi_ep_disconnect(struct iscsi_endpoint *ep)
2537{
2538 struct cxgbi_endpoint *cep = ep->dd_data;
2539 struct cxgbi_conn *cconn = cep->cconn;
2540 struct cxgbi_sock *csk = cep->csk;
2541
2542 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2543 "ep 0x%p, cep 0x%p, cconn 0x%p, csk 0x%p,%u,0x%lx.\n",
2544 ep, cep, cconn, csk, csk->state, csk->flags);
2545
2546 if (cconn && cconn->iconn) {
2547 iscsi_suspend_tx(cconn->iconn);
2548 write_lock_bh(&csk->callback_lock);
2549 cep->csk->user_data = NULL;
2550 cconn->cep = NULL;
2551 write_unlock_bh(&csk->callback_lock);
2552 }
2553 iscsi_destroy_endpoint(ep);
2554
2555 if (likely(csk->state >= CTP_ESTABLISHED))
2556 need_active_close(csk);
2557 else
2558 cxgbi_sock_closed(csk);
2559
2560 cxgbi_sock_put(csk);
2561}
2562EXPORT_SYMBOL_GPL(cxgbi_ep_disconnect);
2563
2564int cxgbi_iscsi_init(struct iscsi_transport *itp,
2565 struct scsi_transport_template **stt)
2566{
2567 *stt = iscsi_register_transport(itp);
2568 if (*stt == NULL) {
2569 pr_err("unable to register %s transport 0x%p.\n",
2570 itp->name, itp);
2571 return -ENODEV;
2572 }
2573 log_debug(1 << CXGBI_DBG_ISCSI,
2574 "%s, registered iscsi transport 0x%p.\n",
2575 itp->name, stt);
2576 return 0;
2577}
2578EXPORT_SYMBOL_GPL(cxgbi_iscsi_init);
2579
2580void cxgbi_iscsi_cleanup(struct iscsi_transport *itp,
2581 struct scsi_transport_template **stt)
2582{
2583 if (*stt) {
2584 log_debug(1 << CXGBI_DBG_ISCSI,
2585 "de-register transport 0x%p, %s, stt 0x%p.\n",
2586 itp, itp->name, *stt);
2587 *stt = NULL;
2588 iscsi_unregister_transport(itp);
2589 }
2590}
2591EXPORT_SYMBOL_GPL(cxgbi_iscsi_cleanup);
2592
2593static int __init libcxgbi_init_module(void)
2594{
2595 sw_tag_idx_bits = (__ilog2_u32(ISCSI_ITT_MASK)) + 1;
2596 sw_tag_age_bits = (__ilog2_u32(ISCSI_AGE_MASK)) + 1;
2597
2598 pr_info("tag itt 0x%x, %u bits, age 0x%x, %u bits.\n",
2599 ISCSI_ITT_MASK, sw_tag_idx_bits,
2600 ISCSI_AGE_MASK, sw_tag_age_bits);
2601
2602 ddp_setup_host_page_size();
2603 return 0;
2604}
2605
2606static void __exit libcxgbi_exit_module(void)
2607{
2608 cxgbi_device_unregister_all(0xFF);
2609 return;
2610}
2611
2612module_init(libcxgbi_init_module);
2613module_exit(libcxgbi_exit_module);