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