vpo.c revision 114451
128219Smsmith/*-
255939Snsouch * Copyright (c) 1997, 1998, 1999 Nicolas Souchu
328219Smsmith * All rights reserved.
428219Smsmith *
528219Smsmith * Redistribution and use in source and binary forms, with or without
628219Smsmith * modification, are permitted provided that the following conditions
728219Smsmith * are met:
828219Smsmith * 1. Redistributions of source code must retain the above copyright
928219Smsmith *    notice, this list of conditions and the following disclaimer.
1028219Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1128219Smsmith *    notice, this list of conditions and the following disclaimer in the
1228219Smsmith *    documentation and/or other materials provided with the distribution.
1328219Smsmith *
1428219Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1528219Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1628219Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1728219Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1828219Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1928219Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2028219Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2128219Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2228219Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2328219Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2428219Smsmith * SUCH DAMAGE.
2528219Smsmith *
2650477Speter * $FreeBSD: head/sys/dev/ppbus/vpo.c 114451 2003-05-01 19:46:21Z jhb $
2728219Smsmith */
2828219Smsmith
2928219Smsmith#include <sys/param.h>
3028219Smsmith#include <sys/systm.h>
3155939Snsouch#include <sys/module.h>
3255939Snsouch#include <sys/bus.h>
3359018Sn_hibma#include <sys/malloc.h>
3428219Smsmith
3539520Snsouch#include <cam/cam.h>
3639520Snsouch#include <cam/cam_ccb.h>
3739520Snsouch#include <cam/cam_sim.h>
3839520Snsouch#include <cam/cam_xpt_sim.h>
3939520Snsouch#include <cam/cam_debug.h>
4059018Sn_hibma#include <cam/cam_periph.h>
4139520Snsouch
4239520Snsouch#include <cam/scsi/scsi_all.h>
4339520Snsouch#include <cam/scsi/scsi_message.h>
4439520Snsouch#include <cam/scsi/scsi_da.h>
4539520Snsouch
4628219Smsmith#include <sys/kernel.h>
4728219Smsmith
4842475Snsouch#include "opt_vpo.h"
4942475Snsouch
5028219Smsmith#include <dev/ppbus/ppbconf.h>
5138061Smsmith#include <dev/ppbus/vpoio.h>
5228219Smsmith
5355939Snsouch#include "ppbus_if.h"
5455939Snsouch
5538061Smsmithstruct vpo_sense {
5638061Smsmith	struct scsi_sense cmd;
5738061Smsmith	unsigned int stat;
5838061Smsmith	unsigned int count;
5938061Smsmith};
6028219Smsmith
6138061Smsmithstruct vpo_data {
6238061Smsmith	unsigned short vpo_unit;
6328219Smsmith
6438061Smsmith	int vpo_stat;
6538061Smsmith	int vpo_count;
6638061Smsmith	int vpo_error;
6728219Smsmith
6839134Snsouch	int vpo_isplus;
6939134Snsouch
7039520Snsouch	struct cam_sim  *sim;
7139520Snsouch
7238061Smsmith	struct vpo_sense vpo_sense;
7338061Smsmith
7438061Smsmith	struct vpoio_data vpo_io;	/* interface to low level functions */
7538061Smsmith};
7638061Smsmith
7755939Snsouch#define DEVTOSOFTC(dev) \
7855939Snsouch	((struct vpo_data *)device_get_softc(dev))
7928219Smsmith
8055939Snsouch/* cam related functions */
8155939Snsouchstatic void	vpo_action(struct cam_sim *sim, union ccb *ccb);
8255939Snsouchstatic void	vpo_poll(struct cam_sim *sim);
8359018Sn_hibmastatic void	vpo_cam_rescan_callback(struct cam_periph *periph,
8459018Sn_hibma					union ccb *ccb);
8559018Sn_hibmastatic void	vpo_cam_rescan(struct vpo_data *vpo);
8655939Snsouch
8756455Speterstatic void
8856455Spetervpo_identify(driver_t *driver, device_t parent)
8956455Speter{
9056455Speter
9194154Sticso	BUS_ADD_CHILD(parent, 0, "vpo", -1);
9256455Speter}
9356455Speter
9428219Smsmith/*
9555939Snsouch * vpo_probe()
9628219Smsmith */
9755939Snsouchstatic int
9855939Snsouchvpo_probe(device_t dev)
9928219Smsmith{
10028219Smsmith	struct vpo_data *vpo;
10155939Snsouch	int error;
10228219Smsmith
10355939Snsouch	vpo = DEVTOSOFTC(dev);
10428219Smsmith	bzero(vpo, sizeof(struct vpo_data));
10528219Smsmith
10628219Smsmith	/* vpo dependent initialisation */
10755939Snsouch	vpo->vpo_unit = device_get_unit(dev);
10828219Smsmith
10938061Smsmith	/* low level probe */
11038061Smsmith	vpoio_set_unit(&vpo->vpo_io, vpo->vpo_unit);
11138061Smsmith
11239520Snsouch	/* check ZIP before ZIP+ or imm_probe() will send controls to
11339520Snsouch	 * the printer or whatelse connected to the port */
11455939Snsouch	if ((error = vpoio_probe(dev, &vpo->vpo_io)) == 0) {
11539520Snsouch		vpo->vpo_isplus = 0;
11655939Snsouch		device_set_desc(dev,
11755939Snsouch				"Iomega VPI0 Parallel to SCSI interface");
11855939Snsouch	} else if ((error = imm_probe(dev, &vpo->vpo_io)) == 0) {
11939134Snsouch		vpo->vpo_isplus = 1;
12055939Snsouch		device_set_desc(dev,
12155939Snsouch				"Iomega Matchmaker Parallel to SCSI interface");
12239520Snsouch	} else {
12355939Snsouch		return (error);
12428219Smsmith	}
12528219Smsmith
12655939Snsouch	return (0);
12728219Smsmith}
12828219Smsmith
12928219Smsmith/*
13055939Snsouch * vpo_attach()
13128219Smsmith */
13228219Smsmithstatic int
13355939Snsouchvpo_attach(device_t dev)
13428219Smsmith{
13555939Snsouch	struct vpo_data *vpo = DEVTOSOFTC(dev);
13639520Snsouch	struct cam_devq *devq;
13755939Snsouch	int error;
13828219Smsmith
13938061Smsmith	/* low level attachment */
14039134Snsouch	if (vpo->vpo_isplus) {
14155939Snsouch		if ((error = imm_attach(&vpo->vpo_io)))
14255939Snsouch			return (error);
14339134Snsouch	} else {
14455939Snsouch		if ((error = vpoio_attach(&vpo->vpo_io)))
14555939Snsouch			return (error);
14639134Snsouch	}
14738061Smsmith
14828219Smsmith	/*
14939520Snsouch	**	Now tell the generic SCSI layer
15039520Snsouch	**	about our bus.
15139520Snsouch	*/
15239520Snsouch	devq = cam_simq_alloc(/*maxopenings*/1);
15339520Snsouch	/* XXX What about low-level detach on error? */
15439520Snsouch	if (devq == NULL)
15555939Snsouch		return (ENXIO);
15628219Smsmith
15755939Snsouch	vpo->sim = cam_sim_alloc(vpo_action, vpo_poll, "vpo", vpo,
15855939Snsouch				 device_get_unit(dev),
15939520Snsouch				 /*untagged*/1, /*tagged*/0, devq);
16039520Snsouch	if (vpo->sim == NULL) {
16139520Snsouch		cam_simq_free(devq);
16255939Snsouch		return (ENXIO);
16339520Snsouch	}
16438061Smsmith
16539520Snsouch	if (xpt_bus_register(vpo->sim, /*bus*/0) != CAM_SUCCESS) {
16639520Snsouch		cam_sim_free(vpo->sim, /*free_devq*/TRUE);
16755939Snsouch		return (ENXIO);
16839520Snsouch	}
16928219Smsmith
17039520Snsouch	/* all went ok */
17128219Smsmith
17259018Sn_hibma	vpo_cam_rescan(vpo);	/* have CAM rescan the bus */
17359018Sn_hibma
17455939Snsouch	return (0);
17528219Smsmith}
17628219Smsmith
17759018Sn_hibmastatic void
17859018Sn_hibmavpo_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
17959018Sn_hibma{
18059018Sn_hibma        free(ccb, M_TEMP);
18159018Sn_hibma}
18259018Sn_hibma
18359018Sn_hibmastatic void
18459018Sn_hibmavpo_cam_rescan(struct vpo_data *vpo)
18559018Sn_hibma{
18659018Sn_hibma        struct cam_path *path;
187111119Simp        union ccb *ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO);
18859018Sn_hibma
18959018Sn_hibma        if (xpt_create_path(&path, xpt_periph, cam_sim_path(vpo->sim), 0, 0)
19059018Sn_hibma            != CAM_REQ_CMP) {
19159018Sn_hibma		/* A failure is benign as the user can do a manual rescan */
19259018Sn_hibma                return;
19359018Sn_hibma	}
19459018Sn_hibma
19559018Sn_hibma        xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
19659018Sn_hibma        ccb->ccb_h.func_code = XPT_SCAN_BUS;
19759018Sn_hibma        ccb->ccb_h.cbfcnp = vpo_cam_rescan_callback;
19859018Sn_hibma        ccb->crcn.flags = CAM_FLAG_NONE;
19959018Sn_hibma        xpt_action(ccb);
20059018Sn_hibma
20159018Sn_hibma        /* The scan is in progress now. */
20259018Sn_hibma}
20359018Sn_hibma
20428219Smsmith/*
20538061Smsmith * vpo_intr()
20628219Smsmith */
20738061Smsmithstatic void
20839520Snsouchvpo_intr(struct vpo_data *vpo, struct ccb_scsiio *csio)
20928219Smsmith{
21041591Sarchie	int errno;	/* error in errno.h */
21139520Snsouch	int s;
21241591Sarchie#ifdef VP0_DEBUG
21341591Sarchie	int i;
21441591Sarchie#endif
21528219Smsmith
21639520Snsouch	s = splcam();
21728219Smsmith
21839134Snsouch	if (vpo->vpo_isplus) {
21939134Snsouch		errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
22039520Snsouch			csio->ccb_h.target_id,
22139520Snsouch			(char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
22239520Snsouch			(char *)csio->data_ptr, csio->dxfer_len,
22339520Snsouch			&vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
22439134Snsouch	} else {
22539134Snsouch		errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
22639520Snsouch			csio->ccb_h.target_id,
22739520Snsouch			(char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
22839520Snsouch			(char *)csio->data_ptr, csio->dxfer_len,
22939520Snsouch			&vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
23039134Snsouch	}
23128219Smsmith
23228257Smsmith#ifdef VP0_DEBUG
23328257Smsmith	printf("vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
23428257Smsmith		 errno, vpo->vpo_stat, vpo->vpo_count, vpo->vpo_error);
23539520Snsouch
23639520Snsouch	/* dump of command */
23739520Snsouch	for (i=0; i<csio->cdb_len; i++)
23839520Snsouch		printf("%x ", ((char *)&csio->cdb_io.cdb_bytes)[i]);
23939520Snsouch
24039520Snsouch	printf("\n");
24128257Smsmith#endif
24228257Smsmith
24328257Smsmith	if (errno) {
24428257Smsmith		/* connection to ppbus interrupted */
24539520Snsouch		csio->ccb_h.status = CAM_CMD_TIMEOUT;
24628257Smsmith		goto error;
24728257Smsmith	}
24828219Smsmith
24928257Smsmith	/* if a timeout occured, no sense */
25028257Smsmith	if (vpo->vpo_error) {
25139520Snsouch		if (vpo->vpo_error != VP0_ESELECT_TIMEOUT)
25239520Snsouch			printf("vpo%d: VP0 error/timeout (%d)\n",
25339520Snsouch				vpo->vpo_unit, vpo->vpo_error);
25439520Snsouch
25539520Snsouch		csio->ccb_h.status = CAM_CMD_TIMEOUT;
25628257Smsmith		goto error;
25728257Smsmith	}
25828219Smsmith
25939520Snsouch	/* check scsi status */
26039520Snsouch	if (vpo->vpo_stat != SCSI_STATUS_OK) {
26139520Snsouch	   csio->scsi_status = vpo->vpo_stat;
26228219Smsmith
26339520Snsouch	   /* check if we have to sense the drive */
26439520Snsouch	   if ((vpo->vpo_stat & SCSI_STATUS_CHECK_COND) != 0) {
26528219Smsmith
26639520Snsouch		vpo->vpo_sense.cmd.opcode = REQUEST_SENSE;
26739520Snsouch		vpo->vpo_sense.cmd.length = csio->sense_len;
26828219Smsmith		vpo->vpo_sense.cmd.control = 0;
26928219Smsmith
27039134Snsouch		if (vpo->vpo_isplus) {
27139134Snsouch			errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
27239520Snsouch				csio->ccb_h.target_id,
27339134Snsouch				(char *)&vpo->vpo_sense.cmd,
27439134Snsouch				sizeof(vpo->vpo_sense.cmd),
27539520Snsouch				(char *)&csio->sense_data, csio->sense_len,
27639134Snsouch				&vpo->vpo_sense.stat, &vpo->vpo_sense.count,
27739134Snsouch				&vpo->vpo_error);
27839134Snsouch		} else {
27939134Snsouch			errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
28039520Snsouch				csio->ccb_h.target_id,
28139134Snsouch				(char *)&vpo->vpo_sense.cmd,
28239134Snsouch				sizeof(vpo->vpo_sense.cmd),
28339520Snsouch				(char *)&csio->sense_data, csio->sense_len,
28439134Snsouch				&vpo->vpo_sense.stat, &vpo->vpo_sense.count,
28539134Snsouch				&vpo->vpo_error);
28639134Snsouch		}
28739520Snsouch
28828219Smsmith
28939520Snsouch#ifdef VP0_DEBUG
29039520Snsouch		printf("(sense) vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
29139520Snsouch			errno, vpo->vpo_sense.stat, vpo->vpo_sense.count, vpo->vpo_error);
29239520Snsouch#endif
29328257Smsmith
29439520Snsouch		/* check sense return status */
29539520Snsouch		if (errno == 0 && vpo->vpo_sense.stat == SCSI_STATUS_OK) {
29639520Snsouch		   /* sense ok */
29739520Snsouch		   csio->ccb_h.status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR;
29839520Snsouch		   csio->sense_resid = csio->sense_len - vpo->vpo_sense.count;
29928219Smsmith
30039520Snsouch#ifdef VP0_DEBUG
30139520Snsouch		   /* dump of sense info */
30239520Snsouch		   printf("(sense) ");
30339520Snsouch		   for (i=0; i<vpo->vpo_sense.count; i++)
30439520Snsouch			printf("%x ", ((char *)&csio->sense_data)[i]);
30539520Snsouch		   printf("\n");
30639520Snsouch#endif
30739520Snsouch
30839520Snsouch		} else {
30939520Snsouch		   /* sense failed */
31039520Snsouch		   csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
31139520Snsouch		}
31239520Snsouch	   } else {
31339520Snsouch		/* no sense */
31439520Snsouch		csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
31539520Snsouch	   }
31639520Snsouch
31739520Snsouch	   goto error;
31828219Smsmith	}
31928219Smsmith
32039520Snsouch	csio->resid = csio->dxfer_len - vpo->vpo_count;
32139520Snsouch	csio->ccb_h.status = CAM_REQ_CMP;
32228219Smsmith
32328219Smsmitherror:
32439520Snsouch	splx(s);
32528219Smsmith
32628219Smsmith	return;
32728219Smsmith}
32828219Smsmith
32939520Snsouchstatic void
33039520Snsouchvpo_action(struct cam_sim *sim, union ccb *ccb)
33128219Smsmith{
33228219Smsmith
33339520Snsouch	struct vpo_data *vpo = (struct vpo_data *)sim->softc;
33428219Smsmith
33539520Snsouch	switch (ccb->ccb_h.func_code) {
33639520Snsouch	case XPT_SCSI_IO:
33739520Snsouch	{
33839520Snsouch		struct ccb_scsiio *csio;
33939520Snsouch
34039520Snsouch		csio = &ccb->csio;
34139520Snsouch
34239520Snsouch#ifdef VP0_DEBUG
34339520Snsouch		printf("vpo%d: XPT_SCSI_IO (0x%x) request\n",
34439520Snsouch			vpo->vpo_unit, csio->cdb_io.cdb_bytes[0]);
34539520Snsouch#endif
34639520Snsouch
34739520Snsouch		vpo_intr(vpo, csio);
34839520Snsouch
34939520Snsouch		xpt_done(ccb);
35039520Snsouch
35139520Snsouch		break;
35228219Smsmith	}
35339520Snsouch	case XPT_CALC_GEOMETRY:
35439520Snsouch	{
35539520Snsouch		struct	  ccb_calc_geometry *ccg;
35628219Smsmith
35739520Snsouch		ccg = &ccb->ccg;
35839520Snsouch
35939520Snsouch#ifdef VP0_DEBUG
360114451Sjhb		printf("vpo%d: XPT_CALC_GEOMETRY (bs=%d,vs=%jd,c=%d,h=%d,spt=%d) request\n",
36151952Sn_hibma			vpo->vpo_unit,
36251952Sn_hibma			ccg->block_size,
363114451Sjhb			(intmax_t)ccg->volume_size,
36451952Sn_hibma			ccg->cylinders,
36551952Sn_hibma			ccg->heads,
36651952Sn_hibma			ccg->secs_per_track);
36739520Snsouch#endif
36851952Sn_hibma
36939520Snsouch		ccg->heads = 64;
37039520Snsouch		ccg->secs_per_track = 32;
37151952Sn_hibma		ccg->cylinders = ccg->volume_size /
37251952Sn_hibma				 (ccg->heads * ccg->secs_per_track);
37339520Snsouch
37439520Snsouch		ccb->ccb_h.status = CAM_REQ_CMP;
37539520Snsouch		xpt_done(ccb);
37639520Snsouch		break;
37728219Smsmith	}
37839520Snsouch	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
37939520Snsouch	{
38028219Smsmith
38128257Smsmith#ifdef VP0_DEBUG
38239520Snsouch		printf("vpo%d: XPT_RESET_BUS request\n", vpo->vpo_unit);
38328219Smsmith#endif
38428219Smsmith
38539520Snsouch		if (vpo->vpo_isplus) {
38639520Snsouch			if (imm_reset_bus(&vpo->vpo_io)) {
38739520Snsouch				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
38839520Snsouch				xpt_done(ccb);
38939520Snsouch				return;
39039520Snsouch			}
39139520Snsouch		} else {
39239520Snsouch			if (vpoio_reset_bus(&vpo->vpo_io)) {
39339520Snsouch				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
39439520Snsouch				xpt_done(ccb);
39539520Snsouch				return;
39639520Snsouch			}
39739520Snsouch		}
39839520Snsouch
39939520Snsouch		ccb->ccb_h.status = CAM_REQ_CMP;
40039520Snsouch		xpt_done(ccb);
40139520Snsouch		break;
40228219Smsmith	}
40339520Snsouch	case XPT_PATH_INQ:		/* Path routing inquiry */
40439520Snsouch	{
40539520Snsouch		struct ccb_pathinq *cpi = &ccb->cpi;
40639520Snsouch
40739520Snsouch#ifdef VP0_DEBUG
40839520Snsouch		printf("vpo%d: XPT_PATH_INQ request\n", vpo->vpo_unit);
40939520Snsouch#endif
41039520Snsouch		cpi->version_num = 1; /* XXX??? */
41142441Snsouch		cpi->hba_inquiry = 0;
41242441Snsouch		cpi->target_sprt = 0;
41342441Snsouch		cpi->hba_misc = 0;
41442441Snsouch		cpi->hba_eng_cnt = 0;
41539520Snsouch		cpi->max_target = 7;
41639520Snsouch		cpi->max_lun = 0;
41739520Snsouch		cpi->initiator_id = VP0_INITIATOR;
41839520Snsouch		cpi->bus_id = sim->bus_id;
41946586Sken		cpi->base_transfer_speed = 93;
42039520Snsouch		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
42139520Snsouch		strncpy(cpi->hba_vid, "Iomega", HBA_IDLEN);
42239520Snsouch		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
42339520Snsouch		cpi->unit_number = sim->unit_number;
42428219Smsmith
42539520Snsouch		cpi->ccb_h.status = CAM_REQ_CMP;
42639520Snsouch		xpt_done(ccb);
42739520Snsouch		break;
42839520Snsouch	}
42939520Snsouch	default:
43039520Snsouch		ccb->ccb_h.status = CAM_REQ_INVALID;
43139520Snsouch		xpt_done(ccb);
43239520Snsouch		break;
43339520Snsouch	}
43428219Smsmith
43539520Snsouch	return;
43639520Snsouch}
43728219Smsmith
43839520Snsouchstatic void
43939520Snsouchvpo_poll(struct cam_sim *sim)
44039520Snsouch{
44139520Snsouch	/* The ZIP is actually always polled throw vpo_action() */
44239520Snsouch	return;
44328219Smsmith}
44455939Snsouch
44556455Speterstatic devclass_t vpo_devclass;
44656455Speter
44756455Speterstatic device_method_t vpo_methods[] = {
44856455Speter	/* device interface */
44956455Speter	DEVMETHOD(device_identify,	vpo_identify),
45056455Speter	DEVMETHOD(device_probe,		vpo_probe),
45156455Speter	DEVMETHOD(device_attach,	vpo_attach),
45256455Speter
45356455Speter	{ 0, 0 }
45456455Speter};
45556455Speter
45656455Speterstatic driver_t vpo_driver = {
45756455Speter	"vpo",
45856455Speter	vpo_methods,
45956455Speter	sizeof(struct vpo_data),
46056455Speter};
46155939SnsouchDRIVER_MODULE(vpo, ppbus, vpo_driver, vpo_devclass, 0, 0);
462