2 * Copyright (c) 1997, 1998, 1999 Nicolas Souchu
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 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/malloc.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>
46 #include <cam/scsi/scsi_all.h>
47 #include <cam/scsi/scsi_message.h>
48 #include <cam/scsi/scsi_da.h>
50 #include <sys/kernel.h>
54 #include <dev/ppbus/ppbconf.h>
55 #include <dev/ppbus/vpoio.h>
60 struct scsi_sense cmd;
66 unsigned short vpo_unit;
76 struct vpo_sense vpo_sense;
78 struct vpoio_data vpo_io; /* interface to low level functions */
81 #define DEVTOSOFTC(dev) \
82 ((struct vpo_data *)device_get_softc(dev))
84 /* cam related functions */
85 static void vpo_action(struct cam_sim *sim, union ccb *ccb);
86 static void vpo_poll(struct cam_sim *sim);
87 static void vpo_cam_rescan_callback(struct cam_periph *periph,
89 static void vpo_cam_rescan(struct vpo_data *vpo);
92 vpo_identify(driver_t *driver, device_t parent)
97 dev = device_find_child(parent, "vpo", 0);
99 BUS_ADD_CHILD(parent, 0, "vpo", -1);
106 vpo_probe(device_t dev)
108 struct vpo_data *vpo;
111 vpo = DEVTOSOFTC(dev);
113 /* vpo dependent initialisation */
114 vpo->vpo_unit = device_get_unit(dev);
116 /* low level probe */
117 vpoio_set_unit(&vpo->vpo_io, vpo->vpo_unit);
119 /* check ZIP before ZIP+ or imm_probe() will send controls to
120 * the printer or whatelse connected to the port */
121 if ((error = vpoio_probe(dev, &vpo->vpo_io)) == 0) {
124 "Iomega VPI0 Parallel to SCSI interface");
125 } else if ((error = imm_probe(dev, &vpo->vpo_io)) == 0) {
128 "Iomega Matchmaker Parallel to SCSI interface");
140 vpo_attach(device_t dev)
142 struct vpo_data *vpo = DEVTOSOFTC(dev);
143 struct cam_devq *devq;
146 /* low level attachment */
147 if (vpo->vpo_isplus) {
148 if ((error = imm_attach(&vpo->vpo_io)))
151 if ((error = vpoio_attach(&vpo->vpo_io)))
156 ** Now tell the generic SCSI layer
159 devq = cam_simq_alloc(/*maxopenings*/1);
160 /* XXX What about low-level detach on error? */
164 vpo->sim = cam_sim_alloc(vpo_action, vpo_poll, "vpo", vpo,
165 device_get_unit(dev), &Giant,
166 /*untagged*/1, /*tagged*/0, devq);
167 if (vpo->sim == NULL) {
172 if (xpt_bus_register(vpo->sim, dev, /*bus*/0) != CAM_SUCCESS) {
173 cam_sim_free(vpo->sim, /*free_devq*/TRUE);
179 vpo_cam_rescan(vpo); /* have CAM rescan the bus */
185 vpo_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
191 vpo_cam_rescan(struct vpo_data *vpo)
193 struct cam_path *path;
194 union ccb *ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO);
196 if (xpt_create_path(&path, xpt_periph, cam_sim_path(vpo->sim), 0, 0)
198 /* A failure is benign as the user can do a manual rescan */
203 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
204 ccb->ccb_h.func_code = XPT_SCAN_BUS;
205 ccb->ccb_h.cbfcnp = vpo_cam_rescan_callback;
206 ccb->crcn.flags = CAM_FLAG_NONE;
209 /* The scan is in progress now. */
216 vpo_intr(struct vpo_data *vpo, struct ccb_scsiio *csio)
218 int errno; /* error in errno.h */
226 if (vpo->vpo_isplus) {
227 errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
228 csio->ccb_h.target_id,
229 (char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
230 (char *)csio->data_ptr, csio->dxfer_len,
231 &vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
233 errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
234 csio->ccb_h.target_id,
235 (char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
236 (char *)csio->data_ptr, csio->dxfer_len,
237 &vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
241 printf("vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
242 errno, vpo->vpo_stat, vpo->vpo_count, vpo->vpo_error);
244 /* dump of command */
245 for (i=0; i<csio->cdb_len; i++)
246 printf("%x ", ((char *)&csio->cdb_io.cdb_bytes)[i]);
252 /* connection to ppbus interrupted */
253 csio->ccb_h.status = CAM_CMD_TIMEOUT;
257 /* if a timeout occured, no sense */
258 if (vpo->vpo_error) {
259 if (vpo->vpo_error != VP0_ESELECT_TIMEOUT)
260 printf("vpo%d: VP0 error/timeout (%d)\n",
261 vpo->vpo_unit, vpo->vpo_error);
263 csio->ccb_h.status = CAM_CMD_TIMEOUT;
267 /* check scsi status */
268 if (vpo->vpo_stat != SCSI_STATUS_OK) {
269 csio->scsi_status = vpo->vpo_stat;
271 /* check if we have to sense the drive */
272 if ((vpo->vpo_stat & SCSI_STATUS_CHECK_COND) != 0) {
274 vpo->vpo_sense.cmd.opcode = REQUEST_SENSE;
275 vpo->vpo_sense.cmd.length = csio->sense_len;
276 vpo->vpo_sense.cmd.control = 0;
278 if (vpo->vpo_isplus) {
279 errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
280 csio->ccb_h.target_id,
281 (char *)&vpo->vpo_sense.cmd,
282 sizeof(vpo->vpo_sense.cmd),
283 (char *)&csio->sense_data, csio->sense_len,
284 &vpo->vpo_sense.stat, &vpo->vpo_sense.count,
287 errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
288 csio->ccb_h.target_id,
289 (char *)&vpo->vpo_sense.cmd,
290 sizeof(vpo->vpo_sense.cmd),
291 (char *)&csio->sense_data, csio->sense_len,
292 &vpo->vpo_sense.stat, &vpo->vpo_sense.count,
298 printf("(sense) vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
299 errno, vpo->vpo_sense.stat, vpo->vpo_sense.count, vpo->vpo_error);
302 /* check sense return status */
303 if (errno == 0 && vpo->vpo_sense.stat == SCSI_STATUS_OK) {
305 csio->ccb_h.status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR;
306 csio->sense_resid = csio->sense_len - vpo->vpo_sense.count;
309 /* dump of sense info */
311 for (i=0; i<vpo->vpo_sense.count; i++)
312 printf("%x ", ((char *)&csio->sense_data)[i]);
318 csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
322 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
328 csio->resid = csio->dxfer_len - vpo->vpo_count;
329 csio->ccb_h.status = CAM_REQ_CMP;
338 vpo_action(struct cam_sim *sim, union ccb *ccb)
341 struct vpo_data *vpo = (struct vpo_data *)sim->softc;
343 switch (ccb->ccb_h.func_code) {
346 struct ccb_scsiio *csio;
351 printf("vpo%d: XPT_SCSI_IO (0x%x) request\n",
352 vpo->vpo_unit, csio->cdb_io.cdb_bytes[0]);
361 case XPT_CALC_GEOMETRY:
363 struct ccb_calc_geometry *ccg;
368 printf("vpo%d: XPT_CALC_GEOMETRY (bs=%d,vs=%jd,c=%d,h=%d,spt=%d) request\n",
371 (intmax_t)ccg->volume_size,
374 ccg->secs_per_track);
378 ccg->secs_per_track = 32;
379 ccg->cylinders = ccg->volume_size /
380 (ccg->heads * ccg->secs_per_track);
382 ccb->ccb_h.status = CAM_REQ_CMP;
386 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
390 printf("vpo%d: XPT_RESET_BUS request\n", vpo->vpo_unit);
393 if (vpo->vpo_isplus) {
394 if (imm_reset_bus(&vpo->vpo_io)) {
395 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
400 if (vpoio_reset_bus(&vpo->vpo_io)) {
401 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
407 ccb->ccb_h.status = CAM_REQ_CMP;
411 case XPT_PATH_INQ: /* Path routing inquiry */
413 struct ccb_pathinq *cpi = &ccb->cpi;
416 printf("vpo%d: XPT_PATH_INQ request\n", vpo->vpo_unit);
418 cpi->version_num = 1; /* XXX??? */
419 cpi->hba_inquiry = 0;
420 cpi->target_sprt = 0;
422 cpi->hba_eng_cnt = 0;
425 cpi->initiator_id = VP0_INITIATOR;
426 cpi->bus_id = sim->bus_id;
427 cpi->base_transfer_speed = 93;
428 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
429 strncpy(cpi->hba_vid, "Iomega", HBA_IDLEN);
430 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
431 cpi->unit_number = sim->unit_number;
433 cpi->ccb_h.status = CAM_REQ_CMP;
438 ccb->ccb_h.status = CAM_REQ_INVALID;
447 vpo_poll(struct cam_sim *sim)
449 /* The ZIP is actually always polled throw vpo_action() */
453 static devclass_t vpo_devclass;
455 static device_method_t vpo_methods[] = {
456 /* device interface */
457 DEVMETHOD(device_identify, vpo_identify),
458 DEVMETHOD(device_probe, vpo_probe),
459 DEVMETHOD(device_attach, vpo_attach),
464 static driver_t vpo_driver = {
467 sizeof(struct vpo_data),
469 DRIVER_MODULE(vpo, ppbus, vpo_driver, vpo_devclass, 0, 0);
470 MODULE_DEPEND(vpo, ppbus, 1, 1, 1);
471 MODULE_DEPEND(vpo, cam, 1, 1, 1);