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