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