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;
75 struct vpo_sense vpo_sense;
77 struct vpoio_data vpo_io; /* interface to low level functions */
80 #define DEVTOSOFTC(dev) \
81 ((struct vpo_data *)device_get_softc(dev))
83 /* cam related functions */
84 static void vpo_action(struct cam_sim *sim, union ccb *ccb);
85 static void vpo_poll(struct cam_sim *sim);
88 vpo_identify(driver_t *driver, device_t parent)
93 dev = device_find_child(parent, "vpo", -1);
95 BUS_ADD_CHILD(parent, 0, "vpo", -1);
102 vpo_probe(device_t dev)
104 device_t ppbus = device_get_parent(dev);
105 struct vpo_data *vpo;
108 vpo = DEVTOSOFTC(dev);
111 /* check ZIP before ZIP+ or imm_probe() will send controls to
112 * the printer or whatelse connected to the port */
114 if ((error = vpoio_probe(dev, &vpo->vpo_io)) == 0) {
117 "Iomega VPI0 Parallel to SCSI interface");
118 } else if ((error = imm_probe(dev, &vpo->vpo_io)) == 0) {
121 "Iomega Matchmaker Parallel to SCSI interface");
135 vpo_attach(device_t dev)
137 struct vpo_data *vpo = DEVTOSOFTC(dev);
138 device_t ppbus = device_get_parent(dev);
139 struct ppb_data *ppb = device_get_softc(ppbus); /* XXX: layering */
140 struct cam_devq *devq;
143 /* low level attachment */
144 if (vpo->vpo_isplus) {
145 if ((error = imm_attach(&vpo->vpo_io)))
148 if ((error = vpoio_attach(&vpo->vpo_io)))
153 ** Now tell the generic SCSI layer
156 devq = cam_simq_alloc(/*maxopenings*/1);
157 /* XXX What about low-level detach on error? */
161 vpo->sim = cam_sim_alloc(vpo_action, vpo_poll, "vpo", vpo,
162 device_get_unit(dev), ppb->ppc_lock,
163 /*untagged*/1, /*tagged*/0, devq);
164 if (vpo->sim == NULL) {
170 if (xpt_bus_register(vpo->sim, dev, /*bus*/0) != CAM_SUCCESS) {
171 cam_sim_free(vpo->sim, /*free_devq*/TRUE);
184 vpo_intr(struct vpo_data *vpo, struct ccb_scsiio *csio)
186 int errno; /* error in errno.h */
192 ptr = scsiio_cdb_ptr(csio);
193 if (vpo->vpo_isplus) {
194 errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
195 csio->ccb_h.target_id,
197 (char *)csio->data_ptr, csio->dxfer_len,
198 &vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
200 errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
201 csio->ccb_h.target_id,
203 (char *)csio->data_ptr, csio->dxfer_len,
204 &vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
208 printf("vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
209 errno, vpo->vpo_stat, vpo->vpo_count, vpo->vpo_error);
211 /* dump of command */
212 for (i=0; i<csio->cdb_len; i++)
213 printf("%x ", ((char *)ptr)[i]);
219 /* connection to ppbus interrupted */
220 csio->ccb_h.status = CAM_CMD_TIMEOUT;
224 /* if a timeout occurred, no sense */
225 if (vpo->vpo_error) {
226 if (vpo->vpo_error != VP0_ESELECT_TIMEOUT)
227 device_printf(vpo->vpo_dev, "VP0 error/timeout (%d)\n",
230 csio->ccb_h.status = CAM_CMD_TIMEOUT;
234 /* check scsi status */
235 if (vpo->vpo_stat != SCSI_STATUS_OK) {
236 csio->scsi_status = vpo->vpo_stat;
238 /* check if we have to sense the drive */
239 if ((vpo->vpo_stat & SCSI_STATUS_CHECK_COND) != 0) {
241 vpo->vpo_sense.cmd.opcode = REQUEST_SENSE;
242 vpo->vpo_sense.cmd.length = csio->sense_len;
243 vpo->vpo_sense.cmd.control = 0;
245 if (vpo->vpo_isplus) {
246 errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
247 csio->ccb_h.target_id,
248 (char *)&vpo->vpo_sense.cmd,
249 sizeof(vpo->vpo_sense.cmd),
250 (char *)&csio->sense_data, csio->sense_len,
251 &vpo->vpo_sense.stat, &vpo->vpo_sense.count,
254 errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
255 csio->ccb_h.target_id,
256 (char *)&vpo->vpo_sense.cmd,
257 sizeof(vpo->vpo_sense.cmd),
258 (char *)&csio->sense_data, csio->sense_len,
259 &vpo->vpo_sense.stat, &vpo->vpo_sense.count,
265 printf("(sense) vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
266 errno, vpo->vpo_sense.stat, vpo->vpo_sense.count, vpo->vpo_error);
269 /* check sense return status */
270 if (errno == 0 && vpo->vpo_sense.stat == SCSI_STATUS_OK) {
272 csio->ccb_h.status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR;
273 csio->sense_resid = csio->sense_len - vpo->vpo_sense.count;
276 /* dump of sense info */
278 for (i=0; i<vpo->vpo_sense.count; i++)
279 printf("%x ", ((char *)&csio->sense_data)[i]);
285 csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
289 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
295 csio->resid = csio->dxfer_len - vpo->vpo_count;
296 csio->ccb_h.status = CAM_REQ_CMP;
300 vpo_action(struct cam_sim *sim, union ccb *ccb)
302 struct vpo_data *vpo = (struct vpo_data *)sim->softc;
304 ppb_assert_locked(device_get_parent(vpo->vpo_dev));
305 switch (ccb->ccb_h.func_code) {
308 struct ccb_scsiio *csio;
312 if (ccb->ccb_h.flags & CAM_CDB_PHYS) {
313 ccb->ccb_h.status = CAM_REQ_INVALID;
318 device_printf(vpo->vpo_dev, "XPT_SCSI_IO (0x%x) request\n",
319 *scsiio_cdb_ptr(csio));
327 case XPT_CALC_GEOMETRY:
329 struct ccb_calc_geometry *ccg;
334 device_printf(vpo->vpo_dev, "XPT_CALC_GEOMETRY (bs=%d,vs=%jd,c=%d,h=%d,spt=%d) request\n",
336 (intmax_t)ccg->volume_size,
339 ccg->secs_per_track);
343 ccg->secs_per_track = 32;
344 ccg->cylinders = ccg->volume_size /
345 (ccg->heads * ccg->secs_per_track);
347 ccb->ccb_h.status = CAM_REQ_CMP;
351 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
355 device_printf(vpo->vpo_dev, "XPT_RESET_BUS request\n");
358 if (vpo->vpo_isplus) {
359 if (imm_reset_bus(&vpo->vpo_io)) {
360 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
365 if (vpoio_reset_bus(&vpo->vpo_io)) {
366 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
372 ccb->ccb_h.status = CAM_REQ_CMP;
376 case XPT_PATH_INQ: /* Path routing inquiry */
378 struct ccb_pathinq *cpi = &ccb->cpi;
381 device_printf(vpo->vpo_dev, "XPT_PATH_INQ request\n");
383 cpi->version_num = 1; /* XXX??? */
384 cpi->hba_inquiry = 0;
385 cpi->target_sprt = 0;
387 cpi->hba_eng_cnt = 0;
390 cpi->initiator_id = VP0_INITIATOR;
391 cpi->bus_id = sim->bus_id;
392 cpi->base_transfer_speed = 93;
393 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
394 strlcpy(cpi->hba_vid, "Iomega", HBA_IDLEN);
395 strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
396 cpi->unit_number = sim->unit_number;
397 cpi->transport = XPORT_PPB;
398 cpi->transport_version = 0;
400 cpi->ccb_h.status = CAM_REQ_CMP;
405 ccb->ccb_h.status = CAM_REQ_INVALID;
414 vpo_poll(struct cam_sim *sim)
417 /* The ZIP is actually always polled throw vpo_action(). */
420 static devclass_t vpo_devclass;
422 static device_method_t vpo_methods[] = {
423 /* device interface */
424 DEVMETHOD(device_identify, vpo_identify),
425 DEVMETHOD(device_probe, vpo_probe),
426 DEVMETHOD(device_attach, vpo_attach),
431 static driver_t vpo_driver = {
434 sizeof(struct vpo_data),
436 DRIVER_MODULE(vpo, ppbus, vpo_driver, vpo_devclass, 0, 0);
437 MODULE_DEPEND(vpo, ppbus, 1, 1, 1);
438 MODULE_DEPEND(vpo, cam, 1, 1, 1);