1 /*- 2 * Copyright (c) 2005-2008 Daniel Braniss <danny@cs.huji.ac.il> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/sys/dev/iscsi/initiator/isc_cam.c,v 1.3 2008/11/25 07:17:11 scottl Exp $ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/kernel.h> 31 #include <sys/callout.h> 32 #include <sys/lock.h> 33 #include <sys/conf.h> 34 #include <sys/systm.h> 35 #include <sys/malloc.h> 36 #include <sys/mbuf.h> 37 #include <sys/uio.h> 38 #include <sys/sysctl.h> 39 #include <sys/eventhandler.h> 40 #include <sys/globaldata.h> 41 #include <sys/mutex.h> 42 #include <sys/mutex2.h> 43 44 #include <bus/cam/cam.h> 45 #include <bus/cam/cam_ccb.h> 46 #include <bus/cam/cam_sim.h> 47 #include <bus/cam/cam_xpt_sim.h> 48 #include <bus/cam/cam_periph.h> 49 50 #include <dev/disk/iscsi/initiator/iscsi.h> 51 #include <dev/disk/iscsi/initiator/iscsivar.h> 52 53 // XXX: untested/incomplete 54 void 55 ic_freeze(isc_session_t *sp) 56 { 57 debug_called(8); 58 #if 0 59 sdebug(2, "freezing path=%p", sp->cam_path == NULL? 0: sp->cam_path); 60 if((sp->cam_path != NULL) && !(sp->flags & ISC_FROZEN)) { 61 xpt_freeze_devq(sp->cam_path, 1); 62 } 63 #endif 64 sp->flags |= ISC_FROZEN; 65 } 66 67 // XXX: untested/incomplete 68 void 69 ic_release(isc_session_t *sp) 70 { 71 debug_called(8); 72 #if 0 73 sdebug(2, "release path=%p", sp->cam_path == NULL? 0: sp->cam_path); 74 if((sp->cam_path != NULL) && (sp->flags & ISC_FROZEN)) { 75 xpt_release_devq(sp->cam_path, 1, TRUE); 76 } 77 #endif 78 sp->flags &= ~ISC_FROZEN; 79 } 80 81 void 82 ic_lost_target(isc_session_t *sp, int target) 83 { 84 struct isc_softc *isp = sp->isc; 85 86 debug_called(8); 87 sdebug(2, "target=%d", target); 88 if(sp->cam_path != NULL) { 89 lockmgr(&isp->cam_lock, LK_EXCLUSIVE); 90 xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL); 91 xpt_free_path(sp->cam_path); 92 lockmgr(&isp->cam_lock, LK_RELEASE); 93 sp->cam_path = 0; // XXX 94 } 95 } 96 97 static void 98 _scan_callback(struct cam_periph *periph, union ccb *ccb) 99 { 100 isc_session_t *sp = (isc_session_t *)ccb->ccb_h.spriv_ptr0; 101 102 debug_called(8); 103 104 kfree(ccb, M_TEMP); 105 106 if(sp->flags & ISC_FFPWAIT) { 107 sp->flags &= ~ISC_FFPWAIT; 108 wakeup(sp); 109 } 110 } 111 112 static void 113 _scan_target(isc_session_t *sp, int target) 114 { 115 union ccb *ccb; 116 117 debug_called(8); 118 sdebug(2, "target=%d", target); 119 120 if((ccb = kmalloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO)) == NULL) { 121 xdebug("scan failed (can't allocate CCB)"); 122 return; 123 } 124 CAM_LOCK(sp->isc); 125 xpt_setup_ccb(&ccb->ccb_h, sp->cam_path, 5/*priority (low)*/); 126 ccb->ccb_h.func_code = XPT_SCAN_BUS; 127 ccb->ccb_h.cbfcnp = _scan_callback; 128 ccb->crcn.flags = CAM_FLAG_NONE; 129 ccb->ccb_h.spriv_ptr0 = sp; 130 131 xpt_action(ccb); 132 CAM_UNLOCK(sp->isc); 133 } 134 135 int 136 ic_fullfeature(struct cdev *dev) 137 { 138 struct isc_softc *isp = dev->si_drv1; 139 isc_session_t *sp = (isc_session_t *)dev->si_drv2; 140 141 debug_called(8); 142 sdebug(3, "dev=%d sc=%p", minor(dev), isp); 143 144 sp->flags &= ~ISC_FFPHASE; 145 sp->flags |= ISC_FFPWAIT; 146 147 CAM_LOCK(isp); 148 if(xpt_create_path(&sp->cam_path, xpt_periph, cam_sim_path(sp->isc->cam_sim), 149 sp->sid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 150 xdebug("can't create cam path"); 151 CAM_UNLOCK(isp); 152 return ENODEV; // XXX 153 } 154 CAM_UNLOCK(isp); 155 156 _scan_target(sp, sp->sid); 157 158 while(sp->flags & ISC_FFPWAIT) 159 tsleep(sp, 0, "ffp", 5*hz); // timeout should be configurable 160 if(sp->target_nluns > 0) { 161 sp->flags |= ISC_FFPHASE; 162 return 0; 163 } 164 165 return ENODEV; 166 } 167 168 static void 169 _inq(struct cam_sim *sim, union ccb *ccb, int maxluns) 170 { 171 struct ccb_pathinq *cpi = &ccb->cpi; 172 173 debug_called(4); 174 175 cpi->version_num = 1; /* XXX??? */ 176 cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE | PI_WIDE_32; 177 cpi->target_sprt = 0; 178 cpi->hba_misc = 0; 179 cpi->hba_eng_cnt = 0; 180 cpi->max_target = ISCSI_MAX_TARGETS - 1; 181 cpi->initiator_id = ISCSI_MAX_TARGETS; 182 cpi->max_lun = maxluns; 183 cpi->bus_id = cam_sim_bus(sim); 184 cpi->base_transfer_speed = 3300; 185 strncpy(cpi->sim_vid, "DragonFly", SIM_IDLEN); 186 strncpy(cpi->hba_vid, "iSCSI", HBA_IDLEN); 187 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 188 cpi->unit_number = cam_sim_unit(sim); 189 cpi->ccb_h.status = CAM_REQ_CMP; 190 } 191 192 static __inline int 193 _scsi_encap(struct cam_sim *sim, union ccb *ccb) 194 { 195 int ret; 196 struct isc_softc *isp = (struct isc_softc *)cam_sim_softc(sim); 197 198 lockmgr(&isp->cam_lock, LK_EXCLUSIVE); 199 ret = scsi_encap(sim, ccb); 200 lockmgr(&isp->cam_lock, LK_RELEASE); 201 return ret; 202 } 203 204 static void 205 ic_action(struct cam_sim *sim, union ccb *ccb) 206 { 207 struct ccb_hdr *ccb_h = &ccb->ccb_h; 208 struct isc_softc *isp = (struct isc_softc *)cam_sim_softc(sim); 209 isc_session_t *sp; 210 211 debug_called(8); 212 213 if((ccb_h->target_id != CAM_TARGET_WILDCARD) && (ccb_h->target_id < MAX_SESSIONS)) 214 sp = isp->sessions[ccb_h->target_id]; 215 else 216 sp = NULL; 217 218 ccb_h->spriv_ptr0 = sp; 219 220 debug(4, "func_code=0x%x flags=0x%x status=0x%x target=%d lun=%d retry_count=%d timeout=%d", 221 ccb_h->func_code, ccb->ccb_h.flags, ccb->ccb_h.status, 222 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 223 ccb->ccb_h.retry_count, ccb_h->timeout); 224 /* 225 | first quick check 226 */ 227 switch(ccb_h->func_code) { 228 default: 229 // XXX: maybe check something else? 230 break; 231 232 case XPT_SCSI_IO: 233 case XPT_RESET_DEV: 234 case XPT_GET_TRAN_SETTINGS: 235 case XPT_SET_TRAN_SETTINGS: 236 case XPT_CALC_GEOMETRY: 237 if(sp == NULL) { 238 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 239 xpt_done(ccb); 240 return; 241 } 242 break; 243 244 case XPT_PATH_INQ: 245 case XPT_NOOP: 246 if(sp == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) { 247 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 248 xpt_done(ccb); 249 debug(4, "status = CAM_DEV_NOT_THERE"); 250 return; 251 } 252 } 253 254 switch(ccb_h->func_code) { 255 256 case XPT_PATH_INQ: 257 _inq(sim, ccb, (sp? sp->opt.maxluns: ISCSI_MAX_LUNS) - 1); 258 break; 259 260 case XPT_RESET_BUS: // (can just be a stub that does nothing and completes) 261 { 262 struct ccb_pathinq *cpi = &ccb->cpi; 263 264 debug(3, "XPT_RESET_BUS"); 265 cpi->ccb_h.status = CAM_REQ_CMP; 266 break; 267 } 268 269 case XPT_SCSI_IO: 270 { 271 struct ccb_scsiio* csio = &ccb->csio; 272 273 debug(4, "XPT_SCSI_IO cmd=0x%x", csio->cdb_io.cdb_bytes[0]); 274 if(sp == NULL) { 275 ccb_h->status = CAM_REQ_INVALID; //CAM_NO_NEXUS; 276 debug(4, "xpt_done.status=%d", ccb_h->status); 277 break; 278 } 279 if(ccb_h->target_lun == CAM_LUN_WILDCARD) { 280 debug(3, "target=%d: bad lun (-1)", ccb_h->target_id); 281 ccb_h->status = CAM_LUN_INVALID; 282 break; 283 } 284 if(_scsi_encap(sim, ccb) != 0) 285 return; 286 break; 287 } 288 289 case XPT_CALC_GEOMETRY: 290 { 291 struct ccb_calc_geometry *ccg; 292 293 ccg = &ccb->ccg; 294 debug(6, "XPT_CALC_GEOMETRY vsize=%jd bsize=%d", ccg->volume_size, ccg->block_size); 295 if(ccg->block_size == 0 || 296 (ccg->volume_size < ccg->block_size)) { 297 // print error message ... 298 /* XXX: what error is appropiate? */ 299 break; 300 } else 301 cam_calc_geometry(ccg, /*extended*/1); 302 break; 303 } 304 305 case XPT_GET_TRAN_SETTINGS: 306 default: 307 ccb_h->status = CAM_REQ_INVALID; 308 break; 309 } 310 xpt_done(ccb); 311 return; 312 } 313 314 static void 315 ic_poll(struct cam_sim *sim) 316 { 317 debug_called(8); 318 319 } 320 321 int 322 ic_getCamVals(isc_session_t *sp, iscsi_cam_t *cp) 323 { 324 int i; 325 326 debug_called(8); 327 328 if(sp && sp->isc->cam_sim) { 329 cp->path_id = cam_sim_path(sp->isc->cam_sim); 330 cp->target_id = sp->sid; 331 cp->target_nluns = sp->target_nluns; // XXX: -1? 332 for(i = 0; i < cp->target_nluns; i++) 333 cp->target_lun[i] = sp->target_lun[i]; 334 return 0; 335 } 336 return ENXIO; 337 } 338 339 void 340 ic_destroy(struct isc_softc *isp) 341 { 342 debug_called(8); 343 344 CAM_LOCK(isp); // can't harm :-) 345 346 xpt_async(AC_LOST_DEVICE, isp->cam_path, NULL); 347 xpt_free_path(isp->cam_path); 348 xpt_bus_deregister(cam_sim_path(isp->cam_sim)); 349 cam_sim_free(isp->cam_sim); 350 351 CAM_UNLOCK(isp); 352 } 353 354 int 355 ic_init(struct isc_softc *isp) 356 { 357 struct cam_sim *sim; 358 struct cam_devq *devq; 359 struct cam_path *path; 360 361 if((devq = cam_simq_alloc(256)) == NULL) 362 return ENOMEM; 363 364 lockinit(&isp->cam_lock, "isc-cam", 0, LK_CANRECURSE); 365 sim = cam_sim_alloc(ic_action, ic_poll, 366 "iscsi", isp, 0, /* unit */ 367 &sim_mplock, 368 1, /* max_dev_transactions */ 369 100, /* max_tagged_dev_transactions */ 370 devq); 371 if(sim == NULL) { 372 cam_simq_release(devq); 373 lockuninit(&isp->cam_lock); 374 return ENXIO; 375 } 376 CAM_LOCK(isp); 377 if(xpt_bus_register(sim, 378 0/*bus_number*/) != CAM_SUCCESS) 379 goto bad; 380 381 if(xpt_create_path(&path, xpt_periph, cam_sim_path(sim), 382 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 383 xpt_bus_deregister(cam_sim_path(sim)); 384 goto bad; 385 } 386 387 CAM_UNLOCK(isp); 388 389 isp->cam_sim = sim; 390 isp->cam_path = path; 391 392 debug(2, "cam subsystem initialized"); // XXX: add dev ... 393 debug(4, "sim=%p path=%p", sim, path); 394 return 0; 395 396 bad: 397 cam_sim_free(sim); 398 CAM_UNLOCK(isp); 399 return ENXIO; 400 } 401