]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/iscsi_initiator/isc_cam.c
sysctl(9): Fix a few mandoc related issues
[FreeBSD/FreeBSD.git] / sys / dev / iscsi_initiator / isc_cam.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: isc_cam.c 998 2009-12-20 10:32:45Z danny $
31  */
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_iscsi_initiator.h"
36
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/callout.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/conf.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.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
57 #include <dev/iscsi_initiator/iscsi.h>
58 #include <dev/iscsi_initiator/iscsivar.h>
59
60 static void
61 _inq(struct cam_sim *sim, union ccb *ccb)
62 {
63      struct ccb_pathinq *cpi = &ccb->cpi;
64      isc_session_t *sp = cam_sim_softc(sim);
65
66      debug_called(8);
67      debug(3, "sid=%d target=%d lun=%jx", sp->sid, ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun);
68
69      cpi->version_num = 1; /* XXX??? */
70      cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE | PI_WIDE_32;
71      cpi->target_sprt = 0;
72      cpi->hba_misc = 0;
73      cpi->hba_eng_cnt = 0;
74      cpi->max_target = 0; //ISCSI_MAX_TARGETS - 1;
75      cpi->initiator_id = ISCSI_MAX_TARGETS;
76      cpi->max_lun = sp->opt.maxluns - 1;
77      cpi->bus_id = cam_sim_bus(sim);
78      cpi->base_transfer_speed = 3300; // 40000; // XXX:
79      strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
80      strlcpy(cpi->hba_vid, "iSCSI", HBA_IDLEN);
81      strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
82      cpi->unit_number = cam_sim_unit(sim);
83      cpi->ccb_h.status = CAM_REQ_CMP;
84 #if defined(KNOB_VALID_ADDRESS)
85      cpi->transport = XPORT_ISCSI;
86      cpi->transport_version = 0;
87 #endif
88 }
89
90 static __inline int
91 _scsi_encap(struct cam_sim *sim, union ccb *ccb)
92 {
93      int                ret;
94      isc_session_t      *sp = cam_sim_softc(sim);
95
96      mtx_unlock(&sp->cam_mtx);
97      ret = scsi_encap(sim, ccb);
98      mtx_lock(&sp->cam_mtx);
99
100      return ret;
101 }
102
103 void
104 ic_lost_target(isc_session_t *sp, int target)
105 {
106      debug_called(8);
107      sdebug(2, "lost target=%d", target);
108
109      if(sp->cam_path != NULL) {
110           mtx_lock(&sp->cam_mtx);
111           xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL);
112           xpt_free_path(sp->cam_path);
113           mtx_unlock(&sp->cam_mtx);
114           sp->cam_path = 0; // XXX
115      }
116 }
117
118 static void
119 scan_callback(struct cam_periph *periph, union ccb *ccb)
120 {
121      isc_session_t *sp = (isc_session_t *)ccb->ccb_h.spriv_ptr0;
122
123      debug_called(8);
124
125      xpt_free_ccb(ccb);
126
127      if(sp->flags & ISC_SCANWAIT) {
128           sp->flags &= ~ISC_SCANWAIT;
129           wakeup(sp);
130      }
131 }
132
133 static int
134 ic_scan(isc_session_t *sp)
135 {
136      union ccb  *ccb;
137
138      debug_called(8);
139      sdebug(2, "scanning sid=%d", sp->sid);
140
141      sp->flags &= ~ISC_CAMDEVS;
142      sp->flags |= ISC_SCANWAIT;
143
144      ccb = xpt_alloc_ccb();
145      ccb->ccb_h.path            = sp->cam_path;
146      ccb->ccb_h.cbfcnp          = scan_callback;
147      ccb->ccb_h.spriv_ptr0      = sp;
148
149      xpt_rescan(ccb);
150
151      while(sp->flags & ISC_SCANWAIT)
152           tsleep(sp, PRIBIO, "ffp", 5*hz); // the timeout time should
153                                             // be configurable
154      sdebug(2, "# of luns=%d", sp->target_nluns);
155
156      if(sp->target_nluns > 0) {
157           sp->flags |= ISC_CAMDEVS;
158           return 0;
159      }
160
161      return ENODEV;
162 }
163
164 static void
165 ic_action(struct cam_sim *sim, union ccb *ccb)
166 {
167      isc_session_t      *sp = cam_sim_softc(sim);
168      struct ccb_hdr     *ccb_h = &ccb->ccb_h;
169
170      debug_called(8);
171
172      ccb_h->spriv_ptr0 = sp;
173      sdebug(4, "func_code=0x%x flags=0x%x status=0x%x target=%d lun=%jx retry_count=%d timeout=%d",
174            ccb_h->func_code, ccb->ccb_h.flags, ccb->ccb_h.status,
175            ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun, 
176            ccb->ccb_h.retry_count, ccb_h->timeout);
177      if(sp == NULL) {
178           xdebug("sp == NULL! cannot happen");
179           return;
180      }    
181      switch(ccb_h->func_code) {
182      case XPT_PATH_INQ:
183           _inq(sim, ccb);
184           break;
185
186      case XPT_RESET_BUS: // (can just be a stub that does nothing and completes)
187      {
188           struct ccb_pathinq *cpi = &ccb->cpi;
189
190           debug(3, "XPT_RESET_BUS");
191           cpi->ccb_h.status = CAM_REQ_CMP;
192           break;
193      }
194
195      case XPT_SCSI_IO: 
196      {
197           struct ccb_scsiio* csio = &ccb->csio;
198
199           debug(4, "XPT_SCSI_IO cmd=0x%x", csio->cdb_io.cdb_bytes[0]);
200           if(sp == NULL) {
201                ccb_h->status = CAM_REQ_INVALID; //CAM_NO_NEXUS;
202                debug(4, "xpt_done.status=%d", ccb_h->status);
203                break;
204           }
205           if(ccb_h->target_lun == CAM_LUN_WILDCARD) {
206                debug(3, "target=%d: bad lun (-1)", ccb_h->target_id);
207                ccb_h->status = CAM_LUN_INVALID;
208                break;
209           }
210           if(_scsi_encap(sim, ccb) != 0)
211                return;
212           break;
213      }
214
215      case XPT_CALC_GEOMETRY:
216      {
217           struct        ccb_calc_geometry *ccg;
218
219           ccg = &ccb->ccg;
220           debug(4, "sid=%d target=%d lun=%jx XPT_CALC_GEOMETRY vsize=%jd bsize=%d",
221                 sp->sid, ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun,
222                 ccg->volume_size, ccg->block_size);
223           if(ccg->block_size == 0 ||
224              (ccg->volume_size < ccg->block_size)) {
225                // print error message  ...
226                /* XXX: what error is appropriate? */
227                break;
228           } 
229           else {
230                int      lun, *off, boff;
231
232                lun = ccb->ccb_h.target_lun;
233                if(lun > ISCSI_MAX_LUNS) {
234                     // XXX: 
235                     xdebug("lun %d > ISCSI_MAX_LUNS!\n", lun);
236                     lun %= ISCSI_MAX_LUNS;
237                }
238                off = &sp->target_lun[lun / (sizeof(int)*8)];
239                boff = BIT(lun % (sizeof(int)*8));
240                debug(4, "sp->target_nluns=%d *off=%x boff=%x",
241                      sp->target_nluns, *off, boff);
242
243                if((*off & boff) == 0) {
244                     sp->target_nluns++;
245                     *off |= boff;
246                }
247                cam_calc_geometry(ccg, /*extended*/1);
248           }
249           break;
250      }
251
252      case XPT_GET_TRAN_SETTINGS:
253      default:
254           ccb_h->status = CAM_REQ_INVALID;
255           break;
256      }
257      xpt_done(ccb);
258      return;
259 }
260
261 static void
262 ic_poll(struct cam_sim *sim)
263 {
264      debug_called(4);
265
266 }
267
268 int
269 ic_getCamVals(isc_session_t *sp, iscsi_cam_t *cp)
270 {
271      debug_called(8);
272
273      if(sp && sp->cam_sim) {
274           cp->path_id = cam_sim_path(sp->cam_sim);
275           cp->target_id = 0;
276           cp->target_nluns = ISCSI_MAX_LUNS; // XXX: -1?
277           return 0;
278      }
279      return ENXIO;
280 }
281
282 void
283 ic_destroy(isc_session_t *sp )
284 {
285      debug_called(8);
286
287      if(sp->cam_path != NULL) {
288           sdebug(2, "name=%s unit=%d",
289                  cam_sim_name(sp->cam_sim), cam_sim_unit(sp->cam_sim));
290 #if 0
291           xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL);
292 #else
293           xpt_async(XPT_RESET_BUS, sp->cam_path, NULL);
294 #endif
295           xpt_free_path(sp->cam_path);
296           xpt_bus_deregister(cam_sim_path(sp->cam_sim));
297           cam_sim_free(sp->cam_sim, TRUE /*free_devq*/);
298
299           sdebug(2, "done");
300      }
301 }
302
303 int
304 ic_init(isc_session_t *sp)
305 {
306      struct cam_sim     *sim;
307      struct cam_devq    *devq;
308
309      debug_called(8);
310
311      if((devq = cam_simq_alloc(256)) == NULL)
312           return ENOMEM;
313
314      mtx_init(&sp->cam_mtx, "isc-cam", NULL, MTX_DEF);
315      sim = cam_sim_alloc(ic_action,
316                          ic_poll,
317                          "iscsi",
318                          sp,
319                          sp->sid,       // unit
320                          &sp->cam_mtx,
321                          1,             // max_dev_transactions
322                          0,             // max_tagged_dev_transactions
323                          devq);
324      if(sim == NULL) {
325           cam_simq_free(devq);
326           mtx_destroy(&sp->cam_mtx);
327           return ENXIO;
328      }
329
330      if(xpt_bus_register(sim,
331                          NULL,
332                          0/*bus_number*/) != CAM_SUCCESS) {
333           cam_sim_free(sim, /*free_devq*/TRUE);
334           mtx_destroy(&sp->cam_mtx);
335           return ENXIO;
336      }
337      sp->cam_sim = sim;
338      if(xpt_create_path(&sp->cam_path, NULL, cam_sim_path(sp->cam_sim),
339             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
340           xpt_bus_deregister(cam_sim_path(sp->cam_sim));
341           cam_sim_free(sim, /*free_devq*/TRUE);
342           mtx_destroy(&sp->cam_mtx);
343           return ENXIO;
344      }
345
346      sdebug(1, "cam subsystem initialized");
347
348      ic_scan(sp);
349
350      return 0;
351 }