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