1 /*
2 * libcxgbi.c: Chelsio common library for T3/T4 iSCSI driver.
3 *
4 * Copyright (c) 2010-2015 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 <net/ipv6.h>
28 #include <net/ip6_route.h>
29 #include <net/addrconf.h>
30
31 #include <linux/inetdevice.h> /* ip_dev_find */
32 #include <linux/module.h>
33 #include <net/tcp.h>
34
35 static unsigned int dbg_level;
36
37 #include "libcxgbi.h"
38
39 #define DRV_MODULE_NAME "libcxgbi"
40 #define DRV_MODULE_DESC "Chelsio iSCSI driver library"
41 #define DRV_MODULE_VERSION "0.9.1-ko"
42 #define DRV_MODULE_RELDATE "Apr. 2015"
43
44 static char version[] =
45 DRV_MODULE_DESC " " DRV_MODULE_NAME
46 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
47
48 MODULE_AUTHOR("Chelsio Communications, Inc.");
49 MODULE_DESCRIPTION(DRV_MODULE_DESC);
50 MODULE_VERSION(DRV_MODULE_VERSION);
51 MODULE_LICENSE("GPL");
52
53 module_param(dbg_level, uint, 0644);
54 MODULE_PARM_DESC(dbg_level, "libiscsi debug level (default=0)");
55
56
57 /*
58 * cxgbi device management
59 * maintains a list of the cxgbi devices
60 */
61 static LIST_HEAD(cdev_list);
62 static DEFINE_MUTEX(cdev_mutex);
63
64 static LIST_HEAD(cdev_rcu_list);
65 static DEFINE_SPINLOCK(cdev_rcu_lock);
66
cxgbi_decode_sw_tag(u32 sw_tag,int * idx,int * age)67 static inline void cxgbi_decode_sw_tag(u32 sw_tag, int *idx, int *age)
68 {
69 if (age)
70 *age = sw_tag & 0x7FFF;
71 if (idx)
72 *idx = (sw_tag >> 16) & 0x7FFF;
73 }
74
cxgbi_device_portmap_create(struct cxgbi_device * cdev,unsigned int base,unsigned int max_conn)75 int cxgbi_device_portmap_create(struct cxgbi_device *cdev, unsigned int base,
76 unsigned int max_conn)
77 {
78 struct cxgbi_ports_map *pmap = &cdev->pmap;
79
80 pmap->port_csk = cxgbi_alloc_big_mem(max_conn *
81 sizeof(struct cxgbi_sock *),
82 GFP_KERNEL);
83 if (!pmap->port_csk) {
84 pr_warn("cdev 0x%p, portmap OOM %u.\n", cdev, max_conn);
85 return -ENOMEM;
86 }
87
88 pmap->max_connect = max_conn;
89 pmap->sport_base = base;
90 spin_lock_init(&pmap->lock);
91 return 0;
92 }
93 EXPORT_SYMBOL_GPL(cxgbi_device_portmap_create);
94
cxgbi_device_portmap_cleanup(struct cxgbi_device * cdev)95 void cxgbi_device_portmap_cleanup(struct cxgbi_device *cdev)
96 {
97 struct cxgbi_ports_map *pmap = &cdev->pmap;
98 struct cxgbi_sock *csk;
99 int i;
100
101 for (i = 0; i < pmap->max_connect; i++) {
102 if (pmap->port_csk[i]) {
103 csk = pmap->port_csk[i];
104 pmap->port_csk[i] = NULL;
105 log_debug(1 << CXGBI_DBG_SOCK,
106 "csk 0x%p, cdev 0x%p, offload down.\n",
107 csk, cdev);
108 spin_lock_bh(&csk->lock);
109 cxgbi_sock_set_flag(csk, CTPF_OFFLOAD_DOWN);
110 cxgbi_sock_closed(csk);
111 spin_unlock_bh(&csk->lock);
112 cxgbi_sock_put(csk);
113 }
114 }
115 }
116 EXPORT_SYMBOL_GPL(cxgbi_device_portmap_cleanup);
117
cxgbi_device_destroy(struct cxgbi_device * cdev)118 static inline void cxgbi_device_destroy(struct cxgbi_device *cdev)
119 {
120 log_debug(1 << CXGBI_DBG_DEV,
121 "cdev 0x%p, p# %u.\n", cdev, cdev->nports);
122 cxgbi_hbas_remove(cdev);
123 cxgbi_device_portmap_cleanup(cdev);
124 cxgbi_ppm_release(cdev->cdev2ppm(cdev));
125 if (cdev->pmap.max_connect)
126 cxgbi_free_big_mem(cdev->pmap.port_csk);
127 kfree(cdev);
128 }
129
cxgbi_device_register(unsigned int extra,unsigned int nports)130 struct cxgbi_device *cxgbi_device_register(unsigned int extra,
131 unsigned int nports)
132 {
133 struct cxgbi_device *cdev;
134
135 cdev = kzalloc(sizeof(*cdev) + extra + nports *
136 (sizeof(struct cxgbi_hba *) +
137 sizeof(struct net_device *)),
138 GFP_KERNEL);
139 if (!cdev) {
140 pr_warn("nport %d, OOM.\n", nports);
141 return NULL;
142 }
143 cdev->ports = (struct net_device **)(cdev + 1);
144 cdev->hbas = (struct cxgbi_hba **)(((char*)cdev->ports) + nports *
145 sizeof(struct net_device *));
146 if (extra)
147 cdev->dd_data = ((char *)cdev->hbas) +
148 nports * sizeof(struct cxgbi_hba *);
149 spin_lock_init(&cdev->pmap.lock);
150
151 mutex_lock(&cdev_mutex);
152 list_add_tail(&cdev->list_head, &cdev_list);
153 mutex_unlock(&cdev_mutex);
154
155 spin_lock(&cdev_rcu_lock);
156 list_add_tail_rcu(&cdev->rcu_node, &cdev_rcu_list);
157 spin_unlock(&cdev_rcu_lock);
158
159 log_debug(1 << CXGBI_DBG_DEV,
160 "cdev 0x%p, p# %u.\n", cdev, nports);
161 return cdev;
162 }
163 EXPORT_SYMBOL_GPL(cxgbi_device_register);
164
cxgbi_device_unregister(struct cxgbi_device * cdev)165 void cxgbi_device_unregister(struct cxgbi_device *cdev)
166 {
167 log_debug(1 << CXGBI_DBG_DEV,
168 "cdev 0x%p, p# %u,%s.\n",
169 cdev, cdev->nports, cdev->nports ? cdev->ports[0]->name : "");
170
171 mutex_lock(&cdev_mutex);
172 list_del(&cdev->list_head);
173 mutex_unlock(&cdev_mutex);
174
175 spin_lock(&cdev_rcu_lock);
176 list_del_rcu(&cdev->rcu_node);
177 spin_unlock(&cdev_rcu_lock);
178 synchronize_rcu();
179
180 cxgbi_device_destroy(cdev);
181 }
182 EXPORT_SYMBOL_GPL(cxgbi_device_unregister);
183
cxgbi_device_unregister_all(unsigned int flag)184 void cxgbi_device_unregister_all(unsigned int flag)
185 {
186 struct cxgbi_device *cdev, *tmp;
187
188 mutex_lock(&cdev_mutex);
189 list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
190 if ((cdev->flags & flag) == flag) {
191 mutex_unlock(&cdev_mutex);
192 cxgbi_device_unregister(cdev);
193 mutex_lock(&cdev_mutex);
194 }
195 }
196 mutex_unlock(&cdev_mutex);
197 }
198 EXPORT_SYMBOL_GPL(cxgbi_device_unregister_all);
199
cxgbi_device_find_by_lldev(void * lldev)200 struct cxgbi_device *cxgbi_device_find_by_lldev(void *lldev)
201 {
202 struct cxgbi_device *cdev, *tmp;
203
204 mutex_lock(&cdev_mutex);
205 list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
206 if (cdev->lldev == lldev) {
207 mutex_unlock(&cdev_mutex);
208 return cdev;
209 }
210 }
211 mutex_unlock(&cdev_mutex);
212
213 log_debug(1 << CXGBI_DBG_DEV,
214 "lldev 0x%p, NO match found.\n", lldev);
215 return NULL;
216 }
217 EXPORT_SYMBOL_GPL(cxgbi_device_find_by_lldev);
218
cxgbi_device_find_by_netdev(struct net_device * ndev,int * port)219 struct cxgbi_device *cxgbi_device_find_by_netdev(struct net_device *ndev,
220 int *port)
221 {
222 struct net_device *vdev = NULL;
223 struct cxgbi_device *cdev, *tmp;
224 int i;
225
226 if (is_vlan_dev(ndev)) {
227 vdev = ndev;
228 ndev = vlan_dev_real_dev(ndev);
229 log_debug(1 << CXGBI_DBG_DEV,
230 "vlan dev %s -> %s.\n", vdev->name, ndev->name);
231 }
232
233 mutex_lock(&cdev_mutex);
234 list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
235 for (i = 0; i < cdev->nports; i++) {
236 if (ndev == cdev->ports[i]) {
237 cdev->hbas[i]->vdev = vdev;
238 mutex_unlock(&cdev_mutex);
239 if (port)
240 *port = i;
241 return cdev;
242 }
243 }
244 }
245 mutex_unlock(&cdev_mutex);
246 log_debug(1 << CXGBI_DBG_DEV,
247 "ndev 0x%p, %s, NO match found.\n", ndev, ndev->name);
248 return NULL;
249 }
250 EXPORT_SYMBOL_GPL(cxgbi_device_find_by_netdev);
251
cxgbi_device_find_by_netdev_rcu(struct net_device * ndev,int * port)252 struct cxgbi_device *cxgbi_device_find_by_netdev_rcu(struct net_device *ndev,
253 int *port)
254 {
255 struct net_device *vdev = NULL;
256 struct cxgbi_device *cdev;
257 int i;
258
259 if (is_vlan_dev(ndev)) {
260 vdev = ndev;
261 ndev = vlan_dev_real_dev(ndev);
262 pr_info("vlan dev %s -> %s.\n", vdev->name, ndev->name);
263 }
264
265 rcu_read_lock();
266 list_for_each_entry_rcu(cdev, &cdev_rcu_list, rcu_node) {
267 for (i = 0; i < cdev->nports; i++) {
268 if (ndev == cdev->ports[i]) {
269 cdev->hbas[i]->vdev = vdev;
270 rcu_read_unlock();
271 if (port)
272 *port = i;
273 return cdev;
274 }
275 }
276 }
277 rcu_read_unlock();
278
279 log_debug(1 << CXGBI_DBG_DEV,
280 "ndev 0x%p, %s, NO match found.\n", ndev, ndev->name);
281 return NULL;
282 }
283 EXPORT_SYMBOL_GPL(cxgbi_device_find_by_netdev_rcu);
284
cxgbi_device_find_by_mac(struct net_device * ndev,int * port)285 static struct cxgbi_device *cxgbi_device_find_by_mac(struct net_device *ndev,
286 int *port)
287 {
288 struct net_device *vdev = NULL;
289 struct cxgbi_device *cdev, *tmp;
290 int i;
291
292 if (is_vlan_dev(ndev)) {
293 vdev = ndev;
294 ndev = vlan_dev_real_dev(ndev);
295 pr_info("vlan dev %s -> %s.\n", vdev->name, ndev->name);
296 }
297
298 mutex_lock(&cdev_mutex);
299 list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
300 for (i = 0; i < cdev->nports; i++) {
301 if (!memcmp(ndev->dev_addr, cdev->ports[i]->dev_addr,
302 MAX_ADDR_LEN)) {
303 cdev->hbas[i]->vdev = vdev;
304 mutex_unlock(&cdev_mutex);
305 if (port)
306 *port = i;
307 return cdev;
308 }
309 }
310 }
311 mutex_unlock(&cdev_mutex);
312 log_debug(1 << CXGBI_DBG_DEV,
313 "ndev 0x%p, %s, NO match mac found.\n",
314 ndev, ndev->name);
315 return NULL;
316 }
317
cxgbi_hbas_remove(struct cxgbi_device * cdev)318 void cxgbi_hbas_remove(struct cxgbi_device *cdev)
319 {
320 int i;
321 struct cxgbi_hba *chba;
322
323 log_debug(1 << CXGBI_DBG_DEV,
324 "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
325
326 for (i = 0; i < cdev->nports; i++) {
327 chba = cdev->hbas[i];
328 if (chba) {
329 cdev->hbas[i] = NULL;
330 iscsi_host_remove(chba->shost);
331 pci_dev_put(cdev->pdev);
332 iscsi_host_free(chba->shost);
333 }
334 }
335 }
336 EXPORT_SYMBOL_GPL(cxgbi_hbas_remove);
337
cxgbi_hbas_add(struct cxgbi_device * cdev,u64 max_lun,unsigned int max_id,struct scsi_host_template * sht,struct scsi_transport_template * stt)338 int cxgbi_hbas_add(struct cxgbi_device *cdev, u64 max_lun,
339 unsigned int max_id, struct scsi_host_template *sht,
340 struct scsi_transport_template *stt)
341 {
342 struct cxgbi_hba *chba;
343 struct Scsi_Host *shost;
344 int i, err;
345
346 log_debug(1 << CXGBI_DBG_DEV, "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
347
348 for (i = 0; i < cdev->nports; i++) {
349 shost = iscsi_host_alloc(sht, sizeof(*chba), 1);
350 if (!shost) {
351 pr_info("0x%p, p%d, %s, host alloc failed.\n",
352 cdev, i, cdev->ports[i]->name);
353 err = -ENOMEM;
354 goto err_out;
355 }
356
357 shost->transportt = stt;
358 shost->max_lun = max_lun;
359 shost->max_id = max_id;
360 shost->max_channel = 0;
361 shost->max_cmd_len = 16;
362
363 chba = iscsi_host_priv(shost);
364 chba->cdev = cdev;
365 chba->ndev = cdev->ports[i];
366 chba->shost = shost;
367
368 log_debug(1 << CXGBI_DBG_DEV,
369 "cdev 0x%p, p#%d %s: chba 0x%p.\n",
370 cdev, i, cdev->ports[i]->name, chba);
371
372 pci_dev_get(cdev->pdev);
373 err = iscsi_host_add(shost, &cdev->pdev->dev);
374 if (err) {
375 pr_info("cdev 0x%p, p#%d %s, host add failed.\n",
376 cdev, i, cdev->ports[i]->name);
377 pci_dev_put(cdev->pdev);
378 scsi_host_put(shost);
379 goto err_out;
380 }
381
382 cdev->hbas[i] = chba;
383 }
384
385 return 0;
386
387 err_out:
388 cxgbi_hbas_remove(cdev);
389 return err;
390 }
391 EXPORT_SYMBOL_GPL(cxgbi_hbas_add);
392
393 /*
394 * iSCSI offload
395 *
396 * - source port management
397 * To find a free source port in the port allocation map we use a very simple
398 * rotor scheme to look for the next free port.
399 *
400 * If a source port has been specified make sure that it doesn't collide with
401 * our normal source port allocation map. If it's outside the range of our
402 * allocation/deallocation scheme just let them use it.
403 *
404 * If the source port is outside our allocation range, the caller is
405 * responsible for keeping track of their port usage.
406 */
407
find_sock_on_port(struct cxgbi_device * cdev,unsigned char port_id)408 static struct cxgbi_sock *find_sock_on_port(struct cxgbi_device *cdev,
409 unsigned char port_id)
410 {
411 struct cxgbi_ports_map *pmap = &cdev->pmap;
412 unsigned int i;
413 unsigned int used;
414
415 if (!pmap->max_connect || !pmap->used)
416 return NULL;
417
418 spin_lock_bh(&pmap->lock);
419 used = pmap->used;
420 for (i = 0; used && i < pmap->max_connect; i++) {
421 struct cxgbi_sock *csk = pmap->port_csk[i];
422
423 if (csk) {
424 if (csk->port_id == port_id) {
425 spin_unlock_bh(&pmap->lock);
426 return csk;
427 }
428 used--;
429 }
430 }
431 spin_unlock_bh(&pmap->lock);
432
433 return NULL;
434 }
435
sock_get_port(struct cxgbi_sock * csk)436 static int sock_get_port(struct cxgbi_sock *csk)
437 {
438 struct cxgbi_device *cdev = csk->cdev;
439 struct cxgbi_ports_map *pmap = &cdev->pmap;
440 unsigned int start;
441 int idx;
442 __be16 *port;
443
444 if (!pmap->max_connect) {
445 pr_err("cdev 0x%p, p#%u %s, NO port map.\n",
446 cdev, csk->port_id, cdev->ports[csk->port_id]->name);
447 return -EADDRNOTAVAIL;
448 }
449
450 if (csk->csk_family == AF_INET)
451 port = &csk->saddr.sin_port;
452 else /* ipv6 */
453 port = &csk->saddr6.sin6_port;
454
455 if (*port) {
456 pr_err("source port NON-ZERO %u.\n",
457 ntohs(*port));
458 return -EADDRINUSE;
459 }
460
461 spin_lock_bh(&pmap->lock);
462 if (pmap->used >= pmap->max_connect) {
463 spin_unlock_bh(&pmap->lock);
464 pr_info("cdev 0x%p, p#%u %s, ALL ports used.\n",
465 cdev, csk->port_id, cdev->ports[csk->port_id]->name);
466 return -EADDRNOTAVAIL;
467 }
468
469 start = idx = pmap->next;
470 do {
471 if (++idx >= pmap->max_connect)
472 idx = 0;
473 if (!pmap->port_csk[idx]) {
474 pmap->used++;
475 *port = htons(pmap->sport_base + idx);
476 pmap->next = idx;
477 pmap->port_csk[idx] = csk;
478 spin_unlock_bh(&pmap->lock);
479 cxgbi_sock_get(csk);
480 log_debug(1 << CXGBI_DBG_SOCK,
481 "cdev 0x%p, p#%u %s, p %u, %u.\n",
482 cdev, csk->port_id,
483 cdev->ports[csk->port_id]->name,
484 pmap->sport_base + idx, pmap->next);
485 return 0;
486 }
487 } while (idx != start);
488 spin_unlock_bh(&pmap->lock);
489
490 /* should not happen */
491 pr_warn("cdev 0x%p, p#%u %s, next %u?\n",
492 cdev, csk->port_id, cdev->ports[csk->port_id]->name,
493 pmap->next);
494 return -EADDRNOTAVAIL;
495 }
496
sock_put_port(struct cxgbi_sock * csk)497 static void sock_put_port(struct cxgbi_sock *csk)
498 {
499 struct cxgbi_device *cdev = csk->cdev;
500 struct cxgbi_ports_map *pmap = &cdev->pmap;
501 __be16 *port;
502
503 if (csk->csk_family == AF_INET)
504 port = &csk->saddr.sin_port;
505 else /* ipv6 */
506 port = &csk->saddr6.sin6_port;
507
508 if (*port) {
509 int idx = ntohs(*port) - pmap->sport_base;
510
511 *port = 0;
512 if (idx < 0 || idx >= pmap->max_connect) {
513 pr_err("cdev 0x%p, p#%u %s, port %u OOR.\n",
514 cdev, csk->port_id,
515 cdev->ports[csk->port_id]->name,
516 ntohs(*port));
517 return;
518 }
519
520 spin_lock_bh(&pmap->lock);
521 pmap->port_csk[idx] = NULL;
522 pmap->used--;
523 spin_unlock_bh(&pmap->lock);
524
525 log_debug(1 << CXGBI_DBG_SOCK,
526 "cdev 0x%p, p#%u %s, release %u.\n",
527 cdev, csk->port_id, cdev->ports[csk->port_id]->name,
528 pmap->sport_base + idx);
529
530 cxgbi_sock_put(csk);
531 }
532 }
533
534 /*
535 * iscsi tcp connection
536 */
cxgbi_sock_free_cpl_skbs(struct cxgbi_sock * csk)537 void cxgbi_sock_free_cpl_skbs(struct cxgbi_sock *csk)
538 {
539 if (csk->cpl_close) {
540 kfree_skb(csk->cpl_close);
541 csk->cpl_close = NULL;
542 }
543 if (csk->cpl_abort_req) {
544 kfree_skb(csk->cpl_abort_req);
545 csk->cpl_abort_req = NULL;
546 }
547 if (csk->cpl_abort_rpl) {
548 kfree_skb(csk->cpl_abort_rpl);
549 csk->cpl_abort_rpl = NULL;
550 }
551 }
552 EXPORT_SYMBOL_GPL(cxgbi_sock_free_cpl_skbs);
553
cxgbi_sock_create(struct cxgbi_device * cdev)554 static struct cxgbi_sock *cxgbi_sock_create(struct cxgbi_device *cdev)
555 {
556 struct cxgbi_sock *csk = kzalloc(sizeof(*csk), GFP_NOIO);
557
558 if (!csk) {
559 pr_info("alloc csk %zu failed.\n", sizeof(*csk));
560 return NULL;
561 }
562
563 if (cdev->csk_alloc_cpls(csk) < 0) {
564 pr_info("csk 0x%p, alloc cpls failed.\n", csk);
565 kfree(csk);
566 return NULL;
567 }
568
569 spin_lock_init(&csk->lock);
570 kref_init(&csk->refcnt);
571 skb_queue_head_init(&csk->receive_queue);
572 skb_queue_head_init(&csk->write_queue);
573 timer_setup(&csk->retry_timer, NULL, 0);
574 init_completion(&csk->cmpl);
575 rwlock_init(&csk->callback_lock);
576 csk->cdev = cdev;
577 csk->flags = 0;
578 cxgbi_sock_set_state(csk, CTP_CLOSED);
579
580 log_debug(1 << CXGBI_DBG_SOCK, "cdev 0x%p, new csk 0x%p.\n", cdev, csk);
581
582 return csk;
583 }
584
find_route_ipv4(struct flowi4 * fl4,__be32 saddr,__be32 daddr,__be16 sport,__be16 dport,u8 tos,int ifindex)585 static struct rtable *find_route_ipv4(struct flowi4 *fl4,
586 __be32 saddr, __be32 daddr,
587 __be16 sport, __be16 dport, u8 tos,
588 int ifindex)
589 {
590 struct rtable *rt;
591
592 rt = ip_route_output_ports(&init_net, fl4, NULL, daddr, saddr,
593 dport, sport, IPPROTO_TCP, tos, ifindex);
594 if (IS_ERR(rt))
595 return NULL;
596
597 return rt;
598 }
599
600 static struct cxgbi_sock *
cxgbi_check_route(struct sockaddr * dst_addr,int ifindex)601 cxgbi_check_route(struct sockaddr *dst_addr, int ifindex)
602 {
603 struct sockaddr_in *daddr = (struct sockaddr_in *)dst_addr;
604 struct dst_entry *dst;
605 struct net_device *ndev;
606 struct cxgbi_device *cdev;
607 struct rtable *rt = NULL;
608 struct neighbour *n;
609 struct flowi4 fl4;
610 struct cxgbi_sock *csk = NULL;
611 unsigned int mtu = 0;
612 int port = 0xFFFF;
613 int err = 0;
614
615 rt = find_route_ipv4(&fl4, 0, daddr->sin_addr.s_addr, 0,
616 daddr->sin_port, 0, ifindex);
617 if (!rt) {
618 pr_info("no route to ipv4 0x%x, port %u.\n",
619 be32_to_cpu(daddr->sin_addr.s_addr),
620 be16_to_cpu(daddr->sin_port));
621 err = -ENETUNREACH;
622 goto err_out;
623 }
624 dst = &rt->dst;
625 n = dst_neigh_lookup(dst, &daddr->sin_addr.s_addr);
626 if (!n) {
627 err = -ENODEV;
628 goto rel_rt;
629 }
630 ndev = n->dev;
631
632 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
633 pr_info("multi-cast route %pI4, port %u, dev %s.\n",
634 &daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
635 ndev->name);
636 err = -ENETUNREACH;
637 goto rel_neigh;
638 }
639
640 if (ndev->flags & IFF_LOOPBACK) {
641 ndev = ip_dev_find(&init_net, daddr->sin_addr.s_addr);
642 if (!ndev) {
643 err = -ENETUNREACH;
644 goto rel_neigh;
645 }
646 mtu = ndev->mtu;
647 pr_info("rt dev %s, loopback -> %s, mtu %u.\n",
648 n->dev->name, ndev->name, mtu);
649 }
650
651 if (!(ndev->flags & IFF_UP) || !netif_carrier_ok(ndev)) {
652 pr_info("%s interface not up.\n", ndev->name);
653 err = -ENETDOWN;
654 goto rel_neigh;
655 }
656
657 cdev = cxgbi_device_find_by_netdev(ndev, &port);
658 if (!cdev)
659 cdev = cxgbi_device_find_by_mac(ndev, &port);
660 if (!cdev) {
661 pr_info("dst %pI4, %s, NOT cxgbi device.\n",
662 &daddr->sin_addr.s_addr, ndev->name);
663 err = -ENETUNREACH;
664 goto rel_neigh;
665 }
666 log_debug(1 << CXGBI_DBG_SOCK,
667 "route to %pI4 :%u, ndev p#%d,%s, cdev 0x%p.\n",
668 &daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
669 port, ndev->name, cdev);
670
671 csk = cxgbi_sock_create(cdev);
672 if (!csk) {
673 err = -ENOMEM;
674 goto rel_neigh;
675 }
676 csk->cdev = cdev;
677 csk->port_id = port;
678 csk->mtu = mtu;
679 csk->dst = dst;
680
681 csk->csk_family = AF_INET;
682 csk->daddr.sin_addr.s_addr = daddr->sin_addr.s_addr;
683 csk->daddr.sin_port = daddr->sin_port;
684 csk->daddr.sin_family = daddr->sin_family;
685 csk->saddr.sin_family = daddr->sin_family;
686 csk->saddr.sin_addr.s_addr = fl4.saddr;
687 neigh_release(n);
688
689 return csk;
690
691 rel_neigh:
692 neigh_release(n);
693
694 rel_rt:
695 ip_rt_put(rt);
696 err_out:
697 return ERR_PTR(err);
698 }
699
700 #if IS_ENABLED(CONFIG_IPV6)
find_route_ipv6(const struct in6_addr * saddr,const struct in6_addr * daddr,int ifindex)701 static struct rt6_info *find_route_ipv6(const struct in6_addr *saddr,
702 const struct in6_addr *daddr,
703 int ifindex)
704 {
705 struct flowi6 fl;
706
707 memset(&fl, 0, sizeof(fl));
708 fl.flowi6_oif = ifindex;
709 if (saddr)
710 memcpy(&fl.saddr, saddr, sizeof(struct in6_addr));
711 if (daddr)
712 memcpy(&fl.daddr, daddr, sizeof(struct in6_addr));
713 return (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
714 }
715
716 static struct cxgbi_sock *
cxgbi_check_route6(struct sockaddr * dst_addr,int ifindex)717 cxgbi_check_route6(struct sockaddr *dst_addr, int ifindex)
718 {
719 struct sockaddr_in6 *daddr6 = (struct sockaddr_in6 *)dst_addr;
720 struct dst_entry *dst;
721 struct net_device *ndev;
722 struct cxgbi_device *cdev;
723 struct rt6_info *rt = NULL;
724 struct neighbour *n;
725 struct in6_addr pref_saddr;
726 struct cxgbi_sock *csk = NULL;
727 unsigned int mtu = 0;
728 int port = 0xFFFF;
729 int err = 0;
730
731 rt = find_route_ipv6(NULL, &daddr6->sin6_addr, ifindex);
732
733 if (!rt) {
734 pr_info("no route to ipv6 %pI6 port %u\n",
735 daddr6->sin6_addr.s6_addr,
736 be16_to_cpu(daddr6->sin6_port));
737 err = -ENETUNREACH;
738 goto err_out;
739 }
740
741 dst = &rt->dst;
742
743 n = dst_neigh_lookup(dst, &daddr6->sin6_addr);
744
745 if (!n) {
746 pr_info("%pI6, port %u, dst no neighbour.\n",
747 daddr6->sin6_addr.s6_addr,
748 be16_to_cpu(daddr6->sin6_port));
749 err = -ENETUNREACH;
750 goto rel_rt;
751 }
752 ndev = n->dev;
753
754 if (!(ndev->flags & IFF_UP) || !netif_carrier_ok(ndev)) {
755 pr_info("%s interface not up.\n", ndev->name);
756 err = -ENETDOWN;
757 goto rel_rt;
758 }
759
760 if (ipv6_addr_is_multicast(&daddr6->sin6_addr)) {
761 pr_info("multi-cast route %pI6 port %u, dev %s.\n",
762 daddr6->sin6_addr.s6_addr,
763 ntohs(daddr6->sin6_port), ndev->name);
764 err = -ENETUNREACH;
765 goto rel_rt;
766 }
767
768 cdev = cxgbi_device_find_by_netdev(ndev, &port);
769 if (!cdev)
770 cdev = cxgbi_device_find_by_mac(ndev, &port);
771 if (!cdev) {
772 pr_info("dst %pI6 %s, NOT cxgbi device.\n",
773 daddr6->sin6_addr.s6_addr, ndev->name);
774 err = -ENETUNREACH;
775 goto rel_rt;
776 }
777 log_debug(1 << CXGBI_DBG_SOCK,
778 "route to %pI6 :%u, ndev p#%d,%s, cdev 0x%p.\n",
779 daddr6->sin6_addr.s6_addr, ntohs(daddr6->sin6_port), port,
780 ndev->name, cdev);
781
782 csk = cxgbi_sock_create(cdev);
783 if (!csk) {
784 err = -ENOMEM;
785 goto rel_rt;
786 }
787 csk->cdev = cdev;
788 csk->port_id = port;
789 csk->mtu = mtu;
790 csk->dst = dst;
791
792 rt6_get_prefsrc(rt, &pref_saddr);
793 if (ipv6_addr_any(&pref_saddr)) {
794 struct inet6_dev *idev = ip6_dst_idev((struct dst_entry *)rt);
795
796 err = ipv6_dev_get_saddr(&init_net, idev ? idev->dev : NULL,
797 &daddr6->sin6_addr, 0, &pref_saddr);
798 if (err) {
799 pr_info("failed to get source address to reach %pI6\n",
800 &daddr6->sin6_addr);
801 goto rel_rt;
802 }
803 }
804
805 csk->csk_family = AF_INET6;
806 csk->daddr6.sin6_addr = daddr6->sin6_addr;
807 csk->daddr6.sin6_port = daddr6->sin6_port;
808 csk->daddr6.sin6_family = daddr6->sin6_family;
809 csk->saddr6.sin6_family = daddr6->sin6_family;
810 csk->saddr6.sin6_addr = pref_saddr;
811
812 neigh_release(n);
813 return csk;
814
815 rel_rt:
816 if (n)
817 neigh_release(n);
818
819 ip6_rt_put(rt);
820 if (csk)
821 cxgbi_sock_closed(csk);
822 err_out:
823 return ERR_PTR(err);
824 }
825 #endif /* IS_ENABLED(CONFIG_IPV6) */
826
cxgbi_sock_established(struct cxgbi_sock * csk,unsigned int snd_isn,unsigned int opt)827 void cxgbi_sock_established(struct cxgbi_sock *csk, unsigned int snd_isn,
828 unsigned int opt)
829 {
830 csk->write_seq = csk->snd_nxt = csk->snd_una = snd_isn;
831 dst_confirm(csk->dst);
832 smp_mb();
833 cxgbi_sock_set_state(csk, CTP_ESTABLISHED);
834 }
835 EXPORT_SYMBOL_GPL(cxgbi_sock_established);
836
cxgbi_inform_iscsi_conn_closing(struct cxgbi_sock * csk)837 static void cxgbi_inform_iscsi_conn_closing(struct cxgbi_sock *csk)
838 {
839 log_debug(1 << CXGBI_DBG_SOCK,
840 "csk 0x%p, state %u, flags 0x%lx, conn 0x%p.\n",
841 csk, csk->state, csk->flags, csk->user_data);
842
843 if (csk->state != CTP_ESTABLISHED) {
844 read_lock_bh(&csk->callback_lock);
845 if (csk->user_data)
846 iscsi_conn_failure(csk->user_data,
847 ISCSI_ERR_TCP_CONN_CLOSE);
848 read_unlock_bh(&csk->callback_lock);
849 }
850 }
851
cxgbi_sock_closed(struct cxgbi_sock * csk)852 void cxgbi_sock_closed(struct cxgbi_sock *csk)
853 {
854 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
855 csk, (csk)->state, (csk)->flags, (csk)->tid);
856 cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
857 if (csk->state == CTP_ACTIVE_OPEN || csk->state == CTP_CLOSED)
858 return;
859 if (csk->saddr.sin_port)
860 sock_put_port(csk);
861 if (csk->dst)
862 dst_release(csk->dst);
863 csk->cdev->csk_release_offload_resources(csk);
864 cxgbi_sock_set_state(csk, CTP_CLOSED);
865 cxgbi_inform_iscsi_conn_closing(csk);
866 cxgbi_sock_put(csk);
867 }
868 EXPORT_SYMBOL_GPL(cxgbi_sock_closed);
869
need_active_close(struct cxgbi_sock * csk)870 static void need_active_close(struct cxgbi_sock *csk)
871 {
872 int data_lost;
873 int close_req = 0;
874
875 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
876 csk, (csk)->state, (csk)->flags, (csk)->tid);
877 spin_lock_bh(&csk->lock);
878 if (csk->dst)
879 dst_confirm(csk->dst);
880 data_lost = skb_queue_len(&csk->receive_queue);
881 __skb_queue_purge(&csk->receive_queue);
882
883 if (csk->state == CTP_ACTIVE_OPEN)
884 cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
885 else if (csk->state == CTP_ESTABLISHED) {
886 close_req = 1;
887 cxgbi_sock_set_state(csk, CTP_ACTIVE_CLOSE);
888 } else if (csk->state == CTP_PASSIVE_CLOSE) {
889 close_req = 1;
890 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
891 }
892
893 if (close_req) {
894 if (!cxgbi_sock_flag(csk, CTPF_LOGOUT_RSP_RCVD) ||
895 data_lost)
896 csk->cdev->csk_send_abort_req(csk);
897 else
898 csk->cdev->csk_send_close_req(csk);
899 }
900
901 spin_unlock_bh(&csk->lock);
902 }
903
cxgbi_sock_fail_act_open(struct cxgbi_sock * csk,int errno)904 void cxgbi_sock_fail_act_open(struct cxgbi_sock *csk, int errno)
905 {
906 pr_info("csk 0x%p,%u,%lx, %pI4:%u-%pI4:%u, err %d.\n",
907 csk, csk->state, csk->flags,
908 &csk->saddr.sin_addr.s_addr, csk->saddr.sin_port,
909 &csk->daddr.sin_addr.s_addr, csk->daddr.sin_port,
910 errno);
911
912 cxgbi_sock_set_state(csk, CTP_CONNECTING);
913 csk->err = errno;
914 cxgbi_sock_closed(csk);
915 }
916 EXPORT_SYMBOL_GPL(cxgbi_sock_fail_act_open);
917
cxgbi_sock_act_open_req_arp_failure(void * handle,struct sk_buff * skb)918 void cxgbi_sock_act_open_req_arp_failure(void *handle, struct sk_buff *skb)
919 {
920 struct cxgbi_sock *csk = (struct cxgbi_sock *)skb->sk;
921 struct module *owner = csk->cdev->owner;
922
923 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
924 csk, (csk)->state, (csk)->flags, (csk)->tid);
925 cxgbi_sock_get(csk);
926 spin_lock_bh(&csk->lock);
927 if (csk->state == CTP_ACTIVE_OPEN)
928 cxgbi_sock_fail_act_open(csk, -EHOSTUNREACH);
929 spin_unlock_bh(&csk->lock);
930 cxgbi_sock_put(csk);
931 __kfree_skb(skb);
932
933 module_put(owner);
934 }
935 EXPORT_SYMBOL_GPL(cxgbi_sock_act_open_req_arp_failure);
936
cxgbi_sock_rcv_abort_rpl(struct cxgbi_sock * csk)937 void cxgbi_sock_rcv_abort_rpl(struct cxgbi_sock *csk)
938 {
939 cxgbi_sock_get(csk);
940 spin_lock_bh(&csk->lock);
941
942 cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_RCVD);
943 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
944 cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_PENDING);
945 if (cxgbi_sock_flag(csk, CTPF_ABORT_REQ_RCVD))
946 pr_err("csk 0x%p,%u,0x%lx,%u,ABT_RPL_RSS.\n",
947 csk, csk->state, csk->flags, csk->tid);
948 cxgbi_sock_closed(csk);
949 }
950
951 spin_unlock_bh(&csk->lock);
952 cxgbi_sock_put(csk);
953 }
954 EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_abort_rpl);
955
cxgbi_sock_rcv_peer_close(struct cxgbi_sock * csk)956 void cxgbi_sock_rcv_peer_close(struct cxgbi_sock *csk)
957 {
958 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
959 csk, (csk)->state, (csk)->flags, (csk)->tid);
960 cxgbi_sock_get(csk);
961 spin_lock_bh(&csk->lock);
962
963 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
964 goto done;
965
966 switch (csk->state) {
967 case CTP_ESTABLISHED:
968 cxgbi_sock_set_state(csk, CTP_PASSIVE_CLOSE);
969 break;
970 case CTP_ACTIVE_CLOSE:
971 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
972 break;
973 case CTP_CLOSE_WAIT_1:
974 cxgbi_sock_closed(csk);
975 break;
976 case CTP_ABORTING:
977 break;
978 default:
979 pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
980 csk, csk->state, csk->flags, csk->tid);
981 }
982 cxgbi_inform_iscsi_conn_closing(csk);
983 done:
984 spin_unlock_bh(&csk->lock);
985 cxgbi_sock_put(csk);
986 }
987 EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_peer_close);
988
cxgbi_sock_rcv_close_conn_rpl(struct cxgbi_sock * csk,u32 snd_nxt)989 void cxgbi_sock_rcv_close_conn_rpl(struct cxgbi_sock *csk, u32 snd_nxt)
990 {
991 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
992 csk, (csk)->state, (csk)->flags, (csk)->tid);
993 cxgbi_sock_get(csk);
994 spin_lock_bh(&csk->lock);
995
996 csk->snd_una = snd_nxt - 1;
997 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
998 goto done;
999
1000 switch (csk->state) {
1001 case CTP_ACTIVE_CLOSE:
1002 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_1);
1003 break;
1004 case CTP_CLOSE_WAIT_1:
1005 case CTP_CLOSE_WAIT_2:
1006 cxgbi_sock_closed(csk);
1007 break;
1008 case CTP_ABORTING:
1009 break;
1010 default:
1011 pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
1012 csk, csk->state, csk->flags, csk->tid);
1013 }
1014 done:
1015 spin_unlock_bh(&csk->lock);
1016 cxgbi_sock_put(csk);
1017 }
1018 EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_close_conn_rpl);
1019
cxgbi_sock_rcv_wr_ack(struct cxgbi_sock * csk,unsigned int credits,unsigned int snd_una,int seq_chk)1020 void cxgbi_sock_rcv_wr_ack(struct cxgbi_sock *csk, unsigned int credits,
1021 unsigned int snd_una, int seq_chk)
1022 {
1023 log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
1024 "csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, snd_una %u,%d.\n",
1025 csk, csk->state, csk->flags, csk->tid, credits,
1026 csk->wr_cred, csk->wr_una_cred, snd_una, seq_chk);
1027
1028 spin_lock_bh(&csk->lock);
1029
1030 csk->wr_cred += credits;
1031 if (csk->wr_una_cred > csk->wr_max_cred - csk->wr_cred)
1032 csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
1033
1034 while (credits) {
1035 struct sk_buff *p = cxgbi_sock_peek_wr(csk);
1036
1037 if (unlikely(!p)) {
1038 pr_err("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, empty.\n",
1039 csk, csk->state, csk->flags, csk->tid, credits,
1040 csk->wr_cred, csk->wr_una_cred);
1041 break;
1042 }
1043
1044 if (unlikely(credits < p->csum)) {
1045 pr_warn("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, < %u.\n",
1046 csk, csk->state, csk->flags, csk->tid,
1047 credits, csk->wr_cred, csk->wr_una_cred,
1048 p->csum);
1049 p->csum -= credits;
1050 break;
1051 } else {
1052 cxgbi_sock_dequeue_wr(csk);
1053 credits -= p->csum;
1054 kfree_skb(p);
1055 }
1056 }
1057
1058 cxgbi_sock_check_wr_invariants(csk);
1059
1060 if (seq_chk) {
1061 if (unlikely(before(snd_una, csk->snd_una))) {
1062 pr_warn("csk 0x%p,%u,0x%lx,%u, snd_una %u/%u.",
1063 csk, csk->state, csk->flags, csk->tid, snd_una,
1064 csk->snd_una);
1065 goto done;
1066 }
1067
1068 if (csk->snd_una != snd_una) {
1069 csk->snd_una = snd_una;
1070 dst_confirm(csk->dst);
1071 }
1072 }
1073
1074 if (skb_queue_len(&csk->write_queue)) {
1075 if (csk->cdev->csk_push_tx_frames(csk, 0))
1076 cxgbi_conn_tx_open(csk);
1077 } else
1078 cxgbi_conn_tx_open(csk);
1079 done:
1080 spin_unlock_bh(&csk->lock);
1081 }
1082 EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_wr_ack);
1083
cxgbi_sock_find_best_mtu(struct cxgbi_sock * csk,unsigned short mtu)1084 static unsigned int cxgbi_sock_find_best_mtu(struct cxgbi_sock *csk,
1085 unsigned short mtu)
1086 {
1087 int i = 0;
1088
1089 while (i < csk->cdev->nmtus - 1 && csk->cdev->mtus[i + 1] <= mtu)
1090 ++i;
1091
1092 return i;
1093 }
1094
cxgbi_sock_select_mss(struct cxgbi_sock * csk,unsigned int pmtu)1095 unsigned int cxgbi_sock_select_mss(struct cxgbi_sock *csk, unsigned int pmtu)
1096 {
1097 unsigned int idx;
1098 struct dst_entry *dst = csk->dst;
1099
1100 csk->advmss = dst_metric_advmss(dst);
1101
1102 if (csk->advmss > pmtu - 40)
1103 csk->advmss = pmtu - 40;
1104 if (csk->advmss < csk->cdev->mtus[0] - 40)
1105 csk->advmss = csk->cdev->mtus[0] - 40;
1106 idx = cxgbi_sock_find_best_mtu(csk, csk->advmss + 40);
1107
1108 return idx;
1109 }
1110 EXPORT_SYMBOL_GPL(cxgbi_sock_select_mss);
1111
cxgbi_sock_skb_entail(struct cxgbi_sock * csk,struct sk_buff * skb)1112 void cxgbi_sock_skb_entail(struct cxgbi_sock *csk, struct sk_buff *skb)
1113 {
1114 cxgbi_skcb_tcp_seq(skb) = csk->write_seq;
1115 __skb_queue_tail(&csk->write_queue, skb);
1116 }
1117 EXPORT_SYMBOL_GPL(cxgbi_sock_skb_entail);
1118
cxgbi_sock_purge_wr_queue(struct cxgbi_sock * csk)1119 void cxgbi_sock_purge_wr_queue(struct cxgbi_sock *csk)
1120 {
1121 struct sk_buff *skb;
1122
1123 while ((skb = cxgbi_sock_dequeue_wr(csk)) != NULL)
1124 kfree_skb(skb);
1125 }
1126 EXPORT_SYMBOL_GPL(cxgbi_sock_purge_wr_queue);
1127
cxgbi_sock_check_wr_invariants(const struct cxgbi_sock * csk)1128 void cxgbi_sock_check_wr_invariants(const struct cxgbi_sock *csk)
1129 {
1130 int pending = cxgbi_sock_count_pending_wrs(csk);
1131
1132 if (unlikely(csk->wr_cred + pending != csk->wr_max_cred))
1133 pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
1134 csk, csk->tid, csk->wr_cred, pending, csk->wr_max_cred);
1135 }
1136 EXPORT_SYMBOL_GPL(cxgbi_sock_check_wr_invariants);
1137
cxgbi_sock_send_pdus(struct cxgbi_sock * csk,struct sk_buff * skb)1138 static int cxgbi_sock_send_pdus(struct cxgbi_sock *csk, struct sk_buff *skb)
1139 {
1140 struct cxgbi_device *cdev = csk->cdev;
1141 struct sk_buff *next;
1142 int err, copied = 0;
1143
1144 spin_lock_bh(&csk->lock);
1145
1146 if (csk->state != CTP_ESTABLISHED) {
1147 log_debug(1 << CXGBI_DBG_PDU_TX,
1148 "csk 0x%p,%u,0x%lx,%u, EAGAIN.\n",
1149 csk, csk->state, csk->flags, csk->tid);
1150 err = -EAGAIN;
1151 goto out_err;
1152 }
1153
1154 if (csk->err) {
1155 log_debug(1 << CXGBI_DBG_PDU_TX,
1156 "csk 0x%p,%u,0x%lx,%u, EPIPE %d.\n",
1157 csk, csk->state, csk->flags, csk->tid, csk->err);
1158 err = -EPIPE;
1159 goto out_err;
1160 }
1161
1162 if (csk->write_seq - csk->snd_una >= csk->snd_win) {
1163 log_debug(1 << CXGBI_DBG_PDU_TX,
1164 "csk 0x%p,%u,0x%lx,%u, FULL %u-%u >= %u.\n",
1165 csk, csk->state, csk->flags, csk->tid, csk->write_seq,
1166 csk->snd_una, csk->snd_win);
1167 err = -ENOBUFS;
1168 goto out_err;
1169 }
1170
1171 while (skb) {
1172 int frags = skb_shinfo(skb)->nr_frags +
1173 (skb->len != skb->data_len);
1174
1175 if (unlikely(skb_headroom(skb) < cdev->skb_tx_rsvd)) {
1176 pr_err("csk 0x%p, skb head %u < %u.\n",
1177 csk, skb_headroom(skb), cdev->skb_tx_rsvd);
1178 err = -EINVAL;
1179 goto out_err;
1180 }
1181
1182 if (frags >= SKB_WR_LIST_SIZE) {
1183 pr_err("csk 0x%p, frags %d, %u,%u >%u.\n",
1184 csk, skb_shinfo(skb)->nr_frags, skb->len,
1185 skb->data_len, (uint)(SKB_WR_LIST_SIZE));
1186 err = -EINVAL;
1187 goto out_err;
1188 }
1189
1190 next = skb->next;
1191 skb->next = NULL;
1192 cxgbi_skcb_set_flag(skb, SKCBF_TX_NEED_HDR);
1193 cxgbi_sock_skb_entail(csk, skb);
1194 copied += skb->len;
1195 csk->write_seq += skb->len +
1196 cxgbi_ulp_extra_len(cxgbi_skcb_ulp_mode(skb));
1197 skb = next;
1198 }
1199
1200 if (likely(skb_queue_len(&csk->write_queue)))
1201 cdev->csk_push_tx_frames(csk, 1);
1202 done:
1203 spin_unlock_bh(&csk->lock);
1204 return copied;
1205
1206 out_err:
1207 if (copied == 0 && err == -EPIPE)
1208 copied = csk->err ? csk->err : -EPIPE;
1209 else
1210 copied = err;
1211 goto done;
1212 }
1213
1214 static inline void
scmd_get_params(struct scsi_cmnd * sc,struct scatterlist ** sgl,unsigned int * sgcnt,unsigned int * dlen,unsigned int prot)1215 scmd_get_params(struct scsi_cmnd *sc, struct scatterlist **sgl,
1216 unsigned int *sgcnt, unsigned int *dlen,
1217 unsigned int prot)
1218 {
1219 struct scsi_data_buffer *sdb = prot ? scsi_prot(sc) : &sc->sdb;
1220
1221 *sgl = sdb->table.sgl;
1222 *sgcnt = sdb->table.nents;
1223 *dlen = sdb->length;
1224 /* Caution: for protection sdb, sdb->length is invalid */
1225 }
1226
cxgbi_ddp_set_one_ppod(struct cxgbi_pagepod * ppod,struct cxgbi_task_tag_info * ttinfo,struct scatterlist ** sg_pp,unsigned int * sg_off)1227 void cxgbi_ddp_set_one_ppod(struct cxgbi_pagepod *ppod,
1228 struct cxgbi_task_tag_info *ttinfo,
1229 struct scatterlist **sg_pp, unsigned int *sg_off)
1230 {
1231 struct scatterlist *sg = sg_pp ? *sg_pp : NULL;
1232 unsigned int offset = sg_off ? *sg_off : 0;
1233 dma_addr_t addr = 0UL;
1234 unsigned int len = 0;
1235 int i;
1236
1237 memcpy(ppod, &ttinfo->hdr, sizeof(struct cxgbi_pagepod_hdr));
1238
1239 if (sg) {
1240 addr = sg_dma_address(sg);
1241 len = sg_dma_len(sg);
1242 }
1243
1244 for (i = 0; i < PPOD_PAGES_MAX; i++) {
1245 if (sg) {
1246 ppod->addr[i] = cpu_to_be64(addr + offset);
1247 offset += PAGE_SIZE;
1248 if (offset == (len + sg->offset)) {
1249 offset = 0;
1250 sg = sg_next(sg);
1251 if (sg) {
1252 addr = sg_dma_address(sg);
1253 len = sg_dma_len(sg);
1254 }
1255 }
1256 } else {
1257 ppod->addr[i] = 0ULL;
1258 }
1259 }
1260
1261 /*
1262 * the fifth address needs to be repeated in the next ppod, so do
1263 * not move sg
1264 */
1265 if (sg_pp) {
1266 *sg_pp = sg;
1267 *sg_off = offset;
1268 }
1269
1270 if (offset == len) {
1271 offset = 0;
1272 sg = sg_next(sg);
1273 if (sg) {
1274 addr = sg_dma_address(sg);
1275 len = sg_dma_len(sg);
1276 }
1277 }
1278 ppod->addr[i] = sg ? cpu_to_be64(addr + offset) : 0ULL;
1279 }
1280 EXPORT_SYMBOL_GPL(cxgbi_ddp_set_one_ppod);
1281
1282 /*
1283 * APIs interacting with open-iscsi libraries
1284 */
1285
1286 static unsigned char padding[4];
1287
cxgbi_ddp_ppm_setup(void ** ppm_pp,struct cxgbi_device * cdev,struct cxgbi_tag_format * tformat,unsigned int iscsi_size,unsigned int llimit,unsigned int start,unsigned int rsvd_factor,unsigned int edram_start,unsigned int edram_size)1288 int cxgbi_ddp_ppm_setup(void **ppm_pp, struct cxgbi_device *cdev,
1289 struct cxgbi_tag_format *tformat,
1290 unsigned int iscsi_size, unsigned int llimit,
1291 unsigned int start, unsigned int rsvd_factor,
1292 unsigned int edram_start, unsigned int edram_size)
1293 {
1294 int err = cxgbi_ppm_init(ppm_pp, cdev->ports[0], cdev->pdev,
1295 cdev->lldev, tformat, iscsi_size, llimit, start,
1296 rsvd_factor, edram_start, edram_size);
1297
1298 if (err >= 0) {
1299 struct cxgbi_ppm *ppm = (struct cxgbi_ppm *)(*ppm_pp);
1300
1301 if (ppm->ppmax < 1024 ||
1302 ppm->tformat.pgsz_idx_dflt >= DDP_PGIDX_MAX)
1303 cdev->flags |= CXGBI_FLAG_DDP_OFF;
1304 err = 0;
1305 } else {
1306 cdev->flags |= CXGBI_FLAG_DDP_OFF;
1307 }
1308
1309 return err;
1310 }
1311 EXPORT_SYMBOL_GPL(cxgbi_ddp_ppm_setup);
1312
cxgbi_ddp_sgl_check(struct scatterlist * sgl,int nents)1313 static int cxgbi_ddp_sgl_check(struct scatterlist *sgl, int nents)
1314 {
1315 int i;
1316 int last_sgidx = nents - 1;
1317 struct scatterlist *sg = sgl;
1318
1319 for (i = 0; i < nents; i++, sg = sg_next(sg)) {
1320 unsigned int len = sg->length + sg->offset;
1321
1322 if ((sg->offset & 0x3) || (i && sg->offset) ||
1323 ((i != last_sgidx) && len != PAGE_SIZE)) {
1324 log_debug(1 << CXGBI_DBG_DDP,
1325 "sg %u/%u, %u,%u, not aligned.\n",
1326 i, nents, sg->offset, sg->length);
1327 goto err_out;
1328 }
1329 }
1330 return 0;
1331 err_out:
1332 return -EINVAL;
1333 }
1334
cxgbi_ddp_reserve(struct cxgbi_conn * cconn,struct cxgbi_task_data * tdata,u32 sw_tag,unsigned int xferlen)1335 static int cxgbi_ddp_reserve(struct cxgbi_conn *cconn,
1336 struct cxgbi_task_data *tdata, u32 sw_tag,
1337 unsigned int xferlen)
1338 {
1339 struct cxgbi_sock *csk = cconn->cep->csk;
1340 struct cxgbi_device *cdev = csk->cdev;
1341 struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
1342 struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
1343 struct scatterlist *sgl = ttinfo->sgl;
1344 unsigned int sgcnt = ttinfo->nents;
1345 unsigned int sg_offset = sgl->offset;
1346 int err;
1347
1348 if (cdev->flags & CXGBI_FLAG_DDP_OFF) {
1349 log_debug(1 << CXGBI_DBG_DDP,
1350 "cdev 0x%p DDP off.\n", cdev);
1351 return -EINVAL;
1352 }
1353
1354 if (!ppm || xferlen < DDP_THRESHOLD || !sgcnt ||
1355 ppm->tformat.pgsz_idx_dflt >= DDP_PGIDX_MAX) {
1356 log_debug(1 << CXGBI_DBG_DDP,
1357 "ppm 0x%p, pgidx %u, xfer %u, sgcnt %u, NO ddp.\n",
1358 ppm, ppm ? ppm->tformat.pgsz_idx_dflt : DDP_PGIDX_MAX,
1359 xferlen, ttinfo->nents);
1360 return -EINVAL;
1361 }
1362
1363 /* make sure the buffer is suitable for ddp */
1364 if (cxgbi_ddp_sgl_check(sgl, sgcnt) < 0)
1365 return -EINVAL;
1366
1367 ttinfo->nr_pages = (xferlen + sgl->offset + (1 << PAGE_SHIFT) - 1) >>
1368 PAGE_SHIFT;
1369
1370 /*
1371 * the ddp tag will be used for the itt in the outgoing pdu,
1372 * the itt genrated by libiscsi is saved in the ppm and can be
1373 * retrieved via the ddp tag
1374 */
1375 err = cxgbi_ppm_ppods_reserve(ppm, ttinfo->nr_pages, 0, &ttinfo->idx,
1376 &ttinfo->tag, (unsigned long)sw_tag);
1377 if (err < 0) {
1378 cconn->ddp_full++;
1379 return err;
1380 }
1381 ttinfo->npods = err;
1382
1383 /* setup dma from scsi command sgl */
1384 sgl->offset = 0;
1385 err = dma_map_sg(&ppm->pdev->dev, sgl, sgcnt, DMA_FROM_DEVICE);
1386 sgl->offset = sg_offset;
1387 if (err == 0) {
1388 pr_info("%s: 0x%x, xfer %u, sgl %u dma mapping err.\n",
1389 __func__, sw_tag, xferlen, sgcnt);
1390 goto rel_ppods;
1391 }
1392 if (err != ttinfo->nr_pages) {
1393 log_debug(1 << CXGBI_DBG_DDP,
1394 "%s: sw tag 0x%x, xfer %u, sgl %u, dma count %d.\n",
1395 __func__, sw_tag, xferlen, sgcnt, err);
1396 }
1397
1398 ttinfo->flags |= CXGBI_PPOD_INFO_FLAG_MAPPED;
1399 ttinfo->cid = csk->port_id;
1400
1401 cxgbi_ppm_make_ppod_hdr(ppm, ttinfo->tag, csk->tid, sgl->offset,
1402 xferlen, &ttinfo->hdr);
1403
1404 if (cdev->flags & CXGBI_FLAG_USE_PPOD_OFLDQ) {
1405 /* write ppod from xmit_pdu (of iscsi_scsi_command pdu) */
1406 ttinfo->flags |= CXGBI_PPOD_INFO_FLAG_VALID;
1407 } else {
1408 /* write ppod from control queue now */
1409 err = cdev->csk_ddp_set_map(ppm, csk, ttinfo);
1410 if (err < 0)
1411 goto rel_ppods;
1412 }
1413
1414 return 0;
1415
1416 rel_ppods:
1417 cxgbi_ppm_ppod_release(ppm, ttinfo->idx);
1418
1419 if (ttinfo->flags & CXGBI_PPOD_INFO_FLAG_MAPPED) {
1420 ttinfo->flags &= ~CXGBI_PPOD_INFO_FLAG_MAPPED;
1421 dma_unmap_sg(&ppm->pdev->dev, sgl, sgcnt, DMA_FROM_DEVICE);
1422 }
1423 return -EINVAL;
1424 }
1425
task_release_itt(struct iscsi_task * task,itt_t hdr_itt)1426 static void task_release_itt(struct iscsi_task *task, itt_t hdr_itt)
1427 {
1428 struct scsi_cmnd *sc = task->sc;
1429 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
1430 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1431 struct cxgbi_device *cdev = cconn->chba->cdev;
1432 struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
1433 u32 tag = ntohl((__force u32)hdr_itt);
1434
1435 log_debug(1 << CXGBI_DBG_DDP,
1436 "cdev 0x%p, task 0x%p, release tag 0x%x.\n",
1437 cdev, task, tag);
1438 if (sc && sc->sc_data_direction == DMA_FROM_DEVICE &&
1439 cxgbi_ppm_is_ddp_tag(ppm, tag)) {
1440 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1441 struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
1442
1443 if (!(cdev->flags & CXGBI_FLAG_USE_PPOD_OFLDQ))
1444 cdev->csk_ddp_clear_map(cdev, ppm, ttinfo);
1445 cxgbi_ppm_ppod_release(ppm, ttinfo->idx);
1446 dma_unmap_sg(&ppm->pdev->dev, ttinfo->sgl, ttinfo->nents,
1447 DMA_FROM_DEVICE);
1448 }
1449 }
1450
cxgbi_build_sw_tag(u32 idx,u32 age)1451 static inline u32 cxgbi_build_sw_tag(u32 idx, u32 age)
1452 {
1453 /* assume idx and age both are < 0x7FFF (32767) */
1454 return (idx << 16) | age;
1455 }
1456
task_reserve_itt(struct iscsi_task * task,itt_t * hdr_itt)1457 static int task_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
1458 {
1459 struct scsi_cmnd *sc = task->sc;
1460 struct iscsi_conn *conn = task->conn;
1461 struct iscsi_session *sess = conn->session;
1462 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1463 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1464 struct cxgbi_device *cdev = cconn->chba->cdev;
1465 struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
1466 u32 sw_tag = cxgbi_build_sw_tag(task->itt, sess->age);
1467 u32 tag = 0;
1468 int err = -EINVAL;
1469
1470 if (sc && sc->sc_data_direction == DMA_FROM_DEVICE) {
1471 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1472 struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
1473
1474 scmd_get_params(sc, &ttinfo->sgl, &ttinfo->nents,
1475 &tdata->dlen, 0);
1476 err = cxgbi_ddp_reserve(cconn, tdata, sw_tag, tdata->dlen);
1477 if (!err)
1478 tag = ttinfo->tag;
1479 else
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, tdata->dlen,
1483 ttinfo->nents);
1484 }
1485
1486 if (err < 0) {
1487 err = cxgbi_ppm_make_non_ddp_tag(ppm, sw_tag, &tag);
1488 if (err < 0)
1489 return err;
1490 }
1491 /* the itt need to sent in big-endian order */
1492 *hdr_itt = (__force itt_t)htonl(tag);
1493
1494 log_debug(1 << CXGBI_DBG_DDP,
1495 "cdev 0x%p, task 0x%p, 0x%x(0x%x,0x%x)->0x%x/0x%x.\n",
1496 cdev, task, sw_tag, task->itt, sess->age, tag, *hdr_itt);
1497 return 0;
1498 }
1499
cxgbi_parse_pdu_itt(struct iscsi_conn * conn,itt_t itt,int * idx,int * age)1500 void cxgbi_parse_pdu_itt(struct iscsi_conn *conn, itt_t itt, int *idx, int *age)
1501 {
1502 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1503 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1504 struct cxgbi_device *cdev = cconn->chba->cdev;
1505 struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
1506 u32 tag = ntohl((__force u32)itt);
1507 u32 sw_bits;
1508
1509 if (ppm) {
1510 if (cxgbi_ppm_is_ddp_tag(ppm, tag))
1511 sw_bits = cxgbi_ppm_get_tag_caller_data(ppm, tag);
1512 else
1513 sw_bits = cxgbi_ppm_decode_non_ddp_tag(ppm, tag);
1514 } else {
1515 sw_bits = tag;
1516 }
1517
1518 cxgbi_decode_sw_tag(sw_bits, idx, age);
1519 log_debug(1 << CXGBI_DBG_DDP,
1520 "cdev 0x%p, tag 0x%x/0x%x, -> 0x%x(0x%x,0x%x).\n",
1521 cdev, tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
1522 age ? *age : 0xFF);
1523 }
1524 EXPORT_SYMBOL_GPL(cxgbi_parse_pdu_itt);
1525
cxgbi_conn_tx_open(struct cxgbi_sock * csk)1526 void cxgbi_conn_tx_open(struct cxgbi_sock *csk)
1527 {
1528 struct iscsi_conn *conn = csk->user_data;
1529
1530 if (conn) {
1531 log_debug(1 << CXGBI_DBG_SOCK,
1532 "csk 0x%p, cid %d.\n", csk, conn->id);
1533 iscsi_conn_queue_work(conn);
1534 }
1535 }
1536 EXPORT_SYMBOL_GPL(cxgbi_conn_tx_open);
1537
1538 /*
1539 * pdu receive, interact with libiscsi_tcp
1540 */
read_pdu_skb(struct iscsi_conn * conn,struct sk_buff * skb,unsigned int offset,int offloaded)1541 static inline int read_pdu_skb(struct iscsi_conn *conn,
1542 struct sk_buff *skb,
1543 unsigned int offset,
1544 int offloaded)
1545 {
1546 int status = 0;
1547 int bytes_read;
1548
1549 bytes_read = iscsi_tcp_recv_skb(conn, skb, offset, offloaded, &status);
1550 switch (status) {
1551 case ISCSI_TCP_CONN_ERR:
1552 pr_info("skb 0x%p, off %u, %d, TCP_ERR.\n",
1553 skb, offset, offloaded);
1554 return -EIO;
1555 case ISCSI_TCP_SUSPENDED:
1556 log_debug(1 << CXGBI_DBG_PDU_RX,
1557 "skb 0x%p, off %u, %d, TCP_SUSPEND, rc %d.\n",
1558 skb, offset, offloaded, bytes_read);
1559 /* no transfer - just have caller flush queue */
1560 return bytes_read;
1561 case ISCSI_TCP_SKB_DONE:
1562 pr_info("skb 0x%p, off %u, %d, TCP_SKB_DONE.\n",
1563 skb, offset, offloaded);
1564 /*
1565 * pdus should always fit in the skb and we should get
1566 * segment done notifcation.
1567 */
1568 iscsi_conn_printk(KERN_ERR, conn, "Invalid pdu or skb.");
1569 return -EFAULT;
1570 case ISCSI_TCP_SEGMENT_DONE:
1571 log_debug(1 << CXGBI_DBG_PDU_RX,
1572 "skb 0x%p, off %u, %d, TCP_SEG_DONE, rc %d.\n",
1573 skb, offset, offloaded, bytes_read);
1574 return bytes_read;
1575 default:
1576 pr_info("skb 0x%p, off %u, %d, invalid status %d.\n",
1577 skb, offset, offloaded, status);
1578 return -EINVAL;
1579 }
1580 }
1581
1582 static int
skb_read_pdu_bhs(struct cxgbi_sock * csk,struct iscsi_conn * conn,struct sk_buff * skb)1583 skb_read_pdu_bhs(struct cxgbi_sock *csk, struct iscsi_conn *conn,
1584 struct sk_buff *skb)
1585 {
1586 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1587 int err;
1588
1589 log_debug(1 << CXGBI_DBG_PDU_RX,
1590 "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
1591 conn, skb, skb->len, cxgbi_skcb_flags(skb));
1592
1593 if (!iscsi_tcp_recv_segment_is_hdr(tcp_conn)) {
1594 pr_info("conn 0x%p, skb 0x%p, not hdr.\n", conn, skb);
1595 iscsi_conn_failure(conn, ISCSI_ERR_PROTO);
1596 return -EIO;
1597 }
1598
1599 if (conn->hdrdgst_en &&
1600 cxgbi_skcb_test_flag(skb, SKCBF_RX_HCRC_ERR)) {
1601 pr_info("conn 0x%p, skb 0x%p, hcrc.\n", conn, skb);
1602 iscsi_conn_failure(conn, ISCSI_ERR_HDR_DGST);
1603 return -EIO;
1604 }
1605
1606 if (cxgbi_skcb_test_flag(skb, SKCBF_RX_ISCSI_COMPL) &&
1607 cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA_DDPD)) {
1608 /* If completion flag is set and data is directly
1609 * placed in to the host memory then update
1610 * task->exp_datasn to the datasn in completion
1611 * iSCSI hdr as T6 adapter generates completion only
1612 * for the last pdu of a sequence.
1613 */
1614 itt_t itt = ((struct iscsi_data *)skb->data)->itt;
1615 struct iscsi_task *task = iscsi_itt_to_ctask(conn, itt);
1616 u32 data_sn = be32_to_cpu(((struct iscsi_data *)
1617 skb->data)->datasn);
1618 if (task && task->sc) {
1619 struct iscsi_tcp_task *tcp_task = task->dd_data;
1620
1621 tcp_task->exp_datasn = data_sn;
1622 }
1623 }
1624
1625 err = read_pdu_skb(conn, skb, 0, 0);
1626 if (likely(err >= 0)) {
1627 struct iscsi_hdr *hdr = (struct iscsi_hdr *)skb->data;
1628 u8 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
1629
1630 if (unlikely(opcode == ISCSI_OP_LOGOUT_RSP))
1631 cxgbi_sock_set_flag(csk, CTPF_LOGOUT_RSP_RCVD);
1632 }
1633
1634 return err;
1635 }
1636
skb_read_pdu_data(struct iscsi_conn * conn,struct sk_buff * lskb,struct sk_buff * skb,unsigned int offset)1637 static int skb_read_pdu_data(struct iscsi_conn *conn, struct sk_buff *lskb,
1638 struct sk_buff *skb, unsigned int offset)
1639 {
1640 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1641 bool offloaded = 0;
1642 int opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
1643
1644 log_debug(1 << CXGBI_DBG_PDU_RX,
1645 "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
1646 conn, skb, skb->len, cxgbi_skcb_flags(skb));
1647
1648 if (conn->datadgst_en &&
1649 cxgbi_skcb_test_flag(lskb, SKCBF_RX_DCRC_ERR)) {
1650 pr_info("conn 0x%p, skb 0x%p, dcrc 0x%lx.\n",
1651 conn, lskb, cxgbi_skcb_flags(lskb));
1652 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
1653 return -EIO;
1654 }
1655
1656 if (iscsi_tcp_recv_segment_is_hdr(tcp_conn))
1657 return 0;
1658
1659 /* coalesced, add header digest length */
1660 if (lskb == skb && conn->hdrdgst_en)
1661 offset += ISCSI_DIGEST_SIZE;
1662
1663 if (cxgbi_skcb_test_flag(lskb, SKCBF_RX_DATA_DDPD))
1664 offloaded = 1;
1665
1666 if (opcode == ISCSI_OP_SCSI_DATA_IN)
1667 log_debug(1 << CXGBI_DBG_PDU_RX,
1668 "skb 0x%p, op 0x%x, itt 0x%x, %u %s ddp'ed.\n",
1669 skb, opcode, ntohl(tcp_conn->in.hdr->itt),
1670 tcp_conn->in.datalen, offloaded ? "is" : "not");
1671
1672 return read_pdu_skb(conn, skb, offset, offloaded);
1673 }
1674
csk_return_rx_credits(struct cxgbi_sock * csk,int copied)1675 static void csk_return_rx_credits(struct cxgbi_sock *csk, int copied)
1676 {
1677 struct cxgbi_device *cdev = csk->cdev;
1678 int must_send;
1679 u32 credits;
1680
1681 log_debug(1 << CXGBI_DBG_PDU_RX,
1682 "csk 0x%p,%u,0x%lx,%u, seq %u, wup %u, thre %u, %u.\n",
1683 csk, csk->state, csk->flags, csk->tid, csk->copied_seq,
1684 csk->rcv_wup, cdev->rx_credit_thres,
1685 csk->rcv_win);
1686
1687 if (!cdev->rx_credit_thres)
1688 return;
1689
1690 if (csk->state != CTP_ESTABLISHED)
1691 return;
1692
1693 credits = csk->copied_seq - csk->rcv_wup;
1694 if (unlikely(!credits))
1695 return;
1696 must_send = credits + 16384 >= csk->rcv_win;
1697 if (must_send || credits >= cdev->rx_credit_thres)
1698 csk->rcv_wup += cdev->csk_send_rx_credits(csk, credits);
1699 }
1700
cxgbi_conn_pdu_ready(struct cxgbi_sock * csk)1701 void cxgbi_conn_pdu_ready(struct cxgbi_sock *csk)
1702 {
1703 struct cxgbi_device *cdev = csk->cdev;
1704 struct iscsi_conn *conn = csk->user_data;
1705 struct sk_buff *skb;
1706 unsigned int read = 0;
1707 int err = 0;
1708
1709 log_debug(1 << CXGBI_DBG_PDU_RX,
1710 "csk 0x%p, conn 0x%p.\n", csk, conn);
1711
1712 if (unlikely(!conn || conn->suspend_rx)) {
1713 log_debug(1 << CXGBI_DBG_PDU_RX,
1714 "csk 0x%p, conn 0x%p, id %d, suspend_rx %lu!\n",
1715 csk, conn, conn ? conn->id : 0xFF,
1716 conn ? conn->suspend_rx : 0xFF);
1717 return;
1718 }
1719
1720 while (!err) {
1721 skb = skb_peek(&csk->receive_queue);
1722 if (!skb ||
1723 !(cxgbi_skcb_test_flag(skb, SKCBF_RX_STATUS))) {
1724 if (skb)
1725 log_debug(1 << CXGBI_DBG_PDU_RX,
1726 "skb 0x%p, NOT ready 0x%lx.\n",
1727 skb, cxgbi_skcb_flags(skb));
1728 break;
1729 }
1730 __skb_unlink(skb, &csk->receive_queue);
1731
1732 read += cxgbi_skcb_rx_pdulen(skb);
1733 log_debug(1 << CXGBI_DBG_PDU_RX,
1734 "csk 0x%p, skb 0x%p,%u,f 0x%lx, pdu len %u.\n",
1735 csk, skb, skb->len, cxgbi_skcb_flags(skb),
1736 cxgbi_skcb_rx_pdulen(skb));
1737
1738 if (cxgbi_skcb_test_flag(skb, SKCBF_RX_COALESCED)) {
1739 err = skb_read_pdu_bhs(csk, conn, skb);
1740 if (err < 0) {
1741 pr_err("coalesced bhs, csk 0x%p, skb 0x%p,%u, "
1742 "f 0x%lx, plen %u.\n",
1743 csk, skb, skb->len,
1744 cxgbi_skcb_flags(skb),
1745 cxgbi_skcb_rx_pdulen(skb));
1746 goto skb_done;
1747 }
1748 err = skb_read_pdu_data(conn, skb, skb,
1749 err + cdev->skb_rx_extra);
1750 if (err < 0)
1751 pr_err("coalesced data, csk 0x%p, skb 0x%p,%u, "
1752 "f 0x%lx, plen %u.\n",
1753 csk, skb, skb->len,
1754 cxgbi_skcb_flags(skb),
1755 cxgbi_skcb_rx_pdulen(skb));
1756 } else {
1757 err = skb_read_pdu_bhs(csk, conn, skb);
1758 if (err < 0) {
1759 pr_err("bhs, csk 0x%p, skb 0x%p,%u, "
1760 "f 0x%lx, plen %u.\n",
1761 csk, skb, skb->len,
1762 cxgbi_skcb_flags(skb),
1763 cxgbi_skcb_rx_pdulen(skb));
1764 goto skb_done;
1765 }
1766
1767 if (cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA)) {
1768 struct sk_buff *dskb;
1769
1770 dskb = skb_peek(&csk->receive_queue);
1771 if (!dskb) {
1772 pr_err("csk 0x%p, skb 0x%p,%u, f 0x%lx,"
1773 " plen %u, NO data.\n",
1774 csk, skb, skb->len,
1775 cxgbi_skcb_flags(skb),
1776 cxgbi_skcb_rx_pdulen(skb));
1777 err = -EIO;
1778 goto skb_done;
1779 }
1780 __skb_unlink(dskb, &csk->receive_queue);
1781
1782 err = skb_read_pdu_data(conn, skb, dskb, 0);
1783 if (err < 0)
1784 pr_err("data, csk 0x%p, skb 0x%p,%u, "
1785 "f 0x%lx, plen %u, dskb 0x%p,"
1786 "%u.\n",
1787 csk, skb, skb->len,
1788 cxgbi_skcb_flags(skb),
1789 cxgbi_skcb_rx_pdulen(skb),
1790 dskb, dskb->len);
1791 __kfree_skb(dskb);
1792 } else
1793 err = skb_read_pdu_data(conn, skb, skb, 0);
1794 }
1795 skb_done:
1796 __kfree_skb(skb);
1797
1798 if (err < 0)
1799 break;
1800 }
1801
1802 log_debug(1 << CXGBI_DBG_PDU_RX, "csk 0x%p, read %u.\n", csk, read);
1803 if (read) {
1804 csk->copied_seq += read;
1805 csk_return_rx_credits(csk, read);
1806 conn->rxdata_octets += read;
1807 }
1808
1809 if (err < 0) {
1810 pr_info("csk 0x%p, 0x%p, rx failed %d, read %u.\n",
1811 csk, conn, err, read);
1812 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1813 }
1814 }
1815 EXPORT_SYMBOL_GPL(cxgbi_conn_pdu_ready);
1816
sgl_seek_offset(struct scatterlist * sgl,unsigned int sgcnt,unsigned int offset,unsigned int * off,struct scatterlist ** sgp)1817 static int sgl_seek_offset(struct scatterlist *sgl, unsigned int sgcnt,
1818 unsigned int offset, unsigned int *off,
1819 struct scatterlist **sgp)
1820 {
1821 int i;
1822 struct scatterlist *sg;
1823
1824 for_each_sg(sgl, sg, sgcnt, i) {
1825 if (offset < sg->length) {
1826 *off = offset;
1827 *sgp = sg;
1828 return 0;
1829 }
1830 offset -= sg->length;
1831 }
1832 return -EFAULT;
1833 }
1834
sgl_read_to_frags(struct scatterlist * sg,unsigned int sgoffset,unsigned int dlen,struct page_frag * frags,int frag_max)1835 static int sgl_read_to_frags(struct scatterlist *sg, unsigned int sgoffset,
1836 unsigned int dlen, struct page_frag *frags,
1837 int frag_max)
1838 {
1839 unsigned int datalen = dlen;
1840 unsigned int sglen = sg->length - sgoffset;
1841 struct page *page = sg_page(sg);
1842 int i;
1843
1844 i = 0;
1845 do {
1846 unsigned int copy;
1847
1848 if (!sglen) {
1849 sg = sg_next(sg);
1850 if (!sg) {
1851 pr_warn("sg %d NULL, len %u/%u.\n",
1852 i, datalen, dlen);
1853 return -EINVAL;
1854 }
1855 sgoffset = 0;
1856 sglen = sg->length;
1857 page = sg_page(sg);
1858
1859 }
1860 copy = min(datalen, sglen);
1861 if (i && page == frags[i - 1].page &&
1862 sgoffset + sg->offset ==
1863 frags[i - 1].offset + frags[i - 1].size) {
1864 frags[i - 1].size += copy;
1865 } else {
1866 if (i >= frag_max) {
1867 pr_warn("too many pages %u, dlen %u.\n",
1868 frag_max, dlen);
1869 return -EINVAL;
1870 }
1871
1872 frags[i].page = page;
1873 frags[i].offset = sg->offset + sgoffset;
1874 frags[i].size = copy;
1875 i++;
1876 }
1877 datalen -= copy;
1878 sgoffset += copy;
1879 sglen -= copy;
1880 } while (datalen);
1881
1882 return i;
1883 }
1884
cxgbi_conn_alloc_pdu(struct iscsi_task * task,u8 opcode)1885 int cxgbi_conn_alloc_pdu(struct iscsi_task *task, u8 opcode)
1886 {
1887 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
1888 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1889 struct cxgbi_device *cdev = cconn->chba->cdev;
1890 struct iscsi_conn *conn = task->conn;
1891 struct iscsi_tcp_task *tcp_task = task->dd_data;
1892 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1893 struct scsi_cmnd *sc = task->sc;
1894 struct cxgbi_sock *csk = cconn->cep->csk;
1895 struct net_device *ndev = cdev->ports[csk->port_id];
1896 int headroom = SKB_TX_ISCSI_PDU_HEADER_MAX;
1897
1898 tcp_task->dd_data = tdata;
1899 task->hdr = NULL;
1900
1901 if (SKB_MAX_HEAD(cdev->skb_tx_rsvd) > (512 * MAX_SKB_FRAGS) &&
1902 (opcode == ISCSI_OP_SCSI_DATA_OUT ||
1903 (opcode == ISCSI_OP_SCSI_CMD &&
1904 sc->sc_data_direction == DMA_TO_DEVICE)))
1905 /* data could goes into skb head */
1906 headroom += min_t(unsigned int,
1907 SKB_MAX_HEAD(cdev->skb_tx_rsvd),
1908 conn->max_xmit_dlength);
1909
1910 tdata->skb = alloc_skb(cdev->skb_tx_rsvd + headroom, GFP_ATOMIC);
1911 if (!tdata->skb) {
1912 ndev->stats.tx_dropped++;
1913 return -ENOMEM;
1914 }
1915
1916 skb_reserve(tdata->skb, cdev->skb_tx_rsvd);
1917
1918 if (task->sc) {
1919 task->hdr = (struct iscsi_hdr *)tdata->skb->data;
1920 } else {
1921 task->hdr = kzalloc(SKB_TX_ISCSI_PDU_HEADER_MAX, GFP_ATOMIC);
1922 if (!task->hdr) {
1923 __kfree_skb(tdata->skb);
1924 tdata->skb = NULL;
1925 ndev->stats.tx_dropped++;
1926 return -ENOMEM;
1927 }
1928 }
1929 task->hdr_max = SKB_TX_ISCSI_PDU_HEADER_MAX; /* BHS + AHS */
1930
1931 /* data_out uses scsi_cmd's itt */
1932 if (opcode != ISCSI_OP_SCSI_DATA_OUT)
1933 task_reserve_itt(task, &task->hdr->itt);
1934
1935 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1936 "task 0x%p, op 0x%x, skb 0x%p,%u+%u/%u, itt 0x%x.\n",
1937 task, opcode, tdata->skb, cdev->skb_tx_rsvd, headroom,
1938 conn->max_xmit_dlength, ntohl(task->hdr->itt));
1939
1940 return 0;
1941 }
1942 EXPORT_SYMBOL_GPL(cxgbi_conn_alloc_pdu);
1943
tx_skb_setmode(struct sk_buff * skb,int hcrc,int dcrc)1944 static inline void tx_skb_setmode(struct sk_buff *skb, int hcrc, int dcrc)
1945 {
1946 if (hcrc || dcrc) {
1947 u8 submode = 0;
1948
1949 if (hcrc)
1950 submode |= 1;
1951 if (dcrc)
1952 submode |= 2;
1953 cxgbi_skcb_ulp_mode(skb) = (ULP2_MODE_ISCSI << 4) | submode;
1954 } else
1955 cxgbi_skcb_ulp_mode(skb) = 0;
1956 }
1957
cxgbi_conn_init_pdu(struct iscsi_task * task,unsigned int offset,unsigned int count)1958 int cxgbi_conn_init_pdu(struct iscsi_task *task, unsigned int offset,
1959 unsigned int count)
1960 {
1961 struct iscsi_conn *conn = task->conn;
1962 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1963 struct sk_buff *skb = tdata->skb;
1964 unsigned int datalen = count;
1965 int i, padlen = iscsi_padding(count);
1966 struct page *pg;
1967
1968 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1969 "task 0x%p,0x%p, skb 0x%p, 0x%x,0x%x,0x%x, %u+%u.\n",
1970 task, task->sc, skb, (*skb->data) & ISCSI_OPCODE_MASK,
1971 ntohl(task->cmdsn), ntohl(task->hdr->itt), offset, count);
1972
1973 skb_put(skb, task->hdr_len);
1974 tx_skb_setmode(skb, conn->hdrdgst_en, datalen ? conn->datadgst_en : 0);
1975 if (!count)
1976 return 0;
1977
1978 if (task->sc) {
1979 struct scsi_data_buffer *sdb = &task->sc->sdb;
1980 struct scatterlist *sg = NULL;
1981 int err;
1982
1983 tdata->offset = offset;
1984 tdata->count = count;
1985 err = sgl_seek_offset(
1986 sdb->table.sgl, sdb->table.nents,
1987 tdata->offset, &tdata->sgoffset, &sg);
1988 if (err < 0) {
1989 pr_warn("tpdu, sgl %u, bad offset %u/%u.\n",
1990 sdb->table.nents, tdata->offset, sdb->length);
1991 return err;
1992 }
1993 err = sgl_read_to_frags(sg, tdata->sgoffset, tdata->count,
1994 tdata->frags, MAX_PDU_FRAGS);
1995 if (err < 0) {
1996 pr_warn("tpdu, sgl %u, bad offset %u + %u.\n",
1997 sdb->table.nents, tdata->offset, tdata->count);
1998 return err;
1999 }
2000 tdata->nr_frags = err;
2001
2002 if (tdata->nr_frags > MAX_SKB_FRAGS ||
2003 (padlen && tdata->nr_frags == MAX_SKB_FRAGS)) {
2004 char *dst = skb->data + task->hdr_len;
2005 struct page_frag *frag = tdata->frags;
2006
2007 /* data fits in the skb's headroom */
2008 for (i = 0; i < tdata->nr_frags; i++, frag++) {
2009 char *src = kmap_atomic(frag->page);
2010
2011 memcpy(dst, src+frag->offset, frag->size);
2012 dst += frag->size;
2013 kunmap_atomic(src);
2014 }
2015 if (padlen) {
2016 memset(dst, 0, padlen);
2017 padlen = 0;
2018 }
2019 skb_put(skb, count + padlen);
2020 } else {
2021 /* data fit into frag_list */
2022 for (i = 0; i < tdata->nr_frags; i++) {
2023 __skb_fill_page_desc(skb, i,
2024 tdata->frags[i].page,
2025 tdata->frags[i].offset,
2026 tdata->frags[i].size);
2027 skb_frag_ref(skb, i);
2028 }
2029 skb_shinfo(skb)->nr_frags = tdata->nr_frags;
2030 skb->len += count;
2031 skb->data_len += count;
2032 skb->truesize += count;
2033 }
2034
2035 } else {
2036 pg = virt_to_page(task->data);
2037
2038 get_page(pg);
2039 skb_fill_page_desc(skb, 0, pg, offset_in_page(task->data),
2040 count);
2041 skb->len += count;
2042 skb->data_len += count;
2043 skb->truesize += count;
2044 }
2045
2046 if (padlen) {
2047 i = skb_shinfo(skb)->nr_frags;
2048 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
2049 virt_to_page(padding), offset_in_page(padding),
2050 padlen);
2051
2052 skb->data_len += padlen;
2053 skb->truesize += padlen;
2054 skb->len += padlen;
2055 }
2056
2057 return 0;
2058 }
2059 EXPORT_SYMBOL_GPL(cxgbi_conn_init_pdu);
2060
cxgbi_conn_xmit_pdu(struct iscsi_task * task)2061 int cxgbi_conn_xmit_pdu(struct iscsi_task *task)
2062 {
2063 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
2064 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2065 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
2066 struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
2067 struct sk_buff *skb = tdata->skb;
2068 struct cxgbi_sock *csk = NULL;
2069 unsigned int datalen;
2070 int err;
2071
2072 if (!skb) {
2073 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2074 "task 0x%p\n", task);
2075 return 0;
2076 }
2077
2078 if (cconn && cconn->cep)
2079 csk = cconn->cep->csk;
2080 if (!csk) {
2081 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2082 "task 0x%p, csk gone.\n", task);
2083 return -EPIPE;
2084 }
2085
2086 tdata->skb = NULL;
2087 datalen = skb->data_len;
2088
2089 /* write ppod first if using ofldq to write ppod */
2090 if (ttinfo->flags & CXGBI_PPOD_INFO_FLAG_VALID) {
2091 struct cxgbi_ppm *ppm = csk->cdev->cdev2ppm(csk->cdev);
2092
2093 ttinfo->flags &= ~CXGBI_PPOD_INFO_FLAG_VALID;
2094 if (csk->cdev->csk_ddp_set_map(ppm, csk, ttinfo) < 0)
2095 pr_err("task 0x%p, ppod writing using ofldq failed.\n",
2096 task);
2097 /* continue. Let fl get the data */
2098 }
2099
2100 if (!task->sc)
2101 memcpy(skb->data, task->hdr, SKB_TX_ISCSI_PDU_HEADER_MAX);
2102
2103 err = cxgbi_sock_send_pdus(cconn->cep->csk, skb);
2104 if (err > 0) {
2105 int pdulen = err;
2106
2107 log_debug(1 << CXGBI_DBG_PDU_TX,
2108 "task 0x%p,0x%p, skb 0x%p, len %u/%u, rv %d.\n",
2109 task, task->sc, skb, skb->len, skb->data_len, err);
2110
2111 if (task->conn->hdrdgst_en)
2112 pdulen += ISCSI_DIGEST_SIZE;
2113
2114 if (datalen && task->conn->datadgst_en)
2115 pdulen += ISCSI_DIGEST_SIZE;
2116
2117 task->conn->txdata_octets += pdulen;
2118 return 0;
2119 }
2120
2121 if (err == -EAGAIN || err == -ENOBUFS) {
2122 log_debug(1 << CXGBI_DBG_PDU_TX,
2123 "task 0x%p, skb 0x%p, len %u/%u, %d EAGAIN.\n",
2124 task, skb, skb->len, skb->data_len, err);
2125 /* reset skb to send when we are called again */
2126 tdata->skb = skb;
2127 return err;
2128 }
2129
2130 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2131 "itt 0x%x, skb 0x%p, len %u/%u, xmit err %d.\n",
2132 task->itt, skb, skb->len, skb->data_len, err);
2133
2134 __kfree_skb(skb);
2135
2136 iscsi_conn_printk(KERN_ERR, task->conn, "xmit err %d.\n", err);
2137 iscsi_conn_failure(task->conn, ISCSI_ERR_XMIT_FAILED);
2138 return err;
2139 }
2140 EXPORT_SYMBOL_GPL(cxgbi_conn_xmit_pdu);
2141
cxgbi_cleanup_task(struct iscsi_task * task)2142 void cxgbi_cleanup_task(struct iscsi_task *task)
2143 {
2144 struct iscsi_tcp_task *tcp_task = task->dd_data;
2145 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
2146
2147 if (!tcp_task || !tdata || (tcp_task->dd_data != tdata)) {
2148 pr_info("task 0x%p,0x%p, tcp_task 0x%p, tdata 0x%p/0x%p.\n",
2149 task, task->sc, tcp_task,
2150 tcp_task ? tcp_task->dd_data : NULL, tdata);
2151 return;
2152 }
2153
2154 log_debug(1 << CXGBI_DBG_ISCSI,
2155 "task 0x%p, skb 0x%p, itt 0x%x.\n",
2156 task, tdata->skb, task->hdr_itt);
2157
2158 tcp_task->dd_data = NULL;
2159
2160 if (!task->sc)
2161 kfree(task->hdr);
2162 task->hdr = NULL;
2163
2164 /* never reached the xmit task callout */
2165 if (tdata->skb) {
2166 __kfree_skb(tdata->skb);
2167 tdata->skb = NULL;
2168 }
2169
2170 task_release_itt(task, task->hdr_itt);
2171 memset(tdata, 0, sizeof(*tdata));
2172
2173 iscsi_tcp_cleanup_task(task);
2174 }
2175 EXPORT_SYMBOL_GPL(cxgbi_cleanup_task);
2176
cxgbi_get_conn_stats(struct iscsi_cls_conn * cls_conn,struct iscsi_stats * stats)2177 void cxgbi_get_conn_stats(struct iscsi_cls_conn *cls_conn,
2178 struct iscsi_stats *stats)
2179 {
2180 struct iscsi_conn *conn = cls_conn->dd_data;
2181
2182 stats->txdata_octets = conn->txdata_octets;
2183 stats->rxdata_octets = conn->rxdata_octets;
2184 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2185 stats->dataout_pdus = conn->dataout_pdus_cnt;
2186 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2187 stats->datain_pdus = conn->datain_pdus_cnt;
2188 stats->r2t_pdus = conn->r2t_pdus_cnt;
2189 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2190 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2191 stats->digest_err = 0;
2192 stats->timeout_err = 0;
2193 stats->custom_length = 1;
2194 strcpy(stats->custom[0].desc, "eh_abort_cnt");
2195 stats->custom[0].value = conn->eh_abort_cnt;
2196 }
2197 EXPORT_SYMBOL_GPL(cxgbi_get_conn_stats);
2198
cxgbi_conn_max_xmit_dlength(struct iscsi_conn * conn)2199 static int cxgbi_conn_max_xmit_dlength(struct iscsi_conn *conn)
2200 {
2201 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2202 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2203 struct cxgbi_device *cdev = cconn->chba->cdev;
2204 unsigned int headroom = SKB_MAX_HEAD(cdev->skb_tx_rsvd);
2205 unsigned int max_def = 512 * MAX_SKB_FRAGS;
2206 unsigned int max = max(max_def, headroom);
2207
2208 max = min(cconn->chba->cdev->tx_max_size, max);
2209 if (conn->max_xmit_dlength)
2210 conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
2211 else
2212 conn->max_xmit_dlength = max;
2213 cxgbi_align_pdu_size(conn->max_xmit_dlength);
2214
2215 return 0;
2216 }
2217
cxgbi_conn_max_recv_dlength(struct iscsi_conn * conn)2218 static int cxgbi_conn_max_recv_dlength(struct iscsi_conn *conn)
2219 {
2220 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2221 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2222 unsigned int max = cconn->chba->cdev->rx_max_size;
2223
2224 cxgbi_align_pdu_size(max);
2225
2226 if (conn->max_recv_dlength) {
2227 if (conn->max_recv_dlength > max) {
2228 pr_err("MaxRecvDataSegmentLength %u > %u.\n",
2229 conn->max_recv_dlength, max);
2230 return -EINVAL;
2231 }
2232 conn->max_recv_dlength = min(conn->max_recv_dlength, max);
2233 cxgbi_align_pdu_size(conn->max_recv_dlength);
2234 } else
2235 conn->max_recv_dlength = max;
2236
2237 return 0;
2238 }
2239
cxgbi_set_conn_param(struct iscsi_cls_conn * cls_conn,enum iscsi_param param,char * buf,int buflen)2240 int cxgbi_set_conn_param(struct iscsi_cls_conn *cls_conn,
2241 enum iscsi_param param, char *buf, int buflen)
2242 {
2243 struct iscsi_conn *conn = cls_conn->dd_data;
2244 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2245 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2246 struct cxgbi_sock *csk = cconn->cep->csk;
2247 int err;
2248
2249 log_debug(1 << CXGBI_DBG_ISCSI,
2250 "cls_conn 0x%p, param %d, buf(%d) %s.\n",
2251 cls_conn, param, buflen, buf);
2252
2253 switch (param) {
2254 case ISCSI_PARAM_HDRDGST_EN:
2255 err = iscsi_set_param(cls_conn, param, buf, buflen);
2256 if (!err && conn->hdrdgst_en)
2257 err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
2258 conn->hdrdgst_en,
2259 conn->datadgst_en);
2260 break;
2261 case ISCSI_PARAM_DATADGST_EN:
2262 err = iscsi_set_param(cls_conn, param, buf, buflen);
2263 if (!err && conn->datadgst_en)
2264 err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
2265 conn->hdrdgst_en,
2266 conn->datadgst_en);
2267 break;
2268 case ISCSI_PARAM_MAX_R2T:
2269 return iscsi_tcp_set_max_r2t(conn, buf);
2270 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2271 err = iscsi_set_param(cls_conn, param, buf, buflen);
2272 if (!err)
2273 err = cxgbi_conn_max_recv_dlength(conn);
2274 break;
2275 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2276 err = iscsi_set_param(cls_conn, param, buf, buflen);
2277 if (!err)
2278 err = cxgbi_conn_max_xmit_dlength(conn);
2279 break;
2280 default:
2281 return iscsi_set_param(cls_conn, param, buf, buflen);
2282 }
2283 return err;
2284 }
2285 EXPORT_SYMBOL_GPL(cxgbi_set_conn_param);
2286
csk_print_port(struct cxgbi_sock * csk,char * buf)2287 static inline int csk_print_port(struct cxgbi_sock *csk, char *buf)
2288 {
2289 int len;
2290
2291 cxgbi_sock_get(csk);
2292 len = sprintf(buf, "%hu\n", ntohs(csk->daddr.sin_port));
2293 cxgbi_sock_put(csk);
2294
2295 return len;
2296 }
2297
csk_print_ip(struct cxgbi_sock * csk,char * buf)2298 static inline int csk_print_ip(struct cxgbi_sock *csk, char *buf)
2299 {
2300 int len;
2301
2302 cxgbi_sock_get(csk);
2303 if (csk->csk_family == AF_INET)
2304 len = sprintf(buf, "%pI4",
2305 &csk->daddr.sin_addr.s_addr);
2306 else
2307 len = sprintf(buf, "%pI6",
2308 &csk->daddr6.sin6_addr);
2309
2310 cxgbi_sock_put(csk);
2311
2312 return len;
2313 }
2314
cxgbi_get_ep_param(struct iscsi_endpoint * ep,enum iscsi_param param,char * buf)2315 int cxgbi_get_ep_param(struct iscsi_endpoint *ep, enum iscsi_param param,
2316 char *buf)
2317 {
2318 struct cxgbi_endpoint *cep = ep->dd_data;
2319 struct cxgbi_sock *csk;
2320
2321 log_debug(1 << CXGBI_DBG_ISCSI,
2322 "cls_conn 0x%p, param %d.\n", ep, param);
2323
2324 switch (param) {
2325 case ISCSI_PARAM_CONN_PORT:
2326 case ISCSI_PARAM_CONN_ADDRESS:
2327 if (!cep)
2328 return -ENOTCONN;
2329
2330 csk = cep->csk;
2331 if (!csk)
2332 return -ENOTCONN;
2333
2334 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
2335 &csk->daddr, param, buf);
2336 default:
2337 break;
2338 }
2339 return -ENOSYS;
2340 }
2341 EXPORT_SYMBOL_GPL(cxgbi_get_ep_param);
2342
2343 struct iscsi_cls_conn *
cxgbi_create_conn(struct iscsi_cls_session * cls_session,u32 cid)2344 cxgbi_create_conn(struct iscsi_cls_session *cls_session, u32 cid)
2345 {
2346 struct iscsi_cls_conn *cls_conn;
2347 struct iscsi_conn *conn;
2348 struct iscsi_tcp_conn *tcp_conn;
2349 struct cxgbi_conn *cconn;
2350
2351 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
2352 if (!cls_conn)
2353 return NULL;
2354
2355 conn = cls_conn->dd_data;
2356 tcp_conn = conn->dd_data;
2357 cconn = tcp_conn->dd_data;
2358 cconn->iconn = conn;
2359
2360 log_debug(1 << CXGBI_DBG_ISCSI,
2361 "cid %u(0x%x), cls 0x%p,0x%p, conn 0x%p,0x%p,0x%p.\n",
2362 cid, cid, cls_session, cls_conn, conn, tcp_conn, cconn);
2363
2364 return cls_conn;
2365 }
2366 EXPORT_SYMBOL_GPL(cxgbi_create_conn);
2367
cxgbi_bind_conn(struct iscsi_cls_session * cls_session,struct iscsi_cls_conn * cls_conn,u64 transport_eph,int is_leading)2368 int cxgbi_bind_conn(struct iscsi_cls_session *cls_session,
2369 struct iscsi_cls_conn *cls_conn,
2370 u64 transport_eph, int is_leading)
2371 {
2372 struct iscsi_conn *conn = cls_conn->dd_data;
2373 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2374 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2375 struct cxgbi_ppm *ppm;
2376 struct iscsi_endpoint *ep;
2377 struct cxgbi_endpoint *cep;
2378 struct cxgbi_sock *csk;
2379 int err;
2380
2381 ep = iscsi_lookup_endpoint(transport_eph);
2382 if (!ep)
2383 return -EINVAL;
2384
2385 /* setup ddp pagesize */
2386 cep = ep->dd_data;
2387 csk = cep->csk;
2388
2389 ppm = csk->cdev->cdev2ppm(csk->cdev);
2390 err = csk->cdev->csk_ddp_setup_pgidx(csk, csk->tid,
2391 ppm->tformat.pgsz_idx_dflt);
2392 if (err < 0)
2393 return err;
2394
2395 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
2396 if (err)
2397 return -EINVAL;
2398
2399 /* calculate the tag idx bits needed for this conn based on cmds_max */
2400 cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
2401
2402 write_lock_bh(&csk->callback_lock);
2403 csk->user_data = conn;
2404 cconn->chba = cep->chba;
2405 cconn->cep = cep;
2406 cep->cconn = cconn;
2407 write_unlock_bh(&csk->callback_lock);
2408
2409 cxgbi_conn_max_xmit_dlength(conn);
2410 cxgbi_conn_max_recv_dlength(conn);
2411
2412 log_debug(1 << CXGBI_DBG_ISCSI,
2413 "cls 0x%p,0x%p, ep 0x%p, cconn 0x%p, csk 0x%p.\n",
2414 cls_session, cls_conn, ep, cconn, csk);
2415 /* init recv engine */
2416 iscsi_tcp_hdr_recv_prep(tcp_conn);
2417
2418 return 0;
2419 }
2420 EXPORT_SYMBOL_GPL(cxgbi_bind_conn);
2421
cxgbi_create_session(struct iscsi_endpoint * ep,u16 cmds_max,u16 qdepth,u32 initial_cmdsn)2422 struct iscsi_cls_session *cxgbi_create_session(struct iscsi_endpoint *ep,
2423 u16 cmds_max, u16 qdepth,
2424 u32 initial_cmdsn)
2425 {
2426 struct cxgbi_endpoint *cep;
2427 struct cxgbi_hba *chba;
2428 struct Scsi_Host *shost;
2429 struct iscsi_cls_session *cls_session;
2430 struct iscsi_session *session;
2431
2432 if (!ep) {
2433 pr_err("missing endpoint.\n");
2434 return NULL;
2435 }
2436
2437 cep = ep->dd_data;
2438 chba = cep->chba;
2439 shost = chba->shost;
2440
2441 BUG_ON(chba != iscsi_host_priv(shost));
2442
2443 cls_session = iscsi_session_setup(chba->cdev->itp, shost,
2444 cmds_max, 0,
2445 sizeof(struct iscsi_tcp_task) +
2446 sizeof(struct cxgbi_task_data),
2447 initial_cmdsn, ISCSI_MAX_TARGET);
2448 if (!cls_session)
2449 return NULL;
2450
2451 session = cls_session->dd_data;
2452 if (iscsi_tcp_r2tpool_alloc(session))
2453 goto remove_session;
2454
2455 log_debug(1 << CXGBI_DBG_ISCSI,
2456 "ep 0x%p, cls sess 0x%p.\n", ep, cls_session);
2457 return cls_session;
2458
2459 remove_session:
2460 iscsi_session_teardown(cls_session);
2461 return NULL;
2462 }
2463 EXPORT_SYMBOL_GPL(cxgbi_create_session);
2464
cxgbi_destroy_session(struct iscsi_cls_session * cls_session)2465 void cxgbi_destroy_session(struct iscsi_cls_session *cls_session)
2466 {
2467 log_debug(1 << CXGBI_DBG_ISCSI,
2468 "cls sess 0x%p.\n", cls_session);
2469
2470 iscsi_tcp_r2tpool_free(cls_session->dd_data);
2471 iscsi_session_teardown(cls_session);
2472 }
2473 EXPORT_SYMBOL_GPL(cxgbi_destroy_session);
2474
cxgbi_set_host_param(struct Scsi_Host * shost,enum iscsi_host_param param,char * buf,int buflen)2475 int cxgbi_set_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2476 char *buf, int buflen)
2477 {
2478 struct cxgbi_hba *chba = iscsi_host_priv(shost);
2479
2480 if (!chba->ndev) {
2481 shost_printk(KERN_ERR, shost, "Could not get host param. "
2482 "netdev for host not set.\n");
2483 return -ENODEV;
2484 }
2485
2486 log_debug(1 << CXGBI_DBG_ISCSI,
2487 "shost 0x%p, hba 0x%p,%s, param %d, buf(%d) %s.\n",
2488 shost, chba, chba->ndev->name, param, buflen, buf);
2489
2490 switch (param) {
2491 case ISCSI_HOST_PARAM_IPADDRESS:
2492 {
2493 __be32 addr = in_aton(buf);
2494 log_debug(1 << CXGBI_DBG_ISCSI,
2495 "hba %s, req. ipv4 %pI4.\n", chba->ndev->name, &addr);
2496 cxgbi_set_iscsi_ipv4(chba, addr);
2497 return 0;
2498 }
2499 case ISCSI_HOST_PARAM_HWADDRESS:
2500 case ISCSI_HOST_PARAM_NETDEV_NAME:
2501 return 0;
2502 default:
2503 return iscsi_host_set_param(shost, param, buf, buflen);
2504 }
2505 }
2506 EXPORT_SYMBOL_GPL(cxgbi_set_host_param);
2507
cxgbi_get_host_param(struct Scsi_Host * shost,enum iscsi_host_param param,char * buf)2508 int cxgbi_get_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2509 char *buf)
2510 {
2511 struct cxgbi_hba *chba = iscsi_host_priv(shost);
2512 int len = 0;
2513
2514 if (!chba->ndev) {
2515 shost_printk(KERN_ERR, shost, "Could not get host param. "
2516 "netdev for host not set.\n");
2517 return -ENODEV;
2518 }
2519
2520 log_debug(1 << CXGBI_DBG_ISCSI,
2521 "shost 0x%p, hba 0x%p,%s, param %d.\n",
2522 shost, chba, chba->ndev->name, param);
2523
2524 switch (param) {
2525 case ISCSI_HOST_PARAM_HWADDRESS:
2526 len = sysfs_format_mac(buf, chba->ndev->dev_addr, 6);
2527 break;
2528 case ISCSI_HOST_PARAM_NETDEV_NAME:
2529 len = sprintf(buf, "%s\n", chba->ndev->name);
2530 break;
2531 case ISCSI_HOST_PARAM_IPADDRESS:
2532 {
2533 struct cxgbi_sock *csk = find_sock_on_port(chba->cdev,
2534 chba->port_id);
2535 if (csk) {
2536 len = sprintf(buf, "%pIS",
2537 (struct sockaddr *)&csk->saddr);
2538 }
2539 log_debug(1 << CXGBI_DBG_ISCSI,
2540 "hba %s, addr %s.\n", chba->ndev->name, buf);
2541 break;
2542 }
2543 default:
2544 return iscsi_host_get_param(shost, param, buf);
2545 }
2546
2547 return len;
2548 }
2549 EXPORT_SYMBOL_GPL(cxgbi_get_host_param);
2550
cxgbi_ep_connect(struct Scsi_Host * shost,struct sockaddr * dst_addr,int non_blocking)2551 struct iscsi_endpoint *cxgbi_ep_connect(struct Scsi_Host *shost,
2552 struct sockaddr *dst_addr,
2553 int non_blocking)
2554 {
2555 struct iscsi_endpoint *ep;
2556 struct cxgbi_endpoint *cep;
2557 struct cxgbi_hba *hba = NULL;
2558 struct cxgbi_sock *csk;
2559 int ifindex = 0;
2560 int err = -EINVAL;
2561
2562 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2563 "shost 0x%p, non_blocking %d, dst_addr 0x%p.\n",
2564 shost, non_blocking, dst_addr);
2565
2566 if (shost) {
2567 hba = iscsi_host_priv(shost);
2568 if (!hba) {
2569 pr_info("shost 0x%p, priv NULL.\n", shost);
2570 goto err_out;
2571 }
2572 }
2573
2574 check_route:
2575 if (dst_addr->sa_family == AF_INET) {
2576 csk = cxgbi_check_route(dst_addr, ifindex);
2577 #if IS_ENABLED(CONFIG_IPV6)
2578 } else if (dst_addr->sa_family == AF_INET6) {
2579 csk = cxgbi_check_route6(dst_addr, ifindex);
2580 #endif
2581 } else {
2582 pr_info("address family 0x%x NOT supported.\n",
2583 dst_addr->sa_family);
2584 err = -EAFNOSUPPORT;
2585 return (struct iscsi_endpoint *)ERR_PTR(err);
2586 }
2587
2588 if (IS_ERR(csk))
2589 return (struct iscsi_endpoint *)csk;
2590 cxgbi_sock_get(csk);
2591
2592 if (!hba)
2593 hba = csk->cdev->hbas[csk->port_id];
2594 else if (hba != csk->cdev->hbas[csk->port_id]) {
2595 if (ifindex != hba->ndev->ifindex) {
2596 cxgbi_sock_put(csk);
2597 cxgbi_sock_closed(csk);
2598 ifindex = hba->ndev->ifindex;
2599 goto check_route;
2600 }
2601
2602 pr_info("Could not connect through requested host %u"
2603 "hba 0x%p != 0x%p (%u).\n",
2604 shost->host_no, hba,
2605 csk->cdev->hbas[csk->port_id], csk->port_id);
2606 err = -ENOSPC;
2607 goto release_conn;
2608 }
2609
2610 err = sock_get_port(csk);
2611 if (err)
2612 goto release_conn;
2613
2614 cxgbi_sock_set_state(csk, CTP_CONNECTING);
2615 err = csk->cdev->csk_init_act_open(csk);
2616 if (err)
2617 goto release_conn;
2618
2619 if (cxgbi_sock_is_closing(csk)) {
2620 err = -ENOSPC;
2621 pr_info("csk 0x%p is closing.\n", csk);
2622 goto release_conn;
2623 }
2624
2625 ep = iscsi_create_endpoint(sizeof(*cep));
2626 if (!ep) {
2627 err = -ENOMEM;
2628 pr_info("iscsi alloc ep, OOM.\n");
2629 goto release_conn;
2630 }
2631
2632 cep = ep->dd_data;
2633 cep->csk = csk;
2634 cep->chba = hba;
2635
2636 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2637 "ep 0x%p, cep 0x%p, csk 0x%p, hba 0x%p,%s.\n",
2638 ep, cep, csk, hba, hba->ndev->name);
2639 return ep;
2640
2641 release_conn:
2642 cxgbi_sock_put(csk);
2643 cxgbi_sock_closed(csk);
2644 err_out:
2645 return ERR_PTR(err);
2646 }
2647 EXPORT_SYMBOL_GPL(cxgbi_ep_connect);
2648
cxgbi_ep_poll(struct iscsi_endpoint * ep,int timeout_ms)2649 int cxgbi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
2650 {
2651 struct cxgbi_endpoint *cep = ep->dd_data;
2652 struct cxgbi_sock *csk = cep->csk;
2653
2654 if (!cxgbi_sock_is_established(csk))
2655 return 0;
2656 return 1;
2657 }
2658 EXPORT_SYMBOL_GPL(cxgbi_ep_poll);
2659
cxgbi_ep_disconnect(struct iscsi_endpoint * ep)2660 void cxgbi_ep_disconnect(struct iscsi_endpoint *ep)
2661 {
2662 struct cxgbi_endpoint *cep = ep->dd_data;
2663 struct cxgbi_conn *cconn = cep->cconn;
2664 struct cxgbi_sock *csk = cep->csk;
2665
2666 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2667 "ep 0x%p, cep 0x%p, cconn 0x%p, csk 0x%p,%u,0x%lx.\n",
2668 ep, cep, cconn, csk, csk->state, csk->flags);
2669
2670 if (cconn && cconn->iconn) {
2671 iscsi_suspend_tx(cconn->iconn);
2672 write_lock_bh(&csk->callback_lock);
2673 cep->csk->user_data = NULL;
2674 cconn->cep = NULL;
2675 write_unlock_bh(&csk->callback_lock);
2676 }
2677 iscsi_destroy_endpoint(ep);
2678
2679 if (likely(csk->state >= CTP_ESTABLISHED))
2680 need_active_close(csk);
2681 else
2682 cxgbi_sock_closed(csk);
2683
2684 cxgbi_sock_put(csk);
2685 }
2686 EXPORT_SYMBOL_GPL(cxgbi_ep_disconnect);
2687
cxgbi_iscsi_init(struct iscsi_transport * itp,struct scsi_transport_template ** stt)2688 int cxgbi_iscsi_init(struct iscsi_transport *itp,
2689 struct scsi_transport_template **stt)
2690 {
2691 *stt = iscsi_register_transport(itp);
2692 if (*stt == NULL) {
2693 pr_err("unable to register %s transport 0x%p.\n",
2694 itp->name, itp);
2695 return -ENODEV;
2696 }
2697 log_debug(1 << CXGBI_DBG_ISCSI,
2698 "%s, registered iscsi transport 0x%p.\n",
2699 itp->name, stt);
2700 return 0;
2701 }
2702 EXPORT_SYMBOL_GPL(cxgbi_iscsi_init);
2703
cxgbi_iscsi_cleanup(struct iscsi_transport * itp,struct scsi_transport_template ** stt)2704 void cxgbi_iscsi_cleanup(struct iscsi_transport *itp,
2705 struct scsi_transport_template **stt)
2706 {
2707 if (*stt) {
2708 log_debug(1 << CXGBI_DBG_ISCSI,
2709 "de-register transport 0x%p, %s, stt 0x%p.\n",
2710 itp, itp->name, *stt);
2711 *stt = NULL;
2712 iscsi_unregister_transport(itp);
2713 }
2714 }
2715 EXPORT_SYMBOL_GPL(cxgbi_iscsi_cleanup);
2716
cxgbi_attr_is_visible(int param_type,int param)2717 umode_t cxgbi_attr_is_visible(int param_type, int param)
2718 {
2719 switch (param_type) {
2720 case ISCSI_HOST_PARAM:
2721 switch (param) {
2722 case ISCSI_HOST_PARAM_NETDEV_NAME:
2723 case ISCSI_HOST_PARAM_HWADDRESS:
2724 case ISCSI_HOST_PARAM_IPADDRESS:
2725 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2726 return S_IRUGO;
2727 default:
2728 return 0;
2729 }
2730 case ISCSI_PARAM:
2731 switch (param) {
2732 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2733 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2734 case ISCSI_PARAM_HDRDGST_EN:
2735 case ISCSI_PARAM_DATADGST_EN:
2736 case ISCSI_PARAM_CONN_ADDRESS:
2737 case ISCSI_PARAM_CONN_PORT:
2738 case ISCSI_PARAM_EXP_STATSN:
2739 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2740 case ISCSI_PARAM_PERSISTENT_PORT:
2741 case ISCSI_PARAM_PING_TMO:
2742 case ISCSI_PARAM_RECV_TMO:
2743 case ISCSI_PARAM_INITIAL_R2T_EN:
2744 case ISCSI_PARAM_MAX_R2T:
2745 case ISCSI_PARAM_IMM_DATA_EN:
2746 case ISCSI_PARAM_FIRST_BURST:
2747 case ISCSI_PARAM_MAX_BURST:
2748 case ISCSI_PARAM_PDU_INORDER_EN:
2749 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2750 case ISCSI_PARAM_ERL:
2751 case ISCSI_PARAM_TARGET_NAME:
2752 case ISCSI_PARAM_TPGT:
2753 case ISCSI_PARAM_USERNAME:
2754 case ISCSI_PARAM_PASSWORD:
2755 case ISCSI_PARAM_USERNAME_IN:
2756 case ISCSI_PARAM_PASSWORD_IN:
2757 case ISCSI_PARAM_FAST_ABORT:
2758 case ISCSI_PARAM_ABORT_TMO:
2759 case ISCSI_PARAM_LU_RESET_TMO:
2760 case ISCSI_PARAM_TGT_RESET_TMO:
2761 case ISCSI_PARAM_IFACE_NAME:
2762 case ISCSI_PARAM_INITIATOR_NAME:
2763 return S_IRUGO;
2764 default:
2765 return 0;
2766 }
2767 }
2768
2769 return 0;
2770 }
2771 EXPORT_SYMBOL_GPL(cxgbi_attr_is_visible);
2772
libcxgbi_init_module(void)2773 static int __init libcxgbi_init_module(void)
2774 {
2775 pr_info("%s", version);
2776
2777 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, cb) <
2778 sizeof(struct cxgbi_skb_cb));
2779 return 0;
2780 }
2781
libcxgbi_exit_module(void)2782 static void __exit libcxgbi_exit_module(void)
2783 {
2784 cxgbi_device_unregister_all(0xFF);
2785 return;
2786 }
2787
2788 module_init(libcxgbi_init_module);
2789 module_exit(libcxgbi_exit_module);
2790