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