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>
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)
95 #if __FreeBSD_version < 700000
96 ret = scsi_encap(sim, ccb);
98 isc_session_t *sp = cam_sim_softc(sim);
100 mtx_unlock(&sp->cam_mtx);
101 ret = scsi_encap(sim, ccb);
102 mtx_lock(&sp->cam_mtx);
108 ic_lost_target(isc_session_t *sp, int target)
111 sdebug(2, "lost target=%d", target);
113 if(sp->cam_path != NULL) {
114 mtx_lock(&sp->cam_mtx);
115 xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL);
116 xpt_free_path(sp->cam_path);
117 mtx_unlock(&sp->cam_mtx);
118 sp->cam_path = 0; // XXX
123 scan_callback(struct cam_periph *periph, union ccb *ccb)
125 isc_session_t *sp = (isc_session_t *)ccb->ccb_h.spriv_ptr0;
131 if(sp->flags & ISC_SCANWAIT) {
132 sp->flags &= ~ISC_SCANWAIT;
138 ic_scan(isc_session_t *sp)
143 sdebug(2, "scanning sid=%d", sp->sid);
145 sp->flags &= ~ISC_CAMDEVS;
146 sp->flags |= ISC_SCANWAIT;
148 ccb = xpt_alloc_ccb();
149 ccb->ccb_h.path = sp->cam_path;
150 ccb->ccb_h.cbfcnp = scan_callback;
151 ccb->ccb_h.spriv_ptr0 = sp;
155 while(sp->flags & ISC_SCANWAIT)
156 tsleep(sp, PRIBIO, "ffp", 5*hz); // the timeout time should
158 sdebug(2, "# of luns=%d", sp->target_nluns);
160 if(sp->target_nluns > 0) {
161 sp->flags |= ISC_CAMDEVS;
169 ic_action(struct cam_sim *sim, union ccb *ccb)
171 isc_session_t *sp = cam_sim_softc(sim);
172 struct ccb_hdr *ccb_h = &ccb->ccb_h;
176 ccb_h->spriv_ptr0 = sp;
177 sdebug(4, "func_code=0x%x flags=0x%x status=0x%x target=%d lun=%jx retry_count=%d timeout=%d",
178 ccb_h->func_code, ccb->ccb_h.flags, ccb->ccb_h.status,
179 ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun,
180 ccb->ccb_h.retry_count, ccb_h->timeout);
182 xdebug("sp == NULL! cannot happen");
185 switch(ccb_h->func_code) {
190 case XPT_RESET_BUS: // (can just be a stub that does nothing and completes)
192 struct ccb_pathinq *cpi = &ccb->cpi;
194 debug(3, "XPT_RESET_BUS");
195 cpi->ccb_h.status = CAM_REQ_CMP;
201 struct ccb_scsiio* csio = &ccb->csio;
203 debug(4, "XPT_SCSI_IO cmd=0x%x", csio->cdb_io.cdb_bytes[0]);
205 ccb_h->status = CAM_REQ_INVALID; //CAM_NO_NEXUS;
206 debug(4, "xpt_done.status=%d", ccb_h->status);
209 if(ccb_h->target_lun == CAM_LUN_WILDCARD) {
210 debug(3, "target=%d: bad lun (-1)", ccb_h->target_id);
211 ccb_h->status = CAM_LUN_INVALID;
214 if(_scsi_encap(sim, ccb) != 0)
219 case XPT_CALC_GEOMETRY:
221 struct ccb_calc_geometry *ccg;
224 debug(4, "sid=%d target=%d lun=%jx XPT_CALC_GEOMETRY vsize=%jd bsize=%d",
225 sp->sid, ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun,
226 ccg->volume_size, ccg->block_size);
227 if(ccg->block_size == 0 ||
228 (ccg->volume_size < ccg->block_size)) {
229 // print error message ...
230 /* XXX: what error is appropriate? */
236 lun = ccb->ccb_h.target_lun;
237 if(lun > ISCSI_MAX_LUNS) {
239 xdebug("lun %d > ISCSI_MAX_LUNS!\n", lun);
240 lun %= ISCSI_MAX_LUNS;
242 off = &sp->target_lun[lun / (sizeof(int)*8)];
243 boff = BIT(lun % (sizeof(int)*8));
244 debug(4, "sp->target_nluns=%d *off=%x boff=%x",
245 sp->target_nluns, *off, boff);
247 if((*off & boff) == 0) {
251 cam_calc_geometry(ccg, /*extended*/1);
256 case XPT_GET_TRAN_SETTINGS:
258 ccb_h->status = CAM_REQ_INVALID;
261 #if __FreeBSD_version < 700000
270 ic_poll(struct cam_sim *sim)
277 ic_getCamVals(isc_session_t *sp, iscsi_cam_t *cp)
281 if(sp && sp->cam_sim) {
282 cp->path_id = cam_sim_path(sp->cam_sim);
284 cp->target_nluns = ISCSI_MAX_LUNS; // XXX: -1?
291 ic_destroy(isc_session_t *sp )
295 if(sp->cam_path != NULL) {
296 sdebug(2, "name=%s unit=%d",
297 cam_sim_name(sp->cam_sim), cam_sim_unit(sp->cam_sim));
300 xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL);
302 xpt_async(XPT_RESET_BUS, sp->cam_path, NULL);
304 xpt_free_path(sp->cam_path);
305 xpt_bus_deregister(cam_sim_path(sp->cam_sim));
306 cam_sim_free(sp->cam_sim, TRUE /*free_devq*/);
314 ic_init(isc_session_t *sp)
317 struct cam_devq *devq;
321 if((devq = cam_simq_alloc(256)) == NULL)
324 #if __FreeBSD_version >= 700000
325 mtx_init(&sp->cam_mtx, "isc-cam", NULL, MTX_DEF);
327 isp->cam_mtx = Giant;
329 sim = cam_sim_alloc(ic_action,
334 #if __FreeBSD_version >= 700000
337 1, // max_dev_transactions
338 0, // max_tagged_dev_transactions
342 #if __FreeBSD_version >= 700000
343 mtx_destroy(&sp->cam_mtx);
349 if(xpt_bus_register(sim,
350 #if __FreeBSD_version >= 700000
353 0/*bus_number*/) != CAM_SUCCESS) {
355 cam_sim_free(sim, /*free_devq*/TRUE);
357 #if __FreeBSD_version >= 700000
358 mtx_destroy(&sp->cam_mtx);
363 if(xpt_create_path(&sp->cam_path, NULL, cam_sim_path(sp->cam_sim),
364 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
365 xpt_bus_deregister(cam_sim_path(sp->cam_sim));
366 cam_sim_free(sim, /*free_devq*/TRUE);
368 #if __FreeBSD_version >= 700000
369 mtx_destroy(&sp->cam_mtx);
375 sdebug(1, "cam subsystem initialized");