Home | History | Annotate | Line # | Download | only in iscsi
iscsi_main.c revision 1.29
      1 /*	$NetBSD: iscsi_main.c,v 1.29 2019/04/21 11:26:46 mlelstv Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2004,2005,2006,2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Wasabi Systems, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include "iscsi_globals.h"
     32 
     33 #include <sys/systm.h>
     34 #include <sys/buf.h>
     35 #include <sys/file.h>
     36 #include <sys/filedesc.h>
     37 #include <sys/kmem.h>
     38 #include <sys/socketvar.h>
     39 #include <sys/sysctl.h>
     40 
     41 #include "ioconf.h"
     42 
     43 /*------------------------- Global Variables ------------------------*/
     44 
     45 extern struct cfdriver iscsi_cd;
     46 
     47 #if defined(ISCSI_DEBUG)
     48 int iscsi_debug_level = ISCSI_DEBUG;
     49 #endif
     50 bool iscsi_hex_bignums = false;
     51 
     52 bool iscsi_detaching;
     53 
     54 /* the list of sessions */
     55 session_list_t iscsi_sessions = TAILQ_HEAD_INITIALIZER(iscsi_sessions);
     56 
     57 /* the number of active send threads (for cleanup thread) */
     58 uint32_t iscsi_num_send_threads = 0;
     59 
     60 /* Our node name, alias, and ISID */
     61 uint8_t iscsi_InitiatorName[ISCSI_STRING_LENGTH] = "";
     62 uint8_t iscsi_InitiatorAlias[ISCSI_STRING_LENGTH] = "";
     63 login_isid_t iscsi_InitiatorISID;
     64 
     65 /******************************************************************************/
     66 
     67 /*
     68    System interface: autoconf and device structures
     69 */
     70 
     71 static void iscsi_attach(device_t parent, device_t self, void *aux);
     72 static int iscsi_match(device_t, cfdata_t, void *);
     73 static int iscsi_detach(device_t, int);
     74 
     75 struct iscsi_softc {
     76 	device_t		dev;
     77 	kmutex_t		lock;
     78 	TAILQ_HEAD(, iscsifd)	fds;
     79 };
     80 
     81 CFATTACH_DECL_NEW(iscsi, sizeof(struct iscsi_softc), iscsi_match, iscsi_attach,
     82 			  iscsi_detach, NULL);
     83 
     84 
     85 static dev_type_open(iscsiopen);
     86 static int iscsiclose(struct file *);
     87 
     88 static const struct fileops iscsi_fileops = {
     89 	.fo_name = "iscsi",
     90 	.fo_ioctl = iscsiioctl,
     91 	.fo_close = iscsiclose,
     92 };
     93 
     94 struct cdevsw iscsi_cdevsw = {
     95 	.d_open = iscsiopen,
     96 	.d_close = noclose,
     97 	.d_read = noread,
     98 	.d_write = nowrite,
     99 	.d_ioctl = noioctl,
    100 	.d_stop = nostop,
    101 	.d_tty = notty,
    102 	.d_poll = nopoll,
    103 	.d_mmap = nommap,
    104 	.d_kqfilter = nokqfilter,
    105 	.d_discard = nodiscard,
    106 	.d_flag = D_OTHER
    107 };
    108 
    109 /******************************************************************************/
    110 
    111 STATIC void iscsi_scsipi_request(struct scsipi_channel *,
    112                                  scsipi_adapter_req_t, void *);
    113 STATIC void iscsi_minphys(struct buf *);
    114 
    115 /******************************************************************************/
    116 
    117 /*******************************************************************************
    118 * Open and Close device interfaces. We don't really need them, because we don't
    119 * have to keep track of device opens and closes from userland. But apps can't
    120 * call ioctl without a handle to the device, and the kernel doesn't hand out
    121 * handles without an open routine in the driver. So here they are in all their
    122 * glory...
    123 *******************************************************************************/
    124 
    125 int
    126 iscsiopen(dev_t dev, int flag, int mode, struct lwp *l)
    127 {
    128 	struct iscsifd *d;
    129 	struct iscsi_softc *sc;
    130 	struct file *fp;
    131 	int error, fd, unit;
    132 
    133 	unit = minor(dev);
    134 
    135 	DEB(99, ("ISCSI Open unit=%d\n",unit));
    136 
    137 	sc = device_lookup_private(&iscsi_cd, unit);
    138 	if (sc == NULL)
    139 		return ENXIO;
    140 
    141 	if ((error = fd_allocfile(&fp, &fd)) != 0)
    142 		return error;
    143 
    144 	d = kmem_alloc(sizeof(*d), KM_SLEEP);
    145 	d->fd_dev = sc->dev;
    146 	d->fd_unit = unit;
    147 
    148 	mutex_enter(&sc->lock);
    149 	if (iscsi_detaching) {
    150 		mutex_exit(&sc->lock);
    151 		kmem_free(d, sizeof(*d));
    152 		DEB(99, ("ISCSI Open aborting\n"));
    153 		fd_abort(curproc, fp, fd);
    154 		return ENXIO;
    155 	}
    156 	TAILQ_INSERT_TAIL(&sc->fds, d, fd_link);
    157 	mutex_exit(&sc->lock);
    158 
    159 	return fd_clone(fp, fd, flag, &iscsi_fileops, d);
    160 }
    161 
    162 static int
    163 iscsiclose(struct file *fp)
    164 {
    165 	struct iscsifd *d = fp->f_iscsi;
    166 	struct iscsi_softc *sc;
    167 
    168 	sc = device_lookup_private(&iscsi_cd, d->fd_unit);
    169 	if (sc == NULL) {
    170 		DEBOUT(("%s: Cannot find private data\n",__func__));
    171 		return ENXIO;
    172 	}
    173 
    174 	mutex_enter(&sc->lock);
    175 	TAILQ_REMOVE(&sc->fds, d, fd_link);
    176 	mutex_exit(&sc->lock);
    177 
    178 	kmem_free(d, sizeof(*d));
    179 	fp->f_iscsi = NULL;
    180 
    181 	DEB(99, ("ISCSI Close\n"));
    182 	return 0;
    183 }
    184 
    185 /******************************************************************************/
    186 
    187 /*
    188  * The config Match routine.
    189  *    Not much to do here, either - this is a pseudo-device.
    190  */
    191 
    192 static int
    193 iscsi_match(device_t self, cfdata_t cfdata, void *arg)
    194 {
    195 	return 1;
    196 }
    197 
    198 /*
    199  * iscsiattach:
    200  *    Only called when statically configured into a kernel
    201  */
    202 void
    203 iscsiattach(int n)
    204 {
    205 	int err;
    206 	cfdata_t cf;
    207 
    208 	err = config_cfattach_attach(iscsi_cd.cd_name, &iscsi_ca);
    209 	if (err) {
    210 		aprint_error("%s: couldn't register cfattach: %d\n",
    211 		    iscsi_cd.cd_name, err);
    212 		config_cfdriver_detach(&iscsi_cd);
    213 		return;
    214 	}
    215 
    216 	if (n > 1)
    217 		aprint_error("%s: only one device supported\n",
    218 		    iscsi_cd.cd_name);
    219 
    220 	cf = kmem_alloc(sizeof(struct cfdata), KM_NOSLEEP);
    221 	if (cf == NULL) {
    222 		aprint_error("%s: couldn't allocate cfdata\n",
    223 		    iscsi_cd.cd_name);
    224 		return;
    225 	}
    226 	cf->cf_name = iscsi_cd.cd_name;
    227 	cf->cf_atname = iscsi_cd.cd_name;
    228 	cf->cf_unit = 0;
    229 	cf->cf_fstate = FSTATE_NOTFOUND;
    230 
    231 	(void)config_attach_pseudo(cf);
    232 	return;
    233 }
    234 
    235 /*
    236  * iscsi_attach:
    237  *    One-time inits go here. Not much for now, probably even less later.
    238  */
    239 static void
    240 iscsi_attach(device_t parent, device_t self, void *aux)
    241 {
    242 	struct iscsi_softc *sc;
    243 
    244 	DEB(1, ("ISCSI: iscsi_attach, parent=%p, self=%p, aux=%p\n", parent,
    245 			self, aux));
    246 	sc = (struct iscsi_softc *) device_private(self);
    247 	sc->dev = self;
    248 
    249 	TAILQ_INIT(&sc->fds);
    250 	mutex_init(&sc->lock, MUTEX_DEFAULT, IPL_NONE);
    251 
    252 	iscsi_detaching = false;
    253 	iscsi_init_cleanup();
    254 
    255 	aprint_normal("%s: attached.  major = %d\n", iscsi_cd.cd_name,
    256 	    cdevsw_lookup_major(&iscsi_cdevsw));
    257 }
    258 
    259 /*
    260  * iscsi_detach:
    261  *    Cleanup.
    262  */
    263 static int
    264 iscsi_detach(device_t self, int flags)
    265 {
    266 	struct iscsi_softc *sc;
    267 	int error;
    268 
    269 	DEB(1, ("ISCSI: detach\n"));
    270 	sc = (struct iscsi_softc *) device_private(self);
    271 
    272 	mutex_enter(&sc->lock);
    273 	if (!TAILQ_EMPTY(&sc->fds)) {
    274 		mutex_exit(&sc->lock);
    275 		return EBUSY;
    276 	}
    277 	iscsi_detaching = true;
    278 	mutex_exit(&sc->lock);
    279 
    280 	error = kill_all_sessions();
    281 	if (error)
    282 		return error;
    283 
    284 	error = iscsi_destroy_cleanup();
    285 	if (error)
    286 		return error;
    287 
    288 	mutex_destroy(&sc->lock);
    289 
    290 	return 0;
    291 }
    292 
    293 /******************************************************************************/
    294 
    295 typedef struct quirktab_t {
    296 	const char	*tgt;
    297 	const char	*iqn;
    298 	uint32_t	 quirks;
    299 } quirktab_t;
    300 
    301 static const quirktab_t	quirktab[] = {
    302 	{ "StarWind", "iqn.2008-08.com.starwindsoftware", PQUIRK_ONLYBIG },
    303 	{ "UNH", "iqn.2002-10.edu.unh.",
    304 	    PQUIRK_NOBIGMODESENSE |
    305 	    PQUIRK_NOMODESENSE |
    306 	    PQUIRK_NOSYNCCACHE },
    307 	{ "NetBSD", "iqn.1994-04.org.netbsd.", 0 },
    308 	{ "Unknown", "unknown", 0 },
    309 	{ NULL, NULL, 0 }
    310 };
    311 
    312 /* loop through the quirktab looking for a match on target name */
    313 static const quirktab_t *
    314 getquirks(const char *iqn)
    315 {
    316 	const quirktab_t	*qp;
    317 	size_t iqnlen, quirklen;
    318 
    319 	if (iqn == NULL)
    320 		iqn = "unknown";
    321 	iqnlen = strlen(iqn);
    322 	for (qp = quirktab ; qp->iqn ; qp++) {
    323 		quirklen = strlen(qp->iqn);
    324 		if (quirklen > iqnlen)
    325 			continue;
    326 		if (memcmp(qp->iqn, iqn, quirklen) == 0)
    327 			break;
    328 	}
    329 	return qp;
    330 }
    331 
    332 /******************************************************************************/
    333 
    334 /*
    335  * map_session
    336  *    This (indirectly) maps the existing LUNs for a target to SCSI devices
    337  *    by going through config_found to tell any child drivers that there's
    338  *    a new adapter.
    339  *    Note that each session is equivalent to a SCSI adapter.
    340  *
    341  *    Parameter:  the session pointer
    342  *
    343  *    Returns:    1 on success, 0 on failure
    344  *
    345  * ToDo: Figuring out how to handle more than one LUN. It appears that
    346  *    the NetBSD SCSI LUN discovery doesn't use "report LUNs", and instead
    347  *    goes through the LUNs sequentially, stopping somewhere on the way if it
    348  *    gets an error. We may have to do some LUN mapping in here if this is
    349  *    really how things work.
    350  */
    351 
    352 int
    353 map_session(session_t *sess, device_t dev)
    354 {
    355 	struct scsipi_adapter *adapt = &sess->s_sc_adapter;
    356 	struct scsipi_channel *chan = &sess->s_sc_channel;
    357 	const quirktab_t	*tgt;
    358 
    359 	mutex_enter(&sess->s_lock);
    360 	sess->s_send_window = max(2, window_size(sess, CCBS_FOR_SCSIPI));
    361 	mutex_exit(&sess->s_lock);
    362 
    363 	/*
    364 	 * Fill in the scsipi_adapter.
    365 	 */
    366 	adapt->adapt_dev = dev;
    367 	adapt->adapt_nchannels = 1;
    368 	adapt->adapt_request = iscsi_scsipi_request;
    369 	adapt->adapt_minphys = iscsi_minphys;
    370 	adapt->adapt_openings = sess->s_send_window;
    371 	adapt->adapt_max_periph = CCBS_FOR_SCSIPI;
    372 	adapt->adapt_flags = SCSIPI_ADAPT_MPSAFE;
    373 
    374 	/*
    375 	 * Fill in the scsipi_channel.
    376 	 */
    377 	if ((tgt = getquirks(chan->chan_name)) == NULL) {
    378 		tgt = getquirks("unknown");
    379 	}
    380 	chan->chan_name = tgt->tgt;
    381 	chan->chan_defquirks = tgt->quirks;
    382 	chan->chan_adapter = adapt;
    383 	chan->chan_bustype = &scsi_bustype;
    384 	chan->chan_channel = 0;
    385 	chan->chan_flags = SCSIPI_CHAN_NOSETTLE | SCSIPI_CHAN_CANGROW;
    386 	chan->chan_ntargets = 1;
    387 	chan->chan_nluns = 16;
    388 	chan->chan_id = sess->s_id;
    389 
    390 	sess->s_child_dev = config_found(dev, chan, scsiprint);
    391 
    392 	return sess->s_child_dev != NULL;
    393 }
    394 
    395 
    396 /*
    397  * unmap_session
    398  *    This (indirectly) unmaps the existing all LUNs for a target by
    399  *    telling the config system that the adapter has detached.
    400  *
    401  *    Parameter:  the session pointer
    402  *
    403  *    Returns:    1 on success, 0 on failure
    404  */
    405 
    406 int
    407 unmap_session(session_t *sess)
    408 {
    409 	device_t dev;
    410 	int rv = 1;
    411 
    412 	if ((dev = sess->s_child_dev) != NULL) {
    413 		sess->s_child_dev = NULL;
    414 		if (config_detach(dev, 0))
    415 			rv = 0;
    416 	}
    417 
    418 	return rv;
    419 }
    420 
    421 /*
    422  * grow_resources
    423  *    Try to grow openings up to current window size
    424  */
    425 static void
    426 grow_resources(session_t *sess)
    427 {
    428 	struct scsipi_adapter *adapt = &sess->s_sc_adapter;
    429 	int win;
    430 
    431 	mutex_enter(&sess->s_lock);
    432 	if (sess->s_refcount < CCBS_FOR_SCSIPI &&
    433 	    sess->s_send_window < CCBS_FOR_SCSIPI) {
    434 		win = window_size(sess, CCBS_FOR_SCSIPI - sess->s_refcount);
    435 		if (win > sess->s_send_window) {
    436 			sess->s_send_window++;
    437 			adapt->adapt_openings++;
    438 			DEB(5, ("Grow send window to %d\n", sess->s_send_window));
    439 		}
    440 	}
    441 	mutex_exit(&sess->s_lock);
    442 }
    443 
    444 /******************************************************************************/
    445 
    446 /*****************************************************************************
    447  * SCSI interface routines
    448  *****************************************************************************/
    449 
    450 /*
    451  * iscsi_scsipi_request:
    452  *    Perform a request for the SCSIPI layer.
    453  */
    454 
    455 void
    456 iscsi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    457 					 void *arg)
    458 {
    459 	struct scsipi_adapter *adapt = chan->chan_adapter;
    460 	struct scsipi_xfer *xs;
    461 	session_t *sess;
    462 	int flags;
    463 	struct scsipi_xfer_mode *xm;
    464 	int error;
    465 
    466 	sess = (session_t *) adapt;	/* adapter is first field in session */
    467 
    468 	error = ref_session(sess);
    469 
    470 	switch (req) {
    471 	case ADAPTER_REQ_RUN_XFER:
    472 		DEB(9, ("ISCSI: scsipi_request RUN_XFER\n"));
    473 		xs = arg;
    474 		flags = xs->xs_control;
    475 
    476 		if (error) {
    477 			DEB(9, ("ISCSI: refcount too high: %d, winsize %d\n",
    478 				sess->s_refcount, sess->s_send_window));
    479 			xs->error = XS_BUSY;
    480 			xs->status = XS_BUSY;
    481 			scsipi_done(xs);
    482 			return;
    483 		}
    484 
    485 		if ((flags & XS_CTL_POLL) != 0) {
    486 			xs->error = XS_DRIVER_STUFFUP;
    487 			DEBOUT(("Run Xfer request with polling\n"));
    488 			scsipi_done(xs);
    489 			break;
    490 		}
    491 		/*
    492 		 * NOTE: It appears that XS_CTL_DATA_UIO is not actually used anywhere.
    493 		 * Since it really would complicate matters to handle offsets
    494 		 * into scatter-gather lists, and a number of other drivers don't
    495 		 * handle uio-based data as well, XS_CTL_DATA_UIO isn't
    496 		 * implemented in this driver (at least for now).
    497 		 */
    498 		if (flags & XS_CTL_DATA_UIO) {
    499 			xs->error = XS_DRIVER_STUFFUP;
    500 			DEBOUT(("Run Xfer with data in UIO\n"));
    501 			scsipi_done(xs);
    502 			break;
    503 		}
    504 
    505 		send_run_xfer(sess, xs);
    506 		DEB(15, ("scsipi_req returns, refcount = %d\n", sess->s_refcount));
    507 		return;
    508 
    509 	case ADAPTER_REQ_GROW_RESOURCES:
    510 		DEB(5, ("ISCSI: scsipi_request GROW_RESOURCES\n"));
    511 		grow_resources(sess);
    512 		break;
    513 
    514 	case ADAPTER_REQ_SET_XFER_MODE:
    515 		DEB(5, ("ISCSI: scsipi_request SET_XFER_MODE\n"));
    516 		xm = (struct scsipi_xfer_mode *)arg;
    517 		xm->xm_mode = PERIPH_CAP_TQING;
    518 		scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
    519 		break;
    520 
    521 	default:
    522 		DEBOUT(("ISCSI: scsipi_request with invalid REQ code %d\n", req));
    523 		break;
    524 	}
    525 
    526 	if (!error)
    527 		unref_session(sess);
    528 }
    529 
    530 /* cap the transfer at 64K */
    531 #define ISCSI_MAX_XFER	65536
    532 
    533 /*
    534  * iscsi_minphys:
    535  *    Limit a transfer to our maximum transfer size.
    536  */
    537 
    538 void
    539 iscsi_minphys(struct buf *bp)
    540 {
    541 	if (bp->b_bcount > ISCSI_MAX_XFER) {
    542 		bp->b_bcount = ISCSI_MAX_XFER;
    543 	}
    544 }
    545 
    546 /*****************************************************************************
    547  * SCSI job execution helper routines
    548  *****************************************************************************/
    549 
    550 /*
    551  * iscsi_done:
    552  *
    553  * A CCB has completed execution.  Pass the status back to the
    554  * upper layer.
    555  */
    556 void
    557 iscsi_done(ccb_t *ccb)
    558 {
    559 	struct scsipi_xfer *xs = ccb->ccb_xs;
    560 	DEB(9, ("iscsi_done\n"));
    561 
    562 	if (xs != NULL) {
    563 		xs->resid = ccb->ccb_residual;
    564 		ccb->ccb_xs = NULL;
    565 		xs->resid = ccb->ccb_residual;
    566 
    567 		switch (ccb->ccb_status) {
    568 		case ISCSI_STATUS_SUCCESS:
    569 			xs->error = XS_NOERROR;
    570 			xs->status = SCSI_OK;
    571 			break;
    572 
    573 		case ISCSI_STATUS_CHECK_CONDITION:
    574 			xs->error = XS_SENSE;
    575 			xs->status = SCSI_CHECK;
    576 			break;
    577 
    578 		case ISCSI_STATUS_TARGET_BUSY:
    579 		case ISCSI_STATUS_NO_RESOURCES:
    580 			DEBC(ccb->ccb_connection, 5, ("target busy, ccb %p\n", ccb));
    581 			xs->error = XS_BUSY;
    582 			xs->status = SCSI_BUSY;
    583 			break;
    584 
    585 		case ISCSI_STATUS_SOCKET_ERROR:
    586 		case ISCSI_STATUS_TIMEOUT:
    587 			xs->error = XS_SELTIMEOUT;
    588 			xs->status = SCSI_BUSY;
    589 			break;
    590 
    591 		case ISCSI_STATUS_QUEUE_FULL:
    592 			DEBC(ccb->ccb_connection, 5, ("queue full, ccb %p\n", ccb));
    593 			xs->error = XS_BUSY;
    594 			xs->status = SCSI_QUEUE_FULL;
    595 			break;
    596 
    597 		default:
    598 			xs->error = XS_DRIVER_STUFFUP;
    599 			break;
    600 		}
    601 
    602 		unref_session(ccb->ccb_session);
    603 
    604 		DEB(99, ("Calling scsipi_done (%p), err = %d\n", xs, xs->error));
    605 		scsipi_done(xs);
    606 		DEB(99, ("scsipi_done returned\n"));
    607 	} else {
    608 		DEBOUT(("ISCSI: iscsi_done CCB %p without XS\n", ccb));
    609 	}
    610 }
    611 
    612 SYSCTL_SETUP(sysctl_iscsi_setup, "ISCSI subtree setup")
    613 {
    614 	const struct sysctlnode *node = NULL;
    615 
    616 	sysctl_createv(clog, 0, NULL, &node,
    617 		CTLFLAG_PERMANENT,
    618 		CTLTYPE_NODE, "iscsi",
    619 		SYSCTL_DESCR("iscsi controls"),
    620 		NULL, 0, NULL, 0,
    621 		CTL_HW, CTL_CREATE, CTL_EOL);
    622 	sysctl_createv(clog, 0, &node, NULL,
    623 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    624 		CTLTYPE_BOOL, "hexbignums",
    625 		SYSCTL_DESCR("encode parameters in hex"),
    626 		NULL, 0,  &iscsi_hex_bignums, 0,
    627 		CTL_CREATE, CTL_EOL);
    628 
    629 #ifdef ISCSI_DEBUG
    630 	sysctl_createv(clog, 0, &node, NULL,
    631 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    632 		CTLTYPE_INT, "debug",
    633 		SYSCTL_DESCR("debug level"),
    634 		NULL, 0,  &iscsi_debug_level, sizeof(iscsi_debug_level),
    635 		CTL_CREATE, CTL_EOL);
    636 #endif
    637 }
    638 
    639 
    640 /* Kernel Module support */
    641 
    642 #include <sys/module.h>
    643 
    644 MODULE(MODULE_CLASS_DRIVER, iscsi, NULL); /* Possibly a builtin module */
    645 
    646 #ifdef _MODULE
    647 static const struct cfiattrdata ibescsi_info = { "scsi", 1,
    648 	{{"channel", "-1", -1},}
    649 };
    650 
    651 static const struct cfiattrdata *const iscsi_attrs[] = { &ibescsi_info, NULL };
    652 
    653 CFDRIVER_DECL(iscsi, DV_DULL, iscsi_attrs);
    654 
    655 static struct cfdata iscsi_cfdata[] = {
    656 	{
    657 		.cf_name = "iscsi",
    658 		.cf_atname = "iscsi",
    659 		.cf_unit = 0,		/* Only unit 0 is ever used  */
    660 		.cf_fstate = FSTATE_NOTFOUND,
    661 		.cf_loc = NULL,
    662 		.cf_flags = 0,
    663 		.cf_pspec = NULL,
    664 	},
    665 	{ NULL, NULL, 0, 0, NULL, 0, NULL }
    666 };
    667 #endif
    668 
    669 static int
    670 iscsi_modcmd(modcmd_t cmd, void *arg)
    671 {
    672 #ifdef _MODULE
    673 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
    674 	int error;
    675 	static struct sysctllog *clog;
    676 #endif
    677 
    678 	switch (cmd) {
    679 	case MODULE_CMD_INIT:
    680 #ifdef _MODULE
    681 		error = config_cfdriver_attach(&iscsi_cd);
    682 		if (error) {
    683 			return error;
    684 		}
    685 
    686 		error = config_cfattach_attach(iscsi_cd.cd_name, &iscsi_ca);
    687 		if (error) {
    688 			config_cfdriver_detach(&iscsi_cd);
    689 			aprint_error("%s: unable to register cfattach\n",
    690 				iscsi_cd.cd_name);
    691 			return error;
    692 		}
    693 
    694 		error = config_cfdata_attach(iscsi_cfdata, 1);
    695 		if (error) {
    696 			aprint_error("%s: unable to attach cfdata\n",
    697 				iscsi_cd.cd_name);
    698 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    699 			config_cfdriver_detach(&iscsi_cd);
    700 			return error;
    701 		}
    702 
    703 		error = devsw_attach(iscsi_cd.cd_name, NULL, &bmajor,
    704 			&iscsi_cdevsw, &cmajor);
    705 		if (error) {
    706 			aprint_error("%s: unable to register devsw\n",
    707 				iscsi_cd.cd_name);
    708 			config_cfdata_detach(iscsi_cfdata);
    709 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    710 			config_cfdriver_detach(&iscsi_cd);
    711 			return error;
    712 		}
    713 
    714 		if (config_attach_pseudo(iscsi_cfdata) == NULL) {
    715 			aprint_error("%s: config_attach_pseudo failed\n",
    716 				iscsi_cd.cd_name);
    717 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    718 			config_cfdriver_detach(&iscsi_cd);
    719 			return ENXIO;
    720 		}
    721 
    722 		sysctl_iscsi_setup(&clog);
    723 #endif
    724 		return 0;
    725 		break;
    726 
    727 	case MODULE_CMD_FINI:
    728 #ifdef _MODULE
    729 		error = config_cfdata_detach(iscsi_cfdata);
    730 		if (error)
    731 			return error;
    732 
    733 		sysctl_teardown(&clog);
    734 
    735 		config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    736 		config_cfdriver_detach(&iscsi_cd);
    737 		devsw_detach(NULL, &iscsi_cdevsw);
    738 #endif
    739 		return 0;
    740 		break;
    741 
    742 	case MODULE_CMD_AUTOUNLOAD:
    743 		return EBUSY;
    744 		break;
    745 
    746 	default:
    747 		return ENOTTY;
    748 		break;
    749 	}
    750 }
    751