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