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