2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2011 Daniel Braniss <danny@cs.huji.ac.il>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 | $Id: iscsivar.h 743 2009-08-08 10:54:53Z danny $
34 #define ISCSI_MAX_LUNS 128 // don't touch this
35 #if ISCSI_MAX_LUNS > 8
38 | sysctl kern.cam.cam_srch_hi=1
42 #ifndef ISCSI_INITIATOR_DEBUG
43 #define ISCSI_INITIATOR_DEBUG 1
46 #ifdef ISCSI_INITIATOR_DEBUG
47 extern int iscsi_debug;
48 #define debug(level, fmt, args...) do {if(level <= iscsi_debug)\
49 printf("%s: " fmt "\n", __func__ , ##args);} while(0)
50 #define sdebug(level, fmt, args...) do {if(level <= iscsi_debug)\
51 printf("%d] %s: " fmt "\n", sp->sid, __func__ , ##args);} while(0)
52 #define debug_called(level) do {if(level <= iscsi_debug)\
53 printf("%s: called\n", __func__);} while(0)
55 #define debug(level, fmt, args...)
56 #define debug_called(level)
57 #define sdebug(level, fmt, args...)
58 #endif /* ISCSI_INITIATOR_DEBUG */
60 #define xdebug(fmt, args...) printf(">>> %s: " fmt "\n", __func__ , ##args)
62 #define MAX_SESSIONS ISCSI_MAX_TARGETS
63 #define MAX_PDUS (MAX_SESSIONS*256) // XXX: at the moment this is arbitrary
65 typedef uint32_t digest_t(const void *, int len, uint32_t ocrc);
67 MALLOC_DECLARE(M_ISCSI);
68 MALLOC_DECLARE(M_ISCSIBUF);
70 #define ISOK2DIG(dig, pp) ((dig != NULL) && ((pp->ipdu.bhs.opcode & 0x1f) != ISCSI_LOGIN_CMD))
73 #define BIT(n) (1 <<(n))
76 #define ISC_SM_RUN BIT(0)
77 #define ISC_SM_RUNNING BIT(1)
79 #define ISC_LINK_UP BIT(2)
80 #define ISC_CON_RUN BIT(3)
81 #define ISC_CON_RUNNING BIT(4)
82 #define ISC_KILL BIT(5)
83 #define ISC_OQNOTEMPTY BIT(6)
84 #define ISC_OWAITING BIT(7)
85 #define ISC_FFPHASE BIT(8)
87 #define ISC_CAMDEVS BIT(9)
88 #define ISC_SCANWAIT BIT(10)
90 #define ISC_MEMWAIT BIT(11)
91 #define ISC_SIGNALED BIT(12)
93 #define ISC_HOLD BIT(15)
94 #define ISC_HOLDED BIT(16)
96 #define ISC_SHUTDOWN BIT(31)
102 int npdu; // number of pdus malloc'ed.
103 int nrecv; // unprocessed received pdus
104 int nsent; // sent pdus
113 struct bintime t_sent;
114 struct bintime t_recv;
121 typedef TAILQ_HEAD(, pduq) queue_t;
123 typedef struct isc_session {
124 TAILQ_ENTRY(isc_session) sp_link;
131 struct proc *proc; // the userland process
133 struct proc *soc_proc;
134 struct proc *stp; // the sm thread
136 struct isc_softc *isc;
138 digest_t *hdrDigest; // the digest alg. if any
139 digest_t *dataDigest; // the digest alg. if any
141 int sid; // Session ID
142 sn_t sn; // sequence number stuff;
143 int cws; // current window size
145 int target_nluns; // this and target_lun are
146 // hopefully temporal till I
147 // figure out a better way.
148 int target_lun[ISCSI_MAX_LUNS/(sizeof(int)*8) + 1];
162 isc_opt_t opt; // negotiable values
164 struct i_stats stats;
171 struct cam_sim *cam_sim;
172 struct cam_path *cam_path;
177 struct sysctl_ctx_list clist;
178 struct sysctl_oid *oid;
179 int douio; //XXX: turn on/off uio on read
182 typedef struct pduq {
183 TAILQ_ENTRY(pduq) pq_link;
186 u_int len; // the total length of the pdu
191 struct iovec iov[5]; // XXX: careful ...
200 TAILQ_HEAD(,isc_session) isc_sess;
203 char isid[6]; // Initiator Session ID (48 bits)
207 uma_zone_t pdu_zone; // pool of free pdu's
208 TAILQ_HEAD(,pduq) freepdu;
210 #ifdef ISCSI_INITIATOR_DEBUG
211 int npdu_alloc, npdu_max; // for instrumentation
213 #ifdef DO_EVENTHANDLER
219 struct sysctl_ctx_list clist;
220 struct sysctl_oid *oid;
223 #ifdef ISCSI_INITIATOR_DEBUG
224 extern struct mtx iscsi_dbg_mtx;
227 void isc_start_receiver(isc_session_t *sp);
228 void isc_stop_receiver(isc_session_t *sp);
230 int isc_sendPDU(isc_session_t *sp, pduq_t *pq);
231 int isc_qout(isc_session_t *sp, pduq_t *pq);
232 int i_prepPDU(isc_session_t *sp, pduq_t *pq);
234 int ism_fullfeature(struct cdev *dev, int flag);
236 int i_pdu_flush(isc_session_t *sc);
237 int i_setopt(isc_session_t *sp, isc_opt_t *opt);
238 void i_freeopt(isc_opt_t *opt);
240 int ic_init(isc_session_t *sp);
241 void ic_destroy(isc_session_t *sp);
242 void ic_lost_target(isc_session_t *sp, int target);
243 int ic_getCamVals(isc_session_t *sp, iscsi_cam_t *cp);
245 void ism_recv(isc_session_t *sp, pduq_t *pq);
246 int ism_start(isc_session_t *sp);
247 void ism_restart(isc_session_t *sp);
248 void ism_stop(isc_session_t *sp);
250 int scsi_encap(struct cam_sim *sim, union ccb *ccb);
251 int scsi_decap(isc_session_t *sp, pduq_t *opq, pduq_t *pq);
252 void iscsi_r2t(isc_session_t *sp, pduq_t *opq, pduq_t *pq);
253 void iscsi_done(isc_session_t *sp, pduq_t *opq, pduq_t *pq);
254 void iscsi_reject(isc_session_t *sp, pduq_t *opq, pduq_t *pq);
255 void iscsi_async(isc_session_t *sp, pduq_t *pq);
256 void iscsi_cleanup(isc_session_t *sp);
257 int iscsi_requeue(isc_session_t *sp);
259 // Serial Number Arithmetic
260 #define _MAXINCR 0x7FFFFFFF // 2 ^ 31 - 1
261 #define SNA_GT(i1, i2) ((i1 != i2) && (\
262 (i1 < i2 && i2 - i1 > _MAXINCR) ||\
263 (i1 > i2 && i1 - i2 < _MAXINCR))?1: 0)
268 #ifdef _CAM_CAM_XPT_SIM_H
270 #if __FreeBSD_version < 600000
271 #define CAM_LOCK(arg)
272 #define CAM_ULOCK(arg)
275 XPT_DONE(isc_session_t *sp, union ccb *ccb)
281 #elif __FreeBSD_version >= 700000
282 #define CAM_LOCK(arg) mtx_lock(&arg->cam_mtx)
283 #define CAM_UNLOCK(arg) mtx_unlock(&arg->cam_mtx)
286 XPT_DONE(isc_session_t *sp, union ccb *ccb)
293 //__FreeBSD_version >= 600000
294 #define CAM_LOCK(arg)
295 #define CAM_UNLOCK(arg)
296 #define XPT_DONE(ignore, arg) xpt_done(arg)
299 #endif /* _CAM_CAM_XPT_SIM_H */
301 static __inline pduq_t *
302 pdu_alloc(struct isc_softc *isc, int wait)
306 pq = (pduq_t *)uma_zalloc(isc->pdu_zone, wait /* M_WAITOK or M_NOWAIT*/);
308 debug(7, "out of mem");
311 #ifdef ISCSI_INITIATOR_DEBUG
312 mtx_lock(&iscsi_dbg_mtx);
314 if(isc->npdu_alloc > isc->npdu_max)
315 isc->npdu_max = isc->npdu_alloc;
316 mtx_unlock(&iscsi_dbg_mtx);
318 memset(pq, 0, sizeof(pduq_t));
324 pdu_free(struct isc_softc *isc, pduq_t *pq)
330 free(pq->buf, M_ISCSIBUF);
332 uma_zfree(isc->pdu_zone, pq);
333 #ifdef ISCSI_INITIATOR_DEBUG
334 mtx_lock(&iscsi_dbg_mtx);
336 mtx_unlock(&iscsi_dbg_mtx);
341 i_nqueue_rsp(isc_session_t *sp, pduq_t *pq)
343 mtx_lock(&sp->rsp_mtx);
344 if(++sp->stats.nrsp > sp->stats.max_rsp)
345 sp->stats.max_rsp = sp->stats.nrsp;
346 TAILQ_INSERT_TAIL(&sp->rsp, pq, pq_link);
347 mtx_unlock(&sp->rsp_mtx);
350 static __inline pduq_t *
351 i_dqueue_rsp(isc_session_t *sp)
355 mtx_lock(&sp->rsp_mtx);
356 if((pq = TAILQ_FIRST(&sp->rsp)) != NULL) {
358 TAILQ_REMOVE(&sp->rsp, pq, pq_link);
360 mtx_unlock(&sp->rsp_mtx);
366 i_nqueue_rsv(isc_session_t *sp, pduq_t *pq)
368 mtx_lock(&sp->rsv_mtx);
369 if(++sp->stats.nrsv > sp->stats.max_rsv)
370 sp->stats.max_rsv = sp->stats.nrsv;
371 TAILQ_INSERT_TAIL(&sp->rsv, pq, pq_link);
372 mtx_unlock(&sp->rsv_mtx);
375 static __inline pduq_t *
376 i_dqueue_rsv(isc_session_t *sp)
380 mtx_lock(&sp->rsv_mtx);
381 if((pq = TAILQ_FIRST(&sp->rsv)) != NULL) {
383 TAILQ_REMOVE(&sp->rsv, pq, pq_link);
385 mtx_unlock(&sp->rsv_mtx);
391 i_nqueue_csnd(isc_session_t *sp, pduq_t *pq)
393 mtx_lock(&sp->snd_mtx);
394 if(++sp->stats.ncsnd > sp->stats.max_csnd)
395 sp->stats.max_csnd = sp->stats.ncsnd;
396 TAILQ_INSERT_TAIL(&sp->csnd, pq, pq_link);
397 mtx_unlock(&sp->snd_mtx);
400 static __inline pduq_t *
401 i_dqueue_csnd(isc_session_t *sp)
405 mtx_lock(&sp->snd_mtx);
406 if((pq = TAILQ_FIRST(&sp->csnd)) != NULL) {
408 TAILQ_REMOVE(&sp->csnd, pq, pq_link);
410 mtx_unlock(&sp->snd_mtx);
416 i_nqueue_isnd(isc_session_t *sp, pduq_t *pq)
418 mtx_lock(&sp->snd_mtx);
419 if(++sp->stats.nisnd > sp->stats.max_isnd)
420 sp->stats.max_isnd = sp->stats.nisnd;
421 TAILQ_INSERT_TAIL(&sp->isnd, pq, pq_link);
422 mtx_unlock(&sp->snd_mtx);
425 static __inline pduq_t *
426 i_dqueue_isnd(isc_session_t *sp)
430 mtx_lock(&sp->snd_mtx);
431 if((pq = TAILQ_FIRST(&sp->isnd)) != NULL) {
433 TAILQ_REMOVE(&sp->isnd, pq, pq_link);
435 mtx_unlock(&sp->snd_mtx);
441 i_nqueue_wsnd(isc_session_t *sp, pduq_t *pq)
443 mtx_lock(&sp->snd_mtx);
444 if(++sp->stats.nwsnd > sp->stats.max_wsnd)
445 sp->stats.max_wsnd = sp->stats.nwsnd;
446 TAILQ_INSERT_TAIL(&sp->wsnd, pq, pq_link);
447 mtx_unlock(&sp->snd_mtx);
450 static __inline pduq_t *
451 i_dqueue_wsnd(isc_session_t *sp)
455 mtx_lock(&sp->snd_mtx);
456 if((pq = TAILQ_FIRST(&sp->wsnd)) != NULL) {
458 TAILQ_REMOVE(&sp->wsnd, pq, pq_link);
460 mtx_unlock(&sp->snd_mtx);
465 static __inline pduq_t *
466 i_dqueue_snd(isc_session_t *sp, int which)
471 mtx_lock(&sp->snd_mtx);
472 if((which & BIT(0)) && (pq = TAILQ_FIRST(&sp->isnd)) != NULL) {
474 TAILQ_REMOVE(&sp->isnd, pq, pq_link);
475 pq->pduq = &sp->isnd; // remember where you came from
477 if((which & BIT(1)) && (pq = TAILQ_FIRST(&sp->wsnd)) != NULL) {
479 TAILQ_REMOVE(&sp->wsnd, pq, pq_link);
480 pq->pduq = &sp->wsnd; // remember where you came from
482 if((which & BIT(2)) && (pq = TAILQ_FIRST(&sp->csnd)) != NULL) {
484 TAILQ_REMOVE(&sp->csnd, pq, pq_link);
485 pq->pduq = &sp->csnd; // remember where you came from
487 mtx_unlock(&sp->snd_mtx);
493 i_rqueue_pdu(isc_session_t *sp, pduq_t *pq)
495 mtx_lock(&sp->snd_mtx);
496 KASSERT(pq->pduq != NULL, ("pq->pduq is NULL"));
497 TAILQ_INSERT_TAIL(pq->pduq, pq, pq_link);
498 mtx_unlock(&sp->snd_mtx);
502 | Waiting for ACK (or something :-)
505 i_nqueue_hld(isc_session_t *sp, pduq_t *pq)
508 mtx_lock(&sp->hld_mtx);
509 if(++sp->stats.nhld > sp->stats.max_hld)
510 sp->stats.max_hld = sp->stats.nhld;
511 TAILQ_INSERT_TAIL(&sp->hld, pq, pq_link);
512 mtx_unlock(&sp->hld_mtx);
517 i_remove_hld(isc_session_t *sp, pduq_t *pq)
519 mtx_lock(&sp->hld_mtx);
521 TAILQ_REMOVE(&sp->hld, pq, pq_link);
522 mtx_unlock(&sp->hld_mtx);
525 static __inline pduq_t *
526 i_dqueue_hld(isc_session_t *sp)
530 mtx_lock(&sp->hld_mtx);
531 if((pq = TAILQ_FIRST(&sp->hld)) != NULL) {
533 TAILQ_REMOVE(&sp->hld, pq, pq_link);
535 mtx_unlock(&sp->hld_mtx);
540 static __inline pduq_t *
541 i_search_hld(isc_session_t *sp, int itt, int keep)
547 mtx_lock(&sp->hld_mtx);
548 TAILQ_FOREACH_SAFE(pq, &sp->hld, pq_link, tmp) {
549 if(pq->pdu.ipdu.bhs.itt == itt) {
552 TAILQ_REMOVE(&sp->hld, pq, pq_link);
557 mtx_unlock(&sp->hld_mtx);
563 i_acked_hld(isc_session_t *sp, pdu_t *op)
566 u_int exp = sp->sn.expCmd;
569 mtx_lock(&sp->hld_mtx);
570 TAILQ_FOREACH_SAFE(pq, &sp->hld, pq_link, tmp) {
571 if((op && op->ipdu.bhs.itt == pq->pdu.ipdu.bhs.itt)
573 && (pq->pdu.ipdu.bhs.opcode != ISCSI_WRITE_DATA)
574 && SNA_GT(exp, ntohl(pq->pdu.ipdu.bhs.ExpStSN)))) {
576 TAILQ_REMOVE(&sp->hld, pq, pq_link);
577 pdu_free(sp->isc, pq);
580 mtx_unlock(&sp->hld_mtx);
584 i_mbufcopy(struct mbuf *mp, caddr_t dp, int len)
589 for(m = mp; m != NULL; m = m->m_next) {
590 bp = mtod(m, caddr_t);
592 | the pdu is word (4 octed) aligned
595 memcpy(dp, bp, MIN(len, m->m_len));