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