iscsi_main.c revision 1.18 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 #include <sys/sysctl.h>
40
41 #include "ioconf.h"
42
43 /*------------------------- Global Variables ------------------------*/
44
45 extern struct cfdriver iscsi_cd;
46
47 #if defined(ISCSI_DEBUG)
48 int iscsi_debug_level = ISCSI_DEBUG;
49 #endif
50
51 bool iscsi_detaching;
52
53 /* the list of sessions */
54 session_list_t iscsi_sessions = TAILQ_HEAD_INITIALIZER(iscsi_sessions);
55
56 /* the number of active send threads (for cleanup thread) */
57 uint32_t iscsi_num_send_threads = 0;
58
59 /* Our node name, alias, and ISID */
60 uint8_t iscsi_InitiatorName[ISCSI_STRING_LENGTH] = "";
61 uint8_t iscsi_InitiatorAlias[ISCSI_STRING_LENGTH] = "";
62 login_isid_t iscsi_InitiatorISID;
63
64 /******************************************************************************/
65
66 /*
67 System interface: autoconf and device structures
68 */
69
70 static void iscsi_attach(device_t parent, device_t self, void *aux);
71 static int iscsi_match(device_t, cfdata_t, void *);
72 static int iscsi_detach(device_t, int);
73
74 struct iscsi_softc {
75 device_t dev;
76 kmutex_t lock;
77 TAILQ_HEAD(, iscsifd) fds;
78 };
79
80 CFATTACH_DECL_NEW(iscsi, sizeof(struct iscsi_softc), iscsi_match, iscsi_attach,
81 iscsi_detach, NULL);
82
83
84 static dev_type_open(iscsiopen);
85 static int iscsiclose(struct file *);
86
87 static const struct fileops iscsi_fileops = {
88 .fo_ioctl = iscsiioctl,
89 .fo_close = iscsiclose,
90 };
91
92 struct cdevsw iscsi_cdevsw = {
93 .d_open = iscsiopen,
94 .d_close = noclose,
95 .d_read = noread,
96 .d_write = nowrite,
97 .d_ioctl = noioctl,
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, struct lwp *l)
125 {
126 struct iscsifd *d;
127 struct iscsi_softc *sc;
128 struct file *fp;
129 int error, fd, unit;
130
131 unit = minor(dev);
132
133 DEB(99, ("ISCSI Open unit=%d\n",unit));
134
135 sc = device_lookup_private(&iscsi_cd, unit);
136 if (sc == NULL)
137 return ENXIO;
138
139 if ((error = fd_allocfile(&fp, &fd)) != 0)
140 return error;
141
142 d = kmem_alloc(sizeof(*d), KM_SLEEP);
143 d->dev = sc->dev;
144 d->unit = unit;
145
146 mutex_enter(&sc->lock);
147 if (iscsi_detaching) {
148 mutex_exit(&sc->lock);
149 kmem_free(d, sizeof(*d));
150 DEB(99, ("ISCSI Open aborting\n"));
151 fd_abort(curproc, fp, fd);
152 return ENXIO;
153 }
154 TAILQ_INSERT_TAIL(&sc->fds, d, link);
155 mutex_exit(&sc->lock);
156
157 return fd_clone(fp, fd, flag, &iscsi_fileops, d);
158 }
159
160 static int
161 iscsiclose(struct file *fp)
162 {
163 struct iscsifd *d = fp->f_iscsi;
164 struct iscsi_softc *sc;
165
166 sc = device_lookup_private(&iscsi_cd, d->unit);
167 if (sc == NULL) {
168 DEBOUT(("%s: Cannot find private data\n",__func__));
169 return ENXIO;
170 }
171
172 mutex_enter(&sc->lock);
173 TAILQ_REMOVE(&sc->fds, d, link);
174 mutex_exit(&sc->lock);
175
176 kmem_free(d, sizeof(*d));
177 fp->f_iscsi = NULL;
178
179 DEB(99, ("ISCSI Close\n"));
180 return 0;
181 }
182
183 /******************************************************************************/
184
185 /*
186 * The config Match routine.
187 * Not much to do here, either - this is a pseudo-device.
188 */
189
190 static int
191 iscsi_match(device_t self, cfdata_t cfdata, void *arg)
192 {
193 return 1;
194 }
195
196 /*
197 * iscsiattach:
198 * Only called when statically configured into a kernel
199 */
200 void
201 iscsiattach(int n)
202 {
203 int err;
204 cfdata_t cf;
205
206 err = config_cfattach_attach(iscsi_cd.cd_name, &iscsi_ca);
207 if (err) {
208 aprint_error("%s: couldn't register cfattach: %d\n",
209 iscsi_cd.cd_name, err);
210 config_cfdriver_detach(&iscsi_cd);
211 return;
212 }
213
214 if (n > 1)
215 aprint_error("%s: only one device supported\n",
216 iscsi_cd.cd_name);
217
218 cf = kmem_alloc(sizeof(struct cfdata), KM_NOSLEEP);
219 if (cf == NULL) {
220 aprint_error("%s: couldn't allocate cfdata\n",
221 iscsi_cd.cd_name);
222 return;
223 }
224 cf->cf_name = iscsi_cd.cd_name;
225 cf->cf_atname = iscsi_cd.cd_name;
226 cf->cf_unit = 0;
227 cf->cf_fstate = FSTATE_NOTFOUND;
228
229 (void)config_attach_pseudo(cf);
230 return;
231 }
232
233 /*
234 * iscsi_attach:
235 * One-time inits go here. Not much for now, probably even less later.
236 */
237 static void
238 iscsi_attach(device_t parent, device_t self, void *aux)
239 {
240 struct iscsi_softc *sc;
241
242 DEB(1, ("ISCSI: iscsi_attach, parent=%p, self=%p, aux=%p\n", parent,
243 self, aux));
244 sc = (struct iscsi_softc *) device_private(self);
245 sc->dev = self;
246
247 TAILQ_INIT(&sc->fds);
248 mutex_init(&sc->lock, MUTEX_DEFAULT, IPL_NONE);
249
250 iscsi_detaching = false;
251 iscsi_init_cleanup();
252
253 aprint_normal("%s: attached. major = %d\n", iscsi_cd.cd_name,
254 cdevsw_lookup_major(&iscsi_cdevsw));
255 }
256
257 /*
258 * iscsi_detach:
259 * Cleanup.
260 */
261 static int
262 iscsi_detach(device_t self, int flags)
263 {
264 struct iscsi_softc *sc;
265
266 DEB(1, ("ISCSI: detach\n"));
267 sc = (struct iscsi_softc *) device_private(self);
268
269 mutex_enter(&sc->lock);
270 if (!TAILQ_EMPTY(&sc->fds)) {
271 mutex_exit(&sc->lock);
272 return EBUSY;
273 }
274 iscsi_detaching = true;
275 mutex_exit(&sc->lock);
276
277 kill_all_sessions();
278 iscsi_destroy_cleanup();
279
280 mutex_destroy(&sc->lock);
281
282 return 0;
283 }
284
285 /******************************************************************************/
286
287 typedef struct quirktab_t {
288 const char *tgt;
289 const char *iqn;
290 uint32_t quirks;
291 } quirktab_t;
292
293 static const quirktab_t quirktab[] = {
294 { "StarWind", "iqn.2008-08.com.starwindsoftware", PQUIRK_ONLYBIG },
295 { "UNH", "iqn.2002-10.edu.unh.",
296 PQUIRK_NOBIGMODESENSE |
297 PQUIRK_NOMODESENSE |
298 PQUIRK_NOSYNCCACHE },
299 { "NetBSD", "iqn.1994-04.org.netbsd.", 0 },
300 { "Unknown", "unknown", 0 },
301 { NULL, NULL, 0 }
302 };
303
304 /* loop through the quirktab looking for a match on target name */
305 static const quirktab_t *
306 getquirks(const char *iqn)
307 {
308 const quirktab_t *qp;
309 size_t iqnlen, quirklen;
310
311 if (iqn == NULL)
312 iqn = "unknown";
313 iqnlen = strlen(iqn);
314 for (qp = quirktab ; qp->iqn ; qp++) {
315 quirklen = strlen(qp->iqn);
316 if (quirklen > iqnlen)
317 continue;
318 if (memcmp(qp->iqn, iqn, quirklen) == 0)
319 break;
320 }
321 return qp;
322 }
323
324 /******************************************************************************/
325
326 /*
327 * map_session
328 * This (indirectly) maps the existing LUNs for a target to SCSI devices
329 * by going through config_found to tell any child drivers that there's
330 * a new adapter.
331 * Note that each session is equivalent to a SCSI adapter.
332 *
333 * Parameter: the session pointer
334 *
335 * Returns: 1 on success, 0 on failure
336 *
337 * ToDo: Figuring out how to handle more than one LUN. It appears that
338 * the NetBSD SCSI LUN discovery doesn't use "report LUNs", and instead
339 * goes through the LUNs sequentially, stopping somewhere on the way if it
340 * gets an error. We may have to do some LUN mapping in here if this is
341 * really how things work.
342 */
343
344 int
345 map_session(session_t *session, device_t dev)
346 {
347 struct scsipi_adapter *adapt = &session->sc_adapter;
348 struct scsipi_channel *chan = &session->sc_channel;
349 const quirktab_t *tgt;
350
351 /*
352 * Fill in the scsipi_adapter.
353 */
354 adapt->adapt_dev = dev;
355 adapt->adapt_nchannels = 1;
356 adapt->adapt_request = iscsi_scsipi_request;
357 adapt->adapt_minphys = iscsi_minphys;
358 adapt->adapt_openings = CCBS_PER_SESSION;
359 adapt->adapt_max_periph = CCBS_PER_SESSION;
360
361 /*
362 * Fill in the scsipi_channel.
363 */
364 if ((tgt = getquirks(chan->chan_name)) == NULL) {
365 tgt = getquirks("unknown");
366 }
367 chan->chan_name = tgt->tgt;
368 chan->chan_defquirks = tgt->quirks;
369 chan->chan_adapter = adapt;
370 chan->chan_bustype = &scsi_bustype;
371 chan->chan_channel = 0;
372 chan->chan_flags = SCSIPI_CHAN_NOSETTLE;
373 chan->chan_ntargets = 1;
374 chan->chan_nluns = 16; /* ToDo: ??? */
375 chan->chan_id = session->id;
376
377 session->child_dev = config_found(dev, chan, scsiprint);
378
379 return session->child_dev != NULL;
380 }
381
382
383 /*
384 * unmap_session
385 * This (indirectly) unmaps the existing all LUNs for a target by
386 * telling the config system that the adapter has detached.
387 *
388 * Parameter: the session pointer
389 *
390 * Returns: 1 on success, 0 on failure
391 */
392
393 int
394 unmap_session(session_t *session)
395 {
396 device_t dev;
397 int rv = 1;
398
399 if ((dev = session->child_dev) != NULL) {
400 session->child_dev = NULL;
401 if (config_detach(dev, 0))
402 rv = 0;
403 }
404
405 return rv;
406 }
407
408 /******************************************************************************/
409
410 /*****************************************************************************
411 * SCSI interface routines
412 *****************************************************************************/
413
414 /*
415 * iscsi_scsipi_request:
416 * Perform a request for the SCSIPI layer.
417 */
418
419 void
420 iscsi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
421 void *arg)
422 {
423 struct scsipi_adapter *adapt = chan->chan_adapter;
424 struct scsipi_xfer *xs;
425 session_t *session;
426 int flags;
427 struct scsipi_xfer_mode *xm;
428
429 session = (session_t *) adapt; /* adapter is first field in session */
430
431 switch (req) {
432 case ADAPTER_REQ_RUN_XFER:
433 DEB(9, ("ISCSI: scsipi_request RUN_XFER\n"));
434 xs = arg;
435 flags = xs->xs_control;
436
437 if ((flags & XS_CTL_POLL) != 0) {
438 xs->error = XS_DRIVER_STUFFUP;
439 DEBOUT(("Run Xfer request with polling\n"));
440 scsipi_done(xs);
441 return;
442 }
443 /*
444 * NOTE: It appears that XS_CTL_DATA_UIO is not actually used anywhere.
445 * Since it really would complicate matters to handle offsets
446 * into scatter-gather lists, and a number of other drivers don't
447 * handle uio-based data as well, XS_CTL_DATA_UIO isn't
448 * implemented in this driver (at least for now).
449 */
450 if (flags & XS_CTL_DATA_UIO) {
451 xs->error = XS_DRIVER_STUFFUP;
452 DEBOUT(("Run Xfer with data in UIO\n"));
453 scsipi_done(xs);
454 return;
455 }
456
457 send_run_xfer(session, xs);
458 DEB(9, ("scsipi_req returns\n"));
459 return;
460
461 case ADAPTER_REQ_GROW_RESOURCES:
462 DEB(5, ("ISCSI: scsipi_request GROW_RESOURCES\n"));
463 return;
464
465 case ADAPTER_REQ_SET_XFER_MODE:
466 DEB(5, ("ISCSI: scsipi_request SET_XFER_MODE\n"));
467 xm = (struct scsipi_xfer_mode *)arg;
468 xm->xm_mode = PERIPH_CAP_TQING;
469 scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
470 return;
471
472 default:
473 break;
474 }
475 DEBOUT(("ISCSI: scsipi_request with invalid REQ code %d\n", req));
476 }
477
478 /* cap the transfer at 64K */
479 #define ISCSI_MAX_XFER 65536
480
481 /*
482 * iscsi_minphys:
483 * Limit a transfer to our maximum transfer size.
484 */
485
486 void
487 iscsi_minphys(struct buf *bp)
488 {
489 if (bp->b_bcount > ISCSI_MAX_XFER) {
490 bp->b_bcount = ISCSI_MAX_XFER;
491 }
492 }
493
494 /*****************************************************************************
495 * SCSI job execution helper routines
496 *****************************************************************************/
497
498 /*
499 * iscsi_done:
500 *
501 * A CCB has completed execution. Pass the status back to the
502 * upper layer.
503 */
504 void
505 iscsi_done(ccb_t *ccb)
506 {
507 struct scsipi_xfer *xs = ccb->xs;
508 DEB(9, ("iscsi_done\n"));
509
510 if (xs != NULL) {
511 xs->resid = ccb->residual;
512
513 switch (ccb->status) {
514 case ISCSI_STATUS_SUCCESS:
515 xs->error = XS_NOERROR;
516 xs->status = SCSI_OK;
517 break;
518
519 case ISCSI_STATUS_CHECK_CONDITION:
520 xs->error = XS_SENSE;
521 xs->status = SCSI_CHECK;
522 break;
523
524 case ISCSI_STATUS_TARGET_BUSY:
525 xs->error = XS_BUSY;
526 xs->status = SCSI_BUSY;
527 break;
528
529 case ISCSI_STATUS_SOCKET_ERROR:
530 case ISCSI_STATUS_TIMEOUT:
531 xs->error = XS_SELTIMEOUT;
532 xs->status = SCSI_BUSY;
533 break;
534
535 case ISCSI_STATUS_NO_RESOURCES:
536 xs->error = XS_BUSY;
537 xs->status = SCSI_QUEUE_FULL;
538 break;
539
540 default:
541 xs->error = XS_DRIVER_STUFFUP;
542 break;
543 }
544
545 DEB(99, ("Calling scsipi_done (%p), err = %d\n", xs, xs->error));
546 KERNEL_LOCK(1, curlwp);
547 scsipi_done(xs);
548 KERNEL_UNLOCK_ONE(curlwp);
549 DEB(99, ("scsipi_done returned\n"));
550 } else {
551 DEBOUT(("ISCSI: iscsi_done CCB %p without XS\n", ccb));
552 }
553 }
554
555 SYSCTL_SETUP(sysctl_iscsi_setup, "ISCSI subtree setup")
556 {
557 const struct sysctlnode *node = NULL;
558
559 sysctl_createv(clog, 0, NULL, &node,
560 CTLFLAG_PERMANENT,
561 CTLTYPE_NODE, "iscsi",
562 SYSCTL_DESCR("iscsi controls"),
563 NULL, 0, NULL, 0,
564 CTL_HW, CTL_CREATE, CTL_EOL);
565
566 #ifdef ISCSI_DEBUG
567 sysctl_createv(clog, 0, &node, NULL,
568 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
569 CTLTYPE_INT, "debug",
570 SYSCTL_DESCR("debug level"),
571 NULL, 0, &iscsi_debug_level, sizeof(iscsi_debug_level),
572 CTL_CREATE, CTL_EOL);
573 #endif
574 }
575
576
577 /* Kernel Module support */
578
579 #include <sys/module.h>
580
581 MODULE(MODULE_CLASS_DRIVER, iscsi, NULL); /* Possibly a builtin module */
582
583 #ifdef _MODULE
584 static const struct cfiattrdata ibescsi_info = { "scsi", 1,
585 {{"channel", "-1", -1},}
586 };
587
588 static const struct cfiattrdata *const iscsi_attrs[] = { &ibescsi_info, NULL };
589
590 CFDRIVER_DECL(iscsi, DV_DULL, iscsi_attrs);
591
592 static struct cfdata iscsi_cfdata[] = {
593 {
594 .cf_name = "iscsi",
595 .cf_atname = "iscsi",
596 .cf_unit = 0, /* Only unit 0 is ever used */
597 .cf_fstate = FSTATE_NOTFOUND,
598 .cf_loc = NULL,
599 .cf_flags = 0,
600 .cf_pspec = NULL,
601 },
602 { NULL, NULL, 0, 0, NULL, 0, NULL }
603 };
604 #endif
605
606 static int
607 iscsi_modcmd(modcmd_t cmd, void *arg)
608 {
609 #ifdef _MODULE
610 devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
611 int error;
612 static struct sysctllog *clog;
613 #endif
614
615 switch (cmd) {
616 case MODULE_CMD_INIT:
617 #ifdef _MODULE
618 error = config_cfdriver_attach(&iscsi_cd);
619 if (error) {
620 return error;
621 }
622
623 error = config_cfattach_attach(iscsi_cd.cd_name, &iscsi_ca);
624 if (error) {
625 config_cfdriver_detach(&iscsi_cd);
626 aprint_error("%s: unable to register cfattach\n",
627 iscsi_cd.cd_name);
628 return error;
629 }
630
631 error = config_cfdata_attach(iscsi_cfdata, 1);
632 if (error) {
633 aprint_error("%s: unable to attach cfdata\n",
634 iscsi_cd.cd_name);
635 config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
636 config_cfdriver_detach(&iscsi_cd);
637 return error;
638 }
639
640 error = devsw_attach(iscsi_cd.cd_name, NULL, &bmajor,
641 &iscsi_cdevsw, &cmajor);
642 if (error) {
643 aprint_error("%s: unable to register devsw\n",
644 iscsi_cd.cd_name);
645 config_cfdata_detach(iscsi_cfdata);
646 config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
647 config_cfdriver_detach(&iscsi_cd);
648 return error;
649 }
650
651 if (config_attach_pseudo(iscsi_cfdata) == NULL) {
652 aprint_error("%s: config_attach_pseudo failed\n",
653 iscsi_cd.cd_name);
654 config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
655 config_cfdriver_detach(&iscsi_cd);
656 return ENXIO;
657 }
658
659 sysctl_iscsi_setup(&clog);
660 #endif
661 return 0;
662 break;
663
664 case MODULE_CMD_FINI:
665 #ifdef _MODULE
666 sysctl_teardown(&clog);
667
668 error = config_cfdata_detach(iscsi_cfdata);
669 if (error)
670 return error;
671
672 config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
673 config_cfdriver_detach(&iscsi_cd);
674 devsw_detach(NULL, &iscsi_cdevsw);
675 #endif
676 return 0;
677 break;
678
679 case MODULE_CMD_AUTOUNLOAD:
680 return EBUSY;
681 break;
682
683 default:
684 return ENOTTY;
685 break;
686 }
687 }
688