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