iscsi_main.c revision 1.14 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, struct lwp *l)
125 {
126
127 DEB(99, ("ISCSI Open\n"));
128 return 0;
129 }
130
131 int
132 iscsiclose(dev_t dev, int flag, int mode, struct lwp *l)
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 const char *iqn;
232 uint32_t quirks;
233 } quirktab_t;
234
235 static const quirktab_t quirktab[] = {
236 { "StarWind", "iqn.2008-08.com.starwindsoftware", PQUIRK_ONLYBIG },
237 { "UNH", "iqn.2002-10.edu.unh.",
238 PQUIRK_NOBIGMODESENSE |
239 PQUIRK_NOMODESENSE |
240 PQUIRK_NOSYNCCACHE },
241 { "NetBSD", "iqn.1994-04.org.netbsd.", 0 },
242 { "Unknown", "unknown", 0 },
243 { NULL, NULL, 0 }
244 };
245
246 /* loop through the quirktab looking for a match on target name */
247 static const quirktab_t *
248 getquirks(const char *iqn)
249 {
250 const quirktab_t *qp;
251 size_t iqnlen, quirklen;
252
253 if (iqn == NULL)
254 iqn = "unknown";
255 iqnlen = strlen(iqn);
256 for (qp = quirktab ; qp->iqn ; qp++) {
257 quirklen = strlen(qp->iqn);
258 if (quirklen > iqnlen)
259 continue;
260 if (memcmp(qp->iqn, iqn, quirklen) == 0)
261 break;
262 }
263 return qp;
264 }
265
266 /******************************************************************************/
267
268 /*
269 * map_session
270 * This (indirectly) maps the existing LUNs for a target to SCSI devices
271 * by going through config_found to tell any child drivers that there's
272 * a new adapter.
273 * Note that each session is equivalent to a SCSI adapter.
274 *
275 * Parameter: the session pointer
276 *
277 * Returns: 1 on success, 0 on failure
278 *
279 * ToDo: Figuring out how to handle more than one LUN. It appears that
280 * the NetBSD SCSI LUN discovery doesn't use "report LUNs", and instead
281 * goes through the LUNs sequentially, stopping somewhere on the way if it
282 * gets an error. We may have to do some LUN mapping in here if this is
283 * really how things work.
284 */
285
286 int
287 map_session(session_t *session)
288 {
289 struct scsipi_adapter *adapt = &session->sc_adapter;
290 struct scsipi_channel *chan = &session->sc_channel;
291 const quirktab_t *tgt;
292
293 if (sc == NULL) {
294 /* we haven't gone through the config process */
295 /* (shouldn't happen) */
296 DEBOUT(("Map: No device pointer!\n"));
297 return 0;
298 }
299 /*
300 * Fill in the scsipi_adapter.
301 */
302 adapt->adapt_dev = sc->sc_dev;
303 adapt->adapt_nchannels = 1;
304 adapt->adapt_request = iscsi_scsipi_request;
305 adapt->adapt_minphys = iscsi_minphys;
306 adapt->adapt_openings = CCBS_PER_SESSION;
307 adapt->adapt_max_periph = CCBS_PER_SESSION;
308
309 /*
310 * Fill in the scsipi_channel.
311 */
312 if ((tgt = getquirks(chan->chan_name)) == NULL) {
313 tgt = getquirks("unknown");
314 }
315 chan->chan_name = tgt->tgt;
316 chan->chan_defquirks = tgt->quirks;
317 chan->chan_adapter = adapt;
318 chan->chan_bustype = &scsi_bustype;
319 chan->chan_channel = 0;
320 chan->chan_flags = SCSIPI_CHAN_NOSETTLE;
321 chan->chan_ntargets = 1;
322 chan->chan_nluns = 16; /* ToDo: ??? */
323 chan->chan_id = session->id;
324
325 session->child_dev = config_found(sc->sc_dev, chan, scsiprint);
326
327 return session->child_dev != NULL;
328 }
329
330
331 /*
332 * unmap_session
333 * This (indirectly) unmaps the existing all LUNs for a target by
334 * telling the config system that the adapter has detached.
335 *
336 * Parameter: the session pointer
337 *
338 * Returns: 1 on success, 0 on failure
339 */
340
341 int
342 unmap_session(session_t *session)
343 {
344 device_t dev;
345 int rv = 1;
346
347 if ((dev = session->child_dev) != NULL) {
348 session->child_dev = NULL;
349 if (config_detach(dev, 0))
350 rv = 0;
351 }
352
353 return rv;
354 }
355
356 /******************************************************************************/
357
358 /*****************************************************************************
359 * SCSI interface routines
360 *****************************************************************************/
361
362 /*
363 * iscsi_scsipi_request:
364 * Perform a request for the SCSIPI layer.
365 */
366
367 void
368 iscsi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
369 void *arg)
370 {
371 struct scsipi_adapter *adapt = chan->chan_adapter;
372 struct scsipi_xfer *xs;
373 session_t *session;
374 int flags;
375 struct scsipi_xfer_mode *xm;
376
377 session = (session_t *) adapt; /* adapter is first field in session */
378
379 switch (req) {
380 case ADAPTER_REQ_RUN_XFER:
381 DEB(9, ("ISCSI: scsipi_request RUN_XFER\n"));
382 xs = arg;
383 flags = xs->xs_control;
384
385 if ((flags & XS_CTL_POLL) != 0) {
386 xs->error = XS_DRIVER_STUFFUP;
387 DEBOUT(("Run Xfer request with polling\n"));
388 scsipi_done(xs);
389 return;
390 }
391 /*
392 * NOTE: It appears that XS_CTL_DATA_UIO is not actually used anywhere.
393 * Since it really would complicate matters to handle offsets
394 * into scatter-gather lists, and a number of other drivers don't
395 * handle uio-based data as well, XS_CTL_DATA_UIO isn't
396 * implemented in this driver (at least for now).
397 */
398 if (flags & XS_CTL_DATA_UIO) {
399 xs->error = XS_DRIVER_STUFFUP;
400 DEBOUT(("Run Xfer with data in UIO\n"));
401 scsipi_done(xs);
402 return;
403 }
404
405 send_run_xfer(session, xs);
406 DEB(9, ("scsipi_req returns\n"));
407 return;
408
409 case ADAPTER_REQ_GROW_RESOURCES:
410 DEBOUT(("ISCSI: scsipi_request GROW_RESOURCES\n"));
411 return;
412
413 case ADAPTER_REQ_SET_XFER_MODE:
414 DEB(5, ("ISCSI: scsipi_request SET_XFER_MODE\n"));
415 xm = (struct scsipi_xfer_mode *)arg;
416 xm->xm_mode = PERIPH_CAP_TQING;
417 scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
418 return;
419
420 default:
421 break;
422 }
423 DEBOUT(("ISCSI: scsipi_request with invalid REQ code %d\n", req));
424 }
425
426 /* cap the transfer at 64K */
427 #define ISCSI_MAX_XFER 65536
428
429 /*
430 * iscsi_minphys:
431 * Limit a transfer to our maximum transfer size.
432 */
433
434 void
435 iscsi_minphys(struct buf *bp)
436 {
437 if (bp->b_bcount > ISCSI_MAX_XFER) {
438 bp->b_bcount = ISCSI_MAX_XFER;
439 }
440 }
441
442 /*****************************************************************************
443 * SCSI job execution helper routines
444 *****************************************************************************/
445
446 /*
447 * iscsi_done:
448 *
449 * A CCB has completed execution. Pass the status back to the
450 * upper layer.
451 */
452 void
453 iscsi_done(ccb_t *ccb)
454 {
455 struct scsipi_xfer *xs = ccb->xs;
456 /*DEBOUT (("iscsi_done\n")); */
457
458 if (xs != NULL) {
459 xs->resid = ccb->residual;
460
461 switch (ccb->status) {
462 case ISCSI_STATUS_SUCCESS:
463 xs->error = 0;
464 break;
465
466 case ISCSI_STATUS_CHECK_CONDITION:
467 xs->error = XS_SENSE;
468 #ifdef ISCSI_DEBUG
469 {
470 uint8_t *s = (uint8_t *) (&xs->sense);
471 DEB(5, ("Scsipi_done, error=XS_SENSE, sense data=%02x "
472 "%02x %02x %02x...\n",
473 s[0], s[1], s[2], s[3]));
474 }
475 #endif
476 break;
477
478 case ISCSI_STATUS_TARGET_BUSY:
479 xs->error = XS_BUSY;
480 break;
481
482 case ISCSI_STATUS_SOCKET_ERROR:
483 case ISCSI_STATUS_TIMEOUT:
484 xs->error = XS_SELTIMEOUT;
485 break;
486
487 default:
488 xs->error = XS_DRIVER_STUFFUP;
489 break;
490 }
491
492 DEB(99, ("Calling scsipi_done (%p), err = %d\n", xs, xs->error));
493 scsipi_done(xs);
494 DEB(99, ("scsipi_done returned\n"));
495 }
496 }
497
498 /* Kernel Module support */
499
500 #include <sys/module.h>
501
502 MODULE(MODULE_CLASS_DRIVER, iscsi, NULL); /* Possibly a builtin module */
503
504 #ifdef _MODULE
505 static const struct cfiattrdata ibescsi_info = { "scsi", 1,
506 {{"channel", "-1", -1},}
507 };
508
509 static const struct cfiattrdata *const iscsi_attrs[] = { &ibescsi_info, NULL };
510
511 CFDRIVER_DECL(iscsi, DV_DULL, iscsi_attrs);
512
513 static struct cfdata iscsi_cfdata[] = {
514 {
515 .cf_name = "iscsi",
516 .cf_atname = "iscsi",
517 .cf_unit = 0, /* Only unit 0 is ever used */
518 .cf_fstate = FSTATE_NOTFOUND,
519 .cf_loc = NULL,
520 .cf_flags = 0,
521 .cf_pspec = NULL,
522 },
523 { NULL, NULL, 0, 0, NULL, 0, NULL }
524 };
525 #endif
526
527 static int
528 iscsi_modcmd(modcmd_t cmd, void *arg)
529 {
530 #ifdef _MODULE
531 devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
532 int error;
533 #endif
534
535 switch (cmd) {
536 case MODULE_CMD_INIT:
537 #ifdef _MODULE
538 error = config_cfdriver_attach(&iscsi_cd);
539 if (error) {
540 return error;
541 }
542
543 error = config_cfattach_attach(iscsi_cd.cd_name, &iscsi_ca);
544 if (error) {
545 config_cfdriver_detach(&iscsi_cd);
546 aprint_error("%s: unable to register cfattach\n",
547 iscsi_cd.cd_name);
548 return error;
549 }
550
551 error = config_cfdata_attach(iscsi_cfdata, 1);
552 if (error) {
553 aprint_error("%s: unable to attach cfdata\n",
554 iscsi_cd.cd_name);
555 config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
556 config_cfdriver_detach(&iscsi_cd);
557 return error;
558 }
559
560 error = devsw_attach(iscsi_cd.cd_name, NULL, &bmajor,
561 &iscsi_cdevsw, &cmajor);
562 if (error) {
563 aprint_error("%s: unable to register devsw\n",
564 iscsi_cd.cd_name);
565 config_cfdata_detach(iscsi_cfdata);
566 config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
567 config_cfdriver_detach(&iscsi_cd);
568 return error;
569 }
570
571 if (config_attach_pseudo(iscsi_cfdata) == NULL) {
572 aprint_error("%s: config_attach_pseudo failed\n",
573 iscsi_cd.cd_name);
574 config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
575 config_cfdriver_detach(&iscsi_cd);
576 return ENXIO;
577 }
578 #endif
579 return 0;
580 break;
581
582 case MODULE_CMD_FINI:
583 #ifdef _MODULE
584 error = config_cfdata_detach(iscsi_cfdata);
585 if (error)
586 return error;
587
588 config_cfattach_detach(iscsi_cd.cd_name, &iscsi_ca);
589 config_cfdriver_detach(&iscsi_cd);
590 devsw_detach(NULL, &iscsi_cdevsw);
591 #endif
592 return 0;
593 break;
594
595 case MODULE_CMD_AUTOUNLOAD:
596 return EBUSY;
597 break;
598
599 default:
600 return ENOTTY;
601 break;
602 }
603 }
604