]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/iscsi/initiator/iscsi_subr.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / iscsi / initiator / iscsi_subr.c
1 /*-
2  * Copyright (c) 2005-2007 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  */
27 /*
28  | $Id: iscsi_subr.c,v 1.17 2006/11/26 14:50:43 danny Exp danny $
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
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
47 #include <cam/cam.h>
48 #include <cam/cam_ccb.h>
49 #include <cam/cam_sim.h>
50 #include <cam/cam_xpt_sim.h>
51 #include <cam/cam_periph.h>
52 #include <cam/scsi/scsi_message.h>
53 #include <sys/eventhandler.h>
54
55 #include <dev/iscsi/initiator/iscsi.h>
56 #include <dev/iscsi/initiator/iscsivar.h>
57
58 /*
59  | Interface to the SCSI layer
60  */
61 void
62 iscsi_r2t(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
63 {
64      union ccb          *ccb = opq->ccb;
65      struct ccb_scsiio  *csio = &ccb->csio;
66      pdu_t              *opp = &opq->pdu;
67      bhs_t              *bhp = &opp->ipdu.bhs;
68      r2t_t              *r2t = &pq->pdu.ipdu.r2t;
69      pduq_t     *wpq;
70      int        error;
71
72      debug_called(8);
73      sdebug(4, "itt=%x r2tSN=%d bo=%x ddtl=%x W=%d", ntohl(r2t->itt),
74            ntohl(r2t->r2tSN), ntohl(r2t->bo), ntohl(r2t->ddtl), opp->ipdu.scsi_req.W);
75
76      switch(bhp->opcode) {
77      case ISCSI_SCSI_CMD:
78           if(opp->ipdu.scsi_req.W) {
79                data_out_t       *cmd;
80                u_int            ddtl = ntohl(r2t->ddtl);
81                u_int            edtl = ntohl(opp->ipdu.scsi_req.edtlen);
82                u_int            bleft, bs, dsn, bo;
83                caddr_t          bp = csio->data_ptr;
84
85                bo = ntohl(r2t->bo);
86                bleft = ddtl;
87
88                if(sp->opt.maxXmitDataSegmentLength > 0) // danny's RFC
89                     bs = MIN(sp->opt.maxXmitDataSegmentLength, ddtl);
90                else
91                     bs = ddtl;
92                dsn = 0;
93                sdebug(4, "edtl=%x ddtl=%x bo=%x dsn=%x bs=%x maxX=%x",
94                       edtl, ddtl, bo, dsn, bs, sp->opt.maxXmitDataSegmentLength);
95                while(bleft > 0) {
96                     wpq = pdu_alloc(sp->isc, 1);
97                     if(wpq == NULL) {
98                          // should not happen if above is 1
99                          sdebug(1, "now what?");
100                          return;
101                     }
102                     cmd = &wpq->pdu.ipdu.data_out;
103                     cmd->opcode = ISCSI_WRITE_DATA;
104                     cmd->lun[0] = r2t->lun[0];
105                     cmd->lun[1] = r2t->lun[1];
106                     cmd->ttt    = r2t->ttt;
107                     cmd->itt    = r2t->itt;
108
109                     cmd->dsn    = htonl(dsn);
110                     cmd->bo     = htonl(bo);
111
112                     cmd->F      = (bs < bleft)? 0: 1; // is this the last one?
113                     bs = MIN(bs, bleft);
114                     
115                     wpq->pdu.ds_len     = bs;
116                     wpq->pdu.ds         = bp;
117                     
118                     error = isc_qout(sp, wpq);
119                     sdebug(6, "bs=%x bo=%x bp=%p dsn=%x error=%d", bs, bo, bp, dsn, error);
120                     if(error)
121                          break;
122                     bo += bs;
123                     bp += bs;
124                     bleft -= bs;
125                     dsn++;
126                }
127           }
128           break;
129
130      default:
131           // XXX: should not happen ...
132           xdebug("huh? opcode=0x%x", bhp->opcode);
133      }
134 }
135
136 static int
137 getSenseData(u_int status, union ccb *ccb, pduq_t *pq)
138 {
139      pdu_t              *pp = &pq->pdu;
140      struct             ccb_scsiio *scsi = (struct ccb_scsiio *)ccb;
141      struct             scsi_sense_data *sense = &scsi->sense_data;
142      struct mbuf        *m = pq->mp;
143      scsi_rsp_t         *cmd = &pp->ipdu.scsi_rsp;
144      caddr_t            bp;
145      int                sense_len, mustfree = 0;
146
147      bp = mtod(pq->mp, caddr_t);
148      if((sense_len = scsi_2btoul(bp)) == 0)
149           return 0;
150      debug(4, "sense_len=%d", sense_len);
151      /*
152       | according to the specs, the sense data cannot
153       | be larger than 252 ...
154       */
155      if(sense_len > m->m_len) {
156           bp = malloc(sense_len, M_ISCSI, M_WAITOK);
157           debug(3, "calling i_mbufcopy(len=%d)", sense_len);
158           i_mbufcopy(pq->mp, bp, sense_len);
159           mustfree++;
160      }
161      scsi->scsi_status = status;
162
163      bcopy(bp+2, sense, min(sense_len, scsi->sense_len));
164      scsi->sense_resid = 0;
165      if(cmd->flag & (BIT(1)|BIT(2)))
166           scsi->sense_resid = ntohl(pp->ipdu.scsi_rsp.rcnt);
167      debug(3, "sense_len=%d rcnt=%d sense_resid=%d dsl=%d error_code=%x flags=%x",
168            sense_len,
169            ntohl(pp->ipdu.scsi_rsp.rcnt), scsi->sense_resid,
170            pp->ds_len, sense->error_code, sense->flags);
171
172      if(mustfree)
173           free(bp, M_ISCSI);
174
175      return 1;
176 }
177
178 /*
179  | Some information is from SAM draft.
180  */
181 static void
182 _scsi_done(struct isc_softc *isp, u_int response, u_int status, union ccb *ccb, pduq_t *pq)
183 {
184      struct ccb_hdr     *ccb_h = &ccb->ccb_h;
185
186      debug_called(8);
187
188      if(status || response) {
189           debug(3, "response=%x status=%x ccb=%p pq=%p", response, status, ccb, pq);
190           if(pq != NULL)
191                debug(3, "mp=%p buf=%p len=%d", pq->mp, pq->buf, pq->len);
192      }
193      ccb_h->status = 0;
194      switch(response) {
195      case 0: // Command Completed at Target
196           switch(status) {
197           case 0:       // Good, all is ok
198                ccb_h->status = CAM_REQ_CMP;
199                break;
200                
201           case 0x02:    // Check Condition
202                if((pq != NULL) && (pq->mp != NULL) && getSenseData(status, ccb, pq))
203                     ccb_h->status |= CAM_AUTOSNS_VALID;
204
205           case 0x14:    // Intermediate-Condition Met
206           case 0x10:    // Intermediate
207           case 0x04:    // Condition Met
208                ccb_h->status |= CAM_SCSI_STATUS_ERROR;
209                break;
210
211           case 0x08:
212                ccb_h->status = CAM_BUSY;
213                break;
214
215           case 0x18: // Reservation Conflict
216           case 0x28: // Task Set Full
217                ccb_h->status = CAM_REQUEUE_REQ;
218                break;
219           default:
220                //case 0x22: // Command Terminated
221                //case 0x30: // ACA Active
222                //case 0x40: // Task Aborted
223                ccb_h->status = CAM_REQ_ABORTED;
224           }
225           break;
226
227      default:
228           if((response >= 0x80) && (response <= 0xFF)) {
229                // Vendor specific ...
230           }
231      case 1: // target failure
232           ccb_h->status = CAM_REQ_CMP_ERR; //CAM_REQ_ABORTED;
233           break;
234      }
235      debug(5, "ccb_h->status=%x", ccb_h->status);
236
237      XPT_DONE(isp, ccb);
238 }
239
240 /*
241  | returns the lowest cmdseq that was not acked
242  */
243 int
244 iscsi_requeue(isc_session_t *sp)
245 {
246      pduq_t     *pq;
247      u_int      i, n, last;
248
249      debug_called(8);
250      last = -1;
251      i = 0;
252      while((pq = i_dqueue_hld(sp)) != NULL) {
253           i++;
254           _scsi_done(sp->isc, 0, 0x28, pq->ccb, NULL);
255           n = ntohl(pq->pdu.ipdu.bhs.CmdSN);
256           if(last > n)
257                last = n;
258           sdebug(2, "last=%x n=%x", last, n);
259           pdu_free(sp->isc, pq);
260      }
261      return i? last: sp->sn.cmd;
262 }
263
264 int
265 i_pdu_flush(isc_session_t *sp)
266 {
267      int        n = 0;
268      pduq_t     *pq;
269
270      debug_called(8);
271      while((pq = i_dqueue_rsp(sp)) != NULL) {
272           pdu_free(sp->isc, pq);
273           n++;
274      }
275      while((pq = i_dqueue_rsv(sp)) != NULL) {
276           pdu_free(sp->isc, pq);
277           n++;
278      }
279      while((pq = i_dqueue_snd(sp, -1)) != NULL) {
280           pdu_free(sp->isc, pq);
281           n++;
282      }
283      while((pq = i_dqueue_hld(sp)) != NULL) {
284           pdu_free(sp->isc, pq);
285           n++;
286      }
287      if(n != 0)
288           xdebug("%d pdus recovered, should have been ZERO!", n);
289      return n;
290 }
291 /*
292  | called from ism_destroy.
293  */
294 void
295 iscsi_cleanup(isc_session_t *sp)
296 {
297      pduq_t *pq, *pqtmp;
298
299      debug_called(8);
300
301      TAILQ_FOREACH_SAFE(pq, &sp->hld, pq_link, pqtmp) {
302           sdebug(3, "hld pq=%p", pq);
303           if(pq->ccb)
304                _scsi_done(sp->isc, 1, 0x40, pq->ccb, NULL);
305           TAILQ_REMOVE(&sp->hld, pq, pq_link);
306           pdu_free(sp->isc, pq);
307      }
308      while((pq = i_dqueue_snd(sp, BIT(0)|BIT(1)|BIT(2))) != NULL) {
309           sdebug(3, "pq=%p", pq);
310           if(pq->ccb)
311                _scsi_done(sp->isc, 1, 0x40, pq->ccb, NULL);
312           pdu_free(sp->isc, pq);
313      }
314
315      wakeup(&sp->rsp);
316 }
317
318 void
319 iscsi_done(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
320 {
321      pdu_t              *pp = &pq->pdu;
322      scsi_rsp_t         *cmd = &pp->ipdu.scsi_rsp;
323
324      debug_called(8);
325
326      _scsi_done(sp->isc, cmd->response, cmd->status, opq->ccb, pq);
327
328      pdu_free(sp->isc, opq);
329 }
330
331 // see RFC 3720, 10.9.1 page 146
332 void
333 iscsi_async(isc_session_t *sp, pduq_t *pq)
334 {
335      pdu_t              *pp = &pq->pdu;
336      async_t            *cmd = &pp->ipdu.async;
337
338      debug_called(8);
339
340      sdebug(3, "asyncevent=0x%x asyncVCode=0x%0x", cmd->asyncEvent, cmd->asyncVCode);
341      switch(cmd->asyncEvent) {
342      case 0: // check status ...
343           break;
344      case 1: // target request logout
345           break;
346      case 2: // target indicates it wants to drop connection
347           break;
348
349      case 3: // target indicates it will drop all connections.
350           isc_stop_receiver(sp);
351           break;
352
353      case 4: // target request parameter negotiation
354           break;
355      default:
356           break;
357      }
358 }
359
360 void
361 iscsi_reject(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
362 {
363      union ccb          *ccb = opq->ccb;
364      //reject_t         *reject = &pq->pdu.ipdu.reject;
365
366      debug_called(8);
367      //XXX: check RFC 10.17.1 (page 176)
368      ccb->ccb_h.status = CAM_REQ_ABORTED;
369      XPT_DONE(sp->isc, ccb);
370  
371      pdu_free(sp->isc, opq);
372 }
373
374 /*
375  | deal with lun
376  */
377 static int
378 dwl(isc_session_t *sp, int lun, u_char *lp)
379 {
380      int        i;
381
382      debug_called(8);
383
384      /*
385       | mapping LUN to iSCSI LUN
386       | check the SAM-2 specs
387       | hint: maxLUNS is a small number, cam's LUN is 32bits
388       | iSCSI is 64bits, scsi is ?
389       */
390      // XXX: check if this will pass the endian test
391      if(lun < 256) {
392           lp[0] = 0;
393           lp[1] = lun;
394      } else
395      if(lun < 16384) {
396           lp[0] = (1 << 5) | ((lun >> 8) & 0x3f);
397           lp[1] = lun & 0xff;
398      } 
399      else {
400           xdebug("lun %d: is unsupported!", lun);
401           return -1;
402      }
403
404      for(i = 0; i < sp->target_nluns; i++)
405           if(sp->target_lun[i] == lun)
406                return 0;
407      if(sp->target_nluns < ISCSI_MAX_LUNS)
408           sp->target_lun[sp->target_nluns++] = lun;
409
410      sdebug(3, "nluns=%d lun=%d", sp->target_nluns, lun);
411
412      return 0;
413 }
414
415 /*
416  | encapsulate the scsi command and 
417  */
418 int
419 scsi_encap(struct cam_sim *sim, union ccb *ccb)
420 {
421      struct isc_softc   *isp = (struct isc_softc *)cam_sim_softc(sim);
422      isc_session_t      *sp;
423      struct ccb_scsiio  *csio = &ccb->csio;
424      struct ccb_hdr     *ccb_h = &ccb->ccb_h;
425      pduq_t             *pq;
426      scsi_req_t         *cmd;
427
428      debug_called(8);
429
430      debug(4, "ccb->sp=%p", ccb_h->spriv_ptr0);
431      sp = ccb_h->spriv_ptr0;
432
433      if((pq = pdu_alloc(isp, 1)) == NULL) { // cannot happen
434           sdebug(3, "freezing");
435           ccb->ccb_h.status = CAM_REQUEUE_REQ;
436           ic_freeze(sp);
437           return 0;
438      }
439 #if 0
440      if((sp->flags & ISC_FFPHASE) == 0) {
441           ccb->ccb_h.status = CAM_DEV_NOT_THERE; // CAM_NO_NEXUS;
442           sdebug(3, "no active session with target %d", ccb_h->target_id);
443           goto bad;
444      }
445 #endif
446      cmd = &pq->pdu.ipdu.scsi_req;
447      cmd->opcode = ISCSI_SCSI_CMD;
448      cmd->F = 1;
449      /*
450       | map tag option, default is UNTAGGED
451       */
452      switch(csio->tag_action) {
453      case MSG_SIMPLE_Q_TAG:     cmd->attr = iSCSI_TASK_SIMPLE;  break;
454      case MSG_HEAD_OF_Q_TAG:    cmd->attr = iSCSI_TASK_ORDER;   break;
455      case MSG_ORDERED_Q_TAG:    cmd->attr = iSCSI_TASK_HOFQ;    break;
456      case MSG_ACA_TASK:         cmd->attr = iSCSI_TASK_ACA;     break;
457      }
458
459      dwl(sp, ccb_h->target_lun, (u_char *)&cmd->lun);
460
461      if((ccb_h->flags & CAM_CDB_POINTER) != 0) {
462           if((ccb_h->flags & CAM_CDB_PHYS) == 0) {
463                if(csio->cdb_len > 16) {
464                     sdebug(3, "oversize cdb %d > 16", csio->cdb_len);
465                     goto invalid;
466                }
467           }
468           else {
469                sdebug(3, "not phys");
470                goto invalid;
471           }
472      }
473
474      if(csio->cdb_len > sizeof(cmd->cdb))
475           xdebug("guevalt! %d > %ld", csio->cdb_len, (long)sizeof(cmd->cdb));
476
477      memcpy(cmd->cdb,
478             ccb_h->flags & CAM_CDB_POINTER? csio->cdb_io.cdb_ptr: csio->cdb_io.cdb_bytes,
479             csio->cdb_len);
480
481      cmd->W = (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT;
482      cmd->R = (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN;
483      cmd->edtlen = htonl(csio->dxfer_len);
484
485      pq->ccb = ccb;
486      /*
487       | place it in the out queue
488       */
489      if(isc_qout(sp, pq) == 0)
490           return 1; 
491  invalid:
492      ccb->ccb_h.status = CAM_REQ_INVALID;
493      pdu_free(isp, pq);
494      return 0;
495 }
496
497 int
498 scsi_decap(isc_session_t *sp, pduq_t *opq, pduq_t *pq)
499 {
500      union ccb          *ccb = opq->ccb;
501      struct ccb_scsiio  *csio = &ccb->csio;
502      pdu_t              *opp = &opq->pdu;
503      bhs_t              *bhp = &opp->ipdu.bhs;
504      
505      debug_called(8);
506      sdebug(6, "pq=%p opq=%p bhp->opcode=0x%x len=%d",
507             pq, opq, bhp->opcode, pq->pdu.ds_len);
508      if(ccb == NULL) {
509           sdebug(1, "itt=0x%x pq=%p opq=%p bhp->opcode=0x%x len=%d",
510                  ntohl(pq->pdu.ipdu.bhs.itt),
511                  pq, opq, bhp->opcode, pq->pdu.ds_len);
512           xdebug("%d] ccb == NULL!", sp->sid);
513           return 0;
514      }
515      if(pq->pdu.ds_len != 0) {
516           switch(bhp->opcode) {
517           case ISCSI_SCSI_CMD: {
518                scsi_req_t *cmd = &opp->ipdu.scsi_req;
519                sdebug(5, "itt=0x%x opcode=%x R=%d",
520                       ntohl(pq->pdu.ipdu.bhs.itt),
521                       pq->pdu.ipdu.bhs.opcode, cmd->R);
522
523                switch(pq->pdu.ipdu.bhs.opcode) {
524                case ISCSI_READ_DATA: // SCSI Data in
525                {
526                     caddr_t     bp = mtod(pq->mp, caddr_t);
527                     data_in_t   *rcmd = &pq->pdu.ipdu.data_in;
528
529                     if(cmd->R) {
530                          sdebug(5, "copy to=%p from=%p l1=%d l2=%d",
531                                 csio->data_ptr, bp,
532                                 ntohl(cmd->edtlen), pq->pdu.ds_len);
533                          if(ntohl(cmd->edtlen) >= pq->pdu.ds_len) {
534                               int               offset, len = pq->pdu.ds_len;
535                               caddr_t           dp;
536
537                               offset = ntohl(rcmd->bo);
538                               dp = csio->data_ptr + offset;
539                               i_mbufcopy(pq->mp, dp, len);
540                          }
541                          else {
542                               xdebug("edtlen=%d < ds_len=%d",
543                                      ntohl(cmd->edtlen), pq->pdu.ds_len);
544                          }
545                     }
546                     if(rcmd->S) {
547                          /*
548                           | contains also the SCSI Status
549                           */
550                          _scsi_done(sp->isc, 0, rcmd->status, opq->ccb, NULL);
551                          return 0;
552                     } else
553                          return 1;
554                }
555                break;
556                }
557           }
558           default:
559                sdebug(3, "opcode=%02x", bhp->opcode);
560                break;
561           }
562      }
563      /*
564       | XXX: error ...
565       */
566      return 1;
567 }