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