Home | History | Annotate | Line # | Download | only in iscsi
iscsi_main.c revision 1.35
      1 /*	$NetBSD: iscsi_main.c,v 1.35 2021/04/24 23:36:56 thorpej 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_SLEEP);
    221 	cf->cf_name = iscsi_cd.cd_name;
    222 	cf->cf_atname = iscsi_cd.cd_name;
    223 	cf->cf_unit = 0;
    224 	cf->cf_fstate = FSTATE_NOTFOUND;
    225 
    226 	(void)config_attach_pseudo(cf);
    227 	return;
    228 }
    229 
    230 /*
    231  * iscsi_attach:
    232  *    One-time inits go here. Not much for now, probably even less later.
    233  */
    234 static void
    235 iscsi_attach(device_t parent, device_t self, void *aux)
    236 {
    237 	struct iscsi_softc *sc;
    238 
    239 	DEB(1, ("ISCSI: iscsi_attach, parent=%p, self=%p, aux=%p\n", parent,
    240 			self, aux));
    241 	sc = (struct iscsi_softc *) device_private(self);
    242 	sc->dev = self;
    243 
    244 	TAILQ_INIT(&sc->fds);
    245 	mutex_init(&sc->lock, MUTEX_DEFAULT, IPL_NONE);
    246 
    247 	iscsi_detaching = false;
    248 	iscsi_init_cleanup();
    249 
    250 	if (!pmf_device_register(self, NULL, NULL))
    251 		aprint_error_dev(self, "couldn't establish power handler\n");
    252 
    253 	aprint_verbose("%s: attached.  major = %d\n", iscsi_cd.cd_name,
    254 	    cdevsw_lookup_major(&iscsi_cdevsw));
    255 }
    256 
    257 /*
    258  * iscsi_detach:
    259  *    Cleanup.
    260  */
    261 static int
    262 iscsi_detach(device_t self, int flags)
    263 {
    264 	struct iscsi_softc *sc;
    265 	int error;
    266 
    267 	DEB(1, ("ISCSI: detach\n"));
    268 	sc = (struct iscsi_softc *) device_private(self);
    269 
    270 	mutex_enter(&sc->lock);
    271 	if (!TAILQ_EMPTY(&sc->fds)) {
    272 		mutex_exit(&sc->lock);
    273 		return EBUSY;
    274 	}
    275 	iscsi_detaching = true;
    276 	mutex_exit(&sc->lock);
    277 
    278 	error = kill_all_sessions();
    279 	if (error)
    280 		return error;
    281 
    282 	error = iscsi_destroy_cleanup();
    283 	if (error)
    284 		return error;
    285 
    286 	pmf_device_deregister(sc->dev);
    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, CFARG_EOL);
    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 int
    426 grow_resources(session_t *sess)
    427 {
    428 	struct scsipi_adapter *adapt = &sess->s_sc_adapter;
    429 	int win;
    430 	int rc = -1;
    431 
    432 	mutex_enter(&sess->s_lock);
    433 	if (sess->s_refcount < CCBS_FOR_SCSIPI &&
    434 	    sess->s_send_window < CCBS_FOR_SCSIPI) {
    435 		win = window_size(sess, CCBS_FOR_SCSIPI - sess->s_refcount);
    436 		if (win > sess->s_send_window) {
    437 			sess->s_send_window++;
    438 			adapt->adapt_openings++;
    439 			rc = 0;
    440 			DEB(5, ("Grow send window to %d\n", sess->s_send_window));
    441 		}
    442 	}
    443 	mutex_exit(&sess->s_lock);
    444 
    445 	return rc;
    446 }
    447 
    448 /******************************************************************************/
    449 
    450 /*****************************************************************************
    451  * SCSI interface routines
    452  *****************************************************************************/
    453 
    454 /*
    455  * iscsi_scsipi_request:
    456  *    Perform a request for the SCSIPI layer.
    457  */
    458 
    459 void
    460 iscsi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    461 					 void *arg)
    462 {
    463 	struct scsipi_adapter *adapt = chan->chan_adapter;
    464 	struct scsipi_xfer *xs;
    465 	session_t *sess;
    466 	int flags;
    467 	struct scsipi_xfer_mode *xm;
    468 	int error;
    469 
    470 	sess = (session_t *) adapt;	/* adapter is first field in session */
    471 
    472 	error = ref_session(sess);
    473 
    474 	switch (req) {
    475 	case ADAPTER_REQ_RUN_XFER:
    476 		DEB(9, ("ISCSI: scsipi_request RUN_XFER\n"));
    477 		xs = arg;
    478 		flags = xs->xs_control;
    479 
    480 		if (error) {
    481 			DEB(9, ("ISCSI: refcount too high: %d, winsize %d\n",
    482 				sess->s_refcount, sess->s_send_window));
    483 			xs->error = XS_BUSY;
    484 			xs->status = XS_BUSY;
    485 			scsipi_done(xs);
    486 			return;
    487 		}
    488 
    489 		if ((flags & XS_CTL_POLL) != 0) {
    490 			xs->error = XS_DRIVER_STUFFUP;
    491 			DEBOUT(("Run Xfer request with polling\n"));
    492 			scsipi_done(xs);
    493 			break;
    494 		}
    495 		/*
    496 		 * NOTE: It appears that XS_CTL_DATA_UIO is not actually used anywhere.
    497 		 * Since it really would complicate matters to handle offsets
    498 		 * into scatter-gather lists, and a number of other drivers don't
    499 		 * handle uio-based data as well, XS_CTL_DATA_UIO isn't
    500 		 * implemented in this driver (at least for now).
    501 		 */
    502 		if (flags & XS_CTL_DATA_UIO) {
    503 			xs->error = XS_DRIVER_STUFFUP;
    504 			DEBOUT(("Run Xfer with data in UIO\n"));
    505 			scsipi_done(xs);
    506 			break;
    507 		}
    508 
    509 		send_run_xfer(sess, xs);
    510 		DEB(15, ("scsipi_req returns, refcount = %d\n", sess->s_refcount));
    511 		return;
    512 
    513 	case ADAPTER_REQ_GROW_RESOURCES:
    514 		DEB(5, ("ISCSI: scsipi_request GROW_RESOURCES\n"));
    515 		if (grow_resources(sess)) {
    516 			/* reached maximum */
    517 			chan->chan_flags &= ~SCSIPI_CHAN_CANGROW;
    518 		}
    519 		break;
    520 
    521 	case ADAPTER_REQ_SET_XFER_MODE:
    522 		DEB(5, ("ISCSI: scsipi_request SET_XFER_MODE\n"));
    523 		xm = (struct scsipi_xfer_mode *)arg;
    524 		xm->xm_mode = PERIPH_CAP_TQING;
    525 		scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
    526 		break;
    527 
    528 	default:
    529 		DEBOUT(("ISCSI: scsipi_request with invalid REQ code %d\n", req));
    530 		break;
    531 	}
    532 
    533 	if (!error)
    534 		unref_session(sess);
    535 }
    536 
    537 /* cap the transfer at 64K */
    538 #define ISCSI_MAX_XFER	65536
    539 
    540 /*
    541  * iscsi_minphys:
    542  *    Limit a transfer to our maximum transfer size.
    543  */
    544 
    545 void
    546 iscsi_minphys(struct buf *bp)
    547 {
    548 	if (bp->b_bcount > ISCSI_MAX_XFER) {
    549 		bp->b_bcount = ISCSI_MAX_XFER;
    550 	}
    551 }
    552 
    553 /*****************************************************************************
    554  * SCSI job execution helper routines
    555  *****************************************************************************/
    556 
    557 /*
    558  * iscsi_done:
    559  *
    560  * A CCB has completed execution.  Pass the status back to the
    561  * upper layer.
    562  */
    563 void
    564 iscsi_done(ccb_t *ccb)
    565 {
    566 	struct scsipi_xfer *xs = ccb->ccb_xs;
    567 	DEB(9, ("iscsi_done\n"));
    568 
    569 	if (xs != NULL) {
    570 		xs->resid = ccb->ccb_residual;
    571 		ccb->ccb_xs = NULL;
    572 		xs->resid = ccb->ccb_residual;
    573 
    574 		switch (ccb->ccb_status) {
    575 		case ISCSI_STATUS_SUCCESS:
    576 			xs->error = XS_NOERROR;
    577 			xs->status = SCSI_OK;
    578 			break;
    579 
    580 		case ISCSI_STATUS_CHECK_CONDITION:
    581 			xs->error = XS_SENSE;
    582 			xs->status = SCSI_CHECK;
    583 			break;
    584 
    585 		case ISCSI_STATUS_TARGET_BUSY:
    586 		case ISCSI_STATUS_NO_RESOURCES:
    587 			DEBC(ccb->ccb_connection, 5, ("target busy, ccb %p\n", ccb));
    588 			xs->error = XS_BUSY;
    589 			xs->status = SCSI_BUSY;
    590 			break;
    591 
    592 		case ISCSI_STATUS_SOCKET_ERROR:
    593 		case ISCSI_STATUS_TIMEOUT:
    594 			xs->error = XS_SELTIMEOUT;
    595 			xs->status = SCSI_BUSY;
    596 			break;
    597 
    598 		case ISCSI_STATUS_QUEUE_FULL:
    599 			DEBC(ccb->ccb_connection, 5, ("queue full, ccb %p\n", ccb));
    600 			xs->error = XS_BUSY;
    601 			xs->status = SCSI_QUEUE_FULL;
    602 			break;
    603 
    604 		default:
    605 			xs->error = XS_DRIVER_STUFFUP;
    606 			break;
    607 		}
    608 
    609 		unref_session(ccb->ccb_session);
    610 
    611 		DEB(99, ("Calling scsipi_done (%p), err = %d\n", xs, xs->error));
    612 		scsipi_done(xs);
    613 		DEB(99, ("scsipi_done returned\n"));
    614 	} else {
    615 		DEBOUT(("ISCSI: iscsi_done CCB %p without XS\n", ccb));
    616 	}
    617 }
    618 
    619 SYSCTL_SETUP(sysctl_iscsi_setup, "ISCSI subtree setup")
    620 {
    621 	const struct sysctlnode *node = NULL;
    622 
    623 	sysctl_createv(clog, 0, NULL, &node,
    624 		CTLFLAG_PERMANENT,
    625 		CTLTYPE_NODE, "iscsi",
    626 		SYSCTL_DESCR("iscsi controls"),
    627 		NULL, 0, NULL, 0,
    628 		CTL_HW, CTL_CREATE, CTL_EOL);
    629 	sysctl_createv(clog, 0, &node, NULL,
    630 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    631 		CTLTYPE_BOOL, "hexbignums",
    632 		SYSCTL_DESCR("encode parameters in hex"),
    633 		NULL, 0,  &iscsi_hex_bignums, 0,
    634 		CTL_CREATE, CTL_EOL);
    635 
    636 #ifdef ISCSI_DEBUG
    637 	sysctl_createv(clog, 0, &node, NULL,
    638 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    639 		CTLTYPE_INT, "debug",
    640 		SYSCTL_DESCR("debug level"),
    641 		NULL, 0,  &iscsi_debug_level, sizeof(iscsi_debug_level),
    642 		CTL_CREATE, CTL_EOL);
    643 #endif
    644 }
    645 
    646 
    647 /* Kernel Module support */
    648 
    649 #include <sys/module.h>
    650 
    651 MODULE(MODULE_CLASS_DRIVER, iscsi, NULL); /* Possibly a builtin module */
    652 
    653 #ifdef _MODULE
    654 static const struct cfiattrdata ibescsi_info = { "scsi", 1,
    655 	{{"channel", "-1", -1},}
    656 };
    657 
    658 static const struct cfiattrdata *const iscsi_attrs[] = { &ibescsi_info, NULL };
    659 
    660 CFDRIVER_DECL(iscsi, DV_DULL, iscsi_attrs);
    661 
    662 static struct cfdata iscsi_cfdata[] = {
    663 	{
    664 		.cf_name = "iscsi",
    665 		.cf_atname = "iscsi",
    666 		.cf_unit = 0,		/* Only unit 0 is ever used  */
    667 		.cf_fstate = FSTATE_NOTFOUND,
    668 		.cf_loc = NULL,
    669 		.cf_flags = 0,
    670 		.cf_pspec = NULL,
    671 	},
    672 	{ NULL, NULL, 0, 0, NULL, 0, NULL }
    673 };
    674 #endif
    675 
    676 static int
    677 iscsi_modcmd(modcmd_t cmd, void *arg)
    678 {
    679 #ifdef _MODULE
    680 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
    681 	int error;
    682 #endif
    683 
    684 	switch (cmd) {
    685 	case MODULE_CMD_INIT:
    686 #ifdef _MODULE
    687 		error = config_cfdriver_attach(&iscsi_cd);
    688 		if (error) {
    689 			return error;
    690 		}
    691 
    692 		error = config_cfattach_attach(iscsi_cd.cd_name, &iscsi_ca);
    693 		if (error) {
    694 			config_cfdriver_detach(&iscsi_cd);
    695 			aprint_error("%s: unable to register cfattach\n",
    696 				iscsi_cd.cd_name);
    697 			return error;
    698 		}
    699 
    700 		error = config_cfdata_attach(iscsi_cfdata, 1);
    701 		if (error) {
    702 			aprint_error("%s: unable to attach cfdata\n",
    703 				iscsi_cd.cd_name);
    704 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    705 			config_cfdriver_detach(&iscsi_cd);
    706 			return error;
    707 		}
    708 
    709 		error = devsw_attach(iscsi_cd.cd_name, NULL, &bmajor,
    710 			&iscsi_cdevsw, &cmajor);
    711 		if (error) {
    712 			aprint_error("%s: unable to register devsw\n",
    713 				iscsi_cd.cd_name);
    714 			config_cfdata_detach(iscsi_cfdata);
    715 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    716 			config_cfdriver_detach(&iscsi_cd);
    717 			return error;
    718 		}
    719 
    720 		if (config_attach_pseudo(iscsi_cfdata) == NULL) {
    721 			aprint_error("%s: config_attach_pseudo failed\n",
    722 				iscsi_cd.cd_name);
    723 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    724 			config_cfdriver_detach(&iscsi_cd);
    725 			return ENXIO;
    726 		}
    727 #endif
    728 		return 0;
    729 		break;
    730 
    731 	case MODULE_CMD_FINI:
    732 #ifdef _MODULE
    733 		error = config_cfdata_detach(iscsi_cfdata);
    734 		if (error)
    735 			return error;
    736 
    737 		config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    738 		config_cfdriver_detach(&iscsi_cd);
    739 		devsw_detach(NULL, &iscsi_cdevsw);
    740 #endif
    741 		return 0;
    742 		break;
    743 
    744 	case MODULE_CMD_AUTOUNLOAD:
    745 		return EBUSY;
    746 		break;
    747 
    748 	default:
    749 		return ENOTTY;
    750 		break;
    751 	}
    752 }
    753