blob: 1a4cfa562a6087206ee2f68eaf3fc63988d1934a [file] [log] [blame]
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001/*
2 * libcxgbi.c: Chelsio common library for T3/T4 iSCSI driver.
3 *
Karen Xie1149a5e2015-04-10 13:57:15 -07004 * Copyright (c) 2010-2015 Chelsio Communications, Inc.
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07005 *
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>
Anish Bhattfc8d0592014-07-17 00:18:17 -070027#include <net/ipv6.h>
28#include <net/ip6_route.h>
29#include <net/addrconf.h>
30
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -070031#include <linux/inetdevice.h> /* ip_dev_find */
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040032#include <linux/module.h>
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -070033#include <net/tcp.h>
34
35static 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"
Karen Xie0ea5bf32015-04-10 13:57:18 -070041#define DRV_MODULE_VERSION "0.9.1-ko"
42#define DRV_MODULE_RELDATE "Apr. 2015"
43
44static char version[] =
45 DRV_MODULE_DESC " " DRV_MODULE_NAME
46 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -070047
48MODULE_AUTHOR("Chelsio Communications, Inc.");
49MODULE_DESCRIPTION(DRV_MODULE_DESC);
50MODULE_VERSION(DRV_MODULE_VERSION);
51MODULE_LICENSE("GPL");
52
53module_param(dbg_level, uint, 0644);
54MODULE_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 */
61static LIST_HEAD(cdev_list);
62static DEFINE_MUTEX(cdev_mutex);
63
Anish Bhatt576b58632014-09-15 17:44:18 -070064static LIST_HEAD(cdev_rcu_list);
65static DEFINE_SPINLOCK(cdev_rcu_lock);
66
Varun Prakash71f7a002016-07-21 22:57:16 +053067static 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
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -070075int 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}
93EXPORT_SYMBOL_GPL(cxgbi_device_portmap_create);
94
95void 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}
116EXPORT_SYMBOL_GPL(cxgbi_device_portmap_cleanup);
117
118static 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);
Varun Prakash9d5c44b2016-07-21 22:57:18 +0530124 cxgbi_ppm_release(cdev->cdev2ppm(cdev));
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700125 if (cdev->pmap.max_connect)
126 cxgbi_free_big_mem(cdev->pmap.port_csk);
127 kfree(cdev);
128}
129
130struct 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
Anish Bhatt576b58632014-09-15 17:44:18 -0700155 spin_lock(&cdev_rcu_lock);
156 list_add_tail_rcu(&cdev->rcu_node, &cdev_rcu_list);
157 spin_unlock(&cdev_rcu_lock);
158
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700159 log_debug(1 << CXGBI_DBG_DEV,
160 "cdev 0x%p, p# %u.\n", cdev, nports);
161 return cdev;
162}
163EXPORT_SYMBOL_GPL(cxgbi_device_register);
164
165void 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 : "");
Anish Bhatt576b58632014-09-15 17:44:18 -0700170
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700171 mutex_lock(&cdev_mutex);
172 list_del(&cdev->list_head);
173 mutex_unlock(&cdev_mutex);
Anish Bhatt576b58632014-09-15 17:44:18 -0700174
175 spin_lock(&cdev_rcu_lock);
176 list_del_rcu(&cdev->rcu_node);
177 spin_unlock(&cdev_rcu_lock);
178 synchronize_rcu();
179
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700180 cxgbi_device_destroy(cdev);
181}
182EXPORT_SYMBOL_GPL(cxgbi_device_unregister);
183
184void 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) {
Anish Bhatt576b58632014-09-15 17:44:18 -0700191 mutex_unlock(&cdev_mutex);
192 cxgbi_device_unregister(cdev);
193 mutex_lock(&cdev_mutex);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700194 }
195 }
196 mutex_unlock(&cdev_mutex);
197}
198EXPORT_SYMBOL_GPL(cxgbi_device_unregister_all);
199
200struct 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);
Anish Bhatt576b58632014-09-15 17:44:18 -0700212
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700213 log_debug(1 << CXGBI_DBG_DEV,
214 "lldev 0x%p, NO match found.\n", lldev);
215 return NULL;
216}
217EXPORT_SYMBOL_GPL(cxgbi_device_find_by_lldev);
218
Anish Bhattfc8d0592014-07-17 00:18:17 -0700219struct cxgbi_device *cxgbi_device_find_by_netdev(struct net_device *ndev,
220 int *port)
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700221{
kxie@chelsio.com0b3d8942010-09-23 16:43:23 -0700222 struct net_device *vdev = NULL;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700223 struct cxgbi_device *cdev, *tmp;
224 int i;
225
Parav Panditd0d7b102017-02-04 11:00:49 -0600226 if (is_vlan_dev(ndev)) {
kxie@chelsio.com0b3d8942010-09-23 16:43:23 -0700227 vdev = ndev;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700228 ndev = vlan_dev_real_dev(ndev);
kxie@chelsio.com0b3d8942010-09-23 16:43:23 -0700229 log_debug(1 << CXGBI_DBG_DEV,
230 "vlan dev %s -> %s.\n", vdev->name, ndev->name);
231 }
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700232
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]) {
kxie@chelsio.com0b3d8942010-09-23 16:43:23 -0700237 cdev->hbas[i]->vdev = vdev;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700238 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}
Anish Bhattfc8d0592014-07-17 00:18:17 -0700250EXPORT_SYMBOL_GPL(cxgbi_device_find_by_netdev);
251
Anish Bhatt576b58632014-09-15 17:44:18 -0700252struct 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
Parav Panditd0d7b102017-02-04 11:00:49 -0600259 if (is_vlan_dev(ndev)) {
Anish Bhatt576b58632014-09-15 17:44:18 -0700260 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}
283EXPORT_SYMBOL_GPL(cxgbi_device_find_by_netdev_rcu);
284
Anish Bhattf42bb572014-10-14 20:07:23 -0700285#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattfc8d0592014-07-17 00:18:17 -0700286static struct cxgbi_device *cxgbi_device_find_by_mac(struct net_device *ndev,
287 int *port)
288{
289 struct net_device *vdev = NULL;
290 struct cxgbi_device *cdev, *tmp;
291 int i;
292
Parav Panditd0d7b102017-02-04 11:00:49 -0600293 if (is_vlan_dev(ndev)) {
Anish Bhattfc8d0592014-07-17 00:18:17 -0700294 vdev = ndev;
295 ndev = vlan_dev_real_dev(ndev);
296 pr_info("vlan dev %s -> %s.\n", vdev->name, ndev->name);
297 }
298
299 mutex_lock(&cdev_mutex);
300 list_for_each_entry_safe(cdev, tmp, &cdev_list, list_head) {
301 for (i = 0; i < cdev->nports; i++) {
302 if (!memcmp(ndev->dev_addr, cdev->ports[i]->dev_addr,
303 MAX_ADDR_LEN)) {
304 cdev->hbas[i]->vdev = vdev;
305 mutex_unlock(&cdev_mutex);
306 if (port)
307 *port = i;
308 return cdev;
309 }
310 }
311 }
312 mutex_unlock(&cdev_mutex);
313 log_debug(1 << CXGBI_DBG_DEV,
314 "ndev 0x%p, %s, NO match mac found.\n",
315 ndev, ndev->name);
316 return NULL;
317}
Anish Bhattf42bb572014-10-14 20:07:23 -0700318#endif
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700319
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700320void cxgbi_hbas_remove(struct cxgbi_device *cdev)
321{
322 int i;
323 struct cxgbi_hba *chba;
324
325 log_debug(1 << CXGBI_DBG_DEV,
326 "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
327
328 for (i = 0; i < cdev->nports; i++) {
329 chba = cdev->hbas[i];
330 if (chba) {
331 cdev->hbas[i] = NULL;
332 iscsi_host_remove(chba->shost);
333 pci_dev_put(cdev->pdev);
334 iscsi_host_free(chba->shost);
335 }
336 }
337}
338EXPORT_SYMBOL_GPL(cxgbi_hbas_remove);
339
Hannes Reinecke1abf6352014-06-25 15:27:38 +0200340int cxgbi_hbas_add(struct cxgbi_device *cdev, u64 max_lun,
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700341 unsigned int max_id, struct scsi_host_template *sht,
342 struct scsi_transport_template *stt)
343{
344 struct cxgbi_hba *chba;
345 struct Scsi_Host *shost;
346 int i, err;
347
348 log_debug(1 << CXGBI_DBG_DEV, "cdev 0x%p, p#%u.\n", cdev, cdev->nports);
349
350 for (i = 0; i < cdev->nports; i++) {
351 shost = iscsi_host_alloc(sht, sizeof(*chba), 1);
352 if (!shost) {
353 pr_info("0x%p, p%d, %s, host alloc failed.\n",
354 cdev, i, cdev->ports[i]->name);
355 err = -ENOMEM;
356 goto err_out;
357 }
358
359 shost->transportt = stt;
360 shost->max_lun = max_lun;
361 shost->max_id = max_id;
362 shost->max_channel = 0;
363 shost->max_cmd_len = 16;
364
365 chba = iscsi_host_priv(shost);
366 chba->cdev = cdev;
367 chba->ndev = cdev->ports[i];
368 chba->shost = shost;
369
370 log_debug(1 << CXGBI_DBG_DEV,
371 "cdev 0x%p, p#%d %s: chba 0x%p.\n",
372 cdev, i, cdev->ports[i]->name, chba);
373
374 pci_dev_get(cdev->pdev);
375 err = iscsi_host_add(shost, &cdev->pdev->dev);
376 if (err) {
377 pr_info("cdev 0x%p, p#%d %s, host add failed.\n",
378 cdev, i, cdev->ports[i]->name);
379 pci_dev_put(cdev->pdev);
380 scsi_host_put(shost);
381 goto err_out;
382 }
383
384 cdev->hbas[i] = chba;
385 }
386
387 return 0;
388
389err_out:
390 cxgbi_hbas_remove(cdev);
391 return err;
392}
393EXPORT_SYMBOL_GPL(cxgbi_hbas_add);
394
395/*
396 * iSCSI offload
397 *
398 * - source port management
399 * To find a free source port in the port allocation map we use a very simple
400 * rotor scheme to look for the next free port.
401 *
402 * If a source port has been specified make sure that it doesn't collide with
403 * our normal source port allocation map. If it's outside the range of our
404 * allocation/deallocation scheme just let them use it.
405 *
406 * If the source port is outside our allocation range, the caller is
407 * responsible for keeping track of their port usage.
408 */
Anish Bhattdd9ad672014-10-16 15:59:19 -0700409
410static struct cxgbi_sock *find_sock_on_port(struct cxgbi_device *cdev,
411 unsigned char port_id)
412{
413 struct cxgbi_ports_map *pmap = &cdev->pmap;
414 unsigned int i;
415 unsigned int used;
416
417 if (!pmap->max_connect || !pmap->used)
418 return NULL;
419
420 spin_lock_bh(&pmap->lock);
421 used = pmap->used;
422 for (i = 0; used && i < pmap->max_connect; i++) {
423 struct cxgbi_sock *csk = pmap->port_csk[i];
424
425 if (csk) {
426 if (csk->port_id == port_id) {
427 spin_unlock_bh(&pmap->lock);
428 return csk;
429 }
430 used--;
431 }
432 }
433 spin_unlock_bh(&pmap->lock);
434
435 return NULL;
436}
437
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700438static int sock_get_port(struct cxgbi_sock *csk)
439{
440 struct cxgbi_device *cdev = csk->cdev;
441 struct cxgbi_ports_map *pmap = &cdev->pmap;
442 unsigned int start;
443 int idx;
Anish Bhattfc8d0592014-07-17 00:18:17 -0700444 __be16 *port;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700445
446 if (!pmap->max_connect) {
447 pr_err("cdev 0x%p, p#%u %s, NO port map.\n",
448 cdev, csk->port_id, cdev->ports[csk->port_id]->name);
449 return -EADDRNOTAVAIL;
450 }
451
Anish Bhattfc8d0592014-07-17 00:18:17 -0700452 if (csk->csk_family == AF_INET)
453 port = &csk->saddr.sin_port;
454 else /* ipv6 */
455 port = &csk->saddr6.sin6_port;
456
457 if (*port) {
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700458 pr_err("source port NON-ZERO %u.\n",
Anish Bhattfc8d0592014-07-17 00:18:17 -0700459 ntohs(*port));
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700460 return -EADDRINUSE;
461 }
462
463 spin_lock_bh(&pmap->lock);
464 if (pmap->used >= pmap->max_connect) {
465 spin_unlock_bh(&pmap->lock);
466 pr_info("cdev 0x%p, p#%u %s, ALL ports used.\n",
467 cdev, csk->port_id, cdev->ports[csk->port_id]->name);
468 return -EADDRNOTAVAIL;
469 }
470
471 start = idx = pmap->next;
472 do {
473 if (++idx >= pmap->max_connect)
474 idx = 0;
475 if (!pmap->port_csk[idx]) {
476 pmap->used++;
Anish Bhattfc8d0592014-07-17 00:18:17 -0700477 *port = htons(pmap->sport_base + idx);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700478 pmap->next = idx;
479 pmap->port_csk[idx] = csk;
480 spin_unlock_bh(&pmap->lock);
481 cxgbi_sock_get(csk);
482 log_debug(1 << CXGBI_DBG_SOCK,
483 "cdev 0x%p, p#%u %s, p %u, %u.\n",
484 cdev, csk->port_id,
485 cdev->ports[csk->port_id]->name,
486 pmap->sport_base + idx, pmap->next);
487 return 0;
488 }
489 } while (idx != start);
490 spin_unlock_bh(&pmap->lock);
491
492 /* should not happen */
493 pr_warn("cdev 0x%p, p#%u %s, next %u?\n",
494 cdev, csk->port_id, cdev->ports[csk->port_id]->name,
495 pmap->next);
496 return -EADDRNOTAVAIL;
497}
498
499static void sock_put_port(struct cxgbi_sock *csk)
500{
501 struct cxgbi_device *cdev = csk->cdev;
502 struct cxgbi_ports_map *pmap = &cdev->pmap;
Anish Bhattfc8d0592014-07-17 00:18:17 -0700503 __be16 *port;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700504
Anish Bhattfc8d0592014-07-17 00:18:17 -0700505 if (csk->csk_family == AF_INET)
506 port = &csk->saddr.sin_port;
507 else /* ipv6 */
508 port = &csk->saddr6.sin6_port;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700509
Anish Bhattfc8d0592014-07-17 00:18:17 -0700510 if (*port) {
511 int idx = ntohs(*port) - pmap->sport_base;
512
513 *port = 0;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700514 if (idx < 0 || idx >= pmap->max_connect) {
515 pr_err("cdev 0x%p, p#%u %s, port %u OOR.\n",
516 cdev, csk->port_id,
517 cdev->ports[csk->port_id]->name,
Anish Bhattfc8d0592014-07-17 00:18:17 -0700518 ntohs(*port));
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700519 return;
520 }
521
522 spin_lock_bh(&pmap->lock);
523 pmap->port_csk[idx] = NULL;
524 pmap->used--;
525 spin_unlock_bh(&pmap->lock);
526
527 log_debug(1 << CXGBI_DBG_SOCK,
528 "cdev 0x%p, p#%u %s, release %u.\n",
529 cdev, csk->port_id, cdev->ports[csk->port_id]->name,
530 pmap->sport_base + idx);
531
532 cxgbi_sock_put(csk);
533 }
534}
535
536/*
537 * iscsi tcp connection
538 */
539void cxgbi_sock_free_cpl_skbs(struct cxgbi_sock *csk)
540{
541 if (csk->cpl_close) {
542 kfree_skb(csk->cpl_close);
543 csk->cpl_close = NULL;
544 }
545 if (csk->cpl_abort_req) {
546 kfree_skb(csk->cpl_abort_req);
547 csk->cpl_abort_req = NULL;
548 }
549 if (csk->cpl_abort_rpl) {
550 kfree_skb(csk->cpl_abort_rpl);
551 csk->cpl_abort_rpl = NULL;
552 }
553}
554EXPORT_SYMBOL_GPL(cxgbi_sock_free_cpl_skbs);
555
556static struct cxgbi_sock *cxgbi_sock_create(struct cxgbi_device *cdev)
557{
558 struct cxgbi_sock *csk = kzalloc(sizeof(*csk), GFP_NOIO);
559
560 if (!csk) {
561 pr_info("alloc csk %zu failed.\n", sizeof(*csk));
562 return NULL;
563 }
564
565 if (cdev->csk_alloc_cpls(csk) < 0) {
566 pr_info("csk 0x%p, alloc cpls failed.\n", csk);
567 kfree(csk);
568 return NULL;
569 }
570
571 spin_lock_init(&csk->lock);
572 kref_init(&csk->refcnt);
573 skb_queue_head_init(&csk->receive_queue);
574 skb_queue_head_init(&csk->write_queue);
575 setup_timer(&csk->retry_timer, NULL, (unsigned long)csk);
576 rwlock_init(&csk->callback_lock);
577 csk->cdev = cdev;
578 csk->flags = 0;
579 cxgbi_sock_set_state(csk, CTP_CLOSED);
580
581 log_debug(1 << CXGBI_DBG_SOCK, "cdev 0x%p, new csk 0x%p.\n", cdev, csk);
582
583 return csk;
584}
585
David S. Millerf4bfd992011-05-03 20:43:40 -0700586static struct rtable *find_route_ipv4(struct flowi4 *fl4,
587 __be32 saddr, __be32 daddr,
David S. Miller78fbfd82011-03-12 00:00:52 -0500588 __be16 sport, __be16 dport, u8 tos)
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700589{
590 struct rtable *rt;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700591
David S. Millerf4bfd992011-05-03 20:43:40 -0700592 rt = ip_route_output_ports(&init_net, fl4, NULL, daddr, saddr,
David S. Miller78fbfd82011-03-12 00:00:52 -0500593 dport, sport, IPPROTO_TCP, tos, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800594 if (IS_ERR(rt))
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700595 return NULL;
596
597 return rt;
598}
599
600static struct cxgbi_sock *cxgbi_check_route(struct sockaddr *dst_addr)
601{
602 struct sockaddr_in *daddr = (struct sockaddr_in *)dst_addr;
603 struct dst_entry *dst;
604 struct net_device *ndev;
605 struct cxgbi_device *cdev;
606 struct rtable *rt = NULL;
David Millera58b61e2011-12-02 16:52:35 +0000607 struct neighbour *n;
David S. Millerf4bfd992011-05-03 20:43:40 -0700608 struct flowi4 fl4;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700609 struct cxgbi_sock *csk = NULL;
610 unsigned int mtu = 0;
611 int port = 0xFFFF;
612 int err = 0;
613
David S. Millerf4bfd992011-05-03 20:43:40 -0700614 rt = find_route_ipv4(&fl4, 0, daddr->sin_addr.s_addr, 0, daddr->sin_port, 0);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700615 if (!rt) {
616 pr_info("no route to ipv4 0x%x, port %u.\n",
Anish Bhattfc8d0592014-07-17 00:18:17 -0700617 be32_to_cpu(daddr->sin_addr.s_addr),
618 be16_to_cpu(daddr->sin_port));
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700619 err = -ENETUNREACH;
620 goto err_out;
621 }
622 dst = &rt->dst;
David S. Miller0b399d42012-07-02 22:08:58 -0700623 n = dst_neigh_lookup(dst, &daddr->sin_addr.s_addr);
David Millera58b61e2011-12-02 16:52:35 +0000624 if (!n) {
625 err = -ENODEV;
626 goto rel_rt;
627 }
628 ndev = n->dev;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700629
630 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
631 pr_info("multi-cast route %pI4, port %u, dev %s.\n",
632 &daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
633 ndev->name);
634 err = -ENETUNREACH;
David S. Miller0b399d42012-07-02 22:08:58 -0700635 goto rel_neigh;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700636 }
637
638 if (ndev->flags & IFF_LOOPBACK) {
639 ndev = ip_dev_find(&init_net, daddr->sin_addr.s_addr);
640 mtu = ndev->mtu;
641 pr_info("rt dev %s, loopback -> %s, mtu %u.\n",
David Millera58b61e2011-12-02 16:52:35 +0000642 n->dev->name, ndev->name, mtu);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700643 }
644
Varun Prakash165ae502016-12-09 14:40:07 +0530645 if (!(ndev->flags & IFF_UP) || !netif_carrier_ok(ndev)) {
646 pr_info("%s interface not up.\n", ndev->name);
647 err = -ENETDOWN;
648 goto rel_neigh;
649 }
650
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700651 cdev = cxgbi_device_find_by_netdev(ndev, &port);
652 if (!cdev) {
653 pr_info("dst %pI4, %s, NOT cxgbi device.\n",
654 &daddr->sin_addr.s_addr, ndev->name);
655 err = -ENETUNREACH;
David S. Miller0b399d42012-07-02 22:08:58 -0700656 goto rel_neigh;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700657 }
658 log_debug(1 << CXGBI_DBG_SOCK,
659 "route to %pI4 :%u, ndev p#%d,%s, cdev 0x%p.\n",
660 &daddr->sin_addr.s_addr, ntohs(daddr->sin_port),
661 port, ndev->name, cdev);
662
663 csk = cxgbi_sock_create(cdev);
664 if (!csk) {
665 err = -ENOMEM;
David S. Miller0b399d42012-07-02 22:08:58 -0700666 goto rel_neigh;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700667 }
668 csk->cdev = cdev;
669 csk->port_id = port;
670 csk->mtu = mtu;
671 csk->dst = dst;
Anish Bhattfc8d0592014-07-17 00:18:17 -0700672
673 csk->csk_family = AF_INET;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700674 csk->daddr.sin_addr.s_addr = daddr->sin_addr.s_addr;
675 csk->daddr.sin_port = daddr->sin_port;
Mike Christiec71b9b62011-02-16 15:04:38 -0600676 csk->daddr.sin_family = daddr->sin_family;
Anish Bhattfc8d0592014-07-17 00:18:17 -0700677 csk->saddr.sin_family = daddr->sin_family;
David S. Millerf4bfd992011-05-03 20:43:40 -0700678 csk->saddr.sin_addr.s_addr = fl4.saddr;
David S. Miller0b399d42012-07-02 22:08:58 -0700679 neigh_release(n);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700680
681 return csk;
682
David S. Miller0b399d42012-07-02 22:08:58 -0700683rel_neigh:
684 neigh_release(n);
685
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700686rel_rt:
687 ip_rt_put(rt);
688 if (csk)
689 cxgbi_sock_closed(csk);
690err_out:
691 return ERR_PTR(err);
692}
693
Anish Bhatte81fbf62014-07-17 18:34:44 -0700694#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattfc8d0592014-07-17 00:18:17 -0700695static struct rt6_info *find_route_ipv6(const struct in6_addr *saddr,
696 const struct in6_addr *daddr)
697{
698 struct flowi6 fl;
699
Jiri Benc3d6d30d2016-04-22 13:09:13 +0200700 memset(&fl, 0, sizeof(fl));
Anish Bhattfc8d0592014-07-17 00:18:17 -0700701 if (saddr)
702 memcpy(&fl.saddr, saddr, sizeof(struct in6_addr));
703 if (daddr)
704 memcpy(&fl.daddr, daddr, sizeof(struct in6_addr));
705 return (struct rt6_info *)ip6_route_output(&init_net, NULL, &fl);
706}
707
708static struct cxgbi_sock *cxgbi_check_route6(struct sockaddr *dst_addr)
709{
710 struct sockaddr_in6 *daddr6 = (struct sockaddr_in6 *)dst_addr;
711 struct dst_entry *dst;
712 struct net_device *ndev;
713 struct cxgbi_device *cdev;
714 struct rt6_info *rt = NULL;
715 struct neighbour *n;
716 struct in6_addr pref_saddr;
717 struct cxgbi_sock *csk = NULL;
718 unsigned int mtu = 0;
719 int port = 0xFFFF;
720 int err = 0;
721
722 rt = find_route_ipv6(NULL, &daddr6->sin6_addr);
723
724 if (!rt) {
725 pr_info("no route to ipv6 %pI6 port %u\n",
726 daddr6->sin6_addr.s6_addr,
727 be16_to_cpu(daddr6->sin6_port));
728 err = -ENETUNREACH;
729 goto err_out;
730 }
731
732 dst = &rt->dst;
733
734 n = dst_neigh_lookup(dst, &daddr6->sin6_addr);
735
736 if (!n) {
737 pr_info("%pI6, port %u, dst no neighbour.\n",
738 daddr6->sin6_addr.s6_addr,
739 be16_to_cpu(daddr6->sin6_port));
740 err = -ENETUNREACH;
741 goto rel_rt;
742 }
743 ndev = n->dev;
744
Varun Prakash165ae502016-12-09 14:40:07 +0530745 if (!(ndev->flags & IFF_UP) || !netif_carrier_ok(ndev)) {
746 pr_info("%s interface not up.\n", ndev->name);
747 err = -ENETDOWN;
748 goto rel_rt;
749 }
750
Martin KaFai Laufd0273d2015-05-22 20:55:57 -0700751 if (ipv6_addr_is_multicast(&daddr6->sin6_addr)) {
Anish Bhattfc8d0592014-07-17 00:18:17 -0700752 pr_info("multi-cast route %pI6 port %u, dev %s.\n",
753 daddr6->sin6_addr.s6_addr,
754 ntohs(daddr6->sin6_port), ndev->name);
755 err = -ENETUNREACH;
756 goto rel_rt;
757 }
758
759 cdev = cxgbi_device_find_by_netdev(ndev, &port);
760 if (!cdev)
761 cdev = cxgbi_device_find_by_mac(ndev, &port);
762 if (!cdev) {
763 pr_info("dst %pI6 %s, NOT cxgbi device.\n",
764 daddr6->sin6_addr.s6_addr, ndev->name);
765 err = -ENETUNREACH;
766 goto rel_rt;
767 }
768 log_debug(1 << CXGBI_DBG_SOCK,
769 "route to %pI6 :%u, ndev p#%d,%s, cdev 0x%p.\n",
770 daddr6->sin6_addr.s6_addr, ntohs(daddr6->sin6_port), port,
771 ndev->name, cdev);
772
773 csk = cxgbi_sock_create(cdev);
774 if (!csk) {
775 err = -ENOMEM;
776 goto rel_rt;
777 }
778 csk->cdev = cdev;
779 csk->port_id = port;
780 csk->mtu = mtu;
781 csk->dst = dst;
782
783 if (ipv6_addr_any(&rt->rt6i_prefsrc.addr)) {
784 struct inet6_dev *idev = ip6_dst_idev((struct dst_entry *)rt);
785
786 err = ipv6_dev_get_saddr(&init_net, idev ? idev->dev : NULL,
787 &daddr6->sin6_addr, 0, &pref_saddr);
788 if (err) {
789 pr_info("failed to get source address to reach %pI6\n",
790 &daddr6->sin6_addr);
791 goto rel_rt;
792 }
793 } else {
794 pref_saddr = rt->rt6i_prefsrc.addr;
795 }
796
797 csk->csk_family = AF_INET6;
798 csk->daddr6.sin6_addr = daddr6->sin6_addr;
799 csk->daddr6.sin6_port = daddr6->sin6_port;
800 csk->daddr6.sin6_family = daddr6->sin6_family;
Anish Bhattdd9ad672014-10-16 15:59:19 -0700801 csk->saddr6.sin6_family = daddr6->sin6_family;
Anish Bhattfc8d0592014-07-17 00:18:17 -0700802 csk->saddr6.sin6_addr = pref_saddr;
803
804 neigh_release(n);
805 return csk;
806
807rel_rt:
808 if (n)
809 neigh_release(n);
810
811 ip6_rt_put(rt);
812 if (csk)
813 cxgbi_sock_closed(csk);
814err_out:
815 return ERR_PTR(err);
816}
Anish Bhatte81fbf62014-07-17 18:34:44 -0700817#endif /* IS_ENABLED(CONFIG_IPV6) */
Anish Bhattfc8d0592014-07-17 00:18:17 -0700818
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700819void cxgbi_sock_established(struct cxgbi_sock *csk, unsigned int snd_isn,
820 unsigned int opt)
821{
822 csk->write_seq = csk->snd_nxt = csk->snd_una = snd_isn;
823 dst_confirm(csk->dst);
824 smp_mb();
825 cxgbi_sock_set_state(csk, CTP_ESTABLISHED);
826}
827EXPORT_SYMBOL_GPL(cxgbi_sock_established);
828
829static void cxgbi_inform_iscsi_conn_closing(struct cxgbi_sock *csk)
830{
831 log_debug(1 << CXGBI_DBG_SOCK,
832 "csk 0x%p, state %u, flags 0x%lx, conn 0x%p.\n",
833 csk, csk->state, csk->flags, csk->user_data);
834
835 if (csk->state != CTP_ESTABLISHED) {
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -0700836 read_lock_bh(&csk->callback_lock);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700837 if (csk->user_data)
838 iscsi_conn_failure(csk->user_data,
Anish Bhattee7255a2014-11-18 19:09:51 -0800839 ISCSI_ERR_TCP_CONN_CLOSE);
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -0700840 read_unlock_bh(&csk->callback_lock);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700841 }
842}
843
844void cxgbi_sock_closed(struct cxgbi_sock *csk)
845{
846 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
847 csk, (csk)->state, (csk)->flags, (csk)->tid);
848 cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
849 if (csk->state == CTP_ACTIVE_OPEN || csk->state == CTP_CLOSED)
850 return;
851 if (csk->saddr.sin_port)
852 sock_put_port(csk);
853 if (csk->dst)
854 dst_release(csk->dst);
855 csk->cdev->csk_release_offload_resources(csk);
856 cxgbi_sock_set_state(csk, CTP_CLOSED);
857 cxgbi_inform_iscsi_conn_closing(csk);
858 cxgbi_sock_put(csk);
859}
860EXPORT_SYMBOL_GPL(cxgbi_sock_closed);
861
862static void need_active_close(struct cxgbi_sock *csk)
863{
864 int data_lost;
865 int close_req = 0;
866
867 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
868 csk, (csk)->state, (csk)->flags, (csk)->tid);
869 spin_lock_bh(&csk->lock);
Varun Prakashe0f8e8c2017-05-27 20:22:51 +0530870 if (csk->dst)
871 dst_confirm(csk->dst);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700872 data_lost = skb_queue_len(&csk->receive_queue);
873 __skb_queue_purge(&csk->receive_queue);
874
875 if (csk->state == CTP_ACTIVE_OPEN)
876 cxgbi_sock_set_flag(csk, CTPF_ACTIVE_CLOSE_NEEDED);
877 else if (csk->state == CTP_ESTABLISHED) {
878 close_req = 1;
879 cxgbi_sock_set_state(csk, CTP_ACTIVE_CLOSE);
880 } else if (csk->state == CTP_PASSIVE_CLOSE) {
881 close_req = 1;
882 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
883 }
884
885 if (close_req) {
Varun Prakashe0f8e8c2017-05-27 20:22:51 +0530886 if (!cxgbi_sock_flag(csk, CTPF_LOGOUT_RSP_RCVD) ||
887 data_lost)
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700888 csk->cdev->csk_send_abort_req(csk);
889 else
890 csk->cdev->csk_send_close_req(csk);
891 }
892
893 spin_unlock_bh(&csk->lock);
894}
895
896void cxgbi_sock_fail_act_open(struct cxgbi_sock *csk, int errno)
897{
898 pr_info("csk 0x%p,%u,%lx, %pI4:%u-%pI4:%u, err %d.\n",
899 csk, csk->state, csk->flags,
900 &csk->saddr.sin_addr.s_addr, csk->saddr.sin_port,
901 &csk->daddr.sin_addr.s_addr, csk->daddr.sin_port,
902 errno);
903
904 cxgbi_sock_set_state(csk, CTP_CONNECTING);
905 csk->err = errno;
906 cxgbi_sock_closed(csk);
907}
908EXPORT_SYMBOL_GPL(cxgbi_sock_fail_act_open);
909
910void cxgbi_sock_act_open_req_arp_failure(void *handle, struct sk_buff *skb)
911{
912 struct cxgbi_sock *csk = (struct cxgbi_sock *)skb->sk;
Varun Prakash1fe1fdb2016-12-07 21:06:45 +0530913 struct module *owner = csk->cdev->owner;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700914
915 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
916 csk, (csk)->state, (csk)->flags, (csk)->tid);
917 cxgbi_sock_get(csk);
918 spin_lock_bh(&csk->lock);
919 if (csk->state == CTP_ACTIVE_OPEN)
920 cxgbi_sock_fail_act_open(csk, -EHOSTUNREACH);
921 spin_unlock_bh(&csk->lock);
922 cxgbi_sock_put(csk);
923 __kfree_skb(skb);
Varun Prakash1fe1fdb2016-12-07 21:06:45 +0530924
925 module_put(owner);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700926}
927EXPORT_SYMBOL_GPL(cxgbi_sock_act_open_req_arp_failure);
928
929void cxgbi_sock_rcv_abort_rpl(struct cxgbi_sock *csk)
930{
931 cxgbi_sock_get(csk);
932 spin_lock_bh(&csk->lock);
Anish Bhatt7b07bf22014-11-06 12:53:58 -0800933
934 cxgbi_sock_set_flag(csk, CTPF_ABORT_RPL_RCVD);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700935 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING)) {
Anish Bhatt7b07bf22014-11-06 12:53:58 -0800936 cxgbi_sock_clear_flag(csk, CTPF_ABORT_RPL_PENDING);
937 if (cxgbi_sock_flag(csk, CTPF_ABORT_REQ_RCVD))
938 pr_err("csk 0x%p,%u,0x%lx,%u,ABT_RPL_RSS.\n",
939 csk, csk->state, csk->flags, csk->tid);
940 cxgbi_sock_closed(csk);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700941 }
Anish Bhatt7b07bf22014-11-06 12:53:58 -0800942
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -0700943 spin_unlock_bh(&csk->lock);
944 cxgbi_sock_put(csk);
945}
946EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_abort_rpl);
947
948void cxgbi_sock_rcv_peer_close(struct cxgbi_sock *csk)
949{
950 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
951 csk, (csk)->state, (csk)->flags, (csk)->tid);
952 cxgbi_sock_get(csk);
953 spin_lock_bh(&csk->lock);
954
955 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
956 goto done;
957
958 switch (csk->state) {
959 case CTP_ESTABLISHED:
960 cxgbi_sock_set_state(csk, CTP_PASSIVE_CLOSE);
961 break;
962 case CTP_ACTIVE_CLOSE:
963 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_2);
964 break;
965 case CTP_CLOSE_WAIT_1:
966 cxgbi_sock_closed(csk);
967 break;
968 case CTP_ABORTING:
969 break;
970 default:
971 pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
972 csk, csk->state, csk->flags, csk->tid);
973 }
974 cxgbi_inform_iscsi_conn_closing(csk);
975done:
976 spin_unlock_bh(&csk->lock);
977 cxgbi_sock_put(csk);
978}
979EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_peer_close);
980
981void cxgbi_sock_rcv_close_conn_rpl(struct cxgbi_sock *csk, u32 snd_nxt)
982{
983 log_debug(1 << CXGBI_DBG_SOCK, "csk 0x%p,%u,0x%lx,%u.\n",
984 csk, (csk)->state, (csk)->flags, (csk)->tid);
985 cxgbi_sock_get(csk);
986 spin_lock_bh(&csk->lock);
987
988 csk->snd_una = snd_nxt - 1;
989 if (cxgbi_sock_flag(csk, CTPF_ABORT_RPL_PENDING))
990 goto done;
991
992 switch (csk->state) {
993 case CTP_ACTIVE_CLOSE:
994 cxgbi_sock_set_state(csk, CTP_CLOSE_WAIT_1);
995 break;
996 case CTP_CLOSE_WAIT_1:
997 case CTP_CLOSE_WAIT_2:
998 cxgbi_sock_closed(csk);
999 break;
1000 case CTP_ABORTING:
1001 break;
1002 default:
1003 pr_err("csk 0x%p,%u,0x%lx,%u, bad state.\n",
1004 csk, csk->state, csk->flags, csk->tid);
1005 }
1006done:
1007 spin_unlock_bh(&csk->lock);
1008 cxgbi_sock_put(csk);
1009}
1010EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_close_conn_rpl);
1011
1012void cxgbi_sock_rcv_wr_ack(struct cxgbi_sock *csk, unsigned int credits,
1013 unsigned int snd_una, int seq_chk)
1014{
1015 log_debug(1 << CXGBI_DBG_TOE | 1 << CXGBI_DBG_SOCK,
1016 "csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, snd_una %u,%d.\n",
1017 csk, csk->state, csk->flags, csk->tid, credits,
1018 csk->wr_cred, csk->wr_una_cred, snd_una, seq_chk);
1019
1020 spin_lock_bh(&csk->lock);
1021
1022 csk->wr_cred += credits;
1023 if (csk->wr_una_cred > csk->wr_max_cred - csk->wr_cred)
1024 csk->wr_una_cred = csk->wr_max_cred - csk->wr_cred;
1025
1026 while (credits) {
1027 struct sk_buff *p = cxgbi_sock_peek_wr(csk);
1028
1029 if (unlikely(!p)) {
1030 pr_err("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, empty.\n",
1031 csk, csk->state, csk->flags, csk->tid, credits,
1032 csk->wr_cred, csk->wr_una_cred);
1033 break;
1034 }
1035
1036 if (unlikely(credits < p->csum)) {
1037 pr_warn("csk 0x%p,%u,0x%lx,%u, cr %u,%u+%u, < %u.\n",
1038 csk, csk->state, csk->flags, csk->tid,
1039 credits, csk->wr_cred, csk->wr_una_cred,
1040 p->csum);
1041 p->csum -= credits;
1042 break;
1043 } else {
1044 cxgbi_sock_dequeue_wr(csk);
1045 credits -= p->csum;
1046 kfree_skb(p);
1047 }
1048 }
1049
1050 cxgbi_sock_check_wr_invariants(csk);
1051
1052 if (seq_chk) {
1053 if (unlikely(before(snd_una, csk->snd_una))) {
1054 pr_warn("csk 0x%p,%u,0x%lx,%u, snd_una %u/%u.",
1055 csk, csk->state, csk->flags, csk->tid, snd_una,
1056 csk->snd_una);
1057 goto done;
1058 }
1059
1060 if (csk->snd_una != snd_una) {
1061 csk->snd_una = snd_una;
1062 dst_confirm(csk->dst);
1063 }
1064 }
1065
1066 if (skb_queue_len(&csk->write_queue)) {
1067 if (csk->cdev->csk_push_tx_frames(csk, 0))
1068 cxgbi_conn_tx_open(csk);
1069 } else
1070 cxgbi_conn_tx_open(csk);
1071done:
1072 spin_unlock_bh(&csk->lock);
1073}
1074EXPORT_SYMBOL_GPL(cxgbi_sock_rcv_wr_ack);
1075
1076static unsigned int cxgbi_sock_find_best_mtu(struct cxgbi_sock *csk,
1077 unsigned short mtu)
1078{
1079 int i = 0;
1080
1081 while (i < csk->cdev->nmtus - 1 && csk->cdev->mtus[i + 1] <= mtu)
1082 ++i;
1083
1084 return i;
1085}
1086
1087unsigned int cxgbi_sock_select_mss(struct cxgbi_sock *csk, unsigned int pmtu)
1088{
1089 unsigned int idx;
1090 struct dst_entry *dst = csk->dst;
1091
David S. Miller0dbaee32010-12-13 12:52:14 -08001092 csk->advmss = dst_metric_advmss(dst);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001093
1094 if (csk->advmss > pmtu - 40)
1095 csk->advmss = pmtu - 40;
1096 if (csk->advmss < csk->cdev->mtus[0] - 40)
1097 csk->advmss = csk->cdev->mtus[0] - 40;
1098 idx = cxgbi_sock_find_best_mtu(csk, csk->advmss + 40);
1099
1100 return idx;
1101}
1102EXPORT_SYMBOL_GPL(cxgbi_sock_select_mss);
1103
1104void cxgbi_sock_skb_entail(struct cxgbi_sock *csk, struct sk_buff *skb)
1105{
1106 cxgbi_skcb_tcp_seq(skb) = csk->write_seq;
1107 __skb_queue_tail(&csk->write_queue, skb);
1108}
1109EXPORT_SYMBOL_GPL(cxgbi_sock_skb_entail);
1110
1111void cxgbi_sock_purge_wr_queue(struct cxgbi_sock *csk)
1112{
1113 struct sk_buff *skb;
1114
1115 while ((skb = cxgbi_sock_dequeue_wr(csk)) != NULL)
1116 kfree_skb(skb);
1117}
1118EXPORT_SYMBOL_GPL(cxgbi_sock_purge_wr_queue);
1119
1120void cxgbi_sock_check_wr_invariants(const struct cxgbi_sock *csk)
1121{
1122 int pending = cxgbi_sock_count_pending_wrs(csk);
1123
1124 if (unlikely(csk->wr_cred + pending != csk->wr_max_cred))
1125 pr_err("csk 0x%p, tid %u, credit %u + %u != %u.\n",
1126 csk, csk->tid, csk->wr_cred, pending, csk->wr_max_cred);
1127}
1128EXPORT_SYMBOL_GPL(cxgbi_sock_check_wr_invariants);
1129
1130static int cxgbi_sock_send_pdus(struct cxgbi_sock *csk, struct sk_buff *skb)
1131{
1132 struct cxgbi_device *cdev = csk->cdev;
1133 struct sk_buff *next;
1134 int err, copied = 0;
1135
1136 spin_lock_bh(&csk->lock);
1137
1138 if (csk->state != CTP_ESTABLISHED) {
1139 log_debug(1 << CXGBI_DBG_PDU_TX,
1140 "csk 0x%p,%u,0x%lx,%u, EAGAIN.\n",
1141 csk, csk->state, csk->flags, csk->tid);
1142 err = -EAGAIN;
1143 goto out_err;
1144 }
1145
1146 if (csk->err) {
1147 log_debug(1 << CXGBI_DBG_PDU_TX,
1148 "csk 0x%p,%u,0x%lx,%u, EPIPE %d.\n",
1149 csk, csk->state, csk->flags, csk->tid, csk->err);
1150 err = -EPIPE;
1151 goto out_err;
1152 }
1153
Karen Xie81daf102015-04-10 13:57:12 -07001154 if (csk->write_seq - csk->snd_una >= csk->snd_win) {
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001155 log_debug(1 << CXGBI_DBG_PDU_TX,
1156 "csk 0x%p,%u,0x%lx,%u, FULL %u-%u >= %u.\n",
1157 csk, csk->state, csk->flags, csk->tid, csk->write_seq,
Karen Xie81daf102015-04-10 13:57:12 -07001158 csk->snd_una, csk->snd_win);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001159 err = -ENOBUFS;
1160 goto out_err;
1161 }
1162
1163 while (skb) {
1164 int frags = skb_shinfo(skb)->nr_frags +
1165 (skb->len != skb->data_len);
1166
1167 if (unlikely(skb_headroom(skb) < cdev->skb_tx_rsvd)) {
1168 pr_err("csk 0x%p, skb head %u < %u.\n",
1169 csk, skb_headroom(skb), cdev->skb_tx_rsvd);
1170 err = -EINVAL;
1171 goto out_err;
1172 }
1173
1174 if (frags >= SKB_WR_LIST_SIZE) {
1175 pr_err("csk 0x%p, frags %d, %u,%u >%u.\n",
1176 csk, skb_shinfo(skb)->nr_frags, skb->len,
1177 skb->data_len, (uint)(SKB_WR_LIST_SIZE));
1178 err = -EINVAL;
1179 goto out_err;
1180 }
1181
1182 next = skb->next;
1183 skb->next = NULL;
1184 cxgbi_skcb_set_flag(skb, SKCBF_TX_NEED_HDR);
1185 cxgbi_sock_skb_entail(csk, skb);
1186 copied += skb->len;
1187 csk->write_seq += skb->len +
1188 cxgbi_ulp_extra_len(cxgbi_skcb_ulp_mode(skb));
1189 skb = next;
1190 }
Varun Prakashe0f8e8c2017-05-27 20:22:51 +05301191
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001192 if (likely(skb_queue_len(&csk->write_queue)))
1193 cdev->csk_push_tx_frames(csk, 1);
Varun Prakashe0f8e8c2017-05-27 20:22:51 +05301194done:
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001195 spin_unlock_bh(&csk->lock);
1196 return copied;
1197
1198out_err:
1199 if (copied == 0 && err == -EPIPE)
1200 copied = csk->err ? csk->err : -EPIPE;
1201 else
1202 copied = err;
1203 goto done;
1204}
1205
Varun Prakash71f7a002016-07-21 22:57:16 +05301206static inline void
1207scmd_get_params(struct scsi_cmnd *sc, struct scatterlist **sgl,
1208 unsigned int *sgcnt, unsigned int *dlen,
1209 unsigned int prot)
1210{
1211 struct scsi_data_buffer *sdb = prot ? scsi_prot(sc) : scsi_out(sc);
1212
1213 *sgl = sdb->table.sgl;
1214 *sgcnt = sdb->table.nents;
1215 *dlen = sdb->length;
1216 /* Caution: for protection sdb, sdb->length is invalid */
1217}
1218
1219void cxgbi_ddp_set_one_ppod(struct cxgbi_pagepod *ppod,
1220 struct cxgbi_task_tag_info *ttinfo,
1221 struct scatterlist **sg_pp, unsigned int *sg_off)
1222{
1223 struct scatterlist *sg = sg_pp ? *sg_pp : NULL;
1224 unsigned int offset = sg_off ? *sg_off : 0;
1225 dma_addr_t addr = 0UL;
1226 unsigned int len = 0;
1227 int i;
1228
1229 memcpy(ppod, &ttinfo->hdr, sizeof(struct cxgbi_pagepod_hdr));
1230
1231 if (sg) {
1232 addr = sg_dma_address(sg);
1233 len = sg_dma_len(sg);
1234 }
1235
1236 for (i = 0; i < PPOD_PAGES_MAX; i++) {
1237 if (sg) {
1238 ppod->addr[i] = cpu_to_be64(addr + offset);
1239 offset += PAGE_SIZE;
1240 if (offset == (len + sg->offset)) {
1241 offset = 0;
1242 sg = sg_next(sg);
1243 if (sg) {
1244 addr = sg_dma_address(sg);
1245 len = sg_dma_len(sg);
1246 }
1247 }
1248 } else {
1249 ppod->addr[i] = 0ULL;
1250 }
1251 }
1252
1253 /*
1254 * the fifth address needs to be repeated in the next ppod, so do
1255 * not move sg
1256 */
1257 if (sg_pp) {
1258 *sg_pp = sg;
1259 *sg_off = offset;
1260 }
1261
1262 if (offset == len) {
1263 offset = 0;
1264 sg = sg_next(sg);
1265 if (sg) {
1266 addr = sg_dma_address(sg);
1267 len = sg_dma_len(sg);
1268 }
1269 }
1270 ppod->addr[i] = sg ? cpu_to_be64(addr + offset) : 0ULL;
1271}
1272EXPORT_SYMBOL_GPL(cxgbi_ddp_set_one_ppod);
1273
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001274/*
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001275 * APIs interacting with open-iscsi libraries
1276 */
1277
1278static unsigned char padding[4];
1279
Varun Prakash71f7a002016-07-21 22:57:16 +05301280void cxgbi_ddp_ppm_setup(void **ppm_pp, struct cxgbi_device *cdev,
1281 struct cxgbi_tag_format *tformat, unsigned int ppmax,
1282 unsigned int llimit, unsigned int start,
1283 unsigned int rsvd_factor)
1284{
1285 int err = cxgbi_ppm_init(ppm_pp, cdev->ports[0], cdev->pdev,
1286 cdev->lldev, tformat, ppmax, llimit, start,
1287 rsvd_factor);
1288
1289 if (err >= 0) {
1290 struct cxgbi_ppm *ppm = (struct cxgbi_ppm *)(*ppm_pp);
1291
1292 if (ppm->ppmax < 1024 ||
1293 ppm->tformat.pgsz_idx_dflt >= DDP_PGIDX_MAX)
1294 cdev->flags |= CXGBI_FLAG_DDP_OFF;
1295 err = 0;
1296 } else {
1297 cdev->flags |= CXGBI_FLAG_DDP_OFF;
1298 }
1299}
1300EXPORT_SYMBOL_GPL(cxgbi_ddp_ppm_setup);
1301
1302static int cxgbi_ddp_sgl_check(struct scatterlist *sgl, int nents)
1303{
1304 int i;
1305 int last_sgidx = nents - 1;
1306 struct scatterlist *sg = sgl;
1307
1308 for (i = 0; i < nents; i++, sg = sg_next(sg)) {
1309 unsigned int len = sg->length + sg->offset;
1310
1311 if ((sg->offset & 0x3) || (i && sg->offset) ||
1312 ((i != last_sgidx) && len != PAGE_SIZE)) {
1313 log_debug(1 << CXGBI_DBG_DDP,
1314 "sg %u/%u, %u,%u, not aligned.\n",
1315 i, nents, sg->offset, sg->length);
1316 goto err_out;
1317 }
1318 }
1319 return 0;
1320err_out:
1321 return -EINVAL;
1322}
1323
1324static int cxgbi_ddp_reserve(struct cxgbi_conn *cconn,
1325 struct cxgbi_task_data *tdata, u32 sw_tag,
1326 unsigned int xferlen)
1327{
1328 struct cxgbi_sock *csk = cconn->cep->csk;
1329 struct cxgbi_device *cdev = csk->cdev;
1330 struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
1331 struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
1332 struct scatterlist *sgl = ttinfo->sgl;
1333 unsigned int sgcnt = ttinfo->nents;
1334 unsigned int sg_offset = sgl->offset;
1335 int err;
1336
1337 if (cdev->flags & CXGBI_FLAG_DDP_OFF) {
1338 log_debug(1 << CXGBI_DBG_DDP,
1339 "cdev 0x%p DDP off.\n", cdev);
1340 return -EINVAL;
1341 }
1342
1343 if (!ppm || xferlen < DDP_THRESHOLD || !sgcnt ||
1344 ppm->tformat.pgsz_idx_dflt >= DDP_PGIDX_MAX) {
1345 log_debug(1 << CXGBI_DBG_DDP,
1346 "ppm 0x%p, pgidx %u, xfer %u, sgcnt %u, NO ddp.\n",
1347 ppm, ppm ? ppm->tformat.pgsz_idx_dflt : DDP_PGIDX_MAX,
1348 xferlen, ttinfo->nents);
1349 return -EINVAL;
1350 }
1351
1352 /* make sure the buffer is suitable for ddp */
1353 if (cxgbi_ddp_sgl_check(sgl, sgcnt) < 0)
1354 return -EINVAL;
1355
1356 ttinfo->nr_pages = (xferlen + sgl->offset + (1 << PAGE_SHIFT) - 1) >>
1357 PAGE_SHIFT;
1358
1359 /*
1360 * the ddp tag will be used for the itt in the outgoing pdu,
1361 * the itt genrated by libiscsi is saved in the ppm and can be
1362 * retrieved via the ddp tag
1363 */
1364 err = cxgbi_ppm_ppods_reserve(ppm, ttinfo->nr_pages, 0, &ttinfo->idx,
1365 &ttinfo->tag, (unsigned long)sw_tag);
1366 if (err < 0) {
1367 cconn->ddp_full++;
1368 return err;
1369 }
1370 ttinfo->npods = err;
1371
1372 /* setup dma from scsi command sgl */
1373 sgl->offset = 0;
1374 err = dma_map_sg(&ppm->pdev->dev, sgl, sgcnt, DMA_FROM_DEVICE);
1375 sgl->offset = sg_offset;
1376 if (err == 0) {
1377 pr_info("%s: 0x%x, xfer %u, sgl %u dma mapping err.\n",
1378 __func__, sw_tag, xferlen, sgcnt);
1379 goto rel_ppods;
1380 }
1381 if (err != ttinfo->nr_pages) {
1382 log_debug(1 << CXGBI_DBG_DDP,
1383 "%s: sw tag 0x%x, xfer %u, sgl %u, dma count %d.\n",
1384 __func__, sw_tag, xferlen, sgcnt, err);
1385 }
1386
1387 ttinfo->flags |= CXGBI_PPOD_INFO_FLAG_MAPPED;
1388 ttinfo->cid = csk->port_id;
1389
1390 cxgbi_ppm_make_ppod_hdr(ppm, ttinfo->tag, csk->tid, sgl->offset,
1391 xferlen, &ttinfo->hdr);
1392
1393 if (cdev->flags & CXGBI_FLAG_USE_PPOD_OFLDQ) {
1394 /* write ppod from xmit_pdu (of iscsi_scsi_command pdu) */
1395 ttinfo->flags |= CXGBI_PPOD_INFO_FLAG_VALID;
1396 } else {
1397 /* write ppod from control queue now */
1398 err = cdev->csk_ddp_set_map(ppm, csk, ttinfo);
1399 if (err < 0)
1400 goto rel_ppods;
1401 }
1402
1403 return 0;
1404
1405rel_ppods:
1406 cxgbi_ppm_ppod_release(ppm, ttinfo->idx);
1407
1408 if (ttinfo->flags & CXGBI_PPOD_INFO_FLAG_MAPPED) {
1409 ttinfo->flags &= ~CXGBI_PPOD_INFO_FLAG_MAPPED;
1410 dma_unmap_sg(&ppm->pdev->dev, sgl, sgcnt, DMA_FROM_DEVICE);
1411 }
1412 return -EINVAL;
1413}
1414
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001415static void task_release_itt(struct iscsi_task *task, itt_t hdr_itt)
1416{
Varun Prakash71f7a002016-07-21 22:57:16 +05301417 struct scsi_cmnd *sc = task->sc;
1418 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
1419 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1420 struct cxgbi_device *cdev = cconn->chba->cdev;
1421 struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
1422 u32 tag = ntohl((__force u32)hdr_itt);
1423
1424 log_debug(1 << CXGBI_DBG_DDP,
1425 "cdev 0x%p, task 0x%p, release tag 0x%x.\n",
1426 cdev, task, tag);
1427 if (sc &&
1428 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
1429 cxgbi_ppm_is_ddp_tag(ppm, tag)) {
1430 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1431 struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
1432
1433 if (!(cdev->flags & CXGBI_FLAG_USE_PPOD_OFLDQ))
1434 cdev->csk_ddp_clear_map(cdev, ppm, ttinfo);
1435 cxgbi_ppm_ppod_release(ppm, ttinfo->idx);
1436 dma_unmap_sg(&ppm->pdev->dev, ttinfo->sgl, ttinfo->nents,
1437 DMA_FROM_DEVICE);
1438 }
1439}
1440
1441static inline u32 cxgbi_build_sw_tag(u32 idx, u32 age)
1442{
1443 /* assume idx and age both are < 0x7FFF (32767) */
1444 return (idx << 16) | age;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001445}
1446
1447static int task_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
1448{
Varun Prakash71f7a002016-07-21 22:57:16 +05301449 struct scsi_cmnd *sc = task->sc;
1450 struct iscsi_conn *conn = task->conn;
1451 struct iscsi_session *sess = conn->session;
1452 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1453 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1454 struct cxgbi_device *cdev = cconn->chba->cdev;
1455 struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
1456 u32 sw_tag = cxgbi_build_sw_tag(task->itt, sess->age);
1457 u32 tag = 0;
1458 int err = -EINVAL;
1459
1460 if (sc &&
1461 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE)
1462 ) {
1463 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
1464 struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
1465
1466 scmd_get_params(sc, &ttinfo->sgl, &ttinfo->nents,
1467 &tdata->dlen, 0);
1468 err = cxgbi_ddp_reserve(cconn, tdata, sw_tag, tdata->dlen);
1469 if (!err)
1470 tag = ttinfo->tag;
1471 else
1472 log_debug(1 << CXGBI_DBG_DDP,
1473 "csk 0x%p, R task 0x%p, %u,%u, no ddp.\n",
1474 cconn->cep->csk, task, tdata->dlen,
1475 ttinfo->nents);
1476 }
1477
1478 if (err < 0) {
1479 err = cxgbi_ppm_make_non_ddp_tag(ppm, sw_tag, &tag);
1480 if (err < 0)
1481 return err;
1482 }
1483 /* the itt need to sent in big-endian order */
1484 *hdr_itt = (__force itt_t)htonl(tag);
1485
1486 log_debug(1 << CXGBI_DBG_DDP,
1487 "cdev 0x%p, task 0x%p, 0x%x(0x%x,0x%x)->0x%x/0x%x.\n",
1488 cdev, task, sw_tag, task->itt, sess->age, tag, *hdr_itt);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001489 return 0;
1490}
1491
1492void cxgbi_parse_pdu_itt(struct iscsi_conn *conn, itt_t itt, int *idx, int *age)
1493{
Varun Prakash71f7a002016-07-21 22:57:16 +05301494 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1495 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1496 struct cxgbi_device *cdev = cconn->chba->cdev;
1497 struct cxgbi_ppm *ppm = cdev->cdev2ppm(cdev);
1498 u32 tag = ntohl((__force u32)itt);
1499 u32 sw_bits;
1500
1501 if (ppm) {
1502 if (cxgbi_ppm_is_ddp_tag(ppm, tag))
1503 sw_bits = cxgbi_ppm_get_tag_caller_data(ppm, tag);
1504 else
1505 sw_bits = cxgbi_ppm_decode_non_ddp_tag(ppm, tag);
1506 } else {
1507 sw_bits = tag;
1508 }
1509
1510 cxgbi_decode_sw_tag(sw_bits, idx, age);
1511 log_debug(1 << CXGBI_DBG_DDP,
1512 "cdev 0x%p, tag 0x%x/0x%x, -> 0x%x(0x%x,0x%x).\n",
1513 cdev, tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
1514 age ? *age : 0xFF);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001515}
1516EXPORT_SYMBOL_GPL(cxgbi_parse_pdu_itt);
1517
1518void cxgbi_conn_tx_open(struct cxgbi_sock *csk)
1519{
1520 struct iscsi_conn *conn = csk->user_data;
1521
1522 if (conn) {
1523 log_debug(1 << CXGBI_DBG_SOCK,
1524 "csk 0x%p, cid %d.\n", csk, conn->id);
1525 iscsi_conn_queue_work(conn);
1526 }
1527}
1528EXPORT_SYMBOL_GPL(cxgbi_conn_tx_open);
1529
1530/*
1531 * pdu receive, interact with libiscsi_tcp
1532 */
1533static inline int read_pdu_skb(struct iscsi_conn *conn,
1534 struct sk_buff *skb,
1535 unsigned int offset,
1536 int offloaded)
1537{
1538 int status = 0;
1539 int bytes_read;
1540
1541 bytes_read = iscsi_tcp_recv_skb(conn, skb, offset, offloaded, &status);
1542 switch (status) {
1543 case ISCSI_TCP_CONN_ERR:
1544 pr_info("skb 0x%p, off %u, %d, TCP_ERR.\n",
1545 skb, offset, offloaded);
1546 return -EIO;
1547 case ISCSI_TCP_SUSPENDED:
1548 log_debug(1 << CXGBI_DBG_PDU_RX,
1549 "skb 0x%p, off %u, %d, TCP_SUSPEND, rc %d.\n",
1550 skb, offset, offloaded, bytes_read);
1551 /* no transfer - just have caller flush queue */
1552 return bytes_read;
1553 case ISCSI_TCP_SKB_DONE:
1554 pr_info("skb 0x%p, off %u, %d, TCP_SKB_DONE.\n",
1555 skb, offset, offloaded);
1556 /*
1557 * pdus should always fit in the skb and we should get
1558 * segment done notifcation.
1559 */
1560 iscsi_conn_printk(KERN_ERR, conn, "Invalid pdu or skb.");
1561 return -EFAULT;
1562 case ISCSI_TCP_SEGMENT_DONE:
1563 log_debug(1 << CXGBI_DBG_PDU_RX,
1564 "skb 0x%p, off %u, %d, TCP_SEG_DONE, rc %d.\n",
1565 skb, offset, offloaded, bytes_read);
1566 return bytes_read;
1567 default:
1568 pr_info("skb 0x%p, off %u, %d, invalid status %d.\n",
1569 skb, offset, offloaded, status);
1570 return -EINVAL;
1571 }
1572}
1573
Varun Prakashe0f8e8c2017-05-27 20:22:51 +05301574static int
1575skb_read_pdu_bhs(struct cxgbi_sock *csk, struct iscsi_conn *conn,
1576 struct sk_buff *skb)
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001577{
1578 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
Varun Prakashe0f8e8c2017-05-27 20:22:51 +05301579 int err;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001580
1581 log_debug(1 << CXGBI_DBG_PDU_RX,
1582 "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
1583 conn, skb, skb->len, cxgbi_skcb_flags(skb));
1584
1585 if (!iscsi_tcp_recv_segment_is_hdr(tcp_conn)) {
1586 pr_info("conn 0x%p, skb 0x%p, not hdr.\n", conn, skb);
1587 iscsi_conn_failure(conn, ISCSI_ERR_PROTO);
1588 return -EIO;
1589 }
1590
1591 if (conn->hdrdgst_en &&
1592 cxgbi_skcb_test_flag(skb, SKCBF_RX_HCRC_ERR)) {
1593 pr_info("conn 0x%p, skb 0x%p, hcrc.\n", conn, skb);
1594 iscsi_conn_failure(conn, ISCSI_ERR_HDR_DGST);
1595 return -EIO;
1596 }
1597
Varun Prakash44830d82016-12-01 20:28:29 +05301598 if (cxgbi_skcb_test_flag(skb, SKCBF_RX_ISCSI_COMPL) &&
1599 cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA_DDPD)) {
1600 /* If completion flag is set and data is directly
1601 * placed in to the host memory then update
1602 * task->exp_datasn to the datasn in completion
1603 * iSCSI hdr as T6 adapter generates completion only
1604 * for the last pdu of a sequence.
1605 */
1606 itt_t itt = ((struct iscsi_data *)skb->data)->itt;
1607 struct iscsi_task *task = iscsi_itt_to_ctask(conn, itt);
1608 u32 data_sn = be32_to_cpu(((struct iscsi_data *)
1609 skb->data)->datasn);
1610 if (task && task->sc) {
1611 struct iscsi_tcp_task *tcp_task = task->dd_data;
1612
1613 tcp_task->exp_datasn = data_sn;
1614 }
1615 }
1616
Varun Prakashe0f8e8c2017-05-27 20:22:51 +05301617 err = read_pdu_skb(conn, skb, 0, 0);
1618 if (likely(err >= 0)) {
1619 struct iscsi_hdr *hdr = (struct iscsi_hdr *)skb->data;
1620 u8 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
1621
1622 if (unlikely(opcode == ISCSI_OP_LOGOUT_RSP))
1623 cxgbi_sock_set_flag(csk, CTPF_LOGOUT_RSP_RCVD);
1624 }
1625
1626 return err;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001627}
1628
1629static int skb_read_pdu_data(struct iscsi_conn *conn, struct sk_buff *lskb,
1630 struct sk_buff *skb, unsigned int offset)
1631{
1632 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1633 bool offloaded = 0;
1634 int opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
1635
1636 log_debug(1 << CXGBI_DBG_PDU_RX,
1637 "conn 0x%p, skb 0x%p, len %u, flag 0x%lx.\n",
1638 conn, skb, skb->len, cxgbi_skcb_flags(skb));
1639
1640 if (conn->datadgst_en &&
1641 cxgbi_skcb_test_flag(lskb, SKCBF_RX_DCRC_ERR)) {
1642 pr_info("conn 0x%p, skb 0x%p, dcrc 0x%lx.\n",
1643 conn, lskb, cxgbi_skcb_flags(lskb));
1644 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
1645 return -EIO;
1646 }
1647
1648 if (iscsi_tcp_recv_segment_is_hdr(tcp_conn))
1649 return 0;
1650
1651 /* coalesced, add header digest length */
1652 if (lskb == skb && conn->hdrdgst_en)
1653 offset += ISCSI_DIGEST_SIZE;
1654
1655 if (cxgbi_skcb_test_flag(lskb, SKCBF_RX_DATA_DDPD))
1656 offloaded = 1;
1657
1658 if (opcode == ISCSI_OP_SCSI_DATA_IN)
1659 log_debug(1 << CXGBI_DBG_PDU_RX,
1660 "skb 0x%p, op 0x%x, itt 0x%x, %u %s ddp'ed.\n",
1661 skb, opcode, ntohl(tcp_conn->in.hdr->itt),
1662 tcp_conn->in.datalen, offloaded ? "is" : "not");
1663
1664 return read_pdu_skb(conn, skb, offset, offloaded);
1665}
1666
1667static void csk_return_rx_credits(struct cxgbi_sock *csk, int copied)
1668{
1669 struct cxgbi_device *cdev = csk->cdev;
1670 int must_send;
1671 u32 credits;
1672
1673 log_debug(1 << CXGBI_DBG_PDU_RX,
Hans Wennborgfc385042014-08-05 21:43:29 -07001674 "csk 0x%p,%u,0x%lx,%u, seq %u, wup %u, thre %u, %u.\n",
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001675 csk, csk->state, csk->flags, csk->tid, csk->copied_seq,
1676 csk->rcv_wup, cdev->rx_credit_thres,
Karen Xie81daf102015-04-10 13:57:12 -07001677 csk->rcv_win);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001678
Varun Prakash586be7c2016-12-01 20:28:28 +05301679 if (!cdev->rx_credit_thres)
1680 return;
1681
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001682 if (csk->state != CTP_ESTABLISHED)
1683 return;
1684
1685 credits = csk->copied_seq - csk->rcv_wup;
1686 if (unlikely(!credits))
1687 return;
Karen Xie81daf102015-04-10 13:57:12 -07001688 must_send = credits + 16384 >= csk->rcv_win;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001689 if (must_send || credits >= cdev->rx_credit_thres)
1690 csk->rcv_wup += cdev->csk_send_rx_credits(csk, credits);
1691}
1692
1693void cxgbi_conn_pdu_ready(struct cxgbi_sock *csk)
1694{
1695 struct cxgbi_device *cdev = csk->cdev;
1696 struct iscsi_conn *conn = csk->user_data;
1697 struct sk_buff *skb;
1698 unsigned int read = 0;
1699 int err = 0;
1700
1701 log_debug(1 << CXGBI_DBG_PDU_RX,
1702 "csk 0x%p, conn 0x%p.\n", csk, conn);
1703
1704 if (unlikely(!conn || conn->suspend_rx)) {
1705 log_debug(1 << CXGBI_DBG_PDU_RX,
1706 "csk 0x%p, conn 0x%p, id %d, suspend_rx %lu!\n",
1707 csk, conn, conn ? conn->id : 0xFF,
1708 conn ? conn->suspend_rx : 0xFF);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001709 return;
1710 }
1711
1712 while (!err) {
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001713 skb = skb_peek(&csk->receive_queue);
1714 if (!skb ||
1715 !(cxgbi_skcb_test_flag(skb, SKCBF_RX_STATUS))) {
1716 if (skb)
1717 log_debug(1 << CXGBI_DBG_PDU_RX,
1718 "skb 0x%p, NOT ready 0x%lx.\n",
1719 skb, cxgbi_skcb_flags(skb));
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001720 break;
1721 }
1722 __skb_unlink(skb, &csk->receive_queue);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001723
1724 read += cxgbi_skcb_rx_pdulen(skb);
1725 log_debug(1 << CXGBI_DBG_PDU_RX,
1726 "csk 0x%p, skb 0x%p,%u,f 0x%lx, pdu len %u.\n",
1727 csk, skb, skb->len, cxgbi_skcb_flags(skb),
1728 cxgbi_skcb_rx_pdulen(skb));
1729
1730 if (cxgbi_skcb_test_flag(skb, SKCBF_RX_COALESCED)) {
Varun Prakashe0f8e8c2017-05-27 20:22:51 +05301731 err = skb_read_pdu_bhs(csk, conn, skb);
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001732 if (err < 0) {
1733 pr_err("coalesced bhs, csk 0x%p, skb 0x%p,%u, "
1734 "f 0x%lx, plen %u.\n",
1735 csk, skb, skb->len,
1736 cxgbi_skcb_flags(skb),
1737 cxgbi_skcb_rx_pdulen(skb));
1738 goto skb_done;
1739 }
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001740 err = skb_read_pdu_data(conn, skb, skb,
1741 err + cdev->skb_rx_extra);
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001742 if (err < 0)
1743 pr_err("coalesced data, csk 0x%p, skb 0x%p,%u, "
1744 "f 0x%lx, plen %u.\n",
1745 csk, skb, skb->len,
1746 cxgbi_skcb_flags(skb),
1747 cxgbi_skcb_rx_pdulen(skb));
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001748 } else {
Varun Prakashe0f8e8c2017-05-27 20:22:51 +05301749 err = skb_read_pdu_bhs(csk, conn, skb);
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001750 if (err < 0) {
1751 pr_err("bhs, 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 goto skb_done;
1757 }
1758
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001759 if (cxgbi_skcb_test_flag(skb, SKCBF_RX_DATA)) {
1760 struct sk_buff *dskb;
1761
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001762 dskb = skb_peek(&csk->receive_queue);
1763 if (!dskb) {
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001764 pr_err("csk 0x%p, skb 0x%p,%u, f 0x%lx,"
1765 " plen %u, NO data.\n",
1766 csk, skb, skb->len,
1767 cxgbi_skcb_flags(skb),
1768 cxgbi_skcb_rx_pdulen(skb));
1769 err = -EIO;
1770 goto skb_done;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001771 }
1772 __skb_unlink(dskb, &csk->receive_queue);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001773
1774 err = skb_read_pdu_data(conn, skb, dskb, 0);
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001775 if (err < 0)
1776 pr_err("data, csk 0x%p, skb 0x%p,%u, "
1777 "f 0x%lx, plen %u, dskb 0x%p,"
1778 "%u.\n",
1779 csk, skb, skb->len,
1780 cxgbi_skcb_flags(skb),
1781 cxgbi_skcb_rx_pdulen(skb),
1782 dskb, dskb->len);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001783 __kfree_skb(dskb);
1784 } else
1785 err = skb_read_pdu_data(conn, skb, skb, 0);
1786 }
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001787skb_done:
1788 __kfree_skb(skb);
1789
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001790 if (err < 0)
1791 break;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001792 }
1793
1794 log_debug(1 << CXGBI_DBG_PDU_RX, "csk 0x%p, read %u.\n", csk, read);
1795 if (read) {
1796 csk->copied_seq += read;
1797 csk_return_rx_credits(csk, read);
1798 conn->rxdata_octets += read;
1799 }
1800
1801 if (err < 0) {
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001802 pr_info("csk 0x%p, 0x%p, rx failed %d, read %u.\n",
1803 csk, conn, err, read);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001804 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1805 }
1806}
1807EXPORT_SYMBOL_GPL(cxgbi_conn_pdu_ready);
1808
1809static int sgl_seek_offset(struct scatterlist *sgl, unsigned int sgcnt,
1810 unsigned int offset, unsigned int *off,
1811 struct scatterlist **sgp)
1812{
1813 int i;
1814 struct scatterlist *sg;
1815
1816 for_each_sg(sgl, sg, sgcnt, i) {
1817 if (offset < sg->length) {
1818 *off = offset;
1819 *sgp = sg;
1820 return 0;
1821 }
1822 offset -= sg->length;
1823 }
1824 return -EFAULT;
1825}
1826
1827static int sgl_read_to_frags(struct scatterlist *sg, unsigned int sgoffset,
Ian Campbell6a39a162011-10-19 23:01:48 +00001828 unsigned int dlen, struct page_frag *frags,
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001829 int frag_max)
1830{
1831 unsigned int datalen = dlen;
1832 unsigned int sglen = sg->length - sgoffset;
1833 struct page *page = sg_page(sg);
1834 int i;
1835
1836 i = 0;
1837 do {
1838 unsigned int copy;
1839
1840 if (!sglen) {
1841 sg = sg_next(sg);
1842 if (!sg) {
1843 pr_warn("sg %d NULL, len %u/%u.\n",
1844 i, datalen, dlen);
1845 return -EINVAL;
1846 }
1847 sgoffset = 0;
1848 sglen = sg->length;
1849 page = sg_page(sg);
1850
1851 }
1852 copy = min(datalen, sglen);
1853 if (i && page == frags[i - 1].page &&
1854 sgoffset + sg->offset ==
Ian Campbell6a39a162011-10-19 23:01:48 +00001855 frags[i - 1].offset + frags[i - 1].size) {
1856 frags[i - 1].size += copy;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001857 } else {
1858 if (i >= frag_max) {
1859 pr_warn("too many pages %u, dlen %u.\n",
1860 frag_max, dlen);
1861 return -EINVAL;
1862 }
1863
1864 frags[i].page = page;
Ian Campbell6a39a162011-10-19 23:01:48 +00001865 frags[i].offset = sg->offset + sgoffset;
1866 frags[i].size = copy;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001867 i++;
1868 }
1869 datalen -= copy;
1870 sgoffset += copy;
1871 sglen -= copy;
1872 } while (datalen);
1873
1874 return i;
1875}
1876
1877int cxgbi_conn_alloc_pdu(struct iscsi_task *task, u8 opcode)
1878{
1879 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
1880 struct cxgbi_conn *cconn = tcp_conn->dd_data;
1881 struct cxgbi_device *cdev = cconn->chba->cdev;
1882 struct iscsi_conn *conn = task->conn;
1883 struct iscsi_tcp_task *tcp_task = task->dd_data;
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001884 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001885 struct scsi_cmnd *sc = task->sc;
1886 int headroom = SKB_TX_ISCSI_PDU_HEADER_MAX;
1887
1888 tcp_task->dd_data = tdata;
1889 task->hdr = NULL;
1890
Varun Prakash75b61252017-05-16 19:23:44 +05301891 if (tdata->skb) {
1892 kfree_skb(tdata->skb);
1893 tdata->skb = NULL;
1894 }
1895
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001896 if (SKB_MAX_HEAD(cdev->skb_tx_rsvd) > (512 * MAX_SKB_FRAGS) &&
1897 (opcode == ISCSI_OP_SCSI_DATA_OUT ||
1898 (opcode == ISCSI_OP_SCSI_CMD &&
1899 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_TO_DEVICE))))
1900 /* data could goes into skb head */
1901 headroom += min_t(unsigned int,
1902 SKB_MAX_HEAD(cdev->skb_tx_rsvd),
1903 conn->max_xmit_dlength);
1904
1905 tdata->skb = alloc_skb(cdev->skb_tx_rsvd + headroom, GFP_ATOMIC);
1906 if (!tdata->skb) {
Thadeu Lima de Souza Cascardo00c4a092011-12-14 13:46:23 -02001907 struct cxgbi_sock *csk = cconn->cep->csk;
1908 struct net_device *ndev = cdev->ports[csk->port_id];
1909 ndev->stats.tx_dropped++;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001910 return -ENOMEM;
1911 }
1912
Varun Prakash75b61252017-05-16 19:23:44 +05301913 skb_get(tdata->skb);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001914 skb_reserve(tdata->skb, cdev->skb_tx_rsvd);
1915 task->hdr = (struct iscsi_hdr *)tdata->skb->data;
1916 task->hdr_max = SKB_TX_ISCSI_PDU_HEADER_MAX; /* BHS + AHS */
1917
1918 /* data_out uses scsi_cmd's itt */
1919 if (opcode != ISCSI_OP_SCSI_DATA_OUT)
1920 task_reserve_itt(task, &task->hdr->itt);
1921
1922 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1923 "task 0x%p, op 0x%x, skb 0x%p,%u+%u/%u, itt 0x%x.\n",
1924 task, opcode, tdata->skb, cdev->skb_tx_rsvd, headroom,
1925 conn->max_xmit_dlength, ntohl(task->hdr->itt));
1926
1927 return 0;
1928}
1929EXPORT_SYMBOL_GPL(cxgbi_conn_alloc_pdu);
1930
1931static inline void tx_skb_setmode(struct sk_buff *skb, int hcrc, int dcrc)
1932{
kxie@chelsio.comc343a012011-01-07 14:45:39 -08001933 if (hcrc || dcrc) {
1934 u8 submode = 0;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001935
kxie@chelsio.comc343a012011-01-07 14:45:39 -08001936 if (hcrc)
1937 submode |= 1;
1938 if (dcrc)
1939 submode |= 2;
1940 cxgbi_skcb_ulp_mode(skb) = (ULP2_MODE_ISCSI << 4) | submode;
1941 } else
1942 cxgbi_skcb_ulp_mode(skb) = 0;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001943}
1944
1945int cxgbi_conn_init_pdu(struct iscsi_task *task, unsigned int offset,
1946 unsigned int count)
1947{
1948 struct iscsi_conn *conn = task->conn;
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07001949 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001950 struct sk_buff *skb = tdata->skb;
1951 unsigned int datalen = count;
1952 int i, padlen = iscsi_padding(count);
1953 struct page *pg;
1954
1955 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
1956 "task 0x%p,0x%p, skb 0x%p, 0x%x,0x%x,0x%x, %u+%u.\n",
1957 task, task->sc, skb, (*skb->data) & ISCSI_OPCODE_MASK,
1958 ntohl(task->cmdsn), ntohl(task->hdr->itt), offset, count);
1959
1960 skb_put(skb, task->hdr_len);
1961 tx_skb_setmode(skb, conn->hdrdgst_en, datalen ? conn->datadgst_en : 0);
1962 if (!count)
1963 return 0;
1964
1965 if (task->sc) {
1966 struct scsi_data_buffer *sdb = scsi_out(task->sc);
1967 struct scatterlist *sg = NULL;
1968 int err;
1969
1970 tdata->offset = offset;
1971 tdata->count = count;
1972 err = sgl_seek_offset(
1973 sdb->table.sgl, sdb->table.nents,
1974 tdata->offset, &tdata->sgoffset, &sg);
1975 if (err < 0) {
1976 pr_warn("tpdu, sgl %u, bad offset %u/%u.\n",
1977 sdb->table.nents, tdata->offset, sdb->length);
1978 return err;
1979 }
1980 err = sgl_read_to_frags(sg, tdata->sgoffset, tdata->count,
1981 tdata->frags, MAX_PDU_FRAGS);
1982 if (err < 0) {
1983 pr_warn("tpdu, sgl %u, bad offset %u + %u.\n",
1984 sdb->table.nents, tdata->offset, tdata->count);
1985 return err;
1986 }
1987 tdata->nr_frags = err;
1988
1989 if (tdata->nr_frags > MAX_SKB_FRAGS ||
1990 (padlen && tdata->nr_frags == MAX_SKB_FRAGS)) {
1991 char *dst = skb->data + task->hdr_len;
Ian Campbell6a39a162011-10-19 23:01:48 +00001992 struct page_frag *frag = tdata->frags;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001993
1994 /* data fits in the skb's headroom */
1995 for (i = 0; i < tdata->nr_frags; i++, frag++) {
Cong Wang77dfce02011-11-25 23:14:23 +08001996 char *src = kmap_atomic(frag->page);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07001997
Ian Campbell6a39a162011-10-19 23:01:48 +00001998 memcpy(dst, src+frag->offset, frag->size);
1999 dst += frag->size;
Cong Wang77dfce02011-11-25 23:14:23 +08002000 kunmap_atomic(src);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002001 }
2002 if (padlen) {
2003 memset(dst, 0, padlen);
2004 padlen = 0;
2005 }
2006 skb_put(skb, count + padlen);
2007 } else {
2008 /* data fit into frag_list */
Ian Campbell6a39a162011-10-19 23:01:48 +00002009 for (i = 0; i < tdata->nr_frags; i++) {
2010 __skb_fill_page_desc(skb, i,
2011 tdata->frags[i].page,
2012 tdata->frags[i].offset,
2013 tdata->frags[i].size);
2014 skb_frag_ref(skb, i);
2015 }
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002016 skb_shinfo(skb)->nr_frags = tdata->nr_frags;
2017 skb->len += count;
2018 skb->data_len += count;
2019 skb->truesize += count;
2020 }
2021
2022 } else {
2023 pg = virt_to_page(task->data);
2024
2025 get_page(pg);
2026 skb_fill_page_desc(skb, 0, pg, offset_in_page(task->data),
2027 count);
2028 skb->len += count;
2029 skb->data_len += count;
2030 skb->truesize += count;
2031 }
2032
2033 if (padlen) {
2034 i = skb_shinfo(skb)->nr_frags;
2035 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
2036 virt_to_page(padding), offset_in_page(padding),
2037 padlen);
2038
2039 skb->data_len += padlen;
2040 skb->truesize += padlen;
2041 skb->len += padlen;
2042 }
2043
2044 return 0;
2045}
2046EXPORT_SYMBOL_GPL(cxgbi_conn_init_pdu);
2047
2048int cxgbi_conn_xmit_pdu(struct iscsi_task *task)
2049{
2050 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
2051 struct cxgbi_conn *cconn = tcp_conn->dd_data;
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07002052 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
Varun Prakash71f7a002016-07-21 22:57:16 +05302053 struct cxgbi_task_tag_info *ttinfo = &tdata->ttinfo;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002054 struct sk_buff *skb = tdata->skb;
Varun Prakash71f7a002016-07-21 22:57:16 +05302055 struct cxgbi_sock *csk = NULL;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002056 unsigned int datalen;
2057 int err;
2058
Varun Prakash75b61252017-05-16 19:23:44 +05302059 if (!skb || cxgbi_skcb_test_flag(skb, SKCBF_TX_DONE)) {
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002060 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
Varun Prakash75b61252017-05-16 19:23:44 +05302061 "task 0x%p, skb 0x%p\n", task, skb);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002062 return 0;
2063 }
2064
Varun Prakash71f7a002016-07-21 22:57:16 +05302065 if (cconn && cconn->cep)
2066 csk = cconn->cep->csk;
2067 if (!csk) {
2068 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2069 "task 0x%p, csk gone.\n", task);
2070 return -EPIPE;
2071 }
2072
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002073 datalen = skb->data_len;
Varun Prakash71f7a002016-07-21 22:57:16 +05302074
2075 /* write ppod first if using ofldq to write ppod */
2076 if (ttinfo->flags & CXGBI_PPOD_INFO_FLAG_VALID) {
2077 struct cxgbi_ppm *ppm = csk->cdev->cdev2ppm(csk->cdev);
2078
2079 ttinfo->flags &= ~CXGBI_PPOD_INFO_FLAG_VALID;
2080 if (csk->cdev->csk_ddp_set_map(ppm, csk, ttinfo) < 0)
2081 pr_err("task 0x%p, ppod writing using ofldq failed.\n",
2082 task);
2083 /* continue. Let fl get the data */
2084 }
2085
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002086 err = cxgbi_sock_send_pdus(cconn->cep->csk, skb);
2087 if (err > 0) {
2088 int pdulen = err;
2089
2090 log_debug(1 << CXGBI_DBG_PDU_TX,
2091 "task 0x%p,0x%p, skb 0x%p, len %u/%u, rv %d.\n",
2092 task, task->sc, skb, skb->len, skb->data_len, err);
2093
2094 if (task->conn->hdrdgst_en)
2095 pdulen += ISCSI_DIGEST_SIZE;
2096
2097 if (datalen && task->conn->datadgst_en)
2098 pdulen += ISCSI_DIGEST_SIZE;
2099
2100 task->conn->txdata_octets += pdulen;
Varun Prakash75b61252017-05-16 19:23:44 +05302101 cxgbi_skcb_set_flag(skb, SKCBF_TX_DONE);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002102 return 0;
2103 }
2104
2105 if (err == -EAGAIN || err == -ENOBUFS) {
2106 log_debug(1 << CXGBI_DBG_PDU_TX,
2107 "task 0x%p, skb 0x%p, len %u/%u, %d EAGAIN.\n",
2108 task, skb, skb->len, skb->data_len, err);
2109 /* reset skb to send when we are called again */
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002110 return err;
2111 }
2112
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002113 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_PDU_TX,
2114 "itt 0x%x, skb 0x%p, len %u/%u, xmit err %d.\n",
2115 task->itt, skb, skb->len, skb->data_len, err);
Karen Xieed481a32014-12-11 19:13:47 -08002116
Varun Prakash75b61252017-05-16 19:23:44 +05302117 __kfree_skb(tdata->skb);
2118 tdata->skb = NULL;
Karen Xieed481a32014-12-11 19:13:47 -08002119
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002120 iscsi_conn_printk(KERN_ERR, task->conn, "xmit err %d.\n", err);
2121 iscsi_conn_failure(task->conn, ISCSI_ERR_XMIT_FAILED);
2122 return err;
2123}
2124EXPORT_SYMBOL_GPL(cxgbi_conn_xmit_pdu);
2125
2126void cxgbi_cleanup_task(struct iscsi_task *task)
2127{
Varun Prakash71f7a002016-07-21 22:57:16 +05302128 struct iscsi_tcp_task *tcp_task = task->dd_data;
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07002129 struct cxgbi_task_data *tdata = iscsi_task_cxgbi_data(task);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002130
Varun Prakash50292712017-07-23 20:01:32 +05302131 if (!tcp_task || !tdata || (tcp_task->dd_data != tdata)) {
2132 pr_info("task 0x%p,0x%p, tcp_task 0x%p, tdata 0x%p/0x%p.\n",
2133 task, task->sc, tcp_task,
2134 tcp_task ? tcp_task->dd_data : NULL, tdata);
2135 return;
2136 }
2137
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002138 log_debug(1 << CXGBI_DBG_ISCSI,
2139 "task 0x%p, skb 0x%p, itt 0x%x.\n",
2140 task, tdata->skb, task->hdr_itt);
2141
Varun Prakash71f7a002016-07-21 22:57:16 +05302142 tcp_task->dd_data = NULL;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002143 /* never reached the xmit task callout */
Varun Prakash75b61252017-05-16 19:23:44 +05302144 if (tdata->skb) {
2145 kfree_skb(tdata->skb);
2146 tdata->skb = NULL;
2147 }
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002148
2149 task_release_itt(task, task->hdr_itt);
Varun Prakash69e2d1e2016-11-05 21:49:28 +05302150 memset(tdata, 0, sizeof(*tdata));
2151
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002152 iscsi_tcp_cleanup_task(task);
2153}
2154EXPORT_SYMBOL_GPL(cxgbi_cleanup_task);
2155
2156void cxgbi_get_conn_stats(struct iscsi_cls_conn *cls_conn,
2157 struct iscsi_stats *stats)
2158{
2159 struct iscsi_conn *conn = cls_conn->dd_data;
2160
2161 stats->txdata_octets = conn->txdata_octets;
2162 stats->rxdata_octets = conn->rxdata_octets;
2163 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2164 stats->dataout_pdus = conn->dataout_pdus_cnt;
2165 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2166 stats->datain_pdus = conn->datain_pdus_cnt;
2167 stats->r2t_pdus = conn->r2t_pdus_cnt;
2168 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2169 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2170 stats->digest_err = 0;
2171 stats->timeout_err = 0;
2172 stats->custom_length = 1;
2173 strcpy(stats->custom[0].desc, "eh_abort_cnt");
2174 stats->custom[0].value = conn->eh_abort_cnt;
2175}
2176EXPORT_SYMBOL_GPL(cxgbi_get_conn_stats);
2177
2178static int cxgbi_conn_max_xmit_dlength(struct iscsi_conn *conn)
2179{
2180 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2181 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2182 struct cxgbi_device *cdev = cconn->chba->cdev;
2183 unsigned int headroom = SKB_MAX_HEAD(cdev->skb_tx_rsvd);
2184 unsigned int max_def = 512 * MAX_SKB_FRAGS;
2185 unsigned int max = max(max_def, headroom);
2186
2187 max = min(cconn->chba->cdev->tx_max_size, max);
2188 if (conn->max_xmit_dlength)
2189 conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
2190 else
2191 conn->max_xmit_dlength = max;
2192 cxgbi_align_pdu_size(conn->max_xmit_dlength);
2193
2194 return 0;
2195}
2196
2197static int cxgbi_conn_max_recv_dlength(struct iscsi_conn *conn)
2198{
2199 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2200 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2201 unsigned int max = cconn->chba->cdev->rx_max_size;
2202
2203 cxgbi_align_pdu_size(max);
2204
2205 if (conn->max_recv_dlength) {
2206 if (conn->max_recv_dlength > max) {
2207 pr_err("MaxRecvDataSegmentLength %u > %u.\n",
2208 conn->max_recv_dlength, max);
2209 return -EINVAL;
2210 }
2211 conn->max_recv_dlength = min(conn->max_recv_dlength, max);
2212 cxgbi_align_pdu_size(conn->max_recv_dlength);
2213 } else
2214 conn->max_recv_dlength = max;
2215
2216 return 0;
2217}
2218
2219int cxgbi_set_conn_param(struct iscsi_cls_conn *cls_conn,
2220 enum iscsi_param param, char *buf, int buflen)
2221{
2222 struct iscsi_conn *conn = cls_conn->dd_data;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002223 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2224 struct cxgbi_conn *cconn = tcp_conn->dd_data;
2225 struct cxgbi_sock *csk = cconn->cep->csk;
Mike Christie1304be52012-01-26 21:13:10 -06002226 int err;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002227
2228 log_debug(1 << CXGBI_DBG_ISCSI,
2229 "cls_conn 0x%p, param %d, buf(%d) %s.\n",
2230 cls_conn, param, buflen, buf);
2231
2232 switch (param) {
2233 case ISCSI_PARAM_HDRDGST_EN:
2234 err = iscsi_set_param(cls_conn, param, buf, buflen);
2235 if (!err && conn->hdrdgst_en)
2236 err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
2237 conn->hdrdgst_en,
2238 conn->datadgst_en, 0);
2239 break;
2240 case ISCSI_PARAM_DATADGST_EN:
2241 err = iscsi_set_param(cls_conn, param, buf, buflen);
2242 if (!err && conn->datadgst_en)
2243 err = csk->cdev->csk_ddp_setup_digest(csk, csk->tid,
2244 conn->hdrdgst_en,
2245 conn->datadgst_en, 0);
2246 break;
2247 case ISCSI_PARAM_MAX_R2T:
Mike Christie1304be52012-01-26 21:13:10 -06002248 return iscsi_tcp_set_max_r2t(conn, buf);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002249 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2250 err = iscsi_set_param(cls_conn, param, buf, buflen);
2251 if (!err)
2252 err = cxgbi_conn_max_recv_dlength(conn);
2253 break;
2254 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2255 err = iscsi_set_param(cls_conn, param, buf, buflen);
2256 if (!err)
2257 err = cxgbi_conn_max_xmit_dlength(conn);
2258 break;
2259 default:
2260 return iscsi_set_param(cls_conn, param, buf, buflen);
2261 }
2262 return err;
2263}
2264EXPORT_SYMBOL_GPL(cxgbi_set_conn_param);
2265
Anish Bhattfc8d0592014-07-17 00:18:17 -07002266static inline int csk_print_port(struct cxgbi_sock *csk, char *buf)
2267{
2268 int len;
2269
2270 cxgbi_sock_get(csk);
2271 len = sprintf(buf, "%hu\n", ntohs(csk->daddr.sin_port));
2272 cxgbi_sock_put(csk);
2273
2274 return len;
2275}
2276
2277static inline int csk_print_ip(struct cxgbi_sock *csk, char *buf)
2278{
2279 int len;
2280
2281 cxgbi_sock_get(csk);
2282 if (csk->csk_family == AF_INET)
2283 len = sprintf(buf, "%pI4",
2284 &csk->daddr.sin_addr.s_addr);
2285 else
2286 len = sprintf(buf, "%pI6",
2287 &csk->daddr6.sin6_addr);
2288
2289 cxgbi_sock_put(csk);
2290
2291 return len;
2292}
2293
Mike Christiec71b9b62011-02-16 15:04:38 -06002294int cxgbi_get_ep_param(struct iscsi_endpoint *ep, enum iscsi_param param,
2295 char *buf)
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002296{
Mike Christiec71b9b62011-02-16 15:04:38 -06002297 struct cxgbi_endpoint *cep = ep->dd_data;
2298 struct cxgbi_sock *csk;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002299 int len;
2300
2301 log_debug(1 << CXGBI_DBG_ISCSI,
Mike Christiec71b9b62011-02-16 15:04:38 -06002302 "cls_conn 0x%p, param %d.\n", ep, param);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002303
2304 switch (param) {
2305 case ISCSI_PARAM_CONN_PORT:
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002306 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christiec71b9b62011-02-16 15:04:38 -06002307 if (!cep)
2308 return -ENOTCONN;
2309
2310 csk = cep->csk;
2311 if (!csk)
2312 return -ENOTCONN;
2313
2314 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
2315 &csk->daddr, param, buf);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002316 default:
Mike Christiec71b9b62011-02-16 15:04:38 -06002317 return -ENOSYS;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002318 }
2319 return len;
2320}
Mike Christiec71b9b62011-02-16 15:04:38 -06002321EXPORT_SYMBOL_GPL(cxgbi_get_ep_param);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002322
2323struct iscsi_cls_conn *
2324cxgbi_create_conn(struct iscsi_cls_session *cls_session, u32 cid)
2325{
2326 struct iscsi_cls_conn *cls_conn;
2327 struct iscsi_conn *conn;
2328 struct iscsi_tcp_conn *tcp_conn;
2329 struct cxgbi_conn *cconn;
2330
2331 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
2332 if (!cls_conn)
2333 return NULL;
2334
2335 conn = cls_conn->dd_data;
2336 tcp_conn = conn->dd_data;
2337 cconn = tcp_conn->dd_data;
2338 cconn->iconn = conn;
2339
2340 log_debug(1 << CXGBI_DBG_ISCSI,
2341 "cid %u(0x%x), cls 0x%p,0x%p, conn 0x%p,0x%p,0x%p.\n",
2342 cid, cid, cls_session, cls_conn, conn, tcp_conn, cconn);
2343
2344 return cls_conn;
2345}
2346EXPORT_SYMBOL_GPL(cxgbi_create_conn);
2347
2348int cxgbi_bind_conn(struct iscsi_cls_session *cls_session,
2349 struct iscsi_cls_conn *cls_conn,
2350 u64 transport_eph, int is_leading)
2351{
2352 struct iscsi_conn *conn = cls_conn->dd_data;
2353 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2354 struct cxgbi_conn *cconn = tcp_conn->dd_data;
Varun Prakash71f7a002016-07-21 22:57:16 +05302355 struct cxgbi_ppm *ppm;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002356 struct iscsi_endpoint *ep;
2357 struct cxgbi_endpoint *cep;
2358 struct cxgbi_sock *csk;
2359 int err;
2360
2361 ep = iscsi_lookup_endpoint(transport_eph);
2362 if (!ep)
2363 return -EINVAL;
2364
2365 /* setup ddp pagesize */
2366 cep = ep->dd_data;
2367 csk = cep->csk;
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002368
Varun Prakash71f7a002016-07-21 22:57:16 +05302369 ppm = csk->cdev->cdev2ppm(csk->cdev);
2370 err = csk->cdev->csk_ddp_setup_pgidx(csk, csk->tid,
2371 ppm->tformat.pgsz_idx_dflt, 0);
2372 if (err < 0)
2373 return err;
2374
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002375 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
2376 if (err)
2377 return -EINVAL;
2378
2379 /* calculate the tag idx bits needed for this conn based on cmds_max */
2380 cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
2381
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07002382 write_lock_bh(&csk->callback_lock);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002383 csk->user_data = conn;
2384 cconn->chba = cep->chba;
2385 cconn->cep = cep;
2386 cep->cconn = cconn;
kxie@chelsio.come3d2ad82010-09-23 16:43:23 -07002387 write_unlock_bh(&csk->callback_lock);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002388
2389 cxgbi_conn_max_xmit_dlength(conn);
2390 cxgbi_conn_max_recv_dlength(conn);
2391
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002392 log_debug(1 << CXGBI_DBG_ISCSI,
2393 "cls 0x%p,0x%p, ep 0x%p, cconn 0x%p, csk 0x%p.\n",
2394 cls_session, cls_conn, ep, cconn, csk);
2395 /* init recv engine */
2396 iscsi_tcp_hdr_recv_prep(tcp_conn);
2397
2398 return 0;
2399}
2400EXPORT_SYMBOL_GPL(cxgbi_bind_conn);
2401
2402struct iscsi_cls_session *cxgbi_create_session(struct iscsi_endpoint *ep,
2403 u16 cmds_max, u16 qdepth,
2404 u32 initial_cmdsn)
2405{
2406 struct cxgbi_endpoint *cep;
2407 struct cxgbi_hba *chba;
2408 struct Scsi_Host *shost;
2409 struct iscsi_cls_session *cls_session;
2410 struct iscsi_session *session;
2411
2412 if (!ep) {
2413 pr_err("missing endpoint.\n");
2414 return NULL;
2415 }
2416
2417 cep = ep->dd_data;
2418 chba = cep->chba;
2419 shost = chba->shost;
2420
2421 BUG_ON(chba != iscsi_host_priv(shost));
2422
2423 cls_session = iscsi_session_setup(chba->cdev->itp, shost,
2424 cmds_max, 0,
2425 sizeof(struct iscsi_tcp_task) +
2426 sizeof(struct cxgbi_task_data),
2427 initial_cmdsn, ISCSI_MAX_TARGET);
2428 if (!cls_session)
2429 return NULL;
2430
2431 session = cls_session->dd_data;
2432 if (iscsi_tcp_r2tpool_alloc(session))
2433 goto remove_session;
2434
2435 log_debug(1 << CXGBI_DBG_ISCSI,
2436 "ep 0x%p, cls sess 0x%p.\n", ep, cls_session);
2437 return cls_session;
2438
2439remove_session:
2440 iscsi_session_teardown(cls_session);
2441 return NULL;
2442}
2443EXPORT_SYMBOL_GPL(cxgbi_create_session);
2444
2445void cxgbi_destroy_session(struct iscsi_cls_session *cls_session)
2446{
2447 log_debug(1 << CXGBI_DBG_ISCSI,
2448 "cls sess 0x%p.\n", cls_session);
2449
2450 iscsi_tcp_r2tpool_free(cls_session->dd_data);
2451 iscsi_session_teardown(cls_session);
2452}
2453EXPORT_SYMBOL_GPL(cxgbi_destroy_session);
2454
2455int cxgbi_set_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2456 char *buf, int buflen)
2457{
2458 struct cxgbi_hba *chba = iscsi_host_priv(shost);
2459
2460 if (!chba->ndev) {
2461 shost_printk(KERN_ERR, shost, "Could not get host param. "
2462 "netdev for host not set.\n");
2463 return -ENODEV;
2464 }
2465
2466 log_debug(1 << CXGBI_DBG_ISCSI,
2467 "shost 0x%p, hba 0x%p,%s, param %d, buf(%d) %s.\n",
2468 shost, chba, chba->ndev->name, param, buflen, buf);
2469
2470 switch (param) {
2471 case ISCSI_HOST_PARAM_IPADDRESS:
2472 {
2473 __be32 addr = in_aton(buf);
2474 log_debug(1 << CXGBI_DBG_ISCSI,
2475 "hba %s, req. ipv4 %pI4.\n", chba->ndev->name, &addr);
2476 cxgbi_set_iscsi_ipv4(chba, addr);
2477 return 0;
2478 }
2479 case ISCSI_HOST_PARAM_HWADDRESS:
2480 case ISCSI_HOST_PARAM_NETDEV_NAME:
2481 return 0;
2482 default:
2483 return iscsi_host_set_param(shost, param, buf, buflen);
2484 }
2485}
2486EXPORT_SYMBOL_GPL(cxgbi_set_host_param);
2487
2488int cxgbi_get_host_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2489 char *buf)
2490{
2491 struct cxgbi_hba *chba = iscsi_host_priv(shost);
2492 int len = 0;
2493
2494 if (!chba->ndev) {
2495 shost_printk(KERN_ERR, shost, "Could not get host param. "
2496 "netdev for host not set.\n");
2497 return -ENODEV;
2498 }
2499
2500 log_debug(1 << CXGBI_DBG_ISCSI,
2501 "shost 0x%p, hba 0x%p,%s, param %d.\n",
2502 shost, chba, chba->ndev->name, param);
2503
2504 switch (param) {
2505 case ISCSI_HOST_PARAM_HWADDRESS:
2506 len = sysfs_format_mac(buf, chba->ndev->dev_addr, 6);
2507 break;
2508 case ISCSI_HOST_PARAM_NETDEV_NAME:
2509 len = sprintf(buf, "%s\n", chba->ndev->name);
2510 break;
2511 case ISCSI_HOST_PARAM_IPADDRESS:
2512 {
Anish Bhattdd9ad672014-10-16 15:59:19 -07002513 struct cxgbi_sock *csk = find_sock_on_port(chba->cdev,
2514 chba->port_id);
2515 if (csk) {
2516 len = sprintf(buf, "%pIS",
2517 (struct sockaddr *)&csk->saddr);
2518 }
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002519 log_debug(1 << CXGBI_DBG_ISCSI,
Anish Bhattdd9ad672014-10-16 15:59:19 -07002520 "hba %s, addr %s.\n", chba->ndev->name, buf);
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002521 break;
2522 }
2523 default:
2524 return iscsi_host_get_param(shost, param, buf);
2525 }
2526
2527 return len;
2528}
2529EXPORT_SYMBOL_GPL(cxgbi_get_host_param);
2530
2531struct iscsi_endpoint *cxgbi_ep_connect(struct Scsi_Host *shost,
2532 struct sockaddr *dst_addr,
2533 int non_blocking)
2534{
2535 struct iscsi_endpoint *ep;
2536 struct cxgbi_endpoint *cep;
2537 struct cxgbi_hba *hba = NULL;
2538 struct cxgbi_sock *csk;
2539 int err = -EINVAL;
2540
2541 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2542 "shost 0x%p, non_blocking %d, dst_addr 0x%p.\n",
2543 shost, non_blocking, dst_addr);
2544
2545 if (shost) {
2546 hba = iscsi_host_priv(shost);
2547 if (!hba) {
2548 pr_info("shost 0x%p, priv NULL.\n", shost);
2549 goto err_out;
2550 }
2551 }
2552
Anish Bhattfc8d0592014-07-17 00:18:17 -07002553 if (dst_addr->sa_family == AF_INET) {
2554 csk = cxgbi_check_route(dst_addr);
Anish Bhatte81fbf62014-07-17 18:34:44 -07002555#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattfc8d0592014-07-17 00:18:17 -07002556 } else if (dst_addr->sa_family == AF_INET6) {
2557 csk = cxgbi_check_route6(dst_addr);
Anish Bhatte81fbf62014-07-17 18:34:44 -07002558#endif
Anish Bhattfc8d0592014-07-17 00:18:17 -07002559 } else {
2560 pr_info("address family 0x%x NOT supported.\n",
2561 dst_addr->sa_family);
2562 err = -EAFNOSUPPORT;
2563 return (struct iscsi_endpoint *)ERR_PTR(err);
2564 }
2565
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002566 if (IS_ERR(csk))
2567 return (struct iscsi_endpoint *)csk;
2568 cxgbi_sock_get(csk);
2569
2570 if (!hba)
2571 hba = csk->cdev->hbas[csk->port_id];
2572 else if (hba != csk->cdev->hbas[csk->port_id]) {
2573 pr_info("Could not connect through requested host %u"
2574 "hba 0x%p != 0x%p (%u).\n",
2575 shost->host_no, hba,
2576 csk->cdev->hbas[csk->port_id], csk->port_id);
2577 err = -ENOSPC;
2578 goto release_conn;
2579 }
2580
2581 err = sock_get_port(csk);
2582 if (err)
2583 goto release_conn;
2584
2585 cxgbi_sock_set_state(csk, CTP_CONNECTING);
2586 err = csk->cdev->csk_init_act_open(csk);
2587 if (err)
2588 goto release_conn;
2589
2590 if (cxgbi_sock_is_closing(csk)) {
2591 err = -ENOSPC;
2592 pr_info("csk 0x%p is closing.\n", csk);
2593 goto release_conn;
2594 }
2595
2596 ep = iscsi_create_endpoint(sizeof(*cep));
2597 if (!ep) {
2598 err = -ENOMEM;
2599 pr_info("iscsi alloc ep, OOM.\n");
2600 goto release_conn;
2601 }
2602
2603 cep = ep->dd_data;
2604 cep->csk = csk;
2605 cep->chba = hba;
2606
2607 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2608 "ep 0x%p, cep 0x%p, csk 0x%p, hba 0x%p,%s.\n",
2609 ep, cep, csk, hba, hba->ndev->name);
2610 return ep;
2611
2612release_conn:
2613 cxgbi_sock_put(csk);
2614 cxgbi_sock_closed(csk);
2615err_out:
2616 return ERR_PTR(err);
2617}
2618EXPORT_SYMBOL_GPL(cxgbi_ep_connect);
2619
2620int cxgbi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
2621{
2622 struct cxgbi_endpoint *cep = ep->dd_data;
2623 struct cxgbi_sock *csk = cep->csk;
2624
2625 if (!cxgbi_sock_is_established(csk))
2626 return 0;
2627 return 1;
2628}
2629EXPORT_SYMBOL_GPL(cxgbi_ep_poll);
2630
2631void cxgbi_ep_disconnect(struct iscsi_endpoint *ep)
2632{
2633 struct cxgbi_endpoint *cep = ep->dd_data;
2634 struct cxgbi_conn *cconn = cep->cconn;
2635 struct cxgbi_sock *csk = cep->csk;
2636
2637 log_debug(1 << CXGBI_DBG_ISCSI | 1 << CXGBI_DBG_SOCK,
2638 "ep 0x%p, cep 0x%p, cconn 0x%p, csk 0x%p,%u,0x%lx.\n",
2639 ep, cep, cconn, csk, csk->state, csk->flags);
2640
2641 if (cconn && cconn->iconn) {
2642 iscsi_suspend_tx(cconn->iconn);
2643 write_lock_bh(&csk->callback_lock);
2644 cep->csk->user_data = NULL;
2645 cconn->cep = NULL;
2646 write_unlock_bh(&csk->callback_lock);
2647 }
2648 iscsi_destroy_endpoint(ep);
2649
2650 if (likely(csk->state >= CTP_ESTABLISHED))
2651 need_active_close(csk);
2652 else
2653 cxgbi_sock_closed(csk);
2654
2655 cxgbi_sock_put(csk);
2656}
2657EXPORT_SYMBOL_GPL(cxgbi_ep_disconnect);
2658
2659int cxgbi_iscsi_init(struct iscsi_transport *itp,
2660 struct scsi_transport_template **stt)
2661{
2662 *stt = iscsi_register_transport(itp);
2663 if (*stt == NULL) {
2664 pr_err("unable to register %s transport 0x%p.\n",
2665 itp->name, itp);
2666 return -ENODEV;
2667 }
2668 log_debug(1 << CXGBI_DBG_ISCSI,
2669 "%s, registered iscsi transport 0x%p.\n",
2670 itp->name, stt);
2671 return 0;
2672}
2673EXPORT_SYMBOL_GPL(cxgbi_iscsi_init);
2674
2675void cxgbi_iscsi_cleanup(struct iscsi_transport *itp,
2676 struct scsi_transport_template **stt)
2677{
2678 if (*stt) {
2679 log_debug(1 << CXGBI_DBG_ISCSI,
2680 "de-register transport 0x%p, %s, stt 0x%p.\n",
2681 itp, itp->name, *stt);
2682 *stt = NULL;
2683 iscsi_unregister_transport(itp);
2684 }
2685}
2686EXPORT_SYMBOL_GPL(cxgbi_iscsi_cleanup);
2687
Al Viro587a1f12011-07-23 23:11:19 -04002688umode_t cxgbi_attr_is_visible(int param_type, int param)
Mike Christie3128c6c2011-07-25 13:48:42 -05002689{
2690 switch (param_type) {
Mike Christief27fb2e2011-07-25 13:48:45 -05002691 case ISCSI_HOST_PARAM:
2692 switch (param) {
2693 case ISCSI_HOST_PARAM_NETDEV_NAME:
2694 case ISCSI_HOST_PARAM_HWADDRESS:
2695 case ISCSI_HOST_PARAM_IPADDRESS:
2696 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2697 return S_IRUGO;
2698 default:
2699 return 0;
2700 }
Mike Christie3128c6c2011-07-25 13:48:42 -05002701 case ISCSI_PARAM:
2702 switch (param) {
2703 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2704 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2705 case ISCSI_PARAM_HDRDGST_EN:
2706 case ISCSI_PARAM_DATADGST_EN:
2707 case ISCSI_PARAM_CONN_ADDRESS:
2708 case ISCSI_PARAM_CONN_PORT:
2709 case ISCSI_PARAM_EXP_STATSN:
2710 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2711 case ISCSI_PARAM_PERSISTENT_PORT:
2712 case ISCSI_PARAM_PING_TMO:
2713 case ISCSI_PARAM_RECV_TMO:
Mike Christie1d063c12011-07-25 13:48:43 -05002714 case ISCSI_PARAM_INITIAL_R2T_EN:
2715 case ISCSI_PARAM_MAX_R2T:
2716 case ISCSI_PARAM_IMM_DATA_EN:
2717 case ISCSI_PARAM_FIRST_BURST:
2718 case ISCSI_PARAM_MAX_BURST:
2719 case ISCSI_PARAM_PDU_INORDER_EN:
2720 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2721 case ISCSI_PARAM_ERL:
2722 case ISCSI_PARAM_TARGET_NAME:
2723 case ISCSI_PARAM_TPGT:
2724 case ISCSI_PARAM_USERNAME:
2725 case ISCSI_PARAM_PASSWORD:
2726 case ISCSI_PARAM_USERNAME_IN:
2727 case ISCSI_PARAM_PASSWORD_IN:
2728 case ISCSI_PARAM_FAST_ABORT:
2729 case ISCSI_PARAM_ABORT_TMO:
2730 case ISCSI_PARAM_LU_RESET_TMO:
2731 case ISCSI_PARAM_TGT_RESET_TMO:
2732 case ISCSI_PARAM_IFACE_NAME:
2733 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie3128c6c2011-07-25 13:48:42 -05002734 return S_IRUGO;
2735 default:
2736 return 0;
2737 }
2738 }
2739
2740 return 0;
2741}
2742EXPORT_SYMBOL_GPL(cxgbi_attr_is_visible);
2743
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002744static int __init libcxgbi_init_module(void)
2745{
Karen Xie0ea5bf32015-04-10 13:57:18 -07002746 pr_info("%s", version);
Varun Prakash75b61252017-05-16 19:23:44 +05302747
2748 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, cb) <
2749 sizeof(struct cxgbi_skb_cb));
kxie@chelsio.com9ba682f2010-08-16 20:55:53 -07002750 return 0;
2751}
2752
2753static void __exit libcxgbi_exit_module(void)
2754{
2755 cxgbi_device_unregister_all(0xFF);
2756 return;
2757}
2758
2759module_init(libcxgbi_init_module);
2760module_exit(libcxgbi_exit_module);