2 * Copyright (c) 2005-2008 Daniel Braniss <danny@cs.huji.ac.il>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 | $Id: iscsivar.h,v 1.30 2007/04/22 10:12:11 danny Exp danny $
31 #ifndef ISCSI_INITIATOR_DEBUG
32 #define ISCSI_INITIATOR_DEBUG 1
35 #ifdef ISCSI_INITIATOR_DEBUG
36 extern int iscsi_debug;
37 #define debug(level, fmt, args...) do {if(level <= iscsi_debug)\
38 printf("%s: " fmt "\n", __func__ , ##args);} while(0)
39 #define sdebug(level, fmt, args...) do {if(level <= iscsi_debug)\
40 printf("%d] %s: " fmt "\n", sp->sid, __func__ , ##args);} while(0)
41 #define debug_called(level) do {if(level <= iscsi_debug)\
42 printf("%s: called\n", __func__);} while(0)
44 #define debug(level, fmt, args...)
45 #define debug_called(level)
46 #define sdebug(level, fmt, args...)
47 #endif /* ISCSI_INITIATOR_DEBUG */
49 #define xdebug(fmt, args...) printf(">>> %s: " fmt "\n", __func__ , ##args)
51 #define MAX_SESSIONS ISCSI_MAX_TARGETS
53 typedef uint32_t digest_t(const void *, int len, uint32_t ocrc);
55 MALLOC_DECLARE(M_ISCSI);
56 MALLOC_DECLARE(M_PDU);
59 #define BIT(n) (1 <<(n))
62 #define ISC_SM_RUN BIT(0)
63 #define ISC_SM_RUNNING BIT(1)
65 #define ISC_LINK_UP BIT(2)
66 #define ISC_CON_RUN BIT(3)
67 #define ISC_CON_RUNNING BIT(4)
68 #define ISC_KILL BIT(5)
69 #define ISC_OQNOTEMPTY BIT(6)
70 #define ISC_OWAITING BIT(7)
71 #define ISC_FFPHASE BIT(8)
72 #define ISC_FFPWAIT BIT(9)
74 #define ISC_MEMWAIT BIT(10)
75 #define ISC_SIGNALED BIT(11)
76 #define ISC_FROZEN BIT(12)
77 #define ISC_STALLED BIT(13)
79 #define ISC_HOLD BIT(14)
80 #define ISC_HOLDED BIT(15)
82 #define ISC_SHUTDOWN BIT(31)
88 int npdu; // number of pdus malloc'ed.
89 int nrecv; // unprocessed received pdus
90 int nsent; // sent pdus
99 struct bintime t_sent;
100 struct bintime t_recv;
107 typedef TAILQ_HEAD(, pduq) queue_t;
109 typedef struct isc_session {
110 TAILQ_ENTRY(isc_session) sp_link;
117 struct proc *proc; // the userland process
120 struct proc *soc_proc;
122 struct proc *stp; // the sm thread
124 struct isc_softc *isc;
126 digest_t *hdrDigest; // the digest alg. if any
127 digest_t *dataDigest; // the digest alg. if any
129 int sid; // Session ID
131 // int cid; // Connection ID
132 // int tsih; // target session identifier handle
133 sn_t sn; // sequence number stuff;
134 int cws; // current window size
136 int target_nluns; // this and target_lun are
137 // hopefully temporal till I
138 // figure out a better way.
139 lun_id_t target_lun[ISCSI_MAX_LUNS];
158 struct i_stats stats;
159 struct cam_path *cam_path;
166 struct sysctl_ctx_list clist;
167 struct sysctl_oid *oid;
168 int douio; //XXX: turn on/off uio on read
171 typedef struct pduq {
172 TAILQ_ENTRY(pduq) pq_link;
175 u_int len; // the total length of the pdu
180 struct iovec iov[5]; // XXX: careful ...
190 char isid[6]; // Initiator Session ID (48 bits)
194 TAILQ_HEAD(,isc_session) isc_sess;
195 isc_session_t *sessions[MAX_SESSIONS];
198 #ifdef ISCSI_INITIATOR_DEBUG
199 int npdu_alloc, npdu_max; // for instrumentation
201 #define MAX_PDUS (MAX_SESSIONS*256) // XXX: at the moment this is arbitrary
202 uma_zone_t pdu_zone; // pool of free pdu's
203 TAILQ_HEAD(,pduq) freepdu;
207 struct cam_sim *cam_sim;
208 struct cam_path *cam_path;
213 struct sysctl_ctx_list clist;
214 struct sysctl_oid *oid;
217 #ifdef ISCSI_INITIATOR_DEBUG
218 extern struct mtx iscsi_dbg_mtx;
221 void isc_start_receiver(isc_session_t *sp);
222 void isc_stop_receiver(isc_session_t *sp);
224 int isc_sendPDU(isc_session_t *sp, pduq_t *pq);
225 int isc_qout(isc_session_t *sp, pduq_t *pq);
226 int i_prepPDU(isc_session_t *sp, pduq_t *pq);
228 int ism_fullfeature(struct cdev *dev, int flag);
230 int i_pdu_flush(isc_session_t *sc);
231 int i_setopt(isc_session_t *sp, isc_opt_t *opt);
232 void i_freeopt(isc_opt_t *opt);
234 int ic_init(struct isc_softc *sc);
235 void ic_destroy(struct isc_softc *sc);
236 int ic_fullfeature(struct cdev *dev);
237 void ic_lost_target(isc_session_t *sp, int target);
238 int ic_getCamVals(isc_session_t *sp, iscsi_cam_t *cp);
240 void ism_recv(isc_session_t *sp, pduq_t *pq);
241 int ism_start(isc_session_t *sp);
242 void ism_stop(isc_session_t *sp);
244 int scsi_encap(struct cam_sim *sim, union ccb *ccb);
245 int scsi_decap(isc_session_t *sp, pduq_t *opq, pduq_t *pq);
246 void iscsi_r2t(isc_session_t *sp, pduq_t *opq, pduq_t *pq);
247 void iscsi_done(isc_session_t *sp, pduq_t *opq, pduq_t *pq);
248 void iscsi_reject(isc_session_t *sp, pduq_t *opq, pduq_t *pq);
249 void iscsi_async(isc_session_t *sp, pduq_t *pq);
250 void iscsi_cleanup(isc_session_t *sp);
251 int iscsi_requeue(isc_session_t *sp);
253 void ic_freeze(isc_session_t *sp);
254 void ic_release(isc_session_t *sp);
256 // Serial Number Arithmetic
257 #define _MAXINCR 0x7FFFFFFF // 2 ^ 31 - 1
258 #define SNA_GT(i1, i2) ((i1 != i2) && (\
259 (i1 < i2 && i2 - i1 > _MAXINCR) ||\
260 (i1 > i2 && i1 - i2 < _MAXINCR))?1: 0)
265 #ifdef _CAM_CAM_XPT_SIM_H
267 #if __FreeBSD_version < 600000
268 #define CAM_LOCK(arg)
269 #define CAM_ULOCK(arg)
272 XPT_DONE(struct isc_softc *isp, union ccb *ccb)
278 #elif __FreeBSD_version >= 700000
279 #define CAM_LOCK(arg) mtx_lock(&arg->cam_mtx)
280 #define CAM_UNLOCK(arg) mtx_unlock(&arg->cam_mtx)
283 XPT_DONE(struct isc_softc *isp, union ccb *ccb)
290 //__FreeBSD_version >= 600000
291 #define CAM_LOCK(arg)
292 #define CAM_UNLOCK(arg)
293 #define XPT_DONE(ignore, arg) xpt_done(arg)
296 #endif /* _CAM_CAM_XPT_SIM_H */
298 static __inline pduq_t *
299 pdu_alloc(struct isc_softc *isc, int wait)
303 mtx_lock(&isc->pdu_mtx);
304 if((pq = TAILQ_FIRST(&isc->freepdu)) == NULL) {
305 mtx_unlock(&isc->pdu_mtx);
306 pq = (pduq_t *)uma_zalloc(isc->pdu_zone, wait /* M_WAITOK or M_NOWAIT*/);
309 TAILQ_REMOVE(&isc->freepdu, pq, pq_link);
310 mtx_unlock(&isc->pdu_mtx);
313 debug(7, "out of mem");
316 #ifdef ISCSI_INITIATOR_DEBUG
317 mtx_lock(&isc->pdu_mtx);
319 if(isc->npdu_alloc > isc->npdu_max)
320 isc->npdu_max = isc->npdu_alloc;
321 mtx_unlock(&isc->pdu_mtx);
323 memset(pq, 0, sizeof(pduq_t));
329 pdu_free(struct isc_softc *isc, pduq_t *pq)
335 free(pq->buf, M_ISCSI);
337 mtx_lock(&isc->pdu_mtx);
338 TAILQ_INSERT_TAIL(&isc->freepdu, pq, pq_link);
339 #ifdef ISCSI_INITIATOR_DEBUG
342 mtx_unlock(&isc->pdu_mtx);
346 i_nqueue_rsp(isc_session_t *sp, pduq_t *pq)
348 mtx_lock(&sp->rsp_mtx);
349 if(++sp->stats.nrsp > sp->stats.max_rsp)
350 sp->stats.max_rsp = sp->stats.nrsp;
351 TAILQ_INSERT_TAIL(&sp->rsp, pq, pq_link);
352 mtx_unlock(&sp->rsp_mtx);
355 static __inline pduq_t *
356 i_dqueue_rsp(isc_session_t *sp)
360 mtx_lock(&sp->rsp_mtx);
361 if((pq = TAILQ_FIRST(&sp->rsp)) != NULL) {
363 TAILQ_REMOVE(&sp->rsp, pq, pq_link);
365 mtx_unlock(&sp->rsp_mtx);
371 i_nqueue_rsv(isc_session_t *sp, pduq_t *pq)
373 mtx_lock(&sp->rsv_mtx);
374 if(++sp->stats.nrsv > sp->stats.max_rsv)
375 sp->stats.max_rsv = sp->stats.nrsv;
376 TAILQ_INSERT_TAIL(&sp->rsv, pq, pq_link);
377 mtx_unlock(&sp->rsv_mtx);
380 static __inline pduq_t *
381 i_dqueue_rsv(isc_session_t *sp)
385 mtx_lock(&sp->rsv_mtx);
386 if((pq = TAILQ_FIRST(&sp->rsv)) != NULL) {
388 TAILQ_REMOVE(&sp->rsv, pq, pq_link);
390 mtx_unlock(&sp->rsv_mtx);
396 i_nqueue_csnd(isc_session_t *sp, pduq_t *pq)
398 mtx_lock(&sp->snd_mtx);
399 if(++sp->stats.ncsnd > sp->stats.max_csnd)
400 sp->stats.max_csnd = sp->stats.ncsnd;
401 TAILQ_INSERT_TAIL(&sp->csnd, pq, pq_link);
402 mtx_unlock(&sp->snd_mtx);
405 static __inline pduq_t *
406 i_dqueue_csnd(isc_session_t *sp)
410 mtx_lock(&sp->snd_mtx);
411 if((pq = TAILQ_FIRST(&sp->csnd)) != NULL) {
413 TAILQ_REMOVE(&sp->csnd, pq, pq_link);
415 mtx_unlock(&sp->snd_mtx);
421 i_nqueue_isnd(isc_session_t *sp, pduq_t *pq)
423 mtx_lock(&sp->snd_mtx);
424 if(++sp->stats.nisnd > sp->stats.max_isnd)
425 sp->stats.max_isnd = sp->stats.nisnd;
426 TAILQ_INSERT_TAIL(&sp->isnd, pq, pq_link);
427 mtx_unlock(&sp->snd_mtx);
430 static __inline pduq_t *
431 i_dqueue_isnd(isc_session_t *sp)
435 mtx_lock(&sp->snd_mtx);
436 if((pq = TAILQ_FIRST(&sp->isnd)) != NULL) {
438 TAILQ_REMOVE(&sp->isnd, pq, pq_link);
440 mtx_unlock(&sp->snd_mtx);
446 i_nqueue_wsnd(isc_session_t *sp, pduq_t *pq)
448 mtx_lock(&sp->snd_mtx);
449 if(++sp->stats.nwsnd > sp->stats.max_wsnd)
450 sp->stats.max_wsnd = sp->stats.nwsnd;
451 TAILQ_INSERT_TAIL(&sp->wsnd, pq, pq_link);
452 mtx_unlock(&sp->snd_mtx);
455 static __inline pduq_t *
456 i_dqueue_wsnd(isc_session_t *sp)
460 mtx_lock(&sp->snd_mtx);
461 if((pq = TAILQ_FIRST(&sp->wsnd)) != NULL) {
463 TAILQ_REMOVE(&sp->wsnd, pq, pq_link);
465 mtx_unlock(&sp->snd_mtx);
470 static __inline pduq_t *
471 i_dqueue_snd(isc_session_t *sp, int which)
476 mtx_lock(&sp->snd_mtx);
477 if((which & BIT(0)) && (pq = TAILQ_FIRST(&sp->isnd)) != NULL) {
479 TAILQ_REMOVE(&sp->isnd, pq, pq_link);
480 pq->pduq = &sp->isnd; // remember where you came from
482 if((which & BIT(1)) && (pq = TAILQ_FIRST(&sp->wsnd)) != NULL) {
484 TAILQ_REMOVE(&sp->wsnd, pq, pq_link);
485 pq->pduq = &sp->wsnd; // remember where you came from
487 if((which & BIT(2)) && (pq = TAILQ_FIRST(&sp->csnd)) != NULL) {
489 TAILQ_REMOVE(&sp->csnd, pq, pq_link);
490 pq->pduq = &sp->csnd; // remember where you came from
492 mtx_unlock(&sp->snd_mtx);
498 i_rqueue_pdu(isc_session_t *sp, pduq_t *pq)
500 mtx_lock(&sp->snd_mtx);
501 KASSERT(pq->pduq != NULL, ("pq->pduq is NULL"));
502 TAILQ_INSERT_TAIL(pq->pduq, pq, pq_link);
503 mtx_unlock(&sp->snd_mtx);
507 | Waiting for ACK (or something :-)
510 i_nqueue_hld(isc_session_t *sp, pduq_t *pq)
513 mtx_lock(&sp->hld_mtx);
514 if(++sp->stats.nhld > sp->stats.max_hld)
515 sp->stats.max_hld = sp->stats.nhld;
516 TAILQ_INSERT_TAIL(&sp->hld, pq, pq_link);
517 mtx_unlock(&sp->hld_mtx);
522 i_remove_hld(isc_session_t *sp, pduq_t *pq)
524 mtx_lock(&sp->hld_mtx);
526 TAILQ_REMOVE(&sp->hld, pq, pq_link);
527 mtx_unlock(&sp->hld_mtx);
530 static __inline pduq_t *
531 i_dqueue_hld(isc_session_t *sp)
535 mtx_lock(&sp->hld_mtx);
536 if((pq = TAILQ_FIRST(&sp->hld)) != NULL) {
538 TAILQ_REMOVE(&sp->hld, pq, pq_link);
540 mtx_unlock(&sp->hld_mtx);
545 static __inline pduq_t *
546 i_search_hld(isc_session_t *sp, int itt, int keep)
552 mtx_lock(&sp->hld_mtx);
553 TAILQ_FOREACH_SAFE(pq, &sp->hld, pq_link, tmp) {
554 if(pq->pdu.ipdu.bhs.itt == itt) {
557 TAILQ_REMOVE(&sp->hld, pq, pq_link);
562 mtx_unlock(&sp->hld_mtx);
568 i_mbufcopy(struct mbuf *mp, caddr_t dp, int len)
573 for(m = mp; m != NULL; m = m->m_next) {
574 bp = mtod(m, caddr_t);
576 | the pdu is word (4 octed) aligned
579 memcpy(dp, bp, MIN(len, m->m_len));