vpo.c revision 185003
1/*- 2 * Copyright (c) 1997, 1998, 1999 Nicolas Souchu 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 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: head/sys/dev/ppbus/vpo.c 185003 2008-11-16 17:42:02Z jhb $"); 30 31#include <sys/param.h> 32#include <sys/systm.h> 33#include <sys/module.h> 34#include <sys/bus.h> 35#include <sys/lock.h> 36#include <sys/mutex.h> 37#include <sys/malloc.h> 38 39#include <cam/cam.h> 40#include <cam/cam_ccb.h> 41#include <cam/cam_sim.h> 42#include <cam/cam_xpt_sim.h> 43#include <cam/cam_debug.h> 44#include <cam/cam_periph.h> 45 46#include <cam/scsi/scsi_all.h> 47#include <cam/scsi/scsi_message.h> 48#include <cam/scsi/scsi_da.h> 49 50#include <sys/kernel.h> 51 52#include "opt_vpo.h" 53 54#include <dev/ppbus/ppbconf.h> 55#include <dev/ppbus/vpoio.h> 56 57#include "ppbus_if.h" 58 59struct vpo_sense { 60 struct scsi_sense cmd; 61 unsigned int stat; 62 unsigned int count; 63}; 64 65struct vpo_data { 66 device_t vpo_dev; 67 int vpo_stat; 68 int vpo_count; 69 int vpo_error; 70 71 int vpo_isplus; 72 73 struct cam_sim *sim; 74 75 struct vpo_sense vpo_sense; 76 77 struct vpoio_data vpo_io; /* interface to low level functions */ 78}; 79 80#define DEVTOSOFTC(dev) \ 81 ((struct vpo_data *)device_get_softc(dev)) 82 83/* cam related functions */ 84static void vpo_action(struct cam_sim *sim, union ccb *ccb); 85static void vpo_poll(struct cam_sim *sim); 86static void vpo_cam_rescan_callback(struct cam_periph *periph, 87 union ccb *ccb); 88static void vpo_cam_rescan(struct vpo_data *vpo); 89 90static void 91vpo_identify(driver_t *driver, device_t parent) 92{ 93 94 device_t dev; 95 96 dev = device_find_child(parent, "vpo", -1); 97 if (!dev) 98 BUS_ADD_CHILD(parent, 0, "vpo", -1); 99} 100 101/* 102 * vpo_probe() 103 */ 104static int 105vpo_probe(device_t dev) 106{ 107 struct vpo_data *vpo; 108 int error; 109 110 vpo = DEVTOSOFTC(dev); 111 vpo->vpo_dev = dev; 112 113 /* check ZIP before ZIP+ or imm_probe() will send controls to 114 * the printer or whatelse connected to the port */ 115 if ((error = vpoio_probe(dev, &vpo->vpo_io)) == 0) { 116 vpo->vpo_isplus = 0; 117 device_set_desc(dev, 118 "Iomega VPI0 Parallel to SCSI interface"); 119 } else if ((error = imm_probe(dev, &vpo->vpo_io)) == 0) { 120 vpo->vpo_isplus = 1; 121 device_set_desc(dev, 122 "Iomega Matchmaker Parallel to SCSI interface"); 123 } else { 124 return (error); 125 } 126 127 return (0); 128} 129 130/* 131 * vpo_attach() 132 */ 133static int 134vpo_attach(device_t dev) 135{ 136 struct vpo_data *vpo = DEVTOSOFTC(dev); 137 struct cam_devq *devq; 138 int error; 139 140 /* low level attachment */ 141 if (vpo->vpo_isplus) { 142 if ((error = imm_attach(&vpo->vpo_io))) 143 return (error); 144 } else { 145 if ((error = vpoio_attach(&vpo->vpo_io))) 146 return (error); 147 } 148 149 /* 150 ** Now tell the generic SCSI layer 151 ** about our bus. 152 */ 153 devq = cam_simq_alloc(/*maxopenings*/1); 154 /* XXX What about low-level detach on error? */ 155 if (devq == NULL) 156 return (ENXIO); 157 158 vpo->sim = cam_sim_alloc(vpo_action, vpo_poll, "vpo", vpo, 159 device_get_unit(dev), &Giant, 160 /*untagged*/1, /*tagged*/0, devq); 161 if (vpo->sim == NULL) { 162 cam_simq_free(devq); 163 return (ENXIO); 164 } 165 166 if (xpt_bus_register(vpo->sim, dev, /*bus*/0) != CAM_SUCCESS) { 167 cam_sim_free(vpo->sim, /*free_devq*/TRUE); 168 return (ENXIO); 169 } 170 171 /* all went ok */ 172 173 vpo_cam_rescan(vpo); /* have CAM rescan the bus */ 174 175 return (0); 176} 177 178static void 179vpo_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb) 180{ 181 182 free(ccb, M_TEMP); 183} 184 185static void 186vpo_cam_rescan(struct vpo_data *vpo) 187{ 188 struct cam_path *path; 189 union ccb *ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO); 190 191 if (xpt_create_path(&path, xpt_periph, cam_sim_path(vpo->sim), 0, 0) 192 != CAM_REQ_CMP) { 193 /* A failure is benign as the user can do a manual rescan */ 194 free(ccb, M_TEMP); 195 return; 196 } 197 198 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/); 199 ccb->ccb_h.func_code = XPT_SCAN_BUS; 200 ccb->ccb_h.cbfcnp = vpo_cam_rescan_callback; 201 ccb->crcn.flags = CAM_FLAG_NONE; 202 xpt_action(ccb); 203 204 /* The scan is in progress now. */ 205} 206 207/* 208 * vpo_intr() 209 */ 210static void 211vpo_intr(struct vpo_data *vpo, struct ccb_scsiio *csio) 212{ 213 int errno; /* error in errno.h */ 214 int s; 215#ifdef VP0_DEBUG 216 int i; 217#endif 218 219 s = splcam(); 220 221 if (vpo->vpo_isplus) { 222 errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR, 223 csio->ccb_h.target_id, 224 (char *)&csio->cdb_io.cdb_bytes, csio->cdb_len, 225 (char *)csio->data_ptr, csio->dxfer_len, 226 &vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error); 227 } else { 228 errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR, 229 csio->ccb_h.target_id, 230 (char *)&csio->cdb_io.cdb_bytes, csio->cdb_len, 231 (char *)csio->data_ptr, csio->dxfer_len, 232 &vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error); 233 } 234 235#ifdef VP0_DEBUG 236 printf("vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n", 237 errno, vpo->vpo_stat, vpo->vpo_count, vpo->vpo_error); 238 239 /* dump of command */ 240 for (i=0; i<csio->cdb_len; i++) 241 printf("%x ", ((char *)&csio->cdb_io.cdb_bytes)[i]); 242 243 printf("\n"); 244#endif 245 246 if (errno) { 247 /* connection to ppbus interrupted */ 248 csio->ccb_h.status = CAM_CMD_TIMEOUT; 249 goto error; 250 } 251 252 /* if a timeout occured, no sense */ 253 if (vpo->vpo_error) { 254 if (vpo->vpo_error != VP0_ESELECT_TIMEOUT) 255 device_printf(vpo->vpo_dev, "VP0 error/timeout (%d)\n", 256 vpo->vpo_error); 257 258 csio->ccb_h.status = CAM_CMD_TIMEOUT; 259 goto error; 260 } 261 262 /* check scsi status */ 263 if (vpo->vpo_stat != SCSI_STATUS_OK) { 264 csio->scsi_status = vpo->vpo_stat; 265 266 /* check if we have to sense the drive */ 267 if ((vpo->vpo_stat & SCSI_STATUS_CHECK_COND) != 0) { 268 269 vpo->vpo_sense.cmd.opcode = REQUEST_SENSE; 270 vpo->vpo_sense.cmd.length = csio->sense_len; 271 vpo->vpo_sense.cmd.control = 0; 272 273 if (vpo->vpo_isplus) { 274 errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR, 275 csio->ccb_h.target_id, 276 (char *)&vpo->vpo_sense.cmd, 277 sizeof(vpo->vpo_sense.cmd), 278 (char *)&csio->sense_data, csio->sense_len, 279 &vpo->vpo_sense.stat, &vpo->vpo_sense.count, 280 &vpo->vpo_error); 281 } else { 282 errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR, 283 csio->ccb_h.target_id, 284 (char *)&vpo->vpo_sense.cmd, 285 sizeof(vpo->vpo_sense.cmd), 286 (char *)&csio->sense_data, csio->sense_len, 287 &vpo->vpo_sense.stat, &vpo->vpo_sense.count, 288 &vpo->vpo_error); 289 } 290 291 292#ifdef VP0_DEBUG 293 printf("(sense) vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n", 294 errno, vpo->vpo_sense.stat, vpo->vpo_sense.count, vpo->vpo_error); 295#endif 296 297 /* check sense return status */ 298 if (errno == 0 && vpo->vpo_sense.stat == SCSI_STATUS_OK) { 299 /* sense ok */ 300 csio->ccb_h.status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR; 301 csio->sense_resid = csio->sense_len - vpo->vpo_sense.count; 302 303#ifdef VP0_DEBUG 304 /* dump of sense info */ 305 printf("(sense) "); 306 for (i=0; i<vpo->vpo_sense.count; i++) 307 printf("%x ", ((char *)&csio->sense_data)[i]); 308 printf("\n"); 309#endif 310 311 } else { 312 /* sense failed */ 313 csio->ccb_h.status = CAM_AUTOSENSE_FAIL; 314 } 315 } else { 316 /* no sense */ 317 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR; 318 } 319 320 goto error; 321 } 322 323 csio->resid = csio->dxfer_len - vpo->vpo_count; 324 csio->ccb_h.status = CAM_REQ_CMP; 325 326error: 327 splx(s); 328 329 return; 330} 331 332static void 333vpo_action(struct cam_sim *sim, union ccb *ccb) 334{ 335 336 struct vpo_data *vpo = (struct vpo_data *)sim->softc; 337 338 switch (ccb->ccb_h.func_code) { 339 case XPT_SCSI_IO: 340 { 341 struct ccb_scsiio *csio; 342 343 csio = &ccb->csio; 344 345#ifdef VP0_DEBUG 346 device_printf(vpo->vpo_dev, "XPT_SCSI_IO (0x%x) request\n", 347 csio->cdb_io.cdb_bytes[0]); 348#endif 349 350 vpo_intr(vpo, csio); 351 352 xpt_done(ccb); 353 354 break; 355 } 356 case XPT_CALC_GEOMETRY: 357 { 358 struct ccb_calc_geometry *ccg; 359 360 ccg = &ccb->ccg; 361 362#ifdef VP0_DEBUG 363 device_printf(vpo->vpo_dev, "XPT_CALC_GEOMETRY (bs=%d,vs=%jd,c=%d,h=%d,spt=%d) request\n", 364 ccg->block_size, 365 (intmax_t)ccg->volume_size, 366 ccg->cylinders, 367 ccg->heads, 368 ccg->secs_per_track); 369#endif 370 371 ccg->heads = 64; 372 ccg->secs_per_track = 32; 373 ccg->cylinders = ccg->volume_size / 374 (ccg->heads * ccg->secs_per_track); 375 376 ccb->ccb_h.status = CAM_REQ_CMP; 377 xpt_done(ccb); 378 break; 379 } 380 case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 381 { 382 383#ifdef VP0_DEBUG 384 device_printf(vpo->vpo_dev, "XPT_RESET_BUS request\n"); 385#endif 386 387 if (vpo->vpo_isplus) { 388 if (imm_reset_bus(&vpo->vpo_io)) { 389 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 390 xpt_done(ccb); 391 return; 392 } 393 } else { 394 if (vpoio_reset_bus(&vpo->vpo_io)) { 395 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 396 xpt_done(ccb); 397 return; 398 } 399 } 400 401 ccb->ccb_h.status = CAM_REQ_CMP; 402 xpt_done(ccb); 403 break; 404 } 405 case XPT_PATH_INQ: /* Path routing inquiry */ 406 { 407 struct ccb_pathinq *cpi = &ccb->cpi; 408 409#ifdef VP0_DEBUG 410 device_printf(vpo->vpo_dev, "XPT_PATH_INQ request\n"); 411#endif 412 cpi->version_num = 1; /* XXX??? */ 413 cpi->hba_inquiry = 0; 414 cpi->target_sprt = 0; 415 cpi->hba_misc = 0; 416 cpi->hba_eng_cnt = 0; 417 cpi->max_target = 7; 418 cpi->max_lun = 0; 419 cpi->initiator_id = VP0_INITIATOR; 420 cpi->bus_id = sim->bus_id; 421 cpi->base_transfer_speed = 93; 422 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 423 strncpy(cpi->hba_vid, "Iomega", HBA_IDLEN); 424 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN); 425 cpi->unit_number = sim->unit_number; 426 427 cpi->ccb_h.status = CAM_REQ_CMP; 428 xpt_done(ccb); 429 break; 430 } 431 default: 432 ccb->ccb_h.status = CAM_REQ_INVALID; 433 xpt_done(ccb); 434 break; 435 } 436 437 return; 438} 439 440static void 441vpo_poll(struct cam_sim *sim) 442{ 443 444 /* The ZIP is actually always polled throw vpo_action(). */ 445} 446 447static devclass_t vpo_devclass; 448 449static device_method_t vpo_methods[] = { 450 /* device interface */ 451 DEVMETHOD(device_identify, vpo_identify), 452 DEVMETHOD(device_probe, vpo_probe), 453 DEVMETHOD(device_attach, vpo_attach), 454 455 { 0, 0 } 456}; 457 458static driver_t vpo_driver = { 459 "vpo", 460 vpo_methods, 461 sizeof(struct vpo_data), 462}; 463DRIVER_MODULE(vpo, ppbus, vpo_driver, vpo_devclass, 0, 0); 464MODULE_DEPEND(vpo, ppbus, 1, 1, 1); 465MODULE_DEPEND(vpo, cam, 1, 1, 1); 466