/*-
* Copyright (c) 2004,2005,2006,2011 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Wasabi Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "iscsi_globals.h"
/*
* my_soo_write:
* Replacement for soo_write with flag handling.
*
* Parameter:
* conn The connection
* u The uio descriptor
*
* Returns: 0 on success, else EIO.
*/
STATIC int
my_soo_write(connection_t *conn, struct uio *u)
{
struct socket *so;
int ret;
#ifdef ISCSI_DEBUG
size_t resid = u->uio_resid;
#endif
KASSERT(u->uio_resid != 0);
rw_enter(&conn->c_sock_rw, RW_READER);
if (conn->c_sock == NULL) {
ret = EIO;
} else {
so = conn->c_sock->f_socket;
ret = (*so->so_send)(so, NULL, u,
NULL, NULL, 0, conn->c_threadobj);
}
rw_exit(&conn->c_sock_rw);
DEB(99, ("soo_write done: len = %zu\n", u->uio_resid));
/*
* assign_connection:
* This function returns the connection to use for the next transaction.
*
* Parameter: The session
*
* Returns: The connection
*/
mutex_enter(&sess->s_lock);
do {
if (sess->s_terminating ||
(conn = sess->s_mru_connection) == NULL) {
mutex_exit(&sess->s_lock);
return NULL;
}
next = conn;
do {
next = TAILQ_NEXT(next, c_connections);
if (next == NULL) {
next = TAILQ_FIRST(&sess->s_conn_list);
}
} while (next != NULL && next != conn &&
next->c_state != ST_FULL_FEATURE);
if (next->c_state != ST_FULL_FEATURE) {
if (waitok) {
cv_wait(&sess->s_sess_cv, &sess->s_lock);
next = TAILQ_FIRST(&sess->s_conn_list);
} else {
mutex_exit(&sess->s_lock);
return NULL;
}
} else {
sess->s_mru_connection = next;
}
} while (next != NULL && next->c_state != ST_FULL_FEATURE);
mutex_exit(&sess->s_lock);
return next;
}
/*
* reassign_tasks:
* Reassign pending commands to one of the still existing connections
* of a session.
*
* Parameter:
* oldconn The terminating connection
*/
if ((conn = assign_connection(sess, FALSE)) == NULL) {
DEB(1, ("Reassign_tasks of Session %d, connection %d failed, "
"no active connection\n",
sess->s_id, oldconn->c_id));
/* XXX here we need to abort the waiting CCBs */
return;
}
/* restore overwritten back ptr */
pdu->pdu_connection = conn;
/* fixup saved UIO and IOVEC (regular one will be overwritten anyway) */
pdu->pdu_save_uio.uio_iov = pdu->pdu_io_vec;
pdu->pdu_save_iovec [0].iov_base = &pdu->pdu_hdr;
/* link new PDU into old CCB */
ccb->ccb_pdu_waiting = pdu;
/* link new CCB into new connection */
ccb->ccb_connection = conn;
/* reset timeouts */
ccb->ccb_num_timeouts = 0;
/* put ready CCB into waiting list of new connection */
suspend_ccb(ccb, TRUE);
mutex_exit(&conn->c_lock);
}
if (TAILQ_FIRST(&old_waiting) != NULL) {
DEBC(conn, 0, ("Error while copying PDUs in reassign_tasks!\n"));
/*
* give up recovering, the other connection is screwed up
* as well...
*/
while ((ccb = TAILQ_FIRST(&old_waiting)) != NULL) {
TAILQ_REMOVE(&old_waiting, ccb, ccb_chain);
TAILQ_FOREACH(ccb, &conn->c_ccbs_waiting, ccb_chain) {
if (!no_tm) {
rc = send_task_management(conn, ccb, NULL, TASK_REASSIGN);
}
/* if we get an error on reassign, restart the original request */
if (no_tm || rc) {
mutex_enter(&sess->s_lock);
if (ccb->ccb_CmdSN < sess->s_ExpCmdSN) {
pdu = ccb->ccb_pdu_waiting;
sn = get_sernum(sess, pdu);
/*
* iscsi_send_thread:
* This thread services the send queue, writing the PDUs to the socket.
* It also handles the cleanup when the connection is terminated.
*
* Parameter:
* par The connection this thread services
*/
sess = conn->c_session;
/* so cleanup thread knows there's someone left */
iscsi_num_send_threads++;
do {
mutex_enter(&conn->c_lock);
while (!conn->c_terminating) {
while (!conn->c_terminating &&
(pdu = TAILQ_FIRST(&conn->c_pdus_to_send)) != NULL) {
TAILQ_REMOVE(&conn->c_pdus_to_send, pdu, pdu_send_chain);
pdu->pdu_flags &= ~PDUF_INQUEUE;
mutex_exit(&conn->c_lock);
/* update ExpStatSN here to avoid discontinuities */
/* and delays in updating target */
pdu->pdu_hdr.pduh_p.command.ExpStatSN = htonl(conn->c_StatSN_buf.ExpSN);
if (conn->c_HeaderDigest)
pdu->pdu_hdr.pduh_HeaderDigest = gen_digest(&pdu->pdu_hdr, BHS_SIZE);
mutex_enter(&conn->c_lock);
pdisp = pdu->pdu_disp;
if (pdisp > PDUDISP_FREE)
pdu->pdu_flags &= ~PDUF_BUSY;
mutex_exit(&conn->c_lock);
if (pdisp <= PDUDISP_FREE)
free_pdu(pdu);
mutex_enter(&conn->c_lock);
}
if (!conn->c_terminating)
cv_wait(&conn->c_conn_cv, &conn->c_lock);
}
mutex_exit(&conn->c_lock);
/* ------------------------------------------------------------------------
* Here this thread takes over cleanup of the terminating connection.
* ------------------------------------------------------------------------
*/
connection_timeout_stop(conn);
conn->c_idle_timeout_val = CONNECTION_IDLE_TIMEOUT;
fp = conn->c_sock;
/*
* We shutdown the socket here to force the receive
* thread to wake up
*/
DEBC(conn, 1, ("Closing Socket %p\n", conn->c_sock));
solock(fp->f_socket);
soshutdown(fp->f_socket, SHUT_RDWR);
sounlock(fp->f_socket);
/* wake up any non-reassignable waiting CCBs */
TAILQ_FOREACH_SAFE(ccb, &conn->c_ccbs_waiting, ccb_chain, nccb) {
if (!(ccb->ccb_flags & CCBF_REASSIGN) || ccb->ccb_pdu_waiting == NULL) {
DEBC(conn, 1, ("Terminating CCB %p (t=%p)\n",
ccb,&ccb->ccb_timeout));
wake_ccb(ccb, conn->c_terminating);
} else {
ccb_timeout_stop(ccb);
ccb->ccb_num_timeouts = 0;
}
}
/* clean out anything left in send queue */
mutex_enter(&conn->c_lock);
while ((pdu = TAILQ_FIRST(&conn->c_pdus_to_send)) != NULL) {
TAILQ_REMOVE(&conn->c_pdus_to_send, pdu, pdu_send_chain);
pdu->pdu_flags &= ~(PDUF_INQUEUE | PDUF_BUSY);
mutex_exit(&conn->c_lock);
/* if it's not attached to a waiting CCB, free it */
if (pdu->pdu_owner == NULL ||
pdu->pdu_owner->ccb_pdu_waiting != pdu) {
free_pdu(pdu);
}
mutex_enter(&conn->c_lock);
}
mutex_exit(&conn->c_lock);
/* If there's another connection available, transfer pending tasks */
if (sess->s_active_connections &&
TAILQ_FIRST(&conn->c_ccbs_waiting) != NULL) {
DEBC(conn, 1, ("Waiting for conn_idle\n"));
mutex_enter(&conn->c_lock);
if (!conn->c_destroy)
cv_timedwait(&conn->c_idle_cv, &conn->c_lock, CONNECTION_IDLE_TIMEOUT);
mutex_exit(&conn->c_lock);
DEBC(conn, 1, ("Waited for conn_idle, destroy = %d\n", conn->c_destroy));
} while (!conn->c_destroy);
/* wake up anyone waiting for a PDU */
mutex_enter(&conn->c_lock);
cv_broadcast(&conn->c_conn_cv);
mutex_exit(&conn->c_lock);
/* wake up any waiting CCBs */
while ((ccb = TAILQ_FIRST(&conn->c_ccbs_waiting)) != NULL) {
KASSERT(ccb->ccb_disp >= CCBDISP_NOWAIT);
wake_ccb(ccb, conn->c_terminating);
/* NOTE: wake_ccb will remove the CCB from the queue */
}
/*
* send_pdu:
* Enqueue a PDU to be sent, and handle its disposition as well as
* the disposition of its associated CCB.
*
* Parameter:
* ccb The associated CCB. May be NULL if cdisp is CCBDISP_NOWAIT
* and pdisp is not PDUDISP_WAIT
* cdisp The CCB's disposition
* pdu The PDU
* pdisp The PDU's disposition
*/
/*
* setup_tx_uio:
* Initialize the uio structure for sending, including header,
* data (if present), padding, and Data Digest.
* Header Digest is generated in send thread.
*
* Parameter:
* pdu The PDU
* dsl The Data Segment Length
* data The data pointer
* read TRUE if this is a read operation
*/
/* Pad to next multiple of 4 */
pad = uio->uio_resid & 0x03;
if (pad) {
i = uio->uio_iovcnt++;
pad = 4 - pad;
pdu->pdu_io_vec[i].iov_base = pad_bytes;
pdu->pdu_io_vec[i].iov_len = pad;
uio->uio_resid += pad;
}
/*
* negotiate_login:
* Control login negotiation.
*
* Parameter:
* conn The connection
* rx_pdu The received login response PDU
* tx_ccb The originally sent login CCB
*/
void
negotiate_login(connection_t *conn, pdu_t *rx_pdu, ccb_t *tx_ccb)
{
int rc;
bool next = TRUE;
pdu_t *tx_pdu;
uint8_t c_phase;
switch (c_phase) {
case SG_SECURITY_NEGOTIATION:
rc = assemble_security_parameters(conn, tx_ccb, rx_pdu, tx_pdu);
if (rc < 0)
next = FALSE;
break;
case SG_LOGIN_OPERATIONAL_NEGOTIATION:
if (conn->c_state == ST_SEC_FIN) {
/*
* Both sides announced to continue with
* operational negotation, but this is the
* last target packet from mutual CHAP
* that needs to be validated.
*/
rc = assemble_security_parameters(conn, tx_ccb, rx_pdu, tx_pdu);
if (rc)
break;
/*
* Response was valid, drop (security) parameters
* so that we start negotiating operational
* parameters.
*/
rx_pdu->pdu_temp_data = NULL;
}
/*
* init_text_pdu:
* Initialize the text PDU.
*
* Parameter:
* conn The connection
* ccb The transmit CCB
* ppdu The transmit PDU
* rx_pdu The received PDU if this is an unsolicited negotiation
*/
/*
* acknowledge_text:
* Acknowledge a continued login or text response.
*
* Parameter:
* conn The connection
* rx_pdu The received login/text response PDU
* tx_ccb The originally sent login/text request CCB
*/
/*
* negotiate_text:
* Handle received text negotiation.
*
* Parameter:
* conn The connection
* rx_pdu The received text response PDU
* tx_ccb The original CCB
*/
/*
* send_send_targets:
* Send out a SendTargets text request.
* The result is stored in the fields in the session structure.
*
* Parameter:
* session The session
* key The text key to use
*
* Returns: 0 on success, else an error code.
*/
int
send_send_targets(session_t *sess, uint8_t *key)
{
ccb_t *ccb;
pdu_t *pdu;
int rc = 0;
connection_t *conn;
/*
* snack_missing:
* Send SNACK request for missing data.
*
* Parameter:
* conn The connection
* ccb The task's CCB (for Data NAK only)
* type The SNACK type
* BegRun The BegRun field
* RunLength The RunLength field
*/
/*
* send_snack:
* Send SNACK request.
*
* Parameter:
* conn The connection
* rx_pdu The received data in PDU
* tx_ccb The original command CCB (required for Data ACK only)
* type The SNACK type
*
* Returns: 0 on success, else an error code.
*/
/*
* send_logout:
* Send logout request.
* NOTE: This function does not wait for the logout to complete.
*
* Parameter:
* conn The connection
* refconn The referenced connection
* reason The reason code
* wait Wait for completion if TRUE
*
* Returns: 0 on success (logout sent), else an error code.
*/
int
send_logout(connection_t *conn, connection_t *refconn, int reason,
bool wait)
{
ccb_t *ccb;
pdu_t *ppdu;
pdu_header_t *hpdu;
DEBC(conn, 5, ("Send_logout\n"));
ccb = get_ccb(conn, TRUE);
/* can only happen if terminating... */
if (ccb == NULL)
return conn->c_terminating;
ppdu = get_pdu(conn, TRUE);
if (ppdu == NULL) {
free_ccb(ccb);
return conn->c_terminating;
}
if (wait) {
int rc = ccb->ccb_status;
free_ccb (ccb);
return rc;
}
return 0;
}
/*
* send_task_management:
* Send task management request.
*
* Parameter:
* conn The connection
* ref_ccb The referenced command (NULL if none)
* xs The scsipi command structure (NULL if not a scsipi request)
* function The function code
*
* Returns: 0 on success, else an error code.
*/
int
send_task_management(connection_t *conn, ccb_t *ref_ccb, struct scsipi_xfer *xs,
int function)
{
ccb_t *ccb;
pdu_t *ppdu;
pdu_header_t *hpdu;
if (xs == NULL) {
int rc = ccb->ccb_status;
free_ccb(ccb);
return rc;
}
return 0;
}
/*
* send_data_out:
* Send data to target in response to an R2T or as unsolicited data.
*
* Parameter:
* conn The connection
* rx_pdu The received R2T PDU (NULL if unsolicited)
* tx_ccb The originally sent command CCB
* waitok Whether it's OK to wait for an available PDU or not
*/
if (totlen) {
if (ccb->ccb_data_in) {
hpdu->pduh_Flags = FLAG_READ;
totlen = 0;
} else {
hpdu->pduh_Flags = FLAG_WRITE;
/* immediate data we can send */
len = min(totlen, conn->c_max_firstimmed);
/* can we send more unsolicited data ? */
totlen = conn->c_max_firstdata ? totlen - len : 0;
}
}
if (!totlen)
hpdu->pduh_Flags |= FLAG_FINAL;
hpdu->pduh_Flags |= ccb->ccb_tag;
if (ccb->ccb_data_in)
init_sernum(&ccb->ccb_DataSN_buf);
#ifndef ISCSI_MINIMAL
/*
* send_io_command:
* Handle a SCSI io command request from user space.
*
* Parameter:
* session The session
* lun The LUN to use
* req The SCSI request block
* immed Immediate command if TRUE
* conn_id Assign to this connection ID if nonzero
*/
int
send_io_command(session_t *session, uint64_t lun, scsireq_t *req,
bool immed, uint32_t conn_id)
{
ccb_t *ccb;
connection_t *conn;
int rc;
/*****************************************************************************
* Timeout handlers
*****************************************************************************/
/*
* connection_timeout:
* Handle prolonged silence on a connection by checking whether
* it's still alive.
* This has the side effect of discovering missing status or lost commands
* before those time out.
*
* Parameter:
* conn The connection
*/