vpo.c revision 67164
1/*-
2 * Copyright (c) 1997, 1998, 1999 Nicolas Souchu
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/dev/ppbus/vpo.c 67164 2000-10-15 14:19:01Z phk $
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/module.h>
32#include <sys/bus.h>
33#include <sys/malloc.h>
34#include <sys/devicestat.h>	/* for struct devstat */
35
36
37#include <cam/cam.h>
38#include <cam/cam_ccb.h>
39#include <cam/cam_sim.h>
40#include <cam/cam_xpt_sim.h>
41#include <cam/cam_debug.h>
42#include <cam/cam_periph.h>
43
44#include <cam/scsi/scsi_all.h>
45#include <cam/scsi/scsi_message.h>
46#include <cam/scsi/scsi_da.h>
47
48#include <sys/kernel.h>
49
50#include "opt_vpo.h"
51
52#include <dev/ppbus/ppbconf.h>
53#include <dev/ppbus/vpoio.h>
54
55#include "ppbus_if.h"
56
57struct vpo_sense {
58	struct scsi_sense cmd;
59	unsigned int stat;
60	unsigned int count;
61};
62
63struct vpo_data {
64	unsigned short vpo_unit;
65
66	int vpo_stat;
67	int vpo_count;
68	int vpo_error;
69
70	int vpo_isplus;
71
72	struct cam_sim  *sim;
73
74	struct vpo_sense vpo_sense;
75
76	struct vpoio_data vpo_io;	/* interface to low level functions */
77};
78
79#define DEVTOSOFTC(dev) \
80	((struct vpo_data *)device_get_softc(dev))
81
82/* cam related functions */
83static void	vpo_action(struct cam_sim *sim, union ccb *ccb);
84static void	vpo_poll(struct cam_sim *sim);
85static void	vpo_cam_rescan_callback(struct cam_periph *periph,
86					union ccb *ccb);
87static void	vpo_cam_rescan(struct vpo_data *vpo);
88
89static void
90vpo_identify(driver_t *driver, device_t parent)
91{
92
93	BUS_ADD_CHILD(parent, 0, "vpo", 0);
94}
95
96/*
97 * vpo_probe()
98 */
99static int
100vpo_probe(device_t dev)
101{
102	struct vpo_data *vpo;
103	int error;
104
105	vpo = DEVTOSOFTC(dev);
106	bzero(vpo, sizeof(struct vpo_data));
107
108	/* vpo dependent initialisation */
109	vpo->vpo_unit = device_get_unit(dev);
110
111	/* low level probe */
112	vpoio_set_unit(&vpo->vpo_io, vpo->vpo_unit);
113
114	/* check ZIP before ZIP+ or imm_probe() will send controls to
115	 * the printer or whatelse connected to the port */
116	if ((error = vpoio_probe(dev, &vpo->vpo_io)) == 0) {
117		vpo->vpo_isplus = 0;
118		device_set_desc(dev,
119				"Iomega VPI0 Parallel to SCSI interface");
120	} else if ((error = imm_probe(dev, &vpo->vpo_io)) == 0) {
121		vpo->vpo_isplus = 1;
122		device_set_desc(dev,
123				"Iomega Matchmaker Parallel to SCSI interface");
124	} else {
125		return (error);
126	}
127
128	return (0);
129}
130
131/*
132 * vpo_attach()
133 */
134static int
135vpo_attach(device_t dev)
136{
137	struct vpo_data *vpo = DEVTOSOFTC(dev);
138	struct cam_devq *devq;
139	int error;
140
141	/* low level attachment */
142	if (vpo->vpo_isplus) {
143		if ((error = imm_attach(&vpo->vpo_io)))
144			return (error);
145	} else {
146		if ((error = vpoio_attach(&vpo->vpo_io)))
147			return (error);
148	}
149
150	/*
151	**	Now tell the generic SCSI layer
152	**	about our bus.
153	*/
154	devq = cam_simq_alloc(/*maxopenings*/1);
155	/* XXX What about low-level detach on error? */
156	if (devq == NULL)
157		return (ENXIO);
158
159	vpo->sim = cam_sim_alloc(vpo_action, vpo_poll, "vpo", vpo,
160				 device_get_unit(dev),
161				 /*untagged*/1, /*tagged*/0, devq);
162	if (vpo->sim == NULL) {
163		cam_simq_free(devq);
164		return (ENXIO);
165	}
166
167	if (xpt_bus_register(vpo->sim, /*bus*/0) != CAM_SUCCESS) {
168		cam_sim_free(vpo->sim, /*free_devq*/TRUE);
169		return (ENXIO);
170	}
171
172	/* all went ok */
173
174	vpo_cam_rescan(vpo);	/* have CAM rescan the bus */
175
176	return (0);
177}
178
179static void
180vpo_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
181{
182        free(ccb, M_TEMP);
183}
184
185static void
186vpo_cam_rescan(struct vpo_data *vpo)
187{
188        struct cam_path *path;
189        union ccb *ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK);
190
191        bzero(ccb, sizeof(union ccb));
192
193        if (xpt_create_path(&path, xpt_periph, cam_sim_path(vpo->sim), 0, 0)
194            != CAM_REQ_CMP) {
195		/* A failure is benign as the user can do a manual rescan */
196                return;
197	}
198
199        xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
200        ccb->ccb_h.func_code = XPT_SCAN_BUS;
201        ccb->ccb_h.cbfcnp = vpo_cam_rescan_callback;
202        ccb->crcn.flags = CAM_FLAG_NONE;
203        xpt_action(ccb);
204
205        /* The scan is in progress now. */
206}
207
208/*
209 * vpo_intr()
210 */
211static void
212vpo_intr(struct vpo_data *vpo, struct ccb_scsiio *csio)
213{
214	int errno;	/* error in errno.h */
215	int s;
216#ifdef VP0_DEBUG
217	int i;
218#endif
219
220	s = splcam();
221
222	if (vpo->vpo_isplus) {
223		errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
224			csio->ccb_h.target_id,
225			(char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
226			(char *)csio->data_ptr, csio->dxfer_len,
227			&vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
228	} else {
229		errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
230			csio->ccb_h.target_id,
231			(char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
232			(char *)csio->data_ptr, csio->dxfer_len,
233			&vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
234	}
235
236#ifdef VP0_DEBUG
237	printf("vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
238		 errno, vpo->vpo_stat, vpo->vpo_count, vpo->vpo_error);
239
240	/* dump of command */
241	for (i=0; i<csio->cdb_len; i++)
242		printf("%x ", ((char *)&csio->cdb_io.cdb_bytes)[i]);
243
244	printf("\n");
245#endif
246
247	if (errno) {
248		/* connection to ppbus interrupted */
249		csio->ccb_h.status = CAM_CMD_TIMEOUT;
250		goto error;
251	}
252
253	/* if a timeout occured, no sense */
254	if (vpo->vpo_error) {
255		if (vpo->vpo_error != VP0_ESELECT_TIMEOUT)
256			printf("vpo%d: VP0 error/timeout (%d)\n",
257				vpo->vpo_unit, vpo->vpo_error);
258
259		csio->ccb_h.status = CAM_CMD_TIMEOUT;
260		goto error;
261	}
262
263	/* check scsi status */
264	if (vpo->vpo_stat != SCSI_STATUS_OK) {
265	   csio->scsi_status = vpo->vpo_stat;
266
267	   /* check if we have to sense the drive */
268	   if ((vpo->vpo_stat & SCSI_STATUS_CHECK_COND) != 0) {
269
270		vpo->vpo_sense.cmd.opcode = REQUEST_SENSE;
271		vpo->vpo_sense.cmd.length = csio->sense_len;
272		vpo->vpo_sense.cmd.control = 0;
273
274		if (vpo->vpo_isplus) {
275			errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
276				csio->ccb_h.target_id,
277				(char *)&vpo->vpo_sense.cmd,
278				sizeof(vpo->vpo_sense.cmd),
279				(char *)&csio->sense_data, csio->sense_len,
280				&vpo->vpo_sense.stat, &vpo->vpo_sense.count,
281				&vpo->vpo_error);
282		} else {
283			errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
284				csio->ccb_h.target_id,
285				(char *)&vpo->vpo_sense.cmd,
286				sizeof(vpo->vpo_sense.cmd),
287				(char *)&csio->sense_data, csio->sense_len,
288				&vpo->vpo_sense.stat, &vpo->vpo_sense.count,
289				&vpo->vpo_error);
290		}
291
292
293#ifdef VP0_DEBUG
294		printf("(sense) vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
295			errno, vpo->vpo_sense.stat, vpo->vpo_sense.count, vpo->vpo_error);
296#endif
297
298		/* check sense return status */
299		if (errno == 0 && vpo->vpo_sense.stat == SCSI_STATUS_OK) {
300		   /* sense ok */
301		   csio->ccb_h.status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR;
302		   csio->sense_resid = csio->sense_len - vpo->vpo_sense.count;
303
304#ifdef VP0_DEBUG
305		   /* dump of sense info */
306		   printf("(sense) ");
307		   for (i=0; i<vpo->vpo_sense.count; i++)
308			printf("%x ", ((char *)&csio->sense_data)[i]);
309		   printf("\n");
310#endif
311
312		} else {
313		   /* sense failed */
314		   csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
315		}
316	   } else {
317		/* no sense */
318		csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
319	   }
320
321	   goto error;
322	}
323
324	csio->resid = csio->dxfer_len - vpo->vpo_count;
325	csio->ccb_h.status = CAM_REQ_CMP;
326
327error:
328	splx(s);
329
330	return;
331}
332
333static void
334vpo_action(struct cam_sim *sim, union ccb *ccb)
335{
336
337	struct vpo_data *vpo = (struct vpo_data *)sim->softc;
338
339	switch (ccb->ccb_h.func_code) {
340	case XPT_SCSI_IO:
341	{
342		struct ccb_scsiio *csio;
343
344		csio = &ccb->csio;
345
346#ifdef VP0_DEBUG
347		printf("vpo%d: XPT_SCSI_IO (0x%x) request\n",
348			vpo->vpo_unit, csio->cdb_io.cdb_bytes[0]);
349#endif
350
351		vpo_intr(vpo, csio);
352
353		xpt_done(ccb);
354
355		break;
356	}
357	case XPT_CALC_GEOMETRY:
358	{
359		struct	  ccb_calc_geometry *ccg;
360
361		ccg = &ccb->ccg;
362
363#ifdef VP0_DEBUG
364		printf("vpo%d: XPT_CALC_GEOMETRY (bs=%d,vs=%d,c=%d,h=%d,spt=%d) request\n",
365			vpo->vpo_unit,
366			ccg->block_size,
367			ccg->volume_size,
368			ccg->cylinders,
369			ccg->heads,
370			ccg->secs_per_track);
371#endif
372
373		ccg->heads = 64;
374		ccg->secs_per_track = 32;
375		ccg->cylinders = ccg->volume_size /
376				 (ccg->heads * ccg->secs_per_track);
377
378		ccb->ccb_h.status = CAM_REQ_CMP;
379		xpt_done(ccb);
380		break;
381	}
382	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
383	{
384
385#ifdef VP0_DEBUG
386		printf("vpo%d: XPT_RESET_BUS request\n", vpo->vpo_unit);
387#endif
388
389		if (vpo->vpo_isplus) {
390			if (imm_reset_bus(&vpo->vpo_io)) {
391				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
392				xpt_done(ccb);
393				return;
394			}
395		} else {
396			if (vpoio_reset_bus(&vpo->vpo_io)) {
397				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
398				xpt_done(ccb);
399				return;
400			}
401		}
402
403		ccb->ccb_h.status = CAM_REQ_CMP;
404		xpt_done(ccb);
405		break;
406	}
407	case XPT_PATH_INQ:		/* Path routing inquiry */
408	{
409		struct ccb_pathinq *cpi = &ccb->cpi;
410
411#ifdef VP0_DEBUG
412		printf("vpo%d: XPT_PATH_INQ request\n", vpo->vpo_unit);
413#endif
414		cpi->version_num = 1; /* XXX??? */
415		cpi->hba_inquiry = 0;
416		cpi->target_sprt = 0;
417		cpi->hba_misc = 0;
418		cpi->hba_eng_cnt = 0;
419		cpi->max_target = 7;
420		cpi->max_lun = 0;
421		cpi->initiator_id = VP0_INITIATOR;
422		cpi->bus_id = sim->bus_id;
423		cpi->base_transfer_speed = 93;
424		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
425		strncpy(cpi->hba_vid, "Iomega", HBA_IDLEN);
426		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
427		cpi->unit_number = sim->unit_number;
428
429		cpi->ccb_h.status = CAM_REQ_CMP;
430		xpt_done(ccb);
431		break;
432	}
433	default:
434		ccb->ccb_h.status = CAM_REQ_INVALID;
435		xpt_done(ccb);
436		break;
437	}
438
439	return;
440}
441
442static void
443vpo_poll(struct cam_sim *sim)
444{
445	/* The ZIP is actually always polled throw vpo_action() */
446	return;
447}
448
449static devclass_t vpo_devclass;
450
451static device_method_t vpo_methods[] = {
452	/* device interface */
453	DEVMETHOD(device_identify,	vpo_identify),
454	DEVMETHOD(device_probe,		vpo_probe),
455	DEVMETHOD(device_attach,	vpo_attach),
456
457	{ 0, 0 }
458};
459
460static driver_t vpo_driver = {
461	"vpo",
462	vpo_methods,
463	sizeof(struct vpo_data),
464};
465DRIVER_MODULE(vpo, ppbus, vpo_driver, vpo_devclass, 0, 0);
466