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