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
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
34 #include <machine/clock.h>
37 #include <cam/cam_ccb.h>
38 #include <cam/cam_sim.h>
39 #include <cam/cam_xpt_sim.h>
40 #include <cam/cam_debug.h>
42 #include <cam/scsi/scsi_all.h>
43 #include <cam/scsi/scsi_message.h>
44 #include <cam/scsi/scsi_da.h>
46 #include <sys/kernel.h>
50 #include <dev/ppbus/ppbconf.h>
51 #include <dev/ppbus/vpoio.h>
56 struct scsi_sense cmd;
62 unsigned short vpo_unit;
71 struct cam_path *path;
73 struct vpo_sense vpo_sense;
75 struct vpoio_data vpo_io; /* interface to low level functions */
78 #define DEVTOSOFTC(dev) \
79 ((struct vpo_data *)device_get_softc(dev))
81 /* cam related functions */
82 static void vpo_action(struct cam_sim *sim, union ccb *ccb);
83 static void vpo_poll(struct cam_sim *sim);
86 vpo_identify(driver_t *driver, device_t parent)
89 BUS_ADD_CHILD(parent, 0, "vpo", 0);
96 vpo_probe(device_t dev)
101 vpo = DEVTOSOFTC(dev);
102 bzero(vpo, sizeof(struct vpo_data));
104 /* vpo dependent initialisation */
105 vpo->vpo_unit = device_get_unit(dev);
107 /* low level probe */
108 vpoio_set_unit(&vpo->vpo_io, vpo->vpo_unit);
110 /* check ZIP before ZIP+ or imm_probe() will send controls to
111 * the printer or whatelse connected to the port */
112 if ((error = vpoio_probe(dev, &vpo->vpo_io)) == 0) {
115 "Iomega VPI0 Parallel to SCSI interface");
116 } else if ((error = imm_probe(dev, &vpo->vpo_io)) == 0) {
119 "Iomega Matchmaker Parallel to SCSI interface");
131 vpo_attach(device_t dev)
133 struct vpo_data *vpo = DEVTOSOFTC(dev);
134 struct cam_devq *devq;
137 /* low level attachment */
138 if (vpo->vpo_isplus) {
139 if ((error = imm_attach(&vpo->vpo_io)))
142 if ((error = vpoio_attach(&vpo->vpo_io)))
147 ** Now tell the generic SCSI layer
150 devq = cam_simq_alloc(/*maxopenings*/1);
151 /* XXX What about low-level detach on error? */
155 vpo->sim = cam_sim_alloc(vpo_action, vpo_poll, "vpo", vpo,
156 device_get_unit(dev),
157 /*untagged*/1, /*tagged*/0, devq);
158 if (vpo->sim == NULL) {
163 if (xpt_bus_register(vpo->sim, /*bus*/0) != CAM_SUCCESS) {
164 cam_sim_free(vpo->sim, /*free_devq*/TRUE);
168 if (xpt_create_path(&vpo->path, /*periph*/NULL,
169 cam_sim_path(vpo->sim), CAM_TARGET_WILDCARD,
170 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
171 xpt_bus_deregister(cam_sim_path(vpo->sim));
172 cam_sim_free(vpo->sim, /*free_devq*/TRUE);
185 vpo_intr(struct vpo_data *vpo, struct ccb_scsiio *csio)
187 int errno; /* error in errno.h */
195 if (vpo->vpo_isplus) {
196 errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
197 csio->ccb_h.target_id,
198 (char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
199 (char *)csio->data_ptr, csio->dxfer_len,
200 &vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
202 errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
203 csio->ccb_h.target_id,
204 (char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
205 (char *)csio->data_ptr, csio->dxfer_len,
206 &vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
210 printf("vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
211 errno, vpo->vpo_stat, vpo->vpo_count, vpo->vpo_error);
213 /* dump of command */
214 for (i=0; i<csio->cdb_len; i++)
215 printf("%x ", ((char *)&csio->cdb_io.cdb_bytes)[i]);
221 /* connection to ppbus interrupted */
222 csio->ccb_h.status = CAM_CMD_TIMEOUT;
226 /* if a timeout occured, no sense */
227 if (vpo->vpo_error) {
228 if (vpo->vpo_error != VP0_ESELECT_TIMEOUT)
229 printf("vpo%d: VP0 error/timeout (%d)\n",
230 vpo->vpo_unit, vpo->vpo_error);
232 csio->ccb_h.status = CAM_CMD_TIMEOUT;
236 /* check scsi status */
237 if (vpo->vpo_stat != SCSI_STATUS_OK) {
238 csio->scsi_status = vpo->vpo_stat;
240 /* check if we have to sense the drive */
241 if ((vpo->vpo_stat & SCSI_STATUS_CHECK_COND) != 0) {
243 vpo->vpo_sense.cmd.opcode = REQUEST_SENSE;
244 vpo->vpo_sense.cmd.length = csio->sense_len;
245 vpo->vpo_sense.cmd.control = 0;
247 if (vpo->vpo_isplus) {
248 errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
249 csio->ccb_h.target_id,
250 (char *)&vpo->vpo_sense.cmd,
251 sizeof(vpo->vpo_sense.cmd),
252 (char *)&csio->sense_data, csio->sense_len,
253 &vpo->vpo_sense.stat, &vpo->vpo_sense.count,
256 errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
257 csio->ccb_h.target_id,
258 (char *)&vpo->vpo_sense.cmd,
259 sizeof(vpo->vpo_sense.cmd),
260 (char *)&csio->sense_data, csio->sense_len,
261 &vpo->vpo_sense.stat, &vpo->vpo_sense.count,
267 printf("(sense) vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
268 errno, vpo->vpo_sense.stat, vpo->vpo_sense.count, vpo->vpo_error);
271 /* check sense return status */
272 if (errno == 0 && vpo->vpo_sense.stat == SCSI_STATUS_OK) {
274 csio->ccb_h.status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR;
275 csio->sense_resid = csio->sense_len - vpo->vpo_sense.count;
278 /* dump of sense info */
280 for (i=0; i<vpo->vpo_sense.count; i++)
281 printf("%x ", ((char *)&csio->sense_data)[i]);
287 csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
291 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
297 csio->resid = csio->dxfer_len - vpo->vpo_count;
298 csio->ccb_h.status = CAM_REQ_CMP;
307 vpo_action(struct cam_sim *sim, union ccb *ccb)
310 struct vpo_data *vpo = (struct vpo_data *)sim->softc;
312 switch (ccb->ccb_h.func_code) {
315 struct ccb_scsiio *csio;
320 printf("vpo%d: XPT_SCSI_IO (0x%x) request\n",
321 vpo->vpo_unit, csio->cdb_io.cdb_bytes[0]);
330 case XPT_CALC_GEOMETRY:
332 struct ccb_calc_geometry *ccg;
337 printf("vpo%d: XPT_CALC_GEOMETRY (bs=%d,vs=%d,c=%d,h=%d,spt=%d) request\n",
343 ccg->secs_per_track);
347 ccg->secs_per_track = 32;
348 ccg->cylinders = ccg->volume_size /
349 (ccg->heads * ccg->secs_per_track);
351 ccb->ccb_h.status = CAM_REQ_CMP;
355 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
359 printf("vpo%d: XPT_RESET_BUS request\n", vpo->vpo_unit);
362 if (vpo->vpo_isplus) {
363 if (imm_reset_bus(&vpo->vpo_io)) {
364 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
369 if (vpoio_reset_bus(&vpo->vpo_io)) {
370 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
376 ccb->ccb_h.status = CAM_REQ_CMP;
380 case XPT_PATH_INQ: /* Path routing inquiry */
382 struct ccb_pathinq *cpi = &ccb->cpi;
385 printf("vpo%d: XPT_PATH_INQ request\n", vpo->vpo_unit);
387 cpi->version_num = 1; /* XXX??? */
388 cpi->hba_inquiry = 0;
389 cpi->target_sprt = 0;
391 cpi->hba_eng_cnt = 0;
394 cpi->initiator_id = VP0_INITIATOR;
395 cpi->bus_id = sim->bus_id;
396 cpi->base_transfer_speed = 93;
397 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
398 strncpy(cpi->hba_vid, "Iomega", HBA_IDLEN);
399 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
400 cpi->unit_number = sim->unit_number;
402 cpi->ccb_h.status = CAM_REQ_CMP;
407 ccb->ccb_h.status = CAM_REQ_INVALID;
416 vpo_poll(struct cam_sim *sim)
418 /* The ZIP is actually always polled throw vpo_action() */
422 static devclass_t vpo_devclass;
424 static device_method_t vpo_methods[] = {
425 /* device interface */
426 DEVMETHOD(device_identify, vpo_identify),
427 DEVMETHOD(device_probe, vpo_probe),
428 DEVMETHOD(device_attach, vpo_attach),
433 static driver_t vpo_driver = {
436 sizeof(struct vpo_data),
438 DRIVER_MODULE(vpo, ppbus, vpo_driver, vpo_devclass, 0, 0);