2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2010 Daniel Braniss <danny@cs.huji.ac.il>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 | $Id: isc_cam.c 998 2009-12-20 10:32:45Z danny $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_iscsi_initiator.h"
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/callout.h>
40 #if __FreeBSD_version >= 700000
42 #include <sys/mutex.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
49 #include <sys/sysctl.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_sim.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/cam_periph.h>
59 #include <dev/iscsi_initiator/iscsi.h>
60 #include <dev/iscsi_initiator/iscsivar.h>
63 _inq(struct cam_sim *sim, union ccb *ccb)
65 struct ccb_pathinq *cpi = &ccb->cpi;
66 isc_session_t *sp = cam_sim_softc(sim);
69 debug(3, "sid=%d target=%d lun=%jx", sp->sid, ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun);
71 cpi->version_num = 1; /* XXX??? */
72 cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE | PI_WIDE_32;
76 cpi->max_target = 0; //ISCSI_MAX_TARGETS - 1;
77 cpi->initiator_id = ISCSI_MAX_TARGETS;
78 cpi->max_lun = sp->opt.maxluns - 1;
79 cpi->bus_id = cam_sim_bus(sim);
80 cpi->base_transfer_speed = 3300; // 40000; // XXX:
81 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
82 strlcpy(cpi->hba_vid, "iSCSI", HBA_IDLEN);
83 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
84 cpi->unit_number = cam_sim_unit(sim);
85 cpi->ccb_h.status = CAM_REQ_CMP;
86 #if defined(KNOB_VALID_ADDRESS)
87 cpi->transport = XPORT_ISCSI;
88 cpi->transport_version = 0;
93 _scsi_encap(struct cam_sim *sim, union ccb *ccb)
97 #if __FreeBSD_version < 700000
98 ret = scsi_encap(sim, ccb);
100 isc_session_t *sp = cam_sim_softc(sim);
102 mtx_unlock(&sp->cam_mtx);
103 ret = scsi_encap(sim, ccb);
104 mtx_lock(&sp->cam_mtx);
110 ic_lost_target(isc_session_t *sp, int target)
113 sdebug(2, "lost target=%d", target);
115 if(sp->cam_path != NULL) {
116 mtx_lock(&sp->cam_mtx);
117 xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL);
118 xpt_free_path(sp->cam_path);
119 mtx_unlock(&sp->cam_mtx);
120 sp->cam_path = 0; // XXX
125 scan_callback(struct cam_periph *periph, union ccb *ccb)
127 isc_session_t *sp = (isc_session_t *)ccb->ccb_h.spriv_ptr0;
133 if(sp->flags & ISC_SCANWAIT) {
134 sp->flags &= ~ISC_SCANWAIT;
140 ic_scan(isc_session_t *sp)
145 sdebug(2, "scanning sid=%d", sp->sid);
147 sp->flags &= ~ISC_CAMDEVS;
148 sp->flags |= ISC_SCANWAIT;
150 ccb = xpt_alloc_ccb();
151 ccb->ccb_h.path = sp->cam_path;
152 ccb->ccb_h.cbfcnp = scan_callback;
153 ccb->ccb_h.spriv_ptr0 = sp;
157 while(sp->flags & ISC_SCANWAIT)
158 tsleep(sp, PRIBIO, "ffp", 5*hz); // the timeout time should
160 sdebug(2, "# of luns=%d", sp->target_nluns);
162 if(sp->target_nluns > 0) {
163 sp->flags |= ISC_CAMDEVS;
171 ic_action(struct cam_sim *sim, union ccb *ccb)
173 isc_session_t *sp = cam_sim_softc(sim);
174 struct ccb_hdr *ccb_h = &ccb->ccb_h;
178 ccb_h->spriv_ptr0 = sp;
179 sdebug(4, "func_code=0x%x flags=0x%x status=0x%x target=%d lun=%jx retry_count=%d timeout=%d",
180 ccb_h->func_code, ccb->ccb_h.flags, ccb->ccb_h.status,
181 ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun,
182 ccb->ccb_h.retry_count, ccb_h->timeout);
184 xdebug("sp == NULL! cannot happen");
187 switch(ccb_h->func_code) {
192 case XPT_RESET_BUS: // (can just be a stub that does nothing and completes)
194 struct ccb_pathinq *cpi = &ccb->cpi;
196 debug(3, "XPT_RESET_BUS");
197 cpi->ccb_h.status = CAM_REQ_CMP;
203 struct ccb_scsiio* csio = &ccb->csio;
205 debug(4, "XPT_SCSI_IO cmd=0x%x", csio->cdb_io.cdb_bytes[0]);
207 ccb_h->status = CAM_REQ_INVALID; //CAM_NO_NEXUS;
208 debug(4, "xpt_done.status=%d", ccb_h->status);
211 if(ccb_h->target_lun == CAM_LUN_WILDCARD) {
212 debug(3, "target=%d: bad lun (-1)", ccb_h->target_id);
213 ccb_h->status = CAM_LUN_INVALID;
216 if(_scsi_encap(sim, ccb) != 0)
221 case XPT_CALC_GEOMETRY:
223 struct ccb_calc_geometry *ccg;
226 debug(4, "sid=%d target=%d lun=%jx XPT_CALC_GEOMETRY vsize=%jd bsize=%d",
227 sp->sid, ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun,
228 ccg->volume_size, ccg->block_size);
229 if(ccg->block_size == 0 ||
230 (ccg->volume_size < ccg->block_size)) {
231 // print error message ...
232 /* XXX: what error is appropriate? */
238 lun = ccb->ccb_h.target_lun;
239 if(lun > ISCSI_MAX_LUNS) {
241 xdebug("lun %d > ISCSI_MAX_LUNS!\n", lun);
242 lun %= ISCSI_MAX_LUNS;
244 off = &sp->target_lun[lun / (sizeof(int)*8)];
245 boff = BIT(lun % (sizeof(int)*8));
246 debug(4, "sp->target_nluns=%d *off=%x boff=%x",
247 sp->target_nluns, *off, boff);
249 if((*off & boff) == 0) {
253 cam_calc_geometry(ccg, /*extended*/1);
258 case XPT_GET_TRAN_SETTINGS:
260 ccb_h->status = CAM_REQ_INVALID;
263 #if __FreeBSD_version < 700000
272 ic_poll(struct cam_sim *sim)
279 ic_getCamVals(isc_session_t *sp, iscsi_cam_t *cp)
283 if(sp && sp->cam_sim) {
284 cp->path_id = cam_sim_path(sp->cam_sim);
286 cp->target_nluns = ISCSI_MAX_LUNS; // XXX: -1?
293 ic_destroy(isc_session_t *sp )
297 if(sp->cam_path != NULL) {
298 sdebug(2, "name=%s unit=%d",
299 cam_sim_name(sp->cam_sim), cam_sim_unit(sp->cam_sim));
302 xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL);
304 xpt_async(XPT_RESET_BUS, sp->cam_path, NULL);
306 xpt_free_path(sp->cam_path);
307 xpt_bus_deregister(cam_sim_path(sp->cam_sim));
308 cam_sim_free(sp->cam_sim, TRUE /*free_devq*/);
316 ic_init(isc_session_t *sp)
319 struct cam_devq *devq;
323 if((devq = cam_simq_alloc(256)) == NULL)
326 #if __FreeBSD_version >= 700000
327 mtx_init(&sp->cam_mtx, "isc-cam", NULL, MTX_DEF);
329 isp->cam_mtx = Giant;
331 sim = cam_sim_alloc(ic_action,
336 #if __FreeBSD_version >= 700000
339 1, // max_dev_transactions
340 0, // max_tagged_dev_transactions
344 #if __FreeBSD_version >= 700000
345 mtx_destroy(&sp->cam_mtx);
351 if(xpt_bus_register(sim,
352 #if __FreeBSD_version >= 700000
355 0/*bus_number*/) != CAM_SUCCESS) {
357 cam_sim_free(sim, /*free_devq*/TRUE);
359 #if __FreeBSD_version >= 700000
360 mtx_destroy(&sp->cam_mtx);
365 if(xpt_create_path(&sp->cam_path, NULL, cam_sim_path(sp->cam_sim),
366 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
367 xpt_bus_deregister(cam_sim_path(sp->cam_sim));
368 cam_sim_free(sim, /*free_devq*/TRUE);
370 #if __FreeBSD_version >= 700000
371 mtx_destroy(&sp->cam_mtx);
377 sdebug(1, "cam subsystem initialized");