aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Bottomley <jejb@mulgrave.(none)>2005-10-28 12:41:41 -0400
committerJames Bottomley <jejb@mulgrave.(none)>2005-10-28 12:41:41 -0400
commit38a9a621aba953ddb8051547e98c10ec3c741312 (patch)
tree53fc96a2902d2c5bf2a82d97d99b78570c76291e
parent27d1097d39509494706eaa2620ef3b1e780a3224 (diff)
parente75d51761debffbc5e556e02c8ceda4f8c1a3e83 (diff)
Merge HEAD from ../scsi-misc-2.6-old
-rw-r--r--drivers/scsi/Kconfig26
-rw-r--r--drivers/scsi/Makefile1
-rw-r--r--drivers/scsi/aacraid/aachba.c57
-rw-r--r--drivers/scsi/aacraid/aacraid.h2
-rw-r--r--drivers/scsi/aacraid/commctrl.c6
-rw-r--r--drivers/scsi/aacraid/linit.c10
-rw-r--r--drivers/scsi/dc395x.c13
-rw-r--r--drivers/scsi/iscsi_tcp.c3642
-rw-r--r--drivers/scsi/iscsi_tcp.h322
-rw-r--r--drivers/scsi/ncr53c8xx.c22
-rw-r--r--drivers/scsi/scsi_transport_iscsi.c1380
-rw-r--r--drivers/scsi/sym53c8xx_defs.h13
-rw-r--r--drivers/scsi/tmscsim.c8
-rw-r--r--include/scsi/iscsi_if.h245
-rw-r--r--include/scsi/iscsi_proto.h589
-rw-r--r--include/scsi/scsi_transport_iscsi.h202
16 files changed, 6095 insertions, 443 deletions
diff --git a/drivers/scsi/Kconfig b/drivers/scsi/Kconfig
index 9c9f162bd6e..78c33180ebe 100644
--- a/drivers/scsi/Kconfig
+++ b/drivers/scsi/Kconfig
@@ -229,7 +229,7 @@ config SCSI_FC_ATTRS
229 229
230config SCSI_ISCSI_ATTRS 230config SCSI_ISCSI_ATTRS
231 tristate "iSCSI Transport Attributes" 231 tristate "iSCSI Transport Attributes"
232 depends on SCSI 232 depends on SCSI && NET
233 help 233 help
234 If you wish to export transport-specific information about 234 If you wish to export transport-specific information about
235 each attached iSCSI device to sysfs, say Y. 235 each attached iSCSI device to sysfs, say Y.
@@ -247,6 +247,30 @@ endmenu
247menu "SCSI low-level drivers" 247menu "SCSI low-level drivers"
248 depends on SCSI!=n 248 depends on SCSI!=n
249 249
250config ISCSI_TCP
251 tristate "iSCSI Initiator over TCP/IP"
252 depends on SCSI && INET
253 select CRYPTO
254 select CRYPTO_MD5
255 select CRYPTO_CRC32C
256 select SCSI_ISCSI_ATTRS
257 help
258 The iSCSI Driver provides a host with the ability to access storage
259 through an IP network. The driver uses the iSCSI protocol to transport
260 SCSI requests and responses over a TCP/IP network between the host
261 (the "initiator") and "targets". Architecturally, the iSCSI driver
262 combines with the host's TCP/IP stack, network drivers, and Network
263 Interface Card (NIC) to provide the same functions as a SCSI or a
264 Fibre Channel (FC) adapter driver with a Host Bus Adapter (HBA).
265
266 To compile this driver as a module, choose M here: the
267 module will be called iscsi_tcp.
268
269 The userspace component needed to initialize the driver, documentation,
270 and sample configuration files can be found here:
271
272 http://linux-iscsi.sf.net
273
250config SGIWD93_SCSI 274config SGIWD93_SCSI
251 tristate "SGI WD93C93 SCSI Driver" 275 tristate "SGI WD93C93 SCSI Driver"
252 depends on SGI_IP22 && SCSI 276 depends on SGI_IP22 && SCSI
diff --git a/drivers/scsi/Makefile b/drivers/scsi/Makefile
index 2d4439826c0..8dfb9884afe 100644
--- a/drivers/scsi/Makefile
+++ b/drivers/scsi/Makefile
@@ -33,6 +33,7 @@ obj-$(CONFIG_SCSI_FC_ATTRS) += scsi_transport_fc.o
33obj-$(CONFIG_SCSI_ISCSI_ATTRS) += scsi_transport_iscsi.o 33obj-$(CONFIG_SCSI_ISCSI_ATTRS) += scsi_transport_iscsi.o
34obj-$(CONFIG_SCSI_SAS_ATTRS) += scsi_transport_sas.o 34obj-$(CONFIG_SCSI_SAS_ATTRS) += scsi_transport_sas.o
35 35
36obj-$(CONFIG_ISCSI_TCP) += iscsi_tcp.o
36obj-$(CONFIG_SCSI_AMIGA7XX) += amiga7xx.o 53c7xx.o 37obj-$(CONFIG_SCSI_AMIGA7XX) += amiga7xx.o 53c7xx.o
37obj-$(CONFIG_A3000_SCSI) += a3000.o wd33c93.o 38obj-$(CONFIG_A3000_SCSI) += a3000.o wd33c93.o
38obj-$(CONFIG_A2091_SCSI) += a2091.o wd33c93.o 39obj-$(CONFIG_A2091_SCSI) += a2091.o wd33c93.o
diff --git a/drivers/scsi/aacraid/aachba.c b/drivers/scsi/aacraid/aachba.c
index 93416f760e5..a913196459d 100644
--- a/drivers/scsi/aacraid/aachba.c
+++ b/drivers/scsi/aacraid/aachba.c
@@ -608,17 +608,43 @@ static char *container_types[] = {
608 * files instead of in OS dependant driver source. 608 * files instead of in OS dependant driver source.
609 */ 609 */
610 610
611static void setinqstr(int devtype, void *data, int tindex) 611static void setinqstr(struct aac_dev *dev, void *data, int tindex)
612{ 612{
613 struct scsi_inq *str; 613 struct scsi_inq *str;
614 struct aac_driver_ident *mp;
615 614
616 mp = aac_get_driver_ident(devtype);
617
618 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */ 615 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
619 616 memset(str, ' ', sizeof(*str));
620 inqstrcpy (mp->vname, str->vid); 617
621 inqstrcpy (mp->model, str->pid); /* last six chars reserved for vol type */ 618 if (dev->supplement_adapter_info.AdapterTypeText[0]) {
619 char * cp = dev->supplement_adapter_info.AdapterTypeText;
620 int c = sizeof(str->vid);
621 while (*cp && *cp != ' ' && --c)
622 ++cp;
623 c = *cp;
624 *cp = '\0';
625 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
626 str->vid);
627 *cp = c;
628 while (*cp && *cp != ' ')
629 ++cp;
630 while (*cp == ' ')
631 ++cp;
632 /* last six chars reserved for vol type */
633 c = 0;
634 if (strlen(cp) > sizeof(str->pid)) {
635 c = cp[sizeof(str->pid)];
636 cp[sizeof(str->pid)] = '\0';
637 }
638 inqstrcpy (cp, str->pid);
639 if (c)
640 cp[sizeof(str->pid)] = c;
641 } else {
642 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
643
644 inqstrcpy (mp->vname, str->vid);
645 /* last six chars reserved for vol type */
646 inqstrcpy (mp->model, str->pid);
647 }
622 648
623 if (tindex < (sizeof(container_types)/sizeof(char *))){ 649 if (tindex < (sizeof(container_types)/sizeof(char *))){
624 char *findit = str->pid; 650 char *findit = str->pid;
@@ -627,7 +653,9 @@ static void setinqstr(int devtype, void *data, int tindex)
627 /* RAID is superfluous in the context of a RAID device */ 653 /* RAID is superfluous in the context of a RAID device */
628 if (memcmp(findit-4, "RAID", 4) == 0) 654 if (memcmp(findit-4, "RAID", 4) == 0)
629 *(findit -= 4) = ' '; 655 *(findit -= 4) = ' ';
630 inqstrcpy (container_types[tindex], findit + 1); 656 if (((findit - str->pid) + strlen(container_types[tindex]))
657 < (sizeof(str->pid) + sizeof(str->prl)))
658 inqstrcpy (container_types[tindex], findit + 1);
631 } 659 }
632 inqstrcpy ("V1.0", str->prl); 660 inqstrcpy ("V1.0", str->prl);
633} 661}
@@ -822,12 +850,12 @@ int aac_get_adapter_info(struct aac_dev* dev)
822 dev->dac_support = (dacmode!=0); 850 dev->dac_support = (dacmode!=0);
823 } 851 }
824 if(dev->dac_support != 0) { 852 if(dev->dac_support != 0) {
825 if (!pci_set_dma_mask(dev->pdev, 0xFFFFFFFFFFFFFFFFULL) && 853 if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
826 !pci_set_consistent_dma_mask(dev->pdev, 0xFFFFFFFFFFFFFFFFULL)) { 854 !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
827 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n", 855 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
828 dev->name, dev->id); 856 dev->name, dev->id);
829 } else if (!pci_set_dma_mask(dev->pdev, 0xFFFFFFFFULL) && 857 } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
830 !pci_set_consistent_dma_mask(dev->pdev, 0xFFFFFFFFULL)) { 858 !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
831 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n", 859 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
832 dev->name, dev->id); 860 dev->name, dev->id);
833 dev->dac_support = 0; 861 dev->dac_support = 0;
@@ -1438,7 +1466,6 @@ int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1438 struct Scsi_Host *host = scsicmd->device->host; 1466 struct Scsi_Host *host = scsicmd->device->host;
1439 struct aac_dev *dev = (struct aac_dev *)host->hostdata; 1467 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
1440 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev; 1468 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
1441 int cardtype = dev->cardtype;
1442 int ret; 1469 int ret;
1443 1470
1444 /* 1471 /*
@@ -1542,14 +1569,14 @@ int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
1542 * see: <vendor>.c i.e. aac.c 1569 * see: <vendor>.c i.e. aac.c
1543 */ 1570 */
1544 if (scsicmd->device->id == host->this_id) { 1571 if (scsicmd->device->id == host->this_id) {
1545 setinqstr(cardtype, (void *) (inq_data.inqd_vid), (sizeof(container_types)/sizeof(char *))); 1572 setinqstr(dev, (void *) (inq_data.inqd_vid), (sizeof(container_types)/sizeof(char *)));
1546 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */ 1573 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
1547 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data)); 1574 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
1548 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD; 1575 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1549 scsicmd->scsi_done(scsicmd); 1576 scsicmd->scsi_done(scsicmd);
1550 return 0; 1577 return 0;
1551 } 1578 }
1552 setinqstr(cardtype, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type); 1579 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
1553 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */ 1580 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
1554 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data)); 1581 aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
1555 return aac_get_container_name(scsicmd, cid); 1582 return aac_get_container_name(scsicmd, cid);
diff --git a/drivers/scsi/aacraid/aacraid.h b/drivers/scsi/aacraid/aacraid.h
index d54b1cc88d0..2ebe402bc31 100644
--- a/drivers/scsi/aacraid/aacraid.h
+++ b/drivers/scsi/aacraid/aacraid.h
@@ -1560,7 +1560,7 @@ struct fib_ioctl
1560 1560
1561struct revision 1561struct revision
1562{ 1562{
1563 __le32 compat; 1563 u32 compat;
1564 __le32 version; 1564 __le32 version;
1565 __le32 build; 1565 __le32 build;
1566}; 1566};
diff --git a/drivers/scsi/aacraid/commctrl.c b/drivers/scsi/aacraid/commctrl.c
index 71f1cad9b5f..ef623bd965f 100644
--- a/drivers/scsi/aacraid/commctrl.c
+++ b/drivers/scsi/aacraid/commctrl.c
@@ -408,7 +408,7 @@ static int check_revision(struct aac_dev *dev, void __user *arg)
408 char *driver_version = aac_driver_version; 408 char *driver_version = aac_driver_version;
409 u32 version; 409 u32 version;
410 410
411 response.compat = cpu_to_le32(1); 411 response.compat = 1;
412 version = (simple_strtol(driver_version, 412 version = (simple_strtol(driver_version,
413 &driver_version, 10) << 24) | 0x00000400; 413 &driver_version, 10) << 24) | 0x00000400;
414 version += simple_strtol(driver_version + 1, &driver_version, 10) << 16; 414 version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
@@ -574,7 +574,7 @@ static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
574 rcode = -ENOMEM; 574 rcode = -ENOMEM;
575 goto cleanup; 575 goto cleanup;
576 } 576 }
577 sg_user[i] = (void __user *)usg->sg[i].addr; 577 sg_user[i] = (void __user *)(long)usg->sg[i].addr;
578 sg_list[i] = p; // save so we can clean up later 578 sg_list[i] = p; // save so we can clean up later
579 sg_indx = i; 579 sg_indx = i;
580 580
@@ -624,7 +624,7 @@ static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
624 rcode = -ENOMEM; 624 rcode = -ENOMEM;
625 goto cleanup; 625 goto cleanup;
626 } 626 }
627 sg_user[i] = (void __user *)upsg->sg[i].addr; 627 sg_user[i] = (void __user *)(long)upsg->sg[i].addr;
628 sg_list[i] = p; // save so we can clean up later 628 sg_list[i] = p; // save so we can clean up later
629 sg_indx = i; 629 sg_indx = i;
630 630
diff --git a/drivers/scsi/aacraid/linit.c b/drivers/scsi/aacraid/linit.c
index a1f9ceef0ac..c235d0c0e7a 100644
--- a/drivers/scsi/aacraid/linit.c
+++ b/drivers/scsi/aacraid/linit.c
@@ -752,8 +752,8 @@ static int __devinit aac_probe_one(struct pci_dev *pdev,
752 if (error) 752 if (error)
753 goto out; 753 goto out;
754 754
755 if (pci_set_dma_mask(pdev, 0xFFFFFFFFULL) || 755 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) ||
756 pci_set_consistent_dma_mask(pdev, 0xFFFFFFFFULL)) 756 pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK))
757 goto out; 757 goto out;
758 /* 758 /*
759 * If the quirk31 bit is set, the adapter needs adapter 759 * If the quirk31 bit is set, the adapter needs adapter
@@ -797,9 +797,9 @@ static int __devinit aac_probe_one(struct pci_dev *pdev,
797 * address space. 797 * address space.
798 */ 798 */
799 if (aac_drivers[index].quirks & AAC_QUIRK_31BIT) 799 if (aac_drivers[index].quirks & AAC_QUIRK_31BIT)
800 if (pci_set_dma_mask(pdev, 0xFFFFFFFFULL)) 800 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK))
801 goto out_free_fibs; 801 goto out_deinit;
802 802
803 aac->maximum_num_channels = aac_drivers[index].channels; 803 aac->maximum_num_channels = aac_drivers[index].channels;
804 error = aac_get_adapter_info(aac); 804 error = aac_get_adapter_info(aac);
805 if (error < 0) 805 if (error < 0)
diff --git a/drivers/scsi/dc395x.c b/drivers/scsi/dc395x.c
index 600ba120286..c44af5795b1 100644
--- a/drivers/scsi/dc395x.c
+++ b/drivers/scsi/dc395x.c
@@ -976,6 +976,16 @@ static void send_srb(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb)
976 } 976 }
977} 977}
978 978
979static inline void pio_trigger(void)
980{
981 static int feedback_requested;
982
983 if (!feedback_requested) {
984 feedback_requested = 1;
985 printk(KERN_WARNING "%s: Please, contact <linux-scsi@vger.kernel.org> "
986 "to help improve support for your system.\n", __FILE__);
987 }
988}
979 989
980/* Prepare SRB for being sent to Device DCB w/ command *cmd */ 990/* Prepare SRB for being sent to Device DCB w/ command *cmd */
981static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb, 991static void build_srb(struct scsi_cmnd *cmd, struct DeviceCtlBlk *dcb,
@@ -2320,6 +2330,7 @@ static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2320 CFG2_WIDEFIFO); 2330 CFG2_WIDEFIFO);
2321 while (DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) != 0x40) { 2331 while (DC395x_read8(acb, TRM_S1040_SCSI_FIFOCNT) != 0x40) {
2322 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); 2332 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2333 pio_trigger();
2323 *(srb->virt_addr)++ = byte; 2334 *(srb->virt_addr)++ = byte;
2324 if (debug_enabled(DBG_PIO)) 2335 if (debug_enabled(DBG_PIO))
2325 printk(" %02x", byte); 2336 printk(" %02x", byte);
@@ -2331,6 +2342,7 @@ static void data_in_phase0(struct AdapterCtlBlk *acb, struct ScsiReqBlk *srb,
2331 /* Read the last byte ... */ 2342 /* Read the last byte ... */
2332 if (srb->total_xfer_length > 0) { 2343 if (srb->total_xfer_length > 0) {
2333 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO); 2344 u8 byte = DC395x_read8(acb, TRM_S1040_SCSI_FIFO);
2345 pio_trigger();
2334 *(srb->virt_addr)++ = byte; 2346 *(srb->virt_addr)++ = byte;
2335 srb->total_xfer_length--; 2347 srb->total_xfer_length--;
2336 if (debug_enabled(DBG_PIO)) 2348 if (debug_enabled(DBG_PIO))
@@ -2507,6 +2519,7 @@ static void data_io_transfer(struct AdapterCtlBlk *acb,
2507 if (debug_enabled(DBG_PIO)) 2519 if (debug_enabled(DBG_PIO))
2508 printk(" %02x", (unsigned char) *(srb->virt_addr)); 2520 printk(" %02x", (unsigned char) *(srb->virt_addr));
2509 2521
2522 pio_trigger();
2510 DC395x_write8(acb, TRM_S1040_SCSI_FIFO, 2523 DC395x_write8(acb, TRM_S1040_SCSI_FIFO,
2511 *(srb->virt_addr)++); 2524 *(srb->virt_addr)++);
2512 2525
diff --git a/drivers/scsi/iscsi_tcp.c b/drivers/scsi/iscsi_tcp.c
new file mode 100644
index 00000000000..4fea3e4edaa
--- /dev/null
+++ b/drivers/scsi/iscsi_tcp.c
@@ -0,0 +1,3642 @@
1/*
2 * iSCSI Initiator over TCP/IP Data-Path
3 *
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
6 * Copyright (C) 2005 Mike Christie
7 * maintained by open-iscsi@googlegroups.com
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published
11 * by the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * See the file COPYING included with this distribution for more details.
20 *
21 * Credits:
22 * Christoph Hellwig
23 * FUJITA Tomonori
24 * Arne Redlich
25 * Zhenyu Wang
26 */
27
28#include <linux/types.h>
29#include <linux/list.h>
30#include <linux/inet.h>
31#include <linux/blkdev.h>
32#include <linux/crypto.h>
33#include <linux/delay.h>
34#include <linux/kfifo.h>
35#include <linux/scatterlist.h>
36#include <net/tcp.h>
37#include <scsi/scsi_cmnd.h>
38#include <scsi/scsi_device.h>
39#include <scsi/scsi_eh.h>
40#include <scsi/scsi_request.h>
41#include <scsi/scsi_tcq.h>
42#include <scsi/scsi_host.h>
43#include <scsi/scsi.h>
44#include <scsi/scsi_transport_iscsi.h>
45
46#include "iscsi_tcp.h"
47
48MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
49 "Alex Aizman <itn780@yahoo.com>");
50MODULE_DESCRIPTION("iSCSI/TCP data-path");
51MODULE_LICENSE("GPL");
52MODULE_VERSION("0:4.409");
53/* #define DEBUG_TCP */
54/* #define DEBUG_SCSI */
55#define DEBUG_ASSERT
56
57#ifdef DEBUG_TCP
58#define debug_tcp(fmt...) printk(KERN_DEBUG "tcp: " fmt)
59#else
60#define debug_tcp(fmt...)
61#endif
62
63#ifdef DEBUG_SCSI
64#define debug_scsi(fmt...) printk(KERN_DEBUG "scsi: " fmt)
65#else
66#define debug_scsi(fmt...)
67#endif
68
69#ifndef DEBUG_ASSERT
70#ifdef BUG_ON
71#undef BUG_ON
72#endif
73#define BUG_ON(expr)
74#endif
75
76#define INVALID_SN_DELTA 0xffff
77
78static unsigned int iscsi_max_lun = 512;
79module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
80
81/* global data */
82static kmem_cache_t *taskcache;
83
84static inline void
85iscsi_buf_init_virt(struct iscsi_buf *ibuf, char *vbuf, int size)
86{
87 sg_init_one(&ibuf->sg, (u8 *)vbuf, size);
88 ibuf->sent = 0;
89}
90
91static inline void
92iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
93{
94 ibuf->sg.page = (void*)vbuf;
95 ibuf->sg.offset = (unsigned int)-1;
96 ibuf->sg.length = size;
97 ibuf->sent = 0;
98}
99
100static inline void*
101iscsi_buf_iov_base(struct iscsi_buf *ibuf)
102{
103 return (char*)ibuf->sg.page + ibuf->sent;
104}
105
106static inline void
107iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
108{
109 /*
110 * Fastpath: sg element fits into single page
111 */
112 if (sg->length + sg->offset <= PAGE_SIZE && page_count(sg->page) >= 2) {
113 ibuf->sg.page = sg->page;
114 ibuf->sg.offset = sg->offset;
115 ibuf->sg.length = sg->length;
116 } else
117 iscsi_buf_init_iov(ibuf, page_address(sg->page), sg->length);
118 ibuf->sent = 0;
119}
120
121static inline int
122iscsi_buf_left(struct iscsi_buf *ibuf)
123{
124 int rc;
125
126 rc = ibuf->sg.length - ibuf->sent;
127 BUG_ON(rc < 0);
128 return rc;
129}
130
131static inline void
132iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
133 u8* crc)
134{
135 crypto_digest_digest(conn->tx_tfm, &buf->sg, 1, crc);
136 buf->sg.length += sizeof(uint32_t);
137}
138
139static void
140iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
141{
142 struct iscsi_session *session = conn->session;
143 unsigned long flags;
144
145 spin_lock_irqsave(&session->lock, flags);
146 if (session->conn_cnt == 1 || session->leadconn == conn)
147 session->state = ISCSI_STATE_FAILED;
148 spin_unlock_irqrestore(&session->lock, flags);
149 set_bit(SUSPEND_BIT, &conn->suspend_tx);
150 set_bit(SUSPEND_BIT, &conn->suspend_rx);
151 iscsi_conn_error(iscsi_handle(conn), err);
152}
153
154static inline int
155iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
156{
157 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
158 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
159
160 if (max_cmdsn < exp_cmdsn -1 &&
161 max_cmdsn > exp_cmdsn - INVALID_SN_DELTA)
162 return ISCSI_ERR_MAX_CMDSN;
163 if (max_cmdsn > session->max_cmdsn ||
164 max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA)
165 session->max_cmdsn = max_cmdsn;
166 if (exp_cmdsn > session->exp_cmdsn ||
167 exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA)
168 session->exp_cmdsn = exp_cmdsn;
169
170 return 0;
171}
172
173static inline int
174iscsi_hdr_extract(struct iscsi_conn *conn)
175{
176 struct sk_buff *skb = conn->in.skb;
177
178 if (conn->in.copy >= conn->hdr_size &&
179 conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
180 /*
181 * Zero-copy PDU Header: using connection context
182 * to store header pointer.
183 */
184 if (skb_shinfo(skb)->frag_list == NULL &&
185 !skb_shinfo(skb)->nr_frags)
186 conn->in.hdr = (struct iscsi_hdr *)
187 ((char*)skb->data + conn->in.offset);
188 else {
189 /* ignoring return code since we checked
190 * in.copy before */
191 skb_copy_bits(skb, conn->in.offset,
192 &conn->hdr, conn->hdr_size);
193 conn->in.hdr = &conn->hdr;
194 }
195 conn->in.offset += conn->hdr_size;
196 conn->in.copy -= conn->hdr_size;
197 } else {
198 int hdr_remains;
199 int copylen;
200
201 /*
202 * PDU header scattered across SKB's,
203 * copying it... This'll happen quite rarely.
204 */
205
206 if (conn->in_progress == IN_PROGRESS_WAIT_HEADER)
207 conn->in.hdr_offset = 0;
208
209 hdr_remains = conn->hdr_size - conn->in.hdr_offset;
210 BUG_ON(hdr_remains <= 0);
211
212 copylen = min(conn->in.copy, hdr_remains);
213 skb_copy_bits(skb, conn->in.offset,
214 (char*)&conn->hdr + conn->in.hdr_offset, copylen);
215
216 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
217 "in.copy %d\n", conn->in.hdr_offset, copylen,
218 conn->in.offset, conn->in.copy);
219
220 conn->in.offset += copylen;
221 conn->in.copy -= copylen;
222 if (copylen < hdr_remains) {
223 conn->in_progress = IN_PROGRESS_HEADER_GATHER;
224 conn->in.hdr_offset += copylen;
225 return -EAGAIN;
226 }
227 conn->in.hdr = &conn->hdr;
228 conn->discontiguous_hdr_cnt++;
229 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
230 }
231
232 return 0;
233}
234
235static inline void
236iscsi_ctask_cleanup(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
237{
238 struct scsi_cmnd *sc = ctask->sc;
239 struct iscsi_session *session = conn->session;
240
241 spin_lock(&session->lock);
242 if (unlikely(!sc)) {
243 spin_unlock(&session->lock);
244 return;
245 }
246 if (sc->sc_data_direction == DMA_TO_DEVICE) {
247 struct iscsi_data_task *dtask, *n;
248 /* WRITE: cleanup Data-Out's if any */
249 spin_lock(&conn->lock);
250 list_for_each_entry_safe(dtask, n, &ctask->dataqueue, item) {
251 list_del(&dtask->item);
252 mempool_free(dtask, ctask->datapool);
253 }
254 spin_unlock(&conn->lock);
255 }
256 ctask->xmstate = XMSTATE_IDLE;
257 ctask->r2t = NULL;
258 ctask->sc = NULL;
259 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
260 spin_unlock(&session->lock);
261}
262
263/**
264 * iscsi_cmd_rsp - SCSI Command Response processing
265 * @conn: iscsi connection
266 * @ctask: scsi command task
267 **/
268static int
269iscsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
270{
271 int rc;
272 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)conn->in.hdr;
273 struct iscsi_session *session = conn->session;
274 struct scsi_cmnd *sc = ctask->sc;
275
276 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
277 if (rc) {
278 sc->result = (DID_ERROR << 16);
279 goto out;
280 }
281
282 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
283
284 sc->result = (DID_OK << 16) | rhdr->cmd_status;
285
286 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
287 sc->result = (DID_ERROR << 16);
288 goto out;
289 }
290
291 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION && conn->senselen) {
292 int sensecopy = min(conn->senselen, SCSI_SENSE_BUFFERSIZE);
293
294 memcpy(sc->sense_buffer, conn->data + 2, sensecopy);
295 debug_scsi("copied %d bytes of sense\n", sensecopy);
296 }
297
298 if (sc->sc_data_direction == DMA_TO_DEVICE)
299 goto out;
300
301 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
302 int res_count = be32_to_cpu(rhdr->residual_count);
303
304 if (res_count > 0 && res_count <= sc->request_bufflen)
305 sc->resid = res_count;
306 else
307 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
308 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
309 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
310 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
311 sc->resid = be32_to_cpu(rhdr->residual_count);
312
313out:
314 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
315 (long)sc, sc->result, ctask->itt);
316 conn->scsirsp_pdus_cnt++;
317 iscsi_ctask_cleanup(conn, ctask);
318 sc->scsi_done(sc);
319 return rc;
320}
321
322/**
323 * iscsi_data_rsp - SCSI Data-In Response processing
324 * @conn: iscsi connection
325 * @ctask: scsi command task
326 **/
327static int
328iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
329{
330 int rc;
331 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)conn->in.hdr;
332 struct iscsi_session *session = conn->session;
333 int datasn = be32_to_cpu(rhdr->datasn);
334
335 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
336 if (rc)
337 return rc;
338 /*
339 * setup Data-In byte counter (gets decremented..)
340 */
341 ctask->data_count = conn->in.datalen;
342
343 if (conn->in.datalen == 0)
344 return 0;
345
346 if (ctask->datasn != datasn)
347 return ISCSI_ERR_DATASN;
348
349 ctask->datasn++;
350
351 ctask->data_offset = be32_to_cpu(rhdr->offset);
352 if (ctask->data_offset + conn->in.datalen > ctask->total_length)
353 return ISCSI_ERR_DATA_OFFSET;
354
355 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
356 struct scsi_cmnd *sc = ctask->sc;
357
358 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
359 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
360 int res_count = be32_to_cpu(rhdr->residual_count);
361
362 if (res_count > 0 &&
363 res_count <= sc->request_bufflen) {
364 sc->resid = res_count;
365 sc->result = (DID_OK << 16) | rhdr->cmd_status;
366 } else
367 sc->result = (DID_BAD_TARGET << 16) |
368 rhdr->cmd_status;
369 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
370 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
371 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW) {
372 sc->resid = be32_to_cpu(rhdr->residual_count);
373 sc->result = (DID_OK << 16) | rhdr->cmd_status;
374 } else
375 sc->result = (DID_OK << 16) | rhdr->cmd_status;
376 }
377
378 conn->datain_pdus_cnt++;
379 return 0;
380}
381
382/**
383 * iscsi_solicit_data_init - initialize first Data-Out
384 * @conn: iscsi connection
385 * @ctask: scsi command task
386 * @r2t: R2T info
387 *
388 * Notes:
389 * Initialize first Data-Out within this R2T sequence and finds
390 * proper data_offset within this SCSI command.
391 *
392 * This function is called with connection lock taken.
393 **/
394static void
395iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
396 struct iscsi_r2t_info *r2t)
397{
398 struct iscsi_data *hdr;
399 struct iscsi_data_task *dtask;
400 struct scsi_cmnd *sc = ctask->sc;
401
402 dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC);
403 BUG_ON(!dtask);
404 hdr = &dtask->hdr;
405 memset(hdr, 0, sizeof(struct iscsi_data));
406 hdr->ttt = r2t->ttt;
407 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
408 r2t->solicit_datasn++;
409 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
410 memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
411 hdr->itt = ctask->hdr.itt;
412 hdr->exp_statsn = r2t->exp_statsn;
413 hdr->offset = cpu_to_be32(r2t->data_offset);
414 if (r2t->data_length > conn->max_xmit_dlength) {
415 hton24(hdr->dlength, conn->max_xmit_dlength);
416 r2t->data_count = conn->max_xmit_dlength;
417 hdr->flags = 0;
418 } else {
419 hton24(hdr->dlength, r2t->data_length);
420 r2t->data_count = r2t->data_length;
421 hdr->flags = ISCSI_FLAG_CMD_FINAL;
422 }
423 conn->dataout_pdus_cnt++;
424
425 r2t->sent = 0;
426
427 iscsi_buf_init_virt(&r2t->headbuf, (char*)hdr,
428 sizeof(struct iscsi_hdr));
429
430 r2t->dtask = dtask;
431
432 if (sc->use_sg) {
433 int i, sg_count = 0;
434 struct scatterlist *sg = sc->request_buffer;
435
436 r2t->sg = NULL;
437 for (i = 0; i < sc->use_sg; i++, sg += 1) {
438 /* FIXME: prefetch ? */
439 if (sg_count + sg->length > r2t->data_offset) {
440 int page_offset;
441
442 /* sg page found! */
443
444 /* offset within this page */
445 page_offset = r2t->data_offset - sg_count;
446
447 /* fill in this buffer */
448 iscsi_buf_init_sg(&r2t->sendbuf, sg);
449 r2t->sendbuf.sg.offset += page_offset;
450 r2t->sendbuf.sg.length -= page_offset;
451
452 /* xmit logic will continue with next one */
453 r2t->sg = sg + 1;
454 break;
455 }
456 sg_count += sg->length;
457 }
458 BUG_ON(r2t->sg == NULL);
459 } else
460 iscsi_buf_init_iov(&ctask->sendbuf,
461 (char*)sc->request_buffer + r2t->data_offset,
462 r2t->data_count);
463
464 list_add(&dtask->item, &ctask->dataqueue);
465}
466
467/**
468 * iscsi_r2t_rsp - iSCSI R2T Response processing
469 * @conn: iscsi connection
470 * @ctask: scsi command task
471 **/
472static int
473iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
474{
475 struct iscsi_r2t_info *r2t;
476 struct iscsi_session *session = conn->session;
477 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)conn->in.hdr;
478 int r2tsn = be32_to_cpu(rhdr->r2tsn);
479 int rc;
480
481 if (conn->in.ahslen)
482 return ISCSI_ERR_AHSLEN;
483
484 if (conn->in.datalen)
485 return ISCSI_ERR_DATALEN;
486
487 if (ctask->exp_r2tsn && ctask->exp_r2tsn != r2tsn)
488 return ISCSI_ERR_R2TSN;
489
490 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
491 if (rc)
492 return rc;
493
494 /* FIXME: use R2TSN to detect missing R2T */
495
496 /* fill-in new R2T associated with the task */
497 spin_lock(&session->lock);
498 if (!ctask->sc || ctask->mtask ||
499 session->state != ISCSI_STATE_LOGGED_IN) {
500 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
501 "recovery...\n", ctask->itt);
502 spin_unlock(&session->lock);
503 return 0;
504 }
505 rc = __kfifo_get(ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
506 BUG_ON(!rc);
507
508 r2t->exp_statsn = rhdr->statsn;
509 r2t->data_length = be32_to_cpu(rhdr->data_length);
510 if (r2t->data_length == 0 ||
511 r2t->data_length > session->max_burst) {
512 spin_unlock(&session->lock);
513 return ISCSI_ERR_DATALEN;
514 }
515
516 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
517 if (r2t->data_offset + r2t->data_length > ctask->total_length) {
518 spin_unlock(&session->lock);
519 return ISCSI_ERR_DATALEN;
520 }
521
522 r2t->ttt = rhdr->ttt; /* no flip */
523 r2t->solicit_datasn = 0;
524
525 iscsi_solicit_data_init(conn, ctask, r2t);
526
527 ctask->exp_r2tsn = r2tsn + 1;
528 ctask->xmstate |= XMSTATE_SOL_HDR;
529 __kfifo_put(ctask->r2tqueue, (void*)&r2t, sizeof(void*));
530 __kfifo_put(conn->writequeue, (void*)&ctask, sizeof(void*));
531
532 schedule_work(&conn->xmitwork);
533 conn->r2t_pdus_cnt++;
534 spin_unlock(&session->lock);
535
536 return 0;
537}
538
539static int
540iscsi_hdr_recv(struct iscsi_conn *conn)
541{
542 int rc = 0;
543 struct iscsi_hdr *hdr;
544 struct iscsi_cmd_task *ctask;
545 struct iscsi_session *session = conn->session;
546 uint32_t cdgst, rdgst = 0;
547
548 hdr = conn->in.hdr;
549
550 /* verify PDU length */
551 conn->in.datalen = ntoh24(hdr->dlength);
552 if (conn->in.datalen > conn->max_recv_dlength) {
553 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
554 conn->in.datalen, conn->max_recv_dlength);
555 return ISCSI_ERR_DATALEN;
556 }
557 conn->data_copied = 0;
558
559 /* read AHS */
560 conn->in.ahslen = hdr->hlength * 4;
561 conn->in.offset += conn->in.ahslen;
562 conn->in.copy -= conn->in.ahslen;
563 if (conn->in.copy < 0) {
564 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
565 "%d bytes\n", conn->in.ahslen);
566 return ISCSI_ERR_AHSLEN;
567 }
568
569 /* calculate read padding */
570 conn->in.padding = conn->in.datalen & (ISCSI_PAD_LEN-1);
571 if (conn->in.padding) {
572 conn->in.padding = ISCSI_PAD_LEN - conn->in.padding;
573 debug_scsi("read padding %d bytes\n", conn->in.padding);
574 }
575
576 if (conn->hdrdgst_en) {
577 struct scatterlist sg;
578
579 sg_init_one(&sg, (u8 *)hdr,
580 sizeof(struct iscsi_hdr) + conn->in.ahslen);
581 crypto_digest_digest(conn->rx_tfm, &sg, 1, (u8 *)&cdgst);
582 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
583 conn->in.ahslen);
584 }
585
586 /* save opcode for later */
587 conn->in.opcode = hdr->opcode;
588
589 /* verify itt (itt encoding: age+cid+itt) */
590 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
591 if ((hdr->itt & AGE_MASK) !=
592 (session->age << AGE_SHIFT)) {
593 printk(KERN_ERR "iscsi_tcp: received itt %x expected "
594 "session age (%x)\n", hdr->itt,
595 session->age & AGE_MASK);
596 return ISCSI_ERR_BAD_ITT;
597 }
598
599 if ((hdr->itt & CID_MASK) != (conn->id << CID_SHIFT)) {
600 printk(KERN_ERR "iscsi_tcp: received itt %x, expected "
601 "CID (%x)\n", hdr->itt, conn->id);
602 return ISCSI_ERR_BAD_ITT;
603 }
604 conn->in.itt = hdr->itt & ITT_MASK;
605 } else
606 conn->in.itt = hdr->itt;
607
608 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
609 hdr->opcode, conn->in.offset, conn->in.copy,
610 conn->in.ahslen, conn->in.datalen);
611
612 if (conn->in.itt < session->cmds_max) {
613 if (conn->hdrdgst_en && cdgst != rdgst) {
614 printk(KERN_ERR "iscsi_tcp: itt %x: hdrdgst error "
615 "recv 0x%x calc 0x%x\n", conn->in.itt, rdgst,
616 cdgst);
617 return ISCSI_ERR_HDR_DGST;
618 }
619
620 ctask = (struct iscsi_cmd_task *)session->cmds[conn->in.itt];
621
622 if (!ctask->sc) {
623 printk(KERN_INFO "iscsi_tcp: dropping ctask with "
624 "itt 0x%x\n", ctask->itt);
625 conn->in.datalen = 0; /* force drop */
626 return 0;
627 }
628
629 if (ctask->sc->SCp.phase != session->age) {
630 printk(KERN_ERR "iscsi_tcp: ctask's session age %d, "
631 "expected %d\n", ctask->sc->SCp.phase,
632 session->age);
633 return ISCSI_ERR_SESSION_FAILED;
634 }
635
636 conn->in.ctask = ctask;
637
638 debug_scsi("rsp [op 0x%x cid %d sc %lx itt 0x%x len %d]\n",
639 hdr->opcode, conn->id, (long)ctask->sc,
640 ctask->itt, conn->in.datalen);
641
642 switch(conn->in.opcode) {
643 case ISCSI_OP_SCSI_CMD_RSP:
644 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
645 if (ctask->hdr.flags & ISCSI_FLAG_CMD_WRITE)
646 rc = iscsi_cmd_rsp(conn, ctask);
647 else if (!conn->in.datalen)
648 rc = iscsi_cmd_rsp(conn, ctask);
649 else
650 /*
651 * got sense or response data; copying PDU
652 * Header to the connection's header
653 * placeholder
654 */
655 memcpy(&conn->hdr, hdr,
656 sizeof(struct iscsi_hdr));
657 break;
658 case ISCSI_OP_SCSI_DATA_IN:
659 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
660 /* save flags for non-exceptional status */
661 conn->in.flags = hdr->flags;
662 /* save cmd_status for sense data */
663 conn->in.cmd_status =
664 ((struct iscsi_data_rsp*)hdr)->cmd_status;
665 rc = iscsi_data_rsp(conn, ctask);
666 break;
667 case ISCSI_OP_R2T:
668 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
669 if (ctask->hdr.flags & ISCSI_FLAG_CMD_WRITE &&
670 ctask->sc->sc_data_direction == DMA_TO_DEVICE)
671 rc = iscsi_r2t_rsp(conn, ctask);
672 else
673 rc = ISCSI_ERR_PROTO;
674 break;
675 default:
676 rc = ISCSI_ERR_BAD_OPCODE;
677 break;
678 }
679 } else if (conn->in.itt >= ISCSI_MGMT_ITT_OFFSET &&
680 conn->in.itt < ISCSI_MGMT_ITT_OFFSET +
681 session->mgmtpool_max) {
682 struct iscsi_mgmt_task *mtask = (struct iscsi_mgmt_task *)
683 session->mgmt_cmds[conn->in.itt -
684 ISCSI_MGMT_ITT_OFFSET];
685
686 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
687 conn->in.opcode, conn->id, mtask->itt,
688 conn->in.datalen);
689
690 switch(conn->in.opcode) {
691 case ISCSI_OP_LOGIN_RSP:
692 case ISCSI_OP_TEXT_RSP:
693 case ISCSI_OP_LOGOUT_RSP:
694 rc = iscsi_check_assign_cmdsn(session,
695 (struct iscsi_nopin*)hdr);
696 if (rc)
697 break;
698
699 if (!conn->in.datalen) {
700 rc = iscsi_recv_pdu(iscsi_handle(conn), hdr,
701 NULL, 0);
702 if (conn->login_mtask != mtask) {
703 spin_lock(&session->lock);
704 __kfifo_put(session->mgmtpool.queue,
705 (void*)&mtask, sizeof(void*));
706 spin_unlock(&session->lock);
707 }
708 }
709 break;
710 case ISCSI_OP_SCSI_TMFUNC_RSP:
711 rc = iscsi_check_assign_cmdsn(session,
712 (struct iscsi_nopin*)hdr);
713 if (rc)
714 break;
715
716 if (conn->in.datalen || conn->in.ahslen) {
717 rc = ISCSI_ERR_PROTO;
718 break;
719 }
720 conn->tmfrsp_pdus_cnt++;
721 spin_lock(&session->lock);
722 if (conn->tmabort_state == TMABORT_INITIAL) {
723 __kfifo_put(session->mgmtpool.queue,
724 (void*)&mtask, sizeof(void*));
725 conn->tmabort_state =
726 ((struct iscsi_tm_rsp *)hdr)->
727 response == ISCSI_TMF_RSP_COMPLETE ?
728 TMABORT_SUCCESS:TMABORT_FAILED;
729 /* unblock eh_abort() */
730 wake_up(&conn->ehwait);
731 }
732 spin_unlock(&session->lock);
733 break;
734 case ISCSI_OP_NOOP_IN:
735 if (hdr->ttt != ISCSI_RESERVED_TAG) {
736 rc = ISCSI_ERR_PROTO;
737 break;
738 }
739 rc = iscsi_check_assign_cmdsn(session,
740 (struct iscsi_nopin*)hdr);
741 if (rc)
742 break;
743 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
744
745 if (!conn->in.datalen) {
746 struct iscsi_mgmt_task *mtask;
747
748 rc = iscsi_recv_pdu(iscsi_handle(conn), hdr,
749 NULL, 0);
750 mtask = (struct iscsi_mgmt_task *)
751 session->mgmt_cmds[conn->in.itt -
752 ISCSI_MGMT_ITT_OFFSET];
753 if (conn->login_mtask != mtask) {
754 spin_lock(&session->lock);
755 __kfifo_put(session->mgmtpool.queue,
756 (void*)&mtask, sizeof(void*));
757 spin_unlock(&session->lock);
758 }
759 }
760 break;
761 default:
762 rc = ISCSI_ERR_BAD_OPCODE;
763 break;
764 }
765 } else if (conn->in.itt == ISCSI_RESERVED_TAG) {
766 switch(conn->in.opcode) {
767 case ISCSI_OP_NOOP_IN:
768 if (!conn->in.datalen) {
769 rc = iscsi_check_assign_cmdsn(session,
770 (struct iscsi_nopin*)hdr);
771 if (!rc && hdr->ttt != ISCSI_RESERVED_TAG)
772 rc = iscsi_recv_pdu(iscsi_handle(conn),
773 hdr, NULL, 0);
774 } else
775 rc = ISCSI_ERR_PROTO;
776 break;
777 case ISCSI_OP_REJECT:
778 /* we need sth like iscsi_reject_rsp()*/
779 case ISCSI_OP_ASYNC_EVENT:
780 /* we need sth like iscsi_async_event_rsp() */
781 rc = ISCSI_ERR_BAD_OPCODE;
782 break;
783 default:
784 rc = ISCSI_ERR_BAD_OPCODE;
785 break;
786 }
787 } else
788 rc = ISCSI_ERR_BAD_ITT;
789
790 return rc;
791}
792
793/**
794 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
795 * @conn: iscsi connection
796 * @ctask: scsi command task
797 * @buf: buffer to copy to
798 * @buf_size: size of buffer
799 * @offset: offset within the buffer
800 *
801 * Notes:
802 * The function calls skb_copy_bits() and updates per-connection and
803 * per-cmd byte counters.
804 *
805 * Read counters (in bytes):
806 *
807 * conn->in.offset offset within in progress SKB
808 * conn->in.copy left to copy from in progress SKB
809 * including padding
810 * conn->in.copied copied already from in progress SKB
811 * conn->data_copied copied already from in progress buffer
812 * ctask->sent total bytes sent up to the MidLayer
813 * ctask->data_count left to copy from in progress Data-In
814 * buf_left left to copy from in progress buffer
815 **/
816static inline int
817iscsi_ctask_copy(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
818 void *buf, int buf_size, int offset)
819{
820 int buf_left = buf_size - (conn->data_copied + offset);
821 int size = min(conn->in.copy, buf_left);
822 int rc;
823
824 size = min(size, ctask->data_count);
825
826 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
827 size, conn->in.offset, conn->in.copied);
828
829 BUG_ON(size <= 0);
830 BUG_ON(ctask->sent + size > ctask->total_length);
831
832 rc = skb_copy_bits(conn->in.skb, conn->in.offset,
833 (char*)buf + (offset + conn->data_copied), size);
834 /* must fit into skb->len */
835 BUG_ON(rc);
836
837 conn->in.offset += size;
838 conn->in.copy -= size;
839 conn->in.copied += size;
840 conn->data_copied += size;
841 ctask->sent += size;
842 ctask->data_count -= size;
843
844 BUG_ON(conn->in.copy < 0);
845 BUG_ON(ctask->data_count < 0);
846
847 if (buf_size != (conn->data_copied + offset)) {
848 if (!ctask->data_count) {
849 BUG_ON(buf_size - conn->data_copied < 0);
850 /* done with this PDU */
851 return buf_size - conn->data_copied;
852 }
853 return -EAGAIN;
854 }
855
856 /* done with this buffer or with both - PDU and buffer */
857 conn->data_copied = 0;
858 return 0;
859}
860
861/**
862 * iscsi_tcp_copy - copy skb bits to the destanation buffer
863 * @conn: iscsi connection
864 * @buf: buffer to copy to
865 * @buf_size: number of bytes to copy
866 *
867 * Notes:
868 * The function calls skb_copy_bits() and updates per-connection
869 * byte counters.
870 **/
871static inline int
872iscsi_tcp_copy(struct iscsi_conn *conn, void *buf, int buf_size)
873{
874 int buf_left = buf_size - conn->data_copied;
875 int size = min(conn->in.copy, buf_left);
876 int rc;
877
878 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
879 size, conn->in.offset, conn->data_copied);
880 BUG_ON(size <= 0);
881
882 rc = skb_copy_bits(conn->in.skb, conn->in.offset,
883 (char*)buf + conn->data_copied, size);
884 BUG_ON(rc);
885
886 conn->in.offset += size;
887 conn->in.copy -= size;
888 conn->in.copied += size;
889 conn->data_copied += size;
890
891 if (buf_size != conn->data_copied)
892 return -EAGAIN;
893
894 return 0;
895}
896
897static inline void
898partial_sg_digest_update(struct iscsi_conn *conn, struct scatterlist *sg,
899 int offset, int length)
900{
901 struct scatterlist temp;
902
903 memcpy(&temp, sg, sizeof(struct scatterlist));
904 temp.offset = offset;
905 temp.length = length;
906 crypto_digest_update(conn->data_rx_tfm, &temp, 1);
907}
908
909static int iscsi_scsi_data_in(struct iscsi_conn *conn)
910{
911 struct iscsi_cmd_task *ctask = conn->in.ctask;
912 struct scsi_cmnd *sc = ctask->sc;
913 struct scatterlist tmp, *sg;
914 int i, offset, rc = 0;
915
916 BUG_ON((void*)ctask != sc->SCp.ptr);
917
918 /*
919 * copying Data-In into the Scsi_Cmnd
920 */
921 if (!sc->use_sg) {
922 i = ctask->data_count;
923 rc = iscsi_ctask_copy(conn, ctask, sc->request_buffer,
924 sc->request_bufflen, ctask->data_offset);
925 if (rc == -EAGAIN)
926 return rc;
927 if (conn->datadgst_en) {
928 sg_init_one(&tmp, sc->request_buffer, i);
929 crypto_digest_update(conn->data_rx_tfm, &tmp, 1);
930 }
931 rc = 0;
932 goto done;
933 }
934
935 offset = ctask->data_offset;
936 sg = sc->request_buffer;
937
938 if (ctask->data_offset)
939 for (i = 0; i < ctask->sg_count; i++)
940 offset -= sg[i].length;
941 /* we've passed through partial sg*/
942 if (offset < 0)
943 offset = 0;
944
945 for (i = ctask->sg_count; i < sc->use_sg; i++) {
946 char *dest;
947
948 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
949 rc = iscsi_ctask_copy(conn, ctask, dest + sg[i].offset,
950 sg[i].length, offset);
951 kunmap_atomic(dest, KM_SOFTIRQ0);
952 if (rc == -EAGAIN)
953 /* continue with the next SKB/PDU */
954 return rc;
955 if (!rc) {
956 if (conn->datadgst_en) {
957 if (!offset)
958 crypto_digest_update(conn->data_rx_tfm,
959 &sg[i], 1);
960 else
961 partial_sg_digest_update(conn, &sg[i],
962 sg[i].offset + offset,
963 sg[i].length - offset);
964 }
965 offset = 0;
966 ctask->sg_count++;
967 }
968
969 if (!ctask->data_count) {
970 if (rc && conn->datadgst_en)
971 /*
972 * data-in is complete, but buffer not...
973 */
974 partial_sg_digest_update(conn, &sg[i],
975 sg[i].offset, sg[i].length-rc);
976 rc = 0;
977 break;
978 }
979
980 if (!conn->in.copy)
981 return -EAGAIN;
982 }
983 BUG_ON(ctask->data_count);
984
985done:
986 /* check for non-exceptional status */
987 if (conn->in.flags & ISCSI_FLAG_DATA_STATUS) {
988 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
989 (long)sc, sc->result, ctask->itt);
990 conn->scsirsp_pdus_cnt++;
991 iscsi_ctask_cleanup(conn, ctask);
992 sc->scsi_done(sc);
993 }
994
995 return rc;
996}
997
998static int
999iscsi_data_recv(struct iscsi_conn *conn)
1000{
1001 struct iscsi_session *session = conn->session;
1002 int rc = 0;
1003
1004 switch(conn->in.opcode) {
1005 case ISCSI_OP_SCSI_DATA_IN:
1006 rc = iscsi_scsi_data_in(conn);
1007 break;
1008 case ISCSI_OP_SCSI_CMD_RSP: {
1009 /*
1010 * SCSI Sense Data:
1011 * copying the entire Data Segment.
1012 */
1013 if (iscsi_tcp_copy(conn, conn->data, conn->in.datalen)) {
1014 rc = -EAGAIN;
1015 goto exit;
1016 }
1017
1018 /*
1019 * check for sense
1020 */
1021 conn->in.hdr = &conn->hdr;
1022 conn->senselen = (conn->data[0] << 8) | conn->data[1];
1023 rc = iscsi_cmd_rsp(conn, conn->in.ctask);
1024 }
1025 break;
1026 case ISCSI_OP_TEXT_RSP:
1027 case ISCSI_OP_LOGIN_RSP:
1028 case ISCSI_OP_NOOP_IN: {
1029 struct iscsi_mgmt_task *mtask = NULL;
1030
1031 if (conn->in.itt != ISCSI_RESERVED_TAG)
1032 mtask = (struct iscsi_mgmt_task *)
1033 session->mgmt_cmds[conn->in.itt -
1034 ISCSI_MGMT_ITT_OFFSET];
1035
1036 /*
1037 * Collect data segment to the connection's data
1038 * placeholder
1039 */
1040 if (iscsi_tcp_copy(conn, conn->data, conn->in.datalen)) {
1041 rc = -EAGAIN;
1042 goto exit;
1043 }
1044
1045 rc = iscsi_recv_pdu(iscsi_handle(conn), conn->in.hdr,
1046 conn->data, conn->in.datalen);
1047
1048 if (mtask && conn->login_mtask != mtask) {
1049 spin_lock(&session->lock);
1050 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1051 sizeof(void*));
1052 spin_unlock(&session->lock);
1053 }
1054 }
1055 break;
1056 default:
1057 BUG_ON(1);
1058 }
1059exit:
1060 return rc;
1061}
1062
1063/**
1064 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
1065 * @rd_desc: read descriptor
1066 * @skb: socket buffer
1067 * @offset: offset in skb
1068 * @len: skb->len - offset
1069 **/
1070static int
1071iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
1072 unsigned int offset, size_t len)
1073{
1074 int rc;
1075 struct iscsi_conn *conn = rd_desc->arg.data;
1076 int processed;
1077 char pad[ISCSI_PAD_LEN];
1078 struct scatterlist sg;
1079
1080 /*
1081 * Save current SKB and its offset in the corresponding
1082 * connection context.
1083 */
1084 conn->in.copy = skb->len - offset;
1085 conn->in.offset = offset;
1086 conn->in.skb = skb;
1087 conn->in.len = conn->in.copy;
1088 BUG_ON(conn->in.copy <= 0);
1089 debug_tcp("in %d bytes\n", conn->in.copy);
1090
1091more:
1092 conn->in.copied = 0;
1093 rc = 0;
1094
1095 if (unlikely(conn->suspend_rx)) {
1096 debug_tcp("conn %d Rx suspended!\n", conn->id);
1097 return 0;
1098 }
1099
1100 if (conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
1101 conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
1102 rc = iscsi_hdr_extract(conn);
1103 if (rc) {
1104 if (rc == -EAGAIN)
1105 goto nomore;
1106 else {
1107 iscsi_conn_failure(conn, rc);
1108 return 0;
1109 }
1110 }
1111
1112 /*
1113 * Verify and process incoming PDU header.
1114 */
1115 rc = iscsi_hdr_recv(conn);
1116 if (!rc && conn->in.datalen) {
1117 if (conn->datadgst_en &&
1118 conn->in.opcode == ISCSI_OP_SCSI_DATA_IN) {
1119 BUG_ON(!conn->data_rx_tfm);
1120 crypto_digest_init(conn->data_rx_tfm);
1121 }
1122 conn->in_progress = IN_PROGRESS_DATA_RECV;
1123 } else if (rc) {
1124 iscsi_conn_failure(conn, rc);
1125 return 0;
1126 }
1127 }
1128
1129 if (conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
1130 debug_tcp("extra data_recv offset %d copy %d\n",
1131 conn->in.offset, conn->in.copy);
1132 if (conn->in.opcode == ISCSI_OP_SCSI_DATA_IN) {
1133 uint32_t recv_digest;
1134 skb_copy_bits(conn->in.skb, conn->in.offset,
1135 &recv_digest, 4);
1136 conn->in.offset += 4;
1137 conn->in.copy -= 4;
1138 if (recv_digest != conn->in.datadgst) {
1139 debug_tcp("iscsi_tcp: data digest error!"
1140 "0x%x != 0x%x\n", recv_digest,
1141 conn->in.datadgst);
1142 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
1143 return 0;
1144 } else {
1145 debug_tcp("iscsi_tcp: data digest match!"
1146 "0x%x == 0x%x\n", recv_digest,
1147 conn->in.datadgst);
1148 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1149 }
1150 }
1151 }
1152
1153 if (conn->in_progress == IN_PROGRESS_DATA_RECV && conn->in.copy) {
1154
1155 debug_tcp("data_recv offset %d copy %d\n",
1156 conn->in.offset, conn->in.copy);
1157
1158 rc = iscsi_data_recv(conn);
1159 if (rc) {
1160 if (rc == -EAGAIN) {
1161 rd_desc->count = conn->in.datalen -
1162 conn->in.ctask->data_count;
1163 goto again;
1164 }
1165 iscsi_conn_failure(conn, rc);
1166 return 0;
1167 }
1168 conn->in.copy -= conn->in.padding;
1169 conn->in.offset += conn->in.padding;
1170 if (conn->datadgst_en &&
1171 conn->in.opcode == ISCSI_OP_SCSI_DATA_IN) {
1172 if (conn->in.padding) {
1173 debug_tcp("padding -> %d\n", conn->in.padding);
1174 memset(pad, 0, conn->in.padding);
1175 sg_init_one(&sg, pad, conn->in.padding);
1176 crypto_digest_update(conn->data_rx_tfm, &sg, 1);
1177 }
1178 crypto_digest_final(conn->data_rx_tfm,
1179 (u8 *) & conn->in.datadgst);
1180 debug_tcp("rx digest 0x%x\n", conn->in.datadgst);
1181 conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
1182 } else
1183 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1184 }
1185
1186 debug_tcp("f, processed %d from out of %d padding %d\n",
1187 conn->in.offset - offset, (int)len, conn->in.padding);
1188 BUG_ON(conn->in.offset - offset > len);
1189
1190 if (conn->in.offset - offset != len) {
1191 debug_tcp("continue to process %d bytes\n",
1192 (int)len - (conn->in.offset - offset));
1193 goto more;
1194 }
1195
1196nomore:
1197 processed = conn->in.offset - offset;
1198 BUG_ON(processed == 0);
1199 return processed;
1200
1201again:
1202 processed = conn->in.offset - offset;
1203 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
1204 processed, (int)len, (int)rd_desc->count);
1205 BUG_ON(processed == 0);
1206 BUG_ON(processed > len);
1207
1208 conn->rxdata_octets += processed;
1209 return processed;
1210}
1211
1212static void
1213iscsi_tcp_data_ready(struct sock *sk, int flag)
1214{
1215 struct iscsi_conn *conn = sk->sk_user_data;
1216 read_descriptor_t rd_desc;
1217
1218 read_lock(&sk->sk_callback_lock);
1219
1220 /* use rd_desc to pass 'conn' to iscsi_tcp_data_recv */
1221 rd_desc.arg.data = conn;
1222 rd_desc.count = 0;
1223 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
1224
1225 read_unlock(&sk->sk_callback_lock);
1226}
1227
1228static void
1229iscsi_tcp_state_change(struct sock *sk)
1230{
1231 struct iscsi_conn *conn;
1232 struct iscsi_session *session;
1233 void (*old_state_change)(struct sock *);
1234
1235 read_lock(&sk->sk_callback_lock);
1236
1237 conn = (struct iscsi_conn*)sk->sk_user_data;
1238 session = conn->session;
1239
1240 if (sk->sk_state == TCP_CLOSE_WAIT ||
1241 sk->sk_state == TCP_CLOSE) {
1242 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1243 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1244 }
1245
1246 old_state_change = conn->old_state_change;
1247
1248 read_unlock(&sk->sk_callback_lock);
1249
1250 old_state_change(sk);
1251}
1252
1253/**
1254 * iscsi_write_space - Called when more output buffer space is available
1255 * @sk: socket space is available for
1256 **/
1257static void
1258iscsi_write_space(struct sock *sk)
1259{
1260 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
1261 conn->old_write_space(sk);
1262 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
1263 clear_bit(SUSPEND_BIT, &conn->suspend_tx);
1264 schedule_work(&conn->xmitwork);
1265}
1266
1267static void
1268iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1269{
1270 struct sock *sk = conn->sock->sk;
1271
1272 /* assign new callbacks */
1273 write_lock_bh(&sk->sk_callback_lock);
1274 sk->sk_user_data = conn;
1275 conn->old_data_ready = sk->sk_data_ready;
1276 conn->old_state_change = sk->sk_state_change;
1277 conn->old_write_space = sk->sk_write_space;
1278 sk->sk_data_ready = iscsi_tcp_data_ready;
1279 sk->sk_state_change = iscsi_tcp_state_change;
1280 sk->sk_write_space = iscsi_write_space;
1281 write_unlock_bh(&sk->sk_callback_lock);
1282}
1283
1284static void
1285iscsi_conn_restore_callbacks(struct iscsi_conn *conn)
1286{
1287 struct sock *sk = conn->sock->sk;
1288
1289 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1290 write_lock_bh(&sk->sk_callback_lock);
1291 sk->sk_user_data = NULL;
1292 sk->sk_data_ready = conn->old_data_ready;
1293 sk->sk_state_change = conn->old_state_change;
1294 sk->sk_write_space = conn->old_write_space;
1295 sk->sk_no_check = 0;
1296 write_unlock_bh(&sk->sk_callback_lock);
1297}
1298
1299/**
1300 * iscsi_send - generic send routine
1301 * @sk: kernel's socket
1302 * @buf: buffer to write from
1303 * @size: actual size to write
1304 * @flags: socket's flags
1305 *
1306 * Notes:
1307 * depending on buffer will use tcp_sendpage() or tcp_sendmsg().
1308 * buf->sg.offset == -1 tells us that buffer is non S/G and forces
1309 * to use tcp_sendmsg().
1310 */
1311static inline int
1312iscsi_send(struct socket *sk, struct iscsi_buf *buf, int size, int flags)
1313{
1314 int res;
1315
1316 if ((int)buf->sg.offset >= 0) {
1317 int offset = buf->sg.offset + buf->sent;
1318
1319 /* tcp_sendpage */
1320 res = sk->ops->sendpage(sk, buf->sg.page, offset, size, flags);
1321 } else {
1322 struct msghdr msg;
1323
1324 buf->iov.iov_base = iscsi_buf_iov_base(buf);
1325 buf->iov.iov_len = size;
1326
1327 memset(&msg, 0, sizeof(struct msghdr));
1328
1329 /* tcp_sendmsg */
1330 res = kernel_sendmsg(sk, &msg, &buf->iov, 1, size);
1331 }
1332
1333 return res;
1334}
1335
1336/**
1337 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1338 * @conn: iscsi connection
1339 * @buf: buffer to write from
1340 * @datalen: lenght of data to be sent after the header
1341 *
1342 * Notes:
1343 * (Tx, Fast Path)
1344 **/
1345static inline int
1346iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1347{
1348 struct socket *sk = conn->sock;
1349 int flags = 0; /* MSG_DONTWAIT; */
1350 int res, size;
1351
1352 size = buf->sg.length - buf->sent;
1353 BUG_ON(buf->sent + size > buf->sg.length);
1354 if (buf->sent + size != buf->sg.length || datalen)
1355 flags |= MSG_MORE;
1356
1357 res = iscsi_send(sk, buf, size, flags);
1358 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1359 if (res >= 0) {
1360 conn->txdata_octets += res;
1361 buf->sent += res;
1362 if (size != res)
1363 return -EAGAIN;
1364 return 0;
1365 } else if (res == -EAGAIN) {
1366 conn->sendpage_failures_cnt++;
1367 set_bit(SUSPEND_BIT, &conn->suspend_tx);
1368 } else if (res == -EPIPE)
1369 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1370
1371 return res;
1372}
1373
1374/**
1375 * iscsi_sendpage - send one page of iSCSI Data-Out.
1376 * @conn: iscsi connection
1377 * @buf: buffer to write from
1378 * @count: remaining data
1379 * @sent: number of bytes sent
1380 *
1381 * Notes:
1382 * (Tx, Fast Path)
1383 **/
1384static inline int
1385iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1386 int *count, int *sent)
1387{
1388 struct socket *sk = conn->sock;
1389 int flags = 0; /* MSG_DONTWAIT; */
1390 int res, size;
1391
1392 size = buf->sg.length - buf->sent;
1393 BUG_ON(buf->sent + size > buf->sg.length);
1394 if (size > *count)
1395 size = *count;
1396 if (buf->sent + size != buf->sg.length || *count != size)
1397 flags |= MSG_MORE;
1398
1399 res = iscsi_send(sk, buf, size, flags);
1400 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1401 size, buf->sent, *count, *sent, res);
1402 if (res >= 0) {
1403 conn->txdata_octets += res;
1404 buf->sent += res;
1405 *count -= res;
1406 *sent += res;
1407 if (size != res)
1408 return -EAGAIN;
1409 return 0;
1410 } else if (res == -EAGAIN) {
1411 conn->sendpage_failures_cnt++;
1412 set_bit(SUSPEND_BIT, &conn->suspend_tx);
1413 } else if (res == -EPIPE)
1414 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1415
1416 return res;
1417}
1418
1419static inline void
1420iscsi_data_digest_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1421{
1422 BUG_ON(!conn->data_tx_tfm);
1423 crypto_digest_init(conn->data_tx_tfm);
1424 ctask->digest_count = 4;
1425}
1426
1427static inline void
1428iscsi_buf_data_digest_update(struct iscsi_conn *conn, struct iscsi_buf *buf)
1429{
1430 struct scatterlist sg;
1431
1432 if (buf->sg.offset != -1)
1433 crypto_digest_update(conn->data_tx_tfm, &buf->sg, 1);
1434 else {
1435 sg_init_one(&sg, (char *)buf->sg.page, buf->sg.length);
1436 crypto_digest_update(conn->data_tx_tfm, &sg, 1);
1437 }
1438}
1439
1440static inline int
1441iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1442 struct iscsi_buf *buf, uint32_t *digest, int final)
1443{
1444 int rc = 0;
1445 int sent = 0;
1446
1447 if (final)
1448 crypto_digest_final(conn->data_tx_tfm, (u8*)digest);
1449
1450 iscsi_buf_init_virt(buf, (char*)digest, 4);
1451 rc = iscsi_sendpage(conn, buf, &ctask->digest_count, &sent);
1452 if (rc) {
1453 ctask->datadigest = *digest;
1454 ctask->xmstate |= XMSTATE_DATA_DIGEST;
1455 } else
1456 ctask->digest_count = 4;
1457 return rc;
1458}
1459
1460/**
1461 * iscsi_solicit_data_cont - initialize next Data-Out
1462 * @conn: iscsi connection
1463 * @ctask: scsi command task
1464 * @r2t: R2T info
1465 * @left: bytes left to transfer
1466 *
1467 * Notes:
1468 * Initialize next Data-Out within this R2T sequence and continue
1469 * to process next Scatter-Gather element(if any) of this SCSI command.
1470 *
1471 * Called under connection lock.
1472 **/
1473static void
1474iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1475 struct iscsi_r2t_info *r2t, int left)
1476{
1477 struct iscsi_data *hdr;
1478 struct iscsi_data_task *dtask;
1479 struct scsi_cmnd *sc = ctask->sc;
1480 int new_offset;
1481
1482 dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC);
1483 BUG_ON(!dtask);
1484 hdr = &dtask->hdr;
1485 memset(hdr, 0, sizeof(struct iscsi_data));
1486 hdr->ttt = r2t->ttt;
1487 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1488 r2t->solicit_datasn++;
1489 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
1490 memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
1491 hdr->itt = ctask->hdr.itt;
1492 hdr->exp_statsn = r2t->exp_statsn;
1493 new_offset = r2t->data_offset + r2t->sent;
1494 hdr->offset = cpu_to_be32(new_offset);
1495 if (left > conn->max_xmit_dlength) {
1496 hton24(hdr->dlength, conn->max_xmit_dlength);
1497 r2t->data_count = conn->max_xmit_dlength;
1498 } else {
1499 hton24(hdr->dlength, left);
1500 r2t->data_count = left;
1501 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1502 }
1503 conn->dataout_pdus_cnt++;
1504
1505 iscsi_buf_init_virt(&r2t->headbuf, (char*)hdr,
1506 sizeof(struct iscsi_hdr));
1507
1508 r2t->dtask = dtask;
1509
1510 if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) {
1511 BUG_ON(ctask->bad_sg == r2t->sg);
1512 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1513 r2t->sg += 1;
1514 } else
1515 iscsi_buf_init_iov(&ctask->sendbuf,
1516 (char*)sc->request_buffer + new_offset,
1517 r2t->data_count);
1518
1519 list_add(&dtask->item, &ctask->dataqueue);
1520}
1521
1522static void
1523iscsi_unsolicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1524{
1525 struct iscsi_data *hdr;
1526 struct iscsi_data_task *dtask;
1527
1528 dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC);
1529 BUG_ON(!dtask);
1530 hdr = &dtask->hdr;
1531 memset(hdr, 0, sizeof(struct iscsi_data));
1532 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
1533 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
1534 ctask->unsol_datasn++;
1535 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
1536 memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
1537 hdr->itt = ctask->hdr.itt;
1538 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
1539 hdr->offset = cpu_to_be32(ctask->total_length -
1540 ctask->r2t_data_count -
1541 ctask->unsol_count);
1542 if (ctask->unsol_count > conn->max_xmit_dlength) {
1543 hton24(hdr->dlength, conn->max_xmit_dlength);
1544 ctask->data_count = conn->max_xmit_dlength;
1545 hdr->flags = 0;
1546 } else {
1547 hton24(hdr->dlength, ctask->unsol_count);
1548 ctask->data_count = ctask->unsol_count;
1549 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1550 }
1551
1552 iscsi_buf_init_virt(&ctask->headbuf, (char*)hdr,
1553 sizeof(struct iscsi_hdr));
1554
1555 list_add(&dtask->item, &ctask->dataqueue);
1556
1557 ctask->dtask = dtask;
1558}
1559
1560/**
1561 * iscsi_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
1562 * @conn: iscsi connection
1563 * @ctask: scsi command task
1564 * @sc: scsi command
1565 **/
1566static void
1567iscsi_cmd_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1568 struct scsi_cmnd *sc)
1569{
1570 struct iscsi_session *session = conn->session;
1571
1572 BUG_ON(__kfifo_len(ctask->r2tqueue));
1573
1574 ctask->sc = sc;
1575 ctask->conn = conn;
1576 ctask->hdr.opcode = ISCSI_OP_SCSI_CMD;
1577 ctask->hdr.flags = ISCSI_ATTR_SIMPLE;
1578 int_to_scsilun(sc->device->lun, (struct scsi_lun *)ctask->hdr.lun);
1579 ctask->hdr.itt = ctask->itt | (conn->id << CID_SHIFT) |
1580 (session->age << AGE_SHIFT);
1581 ctask->hdr.data_length = cpu_to_be32(sc->request_bufflen);
1582 ctask->hdr.cmdsn = cpu_to_be32(session->cmdsn); session->cmdsn++;
1583 ctask->hdr.exp_statsn = cpu_to_be32(conn->exp_statsn);
1584 memcpy(ctask->hdr.cdb, sc->cmnd, sc->cmd_len);
1585 memset(&ctask->hdr.cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
1586
1587 ctask->mtask = NULL;
1588 ctask->sent = 0;
1589 ctask->sg_count = 0;
1590
1591 ctask->total_length = sc->request_bufflen;
1592
1593 if (sc->sc_data_direction == DMA_TO_DEVICE) {
1594 ctask->exp_r2tsn = 0;
1595 ctask->hdr.flags |= ISCSI_FLAG_CMD_WRITE;
1596 BUG_ON(ctask->total_length == 0);
1597 if (sc->use_sg) {
1598 struct scatterlist *sg = sc->request_buffer;
1599
1600 iscsi_buf_init_sg(&ctask->sendbuf,
1601 &sg[ctask->sg_count++]);
1602 ctask->sg = sg;
1603 ctask->bad_sg = sg + sc->use_sg;
1604 } else {
1605 iscsi_buf_init_iov(&ctask->sendbuf, sc->request_buffer,
1606 sc->request_bufflen);
1607 }
1608
1609 /*
1610 * Write counters:
1611 *
1612 * imm_count bytes to be sent right after
1613 * SCSI PDU Header
1614 *
1615 * unsol_count bytes(as Data-Out) to be sent
1616 * without R2T ack right after
1617 * immediate data
1618 *
1619 * r2t_data_count bytes to be sent via R2T ack's
1620 *
1621 * pad_count bytes to be sent as zero-padding
1622 */
1623 ctask->imm_count = 0;
1624 ctask->unsol_count = 0;
1625 ctask->unsol_datasn = 0;
1626 ctask->xmstate = XMSTATE_W_HDR;
1627 /* calculate write padding */
1628 ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1);
1629 if (ctask->pad_count) {
1630 ctask->pad_count = ISCSI_PAD_LEN - ctask->pad_count;
1631 debug_scsi("write padding %d bytes\n",
1632 ctask->pad_count);
1633 ctask->xmstate |= XMSTATE_W_PAD;
1634 }
1635 if (session->imm_data_en) {
1636 if (ctask->total_length >= session->first_burst)
1637 ctask->imm_count = min(session->first_burst,
1638 conn->max_xmit_dlength);
1639 else
1640 ctask->imm_count = min(ctask->total_length,
1641 conn->max_xmit_dlength);
1642 hton24(ctask->hdr.dlength, ctask->imm_count);
1643 ctask->xmstate |= XMSTATE_IMM_DATA;
1644 } else
1645 zero_data(ctask->hdr.dlength);
1646
1647 if (!session->initial_r2t_en)
1648 ctask->unsol_count = min(session->first_burst,
1649 ctask->total_length) - ctask->imm_count;
1650 if (!ctask->unsol_count)
1651 /* No unsolicit Data-Out's */
1652 ctask->hdr.flags |= ISCSI_FLAG_CMD_FINAL;
1653 else
1654 ctask->xmstate |= XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
1655
1656 ctask->r2t_data_count = ctask->total_length -
1657 ctask->imm_count -
1658 ctask->unsol_count;
1659
1660 debug_scsi("cmd [itt %x total %d imm %d imm_data %d "
1661 "r2t_data %d]\n",
1662 ctask->itt, ctask->total_length, ctask->imm_count,
1663 ctask->unsol_count, ctask->r2t_data_count);
1664 } else {
1665 ctask->hdr.flags |= ISCSI_FLAG_CMD_FINAL;
1666 if (sc->sc_data_direction == DMA_FROM_DEVICE)
1667 ctask->hdr.flags |= ISCSI_FLAG_CMD_READ;
1668 ctask->datasn = 0;
1669 ctask->xmstate = XMSTATE_R_HDR;
1670 zero_data(ctask->hdr.dlength);
1671 }
1672
1673 iscsi_buf_init_virt(&ctask->headbuf, (char*)&ctask->hdr,
1674 sizeof(struct iscsi_hdr));
1675 conn->scsicmd_pdus_cnt++;
1676}
1677
1678/**
1679 * iscsi_mtask_xmit - xmit management(immediate) task
1680 * @conn: iscsi connection
1681 * @mtask: task management task
1682 *
1683 * Notes:
1684 * The function can return -EAGAIN in which case caller must
1685 * call it again later, or recover. '0' return code means successful
1686 * xmit.
1687 *
1688 * Management xmit state machine consists of two states:
1689 * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
1690 * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
1691 **/
1692static int
1693iscsi_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1694{
1695
1696 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
1697 conn->id, mtask->xmstate, mtask->itt);
1698
1699 if (mtask->xmstate & XMSTATE_IMM_HDR) {
1700 mtask->xmstate &= ~XMSTATE_IMM_HDR;
1701 if (mtask->data_count)
1702 mtask->xmstate |= XMSTATE_IMM_DATA;
1703 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
1704 conn->stop_stage != STOP_CONN_RECOVER &&
1705 conn->hdrdgst_en)
1706 iscsi_hdr_digest(conn, &mtask->headbuf,
1707 (u8*)mtask->hdrext);
1708 if (iscsi_sendhdr(conn, &mtask->headbuf, mtask->data_count)) {
1709 mtask->xmstate |= XMSTATE_IMM_HDR;
1710 if (mtask->data_count)
1711 mtask->xmstate &= ~XMSTATE_IMM_DATA;
1712 return -EAGAIN;
1713 }
1714 }
1715
1716 if (mtask->xmstate & XMSTATE_IMM_DATA) {
1717 BUG_ON(!mtask->data_count);
1718 mtask->xmstate &= ~XMSTATE_IMM_DATA;
1719 /* FIXME: implement.
1720 * Virtual buffer could be spreaded across multiple pages...
1721 */
1722 do {
1723 if (iscsi_sendpage(conn, &mtask->sendbuf,
1724 &mtask->data_count, &mtask->sent)) {
1725 mtask->xmstate |= XMSTATE_IMM_DATA;
1726 return -EAGAIN;
1727 }
1728 } while (mtask->data_count);
1729 }
1730
1731 BUG_ON(mtask->xmstate != XMSTATE_IDLE);
1732 return 0;
1733}
1734
1735static inline int
1736handle_xmstate_r_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1737{
1738 ctask->xmstate &= ~XMSTATE_R_HDR;
1739 if (conn->hdrdgst_en)
1740 iscsi_hdr_digest(conn, &ctask->headbuf, (u8*)ctask->hdrext);
1741 if (!iscsi_sendhdr(conn, &ctask->headbuf, 0)) {
1742 BUG_ON(ctask->xmstate != XMSTATE_IDLE);
1743 return 0; /* wait for Data-In */
1744 }
1745 ctask->xmstate |= XMSTATE_R_HDR;
1746 return -EAGAIN;
1747}
1748
1749static inline int
1750handle_xmstate_w_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1751{
1752 ctask->xmstate &= ~XMSTATE_W_HDR;
1753 if (conn->hdrdgst_en)
1754 iscsi_hdr_digest(conn, &ctask->headbuf, (u8*)ctask->hdrext);
1755 if (iscsi_sendhdr(conn, &ctask->headbuf, ctask->imm_count)) {
1756 ctask->xmstate |= XMSTATE_W_HDR;
1757 return -EAGAIN;
1758 }
1759 return 0;
1760}
1761
1762static inline int
1763handle_xmstate_data_digest(struct iscsi_conn *conn,
1764 struct iscsi_cmd_task *ctask)
1765{
1766 ctask->xmstate &= ~XMSTATE_DATA_DIGEST;
1767 debug_tcp("resent data digest 0x%x\n", ctask->datadigest);
1768 if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf,
1769 &ctask->datadigest, 0)) {
1770 ctask->xmstate |= XMSTATE_DATA_DIGEST;
1771 debug_tcp("resent data digest 0x%x fail!\n",
1772 ctask->datadigest);
1773 return -EAGAIN;
1774 }
1775 return 0;
1776}
1777
1778static inline int
1779handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1780{
1781 BUG_ON(!ctask->imm_count);
1782 ctask->xmstate &= ~XMSTATE_IMM_DATA;
1783
1784 if (conn->datadgst_en) {
1785 iscsi_data_digest_init(conn, ctask);
1786 ctask->immdigest = 0;
1787 }
1788
1789 for (;;) {
1790 if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->imm_count,
1791 &ctask->sent)) {
1792 ctask->xmstate |= XMSTATE_IMM_DATA;
1793 if (conn->datadgst_en) {
1794 crypto_digest_final(conn->data_tx_tfm,
1795 (u8*)&ctask->immdigest);
1796 debug_tcp("tx imm sendpage fail 0x%x\n",
1797 ctask->datadigest);
1798 }
1799 return -EAGAIN;
1800 }
1801 if (conn->datadgst_en)
1802 iscsi_buf_data_digest_update(conn, &ctask->sendbuf);
1803
1804 if (!ctask->imm_count)
1805 break;
1806 iscsi_buf_init_sg(&ctask->sendbuf,
1807 &ctask->sg[ctask->sg_count++]);
1808 }
1809
1810 if (conn->datadgst_en && !(ctask->xmstate & XMSTATE_W_PAD)) {
1811 if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf,
1812 &ctask->immdigest, 1)) {
1813 debug_tcp("sending imm digest 0x%x fail!\n",
1814 ctask->immdigest);
1815 return -EAGAIN;
1816 }
1817 debug_tcp("sending imm digest 0x%x\n", ctask->immdigest);
1818 }
1819
1820 return 0;
1821}
1822
1823static inline int
1824handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1825{
1826 struct iscsi_data_task *dtask;
1827
1828 ctask->xmstate |= XMSTATE_UNS_DATA;
1829 if (ctask->xmstate & XMSTATE_UNS_INIT) {
1830 iscsi_unsolicit_data_init(conn, ctask);
1831 BUG_ON(!ctask->dtask);
1832 dtask = ctask->dtask;
1833 if (conn->hdrdgst_en)
1834 iscsi_hdr_digest(conn, &ctask->headbuf,
1835 (u8*)dtask->hdrext);
1836 ctask->xmstate &= ~XMSTATE_UNS_INIT;
1837 }
1838 if (iscsi_sendhdr(conn, &ctask->headbuf, ctask->data_count)) {
1839 ctask->xmstate &= ~XMSTATE_UNS_DATA;
1840 ctask->xmstate |= XMSTATE_UNS_HDR;
1841 return -EAGAIN;
1842 }
1843
1844 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
1845 ctask->itt, ctask->unsol_count, ctask->sent);
1846 return 0;
1847}
1848
1849static inline int
1850handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1851{
1852 struct iscsi_data_task *dtask = ctask->dtask;
1853
1854 BUG_ON(!ctask->data_count);
1855 ctask->xmstate &= ~XMSTATE_UNS_DATA;
1856
1857 if (conn->datadgst_en) {
1858 iscsi_data_digest_init(conn, ctask);
1859 dtask->digest = 0;
1860 }
1861
1862 for (;;) {
1863 int start = ctask->sent;
1864
1865 if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->data_count,
1866 &ctask->sent)) {
1867 ctask->unsol_count -= ctask->sent - start;
1868 ctask->xmstate |= XMSTATE_UNS_DATA;
1869 /* will continue with this ctask later.. */
1870 if (conn->datadgst_en) {
1871 crypto_digest_final(conn->data_tx_tfm,
1872 (u8 *)&dtask->digest);
1873 debug_tcp("tx uns data fail 0x%x\n",
1874 dtask->digest);
1875 }
1876 return -EAGAIN;
1877 }
1878
1879 BUG_ON(ctask->sent > ctask->total_length);
1880 ctask->unsol_count -= ctask->sent - start;
1881
1882 /*
1883 * XXX:we may run here with un-initial sendbuf.
1884 * so pass it
1885 */
1886 if (conn->datadgst_en && ctask->sent - start > 0)
1887 iscsi_buf_data_digest_update(conn, &ctask->sendbuf);
1888
1889 if (!ctask->data_count)
1890 break;
1891 iscsi_buf_init_sg(&ctask->sendbuf,
1892 &ctask->sg[ctask->sg_count++]);
1893 }
1894 BUG_ON(ctask->unsol_count < 0);
1895
1896 /*
1897 * Done with the Data-Out. Next, check if we need
1898 * to send another unsolicited Data-Out.
1899 */
1900 if (ctask->unsol_count) {
1901 if (conn->datadgst_en) {
1902 if (iscsi_digest_final_send(conn, ctask,
1903 &dtask->digestbuf,
1904 &dtask->digest, 1)) {
1905 debug_tcp("send uns digest 0x%x fail\n",
1906 dtask->digest);
1907 return -EAGAIN;
1908 }
1909 debug_tcp("sending uns digest 0x%x, more uns\n",
1910 dtask->digest);
1911 }
1912 ctask->xmstate |= XMSTATE_UNS_INIT;
1913 return 1;
1914 }
1915
1916 if (conn->datadgst_en && !(ctask->xmstate & XMSTATE_W_PAD)) {
1917 if (iscsi_digest_final_send(conn, ctask,
1918 &dtask->digestbuf,
1919 &dtask->digest, 1)) {
1920 debug_tcp("send last uns digest 0x%x fail\n",
1921 dtask->digest);
1922 return -EAGAIN;
1923 }
1924 debug_tcp("sending uns digest 0x%x\n",dtask->digest);
1925 }
1926
1927 return 0;
1928}
1929
1930static inline int
1931handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1932{
1933 struct iscsi_session *session = conn->session;
1934 struct iscsi_r2t_info *r2t = ctask->r2t;
1935 struct iscsi_data_task *dtask = r2t->dtask;
1936 int left;
1937
1938 ctask->xmstate &= ~XMSTATE_SOL_DATA;
1939 ctask->dtask = dtask;
1940
1941 if (conn->datadgst_en) {
1942 iscsi_data_digest_init(conn, ctask);
1943 dtask->digest = 0;
1944 }
1945solicit_again:
1946 /*
1947 * send Data-Out whitnin this R2T sequence.
1948 */
1949 if (!r2t->data_count)
1950 goto data_out_done;
1951
1952 if (iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent)) {
1953 ctask->xmstate |= XMSTATE_SOL_DATA;
1954 /* will continue with this ctask later.. */
1955 if (conn->datadgst_en) {
1956 crypto_digest_final(conn->data_tx_tfm,
1957 (u8 *)&dtask->digest);
1958 debug_tcp("r2t data send fail 0x%x\n", dtask->digest);
1959 }
1960 return -EAGAIN;
1961 }
1962
1963 BUG_ON(r2t->data_count < 0);
1964 if (conn->datadgst_en)
1965 iscsi_buf_data_digest_update(conn, &r2t->sendbuf);
1966
1967 if (r2t->data_count) {
1968 BUG_ON(ctask->sc->use_sg == 0);
1969 if (!iscsi_buf_left(&r2t->sendbuf)) {
1970 BUG_ON(ctask->bad_sg == r2t->sg);
1971 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1972 r2t->sg += 1;
1973 }
1974 goto solicit_again;
1975 }
1976
1977data_out_done:
1978 /*
1979 * Done with this Data-Out. Next, check if we have
1980 * to send another Data-Out for this R2T.
1981 */
1982 BUG_ON(r2t->data_length - r2t->sent < 0);
1983 left = r2t->data_length - r2t->sent;
1984 if (left) {
1985 if (conn->datadgst_en) {
1986 if (iscsi_digest_final_send(conn, ctask,
1987 &dtask->digestbuf,
1988 &dtask->digest, 1)) {
1989 debug_tcp("send r2t data digest 0x%x"
1990 "fail\n", dtask->digest);
1991 return -EAGAIN;
1992 }
1993 debug_tcp("r2t data send digest 0x%x\n",
1994 dtask->digest);
1995 }
1996 iscsi_solicit_data_cont(conn, ctask, r2t, left);
1997 ctask->xmstate |= XMSTATE_SOL_DATA;
1998 ctask->xmstate &= ~XMSTATE_SOL_HDR;
1999 return 1;
2000 }
2001
2002 /*
2003 * Done with this R2T. Check if there are more
2004 * outstanding R2Ts ready to be processed.
2005 */
2006 BUG_ON(ctask->r2t_data_count - r2t->data_length < 0);
2007 if (conn->datadgst_en) {
2008 if (iscsi_digest_final_send(conn, ctask, &dtask->digestbuf,
2009 &dtask->digest, 1)) {
2010 debug_tcp("send last r2t data digest 0x%x"
2011 "fail\n", dtask->digest);
2012 return -EAGAIN;
2013 }
2014 debug_tcp("r2t done dout digest 0x%x\n", dtask->digest);
2015 }
2016
2017 ctask->r2t_data_count -= r2t->data_length;
2018 ctask->r2t = NULL;
2019 spin_lock_bh(&session->lock);
2020 __kfifo_put(ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
2021 spin_unlock_bh(&session->lock);
2022 if (__kfifo_get(ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
2023 ctask->r2t = r2t;
2024 ctask->xmstate |= XMSTATE_SOL_DATA;
2025 ctask->xmstate &= ~XMSTATE_SOL_HDR;
2026 return 1;
2027 }
2028
2029 return 0;
2030}
2031
2032static inline int
2033handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
2034{
2035 struct iscsi_data_task *dtask = ctask->dtask;
2036 int sent;
2037
2038 ctask->xmstate &= ~XMSTATE_W_PAD;
2039 iscsi_buf_init_virt(&ctask->sendbuf, (char*)&ctask->pad,
2040 ctask->pad_count);
2041 if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->pad_count, &sent)) {
2042 ctask->xmstate |= XMSTATE_W_PAD;
2043 return -EAGAIN;
2044 }
2045
2046 if (conn->datadgst_en) {
2047 iscsi_buf_data_digest_update(conn, &ctask->sendbuf);
2048 /* imm data? */
2049 if (!dtask) {
2050 if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf,
2051 &ctask->immdigest, 1)) {
2052 debug_tcp("send padding digest 0x%x"
2053 "fail!\n", ctask->immdigest);
2054 return -EAGAIN;
2055 }
2056 debug_tcp("done with padding, digest 0x%x\n",
2057 ctask->datadigest);
2058 } else {
2059 if (iscsi_digest_final_send(conn, ctask,
2060 &dtask->digestbuf,
2061 &dtask->digest, 1)) {
2062 debug_tcp("send padding digest 0x%x"
2063 "fail\n", dtask->digest);
2064 return -EAGAIN;
2065 }
2066 debug_tcp("done with padding, digest 0x%x\n",
2067 dtask->digest);
2068 }
2069 }
2070
2071 return 0;
2072}
2073
2074static int
2075iscsi_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
2076{
2077 int rc = 0;
2078
2079 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
2080 conn->id, ctask->xmstate, ctask->itt);
2081
2082 /*
2083 * serialize with TMF AbortTask
2084 */
2085 if (ctask->mtask)
2086 return rc;
2087
2088 if (ctask->xmstate & XMSTATE_R_HDR) {
2089 rc = handle_xmstate_r_hdr(conn, ctask);
2090 return rc;
2091 }
2092
2093 if (ctask->xmstate & XMSTATE_W_HDR) {
2094 rc = handle_xmstate_w_hdr(conn, ctask);
2095 if (rc)
2096 return rc;
2097 }
2098
2099 /* XXX: for data digest xmit recover */
2100 if (ctask->xmstate & XMSTATE_DATA_DIGEST) {
2101 rc = handle_xmstate_data_digest(conn, ctask);
2102 if (rc)
2103 return rc;
2104 }
2105
2106 if (ctask->xmstate & XMSTATE_IMM_DATA) {
2107 rc = handle_xmstate_imm_data(conn, ctask);
2108 if (rc)
2109 return rc;
2110 }
2111
2112 if (ctask->xmstate & XMSTATE_UNS_HDR) {
2113 BUG_ON(!ctask->unsol_count);
2114 ctask->xmstate &= ~XMSTATE_UNS_HDR;
2115unsolicit_head_again:
2116 rc = handle_xmstate_uns_hdr(conn, ctask);
2117 if (rc)
2118 return rc;
2119 }
2120
2121 if (ctask->xmstate & XMSTATE_UNS_DATA) {
2122 rc = handle_xmstate_uns_data(conn, ctask);
2123 if (rc == 1)
2124 goto unsolicit_head_again;
2125 else if (rc)
2126 return rc;
2127 goto done;
2128 }
2129
2130 if (ctask->xmstate & XMSTATE_SOL_HDR) {
2131 struct iscsi_r2t_info *r2t;
2132
2133 ctask->xmstate &= ~XMSTATE_SOL_HDR;
2134 ctask->xmstate |= XMSTATE_SOL_DATA;
2135 if (!ctask->r2t)
2136 __kfifo_get(ctask->r2tqueue, (void*)&ctask->r2t,
2137 sizeof(void*));
2138solicit_head_again:
2139 r2t = ctask->r2t;
2140 if (conn->hdrdgst_en)
2141 iscsi_hdr_digest(conn, &r2t->headbuf,
2142 (u8*)r2t->dtask->hdrext);
2143 if (iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count)) {
2144 ctask->xmstate &= ~XMSTATE_SOL_DATA;
2145 ctask->xmstate |= XMSTATE_SOL_HDR;
2146 return -EAGAIN;
2147 }
2148
2149 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
2150 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
2151 r2t->sent);
2152 }
2153
2154 if (ctask->xmstate & XMSTATE_SOL_DATA) {
2155 rc = handle_xmstate_sol_data(conn, ctask);
2156 if (rc == 1)
2157 goto solicit_head_again;
2158 if (rc)
2159 return rc;
2160 }
2161
2162done:
2163 /*
2164 * Last thing to check is whether we need to send write
2165 * padding. Note that we check for xmstate equality, not just the bit.
2166 */
2167 if (ctask->xmstate == XMSTATE_W_PAD)
2168 rc = handle_xmstate_w_pad(conn, ctask);
2169
2170 return rc;
2171}
2172
2173/**
2174 * iscsi_data_xmit - xmit any command into the scheduled connection
2175 * @conn: iscsi connection
2176 *
2177 * Notes:
2178 * The function can return -EAGAIN in which case the caller must
2179 * re-schedule it again later or recover. '0' return code means
2180 * successful xmit.
2181 **/
2182static int
2183iscsi_data_xmit(struct iscsi_conn *conn)
2184{
2185 if (unlikely(conn->suspend_tx)) {
2186 debug_tcp("conn %d Tx suspended!\n", conn->id);
2187 return 0;
2188 }
2189
2190 /*
2191 * Transmit in the following order:
2192 *
2193 * 1) un-finished xmit (ctask or mtask)
2194 * 2) immediate control PDUs
2195 * 3) write data
2196 * 4) SCSI commands
2197 * 5) non-immediate control PDUs
2198 *
2199 * No need to lock around __kfifo_get as long as
2200 * there's one producer and one consumer.
2201 */
2202
2203 BUG_ON(conn->ctask && conn->mtask);
2204
2205 if (conn->ctask) {
2206 if (iscsi_ctask_xmit(conn, conn->ctask))
2207 goto again;
2208 /* done with this in-progress ctask */
2209 conn->ctask = NULL;
2210 }
2211 if (conn->mtask) {
2212 if (iscsi_mtask_xmit(conn, conn->mtask))
2213 goto again;
2214 /* done with this in-progress mtask */
2215 conn->mtask = NULL;
2216 }
2217
2218 /* process immediate first */
2219 if (unlikely(__kfifo_len(conn->immqueue))) {
2220 struct iscsi_session *session = conn->session;
2221 while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
2222 sizeof(void*))) {
2223 if (iscsi_mtask_xmit(conn, conn->mtask))
2224 goto again;
2225
2226 if (conn->mtask->hdr.itt ==
2227 cpu_to_be32(ISCSI_RESERVED_TAG)) {
2228 spin_lock_bh(&session->lock);
2229 __kfifo_put(session->mgmtpool.queue,
2230 (void*)&conn->mtask, sizeof(void*));
2231 spin_unlock_bh(&session->lock);
2232 }
2233 }
2234 /* done with this mtask */
2235 conn->mtask = NULL;
2236 }
2237
2238 /* process write queue */
2239 while (__kfifo_get(conn->writequeue, (void*)&conn->ctask,
2240 sizeof(void*))) {
2241 if (iscsi_ctask_xmit(conn, conn->ctask))
2242 goto again;
2243 }
2244
2245 /* process command queue */
2246 while (__kfifo_get(conn->xmitqueue, (void*)&conn->ctask,
2247 sizeof(void*))) {
2248 if (iscsi_ctask_xmit(conn, conn->ctask))
2249 goto again;
2250 }
2251 /* done with this ctask */
2252 conn->ctask = NULL;
2253
2254 /* process the rest control plane PDUs, if any */
2255 if (unlikely(__kfifo_len(conn->mgmtqueue))) {
2256 struct iscsi_session *session = conn->session;
2257
2258 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
2259 sizeof(void*))) {
2260 if (iscsi_mtask_xmit(conn, conn->mtask))
2261 goto again;
2262
2263 if (conn->mtask->hdr.itt ==
2264 cpu_to_be32(ISCSI_RESERVED_TAG)) {
2265 spin_lock_bh(&session->lock);
2266 __kfifo_put(session->mgmtpool.queue,
2267 (void*)&conn->mtask,
2268 sizeof(void*));
2269 spin_unlock_bh(&session->lock);
2270 }
2271 }
2272 /* done with this mtask */
2273 conn->mtask = NULL;
2274 }
2275
2276 return 0;
2277
2278again:
2279 if (unlikely(conn->suspend_tx))
2280 return 0;
2281
2282 return -EAGAIN;
2283}
2284
2285static void
2286iscsi_xmitworker(void *data)
2287{
2288 struct iscsi_conn *conn = data;
2289
2290 /*
2291 * serialize Xmit worker on a per-connection basis.
2292 */
2293 down(&conn->xmitsema);
2294 if (iscsi_data_xmit(conn))
2295 schedule_work(&conn->xmitwork);
2296 up(&conn->xmitsema);
2297}
2298
2299#define FAILURE_BAD_HOST 1
2300#define FAILURE_SESSION_FAILED 2
2301#define FAILURE_SESSION_FREED 3
2302#define FAILURE_WINDOW_CLOSED 4
2303#define FAILURE_SESSION_TERMINATE 5
2304
2305static int
2306iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
2307{
2308 struct Scsi_Host *host;
2309 int reason = 0;
2310 struct iscsi_session *session;
2311 struct iscsi_conn *conn = NULL;
2312 struct iscsi_cmd_task *ctask = NULL;
2313
2314 sc->scsi_done = done;
2315 sc->result = 0;
2316
2317 host = sc->device->host;
2318 session = iscsi_hostdata(host->hostdata);
2319 BUG_ON(host != session->host);
2320
2321 spin_lock(&session->lock);
2322
2323 if (session->state != ISCSI_STATE_LOGGED_IN) {
2324 if (session->state == ISCSI_STATE_FAILED) {
2325 reason = FAILURE_SESSION_FAILED;
2326 goto reject;
2327 } else if (session->state == ISCSI_STATE_TERMINATE) {
2328 reason = FAILURE_SESSION_TERMINATE;
2329 goto fault;
2330 }
2331 reason = FAILURE_SESSION_FREED;
2332 goto fault;
2333 }
2334
2335 /*
2336 * Check for iSCSI window and take care of CmdSN wrap-around
2337 */
2338 if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
2339 reason = FAILURE_WINDOW_CLOSED;
2340 goto reject;
2341 }
2342
2343 conn = session->leadconn;
2344
2345 __kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
2346 BUG_ON(ctask->sc);
2347
2348 sc->SCp.phase = session->age;
2349 sc->SCp.ptr = (char*)ctask;
2350 iscsi_cmd_init(conn, ctask, sc);
2351
2352 __kfifo_put(conn->xmitqueue, (void*)&ctask, sizeof(void*));
2353 debug_scsi(
2354 "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
2355 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
2356 conn->id, (long)sc, ctask->itt, sc->request_bufflen,
2357 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
2358 spin_unlock(&session->lock);
2359
2360 if (!in_interrupt() && !down_trylock(&conn->xmitsema)) {
2361 spin_unlock_irq(host->host_lock);
2362 if (iscsi_data_xmit(conn))
2363 schedule_work(&conn->xmitwork);
2364 up(&conn->xmitsema);
2365 spin_lock_irq(host->host_lock);
2366 } else
2367 schedule_work(&conn->xmitwork);
2368
2369 return 0;
2370
2371reject:
2372 spin_unlock(&session->lock);
2373 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
2374 return SCSI_MLQUEUE_HOST_BUSY;
2375
2376fault:
2377 spin_unlock(&session->lock);
2378 printk(KERN_ERR "iscsi_tcp: cmd 0x%x is not queued (%d)\n",
2379 sc->cmnd[0], reason);
2380 sc->sense_buffer[0] = 0x70;
2381 sc->sense_buffer[2] = NOT_READY;
2382 sc->sense_buffer[7] = 0x6;
2383 sc->sense_buffer[12] = 0x08;
2384 sc->sense_buffer[13] = 0x00;
2385 sc->result = (DID_NO_CONNECT << 16);
2386 sc->resid = sc->request_bufflen;
2387 sc->scsi_done(sc);
2388 return 0;
2389}
2390
2391static int
2392iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
2393{
2394 int i;
2395
2396 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
2397 if (*items == NULL)
2398 return -ENOMEM;
2399
2400 q->max = max;
2401 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
2402 if (q->pool == NULL) {
2403 kfree(*items);
2404 return -ENOMEM;
2405 }
2406
2407 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2408 GFP_KERNEL, NULL);
2409 if (q->queue == ERR_PTR(-ENOMEM)) {
2410 kfree(q->pool);
2411 kfree(*items);
2412 return -ENOMEM;
2413 }
2414
2415 for (i = 0; i < max; i++) {
2416 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
2417 if (q->pool[i] == NULL) {
2418 int j;
2419
2420 for (j = 0; j < i; j++)
2421 kfree(q->pool[j]);
2422
2423 kfifo_free(q->queue);
2424 kfree(q->pool);
2425 kfree(*items);
2426 return -ENOMEM;
2427 }
2428 memset(q->pool[i], 0, item_size);
2429 (*items)[i] = q->pool[i];
2430 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2431 }
2432 return 0;
2433}
2434
2435static void
2436iscsi_pool_free(struct iscsi_queue *q, void **items)
2437{
2438 int i;
2439
2440 for (i = 0; i < q->max; i++)
2441 kfree(items[i]);
2442 kfree(q->pool);
2443 kfree(items);
2444}
2445
2446static iscsi_connh_t
2447iscsi_conn_create(iscsi_sessionh_t sessionh, uint32_t conn_idx)
2448{
2449 struct iscsi_session *session = iscsi_ptr(sessionh);
2450 struct iscsi_conn *conn = NULL;
2451
2452 conn = kmalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
2453 if (conn == NULL)
2454 goto conn_alloc_fail;
2455 memset(conn, 0, sizeof(struct iscsi_conn));
2456
2457 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2458 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
2459 conn->id = conn_idx;
2460 conn->exp_statsn = 0;
2461 conn->tmabort_state = TMABORT_INITIAL;
2462
2463 /* initial operational parameters */
2464 conn->hdr_size = sizeof(struct iscsi_hdr);
2465 conn->data_size = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
2466 conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
2467
2468 spin_lock_init(&conn->lock);
2469
2470 /* initialize general xmit PDU commands queue */
2471 conn->xmitqueue = kfifo_alloc(session->cmds_max * sizeof(void*),
2472 GFP_KERNEL, NULL);
2473 if (conn->xmitqueue == ERR_PTR(-ENOMEM))
2474 goto xmitqueue_alloc_fail;
2475
2476 /* initialize write response PDU commands queue */
2477 conn->writequeue = kfifo_alloc(session->cmds_max * sizeof(void*),
2478 GFP_KERNEL, NULL);
2479 if (conn->writequeue == ERR_PTR(-ENOMEM))
2480 goto writequeue_alloc_fail;
2481
2482 /* initialize general immediate & non-immediate PDU commands queue */
2483 conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
2484 GFP_KERNEL, NULL);
2485 if (conn->immqueue == ERR_PTR(-ENOMEM))
2486 goto immqueue_alloc_fail;
2487
2488 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
2489 GFP_KERNEL, NULL);
2490 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
2491 goto mgmtqueue_alloc_fail;
2492
2493 INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
2494
2495 /* allocate login_mtask used for the login/text sequences */
2496 spin_lock_bh(&session->lock);
2497 if (!__kfifo_get(session->mgmtpool.queue,
2498 (void*)&conn->login_mtask,
2499 sizeof(void*))) {
2500 spin_unlock_bh(&session->lock);
2501 goto login_mtask_alloc_fail;
2502 }
2503 spin_unlock_bh(&session->lock);
2504
2505 /* allocate initial PDU receive place holder */
2506 if (conn->data_size <= PAGE_SIZE)
2507 conn->data = kmalloc(conn->data_size, GFP_KERNEL);
2508 else
2509 conn->data = (void*)__get_free_pages(GFP_KERNEL,
2510 get_order(conn->data_size));
2511 if (!conn->data)
2512 goto max_recv_dlenght_alloc_fail;
2513
2514 init_timer(&conn->tmabort_timer);
2515 init_MUTEX(&conn->xmitsema);
2516 init_waitqueue_head(&conn->ehwait);
2517
2518 return iscsi_handle(conn);
2519
2520max_recv_dlenght_alloc_fail:
2521 spin_lock_bh(&session->lock);
2522 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2523 sizeof(void*));
2524 spin_unlock_bh(&session->lock);
2525login_mtask_alloc_fail:
2526 kfifo_free(conn->mgmtqueue);
2527mgmtqueue_alloc_fail:
2528 kfifo_free(conn->immqueue);
2529immqueue_alloc_fail:
2530 kfifo_free(conn->writequeue);
2531writequeue_alloc_fail:
2532 kfifo_free(conn->xmitqueue);
2533xmitqueue_alloc_fail:
2534 kfree(conn);
2535conn_alloc_fail:
2536 return iscsi_handle(NULL);
2537}
2538
2539static void
2540iscsi_conn_destroy(iscsi_connh_t connh)
2541{
2542 struct iscsi_conn *conn = iscsi_ptr(connh);
2543 struct iscsi_session *session = conn->session;
2544
2545 down(&conn->xmitsema);
2546 set_bit(SUSPEND_BIT, &conn->suspend_tx);
2547 if (conn->c_stage == ISCSI_CONN_INITIAL_STAGE && conn->sock) {
2548 struct sock *sk = conn->sock->sk;
2549
2550 /*
2551 * conn_start() has never been called!
2552 * need to cleanup the socket.
2553 */
2554 write_lock_bh(&sk->sk_callback_lock);
2555 set_bit(SUSPEND_BIT, &conn->suspend_rx);
2556 write_unlock_bh(&sk->sk_callback_lock);
2557
2558 sock_hold(conn->sock->sk);
2559 iscsi_conn_restore_callbacks(conn);
2560 sock_put(conn->sock->sk);
2561 sock_release(conn->sock);
2562 conn->sock = NULL;
2563 }
2564
2565 spin_lock_bh(&session->lock);
2566 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2567 if (session->leadconn == conn) {
2568 /*
2569 * leading connection? then give up on recovery.
2570 */
2571 session->state = ISCSI_STATE_TERMINATE;
2572 wake_up(&conn->ehwait);
2573 }
2574 spin_unlock_bh(&session->lock);
2575
2576 up(&conn->xmitsema);
2577
2578 /*
2579 * Block until all in-progress commands for this connection
2580 * time out or fail.
2581 */
2582 for (;;) {
2583 spin_lock_bh(&conn->lock);
2584 if (!session->host->host_busy) { /* OK for ERL == 0 */
2585 spin_unlock_bh(&conn->lock);
2586 break;
2587 }
2588 spin_unlock_bh(&conn->lock);
2589 msleep_interruptible(500);
2590 printk("conn_destroy(): host_busy %d host_failed %d\n",
2591 session->host->host_busy, session->host->host_failed);
2592 /*
2593 * force eh_abort() to unblock
2594 */
2595 wake_up(&conn->ehwait);
2596 }
2597
2598 /* now free crypto */
2599 if (conn->hdrdgst_en || conn->datadgst_en) {
2600 if (conn->tx_tfm)
2601 crypto_free_tfm(conn->tx_tfm);
2602 if (conn->rx_tfm)
2603 crypto_free_tfm(conn->rx_tfm);
2604 if (conn->data_tx_tfm)
2605 crypto_free_tfm(conn->data_tx_tfm);
2606 if (conn->data_rx_tfm)
2607 crypto_free_tfm(conn->data_rx_tfm);
2608 }
2609
2610 /* free conn->data, size = MaxRecvDataSegmentLength */
2611 if (conn->data_size <= PAGE_SIZE)
2612 kfree(conn->data);
2613 else
2614 free_pages((unsigned long)conn->data,
2615 get_order(conn->data_size));
2616
2617 spin_lock_bh(&session->lock);
2618 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2619 sizeof(void*));
2620 list_del(&conn->item);
2621 if (list_empty(&session->connections))
2622 session->leadconn = NULL;
2623 if (session->leadconn && session->leadconn == conn)
2624 session->leadconn = container_of(session->connections.next,
2625 struct iscsi_conn, item);
2626
2627 if (session->leadconn == NULL)
2628 /* none connections exits.. reset sequencing */
2629 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
2630 spin_unlock_bh(&session->lock);
2631
2632 kfifo_free(conn->xmitqueue);
2633 kfifo_free(conn->writequeue);
2634 kfifo_free(conn->immqueue);
2635 kfifo_free(conn->mgmtqueue);
2636 kfree(conn);
2637}
2638
2639static int
2640iscsi_conn_bind(iscsi_sessionh_t sessionh, iscsi_connh_t connh,
2641 uint32_t transport_fd, int is_leading)
2642{
2643 struct iscsi_session *session = iscsi_ptr(sessionh);
2644 struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = iscsi_ptr(connh);
2645 struct sock *sk;
2646 struct socket *sock;
2647 int err;
2648
2649 /* lookup for existing socket */
2650 sock = sockfd_lookup(transport_fd, &err);
2651 if (!sock) {
2652 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
2653 return -EEXIST;
2654 }
2655
2656 /* lookup for existing connection */
2657 spin_lock_bh(&session->lock);
2658 list_for_each_entry(tmp, &session->connections, item) {
2659 if (tmp == conn) {
2660 if (conn->c_stage != ISCSI_CONN_STOPPED ||
2661 conn->stop_stage == STOP_CONN_TERM) {
2662 printk(KERN_ERR "iscsi_tcp: can't bind "
2663 "non-stopped connection (%d:%d)\n",
2664 conn->c_stage, conn->stop_stage);
2665 spin_unlock_bh(&session->lock);
2666 return -EIO;
2667 }
2668 break;
2669 }
2670 }
2671 if (tmp != conn) {
2672 /* bind new iSCSI connection to session */
2673 conn->session = session;
2674
2675 list_add(&conn->item, &session->connections);
2676 }
2677 spin_unlock_bh(&session->lock);
2678
2679 if (conn->stop_stage != STOP_CONN_SUSPEND) {
2680 /* bind iSCSI connection and socket */
2681 conn->sock = sock;
2682
2683 /* setup Socket parameters */
2684 sk = sock->sk;
2685 sk->sk_reuse = 1;
2686 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
2687 sk->sk_allocation = GFP_ATOMIC;
2688
2689 /* FIXME: disable Nagle's algorithm */
2690
2691 /*
2692 * Intercept TCP callbacks for sendfile like receive
2693 * processing.
2694 */
2695 iscsi_conn_set_callbacks(conn);
2696
2697 /*
2698 * set receive state machine into initial state
2699 */
2700 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
2701 }
2702
2703 if (is_leading)
2704 session->leadconn = conn;
2705
2706 /*
2707 * Unblock xmitworker(), Login Phase will pass through.
2708 */
2709 clear_bit(SUSPEND_BIT, &conn->suspend_rx);
2710 clear_bit(SUSPEND_BIT, &conn->suspend_tx);
2711
2712 return 0;
2713}
2714
2715static int
2716iscsi_conn_start(iscsi_connh_t connh)
2717{
2718 struct iscsi_conn *conn = iscsi_ptr(connh);
2719 struct iscsi_session *session = conn->session;
2720 struct sock *sk;
2721
2722 /* FF phase warming up... */
2723
2724 if (session == NULL) {
2725 printk(KERN_ERR "iscsi_tcp: can't start unbound connection\n");
2726 return -EPERM;
2727 }
2728
2729 sk = conn->sock->sk;
2730
2731 write_lock_bh(&sk->sk_callback_lock);
2732 spin_lock_bh(&session->lock);
2733 conn->c_stage = ISCSI_CONN_STARTED;
2734 session->state = ISCSI_STATE_LOGGED_IN;
2735
2736 switch(conn->stop_stage) {
2737 case STOP_CONN_RECOVER:
2738 /*
2739 * unblock eh_abort() if it is blocked. re-try all
2740 * commands after successful recovery
2741 */
2742 session->conn_cnt++;
2743 conn->stop_stage = 0;
2744 conn->tmabort_state = TMABORT_INITIAL;
2745 session->age++;
2746 wake_up(&conn->ehwait);
2747 break;
2748 case STOP_CONN_TERM:
2749 session->conn_cnt++;
2750 conn->stop_stage = 0;
2751 break;
2752 case STOP_CONN_SUSPEND:
2753 conn->stop_stage = 0;
2754 clear_bit(SUSPEND_BIT, &conn->suspend_rx);
2755 clear_bit(SUSPEND_BIT, &conn->suspend_tx);
2756 break;
2757 default:
2758 break;
2759 }
2760 spin_unlock_bh(&session->lock);
2761 write_unlock_bh(&sk->sk_callback_lock);
2762
2763 return 0;
2764}
2765
2766static void
2767iscsi_conn_stop(iscsi_connh_t connh, int flag)
2768{
2769 struct iscsi_conn *conn = iscsi_ptr(connh);
2770 struct iscsi_session *session = conn->session;
2771 struct sock *sk;
2772 unsigned long flags;
2773
2774 BUG_ON(!conn->sock);
2775 sk = conn->sock->sk;
2776 write_lock_bh(&sk->sk_callback_lock);
2777 set_bit(SUSPEND_BIT, &conn->suspend_rx);
2778 write_unlock_bh(&sk->sk_callback_lock);
2779
2780 down(&conn->xmitsema);
2781
2782 spin_lock_irqsave(session->host->host_lock, flags);
2783 spin_lock(&session->lock);
2784 conn->stop_stage = flag;
2785 conn->c_stage = ISCSI_CONN_STOPPED;
2786 set_bit(SUSPEND_BIT, &conn->suspend_tx);
2787
2788 if (flag != STOP_CONN_SUSPEND)
2789 session->conn_cnt--;
2790
2791 if (session->conn_cnt == 0 || session->leadconn == conn)
2792 session->state = ISCSI_STATE_FAILED;
2793
2794 spin_unlock(&session->lock);
2795 spin_unlock_irqrestore(session->host->host_lock, flags);
2796
2797 if (flag == STOP_CONN_TERM || flag == STOP_CONN_RECOVER) {
2798 struct iscsi_cmd_task *ctask;
2799 struct iscsi_mgmt_task *mtask;
2800
2801 /*
2802 * Socket must go now.
2803 */
2804 sock_hold(conn->sock->sk);
2805 iscsi_conn_restore_callbacks(conn);
2806 sock_put(conn->sock->sk);
2807
2808 /*
2809 * flush xmit queues.
2810 */
2811 spin_lock_bh(&session->lock);
2812 while (__kfifo_get(conn->writequeue, (void*)&ctask,
2813 sizeof(void*)) ||
2814 __kfifo_get(conn->xmitqueue, (void*)&ctask,
2815 sizeof(void*))) {
2816 struct iscsi_r2t_info *r2t;
2817
2818 /*
2819 * flush ctask's r2t queues
2820 */
2821 while (__kfifo_get(ctask->r2tqueue, (void*)&r2t,
2822 sizeof(void*)))
2823 __kfifo_put(ctask->r2tpool.queue, (void*)&r2t,
2824 sizeof(void*));
2825
2826 spin_unlock_bh(&session->lock);
2827 local_bh_disable();
2828 iscsi_ctask_cleanup(conn, ctask);
2829 local_bh_enable();
2830 spin_lock_bh(&session->lock);
2831 }
2832 conn->ctask = NULL;
2833 while (__kfifo_get(conn->immqueue, (void*)&mtask,
2834 sizeof(void*)) ||
2835 __kfifo_get(conn->mgmtqueue, (void*)&mtask,
2836 sizeof(void*))) {
2837 __kfifo_put(session->mgmtpool.queue,
2838 (void*)&mtask, sizeof(void*));
2839 }
2840 conn->mtask = NULL;
2841 spin_unlock_bh(&session->lock);
2842
2843 /*
2844 * release socket only after we stopped data_xmit()
2845 * activity and flushed all outstandings
2846 */
2847 sock_release(conn->sock);
2848 conn->sock = NULL;
2849
2850 /*
2851 * for connection level recovery we should not calculate
2852 * header digest. conn->hdr_size used for optimization
2853 * in hdr_extract() and will be re-negotiated at
2854 * set_param() time.
2855 */
2856 if (flag == STOP_CONN_RECOVER)
2857 conn->hdr_size = sizeof(struct iscsi_hdr);
2858 }
2859 up(&conn->xmitsema);
2860}
2861
2862static int
2863iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
2864 char *data, uint32_t data_size)
2865{
2866 struct iscsi_session *session = conn->session;
2867 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
2868 struct iscsi_mgmt_task *mtask;
2869
2870 spin_lock_bh(&session->lock);
2871 if (session->state == ISCSI_STATE_TERMINATE) {
2872 spin_unlock_bh(&session->lock);
2873 return -EPERM;
2874 }
2875 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
2876 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
2877 /*
2878 * Login and Text are sent serially, in
2879 * request-followed-by-response sequence.
2880 * Same mtask can be used. Same ITT must be used.
2881 * Note that login_mtask is preallocated at conn_create().
2882 */
2883 mtask = conn->login_mtask;
2884 else {
2885 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
2886 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
2887
2888 if (!__kfifo_get(session->mgmtpool.queue,
2889 (void*)&mtask, sizeof(void*))) {
2890 spin_unlock_bh(&session->lock);
2891 return -ENOSPC;
2892 }
2893 }
2894
2895 /*
2896 * pre-format CmdSN and ExpStatSN for outgoing PDU.
2897 */
2898 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
2899 hdr->itt = mtask->itt | (conn->id << CID_SHIFT) |
2900 (session->age << AGE_SHIFT);
2901 nop->cmdsn = cpu_to_be32(session->cmdsn);
2902 if (conn->c_stage == ISCSI_CONN_STARTED &&
2903 !(hdr->opcode & ISCSI_OP_IMMEDIATE))
2904 session->cmdsn++;
2905 } else
2906 /* do not advance CmdSN */
2907 nop->cmdsn = cpu_to_be32(session->cmdsn);
2908
2909 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
2910
2911 memcpy(&mtask->hdr, hdr, sizeof(struct iscsi_hdr));
2912
2913 iscsi_buf_init_virt(&mtask->headbuf, (char*)&mtask->hdr,
2914 sizeof(struct iscsi_hdr));
2915
2916 spin_unlock_bh(&session->lock);
2917
2918 if (data_size) {
2919 memcpy(mtask->data, data, data_size);
2920 mtask->data_count = data_size;
2921 } else
2922 mtask->data_count = 0;
2923
2924 mtask->xmstate = XMSTATE_IMM_HDR;
2925
2926 if (mtask->data_count) {
2927 iscsi_buf_init_iov(&mtask->sendbuf, (char*)mtask->data,
2928 mtask->data_count);
2929 }
2930
2931 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
2932 hdr->opcode, hdr->itt, data_size);
2933
2934 /*
2935 * since send_pdu() could be called at least from two contexts,
2936 * we need to serialize __kfifo_put, so we don't have to take
2937 * additional lock on fast data-path
2938 */
2939 if (hdr->opcode & ISCSI_OP_IMMEDIATE)
2940 __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
2941 else
2942 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
2943
2944 schedule_work(&conn->xmitwork);
2945
2946 return 0;
2947}
2948
2949static int
2950iscsi_eh_host_reset(struct scsi_cmnd *sc)
2951{
2952 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
2953 struct iscsi_conn *conn = ctask->conn;
2954 struct iscsi_session *session = conn->session;
2955
2956 spin_lock_bh(&session->lock);
2957 if (session->state == ISCSI_STATE_TERMINATE) {
2958 debug_scsi("failing host reset: session terminated "
2959 "[CID %d age %d]", conn->id, session->age);
2960 spin_unlock_bh(&session->lock);
2961 return FAILED;
2962 }
2963 spin_unlock_bh(&session->lock);
2964
2965 debug_scsi("failing connection CID %d due to SCSI host reset "
2966 "[itt 0x%x age %d]", conn->id, ctask->itt,
2967 session->age);
2968 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2969
2970 return SUCCESS;
2971}
2972
2973static void
2974iscsi_tmabort_timedout(unsigned long data)
2975{
2976 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
2977 struct iscsi_conn *conn = ctask->conn;
2978 struct iscsi_session *session = conn->session;
2979
2980 spin_lock(&session->lock);
2981 if (conn->tmabort_state == TMABORT_INITIAL) {
2982 __kfifo_put(session->mgmtpool.queue,
2983 (void*)&ctask->mtask, sizeof(void*));
2984 conn->tmabort_state = TMABORT_TIMEDOUT;
2985 debug_scsi("tmabort timedout [sc %lx itt 0x%x]\n",
2986 (long)ctask->sc, ctask->itt);
2987 /* unblock eh_abort() */
2988 wake_up(&conn->ehwait);
2989 }
2990 spin_unlock(&session->lock);
2991}
2992
2993static int
2994iscsi_eh_abort(struct scsi_cmnd *sc)
2995{
2996 int rc;
2997 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
2998 struct iscsi_conn *conn = ctask->conn;
2999 struct iscsi_session *session = conn->session;
3000
3001 conn->eh_abort_cnt++;
3002 debug_scsi("aborting [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
3003
3004 /*
3005 * two cases for ERL=0 here:
3006 *
3007 * 1) connection-level failure;
3008 * 2) recovery due protocol error;
3009 */
3010 down(&conn->xmitsema);
3011 spin_lock_bh(&session->lock);
3012 if (session->state != ISCSI_STATE_LOGGED_IN) {
3013 if (session->state == ISCSI_STATE_TERMINATE) {
3014 spin_unlock_bh(&session->lock);
3015 up(&conn->xmitsema);
3016 goto failed;
3017 }
3018 spin_unlock_bh(&session->lock);
3019 } else {
3020 struct iscsi_tm *hdr = &conn->tmhdr;
3021
3022 /*
3023 * Still LOGGED_IN...
3024 */
3025
3026 if (!ctask->sc || sc->SCp.phase != session->age) {
3027 /*
3028 * 1) ctask completed before time out. But session
3029 * is still ok => Happy Retry.
3030 * 2) session was re-open during time out of ctask.
3031 */
3032 spin_unlock_bh(&session->lock);
3033 up(&conn->xmitsema);
3034 goto success;
3035 }
3036 conn->tmabort_state = TMABORT_INITIAL;
3037 spin_unlock_bh(&session->lock);
3038
3039 /*
3040 * ctask timed out but session is OK
3041 * ERL=0 requires task mgmt abort to be issued on each
3042 * failed command. requests must be serialized.
3043 */
3044 memset(hdr, 0, sizeof(struct iscsi_tm));
3045 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
3046 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
3047 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3048 memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
3049 hdr->rtt = ctask->hdr.itt;
3050 hdr->refcmdsn = ctask->hdr.cmdsn;
3051
3052 rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
3053 NULL, 0);
3054 if (rc) {
3055 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3056 debug_scsi("abort sent failure [itt 0x%x]", ctask->itt);
3057 } else {
3058 struct iscsi_r2t_info *r2t;
3059
3060 /*
3061 * TMF abort vs. TMF response race logic
3062 */
3063 spin_lock_bh(&session->lock);
3064 ctask->mtask = (struct iscsi_mgmt_task *)
3065 session->mgmt_cmds[(hdr->itt & ITT_MASK) -
3066 ISCSI_MGMT_ITT_OFFSET];
3067 /*
3068 * have to flush r2tqueue to avoid r2t leaks
3069 */
3070 while (__kfifo_get(ctask->r2tqueue, (void*)&r2t,
3071 sizeof(void*))) {
3072 __kfifo_put(ctask->r2tpool.queue, (void*)&r2t,
3073 sizeof(void*));
3074 }
3075 if (conn->tmabort_state == TMABORT_INITIAL) {
3076 conn->tmfcmd_pdus_cnt++;
3077 conn->tmabort_timer.expires = 3*HZ + jiffies;
3078 conn->tmabort_timer.function =
3079 iscsi_tmabort_timedout;
3080 conn->tmabort_timer.data = (unsigned long)ctask;
3081 add_timer(&conn->tmabort_timer);
3082 debug_scsi("abort sent [itt 0x%x]", ctask->itt);
3083 } else {
3084 if (!ctask->sc ||
3085 conn->tmabort_state == TMABORT_SUCCESS) {
3086 conn->tmabort_state = TMABORT_INITIAL;
3087 spin_unlock_bh(&session->lock);
3088 up(&conn->xmitsema);
3089 goto success;
3090 }
3091 conn->tmabort_state = TMABORT_INITIAL;
3092 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3093 }
3094 spin_unlock_bh(&session->lock);
3095 }
3096 }
3097 up(&conn->xmitsema);
3098
3099
3100 /*
3101 * block eh thread until:
3102 *
3103 * 1) abort response;
3104 * 2) abort timeout;
3105 * 3) session re-opened;
3106 * 4) session terminated;
3107 */
3108 for (;;) {
3109 int p_state = session->state;
3110
3111 rc = wait_event_interruptible(conn->ehwait,
3112 (p_state == ISCSI_STATE_LOGGED_IN ?
3113 (session->state == ISCSI_STATE_TERMINATE ||
3114 conn->tmabort_state != TMABORT_INITIAL) :
3115 (session->state == ISCSI_STATE_TERMINATE ||
3116 session->state == ISCSI_STATE_LOGGED_IN)));
3117 if (rc) {
3118 /* shutdown.. */
3119 session->state = ISCSI_STATE_TERMINATE;
3120 goto failed;
3121 }
3122
3123 if (signal_pending(current))
3124 flush_signals(current);
3125
3126 if (session->state == ISCSI_STATE_TERMINATE)
3127 goto failed;
3128
3129 spin_lock_bh(&session->lock);
3130 if (sc->SCp.phase == session->age &&
3131 (conn->tmabort_state == TMABORT_TIMEDOUT ||
3132 conn->tmabort_state == TMABORT_FAILED)) {
3133 conn->tmabort_state = TMABORT_INITIAL;
3134 if (!ctask->sc) {
3135 /*
3136 * ctask completed before tmf abort response or
3137 * time out.
3138 * But session is still ok => Happy Retry.
3139 */
3140 spin_unlock_bh(&session->lock);
3141 break;
3142 }
3143 spin_unlock_bh(&session->lock);
3144 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3145 continue;
3146 }
3147 spin_unlock_bh(&session->lock);
3148 break;
3149 }
3150
3151success:
3152 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
3153 rc = SUCCESS;
3154 goto exit;
3155
3156failed:
3157 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
3158 rc = FAILED;
3159
3160exit:
3161 del_timer_sync(&conn->tmabort_timer);
3162
3163 down(&conn->xmitsema);
3164 if (conn->sock) {
3165 struct sock *sk = conn->sock->sk;
3166
3167 write_lock_bh(&sk->sk_callback_lock);
3168 iscsi_ctask_cleanup(conn, ctask);
3169 write_unlock_bh(&sk->sk_callback_lock);
3170 }
3171 up(&conn->xmitsema);
3172 return rc;
3173}
3174
3175static int
3176iscsi_r2tpool_alloc(struct iscsi_session *session)
3177{
3178 int i;
3179 int cmd_i;
3180
3181 /*
3182 * initialize per-task: R2T pool and xmit queue
3183 */
3184 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
3185 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
3186
3187 /*
3188 * pre-allocated x4 as much r2ts to handle race when
3189 * target acks DataOut faster than we data_xmit() queues
3190 * could replenish r2tqueue.
3191 */
3192
3193 /* R2T pool */
3194 if (iscsi_pool_init(&ctask->r2tpool, session->max_r2t * 4,
3195 (void***)&ctask->r2ts, sizeof(struct iscsi_r2t_info))) {
3196 goto r2t_alloc_fail;
3197 }
3198
3199 /* R2T xmit queue */
3200 ctask->r2tqueue = kfifo_alloc(
3201 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
3202 if (ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
3203 iscsi_pool_free(&ctask->r2tpool, (void**)ctask->r2ts);
3204 goto r2t_alloc_fail;
3205 }
3206
3207 /*
3208 * number of
3209 * Data-Out PDU's within R2T-sequence can be quite big;
3210 * using mempool
3211 */
3212 ctask->datapool = mempool_create(ISCSI_DTASK_DEFAULT_MAX,
3213 mempool_alloc_slab, mempool_free_slab, taskcache);
3214 if (ctask->datapool == NULL) {
3215 kfifo_free(ctask->r2tqueue);
3216 iscsi_pool_free(&ctask->r2tpool, (void**)ctask->r2ts);
3217 goto r2t_alloc_fail;
3218 }
3219 INIT_LIST_HEAD(&ctask->dataqueue);
3220 }
3221
3222 return 0;
3223
3224r2t_alloc_fail:
3225 for (i = 0; i < cmd_i; i++) {
3226 mempool_destroy(session->cmds[i]->datapool);
3227 kfifo_free(session->cmds[i]->r2tqueue);
3228 iscsi_pool_free(&session->cmds[i]->r2tpool,
3229 (void**)session->cmds[i]->r2ts);
3230 }
3231 return -ENOMEM;
3232}
3233
3234static void
3235iscsi_r2tpool_free(struct iscsi_session *session)
3236{
3237 int i;
3238
3239 for (i = 0; i < session->cmds_max; i++) {
3240 mempool_destroy(session->cmds[i]->datapool);
3241 kfifo_free(session->cmds[i]->r2tqueue);
3242 iscsi_pool_free(&session->cmds[i]->r2tpool,
3243 (void**)session->cmds[i]->r2ts);
3244 }
3245}
3246
3247static struct scsi_host_template iscsi_sht = {
3248 .name = "iSCSI Initiator over TCP/IP, v."
3249 ISCSI_VERSION_STR,
3250 .queuecommand = iscsi_queuecommand,
3251 .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
3252 .sg_tablesize = ISCSI_SG_TABLESIZE,
3253 .cmd_per_lun = ISCSI_CMD_PER_LUN,
3254 .eh_abort_handler = iscsi_eh_abort,
3255 .eh_host_reset_handler = iscsi_eh_host_reset,
3256 .use_clustering = DISABLE_CLUSTERING,
3257 .proc_name = "iscsi_tcp",
3258 .this_id = -1,
3259};
3260
3261static iscsi_sessionh_t
3262iscsi_session_create(uint32_t initial_cmdsn, struct Scsi_Host *host)
3263{
3264 int cmd_i;
3265 struct iscsi_session *session;
3266
3267 session = iscsi_hostdata(host->hostdata);
3268 memset(session, 0, sizeof(struct iscsi_session));
3269
3270 session->host = host;
3271 session->id = host->host_no;
3272 session->state = ISCSI_STATE_LOGGED_IN;
3273 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
3274 session->cmds_max = ISCSI_XMIT_CMDS_MAX;
3275 session->cmdsn = initial_cmdsn;
3276 session->exp_cmdsn = initial_cmdsn + 1;
3277 session->max_cmdsn = initial_cmdsn + 1;
3278 session->max_r2t = 1;
3279
3280 /* initialize SCSI PDU commands pool */
3281 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
3282 (void***)&session->cmds, sizeof(struct iscsi_cmd_task)))
3283 goto cmdpool_alloc_fail;
3284
3285 /* pre-format cmds pool with ITT */
3286 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++)
3287 session->cmds[cmd_i]->itt = cmd_i;
3288
3289 spin_lock_init(&session->lock);
3290 INIT_LIST_HEAD(&session->connections);
3291
3292 /* initialize immediate command pool */
3293 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
3294 (void***)&session->mgmt_cmds, sizeof(struct iscsi_mgmt_task)))
3295 goto mgmtpool_alloc_fail;
3296
3297
3298 /* pre-format immediate cmds pool with ITT */
3299 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
3300 session->mgmt_cmds[cmd_i]->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
3301 session->mgmt_cmds[cmd_i]->data = kmalloc(
3302 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
3303 if (!session->mgmt_cmds[cmd_i]->data) {
3304 int j;
3305
3306 for (j = 0; j < cmd_i; j++)
3307 kfree(session->mgmt_cmds[j]->data);
3308 goto immdata_alloc_fail;
3309 }
3310 }
3311
3312 if (iscsi_r2tpool_alloc(session))
3313 goto r2tpool_alloc_fail;
3314
3315 return iscsi_handle(session);
3316
3317r2tpool_alloc_fail:
3318 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++)
3319 kfree(session->mgmt_cmds[cmd_i]->data);
3320 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
3321immdata_alloc_fail:
3322mgmtpool_alloc_fail:
3323 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
3324cmdpool_alloc_fail:
3325 return iscsi_handle(NULL);
3326}
3327
3328static void
3329iscsi_session_destroy(iscsi_sessionh_t sessionh)
3330{
3331 int cmd_i;
3332 struct iscsi_data_task *dtask, *n;
3333 struct iscsi_session *session = iscsi_ptr(sessionh);
3334
3335 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
3336 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
3337 list_for_each_entry_safe(dtask, n, &ctask->dataqueue, item) {
3338 list_del(&dtask->item);
3339 mempool_free(dtask, ctask->datapool);
3340 }
3341 }
3342
3343 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++)
3344 kfree(session->mgmt_cmds[cmd_i]->data);
3345
3346 iscsi_r2tpool_free(session);
3347 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
3348 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
3349}
3350
3351static int
3352iscsi_conn_set_param(iscsi_connh_t connh, enum iscsi_param param,
3353 uint32_t value)
3354{
3355 struct iscsi_conn *conn = iscsi_ptr(connh);
3356 struct iscsi_session *session = conn->session;
3357
3358 spin_lock_bh(&session->lock);
3359 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
3360 conn->stop_stage != STOP_CONN_RECOVER) {
3361 printk(KERN_ERR "iscsi_tcp: can not change parameter [%d]\n",
3362 param);
3363 spin_unlock_bh(&session->lock);
3364 return 0;
3365 }
3366 spin_unlock_bh(&session->lock);
3367
3368 switch(param) {
3369 case ISCSI_PARAM_MAX_RECV_DLENGTH: {
3370 char *saveptr = conn->data;
3371 int flags = GFP_KERNEL;
3372
3373 if (conn->data_size >= value) {
3374 conn->max_recv_dlength = value;
3375 break;
3376 }
3377
3378 spin_lock_bh(&session->lock);
3379 if (conn->stop_stage == STOP_CONN_RECOVER)
3380 flags = GFP_ATOMIC;
3381 spin_unlock_bh(&session->lock);
3382
3383 if (value <= PAGE_SIZE)
3384 conn->data = kmalloc(value, flags);
3385 else
3386 conn->data = (void*)__get_free_pages(flags,
3387 get_order(value));
3388 if (conn->data == NULL) {
3389 conn->data = saveptr;
3390 return -ENOMEM;
3391 }
3392 if (conn->data_size <= PAGE_SIZE)
3393 kfree(saveptr);
3394 else
3395 free_pages((unsigned long)saveptr,
3396 get_order(conn->data_size));
3397 conn->max_recv_dlength = value;
3398 conn->data_size = value;
3399 }
3400 break;
3401 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3402 conn->max_xmit_dlength = value;
3403 break;
3404 case ISCSI_PARAM_HDRDGST_EN:
3405 conn->hdrdgst_en = value;
3406 conn->hdr_size = sizeof(struct iscsi_hdr);
3407 if (conn->hdrdgst_en) {
3408 conn->hdr_size += sizeof(__u32);
3409 if (!conn->tx_tfm)
3410 conn->tx_tfm = crypto_alloc_tfm("crc32c", 0);
3411 if (!conn->tx_tfm)
3412 return -ENOMEM;
3413 if (!conn->rx_tfm)
3414 conn->rx_tfm = crypto_alloc_tfm("crc32c", 0);
3415 if (!conn->rx_tfm) {
3416 crypto_free_tfm(conn->tx_tfm);
3417 return -ENOMEM;
3418 }
3419 } else {
3420 if (conn->tx_tfm)
3421 crypto_free_tfm(conn->tx_tfm);
3422 if (conn->rx_tfm)
3423 crypto_free_tfm(conn->rx_tfm);
3424 }
3425 break;
3426 case ISCSI_PARAM_DATADGST_EN:
3427 conn->datadgst_en = value;
3428 if (conn->datadgst_en) {
3429 if (!conn->data_tx_tfm)
3430 conn->data_tx_tfm =
3431 crypto_alloc_tfm("crc32c", 0);
3432 if (!conn->data_tx_tfm)
3433 return -ENOMEM;
3434 if (!conn->data_rx_tfm)
3435 conn->data_rx_tfm =
3436 crypto_alloc_tfm("crc32c", 0);
3437 if (!conn->data_rx_tfm) {
3438 crypto_free_tfm(conn->data_tx_tfm);
3439 return -ENOMEM;
3440 }
3441 } else {
3442 if (conn->data_tx_tfm)
3443 crypto_free_tfm(conn->data_tx_tfm);
3444 if (conn->data_rx_tfm)
3445 crypto_free_tfm(conn->data_rx_tfm);
3446 }
3447 break;
3448 case ISCSI_PARAM_INITIAL_R2T_EN:
3449 session->initial_r2t_en = value;
3450 break;
3451 case ISCSI_PARAM_MAX_R2T:
3452 if (session->max_r2t == roundup_pow_of_two(value))
3453 break;
3454 iscsi_r2tpool_free(session);
3455 session->max_r2t = value;
3456 if (session->max_r2t & (session->max_r2t - 1))
3457 session->max_r2t = roundup_pow_of_two(session->max_r2t);
3458 if (iscsi_r2tpool_alloc(session))
3459 return -ENOMEM;
3460 break;
3461 case ISCSI_PARAM_IMM_DATA_EN:
3462 session->imm_data_en = value;
3463 break;
3464 case ISCSI_PARAM_FIRST_BURST:
3465 session->first_burst = value;
3466 break;
3467 case ISCSI_PARAM_MAX_BURST:
3468 session->max_burst = value;
3469 break;
3470 case ISCSI_PARAM_PDU_INORDER_EN:
3471 session->pdu_inorder_en = value;
3472 break;
3473 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3474 session->dataseq_inorder_en = value;
3475 break;
3476 case ISCSI_PARAM_ERL:
3477 session->erl = value;
3478 break;
3479 case ISCSI_PARAM_IFMARKER_EN:
3480 BUG_ON(value);
3481 session->ifmarker_en = value;
3482 break;
3483 case ISCSI_PARAM_OFMARKER_EN:
3484 BUG_ON(value);
3485 session->ofmarker_en = value;
3486 break;
3487 default:
3488 break;
3489 }
3490
3491 return 0;
3492}
3493
3494static int
3495iscsi_conn_get_param(iscsi_connh_t connh, enum iscsi_param param,
3496 uint32_t *value)
3497{
3498 struct iscsi_conn *conn = iscsi_ptr(connh);
3499 struct iscsi_session *session = conn->session;
3500
3501 switch(param) {
3502 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3503 *value = conn->max_recv_dlength;
3504 break;
3505 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3506 *value = conn->max_xmit_dlength;
3507 break;
3508 case ISCSI_PARAM_HDRDGST_EN:
3509 *value = conn->hdrdgst_en;
3510 break;
3511 case ISCSI_PARAM_DATADGST_EN:
3512 *value = conn->datadgst_en;
3513 break;
3514 case ISCSI_PARAM_INITIAL_R2T_EN:
3515 *value = session->initial_r2t_en;
3516 break;
3517 case ISCSI_PARAM_MAX_R2T:
3518 *value = session->max_r2t;
3519 break;
3520 case ISCSI_PARAM_IMM_DATA_EN:
3521 *value = session->imm_data_en;
3522 break;
3523 case ISCSI_PARAM_FIRST_BURST:
3524 *value = session->first_burst;
3525 break;
3526 case ISCSI_PARAM_MAX_BURST:
3527 *value = session->max_burst;
3528 break;
3529 case ISCSI_PARAM_PDU_INORDER_EN:
3530 *value = session->pdu_inorder_en;
3531 break;
3532 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3533 *value = session->dataseq_inorder_en;
3534 break;
3535 case ISCSI_PARAM_ERL:
3536 *value = session->erl;
3537 break;
3538 case ISCSI_PARAM_IFMARKER_EN:
3539 *value = session->ifmarker_en;
3540 break;
3541 case ISCSI_PARAM_OFMARKER_EN:
3542 *value = session->ofmarker_en;
3543 break;
3544 default:
3545 return ISCSI_ERR_PARAM_NOT_FOUND;
3546 }
3547
3548 return 0;
3549}
3550
3551static void
3552iscsi_conn_get_stats(iscsi_connh_t connh, struct iscsi_stats *stats)
3553{
3554 struct iscsi_conn *conn = iscsi_ptr(connh);
3555
3556 stats->txdata_octets = conn->txdata_octets;
3557 stats->rxdata_octets = conn->rxdata_octets;
3558 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
3559 stats->dataout_pdus = conn->dataout_pdus_cnt;
3560 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
3561 stats->datain_pdus = conn->datain_pdus_cnt;
3562 stats->r2t_pdus = conn->r2t_pdus_cnt;
3563 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
3564 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
3565 stats->custom_length = 3;
3566 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
3567 stats->custom[0].value = conn->sendpage_failures_cnt;
3568 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
3569 stats->custom[1].value = conn->discontiguous_hdr_cnt;
3570 strcpy(stats->custom[2].desc, "eh_abort_cnt");
3571 stats->custom[2].value = conn->eh_abort_cnt;
3572}
3573
3574static int
3575iscsi_conn_send_pdu(iscsi_connh_t connh, struct iscsi_hdr *hdr, char *data,
3576 uint32_t data_size)
3577{
3578 struct iscsi_conn *conn = iscsi_ptr(connh);
3579 int rc;
3580
3581 down(&conn->xmitsema);
3582 rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
3583 up(&conn->xmitsema);
3584
3585 return rc;
3586}
3587
3588static struct iscsi_transport iscsi_tcp_transport = {
3589 .owner = THIS_MODULE,
3590 .name = "tcp",
3591 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
3592 | CAP_DATADGST,
3593 .host_template = &iscsi_sht,
3594 .hostdata_size = sizeof(struct iscsi_session),
3595 .max_conn = 1,
3596 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
3597 .create_session = iscsi_session_create,
3598 .destroy_session = iscsi_session_destroy,
3599 .create_conn = iscsi_conn_create,
3600 .bind_conn = iscsi_conn_bind,
3601 .destroy_conn = iscsi_conn_destroy,
3602 .set_param = iscsi_conn_set_param,
3603 .get_param = iscsi_conn_get_param,
3604 .start_conn = iscsi_conn_start,
3605 .stop_conn = iscsi_conn_stop,
3606 .send_pdu = iscsi_conn_send_pdu,
3607 .get_stats = iscsi_conn_get_stats,
3608};
3609
3610static int __init
3611iscsi_tcp_init(void)
3612{
3613 int error;
3614
3615 if (iscsi_max_lun < 1) {
3616 printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun);
3617 return -EINVAL;
3618 }
3619 iscsi_tcp_transport.max_lun = iscsi_max_lun;
3620
3621 taskcache = kmem_cache_create("iscsi_taskcache",
3622 sizeof(struct iscsi_data_task), 0,
3623 SLAB_HWCACHE_ALIGN | SLAB_NO_REAP, NULL, NULL);
3624 if (!taskcache)
3625 return -ENOMEM;
3626
3627 error = iscsi_register_transport(&iscsi_tcp_transport);
3628 if (error)
3629 kmem_cache_destroy(taskcache);
3630
3631 return error;
3632}
3633
3634static void __exit
3635iscsi_tcp_exit(void)
3636{
3637 iscsi_unregister_transport(&iscsi_tcp_transport);
3638 kmem_cache_destroy(taskcache);
3639}
3640
3641module_init(iscsi_tcp_init);
3642module_exit(iscsi_tcp_exit);
diff --git a/drivers/scsi/iscsi_tcp.h b/drivers/scsi/iscsi_tcp.h
new file mode 100644
index 00000000000..d23ae68fae0
--- /dev/null
+++ b/drivers/scsi/iscsi_tcp.h
@@ -0,0 +1,322 @@
1/*
2 * iSCSI Initiator TCP Transport
3 * Copyright (C) 2004 Dmitry Yusupov
4 * Copyright (C) 2004 Alex Aizman
5 * Copyright (C) 2005 Mike Christie
6 * maintained by open-iscsi@googlegroups.com
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * See the file COPYING included with this distribution for more details.
19 */
20
21#ifndef ISCSI_TCP_H
22#define ISCSI_TCP_H
23
24/* Session's states */
25#define ISCSI_STATE_FREE 1
26#define ISCSI_STATE_LOGGED_IN 2
27#define ISCSI_STATE_FAILED 3
28#define ISCSI_STATE_TERMINATE 4
29
30/* Connection's states */
31#define ISCSI_CONN_INITIAL_STAGE 0
32#define ISCSI_CONN_STARTED 1
33#define ISCSI_CONN_STOPPED 2
34#define ISCSI_CONN_CLEANUP_WAIT 3
35
36/* Connection suspend "bit" */
37#define SUSPEND_BIT 1
38
39/* Socket's Receive state machine */
40#define IN_PROGRESS_WAIT_HEADER 0x0
41#define IN_PROGRESS_HEADER_GATHER 0x1
42#define IN_PROGRESS_DATA_RECV 0x2
43#define IN_PROGRESS_DDIGEST_RECV 0x3
44
45/* Task Mgmt states */
46#define TMABORT_INITIAL 0x0
47#define TMABORT_SUCCESS 0x1
48#define TMABORT_FAILED 0x2
49#define TMABORT_TIMEDOUT 0x3
50
51/* xmit state machine */
52#define XMSTATE_IDLE 0x0
53#define XMSTATE_R_HDR 0x1
54#define XMSTATE_W_HDR 0x2
55#define XMSTATE_IMM_HDR 0x4
56#define XMSTATE_IMM_DATA 0x8
57#define XMSTATE_UNS_INIT 0x10
58#define XMSTATE_UNS_HDR 0x20
59#define XMSTATE_UNS_DATA 0x40
60#define XMSTATE_SOL_HDR 0x80
61#define XMSTATE_SOL_DATA 0x100
62#define XMSTATE_W_PAD 0x200
63#define XMSTATE_DATA_DIGEST 0x400
64
65#define ISCSI_CONN_MAX 1
66#define ISCSI_CONN_RCVBUF_MIN 262144
67#define ISCSI_CONN_SNDBUF_MIN 262144
68#define ISCSI_PAD_LEN 4
69#define ISCSI_R2T_MAX 16
70#define ISCSI_XMIT_CMDS_MAX 128 /* must be power of 2 */
71#define ISCSI_MGMT_CMDS_MAX 32 /* must be power of 2 */
72#define ISCSI_MGMT_ITT_OFFSET 0xa00
73#define ISCSI_SG_TABLESIZE SG_ALL
74#define ISCSI_CMD_PER_LUN 128
75#define ISCSI_TCP_MAX_CMD_LEN 16
76
77#define ITT_MASK (0xfff)
78#define CID_SHIFT 12
79#define CID_MASK (0xffff<<CID_SHIFT)
80#define AGE_SHIFT 28
81#define AGE_MASK (0xf<<AGE_SHIFT)
82
83struct iscsi_queue {
84 struct kfifo *queue; /* FIFO Queue */
85 void **pool; /* Pool of elements */
86 int max; /* Max number of elements */
87};
88
89struct iscsi_session;
90struct iscsi_cmd_task;
91struct iscsi_mgmt_task;
92
93/* Socket connection recieve helper */
94struct iscsi_tcp_recv {
95 struct iscsi_hdr *hdr;
96 struct sk_buff *skb;
97 int offset;
98 int len;
99 int hdr_offset;
100 int copy;
101 int copied;
102 int padding;
103 struct iscsi_cmd_task *ctask; /* current cmd in progress */
104
105 /* copied and flipped values */
106 int opcode;
107 int flags;
108 int cmd_status;
109 int ahslen;
110 int datalen;
111 uint32_t itt;
112 int datadgst;
113};
114
115struct iscsi_conn {
116 struct iscsi_hdr hdr; /* header placeholder */
117 char hdrext[4*sizeof(__u16) +
118 sizeof(__u32)];
119 int data_copied;
120 char *data; /* data placeholder */
121 struct socket *sock; /* TCP socket */
122 int data_size; /* actual recv_dlength */
123 int stop_stage; /* conn_stop() flag: *
124 * stop to recover, *
125 * stop to terminate */
126 /* iSCSI connection-wide sequencing */
127 uint32_t exp_statsn;
128 int hdr_size; /* PDU header size */
129 unsigned long suspend_rx; /* suspend Rx */
130
131 struct crypto_tfm *rx_tfm; /* CRC32C (Rx) */
132 struct crypto_tfm *data_rx_tfm; /* CRC32C (Rx) for data */
133
134 /* control data */
135 int senselen; /* scsi sense length */
136 int id; /* CID */
137 struct iscsi_tcp_recv in; /* TCP receive context */
138 struct iscsi_session *session; /* parent session */
139 struct list_head item; /* maintains list of conns */
140 int in_progress; /* connection state machine */
141 int c_stage; /* connection state */
142 struct iscsi_mgmt_task *login_mtask; /* mtask used for login/text */
143 struct iscsi_mgmt_task *mtask; /* xmit mtask in progress */
144 struct iscsi_cmd_task *ctask; /* xmit ctask in progress */
145 spinlock_t lock; /* FIXME: to be removed */
146
147 /* old values for socket callbacks */
148 void (*old_data_ready)(struct sock *, int);
149 void (*old_state_change)(struct sock *);
150 void (*old_write_space)(struct sock *);
151
152 /* xmit */
153 struct crypto_tfm *tx_tfm; /* CRC32C (Tx) */
154 struct crypto_tfm *data_tx_tfm; /* CRC32C (Tx) for data */
155 struct kfifo *writequeue; /* write cmds for Data-Outs */
156 struct kfifo *immqueue; /* immediate xmit queue */
157 struct kfifo *mgmtqueue; /* mgmt (control) xmit queue */
158 struct kfifo *xmitqueue; /* data-path cmd queue */
159 struct work_struct xmitwork; /* per-conn. xmit workqueue */
160 struct semaphore xmitsema; /* serializes connection xmit,
161 * access to kfifos: *
162 * xmitqueue, writequeue, *
163 * immqueue, mgmtqueue */
164 unsigned long suspend_tx; /* suspend Tx */
165
166 /* abort */
167 wait_queue_head_t ehwait; /* used in eh_abort() */
168 struct iscsi_tm tmhdr;
169 struct timer_list tmabort_timer; /* abort timer */
170 int tmabort_state; /* see TMABORT_INITIAL, etc.*/
171
172 /* negotiated params */
173 int max_recv_dlength;
174 int max_xmit_dlength;
175 int hdrdgst_en;
176 int datadgst_en;
177
178 /* MIB-statistics */
179 uint64_t txdata_octets;
180 uint64_t rxdata_octets;
181 uint32_t scsicmd_pdus_cnt;
182 uint32_t dataout_pdus_cnt;
183 uint32_t scsirsp_pdus_cnt;
184 uint32_t datain_pdus_cnt;
185 uint32_t r2t_pdus_cnt;
186 uint32_t tmfcmd_pdus_cnt;
187 int32_t tmfrsp_pdus_cnt;
188
189 /* custom statistics */
190 uint32_t sendpage_failures_cnt;
191 uint32_t discontiguous_hdr_cnt;
192 uint32_t eh_abort_cnt;
193};
194
195struct iscsi_session {
196 /* iSCSI session-wide sequencing */
197 uint32_t cmdsn;
198 uint32_t exp_cmdsn;
199 uint32_t max_cmdsn;
200
201 /* configuration */
202 int initial_r2t_en;
203 int max_r2t;
204 int imm_data_en;
205 int first_burst;
206 int max_burst;
207 int time2wait;
208 int time2retain;
209 int pdu_inorder_en;
210 int dataseq_inorder_en;
211 int erl;
212 int ifmarker_en;
213 int ofmarker_en;
214
215 /* control data */
216 struct Scsi_Host *host;
217 int id;
218 struct iscsi_conn *leadconn; /* leading connection */
219 spinlock_t lock; /* protects session state, *
220 * sequence numbers, *
221 * session resources: *
222 * - cmdpool, *
223 * - mgmtpool, *
224 * - r2tpool */
225 int state; /* session state */
226 struct list_head item;
227 void *auth_client;
228 int conn_cnt;
229 int age; /* counts session re-opens */
230
231 struct list_head connections; /* list of connections */
232 int cmds_max; /* size of cmds array */
233 struct iscsi_cmd_task **cmds; /* Original Cmds arr */
234 struct iscsi_queue cmdpool; /* PDU's pool */
235 int mgmtpool_max; /* size of mgmt array */
236 struct iscsi_mgmt_task **mgmt_cmds; /* Original mgmt arr */
237 struct iscsi_queue mgmtpool; /* Mgmt PDU's pool */
238};
239
240struct iscsi_buf {
241 struct scatterlist sg;
242 struct kvec iov;
243 unsigned int sent;
244};
245
246struct iscsi_data_task {
247 struct iscsi_data hdr; /* PDU */
248 char hdrext[sizeof(__u32)]; /* Header-Digest */
249 struct list_head item; /* data queue item */
250 struct iscsi_buf digestbuf; /* digest buffer */
251 uint32_t digest; /* data digest */
252};
253#define ISCSI_DTASK_DEFAULT_MAX ISCSI_SG_TABLESIZE * PAGE_SIZE / 512
254
255struct iscsi_mgmt_task {
256 struct iscsi_hdr hdr; /* mgmt. PDU */
257 char hdrext[sizeof(__u32)]; /* Header-Digest */
258 char *data; /* mgmt payload */
259 int xmstate; /* mgmt xmit progress */
260 int data_count; /* counts data to be sent */
261 struct iscsi_buf headbuf; /* header buffer */
262 struct iscsi_buf sendbuf; /* in progress buffer */
263 int sent;
264 uint32_t itt; /* this ITT */
265};
266
267struct iscsi_r2t_info {
268 __be32 ttt; /* copied from R2T */
269 __be32 exp_statsn; /* copied from R2T */
270 uint32_t data_length; /* copied from R2T */
271 uint32_t data_offset; /* copied from R2T */
272 struct iscsi_buf headbuf; /* Data-Out Header Buffer */
273 struct iscsi_buf sendbuf; /* Data-Out in progress buffer*/
274 int sent; /* R2T sequence progress */
275 int data_count; /* DATA-Out payload progress */
276 struct scatterlist *sg; /* per-R2T SG list */
277 int solicit_datasn;
278 struct iscsi_data_task *dtask; /* which data task */
279};
280
281struct iscsi_cmd_task {
282 struct iscsi_cmd hdr; /* iSCSI PDU header */
283 char hdrext[4*sizeof(__u16)+ /* AHS */
284 sizeof(__u32)]; /* HeaderDigest */
285 char pad[ISCSI_PAD_LEN];
286 int itt; /* this ITT */
287 int datasn; /* DataSN */
288 struct iscsi_buf headbuf; /* header buf (xmit) */
289 struct iscsi_buf sendbuf; /* in progress buffer*/
290 int sent;
291 struct scatterlist *sg; /* per-cmd SG list */
292 struct scatterlist *bad_sg; /* assert statement */
293 int sg_count; /* SG's to process */
294 uint32_t unsol_datasn;
295 uint32_t exp_r2tsn;
296 int xmstate; /* xmit xtate machine */
297 int imm_count; /* imm-data (bytes) */
298 int unsol_count; /* unsolicited (bytes)*/
299 int r2t_data_count; /* R2T Data-Out bytes */
300 int data_count; /* remaining Data-Out */
301 int pad_count; /* padded bytes */
302 struct scsi_cmnd *sc; /* associated SCSI cmd*/
303 int total_length;
304 int data_offset;
305 struct iscsi_conn *conn; /* used connection */
306 struct iscsi_mgmt_task *mtask; /* tmf mtask in progr */
307
308 struct iscsi_r2t_info *r2t; /* in progress R2T */
309 struct iscsi_queue r2tpool;
310 struct kfifo *r2tqueue;
311 struct iscsi_r2t_info **r2ts;
312 struct list_head dataqueue; /* Data-Out dataqueue */
313 mempool_t *datapool;
314 uint32_t datadigest; /* for recover digest */
315 int digest_count;
316 uint32_t immdigest; /* for imm data */
317 struct iscsi_buf immbuf; /* for imm data digest */
318 struct iscsi_data_task *dtask; /* data task in progress*/
319 int digest_offset; /* for partial buff digest */
320};
321
322#endif /* ISCSI_H */
diff --git a/drivers/scsi/ncr53c8xx.c b/drivers/scsi/ncr53c8xx.c
index 519486d24b2..9a4f576c0d5 100644
--- a/drivers/scsi/ncr53c8xx.c
+++ b/drivers/scsi/ncr53c8xx.c
@@ -3481,8 +3481,8 @@ static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
3481 **---------------------------------------------------- 3481 **----------------------------------------------------
3482 */ 3482 */
3483 if (np->settle_time && cmd->timeout_per_command >= HZ) { 3483 if (np->settle_time && cmd->timeout_per_command >= HZ) {
3484 u_long tlimit = ktime_get(cmd->timeout_per_command - HZ); 3484 u_long tlimit = jiffies + cmd->timeout_per_command - HZ;
3485 if (ktime_dif(np->settle_time, tlimit) > 0) 3485 if (time_after(np->settle_time, tlimit))
3486 np->settle_time = tlimit; 3486 np->settle_time = tlimit;
3487 } 3487 }
3488 3488
@@ -3516,7 +3516,7 @@ static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
3516 ** Force ordered tag if necessary to avoid timeouts 3516 ** Force ordered tag if necessary to avoid timeouts
3517 ** and to preserve interactivity. 3517 ** and to preserve interactivity.
3518 */ 3518 */
3519 if (lp && ktime_exp(lp->tags_stime)) { 3519 if (lp && time_after(jiffies, lp->tags_stime)) {
3520 if (lp->tags_smap) { 3520 if (lp->tags_smap) {
3521 order = M_ORDERED_TAG; 3521 order = M_ORDERED_TAG;
3522 if ((DEBUG_FLAGS & DEBUG_TAGS)||bootverbose>2){ 3522 if ((DEBUG_FLAGS & DEBUG_TAGS)||bootverbose>2){
@@ -3524,7 +3524,7 @@ static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
3524 "ordered tag forced.\n"); 3524 "ordered tag forced.\n");
3525 } 3525 }
3526 } 3526 }
3527 lp->tags_stime = ktime_get(3*HZ); 3527 lp->tags_stime = jiffies + 3*HZ;
3528 lp->tags_smap = lp->tags_umap; 3528 lp->tags_smap = lp->tags_umap;
3529 } 3529 }
3530 3530
@@ -3792,7 +3792,7 @@ static int ncr_reset_scsi_bus(struct ncb *np, int enab_int, int settle_delay)
3792 u32 term; 3792 u32 term;
3793 int retv = 0; 3793 int retv = 0;
3794 3794
3795 np->settle_time = ktime_get(settle_delay * HZ); 3795 np->settle_time = jiffies + settle_delay * HZ;
3796 3796
3797 if (bootverbose > 1) 3797 if (bootverbose > 1)
3798 printk("%s: resetting, " 3798 printk("%s: resetting, "
@@ -5044,7 +5044,7 @@ static void ncr_setup_tags (struct ncb *np, struct scsi_device *sdev)
5044 5044
5045static void ncr_timeout (struct ncb *np) 5045static void ncr_timeout (struct ncb *np)
5046{ 5046{
5047 u_long thistime = ktime_get(0); 5047 u_long thistime = jiffies;
5048 5048
5049 /* 5049 /*
5050 ** If release process in progress, let's go 5050 ** If release process in progress, let's go
@@ -5057,7 +5057,7 @@ static void ncr_timeout (struct ncb *np)
5057 return; 5057 return;
5058 } 5058 }
5059 5059
5060 np->timer.expires = ktime_get(SCSI_NCR_TIMER_INTERVAL); 5060 np->timer.expires = jiffies + SCSI_NCR_TIMER_INTERVAL;
5061 add_timer(&np->timer); 5061 add_timer(&np->timer);
5062 5062
5063 /* 5063 /*
@@ -5336,8 +5336,8 @@ void ncr_exception (struct ncb *np)
5336 **========================================================= 5336 **=========================================================
5337 */ 5337 */
5338 5338
5339 if (ktime_exp(np->regtime)) { 5339 if (time_after(jiffies, np->regtime)) {
5340 np->regtime = ktime_get(10*HZ); 5340 np->regtime = jiffies + 10*HZ;
5341 for (i = 0; i<sizeof(np->regdump); i++) 5341 for (i = 0; i<sizeof(np->regdump); i++)
5342 ((char*)&np->regdump)[i] = INB_OFF(i); 5342 ((char*)&np->regdump)[i] = INB_OFF(i);
5343 np->regdump.nc_dstat = dstat; 5343 np->regdump.nc_dstat = dstat;
@@ -5453,7 +5453,7 @@ static int ncr_int_sbmc (struct ncb *np)
5453 ** Suspend command processing for 1 second and 5453 ** Suspend command processing for 1 second and
5454 ** reinitialize all except the chip. 5454 ** reinitialize all except the chip.
5455 */ 5455 */
5456 np->settle_time = ktime_get(1*HZ); 5456 np->settle_time = jiffies + HZ;
5457 ncr_init (np, 0, bootverbose ? "scsi mode change" : NULL, HS_RESET); 5457 ncr_init (np, 0, bootverbose ? "scsi mode change" : NULL, HS_RESET);
5458 return 1; 5458 return 1;
5459 } 5459 }
@@ -6923,7 +6923,7 @@ static struct lcb *ncr_setup_lcb (struct ncb *np, struct scsi_device *sdev)
6923 for (i = 0 ; i < MAX_TAGS ; i++) 6923 for (i = 0 ; i < MAX_TAGS ; i++)
6924 lp->cb_tags[i] = i; 6924 lp->cb_tags[i] = i;
6925 lp->maxnxs = MAX_TAGS; 6925 lp->maxnxs = MAX_TAGS;
6926 lp->tags_stime = ktime_get(3*HZ); 6926 lp->tags_stime = jiffies + 3*HZ;
6927 ncr_setup_tags (np, sdev); 6927 ncr_setup_tags (np, sdev);
6928 } 6928 }
6929 6929
diff --git a/drivers/scsi/scsi_transport_iscsi.c b/drivers/scsi/scsi_transport_iscsi.c
index 8bb8222ea58..d010aeda9d6 100644
--- a/drivers/scsi/scsi_transport_iscsi.c
+++ b/drivers/scsi/scsi_transport_iscsi.c
@@ -1,8 +1,10 @@
1/* 1/*
2 * iSCSI transport class definitions 2 * iSCSI transport class definitions
3 * 3 *
4 * Copyright (C) IBM Corporation, 2004 4 * Copyright (C) IBM Corporation, 2004
5 * Copyright (C) Mike Christie, 2004 5 * Copyright (C) Mike Christie, 2004 - 2005
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005
6 * 8 *
7 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
@@ -19,370 +21,1254 @@
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */ 22 */
21#include <linux/module.h> 23#include <linux/module.h>
24#include <linux/mempool.h>
25#include <net/tcp.h>
22#include <scsi/scsi.h> 26#include <scsi/scsi.h>
23#include <scsi/scsi_host.h> 27#include <scsi/scsi_host.h>
24#include <scsi/scsi_device.h> 28#include <scsi/scsi_device.h>
25#include <scsi/scsi_transport.h> 29#include <scsi/scsi_transport.h>
26#include <scsi/scsi_transport_iscsi.h> 30#include <scsi/scsi_transport_iscsi.h>
31#include <scsi/iscsi_if.h>
27 32
28#define ISCSI_SESSION_ATTRS 20 33#define ISCSI_SESSION_ATTRS 8
29#define ISCSI_HOST_ATTRS 2 34#define ISCSI_CONN_ATTRS 6
30 35
31struct iscsi_internal { 36struct iscsi_internal {
32 struct scsi_transport_template t; 37 struct scsi_transport_template t;
33 struct iscsi_function_template *fnt; 38 struct iscsi_transport *iscsi_transport;
39 struct list_head list;
40 /*
41 * List of sessions for this transport
42 */
43 struct list_head sessions;
44 /*
45 * lock to serialize access to the sessions list which must
46 * be taken after the rx_queue_sema
47 */
48 spinlock_t session_lock;
49 /*
50 * based on transport capabilities, at register time we set these
51 * bits to tell the transport class it wants attributes displayed
52 * in sysfs or that it can support different iSCSI Data-Path
53 * capabilities
54 */
55 uint32_t param_mask;
56
57 struct class_device cdev;
34 /* 58 /*
35 * We do not have any private or other attrs. 59 * We do not have any private or other attrs.
36 */ 60 */
61 struct transport_container conn_cont;
62 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
63 struct transport_container session_cont;
37 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1]; 64 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
38 struct class_device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
39}; 65};
40 66
41#define to_iscsi_internal(tmpl) container_of(tmpl, struct iscsi_internal, t) 67/*
68 * list of registered transports and lock that must
69 * be held while accessing list. The iscsi_transport_lock must
70 * be acquired after the rx_queue_sema.
71 */
72static LIST_HEAD(iscsi_transports);
73static DEFINE_SPINLOCK(iscsi_transport_lock);
74
75#define to_iscsi_internal(tmpl) \
76 container_of(tmpl, struct iscsi_internal, t)
77
78#define cdev_to_iscsi_internal(_cdev) \
79 container_of(_cdev, struct iscsi_internal, cdev)
80
81static void iscsi_transport_release(struct class_device *cdev)
82{
83 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
84 kfree(priv);
85}
42 86
43static DECLARE_TRANSPORT_CLASS(iscsi_transport_class, 87/*
44 "iscsi_transport", 88 * iscsi_transport_class represents the iscsi_transports that are
89 * registered.
90 */
91static struct class iscsi_transport_class = {
92 .name = "iscsi_transport",
93 .release = iscsi_transport_release,
94};
95
96static ssize_t
97show_transport_handle(struct class_device *cdev, char *buf)
98{
99 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
100 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
101}
102static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
103
104#define show_transport_attr(name, format) \
105static ssize_t \
106show_transport_##name(struct class_device *cdev, char *buf) \
107{ \
108 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
109 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
110} \
111static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
112
113show_transport_attr(caps, "0x%x");
114show_transport_attr(max_lun, "%d");
115show_transport_attr(max_conn, "%d");
116show_transport_attr(max_cmd_len, "%d");
117
118static struct attribute *iscsi_transport_attrs[] = {
119 &class_device_attr_handle.attr,
120 &class_device_attr_caps.attr,
121 &class_device_attr_max_lun.attr,
122 &class_device_attr_max_conn.attr,
123 &class_device_attr_max_cmd_len.attr,
124 NULL,
125};
126
127static struct attribute_group iscsi_transport_group = {
128 .attrs = iscsi_transport_attrs,
129};
130
131static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
132 "iscsi_session",
45 NULL, 133 NULL,
46 NULL, 134 NULL,
47 NULL); 135 NULL);
48 136
49static DECLARE_TRANSPORT_CLASS(iscsi_host_class, 137static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
50 "iscsi_host", 138 "iscsi_connection",
51 NULL, 139 NULL,
52 NULL, 140 NULL,
53 NULL); 141 NULL);
142
143static struct sock *nls;
144static int daemon_pid;
145static DECLARE_MUTEX(rx_queue_sema);
146
147struct mempool_zone {
148 mempool_t *pool;
149 atomic_t allocated;
150 int size;
151 int hiwat;
152 struct list_head freequeue;
153 spinlock_t freelock;
154};
155
156static struct mempool_zone z_reply;
157
54/* 158/*
55 * iSCSI target and session attrs 159 * Z_MAX_* - actual mempool size allocated at the mempool_zone_init() time
160 * Z_HIWAT_* - zone's high watermark when if_error bit will be set to -ENOMEM
161 * so daemon will notice OOM on NETLINK tranposrt level and will
162 * be able to predict or change operational behavior
56 */ 163 */
57#define iscsi_session_show_fn(field, format) \ 164#define Z_MAX_REPLY 8
58 \ 165#define Z_HIWAT_REPLY 6
59static ssize_t \ 166#define Z_MAX_PDU 8
60show_session_##field(struct class_device *cdev, char *buf) \ 167#define Z_HIWAT_PDU 6
61{ \ 168#define Z_MAX_ERROR 16
62 struct scsi_target *starget = transport_class_to_starget(cdev); \ 169#define Z_HIWAT_ERROR 12
63 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \ 170
64 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); \ 171struct iscsi_if_conn {
65 \ 172 struct list_head conn_list; /* item in connlist */
66 if (i->fnt->get_##field) \ 173 struct list_head session_list; /* item in session->connections */
67 i->fnt->get_##field(starget); \ 174 iscsi_connh_t connh;
68 return snprintf(buf, 20, format"\n", iscsi_##field(starget)); \ 175 int active; /* must be accessed with the connlock */
176 struct Scsi_Host *host; /* originated shost */
177 struct device dev; /* sysfs transport/container device */
178 struct iscsi_transport *transport;
179 struct mempool_zone z_error;
180 struct mempool_zone z_pdu;
181 struct list_head freequeue;
182};
183
184#define iscsi_dev_to_if_conn(_dev) \
185 container_of(_dev, struct iscsi_if_conn, dev)
186
187#define iscsi_cdev_to_if_conn(_cdev) \
188 iscsi_dev_to_if_conn(_cdev->dev)
189
190static LIST_HEAD(connlist);
191static DEFINE_SPINLOCK(connlock);
192
193struct iscsi_if_session {
194 struct list_head list; /* item in session_list */
195 struct list_head connections;
196 iscsi_sessionh_t sessionh;
197 struct iscsi_transport *transport;
198 struct device dev; /* sysfs transport/container device */
199};
200
201#define iscsi_dev_to_if_session(_dev) \
202 container_of(_dev, struct iscsi_if_session, dev)
203
204#define iscsi_cdev_to_if_session(_cdev) \
205 iscsi_dev_to_if_session(_cdev->dev)
206
207#define iscsi_if_session_to_shost(_session) \
208 dev_to_shost(_session->dev.parent)
209
210static struct iscsi_if_conn*
211iscsi_if_find_conn(uint64_t key)
212{
213 unsigned long flags;
214 struct iscsi_if_conn *conn;
215
216 spin_lock_irqsave(&connlock, flags);
217 list_for_each_entry(conn, &connlist, conn_list)
218 if (conn->connh == key) {
219 spin_unlock_irqrestore(&connlock, flags);
220 return conn;
221 }
222 spin_unlock_irqrestore(&connlock, flags);
223 return NULL;
69} 224}
70 225
71#define iscsi_session_rd_attr(field, format) \ 226static struct iscsi_internal *
72 iscsi_session_show_fn(field, format) \ 227iscsi_if_transport_lookup(struct iscsi_transport *tt)
73static CLASS_DEVICE_ATTR(field, S_IRUGO, show_session_##field, NULL); 228{
229 struct iscsi_internal *priv;
230 unsigned long flags;
231
232 spin_lock_irqsave(&iscsi_transport_lock, flags);
233 list_for_each_entry(priv, &iscsi_transports, list) {
234 if (tt == priv->iscsi_transport) {
235 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
236 return priv;
237 }
238 }
239 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
240 return NULL;
241}
74 242
75iscsi_session_rd_attr(tpgt, "%hu"); 243static inline struct list_head *skb_to_lh(struct sk_buff *skb)
76iscsi_session_rd_attr(tsih, "%2x"); 244{
77iscsi_session_rd_attr(max_recv_data_segment_len, "%u"); 245 return (struct list_head *)&skb->cb;
78iscsi_session_rd_attr(max_burst_len, "%u"); 246}
79iscsi_session_rd_attr(first_burst_len, "%u");
80iscsi_session_rd_attr(def_time2wait, "%hu");
81iscsi_session_rd_attr(def_time2retain, "%hu");
82iscsi_session_rd_attr(max_outstanding_r2t, "%hu");
83iscsi_session_rd_attr(erl, "%d");
84 247
248static void*
249mempool_zone_alloc_skb(unsigned int gfp_mask, void *pool_data)
250{
251 struct mempool_zone *zone = pool_data;
85 252
86#define iscsi_session_show_bool_fn(field) \ 253 return alloc_skb(zone->size, gfp_mask);
87 \
88static ssize_t \
89show_session_bool_##field(struct class_device *cdev, char *buf) \
90{ \
91 struct scsi_target *starget = transport_class_to_starget(cdev); \
92 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
93 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); \
94 \
95 if (i->fnt->get_##field) \
96 i->fnt->get_##field(starget); \
97 \
98 if (iscsi_##field(starget)) \
99 return sprintf(buf, "Yes\n"); \
100 return sprintf(buf, "No\n"); \
101} 254}
102 255
103#define iscsi_session_rd_bool_attr(field) \ 256static void
104 iscsi_session_show_bool_fn(field) \ 257mempool_zone_free_skb(void *element, void *pool_data)
105static CLASS_DEVICE_ATTR(field, S_IRUGO, show_session_bool_##field, NULL); 258{
259 kfree_skb(element);
260}
106 261
107iscsi_session_rd_bool_attr(initial_r2t); 262static void
108iscsi_session_rd_bool_attr(immediate_data); 263mempool_zone_complete(struct mempool_zone *zone)
109iscsi_session_rd_bool_attr(data_pdu_in_order); 264{
110iscsi_session_rd_bool_attr(data_sequence_in_order); 265 unsigned long flags;
266 struct list_head *lh, *n;
111 267
112#define iscsi_session_show_digest_fn(field) \ 268 spin_lock_irqsave(&zone->freelock, flags);
113 \ 269 list_for_each_safe(lh, n, &zone->freequeue) {
114static ssize_t \ 270 struct sk_buff *skb = (struct sk_buff *)((char *)lh -
115show_##field(struct class_device *cdev, char *buf) \ 271 offsetof(struct sk_buff, cb));
116{ \ 272 if (!skb_shared(skb)) {
117 struct scsi_target *starget = transport_class_to_starget(cdev); \ 273 list_del(skb_to_lh(skb));
118 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \ 274 mempool_free(skb, zone->pool);
119 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); \ 275 atomic_dec(&zone->allocated);
120 \ 276 }
121 if (i->fnt->get_##field) \ 277 }
122 i->fnt->get_##field(starget); \ 278 spin_unlock_irqrestore(&zone->freelock, flags);
123 \
124 if (iscsi_##field(starget)) \
125 return sprintf(buf, "CRC32C\n"); \
126 return sprintf(buf, "None\n"); \
127} 279}
128 280
129#define iscsi_session_rd_digest_attr(field) \ 281static int
130 iscsi_session_show_digest_fn(field) \ 282mempool_zone_init(struct mempool_zone *zp, unsigned max, unsigned size,
131static CLASS_DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); 283 unsigned hiwat)
284{
285 zp->pool = mempool_create(max, mempool_zone_alloc_skb,
286 mempool_zone_free_skb, zp);
287 if (!zp->pool)
288 return -ENOMEM;
132 289
133iscsi_session_rd_digest_attr(header_digest); 290 zp->size = size;
134iscsi_session_rd_digest_attr(data_digest); 291 zp->hiwat = hiwat;
135 292
136static ssize_t 293 INIT_LIST_HEAD(&zp->freequeue);
137show_port(struct class_device *cdev, char *buf) 294 spin_lock_init(&zp->freelock);
295 atomic_set(&zp->allocated, 0);
296
297 return 0;
298}
299
300
301static struct sk_buff*
302mempool_zone_get_skb(struct mempool_zone *zone)
138{ 303{
139 struct scsi_target *starget = transport_class_to_starget(cdev); 304 struct sk_buff *skb;
140 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 305
141 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); 306 skb = mempool_alloc(zone->pool, GFP_ATOMIC);
307 if (skb)
308 atomic_inc(&zone->allocated);
309 return skb;
310}
142 311
143 if (i->fnt->get_port) 312static int
144 i->fnt->get_port(starget); 313iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb)
314{
315 unsigned long flags;
316 int rc;
145 317
146 return snprintf(buf, 20, "%hu\n", ntohs(iscsi_port(starget))); 318 skb_get(skb);
319 rc = netlink_unicast(nls, skb, daemon_pid, MSG_DONTWAIT);
320 if (rc < 0) {
321 mempool_free(skb, zone->pool);
322 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
323 return rc;
324 }
325
326 spin_lock_irqsave(&zone->freelock, flags);
327 list_add(skb_to_lh(skb), &zone->freequeue);
328 spin_unlock_irqrestore(&zone->freelock, flags);
329
330 return 0;
147} 331}
148static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
149 332
150static ssize_t 333int iscsi_recv_pdu(iscsi_connh_t connh, struct iscsi_hdr *hdr,
151show_ip_address(struct class_device *cdev, char *buf) 334 char *data, uint32_t data_size)
152{ 335{
153 struct scsi_target *starget = transport_class_to_starget(cdev); 336 struct nlmsghdr *nlh;
154 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 337 struct sk_buff *skb;
155 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); 338 struct iscsi_uevent *ev;
339 struct iscsi_if_conn *conn;
340 char *pdu;
341 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
342 data_size);
343
344 conn = iscsi_if_find_conn(connh);
345 BUG_ON(!conn);
346
347 mempool_zone_complete(&conn->z_pdu);
348
349 skb = mempool_zone_get_skb(&conn->z_pdu);
350 if (!skb) {
351 iscsi_conn_error(connh, ISCSI_ERR_CONN_FAILED);
352 printk(KERN_ERR "iscsi%d: can not deliver control PDU: OOM\n",
353 conn->host->host_no);
354 return -ENOMEM;
355 }
156 356
157 if (i->fnt->get_ip_address) 357 nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
158 i->fnt->get_ip_address(starget); 358 ev = NLMSG_DATA(nlh);
359 memset(ev, 0, sizeof(*ev));
360 ev->transport_handle = iscsi_handle(conn->transport);
361 ev->type = ISCSI_KEVENT_RECV_PDU;
362 if (atomic_read(&conn->z_pdu.allocated) >= conn->z_pdu.hiwat)
363 ev->iferror = -ENOMEM;
364 ev->r.recv_req.conn_handle = connh;
365 pdu = (char*)ev + sizeof(*ev);
366 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
367 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
159 368
160 if (iscsi_addr_type(starget) == AF_INET) 369 return iscsi_unicast_skb(&conn->z_pdu, skb);
161 return sprintf(buf, "%u.%u.%u.%u\n",
162 NIPQUAD(iscsi_sin_addr(starget)));
163 else if(iscsi_addr_type(starget) == AF_INET6)
164 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
165 NIP6(iscsi_sin6_addr(starget)));
166 return -EINVAL;
167} 370}
168static CLASS_DEVICE_ATTR(ip_address, S_IRUGO, show_ip_address, NULL); 371EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
169 372
170static ssize_t 373void iscsi_conn_error(iscsi_connh_t connh, enum iscsi_err error)
171show_isid(struct class_device *cdev, char *buf)
172{ 374{
173 struct scsi_target *starget = transport_class_to_starget(cdev); 375 struct nlmsghdr *nlh;
174 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 376 struct sk_buff *skb;
175 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); 377 struct iscsi_uevent *ev;
378 struct iscsi_if_conn *conn;
379 int len = NLMSG_SPACE(sizeof(*ev));
380
381 conn = iscsi_if_find_conn(connh);
382 BUG_ON(!conn);
383
384 mempool_zone_complete(&conn->z_error);
385
386 skb = mempool_zone_get_skb(&conn->z_error);
387 if (!skb) {
388 printk(KERN_ERR "iscsi%d: gracefully ignored conn error (%d)\n",
389 conn->host->host_no, error);
390 return;
391 }
392
393 nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
394 ev = NLMSG_DATA(nlh);
395 ev->transport_handle = iscsi_handle(conn->transport);
396 ev->type = ISCSI_KEVENT_CONN_ERROR;
397 if (atomic_read(&conn->z_error.allocated) >= conn->z_error.hiwat)
398 ev->iferror = -ENOMEM;
399 ev->r.connerror.error = error;
400 ev->r.connerror.conn_handle = connh;
176 401
177 if (i->fnt->get_isid) 402 iscsi_unicast_skb(&conn->z_error, skb);
178 i->fnt->get_isid(starget);
179 403
180 return sprintf(buf, "%02x%02x%02x%02x%02x%02x\n", 404 printk(KERN_INFO "iscsi%d: detected conn error (%d)\n",
181 iscsi_isid(starget)[0], iscsi_isid(starget)[1], 405 conn->host->host_no, error);
182 iscsi_isid(starget)[2], iscsi_isid(starget)[3], 406}
183 iscsi_isid(starget)[4], iscsi_isid(starget)[5]); 407EXPORT_SYMBOL_GPL(iscsi_conn_error);
408
409static int
410iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
411 void *payload, int size)
412{
413 struct sk_buff *skb;
414 struct nlmsghdr *nlh;
415 int len = NLMSG_SPACE(size);
416 int flags = multi ? NLM_F_MULTI : 0;
417 int t = done ? NLMSG_DONE : type;
418
419 mempool_zone_complete(&z_reply);
420
421 skb = mempool_zone_get_skb(&z_reply);
422 /*
423 * FIXME:
424 * user is supposed to react on iferror == -ENOMEM;
425 * see iscsi_if_rx().
426 */
427 BUG_ON(!skb);
428
429 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
430 nlh->nlmsg_flags = flags;
431 memcpy(NLMSG_DATA(nlh), payload, size);
432 return iscsi_unicast_skb(&z_reply, skb);
184} 433}
185static CLASS_DEVICE_ATTR(isid, S_IRUGO, show_isid, NULL);
186 434
187/* 435/*
188 * This is used for iSCSI names. Normally, we follow 436 * iSCSI Session's hostdata organization:
189 * the transport class convention of having the lld 437 *
190 * set the field, but in these cases the value is 438 * *------------------* <== host->hostdata
191 * too large. 439 * | transport |
440 * |------------------| <== iscsi_hostdata(host->hostdata)
441 * | transport's data |
442 * |------------------| <== hostdata_session(host->hostdata)
443 * | interface's data |
444 * *------------------*
192 */ 445 */
193#define iscsi_session_show_str_fn(field) \ 446
194 \ 447#define hostdata_privsize(_t) (sizeof(unsigned long) + _t->hostdata_size + \
195static ssize_t \ 448 _t->hostdata_size % sizeof(unsigned long) + \
196show_session_str_##field(struct class_device *cdev, char *buf) \ 449 sizeof(struct iscsi_if_session))
197{ \ 450
198 ssize_t ret = 0; \ 451#define hostdata_session(_hostdata) ((void*)_hostdata + sizeof(unsigned long) + \
199 struct scsi_target *starget = transport_class_to_starget(cdev); \ 452 ((struct iscsi_transport *) \
200 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \ 453 iscsi_ptr(*(uint64_t *)_hostdata))->hostdata_size)
201 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); \ 454
202 \ 455static void iscsi_if_session_dev_release(struct device *dev)
203 if (i->fnt->get_##field) \ 456{
204 ret = i->fnt->get_##field(starget, buf, PAGE_SIZE); \ 457 struct iscsi_if_session *session = iscsi_dev_to_if_session(dev);
205 return ret; \ 458 struct iscsi_transport *transport = session->transport;
459 struct Scsi_Host *shost = iscsi_if_session_to_shost(session);
460 struct iscsi_if_conn *conn, *tmp;
461 unsigned long flags;
462
463 /* now free connections */
464 spin_lock_irqsave(&connlock, flags);
465 list_for_each_entry_safe(conn, tmp, &session->connections,
466 session_list) {
467 list_del(&conn->session_list);
468 mempool_destroy(conn->z_pdu.pool);
469 mempool_destroy(conn->z_error.pool);
470 kfree(conn);
471 }
472 spin_unlock_irqrestore(&connlock, flags);
473 scsi_host_put(shost);
474 module_put(transport->owner);
475}
476
477static int
478iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
479{
480 struct iscsi_transport *transport = priv->iscsi_transport;
481 struct iscsi_if_session *session;
482 struct Scsi_Host *shost;
483 unsigned long flags;
484 int error;
485
486 if (!try_module_get(transport->owner))
487 return -EPERM;
488
489 shost = scsi_host_alloc(transport->host_template,
490 hostdata_privsize(transport));
491 if (!shost) {
492 ev->r.c_session_ret.session_handle = iscsi_handle(NULL);
493 printk(KERN_ERR "iscsi: can not allocate SCSI host for "
494 "session\n");
495 error = -ENOMEM;
496 goto out_module_put;
497 }
498 shost->max_id = 1;
499 shost->max_channel = 0;
500 shost->max_lun = transport->max_lun;
501 shost->max_cmd_len = transport->max_cmd_len;
502 shost->transportt = &priv->t;
503
504 /* store struct iscsi_transport in hostdata */
505 *(uint64_t*)shost->hostdata = ev->transport_handle;
506
507 ev->r.c_session_ret.session_handle = transport->create_session(
508 ev->u.c_session.initial_cmdsn, shost);
509 if (ev->r.c_session_ret.session_handle == iscsi_handle(NULL)) {
510 error = 0;
511 goto out_host_put;
512 }
513
514 /* host_no becomes assigned SID */
515 ev->r.c_session_ret.sid = shost->host_no;
516 /* initialize session */
517 session = hostdata_session(shost->hostdata);
518 INIT_LIST_HEAD(&session->connections);
519 INIT_LIST_HEAD(&session->list);
520 session->sessionh = ev->r.c_session_ret.session_handle;
521 session->transport = transport;
522
523 error = scsi_add_host(shost, NULL);
524 if (error)
525 goto out_destroy_session;
526
527 /*
528 * this is released in the dev's release function)
529 */
530 scsi_host_get(shost);
531 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u", shost->host_no);
532 session->dev.parent = &shost->shost_gendev;
533 session->dev.release = iscsi_if_session_dev_release;
534 error = device_register(&session->dev);
535 if (error) {
536 printk(KERN_ERR "iscsi: could not register session%d's dev\n",
537 shost->host_no);
538 goto out_remove_host;
539 }
540 transport_register_device(&session->dev);
541
542 /* add this session to the list of active sessions */
543 spin_lock_irqsave(&priv->session_lock, flags);
544 list_add(&session->list, &priv->sessions);
545 spin_unlock_irqrestore(&priv->session_lock, flags);
546
547 return 0;
548
549out_remove_host:
550 scsi_remove_host(shost);
551out_destroy_session:
552 transport->destroy_session(ev->r.c_session_ret.session_handle);
553 ev->r.c_session_ret.session_handle = iscsi_handle(NULL);
554out_host_put:
555 scsi_host_put(shost);
556out_module_put:
557 module_put(transport->owner);
558 return error;
559}
560
561static int
562iscsi_if_destroy_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
563{
564 struct iscsi_transport *transport = priv->iscsi_transport;
565 struct Scsi_Host *shost;
566 struct iscsi_if_session *session;
567 unsigned long flags;
568 struct iscsi_if_conn *conn;
569 int error = 0;
570
571 shost = scsi_host_lookup(ev->u.d_session.sid);
572 if (shost == ERR_PTR(-ENXIO))
573 return -EEXIST;
574 session = hostdata_session(shost->hostdata);
575
576 /* check if we have active connections */
577 spin_lock_irqsave(&connlock, flags);
578 list_for_each_entry(conn, &session->connections, session_list) {
579 if (conn->active) {
580 printk(KERN_ERR "iscsi%d: can not destroy session: "
581 "has active connection (%p)\n",
582 shost->host_no, iscsi_ptr(conn->connh));
583 spin_unlock_irqrestore(&connlock, flags);
584 error = EIO;
585 goto out_release_ref;
586 }
587 }
588 spin_unlock_irqrestore(&connlock, flags);
589
590 scsi_remove_host(shost);
591 transport->destroy_session(ev->u.d_session.session_handle);
592 transport_unregister_device(&session->dev);
593 device_unregister(&session->dev);
594
595 /* remove this session from the list of active sessions */
596 spin_lock_irqsave(&priv->session_lock, flags);
597 list_del(&session->list);
598 spin_unlock_irqrestore(&priv->session_lock, flags);
599
600 /* ref from host alloc */
601 scsi_host_put(shost);
602out_release_ref:
603 /* ref from host lookup */
604 scsi_host_put(shost);
605 return error;
606}
607
608static void iscsi_if_conn_dev_release(struct device *dev)
609{
610 struct iscsi_if_conn *conn = iscsi_dev_to_if_conn(dev);
611 struct Scsi_Host *shost = conn->host;
612
613 scsi_host_put(shost);
614}
615
616static int
617iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
618{
619 struct iscsi_if_session *session;
620 struct Scsi_Host *shost;
621 struct iscsi_if_conn *conn;
622 unsigned long flags;
623 int error;
624
625 shost = scsi_host_lookup(ev->u.c_conn.sid);
626 if (shost == ERR_PTR(-ENXIO))
627 return -EEXIST;
628 session = hostdata_session(shost->hostdata);
629
630 conn = kmalloc(sizeof(struct iscsi_if_conn), GFP_KERNEL);
631 if (!conn) {
632 error = -ENOMEM;
633 goto out_release_ref;
634 }
635 memset(conn, 0, sizeof(struct iscsi_if_conn));
636 INIT_LIST_HEAD(&conn->session_list);
637 INIT_LIST_HEAD(&conn->conn_list);
638 conn->host = shost;
639 conn->transport = transport;
640
641 error = mempool_zone_init(&conn->z_pdu, Z_MAX_PDU,
642 NLMSG_SPACE(sizeof(struct iscsi_uevent) +
643 sizeof(struct iscsi_hdr) +
644 DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH),
645 Z_HIWAT_PDU);
646 if (error) {
647 printk(KERN_ERR "iscsi%d: can not allocate pdu zone for new "
648 "conn\n", shost->host_no);
649 goto out_free_conn;
650 }
651 error = mempool_zone_init(&conn->z_error, Z_MAX_ERROR,
652 NLMSG_SPACE(sizeof(struct iscsi_uevent)),
653 Z_HIWAT_ERROR);
654 if (error) {
655 printk(KERN_ERR "iscsi%d: can not allocate error zone for "
656 "new conn\n", shost->host_no);
657 goto out_free_pdu_pool;
658 }
659
660 ev->r.handle = transport->create_conn(ev->u.c_conn.session_handle,
661 ev->u.c_conn.cid);
662 if (!ev->r.handle) {
663 error = -ENODEV;
664 goto out_free_error_pool;
665 }
666
667 conn->connh = ev->r.handle;
668
669 /*
670 * this is released in the dev's release function
671 */
672 if (!scsi_host_get(shost))
673 goto out_destroy_conn;
674 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
675 shost->host_no, ev->u.c_conn.cid);
676 conn->dev.parent = &session->dev;
677 conn->dev.release = iscsi_if_conn_dev_release;
678 error = device_register(&conn->dev);
679 if (error) {
680 printk(KERN_ERR "iscsi%d: could not register connections%u "
681 "dev\n", shost->host_no, ev->u.c_conn.cid);
682 goto out_release_parent_ref;
683 }
684 transport_register_device(&conn->dev);
685
686 spin_lock_irqsave(&connlock, flags);
687 list_add(&conn->conn_list, &connlist);
688 list_add(&conn->session_list, &session->connections);
689 conn->active = 1;
690 spin_unlock_irqrestore(&connlock, flags);
691
692 scsi_host_put(shost);
693 return 0;
694
695out_release_parent_ref:
696 scsi_host_put(shost);
697out_destroy_conn:
698 transport->destroy_conn(ev->r.handle);
699out_free_error_pool:
700 mempool_destroy(conn->z_error.pool);
701out_free_pdu_pool:
702 mempool_destroy(conn->z_pdu.pool);
703out_free_conn:
704 kfree(conn);
705out_release_ref:
706 scsi_host_put(shost);
707 return error;
206} 708}
207 709
208#define iscsi_session_rd_str_attr(field) \ 710static int
209 iscsi_session_show_str_fn(field) \ 711iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
210static CLASS_DEVICE_ATTR(field, S_IRUGO, show_session_str_##field, NULL); 712{
713 unsigned long flags;
714 struct iscsi_if_conn *conn;
715
716 conn = iscsi_if_find_conn(ev->u.d_conn.conn_handle);
717 if (!conn)
718 return -EEXIST;
719
720 transport->destroy_conn(ev->u.d_conn.conn_handle);
721
722 spin_lock_irqsave(&connlock, flags);
723 conn->active = 0;
724 list_del(&conn->conn_list);
725 spin_unlock_irqrestore(&connlock, flags);
726
727 transport_unregister_device(&conn->dev);
728 device_unregister(&conn->dev);
729 return 0;
730}
731
732static int
733iscsi_if_get_stats(struct iscsi_transport *transport, struct sk_buff *skb,
734 struct nlmsghdr *nlh)
735{
736 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
737 struct iscsi_stats *stats;
738 struct sk_buff *skbstat;
739 struct iscsi_if_conn *conn;
740 struct nlmsghdr *nlhstat;
741 struct iscsi_uevent *evstat;
742 int len = NLMSG_SPACE(sizeof(*ev) +
743 sizeof(struct iscsi_stats) +
744 sizeof(struct iscsi_stats_custom) *
745 ISCSI_STATS_CUSTOM_MAX);
746 int err = 0;
747
748 conn = iscsi_if_find_conn(ev->u.get_stats.conn_handle);
749 if (!conn)
750 return -EEXIST;
751
752 do {
753 int actual_size;
754
755 mempool_zone_complete(&conn->z_pdu);
756
757 skbstat = mempool_zone_get_skb(&conn->z_pdu);
758 if (!skbstat) {
759 printk(KERN_ERR "iscsi%d: can not deliver stats: OOM\n",
760 conn->host->host_no);
761 return -ENOMEM;
762 }
763
764 nlhstat = __nlmsg_put(skbstat, daemon_pid, 0, 0,
765 (len - sizeof(*nlhstat)), 0);
766 evstat = NLMSG_DATA(nlhstat);
767 memset(evstat, 0, sizeof(*evstat));
768 evstat->transport_handle = iscsi_handle(conn->transport);
769 evstat->type = nlh->nlmsg_type;
770 if (atomic_read(&conn->z_pdu.allocated) >= conn->z_pdu.hiwat)
771 evstat->iferror = -ENOMEM;
772 evstat->u.get_stats.conn_handle =
773 ev->u.get_stats.conn_handle;
774 stats = (struct iscsi_stats *)
775 ((char*)evstat + sizeof(*evstat));
776 memset(stats, 0, sizeof(*stats));
777
778 transport->get_stats(ev->u.get_stats.conn_handle, stats);
779 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
780 sizeof(struct iscsi_stats) +
781 sizeof(struct iscsi_stats_custom) *
782 stats->custom_length);
783 actual_size -= sizeof(*nlhstat);
784 actual_size = NLMSG_LENGTH(actual_size);
785 skb_trim(skb, NLMSG_ALIGN(actual_size));
786 nlhstat->nlmsg_len = actual_size;
787
788 err = iscsi_unicast_skb(&conn->z_pdu, skbstat);
789 } while (err < 0 && err != -ECONNREFUSED);
790
791 return err;
792}
793
794static int
795iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
796{
797 int err = 0;
798 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
799 struct iscsi_transport *transport = NULL;
800 struct iscsi_internal *priv;
801
802 if (NETLINK_CREDS(skb)->uid)
803 return -EPERM;
804
805 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
806 if (!priv)
807 return -EINVAL;
808 transport = priv->iscsi_transport;
809
810 daemon_pid = NETLINK_CREDS(skb)->pid;
811
812 switch (nlh->nlmsg_type) {
813 case ISCSI_UEVENT_CREATE_SESSION:
814 err = iscsi_if_create_session(priv, ev);
815 break;
816 case ISCSI_UEVENT_DESTROY_SESSION:
817 err = iscsi_if_destroy_session(priv, ev);
818 break;
819 case ISCSI_UEVENT_CREATE_CONN:
820 err = iscsi_if_create_conn(transport, ev);
821 break;
822 case ISCSI_UEVENT_DESTROY_CONN:
823 err = iscsi_if_destroy_conn(transport, ev);
824 break;
825 case ISCSI_UEVENT_BIND_CONN:
826 if (!iscsi_if_find_conn(ev->u.b_conn.conn_handle))
827 return -EEXIST;
828 ev->r.retcode = transport->bind_conn(
829 ev->u.b_conn.session_handle,
830 ev->u.b_conn.conn_handle,
831 ev->u.b_conn.transport_fd,
832 ev->u.b_conn.is_leading);
833 break;
834 case ISCSI_UEVENT_SET_PARAM:
835 if (!iscsi_if_find_conn(ev->u.set_param.conn_handle))
836 return -EEXIST;
837 ev->r.retcode = transport->set_param(
838 ev->u.set_param.conn_handle,
839 ev->u.set_param.param, ev->u.set_param.value);
840 break;
841 case ISCSI_UEVENT_START_CONN:
842 if (!iscsi_if_find_conn(ev->u.start_conn.conn_handle))
843 return -EEXIST;
844 ev->r.retcode = transport->start_conn(
845 ev->u.start_conn.conn_handle);
846 break;
847 case ISCSI_UEVENT_STOP_CONN:
848 if (!iscsi_if_find_conn(ev->u.stop_conn.conn_handle))
849 return -EEXIST;
850 transport->stop_conn(ev->u.stop_conn.conn_handle,
851 ev->u.stop_conn.flag);
852 break;
853 case ISCSI_UEVENT_SEND_PDU:
854 if (!iscsi_if_find_conn(ev->u.send_pdu.conn_handle))
855 return -EEXIST;
856 ev->r.retcode = transport->send_pdu(
857 ev->u.send_pdu.conn_handle,
858 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
859 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
860 ev->u.send_pdu.data_size);
861 break;
862 case ISCSI_UEVENT_GET_STATS:
863 err = iscsi_if_get_stats(transport, skb, nlh);
864 break;
865 default:
866 err = -EINVAL;
867 break;
868 }
869
870 return err;
871}
872
873/* Get message from skb (based on rtnetlink_rcv_skb). Each message is
874 * processed by iscsi_if_recv_msg. Malformed skbs with wrong length are
875 * discarded silently. */
876static void
877iscsi_if_rx(struct sock *sk, int len)
878{
879 struct sk_buff *skb;
211 880
212iscsi_session_rd_str_attr(target_name); 881 down(&rx_queue_sema);
213iscsi_session_rd_str_attr(target_alias); 882 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
883 while (skb->len >= NLMSG_SPACE(0)) {
884 int err;
885 uint32_t rlen;
886 struct nlmsghdr *nlh;
887 struct iscsi_uevent *ev;
888
889 nlh = (struct nlmsghdr *)skb->data;
890 if (nlh->nlmsg_len < sizeof(*nlh) ||
891 skb->len < nlh->nlmsg_len) {
892 break;
893 }
894 ev = NLMSG_DATA(nlh);
895 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
896 if (rlen > skb->len)
897 rlen = skb->len;
898 err = iscsi_if_recv_msg(skb, nlh);
899 if (err) {
900 ev->type = ISCSI_KEVENT_IF_ERROR;
901 ev->iferror = err;
902 }
903 do {
904 /*
905 * special case for GET_STATS:
906 * on success - sending reply and stats from
907 * inside of if_recv_msg(),
908 * on error - fall through.
909 */
910 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
911 break;
912 err = iscsi_if_send_reply(
913 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
914 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
915 if (atomic_read(&z_reply.allocated) >=
916 z_reply.hiwat)
917 ev->iferror = -ENOMEM;
918 } while (err < 0 && err != -ECONNREFUSED);
919 skb_pull(skb, rlen);
920 }
921 kfree_skb(skb);
922 }
923 up(&rx_queue_sema);
924}
214 925
215/* 926/*
216 * iSCSI host attrs 927 * iSCSI connection attrs
217 */ 928 */
929#define iscsi_conn_int_attr_show(param, format) \
930static ssize_t \
931show_conn_int_param_##param(struct class_device *cdev, char *buf) \
932{ \
933 uint32_t value = 0; \
934 struct iscsi_if_conn *conn = iscsi_cdev_to_if_conn(cdev); \
935 struct iscsi_internal *priv; \
936 \
937 priv = to_iscsi_internal(conn->host->transportt); \
938 if (priv->param_mask & (1 << param)) \
939 priv->iscsi_transport->get_param(conn->connh, param, &value); \
940 return snprintf(buf, 20, format"\n", value); \
941}
942
943#define iscsi_conn_int_attr(field, param, format) \
944 iscsi_conn_int_attr_show(param, format) \
945static CLASS_DEVICE_ATTR(field, S_IRUGO, show_conn_int_param_##param, NULL);
946
947iscsi_conn_int_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH, "%u");
948iscsi_conn_int_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH, "%u");
949iscsi_conn_int_attr(header_digest, ISCSI_PARAM_HDRDGST_EN, "%d");
950iscsi_conn_int_attr(data_digest, ISCSI_PARAM_DATADGST_EN, "%d");
951iscsi_conn_int_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN, "%d");
952iscsi_conn_int_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN, "%d");
218 953
219/* 954/*
220 * Again, this is used for iSCSI names. Normally, we follow 955 * iSCSI session attrs
221 * the transport class convention of having the lld set
222 * the field, but in these cases the value is too large.
223 */ 956 */
224#define iscsi_host_show_str_fn(field) \ 957#define iscsi_session_int_attr_show(param, format) \
225 \
226static ssize_t \ 958static ssize_t \
227show_host_str_##field(struct class_device *cdev, char *buf) \ 959show_session_int_param_##param(struct class_device *cdev, char *buf) \
228{ \ 960{ \
229 int ret = 0; \ 961 uint32_t value = 0; \
230 struct Scsi_Host *shost = transport_class_to_shost(cdev); \ 962 struct iscsi_if_session *session = iscsi_cdev_to_if_session(cdev); \
231 struct iscsi_internal *i = to_iscsi_internal(shost->transportt); \ 963 struct Scsi_Host *shost = iscsi_if_session_to_shost(session); \
964 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
965 struct iscsi_if_conn *conn = NULL; \
966 unsigned long flags; \
967 \
968 spin_lock_irqsave(&connlock, flags); \
969 if (!list_empty(&session->connections)) \
970 conn = list_entry(session->connections.next, \
971 struct iscsi_if_conn, session_list); \
972 spin_unlock_irqrestore(&connlock, flags); \
232 \ 973 \
233 if (i->fnt->get_##field) \ 974 if (conn && (priv->param_mask & (1 << param))) \
234 ret = i->fnt->get_##field(shost, buf, PAGE_SIZE); \ 975 priv->iscsi_transport->get_param(conn->connh, param, &value);\
235 return ret; \ 976 return snprintf(buf, 20, format"\n", value); \
236} 977}
237 978
238#define iscsi_host_rd_str_attr(field) \ 979#define iscsi_session_int_attr(field, param, format) \
239 iscsi_host_show_str_fn(field) \ 980 iscsi_session_int_attr_show(param, format) \
240static CLASS_DEVICE_ATTR(field, S_IRUGO, show_host_str_##field, NULL); 981static CLASS_DEVICE_ATTR(field, S_IRUGO, show_session_int_param_##param, NULL);
241 982
242iscsi_host_rd_str_attr(initiator_name); 983iscsi_session_int_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, "%d");
243iscsi_host_rd_str_attr(initiator_alias); 984iscsi_session_int_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, "%hu");
985iscsi_session_int_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, "%d");
986iscsi_session_int_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, "%u");
987iscsi_session_int_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, "%u");
988iscsi_session_int_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, "%d");
989iscsi_session_int_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, "%d");
990iscsi_session_int_attr(erl, ISCSI_PARAM_ERL, "%d");
244 991
245#define SETUP_SESSION_RD_ATTR(field) \ 992#define SETUP_SESSION_RD_ATTR(field, param) \
246 if (i->fnt->show_##field) { \ 993 if (priv->param_mask & (1 << param)) { \
247 i->session_attrs[count] = &class_device_attr_##field; \ 994 priv->session_attrs[count] = &class_device_attr_##field;\
248 count++; \ 995 count++; \
249 } 996 }
250 997
251#define SETUP_HOST_RD_ATTR(field) \ 998#define SETUP_CONN_RD_ATTR(field, param) \
252 if (i->fnt->show_##field) { \ 999 if (priv->param_mask & (1 << param)) { \
253 i->host_attrs[count] = &class_device_attr_##field; \ 1000 priv->conn_attrs[count] = &class_device_attr_##field; \
254 count++; \ 1001 count++; \
255 } 1002 }
256 1003
257static int iscsi_host_match(struct attribute_container *cont, 1004static int iscsi_is_session_dev(const struct device *dev)
258 struct device *dev) 1005{
1006 return dev->release == iscsi_if_session_dev_release;
1007}
1008
1009static int iscsi_session_match(struct attribute_container *cont,
1010 struct device *dev)
259{ 1011{
1012 struct iscsi_if_session *session;
260 struct Scsi_Host *shost; 1013 struct Scsi_Host *shost;
261 struct iscsi_internal *i; 1014 struct iscsi_internal *priv;
1015
1016 if (!iscsi_is_session_dev(dev))
1017 return 0;
262 1018
263 if (!scsi_is_host_device(dev)) 1019 session = iscsi_dev_to_if_session(dev);
1020 shost = iscsi_if_session_to_shost(session);
1021 if (!shost->transportt)
264 return 0; 1022 return 0;
265 1023
266 shost = dev_to_shost(dev); 1024 priv = to_iscsi_internal(shost->transportt);
267 if (!shost->transportt || shost->transportt->host_attrs.ac.class 1025 if (priv->session_cont.ac.class != &iscsi_session_class.class)
268 != &iscsi_host_class.class)
269 return 0; 1026 return 0;
270 1027
271 i = to_iscsi_internal(shost->transportt); 1028 return &priv->session_cont.ac == cont;
272
273 return &i->t.host_attrs.ac == cont;
274} 1029}
275 1030
276static int iscsi_target_match(struct attribute_container *cont, 1031static int iscsi_is_conn_dev(const struct device *dev)
277 struct device *dev)
278{ 1032{
1033 return dev->release == iscsi_if_conn_dev_release;
1034}
1035
1036static int iscsi_conn_match(struct attribute_container *cont,
1037 struct device *dev)
1038{
1039 struct iscsi_if_conn *conn;
279 struct Scsi_Host *shost; 1040 struct Scsi_Host *shost;
280 struct iscsi_internal *i; 1041 struct iscsi_internal *priv;
281 1042
282 if (!scsi_is_target_device(dev)) 1043 if (!iscsi_is_conn_dev(dev))
283 return 0; 1044 return 0;
284 1045
285 shost = dev_to_shost(dev->parent); 1046 conn = iscsi_dev_to_if_conn(dev);
286 if (!shost->transportt || shost->transportt->host_attrs.ac.class 1047 shost = conn->host;
287 != &iscsi_host_class.class) 1048 if (!shost->transportt)
288 return 0; 1049 return 0;
289 1050
290 i = to_iscsi_internal(shost->transportt); 1051 priv = to_iscsi_internal(shost->transportt);
291 1052 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
292 return &i->t.target_attrs.ac == cont; 1053 return 0;
293}
294
295struct scsi_transport_template *
296iscsi_attach_transport(struct iscsi_function_template *fnt)
297{
298 struct iscsi_internal *i = kmalloc(sizeof(struct iscsi_internal),
299 GFP_KERNEL);
300 int count = 0;
301
302 if (unlikely(!i))
303 return NULL;
304
305 memset(i, 0, sizeof(struct iscsi_internal));
306 i->fnt = fnt;
307
308 i->t.target_attrs.ac.attrs = &i->session_attrs[0];
309 i->t.target_attrs.ac.class = &iscsi_transport_class.class;
310 i->t.target_attrs.ac.match = iscsi_target_match;
311 transport_container_register(&i->t.target_attrs);
312 i->t.target_size = sizeof(struct iscsi_class_session);
313
314 SETUP_SESSION_RD_ATTR(tsih);
315 SETUP_SESSION_RD_ATTR(isid);
316 SETUP_SESSION_RD_ATTR(header_digest);
317 SETUP_SESSION_RD_ATTR(data_digest);
318 SETUP_SESSION_RD_ATTR(target_name);
319 SETUP_SESSION_RD_ATTR(target_alias);
320 SETUP_SESSION_RD_ATTR(port);
321 SETUP_SESSION_RD_ATTR(tpgt);
322 SETUP_SESSION_RD_ATTR(ip_address);
323 SETUP_SESSION_RD_ATTR(initial_r2t);
324 SETUP_SESSION_RD_ATTR(immediate_data);
325 SETUP_SESSION_RD_ATTR(max_recv_data_segment_len);
326 SETUP_SESSION_RD_ATTR(max_burst_len);
327 SETUP_SESSION_RD_ATTR(first_burst_len);
328 SETUP_SESSION_RD_ATTR(def_time2wait);
329 SETUP_SESSION_RD_ATTR(def_time2retain);
330 SETUP_SESSION_RD_ATTR(max_outstanding_r2t);
331 SETUP_SESSION_RD_ATTR(data_pdu_in_order);
332 SETUP_SESSION_RD_ATTR(data_sequence_in_order);
333 SETUP_SESSION_RD_ATTR(erl);
334 1054
335 BUG_ON(count > ISCSI_SESSION_ATTRS); 1055 return &priv->conn_cont.ac == cont;
336 i->session_attrs[count] = NULL; 1056}
1057
1058int iscsi_register_transport(struct iscsi_transport *tt)
1059{
1060 struct iscsi_internal *priv;
1061 unsigned long flags;
1062 int count = 0, err;
1063
1064 BUG_ON(!tt);
1065
1066 priv = iscsi_if_transport_lookup(tt);
1067 if (priv)
1068 return -EEXIST;
1069
1070 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
1071 if (!priv)
1072 return -ENOMEM;
1073 memset(priv, 0, sizeof(*priv));
1074 INIT_LIST_HEAD(&priv->list);
1075 INIT_LIST_HEAD(&priv->sessions);
1076 spin_lock_init(&priv->session_lock);
1077 priv->iscsi_transport = tt;
1078
1079 priv->cdev.class = &iscsi_transport_class;
1080 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1081 err = class_device_register(&priv->cdev);
1082 if (err)
1083 goto free_priv;
1084
1085 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1086 if (err)
1087 goto unregister_cdev;
1088
1089 /* setup parameters mask */
1090 priv->param_mask = 0xFFFFFFFF;
1091 if (!(tt->caps & CAP_MULTI_R2T))
1092 priv->param_mask &= ~(1 << ISCSI_PARAM_MAX_R2T);
1093 if (!(tt->caps & CAP_HDRDGST))
1094 priv->param_mask &= ~(1 << ISCSI_PARAM_HDRDGST_EN);
1095 if (!(tt->caps & CAP_DATADGST))
1096 priv->param_mask &= ~(1 << ISCSI_PARAM_DATADGST_EN);
1097 if (!(tt->caps & CAP_MARKERS)) {
1098 priv->param_mask &= ~(1 << ISCSI_PARAM_IFMARKER_EN);
1099 priv->param_mask &= ~(1 << ISCSI_PARAM_OFMARKER_EN);
1100 }
337 1101
338 i->t.host_attrs.ac.attrs = &i->host_attrs[0]; 1102 /* connection parameters */
339 i->t.host_attrs.ac.class = &iscsi_host_class.class; 1103 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
340 i->t.host_attrs.ac.match = iscsi_host_match; 1104 priv->conn_cont.ac.class = &iscsi_connection_class.class;
341 transport_container_register(&i->t.host_attrs); 1105 priv->conn_cont.ac.match = iscsi_conn_match;
342 i->t.host_size = 0; 1106 transport_container_register(&priv->conn_cont);
343 1107
1108 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1109 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1110 SETUP_CONN_RD_ATTR(header_digest, ISCSI_PARAM_HDRDGST_EN);
1111 SETUP_CONN_RD_ATTR(data_digest, ISCSI_PARAM_DATADGST_EN);
1112 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1113 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1114
1115 BUG_ON(count > ISCSI_CONN_ATTRS);
1116 priv->conn_attrs[count] = NULL;
344 count = 0; 1117 count = 0;
345 SETUP_HOST_RD_ATTR(initiator_name);
346 SETUP_HOST_RD_ATTR(initiator_alias);
347 1118
348 BUG_ON(count > ISCSI_HOST_ATTRS); 1119 /* session parameters */
349 i->host_attrs[count] = NULL; 1120 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1121 priv->session_cont.ac.class = &iscsi_session_class.class;
1122 priv->session_cont.ac.match = iscsi_session_match;
1123 transport_container_register(&priv->session_cont);
1124
1125 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN);
1126 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T);
1127 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_PARAM_IMM_DATA_EN);
1128 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_PARAM_FIRST_BURST);
1129 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_PARAM_MAX_BURST);
1130 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN);
1131 SETUP_SESSION_RD_ATTR(data_seq_in_order,ISCSI_PARAM_DATASEQ_INORDER_EN)
1132 SETUP_SESSION_RD_ATTR(erl, ISCSI_PARAM_ERL);
1133
1134 BUG_ON(count > ISCSI_SESSION_ATTRS);
1135 priv->session_attrs[count] = NULL;
1136
1137 spin_lock_irqsave(&iscsi_transport_lock, flags);
1138 list_add(&priv->list, &iscsi_transports);
1139 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1140
1141 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
1142 return 0;
350 1143
351 return &i->t; 1144unregister_cdev:
1145 class_device_unregister(&priv->cdev);
1146free_priv:
1147 kfree(priv);
1148 return err;
352} 1149}
1150EXPORT_SYMBOL_GPL(iscsi_register_transport);
1151
1152int iscsi_unregister_transport(struct iscsi_transport *tt)
1153{
1154 struct iscsi_internal *priv;
1155 unsigned long flags;
1156
1157 BUG_ON(!tt);
1158
1159 down(&rx_queue_sema);
1160
1161 priv = iscsi_if_transport_lookup(tt);
1162 BUG_ON (!priv);
1163
1164 spin_lock_irqsave(&priv->session_lock, flags);
1165 if (!list_empty(&priv->sessions)) {
1166 spin_unlock_irqrestore(&priv->session_lock, flags);
1167 up(&rx_queue_sema);
1168 return -EPERM;
1169 }
1170 spin_unlock_irqrestore(&priv->session_lock, flags);
1171
1172 spin_lock_irqsave(&iscsi_transport_lock, flags);
1173 list_del(&priv->list);
1174 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1175
1176 transport_container_unregister(&priv->conn_cont);
1177 transport_container_unregister(&priv->session_cont);
1178
1179 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1180 class_device_unregister(&priv->cdev);
1181 up(&rx_queue_sema);
353 1182
354EXPORT_SYMBOL(iscsi_attach_transport); 1183 return 0;
1184}
1185EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
355 1186
356void iscsi_release_transport(struct scsi_transport_template *t) 1187static int
1188iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr)
357{ 1189{
358 struct iscsi_internal *i = to_iscsi_internal(t); 1190 struct netlink_notify *n = ptr;
1191
1192 if (event == NETLINK_URELEASE &&
1193 n->protocol == NETLINK_ISCSI && n->pid) {
1194 struct iscsi_if_conn *conn;
1195 unsigned long flags;
1196
1197 mempool_zone_complete(&z_reply);
1198 spin_lock_irqsave(&connlock, flags);
1199 list_for_each_entry(conn, &connlist, conn_list) {
1200 mempool_zone_complete(&conn->z_error);
1201 mempool_zone_complete(&conn->z_pdu);
1202 }
1203 spin_unlock_irqrestore(&connlock, flags);
1204 }
359 1205
360 transport_container_unregister(&i->t.target_attrs); 1206 return NOTIFY_DONE;
361 transport_container_unregister(&i->t.host_attrs);
362
363 kfree(i);
364} 1207}
365 1208
366EXPORT_SYMBOL(iscsi_release_transport); 1209static struct notifier_block iscsi_nl_notifier = {
1210 .notifier_call = iscsi_rcv_nl_event,
1211};
367 1212
368static __init int iscsi_transport_init(void) 1213static __init int iscsi_transport_init(void)
369{ 1214{
370 int err = transport_class_register(&iscsi_transport_class); 1215 int err;
371 1216
1217 err = class_register(&iscsi_transport_class);
372 if (err) 1218 if (err)
373 return err; 1219 return err;
374 return transport_class_register(&iscsi_host_class); 1220
1221 err = transport_class_register(&iscsi_connection_class);
1222 if (err)
1223 goto unregister_transport_class;
1224
1225 err = transport_class_register(&iscsi_session_class);
1226 if (err)
1227 goto unregister_conn_class;
1228
1229 err = netlink_register_notifier(&iscsi_nl_notifier);
1230 if (err)
1231 goto unregister_session_class;
1232
1233 nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx,
1234 THIS_MODULE);
1235 if (!nls) {
1236 err = -ENOBUFS;
1237 goto unregister_notifier;
1238 }
1239
1240 err = mempool_zone_init(&z_reply, Z_MAX_REPLY,
1241 NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY);
1242 if (!err)
1243 return 0;
1244
1245 sock_release(nls->sk_socket);
1246unregister_notifier:
1247 netlink_unregister_notifier(&iscsi_nl_notifier);
1248unregister_session_class:
1249 transport_class_unregister(&iscsi_session_class);
1250unregister_conn_class:
1251 transport_class_unregister(&iscsi_connection_class);
1252unregister_transport_class:
1253 class_unregister(&iscsi_transport_class);
1254 return err;
375} 1255}
376 1256
377static void __exit iscsi_transport_exit(void) 1257static void __exit iscsi_transport_exit(void)
378{ 1258{
379 transport_class_unregister(&iscsi_host_class); 1259 mempool_destroy(z_reply.pool);
380 transport_class_unregister(&iscsi_transport_class); 1260 sock_release(nls->sk_socket);
1261 netlink_unregister_notifier(&iscsi_nl_notifier);
1262 transport_class_unregister(&iscsi_connection_class);
1263 transport_class_unregister(&iscsi_session_class);
1264 class_unregister(&iscsi_transport_class);
381} 1265}
382 1266
383module_init(iscsi_transport_init); 1267module_init(iscsi_transport_init);
384module_exit(iscsi_transport_exit); 1268module_exit(iscsi_transport_exit);
385 1269
386MODULE_AUTHOR("Mike Christie"); 1270MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
387MODULE_DESCRIPTION("iSCSI Transport Attributes"); 1271 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1272 "Alex Aizman <itn780@yahoo.com>");
1273MODULE_DESCRIPTION("iSCSI Transport Interface");
388MODULE_LICENSE("GPL"); 1274MODULE_LICENSE("GPL");
diff --git a/drivers/scsi/sym53c8xx_defs.h b/drivers/scsi/sym53c8xx_defs.h
index 4c4ae7d4713..139cd0e12e6 100644
--- a/drivers/scsi/sym53c8xx_defs.h
+++ b/drivers/scsi/sym53c8xx_defs.h
@@ -281,19 +281,6 @@
281#endif 281#endif
282 282
283/* 283/*
284** These simple macros limit expression involving
285** kernel time values (jiffies) to some that have
286** chance not to be too much incorrect. :-)
287*/
288#define ktime_get(o) (jiffies + (u_long) o)
289#define ktime_exp(b) ((long)(jiffies) - (long)(b) >= 0)
290#define ktime_dif(a, b) ((long)(a) - (long)(b))
291/* These ones are not used in this driver */
292#define ktime_add(a, o) ((a) + (u_long)(o))
293#define ktime_sub(a, o) ((a) - (u_long)(o))
294
295
296/*
297 * IO functions definition for big/little endian CPU support. 284 * IO functions definition for big/little endian CPU support.
298 * For now, the NCR is only supported in little endian addressing mode, 285 * For now, the NCR is only supported in little endian addressing mode,
299 */ 286 */
diff --git a/drivers/scsi/tmscsim.c b/drivers/scsi/tmscsim.c
index 9589c67de53..386bd6c67e7 100644
--- a/drivers/scsi/tmscsim.c
+++ b/drivers/scsi/tmscsim.c
@@ -988,7 +988,15 @@ din_1:
988 988
989 if( residual ) 989 if( residual )
990 { 990 {
991 static int feedback_requested;
991 bval = DC390_read8 (ScsiFifo); /* get one residual byte */ 992 bval = DC390_read8 (ScsiFifo); /* get one residual byte */
993
994 if (!feedback_requested) {
995 feedback_requested = 1;
996 printk(KERN_WARNING "%s: Please, contact <linux-scsi@vger.kernel.org> "
997 "to help improve support for your system.\n", __FILE__);
998 }
999
992 ptr = (u8 *) bus_to_virt( pSRB->SGBusAddr ); 1000 ptr = (u8 *) bus_to_virt( pSRB->SGBusAddr );
993 *ptr = bval; 1001 *ptr = bval;
994 pSRB->SGBusAddr++; xferCnt++; 1002 pSRB->SGBusAddr++; xferCnt++;
diff --git a/include/scsi/iscsi_if.h b/include/scsi/iscsi_if.h
new file mode 100644
index 00000000000..be1bc792ab1
--- /dev/null
+++ b/include/scsi/iscsi_if.h
@@ -0,0 +1,245 @@
1/*
2 * iSCSI User/Kernel Shares (Defines, Constants, Protocol definitions, etc)
3 *
4 * Copyright (C) 2005 Dmitry Yusupov
5 * Copyright (C) 2005 Alex Aizman
6 * maintained by open-iscsi@googlegroups.com
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * See the file COPYING included with this distribution for more details.
19 */
20
21#ifndef ISCSI_IF_H
22#define ISCSI_IF_H
23
24#include <scsi/iscsi_proto.h>
25
26#define UEVENT_BASE 10
27#define KEVENT_BASE 100
28#define ISCSI_ERR_BASE 1000
29
30enum iscsi_uevent_e {
31 ISCSI_UEVENT_UNKNOWN = 0,
32
33 /* down events */
34 ISCSI_UEVENT_CREATE_SESSION = UEVENT_BASE + 1,
35 ISCSI_UEVENT_DESTROY_SESSION = UEVENT_BASE + 2,
36 ISCSI_UEVENT_CREATE_CONN = UEVENT_BASE + 3,
37 ISCSI_UEVENT_DESTROY_CONN = UEVENT_BASE + 4,
38 ISCSI_UEVENT_BIND_CONN = UEVENT_BASE + 5,
39 ISCSI_UEVENT_SET_PARAM = UEVENT_BASE + 6,
40 ISCSI_UEVENT_START_CONN = UEVENT_BASE + 7,
41 ISCSI_UEVENT_STOP_CONN = UEVENT_BASE + 8,
42 ISCSI_UEVENT_SEND_PDU = UEVENT_BASE + 9,
43 ISCSI_UEVENT_GET_STATS = UEVENT_BASE + 10,
44 ISCSI_UEVENT_GET_PARAM = UEVENT_BASE + 11,
45
46 /* up events */
47 ISCSI_KEVENT_RECV_PDU = KEVENT_BASE + 1,
48 ISCSI_KEVENT_CONN_ERROR = KEVENT_BASE + 2,
49 ISCSI_KEVENT_IF_ERROR = KEVENT_BASE + 3,
50};
51
52struct iscsi_uevent {
53 uint32_t type; /* k/u events type */
54 uint32_t iferror; /* carries interface or resource errors */
55 uint64_t transport_handle;
56
57 union {
58 /* messages u -> k */
59 struct msg_create_session {
60 uint32_t initial_cmdsn;
61 } c_session;
62 struct msg_destroy_session {
63 uint64_t session_handle;
64 uint32_t sid;
65 } d_session;
66 struct msg_create_conn {
67 uint64_t session_handle;
68 uint32_t cid;
69 uint32_t sid;
70 } c_conn;
71 struct msg_bind_conn {
72 uint64_t session_handle;
73 uint64_t conn_handle;
74 uint32_t transport_fd;
75 uint32_t is_leading;
76 } b_conn;
77 struct msg_destroy_conn {
78 uint64_t conn_handle;
79 uint32_t cid;
80 } d_conn;
81 struct msg_send_pdu {
82 uint32_t hdr_size;
83 uint32_t data_size;
84 uint64_t conn_handle;
85 } send_pdu;
86 struct msg_set_param {
87 uint64_t conn_handle;
88 uint32_t param; /* enum iscsi_param */
89 uint32_t value;
90 } set_param;
91 struct msg_start_conn {
92 uint64_t conn_handle;
93 } start_conn;
94 struct msg_stop_conn {
95 uint64_t conn_handle;
96 uint32_t flag;
97 } stop_conn;
98 struct msg_get_stats {
99 uint64_t conn_handle;
100 } get_stats;
101 } u;
102 union {
103 /* messages k -> u */
104 uint64_t handle;
105 int retcode;
106 struct msg_create_session_ret {
107 uint64_t session_handle;
108 uint32_t sid;
109 } c_session_ret;
110 struct msg_recv_req {
111 uint64_t recv_handle;
112 uint64_t conn_handle;
113 } recv_req;
114 struct msg_conn_error {
115 uint64_t conn_handle;
116 uint32_t error; /* enum iscsi_err */
117 } connerror;
118 } r;
119} __attribute__ ((aligned (sizeof(uint64_t))));
120
121/*
122 * Common error codes
123 */
124enum iscsi_err {
125 ISCSI_OK = 0,
126
127 ISCSI_ERR_DATASN = ISCSI_ERR_BASE + 1,
128 ISCSI_ERR_DATA_OFFSET = ISCSI_ERR_BASE + 2,
129 ISCSI_ERR_MAX_CMDSN = ISCSI_ERR_BASE + 3,
130 ISCSI_ERR_EXP_CMDSN = ISCSI_ERR_BASE + 4,
131 ISCSI_ERR_BAD_OPCODE = ISCSI_ERR_BASE + 5,
132 ISCSI_ERR_DATALEN = ISCSI_ERR_BASE + 6,
133 ISCSI_ERR_AHSLEN = ISCSI_ERR_BASE + 7,
134 ISCSI_ERR_PROTO = ISCSI_ERR_BASE + 8,
135 ISCSI_ERR_LUN = ISCSI_ERR_BASE + 9,
136 ISCSI_ERR_BAD_ITT = ISCSI_ERR_BASE + 10,
137 ISCSI_ERR_CONN_FAILED = ISCSI_ERR_BASE + 11,
138 ISCSI_ERR_R2TSN = ISCSI_ERR_BASE + 12,
139 ISCSI_ERR_SESSION_FAILED = ISCSI_ERR_BASE + 13,
140 ISCSI_ERR_HDR_DGST = ISCSI_ERR_BASE + 14,
141 ISCSI_ERR_DATA_DGST = ISCSI_ERR_BASE + 15,
142 ISCSI_ERR_PARAM_NOT_FOUND = ISCSI_ERR_BASE + 16
143};
144
145/*
146 * iSCSI Parameters (RFC3720)
147 */
148enum iscsi_param {
149 ISCSI_PARAM_MAX_RECV_DLENGTH = 0,
150 ISCSI_PARAM_MAX_XMIT_DLENGTH = 1,
151 ISCSI_PARAM_HDRDGST_EN = 2,
152 ISCSI_PARAM_DATADGST_EN = 3,
153 ISCSI_PARAM_INITIAL_R2T_EN = 4,
154 ISCSI_PARAM_MAX_R2T = 5,
155 ISCSI_PARAM_IMM_DATA_EN = 6,
156 ISCSI_PARAM_FIRST_BURST = 7,
157 ISCSI_PARAM_MAX_BURST = 8,
158 ISCSI_PARAM_PDU_INORDER_EN = 9,
159 ISCSI_PARAM_DATASEQ_INORDER_EN = 10,
160 ISCSI_PARAM_ERL = 11,
161 ISCSI_PARAM_IFMARKER_EN = 12,
162 ISCSI_PARAM_OFMARKER_EN = 13,
163};
164#define ISCSI_PARAM_MAX 14
165
166typedef uint64_t iscsi_sessionh_t; /* iSCSI Data-Path session handle */
167typedef uint64_t iscsi_connh_t; /* iSCSI Data-Path connection handle */
168
169#define iscsi_ptr(_handle) ((void*)(unsigned long)_handle)
170#define iscsi_handle(_ptr) ((uint64_t)(unsigned long)_ptr)
171#define iscsi_hostdata(_hostdata) ((void*)_hostdata + sizeof(unsigned long))
172
173/*
174 * These flags presents iSCSI Data-Path capabilities.
175 */
176#define CAP_RECOVERY_L0 0x1
177#define CAP_RECOVERY_L1 0x2
178#define CAP_RECOVERY_L2 0x4
179#define CAP_MULTI_R2T 0x8
180#define CAP_HDRDGST 0x10
181#define CAP_DATADGST 0x20
182#define CAP_MULTI_CONN 0x40
183#define CAP_TEXT_NEGO 0x80
184#define CAP_MARKERS 0x100
185
186/*
187 * These flags describes reason of stop_conn() call
188 */
189#define STOP_CONN_TERM 0x1
190#define STOP_CONN_SUSPEND 0x2
191#define STOP_CONN_RECOVER 0x3
192
193#define ISCSI_STATS_CUSTOM_MAX 32
194#define ISCSI_STATS_CUSTOM_DESC_MAX 64
195struct iscsi_stats_custom {
196 char desc[ISCSI_STATS_CUSTOM_DESC_MAX];
197 uint64_t value;
198};
199
200/*
201 * struct iscsi_stats - iSCSI Statistics (iSCSI MIB)
202 *
203 * Note: this structure contains counters collected on per-connection basis.
204 */
205struct iscsi_stats {
206 /* octets */
207 uint64_t txdata_octets;
208 uint64_t rxdata_octets;
209
210 /* xmit pdus */
211 uint32_t noptx_pdus;
212 uint32_t scsicmd_pdus;
213 uint32_t tmfcmd_pdus;
214 uint32_t login_pdus;
215 uint32_t text_pdus;
216 uint32_t dataout_pdus;
217 uint32_t logout_pdus;
218 uint32_t snack_pdus;
219
220 /* recv pdus */
221 uint32_t noprx_pdus;
222 uint32_t scsirsp_pdus;
223 uint32_t tmfrsp_pdus;
224 uint32_t textrsp_pdus;
225 uint32_t datain_pdus;
226 uint32_t logoutrsp_pdus;
227 uint32_t r2t_pdus;
228 uint32_t async_pdus;
229 uint32_t rjt_pdus;
230
231 /* errors */
232 uint32_t digest_err;
233 uint32_t timeout_err;
234
235 /*
236 * iSCSI Custom Statistics support, i.e. Transport could
237 * extend existing MIB statistics with its own specific statistics
238 * up to ISCSI_STATS_CUSTOM_MAX
239 */
240 uint32_t custom_length;
241 struct iscsi_stats_custom custom[0]
242 __attribute__ ((aligned (sizeof(uint64_t))));
243};
244
245#endif
diff --git a/include/scsi/iscsi_proto.h b/include/scsi/iscsi_proto.h
new file mode 100644
index 00000000000..4feda05fdf2
--- /dev/null
+++ b/include/scsi/iscsi_proto.h
@@ -0,0 +1,589 @@
1/*
2 * RFC 3720 (iSCSI) protocol data types
3 *
4 * Copyright (C) 2005 Dmitry Yusupov
5 * Copyright (C) 2005 Alex Aizman
6 * maintained by open-iscsi@googlegroups.com
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * See the file COPYING included with this distribution for more details.
19 */
20
21#ifndef ISCSI_PROTO_H
22#define ISCSI_PROTO_H
23
24#define ISCSI_VERSION_STR "0.3"
25#define ISCSI_DATE_STR "22-Apr-2005"
26#define ISCSI_DRAFT20_VERSION 0x00
27
28/* default iSCSI listen port for incoming connections */
29#define ISCSI_LISTEN_PORT 3260
30
31/* Padding word length */
32#define PAD_WORD_LEN 4
33
34/*
35 * useful common(control and data pathes) macro
36 */
37#define ntoh24(p) (((p)[0] << 16) | ((p)[1] << 8) | ((p)[2]))
38#define hton24(p, v) { \
39 p[0] = (((v) >> 16) & 0xFF); \
40 p[1] = (((v) >> 8) & 0xFF); \
41 p[2] = ((v) & 0xFF); \
42}
43#define zero_data(p) {p[0]=0;p[1]=0;p[2]=0;}
44
45/*
46 * iSCSI Template Message Header
47 */
48struct iscsi_hdr {
49 uint8_t opcode;
50 uint8_t flags; /* Final bit */
51 uint8_t rsvd2[2];
52 uint8_t hlength; /* AHSs total length */
53 uint8_t dlength[3]; /* Data length */
54 uint8_t lun[8];
55 __be32 itt; /* Initiator Task Tag */
56 __be32 ttt; /* Target Task Tag */
57 __be32 statsn;
58 __be32 exp_statsn;
59 __be32 max_statsn;
60 uint8_t other[12];
61};
62
63/************************* RFC 3720 Begin *****************************/
64
65#define ISCSI_RESERVED_TAG 0xffffffff
66
67/* Opcode encoding bits */
68#define ISCSI_OP_RETRY 0x80
69#define ISCSI_OP_IMMEDIATE 0x40
70#define ISCSI_OPCODE_MASK 0x3F
71
72/* Initiator Opcode values */
73#define ISCSI_OP_NOOP_OUT 0x00
74#define ISCSI_OP_SCSI_CMD 0x01
75#define ISCSI_OP_SCSI_TMFUNC 0x02
76#define ISCSI_OP_LOGIN 0x03
77#define ISCSI_OP_TEXT 0x04
78#define ISCSI_OP_SCSI_DATA_OUT 0x05
79#define ISCSI_OP_LOGOUT 0x06
80#define ISCSI_OP_SNACK 0x10
81
82#define ISCSI_OP_VENDOR1_CMD 0x1c
83#define ISCSI_OP_VENDOR2_CMD 0x1d
84#define ISCSI_OP_VENDOR3_CMD 0x1e
85#define ISCSI_OP_VENDOR4_CMD 0x1f
86
87/* Target Opcode values */
88#define ISCSI_OP_NOOP_IN 0x20
89#define ISCSI_OP_SCSI_CMD_RSP 0x21
90#define ISCSI_OP_SCSI_TMFUNC_RSP 0x22
91#define ISCSI_OP_LOGIN_RSP 0x23
92#define ISCSI_OP_TEXT_RSP 0x24
93#define ISCSI_OP_SCSI_DATA_IN 0x25
94#define ISCSI_OP_LOGOUT_RSP 0x26
95#define ISCSI_OP_R2T 0x31
96#define ISCSI_OP_ASYNC_EVENT 0x32
97#define ISCSI_OP_REJECT 0x3f
98
99struct iscsi_ahs_hdr {
100 __be16 ahslength;
101 uint8_t ahstype;
102 uint8_t ahspec[5];
103};
104
105#define ISCSI_AHSTYPE_CDB 1
106#define ISCSI_AHSTYPE_RLENGTH 2
107
108/* iSCSI PDU Header */
109struct iscsi_cmd {
110 uint8_t opcode;
111 uint8_t flags;
112 __be16 rsvd2;
113 uint8_t hlength;
114 uint8_t dlength[3];
115 uint8_t lun[8];
116 __be32 itt; /* Initiator Task Tag */
117 __be32 data_length;
118 __be32 cmdsn;
119 __be32 exp_statsn;
120 uint8_t cdb[16]; /* SCSI Command Block */
121 /* Additional Data (Command Dependent) */
122};
123
124/* Command PDU flags */
125#define ISCSI_FLAG_CMD_FINAL 0x80
126#define ISCSI_FLAG_CMD_READ 0x40
127#define ISCSI_FLAG_CMD_WRITE 0x20
128#define ISCSI_FLAG_CMD_ATTR_MASK 0x07 /* 3 bits */
129
130/* SCSI Command Attribute values */
131#define ISCSI_ATTR_UNTAGGED 0
132#define ISCSI_ATTR_SIMPLE 1
133#define ISCSI_ATTR_ORDERED 2
134#define ISCSI_ATTR_HEAD_OF_QUEUE 3
135#define ISCSI_ATTR_ACA 4
136
137struct iscsi_rlength_ahdr {
138 __be16 ahslength;
139 uint8_t ahstype;
140 uint8_t reserved;
141 __be32 read_length;
142};
143
144/* SCSI Response Header */
145struct iscsi_cmd_rsp {
146 uint8_t opcode;
147 uint8_t flags;
148 uint8_t response;
149 uint8_t cmd_status;
150 uint8_t hlength;
151 uint8_t dlength[3];
152 uint8_t rsvd[8];
153 __be32 itt; /* Initiator Task Tag */
154 __be32 rsvd1;
155 __be32 statsn;
156 __be32 exp_cmdsn;
157 __be32 max_cmdsn;
158 __be32 exp_datasn;
159 __be32 bi_residual_count;
160 __be32 residual_count;
161 /* Response or Sense Data (optional) */
162};
163
164/* Command Response PDU flags */
165#define ISCSI_FLAG_CMD_BIDI_OVERFLOW 0x10
166#define ISCSI_FLAG_CMD_BIDI_UNDERFLOW 0x08
167#define ISCSI_FLAG_CMD_OVERFLOW 0x04
168#define ISCSI_FLAG_CMD_UNDERFLOW 0x02
169
170/* iSCSI Status values. Valid if Rsp Selector bit is not set */
171#define ISCSI_STATUS_CMD_COMPLETED 0
172#define ISCSI_STATUS_TARGET_FAILURE 1
173#define ISCSI_STATUS_SUBSYS_FAILURE 2
174
175/* Asynchronous Event Header */
176struct iscsi_async {
177 uint8_t opcode;
178 uint8_t flags;
179 uint8_t rsvd2[2];
180 uint8_t rsvd3;
181 uint8_t dlength[3];
182 uint8_t lun[8];
183 uint8_t rsvd4[8];
184 __be32 statsn;
185 __be32 exp_cmdsn;
186 __be32 max_cmdsn;
187 uint8_t async_event;
188 uint8_t async_vcode;
189 __be16 param1;
190 __be16 param2;
191 __be16 param3;
192 uint8_t rsvd5[4];
193};
194
195/* iSCSI Event Codes */
196#define ISCSI_ASYNC_MSG_SCSI_EVENT 0
197#define ISCSI_ASYNC_MSG_REQUEST_LOGOUT 1
198#define ISCSI_ASYNC_MSG_DROPPING_CONNECTION 2
199#define ISCSI_ASYNC_MSG_DROPPING_ALL_CONNECTIONS 3
200#define ISCSI_ASYNC_MSG_PARAM_NEGOTIATION 4
201#define ISCSI_ASYNC_MSG_VENDOR_SPECIFIC 255
202
203/* NOP-Out Message */
204struct iscsi_nopout {
205 uint8_t opcode;
206 uint8_t flags;
207 __be16 rsvd2;
208 uint8_t rsvd3;
209 uint8_t dlength[3];
210 uint8_t lun[8];
211 __be32 itt; /* Initiator Task Tag */
212 __be32 ttt; /* Target Transfer Tag */
213 __be32 cmdsn;
214 __be32 exp_statsn;
215 uint8_t rsvd4[16];
216};
217
218/* NOP-In Message */
219struct iscsi_nopin {
220 uint8_t opcode;
221 uint8_t flags;
222 __be16 rsvd2;
223 uint8_t rsvd3;
224 uint8_t dlength[3];
225 uint8_t lun[8];
226 __be32 itt; /* Initiator Task Tag */
227 __be32 ttt; /* Target Transfer Tag */
228 __be32 statsn;
229 __be32 exp_cmdsn;
230 __be32 max_cmdsn;
231 uint8_t rsvd4[12];
232};
233
234/* SCSI Task Management Message Header */
235struct iscsi_tm {
236 uint8_t opcode;
237 uint8_t flags;
238 uint8_t rsvd1[2];
239 uint8_t hlength;
240 uint8_t dlength[3];
241 uint8_t lun[8];
242 __be32 itt; /* Initiator Task Tag */
243 __be32 rtt; /* Reference Task Tag */
244 __be32 cmdsn;
245 __be32 exp_statsn;
246 __be32 refcmdsn;
247 __be32 exp_datasn;
248 uint8_t rsvd2[8];
249};
250
251#define ISCSI_FLAG_TM_FUNC_MASK 0x7F
252
253/* Function values */
254#define ISCSI_TM_FUNC_ABORT_TASK 1
255#define ISCSI_TM_FUNC_ABORT_TASK_SET 2
256#define ISCSI_TM_FUNC_CLEAR_ACA 3
257#define ISCSI_TM_FUNC_CLEAR_TASK_SET 4
258#define ISCSI_TM_FUNC_LOGICAL_UNIT_RESET 5
259#define ISCSI_TM_FUNC_TARGET_WARM_RESET 6
260#define ISCSI_TM_FUNC_TARGET_COLD_RESET 7
261#define ISCSI_TM_FUNC_TASK_REASSIGN 8
262
263/* SCSI Task Management Response Header */
264struct iscsi_tm_rsp {
265 uint8_t opcode;
266 uint8_t flags;
267 uint8_t response; /* see Response values below */
268 uint8_t qualifier;
269 uint8_t hlength;
270 uint8_t dlength[3];
271 uint8_t rsvd2[8];
272 __be32 itt; /* Initiator Task Tag */
273 __be32 rtt; /* Reference Task Tag */
274 __be32 statsn;
275 __be32 exp_cmdsn;
276 __be32 max_cmdsn;
277 uint8_t rsvd3[12];
278};
279
280/* Response values */
281#define ISCSI_TMF_RSP_COMPLETE 0x00
282#define ISCSI_TMF_RSP_NO_TASK 0x01
283#define ISCSI_TMF_RSP_NO_LUN 0x02
284#define ISCSI_TMF_RSP_TASK_ALLEGIANT 0x03
285#define ISCSI_TMF_RSP_NO_FAILOVER 0x04
286#define ISCSI_TMF_RSP_NOT_SUPPORTED 0x05
287#define ISCSI_TMF_RSP_AUTH_FAILED 0x06
288#define ISCSI_TMF_RSP_REJECTED 0xff
289
290/* Ready To Transfer Header */
291struct iscsi_r2t_rsp {
292 uint8_t opcode;
293 uint8_t flags;
294 uint8_t rsvd2[2];
295 uint8_t hlength;
296 uint8_t dlength[3];
297 uint8_t lun[8];
298 __be32 itt; /* Initiator Task Tag */
299 __be32 ttt; /* Target Transfer Tag */
300 __be32 statsn;
301 __be32 exp_cmdsn;
302 __be32 max_cmdsn;
303 __be32 r2tsn;
304 __be32 data_offset;
305 __be32 data_length;
306};
307
308/* SCSI Data Hdr */
309struct iscsi_data {
310 uint8_t opcode;
311 uint8_t flags;
312 uint8_t rsvd2[2];
313 uint8_t rsvd3;
314 uint8_t dlength[3];
315 uint8_t lun[8];
316 __be32 itt;
317 __be32 ttt;
318 __be32 rsvd4;
319 __be32 exp_statsn;
320 __be32 rsvd5;
321 __be32 datasn;
322 __be32 offset;
323 __be32 rsvd6;
324 /* Payload */
325};
326
327/* SCSI Data Response Hdr */
328struct iscsi_data_rsp {
329 uint8_t opcode;
330 uint8_t flags;
331 uint8_t rsvd2;
332 uint8_t cmd_status;
333 uint8_t hlength;
334 uint8_t dlength[3];
335 uint8_t lun[8];
336 __be32 itt;
337 __be32 ttt;
338 __be32 statsn;
339 __be32 exp_cmdsn;
340 __be32 max_cmdsn;
341 __be32 datasn;
342 __be32 offset;
343 __be32 residual_count;
344};
345
346/* Data Response PDU flags */
347#define ISCSI_FLAG_DATA_ACK 0x40
348#define ISCSI_FLAG_DATA_OVERFLOW 0x04
349#define ISCSI_FLAG_DATA_UNDERFLOW 0x02
350#define ISCSI_FLAG_DATA_STATUS 0x01
351
352/* Text Header */
353struct iscsi_text {
354 uint8_t opcode;
355 uint8_t flags;
356 uint8_t rsvd2[2];
357 uint8_t hlength;
358 uint8_t dlength[3];
359 uint8_t rsvd4[8];
360 __be32 itt;
361 __be32 ttt;
362 __be32 cmdsn;
363 __be32 exp_statsn;
364 uint8_t rsvd5[16];
365 /* Text - key=value pairs */
366};
367
368#define ISCSI_FLAG_TEXT_CONTINUE 0x40
369
370/* Text Response Header */
371struct iscsi_text_rsp {
372 uint8_t opcode;
373 uint8_t flags;
374 uint8_t rsvd2[2];
375 uint8_t hlength;
376 uint8_t dlength[3];
377 uint8_t rsvd4[8];
378 __be32 itt;
379 __be32 ttt;
380 __be32 statsn;
381 __be32 exp_cmdsn;
382 __be32 max_cmdsn;
383 uint8_t rsvd5[12];
384 /* Text Response - key:value pairs */
385};
386
387/* Login Header */
388struct iscsi_login {
389 uint8_t opcode;
390 uint8_t flags;
391 uint8_t max_version; /* Max. version supported */
392 uint8_t min_version; /* Min. version supported */
393 uint8_t hlength;
394 uint8_t dlength[3];
395 uint8_t isid[6]; /* Initiator Session ID */
396 __be16 tsih; /* Target Session Handle */
397 __be32 itt; /* Initiator Task Tag */
398 __be16 cid;
399 __be16 rsvd3;
400 __be32 cmdsn;
401 __be32 exp_statsn;
402 uint8_t rsvd5[16];
403};
404
405/* Login PDU flags */
406#define ISCSI_FLAG_LOGIN_TRANSIT 0x80
407#define ISCSI_FLAG_LOGIN_CONTINUE 0x40
408#define ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK 0x0C /* 2 bits */
409#define ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK 0x03 /* 2 bits */
410
411#define ISCSI_LOGIN_CURRENT_STAGE(flags) \
412 ((flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) >> 2)
413#define ISCSI_LOGIN_NEXT_STAGE(flags) \
414 (flags & ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK)
415
416/* Login Response Header */
417struct iscsi_login_rsp {
418 uint8_t opcode;
419 uint8_t flags;
420 uint8_t max_version; /* Max. version supported */
421 uint8_t active_version; /* Active version */
422 uint8_t hlength;
423 uint8_t dlength[3];
424 uint8_t isid[6]; /* Initiator Session ID */
425 __be16 tsih; /* Target Session Handle */
426 __be32 itt; /* Initiator Task Tag */
427 __be32 rsvd3;
428 __be32 statsn;
429 __be32 exp_cmdsn;
430 __be32 max_cmdsn;
431 uint8_t status_class; /* see Login RSP ststus classes below */
432 uint8_t status_detail; /* see Login RSP Status details below */
433 uint8_t rsvd4[10];
434};
435
436/* Login stage (phase) codes for CSG, NSG */
437#define ISCSI_INITIAL_LOGIN_STAGE -1
438#define ISCSI_SECURITY_NEGOTIATION_STAGE 0
439#define ISCSI_OP_PARMS_NEGOTIATION_STAGE 1
440#define ISCSI_FULL_FEATURE_PHASE 3
441
442/* Login Status response classes */
443#define ISCSI_STATUS_CLS_SUCCESS 0x00
444#define ISCSI_STATUS_CLS_REDIRECT 0x01
445#define ISCSI_STATUS_CLS_INITIATOR_ERR 0x02
446#define ISCSI_STATUS_CLS_TARGET_ERR 0x03
447
448/* Login Status response detail codes */
449/* Class-0 (Success) */
450#define ISCSI_LOGIN_STATUS_ACCEPT 0x00
451
452/* Class-1 (Redirection) */
453#define ISCSI_LOGIN_STATUS_TGT_MOVED_TEMP 0x01
454#define ISCSI_LOGIN_STATUS_TGT_MOVED_PERM 0x02
455
456/* Class-2 (Initiator Error) */
457#define ISCSI_LOGIN_STATUS_INIT_ERR 0x00
458#define ISCSI_LOGIN_STATUS_AUTH_FAILED 0x01
459#define ISCSI_LOGIN_STATUS_TGT_FORBIDDEN 0x02
460#define ISCSI_LOGIN_STATUS_TGT_NOT_FOUND 0x03
461#define ISCSI_LOGIN_STATUS_TGT_REMOVED 0x04
462#define ISCSI_LOGIN_STATUS_NO_VERSION 0x05
463#define ISCSI_LOGIN_STATUS_ISID_ERROR 0x06
464#define ISCSI_LOGIN_STATUS_MISSING_FIELDS 0x07
465#define ISCSI_LOGIN_STATUS_CONN_ADD_FAILED 0x08
466#define ISCSI_LOGIN_STATUS_NO_SESSION_TYPE 0x09
467#define ISCSI_LOGIN_STATUS_NO_SESSION 0x0a
468#define ISCSI_LOGIN_STATUS_INVALID_REQUEST 0x0b
469
470/* Class-3 (Target Error) */
471#define ISCSI_LOGIN_STATUS_TARGET_ERROR 0x00
472#define ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE 0x01
473#define ISCSI_LOGIN_STATUS_NO_RESOURCES 0x02
474
475/* Logout Header */
476struct iscsi_logout {
477 uint8_t opcode;
478 uint8_t flags;
479 uint8_t rsvd1[2];
480 uint8_t hlength;
481 uint8_t dlength[3];
482 uint8_t rsvd2[8];
483 __be32 itt; /* Initiator Task Tag */
484 __be16 cid;
485 uint8_t rsvd3[2];
486 __be32 cmdsn;
487 __be32 exp_statsn;
488 uint8_t rsvd4[16];
489};
490
491/* Logout PDU flags */
492#define ISCSI_FLAG_LOGOUT_REASON_MASK 0x7F
493
494/* logout reason_code values */
495
496#define ISCSI_LOGOUT_REASON_CLOSE_SESSION 0
497#define ISCSI_LOGOUT_REASON_CLOSE_CONNECTION 1
498#define ISCSI_LOGOUT_REASON_RECOVERY 2
499#define ISCSI_LOGOUT_REASON_AEN_REQUEST 3
500
501/* Logout Response Header */
502struct iscsi_logout_rsp {
503 uint8_t opcode;
504 uint8_t flags;
505 uint8_t response; /* see Logout response values below */
506 uint8_t rsvd2;
507 uint8_t hlength;
508 uint8_t dlength[3];
509 uint8_t rsvd3[8];
510 __be32 itt; /* Initiator Task Tag */
511 __be32 rsvd4;
512 __be32 statsn;
513 __be32 exp_cmdsn;
514 __be32 max_cmdsn;
515 __be32 rsvd5;
516 __be16 t2wait;
517 __be16 t2retain;
518 __be32 rsvd6;
519};
520
521/* logout response status values */
522
523#define ISCSI_LOGOUT_SUCCESS 0
524#define ISCSI_LOGOUT_CID_NOT_FOUND 1
525#define ISCSI_LOGOUT_RECOVERY_UNSUPPORTED 2
526#define ISCSI_LOGOUT_CLEANUP_FAILED 3
527
528/* SNACK Header */
529struct iscsi_snack {
530 uint8_t opcode;
531 uint8_t flags;
532 uint8_t rsvd2[14];
533 __be32 itt;
534 __be32 begrun;
535 __be32 runlength;
536 __be32 exp_statsn;
537 __be32 rsvd3;
538 __be32 exp_datasn;
539 uint8_t rsvd6[8];
540};
541
542/* SNACK PDU flags */
543#define ISCSI_FLAG_SNACK_TYPE_MASK 0x0F /* 4 bits */
544
545/* Reject Message Header */
546struct iscsi_reject {
547 uint8_t opcode;
548 uint8_t flags;
549 uint8_t reason;
550 uint8_t rsvd2;
551 uint8_t hlength;
552 uint8_t dlength[3];
553 uint8_t rsvd3[8];
554 __be32 ffffffff;
555 uint8_t rsvd4[4];
556 __be32 statsn;
557 __be32 exp_cmdsn;
558 __be32 max_cmdsn;
559 __be32 datasn;
560 uint8_t rsvd5[8];
561 /* Text - Rejected hdr */
562};
563
564/* Reason for Reject */
565#define ISCSI_REASON_CMD_BEFORE_LOGIN 1
566#define ISCSI_REASON_DATA_DIGEST_ERROR 2
567#define ISCSI_REASON_DATA_SNACK_REJECT 3
568#define ISCSI_REASON_PROTOCOL_ERROR 4
569#define ISCSI_REASON_CMD_NOT_SUPPORTED 5
570#define ISCSI_REASON_IMM_CMD_REJECT 6
571#define ISCSI_REASON_TASK_IN_PROGRESS 7
572#define ISCSI_REASON_INVALID_SNACK 8
573#define ISCSI_REASON_BOOKMARK_INVALID 9
574#define ISCSI_REASON_BOOKMARK_NO_RESOURCES 10
575#define ISCSI_REASON_NEGOTIATION_RESET 11
576
577/* Max. number of Key=Value pairs in a text message */
578#define MAX_KEY_VALUE_PAIRS 8192
579
580/* maximum length for text keys/values */
581#define KEY_MAXLEN 64
582#define VALUE_MAXLEN 255
583#define TARGET_NAME_MAXLEN VALUE_MAXLEN
584
585#define DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH 8192
586
587/************************* RFC 3720 End *****************************/
588
589#endif /* ISCSI_PROTO_H */
diff --git a/include/scsi/scsi_transport_iscsi.h b/include/scsi/scsi_transport_iscsi.h
index 1b26a6c0aa2..f25041c386e 100644
--- a/include/scsi/scsi_transport_iscsi.h
+++ b/include/scsi/scsi_transport_iscsi.h
@@ -1,8 +1,10 @@
1/* 1/*
2 * iSCSI transport class definitions 2 * iSCSI transport class definitions
3 * 3 *
4 * Copyright (C) IBM Corporation, 2004 4 * Copyright (C) IBM Corporation, 2004
5 * Copyright (C) Mike Christie, 2004 5 * Copyright (C) Mike Christie, 2004 - 2005
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005
6 * 8 *
7 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
@@ -21,158 +23,64 @@
21#ifndef SCSI_TRANSPORT_ISCSI_H 23#ifndef SCSI_TRANSPORT_ISCSI_H
22#define SCSI_TRANSPORT_ISCSI_H 24#define SCSI_TRANSPORT_ISCSI_H
23 25
24#include <linux/config.h> 26#include <scsi/iscsi_if.h>
25#include <linux/in6.h>
26#include <linux/in.h>
27
28struct scsi_transport_template;
29 27
30struct iscsi_class_session { 28/**
31 uint8_t isid[6]; 29 * struct iscsi_transport - iSCSI Transport template
32 uint16_t tsih; 30 *
33 int header_digest; /* 1 CRC32, 0 None */ 31 * @name: transport name
34 int data_digest; /* 1 CRC32, 0 None */ 32 * @caps: iSCSI Data-Path capabilities
35 uint16_t tpgt; 33 * @create_session: create new iSCSI session object
36 union { 34 * @destroy_session: destroy existing iSCSI session object
37 struct in6_addr sin6_addr; 35 * @create_conn: create new iSCSI connection
38 struct in_addr sin_addr; 36 * @bind_conn: associate this connection with existing iSCSI session
39 } u; 37 * and specified transport descriptor
40 sa_family_t addr_type; /* must be AF_INET or AF_INET6 */ 38 * @destroy_conn: destroy inactive iSCSI connection
41 uint16_t port; /* must be in network byte order */ 39 * @set_param: set iSCSI Data-Path operational parameter
42 int initial_r2t; /* 1 Yes, 0 No */ 40 * @start_conn: set connection to be operational
43 int immediate_data; /* 1 Yes, 0 No */ 41 * @stop_conn: suspend/recover/terminate connection
44 uint32_t max_recv_data_segment_len; 42 * @send_pdu: send iSCSI PDU, Login, Logout, NOP-Out, Reject, Text.
45 uint32_t max_burst_len; 43 *
46 uint32_t first_burst_len; 44 * Template API provided by iSCSI Transport
47 uint16_t def_time2wait; 45 */
48 uint16_t def_time2retain; 46struct iscsi_transport {
49 uint16_t max_outstanding_r2t; 47 struct module *owner;
50 int data_pdu_in_order; /* 1 Yes, 0 No */ 48 char *name;
51 int data_sequence_in_order; /* 1 Yes, 0 No */ 49 unsigned int caps;
52 int erl; 50 struct scsi_host_template *host_template;
51 int hostdata_size;
52 int max_lun;
53 unsigned int max_conn;
54 unsigned int max_cmd_len;
55 iscsi_sessionh_t (*create_session) (uint32_t initial_cmdsn,
56 struct Scsi_Host *shost);
57 void (*destroy_session) (iscsi_sessionh_t session);
58 iscsi_connh_t (*create_conn) (iscsi_sessionh_t session, uint32_t cid);
59 int (*bind_conn) (iscsi_sessionh_t session, iscsi_connh_t conn,
60 uint32_t transport_fd, int is_leading);
61 int (*start_conn) (iscsi_connh_t conn);
62 void (*stop_conn) (iscsi_connh_t conn, int flag);
63 void (*destroy_conn) (iscsi_connh_t conn);
64 int (*set_param) (iscsi_connh_t conn, enum iscsi_param param,
65 uint32_t value);
66 int (*get_param) (iscsi_connh_t conn, enum iscsi_param param,
67 uint32_t *value);
68 int (*send_pdu) (iscsi_connh_t conn, struct iscsi_hdr *hdr,
69 char *data, uint32_t data_size);
70 void (*get_stats) (iscsi_connh_t conn, struct iscsi_stats *stats);
53}; 71};
54 72
55/* 73/*
56 * accessor macros 74 * transport registration upcalls
57 */ 75 */
58#define iscsi_isid(x) \ 76extern int iscsi_register_transport(struct iscsi_transport *tt);
59 (((struct iscsi_class_session *)&(x)->starget_data)->isid) 77extern int iscsi_unregister_transport(struct iscsi_transport *tt);
60#define iscsi_tsih(x) \
61 (((struct iscsi_class_session *)&(x)->starget_data)->tsih)
62#define iscsi_header_digest(x) \
63 (((struct iscsi_class_session *)&(x)->starget_data)->header_digest)
64#define iscsi_data_digest(x) \
65 (((struct iscsi_class_session *)&(x)->starget_data)->data_digest)
66#define iscsi_port(x) \
67 (((struct iscsi_class_session *)&(x)->starget_data)->port)
68#define iscsi_addr_type(x) \
69 (((struct iscsi_class_session *)&(x)->starget_data)->addr_type)
70#define iscsi_sin_addr(x) \
71 (((struct iscsi_class_session *)&(x)->starget_data)->u.sin_addr)
72#define iscsi_sin6_addr(x) \
73 (((struct iscsi_class_session *)&(x)->starget_data)->u.sin6_addr)
74#define iscsi_tpgt(x) \
75 (((struct iscsi_class_session *)&(x)->starget_data)->tpgt)
76#define iscsi_initial_r2t(x) \
77 (((struct iscsi_class_session *)&(x)->starget_data)->initial_r2t)
78#define iscsi_immediate_data(x) \
79 (((struct iscsi_class_session *)&(x)->starget_data)->immediate_data)
80#define iscsi_max_recv_data_segment_len(x) \
81 (((struct iscsi_class_session *)&(x)->starget_data)->max_recv_data_segment_len)
82#define iscsi_max_burst_len(x) \
83 (((struct iscsi_class_session *)&(x)->starget_data)->max_burst_len)
84#define iscsi_first_burst_len(x) \
85 (((struct iscsi_class_session *)&(x)->starget_data)->first_burst_len)
86#define iscsi_def_time2wait(x) \
87 (((struct iscsi_class_session *)&(x)->starget_data)->def_time2wait)
88#define iscsi_def_time2retain(x) \
89 (((struct iscsi_class_session *)&(x)->starget_data)->def_time2retain)
90#define iscsi_max_outstanding_r2t(x) \
91 (((struct iscsi_class_session *)&(x)->starget_data)->max_outstanding_r2t)
92#define iscsi_data_pdu_in_order(x) \
93 (((struct iscsi_class_session *)&(x)->starget_data)->data_pdu_in_order)
94#define iscsi_data_sequence_in_order(x) \
95 (((struct iscsi_class_session *)&(x)->starget_data)->data_sequence_in_order)
96#define iscsi_erl(x) \
97 (((struct iscsi_class_session *)&(x)->starget_data)->erl)
98 78
99/* 79/*
100 * The functions by which the transport class and the driver communicate 80 * control plane upcalls
101 */ 81 */
102struct iscsi_function_template { 82extern void iscsi_conn_error(iscsi_connh_t conn, enum iscsi_err error);
103 /* 83extern int iscsi_recv_pdu(iscsi_connh_t conn, struct iscsi_hdr *hdr,
104 * target attrs 84 char *data, uint32_t data_size);
105 */
106 void (*get_isid)(struct scsi_target *);
107 void (*get_tsih)(struct scsi_target *);
108 void (*get_header_digest)(struct scsi_target *);
109 void (*get_data_digest)(struct scsi_target *);
110 void (*get_port)(struct scsi_target *);
111 void (*get_tpgt)(struct scsi_target *);
112 /*
113 * In get_ip_address the lld must set the address and
114 * the address type
115 */
116 void (*get_ip_address)(struct scsi_target *);
117 /*
118 * The lld should snprintf the name or alias to the buffer
119 */
120 ssize_t (*get_target_name)(struct scsi_target *, char *, ssize_t);
121 ssize_t (*get_target_alias)(struct scsi_target *, char *, ssize_t);
122 void (*get_initial_r2t)(struct scsi_target *);
123 void (*get_immediate_data)(struct scsi_target *);
124 void (*get_max_recv_data_segment_len)(struct scsi_target *);
125 void (*get_max_burst_len)(struct scsi_target *);
126 void (*get_first_burst_len)(struct scsi_target *);
127 void (*get_def_time2wait)(struct scsi_target *);
128 void (*get_def_time2retain)(struct scsi_target *);
129 void (*get_max_outstanding_r2t)(struct scsi_target *);
130 void (*get_data_pdu_in_order)(struct scsi_target *);
131 void (*get_data_sequence_in_order)(struct scsi_target *);
132 void (*get_erl)(struct scsi_target *);
133
134 /*
135 * host atts
136 */
137
138 /*
139 * The lld should snprintf the name or alias to the buffer
140 */
141 ssize_t (*get_initiator_alias)(struct Scsi_Host *, char *, ssize_t);
142 ssize_t (*get_initiator_name)(struct Scsi_Host *, char *, ssize_t);
143 /*
144 * The driver sets these to tell the transport class it
145 * wants the attributes displayed in sysfs. If the show_ flag
146 * is not set, the attribute will be private to the transport
147 * class. We could probably just test if a get_ fn was set
148 * since we only use the values for sysfs but this is how
149 * fc does it too.
150 */
151 unsigned long show_isid:1;
152 unsigned long show_tsih:1;
153 unsigned long show_header_digest:1;
154 unsigned long show_data_digest:1;
155 unsigned long show_port:1;
156 unsigned long show_tpgt:1;
157 unsigned long show_ip_address:1;
158 unsigned long show_target_name:1;
159 unsigned long show_target_alias:1;
160 unsigned long show_initial_r2t:1;
161 unsigned long show_immediate_data:1;
162 unsigned long show_max_recv_data_segment_len:1;
163 unsigned long show_max_burst_len:1;
164 unsigned long show_first_burst_len:1;
165 unsigned long show_def_time2wait:1;
166 unsigned long show_def_time2retain:1;
167 unsigned long show_max_outstanding_r2t:1;
168 unsigned long show_data_pdu_in_order:1;
169 unsigned long show_data_sequence_in_order:1;
170 unsigned long show_erl:1;
171 unsigned long show_initiator_name:1;
172 unsigned long show_initiator_alias:1;
173};
174
175struct scsi_transport_template *iscsi_attach_transport(struct iscsi_function_template *);
176void iscsi_release_transport(struct scsi_transport_template *);
177 85
178#endif 86#endif