Home | History | Annotate | Line # | Download | only in iscsi
iscsi_main.c revision 1.30
      1 /*	$NetBSD: iscsi_main.c,v 1.30 2019/07/13 17:06:00 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 	if (!pmf_device_register(self, NULL, NULL))
    256 		aprint_error_dev(self, "couldn't establish power handler\n");
    257 
    258 	aprint_normal("%s: attached.  major = %d\n", iscsi_cd.cd_name,
    259 	    cdevsw_lookup_major(&iscsi_cdevsw));
    260 }
    261 
    262 /*
    263  * iscsi_detach:
    264  *    Cleanup.
    265  */
    266 static int
    267 iscsi_detach(device_t self, int flags)
    268 {
    269 	struct iscsi_softc *sc;
    270 	int error;
    271 
    272 	DEB(1, ("ISCSI: detach\n"));
    273 	sc = (struct iscsi_softc *) device_private(self);
    274 
    275 	mutex_enter(&sc->lock);
    276 	if (!TAILQ_EMPTY(&sc->fds)) {
    277 		mutex_exit(&sc->lock);
    278 		return EBUSY;
    279 	}
    280 	iscsi_detaching = true;
    281 	mutex_exit(&sc->lock);
    282 
    283 	error = kill_all_sessions();
    284 	if (error)
    285 		return error;
    286 
    287 	error = iscsi_destroy_cleanup();
    288 	if (error)
    289 		return error;
    290 
    291 	pmf_device_deregister(sc->dev);
    292 
    293 	mutex_destroy(&sc->lock);
    294 
    295 	return 0;
    296 }
    297 
    298 /******************************************************************************/
    299 
    300 typedef struct quirktab_t {
    301 	const char	*tgt;
    302 	const char	*iqn;
    303 	uint32_t	 quirks;
    304 } quirktab_t;
    305 
    306 static const quirktab_t	quirktab[] = {
    307 	{ "StarWind", "iqn.2008-08.com.starwindsoftware", PQUIRK_ONLYBIG },
    308 	{ "UNH", "iqn.2002-10.edu.unh.",
    309 	    PQUIRK_NOBIGMODESENSE |
    310 	    PQUIRK_NOMODESENSE |
    311 	    PQUIRK_NOSYNCCACHE },
    312 	{ "NetBSD", "iqn.1994-04.org.netbsd.", 0 },
    313 	{ "Unknown", "unknown", 0 },
    314 	{ NULL, NULL, 0 }
    315 };
    316 
    317 /* loop through the quirktab looking for a match on target name */
    318 static const quirktab_t *
    319 getquirks(const char *iqn)
    320 {
    321 	const quirktab_t	*qp;
    322 	size_t iqnlen, quirklen;
    323 
    324 	if (iqn == NULL)
    325 		iqn = "unknown";
    326 	iqnlen = strlen(iqn);
    327 	for (qp = quirktab ; qp->iqn ; qp++) {
    328 		quirklen = strlen(qp->iqn);
    329 		if (quirklen > iqnlen)
    330 			continue;
    331 		if (memcmp(qp->iqn, iqn, quirklen) == 0)
    332 			break;
    333 	}
    334 	return qp;
    335 }
    336 
    337 /******************************************************************************/
    338 
    339 /*
    340  * map_session
    341  *    This (indirectly) maps the existing LUNs for a target to SCSI devices
    342  *    by going through config_found to tell any child drivers that there's
    343  *    a new adapter.
    344  *    Note that each session is equivalent to a SCSI adapter.
    345  *
    346  *    Parameter:  the session pointer
    347  *
    348  *    Returns:    1 on success, 0 on failure
    349  *
    350  * ToDo: Figuring out how to handle more than one LUN. It appears that
    351  *    the NetBSD SCSI LUN discovery doesn't use "report LUNs", and instead
    352  *    goes through the LUNs sequentially, stopping somewhere on the way if it
    353  *    gets an error. We may have to do some LUN mapping in here if this is
    354  *    really how things work.
    355  */
    356 
    357 int
    358 map_session(session_t *sess, device_t dev)
    359 {
    360 	struct scsipi_adapter *adapt = &sess->s_sc_adapter;
    361 	struct scsipi_channel *chan = &sess->s_sc_channel;
    362 	const quirktab_t	*tgt;
    363 
    364 	mutex_enter(&sess->s_lock);
    365 	sess->s_send_window = max(2, window_size(sess, CCBS_FOR_SCSIPI));
    366 	mutex_exit(&sess->s_lock);
    367 
    368 	/*
    369 	 * Fill in the scsipi_adapter.
    370 	 */
    371 	adapt->adapt_dev = dev;
    372 	adapt->adapt_nchannels = 1;
    373 	adapt->adapt_request = iscsi_scsipi_request;
    374 	adapt->adapt_minphys = iscsi_minphys;
    375 	adapt->adapt_openings = sess->s_send_window;
    376 	adapt->adapt_max_periph = CCBS_FOR_SCSIPI;
    377 	adapt->adapt_flags = SCSIPI_ADAPT_MPSAFE;
    378 
    379 	/*
    380 	 * Fill in the scsipi_channel.
    381 	 */
    382 	if ((tgt = getquirks(chan->chan_name)) == NULL) {
    383 		tgt = getquirks("unknown");
    384 	}
    385 	chan->chan_name = tgt->tgt;
    386 	chan->chan_defquirks = tgt->quirks;
    387 	chan->chan_adapter = adapt;
    388 	chan->chan_bustype = &scsi_bustype;
    389 	chan->chan_channel = 0;
    390 	chan->chan_flags = SCSIPI_CHAN_NOSETTLE | SCSIPI_CHAN_CANGROW;
    391 	chan->chan_ntargets = 1;
    392 	chan->chan_nluns = 16;
    393 	chan->chan_id = sess->s_id;
    394 
    395 	sess->s_child_dev = config_found(dev, chan, scsiprint);
    396 
    397 	return sess->s_child_dev != NULL;
    398 }
    399 
    400 
    401 /*
    402  * unmap_session
    403  *    This (indirectly) unmaps the existing all LUNs for a target by
    404  *    telling the config system that the adapter has detached.
    405  *
    406  *    Parameter:  the session pointer
    407  *
    408  *    Returns:    1 on success, 0 on failure
    409  */
    410 
    411 int
    412 unmap_session(session_t *sess)
    413 {
    414 	device_t dev;
    415 	int rv = 1;
    416 
    417 	if ((dev = sess->s_child_dev) != NULL) {
    418 		sess->s_child_dev = NULL;
    419 		if (config_detach(dev, 0))
    420 			rv = 0;
    421 	}
    422 
    423 	return rv;
    424 }
    425 
    426 /*
    427  * grow_resources
    428  *    Try to grow openings up to current window size
    429  */
    430 static void
    431 grow_resources(session_t *sess)
    432 {
    433 	struct scsipi_adapter *adapt = &sess->s_sc_adapter;
    434 	int win;
    435 
    436 	mutex_enter(&sess->s_lock);
    437 	if (sess->s_refcount < CCBS_FOR_SCSIPI &&
    438 	    sess->s_send_window < CCBS_FOR_SCSIPI) {
    439 		win = window_size(sess, CCBS_FOR_SCSIPI - sess->s_refcount);
    440 		if (win > sess->s_send_window) {
    441 			sess->s_send_window++;
    442 			adapt->adapt_openings++;
    443 			DEB(5, ("Grow send window to %d\n", sess->s_send_window));
    444 		}
    445 	}
    446 	mutex_exit(&sess->s_lock);
    447 }
    448 
    449 /******************************************************************************/
    450 
    451 /*****************************************************************************
    452  * SCSI interface routines
    453  *****************************************************************************/
    454 
    455 /*
    456  * iscsi_scsipi_request:
    457  *    Perform a request for the SCSIPI layer.
    458  */
    459 
    460 void
    461 iscsi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    462 					 void *arg)
    463 {
    464 	struct scsipi_adapter *adapt = chan->chan_adapter;
    465 	struct scsipi_xfer *xs;
    466 	session_t *sess;
    467 	int flags;
    468 	struct scsipi_xfer_mode *xm;
    469 	int error;
    470 
    471 	sess = (session_t *) adapt;	/* adapter is first field in session */
    472 
    473 	error = ref_session(sess);
    474 
    475 	switch (req) {
    476 	case ADAPTER_REQ_RUN_XFER:
    477 		DEB(9, ("ISCSI: scsipi_request RUN_XFER\n"));
    478 		xs = arg;
    479 		flags = xs->xs_control;
    480 
    481 		if (error) {
    482 			DEB(9, ("ISCSI: refcount too high: %d, winsize %d\n",
    483 				sess->s_refcount, sess->s_send_window));
    484 			xs->error = XS_BUSY;
    485 			xs->status = XS_BUSY;
    486 			scsipi_done(xs);
    487 			return;
    488 		}
    489 
    490 		if ((flags & XS_CTL_POLL) != 0) {
    491 			xs->error = XS_DRIVER_STUFFUP;
    492 			DEBOUT(("Run Xfer request with polling\n"));
    493 			scsipi_done(xs);
    494 			break;
    495 		}
    496 		/*
    497 		 * NOTE: It appears that XS_CTL_DATA_UIO is not actually used anywhere.
    498 		 * Since it really would complicate matters to handle offsets
    499 		 * into scatter-gather lists, and a number of other drivers don't
    500 		 * handle uio-based data as well, XS_CTL_DATA_UIO isn't
    501 		 * implemented in this driver (at least for now).
    502 		 */
    503 		if (flags & XS_CTL_DATA_UIO) {
    504 			xs->error = XS_DRIVER_STUFFUP;
    505 			DEBOUT(("Run Xfer with data in UIO\n"));
    506 			scsipi_done(xs);
    507 			break;
    508 		}
    509 
    510 		send_run_xfer(sess, xs);
    511 		DEB(15, ("scsipi_req returns, refcount = %d\n", sess->s_refcount));
    512 		return;
    513 
    514 	case ADAPTER_REQ_GROW_RESOURCES:
    515 		DEB(5, ("ISCSI: scsipi_request GROW_RESOURCES\n"));
    516 		grow_resources(sess);
    517 		break;
    518 
    519 	case ADAPTER_REQ_SET_XFER_MODE:
    520 		DEB(5, ("ISCSI: scsipi_request SET_XFER_MODE\n"));
    521 		xm = (struct scsipi_xfer_mode *)arg;
    522 		xm->xm_mode = PERIPH_CAP_TQING;
    523 		scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
    524 		break;
    525 
    526 	default:
    527 		DEBOUT(("ISCSI: scsipi_request with invalid REQ code %d\n", req));
    528 		break;
    529 	}
    530 
    531 	if (!error)
    532 		unref_session(sess);
    533 }
    534 
    535 /* cap the transfer at 64K */
    536 #define ISCSI_MAX_XFER	65536
    537 
    538 /*
    539  * iscsi_minphys:
    540  *    Limit a transfer to our maximum transfer size.
    541  */
    542 
    543 void
    544 iscsi_minphys(struct buf *bp)
    545 {
    546 	if (bp->b_bcount > ISCSI_MAX_XFER) {
    547 		bp->b_bcount = ISCSI_MAX_XFER;
    548 	}
    549 }
    550 
    551 /*****************************************************************************
    552  * SCSI job execution helper routines
    553  *****************************************************************************/
    554 
    555 /*
    556  * iscsi_done:
    557  *
    558  * A CCB has completed execution.  Pass the status back to the
    559  * upper layer.
    560  */
    561 void
    562 iscsi_done(ccb_t *ccb)
    563 {
    564 	struct scsipi_xfer *xs = ccb->ccb_xs;
    565 	DEB(9, ("iscsi_done\n"));
    566 
    567 	if (xs != NULL) {
    568 		xs->resid = ccb->ccb_residual;
    569 		ccb->ccb_xs = NULL;
    570 		xs->resid = ccb->ccb_residual;
    571 
    572 		switch (ccb->ccb_status) {
    573 		case ISCSI_STATUS_SUCCESS:
    574 			xs->error = XS_NOERROR;
    575 			xs->status = SCSI_OK;
    576 			break;
    577 
    578 		case ISCSI_STATUS_CHECK_CONDITION:
    579 			xs->error = XS_SENSE;
    580 			xs->status = SCSI_CHECK;
    581 			break;
    582 
    583 		case ISCSI_STATUS_TARGET_BUSY:
    584 		case ISCSI_STATUS_NO_RESOURCES:
    585 			DEBC(ccb->ccb_connection, 5, ("target busy, ccb %p\n", ccb));
    586 			xs->error = XS_BUSY;
    587 			xs->status = SCSI_BUSY;
    588 			break;
    589 
    590 		case ISCSI_STATUS_SOCKET_ERROR:
    591 		case ISCSI_STATUS_TIMEOUT:
    592 			xs->error = XS_SELTIMEOUT;
    593 			xs->status = SCSI_BUSY;
    594 			break;
    595 
    596 		case ISCSI_STATUS_QUEUE_FULL:
    597 			DEBC(ccb->ccb_connection, 5, ("queue full, ccb %p\n", ccb));
    598 			xs->error = XS_BUSY;
    599 			xs->status = SCSI_QUEUE_FULL;
    600 			break;
    601 
    602 		default:
    603 			xs->error = XS_DRIVER_STUFFUP;
    604 			break;
    605 		}
    606 
    607 		unref_session(ccb->ccb_session);
    608 
    609 		DEB(99, ("Calling scsipi_done (%p), err = %d\n", xs, xs->error));
    610 		scsipi_done(xs);
    611 		DEB(99, ("scsipi_done returned\n"));
    612 	} else {
    613 		DEBOUT(("ISCSI: iscsi_done CCB %p without XS\n", ccb));
    614 	}
    615 }
    616 
    617 SYSCTL_SETUP(sysctl_iscsi_setup, "ISCSI subtree setup")
    618 {
    619 	const struct sysctlnode *node = NULL;
    620 
    621 	sysctl_createv(clog, 0, NULL, &node,
    622 		CTLFLAG_PERMANENT,
    623 		CTLTYPE_NODE, "iscsi",
    624 		SYSCTL_DESCR("iscsi controls"),
    625 		NULL, 0, NULL, 0,
    626 		CTL_HW, CTL_CREATE, CTL_EOL);
    627 	sysctl_createv(clog, 0, &node, NULL,
    628 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    629 		CTLTYPE_BOOL, "hexbignums",
    630 		SYSCTL_DESCR("encode parameters in hex"),
    631 		NULL, 0,  &iscsi_hex_bignums, 0,
    632 		CTL_CREATE, CTL_EOL);
    633 
    634 #ifdef ISCSI_DEBUG
    635 	sysctl_createv(clog, 0, &node, NULL,
    636 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    637 		CTLTYPE_INT, "debug",
    638 		SYSCTL_DESCR("debug level"),
    639 		NULL, 0,  &iscsi_debug_level, sizeof(iscsi_debug_level),
    640 		CTL_CREATE, CTL_EOL);
    641 #endif
    642 }
    643 
    644 
    645 /* Kernel Module support */
    646 
    647 #include <sys/module.h>
    648 
    649 MODULE(MODULE_CLASS_DRIVER, iscsi, NULL); /* Possibly a builtin module */
    650 
    651 #ifdef _MODULE
    652 static const struct cfiattrdata ibescsi_info = { "scsi", 1,
    653 	{{"channel", "-1", -1},}
    654 };
    655 
    656 static const struct cfiattrdata *const iscsi_attrs[] = { &ibescsi_info, NULL };
    657 
    658 CFDRIVER_DECL(iscsi, DV_DULL, iscsi_attrs);
    659 
    660 static struct cfdata iscsi_cfdata[] = {
    661 	{
    662 		.cf_name = "iscsi",
    663 		.cf_atname = "iscsi",
    664 		.cf_unit = 0,		/* Only unit 0 is ever used  */
    665 		.cf_fstate = FSTATE_NOTFOUND,
    666 		.cf_loc = NULL,
    667 		.cf_flags = 0,
    668 		.cf_pspec = NULL,
    669 	},
    670 	{ NULL, NULL, 0, 0, NULL, 0, NULL }
    671 };
    672 #endif
    673 
    674 static int
    675 iscsi_modcmd(modcmd_t cmd, void *arg)
    676 {
    677 #ifdef _MODULE
    678 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
    679 	int error;
    680 	static struct sysctllog *clog;
    681 #endif
    682 
    683 	switch (cmd) {
    684 	case MODULE_CMD_INIT:
    685 #ifdef _MODULE
    686 		error = config_cfdriver_attach(&iscsi_cd);
    687 		if (error) {
    688 			return error;
    689 		}
    690 
    691 		error = config_cfattach_attach(iscsi_cd.cd_name, &iscsi_ca);
    692 		if (error) {
    693 			config_cfdriver_detach(&iscsi_cd);
    694 			aprint_error("%s: unable to register cfattach\n",
    695 				iscsi_cd.cd_name);
    696 			return error;
    697 		}
    698 
    699 		error = config_cfdata_attach(iscsi_cfdata, 1);
    700 		if (error) {
    701 			aprint_error("%s: unable to attach cfdata\n",
    702 				iscsi_cd.cd_name);
    703 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    704 			config_cfdriver_detach(&iscsi_cd);
    705 			return error;
    706 		}
    707 
    708 		error = devsw_attach(iscsi_cd.cd_name, NULL, &bmajor,
    709 			&iscsi_cdevsw, &cmajor);
    710 		if (error) {
    711 			aprint_error("%s: unable to register devsw\n",
    712 				iscsi_cd.cd_name);
    713 			config_cfdata_detach(iscsi_cfdata);
    714 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    715 			config_cfdriver_detach(&iscsi_cd);
    716 			return error;
    717 		}
    718 
    719 		if (config_attach_pseudo(iscsi_cfdata) == NULL) {
    720 			aprint_error("%s: config_attach_pseudo failed\n",
    721 				iscsi_cd.cd_name);
    722 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    723 			config_cfdriver_detach(&iscsi_cd);
    724 			return ENXIO;
    725 		}
    726 
    727 		sysctl_iscsi_setup(&clog);
    728 #endif
    729 		return 0;
    730 		break;
    731 
    732 	case MODULE_CMD_FINI:
    733 #ifdef _MODULE
    734 		error = config_cfdata_detach(iscsi_cfdata);
    735 		if (error)
    736 			return error;
    737 
    738 		sysctl_teardown(&clog);
    739 
    740 		config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    741 		config_cfdriver_detach(&iscsi_cd);
    742 		devsw_detach(NULL, &iscsi_cdevsw);
    743 #endif
    744 		return 0;
    745 		break;
    746 
    747 	case MODULE_CMD_AUTOUNLOAD:
    748 		return EBUSY;
    749 		break;
    750 
    751 	default:
    752 		return ENOTTY;
    753 		break;
    754 	}
    755 }
    756