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