Home | History | Annotate | Line # | Download | only in iscsi
iscsi_main.c revision 1.24.6.1
      1 /*	$netBSD: iscsi_main.c,v 1.1.1.1 2011/05/02 07:01:11 agc 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 
     51 bool iscsi_detaching;
     52 
     53 /* the list of sessions */
     54 session_list_t iscsi_sessions = TAILQ_HEAD_INITIALIZER(iscsi_sessions);
     55 
     56 /* the number of active send threads (for cleanup thread) */
     57 uint32_t iscsi_num_send_threads = 0;
     58 
     59 /* Our node name, alias, and ISID */
     60 uint8_t iscsi_InitiatorName[ISCSI_STRING_LENGTH] = "";
     61 uint8_t iscsi_InitiatorAlias[ISCSI_STRING_LENGTH] = "";
     62 login_isid_t iscsi_InitiatorISID;
     63 
     64 /******************************************************************************/
     65 
     66 /*
     67    System interface: autoconf and device structures
     68 */
     69 
     70 static void iscsi_attach(device_t parent, device_t self, void *aux);
     71 static int iscsi_match(device_t, cfdata_t, void *);
     72 static int iscsi_detach(device_t, int);
     73 
     74 struct iscsi_softc {
     75 	device_t		dev;
     76 	kmutex_t		lock;
     77 	TAILQ_HEAD(, iscsifd)	fds;
     78 };
     79 
     80 CFATTACH_DECL_NEW(iscsi, sizeof(struct iscsi_softc), iscsi_match, iscsi_attach,
     81 			  iscsi_detach, NULL);
     82 
     83 
     84 static dev_type_open(iscsiopen);
     85 static int iscsiclose(struct file *);
     86 
     87 static const struct fileops iscsi_fileops = {
     88 	.fo_ioctl = iscsiioctl,
     89 	.fo_close = iscsiclose,
     90 };
     91 
     92 struct cdevsw iscsi_cdevsw = {
     93 	.d_open = iscsiopen,
     94 	.d_close = noclose,
     95 	.d_read = noread,
     96 	.d_write = nowrite,
     97 	.d_ioctl = noioctl,
     98 	.d_stop = nostop,
     99 	.d_tty = notty,
    100 	.d_poll = nopoll,
    101 	.d_mmap = nommap,
    102 	.d_kqfilter = nokqfilter,
    103 	.d_discard = nodiscard,
    104 	.d_flag = D_OTHER
    105 };
    106 
    107 /******************************************************************************/
    108 
    109 STATIC void iscsi_scsipi_request(struct scsipi_channel *,
    110                                  scsipi_adapter_req_t, void *);
    111 STATIC void iscsi_minphys(struct buf *);
    112 
    113 /******************************************************************************/
    114 
    115 /*******************************************************************************
    116 * Open and Close device interfaces. We don't really need them, because we don't
    117 * have to keep track of device opens and closes from userland. But apps can't
    118 * call ioctl without a handle to the device, and the kernel doesn't hand out
    119 * handles without an open routine in the driver. So here they are in all their
    120 * glory...
    121 *******************************************************************************/
    122 
    123 int
    124 iscsiopen(dev_t dev, int flag, int mode, struct lwp *l)
    125 {
    126 	struct iscsifd *d;
    127 	struct iscsi_softc *sc;
    128 	struct file *fp;
    129 	int error, fd, unit;
    130 
    131 	unit = minor(dev);
    132 
    133 	DEB(99, ("ISCSI Open unit=%d\n",unit));
    134 
    135 	sc = device_lookup_private(&iscsi_cd, unit);
    136 	if (sc == NULL)
    137 		return ENXIO;
    138 
    139 	if ((error = fd_allocfile(&fp, &fd)) != 0)
    140 		return error;
    141 
    142 	d = kmem_alloc(sizeof(*d), KM_SLEEP);
    143 	d->dev = sc->dev;
    144 	d->unit = unit;
    145 
    146 	mutex_enter(&sc->lock);
    147 	if (iscsi_detaching) {
    148 		mutex_exit(&sc->lock);
    149 		kmem_free(d, sizeof(*d));
    150 		DEB(99, ("ISCSI Open aborting\n"));
    151 		fd_abort(curproc, fp, fd);
    152 		return ENXIO;
    153 	}
    154 	TAILQ_INSERT_TAIL(&sc->fds, d, link);
    155 	mutex_exit(&sc->lock);
    156 
    157 	return fd_clone(fp, fd, flag, &iscsi_fileops, d);
    158 }
    159 
    160 static int
    161 iscsiclose(struct file *fp)
    162 {
    163 	struct iscsifd *d = fp->f_iscsi;
    164 	struct iscsi_softc *sc;
    165 
    166 	sc = device_lookup_private(&iscsi_cd, d->unit);
    167 	if (sc == NULL) {
    168 		DEBOUT(("%s: Cannot find private data\n",__func__));
    169 		return ENXIO;
    170 	}
    171 
    172 	mutex_enter(&sc->lock);
    173 	TAILQ_REMOVE(&sc->fds, d, link);
    174 	mutex_exit(&sc->lock);
    175 
    176 	kmem_free(d, sizeof(*d));
    177 	fp->f_iscsi = NULL;
    178 
    179 	DEB(99, ("ISCSI Close\n"));
    180 	return 0;
    181 }
    182 
    183 /******************************************************************************/
    184 
    185 /*
    186  * The config Match routine.
    187  *    Not much to do here, either - this is a pseudo-device.
    188  */
    189 
    190 static int
    191 iscsi_match(device_t self, cfdata_t cfdata, void *arg)
    192 {
    193 	return 1;
    194 }
    195 
    196 /*
    197  * iscsiattach:
    198  *    Only called when statically configured into a kernel
    199  */
    200 void
    201 iscsiattach(int n)
    202 {
    203 	device_t dev;
    204 	int err;
    205 	cfdata_t cf;
    206 
    207 	err = config_cfattach_attach(iscsi_cd.cd_name, &iscsi_ca);
    208 	if (err) {
    209 		aprint_error("%s: couldn't register cfattach: %d\n",
    210 		    iscsi_cd.cd_name, err);
    211 		config_cfdriver_detach(&iscsi_cd);
    212 		return;
    213 	}
    214 
    215 	if (n > 1)
    216 		aprint_error("%s: only one device supported\n",
    217 		    iscsi_cd.cd_name);
    218 
    219 	cf = kmem_alloc(sizeof(struct cfdata), KM_NOSLEEP);
    220 	if (cf == NULL) {
    221 		aprint_error("%s: couldn't allocate cfdata\n",
    222 		    iscsi_cd.cd_name);
    223 		return;
    224 	}
    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 	dev = config_attach_pseudo(cf);
    231 	device_release(dev);
    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 *session, device_t dev)
    354 {
    355 	struct scsipi_adapter *adapt = &session->sc_adapter;
    356 	struct scsipi_channel *chan = &session->sc_channel;
    357 	const quirktab_t	*tgt;
    358 
    359 	mutex_enter(&session->lock);
    360 	session->send_window = max(2, window_size(session, CCBS_FOR_SCSIPI));
    361 	mutex_exit(&session->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 = session->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;		/* ToDo: ??? */
    388 	chan->chan_id = session->id;
    389 
    390 	session->child_dev = config_found(dev, chan, scsiprint);
    391 
    392 	return session->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 *session)
    408 {
    409 	device_t dev;
    410 	int rv = 1;
    411 
    412 	if ((dev = session->child_dev) != NULL) {
    413 		session->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 *session)
    427 {
    428 	struct scsipi_adapter *adapt = &session->sc_adapter;
    429 	int win;
    430 
    431 	mutex_enter(&session->lock);
    432 	if (session->refcount < CCBS_FOR_SCSIPI &&
    433 	    session->send_window < CCBS_FOR_SCSIPI) {
    434 		win = window_size(session, CCBS_FOR_SCSIPI - session->refcount);
    435 		if (win > session->send_window) {
    436 			session->send_window++;
    437 			adapt->adapt_openings++;
    438 			DEB(5, ("Grow send window to %d\n", session->send_window));
    439 		}
    440 	}
    441 	mutex_exit(&session->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 *session;
    462 	int flags;
    463 	struct scsipi_xfer_mode *xm;
    464 	int error;
    465 
    466 	session = (session_t *) adapt;	/* adapter is first field in session */
    467 
    468 	error = ref_session(session);
    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 				session->refcount, session->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(session, xs);
    506 		DEB(15, ("scsipi_req returns, refcount = %d\n", session->refcount));
    507 		return;
    508 
    509 	case ADAPTER_REQ_GROW_RESOURCES:
    510 		DEB(5, ("ISCSI: scsipi_request GROW_RESOURCES\n"));
    511 		grow_resources(session);
    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(session);
    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->xs;
    560 	DEB(9, ("iscsi_done\n"));
    561 
    562 	if (xs != NULL) {
    563 		xs->resid = ccb->residual;
    564 
    565 		switch (ccb->status) {
    566 		case ISCSI_STATUS_SUCCESS:
    567 			xs->error = XS_NOERROR;
    568 			xs->status = SCSI_OK;
    569 			break;
    570 
    571 		case ISCSI_STATUS_CHECK_CONDITION:
    572 			xs->error = XS_SENSE;
    573 			xs->status = SCSI_CHECK;
    574 			break;
    575 
    576 		case ISCSI_STATUS_TARGET_BUSY:
    577 		case ISCSI_STATUS_NO_RESOURCES:
    578 			DEBC(ccb->connection, 5, ("target busy, ccb %p\n", ccb));
    579 			xs->error = XS_BUSY;
    580 			xs->status = SCSI_BUSY;
    581 			break;
    582 
    583 		case ISCSI_STATUS_SOCKET_ERROR:
    584 		case ISCSI_STATUS_TIMEOUT:
    585 			xs->error = XS_SELTIMEOUT;
    586 			xs->status = SCSI_BUSY;
    587 			break;
    588 
    589 		case ISCSI_STATUS_QUEUE_FULL:
    590 			DEBC(ccb->connection, 5, ("queue full, ccb %p\n", ccb));
    591 			xs->error = XS_BUSY;
    592 			xs->status = SCSI_QUEUE_FULL;
    593 			break;
    594 
    595 		default:
    596 			xs->error = XS_DRIVER_STUFFUP;
    597 			break;
    598 		}
    599 
    600 		DEB(99, ("Calling scsipi_done (%p), err = %d\n", xs, xs->error));
    601 		scsipi_done(xs);
    602 		DEB(99, ("scsipi_done returned\n"));
    603 	} else {
    604 		DEBOUT(("ISCSI: iscsi_done CCB %p without XS\n", ccb));
    605 	}
    606 
    607 	unref_session(ccb->session);
    608 }
    609 
    610 SYSCTL_SETUP(sysctl_iscsi_setup, "ISCSI subtree setup")
    611 {
    612 	const struct sysctlnode *node = NULL;
    613 
    614 	sysctl_createv(clog, 0, NULL, &node,
    615 		CTLFLAG_PERMANENT,
    616 		CTLTYPE_NODE, "iscsi",
    617 		SYSCTL_DESCR("iscsi controls"),
    618 		NULL, 0, NULL, 0,
    619 		CTL_HW, CTL_CREATE, CTL_EOL);
    620 
    621 #ifdef ISCSI_DEBUG
    622 	sysctl_createv(clog, 0, &node, NULL,
    623 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    624 		CTLTYPE_INT, "debug",
    625 		SYSCTL_DESCR("debug level"),
    626 		NULL, 0,  &iscsi_debug_level, sizeof(iscsi_debug_level),
    627 		CTL_CREATE, CTL_EOL);
    628 #endif
    629 }
    630 
    631 
    632 /* Kernel Module support */
    633 
    634 #include <sys/module.h>
    635 
    636 MODULE(MODULE_CLASS_DRIVER, iscsi, NULL); /* Possibly a builtin module */
    637 
    638 #ifdef _MODULE
    639 static const struct cfiattrdata ibescsi_info = { "scsi", 1,
    640 	{{"channel", "-1", -1},}
    641 };
    642 
    643 static const struct cfiattrdata *const iscsi_attrs[] = { &ibescsi_info, NULL };
    644 
    645 CFDRIVER_DECL(iscsi, DV_DULL, iscsi_attrs);
    646 
    647 static struct cfdata iscsi_cfdata[] = {
    648 	{
    649 		.cf_name = "iscsi",
    650 		.cf_atname = "iscsi",
    651 		.cf_unit = 0,		/* Only unit 0 is ever used  */
    652 		.cf_fstate = FSTATE_NOTFOUND,
    653 		.cf_loc = NULL,
    654 		.cf_flags = 0,
    655 		.cf_pspec = NULL,
    656 	},
    657 	{ NULL, NULL, 0, 0, NULL, 0, NULL }
    658 };
    659 #endif
    660 
    661 static int
    662 iscsi_modcmd(modcmd_t cmd, void *arg)
    663 {
    664 #ifdef _MODULE
    665 	device_t dev;
    666 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
    667 	int error;
    668 	static struct sysctllog *clog;
    669 #endif
    670 
    671 	switch (cmd) {
    672 	case MODULE_CMD_INIT:
    673 #ifdef _MODULE
    674 		error = config_cfdriver_attach(&iscsi_cd);
    675 		if (error) {
    676 			return error;
    677 		}
    678 
    679 		error = config_cfattach_attach(iscsi_cd.cd_name, &iscsi_ca);
    680 		if (error) {
    681 			config_cfdriver_detach(&iscsi_cd);
    682 			aprint_error("%s: unable to register cfattach\n",
    683 				iscsi_cd.cd_name);
    684 			return error;
    685 		}
    686 
    687 		error = config_cfdata_attach(iscsi_cfdata, 1);
    688 		if (error) {
    689 			aprint_error("%s: unable to attach cfdata\n",
    690 				iscsi_cd.cd_name);
    691 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    692 			config_cfdriver_detach(&iscsi_cd);
    693 			return error;
    694 		}
    695 
    696 		error = devsw_attach(iscsi_cd.cd_name, NULL, &bmajor,
    697 			&iscsi_cdevsw, &cmajor);
    698 		if (error) {
    699 			aprint_error("%s: unable to register devsw\n",
    700 				iscsi_cd.cd_name);
    701 			config_cfdata_detach(iscsi_cfdata);
    702 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    703 			config_cfdriver_detach(&iscsi_cd);
    704 			return error;
    705 		}
    706 
    707 		if ((dev = config_attach_pseudo(iscsi_cfdata)) == NULL) {
    708 			aprint_error("%s: config_attach_pseudo failed\n",
    709 				iscsi_cd.cd_name);
    710 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    711 			config_cfdriver_detach(&iscsi_cd);
    712 			return ENXIO;
    713 		}
    714 		device_release(dev);
    715 
    716 		sysctl_iscsi_setup(&clog);
    717 #endif
    718 		return 0;
    719 		break;
    720 
    721 	case MODULE_CMD_FINI:
    722 #ifdef _MODULE
    723 		error = config_cfdata_detach(iscsi_cfdata);
    724 		if (error)
    725 			return error;
    726 
    727 		sysctl_teardown(&clog);
    728 
    729 		config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    730 		config_cfdriver_detach(&iscsi_cd);
    731 		devsw_detach(NULL, &iscsi_cdevsw);
    732 #endif
    733 		return 0;
    734 		break;
    735 
    736 	case MODULE_CMD_AUTOUNLOAD:
    737 		return EBUSY;
    738 		break;
    739 
    740 	default:
    741 		return ENOTTY;
    742 		break;
    743 	}
    744 }
    745