1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1997, 1998, 1999 Nicolas Souchu
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD$");
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/module.h>
36#include <sys/bus.h>
37#include <sys/lock.h>
38#include <sys/mutex.h>
39#include <sys/malloc.h>
40
41#include <cam/cam.h>
42#include <cam/cam_ccb.h>
43#include <cam/cam_sim.h>
44#include <cam/cam_xpt_sim.h>
45#include <cam/cam_debug.h>
46#include <cam/cam_periph.h>
47
48#include <cam/scsi/scsi_all.h>
49#include <cam/scsi/scsi_message.h>
50#include <cam/scsi/scsi_da.h>
51
52#include <sys/kernel.h>
53
54#include "opt_vpo.h"
55
56#include <dev/ppbus/ppbconf.h>
57#include <dev/ppbus/vpoio.h>
58
59#include "ppbus_if.h"
60
61struct vpo_sense {
62	struct scsi_sense cmd;
63	unsigned int stat;
64	unsigned int count;
65};
66
67struct vpo_data {
68	device_t vpo_dev;
69	int vpo_stat;
70	int vpo_count;
71	int vpo_error;
72
73	int vpo_isplus;
74
75	struct cam_sim  *sim;
76
77	struct vpo_sense vpo_sense;
78
79	struct vpoio_data vpo_io;	/* interface to low level functions */
80};
81
82#define DEVTOSOFTC(dev) \
83	((struct vpo_data *)device_get_softc(dev))
84
85/* cam related functions */
86static void	vpo_action(struct cam_sim *sim, union ccb *ccb);
87static void	vpo_poll(struct cam_sim *sim);
88
89static void
90vpo_identify(driver_t *driver, device_t parent)
91{
92
93	device_t dev;
94
95	dev = device_find_child(parent, "vpo", -1);
96	if (!dev)
97		BUS_ADD_CHILD(parent, 0, "vpo", -1);
98}
99
100/*
101 * vpo_probe()
102 */
103static int
104vpo_probe(device_t dev)
105{
106	device_t ppbus = device_get_parent(dev);
107	struct vpo_data *vpo;
108	int error;
109
110	vpo = DEVTOSOFTC(dev);
111	vpo->vpo_dev = dev;
112
113	/* check ZIP before ZIP+ or imm_probe() will send controls to
114	 * the printer or whatelse connected to the port */
115	ppb_lock(ppbus);
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		ppb_unlock(ppbus);
126		return (error);
127	}
128	ppb_unlock(ppbus);
129
130	return (0);
131}
132
133/*
134 * vpo_attach()
135 */
136static int
137vpo_attach(device_t dev)
138{
139	struct vpo_data *vpo = DEVTOSOFTC(dev);
140	device_t ppbus = device_get_parent(dev);
141	struct ppb_data *ppb = device_get_softc(ppbus);	/* XXX: layering */
142	struct cam_devq *devq;
143	int error;
144
145	/* low level attachment */
146	if (vpo->vpo_isplus) {
147		if ((error = imm_attach(&vpo->vpo_io)))
148			return (error);
149	} else {
150		if ((error = vpoio_attach(&vpo->vpo_io)))
151			return (error);
152	}
153
154	/*
155	**	Now tell the generic SCSI layer
156	**	about our bus.
157	*/
158	devq = cam_simq_alloc(/*maxopenings*/1);
159	/* XXX What about low-level detach on error? */
160	if (devq == NULL)
161		return (ENXIO);
162
163	vpo->sim = cam_sim_alloc(vpo_action, vpo_poll, "vpo", vpo,
164				 device_get_unit(dev), ppb->ppc_lock,
165				 /*untagged*/1, /*tagged*/0, devq);
166	if (vpo->sim == NULL) {
167		cam_simq_free(devq);
168		return (ENXIO);
169	}
170
171	ppb_lock(ppbus);
172	if (xpt_bus_register(vpo->sim, dev, /*bus*/0) != CAM_SUCCESS) {
173		cam_sim_free(vpo->sim, /*free_devq*/TRUE);
174		ppb_unlock(ppbus);
175		return (ENXIO);
176	}
177	ppb_unlock(ppbus);
178
179	return (0);
180}
181
182/*
183 * vpo_intr()
184 */
185static void
186vpo_intr(struct vpo_data *vpo, struct ccb_scsiio *csio)
187{
188	int errno;	/* error in errno.h */
189#ifdef VP0_DEBUG
190	int i;
191#endif
192	uint8_t *ptr;
193
194	ptr = scsiio_cdb_ptr(csio);
195	if (vpo->vpo_isplus) {
196		errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
197			csio->ccb_h.target_id,
198			ptr, csio->cdb_len,
199			(char *)csio->data_ptr, csio->dxfer_len,
200			&vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
201	} else {
202		errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
203			csio->ccb_h.target_id,
204			ptr, csio->cdb_len,
205			(char *)csio->data_ptr, csio->dxfer_len,
206			&vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
207	}
208
209#ifdef VP0_DEBUG
210	printf("vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
211		 errno, vpo->vpo_stat, vpo->vpo_count, vpo->vpo_error);
212
213	/* dump of command */
214	for (i=0; i<csio->cdb_len; i++)
215		printf("%x ", ((char *)ptr)[i]);
216
217	printf("\n");
218#endif
219
220	if (errno) {
221		/* connection to ppbus interrupted */
222		csio->ccb_h.status = CAM_CMD_TIMEOUT;
223		return;
224	}
225
226	/* if a timeout occurred, no sense */
227	if (vpo->vpo_error) {
228		if (vpo->vpo_error != VP0_ESELECT_TIMEOUT)
229			device_printf(vpo->vpo_dev, "VP0 error/timeout (%d)\n",
230				vpo->vpo_error);
231
232		csio->ccb_h.status = CAM_CMD_TIMEOUT;
233		return;
234	}
235
236	/* check scsi status */
237	if (vpo->vpo_stat != SCSI_STATUS_OK) {
238	   csio->scsi_status = vpo->vpo_stat;
239
240	   /* check if we have to sense the drive */
241	   if ((vpo->vpo_stat & SCSI_STATUS_CHECK_COND) != 0) {
242
243		vpo->vpo_sense.cmd.opcode = REQUEST_SENSE;
244		vpo->vpo_sense.cmd.length = csio->sense_len;
245		vpo->vpo_sense.cmd.control = 0;
246
247		if (vpo->vpo_isplus) {
248			errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
249				csio->ccb_h.target_id,
250				(char *)&vpo->vpo_sense.cmd,
251				sizeof(vpo->vpo_sense.cmd),
252				(char *)&csio->sense_data, csio->sense_len,
253				&vpo->vpo_sense.stat, &vpo->vpo_sense.count,
254				&vpo->vpo_error);
255		} else {
256			errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
257				csio->ccb_h.target_id,
258				(char *)&vpo->vpo_sense.cmd,
259				sizeof(vpo->vpo_sense.cmd),
260				(char *)&csio->sense_data, csio->sense_len,
261				&vpo->vpo_sense.stat, &vpo->vpo_sense.count,
262				&vpo->vpo_error);
263		}
264
265
266#ifdef VP0_DEBUG
267		printf("(sense) vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
268			errno, vpo->vpo_sense.stat, vpo->vpo_sense.count, vpo->vpo_error);
269#endif
270
271		/* check sense return status */
272		if (errno == 0 && vpo->vpo_sense.stat == SCSI_STATUS_OK) {
273		   /* sense ok */
274		   csio->ccb_h.status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR;
275		   csio->sense_resid = csio->sense_len - vpo->vpo_sense.count;
276
277#ifdef VP0_DEBUG
278		   /* dump of sense info */
279		   printf("(sense) ");
280		   for (i=0; i<vpo->vpo_sense.count; i++)
281			printf("%x ", ((char *)&csio->sense_data)[i]);
282		   printf("\n");
283#endif
284
285		} else {
286		   /* sense failed */
287		   csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
288		}
289	   } else {
290		/* no sense */
291		csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
292	   }
293
294	   return;
295	}
296
297	csio->resid = csio->dxfer_len - vpo->vpo_count;
298	csio->ccb_h.status = CAM_REQ_CMP;
299}
300
301static void
302vpo_action(struct cam_sim *sim, union ccb *ccb)
303{
304	struct vpo_data *vpo = (struct vpo_data *)sim->softc;
305
306	ppb_assert_locked(device_get_parent(vpo->vpo_dev));
307	switch (ccb->ccb_h.func_code) {
308	case XPT_SCSI_IO:
309	{
310		struct ccb_scsiio *csio;
311
312		csio = &ccb->csio;
313
314		if (ccb->ccb_h.flags & CAM_CDB_PHYS) {
315			ccb->ccb_h.status = CAM_REQ_INVALID;
316			xpt_done(ccb);
317			break;
318		}
319#ifdef VP0_DEBUG
320		device_printf(vpo->vpo_dev, "XPT_SCSI_IO (0x%x) request\n",
321		    *scsiio_cdb_ptr(csio));
322#endif
323		vpo_intr(vpo, csio);
324
325		xpt_done(ccb);
326
327		break;
328	}
329	case XPT_CALC_GEOMETRY:
330	{
331		struct	  ccb_calc_geometry *ccg;
332
333		ccg = &ccb->ccg;
334
335#ifdef VP0_DEBUG
336		device_printf(vpo->vpo_dev, "XPT_CALC_GEOMETRY (bs=%d,vs=%jd,c=%d,h=%d,spt=%d) request\n",
337			ccg->block_size,
338			(intmax_t)ccg->volume_size,
339			ccg->cylinders,
340			ccg->heads,
341			ccg->secs_per_track);
342#endif
343
344		ccg->heads = 64;
345		ccg->secs_per_track = 32;
346		ccg->cylinders = ccg->volume_size /
347				 (ccg->heads * ccg->secs_per_track);
348
349		ccb->ccb_h.status = CAM_REQ_CMP;
350		xpt_done(ccb);
351		break;
352	}
353	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
354	{
355
356#ifdef VP0_DEBUG
357		device_printf(vpo->vpo_dev, "XPT_RESET_BUS request\n");
358#endif
359
360		if (vpo->vpo_isplus) {
361			if (imm_reset_bus(&vpo->vpo_io)) {
362				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
363				xpt_done(ccb);
364				return;
365			}
366		} else {
367			if (vpoio_reset_bus(&vpo->vpo_io)) {
368				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
369				xpt_done(ccb);
370				return;
371			}
372		}
373
374		ccb->ccb_h.status = CAM_REQ_CMP;
375		xpt_done(ccb);
376		break;
377	}
378	case XPT_PATH_INQ:		/* Path routing inquiry */
379	{
380		struct ccb_pathinq *cpi = &ccb->cpi;
381
382#ifdef VP0_DEBUG
383		device_printf(vpo->vpo_dev, "XPT_PATH_INQ request\n");
384#endif
385		cpi->version_num = 1; /* XXX??? */
386		cpi->hba_inquiry = 0;
387		cpi->target_sprt = 0;
388		cpi->hba_misc = 0;
389		cpi->hba_eng_cnt = 0;
390		cpi->max_target = 7;
391		cpi->max_lun = 0;
392		cpi->initiator_id = VP0_INITIATOR;
393		cpi->bus_id = sim->bus_id;
394		cpi->base_transfer_speed = 93;
395		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
396		strlcpy(cpi->hba_vid, "Iomega", HBA_IDLEN);
397		strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
398		cpi->unit_number = sim->unit_number;
399		cpi->transport = XPORT_PPB;
400		cpi->transport_version = 0;
401
402		cpi->ccb_h.status = CAM_REQ_CMP;
403		xpt_done(ccb);
404		break;
405	}
406	default:
407		ccb->ccb_h.status = CAM_REQ_INVALID;
408		xpt_done(ccb);
409		break;
410	}
411
412	return;
413}
414
415static void
416vpo_poll(struct cam_sim *sim)
417{
418
419	/* The ZIP is actually always polled throw vpo_action(). */
420}
421
422static devclass_t vpo_devclass;
423
424static device_method_t vpo_methods[] = {
425	/* device interface */
426	DEVMETHOD(device_identify,	vpo_identify),
427	DEVMETHOD(device_probe,		vpo_probe),
428	DEVMETHOD(device_attach,	vpo_attach),
429
430	{ 0, 0 }
431};
432
433static driver_t vpo_driver = {
434	"vpo",
435	vpo_methods,
436	sizeof(struct vpo_data),
437};
438DRIVER_MODULE(vpo, ppbus, vpo_driver, vpo_devclass, 0, 0);
439MODULE_DEPEND(vpo, ppbus, 1, 1, 1);
440MODULE_DEPEND(vpo, cam, 1, 1, 1);
441