1/*- |
2 * Copyright (c) 2005-2010 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 --- 9 unchanged lines hidden (view full) --- 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 */ 27/* |
28 | $Id: iscsi_subr.c 743 2009-08-08 10:54:53Z danny $ |
29 */ 30 31#include <sys/cdefs.h> |
32__FBSDID("$FreeBSD: head/sys/dev/iscsi/initiator/iscsi_subr.c 211095 2010-08-09 12:36:36Z des $"); |
33 34#include "opt_iscsi_initiator.h" 35 36#include <sys/param.h> 37#include <sys/kernel.h> 38#include <sys/callout.h> 39#include <sys/malloc.h> 40#include <sys/mbuf.h> 41#include <sys/kthread.h> 42#include <sys/lock.h> 43#include <sys/mutex.h> 44#include <sys/uio.h> 45#include <sys/sysctl.h> |
46#include <sys/sx.h> |
47 48#include <cam/cam.h> 49#include <cam/cam_ccb.h> 50#include <cam/cam_sim.h> 51#include <cam/cam_xpt_sim.h> 52#include <cam/cam_periph.h> 53#include <cam/scsi/scsi_message.h> 54#include <sys/eventhandler.h> --- 63 unchanged lines hidden (view full) --- 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_addr = 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; --- 49 unchanged lines hidden (view full) --- 184 185 return 1; 186} 187 188/* 189 | Some information is from SAM draft. 190 */ 191static void |
192_scsi_done(isc_session_t *sp, 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 sdebug(3, "response=%x status=%x ccb=%p pq=%p", response, status, ccb, pq); |
200 if(pq != NULL) |
201 sdebug(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; --- 27 unchanged lines hidden (view full) --- 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 sdebug(5, "ccb_h->status=%x", ccb_h->status); |
246 |
247 XPT_DONE(sp, ccb); |
248} 249 250/* 251 | returns the lowest cmdseq that was not acked 252 */ 253int 254iscsi_requeue(isc_session_t *sp) 255{ 256 pduq_t *pq; 257 u_int i, n, last; 258 259 debug_called(8); |
260 i = last = 0; |
261 sp->flags |= ISC_HOLD; 262 while((pq = i_dqueue_hld(sp)) != NULL) { 263 i++; |
264 if(pq->ccb != NULL) { 265 _scsi_done(sp, 0, 0x28, pq->ccb, NULL); 266 n = ntohl(pq->pdu.ipdu.bhs.CmdSN); 267 if(last==0 || (last > n)) 268 last = n; 269 sdebug(2, "last=%x n=%x", last, n); 270 } |
271 pdu_free(sp->isc, pq); 272 } 273 sp->flags &= ~ISC_HOLD; 274 return i? last: sp->sn.cmd; 275} 276 277int 278i_pdu_flush(isc_session_t *sp) --- 34 unchanged lines hidden (view full) --- 313{ 314 pduq_t *pq, *pqtmp; 315 316 debug_called(8); 317 318 TAILQ_FOREACH_SAFE(pq, &sp->hld, pq_link, pqtmp) { 319 sdebug(3, "hld pq=%p", pq); 320 if(pq->ccb) |
321 _scsi_done(sp, 1, 0x40, pq->ccb, NULL); |
322 TAILQ_REMOVE(&sp->hld, pq, pq_link); |
323 if(pq->buf) { 324 free(pq->buf, M_ISCSIBUF); 325 pq->buf = NULL; 326 } |
327 pdu_free(sp->isc, pq); 328 } 329 while((pq = i_dqueue_snd(sp, BIT(0)|BIT(1)|BIT(2))) != NULL) { 330 sdebug(3, "pq=%p", pq); 331 if(pq->ccb) |
332 _scsi_done(sp, 1, 0x40, pq->ccb, NULL); 333 if(pq->buf) { 334 free(pq->buf, M_ISCSIBUF); 335 pq->buf = NULL; 336 } |
337 pdu_free(sp->isc, pq); 338 } 339 340 wakeup(&sp->rsp); 341} 342 343void 344iscsi_done(isc_session_t *sp, pduq_t *opq, pduq_t *pq) 345{ 346 pdu_t *pp = &pq->pdu; 347 scsi_rsp_t *cmd = &pp->ipdu.scsi_rsp; 348 349 debug_called(8); 350 |
351 _scsi_done(sp, cmd->response, cmd->status, opq->ccb, pq); |
352 353 pdu_free(sp->isc, opq); 354} 355 356// see RFC 3720, 10.9.1 page 146 357/* 358 | NOTE: 359 | the call to isc_stop_receiver is a kludge, --- 39 unchanged lines hidden (view full) --- 399iscsi_reject(isc_session_t *sp, pduq_t *opq, pduq_t *pq) 400{ 401 union ccb *ccb = opq->ccb; 402 //reject_t *reject = &pq->pdu.ipdu.reject; 403 404 debug_called(8); 405 //XXX: check RFC 10.17.1 (page 176) 406 ccb->ccb_h.status = CAM_REQ_ABORTED; |
407 XPT_DONE(sp, ccb); |
408 409 pdu_free(sp->isc, opq); 410} 411 412/* 413 | deal with lun 414 */ 415static int 416dwl(isc_session_t *sp, int lun, u_char *lp) 417{ |
418 debug_called(8); |
419 sdebug(4, "lun=%d", lun); |
420 /* 421 | mapping LUN to iSCSI LUN 422 | check the SAM-2 specs 423 | hint: maxLUNS is a small number, cam's LUN is 32bits 424 | iSCSI is 64bits, scsi is ? 425 */ 426 // XXX: check if this will pass the endian test 427 if(lun < 256) { --- 4 unchanged lines hidden (view full) --- 432 lp[0] = (1 << 5) | ((lun >> 8) & 0x3f); 433 lp[1] = lun & 0xff; 434 } 435 else { 436 xdebug("lun %d: is unsupported!", lun); 437 return -1; 438 } 439 |
440 return 0; 441} 442 443/* 444 | encapsulate the scsi command and 445 */ 446int 447scsi_encap(struct cam_sim *sim, union ccb *ccb) 448{ |
449 isc_session_t *sp = cam_sim_softc(sim); |
450 struct ccb_scsiio *csio = &ccb->csio; 451 struct ccb_hdr *ccb_h = &ccb->ccb_h; 452 pduq_t *pq; 453 scsi_req_t *cmd; 454 455 debug_called(8); 456 457 debug(4, "ccb->sp=%p", ccb_h->spriv_ptr0); 458 sp = ccb_h->spriv_ptr0; 459 |
460 if((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) { |
461 debug(2, "ccb->sp=%p", ccb_h->spriv_ptr0); 462 sdebug(1, "pdu_alloc failed sc->npdu_max=%d npdu_alloc=%d", 463 sp->isc->npdu_max, sp->isc->npdu_alloc); 464 while((pq = pdu_alloc(sp->isc, M_NOWAIT)) == NULL) { |
465 sdebug(2, "waiting..."); |
466#if __FreeBSD_version >= 700000 467 pause("isc_encap", 5*hz); 468#else 469 tsleep(sp->isc, 0, "isc_encap", 5*hz); 470#endif 471 } |
472 } |
473 cmd = &pq->pdu.ipdu.scsi_req; 474 cmd->opcode = ISCSI_SCSI_CMD; 475 cmd->F = 1; 476 /* 477 | map tag option, default is UNTAGGED 478 */ 479 switch(csio->tag_action) { 480 case MSG_SIMPLE_Q_TAG: cmd->attr = iSCSI_TASK_SIMPLE; break; |
481 case MSG_HEAD_OF_Q_TAG: cmd->attr = iSCSI_TASK_HOFQ; break; 482 case MSG_ORDERED_Q_TAG: cmd->attr = iSCSI_TASK_ORDER; break; |
483 case MSG_ACA_TASK: cmd->attr = iSCSI_TASK_ACA; break; 484 } 485 486 dwl(sp, ccb_h->target_lun, (u_char *)&cmd->lun); 487 488 if((ccb_h->flags & CAM_CDB_POINTER) != 0) { 489 if((ccb_h->flags & CAM_CDB_PHYS) == 0) { 490 if(csio->cdb_len > 16) { --- 21 unchanged lines hidden (view full) --- 512 pq->ccb = ccb; 513 /* 514 | place it in the out queue 515 */ 516 if(isc_qout(sp, pq) == 0) 517 return 1; 518 invalid: 519 ccb->ccb_h.status = CAM_REQ_INVALID; |
520 pdu_free(sp->isc, pq); 521 |
522 return 0; 523} 524 525int 526scsi_decap(isc_session_t *sp, pduq_t *opq, pduq_t *pq) 527{ 528 union ccb *ccb = opq->ccb; 529 struct ccb_scsiio *csio = &ccb->csio; --- 24 unchanged lines hidden (view full) --- 554 caddr_t bp = NULL; // = mtod(pq->mp, caddr_t); 555 data_in_t *rcmd = &pq->pdu.ipdu.data_in; 556 557 if(cmd->R) { 558 sdebug(5, "copy to=%p from=%p l1=%d l2=%d mp@%p", 559 csio->data_ptr, bp? mtod(pq->mp, caddr_t): 0, 560 ntohl(cmd->edtlen), pq->pdu.ds_len, pq->mp); 561 if(ntohl(cmd->edtlen) >= pq->pdu.ds_len) { |
562 int offset, len = pq->pdu.ds_len; |
563 564 if(pq->mp != NULL) { |
565 caddr_t dp; |
566 |
567 offset = ntohl(rcmd->bo); 568 dp = csio->data_ptr + offset; 569 i_mbufcopy(pq->mp, dp, len); 570 } |
571 } |
572 else { 573 xdebug("edtlen=%d < ds_len=%d", 574 ntohl(cmd->edtlen), pq->pdu.ds_len); 575 } 576 } 577 if(rcmd->S) { 578 /* 579 | contains also the SCSI Status 580 */ |
581 _scsi_done(sp, 0, rcmd->status, opq->ccb, NULL); |
582 return 0; 583 } else 584 return 1; 585 } 586 break; 587 } 588 } 589 default: 590 sdebug(3, "opcode=%02x", bhp->opcode); 591 break; 592 } 593 } 594 /* 595 | XXX: error ... 596 */ 597 return 1; 598} |