Home | History | Annotate | Line # | Download | only in iscsi
iscsi_main.c revision 1.8
      1  1.1       agc /*	$netBSD: iscsi_main.c,v 1.1.1.1 2011/05/02 07:01:11 agc Exp $	*/
      2  1.1       agc 
      3  1.1       agc /*-
      4  1.1       agc  * Copyright (c) 2004,2005,2006,2011 The NetBSD Foundation, Inc.
      5  1.1       agc  * All rights reserved.
      6  1.1       agc  *
      7  1.1       agc  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1       agc  * by Wasabi Systems, Inc.
      9  1.1       agc  *
     10  1.1       agc  * Redistribution and use in source and binary forms, with or without
     11  1.1       agc  * modification, are permitted provided that the following conditions
     12  1.1       agc  * are met:
     13  1.1       agc  * 1. Redistributions of source code must retain the above copyright
     14  1.1       agc  *    notice, this list of conditions and the following disclaimer.
     15  1.1       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1       agc  *    notice, this list of conditions and the following disclaimer in the
     17  1.1       agc  *    documentation and/or other materials provided with the distribution.
     18  1.1       agc  *
     19  1.1       agc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1       agc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1       agc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1       agc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1       agc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1       agc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1       agc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1       agc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1       agc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1       agc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1       agc  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1       agc  */
     31  1.1       agc #include "iscsi_globals.h"
     32  1.1       agc 
     33  1.1       agc #include <sys/systm.h>
     34  1.1       agc #include <sys/buf.h>
     35  1.1       agc #include <sys/kmem.h>
     36  1.1       agc #include <sys/socketvar.h>
     37  1.1       agc 
     38  1.1       agc 
     39  1.1       agc /*------------------------- Global Variables ------------------------*/
     40  1.1       agc 
     41  1.1       agc extern struct cfdriver iscsi_cd;
     42  1.1       agc 
     43  1.1       agc #if defined(ISCSI_DEBUG)
     44  1.4   mlelstv int iscsi_debug_level = ISCSI_DEBUG;
     45  1.1       agc #endif
     46  1.1       agc 
     47  1.1       agc #if defined(ISCSI_PERFTEST)
     48  1.4   mlelstv int iscsi_perf_level = 0;
     49  1.1       agc #endif
     50  1.1       agc 
     51  1.1       agc /* Device Structure */
     52  1.1       agc iscsi_softc_t *sc = NULL;
     53  1.1       agc 
     54  1.1       agc /* the list of sessions */
     55  1.6   mlelstv session_list_t iscsi_sessions = TAILQ_HEAD_INITIALIZER(iscsi_sessions);
     56  1.1       agc 
     57  1.1       agc /* connections to clean up */
     58  1.7   mlelstv connection_list_t iscsi_cleanupc_list = TAILQ_HEAD_INITIALIZER(iscsi_cleanupc_list);
     59  1.7   mlelstv session_list_t iscsi_cleanups_list = TAILQ_HEAD_INITIALIZER(iscsi_cleanups_list);
     60  1.7   mlelstv 
     61  1.6   mlelstv bool iscsi_detaching = FALSE;
     62  1.6   mlelstv struct lwp *iscsi_cleanproc = NULL;
     63  1.1       agc 
     64  1.1       agc /* the number of active send threads (for cleanup thread) */
     65  1.6   mlelstv uint32_t iscsi_num_send_threads = 0;
     66  1.1       agc 
     67  1.1       agc /* Our node name, alias, and ISID */
     68  1.6   mlelstv uint8_t iscsi_InitiatorName[ISCSI_STRING_LENGTH] = "";
     69  1.6   mlelstv uint8_t iscsi_InitiatorAlias[ISCSI_STRING_LENGTH] = "";
     70  1.6   mlelstv login_isid_t iscsi_InitiatorISID;
     71  1.1       agc 
     72  1.1       agc /******************************************************************************/
     73  1.1       agc 
     74  1.1       agc /*
     75  1.1       agc    System interface: autoconf and device structures
     76  1.1       agc */
     77  1.1       agc 
     78  1.1       agc void iscsiattach(int);
     79  1.6   mlelstv 
     80  1.6   mlelstv static void iscsi_attach(device_t parent, device_t self, void *aux);
     81  1.6   mlelstv static int iscsi_match(device_t, cfdata_t, void *);
     82  1.6   mlelstv static int iscsi_detach(device_t, int);
     83  1.1       agc 
     84  1.1       agc 
     85  1.1       agc CFATTACH_DECL_NEW(iscsi, sizeof(struct iscsi_softc), iscsi_match, iscsi_attach,
     86  1.1       agc 			  iscsi_detach, NULL);
     87  1.1       agc 
     88  1.1       agc 
     89  1.6   mlelstv static dev_type_open(iscsiopen);
     90  1.6   mlelstv static dev_type_close(iscsiclose);
     91  1.1       agc 
     92  1.1       agc struct cdevsw iscsi_cdevsw = {
     93  1.8  dholland 	.d_open = iscsiopen,
     94  1.8  dholland 	.d_close = iscsiclose,
     95  1.8  dholland 	.d_read = noread,
     96  1.8  dholland 	.d_write = nowrite,
     97  1.8  dholland 	.d_ioctl = iscsiioctl,
     98  1.8  dholland 	.d_stop = nostop,
     99  1.8  dholland 	.d_tty = notty,
    100  1.8  dholland 	.d_poll = nopoll,
    101  1.8  dholland 	.d_mmap = nommap,
    102  1.8  dholland 	.d_kqfilter = nokqfilter,
    103  1.8  dholland 	.d_flag = D_OTHER
    104  1.1       agc };
    105  1.1       agc 
    106  1.1       agc /******************************************************************************/
    107  1.1       agc 
    108  1.1       agc STATIC void iscsi_scsipi_request(struct scsipi_channel *,
    109  1.1       agc 								 scsipi_adapter_req_t, void *);
    110  1.1       agc STATIC void iscsi_minphys(struct buf *);
    111  1.1       agc 
    112  1.1       agc /******************************************************************************/
    113  1.1       agc 
    114  1.1       agc /*******************************************************************************
    115  1.1       agc * Open and Close device interfaces. We don't really need them, because we don't
    116  1.1       agc * have to keep track of device opens and closes from userland. But apps can't
    117  1.1       agc * call ioctl without a handle to the device, and the kernel doesn't hand out
    118  1.1       agc * handles without an open routine in the driver. So here they are in all their
    119  1.1       agc * glory...
    120  1.1       agc *******************************************************************************/
    121  1.1       agc 
    122  1.1       agc int
    123  1.1       agc iscsiopen(dev_t dev, int flag, int mode, PTHREADOBJ p)
    124  1.1       agc {
    125  1.1       agc 
    126  1.1       agc 	DEB(99, ("ISCSI Open\n"));
    127  1.1       agc 	return 0;
    128  1.1       agc }
    129  1.1       agc 
    130  1.1       agc int
    131  1.1       agc iscsiclose(dev_t dev, int flag, int mode, PTHREADOBJ p)
    132  1.1       agc {
    133  1.1       agc 
    134  1.1       agc 	DEB(99, ("ISCSI Close\n"));
    135  1.1       agc 	return 0;
    136  1.1       agc }
    137  1.1       agc 
    138  1.1       agc /******************************************************************************/
    139  1.1       agc 
    140  1.1       agc /*
    141  1.1       agc  * The config Match routine.
    142  1.1       agc  *    Not much to do here, either - this is a pseudo-device.
    143  1.1       agc  */
    144  1.1       agc 
    145  1.6   mlelstv static int
    146  1.1       agc iscsi_match(device_t self, cfdata_t cfdata, void *arg)
    147  1.1       agc {
    148  1.1       agc 	return 1;
    149  1.1       agc }
    150  1.1       agc 
    151  1.1       agc /*
    152  1.1       agc  * iscsiattach:
    153  1.1       agc  *    Only called when statically configured into a kernel
    154  1.1       agc  */
    155  1.1       agc void
    156  1.1       agc iscsiattach(int n)
    157  1.1       agc {
    158  1.1       agc 	int err;
    159  1.1       agc 	cfdata_t cf;
    160  1.1       agc 
    161  1.1       agc 	err = config_cfattach_attach(iscsi_cd.cd_name, &iscsi_ca);
    162  1.1       agc 	if (err) {
    163  1.1       agc 		aprint_error("%s: couldn't register cfattach: %d\n",
    164  1.1       agc 		    iscsi_cd.cd_name, err);
    165  1.1       agc 		config_cfdriver_detach(&iscsi_cd);
    166  1.1       agc 		return;
    167  1.1       agc 	}
    168  1.1       agc 
    169  1.1       agc 	if (n > 1)
    170  1.1       agc 		aprint_error("%s: only one device supported\n",
    171  1.1       agc 		    iscsi_cd.cd_name);
    172  1.1       agc 
    173  1.1       agc 	cf = kmem_alloc(sizeof(struct cfdata), KM_NOSLEEP);
    174  1.1       agc 	if (cf == NULL) {
    175  1.1       agc 		aprint_error("%s: couldn't allocate cfdata\n",
    176  1.1       agc 		    iscsi_cd.cd_name);
    177  1.1       agc 		return;
    178  1.1       agc 	}
    179  1.1       agc 	cf->cf_name = iscsi_cd.cd_name;
    180  1.1       agc 	cf->cf_atname = iscsi_cd.cd_name;
    181  1.1       agc 	cf->cf_unit = 0;
    182  1.1       agc 	cf->cf_fstate = FSTATE_NOTFOUND;
    183  1.1       agc 
    184  1.1       agc 	(void)config_attach_pseudo(cf);
    185  1.1       agc 	return;
    186  1.1       agc }
    187  1.1       agc 
    188  1.1       agc /*
    189  1.1       agc  * iscsi_attach:
    190  1.1       agc  *    One-time inits go here. Not much for now, probably even less later.
    191  1.1       agc  */
    192  1.6   mlelstv static void
    193  1.1       agc iscsi_attach(device_t parent, device_t self, void *aux)
    194  1.1       agc {
    195  1.1       agc 
    196  1.1       agc 	DEBOUT(("ISCSI: iscsi_attach, parent=%p, self=%p, aux=%p\n", parent,
    197  1.1       agc 			self, aux));
    198  1.1       agc 	sc = (iscsi_softc_t *) device_private(self);
    199  1.1       agc 	sc->sc_dev = self;
    200  1.1       agc 	if (kthread_create(PRI_NONE, 0, NULL, iscsi_cleanup_thread,
    201  1.6   mlelstv 	    NULL, &iscsi_cleanproc, "Cleanup") != 0) {
    202  1.1       agc 		panic("Can't create cleanup thread!");
    203  1.1       agc 	}
    204  1.1       agc 	aprint_normal("%s: attached.  major = %d\n", iscsi_cd.cd_name,
    205  1.1       agc 	    cdevsw_lookup_major(&iscsi_cdevsw));
    206  1.1       agc }
    207  1.1       agc 
    208  1.1       agc /*
    209  1.1       agc  * iscsi_detach:
    210  1.1       agc  *    Cleanup.
    211  1.1       agc  */
    212  1.6   mlelstv static int
    213  1.1       agc iscsi_detach(device_t self, int flags)
    214  1.1       agc {
    215  1.1       agc 
    216  1.1       agc 	DEBOUT(("ISCSI: detach\n"));
    217  1.1       agc 	kill_all_sessions();
    218  1.6   mlelstv 	iscsi_detaching = TRUE;
    219  1.6   mlelstv 	while (iscsi_cleanproc != NULL) {
    220  1.7   mlelstv 		wakeup(&iscsi_cleanupc_list);
    221  1.7   mlelstv 		tsleep(&iscsi_cleanupc_list, PWAIT, "detach_wait", 20 * hz);
    222  1.1       agc 	}
    223  1.1       agc 	return 0;
    224  1.1       agc }
    225  1.1       agc 
    226  1.1       agc /******************************************************************************/
    227  1.1       agc 
    228  1.1       agc typedef struct quirktab_t {
    229  1.1       agc 	const char	*tgt;
    230  1.1       agc 	size_t		 tgtlen;
    231  1.1       agc 	const char	*iqn;
    232  1.1       agc 	size_t		 iqnlen;
    233  1.1       agc 	uint32_t	 quirks;
    234  1.1       agc } quirktab_t;
    235  1.1       agc 
    236  1.1       agc static const quirktab_t	quirktab[] = {
    237  1.1       agc 	{ "StarWind",	8,
    238  1.1       agc 		"iqn.2008-08.com.starwindsoftware",	32,
    239  1.1       agc 		PQUIRK_ONLYBIG	},
    240  1.1       agc 	{ "UNH",	3,
    241  1.1       agc 		"iqn.2002-10.edu.unh.",	20,
    242  1.1       agc 		PQUIRK_NOBIGMODESENSE |
    243  1.1       agc 		PQUIRK_NOMODESENSE |
    244  1.1       agc 		PQUIRK_NOSYNCCACHE },
    245  1.1       agc 	{ "NetBSD",	6,
    246  1.1       agc 		"iqn.1994-04.org.netbsd.",	23,
    247  1.1       agc 		0	},
    248  1.1       agc 	{ "Unknown",	7,
    249  1.1       agc 		"unknown",	7,
    250  1.1       agc 		0	},
    251  1.1       agc 	{ NULL,		0,	NULL,	0,	0	}
    252  1.1       agc };
    253  1.1       agc 
    254  1.1       agc /* loop through the quirktab looking for a match on target name */
    255  1.1       agc static const quirktab_t *
    256  1.1       agc getquirks(const char *iqn)
    257  1.1       agc {
    258  1.1       agc 	const quirktab_t	*qp;
    259  1.1       agc 
    260  1.1       agc 	if (iqn == NULL) {
    261  1.1       agc 		iqn = "unknown";
    262  1.1       agc 	}
    263  1.1       agc 	for (qp = quirktab ; qp->iqn ; qp++) {
    264  1.1       agc 		if (strncmp(qp->iqn, iqn, qp->iqnlen) == 0) {
    265  1.1       agc 			break;
    266  1.1       agc 		}
    267  1.1       agc 	}
    268  1.1       agc 	return qp;
    269  1.1       agc }
    270  1.1       agc 
    271  1.1       agc /******************************************************************************/
    272  1.1       agc 
    273  1.1       agc /*
    274  1.1       agc  * map_session
    275  1.1       agc  *    This (indirectly) maps the existing LUNs for a target to SCSI devices
    276  1.1       agc  *    by going through config_found to tell any child drivers that there's
    277  1.1       agc  *    a new adapter.
    278  1.1       agc  *    Note that each session is equivalent to a SCSI adapter.
    279  1.1       agc  *
    280  1.1       agc  *    Parameter:  the session pointer
    281  1.1       agc  *
    282  1.1       agc  *    Returns:    1 on success, 0 on failure
    283  1.1       agc  *
    284  1.1       agc  * ToDo: Figuring out how to handle more than one LUN. It appears that
    285  1.1       agc  *    the NetBSD SCSI LUN discovery doesn't use "report LUNs", and instead
    286  1.1       agc  *    goes through the LUNs sequentially, stopping somewhere on the way if it
    287  1.1       agc  *    gets an error. We may have to do some LUN mapping in here if this is
    288  1.1       agc  *    really how things work.
    289  1.1       agc  */
    290  1.1       agc 
    291  1.1       agc int
    292  1.1       agc map_session(session_t *session)
    293  1.1       agc {
    294  1.1       agc 	struct scsipi_adapter *adapt = &session->sc_adapter;
    295  1.1       agc 	struct scsipi_channel *chan = &session->sc_channel;
    296  1.1       agc 	const quirktab_t	*tgt;
    297  1.1       agc 
    298  1.1       agc 	if (sc == NULL) {
    299  1.1       agc 		/* we haven't gone through the config process */
    300  1.1       agc 		/* (shouldn't happen) */
    301  1.1       agc 		DEBOUT(("Map: No device pointer!\n"));
    302  1.1       agc 		return 0;
    303  1.1       agc 	}
    304  1.1       agc 	/*
    305  1.1       agc 	 * Fill in the scsipi_adapter.
    306  1.1       agc 	 */
    307  1.1       agc 	adapt->adapt_dev = sc->sc_dev;
    308  1.1       agc 	adapt->adapt_nchannels = 1;
    309  1.1       agc 	adapt->adapt_request = iscsi_scsipi_request;
    310  1.1       agc 	adapt->adapt_minphys = iscsi_minphys;
    311  1.1       agc 	adapt->adapt_openings = CCBS_PER_SESSION;
    312  1.1       agc 	adapt->adapt_max_periph = CCBS_PER_SESSION;
    313  1.1       agc 
    314  1.1       agc 	/*
    315  1.1       agc 	 * Fill in the scsipi_channel.
    316  1.1       agc 	 */
    317  1.1       agc 	if ((tgt = getquirks(chan->chan_name)) == NULL) {
    318  1.1       agc 		tgt = getquirks("unknown");
    319  1.1       agc 	}
    320  1.1       agc 	chan->chan_name = tgt->tgt;
    321  1.1       agc 	chan->chan_defquirks = tgt->quirks;
    322  1.1       agc 	chan->chan_adapter = adapt;
    323  1.1       agc 	chan->chan_bustype = &scsi_bustype;
    324  1.1       agc 	chan->chan_channel = 0;
    325  1.1       agc 	chan->chan_flags = SCSIPI_CHAN_NOSETTLE;
    326  1.1       agc 	chan->chan_ntargets = 1;
    327  1.1       agc 	chan->chan_nluns = 16;		/* ToDo: ??? */
    328  1.1       agc 	chan->chan_id = session->id;
    329  1.1       agc 
    330  1.1       agc 	session->child_dev = config_found(sc->sc_dev, chan, scsiprint);
    331  1.1       agc 
    332  1.1       agc 	return session->child_dev != NULL;
    333  1.1       agc }
    334  1.1       agc 
    335  1.1       agc 
    336  1.1       agc /*
    337  1.1       agc  * unmap_session
    338  1.1       agc  *    This (indirectly) unmaps the existing all LUNs for a target by
    339  1.1       agc  *    telling the config system that the adapter has detached.
    340  1.1       agc  *
    341  1.1       agc  *    Parameter:  the session pointer
    342  1.7   mlelstv  *
    343  1.7   mlelstv  *    Returns:    1 on success, 0 on failure
    344  1.1       agc  */
    345  1.1       agc 
    346  1.7   mlelstv int
    347  1.1       agc unmap_session(session_t *session)
    348  1.1       agc {
    349  1.1       agc 	device_t dev;
    350  1.7   mlelstv 	int rv = 1;
    351  1.1       agc 
    352  1.1       agc 	if ((dev = session->child_dev) != NULL) {
    353  1.1       agc 		session->child_dev = NULL;
    354  1.7   mlelstv 		if (config_detach(dev, 0))
    355  1.7   mlelstv 			rv = 0;
    356  1.1       agc 	}
    357  1.7   mlelstv 
    358  1.7   mlelstv 	return rv;
    359  1.1       agc }
    360  1.1       agc 
    361  1.1       agc /******************************************************************************/
    362  1.1       agc 
    363  1.1       agc /*****************************************************************************
    364  1.1       agc  * SCSI interface routines
    365  1.1       agc  *****************************************************************************/
    366  1.1       agc 
    367  1.1       agc /*
    368  1.1       agc  * iscsi_scsipi_request:
    369  1.1       agc  *    Perform a request for the SCSIPI layer.
    370  1.1       agc  */
    371  1.1       agc 
    372  1.1       agc void
    373  1.1       agc iscsi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    374  1.1       agc 					 void *arg)
    375  1.1       agc {
    376  1.1       agc 	struct scsipi_adapter *adapt = chan->chan_adapter;
    377  1.1       agc 	struct scsipi_xfer *xs;
    378  1.1       agc 	session_t *session;
    379  1.1       agc 	int flags;
    380  1.7   mlelstv 	struct scsipi_xfer_mode *xm;
    381  1.1       agc 
    382  1.1       agc 	session = (session_t *) adapt;	/* adapter is first field in session */
    383  1.1       agc 
    384  1.1       agc 	switch (req) {
    385  1.1       agc 	case ADAPTER_REQ_RUN_XFER:
    386  1.1       agc 		DEB(9, ("ISCSI: scsipi_request RUN_XFER\n"));
    387  1.1       agc 		xs = arg;
    388  1.1       agc 		flags = xs->xs_control;
    389  1.1       agc 
    390  1.1       agc 		if ((flags & XS_CTL_POLL) != 0) {
    391  1.1       agc 			xs->error = XS_DRIVER_STUFFUP;
    392  1.1       agc 			DEBOUT(("Run Xfer request with polling\n"));
    393  1.1       agc 			scsipi_done(xs);
    394  1.1       agc 			return;
    395  1.1       agc 		}
    396  1.1       agc 		/*
    397  1.1       agc 		 * NOTE: It appears that XS_CTL_DATA_UIO is not actually used anywhere.
    398  1.1       agc          *       Since it really would complicate matters to handle offsets
    399  1.1       agc          *       into scatter-gather lists, and a number of other drivers don't
    400  1.1       agc          *       handle uio-based data as well, XS_CTL_DATA_UIO isn't
    401  1.1       agc          *       implemented in this driver (at least for now).
    402  1.1       agc 		 */
    403  1.1       agc 		if (flags & XS_CTL_DATA_UIO) {
    404  1.1       agc 			xs->error = XS_DRIVER_STUFFUP;
    405  1.1       agc 			DEBOUT(("Run Xfer with data in UIO\n"));
    406  1.1       agc 			scsipi_done(xs);
    407  1.1       agc 			return;
    408  1.1       agc 		}
    409  1.1       agc 
    410  1.1       agc 		send_run_xfer(session, xs);
    411  1.1       agc 		DEB(9, ("scsipi_req returns\n"));
    412  1.1       agc 		return;
    413  1.1       agc 
    414  1.1       agc 	case ADAPTER_REQ_GROW_RESOURCES:
    415  1.1       agc 		DEBOUT(("ISCSI: scsipi_request GROW_RESOURCES\n"));
    416  1.1       agc 		return;
    417  1.1       agc 
    418  1.1       agc 	case ADAPTER_REQ_SET_XFER_MODE:
    419  1.1       agc 		DEB(5, ("ISCSI: scsipi_request SET_XFER_MODE\n"));
    420  1.7   mlelstv 		xm = (struct scsipi_xfer_mode *)arg;
    421  1.7   mlelstv 		xm->xm_mode = PERIPH_CAP_TQING;
    422  1.7   mlelstv 		scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
    423  1.1       agc 		return;
    424  1.1       agc 
    425  1.1       agc 	default:
    426  1.1       agc 		break;
    427  1.1       agc 	}
    428  1.1       agc 	DEBOUT(("ISCSI: scsipi_request with invalid REQ code %d\n", req));
    429  1.1       agc }
    430  1.1       agc 
    431  1.1       agc /* cap the transfer at 64K */
    432  1.1       agc #define ISCSI_MAX_XFER	65536
    433  1.1       agc 
    434  1.1       agc /*
    435  1.1       agc  * iscsi_minphys:
    436  1.1       agc  *    Limit a transfer to our maximum transfer size.
    437  1.1       agc  */
    438  1.1       agc 
    439  1.1       agc void
    440  1.1       agc iscsi_minphys(struct buf *bp)
    441  1.1       agc {
    442  1.1       agc 	if (bp->b_bcount > ISCSI_MAX_XFER) {
    443  1.1       agc 		bp->b_bcount = ISCSI_MAX_XFER;
    444  1.1       agc 	}
    445  1.1       agc }
    446  1.1       agc 
    447  1.1       agc /*****************************************************************************
    448  1.1       agc  * SCSI job execution helper routines
    449  1.1       agc  *****************************************************************************/
    450  1.1       agc 
    451  1.1       agc /*
    452  1.1       agc  * iscsi_done:
    453  1.1       agc  *
    454  1.1       agc  * A CCB has completed execution.  Pass the status back to the
    455  1.1       agc  * upper layer.
    456  1.1       agc  */
    457  1.1       agc void
    458  1.1       agc iscsi_done(ccb_t *ccb)
    459  1.1       agc {
    460  1.1       agc 	struct scsipi_xfer *xs = ccb->xs;
    461  1.1       agc 	/*DEBOUT (("iscsi_done\n")); */
    462  1.1       agc 
    463  1.1       agc 	if (xs != NULL) {
    464  1.1       agc 		xs->resid = ccb->residual;
    465  1.1       agc 
    466  1.1       agc 		switch (ccb->status) {
    467  1.1       agc 		case ISCSI_STATUS_SUCCESS:
    468  1.1       agc 			xs->error = 0;
    469  1.1       agc 			break;
    470  1.1       agc 
    471  1.1       agc 		case ISCSI_STATUS_CHECK_CONDITION:
    472  1.1       agc 			xs->error = XS_SENSE;
    473  1.1       agc #ifdef ISCSI_DEBUG
    474  1.1       agc 			{
    475  1.1       agc 				uint8_t *s = (uint8_t *) (&xs->sense);
    476  1.1       agc 				DEB(5, ("Scsipi_done, error=XS_SENSE, sense data=%02x "
    477  1.1       agc 						"%02x %02x %02x...\n",
    478  1.1       agc 						s[0], s[1], s[2], s[3]));
    479  1.1       agc 			}
    480  1.1       agc #endif
    481  1.1       agc 			break;
    482  1.1       agc 
    483  1.1       agc 		case ISCSI_STATUS_TARGET_BUSY:
    484  1.1       agc 			xs->error = XS_BUSY;
    485  1.1       agc 			break;
    486  1.1       agc 
    487  1.1       agc 		case ISCSI_STATUS_SOCKET_ERROR:
    488  1.1       agc 		case ISCSI_STATUS_TIMEOUT:
    489  1.1       agc 			xs->error = XS_SELTIMEOUT;
    490  1.1       agc 			break;
    491  1.1       agc 
    492  1.1       agc 		default:
    493  1.1       agc 			xs->error = XS_DRIVER_STUFFUP;
    494  1.1       agc 			break;
    495  1.1       agc 		}
    496  1.1       agc 
    497  1.1       agc 		DEB(99, ("Calling scsipi_done (%p), err = %d\n", xs, xs->error));
    498  1.1       agc 		scsipi_done(xs);
    499  1.1       agc 		DEB(99, ("scsipi_done returned\n"));
    500  1.1       agc 	}
    501  1.1       agc }
    502  1.1       agc 
    503  1.1       agc /* Kernel Module support */
    504  1.1       agc 
    505  1.1       agc #include <sys/module.h>
    506  1.1       agc 
    507  1.1       agc MODULE(MODULE_CLASS_DRIVER, iscsi, NULL);
    508  1.1       agc static const struct cfiattrdata ibescsi_info = { "scsi", 1,
    509  1.1       agc 	{{"channel", "-1", -1},}
    510  1.1       agc };
    511  1.1       agc static const struct cfiattrdata *const iscsi_attrs[] = { &ibescsi_info, NULL };
    512  1.1       agc 
    513  1.3       riz #ifdef _MODULE
    514  1.1       agc CFDRIVER_DECL(iscsi, DV_DULL, iscsi_attrs);
    515  1.1       agc 
    516  1.1       agc static struct cfdata iscsi_cfdata[] = {
    517  1.1       agc 	{
    518  1.1       agc 		.cf_name = "iscsi",
    519  1.1       agc 		.cf_atname = "iscsi",
    520  1.1       agc 		.cf_unit = 0,		/* Only unit 0 is ever used  */
    521  1.1       agc 		.cf_fstate = FSTATE_NOTFOUND,
    522  1.1       agc 		.cf_loc = NULL,
    523  1.1       agc 		.cf_flags = 0,
    524  1.1       agc 		.cf_pspec = NULL,
    525  1.1       agc 	},
    526  1.1       agc 	{ NULL, NULL, 0, 0, NULL, 0, NULL }
    527  1.1       agc };
    528  1.3       riz #endif
    529  1.1       agc 
    530  1.1       agc static int
    531  1.1       agc iscsi_modcmd(modcmd_t cmd, void *arg)
    532  1.1       agc {
    533  1.3       riz #ifdef _MODULE
    534  1.1       agc 	devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
    535  1.1       agc 	int error;
    536  1.3       riz #endif
    537  1.1       agc 
    538  1.1       agc 	switch (cmd) {
    539  1.1       agc 	case MODULE_CMD_INIT:
    540  1.3       riz #ifdef _MODULE
    541  1.1       agc 		error = config_cfdriver_attach(&iscsi_cd);
    542  1.1       agc 		if (error) {
    543  1.1       agc 			return error;
    544  1.1       agc 		}
    545  1.1       agc 
    546  1.1       agc 		error = config_cfattach_attach(iscsi_cd.cd_name, &iscsi_ca);
    547  1.1       agc 		if (error) {
    548  1.1       agc 			config_cfdriver_detach(&iscsi_cd);
    549  1.1       agc 			aprint_error("%s: unable to register cfattach\n",
    550  1.1       agc 				iscsi_cd.cd_name);
    551  1.1       agc 			return error;
    552  1.1       agc 		}
    553  1.1       agc 
    554  1.1       agc 		error = config_cfdata_attach(iscsi_cfdata, 1);
    555  1.1       agc 		if (error) {
    556  1.1       agc 			aprint_error("%s: unable to attach cfdata\n",
    557  1.1       agc 				iscsi_cd.cd_name);
    558  1.1       agc 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    559  1.1       agc 			config_cfdriver_detach(&iscsi_cd);
    560  1.1       agc 			return error;
    561  1.1       agc 		}
    562  1.1       agc 
    563  1.1       agc 		error = devsw_attach(iscsi_cd.cd_name, NULL, &bmajor,
    564  1.1       agc 			&iscsi_cdevsw, &cmajor);
    565  1.1       agc 		if (error) {
    566  1.1       agc 			aprint_error("%s: unable to register devsw\n",
    567  1.1       agc 				iscsi_cd.cd_name);
    568  1.1       agc 			config_cfdata_detach(iscsi_cfdata);
    569  1.1       agc 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    570  1.1       agc 			config_cfdriver_detach(&iscsi_cd);
    571  1.1       agc 			return error;
    572  1.1       agc 		}
    573  1.1       agc 
    574  1.1       agc 		if (config_attach_pseudo(iscsi_cfdata) == NULL) {
    575  1.1       agc 			aprint_error("%s: config_attach_pseudo failed\n",
    576  1.1       agc 				iscsi_cd.cd_name);
    577  1.1       agc 			config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    578  1.1       agc 			config_cfdriver_detach(&iscsi_cd);
    579  1.1       agc 			return ENXIO;
    580  1.1       agc 		}
    581  1.3       riz #endif
    582  1.1       agc 		return 0;
    583  1.1       agc 		break;
    584  1.1       agc 
    585  1.1       agc 	case MODULE_CMD_FINI:
    586  1.3       riz #ifdef _MODULE
    587  1.1       agc 		error = config_cfdata_detach(iscsi_cfdata);
    588  1.1       agc 		if (error)
    589  1.1       agc 			return error;
    590  1.1       agc 
    591  1.1       agc 		config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
    592  1.1       agc 		config_cfdriver_detach(&iscsi_cd);
    593  1.1       agc 		devsw_detach(NULL, &iscsi_cdevsw);
    594  1.3       riz #endif
    595  1.1       agc 		return 0;
    596  1.1       agc 		break;
    597  1.1       agc 
    598  1.2       riz 	case MODULE_CMD_AUTOUNLOAD:
    599  1.2       riz 		return EBUSY;
    600  1.2       riz 		break;
    601  1.2       riz 
    602  1.1       agc 	default:
    603  1.1       agc 		return ENOTTY;
    604  1.1       agc 		break;
    605  1.1       agc 	}
    606  1.1       agc }
    607