xref: /netbsd-src/sys/dev/iscsi/iscsi_main.c (revision 2718af68c3efc72c9769069b5c7f9ed36f6b9def)
1 /*	$NetBSD: iscsi_main.c,v 1.40 2022/04/14 16:50:26 pgoyette 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 	int found;
363 
364 	mutex_enter(&sess->s_lock);
365 	sess->s_send_window = max(2, window_size(sess, CCBS_FOR_SCSIPI));
366 	mutex_exit(&sess->s_lock);
367 
368 	/*
369 	 * Fill in the scsipi_adapter.
370 	 */
371 	adapt->adapt_dev = dev;
372 	adapt->adapt_nchannels = 1;
373 	adapt->adapt_request = iscsi_scsipi_request;
374 	adapt->adapt_minphys = iscsi_minphys;
375 	adapt->adapt_openings = sess->s_send_window;
376 	adapt->adapt_max_periph = CCBS_FOR_SCSIPI;
377 	adapt->adapt_flags = SCSIPI_ADAPT_MPSAFE;
378 
379 	/*
380 	 * Fill in the scsipi_channel.
381 	 */
382 	if ((tgt = getquirks(chan->chan_name)) == NULL) {
383 		tgt = getquirks("unknown");
384 	}
385 	chan->chan_name = tgt->tgt;
386 	chan->chan_defquirks = tgt->quirks;
387 	chan->chan_adapter = adapt;
388 	chan->chan_bustype = &scsi_bustype;
389 	chan->chan_channel = 0;
390 	chan->chan_flags = SCSIPI_CHAN_NOSETTLE | SCSIPI_CHAN_CANGROW;
391 	chan->chan_ntargets = 1;
392 	chan->chan_nluns = 16;
393 	chan->chan_id = sess->s_id;
394 
395 	KERNEL_LOCK(1, NULL);
396 	sess->s_child_dev = config_found(dev, chan, scsiprint, CFARGS_NONE);
397 	found = (sess->s_child_dev != NULL);
398 	KERNEL_UNLOCK_ONE(NULL);
399 
400 	return found;
401 }
402 
403 
404 /*
405  * unmap_session
406  *    This (indirectly) unmaps the existing all LUNs for a target by
407  *    telling the config system that the adapter has detached.
408  *
409  *    Parameter:  the session pointer
410  *
411  *    Returns:    1 on success, 0 on failure
412  */
413 
414 int
415 unmap_session(session_t *sess)
416 {
417 	device_t dev;
418 	int rv = 1;
419 
420 	if ((dev = sess->s_child_dev) != NULL) {
421 		sess->s_child_dev = NULL;
422 		if (config_detach(dev, 0))
423 			rv = 0;
424 	}
425 
426 	return rv;
427 }
428 
429 /*
430  * grow_resources
431  *    Try to grow openings up to current window size
432  */
433 static int
434 grow_resources(session_t *sess)
435 {
436 	struct scsipi_adapter *adapt = &sess->s_sc_adapter;
437 	int win;
438 	int rc = -1;
439 
440 	mutex_enter(&sess->s_lock);
441 	if (sess->s_refcount < CCBS_FOR_SCSIPI &&
442 	    sess->s_send_window < CCBS_FOR_SCSIPI) {
443 		win = window_size(sess, CCBS_FOR_SCSIPI - sess->s_refcount);
444 		if (win > sess->s_send_window) {
445 			sess->s_send_window++;
446 			adapt->adapt_openings++;
447 			rc = 0;
448 			DEB(5, ("Grow send window to %d\n", sess->s_send_window));
449 		}
450 	}
451 	mutex_exit(&sess->s_lock);
452 
453 	return rc;
454 }
455 
456 /******************************************************************************/
457 
458 /*****************************************************************************
459  * SCSI interface routines
460  *****************************************************************************/
461 
462 /*
463  * iscsi_scsipi_request:
464  *    Perform a request for the SCSIPI layer.
465  */
466 
467 void
468 iscsi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
469 					 void *arg)
470 {
471 	struct scsipi_adapter *adapt = chan->chan_adapter;
472 	struct scsipi_xfer *xs;
473 	session_t *sess;
474 	int flags;
475 	struct scsipi_xfer_mode *xm;
476 	int error;
477 
478 	sess = (session_t *) adapt;	/* adapter is first field in session */
479 
480 	error = ref_session(sess);
481 
482 	switch (req) {
483 	case ADAPTER_REQ_RUN_XFER:
484 		DEB(9, ("ISCSI: scsipi_request RUN_XFER\n"));
485 		xs = arg;
486 		flags = xs->xs_control;
487 
488 		if (error) {
489 			DEB(9, ("ISCSI: refcount too high: %d, winsize %d\n",
490 				sess->s_refcount, sess->s_send_window));
491 			xs->error = XS_BUSY;
492 			xs->status = XS_BUSY;
493 			scsipi_done(xs);
494 			return;
495 		}
496 
497 		if ((flags & XS_CTL_POLL) != 0) {
498 			xs->error = XS_DRIVER_STUFFUP;
499 			DEBOUT(("Run Xfer request with polling\n"));
500 			scsipi_done(xs);
501 			break;
502 		}
503 		/*
504 		 * NOTE: It appears that XS_CTL_DATA_UIO is not actually used anywhere.
505 		 * Since it really would complicate matters to handle offsets
506 		 * into scatter-gather lists, and a number of other drivers don't
507 		 * handle uio-based data as well, XS_CTL_DATA_UIO isn't
508 		 * implemented in this driver (at least for now).
509 		 */
510 		if (flags & XS_CTL_DATA_UIO) {
511 			xs->error = XS_DRIVER_STUFFUP;
512 			DEBOUT(("Run Xfer with data in UIO\n"));
513 			scsipi_done(xs);
514 			break;
515 		}
516 
517 		send_run_xfer(sess, xs);
518 		DEB(15, ("scsipi_req returns, refcount = %d\n", sess->s_refcount));
519 		return;
520 
521 	case ADAPTER_REQ_GROW_RESOURCES:
522 		DEB(5, ("ISCSI: scsipi_request GROW_RESOURCES\n"));
523 		if (grow_resources(sess)) {
524 			/* reached maximum */
525 			chan->chan_flags &= ~SCSIPI_CHAN_CANGROW;
526 		}
527 		break;
528 
529 	case ADAPTER_REQ_SET_XFER_MODE:
530 		DEB(5, ("ISCSI: scsipi_request SET_XFER_MODE\n"));
531 		xm = (struct scsipi_xfer_mode *)arg;
532 		xm->xm_mode = PERIPH_CAP_TQING;
533 		scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
534 		break;
535 
536 	default:
537 		DEBOUT(("ISCSI: scsipi_request with invalid REQ code %d\n", req));
538 		break;
539 	}
540 
541 	if (!error)
542 		unref_session(sess);
543 }
544 
545 /* cap the transfer at 64K */
546 #define ISCSI_MAX_XFER	65536
547 
548 /*
549  * iscsi_minphys:
550  *    Limit a transfer to our maximum transfer size.
551  */
552 
553 void
554 iscsi_minphys(struct buf *bp)
555 {
556 	if (bp->b_bcount > ISCSI_MAX_XFER) {
557 		bp->b_bcount = ISCSI_MAX_XFER;
558 	}
559 }
560 
561 /*****************************************************************************
562  * SCSI job execution helper routines
563  *****************************************************************************/
564 
565 /*
566  * iscsi_done:
567  *
568  * A CCB has completed execution.  Pass the status back to the
569  * upper layer.
570  */
571 void
572 iscsi_done(ccb_t *ccb)
573 {
574 	struct scsipi_xfer *xs = ccb->ccb_xs;
575 	DEB(9, ("iscsi_done\n"));
576 
577 	if (xs != NULL) {
578 		xs->resid = ccb->ccb_residual;
579 		ccb->ccb_xs = NULL;
580 		xs->resid = ccb->ccb_residual;
581 
582 		switch (ccb->ccb_status) {
583 		case ISCSI_STATUS_SUCCESS:
584 			xs->error = XS_NOERROR;
585 			xs->status = SCSI_OK;
586 			break;
587 
588 		case ISCSI_STATUS_CHECK_CONDITION:
589 			xs->error = XS_SENSE;
590 			xs->status = SCSI_CHECK;
591 			break;
592 
593 		case ISCSI_STATUS_TARGET_BUSY:
594 		case ISCSI_STATUS_NO_RESOURCES:
595 			DEBC(ccb->ccb_connection, 5, ("target busy, ccb %p\n", ccb));
596 			xs->error = XS_BUSY;
597 			xs->status = SCSI_BUSY;
598 			break;
599 
600 		case ISCSI_STATUS_SOCKET_ERROR:
601 		case ISCSI_STATUS_TIMEOUT:
602 			xs->error = XS_SELTIMEOUT;
603 			xs->status = SCSI_BUSY;
604 			break;
605 
606 		case ISCSI_STATUS_QUEUE_FULL:
607 			DEBC(ccb->ccb_connection, 5, ("queue full, ccb %p\n", ccb));
608 			xs->error = XS_BUSY;
609 			xs->status = SCSI_QUEUE_FULL;
610 			break;
611 
612 		default:
613 			xs->error = XS_DRIVER_STUFFUP;
614 			break;
615 		}
616 
617 		unref_session(ccb->ccb_session);
618 
619 		DEB(99, ("Calling scsipi_done (%p), err = %d\n", xs, xs->error));
620 		scsipi_done(xs);
621 		DEB(99, ("scsipi_done returned\n"));
622 	} else {
623 		DEBOUT(("ISCSI: iscsi_done CCB %p without XS\n", ccb));
624 	}
625 }
626 
627 SYSCTL_SETUP(sysctl_iscsi_setup, "ISCSI subtree setup")
628 {
629 	const struct sysctlnode *node = NULL;
630 
631 	sysctl_createv(clog, 0, NULL, &node,
632 		CTLFLAG_PERMANENT,
633 		CTLTYPE_NODE, "iscsi",
634 		SYSCTL_DESCR("iscsi controls"),
635 		NULL, 0, NULL, 0,
636 		CTL_HW, CTL_CREATE, CTL_EOL);
637 	sysctl_createv(clog, 0, &node, NULL,
638 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
639 		CTLTYPE_BOOL, "hexbignums",
640 		SYSCTL_DESCR("encode parameters in hex"),
641 		NULL, 0,  &iscsi_hex_bignums, 0,
642 		CTL_CREATE, CTL_EOL);
643 
644 #ifdef ISCSI_DEBUG
645 	sysctl_createv(clog, 0, &node, NULL,
646 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
647 		CTLTYPE_INT, "debug",
648 		SYSCTL_DESCR("debug level"),
649 		NULL, 0,  &iscsi_debug_level, sizeof(iscsi_debug_level),
650 		CTL_CREATE, CTL_EOL);
651 #endif
652 }
653 
654 
655 /* Kernel Module support */
656 
657 #include <sys/module.h>
658 
659 MODULE(MODULE_CLASS_DRIVER, iscsi, "scsi_subr"); /* Possibly a builtin module */
660 
661 #ifdef _MODULE
662 static const struct cfiattrdata ibescsi_info = { "scsi", 1,
663 	{{"channel", "-1", -1},}
664 };
665 
666 static const struct cfiattrdata *const iscsi_attrs[] = { &ibescsi_info, NULL };
667 
668 CFDRIVER_DECL(iscsi, DV_DULL, iscsi_attrs);
669 
670 static struct cfdata iscsi_cfdata[] = {
671 	{
672 		.cf_name = "iscsi",
673 		.cf_atname = "iscsi",
674 		.cf_unit = 0,		/* Only unit 0 is ever used  */
675 		.cf_fstate = FSTATE_NOTFOUND,
676 		.cf_loc = NULL,
677 		.cf_flags = 0,
678 		.cf_pspec = NULL,
679 	},
680 	{ NULL, NULL, 0, 0, NULL, 0, NULL }
681 };
682 #endif
683 
684 static int
685 iscsi_modcmd(modcmd_t cmd, void *arg)
686 {
687 #ifdef _MODULE
688 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
689 	int error;
690 #endif
691 
692 	switch (cmd) {
693 	case MODULE_CMD_INIT:
694 #ifdef _MODULE
695 		error = devsw_attach(iscsi_cd.cd_name, NULL, &bmajor,
696 			&iscsi_cdevsw, &cmajor);
697 		if (error) {
698 			aprint_error("%s: unable to register devsw\n",
699 				iscsi_cd.cd_name);
700 			return error;
701 		}
702 		error = config_cfdriver_attach(&iscsi_cd);
703 		if (error) {
704 			devsw_detach(NULL, &iscsi_cdevsw);
705 			return error;
706 		}
707 
708 		error = config_cfattach_attach(iscsi_cd.cd_name, &iscsi_ca);
709 		if (error) {
710 			config_cfdriver_detach(&iscsi_cd);
711 			devsw_detach(NULL, &iscsi_cdevsw);
712 			aprint_error("%s: unable to register cfattach\n",
713 				iscsi_cd.cd_name);
714 			return error;
715 		}
716 
717 		error = config_cfdata_attach(iscsi_cfdata, 1);
718 		if (error) {
719 			aprint_error("%s: unable to attach cfdata\n",
720 				iscsi_cd.cd_name);
721 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
722 			config_cfdriver_detach(&iscsi_cd);
723 			devsw_detach(NULL, &iscsi_cdevsw);
724 			return error;
725 		}
726 
727 		if (config_attach_pseudo(iscsi_cfdata) == NULL) {
728 			aprint_error("%s: config_attach_pseudo failed\n",
729 				iscsi_cd.cd_name);
730 
731 			config_cfdata_detach(iscsi_cfdata);
732 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
733 			config_cfdriver_detach(&iscsi_cd);
734 			devsw_detach(NULL, &iscsi_cdevsw);
735 			return ENXIO;
736 		}
737 #endif
738 		return 0;
739 		break;
740 
741 	case MODULE_CMD_FINI:
742 #ifdef _MODULE
743 		error = config_cfdata_detach(iscsi_cfdata);
744 		if (error)
745 			return error;
746 
747 		config_cfdata_detach(iscsi_cfdata);
748 		config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
749 		config_cfdriver_detach(&iscsi_cd);
750 		devsw_detach(NULL, &iscsi_cdevsw);
751 #endif
752 		return 0;
753 		break;
754 
755 	case MODULE_CMD_AUTOUNLOAD:
756 		return EBUSY;
757 		break;
758 
759 	default:
760 		return ENOTTY;
761 		break;
762 	}
763 }
764