Home | History | Annotate | Line # | Download | only in iscsi
iscsi_rcv.c revision 1.3.2.1
      1  1.3.2.1      tls /*	$NetBSD: iscsi_rcv.c,v 1.3.2.1 2013/02/25 00:29:16 tls Exp $	*/
      2      1.1      agc 
      3      1.1      agc /*-
      4      1.1      agc  * Copyright (c) 2004,2005,2006,2011 The NetBSD Foundation, Inc.
      5      1.1      agc  * All rights reserved.
      6      1.1      agc  *
      7      1.1      agc  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1      agc  * by Wasabi Systems, Inc.
      9      1.1      agc  *
     10      1.1      agc  * Redistribution and use in source and binary forms, with or without
     11      1.1      agc  * modification, are permitted provided that the following conditions
     12      1.1      agc  * are met:
     13      1.1      agc  * 1. Redistributions of source code must retain the above copyright
     14      1.1      agc  *    notice, this list of conditions and the following disclaimer.
     15      1.1      agc  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1      agc  *    notice, this list of conditions and the following disclaimer in the
     17      1.1      agc  *    documentation and/or other materials provided with the distribution.
     18      1.1      agc  *
     19      1.1      agc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1      agc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1      agc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1      agc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1      agc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1      agc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1      agc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1      agc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1      agc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1      agc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1      agc  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1      agc  */
     31      1.1      agc #include "iscsi_globals.h"
     32      1.1      agc 
     33      1.1      agc #include <sys/file.h>
     34      1.1      agc #include <sys/socket.h>
     35      1.1      agc #include <sys/socketvar.h>
     36      1.1      agc 
     37      1.1      agc /*****************************************************************************/
     38      1.1      agc 
     39      1.1      agc /*
     40      1.1      agc  * my_soo_read:
     41      1.1      agc  *    Replacement for soo_read with flag handling.
     42      1.1      agc  *
     43      1.1      agc  *    Parameter:
     44      1.1      agc  *          conn     The connection
     45      1.1      agc  *          u        The uio descriptor
     46      1.1      agc  *          flags    Read flags
     47      1.1      agc  *
     48      1.1      agc  *    Returns:    0 on success, else 1
     49      1.1      agc  */
     50      1.1      agc 
     51      1.1      agc STATIC int
     52      1.1      agc my_soo_read(connection_t *conn, struct uio *u, int flags)
     53      1.1      agc {
     54      1.1      agc 	struct socket *so = (struct socket *) conn->sock->f_data;
     55      1.1      agc 	int ret;
     56      1.1      agc #ifdef ISCSI_DEBUG
     57      1.1      agc 	size_t resid = u->uio_resid;
     58      1.1      agc #endif
     59      1.1      agc 
     60      1.1      agc 	DEBC(conn, 99, ("soo_read req: %zu\n", resid));
     61      1.1      agc 
     62      1.1      agc 	ret = soreceive(so, NULL, u, NULL, NULL, &flags);
     63      1.1      agc 
     64      1.1      agc 	if (ret || (flags != MSG_DONTWAIT && u->uio_resid)) {
     65      1.1      agc 		DEBC(conn, 1, ("Read failed (ret: %d, req: %zu, out: %zu)\n", ret, resid,
     66      1.1      agc 				u->uio_resid));
     67      1.1      agc 		handle_connection_error(conn, ISCSI_STATUS_SOCKET_ERROR,
     68      1.1      agc 								RECOVER_CONNECTION);
     69      1.1      agc 		return 1;
     70      1.1      agc 	}
     71      1.1      agc 	return 0;
     72      1.1      agc }
     73      1.1      agc 
     74      1.1      agc 
     75      1.1      agc /*
     76      1.1      agc  * try_resynch_receive:
     77      1.1      agc  *    Skip over everything in the socket's receive buffer, in the hope of
     78      1.1      agc  *    ending up at the start of a new PDU.
     79      1.1      agc  *
     80      1.1      agc  *    Parameter:
     81      1.1      agc  *          conn     The connection
     82      1.1      agc  */
     83      1.1      agc 
     84      1.1      agc STATIC void
     85      1.1      agc try_resynch_receive(connection_t *conn)
     86      1.1      agc {
     87      1.1      agc 	uint8_t buffer[64];
     88      1.1      agc 	struct uio uio;
     89      1.1      agc 	struct iovec io_vec;
     90      1.1      agc 	int rc;
     91      1.1      agc 
     92      1.1      agc 	uio.uio_rw = UIO_READ;
     93      1.1      agc #if (__NetBSD_Version__ >= 399000900)
     94      1.1      agc 	UIO_SETUP_SYSSPACE(&uio);
     95      1.1      agc #else
     96      1.1      agc 	uio.uio_segflg = UIO_SYSSPACE;
     97      1.1      agc #endif
     98      1.1      agc 
     99      1.1      agc 	do {
    100      1.1      agc 		io_vec.iov_base = buffer;
    101      1.1      agc 		uio.uio_iov = &io_vec;
    102      1.1      agc 		uio.uio_iovcnt = 1;
    103      1.1      agc 		uio.uio_resid = io_vec.iov_len = sizeof(buffer);
    104      1.1      agc 
    105      1.1      agc 		rc = my_soo_read(conn, &uio, MSG_DONTWAIT);
    106      1.1      agc 		DEBC(conn, 9, ("try_resynch_receive: rc = %d, resid = %zu\n",
    107      1.1      agc 				rc, uio.uio_resid));
    108      1.1      agc 	} while (!rc && !uio.uio_resid);
    109      1.1      agc }
    110      1.1      agc 
    111      1.1      agc 
    112      1.1      agc /*
    113      1.1      agc  * ccb_from_itt
    114      1.1      agc  *    Translate ITT into CCB pointer.
    115      1.1      agc  *
    116      1.1      agc  *    Parameter:
    117      1.1      agc  *          conn     The connection
    118      1.1      agc  *          itt      The Initiator Task Tag
    119      1.1      agc  *
    120      1.1      agc  *    Returns:
    121      1.1      agc  *          Pointer to CCB, or NULL if ITT is not a valid CCB index.
    122      1.1      agc  */
    123      1.1      agc 
    124      1.1      agc STATIC ccb_t *
    125      1.1      agc ccb_from_itt(connection_t *conn, uint32_t itt)
    126      1.1      agc {
    127      1.1      agc 	ccb_t *ccb;
    128      1.1      agc 	int cidx;
    129      1.1      agc 
    130      1.1      agc 	cidx = itt & 0xff;
    131      1.1      agc 	if (cidx >= CCBS_PER_SESSION) {
    132      1.1      agc 		return NULL;
    133      1.1      agc 	}
    134      1.1      agc 	ccb = &conn->session->ccb[cidx];
    135      1.1      agc 	if (ccb->ITT != itt || ccb->disp <= CCBDISP_BUSY) {
    136      1.1      agc 		return NULL;
    137      1.1      agc 	}
    138      1.1      agc 	return ccb;
    139      1.1      agc }
    140      1.1      agc 
    141      1.1      agc 
    142      1.1      agc /*
    143      1.1      agc  * read_pdu_data:
    144      1.1      agc  *    Initialize the uio structure for receiving everything after the
    145      1.1      agc  *    header, including data (if present), and padding. Read the data.
    146      1.1      agc  *
    147      1.1      agc  *    Parameter:
    148      1.1      agc  *          pdu      The PDU
    149      1.1      agc  *          data     Pointer to data (may be NULL for auto-allocation)
    150      1.1      agc  *          offset   The offset into the data pointer
    151      1.1      agc  *
    152      1.1      agc  *    Returns:     0 on success
    153      1.1      agc  *                 1 if an error occurs during read
    154      1.1      agc  *                -1 if the data digest was incorrect (PDU must be ignored)
    155      1.1      agc  */
    156      1.1      agc 
    157      1.1      agc STATIC int
    158      1.1      agc read_pdu_data(pdu_t *pdu, uint8_t *data, uint32_t offset)
    159      1.1      agc {
    160      1.1      agc 	static uint8_t pad_bytes[4];
    161      1.1      agc 	uint32_t len, digest;
    162      1.1      agc 	struct uio *uio;
    163      1.1      agc 	int i, pad;
    164      1.1      agc 	connection_t *conn = pdu->connection;
    165      1.1      agc 
    166      1.3  mlelstv 	DEBOUT(("read_pdu_data: data segment length = %d\n",
    167      1.3  mlelstv 		ntoh3(pdu->pdu.DataSegmentLength)));
    168      1.1      agc 	if (!(len = ntoh3(pdu->pdu.DataSegmentLength))) {
    169      1.1      agc 		return 0;
    170      1.1      agc 	}
    171      1.1      agc 	pad = len & 0x03;
    172      1.1      agc 	if (pad) {
    173      1.1      agc 		pad = 4 - pad;
    174      1.1      agc 	}
    175      1.1      agc 	assert((data != NULL) || (offset == 0));
    176      1.1      agc 
    177      1.1      agc 	if (data == NULL) {
    178      1.1      agc 		/*
    179      1.1      agc 		 * NOTE: Always allocate 2 extra bytes when reading temp data,
    180      1.1      agc 		 * since temp data is mostly used for received text, and we can
    181      1.1      agc 		 * make sure there's a double zero at the end of the data to mark EOF.
    182      1.1      agc 		 */
    183      1.1      agc 		if ((data = (uint8_t *) malloc(len + 2, M_TEMP, M_WAITOK)) == NULL) {
    184      1.1      agc 			DEBOUT(("ran out of mem on receive\n"));
    185      1.1      agc 			handle_connection_error(pdu->connection,
    186      1.1      agc 				ISCSI_STATUS_NO_RESOURCES, LOGOUT_SESSION);
    187      1.1      agc 			return 1;
    188      1.1      agc 		}
    189      1.1      agc 		pdu->temp_data = data;
    190      1.1      agc 		pdu->temp_data_len = len;
    191      1.1      agc 	}
    192      1.1      agc 
    193      1.1      agc 	pdu->io_vec[0].iov_base = data + offset;
    194      1.1      agc 	pdu->io_vec[0].iov_len = len;
    195      1.1      agc 
    196      1.1      agc 	uio = &pdu->uio;
    197      1.1      agc 
    198      1.1      agc 	uio->uio_iov = pdu->io_vec;
    199      1.1      agc 	uio->uio_iovcnt = 1;
    200      1.1      agc 	uio->uio_rw = UIO_READ;
    201      1.1      agc 	uio->uio_resid = len;
    202      1.1      agc #if (__NetBSD_Version__ >= 399000900)
    203      1.1      agc 	UIO_SETUP_SYSSPACE(uio);
    204      1.1      agc #else
    205      1.1      agc 	uio->uio_segflg = UIO_SYSSPACE;
    206      1.1      agc #endif
    207      1.1      agc 
    208      1.1      agc 	if (pad) {
    209      1.1      agc 		uio->uio_iovcnt++;
    210      1.1      agc 		uio->uio_iov[1].iov_base = pad_bytes;
    211      1.1      agc 		uio->uio_iov[1].iov_len = pad;
    212      1.1      agc 		uio->uio_resid += pad;
    213      1.1      agc 	}
    214      1.1      agc 
    215      1.1      agc 	if (conn->DataDigest) {
    216      1.1      agc 		i = uio->uio_iovcnt++;
    217      1.1      agc 		pdu->io_vec[i].iov_base = &pdu->data_digest;
    218      1.1      agc 		pdu->io_vec[i].iov_len = 4;
    219      1.1      agc 		uio->uio_resid += 4;
    220      1.1      agc 	}
    221      1.1      agc #ifdef ISCSI_TEST_MODE
    222      1.1      agc 	/* save data pointer and size */
    223      1.1      agc 	pdu->save_uio.uio_iov = (struct iovec *) data;
    224      1.1      agc 	pdu->save_uio.uio_resid = len;
    225      1.1      agc #endif
    226      1.1      agc 
    227      1.1      agc 	/* get the data */
    228      1.1      agc 	if (my_soo_read(conn, &pdu->uio, MSG_WAITALL) != 0) {
    229      1.1      agc 		return 1;
    230      1.1      agc 	}
    231      1.1      agc 	if (conn->DataDigest) {
    232      1.1      agc 		digest = gen_digest_2(data, len, pad_bytes, pad);
    233      1.1      agc 
    234      1.1      agc 		if (digest != pdu->data_digest) {
    235      1.1      agc 			DEBOUT(("Data Digest Error: comp = %08x, rx = %08x\n",
    236      1.1      agc 					digest, pdu->data_digest));
    237      1.1      agc 			switch (pdu->pdu.Opcode & OPCODE_MASK) {
    238      1.1      agc 			case TOP_SCSI_Response:
    239      1.1      agc 			case TOP_Text_Response:
    240      1.1      agc 				send_snack(pdu->connection, pdu, NULL, SNACK_STATUS_NAK);
    241      1.1      agc 				break;
    242      1.1      agc 
    243      1.1      agc 			case TOP_SCSI_Data_in:
    244      1.1      agc 				send_snack(pdu->connection, pdu, NULL, SNACK_DATA_NAK);
    245      1.1      agc 				break;
    246      1.1      agc 
    247      1.1      agc 			default:
    248      1.1      agc 				/* ignore all others */
    249      1.1      agc 				break;
    250      1.1      agc 			}
    251      1.1      agc 			return -1;
    252      1.1      agc 		}
    253      1.1      agc 	}
    254      1.1      agc 	return 0;
    255      1.1      agc }
    256      1.1      agc 
    257      1.1      agc 
    258      1.1      agc /*
    259      1.1      agc  * collect_text_data
    260      1.1      agc  *    Handle text continuation in login and text response PDUs
    261      1.1      agc  *
    262      1.1      agc  *    Parameter:
    263      1.1      agc  *          pdu      The received PDU
    264      1.1      agc  *          req_CCB  The CCB associated with the original request
    265      1.1      agc  *
    266      1.1      agc  *    Returns:    -1    if continue flag is set
    267      1.1      agc  *                0     if text is complete
    268      1.1      agc  *                +1    if an error occurred (out of resources)
    269      1.1      agc  */
    270      1.1      agc STATIC int
    271      1.1      agc collect_text_data(pdu_t *pdu, ccb_t *req_ccb)
    272      1.1      agc {
    273      1.1      agc 
    274      1.1      agc 	if (req_ccb->text_data) {
    275      1.1      agc 		int nlen;
    276      1.1      agc 		uint8_t *newp;
    277      1.1      agc 
    278      1.1      agc 		nlen = req_ccb->text_len + pdu->temp_data_len;
    279      1.1      agc 		/* Note: allocate extra 2 bytes for text terminator */
    280      1.1      agc 		if ((newp = malloc(nlen + 2, M_TEMP, M_WAITOK)) == NULL) {
    281  1.3.2.1      tls 			DEBOUT(("Collect Text Data: Out of Memory, ccb = %p\n", req_ccb));
    282      1.1      agc 			req_ccb->status = ISCSI_STATUS_NO_RESOURCES;
    283  1.3.2.1      tls 			/* XXX where is CCB freed? */
    284      1.1      agc 			return 1;
    285      1.1      agc 		}
    286      1.1      agc 		memcpy(newp, req_ccb->text_data, req_ccb->text_len);
    287      1.1      agc 		memcpy(&newp[req_ccb->text_len], pdu->temp_data, pdu->temp_data_len);
    288      1.1      agc 
    289      1.1      agc 		free(req_ccb->text_data, M_TEMP);
    290      1.1      agc 		free(pdu->temp_data, M_TEMP);
    291      1.1      agc 
    292      1.1      agc 		req_ccb->text_data = NULL;
    293      1.1      agc 		pdu->temp_data = newp;
    294      1.1      agc 		pdu->temp_data_len = nlen;
    295      1.1      agc 	}
    296      1.1      agc 
    297      1.1      agc 	if (pdu->pdu.Flags & FLAG_CONTINUE) {
    298      1.1      agc 		req_ccb->text_data = pdu->temp_data;
    299      1.1      agc 		req_ccb->text_len = pdu->temp_data_len;
    300      1.1      agc 		pdu->temp_data = NULL;
    301      1.1      agc 
    302      1.1      agc 		acknowledge_text(req_ccb->connection, pdu, req_ccb);
    303      1.1      agc 		return -1;
    304      1.1      agc 	}
    305      1.1      agc 	return 0;
    306      1.1      agc }
    307      1.1      agc 
    308      1.1      agc 
    309      1.1      agc /*
    310      1.1      agc  * check_StatSN
    311      1.1      agc  *    Check received vs. expected StatSN
    312      1.1      agc  *
    313      1.1      agc  *    Parameter:
    314      1.1      agc  *          conn     The connection
    315      1.1      agc  *          nw_sn    The received StatSN in network byte order
    316      1.1      agc  *          ack      Acknowledge this SN if TRUE
    317      1.1      agc  */
    318      1.1      agc 
    319      1.1      agc STATIC int
    320      1.1      agc check_StatSN(connection_t *conn, uint32_t nw_sn, bool ack)
    321      1.1      agc {
    322      1.1      agc 	int rc;
    323      1.1      agc 	uint32_t sn = ntohl(nw_sn);
    324      1.1      agc 
    325      1.1      agc 	rc = add_sernum(&conn->StatSN_buf, sn);
    326      1.1      agc 
    327      1.1      agc 	if (ack)
    328      1.1      agc 		ack_sernum(&conn->StatSN_buf, sn);
    329      1.1      agc 
    330      1.1      agc 	if (rc != 1) {
    331      1.3  mlelstv 		if (rc == 0) {
    332      1.3  mlelstv 			DEBOUT(("Duplicate PDU, ExpSN %d, Recvd: %d\n",
    333      1.3  mlelstv 				conn->StatSN_buf.ExpSN, sn));
    334      1.1      agc 			return -1;
    335      1.3  mlelstv 		}
    336      1.1      agc 
    337      1.1      agc 		if (rc < 0) {
    338      1.1      agc 			DEBOUT(("Excessive outstanding Status PDUs, ExpSN %d, Recvd: %d\n",
    339      1.1      agc 					conn->StatSN_buf.ExpSN, sn));
    340      1.1      agc 			handle_connection_error(conn, ISCSI_STATUS_PDUS_LOST,
    341      1.1      agc 									RECOVER_CONNECTION);
    342      1.1      agc 			return rc;
    343      1.1      agc 		}
    344      1.1      agc 
    345      1.1      agc 		DEBOUT(("Missing Status PDUs: First %d, num: %d\n",
    346      1.1      agc 				conn->StatSN_buf.ExpSN, rc - 1));
    347      1.1      agc 		if (conn->state == ST_FULL_FEATURE &&
    348      1.1      agc 			conn->session->ErrorRecoveryLevel) {
    349      1.1      agc 			snack_missing(conn, NULL, SNACK_STATUS_NAK,
    350      1.1      agc 						  conn->StatSN_buf.ExpSN, rc - 1);
    351      1.1      agc 		} else {
    352      1.1      agc 			DEBOUT(("StatSN killing connection (State = %d, "
    353      1.1      agc 					"ErrorRecoveryLevel = %d)\n",
    354      1.1      agc 					conn->state, conn->session->ErrorRecoveryLevel));
    355      1.1      agc 			handle_connection_error(conn, ISCSI_STATUS_PDUS_LOST,
    356      1.1      agc 									RECOVER_CONNECTION);
    357      1.1      agc 			return -1;
    358      1.1      agc 		}
    359      1.1      agc 	}
    360      1.1      agc 	return 0;
    361      1.1      agc }
    362      1.1      agc 
    363      1.1      agc 
    364      1.1      agc /*
    365      1.1      agc  * check_CmdSN
    366      1.1      agc  *    Check received vs. expected CmdSN
    367      1.1      agc  *
    368      1.1      agc  *    Parameter:
    369      1.1      agc  *          conn     The connection
    370      1.1      agc  *          nw_sn    The received ExpCmdSN in network byte order
    371      1.1      agc  */
    372      1.1      agc 
    373      1.1      agc STATIC void
    374      1.1      agc check_CmdSN(connection_t *conn, uint32_t nw_sn)
    375      1.1      agc {
    376      1.1      agc 	uint32_t sn = ntohl(nw_sn);
    377      1.1      agc 	ccb_t *ccb, *nxt;
    378      1.1      agc 
    379  1.3.2.1      tls 	TAILQ_FOREACH_SAFE(ccb, &conn->ccbs_waiting, chain, nxt) {
    380      1.1      agc 		DEBC(conn, 10,
    381      1.1      agc 			("CheckCmdSN - CmdSN=%d, ExpCmdSn=%d, waiting=%p, flags=%x\n",
    382      1.1      agc 			ccb->CmdSN, sn, ccb->pdu_waiting, ccb->flags));
    383      1.1      agc 		if (ccb->pdu_waiting != NULL && ccb->CmdSN > sn &&
    384      1.1      agc 			!(ccb->flags & CCBF_GOT_RSP)) {
    385      1.1      agc 			DEBC(conn, 1, ("CheckCmdSN resending - CmdSN=%d, ExpCmdSn=%d\n",
    386      1.1      agc 						   ccb->CmdSN, sn));
    387      1.1      agc 
    388      1.1      agc 			ccb->total_tries++;
    389      1.1      agc 
    390      1.1      agc 			if (++ccb->num_timeouts > MAX_CCB_TIMEOUTS ||
    391      1.1      agc 				ccb->total_tries > MAX_CCB_TRIES) {
    392      1.1      agc 				handle_connection_error(conn, ISCSI_STATUS_TIMEOUT,
    393      1.1      agc 					(ccb->total_tries <= MAX_CCB_TRIES) ? RECOVER_CONNECTION
    394      1.1      agc 														: LOGOUT_CONNECTION);
    395      1.1      agc 				break;
    396      1.1      agc 			} else {
    397      1.1      agc 				resend_pdu(ccb);
    398      1.1      agc 			}
    399      1.1      agc 		}
    400      1.1      agc 	}
    401      1.1      agc }
    402      1.1      agc 
    403      1.1      agc 
    404      1.1      agc /*
    405      1.1      agc  * receive_login_pdu
    406      1.1      agc  *    Handle receipt of a login response PDU.
    407      1.1      agc  *
    408      1.1      agc  *    Parameter:
    409      1.1      agc  *          conn     The connection
    410      1.1      agc  *          pdu      The PDU
    411      1.1      agc  *          req_CCB  The CCB associated with the original request (if any)
    412      1.1      agc  */
    413      1.1      agc 
    414      1.1      agc STATIC int
    415      1.1      agc receive_login_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
    416      1.1      agc {
    417      1.1      agc 	int rc;
    418      1.1      agc 
    419      1.3  mlelstv 	DEBC(conn, 9, ("Received Login Response PDU, op=%x, flags=%x, sn=%u\n",
    420      1.3  mlelstv 			pdu->pdu.Opcode, pdu->pdu.Flags,
    421      1.3  mlelstv 			ntohl(pdu->pdu.p.login_rsp.StatSN)));
    422      1.1      agc 
    423      1.1      agc 	if (req_ccb == NULL) {
    424      1.1      agc 		/* Duplicate?? */
    425      1.1      agc 		DEBOUT(("Received duplicate login response (no associated CCB)\n"));
    426      1.1      agc 		return -1;
    427      1.1      agc 	}
    428      1.1      agc 
    429      1.3  mlelstv 	if (pdu->pdu.p.login_rsp.StatusClass) {
    430      1.3  mlelstv 		DEBC(conn, 1, ("Login problem - Class = %x, Detail = %x\n",
    431      1.3  mlelstv 				pdu->pdu.p.login_rsp.StatusClass,
    432      1.3  mlelstv 				pdu->pdu.p.login_rsp.StatusDetail));
    433  1.3.2.1      tls 		wake_ccb(req_ccb, ISCSI_STATUS_LOGIN_FAILED);
    434      1.3  mlelstv 		return 0;
    435      1.3  mlelstv 	}
    436      1.3  mlelstv 
    437      1.3  mlelstv 	if (!conn->StatSN_buf.next_sn) {
    438      1.1      agc 		conn->StatSN_buf.next_sn = conn->StatSN_buf.ExpSN =
    439      1.1      agc 			ntohl(pdu->pdu.p.login_rsp.StatSN) + 1;
    440      1.3  mlelstv 	} else if (check_StatSN(conn, pdu->pdu.p.login_rsp.StatSN, TRUE))
    441      1.1      agc 		return -1;
    442      1.1      agc 
    443      1.1      agc 	if (pdu->temp_data_len) {
    444      1.3  mlelstv 		if ((rc = collect_text_data(pdu, req_ccb)) != 0)
    445      1.1      agc 			return max(rc, 0);
    446      1.1      agc 	}
    447      1.1      agc 
    448      1.3  mlelstv 	negotiate_login(conn, pdu, req_ccb);
    449      1.1      agc 
    450      1.3  mlelstv 	/* negotiate_login will decide whether login is complete or not */
    451      1.1      agc 	return 0;
    452      1.1      agc }
    453      1.1      agc 
    454      1.1      agc 
    455      1.1      agc /*
    456      1.1      agc  * receive_text_response_pdu
    457      1.1      agc  *    Handle receipt of a text response PDU.
    458      1.1      agc  *
    459      1.1      agc  *    Parameter:
    460      1.1      agc  *          conn     The connection
    461      1.1      agc  *          pdu      The PDU
    462      1.1      agc  *          req_CCB  The CCB associated with the original request (if any)
    463      1.1      agc  */
    464      1.1      agc 
    465      1.1      agc STATIC int
    466      1.1      agc receive_text_response_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
    467      1.1      agc {
    468      1.1      agc 	int rc;
    469      1.1      agc 
    470      1.1      agc 	DEBC(conn, 9, ("Received Text Response PDU, op=%x, flags=%x\n",
    471      1.1      agc 			pdu->pdu.Opcode, pdu->pdu.Flags));
    472      1.1      agc 
    473      1.1      agc 	if (check_StatSN(conn, pdu->pdu.p.text_rsp.StatSN, TRUE)) {
    474      1.1      agc 		return -1;
    475      1.1      agc 	}
    476      1.1      agc 	if (req_ccb == NULL) {
    477      1.1      agc 		DEBOUT(("Received unsolicited text response\n"));
    478      1.1      agc 		handle_connection_error(conn, ISCSI_STATUS_TARGET_ERROR,
    479      1.1      agc 							LOGOUT_CONNECTION);
    480      1.1      agc 		return -1;
    481      1.1      agc 	}
    482      1.1      agc 
    483      1.1      agc 	if (req_ccb->pdu_waiting != NULL) {
    484      1.1      agc 		SET_CCB_TIMEOUT(conn, req_ccb, COMMAND_TIMEOUT);
    485      1.1      agc 		req_ccb->num_timeouts = 0;
    486      1.1      agc 	}
    487      1.1      agc 
    488      1.1      agc 	if ((rc = collect_text_data(pdu, req_ccb)) != 0) {
    489      1.1      agc 		return max(0, rc);
    490      1.1      agc 	}
    491      1.1      agc 	negotiate_text(conn, pdu, req_ccb);
    492      1.1      agc 
    493      1.1      agc 	return 0;
    494      1.1      agc }
    495      1.1      agc 
    496      1.1      agc 
    497      1.1      agc /*
    498      1.1      agc  * receive_logout_pdu
    499      1.1      agc  *    Handle receipt of a logout response PDU.
    500      1.1      agc  *
    501      1.1      agc  *    Parameter:
    502      1.1      agc  *          conn     The connection
    503      1.1      agc  *          pdu      The PDU
    504      1.1      agc  *          req_CCB  The CCB associated with the original request (if any)
    505      1.1      agc  */
    506      1.1      agc 
    507      1.1      agc STATIC int
    508      1.1      agc receive_logout_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
    509      1.1      agc {
    510      1.1      agc 	bool otherconn;
    511      1.1      agc 	uint8_t response;
    512  1.3.2.1      tls 	uint32_t status;
    513      1.1      agc 
    514      1.1      agc 	otherconn = (req_ccb != NULL) ? (req_ccb->flags & CCBF_OTHERCONN) != 0 : 1;
    515      1.1      agc 	response = pdu->pdu.OpcodeSpecific [0];
    516      1.1      agc 	DEBC(conn, 1,
    517      1.1      agc 		("Received Logout PDU - CCB = %p, otherconn=%d, response=%d\n",
    518      1.1      agc 		req_ccb, otherconn, response));
    519      1.1      agc 
    520      1.1      agc 	if (req_ccb == NULL)
    521      1.1      agc 		return 0;
    522      1.1      agc 
    523      1.1      agc 	if (otherconn && check_StatSN(conn, pdu->pdu.p.logout_rsp.StatSN, TRUE))
    524      1.1      agc 		return -1;
    525      1.1      agc 
    526      1.1      agc 	switch (response) {
    527      1.1      agc 	case 0:
    528  1.3.2.1      tls 		status = ISCSI_STATUS_SUCCESS;
    529      1.1      agc 		break;
    530      1.1      agc 	case 1:
    531  1.3.2.1      tls 		status = ISCSI_STATUS_LOGOUT_CID_NOT_FOUND;
    532      1.1      agc 		break;
    533      1.1      agc 	case 2:
    534  1.3.2.1      tls 		status = ISCSI_STATUS_LOGOUT_RECOVERY_NS;
    535      1.1      agc 		break;
    536      1.1      agc 	default:
    537  1.3.2.1      tls 		status = ISCSI_STATUS_LOGOUT_ERROR;
    538      1.1      agc 		break;
    539      1.1      agc 	}
    540      1.1      agc 
    541      1.1      agc 	if (conn->session->ErrorRecoveryLevel >= 2 && response != 1) {
    542      1.1      agc 		connection_t *refconn = (otherconn) ? req_ccb->par : conn;
    543      1.1      agc 
    544      1.1      agc 		refconn->Time2Wait = ntohs(pdu->pdu.p.logout_rsp.Time2Wait);
    545      1.1      agc 		refconn->Time2Retain = ntohs(pdu->pdu.p.logout_rsp.Time2Retain);
    546      1.1      agc 	}
    547      1.1      agc 
    548  1.3.2.1      tls 	wake_ccb(req_ccb, status);
    549      1.1      agc 
    550      1.1      agc 	if (!otherconn && conn->state == ST_LOGOUT_SENT) {
    551      1.1      agc 		conn->terminating = ISCSI_STATUS_LOGOUT;
    552      1.1      agc 		conn->state = ST_SETTLING;
    553      1.1      agc 		conn->loggedout = (response) ? LOGOUT_FAILED : LOGOUT_SUCCESS;
    554      1.1      agc 
    555      1.1      agc 		callout_stop(&conn->timeout);
    556      1.1      agc 
    557      1.1      agc #ifdef ISCSI_TEST_MODE
    558      1.1      agc 		test_remove_connection(conn);
    559      1.1      agc #endif
    560      1.1      agc 		/* let send thread take over next step of cleanup */
    561      1.1      agc 		wakeup(&conn->pdus_to_send);
    562      1.1      agc 	}
    563      1.1      agc 
    564      1.1      agc 	return !otherconn;
    565      1.1      agc }
    566      1.1      agc 
    567      1.1      agc 
    568      1.1      agc /*
    569      1.1      agc  * receive_data_in_pdu
    570      1.1      agc  *    Handle receipt of a data in PDU.
    571      1.1      agc  *
    572      1.1      agc  *    Parameter:
    573      1.1      agc  *          conn     The connection
    574      1.1      agc  *          pdu      The PDU
    575      1.1      agc  *          req_CCB  The CCB associated with the original request (if any)
    576      1.1      agc  */
    577      1.1      agc 
    578      1.1      agc STATIC int
    579      1.1      agc receive_data_in_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
    580      1.1      agc {
    581      1.1      agc 	uint32_t dsl, sn;
    582      1.1      agc 	bool done;
    583      1.1      agc 	int rc;
    584      1.1      agc 
    585      1.1      agc 	dsl = ntoh3(pdu->pdu.DataSegmentLength);
    586      1.1      agc 
    587      1.1      agc 	if (req_ccb == NULL || !req_ccb->data_in || !req_ccb->data_len) {
    588      1.1      agc 		DEBOUT(("Received Data In, but req_ccb not waiting for it, ignored\n"));
    589      1.1      agc 		return 0;
    590      1.1      agc 	}
    591      1.1      agc 	req_ccb->flags |= CCBF_GOT_RSP;
    592      1.1      agc 
    593      1.1      agc 	if (req_ccb->pdu_waiting != NULL) {
    594      1.1      agc 		SET_CCB_TIMEOUT(conn, req_ccb, COMMAND_TIMEOUT);
    595      1.1      agc 		req_ccb->num_timeouts = 0;
    596      1.1      agc 	}
    597      1.1      agc 
    598      1.1      agc 	sn = ntohl(pdu->pdu.p.data_in.DataSN);
    599      1.1      agc 
    600      1.1      agc 	if ((rc = add_sernum(&req_ccb->DataSN_buf, sn)) != 1) {
    601      1.1      agc 		if (!rc) {
    602      1.1      agc 			return -1;
    603      1.1      agc 		}
    604      1.1      agc 		if (rc < 0) {
    605      1.1      agc 			DEBOUT(("Excessive outstanding Data PDUs\n"));
    606      1.1      agc 			handle_connection_error(req_ccb->connection,
    607      1.1      agc 				ISCSI_STATUS_PDUS_LOST, LOGOUT_CONNECTION);
    608      1.1      agc 			return -1;
    609      1.1      agc 		}
    610      1.1      agc 		DEBOUT(("Missing Data PDUs: First %d, num: %d\n",
    611      1.1      agc 				req_ccb->DataSN_buf.ExpSN, rc - 1));
    612      1.1      agc 
    613      1.1      agc 		if (conn->state == ST_FULL_FEATURE &&
    614      1.1      agc 			conn->session->ErrorRecoveryLevel) {
    615      1.1      agc 			snack_missing(req_ccb->connection, req_ccb,
    616      1.1      agc 				SNACK_DATA_NAK, req_ccb->DataSN_buf.ExpSN,
    617      1.1      agc 				rc - 1);
    618      1.1      agc 		} else {
    619      1.1      agc 			DEBOUT(("Killing connection (State=%d, ErrorRecoveryLevel=%d)\n",
    620      1.1      agc 					conn->state, conn->session->ErrorRecoveryLevel));
    621      1.1      agc 			handle_connection_error(conn, ISCSI_STATUS_PDUS_LOST,
    622      1.1      agc 						LOGOUT_CONNECTION);
    623      1.1      agc 			return -1;
    624      1.1      agc 		}
    625      1.1      agc 	}
    626      1.1      agc 
    627      1.1      agc 	ack_sernum(&req_ccb->DataSN_buf, sn);
    628      1.1      agc 
    629      1.1      agc 	req_ccb->xfer_len += dsl;
    630      1.1      agc 
    631      1.1      agc 	if ((pdu->pdu.Flags & FLAG_ACK) && conn->session->ErrorRecoveryLevel)
    632      1.1      agc 		send_snack(conn, pdu, req_ccb, SNACK_DATA_ACK);
    633      1.1      agc 
    634      1.1      agc 	done = sn_empty(&req_ccb->DataSN_buf);
    635      1.1      agc 
    636      1.1      agc 	if (pdu->pdu.Flags & FLAG_STATUS) {
    637      1.1      agc 		DEBC(conn, 10, ("Rx Data In Complete, done = %d\n", done));
    638      1.1      agc 
    639      1.1      agc 		req_ccb->flags |= CCBF_COMPLETE;
    640      1.1      agc 		/* successful transfer, reset recover count */
    641      1.1      agc 		conn->recover = 0;
    642      1.1      agc 
    643  1.3.2.1      tls 		if (done)
    644  1.3.2.1      tls 			wake_ccb(req_ccb, ISCSI_STATUS_SUCCESS);
    645  1.3.2.1      tls 		if (check_StatSN(conn, pdu->pdu.p.data_in.StatSN, done))
    646      1.1      agc 			return -1;
    647  1.3.2.1      tls 
    648      1.1      agc 	} else if (done && (req_ccb->flags & CCBF_COMPLETE)) {
    649  1.3.2.1      tls 		wake_ccb(req_ccb, ISCSI_STATUS_SUCCESS);
    650      1.1      agc 	}
    651      1.1      agc 	/* else wait for command response */
    652      1.1      agc 
    653      1.1      agc 	return 0;
    654      1.1      agc }
    655      1.1      agc 
    656      1.1      agc 
    657      1.1      agc /*
    658      1.1      agc  * receive_r2t_pdu
    659      1.1      agc  *    Handle receipt of a R2T PDU.
    660      1.1      agc  *
    661      1.1      agc  *    Parameter:
    662      1.1      agc  *          conn     The connection
    663      1.1      agc  *          pdu      The PDU
    664      1.1      agc  *          req_CCB  The CCB associated with the original request (if any)
    665      1.1      agc  */
    666      1.1      agc 
    667      1.1      agc STATIC int
    668      1.1      agc receive_r2t_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
    669      1.1      agc {
    670      1.1      agc 
    671      1.1      agc 	DEBC(conn, 10, ("Received R2T PDU - CCB = %p\n", req_ccb));
    672      1.1      agc 
    673      1.1      agc 	if (req_ccb != NULL) {
    674      1.1      agc 		if (req_ccb->pdu_waiting != NULL) {
    675      1.1      agc 			SET_CCB_TIMEOUT(conn, req_ccb, COMMAND_TIMEOUT);
    676      1.1      agc 			req_ccb->num_timeouts = 0;
    677      1.1      agc 		}
    678      1.1      agc 		send_data_out(conn, pdu, req_ccb, CCBDISP_NOWAIT, TRUE);
    679      1.1      agc 	}
    680      1.1      agc 
    681      1.1      agc 	return 0;
    682      1.1      agc }
    683      1.1      agc 
    684      1.1      agc 
    685      1.1      agc /*
    686      1.1      agc  * receive_command_response_pdu
    687      1.1      agc  *    Handle receipt of a command response PDU.
    688      1.1      agc  *
    689      1.1      agc  *    Parameter:
    690      1.1      agc  *          conn     The connection
    691      1.1      agc  *          pdu      The PDU
    692      1.1      agc  *          req_CCB  The CCB associated with the original request (if any)
    693      1.1      agc  */
    694      1.1      agc 
    695      1.1      agc STATIC int
    696      1.1      agc receive_command_response_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
    697      1.1      agc {
    698      1.1      agc 	int len, rc;
    699      1.1      agc 	bool done;
    700  1.3.2.1      tls 	uint32_t status;
    701      1.1      agc 
    702      1.1      agc 	/* Read any provided data */
    703      1.1      agc 	if (pdu->temp_data_len && req_ccb != NULL && req_ccb->sense_len_req) {
    704      1.1      agc 		len = min(req_ccb->sense_len_req,
    705      1.1      agc 				  ntohs(*((uint16_t *) pdu->temp_data)));
    706      1.1      agc 		memcpy(req_ccb->sense_ptr, ((uint16_t *) pdu->temp_data) + 1,
    707      1.1      agc 					len);
    708      1.1      agc 		req_ccb->sense_len_got = len;
    709      1.1      agc 	}
    710      1.1      agc 
    711      1.1      agc 	if (req_ccb == NULL) {
    712      1.1      agc 		/* Assume duplicate... */
    713      1.1      agc 		DEBOUT(("Possibly duplicate command response (no associated CCB)\n"));
    714      1.1      agc 		return -1;
    715      1.1      agc 	}
    716      1.1      agc 
    717      1.1      agc 	PERF_SNAP(req_ccb, PERF_PDURCVSTS);
    718      1.1      agc 
    719      1.1      agc 	if (req_ccb->pdu_waiting != NULL) {
    720      1.1      agc 		SET_CCB_TIMEOUT(conn, req_ccb, COMMAND_TIMEOUT);
    721      1.1      agc 		req_ccb->num_timeouts = 0;
    722      1.1      agc 	}
    723      1.1      agc 
    724      1.1      agc 	req_ccb->flags |= CCBF_COMPLETE;
    725      1.1      agc 	conn->recover = 0;	/* successful transfer, reset recover count */
    726      1.1      agc 
    727      1.1      agc 	if (pdu->pdu.OpcodeSpecific[0]) {	/* Response */
    728  1.3.2.1      tls 		status = ISCSI_STATUS_TARGET_FAILURE;
    729      1.1      agc 	} else {
    730      1.1      agc 		switch (pdu->pdu.OpcodeSpecific[1]) {	/* Status */
    731      1.1      agc 		case 0x00:
    732  1.3.2.1      tls 			status = ISCSI_STATUS_SUCCESS;
    733      1.1      agc 			break;
    734      1.1      agc 
    735      1.1      agc 		case 0x02:
    736  1.3.2.1      tls 			status = ISCSI_STATUS_CHECK_CONDITION;
    737      1.1      agc 			break;
    738      1.1      agc 
    739      1.1      agc 		case 0x08:
    740  1.3.2.1      tls 			status = ISCSI_STATUS_TARGET_BUSY;
    741      1.1      agc 			break;
    742      1.1      agc 
    743      1.1      agc 		default:
    744  1.3.2.1      tls 			status = ISCSI_STATUS_TARGET_ERROR;
    745      1.1      agc 			break;
    746      1.1      agc 		}
    747      1.1      agc 	}
    748      1.1      agc 
    749      1.1      agc 	if (pdu->pdu.Flags & (FLAG_OVERFLOW | FLAG_UNDERFLOW))
    750      1.1      agc 		req_ccb->residual = ntohl(pdu->pdu.p.response.ResidualCount);
    751      1.1      agc 
    752  1.3.2.1      tls 	done = status || sn_empty(&req_ccb->DataSN_buf);
    753      1.1      agc 
    754      1.1      agc 	DEBC(conn, 10, ("Rx Command Response rsp = %x, status = %x\n",
    755      1.1      agc 			pdu->pdu.OpcodeSpecific[0], pdu->pdu.OpcodeSpecific[1]));
    756      1.1      agc 
    757      1.1      agc 	rc = check_StatSN(conn, pdu->pdu.p.response.StatSN, done);
    758      1.1      agc 
    759      1.1      agc 	if (done)
    760  1.3.2.1      tls 		wake_ccb(req_ccb, status);
    761      1.1      agc 
    762      1.1      agc 	return rc;
    763      1.1      agc }
    764      1.1      agc 
    765      1.1      agc 
    766      1.1      agc /*
    767      1.1      agc  * receive_asynch_pdu
    768      1.1      agc  *    Handle receipt of an asynchronous message PDU.
    769      1.1      agc  *
    770      1.1      agc  *    Parameter:
    771      1.1      agc  *          conn     The connection
    772      1.1      agc  *          pdu      The PDU
    773      1.1      agc  */
    774      1.1      agc 
    775      1.1      agc STATIC int
    776      1.1      agc receive_asynch_pdu(connection_t *conn, pdu_t *pdu)
    777      1.1      agc {
    778      1.1      agc 
    779      1.1      agc 	DEBOUT(("Received Asynch PDU, Event %d\n", pdu->pdu.p.asynch.AsyncEvent));
    780      1.1      agc 
    781      1.1      agc 	switch (pdu->pdu.p.asynch.AsyncEvent) {
    782      1.1      agc 	case 0:		   		/* SCSI Asynch event. Don't know what to do with it... */
    783      1.1      agc 		break;
    784      1.1      agc 
    785      1.1      agc 	case 1:		   		/* Target requests logout. */
    786      1.1      agc 		if (conn->session->active_connections > 1) {
    787      1.1      agc 			kill_connection(conn, ISCSI_STATUS_TARGET_LOGOUT,
    788      1.1      agc 						LOGOUT_CONNECTION, FALSE);
    789      1.1      agc 		} else {
    790      1.1      agc 			kill_session(conn->session, ISCSI_STATUS_TARGET_LOGOUT,
    791      1.1      agc 						 LOGOUT_SESSION, FALSE);
    792      1.1      agc 		}
    793      1.1      agc 		break;
    794      1.1      agc 
    795      1.1      agc 	case 2:				/* Target is dropping connection */
    796      1.1      agc 		conn = find_connection(conn->session,
    797      1.1      agc 					ntohs(pdu->pdu.p.asynch.Parameter1));
    798      1.1      agc 		if (conn != NULL) {
    799      1.1      agc 			conn->Time2Wait = ntohs(pdu->pdu.p.asynch.Parameter2);
    800      1.1      agc 			conn->Time2Retain = ntohs(pdu->pdu.p.asynch.Parameter3);
    801      1.1      agc 			kill_connection(conn, ISCSI_STATUS_TARGET_DROP,
    802      1.1      agc 					NO_LOGOUT, TRUE);
    803      1.1      agc 		}
    804      1.1      agc 		break;
    805      1.1      agc 
    806      1.1      agc 	case 3:				/* Target is dropping all connections of session */
    807      1.1      agc 		conn->session->DefaultTime2Wait = ntohs(pdu->pdu.p.asynch.Parameter2);
    808      1.1      agc 		conn->session->DefaultTime2Retain = ntohs(pdu->pdu.p.asynch.Parameter3);
    809      1.1      agc 		kill_session(conn->session, ISCSI_STATUS_TARGET_DROP, NO_LOGOUT, TRUE);
    810      1.1      agc 		break;
    811      1.1      agc 
    812      1.1      agc 	case 4:				/* Target requests parameter negotiation */
    813      1.1      agc 		start_text_negotiation(conn);
    814      1.1      agc 		break;
    815      1.1      agc 
    816      1.1      agc 	default:
    817      1.1      agc 		/* ignore */
    818      1.1      agc 		break;
    819      1.1      agc 	}
    820      1.1      agc 	return 0;
    821      1.1      agc }
    822      1.1      agc 
    823      1.1      agc 
    824      1.1      agc /*
    825      1.1      agc  * receive_reject_pdu
    826      1.1      agc  *    Handle receipt of a reject PDU.
    827      1.1      agc  *
    828      1.1      agc  *    Parameter:
    829      1.1      agc  *          conn     The connection
    830      1.1      agc  *          pdu      The PDU
    831      1.1      agc  */
    832      1.1      agc 
    833      1.1      agc STATIC int
    834      1.1      agc receive_reject_pdu(connection_t *conn, pdu_t *pdu)
    835      1.1      agc {
    836      1.1      agc 	pdu_header_t *hpdu;
    837      1.1      agc 	ccb_t *req_ccb;
    838  1.3.2.1      tls 	uint32_t status;
    839      1.1      agc 
    840      1.1      agc 	DEBOUT(("Received Reject PDU, reason = %x, data_len = %d\n",
    841      1.1      agc 			pdu->pdu.OpcodeSpecific[0], pdu->temp_data_len));
    842      1.1      agc 
    843      1.1      agc 	if (pdu->temp_data_len >= BHS_SIZE) {
    844      1.1      agc 		hpdu = (pdu_header_t *) pdu->temp_data;
    845      1.1      agc 		req_ccb = ccb_from_itt(conn, hpdu->InitiatorTaskTag);
    846      1.1      agc 
    847      1.1      agc 		DEBC(conn, 9, ("Reject PDU ITT (ccb)= %x (%p)\n",
    848      1.1      agc 				hpdu->InitiatorTaskTag, req_ccb));
    849      1.1      agc 		if (!req_ccb) {
    850      1.1      agc 			return 0;
    851      1.1      agc 		}
    852      1.1      agc 		switch (pdu->pdu.OpcodeSpecific[0]) {
    853      1.1      agc 		case REJECT_DIGEST_ERROR:
    854      1.1      agc 			/* don't retransmit data out */
    855      1.1      agc 			if ((hpdu->Opcode & OPCODE_MASK) == IOP_SCSI_Data_out)
    856      1.1      agc 				return 0;
    857      1.1      agc 			resend_pdu(req_ccb);
    858      1.1      agc 			return 0;
    859      1.1      agc 
    860      1.1      agc 		case REJECT_IMMED_COMMAND:
    861      1.1      agc 		case REJECT_LONG_OPERATION:
    862      1.1      agc 			resend_pdu(req_ccb);
    863      1.1      agc 			return 0;
    864      1.1      agc 
    865      1.1      agc 		case REJECT_SNACK:
    866      1.1      agc 		case REJECT_PROTOCOL_ERROR:
    867  1.3.2.1      tls 			status = ISCSI_STATUS_PROTOCOL_ERROR;
    868      1.1      agc 			break;
    869      1.1      agc 
    870      1.1      agc 		case REJECT_CMD_NOT_SUPPORTED:
    871  1.3.2.1      tls 			status = ISCSI_STATUS_CMD_NOT_SUPPORTED;
    872      1.1      agc 			break;
    873      1.1      agc 
    874      1.1      agc 		case REJECT_INVALID_PDU_FIELD:
    875  1.3.2.1      tls 			status = ISCSI_STATUS_PDU_ERROR;
    876      1.1      agc 			break;
    877      1.1      agc 
    878      1.1      agc 		default:
    879  1.3.2.1      tls 			status = ISCSI_STATUS_GENERAL_ERROR;
    880      1.1      agc 			break;
    881      1.1      agc 		}
    882      1.1      agc 
    883  1.3.2.1      tls 		wake_ccb(req_ccb, status);
    884      1.1      agc 		handle_connection_error(conn, ISCSI_STATUS_PROTOCOL_ERROR,
    885      1.1      agc 							LOGOUT_CONNECTION);
    886      1.1      agc 	}
    887      1.1      agc 	return 0;
    888      1.1      agc }
    889      1.1      agc 
    890      1.1      agc 
    891      1.1      agc /*
    892      1.1      agc  * receive_task_management_pdu
    893      1.1      agc  *    Handle receipt of a task management PDU.
    894      1.1      agc  *
    895      1.1      agc  *    Parameter:
    896      1.1      agc  *          conn     The connection
    897      1.1      agc  *          pdu      The PDU
    898      1.1      agc  *          req_CCB  The CCB associated with the original request (if any)
    899      1.1      agc  */
    900      1.1      agc 
    901      1.1      agc STATIC int
    902      1.1      agc receive_task_management_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
    903      1.1      agc {
    904  1.3.2.1      tls 	uint32_t status;
    905      1.1      agc 
    906      1.1      agc 	DEBC(conn, 2, ("Received Task Management PDU, response %d, req_ccb %p\n",
    907      1.1      agc 			pdu->pdu.OpcodeSpecific[0], req_ccb));
    908      1.1      agc 
    909      1.1      agc 	if (req_ccb != NULL) {
    910      1.1      agc 		switch (pdu->pdu.OpcodeSpecific[0]) {	/* Response */
    911      1.1      agc 		case 0:
    912  1.3.2.1      tls 			status = ISCSI_STATUS_SUCCESS;
    913      1.1      agc 			break;
    914      1.1      agc 		case 1:
    915  1.3.2.1      tls 			status = ISCSI_STATUS_TASK_NOT_FOUND;
    916      1.1      agc 			break;
    917      1.1      agc 		case 2:
    918  1.3.2.1      tls 			status = ISCSI_STATUS_LUN_NOT_FOUND;
    919      1.1      agc 			break;
    920      1.1      agc 		case 3:
    921  1.3.2.1      tls 			status = ISCSI_STATUS_TASK_ALLEGIANT;
    922      1.1      agc 			break;
    923      1.1      agc 		case 4:
    924  1.3.2.1      tls 			status = ISCSI_STATUS_CANT_REASSIGN;
    925      1.1      agc 			break;
    926      1.1      agc 		case 5:
    927  1.3.2.1      tls 			status = ISCSI_STATUS_FUNCTION_UNSUPPORTED;
    928      1.1      agc 			break;
    929      1.1      agc 		case 6:
    930  1.3.2.1      tls 			status = ISCSI_STATUS_FUNCTION_NOT_AUTHORIZED;
    931      1.1      agc 			break;
    932      1.1      agc 		case 255:
    933  1.3.2.1      tls 			status = ISCSI_STATUS_FUNCTION_REJECTED;
    934      1.1      agc 			break;
    935      1.1      agc 		default:
    936  1.3.2.1      tls 			status = ISCSI_STATUS_UNKNOWN_REASON;
    937      1.1      agc 			break;
    938      1.1      agc 		}
    939  1.3.2.1      tls 		wake_ccb(req_ccb, status);
    940      1.1      agc 	}
    941      1.1      agc 
    942      1.1      agc 	check_StatSN(conn, pdu->pdu.p.task_rsp.StatSN, TRUE);
    943      1.1      agc 
    944      1.1      agc 	return 0;
    945      1.1      agc }
    946      1.1      agc 
    947      1.1      agc 
    948      1.1      agc /*
    949      1.1      agc  * receive_nop_in_pdu
    950      1.1      agc  *    Handle receipt of a Nop-In PDU.
    951      1.1      agc  *
    952      1.1      agc  *    Parameter:
    953      1.1      agc  *          conn     The connection
    954      1.1      agc  *          pdu      The PDU
    955      1.1      agc  *          req_CCB  The CCB associated with the original request (if any)
    956      1.1      agc  */
    957      1.1      agc 
    958      1.1      agc STATIC int
    959      1.1      agc receive_nop_in_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
    960      1.1      agc {
    961      1.1      agc 	DEBC(conn, 10,
    962      1.1      agc 		("Received NOP-In PDU, req_ccb=%p, ITT=%x, TTT=%x, StatSN=%x\n",
    963      1.1      agc 		req_ccb, pdu->pdu.InitiatorTaskTag,
    964      1.1      agc 		pdu->pdu.p.nop_in.TargetTransferTag,
    965      1.1      agc 		ntohl(pdu->pdu.p.nop_in.StatSN)));
    966      1.1      agc 
    967      1.1      agc 	if (pdu->pdu.InitiatorTaskTag == 0xffffffff) {
    968      1.1      agc 		/* this is a target ping - respond with a pong */
    969      1.1      agc 		if (pdu->pdu.p.nop_in.TargetTransferTag != 0xffffffff)
    970      1.1      agc 			send_nop_out(conn, pdu);
    971      1.1      agc 
    972      1.1      agc 		/*
    973      1.1      agc 		   Any receive resets the connection timeout, but we got a ping, which
    974      1.1      agc 		   means that it's likely the other side was waiting for something to
    975      1.1      agc 		   happen on the connection. If we aren't idle, send a ping right
    976      1.1      agc 		   away to synch counters (don't synch on this ping because other
    977      1.1      agc 		   PDUs may be on the way).
    978      1.1      agc 		 */
    979      1.1      agc 		if (TAILQ_FIRST(&conn->ccbs_waiting) != NULL)
    980      1.1      agc 			send_nop_out(conn, NULL);
    981      1.1      agc 	} else if (req_ccb != NULL) {
    982      1.1      agc 		/* this is a solicited ping, check CmdSN for lost commands */
    983      1.1      agc 		/* and advance StatSN */
    984      1.1      agc 		check_CmdSN(conn, pdu->pdu.p.nop_in.ExpCmdSN);
    985      1.1      agc 
    986  1.3.2.1      tls 		wake_ccb(req_ccb, ISCSI_STATUS_SUCCESS);
    987      1.1      agc 
    988      1.1      agc 		check_StatSN(conn, pdu->pdu.p.nop_in.StatSN, TRUE);
    989      1.1      agc 	}
    990      1.1      agc 
    991      1.1      agc 	return 0;
    992      1.1      agc }
    993      1.1      agc 
    994      1.1      agc 
    995      1.1      agc /*
    996      1.1      agc  * receive_pdu
    997      1.1      agc  *    Get parameters, call the appropriate handler for a received PDU.
    998      1.1      agc  *
    999      1.1      agc  *    Parameter:
   1000      1.1      agc  *          conn     The connection
   1001      1.1      agc  *          pdu      The PDU
   1002      1.1      agc  *
   1003      1.1      agc  *    Returns:    0 on success, nonzero if the connection is broken.
   1004      1.1      agc  */
   1005      1.1      agc 
   1006      1.1      agc STATIC int
   1007      1.1      agc receive_pdu(connection_t *conn, pdu_t *pdu)
   1008      1.1      agc {
   1009      1.1      agc 	ccb_t *req_ccb;
   1010      1.1      agc 	ccb_list_t waiting;
   1011  1.3.2.1      tls 	int rc, s;
   1012      1.1      agc 	uint32_t MaxCmdSN, digest;
   1013      1.1      agc 	session_t *sess = conn->session;
   1014      1.1      agc 
   1015      1.1      agc 	if (conn->HeaderDigest) {
   1016      1.1      agc 		digest = gen_digest(&pdu->pdu, BHS_SIZE);
   1017      1.1      agc 		if (digest != pdu->pdu.HeaderDigest) {
   1018      1.1      agc 			DEBOUT(("Header Digest Error: comp = %08x, rx = %08x\n",
   1019      1.1      agc 					digest, pdu->pdu.HeaderDigest));
   1020      1.1      agc #ifdef ISCSI_TEST_MODE
   1021      1.1      agc 			test_mode_rx(conn, pdu, TEST_INVALID_HEADER_CRC);
   1022      1.1      agc #endif
   1023      1.1      agc 			/* try to skip to next PDU */
   1024      1.1      agc 			try_resynch_receive(conn);
   1025      1.1      agc 			free_pdu(pdu);
   1026      1.1      agc 			return 0;
   1027      1.1      agc 		}
   1028      1.1      agc 	}
   1029      1.1      agc 
   1030      1.1      agc 	req_ccb = ccb_from_itt(conn, pdu->pdu.InitiatorTaskTag);
   1031      1.1      agc 
   1032      1.1      agc 	if (req_ccb != NULL && req_ccb->data_in && req_ccb->data_len &&
   1033      1.1      agc 		(pdu->pdu.Opcode & OPCODE_MASK) == TOP_SCSI_Data_in) {
   1034      1.1      agc 		uint32_t dsl, offset;
   1035      1.1      agc 
   1036      1.1      agc 		dsl = ntoh3(pdu->pdu.DataSegmentLength);
   1037      1.1      agc 		offset = ntohl(pdu->pdu.p.data_in.BufferOffset);
   1038      1.1      agc 
   1039      1.1      agc 		if ((offset + dsl) > req_ccb->data_len) {
   1040      1.1      agc 			DEBOUT(("Received more data than requested (len %d, offset %d)\n",
   1041      1.1      agc 					dsl, offset));
   1042      1.1      agc 			handle_connection_error(conn, ISCSI_STATUS_TARGET_ERROR, NO_LOGOUT);
   1043      1.1      agc 			return 1;
   1044      1.1      agc 		}
   1045      1.1      agc 		DEBC(conn, 10,
   1046      1.1      agc 			("Received Data in PDU - CCB = %p, Datalen = %d, Offset = %d\n",
   1047      1.1      agc 			req_ccb, dsl, offset));
   1048      1.1      agc 
   1049      1.1      agc 		PERF_SNAPC(req_ccb, PERF_BEGIN_PDURCVDATA);
   1050      1.1      agc 		rc = read_pdu_data(pdu, req_ccb->data_ptr, offset);
   1051      1.1      agc 		PERF_SNAP(req_ccb, PERF_END_PDURCVDATA);
   1052      1.1      agc 	} else {
   1053      1.1      agc 		rc = read_pdu_data(pdu, NULL, 0);
   1054      1.1      agc 	}
   1055      1.1      agc #ifdef ISCSI_TEST_MODE
   1056      1.1      agc 	if (test_mode_rx(conn, pdu, rc)) {
   1057      1.1      agc 		free_pdu(pdu);
   1058      1.1      agc 		return rc;
   1059      1.1      agc 	}
   1060      1.1      agc #endif
   1061      1.1      agc 	if (!rc && (conn->state <= ST_WINDING_DOWN ||
   1062      1.1      agc 		(pdu->pdu.Opcode & OPCODE_MASK) == TOP_Logout_Response)) {
   1063      1.1      agc 
   1064      1.1      agc 		switch (pdu->pdu.Opcode & OPCODE_MASK) {
   1065      1.1      agc 		case TOP_NOP_In:
   1066      1.1      agc 			rc = receive_nop_in_pdu(conn, pdu, req_ccb);
   1067      1.1      agc 			break;
   1068      1.1      agc 
   1069      1.1      agc 		case TOP_SCSI_Response:
   1070      1.1      agc 			rc = receive_command_response_pdu(conn, pdu, req_ccb);
   1071      1.1      agc 			break;
   1072      1.1      agc 
   1073      1.1      agc 		case TOP_SCSI_Task_Management:
   1074      1.1      agc 			rc = receive_task_management_pdu(conn, pdu, req_ccb);
   1075      1.1      agc 			break;
   1076      1.1      agc 
   1077      1.1      agc 		case TOP_Login_Response:
   1078      1.1      agc 			rc = receive_login_pdu(conn, pdu, req_ccb);
   1079      1.1      agc 			break;
   1080      1.1      agc 
   1081      1.1      agc 		case TOP_Text_Response:
   1082      1.1      agc 			rc = receive_text_response_pdu(conn, pdu, req_ccb);
   1083      1.1      agc 			break;
   1084      1.1      agc 
   1085      1.1      agc 		case TOP_SCSI_Data_in:
   1086      1.1      agc 			rc = receive_data_in_pdu(conn, pdu, req_ccb);
   1087      1.1      agc 			break;
   1088      1.1      agc 
   1089      1.1      agc 		case TOP_Logout_Response:
   1090      1.1      agc 			rc = receive_logout_pdu(conn, pdu, req_ccb);
   1091      1.1      agc 			break;
   1092      1.1      agc 
   1093      1.1      agc 		case TOP_R2T:
   1094      1.1      agc 			rc = receive_r2t_pdu(conn, pdu, req_ccb);
   1095      1.1      agc 			break;
   1096      1.1      agc 
   1097      1.1      agc 		case TOP_Asynchronous_Message:
   1098      1.1      agc 			rc = receive_asynch_pdu(conn, pdu);
   1099      1.1      agc 			break;
   1100      1.1      agc 
   1101      1.1      agc 		case TOP_Reject:
   1102      1.1      agc 			rc = receive_reject_pdu(conn, pdu);
   1103      1.1      agc 			break;
   1104      1.1      agc 
   1105      1.1      agc 		default:
   1106      1.1      agc 			DEBOUT(("Received Invalid Opcode %x\n", pdu->pdu.Opcode));
   1107      1.1      agc 			try_resynch_receive(conn);
   1108      1.1      agc 			rc = -1;
   1109      1.1      agc 			break;
   1110      1.1      agc 		}
   1111      1.1      agc 	}
   1112      1.1      agc 
   1113      1.1      agc 	free_pdu(pdu);
   1114      1.1      agc 	if (rc)
   1115      1.1      agc 		return rc;
   1116      1.1      agc 
   1117      1.1      agc 	/* MaxCmdSN and ExpCmdSN are in the same place in all received PDUs */
   1118      1.1      agc 	sess->ExpCmdSN = max(sess->ExpCmdSN, ntohl(pdu->pdu.p.nop_in.ExpCmdSN));
   1119      1.1      agc 	MaxCmdSN = ntohl(pdu->pdu.p.nop_in.MaxCmdSN);
   1120      1.1      agc 
   1121      1.1      agc 	/* received a valid frame, reset timeout */
   1122      1.1      agc 
   1123      1.1      agc 	SET_CONN_TIMEOUT(conn,
   1124      1.1      agc 			(((pdu->pdu.Opcode & OPCODE_MASK) == TOP_NOP_In) &&
   1125      1.1      agc 			(TAILQ_FIRST(&conn->ccbs_waiting) == NULL)) ?
   1126      1.1      agc 			conn->idle_timeout_val : CONNECTION_TIMEOUT);
   1127      1.1      agc 	conn->num_timeouts = 0;
   1128      1.1      agc 
   1129      1.1      agc 	/*
   1130      1.1      agc 	 * Un-throttle - wakeup all CCBs waiting for MaxCmdSN to increase.
   1131      1.1      agc 	 * We have to handle wait/nowait CCBs a bit differently.
   1132      1.1      agc 	 */
   1133      1.1      agc 	if (MaxCmdSN != sess->MaxCmdSN) {
   1134      1.1      agc 		sess->MaxCmdSN = MaxCmdSN;
   1135      1.1      agc 		if (TAILQ_FIRST(&sess->ccbs_throttled) == NULL)
   1136      1.1      agc 			return 0;
   1137      1.1      agc 
   1138      1.1      agc 		DEBC(conn, 1, ("Unthrottling - MaxCmdSN = %d\n", MaxCmdSN));
   1139      1.1      agc 
   1140  1.3.2.1      tls 		s = splbio();
   1141      1.2   mhitch 		TAILQ_INIT(&waiting);
   1142  1.3.2.1      tls 		while ((req_ccb = TAILQ_FIRST(&sess->ccbs_throttled)) != NULL) {
   1143  1.3.2.1      tls 			throttle_ccb(req_ccb, FALSE);
   1144  1.3.2.1      tls 			TAILQ_INSERT_TAIL(&waiting, req_ccb, chain);
   1145  1.3.2.1      tls 		}
   1146  1.3.2.1      tls 		splbio();
   1147  1.3.2.1      tls 
   1148      1.1      agc 		while ((req_ccb = TAILQ_FIRST(&waiting)) != NULL) {
   1149      1.1      agc 			TAILQ_REMOVE(&waiting, req_ccb, chain);
   1150  1.3.2.1      tls 
   1151      1.1      agc 			DEBC(conn, 1, ("Unthrottling - ccb = %p, disp = %d\n",
   1152      1.1      agc 					req_ccb, req_ccb->disp));
   1153      1.1      agc 
   1154      1.1      agc 			if (req_ccb->flags & CCBF_WAITING)
   1155      1.1      agc 				wakeup(req_ccb);
   1156      1.1      agc 			else
   1157      1.1      agc 				send_command(req_ccb, req_ccb->disp, FALSE, FALSE);
   1158      1.1      agc 		}
   1159      1.1      agc 	}
   1160      1.1      agc 
   1161      1.1      agc 	return 0;
   1162      1.1      agc }
   1163      1.1      agc 
   1164      1.1      agc /*****************************************************************************/
   1165      1.1      agc 
   1166      1.1      agc /*
   1167      1.1      agc  * iscsi_receive_thread
   1168      1.1      agc  *    Per connection thread handling receive data.
   1169      1.1      agc  *
   1170      1.1      agc  *    Parameter:
   1171      1.1      agc  *          conn     The connection
   1172      1.1      agc  */
   1173      1.1      agc 
   1174      1.1      agc void
   1175      1.1      agc iscsi_rcv_thread(void *par)
   1176      1.1      agc {
   1177      1.1      agc 	connection_t *conn = (connection_t *) par;
   1178      1.1      agc 	pdu_t *pdu;
   1179      1.1      agc 	size_t hlen;
   1180      1.1      agc 
   1181      1.1      agc 	do {
   1182      1.1      agc 		while (!conn->terminating) {
   1183  1.3.2.1      tls 			pdu = get_pdu(conn, TRUE);
   1184      1.1      agc 			pdu->uio.uio_iov = pdu->io_vec;
   1185      1.1      agc 			UIO_SETUP_SYSSPACE(&pdu->uio);
   1186      1.1      agc 			pdu->uio.uio_iovcnt = 1;
   1187      1.1      agc 			pdu->uio.uio_rw = UIO_READ;
   1188      1.1      agc 
   1189      1.1      agc 			pdu->io_vec[0].iov_base = &pdu->pdu;
   1190      1.1      agc 			hlen = (conn->HeaderDigest) ? BHS_SIZE + 4 : BHS_SIZE;
   1191      1.1      agc 			pdu->io_vec[0].iov_len = hlen;
   1192      1.1      agc 			pdu->uio.uio_resid = hlen;
   1193      1.1      agc 
   1194      1.1      agc 			DEBC(conn, 99, ("Receive thread waiting for data\n"));
   1195      1.1      agc 			if (my_soo_read(conn, &pdu->uio, MSG_WAITALL)) {
   1196      1.1      agc 				free_pdu(pdu);
   1197      1.1      agc 				break;
   1198      1.1      agc 			}
   1199      1.1      agc 			/* Check again for header digest */
   1200      1.1      agc 			/* (it may have changed during the wait) */
   1201      1.1      agc 			if (hlen == BHS_SIZE && conn->HeaderDigest) {
   1202      1.1      agc 				pdu->uio.uio_iov = pdu->io_vec;
   1203      1.1      agc 				pdu->uio.uio_iovcnt = 1;
   1204      1.1      agc 				pdu->io_vec[0].iov_base = &pdu->pdu.HeaderDigest;
   1205      1.1      agc 				pdu->io_vec[0].iov_len = 4;
   1206      1.1      agc 				pdu->uio.uio_resid = 4;
   1207      1.1      agc 				if (my_soo_read(conn, &pdu->uio, MSG_WAITALL)) {
   1208      1.1      agc 					free_pdu(pdu);
   1209      1.1      agc 					break;
   1210      1.1      agc 				}
   1211      1.1      agc 			}
   1212      1.1      agc 
   1213      1.1      agc 			if (receive_pdu(conn, pdu) > 0) {
   1214      1.1      agc 				break;
   1215      1.1      agc 			}
   1216      1.1      agc 		}
   1217      1.1      agc 		if (!conn->destroy) {
   1218  1.3.2.1      tls 			tsleep(conn, PRIBIO, "conn_idle", 30 * hz);
   1219      1.1      agc 		}
   1220      1.1      agc 	} while (!conn->destroy);
   1221      1.1      agc 
   1222      1.1      agc 	conn->rcvproc = NULL;
   1223      1.1      agc 	DEBC(conn, 5, ("Receive thread exits\n"));
   1224      1.1      agc 	kthread_exit(0);
   1225      1.1      agc }
   1226