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