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