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