vpo.c revision 39520
1/*-
2 * Copyright (c) 1997, 1998 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 *	$Id: vpo.c,v 1.4 1997/09/01 00:51:52 bde Exp $
27 *
28 */
29
30#ifdef KERNEL
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/malloc.h>
34#include <sys/buf.h>
35
36#include <machine/clock.h>
37
38#endif	/* KERNEL */
39
40#include <cam/cam.h>
41#include <cam/cam_ccb.h>
42#include <cam/cam_sim.h>
43#include <cam/cam_xpt_sim.h>
44#include <cam/cam_debug.h>
45
46#include <cam/scsi/scsi_all.h>
47#include <cam/scsi/scsi_message.h>
48#include <cam/scsi/scsi_da.h>
49
50#ifdef	KERNEL
51#include <sys/kernel.h>
52#endif /*KERNEL */
53
54#include <dev/ppbus/ppbconf.h>
55#include <dev/ppbus/vpoio.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	struct cam_path *path;
74
75	struct vpo_sense vpo_sense;
76
77	struct vpoio_data vpo_io;	/* interface to low level functions */
78};
79
80/* cam related functions */
81static void	vpo_action(struct cam_sim *sim, union ccb *ccb);
82static void	vpo_poll(struct cam_sim *sim);
83
84static int	nvpo = 0;
85#define MAXVP0	8			/* XXX not much better! */
86static struct vpo_data *vpodata[MAXVP0];
87
88#ifdef KERNEL
89
90/*
91 * Make ourselves visible as a ppbus driver
92 */
93static struct ppb_device	*vpoprobe(struct ppb_data *ppb);
94static int			vpoattach(struct ppb_device *dev);
95
96static struct ppb_driver vpodriver = {
97    vpoprobe, vpoattach, "vpo"
98};
99DATA_SET(ppbdriver_set, vpodriver);
100
101#endif /* KERNEL */
102
103/*
104 * vpoprobe()
105 *
106 * Called by ppb_attachdevs().
107 */
108static struct ppb_device *
109vpoprobe(struct ppb_data *ppb)
110{
111	struct vpo_data *vpo;
112	struct ppb_device *dev;
113
114	if (nvpo >= MAXVP0) {
115		printf("vpo: Too many devices (max %d)\n", MAXVP0);
116		return(NULL);
117	}
118
119	vpo = (struct vpo_data *)malloc(sizeof(struct vpo_data),
120							M_DEVBUF, M_NOWAIT);
121	if (!vpo) {
122		printf("vpo: cannot malloc!\n");
123		return(NULL);
124	}
125	bzero(vpo, sizeof(struct vpo_data));
126
127	vpodata[nvpo] = vpo;
128
129	/* vpo dependent initialisation */
130	vpo->vpo_unit = nvpo;
131
132	/* ok, go to next device on next probe */
133	nvpo ++;
134
135	/* low level probe */
136	vpoio_set_unit(&vpo->vpo_io, vpo->vpo_unit);
137
138	/* check ZIP before ZIP+ or imm_probe() will send controls to
139	 * the printer or whatelse connected to the port */
140	if ((dev = vpoio_probe(ppb, &vpo->vpo_io))) {
141		vpo->vpo_isplus = 0;
142	} else if ((dev = imm_probe(ppb, &vpo->vpo_io))) {
143		vpo->vpo_isplus = 1;
144	} else {
145		free(vpo, M_DEVBUF);
146		return (NULL);
147	}
148
149	return (dev);
150}
151
152/*
153 * vpoattach()
154 *
155 * Called by ppb_attachdevs().
156 */
157static int
158vpoattach(struct ppb_device *dev)
159{
160	struct scsibus_data *scbus;
161	struct vpo_data *vpo = vpodata[dev->id_unit];
162	struct cam_devq *devq;
163
164	/* low level attachment */
165	if (vpo->vpo_isplus) {
166		if (!imm_attach(&vpo->vpo_io))
167			return (0);
168	} else {
169		if (!vpoio_attach(&vpo->vpo_io))
170			return (0);
171	}
172
173	/*
174	**	Now tell the generic SCSI layer
175	**	about our bus.
176	*/
177	devq = cam_simq_alloc(/*maxopenings*/1);
178	/* XXX What about low-level detach on error? */
179	if (devq == NULL)
180		return (0);
181
182	vpo->sim = cam_sim_alloc(vpo_action, vpo_poll, "vpo", vpo, dev->id_unit,
183				 /*untagged*/1, /*tagged*/0, devq);
184	if (vpo->sim == NULL) {
185		cam_simq_free(devq);
186		return (0);
187	}
188
189	cam_sim_set_basexfer_speed(vpo->sim, 93/*kB/s*/);
190	if (xpt_bus_register(vpo->sim, /*bus*/0) != CAM_SUCCESS) {
191		cam_sim_free(vpo->sim, /*free_devq*/TRUE);
192		return (0);
193	}
194
195	if (xpt_create_path(&vpo->path, /*periph*/NULL,
196			    cam_sim_path(vpo->sim), CAM_TARGET_WILDCARD,
197			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
198		xpt_bus_deregister(cam_sim_path(vpo->sim));
199		cam_sim_free(vpo->sim, /*free_devq*/TRUE);
200		return (0);
201	}
202
203	/* all went ok */
204
205	return (1);
206}
207
208/*
209 * vpo_intr()
210 */
211static void
212vpo_intr(struct vpo_data *vpo, struct ccb_scsiio *csio)
213{
214
215	int i, errno;	/* error in errno.h */
216	int s;
217
218	s = splcam();
219
220	if (vpo->vpo_isplus) {
221		errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
222			csio->ccb_h.target_id,
223			(char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
224			(char *)csio->data_ptr, csio->dxfer_len,
225			&vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
226	} else {
227		errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
228			csio->ccb_h.target_id,
229			(char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
230			(char *)csio->data_ptr, csio->dxfer_len,
231			&vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
232	}
233
234#ifdef VP0_DEBUG
235	printf("vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
236		 errno, vpo->vpo_stat, vpo->vpo_count, vpo->vpo_error);
237
238	/* dump of command */
239	for (i=0; i<csio->cdb_len; i++)
240		printf("%x ", ((char *)&csio->cdb_io.cdb_bytes)[i]);
241
242	printf("\n");
243#endif
244
245	if (errno) {
246		/* connection to ppbus interrupted */
247		csio->ccb_h.status = CAM_CMD_TIMEOUT;
248		goto error;
249	}
250
251	/* if a timeout occured, no sense */
252	if (vpo->vpo_error) {
253		if (vpo->vpo_error != VP0_ESELECT_TIMEOUT)
254			printf("vpo%d: VP0 error/timeout (%d)\n",
255				vpo->vpo_unit, vpo->vpo_error);
256
257		csio->ccb_h.status = CAM_CMD_TIMEOUT;
258		goto error;
259	}
260
261	/* check scsi status */
262	if (vpo->vpo_stat != SCSI_STATUS_OK) {
263	   csio->scsi_status = vpo->vpo_stat;
264
265	   /* check if we have to sense the drive */
266	   if ((vpo->vpo_stat & SCSI_STATUS_CHECK_COND) != 0) {
267
268		vpo->vpo_sense.cmd.opcode = REQUEST_SENSE;
269		vpo->vpo_sense.cmd.length = csio->sense_len;
270		vpo->vpo_sense.cmd.control = 0;
271
272		if (vpo->vpo_isplus) {
273			errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
274				csio->ccb_h.target_id,
275				(char *)&vpo->vpo_sense.cmd,
276				sizeof(vpo->vpo_sense.cmd),
277				(char *)&csio->sense_data, csio->sense_len,
278				&vpo->vpo_sense.stat, &vpo->vpo_sense.count,
279				&vpo->vpo_error);
280		} else {
281			errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
282				csio->ccb_h.target_id,
283				(char *)&vpo->vpo_sense.cmd,
284				sizeof(vpo->vpo_sense.cmd),
285				(char *)&csio->sense_data, csio->sense_len,
286				&vpo->vpo_sense.stat, &vpo->vpo_sense.count,
287				&vpo->vpo_error);
288		}
289
290
291#ifdef VP0_DEBUG
292		printf("(sense) vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
293			errno, vpo->vpo_sense.stat, vpo->vpo_sense.count, vpo->vpo_error);
294#endif
295
296		/* check sense return status */
297		if (errno == 0 && vpo->vpo_sense.stat == SCSI_STATUS_OK) {
298		   /* sense ok */
299		   csio->ccb_h.status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR;
300		   csio->sense_resid = csio->sense_len - vpo->vpo_sense.count;
301
302#ifdef VP0_DEBUG
303		   /* dump of sense info */
304		   printf("(sense) ");
305		   for (i=0; i<vpo->vpo_sense.count; i++)
306			printf("%x ", ((char *)&csio->sense_data)[i]);
307		   printf("\n");
308#endif
309
310		} else {
311		   /* sense failed */
312		   csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
313		}
314	   } else {
315		/* no sense */
316		csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
317	   }
318
319	   goto error;
320	}
321
322	csio->resid = csio->dxfer_len - vpo->vpo_count;
323	csio->ccb_h.status = CAM_REQ_CMP;
324
325error:
326	splx(s);
327
328	return;
329}
330
331static void
332vpo_action(struct cam_sim *sim, union ccb *ccb)
333{
334
335	struct vpo_data *vpo = (struct vpo_data *)sim->softc;
336
337	switch (ccb->ccb_h.func_code) {
338	case XPT_SCSI_IO:
339	{
340		struct ccb_scsiio *csio;
341
342		csio = &ccb->csio;
343
344#ifdef VP0_DEBUG
345		printf("vpo%d: XPT_SCSI_IO (0x%x) request\n",
346			vpo->vpo_unit, csio->cdb_io.cdb_bytes[0]);
347#endif
348
349		vpo_intr(vpo, csio);
350
351		xpt_done(ccb);
352
353		break;
354	}
355	case XPT_CALC_GEOMETRY:
356	{
357		struct	  ccb_calc_geometry *ccg;
358		u_int32_t size_mb;
359		u_int32_t secs_per_cylinder;
360
361		ccg = &ccb->ccg;
362		size_mb = ccg->volume_size
363			/ ((1024L * 1024L) / ccg->block_size);
364
365#ifdef VP0_DEBUG
366		printf("vpo%d: XPT_CALC_GEOMETRY (%d, %d) request\n",
367			vpo->vpo_unit, ccg->volume_size, ccg->block_size);
368#endif
369
370		ccg->heads = 64;
371		ccg->secs_per_track = 32;
372
373		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
374		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
375
376		ccb->ccb_h.status = CAM_REQ_CMP;
377		xpt_done(ccb);
378		break;
379	}
380	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
381	{
382
383#ifdef VP0_DEBUG
384		printf("vpo%d: XPT_RESET_BUS request\n", vpo->vpo_unit);
385#endif
386
387		if (vpo->vpo_isplus) {
388			if (imm_reset_bus(&vpo->vpo_io)) {
389				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
390				xpt_done(ccb);
391				return;
392			}
393		} else {
394			if (vpoio_reset_bus(&vpo->vpo_io)) {
395				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
396				xpt_done(ccb);
397				return;
398			}
399		}
400
401		ccb->ccb_h.status = CAM_REQ_CMP;
402		xpt_done(ccb);
403		break;
404	}
405	case XPT_PATH_INQ:		/* Path routing inquiry */
406	{
407		struct ccb_pathinq *cpi = &ccb->cpi;
408
409#ifdef VP0_DEBUG
410		printf("vpo%d: XPT_PATH_INQ request\n", vpo->vpo_unit);
411#endif
412		cpi->version_num = 1; /* XXX??? */
413		cpi->max_target = 7;
414		cpi->max_lun = 0;
415		cpi->initiator_id = VP0_INITIATOR;
416		cpi->bus_id = sim->bus_id;
417		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
418		strncpy(cpi->hba_vid, "Iomega", HBA_IDLEN);
419		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
420		cpi->unit_number = sim->unit_number;
421
422		cpi->ccb_h.status = CAM_REQ_CMP;
423		xpt_done(ccb);
424		break;
425	}
426	default:
427		ccb->ccb_h.status = CAM_REQ_INVALID;
428		xpt_done(ccb);
429		break;
430	}
431
432	return;
433}
434
435static void
436vpo_poll(struct cam_sim *sim)
437{
438	/* The ZIP is actually always polled throw vpo_action() */
439	return;
440}
441