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
26 * $FreeBSD: src/sys/dev/ppbus/vpo.c,v 1.20.2.1 2000/05/07 21:08:18 n_hibma Exp $
27 * $DragonFly: src/sys/dev/disk/vpo/vpo.c,v 1.6 2005/10/28 03:25:43 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
34 #include <sys/malloc.h>
35 #include <sys/devicestat.h> /* for struct devstat */
36 #include <sys/thread2.h> /* for crit_*() */
38 #include <machine/clock.h>
40 #include <bus/cam/cam.h>
41 #include <bus/cam/cam_ccb.h>
42 #include <bus/cam/cam_sim.h>
43 #include <bus/cam/cam_xpt_sim.h>
44 #include <bus/cam/cam_debug.h>
45 #include <bus/cam/cam_periph.h>
47 #include <bus/cam/scsi/scsi_all.h>
48 #include <bus/cam/scsi/scsi_message.h>
49 #include <bus/cam/scsi/scsi_da.h>
51 #include <sys/kernel.h>
55 #include <bus/ppbus/ppbconf.h>
61 struct scsi_sense cmd;
67 unsigned short vpo_unit;
77 struct vpo_sense vpo_sense;
79 struct vpoio_data vpo_io; /* interface to low level functions */
82 #define DEVTOSOFTC(dev) \
83 ((struct vpo_data *)device_get_softc(dev))
85 /* cam related functions */
86 static void vpo_action(struct cam_sim *sim, union ccb *ccb);
87 static void vpo_poll(struct cam_sim *sim);
88 static void vpo_cam_rescan_callback(struct cam_periph *periph,
90 static void vpo_cam_rescan(struct vpo_data *vpo);
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 cam_simq_release(devq);
159 if (vpo->sim == NULL) {
163 if (xpt_bus_register(vpo->sim, /*bus*/0) != CAM_SUCCESS) {
164 cam_sim_free(vpo->sim);
170 vpo_cam_rescan(vpo); /* have CAM rescan the bus */
176 vpo_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
182 vpo_cam_rescan(struct vpo_data *vpo)
184 struct cam_path *path;
185 union ccb *ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK);
187 bzero(ccb, sizeof(union ccb));
189 if (xpt_create_path(&path, xpt_periph, cam_sim_path(vpo->sim), 0, 0)
191 /* A failure is benign as the user can do a manual rescan */
195 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
196 ccb->ccb_h.func_code = XPT_SCAN_BUS;
197 ccb->ccb_h.cbfcnp = vpo_cam_rescan_callback;
198 ccb->crcn.flags = CAM_FLAG_NONE;
201 /* The scan is in progress now. */
208 vpo_intr(struct vpo_data *vpo, struct ccb_scsiio *csio)
210 int errno; /* error in errno.h */
217 if (vpo->vpo_isplus) {
218 errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
219 csio->ccb_h.target_id,
220 (char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
221 (char *)csio->data_ptr, csio->dxfer_len,
222 &vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
224 errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
225 csio->ccb_h.target_id,
226 (char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
227 (char *)csio->data_ptr, csio->dxfer_len,
228 &vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
232 printf("vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
233 errno, vpo->vpo_stat, vpo->vpo_count, vpo->vpo_error);
235 /* dump of command */
236 for (i=0; i<csio->cdb_len; i++)
237 printf("%x ", ((char *)&csio->cdb_io.cdb_bytes)[i]);
243 /* connection to ppbus interrupted */
244 csio->ccb_h.status = CAM_CMD_TIMEOUT;
248 /* if a timeout occured, no sense */
249 if (vpo->vpo_error) {
250 if (vpo->vpo_error != VP0_ESELECT_TIMEOUT)
251 printf("vpo%d: VP0 error/timeout (%d)\n",
252 vpo->vpo_unit, vpo->vpo_error);
254 csio->ccb_h.status = CAM_CMD_TIMEOUT;
258 /* check scsi status */
259 if (vpo->vpo_stat != SCSI_STATUS_OK) {
260 csio->scsi_status = vpo->vpo_stat;
262 /* check if we have to sense the drive */
263 if ((vpo->vpo_stat & SCSI_STATUS_CHECK_COND) != 0) {
265 vpo->vpo_sense.cmd.opcode = REQUEST_SENSE;
266 vpo->vpo_sense.cmd.length = csio->sense_len;
267 vpo->vpo_sense.cmd.control = 0;
269 if (vpo->vpo_isplus) {
270 errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
271 csio->ccb_h.target_id,
272 (char *)&vpo->vpo_sense.cmd,
273 sizeof(vpo->vpo_sense.cmd),
274 (char *)&csio->sense_data, csio->sense_len,
275 &vpo->vpo_sense.stat, &vpo->vpo_sense.count,
278 errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
279 csio->ccb_h.target_id,
280 (char *)&vpo->vpo_sense.cmd,
281 sizeof(vpo->vpo_sense.cmd),
282 (char *)&csio->sense_data, csio->sense_len,
283 &vpo->vpo_sense.stat, &vpo->vpo_sense.count,
289 printf("(sense) vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
290 errno, vpo->vpo_sense.stat, vpo->vpo_sense.count, vpo->vpo_error);
293 /* check sense return status */
294 if (errno == 0 && vpo->vpo_sense.stat == SCSI_STATUS_OK) {
296 csio->ccb_h.status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR;
297 csio->sense_resid = csio->sense_len - vpo->vpo_sense.count;
300 /* dump of sense info */
302 for (i=0; i<vpo->vpo_sense.count; i++)
303 printf("%x ", ((char *)&csio->sense_data)[i]);
309 csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
313 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
319 csio->resid = csio->dxfer_len - vpo->vpo_count;
320 csio->ccb_h.status = CAM_REQ_CMP;
329 vpo_action(struct cam_sim *sim, union ccb *ccb)
332 struct vpo_data *vpo = (struct vpo_data *)sim->softc;
334 switch (ccb->ccb_h.func_code) {
337 struct ccb_scsiio *csio;
342 printf("vpo%d: XPT_SCSI_IO (0x%x) request\n",
343 vpo->vpo_unit, csio->cdb_io.cdb_bytes[0]);
352 case XPT_CALC_GEOMETRY:
354 struct ccb_calc_geometry *ccg;
359 printf("vpo%d: XPT_CALC_GEOMETRY (bs=%d,vs=%d,c=%d,h=%d,spt=%d) request\n",
365 ccg->secs_per_track);
369 ccg->secs_per_track = 32;
370 ccg->cylinders = ccg->volume_size /
371 (ccg->heads * ccg->secs_per_track);
373 ccb->ccb_h.status = CAM_REQ_CMP;
377 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
381 printf("vpo%d: XPT_RESET_BUS request\n", vpo->vpo_unit);
384 if (vpo->vpo_isplus) {
385 if (imm_reset_bus(&vpo->vpo_io)) {
386 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
391 if (vpoio_reset_bus(&vpo->vpo_io)) {
392 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
398 ccb->ccb_h.status = CAM_REQ_CMP;
402 case XPT_PATH_INQ: /* Path routing inquiry */
404 struct ccb_pathinq *cpi = &ccb->cpi;
407 printf("vpo%d: XPT_PATH_INQ request\n", vpo->vpo_unit);
409 cpi->version_num = 1; /* XXX??? */
410 cpi->hba_inquiry = 0;
411 cpi->target_sprt = 0;
413 cpi->hba_eng_cnt = 0;
416 cpi->initiator_id = VP0_INITIATOR;
417 cpi->bus_id = sim->bus_id;
418 cpi->base_transfer_speed = 93;
419 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
420 strncpy(cpi->hba_vid, "Iomega", HBA_IDLEN);
421 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
422 cpi->unit_number = sim->unit_number;
424 cpi->ccb_h.status = CAM_REQ_CMP;
429 ccb->ccb_h.status = CAM_REQ_INVALID;
438 vpo_poll(struct cam_sim *sim)
440 /* The ZIP is actually always polled throw vpo_action() */
445 * Always create a "vpo" device under ppbus. Use device_identify to
446 * create the static entry for any attached ppbus.
448 static devclass_t vpo_devclass;
450 static device_method_t vpo_methods[] = {
451 /* device interface */
452 DEVMETHOD(device_identify, bus_generic_identify),
453 DEVMETHOD(device_probe, vpo_probe),
454 DEVMETHOD(device_attach, vpo_attach),
458 static driver_t vpo_driver = {
461 sizeof(struct vpo_data),
463 DRIVER_MODULE(vpo, ppbus, vpo_driver, vpo_devclass, 0, 0);