2 * Copyright (c) 2005-2010 Daniel Braniss <danny@cs.huji.ac.il>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 | $Id: isc_cam.c 998 2009-12-20 10:32:45Z danny $
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_iscsi_initiator.h"
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/callout.h>
38 #if __FreeBSD_version >= 700000
40 #include <sys/mutex.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
47 #include <sys/sysctl.h>
51 #include <cam/cam_ccb.h>
52 #include <cam/cam_sim.h>
53 #include <cam/cam_xpt_sim.h>
54 #include <cam/cam_periph.h>
56 #include <dev/iscsi_initiator/iscsi.h>
57 #include <dev/iscsi_initiator/iscsivar.h>
60 _inq(struct cam_sim *sim, union ccb *ccb)
62 struct ccb_pathinq *cpi = &ccb->cpi;
63 isc_session_t *sp = cam_sim_softc(sim);
66 debug(3, "sid=%d target=%d lun=%d", sp->sid, ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
68 cpi->version_num = 1; /* XXX??? */
69 cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE | PI_WIDE_32;
73 cpi->max_target = 0; //ISCSI_MAX_TARGETS - 1;
74 cpi->initiator_id = ISCSI_MAX_TARGETS;
75 cpi->max_lun = sp->opt.maxluns - 1;
76 cpi->bus_id = cam_sim_bus(sim);
77 cpi->base_transfer_speed = 3300; // 40000; // XXX:
78 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
79 strncpy(cpi->hba_vid, "iSCSI", HBA_IDLEN);
80 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
81 cpi->unit_number = cam_sim_unit(sim);
82 cpi->ccb_h.status = CAM_REQ_CMP;
83 #if defined(KNOB_VALID_ADDRESS)
84 cpi->transport = XPORT_ISCSI;
85 cpi->transport_version = 0;
90 _scsi_encap(struct cam_sim *sim, union ccb *ccb)
94 #if __FreeBSD_version < 700000
95 ret = scsi_encap(sim, ccb);
97 isc_session_t *sp = cam_sim_softc(sim);
99 mtx_unlock(&sp->cam_mtx);
100 ret = scsi_encap(sim, ccb);
101 mtx_lock(&sp->cam_mtx);
107 ic_lost_target(isc_session_t *sp, int target)
110 sdebug(2, "lost target=%d", target);
112 if(sp->cam_path != NULL) {
113 mtx_lock(&sp->cam_mtx);
114 xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL);
115 xpt_free_path(sp->cam_path);
116 mtx_unlock(&sp->cam_mtx);
117 sp->cam_path = 0; // XXX
122 scan_callback(struct cam_periph *periph, union ccb *ccb)
124 isc_session_t *sp = (isc_session_t *)ccb->ccb_h.spriv_ptr0;
130 if(sp->flags & ISC_SCANWAIT) {
131 sp->flags &= ~ISC_SCANWAIT;
137 ic_scan(isc_session_t *sp)
142 sdebug(2, "scanning sid=%d", sp->sid);
144 if((ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO)) == NULL) {
145 xdebug("scan failed (can't allocate CCB)");
146 return ENOMEM; // XXX
149 sp->flags &= ~ISC_CAMDEVS;
150 sp->flags |= ISC_SCANWAIT;
153 if(xpt_create_path(&sp->cam_path, NULL, cam_sim_path(sp->cam_sim),
154 0, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
155 xdebug("can't create cam path");
158 return ENODEV; // XXX
160 xpt_setup_ccb(&ccb->ccb_h, sp->cam_path, 5/*priority (low)*/);
161 ccb->ccb_h.func_code = XPT_SCAN_BUS;
162 ccb->ccb_h.cbfcnp = scan_callback;
163 ccb->crcn.flags = CAM_FLAG_NONE;
164 ccb->ccb_h.spriv_ptr0 = sp;
169 while(sp->flags & ISC_SCANWAIT)
170 tsleep(sp, PRIBIO, "ffp", 5*hz); // the timeout time should
172 sdebug(2, "# of luns=%d", sp->target_nluns);
174 if(sp->target_nluns > 0) {
175 sp->flags |= ISC_CAMDEVS;
183 ic_action(struct cam_sim *sim, union ccb *ccb)
185 isc_session_t *sp = cam_sim_softc(sim);
186 struct ccb_hdr *ccb_h = &ccb->ccb_h;
190 ccb_h->spriv_ptr0 = sp;
191 sdebug(4, "func_code=0x%x flags=0x%x status=0x%x target=%d lun=%d retry_count=%d timeout=%d",
192 ccb_h->func_code, ccb->ccb_h.flags, ccb->ccb_h.status,
193 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
194 ccb->ccb_h.retry_count, ccb_h->timeout);
196 xdebug("sp == NULL! cannot happen");
199 switch(ccb_h->func_code) {
204 case XPT_RESET_BUS: // (can just be a stub that does nothing and completes)
206 struct ccb_pathinq *cpi = &ccb->cpi;
208 debug(3, "XPT_RESET_BUS");
209 cpi->ccb_h.status = CAM_REQ_CMP;
215 struct ccb_scsiio* csio = &ccb->csio;
217 debug(4, "XPT_SCSI_IO cmd=0x%x", csio->cdb_io.cdb_bytes[0]);
219 ccb_h->status = CAM_REQ_INVALID; //CAM_NO_NEXUS;
220 debug(4, "xpt_done.status=%d", ccb_h->status);
223 if(ccb_h->target_lun == CAM_LUN_WILDCARD) {
224 debug(3, "target=%d: bad lun (-1)", ccb_h->target_id);
225 ccb_h->status = CAM_LUN_INVALID;
228 if(_scsi_encap(sim, ccb) != 0)
233 case XPT_CALC_GEOMETRY:
235 struct ccb_calc_geometry *ccg;
238 debug(4, "sid=%d target=%d lun=%d XPT_CALC_GEOMETRY vsize=%jd bsize=%d",
239 sp->sid, ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
240 ccg->volume_size, ccg->block_size);
241 if(ccg->block_size == 0 ||
242 (ccg->volume_size < ccg->block_size)) {
243 // print error message ...
244 /* XXX: what error is appropiate? */
250 lun = ccb->ccb_h.target_lun;
251 if(lun > ISCSI_MAX_LUNS) {
253 xdebug("lun %d > ISCSI_MAX_LUNS!\n", lun);
254 lun %= ISCSI_MAX_LUNS;
256 off = &sp->target_lun[lun / (sizeof(int)*8)];
257 boff = BIT(lun % (sizeof(int)*8));
258 debug(4, "sp->target_nluns=%d *off=%x boff=%x",
259 sp->target_nluns, *off, boff);
261 if((*off & boff) == 0) {
265 cam_calc_geometry(ccg, /*extended*/1);
270 case XPT_GET_TRAN_SETTINGS:
272 ccb_h->status = CAM_REQ_INVALID;
275 #if __FreeBSD_version < 700000
284 ic_poll(struct cam_sim *sim)
291 ic_getCamVals(isc_session_t *sp, iscsi_cam_t *cp)
295 if(sp && sp->cam_sim) {
296 cp->path_id = cam_sim_path(sp->cam_sim);
298 cp->target_nluns = ISCSI_MAX_LUNS; // XXX: -1?
305 ic_destroy(isc_session_t *sp )
309 if(sp->cam_path != NULL) {
310 sdebug(2, "name=%s unit=%d",
311 cam_sim_name(sp->cam_sim), cam_sim_unit(sp->cam_sim));
314 xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL);
316 xpt_async(XPT_RESET_BUS, sp->cam_path, NULL);
318 xpt_free_path(sp->cam_path);
319 xpt_bus_deregister(cam_sim_path(sp->cam_sim));
320 cam_sim_free(sp->cam_sim, TRUE /*free_devq*/);
328 ic_init(isc_session_t *sp)
331 struct cam_devq *devq;
335 if((devq = cam_simq_alloc(256)) == NULL)
338 #if __FreeBSD_version >= 700000
339 mtx_init(&sp->cam_mtx, "isc-cam", NULL, MTX_DEF);
341 isp->cam_mtx = Giant;
343 sim = cam_sim_alloc(ic_action,
348 #if __FreeBSD_version >= 700000
351 1, // max_dev_transactions
352 0, // max_tagged_dev_transactions
356 #if __FreeBSD_version >= 700000
357 mtx_destroy(&sp->cam_mtx);
363 if(xpt_bus_register(sim,
364 #if __FreeBSD_version >= 700000
367 0/*bus_number*/) != CAM_SUCCESS) {
369 cam_sim_free(sim, /*free_devq*/TRUE);
371 #if __FreeBSD_version >= 700000
372 mtx_destroy(&sp->cam_mtx);
379 sdebug(1, "cam subsystem initialized");