1 /*- 2 * Copyright (c) 2005-2008 Daniel Braniss <danny@cs.huji.ac.il> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/sys/dev/iscsi/initiator/iscsi_subr.c,v 1.2 2008/11/25 07:17:11 scottl Exp $ 27 */ 28 /* 29 | $Id: iscsi_subr.c,v 1.17 2006/11/26 14:50:43 danny Exp danny $ 30 */ 31 32 #include "opt_iscsi_initiator.h" 33 34 #include <sys/param.h> 35 #include <sys/kernel.h> 36 #include <sys/callout.h> 37 #include <sys/malloc.h> 38 #include <sys/mbuf.h> 39 #include <sys/kthread.h> 40 #include <sys/lock.h> 41 #include <sys/uio.h> 42 #include <sys/sysctl.h> 43 #include <sys/systm.h> 44 #include <sys/globaldata.h> 45 46 #include <bus/cam/cam.h> 47 #include <bus/cam/cam_ccb.h> 48 #include <bus/cam/cam_sim.h> 49 #include <bus/cam/cam_xpt_sim.h> 50 #include <bus/cam/cam_periph.h> 51 #include <bus/cam/scsi/scsi_message.h> 52 #include <sys/eventhandler.h> 53 54 #include <sys/mutex2.h> 55 56 #include <dev/disk/iscsi/initiator/iscsi.h> 57 #include <dev/disk/iscsi/initiator/iscsivar.h> 58 59 /* 60 | Interface to the SCSI layer 61 */ 62 void 63 iscsi_r2t(isc_session_t *sp, pduq_t *opq, pduq_t *pq) 64 { 65 union ccb *ccb = opq->ccb; 66 struct ccb_scsiio *csio = &ccb->csio; 67 pdu_t *opp = &opq->pdu; 68 bhs_t *bhp = &opp->ipdu.bhs; 69 r2t_t *r2t = &pq->pdu.ipdu.r2t; 70 pduq_t *wpq; 71 int error; 72 73 debug_called(8); 74 sdebug(4, "itt=%x r2tSN=%d bo=%x ddtl=%x W=%d", ntohl(r2t->itt), 75 ntohl(r2t->r2tSN), ntohl(r2t->bo), ntohl(r2t->ddtl), opp->ipdu.scsi_req.W); 76 77 switch(bhp->opcode) { 78 case ISCSI_SCSI_CMD: 79 if(opp->ipdu.scsi_req.W) { 80 data_out_t *cmd; 81 u_int ddtl = ntohl(r2t->ddtl); 82 u_int edtl = ntohl(opp->ipdu.scsi_req.edtlen); 83 u_int bleft, bs, dsn, bo; 84 caddr_t bp = csio->data_ptr; 85 86 bo = ntohl(r2t->bo); 87 bleft = ddtl; 88 89 if(sp->opt.maxXmitDataSegmentLength > 0) // danny's RFC 90 bs = MIN(sp->opt.maxXmitDataSegmentLength, ddtl); 91 else 92 bs = ddtl; 93 dsn = 0; 94 sdebug(4, "edtl=%x ddtl=%x bo=%x dsn=%x bs=%x maxX=%x", 95 edtl, ddtl, bo, dsn, bs, sp->opt.maxXmitDataSegmentLength); 96 while(bleft > 0) { 97 wpq = pdu_alloc(sp->isc, M_NOWAIT); // testing ... 98 if(wpq == NULL) { 99 sdebug(3, "itt=%x r2tSN=%d bo=%x ddtl=%x W=%d", ntohl(r2t->itt), 100 ntohl(r2t->r2tSN), ntohl(r2t->bo), ntohl(r2t->ddtl), opp->ipdu.scsi_req.W); 101 sdebug(1, "npdu_max=%d npdu_alloc=%d", sp->isc->npdu_max, sp->isc->npdu_alloc); 102 103 while((wpq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) { 104 sdebug(2, "waiting..."); 105 #if __FreeBSD_version >= 700000 106 pause("isc_r2t", 5*hz); 107 #else 108 tsleep(sp->isc, 0, "isc_r2t", 5*hz); 109 #endif 110 } 111 } 112 cmd = &wpq->pdu.ipdu.data_out; 113 cmd->opcode = ISCSI_WRITE_DATA; 114 cmd->lun[0] = r2t->lun[0]; 115 cmd->lun[1] = r2t->lun[1]; 116 cmd->ttt = r2t->ttt; 117 cmd->itt = r2t->itt; 118 119 cmd->dsn = htonl(dsn); 120 cmd->bo = htonl(bo); 121 122 cmd->F = (bs < bleft)? 0: 1; // is this the last one? 123 bs = MIN(bs, bleft); 124 125 wpq->pdu.ds_len = bs; 126 wpq->pdu.ds = bp; 127 128 error = isc_qout(sp, wpq); 129 sdebug(6, "bs=%x bo=%x bp=%p dsn=%x error=%d", bs, bo, bp, dsn, error); 130 if(error) 131 break; 132 bo += bs; 133 bp += bs; 134 bleft -= bs; 135 dsn++; 136 } 137 } 138 break; 139 140 default: 141 // XXX: should not happen ... 142 xdebug("huh? opcode=0x%x", bhp->opcode); 143 } 144 } 145 146 static int 147 getSenseData(u_int status, union ccb *ccb, pduq_t *pq) 148 { 149 pdu_t *pp = &pq->pdu; 150 struct ccb_scsiio *scsi = (struct ccb_scsiio *)ccb; 151 struct scsi_sense_data *sense = &scsi->sense_data; 152 struct mbuf *m = pq->mp; 153 scsi_rsp_t *cmd = &pp->ipdu.scsi_rsp; 154 caddr_t bp; 155 int sense_len, mustfree = 0; 156 157 bp = mtod(pq->mp, caddr_t); 158 if((sense_len = scsi_2btoul(bp)) == 0) 159 return 0; 160 debug(4, "sense_len=%d", sense_len); 161 /* 162 | according to the specs, the sense data cannot 163 | be larger than 252 ... 164 */ 165 if(sense_len > m->m_len) { 166 bp = kmalloc(sense_len, M_ISCSI, M_WAITOK); 167 debug(3, "calling i_mbufcopy(len=%d)", sense_len); 168 i_mbufcopy(pq->mp, bp, sense_len); 169 mustfree++; 170 } 171 scsi->scsi_status = status; 172 173 bcopy(bp+2, sense, min(sense_len, scsi->sense_len)); 174 scsi->sense_resid = 0; 175 if(cmd->flag & (BIT(1)|BIT(2))) 176 scsi->sense_resid = ntohl(pp->ipdu.scsi_rsp.rcnt); 177 debug(3, "sense_len=%d rcnt=%d sense_resid=%d dsl=%d error_code=%x flags=%x", 178 sense_len, 179 ntohl(pp->ipdu.scsi_rsp.rcnt), scsi->sense_resid, 180 pp->ds_len, sense->error_code, sense->flags); 181 182 if(mustfree) 183 kfree(bp, M_ISCSI); 184 185 return 1; 186 } 187 188 /* 189 | Some information is from SAM draft. 190 */ 191 static void 192 _scsi_done(struct isc_softc *isp, u_int response, u_int status, union ccb *ccb, pduq_t *pq) 193 { 194 struct ccb_hdr *ccb_h = &ccb->ccb_h; 195 196 debug_called(8); 197 198 if(status || response) { 199 debug(3, "response=%x status=%x ccb=%p pq=%p", response, status, ccb, pq); 200 if(pq != NULL) 201 debug(3, "mp=%p buf=%p len=%d", pq->mp, pq->buf, pq->len); 202 } 203 ccb_h->status = 0; 204 switch(response) { 205 case 0: // Command Completed at Target 206 switch(status) { 207 case 0: // Good, all is ok 208 ccb_h->status = CAM_REQ_CMP; 209 break; 210 211 case 0x02: // Check Condition 212 if((pq != NULL) && (pq->mp != NULL) && getSenseData(status, ccb, pq)) 213 ccb_h->status |= CAM_AUTOSNS_VALID; 214 215 case 0x14: // Intermediate-Condition Met 216 case 0x10: // Intermediate 217 case 0x04: // Condition Met 218 ccb_h->status |= CAM_SCSI_STATUS_ERROR; 219 break; 220 221 case 0x08: 222 ccb_h->status = CAM_BUSY; 223 break; 224 225 case 0x18: // Reservation Conflict 226 case 0x28: // Task Set Full 227 ccb_h->status = CAM_REQUEUE_REQ; 228 break; 229 default: 230 //case 0x22: // Command Terminated 231 //case 0x30: // ACA Active 232 //case 0x40: // Task Aborted 233 ccb_h->status = CAM_REQ_CMP_ERR; //CAM_REQ_ABORTED; 234 } 235 break; 236 237 default: 238 if((response >= 0x80) && (response <= 0xFF)) { 239 // Vendor specific ... 240 } 241 case 1: // target failure 242 ccb_h->status = CAM_REQ_CMP_ERR; //CAM_REQ_ABORTED; 243 break; 244 } 245 debug(5, "ccb_h->status=%x", ccb_h->status); 246 247 XPT_DONE(isp, ccb); 248 } 249 250 /* 251 | returns the lowest cmdseq that was not acked 252 */ 253 int 254 iscsi_requeue(isc_session_t *sp) 255 { 256 pduq_t *pq; 257 u_int i, n, last; 258 259 debug_called(8); 260 last = -1; 261 i = 0; 262 sp->flags |= ISC_HOLD; 263 while((pq = i_dqueue_hld(sp)) != NULL) { 264 i++; 265 _scsi_done(sp->isc, 0, 0x28, pq->ccb, NULL); 266 n = ntohl(pq->pdu.ipdu.bhs.CmdSN); 267 if(last > n) 268 last = n; 269 sdebug(2, "last=%x n=%x", last, n); 270 pdu_free(sp->isc, pq); 271 } 272 sp->flags &= ~ISC_HOLD; 273 return i? last: sp->sn.cmd; 274 } 275 276 int 277 i_pdu_flush(isc_session_t *sp) 278 { 279 int n = 0; 280 pduq_t *pq; 281 282 debug_called(8); 283 while((pq = i_dqueue_rsp(sp)) != NULL) { 284 pdu_free(sp->isc, pq); 285 n++; 286 } 287 while((pq = i_dqueue_rsv(sp)) != NULL) { 288 pdu_free(sp->isc, pq); 289 n++; 290 } 291 while((pq = i_dqueue_snd(sp, -1)) != NULL) { 292 pdu_free(sp->isc, pq); 293 n++; 294 } 295 while((pq = i_dqueue_hld(sp)) != NULL) { 296 pdu_free(sp->isc, pq); 297 n++; 298 } 299 while((pq = i_dqueue_wsnd(sp)) != NULL) { 300 pdu_free(sp->isc, pq); 301 n++; 302 } 303 if(n != 0) 304 xdebug("%d pdus recovered, should have been ZERO!", n); 305 return n; 306 } 307 /* 308 | called from ism_destroy. 309 */ 310 void 311 iscsi_cleanup(isc_session_t *sp) 312 { 313 pduq_t *pq, *pqtmp; 314 315 debug_called(8); 316 317 TAILQ_FOREACH_MUTABLE(pq, &sp->hld, pq_link, pqtmp) { 318 sdebug(3, "hld pq=%p", pq); 319 if(pq->ccb) 320 _scsi_done(sp->isc, 1, 0x40, pq->ccb, NULL); 321 TAILQ_REMOVE(&sp->hld, pq, pq_link); 322 pdu_free(sp->isc, pq); 323 } 324 while((pq = i_dqueue_snd(sp, BIT(0)|BIT(1)|BIT(2))) != NULL) { 325 sdebug(3, "pq=%p", pq); 326 if(pq->ccb) 327 _scsi_done(sp->isc, 1, 0x40, pq->ccb, NULL); 328 pdu_free(sp->isc, pq); 329 } 330 331 wakeup(&sp->rsp); 332 } 333 334 void 335 iscsi_done(isc_session_t *sp, pduq_t *opq, pduq_t *pq) 336 { 337 pdu_t *pp = &pq->pdu; 338 scsi_rsp_t *cmd = &pp->ipdu.scsi_rsp; 339 340 debug_called(8); 341 342 _scsi_done(sp->isc, cmd->response, cmd->status, opq->ccb, pq); 343 344 pdu_free(sp->isc, opq); 345 } 346 347 // see RFC 3720, 10.9.1 page 146 348 /* 349 | NOTE: 350 | the call to isc_stop_receiver is a kludge, 351 | instead, it should be handled by the userland controller, 352 | but that means that there should be a better way, other than 353 | sending a signal. Somehow, this packet should be supplied to 354 | the userland via read. 355 */ 356 void 357 iscsi_async(isc_session_t *sp, pduq_t *pq) 358 { 359 pdu_t *pp = &pq->pdu; 360 async_t *cmd = &pp->ipdu.async; 361 362 debug_called(8); 363 364 sdebug(3, "asyncevent=0x%x asyncVCode=0x%0x", cmd->asyncEvent, cmd->asyncVCode); 365 switch(cmd->asyncEvent) { 366 case 0: // check status ... 367 break; 368 369 case 1: // target request logout 370 isc_stop_receiver(sp); // XXX: temporary solution 371 break; 372 373 case 2: // target indicates it wants to drop connection 374 isc_stop_receiver(sp); // XXX: temporary solution 375 break; 376 377 case 3: // target indicates it will drop all connections. 378 isc_stop_receiver(sp); // XXX: temporary solution 379 break; 380 381 case 4: // target request parameter negotiation 382 break; 383 384 default: 385 break; 386 } 387 } 388 389 void 390 iscsi_reject(isc_session_t *sp, pduq_t *opq, pduq_t *pq) 391 { 392 union ccb *ccb = opq->ccb; 393 //reject_t *reject = &pq->pdu.ipdu.reject; 394 395 debug_called(8); 396 //XXX: check RFC 10.17.1 (page 176) 397 ccb->ccb_h.status = CAM_REQ_ABORTED; 398 XPT_DONE(sp->isc, ccb); 399 400 pdu_free(sp->isc, opq); 401 } 402 403 /* 404 | deal with lun 405 */ 406 static int 407 dwl(isc_session_t *sp, int lun, u_char *lp) 408 { 409 int i; 410 411 debug_called(8); 412 413 /* 414 | mapping LUN to iSCSI LUN 415 | check the SAM-2 specs 416 | hint: maxLUNS is a small number, cam's LUN is 32bits 417 | iSCSI is 64bits, scsi is ? 418 */ 419 // XXX: check if this will pass the endian test 420 if(lun < 256) { 421 lp[0] = 0; 422 lp[1] = lun; 423 } else 424 if(lun < 16384) { 425 lp[0] = (1 << 5) | ((lun >> 8) & 0x3f); 426 lp[1] = lun & 0xff; 427 } 428 else { 429 xdebug("lun %d: is unsupported!", lun); 430 return -1; 431 } 432 433 for(i = 0; i < sp->target_nluns; i++) 434 if(sp->target_lun[i] == lun) 435 return 0; 436 if(sp->target_nluns < ISCSI_MAX_LUNS) 437 sp->target_lun[sp->target_nluns++] = lun; 438 439 sdebug(3, "nluns=%d lun=%d", sp->target_nluns, lun); 440 441 return 0; 442 } 443 444 /* 445 | encapsulate the scsi command and 446 */ 447 int 448 scsi_encap(struct cam_sim *sim, union ccb *ccb) 449 { 450 struct isc_softc *isp = (struct isc_softc *)cam_sim_softc(sim); 451 isc_session_t *sp; 452 struct ccb_scsiio *csio = &ccb->csio; 453 struct ccb_hdr *ccb_h = &ccb->ccb_h; 454 pduq_t *pq; 455 scsi_req_t *cmd; 456 457 debug_called(8); 458 459 debug(4, "ccb->sp=%p", ccb_h->spriv_ptr0); 460 sp = ccb_h->spriv_ptr0; 461 462 if((pq = pdu_alloc(isp, M_NOWAIT)) == NULL) { 463 debug(2, "ccb->sp=%p", ccb_h->spriv_ptr0); 464 sdebug(1, "pdu_alloc failed sc->npdu_max=%d npdu_alloc=%d", 465 sp->isc->npdu_max, sp->isc->npdu_alloc); 466 while((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) { 467 sdebug(3, "waiting..."); 468 #if __FreeBSD_version >= 700000 469 pause("isc_encap", 5*hz); 470 #else 471 tsleep(sp->isc, 0, "isc_encap", 5*hz); 472 #endif 473 } 474 #if 0 475 sdebug(3, "freezing"); 476 ccb->ccb_h.status = CAM_REQUEUE_REQ; 477 ic_freeze(sp); 478 return 0; 479 #endif 480 } 481 482 #if 0 483 if((sp->flags & ISC_FFPHASE) == 0) { 484 ccb->ccb_h.status = CAM_DEV_NOT_THERE; // CAM_NO_NEXUS; 485 sdebug(3, "no active session with target %d", ccb_h->target_id); 486 goto bad; 487 } 488 #endif 489 cmd = &pq->pdu.ipdu.scsi_req; 490 cmd->opcode = ISCSI_SCSI_CMD; 491 cmd->F = 1; 492 /* 493 | map tag option, default is UNTAGGED 494 */ 495 switch(csio->tag_action) { 496 case MSG_SIMPLE_Q_TAG: cmd->attr = iSCSI_TASK_SIMPLE; break; 497 case MSG_HEAD_OF_Q_TAG: cmd->attr = iSCSI_TASK_ORDER; break; 498 case MSG_ORDERED_Q_TAG: cmd->attr = iSCSI_TASK_HOFQ; break; 499 case MSG_ACA_TASK: cmd->attr = iSCSI_TASK_ACA; break; 500 } 501 502 dwl(sp, ccb_h->target_lun, (u_char *)&cmd->lun); 503 504 if((ccb_h->flags & CAM_CDB_POINTER) != 0) { 505 if((ccb_h->flags & CAM_CDB_PHYS) == 0) { 506 if(csio->cdb_len > 16) { 507 sdebug(3, "oversize cdb %d > 16", csio->cdb_len); 508 goto invalid; 509 } 510 } 511 else { 512 sdebug(3, "not phys"); 513 goto invalid; 514 } 515 } 516 517 if(csio->cdb_len > sizeof(cmd->cdb)) 518 xdebug("guevalt! %d > %ld", csio->cdb_len, (long)sizeof(cmd->cdb)); 519 520 memcpy(cmd->cdb, 521 ccb_h->flags & CAM_CDB_POINTER? csio->cdb_io.cdb_ptr: csio->cdb_io.cdb_bytes, 522 csio->cdb_len); 523 524 cmd->W = (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT; 525 cmd->R = (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN; 526 cmd->edtlen = htonl(csio->dxfer_len); 527 528 pq->ccb = ccb; 529 /* 530 | place it in the out queue 531 */ 532 if(isc_qout(sp, pq) == 0) 533 return 1; 534 invalid: 535 ccb->ccb_h.status = CAM_REQ_INVALID; 536 pdu_free(isp, pq); 537 return 0; 538 } 539 540 int 541 scsi_decap(isc_session_t *sp, pduq_t *opq, pduq_t *pq) 542 { 543 union ccb *ccb = opq->ccb; 544 struct ccb_scsiio *csio = &ccb->csio; 545 pdu_t *opp = &opq->pdu; 546 bhs_t *bhp = &opp->ipdu.bhs; 547 548 debug_called(8); 549 sdebug(6, "pq=%p opq=%p bhp->opcode=0x%x len=%d", 550 pq, opq, bhp->opcode, pq->pdu.ds_len); 551 if(ccb == NULL) { 552 sdebug(1, "itt=0x%x pq=%p opq=%p bhp->opcode=0x%x len=%d", 553 ntohl(pq->pdu.ipdu.bhs.itt), 554 pq, opq, bhp->opcode, pq->pdu.ds_len); 555 xdebug("%d] ccb == NULL!", sp->sid); 556 return 0; 557 } 558 if(pq->pdu.ds_len != 0) { 559 switch(bhp->opcode) { 560 case ISCSI_SCSI_CMD: { 561 scsi_req_t *cmd = &opp->ipdu.scsi_req; 562 sdebug(5, "itt=0x%x opcode=%x R=%d", 563 ntohl(pq->pdu.ipdu.bhs.itt), 564 pq->pdu.ipdu.bhs.opcode, cmd->R); 565 566 switch(pq->pdu.ipdu.bhs.opcode) { 567 case ISCSI_READ_DATA: // SCSI Data in 568 { 569 caddr_t bp = NULL; // = mtod(pq->mp, caddr_t); 570 data_in_t *rcmd = &pq->pdu.ipdu.data_in; 571 572 if(cmd->R) { 573 sdebug(5, "copy to=%p from=%p l1=%d l2=%d mp@%p", 574 csio->data_ptr, bp? mtod(pq->mp, caddr_t): 0, 575 ntohl(cmd->edtlen), pq->pdu.ds_len, pq->mp); 576 if(ntohl(cmd->edtlen) >= pq->pdu.ds_len) { 577 int offset, len = pq->pdu.ds_len; 578 579 if(pq->mp != NULL) { 580 caddr_t dp; 581 582 offset = ntohl(rcmd->bo); 583 dp = csio->data_ptr + offset; 584 i_mbufcopy(pq->mp, dp, len); 585 } 586 } 587 else { 588 xdebug("edtlen=%d < ds_len=%d", 589 ntohl(cmd->edtlen), pq->pdu.ds_len); 590 } 591 } 592 if(rcmd->S) { 593 /* 594 | contains also the SCSI Status 595 */ 596 _scsi_done(sp->isc, 0, rcmd->status, opq->ccb, NULL); 597 return 0; 598 } else 599 return 1; 600 } 601 break; 602 } 603 } 604 default: 605 sdebug(3, "opcode=%02x", bhp->opcode); 606 break; 607 } 608 } 609 /* 610 | XXX: error ... 611 */ 612 return 1; 613 } 614