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>
41 #include <sys/mutex.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
47 #include <sys/sysctl.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>
57 #include <dev/iscsi_initiator/iscsi.h>
58 #include <dev/iscsi_initiator/iscsivar.h>
61 _inq(struct cam_sim *sim, union ccb *ccb)
63 struct ccb_pathinq *cpi = &ccb->cpi;
64 isc_session_t *sp = cam_sim_softc(sim);
67 debug(3, "sid=%d target=%d lun=%jx", sp->sid, ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun);
69 cpi->version_num = 1; /* XXX??? */
70 cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE | PI_WIDE_32;
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;
91 _scsi_encap(struct cam_sim *sim, union ccb *ccb)
94 isc_session_t *sp = cam_sim_softc(sim);
96 mtx_unlock(&sp->cam_mtx);
97 ret = scsi_encap(sim, ccb);
98 mtx_lock(&sp->cam_mtx);
104 ic_lost_target(isc_session_t *sp, int target)
107 sdebug(2, "lost target=%d", target);
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
119 scan_callback(struct cam_periph *periph, union ccb *ccb)
121 isc_session_t *sp = (isc_session_t *)ccb->ccb_h.spriv_ptr0;
127 if(sp->flags & ISC_SCANWAIT) {
128 sp->flags &= ~ISC_SCANWAIT;
134 ic_scan(isc_session_t *sp)
139 sdebug(2, "scanning sid=%d", sp->sid);
141 sp->flags &= ~ISC_CAMDEVS;
142 sp->flags |= ISC_SCANWAIT;
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;
151 while(sp->flags & ISC_SCANWAIT)
152 tsleep(sp, PRIBIO, "ffp", 5*hz); // the timeout time should
154 sdebug(2, "# of luns=%d", sp->target_nluns);
156 if(sp->target_nluns > 0) {
157 sp->flags |= ISC_CAMDEVS;
165 ic_action(struct cam_sim *sim, union ccb *ccb)
167 isc_session_t *sp = cam_sim_softc(sim);
168 struct ccb_hdr *ccb_h = &ccb->ccb_h;
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);
178 xdebug("sp == NULL! cannot happen");
181 switch(ccb_h->func_code) {
186 case XPT_RESET_BUS: // (can just be a stub that does nothing and completes)
188 struct ccb_pathinq *cpi = &ccb->cpi;
190 debug(3, "XPT_RESET_BUS");
191 cpi->ccb_h.status = CAM_REQ_CMP;
197 struct ccb_scsiio* csio = &ccb->csio;
199 debug(4, "XPT_SCSI_IO cmd=0x%x", csio->cdb_io.cdb_bytes[0]);
201 ccb_h->status = CAM_REQ_INVALID; //CAM_NO_NEXUS;
202 debug(4, "xpt_done.status=%d", ccb_h->status);
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;
210 if(_scsi_encap(sim, ccb) != 0)
215 case XPT_CALC_GEOMETRY:
217 struct ccb_calc_geometry *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? */
232 lun = ccb->ccb_h.target_lun;
233 if(lun > ISCSI_MAX_LUNS) {
235 xdebug("lun %d > ISCSI_MAX_LUNS!\n", lun);
236 lun %= ISCSI_MAX_LUNS;
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);
243 if((*off & boff) == 0) {
247 cam_calc_geometry(ccg, /*extended*/1);
252 case XPT_GET_TRAN_SETTINGS:
254 ccb_h->status = CAM_REQ_INVALID;
262 ic_poll(struct cam_sim *sim)
269 ic_getCamVals(isc_session_t *sp, iscsi_cam_t *cp)
273 if(sp && sp->cam_sim) {
274 cp->path_id = cam_sim_path(sp->cam_sim);
276 cp->target_nluns = ISCSI_MAX_LUNS; // XXX: -1?
283 ic_destroy(isc_session_t *sp )
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));
291 xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL);
293 xpt_async(XPT_RESET_BUS, sp->cam_path, NULL);
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*/);
304 ic_init(isc_session_t *sp)
307 struct cam_devq *devq;
311 if((devq = cam_simq_alloc(256)) == NULL)
314 mtx_init(&sp->cam_mtx, "isc-cam", NULL, MTX_DEF);
315 sim = cam_sim_alloc(ic_action,
321 1, // max_dev_transactions
322 0, // max_tagged_dev_transactions
326 mtx_destroy(&sp->cam_mtx);
330 if(xpt_bus_register(sim,
332 0/*bus_number*/) != CAM_SUCCESS) {
333 cam_sim_free(sim, /*free_devq*/TRUE);
334 mtx_destroy(&sp->cam_mtx);
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);
346 sdebug(1, "cam subsystem initialized");