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