Home | History | Annotate | Line # | Download | only in iscsi
iscsi_send.c revision 1.1.2.1
      1  1.1.2.1  yamt /*	$NetBSD: iscsi_send.c,v 1.1.2.1 2012/10/30 17:21:17 yamt 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/filedesc.h>
     35      1.1   agc #include <sys/socket.h>
     36      1.1   agc #include <sys/socketvar.h>
     37      1.1   agc 
     38      1.1   agc /*#define LUN_1  1 */
     39      1.1   agc 
     40      1.1   agc /*****************************************************************************/
     41      1.1   agc 
     42      1.1   agc /*
     43      1.1   agc  * my_soo_write:
     44      1.1   agc  *    Replacement for soo_write with flag handling.
     45      1.1   agc  *
     46      1.1   agc  *    Parameter:
     47      1.1   agc  *          conn     The connection
     48      1.1   agc  *          u        The uio descriptor
     49      1.1   agc  *
     50      1.1   agc  *    Returns:    0 on success, else EIO.
     51      1.1   agc  */
     52      1.1   agc 
     53      1.1   agc STATIC int
     54      1.1   agc my_soo_write(connection_t *conn, struct uio *u)
     55      1.1   agc {
     56      1.1   agc 	struct socket *so = (struct socket *) conn->sock->f_data;
     57      1.1   agc 	size_t resid = u->uio_resid;
     58      1.1   agc 	int ret;
     59      1.1   agc 
     60      1.1   agc 	assert(resid != 0);
     61      1.1   agc 
     62      1.1   agc #if (__NetBSD_Version__ >= 300000000)
     63      1.1   agc 	ret = sosend(so, NULL, u, NULL, NULL, 0, conn->threadobj);
     64      1.1   agc #else
     65      1.1   agc 	ret = sosend(so, NULL, u, NULL, NULL, 0);
     66      1.1   agc #endif
     67      1.1   agc 
     68      1.1   agc 	DEB(99, ("soo_write done: len = %zu\n", u->uio_resid));
     69      1.1   agc 
     70      1.1   agc 	if (ret != 0 || u->uio_resid) {
     71      1.1   agc 		DEBC(conn, 0, ("Write failed sock %p (ret: %d, req: %zu, resid: %zu)\n",
     72      1.1   agc 			conn->sock, ret, resid, u->uio_resid));
     73      1.1   agc 		handle_connection_error(conn, ISCSI_STATUS_SOCKET_ERROR, NO_LOGOUT);
     74      1.1   agc 		return EIO;
     75      1.1   agc 	}
     76      1.1   agc 	return 0;
     77      1.1   agc }
     78      1.1   agc 
     79      1.1   agc /*****************************************************************************/
     80      1.1   agc 
     81      1.1   agc /*
     82      1.1   agc  * assign_connection:
     83      1.1   agc  *    This function returns the connection to use for the next transaction.
     84      1.1   agc  *
     85      1.1   agc  *    Parameter:  The session
     86      1.1   agc  *
     87      1.1   agc  *    Returns:    The connection
     88      1.1   agc  */
     89      1.1   agc 
     90      1.1   agc connection_t *
     91      1.1   agc assign_connection(session_t *session, bool waitok)
     92      1.1   agc {
     93      1.1   agc 	connection_t *conn, *next;
     94      1.1   agc 
     95      1.1   agc 	do {
     96      1.1   agc 		if ((conn = session->mru_connection) == NULL) {
     97      1.1   agc 			return NULL;
     98      1.1   agc 		}
     99      1.1   agc 		next = conn;
    100      1.1   agc 		do {
    101      1.1   agc 			next = TAILQ_NEXT(next, connections);
    102      1.1   agc 			if (next == NULL) {
    103      1.1   agc 				next = TAILQ_FIRST(&session->conn_list);
    104      1.1   agc 			}
    105      1.1   agc 		} while (next != NULL && next != conn &&
    106      1.1   agc 				 next->state != ST_FULL_FEATURE);
    107      1.1   agc 
    108      1.1   agc 		if (next->state != ST_FULL_FEATURE) {
    109      1.1   agc 			if (waitok) {
    110      1.1   agc 				tsleep(session, PRIBIO, "iscsi_assign_connection", 0);
    111      1.1   agc 				next = TAILQ_FIRST(&session->conn_list);
    112      1.1   agc 			} else {
    113      1.1   agc 				return NULL;
    114      1.1   agc 			}
    115      1.1   agc 		} else {
    116      1.1   agc 			session->mru_connection = next;
    117      1.1   agc 		}
    118      1.1   agc 	} while (next != NULL && next->state != ST_FULL_FEATURE);
    119      1.1   agc 
    120      1.1   agc 	return next;
    121      1.1   agc }
    122      1.1   agc 
    123      1.1   agc 
    124      1.1   agc /*
    125      1.1   agc  * reassign_tasks:
    126      1.1   agc  *    Reassign pending commands to one of the still existing connections
    127      1.1   agc  *    of a session.
    128      1.1   agc  *
    129      1.1   agc  *    Parameter:
    130      1.1   agc  *          oldconn		The terminating connection
    131      1.1   agc  */
    132      1.1   agc 
    133      1.1   agc STATIC void
    134      1.1   agc reassign_tasks(connection_t *oldconn)
    135      1.1   agc {
    136      1.1   agc 	session_t *sess = oldconn->session;
    137      1.1   agc 	connection_t *conn;
    138      1.1   agc 	ccb_t *ccb;
    139      1.1   agc 	pdu_t *pdu = NULL;
    140      1.1   agc 	pdu_t *opdu;
    141      1.1   agc 	int no_tm = 1;
    142      1.1   agc 	int rc = 1;
    143      1.1   agc 
    144      1.1   agc 	if ((conn = assign_connection(sess, FALSE)) == NULL) {
    145      1.1   agc 		DEB(1, ("Reassign_tasks of Session %d, connection %d failed, "
    146      1.1   agc 			    "no active connection\n",
    147      1.1   agc 			    sess->id, oldconn->id));
    148      1.1   agc 		return;
    149      1.1   agc 	}
    150      1.1   agc 
    151      1.1   agc 	if (sess->ErrorRecoveryLevel >= 2) {
    152      1.1   agc 		if (oldconn->loggedout == NOT_LOGGED_OUT) {
    153      1.1   agc 			oldconn->loggedout = LOGOUT_SENT;
    154      1.1   agc 			no_tm = send_logout(conn, oldconn, RECOVER_CONNECTION, TRUE);
    155      1.1   agc 			oldconn->loggedout = (rc) ? LOGOUT_FAILED : LOGOUT_SUCCESS;
    156      1.1   agc 			if (!oldconn->Time2Retain) {
    157      1.1   agc 				DEBC(conn, 1, ("Time2Retain is zero, setting no_tm\n"));
    158      1.1   agc 				no_tm = 1;
    159      1.1   agc 			}
    160      1.1   agc 		} else if (oldconn->loggedout == LOGOUT_SUCCESS) {
    161      1.1   agc 			no_tm = 0;
    162      1.1   agc 		}
    163      1.1   agc 		if (!no_tm && oldconn->Time2Wait) {
    164      1.1   agc 			DEBC(conn, 1, ("Time2Wait=%d, hz=%d, waiting...\n",
    165      1.1   agc 						   oldconn->Time2Wait, hz));
    166      1.1   agc 			tsleep(&no_tm, PRIBIO, "Time2Wait", oldconn->Time2Wait * hz);
    167      1.1   agc 		}
    168      1.1   agc 	}
    169      1.1   agc 
    170      1.1   agc 	DEBC(conn, 1, ("Reassign_tasks: Session %d, conn %d -> conn %d, no_tm=%d\n",
    171      1.1   agc 		sess->id, oldconn->id, conn->id, no_tm));
    172      1.1   agc 
    173      1.1   agc 	TAILQ_FOREACH(ccb, &oldconn->ccbs_waiting, chain) {
    174      1.1   agc 		/* Copy PDU contents (PDUs are bound to connection) */
    175      1.1   agc 		if ((pdu = get_pdu(conn)) == NULL) {
    176      1.1   agc 			break;
    177      1.1   agc 		}
    178      1.1   agc 		opdu = ccb->pdu_waiting;
    179      1.1   agc 		*pdu = *opdu;
    180      1.1   agc 		/* restore overwritten back ptr */
    181      1.1   agc 		pdu->connection = conn;
    182      1.1   agc 
    183      1.1   agc 		/* fixup saved UIO and IOVEC (regular one will be overwritten anyway) */
    184      1.1   agc 		pdu->save_uio.uio_iov = pdu->io_vec;
    185      1.1   agc 		pdu->save_iovec [0].iov_base = &pdu->pdu;
    186      1.1   agc 
    187      1.1   agc 		if (conn->DataDigest && pdu->save_uio.uio_iovcnt > 1) {
    188      1.1   agc 			if (pdu->save_iovec [2].iov_base == NULL) {
    189      1.1   agc 				pdu->save_iovec [2].iov_base = &pdu->data_digest;
    190      1.1   agc 				pdu->save_uio.uio_iovcnt = 3;
    191      1.1   agc 			} else {
    192      1.1   agc 				pdu->save_iovec [3].iov_base = &pdu->data_digest;
    193      1.1   agc 				pdu->save_uio.uio_iovcnt = 4;
    194      1.1   agc 			}
    195      1.1   agc 		}
    196      1.1   agc 		pdu->save_iovec [0].iov_len =
    197      1.1   agc 			(conn->HeaderDigest) ? BHS_SIZE + 4 : BHS_SIZE;
    198      1.1   agc 
    199      1.1   agc 		ccb->pdu_waiting = pdu;
    200      1.1   agc 		ccb->connection = conn;
    201      1.1   agc 		ccb->num_timeouts = 0;
    202  1.1.2.1  yamt 		oldconn->usecount--;
    203  1.1.2.1  yamt 		conn->usecount++;
    204      1.1   agc 
    205      1.1   agc 		DEBC(conn, 1, ("CCB %p: Copied PDU %p to %p\n",
    206      1.1   agc 					   ccb, opdu, pdu));
    207      1.1   agc 
    208      1.1   agc 		/* free the old PDU */
    209      1.1   agc 		opdu->temp_data = NULL;
    210      1.1   agc 		free_pdu(opdu);
    211      1.1   agc 	}
    212      1.1   agc 
    213      1.1   agc 	if (pdu == NULL) {
    214      1.1   agc 		DEBC(conn, 1, ("Error while copying PDUs in reassign_tasks!\n"));
    215      1.1   agc 		/* give up recovering, the other connection is screwed up as well... */
    216      1.1   agc 		while ((ccb = TAILQ_FIRST(&oldconn->ccbs_waiting)) != NULL) {
    217      1.1   agc 			ccb->status = oldconn->terminating;
    218      1.1   agc 			wake_ccb(ccb);
    219      1.1   agc 		}
    220      1.1   agc 		return;
    221      1.1   agc 	}
    222      1.1   agc 
    223      1.1   agc 	while ((ccb = TAILQ_FIRST(&oldconn->ccbs_waiting)) != NULL) {
    224      1.1   agc 		TAILQ_REMOVE(&oldconn->ccbs_waiting, ccb, chain);
    225      1.1   agc 		TAILQ_INSERT_TAIL(&conn->ccbs_waiting, ccb, chain);
    226      1.1   agc 
    227      1.1   agc 		if (!no_tm) {
    228      1.1   agc 			rc = send_task_management(conn, ccb, NULL, TASK_REASSIGN);
    229      1.1   agc 		}
    230      1.1   agc 		/* if we get an error on reassign, restart the original request */
    231      1.1   agc 		if (no_tm || rc) {
    232      1.1   agc 			if (ccb->CmdSN < sess->ExpCmdSN) {
    233      1.1   agc 				pdu = ccb->pdu_waiting;
    234      1.1   agc 
    235      1.1   agc 				/* update CmdSN */
    236      1.1   agc 				DEBC(conn, 1, ("Resend Updating CmdSN - old %d, new %d\n",
    237      1.1   agc 					   ccb->CmdSN, sess->CmdSN));
    238      1.1   agc 				ccb->CmdSN = sess->CmdSN;
    239      1.1   agc 				if (!(pdu->pdu.Opcode & OP_IMMEDIATE)) {
    240      1.1   agc 					sess->CmdSN++;
    241      1.1   agc 				}
    242      1.1   agc 				pdu->pdu.p.command.CmdSN = htonl(ccb->CmdSN);
    243      1.1   agc 			}
    244      1.1   agc 			resend_pdu(ccb);
    245      1.1   agc 		} else {
    246      1.1   agc 			SET_CCB_TIMEOUT(conn, ccb, COMMAND_TIMEOUT);
    247      1.1   agc 		}
    248      1.1   agc 		DEBC(conn, 1, ("Reassign ccb %p, no_tm=%d, rc=%d\n",
    249      1.1   agc 					   ccb, no_tm, rc));
    250      1.1   agc 	}
    251      1.1   agc }
    252      1.1   agc 
    253      1.1   agc 
    254      1.1   agc /*
    255      1.1   agc  * iscsi_send_thread:
    256      1.1   agc  *    This thread services the send queue, writing the PDUs to the socket.
    257      1.1   agc  *    It also handles the cleanup when the connection is terminated.
    258      1.1   agc  *
    259      1.1   agc  *    Parameter:
    260      1.1   agc  *          par		The connection this thread services
    261      1.1   agc  */
    262      1.1   agc 
    263      1.1   agc void
    264      1.1   agc iscsi_send_thread(void *par)
    265      1.1   agc {
    266      1.1   agc 	connection_t *conn = (connection_t *) par;
    267      1.1   agc 	session_t *sess;
    268      1.1   agc 	ccb_t *ccb, *nccb;
    269      1.1   agc 	pdu_t *pdu;
    270      1.1   agc 	struct file *fp;
    271      1.1   agc 	int s;
    272      1.1   agc 
    273      1.1   agc 	sess = conn->session;
    274      1.1   agc 	/* so cleanup thread knows there's someone left */
    275  1.1.2.1  yamt 	iscsi_num_send_threads++;
    276      1.1   agc 
    277      1.1   agc 	do {
    278      1.1   agc 		while (!conn->terminating) {
    279      1.1   agc 			s = splbio();
    280      1.1   agc 			while (!conn->terminating &&
    281      1.1   agc 				(pdu = TAILQ_FIRST(&conn->pdus_to_send)) != NULL) {
    282      1.1   agc 				TAILQ_REMOVE(&conn->pdus_to_send, pdu, send_chain);
    283      1.1   agc 				pdu->flags &= ~PDUF_INQUEUE;
    284      1.1   agc 				splx(s);
    285      1.1   agc 
    286      1.1   agc 				PERF_PDUSNAPB(pdu);
    287      1.1   agc 
    288      1.1   agc #ifdef ISCSI_DEBUG
    289      1.1   agc 				if (!pdu->uio.uio_resid) {
    290      1.1   agc 					DEBOUT(("uio.resid = 0 in iscsi_send_thread! pdu=%p\n",
    291      1.1   agc 							pdu));
    292      1.1   agc 					assert(pdu->uio.uio_resid != 0);
    293      1.1   agc 				}
    294      1.1   agc #endif
    295      1.1   agc 				/*DEB (99,("Send thread woke up, pdu = %x)\n", (int)pdu)); */
    296      1.1   agc 
    297      1.1   agc #ifdef ISCSI_TEST_MODE
    298      1.1   agc 				if (!test_mode_tx(conn, pdu)) {
    299      1.1   agc 					 if (!(pdu->flags & PDUF_NOUPDATE)) {
    300      1.1   agc #endif
    301      1.1   agc 						 /* update ExpStatSN here to avoid discontinuities */
    302      1.1   agc 						 /* and delays in updating target */
    303      1.1   agc 						 pdu->pdu.p.command.ExpStatSN =
    304      1.1   agc 							 htonl(conn->StatSN_buf.ExpSN);
    305      1.1   agc 
    306      1.1   agc 						 if (conn->HeaderDigest)
    307      1.1   agc 							 pdu->pdu.HeaderDigest =
    308      1.1   agc 								gen_digest(&pdu->pdu, BHS_SIZE);
    309      1.1   agc #ifdef ISCSI_TEST_MODE
    310      1.1   agc 					 }
    311      1.1   agc #endif
    312      1.1   agc 					 my_soo_write(conn, &pdu->uio);
    313      1.1   agc #ifdef ISCSI_TEST_MODE
    314      1.1   agc 				}
    315      1.1   agc #endif
    316      1.1   agc 				PERF_PDUSNAPE(pdu);
    317      1.1   agc 
    318      1.1   agc 				if (pdu->disp <= PDUDISP_FREE) {
    319      1.1   agc 					free_pdu(pdu);
    320      1.1   agc 				} else {
    321      1.1   agc 					pdu->flags &= ~PDUF_BUSY;
    322      1.1   agc 				}
    323      1.1   agc 				s = splbio();
    324      1.1   agc 			}
    325      1.1   agc 
    326      1.1   agc 			/*DEB (99,("Send thread done, waiting (conn->terminating = %d)\n", */
    327      1.1   agc 			/* 		conn->terminating)); */
    328      1.1   agc 
    329      1.1   agc 			if (!conn->terminating) {
    330      1.1   agc 				tsleep(&conn->pdus_to_send, PRIBIO,
    331      1.1   agc 						"iscsisend", 0);
    332      1.1   agc 			}
    333      1.1   agc 
    334      1.1   agc 			splx(s);
    335      1.1   agc 		}
    336      1.1   agc 
    337      1.1   agc 		/* ------------------------------------------------------------------------
    338      1.1   agc 		 *    Here this thread takes over cleanup of the terminating connection.
    339      1.1   agc 		 * ------------------------------------------------------------------------
    340      1.1   agc 		 */
    341      1.1   agc 		callout_stop(&conn->timeout);
    342      1.1   agc 		conn->idle_timeout_val = CONNECTION_IDLE_TIMEOUT;
    343      1.1   agc 
    344      1.1   agc 		fp = conn->sock;
    345      1.1   agc 
    346      1.1   agc 		/*
    347  1.1.2.1  yamt 		 * We shutdown the socket here to force the receive
    348      1.1   agc 		 * thread to wake up
    349      1.1   agc 		 */
    350  1.1.2.1  yamt 		DEBC(conn, 9, ("Closing Socket %p\n", conn->sock));
    351      1.1   agc 		solock((struct socket *) fp->f_data);
    352      1.1   agc 		soshutdown((struct socket *) fp->f_data, SHUT_RDWR);
    353      1.1   agc 		sounlock((struct socket *) fp->f_data);
    354      1.1   agc 
    355      1.1   agc 		/* wake up any non-reassignable waiting CCBs */
    356      1.1   agc 		for (ccb = TAILQ_FIRST(&conn->ccbs_waiting); ccb != NULL; ccb = nccb) {
    357      1.1   agc 			nccb = TAILQ_NEXT(ccb, chain);
    358      1.1   agc 			if (!(ccb->flags & CCBF_REASSIGN) || ccb->pdu_waiting == NULL) {
    359  1.1.2.1  yamt 				DEBC(conn, 9, ("Terminating CCB %p (t=%p)\n",
    360  1.1.2.1  yamt 					ccb,&ccb->timeout));
    361      1.1   agc 				ccb->status = conn->terminating;
    362      1.1   agc 				wake_ccb(ccb);
    363      1.1   agc 			} else {
    364      1.1   agc 				callout_stop(&ccb->timeout);
    365      1.1   agc 				ccb->num_timeouts = 0;
    366      1.1   agc 			}
    367      1.1   agc 		}
    368      1.1   agc 
    369      1.1   agc 		/* clean out anything left in send queue */
    370      1.1   agc 		while ((pdu = TAILQ_FIRST(&conn->pdus_to_send)) != NULL) {
    371      1.1   agc 			TAILQ_REMOVE(&conn->pdus_to_send, pdu, send_chain);
    372      1.1   agc 			pdu->flags &= ~(PDUF_INQUEUE | PDUF_BUSY);
    373      1.1   agc 			/* if it's not attached to a waiting CCB, free it */
    374      1.1   agc 			if (pdu->owner == NULL ||
    375      1.1   agc 			    pdu->owner->pdu_waiting != pdu) {
    376      1.1   agc 				free_pdu(pdu);
    377      1.1   agc 			}
    378      1.1   agc 		}
    379      1.1   agc 
    380      1.1   agc         /* If there's another connection available, transfer pending tasks */
    381      1.1   agc 		if (sess->active_connections &&
    382      1.1   agc 			TAILQ_FIRST(&conn->ccbs_waiting) != NULL) {
    383      1.1   agc 			reassign_tasks (conn);
    384      1.1   agc 		} else if (!conn->destroy && conn->Time2Wait) {
    385      1.1   agc 			tsleep(&s, PRIBIO, "Time2Wait", conn->Time2Wait * hz);
    386      1.1   agc 		}
    387      1.1   agc 		/* notify event handlers of connection shutdown */
    388      1.1   agc 		add_event((conn->destroy) ? ISCSI_CONNECTION_TERMINATED
    389      1.1   agc 								  : ISCSI_RECOVER_CONNECTION,
    390      1.1   agc 				  sess->id, conn->id, conn->terminating);
    391      1.1   agc 
    392      1.1   agc 		if (!conn->destroy)
    393      1.1   agc 			tsleep(conn, PRIBIO, "conn_idle", 0);
    394      1.1   agc 
    395      1.1   agc 	} while (!conn->destroy);
    396      1.1   agc 
    397      1.1   agc 	/* wake up anyone waiting for a PDU */
    398      1.1   agc 	wakeup(&conn->pdu_pool);
    399      1.1   agc 
    400      1.1   agc 	/* wake up any waiting CCBs */
    401      1.1   agc 	while ((ccb = TAILQ_FIRST(&conn->ccbs_waiting)) != NULL) {
    402      1.1   agc 		ccb->status = conn->terminating;
    403      1.1   agc 		wake_ccb(ccb);
    404      1.1   agc 		/* NOTE: wake_ccb will remove the CCB from the queue */
    405      1.1   agc 	}
    406      1.1   agc 
    407      1.1   agc 	s = splbio();
    408      1.1   agc 	if (conn->in_session) {
    409      1.1   agc 		conn->in_session = FALSE;
    410      1.1   agc 		TAILQ_REMOVE(&sess->conn_list, conn, connections);
    411      1.1   agc 		sess->mru_connection = TAILQ_FIRST(&sess->conn_list);
    412      1.1   agc 	}
    413      1.1   agc 
    414  1.1.2.1  yamt 	TAILQ_INSERT_TAIL(&iscsi_cleanup_list, conn, connections);
    415      1.1   agc 	splx(s);
    416      1.1   agc 
    417  1.1.2.1  yamt 	wakeup(&iscsi_cleanup_list);
    418      1.1   agc 
    419      1.1   agc 	conn->sendproc = NULL;
    420      1.1   agc 	DEBC(conn, 5, ("Send thread exits\n"));
    421  1.1.2.1  yamt 	iscsi_num_send_threads--;
    422      1.1   agc 	kthread_exit(0);
    423      1.1   agc }
    424      1.1   agc 
    425      1.1   agc 
    426      1.1   agc /*
    427      1.1   agc  * send_pdu:
    428      1.1   agc  *    Enqueue a PDU to be sent, and handle its disposition as well as
    429      1.1   agc  *    the disposition of its associated CCB.
    430      1.1   agc  *
    431      1.1   agc  *    Parameter:
    432      1.1   agc  *          ccb      The associated CCB. May be NULL if cdisp is CCBDISP_NOWAIT
    433      1.1   agc  *                   and pdisp is not PDUDISP_WAIT
    434      1.1   agc  *          cdisp    The CCB's disposition
    435      1.1   agc  *          pdu      The PDU
    436      1.1   agc  *          pdisp    The PDU's disposition
    437      1.1   agc  */
    438      1.1   agc 
    439      1.1   agc STATIC void
    440      1.1   agc send_pdu(ccb_t *ccb, pdu_t *pdu, ccb_disp_t cdisp, pdu_disp_t pdisp)
    441      1.1   agc {
    442      1.1   agc 	connection_t *conn = pdu->connection;
    443      1.1   agc 	ccb_disp_t prev_cdisp = 0;
    444      1.1   agc 
    445      1.1   agc 	if (ccb != NULL) {
    446      1.1   agc 		prev_cdisp = ccb->disp;
    447      1.1   agc 		pdu->pdu.InitiatorTaskTag = ccb->ITT;
    448      1.1   agc 		pdu->owner = ccb;
    449      1.1   agc 		if (cdisp != CCBDISP_NOWAIT)
    450      1.1   agc 			ccb->disp = cdisp;
    451      1.1   agc 	}
    452      1.1   agc 
    453      1.1   agc 	pdu->disp = pdisp;
    454      1.1   agc 
    455      1.1   agc 	DEBC(conn, 10, ("Send_pdu: ccb=%p, pcd=%d, cdsp=%d, pdu=%p, pdsp=%d\n",
    456      1.1   agc 			ccb, prev_cdisp, cdisp, pdu, pdisp));
    457      1.1   agc 
    458      1.1   agc 	CS_BEGIN;
    459      1.1   agc 	if (pdisp == PDUDISP_WAIT) {
    460      1.1   agc 		ccb->pdu_waiting = pdu;
    461      1.1   agc 
    462      1.1   agc 		/* save UIO and IOVEC for retransmit */
    463      1.1   agc 		pdu->save_uio = pdu->uio;
    464      1.1   agc 		memcpy(pdu->save_iovec, pdu->io_vec, sizeof(pdu->save_iovec));
    465      1.1   agc 
    466      1.1   agc 		pdu->flags |= PDUF_BUSY;
    467      1.1   agc 	}
    468      1.1   agc 	/* Enqueue for sending */
    469      1.1   agc 	pdu->flags |= PDUF_INQUEUE;
    470      1.1   agc 
    471      1.1   agc 	if (pdu->flags & PDUF_PRIORITY)
    472      1.1   agc 		TAILQ_INSERT_HEAD(&conn->pdus_to_send, pdu, send_chain);
    473      1.1   agc 	else
    474      1.1   agc 		TAILQ_INSERT_TAIL(&conn->pdus_to_send, pdu, send_chain);
    475      1.1   agc 
    476      1.1   agc 	wakeup(&conn->pdus_to_send);
    477      1.1   agc 
    478      1.1   agc 	if (cdisp != CCBDISP_NOWAIT) {
    479      1.1   agc 		SET_CCB_TIMEOUT(conn, ccb, COMMAND_TIMEOUT);
    480      1.1   agc 
    481      1.1   agc 		if (prev_cdisp <= CCBDISP_NOWAIT)
    482      1.1   agc 			TAILQ_INSERT_TAIL(&conn->ccbs_waiting, ccb, chain);
    483      1.1   agc 
    484      1.1   agc 		if (cdisp == CCBDISP_WAIT)
    485      1.1   agc 			tsleep(ccb, PWAIT, "sendpdu", 0);
    486      1.1   agc 	}
    487      1.1   agc 	CS_END;
    488      1.1   agc }
    489      1.1   agc 
    490      1.1   agc 
    491      1.1   agc /*
    492      1.1   agc  * resend_pdu:
    493      1.1   agc  *    Re-Enqueue a PDU that has apparently gotten lost.
    494      1.1   agc  *
    495      1.1   agc  *    Parameter:
    496      1.1   agc  *          ccb      The associated CCB.
    497      1.1   agc  */
    498      1.1   agc 
    499      1.1   agc void
    500      1.1   agc resend_pdu(ccb_t *ccb)
    501      1.1   agc {
    502      1.1   agc 	connection_t *conn = ccb->connection;
    503      1.1   agc 	pdu_t *pdu = ccb->pdu_waiting;
    504      1.1   agc 	int s;
    505      1.1   agc 
    506      1.1   agc 	s = splbio ();
    507      1.1   agc 	if (pdu == NULL || (pdu->flags & PDUF_BUSY)) {
    508      1.1   agc 		splx (s);
    509      1.1   agc 		return;
    510      1.1   agc 	}
    511      1.1   agc 	pdu->flags |= PDUF_BUSY;
    512      1.1   agc 	splx (s);
    513      1.1   agc 
    514      1.1   agc 	/* restore UIO and IOVEC */
    515      1.1   agc 	pdu->uio = pdu->save_uio;
    516      1.1   agc 	memcpy(pdu->io_vec, pdu->save_iovec, sizeof(pdu->io_vec));
    517      1.1   agc 
    518      1.1   agc 	DEBC(conn, 8, ("ReSend_pdu ccb=%p, pdu=%p\n", ccb, pdu));
    519      1.1   agc 
    520      1.1   agc 	s = splbio ();
    521      1.1   agc 
    522      1.1   agc 	/* Enqueue for sending */
    523      1.1   agc 	pdu->flags |= PDUF_INQUEUE;
    524      1.1   agc 
    525      1.1   agc 	if (pdu->flags & PDUF_PRIORITY) {
    526      1.1   agc 		TAILQ_INSERT_HEAD(&conn->pdus_to_send, pdu, send_chain);
    527      1.1   agc 	} else {
    528      1.1   agc 		TAILQ_INSERT_TAIL(&conn->pdus_to_send, pdu, send_chain);
    529      1.1   agc 	}
    530      1.1   agc 	SET_CCB_TIMEOUT(conn, ccb, COMMAND_TIMEOUT);
    531      1.1   agc 	splx (s);
    532      1.1   agc 
    533      1.1   agc 	wakeup(&conn->pdus_to_send);
    534      1.1   agc }
    535      1.1   agc 
    536      1.1   agc 
    537      1.1   agc /*
    538      1.1   agc  * setup_tx_uio:
    539      1.1   agc  *    Initialize the uio structure for sending, including header,
    540      1.1   agc  *    data (if present), padding, and Data Digest.
    541      1.1   agc  *    Header Digest is generated in send thread.
    542      1.1   agc  *
    543      1.1   agc  *    Parameter:
    544      1.1   agc  *          pdu      The PDU
    545      1.1   agc  *          dsl      The Data Segment Length
    546      1.1   agc  *          data     The data pointer
    547      1.1   agc  *          read     TRUE if this is a read operation
    548      1.1   agc  */
    549      1.1   agc 
    550      1.1   agc STATIC void
    551      1.1   agc setup_tx_uio(pdu_t *pdu, uint32_t dsl, void *data, bool read)
    552      1.1   agc {
    553      1.1   agc 	static uint8_t pad_bytes[4] = { 0 };
    554      1.1   agc 	struct uio *uio;
    555      1.1   agc 	int i, pad, hlen;
    556      1.1   agc 	connection_t *conn = pdu->connection;
    557      1.1   agc 
    558      1.1   agc 	DEB(99, ("SetupTxUio: dlen = %d, dptr: %p, read: %d\n",
    559      1.1   agc 			 dsl, data, read));
    560      1.1   agc 
    561      1.1   agc 	if (!read && dsl) {
    562      1.1   agc 		hton3(dsl, pdu->pdu.DataSegmentLength);
    563      1.1   agc 	}
    564      1.1   agc 	hlen = (conn->HeaderDigest) ? BHS_SIZE + 4 : BHS_SIZE;
    565      1.1   agc 
    566      1.1   agc 	pdu->io_vec[0].iov_base = &pdu->pdu;
    567      1.1   agc 	pdu->io_vec[0].iov_len = hlen;
    568      1.1   agc 
    569      1.1   agc 	uio = &pdu->uio;
    570      1.1   agc 
    571      1.1   agc 	uio->uio_iov = pdu->io_vec;
    572      1.1   agc 	uio->uio_iovcnt = 1;
    573      1.1   agc 	uio->uio_rw = UIO_WRITE;
    574      1.1   agc 	uio->uio_resid = hlen;
    575      1.1   agc 	UIO_SETUP_SYSSPACE(uio);
    576      1.1   agc 
    577      1.1   agc 	if (!read && dsl) {
    578      1.1   agc 		uio->uio_iovcnt++;
    579      1.1   agc 		pdu->io_vec[1].iov_base = data;
    580      1.1   agc 		pdu->io_vec[1].iov_len = dsl;
    581      1.1   agc 		uio->uio_resid += dsl;
    582      1.1   agc 
    583      1.1   agc 		/* Pad to next multiple of 4 */
    584      1.1   agc 		pad = uio->uio_resid & 0x03;
    585      1.1   agc 		if (pad) {
    586      1.1   agc 			i = uio->uio_iovcnt++;
    587      1.1   agc 			pad = 4 - pad;
    588      1.1   agc 			pdu->io_vec[i].iov_base = pad_bytes;
    589      1.1   agc 			pdu->io_vec[i].iov_len = pad;
    590      1.1   agc 			uio->uio_resid += pad;
    591      1.1   agc 		}
    592      1.1   agc 
    593      1.1   agc 		if (conn->DataDigest) {
    594      1.1   agc 			pdu->data_digest = gen_digest_2(data, dsl, pad_bytes, pad);
    595      1.1   agc 			i = uio->uio_iovcnt++;
    596      1.1   agc 			pdu->io_vec[i].iov_base = &pdu->data_digest;
    597      1.1   agc 			pdu->io_vec[i].iov_len = 4;
    598      1.1   agc 			uio->uio_resid += 4;
    599      1.1   agc 		}
    600      1.1   agc 	}
    601      1.1   agc }
    602      1.1   agc 
    603      1.1   agc /*
    604      1.1   agc  * init_login_pdu:
    605      1.1   agc  *    Initialize the login PDU.
    606      1.1   agc  *
    607      1.1   agc  *    Parameter:
    608      1.1   agc  *          conn     The connection
    609      1.1   agc  *          pdu      The PDU
    610      1.1   agc  */
    611      1.1   agc 
    612      1.1   agc STATIC void
    613      1.1   agc init_login_pdu(connection_t *conn, pdu_t *ppdu, bool next)
    614      1.1   agc {
    615      1.1   agc 	pdu_header_t *pdu = &ppdu->pdu;
    616      1.1   agc 	login_isid_t *isid = (login_isid_t *) & pdu->LUN;
    617      1.1   agc 	uint8_t c_phase;
    618      1.1   agc 
    619      1.1   agc 	pdu->Opcode = IOP_Login_Request | OP_IMMEDIATE;
    620      1.1   agc 
    621      1.1   agc 	if (next) {
    622      1.1   agc 		c_phase = (pdu->Flags >> CSG_SHIFT) & SG_MASK;
    623      1.1   agc 		pdu->Flags = FLAG_TRANSIT | (c_phase << CSG_SHIFT) |
    624      1.1   agc 					 NEXT_PHASE(c_phase);
    625      1.1   agc 	}
    626      1.1   agc 
    627  1.1.2.1  yamt 	memcpy(isid, &iscsi_InitiatorISID, 6);
    628      1.1   agc 	isid->TSIH = conn->session->TSIH;
    629      1.1   agc 
    630      1.1   agc 	pdu->p.login_req.CID = htons(conn->id);
    631      1.1   agc 	pdu->p.login_req.CmdSN = htonl(conn->session->CmdSN);
    632      1.1   agc }
    633      1.1   agc 
    634      1.1   agc 
    635      1.1   agc /*
    636      1.1   agc  * negotiate_login:
    637      1.1   agc  *    Control login negotiation.
    638      1.1   agc  *
    639      1.1   agc  *    Parameter:
    640      1.1   agc  *          conn     The connection
    641      1.1   agc  *          rx_pdu   The received login response PDU
    642      1.1   agc  *          tx_ccb   The originally sent login CCB
    643      1.1   agc  */
    644      1.1   agc 
    645      1.1   agc void
    646      1.1   agc negotiate_login(connection_t *conn, pdu_t *rx_pdu, ccb_t *tx_ccb)
    647      1.1   agc {
    648      1.1   agc 	int rc;
    649      1.1   agc 	bool next = TRUE;
    650      1.1   agc 	pdu_t *tx_pdu;
    651      1.1   agc 	uint8_t c_phase;
    652      1.1   agc 
    653      1.1   agc 	if (rx_pdu->pdu.Flags & FLAG_TRANSIT)
    654      1.1   agc 		c_phase = rx_pdu->pdu.Flags & SG_MASK;
    655      1.1   agc 	else
    656      1.1   agc 		c_phase = (rx_pdu->pdu.Flags >> CSG_SHIFT) & SG_MASK;
    657      1.1   agc 
    658      1.1   agc 	DEB(99, ("NegotiateLogin: Flags=%x Phase=%x\n",
    659      1.1   agc 			 rx_pdu->pdu.Flags, c_phase));
    660      1.1   agc 
    661      1.1   agc 	if (c_phase == SG_FULL_FEATURE_PHASE) {
    662      1.1   agc 		session_t *sess = conn->session;
    663      1.1   agc 
    664      1.1   agc 		if (!sess->TSIH)
    665      1.1   agc 			sess->TSIH = ((login_isid_t *) &rx_pdu->pdu.LUN)->TSIH;
    666      1.1   agc 
    667      1.1   agc 		if (rx_pdu->temp_data != NULL)
    668      1.1   agc 			assemble_negotiation_parameters(conn, tx_ccb, rx_pdu, NULL);
    669      1.1   agc 
    670      1.1   agc 		/* negotiated values are now valid */
    671      1.1   agc 		set_negotiated_parameters(tx_ccb);
    672      1.1   agc 
    673      1.1   agc 		DEBC(conn, 5, ("Login Successful!\n"));
    674      1.1   agc 		wake_ccb(tx_ccb);
    675      1.1   agc 		return;
    676      1.1   agc 	}
    677      1.1   agc 
    678      1.1   agc 	tx_pdu = get_pdu(conn);
    679      1.1   agc 	if (tx_pdu == NULL)
    680      1.1   agc 		return;
    681      1.1   agc 
    682      1.1   agc 	tx_pdu->pdu.Flags = c_phase << CSG_SHIFT;
    683      1.1   agc 
    684      1.1   agc 	switch (c_phase) {
    685      1.1   agc 	case SG_SECURITY_NEGOTIATION:
    686      1.1   agc 		rc = assemble_security_parameters(conn, tx_ccb, rx_pdu, tx_pdu);
    687      1.1   agc 		if (rc < 0)
    688      1.1   agc 			next = FALSE;
    689      1.1   agc 		break;
    690      1.1   agc 
    691      1.1   agc 	case SG_LOGIN_OPERATIONAL_NEGOTIATION:
    692      1.1   agc 		rc = assemble_negotiation_parameters(conn, tx_ccb, rx_pdu, tx_pdu);
    693      1.1   agc 		break;
    694      1.1   agc 
    695      1.1   agc 	default:
    696      1.1   agc 		DEBOUT(("Invalid phase %x in negotiate_login\n", c_phase));
    697      1.1   agc 		rc = ISCSI_STATUS_TARGET_ERROR;
    698      1.1   agc 		break;
    699      1.1   agc 	}
    700      1.1   agc 
    701      1.1   agc 	if (rc > 0) {
    702      1.1   agc 		tx_ccb->status = rc;
    703      1.1   agc 		wake_ccb(tx_ccb);
    704      1.1   agc 		free_pdu(tx_pdu);
    705      1.1   agc 	} else {
    706      1.1   agc 		init_login_pdu(conn, tx_pdu, next);
    707      1.1   agc 		setup_tx_uio(tx_pdu, tx_pdu->temp_data_len, tx_pdu->temp_data, FALSE);
    708      1.1   agc 		send_pdu(tx_ccb, tx_pdu, CCBDISP_NOWAIT, PDUDISP_FREE);
    709      1.1   agc 	}
    710      1.1   agc }
    711      1.1   agc 
    712      1.1   agc 
    713      1.1   agc /*
    714      1.1   agc  * init_text_pdu:
    715      1.1   agc  *    Initialize the text PDU.
    716      1.1   agc  *
    717      1.1   agc  *    Parameter:
    718      1.1   agc  *          conn     The connection
    719      1.1   agc  *          ppdu     The transmit PDU
    720      1.1   agc  *          rx_pdu   The received PDU if this is an unsolicited negotiation
    721      1.1   agc  */
    722      1.1   agc 
    723      1.1   agc STATIC void
    724      1.1   agc init_text_pdu(connection_t *conn, pdu_t *ppdu, pdu_t *rx_pdu)
    725      1.1   agc {
    726      1.1   agc 	pdu_header_t *pdu = &ppdu->pdu;
    727      1.1   agc 
    728      1.1   agc 	pdu->Opcode = IOP_Text_Request | OP_IMMEDIATE;
    729      1.1   agc 	pdu->Flags = FLAG_FINAL;
    730      1.1   agc 
    731      1.1   agc 	if (rx_pdu != NULL) {
    732      1.1   agc 		pdu->p.text_req.TargetTransferTag =
    733      1.1   agc 			rx_pdu->pdu.p.text_rsp.TargetTransferTag;
    734      1.1   agc 		pdu->LUN = rx_pdu->pdu.LUN;
    735      1.1   agc 	} else
    736      1.1   agc 		pdu->p.text_req.TargetTransferTag = 0xffffffff;
    737      1.1   agc 
    738      1.1   agc 	pdu->p.text_req.CmdSN = htonl(conn->session->CmdSN);
    739      1.1   agc }
    740      1.1   agc 
    741      1.1   agc 
    742      1.1   agc /*
    743      1.1   agc  * acknowledge_text:
    744      1.1   agc  *    Acknowledge a continued login or text response.
    745      1.1   agc  *
    746      1.1   agc  *    Parameter:
    747      1.1   agc  *          conn     The connection
    748      1.1   agc  *          rx_pdu   The received login/text response PDU
    749      1.1   agc  *          tx_ccb   The originally sent login/text request CCB
    750      1.1   agc  */
    751      1.1   agc 
    752      1.1   agc void
    753      1.1   agc acknowledge_text(connection_t *conn, pdu_t *rx_pdu, ccb_t *tx_ccb)
    754      1.1   agc {
    755      1.1   agc 	pdu_t *tx_pdu;
    756      1.1   agc 
    757      1.1   agc 	tx_pdu = get_pdu(conn);
    758      1.1   agc 	if (tx_pdu == NULL)
    759      1.1   agc 		return;
    760      1.1   agc 
    761      1.1   agc 	if (rx_pdu != NULL &&
    762      1.1   agc 		(rx_pdu->pdu.Opcode & OPCODE_MASK) == IOP_Login_Request)
    763      1.1   agc 		init_login_pdu(conn, tx_pdu, FALSE);
    764      1.1   agc 	else
    765      1.1   agc 		init_text_pdu(conn, tx_pdu, rx_pdu);
    766      1.1   agc 
    767      1.1   agc 	setup_tx_uio(tx_pdu, 0, NULL, FALSE);
    768      1.1   agc 	send_pdu(tx_ccb, tx_pdu, CCBDISP_NOWAIT, PDUDISP_FREE);
    769      1.1   agc }
    770      1.1   agc 
    771      1.1   agc 
    772      1.1   agc /*
    773      1.1   agc  * start_text_negotiation:
    774      1.1   agc  *    Handle target request to negotiate (via asynch event)
    775      1.1   agc  *
    776      1.1   agc  *    Parameter:
    777      1.1   agc  *          conn     The connection
    778      1.1   agc  */
    779      1.1   agc 
    780      1.1   agc void
    781      1.1   agc start_text_negotiation(connection_t *conn)
    782      1.1   agc {
    783      1.1   agc 	pdu_t *pdu;
    784      1.1   agc 	ccb_t *ccb;
    785      1.1   agc 
    786      1.1   agc 	ccb = get_ccb(conn, TRUE);
    787  1.1.2.1  yamt 	if (ccb == NULL)
    788  1.1.2.1  yamt 		return;
    789      1.1   agc 	pdu = get_pdu(conn);
    790  1.1.2.1  yamt 	if (pdu == NULL) {
    791  1.1.2.1  yamt 		free_ccb(ccb);
    792      1.1   agc 		return;
    793  1.1.2.1  yamt 	}
    794      1.1   agc 
    795      1.1   agc 	if (init_text_parameters(conn, ccb)) {
    796      1.1   agc 		free_ccb(ccb);
    797      1.1   agc 		free_pdu(pdu);
    798      1.1   agc 		return;
    799      1.1   agc 	}
    800      1.1   agc 
    801      1.1   agc 	init_text_pdu(conn, pdu, NULL);
    802      1.1   agc 	setup_tx_uio(pdu, 0, NULL, FALSE);
    803      1.1   agc 	send_pdu(ccb, pdu, CCBDISP_FREE, PDUDISP_WAIT);
    804      1.1   agc }
    805      1.1   agc 
    806      1.1   agc 
    807      1.1   agc /*
    808      1.1   agc  * negotiate_text:
    809      1.1   agc  *    Handle received text negotiation.
    810      1.1   agc  *
    811      1.1   agc  *    Parameter:
    812      1.1   agc  *          conn     The connection
    813      1.1   agc  *          rx_pdu   The received text response PDU
    814      1.1   agc  *          tx_ccb   The original CCB
    815      1.1   agc  */
    816      1.1   agc 
    817      1.1   agc void
    818      1.1   agc negotiate_text(connection_t *conn, pdu_t *rx_pdu, ccb_t *tx_ccb)
    819      1.1   agc {
    820      1.1   agc 	int rc;
    821      1.1   agc 	pdu_t *tx_pdu;
    822      1.1   agc 
    823      1.1   agc 	if (tx_ccb->flags & CCBF_SENDTARGET) {
    824      1.1   agc 		if (!(rx_pdu->pdu.Flags & FLAG_FINAL)) {
    825      1.1   agc 			handle_connection_error(conn, ISCSI_STATUS_PROTOCOL_ERROR,
    826      1.1   agc 									LOGOUT_CONNECTION);
    827      1.1   agc 			return;
    828      1.1   agc 		}
    829      1.1   agc 		/* transfer ownership of text to CCB */
    830      1.1   agc 		tx_ccb->text_data = rx_pdu->temp_data;
    831      1.1   agc 		tx_ccb->text_len = rx_pdu->temp_data_len;
    832      1.1   agc 		rx_pdu->temp_data = NULL;
    833      1.1   agc 		wake_ccb(tx_ccb);
    834      1.1   agc 	} else {
    835      1.1   agc 		if (!(rx_pdu->pdu.Flags & FLAG_FINAL))
    836      1.1   agc 			tx_pdu = get_pdu(conn);
    837      1.1   agc 		else
    838      1.1   agc 			tx_pdu = NULL;
    839      1.1   agc 
    840      1.1   agc 		rc = assemble_negotiation_parameters(conn, tx_ccb, rx_pdu, tx_pdu);
    841      1.1   agc 		if (rc) {
    842      1.1   agc 			if (tx_pdu != NULL)
    843      1.1   agc 				free_pdu(tx_pdu);
    844      1.1   agc 
    845      1.1   agc 			handle_connection_error(conn, rc, LOGOUT_CONNECTION);
    846      1.1   agc 		} else if (tx_pdu != NULL) {
    847      1.1   agc 			init_text_pdu(conn, tx_pdu, rx_pdu);
    848      1.1   agc 			setup_tx_uio(tx_pdu, tx_pdu->temp_data_len, tx_pdu->temp_data,
    849      1.1   agc 						 FALSE);
    850      1.1   agc 			send_pdu(tx_ccb, tx_pdu, CCBDISP_NOWAIT, PDUDISP_FREE);
    851      1.1   agc 		} else {
    852      1.1   agc 			set_negotiated_parameters(tx_ccb);
    853      1.1   agc 			wake_ccb(tx_ccb);
    854      1.1   agc 		}
    855      1.1   agc 	}
    856      1.1   agc }
    857      1.1   agc 
    858      1.1   agc 
    859      1.1   agc /*
    860      1.1   agc  * send_send_targets:
    861      1.1   agc  *    Send out a SendTargets text request.
    862      1.1   agc  *    The result is stored in the fields in the session structure.
    863      1.1   agc  *
    864      1.1   agc  *    Parameter:
    865      1.1   agc  *          session  The session
    866      1.1   agc  *          key      The text key to use
    867      1.1   agc  *
    868      1.1   agc  *    Returns:    0 on success, else an error code.
    869      1.1   agc  */
    870      1.1   agc 
    871      1.1   agc int
    872      1.1   agc send_send_targets(session_t *session, uint8_t *key)
    873      1.1   agc {
    874      1.1   agc 	ccb_t *ccb;
    875      1.1   agc 	pdu_t *pdu;
    876      1.1   agc 	int rc = 0;
    877      1.1   agc 	connection_t *conn;
    878      1.1   agc 
    879      1.1   agc 	DEB(9, ("Send_send_targets\n"));
    880      1.1   agc 
    881      1.1   agc 	conn = assign_connection(session, TRUE);
    882      1.1   agc 	if (conn == NULL || conn->terminating || conn->state != ST_FULL_FEATURE)
    883      1.1   agc 		return (conn != NULL && conn->terminating) ? conn->terminating
    884      1.1   agc 			: ISCSI_STATUS_CONNECTION_FAILED;
    885      1.1   agc 
    886      1.1   agc 	ccb = get_ccb(conn, TRUE);
    887  1.1.2.1  yamt 	if (ccb == NULL)
    888  1.1.2.1  yamt 		return conn->terminating;
    889      1.1   agc 	pdu = get_pdu(conn);
    890  1.1.2.1  yamt 	if (pdu == NULL) {
    891  1.1.2.1  yamt 		free_ccb(ccb);
    892      1.1   agc 		return conn->terminating;
    893  1.1.2.1  yamt 	}
    894      1.1   agc 
    895      1.1   agc 	ccb->flags |= CCBF_SENDTARGET;
    896      1.1   agc 
    897      1.1   agc 	if ((rc = assemble_send_targets(pdu, key)) != 0) {
    898      1.1   agc 		free_ccb(ccb);
    899      1.1   agc 		free_pdu(pdu);
    900      1.1   agc 		return rc;
    901      1.1   agc 	}
    902      1.1   agc 
    903      1.1   agc 	init_text_pdu(conn, pdu, NULL);
    904      1.1   agc 
    905      1.1   agc 	setup_tx_uio(pdu, pdu->temp_data_len, pdu->temp_data, FALSE);
    906      1.1   agc 	send_pdu(ccb, pdu, CCBDISP_WAIT, PDUDISP_WAIT);
    907      1.1   agc 
    908      1.1   agc 	rc = ccb->status;
    909      1.1   agc 	if (!rc) {
    910      1.1   agc 		/* transfer ownership of data */
    911      1.1   agc 		session->target_list = ccb->text_data;
    912      1.1   agc 		session->target_list_len = ccb->text_len;
    913      1.1   agc 		ccb->text_data = NULL;
    914      1.1   agc 	}
    915      1.1   agc 	free_ccb(ccb);
    916      1.1   agc 	return rc;
    917      1.1   agc }
    918      1.1   agc 
    919      1.1   agc 
    920      1.1   agc /*
    921      1.1   agc  * send_nop_out:
    922      1.1   agc  *    Send nop out request.
    923      1.1   agc  *
    924      1.1   agc  *    Parameter:
    925      1.1   agc  *          conn     The connection
    926      1.1   agc  *          rx_pdu   The received Nop-In PDU
    927      1.1   agc  *
    928      1.1   agc  *    Returns:    0 on success, else an error code.
    929      1.1   agc  */
    930      1.1   agc 
    931      1.1   agc int
    932      1.1   agc send_nop_out(connection_t *conn, pdu_t *rx_pdu)
    933      1.1   agc {
    934      1.1   agc 	ccb_t *ccb;
    935      1.1   agc 	pdu_t *ppdu;
    936      1.1   agc 	pdu_header_t *pdu;
    937      1.1   agc 
    938      1.1   agc 	DEBC(conn, 10, ("Send NOP_Out rx_pdu=%p\n", rx_pdu));
    939      1.1   agc 
    940      1.1   agc 	if (rx_pdu != NULL) {
    941      1.1   agc 		ccb = NULL;
    942      1.1   agc 		ppdu = get_pdu(conn);
    943      1.1   agc 		if (ppdu == NULL)
    944      1.1   agc 			return 1;
    945      1.1   agc 	} else {
    946      1.1   agc 		ccb = get_ccb(conn, FALSE);
    947      1.1   agc 		if (ccb == NULL) {
    948      1.1   agc 			DEBOUT(("Can't get CCB in send_nop_out\n"));
    949      1.1   agc 			return 1;
    950      1.1   agc 		}
    951      1.1   agc 		ppdu = get_pdu_c(conn, FALSE);
    952      1.1   agc 		if (ppdu == NULL) {
    953      1.1   agc 			free_ccb(ccb);
    954      1.1   agc 			DEBOUT(("Can't get PDU in send_nop_out\n"));
    955      1.1   agc 			return 1;
    956      1.1   agc 		}
    957      1.1   agc 	}
    958      1.1   agc 
    959      1.1   agc 	pdu = &ppdu->pdu;
    960      1.1   agc 	pdu->Flags = FLAG_FINAL;
    961      1.1   agc 	pdu->Opcode = IOP_NOP_Out | OP_IMMEDIATE;
    962      1.1   agc 
    963      1.1   agc 	if (rx_pdu != NULL) {
    964      1.1   agc 		pdu->p.nop_out.TargetTransferTag =
    965      1.1   agc 			rx_pdu->pdu.p.nop_in.TargetTransferTag;
    966      1.1   agc 		pdu->InitiatorTaskTag = rx_pdu->pdu.InitiatorTaskTag;
    967      1.1   agc 		pdu->p.nop_out.CmdSN = htonl(conn->session->CmdSN);
    968      1.1   agc 		pdu->LUN = rx_pdu->pdu.LUN;
    969      1.1   agc 	} else {
    970      1.1   agc 		pdu->p.nop_out.TargetTransferTag = 0xffffffff;
    971      1.1   agc 		ccb->CmdSN = ccb->session->CmdSN;
    972      1.1   agc 		pdu->p.nop_out.CmdSN = htonl(ccb->CmdSN);
    973      1.1   agc 	}
    974      1.1   agc 
    975      1.1   agc 	setup_tx_uio(ppdu, 0, NULL, FALSE);
    976      1.1   agc 	send_pdu(ccb, ppdu, (rx_pdu != NULL) ? CCBDISP_NOWAIT : CCBDISP_FREE,
    977      1.1   agc 			 PDUDISP_FREE);
    978      1.1   agc 	return 0;
    979      1.1   agc }
    980      1.1   agc 
    981      1.1   agc 
    982      1.1   agc /*
    983      1.1   agc  * snack_missing:
    984      1.1   agc  *    Send SNACK request for missing data.
    985      1.1   agc  *
    986      1.1   agc  *    Parameter:
    987      1.1   agc  *          conn     The connection
    988      1.1   agc  *          ccb      The task's CCB (for Data NAK only)
    989      1.1   agc  *          type     The SNACK type
    990      1.1   agc  *          BegRun   The BegRun field
    991      1.1   agc  *          RunLength   The RunLength field
    992      1.1   agc  */
    993      1.1   agc 
    994      1.1   agc void
    995      1.1   agc snack_missing(connection_t *conn, ccb_t *ccb, uint8_t type,
    996      1.1   agc 			  uint32_t BegRun, uint32_t RunLength)
    997      1.1   agc {
    998      1.1   agc 	pdu_t *ppdu;
    999      1.1   agc 	pdu_header_t *pdu;
   1000      1.1   agc 
   1001      1.1   agc 	PDEBC(conn, 1, ("SNACK Missing type = %d, BegRun %d RunLength %d\n",
   1002      1.1   agc 			 type, BegRun, RunLength));
   1003      1.1   agc 
   1004      1.1   agc 	ppdu = get_pdu(conn);
   1005      1.1   agc 	if (ppdu == NULL)
   1006      1.1   agc 		return;
   1007      1.1   agc 	pdu = &ppdu->pdu;
   1008      1.1   agc 	pdu->Opcode = IOP_SNACK_Request;
   1009      1.1   agc 	pdu->Flags = FLAG_FINAL | type;
   1010      1.1   agc 
   1011      1.1   agc 	pdu->InitiatorTaskTag = (type == SNACK_DATA_NAK) ? ccb->ITT : 0xffffffff;
   1012      1.1   agc 	pdu->p.snack.TargetTransferTag = 0xffffffff;
   1013      1.1   agc 	pdu->p.snack.BegRun = htonl(BegRun);
   1014      1.1   agc 	pdu->p.snack.RunLength = htonl(RunLength);
   1015      1.1   agc 
   1016      1.1   agc 	ppdu->flags = PDUF_PRIORITY;
   1017      1.1   agc 
   1018      1.1   agc 	setup_tx_uio(ppdu, 0, NULL, FALSE);
   1019      1.1   agc 	send_pdu(NULL, ppdu, CCBDISP_NOWAIT, PDUDISP_FREE);
   1020      1.1   agc }
   1021      1.1   agc 
   1022      1.1   agc 
   1023      1.1   agc /*
   1024      1.1   agc  * send_snack:
   1025      1.1   agc  *    Send SNACK request.
   1026      1.1   agc  *
   1027      1.1   agc  *    Parameter:
   1028      1.1   agc  *          conn     The connection
   1029      1.1   agc  *          rx_pdu   The received data in PDU
   1030      1.1   agc  *          tx_ccb   The original command CCB (required for Data ACK only)
   1031      1.1   agc  *          type     The SNACK type
   1032      1.1   agc  *
   1033      1.1   agc  *    Returns:    0 on success, else an error code.
   1034      1.1   agc  */
   1035      1.1   agc 
   1036      1.1   agc void
   1037      1.1   agc send_snack(connection_t *conn, pdu_t *rx_pdu, ccb_t *tx_ccb, uint8_t type)
   1038      1.1   agc {
   1039      1.1   agc 	pdu_t *ppdu;
   1040      1.1   agc 	pdu_header_t *pdu;
   1041      1.1   agc 
   1042      1.1   agc 	PDEBC(conn, 1, ("Send SNACK type = %d\n", type));
   1043      1.1   agc 
   1044      1.1   agc 	ppdu = get_pdu(conn);
   1045      1.1   agc 	if (ppdu == NULL)
   1046      1.1   agc 		return;
   1047      1.1   agc 	pdu = &ppdu->pdu;
   1048      1.1   agc 	pdu->Opcode = IOP_SNACK_Request;
   1049      1.1   agc 	pdu->Flags = FLAG_FINAL | type;
   1050      1.1   agc 
   1051      1.1   agc 	switch (type) {
   1052      1.1   agc 	case SNACK_DATA_NAK:
   1053      1.1   agc 		pdu->InitiatorTaskTag = rx_pdu->pdu.InitiatorTaskTag;
   1054      1.1   agc 		pdu->p.snack.TargetTransferTag = 0xffffffff;
   1055      1.1   agc 		pdu->p.snack.BegRun = rx_pdu->pdu.p.data_in.DataSN;
   1056      1.1   agc 		pdu->p.snack.RunLength = htonl(1);
   1057      1.1   agc 		break;
   1058      1.1   agc 
   1059      1.1   agc 	case SNACK_STATUS_NAK:
   1060      1.1   agc 		pdu->InitiatorTaskTag = 0xffffffff;
   1061      1.1   agc 		pdu->p.snack.TargetTransferTag = 0xffffffff;
   1062      1.1   agc 		pdu->p.snack.BegRun = rx_pdu->pdu.p.response.StatSN;
   1063      1.1   agc 		pdu->p.snack.RunLength = htonl(1);
   1064      1.1   agc 		break;
   1065      1.1   agc 
   1066      1.1   agc 	case SNACK_DATA_ACK:
   1067      1.1   agc 		pdu->InitiatorTaskTag = 0xffffffff;
   1068      1.1   agc 		pdu->p.snack.TargetTransferTag =
   1069      1.1   agc 			rx_pdu->pdu.p.data_in.TargetTransferTag;
   1070      1.1   agc 		pdu->p.snack.BegRun = tx_ccb->DataSN_buf.ExpSN;
   1071      1.1   agc 		pdu->p.snack.RunLength = 0;
   1072      1.1   agc 		break;
   1073      1.1   agc 
   1074      1.1   agc 	default:
   1075      1.1   agc 		DEBOUT(("Invalid type %d in send_snack\n", type));
   1076      1.1   agc 		return;
   1077      1.1   agc 	}
   1078      1.1   agc 
   1079      1.1   agc 	pdu->LUN = rx_pdu->pdu.LUN;
   1080      1.1   agc 
   1081      1.1   agc 	ppdu->flags = PDUF_PRIORITY;
   1082      1.1   agc 
   1083      1.1   agc 	setup_tx_uio(ppdu, 0, NULL, FALSE);
   1084      1.1   agc 	send_pdu(NULL, ppdu, CCBDISP_NOWAIT, PDUDISP_FREE);
   1085      1.1   agc }
   1086      1.1   agc 
   1087      1.1   agc 
   1088      1.1   agc /*
   1089      1.1   agc  * send_login:
   1090      1.1   agc  *    Send login request.
   1091      1.1   agc  *
   1092      1.1   agc  *    Parameter:
   1093      1.1   agc  *          conn     The connection
   1094      1.1   agc  *          par      The login parameters (for negotiation)
   1095      1.1   agc  *
   1096      1.1   agc  *    Returns:       0 on success, else an error code.
   1097      1.1   agc  */
   1098      1.1   agc 
   1099      1.1   agc int
   1100      1.1   agc send_login(connection_t *conn)
   1101      1.1   agc {
   1102      1.1   agc 	ccb_t *ccb;
   1103      1.1   agc 	pdu_t *pdu;
   1104      1.1   agc 	int rc;
   1105      1.1   agc 
   1106      1.1   agc 	DEBC(conn, 9, ("Send_login\n"));
   1107      1.1   agc 	ccb = get_ccb(conn, TRUE);
   1108      1.1   agc 	/* only if terminating (which couldn't possibly happen here, but...) */
   1109  1.1.2.1  yamt 	if (ccb == NULL)
   1110  1.1.2.1  yamt 		return conn->terminating;
   1111  1.1.2.1  yamt 	pdu = get_pdu(conn);
   1112  1.1.2.1  yamt 	if (pdu == NULL) {
   1113  1.1.2.1  yamt 		free_ccb(ccb);
   1114      1.1   agc 		return conn->terminating;
   1115      1.1   agc 	}
   1116  1.1.2.1  yamt 
   1117  1.1.2.1  yamt 	if ((rc = assemble_login_parameters(conn, ccb, pdu)) <= 0) {
   1118      1.1   agc 		init_login_pdu(conn, pdu, !rc);
   1119      1.1   agc 		setup_tx_uio(pdu, pdu->temp_data_len, pdu->temp_data, FALSE);
   1120      1.1   agc 		send_pdu(ccb, pdu, CCBDISP_WAIT, PDUDISP_FREE);
   1121      1.1   agc 		rc = ccb->status;
   1122      1.1   agc 	} else {
   1123      1.1   agc 		free_pdu(pdu);
   1124      1.1   agc 	}
   1125      1.1   agc 	free_ccb(ccb);
   1126      1.1   agc 	return rc;
   1127      1.1   agc }
   1128      1.1   agc 
   1129      1.1   agc 
   1130      1.1   agc /*
   1131      1.1   agc  * send_logout:
   1132      1.1   agc  *    Send logout request.
   1133      1.1   agc  *	  NOTE: This function does not wait for the logout to complete.
   1134      1.1   agc  *
   1135      1.1   agc  *    Parameter:
   1136      1.1   agc  *          conn	The connection
   1137      1.1   agc  *			refconn	The referenced connection
   1138      1.1   agc  *			reason	The reason code
   1139      1.1   agc  *			wait	Wait for completion if TRUE
   1140      1.1   agc  *
   1141      1.1   agc  *    Returns:       0 on success (logout sent), else an error code.
   1142      1.1   agc  */
   1143      1.1   agc 
   1144      1.1   agc int
   1145      1.1   agc send_logout(connection_t *conn, connection_t *refconn, int reason,
   1146      1.1   agc 			bool wait)
   1147      1.1   agc {
   1148      1.1   agc 	ccb_t *ccb;
   1149      1.1   agc 	pdu_t *ppdu;
   1150      1.1   agc 	pdu_header_t *pdu;
   1151      1.1   agc 
   1152      1.1   agc 	DEBC(conn, 5, ("Send_logout\n"));
   1153      1.1   agc 	ccb = get_ccb(conn, TRUE);
   1154      1.1   agc 	/* can only happen if terminating... */
   1155  1.1.2.1  yamt 	if (ccb == NULL)
   1156      1.1   agc 		return conn->terminating;
   1157  1.1.2.1  yamt 	ppdu = get_pdu(conn);
   1158  1.1.2.1  yamt 	if (ppdu == NULL) {
   1159  1.1.2.1  yamt 		free_ccb(ccb);
   1160  1.1.2.1  yamt 		return conn->terminating;
   1161  1.1.2.1  yamt 	}
   1162      1.1   agc 
   1163      1.1   agc 	pdu = &ppdu->pdu;
   1164      1.1   agc 	pdu->Opcode = IOP_Logout_Request | OP_IMMEDIATE;
   1165      1.1   agc 
   1166      1.1   agc 	pdu->Flags = FLAG_FINAL | reason;
   1167      1.1   agc 	ccb->CmdSN = conn->session->CmdSN;
   1168      1.1   agc 	pdu->p.logout_req.CmdSN = htonl(ccb->CmdSN);
   1169      1.1   agc 	if (reason > 0)
   1170      1.1   agc 		pdu->p.logout_req.CID = htons(refconn->id);
   1171      1.1   agc 
   1172      1.1   agc 	ccb->par = refconn;
   1173      1.1   agc 	if (refconn != conn) {
   1174      1.1   agc 		ccb->flags |= CCBF_OTHERCONN;
   1175      1.1   agc 	} else {
   1176      1.1   agc 		conn->state = ST_LOGOUT_SENT;
   1177      1.1   agc 		conn->loggedout = LOGOUT_SENT;
   1178      1.1   agc 	}
   1179      1.1   agc 
   1180      1.1   agc 	setup_tx_uio(ppdu, 0, NULL, FALSE);
   1181      1.1   agc 
   1182      1.1   agc 	send_pdu(ccb, ppdu, (wait) ? CCBDISP_WAIT : CCBDISP_FREE, PDUDISP_FREE);
   1183      1.1   agc 
   1184      1.1   agc 	if (wait) {
   1185      1.1   agc 		int rc = ccb->status;
   1186      1.1   agc 		free_ccb (ccb);
   1187      1.1   agc 		return rc;
   1188      1.1   agc 	}
   1189      1.1   agc 	return 0;
   1190      1.1   agc }
   1191      1.1   agc 
   1192      1.1   agc 
   1193      1.1   agc /*
   1194      1.1   agc  * send_task_management:
   1195      1.1   agc  *    Send task management request.
   1196      1.1   agc  *
   1197      1.1   agc  *    Parameter:
   1198      1.1   agc  *          conn     The connection
   1199      1.1   agc  *          ref_ccb  The referenced command (NULL if none)
   1200      1.1   agc  *          xs       The scsipi command structure (NULL if not a scsipi request)
   1201      1.1   agc  *          function The function code
   1202      1.1   agc  *
   1203      1.1   agc  *    Returns:       0 on success, else an error code.
   1204      1.1   agc  */
   1205      1.1   agc 
   1206      1.1   agc int
   1207      1.1   agc send_task_management(connection_t *conn, ccb_t *ref_ccb, struct scsipi_xfer *xs,
   1208      1.1   agc 					 int function)
   1209      1.1   agc {
   1210      1.1   agc 	ccb_t *ccb;
   1211      1.1   agc 	pdu_t *ppdu;
   1212      1.1   agc 	pdu_header_t *pdu;
   1213      1.1   agc 
   1214      1.1   agc 	DEBC(conn, 5, ("Send_task_management, ref_ccb=%p, func = %d\n",
   1215      1.1   agc 			ref_ccb, function));
   1216      1.1   agc 
   1217      1.1   agc 	if (function == TASK_REASSIGN && conn->session->ErrorRecoveryLevel < 2)
   1218      1.1   agc 		return ISCSI_STATUS_CANT_REASSIGN;
   1219      1.1   agc 
   1220      1.1   agc 	ccb = get_ccb(conn, xs == NULL);
   1221      1.1   agc 	/* can only happen if terminating... */
   1222  1.1.2.1  yamt 	if (ccb == NULL)
   1223      1.1   agc 		return conn->terminating;
   1224  1.1.2.1  yamt 	ppdu = get_pdu(conn);
   1225  1.1.2.1  yamt 	if (ppdu == NULL) {
   1226  1.1.2.1  yamt 		free_ccb(ccb);
   1227  1.1.2.1  yamt 		return conn->terminating;
   1228  1.1.2.1  yamt 	}
   1229      1.1   agc 
   1230      1.1   agc 	ccb->xs = xs;
   1231      1.1   agc 
   1232      1.1   agc 	pdu = &ppdu->pdu;
   1233      1.1   agc 	pdu->Opcode = IOP_SCSI_Task_Management | OP_IMMEDIATE;
   1234      1.1   agc 	pdu->Flags = FLAG_FINAL | function;
   1235      1.1   agc 
   1236      1.1   agc 	ccb->CmdSN = conn->session->CmdSN;
   1237      1.1   agc 	pdu->p.task_req.CmdSN = htonl(ccb->CmdSN);
   1238      1.1   agc 
   1239      1.1   agc 	if (ref_ccb != NULL) {
   1240      1.1   agc 		pdu->p.task_req.ReferencedTaskTag = ref_ccb->ITT;
   1241      1.1   agc 		pdu->p.task_req.RefCmdSN = htonl(ref_ccb->CmdSN);
   1242      1.1   agc 		pdu->p.task_req.ExpDataSN = htonl(ref_ccb->DataSN_buf.ExpSN);
   1243      1.1   agc 	} else
   1244      1.1   agc 		pdu->p.task_req.ReferencedTaskTag = 0xffffffff;
   1245      1.1   agc 
   1246      1.1   agc 	ppdu->flags |= PDUF_PRIORITY;
   1247      1.1   agc 
   1248      1.1   agc 	setup_tx_uio(ppdu, 0, NULL, FALSE);
   1249      1.1   agc 	send_pdu(ccb, ppdu, (xs) ? CCBDISP_SCSIPI : CCBDISP_WAIT, PDUDISP_FREE);
   1250      1.1   agc 
   1251      1.1   agc 	if (xs == NULL) {
   1252      1.1   agc 		int rc = ccb->status;
   1253      1.1   agc 		free_ccb(ccb);
   1254      1.1   agc 		return rc;
   1255      1.1   agc 	}
   1256      1.1   agc 	return 0;
   1257      1.1   agc }
   1258      1.1   agc 
   1259      1.1   agc 
   1260      1.1   agc /*
   1261      1.1   agc  * send_data_out:
   1262      1.1   agc  *    Send data to target in response to an R2T or as unsolicited data.
   1263      1.1   agc  *
   1264      1.1   agc  *    Parameter:
   1265      1.1   agc  *          conn     The connection
   1266      1.1   agc  *          rx_pdu   The received R2T PDU (NULL if unsolicited)
   1267      1.1   agc  *          tx_ccb   The originally sent command CCB
   1268      1.1   agc  *          waitok   Whether it's OK to wait for an available PDU or not
   1269      1.1   agc  */
   1270      1.1   agc 
   1271      1.1   agc int
   1272      1.1   agc send_data_out(connection_t *conn, pdu_t *rx_pdu, ccb_t *tx_ccb,
   1273      1.1   agc 			  ccb_disp_t disp, bool waitok)
   1274      1.1   agc {
   1275      1.1   agc 	pdu_header_t *pdu;
   1276      1.1   agc 	uint32_t totlen, len, offs, sn;
   1277      1.1   agc 	pdu_t *tx_pdu;
   1278      1.1   agc 
   1279      1.1   agc 	assert(conn->max_transfer != 0);
   1280      1.1   agc 
   1281      1.1   agc 	if (rx_pdu) {
   1282      1.1   agc 		offs = ntohl(rx_pdu->pdu.p.r2t.BufferOffset);
   1283      1.1   agc 		totlen = ntohl(rx_pdu->pdu.p.r2t.DesiredDataTransferLength);
   1284      1.1   agc 	} else {
   1285      1.1   agc 		offs = conn->max_firstimmed;
   1286      1.1   agc 		totlen = min(conn->max_firstdata - offs, tx_ccb->data_len - offs);
   1287      1.1   agc 	}
   1288      1.1   agc 	sn = 0;
   1289      1.1   agc 
   1290      1.1   agc 	while (totlen) {
   1291      1.1   agc 		len = min(totlen, conn->max_transfer);
   1292      1.1   agc 
   1293      1.1   agc 		tx_pdu = get_pdu_c(conn, waitok);
   1294      1.1   agc 		if (tx_pdu == NULL) {
   1295      1.1   agc 			DEBOUT(("No PDU in send_data_out\n"));
   1296      1.1   agc 
   1297      1.1   agc 			tx_ccb->status = ISCSI_STATUS_NO_RESOURCES;
   1298      1.1   agc 			handle_connection_error(conn, ISCSI_STATUS_NO_RESOURCES, NO_LOGOUT);
   1299      1.1   agc 
   1300      1.1   agc 			return ISCSI_STATUS_NO_RESOURCES;
   1301      1.1   agc 		}
   1302      1.1   agc 
   1303      1.1   agc 		totlen -= len;
   1304      1.1   agc 		pdu = &tx_pdu->pdu;
   1305      1.1   agc 		pdu->Opcode = IOP_SCSI_Data_out;
   1306      1.1   agc 		if (!totlen)
   1307      1.1   agc 			pdu->Flags = FLAG_FINAL;
   1308      1.1   agc 
   1309      1.1   agc 		if (rx_pdu != NULL)
   1310      1.1   agc 			pdu->p.data_out.TargetTransferTag =
   1311      1.1   agc 				rx_pdu->pdu.p.r2t.TargetTransferTag;
   1312      1.1   agc 		else
   1313      1.1   agc 			pdu->p.data_out.TargetTransferTag = 0xffffffff;
   1314      1.1   agc 		pdu->p.data_out.BufferOffset = htonl(offs);
   1315      1.1   agc 		pdu->p.data_out.DataSN = htonl(sn);
   1316      1.1   agc 
   1317      1.1   agc 		PERF_PDUSET(tx_pdu, tx_ccb, PERF_BEGIN_PDUWRITEDATA);
   1318      1.1   agc 
   1319      1.1   agc 		DEBC(conn, 10, ("Send DataOut: DataSN %d, len %d offs %x totlen %d\n",
   1320      1.1   agc 				sn, len, offs, totlen));
   1321      1.1   agc 
   1322      1.1   agc 		setup_tx_uio(tx_pdu, len, tx_ccb->data_ptr + offs, FALSE);
   1323      1.1   agc 
   1324      1.1   agc 		send_pdu(tx_ccb, tx_pdu, (totlen) ? CCBDISP_NOWAIT : disp, PDUDISP_FREE);
   1325      1.1   agc 
   1326      1.1   agc 		sn++;
   1327      1.1   agc 		offs += len;
   1328      1.1   agc 	}
   1329      1.1   agc 	return 0;
   1330      1.1   agc }
   1331      1.1   agc 
   1332      1.1   agc 
   1333      1.1   agc /*
   1334      1.1   agc  * send_command:
   1335      1.1   agc  *    Send a SCSI command request.
   1336      1.1   agc  *
   1337      1.1   agc  *    Parameter:
   1338      1.1   agc  *          CCB      The CCB
   1339      1.1   agc  *          disp     The CCB disposition
   1340      1.1   agc  */
   1341      1.1   agc 
   1342      1.1   agc void
   1343      1.1   agc send_command(ccb_t *ccb, ccb_disp_t disp, bool waitok, bool immed)
   1344      1.1   agc {
   1345      1.1   agc 	uint32_t totlen, len;
   1346      1.1   agc 	connection_t *conn = ccb->connection;
   1347      1.1   agc 	session_t *sess = ccb->session;
   1348      1.1   agc 	pdu_t *ppdu;
   1349      1.1   agc 	pdu_header_t *pdu;
   1350      1.1   agc 	int s;
   1351      1.1   agc 
   1352      1.1   agc 	PERF_BEGIN(ccb, !waitok);
   1353      1.1   agc 
   1354      1.1   agc 	if (!waitok) {
   1355      1.1   agc 		s = splbio();
   1356      1.1   agc 		if (/*CONSTCOND*/ISCSI_TROTTLING_ENABLED &&
   1357      1.1   agc 		    /*CONSTCOND*/ISCSI_SERVER_TRUSTED &&
   1358      1.1   agc 		    !sn_a_le_b(sess->CmdSN, sess->MaxCmdSN)) {
   1359  1.1.2.1  yamt 			ccb->disp = disp;
   1360      1.1   agc 			TAILQ_INSERT_TAIL(&sess->ccbs_throttled, ccb, chain);
   1361      1.1   agc 			splx(s);
   1362      1.1   agc 			PDEBOUT(("Throttling S - CmdSN = %d, MaxCmdSN = %d\n",
   1363      1.1   agc 					 sess->CmdSN, sess->MaxCmdSN));
   1364      1.1   agc 			return;
   1365      1.1   agc 		}
   1366      1.1   agc 		splx(s);
   1367      1.1   agc 		ppdu = get_pdu_c(conn, FALSE);
   1368      1.1   agc 		if (ppdu == NULL) {
   1369      1.1   agc 			ccb->status = ISCSI_STATUS_NO_RESOURCES;
   1370      1.1   agc 			iscsi_done(ccb);
   1371      1.1   agc 			return;
   1372      1.1   agc 		}
   1373      1.1   agc 	} else {
   1374      1.1   agc 		s = splbio();
   1375      1.1   agc 		while (/*CONSTCOND*/ISCSI_TROTTLING_ENABLED &&
   1376      1.1   agc 		       /*CONSTCOND*/ISCSI_SERVER_TRUSTED &&
   1377      1.1   agc 		       !sn_a_le_b(sess->CmdSN, sess->MaxCmdSN)) {
   1378  1.1.2.1  yamt 			ccb->disp = disp;
   1379      1.1   agc 			ccb->flags |= CCBF_WAITING;
   1380      1.1   agc 			TAILQ_INSERT_TAIL(&sess->ccbs_throttled, ccb, chain);
   1381      1.1   agc 			PDEBOUT(("Throttling W - CmdSN = %d, MaxCmdSN = %d\n",
   1382      1.1   agc 					 sess->CmdSN, sess->MaxCmdSN));
   1383      1.1   agc 			tsleep(ccb, PWAIT, "waitMaxCmd", 0);
   1384      1.1   agc 			splbio();
   1385      1.1   agc 		}
   1386      1.1   agc 		splx(s);
   1387      1.1   agc 		ppdu = get_pdu(conn);
   1388      1.1   agc 	}
   1389      1.1   agc 
   1390      1.1   agc 	pdu = &ppdu->pdu;
   1391      1.1   agc 	pdu->LUN = htonq(ccb->lun);
   1392      1.1   agc 	memcpy(pdu->p.command.SCSI_CDB, ccb->cmd, ccb->cmdlen);
   1393      1.1   agc 	totlen = len = ccb->data_len;
   1394      1.1   agc 	pdu->Opcode = IOP_SCSI_Command;
   1395      1.1   agc 	if (immed)
   1396      1.1   agc 		pdu->Opcode |= OP_IMMEDIATE;
   1397      1.1   agc 	pdu->p.command.ExpectedDataTransferLength = htonl(totlen);
   1398      1.1   agc 
   1399      1.1   agc 
   1400      1.1   agc 	if (totlen) {
   1401      1.1   agc 		if (ccb->data_in) {
   1402      1.1   agc 			pdu->Flags = FLAG_READ;
   1403      1.1   agc 			totlen = 0;
   1404      1.1   agc 		} else {
   1405      1.1   agc 			pdu->Flags = FLAG_WRITE;
   1406  1.1.2.1  yamt 			/* immediate data we can send */
   1407      1.1   agc 			len = min(totlen, conn->max_firstimmed);
   1408  1.1.2.1  yamt 
   1409  1.1.2.1  yamt 			/* can we send more unsolicited data ? */
   1410  1.1.2.1  yamt 			totlen = conn->max_firstdata ? totlen - len : 0;
   1411      1.1   agc 		}
   1412      1.1   agc 	}
   1413      1.1   agc 
   1414      1.1   agc 	if (!totlen)
   1415      1.1   agc 		pdu->Flags |= FLAG_FINAL;
   1416      1.1   agc 
   1417      1.1   agc 	if (ccb->data_in)
   1418      1.1   agc 		init_sernum(&ccb->DataSN_buf);
   1419      1.1   agc 
   1420      1.1   agc 	ccb->sense_len_got = 0;
   1421      1.1   agc 	ccb->xfer_len = 0;
   1422      1.1   agc 	ccb->residual = 0;
   1423      1.1   agc 	ccb->flags |= CCBF_REASSIGN;
   1424      1.1   agc 
   1425      1.1   agc 	s = splbio();
   1426      1.1   agc 	ccb->CmdSN = sess->CmdSN;
   1427      1.1   agc 	if (!immed)
   1428      1.1   agc 		sess->CmdSN++;
   1429      1.1   agc 	splx(s);
   1430      1.1   agc 
   1431      1.1   agc 	pdu->p.command.CmdSN = htonl(ccb->CmdSN);
   1432      1.1   agc 
   1433      1.1   agc 	DEBC(conn, 10, ("Send Command: CmdSN %d, data_in %d, len %d, totlen %d\n",
   1434      1.1   agc 			ccb->CmdSN, ccb->data_in, len, totlen));
   1435      1.1   agc 
   1436      1.1   agc 	PERF_PDUSET(ppdu, ccb, PERF_BEGIN_PDUWRITECMD);
   1437      1.1   agc 
   1438      1.1   agc 	setup_tx_uio(ppdu, len, ccb->data_ptr, ccb->data_in);
   1439      1.1   agc 
   1440      1.1   agc 	send_pdu(ccb, ppdu, (totlen) ? CCBDISP_DEFER : disp, PDUDISP_WAIT);
   1441      1.1   agc 
   1442      1.1   agc 	if (totlen)
   1443      1.1   agc 		send_data_out(conn, NULL, ccb, disp, waitok);
   1444      1.1   agc }
   1445      1.1   agc 
   1446      1.1   agc 
   1447      1.1   agc /*
   1448      1.1   agc  * send_run_xfer:
   1449      1.1   agc  *    Handle a SCSI command transfer request from scsipi.
   1450      1.1   agc  *
   1451      1.1   agc  *    Parameter:
   1452      1.1   agc  *          session  The session
   1453      1.1   agc  *          xs       The transfer parameters
   1454      1.1   agc  */
   1455      1.1   agc 
   1456      1.1   agc void
   1457      1.1   agc send_run_xfer(session_t *session, struct scsipi_xfer *xs)
   1458      1.1   agc {
   1459      1.1   agc 	ccb_t *ccb;
   1460      1.1   agc 	connection_t *conn;
   1461      1.1   agc 	bool waitok;
   1462      1.1   agc 
   1463      1.1   agc 	waitok = !(xs->xs_control & XS_CTL_NOSLEEP);
   1464      1.1   agc 
   1465      1.1   agc 	DEB(10, ("RunXfer: flags=%x, data=%p, datalen=%d, resid=%d, cmdlen=%d, "
   1466      1.1   agc 			"waitok=%d\n", xs->xs_control, xs->data, xs->datalen,
   1467      1.1   agc 			xs->resid, xs->cmdlen, waitok));
   1468      1.1   agc 
   1469      1.1   agc 	conn = assign_connection(session, waitok);
   1470      1.1   agc 
   1471      1.1   agc 	if (conn == NULL || conn->terminating || conn->state != ST_FULL_FEATURE) {
   1472  1.1.2.1  yamt 		xs->error = XS_SELTIMEOUT;
   1473      1.1   agc 		DEBC(conn, 10, ("run_xfer on dead connection\n"));
   1474      1.1   agc 		scsipi_done(xs);
   1475      1.1   agc 		return;
   1476      1.1   agc 	}
   1477      1.1   agc 
   1478      1.1   agc 	if (xs->xs_control & XS_CTL_RESET) {
   1479      1.1   agc 		if (send_task_management(conn, NULL, xs, TARGET_WARM_RESET)) {
   1480      1.1   agc 			xs->error = XS_SELTIMEOUT;
   1481      1.1   agc 			scsipi_done(xs);
   1482      1.1   agc 		}
   1483      1.1   agc 		return;
   1484      1.1   agc 	}
   1485      1.1   agc 
   1486      1.1   agc 	ccb = get_ccb(conn, waitok);
   1487      1.1   agc 	if (ccb == NULL) {
   1488      1.1   agc 		xs->error = XS_RESOURCE_SHORTAGE;
   1489      1.1   agc 		DEBC(conn, 0, ("No CCB in run_xfer\n"));
   1490      1.1   agc 		scsipi_done(xs);
   1491      1.1   agc 		return;
   1492      1.1   agc 	}
   1493      1.1   agc 	/* copy parameters into CCB for easier access */
   1494      1.1   agc 	ccb->xs = xs;
   1495      1.1   agc 
   1496      1.1   agc 	ccb->data_in = (xs->xs_control & XS_CTL_DATA_IN) != 0;
   1497      1.1   agc 	ccb->data_len = (uint32_t) xs->datalen;
   1498      1.1   agc 	ccb->data_ptr = xs->data;
   1499      1.1   agc 
   1500      1.1   agc 	ccb->sense_len_req = sizeof(xs->sense.scsi_sense);
   1501      1.1   agc 	ccb->sense_ptr = &xs->sense;
   1502      1.1   agc 
   1503      1.1   agc 	ccb->lun = (uint64_t) xs->xs_periph->periph_lun << 48;
   1504      1.1   agc 	ccb->cmd = (uint8_t *) xs->cmd;
   1505      1.1   agc 	ccb->cmdlen = xs->cmdlen;
   1506      1.1   agc 	DEB(10, ("RunXfer: Periph_lun = %d, cmd[1] = %x, cmdlen = %d\n",
   1507      1.1   agc 			xs->xs_periph->periph_lun, ccb->cmd[1], xs->cmdlen));
   1508      1.1   agc 
   1509      1.1   agc #ifdef LUN_1
   1510      1.1   agc 	ccb->lun += 0x1000000000000LL;
   1511      1.1   agc 	ccb->cmd[1] += 0x10;
   1512      1.1   agc #endif
   1513      1.1   agc 	send_command(ccb, CCBDISP_SCSIPI, waitok, FALSE);
   1514      1.1   agc }
   1515      1.1   agc 
   1516      1.1   agc 
   1517      1.1   agc #ifndef ISCSI_MINIMAL
   1518      1.1   agc /*
   1519      1.1   agc  * send_io_command:
   1520      1.1   agc  *    Handle a SCSI io command request from user space.
   1521      1.1   agc  *
   1522      1.1   agc  *    Parameter:
   1523      1.1   agc  *          session 	The session
   1524      1.1   agc  *          lun		    The LUN to use
   1525      1.1   agc  *          req			The SCSI request block
   1526      1.1   agc  *			immed		Immediate command if TRUE
   1527      1.1   agc  *			conn_id		Assign to this connection ID if nonzero
   1528      1.1   agc  */
   1529      1.1   agc 
   1530      1.1   agc int
   1531      1.1   agc send_io_command(session_t *session, uint64_t lun, scsireq_t *req,
   1532      1.1   agc 				bool immed, uint32_t conn_id)
   1533      1.1   agc {
   1534      1.1   agc 	ccb_t *ccb;
   1535      1.1   agc 	connection_t *conn;
   1536      1.1   agc 	int rc;
   1537      1.1   agc 
   1538      1.1   agc 	DEB(9, ("IoCommand: lun=%x, datalen=%d, cmdlen=%d, immed=%d, cid=%d\n",
   1539      1.1   agc 			(int) lun, (int) req->datalen, (int) req->cmdlen, immed, conn_id));
   1540      1.1   agc 
   1541      1.1   agc 	conn = (conn_id) ? find_connection(session, conn_id)
   1542      1.1   agc 					 : assign_connection(session, TRUE);
   1543      1.1   agc 
   1544      1.1   agc 	if (conn == NULL || conn->terminating || conn->state != ST_FULL_FEATURE) {
   1545      1.1   agc 		DEBOUT(("io_command on dead connection (state = %d)\n",
   1546      1.1   agc 				(conn != NULL) ? conn->state : -1));
   1547      1.1   agc 		return ISCSI_STATUS_INVALID_CONNECTION_ID;
   1548      1.1   agc 	}
   1549      1.1   agc 
   1550      1.1   agc 	ccb = get_ccb(conn, TRUE);
   1551      1.1   agc 	if (ccb == NULL) {
   1552      1.1   agc 		DEBOUT(("No CCB in io_command\n"));
   1553      1.1   agc 		return ISCSI_STATUS_NO_RESOURCES;
   1554      1.1   agc 	}
   1555      1.1   agc 
   1556      1.1   agc 	ccb->data_in = (req->flags & SCCMD_READ) != 0;
   1557      1.1   agc 	ccb->data_len = (uint32_t) req->datalen;
   1558      1.1   agc 	ccb->data_ptr = req->databuf;
   1559      1.1   agc 
   1560      1.1   agc 	ccb->sense_len_req = req->senselen;
   1561      1.1   agc 	ccb->sense_ptr = &req->sense;
   1562      1.1   agc 
   1563      1.1   agc 	ccb->lun = lun;
   1564      1.1   agc 	ccb->cmd = (uint8_t *) req->cmd;
   1565      1.1   agc 	ccb->cmdlen = req->cmdlen;
   1566      1.1   agc 	DEBC(conn, 10, ("IoCommand: cmd[1] = %x, cmdlen = %d\n",
   1567      1.1   agc 			 ccb->cmd[1], ccb->cmdlen));
   1568      1.1   agc 
   1569      1.1   agc 	send_command(ccb, CCBDISP_WAIT, TRUE, immed);
   1570      1.1   agc 
   1571      1.1   agc 	rc = ccb->status;
   1572      1.1   agc 
   1573      1.1   agc 	req->senselen_used = ccb->sense_len_got;
   1574      1.1   agc 	req->datalen_used = req->datalen - ccb->residual;
   1575      1.1   agc 
   1576      1.1   agc 	free_ccb(ccb);
   1577      1.1   agc 
   1578      1.1   agc 	return rc;
   1579      1.1   agc }
   1580      1.1   agc #endif
   1581      1.1   agc 
   1582      1.1   agc 
   1583      1.1   agc /*****************************************************************************
   1584      1.1   agc  * Timeout handlers
   1585      1.1   agc  *****************************************************************************/
   1586      1.1   agc /*
   1587      1.1   agc  * connection_timeout:
   1588      1.1   agc  *    Handle prolonged silence on a connection by checking whether
   1589      1.1   agc  *    it's still alive.
   1590      1.1   agc  *    This has the side effect of discovering missing status or lost commands
   1591      1.1   agc  *    before those time out.
   1592      1.1   agc  *
   1593      1.1   agc  *    Parameter:
   1594      1.1   agc  *          par      The connection
   1595      1.1   agc  */
   1596      1.1   agc 
   1597      1.1   agc void
   1598      1.1   agc connection_timeout(void *par)
   1599      1.1   agc {
   1600      1.1   agc 	connection_t *conn = (connection_t *) par;
   1601      1.1   agc 
   1602      1.1   agc 	PDEBC(conn, 1, ("Connection Timeout, num_timeouts=%d\n",
   1603      1.1   agc 					conn->num_timeouts));
   1604      1.1   agc 
   1605      1.1   agc 	if (++conn->num_timeouts > MAX_CONN_TIMEOUTS)
   1606      1.1   agc 		handle_connection_error(conn, ISCSI_STATUS_TIMEOUT, NO_LOGOUT);
   1607      1.1   agc 	else {
   1608      1.1   agc 		if (conn->state == ST_FULL_FEATURE)
   1609      1.1   agc 			send_nop_out(conn, NULL);
   1610      1.1   agc 
   1611      1.1   agc 		SET_CONN_TIMEOUT(conn, CONNECTION_TIMEOUT);
   1612      1.1   agc 	}
   1613      1.1   agc }
   1614      1.1   agc 
   1615      1.1   agc /*
   1616      1.1   agc  * ccb_timeout:
   1617      1.1   agc  *    Handle timeout of a sent command.
   1618      1.1   agc  *
   1619      1.1   agc  *    Parameter:
   1620      1.1   agc  *          par      The CCB
   1621      1.1   agc  */
   1622      1.1   agc 
   1623      1.1   agc void
   1624      1.1   agc ccb_timeout(void *par)
   1625      1.1   agc {
   1626      1.1   agc 	ccb_t *ccb = (ccb_t *) par;
   1627      1.1   agc 	connection_t *conn = ccb->connection;
   1628      1.1   agc 	PDEBC(conn, 1, ("CCB Timeout, ccb=%x, num_timeouts=%d\n",
   1629      1.1   agc 			 (int) ccb, ccb->num_timeouts));
   1630      1.1   agc 
   1631      1.1   agc 	ccb->total_tries++;
   1632      1.1   agc 
   1633      1.1   agc 	if (++ccb->num_timeouts > MAX_CCB_TIMEOUTS ||
   1634      1.1   agc 		ccb->total_tries > MAX_CCB_TRIES ||
   1635      1.1   agc 		ccb->disp <= CCBDISP_FREE ||
   1636      1.1   agc 		!ccb->session->ErrorRecoveryLevel) {
   1637  1.1.2.1  yamt 		ccb->status = ISCSI_STATUS_TIMEOUT;
   1638  1.1.2.1  yamt 		complete_ccb(ccb);
   1639  1.1.2.1  yamt 
   1640  1.1.2.1  yamt 		handle_connection_error(conn, ISCSI_STATUS_TIMEOUT, RECOVER_CONNECTION);
   1641      1.1   agc 	} else {
   1642      1.1   agc 		if (ccb->data_in && ccb->xfer_len < ccb->data_len) {
   1643      1.1   agc 			/* request resend of all missing data */
   1644      1.1   agc 			snack_missing(conn, ccb, SNACK_DATA_NAK, 0, 0);
   1645      1.1   agc 		} else {
   1646      1.1   agc 			/* request resend of all missing status */
   1647      1.1   agc 			snack_missing(conn, NULL, SNACK_STATUS_NAK, 0, 0);
   1648      1.1   agc 		}
   1649      1.1   agc 		SET_CCB_TIMEOUT(conn, ccb, COMMAND_TIMEOUT);
   1650      1.1   agc 	}
   1651      1.1   agc }
   1652