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#if __FreeBSD_version >= 700000 41#include <sys/lock.h> 42#include <sys/mutex.h> 43#endif 44#include <sys/conf.h> 45#include <sys/systm.h> 46#include <sys/malloc.h> 47#include <sys/mbuf.h> 48#include <sys/uio.h> 49#include <sys/sysctl.h> 50#include <sys/sx.h> 51#include <vm/uma.h> 52 53#include <cam/cam.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> 58 59#include <dev/iscsi_initiator/iscsi.h> 60#include <dev/iscsi_initiator/iscsivar.h> 61 62static void 63_inq(struct cam_sim *sim, union ccb *ccb) 64{ 65 struct ccb_pathinq *cpi = &ccb->cpi; 66 isc_session_t *sp = cam_sim_softc(sim); 67 68 debug_called(8); 69 debug(3, "sid=%d target=%d lun=%jx", sp->sid, ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun); 70 71 cpi->version_num = 1; /* XXX??? */ 72 cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE | PI_WIDE_32; 73 cpi->target_sprt = 0; 74 cpi->hba_misc = 0; 75 cpi->hba_eng_cnt = 0; 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; 89#endif 90} 91 92static __inline int 93_scsi_encap(struct cam_sim *sim, union ccb *ccb) 94{ 95 int ret; 96 97#if __FreeBSD_version < 700000 98 ret = scsi_encap(sim, ccb); 99#else 100 isc_session_t *sp = cam_sim_softc(sim); 101 102 mtx_unlock(&sp->cam_mtx); 103 ret = scsi_encap(sim, ccb); 104 mtx_lock(&sp->cam_mtx); 105#endif 106 return ret; 107} 108 109void 110ic_lost_target(isc_session_t *sp, int target) 111{ 112 debug_called(8); 113 sdebug(2, "lost target=%d", target); 114 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 121 } 122} 123 124static void 125scan_callback(struct cam_periph *periph, union ccb *ccb) 126{ 127 isc_session_t *sp = (isc_session_t *)ccb->ccb_h.spriv_ptr0; 128 129 debug_called(8); 130 131 xpt_free_ccb(ccb); 132 133 if(sp->flags & ISC_SCANWAIT) { 134 sp->flags &= ~ISC_SCANWAIT; 135 wakeup(sp); 136 } 137} 138 139static int 140ic_scan(isc_session_t *sp) 141{ 142 union ccb *ccb; 143 144 debug_called(8); 145 sdebug(2, "scanning sid=%d", sp->sid); 146 147 sp->flags &= ~ISC_CAMDEVS; 148 sp->flags |= ISC_SCANWAIT; 149 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; 154 155 xpt_rescan(ccb); 156 157 while(sp->flags & ISC_SCANWAIT) 158 tsleep(sp, PRIBIO, "ffp", 5*hz); // the timeout time should 159 // be configurable 160 sdebug(2, "# of luns=%d", sp->target_nluns); 161 162 if(sp->target_nluns > 0) { 163 sp->flags |= ISC_CAMDEVS; 164 return 0; 165 } 166 167 return ENODEV; 168} 169 170static void 171ic_action(struct cam_sim *sim, union ccb *ccb) 172{ 173 isc_session_t *sp = cam_sim_softc(sim); 174 struct ccb_hdr *ccb_h = &ccb->ccb_h; 175 176 debug_called(8); 177 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); 183 if(sp == NULL) { 184 xdebug("sp == NULL! cannot happen"); 185 return; 186 } 187 switch(ccb_h->func_code) { 188 case XPT_PATH_INQ: 189 _inq(sim, ccb); 190 break; 191 192 case XPT_RESET_BUS: // (can just be a stub that does nothing and completes) 193 { 194 struct ccb_pathinq *cpi = &ccb->cpi; 195 196 debug(3, "XPT_RESET_BUS"); 197 cpi->ccb_h.status = CAM_REQ_CMP; 198 break; 199 } 200 201 case XPT_SCSI_IO: 202 { 203 struct ccb_scsiio* csio = &ccb->csio; 204 205 debug(4, "XPT_SCSI_IO cmd=0x%x", csio->cdb_io.cdb_bytes[0]); 206 if(sp == NULL) { 207 ccb_h->status = CAM_REQ_INVALID; //CAM_NO_NEXUS; 208 debug(4, "xpt_done.status=%d", ccb_h->status); 209 break; 210 } 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; 214 break; 215 } 216 if(_scsi_encap(sim, ccb) != 0) 217 return; 218 break; 219 } 220 221 case XPT_CALC_GEOMETRY: 222 { 223 struct ccb_calc_geometry *ccg; 224 225 ccg = &ccb->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? */ 233 break; 234 } 235 else { 236 int lun, *off, boff; 237 238 lun = ccb->ccb_h.target_lun; 239 if(lun > ISCSI_MAX_LUNS) { 240 // XXX: 241 xdebug("lun %d > ISCSI_MAX_LUNS!\n", lun); 242 lun %= ISCSI_MAX_LUNS; 243 } 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); 248 249 if((*off & boff) == 0) { 250 sp->target_nluns++; 251 *off |= boff; 252 } 253 cam_calc_geometry(ccg, /*extended*/1); 254 } 255 break; 256 } 257 258 case XPT_GET_TRAN_SETTINGS: 259 default: 260 ccb_h->status = CAM_REQ_INVALID; 261 break; 262 } 263#if __FreeBSD_version < 700000 264 XPT_DONE(sp, ccb); 265#else 266 xpt_done(ccb); 267#endif 268 return; 269} 270 271static void 272ic_poll(struct cam_sim *sim) 273{ 274 debug_called(4); 275 276} 277 278int 279ic_getCamVals(isc_session_t *sp, iscsi_cam_t *cp) 280{ 281 debug_called(8); 282 283 if(sp && sp->cam_sim) { 284 cp->path_id = cam_sim_path(sp->cam_sim); 285 cp->target_id = 0; 286 cp->target_nluns = ISCSI_MAX_LUNS; // XXX: -1? 287 return 0; 288 } 289 return ENXIO; 290} 291 292void 293ic_destroy(isc_session_t *sp ) 294{ 295 debug_called(8); 296 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)); 300 CAM_LOCK(sp); 301#if 0 302 xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL); 303#else 304 xpt_async(XPT_RESET_BUS, sp->cam_path, NULL); 305#endif 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*/); 309 310 CAM_UNLOCK(sp); 311 sdebug(2, "done"); 312 } 313} 314 315int 316ic_init(isc_session_t *sp) 317{ 318 struct cam_sim *sim; 319 struct cam_devq *devq; 320 321 debug_called(8); 322 323 if((devq = cam_simq_alloc(256)) == NULL) 324 return ENOMEM; 325 326#if __FreeBSD_version >= 700000 327 mtx_init(&sp->cam_mtx, "isc-cam", NULL, MTX_DEF); 328#else 329 isp->cam_mtx = Giant; 330#endif 331 sim = cam_sim_alloc(ic_action, 332 ic_poll, 333 "iscsi", 334 sp, 335 sp->sid, // unit 336#if __FreeBSD_version >= 700000 337 &sp->cam_mtx, 338#endif 339 1, // max_dev_transactions 340 0, // max_tagged_dev_transactions 341 devq); 342 if(sim == NULL) { 343 cam_simq_free(devq); 344#if __FreeBSD_version >= 700000 345 mtx_destroy(&sp->cam_mtx); 346#endif 347 return ENXIO; 348 } 349 350 CAM_LOCK(sp); 351 if(xpt_bus_register(sim, 352#if __FreeBSD_version >= 700000 353 NULL, 354#endif 355 0/*bus_number*/) != CAM_SUCCESS) { 356 357 cam_sim_free(sim, /*free_devq*/TRUE); 358 CAM_UNLOCK(sp); 359#if __FreeBSD_version >= 700000 360 mtx_destroy(&sp->cam_mtx); 361#endif 362 return ENXIO; 363 } 364 sp->cam_sim = sim; 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); 369 CAM_UNLOCK(sp); 370#if __FreeBSD_version >= 700000 371 mtx_destroy(&sp->cam_mtx); 372#endif 373 return ENXIO; 374 } 375 CAM_UNLOCK(sp); 376 377 sdebug(1, "cam subsystem initialized"); 378 379 ic_scan(sp); 380 381 return 0; 382} 383