Home | History | Annotate | Line # | Download | only in iscsi
iscsi_ioctl.c revision 1.2
      1  1.2  para /*	$NetBSD: iscsi_ioctl.c,v 1.2 2012/01/27 19:48:39 para 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 
     32  1.1   agc #include "iscsi_globals.h"
     33  1.1   agc 
     34  1.1   agc #include <sys/file.h>
     35  1.1   agc #include <sys/filedesc.h>
     36  1.1   agc #include <sys/proc.h>
     37  1.1   agc 
     38  1.1   agc #ifndef ISCSI_MINIMAL
     39  1.1   agc #include <uvm/uvm.h>
     40  1.1   agc #include <uvm/uvm_pmap.h>
     41  1.1   agc #endif
     42  1.1   agc 
     43  1.1   agc static uint16_t current_id = 0;	/* Global session ID counter */
     44  1.1   agc 
     45  1.1   agc /* list of event handlers */
     46  1.1   agc static event_handler_list_t event_handlers =
     47  1.1   agc 	TAILQ_HEAD_INITIALIZER(event_handlers);
     48  1.1   agc 
     49  1.1   agc static uint32_t handler_id = 0;	/* Handler ID counter */
     50  1.1   agc 
     51  1.1   agc /* -------------------------------------------------------------------------- */
     52  1.1   agc 
     53  1.1   agc /* Event management functions */
     54  1.1   agc 
     55  1.1   agc /*
     56  1.1   agc  * find_handler:
     57  1.1   agc  *    Search the event handler list for the given ID.
     58  1.1   agc  *
     59  1.1   agc  *    Parameter:
     60  1.1   agc  *          id    The handler ID.
     61  1.1   agc  *
     62  1.1   agc  *    Returns:
     63  1.1   agc  *          Pointer to handler if found, else NULL.
     64  1.1   agc  */
     65  1.1   agc 
     66  1.1   agc 
     67  1.1   agc STATIC event_handler_t *
     68  1.1   agc find_handler(uint32_t id)
     69  1.1   agc {
     70  1.1   agc 	event_handler_t *curr;
     71  1.1   agc 
     72  1.1   agc 	TAILQ_FOREACH(curr, &event_handlers, link)
     73  1.1   agc 		if (curr->id == id)
     74  1.1   agc 			break;
     75  1.1   agc 
     76  1.1   agc 	return curr;
     77  1.1   agc }
     78  1.1   agc 
     79  1.1   agc 
     80  1.1   agc /*
     81  1.1   agc  * register_event:
     82  1.1   agc  *    Create event handler entry, return ID.
     83  1.1   agc  *
     84  1.1   agc  *    Parameter:
     85  1.1   agc  *          par   The parameter.
     86  1.1   agc  */
     87  1.1   agc 
     88  1.1   agc STATIC void
     89  1.1   agc register_event(iscsi_register_event_parameters_t *par)
     90  1.1   agc {
     91  1.1   agc 	event_handler_t *handler;
     92  1.1   agc 	int was_empty;
     93  1.1   agc 
     94  1.1   agc 	handler = malloc(sizeof(event_handler_t), M_DEVBUF, M_WAITOK | M_ZERO);
     95  1.1   agc 	if (handler == NULL) {
     96  1.1   agc 		DEBOUT(("No mem for event handler\n"));
     97  1.1   agc 		par->status = ISCSI_STATUS_NO_RESOURCES;
     98  1.1   agc 		return;
     99  1.1   agc 	}
    100  1.1   agc 
    101  1.1   agc 	TAILQ_INIT(&handler->events);
    102  1.1   agc 
    103  1.1   agc 	/* create a unique ID */
    104  1.1   agc 	CS_BEGIN;
    105  1.1   agc 	do {
    106  1.1   agc 		++handler_id;
    107  1.1   agc 	} while (!handler_id || find_handler(handler_id) != NULL);
    108  1.1   agc 	par->event_id = handler->id = handler_id;
    109  1.1   agc 
    110  1.1   agc 	was_empty = TAILQ_FIRST(&event_handlers) == NULL;
    111  1.1   agc 
    112  1.1   agc 	TAILQ_INSERT_TAIL(&event_handlers, handler, link);
    113  1.1   agc 
    114  1.1   agc 	if (was_empty) {
    115  1.1   agc 		wakeup(&cleanup_list);
    116  1.1   agc 	}
    117  1.1   agc 	CS_END;
    118  1.1   agc 
    119  1.1   agc 	par->status = ISCSI_STATUS_SUCCESS;
    120  1.1   agc 	DEB(5, ("Register Event OK, ID %d\n", par->event_id));
    121  1.1   agc }
    122  1.1   agc 
    123  1.1   agc 
    124  1.1   agc /*
    125  1.1   agc  * deregister_event:
    126  1.1   agc  *    Destroy handler entry and any waiting events, wake up waiter.
    127  1.1   agc  *
    128  1.1   agc  *    Parameter:
    129  1.1   agc  *          par   The parameter.
    130  1.1   agc  */
    131  1.1   agc 
    132  1.1   agc STATIC void
    133  1.1   agc deregister_event(iscsi_register_event_parameters_t *par)
    134  1.1   agc {
    135  1.1   agc 	event_handler_t *handler;
    136  1.1   agc 	event_t *evt;
    137  1.1   agc 
    138  1.1   agc 	handler = find_handler(par->event_id);
    139  1.1   agc 	if (handler == NULL) {
    140  1.1   agc 		DEB(1, ("Deregister Event ID %d not found\n", par->event_id));
    141  1.1   agc 		par->status = ISCSI_STATUS_INVALID_EVENT_ID;
    142  1.1   agc 		return;
    143  1.1   agc 	}
    144  1.1   agc 	CS_BEGIN;
    145  1.1   agc 	TAILQ_REMOVE(&event_handlers, handler, link);
    146  1.1   agc 	CS_END;
    147  1.1   agc 	if (handler->waiter != NULL) {
    148  1.1   agc 		handler->waiter->status = ISCSI_STATUS_EVENT_DEREGISTERED;
    149  1.1   agc 		wakeup(handler->waiter);
    150  1.1   agc 	}
    151  1.1   agc 
    152  1.1   agc 	while ((evt = TAILQ_FIRST(&handler->events)) != NULL) {
    153  1.1   agc 		TAILQ_REMOVE(&handler->events, evt, link);
    154  1.1   agc 		free(evt, M_TEMP);
    155  1.1   agc 	}
    156  1.1   agc 
    157  1.1   agc 	free(handler, M_DEVBUF);
    158  1.1   agc 	par->status = ISCSI_STATUS_SUCCESS;
    159  1.1   agc 	DEB(5, ("Deregister Event ID %d complete\n", par->event_id));
    160  1.1   agc }
    161  1.1   agc 
    162  1.1   agc 
    163  1.1   agc /*
    164  1.1   agc  * check_event:
    165  1.1   agc  *    Return first queued event. Optionally wait for arrival of event.
    166  1.1   agc  *
    167  1.1   agc  *    Parameter:
    168  1.1   agc  *          par   The parameter.
    169  1.1   agc  *          wait  Wait for event if true
    170  1.1   agc  */
    171  1.1   agc 
    172  1.1   agc STATIC void
    173  1.1   agc check_event(iscsi_wait_event_parameters_t *par, bool wait)
    174  1.1   agc {
    175  1.1   agc 	event_handler_t *handler;
    176  1.1   agc 	event_t *evt;
    177  1.1   agc 
    178  1.1   agc 	handler = find_handler(par->event_id);
    179  1.1   agc 	if (handler == NULL) {
    180  1.1   agc 		DEBOUT(("Wait Event ID %d not found\n", par->event_id));
    181  1.1   agc 		par->status = ISCSI_STATUS_INVALID_EVENT_ID;
    182  1.1   agc 		return;
    183  1.1   agc 	}
    184  1.1   agc 	if (handler->waiter != NULL) {
    185  1.1   agc 		DEBOUT(("Wait Event ID %d already waiting\n", par->event_id));
    186  1.1   agc 		par->status = ISCSI_STATUS_EVENT_WAITING;
    187  1.1   agc 		return;
    188  1.1   agc 	}
    189  1.1   agc 	par->status = ISCSI_STATUS_SUCCESS;
    190  1.1   agc 	DEB(99, ("Wait Event ID %d\n", par->event_id));
    191  1.1   agc 
    192  1.1   agc 	do {
    193  1.1   agc 		int s = splbio();
    194  1.1   agc 		evt = TAILQ_FIRST(&handler->events);
    195  1.1   agc 		if (evt != NULL) {
    196  1.1   agc 			TAILQ_REMOVE(&handler->events, evt, link);
    197  1.1   agc 			splx(s);
    198  1.1   agc 		} else {
    199  1.1   agc 			if (!wait) {
    200  1.1   agc 				splx(s);
    201  1.1   agc 				par->status = ISCSI_STATUS_LIST_EMPTY;
    202  1.1   agc 				return;
    203  1.1   agc 			}
    204  1.1   agc 			if (par->status != ISCSI_STATUS_SUCCESS) {
    205  1.1   agc 				splx(s);
    206  1.1   agc 				return;
    207  1.1   agc 			}
    208  1.1   agc 			handler->waiter = par;
    209  1.1   agc 			splx(s);
    210  1.1   agc 			tsleep(par, PRIBIO, "iscsievtwait", 0);
    211  1.1   agc 		}
    212  1.1   agc 	} while (evt == NULL);
    213  1.1   agc 
    214  1.1   agc 	par->connection_id = evt->connection_id;
    215  1.1   agc 	par->session_id = evt->session_id;
    216  1.1   agc 	par->event_kind = evt->event_kind;
    217  1.1   agc 	par->reason = evt->reason;
    218  1.1   agc 
    219  1.1   agc 	free(evt, M_TEMP);
    220  1.1   agc }
    221  1.1   agc 
    222  1.1   agc /*
    223  1.1   agc  * add_event
    224  1.1   agc  *    Adds an event entry to each registered handler queue.
    225  1.1   agc  *    Note that events are simply duplicated because we expect the number of
    226  1.1   agc  *    handlers to be very small, usually 1 (the daemon).
    227  1.1   agc  *
    228  1.1   agc  *    Parameters:
    229  1.1   agc  *       kind     The event kind
    230  1.1   agc  *       sid      The ID of the affected session
    231  1.1   agc  *       cid      The ID of the affected connection
    232  1.1   agc  *       reason   The reason code
    233  1.1   agc  */
    234  1.1   agc 
    235  1.1   agc void
    236  1.1   agc add_event(iscsi_event_t kind, uint32_t sid, uint32_t cid, uint32_t reason)
    237  1.1   agc {
    238  1.1   agc 	event_handler_t *curr;
    239  1.1   agc 	event_t *evt;
    240  1.1   agc 
    241  1.1   agc 	DEB(9, ("Add_event kind %d, sid %d, cid %d, reason %d\n",
    242  1.1   agc 		kind, sid, cid, reason));
    243  1.1   agc 
    244  1.1   agc 	TAILQ_FOREACH(curr, &event_handlers, link) {
    245  1.1   agc 		evt = malloc(sizeof(*evt), M_TEMP, M_WAITOK);
    246  1.1   agc 		if (evt == NULL) {
    247  1.1   agc 			panic("iSCSI: add_event failed to alloc memory");
    248  1.1   agc 		}
    249  1.1   agc 		evt->event_kind = kind;
    250  1.1   agc 		evt->session_id = sid;
    251  1.1   agc 		evt->connection_id = cid;
    252  1.1   agc 		evt->reason = reason;
    253  1.1   agc 		CS_BEGIN;
    254  1.1   agc 		TAILQ_INSERT_TAIL(&curr->events, evt, link);
    255  1.1   agc 		if (curr->waiter != NULL) {
    256  1.1   agc 			wakeup(curr->waiter);
    257  1.1   agc 			curr->waiter = NULL;
    258  1.1   agc 		}
    259  1.1   agc 		CS_END;
    260  1.1   agc 	}
    261  1.1   agc }
    262  1.1   agc 
    263  1.1   agc 
    264  1.1   agc /*
    265  1.1   agc  * check_event_handlers
    266  1.1   agc  *    Checks for dead event handlers. A dead event handler would deplete
    267  1.1   agc  *    memory over time, so we have to make sure someone at the other
    268  1.1   agc  *    end is actively monitoring events.
    269  1.1   agc  *    This function is called every 30 seconds or so (less frequent if there
    270  1.1   agc  *    is other activity for the cleanup thread to deal with) to go through
    271  1.1   agc  *    the list of handlers and check whether the first element in the event
    272  1.1   agc  *    list has changed at all. If not, the event is deregistered.
    273  1.1   agc  *    Note that this will not detect dead handlers if no events are pending,
    274  1.1   agc  *    but we don't care as long as events don't accumulate in the list.
    275  1.1   agc  */
    276  1.1   agc 
    277  1.1   agc STATIC void
    278  1.1   agc check_event_handlers(void)
    279  1.1   agc {
    280  1.1   agc 	event_handler_t *curr, *next;
    281  1.1   agc 	event_t *evt;
    282  1.1   agc 
    283  1.1   agc 	for (curr = TAILQ_FIRST(&event_handlers); curr != NULL; curr = next) {
    284  1.1   agc 		next = TAILQ_NEXT(curr, link);
    285  1.1   agc 		evt = TAILQ_FIRST(&curr->events);
    286  1.1   agc 
    287  1.1   agc 		if (evt != NULL && evt == curr->first_in_list) {
    288  1.1   agc 			DEBOUT(("Found Dead Event Handler %d, removing\n", curr->id));
    289  1.1   agc 
    290  1.1   agc 			TAILQ_REMOVE(&event_handlers, curr, link);
    291  1.1   agc 			while ((evt = TAILQ_FIRST(&curr->events)) != NULL) {
    292  1.1   agc 				TAILQ_REMOVE(&curr->events, evt, link);
    293  1.1   agc 				free(evt, M_TEMP);
    294  1.1   agc 			}
    295  1.1   agc 			free(curr, M_DEVBUF);
    296  1.1   agc 		} else
    297  1.1   agc 			curr->first_in_list = evt;
    298  1.1   agc 	}
    299  1.1   agc }
    300  1.1   agc 
    301  1.1   agc 
    302  1.1   agc /* -------------------------------------------------------------------------- */
    303  1.1   agc 
    304  1.1   agc /*
    305  1.1   agc  * get_socket:
    306  1.1   agc  *    Get the file pointer from the socket handle passed into login.
    307  1.1   agc  *
    308  1.1   agc  *    Parameter:
    309  1.1   agc  *          fdes     IN: The socket handle
    310  1.1   agc  *          fpp      OUT: The pointer to the resulting file pointer
    311  1.1   agc  *
    312  1.1   agc  *    Returns:    0 on success, else an error code.
    313  1.1   agc  *
    314  1.1   agc  */
    315  1.1   agc 
    316  1.1   agc STATIC int
    317  1.1   agc get_socket(int fdes, struct file **fpp)
    318  1.1   agc {
    319  1.1   agc 	struct file *fp;
    320  1.1   agc 
    321  1.1   agc 	if ((fp = fd_getfile(fdes)) == NULL) {
    322  1.1   agc 		return EBADF;
    323  1.1   agc 	}
    324  1.1   agc 	if (fp->f_type != DTYPE_SOCKET) {
    325  1.1   agc 		return ENOTSOCK;
    326  1.1   agc 	}
    327  1.1   agc 	/* Add the reference */
    328  1.1   agc 	fp->f_count++;
    329  1.1   agc 
    330  1.1   agc 	/*simple_unlock (&fp->f_slock); */
    331  1.1   agc 
    332  1.1   agc 	*fpp = fp;
    333  1.1   agc 	return 0;
    334  1.1   agc }
    335  1.1   agc 
    336  1.1   agc 
    337  1.1   agc /*
    338  1.1   agc  * find_session:
    339  1.1   agc  *    Find a session by ID.
    340  1.1   agc  *
    341  1.1   agc  *    Parameter:  the session ID
    342  1.1   agc  *
    343  1.1   agc  *    Returns:    The pointer to the session (or NULL if not found)
    344  1.1   agc  */
    345  1.1   agc 
    346  1.1   agc session_t *
    347  1.1   agc find_session(uint32_t id)
    348  1.1   agc {
    349  1.1   agc 	session_t *curr;
    350  1.1   agc 
    351  1.1   agc 	TAILQ_FOREACH(curr, &sessions, sessions)
    352  1.1   agc 		if (curr->id == id) {
    353  1.1   agc 			break;
    354  1.1   agc 		}
    355  1.1   agc 	return curr;
    356  1.1   agc }
    357  1.1   agc 
    358  1.1   agc 
    359  1.1   agc /*
    360  1.1   agc  * find_connection:
    361  1.1   agc  *    Find a connection by ID.
    362  1.1   agc  *
    363  1.1   agc  *    Parameter:  the session pointer and the connection ID
    364  1.1   agc  *
    365  1.1   agc  *    Returns:    The pointer to the connection (or NULL if not found)
    366  1.1   agc  */
    367  1.1   agc 
    368  1.1   agc connection_t *
    369  1.1   agc find_connection(session_t *session, uint32_t id)
    370  1.1   agc {
    371  1.1   agc 	connection_t *curr;
    372  1.1   agc 
    373  1.1   agc 	TAILQ_FOREACH(curr, &session->conn_list, connections)
    374  1.1   agc 		if (curr->id == id) {
    375  1.1   agc 			break;
    376  1.1   agc 		}
    377  1.1   agc 	return curr;
    378  1.1   agc }
    379  1.1   agc 
    380  1.1   agc 
    381  1.1   agc /*
    382  1.1   agc  * kill_connection:
    383  1.1   agc  *    Terminate the connection as gracefully as possible.
    384  1.1   agc  *
    385  1.1   agc  *    Parameter:
    386  1.1   agc  *		conn		The connection to terminate
    387  1.1   agc  *		status		The status code for the connection's "terminating" field
    388  1.1   agc  *		logout		The logout reason code
    389  1.1   agc  *		recover	Attempt to recover connection
    390  1.1   agc  */
    391  1.1   agc 
    392  1.1   agc void
    393  1.1   agc kill_connection(connection_t *conn, uint32_t status, int logout, bool recover)
    394  1.1   agc {
    395  1.1   agc 	session_t *sess = conn->session;
    396  1.1   agc 
    397  1.1   agc 	DEBC(conn, 1, ("Kill_connection: terminating=%d, status=%d, logout=%d, "
    398  1.1   agc 			   "state=%d\n",
    399  1.1   agc 			   conn->terminating, status, logout, conn->state));
    400  1.1   agc 
    401  1.1   agc 	if (recover &&
    402  1.1   agc 	    !conn->destroy &&
    403  1.1   agc 	    conn->recover > MAX_RECOVERY_ATTEMPTS) {
    404  1.1   agc 		DEBC(conn, 1,
    405  1.1   agc 			  ("Kill_connection: Too many recovery attempts, destroying\n"));
    406  1.1   agc 		conn->destroy = TRUE;
    407  1.1   agc 	}
    408  1.1   agc 
    409  1.1   agc 	if (!recover || conn->destroy) {
    410  1.1   agc 		CS_BEGIN;
    411  1.1   agc 		if (conn->in_session) {
    412  1.1   agc 			conn->in_session = FALSE;
    413  1.1   agc 			TAILQ_REMOVE(&sess->conn_list, conn, connections);
    414  1.1   agc 			sess->mru_connection = TAILQ_FIRST(&sess->conn_list);
    415  1.1   agc 		}
    416  1.1   agc 		CS_END;
    417  1.1   agc 		if (!conn->destroy) {
    418  1.1   agc 			DEBC(conn, 1, ("Kill_connection setting destroy flag\n"));
    419  1.1   agc 			conn->destroy = TRUE;
    420  1.1   agc 		}
    421  1.1   agc 		/* in case it was already terminated earlier and rcv/send-threads */
    422  1.1   agc 		/* are waiting */
    423  1.1   agc 		wakeup(conn);
    424  1.1   agc 	}
    425  1.1   agc 
    426  1.1   agc 	/* Don't recurse */
    427  1.1   agc 	if (conn->terminating) {
    428  1.1   agc 		DEBC(conn, 1, ("Kill_connection exiting (already terminating)\n"));
    429  1.1   agc 		return;
    430  1.1   agc 	}
    431  1.1   agc 
    432  1.1   agc 	if (conn->state == ST_FULL_FEATURE) {
    433  1.1   agc 		sess->active_connections--;
    434  1.1   agc 
    435  1.1   agc 		/* If this is the last connection and ERL < 2, reset TSIH */
    436  1.1   agc 		if (!sess->active_connections && sess->ErrorRecoveryLevel < 2)
    437  1.1   agc 			sess->TSIH = 0;
    438  1.1   agc 
    439  1.1   agc 		/* Don't try to log out if the socket is broken or we're in the middle */
    440  1.1   agc 		/* of logging in */
    441  1.1   agc 		if (logout >= 0) {
    442  1.1   agc 			conn->state = ST_WINDING_DOWN;
    443  1.1   agc 			SET_CONN_TIMEOUT(conn, CONNECTION_TIMEOUT);
    444  1.1   agc 
    445  1.1   agc 			if (sess->ErrorRecoveryLevel < 2 &&
    446  1.1   agc 			    logout == RECOVER_CONNECTION) {
    447  1.1   agc 				logout = LOGOUT_CONNECTION;
    448  1.1   agc 			}
    449  1.1   agc 			if (!sess->active_connections &&
    450  1.1   agc 			    logout == LOGOUT_CONNECTION) {
    451  1.1   agc 				logout = LOGOUT_SESSION;
    452  1.1   agc 			}
    453  1.1   agc 			if (!send_logout(conn, conn, logout, FALSE)) {
    454  1.1   agc 				return;
    455  1.1   agc 			}
    456  1.1   agc 			/*
    457  1.1   agc 			 * if the logout request was successfully sent, the logout response
    458  1.1   agc 			 * handler will do the rest of the termination processing. If the
    459  1.1   agc 			 * logout doesn't get a response, we'll get back in here once
    460  1.1   agc 			 * the timeout hits.
    461  1.1   agc 			 */
    462  1.1   agc 		}
    463  1.1   agc 	}
    464  1.1   agc 
    465  1.1   agc #ifdef ISCSI_TEST_MODE
    466  1.1   agc 	test_remove_connection(conn);
    467  1.1   agc #endif
    468  1.1   agc 
    469  1.1   agc 	conn->terminating = status;
    470  1.1   agc 	conn->state = ST_SETTLING;
    471  1.1   agc 	callout_stop(&conn->timeout);
    472  1.1   agc 
    473  1.1   agc 	/* let send thread take over next step of cleanup */
    474  1.1   agc 	wakeup(&conn->pdus_to_send);
    475  1.1   agc 
    476  1.1   agc 	DEBC(conn, 5, ("kill_connection returns\n"));
    477  1.1   agc }
    478  1.1   agc 
    479  1.1   agc 
    480  1.1   agc /*
    481  1.1   agc  * kill_session:
    482  1.1   agc  *    Terminate the session as gracefully as possible.
    483  1.1   agc  *
    484  1.1   agc  *    Parameter:
    485  1.1   agc  *		session		Session to terminate
    486  1.1   agc  *		status		The status code for the termination
    487  1.1   agc  *		logout		The logout reason code
    488  1.1   agc 
    489  1.1   agc  */
    490  1.1   agc 
    491  1.1   agc void
    492  1.1   agc kill_session(session_t *session, uint32_t status, int logout, bool recover)
    493  1.1   agc {
    494  1.1   agc 	connection_t *curr;
    495  1.1   agc 	ccb_t *ccb;
    496  1.1   agc 
    497  1.1   agc 	DEB(1, ("ISCSI: kill_session %d, status %d, logout %d, recover %d\n",
    498  1.1   agc 			session->id, status, logout, recover));
    499  1.1   agc 
    500  1.1   agc 	/*
    501  1.1   agc 	 * don't do anything if session isn't established yet, termination will be
    502  1.1   agc 	 * handled elsewhere
    503  1.1   agc 	 */
    504  1.1   agc 	if (session->sessions.tqe_next == NULL &&
    505  1.1   agc 	    session->sessions.tqe_prev == NULL) {
    506  1.1   agc 		return;
    507  1.1   agc 	}
    508  1.1   agc 
    509  1.1   agc 	if (recover) {
    510  1.1   agc 		/*
    511  1.1   agc 		 * Only recover when there's just one active connection left.
    512  1.1   agc 		 * Otherwise we get in all sorts of timing problems, and it doesn't
    513  1.1   agc 		 * make much sense anyway to recover when the other side has
    514  1.1   agc 		 * requested that we kill a multipathed session.
    515  1.1   agc 		 */
    516  1.1   agc 		if (session->active_connections == 1) {
    517  1.1   agc 			curr = assign_connection(session, FALSE);
    518  1.1   agc 			if (curr != NULL)
    519  1.1   agc 				kill_connection(curr, status, logout, TRUE);
    520  1.1   agc 		}
    521  1.1   agc 		/* don't allow the session to disappear when the target */
    522  1.1   agc 		/* requested the logout */
    523  1.1   agc 		return;
    524  1.1   agc 	}
    525  1.1   agc 
    526  1.1   agc 	/* remove from session list */
    527  1.1   agc 	TAILQ_REMOVE(&sessions, session, sessions);
    528  1.1   agc 	session->sessions.tqe_next = NULL;
    529  1.1   agc 	session->sessions.tqe_prev = NULL;
    530  1.1   agc 
    531  1.1   agc 	/* complete any throttled CCBs */
    532  1.1   agc 	while ((ccb = TAILQ_FIRST(&session->ccbs_throttled)) != NULL) {
    533  1.1   agc 		ccb->status = ISCSI_STATUS_LOGOUT;
    534  1.1   agc 		TAILQ_REMOVE(&session->ccbs_throttled, ccb, chain);
    535  1.1   agc 		complete_ccb(ccb);
    536  1.1   agc 	}
    537  1.1   agc 
    538  1.1   agc 	/*
    539  1.1   agc 	 * unmap first to give the system an opportunity to flush its buffers,
    540  1.1   agc 	 * but don't try to unmap if it's a forced termination (connection is dead)
    541  1.1   agc 	 * to avoid waiting for pending commands that can't complete anyway.
    542  1.1   agc 	 */
    543  1.1   agc 	if (logout >= 0) {
    544  1.1   agc 		unmap_session(session);
    545  1.1   agc 		DEB(5, ("Unmap Returns\n"));
    546  1.1   agc 	}
    547  1.1   agc 
    548  1.1   agc 	/* kill all connections */
    549  1.1   agc 	while ((curr = TAILQ_FIRST(&session->conn_list)) != NULL) {
    550  1.1   agc 		kill_connection(curr, status, logout, FALSE);
    551  1.1   agc 		logout = NO_LOGOUT;
    552  1.1   agc 	}
    553  1.1   agc }
    554  1.1   agc 
    555  1.1   agc 
    556  1.1   agc /*
    557  1.1   agc  * create_connection:
    558  1.1   agc  *    Create and init the necessary framework for a connection:
    559  1.1   agc  *       Alloc the connection structure itself
    560  1.1   agc  *       Copy connection parameters
    561  1.1   agc  *       Create the send and receive threads
    562  1.1   agc  *       And finally, log in.
    563  1.1   agc  *
    564  1.1   agc  *    Parameter:
    565  1.1   agc  *          par      IN/OUT: The login parameters
    566  1.1   agc  *          session  IN: The owning session
    567  1.1   agc  *          p        IN: The proc pointer of the caller
    568  1.1   agc  *
    569  1.1   agc  *    Returns:    0 on success
    570  1.1   agc  *                >0 on failure, connection structure deleted
    571  1.1   agc  *                <0 on failure, connection is still terminating
    572  1.1   agc  */
    573  1.1   agc 
    574  1.1   agc STATIC int
    575  1.1   agc create_connection(iscsi_login_parameters_t *par, session_t *session,
    576  1.1   agc 				  PTHREADOBJ p)
    577  1.1   agc {
    578  1.1   agc 	connection_t *connection;
    579  1.1   agc 	int rc;
    580  1.1   agc 
    581  1.1   agc 	DEB(1, ("Create Connection for Session %d\n", session->id));
    582  1.1   agc 
    583  1.1   agc 	if (session->MaxConnections &&
    584  1.1   agc 	    session->active_connections >= session->MaxConnections) {
    585  1.1   agc 		DEBOUT(("Too many connections (max = %d, curr = %d)\n",
    586  1.1   agc 				session->MaxConnections, session->active_connections));
    587  1.1   agc 		par->status = ISCSI_STATUS_MAXED_CONNECTIONS;
    588  1.1   agc 		return EIO;
    589  1.1   agc 	}
    590  1.1   agc 
    591  1.1   agc 	connection = malloc(sizeof(*connection), M_DEVBUF, M_WAITOK | M_ZERO);
    592  1.1   agc 	if (connection == NULL) {
    593  1.1   agc 		DEBOUT(("No mem for connection\n"));
    594  1.1   agc 		par->status = ISCSI_STATUS_NO_RESOURCES;
    595  1.1   agc 		return EIO;
    596  1.1   agc 	}
    597  1.1   agc 
    598  1.1   agc 	/* create a unique ID */
    599  1.1   agc 	do {
    600  1.1   agc 		++session->conn_id;
    601  1.1   agc 	} while (!session->conn_id ||
    602  1.1   agc 		 find_connection(session, session->conn_id) != NULL);
    603  1.1   agc 
    604  1.1   agc 	par->connection_id = connection->id = session->conn_id;
    605  1.1   agc 	DEB(99, ("Connection ID = %d\n", connection->id));
    606  1.1   agc 
    607  1.1   agc 	connection->session = session;
    608  1.1   agc 
    609  1.1   agc 	TAILQ_INIT(&connection->ccbs_waiting);
    610  1.1   agc 	TAILQ_INIT(&connection->pdus_to_send);
    611  1.1   agc 	TAILQ_INIT(&connection->pdu_pool);
    612  1.1   agc 
    613  1.1   agc 	callout_init(&connection->timeout, 0);
    614  1.1   agc 	callout_setfunc(&connection->timeout, connection_timeout, connection);
    615  1.1   agc 	connection->idle_timeout_val = CONNECTION_IDLE_TIMEOUT;
    616  1.1   agc 
    617  1.1   agc 	create_pdus(connection);
    618  1.1   agc 
    619  1.1   agc 	if ((rc = get_socket(par->socket, &connection->sock)) != 0) {
    620  1.1   agc 		DEBOUT(("Invalid socket %d\n", par->socket));
    621  1.1   agc 
    622  1.1   agc 		free(connection, M_DEVBUF);
    623  1.1   agc 		par->status = ISCSI_STATUS_INVALID_SOCKET;
    624  1.1   agc 		return rc;
    625  1.1   agc 	}
    626  1.1   agc 	DEBC(connection, 1, ("get_socket: par_sock=%d, fdesc=%p\n",
    627  1.1   agc 			par->socket, connection->sock));
    628  1.1   agc 
    629  1.1   agc 	connection->threadobj = p;
    630  1.1   agc 	connection->login_par = par;
    631  1.1   agc 
    632  1.1   agc 	/*DEBOUT (("Creating receive thread\n")); */
    633  1.1   agc 	if ((rc = kthread_create(PRI_NONE, 0, NULL, iscsi_rcv_thread,
    634  1.1   agc 				connection, &connection->rcvproc,
    635  1.1   agc 				"ConnRcv")) != 0) {
    636  1.1   agc 		DEBOUT(("Can't create rcv thread (rc %d)\n", rc));
    637  1.1   agc 
    638  1.1   agc 		free(connection, M_DEVBUF);
    639  1.1   agc 		par->status = ISCSI_STATUS_NO_RESOURCES;
    640  1.1   agc 		return rc;
    641  1.1   agc 	}
    642  1.1   agc 	/*DEBOUT (("Creating send thread\n")); */
    643  1.1   agc 	if ((rc = kthread_create(PRI_NONE, 0, NULL, iscsi_send_thread,
    644  1.1   agc 				connection, &connection->sendproc,
    645  1.1   agc 				"ConnSend")) != 0) {
    646  1.1   agc 		DEBOUT(("Can't create send thread (rc %d)\n", rc));
    647  1.1   agc 
    648  1.1   agc 		connection->terminating = TRUE;
    649  1.1   agc 
    650  1.1   agc 		/*
    651  1.1   agc 		 * We must close the socket here to force the receive
    652  1.1   agc 		 * thread to wake up
    653  1.1   agc 		 */
    654  1.1   agc 		DEBC(connection, 1,
    655  1.1   agc 			("Closing Socket %p\n", connection->sock));
    656  1.1   agc #if __NetBSD_Version__ > 500000000
    657  1.1   agc 		mutex_enter(&connection->sock->f_lock);
    658  1.1   agc 		connection->sock->f_count += 1;
    659  1.1   agc 		mutex_exit(&connection->sock->f_lock);
    660  1.1   agc #else
    661  1.1   agc 		FILE_USE(connection->sock);
    662  1.1   agc #endif
    663  1.1   agc 		closef(connection->sock);
    664  1.1   agc 
    665  1.1   agc 		/* give receive thread time to exit */
    666  1.1   agc 		tsleep(connection, PWAIT, "settle", 20);
    667  1.1   agc 
    668  1.1   agc 		free(connection, M_DEVBUF);
    669  1.1   agc 		par->status = ISCSI_STATUS_NO_RESOURCES;
    670  1.1   agc 		return rc;
    671  1.1   agc 	}
    672  1.1   agc 
    673  1.1   agc 	/*
    674  1.1   agc 	 * At this point, each thread will tie 'sock' into its own file descriptor
    675  1.1   agc 	 * tables w/o increasing the use count - they will inherit the use
    676  1.1   agc 	 * increments performed in get_socket().
    677  1.1   agc 	 */
    678  1.1   agc 
    679  1.1   agc #ifdef ISCSI_TEST_MODE
    680  1.1   agc 	test_assign_connection(connection);
    681  1.1   agc #endif
    682  1.1   agc 
    683  1.1   agc 	if ((rc = send_login(connection)) != 0) {
    684  1.1   agc 		DEBC(connection, 0, ("Login failed (rc %d)\n", rc));
    685  1.1   agc 		/* Don't attempt to recover, there seems to be something amiss */
    686  1.1   agc 		kill_connection(connection, rc, NO_LOGOUT, FALSE);
    687  1.1   agc 		par->status = rc;
    688  1.1   agc 		return -1;
    689  1.1   agc 	}
    690  1.1   agc 
    691  1.1   agc 	CS_BEGIN;
    692  1.1   agc 	connection->state = ST_FULL_FEATURE;
    693  1.1   agc 
    694  1.1   agc 	TAILQ_INSERT_TAIL(&session->conn_list, connection, connections);
    695  1.1   agc 	connection->in_session = TRUE;
    696  1.1   agc 	session->total_connections++;
    697  1.1   agc 	session->active_connections++;
    698  1.1   agc 	session->mru_connection = connection;
    699  1.1   agc 	CS_END;
    700  1.1   agc 
    701  1.1   agc 	DEBC(connection, 5, ("Connection created successfully!\n"));
    702  1.1   agc 	return 0;
    703  1.1   agc }
    704  1.1   agc 
    705  1.1   agc 
    706  1.1   agc /*
    707  1.1   agc  * recreate_connection:
    708  1.1   agc  *    Revive dead connection
    709  1.1   agc  *
    710  1.1   agc  *    Parameter:
    711  1.1   agc  *          par      IN/OUT: The login parameters
    712  1.1   agc  *          conn     IN: The connection
    713  1.1   agc  *          p        IN: The proc pointer of the caller
    714  1.1   agc  *
    715  1.1   agc  *    Returns:    0 on success
    716  1.1   agc  *                >0 on failure, connection structure deleted
    717  1.1   agc  *                <0 on failure, connection is still terminating
    718  1.1   agc  */
    719  1.1   agc 
    720  1.1   agc STATIC int
    721  1.1   agc recreate_connection(iscsi_login_parameters_t *par, session_t *session,
    722  1.1   agc 					connection_t *connection, PTHREADOBJ p)
    723  1.1   agc {
    724  1.1   agc 	int rc;
    725  1.1   agc 	ccb_t *ccb;
    726  1.1   agc 	ccb_list_t old_waiting;
    727  1.1   agc 
    728  1.1   agc 	DEB(1, ("ReCreate Connection %d for Session %d, ERL=%d\n",
    729  1.1   agc 		connection->id, connection->session->id,
    730  1.1   agc 		connection->session->ErrorRecoveryLevel));
    731  1.1   agc 
    732  1.1   agc 	if (session->MaxConnections &&
    733  1.1   agc 	    session->active_connections >= session->MaxConnections) {
    734  1.1   agc 		DEBOUT(("Too many connections (max = %d, curr = %d)\n",
    735  1.1   agc 			session->MaxConnections, session->active_connections));
    736  1.1   agc 		par->status = ISCSI_STATUS_MAXED_CONNECTIONS;
    737  1.1   agc 		return EIO;
    738  1.1   agc 	}
    739  1.1   agc 
    740  1.1   agc 	if ((rc = get_socket(par->socket, &connection->sock)) != 0) {
    741  1.1   agc 		DEBOUT(("Invalid socket %d\n", par->socket));
    742  1.1   agc 		par->status = ISCSI_STATUS_INVALID_SOCKET;
    743  1.1   agc 		return rc;
    744  1.1   agc 	}
    745  1.1   agc 
    746  1.1   agc 	connection->threadobj = p;
    747  1.1   agc 	connection->login_par = par;
    748  1.1   agc 	connection->terminating = 0;
    749  1.1   agc 	connection->recover++;
    750  1.1   agc 	connection->num_timeouts = 0;
    751  1.1   agc 	connection->state = ST_SEC_NEG;
    752  1.1   agc 
    753  1.1   agc 	session->active_connections++;
    754  1.1   agc 
    755  1.1   agc 	TAILQ_INIT(&old_waiting);
    756  1.1   agc 	while ((ccb = TAILQ_FIRST(&connection->ccbs_waiting)) != NULL) {
    757  1.1   agc 		TAILQ_REMOVE(&connection->ccbs_waiting, ccb, chain);
    758  1.1   agc 		TAILQ_INSERT_TAIL(&old_waiting, ccb, chain);
    759  1.1   agc 	}
    760  1.1   agc 
    761  1.1   agc 	init_sernum(&connection->StatSN_buf);
    762  1.1   agc 	wakeup(connection);
    763  1.1   agc 
    764  1.1   agc 	if ((rc = send_login(connection)) != 0) {
    765  1.1   agc 		DEBOUT(("Login failed (rc %d)\n", rc));
    766  1.1   agc 		while ((ccb = TAILQ_FIRST(&old_waiting)) != NULL) {
    767  1.1   agc 			TAILQ_REMOVE(&old_waiting, ccb, chain);
    768  1.1   agc 			ccb->status = rc;
    769  1.1   agc 			complete_ccb(ccb);
    770  1.1   agc 		}
    771  1.1   agc 		/* Don't attempt to recover, there seems to be something amiss */
    772  1.1   agc 		kill_connection(connection, rc, NO_LOGOUT, FALSE);
    773  1.1   agc 		par->status = rc;
    774  1.1   agc 		return -1;
    775  1.1   agc 	}
    776  1.1   agc 
    777  1.1   agc 	DEBC(connection, 9, ("Re-Login successful\n"));
    778  1.1   agc 	par->status = ISCSI_STATUS_SUCCESS;
    779  1.1   agc 	CS_BEGIN;
    780  1.1   agc 	connection->state = ST_FULL_FEATURE;
    781  1.1   agc 	session->mru_connection = connection;
    782  1.1   agc 	CS_END;
    783  1.1   agc 
    784  1.1   agc 	while ((ccb = TAILQ_FIRST(&old_waiting)) != NULL) {
    785  1.1   agc 		TAILQ_REMOVE(&old_waiting, ccb, chain);
    786  1.1   agc 		TAILQ_INSERT_TAIL(&connection->ccbs_waiting, ccb, chain);
    787  1.1   agc 
    788  1.1   agc 		rc = send_task_management(connection, ccb, NULL, TASK_REASSIGN);
    789  1.1   agc 		/* if we get an error on reassign, restart the original request */
    790  1.1   agc 		if (rc && ccb->pdu_waiting != NULL) {
    791  1.1   agc 			if (ccb->CmdSN < session->ExpCmdSN) {
    792  1.1   agc 				pdu_t *pdu = ccb->pdu_waiting;
    793  1.1   agc 
    794  1.1   agc 				/* update CmdSN */
    795  1.1   agc 				DEBC(connection, 1, ("Resend Updating CmdSN - old %d, new %d\n",
    796  1.1   agc 					   ccb->CmdSN, session->CmdSN));
    797  1.1   agc 				ccb->CmdSN = session->CmdSN;
    798  1.1   agc 				if (!(pdu->pdu.Opcode & OP_IMMEDIATE))
    799  1.1   agc 					session->CmdSN++;
    800  1.1   agc 				pdu->pdu.p.command.CmdSN = htonl(ccb->CmdSN);
    801  1.1   agc 			}
    802  1.1   agc 			resend_pdu(ccb);
    803  1.1   agc 		} else {
    804  1.1   agc 			SET_CCB_TIMEOUT(connection, ccb, COMMAND_TIMEOUT);
    805  1.1   agc 		}
    806  1.1   agc 	}
    807  1.1   agc 
    808  1.1   agc 	wakeup(session);
    809  1.1   agc 
    810  1.1   agc 	DEBC(connection, 5, ("Connection ReCreated successfully - status %d\n",
    811  1.1   agc 						 par->status));
    812  1.1   agc 	return 0;
    813  1.1   agc }
    814  1.1   agc 
    815  1.1   agc /* -------------------------------------------------------------------------- */
    816  1.1   agc 
    817  1.1   agc /*
    818  1.1   agc  * check_login_pars:
    819  1.1   agc  *    Check the parameters passed into login/add_connection
    820  1.1   agc  *    for validity and consistency.
    821  1.1   agc  *
    822  1.1   agc  *    Parameter:
    823  1.1   agc  *          par      The login parameters
    824  1.1   agc  *
    825  1.1   agc  *    Returns:    0 on success, else an error code.
    826  1.1   agc  */
    827  1.1   agc 
    828  1.1   agc STATIC int
    829  1.1   agc check_login_pars(iscsi_login_parameters_t *par)
    830  1.1   agc {
    831  1.1   agc 	int i, n;
    832  1.1   agc 
    833  1.1   agc 	if (par->is_present.auth_info) {
    834  1.1   agc 		/* check consistency of authentication parameters */
    835  1.1   agc 
    836  1.1   agc 		if (par->auth_info.auth_number > ISCSI_AUTH_OPTIONS) {
    837  1.1   agc 			DEBOUT(("Auth number invalid: %d\n", par->auth_info.auth_number));
    838  1.1   agc 			return ISCSI_STATUS_PARAMETER_INVALID;
    839  1.1   agc 		}
    840  1.1   agc 
    841  1.1   agc 		if (par->auth_info.auth_number > 2) {
    842  1.1   agc 			DEBOUT(("Auth number invalid: %d\n", par->auth_info.auth_number));
    843  1.1   agc 			return ISCSI_STATUS_NOTIMPL;
    844  1.1   agc 		}
    845  1.1   agc 
    846  1.1   agc 		for (i = 0, n = 0; i < par->auth_info.auth_number; i++) {
    847  1.1   agc #if 0
    848  1.1   agc 			if (par->auth_info.auth_type[i] < ISCSI_AUTH_None) {
    849  1.1   agc 				DEBOUT(("Auth type invalid: %d\n",
    850  1.1   agc 						par->auth_info.auth_type[i]));
    851  1.1   agc 				return ISCSI_STATUS_PARAMETER_INVALID;
    852  1.1   agc 			}
    853  1.1   agc #endif
    854  1.1   agc 			if (par->auth_info.auth_type[i] > ISCSI_AUTH_CHAP) {
    855  1.1   agc 				DEBOUT(("Auth type invalid: %d\n",
    856  1.1   agc 						par->auth_info.auth_type[i]));
    857  1.1   agc 				return ISCSI_STATUS_NOTIMPL;
    858  1.1   agc 			}
    859  1.1   agc 			n = max(n, par->auth_info.auth_type[i]);
    860  1.1   agc 		}
    861  1.1   agc 		if (n) {
    862  1.1   agc 			if (!par->is_present.password ||
    863  1.1   agc 				(par->auth_info.mutual_auth &&
    864  1.1   agc 				 !par->is_present.target_password)) {
    865  1.1   agc 				DEBOUT(("Password missing\n"));
    866  1.1   agc 				return ISCSI_STATUS_PARAMETER_MISSING;
    867  1.1   agc 			}
    868  1.1   agc 			/* Note: Default for user-name is initiator name */
    869  1.1   agc 		}
    870  1.1   agc 	}
    871  1.1   agc 	if (par->login_type != ISCSI_LOGINTYPE_DISCOVERY &&
    872  1.1   agc 	    !par->is_present.TargetName) {
    873  1.1   agc 		DEBOUT(("Target name missing, login type %d\n", par->login_type));
    874  1.1   agc 		return ISCSI_STATUS_PARAMETER_MISSING;
    875  1.1   agc 	}
    876  1.1   agc 	if (par->is_present.MaxRecvDataSegmentLength) {
    877  1.1   agc 		if (par->MaxRecvDataSegmentLength < 512 ||
    878  1.1   agc 			par->MaxRecvDataSegmentLength > 0xffffff) {
    879  1.1   agc 			DEBOUT(("MaxRecvDataSegmentLength invalid: %d\n",
    880  1.1   agc 					par->MaxRecvDataSegmentLength));
    881  1.1   agc 			return ISCSI_STATUS_PARAMETER_INVALID;
    882  1.1   agc 		}
    883  1.1   agc 	}
    884  1.1   agc 	return ISCSI_STATUS_SUCCESS;
    885  1.1   agc }
    886  1.1   agc 
    887  1.1   agc 
    888  1.1   agc /*
    889  1.1   agc  * login:
    890  1.1   agc  *    Handle the login ioctl - Create a session:
    891  1.1   agc  *       Alloc the session structure
    892  1.1   agc  *       Copy session parameters
    893  1.1   agc  *       And call create_connection to establish the connection.
    894  1.1   agc  *
    895  1.1   agc  *    Parameter:
    896  1.1   agc  *          par      IN/OUT: The login parameters
    897  1.1   agc  *          p        IN: The proc pointer of the caller
    898  1.1   agc  */
    899  1.1   agc 
    900  1.1   agc STATIC void
    901  1.1   agc login(iscsi_login_parameters_t *par, PTHREADOBJ p)
    902  1.1   agc {
    903  1.1   agc 	session_t *session;
    904  1.1   agc 	int rc;
    905  1.1   agc 
    906  1.1   agc 	DEB(99, ("ISCSI: login\n"));
    907  1.1   agc 
    908  1.1   agc 	if (!InitiatorName[0]) {
    909  1.1   agc 		DEB(1, ("No Initiator Name\n"));
    910  1.1   agc 		par->status = ISCSI_STATUS_NO_INITIATOR_NAME;
    911  1.1   agc 		return;
    912  1.1   agc 	}
    913  1.1   agc 
    914  1.1   agc 	if ((par->status = check_login_pars(par)) != 0)
    915  1.1   agc 		return;
    916  1.1   agc 
    917  1.1   agc 	/* alloc the session */
    918  1.1   agc 	session = malloc(sizeof(*session), M_DEVBUF, M_WAITOK | M_ZERO);
    919  1.1   agc 	if (session == NULL) {
    920  1.1   agc 		DEBOUT(("No mem for session\n"));
    921  1.1   agc 		par->status = ISCSI_STATUS_NO_RESOURCES;
    922  1.1   agc 		return;
    923  1.1   agc 	}
    924  1.1   agc 	TAILQ_INIT(&session->conn_list);
    925  1.1   agc 	TAILQ_INIT(&session->ccb_pool);
    926  1.1   agc 	TAILQ_INIT(&session->ccbs_throttled);
    927  1.1   agc 
    928  1.1   agc 	/* create a unique ID */
    929  1.1   agc 	do {
    930  1.1   agc 		++current_id;
    931  1.1   agc 	} while (!current_id || find_session(current_id) != NULL);
    932  1.1   agc 	par->session_id = session->id = current_id;
    933  1.1   agc 
    934  1.1   agc 	create_ccbs(session);
    935  1.1   agc 	session->login_type = par->login_type;
    936  1.1   agc 	session->CmdSN = 1;
    937  1.1   agc 
    938  1.1   agc 	if ((rc = create_connection(par, session, p)) != 0) {
    939  1.1   agc 		if (rc > 0) {
    940  1.1   agc 			free(session, M_DEVBUF);
    941  1.1   agc 		}
    942  1.1   agc 		return;
    943  1.1   agc 	}
    944  1.1   agc 
    945  1.1   agc 	CS_BEGIN;
    946  1.1   agc 	TAILQ_INSERT_HEAD(&sessions, session, sessions);
    947  1.1   agc 	CS_END;
    948  1.1   agc 
    949  1.1   agc 	/* Session established, map LUNs? */
    950  1.1   agc 	if (par->login_type == ISCSI_LOGINTYPE_MAP) {
    951  1.1   agc 		copyinstr(par->TargetName, session->tgtname,
    952  1.1   agc 		    sizeof(session->tgtname), NULL);
    953  1.1   agc 		if (!map_session(session)) {
    954  1.1   agc 			kill_session(session, ISCSI_STATUS_MAP_FAILED,
    955  1.1   agc 					LOGOUT_SESSION, FALSE);
    956  1.1   agc 			par->status = ISCSI_STATUS_MAP_FAILED;
    957  1.1   agc 			return;
    958  1.1   agc 		}
    959  1.1   agc 	}
    960  1.1   agc }
    961  1.1   agc 
    962  1.1   agc 
    963  1.1   agc /*
    964  1.1   agc  * logout:
    965  1.1   agc  *    Handle the logout ioctl - Kill a session.
    966  1.1   agc  *
    967  1.1   agc  *    Parameter:
    968  1.1   agc  *          par      IN/OUT: The login parameters
    969  1.1   agc  */
    970  1.1   agc 
    971  1.1   agc STATIC void
    972  1.1   agc logout(iscsi_logout_parameters_t *par)
    973  1.1   agc {
    974  1.1   agc 	session_t *session;
    975  1.1   agc 
    976  1.1   agc 	DEB(5, ("ISCSI: logout session %d\n", par->session_id));
    977  1.1   agc 
    978  1.1   agc 	if ((session = find_session(par->session_id)) == NULL) {
    979  1.1   agc 		DEBOUT(("Session %d not found\n", par->session_id));
    980  1.1   agc 		par->status = ISCSI_STATUS_INVALID_SESSION_ID;
    981  1.1   agc 		return;
    982  1.1   agc 	}
    983  1.1   agc 	/* If the session exists, this always succeeds */
    984  1.1   agc 	par->status = ISCSI_STATUS_SUCCESS;
    985  1.1   agc 
    986  1.1   agc 	kill_session(session, ISCSI_STATUS_LOGOUT, LOGOUT_SESSION, FALSE);
    987  1.1   agc }
    988  1.1   agc 
    989  1.1   agc 
    990  1.1   agc /*
    991  1.1   agc  * add_connection:
    992  1.1   agc  *    Handle the add_connection ioctl.
    993  1.1   agc  *
    994  1.1   agc  *    Parameter:
    995  1.1   agc  *          par      IN/OUT: The login parameters
    996  1.1   agc  *          p        IN: The proc pointer of the caller
    997  1.1   agc  */
    998  1.1   agc 
    999  1.1   agc STATIC void
   1000  1.1   agc add_connection(iscsi_login_parameters_t *par, PTHREADOBJ p)
   1001  1.1   agc {
   1002  1.1   agc 	session_t *session;
   1003  1.1   agc 
   1004  1.1   agc 	DEB(5, ("ISCSI: add_connection to session %d\n", par->session_id));
   1005  1.1   agc 
   1006  1.1   agc 	if ((session = find_session(par->session_id)) == NULL) {
   1007  1.1   agc 		DEBOUT(("Session %d not found\n", par->session_id));
   1008  1.1   agc 		par->status = ISCSI_STATUS_INVALID_SESSION_ID;
   1009  1.1   agc 		return;
   1010  1.1   agc 	}
   1011  1.1   agc 	if ((par->status = check_login_pars(par)) == 0) {
   1012  1.1   agc 		create_connection(par, session, p);
   1013  1.1   agc 	}
   1014  1.1   agc }
   1015  1.1   agc 
   1016  1.1   agc 
   1017  1.1   agc /*
   1018  1.1   agc  * remove_connection:
   1019  1.1   agc  *    Handle the remove_connection ioctl.
   1020  1.1   agc  *
   1021  1.1   agc  *    Parameter:
   1022  1.1   agc  *          par      IN/OUT: The remove parameters
   1023  1.1   agc  */
   1024  1.1   agc 
   1025  1.1   agc STATIC void
   1026  1.1   agc remove_connection(iscsi_remove_parameters_t *par)
   1027  1.1   agc {
   1028  1.1   agc 	connection_t *conn;
   1029  1.1   agc 	session_t *session;
   1030  1.1   agc 
   1031  1.1   agc 	DEB(5, ("ISCSI: remove_connection %d from session %d\n",
   1032  1.1   agc 			par->connection_id, par->session_id));
   1033  1.1   agc 
   1034  1.1   agc 	if ((session = find_session(par->session_id)) == NULL) {
   1035  1.1   agc 		DEBOUT(("Session %d not found\n", par->session_id));
   1036  1.1   agc 		par->status = ISCSI_STATUS_INVALID_SESSION_ID;
   1037  1.1   agc 		return;
   1038  1.1   agc 	}
   1039  1.1   agc 
   1040  1.1   agc 	if ((conn = find_connection(session, par->connection_id)) == NULL) {
   1041  1.1   agc 		DEBOUT(("Connection %d not found in session %d\n",
   1042  1.1   agc 				par->connection_id, par->session_id));
   1043  1.1   agc 
   1044  1.1   agc 		par->status = ISCSI_STATUS_INVALID_CONNECTION_ID;
   1045  1.1   agc 	} else {
   1046  1.1   agc 		kill_connection(conn, ISCSI_STATUS_LOGOUT, LOGOUT_CONNECTION,
   1047  1.1   agc 					FALSE);
   1048  1.1   agc 		par->status = ISCSI_STATUS_SUCCESS;
   1049  1.1   agc 	}
   1050  1.1   agc }
   1051  1.1   agc 
   1052  1.1   agc 
   1053  1.1   agc /*
   1054  1.1   agc  * restore_connection:
   1055  1.1   agc  *    Handle the restore_connection ioctl.
   1056  1.1   agc  *
   1057  1.1   agc  *    Parameter:
   1058  1.1   agc  *          par      IN/OUT: The login parameters
   1059  1.1   agc  *          p        IN: The proc pointer of the caller
   1060  1.1   agc  */
   1061  1.1   agc 
   1062  1.1   agc STATIC void
   1063  1.1   agc restore_connection(iscsi_login_parameters_t *par, PTHREADOBJ p)
   1064  1.1   agc {
   1065  1.1   agc 	session_t *session;
   1066  1.1   agc 	connection_t *connection;
   1067  1.1   agc 
   1068  1.1   agc 	DEB(5, ("ISCSI: restore_connection %d of session %d\n",
   1069  1.1   agc 			par->connection_id, par->session_id));
   1070  1.1   agc 
   1071  1.1   agc 	if ((session = find_session(par->session_id)) == NULL) {
   1072  1.1   agc 		DEBOUT(("Session %d not found\n", par->session_id));
   1073  1.1   agc 		par->status = ISCSI_STATUS_INVALID_SESSION_ID;
   1074  1.1   agc 		return;
   1075  1.1   agc 	}
   1076  1.1   agc 
   1077  1.1   agc 	if ((connection = find_connection(session, par->connection_id)) == NULL) {
   1078  1.1   agc 		DEBOUT(("Connection %d not found in session %d\n",
   1079  1.1   agc 				par->connection_id, par->session_id));
   1080  1.1   agc 		par->status = ISCSI_STATUS_INVALID_CONNECTION_ID;
   1081  1.1   agc 		return;
   1082  1.1   agc 	}
   1083  1.1   agc 
   1084  1.1   agc 	if ((par->status = check_login_pars(par)) == 0) {
   1085  1.1   agc 		recreate_connection(par, session, connection, p);
   1086  1.1   agc 	}
   1087  1.1   agc }
   1088  1.1   agc 
   1089  1.1   agc 
   1090  1.1   agc #ifndef ISCSI_MINIMAL
   1091  1.1   agc 
   1092  1.1   agc /*
   1093  1.1   agc  * map_databuf:
   1094  1.1   agc  *    Map user-supplied data buffer into kernel space.
   1095  1.1   agc  *
   1096  1.1   agc  *    Parameter:
   1097  1.1   agc  *          p        IN: The proc pointer of the caller
   1098  1.1   agc  *          buf      IN/OUT: The virtual address of the buffer, modified
   1099  1.1   agc  *                   on exit to reflect kernel VA.
   1100  1.1   agc  *          datalen  IN: The size of the data buffer
   1101  1.1   agc  *
   1102  1.1   agc  *    Returns:
   1103  1.1   agc  *          An ISCSI status code on error, else 0.
   1104  1.1   agc  */
   1105  1.1   agc 
   1106  1.1   agc uint32_t
   1107  1.1   agc map_databuf(struct proc *p, void **buf, uint32_t datalen)
   1108  1.1   agc {
   1109  1.1   agc 	vaddr_t kva, databuf, offs;
   1110  1.1   agc 	int error;
   1111  1.1   agc 
   1112  1.1   agc 	/* page align address */
   1113  1.1   agc 	databuf = (vaddr_t) * buf & ~PAGE_MASK;
   1114  1.1   agc 	/* offset of VA into page */
   1115  1.1   agc 	offs = (vaddr_t) * buf & PAGE_MASK;
   1116  1.1   agc 	/* round to full page including offset */
   1117  1.1   agc 	datalen = (datalen + offs + PAGE_MASK) & ~PAGE_MASK;
   1118  1.1   agc 
   1119  1.1   agc 	/* Do some magic to the vm space reference count (copied from "copyin_proc") */
   1120  1.1   agc 	if ((p->p_sflag & PS_WEXIT) || (p->p_vmspace->vm_refcnt < 1)) {
   1121  1.1   agc 		return ISCSI_STATUS_NO_RESOURCES;
   1122  1.1   agc 	}
   1123  1.1   agc 	p->p_vmspace->vm_refcnt++;
   1124  1.1   agc 
   1125  1.1   agc 	/* this is lifted from uvm_io */
   1126  1.1   agc 	error = uvm_map_extract(&p->p_vmspace->vm_map, databuf, datalen,
   1127  1.1   agc 			kernel_map, &kva,
   1128  1.1   agc 			UVM_EXTRACT_QREF | UVM_EXTRACT_CONTIG |
   1129  1.1   agc 				UVM_EXTRACT_FIXPROT);
   1130  1.1   agc 	if (error) {
   1131  1.1   agc 		DEBOUT(("uvm_map_extract failed, error = %d\n", error));
   1132  1.1   agc 		return ISCSI_STATUS_NO_RESOURCES;
   1133  1.1   agc 	}
   1134  1.1   agc 	/* add offset back into kernel VA */
   1135  1.1   agc 	*buf = (void *) (kva + offs);
   1136  1.1   agc 
   1137  1.1   agc 	return 0;
   1138  1.1   agc }
   1139  1.1   agc 
   1140  1.1   agc 
   1141  1.1   agc /*
   1142  1.1   agc  * unmap_databuf:
   1143  1.1   agc  *    Remove kernel space mapping of data buffer.
   1144  1.1   agc  *
   1145  1.1   agc  *    Parameter:
   1146  1.1   agc  *          p        IN: The proc pointer of the caller
   1147  1.1   agc  *          buf      IN: The kernel virtual address of the buffer
   1148  1.1   agc  *          datalen  IN: The size of the data buffer
   1149  1.1   agc  *
   1150  1.1   agc  *    Returns:
   1151  1.1   agc  *          An ISCSI status code on error, else 0.
   1152  1.1   agc  */
   1153  1.1   agc 
   1154  1.1   agc void
   1155  1.1   agc unmap_databuf(struct proc *p, void *buf, uint32_t datalen)
   1156  1.1   agc {
   1157  1.1   agc 	struct vm_map_entry *dead_entries;
   1158  1.1   agc 	vaddr_t databuf;
   1159  1.1   agc 
   1160  1.1   agc 	/* round to full page */
   1161  1.1   agc 	datalen = (datalen + ((uintptr_t) buf & PAGE_MASK) + PAGE_MASK) & ~PAGE_MASK;
   1162  1.1   agc 	/* page align address */
   1163  1.1   agc 	databuf = (vaddr_t) buf & ~PAGE_MASK;
   1164  1.1   agc 
   1165  1.1   agc 	/* following code lifted almost verbatim from uvm_io.c */
   1166  1.1   agc 	vm_map_lock(kernel_map);
   1167  1.1   agc 	uvm_unmap_remove(kernel_map, databuf, databuf + datalen, &dead_entries
   1168  1.1   agc #if (__NetBSD_Version__ >= 399000500)
   1169  1.2  para 					 , 0
   1170  1.1   agc #elif   (__NetBSD_Version__ >= 300000000)
   1171  1.1   agc 					 , NULL
   1172  1.1   agc #endif
   1173  1.1   agc 		);
   1174  1.1   agc 	vm_map_unlock(kernel_map);
   1175  1.1   agc 	if (dead_entries != NULL) {
   1176  1.1   agc 		uvm_unmap_detach(dead_entries, AMAP_REFALL);
   1177  1.1   agc 	}
   1178  1.1   agc 	/* this apparently reverses the magic to the vm ref count, from copyin_proc */
   1179  1.1   agc 	uvmspace_free(p->p_vmspace);
   1180  1.1   agc }
   1181  1.1   agc 
   1182  1.1   agc 
   1183  1.1   agc /*
   1184  1.1   agc  * io_command:
   1185  1.1   agc  *    Handle the io_command ioctl.
   1186  1.1   agc  *
   1187  1.1   agc  *    Parameter:
   1188  1.1   agc  *          par      IN/OUT: The iocommand parameters
   1189  1.1   agc  *          p        IN: The proc pointer of the caller
   1190  1.1   agc  */
   1191  1.1   agc 
   1192  1.1   agc STATIC void
   1193  1.1   agc io_command(iscsi_iocommand_parameters_t *par, PTHREADOBJ p)
   1194  1.1   agc {
   1195  1.1   agc 	uint32_t datalen = par->req.datalen;
   1196  1.1   agc 	void *databuf = par->req.databuf;
   1197  1.1   agc 	session_t *session;
   1198  1.1   agc 
   1199  1.1   agc 	DEB(9, ("ISCSI: io_command, SID=%d, lun=%" PRIu64 "\n", par->session_id, par->lun));
   1200  1.1   agc 	if ((session = find_session(par->session_id)) == NULL) {
   1201  1.1   agc 		DEBOUT(("Session %d not found\n", par->session_id));
   1202  1.1   agc 		par->status = ISCSI_STATUS_INVALID_SESSION_ID;
   1203  1.1   agc 		return;
   1204  1.1   agc 	}
   1205  1.1   agc 
   1206  1.1   agc 	par->req.senselen_used = 0;
   1207  1.1   agc 	par->req.datalen_used = 0;
   1208  1.1   agc 	par->req.error = 0;
   1209  1.1   agc 	par->req.status = 0;
   1210  1.1   agc 	par->req.retsts = SCCMD_UNKNOWN;	/* init to failure code */
   1211  1.1   agc 
   1212  1.1   agc 	if (par->req.cmdlen > 16 || par->req.senselen > sizeof(par->req.sense)) {
   1213  1.1   agc 		par->status = ISCSI_STATUS_PARAMETER_INVALID;
   1214  1.1   agc 		return;
   1215  1.1   agc 	}
   1216  1.1   agc 
   1217  1.1   agc 	if (datalen && (par->status = map_databuf(PROCP(p),
   1218  1.1   agc 			&par->req.databuf, datalen)) != 0) {
   1219  1.1   agc 		return;
   1220  1.1   agc 	}
   1221  1.1   agc 	par->status = send_io_command(session, par->lun, &par->req,
   1222  1.1   agc 								  par->options.immediate, par->connection_id);
   1223  1.1   agc 
   1224  1.1   agc 	if (datalen) {
   1225  1.1   agc 		unmap_databuf(PROCP(p), par->req.databuf, datalen);
   1226  1.1   agc 		par->req.databuf = databuf;	/* restore original addr */
   1227  1.1   agc 	}
   1228  1.1   agc 
   1229  1.1   agc 	switch (par->status) {
   1230  1.1   agc 	case ISCSI_STATUS_SUCCESS:
   1231  1.1   agc 		par->req.retsts = SCCMD_OK;
   1232  1.1   agc 		break;
   1233  1.1   agc 
   1234  1.1   agc 	case ISCSI_STATUS_TARGET_BUSY:
   1235  1.1   agc 		par->req.retsts = SCCMD_BUSY;
   1236  1.1   agc 		break;
   1237  1.1   agc 
   1238  1.1   agc 	case ISCSI_STATUS_TIMEOUT:
   1239  1.1   agc 	case ISCSI_STATUS_SOCKET_ERROR:
   1240  1.1   agc 		par->req.retsts = SCCMD_TIMEOUT;
   1241  1.1   agc 		break;
   1242  1.1   agc 
   1243  1.1   agc 	default:
   1244  1.1   agc 		par->req.retsts = (par->req.senselen_used) ? SCCMD_SENSE
   1245  1.1   agc 												   : SCCMD_UNKNOWN;
   1246  1.1   agc 		break;
   1247  1.1   agc 	}
   1248  1.1   agc }
   1249  1.1   agc #endif
   1250  1.1   agc 
   1251  1.1   agc /*
   1252  1.1   agc  * send_targets:
   1253  1.1   agc  *    Handle the send_targets ioctl.
   1254  1.1   agc  *    Note: If the passed buffer is too small to hold the complete response,
   1255  1.1   agc  *    the response is kept in the session structure so it can be
   1256  1.1   agc  *    retrieved with the next call to this function without having to go to
   1257  1.1   agc  *    the target again. Once the complete response has been retrieved, it
   1258  1.1   agc  *    is discarded.
   1259  1.1   agc  *
   1260  1.1   agc  *    Parameter:
   1261  1.1   agc  *          par      IN/OUT: The send_targets parameters
   1262  1.1   agc  */
   1263  1.1   agc 
   1264  1.1   agc STATIC void
   1265  1.1   agc send_targets(iscsi_send_targets_parameters_t *par)
   1266  1.1   agc {
   1267  1.1   agc 	int rc;
   1268  1.1   agc 	uint32_t rlen, cplen;
   1269  1.1   agc 	session_t *session;
   1270  1.1   agc 
   1271  1.1   agc 	if ((session = find_session(par->session_id)) == NULL) {
   1272  1.1   agc 		DEBOUT(("Session %d not found\n", par->session_id));
   1273  1.1   agc 		par->status = ISCSI_STATUS_INVALID_SESSION_ID;
   1274  1.1   agc 		return;
   1275  1.1   agc 	}
   1276  1.1   agc 
   1277  1.1   agc 	DEB(9, ("ISCSI: send_targets, rsp_size=%d; Saved list: %p\n",
   1278  1.1   agc 			par->response_size, session->target_list));
   1279  1.1   agc 
   1280  1.1   agc 	if (session->target_list == NULL) {
   1281  1.1   agc 		rc = send_send_targets(session, par->key);
   1282  1.1   agc 		if (rc) {
   1283  1.1   agc 			par->status = rc;
   1284  1.1   agc 			return;
   1285  1.1   agc 		}
   1286  1.1   agc 	}
   1287  1.1   agc 	rlen = session->target_list_len;
   1288  1.1   agc 	par->response_total = rlen;
   1289  1.1   agc 	cplen = min(par->response_size, rlen);
   1290  1.1   agc 	if (cplen) {
   1291  1.1   agc 		copyout(session->target_list, par->response_buffer, cplen);
   1292  1.1   agc 	}
   1293  1.1   agc 	par->response_used = cplen;
   1294  1.1   agc 
   1295  1.1   agc 	/* If all of the response was copied, don't keep it around */
   1296  1.1   agc 	if (rlen && par->response_used == rlen) {
   1297  1.1   agc 		free(session->target_list, M_TEMP);
   1298  1.1   agc 		session->target_list = NULL;
   1299  1.1   agc 	}
   1300  1.1   agc 
   1301  1.1   agc 	par->status = ISCSI_STATUS_SUCCESS;
   1302  1.1   agc }
   1303  1.1   agc 
   1304  1.1   agc 
   1305  1.1   agc /*
   1306  1.1   agc  * set_node_name:
   1307  1.1   agc  *    Handle the set_node_name ioctl.
   1308  1.1   agc  *
   1309  1.1   agc  *    Parameter:
   1310  1.1   agc  *          par      IN/OUT: The set_node_name parameters
   1311  1.1   agc  */
   1312  1.1   agc 
   1313  1.1   agc STATIC void
   1314  1.1   agc set_node_name(iscsi_set_node_name_parameters_t *par)
   1315  1.1   agc {
   1316  1.1   agc 
   1317  1.1   agc 	if (strlen(par->InitiatorName) >= ISCSI_STRING_LENGTH ||
   1318  1.1   agc 		strlen(par->InitiatorAlias) >= ISCSI_STRING_LENGTH) {
   1319  1.1   agc 		DEBOUT(("*** set_node_name string too long!\n"));
   1320  1.1   agc 		par->status = ISCSI_STATUS_PARAMETER_INVALID;
   1321  1.1   agc 		return;
   1322  1.1   agc 	}
   1323  1.1   agc 	strlcpy(InitiatorName, par->InitiatorName, sizeof(InitiatorName));
   1324  1.1   agc 	strlcpy(InitiatorAlias, par->InitiatorAlias, sizeof(InitiatorAlias));
   1325  1.1   agc 	memcpy(&InitiatorISID, par->ISID, 6);
   1326  1.1   agc 	DEB(5, ("ISCSI: set_node_name, ISID A=%x, B=%x, C=%x, D=%x\n",
   1327  1.1   agc 			InitiatorISID.ISID_A, InitiatorISID.ISID_B,
   1328  1.1   agc 			InitiatorISID.ISID_C, InitiatorISID.ISID_D));
   1329  1.1   agc 
   1330  1.1   agc 	if (!InitiatorISID.ISID_A && !InitiatorISID.ISID_B &&
   1331  1.1   agc 		!InitiatorISID.ISID_C && !InitiatorISID.ISID_D) {
   1332  1.1   agc 		InitiatorISID.ISID_A = T_FORMAT_EN;
   1333  1.1   agc 		InitiatorISID.ISID_B = htons(0x1);
   1334  1.1   agc 		InitiatorISID.ISID_C = 0x37;
   1335  1.1   agc 		InitiatorISID.ISID_D = 0;
   1336  1.1   agc 	}
   1337  1.1   agc 
   1338  1.1   agc 	par->status = ISCSI_STATUS_SUCCESS;
   1339  1.1   agc }
   1340  1.1   agc 
   1341  1.1   agc 
   1342  1.1   agc /*
   1343  1.1   agc  * connection_status:
   1344  1.1   agc  *    Handle the connection_status ioctl.
   1345  1.1   agc  *
   1346  1.1   agc  *    Parameter:
   1347  1.1   agc  *          par      IN/OUT: The status parameters
   1348  1.1   agc  */
   1349  1.1   agc 
   1350  1.1   agc STATIC void
   1351  1.1   agc connection_status(iscsi_conn_status_parameters_t *par)
   1352  1.1   agc {
   1353  1.1   agc 	connection_t *conn;
   1354  1.1   agc 	session_t *session;
   1355  1.1   agc 
   1356  1.1   agc 	if ((session = find_session(par->session_id)) == NULL) {
   1357  1.1   agc 		par->status = ISCSI_STATUS_INVALID_SESSION_ID;
   1358  1.1   agc 		return;
   1359  1.1   agc 	}
   1360  1.1   agc 
   1361  1.1   agc 	if (par->connection_id) {
   1362  1.1   agc 		conn = find_connection(session, par->connection_id);
   1363  1.1   agc 	} else {
   1364  1.1   agc 		conn = TAILQ_FIRST(&session->conn_list);
   1365  1.1   agc 	}
   1366  1.1   agc 	par->status = (conn == NULL) ? ISCSI_STATUS_INVALID_CONNECTION_ID :
   1367  1.1   agc 					ISCSI_STATUS_SUCCESS;
   1368  1.1   agc 	DEB(9, ("ISCSI: connection_status, session %d connection %d --> %d\n",
   1369  1.1   agc 			par->session_id, par->connection_id, par->status));
   1370  1.1   agc }
   1371  1.1   agc 
   1372  1.1   agc 
   1373  1.1   agc /*
   1374  1.1   agc  * get_version:
   1375  1.1   agc  *    Handle the get_version ioctl.
   1376  1.1   agc  *
   1377  1.1   agc  *    Parameter:
   1378  1.1   agc  *          par      IN/OUT: The version parameters
   1379  1.1   agc  */
   1380  1.1   agc 
   1381  1.1   agc STATIC void
   1382  1.1   agc get_version(iscsi_get_version_parameters_t *par)
   1383  1.1   agc {
   1384  1.1   agc 	par->status = ISCSI_STATUS_SUCCESS;
   1385  1.1   agc 	par->interface_version = INTERFACE_VERSION;
   1386  1.1   agc 	par->major = VERSION_MAJOR;
   1387  1.1   agc 	par->minor = VERSION_MINOR;
   1388  1.1   agc 	strlcpy(par->version_string, VERSION_STRING,
   1389  1.1   agc 		sizeof(par->version_string));
   1390  1.1   agc }
   1391  1.1   agc 
   1392  1.1   agc 
   1393  1.1   agc /* -------------------------------------------------------------------- */
   1394  1.1   agc 
   1395  1.1   agc /*
   1396  1.1   agc  * kill_all_sessions:
   1397  1.1   agc  *    Terminate all sessions (called when the driver unloads).
   1398  1.1   agc  */
   1399  1.1   agc 
   1400  1.1   agc void
   1401  1.1   agc kill_all_sessions(void)
   1402  1.1   agc {
   1403  1.1   agc 	session_t *sess;
   1404  1.1   agc 
   1405  1.1   agc 	while ((sess = TAILQ_FIRST(&sessions)) != NULL) {
   1406  1.1   agc 		kill_session(sess, ISCSI_STATUS_DRIVER_UNLOAD, LOGOUT_SESSION,
   1407  1.1   agc 				FALSE);
   1408  1.1   agc 	}
   1409  1.1   agc }
   1410  1.1   agc 
   1411  1.1   agc /*
   1412  1.1   agc  * handle_connection_error:
   1413  1.1   agc  *    Deal with a problem during send or receive.
   1414  1.1   agc  *
   1415  1.1   agc  *    Parameter:
   1416  1.1   agc  *       conn        The connection the problem is associated with
   1417  1.1   agc  *       status      The status code to insert into any unfinished CCBs
   1418  1.1   agc  *       dologout    Whether Logout should be attempted
   1419  1.1   agc  */
   1420  1.1   agc 
   1421  1.1   agc void
   1422  1.1   agc handle_connection_error(connection_t *conn, uint32_t status, int dologout)
   1423  1.1   agc {
   1424  1.1   agc 
   1425  1.1   agc 	DEBC(conn, 0, ("*** Connection Error, status=%d, logout=%d, state=%d\n",
   1426  1.1   agc 				   status, dologout, conn->state));
   1427  1.1   agc 
   1428  1.1   agc 	if (!conn->terminating && conn->state <= ST_LOGOUT_SENT) {
   1429  1.1   agc 		/* if we get an error while winding down, escalate it */
   1430  1.1   agc 		if (dologout >= 0 && conn->state >= ST_WINDING_DOWN) {
   1431  1.1   agc 			dologout = NO_LOGOUT;
   1432  1.1   agc 		}
   1433  1.1   agc 		kill_connection(conn, status, dologout, TRUE);
   1434  1.1   agc 	}
   1435  1.1   agc }
   1436  1.1   agc 
   1437  1.1   agc 
   1438  1.1   agc /*
   1439  1.1   agc  * iscsi_cleanup_thread
   1440  1.1   agc  *    Global thread to handle connection and session cleanup after termination.
   1441  1.1   agc  */
   1442  1.1   agc 
   1443  1.1   agc void
   1444  1.1   agc iscsi_cleanup_thread(void *par)
   1445  1.1   agc {
   1446  1.1   agc 	int s, rc;
   1447  1.1   agc 	connection_t *conn;
   1448  1.1   agc 	session_t *sess;
   1449  1.1   agc 	uint32_t status;
   1450  1.1   agc 
   1451  1.1   agc 	s = splbio();
   1452  1.1   agc 	while ((conn = TAILQ_FIRST(&cleanup_list)) != NULL ||
   1453  1.1   agc 		num_send_threads ||
   1454  1.1   agc 		!detaching) {
   1455  1.1   agc 		if (conn != NULL) {
   1456  1.1   agc 			TAILQ_REMOVE(&cleanup_list, conn, connections);
   1457  1.1   agc 			splx(s);
   1458  1.1   agc 
   1459  1.1   agc 			sess = conn->session;
   1460  1.1   agc 			status = conn->terminating;
   1461  1.1   agc 
   1462  1.1   agc 			DEBC(conn, 5, ("Cleanup: Waiting for threads to exit\n"));
   1463  1.1   agc 			while (conn->sendproc || conn->rcvproc)
   1464  1.1   agc 				tsleep(conn, PWAIT, "termwait", 20);
   1465  1.1   agc 
   1466  1.1   agc 			/* just in case any CCB is still being processed */
   1467  1.1   agc 			/* that references this connection */
   1468  1.1   agc 			tsleep(conn, PWAIT, "finalwait", 20);
   1469  1.1   agc 
   1470  1.1   agc 			free(conn, M_DEVBUF);
   1471  1.1   agc 
   1472  1.1   agc 			if (!(--sess->total_connections)) {
   1473  1.1   agc 				/* just in case */
   1474  1.1   agc 				unmap_session(sess);
   1475  1.1   agc 
   1476  1.1   agc 				/* unlink and free the session */
   1477  1.1   agc 				if (sess->sessions.tqe_next != NULL ||
   1478  1.1   agc 					sess->sessions.tqe_prev != NULL)
   1479  1.1   agc 					TAILQ_REMOVE(&sessions, sess, sessions);
   1480  1.1   agc 
   1481  1.1   agc 				if (sess->target_list != NULL)
   1482  1.1   agc 					free(sess->target_list, M_TEMP);
   1483  1.1   agc 
   1484  1.1   agc 				/* notify event handlers of session shutdown */
   1485  1.1   agc 				add_event(ISCSI_SESSION_TERMINATED, sess->id, 0, status);
   1486  1.1   agc 
   1487  1.1   agc 				free(sess, M_DEVBUF);
   1488  1.1   agc 			}
   1489  1.1   agc 
   1490  1.1   agc 			DEB(5, ("Cleanup: Done\n"));
   1491  1.1   agc 
   1492  1.1   agc 			s = splbio();
   1493  1.1   agc 		} else {
   1494  1.1   agc 			/* Go to sleep, but wake up every 30 seconds to check for */
   1495  1.1   agc 			/* dead event handlers */
   1496  1.1   agc 			splx(s);
   1497  1.1   agc 			rc = tsleep(&cleanup_list, PWAIT, "cleanup",
   1498  1.1   agc 				(TAILQ_FIRST(&event_handlers)) ? 30 * hz : 0);
   1499  1.1   agc 			s = splbio();
   1500  1.1   agc 			/* if timed out, not woken up */
   1501  1.1   agc 			if (rc == EWOULDBLOCK)
   1502  1.1   agc 				check_event_handlers();
   1503  1.1   agc 		}
   1504  1.1   agc 	}
   1505  1.1   agc 	splx(s);
   1506  1.1   agc 
   1507  1.1   agc 	add_event(ISCSI_DRIVER_TERMINATING, 0, 0, ISCSI_STATUS_DRIVER_UNLOAD);
   1508  1.1   agc 
   1509  1.1   agc 	/*
   1510  1.1   agc      * Wait for all event handlers to deregister, but don't wait more
   1511  1.1   agc      * than 1 minute (assume registering app has died if it takes longer).
   1512  1.1   agc 	 */
   1513  1.1   agc 	for (s = 0; TAILQ_FIRST(&event_handlers) != NULL && s < 60; s++)
   1514  1.1   agc 		tsleep(&s, PWAIT, "waiteventclr", hz);
   1515  1.1   agc 
   1516  1.1   agc 	cleanproc = NULL;
   1517  1.1   agc 	DEB(5, ("Cleanup thread exits\n"));
   1518  1.1   agc 	kthread_exit(0);
   1519  1.1   agc }
   1520  1.1   agc 
   1521  1.1   agc 
   1522  1.1   agc /* -------------------------------------------------------------------- */
   1523  1.1   agc 
   1524  1.1   agc /*
   1525  1.1   agc  * iscsi_ioctl:
   1526  1.1   agc  *    Driver ioctl entry.
   1527  1.1   agc  *
   1528  1.1   agc  *    Parameter:
   1529  1.1   agc  *       dev      The device (ignored)
   1530  1.1   agc  *       cmd      The ioctl Command
   1531  1.1   agc  *       addr     IN/OUT: The command parameter
   1532  1.1   agc  *       flag     Flags (ignored)
   1533  1.1   agc  *       p        IN: The thread object of the caller
   1534  1.1   agc  */
   1535  1.1   agc 
   1536  1.1   agc int
   1537  1.1   agc iscsiioctl(dev_t dev, u_long cmd, void *addr, int flag, PTHREADOBJ p)
   1538  1.1   agc {
   1539  1.1   agc 
   1540  1.1   agc 	DEB(99, ("ISCSI Ioctl cmd = %x\n", (int) cmd));
   1541  1.1   agc 
   1542  1.1   agc 	switch (cmd) {
   1543  1.1   agc 	case ISCSI_GET_VERSION:
   1544  1.1   agc 		get_version((iscsi_get_version_parameters_t *) addr);
   1545  1.1   agc 		break;
   1546  1.1   agc 
   1547  1.1   agc 	case ISCSI_LOGIN:
   1548  1.1   agc 		login((iscsi_login_parameters_t *) addr, p);
   1549  1.1   agc 		break;
   1550  1.1   agc 
   1551  1.1   agc 	case ISCSI_ADD_CONNECTION:
   1552  1.1   agc 		add_connection((iscsi_login_parameters_t *) addr, p);
   1553  1.1   agc 		break;
   1554  1.1   agc 
   1555  1.1   agc 	case ISCSI_RESTORE_CONNECTION:
   1556  1.1   agc 		restore_connection((iscsi_login_parameters_t *) addr, p);
   1557  1.1   agc 		break;
   1558  1.1   agc 
   1559  1.1   agc 	case ISCSI_LOGOUT:
   1560  1.1   agc 		logout((iscsi_logout_parameters_t *) addr);
   1561  1.1   agc 		break;
   1562  1.1   agc 
   1563  1.1   agc 	case ISCSI_REMOVE_CONNECTION:
   1564  1.1   agc 		remove_connection((iscsi_remove_parameters_t *) addr);
   1565  1.1   agc 		break;
   1566  1.1   agc 
   1567  1.1   agc #ifndef ISCSI_MINIMAL
   1568  1.1   agc 	case ISCSI_IO_COMMAND:
   1569  1.1   agc 		io_command((iscsi_iocommand_parameters_t *) addr, p);
   1570  1.1   agc 		break;
   1571  1.1   agc #endif
   1572  1.1   agc 
   1573  1.1   agc 	case ISCSI_SEND_TARGETS:
   1574  1.1   agc 		send_targets((iscsi_send_targets_parameters_t *) addr);
   1575  1.1   agc 		break;
   1576  1.1   agc 
   1577  1.1   agc 	case ISCSI_SET_NODE_NAME:
   1578  1.1   agc 		set_node_name((iscsi_set_node_name_parameters_t *) addr);
   1579  1.1   agc 		break;
   1580  1.1   agc 
   1581  1.1   agc 	case ISCSI_CONNECTION_STATUS:
   1582  1.1   agc 		connection_status((iscsi_conn_status_parameters_t *) addr);
   1583  1.1   agc 		break;
   1584  1.1   agc 
   1585  1.1   agc 	case ISCSI_REGISTER_EVENT:
   1586  1.1   agc 		register_event((iscsi_register_event_parameters_t *) addr);
   1587  1.1   agc 		break;
   1588  1.1   agc 
   1589  1.1   agc 	case ISCSI_DEREGISTER_EVENT:
   1590  1.1   agc 		deregister_event((iscsi_register_event_parameters_t *) addr);
   1591  1.1   agc 		break;
   1592  1.1   agc 
   1593  1.1   agc 	case ISCSI_WAIT_EVENT:
   1594  1.1   agc 		check_event((iscsi_wait_event_parameters_t *) addr, TRUE);
   1595  1.1   agc 		break;
   1596  1.1   agc 
   1597  1.1   agc 	case ISCSI_POLL_EVENT:
   1598  1.1   agc 		check_event((iscsi_wait_event_parameters_t *) addr, FALSE);
   1599  1.1   agc 		break;
   1600  1.1   agc 
   1601  1.1   agc #ifdef ISCSI_PERFTEST
   1602  1.1   agc 	case ISCSI_PERFDATA_START:
   1603  1.1   agc 		perf_start((iscsi_perf_startstop_parameters_t *) addr);
   1604  1.1   agc 		break;
   1605  1.1   agc 
   1606  1.1   agc 	case ISCSI_PERFDATA_STOP:
   1607  1.1   agc 		perf_stop((iscsi_perf_startstop_parameters_t *) addr);
   1608  1.1   agc 		break;
   1609  1.1   agc 
   1610  1.1   agc 	case ISCSI_PERFDATA_GET:
   1611  1.1   agc 		perf_get((iscsi_perf_get_parameters_t *) addr);
   1612  1.1   agc 		break;
   1613  1.1   agc #endif
   1614  1.1   agc 
   1615  1.1   agc #ifdef ISCSI_TEST_MODE
   1616  1.1   agc 	case ISCSI_TEST_DEFINE:
   1617  1.1   agc 		test_define((iscsi_test_define_parameters_t *) addr);
   1618  1.1   agc 		break;
   1619  1.1   agc 
   1620  1.1   agc 	case ISCSI_TEST_ADD_NEGOTIATION:
   1621  1.1   agc 		test_add_neg((iscsi_test_add_negotiation_parameters_t *) addr);
   1622  1.1   agc 		break;
   1623  1.1   agc 
   1624  1.1   agc 	case ISCSI_TEST_ADD_MODIFICATION:
   1625  1.1   agc 		test_add_mod(PROCP(p), (iscsi_test_add_modification_parameters_t *) addr);
   1626  1.1   agc 		break;
   1627  1.1   agc 
   1628  1.1   agc 	case ISCSI_TEST_SEND_PDU:
   1629  1.1   agc 		test_send_pdu(PROCP(p), (iscsi_test_send_pdu_parameters_t *) addr);
   1630  1.1   agc 		break;
   1631  1.1   agc 
   1632  1.1   agc 	case ISCSI_TEST_CANCEL:
   1633  1.1   agc 		test_cancel((iscsi_test_cancel_parameters_t *) addr);
   1634  1.1   agc 		break;
   1635  1.1   agc #endif
   1636  1.1   agc 
   1637  1.1   agc 	default:
   1638  1.1   agc 		DEBOUT(("Invalid IO-Control Code\n"));
   1639  1.1   agc 		return ENOTTY;
   1640  1.1   agc 	}
   1641  1.1   agc 
   1642  1.1   agc     /*
   1643  1.1   agc      * NOTE: We return 0 even if the function fails as long as the ioctl code
   1644  1.1   agc      * is good, so the status code is copied back to the caller.
   1645  1.1   agc 	 */
   1646  1.1   agc 	return 0;
   1647  1.1   agc }
   1648