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