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