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