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